avcodec/opus/parser: set duration when complete frames are fed

Fixes a regression since 873a34c129.

Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
James Almer 2025-01-03 11:58:44 -03:00
parent 4bf784c0e5
commit 37155d68ec

View file

@ -78,6 +78,21 @@ static const uint8_t *parse_opus_ts_header(const uint8_t *start, int *payload_le
return buf + bytestream2_tell(&gb);
}
static int set_frame_duration(AVCodecParserContext *ctx, AVCodecContext *avctx,
const uint8_t *buf, int buf_size)
{
OpusParserContext *s = ctx->priv_data;
if (ff_opus_parse_packet(&s->pkt, buf, buf_size, s->ctx.nb_streams > 1) < 0) {
av_log(avctx, AV_LOG_ERROR, "Error parsing Opus packet header.\n");
return AVERROR_INVALIDDATA;
}
ctx->duration = s->pkt.frame_count * s->pkt.frame_duration;
return 0;
}
/**
* Find the end of the current frame in the bitstream.
* @return the position of the first byte of the next frame, or -1
@ -126,25 +141,12 @@ static int opus_find_frame_end(AVCodecParserContext *ctx, AVCodecContext *avctx,
if (!s->ts_framing)
payload_len = buf_size;
if (avctx->extradata && !s->extradata_parsed) {
ret = ff_opus_parse_extradata(avctx, &s->ctx);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "Error parsing Ogg extradata.\n");
return AVERROR_INVALIDDATA;
}
av_freep(&s->ctx.channel_maps);
s->extradata_parsed = 1;
}
if (payload_len <= buf_size && (!s->ts_framing || start_found)) {
ret = ff_opus_parse_packet(&s->pkt, payload, payload_len, s->ctx.nb_streams > 1);
ret = set_frame_duration(ctx, avctx, payload, payload_len);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "Error parsing Opus packet header.\n");
pc->frame_start_found = 0;
return AVERROR_INVALIDDATA;
}
ctx->duration = s->pkt.frame_count * s->pkt.frame_duration;
}
if (s->ts_framing) {
@ -174,9 +176,26 @@ static int opus_parse(AVCodecParserContext *ctx, AVCodecContext *avctx,
avctx->sample_rate = 48000;
if (ctx->flags & PARSER_FLAG_COMPLETE_FRAMES)
if (avctx->extradata && !s->extradata_parsed) {
if (ff_opus_parse_extradata(avctx, &s->ctx) < 0) {
av_log(avctx, AV_LOG_ERROR, "Error parsing Ogg extradata.\n");
*poutbuf = NULL;
*poutbuf_size = 0;
return buf_size;
}
av_freep(&s->ctx.channel_maps);
s->extradata_parsed = 1;
}
if (ctx->flags & PARSER_FLAG_COMPLETE_FRAMES) {
next = buf_size;
else {
if (set_frame_duration(ctx, avctx, buf, buf_size) < 0) {
*poutbuf = NULL;
*poutbuf_size = 0;
return buf_size;
}
} else {
next = opus_find_frame_end(ctx, avctx, buf, buf_size, &header_len);
if (s->ts_framing && next != AVERROR_INVALIDDATA &&