[FFmpeg-cvslog] mpc7: use av_fast_padded_malloc()

Justin Ruggles git at videolan.org
Fri Feb 3 04:15:58 CET 2012


ffmpeg | branch: master | Justin Ruggles <justin.ruggles at gmail.com> | Tue Jan 31 11:17:04 2012 -0500| [83ce51cc7d10c1589f07fda1b9f10fbc5aa93e77] | committer: Justin Ruggles

mpc7: use av_fast_padded_malloc()

Avoids doing malloc/free for each frame.
Also fixes valgrind errors due to use of uninitialized padding bytes.

Based on a patch by Reimar Döffinger <Reimar.Doeffinger at gmx.de>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=83ce51cc7d10c1589f07fda1b9f10fbc5aa93e77
---

 libavcodec/mpc7.c |   20 +++++++++++++-------
 1 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/libavcodec/mpc7.c b/libavcodec/mpc7.c
index ab2f543..8b3a1b9 100644
--- a/libavcodec/mpc7.c
+++ b/libavcodec/mpc7.c
@@ -203,7 +203,6 @@ static int mpc7_decode_frame(AVCodecContext * avctx, void *data,
     int buf_size;
     MPCContext *c = avctx->priv_data;
     GetBitContext gb;
-    uint8_t *bits;
     int i, ch;
     int mb = -1;
     Band *bands = c->bands;
@@ -235,11 +234,11 @@ static int mpc7_decode_frame(AVCodecContext * avctx, void *data,
         return ret;
     }
 
-    bits = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
-    if (!bits)
+    av_fast_padded_malloc(&c->bits, &c->buf_size, buf_size);
+    if (!c->bits)
         return AVERROR(ENOMEM);
-    c->dsp.bswap_buf((uint32_t *)bits, (const uint32_t *)buf, buf_size >> 2);
-    init_get_bits(&gb, bits, buf_size * 8);
+    c->dsp.bswap_buf((uint32_t *)c->bits, (const uint32_t *)buf, buf_size >> 2);
+    init_get_bits(&gb, c->bits, buf_size * 8);
     skip_bits_long(&gb, skip);
 
     /* read subband indexes */
@@ -296,8 +295,6 @@ static int mpc7_decode_frame(AVCodecContext * avctx, void *data,
 
     ff_mpc_dequantize_and_synth(c, mb, c->frame.data[0], 2);
 
-    av_free(bits);
-
     bits_used = get_bits_count(&gb);
     bits_avail = buf_size * 8;
     if (!last_frame && ((bits_avail < bits_used) || (bits_used + 32 <= bits_avail))) {
@@ -324,12 +321,21 @@ static void mpc7_decode_flush(AVCodecContext *avctx)
     c->frames_to_skip = 32;
 }
 
+static av_cold int mpc7_decode_close(AVCodecContext *avctx)
+{
+    MPCContext *c = avctx->priv_data;
+    av_freep(&c->bits);
+    c->buf_size = 0;
+    return 0;
+}
+
 AVCodec ff_mpc7_decoder = {
     .name           = "mpc7",
     .type           = AVMEDIA_TYPE_AUDIO,
     .id             = CODEC_ID_MUSEPACK7,
     .priv_data_size = sizeof(MPCContext),
     .init           = mpc7_decode_init,
+    .close          = mpc7_decode_close,
     .decode         = mpc7_decode_frame,
     .flush = mpc7_decode_flush,
     .capabilities   = CODEC_CAP_DR1,



More information about the ffmpeg-cvslog mailing list