Commit 534d5b78bf for ffmpeg
commit 534d5b78bf1bb9a730528d04a3b67f12705b1fdd
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date: Sat Nov 21 15:00:52 2026 -0800
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 <baptiste.coudurier@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
diff --git a/libavformat/mov.c b/libavformat/mov.c
index ba9698e73a..785b7697df 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 ff24578da4..62f7409237 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -5186,6 +5186,60 @@ static int mov_write_mdta_keys_tag(AVIOContext *pb, MOVMuxContext *mov,
return update_size(pb, pos);
}
+/* Numeric keys keep their type, even for values outside their documented range.
+ * location.role and rating.user are from the QuickTime File Format, the others
+ * as in Apple files, e.g. the 8-byte type 21 of iPhone Live Photos.
+ * full-frame-rate-playback-intent is written as Compressor does, some iPhone
+ * recordings use type 21 with 8 bytes. */
+static int mov_write_mdta_data_tag(AVIOContext *pb, const char *key, const char *value)
+{
+ 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;
+
+ if (strcmp(key, numeric[i].key))
+ continue;
+ if (type == 23) {
+ float f;
+ int len = 0;
+ if (av_sscanf(value, "%f%n", &f, &len) != 1 || value[len] || !isfinite(f))
+ break;
+ /* the sign of -0 is lost with -fno-signed-zeros */
+ v = av_float2int(f) | (value[strspn(value, " \t\n\v\f\r")] == '-' ? 0x80000000 : 0);
+ } 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], *end;
+ 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)
{
@@ -5200,7 +5254,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 6e29524d2d..5e7cb8f02f 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -537,6 +537,29 @@ fate-mov-yuv2-color-colr: CMD = run_with_temp \
fate-mov-yuv2-color-range: CMD = run_with_temp \
"$(call MOV_YUV2_GEN,$(MOV_YUV2_SRC))" "$(MOV_YUV2_PROBE) -color_range tv" mov
+# 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=north \
+ -metadata com.apple.quicktime.rating.user=-0 \
+ -f mov $(TARGET_PATH)/$@ -y
+
+fate-mov-mdta-numeric: tests/data/mov-mdta-numeric.mov
+fate-mov-mdta-numeric: CMD = stream_remux mov 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 0000000000..b6552e3594
--- /dev/null
+++ b/tests/ref/fate/mov-mdta-numeric
@@ -0,0 +1,20 @@
+b99988f76ef12b0f73e1fad4c64d60fc *tests/data/fate/mov-mdta-numeric.mov
+1337 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=-0.000000
+TAG:com.apple.quicktime.location.role=north
+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]