PR #26417 opened by Romain Beauxis (toots)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26417
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26417.patch

After a seek, a fresh Opus decoder converges to a continuous decode but never 
matches it exactly. With the 80 ms from RFC 7845, the output can still differ 
by up to -23 dBFS. Containers that declare no pre-roll at all (CAF, MPEG-TS, 
Matroska without SeekPreRoll) get none.

I measured 766 seek points across the FATE Opus samples (CELT, SILK and 
hybrid). This is the largest error left after each pre-roll, in 16-bit LSB:

| pre-roll | p99 | max |
|---|---|---|
| 80 ms | 1525 | 2247 |
| 200 ms | 84 | 445 |
| 350 ms | 1.3 | 10 |
| 400 ms | 0.10 | 0.89 |

400 ms is the first value where every point is within 1 LSB. The error never 
reaches zero: some points still differ after 2.8 s.

Changes:

- The `opus` and `libopus` decoders declare a 400 ms `seek_preroll`, or the 
container's value when it is longer. This only applies to seeks with 
`AVSEEK_FLAG_PREROLL`, so stream copy is unchanged.
- A new `seek_preroll` decoder option sets the value. The container's pre-roll 
is the minimum.

Tests:

- `fate-preroll-opus-{celt,silk,hybrid}-match` print the frames that are within 
1 LSB of a decode from the start, at seek points where the decoder converges 
slowly. Before this change they show 1/7, 9/14 and 0/30 frames; after it, all 
of them.
- `fate-preroll-opus-caf-declared-option` covers the option.


>From a745f0c45fb7a63353f3a82c66b8f1efa0ef24e7 Mon Sep 17 00:00:00 2001
From: Romain Beauxis <[email protected]>
Date: Fri, 27 Nov 2026 14:04:40 -0600
Subject: [PATCH 1/3] tests/fate/preroll: compare Opus decoding after a seek
 with a full decode

An Opus decoder started inside the stream never matches a continuous
decode exactly, it converges to it. The new tests print the frames of the
output after a seek that are within 1 LSB of a decode from the start, on
points of the CELT, SILK and hybrid test vectors where the decoder
converges slowly. These samples declare no pre-roll, so the seek decodes
none and most frames differ.
---
 tests/fate/preroll.mak                   | 14 ++++++++++++++
 tests/ref/fate/preroll-opus-celt-match   |  6 ++++++
 tests/ref/fate/preroll-opus-hybrid-match |  5 +++++
 tests/ref/fate/preroll-opus-silk-match   | 14 ++++++++++++++
 4 files changed, 39 insertions(+)
 create mode 100644 tests/ref/fate/preroll-opus-celt-match
 create mode 100644 tests/ref/fate/preroll-opus-hybrid-match
 create mode 100644 tests/ref/fate/preroll-opus-silk-match

diff --git a/tests/fate/preroll.mak b/tests/fate/preroll.mak
index cb5b959ef01..73220b6e258 100644
--- a/tests/fate/preroll.mak
+++ b/tests/fate/preroll.mak
@@ -49,6 +49,20 @@ endef
 $(eval $(call 
FATE_PREROLL_MATCH_TEST,mp3,MP3,MP3FLOAT,mp3float,gapless/gapless.mp3))
 $(eval $(call 
FATE_PREROLL_MATCH_TEST,mp3-itunes,MP3,MP3FLOAT,mp3float,gapless/gapless-itunes.mp3))
 
+# Opus never matches a continuous decode exactly after a seek, it only
+# converges to it: the frames printed are those within 1 LSB of it, silenced
+# so that the output does not depend on the platform. The seek points are
+# ones where the decoder converges slowly.
+# 1: test name, 2: sample, 3: seek target
+define FATE_PREROLL_OPUS_MATCH_TEST
+FATE_PREROLL_FFMPEG-$(call FRAMECRC, MATROSKA, OPUS, PCM_S16LE_ENCODER 
ARESAMPLE_FILTER AMIX_FILTER ASTATS_FILTER AMETADATA_FILTER ATRIM_FILTER 
ASETPTS_FILTER VOLUME_FILTER) += fate-preroll-opus-$(1)-match
+fate-preroll-opus-$(1)-match: CMD = framecrc $(PREROLL_OPT) -ss $(3) -t 0.3 
-c:a opus -i $(TARGET_SAMPLES)/$(2) -c:a opus -i $(TARGET_SAMPLES)/$(2) 
-filter_complex 
"[0:a:0]asetpts=PTS-STARTPTS[seek];[1:a:0]atrim=start=$(3):duration=0.3,asetpts=PTS-STARTPTS[full];[seek][full]amix=inputs=2:weights=1|-1:duration=shortest,astats=metadata=1:reset=1:measure_overall=Peak_level:measure_perchannel=none,ametadata=mode=select:key=lavfi.astats.Overall.Peak_level:value=-96:function=less,volume=0,aresample"
 -c:a pcm_s16le
