avcodec/opus/enc: rescale packet duration when calculating discarded samples

Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
James Almer
2026-02-13 13:03:26 -03:00
parent 0aa3a6fe41
commit 7c877b2e80

View File

@@ -548,7 +548,7 @@ static int opus_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
const AVFrame *frame, int *got_packet_ptr)
{
OpusEncContext *s = avctx->priv_data;
int ret, frame_size, alloc_size = 0;
int ret, frame_size, discard_padding, alloc_size = 0;
if (frame) { /* Add new frame to queue */
if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
@@ -600,11 +600,13 @@ static int opus_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
/* Remove samples from queue and skip if needed */
ff_af_queue_remove(&s->afq, s->packet.frames*frame_size, &avpkt->pts, &avpkt->duration);
if (s->packet.frames*frame_size > avpkt->duration) {
discard_padding = s->packet.frames*frame_size - ff_samples_from_time_base(avctx, avpkt->duration);
if (discard_padding > 0) {
uint8_t *side = av_packet_new_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
if (!side)
return AVERROR(ENOMEM);
AV_WL32(&side[4], s->packet.frames*frame_size - avpkt->duration);
AV_WL32(&side[4], discard_padding);
}
*got_packet_ptr = 1;