1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #include "common/args.h"
13
14 #include <stdlib.h>
15 #include <string.h>
16 #include <limits.h>
17
18 #include "aom/aom_integer.h"
19 #include "aom_ports/msvc.h"
20 #include "aom/aom_codec.h"
21
22 #if defined(__GNUC__) && __GNUC__
23 extern void die(const char *fmt, ...) __attribute__((noreturn));
24 #else
25 extern void die(const char *fmt, ...);
26 #endif
27
arg_init(char ** argv)28 struct arg arg_init(char **argv) {
29 struct arg a;
30
31 a.argv = argv;
32 a.argv_step = 1;
33 a.name = NULL;
34 a.val = NULL;
35 a.def = NULL;
36 return a;
37 }
38
ignore_front_spaces(const char * str)39 char *ignore_front_spaces(const char *str) {
40 while (str[0] == ' ' || str[0] == '\t') ++str;
41 return (char *)str;
42 }
43
ignore_end_spaces(char * str)44 void ignore_end_spaces(char *str) {
45 char *end = str + strlen(str);
46 while (end > str && (end[0] == ' ' || end[0] == '\t' || end[0] == '\n' ||
47 end[0] == '\r' || end[0] == '\0'))
48 --end;
49 if (end >= str) end[1] = '\0';
50 }
51
52 static const char kSbSizeWarningString[] =
53 "super_block_size has to be 64 or 128.";
54 static const char kMinpartWarningString[] =
55 "min_partition_size has to be smaller or equal to max_partition_size.";
56 static const char kMaxpartWarningString[] =
57 "max_partition_size has to be smaller or equal to super_block_size.";
58
parse_cfg(const char * file,cfg_options_t * config)59 int parse_cfg(const char *file, cfg_options_t *config) {
60 char line[1024 * 10];
61 FILE *f = fopen(file, "r");
62 if (!f) return 1;
63
64 #define GET_PARAMS(field) \
65 if (strcmp(left, #field) == 0) { \
66 config->field = atoi(right); \
67 continue; \
68 }
69
70 while (fgets(line, sizeof(line) - 1, f)) {
71 char *actual_line = ignore_front_spaces(line);
72 char *left, *right, *comment;
73 size_t length = strlen(actual_line);
74
75 if (length == 0 || actual_line[0] == '#') continue;
76 right = strchr(actual_line, '=');
77 if (right == NULL) continue;
78 right[0] = '\0';
79
80 left = ignore_front_spaces(actual_line);
81 right = ignore_front_spaces(right + 1);
82
83 comment = strchr(right, '#');
84 if (comment != NULL) comment[0] = '\0';
85
86 ignore_end_spaces(left);
87 ignore_end_spaces(right);
88
89 GET_PARAMS(super_block_size);
90 GET_PARAMS(max_partition_size);
91 GET_PARAMS(min_partition_size);
92 GET_PARAMS(disable_ab_partition_type);
93 GET_PARAMS(disable_rect_partition_type);
94 GET_PARAMS(disable_1to4_partition_type);
95 GET_PARAMS(disable_flip_idtx);
96 GET_PARAMS(disable_cdef);
97 GET_PARAMS(disable_lr);
98 GET_PARAMS(disable_obmc);
99 GET_PARAMS(disable_warp_motion);
100 GET_PARAMS(disable_global_motion);
101 GET_PARAMS(disable_dist_wtd_comp);
102 GET_PARAMS(disable_diff_wtd_comp);
103 GET_PARAMS(disable_inter_intra_comp);
104 GET_PARAMS(disable_masked_comp);
105 GET_PARAMS(disable_one_sided_comp);
106 GET_PARAMS(disable_palette);
107 GET_PARAMS(disable_intrabc);
108 GET_PARAMS(disable_cfl);
109 GET_PARAMS(disable_smooth_intra);
110 GET_PARAMS(disable_filter_intra);
111 GET_PARAMS(disable_dual_filter);
112 GET_PARAMS(disable_intra_angle_delta);
113 GET_PARAMS(disable_intra_edge_filter);
114 GET_PARAMS(disable_tx_64x64);
115 GET_PARAMS(disable_smooth_inter_intra);
116 GET_PARAMS(disable_inter_inter_wedge);
117 GET_PARAMS(disable_inter_intra_wedge);
118 GET_PARAMS(disable_paeth_intra);
119 GET_PARAMS(disable_trellis_quant);
120 GET_PARAMS(disable_ref_frame_mv);
121 GET_PARAMS(reduced_reference_set);
122 GET_PARAMS(reduced_tx_type_set);
123
124 fprintf(stderr, "\nInvalid parameter: %s", left);
125 exit(-1);
126 }
127
128 if (config->super_block_size != 128 && config->super_block_size != 64) {
129 fprintf(stderr, "\n%s", kSbSizeWarningString);
130 exit(-1);
131 }
132 if (config->min_partition_size > config->max_partition_size) {
133 fprintf(stderr, "\n%s", kMinpartWarningString);
134 exit(-1);
135 }
136 if (config->max_partition_size > config->super_block_size) {
137 fprintf(stderr, "\n%s", kMaxpartWarningString);
138 exit(-1);
139 }
140
141 fclose(f);
142 config->init_by_cfg_file = 1;
143
144 return 0;
145 }
146
arg_match(struct arg * arg_,const struct arg_def * def,char ** argv)147 int arg_match(struct arg *arg_, const struct arg_def *def, char **argv) {
148 struct arg arg;
149
150 if (!argv[0] || argv[0][0] != '-') return 0;
151
152 arg = arg_init(argv);
153
154 if (def->short_name && strlen(arg.argv[0]) == strlen(def->short_name) + 1 &&
155 !strcmp(arg.argv[0] + 1, def->short_name)) {
156 arg.name = arg.argv[0] + 1;
157 arg.val = def->has_val ? arg.argv[1] : NULL;
158 arg.argv_step = def->has_val ? 2 : 1;
159 } else if (def->long_name) {
160 const size_t name_len = strlen(def->long_name);
161
162 if (strlen(arg.argv[0]) >= name_len + 2 && arg.argv[0][1] == '-' &&
163 !strncmp(arg.argv[0] + 2, def->long_name, name_len) &&
164 (arg.argv[0][name_len + 2] == '=' ||
165 arg.argv[0][name_len + 2] == '\0')) {
166 arg.name = arg.argv[0] + 2;
167 arg.val = arg.name[name_len] == '=' ? arg.name + name_len + 1 : NULL;
168 arg.argv_step = 1;
169 }
170 }
171
172 if (arg.name && !arg.val && def->has_val)
173 die("Error: option %s requires argument.\n", arg.name);
174
175 if (arg.name && arg.val && !def->has_val)
176 die("Error: option %s requires no argument.\n", arg.name);
177
178 if (arg.name && (arg.val || !def->has_val)) {
179 arg.def = def;
180 *arg_ = arg;
181 return 1;
182 }
183
184 return 0;
185 }
186
arg_next(struct arg * arg)187 const char *arg_next(struct arg *arg) {
188 if (arg->argv[0]) arg->argv += arg->argv_step;
189
190 return *arg->argv;
191 }
192
argv_dup(int argc,const char ** argv)193 char **argv_dup(int argc, const char **argv) {
194 char **new_argv = malloc((argc + 1) * sizeof(*argv));
195
196 memcpy(new_argv, argv, argc * sizeof(*argv));
197 new_argv[argc] = NULL;
198 return new_argv;
199 }
200
arg_show_usage(FILE * fp,const struct arg_def * const * defs)201 void arg_show_usage(FILE *fp, const struct arg_def *const *defs) {
202 char option_text[40] = { 0 };
203
204 for (; *defs; defs++) {
205 const struct arg_def *def = *defs;
206 char *short_val = def->has_val ? " <arg>" : "";
207 char *long_val = def->has_val ? "=<arg>" : "";
208
209 if (def->short_name && def->long_name) {
210 char *comma = def->has_val ? "," : ", ";
211
212 snprintf(option_text, 37, "-%s%s%s --%s%6s", def->short_name, short_val,
213 comma, def->long_name, long_val);
214 } else if (def->short_name)
215 snprintf(option_text, 37, "-%s%s", def->short_name, short_val);
216 else if (def->long_name)
217 snprintf(option_text, 37, " --%s%s", def->long_name, long_val);
218
219 fprintf(fp, " %-37s\t%s\n", option_text, def->desc);
220
221 if (def->enums) {
222 const struct arg_enum_list *listptr;
223
224 fprintf(fp, " %-37s\t ", "");
225
226 for (listptr = def->enums; listptr->name; listptr++)
227 fprintf(fp, "%s%s", listptr->name, listptr[1].name ? ", " : "\n");
228 }
229 }
230 }
231
arg_parse_uint(const struct arg * arg)232 unsigned int arg_parse_uint(const struct arg *arg) {
233 char *endptr;
234 const unsigned long rawval = strtoul(arg->val, &endptr, 10); // NOLINT
235
236 if (arg->val[0] != '\0' && endptr[0] == '\0') {
237 if (rawval <= UINT_MAX) return (unsigned int)rawval;
238
239 die("Option %s: Value %lu out of range for unsigned int\n", arg->name,
240 rawval);
241 }
242
243 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
244 return 0;
245 }
246
arg_parse_int(const struct arg * arg)247 int arg_parse_int(const struct arg *arg) {
248 char *endptr;
249 const long rawval = strtol(arg->val, &endptr, 10); // NOLINT
250
251 if (arg->val[0] != '\0' && endptr[0] == '\0') {
252 if (rawval >= INT_MIN && rawval <= INT_MAX) return (int)rawval;
253
254 die("Option %s: Value %ld out of range for signed int\n", arg->name,
255 rawval);
256 }
257
258 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
259 return 0;
260 }
261
arg_parse_rational(const struct arg * arg)262 struct aom_rational arg_parse_rational(const struct arg *arg) {
263 long int rawval;
264 char *endptr;
265 struct aom_rational rat;
266
267 /* parse numerator */
268 rawval = strtol(arg->val, &endptr, 10);
269
270 if (arg->val[0] != '\0' && endptr[0] == '/') {
271 if (rawval >= INT_MIN && rawval <= INT_MAX)
272 rat.num = (int)rawval;
273 else
274 die("Option %s: Value %ld out of range for signed int\n", arg->name,
275 rawval);
276 } else
277 die("Option %s: Expected / at '%c'\n", arg->name, *endptr);
278
279 /* parse denominator */
280 rawval = strtol(endptr + 1, &endptr, 10);
281
282 if (arg->val[0] != '\0' && endptr[0] == '\0') {
283 if (rawval >= INT_MIN && rawval <= INT_MAX)
284 rat.den = (int)rawval;
285 else
286 die("Option %s: Value %ld out of range for signed int\n", arg->name,
287 rawval);
288 } else
289 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
290
291 return rat;
292 }
293
arg_parse_enum(const struct arg * arg)294 int arg_parse_enum(const struct arg *arg) {
295 const struct arg_enum_list *listptr;
296 long int rawval;
297 char *endptr;
298
299 /* First see if the value can be parsed as a raw value */
300 rawval = strtol(arg->val, &endptr, 10);
301 if (arg->val[0] != '\0' && endptr[0] == '\0') {
302 /* Got a raw value, make sure it's valid */
303 for (listptr = arg->def->enums; listptr->name; listptr++)
304 if (listptr->val == rawval) return (int)rawval;
305 }
306
307 /* Next see if it can be parsed as a string */
308 for (listptr = arg->def->enums; listptr->name; listptr++)
309 if (!strcmp(arg->val, listptr->name)) return listptr->val;
310
311 die("Option %s: Invalid value '%s'\n", arg->name, arg->val);
312 return 0;
313 }
314
arg_parse_enum_or_int(const struct arg * arg)315 int arg_parse_enum_or_int(const struct arg *arg) {
316 if (arg->def->enums) return arg_parse_enum(arg);
317 return arg_parse_int(arg);
318 }
319
320 // parse a comma separated list of at most n integers
321 // return the number of elements in the list
arg_parse_list(const struct arg * arg,int * list,int n)322 int arg_parse_list(const struct arg *arg, int *list, int n) {
323 const char *ptr = arg->val;
324 char *endptr;
325 int i = 0;
326
327 while (ptr[0] != '\0') {
328 int32_t rawval = (int32_t)strtol(ptr, &endptr, 10);
329 if (rawval < INT_MIN || rawval > INT_MAX) {
330 die("Option %s: Value %ld out of range for signed int\n", arg->name,
331 rawval);
332 } else if (i >= n) {
333 die("Option %s: List has more than %d entries\n", arg->name, n);
334 } else if (*endptr == ',') {
335 endptr++;
336 } else if (*endptr != '\0') {
337 die("Option %s: Bad list separator '%c'\n", arg->name, *endptr);
338 }
339 list[i++] = (int)rawval;
340 ptr = endptr;
341 }
342 return i;
343 }
344