Commit 209f7c4e77 for ffmpeg
commit 209f7c4e7799b614877725679e21078eae629390
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date: Wed Nov 11 12:53:15 2026 -0800
avformat/movenc: preserve video delay with negative CTS offsets
Without edit lists or fragments, carry the video start delay in composition offsets
so sample durations stay unchanged, and extend the durations to cover it.
Exclude audio priming.
Refs #25775.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index f302a36612..4b4ba9b9af 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -4110,12 +4110,15 @@ static void get_pts_range(MOVMuxContext *mov, MOVTrack *track,
if (!mdhd && track->end_pts != AV_NOPTS_VALUE &&
track->start_dts != AV_NOPTS_VALUE &&
track->start_cts != AV_NOPTS_VALUE) {
- *start = track->start_dts + track->start_cts;
+ *start = track->start_dts + track->start_cts - track->start_delay;
*end = elst ? track->elst_end_pts : track->end_pts;
return;
}
*start = 0;
*end = track->track_duration;
+ /* The composition offsets delay the samples by start_delay. */
+ if (*end)
+ *end += track->start_delay;
}
static int64_t calc_samples_pts_duration(MOVMuxContext *mov, MOVTrack *track)
@@ -7172,7 +7175,8 @@ static int mov_auto_flush_fragment(AVFormatContext *s, int force)
return ret;
}
-static int check_pkt(AVFormatContext *s, MOVTrack *trk, AVPacket *pkt)
+static int check_pkt(AVFormatContext *s, MOVTrack *trk, AVPacket *pkt,
+ int dts_shifted)
{
int64_t ref;
uint64_t duration;
@@ -7182,10 +7186,12 @@ static int check_pkt(AVFormatContext *s, MOVTrack *trk, AVPacket *pkt)
} else if ( trk->start_dts != AV_NOPTS_VALUE
&& !trk->frag_discont) {
ref = trk->start_dts + trk->track_duration;
- } else
+ } else {
ref = pkt->dts; // Skip tests for the first packet
+ dts_shifted = 1;
+ }
- if (trk->dts_shift != AV_NOPTS_VALUE) {
+ if (trk->dts_shift != AV_NOPTS_VALUE && !dts_shifted) {
/* With negative CTS offsets we have set an offset to the DTS,
* reverse this for the check. */
ref -= trk->dts_shift;
@@ -7249,6 +7255,48 @@ int ff_mov_set_fragment_end_hint(AVFormatContext *s, int stream_index,
return 1;
}
+/**
+ * Take the start delay back out of the composition offsets of a track. Only
+ * set without fragmentation, so none of its samples has been written yet.
+ */
+static int mov_drop_start_delay(AVFormatContext *s, MOVTrack *trk, AVPacket *pkt)
+{
+ int64_t delay = trk->start_delay;
+
+ if (!delay)
+ return 0;
+ for (int i = 0; i < trk->entry; i++) {
+ trk->cluster[i].dts += delay;
+ trk->cluster[i].cts -= delay;
+ }
+ if (trk->start_dts != AV_NOPTS_VALUE)
+ trk->start_dts += delay;
+ if (trk->start_cts != AV_NOPTS_VALUE)
+ trk->start_cts -= delay;
+ trk->dts_shift += delay;
+ trk->start_delay = 0;
+ pkt->dts += delay;
+ return 1;
+}
+
+/**
+ * Whether the composition offset of pkt does not fit the ctts entry: signed
+ * for version 1, as written with negative_cts_offsets, keeping the existing
+ * version 0 range otherwise.
+ */
+static int mov_cts_out_of_range(const MOVMuxContext *mov, const AVPacket *pkt)
+{
+ uint64_t delta;
+
+ if (pkt->pts == AV_NOPTS_VALUE)
+ return 0;
+ if (mov->flags & FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS)
+ delta = (uint64_t)pkt->pts - pkt->dts;
+ else
+ delta = (uint64_t)pkt->dts - pkt->pts;
+ return delta != (int32_t)delta;
+}
+
int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
{
MOVMuxContext *mov = s->priv_data;
@@ -7268,12 +7316,15 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
trk = &mov->tracks[pkt->stream_index];
par = trk->par;
- ret = check_pkt(s, trk, pkt);
+ ret = check_pkt(s, trk, pkt, 1);
if (ret < 0)
return ret;
- if (pkt->pts != AV_NOPTS_VALUE &&
- (uint64_t)pkt->dts - pkt->pts != (int32_t)((uint64_t)pkt->dts - pkt->pts)) {
+ if (mov_cts_out_of_range(mov, pkt) && mov_drop_start_delay(s, trk, pkt))
+ av_log(s, AV_LOG_WARNING, "Composition offsets too large, dropping "
+ "the start delay of stream %d\n", pkt->stream_index);
+
+ if (mov_cts_out_of_range(mov, pkt)) {
av_log(s, AV_LOG_WARNING, "pts/dts pair unsupported\n");
return AVERROR_PATCHWELCOME;
}
@@ -7711,6 +7762,61 @@ err:
return ret;
}
+/**
+ * Presentation time at which a stream starts, given the pts of its first
+ * packet: the priming samples of an audio stream come before it.
+ */
+static int64_t mov_start_time(const AVStream *st, int64_t pts)
+{
+ const AVCodecParameters *par = st->codecpar;
+
+ if (par->codec_type == AVMEDIA_TYPE_AUDIO && par->initial_padding > 0 &&
+ par->sample_rate > 0)
+ pts += av_rescale_q(par->initial_padding,
+ (AVRational){ 1, par->sample_rate },
+ st->time_base);
+ return FFMAX(pts, 0);
+}
+
+/**
+ * How much later the stream of pkt, its first packet, starts than the
+ * earliest audio or video stream, going by the streams already started and
+ * by the packets queued for the other ones. Audio priming is left out:
+ * without an edit list, whether it is played is up to the player.
+ */
+static int64_t mov_get_start_delay(AVFormatContext *s, const AVPacket *pkt)
+{
+ const AVStream *st = s->streams[pkt->stream_index];
+ int64_t start = mov_start_time(st, pkt->pts), ref = start;
+
+ for (int i = 0; i < s->nb_streams; i++) {
+ const AVStream *st2 = s->streams[i];
+ const MOVTrack *trk2 = st2->priv_data;
+ int64_t pts;
+
+ if (i == pkt->stream_index || !trk2 || trk2->st != st2 ||
+ !rtp_hinting_needed(st2))
+ continue;
+ if (trk2->start_dts != AV_NOPTS_VALUE) {
+ if (trk2->start_cts == AV_NOPTS_VALUE)
+ continue;
+ pts = trk2->start_dts + trk2->start_cts;
+ } else {
+ const AVPacket *pkt2 = ff_interleaved_peek(s, i);
+ int64_t offset;
+ if (!pkt2 || pkt2->pts == AV_NOPTS_VALUE)
+ continue;
+ ff_get_muxer_ts_offset(s, i, &offset);
+ pts = pkt2->pts + offset;
+ }
+ pts = mov_start_time(st2, pts);
+ pts = av_rescale_q(pts, st2->time_base, st->time_base);
+ ref = FFMIN(ref, pts);
+ }
+
+ return start - ref;
+}
+
static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
{
MOVMuxContext *mov = s->priv_data;
@@ -7719,7 +7825,7 @@ static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
int64_t frag_duration = 0;
int size = pkt->size;
- int ret = check_pkt(s, trk, pkt);
+ int ret = check_pkt(s, trk, pkt, 0);
if (ret < 0)
return ret;
@@ -7730,8 +7836,24 @@ static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
}
if (mov->flags & FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS) {
- if (trk->dts_shift == AV_NOPTS_VALUE)
+ if (trk->dts_shift == AV_NOPTS_VALUE) {
trk->dts_shift = pkt->pts - pkt->dts;
+ /* Not using edit lists: if this track starts later than the
+ * earliest one, keep the difference in its composition offsets,
+ * which delays its presentation without altering any sample
+ * duration. Leave room in the 32-bit offsets for reordering.
+ * Not when fragmenting: the delay could not be dropped from
+ * fragments already written if the offsets overflow. */
+ if (!mov->use_editlist && !(mov->flags & FF_MOV_FLAG_FRAGMENT) &&
+ s->avoid_negative_ts != AVFMT_AVOID_NEG_TS_MAKE_ZERO &&
+ trk->par->codec_type == AVMEDIA_TYPE_VIDEO) {
+ int64_t delay = mov_get_start_delay(s, pkt);
+ if (delay < INT32_MAX / 2) {
+ trk->start_delay = delay;
+ trk->dts_shift -= delay;
+ }
+ }
+ }
pkt->dts += trk->dts_shift;
}
diff --git a/libavformat/movenc.h b/libavformat/movenc.h
index ce5f928015..576c98320b 100644
--- a/libavformat/movenc.h
+++ b/libavformat/movenc.h
@@ -140,6 +140,7 @@ typedef struct MOVTrack {
int64_t elst_end_pts;
int end_reliable;
int64_t dts_shift;
+ int64_t start_delay; ///< presentation delay kept in the composition offsets
int hint_track; ///< the track that hints this track, -1 if no hint track is set
int nb_src_track;
diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index df622d85c4..03f583e177 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -510,6 +510,75 @@ fate-mov-trun-zero-sample-size: CMD = run_with_patched_temp \
-show_entries packet=pts,dts,duration,size,pos -print_format compact \
-select_streams v -v 0" mp4 927 "\\0000\\0000\\0000\\0000"
+# Version 0 composition offsets are unsigned: an offset of 2^31 is accepted.
+FATE_MOV_FFMPEG-$(call ALLYES, COLOR_FILTER MPEG4_ENCODER MP4_MUXER FILE_PROTOCOL) \
+ += fate-mov-mp4-ctts-v0-boundary
+fate-mov-mp4-ctts-v0-boundary: CMD = md5 -filter_complex color=s=2x2:r=1:d=4 \
+ -c:v mpeg4 -g 12 -bf 1 -q:v 2 -threads 1 -flags +bitexact -fflags +bitexact \
+ -video_track_timescale 1073741824 -f mp4
+
+MOV_NOELST_VIDEO_DELAY_COMPONENTS = COLOR_FILTER SETPTS_FILTER SINE_FILTER \
+ MPEG4_ENCODER PCM_S16LE_ENCODER \
+ MP4_MUXER FILE_PROTOCOL
+
+MOV_NOELST_VIDEO_DELAY_FILTER = color=s=2x2:r=24:d=1,setpts=PTS+0.5/TB[v];sine=r=8000:d=1[a]
+
+MOV_NOELST_VIDEO_DELAY = -map [v] -map [a] -fps_mode passthrough \
+ -c:v mpeg4 -g 12 -bf 2 -q:v 2 -threads 1 -c:a pcm_s16le \
+ -flags +bitexact -fflags +bitexact \
+ -use_editlist 0 -movflags +negative_cts_offsets -f mp4
+
+FATE_MOV_FFMPEG_FFPROBE-$(call ALLYES, $(MOV_NOELST_VIDEO_DELAY_COMPONENTS) MOV_DEMUXER) \
+ += fate-mov-mp4-noelst-video-delay
+
+# Without edit lists, video starting 0.5 s after the audio keeps its delay in
+# the composition offsets, and the durations cover the delayed last frame.
+fate-mov-mp4-noelst-video-delay: CMD = run_with_temp \
+ "$(FFMPEG) -nostdin -v error -filter_complex $(MOV_NOELST_VIDEO_DELAY_FILTER) $(MOV_NOELST_VIDEO_DELAY) -y" \
+ "ffprobe$(PROGSSUF)$(EXESUF) -show_entries format=duration:stream=index,start_time:packet=stream_index,pts,dts \
+ -print_format compact -v 0" mp4
+
+# Include container headers: the MOV demuxer caps stream duration to the
+# stts duration, so the packet probe above cannot check the mdhd extension.
+FATE_MOV_FFMPEG-$(call ALLYES, $(MOV_NOELST_VIDEO_DELAY_COMPONENTS)) \
+ += fate-mov-mp4-noelst-video-delay-md5
+fate-mov-mp4-noelst-video-delay-md5: CMD = md5 -filter_complex "$(MOV_NOELST_VIDEO_DELAY_FILTER)" $(MOV_NOELST_VIDEO_DELAY)
+
+FATE_MOV_FFMPEG_FFPROBE-$(call ALLYES, COLOR_FILTER SETPTS_FILTER SINE_FILTER \
+ ASETPTS_FILTER ARESAMPLE_FILTER MPEG4_ENCODER \
+ AAC_ENCODER \
+ MP4_MUXER MOV_DEMUXER FILE_PROTOCOL) \
+ += fate-mov-mp4-noelst-common-origin
+
+# Video and AAC audio sharing a negative origin start together: the audio
+# priming does not make the video look delayed.
+fate-mov-mp4-noelst-common-origin: CMD = run_with_temp \
+ "$(FFMPEG) -nostdin -v error \
+ -filter_complex color=s=2x2:r=24:d=1,setpts=PTS-1/TB[v];sine=r=8000:d=1,asetpts=PTS-1/TB[a] \
+ -map [v] -map [a] -fps_mode passthrough \
+ -c:v mpeg4 -g 12 -bf 2 -q:v 2 -threads 1 -c:a aac \
+ -flags +bitexact -fflags +bitexact \
+ -use_editlist 0 -movflags +negative_cts_offsets -f mp4 -y" \
+ "ffprobe$(PROGSSUF)$(EXESUF) -show_entries stream=index,start_time:packet=stream_index,pts,dts \
+ -print_format compact -v 0" mp4
+
+FATE_MOV_FFMPEG_FFPROBE-$(call ALLYES, COLOR_FILTER SETTB_FILTER SETPTS_FILTER \
+ SINE_FILTER MPEG4_ENCODER PCM_S16LE_ENCODER \
+ MP4_MUXER MOV_DEMUXER FILE_PROTOCOL) \
+ += fate-mov-mp4-noelst-frag-delay
+
+# Fragmented output keeps no start delay: reordering at a 1e9 timescale would
+# overflow the 32-bit offsets once the first fragment is written.
+fate-mov-mp4-noelst-frag-delay: CMD = run_with_temp \
+ "$(FFMPEG) -nostdin -v error \
+ -filter_complex color=s=2x2:r=1:d=5,settb=1/1000,setpts=PTS+0.5/TB[v];sine=r=8000:d=5[a] \
+ -map [v] -map [a] -fps_mode passthrough \
+ -c:v mpeg4 -g 12 -bf 2 -q:v 2 -threads 1 -c:a pcm_s16le \
+ -flags +bitexact -fflags +bitexact -video_track_timescale 1000000000 \
+ -use_editlist 0 -movflags +negative_cts_offsets -frag_duration 1000000 -f mp4 -y" \
+ "ffprobe$(PROGSSUF)$(EXESUF) -select_streams v -show_entries stream=start_time:packet=pts,dts \
+ -print_format compact -v 0" mp4
+
FATE_MOV_FFMPEG_FFPROBE-$(call ALLYES, LAVFI_INDEV ANULLSRC_FILTER AFORMAT_FILTER \
PCM_S16$(if $(filter yes,$(HAVE_BIGENDIAN)),BE,LE)_DECODER \
PCM_S16BE_ENCODER \
diff --git a/tests/ref/fate/mov-mp4-ctts-v0-boundary b/tests/ref/fate/mov-mp4-ctts-v0-boundary
new file mode 100644
index 0000000000..f367cf6a6a
--- /dev/null
+++ b/tests/ref/fate/mov-mp4-ctts-v0-boundary
@@ -0,0 +1 @@
+65a6d3579aae235fc2eccb3bd4835d98
diff --git a/tests/ref/fate/mov-mp4-noelst-common-origin b/tests/ref/fate/mov-mp4-noelst-common-origin
new file mode 100644
index 0000000000..2d9c7b34ed
--- /dev/null
+++ b/tests/ref/fate/mov-mp4-noelst-common-origin
@@ -0,0 +1,35 @@
+packet|stream_index=1|pts=0|dts=0
+packet|stream_index=0|pts=0|dts=-512
+packet|stream_index=0|pts=1536|dts=0
+packet|stream_index=1|pts=1024|dts=1024
+packet|stream_index=0|pts=512|dts=512
+packet|stream_index=0|pts=1024|dts=1024
+packet|stream_index=0|pts=3072|dts=1536
+packet|stream_index=1|pts=2048|dts=2048
+packet|stream_index=0|pts=2048|dts=2048
+packet|stream_index=0|pts=2560|dts=2560
+packet|stream_index=0|pts=4608|dts=3072
+packet|stream_index=1|pts=3072|dts=3072
+packet|stream_index=0|pts=3584|dts=3584
+packet|stream_index=0|pts=4096|dts=4096
+packet|stream_index=0|pts=6144|dts=4608
+packet|stream_index=1|pts=4096|dts=4096
+packet|stream_index=0|pts=5120|dts=5120
+packet|stream_index=0|pts=5632|dts=5632
+packet|stream_index=0|pts=7680|dts=6144
+packet|stream_index=1|pts=5120|dts=5120
+packet|stream_index=0|pts=6656|dts=6656
+packet|stream_index=0|pts=7168|dts=7168
+packet|stream_index=0|pts=9216|dts=7680
+packet|stream_index=1|pts=6144|dts=6144
+packet|stream_index=0|pts=8192|dts=8192
+packet|stream_index=0|pts=8704|dts=8704
+packet|stream_index=0|pts=10752|dts=9216
+packet|stream_index=1|pts=7168|dts=7168
+packet|stream_index=0|pts=9728|dts=9728
+packet|stream_index=0|pts=10240|dts=10240
+packet|stream_index=0|pts=11776|dts=10752
+packet|stream_index=1|pts=8192|dts=8192
+packet|stream_index=0|pts=11264|dts=11264
+stream|index=0|start_time=0.000000
+stream|index=1|start_time=0.000000
diff --git a/tests/ref/fate/mov-mp4-noelst-frag-delay b/tests/ref/fate/mov-mp4-noelst-frag-delay
new file mode 100644
index 0000000000..b7dc914f80
--- /dev/null
+++ b/tests/ref/fate/mov-mp4-noelst-frag-delay
@@ -0,0 +1,6 @@
+packet|pts=1000000000|dts=0
+packet|pts=4000000000|dts=1000000000
+packet|pts=2000000000|dts=2000000000
+packet|pts=3000000000|dts=3000000000
+packet|pts=5000000000|dts=4000000000
+stream|start_time=0.000000
diff --git a/tests/ref/fate/mov-mp4-noelst-video-delay b/tests/ref/fate/mov-mp4-noelst-video-delay
new file mode 100644
index 0000000000..1190e48c11
--- /dev/null
+++ b/tests/ref/fate/mov-mp4-noelst-video-delay
@@ -0,0 +1,35 @@
+packet|stream_index=1|pts=0|dts=0
+packet|stream_index=1|pts=1024|dts=1024
+packet|stream_index=1|pts=2048|dts=2048
+packet|stream_index=1|pts=3072|dts=3072
+packet|stream_index=0|pts=6144|dts=0
+packet|stream_index=0|pts=7680|dts=512
+packet|stream_index=1|pts=4096|dts=4096
+packet|stream_index=0|pts=6656|dts=1024
+packet|stream_index=0|pts=7168|dts=1536
+packet|stream_index=0|pts=9216|dts=2048
+packet|stream_index=1|pts=5120|dts=5120
+packet|stream_index=0|pts=8192|dts=2560
+packet|stream_index=0|pts=8704|dts=3072
+packet|stream_index=0|pts=10752|dts=3584
+packet|stream_index=1|pts=6144|dts=6144
+packet|stream_index=0|pts=9728|dts=4096
+packet|stream_index=0|pts=10240|dts=4608
+packet|stream_index=0|pts=12288|dts=5120
+packet|stream_index=1|pts=7168|dts=7168
+packet|stream_index=0|pts=11264|dts=5632
+packet|stream_index=0|pts=11776|dts=6144
+packet|stream_index=0|pts=13824|dts=6656
+packet|stream_index=0|pts=12800|dts=7168
+packet|stream_index=0|pts=13312|dts=7680
+packet|stream_index=0|pts=15360|dts=8192
+packet|stream_index=0|pts=14336|dts=8704
+packet|stream_index=0|pts=14848|dts=9216
+packet|stream_index=0|pts=16896|dts=9728
+packet|stream_index=0|pts=15872|dts=10240
+packet|stream_index=0|pts=16384|dts=10752
+packet|stream_index=0|pts=17920|dts=11264
+packet|stream_index=0|pts=17408|dts=11776
+stream|index=0|start_time=0.500000
+stream|index=1|start_time=0.000000
+format|duration=1.500000
diff --git a/tests/ref/fate/mov-mp4-noelst-video-delay-md5 b/tests/ref/fate/mov-mp4-noelst-video-delay-md5
new file mode 100644
index 0000000000..9a9051306a
--- /dev/null
+++ b/tests/ref/fate/mov-mp4-noelst-video-delay-md5
@@ -0,0 +1 @@
+0cca0d6653adcdddb2b28bc5ed6fd884