diff --git a/encoder/analyse.c b/encoder/analyse.c index 8ae426f..a4e9df0 100644 --- a/encoder/analyse.c +++ b/encoder/analyse.c @@ -166,27 +166,27 @@ static const int i_sub_mb_p_cost_table[4] = { static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a ); +static int16_t *x264_cost_mv[52]; uint16_t *x264_cost_mv_fpel[52][4]; /* initialize an array of lambda*nbits for all possible mvs */ static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a ) { - static int16_t *p_cost_mv[52]; int i, j; - if( !p_cost_mv[a->i_qp] ) + if( !x264_cost_mv[a->i_qp] ) { /* could be faster, but isn't called many times */ /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */ - p_cost_mv[a->i_qp] = x264_malloc( (4*4*2048 + 1) * sizeof(int16_t) ); - p_cost_mv[a->i_qp] += 2*4*2048; + x264_cost_mv[a->i_qp] = x264_malloc( (4*4*2048 + 1) * sizeof(int16_t) ); + x264_cost_mv[a->i_qp] += 2*4*2048; for( i = 0; i <= 2*4*2048; i++ ) { - p_cost_mv[a->i_qp][-i] = - p_cost_mv[a->i_qp][i] = a->i_lambda * bs_size_se( i ); + x264_cost_mv[a->i_qp][-i] = + x264_cost_mv[a->i_qp][i] = a->i_lambda * bs_size_se( i ); } } - a->p_cost_mv = p_cost_mv[a->i_qp]; + a->p_cost_mv = x264_cost_mv[a->i_qp]; /* FIXME is this useful for all me methods? */ if( h->param.analyse.i_me_method >= X264_ME_ESA && !x264_cost_mv_fpel[a->i_qp][0] ) @@ -196,11 +196,32 @@ static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a ) x264_cost_mv_fpel[a->i_qp][j] = x264_malloc( (4*2048 + 1) * sizeof(int16_t) ); x264_cost_mv_fpel[a->i_qp][j] += 2*2048; for( i = -2*2048; i < 2*2048; i++ ) - x264_cost_mv_fpel[a->i_qp][j][i] = p_cost_mv[a->i_qp][i*4+j]; + x264_cost_mv_fpel[a->i_qp][j][i] = x264_cost_mv[a->i_qp][i*4+j]; } } } +void x264_mb_analyse_free_costs( void ) +{ + int i, j; + + for( i=0; i<52; i++ ) + { + if( x264_cost_mv[i] ) + { + x264_free( x264_cost_mv[i] - 2*4*2048 ); + x264_cost_mv[i] = NULL; + } + + for( j=0; j<4; j++ ) + if( x264_cost_mv_fpel[i][j] ) + { + x264_free( x264_cost_mv_fpel[i][j] - 2*2048 ); + x264_cost_mv_fpel[i][j] = NULL; + } + } +} + static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp ) { int i = h->param.analyse.i_subpel_refine - (h->sh.i_type == SLICE_TYPE_B); diff --git a/encoder/analyse.h b/encoder/analyse.h index b8c828f..4cf503c 100644 --- a/encoder/analyse.h +++ b/encoder/analyse.h @@ -26,5 +26,6 @@ void x264_macroblock_analyse( x264_t *h ); void x264_slicetype_decide( x264_t *h ); +void x264_mb_analyse_free_costs( void ); #endif diff --git a/encoder/encoder.c b/encoder/encoder.c index b359e3f..b1a109e 100644 --- a/encoder/encoder.c +++ b/encoder/encoder.c @@ -2104,4 +2104,6 @@ void x264_encoder_close ( x264_t *h ) x264_free( h->thread[i]->out.p_bitstream ); x264_free( h->thread[i] ); } + + x264_mb_analyse_free_costs(); }