• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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("%s", 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   if (!new_argv) return NULL;
150 
151   memcpy(new_argv, argv, argc * sizeof(*argv));
152   new_argv[argc] = NULL;
153   return new_argv;
154 }
155 
arg_show_usage(FILE * fp,const struct arg_def * const * defs)156 void arg_show_usage(FILE *fp, const struct arg_def *const *defs) {
157   for (; *defs; defs++) {
158     const struct arg_def *def = *defs;
159     char *short_val = def->has_val ? " <arg>" : "";
160     char *long_val = def->has_val ? "=<arg>" : "";
161     int n = 0;
162 
163     // Short options are indented with two spaces. Long options are indented
164     // with 12 spaces.
165     if (def->short_name && def->long_name) {
166       char *comma = def->has_val ? "," : ",      ";
167 
168       n = fprintf(fp, "  -%s%s%s --%s%s", def->short_name, short_val, comma,
169                   def->long_name, long_val);
170     } else if (def->short_name)
171       n = fprintf(fp, "  -%s%s", def->short_name, short_val);
172     else if (def->long_name)
173       n = fprintf(fp, "            --%s%s", def->long_name, long_val);
174 
175     // Descriptions are indented with 40 spaces. If an option is 40 characters
176     // or longer, its description starts on the next line.
177     if (n < 40)
178       for (int i = 0; i < 40 - n; i++) fputc(' ', fp);
179     else
180       fputs("\n                                        ", fp);
181     fprintf(fp, "%s\n", def->desc);
182 
183     if (def->enums) {
184       const struct arg_enum_list *listptr;
185 
186       fprintf(fp, "  %-37s\t  ", "");
187 
188       for (listptr = def->enums; listptr->name; listptr++)
189         fprintf(fp, "%s%s", listptr->name, listptr[1].name ? ", " : "\n");
190     }
191   }
192 }
193 
arg_parse_uint(const struct arg * arg)194 unsigned int arg_parse_uint(const struct arg *arg) {
195   char err_msg[ARG_ERR_MSG_MAX_LEN];
196   unsigned int ret = arg_parse_uint_helper(arg, err_msg);
197   if (err_msg[0] != '\0') {
198     die("%s", err_msg);
199   }
200   return ret;
201 }
202 
arg_parse_int(const struct arg * arg)203 int arg_parse_int(const struct arg *arg) {
204   char err_msg[ARG_ERR_MSG_MAX_LEN];
205   int ret = arg_parse_int_helper(arg, err_msg);
206   if (err_msg[0] != '\0') {
207     die("%s", err_msg);
208   }
209   return ret;
210 }
211 
arg_parse_rational(const struct arg * arg)212 struct aom_rational arg_parse_rational(const struct arg *arg) {
213   char err_msg[ARG_ERR_MSG_MAX_LEN];
214   struct aom_rational ret = arg_parse_rational_helper(arg, err_msg);
215   if (err_msg[0] != '\0') {
216     die("%s", err_msg);
217   }
218   return ret;
219 }
220 
arg_parse_enum(const struct arg * arg)221 int arg_parse_enum(const struct arg *arg) {
222   char err_msg[ARG_ERR_MSG_MAX_LEN];
223   int ret = arg_parse_enum_helper(arg, err_msg);
224   if (err_msg[0] != '\0') {
225     die("%s", err_msg);
226   }
227   return ret;
228 }
229 
arg_parse_enum_or_int(const struct arg * arg)230 int arg_parse_enum_or_int(const struct arg *arg) {
231   char err_msg[ARG_ERR_MSG_MAX_LEN];
232   int ret = arg_parse_enum_or_int_helper(arg, err_msg);
233   if (err_msg[0] != '\0') {
234     die("%s", err_msg);
235   }
236   return ret;
237 }
238 
239 // parse a comma separated list of at most n integers
240 // return the number of elements in the list
arg_parse_list(const struct arg * arg,int * list,int n)241 int arg_parse_list(const struct arg *arg, int *list, int n) {
242   char err_msg[ARG_ERR_MSG_MAX_LEN];
243   int ret = arg_parse_list_helper(arg, list, n, err_msg);
244   if (err_msg[0] != '\0') {
245     die("%s", err_msg);
246   }
247   return ret;
248 }
249