+endef
+
+$(eval $(call FATE_PREROLL_OPUS_MATCH_TEST,celt,opus/testvector11.mka,9.25))
+$(eval $(call FATE_PREROLL_OPUS_MATCH_TEST,silk,opus/testvector04.mka,6.66))
+$(eval $(call FATE_PREROLL_OPUS_MATCH_TEST,hybrid,opus/testvector06.mka,8.88))
+
 # A seek that lands inside the priming, which here spans the first three
 # packets: the rest of the priming, or the pre-roll if longer, is skipped.
 FATE_PREROLL_FFMPEG-$(call FRAMECRC, CAF, AAC, PCM_S16LE_ENCODER 
ARESAMPLE_FILTER VOLUME_FILTER) += fate-preroll-aac-caf-decode-in-priming
diff --git a/tests/ref/fate/preroll-opus-celt-match 
b/tests/ref/fate/preroll-opus-celt-match
new file mode 100644
index 00000000000..420a3820e79
--- /dev/null
+++ b/tests/ref/fate/preroll-opus-celt-match
@@ -0,0 +1,6 @@
+#tb 0: 1/48000
+#media_type 0: audio
+#codec_id 0: pcm_s16le
+#sample_rate 0: 48000
+#channel_layout_name 0: stereo
+0,      12960,      12960,     1440,     5760, 0x00000000
diff --git a/tests/ref/fate/preroll-opus-hybrid-match 
b/tests/ref/fate/preroll-opus-hybrid-match
new file mode 100644
index 00000000000..f8383372ac9
--- /dev/null
+++ b/tests/ref/fate/preroll-opus-hybrid-match
@@ -0,0 +1,5 @@
+#tb 0: 1/48000
+#media_type 0: audio
+#codec_id 0: pcm_s16le
+#sample_rate 0: 48000
+#channel_layout_name 0: stereo
diff --git a/tests/ref/fate/preroll-opus-silk-match 
b/tests/ref/fate/preroll-opus-silk-match
new file mode 100644
index 00000000000..6d71611d0d7
--- /dev/null
+++ b/tests/ref/fate/preroll-opus-silk-match
@@ -0,0 +1,14 @@
+#tb 0: 1/48000
+#media_type 0: audio
+#codec_id 0: pcm_s16le
+#sample_rate 0: 48000
+#channel_layout_name 0: stereo
+0,       5760,       5760,      960,     3840, 0x00000000
+0,       6720,       6720,      960,     3840, 0x00000000
+0,       7680,       7680,      960,     3840, 0x00000000
+0,       8640,       8640,      960,     3840, 0x00000000
+0,       9600,       9600,      960,     3840, 0x00000000
+0,      10560,      10560,      960,     3840, 0x00000000
+0,      11520,      11520,      960,     3840, 0x00000000
+0,      12480,      12480,      960,     3840, 0x00000000
+0,      13440,      13440,      960,     3840, 0x00000000
-- 
2.54.1


>From 6abc727f8c18db736fcdcceb8892820053a76e3d Mon Sep 17 00:00:00 2001
From: Romain Beauxis <[email protected]>
Date: Fri, 27 Nov 2026 14:04:53 -0600
Subject: [PATCH 2/3] avcodec/opus: declare a seek pre-roll of 400 ms

RFC 7845 asks for at least 80 ms of pre-roll before a seek target, so
that the output is "correct" by then. Measured on the FATE Opus samples
(the 12 test vectors and caf/opus.caf, 766 seek points, each decoded
from a cold start and compared with a decode from the start), the
largest error left after a pre-roll is, in 16-bit LSB:

  pre-roll   p99     max
    80 ms   1525    2247
   200 ms     84     445
   300 ms    6.5      96
   350 ms    1.3      10
   400 ms   0.10    0.89
   500 ms   0.05    0.16

The error falls without a cliff and never reaches zero: some points
still differ after 2.8 s. 400 ms is the first value where every point
is within 1 LSB.

The decoders declare it, or the container's pre-roll when longer. The
pre-roll only applies to seeks with AVSEEK_FLAG_PREROLL, so stream copy
does not change.
---
 libavcodec/libopusdec.c                       |  3 ++
 libavcodec/opus/dec.c                         |  2 ++
 libavcodec/opus/tab.h                         |  8 +++++
 tests/ref/fate/oggopus-demux                  |  2 +-
 tests/ref/fate/preroll-opus-caf-declared      |  2 +-
 .../fate/preroll-opus-caf-decode-noaccurate   |  4 +--
 tests/ref/fate/preroll-opus-caf-landing       |  6 ++--
 tests/ref/fate/preroll-opus-celt-match        |  6 ++++
 tests/ref/fate/preroll-opus-hybrid-match      | 30 +++++++++++++++++++
 tests/ref/fate/preroll-opus-mkv-declared      |  2 +-
 tests/ref/fate/preroll-opus-mkv-landing       |  6 ++--
 tests/ref/fate/preroll-opus-mkv-landing-end   |  3 +-
 tests/ref/fate/preroll-opus-mpegts-declared   |  2 +-
 tests/ref/fate/preroll-opus-mpegts-landing    |  6 ++--
 tests/ref/fate/preroll-opus-ogg-declared      |  2 +-
 tests/ref/fate/preroll-opus-silk-match        |  5 ++++
 tests/ref/fate/ts-opus-demux                  |  2 +-
 17 files changed, 73 insertions(+), 18 deletions(-)

diff --git a/libavcodec/libopusdec.c b/libavcodec/libopusdec.c
index 527ed5890ae..80da1ff4058 100644
--- a/libavcodec/libopusdec.c
+++ b/libavcodec/libopusdec.c
@@ -33,6 +33,7 @@
 #include "internal.h"
 #include "mathops.h"
 #include "libopus.h"
