changeset 2370:26560d4fdb1f libavcodec

Memory leak fix patch by (Burkhard Plaum <plaum >at< ipf.uni-stuttgart )dot( de>)
author michael
date Sat, 27 Nov 2004 18:10:06 +0000
parents ce47b1d51cb1
children 949f84ce470a
files 4xm.c asv1.c avcodec.h common.c common.h dv.c h261.c h263.c h264.c huffyuv.c mjpeg.c mpeg12.c mpegaudiodec.c mpegvideo.c mpegvideo.h msmpeg4.c rv10.c svq1.c utils.c vp3.c wmadec.c
diffstat 21 files changed, 193 insertions(+), 138 deletions(-) [+]
line wrap: on
line diff
--- a/4xm.c	Sat Nov 27 03:54:18 2004 +0000
+++ b/4xm.c	Sat Nov 27 18:10:06 2004 +0000
@@ -220,17 +220,12 @@
 }
 
 static void init_vlcs(FourXContext *f){
-    static int done = 0;
     int i;
 
-    if (!done) {
-        done = 1;
-
-        for(i=0; i<4; i++){
-            init_vlc(&block_type_vlc[i], BLOCK_TYPE_VLC_BITS, 7, 
-                     &block_type_tab[i][0][1], 2, 1,
-                     &block_type_tab[i][0][0], 2, 1);
-        }
+    for(i=0; i<4; i++){
+        init_vlc(&block_type_vlc[i], BLOCK_TYPE_VLC_BITS, 7, 
+                 &block_type_tab[i][0][1], 2, 1,
+                 &block_type_tab[i][0][0], 2, 1, 1);
     }
 }
 
@@ -544,7 +539,7 @@
     
     init_vlc(&f->pre_vlc, ACDC_VLC_BITS, 257, 
              len_tab , 1, 1,
-             bits_tab, 4, 4);
+             bits_tab, 4, 4, 0);
              
     return ptr;
 }
--- a/asv1.c	Sat Nov 27 03:54:18 2004 +0000
+++ b/asv1.c	Sat Nov 27 18:10:06 2004 +0000
@@ -137,19 +137,19 @@
 
         init_vlc(&ccp_vlc, VLC_BITS, 17, 
                  &ccp_tab[0][1], 2, 1,
-                 &ccp_tab[0][0], 2, 1);
+                 &ccp_tab[0][0], 2, 1, 1);
         init_vlc(&dc_ccp_vlc, VLC_BITS, 8, 
                  &dc_ccp_tab[0][1], 2, 1,
-                 &dc_ccp_tab[0][0], 2, 1);
+                 &dc_ccp_tab[0][0], 2, 1, 1);
         init_vlc(&ac_ccp_vlc, VLC_BITS, 16, 
                  &ac_ccp_tab[0][1], 2, 1,
-                 &ac_ccp_tab[0][0], 2, 1);
+                 &ac_ccp_tab[0][0], 2, 1, 1);
         init_vlc(&level_vlc,  VLC_BITS, 7, 
                  &level_tab[0][1], 2, 1,
-                 &level_tab[0][0], 2, 1);
+                 &level_tab[0][0], 2, 1, 1);
         init_vlc(&asv2_level_vlc, ASV2_LEVEL_VLC_BITS, 63, 
                  &asv2_level_tab[0][1], 2, 1,
-                 &asv2_level_tab[0][0], 2, 1);
+                 &asv2_level_tab[0][0], 2, 1, 1);
     }
 }
 
--- a/avcodec.h	Sat Nov 27 03:54:18 2004 +0000
+++ b/avcodec.h	Sat Nov 27 18:10:06 2004 +0000
@@ -2200,6 +2200,7 @@
 /* call av_free_static to release all staticaly allocated tables */
 void av_free_static(void);
 void *av_mallocz_static(unsigned int size);
+void *av_realloc_static(void *ptr, unsigned int size);
 
 /* add by bero : in adx.c */
 int is_adx(const unsigned char *buf,size_t bufsize);
--- a/common.c	Sat Nov 27 03:54:18 2004 +0000
+++ b/common.c	Sat Nov 27 18:10:06 2004 +0000
@@ -126,15 +126,19 @@
 }
 
 
