Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PR] libavformat/movenc: Uses dynamic buffers for fragmented chunks (PR #21614)
@ 2026-01-30 23:28 anthonybajoua via ffmpeg-devel
  0 siblings, 0 replies; only message in thread
From: anthonybajoua via ffmpeg-devel @ 2026-01-30 23:28 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: anthonybajoua

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 <anthonybajoua@meta.com>
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 -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-01-30 23:29 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-01-30 23:28 [FFmpeg-devel] [PR] libavformat/movenc: Uses dynamic buffers for fragmented chunks (PR #21614) anthonybajoua via ffmpeg-devel

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git