+#include "opus/tab.h"
 #include "vorbis_data.h"
 
 struct libopus_context {
@@ -75,6 +76,8 @@ static av_cold int libopus_decode_init(AVCodecContext *avc)
         av_log(avc, AV_LOG_WARNING, "Invalid sample rate %d\n", 
avc->sample_rate);
         return AVERROR(EINVAL);
     }
+    avc->seek_preroll = FFMAX(avc->seek_preroll,
+                              av_rescale(OPUS_SEEK_PREROLL_US, 
avc->sample_rate, AV_TIME_BASE));
 
     avc->sample_fmt     = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
                           AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
diff --git a/libavcodec/opus/dec.c b/libavcodec/opus/dec.c
index 218abd092c1..e873483726f 100644
--- a/libavcodec/opus/dec.c
+++ b/libavcodec/opus/dec.c
@@ -635,6 +635,8 @@ static av_cold int opus_decode_init(AVCodecContext *avctx)
     avctx->sample_fmt  = AV_SAMPLE_FMT_FLTP;
     if (!avctx->sample_rate)
         avctx->sample_rate = 48000;
+    avctx->seek_preroll = FFMAX(avctx->seek_preroll,
+                                av_rescale(OPUS_SEEK_PREROLL_US, 
avctx->sample_rate, AV_TIME_BASE));
 
     c->fdsp = avpriv_float_dsp_alloc(0);
     if (!c->fdsp)
diff --git a/libavcodec/opus/tab.h b/libavcodec/opus/tab.h
index 5e114a1a73c..07fecca8927 100644
--- a/libavcodec/opus/tab.h
+++ b/libavcodec/opus/tab.h
@@ -34,6 +34,14 @@
  */
 #define OPUS_TIMESTAMP_RATE 48000
 
+/**
+ * The pre-roll a decoder started inside the stream needs before its output
+ * is within 1 LSB of a continuous decode, in microseconds. The 80 ms of
+ * RFC 7845 leaves errors up to -23 dBFS. The output never becomes exactly
+ * that of a continuous decode.
+ */
+#define OPUS_SEEK_PREROLL_US 400000
+
 FF_VISIBILITY_PUSH_HIDDEN
 extern const uint16_t ff_opus_frame_duration[32];
 
diff --git a/tests/ref/fate/oggopus-demux b/tests/ref/fate/oggopus-demux
index ed55af7fa3e..3825dfda2f9 100644
--- a/tests/ref/fate/oggopus-demux
+++ b/tests/ref/fate/oggopus-demux
@@ -39,5 +39,5 @@ 
packet|codec_type=audio|stream_index=0|pts=35164|pts_time=0.732583|dts=35164|dts
 
packet|codec_type=audio|stream_index=0|pts=36124|pts_time=0.752583|dts=36124|dts_time=0.752583|duration=960|duration_time=0.020000|size=217|pos=841|flags=K__|data_hash=CRC32:b04fe85a
 
packet|codec_type=audio|stream_index=0|pts=37084|pts_time=0.772583|dts=37084|dts_time=0.772583|duration=960|duration_time=0.020000|size=217|pos=841|flags=K__|data_hash=CRC32:06797ece
 
packet|codec_type=audio|stream_index=0|pts=38044|pts_time=0.792583|dts=38044|dts_time=0.792583|duration=356|duration_time=0.007417|size=359|pos=841|flags=K__|data_hash=CRC32:01ca3f8f|side_datum/skip_samples:side_data_type=Skip
 
Samples|side_datum/skip_samples:skip_samples=0|side_datum/skip_samples:discard_padding=604|side_datum/skip_samples:skip_reason=0|side_datum/skip_samples:discard_reason=0
-stream|index=0|codec_name=opus|profile=unknown|level=-99|codec_type=audio|codec_tag_string=[0][0][0][0]|codec_tag=0x0000|mime_codec_string=opus|sample_fmt=fltp|sample_rate=48000|channels=2|channel_layout=stereo|bits_per_sample=0|initial_padding=356|seek_preroll=3840|id=N/A|r_frame_rate=0/0|avg_frame_rate=0/0|time_base=1/48000|start_pts=0|start_time=0.000000|duration_ts=38756|duration=0.807417|bit_rate=N/A|max_bit_rate=N/A|bits_per_raw_sample=N/A|nb_frames=N/A|nb_read_frames=N/A|nb_read_packets=41|extradata_size=19|extradata_hash=CRC32:58ba5ff3|disposition:default=0|disposition:dub=0|disposition:original=0|disposition:comment=0|disposition:lyrics=0|disposition:karaoke=0|disposition:forced=0|disposition:hearing_impaired=0|disposition:visual_impaired=0|disposition:clean_effects=0|disposition:attached_pic=0|disposition:timed_thumbnails=0|disposition:non_diegetic=0|disposition:captions=0|disposition:descriptions=0|disposition:metadata=0|disposition:dependent=0|disposition:still_image=0|d
 isposition:multilayer=0|tag:ENCODER=opusenc from opus-tools 