-static int alloc_table(VLC *vlc, int size)
+static int alloc_table(VLC *vlc, int size, int use_static)
 {
     int index;
     index = vlc->table_size;
     vlc->table_size += size;
     if (vlc->table_size > vlc->table_allocated) {
         vlc->table_allocated += (1 << vlc->bits);
-        vlc->table = av_realloc(vlc->table,
-                                sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
+        if(use_static)
+            vlc->table = av_realloc_static(vlc->table,
+                                           sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
+        else
+            vlc->table = av_realloc(vlc->table,
+                                    sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
         if (!vlc->table)
             return -1;
     }
@@ -145,14 +149,14 @@
                        int nb_codes,
                        const void *bits, int bits_wrap, int bits_size,
                        const void *codes, int codes_wrap, int codes_size,
-                       uint32_t code_prefix, int n_prefix)
+                       uint32_t code_prefix, int n_prefix, int use_static)
 {
     int i, j, k, n, table_size, table_index, nb, n1, index;
     uint32_t code;
     VLC_TYPE (*table)[2];
 
     table_size = 1 << table_nb_bits;
-    table_index = alloc_table(vlc, table_size);
+    table_index = alloc_table(vlc, table_size, use_static);
 #ifdef DEBUG_VLC
     printf("new table index=%d size=%d code_prefix=%x n=%d\n",
            table_index, table_size, code_prefix, n_prefix);
@@ -225,7 +229,7 @@
                                 bits, bits_wrap, bits_size,
                                 codes, codes_wrap, codes_size,
                                 (code_prefix << table_nb_bits) | i,
-                                n_prefix + table_nb_bits);
+                                n_prefix + table_nb_bits, use_static);
             if (index < 0)
                 return -1;
             /* note: realloc has been done, so reload tables */
@@ -257,15 +261,27 @@
 
    'wrap' and 'size' allows to use any memory configuration and types
    (byte/word/long) to store the 'bits' and 'codes' tables.  
+
+   'use_static' should be set to 1 for tables, which should be freed
+   with av_free_static(), 0 if free_vlc() will be used.
 */
 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
              const void *bits, int bits_wrap, int bits_size,
-             const void *codes, int codes_wrap, int codes_size)
+             const void *codes, int codes_wrap, int codes_size,
+             int use_static)
 {
     vlc->bits = nb_bits;
-    vlc->table = NULL;
-    vlc->table_allocated = 0;
-    vlc->table_size = 0;
+    if(!use_static) {
+        vlc->table = NULL;
+        vlc->table_allocated = 0;
+        vlc->table_size = 0;
+    } else {
+        /* Static tables are initially always NULL, return
+           if vlc->table != NULL to avoid double allocation */
+        if(vlc->table)
+            return 0;
+    }
+
 #ifdef DEBUG_VLC
     printf("build table nb_codes=%d\n", nb_codes);
 #endif
@@ -273,7 +289,7 @@
     if (build_table(vlc, nb_bits, nb_codes,
                     bits, bits_wrap, bits_size,
                     codes, codes_wrap, codes_size,
-                    0, 0) < 0) {
+                    0, 0, use_static) < 0) {
         av_free(vlc->table);
         return -1;
     }
--- a/common.h	Sat Nov 27 03:54:18 2004 +0000
+++ b/common.h	Sat Nov 27 18:10:06 2004 +0000
@@ -939,7 +939,8 @@
 void align_get_bits(GetBitContext *s);
 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
              const void *bits, int bits_wrap, int bits_size,
-             const void *codes, int codes_wrap, int codes_size);
+             const void *codes, int codes_wrap, int codes_size,
+             int use_static);
 void free_vlc(VLC *vlc);
 
 /**
--- a/dv.c	Sat Nov 27 03:54:18 2004 +0000
+++ b/dv.c	Sat Nov 27 18:10:06 2004 +0000
@@ -144,7 +144,7 @@
         /* NOTE: as a trick, we use the fact the no codes are unused
            to accelerate the parsing of partial codes */
         init_vlc(&dv_vlc, TEX_VLC_BITS, j, 
-                 new_dv_vlc_len, 1, 1, new_dv_vlc_bits, 2, 2);
+                 new_dv_vlc_len, 1, 1, new_dv_vlc_bits, 2, 2, 0);
 
         dv_rl_vlc = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM));
 	if (!dv_rl_vlc) {
--- a/h261.c	Sat Nov 27 03:54:18 2004 +0000
+++ b/h261.c	Sat Nov 27 18:10:06 2004 +0000
@@ -282,7 +282,7 @@
     
     if (!done) {
         done = 1;
-        init_rl(&h261_rl_tcoeff);
+        init_rl(&h261_rl_tcoeff, 1);
     }
 
     s->min_qcoeff= -127;
@@ -372,7 +372,7 @@
 static VLC h261_mv_vlc;
 static VLC h261_cbp_vlc;
 
-void init_vlc_rl(RLTable *rl);
+void init_vlc_rl(RLTable *rl, int use_static);
 
 static void h261_decode_init_vlc(H261Context *h){
     static int done = 0;
@@ -381,18 +381,18 @@
         done = 1;
         init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
                  h261_mba_bits, 1, 1,
-                 h261_mba_code, 1, 1);
+                 h261_mba_code, 1, 1, 1);
         init_vlc(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
                  h261_mtype_bits, 1, 1,
-                 h261_mtype_code, 1, 1);
+                 h261_mtype_code, 1, 1, 1);
         init_vlc(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
                  &h261_mv_tab[0][1], 2, 1,
-                 &h261_mv_tab[0][0], 2, 1);
+                 &h261_mv_tab[0][0], 2, 1, 1);
         init_vlc(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
                  &h261_cbp_tab[0][1], 2, 1,
-                 &h261_cbp_tab[0][0], 2, 1);
-        init_rl(&h261_rl_tcoeff);
-        init_vlc_rl(&h261_rl_tcoeff);
+                 &h261_cbp_tab[0][0], 2, 1, 1);
+        init_rl(&h261_rl_tcoeff, 1);
+        init_vlc_rl(&h261_rl_tcoeff, 1);
     }
 }
 
