[vlc-devel] [PATCH 1/2] codec: x264: check var_Get*String return value and some code cleanup

Zhao Zhili quinkblack at foxmail.com
Wed Aug 29 08:27:44 CEST 2018


---
 modules/codec/x264.c | 204 +++++++++++++++++++++++++++------------------------
 1 file changed, 108 insertions(+), 96 deletions(-)

diff --git a/modules/codec/x264.c b/modules/codec/x264.c
index f709ed9..eca3ba0 100644
--- a/modules/codec/x264.c
+++ b/modules/codec/x264.c
@@ -819,7 +819,7 @@ static int  Open ( vlc_object_t *p_this )
     fullrange |= p_enc->fmt_in.video.b_color_range_full;
     p_enc->fmt_in.i_codec = fullrange ? VLC_CODEC_J420 : VLC_CODEC_I420;
     p_sys->i_colorspace = X264_CSP_I420;
-    char *psz_profile = var_GetString( p_enc, SOUT_CFG_PREFIX "profile" );
+    char *psz_profile = var_GetNonEmptyString( p_enc, SOUT_CFG_PREFIX "profile" );
 # ifdef MODULE_NAME_IS_x26410b
     const int mask = X264_CSP_HIGH_DEPTH;
 # else
@@ -867,13 +867,8 @@ static int  Open ( vlc_object_t *p_this )
     p_sys->i_sei_size = 0;
     p_sys->p_sei = NULL;
 
-    char *psz_preset = var_GetString( p_enc, SOUT_CFG_PREFIX  "preset" );
-    char *psz_tune = var_GetString( p_enc, SOUT_CFG_PREFIX  "tune" );
-    if( *psz_preset == '\0' )
-    {
-        free(psz_preset);
-        psz_preset = NULL;
-    }
+    char *psz_preset = var_GetNonEmptyString( p_enc, SOUT_CFG_PREFIX  "preset" );
+    char *psz_tune = var_GetNonEmptyString( p_enc, SOUT_CFG_PREFIX  "tune" );
 #ifdef MODULE_NAME_IS_x262
     p_sys->param.b_mpeg2 = true;
     x264_param_default_mpeg2( &p_sys->param );
@@ -995,7 +990,7 @@ static int  Open ( vlc_object_t *p_this )
     /* disable deblocking when nf (no loop filter) is enabled */
     p_sys->param.b_deblocking_filter = !var_GetBool( p_enc, SOUT_CFG_PREFIX "nf" );
 
-    psz_val = var_GetString( p_enc, SOUT_CFG_PREFIX "deblock" );
+    psz_val = var_GetNonEmptyString( p_enc, SOUT_CFG_PREFIX "deblock" );
     if( psz_val )
     {
         if( atoi( psz_val ) != 0 )
@@ -1008,7 +1003,7 @@ static int  Open ( vlc_object_t *p_this )
         free( psz_val );
     }
 