0.1.9|tag:ENCODER_OPTIONS=--discard-comments
+stream|index=0|codec_name=opus|profile=unknown|level=-99|codec_type=audio|codec_tag_string=[0][0][0][0]|codec_tag=0x0000|mime_codec_string=opus|sample_fmt=fltp|sample_rate=48000|channels=2|channel_layout=stereo|bits_per_sample=0|initial_padding=356|seek_preroll=19200|id=N/A|r_frame_rate=0/0|avg_frame_rate=0/0|time_base=1/48000|start_pts=0|start_time=0.000000|duration_ts=38756|duration=0.807417|bit_rate=N/A|max_bit_rate=N/A|bits_per_raw_sample=N/A|nb_frames=N/A|nb_read_frames=N/A|nb_read_packets=41|extradata_size=19|extradata_hash=CRC32:58ba5ff3|disposition:default=0|disposition:dub=0|disposition:original=0|disposition:comment=0|disposition:lyrics=0|disposition:karaoke=0|disposition:forced=0|disposition:hearing_impaired=0|disposition:visual_impaired=0|disposition:clean_effects=0|disposition:attached_pic=0|disposition:timed_thumbnails=0|disposition:non_diegetic=0|disposition:captions=0|disposition:descriptions=0|disposition:metadata=0|disposition:dependent=0|disposition:still_image=0|
 disposition:multilayer=0|tag:ENCODER=opusenc from opus-tools 
0.1.9|tag:ENCODER_OPTIONS=--discard-comments
 
format|filename=intro-partial.opus|nb_streams=1|nb_programs=0|nb_stream_groups=0|format_name=ogg|start_time=0.000000|duration=0.807417|size=10250|bit_rate=101558|probe_score=100
diff --git a/tests/ref/fate/preroll-opus-caf-declared 
b/tests/ref/fate/preroll-opus-caf-declared
index 41af72aed5a..a9ade7d56b6 100644
--- a/tests/ref/fate/preroll-opus-caf-declared
+++ b/tests/ref/fate/preroll-opus-caf-declared
@@ -1 +1 @@
-stream|codec_name=opus|sample_rate=48000|initial_padding=312|seek_preroll=0
+stream|codec_name=opus|sample_rate=48000|initial_padding=312|seek_preroll=19200
diff --git a/tests/ref/fate/preroll-opus-caf-decode-noaccurate 
b/tests/ref/fate/preroll-opus-caf-decode-noaccurate
index a35cbac6a27..e954dc96f42 100644
--- a/tests/ref/fate/preroll-opus-caf-decode-noaccurate
+++ b/tests/ref/fate/preroll-opus-caf-decode-noaccurate
@@ -3,11 +3,11 @@
 #codec_id 0: pcm_s16le
 #sample_rate 0: 48000
 #channel_layout_name 0: stereo
-0,       -480,       -480,      648,     2592, 0x00000000
+0,       -792,       -792,      960,     3840, 0x00000000
 0,        168,        168,      960,     3840, 0x00000000
 0,       1128,       1128,      960,     3840, 0x00000000
 0,       2088,       2088,      960,     3840, 0x00000000
 0,       3048,       3048,      960,     3840, 0x00000000
 0,       4008,       4008,      960,     3840, 0x00000000
 0,       4968,       4968,      960,     3840, 0x00000000
-0,       5928,       5928,      792,     3168, 0x00000000
+0,       5928,       5928,      480,     1920, 0x00000000
diff --git a/tests/ref/fate/preroll-opus-caf-landing 
b/tests/ref/fate/preroll-opus-caf-landing
index 59f055919ee..4397cb14c37 100644
--- a/tests/ref/fate/preroll-opus-caf-landing
+++ b/tests/ref/fate/preroll-opus-caf-landing
@@ -1,3 +1,3 @@
-packet|pts=24648|dts=24648|duration=960
-packet|pts=25608|dts=25608|duration=960
-packet|pts=26568|dts=26568|duration=960
+packet|pts=5448|dts=5448|duration=960
+packet|pts=6408|dts=6408|duration=960
+packet|pts=7368|dts=7368|duration=960
diff --git a/tests/ref/fate/preroll-opus-celt-match 
b/tests/ref/fate/preroll-opus-celt-match
index 420a3820e79..5b74e4e54c9 100644
--- a/tests/ref/fate/preroll-opus-celt-match
+++ b/tests/ref/fate/preroll-opus-celt-match
@@ -3,4 +3,10 @@
 #codec_id 0: pcm_s16le
 #sample_rate 0: 48000
 #channel_layout_name 0: stereo
+0,          0,          0,     1440,     5760, 0x00000000
+0,       1440,       1440,     5760,    23040, 0x00000000
+0,       7200,       7200,      960,     3840, 0x00000000
+0,       8160,       8160,      960,     3840, 0x00000000
+0,       9120,       9120,      960,     3840, 0x00000000
+0,      10080,      10080,     2880,    11520, 0x00000000
 0,      12960,      12960,     1440,     5760, 0x00000000
diff --git a/tests/ref/fate/preroll-opus-hybrid-match 
b/tests/ref/fate/preroll-opus-hybrid-match
index f8383372ac9..6eebe1ea48d 100644
--- a/tests/ref/fate/preroll-opus-hybrid-match
+++ b/tests/ref/fate/preroll-opus-hybrid-match
@@ -3,3 +3,33 @@
 #codec_id 0: pcm_s16le
 #sample_rate 0: 48000
 #channel_layout_name 0: stereo
