From: Loren Merritt Date: Mon, 2 Oct 2006 08:31:48 +0000 (+0000) Subject: better error checking in x264_param_parse. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=04834f596bb9bec1e8afd7ab7b1eee74ad13df0a;p=libx264 better error checking in x264_param_parse. add synonyms for a few options. git-svn-id: svn://svn.videolan.org/x264/trunk@577 df754926-b1dd-0310-bc7b-ec298dee348c --- diff --git a/common/common.c b/common/common.c index 79f2b7d8..f43bb7a6 100644 --- a/common/common.c +++ b/common/common.c @@ -164,7 +164,7 @@ static int parse_cqm( const char *str, uint8_t *cqm, int length ) return (i == length) ? 0 : -1; } -static int atobool( const char *str ) +static int x264_atobool( const char *str, int *b_error ) { if( !strcmp(str, "1") || !strcmp(str, "true") || @@ -174,32 +174,67 @@ static int atobool( const char *str ) !strcmp(str, "false") || !strcmp(str, "no") ) return 0; - return -1; + *b_error = 1; + return 0; +} + +static int x264_atoi( const char *str, int *b_error ) +{ + char *end; + int v = strtol( str, &end, 0 ); + if( end == str || *end != '\0' ) + *b_error = 1; + return v; +} + +static double x264_atof( const char *str, int *b_error ) +{ + char *end; + double v = strtod( str, &end ); + if( end == str || *end != '\0' ) + *b_error = 1; + return v; } -#define atobool(str) ( (i = atobool(str)) < 0 ? (b_error = 1) : i ) +#define atobool(str) ( name_was_bool = 1, x264_atobool( str, &b_error ) ) +#define atoi(str) x264_atoi( str, &b_error ) +#define atof(str) x264_atof( str, &b_error ) int x264_param_parse( x264_param_t *p, const char *name, const char *value ) { + char *name_buf = NULL; int b_error = 0; + int name_was_bool; + int value_was_null = !value; int i; if( !name ) return X264_PARAM_BAD_NAME; if( !value ) - return X264_PARAM_BAD_VALUE; + value = "true"; if( value[0] == '=' ) value++; + if( strchr( name, '_' ) ) // s/_/-/g + { + char *p; + name_buf = strdup(name); + while( (p = strchr( name_buf, '_' )) ) + *p = '-'; + name = name_buf; + } + if( (!strncmp( name, "no-", 3 ) && (i = 3)) || (!strncmp( name, "no", 2 ) && (i = 2)) ) { name += i; value = atobool(value) ? "false" : "true"; } + name_was_bool = 0; #define OPT(STR) else if( !strcmp( name, STR ) ) +#define OPT2(STR0, STR1) else if( !strcmp( name, STR0 ) || !strcmp( name, STR1 ) ) if(0); OPT("asm") p->cpu = atobool(value) ? x264_cpu_detect() : 0; @@ -241,16 +276,14 @@ int x264_param_parse( x264_param_t *p, const char *name, const char *value ) } OPT("fps") { - float fps; if( sscanf( value, "%d/%d", &p->i_fps_num, &p->i_fps_den ) == 2 ) ; - else if( sscanf( value, "%f", &fps ) ) + else { + float fps = atof(value); 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); @@ -278,7 +311,7 @@ int x264_param_parse( x264_param_t *p, const char *name, const char *value ) p->b_bframe_pyramid = atobool(value); OPT("nf") p->b_deblocking_filter = 0; - OPT("filter") + OPT2("filter", "deblock") { int count; if( 0 < (count = sscanf( value, "%d:%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta )) || @@ -366,7 +399,11 @@ int x264_param_parse( x264_param_t *p, const char *name, const char *value ) } OPT("log") p->i_log_level = atoi(value); - OPT("analyse") +#ifdef VISUALIZE + OPT("visualize") + p->b_visualize = atobool(value); +#endif + OPT2("analyse", "partitions") { p->analyse.inter = 0; if( strstr( value, "none" ) ) p->analyse.inter = 0; @@ -483,8 +520,15 @@ int x264_param_parse( x264_param_t *p, const char *name, const char *value ) else return X264_PARAM_BAD_NAME; #undef OPT +#undef OPT2 #undef atobool +#undef atoi +#undef atof + + if( name_buf ) + free( name_buf ); + b_error |= value_was_null && !name_was_bool; return b_error ? X264_PARAM_BAD_VALUE : 0; } diff --git a/x264.c b/x264.c index 37be0f13..1614aa87 100644 --- a/x264.c +++ b/x264.c @@ -162,8 +162,8 @@ static void Help( x264_param_t *defaults, int b_longhelp ) H0( " --b-pyramid Keep some B-frames as references\n" ); H0( " --no-cabac Disable CABAC\n" ); H0( " -r, --ref Number of reference frames [%d]\n", defaults->i_frame_reference ); - H1( " --nf Disable loop filter\n" ); - H0( " -f, --filter Loop filter AlphaC0 and Beta parameters [%d:%d]\n", + H1( " --no-deblock Disable loop filter\n" ); + H0( " -f, --deblock Loop filter AlphaC0 and Beta parameters [%d:%d]\n", defaults->i_deblocking_filter_alphac0, defaults->i_deblocking_filter_beta ); H0( " --interlaced Enable pure-interlaced mode\n" ); H0( "\n" ); @@ -202,7 +202,7 @@ static void Help( x264_param_t *defaults, int b_longhelp ) H0( "\n" ); H0( "Analysis:\n" ); H0( "\n" ); - H0( " -A, --analyse Partitions to consider [\"p8x8,b8x8,i8x8,i4x4\"]\n" + H0( " -A, --partitions Partitions to consider [\"p8x8,b8x8,i8x8,i4x4\"]\n" " - p8x8, p4x4, b8x8, i8x8, i4x4\n" " - none, all\n" " (p4x4 requires p8x8. i8x8 requires --8x8dct.)\n" ); @@ -362,7 +362,9 @@ static int Parse( int argc, char **argv, { "keyint", required_argument, NULL, 'I' }, { "scenecut",required_argument, NULL, 0 }, { "nf", no_argument, NULL, 0 }, - { "filter", required_argument, NULL, 'f' }, + { "nodeblock", no_argument, NULL, 0 }, + { "filter", required_argument, NULL, 0 }, + { "deblock", required_argument, NULL, 'f' }, { "interlaced", no_argument, NULL, 0 }, { "no-cabac",no_argument, NULL, 0 }, { "qp", required_argument, NULL, 'q' }, @@ -377,7 +379,8 @@ static int Parse( int argc, char **argv, { "frames", required_argument, NULL, OPT_FRAMES }, { "seek", required_argument, NULL, OPT_SEEK }, { "output", required_argument, NULL, 'o' }, - { "analyse", required_argument, NULL, 'A' }, + { "analyse", required_argument, NULL, 0 }, + { "partitions", required_argument, NULL, 'A' }, { "direct", required_argument, NULL, 0 }, { "direct-8x8", required_argument, NULL, 0 }, { "weightb", no_argument, NULL, 'w' }, @@ -550,7 +553,7 @@ static int Parse( int argc, char **argv, } } - b_error |= x264_param_parse( param, long_options[long_options_index].name, optarg ? optarg : "true" ); + b_error |= x264_param_parse( param, long_options[long_options_index].name, optarg ); } } diff --git a/x264.h b/x264.h index 816eb2fb..221f49c7 100644 --- a/x264.h +++ b/x264.h @@ -35,7 +35,7 @@ #include -#define X264_BUILD 52 +#define X264_BUILD 53 /* x264_t: * opaque handler for decoder and encoder */ @@ -290,10 +290,12 @@ extern const x264_level_t x264_levels[]; 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(). */ + * 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(). + * value=NULL means "true" for boolean options, but is a BAD_VALUE for non-booleans. */ #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 );