encoder_open: Fix memory leak
authorHenrik Gramner <henrik@gramner.com>
Fri, 30 Oct 2015 15:55:49 +0000 (16:55 +0100)
committerHenrik Gramner <henrik@gramner.com>
Sun, 3 Jan 2016 16:15:47 +0000 (17:15 +0100)
Furthermore, the x264_analyse_prepare_costs() and x264_analyse_init_costs()
functions were only used in x264_encoder_open(), so move that entire section
of code to analyse.c as well to simplify things.

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

index b82f647..25257a9 100644 (file)
@@ -278,18 +278,7 @@ static uint16_t x264_cost_ref[QP_MAX+1][3][33];
 static UNUSED x264_pthread_mutex_t cost_ref_mutex = X264_PTHREAD_MUTEX_INITIALIZER;
 static uint16_t x264_cost_i4x4_mode[(QP_MAX+2)*32];
 
-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 )
+static int init_costs( x264_t *h, float *logs, int qp )
 {
     int lambda = x264_lambda_tab[qp];
     if( h->cost_mv[qp] )
@@ -325,6 +314,30 @@ fail:
     return -1;
 }
 
+int x264_analyse_init_costs( x264_t *h )
+{
+    float *logs = x264_malloc( (2*4*2048+1) * sizeof(float) );
+    if( !logs )
+        return -1;
+
+    logs[0] = 0.718f;
+    for( int i = 1; i <= 2*4*2048; i++ )
+        logs[i] = log2f( i+1 ) * 2.0f + 1.718f;
+
+    for( int qp = X264_MIN( h->param.rc.i_qp_min, QP_MAX_SPEC ); qp <= h->param.rc.i_qp_max; qp++ )
+        if( init_costs( h, logs, qp ) )
+            goto fail;
+
+    if( init_costs( h, logs, X264_LOOKAHEAD_QP ) )
+        goto fail;
+
+    x264_free( logs );
+    return 0;
+fail:
+    x264_free( logs );
+    return -1;
+}
+
 void x264_analyse_free_costs( x264_t *h )
 {
     for( int i = 0; i < QP_MAX+1; i++ )
index 1a166eb..cea2fff 100644 (file)
@@ -27,8 +27,7 @@
 #ifndef X264_ANALYSE_H
 #define X264_ANALYSE_H
 
-float *x264_analyse_prepare_costs( x264_t *h );
-int x264_analyse_init_costs( x264_t *h, float *logs, int qp );
+int x264_analyse_init_costs( x264_t *h );
 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 c87d79d..92e9674 100644 (file)
@@ -1409,7 +1409,7 @@ x264_t *x264_encoder_open( x264_param_t *param )
 {
     x264_t *h;
     char buf[1000], *p;
-    int qp, i_slicetype_length;
+    int i_slicetype_length;
 
     CHECKED_MALLOCZERO( h, sizeof(x264_t) );
 
@@ -1576,15 +1576,8 @@ 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 )
+    if( x264_analyse_init_costs( h ) )
         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, logs, qp ) )
-            goto fail;
-    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. */