From: anthonybajoua via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: anthonybajoua <code@ffmpeg.org>
Subject: [FFmpeg-devel] [PR] libavformat/movenc: Uses dynamic buffers for fragmented chunks (PR #21614)
Date: Fri, 30 Jan 2026 23:28:51 -0000
Message-ID: <176981573239.25.6846065537294616292@4457048688e7> (raw)
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
reply other threads:[~2026-01-30 23:29 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=176981573239.25.6846065537294616292@4457048688e7 \
--to=ffmpeg-devel@ffmpeg.org \
--cc=code@ffmpeg.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
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