+0,          0,          0,      480,     1920, 0x00000000
+0,        480,        480,      480,     1920, 0x00000000
+0,        960,        960,      480,     1920, 0x00000000
+0,       1440,       1440,      480,     1920, 0x00000000
+0,       1920,       1920,      480,     1920, 0x00000000
+0,       2400,       2400,      480,     1920, 0x00000000
+0,       2880,       2880,      480,     1920, 0x00000000
+0,       3360,       3360,      480,     1920, 0x00000000
+0,       3840,       3840,      480,     1920, 0x00000000
+0,       4320,       4320,      480,     1920, 0x00000000
+0,       4800,       4800,      480,     1920, 0x00000000
+0,       5280,       5280,      480,     1920, 0x00000000
+0,       5760,       5760,      480,     1920, 0x00000000
+0,       6240,       6240,      480,     1920, 0x00000000
+0,       6720,       6720,      480,     1920, 0x00000000
+0,       7200,       7200,      480,     1920, 0x00000000
+0,       7680,       7680,      480,     1920, 0x00000000
+0,       8160,       8160,      480,     1920, 0x00000000
+0,       8640,       8640,      480,     1920, 0x00000000
+0,       9120,       9120,      480,     1920, 0x00000000
+0,       9600,       9600,      480,     1920, 0x00000000
+0,      10080,      10080,      480,     1920, 0x00000000
+0,      10560,      10560,      480,     1920, 0x00000000
+0,      11040,      11040,      480,     1920, 0x00000000
+0,      11520,      11520,      480,     1920, 0x00000000
+0,      12000,      12000,      480,     1920, 0x00000000
+0,      12480,      12480,      480,     1920, 0x00000000
+0,      12960,      12960,      480,     1920, 0x00000000
+0,      13440,      13440,      480,     1920, 0x00000000
+0,      13920,      13920,      480,     1920, 0x00000000
diff --git a/tests/ref/fate/preroll-opus-mkv-declared 
b/tests/ref/fate/preroll-opus-mkv-declared
index 1ccaf495bdf..a9ade7d56b6 100644
--- a/tests/ref/fate/preroll-opus-mkv-declared
+++ b/tests/ref/fate/preroll-opus-mkv-declared
@@ -1 +1 @@
-stream|codec_name=opus|sample_rate=48000|initial_padding=312|seek_preroll=3840
+stream|codec_name=opus|sample_rate=48000|initial_padding=312|seek_preroll=19200
diff --git a/tests/ref/fate/preroll-opus-mkv-landing 
b/tests/ref/fate/preroll-opus-mkv-landing
index 6e42f858ea3..aa0dfb88a82 100644
--- a/tests/ref/fate/preroll-opus-mkv-landing
+++ b/tests/ref/fate/preroll-opus-mkv-landing
@@ -1,3 +1,3 @@
-packet|pts=434|dts=434|duration=20
-packet|pts=454|dts=454|duration=20
-packet|pts=474|dts=474|duration=20
+packet|pts=114|dts=114|duration=20
+packet|pts=134|dts=134|duration=20
+packet|pts=154|dts=154|duration=20
diff --git a/tests/ref/fate/preroll-opus-mkv-landing-end 
b/tests/ref/fate/preroll-opus-mkv-landing-end
index 23388a3e7f8..ef6c89b3ead 100644
--- a/tests/ref/fate/preroll-opus-mkv-landing-end
+++ b/tests/ref/fate/preroll-opus-mkv-landing-end
@@ -1 +1,2 @@
-packet|pts=1014|duration=20|side_datum/skip_samples:skip_samples=3840|side_datum/skip_samples:discard_padding=706
+packet|pts=694|duration=20|side_datum/skip_samples:skip_samples=19200|side_datum/skip_samples:discard_padding=0
+packet|pts=714|duration=20
diff --git a/tests/ref/fate/preroll-opus-mpegts-declared 
b/tests/ref/fate/preroll-opus-mpegts-declared
index 5a47f4983a1..53a9c997f99 100644
--- a/tests/ref/fate/preroll-opus-mpegts-declared
+++ b/tests/ref/fate/preroll-opus-mpegts-declared
@@ -1 +1 @@
-stream|codec_name=opus|sample_rate=48000|initial_padding=0|seek_preroll=0
+stream|codec_name=opus|sample_rate=48000|initial_padding=0|seek_preroll=19200
diff --git a/tests/ref/fate/preroll-opus-mpegts-landing 
b/tests/ref/fate/preroll-opus-mpegts-landing
index 29f4104fb96..6fbe23562d2 100644
--- a/tests/ref/fate/preroll-opus-mpegts-landing
+++ b/tests/ref/fate/preroll-opus-mpegts-landing
@@ -1,3 +1,3 @@
-packet|pts=46800|dts=46800|duration=1800
-packet|pts=48600|dts=48600|duration=1800
-packet|pts=50400|dts=50400|duration=1800
+packet|pts=10800|dts=10800|duration=1800
+packet|pts=12600|dts=12600|duration=1800
+packet|pts=14400|dts=14400|duration=1800
diff --git a/tests/ref/fate/preroll-opus-ogg-declared 
b/tests/ref/fate/preroll-opus-ogg-declared
index 1ccaf495bdf..a9ade7d56b6 100644
--- a/tests/ref/fate/preroll-opus-ogg-declared
+++ b/tests/ref/fate/preroll-opus-ogg-declared
@@ -1 +1 @@
-stream|codec_name=opus|sample_rate=48000|initial_padding=312|seek_preroll=3840
+stream|codec_name=opus|sample_rate=48000|initial_padding=312|seek_preroll=19200
diff --git a/tests/ref/fate/preroll-opus-silk-match 
b/tests/ref/fate/preroll-opus-silk-match
index 6d71611d0d7..80b167b714f 100644
--- a/tests/ref/fate/preroll-opus-silk-match
+++ b/tests/ref/fate/preroll-opus-silk-match
@@ -3,6 +3,11 @@
 #codec_id 0: pcm_s16le
 #sample_rate 0: 48000
 #channel_layout_name 0: stereo
