[FFmpeg-devel] [PATCH] avcodec/opus/parser: set duration when complete frames are fed
James Almer
jamrial at gmail.com
Fri Jan 3 17:07:36 EET 2025
Fixes a regression since 873a34c129869e551cb7d3d2445e28c0ba079948.
Signed-off-by: James Almer <jamrial at gmail.com>
---
libavcodec/opus/parser.c | 38 +++++++++++++++++++++++++++++---------
1 file changed, 29 insertions(+), 9 deletions(-)
diff --git a/libavcodec/opus/parser.c b/libavcodec/opus/parser.c
index e16b0f72a2..be011485f6 100644
--- a/libavcodec/opus/parser.c
+++ b/libavcodec/opus/parser.c
@@ -78,6 +78,25 @@ static const uint8_t *parse_opus_ts_header(const uint8_t *start, int *payload_le
return buf + bytestream2_tell(&gb);
}
+static int set_duration(AVCodecParserContext *ctx, AVCodecContext *avctx,
+ const uint8_t *buf, int buf_size)
+{
+ OpusParserContext *s = ctx->priv_data;
+ ParseContext *pc = &s->pc;
+ int ret;
+
+ ret = ff_opus_parse_packet(&s->pkt, buf, buf_size, s->ctx.nb_streams > 1);
+ 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;
+
+ 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
@@ -137,14 +156,9 @@ static int opus_find_frame_end(AVCodecParserContext *ctx, AVCodecContext *avctx,
}
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);
- if (ret < 0) {
- av_log(avctx, AV_LOG_ERROR, "Error parsing Opus packet header.\n");
- pc->frame_start_found = 0;
+ ret = set_duration(ctx, avctx, payload, payload_len);
+ if (ret < 0)
return AVERROR_INVALIDDATA;
- }
-
- ctx->duration = s->pkt.frame_count * s->pkt.frame_duration;
}
if (s->ts_framing) {
@@ -174,9 +188,15 @@ static int opus_parse(AVCodecParserContext *ctx, AVCodecContext *avctx,
avctx->sample_rate = 48000;
- if (ctx->flags & PARSER_FLAG_COMPLETE_FRAMES)
+ if (ctx->flags & PARSER_FLAG_COMPLETE_FRAMES) {
next = buf_size;
- else {
+
+ if (set_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 &&
--
2.47.1
More information about the ffmpeg-devel
mailing list