PR #26267 opened by Baptiste Coudurier (bcoudurier) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26267 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26267.patch
Depends on https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26254 (its commit is included here); review only the last commit. float32 metadata was printed with %f, so 3effffff (a Live Photo vitality score just under 0.5) became 0.500000 and was written back as 3f000000. Now %.9g on read, strtof() on write. 🤖 Generated with [Claude Code](https://claude.com/claude-code) From 39b569ecc9d68f6048e12abba769d6c41d856cf8 Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Sat, 21 Nov 2026 15:00:52 -0800 Subject: [PATCH 1/2] avformat/mov,movenc: keep the type of numeric QuickTime metadata 8-byte integers, e.g. the Live Photo vitality scoring version, were read as 0, and use_metadata_tags wrote all values as strings. Signed-off-by: Baptiste Coudurier <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> --- libavformat/mov.c | 18 ++++++----- libavformat/movenc.c | 55 ++++++++++++++++++++++++++++++++- tests/fate/mov.mak | 23 ++++++++++++++ tests/ref/fate/mov-mdta-numeric | 20 ++++++++++++ 4 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 tests/ref/fate/mov-mdta-numeric diff --git a/libavformat/mov.c b/libavformat/mov.c index 6d31dc729ad..07f3ac816cf 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -585,7 +585,7 @@ retry: if (atom.size < 0 || str_size >= INT_MAX/2) return AVERROR_INVALIDDATA; - // Allocates enough space if data_type is a int32 or float32 number, otherwise + // Allocates enough space if data_type is an integer or float32 number, otherwise // worst-case requirement for output string in case of utf8 coded input num = (data_type >= 21 && data_type <= 23); str_size_alloc = (num ? 512 : (raw ? str_size : str_size * 2)) + 1; @@ -599,7 +599,7 @@ retry: if (!raw && (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff)))) { // MAC Encoded mov_read_mac_string(c, pb, str_size, str, str_size_alloc); } else if (data_type == 21) { // BE signed integer, variable size - int val = 0; + int64_t val = 0; if (str_size == 1) val = (int8_t)avio_r8(pb); else if (str_size == 2) @@ -608,14 +608,16 @@ retry: val = ((int32_t)(avio_rb24(pb)<<8))>>8; else if (str_size == 4) val = (int32_t)avio_rb32(pb); - if (snprintf(str, str_size_alloc, "%d", val) >= str_size_alloc) { + else if (str_size == 8) + val = (int64_t)avio_rb64(pb); + if (snprintf(str, str_size_alloc, "%"PRId64, val) >= str_size_alloc) { av_log(c->fc, AV_LOG_ERROR, - "Failed to store the number (%d) in string.\n", val); + "Failed to store the number (%"PRId64") in string.\n", val); av_free(str); return AVERROR_INVALIDDATA; } } else if (data_type == 22) { // BE unsigned integer, variable size - unsigned int val = 0; + uint64_t val = 0; if (str_size == 1) val = avio_r8(pb); else if (str_size == 2) @@ -624,9 +626,11 @@ retry: val = avio_rb24(pb); else if (str_size == 4) val = avio_rb32(pb); - if (snprintf(str, str_size_alloc, "%u", val) >= str_size_alloc) { + else if (str_size == 8) + val = avio_rb64(pb); + if (snprintf(str, str_size_alloc, "%"PRIu64, val) >= str_size_alloc) { av_log(c->fc, AV_LOG_ERROR, - "Failed to store the number (%u) in string.\n", val); + "Failed to store the number (%"PRIu64") in string.\n", val); av_free(str); return AVERROR_INVALIDDATA; } diff --git a/libavformat/movenc.c b/libavformat/movenc.c index f302a366129..5dc0b366463 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -23,6 +23,7 @@ #include "config_components.h" +#include <float.h> #include <math.h> #include <stdint.h> #include <inttypes.h> @@ -5169,6 +5170,58 @@ static int mov_write_mdta_keys_tag(AVIOContext *pb, MOVMuxContext *mov, return update_size(pb, pos); } +/* The numeric QuickTime keys are written as numbers when their value is one */ +static int mov_write_mdta_data_tag(AVIOContext *pb, const char *key, const char *value) +{ + /* location.role and rating.user are from the QuickTime File Format, the + * others as found in Apple files: iPhone Live Photos use an 8-byte type 21 + * for the scoring version, beyond the 1 to 4 bytes the spec lists. */ + static const struct { + const char *key; + int type; /* 21: BE signed, 22: BE unsigned, 23: BE float32 */ + int size; + } numeric[] = { + { "com.apple.quicktime.full-frame-rate-playback-intent", 22, 1 }, + { "com.apple.quicktime.live-photo.auto", 22, 1 }, + { "com.apple.quicktime.live-photo.vitality-score", 23, 4 }, + { "com.apple.quicktime.live-photo.vitality-scoring-version", 21, 8 }, + { "com.apple.quicktime.location.role", 22, 1 }, + { "com.apple.quicktime.rating.user", 23, 4 }, + }; + + for (int i = 0; i < FF_ARRAY_ELEMS(numeric); i++) { + int type = numeric[i].type, size = numeric[i].size; + int64_t v; + char *end; + + if (strcmp(key, numeric[i].key)) + continue; + if (type == 23) { + double d = strtod(value, &end); + if (end == value || *end || !(fabs(d) <= FLT_MAX)) + break; + v = av_float2int(d); + } else { + int64_t max = size == 8 ? INT64_MAX : + (INT64_C(1) << (8 * size - (type == 21))) - 1; + int64_t min = type == 22 ? 0 : -max - 1; + char buf[32]; + v = strtoll(value, &end, 10); + snprintf(buf, sizeof(buf), "%"PRId64, v); + if (strcmp(buf, value) || v < min || v > max) + break; + } + avio_wb32(pb, 16 + size); /* size */ + ffio_wfourcc(pb, "data"); + avio_wb32(pb, type); + avio_wb32(pb, 0); + for (int b = size - 1; b >= 0; b--) + avio_w8(pb, v >> (8 * b) & 0xff); + return 16 + size; + } + return mov_write_string_data_tag(pb, value, 0, 1); +} + static int mov_write_mdta_ilst_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s) { @@ -5183,7 +5236,7 @@ static int mov_write_mdta_ilst_tag(AVIOContext *pb, MOVMuxContext *mov, int64_t entry_pos = avio_tell(pb); avio_wb32(pb, 0); /* size */ avio_wb32(pb, count); /* key */ - mov_write_string_data_tag(pb, t->value, 0, 1); + mov_write_mdta_data_tag(pb, t->key, t->value); update_size(pb, entry_pos); count += 1; } diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak index df622d85c47..d8c069c22da 100644 --- a/tests/fate/mov.mak +++ b/tests/fate/mov.mak @@ -476,6 +476,29 @@ fate-mov-3gp-udta-metadata: CMD = transcode lavfi "color=s=32x32:r=1:d=1" 3gp \ -metadata copyright-eng=Copyright" \ "-c copy" "-show_entries format_tags" +# The numeric QuickTime keys keep their type through a remux, a non-number +# stays a string. +FATE_MOV_FFMPEG_FFPROBE-$(call ALLYES, COLOR_FILTER MPEG4_ENCODER MOV_MUXER \ + MOV_DEMUXER FRAMECRC_MUXER \ + FILE_PROTOCOL PIPE_PROTOCOL) \ + += fate-mov-mdta-numeric +tests/data/mov-mdta-numeric.mov: TAG = GEN +tests/data/mov-mdta-numeric.mov: ffmpeg$(PROGSSUF)$(EXESUF) | tests/data + $(M)$(TARGET_EXEC) $(TARGET_PATH)/$< -nostdin -v error \ + -filter_complex color=c=black:s=2x2:r=1 -frames:v 1 -c:v mpeg4 -threads 1 \ + -flags +bitexact -fflags +bitexact -movflags use_metadata_tags \ + -metadata com.apple.quicktime.full-frame-rate-playback-intent=1 \ + -metadata com.apple.quicktime.live-photo.auto=1 \ + -metadata com.apple.quicktime.live-photo.vitality-score=0.918638 \ + -metadata com.apple.quicktime.live-photo.vitality-scoring-version=4294967296 \ + -metadata com.apple.quicktime.location.role=2 \ + -metadata com.apple.quicktime.rating.user=fourstars \ + -f mov $(TARGET_PATH)/$@ -y + +fate-mov-mdta-numeric: tests/data/mov-mdta-numeric.mov +fate-mov-mdta-numeric: CMD = stream_remux mov $(TARGET_PATH)/tests/data/mov-mdta-numeric.mov "" \ + mov "-map 0 -movflags use_metadata_tags" "" "-c copy" "-show_entries format_tags" + FATE_MOV_FFMPEG_FFPROBE-$(call ALLYES, COLOR_FILTER SETPTS_FILTER MPEG4_ENCODER \ MOV_MUXER MOV_DEMUXER FILE_PROTOCOL) \ += fate-mov-vfr-bframes-derived-duration diff --git a/tests/ref/fate/mov-mdta-numeric b/tests/ref/fate/mov-mdta-numeric new file mode 100644 index 00000000000..e586a35fe80 --- /dev/null +++ b/tests/ref/fate/mov-mdta-numeric @@ -0,0 +1,20 @@ +f088f2eb3022045767d9731c03f61163 *tests/data/fate/mov-mdta-numeric.mov +1338 tests/data/fate/mov-mdta-numeric.mov +#extradata 0: 30, 0x434804d7 +#tb 0: 1/16384 +#media_type 0: video +#codec_id 0: mpeg4 +#dimensions 0: 2x2 +#sar 0: 1/1 +0, 0, 0, 16384, 17, 0x13820331 +[FORMAT] +TAG:major_brand=qt +TAG:minor_version=512 +TAG:compatible_brands=qt +TAG:com.apple.quicktime.rating.user=fourstars +TAG:com.apple.quicktime.location.role=2 +TAG:com.apple.quicktime.live-photo.vitality-scoring-version=4294967296 +TAG:com.apple.quicktime.full-frame-rate-playback-intent=1 +TAG:com.apple.quicktime.live-photo.auto=1 +TAG:com.apple.quicktime.live-photo.vitality-score=0.918638 +[/FORMAT] -- 2.54.1 From 6f5e9a651fa5ee7984c7e2b4fdf7164bd10588a0 Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Sat, 21 Nov 2026 15:04:45 -0800 Subject: [PATCH 2/2] avformat/mov,movenc: keep float32 metadata exact %f rounded e.g. 3effffff to 0.500000, written back as 3f000000. Signed-off-by: Baptiste Coudurier <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> --- libavformat/mov.c | 4 ++-- libavformat/movenc.c | 7 +++---- tests/fate/mov.mak | 4 ++-- tests/ref/fate/mov-mdta-numeric | 4 ++-- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/libavformat/mov.c b/libavformat/mov.c index 07f3ac816cf..1918c0287b2 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -636,9 +636,9 @@ retry: } } else if (data_type == 23 && str_size >= 4) { // BE float32 float val = av_int2float(avio_rb32(pb)); - if (snprintf(str, str_size_alloc, "%f", val) >= str_size_alloc) { + if (snprintf(str, str_size_alloc, "%.9g", val) >= str_size_alloc) { av_log(c->fc, AV_LOG_ERROR, - "Failed to store the float32 number (%f) in string.\n", val); + "Failed to store the float32 number (%.9g) in string.\n", val); av_free(str); return AVERROR_INVALIDDATA; } diff --git a/libavformat/movenc.c b/libavformat/movenc.c index 5dc0b366463..b7fdeeb1fe7 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -23,7 +23,6 @@ #include "config_components.h" -#include <float.h> #include <math.h> #include <stdint.h> #include <inttypes.h> @@ -5197,10 +5196,10 @@ static int mov_write_mdta_data_tag(AVIOContext *pb, const char *key, const char if (strcmp(key, numeric[i].key)) continue; if (type == 23) { - double d = strtod(value, &end); - if (end == value || *end || !(fabs(d) <= FLT_MAX)) + float f = strtof(value, &end); + if (end == value || *end || !isfinite(f)) break; - v = av_float2int(d); + v = av_float2int(f); } else { int64_t max = size == 8 ? INT64_MAX : (INT64_C(1) << (8 * size - (type == 21))) - 1; diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak index d8c069c22da..770540b71cf 100644 --- a/tests/fate/mov.mak +++ b/tests/fate/mov.mak @@ -477,7 +477,7 @@ fate-mov-3gp-udta-metadata: CMD = transcode lavfi "color=s=32x32:r=1:d=1" 3gp \ "-c copy" "-show_entries format_tags" # The numeric QuickTime keys keep their type through a remux, a non-number -# stays a string. +# stays a string. The float32 3effffff (0.49999997) must not round to 0.5. FATE_MOV_FFMPEG_FFPROBE-$(call ALLYES, COLOR_FILTER MPEG4_ENCODER MOV_MUXER \ MOV_DEMUXER FRAMECRC_MUXER \ FILE_PROTOCOL PIPE_PROTOCOL) \ @@ -489,7 +489,7 @@ tests/data/mov-mdta-numeric.mov: ffmpeg$(PROGSSUF)$(EXESUF) | tests/data -flags +bitexact -fflags +bitexact -movflags use_metadata_tags \ -metadata com.apple.quicktime.full-frame-rate-playback-intent=1 \ -metadata com.apple.quicktime.live-photo.auto=1 \ - -metadata com.apple.quicktime.live-photo.vitality-score=0.918638 \ + -metadata com.apple.quicktime.live-photo.vitality-score=0.49999997 \ -metadata com.apple.quicktime.live-photo.vitality-scoring-version=4294967296 \ -metadata com.apple.quicktime.location.role=2 \ -metadata com.apple.quicktime.rating.user=fourstars \ diff --git a/tests/ref/fate/mov-mdta-numeric b/tests/ref/fate/mov-mdta-numeric index e586a35fe80..d978c1e8a1e 100644 --- a/tests/ref/fate/mov-mdta-numeric +++ b/tests/ref/fate/mov-mdta-numeric @@ -1,4 +1,4 @@ -f088f2eb3022045767d9731c03f61163 *tests/data/fate/mov-mdta-numeric.mov +5ba3a636247af0d2d9d1b41b684cba7c *tests/data/fate/mov-mdta-numeric.mov 1338 tests/data/fate/mov-mdta-numeric.mov #extradata 0: 30, 0x434804d7 #tb 0: 1/16384 @@ -16,5 +16,5 @@ TAG:com.apple.quicktime.location.role=2 TAG:com.apple.quicktime.live-photo.vitality-scoring-version=4294967296 TAG:com.apple.quicktime.full-frame-rate-playback-intent=1 TAG:com.apple.quicktime.live-photo.auto=1 -TAG:com.apple.quicktime.live-photo.vitality-score=0.918638 +TAG:com.apple.quicktime.live-photo.vitality-score=0.49999997 [/FORMAT] -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
