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

The demuxer did not read the 3GPP TS 26.244 user data boxes `titl`, `perf`, 
`auth`, `dscp`, `albm` and `yrrc`, and parsed `cprt` and `gnre` with the 
QuickTime/iTunes layouts. Title, artist etc. of a 3gp written by `ffmpeg 
-metadata ...` were lost on the way back through ffprobe.

These boxes are now read as full boxes (version/flags 0, packed ISO-639-2/T 
language, NUL-terminated UTF-8 or UTF-16 string with BOM) and mapped to the 
keys the muxer uses. The `albm` optional track number byte goes to `track`, 
`yrrc` to `date`. The language is exported like `loci`: the plain key, plus 
`key-lang` unless it is "und". Boxes with nonzero version/flags, and those 
inside `ilst`, keep the old path.

Tested:
- ffmpeg-written 3gp round-trips all tags with their languages; new FATE test 
`fate-mov-3gp-udta-metadata`.
- 3gp files written by AVFoundation (export session and AVAssetWriter) read 
correctly, including UTF-16 BE/LE strings and an `albm` with a track number.
- Full FATE passes, no other ref changes.

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



From 552ce26faa7f24f82e13a29e52cb2d43731dd6c0 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Thu, 12 Nov 2026 15:03:19 -0800
Subject: [PATCH] avformat/mov: read the 3GPP udta string boxes

titl, perf, auth, dscp, albm, yrrc were dropped and cprt, gnre misparsed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/mov.c                    | 74 ++++++++++++++++++++++++++++
 tests/fate/mov.mak                   |  8 +++
 tests/ref/fate/mov-3gp-udta-metadata | 30 +++++++++++
 3 files changed, 112 insertions(+)
 create mode 100644 tests/ref/fate/mov-3gp-udta-metadata

diff --git a/libavformat/mov.c b/libavformat/mov.c
index ca33cb2285b..fe1cfe5eb27 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -331,6 +331,61 @@ static int mov_metadata_loci(MOVContext *c, AVIOContext 
*pb, unsigned len)
     return av_dict_set(&c->fc->metadata, key, buf, 0);
 }
 