-    psz_val = var_GetString( p_enc, SOUT_CFG_PREFIX "psy-rd" );
+    psz_val = var_GetNonEmptyString( p_enc, SOUT_CFG_PREFIX "psy-rd" );
     if( psz_val )
     {
         if( us_atof( psz_val ) != 1.0 )
@@ -1022,7 +1017,7 @@ static int  Open ( vlc_object_t *p_this )
 
     p_sys->param.analyse.b_psy = var_GetBool( p_enc, SOUT_CFG_PREFIX "psy" );
 
-    psz_val = var_GetString( p_enc, SOUT_CFG_PREFIX "level" );
+    psz_val = var_GetNonEmptyString( p_enc, SOUT_CFG_PREFIX "level" );
     if( psz_val )
     {
         if( us_atof (psz_val) < 6 && us_atof (psz_val) > 0 )
@@ -1075,21 +1070,24 @@ static int  Open ( vlc_object_t *p_this )
 
     p_sys->param.b_intra_refresh = var_GetBool( p_enc, SOUT_CFG_PREFIX "intra-refresh" );
 
-    psz_val = var_GetString( p_enc, SOUT_CFG_PREFIX "bpyramid" );
-    if( !strcmp( psz_val, "normal" ) )
-    {
-        p_sys->param.i_bframe_pyramid = X264_B_PYRAMID_NORMAL;
-    }
-    else if ( !strcmp( psz_val, "strict" ) )
-    {
-        p_sys->param.i_bframe_pyramid = X264_B_PYRAMID_STRICT;
-    }
-    else if ( !strcmp( psz_val, "none" ) )
+    psz_val = var_GetNonEmptyString( p_enc, SOUT_CFG_PREFIX "bpyramid" );
+    if( psz_val )
     {
-        p_sys->param.i_bframe_pyramid = X264_B_PYRAMID_NONE;
-    }
+        if( !strcmp( psz_val, "normal" ) )
+        {
+            p_sys->param.i_bframe_pyramid = X264_B_PYRAMID_NORMAL;
+        }
+        else if ( !strcmp( psz_val, "strict" ) )
+        {
+            p_sys->param.i_bframe_pyramid = X264_B_PYRAMID_STRICT;
+        }
+        else if ( !strcmp( psz_val, "none" ) )
+        {
+            p_sys->param.i_bframe_pyramid = X264_B_PYRAMID_NONE;
+        }
 
-    free( psz_val );
+        free( psz_val );
+    }
 
     i_val = var_GetInteger( p_enc, SOUT_CFG_PREFIX "ref" );
     p_sys->param.i_frame_reference = i_val;
@@ -1104,35 +1102,41 @@ static int  Open ( vlc_object_t *p_this )
     if( i_val >= 1 && i_val != 7 )
         p_sys->param.analyse.i_subpel_refine = i_val;
 
-    psz_val = var_GetString( p_enc, SOUT_CFG_PREFIX "hrd");
-    if( !strcmp( psz_val, "vbr" ) )
-        p_sys->param.i_nal_hrd = X264_NAL_HRD_VBR;
-    else if( !strcmp( psz_val, "cbr" ) )
-        p_sys->param.i_nal_hrd = X264_NAL_HRD_CBR;
-    free( psz_val );
+    psz_val = var_GetNonEmptyString( p_enc, SOUT_CFG_PREFIX "hrd");
+    if( psz_val )
+    {
+        if( !strcmp( psz_val, "vbr" ) )
+            p_sys->param.i_nal_hrd = X264_NAL_HRD_VBR;
+        else if( !strcmp( psz_val, "cbr" ) )
+            p_sys->param.i_nal_hrd = X264_NAL_HRD_CBR;
+        free( psz_val );
+    }
 
-    //TODO: psz_val == NULL ?
-    psz_val = var_GetString( p_enc, SOUT_CFG_PREFIX "me" );
-    if( psz_val && strcmp( psz_val, "hex" ) )
+    psz_val = var_GetNonEmptyString( p_enc, SOUT_CFG_PREFIX "me" );
+    if( psz_val )
     {
-       if( !strcmp( psz_val, "dia" ) )
-       {
+        if( !strcmp( psz_val, "hex" ) )
+        {
+             p_sys->param.analyse.i_me_method = X264_ME_HEX;
+        }
+        else if( !strcmp( psz_val, "dia" ) )
+        {
             p_sys->param.analyse.i_me_method = X264_ME_DIA;
-       }
-       else if( !strcmp( psz_val, "umh" ) )
-       {
+        }
+        else if( !strcmp( psz_val, "umh" ) )
+        {
             p_sys->param.analyse.i_me_method = X264_ME_UMH;
         }
-       else if( !strcmp( psz_val, "esa" ) )
-       {
-           p_sys->param.analyse.i_me_method = X264_ME_ESA;
-       }
-       else if( !strcmp( psz_val, "tesa" ) )
-       {
-           p_sys->param.analyse.i_me_method = X264_ME_TESA;
-       }
+        else if( !strcmp( psz_val, "esa" ) )
+        {
+            p_sys->param.analyse.i_me_method = X264_ME_ESA;
+        }
+        else if( !strcmp( psz_val, "tesa" ) )
+        {
+            p_sys->param.analyse.i_me_method = X264_ME_TESA;
+        }
+        free( psz_val );
     }
-    free( psz_val );
 
     i_val = var_GetInteger( p_enc, SOUT_CFG_PREFIX "merange" );
     p_sys->param.analyse.i_me_range = i_val;
@@ -1142,24 +1146,27 @@ static int  Open ( vlc_object_t *p_this )
     p_sys->param.analyse.i_mv_range_thread = var_GetInteger( p_enc,
                                     SOUT_CFG_PREFIX "mvrange-thread" );
 
-    psz_val = var_GetString( p_enc, SOUT_CFG_PREFIX "direct" );
-    if( !strcmp( psz_val, "none" ) )
-    {
-        p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_NONE;
-    }
-    else if( !strcmp( psz_val, "spatial" ) )
-    {
-        p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
-    }
-    else if( !strcmp( psz_val, "temporal" ) )
-    {
-        p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_TEMPORAL;
-    }
-    else if( !strcmp( psz_val, "auto" ) )
+    psz_val = var_GetNonEmptyString( p_enc, SOUT_CFG_PREFIX "direct" );
+    if( psz_val )
     {
-        p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
+        if( !strcmp( psz_val, "none" ) )
+        {
+            p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_NONE;
+        }
+        else if( !strcmp( psz_val, "spatial" ) )
+        {
+            p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
+        }
+        else if( !strcmp( psz_val, "temporal" ) )
+        {
+            p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_TEMPORAL;
+        }
+        else if( !strcmp( psz_val, "auto" ) )
+        {
+            p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
+        }
+        free( psz_val );
     }
-    free( psz_val );
 
     p_sys->param.analyse.b_psnr = var_GetBool( p_enc, SOUT_CFG_PREFIX "psnr" );
     p_sys->param.analyse.b_ssim = var_GetBool( p_enc, SOUT_CFG_PREFIX "ssim" );
@@ -1205,39 +1212,42 @@ static int  Open ( vlc_object_t *p_this )
 #ifndef X264_ANALYSE_BSUB16x16
 #   define X264_ANALYSE_BSUB16x16 0
 #endif
-    psz_val = var_GetString( p_enc, SOUT_CFG_PREFIX "partitions" );
-    if( !strcmp( psz_val, "none" ) )
-    {
-        p_sys->param.analyse.inter = 0;
-    }
-    else if( !strcmp( psz_val, "fast" ) )
-    {
-        p_sys->param.analyse.inter = X264_ANALYSE_I4x4;
-    }
-    else if( !strcmp( psz_val, "normal" ) )
+    psz_val = var_GetNonEmptyString( p_enc, SOUT_CFG_PREFIX "partitions" );
+    if( psz_val )
     {
-        p_sys->param.analyse.inter =
-            X264_ANALYSE_I4x4 |
-            X264_ANALYSE_PSUB16x16;
+        if( !strcmp( psz_val, "none" ) )
+        {
+            p_sys->param.analyse.inter = 0;
+        }
+        else if( !strcmp( psz_val, "fast" ) )
+        {
+            p_sys->param.analyse.inter = X264_ANALYSE_I4x4;
+        }
+        else if( !strcmp( psz_val, "normal" ) )
+        {
+            p_sys->param.analyse.inter =
+                X264_ANALYSE_I4x4 |
+                X264_ANALYSE_PSUB16x16;
 #ifdef X264_ANALYSE_I8x8
-        p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
+            p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
 #endif
-    }
-    else if( !strcmp( psz_val, "slow" ) )
-    {
-        p_sys->param.analyse.inter =
-            X264_ANALYSE_I4x4 |
-            X264_ANALYSE_PSUB16x16 |
-            X264_ANALYSE_BSUB16x16;
+        }
+        else if( !strcmp( psz_val, "slow" ) )
+        {
+            p_sys->param.analyse.inter =
+                X264_ANALYSE_I4x4 |
+                X264_ANALYSE_PSUB16x16 |
+                X264_ANALYSE_BSUB16x16;
 #ifdef X264_ANALYSE_I8x8
-        p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
+            p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
 #endif
+        }
+        else if( !strcmp( psz_val, "all" ) )
+        {
+            p_sys->param.analyse.inter = ~0;
+        }
+        free( psz_val );
     }
-    else if( !strcmp( psz_val, "all" ) )
-    {
-        p_sys->param.analyse.inter = ~0;
-    }
-    free( psz_val );
 
     p_sys->param.analyse.b_transform_8x8 = var_GetBool( p_enc,
                                     SOUT_CFG_PREFIX "8x8dct" );
@@ -1276,10 +1286,12 @@ static int  Open ( vlc_object_t *p_this )
 
     /* Check if user has given some profile (baseline,main,high) to limit
      * settings, and apply those*/
-    psz_val = var_GetString( p_enc, SOUT_CFG_PREFIX "profile" );
-    if( psz_val && *psz_val )
+    psz_val = var_GetNonEmptyString( p_enc, SOUT_CFG_PREFIX "profile" );
+    if( psz_val )
+    {
         x264_param_apply_profile( &p_sys->param, psz_val );
-    free( psz_val );
+        free( psz_val );
+    }
 
 #if defined (__i386__) || defined (__x86_64__)
     if( !vlc_CPU_MMX() )
@@ -1299,7 +1311,7 @@ static int  Open ( vlc_object_t *p_this )
        default unless ofcourse transcode threads is explicitly specified.. */
     p_sys->param.i_threads = p_enc->i_threads;
 
-    psz_val = var_GetString( p_enc, SOUT_CFG_PREFIX "stats" );
+    psz_val = var_GetNonEmptyString( p_enc, SOUT_CFG_PREFIX "stats" );
     if( psz_val )
     {
         p_sys->param.rc.psz_stat_in  =
@@ -1342,8 +1354,8 @@ static int  Open ( vlc_object_t *p_this )
     /* We don't want repeated headers, we repeat p_extra ourself if needed */
     p_sys->param.b_repeat_headers = 0;
 
-    char *psz_opts = var_InheritString( p_enc, SOUT_CFG_PREFIX "options" );
-    if (psz_opts && *psz_opts) {
+    char *psz_opts = var_GetNonEmptyString( p_enc, SOUT_CFG_PREFIX "options" );
+    if( psz_opts ) {
         config_chain_t *cfg = NULL;
         config_ChainParseOptions(&cfg, psz_opts);
         while (cfg) {
-- 
2.9.5



More information about the vlc-devel mailing list