+0,          0,          0,      960,     3840, 0x00000000
+0,        960,        960,     1920,     7680, 0x00000000
+0,       2880,       2880,      960,     3840, 0x00000000
+0,       3840,       3840,      960,     3840, 0x00000000
+0,       4800,       4800,      960,     3840, 0x00000000
 0,       5760,       5760,      960,     3840, 0x00000000
 0,       6720,       6720,      960,     3840, 0x00000000
 0,       7680,       7680,      960,     3840, 0x00000000
diff --git a/tests/ref/fate/ts-opus-demux b/tests/ref/fate/ts-opus-demux
index 8953f10b93f..d8ec8377848 100644
--- a/tests/ref/fate/ts-opus-demux
+++ b/tests/ref/fate/ts-opus-demux
@@ -510,5 +510,5 @@ 
packet|codec_type=audio|stream_index=0|pts=914400|pts_time=10.160000|dts=914400|
 
packet|codec_type=audio|stream_index=0|pts=916200|pts_time=10.180000|dts=916200|dts_time=10.180000|duration=1800|duration_time=0.020000|size=760|pos=508728|flags=K__|data_hash=CRC32:fdf0ce4a|side_datum/mpegts_stream_id:side_data_type=MPEGTS
 Stream ID|side_datum/mpegts_stream_id:id=189
 
packet|codec_type=audio|stream_index=0|pts=918000|pts_time=10.200000|dts=918000|dts_time=10.200000|duration=1800|duration_time=0.020000|size=761|pos=510044|flags=K__|data_hash=CRC32:75113c11|side_datum/mpegts_stream_id:side_data_type=MPEGTS
 Stream ID|side_datum/mpegts_stream_id:id=189
 
packet|codec_type=audio|stream_index=0|pts=919800|pts_time=10.220000|dts=919800|dts_time=10.220000|duration=1800|duration_time=0.020000|size=759|pos=510984|flags=K__|data_hash=CRC32:59fc266f|side_datum/mpegts_stream_id:side_data_type=MPEGTS
 Stream ID|side_datum/mpegts_stream_id:id=189
-stream|index=0|codec_name=opus|profile=unknown|level=-99|codec_type=audio|codec_tag_string=Opus|codec_tag=0x7375704f|mime_codec_string=opus|sample_fmt=fltp|sample_rate=48000|channels=8|channel_layout=7.1|bits_per_sample=0|initial_padding=0|seek_preroll=0|ts_id=51338|ts_packetsize=188|id=0x44|r_frame_rate=0/0|avg_frame_rate=0/0|time_base=1/90000|start_pts=0|start_time=0.000000|duration_ts=919800|duration=10.220000|bit_rate=N/A|max_bit_rate=N/A|bits_per_raw_sample=N/A|nb_frames=N/A|nb_read_frames=N/A|nb_read_packets=512|extradata_size=29|extradata_hash=CRC32:6d6089a7|disposition:default=0|disposition:dub=0|disposition:original=0|disposition:comment=0|disposition:lyrics=0|disposition:karaoke=0|disposition:forced=0|disposition:hearing_impaired=0|disposition:visual_impaired=0|disposition:clean_effects=0|disposition:attached_pic=0|disposition:timed_thumbnails=0|disposition:non_diegetic=0|disposition:captions=0|disposition:descriptions=0|disposition:metadata=0|disposition:dependent=0|dispo
 sition:still_image=0|disposition:multilayer=0
+stream|index=0|codec_name=opus|profile=unknown|level=-99|codec_type=audio|codec_tag_string=Opus|codec_tag=0x7375704f|mime_codec_string=opus|sample_fmt=fltp|sample_rate=48000|channels=8|channel_layout=7.1|bits_per_sample=0|initial_padding=0|seek_preroll=19200|ts_id=51338|ts_packetsize=188|id=0x44|r_frame_rate=0/0|avg_frame_rate=0/0|time_base=1/90000|start_pts=0|start_time=0.000000|duration_ts=919800|duration=10.220000|bit_rate=N/A|max_bit_rate=N/A|bits_per_raw_sample=N/A|nb_frames=N/A|nb_read_frames=N/A|nb_read_packets=512|extradata_size=29|extradata_hash=CRC32:6d6089a7|disposition:default=0|disposition:dub=0|disposition:original=0|disposition:comment=0|disposition:lyrics=0|disposition:karaoke=0|disposition:forced=0|disposition:hearing_impaired=0|disposition:visual_impaired=0|disposition:clean_effects=0|disposition:attached_pic=0|disposition:timed_thumbnails=0|disposition:non_diegetic=0|disposition:captions=0|disposition:descriptions=0|disposition:metadata=0|disposition:dependent=0|d
 isposition:still_image=0|disposition:multilayer=0
 
format|filename=test-8-7.1.opus-small.ts|nb_streams=1|nb_programs=1|nb_stream_groups=0|format_name=mpegts|start_time=0.000000|duration=10.220000|size=512000|bit_rate=400782|probe_score=50
-- 
2.54.1


