PR #26254 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26254
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26254.patch

Numeric QuickTime mdta metadata was written as text with `use_metadata_tags`, 
and 8-byte integers were read as 0. With these changes, the numeric keys of an 
iPhone Live Photo keep their type and value through a stream-copy remux.

Types come from the QuickTime File Format (`location.role`, `rating.user`) and 
from iPhone and Compressor files.

🤖 Generated with [Claude Code](https://claude.com/claude-code)



From 3d25344409093104138452cd701ff53369cb5fbe Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Wed, 18 Nov 2026 15:28:11 -0800
Subject: [PATCH 1/2] avformat/movenc: write the numeric QuickTime metadata as
 numbers

With use_metadata_tags they were written as strings, e.g. the Live Photo
vitality score.

Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/movenc.c            | 52 ++++++++++++++++++++++++++++++++-
 tests/fate/mov.mak              | 11 +++++++
 tests/ref/fate/mov-mdta-numeric | 19 ++++++++++++
 3 files changed, 81 insertions(+), 1 deletion(-)
 create mode 100644 tests/ref/fate/mov-mdta-numeric

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index f302a366129..06ee5509e7e 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,55 @@ 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)
+{
+    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));
+        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 +5233,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 e8972bc3eb6..846f616f0e3 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -472,6 +472,17 @@ 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, a non-number stays a string.
+FATE_MOV_FFMPEG_FFPROBE-$(call TRANSCODE, MPEG4, MOV, LAVFI_INDEV COLOR_FILTER 
WRAPPED_AVFRAME_DECODER) += fate-mov-mdta-numeric
+fate-mov-mdta-numeric: CMD = transcode lavfi color=c=black:s=2x2:r=1 mov \
+    "-frames:v 1 -c:v mpeg4 -threads 1 -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=1.000000 \
+     -metadata com.apple.quicktime.location.role=2 \
+     -metadata com.apple.quicktime.rating.user=fourstars" \
+    "-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..a9b2717a084
--- /dev/null
+++ b/tests/ref/fate/mov-mdta-numeric
@@ -0,0 +1,19 @@
+e36508da182d65bfb8ff11896b4570fc *tests/data/fate/mov-mdta-numeric.mov
+1243 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.full-frame-rate-playback-intent=1
+TAG:com.apple.quicktime.live-photo.auto=1
+TAG:com.apple.quicktime.live-photo.vitality-score=1.000000
+TAG:com.apple.quicktime.location.role=2
+TAG:com.apple.quicktime.rating.user=fourstars
+[/FORMAT]
-- 
2.54.1


From eae827ad5cac62a658af214c8f552014cea466d1 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Wed, 18 Nov 2026 15:28:41 -0800
Subject: [PATCH 2/2] avformat/mov: read 8-byte integer metadata

iPhone files store e.g. the Live Photo vitality scoring version as one;
it was read as 0.

Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/mov.c               | 18 +++++++++++-------
 tests/fate/mov.mak              |  1 +
 tests/ref/fate/mov-mdta-numeric |  5 +++--
 3 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/libavformat/mov.c b/libavformat/mov.c
index 9ad780cde9c..1334dddf2d9 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/tests/fate/mov.mak b/tests/fate/mov.mak
index 846f616f0e3..fa806b6c651 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -479,6 +479,7 @@ fate-mov-mdta-numeric: CMD = transcode lavfi 
color=c=black:s=2x2:r=1 mov \
      -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=1.000000 \
+     -metadata com.apple.quicktime.live-photo.vitality-scoring-version=4 \
      -metadata com.apple.quicktime.location.role=2 \
      -metadata com.apple.quicktime.rating.user=fourstars" \
     "-c copy" "-show_entries format_tags"
diff --git a/tests/ref/fate/mov-mdta-numeric b/tests/ref/fate/mov-mdta-numeric
index a9b2717a084..bc2d30e48d9 100644
--- a/tests/ref/fate/mov-mdta-numeric
+++ b/tests/ref/fate/mov-mdta-numeric
@@ -1,5 +1,5 @@
-e36508da182d65bfb8ff11896b4570fc *tests/data/fate/mov-mdta-numeric.mov
-1243 tests/data/fate/mov-mdta-numeric.mov
+913453a6d7fa7a0f327f6f8fad5c169d *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
@@ -14,6 +14,7 @@ TAG:compatible_brands=qt
 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=1.000000
+TAG:com.apple.quicktime.live-photo.vitality-scoring-version=4
 TAG:com.apple.quicktime.location.role=2
 TAG:com.apple.quicktime.rating.user=fourstars
 [/FORMAT]
-- 
2.54.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to