[FFmpeg-cvslog] avcodec/ff_init_vlc_sparse: use a local array for static vlcs avoiding malloc()

Michael Niedermayer git at videolan.org
Mon Jul 29 17:00:10 CEST 2013


ffmpeg | branch: master | Michael Niedermayer <michaelni at gmx.at> | Sat Jul 13 13:33:49 2013 +0200| [f80ccf074a3b20c5988c6c25b946fa1bc180b0cf] | committer: Michael Niedermayer

avcodec/ff_init_vlc_sparse: use a local array for static vlcs avoiding malloc()

Signed-off-by: Michael Niedermayer <michaelni at gmx.at>

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

 libavcodec/bitstream.c |   14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/libavcodec/bitstream.c b/libavcodec/bitstream.c
index aac32a5..859dc62 100644
--- a/libavcodec/bitstream.c
+++ b/libavcodec/bitstream.c
@@ -270,6 +270,7 @@ int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
 {
     VLCcode *buf;
     int i, j, ret;
+    VLCcode localbuf[1500]; // the maximum currently needed is 1296 by rv34
     void *state;
 
     vlc->bits = nb_bits;
@@ -281,17 +282,18 @@ int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
             }
         }
         av_assert0(!vlc->table_size);
+        av_assert0(nb_codes + 1 <= FF_ARRAY_ELEMS(localbuf));
+        buf = localbuf;
     } else {
         vlc->table           = NULL;
         vlc->table_allocated = 0;
         vlc->table_size      = 0;
-    }
 
-    av_dlog(NULL, "build table nb_codes=%d\n", nb_codes);
+        buf = av_malloc((nb_codes + 1) * sizeof(VLCcode));
+        if (!buf)
+            return AVERROR(ENOMEM);
+    }
 
-    buf = av_malloc((nb_codes + 1) * sizeof(VLCcode));
-    if (!buf)
-        return AVERROR(ENOMEM);
 
     av_assert0(symbols_size <= 2 || !symbols);
     j = 0;
@@ -329,7 +331,6 @@ int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
 
     ret = build_table(vlc, nb_bits, nb_codes, buf, flags);
 
-    av_free(buf);
     if (flags & INIT_VLC_USE_NEW_STATIC) {
         if(vlc->table_size != vlc->table_allocated)
             av_log(NULL, AV_LOG_ERROR, "needed %d had %d\n", vlc->table_size, vlc->table_allocated);
@@ -337,6 +338,7 @@ int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
         av_assert0(state == vlc);
         av_assert0(ret >= 0);
     } else {
+        av_free(buf);
         if (ret < 0) {
             av_freep(&vlc->table);
             return ret;



More information about the ffmpeg-cvslog mailing list