From 073c2593c9f0aa4445a6fc1b9b24e6e52a8cc2c1 Mon Sep 17 00:00:00 2001 From: Burkhard Plaum Date: Sat, 27 Nov 2004 18:10:06 +0000 Subject: [PATCH] Memory leak fix patch by (Burkhard Plaum at< ipf.uni-stuttgart )dot( de>) Originally committed as revision 3717 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavcodec/4xm.c | 15 ++++------ libavcodec/asv1.c | 10 +++---- libavcodec/avcodec.h | 1 + libavcodec/common.c | 38 +++++++++++++++++------- libavcodec/common.h | 3 +- libavcodec/dv.c | 2 +- libavcodec/h261.c | 16 +++++----- libavcodec/h263.c | 62 +++++++++++++++++++++------------------ libavcodec/h264.c | 12 ++++---- libavcodec/huffyuv.c | 4 +-- libavcodec/mjpeg.c | 14 ++++----- libavcodec/mpeg12.c | 35 ++++++++++++---------- libavcodec/mpegaudiodec.c | 4 +-- libavcodec/mpegvideo.c | 21 ++++++++++--- libavcodec/mpegvideo.h | 4 +-- libavcodec/msmpeg4.c | 38 ++++++++++++------------ libavcodec/rv10.c | 4 +-- libavcodec/svq1.c | 12 ++++---- libavcodec/utils.c | 20 +++++++++++++ libavcodec/vp3.c | 10 +++---- libavcodec/wmadec.c | 6 ++-- 21 files changed, 193 insertions(+), 138 deletions(-) diff --git a/libavcodec/4xm.c b/libavcodec/4xm.c index fd84f89684..9f82fb1c04 100644 --- a/libavcodec/4xm.c +++ b/libavcodec/4xm.c @@ -220,17 +220,12 @@ static void idct(DCTELEM block[64]){ } 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 @@ static uint8_t *read_huffman_tables(FourXContext *f, uint8_t * const buf){ init_vlc(&f->pre_vlc, ACDC_VLC_BITS, 257, len_tab , 1, 1, - bits_tab, 4, 4); + bits_tab, 4, 4, 0); return ptr; } diff --git a/libavcodec/asv1.c b/libavcodec/asv1.c index 2ab729c176..c369c0ea2a 100644 --- a/libavcodec/asv1.c +++ b/libavcodec/asv1.c @@ -137,19 +137,19 @@ static void init_vlcs(ASV1Context *a){ 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); } } diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 86c0c437cf..e64b2a3aca 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -2200,6 +2200,7 @@ void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size); /* 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); diff --git a/libavcodec/common.c b/libavcodec/common.c index 0aa97d85a3..902d5e728d 100644 --- a/libavcodec/common.c +++ b/libavcodec/common.c @@ -126,15 +126,19 @@ int check_marker(GetBitContext *s, const char *msg) } -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 @@ static int build_table(VLC *vlc, int table_nb_bits, 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 @@ static int build_table(VLC *vlc, int table_nb_bits, 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 @@ static int build_table(VLC *vlc, int table_nb_bits, '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 @@ int init_vlc(VLC *vlc, int nb_bits, int nb_codes, 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; } diff --git a/libavcodec/common.h b/libavcodec/common.h index 0fa3f6e2dc..fba2306396 100644 --- a/libavcodec/common.h +++ b/libavcodec/common.h @@ -939,7 +939,8 @@ int check_marker(GetBitContext *s, const char *msg); 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); /** diff --git a/libavcodec/dv.c b/libavcodec/dv.c index ae50315292..1e190a5cbd 100644 --- a/libavcodec/dv.c +++ b/libavcodec/dv.c @@ -144,7 +144,7 @@ static int dvvideo_init(AVCodecContext *avctx) /* 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) { diff --git a/libavcodec/h261.c b/libavcodec/h261.c index 87ac6706d6..4286129dff 100644 --- a/libavcodec/h261.c +++ b/libavcodec/h261.c @@ -282,7 +282,7 @@ void ff_h261_encode_init(MpegEncContext *s){ 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_mtype_vlc; 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 @@ static void h261_decode_init_vlc(H261Context *h){ 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); } } diff --git a/libavcodec/h263.c b/libavcodec/h263.c index fb862cd8b7..7b7d7eb333 100644 --- a/libavcodec/h263.c +++ b/libavcodec/h263.c @@ -1931,9 +1931,9 @@ void h263_encode_init(MpegEncContext *s) 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 mb_type_b_vlc; 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 @@ void init_vlc_rl(RLTable *rl) 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; ivlc.table_size; i++){ int code= rl->vlc.table[i][0]; int len = rl->vlc.table[i][1]; @@ -2856,44 +2862,44 @@ void h263_decode_init_vlc(MpegEncContext *s) 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); } } diff --git a/libavcodec/h264.c b/libavcodec/h264.c index 38d5bebdde..da601d4cd9 100644 --- a/libavcodec/h264.c +++ b/libavcodec/h264.c @@ -2091,33 +2091,33 @@ static void decode_init_vlc(H264Context *h){ 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); } } diff --git a/libavcodec/huffyuv.c b/libavcodec/huffyuv.c index 947e04c202..99925dde96 100644 --- a/libavcodec/huffyuv.c +++ b/libavcodec/huffyuv.c @@ -308,7 +308,7 @@ printf("%6X, %2d, %3d\n", s->bits[i][j], s->len[i][j], j); } #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 @@ static int read_old_huffman_tables(HYuvContext *s){ 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; diff --git a/libavcodec/mjpeg.c b/libavcodec/mjpeg.c index a3743b754d..daa28d3540 100644 --- a/libavcodec/mjpeg.c +++ b/libavcodec/mjpeg.c @@ -844,7 +844,7 @@ typedef struct MJpegDecodeContext { 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 @@ static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_tab 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 @@ static int mjpeg_decode_init(AVCodecContext *avctx) 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 @@ static int mjpeg_decode_dht(MJpegDecodeContext *s) 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; } } diff --git a/libavcodec/mpeg12.c b/libavcodec/mpeg12.c index a9f5bc49da..cb24c514ad 100644 --- a/libavcodec/mpeg12.c +++ b/libavcodec/mpeg12.c @@ -104,16 +104,19 @@ static uint8_t mpeg1_index_run[2][64]; 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); + + 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)); - - rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM)); for(i=0; ivlc.table_size; i++){ int code= rl->vlc.table[i][0]; int len = rl->vlc.table[i][1]; @@ -763,7 +766,7 @@ void ff_mpeg1_encode_init(MpegEncContext *s) int i; done=1; - init_rl(&rl_mpeg1); + init_rl(&rl_mpeg1, 1); for(i=0; i<64; i++) { @@ -991,31 +994,31 @@ static void init_vlcs(void) 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); } } diff --git a/libavcodec/mpegaudiodec.c b/libavcodec/mpegaudiodec.c index 4199691996..a1eadcbbda 100644 --- a/libavcodec/mpegaudiodec.c +++ b/libavcodec/mpegaudiodec.c @@ -375,7 +375,7 @@ static int decode_init(AVCodecContext * avctx) 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 @@ static int decode_init(AVCodecContext * avctx) } 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++) { diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c index 47d7a9635d..7b2cb2224d 100644 --- a/libavcodec/mpegvideo.c +++ b/libavcodec/mpegvideo.c @@ -1267,12 +1267,16 @@ int MPV_encode_end(AVCodecContext *avctx) #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 @@ void init_rl(RLTable *rl) 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); } } diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h index 775b6bcbf0..fc44781cc9 100644 --- a/libavcodec/mpegvideo.h +++ b/libavcodec/mpegvideo.h @@ -841,8 +841,8 @@ typedef struct RLTable { 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) { diff --git a/libavcodec/msmpeg4.c b/libavcodec/msmpeg4.c index 1941991ed7..a22631c222 100644 --- a/libavcodec/msmpeg4.c +++ b/libavcodec/msmpeg4.c @@ -239,7 +239,7 @@ void ff_msmpeg4_encode_init(MpegEncContext *s) init_mv_table(&mv_tables[0]); init_mv_table(&mv_tables[1]); for(i=0;ivlc, 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){ diff --git a/libavcodec/rv10.c b/libavcodec/rv10.c index 65a741c68a..efba219a6a 100644 --- a/libavcodec/rv10.c +++ b/libavcodec/rv10.c @@ -518,10 +518,10 @@ static int rv10_decode_init(AVCodecContext *avctx) 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; } diff --git a/libavcodec/svq1.c b/libavcodec/svq1.c index 794dc02dd4..4c27fb84ef 100644 --- a/libavcodec/svq1.c +++ b/libavcodec/svq1.c @@ -844,28 +844,28 @@ static int svq1_decode_init(AVCodecContext *avctx) 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; } diff --git a/libavcodec/utils.c b/libavcodec/utils.c index c1f88d0cb4..d87ed6e139 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -89,6 +89,26 @@ void *av_mallocz_static(unsigned int size) return ptr; } +/** + * 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. */ diff --git a/libavcodec/vp3.c b/libavcodec/vp3.c index f070040fe1..4c7976cde9 100644 --- a/libavcodec/vp3.c +++ b/libavcodec/vp3.c @@ -2420,27 +2420,27 @@ static int vp3_decode_init(AVCodecContext *avctx) /* 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 */ diff --git a/libavcodec/wmadec.c b/libavcodec/wmadec.c index 01ce89c5ca..cc83f4f823 100644 --- a/libavcodec/wmadec.c +++ b/libavcodec/wmadec.c @@ -182,7 +182,7 @@ static void init_coef_vlc(VLC *vlc, 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 @@ static int wma_decode_init(AVCodecContext * avctx) #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); }