]> git.videolan.org Git - x264.git/commitdiff
api addition: x264_param_parse() to set options by name
authorLoren Merritt <pengvado@videolan.org>
Mon, 31 Jul 2006 21:59:04 +0000 (21:59 +0000)
committerLoren Merritt <pengvado@videolan.org>
Mon, 31 Jul 2006 21:59:04 +0000 (21:59 +0000)
git-svn-id: svn://svn.videolan.org/x264/trunk@542 df754926-b1dd-0310-bc7b-ec298dee348c

common/common.c
x264.c
x264.h

index d882484bddf19e4191b4569864be10e9ec0f773d..d67dc563872c461ed7141c70abbc9e3cdd946cba 100644 (file)
@@ -136,6 +136,339 @@ void    x264_param_default( x264_param_t *param )
     param->b_aud = 0;
 }
 
+static int parse_enum( const char *arg, const char * const *names, int *dst )
+{
+    int i;
+    for( i = 0; names[i]; i++ )
+        if( !strcmp( arg, names[i] ) )
+        {
+            *dst = i;
+            return 0;
+        }
+    return -1;
+}
+
+static int parse_cqm( const char *str, uint8_t *cqm, int length )
+{
+    int i = 0;
+    do {
+        int coef;
+        if( !sscanf( str, "%d", &coef ) || coef < 1 || coef > 255 )
+            return -1;
+        cqm[i++] = coef;
+    } while( i < length && (str = strchr( str, ',' )) && str++ );
+    return (i == length) ? 0 : -1;
+}
+
+static int atobool( const char *str )
+{
+    if( !strcmp(str, "1") || 
+        !strcmp(str, "true") || 
+        !strcmp(str, "yes") )
+        return 1;
+    if( !strcmp(str, "0") || 
+        !strcmp(str, "false") || 
+        !strcmp(str, "no") )
+        return 0;
+    return -1;
+}
+
+#define atobool(str) ( (i = atobool(str)) < 0 ? (b_error = 1) : i )
+
+int x264_param_parse( x264_param_t *p, const char *name, const char *value )
+{
+    int b_error = 0;
+    int i;
+
+    if( !name )
+        return X264_PARAM_BAD_NAME;
+    if( !value )
+        return X264_PARAM_BAD_VALUE;
+
+    if( value[0] == '=' )
+        value++;
+
+    if( (!strncmp( name, "no-", 3 ) && (i = 3)) ||
+        (!strncmp( name, "no", 2 ) && (i = 2)) )
+    {
+        name += i;
+        value = atobool(value) ? "false" : "true";
+    }
+
+#define OPT(STR) else if( !strcmp( name, STR ) )
+    if(0);
+    OPT("asm")
+        p->cpu = atobool(value) ? x264_cpu_detect() : 0;
+    OPT("threads")
+        p->i_threads = atoi(value);
+    OPT("level")
+    {
+        if( atof(value) < 6 )
+            p->i_level_idc = (int)(10*atof(value)+.5);
+        else
+            p->i_level_idc = atoi(value);
+    }
+    OPT("sar")
+    {
+        b_error = ( 2 != sscanf( value, "%d:%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) &&
+                    2 != sscanf( value, "%d/%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) );
+    }
+    OPT("overscan")
+        b_error |= parse_enum( value, x264_overscan_names, &p->vui.i_overscan );
+    OPT("vidformat")
+        b_error |= parse_enum( value, x264_vidformat_names, &p->vui.i_vidformat );
+    OPT("fullrange")
+        b_error |= parse_enum( value, x264_fullrange_names, &p->vui.b_fullrange );
+    OPT("colourprim")
+        b_error |= parse_enum( value, x264_colorprim_names, &p->vui.i_colorprim );
+    OPT("transfer")
+        b_error |= parse_enum( value, x264_transfer_names, &p->vui.i_transfer );
+    OPT("colourmatrix")
+        b_error |= parse_enum( value, x264_colmatrix_names, &p->vui.i_colmatrix );
+    OPT("chromaloc")
+    {
+        p->vui.i_chroma_loc = atoi(value);
+        b_error = ( p->vui.i_chroma_loc < 0 || p->vui.i_chroma_loc > 5 );
+    }
+    OPT("fps")
+    {
+        float fps;
+        if( sscanf( value, "%d/%d", &p->i_fps_num, &p->i_fps_den ) == 2 )
+            ;
+        else if( sscanf( value, "%f", &fps ) )
+        {
+            p->i_fps_num = (int)(fps * 1000 + .5);
+            p->i_fps_den = 1000;
+        }
+        else
+            b_error = 1;
+    }
+    OPT("ref")
+        p->i_frame_reference = atoi(value);
+    OPT("keyint")
+    {
+        p->i_keyint_max = atoi(value);
+        if( p->i_keyint_min > p->i_keyint_max )
+            p->i_keyint_min = p->i_keyint_max;
+    }
+    OPT("min-keyint")
+    {
+        p->i_keyint_min = atoi(value);
+        if( p->i_keyint_max < p->i_keyint_min )
+            p->i_keyint_max = p->i_keyint_min;
+    }
+    OPT("scenecut")
+        p->i_scenecut_threshold = atoi(value);
+    OPT("bframes")
+        p->i_bframe = atoi(value);
+    OPT("b-adapt")
+        p->b_bframe_adaptive = atobool(value);
+    OPT("b-bias")
+        p->i_bframe_bias = atoi(value);
+    OPT("b-pyramid")
+        p->b_bframe_pyramid = atobool(value);
+    OPT("nf")
+        p->b_deblocking_filter = 0;
+    OPT("filter")
+    {
+        int count;
+        if( 0 < (count = sscanf( value, "%d:%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta )) ||
+            0 < (count = sscanf( value, "%d,%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta )) )
+        {
+            p->b_deblocking_filter = 1;
+            if( count == 1 )
+                p->i_deblocking_filter_beta = p->i_deblocking_filter_alphac0;
+        }
+        else
+            p->b_deblocking_filter = atobool(value);
+    }
+    OPT("cabac")
+        p->b_cabac = atobool(value);
+    OPT("cabac-idc")
+        p->i_cabac_init_idc = atoi(value);
+    OPT("cqm")
+    {
+        if( strstr( value, "flat" ) )
+            p->i_cqm_preset = X264_CQM_FLAT;
+        else if( strstr( value, "jvt" ) )
+            p->i_cqm_preset = X264_CQM_JVT;
+        else
+            b_error = 1;
+    }
+    OPT("cqmfile")
+        p->psz_cqm_file = strdup(value);
+    OPT("cqm4")
+    {
+        p->i_cqm_preset = X264_CQM_CUSTOM;
+        b_error |= parse_cqm( value, p->cqm_4iy, 16 );
+        b_error |= parse_cqm( value, p->cqm_4ic, 16 );
+        b_error |= parse_cqm( value, p->cqm_4py, 16 );
+        b_error |= parse_cqm( value, p->cqm_4pc, 16 );
+    }
+    OPT("cqm8")
+    {
+        p->i_cqm_preset = X264_CQM_CUSTOM;
+        b_error |= parse_cqm( value, p->cqm_8iy, 64 );
+        b_error |= parse_cqm( value, p->cqm_8py, 64 );
+    }
+    OPT("cqm4i")
+    {
+        p->i_cqm_preset = X264_CQM_CUSTOM;
+        b_error |= parse_cqm( value, p->cqm_4iy, 16 );
+        b_error |= parse_cqm( value, p->cqm_4ic, 16 );
+    }
+    OPT("cqm4p")
+    {
+        p->i_cqm_preset = X264_CQM_CUSTOM;
+        b_error |= parse_cqm( value, p->cqm_4py, 16 );
+        b_error |= parse_cqm( value, p->cqm_4pc, 16 );
+    }
+    OPT("cqm4iy")
+    {
+        p->i_cqm_preset = X264_CQM_CUSTOM;
+        b_error |= parse_cqm( value, p->cqm_4iy, 16 );
+    }
+    OPT("cqm4ic")
+    {
+        p->i_cqm_preset = X264_CQM_CUSTOM;
+        b_error |= parse_cqm( value, p->cqm_4ic, 16 );
+    }
+    OPT("cqm4py")
+    {
+        p->i_cqm_preset = X264_CQM_CUSTOM;
+        b_error |= parse_cqm( value, p->cqm_4py, 16 );
+    }
+    OPT("cqm4pc")
+    {
+        p->i_cqm_preset = X264_CQM_CUSTOM;
+        b_error |= parse_cqm( value, p->cqm_4pc, 16 );
+    }
+    OPT("cqm8i")
+    {
+        p->i_cqm_preset = X264_CQM_CUSTOM;
+        b_error |= parse_cqm( value, p->cqm_8iy, 64 );
+    }
+    OPT("cqm8p")
+    {
+        p->i_cqm_preset = X264_CQM_CUSTOM;
+        b_error |= parse_cqm( value, p->cqm_8py, 64 );
+    }
+    OPT("log")
+        p->i_log_level = atoi(value);
+    OPT("analyse")
+    {
+        p->analyse.inter = 0;
+        if( strstr( value, "none" ) )  p->analyse.inter =  0;
+        if( strstr( value, "all" ) )   p->analyse.inter = ~0;
+
+        if( strstr( value, "i4x4" ) )  p->analyse.inter |= X264_ANALYSE_I4x4;
+        if( strstr( value, "i8x8" ) )  p->analyse.inter |= X264_ANALYSE_I8x8;
+        if( strstr( value, "p8x8" ) )  p->analyse.inter |= X264_ANALYSE_PSUB16x16;
+        if( strstr( value, "p4x4" ) )  p->analyse.inter |= X264_ANALYSE_PSUB8x8;
+        if( strstr( value, "b8x8" ) )  p->analyse.inter |= X264_ANALYSE_BSUB16x16;
+    }
+    OPT("8x8dct")
+        p->analyse.b_transform_8x8 = atobool(value);
+    OPT("weightb")
+        p->analyse.b_weighted_bipred = atobool(value);
+    OPT("direct")
+        b_error |= parse_enum( value, x264_direct_pred_names, &p->analyse.i_direct_mv_pred );
+    OPT("chroma-qp-offset")
+        p->analyse.i_chroma_qp_offset = atoi(value);
+    OPT("me")
+        b_error |= parse_enum( value, x264_motion_est_names, &p->analyse.i_me_method );
+    OPT("merange")
+        p->analyse.i_me_range = atoi(value);
+    OPT("mvrange")
+        p->analyse.i_mv_range = atoi(value);
+    OPT("subme")
+        p->analyse.i_subpel_refine = atoi(value);
+    OPT("bime")
+        p->analyse.b_bidir_me = atobool(value);
+    OPT("chroma-me")
+        p->analyse.b_chroma_me = atobool(value);
+    OPT("b-rdo")
+        p->analyse.b_bframe_rdo = atobool(value);
+    OPT("mixed-refs")
+        p->analyse.b_mixed_references = atobool(value);
+    OPT("trellis")
+        p->analyse.i_trellis = atoi(value);
+    OPT("fast-pskip")
+        p->analyse.b_fast_pskip = atobool(value);
+    OPT("dct-decimate")
+        p->analyse.b_dct_decimate = atobool(value);
+    OPT("nr")
+        p->analyse.i_noise_reduction = atoi(value);
+    OPT("bitrate")
+    {
+        p->rc.i_bitrate = atoi(value);
+        p->rc.i_rc_method = X264_RC_ABR;
+    }
+    OPT("qp")
+    {
+        p->rc.i_qp_constant = atoi(value);
+        p->rc.i_rc_method = X264_RC_CQP;
+    }
+    OPT("crf")
+    {
+        p->rc.i_rf_constant = atoi(value);
+        p->rc.i_rc_method = X264_RC_CRF;
+    }
+    OPT("qpmin")
+        p->rc.i_qp_min = atoi(value);
+    OPT("qpmax")
+        p->rc.i_qp_max = atoi(value);
+    OPT("qpstep")
+        p->rc.i_qp_step = atoi(value);
+    OPT("ratetol")
+        p->rc.f_rate_tolerance = !strncmp("inf", value, 3) ? 1e9 : atof(value);
+    OPT("vbv-maxrate")
+        p->rc.i_vbv_max_bitrate = atoi(value);
+    OPT("vbv-bufsize")
+        p->rc.i_vbv_buffer_size = atoi(value);
+    OPT("vbv-init")
+        p->rc.f_vbv_buffer_init = atof(value);
+    OPT("ipratio")
+        p->rc.f_ip_factor = atof(value);
+    OPT("pbratio")
+        p->rc.f_pb_factor = atof(value);
+    OPT("pass")
+    {
+        int i = x264_clip3( atoi(value), 0, 3 );
+        p->rc.b_stat_write = i & 1;
+        p->rc.b_stat_read = i & 2;
+    }
+    OPT("stats")
+    {
+        p->rc.psz_stat_in = strdup(value);
+        p->rc.psz_stat_out = strdup(value);
+    }
+    OPT("rceq")
+        p->rc.psz_rc_eq = strdup(value);
+    OPT("qcomp")
+        p->rc.f_qcompress = atof(value);
+    OPT("qblur")
+        p->rc.f_qblur = atof(value);
+    OPT("cplxblur")
+        p->rc.f_complexity_blur = atof(value);
+    OPT("zones")
+        p->rc.psz_zones = strdup(value);
+    OPT("psnr")
+        p->analyse.b_psnr = atobool(value);
+    OPT("aud")
+        p->b_aud = atobool(value);
+    OPT("sps-id")
+        p->i_sps_id = atoi(value);
+    OPT("repeat-headers")
+        p->b_repeat_headers = atobool(value);
+    else
+        return X264_PARAM_BAD_NAME;
+#undef OPT
+#undef atobool
+
+    return b_error ? X264_PARAM_BAD_VALUE : 0;
+}
+
 /****************************************************************************
  * x264_log:
  ****************************************************************************/
diff --git a/x264.c b/x264.c
index 63698cc0de4734214a4c5db00336d96d496a27d6..8bb16c742d607cf5f7a9544841d6ff2310f19d11 100644 (file)
--- a/x264.c
+++ b/x264.c
@@ -111,13 +111,6 @@ int main( int argc, char **argv )
     return Encode( &param, &opt );
 }
 
-static const char * const overscan_str[] = { "undef", "show", "crop", NULL };
-static const char * const vidformat_str[] = { "component", "pal", "ntsc", "secam", "mac", "undef", NULL };
-static const char * const fullrange_str[] = { "off", "on", NULL };
-static const char * const colorprim_str[] = { "", "bt709", "undef", "", "bt470m", "bt470bg", "smpte170m", "smpte240m", "film", NULL };
-static const char * const transfer_str[] = { "", "bt709", "undef", "", "bt470m", "bt470bg", "smpte170m", "smpte240m", "linear", "log100", "log316", NULL };
-static const char * const colmatrix_str[] = { "GBR", "bt709", "undef", "", "fcc", "bt470bg", "smpte170m", "smpte240m", "YCgCo", NULL };
-
 static char const *strtable_lookup( const char * const table[], int index )
 {
     int i = 0; while( table[i] ) i++;
@@ -321,41 +314,17 @@ static void Help( x264_param_t *defaults )
             defaults->analyse.i_subpel_refine,
             defaults->analyse.i_trellis,
             defaults->analyse.i_noise_reduction,
-            strtable_lookup( overscan_str, defaults->vui.i_overscan ),
-            strtable_lookup( vidformat_str, defaults->vui.i_vidformat ),
-            strtable_lookup( fullrange_str, defaults->vui.b_fullrange ),
-            strtable_lookup( colorprim_str, defaults->vui.i_colorprim ),
-            strtable_lookup( transfer_str, defaults->vui.i_transfer ),
-            strtable_lookup( colmatrix_str, defaults->vui.i_colmatrix ),
+            strtable_lookup( x264_overscan_names, defaults->vui.i_overscan ),
+            strtable_lookup( x264_vidformat_names, defaults->vui.i_vidformat ),
+            strtable_lookup( x264_fullrange_names, defaults->vui.b_fullrange ),
+            strtable_lookup( x264_colorprim_names, defaults->vui.i_colorprim ),
+            strtable_lookup( x264_transfer_names, defaults->vui.i_transfer ),
+            strtable_lookup( x264_colmatrix_names, defaults->vui.i_colmatrix ),
             defaults->vui.i_chroma_loc,
             defaults->i_sps_id
            );
 }
 
-static int parse_enum( const char *arg, const char * const *names, int *dst )
-{
-    int i;
-    for( i = 0; names[i]; i++ )
-        if( !strcmp( arg, names[i] ) )
-        {
-            *dst = i;
-            return 0;
-        }
-    return -1;
-}
-
-static int parse_cqm( const char *str, uint8_t *cqm, int length )
-{
-    int i = 0;
-    do {
-        int coef;
-        if( !sscanf( str, "%d", &coef ) || coef < 1 || coef > 255 )
-            return -1;
-        cqm[i++] = coef;
-    } while( i < length && (str = strchr( str, ',' )) && str++ );
-    return (i == length) ? 0 : -1;
-}
-
 /*****************************************************************************
  * Parse:
  *****************************************************************************/
@@ -385,169 +354,109 @@ static int  Parse( int argc, char **argv,
     p_close_outfile = close_file_bsf;
 
     /* Parse command line options */
-    opterr = 0; // no error message
     for( ;; )
     {
         int b_error = 0;
-        int long_options_index;
-#define OPT_QPMIN 256
-#define OPT_QPMAX 257
-#define OPT_QPSTEP 258
-#define OPT_IPRATIO 260
-#define OPT_PBRATIO 261
-#define OPT_RATETOL 262
-#define OPT_RCSTATS 264
-#define OPT_RCEQ 265
-#define OPT_QCOMP 266
-#define OPT_NOPSNR 267
-#define OPT_QUIET 268
-#define OPT_SCENECUT 270
-#define OPT_QBLUR 271
-#define OPT_CPLXBLUR 272
-#define OPT_FRAMES 273
-#define OPT_FPS 274
-#define OPT_DIRECT 275
-#define OPT_LEVEL 276
-#define OPT_NOBADAPT 277
-#define OPT_BBIAS 278
-#define OPT_BPYRAMID 279
-#define OPT_CHROMA_QP 280
-#define OPT_NO_CHROMA_ME 281
-#define OPT_NO_CABAC 282
-#define OPT_AUD 283
-#define OPT_PROGRESS 284
-#define OPT_ME 285
-#define OPT_MERANGE 286
-#define OPT_VBVMAXRATE 287
-#define OPT_VBVBUFSIZE 288
-#define OPT_VBVINIT 289
-#define OPT_VISUALIZE 290
-#define OPT_SEEK 291
-#define OPT_ZONES 292
-#define OPT_THREADS 293
-#define OPT_CQM 294
-#define OPT_CQM4 295
-#define OPT_CQM4I 296
-#define OPT_CQM4IY 297
-#define OPT_CQM4IC 298
-#define OPT_CQM4P 299
-#define OPT_CQM4PY 300
-#define OPT_CQM4PC 301
-#define OPT_CQM8 302
-#define OPT_CQM8I 303
-#define OPT_CQM8P 304
-#define OPT_CQMFILE 305
-#define OPT_SAR 306
-#define OPT_OVERSCAN 307
-#define OPT_VIDFORMAT 308
-#define OPT_FULLRANGE 309
-#define OPT_COLOURPRIM 310
-#define OPT_TRANSFER 311
-#define OPT_COLOURMATRIX 312
-#define OPT_CHROMALOC 313
-#define OPT_MIXED_REFS 314
-#define OPT_CRF 315
-#define OPT_B_RDO 316
-#define OPT_NO_FAST_PSKIP 317
-#define OPT_BIME 318
-#define OPT_NR 319
-#define OPT_THREAD_INPUT 320
-#define OPT_NO_DCT_DECIMATE 321
-#define OPT_SPS_ID 322
-#define OPT_QPFILE 323
+        int long_options_index = -1;
+
+#define OPT_FRAMES 256
+#define OPT_SEEK 257
+#define OPT_QPFILE 258
+#define OPT_THREAD_INPUT 259
+#define OPT_QUIET 260
+#define OPT_PROGRESS 261
+#define OPT_VISUALIZE 262
 
         static struct option long_options[] =
         {
             { "help",    no_argument,       NULL, 'h' },
             { "bitrate", required_argument, NULL, 'B' },
             { "bframes", required_argument, NULL, 'b' },
-            { "no-b-adapt", no_argument,    NULL, OPT_NOBADAPT },
-            { "b-bias",  required_argument, NULL, OPT_BBIAS },
-            { "b-pyramid", no_argument,     NULL, OPT_BPYRAMID },
+            { "no-b-adapt", no_argument,    NULL, 0 },
+            { "b-bias",  required_argument, NULL, 0 },
+            { "b-pyramid", no_argument,     NULL, 0 },
             { "min-keyint",required_argument,NULL,'i' },
             { "keyint",  required_argument, NULL, 'I' },
-            { "scenecut",required_argument, NULL, OPT_SCENECUT },
-            { "nf",      no_argument,       NULL, 'n' },
+            { "scenecut",required_argument, NULL, 0 },
+            { "nf",      no_argument,       NULL, 0 },
             { "filter",  required_argument, NULL, 'f' },
-            { "no-cabac",no_argument,       NULL, OPT_NO_CABAC },
+            { "no-cabac",no_argument,       NULL, 0 },
             { "qp",      required_argument, NULL, 'q' },
-            { "qpmin",   required_argument, NULL, OPT_QPMIN },
-            { "qpmax",   required_argument, NULL, OPT_QPMAX },
-            { "qpstep",  required_argument, NULL, OPT_QPSTEP },
-            { "crf",     required_argument, NULL, OPT_CRF },
+            { "qpmin",   required_argument, NULL, 0 },
+            { "qpmax",   required_argument, NULL, 0 },
+            { "qpstep",  required_argument, NULL, 0 },
+            { "crf",     required_argument, NULL, 0 },
             { "ref",     required_argument, NULL, 'r' },
-            { "no-asm",  no_argument,       NULL, 'C' },
-            { "sar",     required_argument, NULL, OPT_SAR },
-            { "fps",     required_argument, NULL, OPT_FPS },
+            { "no-asm",  no_argument,       NULL, 0 },
+            { "sar",     required_argument, NULL, 0 },
+            { "fps",     required_argument, NULL, 0 },
             { "frames",  required_argument, NULL, OPT_FRAMES },
             { "seek",    required_argument, NULL, OPT_SEEK },
             { "output",  required_argument, NULL, 'o' },
             { "analyse", required_argument, NULL, 'A' },
-            { "direct",  required_argument, NULL, OPT_DIRECT },
+            { "direct",  required_argument, NULL, 0 },
             { "weightb", no_argument,       NULL, 'w' },
-            { "me",      required_argument, NULL, OPT_ME },
-            { "merange", required_argument, NULL, OPT_MERANGE },
+            { "me",      required_argument, NULL, 0 },
+            { "merange", required_argument, NULL, 0 },
             { "subme",   required_argument, NULL, 'm' },
-            { "b-rdo",   no_argument,       NULL, OPT_B_RDO },
-            { "mixed-refs", no_argument,    NULL, OPT_MIXED_REFS },
-            { "no-chroma-me", no_argument,  NULL, OPT_NO_CHROMA_ME },
-            { "bime",    no_argument,       NULL, OPT_BIME },
+            { "b-rdo",   no_argument,       NULL, 0 },
+            { "mixed-refs", no_argument,    NULL, 0 },
+            { "no-chroma-me", no_argument,  NULL, 0 },
+            { "bime",    no_argument,       NULL, 0 },
             { "8x8dct",  no_argument,       NULL, '8' },
             { "trellis", required_argument, NULL, 't' },
-            { "no-fast-pskip", no_argument, NULL, OPT_NO_FAST_PSKIP },
-            { "no-dct-decimate", no_argument, NULL, OPT_NO_DCT_DECIMATE },
-            { "level",   required_argument, NULL, OPT_LEVEL },
-            { "ratetol", required_argument, NULL, OPT_RATETOL },
-            { "vbv-maxrate", required_argument, NULL, OPT_VBVMAXRATE },
-            { "vbv-bufsize", required_argument, NULL, OPT_VBVBUFSIZE },
-            { "vbv-init", required_argument,NULL,  OPT_VBVINIT },
-            { "ipratio", required_argument, NULL, OPT_IPRATIO },
-            { "pbratio", required_argument, NULL, OPT_PBRATIO },
-            { "chroma-qp-offset", required_argument, NULL, OPT_CHROMA_QP },
+            { "no-fast-pskip", no_argument, NULL, 0 },
+            { "no-dct-decimate", no_argument, NULL, 0 },
+            { "level",   required_argument, NULL, 0 },
+            { "ratetol", required_argument, NULL, 0 },
+            { "vbv-maxrate", required_argument, NULL, 0 },
+            { "vbv-bufsize", required_argument, NULL, 0 },
+            { "vbv-init", required_argument,NULL,  0 },
+            { "ipratio", required_argument, NULL, 0 },
+            { "pbratio", required_argument, NULL, 0 },
+            { "chroma-qp-offset", required_argument, NULL, 0 },
             { "pass",    required_argument, NULL, 'p' },
-            { "stats",   required_argument, NULL, OPT_RCSTATS },
-            { "rceq",    required_argument, NULL, OPT_RCEQ },
-            { "qcomp",   required_argument, NULL, OPT_QCOMP },
-            { "qblur",   required_argument, NULL, OPT_QBLUR },
-            { "cplxblur",required_argument, NULL, OPT_CPLXBLUR },
-            { "zones",   required_argument, NULL, OPT_ZONES },
+            { "stats",   required_argument, NULL, 0 },
+            { "rceq",    required_argument, NULL, 0 },
+            { "qcomp",   required_argument, NULL, 0 },
+            { "qblur",   required_argument, NULL, 0 },
+            { "cplxblur",required_argument, NULL, 0 },
+            { "zones",   required_argument, NULL, 0 },
             { "qpfile",  required_argument, NULL, OPT_QPFILE },
-            { "threads", required_argument, NULL, OPT_THREADS },
+            { "threads", required_argument, NULL, 0 },
             { "thread-input", no_argument,  NULL, OPT_THREAD_INPUT },
-            { "no-psnr", no_argument,       NULL, OPT_NOPSNR },
+            { "no-psnr", no_argument,       NULL, 0 },
             { "quiet",   no_argument,       NULL, OPT_QUIET },
             { "verbose", no_argument,       NULL, 'v' },
             { "progress",no_argument,       NULL, OPT_PROGRESS },
             { "visualize",no_argument,      NULL, OPT_VISUALIZE },
-            { "sps-id",  required_argument, NULL, OPT_SPS_ID },
-            { "aud",     no_argument,       NULL, OPT_AUD },
-            { "nr",      required_argument, NULL, OPT_NR },
-            { "cqm",     required_argument, NULL, OPT_CQM },
-            { "cqmfile", required_argument, NULL, OPT_CQMFILE },
-            { "cqm4",    required_argument, NULL, OPT_CQM4 },
-            { "cqm4i",   required_argument, NULL, OPT_CQM4I },
-            { "cqm4iy",  required_argument, NULL, OPT_CQM4IY },
-            { "cqm4ic",  required_argument, NULL, OPT_CQM4IC },
-            { "cqm4p",   required_argument, NULL, OPT_CQM4P },
-            { "cqm4py",  required_argument, NULL, OPT_CQM4PY },
-            { "cqm4pc",  required_argument, NULL, OPT_CQM4PC },
-            { "cqm8",    required_argument, NULL, OPT_CQM8 },
-            { "cqm8i",   required_argument, NULL, OPT_CQM8I },
-            { "cqm8p",   required_argument, NULL, OPT_CQM8P },
-            { "overscan", required_argument, NULL, OPT_OVERSCAN },
-            { "videoformat", required_argument, NULL, OPT_VIDFORMAT },
-            { "fullrange", required_argument, NULL, OPT_FULLRANGE },
-            { "colorprim", required_argument, NULL, OPT_COLOURPRIM },
-            { "transfer", required_argument, NULL, OPT_TRANSFER },
-            { "colormatrix", required_argument, NULL, OPT_COLOURMATRIX },
-            { "chromaloc", required_argument, NULL, OPT_CHROMALOC },
+            { "sps-id",  required_argument, NULL, 0 },
+            { "aud",     no_argument,       NULL, 0 },
+            { "nr",      required_argument, NULL, 0 },
+            { "cqm",     required_argument, NULL, 0 },
+            { "cqmfile", required_argument, NULL, 0 },
+            { "cqm4",    required_argument, NULL, 0 },
+            { "cqm4i",   required_argument, NULL, 0 },
+            { "cqm4iy",  required_argument, NULL, 0 },
+            { "cqm4ic",  required_argument, NULL, 0 },
+            { "cqm4p",   required_argument, NULL, 0 },
+            { "cqm4py",  required_argument, NULL, 0 },
+            { "cqm4pc",  required_argument, NULL, 0 },
+            { "cqm8",    required_argument, NULL, 0 },
+            { "cqm8i",   required_argument, NULL, 0 },
+            { "cqm8p",   required_argument, NULL, 0 },
+            { "overscan", required_argument, NULL, 0 },
+            { "videoformat", required_argument, NULL, 0 },
+            { "fullrange", required_argument, NULL, 0 },
+            { "colorprim", required_argument, NULL, 0 },
+            { "transfer", required_argument, NULL, 0 },
+            { "colormatrix", required_argument, NULL, 0 },
+            { "chromaloc", required_argument, NULL, 0 },
             {0, 0, 0, 0}
         };
 
-        int c;
-
-        c = getopt_long( argc, argv, "hi:I:b:r:cxB:q:f:o:A:m:p:t:vw8",
-                         long_options, &long_options_index);
+        int c = getopt_long( argc, argv, "8A:B:b:f:hI:i:m:o:p:q:r:t:vw",
+                             long_options, &long_options_index);
 
         if( c == -1 )
         {
@@ -559,78 +468,6 @@ static int  Parse( int argc, char **argv,
             case 'h':
                 Help( &defaults );
                 return -1;
-
-            case 0:
-                break;
-            case 'B':
-                param->rc.i_bitrate = atol( optarg );
-                param->rc.i_rc_method = X264_RC_ABR;
-                break;
-            case OPT_CRF:
-                param->rc.i_rf_constant = atol( optarg );
-                param->rc.i_rc_method = X264_RC_CRF;
-                break;
-            case 'b':
-                param->i_bframe = atol( optarg );
-                break;
-            case OPT_NOBADAPT:
-                param->b_bframe_adaptive = 0;
-                break;
-            case OPT_BBIAS:
-                param->i_bframe_bias = atol( optarg );
-                break;
-            case OPT_BPYRAMID:
-                param->b_bframe_pyramid = 1;
-                break;
-            case 'i':
-                param->i_keyint_min = atol( optarg );
-                if( param->i_keyint_max < param->i_keyint_min )
-                    param->i_keyint_max = param->i_keyint_min;
-                break;
-            case 'I':
-                param->i_keyint_max = atol( optarg );
-                if( param->i_keyint_min > param->i_keyint_max )
-                    param->i_keyint_min = param->i_keyint_max;
-                break;
-            case OPT_SCENECUT:
-                param->i_scenecut_threshold = atol( optarg );
-                break;
-            case 'n':
-                param->b_deblocking_filter = 0;
-                break;
-            case 'f':
-            {
-                char *p = strchr( optarg, ':' );
-                if( !p ) p = strchr( optarg, ',' );
-                param->i_deblocking_filter_alphac0 = atoi( optarg );
-                param->i_deblocking_filter_beta = p ? atoi( p+1 ) : param->i_deblocking_filter_alphac0;
-                break;
-            }
-            case 'q':
-                param->rc.i_qp_constant = atoi( optarg );
-                param->rc.i_rc_method = X264_RC_CQP;
-                break;
-            case OPT_QPMIN:
-                param->rc.i_qp_min = atoi( optarg );
-                break;
-            case OPT_QPMAX:
-                param->rc.i_qp_max = atoi( optarg );
-                break;
-            case OPT_QPSTEP:
-                param->rc.i_qp_step = atoi( optarg );
-                break;
-            case 'r':
-                param->i_frame_reference = atoi( optarg );
-                break;
-            case OPT_NO_CABAC:
-                param->b_cabac = 0;
-                break;
-            case 'x':
-                opt->b_decompress = 1;
-                break;
-            case 'C':
-                param->cpu = 0;
-                break;
             case OPT_FRAMES:
                 param->i_frame_total = atoi( optarg );
                 break;
@@ -667,142 +504,6 @@ static int  Parse( int argc, char **argv,
                     return -1;
                 }
                 break;
-            case OPT_SAR:
-            {
-                char *p = strchr( optarg, ':' );
-                if( !p ) p = strchr( optarg, '/' );
-                if( p )
-                {
-                    param->vui.i_sar_width = atoi( optarg );
-                    param->vui.i_sar_height = atoi( p + 1 );
-                }
-                break;
-            }
-            case OPT_FPS:
-            {
-                float fps;
-                if( sscanf( optarg, "%d/%d", &param->i_fps_num, &param->i_fps_den ) == 2 )
-                    ;
-                else if( sscanf( optarg, "%f", &fps ) )
-                {
-                    param->i_fps_num = (int)(fps * 1000 + .5);
-                    param->i_fps_den = 1000;
-                }
-                else
-                {
-                    fprintf( stderr, "bad fps `%s'\n", optarg );
-                    return -1;
-                }
-                break;
-            }
-            case 'A':
-                param->analyse.inter = 0;
-                if( strstr( optarg, "none" ) )  param->analyse.inter =  0;
-                if( strstr( optarg, "all" ) )   param->analyse.inter = ~0;
-
-                if( strstr( optarg, "i4x4" ) )  param->analyse.inter |= X264_ANALYSE_I4x4;
-                if( strstr( optarg, "i8x8" ) )  param->analyse.inter |= X264_ANALYSE_I8x8;
-                if( strstr( optarg, "p8x8" ) )  param->analyse.inter |= X264_ANALYSE_PSUB16x16;
-                if( strstr( optarg, "p4x4" ) )  param->analyse.inter |= X264_ANALYSE_PSUB8x8;
-                if( strstr( optarg, "b8x8" ) )  param->analyse.inter |= X264_ANALYSE_BSUB16x16;
-                break;
-            case OPT_DIRECT:
-                b_error |= parse_enum( optarg, x264_direct_pred_names, &param->analyse.i_direct_mv_pred );
-                break;
-            case 'w':
-                param->analyse.b_weighted_bipred = 1;
-                break;
-            case OPT_ME:
-                b_error |= parse_enum( optarg, x264_motion_est_names, &param->analyse.i_me_method );
-                break;
-            case OPT_MERANGE:
-                param->analyse.i_me_range = atoi(optarg);
-                break;
-            case 'm':
-                param->analyse.i_subpel_refine = atoi(optarg);
-                break;
-            case OPT_B_RDO:
-                param->analyse.b_bframe_rdo = 1;
-                break;
-            case OPT_MIXED_REFS:
-                param->analyse.b_mixed_references = 1;
-                break;
-            case OPT_NO_CHROMA_ME:
-                param->analyse.b_chroma_me = 0;
-                break;
-            case OPT_BIME:
-                param->analyse.b_bidir_me = 1;
-                break;
-            case '8':
-                param->analyse.b_transform_8x8 = 1;
-                break;
-            case 't':
-                param->analyse.i_trellis = atoi(optarg);
-                break;
-            case OPT_NO_FAST_PSKIP:
-                param->analyse.b_fast_pskip = 0;
-                break;
-            case OPT_NO_DCT_DECIMATE:
-                param->analyse.b_dct_decimate = 0;
-                break;
-            case OPT_LEVEL:
-                if( atof(optarg) < 6 )
-                    param->i_level_idc = (int)(10*atof(optarg)+.5);
-                else
-                    param->i_level_idc = atoi(optarg);
-                break;
-            case OPT_RATETOL:
-                param->rc.f_rate_tolerance = !strncmp("inf", optarg, 3) ? 1e9 : atof(optarg);
-                break;
-            case OPT_VBVMAXRATE:
-                param->rc.i_vbv_max_bitrate = atoi( optarg );
-                break;
-            case OPT_VBVBUFSIZE:
-                param->rc.i_vbv_buffer_size = atoi( optarg );
-                break;
-            case OPT_VBVINIT:
-                param->rc.f_vbv_buffer_init = atof(optarg);
-                break;
-            case OPT_IPRATIO:
-                param->rc.f_ip_factor = atof(optarg);
-                break;
-            case OPT_PBRATIO:
-                param->rc.f_pb_factor = atof(optarg);
-                break;
-            case OPT_CHROMA_QP:
-                param->analyse.i_chroma_qp_offset = atoi(optarg);
-                break;
-            case 'p':
-            {
-                int i_pass = atoi(optarg);
-                if( i_pass == 1 )
-                    param->rc.b_stat_write = 1;
-                else if( i_pass == 2 )
-                    param->rc.b_stat_read = 1;
-                else if( i_pass > 2 )
-                    param->rc.b_stat_read =
-                    param->rc.b_stat_write = 1;
-                break;
-            }
-            case OPT_RCSTATS:
-                param->rc.psz_stat_in = optarg;
-                param->rc.psz_stat_out = optarg;
-                break;
-            case OPT_RCEQ:
-                param->rc.psz_rc_eq = optarg;
-               break;
-            case OPT_QCOMP:
-                param->rc.f_qcompress = atof(optarg);
-                break;
-            case OPT_QBLUR:
-                param->rc.f_qblur = atof(optarg);
-                break;
-            case OPT_CPLXBLUR:
-                param->rc.f_complexity_blur = atof(optarg);
-                break;
-            case OPT_ZONES:
-                param->rc.psz_zones = optarg;
-                break;
             case OPT_QPFILE:
                 opt->qpfile = fopen( optarg, "r" );
                 if( !opt->qpfile )
@@ -813,27 +514,16 @@ static int  Parse( int argc, char **argv,
                 param->i_scenecut_threshold = -1;
                 param->b_bframe_adaptive = 0;
                 break;
-            case OPT_THREADS:
-                param->i_threads = atoi(optarg);
-                break;
             case OPT_THREAD_INPUT:
                 b_thread_input = 1;
                 break;
-            case OPT_NOPSNR:
-                param->analyse.b_psnr = 0;
-                break;
             case OPT_QUIET:
                 param->i_log_level = X264_LOG_NONE;
+                param->analyse.b_psnr = 0;
                 break;
             case 'v':
                 param->i_log_level = X264_LOG_DEBUG;
                 break;
-            case OPT_SPS_ID:
-                param->i_sps_id = atoi(optarg);
-                break;
-            case OPT_AUD:
-                param->b_aud = 1;
-                break;
             case OPT_PROGRESS:
                 opt->b_progress = 1;
                 break;
@@ -844,100 +534,32 @@ static int  Parse( int argc, char **argv,
 #else
                 fprintf( stderr, "not compiled with visualization support\n" );
 #endif
-                break;
-            case OPT_NR:
-                param->analyse.i_noise_reduction = atoi(optarg);
-                break;
-            case OPT_CQM:
-                if( strstr( optarg, "flat" ) )
-                    param->i_cqm_preset = X264_CQM_FLAT;
-                else if( strstr( optarg, "jvt" ) )
-                    param->i_cqm_preset = X264_CQM_JVT;
-                else
+            default:
+            {
+                int i;
+                if( long_options_index < 0 )
                 {
-                    fprintf( stderr, "bad CQM preset `%s'\n", optarg );
-                    return -1;
+                    for( i = 0; long_options[i].name; i++ )
+                        if( long_options[i].val == c )
+                        {
+                            long_options_index = i;
+                            break;
+                        }
+                    if( long_options_index < 0 )
+                    {
+                        /* getopt_long already printed an error message */
+                        return -1;
+                    }
                 }
-                break;
-            case OPT_CQMFILE:
-                param->psz_cqm_file = optarg;
-                break;
-            case OPT_CQM4:
-                param->i_cqm_preset = X264_CQM_CUSTOM;
-                b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
-                b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
-                b_error |= parse_cqm( optarg, param->cqm_4py, 16 );
-                b_error |= parse_cqm( optarg, param->cqm_4pc, 16 );
-                break;
-            case OPT_CQM8:
-                param->i_cqm_preset = X264_CQM_CUSTOM;
-                b_error |= parse_cqm( optarg, param->cqm_8iy, 64 );
-                b_error |= parse_cqm( optarg, param->cqm_8py, 64 );
-                break;
-            case OPT_CQM4I:
-                param->i_cqm_preset = X264_CQM_CUSTOM;
-                b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
-                b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
-                break;
-            case OPT_CQM4P:
-                param->i_cqm_preset = X264_CQM_CUSTOM;
-                b_error |= parse_cqm( optarg, param->cqm_4py, 16 );
-                b_error |= parse_cqm( optarg, param->cqm_4pc, 16 );
-                break;
-            case OPT_CQM4IY:
-                param->i_cqm_preset = X264_CQM_CUSTOM;
-                b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
-                break;
-            case OPT_CQM4IC:
-                param->i_cqm_preset = X264_CQM_CUSTOM;
-                b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
-                break;
-            case OPT_CQM4PY:
-                param->i_cqm_preset = X264_CQM_CUSTOM;
-                b_error |= parse_cqm( optarg, param->cqm_4py, 16 );
-                break;
-            case OPT_CQM4PC:
-                param->i_cqm_preset = X264_CQM_CUSTOM;
-                b_error |= parse_cqm( optarg, param->cqm_4pc, 16 );
-                break;
-            case OPT_CQM8I:
-                param->i_cqm_preset = X264_CQM_CUSTOM;
-                b_error |= parse_cqm( optarg, param->cqm_8iy, 64 );
-                break;
-            case OPT_CQM8P:
-                param->i_cqm_preset = X264_CQM_CUSTOM;
-                b_error |= parse_cqm( optarg, param->cqm_8py, 64 );
-                break;
-            case OPT_OVERSCAN:
-                b_error |= parse_enum( optarg, overscan_str, &param->vui.i_overscan );
-                break;
-            case OPT_VIDFORMAT:
-                b_error |= parse_enum( optarg, vidformat_str, &param->vui.i_vidformat );
-                break;
-            case OPT_FULLRANGE:
-                b_error |= parse_enum( optarg, fullrange_str, &param->vui.b_fullrange );
-                break;
-            case OPT_COLOURPRIM:
-                b_error |= parse_enum( optarg, colorprim_str, &param->vui.i_colorprim );
-                break;
-            case OPT_TRANSFER:
-                b_error |= parse_enum( optarg, transfer_str, &param->vui.i_transfer );
-                break;
-            case OPT_COLOURMATRIX:
-                b_error |= parse_enum( optarg, colmatrix_str, &param->vui.i_colmatrix );
-                break;
-            case OPT_CHROMALOC:
-                param->vui.i_chroma_loc = atoi( optarg );
-                b_error = ( param->vui.i_chroma_loc < 0 || param->vui.i_chroma_loc > 5 );
-                break;
-            default:
-                fprintf( stderr, "unknown option (%c)\n", optopt );
-                return -1;
+
+                b_error |= x264_param_parse( param, long_options[long_options_index].name, optarg ? optarg : "true" );
+            }
         }
 
         if( b_error )
         {
-            fprintf( stderr, "bad argument: %s %s\n", argv[optind-2], optarg );
+            const char *name = long_options_index > 0 ? long_options[long_options_index].name : argv[optind-2];
+            fprintf( stderr, "x264 [error]: invalid argument: %s = %s\n", name, optarg );
             return -1;
         }
     }
diff --git a/x264.h b/x264.h
index f2ff831eddc051c48dc476965c30196bf4672e95..f1d947d97b84e8d7bdd2522755173b4a9c2e3dcb 100644 (file)
--- a/x264.h
+++ b/x264.h
@@ -35,7 +35,7 @@
 
 #include <stdarg.h>
 
-#define X264_BUILD 48
+#define X264_BUILD 49
 
 /* x264_t:
  *      opaque handler for decoder and encoder */
@@ -79,6 +79,12 @@ typedef struct x264_t x264_t;
 
 static const char * const x264_direct_pred_names[] = { "none", "spatial", "temporal", "auto", 0 };
 static const char * const x264_motion_est_names[] = { "dia", "hex", "umh", "esa", 0 };
+static const char * const x264_overscan_names[] = { "undef", "show", "crop", 0 };
+static const char * const x264_vidformat_names[] = { "component", "pal", "ntsc", "secam", "mac", "undef", 0 };
+static const char * const x264_fullrange_names[] = { "off", "on", 0 };
+static const char * const x264_colorprim_names[] = { "", "bt709", "undef", "", "bt470m", "bt470bg", "smpte170m", "smpte240m", "film", 0 };
+static const char * const x264_transfer_names[] = { "", "bt709", "undef", "", "bt470m", "bt470bg", "smpte170m", "smpte240m", "linear", "log100", "log316", 0 };
+static const char * const x264_colmatrix_names[] = { "GBR", "bt709", "undef", "", "fcc", "bt470bg", "smpte170m", "smpte240m", "YCgCo", 0 };
 
 /* Colorspace type
  */
@@ -276,6 +282,15 @@ extern const x264_level_t x264_levels[];
  *      fill x264_param_t with default values and do CPU detection */
 void    x264_param_default( x264_param_t * );
 
+/* x264_param_parse:
+ *      set one parameter by name.
+ *      returns 0 on success, or returns one of the following errors.
+ *      note: bad value occurs only if it can't even parse the value,
+ *      numerical range is not checked until x264_encoder_open() or x264_encoder_reconfig(). */
+#define X264_PARAM_BAD_NAME  (-1)
+#define X264_PARAM_BAD_VALUE (-2)
+int x264_param_parse( x264_param_t *, const char *name, const char *value );
+
 /****************************************************************************
  * Picture structures and functions.
  ****************************************************************************/