PR #25929 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25929
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25929.patch

## Problem

In the mov demuxer a sample's pts is computed as index timestamp + 
`sc->dts_shift` + CTS offset, where `dts_shift` is the largest negative CTS 
offset seen in the track. For samples described in the `moov`, 
`mov_build_index()` subtracts `dts_shift` from the index timestamps, so the 
result is correct.

For samples described by `trun` boxes in fragments, `mov_read_trun()` stored 
the raw decode time (from `tfdt`, the previous trun or the track end) as the 
index timestamp. Only the path deriving dts from a known pts subtracted 
`dts_shift`. As a result every pts of the track, and the stream `start_time`, 
came out too large by `dts_shift`.

This affects fragmented MP4 using signed (version 1) composition offsets and no 
edit list, such as what the muxer writes with `-movflags cmaf` or 
`+negative_cts_offsets`: video is demuxed delayed by the reorder delay relative 
to the other streams (0.04 s in the test below).

## Fix

In `mov_read_trun()`:
- keep the local `dts` as the real decode time and store `dts - sc->dts_shift` 
as the index timestamp;
- compare `prev_dts` against that shifted value;
- when `dts_shift` grows while parsing a trun, subtract the increase from the 
index entries already filled in (skipping the not yet filled hole of the 
current trun) and from `prev_dts`.

## Testing

New test `fate-mov-mp4-frag-negative-cts`: muxes `tests/data/vsynth1.yuv` with 
`-c:v mpeg4 -bf 2 -g 12 -movflags 
+frag_keyframe+empty_moov+negative_cts_offsets`, then checks the demuxed 
packets and `stream=start_time`.

- With `libavformat/mov.c` from master the test fails: `start_time=0.040000` 
instead of `start_time=0.000000`.
- With the fix it passes: first packet dts -512 / pts 0, `start_time=0.000000`.

Full `make -k fate` on macOS arm64 (`--disable-doc --disable-autodetect 
--enable-gpl`): no existing reference changes. 14 tests fail here only because 
my local sample set is out of date and their sample file is missing ("No such 
file or directory"), unrelated to this change: `hevc-tile-row-order-nokey`, 
`hevc-atc-sei-reset`, `notchlc-alpha-ramp`, `notchlc-alpha-blocks`, 
`notchlc-alpha-macroblocks`, `notchlc-alpha-ramp-oddsize`, 
`notchlc-chroma-alpha`, `sub-webvtt-timestamp-map`, `vvc-rpr-weighted-pred`, 
`mov-vpcc-reserved-color-tags`, `mpeg4-reserved-color-tags`, 
`probe-format-jpeg-not-cavsvideo`, `probe-format-wav-not-mpegts`, 
`prores-color-tags-fallback`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)



From e603e5bc13123868a191bd7e5c2c92bcfb45a980 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Tue, 10 Nov 2026 23:23:27 -0800
Subject: [PATCH] avformat/mov: fix timestamps of fragments using negative CTS
 offsets

The pts of a sample is computed as its index timestamp + dts_shift + CTS
offset, dts_shift being the largest negative CTS offset of the track. For
this to work, the index timestamps have to be offset by dts_shift, which
is done for the samples described in the moov, but not for the ones from
trun boxes, unless their time was derived from a pts.

As a result, in fragmented files using signed CTS offsets and no edit
list, e.g. as written by the muxer with the cmaf or negative_cts_offsets
flags, all the video timestamps were off by dts_shift, typically delaying
the video by the reorder delay relative to the other streams.

Offset the index timestamps of the samples from trun boxes as well, and
update the entries already added when dts_shift changes.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
---
 libavformat/mov.c                        | 25 ++++++++++++++---
 tests/fate/mov.mak                       |  8 ++++++
 tests/ref/fate/mov-mp4-frag-negative-cts | 35 ++++++++++++++++++++++++
 3 files changed, 64 insertions(+), 4 deletions(-)
 create mode 100644 tests/ref/fate/mov-mp4-frag-negative-cts