--- a/h263.c	Sat Nov 27 03:54:18 2004 +0000
+++ b/h263.c	Sat Nov 27 18:10:06 2004 +0000
@@ -1931,9 +1931,9 @@
 
         init_uni_dc_tab();
 
-        init_rl(&rl_inter);
-        init_rl(&rl_intra);
-        init_rl(&rl_intra_aic);
+        init_rl(&rl_inter, 1);
+        init_rl(&rl_intra, 1);
+        init_rl(&rl_intra_aic, 1);
         
         init_uni_mpeg4_rl_tab(&rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
         init_uni_mpeg4_rl_tab(&rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
@@ -2797,13 +2797,17 @@
 static VLC h263_mbtype_b_vlc;
 static VLC cbpc_b_vlc;
 
-void init_vlc_rl(RLTable *rl)
+void init_vlc_rl(RLTable *rl, int use_static)
 {
     int i, q;
-    
+ 
+    /* Return if static table is already initialized */
+    if(use_static && rl->rl_vlc[0])
+        return;    
+
     init_vlc(&rl->vlc, 9, rl->n + 1, 
              &rl->table_vlc[0][1], 4, 2,
-             &rl->table_vlc[0][0], 4, 2);
+             &rl->table_vlc[0][0], 4, 2, use_static);
 
     
     for(q=0; q<32; q++){
@@ -2814,8 +2818,10 @@
             qmul=1;
             qadd=0;
         }
-        
-        rl->rl_vlc[q]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
+        if(use_static)        
+            rl->rl_vlc[q]= av_mallocz_static(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
+        else
+            rl->rl_vlc[q]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
         for(i=0; i<rl->vlc.table_size; i++){
             int code= rl->vlc.table[i][0];
             int len = rl->vlc.table[i][1];
@@ -2856,44 +2862,44 @@
 
         init_vlc(&intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 9, 
                  intra_MCBPC_bits, 1, 1,
-                 intra_MCBPC_code, 1, 1);
+                 intra_MCBPC_code, 1, 1, 1);
         init_vlc(&inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 28, 
                  inter_MCBPC_bits, 1, 1,
-                 inter_MCBPC_code, 1, 1);
+                 inter_MCBPC_code, 1, 1, 1);
         init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16,
                  &cbpy_tab[0][1], 2, 1,
-                 &cbpy_tab[0][0], 2, 1);
+                 &cbpy_tab[0][0], 2, 1, 1);
         init_vlc(&mv_vlc, MV_VLC_BITS, 33,
                  &mvtab[0][1], 2, 1,
-                 &mvtab[0][0], 2, 1);
-        init_rl(&rl_inter);
-        init_rl(&rl_intra);
-        init_rl(&rvlc_rl_inter);
-        init_rl(&rvlc_rl_intra);
-        init_rl(&rl_intra_aic);
-        init_vlc_rl(&rl_inter);
-        init_vlc_rl(&rl_intra);
-        init_vlc_rl(&rvlc_rl_inter);
-        init_vlc_rl(&rvlc_rl_intra);
-        init_vlc_rl(&rl_intra_aic);
+                 &mvtab[0][0], 2, 1, 1);
+        init_rl(&rl_inter, 1);
+        init_rl(&rl_intra, 1);
+        init_rl(&rvlc_rl_inter, 1);
+        init_rl(&rvlc_rl_intra, 1);
+        init_rl(&rl_intra_aic, 1);
+        init_vlc_rl(&rl_inter, 1);
+        init_vlc_rl(&rl_intra, 1);
+        init_vlc_rl(&rvlc_rl_inter, 1);
+        init_vlc_rl(&rvlc_rl_intra, 1);
+        init_vlc_rl(&rl_intra_aic, 1);
         init_vlc(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
                  &DCtab_lum[0][1], 2, 1,
-                 &DCtab_lum[0][0], 2, 1);
+                 &DCtab_lum[0][0], 2, 1, 1);
         init_vlc(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
                  &DCtab_chrom[0][1], 2, 1,
-                 &DCtab_chrom[0][0], 2, 1);
+                 &DCtab_chrom[0][0], 2, 1, 1);
         init_vlc(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
                  &sprite_trajectory_tab[0][1], 4, 2,
-                 &sprite_trajectory_tab[0][0], 4, 2);
+                 &sprite_trajectory_tab[0][0], 4, 2, 1);
         init_vlc(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
                  &mb_type_b_tab[0][1], 2, 1,
-                 &mb_type_b_tab[0][0], 2, 1);
+                 &mb_type_b_tab[0][0], 2, 1, 1);
         init_vlc(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15,
                  &h263_mbtype_b_tab[0][1], 2, 1,
-                 &h263_mbtype_b_tab[0][0], 2, 1);
+                 &h263_mbtype_b_tab[0][0], 2, 1, 1);
         init_vlc(&cbpc_b_vlc, CBPC_B_VLC_BITS, 4,
                  &cbpc_b_tab[0][1], 2, 1,
-                 &cbpc_b_tab[0][0], 2, 1);
+                 &cbpc_b_tab[0][0], 2, 1, 1);
     }
 }
 
