PR #25927 opened by Baptiste Coudurier (bcoudurier) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25927 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25927.patch
Fixes #25775. Since cc929cee76, the muxer no longer selects `avoid_negative_ts=make_zero` when edit lists are disabled and `negative_cts_offsets` is set. Each track then starts at 0 on its own and the difference between the tracks' start times is lost: AAC priming leaves the audio 1024 samples late, and a start offset such as one set with `-itsoffset` disappears. Select `make_zero` again in that case, so that the first sample of the tracks starting later is stretched to cover the difference, as is done without `negative_cts_offsets`. Since the flag makes each track's first sample have dts == pts, the shift is based on pts instead of dts, otherwise the video B-frame delay would needlessly delay every track. With the commands from the issue (audio marker minus video marker): | Options | Before | After | |---|---|---| | `-use_editlist 0 -movflags +negative_cts_offsets` | +21.333 ms | -0.070 ms | | same, plus `-itsoffset 0.5` on the video input | +21.333 ms | -500.070 ms | | default, and `-use_editlist 0` alone | unchanged | unchanged | The 0.070 ms is the rounding of the 1024 sample lead to the video timescale. AAC priming is still played, only an edit list can trim it. This also applies to fragmented output without edit lists (CMAF, ISMV). The api-movenc ismv case, with B-frames and audio preroll, changes accordingly: the video is delayed by the audio preroll. 🤖 Generated with [Claude Code](https://claude.com/claude-code) From 6721704c939bd851316d287c948eb6cda0b130b9 Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Tue, 10 Nov 2026 23:01:38 -0800 Subject: [PATCH] avformat/movenc: keep the tracks' start offsets with negative_cts_offsets and no edit lists Since cc929cee76, the muxer no longer selects avoid_negative_ts=make_zero when edit lists are disabled and negative_cts_offsets is set. Each track then starts at 0 on its own and the difference between the tracks' start times is lost: AAC priming leaves the audio 1024 samples late, and a start offset such as one set with -itsoffset disappears. Select make_zero again in that case, so that the first sample of the tracks starting later is stretched to cover the difference, as is done without negative_cts_offsets. Since the flag makes each track's first sample have dts == pts, base the shift on pts instead of dts, otherwise the video B-frame delay would needlessly delay every track. This also applies to fragmented output without edit lists (CMAF, ISMV). The api-movenc ismv case, with B-frames and audio preroll, changes accordingly: the video is delayed by the audio preroll. Fixes #25775. Co-Authored-By: Claude Fable 5.2 <[email protected]> --- libavformat/movenc.c | 12 +++++++++--- tests/ref/fate/api-movenc | 4 ++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/libavformat/movenc.c b/libavformat/movenc.c index ea71acde42a..8ddd6c2d438 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -8423,9 +8423,15 @@ static int mov_init(AVFormatContext *s) if (mov->flags & FF_MOV_FLAG_CMAF && mov->use_editlist) mov->flags &= ~FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS; - if (!mov->use_editlist && s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO && - !(mov->flags & FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS)) - s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_MAKE_ZERO; + if (!mov->use_editlist) { + if (s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO) + s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_MAKE_ZERO; + /* With negative CTS offsets, each track's dts is shifted so that + * its first sample has dts == pts; the pts are thus what need to be + * non-negative, and what the tracks' start offsets are based on. */ + if (mov->flags & FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS) + ffformatcontext(s)->avoid_negative_ts_use_pts = 1; + } /* Clear the omit_tfhd_offset flag if default_base_moof is set; * if the latter is set that's enough and omit_tfhd_offset doesn't diff --git a/tests/ref/fate/api-movenc b/tests/ref/fate/api-movenc index 20996b89101..55bbc65c377 100644 --- a/tests/ref/fate/api-movenc +++ b/tests/ref/fate/api-movenc @@ -21,10 +21,10 @@ write_data len 24, time nopts, type header atom ftyp write_data len 1171, time nopts, type header atom - write_data len 728, time 0, type sync atom moof write_data len 828, time nopts, type unknown atom - -write_data len 728, time 999999, type sync atom moof +write_data len 728, time 1046439, type sync atom moof write_data len 812, time nopts, type unknown atom - write_data len 148, time nopts, type trailer atom - -824f4461ac74f8a5dc54186f86c98ac2 4439 ismv +5d57e49a4324a9ead98eb554b4f9caed 4439 ismv write_data len 36, time nopts, type header atom ftyp write_data len 1123, time nopts, type header atom - write_data len 796, time 0, type sync atom moof -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
