PR #26183 opened by clementperon
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26183
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26183.patch
# Summary of changes
`-write_prft` stores, as the media time of the `prft` box, the pts of the
fragment's first packet as it was given to the muxer. The `tfdt` box of the
same fragment holds `dts - start_dts`. The two timelines differ whenever the
track does not start at dts 0, so the box then points at the wrong sample.
This writes the composition time of the sample as stored in the fragment
instead (`dts + cts - start_dts`, i.e. `tfdt` plus the sample's composition
offset).
## Reproduction
```
ffmpeg -f lavfi -i testsrc2=size=64x64:rate=25:duration=0.48 -c:v mpeg4 -bf 2
-g 6 \
-movflags +frag_keyframe+delay_moov+default_base_moof -write_prft pts
out.mp4
```
With `delay_moov` and B-frames the first dts is negative and an edit list
(media_time 512) skips the first frame duration. Values read from the boxes,
track timescale 12800:
| Fragment | tfdt | First sample composition offset | First sample composition
time | prft media time, before | prft media time, after |
|---|---|---|---|---|---|
| 0 | 0 | 512 | 512 | 0 | 512 |
| 1 | 2048 | 1536 | 3584 | 3072 | 3584 |
| 2 | 5120 | 1024 | 6144 | 5632 | 6144 |
Before the change the box is 512 ticks (one frame, 40 ms) early. The DASH muxer
sets `delay_moov`, so its segments are affected in the same way.
With `+frag_keyframe+empty_moov+default_base_moof` the two timelines already
agree, and the output file is byte-identical before and after the change.
## Notes
- The reading that the media time is on the track's media timeline rests on
consistency with `tfdt` in the same file. I did not check it against the text
of ISO/IEC 14496-12.
- No FATE test here: the demuxer does not read `prft` boxes yet. #26182 adds
that, and a test for this case (`delay_moov` with B-frames, read back with
ffprobe) is ready to follow once it is in.
## Tests
`make fate` with the sample suite passes (macOS arm64); no existing reference
changes.
AI disclosure: the patch, the tests and this description were prepared with
Claude Code.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
From adf05b0171f1c618ab1e9ab2453e80e55d4c16cd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20P=C3=A9ron?= <[email protected]>
Date: Wed, 18 Nov 2026 21:58:32 +0100
Subject: [PATCH] avformat/movenc: write the prft media time on the media
timeline
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
The media time of the 'prft' box was the pts of the first packet of the
fragment as given to the muxer, while the tfdt box of the same fragment
holds dts - start_dts. The two differ whenever the track does not start
at dts 0, for example with B-frames and delay_moov, where the first dts
is negative, or with avoid_negative_ts disabled and an input that does
not start at 0. A reader then maps the wallclock time to the wrong
sample.
Write the composition time of the sample as stored in the fragment
(tfdt + cts) instead.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Signed-off-by: Clément Péron <[email protected]>
---
libavformat/movenc.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 293a85633e5..bb6448be8bf 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -6228,7 +6228,8 @@ static int mov_write_prft_tag(AVIOContext *pb,
MOVMuxContext *mov, int tracks)
avio_wb24(pb, flags); // Flags
avio_wb32(pb, first_track->track_id); // reference track ID
avio_wb64(pb, ntp_ts); // NTP time stamp
- avio_wb64(pb, first_track->cluster[0].pts); //media time
+ avio_wb64(pb, first_track->cluster[0].dts + first_track->cluster[0].cts -
+ first_track->start_dts); // media time
return update_size(pb, pos);
}
--
2.54.1
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]