--- a/h264.c	Sat Nov 27 03:54:18 2004 +0000
+++ b/h264.c	Sat Nov 27 18:10:06 2004 +0000
@@ -2091,33 +2091,33 @@
 
         init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5, 
                  &chroma_dc_coeff_token_len [0], 1, 1,
-                 &chroma_dc_coeff_token_bits[0], 1, 1);
+                 &chroma_dc_coeff_token_bits[0], 1, 1, 1);
 
         for(i=0; i<4; i++){
             init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17, 
                      &coeff_token_len [i][0], 1, 1,
-                     &coeff_token_bits[i][0], 1, 1);
+                     &coeff_token_bits[i][0], 1, 1, 1);
         }
 
         for(i=0; i<3; i++){
             init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
                      &chroma_dc_total_zeros_len [i][0], 1, 1,
-                     &chroma_dc_total_zeros_bits[i][0], 1, 1);
+                     &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
         }
         for(i=0; i<15; i++){
             init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16, 
                      &total_zeros_len [i][0], 1, 1,
-                     &total_zeros_bits[i][0], 1, 1);
+                     &total_zeros_bits[i][0], 1, 1, 1);
         }
 
         for(i=0; i<6; i++){
             init_vlc(&run_vlc[i], RUN_VLC_BITS, 7, 
                      &run_len [i][0], 1, 1,
-                     &run_bits[i][0], 1, 1);
+                     &run_bits[i][0], 1, 1, 1);
         }
         init_vlc(&run7_vlc, RUN7_VLC_BITS, 16, 
                  &run_len [6][0], 1, 1,
-                 &run_bits[6][0], 1, 1);
+                 &run_bits[6][0], 1, 1, 1);
     }
 }
 
--- a/huffyuv.c	Sat Nov 27 03:54:18 2004 +0000
+++ b/huffyuv.c	Sat Nov 27 18:10:06 2004 +0000
@@ -308,7 +308,7 @@
 }
 #endif
         free_vlc(&s->vlc[i]);
-        init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4);
+        init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4, 0);
     }
     
     return (get_bits_count(&gb)+7)/8;
@@ -336,7 +336,7 @@
     
     for(i=0; i<3; i++){
         free_vlc(&s->vlc[i]);
-        init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4);
+        init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4, 0);
     }
     
     return 0;
--- a/mjpeg.c	Sat Nov 27 03:54:18 2004 +0000
+++ b/mjpeg.c	Sat Nov 27 18:10:06 2004 +0000
@@ -844,7 +844,7 @@
 static int mjpeg_decode_dht(MJpegDecodeContext *s);
 
 static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table, 
-                      int nb_codes)
+                      int nb_codes, int use_static)
 {
     uint8_t huff_size[256];
     uint16_t huff_code[256];
@@ -852,7 +852,7 @@
     memset(huff_size, 0, sizeof(huff_size));
     build_huffman_codes(huff_size, huff_code, bits_table, val_table);
     
-    return init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2);
+    return init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2, use_static);
 }
 
 static int mjpeg_decode_init(AVCodecContext *avctx)
@@ -882,10 +882,10 @@
     s->first_picture = 1;
     s->org_height = avctx->coded_height;
     
