avcodec/vorbis_parser: Improve returned error codes

av_vorbis_parse_init() doesn't return an error code which is a slight
problem in libvorbisenc.c. Fix this by making the internal
initialization function behind av_vorbis_parse_init() available. This
also avoids allocations and frees.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt
2026-05-30 13:10:07 +02:00
parent 80405d3ceb
commit b8c5376eb4
3 changed files with 21 additions and 30 deletions
+6 -8
View File
@@ -32,6 +32,7 @@
#include "encode.h"
#include "version.h"
#include "vorbis_parser.h"
#include "vorbis_parser_internal.h"
/* Number of samples the user should send in each call.
@@ -53,7 +54,7 @@ typedef struct LibvorbisEncContext {
int dsp_initialized; /**< vd has been initialized */
vorbis_comment vc; /**< VorbisComment info */
double iblock; /**< impulse block bias option */
AVVorbisParseContext *vp; /**< parse context to get durations */
AVVorbisParseContext vp; /**< parse context to get durations */
AudioFrameQueue afq; /**< frame queue for timestamps */
} LibvorbisEncContext;
@@ -224,7 +225,7 @@ static av_cold int libvorbis_get_priming_samples(vorbis_info *vi, AVCodecContext
ret = vorbis_error_to_averror(ret);
goto error;
}
avctx->initial_padding = av_vorbis_parse_frame(s->vp, op.packet, op.bytes);
avctx->initial_padding = av_vorbis_parse_frame(&s->vp, op.packet, op.bytes);
}
ret = 0;
@@ -256,8 +257,6 @@ static av_cold int libvorbis_encode_close(AVCodecContext *avctx)
av_fifo_freep2(&s->pkt_fifo);
ff_af_queue_close(&s->afq);
av_vorbis_parse_free(&s->vp);
return 0;
}
@@ -319,10 +318,9 @@ static av_cold int libvorbis_encode_init(AVCodecContext *avctx)
offset += header_code.bytes;
av_assert0(offset == avctx->extradata_size);
s->vp = av_vorbis_parse_init(avctx->extradata, avctx->extradata_size);
if (!s->vp) {
ret = ff_vorbis_parse_init(&s->vp, avctx->extradata, avctx->extradata_size);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "invalid extradata\n");
ret = AVERROR_UNKNOWN;
goto error;
}
@@ -415,7 +413,7 @@ static int libvorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
avpkt->pts = ff_samples_to_time_base(avctx, op.granulepos);
duration = av_vorbis_parse_frame(s->vp, avpkt->data, avpkt->size);
duration = av_vorbis_parse_frame(&s->vp, avpkt->data, avpkt->size);
if (duration > 0) {
int discard_padding;
+9 -20
View File
@@ -184,8 +184,8 @@ bad_header:
return ret;
}
static int vorbis_parse_init(AVVorbisParseContext *s,
const uint8_t *extradata, int extradata_size)
int ff_vorbis_parse_init(AVVorbisParseContext *s,
const uint8_t *extradata, int extradata_size)
{
const uint8_t *header_start[3];
int header_len[3];
@@ -291,7 +291,7 @@ AVVorbisParseContext *av_vorbis_parse_init(const uint8_t *extradata,
if (!s)
return NULL;
ret = vorbis_parse_init(s, extradata, extradata_size);
ret = ff_vorbis_parse_init(s, extradata, extradata_size);
if (ret < 0) {
av_vorbis_parse_free(&s);
return NULL;
@@ -302,24 +302,20 @@ AVVorbisParseContext *av_vorbis_parse_init(const uint8_t *extradata,
#if CONFIG_VORBIS_PARSER
typedef struct VorbisParseContext {
AVVorbisParseContext *vp;
} VorbisParseContext;
static int vorbis_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
const uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *buf, int buf_size)
{
VorbisParseContext *s = s1->priv_data;
AVVorbisParseContext *s = s1->priv_data;
int duration;
if (!s->vp && avctx->extradata && avctx->extradata_size) {
s->vp = av_vorbis_parse_init(avctx->extradata, avctx->extradata_size);
if (!s->valid_extradata && avctx->extradata && avctx->extradata_size) {
ff_vorbis_parse_init(s, avctx->extradata, avctx->extradata_size);
}
if (!s->vp)
if (!s->valid_extradata)
goto end;
if ((duration = av_vorbis_parse_frame(s->vp, buf, buf_size)) >= 0)
if ((duration = av_vorbis_parse_frame(s, buf, buf_size)) >= 0)
s1->duration = duration;
end:
@@ -330,16 +326,9 @@ end:
return buf_size;
}
static av_cold void vorbis_parser_close(AVCodecParserContext *ctx)
{
VorbisParseContext *s = ctx->priv_data;
av_vorbis_parse_free(&s->vp);
}
const FFCodecParser ff_vorbis_parser = {
PARSER_CODEC_LIST(AV_CODEC_ID_VORBIS),
.priv_data_size = sizeof(VorbisParseContext),
.priv_data_size = sizeof(AVVorbisParseContext),
.parse = vorbis_parse,
.close = vorbis_parser_close,
};
#endif /* CONFIG_VORBIS_PARSER */
+6 -2
View File
@@ -28,11 +28,12 @@
#ifndef AVCODEC_VORBIS_PARSER_INTERNAL_H
#define AVCODEC_VORBIS_PARSER_INTERNAL_H
#include "avcodec.h"
#include <stdint.h>
#include "vorbis_parser.h"
struct AVVorbisParseContext {
const AVClass *class;
const struct AVClass *class;
int extradata_parsed; ///< we have attempted to parse extradata
int valid_extradata; ///< extradata is valid, so we can calculate duration
int blocksize[2]; ///< short and long window sizes
@@ -43,4 +44,7 @@ struct AVVorbisParseContext {
int prev_mask; ///< bitmask used to get the previous mode flag in each packet
};
int ff_vorbis_parse_init(AVVorbisParseContext *s,
const uint8_t *extradata, int extradata_size);
#endif /* AVCODEC_VORBIS_PARSER_INTERNAL_H */