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