diff --git a/libavformat/mov.c b/libavformat/mov.c
index 0c2f592e7f3..26d57434f2f 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -6242,7 +6242,7 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, 
MOVAtom atom)
     unsigned entries, first_sample_flags = frag->flags;
     int flags, distance, i;
     int64_t prev_dts = AV_NOPTS_VALUE;
-    int next_frag_index = -1, index_entry_pos;
+    int next_frag_index = -1, index_entry_pos, first_entry_pos;
     size_t requested_size;
     AVIndexEntry *new_entries;
     MOVFragmentStreamInfo * frag_stream_info;
@@ -6438,6 +6438,7 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, 
MOVAtom atom)
 
     if (index_entry_pos > 0)
         prev_dts = sti->index_entries[index_entry_pos-1].timestamp;
+    first_entry_pos = index_entry_pos;
 
     for (i = 0; i < entries && !pb->eof_reached; i++) {
         unsigned sample_size = frag->size;
@@ -6446,6 +6447,7 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, 
MOVAtom atom)
         unsigned ctts_duration = 0;
         int keyframe = 0;
         int index_entry_flags = 0;
+        int dts_shift = sc->dts_shift;
 
         if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
         if (flags & MOV_TRUN_SAMPLE_SIZE)     sample_size     = avio_rb32(pb);
@@ -6460,8 +6462,23 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, 
MOVAtom atom)
         }
 
         mov_update_dts_shift(sc, ctts_duration, c->fc);
