PR #25993 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25993
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25993.patch
The SILK resampler holds back some samples, which the decoder outputs with the
frame of a packet flagged `AV_PKT_FLAG_EOS` (since 5b2ab843de). Ogg sets that
flag on the last packet, but Matroska does not; it marks the end of the stream
with DiscardPadding on the last packet.
So for an 8 kHz SILK stream in Matroska, the held-back samples came out at the
final drain as a frame of their own, after the trimmed frame of the last
packet. That frame took the timestamp of the last packet ("non monotonically
increasing dts" warning in ffmpeg), and its samples come from the padding that
should have been discarded.
This treats a packet whose end is trimmed (discard_padding > 0) as ending the
sequence too, so the samples are drained into its frame before the trimming.
framecrc tail of a 3 s mono 8 kHz SILK file in Matroska, before:
```
0, 142752, 142752, 960, 3840, 0x32586fab
0, 143712, 143712, 312, 1248, 0xe2755ca2
0, 143712, 143712, 24, 96, 0x07f1585e
```
after (last frame now identical to the Ogg copy of the same packets; decoded
PCM of both files is identical, 144000 samples):
```
0, 142752, 142752, 960, 3840, 0x32586fab
0, 143712, 143712, 336, 1344, 0x537c9ca8
```
FATE: all opus, ogg and matroska tests and fate-source pass.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
From a200e74614b1dcfea187f53f1395fd560cb095ef Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Thu, 12 Nov 2026 12:57:30 -0800
Subject: [PATCH] avcodec/opus/dec: also drain held-back samples before end
trimming
The SILK resampler holds back some samples, which are output with the
frame of a packet flagged AV_PKT_FLAG_EOS. Demuxers other than Ogg do
not set that flag but mark the end of the stream by discarding padding
from the last packet, as Matroska does with DiscardPadding.
Those samples were then output by draining in a frame of their own,
after the trimmed frame of the last packet. They are from the padding,
and the frame took the timestamp of that last packet, making timestamps
go backwards.
Drain them into the frame of a packet whose end is trimmed too, as
nothing can follow it. An 8 kHz SILK stream in Matroska now decodes to
the same samples as its Ogg copy.
Signed-off-by: Baptiste Coudurier <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
libavcodec/opus/dec.c | 21 ++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)
diff --git a/libavcodec/opus/dec.c b/libavcodec/opus/dec.c
index 1f80ecd7600..abc3a3db077 100644
--- a/libavcodec/opus/dec.c
+++ b/libavcodec/opus/dec.c
@@ -41,6 +41,7 @@
#include "libavutil/ffmath.h"
#include "libavutil/float_dsp.h"
#include "libavutil/frame.h"
+#include "libavutil/intreadwrite.h"
#include "libavutil/mem.h"
#include "libavutil/mem_internal.h"
#include "libavutil/opt.h"
@@ -498,6 +499,23 @@ static int opus_drain_subpacket(OpusStreamContext *s, int
decoded_samples)
return ret < 0 ? ret : decoded_samples + ret;
}
+/**
+ * Whether a packet ends the sequence it belongs to: it is flagged so, or
+ * samples are to be discarded from the end of its output. Nothing can
+ * follow the latter, so the samples held back belong before that padding.
+ */
+static int opus_packet_ends_sequence(const AVPacket *pkt)
+{
+ const uint8_t *sd;
+ size_t sd_size;
+
+ if (pkt->flags & AV_PKT_FLAG_EOS)
+ return 1;
+
+ sd = av_packet_get_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, &sd_size);
+ return sd && sd_size >= 8 && AV_RL32(sd + 4);
+}
+
static av_cold void opus_free_streams(OpusContext *c)
{
for (int i = 0; i < c->p.nb_streams; i++) {
@@ -626,6 +644,7 @@ static int opus_decode_packet(AVCodecContext *avctx,
AVFrame *frame,
int coded_samples = 0;
int decoded_samples = INT_MAX;
int delayed_samples = 0;
+ int end_of_sequence = opus_packet_ends_sequence(avpkt);
const uint8_t *new_header;
size_t new_header_size;
int i, ret;
@@ -741,7 +760,7 @@ static int opus_decode_packet(AVCodecContext *avctx,
AVFrame *frame,
}
ret = opus_decode_subpacket(&c->streams[i], buf);
- if (ret >= 0 && buf && (avpkt->flags & AV_PKT_FLAG_EOS))
+ if (ret >= 0 && buf && end_of_sequence)
ret = opus_drain_subpacket(s, ret);
if (ret < 0)
return ret;
--
2.54.0
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]