-    build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12);
-    build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12);
-    build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251);
-    build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251);
+    build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12, 0);
+    build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12, 0);
+    build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251, 0);
+    build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251, 0);
 
     if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
     {
@@ -1036,7 +1036,7 @@
         free_vlc(&s->vlcs[class][index]);
         dprintf("class=%d index=%d nb_codes=%d\n",
                class, index, code_max + 1);
-        if(build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1) < 0){
+        if(build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1, 0) < 0){
             return -1;
         }
     }
--- a/mpeg12.c	Sat Nov 27 03:54:18 2004 +0000
+++ b/mpeg12.c	Sat Nov 27 18:10:06 2004 +0000
@@ -104,16 +104,19 @@
 static int8_t mpeg1_max_level[2][64];
 #endif //CONFIG_ENCODERS
 
-static void init_2d_vlc_rl(RLTable *rl)
+static void init_2d_vlc_rl(RLTable *rl, int use_static)
 {
     int i;
     
     init_vlc(&rl->vlc, TEX_VLC_BITS, rl->n + 2, 
              &rl->table_vlc[0][1], 4, 2,
-             &rl->table_vlc[0][0], 4, 2);
+             &rl->table_vlc[0][0], 4, 2, use_static);
 
-    
-    rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
+    if(use_static)    
+        rl->rl_vlc[0]= av_mallocz_static(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
+    else
+        rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
+
     for(i=0; i<rl->vlc.table_size; i++){
         int code= rl->vlc.table[i][0];
         int len = rl->vlc.table[i][1];
@@ -763,7 +766,7 @@
 	int i;
 
         done=1;
-        init_rl(&rl_mpeg1);
+        init_rl(&rl_mpeg1, 1);
 
 	for(i=0; i<64; i++)
 	{
@@ -991,31 +994,31 @@
 
         init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12, 
                  vlc_dc_lum_bits, 1, 1,
-                 vlc_dc_lum_code, 2, 2);
+                 vlc_dc_lum_code, 2, 2, 1);
         init_vlc(&dc_chroma_vlc,  DC_VLC_BITS, 12, 
                  vlc_dc_chroma_bits, 1, 1,
-                 vlc_dc_chroma_code, 2, 2);
+                 vlc_dc_chroma_code, 2, 2, 1);
         init_vlc(&mv_vlc, MV_VLC_BITS, 17, 
                  &mbMotionVectorTable[0][1], 2, 1,
-                 &mbMotionVectorTable[0][0], 2, 1);
+                 &mbMotionVectorTable[0][0], 2, 1, 1);
         init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 36, 
                  &mbAddrIncrTable[0][1], 2, 1,
-                 &mbAddrIncrTable[0][0], 2, 1);
+                 &mbAddrIncrTable[0][0], 2, 1, 1);
         init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
                  &mbPatTable[0][1], 2, 1,
-                 &mbPatTable[0][0], 2, 1);
+                 &mbPatTable[0][0], 2, 1, 1);
         
         init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7, 
                  &table_mb_ptype[0][1], 2, 1,
-                 &table_mb_ptype[0][0], 2, 1);
+                 &table_mb_ptype[0][0], 2, 1, 1);
         init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11, 
                  &table_mb_btype[0][1], 2, 1,
-                 &table_mb_btype[0][0], 2, 1);
-        init_rl(&rl_mpeg1);
-        init_rl(&rl_mpeg2);
+                 &table_mb_btype[0][0], 2, 1, 1);
+        init_rl(&rl_mpeg1, 1);
+        init_rl(&rl_mpeg2, 1);
 
-        init_2d_vlc_rl(&rl_mpeg1);
-        init_2d_vlc_rl(&rl_mpeg2);
+        init_2d_vlc_rl(&rl_mpeg1, 1);
+        init_2d_vlc_rl(&rl_mpeg2, 1);
     }
 }
 
--- a/mpegaudiodec.c	Sat Nov 27 03:54:18 2004 +0000
+++ b/mpegaudiodec.c	Sat Nov 27 18:10:06 2004 +0000
@@ -375,7 +375,7 @@
             n = xsize * xsize;
             /* XXX: fail test */
             init_vlc(&huff_vlc[i], 8, n, 
-                     h->bits, 1, 1, h->codes, 2, 2);
+                     h->bits, 1, 1, h->codes, 2, 2, 1);
             
             code_table = av_mallocz(n);
             j = 0;