+        if (sc->dts_shift != dts_shift) {
+            // The index timestamps are offset by dts_shift, as the pts is
+            // computed as timestamp + dts_shift + ctts. Update the entries
+            // already added, skipping the ones not yet filled in.
+            dts_shift = sc->dts_shift - dts_shift;
+            for (int j = 0; j < sti->nb_index_entries; j++) {
+                if (j == index_entry_pos)
+                    j = first_entry_pos + entries;
+                if (j < sti->nb_index_entries)
+                    sti->index_entries[j].timestamp =
+                        av_sat_sub64(sti->index_entries[j].timestamp, 
dts_shift);
+            }
+            if (prev_dts != AV_NOPTS_VALUE)
+                prev_dts = av_sat_sub64(prev_dts, dts_shift);
+        }
         if (pts != AV_NOPTS_VALUE) {
-            dts = av_sat_sub64(pts, sc->dts_shift);
+            dts = pts;
             if (flags & MOV_TRUN_SAMPLE_CTS) {
                 dts = av_sat_sub64(dts, ctts_duration);
             } else {
@@ -6487,11 +6504,11 @@ static int mov_read_trun(MOVContext *c, AVIOContext 
*pb, MOVAtom atom)
         }
         // Fragments can overlap in time.  Discard overlapping frames after
         // decoding.
-        if (prev_dts >= dts)
+        if (prev_dts >= av_sat_sub64(dts, sc->dts_shift))
             index_entry_flags |= AVINDEX_DISCARD_FRAME;
 
         sti->index_entries[index_entry_pos].pos   = offset;
-        sti->index_entries[index_entry_pos].timestamp = dts;
+        sti->index_entries[index_entry_pos].timestamp = av_sat_sub64(dts, 
sc->dts_shift);
         sti->index_entries[index_entry_pos].size  = sample_size;
         sti->index_entries[index_entry_pos].min_distance = distance;
         sti->index_entries[index_entry_pos].flags = index_entry_flags;
diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index 7aabe980d3d..645bea31c7f 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -615,6 +615,14 @@ fate-mov-stereo3d-sbs-mp4: CMD = transcode rawvideo 
$(TARGET_PATH)/tests/data/vs
   "-c:v mpeg4 -qscale:v 10 -stereo3d:v sbsl -strict unofficial -frames:v 2" \
   "-c:v copy" "-show_entries stream_side_data_list" "" "" "-s 352x288 -pix_fmt 
yuv420p"
 
+# Fragments using negative CTS offsets, without any edit list.
+FATE_MOV_FFMPEG_FFPROBE-$(call TRANSCODE, MPEG4, MP4 MOV, RAWVIDEO_DEMUXER) \
+                          += fate-mov-mp4-frag-negative-cts
+fate-mov-mp4-frag-negative-cts: tests/data/vsynth1.yuv
+fate-mov-mp4-frag-negative-cts: CMD = transcode rawvideo 
$(TARGET_PATH)/tests/data/vsynth1.yuv mp4 \
+  "-c:v mpeg4 -qscale:v 10 -bf 2 -g 12 -frames:v 24 -movflags 
+frag_keyframe+empty_moov+negative_cts_offsets" \
+  "-c:v copy" "-show_entries stream=start_time" "" "" "-s 352x288 -pix_fmt 
yuv420p"
+
 FATE_FFMPEG += $(FATE_MOV_FFMPEG-yes)
 FATE_FFMPEG_FFPROBE += $(FATE_MOV_FFMPEG_FFPROBE-yes)
 
diff --git a/tests/ref/fate/mov-mp4-frag-negative-cts 
b/tests/ref/fate/mov-mp4-frag-negative-cts
new file mode 100644
index 00000000000..437add04470
--- /dev/null
+++ b/tests/ref/fate/mov-mp4-frag-negative-cts
@@ -0,0 +1,35 @@
+90b9cfb016ba0e338c212aeb359e6356 *tests/data/fate/mov-mp4-frag-negative-cts.mp4
+315067 tests/data/fate/mov-mp4-frag-negative-cts.mp4
+#extradata 0:       31, 0x656a0612
+#tb 0: 1/12800
+#media_type 0: video
+#codec_id 0: mpeg4
+#dimensions 0: 352x288
+#sar 0: 1/1
+0,       -512,          0,      512,    27837, 0xd9809b60
+0,          0,       1536,      512,    11808, 0xe8a80469, F=0x0
+0,        512,        512,      512,     7843, 0x69a26bfc, F=0x0
+0,       1024,       1024,      512,     8815, 0x33504ac2, F=0x0
+0,       1536,       3072,      512,    12344, 0x6b82b0b2, F=0x0
+0,       2048,       2048,      512,    10270, 0x9e881379, F=0x0
+0,       2560,       2560,      512,     8594, 0x9d6adec4, F=0x0
+0,       3072,       4608,      512,    18506, 0x716ac152, F=0x0
+0,       3584,       3584,      512,     9925, 0x844c49d5, F=0x0
+0,       4096,       4096,      512,    10041, 0x4d3d56b6, F=0x0
+0,       4608,       6144,      512,    27925, 0xc719d5f6
+0,       5120,       5120,      512,     8028, 0xe7ae65af, F=0x0
+0,       5632,       5632,      512,     8488, 0x7e95b975, F=0x0
+0,       6144,       7680,      512,    18538, 0x923c2579, F=0x0
+0,       6656,       6656,      512,     9665, 0x7bf9d41d, F=0x0
+0,       7168,       7168,      512,     9793, 0x68872956, F=0x0
+0,       7680,       9216,      512,    19023, 0x1006d3ff, F=0x0
+0,       8192,       8192,      512,     9614, 0x88dba0f9, F=0x0
+0,       8704,       8704,      512,    10769, 0x679a7b1a, F=0x0
+0,       9216,      10752,      512,    14375, 0x8ed53e93, F=0x0
+0,       9728,       9728,      512,     7604, 0x45f7f80d, F=0x0
+0,      10240,      10240,      512,     7926, 0xc22da563, F=0x0
+0,      10752,      11776,      512,    28215, 0x8dff5c71
+0,      11264,      11264,      512,     7688, 0x40575c5e, F=0x0
+[STREAM]
+start_time=0.000000
+[/STREAM]
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to