Index: snow.c =================================================================== RCS file: /cvsroot/ffmpeg/ffmpeg/libavcodec/snow.c,v retrieving revision 1.87 diff -u -r1.87 snow.c --- snow.c 30 Jan 2006 23:33:18 -0000 1.87 +++ snow.c 12 Feb 2006 03:23:16 -0000 @@ -36,6 +36,10 @@ #define LOSSLESS_QLOG -128 #define FRAC_BITS 8 +// define this if you want to save memory for multiple snow encoders, +// not thread safe. helps a lot with mencoder's uspp. +#define SAVE_MEMORY + static const int8_t quant3[256]={ 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, @@ -492,6 +496,19 @@ #define slice_buffer_get_line(slice_buf, line_num) ((slice_buf)->line[line_num] ? (slice_buf)->line[line_num] : slice_buffer_load_line((slice_buf), (line_num))) //#define slice_buffer_get_line(slice_buf, line_num) (slice_buffer_load_line((slice_buf), (line_num))) +#ifdef SAVE_MEMORY +// for static_buffer_init +static DWTELEM * *sbi_buf_line = NULL; +static DWTELEM * *sbi_buf_data_stack = NULL; +// for alloc_blocks +static BlockNode *ab_s_block = NULL; +// for comon_init +static DWTELEM *ci_s_spatial_dwt_buffer = NULL; +static x_and_coeff *ci_b_x_coeff = NULL; +// for encode_init +static MpegEncContext *ei_s_m = NULL; +#endif + static void iterative_me(SnowContext *s); static void slice_buffer_init(slice_buffer * buf, int line_count, int max_allocated_lines, int line_width, DWTELEM * base_buffer) @@ -502,6 +519,11 @@ buf->line_count = line_count; buf->line_width = line_width; buf->data_count = max_allocated_lines; + +#ifdef SAVE_MEMORY + if(sbi_buf_line == NULL) { +#endif + buf->line = (DWTELEM * *) av_mallocz (sizeof(DWTELEM *) * line_count); buf->data_stack = (DWTELEM * *) av_malloc (sizeof(DWTELEM *) * max_allocated_lines); @@ -510,6 +532,16 @@ buf->data_stack[i] = (DWTELEM *) av_malloc (sizeof(DWTELEM) * line_width); } +#ifdef SAVE_MEMORY + sbi_buf_line = buf->line; + sbi_buf_data_stack = buf->data_stack; + } else { + buf->line = (DWTELEM * *) sbi_buf_line; + buf->data_stack = (DWTELEM * *) sbi_buf_data_stack; + } +#endif + + buf->data_stack_top = max_allocated_lines - 1; } @@ -1946,7 +1978,18 @@ s->b_width = w; s->b_height= h; +#ifdef SAVE_MEMORY + if(ab_s_block == NULL) { +#endif + s->block= av_mallocz(w * h * sizeof(BlockNode) << (s->block_max_depth*2)); + +#ifdef SAVE_MEMORY + ab_s_block = s->block; + } else + s->block = (BlockNode *) ab_s_block; +#endif + return 0; } @@ -3790,6 +3833,7 @@ SnowContext *s = avctx->priv_data; int width, height; int level, orientation, plane_index, dec; + x_and_coeff *xc_buf; s->avctx= avctx; @@ -3847,8 +3891,22 @@ width= s->avctx->width; height= s->avctx->height; +#ifdef SAVE_MEMORY + if(ci_s_spatial_dwt_buffer == NULL) { +#endif + s->spatial_dwt_buffer= av_mallocz(width*height*sizeof(DWTELEM)); +#ifdef SAVE_MEMORY + xc_buf = av_mallocz(((s->avctx->width+2) * (s->avctx->height+2))*sizeof(x_and_coeff)); + ci_s_spatial_dwt_buffer = s->spatial_dwt_buffer; + ci_b_x_coeff = xc_buf; + } else { + s->spatial_dwt_buffer = ci_s_spatial_dwt_buffer; + xc_buf = ci_b_x_coeff; + } +#endif + s->mv_scale= (s->avctx->flags & CODEC_FLAG_QPEL) ? 2 : 4; s->block_max_depth= (s->avctx->flags & CODEC_FLAG_4MV) ? 1 : 0; @@ -3888,7 +3946,13 @@ if(level) b->parent= &s->plane[plane_index].band[level-1][orientation]; + +#ifdef SAVE_MEMORY + b->x_coeff = xc_buf; + xc_buf += (b->width+1) * b->height+1; +#else b->x_coeff=av_mallocz(((b->width+1) * b->height+1)*sizeof(x_and_coeff)); +#endif } w= (w+1)>>1; h= (h+1)>>1; @@ -3955,12 +4019,23 @@ s->m.flags = avctx->flags; s->m.bit_rate= avctx->bit_rate; +#ifdef SAVE_MEMORY + if(ei_s_m == NULL) { +#endif + s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t)); s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t)); s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t)); s->m.obmc_scratchpad= av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t)); h263_encode_init(&s->m); //mv_penalty +#ifdef SAVE_MEMORY + ei_s_m = &s->m; + } else { + s->m = *ei_s_m; + } +#endif + if(avctx->flags&CODEC_FLAG_PASS1){ if(!avctx->stats_out) avctx->stats_out = av_mallocz(256); @@ -4234,15 +4309,26 @@ static void common_end(SnowContext *s){ int plane_index, level, orientation; +#ifdef SAVE_MEMORY + if(ci_s_spatial_dwt_buffer != NULL){ +#endif + av_freep(&s->spatial_dwt_buffer); + av_freep(&s->block); + av_freep(&ci_b_x_coeff); +#ifdef SAVE_MEMORY + ci_s_spatial_dwt_buffer = NULL; + ei_s_m = NULL; + ab_s_block = NULL; +#endif + av_freep(&s->m.me.scratchpad); av_freep(&s->m.me.map); av_freep(&s->m.me.score_map); av_freep(&s->m.obmc_scratchpad); - - av_freep(&s->block); - + +#ifndef SAVE_MEMORY for(plane_index=0; plane_index<3; plane_index++){ for(level=s->spatial_decomposition_count-1; level>=0; level--){ for(orientation=level ? 1 : 0; orientation<4; orientation++){ @@ -4251,6 +4337,7 @@ av_freep(&b->x_coeff); } } +#endif } }