Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] avformat/movenc: Fix flush fragment
@ 2025-05-22  9:37 Zhao Zhili
  2025-05-28 12:56 ` Zhao Zhili
  0 siblings, 1 reply; 2+ messages in thread
From: Zhao Zhili @ 2025-05-22  9:37 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Zhao Zhili

From: Zhao Zhili <zhilizhao@tencent.com>

The follow cmd output corrupted file before the patch:

ffmpeg -f lavfi -i color=blue,trim=duration=0.04 \
	-f lavfi -i anullsrc,atrim=duration=2 \
	-movflags +empty_moov+hybrid_fragmented \
	-frag_duration 1000000 \
	-frag_interleave 1 \
	output.mp4

1. first_track is the first track with track->entry != 0. As in the
command above, video track (track index 0) has a single frame. When
flush the second fragment, first_track is 1, the audio track.

2. write_moof = i == first_track, so write_moof is false for i = 0.

3. When mov->frag_interleave != 0, mov->mdat_buf != NULL, because
it contains audio data. So avio_write is called before write_moof,
that is, the data write before moof, and mov_finish_fragment
executed with wrong mdat_start.

4. With normal fmp4 output, the error isn't obvious. With
hybrid_fragmented, ffplay output.mp4 shows a lot of error messages.
---
 libavformat/movenc.c | 4 ++--
 tests/fate/mov.mak   | 5 +++++
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 4bc8bd1b2a..844a371808 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -6509,9 +6509,9 @@ static int mov_flush_fragment(AVFormatContext *s, int force)
         int buf_size, write_moof = 1, moof_tracks = -1;
         uint8_t *buf;
 
+        if (!track->entry)
+            continue;
         if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
-            if (!track->entry)
-                continue;
             mdat_size = avio_tell(track->mdat_buf);
             moof_tracks = i;
         } else {
diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index f7e5e52217..6ab61e9f00 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -84,6 +84,11 @@ fate-mov-ibi-elst-starts-b: CMD = framemd5 -flags +bitexact -i $(TARGET_SAMPLES)
 # Makes sure that we handle overlapping framgments
 fate-mov-frag-overlap: CMD = framemd5 -i $(TARGET_SAMPLES)/mov/frag_overlap.mp4
 
+fate-mov-mp4-frag-flush: CMD = md5 -f lavfi -i color=blue,format=rgb24,trim=duration=0.04 -f lavfi -i anullsrc,aformat=s16,atrim=duration=2 -c:v png -c:a pcm_s16le -movflags +empty_moov+hybrid_fragmented -frag_duration 1000000 -frag_interleave 1 -f mp4
+fate-mov-mp4-frag-flush: CMP = oneline
+fate-mov-mp4-frag-flush: REF = a10c0e2e2dfc120f31ca5e59e0e4392a
+FATE_MOV-$(call ALLYES, LAVFI_INDEV, COLOR_FILTER, FORMAT_FILTER, TRIM_FILTER, ANULL_FILTER, AFORMAT_FILTER, ATRIM_FILTER, PNG_ENCODER, PCM_S16LE_ENCODER, MOV_MUXER) += fate-mov-mp4-frag-flush
+
 # Makes sure that we pick the right frames according to edit list when there is no keyframe with PTS < edit list start.
 # For example, when video starts on a B-frame, and edit list starts on that B-frame too.
 # GOP structure : B B I in presentation order.
-- 
2.46.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [FFmpeg-devel] [PATCH] avformat/movenc: Fix flush fragment
  2025-05-22  9:37 [FFmpeg-devel] [PATCH] avformat/movenc: Fix flush fragment Zhao Zhili
@ 2025-05-28 12:56 ` Zhao Zhili
  0 siblings, 0 replies; 2+ messages in thread
From: Zhao Zhili @ 2025-05-28 12:56 UTC (permalink / raw)
  To: ffmpeg-devel



> On May 22, 2025, at 17:37, Zhao Zhili <quinkblack@foxmail.com> wrote:
> 
> From: Zhao Zhili <zhilizhao@tencent.com>
> 
> The follow cmd output corrupted file before the patch:
> 
> ffmpeg -f lavfi -i color=blue,trim=duration=0.04 \
> 	-f lavfi -i anullsrc,atrim=duration=2 \
> 	-movflags +empty_moov+hybrid_fragmented \
> 	-frag_duration 1000000 \
> 	-frag_interleave 1 \
> 	output.mp4
> 
> 1. first_track is the first track with track->entry != 0. As in the
> command above, video track (track index 0) has a single frame. When
> flush the second fragment, first_track is 1, the audio track.
> 
> 2. write_moof = i == first_track, so write_moof is false for i = 0.
> 
> 3. When mov->frag_interleave != 0, mov->mdat_buf != NULL, because
> it contains audio data. So avio_write is called before write_moof,
> that is, the data write before moof, and mov_finish_fragment
> executed with wrong mdat_start.
> 
> 4. With normal fmp4 output, the error isn't obvious. With
> hybrid_fragmented, ffplay output.mp4 shows a lot of error messages.
> ---
> 

Will apply soon.

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-05-28 12:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-05-22  9:37 [FFmpeg-devel] [PATCH] avformat/movenc: Fix flush fragment Zhao Zhili
2025-05-28 12:56 ` Zhao Zhili

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