PR #26182 opened by clementperon URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26182 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26182.patch
# Summary of changes The MP4 muxer can write `prft` (Producer Reference Time) boxes with `-write_prft`, but the demuxer skips them, so the wallclock time of a fragmented MP4 is lost when the file is read back. This series makes the demuxer export them as `AV_PKT_DATA_PRFT` packet side data, the side data `rtpdec` already produces from RTCP sender reports. - **fftools/ffprobe:** print `wallclock` and `flags` for `AV_PKT_DATA_PRFT`. Only the side data type was printed. - **avformat/mov:** parse `prft` boxes into a per-track array sorted by media time, and attach side data to every packet of the referenced track: the wallclock of the last box at or before the packet, plus the media time elapsed since that box. - **tests/fate/mov:** `fate-mov-mp4-prft`, on generated content (lavfi, mpeg4 with B-frames) written with `-write_prft pts` and read back with ffprobe. No new sample. ## Known limitations - With `-use_mfra_for pts|dts`, a seek jumps straight to the `moof` and does not see the `prft` box in front of it, so the first fragment after the seek gets its time from an earlier box. Stated in the commit message, not fixed. - The side data is always exported; there is no option to turn it off. Muxers that use it (`dashenc`, `movenc -write_prft wallclock`) will see it when remuxing such a file. - Several boxes with the same media time and different flags collapse to the last one read. - `ff_parse_ntp_time()` and the writer both truncate, so a time written by FFmpeg can read back 1 µs low (visible in the test reference). ## Tests - `make fate` with the sample suite passes (macOS arm64). - An ASan+UBSan build passes the mov, lavf-mov/mp4, ffprobe, api-movenc and dash targets that need no samples, and 456 runs on damaged `prft` boxes gave no report. - Packet sequences (pts, dts, size, pos, flags) are identical with and without the demuxer patch on 24 file and option combinations. Not run: valgrind, big-endian and 32-bit builds. AI disclosure: the patches, the tests and this description were prepared with Claude Code. 🤖 Generated with [Claude Code](https://claude.com/claude-code) From 2b9768c3a25d411e3012636924e6aad3b1b05758 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:31:23 +0100 Subject: [PATCH 1/3] fftools/ffprobe: print Producer Reference Time packet side data MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Only the side data type was printed for AV_PKT_DATA_PRFT. Co-Authored-By: Claude Fable 5.2 <[email protected]> Signed-off-by: Clément Péron <[email protected]> --- fftools/ffprobe.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fftools/ffprobe.c b/fftools/ffprobe.c index 0f71c3e6a73..5c393dcad65 100644 --- a/fftools/ffprobe.c +++ b/fftools/ffprobe.c @@ -1283,6 +1283,10 @@ static void print_pkt_side_data(AVTextFormatContext *tfc, print_int("crop_right", AV_RL32(sd->data + 12)); } else if (sd->type == AV_PKT_DATA_AFD && sd->size > 0) { print_int("active_format", *sd->data); + } else if (sd->type == AV_PKT_DATA_PRFT && sd->size >= sizeof(AVProducerReferenceTime)) { + const AVProducerReferenceTime *prft = (AVProducerReferenceTime *)sd->data; + print_int("wallclock", prft->wallclock); + print_int("flags", prft->flags); } else if (sd->type == AV_PKT_DATA_EXIF) { print_int("size", sd->size); } else if (sd->type == AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL && sd->size >= 8) { -- 2.54.1 From ade1c25a63ceb5746c1dd43dba3dcf0fbf2fdd27 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:31:24 +0100 Subject: [PATCH 2/3] avformat/mov: export Producer Reference Time boxes as packet side data MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The muxer can write 'prft' boxes (-write_prft), but the demuxer skipped them, so the wallclock time of a fragmented MP4 was lost when reading it back. Parse the boxes and attach AV_PKT_DATA_PRFT side data to every packet of the referenced track. The wallclock of a packet is taken from the last box at or before its composition time on the media timeline: wallclock = prft wallclock + (packet media time - prft media time) This is what rtpdec already does from RTCP sender reports, and lets a file carry the same per-frame timestamps as a live RTSP source. When a seek goes through the fragment index of an mfra box (-use_mfra_for), the demuxer jumps straight to the moof and does not see the box in front of it. The packets of that fragment then get their time from an earlier box. Co-Authored-By: Claude Fable 5.2 <[email protected]> Signed-off-by: Clément Péron <[email protected]> --- libavformat/isom.h | 9 ++++ libavformat/mov.c | 114 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) diff --git a/libavformat/isom.h b/libavformat/isom.h index 6230218e37f..eb9392eb047 100644 --- a/libavformat/isom.h +++ b/libavformat/isom.h @@ -177,6 +177,12 @@ typedef struct MOVIndexRange { int64_t end; } MOVIndexRange; +typedef struct MOVPrft { + int64_t media_time; + int64_t wallclock; + int flags; +} MOVPrft; + typedef struct MOVStreamContext { AVIOContext *pb; int refcount; @@ -242,6 +248,9 @@ typedef struct MOVStreamContext { uint32_t tmcd_flags; ///< tmcd track flags uint8_t tmcd_nb_frames; ///< tmcd number of frames per tick / second int64_t track_end; ///< used for dts generation in fragmented movie files + unsigned int prft_count; + unsigned int prft_allocated_size; + MOVPrft *prft_data; unsigned int rap_group_count; MOVSbgp *rap_group; unsigned int sync_group_count; diff --git a/libavformat/mov.c b/libavformat/mov.c index 97f26cf28ba..6f62e8a55de 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -6640,6 +6640,81 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom) return 0; } +static unsigned mov_search_prft(const MOVStreamContext *sc, int64_t media_time) +{ + unsigned a, b, m; + + a = 0; + b = sc->prft_count; + + while (a < b) { + m = (a + b) >> 1; + if (sc->prft_data[m].media_time < media_time) + a = m + 1; + else + b = m; + } + return a; +} + +static int mov_read_prft(MOVContext *c, AVIOContext *pb, MOVAtom atom) +{ + AVStream *st = NULL; + MOVStreamContext *sc; + MOVPrft *prft; + int version, flags; + unsigned i, track_id; + uint64_t ntp_ts; + int64_t media_time; + + version = avio_r8(pb); + flags = avio_rb24(pb); + if (atom.size < (version ? 24 : 20)) + return 0; + + track_id = avio_rb32(pb); + for (i = 0; i < c->fc->nb_streams; i++) { + sc = c->fc->streams[i]->priv_data; + if (sc->id == track_id) { + st = c->fc->streams[i]; + break; + } + } + if (!st) { + av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %u\n", track_id); + return 0; + } + sc = st->priv_data; + + ntp_ts = avio_rb64(pb); + if (version) { + media_time = avio_rb64(pb); + } else { + media_time = avio_rb32(pb); + } + + i = mov_search_prft(sc, media_time); + if (i == sc->prft_count || sc->prft_data[i].media_time != media_time) { + if ((uint64_t)sc->prft_count + 1 >= UINT_MAX / sizeof(*sc->prft_data)) + return AVERROR_INVALIDDATA; + prft = av_fast_realloc(sc->prft_data, &sc->prft_allocated_size, + (sc->prft_count + 1) * sizeof(*sc->prft_data)); + if (!prft) + return AVERROR(ENOMEM); + sc->prft_data = prft; + memmove(sc->prft_data + i + 1, sc->prft_data + i, + (sc->prft_count - i) * sizeof(*sc->prft_data)); + sc->prft_count++; + } + + prft = &sc->prft_data[i]; + prft->media_time = media_time; + prft->wallclock = ff_parse_ntp_time(ntp_ts) - NTP_OFFSET_US; + prft->flags = flags; + + return 0; +} + static int mov_read_sidx(MOVContext *c, AVIOContext *pb, MOVAtom atom) { int64_t stream_size = avio_size(pb); @@ -10472,6 +10547,7 @@ static const MOVParseTableEntry mov_default_parse_table[] = { { MKTAG('r','n','d','r'), mov_read_cdsc_rndr }, { MKTAG('s','b','a','s'), mov_read_sbas }, { MKTAG('v','d','e','p'), mov_read_vdep }, +{ MKTAG('p','r','f','t'), mov_read_prft }, { MKTAG('s','i','d','x'), mov_read_sidx }, { MKTAG('s','t','b','l'), mov_read_default }, { MKTAG('s','t','c','o'), mov_read_stco }, @@ -11016,6 +11092,7 @@ static void mov_free_stream_context(AVFormatContext *s, AVStream *st) av_freep(&sc->rap_group); av_freep(&sc->sync_group); av_freep(&sc->sgpd_sync); + av_freep(&sc->prft_data); av_freep(&sc->sample_offsets); av_freep(&sc->open_key_samples); av_freep(&sc->display_matrix); @@ -12980,6 +13057,39 @@ static int mov_finalize_packet(AVFormatContext *s, AVStream *st, AVIndexEntry *s return 0; } +static int mov_export_prft(AVStream *st, AVPacket *pkt) +{ + MOVStreamContext *sc = st->priv_data; + AVProducerReferenceTime *prft; + const MOVPrft *ref; + int64_t media_time; + unsigned i; + + if (!sc->prft_count) + return 0; + + media_time = av_sat_add64(pkt->pts, sc->time_offset); + if (sc->ctts_count) + media_time = av_sat_sub64(media_time, sc->dts_shift); + + i = mov_search_prft(sc, media_time); + if (i && (i == sc->prft_count || sc->prft_data[i].media_time > media_time)) + i--; + ref = &sc->prft_data[i]; + + prft = (AVProducerReferenceTime *)av_packet_new_side_data(pkt, AV_PKT_DATA_PRFT, + sizeof(*prft)); + if (!prft) + return AVERROR(ENOMEM); + + prft->wallclock = av_sat_add64(ref->wallclock, + av_rescale_q(av_sat_sub64(media_time, ref->media_time), + st->time_base, AV_TIME_BASE_Q)); + prft->flags = ref->flags; + + return 0; +} + static int mov_read_packet(AVFormatContext *s, AVPacket *pkt) { MOVContext *mov = s->priv_data; @@ -13150,6 +13260,10 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt) if (st->discard == AVDISCARD_ALL) goto retry; + ret = mov_export_prft(st, pkt); + if (ret < 0) + return ret; + if (mov->aax_mode) aax_filter(pkt->data, pkt->size, mov); -- 2.54.1 From d0015edc24256570899f7d614bf813c40705ee91 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:31:24 +0100 Subject: [PATCH 3/3] tests/fate/mov: add a test for reading Producer Reference Time boxes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Fable 5.2 <[email protected]> Signed-off-by: Clément Péron <[email protected]> --- tests/fate/mov.mak | 5 +++++ tests/ref/fate/mov-mp4-prft | 12 ++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 tests/ref/fate/mov-mp4-prft diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak index be39d60d210..05c180b87d2 100644 --- a/tests/fate/mov.mak +++ b/tests/fate/mov.mak @@ -661,6 +661,11 @@ fate-mov-chpl-utf8: tests/data/chpl-utf8.ffmeta FATE_MOV_FFMPEG_FFPROBE-$(call ALLYES, COLOR_FILTER MPEG4_ENCODER FFMETADATA_DEMUXER TGP_MUXER MOV_DEMUXER FILE_PROTOCOL) += fate-mov-chpl-utf8 fate-mov-chpl-utf8: CMD = run_with_temp "$(FFMPEG) -nostdin -v error -filter_complex color=c=black:s=2x2:r=1 -f ffmetadata -i $(TARGET_PATH)/tests/data/chpl-utf8.ffmeta -map_chapters 0 -frames:v 1 -c:v mpeg4 -threads 1 -flags +bitexact -fflags +bitexact -f 3gp -y" "ffprobe$(PROGSSUF)$(EXESUF) -loglevel error -bitexact -show_chapters" 3gp +# The prft boxes hold the pts of the muxed packets, the demuxer shifts the +# pts by one frame because of the negative cts offsets. +FATE_MOV_FFMPEG_FFPROBE-$(call ALLYES, LAVFI_INDEV TESTSRC2_FILTER MPEG4_ENCODER MP4_MUXER MOV_DEMUXER FILE_PROTOCOL) += fate-mov-mp4-prft +fate-mov-mp4-prft: CMD = run_with_temp "$(FFMPEG) -nostdin -v error -f lavfi -i testsrc2=size=64x64:rate=25:duration=0.48 -c:v mpeg4 -bf 2 -g 6 -threads 1 -flags +bitexact -fflags +bitexact -movflags +frag_keyframe+empty_moov+default_base_moof+negative_cts_offsets -write_prft pts -f mp4 -y" "ffprobe$(PROGSSUF)$(EXESUF) -loglevel error -bitexact -show_entries packet=pts,dts:packet_side_data_list -of compact" mp4 + FATE_FFMPEG += $(FATE_MOV_FFMPEG-yes) FATE_FFMPEG_FFPROBE += $(FATE_MOV_FFMPEG_FFPROBE-yes) diff --git a/tests/ref/fate/mov-mp4-prft b/tests/ref/fate/mov-mp4-prft new file mode 100644 index 00000000000..3f1e76f6668 --- /dev/null +++ b/tests/ref/fate/mov-mp4-prft @@ -0,0 +1,12 @@ +packet|pts=512|dts=0|side_datum/producer_reference_time:side_data_type=Producer Reference Time|side_datum/producer_reference_time:wallclock=0|side_datum/producer_reference_time:flags=24 +packet|pts=2048|dts=512|side_datum/producer_reference_time:side_data_type=Producer Reference Time|side_datum/producer_reference_time:wallclock=120000|side_datum/producer_reference_time:flags=24 +packet|pts=1024|dts=1024|side_datum/producer_reference_time:side_data_type=Producer Reference Time|side_datum/producer_reference_time:wallclock=40000|side_datum/producer_reference_time:flags=24 +packet|pts=1536|dts=1536|side_datum/producer_reference_time:side_data_type=Producer Reference Time|side_datum/producer_reference_time:wallclock=80000|side_datum/producer_reference_time:flags=24 +packet|pts=3584|dts=2048|side_datum/producer_reference_time:side_data_type=Producer Reference Time|side_datum/producer_reference_time:wallclock=239999|side_datum/producer_reference_time:flags=24 +packet|pts=2560|dts=2560|side_datum/producer_reference_time:side_data_type=Producer Reference Time|side_datum/producer_reference_time:wallclock=160000|side_datum/producer_reference_time:flags=24 +packet|pts=3072|dts=3072|side_datum/producer_reference_time:side_data_type=Producer Reference Time|side_datum/producer_reference_time:wallclock=200000|side_datum/producer_reference_time:flags=24 +packet|pts=5120|dts=3584|side_datum/producer_reference_time:side_data_type=Producer Reference Time|side_datum/producer_reference_time:wallclock=359999|side_datum/producer_reference_time:flags=24 +packet|pts=4096|dts=4096|side_datum/producer_reference_time:side_data_type=Producer Reference Time|side_datum/producer_reference_time:wallclock=279999|side_datum/producer_reference_time:flags=24 +packet|pts=4608|dts=4608|side_datum/producer_reference_time:side_data_type=Producer Reference Time|side_datum/producer_reference_time:wallclock=319999|side_datum/producer_reference_time:flags=24 +packet|pts=6144|dts=5120|side_datum/producer_reference_time:side_data_type=Producer Reference Time|side_datum/producer_reference_time:wallclock=439999|side_datum/producer_reference_time:flags=24 +packet|pts=5632|dts=5632|side_datum/producer_reference_time:side_data_type=Producer Reference Time|side_datum/producer_reference_time:wallclock=399999|side_datum/producer_reference_time:flags=24 -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
