From: Fiona Glaser Date: Sun, 24 Apr 2011 04:22:13 +0000 (-0700) Subject: Avoid redundant log2f calls in mv cost initialization X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=866bf26018c8d76c475d23e7fe028774e8ec9814;p=libx264 Avoid redundant log2f calls in mv cost initialization Saves around 100 million clock cycles on x264 init. --- diff --git a/encoder/analyse.c b/encoder/analyse.c index b6907200..3d00c371 100644 --- a/encoder/analyse.c +++ b/encoder/analyse.c @@ -276,7 +276,18 @@ static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a ); static uint16_t x264_cost_ref[QP_MAX+1][3][33]; static UNUSED x264_pthread_mutex_t cost_ref_mutex = X264_PTHREAD_MUTEX_INITIALIZER; -int x264_analyse_init_costs( x264_t *h, int qp ) +float *x264_analyse_prepare_costs( x264_t *h ) +{ + float *logs = x264_malloc( (2*4*2048+1)*sizeof(float) ); + if( !logs ) + return NULL; + logs[0] = 0.718f; + for( int i = 1; i <= 2*4*2048; i++ ) + logs[i] = log2f(i+1)*2 + 1.718f; + return logs; +} + +int x264_analyse_init_costs( x264_t *h, float *logs, int qp ) { int lambda = x264_lambda_tab[qp]; if( h->cost_mv[qp] ) @@ -287,7 +298,7 @@ int x264_analyse_init_costs( x264_t *h, int qp ) for( int i = 0; i <= 2*4*2048; i++ ) { h->cost_mv[qp][-i] = - h->cost_mv[qp][i] = X264_MIN( lambda * (log2f(i+1)*2 + 0.718f + !!i) + .5f, (1<<16)-1 ); + h->cost_mv[qp][i] = X264_MIN( lambda * logs[i] + .5f, (1<<16)-1 ); } x264_pthread_mutex_lock( &cost_ref_mutex ); for( int i = 0; i < 3; i++ ) diff --git a/encoder/analyse.h b/encoder/analyse.h index 434d30ec..0d1c6803 100644 --- a/encoder/analyse.h +++ b/encoder/analyse.h @@ -27,7 +27,8 @@ #ifndef X264_ANALYSE_H #define X264_ANALYSE_H -int x264_analyse_init_costs( x264_t *h, int qp ); +float *x264_analyse_prepare_costs( x264_t *h ); +int x264_analyse_init_costs( x264_t *h, float *logs, int qp ); void x264_analyse_free_costs( x264_t *h ); void x264_analyse_weight_frame( x264_t *h, int end ); void x264_macroblock_analyse( x264_t *h ); diff --git a/encoder/encoder.c b/encoder/encoder.c index 6f448125..eaa63662 100644 --- a/encoder/encoder.c +++ b/encoder/encoder.c @@ -1123,11 +1123,15 @@ x264_t *x264_encoder_open( x264_param_t *param ) p += sprintf( p, " none!" ); x264_log( h, X264_LOG_INFO, "%s\n", buf ); + float *logs = x264_analyse_prepare_costs( h ); + if( !logs ) + goto fail; for( qp = X264_MIN( h->param.rc.i_qp_min, QP_MAX_SPEC ); qp <= h->param.rc.i_qp_max; qp++ ) - if( x264_analyse_init_costs( h, qp ) ) + if( x264_analyse_init_costs( h, logs, qp ) ) goto fail; - if( x264_analyse_init_costs( h, X264_LOOKAHEAD_QP ) ) + if( x264_analyse_init_costs( h, logs, X264_LOOKAHEAD_QP ) ) goto fail; + x264_free( logs ); static const uint16_t cost_mv_correct[7] = { 24, 47, 95, 189, 379, 757, 1515 }; /* Checks for known miscompilation issues. */