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/aom_codec.h"
21 #include "common/tools_common.h"
22
23 static const char kSbSizeWarningString[] =
24 "super_block_size has to be 64 or 128.";
25 static const char kMinpartWarningString[] =
26 "min_partition_size has to be smaller or equal to max_partition_size.";
27 static const char kMaxpartWarningString[] =
28 "max_partition_size has to be smaller or equal to super_block_size.";
29
ignore_front_spaces(const char * str)30 static char *ignore_front_spaces(const char *str) {
31 while (str[0] == ' ' || str[0] == '\t') ++str;
32 return (char *)str;
33 }
34
ignore_end_spaces(char * str)35 static void ignore_end_spaces(char *str) {
36 char *end = str + strlen(str);
37 while (end > str && (end[0] == ' ' || end[0] == '\t' || end[0] == '\n' ||
38 end[0] == '\r' || end[0] == '\0'))
39 --end;
40 if (end >= str) end[1] = '\0';
41 }
42
parse_cfg(const char * file,cfg_options_t * config)43 int parse_cfg(const char *file, cfg_options_t *config) {
44 char line[1024 * 10];
45 FILE *f = fopen(file, "r");
46 if (!f) return 1;
47
48 #define GET_PARAMS(field) \
49 if (strcmp(left, #field) == 0) { \
50 config->field = atoi(right); \
51 continue; \
52 }
53
54 while (fgets(line, sizeof(line) - 1, f)) {
55 char *actual_line = ignore_front_spaces(line);
56 char *left, *right, *comment;
57 size_t length = strlen(actual_line);
58
59 if (length == 0 || actual_line[0] == '#') continue;
60 right = strchr(actual_line, '=');
61 if (right == NULL) continue;
62 right[0] = '\0';
63
64 left = ignore_front_spaces(actual_line);
65 right = ignore_front_spaces(right + 1);
66
67 comment = strchr(right, '#');
68 if (comment != NULL) comment[0] = '\0';
69
70 ignore_end_spaces(left);
71 ignore_end_spaces(right);
72
73 GET_PARAMS(super_block_size)
74 GET_PARAMS(max_partition_size)
75 GET_PARAMS(min_partition_size)
76 GET_PARAMS(disable_ab_partition_type)
77 GET_PARAMS(disable_rect_partition_type)
78 GET_PARAMS(disable_1to4_partition_type)
79 GET_PARAMS(disable_flip_idtx)
80 GET_PARAMS(disable_cdef)
81 GET_PARAMS(disable_lr)
82 GET_PARAMS(disable_obmc)
83 GET_PARAMS(disable_warp_motion)
84 GET_PARAMS(disable_global_motion)
85 GET_PARAMS(disable_dist_wtd_comp)
86 GET_PARAMS(disable_diff_wtd_comp)
87 GET_PARAMS(disable_inter_intra_comp)
88 GET_PARAMS(disable_masked_comp)
89 GET_PARAMS(disable_one_sided_comp)
90 GET_PARAMS(disable_palette)
91 GET_PARAMS(disable_intrabc)
92 GET_PARAMS(disable_cfl)
93 GET_PARAMS(disable_smooth_intra)
94 GET_PARAMS(disable_filter_intra)
95 GET_PARAMS(disable_dual_filter)
96 GET_PARAMS(disable_intra_angle_delta)
97 GET_PARAMS(disable_intra_edge_filter)
98 GET_PARAMS(disable_tx_64x64)
99 GET_PARAMS(disable_smooth_inter_intra)
100 GET_PARAMS(disable_inter_inter_wedge)
101 GET_PARAMS(disable_inter_intra_wedge)
102 GET_PARAMS(disable_paeth_intra)
103 GET_PARAMS(disable_trellis_quant)
104 GET_PARAMS(disable_ref_frame_mv)
105 GET_PARAMS(reduced_reference_set)
106 GET_PARAMS(reduced_tx_type_set)
107
108 fprintf(stderr, "\nInvalid parameter: %s", left);
109 exit(-1);
110 }
111
112 if (config->super_block_size != 128 && config->super_block_size != 64) {
113 fprintf(stderr, "\n%s", kSbSizeWarningString);
114 exit(-1);
115 }
116 if (config->min_partition_size > config->max_partition_size) {
117 fprintf(stderr, "\n%s", kMinpartWarningString);
118 exit(-1);
119 }
120 if (config->max_partition_size > config->super_block_size) {
121 fprintf(stderr, "\n%s", kMaxpartWarningString);
122 exit(-1);
123 }
124
125 fclose(f);
126 config->init_by_cfg_file = 1;
127
128 return 0;
129 }
130
arg_match(struct arg * arg_,const struct arg_def * def,char ** argv)131 int arg_match(struct arg *arg_, const struct arg_def *def, char **argv) {
132 char err_msg[ARG_ERR_MSG_MAX_LEN];
133 int ret = arg_match_helper(arg_, def, argv, err_msg);
134 if (err_msg[0] != '\0') {
135 die("%s", err_msg);
136 }
137 return ret;
138 }
139
arg_next(struct arg * arg)140 const char *arg_next(struct arg *arg) {
141 if (arg->argv[0]) arg->argv += arg->argv_step;
142
143 return *arg->argv;
144 }
145
argv_dup(int argc,const char ** argv)146 char **argv_dup(int argc, const char **argv) {
147 char **new_argv = malloc((argc + 1) * sizeof(*argv));
148 if (!new_argv) return NULL;
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("%s", 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("%s", 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("%s", 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("%s", 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("%s", 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("%s", err_msg);
245 }
246 return ret;
247 }
248