Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Various utilities for command line tools | ||
3 | * Copyright (c) 2000-2003 Fabrice Bellard | ||
4 | * | ||
5 | * This file is part of FFmpeg. | ||
6 | * | ||
7 | * FFmpeg is free software; you can redistribute it and/or | ||
8 | * modify it under the terms of the GNU Lesser General Public | ||
9 | * License as published by the Free Software Foundation; either | ||
10 | * version 2.1 of the License, or (at your option) any later version. | ||
11 | * | ||
12 | * FFmpeg is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with FFmpeg; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
20 | */ | ||
21 | |||
22 | #include <string.h> | ||
23 | #include <stdint.h> | ||
24 | #include <stdlib.h> | ||
25 | #include <errno.h> | ||
26 | #include <math.h> | ||
27 | |||
28 | /* Include only the enabled headers since some compilers (namely, Sun | ||
29 | Studio) will not omit unused inline functions and create undefined | ||
30 | references to libraries that are not being built. */ | ||
31 | |||
32 | #include "config.h" | ||
33 | #include "compat/va_copy.h" | ||
34 | #include "libavformat/avformat.h" | ||
35 | #include "libswscale/swscale.h" | ||
36 | #include "libswscale/version.h" | ||
37 | #include "libswresample/swresample.h" | ||
38 | #include "libavutil/avassert.h" | ||
39 | #include "libavutil/avstring.h" | ||
40 | #include "libavutil/channel_layout.h" | ||
41 | #include "libavutil/display.h" | ||
42 | #include "libavutil/getenv_utf8.h" | ||
43 | #include "libavutil/mathematics.h" | ||
44 | #include "libavutil/imgutils.h" | ||
45 | #include "libavutil/libm.h" | ||
46 | #include "libavutil/parseutils.h" | ||
47 | #include "libavutil/eval.h" | ||
48 | #include "libavutil/dict.h" | ||
49 | #include "libavutil/opt.h" | ||
50 | #include "cmdutils.h" | ||
51 | #include "fopen_utf8.h" | ||
52 | #include "opt_common.h" | ||
53 | #ifdef _WIN32 | ||
54 | #include <windows.h> | ||
55 | #include "compat/w32dlfcn.h" | ||
56 | #endif | ||
57 | |||
58 | AVDictionary *sws_dict; | ||
59 | AVDictionary *swr_opts; | ||
60 | AVDictionary *format_opts, *codec_opts; | ||
61 | |||
62 | int hide_banner = 0; | ||
63 | |||
64 | 13062 | void uninit_opts(void) | |
65 | { | ||
66 | 13062 | av_dict_free(&swr_opts); | |
67 | 13062 | av_dict_free(&sws_dict); | |
68 | 13062 | av_dict_free(&format_opts); | |
69 | 13062 | av_dict_free(&codec_opts); | |
70 | 13062 | } | |
71 | |||
72 | ✗ | void log_callback_help(void *ptr, int level, const char *fmt, va_list vl) | |
73 | { | ||
74 | ✗ | vfprintf(stdout, fmt, vl); | |
75 | } | ||
76 | |||
77 | 6594 | void init_dynload(void) | |
78 | { | ||
79 | #if HAVE_SETDLLDIRECTORY && defined(_WIN32) | ||
80 | /* Calling SetDllDirectory with the empty string (but not NULL) removes the | ||
81 | * current working directory from the DLL search path as a security pre-caution. */ | ||
82 | SetDllDirectory(""); | ||
83 | #endif | ||
84 | 6594 | } | |
85 | |||
86 | static void (*program_exit)(int ret); | ||
87 | |||
88 | 6594 | void register_exit(void (*cb)(int ret)) | |
89 | { | ||
90 | 6594 | program_exit = cb; | |
91 | 6594 | } | |
92 | |||
93 | ✗ | void report_and_exit(int ret) | |
94 | { | ||
95 | ✗ | av_log(NULL, AV_LOG_FATAL, "%s\n", av_err2str(ret)); | |
96 | ✗ | exit_program(AVUNERROR(ret)); | |
97 | } | ||
98 | |||
99 | 6469 | void exit_program(int ret) | |
100 | { | ||
101 |
1/2✓ Branch 0 taken 6469 times.
✗ Branch 1 not taken.
|
6469 | if (program_exit) |
102 | 6469 | program_exit(ret); | |
103 | |||
104 | 6469 | exit(ret); | |
105 | } | ||
106 | |||
107 | 30239 | double parse_number_or_die(const char *context, const char *numstr, int type, | |
108 | double min, double max) | ||
109 | { | ||
110 | char *tail; | ||
111 | const char *error; | ||
112 | 30239 | double d = av_strtod(numstr, &tail); | |
113 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30239 times.
|
30239 | if (*tail) |
114 | ✗ | error = "Expected number for %s but found: %s\n"; | |
115 |
2/4✓ Branch 0 taken 30239 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30239 times.
|
30239 | else if (d < min || d > max) |
116 | ✗ | error = "The value for %s was %s which is not within %f - %f\n"; | |
117 |
3/4✓ Branch 0 taken 29958 times.
✓ Branch 1 taken 281 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 29958 times.
|
30239 | else if (type == OPT_INT64 && (int64_t)d != d) |
118 | ✗ | error = "Expected int64 for %s but found %s\n"; | |
119 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 30239 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
30239 | else if (type == OPT_INT && (int)d != d) |
120 | ✗ | error = "Expected int for %s but found %s\n"; | |
121 | else | ||
122 | 30239 | return d; | |
123 | ✗ | av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max); | |
124 | ✗ | exit_program(1); | |
125 | return 0; | ||
126 | } | ||
127 | |||
128 | 201 | int64_t parse_time_or_die(const char *context, const char *timestr, | |
129 | int is_duration) | ||
130 | { | ||
131 | int64_t us; | ||
132 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 201 times.
|
201 | if (av_parse_time(&us, timestr, is_duration) < 0) { |
133 | ✗ | av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n", | |
134 | is_duration ? "duration" : "date", context, timestr); | ||
135 | ✗ | exit_program(1); | |
136 | } | ||
137 | 201 | return us; | |
138 | } | ||
139 | |||
140 | ✗ | void show_help_options(const OptionDef *options, const char *msg, int req_flags, | |
141 | int rej_flags, int alt_flags) | ||
142 | { | ||
143 | const OptionDef *po; | ||
144 | int first; | ||
145 | |||
146 | ✗ | first = 1; | |
147 | ✗ | for (po = options; po->name; po++) { | |
148 | char buf[128]; | ||
149 | |||
150 | ✗ | if (((po->flags & req_flags) != req_flags) || | |
151 | ✗ | (alt_flags && !(po->flags & alt_flags)) || | |
152 | ✗ | (po->flags & rej_flags)) | |
153 | ✗ | continue; | |
154 | |||
155 | ✗ | if (first) { | |
156 | ✗ | printf("%s\n", msg); | |
157 | ✗ | first = 0; | |
158 | } | ||
159 | ✗ | av_strlcpy(buf, po->name, sizeof(buf)); | |
160 | ✗ | if (po->argname) { | |
161 | ✗ | av_strlcat(buf, " ", sizeof(buf)); | |
162 | ✗ | av_strlcat(buf, po->argname, sizeof(buf)); | |
163 | } | ||
164 | ✗ | printf("-%-17s %s\n", buf, po->help); | |
165 | } | ||
166 | ✗ | printf("\n"); | |
167 | } | ||
168 | |||
169 | ✗ | void show_help_children(const AVClass *class, int flags) | |
170 | { | ||
171 | ✗ | void *iter = NULL; | |
172 | const AVClass *child; | ||
173 | ✗ | if (class->option) { | |
174 | ✗ | av_opt_show2(&class, NULL, flags, 0); | |
175 | ✗ | printf("\n"); | |
176 | } | ||
177 | |||
178 | ✗ | while (child = av_opt_child_class_iterate(class, &iter)) | |
179 | ✗ | show_help_children(child, flags); | |
180 | } | ||
181 | |||
182 | 986481 | static const OptionDef *find_option(const OptionDef *po, const char *name) | |
183 | { | ||
184 |
2/2✓ Branch 0 taken 142807665 times.
✓ Branch 1 taken 562909 times.
|
143370574 | while (po->name) { |
185 | const char *end; | ||
186 |
6/6✓ Branch 1 taken 998375 times.
✓ Branch 2 taken 141809290 times.
✓ Branch 3 taken 619091 times.
✓ Branch 4 taken 379284 times.
✓ Branch 5 taken 574803 times.
✓ Branch 6 taken 44288 times.
|
142807665 | if (av_strstart(name, po->name, &end) && (!*end || *end == ':')) |
187 | break; | ||
188 | 142384093 | po++; | |
189 | } | ||
190 | 986481 | return po; | |
191 | } | ||
192 | |||
193 | /* _WIN32 means using the windows libc - cygwin doesn't define that | ||
194 | * by default. HAVE_COMMANDLINETOARGVW is true on cygwin, while | ||
195 | * it doesn't provide the actual command line via GetCommandLineW(). */ | ||
196 | #if HAVE_COMMANDLINETOARGVW && defined(_WIN32) | ||
197 | #include <shellapi.h> | ||
198 | /* Will be leaked on exit */ | ||
199 | static char** win32_argv_utf8 = NULL; | ||
200 | static int win32_argc = 0; | ||
201 | |||
202 | /** | ||
203 | * Prepare command line arguments for executable. | ||
204 | * For Windows - perform wide-char to UTF-8 conversion. | ||
205 | * Input arguments should be main() function arguments. | ||
206 | * @param argc_ptr Arguments number (including executable) | ||
207 | * @param argv_ptr Arguments list. | ||
208 | */ | ||
209 | static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr) | ||
210 | { | ||
211 | char *argstr_flat; | ||
212 | wchar_t **argv_w; | ||
213 | int i, buffsize = 0, offset = 0; | ||
214 | |||
215 | if (win32_argv_utf8) { | ||
216 | *argc_ptr = win32_argc; | ||
217 | *argv_ptr = win32_argv_utf8; | ||
218 | return; | ||
219 | } | ||
220 | |||
221 | win32_argc = 0; | ||
222 | argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc); | ||
223 | if (win32_argc <= 0 || !argv_w) | ||
224 | return; | ||
225 | |||
226 | /* determine the UTF-8 buffer size (including NULL-termination symbols) */ | ||
227 | for (i = 0; i < win32_argc; i++) | ||
228 | buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1, | ||
229 | NULL, 0, NULL, NULL); | ||
230 | |||
231 | win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize); | ||
232 | argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1); | ||
233 | if (!win32_argv_utf8) { | ||
234 | LocalFree(argv_w); | ||
235 | return; | ||
236 | } | ||
237 | |||
238 | for (i = 0; i < win32_argc; i++) { | ||
239 | win32_argv_utf8[i] = &argstr_flat[offset]; | ||
240 | offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1, | ||
241 | &argstr_flat[offset], | ||
242 | buffsize - offset, NULL, NULL); | ||
243 | } | ||
244 | win32_argv_utf8[i] = NULL; | ||
245 | LocalFree(argv_w); | ||
246 | |||
247 | *argc_ptr = win32_argc; | ||
248 | *argv_ptr = win32_argv_utf8; | ||
249 | } | ||
250 | #else | ||
251 | 6594 | static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr) | |
252 | { | ||
253 | /* nothing to do */ | ||
254 | 6594 | } | |
255 | #endif /* HAVE_COMMANDLINETOARGVW */ | ||
256 | |||
257 | 77398 | static int write_option(void *optctx, const OptionDef *po, const char *opt, | |
258 | const char *arg) | ||
259 | { | ||
260 | /* new-style options contain an offset into optctx, old-style address of | ||
261 | * a global var*/ | ||
262 | 154796 | void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ? | |
263 |
2/2✓ Branch 0 taken 38111 times.
✓ Branch 1 taken 39287 times.
|
77398 | (uint8_t *)optctx + po->u.off : po->u.dst_ptr; |
264 | int *dstcount; | ||
265 | |||
266 |
2/2✓ Branch 0 taken 26287 times.
✓ Branch 1 taken 51111 times.
|
77398 | if (po->flags & OPT_SPEC) { |
267 | 26287 | SpecifierOpt **so = dst; | |
268 | 26287 | char *p = strchr(opt, ':'); | |
269 | char *str; | ||
270 | |||
271 | 26287 | dstcount = (int *)(so + 1); | |
272 | 26287 | *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1); | |
273 |
2/2✓ Branch 0 taken 13975 times.
✓ Branch 1 taken 12312 times.
|
26287 | str = av_strdup(p ? p + 1 : ""); |
274 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26287 times.
|
26287 | if (!str) |
275 | ✗ | return AVERROR(ENOMEM); | |
276 | 26287 | (*so)[*dstcount - 1].specifier = str; | |
277 | 26287 | dst = &(*so)[*dstcount - 1].u; | |
278 | } | ||
279 | |||
280 |
2/2✓ Branch 0 taken 30826 times.
✓ Branch 1 taken 46572 times.
|
77398 | if (po->flags & OPT_STRING) { |
281 | char *str; | ||
282 | 30826 | str = av_strdup(arg); | |
283 | 30826 | av_freep(dst); | |
284 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30826 times.
|
30826 | if (!str) |
285 | ✗ | return AVERROR(ENOMEM); | |
286 | 30826 | *(char **)dst = str; | |
287 |
4/4✓ Branch 0 taken 21413 times.
✓ Branch 1 taken 25159 times.
✓ Branch 2 taken 136 times.
✓ Branch 3 taken 21277 times.
|
46572 | } else if (po->flags & OPT_BOOL || po->flags & OPT_INT) { |
288 | 25295 | *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX); | |
289 |
2/2✓ Branch 0 taken 4663 times.
✓ Branch 1 taken 16614 times.
|
21277 | } else if (po->flags & OPT_INT64) { |
290 | 4663 | *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX); | |
291 |
2/2✓ Branch 0 taken 199 times.
✓ Branch 1 taken 16415 times.
|
16614 | } else if (po->flags & OPT_TIME) { |
292 | 199 | *(int64_t *)dst = parse_time_or_die(opt, arg, 1); | |
293 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16413 times.
|
16415 | } else if (po->flags & OPT_FLOAT) { |
294 | 2 | *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY); | |
295 |
2/2✓ Branch 0 taken 279 times.
✓ Branch 1 taken 16134 times.
|
16413 | } else if (po->flags & OPT_DOUBLE) { |
296 | 279 | *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY); | |
297 |
1/2✓ Branch 0 taken 16134 times.
✗ Branch 1 not taken.
|
16134 | } else if (po->u.func_arg) { |
298 | 16134 | int ret = po->u.func_arg(optctx, opt, arg); | |
299 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16134 times.
|
16134 | if (ret < 0) { |
300 | ✗ | av_log(NULL, AV_LOG_ERROR, | |
301 | "Failed to set value '%s' for option '%s': %s\n", | ||
302 | ✗ | arg, opt, av_err2str(ret)); | |
303 | ✗ | return ret; | |
304 | } | ||
305 | } | ||
306 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 77397 times.
|
77398 | if (po->flags & OPT_EXIT) |
307 | 1 | exit_program(0); | |
308 | |||
309 | 77397 | return 0; | |
310 | } | ||
311 | |||
312 | 8609 | int parse_option(void *optctx, const char *opt, const char *arg, | |
313 | const OptionDef *options) | ||
314 | { | ||
315 | static const OptionDef opt_avoptions = { | ||
316 | .name = "AVOption passthrough", | ||
317 | .flags = HAS_ARG, | ||
318 | .u.func_arg = opt_default, | ||
319 | }; | ||
320 | |||
321 | const OptionDef *po; | ||
322 | int ret; | ||
323 | |||
324 | 8609 | po = find_option(options, opt); | |
325 |
5/6✓ Branch 0 taken 7 times.
✓ Branch 1 taken 8602 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
8609 | if (!po->name && opt[0] == 'n' && opt[1] == 'o') { |
326 | /* handle 'no' bool option */ | ||
327 | 1 | po = find_option(options, opt + 2); | |
328 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if ((po->name && (po->flags & OPT_BOOL))) |
329 | 1 | arg = "0"; | |
330 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 8488 times.
|
8608 | } else if (po->flags & OPT_BOOL) |
331 | 120 | arg = "1"; | |
332 | |||
333 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8603 times.
|
8609 | if (!po->name) |
334 | 6 | po = &opt_avoptions; | |
335 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8609 times.
|
8609 | if (!po->name) { |
336 | ✗ | av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt); | |
337 | ✗ | return AVERROR(EINVAL); | |
338 | } | ||
339 |
3/4✓ Branch 0 taken 8395 times.
✓ Branch 1 taken 214 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8395 times.
|
8609 | if (po->flags & HAS_ARG && !arg) { |
340 | ✗ | av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt); | |
341 | ✗ | return AVERROR(EINVAL); | |
342 | } | ||
343 | |||
344 | 8609 | ret = write_option(optctx, po, opt, arg); | |
345 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8609 times.
|
8609 | if (ret < 0) |
346 | ✗ | return ret; | |
347 | |||
348 | 8609 | return !!(po->flags & HAS_ARG); | |
349 | } | ||
350 | |||
351 | 125 | void parse_options(void *optctx, int argc, char **argv, const OptionDef *options, | |
352 | void (*parse_arg_function)(void *, const char*)) | ||
353 | { | ||
354 | const char *opt; | ||
355 | 125 | int optindex, handleoptions = 1, ret; | |
356 | |||
357 | /* perform system-dependent conversions for arguments list */ | ||
358 | 125 | prepare_app_arguments(&argc, &argv); | |
359 | |||
360 | /* parse options */ | ||
361 | 125 | optindex = 1; | |
362 |
2/2✓ Branch 0 taken 572 times.
✓ Branch 1 taken 125 times.
|
697 | while (optindex < argc) { |
363 | 572 | opt = argv[optindex++]; | |
364 | |||
365 |
4/6✓ Branch 0 taken 572 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 457 times.
✓ Branch 3 taken 115 times.
✓ Branch 4 taken 457 times.
✗ Branch 5 not taken.
|
572 | if (handleoptions && opt[0] == '-' && opt[1] != '\0') { |
366 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 457 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
457 | if (opt[1] == '-' && opt[2] == '\0') { |
367 | ✗ | handleoptions = 0; | |
368 | ✗ | continue; | |
369 | } | ||
370 | 457 | opt++; | |
371 | |||
372 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 457 times.
|
457 | if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0) |
373 | ✗ | exit_program(1); | |
374 | 457 | optindex += ret; | |
375 | } else { | ||
376 |
1/2✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
|
115 | if (parse_arg_function) |
377 | 115 | parse_arg_function(optctx, opt); | |
378 | } | ||
379 | } | ||
380 | 125 | } | |
381 | |||
382 | 19439 | int parse_optgroup(void *optctx, OptionGroup *g) | |
383 | { | ||
384 | int i, ret; | ||
385 | |||
386 | 19439 | av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n", | |
387 | 19439 | g->group_def->name, g->arg); | |
388 | |||
389 |
2/2✓ Branch 0 taken 68789 times.
✓ Branch 1 taken 19438 times.
|
88227 | for (i = 0; i < g->nb_opts; i++) { |
390 | 68789 | Option *o = &g->opts[i]; | |
391 | |||
392 |
2/2✓ Branch 0 taken 38501 times.
✓ Branch 1 taken 30288 times.
|
68789 | if (g->group_def->flags && |
393 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38501 times.
|
38501 | !(g->group_def->flags & o->opt->flags)) { |
394 | ✗ | av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to " | |
395 | "%s %s -- you are trying to apply an input option to an " | ||
396 | "output file or vice versa. Move this option before the " | ||
397 | ✗ | "file it belongs to.\n", o->key, o->opt->help, | |
398 | ✗ | g->group_def->name, g->arg); | |
399 | ✗ | return AVERROR(EINVAL); | |
400 | } | ||
401 | |||
402 | 68789 | av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n", | |
403 | 68789 | o->key, o->opt->help, o->val); | |
404 | |||
405 | 68789 | ret = write_option(optctx, o->opt, o->key, o->val); | |
406 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 68788 times.
|
68788 | if (ret < 0) |
407 | ✗ | return ret; | |
408 | } | ||
409 | |||
410 | 19438 | av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n"); | |
411 | |||
412 | 19438 | return 0; | |
413 | } | ||
414 | |||
415 | 32970 | int locate_option(int argc, char **argv, const OptionDef *options, | |
416 | const char *optname) | ||
417 | { | ||
418 | const OptionDef *po; | ||
419 | int i; | ||
420 | |||
421 |
2/2✓ Branch 0 taken 746500 times.
✓ Branch 1 taken 32955 times.
|
779455 | for (i = 1; i < argc; i++) { |
422 | 746500 | const char *cur_opt = argv[i]; | |
423 | |||
424 |
2/2✓ Branch 0 taken 20568 times.
✓ Branch 1 taken 725932 times.
|
746500 | if (*cur_opt++ != '-') |
425 | 20568 | continue; | |
426 | |||
427 | 725932 | po = find_option(options, cur_opt); | |
428 |
5/6✓ Branch 0 taken 476532 times.
✓ Branch 1 taken 249400 times.
✓ Branch 2 taken 96820 times.
✓ Branch 3 taken 379712 times.
✓ Branch 4 taken 96820 times.
✗ Branch 5 not taken.
|
725932 | if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o') |
429 | 96820 | po = find_option(options, cur_opt + 2); | |
430 | |||
431 |
3/4✓ Branch 0 taken 379752 times.
✓ Branch 1 taken 346180 times.
✓ Branch 2 taken 379752 times.
✗ Branch 3 not taken.
|
725932 | if ((!po->name && !strcmp(cur_opt, optname)) || |
432 |
4/4✓ Branch 0 taken 346180 times.
✓ Branch 1 taken 379752 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 346165 times.
|
725932 | (po->name && !strcmp(optname, po->name))) |
433 | 15 | return i; | |
434 | |||
435 |
4/4✓ Branch 0 taken 346165 times.
✓ Branch 1 taken 379752 times.
✓ Branch 2 taken 219905 times.
✓ Branch 3 taken 126260 times.
|
725917 | if (!po->name || po->flags & HAS_ARG) |
436 | 599657 | i++; | |
437 | } | ||
438 | 32955 | return 0; | |
439 | } | ||
440 | |||
441 | ✗ | static void dump_argument(FILE *report_file, const char *a) | |
442 | { | ||
443 | const unsigned char *p; | ||
444 | |||
445 | ✗ | for (p = a; *p; p++) | |
446 | ✗ | if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') || | |
447 | ✗ | *p == '_' || (*p >= 'a' && *p <= 'z'))) | |
448 | break; | ||
449 | ✗ | if (!*p) { | |
450 | ✗ | fputs(a, report_file); | |
451 | ✗ | return; | |
452 | } | ||
453 | ✗ | fputc('"', report_file); | |
454 | ✗ | for (p = a; *p; p++) { | |
455 | ✗ | if (*p == '\\' || *p == '"' || *p == '$' || *p == '`') | |
456 | ✗ | fprintf(report_file, "\\%c", *p); | |
457 | ✗ | else if (*p < ' ' || *p > '~') | |
458 | ✗ | fprintf(report_file, "\\x%02x", *p); | |
459 | else | ||
460 | ✗ | fputc(*p, report_file); | |
461 | } | ||
462 | ✗ | fputc('"', report_file); | |
463 | } | ||
464 | |||
465 | 6594 | static void check_options(const OptionDef *po) | |
466 | { | ||
467 |
2/2✓ Branch 0 taken 1243954 times.
✓ Branch 1 taken 6594 times.
|
1250548 | while (po->name) { |
468 |
2/2✓ Branch 0 taken 194070 times.
✓ Branch 1 taken 1049884 times.
|
1243954 | if (po->flags & OPT_PERFILE) |
469 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 194070 times.
|
194070 | av_assert0(po->flags & (OPT_INPUT | OPT_OUTPUT)); |
470 | 1243954 | po++; | |
471 | } | ||
472 | 6594 | } | |
473 | |||
474 | 6594 | void parse_loglevel(int argc, char **argv, const OptionDef *options) | |
475 | { | ||
476 | 6594 | int idx = locate_option(argc, argv, options, "loglevel"); | |
477 | char *env; | ||
478 | |||
479 | 6594 | check_options(options); | |
480 | |||
481 |
1/2✓ Branch 0 taken 6594 times.
✗ Branch 1 not taken.
|
6594 | if (!idx) |
482 | 6594 | idx = locate_option(argc, argv, options, "v"); | |
483 |
3/4✓ Branch 0 taken 15 times.
✓ Branch 1 taken 6579 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
|
6594 | if (idx && argv[idx + 1]) |
484 | 15 | opt_loglevel(NULL, "loglevel", argv[idx + 1]); | |
485 | 6594 | idx = locate_option(argc, argv, options, "report"); | |
486 | 6594 | env = getenv_utf8("FFREPORT"); | |
487 |
2/4✓ Branch 0 taken 6594 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6594 times.
|
6594 | if (env || idx) { |
488 | ✗ | FILE *report_file = NULL; | |
489 | ✗ | init_report(env, &report_file); | |
490 | ✗ | if (report_file) { | |
491 | int i; | ||
492 | ✗ | fprintf(report_file, "Command line:\n"); | |
493 | ✗ | for (i = 0; i < argc; i++) { | |
494 | ✗ | dump_argument(report_file, argv[i]); | |
495 | ✗ | fputc(i < argc - 1 ? ' ' : '\n', report_file); | |
496 | } | ||
497 | ✗ | fflush(report_file); | |
498 | } | ||
499 | } | ||
500 | 6594 | freeenv_utf8(env); | |
501 | 6594 | idx = locate_option(argc, argv, options, "hide_banner"); | |
502 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6594 times.
|
6594 | if (idx) |
503 | ✗ | hide_banner = 1; | |
504 | 6594 | } | |
505 | |||
506 | 234559 | static const AVOption *opt_find(void *obj, const char *name, const char *unit, | |
507 | int opt_flags, int search_flags) | ||
508 | { | ||
509 | 234559 | const AVOption *o = av_opt_find(obj, name, unit, opt_flags, search_flags); | |
510 |
4/4✓ Branch 0 taken 67057 times.
✓ Branch 1 taken 167502 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 67055 times.
|
234559 | if(o && !o->flags) |
511 | 2 | return NULL; | |
512 | 234557 | return o; | |
513 | } | ||
514 | |||
515 | #define FLAGS (o->type == AV_OPT_TYPE_FLAGS && (arg[0]=='-' || arg[0]=='+')) ? AV_DICT_APPEND : 0 | ||
516 | 86336 | int opt_default(void *optctx, const char *opt, const char *arg) | |
517 | { | ||
518 | const AVOption *o; | ||
519 | 86336 | int consumed = 0; | |
520 | char opt_stripped[128]; | ||
521 | const char *p; | ||
522 | 86336 | const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class(); | |
523 | #if CONFIG_SWSCALE | ||
524 | 86336 | const AVClass *sc = sws_get_class(); | |
525 | #endif | ||
526 | #if CONFIG_SWRESAMPLE | ||
527 | 86336 | const AVClass *swr_class = swr_get_class(); | |
528 | #endif | ||
529 | |||
530 |
2/4✓ Branch 0 taken 86336 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 86336 times.
|
86336 | if (!strcmp(opt, "debug") || !strcmp(opt, "fdebug")) |
531 | ✗ | av_log_set_level(AV_LOG_DEBUG); | |
532 | |||
533 |
2/2✓ Branch 0 taken 86328 times.
✓ Branch 1 taken 8 times.
|
86336 | if (!(p = strchr(opt, ':'))) |
534 | 86328 | p = opt + strlen(opt); | |
535 | 86336 | av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1)); | |
536 | |||
537 |
2/2✓ Branch 1 taken 42451 times.
✓ Branch 2 taken 43885 times.
|
86336 | if ((o = opt_find(&cc, opt_stripped, NULL, 0, |
538 | 42451 | AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) || | |
539 |
8/8✓ Branch 0 taken 42448 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 42436 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 11593 times.
✓ Branch 5 taken 30843 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 11607 times.
|
54059 | ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') && |
540 | 11608 | (o = opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) { | |
541 |
5/6✓ Branch 0 taken 17823 times.
✓ Branch 1 taken 26063 times.
✓ Branch 2 taken 17823 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11524 times.
✓ Branch 5 taken 6299 times.
|
43886 | av_dict_set(&codec_opts, opt, arg, FLAGS); |
542 | 43886 | consumed = 1; | |
543 | } | ||
544 |
2/2✓ Branch 1 taken 11600 times.
✓ Branch 2 taken 74736 times.
|
86336 | if ((o = opt_find(&fc, opt, NULL, 0, |
545 | AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) { | ||
546 |
5/6✓ Branch 0 taken 11393 times.
✓ Branch 1 taken 207 times.
✓ Branch 2 taken 11393 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11380 times.
✓ Branch 5 taken 13 times.
|
11600 | av_dict_set(&format_opts, opt, arg, FLAGS); |
547 |
2/2✓ Branch 0 taken 74 times.
✓ Branch 1 taken 11526 times.
|
11600 | if (consumed) |
548 | 74 | av_log(NULL, AV_LOG_VERBOSE, "Routing option %s to both codec and muxer layer\n", opt); | |
549 | 11600 | consumed = 1; | |
550 | } | ||
551 | #if CONFIG_SWSCALE | ||
552 |
4/4✓ Branch 0 taken 30924 times.
✓ Branch 1 taken 55412 times.
✓ Branch 3 taken 11569 times.
✓ Branch 4 taken 19355 times.
|
86336 | if (!consumed && (o = opt_find(&sc, opt, NULL, 0, |
553 | AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) { | ||
554 |
2/4✓ Branch 0 taken 11569 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11569 times.
✗ Branch 3 not taken.
|
11569 | if (!strcmp(opt, "srcw") || !strcmp(opt, "srch") || |
555 |
2/4✓ Branch 0 taken 11569 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11569 times.
✗ Branch 3 not taken.
|
11569 | !strcmp(opt, "dstw") || !strcmp(opt, "dsth") || |
556 |
2/4✓ Branch 0 taken 11569 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11569 times.
|
11569 | !strcmp(opt, "src_format") || !strcmp(opt, "dst_format")) { |
557 | ✗ | av_log(NULL, AV_LOG_ERROR, "Directly using swscale dimensions/format options is not supported, please use the -s or -pix_fmt options\n"); | |
558 | ✗ | return AVERROR(EINVAL); | |
559 | } | ||
560 |
4/6✓ Branch 0 taken 11569 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11569 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11384 times.
✓ Branch 5 taken 185 times.
|
11569 | av_dict_set(&sws_dict, opt, arg, FLAGS); |
561 | |||
562 | 11569 | consumed = 1; | |
563 | } | ||
564 | #else | ||
565 | if (!consumed && !strcmp(opt, "sws_flags")) { | ||
566 | av_log(NULL, AV_LOG_WARNING, "Ignoring %s %s, due to disabled swscale\n", opt, arg); | ||
567 | consumed = 1; | ||
568 | } | ||
569 | #endif | ||
570 | #if CONFIG_SWRESAMPLE | ||
571 |
3/4✓ Branch 0 taken 19355 times.
✓ Branch 1 taken 66981 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19355 times.
|
86336 | if (!consumed && (o=opt_find(&swr_class, opt, NULL, 0, |
572 | AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) { | ||
573 | ✗ | av_dict_set(&swr_opts, opt, arg, FLAGS); | |
574 | ✗ | consumed = 1; | |
575 | } | ||
576 | #endif | ||
577 | |||
578 |
2/2✓ Branch 0 taken 66981 times.
✓ Branch 1 taken 19355 times.
|
86336 | if (consumed) |
579 | 66981 | return 0; | |
580 | 19355 | return AVERROR_OPTION_NOT_FOUND; | |
581 | } | ||
582 | |||
583 | /* | ||
584 | * Check whether given option is a group separator. | ||
585 | * | ||
586 | * @return index of the group definition that matched or -1 if none | ||
587 | */ | ||
588 | 142264 | static int match_group_separator(const OptionGroupDef *groups, int nb_groups, | |
589 | const char *opt) | ||
590 | { | ||
591 | int i; | ||
592 | |||
593 |
2/2✓ Branch 0 taken 284528 times.
✓ Branch 1 taken 135764 times.
|
420292 | for (i = 0; i < nb_groups; i++) { |
594 | 284528 | const OptionGroupDef *p = &groups[i]; | |
595 |
4/4✓ Branch 0 taken 142264 times.
✓ Branch 1 taken 142264 times.
✓ Branch 2 taken 6500 times.
✓ Branch 3 taken 135764 times.
|
284528 | if (p->sep && !strcmp(p->sep, opt)) |
596 | 6500 | return i; | |
597 | } | ||
598 | |||
599 | 135764 | return -1; | |
600 | } | ||
601 | |||
602 | /* | ||
603 | * Finish parsing an option group. | ||
604 | * | ||
605 | * @param group_idx which group definition should this group belong to | ||
606 | * @param arg argument of the group delimiting option | ||
607 | */ | ||
608 | 12970 | static void finish_group(OptionParseContext *octx, int group_idx, | |
609 | const char *arg) | ||
610 | { | ||
611 | 12970 | OptionGroupList *l = &octx->groups[group_idx]; | |
612 | OptionGroup *g; | ||
613 | |||
614 | 12970 | GROW_ARRAY(l->groups, l->nb_groups); | |
615 | 12970 | g = &l->groups[l->nb_groups - 1]; | |
616 | |||
617 | 12970 | *g = octx->cur_group; | |
618 | 12970 | g->arg = arg; | |
619 | 12970 | g->group_def = l->group_def; | |
620 | 12970 | g->sws_dict = sws_dict; | |
621 | 12970 | g->swr_opts = swr_opts; | |
622 | 12970 | g->codec_opts = codec_opts; | |
623 | 12970 | g->format_opts = format_opts; | |
624 | |||
625 | 12970 | codec_opts = NULL; | |
626 | 12970 | format_opts = NULL; | |
627 | 12970 | sws_dict = NULL; | |
628 | 12970 | swr_opts = NULL; | |
629 | |||
630 | 12970 | memset(&octx->cur_group, 0, sizeof(octx->cur_group)); | |
631 | 12970 | } | |
632 | |||
633 | /* | ||
634 | * Add an option instance to currently parsed group. | ||
635 | */ | ||
636 | 68789 | static void add_opt(OptionParseContext *octx, const OptionDef *opt, | |
637 | const char *key, const char *val) | ||
638 | { | ||
639 | 68789 | int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET)); | |
640 |
2/2✓ Branch 0 taken 30288 times.
✓ Branch 1 taken 38501 times.
|
68789 | OptionGroup *g = global ? &octx->global_opts : &octx->cur_group; |
641 | |||
642 | 68789 | GROW_ARRAY(g->opts, g->nb_opts); | |
643 | 68789 | g->opts[g->nb_opts - 1].opt = opt; | |
644 | 68789 | g->opts[g->nb_opts - 1].key = key; | |
645 | 68789 | g->opts[g->nb_opts - 1].val = val; | |
646 | 68789 | } | |
647 | |||
648 | 6469 | static void init_parse_context(OptionParseContext *octx, | |
649 | const OptionGroupDef *groups, int nb_groups) | ||
650 | { | ||
651 | static const OptionGroupDef global_group = { "global" }; | ||
652 | int i; | ||
653 | |||
654 | 6469 | memset(octx, 0, sizeof(*octx)); | |
655 | |||
656 | 6469 | octx->nb_groups = nb_groups; | |
657 | 6469 | octx->groups = av_calloc(octx->nb_groups, sizeof(*octx->groups)); | |
658 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6469 times.
|
6469 | if (!octx->groups) |
659 | ✗ | report_and_exit(AVERROR(ENOMEM)); | |
660 | |||
661 |
2/2✓ Branch 0 taken 12938 times.
✓ Branch 1 taken 6469 times.
|
19407 | for (i = 0; i < octx->nb_groups; i++) |
662 | 12938 | octx->groups[i].group_def = &groups[i]; | |
663 | |||
664 | 6469 | octx->global_opts.group_def = &global_group; | |
665 | 6469 | octx->global_opts.arg = ""; | |
666 | 6469 | } | |
667 | |||
668 | 6468 | void uninit_parse_context(OptionParseContext *octx) | |
669 | { | ||
670 | int i, j; | ||
671 | |||
672 |
2/2✓ Branch 0 taken 12936 times.
✓ Branch 1 taken 6468 times.
|
19404 | for (i = 0; i < octx->nb_groups; i++) { |
673 | 12936 | OptionGroupList *l = &octx->groups[i]; | |
674 | |||
675 |
2/2✓ Branch 0 taken 12970 times.
✓ Branch 1 taken 12936 times.
|
25906 | for (j = 0; j < l->nb_groups; j++) { |
676 | 12970 | av_freep(&l->groups[j].opts); | |
677 | 12970 | av_dict_free(&l->groups[j].codec_opts); | |
678 | 12970 | av_dict_free(&l->groups[j].format_opts); | |
679 | |||
680 | 12970 | av_dict_free(&l->groups[j].sws_dict); | |
681 | 12970 | av_dict_free(&l->groups[j].swr_opts); | |
682 | } | ||
683 | 12936 | av_freep(&l->groups); | |
684 | } | ||
685 | 6468 | av_freep(&octx->groups); | |
686 | |||
687 | 6468 | av_freep(&octx->cur_group.opts); | |
688 | 6468 | av_freep(&octx->global_opts.opts); | |
689 | |||
690 | 6468 | uninit_opts(); | |
691 | 6468 | } | |
692 | |||
693 | 6469 | int split_commandline(OptionParseContext *octx, int argc, char *argv[], | |
694 | const OptionDef *options, | ||
695 | const OptionGroupDef *groups, int nb_groups) | ||
696 | { | ||
697 | 6469 | int optindex = 1; | |
698 | 6469 | int dashdash = -2; | |
699 | |||
700 | /* perform system-dependent conversions for arguments list */ | ||
701 | 6469 | prepare_app_arguments(&argc, &argv); | |
702 | |||
703 | 6469 | init_parse_context(octx, groups, nb_groups); | |
704 | 6469 | av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n"); | |
705 | |||
706 |
2/2✓ Branch 0 taken 148734 times.
✓ Branch 1 taken 6469 times.
|
155203 | while (optindex < argc) { |
707 | 148734 | const char *opt = argv[optindex++], *arg; | |
708 | const OptionDef *po; | ||
709 | int ret; | ||
710 | |||
711 | 148734 | av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt); | |
712 | |||
713 |
3/6✓ Branch 0 taken 144735 times.
✓ Branch 1 taken 3999 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 144735 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
148734 | if (opt[0] == '-' && opt[1] == '-' && !opt[2]) { |
714 | ✗ | dashdash = optindex; | |
715 | ✗ | continue; | |
716 | } | ||
717 | /* unnamed group separators, e.g. output filename */ | ||
718 |
5/6✓ Branch 0 taken 144735 times.
✓ Branch 1 taken 3999 times.
✓ Branch 2 taken 142264 times.
✓ Branch 3 taken 2471 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 142264 times.
|
148734 | if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) { |
719 | 6470 | finish_group(octx, 0, opt); | |
720 | 6470 | av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name); | |
721 | 6470 | continue; | |
722 | } | ||
723 | 142264 | opt++; | |
724 | |||
725 | #define GET_ARG(arg) \ | ||
726 | do { \ | ||
727 | arg = argv[optindex++]; \ | ||
728 | if (!arg) { \ | ||
729 | av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\ | ||
730 | return AVERROR(EINVAL); \ | ||
731 | } \ | ||
732 | } while (0) | ||
733 | |||
734 | /* named group separators, e.g. -i */ | ||
735 |
2/2✓ Branch 1 taken 6500 times.
✓ Branch 2 taken 135764 times.
|
142264 | if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) { |
736 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6500 times.
|
6500 | GET_ARG(arg); |
737 | 6500 | finish_group(octx, ret, arg); | |
738 | 6500 | av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n", | |
739 | 6500 | groups[ret].name, arg); | |
740 | 6500 | continue; | |
741 | } | ||
742 | |||
743 | /* normal options */ | ||
744 | 135764 | po = find_option(options, opt); | |
745 |
2/2✓ Branch 0 taken 49434 times.
✓ Branch 1 taken 86330 times.
|
135764 | if (po->name) { |
746 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 49433 times.
|
49434 | if (po->flags & OPT_EXIT) { |
747 | /* optional argument, e.g. -h */ | ||
748 | 1 | arg = argv[optindex++]; | |
749 |
2/2✓ Branch 0 taken 43750 times.
✓ Branch 1 taken 5683 times.
|
49433 | } else if (po->flags & HAS_ARG) { |
750 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43750 times.
|
43750 | GET_ARG(arg); |
751 | } else { | ||
752 | 5683 | arg = "1"; | |
753 | } | ||
754 | |||
755 | 49434 | add_opt(octx, po, opt, arg); | |
756 | 49434 | av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with " | |
757 | 49434 | "argument '%s'.\n", po->name, po->help, arg); | |
758 | 49434 | continue; | |
759 | } | ||
760 | |||
761 | /* AVOptions */ | ||
762 |
1/2✓ Branch 0 taken 86330 times.
✗ Branch 1 not taken.
|
86330 | if (argv[optindex]) { |
763 | 86330 | ret = opt_default(NULL, opt, argv[optindex]); | |
764 |
2/2✓ Branch 0 taken 66975 times.
✓ Branch 1 taken 19355 times.
|
86330 | if (ret >= 0) { |
765 | 66975 | av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with " | |
766 | 66975 | "argument '%s'.\n", opt, argv[optindex]); | |
767 | 66975 | optindex++; | |
768 | 66975 | continue; | |
769 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19355 times.
|
19355 | } else if (ret != AVERROR_OPTION_NOT_FOUND) { |
770 | ✗ | av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' " | |
771 | ✗ | "with argument '%s'.\n", opt, argv[optindex]); | |
772 | ✗ | return ret; | |
773 | } | ||
774 | } | ||
775 | |||
776 | /* boolean -nofoo options */ | ||
777 |
3/6✓ Branch 0 taken 19355 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19355 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19355 times.
✗ Branch 5 not taken.
|
38710 | if (opt[0] == 'n' && opt[1] == 'o' && |
778 | 19355 | (po = find_option(options, opt + 2)) && | |
779 |
2/4✓ Branch 0 taken 19355 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19355 times.
✗ Branch 3 not taken.
|
19355 | po->name && po->flags & OPT_BOOL) { |
780 | 19355 | add_opt(octx, po, opt, "0"); | |
781 | 19355 | av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with " | |
782 | 19355 | "argument 0.\n", po->name, po->help); | |
783 | 19355 | continue; | |
784 | } | ||
785 | |||
786 | ✗ | av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt); | |
787 | ✗ | return AVERROR_OPTION_NOT_FOUND; | |
788 | } | ||
789 | |||
790 |
3/6✓ Branch 0 taken 6469 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6469 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6469 times.
|
6469 | if (octx->cur_group.nb_opts || codec_opts || format_opts) |
791 | ✗ | av_log(NULL, AV_LOG_WARNING, "Trailing option(s) found in the " | |
792 | "command: may be ignored.\n"); | ||
793 | |||
794 | 6469 | av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n"); | |
795 | |||
796 | 6469 | return 0; | |
797 | } | ||
798 | |||
799 | 68 | void print_error(const char *filename, int err) | |
800 | { | ||
801 | 68 | av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, av_err2str(err)); | |
802 | 68 | } | |
803 | |||
804 | ✗ | int read_yesno(void) | |
805 | { | ||
806 | ✗ | int c = getchar(); | |
807 | ✗ | int yesno = (av_toupper(c) == 'Y'); | |
808 | |||
809 | ✗ | while (c != '\n' && c != EOF) | |
810 | ✗ | c = getchar(); | |
811 | |||
812 | ✗ | return yesno; | |
813 | } | ||
814 | |||
815 | ✗ | FILE *get_preset_file(char *filename, size_t filename_size, | |
816 | const char *preset_name, int is_path, | ||
817 | const char *codec_name) | ||
818 | { | ||
819 | ✗ | FILE *f = NULL; | |
820 | int i; | ||
821 | #if HAVE_GETMODULEHANDLE && defined(_WIN32) | ||
822 | char *datadir = NULL; | ||
823 | #endif | ||
824 | ✗ | char *env_home = getenv_utf8("HOME"); | |
825 | ✗ | char *env_ffmpeg_datadir = getenv_utf8("FFMPEG_DATADIR"); | |
826 | ✗ | const char *base[3] = { env_ffmpeg_datadir, | |
827 | env_home, /* index=1(HOME) is special: search in a .ffmpeg subfolder */ | ||
828 | FFMPEG_DATADIR, }; | ||
829 | |||
830 | ✗ | if (is_path) { | |
831 | ✗ | av_strlcpy(filename, preset_name, filename_size); | |
832 | ✗ | f = fopen_utf8(filename, "r"); | |
833 | } else { | ||
834 | #if HAVE_GETMODULEHANDLE && defined(_WIN32) | ||
835 | wchar_t *datadir_w = get_module_filename(NULL); | ||
836 | base[2] = NULL; | ||
837 | |||
838 | if (wchartoutf8(datadir_w, &datadir)) | ||
839 | datadir = NULL; | ||
840 | av_free(datadir_w); | ||
841 | |||
842 | if (datadir) | ||
843 | { | ||
844 | char *ls; | ||
845 | for (ls = datadir; *ls; ls++) | ||
846 | if (*ls == '\\') *ls = '/'; | ||
847 | |||
848 | if (ls = strrchr(datadir, '/')) | ||
849 | { | ||
850 | ptrdiff_t datadir_len = ls - datadir; | ||
851 | size_t desired_size = datadir_len + strlen("/ffpresets") + 1; | ||
852 | char *new_datadir = av_realloc_array( | ||
853 | datadir, desired_size, sizeof *datadir); | ||
854 | if (new_datadir) { | ||
855 | datadir = new_datadir; | ||
856 | datadir[datadir_len] = 0; | ||
857 | strncat(datadir, "/ffpresets", desired_size - 1 - datadir_len); | ||
858 | base[2] = datadir; | ||
859 | } | ||
860 | } | ||
861 | } | ||
862 | #endif | ||
863 | ✗ | for (i = 0; i < 3 && !f; i++) { | |
864 | ✗ | if (!base[i]) | |
865 | ✗ | continue; | |
866 | ✗ | snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i], | |
867 | i != 1 ? "" : "/.ffmpeg", preset_name); | ||
868 | ✗ | f = fopen_utf8(filename, "r"); | |
869 | ✗ | if (!f && codec_name) { | |
870 | ✗ | snprintf(filename, filename_size, | |
871 | "%s%s/%s-%s.ffpreset", | ||
872 | base[i], i != 1 ? "" : "/.ffmpeg", codec_name, | ||
873 | preset_name); | ||
874 | ✗ | f = fopen_utf8(filename, "r"); | |
875 | } | ||
876 | } | ||
877 | } | ||
878 | |||
879 | #if HAVE_GETMODULEHANDLE && defined(_WIN32) | ||
880 | av_free(datadir); | ||
881 | #endif | ||
882 | ✗ | freeenv_utf8(env_ffmpeg_datadir); | |
883 | ✗ | freeenv_utf8(env_home); | |
884 | ✗ | return f; | |
885 | } | ||
886 | |||
887 | 27676 | int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec) | |
888 | { | ||
889 | 27676 | int ret = avformat_match_stream_specifier(s, st, spec); | |
890 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27676 times.
|
27676 | if (ret < 0) |
891 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec); | |
892 | 27676 | return ret; | |
893 | } | ||
894 | |||
895 | 20903 | AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id, | |
896 | AVFormatContext *s, AVStream *st, const AVCodec *codec) | ||
897 | { | ||
898 | 20903 | AVDictionary *ret = NULL; | |
899 | 20903 | const AVDictionaryEntry *t = NULL; | |
900 | 41806 | int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM | |
901 |
2/2✓ Branch 0 taken 6724 times.
✓ Branch 1 taken 14179 times.
|
20903 | : AV_OPT_FLAG_DECODING_PARAM; |
902 | 20903 | char prefix = 0; | |
903 | 20903 | const AVClass *cc = avcodec_get_class(); | |
904 | |||
905 |
2/2✓ Branch 0 taken 7582 times.
✓ Branch 1 taken 13321 times.
|
20903 | if (!codec) |
906 | 7582 | codec = s->oformat ? avcodec_find_encoder(codec_id) | |
907 |
2/2✓ Branch 0 taken 492 times.
✓ Branch 1 taken 7090 times.
|
7582 | : avcodec_find_decoder(codec_id); |
908 | |||
909 |
4/4✓ Branch 0 taken 15926 times.
✓ Branch 1 taken 4613 times.
✓ Branch 2 taken 224 times.
✓ Branch 3 taken 140 times.
|
20903 | switch (st->codecpar->codec_type) { |
910 | 15926 | case AVMEDIA_TYPE_VIDEO: | |
911 | 15926 | prefix = 'v'; | |
912 | 15926 | flags |= AV_OPT_FLAG_VIDEO_PARAM; | |
913 | 15926 | break; | |
914 | 4613 | case AVMEDIA_TYPE_AUDIO: | |
915 | 4613 | prefix = 'a'; | |
916 | 4613 | flags |= AV_OPT_FLAG_AUDIO_PARAM; | |
917 | 4613 | break; | |
918 | 224 | case AVMEDIA_TYPE_SUBTITLE: | |
919 | 224 | prefix = 's'; | |
920 | 224 | flags |= AV_OPT_FLAG_SUBTITLE_PARAM; | |
921 | 224 | break; | |
922 | } | ||
923 | |||
924 |
2/2✓ Branch 1 taken 60536 times.
✓ Branch 2 taken 20903 times.
|
81439 | while (t = av_dict_iterate(opts, t)) { |
925 | const AVClass *priv_class; | ||
926 | 60536 | char *p = strchr(t->key, ':'); | |
927 | |||
928 | /* check stream specification in opt name */ | ||
929 |
2/2✓ Branch 0 taken 253 times.
✓ Branch 1 taken 60283 times.
|
60536 | if (p) |
930 |
2/3✓ Branch 1 taken 203 times.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
|
253 | switch (check_stream_specifier(s, st, p + 1)) { |
931 | 203 | case 1: *p = 0; break; | |
932 | 50 | case 0: continue; | |
933 | ✗ | default: exit_program(1); | |
934 | } | ||
935 | |||
936 |
4/4✓ Branch 1 taken 2264 times.
✓ Branch 2 taken 58222 times.
✓ Branch 3 taken 1953 times.
✓ Branch 4 taken 311 times.
|
60486 | if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) || |
937 | 1953 | !codec || | |
938 |
4/4✓ Branch 0 taken 706 times.
✓ Branch 1 taken 1247 times.
✓ Branch 2 taken 312 times.
✓ Branch 3 taken 394 times.
|
2659 | ((priv_class = codec->priv_class) && |
939 | 706 | av_opt_find(&priv_class, t->key, NULL, flags, | |
940 | AV_OPT_SEARCH_FAKE_OBJ))) | ||
941 | 58845 | av_dict_set(&ret, t->key, t->value, 0); | |
942 |
4/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1638 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
|
1644 | else if (t->key[0] == prefix && |
943 | 3 | av_opt_find(&cc, t->key + 1, NULL, flags, | |
944 | AV_OPT_SEARCH_FAKE_OBJ)) | ||
945 | 1 | av_dict_set(&ret, t->key + 1, t->value, 0); | |
946 | |||
947 |
2/2✓ Branch 0 taken 203 times.
✓ Branch 1 taken 60283 times.
|
60486 | if (p) |
948 | 203 | *p = ':'; | |
949 | } | ||
950 | 20903 | return ret; | |
951 | } | ||
952 | |||
953 | 6623 | AVDictionary **setup_find_stream_info_opts(AVFormatContext *s, | |
954 | AVDictionary *codec_opts) | ||
955 | { | ||
956 | int i; | ||
957 | AVDictionary **opts; | ||
958 | |||
959 |
2/2✓ Branch 0 taken 81 times.
✓ Branch 1 taken 6542 times.
|
6623 | if (!s->nb_streams) |
960 | 81 | return NULL; | |
961 | 6542 | opts = av_calloc(s->nb_streams, sizeof(*opts)); | |
962 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6542 times.
|
6542 | if (!opts) |
963 | ✗ | report_and_exit(AVERROR(ENOMEM)); | |
964 |
2/2✓ Branch 0 taken 7030 times.
✓ Branch 1 taken 6542 times.
|
13572 | for (i = 0; i < s->nb_streams; i++) |
965 | 7030 | opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id, | |
966 | 7030 | s, s->streams[i], NULL); | |
967 | 6542 | return opts; | |
968 | } | ||
969 | |||
970 | 120542 | void *grow_array(void *array, int elem_size, int *size, int new_size) | |
971 | { | ||
972 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 120542 times.
|
120542 | if (new_size >= INT_MAX / elem_size) { |
973 | ✗ | av_log(NULL, AV_LOG_ERROR, "Array too big.\n"); | |
974 | ✗ | exit_program(1); | |
975 | } | ||
976 |
1/2✓ Branch 0 taken 120542 times.
✗ Branch 1 not taken.
|
120542 | if (*size < new_size) { |
977 | 120542 | uint8_t *tmp = av_realloc_array(array, new_size, elem_size); | |
978 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 120542 times.
|
120542 | if (!tmp) |
979 | ✗ | report_and_exit(AVERROR(ENOMEM)); | |
980 | 120542 | memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size); | |
981 | 120542 | *size = new_size; | |
982 | 120542 | return tmp; | |
983 | } | ||
984 | ✗ | return array; | |
985 | } | ||
986 | |||
987 | 39105 | void *allocate_array_elem(void *ptr, size_t elem_size, int *nb_elems) | |
988 | { | ||
989 | void *new_elem; | ||
990 | |||
991 |
2/4✓ Branch 1 taken 39105 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 39105 times.
|
78210 | if (!(new_elem = av_mallocz(elem_size)) || |
992 | 39105 | av_dynarray_add_nofree(ptr, nb_elems, new_elem) < 0) | |
993 | ✗ | report_and_exit(AVERROR(ENOMEM)); | |
994 | 39105 | return new_elem; | |
995 | } | ||
996 | |||
997 | 5015 | double get_rotation(int32_t *displaymatrix) | |
998 | { | ||
999 | 5015 | double theta = 0; | |
1000 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5014 times.
|
5015 | if (displaymatrix) |
1001 | 1 | theta = -round(av_display_rotation_get((int32_t*) displaymatrix)); | |
1002 | |||
1003 | 5015 | theta -= 360*floor(theta/360 + 0.9/360); | |
1004 | |||
1005 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5015 times.
|
5015 | if (fabs(theta - 90*round(theta/90)) > 2) |
1006 | ✗ | av_log(NULL, AV_LOG_WARNING, "Odd rotation angle.\n" | |
1007 | "If you want to help, upload a sample " | ||
1008 | "of this file to https://streams.videolan.org/upload/ " | ||
1009 | "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)"); | ||
1010 | |||
1011 | 5015 | return theta; | |
1012 | } | ||
1013 |