FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/cmdutils.c
Date: 2024-07-14 13:34:57
Exec Total Coverage
Lines: 423 584 72.4%
Functions: 30 36 83.3%
Branches: 291 472 61.7%

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 "libswresample/swresample.h"
37 #include "libavutil/avassert.h"
38 #include "libavutil/avstring.h"
39 #include "libavutil/bprint.h"
40 #include "libavutil/display.h"
41 #include "libavutil/getenv_utf8.h"
42 #include "libavutil/libm.h"
43 #include "libavutil/mem.h"
44 #include "libavutil/parseutils.h"
45 #include "libavutil/eval.h"
46 #include "libavutil/dict.h"
47 #include "libavutil/opt.h"
48 #include "cmdutils.h"
49 #include "fopen_utf8.h"
50 #include "opt_common.h"
51 #ifdef _WIN32
52 #include <windows.h>
53 #include "compat/w32dlfcn.h"
54 #endif
55
56 AVDictionary *sws_dict;
57 AVDictionary *swr_opts;
58 AVDictionary *format_opts, *codec_opts;
59
60 int hide_banner = 0;
61
62 13699 void uninit_opts(void)
63 {
64 13699 av_dict_free(&swr_opts);
65 13699 av_dict_free(&sws_dict);
66 13699 av_dict_free(&format_opts);
67 13699 av_dict_free(&codec_opts);
68 13699 }
69
70 void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
71 {
72 vfprintf(stdout, fmt, vl);
73 }
74
75 6924 void init_dynload(void)
76 {
77 #if HAVE_SETDLLDIRECTORY && defined(_WIN32)
78 /* Calling SetDllDirectory with the empty string (but not NULL) removes the
79 * current working directory from the DLL search path as a security pre-caution. */
80 SetDllDirectory("");
81 #endif
82 6924 }
83
84 31636 int parse_number(const char *context, const char *numstr, enum OptionType type,
85 double min, double max, double *dst)
86 {
87 char *tail;
88 const char *error;
89 31636 double d = av_strtod(numstr, &tail);
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31636 times.
31636 if (*tail)
91 error = "Expected number for %s but found: %s\n";
92
2/4
✓ Branch 0 taken 31636 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 31636 times.
31636 else if (d < min || d > max)
93 error = "The value for %s was %s which is not within %f - %f\n";
94
3/4
✓ Branch 0 taken 31343 times.
✓ Branch 1 taken 293 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 31343 times.
31636 else if (type == OPT_TYPE_INT64 && (int64_t)d != d)
95 error = "Expected int64 for %s but found %s\n";
96
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 31636 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
31636 else if (type == OPT_TYPE_INT && (int)d != d)
97 error = "Expected int for %s but found %s\n";
98 else {
99 31636 *dst = d;
100 31636 return 0;
101 }
102
103 av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
104 return AVERROR(EINVAL);
105 }
106
107 void show_help_options(const OptionDef *options, const char *msg, int req_flags,
108 int rej_flags)
109 {
110 const OptionDef *po;
111 int first;
112
113 first = 1;
114 for (po = options; po->name; po++) {
115 char buf[128];
116
117 if (((po->flags & req_flags) != req_flags) ||
118 (po->flags & rej_flags))
119 continue;
120
121 if (first) {
122 printf("%s\n", msg);
123 first = 0;
124 }
125 av_strlcpy(buf, po->name, sizeof(buf));
126
127 if (po->flags & OPT_FLAG_PERSTREAM)
128 av_strlcat(buf, "[:<stream_spec>]", sizeof(buf));
129 else if (po->flags & OPT_FLAG_SPEC)
130 av_strlcat(buf, "[:<spec>]", sizeof(buf));
131
132 if (po->argname)
133 av_strlcatf(buf, sizeof(buf), " <%s>", po->argname);
134
135 printf("-%-17s %s\n", buf, po->help);
136 }
137 printf("\n");
138 }
139
140 void show_help_children(const AVClass *class, int flags)
141 {
142 void *iter = NULL;
143 const AVClass *child;
144 if (class->option) {
145 av_opt_show2(&class, NULL, flags, 0);
146 printf("\n");
147 }
148
149 while (child = av_opt_child_class_iterate(class, &iter))
150 show_help_children(child, flags);
151 }
152
153 1034408 static const OptionDef *find_option(const OptionDef *po, const char *name)
154 {
155
2/2
✓ Branch 0 taken 318 times.
✓ Branch 1 taken 1034090 times.
1034408 if (*name == '/')
156 318 name++;
157
158
2/2
✓ Branch 0 taken 148270483 times.
✓ Branch 1 taken 587498 times.
148857981 while (po->name) {
159 const char *end;
160
6/6
✓ Branch 1 taken 1048664 times.
✓ Branch 2 taken 147221819 times.
✓ Branch 3 taken 648381 times.
✓ Branch 4 taken 400283 times.
✓ Branch 5 taken 601754 times.
✓ Branch 6 taken 46627 times.
148270483 if (av_strstart(name, po->name, &end) && (!*end || *end == ':'))
161 break;
162 147823573 po++;
163 }
164 1034408 return po;
165 }
166
167 /* _WIN32 means using the windows libc - cygwin doesn't define that
168 * by default. HAVE_COMMANDLINETOARGVW is true on cygwin, while
169 * it doesn't provide the actual command line via GetCommandLineW(). */
170 #if HAVE_COMMANDLINETOARGVW && defined(_WIN32)
171 #include <shellapi.h>
172 /* Will be leaked on exit */
173 static char** win32_argv_utf8 = NULL;
174 static int win32_argc = 0;
175
176 /**
177 * Prepare command line arguments for executable.
178 * For Windows - perform wide-char to UTF-8 conversion.
179 * Input arguments should be main() function arguments.
180 * @param argc_ptr Arguments number (including executable)
181 * @param argv_ptr Arguments list.
182 */
183 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
184 {
185 char *argstr_flat;
186 wchar_t **argv_w;
187 int i, buffsize = 0, offset = 0;
188
189 if (win32_argv_utf8) {
190 *argc_ptr = win32_argc;
191 *argv_ptr = win32_argv_utf8;
192 return;
193 }
194
195 win32_argc = 0;
196 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
197 if (win32_argc <= 0 || !argv_w)
198 return;
199
200 /* determine the UTF-8 buffer size (including NULL-termination symbols) */
201 for (i = 0; i < win32_argc; i++)
202 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
203 NULL, 0, NULL, NULL);
204
205 win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
206 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
207 if (!win32_argv_utf8) {
208 LocalFree(argv_w);
209 return;
210 }
211
212 for (i = 0; i < win32_argc; i++) {
213 win32_argv_utf8[i] = &argstr_flat[offset];
214 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
215 &argstr_flat[offset],
216 buffsize - offset, NULL, NULL);
217 }
218 win32_argv_utf8[i] = NULL;
219 LocalFree(argv_w);
220
221 *argc_ptr = win32_argc;
222 *argv_ptr = win32_argv_utf8;
223 }
224 #else
225 6924 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
226 {
227 /* nothing to do */
228 6924 }
229 #endif /* HAVE_COMMANDLINETOARGVW */
230
231 433257 static int opt_has_arg(const OptionDef *o)
232 {
233
2/2
✓ Branch 0 taken 138336 times.
✓ Branch 1 taken 294921 times.
433257 if (o->type == OPT_TYPE_BOOL)
234 138336 return 0;
235
2/2
✓ Branch 0 taken 99049 times.
✓ Branch 1 taken 195872 times.
294921 if (o->type == OPT_TYPE_FUNC)
236 99049 return !!(o->flags & OPT_FUNC_ARG);
237 195872 return 1;
238 }
239
240 81189 static int write_option(void *optctx, const OptionDef *po, const char *opt,
241 const char *arg, const OptionDef *defs)
242 {
243 /* new-style options contain an offset into optctx, old-style address of
244 * a global var*/
245 162378 void *dst = po->flags & OPT_FLAG_OFFSET ?
246
2/2
✓ Branch 0 taken 40402 times.
✓ Branch 1 taken 40787 times.
81189 (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
247 81189 char *arg_allocated = NULL;
248
249 81189 SpecifierOptList *sol = NULL;
250 double num;
251 81189 int ret = 0;
252
253
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 81136 times.
81189 if (*opt == '/') {
254 53 opt++;
255
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if (po->type == OPT_TYPE_BOOL) {
257 av_log(NULL, AV_LOG_FATAL,
258 "Requested to load an argument from file for a bool option '%s'\n",
259 po->name);
260 return AVERROR(EINVAL);
261 }
262
263 53 arg_allocated = file_read(arg);
264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if (!arg_allocated) {
265 av_log(NULL, AV_LOG_FATAL,
266 "Error reading the value for option '%s' from file: %s\n",
267 opt, arg);
268 return AVERROR(EINVAL);
269 }
270
271 53 arg = arg_allocated;
272 }
273
274
2/2
✓ Branch 0 taken 28031 times.
✓ Branch 1 taken 53158 times.
81189 if (po->flags & OPT_FLAG_SPEC) {
275 28031 char *p = strchr(opt, ':');
276 char *str;
277
278 28031 sol = dst;
279 28031 ret = GROW_ARRAY(sol->opt, sol->nb_opt);
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28031 times.
28031 if (ret < 0)
281 goto finish;
282
283
2/2
✓ Branch 0 taken 14658 times.
✓ Branch 1 taken 13373 times.
28031 str = av_strdup(p ? p + 1 : "");
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28031 times.
28031 if (!str) {
285 ret = AVERROR(ENOMEM);
286 goto finish;
287 }
288 28031 sol->opt[sol->nb_opt - 1].specifier = str;
289 28031 dst = &sol->opt[sol->nb_opt - 1].u;
290 }
291
292
2/2
✓ Branch 0 taken 32864 times.
✓ Branch 1 taken 48325 times.
81189 if (po->type == OPT_TYPE_STRING) {
293 char *str;
294
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 32844 times.
32864 if (arg_allocated) {
295 20 str = arg_allocated;
296 20 arg_allocated = NULL;
297 } else
298 32844 str = av_strdup(arg);
299 32864 av_freep(dst);
300
301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32864 times.
32864 if (!str) {
302 ret = AVERROR(ENOMEM);
303 goto finish;
304 }
305
306 32864 *(char **)dst = str;
307
4/4
✓ Branch 0 taken 21913 times.
✓ Branch 1 taken 26412 times.
✓ Branch 2 taken 129 times.
✓ Branch 3 taken 21784 times.
48325 } else if (po->type == OPT_TYPE_BOOL || po->type == OPT_TYPE_INT) {
308 26541 ret = parse_number(opt, arg, OPT_TYPE_INT64, INT_MIN, INT_MAX, &num);
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26541 times.
26541 if (ret < 0)
310 goto finish;
311
312 26541 *(int *)dst = num;
313
2/2
✓ Branch 0 taken 4802 times.
✓ Branch 1 taken 16982 times.
21784 } else if (po->type == OPT_TYPE_INT64) {
314 4802 ret = parse_number(opt, arg, OPT_TYPE_INT64, INT64_MIN, (double)INT64_MAX, &num);
315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4802 times.
4802 if (ret < 0)
316 goto finish;
317
318 4802 *(int64_t *)dst = num;
319
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 16766 times.
16982 } else if (po->type == OPT_TYPE_TIME) {
320 216 ret = av_parse_time(dst, arg, 1);
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
216 if (ret < 0) {
322 av_log(NULL, AV_LOG_ERROR, "Invalid duration for option %s: %s\n",
323 opt, arg);
324 goto finish;
325 }
326
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 16763 times.
16766 } else if (po->type == OPT_TYPE_FLOAT) {
327 3 ret = parse_number(opt, arg, OPT_TYPE_FLOAT, -INFINITY, INFINITY, &num);
328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
329 goto finish;
330
331 3 *(float *)dst = num;
332
2/2
✓ Branch 0 taken 290 times.
✓ Branch 1 taken 16473 times.
16763 } else if (po->type == OPT_TYPE_DOUBLE) {
333 290 ret = parse_number(opt, arg, OPT_TYPE_DOUBLE, -INFINITY, INFINITY, &num);
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 290 times.
290 if (ret < 0)
335 goto finish;
336
337 290 *(double *)dst = num;
338 } else {
339
2/4
✓ Branch 0 taken 16473 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16473 times.
16473 av_assert0(po->type == OPT_TYPE_FUNC && po->u.func_arg);
340
341 16473 ret = po->u.func_arg(optctx, opt, arg);
342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16473 times.
16473 if (ret < 0) {
343 av_log(NULL, AV_LOG_ERROR,
344 "Failed to set value '%s' for option '%s': %s\n",
345 arg, opt, av_err2str(ret));
346 goto finish;
347 }
348 }
349
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 81188 times.
81189 if (po->flags & OPT_EXIT) {
350 1 ret = AVERROR_EXIT;
351 1 goto finish;
352 }
353
354
2/2
✓ Branch 0 taken 53157 times.
✓ Branch 1 taken 28031 times.
81188 if (sol) {
355 28031 sol->type = po->type;
356
2/2
✓ Branch 0 taken 2404 times.
✓ Branch 1 taken 25627 times.
30435 sol->opt_canon = (po->flags & OPT_HAS_CANON) ?
357 2404 find_option(defs, po->u1.name_canon) : po;
358 }
359
360 53157 finish:
361 81189 av_freep(&arg_allocated);
362 81189 return ret;
363 }
364
365 9033 int parse_option(void *optctx, const char *opt, const char *arg,
366 const OptionDef *options)
367 {
368 static const OptionDef opt_avoptions = {
369 .name = "AVOption passthrough",
370 .type = OPT_TYPE_FUNC,
371 .flags = OPT_FUNC_ARG,
372 .u.func_arg = opt_default,
373 };
374
375 const OptionDef *po;
376 int ret;
377
378 9033 po = find_option(options, opt);
379
5/6
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9024 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
9033 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
380 /* handle 'no' bool option */
381 1 po = find_option(options, opt + 2);
382
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->type == OPT_TYPE_BOOL))
383 1 arg = "0";
384
2/2
✓ Branch 0 taken 141 times.
✓ Branch 1 taken 8891 times.
9032 } else if (po->type == OPT_TYPE_BOOL)
385 141 arg = "1";
386
387
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 9025 times.
9033 if (!po->name)
388 8 po = &opt_avoptions;
389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9033 times.
9033 if (!po->name) {
390 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
391 return AVERROR(EINVAL);
392 }
393
3/4
✓ Branch 1 taken 8786 times.
✓ Branch 2 taken 247 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8786 times.
9033 if (opt_has_arg(po) && !arg) {
394 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
395 return AVERROR(EINVAL);
396 }
397
398 9033 ret = write_option(optctx, po, opt, arg, options);
399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9033 times.
9033 if (ret < 0)
400 return ret;
401
402 9033 return opt_has_arg(po);
403 }
404
405 149 int parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
406 int (*parse_arg_function)(void *, const char*))
407 {
408 const char *opt;
409 149 int optindex, handleoptions = 1, ret;
410
411 /* perform system-dependent conversions for arguments list */
412 149 prepare_app_arguments(&argc, &argv);
413
414 /* parse options */
415 149 optindex = 1;
416
2/2
✓ Branch 0 taken 661 times.
✓ Branch 1 taken 149 times.
810 while (optindex < argc) {
417 661 opt = argv[optindex++];
418
419
4/6
✓ Branch 0 taken 661 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 522 times.
✓ Branch 3 taken 139 times.
✓ Branch 4 taken 522 times.
✗ Branch 5 not taken.
661 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
420
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 522 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
522 if (opt[1] == '-' && opt[2] == '\0') {
421 handleoptions = 0;
422 continue;
423 }
424 522 opt++;
425
426
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 522 times.
522 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
427 return ret;
428 522 optindex += ret;
429 } else {
430
1/2
✓ Branch 0 taken 139 times.
✗ Branch 1 not taken.
139 if (parse_arg_function) {
431 139 ret = parse_arg_function(optctx, opt);
432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
139 if (ret < 0)
433 return ret;
434 }
435 }
436 }
437
438 149 return 0;
439 }
440
441 20357 int parse_optgroup(void *optctx, OptionGroup *g, const OptionDef *defs)
442 {
443 int i, ret;
444
445 20357 av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
446 20357 g->group_def->name, g->arg);
447
448
2/2
✓ Branch 0 taken 72156 times.
✓ Branch 1 taken 20356 times.
92512 for (i = 0; i < g->nb_opts; i++) {
449 72156 Option *o = &g->opts[i];
450
451
2/2
✓ Branch 0 taken 40893 times.
✓ Branch 1 taken 31263 times.
72156 if (g->group_def->flags &&
452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40893 times.
40893 !(g->group_def->flags & o->opt->flags)) {
453 av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
454 "%s %s -- you are trying to apply an input option to an "
455 "output file or vice versa. Move this option before the "
456 "file it belongs to.\n", o->key, o->opt->help,
457 g->group_def->name, g->arg);
458 return AVERROR(EINVAL);
459 }
460
461 72156 av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
462 72156 o->key, o->opt->help, o->val);
463
464 72156 ret = write_option(optctx, o->opt, o->key, o->val, defs);
465
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 72155 times.
72156 if (ret < 0)
466 1 return ret;
467 }
468
469 20356 av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
470
471 20356 return 0;
472 }
473
474 34620 int locate_option(int argc, char **argv, const OptionDef *options,
475 const char *optname)
476 {
477 const OptionDef *po;
478 int i;
479
480
2/2
✓ Branch 0 taken 780909 times.
✓ Branch 1 taken 34604 times.
815513 for (i = 1; i < argc; i++) {
481 780909 const char *cur_opt = argv[i];
482
483
2/2
✓ Branch 0 taken 21567 times.
✓ Branch 1 taken 759342 times.
780909 if (*cur_opt++ != '-')
484 21567 continue;
485
486 759342 po = find_option(options, cur_opt);
487
5/6
✓ Branch 0 taken 497387 times.
✓ Branch 1 taken 261955 times.
✓ Branch 2 taken 101410 times.
✓ Branch 3 taken 395977 times.
✓ Branch 4 taken 101410 times.
✗ Branch 5 not taken.
759342 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
488 101410 po = find_option(options, cur_opt + 2);
489
490
3/4
✓ Branch 0 taken 396017 times.
✓ Branch 1 taken 363325 times.
✓ Branch 2 taken 396017 times.
✗ Branch 3 not taken.
759342 if ((!po->name && !strcmp(cur_opt, optname)) ||
491
4/4
✓ Branch 0 taken 363325 times.
✓ Branch 1 taken 396017 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 363309 times.
759342 (po->name && !strcmp(optname, po->name)))
492 16 return i;
493
494
4/4
✓ Branch 0 taken 363309 times.
✓ Branch 1 taken 396017 times.
✓ Branch 3 taken 230724 times.
✓ Branch 4 taken 132585 times.
759326 if (!po->name || opt_has_arg(po))
495 626741 i++;
496 }
497 34604 return 0;
498 }
499
500 static void dump_argument(FILE *report_file, const char *a)
501 {
502 const unsigned char *p;
503
504 for (p = a; *p; p++)
505 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
506 *p == '_' || (*p >= 'a' && *p <= 'z')))
507 break;
508 if (!*p) {
509 fputs(a, report_file);
510 return;
511 }
512 fputc('"', report_file);
513 for (p = a; *p; p++) {
514 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
515 fprintf(report_file, "\\%c", *p);
516 else if (*p < ' ' || *p > '~')
517 fprintf(report_file, "\\x%02x", *p);
518 else
519 fputc(*p, report_file);
520 }
521 fputc('"', report_file);
522 }
523
524 6924 static void check_options(const OptionDef *po)
525 {
526
2/2
✓ Branch 0 taken 1297531 times.
✓ Branch 1 taken 6924 times.
1304455 while (po->name) {
527
2/2
✓ Branch 0 taken 799450 times.
✓ Branch 1 taken 498081 times.
1297531 if (po->flags & OPT_PERFILE)
528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 799450 times.
799450 av_assert0(po->flags & (OPT_INPUT | OPT_OUTPUT | OPT_DECODER));
529
530
2/2
✓ Branch 0 taken 515724 times.
✓ Branch 1 taken 781807 times.
1297531 if (po->type == OPT_TYPE_FUNC)
531
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 515724 times.
515724 av_assert0(!(po->flags & (OPT_FLAG_OFFSET | OPT_FLAG_SPEC)));
532
533 // OPT_FUNC_ARG can only be ser for OPT_TYPE_FUNC
534
3/4
✓ Branch 0 taken 781807 times.
✓ Branch 1 taken 515724 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 781807 times.
1297531 av_assert0((po->type == OPT_TYPE_FUNC) || !(po->flags & OPT_FUNC_ARG));
535
536 1297531 po++;
537 }
538 6924 }
539
540 6924 void parse_loglevel(int argc, char **argv, const OptionDef *options)
541 {
542 6924 int idx = locate_option(argc, argv, options, "loglevel");
543 char *env;
544
545 6924 check_options(options);
546
547
1/2
✓ Branch 0 taken 6924 times.
✗ Branch 1 not taken.
6924 if (!idx)
548 6924 idx = locate_option(argc, argv, options, "v");
549
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6908 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
6924 if (idx && argv[idx + 1])
550 16 opt_loglevel(NULL, "loglevel", argv[idx + 1]);
551 6924 idx = locate_option(argc, argv, options, "report");
552 6924 env = getenv_utf8("FFREPORT");
553
2/4
✓ Branch 0 taken 6924 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6924 times.
6924 if (env || idx) {
554 FILE *report_file = NULL;
555 init_report(env, &report_file);
556 if (report_file) {
557 int i;
558 fprintf(report_file, "Command line:\n");
559 for (i = 0; i < argc; i++) {
560 dump_argument(report_file, argv[i]);
561 fputc(i < argc - 1 ? ' ' : '\n', report_file);
562 }
563 fflush(report_file);
564 }
565 }
566 6924 freeenv_utf8(env);
567 6924 idx = locate_option(argc, argv, options, "hide_banner");
568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6924 times.
6924 if (idx)
569 hide_banner = 1;
570 6924 }
571
572 244847 static const AVOption *opt_find(void *obj, const char *name, const char *unit,
573 int opt_flags, int search_flags)
574 {
575 244847 const AVOption *o = av_opt_find(obj, name, unit, opt_flags, search_flags);
576
4/4
✓ Branch 0 taken 69897 times.
✓ Branch 1 taken 174950 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 69895 times.
244847 if(o && !o->flags)
577 2 return NULL;
578 244845 return o;
579 }
580
581 #define FLAGS ((o->type == AV_OPT_TYPE_FLAGS && (arg[0]=='-' || arg[0]=='+')) ? AV_DICT_APPEND : 0)
582 90070 int opt_default(void *optctx, const char *opt, const char *arg)
583 {
584 const AVOption *o;
585 90070 int consumed = 0;
586 char opt_stripped[128];
587 const char *p;
588 90070 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
589 #if CONFIG_SWSCALE
590 90070 const AVClass *sc = sws_get_class();
591 #endif
592 #if CONFIG_SWRESAMPLE
593 90070 const AVClass *swr_class = swr_get_class();
594 #endif
595
596
2/4
✓ Branch 0 taken 90070 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 90070 times.
90070 if (!strcmp(opt, "debug") || !strcmp(opt, "fdebug"))
597 av_log_set_level(AV_LOG_DEBUG);
598
599
2/2
✓ Branch 0 taken 90058 times.
✓ Branch 1 taken 12 times.
90070 if (!(p = strchr(opt, ':')))
600 90058 p = opt + strlen(opt);
601 90070 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
602
603
2/2
✓ Branch 1 taken 44359 times.
✓ Branch 2 taken 45711 times.
90070 if ((o = opt_find(&cc, opt_stripped, NULL, 0,
604 44359 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
605
8/8
✓ Branch 0 taken 44356 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 44344 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 12085 times.
✓ Branch 5 taken 32259 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 12099 times.
56459 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
606 12100 (o = opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) {
607
5/6
✓ Branch 0 taken 18472 times.
✓ Branch 1 taken 27240 times.
✓ Branch 2 taken 18472 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12036 times.
✓ Branch 5 taken 6436 times.
45712 av_dict_set(&codec_opts, opt, arg, FLAGS);
608 45712 consumed = 1;
609 }
610
2/2
✓ Branch 1 taken 12122 times.
✓ Branch 2 taken 77948 times.
90070 if ((o = opt_find(&fc, opt, NULL, 0,
611 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
612
5/6
✓ Branch 0 taken 11882 times.
✓ Branch 1 taken 240 times.
✓ Branch 2 taken 11882 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11874 times.
✓ Branch 5 taken 8 times.
12122 av_dict_set(&format_opts, opt, arg, FLAGS);
613
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 12024 times.
12122 if (consumed)
614 98 av_log(NULL, AV_LOG_VERBOSE, "Routing option %s to both codec and muxer layer\n", opt);
615 12122 consumed = 1;
616 }
617 #if CONFIG_SWSCALE
618
4/4
✓ Branch 0 taken 32334 times.
✓ Branch 1 taken 57736 times.
✓ Branch 3 taken 12061 times.
✓ Branch 4 taken 20273 times.
90070 if (!consumed && (o = opt_find(&sc, opt, NULL, 0,
619 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
620
2/4
✓ Branch 0 taken 12061 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12061 times.
✗ Branch 3 not taken.
12061 if (!strcmp(opt, "srcw") || !strcmp(opt, "srch") ||
621
2/4
✓ Branch 0 taken 12061 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12061 times.
✗ Branch 3 not taken.
12061 !strcmp(opt, "dstw") || !strcmp(opt, "dsth") ||
622
2/4
✓ Branch 0 taken 12061 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12061 times.
12061 !strcmp(opt, "src_format") || !strcmp(opt, "dst_format")) {
623 av_log(NULL, AV_LOG_ERROR, "Directly using swscale dimensions/format options is not supported, please use the -s or -pix_fmt options\n");
624 return AVERROR(EINVAL);
625 }
626
4/6
✓ Branch 0 taken 12061 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12061 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11876 times.
✓ Branch 5 taken 185 times.
12061 av_dict_set(&sws_dict, opt, arg, FLAGS);
627
628 12061 consumed = 1;
629 }
630 #else
631 if (!consumed && !strcmp(opt, "sws_flags")) {
632 av_log(NULL, AV_LOG_WARNING, "Ignoring %s %s, due to disabled swscale\n", opt, arg);
633 consumed = 1;
634 }
635 #endif
636 #if CONFIG_SWRESAMPLE
637
3/4
✓ Branch 0 taken 20273 times.
✓ Branch 1 taken 69797 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20273 times.
90070 if (!consumed && (o=opt_find(&swr_class, opt, NULL, 0,
638 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
639 av_dict_set(&swr_opts, opt, arg, FLAGS);
640 consumed = 1;
641 }
642 #endif
643
644
2/2
✓ Branch 0 taken 69797 times.
✓ Branch 1 taken 20273 times.
90070 if (consumed)
645 69797 return 0;
646 20273 return AVERROR_OPTION_NOT_FOUND;
647 }
648
649 /*
650 * Check whether given option is a group separator.
651 *
652 * @return index of the group definition that matched or -1 if none
653 */
654 148751 static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
655 const char *opt)
656 {
657 int i;
658
659
2/2
✓ Branch 0 taken 439448 times.
✓ Branch 1 taken 141945 times.
581393 for (i = 0; i < nb_groups; i++) {
660 439448 const OptionGroupDef *p = &groups[i];
661
4/4
✓ Branch 0 taken 290697 times.
✓ Branch 1 taken 148751 times.
✓ Branch 2 taken 6806 times.
✓ Branch 3 taken 283891 times.
439448 if (p->sep && !strcmp(p->sep, opt))
662 6806 return i;
663 }
664
665 141945 return -1;
666 }
667
668 /*
669 * Finish parsing an option group.
670 *
671 * @param group_idx which group definition should this group belong to
672 * @param arg argument of the group delimiting option
673 */
674 13582 static int finish_group(OptionParseContext *octx, int group_idx,
675 const char *arg)
676 {
677 13582 OptionGroupList *l = &octx->groups[group_idx];
678 OptionGroup *g;
679 int ret;
680
681 13582 ret = GROW_ARRAY(l->groups, l->nb_groups);
682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13582 times.
13582 if (ret < 0)
683 return ret;
684
685 13582 g = &l->groups[l->nb_groups - 1];
686
687 13582 *g = octx->cur_group;
688 13582 g->arg = arg;
689 13582 g->group_def = l->group_def;
690 13582 g->sws_dict = sws_dict;
691 13582 g->swr_opts = swr_opts;
692 13582 g->codec_opts = codec_opts;
693 13582 g->format_opts = format_opts;
694
695 13582 codec_opts = NULL;
696 13582 format_opts = NULL;
697 13582 sws_dict = NULL;
698 13582 swr_opts = NULL;
699
700 13582 memset(&octx->cur_group, 0, sizeof(octx->cur_group));
701
702 13582 return ret;
703 }
704
705 /*
706 * Add an option instance to currently parsed group.
707 */
708 72156 static int add_opt(OptionParseContext *octx, const OptionDef *opt,
709 const char *key, const char *val)
710 {
711 72156 int global = !(opt->flags & OPT_PERFILE);
712
2/2
✓ Branch 0 taken 31263 times.
✓ Branch 1 taken 40893 times.
72156 OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
713 int ret;
714
715 72156 ret = GROW_ARRAY(g->opts, g->nb_opts);
716
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72156 times.
72156 if (ret < 0)
717 return ret;
718
719 72156 g->opts[g->nb_opts - 1].opt = opt;
720 72156 g->opts[g->nb_opts - 1].key = key;
721 72156 g->opts[g->nb_opts - 1].val = val;
722
723 72156 return 0;
724 }
725
726 6775 static int init_parse_context(OptionParseContext *octx,
727 const OptionGroupDef *groups, int nb_groups)
728 {
729 static const OptionGroupDef global_group = { "global" };
730 int i;
731
732 6775 memset(octx, 0, sizeof(*octx));
733
734 6775 octx->groups = av_calloc(nb_groups, sizeof(*octx->groups));
735
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6775 times.
6775 if (!octx->groups)
736 return AVERROR(ENOMEM);
737 6775 octx->nb_groups = nb_groups;
738
739
2/2
✓ Branch 0 taken 20325 times.
✓ Branch 1 taken 6775 times.
27100 for (i = 0; i < octx->nb_groups; i++)
740 20325 octx->groups[i].group_def = &groups[i];
741
742 6775 octx->global_opts.group_def = &global_group;
743 6775 octx->global_opts.arg = "";
744
745 6775 return 0;
746 }
747
748 6775 void uninit_parse_context(OptionParseContext *octx)
749 {
750 int i, j;
751
752
2/2
✓ Branch 0 taken 20325 times.
✓ Branch 1 taken 6775 times.
27100 for (i = 0; i < octx->nb_groups; i++) {
753 20325 OptionGroupList *l = &octx->groups[i];
754
755
2/2
✓ Branch 0 taken 13582 times.
✓ Branch 1 taken 20325 times.
33907 for (j = 0; j < l->nb_groups; j++) {
756 13582 av_freep(&l->groups[j].opts);
757 13582 av_dict_free(&l->groups[j].codec_opts);
758 13582 av_dict_free(&l->groups[j].format_opts);
759
760 13582 av_dict_free(&l->groups[j].sws_dict);
761 13582 av_dict_free(&l->groups[j].swr_opts);
762 }
763 20325 av_freep(&l->groups);
764 }
765 6775 av_freep(&octx->groups);
766
767 6775 av_freep(&octx->cur_group.opts);
768 6775 av_freep(&octx->global_opts.opts);
769
770 6775 uninit_opts();
771 6775 }
772
773 6775 int split_commandline(OptionParseContext *octx, int argc, char *argv[],
774 const OptionDef *options,
775 const OptionGroupDef *groups, int nb_groups)
776 {
777 int ret;
778 6775 int optindex = 1;
779 6775 int dashdash = -2;
780
781 /* perform system-dependent conversions for arguments list */
782 6775 prepare_app_arguments(&argc, &argv);
783
784 6775 ret = init_parse_context(octx, groups, nb_groups);
785
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6775 times.
6775 if (ret < 0)
786 return ret;
787
788 6775 av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
789
790
2/2
✓ Branch 0 taken 155527 times.
✓ Branch 1 taken 6775 times.
162302 while (optindex < argc) {
791 155527 const char *opt = argv[optindex++], *arg;
792 const OptionDef *po;
793 int ret, group_idx;
794
795 155527 av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
796
797
3/6
✓ Branch 0 taken 151354 times.
✓ Branch 1 taken 4173 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 151354 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
155527 if (opt[0] == '-' && opt[1] == '-' && !opt[2]) {
798 dashdash = optindex;
799 continue;
800 }
801 /* unnamed group separators, e.g. output filename */
802
5/6
✓ Branch 0 taken 151354 times.
✓ Branch 1 taken 4173 times.
✓ Branch 2 taken 148751 times.
✓ Branch 3 taken 2603 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 148751 times.
155527 if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) {
803 6776 ret = finish_group(octx, 0, opt);
804
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6776 times.
6776 if (ret < 0)
805 return ret;
806
807 6776 av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
808 6776 continue;
809 }
810 148751 opt++;
811
812 #define GET_ARG(arg) \
813 do { \
814 arg = argv[optindex++]; \
815 if (!arg) { \
816 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
817 return AVERROR(EINVAL); \
818 } \
819 } while (0)
820
821 /* named group separators, e.g. -i */
822 148751 group_idx = match_group_separator(groups, nb_groups, opt);
823
2/2
✓ Branch 0 taken 6806 times.
✓ Branch 1 taken 141945 times.
148751 if (group_idx >= 0) {
824
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6806 times.
6806 GET_ARG(arg);
825 6806 ret = finish_group(octx, group_idx, arg);
826
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6806 times.
6806 if (ret < 0)
827 return ret;
828
829 6806 av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
830 6806 groups[group_idx].name, arg);
831 6806 continue;
832 }
833
834 /* normal options */
835 141945 po = find_option(options, opt);
836
2/2
✓ Branch 0 taken 51883 times.
✓ Branch 1 taken 90062 times.
141945 if (po->name) {
837
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 51882 times.
51883 if (po->flags & OPT_EXIT) {
838 /* optional argument, e.g. -h */
839 1 arg = argv[optindex++];
840
2/2
✓ Branch 1 taken 45885 times.
✓ Branch 2 taken 5997 times.
51882 } else if (opt_has_arg(po)) {
841
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45885 times.
45885 GET_ARG(arg);
842 } else {
843 5997 arg = "1";
844 }
845
846 51883 ret = add_opt(octx, po, opt, arg);
847
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51883 times.
51883 if (ret < 0)
848 return ret;
849
850 51883 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
851 51883 "argument '%s'.\n", po->name, po->help, arg);
852 51883 continue;
853 }
854
855 /* AVOptions */
856
1/2
✓ Branch 0 taken 90062 times.
✗ Branch 1 not taken.
90062 if (argv[optindex]) {
857 90062 ret = opt_default(NULL, opt, argv[optindex]);
858
2/2
✓ Branch 0 taken 69789 times.
✓ Branch 1 taken 20273 times.
90062 if (ret >= 0) {
859 69789 av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
860 69789 "argument '%s'.\n", opt, argv[optindex]);
861 69789 optindex++;
862 69789 continue;
863
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20273 times.
20273 } else if (ret != AVERROR_OPTION_NOT_FOUND) {
864 av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
865 "with argument '%s'.\n", opt, argv[optindex]);
866 return ret;
867 }
868 }
869
870 /* boolean -nofoo options */
871
3/6
✓ Branch 0 taken 20273 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20273 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20273 times.
✗ Branch 5 not taken.
40546 if (opt[0] == 'n' && opt[1] == 'o' &&
872 20273 (po = find_option(options, opt + 2)) &&
873
2/4
✓ Branch 0 taken 20273 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20273 times.
✗ Branch 3 not taken.
20273 po->name && po->type == OPT_TYPE_BOOL) {
874 20273 ret = add_opt(octx, po, opt, "0");
875
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20273 times.
20273 if (ret < 0)
876 return ret;
877
878 20273 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
879 20273 "argument 0.\n", po->name, po->help);
880 20273 continue;
881 }
882
883 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
884 return AVERROR_OPTION_NOT_FOUND;
885 }
886
887
3/6
✓ Branch 0 taken 6775 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6775 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6775 times.
6775 if (octx->cur_group.nb_opts || codec_opts || format_opts)
888 av_log(NULL, AV_LOG_WARNING, "Trailing option(s) found in the "
889 "command: may be ignored.\n");
890
891 6775 av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
892
893 6775 return 0;
894 }
895
896 int read_yesno(void)
897 {
898 int c = getchar();
899 int yesno = (av_toupper(c) == 'Y');
900
901 while (c != '\n' && c != EOF)
902 c = getchar();
903
904 return yesno;
905 }
906
907 FILE *get_preset_file(char *filename, size_t filename_size,
908 const char *preset_name, int is_path,
909 const char *codec_name)
910 {
911 FILE *f = NULL;
912 int i;
913 #if HAVE_GETMODULEHANDLE && defined(_WIN32)
914 char *datadir = NULL;
915 #endif
916 char *env_home = getenv_utf8("HOME");
917 char *env_ffmpeg_datadir = getenv_utf8("FFMPEG_DATADIR");
918 const char *base[3] = { env_ffmpeg_datadir,
919 env_home, /* index=1(HOME) is special: search in a .ffmpeg subfolder */
920 FFMPEG_DATADIR, };
921
922 if (is_path) {
923 av_strlcpy(filename, preset_name, filename_size);
924 f = fopen_utf8(filename, "r");
925 } else {
926 #if HAVE_GETMODULEHANDLE && defined(_WIN32)
927 wchar_t *datadir_w = get_module_filename(NULL);
928 base[2] = NULL;
929
930 if (wchartoutf8(datadir_w, &datadir))
931 datadir = NULL;
932 av_free(datadir_w);
933
934 if (datadir)
935 {
936 char *ls;
937 for (ls = datadir; *ls; ls++)
938 if (*ls == '\\') *ls = '/';
939
940 if (ls = strrchr(datadir, '/'))
941 {
942 ptrdiff_t datadir_len = ls - datadir;
943 size_t desired_size = datadir_len + strlen("/ffpresets") + 1;
944 char *new_datadir = av_realloc_array(
945 datadir, desired_size, sizeof *datadir);
946 if (new_datadir) {
947 datadir = new_datadir;
948 datadir[datadir_len] = 0;
949 strncat(datadir, "/ffpresets", desired_size - 1 - datadir_len);
950 base[2] = datadir;
951 }
952 }
953 }
954 #endif
955 for (i = 0; i < 3 && !f; i++) {
956 if (!base[i])
957 continue;
958 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
959 i != 1 ? "" : "/.ffmpeg", preset_name);
960 f = fopen_utf8(filename, "r");
961 if (!f && codec_name) {
962 snprintf(filename, filename_size,
963 "%s%s/%s-%s.ffpreset",
964 base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
965 preset_name);
966 f = fopen_utf8(filename, "r");
967 }
968 }
969 }
970
971 #if HAVE_GETMODULEHANDLE && defined(_WIN32)
972 av_free(datadir);
973 #endif
974 freeenv_utf8(env_ffmpeg_datadir);
975 freeenv_utf8(env_home);
976 return f;
977 }
978
979 29829 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
980 {
981 29829 int ret = avformat_match_stream_specifier(s, st, spec);
982
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29829 times.
29829 if (ret < 0)
983 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
984 29829 return ret;
985 }
986
987 22106 int filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_id,
988 AVFormatContext *s, AVStream *st, const AVCodec *codec,
989 AVDictionary **dst, AVDictionary **opts_used)
990 {
991 22106 AVDictionary *ret = NULL;
992 22106 const AVDictionaryEntry *t = NULL;
993 44212 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
994
2/2
✓ Branch 0 taken 7129 times.
✓ Branch 1 taken 14977 times.
22106 : AV_OPT_FLAG_DECODING_PARAM;
995 22106 char prefix = 0;
996 22106 const AVClass *cc = avcodec_get_class();
997
998
4/4
✓ Branch 0 taken 16799 times.
✓ Branch 1 taken 4985 times.
✓ Branch 2 taken 240 times.
✓ Branch 3 taken 82 times.
22106 switch (st->codecpar->codec_type) {
999 16799 case AVMEDIA_TYPE_VIDEO:
1000 16799 prefix = 'v';
1001 16799 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1002 16799 break;
1003 4985 case AVMEDIA_TYPE_AUDIO:
1004 4985 prefix = 'a';
1005 4985 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1006 4985 break;
1007 240 case AVMEDIA_TYPE_SUBTITLE:
1008 240 prefix = 's';
1009 240 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1010 240 break;
1011 }
1012
1013
2/2
✓ Branch 1 taken 63487 times.
✓ Branch 2 taken 22106 times.
85593 while (t = av_dict_iterate(opts, t)) {
1014 const AVClass *priv_class;
1015 63487 char *p = strchr(t->key, ':');
1016 63487 int used = 0;
1017
1018 /* check stream specification in opt name */
1019
2/2
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 63199 times.
63487 if (p) {
1020 288 int err = check_stream_specifier(s, st, p + 1);
1021
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 288 times.
288 if (err < 0) {
1022 av_dict_free(&ret);
1023 return err;
1024
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 210 times.
288 } else if (!err)
1025 78 continue;
1026
1027 210 *p = 0;
1028 }
1029
1030
4/4
✓ Branch 1 taken 2661 times.
✓ Branch 2 taken 60748 times.
✓ Branch 3 taken 1535 times.
✓ Branch 4 taken 1126 times.
63409 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1031 1535 !codec ||
1032
4/4
✓ Branch 0 taken 669 times.
✓ Branch 1 taken 866 times.
✓ Branch 2 taken 307 times.
✓ Branch 3 taken 362 times.
2204 ((priv_class = codec->priv_class) &&
1033 669 av_opt_find(&priv_class, t->key, NULL, flags,
1034 AV_OPT_SEARCH_FAKE_OBJ))) {
1035 62181 av_dict_set(&ret, t->key, t->value, 0);
1036 62181 used = 1;
1037
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1225 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
1231 } else if (t->key[0] == prefix &&
1038 3 av_opt_find(&cc, t->key + 1, NULL, flags,
1039 AV_OPT_SEARCH_FAKE_OBJ)) {
1040 1 av_dict_set(&ret, t->key + 1, t->value, 0);
1041 1 used = 1;
1042 }
1043
1044
2/2
✓ Branch 0 taken 210 times.
✓ Branch 1 taken 63199 times.
63409 if (p)
1045 210 *p = ':';
1046
1047
4/4
✓ Branch 0 taken 62182 times.
✓ Branch 1 taken 1227 times.
✓ Branch 2 taken 39376 times.
✓ Branch 3 taken 22806 times.
63409 if (used && opts_used)
1048 39376 av_dict_set(opts_used, t->key, "", 0);
1049 }
1050
1051 22106 *dst = ret;
1052 22106 return 0;
1053 }
1054
1055 6952 int setup_find_stream_info_opts(AVFormatContext *s,
1056 AVDictionary *codec_opts,
1057 AVDictionary ***dst)
1058 {
1059 int ret;
1060 AVDictionary **opts;
1061
1062 6952 *dst = NULL;
1063
1064
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 6867 times.
6952 if (!s->nb_streams)
1065 85 return 0;
1066
1067 6867 opts = av_calloc(s->nb_streams, sizeof(*opts));
1068
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6867 times.
6867 if (!opts)
1069 return AVERROR(ENOMEM);
1070
1071
2/2
✓ Branch 0 taken 7464 times.
✓ Branch 1 taken 6867 times.
14331 for (int i = 0; i < s->nb_streams; i++) {
1072 7464 ret = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id,
1073 7464 s, s->streams[i], NULL, &opts[i], NULL);
1074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7464 times.
7464 if (ret < 0)
1075 goto fail;
1076 }
1077 6867 *dst = opts;
1078 6867 return 0;
1079 fail:
1080 for (int i = 0; i < s->nb_streams; i++)
1081 av_dict_free(&opts[i]);
1082 av_freep(&opts);
1083 return ret;
1084 }
1085
1086 200935 int grow_array(void **array, int elem_size, int *size, int new_size)
1087 {
1088
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200935 times.
200935 if (new_size >= INT_MAX / elem_size) {
1089 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1090 return AVERROR(ERANGE);
1091 }
1092
1/2
✓ Branch 0 taken 200935 times.
✗ Branch 1 not taken.
200935 if (*size < new_size) {
1093 200935 uint8_t *tmp = av_realloc_array(*array, new_size, elem_size);
1094
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200935 times.
200935 if (!tmp)
1095 return AVERROR(ENOMEM);
1096 200935 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
1097 200935 *size = new_size;
1098 200935 *array = tmp;
1099 200935 return 0;
1100 }
1101 return 0;
1102 }
1103
1104 40885 void *allocate_array_elem(void *ptr, size_t elem_size, int *nb_elems)
1105 {
1106 void *new_elem;
1107
1108
2/4
✓ Branch 1 taken 40885 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 40885 times.
81770 if (!(new_elem = av_mallocz(elem_size)) ||
1109 40885 av_dynarray_add_nofree(ptr, nb_elems, new_elem) < 0)
1110 return NULL;
1111 40885 return new_elem;
1112 }
1113
1114 5206 double get_rotation(const int32_t *displaymatrix)
1115 {
1116 5206 double theta = 0;
1117
1/2
✓ Branch 0 taken 5206 times.
✗ Branch 1 not taken.
5206 if (displaymatrix)
1118 5206 theta = -round(av_display_rotation_get(displaymatrix));
1119
1120 5206 theta -= 360*floor(theta/360 + 0.9/360);
1121
1122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5206 times.
5206 if (fabs(theta - 90*round(theta/90)) > 2)
1123 av_log(NULL, AV_LOG_WARNING, "Odd rotation angle.\n"
1124 "If you want to help, upload a sample "
1125 "of this file to https://streams.videolan.org/upload/ "
1126 "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)");
1127
1128 5206 return theta;
1129 }
1130
1131 /* read file contents into a string */
1132 57 char *file_read(const char *filename)
1133 {
1134 57 AVIOContext *pb = NULL;
1135 57 int ret = avio_open(&pb, filename, AVIO_FLAG_READ);
1136 AVBPrint bprint;
1137 char *str;
1138
1139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
57 if (ret < 0) {
1140 av_log(NULL, AV_LOG_ERROR, "Error opening file %s.\n", filename);
1141 return NULL;
1142 }
1143
1144 57 av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
1145 57 ret = avio_read_to_bprint(pb, &bprint, SIZE_MAX);
1146 57 avio_closep(&pb);
1147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
57 if (ret < 0) {
1148 av_bprint_finalize(&bprint, NULL);
1149 return NULL;
1150 }
1151 57 ret = av_bprint_finalize(&bprint, &str);
1152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
57 if (ret < 0)
1153 return NULL;
1154 57 return str;
1155 }
1156
1157 6805 void remove_avoptions(AVDictionary **a, AVDictionary *b)
1158 {
1159 6805 const AVDictionaryEntry *t = NULL;
1160
1161
2/2
✓ Branch 1 taken 21679 times.
✓ Branch 2 taken 6805 times.
28484 while ((t = av_dict_iterate(b, t))) {
1162 21679 av_dict_set(a, t->key, NULL, AV_DICT_MATCH_CASE);
1163 }
1164 6805 }
1165
1166 19693 int check_avoptions(AVDictionary *m)
1167 {
1168 19693 const AVDictionaryEntry *t = av_dict_iterate(m, NULL);
1169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19693 times.
19693 if (t) {
1170 av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key);
1171 return AVERROR_OPTION_NOT_FOUND;
1172 }
1173
1174 19693 return 0;
1175 }
1176