PR #25750 opened by James Almer (jamrial)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25750
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25750.patch
Demuxers should not add considerations for an eventual post-decoding state, and
only export coded parameters.
This ensures timestamps are properly updated when handling priming samples.
Before:
```
$ ffmpeg -i ~/fate-samples/audiomatch/tones_opus_48000_stereo.mka -f framecrc -
Output #0, framecrc, to 'pipe:':
Metadata:
encoder : Lavf63.18.100
Stream #0:0: Audio: pcm_s16le, 48000 Hz, stereo, s16, 1536 kb/s (default)
Metadata:
encoder : Lavc63.26.100 pcm_s16le
ENCODER_OPTIONS : --bitrate 96
0, -27, -27, 648, 2592, 0x372ff818
0, 621, 621, 960, 3840, 0x53577417
[...]
0, 94701, 94701, 960, 3840, 0x56c74d8c
0, 95661, 95661, 312, 1248, 0x67d480ff
video:0KiB audio:375KiB subtitle:0KiB other streams:0KiB global headers:0KiB
muxing overhead: unknown
size= 6KiB time=00:00:01.99
```
After:
```
$ ffmpeg -i ~/fate-samples/audiomatch/tones_opus_48000_stereo.mka -f framecrc -
Output #0, framecrc, to 'pipe:':
Metadata:
encoder : Lavf63.18.100
Stream #0:0: Audio: pcm_s16le, 48000 Hz, stereo, s16, 1536 kb/s (default)
Metadata:
encoder : Lavc63.26.100 pcm_s16le
ENCODER_OPTIONS : --bitrate 96
0, 0, 0, 648, 2592, 0x372ff818
0, 732, 732, 960, 3840, 0x53577417
[...]
0, 95321, 95321, 960, 3840, 0x56c74d8c
0, 95688, 95688, 312, 1248, 0x67d480ff
video:0KiB audio:375KiB subtitle:0KiB other streams:0KiB global headers:0KiB
muxing overhead: unknown
size= 6KiB time=00:00:02.00
```
>From a854f0e147f18ad0638fd8196b2c1b697cbbd485 Mon Sep 17 00:00:00 2001
From: James Almer <[email protected]>
Date: Wed, 4 Nov 2026 10:25:27 -0300
Subject: [PATCH 1/4] avcodec/decode: use frame sample rate for calculations
related to discarded samples
Signed-off-by: James Almer <[email protected]>
---
libavcodec/decode.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index e1d908fd52a..7cce28a54d0 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -374,9 +374,9 @@ static int discard_samples(AVCodecContext *avctx, AVFrame
*frame, int64_t *disca
} else {
av_samples_copy(frame->extended_data, frame->extended_data, 0,
avci->skip_samples,
frame->nb_samples - avci->skip_samples,
avctx->ch_layout.nb_channels, frame->format);
- if (avctx->pkt_timebase.num && avctx->sample_rate) {
+ if (avctx->pkt_timebase.num && frame->sample_rate) {
int64_t diff_ts = av_rescale_q(avci->skip_samples,
- (AVRational){1,
avctx->sample_rate},
+ (AVRational){1,
frame->sample_rate},
avctx->pkt_timebase);
if (diff_ts != AV_NOPTS_VALUE) {
if (frame->pts != AV_NOPTS_VALUE)
--
2.54.0
>From dcec3140cbb9267342a357ae6bd84881e4f08500 Mon Sep 17 00:00:00 2001
From: James Almer <[email protected]>
Date: Wed, 4 Nov 2026 10:26:39 -0300
Subject: [PATCH 2/4] libavcodec/opus/parser: don't override sample rate if
it's set
Signed-off-by: James Almer <[email protected]>
---
libavcodec/opus/parser.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/libavcodec/opus/parser.c b/libavcodec/opus/parser.c
index 7cecac88578..be04cf78bda 100644
--- a/libavcodec/opus/parser.c
+++ b/libavcodec/opus/parser.c
@@ -177,7 +177,8 @@ static int opus_parse(AVCodecParserContext *ctx,
AVCodecContext *avctx,
ParseContext *pc = &s->pc;
int next, header_len = 0;
- avctx->sample_rate = 48000;
+ if (!avctx->sample_rate)
+ avctx->sample_rate = 48000;
if (avctx->extradata && !s->extradata_parsed) {
if (ff_opus_parse_extradata(avctx, &s->ctx) < 0) {
--
2.54.0
>From 0cd50f098b2d67e03862ab5069abe3d3b4e80036 Mon Sep 17 00:00:00 2001
From: James Almer <[email protected]>
Date: Wed, 4 Nov 2026 10:29:14 -0300
Subject: [PATCH 3/4] libavcodec/opus/dec: don't override sample rate if it's
set
While the decoded output will always be 48000Hz, input parameters may be
inherited
from the source audio used during encoding, including sample rate, timestamps,
and
priming/trailing samples to discard.
Signed-off-by: James Almer <[email protected]>
---
libavcodec/opus/dec.c | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
diff --git a/libavcodec/opus/dec.c b/libavcodec/opus/dec.c
index 712a0351b68..9cd79096d1a 100644
--- a/libavcodec/opus/dec.c
+++ b/libavcodec/opus/dec.c
@@ -507,6 +507,7 @@ static int opus_decode_packet(AVCodecContext *avctx,
AVFrame *frame,
c->streams[0].silk_samplerate = get_silk_samplerate(pkt->config);
}
+ frame->sample_rate = 48000;
frame->nb_samples = coded_samples + delayed_samples;
/* no input or buffered data => nothing to do */
@@ -625,6 +626,19 @@ static int opus_decode_packet(AVCodecContext *avctx,
AVFrame *frame,
}
}
+ if (avctx->sample_rate != frame->sample_rate) {
+ AVFrameSideData *sd = av_frame_get_side_data(frame,
AV_FRAME_DATA_SKIP_SAMPLES);
+ if (sd && sd->size >= 10) {
+ uint32_t skip_samples = AV_RL32(sd->data);
+ uint32_t discard_padding = AV_RL32(sd->data + 4);
+ if (skip_samples || discard_padding) {
+ skip_samples = av_rescale(skip_samples, frame->sample_rate,
avctx->sample_rate);
+ discard_padding = av_rescale(discard_padding,
frame->sample_rate, avctx->sample_rate);
+ AV_WL32(sd->data, skip_samples);
+ AV_WL32(sd->data + 4, discard_padding);
+ }
+ }
+ }
frame->nb_samples = decoded_samples;
*got_frame_ptr = !!decoded_samples;
@@ -685,7 +699,8 @@ static av_cold int opus_decode_init(AVCodecContext *avctx)
int ret;
avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
- avctx->sample_rate = 48000;
+ if (!avctx->sample_rate)
+ avctx->sample_rate = 48000;
c->fdsp = avpriv_float_dsp_alloc(0);
if (!c->fdsp)
@@ -731,7 +746,7 @@ static av_cold int opus_decode_init(AVCodecContext *avctx)
av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
av_opt_set_chlayout(s->swr, "in_chlayout", &layout, 0);
av_opt_set_chlayout(s->swr, "out_chlayout", &layout, 0);
- av_opt_set_int(s->swr, "out_sample_rate", avctx->sample_rate, 0);
+ av_opt_set_int(s->swr, "out_sample_rate", 48000, 0);
av_opt_set_int(s->swr, "filter_size", 16, 0);
ret = ff_silk_init(avctx, &s->silk, s->output_channels);
--
2.54.0
>From 9deba493fa7d8f8c5739fdf1c4ce6631de9685bd Mon Sep 17 00:00:00 2001
From: James Almer <[email protected]>
Date: Wed, 4 Nov 2026 10:30:13 -0300
Subject: [PATCH 4/4] avformat/matroskadec: always use the coded sample rate
for intial padding
Demuxers should not add considerations for an eventual post-decoding state, and
only export coded parameters.
Ensures timestamps are properly calculated.
Before:
Output #0, framecrc, to 'pipe:':
Metadata:
encoder : Lavf63.18.100
Stream #0:0: Audio: pcm_s16le, 48000 Hz, stereo, s16, 1536 kb/s (default)
Metadata:
encoder : Lavc63.26.100 pcm_s16le
ENCODER_OPTIONS : --bitrate 96
0, -27, -27, 648, 2592, 0x372ff818
0, 621, 621, 960, 3840, 0x53577417
[...]
0, 94701, 94701, 960, 3840, 0x56c74d8c
0, 95661, 95661, 312, 1248, 0x67d480ff
video:0KiB audio:375KiB subtitle:0KiB other streams:0KiB global headers:0KiB
muxing overhead: unknown
size= 6KiB time=00:00:01.99
After:
Output #0, framecrc, to 'pipe:':
Metadata:
encoder : Lavf63.18.100
Stream #0:0: Audio: pcm_s16le, 48000 Hz, stereo, s16, 1536 kb/s (default)
Metadata:
encoder : Lavc63.26.100 pcm_s16le
ENCODER_OPTIONS : --bitrate 96
0, 0, 0, 648, 2592, 0x372ff818
0, 732, 732, 960, 3840, 0x53577417
[...]
0, 95321, 95321, 960, 3840, 0x56c74d8c
0, 95688, 95688, 312, 1248, 0x67d480ff
video:0KiB audio:375KiB subtitle:0KiB other streams:0KiB global headers:0KiB
muxing overhead: unknown
size= 6KiB time=00:00:02.00
Signed-off-by: James Almer <[email protected]>
---
libavformat/matroskadec.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c
index d36cc5c3452..706a0566bc4 100644
--- a/libavformat/matroskadec.c
+++ b/libavformat/matroskadec.c
@@ -2912,8 +2912,7 @@ static int mka_parse_audio(MatroskaTrack *track, AVStream
*st,
if (track->codec_delay > 0) {
par->initial_padding = av_rescale_q(track->codec_delay,
(AVRational){1, 1000000000},
- (AVRational){1, par->codec_id ==
AV_CODEC_ID_OPUS ?
- 48000 :
par->sample_rate});
+ (AVRational){1, par->sample_rate});
sti->skip_samples = par->initial_padding;
}
if (track->seek_preroll > 0) {
--
2.54.0
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]