Commit 87d6b6a01d for ffmpeg
commit 87d6b6a01dfeb7ec04be701f5bb8c4f15166a083
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date: Wed Nov 18 08:49:18 2026 -0600
avformat/matroskadec: add the best_fit_ts option
Fit timestamps to the units of each stream where the file's clock is
coarser. A millisecond clock places a 48 kHz audio packet up to 24
samples off. Off by default; no effect yet.
diff --git a/doc/demuxers.texi b/doc/demuxers.texi
index 5cfe59f602..e21b887849 100644
--- a/doc/demuxers.texi
+++ b/doc/demuxers.texi
@@ -918,6 +918,34 @@ ffmpeg -i video_and_audio.mxf -eia608_extract 0 -i CC.mcc -c copy -map 0 -map 1
This retains all VANC data and inserts it into the output MXF file as a @code{SMPTE_436M_ANC} data stream.
@end itemize
+@section matroska
+
+Matroska / WebM demuxer.
+
+@subsection Options
+
+@table @option
+@item best_fit_ts @var{bool}
+Fit the timestamps to the units of each stream, where the clock of the file
+is coarser than those units. The most common Matroska clock is a millisecond,
+which places a 48 kHz audio packet up to 24 samples away from its first
+sample.
+
+Audio tracks are moved to a sample time base and their timestamps are
+rebuilt from the packet lengths, which come from the codec for Opus and AAC
+and from the default duration of the track for the other codecs. A stored
+timestamp is followed wherever it departs from that count by more than its
+rounding. Streams whose units the clock already resolves, such as video at
+the usual frame rates, keep the clock of the file.
+
+Vorbis tracks, tracks stored in a compatibility or interleaved mode, tracks
+whose packets a parser re-times and tracks without a known sample rate or
+packet length also keep the clock of the file. The reason is logged at
+verbose level.
+
+Default is disabled.
+@end table
+
@section mov/mp4/3gp
Demuxer for Quicktime File Format & ISO/IEC Base Media File Format (ISO/IEC 14496-12 or MPEG-4 Part 12, ISO/IEC 15444-12 or JPEG 2000 Part 12).
diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c
index b10bf3ac4e..a85eec7f81 100644
--- a/libavformat/matroskadec.c
+++ b/libavformat/matroskadec.c
@@ -433,6 +433,8 @@ typedef struct MatroskaDemuxContext {
MatroskaCluster current_cluster;
int is_webm;
+ /// fit timestamps to the units of each stream, where the clock is coarser
+ int best_fit_ts;
/* WebM DASH Manifest live flag */
int is_live;
@@ -4598,6 +4600,8 @@ static int matroska_read_close(AVFormatContext *s)
return 0;
}
+#define OFFSET(x) offsetof(MatroskaDemuxContext, x)
+
#if CONFIG_WEBM_DASH_MANIFEST_DEMUXER
typedef struct {
int64_t start_time_ns;
@@ -5029,7 +5033,6 @@ static int webm_dash_manifest_read_packet(AVFormatContext *s, AVPacket *pkt)
return AVERROR_EOF;
}
-#define OFFSET(x) offsetof(MatroskaDemuxContext, x)
static const AVOption options[] = {
{ "live", "flag indicating that the input is a live file that only has the headers.", OFFSET(is_live), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
{ "bandwidth", "bandwidth of this stream to be specified in the DASH manifest.", OFFSET(bandwidth), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
@@ -5055,11 +5058,24 @@ const FFInputFormat ff_webm_dash_manifest_demuxer = {
};
#endif
+static const AVOption matroska_options[] = {
+ { "best_fit_ts", "fit timestamps to the units of each stream where the clock of the file is coarser", OFFSET(best_fit_ts), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
+ { NULL },
+};
+
+static const AVClass matroska_class = {
+ .class_name = "Matroska / WebM demuxer",
+ .item_name = av_default_item_name,
+ .option = matroska_options,
+ .version = LIBAVUTIL_VERSION_INT,
+};
+
const FFInputFormat ff_matroska_demuxer = {
.p.name = "matroska,webm",
.p.long_name = NULL_IF_CONFIG_SMALL("Matroska / WebM"),
.p.extensions = "mkv,mk3d,mka,mks,webm",
.p.mime_type = "audio/webm,audio/x-matroska,video/webm,video/x-matroska",
+ .p.priv_class = &matroska_class,
.priv_data_size = sizeof(MatroskaDemuxContext),
.flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
.read_probe = matroska_probe,
diff --git a/tests/fate/matroska.mak b/tests/fate/matroska.mak
index 6bd226abf0..2e8f4f2374 100644
--- a/tests/fate/matroska.mak
+++ b/tests/fate/matroska.mak
@@ -18,28 +18,28 @@ FATE_MATROSKA-$(call FRAMECRC, MATROSKA,, VORBIS_PARSER) += fate-matroska-xiph-
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-best-fit-ts
-fate-matroska-opus-best-fit-ts: CMD = framecrc -i $(TARGET_SAMPLES)/mkv/codec_delay_opus.mkv -c copy
+fate-matroska-opus-best-fit-ts: CMD = framecrc -best_fit_ts 1 -i $(TARGET_SAMPLES)/mkv/codec_delay_opus.mkv -c copy
FATE_MATROSKA_FFMPEG_FFPROBE-$(call REMUX, MATROSKA, OPUS_PARSER OPUS_DECODER) += fate-matroska-opus-best-fit-ts-remux
-fate-matroska-opus-best-fit-ts-remux: CMD = transcode matroska $(TARGET_SAMPLES)/mkv/codec_delay_opus.mkv matroska "-c copy" "-c copy"
+fate-matroska-opus-best-fit-ts-remux: CMD = transcode matroska $(TARGET_SAMPLES)/mkv/codec_delay_opus.mkv matroska "-c copy" "-c copy" "" "" "" "-best_fit_ts 1"
FATE_MATROSKA_FFMPEG_FFPROBE-$(call REMUX, MATROSKA, OPUS_PARSER OPUS_DECODER SETTS_BSF) += fate-matroska-opus-best-fit-ts-gap
-fate-matroska-opus-best-fit-ts-gap: CMD = transcode matroska $(TARGET_SAMPLES)/mkv/codec_delay_opus.mkv matroska "-c copy -bsf:a setts=ts=TS+sgn(N)*100/48000/TB" "-c copy"
+fate-matroska-opus-best-fit-ts-gap: CMD = transcode matroska $(TARGET_SAMPLES)/mkv/codec_delay_opus.mkv matroska "-c copy -bsf:a setts=ts=TS+sgn(N)*100/48000/TB" "-c copy" "" "" "-best_fit_ts 1" "-best_fit_ts 1"
FATE_MATROSKA_FFMPEG_FFPROBE-$(call REMUX, MATROSKA, OPUS_PARSER OPUS_DECODER SETTS_BSF) += fate-matroska-opus-best-fit-ts-sample-gap
-fate-matroska-opus-best-fit-ts-sample-gap: CMD = transcode matroska $(TARGET_SAMPLES)/mkv/codec_delay_opus.mkv matroska "-c copy -bsf:a setts=ts=TS+sgn(N)*1/48000/TB" "-c copy"
+fate-matroska-opus-best-fit-ts-sample-gap: CMD = transcode matroska $(TARGET_SAMPLES)/mkv/codec_delay_opus.mkv matroska "-c copy -bsf:a setts=ts=TS+sgn(N)*1/48000/TB" "-c copy" "" "" "-best_fit_ts 1" "-best_fit_ts 1"
FATE_MATROSKA_FFMPEG_FFPROBE-$(call REMUX, MATROSKA, MOV_DEMUXER AAC_DECODER) += fate-matroska-aac-eld-best-fit-ts
-fate-matroska-aac-eld-best-fit-ts: CMD = transcode mov $(TARGET_SAMPLES)/aac/er_eld2000np_48_ep0.mp4 matroska "-c copy -frames:a 16" "-c copy"
+fate-matroska-aac-eld-best-fit-ts: CMD = transcode mov $(TARGET_SAMPLES)/aac/er_eld2000np_48_ep0.mp4 matroska "-c copy -frames:a 16" "-c copy" "" "" "-best_fit_ts 1"
FATE_MATROSKA-$(call FRAMECRC, MATROSKA,, VORBIS_PARSER) += fate-matroska-vorbis-best-fit-ts
-fate-matroska-vorbis-best-fit-ts: CMD = framecrc -i $(TARGET_SAMPLES)/vp8/RRSF49-short.webm -map 0:a -c copy -frames:a 50
+fate-matroska-vorbis-best-fit-ts: CMD = framecrc -best_fit_ts 1 -i $(TARGET_SAMPLES)/vp8/RRSF49-short.webm -map 0:a -c copy -frames:a 50
FATE_MATROSKA_FFMPEG_FFPROBE-$(call TRANSCODE, PCM_S16LE, MATROSKA, H264_PARSER H264_DECODER LAVFI_INDEV SINE_FILTER) += fate-matroska-h264-pcm96k-remux
fate-matroska-h264-pcm96k-remux: CMD = transcode matroska $(TARGET_SAMPLES)/mkv/test7_cut.mkv matroska "-map 0:v -map 1:a -c:v copy -c:a pcm_s16le -t 2" "-map 0:v -c copy" "" "-f lavfi -i sine=r=96000:d=2"
FATE_MATROSKA_FFPROBE-$(call ALLYES, MATROSKA_DEMUXER) += fate-matroska-opus-best-fit-ts-seek
-fate-matroska-opus-best-fit-ts-seek: CMD = run ffprobe$(PROGSSUF)$(EXESUF) -read_intervals 0%+\#1,1%+\#1 -show_entries packet=pts,pos -print_format compact $(TARGET_SAMPLES)/audiomatch/tones_opus_48000_stereo.mka
+fate-matroska-opus-best-fit-ts-seek: CMD = run ffprobe$(PROGSSUF)$(EXESUF) -best_fit_ts 1 -read_intervals 0%+\#1,1%+\#1 -show_entries packet=pts,pos -print_format compact $(TARGET_SAMPLES)/audiomatch/tones_opus_48000_stereo.mka
# This tests that the Matroska demuxer correctly demuxes WavPack
# without CodecPrivate; it also tests zlib compressed WavPack.
diff --git a/tests/fate/seek.mak b/tests/fate/seek.mak
index 60216c23be..0181dd2e5f 100644
--- a/tests/fate/seek.mak
+++ b/tests/fate/seek.mak
@@ -200,7 +200,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-best-fit-ts: CMD = run libavformat/tests/seek$(EXESUF) $(TARGET_SAMPLES)/mkv/test7_cut.mkv -duration 4 -frames 3
+fate-seek-mkv-best-fit-ts: CMD = run libavformat/tests/seek$(EXESUF) $(TARGET_SAMPLES)/mkv/test7_cut.mkv -duration 4 -frames 3 -best_fit_ts 1
fate-seek-sacd: CMD = run libavformat/tests/seek$(EXESUF) $(TARGET_SAMPLES)/sacd/2ch-dsd_5ch-dst.iso -duration 2
# A master playlist with separate audio and video media playlists with different DTS.