K-means weightp
[x264-7mod.git] / filters / filters.c
blob9212a3e091220f018a4522d7fb81a5a5a3ef9e47
1 /*****************************************************************************
2 * filters.c: common filter functions
3 *****************************************************************************
4 * Copyright (C) 2010-2017 x264 project
6 * Authors: Diogo Franco <diogomfranco@gmail.com>
7 * Steven Walters <kemuri9@gmail.com>
8 * Henrik Gramner <henrik@gramner.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
24 * This program is also available under a commercial proprietary license.
25 * For more information, contact us at licensing@x264.com.
26 *****************************************************************************/
28 #include "filters.h"
29 #define RETURN_IF_ERROR( cond, ... ) RETURN_IF_ERR( cond, "options", NULL, __VA_ARGS__ )
31 char **x264_split_options( const char *opt_str, const char * const *options )
33 int opt_count = 0, options_count = 0, found_named = 0, size = 0;
34 const char *opt = opt_str;
36 if( !opt_str )
37 return NULL;
39 while( options[options_count] )
40 options_count++;
44 int length = strcspn( opt, "=," );
45 if( opt[length] == '=' )
47 const char * const *option = options;
48 while( *option && (strlen( *option ) != length || strncmp( opt, *option, length )) )
49 option++;
51 RETURN_IF_ERROR( !*option, "Invalid option '%.*s'\n", length, opt );
52 found_named = 1;
53 length += strcspn( opt + length, "," );
55 else
57 RETURN_IF_ERROR( opt_count >= options_count, "Too many options given\n" );
58 RETURN_IF_ERROR( found_named, "Ordered option given after named\n" );
59 size += strlen( options[opt_count] ) + 1;
61 opt_count++;
62 opt += length;
63 } while( *opt++ );
65 int offset = 2 * (opt_count+1) * sizeof(char*);
66 size += offset + (opt - opt_str);
67 char **opts = calloc( 1, size );
68 RETURN_IF_ERROR( !opts, "malloc failed\n" );
70 #define insert_opt( src, length )\
71 do {\
72 opts[i++] = memcpy( (char*)opts + offset, src, length );\
73 offset += length + 1;\
74 src += length + 1;\
75 } while( 0 )
77 for( int i = 0; i < 2*opt_count; )
79 int length = strcspn( opt_str, "=," );
80 if( opt_str[length] == '=' )
82 insert_opt( opt_str, length );
83 length = strcspn( opt_str, "," );
85 else
87 const char *option = options[i/2];
88 int option_length = strlen( option );
89 insert_opt( option, option_length );
91 insert_opt( opt_str, length );
94 assert( offset == size );
95 return opts;
98 char *x264_get_option( const char *name, char **split_options )
100 if( split_options )
102 int last_i = -1;
103 for( int i = 0; split_options[i]; i += 2 )
104 if( !strcmp( split_options[i], name ) )
105 last_i = i;
106 if( last_i >= 0 && split_options[last_i+1][0] )
107 return split_options[last_i+1];
109 return NULL;
112 int x264_otob( const char *str, int def )
114 if( str )
115 return !strcasecmp( str, "true" ) || !strcmp( str, "1" ) || !strcasecmp( str, "yes" );
116 return def;
119 double x264_otof( const char *str, double def )
121 double ret = def;
122 if( str )
124 char *end;
125 ret = strtod( str, &end );
126 if( end == str || *end != '\0' )
127 ret = def;
129 return ret;
132 int x264_otoi( const char *str, int def )
134 int ret = def;
135 if( str )
137 char *end;
138 ret = strtol( str, &end, 0 );
139 if( end == str || *end != '\0' )
140 ret = def;
142 return ret;
145 char *x264_otos( char *str, char *def )
147 return str ? str : def;