>From 9cca63e87cbfedf35fb61071a41ccf3977e27e84 Mon Sep 17 00:00:00 2001
From: Romain Beauxis <[email protected]>
Date: Fri, 27 Nov 2026 14:05:26 -0600
Subject: [PATCH 3/3] avcodec/opus: add a seek_preroll option

The pre-roll trades the cost of a seek for the accuracy of the output
after it. The option sets it on the native and libopus decoders. It
takes effect in the decoder that probes the stream, which ffmpeg and
ffprobe pass decoder options to:

  ffmpeg -seek_flags preroll -seek_preroll 0.2 -ss 10 -i in.opus ...
---
 doc/decoders.texi                              | 18 ++++++++++++++++++
 libavcodec/libopusdec.c                        |  4 +++-
 libavcodec/opus/dec.c                          |  4 +++-
 libavcodec/version.h                           |  2 +-
 tests/fate/preroll.mak                         |  9 +++++++--
 .../ref/fate/preroll-opus-caf-declared-option  |  1 +
 tests/ref/fate/preroll-opus-mkv-landing-end    |  3 +--
 7 files changed, 34 insertions(+), 7 deletions(-)
 create mode 100644 tests/ref/fate/preroll-opus-caf-declared-option

diff --git a/doc/decoders.texi b/doc/decoders.texi
index 76b4c813dcd..3472117d175 100644
--- a/doc/decoders.texi
+++ b/doc/decoders.texi
@@ -454,6 +454,24 @@ configuration. You need to explicitly configure the build 
with
 An FFmpeg native decoder for Opus exists, so users can decode Opus
 without this library.
 
+It accepts the @option{seek_preroll} option of the native opus decoder.
+
+@section opus
+
+Opus decoder.
+
+@subsection Options
+
+@table @option
+@item seek_preroll @var{duration}
+Audio to decode and discard before the target of a seek with
+@code{-seek_flags preroll}, which the decoder declares to the demuxer when
+the stream is probed. On the FATE Opus test vectors, the output is within
+1 LSB of a continuous decode after 400 ms, the default, and never matches
+it exactly. A shorter value makes seeks cheaper and less accurate. The
+container's pre-roll applies when it is longer.
+@end table
+
 @section pcm_dvda
 
 PCM DVD-Audio decoder.
diff --git a/libavcodec/libopusdec.c b/libavcodec/libopusdec.c
index 80da1ff4058..b27e2ac5047 100644
--- a/libavcodec/libopusdec.c
+++ b/libavcodec/libopusdec.c
@@ -41,6 +41,7 @@ struct libopus_context {
     OpusMSDecoder *dec;
     int pre_skip;
     int sample_rate;
+    int64_t seek_preroll;
 #ifndef OPUS_SET_GAIN
     union { int i; double d; } gain;
 #endif
@@ -77,7 +78,7 @@ static av_cold int libopus_decode_init(AVCodecContext *avc)
         return AVERROR(EINVAL);
     }
     avc->seek_preroll = FFMAX(avc->seek_preroll,
-                              av_rescale(OPUS_SEEK_PREROLL_US, 
avc->sample_rate, AV_TIME_BASE));
+                              av_rescale(opus->seek_preroll, avc->sample_rate, 
AV_TIME_BASE));
 
     avc->sample_fmt     = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
                           AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
@@ -237,6 +238,7 @@ static const AVOption libopusdec_options[] = {
 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
     { "apply_phase_inv", "Apply intensity stereo phase inversion", 
OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
 #endif
+    { "seek_preroll", "Audio to decode and discard before the target of a 
seek, at least the container's", OFFSET(seek_preroll), AV_OPT_TYPE_DURATION, { 
.i64 = OPUS_SEEK_PREROLL_US }, 0, INT_MAX, FLAGS },
     { NULL },
 };
 
diff --git a/libavcodec/opus/dec.c b/libavcodec/opus/dec.c
index e873483726f..3e91eeb5f4a 100644
--- a/libavcodec/opus/dec.c
+++ b/libavcodec/opus/dec.c
@@ -130,6 +130,7 @@ typedef struct OpusContext {
 
     struct OpusStreamContext *streams;
     int apply_phase_inv;
+    int64_t seek_preroll;
 
     AVFloatDSPContext *fdsp;
     float   gain;
@@ -636,7 +637,7 @@ static av_cold int opus_decode_init(AVCodecContext *avctx)
     if (!avctx->sample_rate)
         avctx->sample_rate = 48000;
     avctx->seek_preroll = FFMAX(avctx->seek_preroll,
-                                av_rescale(OPUS_SEEK_PREROLL_US, 
avctx->sample_rate, AV_TIME_BASE));
+                                av_rescale(c->seek_preroll, 
avctx->sample_rate, AV_TIME_BASE));
 
     c->fdsp = avpriv_float_dsp_alloc(0);
     if (!c->fdsp)
@@ -847,6 +848,7 @@ static av_cold void opus_decode_flush(AVCodecContext *ctx)
 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
 static const AVOption opus_options[] = {
     { "apply_phase_inv", "Apply intensity stereo phase inversion", 
OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AD },
+    { "seek_preroll", "Audio to decode and discard before the target of a 
seek, at least the container's", OFFSET(seek_preroll), AV_OPT_TYPE_DURATION, { 
.i64 = OPUS_SEEK_PREROLL_US }, 0, INT_MAX, AD },
     { NULL },
 };
 
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 87b7284a953..b564e2d8496 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -30,7 +30,7 @@
 #include "version_major.h"
 
 #define LIBAVCODEC_VERSION_MINOR  28