@@ -387,7 +387,7 @@
         }
         for(i=0;i<2;i++) {
             init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16, 
-                     mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1);
+                     mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1, 1);
         }
 
         for(i=0;i<9;i++) {
--- a/mpegvideo.c	Sat Nov 27 03:54:18 2004 +0000
+++ b/mpegvideo.c	Sat Nov 27 18:10:06 2004 +0000
@@ -1267,12 +1267,16 @@
 
 #endif //CONFIG_ENCODERS
 
-void init_rl(RLTable *rl)
+void init_rl(RLTable *rl, int use_static)
 {
     int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
     uint8_t index_run[MAX_RUN+1];
     int last, run, level, start, end, i;
 
+    /* If table is static, we can quit if rl->max_level[0] is not NULL */
+    if(use_static && rl->max_level[0])
+        return;
+
     /* compute max_level[], max_run[] and index_run[] */
     for(last=0;last<2;last++) {
         if (last == 0) {
@@ -1296,11 +1300,20 @@
             if (run > max_run[level])
                 max_run[level] = run;
         }
-        rl->max_level[last] = av_malloc(MAX_RUN + 1);
+        if(use_static)
+            rl->max_level[last] = av_mallocz_static(MAX_RUN + 1);
+        else
+            rl->max_level[last] = av_malloc(MAX_RUN + 1);
         memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
-        rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
+        if(use_static)
+            rl->max_run[last] = av_mallocz_static(MAX_LEVEL + 1);
+        else
+            rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
         memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
-        rl->index_run[last] = av_malloc(MAX_RUN + 1);
+        if(use_static)
+            rl->index_run[last] = av_mallocz_static(MAX_RUN + 1);
+        else
+            rl->index_run[last] = av_malloc(MAX_RUN + 1);
         memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
     }
 }
--- a/mpegvideo.h	Sat Nov 27 03:54:18 2004 +0000
+++ b/mpegvideo.h	Sat Nov 27 18:10:06 2004 +0000
@@ -841,8 +841,8 @@
     RL_VLC_ELEM *rl_vlc[32];       ///< decoding only 
 } RLTable;
 
-void init_rl(RLTable *rl);
-void init_vlc_rl(RLTable *rl);
+void init_rl(RLTable *rl, int use_static);
+void init_vlc_rl(RLTable *rl, int use_static);
 
 static inline int get_rl_index(const RLTable *rl, int last, int run, int level)
 {
--- a/msmpeg4.c	Sat Nov 27 03:54:18 2004 +0000
+++ b/msmpeg4.c	Sat Nov 27 18:10:06 2004 +0000
@@ -239,7 +239,7 @@
         init_mv_table(&mv_tables[0]);
         init_mv_table(&mv_tables[1]);
         for(i=0;i<NB_RL_TABLES;i++)
-            init_rl(&rl_table[i]);
+            init_rl(&rl_table[i], 1);
 
         for(i=0; i<NB_RL_TABLES; i++){
             int level;
@@ -1111,69 +1111,69 @@
         done = 1;
 
         for(i=0;i<NB_RL_TABLES;i++) {
-            init_rl(&rl_table[i]);
-            init_vlc_rl(&rl_table[i]);
+            init_rl(&rl_table[i], 1);
+            init_vlc_rl(&rl_table[i], 1);
         }
         for(i=0;i<2;i++) {
             mv = &mv_tables[i];
             init_vlc(&mv->vlc, MV_VLC_BITS, mv->n + 1, 
                      mv->table_mv_bits, 1, 1,
-                     mv->table_mv_code, 2, 2);
+                     mv->table_mv_code, 2, 2, 1);
         }
 
         init_vlc(&dc_lum_vlc[0], DC_VLC_BITS, 120, 
                  &table0_dc_lum[0][1], 8, 4,
-                 &table0_dc_lum[0][0], 8, 4);
+                 &table0_dc_lum[0][0], 8, 4, 1);
         init_vlc(&dc_chroma_vlc[0], DC_VLC_BITS, 120, 
                  &table0_dc_chroma[0][1], 8, 4,
-                 &table0_dc_chroma[0][0], 8, 4);
+                 &table0_dc_chroma[0][0], 8, 4, 1);
         init_vlc(&dc_lum_vlc[1], DC_VLC_BITS, 120, 
                  &table1_dc_lum[0][1], 8, 4,
-                 &table1_dc_lum[0][0], 8, 4);
+                 &table1_dc_lum[0][0], 8, 4, 1);
         init_vlc(&dc_chroma_vlc[1], DC_VLC_BITS, 120, 
                  &table1_dc_chroma[0][1], 8, 4,
-                 &table1_dc_chroma[0][0], 8, 4);
+                 &table1_dc_chroma[0][0], 8, 4, 1);
     
         init_vlc(&v2_dc_lum_vlc, DC_VLC_BITS, 512, 
                  &v2_dc_lum_table[0][1], 8, 4,
-                 &v2_dc_lum_table[0][0], 8, 4);
+                 &v2_dc_lum_table[0][0], 8, 4, 1);
         init_vlc(&v2_dc_chroma_vlc, DC_VLC_BITS, 512, 
                  &v2_dc_chroma_table[0][1], 8, 4,
-                 &v2_dc_chroma_table[0][0], 8, 4);
+                 &v2_dc_chroma_table[0][0], 8, 4, 1);
     
         init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16,
                  &cbpy_tab[0][1], 2, 1,