+// 3GPP TS 26.244 titl, perf, auth, gnre, dscp, albm, cprt, yrrc
+static int mov_metadata_3gp(MOVContext *c, AVIOContext *pb, int64_t len,
+                            const char *key, AVDictionary **metadata)
+{
+    char language[4] = { 0 };
+    char *str;
+    int bom, ret;
+
+    len -= 4; // version+flags, checked by the caller
+
+    if (!strcmp(key, "date")) {
+        c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
+        return av_dict_set_int(metadata, key, avio_rb16(pb), 0);
+    }
+
+    if (len < 3 || len >= INT_MAX / 2)
+        return 0;
+    ff_mov_lang_to_iso639(avio_rb16(pb), language);
+    len -= 2;
+
+    str = av_malloc(len * 2 + 1);
+    if (!str)
+        return AVERROR(ENOMEM);
+    bom = avio_rb16(pb);
+    if (bom == 0xFEFF) {
+        len -= 2 + avio_get_str16be(pb, len - 2, str, len * 2 + 1);
+    } else if (bom == 0xFFFE) {
+        len -= 2 + avio_get_str16le(pb, len - 2, str, len * 2 + 1);
+    } else {
+        avio_seek(pb, -2, SEEK_CUR);
+        len -= avio_get_str(pb, len, str, len * 2 + 1);
+    }
+
+    c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
+    if (*language && strcmp(language, "und")) {
+        char key2[32];
+        snprintf(key2, sizeof(key2), "%s-%s", key, language);
+        ret = av_dict_set(metadata, key2, str, 0);
+        if (ret < 0) {
+            av_free(str);
+            return ret;
+        }
+    }
+    ret = av_dict_set(metadata, key, str, AV_DICT_DONT_STRDUP_VAL);
+    if (ret < 0)
+        return ret;
+
+    if (!strcmp(key, "album") && len > 0) {
+        int track = avio_r8(pb);
+        if (track)
+            ret = av_dict_set_int(metadata, "track", track, 0);
+    }
+    return ret;
+}
+
 static int mov_metadata_hmmt(MOVContext *c, AVIOContext *pb, unsigned len)
 {
     int i, n_hmmt;
@@ -371,6 +426,25 @@ static int mov_read_udta_string(MOVContext *c, AVIOContext 
*pb, MOVAtom atom)
     else
         metadata = &c->fc->metadata;
 
+    if (!c->itunes_metadata) {
+        switch (atom.type) {
+        case MKTAG( 'a','l','b','m'): key = "album";     break;
+        case MKTAG( 'a','u','t','h'): key = "author";    break;
+        case MKTAG( 'c','p','r','t'): key = "copyright"; break;
+        case MKTAG( 'd','s','c','p'): key = "comment";   break;
+        case MKTAG( 'g','n','r','e'): key = "genre";     break;
+        case MKTAG( 'p','e','r','f'): key = "artist";    break;
+        case MKTAG( 't','i','t','l'): key = "title";     break;
+        case MKTAG( 'y','r','r','c'): key = "date";      break;
+        }
+        if (key && atom.size >= 6) {
+            if (!avio_rb32(pb)) // version+flags
+                return mov_metadata_3gp(c, pb, atom.size, key, metadata);
+            avio_seek(pb, -4, SEEK_CUR);
+        }
+        key = NULL;
+    }
+
     switch (atom.type) {
     case MKTAG( '@','P','R','M'): key = "premiere_version"; raw = 1; break;
     case MKTAG( '@','P','R','Q'): key = "quicktime_version"; raw = 1; break;
diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index 01b1346cec5..fd10a126e2d 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -428,6 +428,14 @@ fate-mov-composer-metadata: CMD = transcode wav 
./tests/data/asynth-44100-2.wav
     "-af aresample -c:a pcm_s16le -t 0.05 -metadata composer=Bach" \
     "-af aresample -c:a pcm_s16le" "-show_entries format_tags"
 
+# The 3GPP TS 26.244 user data boxes have to be read back with their languages.
+FATE_MOV_FFMPEG_FFPROBE-$(call TRANSCODE, MPEG4, TGP MOV, LAVFI_INDEV 
TESTSRC_FILTER FORMAT_FILTER) += fate-mov-3gp-udta-metadata
+fate-mov-3gp-udta-metadata: CMD = transcode lavfi 
"testsrc=s=32x32:r=1:d=1,format=yuv420p" 3gp \
+    "-c:v mpeg4 -metadata title=Title -metadata artist=Artist -metadata 
author=Author \
+     -metadata genre=Genre -metadata comment=Comment -metadata album=Album 
-metadata track=7 \
+     -metadata copyright=Copyright -metadata date=2024" \
+    "-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-3gp-udta-metadata 
b/tests/ref/fate/mov-3gp-udta-metadata
new file mode 100644
index 00000000000..c46ad7584cd
--- /dev/null
+++ b/tests/ref/fate/mov-3gp-udta-metadata
@@ -0,0 +1,30 @@
+bcfbe0053b282becfbbb6bde0da916b5 *tests/data/fate/mov-3gp-udta-metadata.3gp
+2080 tests/data/fate/mov-3gp-udta-metadata.3gp
+#extradata 0:       30, 0x4299048c
+#tb 0: 1/16384
+#media_type 0: video
+#codec_id 0: mpeg4
+#dimensions 0: 32x32
+#sar 0: 1/1
+0,          0,          0,    16384,     1167, 0xd0a407c5
+[FORMAT]
+TAG:major_brand=3gp4
+TAG:minor_version=512
+TAG:compatible_brands=3gp4isomiso2
+TAG:artist-eng=Artist
+TAG:artist=Artist
+TAG:title-eng=Title
+TAG:title=Title
+TAG:author-eng=Author
+TAG:author=Author
+TAG:genre-eng=Genre
+TAG:genre=Genre
+TAG:comment-eng=Comment
+TAG:comment=Comment
+TAG:album-eng=Album
+TAG:album=Album
+TAG:track=7
+TAG:copyright-eng=Copyright
+TAG:copyright=Copyright
+TAG:date=2024
+[/FORMAT]
-- 
2.54.0

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

Reply via email to