]> git.sesse.net Git - x264/blobdiff - filters/video/crop.c
cli: Refactor filter option parsing
[x264] / filters / video / crop.c
index bff6af0a2d1ea54668361415e2fd5f1a9e459b1b..280347da2c904f0656339c72a26bc1de4cd289d7 100644 (file)
@@ -47,6 +47,20 @@ static void help( int longhelp )
     printf( "            removes pixels from the edges of the frame\n" );
 }
 
+static int handle_opts( crop_hnd_t *h, video_info_t *info, char **opts, const char * const *optlist )
+{
+    for( int i = 0; i < 4; i++ )
+    {
+        char *opt = x264_get_option( optlist[i], opts );
+        FAIL_IF_ERROR( !opt, "%s crop value not specified\n", optlist[i] )
+        h->dims[i] = x264_otoi( opt, -1 );
+        FAIL_IF_ERROR( h->dims[i] < 0, "%s crop value `%s' is less than 0\n", optlist[i], opt )
+        int dim_mod = i&1 ? (h->csp->mod_height << info->interlaced) : h->csp->mod_width;
+        FAIL_IF_ERROR( h->dims[i] % dim_mod, "%s crop value `%s' is not a multiple of %d\n", optlist[i], opt, dim_mod )
+    }
+    return 0;
+}
+
 static int init( hnd_t *handle, cli_vid_filter_t *filter, video_info_t *info, x264_param_t *param, char *opt_string )
 {
     FAIL_IF_ERROR( x264_cli_csp_is_invalid( info->csp ), "invalid csp %d\n", info->csp )
@@ -55,21 +69,16 @@ static int init( hnd_t *handle, cli_vid_filter_t *filter, video_info_t *info, x2
         return -1;
 
     h->csp = x264_cli_get_csp( info->csp );
-    static const char *optlist[] = { "left", "top", "right", "bottom", NULL };
+    static const char * const optlist[] = { "left", "top", "right", "bottom", NULL };
     char **opts = x264_split_options( opt_string, optlist );
     if( !opts )
         return -1;
 
-    for( int i = 0; i < 4; i++ )
-    {
-        char *opt = x264_get_option( optlist[i], opts );
-        FAIL_IF_ERROR( !opt, "%s crop value not specified\n", optlist[i] )
-        h->dims[i] = x264_otoi( opt, -1 );
-        FAIL_IF_ERROR( h->dims[i] < 0, "%s crop value `%s' is less than 0\n", optlist[i], opt )
-        int dim_mod = i&1 ? (h->csp->mod_height << info->interlaced) : h->csp->mod_width;
-        FAIL_IF_ERROR( h->dims[i] % dim_mod, "%s crop value `%s' is not a multiple of %d\n", optlist[i], opt, dim_mod )
-    }
-    x264_free_string_array( opts );
+    int err = handle_opts( h, info, opts, optlist );
+    free( opts );
+    if( err )
+        return -1;
+
     h->dims[2] = info->width  - h->dims[0] - h->dims[2];
     h->dims[3] = info->height - h->dims[1] - h->dims[3];
     FAIL_IF_ERROR( h->dims[2] <= 0 || h->dims[3] <= 0, "invalid output resolution %dx%d\n", h->dims[2], h->dims[3] )