PR #21614 opened by anthonybajoua
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21614
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21614.patch

# Problem:
There are frequent 
[realloc](https://pubs.opengroup.org/onlinepubs/7908799/xsh/realloc.html) calls 
implemented when using hybrid/fragmented MP4 in chunking. This can thrash 
memory, cause heap fragmentation.


[It appears this was done for 
MKV](https://github.com/FFmpeg/FFmpeg/commit/4d97b2ad2fa6d851c70fd982ab300e4fd559f1d0)
 but hybrid/fragmented MP4 notably have it missing.



# Solution:
Uses `dyn_buf` directives in managing fragmented, hybrid MP4 memory


## Automated
```
./configure
make
make fate-rsync SAMPLES=fate-suite
make
```

## Manual:


```
curl 
https://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4 
-o bbb.mp4

./ffmpeg -i bbb.mp4 -c copy -movflags hybrid_fragmented+delay_moov out.mp4

./ffprobe out.mp4
```


>From 4b17d85223baa156723e826460a8575f9f600129 Mon Sep 17 00:00:00 2001
From: Anthony Bajoua <[email protected]>
Date: Fri, 30 Jan 2026 13:43:03 -0800
Subject: [PATCH] libavformat/movenc: Uses dynamic buffers for fragmented
 chunks

---
 libavformat/movenc.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 802c37fc4a..fe6b259561 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -6391,7 +6391,7 @@ static int 
mov_flush_fragment_interleaving(AVFormatContext *s, MOVTrack *track)
 
     offset = avio_tell(mov->mdat_buf);
     avio_write(mov->mdat_buf, buf, buf_size);
-    ffio_free_dyn_buf(&track->mdat_buf);
+    ffio_reset_dyn_buf(track->mdat_buf);
 
     for (i = track->entries_flushed; i < track->entry; i++)
         track->cluster[i].pos += offset;
@@ -6596,7 +6596,7 @@ static int mov_flush_fragment(AVFormatContext *s, int 
force)
         avio_wb32(s->pb, buf_size + 8);
         ffio_wfourcc(s->pb, "mdat");
         avio_write(s->pb, buf, buf_size);
-        ffio_free_dyn_buf(&mov->mdat_buf);
+        ffio_reset_dyn_buf(mov->mdat_buf);
 
         if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
             mov->reserved_header_pos = avio_tell(s->pb);
@@ -6683,17 +6683,16 @@ static int mov_flush_fragment(AVFormatContext *s, int 
force)
         if (!mov->frag_interleave) {
             if (!track->mdat_buf)
                 continue;
-            buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
-            track->mdat_buf = NULL;
+            buf_size = avio_get_dyn_buf(track->mdat_buf, &buf);
+            avio_write(s->pb, buf, buf_size);
+            ffio_reset_dyn_buf(track->mdat_buf);
         } else {
             if (!mov->mdat_buf)
                 continue;
-            buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
-            mov->mdat_buf = NULL;
+            buf_size = avio_get_dyn_buf(mov->mdat_buf, &buf);
+            avio_write(s->pb, buf, buf_size);
+            ffio_reset_dyn_buf(mov->mdat_buf);
         }
-
-        avio_write(s->pb, buf, buf_size);
-        av_free(buf);
     }
 
     mov->mdat_size = 0;
-- 
2.52.0

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

Reply via email to