diff mbox series

[FFmpeg-devel,29/30] avcodec/mpegaudiodec_template: Simplify creating mp3on4 child decoders

Message ID 20200915074000.102622-29-andreas.rheinhardt@gmail.com
State Withdrawn
Headers show
Series [FFmpeg-devel,01/30] avcodec/flashsvenc: Avoid allocation of buffer, fix memleak
Related show

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Andreas Rheinhardt Sept. 15, 2020, 7:39 a.m. UTC
Allocating the child codec contexts in one piece simplifies both
allocating as well as freeing.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/mpegaudiodec_template.c | 33 +++++++++++++-----------------
 1 file changed, 14 insertions(+), 19 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/mpegaudiodec_template.c b/libavcodec/mpegaudiodec_template.c
index d2b72497e1..8ed2f74eee 100644
--- a/libavcodec/mpegaudiodec_template.c
+++ b/libavcodec/mpegaudiodec_template.c
@@ -1827,7 +1827,7 @@  typedef struct MP3On4DecodeContext {
     int frames;                     ///< number of mp3 frames per block (number of mp3 decoder instances)
     int syncword;                   ///< syncword patch
     const uint8_t *coff;            ///< channel offsets in output buffer
-    MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
+    MPADecodeContext *mp3decctx;    ///< MPADecodeContext for every decoder instance
 } MP3On4DecodeContext;
 
 #include "mpeg4audio.h"
@@ -1864,10 +1864,8 @@  static const int16_t chan_layout[8] = {
 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
 {
     MP3On4DecodeContext *s = avctx->priv_data;
-    int i;
 
-    for (i = 0; i < s->frames; i++)
-        av_freep(&s->mp3decctx[i]);
+    av_freep(&s->mp3decctx);
 
     return 0;
 }
@@ -1905,12 +1903,12 @@  static av_cold int decode_init_mp3on4(AVCodecContext * avctx)
      * decode_init() does not have to be changed.
      * Other decoders will be initialized here copying data from the first context
      */
-    // Allocate zeroed memory for the first decoder context
-    s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
-    if (!s->mp3decctx[0])
+    // Allocate zeroed memory for the decoder contexts
+    s->mp3decctx = av_mallocz_array(sizeof(MPADecodeContext), s->frames);
+    if (!s->mp3decctx)
         goto alloc_fail;
     // Put decoder context in place to make init_decode() happy
-    avctx->priv_data = s->mp3decctx[0];
+    avctx->priv_data = s->mp3decctx;
     ret = decode_init(avctx);
     // Restore mp3on4 context pointer
     avctx->priv_data = s;
@@ -1918,19 +1916,16 @@  static av_cold int decode_init_mp3on4(AVCodecContext * avctx)
         decode_close_mp3on4(avctx);
         return ret;
     }
-    s->mp3decctx[0]->adu_mode = 1; // Set adu mode
+    s->mp3decctx[0].adu_mode = 1; // Set adu mode
 
     /* Create a separate codec/context for each frame (first is already ok).
      * Each frame is 1 or 2 channels - up to 5 frames allowed
      */
     for (i = 1; i < s->frames; i++) {
-        s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
-        if (!s->mp3decctx[i])
-            goto alloc_fail;
-        s->mp3decctx[i]->adu_mode = 1;
-        s->mp3decctx[i]->avctx = avctx;
-        s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
-        s->mp3decctx[i]->butterflies_float = s->mp3decctx[0]->butterflies_float;
+        s->mp3decctx[i].adu_mode = 1;
+        s->mp3decctx[i].avctx    = avctx;
+        s->mp3decctx[i].mpadsp   = s->mp3decctx[0].mpadsp;
+        s->mp3decctx[i].butterflies_float = s->mp3decctx[0].butterflies_float;
     }
 
     return 0;
@@ -1946,7 +1941,7 @@  static void flush_mp3on4(AVCodecContext *avctx)
     MP3On4DecodeContext *s = avctx->priv_data;
 
     for (i = 0; i < s->frames; i++)
-        mp_flush(s->mp3decctx[i]);
+        mp_flush(&s->mp3decctx[i]);
 }
 
 
@@ -1980,7 +1975,7 @@  static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
     for (fr = 0; fr < s->frames; fr++) {
         fsize = AV_RB16(buf) >> 4;
         fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
-        m     = s->mp3decctx[fr];
+        m     = &s->mp3decctx[fr];
         av_assert1(m);
 
         if (fsize < HEADER_SIZE) {
@@ -2027,7 +2022,7 @@  static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
     }
 
     /* update codec info */
-    avctx->sample_rate = s->mp3decctx[0]->sample_rate;
+    avctx->sample_rate = s->mp3decctx[0].sample_rate;
 
     frame->nb_samples = out_size / (avctx->channels * sizeof(OUT_INT));
     *got_frame_ptr    = 1;