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 <assert.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <limits.h>
18
19 #include "aom/aom_integer.h"
20 #include "aom_ports/msvc.h"
21 #include "aom/aom_codec.h"
22 #include "common/tools_common.h"
23
24 static const char kSbSizeWarningString[] =
25 "super_block_size has to be 64 or 128.";
26 static const char kMinpartWarningString[] =
27 "min_partition_size has to be smaller or equal to max_partition_size.";
28 static const char kMaxpartWarningString[] =
29 "max_partition_size has to be smaller or equal to super_block_size.";
30
ignore_front_spaces(const char * str)31 static char *ignore_front_spaces(const char *str) {
32 while (str[0] == ' ' || str[0] == '\t') ++str;
33 return (char *)str;
34 }
35
ignore_end_spaces(char * str)36 static void ignore_end_spaces(char *str) {
37 char *end = str + strlen(str);
38 while (end > str && (end[0] == ' ' || end[0] == '\t' || end[0] == '\n' ||
39 end[0] == '\r' || end[0] == '\0'))
40 --end;
41 if (end >= str) end[1] = '\0';
42 }
43
parse_cfg(const char * file,cfg_options_t * config)44 int parse_cfg(const char *file, cfg_options_t *config) {
45 char line[1024 * 10];
46 FILE *f = fopen(file, "r");
47 if (!f) return 1;
48
49 #define GET_PARAMS(field) \
50 if (strcmp(left, #field) == 0) { \
51 config->field = atoi(right); \
52 continue; \
53 }
54
55 while (fgets(line, sizeof(line) - 1, f)) {
56 char *actual_line = ignore_front_spaces(line);
57 char *left, *right, *comment;
58 size_t length = strlen(actual_line);
59
60 if (length == 0 || actual_line[0] == '#') continue;
61 right = strchr(actual_line, '=');
62 if (right == NULL) continue;
63 right[0] = '\0';
64
65 left = ignore_front_spaces(actual_line);
66 right = ignore_front_spaces(right + 1);
67
68 comment = strchr(right, '#');
69 if (comment != NULL) comment[0] = '\0';
70
71 ignore_end_spaces(left);
72 ignore_end_spaces(right);
73
74 GET_PARAMS(super_block_size);
75 GET_PARAMS(max_partition_size);
76 GET_PARAMS(min_partition_size);
77 GET_PARAMS(disable_ab_partition_type);
78 GET_PARAMS(disable_rect_partition_type);
79 GET_PARAMS(disable_1to4_partition_type);
80 GET_PARAMS(disable_flip_idtx);
81 GET_PARAMS(disable_cdef);
82 GET_PARAMS(disable_lr);
83 GET_PARAMS(disable_obmc);
84 GET_PARAMS(disable_warp_motion);
85 GET_PARAMS(disable_global_motion);
86 GET_PARAMS(disable_dist_wtd_comp);
87 GET_PARAMS(disable_diff_wtd_comp);
88 GET_PARAMS(disable_inter_intra_comp);
89 GET_PARAMS(disable_masked_comp);
90 GET_PARAMS(disable_one_sided_comp);
91 GET_PARAMS(disable_palette);
92 GET_PARAMS(disable_intrabc);
93 GET_PARAMS(disable_cfl);
94 GET_PARAMS(disable_smooth_intra);
95 GET_PARAMS(disable_filter_intra);
96 GET_PARAMS(disable_dual_filter);
97 GET_PARAMS(disable_intra_angle_delta);
98 GET_PARAMS(disable_intra_edge_filter);
99 GET_PARAMS(disable_tx_64x64);
100 GET_PARAMS(disable_smooth_inter_intra);
101 GET_PARAMS(disable_inter_inter_wedge);
102 GET_PARAMS(disable_inter_intra_wedge);
103 GET_PARAMS(disable_paeth_intra);
104 GET_PARAMS(disable_trellis_quant);
105 GET_PARAMS(disable_ref_frame_mv);
106 GET_PARAMS(reduced_reference_set);
107 GET_PARAMS(reduced_tx_type_set);
108
109 fprintf(stderr, "\nInvalid parameter: %s", left);
110 exit(-1);
111 }
112
113 if (config->super_block_size != 128 && config->super_block_size != 64) {
114 fprintf(stderr, "\n%s", kSbSizeWarningString);
115 exit(-1);
116 }
117 if (config->min_partition_size > config->max_partition_size) {
118 fprintf(stderr, "\n%s", kMinpartWarningString);
119 exit(-1);
120 }
121 if (config->max_partition_size > config->super_block_size) {
122 fprintf(stderr, "\n%s", kMaxpartWarningString);
123 exit(-1);
124 }
125
126 fclose(f);
127 config->init_by_cfg_file = 1;
128
129 return 0;
130 }
131
arg_match(struct arg * arg_,const struct arg_def * def,char ** argv)132 int arg_match(struct arg *arg_, const struct arg_def *def, char **argv) {
133 char err_msg[ARG_ERR_MSG_MAX_LEN];
134 int ret = arg_match_helper(arg_, def, argv, err_msg);
135 if (err_msg[0] != '\0') {
136 die(err_msg);
137 }
138 return ret;
139 }
140
arg_next(struct arg * arg)141 const char *arg_next(struct arg *arg) {
142 if (arg->argv[0]) arg->argv += arg->argv_step;
143
144 return *arg->argv;
145 }
146
argv_dup(int argc,const char ** argv)147 char **argv_dup(int argc, const char **argv) {
148 char **new_argv = malloc((argc + 1) * sizeof(*argv));
149
150 memcpy(new_argv, argv, argc * sizeof(*argv));
151 new_argv[argc] = NULL;
152 return new_argv;
153 }
154
arg_show_usage(FILE * fp,const struct arg_def * const * defs)155 void arg_show_usage(FILE *fp, const struct arg_def *const *defs) {
156 for (; *defs; defs++) {
157 const struct arg_def *def = *defs;
158 char *short_val = def->has_val ? " <arg>" : "";
159 char *long_val = def->has_val ? "=<arg>" : "";
160 int n = 0;
161
162 // Short options are indented with two spaces. Long options are indented
163 // with 12 spaces.
164 if (def->short_name && def->long_name) {
165 char *comma = def->has_val ? "," : ", ";
166
167 n = fprintf(fp, " -%s%s%s --%s%s", def->short_name, short_val, comma,
168 def->long_name, long_val);
169 } else if (def->short_name)
170 n = fprintf(fp, " -%s%s", def->short_name, short_val);
171 else if (def->long_name)
172 n = fprintf(fp, " --%s%s", def->long_name, long_val);
173
174 // Descriptions are indented with 40 spaces. If an option is 40 characters
175 // or longer, its description starts on the next line.
176 if (n < 40)
177 for (int i = 0; i < 40 - n; i++) fputc(' ', fp);
178 else
179 fputs("\n ", fp);
180 fprintf(fp, "%s\n", def->desc);
181
182 if (def->enums) {
183 const struct arg_enum_list *listptr;
184
185 fprintf(fp, " %-37s\t ", "");
186
187 for (listptr = def->enums; listptr->name; listptr++)
188 fprintf(fp, "%s%s", listptr->name, listptr[1].name ? ", " : "\n");
189 }
190 }
191 }
192
arg_parse_uint(const struct arg * arg)193 unsigned int arg_parse_uint(const struct arg *arg) {
194 char err_msg[ARG_ERR_MSG_MAX_LEN];
195 unsigned int ret = arg_parse_uint_helper(arg, err_msg);
196 if (err_msg[0] != '\0') {
197 die(err_msg);
198 }
199 return ret;
200 }
201
arg_parse_int(const struct arg * arg)202 int arg_parse_int(const struct arg *arg) {
203 char err_msg[ARG_ERR_MSG_MAX_LEN];
204 int ret = arg_parse_int_helper(arg, err_msg);
205 if (err_msg[0] != '\0') {
206 die(err_msg);
207 }
208 return ret;
209 }
210
arg_parse_rational(const struct arg * arg)211 struct aom_rational arg_parse_rational(const struct arg *arg) {
212 char err_msg[ARG_ERR_MSG_MAX_LEN];
213 struct aom_rational ret = arg_parse_rational_helper(arg, err_msg);
214 if (err_msg[0] != '\0') {
215 die(err_msg);
216 }
217 return ret;
218 }
219
arg_parse_enum(const struct arg * arg)220 int arg_parse_enum(const struct arg *arg) {
221 char err_msg[ARG_ERR_MSG_MAX_LEN];
222 int ret = arg_parse_enum_helper(arg, err_msg);
223 if (err_msg[0] != '\0') {
224 die(err_msg);
225 }
226 return ret;
227 }
228
arg_parse_enum_or_int(const struct arg * arg)229 int arg_parse_enum_or_int(const struct arg *arg) {
230 char err_msg[ARG_ERR_MSG_MAX_LEN];
231 int ret = arg_parse_enum_or_int_helper(arg, err_msg);
232 if (err_msg[0] != '\0') {
233 die(err_msg);
234 }
235 return ret;
236 }
237
238 // parse a comma separated list of at most n integers
239 // return the number of elements in the list
arg_parse_list(const struct arg * arg,int * list,int n)240 int arg_parse_list(const struct arg *arg, int *list, int n) {
241 char err_msg[ARG_ERR_MSG_MAX_LEN];
242 int ret = arg_parse_list_helper(arg, list, n, err_msg);
243 if (err_msg[0] != '\0') {
244 die(err_msg);
245 }
246 return ret;
247 }
248