PR #23081 opened by mkver
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/23081
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/23081.patch

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.


>From 5cb99c0633650bb2cccfef70b9b7d6f2685f63c6 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <[email protected]>
Date: Thu, 15 Apr 2021 03:56:36 +0200
Subject: [PATCH] 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 <[email protected]>
---
 libavcodec/libvorbisenc.c           | 14 ++++++--------
 libavcodec/vorbis_parser.c          | 29 +++++++++--------------------
 libavcodec/vorbis_parser_internal.h |  8 ++++++--
 3 files changed, 21 insertions(+), 30 deletions(-)

diff --git a/libavcodec/libvorbisenc.c b/libavcodec/libvorbisenc.c
index f8bb794a04..6d9d54c7bf 100644
--- a/libavcodec/libvorbisenc.c
+++ b/libavcodec/libvorbisenc.c
@@ -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;
 
@@ -226,7 +227,7 @@ static av_cold int 
libvorbis_get_priming_samples(vorbis_info *vi, AVCodecContext
         }
     }
 
-    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;
 error:
@@ -257,8 +258,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;
 }
 
@@ -320,10 +319,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;
     }
 
@@ -416,7 +414,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;
 
diff --git a/libavcodec/vorbis_parser.c b/libavcodec/vorbis_parser.c
index 88b81fcb53..8070004e61 100644
--- a/libavcodec/vorbis_parser.c
+++ b/libavcodec/vorbis_parser.c
@@ -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 */
diff --git a/libavcodec/vorbis_parser_internal.h 
b/libavcodec/vorbis_parser_internal.h
index 691a842385..556d8fdccb 100644
--- a/libavcodec/vorbis_parser_internal.h
+++ b/libavcodec/vorbis_parser_internal.h
@@ -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 */
-- 
2.52.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to