-                 &cbpy_tab[0][0], 2, 1);
+                 &cbpy_tab[0][0], 2, 1, 1);
         init_vlc(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
                  &v2_intra_cbpc[0][1], 2, 1,
-                 &v2_intra_cbpc[0][0], 2, 1);
+                 &v2_intra_cbpc[0][0], 2, 1, 1);
         init_vlc(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
                  &v2_mb_type[0][1], 2, 1,
-                 &v2_mb_type[0][0], 2, 1);
+                 &v2_mb_type[0][0], 2, 1, 1);
         init_vlc(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
                  &mvtab[0][1], 2, 1,
-                 &mvtab[0][0], 2, 1);
+                 &mvtab[0][0], 2, 1, 1);
 
         for(i=0; i<4; i++){
             init_vlc(&mb_non_intra_vlc[i], MB_NON_INTRA_VLC_BITS, 128, 
                      &wmv2_inter_table[i][0][1], 8, 4,
-                     &wmv2_inter_table[i][0][0], 8, 4); //FIXME name?
+                     &wmv2_inter_table[i][0][0], 8, 4, 1); //FIXME name?
         }
         
         init_vlc(&mb_intra_vlc, MB_INTRA_VLC_BITS, 64, 
                  &table_mb_intra[0][1], 4, 2,
-                 &table_mb_intra[0][0], 4, 2);
+                 &table_mb_intra[0][0], 4, 2, 1);
         
         init_vlc(&v1_intra_cbpc_vlc, V1_INTRA_CBPC_VLC_BITS, 8, 
                  intra_MCBPC_bits, 1, 1,
-                 intra_MCBPC_code, 1, 1);
+                 intra_MCBPC_code, 1, 1, 1);
         init_vlc(&v1_inter_cbpc_vlc, V1_INTER_CBPC_VLC_BITS, 25, 
                  inter_MCBPC_bits, 1, 1,
-                 inter_MCBPC_code, 1, 1);
+                 inter_MCBPC_code, 1, 1, 1);
         
         init_vlc(&inter_intra_vlc, INTER_INTRA_VLC_BITS, 4, 
                  &table_inter_intra[0][1], 2, 1,
-                 &table_inter_intra[0][0], 2, 1);
+                 &table_inter_intra[0][0], 2, 1, 1);
     }
     
     switch(s->msmpeg4_version){
--- a/rv10.c	Sat Nov 27 03:54:18 2004 +0000
+++ b/rv10.c	Sat Nov 27 18:10:06 2004 +0000
@@ -518,10 +518,10 @@
     if (!done) {
         init_vlc(&rv_dc_lum, DC_VLC_BITS, 256, 
                  rv_lum_bits, 1, 1,
-                 rv_lum_code, 2, 2);
+                 rv_lum_code, 2, 2, 1);
         init_vlc(&rv_dc_chrom, DC_VLC_BITS, 256, 
                  rv_chrom_bits, 1, 1,
-                 rv_chrom_code, 2, 2);
+                 rv_chrom_code, 2, 2, 1);
         done = 1;
     }
     
--- a/svq1.c	Sat Nov 27 03:54:18 2004 +0000
+++ b/svq1.c	Sat Nov 27 18:10:06 2004 +0000
@@ -844,28 +844,28 @@
 
     init_vlc(&svq1_block_type, 2, 4,
         &svq1_block_type_vlc[0][1], 2, 1,
-        &svq1_block_type_vlc[0][0], 2, 1);
+        &svq1_block_type_vlc[0][0], 2, 1, 1);
 
     init_vlc(&svq1_motion_component, 7, 33,
         &mvtab[0][1], 2, 1,
-        &mvtab[0][0], 2, 1);
+        &mvtab[0][0], 2, 1, 1);
 
     for (i = 0; i < 6; i++) {
         init_vlc(&svq1_intra_multistage[i], 3, 8,
             &svq1_intra_multistage_vlc[i][0][1], 2, 1,
-            &svq1_intra_multistage_vlc[i][0][0], 2, 1);
+            &svq1_intra_multistage_vlc[i][0][0], 2, 1, 1);
         init_vlc(&svq1_inter_multistage[i], 3, 8,
             &svq1_inter_multistage_vlc[i][0][1], 2, 1,
-            &svq1_inter_multistage_vlc[i][0][0], 2, 1);
+            &svq1_inter_multistage_vlc[i][0][0], 2, 1, 1);
     }
 
     init_vlc(&svq1_intra_mean, 8, 256,
         &svq1_intra_mean_vlc[0][1], 4, 2,
-        &svq1_intra_mean_vlc[0][0], 4, 2);
+        &svq1_intra_mean_vlc[0][0], 4, 2, 1);
 
     init_vlc(&svq1_inter_mean, 9, 512,
         &svq1_inter_mean_vlc[0][1], 4, 2,
-        &svq1_inter_mean_vlc[0][0], 4, 2);
+        &svq1_inter_mean_vlc[0][0], 4, 2, 1);
 
     return 0;
 }
