]> granicus.if.org Git - libx264/commitdiff
Avoid redundant log2f calls in mv cost initialization
authorFiona Glaser <fiona@x264.com>
Sun, 24 Apr 2011 04:22:13 +0000 (21:22 -0700)
committerFiona Glaser <fiona@x264.com>
Tue, 26 Apr 2011 05:45:25 +0000 (22:45 -0700)
Saves around 100 million clock cycles on x264 init.

encoder/analyse.c
encoder/analyse.h
encoder/encoder.c

index b6907200dc0c72929db330e6d43ba137215a7cb5..3d00c371d97377e3bba6f71d2115f7d210700db5 100644 (file)
@@ -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++ )
index 434d30ec724cb76be45b77ecd22dcff160d9e34b..0d1c6803d418506f5b7aab8898551f543fa03aab 100644 (file)
@@ -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 );
index 6f448125f7d0aeda786421a43deaf1c6541c3e2a..eaa6366250f501ff598c6856929d4ba96f2e606b 100644 (file)
@@ -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. */