[x264-devel] encoder_open: Fix memory leak

Henrik Gramner git at videolan.org
Sun Jan 17 22:17:53 CET 2016


x264 | branch: master | Henrik Gramner <henrik at gramner.com> | Fri Oct 30 16:55:49 2015 +0100| [5c6570495f8f1c716b294aee1430d8766a4beb9c] | committer: Henrik Gramner

encoder_open: Fix memory leak

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.

> http://git.videolan.org/gitweb.cgi/x264.git/?a=commit;h=5c6570495f8f1c716b294aee1430d8766a4beb9c
---

 encoder/analyse.c |   37 +++++++++++++++++++++++++------------
 encoder/analyse.h |    3 +--
 encoder/encoder.c |   11 ++---------
 3 files changed, 28 insertions(+), 23 deletions(-)

diff --git a/encoder/analyse.c b/encoder/analyse.c
index b82f647..25257a9 100644
--- a/encoder/analyse.c
+++ b/encoder/analyse.c
@@ -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++ )
diff --git a/encoder/analyse.h b/encoder/analyse.h
index 1a166eb..cea2fff 100644
--- a/encoder/analyse.h
+++ b/encoder/analyse.h
@@ -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 );
diff --git a/encoder/encoder.c b/encoder/encoder.c
index c87d79d..92e9674 100644
--- a/encoder/encoder.c
+++ b/encoder/encoder.c
@@ -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. */



More information about the x264-devel mailing list