--- a/utils.c	Sat Nov 27 03:54:18 2004 +0000
+++ b/utils.c	Sat Nov 27 18:10:06 2004 +0000
@@ -90,6 +90,26 @@
 }
 
 /**
+ * same as above, but does realloc
+ */
+
+void *av_realloc_static(void *ptr, unsigned int size)
+{
+    int i;
+    if(!ptr)
+      return av_mallocz_static(size);
+    /* Look for the old ptr */
+    for(i = 0; i < last_static; i++) {
+        if(array_static[i] == ptr) {
+            array_static[i] = av_realloc(array_static[i], size);
+            return array_static[i];
+        }
+    }
+    return NULL;
+
+}
+
+/**
  * free all static arrays and reset pointers to 0.
  */
 void av_free_static(void)
--- a/vp3.c	Sat Nov 27 03:54:18 2004 +0000
+++ b/vp3.c	Sat Nov 27 18:10:06 2004 +0000
@@ -2420,27 +2420,27 @@
         /* DC histograms */
         init_vlc(&s->dc_vlc[i], 5, 32,
             &dc_bias[i][0][1], 4, 2,
-            &dc_bias[i][0][0], 4, 2);
+            &dc_bias[i][0][0], 4, 2, 0);
 
         /* group 1 AC histograms */
         init_vlc(&s->ac_vlc_1[i], 5, 32,
             &ac_bias_0[i][0][1], 4, 2,
-            &ac_bias_0[i][0][0], 4, 2);
+            &ac_bias_0[i][0][0], 4, 2, 0);
 
         /* group 2 AC histograms */
         init_vlc(&s->ac_vlc_2[i], 5, 32,
             &ac_bias_1[i][0][1], 4, 2,
-            &ac_bias_1[i][0][0], 4, 2);
+            &ac_bias_1[i][0][0], 4, 2, 0);
 
         /* group 3 AC histograms */
         init_vlc(&s->ac_vlc_3[i], 5, 32,
             &ac_bias_2[i][0][1], 4, 2,
-            &ac_bias_2[i][0][0], 4, 2);
+            &ac_bias_2[i][0][0], 4, 2, 0);
 
         /* group 4 AC histograms */
         init_vlc(&s->ac_vlc_4[i], 5, 32,
             &ac_bias_3[i][0][1], 4, 2,
-            &ac_bias_3[i][0][0], 4, 2);
+            &ac_bias_3[i][0][0], 4, 2, 0);
     }
 
     /* build quantization zigzag table */
--- a/wmadec.c	Sat Nov 27 03:54:18 2004 +0000
+++ b/wmadec.c	Sat Nov 27 18:10:06 2004 +0000
@@ -182,7 +182,7 @@
     const uint16_t *p;
     int i, l, j, level;
 
-    init_vlc(vlc, 9, n, table_bits, 1, 1, table_codes, 4, 4);
+    init_vlc(vlc, 9, n, table_bits, 1, 1, table_codes, 4, 4, 0);
 
     run_table = av_malloc(n * sizeof(uint16_t));
     level_table = av_malloc(n * sizeof(uint16_t));
@@ -493,13 +493,13 @@
 #endif
         init_vlc(&s->hgain_vlc, 9, sizeof(hgain_huffbits), 
                  hgain_huffbits, 1, 1,
-                 hgain_huffcodes, 2, 2);
+                 hgain_huffcodes, 2, 2, 0);
     }
 
     if (s->use_exp_vlc) {
         init_vlc(&s->exp_vlc, 9, sizeof(scale_huffbits), 
                  scale_huffbits, 1, 1,
-                 scale_huffcodes, 4, 4);
+                 scale_huffcodes, 4, 4, 0);
     } else {
         wma_lsp_to_curve_init(s, s->frame_len);
     }