Commit ff4960fd5a for ffmpeg

commit ff4960fd5a97c071de2354b3807365127638fe79
Author: Michael Niedermayer <michael@niedermayer.cc>
Date:   Fri Nov 20 14:29:08 2026 +0000

    avformat/movenc: buffer the initial moov on nonseekable output

    Its sizes could not be patched once it outgrew the output buffer,
    e.g. with large metadata, leaving an unreadable file. Return the
    buffer errors, including the nested udta ones.

    Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
    Assisted-by: Fairy
    Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index c8b460f7d7..f91a9272e6 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -4716,7 +4716,7 @@ static int mov_write_track_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
     int ret, size;
     uint8_t *buf;

-    if (!st)
+    if (!st || !(mov->mode & (MODE_MP4 | MODE_MOV)))
         return 0;

     ret = avio_open_dyn_buf(&pb_buf);
@@ -4794,12 +4794,11 @@ static int mov_write_trak_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext
             mov_write_tapt_tag(pb, track);
         }
     }
-    if ((ret = mov_write_track_udta_tag(pb, mov, st)) < 0)
-        return ret;
+    ret = mov_write_track_udta_tag(pb, mov, st);
     track->entry = entry_backup;
     track->chunkCount = chunk_backup;
     track->sample_count = sample_count_backup;
-    return update_size(pb, pos);
+    return ret < 0 ? ret : update_size(pb, pos);
 }

 static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
@@ -5873,6 +5872,32 @@ static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
     return update_size(pb, pos);
 }

+static int mov_write_initial_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
+                                      AVFormatContext *s)
+{
+    AVIOContext *buf;
+    uint8_t *data;
+    int ret, size;
+
+    if (pb->seekable & AVIO_SEEKABLE_NORMAL)
+        return mov_write_moov_tag(pb, mov, s);
+
+    /* its sizes cannot be patched once it exceeds the output buffer */
+    ret = avio_open_dyn_buf(&buf);
+    if (ret < 0)
+        return ret;
+    ret = mov_write_moov_tag(buf, mov, s);
+    if (ret >= 0) {
+        size = mov_get_dyn_buf(buf, &data);
+        if (size < 0)
+            ret = size;
+        else
+            avio_write(pb, data, size);
+    }
+    ffio_free_dyn_buf(&buf);
+    return ret;
+}
+
 static void param_write_int(AVIOContext *pb, const char *name, int value)
 {
     avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
@@ -7298,7 +7323,7 @@ static int mov_flush_fragment_internal(AVFormatContext *s, int force)
         if (mov->flags & FF_MOV_FLAG_DELAY_MOOV &&
             !(mov->flags & FF_MOV_FLAG_HYBRID_FRAGMENTED))
             mov_write_identification(s->pb, s);
-        if ((ret = mov_write_moov_tag(s->pb, mov, s)) < 0)
+        if ((ret = mov_write_initial_moov_tag(s->pb, mov, s)) < 0)
             return ret;
         if (mov->moov_end)
             *mov->moov_end = avio_tell(s->pb);
@@ -9818,7 +9843,7 @@ static int mov_write_header(AVFormatContext *s)

     if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV &&
         !(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
-        if ((ret = mov_write_moov_tag(pb, mov, s)) < 0)
+        if ((ret = mov_write_initial_moov_tag(pb, mov, s)) < 0)
             return ret;
         mov->moov_written = 1;
         if (mov->moov_end)
diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index ed62a8d640..217d7d5e74 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -586,6 +586,15 @@ fate-mov-mp4-loci-no-altitude: CMD = transcode wav ./tests/data/asynth-44100-2.w
     "-af aresample -c:a pcm_s16le -t 0.05 -metadata location=-33.8568+151.2153/" \
     "-af aresample -c:a pcm_s16le" "-show_entries format_tags=location"

+# A moov larger than the output buffer has to be written whole on nonseekable output.
+tests/data/mov-large-comment: | tests/data
+	$(M)awk 'BEGIN { printf "comment="; for (i = 0; i < 33000; i++) printf "x" }' > $@
+
+FATE_MOV_FFMPEG-$(call ALLYES, COLOR_FILTER WRAPPED_AVFRAME_DECODER MPEG4_ENCODER MOV_MUXER MD5_PROTOCOL FILE_PROTOCOL) += fate-mov-frag-large-comment
+fate-mov-frag-large-comment: tests/data/mov-large-comment
+fate-mov-frag-large-comment: CMD = md5pipe -filter_complex color=s=2x2:r=1 -frames:v 1 -c:v mpeg4 -threads:v 1 -bitexact \
+    -/metadata $(TARGET_PATH)/tests/data/mov-large-comment -movflags empty_moov+frag_keyframe -f mov
+
 # 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 COLOR_FILTER WRAPPED_AVFRAME_DECODER) += fate-mov-3gp-udta-metadata
 fate-mov-3gp-udta-metadata: CMD = transcode lavfi "color=s=32x32:r=1:d=1" 3gp \
diff --git a/tests/ref/fate/mov-frag-large-comment b/tests/ref/fate/mov-frag-large-comment
new file mode 100644
index 0000000000..e2951fafe3
--- /dev/null
+++ b/tests/ref/fate/mov-frag-large-comment
@@ -0,0 +1 @@
+60283556df08611133045caa7be694af