-#define LIBAVCODEC_VERSION_MICRO 100
+#define LIBAVCODEC_VERSION_MICRO 101
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
                                                LIBAVCODEC_VERSION_MINOR, \
diff --git a/tests/fate/preroll.mak b/tests/fate/preroll.mak
index 73220b6e258..4b818fc969f 100644
--- a/tests/fate/preroll.mak
+++ b/tests/fate/preroll.mak
@@ -63,6 +63,10 @@ $(eval $(call 
FATE_PREROLL_OPUS_MATCH_TEST,celt,opus/testvector11.mka,9.25))
 $(eval $(call FATE_PREROLL_OPUS_MATCH_TEST,silk,opus/testvector04.mka,6.66))
 $(eval $(call FATE_PREROLL_OPUS_MATCH_TEST,hybrid,opus/testvector06.mka,8.88))
 
+# A shorter pre-roll set with the decoder option.
+FATE_PREROLL_FFPROBE-$(call DEMDEC, CAF, OPUS) += 
fate-preroll-opus-caf-declared-option
+fate-preroll-opus-caf-declared-option: CMD = run ffprobe$(PROGSSUF)$(EXESUF) 
-seek_preroll 0.2 -select_streams a:0 -show_entries 
stream=codec_name,sample_rate,initial_padding,seek_preroll -print_format 
compact $(TARGET_SAMPLES)/caf/opus.caf
+
 # A seek that lands inside the priming, which here spans the first three
 # packets: the rest of the priming, or the pre-roll if longer, is skipped.
 FATE_PREROLL_FFMPEG-$(call FRAMECRC, CAF, AAC, PCM_S16LE_ENCODER 
ARESAMPLE_FILTER VOLUME_FILTER) += fate-preroll-aac-caf-decode-in-priming
@@ -74,11 +78,12 @@ FATE_PREROLL_SEEK-$(call DEMDEC, MOV, AAC) += 
fate-preroll-aac-mov-seek-forward
 fate-preroll-aac-mov-seek-forward: CMD = run libavformat/tests/seek$(EXESUF) 
$(TARGET_SAMPLES)/gapless/102400samples_qt-lc-aac.m4a -seekforw 530000 -seekmin 
400000 -frames 2 -duration 3 $(PREROLL_OPT)
 
 # A seek past the end lands on the last packet, which carries the padding
-# to discard at the end of the stream.
+# to discard at the end of the stream. The Opus pre-roll is shortened to
+# the container's, so that it lands there too.
 FATE_PREROLL_FFPROBE-$(call DEMDEC, CAF, AAC) += 
fate-preroll-aac-caf-landing-end
 fate-preroll-aac-caf-landing-end: CMD = run ffprobe$(PROGSSUF)$(EXESUF) 
$(PREROLL_OPT) -select_streams a:0 -read_intervals 5.45%+\#2 -show_entries 
packet=pts,duration:packet_side_data=skip_samples,discard_padding -print_format 
compact $(TARGET_SAMPLES)/caf/aac.caf
 FATE_PREROLL_FFPROBE-$(call DEMDEC, MATROSKA, OPUS) += 
fate-preroll-opus-mkv-landing-end
-fate-preroll-opus-mkv-landing-end: CMD = run ffprobe$(PROGSSUF)$(EXESUF) 
$(PREROLL_OPT) -select_streams a:0 -read_intervals 1.1%+\#2 -show_entries 
packet=pts,duration:packet_side_data=skip_samples,discard_padding -print_format 
compact $(TARGET_SAMPLES)/mkv/codec_delay_opus.mkv
+fate-preroll-opus-mkv-landing-end: CMD = run ffprobe$(PROGSSUF)$(EXESUF) 
$(PREROLL_OPT) -seek_preroll 0.08 -select_streams a:0 -read_intervals 1.1%+\#2 
-show_entries packet=pts,duration:packet_side_data=skip_samples,discard_padding 
-print_format compact $(TARGET_SAMPLES)/mkv/codec_delay_opus.mkv
 
 # A seek on the video stream, just after a keyframe: the audio pre-roll
 # still needs room.
diff --git a/tests/ref/fate/preroll-opus-caf-declared-option 
b/tests/ref/fate/preroll-opus-caf-declared-option
new file mode 100644
index 00000000000..3bcc8e19854
--- /dev/null
+++ b/tests/ref/fate/preroll-opus-caf-declared-option
@@ -0,0 +1 @@
+stream|codec_name=opus|sample_rate=48000|initial_padding=312|seek_preroll=9600
diff --git a/tests/ref/fate/preroll-opus-mkv-landing-end 
b/tests/ref/fate/preroll-opus-mkv-landing-end
index ef6c89b3ead..23388a3e7f8 100644
--- a/tests/ref/fate/preroll-opus-mkv-landing-end
+++ b/tests/ref/fate/preroll-opus-mkv-landing-end
@@ -1,2 +1 @@
-packet|pts=694|duration=20|side_datum/skip_samples:skip_samples=19200|side_datum/skip_samples:discard_padding=0
-packet|pts=714|duration=20
+packet|pts=1014|duration=20|side_datum/skip_samples:skip_samples=3840|side_datum/skip_samples:discard_padding=706
-- 
2.54.1

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

Reply via email to