PR #25983 opened by Romain Beauxis (toots) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25983 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25983.patch
This PR adds `AVFMT_FLAG_EXACT_TS` (`-fflags +exact_ts`) and implements it in the Matroska demuxer and muxer. Matroska's 1 ms clock rounds every block timestamp, so a cut such as `-ss` can land up to 24 samples away from the sample it names. With the flag, the demuxer puts Opus, AAC and default-duration audio tracks in a sample time base and rebuilds their timestamps from the packet lengths, following the stored timestamp only across a real gap: ``` ffprobe -fflags +exact_ts -show_entries packet=pts codec_delay_opus.mkv # -312, 648, 1608... on 1/48000 ffprobe -show_entries packet=pts codec_delay_opus.mkv # -7, 14, 34... on 1/1000 ``` The muxer writes on a clock finer than half a sample, 10 us at 48 kHz, which keeps the files ordinary Matroska with clusters capped at 327 ms. The tests come first and record the millisecond timestamps, including a seek test on `mkv/test7_cut.mkv` that checks the video and AAC packets read after each seek are unchanged. >From 493881c515483abb071e70855aaf793a4cc7e7db Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Thu, 12 Nov 2026 12:40:23 -0600 Subject: [PATCH 1/6] avcodec/opus/tab: add ff_opus_packet_samples() The helper reads the duration of an Opus packet from its TOC, as the Matroska muxer did in a static function it now replaces. --- libavcodec/opus/tab.h | 27 +++++++++++++++++++++++++++ libavformat/matroskaenc.c | 30 +----------------------------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/libavcodec/opus/tab.h b/libavcodec/opus/tab.h index c1d38f9e69b..04f06a28b13 100644 --- a/libavcodec/opus/tab.h +++ b/libavcodec/opus/tab.h @@ -26,10 +26,37 @@ #include <stdint.h> #include "libavutil/attributes_internal.h" +#include "libavutil/error.h" FF_VISIBILITY_PUSH_HIDDEN extern const uint16_t ff_opus_frame_duration[32]; +/** + * @return the duration of an Opus packet in samples at 48 kHz, read from + * its TOC, or AVERROR_INVALIDDATA if the packet is too short + */ +static inline int ff_opus_packet_samples(const uint8_t *buf, int buf_size) +{ + int frame_count; + + if (buf_size < 1) + return AVERROR_INVALIDDATA; + switch (buf[0] & 3) { + case 0: + frame_count = 1; + break; + case 3: + if (buf_size < 2) + return AVERROR_INVALIDDATA; + frame_count = buf[1] & 0x3F; + break; + default: + frame_count = 2; + break; + } + return frame_count * ff_opus_frame_duration[buf[0] >> 3]; +} + extern const uint8_t ff_celt_band_end[]; extern const uint8_t ff_opus_default_coupled_streams[]; diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index f08fee0c9aa..25526d079de 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -283,34 +283,6 @@ typedef struct MatroskaMuxContext { /** Seek preroll value for opus */ #define OPUS_SEEK_PREROLL 80000000 -/** - * Returns the duration of an Opus packet in samples. - */ -static int parse_opus_packet_duration(const uint8_t *buf, int buf_size) -{ - int code = buf[0] & 0x3; - int config = buf[0] >> 3; - int frame_count; - - switch (code) { - default: - av_unreachable("code is in 0..3"); - case 0: - frame_count = 1; - break; - case 1: - case 2: - frame_count = 2; - break; - case 3: - if (buf_size <= 1) - return AVERROR_INVALIDDATA; - frame_count = buf[1] & 0x3F; - break; - } - return frame_count * ff_opus_frame_duration[config]; -} - static int ebml_id_size(uint32_t id) { return (av_log2(id) + 7U) / 8; @@ -2900,7 +2872,7 @@ static int mkv_write_block(void *logctx, MatroskaMuxContext *mkv, duration != track->default_duration_low)) ebml_writer_add_uint(&writer, MATROSKA_ID_BLOCKDURATION, duration); else if (par->codec_id == AV_CODEC_ID_OPUS) { - ret = parse_opus_packet_duration(pkt->data, pkt->size); + ret = ff_opus_packet_samples(pkt->data, pkt->size); if (ret >= 0) { /* If the packet's duration is inconsistent with the coded duration, * add an explicit duration element. */ -- 2.54.0 >From ad88a94220188f1f6bed08d783e58d5e2a2e6af3 Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Thu, 12 Nov 2026 12:22:05 -0600 Subject: [PATCH 2/6] avformat: add AVFMT_FLAG_EXACT_TS The flag asks for timestamps that identify the exact sample: a demuxer whose clock is coarser than a sample rebuilds the audio timestamps from the packet lengths, and a muxer that can choose its clock picks one fine enough for every sample. It is off by default, as it changes the time base and the timestamps of the affected streams. --- doc/APIchanges | 3 +++ doc/formats.texi | 6 ++++++ libavformat/avformat.h | 8 ++++++++ libavformat/options_table.h | 1 + libavformat/version.h | 4 ++-- 5 files changed, 20 insertions(+), 2 deletions(-) diff --git a/doc/APIchanges b/doc/APIchanges index 1096fde9c90..e95f344f73a 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -2,6 +2,9 @@ The last version increases of all libraries were on 2026-06-23. API changes, most recent first: +2026-11-xx - xxxxxxxxxx - lavf 63.21.100 - avformat.h + Add AVFMT_FLAG_EXACT_TS. + 2026-11-xx - xxxxxxxxxx - lavf 63.20.101 - avformat.h With AVSEEK_FLAG_PREROLL set, avformat_seek_file() and av_seek_frame() move the target back by the audio pre-roll, and discard the pre-roll diff --git a/doc/formats.texi b/doc/formats.texi index 36b8b9718bc..412b1797773 100644 --- a/doc/formats.texi +++ b/doc/formats.texi @@ -58,6 +58,9 @@ Do not fill in missing values in packet fields that can be exactly calculated. Disable AVParsers, this needs @code{+nofillin} too. @item sortdts Try to interleave output packets by DTS. At present, available only for AVIs with an index. +@item exact_ts +Rebuild the audio timestamps from the packet lengths, in a sample time base, +where the container's clock is coarser than a sample. @end table Possible values for output files: @@ -68,6 +71,9 @@ Automatically apply bitstream filters as required by the output format. Enabled Only write platform-, build- and time-independent data. This ensures that file and data checksums are reproducible and match between platforms. Its primary use is for regression testing. +@item exact_ts +Write the timestamps on a clock finer than a sample, where the format lets the +muxer choose its clock. @item flush_packets Write out packets immediately. @item shortest diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 67686273792..6e3df902204 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -1573,6 +1573,14 @@ typedef struct AVFormatContext { #define AVFMT_FLAG_SORT_DTS 0x10000 ///< try to interleave outputted packets by dts (using this flag can slow demuxing down) #define AVFMT_FLAG_FAST_SEEK 0x80000 ///< Enable fast, but inaccurate seeks for some formats #define AVFMT_FLAG_AUTO_BSF 0x200000 ///< Add bitstream filters as requested by the muxer +/** + * Timestamps identify the exact sample, not the nearest tick of the + * container's clock. When demuxing, a format whose clock is coarser than a + * sample rebuilds the audio timestamps from the packet lengths, in a sample + * time base. When muxing, a format that can choose its clock picks one fine + * enough for every sample. + */ +#define AVFMT_FLAG_EXACT_TS 0x800000 #if FF_API_OLD_ID3V2_COMMENT /** diff --git a/libavformat/options_table.h b/libavformat/options_table.h index c93069c7a90..76e773203f6 100644 --- a/libavformat/options_table.h +++ b/libavformat/options_table.h @@ -52,6 +52,7 @@ static const AVOption avformat_options[] = { {"nobuffer", "reduce the latency introduced by optional buffering", 0, AV_OPT_TYPE_CONST, {.i64 = AVFMT_FLAG_NOBUFFER }, 0, INT_MAX, D, .unit = "fflags"}, {"bitexact", "do not write random/volatile data", 0, AV_OPT_TYPE_CONST, { .i64 = AVFMT_FLAG_BITEXACT }, 0, 0, E, .unit = "fflags" }, {"autobsf", "add needed bsfs automatically", 0, AV_OPT_TYPE_CONST, { .i64 = AVFMT_FLAG_AUTO_BSF }, 0, 0, E, .unit = "fflags" }, +{"exact_ts", "timestamps identify the exact sample, not the nearest container tick", 0, AV_OPT_TYPE_CONST, { .i64 = AVFMT_FLAG_EXACT_TS }, 0, 0, E|D, .unit = "fflags" }, #if FF_API_OLD_ID3V2_COMMENT {"legacy_id3v2_comm_keys", "also export id3v2 COMM descriptors as bare metadata keys", 0, AV_OPT_TYPE_CONST, { .i64 = AVFMT_FLAG_LEGACY_ID3V2_COMM_KEYS }, 0, 0, D|AV_OPT_FLAG_DEPRECATED, .unit = "fflags" }, #endif diff --git a/libavformat/version.h b/libavformat/version.h index f4a26c28709..683184d5da1 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -31,8 +31,8 @@ #include "version_major.h" -#define LIBAVFORMAT_VERSION_MINOR 20 -#define LIBAVFORMAT_VERSION_MICRO 101 +#define LIBAVFORMAT_VERSION_MINOR 21 +#define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ LIBAVFORMAT_VERSION_MINOR, \ -- 2.54.0 >From 034e49678fa086ecc5c4ccda3629940841a41e5f Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Thu, 12 Nov 2026 12:22:53 -0600 Subject: [PATCH 3/6] tests/fate: add tests for exact Matroska timestamps matroska-opus-exact-ts demuxes the codec_delay_opus sample with AVFMT_FLAG_EXACT_TS, matroska-opus-exact-ts-remux remuxes it with the flag and reads the result back without it, and seek-mkv-exact-ts seeks through the video and the AAC stream of test7_cut.mkv with the flag. All three record the packets on the file's millisecond clock. --- tests/fate/matroska.mak | 6 ++ tests/fate/seek.mak | 2 + tests/ref/fate/matroska-opus-exact-ts | 58 +++++++++++ tests/ref/fate/matroska-opus-exact-ts-remux | 60 +++++++++++ tests/ref/seek/mkv-exact-ts | 107 ++++++++++++++++++++ 5 files changed, 233 insertions(+) create mode 100644 tests/ref/fate/matroska-opus-exact-ts create mode 100644 tests/ref/fate/matroska-opus-exact-ts-remux create mode 100644 tests/ref/seek/mkv-exact-ts diff --git a/tests/fate/matroska.mak b/tests/fate/matroska.mak index abe374d75f8..b59c3d93bf4 100644 --- a/tests/fate/matroska.mak +++ b/tests/fate/matroska.mak @@ -17,6 +17,12 @@ fate-matroska-remux: REF = b9c7b650349972c9dce42ab79b472917 FATE_MATROSKA-$(call FRAMECRC, MATROSKA,, VORBIS_PARSER) += fate-matroska-xiph-lacing fate-matroska-xiph-lacing: CMD = framecrc -i $(TARGET_SAMPLES)/mkv/xiph_lacing.mka -c:a copy +FATE_MATROSKA-$(call FRAMECRC, MATROSKA,, OPUS_PARSER) += fate-matroska-opus-exact-ts +fate-matroska-opus-exact-ts: CMD = framecrc -fflags +exact_ts -i $(TARGET_SAMPLES)/mkv/codec_delay_opus.mkv -c copy + +FATE_MATROSKA_FFMPEG_FFPROBE-$(call REMUX, MATROSKA, OPUS_PARSER) += fate-matroska-opus-exact-ts-remux +fate-matroska-opus-exact-ts-remux: CMD = transcode matroska $(TARGET_SAMPLES)/mkv/codec_delay_opus.mkv matroska "-fflags +exact_ts -c copy" "-c copy" "" "" "" "-fflags +exact_ts" + # This tests that the Matroska demuxer correctly demuxes WavPack # without CodecPrivate; it also tests zlib compressed WavPack. FATE_MATROSKA-$(call FRAMECRC, MATROSKA,, ZLIB) += fate-matroska-wavpack-missing-codecprivate diff --git a/tests/fate/seek.mak b/tests/fate/seek.mak index d33cef4d928..120c745a006 100644 --- a/tests/fate/seek.mak +++ b/tests/fate/seek.mak @@ -184,6 +184,7 @@ FATE_SEEK += $(FATE_SEEK_LAVF_IMAGE2PIPE) FATE_SEEK_EXTRA-$(CONFIG_MP3_DEMUXER) += fate-seek-extra-mp3 FATE_SEEK_EXTRA-$(call ALLYES, CACHE_PROTOCOL PIPE_PROTOCOL MP3_DEMUXER) += fate-seek-cache-pipe FATE_SEEK_EXTRA-$(CONFIG_MATROSKA_DEMUXER) += fate-seek-mkv-codec-delay +FATE_SEEK_EXTRA-$(CONFIG_MATROSKA_DEMUXER) += fate-seek-mkv-exact-ts FATE_SEEK_EXTRA-$(call ALLYES, MOV_DEMUXER) += fate-seek-extra-mp4 FATE_SEEK_EXTRA-$(call ALLYES, MOV_DEMUXER) += fate-seek-empty-edit-mp4 FATE_SEEK_EXTRA-$(call ALLYES, MOV_DEMUXER) += fate-seek-test-iibbibb-mp4 @@ -198,6 +199,7 @@ fate-seek-test-iibbibb-mp4: CMD = run libavformat/tests/seek$(EXESUF) $(TARGET_ fate-seek-test-iibbibb-neg-ctts-mp4: CMD = run libavformat/tests/seek$(EXESUF) $(TARGET_SAMPLES)/mov/test_iibbibb_neg_ctts.mp4 -duration 13 -frames 4 fate-seek-cache-pipe: CMD = cat $(SAMPLES)/gapless/gapless.mp3 | run libavformat/tests/seek$(EXESUF) cache:pipe:0 -read_ahead_limit -1 fate-seek-mkv-codec-delay: CMD = run libavformat/tests/seek$(EXESUF) $(TARGET_SAMPLES)/mkv/codec_delay_opus.mkv +fate-seek-mkv-exact-ts: CMD = run libavformat/tests/seek$(EXESUF) $(TARGET_SAMPLES)/mkv/test7_cut.mkv -duration 4 -frames 3 -fflags +exact_ts # A master playlist with separate audio and video media playlists with different DTS. tests/data/hls-seek.m3u8: ffmpeg$(PROGSSUF)$(EXESUF) | tests/data diff --git a/tests/ref/fate/matroska-opus-exact-ts b/tests/ref/fate/matroska-opus-exact-ts new file mode 100644 index 00000000000..e18329417e5 --- /dev/null +++ b/tests/ref/fate/matroska-opus-exact-ts @@ -0,0 +1,58 @@ +#extradata 0: 19, 0x3a04048f +#tb 0: 1/1000 +#media_type 0: audio +#codec_id 0: opus +#sample_rate 0: 48000 +#channel_layout_name 0: mono +0, -7, -7, 20, 320, 0x58b9a88d, S=1, Skip Samples, 10, 0x02390039 +0, 14, 14, 20, 159, 0x6c9c4b4c +0, 34, 34, 20, 148, 0x0caf4b5d +0, 54, 54, 20, 139, 0xc5624226 +0, 74, 74, 20, 146, 0x633c4937 +0, 94, 94, 20, 153, 0x3d0b4f93 +0, 114, 114, 20, 158, 0xe5c55641 +0, 134, 134, 20, 156, 0xf2fd50ef +0, 154, 154, 20, 158, 0x93b15410 +0, 174, 174, 20, 157, 0xb6f74f5f +0, 194, 194, 20, 159, 0x9aff4957 +0, 214, 214, 20, 153, 0xfc5f4aba +0, 234, 234, 20, 158, 0x01e44f70 +0, 254, 254, 20, 153, 0x227149cf +0, 274, 274, 20, 155, 0x312f4cf6 +0, 294, 294, 20, 155, 0xafc54bae +0, 314, 314, 20, 151, 0x7b4252b3 +0, 334, 334, 20, 155, 0x29074a75 +0, 354, 354, 20, 149, 0x82c44bcd +0, 374, 374, 20, 150, 0x55c24eb5 +0, 394, 394, 20, 156, 0xf71d4f33 +0, 414, 414, 20, 153, 0x9b6c4ae5 +0, 434, 434, 20, 156, 0x75954e51 +0, 454, 454, 20, 155, 0x28ff4ff3 +0, 474, 474, 20, 153, 0xc4424969 +0, 494, 494, 20, 154, 0xfbf94cc8 +0, 514, 514, 20, 155, 0x52c549af +0, 534, 534, 20, 150, 0x6f1e4b7a +0, 554, 554, 20, 158, 0xabb45566 +0, 574, 574, 20, 157, 0xe61d4a99 +0, 594, 594, 20, 159, 0xf45d4fac +0, 614, 614, 20, 159, 0xcd0553a5 +0, 634, 634, 20, 156, 0xdb244e63 +0, 654, 654, 20, 154, 0x78654c52 +0, 674, 674, 20, 154, 0x9f804cc8 +0, 694, 694, 20, 150, 0x1fdf4c80 +0, 714, 714, 20, 155, 0x1adc4f89 +0, 734, 734, 20, 155, 0x4b53511c +0, 754, 754, 20, 151, 0x8ff2546d +0, 774, 774, 20, 158, 0xb7e34f1b +0, 794, 794, 20, 154, 0x4d98474b +0, 814, 814, 20, 154, 0x14924ea8 +0, 834, 834, 20, 153, 0x8d4752bf +0, 854, 854, 20, 149, 0x74785066 +0, 874, 874, 20, 151, 0x36c94a4c +0, 894, 894, 20, 155, 0x82904f3b +0, 914, 914, 20, 154, 0xd76b4a45 +0, 934, 934, 20, 159, 0x9fec548d +0, 954, 954, 20, 154, 0x9a084dcd +0, 974, 974, 20, 155, 0x90a54ac8 +0, 994, 994, 20, 324, 0x8e34a2f5 +0, 1014, 1014, 20, 268, 0x10f37203, S=1, Skip Samples, 10, 0x049600c4 diff --git a/tests/ref/fate/matroska-opus-exact-ts-remux b/tests/ref/fate/matroska-opus-exact-ts-remux new file mode 100644 index 00000000000..95181327e59 --- /dev/null +++ b/tests/ref/fate/matroska-opus-exact-ts-remux @@ -0,0 +1,60 @@ +b9881205f8945fefc16a6f23474071a6 *tests/data/fate/matroska-opus-exact-ts-remux.matroska +9358 tests/data/fate/matroska-opus-exact-ts-remux.matroska +#extradata 0: 19, 0x3a04048f +#tb 0: 1/1000 +#media_type 0: audio +#codec_id 0: opus +#sample_rate 0: 48000 +#channel_layout_name 0: mono +0, -7, -7, 20, 320, 0x58b9a88d, S=1, Skip Samples, 10, 0x02390039 +0, 14, 14, 20, 159, 0x6c9c4b4c +0, 34, 34, 20, 148, 0x0caf4b5d +0, 54, 54, 20, 139, 0xc5624226 +0, 74, 74, 20, 146, 0x633c4937 +0, 94, 94, 20, 153, 0x3d0b4f93 +0, 114, 114, 20, 158, 0xe5c55641 +0, 134, 134, 20, 156, 0xf2fd50ef +0, 154, 154, 20, 158, 0x93b15410 +0, 174, 174, 20, 157, 0xb6f74f5f +0, 194, 194, 20, 159, 0x9aff4957 +0, 214, 214, 20, 153, 0xfc5f4aba +0, 234, 234, 20, 158, 0x01e44f70 +0, 254, 254, 20, 153, 0x227149cf +0, 274, 274, 20, 155, 0x312f4cf6 +0, 294, 294, 20, 155, 0xafc54bae +0, 314, 314, 20, 151, 0x7b4252b3 +0, 334, 334, 20, 155, 0x29074a75 +0, 354, 354, 20, 149, 0x82c44bcd +0, 374, 374, 20, 150, 0x55c24eb5 +0, 394, 394, 20, 156, 0xf71d4f33 +0, 414, 414, 20, 153, 0x9b6c4ae5 +0, 434, 434, 20, 156, 0x75954e51 +0, 454, 454, 20, 155, 0x28ff4ff3 +0, 474, 474, 20, 153, 0xc4424969 +0, 494, 494, 20, 154, 0xfbf94cc8 +0, 514, 514, 20, 155, 0x52c549af +0, 534, 534, 20, 150, 0x6f1e4b7a +0, 554, 554, 20, 158, 0xabb45566 +0, 574, 574, 20, 157, 0xe61d4a99 +0, 594, 594, 20, 159, 0xf45d4fac +0, 614, 614, 20, 159, 0xcd0553a5 +0, 634, 634, 20, 156, 0xdb244e63 +0, 654, 654, 20, 154, 0x78654c52 +0, 674, 674, 20, 154, 0x9f804cc8 +0, 694, 694, 20, 150, 0x1fdf4c80 +0, 714, 714, 20, 155, 0x1adc4f89 +0, 734, 734, 20, 155, 0x4b53511c +0, 754, 754, 20, 151, 0x8ff2546d +0, 774, 774, 20, 158, 0xb7e34f1b +0, 794, 794, 20, 154, 0x4d98474b +0, 814, 814, 20, 154, 0x14924ea8 +0, 834, 834, 20, 153, 0x8d4752bf +0, 854, 854, 20, 149, 0x74785066 +0, 874, 874, 20, 151, 0x36c94a4c +0, 894, 894, 20, 155, 0x82904f3b +0, 914, 914, 20, 154, 0xd76b4a45 +0, 934, 934, 20, 159, 0x9fec548d +0, 954, 954, 20, 154, 0x9a084dcd +0, 974, 974, 20, 155, 0x90a54ac8 +0, 994, 994, 20, 324, 0x8e34a2f5 +0, 1014, 1014, 20, 268, 0x10f37203, S=1, Skip Samples, 10, 0x049600c4 diff --git a/tests/ref/seek/mkv-exact-ts b/tests/ref/seek/mkv-exact-ts new file mode 100644 index 00000000000..f281eceb75e --- /dev/null +++ b/tests/ref/seek/mkv-exact-ts @@ -0,0 +1,107 @@ +ret: 0 st: 1 flags:1 dts: 0.008000 pts: 0.008000 pos: 1783 size: 528 +ret: 0 st: 1 flags:1 dts: 0.029000 pts: 0.029000 pos: 1783 size: 510 +ret: 0 st: 1 flags:1 dts: 0.050000 pts: 0.050000 pos: 1783 size: 500 +ret: 0 st:-1 flags:0 ts:-1.000000 +ret: 0 st: 1 flags:1 dts: 0.008000 pts: 0.008000 pos: 1783 size: 528 +ret: 0 st: 1 flags:1 dts: 0.029000 pts: 0.029000 pos: 1783 size: 510 +ret: 0 st: 1 flags:1 dts: 0.050000 pts: 0.050000 pos: 1783 size: 500 +ret: 0 st:-1 flags:1 ts: 1.894167 +ret: 0 st: 1 flags:1 dts: 1.993000 pts: 1.993000 pos: 988964 size: 459 +ret: 0 st: 0 flags:1 dts: NOPTS pts: 2.000000 pos: 989431 size: 60241 +ret: 0 st: 1 flags:1 dts: 2.014000 pts: 2.014000 pos:1049679 size: 481 +ret: 0 st: 0 flags:0 ts: 0.788000 +ret: 0 st: 1 flags:1 dts: 1.993000 pts: 1.993000 pos: 988964 size: 459 +ret: 0 st: 0 flags:1 dts: NOPTS pts: 2.000000 pos: 989431 size: 60241 +ret: 0 st: 1 flags:1 dts: 2.014000 pts: 2.014000 pos:1049679 size: 481 +ret: 0 st: 0 flags:1 ts:-0.317000 +ret: 0 st: 1 flags:1 dts: 0.008000 pts: 0.008000 pos: 1783 size: 528 +ret: 0 st: 1 flags:1 dts: 0.029000 pts: 0.029000 pos: 1783 size: 510 +ret: 0 st: 1 flags:1 dts: 0.050000 pts: 0.050000 pos: 1783 size: 500 +ret: 0 st: 1 flags:0 ts: 2.577000 +ret: 0 st: 1 flags:1 dts: 2.696000 pts: 2.696000 pos:1369672 size: 507 +ret: 0 st: 1 flags:1 dts: 2.717000 pts: 2.717000 pos:1369672 size: 480 +ret: 0 st: 1 flags:1 dts: 2.738000 pts: 2.738000 pos:1369672 size: 492 +ret: 0 st: 1 flags:1 ts: 1.471000 +ret: 0 st: 1 flags:1 dts: 0.990000 pts: 0.990000 pos: 450438 size: 487 +ret: 0 st: 1 flags:1 dts: 1.011000 pts: 1.011000 pos: 450438 size: 485 +ret: 0 st: 1 flags:1 dts: 1.993000 pts: 1.993000 pos: 988964 size: 459 +ret: 0 st:-1 flags:0 ts: 0.365002 +ret: 0 st: 1 flags:1 dts: 1.993000 pts: 1.993000 pos: 988964 size: 459 +ret: 0 st: 0 flags:1 dts: NOPTS pts: 2.000000 pos: 989431 size: 60241 +ret: 0 st: 1 flags:1 dts: 2.014000 pts: 2.014000 pos:1049679 size: 481 +ret: 0 st:-1 flags:1 ts:-0.740831 +ret: 0 st: 1 flags:1 dts: 0.008000 pts: 0.008000 pos: 1783 size: 528 +ret: 0 st: 1 flags:1 dts: 0.029000 pts: 0.029000 pos: 1783 size: 510 +ret: 0 st: 1 flags:1 dts: 0.050000 pts: 0.050000 pos: 1783 size: 500 +ret: 0 st: 0 flags:0 ts: 2.153000 +ret: 0 st: 0 flags:1 dts: NOPTS pts: 3.000000 pos:1530084 size: 64129 +ret: 0 st: 1 flags:1 dts: 3.038000 pts: 3.038000 pos:1594220 size: 483 +ret: 0 st: 1 flags:1 dts: 3.059000 pts: 3.059000 pos:1594220 size: 639 +ret: 0 st: 0 flags:1 ts: 1.048000 +ret: 0 st: 1 flags:1 dts: 1.993000 pts: 1.993000 pos: 988964 size: 459 +ret: 0 st: 0 flags:1 dts: NOPTS pts: 2.000000 pos: 989431 size: 60241 +ret: 0 st: 1 flags:1 dts: 2.014000 pts: 2.014000 pos:1049679 size: 481 +ret: 0 st: 1 flags:0 ts:-0.058000 +ret: 0 st: 1 flags:1 dts: 0.008000 pts: 0.008000 pos: 1783 size: 528 +ret: 0 st: 1 flags:1 dts: 0.029000 pts: 0.029000 pos: 1783 size: 510 +ret: 0 st: 1 flags:1 dts: 0.050000 pts: 0.050000 pos: 1783 size: 500 +ret: 0 st: 1 flags:1 ts: 2.836000 +ret: 0 st: 1 flags:1 dts: 2.696000 pts: 2.696000 pos:1369672 size: 507 +ret: 0 st: 1 flags:1 dts: 2.717000 pts: 2.717000 pos:1369672 size: 480 +ret: 0 st: 1 flags:1 dts: 2.738000 pts: 2.738000 pos:1369672 size: 492 +ret: 0 st:-1 flags:0 ts: 1.730004 +ret: 0 st: 0 flags:1 dts: NOPTS pts: 2.000000 pos: 989431 size: 60241 +ret: 0 st: 1 flags:1 dts: 2.014000 pts: 2.014000 pos:1049679 size: 481 +ret: 0 st: 1 flags:1 dts: 2.035000 pts: 2.035000 pos:1049679 size: 473 +ret: 0 st:-1 flags:1 ts: 0.624171 +ret: 0 st: 1 flags:1 dts: 0.008000 pts: 0.008000 pos: 1783 size: 528 +ret: 0 st: 1 flags:1 dts: 0.029000 pts: 0.029000 pos: 1783 size: 510 +ret: 0 st: 1 flags:1 dts: 0.050000 pts: 0.050000 pos: 1783 size: 500 +ret: 0 st: 0 flags:0 ts:-0.482000 +ret: 0 st: 1 flags:1 dts: 0.008000 pts: 0.008000 pos: 1783 size: 528 +ret: 0 st: 1 flags:1 dts: 0.029000 pts: 0.029000 pos: 1783 size: 510 +ret: 0 st: 1 flags:1 dts: 0.050000 pts: 0.050000 pos: 1783 size: 500 +ret: 0 st: 0 flags:1 ts: 2.413000 +ret: 0 st: 0 flags:1 dts: NOPTS pts: 2.000000 pos: 989431 size: 60241 +ret: 0 st: 1 flags:1 dts: 2.014000 pts: 2.014000 pos:1049679 size: 481 +ret: 0 st: 1 flags:1 dts: 2.035000 pts: 2.035000 pos:1049679 size: 473 +ret: 0 st: 1 flags:0 ts: 1.307000 +ret: 0 st: 1 flags:1 dts: 1.993000 pts: 1.993000 pos: 988964 size: 459 +ret: 0 st: 0 flags:1 dts: NOPTS pts: 2.000000 pos: 989431 size: 60241 +ret: 0 st: 1 flags:1 dts: 2.014000 pts: 2.014000 pos:1049679 size: 481 +ret: 0 st: 1 flags:1 ts: 0.201000 +ret: 0 st: 1 flags:1 dts: 0.179000 pts: 0.179000 pos: 85877 size: 501 +ret: 0 st: 1 flags:1 dts: 0.200000 pts: 0.200000 pos: 85877 size: 521 +ret: 0 st: 1 flags:1 dts: 0.221000 pts: 0.221000 pos: 85877 size: 646 +ret: 0 st:-1 flags:0 ts:-0.904994 +ret: 0 st: 1 flags:1 dts: 0.008000 pts: 0.008000 pos: 1783 size: 528 +ret: 0 st: 1 flags:1 dts: 0.029000 pts: 0.029000 pos: 1783 size: 510 +ret: 0 st: 1 flags:1 dts: 0.050000 pts: 0.050000 pos: 1783 size: 500 +ret: 0 st:-1 flags:1 ts: 1.989173 +ret: 0 st: 1 flags:1 dts: 1.993000 pts: 1.993000 pos: 988964 size: 459 +ret: 0 st: 0 flags:1 dts: NOPTS pts: 2.000000 pos: 989431 size: 60241 +ret: 0 st: 1 flags:1 dts: 2.014000 pts: 2.014000 pos:1049679 size: 481 +ret: 0 st: 0 flags:0 ts: 0.883000 +ret: 0 st: 1 flags:1 dts: 1.993000 pts: 1.993000 pos: 988964 size: 459 +ret: 0 st: 0 flags:1 dts: NOPTS pts: 2.000000 pos: 989431 size: 60241 +ret: 0 st: 1 flags:1 dts: 2.014000 pts: 2.014000 pos:1049679 size: 481 +ret: 0 st: 0 flags:1 ts:-0.222000 +ret: 0 st: 1 flags:1 dts: 0.008000 pts: 0.008000 pos: 1783 size: 528 +ret: 0 st: 1 flags:1 dts: 0.029000 pts: 0.029000 pos: 1783 size: 510 +ret: 0 st: 1 flags:1 dts: 0.050000 pts: 0.050000 pos: 1783 size: 500 +ret: 0 st: 1 flags:0 ts: 2.672000 +ret: 0 st: 1 flags:1 dts: 2.696000 pts: 2.696000 pos:1369672 size: 507 +ret: 0 st: 1 flags:1 dts: 2.717000 pts: 2.717000 pos:1369672 size: 480 +ret: 0 st: 1 flags:1 dts: 2.738000 pts: 2.738000 pos:1369672 size: 492 +ret: 0 st: 1 flags:1 ts: 1.566000 +ret: 0 st: 1 flags:1 dts: 0.990000 pts: 0.990000 pos: 450438 size: 487 +ret: 0 st: 1 flags:1 dts: 1.011000 pts: 1.011000 pos: 450438 size: 485 +ret: 0 st: 1 flags:1 dts: 1.993000 pts: 1.993000 pos: 988964 size: 459 +ret: 0 st:-1 flags:0 ts: 0.460008 +ret: 0 st: 1 flags:1 dts: 1.993000 pts: 1.993000 pos: 988964 size: 459 +ret: 0 st: 0 flags:1 dts: NOPTS pts: 2.000000 pos: 989431 size: 60241 +ret: 0 st: 1 flags:1 dts: 2.014000 pts: 2.014000 pos:1049679 size: 481 +ret: 0 st:-1 flags:1 ts:-0.645825 +ret: 0 st: 1 flags:1 dts: 0.008000 pts: 0.008000 pos: 1783 size: 528 +ret: 0 st: 1 flags:1 dts: 0.029000 pts: 0.029000 pos: 1783 size: 510 +ret: 0 st: 1 flags:1 dts: 0.050000 pts: 0.050000 pos: 1783 size: 500 -- 2.54.0 >From 8f596f7000fd2cfaabf012843ee2f33266906df9 Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Thu, 12 Nov 2026 12:42:52 -0600 Subject: [PATCH 4/6] avformat/matroskadec: factor out trusting the default duration mkv_trust_default_duration() holds the check of a track's default duration that matroska_parse_block() made inline, so that the setup of a track can apply it too. --- libavformat/matroskadec.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index 09639627976..929fca607b6 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -3166,6 +3166,18 @@ static int mkv_parse_subtitle_codec(MatroskaTrack *track, AVStream *st, return 0; } +static int mkv_trust_default_duration(const MatroskaTrack *track, + const AVCodecParameters *par) +{ + if (!track->default_duration) + return 0; + // If this is needed for more codecs, then add them here + if (track->audio.samplerate == 8000 && par->codec_id == AV_CODEC_ID_AC3 && + (track->audio.samplerate != par->sample_rate || !par->frame_size)) + return 0; + return 1; +} + static int matroska_parse_tracks(AVFormatContext *s) { MatroskaDemuxContext *matroska = s->priv_data; @@ -4301,7 +4313,6 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, AVBufferRef *buf uint32_t lace_size[256]; int n, flags, laces = 0; uint64_t num; - int trust_default_duration; av_assert1(buf); @@ -4371,16 +4382,7 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, AVBufferRef *buf return res; } - trust_default_duration = track->default_duration != 0; - if (track->audio.samplerate == 8000 && trust_default_duration) { - // If this is needed for more codecs, then add them here - if (st->codecpar->codec_id == AV_CODEC_ID_AC3) { - if (track->audio.samplerate != st->codecpar->sample_rate || !st->codecpar->frame_size) - trust_default_duration = 0; - } - } - - if (!block_duration && trust_default_duration) + if (!block_duration && mkv_trust_default_duration(track, st->codecpar)) block_duration = av_rescale_q(track->default_duration * laces, (AVRational) { 1, 1000000000 }, st->time_base); -- 2.54.0 >From 28b970a2d3a544b39e25f487c35def720ccce922 Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Thu, 12 Nov 2026 12:44:54 -0600 Subject: [PATCH 5/6] avformat/matroskadec: rebuild exact audio timestamps with AVFMT_FLAG_EXACT_TS With the flag, the demuxer puts Opus, AAC and default-duration audio tracks in a sample time base and counts the samples of their packets, following the block timestamp only where it departs from that count by more than the clock's rounding, or where a BlockDuration trims the block. After a seek, packets are placed on the grid of packet lengths from the stream's origin, cue entries are moved back by the clock's rounding, and the seek target is compared in its own time base, as video tracks stay on the container's clock. The matroska-opus-exact-ts packets move to -312, 648, 1608... samples, and seek-mkv-exact-ts reads the same packets with exact AAC timestamps. --- doc/formats.texi | 1 + libavformat/Makefile | 3 +- libavformat/matroskadec.c | 156 +++++++++++++++++++++++++- tests/ref/fate/matroska-opus-exact-ts | 106 ++++++++--------- tests/ref/seek/mkv-exact-ts | 128 ++++++++++----------- 5 files changed, 270 insertions(+), 124 deletions(-) diff --git a/doc/formats.texi b/doc/formats.texi index 412b1797773..0c234706167 100644 --- a/doc/formats.texi +++ b/doc/formats.texi @@ -61,6 +61,7 @@ Try to interleave output packets by DTS. At present, available only for AVIs wit @item exact_ts Rebuild the audio timestamps from the packet lengths, in a sample time base, where the container's clock is coarser than a sample. +The Matroska demuxer supports it for Opus, AAC and tracks with a default duration. @end table Possible values for output files: diff --git a/libavformat/Makefile b/libavformat/Makefile index 891d7f82d9a..09aaaabc1f7 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -754,7 +754,8 @@ SHLIBOBJS-$(CONFIG_HLS_DEMUXER) += ac3_channel_layout_tab.o SHLIBOBJS-$(CONFIG_IMAGE_JPEGXL_PIPE_DEMUXER) += jpegxl_parse.o SHLIBOBJS-$(CONFIG_JNI) += ffjni.o SHLIBOBJS-$(CONFIG_JPEGXL_ANIM_DEMUXER) += jpegxl_parse.o -SHLIBOBJS-$(CONFIG_MATROSKA_DEMUXER) += mpeg4audio_sample_rates.o +SHLIBOBJS-$(CONFIG_MATROSKA_DEMUXER) += mpeg4audio_sample_rates.o \ + opus_frame_duration_tab.o SHLIBOBJS-$(CONFIG_MATROSKA_MUXER) += opus_frame_duration_tab.o SHLIBOBJS-$(CONFIG_MOV_DEMUXER) += ac3_channel_layout_tab.o SHLIBOBJS-$(CONFIG_MOV_MUXER) += mpegaudiotabs.o diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index 929fca607b6..e16f46a1803 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -57,6 +57,7 @@ #include "libavcodec/flac.h" #include "libavcodec/itut35.h" #include "libavcodec/mpeg4audio.h" +#include "libavcodec/opus/tab.h" #include "packet_internal.h" #include "avformat.h" @@ -290,6 +291,18 @@ typedef struct MatroskaTrack { AVStream *stream; int64_t end_timecode; int ms_compat; + /** + * AVFMT_FLAG_EXACT_TS: the timestamps are in samples, rebuilt from the + * packet lengths, and the container's clock only places the first + * packet and the ones after a gap. + */ + int exact_audio; + AVRational tick_tb; ///< the container's clock for this track + int packet_samples; ///< samples per packet when fixed, 0 to read each packet + int64_t ts_tolerance; ///< the rounding of the container's clock, in samples + int64_t next_pts; ///< where the next packet continues the last one + int64_t grid_origin; ///< the exact position of the first packet, on the container's clock + int grid_step; ///< the length of the last packet int needs_decoding; uint64_t max_block_additional_id; EbmlList block_addition_mappings; @@ -422,6 +435,7 @@ typedef struct MatroskaDemuxContext { /* What to skip before effectively reading a packet. */ int skip_to_keyframe; uint64_t skip_to_timecode; + AVRational skip_to_tb; ///< time base of skip_to_timecode /* File has a CUES element, but we defer parsing until it is needed. */ int cues_parsing_deferred; @@ -2019,11 +2033,16 @@ static void matroska_add_index_entries(MatroskaDemuxContext *matroska) for (j = 0; j < pos_list->nb_elem; j++) { MatroskaTrack *track = matroska_find_track_by_num(matroska, pos[j].track); - if (track && track->stream) + if (track && track->stream) { + int64_t ts = index[i].time / index_scale; + // The cue is the rounded timestamp of an exact one. + if (track->exact_audio) + ts = av_rescale_q(ts, track->tick_tb, track->stream->time_base) - + track->ts_tolerance; av_add_index_entry(track->stream, pos[j].pos + matroska->segment_start, - index[i].time / index_scale, 0, 0, - AVINDEX_KEYFRAME); + ts, 0, 0, AVINDEX_KEYFRAME); + } } } } @@ -3178,6 +3197,63 @@ static int mkv_trust_default_duration(const MatroskaTrack *track, return 1; } +/** + * Rebuild the timestamps of an audio track in samples, from the packet + * lengths, when the container's clock is coarser than a sample. The + * packet length comes from the codec where it is known from the packet or + * the stream, or from the track's default duration, which is in + * nanoseconds. + */ +static void mka_setup_exact_timestamps(MatroskaTrack *track, AVStream *st, + MatroskaDemuxContext *matroska) +{ + AVCodecParameters *par = st->codecpar; + int rate = par->codec_id == AV_CODEC_ID_OPUS ? 48000 : par->sample_rate; + int samples = 0; + + // A full parser re-times the packets it splits or merges. + if (track->ms_compat || track->audio.buf || rate <= 0 || + ffstream(st)->need_parsing == AVSTREAM_PARSE_FULL || + ffstream(st)->need_parsing == AVSTREAM_PARSE_FULL_RAW) + return; + switch (par->codec_id) { + case AV_CODEC_ID_OPUS: + break; + case AV_CODEC_ID_AAC: { + MPEG4AudioConfig cfg; + int frame_length; + + if (avpriv_mpeg4audio_get_config2(&cfg, par->extradata, par->extradata_size, + 1, matroska->ctx) < 0 || cfg.sample_rate <= 0) + return; + // A frame holds its samples at the core rate, whatever the SBR + // signalling. + frame_length = cfg.frame_length_short ? 960 : 1024; + if ((int64_t)frame_length * rate % cfg.sample_rate) + return; + samples = (int64_t)frame_length * rate / cfg.sample_rate; + break; + } + default: + if (!mkv_trust_default_duration(track, par)) + return; + samples = av_rescale_rnd(track->default_duration, rate, 1000000000, AV_ROUND_NEAR_INF); + if (samples <= 0) + return; + } + track->exact_audio = 1; + track->packet_samples = samples; + track->tick_tb = st->time_base; + track->next_pts = AV_NOPTS_VALUE; + track->grid_origin = AV_NOPTS_VALUE; + track->ts_tolerance = av_rescale_q_rnd(1, track->tick_tb, (AVRational){ 1, rate }, + AV_ROUND_UP); + track->codec_delay_in_track_tb = av_rescale_q(track->codec_delay, + (AVRational){ 1, 1000000000 }, + (AVRational){ 1, rate }); + avpriv_set_pts_info(st, 64, 1, rate); +} + static int matroska_parse_tracks(AVFormatContext *s) { MatroskaDemuxContext *matroska = s->priv_data; @@ -3367,6 +3443,9 @@ static int matroska_parse_tracks(AVFormatContext *s) memcpy(par->extradata, src, extra_size); } + if (type == MATROSKA_TRACK_TYPE_AUDIO && s->flags & AVFMT_FLAG_EXACT_TS) + mka_setup_exact_timestamps(track, st, matroska); + ret = mkv_parse_block_addition_mappings(s, st, track); if (ret < 0) return ret; @@ -4311,6 +4390,9 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, AVBufferRef *buf AVStream *st; int16_t block_time; uint32_t lace_size[256]; + int64_t lace_samples[256]; + int64_t explicit_duration; + int laces_in_samples = 0; int n, flags, laces = 0; uint64_t num; @@ -4349,7 +4431,27 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, AVBufferRef *buf if (cluster_time != (uint64_t) -1 && (block_time >= 0 || cluster_time >= -block_time)) { uint64_t timecode_cluster_in_track_tb = (double) cluster_time / track->time_scale; - timecode = timecode_cluster_in_track_tb + block_time - track->codec_delay_in_track_tb; + timecode = timecode_cluster_in_track_tb + block_time; + if (track->exact_audio) { + int64_t ts = av_rescale_q(timecode, track->tick_tb, st->time_base); + // The clock only places the packets after a gap: the others + // continue the one before, or, after a seek, sit on the grid + // of packets that starts at the first one, which starts at the + // stream's origin unless the clock says otherwise. + if (track->grid_origin == AV_NOPTS_VALUE) + track->grid_origin = FFABS(ts) <= track->ts_tolerance ? 0 : ts; + if (track->next_pts != AV_NOPTS_VALUE && + FFABS(ts - track->next_pts) <= track->ts_tolerance) { + ts = track->next_pts; + } else if (track->grid_step > 0) { + int64_t on_grid = track->grid_origin + + av_rescale_rnd(ts - track->grid_origin, 1, track->grid_step, AV_ROUND_NEAR_INF) * track->grid_step; + if (FFABS(on_grid - ts) <= track->ts_tolerance) + ts = on_grid; + } + timecode = ts; + } + timecode -= track->codec_delay_in_track_tb; if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE && timecode < track->end_timecode) is_keyframe = 0; /* overlapping subtitles are not key frame */ @@ -4365,7 +4467,9 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, AVBufferRef *buf // Compare signed timecodes. Timecode may be negative due to codec delay // offset. We don't support timestamps greater than int64_t anyway - see // AVPacket's pts. - if ((int64_t)timecode < (int64_t)matroska->skip_to_timecode) + if (timecode == AV_NOPTS_VALUE || + av_compare_ts(timecode, st->time_base, + matroska->skip_to_timecode, matroska->skip_to_tb) < 0) return res; if (is_keyframe) matroska->skip_to_keyframe = 0; @@ -4382,17 +4486,53 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, AVBufferRef *buf return res; } + if (track->exact_audio) + block_duration = av_rescale_q(block_duration, track->tick_tb, st->time_base); + explicit_duration = block_duration; + if (!block_duration && mkv_trust_default_duration(track, st->codecpar)) block_duration = av_rescale_q(track->default_duration * laces, (AVRational) { 1, 1000000000 }, st->time_base); + if (track->exact_audio) { + const uint8_t *lace_data = data; + int64_t block_samples = 0; + + for (n = 0; n < laces; n++) { + int samples = track->packet_samples; + if (!samples && !track->needs_decoding) + samples = ff_opus_packet_samples(lace_data, lace_size[n]); + if (samples <= 0) { + block_samples = -1; + break; + } + lace_samples[n] = samples; + block_samples += samples; + lace_data += lace_size[n]; + } + // A BlockDuration other than the packet lengths trims the block. + if (explicit_duration && + FFABS(explicit_duration - block_samples) > track->ts_tolerance) + block_samples = -1; + if (block_samples > 0 && timecode != AV_NOPTS_VALUE) { + block_duration = block_samples; + track->next_pts = timecode + track->codec_delay_in_track_tb + block_samples; + track->grid_step = lace_samples[0]; + } else { + block_samples = 0; + track->next_pts = AV_NOPTS_VALUE; + } + laces_in_samples = block_samples > 0; + } + if (cluster_time != (uint64_t)-1 && (block_time >= 0 || cluster_time >= -block_time)) track->end_timecode = FFMAX(track->end_timecode, timecode + block_duration); for (n = 0; n < laces; n++) { - int64_t lace_duration = block_duration*(n+1) / laces - block_duration*n / laces; + int64_t lace_duration = laces_in_samples ? lace_samples[n] : + block_duration*(n+1) / laces - block_duration*n / laces; uint8_t *out_data = data; int out_size = lace_size[n]; @@ -4557,6 +4697,9 @@ static int matroska_read_seek(AVFormatContext *s, int stream_index, tracks[i].audio.sub_packet_cnt = 0; tracks[i].audio.buf_timecode = AV_NOPTS_VALUE; tracks[i].end_timecode = 0; + tracks[i].next_pts = AV_NOPTS_VALUE; + if (tracks[i].exact_audio && tracks[i].grid_origin == AV_NOPTS_VALUE) + tracks[i].grid_origin = 0; } /* We seek to a level 1 element, so set the appropriate status. */ @@ -4568,6 +4711,7 @@ static int matroska_read_seek(AVFormatContext *s, int stream_index, sti->skip_to_keyframe = 1; matroska->skip_to_timecode = sti->index_entries[index].timestamp; } + matroska->skip_to_tb = st->time_base; matroska->skip_to_keyframe = 1; matroska->done = 0; avpriv_update_cur_dts(s, st, sti->index_entries[index].timestamp); diff --git a/tests/ref/fate/matroska-opus-exact-ts b/tests/ref/fate/matroska-opus-exact-ts index e18329417e5..944a0d43fe9 100644 --- a/tests/ref/fate/matroska-opus-exact-ts +++ b/tests/ref/fate/matroska-opus-exact-ts @@ -1,58 +1,58 @@ #extradata 0: 19, 0x3a04048f -#tb 0: 1/1000 +#tb 0: 1/48000 #media_type 0: audio #codec_id 0: opus #sample_rate 0: 48000 #channel_layout_name 0: mono -0, -7, -7, 20, 320, 0x58b9a88d, S=1, Skip Samples, 10, 0x02390039 -0, 14, 14, 20, 159, 0x6c9c4b4c -0, 34, 34, 20, 148, 0x0caf4b5d -0, 54, 54, 20, 139, 0xc5624226 -0, 74, 74, 20, 146, 0x633c4937 -0, 94, 94, 20, 153, 0x3d0b4f93 -0, 114, 114, 20, 158, 0xe5c55641 -0, 134, 134, 20, 156, 0xf2fd50ef -0, 154, 154, 20, 158, 0x93b15410 -0, 174, 174, 20, 157, 0xb6f74f5f -0, 194, 194, 20, 159, 0x9aff4957 -0, 214, 214, 20, 153, 0xfc5f4aba -0, 234, 234, 20, 158, 0x01e44f70 -0, 254, 254, 20, 153, 0x227149cf -0, 274, 274, 20, 155, 0x312f4cf6 -0, 294, 294, 20, 155, 0xafc54bae -0, 314, 314, 20, 151, 0x7b4252b3 -0, 334, 334, 20, 155, 0x29074a75 -0, 354, 354, 20, 149, 0x82c44bcd -0, 374, 374, 20, 150, 0x55c24eb5 -0, 394, 394, 20, 156, 0xf71d4f33 -0, 414, 414, 20, 153, 0x9b6c4ae5 -0, 434, 434, 20, 156, 0x75954e51 -0, 454, 454, 20, 155, 0x28ff4ff3 -0, 474, 474, 20, 153, 0xc4424969 -0, 494, 494, 20, 154, 0xfbf94cc8 -0, 514, 514, 20, 155, 0x52c549af -0, 534, 534, 20, 150, 0x6f1e4b7a -0, 554, 554, 20, 158, 0xabb45566 -0, 574, 574, 20, 157, 0xe61d4a99 -0, 594, 594, 20, 159, 0xf45d4fac -0, 614, 614, 20, 159, 0xcd0553a5 -0, 634, 634, 20, 156, 0xdb244e63 -0, 654, 654, 20, 154, 0x78654c52 -0, 674, 674, 20, 154, 0x9f804cc8 -0, 694, 694, 20, 150, 0x1fdf4c80 -0, 714, 714, 20, 155, 0x1adc4f89 -0, 734, 734, 20, 155, 0x4b53511c -0, 754, 754, 20, 151, 0x8ff2546d -0, 774, 774, 20, 158, 0xb7e34f1b -0, 794, 794, 20, 154, 0x4d98474b -0, 814, 814, 20, 154, 0x14924ea8 -0, 834, 834, 20, 153, 0x8d4752bf -0, 854, 854, 20, 149, 0x74785066 -0, 874, 874, 20, 151, 0x36c94a4c -0, 894, 894, 20, 155, 0x82904f3b -0, 914, 914, 20, 154, 0xd76b4a45 -0, 934, 934, 20, 159, 0x9fec548d -0, 954, 954, 20, 154, 0x9a084dcd -0, 974, 974, 20, 155, 0x90a54ac8 -0, 994, 994, 20, 324, 0x8e34a2f5 -0, 1014, 1014, 20, 268, 0x10f37203, S=1, Skip Samples, 10, 0x049600c4 +0, -312, -312, 960, 320, 0x58b9a88d, S=1, Skip Samples, 10, 0x02390039 +0, 648, 648, 960, 159, 0x6c9c4b4c +0, 1608, 1608, 960, 148, 0x0caf4b5d +0, 2568, 2568, 960, 139, 0xc5624226 +0, 3528, 3528, 960, 146, 0x633c4937 +0, 4488, 4488, 960, 153, 0x3d0b4f93 +0, 5448, 5448, 960, 158, 0xe5c55641 +0, 6408, 6408, 960, 156, 0xf2fd50ef +0, 7368, 7368, 960, 158, 0x93b15410 +0, 8328, 8328, 960, 157, 0xb6f74f5f +0, 9288, 9288, 960, 159, 0x9aff4957 +0, 10248, 10248, 960, 153, 0xfc5f4aba +0, 11208, 11208, 960, 158, 0x01e44f70 +0, 12168, 12168, 960, 153, 0x227149cf +0, 13128, 13128, 960, 155, 0x312f4cf6 +0, 14088, 14088, 960, 155, 0xafc54bae +0, 15048, 15048, 960, 151, 0x7b4252b3 +0, 16008, 16008, 960, 155, 0x29074a75 +0, 16968, 16968, 960, 149, 0x82c44bcd +0, 17928, 17928, 960, 150, 0x55c24eb5 +0, 18888, 18888, 960, 156, 0xf71d4f33 +0, 19848, 19848, 960, 153, 0x9b6c4ae5 +0, 20808, 20808, 960, 156, 0x75954e51 +0, 21768, 21768, 960, 155, 0x28ff4ff3 +0, 22728, 22728, 960, 153, 0xc4424969 +0, 23688, 23688, 960, 154, 0xfbf94cc8 +0, 24648, 24648, 960, 155, 0x52c549af +0, 25608, 25608, 960, 150, 0x6f1e4b7a +0, 26568, 26568, 960, 158, 0xabb45566 +0, 27528, 27528, 960, 157, 0xe61d4a99 +0, 28488, 28488, 960, 159, 0xf45d4fac +0, 29448, 29448, 960, 159, 0xcd0553a5 +0, 30408, 30408, 960, 156, 0xdb244e63 +0, 31368, 31368, 960, 154, 0x78654c52 +0, 32328, 32328, 960, 154, 0x9f804cc8 +0, 33288, 33288, 960, 150, 0x1fdf4c80 +0, 34248, 34248, 960, 155, 0x1adc4f89 +0, 35208, 35208, 960, 155, 0x4b53511c +0, 36168, 36168, 960, 151, 0x8ff2546d +0, 37128, 37128, 960, 158, 0xb7e34f1b +0, 38088, 38088, 960, 154, 0x4d98474b +0, 39048, 39048, 960, 154, 0x14924ea8 +0, 40008, 40008, 960, 153, 0x8d4752bf +0, 40968, 40968, 960, 149, 0x74785066 +0, 41928, 41928, 960, 151, 0x36c94a4c +0, 42888, 42888, 960, 155, 0x82904f3b +0, 43848, 43848, 960, 154, 0xd76b4a45 +0, 44808, 44808, 960, 159, 0x9fec548d +0, 45768, 45768, 960, 154, 0x9a084dcd +0, 46728, 46728, 960, 155, 0x90a54ac8 +0, 47688, 47688, 960, 324, 0x8e34a2f5 +0, 48648, 48648, 960, 268, 0x10f37203, S=1, Skip Samples, 10, 0x049600c4 diff --git a/tests/ref/seek/mkv-exact-ts b/tests/ref/seek/mkv-exact-ts index f281eceb75e..9ce27d5df22 100644 --- a/tests/ref/seek/mkv-exact-ts +++ b/tests/ref/seek/mkv-exact-ts @@ -1,107 +1,107 @@ ret: 0 st: 1 flags:1 dts: 0.008000 pts: 0.008000 pos: 1783 size: 528 -ret: 0 st: 1 flags:1 dts: 0.029000 pts: 0.029000 pos: 1783 size: 510 -ret: 0 st: 1 flags:1 dts: 0.050000 pts: 0.050000 pos: 1783 size: 500 +ret: 0 st: 1 flags:1 dts: 0.029333 pts: 0.029333 pos: 1783 size: 510 +ret: 0 st: 1 flags:1 dts: 0.050667 pts: 0.050667 pos: 1783 size: 500 ret: 0 st:-1 flags:0 ts:-1.000000 ret: 0 st: 1 flags:1 dts: 0.008000 pts: 0.008000 pos: 1783 size: 528 -ret: 0 st: 1 flags:1 dts: 0.029000 pts: 0.029000 pos: 1783 size: 510 -ret: 0 st: 1 flags:1 dts: 0.050000 pts: 0.050000 pos: 1783 size: 500 +ret: 0 st: 1 flags:1 dts: 0.029333 pts: 0.029333 pos: 1783 size: 510 +ret: 0 st: 1 flags:1 dts: 0.050667 pts: 0.050667 pos: 1783 size: 500 ret: 0 st:-1 flags:1 ts: 1.894167 -ret: 0 st: 1 flags:1 dts: 1.993000 pts: 1.993000 pos: 988964 size: 459 +ret: 0 st: 1 flags:1 dts: 1.992000 pts: 1.992000 pos: 988964 size: 459 ret: 0 st: 0 flags:1 dts: NOPTS pts: 2.000000 pos: 989431 size: 60241 -ret: 0 st: 1 flags:1 dts: 2.014000 pts: 2.014000 pos:1049679 size: 481 +ret: 0 st: 1 flags:1 dts: 2.013333 pts: 2.013333 pos:1049679 size: 481 ret: 0 st: 0 flags:0 ts: 0.788000 -ret: 0 st: 1 flags:1 dts: 1.993000 pts: 1.993000 pos: 988964 size: 459 +ret: 0 st: 1 flags:1 dts: 1.992000 pts: 1.992000 pos: 988964 size: 459 ret: 0 st: 0 flags:1 dts: NOPTS pts: 2.000000 pos: 989431 size: 60241 -ret: 0 st: 1 flags:1 dts: 2.014000 pts: 2.014000 pos:1049679 size: 481 +ret: 0 st: 1 flags:1 dts: 2.013333 pts: 2.013333 pos:1049679 size: 481 ret: 0 st: 0 flags:1 ts:-0.317000 ret: 0 st: 1 flags:1 dts: 0.008000 pts: 0.008000 pos: 1783 size: 528 -ret: 0 st: 1 flags:1 dts: 0.029000 pts: 0.029000 pos: 1783 size: 510 -ret: 0 st: 1 flags:1 dts: 0.050000 pts: 0.050000 pos: 1783 size: 500 -ret: 0 st: 1 flags:0 ts: 2.577000 +ret: 0 st: 1 flags:1 dts: 0.029333 pts: 0.029333 pos: 1783 size: 510 +ret: 0 st: 1 flags:1 dts: 0.050667 pts: 0.050667 pos: 1783 size: 500 +ret: 0 st: 1 flags:0 ts: 2.576667 ret: 0 st: 1 flags:1 dts: 2.696000 pts: 2.696000 pos:1369672 size: 507 -ret: 0 st: 1 flags:1 dts: 2.717000 pts: 2.717000 pos:1369672 size: 480 -ret: 0 st: 1 flags:1 dts: 2.738000 pts: 2.738000 pos:1369672 size: 492 -ret: 0 st: 1 flags:1 ts: 1.471000 -ret: 0 st: 1 flags:1 dts: 0.990000 pts: 0.990000 pos: 450438 size: 487 -ret: 0 st: 1 flags:1 dts: 1.011000 pts: 1.011000 pos: 450438 size: 485 -ret: 0 st: 1 flags:1 dts: 1.993000 pts: 1.993000 pos: 988964 size: 459 +ret: 0 st: 1 flags:1 dts: 2.717333 pts: 2.717333 pos:1369672 size: 480 +ret: 0 st: 1 flags:1 dts: 2.738667 pts: 2.738667 pos:1369672 size: 492 +ret: 0 st: 1 flags:1 ts: 1.470833 +ret: 0 st: 1 flags:1 dts: 0.989333 pts: 0.989333 pos: 450438 size: 487 +ret: 0 st: 1 flags:1 dts: 1.010667 pts: 1.010667 pos: 450438 size: 485 +ret: 0 st: 1 flags:1 dts: 1.992000 pts: 1.992000 pos: 988964 size: 459 ret: 0 st:-1 flags:0 ts: 0.365002 -ret: 0 st: 1 flags:1 dts: 1.993000 pts: 1.993000 pos: 988964 size: 459 +ret: 0 st: 1 flags:1 dts: 1.992000 pts: 1.992000 pos: 988964 size: 459 ret: 0 st: 0 flags:1 dts: NOPTS pts: 2.000000 pos: 989431 size: 60241 -ret: 0 st: 1 flags:1 dts: 2.014000 pts: 2.014000 pos:1049679 size: 481 +ret: 0 st: 1 flags:1 dts: 2.013333 pts: 2.013333 pos:1049679 size: 481 ret: 0 st:-1 flags:1 ts:-0.740831 ret: 0 st: 1 flags:1 dts: 0.008000 pts: 0.008000 pos: 1783 size: 528 -ret: 0 st: 1 flags:1 dts: 0.029000 pts: 0.029000 pos: 1783 size: 510 -ret: 0 st: 1 flags:1 dts: 0.050000 pts: 0.050000 pos: 1783 size: 500 +ret: 0 st: 1 flags:1 dts: 0.029333 pts: 0.029333 pos: 1783 size: 510 +ret: 0 st: 1 flags:1 dts: 0.050667 pts: 0.050667 pos: 1783 size: 500 ret: 0 st: 0 flags:0 ts: 2.153000 ret: 0 st: 0 flags:1 dts: NOPTS pts: 3.000000 pos:1530084 size: 64129 -ret: 0 st: 1 flags:1 dts: 3.038000 pts: 3.038000 pos:1594220 size: 483 -ret: 0 st: 1 flags:1 dts: 3.059000 pts: 3.059000 pos:1594220 size: 639 +ret: 0 st: 1 flags:1 dts: 3.037333 pts: 3.037333 pos:1594220 size: 483 +ret: 0 st: 1 flags:1 dts: 3.058667 pts: 3.058667 pos:1594220 size: 639 ret: 0 st: 0 flags:1 ts: 1.048000 -ret: 0 st: 1 flags:1 dts: 1.993000 pts: 1.993000 pos: 988964 size: 459 +ret: 0 st: 1 flags:1 dts: 1.992000 pts: 1.992000 pos: 988964 size: 459 ret: 0 st: 0 flags:1 dts: NOPTS pts: 2.000000 pos: 989431 size: 60241 -ret: 0 st: 1 flags:1 dts: 2.014000 pts: 2.014000 pos:1049679 size: 481 -ret: 0 st: 1 flags:0 ts:-0.058000 +ret: 0 st: 1 flags:1 dts: 2.013333 pts: 2.013333 pos:1049679 size: 481 +ret: 0 st: 1 flags:0 ts:-0.058333 ret: 0 st: 1 flags:1 dts: 0.008000 pts: 0.008000 pos: 1783 size: 528 -ret: 0 st: 1 flags:1 dts: 0.029000 pts: 0.029000 pos: 1783 size: 510 -ret: 0 st: 1 flags:1 dts: 0.050000 pts: 0.050000 pos: 1783 size: 500 -ret: 0 st: 1 flags:1 ts: 2.836000 +ret: 0 st: 1 flags:1 dts: 0.029333 pts: 0.029333 pos: 1783 size: 510 +ret: 0 st: 1 flags:1 dts: 0.050667 pts: 0.050667 pos: 1783 size: 500 +ret: 0 st: 1 flags:1 ts: 2.835833 ret: 0 st: 1 flags:1 dts: 2.696000 pts: 2.696000 pos:1369672 size: 507 -ret: 0 st: 1 flags:1 dts: 2.717000 pts: 2.717000 pos:1369672 size: 480 -ret: 0 st: 1 flags:1 dts: 2.738000 pts: 2.738000 pos:1369672 size: 492 +ret: 0 st: 1 flags:1 dts: 2.717333 pts: 2.717333 pos:1369672 size: 480 +ret: 0 st: 1 flags:1 dts: 2.738667 pts: 2.738667 pos:1369672 size: 492 ret: 0 st:-1 flags:0 ts: 1.730004 ret: 0 st: 0 flags:1 dts: NOPTS pts: 2.000000 pos: 989431 size: 60241 -ret: 0 st: 1 flags:1 dts: 2.014000 pts: 2.014000 pos:1049679 size: 481 -ret: 0 st: 1 flags:1 dts: 2.035000 pts: 2.035000 pos:1049679 size: 473 +ret: 0 st: 1 flags:1 dts: 2.013333 pts: 2.013333 pos:1049679 size: 481 +ret: 0 st: 1 flags:1 dts: 2.034667 pts: 2.034667 pos:1049679 size: 473 ret: 0 st:-1 flags:1 ts: 0.624171 ret: 0 st: 1 flags:1 dts: 0.008000 pts: 0.008000 pos: 1783 size: 528 -ret: 0 st: 1 flags:1 dts: 0.029000 pts: 0.029000 pos: 1783 size: 510 -ret: 0 st: 1 flags:1 dts: 0.050000 pts: 0.050000 pos: 1783 size: 500 +ret: 0 st: 1 flags:1 dts: 0.029333 pts: 0.029333 pos: 1783 size: 510 +ret: 0 st: 1 flags:1 dts: 0.050667 pts: 0.050667 pos: 1783 size: 500 ret: 0 st: 0 flags:0 ts:-0.482000 ret: 0 st: 1 flags:1 dts: 0.008000 pts: 0.008000 pos: 1783 size: 528 -ret: 0 st: 1 flags:1 dts: 0.029000 pts: 0.029000 pos: 1783 size: 510 -ret: 0 st: 1 flags:1 dts: 0.050000 pts: 0.050000 pos: 1783 size: 500 +ret: 0 st: 1 flags:1 dts: 0.029333 pts: 0.029333 pos: 1783 size: 510 +ret: 0 st: 1 flags:1 dts: 0.050667 pts: 0.050667 pos: 1783 size: 500 ret: 0 st: 0 flags:1 ts: 2.413000 ret: 0 st: 0 flags:1 dts: NOPTS pts: 2.000000 pos: 989431 size: 60241 -ret: 0 st: 1 flags:1 dts: 2.014000 pts: 2.014000 pos:1049679 size: 481 -ret: 0 st: 1 flags:1 dts: 2.035000 pts: 2.035000 pos:1049679 size: 473 -ret: 0 st: 1 flags:0 ts: 1.307000 -ret: 0 st: 1 flags:1 dts: 1.993000 pts: 1.993000 pos: 988964 size: 459 +ret: 0 st: 1 flags:1 dts: 2.013333 pts: 2.013333 pos:1049679 size: 481 +ret: 0 st: 1 flags:1 dts: 2.034667 pts: 2.034667 pos:1049679 size: 473 +ret: 0 st: 1 flags:0 ts: 1.306667 +ret: 0 st: 1 flags:1 dts: 1.992000 pts: 1.992000 pos: 988964 size: 459 ret: 0 st: 0 flags:1 dts: NOPTS pts: 2.000000 pos: 989431 size: 60241 -ret: 0 st: 1 flags:1 dts: 2.014000 pts: 2.014000 pos:1049679 size: 481 -ret: 0 st: 1 flags:1 ts: 0.201000 -ret: 0 st: 1 flags:1 dts: 0.179000 pts: 0.179000 pos: 85877 size: 501 +ret: 0 st: 1 flags:1 dts: 2.013333 pts: 2.013333 pos:1049679 size: 481 +ret: 0 st: 1 flags:1 ts: 0.200833 +ret: 0 st: 1 flags:1 dts: 0.178667 pts: 0.178667 pos: 85877 size: 501 ret: 0 st: 1 flags:1 dts: 0.200000 pts: 0.200000 pos: 85877 size: 521 -ret: 0 st: 1 flags:1 dts: 0.221000 pts: 0.221000 pos: 85877 size: 646 +ret: 0 st: 1 flags:1 dts: 0.221333 pts: 0.221333 pos: 85877 size: 646 ret: 0 st:-1 flags:0 ts:-0.904994 ret: 0 st: 1 flags:1 dts: 0.008000 pts: 0.008000 pos: 1783 size: 528 -ret: 0 st: 1 flags:1 dts: 0.029000 pts: 0.029000 pos: 1783 size: 510 -ret: 0 st: 1 flags:1 dts: 0.050000 pts: 0.050000 pos: 1783 size: 500 +ret: 0 st: 1 flags:1 dts: 0.029333 pts: 0.029333 pos: 1783 size: 510 +ret: 0 st: 1 flags:1 dts: 0.050667 pts: 0.050667 pos: 1783 size: 500 ret: 0 st:-1 flags:1 ts: 1.989173 -ret: 0 st: 1 flags:1 dts: 1.993000 pts: 1.993000 pos: 988964 size: 459 +ret: 0 st: 1 flags:1 dts: 1.992000 pts: 1.992000 pos: 988964 size: 459 ret: 0 st: 0 flags:1 dts: NOPTS pts: 2.000000 pos: 989431 size: 60241 -ret: 0 st: 1 flags:1 dts: 2.014000 pts: 2.014000 pos:1049679 size: 481 +ret: 0 st: 1 flags:1 dts: 2.013333 pts: 2.013333 pos:1049679 size: 481 ret: 0 st: 0 flags:0 ts: 0.883000 -ret: 0 st: 1 flags:1 dts: 1.993000 pts: 1.993000 pos: 988964 size: 459 +ret: 0 st: 1 flags:1 dts: 1.992000 pts: 1.992000 pos: 988964 size: 459 ret: 0 st: 0 flags:1 dts: NOPTS pts: 2.000000 pos: 989431 size: 60241 -ret: 0 st: 1 flags:1 dts: 2.014000 pts: 2.014000 pos:1049679 size: 481 +ret: 0 st: 1 flags:1 dts: 2.013333 pts: 2.013333 pos:1049679 size: 481 ret: 0 st: 0 flags:1 ts:-0.222000 ret: 0 st: 1 flags:1 dts: 0.008000 pts: 0.008000 pos: 1783 size: 528 -ret: 0 st: 1 flags:1 dts: 0.029000 pts: 0.029000 pos: 1783 size: 510 -ret: 0 st: 1 flags:1 dts: 0.050000 pts: 0.050000 pos: 1783 size: 500 -ret: 0 st: 1 flags:0 ts: 2.672000 +ret: 0 st: 1 flags:1 dts: 0.029333 pts: 0.029333 pos: 1783 size: 510 +ret: 0 st: 1 flags:1 dts: 0.050667 pts: 0.050667 pos: 1783 size: 500 +ret: 0 st: 1 flags:0 ts: 2.671667 ret: 0 st: 1 flags:1 dts: 2.696000 pts: 2.696000 pos:1369672 size: 507 -ret: 0 st: 1 flags:1 dts: 2.717000 pts: 2.717000 pos:1369672 size: 480 -ret: 0 st: 1 flags:1 dts: 2.738000 pts: 2.738000 pos:1369672 size: 492 -ret: 0 st: 1 flags:1 ts: 1.566000 -ret: 0 st: 1 flags:1 dts: 0.990000 pts: 0.990000 pos: 450438 size: 487 -ret: 0 st: 1 flags:1 dts: 1.011000 pts: 1.011000 pos: 450438 size: 485 -ret: 0 st: 1 flags:1 dts: 1.993000 pts: 1.993000 pos: 988964 size: 459 +ret: 0 st: 1 flags:1 dts: 2.717333 pts: 2.717333 pos:1369672 size: 480 +ret: 0 st: 1 flags:1 dts: 2.738667 pts: 2.738667 pos:1369672 size: 492 +ret: 0 st: 1 flags:1 ts: 1.565833 +ret: 0 st: 1 flags:1 dts: 0.989333 pts: 0.989333 pos: 450438 size: 487 +ret: 0 st: 1 flags:1 dts: 1.010667 pts: 1.010667 pos: 450438 size: 485 +ret: 0 st: 1 flags:1 dts: 1.992000 pts: 1.992000 pos: 988964 size: 459 ret: 0 st:-1 flags:0 ts: 0.460008 -ret: 0 st: 1 flags:1 dts: 1.993000 pts: 1.993000 pos: 988964 size: 459 +ret: 0 st: 1 flags:1 dts: 1.992000 pts: 1.992000 pos: 988964 size: 459 ret: 0 st: 0 flags:1 dts: NOPTS pts: 2.000000 pos: 989431 size: 60241 -ret: 0 st: 1 flags:1 dts: 2.014000 pts: 2.014000 pos:1049679 size: 481 +ret: 0 st: 1 flags:1 dts: 2.013333 pts: 2.013333 pos:1049679 size: 481 ret: 0 st:-1 flags:1 ts:-0.645825 ret: 0 st: 1 flags:1 dts: 0.008000 pts: 0.008000 pos: 1783 size: 528 -ret: 0 st: 1 flags:1 dts: 0.029000 pts: 0.029000 pos: 1783 size: 510 -ret: 0 st: 1 flags:1 dts: 0.050000 pts: 0.050000 pos: 1783 size: 500 +ret: 0 st: 1 flags:1 dts: 0.029333 pts: 0.029333 pos: 1783 size: 510 +ret: 0 st: 1 flags:1 dts: 0.050667 pts: 0.050667 pos: 1783 size: 500 -- 2.54.0 >From 7d8efc2667190bd8fe699784afa794989d2dc6ce Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Thu, 12 Nov 2026 12:46:11 -0600 Subject: [PATCH 6/6] avformat/matroskaenc: pick a clock finer than a sample with AVFMT_FLAG_EXACT_TS With the flag, the muxer sets TimestampScale to the coarsest power of ten of a nanosecond finer than half a sample of every audio stream, 10 us at 44.1 or 48 kHz, and writes the cluster time limit, the default duration and the segment duration on that clock. The cluster time limit is kept within the 16-bit offset of a Block, 327 ms at 10 us, so the files stay ordinary Matroska with shorter clusters. The matroska-opus-exact-ts-remux packets read back at -650, 1350, 3350... on a 1/100000 time base without the flag. --- doc/formats.texi | 2 + libavformat/matroskaenc.c | 59 +++++++++-- tests/ref/fate/matroska-opus-exact-ts-remux | 110 ++++++++++---------- 3 files changed, 105 insertions(+), 66 deletions(-) diff --git a/doc/formats.texi b/doc/formats.texi index 0c234706167..6da51a7899f 100644 --- a/doc/formats.texi +++ b/doc/formats.texi @@ -75,6 +75,8 @@ platforms. Its primary use is for regression testing. @item exact_ts Write the timestamps on a clock finer than a sample, where the format lets the muxer choose its clock. +The Matroska muxer picks 10 microseconds at 48 kHz instead of a millisecond, at +the cost of shorter clusters. @item flush_packets Write out packets immediately. @item shortest diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 25526d079de..8b69664a5cd 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -253,6 +253,7 @@ typedef struct MatroskaMuxContext { int reserve_cues_space; int cluster_size_limit; int64_t cluster_time_limit; + int64_t timestamp_scale; ///< the clock of the file, in nanoseconds int write_crc; int is_live; @@ -1902,13 +1903,15 @@ static int mkv_write_track_video(AVFormatContext *s, MatroskaMuxContext *mkv, } static void mkv_write_default_duration(mkv_track *track, AVIOContext *pb, - AVRational duration) + AVRational duration, int64_t timestamp_scale) { + int64_t den = duration.den * timestamp_scale; + put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, 1000000000LL * duration.num / duration.den); - track->default_duration_low = 1000LL * duration.num / duration.den; + track->default_duration_low = 1000000000LL * duration.num / den; track->default_duration_high = track->default_duration_low + - !!(1000LL * duration.num % duration.den); + !!(1000000000LL * duration.num % den); } static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv, @@ -2033,7 +2036,8 @@ static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv, frame_rate = st->r_frame_rate; if (frame_rate.num > 0) - mkv_write_default_duration(track, pb, av_inv_q(frame_rate)); + mkv_write_default_duration(track, pb, av_inv_q(frame_rate), + mkv->timestamp_scale); if (CONFIG_MATROSKA_MUXER && !native_id && ff_codec_get_tag(ff_codec_movvideo_tags, par->codec_id) && @@ -2092,7 +2096,8 @@ static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv, audio_frame_samples = par->frame_size; if (audio_frame_samples) mkv_write_default_duration(track, pb, (AVRational){ audio_frame_samples, - par->sample_rate }); + par->sample_rate }, + mkv->timestamp_scale); if (!native_id) // no mkv-specific ID, use ACM mode put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM"); @@ -2582,7 +2587,7 @@ static int mkv_write_info(AVFormatContext *s) return ret; pb = mkv->info.bc; - put_ebml_uint(pb, MATROSKA_ID_TIMECODESCALE, 1000000); + put_ebml_uint(pb, MATROSKA_ID_TIMECODESCALE, mkv->timestamp_scale); if ((tag = av_dict_get(s->metadata, "title", NULL, 0))) put_ebml_string(pb, MATROSKA_ID_TITLE, tag->value); if (!(s->flags & AVFMT_FLAG_BITEXACT)) { @@ -2615,11 +2620,11 @@ static int mkv_write_info(AVFormatContext *s) int64_t metadata_duration = get_metadata_duration(s); if (s->duration > 0) { - int64_t scaledDuration = av_rescale(s->duration, 1000, AV_TIME_BASE); + int64_t scaledDuration = av_rescale(s->duration, 1000000000LL / mkv->timestamp_scale, AV_TIME_BASE); put_ebml_float(pb, MATROSKA_ID_DURATION, scaledDuration); av_log(s, AV_LOG_DEBUG, "Write early duration from recording time = %" PRIu64 "\n", scaledDuration); } else if (metadata_duration > 0) { - int64_t scaledDuration = av_rescale(metadata_duration, 1000, AV_TIME_BASE); + int64_t scaledDuration = av_rescale(metadata_duration, 1000000000LL / mkv->timestamp_scale, AV_TIME_BASE); put_ebml_float(pb, MATROSKA_ID_DURATION, scaledDuration); av_log(s, AV_LOG_DEBUG, "Write early duration from metadata = %" PRIu64 "\n", scaledDuration); } else if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) { @@ -2724,6 +2729,12 @@ static int mkv_write_header(AVFormatContext *s) if (mkv->cluster_size_limit < 0) mkv->cluster_size_limit = 32 * 1024; } + // From milliseconds to the clock of the file, within the reach of the + // 16-bit offset of a Block. + mkv->cluster_time_limit = FFMIN(mkv->cluster_time_limit, + INT16_MAX * mkv->timestamp_scale / 1000000); + mkv->cluster_time_limit = av_rescale(mkv->cluster_time_limit, 1000000, + mkv->timestamp_scale); return 0; } @@ -3125,7 +3136,9 @@ static int mkv_write_packet_internal(AVFormatContext *s, const AVPacket *pkt) "timestamp %"PRId64" ms, current cluster timestamp %"PRId64 " ms, delta %"PRId64" ms outside the signed 16-bit range\n", cluster_time < 0 ? "backward" : "forward", pkt->stream_index, - ts, mkv->cluster_pts, cluster_time); + av_rescale(ts, mkv->timestamp_scale, 1000000), + av_rescale(mkv->cluster_pts, mkv->timestamp_scale, 1000000), + av_rescale(cluster_time, mkv->timestamp_scale, 1000000)); } } @@ -3486,6 +3499,30 @@ static uint64_t mkv_get_uid(const mkv_track *tracks, int i, AVLFG *c) } } + +/** + * The clock of the file. Milliseconds are the de-facto standard, and are + * kept unless the timestamps have to identify the exact sample, in which + * case the clock is the coarsest power of ten of a nanosecond that is + * finer than half a sample of every audio stream. A Block holds a signed + * 16-bit offset from its Cluster, so a finer clock means shorter clusters. + */ +static int64_t mkv_timestamp_scale(AVFormatContext *s) +{ + int64_t scale = 1000000, max_rate = 0; + + if (!(s->flags & AVFMT_FLAG_EXACT_TS)) + return scale; + for (unsigned i = 0; i < s->nb_streams; i++) { + const AVCodecParameters *par = s->streams[i]->codecpar; + if (par->codec_type == AVMEDIA_TYPE_AUDIO) + max_rate = FFMAX(max_rate, par->sample_rate); + } + while (scale > 1 && scale * max_rate > 500000000LL) + scale /= 10; + return scale; +} + static int mkv_init(struct AVFormatContext *s) { FFFormatContext *const si = ffformatcontext(s); @@ -3495,6 +3532,7 @@ static int mkv_init(struct AVFormatContext *s) int i; mkv->ctx = s; + mkv->timestamp_scale = mkv_timestamp_scale(s); for (i = 0; i < s->nb_streams; i++) { if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_ATRAC3 || @@ -3576,8 +3614,7 @@ static int mkv_init(struct AVFormatContext *s) track->uid = mkv_get_uid(mkv->tracks, i, &c); } - // ms precision is the de-facto standard timescale for mkv files - avpriv_set_pts_info(st, 64, 1, 1000); + avpriv_set_pts_info(st, 64, mkv->timestamp_scale, 1000000000); if (st->codecpar->codec_type == AVMEDIA_TYPE_ATTACHMENT) { if (IS_WEBM(mkv)) { diff --git a/tests/ref/fate/matroska-opus-exact-ts-remux b/tests/ref/fate/matroska-opus-exact-ts-remux index 95181327e59..4afccacdd24 100644 --- a/tests/ref/fate/matroska-opus-exact-ts-remux +++ b/tests/ref/fate/matroska-opus-exact-ts-remux @@ -1,60 +1,60 @@ -b9881205f8945fefc16a6f23474071a6 *tests/data/fate/matroska-opus-exact-ts-remux.matroska -9358 tests/data/fate/matroska-opus-exact-ts-remux.matroska +f328d1b7a0bebdecbcfab9b605c8c760 *tests/data/fate/matroska-opus-exact-ts-remux.matroska +9461 tests/data/fate/matroska-opus-exact-ts-remux.matroska #extradata 0: 19, 0x3a04048f -#tb 0: 1/1000 +#tb 0: 1/100000 #media_type 0: audio #codec_id 0: opus #sample_rate 0: 48000 #channel_layout_name 0: mono -0, -7, -7, 20, 320, 0x58b9a88d, S=1, Skip Samples, 10, 0x02390039 -0, 14, 14, 20, 159, 0x6c9c4b4c -0, 34, 34, 20, 148, 0x0caf4b5d -0, 54, 54, 20, 139, 0xc5624226 -0, 74, 74, 20, 146, 0x633c4937 -0, 94, 94, 20, 153, 0x3d0b4f93 -0, 114, 114, 20, 158, 0xe5c55641 -0, 134, 134, 20, 156, 0xf2fd50ef -0, 154, 154, 20, 158, 0x93b15410 -0, 174, 174, 20, 157, 0xb6f74f5f -0, 194, 194, 20, 159, 0x9aff4957 -0, 214, 214, 20, 153, 0xfc5f4aba -0, 234, 234, 20, 158, 0x01e44f70 -0, 254, 254, 20, 153, 0x227149cf -0, 274, 274, 20, 155, 0x312f4cf6 -0, 294, 294, 20, 155, 0xafc54bae -0, 314, 314, 20, 151, 0x7b4252b3 -0, 334, 334, 20, 155, 0x29074a75 -0, 354, 354, 20, 149, 0x82c44bcd -0, 374, 374, 20, 150, 0x55c24eb5 -0, 394, 394, 20, 156, 0xf71d4f33 -0, 414, 414, 20, 153, 0x9b6c4ae5 -0, 434, 434, 20, 156, 0x75954e51 -0, 454, 454, 20, 155, 0x28ff4ff3 -0, 474, 474, 20, 153, 0xc4424969 -0, 494, 494, 20, 154, 0xfbf94cc8 -0, 514, 514, 20, 155, 0x52c549af -0, 534, 534, 20, 150, 0x6f1e4b7a -0, 554, 554, 20, 158, 0xabb45566 -0, 574, 574, 20, 157, 0xe61d4a99 -0, 594, 594, 20, 159, 0xf45d4fac -0, 614, 614, 20, 159, 0xcd0553a5 -0, 634, 634, 20, 156, 0xdb244e63 -0, 654, 654, 20, 154, 0x78654c52 -0, 674, 674, 20, 154, 0x9f804cc8 -0, 694, 694, 20, 150, 0x1fdf4c80 -0, 714, 714, 20, 155, 0x1adc4f89 -0, 734, 734, 20, 155, 0x4b53511c -0, 754, 754, 20, 151, 0x8ff2546d -0, 774, 774, 20, 158, 0xb7e34f1b -0, 794, 794, 20, 154, 0x4d98474b -0, 814, 814, 20, 154, 0x14924ea8 -0, 834, 834, 20, 153, 0x8d4752bf -0, 854, 854, 20, 149, 0x74785066 -0, 874, 874, 20, 151, 0x36c94a4c -0, 894, 894, 20, 155, 0x82904f3b -0, 914, 914, 20, 154, 0xd76b4a45 -0, 934, 934, 20, 159, 0x9fec548d -0, 954, 954, 20, 154, 0x9a084dcd -0, 974, 974, 20, 155, 0x90a54ac8 -0, 994, 994, 20, 324, 0x8e34a2f5 -0, 1014, 1014, 20, 268, 0x10f37203, S=1, Skip Samples, 10, 0x049600c4 +0, -650, -650, 2000, 320, 0x58b9a88d, S=1, Skip Samples, 10, 0x02390039 +0, 1350, 1350, 2000, 159, 0x6c9c4b4c +0, 3350, 3350, 2000, 148, 0x0caf4b5d +0, 5350, 5350, 2000, 139, 0xc5624226 +0, 7350, 7350, 2000, 146, 0x633c4937 +0, 9350, 9350, 2000, 153, 0x3d0b4f93 +0, 11350, 11350, 2000, 158, 0xe5c55641 +0, 13350, 13350, 2000, 156, 0xf2fd50ef +0, 15350, 15350, 2000, 158, 0x93b15410 +0, 17350, 17350, 2000, 157, 0xb6f74f5f +0, 19350, 19350, 2000, 159, 0x9aff4957 +0, 21350, 21350, 2000, 153, 0xfc5f4aba +0, 23350, 23350, 2000, 158, 0x01e44f70 +0, 25350, 25350, 2000, 153, 0x227149cf +0, 27350, 27350, 2000, 155, 0x312f4cf6 +0, 29350, 29350, 2000, 155, 0xafc54bae +0, 31350, 31350, 2000, 151, 0x7b4252b3 +0, 33350, 33350, 2000, 155, 0x29074a75 +0, 35350, 35350, 2000, 149, 0x82c44bcd +0, 37350, 37350, 2000, 150, 0x55c24eb5 +0, 39350, 39350, 2000, 156, 0xf71d4f33 +0, 41350, 41350, 2000, 153, 0x9b6c4ae5 +0, 43350, 43350, 2000, 156, 0x75954e51 +0, 45350, 45350, 2000, 155, 0x28ff4ff3 +0, 47350, 47350, 2000, 153, 0xc4424969 +0, 49350, 49350, 2000, 154, 0xfbf94cc8 +0, 51350, 51350, 2000, 155, 0x52c549af +0, 53350, 53350, 2000, 150, 0x6f1e4b7a +0, 55350, 55350, 2000, 158, 0xabb45566 +0, 57350, 57350, 2000, 157, 0xe61d4a99 +0, 59350, 59350, 2000, 159, 0xf45d4fac +0, 61350, 61350, 2000, 159, 0xcd0553a5 +0, 63350, 63350, 2000, 156, 0xdb244e63 +0, 65350, 65350, 2000, 154, 0x78654c52 +0, 67350, 67350, 2000, 154, 0x9f804cc8 +0, 69350, 69350, 2000, 150, 0x1fdf4c80 +0, 71350, 71350, 2000, 155, 0x1adc4f89 +0, 73350, 73350, 2000, 155, 0x4b53511c +0, 75350, 75350, 2000, 151, 0x8ff2546d +0, 77350, 77350, 2000, 158, 0xb7e34f1b +0, 79350, 79350, 2000, 154, 0x4d98474b +0, 81350, 81350, 2000, 154, 0x14924ea8 +0, 83350, 83350, 2000, 153, 0x8d4752bf +0, 85350, 85350, 2000, 149, 0x74785066 +0, 87350, 87350, 2000, 151, 0x36c94a4c +0, 89350, 89350, 2000, 155, 0x82904f3b +0, 91350, 91350, 2000, 154, 0xd76b4a45 +0, 93350, 93350, 2000, 159, 0x9fec548d +0, 95350, 95350, 2000, 154, 0x9a084dcd +0, 97350, 97350, 2000, 155, 0x90a54ac8 +0, 99350, 99350, 2000, 324, 0x8e34a2f5 +0, 101350, 101350, 2000, 268, 0x10f37203, S=1, Skip Samples, 10, 0x049600c4 -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
