FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/cmdutils.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 543 821 66.1%
Functions: 34 40 85.0%
Branches: 402 693 58.0%

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 16632 void uninit_opts(void)
63 {
64 16632 av_dict_free(&swr_opts);
65 16632 av_dict_free(&sws_dict);
66 16632 av_dict_free(&format_opts);
67 16632 av_dict_free(&codec_opts);
68 16632 }
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 8394 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 8394 }
83
84 37649 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 37649 double d = av_strtod(numstr, &tail);
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37649 times.
37649 if (*tail)
91 error = "Expected number for %s but found: %s\n";
92
2/4
✓ Branch 0 taken 37649 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37649 times.
37649 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 37356 times.
✓ Branch 1 taken 293 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 37356 times.
37649 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 37649 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
37649 else if (type == OPT_TYPE_INT && (int)d != d)
97 error = "Expected int for %s but found %s\n";
98 else {
99 37649 *dst = d;
100 37649 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 1268915 static const OptionDef *find_option(const OptionDef *po, const char *name)
154 {
155
2/2
✓ Branch 0 taken 372 times.
✓ Branch 1 taken 1268543 times.
1268915 if (*name == '/')
156 372 name++;
157
158
2/2
✓ Branch 0 taken 183602086 times.
✓ Branch 1 taken 721932 times.
184324018 while (po->name) {
159 const char *end;
160
6/6
✓ Branch 1 taken 1293115 times.
✓ Branch 2 taken 182308971 times.
✓ Branch 3 taken 796427 times.
✓ Branch 4 taken 496688 times.
✓ Branch 5 taken 746132 times.
✓ Branch 6 taken 50295 times.
183602086 if (av_strstart(name, po->name, &end) && (!*end || *end == ':'))
161 break;
162 183055103 po++;
163 }
164 1268915 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 8394 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
226 {
227 /* nothing to do */
228 8394 }
229 #endif /* HAVE_COMMANDLINETOARGVW */
230
231 529856 static int opt_has_arg(const OptionDef *o)
232 {
233
2/2
✓ Branch 0 taken 167439 times.
✓ Branch 1 taken 362417 times.
529856 if (o->type == OPT_TYPE_BOOL)
234 167439 return 0;
235
2/2
✓ Branch 0 taken 126677 times.
✓ Branch 1 taken 235740 times.
362417 if (o->type == OPT_TYPE_FUNC)
236 126677 return !!(o->flags & OPT_FUNC_ARG);
237 235740 return 1;
238 }
239
240 98650 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 197300 void *dst = po->flags & OPT_FLAG_OFFSET ?
246
2/2
✓ Branch 0 taken 47718 times.
✓ Branch 1 taken 50932 times.
98650 (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
247 98650 char *arg_allocated = NULL;
248
249 98650 enum OptionType so_type = po->type;
250
251 98650 SpecifierOptList *sol = NULL;
252 double num;
253 98650 int ret = 0;
254
255
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 98588 times.
98650 if (*opt == '/') {
256 62 opt++;
257
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (po->type == OPT_TYPE_BOOL) {
259 av_log(NULL, AV_LOG_FATAL,
260 "Requested to load an argument from file for a bool option '%s'\n",
261 po->name);
262 return AVERROR(EINVAL);
263 }
264
265 62 arg_allocated = file_read(arg);
266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (!arg_allocated) {
267 av_log(NULL, AV_LOG_FATAL,
268 "Error reading the value for option '%s' from file: %s\n",
269 opt, arg);
270 return AVERROR(EINVAL);
271 }
272
273 62 arg = arg_allocated;
274 }
275
276
2/2
✓ Branch 0 taken 32438 times.
✓ Branch 1 taken 66212 times.
98650 if (po->flags & OPT_FLAG_SPEC) {
277 32438 char *p = strchr(opt, ':');
278 char *str;
279
280 32438 sol = dst;
281 32438 ret = GROW_ARRAY(sol->opt, sol->nb_opt);
282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32438 times.
32438 if (ret < 0)
283 goto finish;
284
285
2/2
✓ Branch 0 taken 16063 times.
✓ Branch 1 taken 16375 times.
32438 str = av_strdup(p ? p + 1 : "");
286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32438 times.
32438 if (!str) {
287 ret = AVERROR(ENOMEM);
288 goto finish;
289 }
290 32438 sol->opt[sol->nb_opt - 1].specifier = str;
291
292
2/2
✓ Branch 0 taken 32187 times.
✓ Branch 1 taken 251 times.
32438 if (po->flags & OPT_FLAG_PERSTREAM) {
293 32187 ret = stream_specifier_parse(&sol->opt[sol->nb_opt - 1].stream_spec,
294 str, 0, NULL);
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32187 times.
32187 if (ret < 0)
296 goto finish;
297 }
298
299 32438 dst = &sol->opt[sol->nb_opt - 1].u;
300 }
301
302
2/2
✓ Branch 0 taken 38658 times.
✓ Branch 1 taken 59992 times.
98650 if (po->type == OPT_TYPE_STRING) {
303 char *str;
304
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 38633 times.
38658 if (arg_allocated) {
305 25 str = arg_allocated;
306 25 arg_allocated = NULL;
307 } else
308 38633 str = av_strdup(arg);
309 38658 av_freep(dst);
310
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38658 times.
38658 if (!str) {
312 ret = AVERROR(ENOMEM);
313 goto finish;
314 }
315
316 38658 *(char **)dst = str;
317
4/4
✓ Branch 0 taken 28000 times.
✓ Branch 1 taken 31992 times.
✓ Branch 2 taken 137 times.
✓ Branch 3 taken 27863 times.
59992 } else if (po->type == OPT_TYPE_BOOL || po->type == OPT_TYPE_INT) {
318 32129 ret = parse_number(opt, arg, OPT_TYPE_INT64, INT_MIN, INT_MAX, &num);
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32129 times.
32129 if (ret < 0)
320 goto finish;
321
322 32129 *(int *)dst = num;
323 32129 so_type = OPT_TYPE_INT;
324
2/2
✓ Branch 0 taken 5227 times.
✓ Branch 1 taken 22636 times.
27863 } else if (po->type == OPT_TYPE_INT64) {
325 5227 ret = parse_number(opt, arg, OPT_TYPE_INT64, INT64_MIN, (double)INT64_MAX, &num);
326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5227 times.
5227 if (ret < 0)
327 goto finish;
328
329 5227 *(int64_t *)dst = num;
330
2/2
✓ Branch 0 taken 1266 times.
✓ Branch 1 taken 21370 times.
22636 } else if (po->type == OPT_TYPE_TIME) {
331 1266 ret = av_parse_time(dst, arg, 1);
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1266 times.
1266 if (ret < 0) {
333 av_log(NULL, AV_LOG_ERROR, "Invalid duration for option %s: %s\n",
334 opt, arg);
335 goto finish;
336 }
337 1266 so_type = OPT_TYPE_INT64;
338
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 21367 times.
21370 } else if (po->type == OPT_TYPE_FLOAT) {
339 3 ret = parse_number(opt, arg, OPT_TYPE_FLOAT, -INFINITY, INFINITY, &num);
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
341 goto finish;
342
343 3 *(float *)dst = num;
344
2/2
✓ Branch 0 taken 290 times.
✓ Branch 1 taken 21077 times.
21367 } else if (po->type == OPT_TYPE_DOUBLE) {
345 290 ret = parse_number(opt, arg, OPT_TYPE_DOUBLE, -INFINITY, INFINITY, &num);
346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 290 times.
290 if (ret < 0)
347 goto finish;
348
349 290 *(double *)dst = num;
350 } else {
351
2/4
✓ Branch 0 taken 21077 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 21077 times.
21077 av_assert0(po->type == OPT_TYPE_FUNC && po->u.func_arg);
352
353 21077 ret = po->u.func_arg(optctx, opt, arg);
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21077 times.
21077 if (ret < 0) {
355 if ((strcmp(opt, "init_hw_device") != 0) || (strcmp(arg, "list") != 0)) {
356 av_log(NULL, AV_LOG_ERROR,
357 "Failed to set value '%s' for option '%s': %s\n",
358 arg, opt, av_err2str(ret));
359 }
360 goto finish;
361 }
362 }
363
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 98649 times.
98650 if (po->flags & OPT_EXIT) {
364 1 ret = AVERROR_EXIT;
365 1 goto finish;
366 }
367
368
2/2
✓ Branch 0 taken 66211 times.
✓ Branch 1 taken 32438 times.
98649 if (sol) {
369 32438 sol->type = so_type;
370
2/2
✓ Branch 0 taken 2457 times.
✓ Branch 1 taken 29981 times.
34895 sol->opt_canon = (po->flags & OPT_HAS_CANON) ?
371 2457 find_option(defs, po->u1.name_canon) : po;
372 }
373
374 66211 finish:
375 98650 av_freep(&arg_allocated);
376 98650 return ret;
377 }
378
379 10000 int parse_option(void *optctx, const char *opt, const char *arg,
380 const OptionDef *options)
381 {
382 static const OptionDef opt_avoptions = {
383 .name = "AVOption passthrough",
384 .type = OPT_TYPE_FUNC,
385 .flags = OPT_FUNC_ARG,
386 .u.func_arg = opt_default,
387 };
388
389 const OptionDef *po;
390 int ret;
391
392 10000 po = find_option(options, opt);
393
5/6
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 9990 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
10000 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
394 /* handle 'no' bool option */
395 1 po = find_option(options, opt + 2);
396
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))
397 1 arg = "0";
398
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 9851 times.
9999 } else if (po->type == OPT_TYPE_BOOL)
399 148 arg = "1";
400
401
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9991 times.
10000 if (!po->name)
402 9 po = &opt_avoptions;
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10000 times.
10000 if (!po->name) {
404 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
405 return AVERROR(EINVAL);
406 }
407
3/4
✓ Branch 1 taken 9744 times.
✓ Branch 2 taken 256 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9744 times.
10000 if (opt_has_arg(po) && !arg) {
408 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
409 return AVERROR(EINVAL);
410 }
411
412 10000 ret = write_option(optctx, po, opt, arg, options);
413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10000 times.
10000 if (ret < 0)
414 return ret;
415
416 10000 return opt_has_arg(po);
417 }
418
419 156 int parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
420 int (*parse_arg_function)(void *, const char*))
421 {
422 const char *opt;
423 156 int optindex, handleoptions = 1, ret;
424
425 /* perform system-dependent conversions for arguments list */
426 156 prepare_app_arguments(&argc, &argv);
427
428 /* parse options */
429 156 optindex = 1;
430
2/2
✓ Branch 0 taken 685 times.
✓ Branch 1 taken 156 times.
841 while (optindex < argc) {
431 685 opt = argv[optindex++];
432
433
4/6
✓ Branch 0 taken 685 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 539 times.
✓ Branch 3 taken 146 times.
✓ Branch 4 taken 539 times.
✗ Branch 5 not taken.
685 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
434
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 539 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
539 if (opt[1] == '-' && opt[2] == '\0') {
435 handleoptions = 0;
436 continue;
437 }
438 539 opt++;
439
440
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 539 times.
539 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
441 return ret;
442 539 optindex += ret;
443 } else {
444
1/2
✓ Branch 0 taken 146 times.
✗ Branch 1 not taken.
146 if (parse_arg_function) {
445 146 ret = parse_arg_function(optctx, opt);
446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 146 times.
146 if (ret < 0)
447 return ret;
448 }
449 }
450 }
451
452 156 return 0;
453 }
454
455 23685 int parse_optgroup(void *optctx, OptionGroup *g, const OptionDef *defs)
456 {
457 int i, ret;
458
459 23685 av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
460 23685 g->group_def->name, g->arg);
461
462
2/2
✓ Branch 0 taken 88650 times.
✓ Branch 1 taken 23684 times.
112334 for (i = 0; i < g->nb_opts; i++) {
463 88650 Option *o = &g->opts[i];
464
465
2/2
✓ Branch 0 taken 48266 times.
✓ Branch 1 taken 40384 times.
88650 if (g->group_def->flags &&
466
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48266 times.
48266 !(g->group_def->flags & o->opt->flags)) {
467 av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
468 "%s %s -- you are trying to apply an input option to an "
469 "output file or vice versa. Move this option before the "
470 "file it belongs to.\n", o->key, o->opt->help,
471 g->group_def->name, g->arg);
472 return AVERROR(EINVAL);
473 }
474
475 88650 av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
476 88650 o->key, o->opt->help, o->val);
477
478 88650 ret = write_option(optctx, o->opt, o->key, o->val, defs);
479
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 88649 times.
88650 if (ret < 0)
480 1 return ret;
481 }
482
483 23684 av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
484
485 23684 return 0;
486 }
487
488 41970 int locate_option(int argc, char **argv, const OptionDef *options,
489 const char *optname)
490 {
491 const OptionDef *po;
492 int i;
493
494
2/2
✓ Branch 0 taken 972074 times.
✓ Branch 1 taken 41954 times.
1014028 for (i = 1; i < argc; i++) {
495 972074 const char *cur_opt = argv[i];
496
497
4/4
✓ Branch 0 taken 943407 times.
✓ Branch 1 taken 28667 times.
✓ Branch 2 taken 13255 times.
✓ Branch 3 taken 930152 times.
972074 if (!(cur_opt[0] == '-' && cur_opt[1]))
498 41922 continue;
499 930152 cur_opt++;
500
501 930152 po = find_option(options, cur_opt);
502
5/6
✓ Branch 0 taken 607582 times.
✓ Branch 1 taken 322570 times.
✓ Branch 2 taken 123355 times.
✓ Branch 3 taken 484227 times.
✓ Branch 4 taken 123355 times.
✗ Branch 5 not taken.
930152 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
503 123355 po = find_option(options, cur_opt + 2);
504
505
3/4
✓ Branch 0 taken 484267 times.
✓ Branch 1 taken 445885 times.
✓ Branch 2 taken 484267 times.
✗ Branch 3 not taken.
930152 if ((!po->name && !strcmp(cur_opt, optname)) ||
506
4/4
✓ Branch 0 taken 445885 times.
✓ Branch 1 taken 484267 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 445869 times.
930152 (po->name && !strcmp(optname, po->name)))
507 16 return i;
508
509
4/4
✓ Branch 0 taken 445869 times.
✓ Branch 1 taken 484267 times.
✓ Branch 3 taken 285369 times.
✓ Branch 4 taken 160500 times.
930136 if (!po->name || opt_has_arg(po))
510 769636 i++;
511 }
512 41954 return 0;
513 }
514
515 static void dump_argument(FILE *report_file, const char *a)
516 {
517 const unsigned char *p;
518
519 for (p = a; *p; p++)
520 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
521 *p == '_' || (*p >= 'a' && *p <= 'z')))
522 break;
523 if (!*p) {
524 fputs(a, report_file);
525 return;
526 }
527 fputc('"', report_file);
528 for (p = a; *p; p++) {
529 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
530 fprintf(report_file, "\\%c", *p);
531 else if (*p < ' ' || *p > '~')
532 fprintf(report_file, "\\x%02x", *p);
533 else
534 fputc(*p, report_file);
535 }
536 fputc('"', report_file);
537 }
538
539 8394 static void check_options(const OptionDef *po)
540 {
541
2/2
✓ Branch 0 taken 1592616 times.
✓ Branch 1 taken 8394 times.
1601010 while (po->name) {
542
2/2
✓ Branch 0 taken 988560 times.
✓ Branch 1 taken 604056 times.
1592616 if (po->flags & OPT_PERFILE)
543
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 988560 times.
988560 av_assert0(po->flags & (OPT_INPUT | OPT_OUTPUT | OPT_DECODER));
544
545
2/2
✓ Branch 0 taken 625806 times.
✓ Branch 1 taken 966810 times.
1592616 if (po->type == OPT_TYPE_FUNC)
546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 625806 times.
625806 av_assert0(!(po->flags & (OPT_FLAG_OFFSET | OPT_FLAG_SPEC)));
547
548 // OPT_FUNC_ARG can only be ser for OPT_TYPE_FUNC
549
3/4
✓ Branch 0 taken 966810 times.
✓ Branch 1 taken 625806 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 966810 times.
1592616 av_assert0((po->type == OPT_TYPE_FUNC) || !(po->flags & OPT_FUNC_ARG));
550
551 1592616 po++;
552 }
553 8394 }
554
555 8394 void parse_loglevel(int argc, char **argv, const OptionDef *options)
556 {
557 int idx;
558 char *env;
559
560 8394 check_options(options);
561
562 8394 idx = locate_option(argc, argv, options, "loglevel");
563
1/2
✓ Branch 0 taken 8394 times.
✗ Branch 1 not taken.
8394 if (!idx)
564 8394 idx = locate_option(argc, argv, options, "v");
565
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8378 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
8394 if (idx && argv[idx + 1])
566 16 opt_loglevel(NULL, "loglevel", argv[idx + 1]);
567 8394 idx = locate_option(argc, argv, options, "report");
568 8394 env = getenv_utf8("FFREPORT");
569
2/4
✓ Branch 0 taken 8394 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8394 times.
8394 if (env || idx) {
570 FILE *report_file = NULL;
571 init_report(env, &report_file);
572 if (report_file) {
573 int i;
574 fprintf(report_file, "Command line:\n");
575 for (i = 0; i < argc; i++) {
576 dump_argument(report_file, argv[i]);
577 fputc(i < argc - 1 ? ' ' : '\n', report_file);
578 }
579 fflush(report_file);
580 }
581 }
582 8394 freeenv_utf8(env);
583 8394 idx = locate_option(argc, argv, options, "hide_banner");
584
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8394 times.
8394 if (idx)
585 hide_banner = 1;
586 8394 }
587
588 308585 static const AVOption *opt_find(void *obj, const char *name, const char *unit,
589 int opt_flags, int search_flags)
590 {
591 308585 const AVOption *o = av_opt_find(obj, name, unit, opt_flags, search_flags);
592
4/4
✓ Branch 0 taken 89725 times.
✓ Branch 1 taken 218860 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 89723 times.
308585 if(o && !o->flags)
593 2 return NULL;
594 308583 return o;
595 }
596
597 #define FLAGS ((o->type == AV_OPT_TYPE_FLAGS && (arg[0]=='-' || arg[0]=='+')) ? AV_DICT_APPEND : 0)
598 114309 int opt_default(void *optctx, const char *opt, const char *arg)
599 {
600 const AVOption *o;
601 114309 int consumed = 0;
602 char opt_stripped[128];
603 const char *p;
604 114309 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
605 #if CONFIG_SWSCALE
606 114309 const AVClass *sc = sws_get_class();
607 #endif
608 #if CONFIG_SWRESAMPLE
609 114309 const AVClass *swr_class = swr_get_class();
610 #endif
611
612
2/4
✓ Branch 0 taken 114309 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 114309 times.
114309 if (!strcmp(opt, "debug") || !strcmp(opt, "fdebug"))
613 av_log_set_level(AV_LOG_DEBUG);
614
615
2/2
✓ Branch 0 taken 114297 times.
✓ Branch 1 taken 12 times.
114309 if (!(p = strchr(opt, ':')))
616 114297 p = opt + strlen(opt);
617 114309 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
618
619
2/2
✓ Branch 1 taken 55198 times.
✓ Branch 2 taken 59111 times.
114309 if ((o = opt_find(&cc, opt_stripped, NULL, 0,
620 55198 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
621
8/8
✓ Branch 0 taken 55195 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 55183 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 15326 times.
✓ Branch 5 taken 39857 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 15340 times.
70539 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
622 15341 (o = opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) {
623
5/6
✓ Branch 0 taken 22236 times.
✓ Branch 1 taken 36876 times.
✓ Branch 2 taken 22236 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15250 times.
✓ Branch 5 taken 6986 times.
59112 av_dict_set(&codec_opts, opt, arg, FLAGS);
624 59112 consumed = 1;
625 }
626
2/2
✓ Branch 1 taken 15309 times.
✓ Branch 2 taken 99000 times.
114309 if ((o = opt_find(&fc, opt, NULL, 0,
627 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
628
5/6
✓ Branch 0 taken 15090 times.
✓ Branch 1 taken 219 times.
✓ Branch 2 taken 15090 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15082 times.
✓ Branch 5 taken 8 times.
15309 av_dict_set(&format_opts, opt, arg, FLAGS);
629
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 15233 times.
15309 if (consumed)
630 76 av_log(NULL, AV_LOG_VERBOSE, "Routing option %s to both codec and muxer layer\n", opt);
631 15309 consumed = 1;
632 }
633 #if CONFIG_SWSCALE
634
4/4
✓ Branch 0 taken 39964 times.
✓ Branch 1 taken 74345 times.
✓ Branch 3 taken 15302 times.
✓ Branch 4 taken 24662 times.
114309 if (!consumed && (o = opt_find(&sc, opt, NULL, 0,
635 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
636
2/4
✓ Branch 0 taken 15302 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15302 times.
✗ Branch 3 not taken.
15302 if (!strcmp(opt, "srcw") || !strcmp(opt, "srch") ||
637
2/4
✓ Branch 0 taken 15302 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15302 times.
✗ Branch 3 not taken.
15302 !strcmp(opt, "dstw") || !strcmp(opt, "dsth") ||
638
2/4
✓ Branch 0 taken 15302 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15302 times.
15302 !strcmp(opt, "src_format") || !strcmp(opt, "dst_format")) {
639 av_log(NULL, AV_LOG_ERROR, "Directly using swscale dimensions/format options is not supported, please use the -s or -pix_fmt options\n");
640 return AVERROR(EINVAL);
641 }
642
4/6
✓ Branch 0 taken 15302 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15302 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15087 times.
✓ Branch 5 taken 215 times.
15302 av_dict_set(&sws_dict, opt, arg, FLAGS);
643
644 15302 consumed = 1;
645 }
646 #else
647 if (!consumed && !strcmp(opt, "sws_flags")) {
648 av_log(NULL, AV_LOG_WARNING, "Ignoring %s %s, due to disabled swscale\n", opt, arg);
649 consumed = 1;
650 }
651 #endif
652 #if CONFIG_SWRESAMPLE
653
3/4
✓ Branch 0 taken 24662 times.
✓ Branch 1 taken 89647 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24662 times.
114309 if (!consumed && (o=opt_find(&swr_class, opt, NULL, 0,
654 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
655 av_dict_set(&swr_opts, opt, arg, FLAGS);
656 consumed = 1;
657 }
658 #endif
659
660
2/2
✓ Branch 0 taken 89647 times.
✓ Branch 1 taken 24662 times.
114309 if (consumed)
661 89647 return 0;
662 24662 return AVERROR_OPTION_NOT_FOUND;
663 }
664
665 /*
666 * Check whether given option is a group separator.
667 *
668 * @return index of the group definition that matched or -1 if none
669 */
670 185496 static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
671 const char *opt)
672 {
673 int i;
674
675
2/2
✓ Branch 0 taken 549281 times.
✓ Branch 1 taken 178288 times.
727569 for (i = 0; i < nb_groups; i++) {
676 549281 const OptionGroupDef *p = &groups[i];
677
4/4
✓ Branch 0 taken 363785 times.
✓ Branch 1 taken 185496 times.
✓ Branch 2 taken 7208 times.
✓ Branch 3 taken 356577 times.
549281 if (p->sep && !strcmp(p->sep, opt))
678 7208 return i;
679 }
680
681 178288 return -1;
682 }
683
684 /*
685 * Finish parsing an option group.
686 *
687 * @param group_idx which group definition should this group belong to
688 * @param arg argument of the group delimiting option
689 */
690 15447 static int finish_group(OptionParseContext *octx, int group_idx,
691 const char *arg)
692 {
693 15447 OptionGroupList *l = &octx->groups[group_idx];
694 OptionGroup *g;
695 int ret;
696
697 15447 ret = GROW_ARRAY(l->groups, l->nb_groups);
698
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15447 times.
15447 if (ret < 0)
699 return ret;
700
701 15447 g = &l->groups[l->nb_groups - 1];
702
703 15447 *g = octx->cur_group;
704 15447 g->arg = arg;
705 15447 g->group_def = l->group_def;
706 15447 g->sws_dict = sws_dict;
707 15447 g->swr_opts = swr_opts;
708 15447 g->codec_opts = codec_opts;
709 15447 g->format_opts = format_opts;
710
711 15447 codec_opts = NULL;
712 15447 format_opts = NULL;
713 15447 sws_dict = NULL;
714 15447 swr_opts = NULL;
715
716 15447 memset(&octx->cur_group, 0, sizeof(octx->cur_group));
717
718 15447 return ret;
719 }
720
721 /*
722 * Add an option instance to currently parsed group.
723 */
724 88650 static int add_opt(OptionParseContext *octx, const OptionDef *opt,
725 const char *key, const char *val)
726 {
727 88650 int global = !(opt->flags & OPT_PERFILE);
728
2/2
✓ Branch 0 taken 40384 times.
✓ Branch 1 taken 48266 times.
88650 OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
729 int ret;
730
731 88650 ret = GROW_ARRAY(g->opts, g->nb_opts);
732
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88650 times.
88650 if (ret < 0)
733 return ret;
734
735 88650 g->opts[g->nb_opts - 1].opt = opt;
736 88650 g->opts[g->nb_opts - 1].key = key;
737 88650 g->opts[g->nb_opts - 1].val = val;
738
739 88650 return 0;
740 }
741
742 8238 static int init_parse_context(OptionParseContext *octx,
743 const OptionGroupDef *groups, int nb_groups)
744 {
745 static const OptionGroupDef global_group = { "global" };
746 int i;
747
748 8238 memset(octx, 0, sizeof(*octx));
749
750 8238 octx->groups = av_calloc(nb_groups, sizeof(*octx->groups));
751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8238 times.
8238 if (!octx->groups)
752 return AVERROR(ENOMEM);
753 8238 octx->nb_groups = nb_groups;
754
755
2/2
✓ Branch 0 taken 24714 times.
✓ Branch 1 taken 8238 times.
32952 for (i = 0; i < octx->nb_groups; i++)
756 24714 octx->groups[i].group_def = &groups[i];
757
758 8238 octx->global_opts.group_def = &global_group;
759 8238 octx->global_opts.arg = "";
760
761 8238 return 0;
762 }
763
764 8238 void uninit_parse_context(OptionParseContext *octx)
765 {
766 int i, j;
767
768
2/2
✓ Branch 0 taken 24714 times.
✓ Branch 1 taken 8238 times.
32952 for (i = 0; i < octx->nb_groups; i++) {
769 24714 OptionGroupList *l = &octx->groups[i];
770
771
2/2
✓ Branch 0 taken 15447 times.
✓ Branch 1 taken 24714 times.
40161 for (j = 0; j < l->nb_groups; j++) {
772 15447 av_freep(&l->groups[j].opts);
773 15447 av_dict_free(&l->groups[j].codec_opts);
774 15447 av_dict_free(&l->groups[j].format_opts);
775
776 15447 av_dict_free(&l->groups[j].sws_dict);
777 15447 av_dict_free(&l->groups[j].swr_opts);
778 }
779 24714 av_freep(&l->groups);
780 }
781 8238 av_freep(&octx->groups);
782
783 8238 av_freep(&octx->cur_group.opts);
784 8238 av_freep(&octx->global_opts.opts);
785
786 8238 uninit_opts();
787 8238 }
788
789 8238 int split_commandline(OptionParseContext *octx, int argc, char *argv[],
790 const OptionDef *options,
791 const OptionGroupDef *groups, int nb_groups)
792 {
793 int ret;
794 8238 int optindex = 1;
795 8238 int dashdash = -2;
796
797 /* perform system-dependent conversions for arguments list */
798 8238 prepare_app_arguments(&argc, &argv);
799
800 8238 ret = init_parse_context(octx, groups, nb_groups);
801
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8238 times.
8238 if (ret < 0)
802 return ret;
803
804 8238 av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
805
806
2/2
✓ Branch 0 taken 193735 times.
✓ Branch 1 taken 8238 times.
201973 while (optindex < argc) {
807 193735 const char *opt = argv[optindex++], *arg;
808 const OptionDef *po;
809 int group_idx;
810
811 193735 av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
812
813
3/6
✓ Branch 0 taken 188147 times.
✓ Branch 1 taken 5588 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 188147 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
193735 if (opt[0] == '-' && opt[1] == '-' && !opt[2]) {
814 dashdash = optindex;
815 continue;
816 }
817 /* unnamed group separators, e.g. output filename */
818
5/6
✓ Branch 0 taken 188147 times.
✓ Branch 1 taken 5588 times.
✓ Branch 2 taken 185496 times.
✓ Branch 3 taken 2651 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 185496 times.
193735 if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) {
819 8239 ret = finish_group(octx, 0, opt);
820
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8239 times.
8239 if (ret < 0)
821 return ret;
822
823 8239 av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
824 8239 continue;
825 }
826 185496 opt++;
827
828 #define GET_ARG(arg) \
829 do { \
830 arg = argv[optindex++]; \
831 if (!arg) { \
832 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
833 return AVERROR(EINVAL); \
834 } \
835 } while (0)
836
837 /* named group separators, e.g. -i */
838 185496 group_idx = match_group_separator(groups, nb_groups, opt);
839
2/2
✓ Branch 0 taken 7208 times.
✓ Branch 1 taken 178288 times.
185496 if (group_idx >= 0) {
840
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7208 times.
7208 GET_ARG(arg);
841 7208 ret = finish_group(octx, group_idx, arg);
842
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7208 times.
7208 if (ret < 0)
843 return ret;
844
845 7208 av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
846 7208 groups[group_idx].name, arg);
847 7208 continue;
848 }
849
850 /* normal options */
851 178288 po = find_option(options, opt);
852
2/2
✓ Branch 0 taken 63988 times.
✓ Branch 1 taken 114300 times.
178288 if (po->name) {
853
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 63987 times.
63988 if (po->flags & OPT_EXIT) {
854 /* optional argument, e.g. -h */
855 1 arg = argv[optindex++];
856
2/2
✓ Branch 1 taken 56806 times.
✓ Branch 2 taken 7181 times.
63987 } else if (opt_has_arg(po)) {
857
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56806 times.
56806 GET_ARG(arg);
858 } else {
859 7181 arg = "1";
860 }
861
862 63988 ret = add_opt(octx, po, opt, arg);
863
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63988 times.
63988 if (ret < 0)
864 return ret;
865
866 63988 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
867 63988 "argument '%s'.\n", po->name, po->help, arg);
868 63988 continue;
869 }
870
871 /* AVOptions */
872
1/2
✓ Branch 0 taken 114300 times.
✗ Branch 1 not taken.
114300 if (argv[optindex]) {
873 114300 ret = opt_default(NULL, opt, argv[optindex]);
874
2/2
✓ Branch 0 taken 89638 times.
✓ Branch 1 taken 24662 times.
114300 if (ret >= 0) {
875 89638 av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
876 89638 "argument '%s'.\n", opt, argv[optindex]);
877 89638 optindex++;
878 89638 continue;
879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24662 times.
24662 } else if (ret != AVERROR_OPTION_NOT_FOUND) {
880 av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
881 "with argument '%s'.\n", opt, argv[optindex]);
882 return ret;
883 }
884 }
885
886 /* boolean -nofoo options */
887
3/6
✓ Branch 0 taken 24662 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24662 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24662 times.
✗ Branch 5 not taken.
49324 if (opt[0] == 'n' && opt[1] == 'o' &&
888 24662 (po = find_option(options, opt + 2)) &&
889
2/4
✓ Branch 0 taken 24662 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24662 times.
✗ Branch 3 not taken.
24662 po->name && po->type == OPT_TYPE_BOOL) {
890 24662 ret = add_opt(octx, po, opt, "0");
891
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24662 times.
24662 if (ret < 0)
892 return ret;
893
894 24662 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
895 24662 "argument 0.\n", po->name, po->help);
896 24662 continue;
897 }
898
899 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
900 return AVERROR_OPTION_NOT_FOUND;
901 }
902
903
3/6
✓ Branch 0 taken 8238 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8238 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 8238 times.
8238 if (octx->cur_group.nb_opts || codec_opts || format_opts)
904 av_log(NULL, AV_LOG_WARNING, "Trailing option(s) found in the "
905 "command: may be ignored.\n");
906
907 8238 av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
908
909 8238 return 0;
910 }
911
912 int read_yesno(void)
913 {
914 int c = getchar();
915 int yesno = (av_toupper(c) == 'Y');
916
917 while (c != '\n' && c != EOF)
918 c = getchar();
919
920 return yesno;
921 }
922
923 FILE *get_preset_file(char *filename, size_t filename_size,
924 const char *preset_name, int is_path,
925 const char *codec_name)
926 {
927 FILE *f = NULL;
928 int i;
929 #if HAVE_GETMODULEHANDLE && defined(_WIN32)
930 char *datadir = NULL;
931 #endif
932 char *env_home = getenv_utf8("HOME");
933 char *env_ffmpeg_datadir = getenv_utf8("FFMPEG_DATADIR");
934 const char *base[3] = { env_ffmpeg_datadir,
935 env_home, /* index=1(HOME) is special: search in a .ffmpeg subfolder */
936 FFMPEG_DATADIR, };
937
938 if (is_path) {
939 av_strlcpy(filename, preset_name, filename_size);
940 f = fopen_utf8(filename, "r");
941 } else {
942 #if HAVE_GETMODULEHANDLE && defined(_WIN32)
943 wchar_t *datadir_w = get_module_filename(NULL);
944 base[2] = NULL;
945
946 if (wchartoutf8(datadir_w, &datadir))
947 datadir = NULL;
948 av_free(datadir_w);
949
950 if (datadir)
951 {
952 char *ls;
953 for (ls = datadir; *ls; ls++)
954 if (*ls == '\\') *ls = '/';
955
956 if (ls = strrchr(datadir, '/'))
957 {
958 ptrdiff_t datadir_len = ls - datadir;
959 size_t desired_size = datadir_len + strlen("/ffpresets") + 1;
960 char *new_datadir = av_realloc_array(
961 datadir, desired_size, sizeof *datadir);
962 if (new_datadir) {
963 datadir = new_datadir;
964 datadir[datadir_len] = 0;
965 strncat(datadir, "/ffpresets", desired_size - 1 - datadir_len);
966 base[2] = datadir;
967 }
968 }
969 }
970 #endif
971 for (i = 0; i < 3 && !f; i++) {
972 if (!base[i])
973 continue;
974 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
975 i != 1 ? "" : "/.ffmpeg", preset_name);
976 f = fopen_utf8(filename, "r");
977 if (!f && codec_name) {
978 snprintf(filename, filename_size,
979 "%s%s/%s-%s.ffpreset",
980 base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
981 preset_name);
982 f = fopen_utf8(filename, "r");
983 }
984 }
985 }
986
987 #if HAVE_GETMODULEHANDLE && defined(_WIN32)
988 av_free(datadir);
989 #endif
990 freeenv_utf8(env_ffmpeg_datadir);
991 freeenv_utf8(env_home);
992 return f;
993 }
994
995 16509 int cmdutils_isalnum(char c)
996 {
997
3/4
✓ Branch 0 taken 214 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✓ Branch 3 taken 16468 times.
16509 return (c >= '0' && c <= '9') ||
998
5/6
✓ Branch 0 taken 214 times.
✓ Branch 1 taken 16295 times.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40 times.
✓ Branch 5 taken 16469 times.
33058 (c >= 'A' && c <= 'Z') ||
999
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 (c >= 'a' && c <= 'z');
1000 }
1001
1002 33075 void stream_specifier_uninit(StreamSpecifier *ss)
1003 {
1004 33075 av_freep(&ss->meta_key);
1005 33075 av_freep(&ss->meta_val);
1006 33075 av_freep(&ss->remainder);
1007
1008 33075 memset(ss, 0, sizeof(*ss));
1009 33075 }
1010
1011 33003 int stream_specifier_parse(StreamSpecifier *ss, const char *spec,
1012 int allow_remainder, void *logctx)
1013 {
1014 char *endptr;
1015 int ret;
1016
1017 33003 memset(ss, 0, sizeof(*ss));
1018
1019 33003 ss->idx = -1;
1020 33003 ss->media_type = AVMEDIA_TYPE_UNKNOWN;
1021 33003 ss->stream_list = STREAM_LIST_ALL;
1022
1023 33003 av_log(logctx, AV_LOG_TRACE, "Parsing stream specifier: %s\n", spec);
1024
1025
2/2
✓ Branch 0 taken 16869 times.
✓ Branch 1 taken 32605 times.
82477 while (*spec) {
1026
3/4
✓ Branch 0 taken 385 times.
✓ Branch 1 taken 16484 times.
✓ Branch 2 taken 385 times.
✗ Branch 3 not taken.
16869 if (*spec <= '9' && *spec >= '0') { /* opt:index */
1027 385 ss->idx = strtol(spec, &endptr, 0);
1028
1029
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 385 times.
385 av_assert0(endptr > spec);
1030 385 spec = endptr;
1031
1032 385 av_log(logctx, AV_LOG_TRACE,
1033 "Parsed index: %d; remainder: %s\n", ss->idx, spec);
1034
1035 // this terminates the specifier
1036 385 break;
1037
6/6
✓ Branch 0 taken 1498 times.
✓ Branch 1 taken 14986 times.
✓ Branch 2 taken 46 times.
✓ Branch 3 taken 1452 times.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 36 times.
16484 } else if ((*spec == 'v' || *spec == 'a' || *spec == 's' ||
1038
7/8
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 16464 times.
✓ Branch 7 taken 14 times.
16488 *spec == 'd' || *spec == 't' || *spec == 'V') &&
1039 16478 !cmdutils_isalnum(*(spec + 1))) { /* opt:[vasdtV] */
1040
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16464 times.
16464 if (ss->media_type != AVMEDIA_TYPE_UNKNOWN) {
1041 av_log(logctx, AV_LOG_ERROR, "Stream type specified multiple times\n");
1042 ret = AVERROR(EINVAL);
1043 goto fail;
1044 }
1045
1046
4/7
✓ Branch 0 taken 14973 times.
✓ Branch 1 taken 1452 times.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
16464 switch (*spec++) {
1047 14973 case 'v': ss->media_type = AVMEDIA_TYPE_VIDEO; break;
1048 1452 case 'a': ss->media_type = AVMEDIA_TYPE_AUDIO; break;
1049 36 case 's': ss->media_type = AVMEDIA_TYPE_SUBTITLE; break;
1050 case 'd': ss->media_type = AVMEDIA_TYPE_DATA; break;
1051 3 case 't': ss->media_type = AVMEDIA_TYPE_ATTACHMENT; break;
1052 case 'V': ss->media_type = AVMEDIA_TYPE_VIDEO;
1053 ss->no_apic = 1; break;
1054 default: av_assert0(0);
1055 }
1056
1057 16464 av_log(logctx, AV_LOG_TRACE, "Parsed media type: %s; remainder: %s\n",
1058 av_get_media_type_string(ss->media_type), spec);
1059
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
20 } else if (*spec == 'g' && *(spec + 1) == ':') {
1060
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ss->stream_list != STREAM_LIST_ALL)
1061 goto multiple_stream_lists;
1062
1063 6 spec += 2;
1064
5/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
6 if (*spec == '#' || (*spec == 'i' && *(spec + 1) == ':')) {
1065 3 ss->stream_list = STREAM_LIST_GROUP_ID;
1066
1067
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 spec += 1 + (*spec == 'i');
1068 } else
1069 3 ss->stream_list = STREAM_LIST_GROUP_IDX;
1070
1071 6 ss->list_id = strtol(spec, &endptr, 0);
1072
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (spec == endptr) {
1073 av_log(logctx, AV_LOG_ERROR, "Expected stream group idx/ID, got: %s\n", spec);
1074 ret = AVERROR(EINVAL);
1075 goto fail;
1076 }
1077 6 spec = endptr;
1078
1079 6 av_log(logctx, AV_LOG_TRACE, "Parsed stream group %s: %"PRId64"; remainder: %s\n",
1080
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 ss->stream_list == STREAM_LIST_GROUP_ID ? "ID" : "index", ss->list_id, spec);
1081
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
14 } else if (*spec == 'p' && *(spec + 1) == ':') {
1082 if (ss->stream_list != STREAM_LIST_ALL)
1083 goto multiple_stream_lists;
1084
1085 ss->stream_list = STREAM_LIST_PROGRAM;
1086
1087 spec += 2;
1088 ss->list_id = strtol(spec, &endptr, 0);
1089 if (spec == endptr) {
1090 av_log(logctx, AV_LOG_ERROR, "Expected program ID, got: %s\n", spec);
1091 ret = AVERROR(EINVAL);
1092 goto fail;
1093 }
1094 spec = endptr;
1095
1096 av_log(logctx, AV_LOG_TRACE,
1097 "Parsed program ID: %"PRId64"; remainder: %s\n", ss->list_id, spec);
1098
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13 times.
14 } else if (!strncmp(spec, "disp:", 5)) {
1099 1 const AVClass *st_class = av_stream_get_class();
1100 1 const AVOption *o = av_opt_find(&st_class, "disposition", NULL, 0, AV_OPT_SEARCH_FAKE_OBJ);
1101 1 char *disp = NULL;
1102 size_t len;
1103
1104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(o);
1105
1106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ss->disposition) {
1107 av_log(logctx, AV_LOG_ERROR, "Multiple disposition specifiers\n");
1108 ret = AVERROR(EINVAL);
1109 goto fail;
1110 }
1111
1112 1 spec += 5;
1113
1114 29 for (len = 0; cmdutils_isalnum(spec[len]) ||
1115
6/6
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
57 spec[len] == '_' || spec[len] == '+'; len++)
1116 28 continue;
1117
1118 1 disp = av_strndup(spec, len);
1119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!disp) {
1120 ret = AVERROR(ENOMEM);
1121 goto fail;
1122 }
1123
1124 1 ret = av_opt_eval_flags(&st_class, o, disp, &ss->disposition);
1125 1 av_freep(&disp);
1126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
1127 av_log(logctx, AV_LOG_ERROR, "Invalid disposition specifier\n");
1128 goto fail;
1129 }
1130
1131 1 spec += len;
1132
1133 1 av_log(logctx, AV_LOG_TRACE,
1134 "Parsed disposition: 0x%x; remainder: %s\n", ss->disposition, spec);
1135
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 } else if (*spec == '#' ||
1136
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
13 (*spec == 'i' && *(spec + 1) == ':')) {
1137 if (ss->stream_list != STREAM_LIST_ALL)
1138 goto multiple_stream_lists;
1139
1140 ss->stream_list = STREAM_LIST_STREAM_ID;
1141
1142 spec += 1 + (*spec == 'i');
1143 ss->list_id = strtol(spec, &endptr, 0);
1144 if (spec == endptr) {
1145 av_log(logctx, AV_LOG_ERROR, "Expected stream ID, got: %s\n", spec);
1146 ret = AVERROR(EINVAL);
1147 goto fail;
1148 }
1149 spec = endptr;
1150
1151 av_log(logctx, AV_LOG_TRACE,
1152 "Parsed stream ID: %"PRId64"; remainder: %s\n", ss->list_id, spec);
1153
1154 // this terminates the specifier
1155 break;
1156
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
13 } else if (*spec == 'm' && *(spec + 1) == ':') {
1157 av_assert0(!ss->meta_key && !ss->meta_val);
1158
1159 spec += 2;
1160 ss->meta_key = av_get_token(&spec, ":");
1161 if (!ss->meta_key) {
1162 ret = AVERROR(ENOMEM);
1163 goto fail;
1164 }
1165 if (*spec == ':') {
1166 spec++;
1167 ss->meta_val = av_get_token(&spec, ":");
1168 if (!ss->meta_val) {
1169 ret = AVERROR(ENOMEM);
1170 goto fail;
1171 }
1172 }
1173
1174 av_log(logctx, AV_LOG_TRACE,
1175 "Parsed metadata: %s:%s; remainder: %s", ss->meta_key,
1176 ss->meta_val ? ss->meta_val : "<any value>", spec);
1177
1178 // this terminates the specifier
1179 break;
1180
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
13 } else if (*spec == 'u' && (*(spec + 1) == '\0' || *(spec + 1) == ':')) {
1181 ss->usable_only = 1;
1182 spec++;
1183 av_log(logctx, AV_LOG_ERROR, "Parsed 'usable only'\n");
1184
1185 // this terminates the specifier
1186 break;
1187 } else
1188 break;
1189
1190
2/2
✓ Branch 0 taken 173 times.
✓ Branch 1 taken 16298 times.
16471 if (*spec == ':')
1191 173 spec++;
1192 }
1193
1194
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 32990 times.
33003 if (*spec) {
1195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (!allow_remainder) {
1196 av_log(logctx, AV_LOG_ERROR,
1197 "Trailing garbage at the end of a stream specifier: %s\n",
1198 spec);
1199 ret = AVERROR(EINVAL);
1200 goto fail;
1201 }
1202
1203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (*spec == ':')
1204 spec++;
1205
1206 13 ss->remainder = av_strdup(spec);
1207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (!ss->remainder) {
1208 ret = AVERROR(EINVAL);
1209 goto fail;
1210 }
1211 }
1212
1213 33003 return 0;
1214
1215 multiple_stream_lists:
1216 av_log(logctx, AV_LOG_ERROR,
1217 "Cannot combine multiple program/group designators in a "
1218 "single stream specifier");
1219 ret = AVERROR(EINVAL);
1220
1221 fail:
1222 stream_specifier_uninit(ss);
1223 return ret;
1224 }
1225
1226 34678 unsigned stream_specifier_match(const StreamSpecifier *ss,
1227 const AVFormatContext *s, const AVStream *st,
1228 void *logctx)
1229 {
1230 34678 const AVStreamGroup *g = NULL;
1231 34678 const AVProgram *p = NULL;
1232 34678 int start_stream = 0, nb_streams;
1233 34678 int nb_matched = 0;
1234
1235
3/6
✗ Branch 0 not taken.
✓ Branch 1 taken 34662 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
34678 switch (ss->stream_list) {
1236 case STREAM_LIST_STREAM_ID:
1237 // <n-th> stream with given ID makes no sense and should be impossible to request
1238 av_assert0(ss->idx < 0);
1239 // return early if we know for sure the stream does not match
1240 if (st->id != ss->list_id)
1241 return 0;
1242 start_stream = st->index;
1243 nb_streams = st->index + 1;
1244 break;
1245 34662 case STREAM_LIST_ALL:
1246
2/2
✓ Branch 0 taken 34004 times.
✓ Branch 1 taken 658 times.
34662 start_stream = ss->idx >= 0 ? 0 : st->index;
1247 34662 nb_streams = st->index + 1;
1248 34662 break;
1249 case STREAM_LIST_PROGRAM:
1250 for (unsigned i = 0; i < s->nb_programs; i++) {
1251 if (s->programs[i]->id == ss->list_id) {
1252 p = s->programs[i];
1253 break;
1254 }
1255 }
1256 if (!p) {
1257 av_log(logctx, AV_LOG_WARNING, "No program with ID %"PRId64" exists,"
1258 " stream specifier can never match\n", ss->list_id);
1259 return 0;
1260 }
1261 nb_streams = p->nb_stream_indexes;
1262 break;
1263 9 case STREAM_LIST_GROUP_ID:
1264
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 for (unsigned i = 0; i < s->nb_stream_groups; i++) {
1265
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 if (ss->list_id == s->stream_groups[i]->id) {
1266 9 g = s->stream_groups[i];
1267 9 break;
1268 }
1269 }
1270 // fall-through
1271 case STREAM_LIST_GROUP_IDX:
1272
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 9 times.
16 if (ss->stream_list == STREAM_LIST_GROUP_IDX &&
1273
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 ss->list_id >= 0 && ss->list_id < s->nb_stream_groups)
1274 7 g = s->stream_groups[ss->list_id];
1275
1276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (!g) {
1277 av_log(logctx, AV_LOG_WARNING, "No stream group with group %s %"
1278 PRId64" exists, stream specifier can never match\n",
1279 ss->stream_list == STREAM_LIST_GROUP_ID ? "ID" : "index",
1280 ss->list_id);
1281 return 0;
1282 }
1283 16 nb_streams = g->nb_streams;
1284 16 break;
1285 default: av_assert0(0);
1286 }
1287
1288
2/2
✓ Branch 0 taken 36124 times.
✓ Branch 1 taken 456 times.
36580 for (int i = start_stream; i < nb_streams; i++) {
1289
3/4
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 36089 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 36089 times.
36124 const AVStream *candidate = s->streams[g ? g->streams[i]->index :
1290 p ? p->stream_index[i] : i];
1291
1292
2/2
✓ Branch 0 taken 20667 times.
✓ Branch 1 taken 15457 times.
36124 if (ss->media_type != AVMEDIA_TYPE_UNKNOWN &&
1293
2/2
✓ Branch 0 taken 19977 times.
✓ Branch 1 taken 690 times.
20667 (ss->media_type != candidate->codecpar->codec_type ||
1294
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 19977 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
19977 (ss->no_apic && (candidate->disposition & AV_DISPOSITION_ATTACHED_PIC))))
1295 690 continue;
1296
1297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35434 times.
35434 if (ss->meta_key) {
1298 const AVDictionaryEntry *tag = av_dict_get(candidate->metadata,
1299 ss->meta_key, NULL, 0);
1300
1301 if (!tag)
1302 continue;
1303 if (ss->meta_val && strcmp(tag->value, ss->meta_val))
1304 continue;
1305 }
1306
1307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35434 times.
35434 if (ss->usable_only) {
1308 const AVCodecParameters *par = candidate->codecpar;
1309
1310 switch (par->codec_type) {
1311 case AVMEDIA_TYPE_AUDIO:
1312 if (!par->sample_rate || !par->ch_layout.nb_channels ||
1313 par->format == AV_SAMPLE_FMT_NONE)
1314 continue;
1315 break;
1316 case AVMEDIA_TYPE_VIDEO:
1317 if (!par->width || !par->height || par->format == AV_PIX_FMT_NONE)
1318 continue;
1319 break;
1320 case AVMEDIA_TYPE_UNKNOWN:
1321 continue;
1322 }
1323 }
1324
1325
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 35379 times.
35434 if (ss->disposition &&
1326
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 11 times.
55 (candidate->disposition & ss->disposition) != ss->disposition)
1327 44 continue;
1328
1329
2/2
✓ Branch 0 taken 34222 times.
✓ Branch 1 taken 1168 times.
35390 if (st == candidate)
1330
4/4
✓ Branch 0 taken 567 times.
✓ Branch 1 taken 33655 times.
✓ Branch 2 taken 172 times.
✓ Branch 3 taken 395 times.
34222 return ss->idx < 0 || ss->idx == nb_matched;
1331
1332 1168 nb_matched++;
1333 }
1334
1335 456 return 0;
1336 }
1337
1338 516 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1339 {
1340 StreamSpecifier ss;
1341 int ret;
1342
1343 516 ret = stream_specifier_parse(&ss, spec, 0, NULL);
1344
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 516 times.
516 if (ret < 0)
1345 return ret;
1346
1347 516 ret = stream_specifier_match(&ss, s, st, NULL);
1348 516 stream_specifier_uninit(&ss);
1349 516 return ret;
1350 }
1351
1352 24618 int filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_id,
1353 AVFormatContext *s, AVStream *st, const AVCodec *codec,
1354 AVDictionary **dst, AVDictionary **opts_used)
1355 {
1356 24618 AVDictionary *ret = NULL;
1357 24618 const AVDictionaryEntry *t = NULL;
1358 49236 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1359
2/2
✓ Branch 0 taken 8716 times.
✓ Branch 1 taken 15902 times.
24618 : AV_OPT_FLAG_DECODING_PARAM;
1360 24618 char prefix = 0;
1361 24618 const AVClass *cc = avcodec_get_class();
1362
1363
4/4
✓ Branch 0 taken 19098 times.
✓ Branch 1 taken 5195 times.
✓ Branch 2 taken 240 times.
✓ Branch 3 taken 85 times.
24618 switch (st->codecpar->codec_type) {
1364 19098 case AVMEDIA_TYPE_VIDEO:
1365 19098 prefix = 'v';
1366 19098 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1367 19098 break;
1368 5195 case AVMEDIA_TYPE_AUDIO:
1369 5195 prefix = 'a';
1370 5195 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1371 5195 break;
1372 240 case AVMEDIA_TYPE_SUBTITLE:
1373 240 prefix = 's';
1374 240 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1375 240 break;
1376 }
1377
1378
2/2
✓ Branch 1 taken 75642 times.
✓ Branch 2 taken 24618 times.
100260 while (t = av_dict_iterate(opts, t)) {
1379 const AVClass *priv_class;
1380 75642 char *p = strchr(t->key, ':');
1381 75642 int used = 0;
1382
1383 /* check stream specification in opt name */
1384
2/2
✓ Branch 0 taken 286 times.
✓ Branch 1 taken 75356 times.
75642 if (p) {
1385 286 int err = check_stream_specifier(s, st, p + 1);
1386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 286 times.
286 if (err < 0) {
1387 av_dict_free(&ret);
1388 return err;
1389
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 208 times.
286 } else if (!err)
1390 78 continue;
1391
1392 208 *p = 0;
1393 }
1394
1395
4/4
✓ Branch 1 taken 2824 times.
✓ Branch 2 taken 72740 times.
✓ Branch 3 taken 1632 times.
✓ Branch 4 taken 1192 times.
75564 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1396 1632 !codec ||
1397
4/4
✓ Branch 0 taken 764 times.
✓ Branch 1 taken 868 times.
✓ Branch 2 taken 345 times.
✓ Branch 3 taken 419 times.
2396 ((priv_class = codec->priv_class) &&
1398 764 av_opt_find(&priv_class, t->key, NULL, flags,
1399 AV_OPT_SEARCH_FAKE_OBJ))) {
1400 74277 av_dict_set(&ret, t->key, t->value, 0);
1401 74277 used = 1;
1402
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1284 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
1290 } else if (t->key[0] == prefix &&
1403 3 av_opt_find(&cc, t->key + 1, NULL, flags,
1404 AV_OPT_SEARCH_FAKE_OBJ)) {
1405 1 av_dict_set(&ret, t->key + 1, t->value, 0);
1406 1 used = 1;
1407 }
1408
1409
2/2
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 75356 times.
75564 if (p)
1410 208 *p = ':';
1411
1412
4/4
✓ Branch 0 taken 74278 times.
✓ Branch 1 taken 1286 times.
✓ Branch 2 taken 49180 times.
✓ Branch 3 taken 25098 times.
75564 if (used && opts_used)
1413 49180 av_dict_set(opts_used, t->key, "", 0);
1414 }
1415
1416 24618 *dst = ret;
1417 24618 return 0;
1418 }
1419
1420 7361 int setup_find_stream_info_opts(AVFormatContext *s,
1421 AVDictionary *local_codec_opts,
1422 AVDictionary ***dst)
1423 {
1424 int ret;
1425 AVDictionary **opts;
1426
1427 7361 *dst = NULL;
1428
1429
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 7271 times.
7361 if (!s->nb_streams)
1430 90 return 0;
1431
1432 7271 opts = av_calloc(s->nb_streams, sizeof(*opts));
1433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7271 times.
7271 if (!opts)
1434 return AVERROR(ENOMEM);
1435
1436
2/2
✓ Branch 0 taken 7910 times.
✓ Branch 1 taken 7271 times.
15181 for (int i = 0; i < s->nb_streams; i++) {
1437 7910 ret = filter_codec_opts(local_codec_opts, s->streams[i]->codecpar->codec_id,
1438 7910 s, s->streams[i], NULL, &opts[i], NULL);
1439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7910 times.
7910 if (ret < 0)
1440 goto fail;
1441 }
1442 7271 *dst = opts;
1443 7271 return 0;
1444 fail:
1445 for (int i = 0; i < s->nb_streams; i++)
1446 av_dict_free(&opts[i]);
1447 av_freep(&opts);
1448 return ret;
1449 }
1450
1451 248911 int grow_array(void **array, int elem_size, int *size, int new_size)
1452 {
1453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 248911 times.
248911 if (new_size >= INT_MAX / elem_size) {
1454 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1455 return AVERROR(ERANGE);
1456 }
1457
1/2
✓ Branch 0 taken 248911 times.
✗ Branch 1 not taken.
248911 if (*size < new_size) {
1458 248911 uint8_t *tmp = av_realloc_array(*array, new_size, elem_size);
1459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 248911 times.
248911 if (!tmp)
1460 return AVERROR(ENOMEM);
1461 248911 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
1462 248911 *size = new_size;
1463 248911 *array = tmp;
1464 248911 return 0;
1465 }
1466 return 0;
1467 }
1468
1469 46728 void *allocate_array_elem(void *ptr, size_t elem_size, int *nb_elems)
1470 {
1471 void *new_elem;
1472
1473
2/4
✓ Branch 1 taken 46728 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 46728 times.
93456 if (!(new_elem = av_mallocz(elem_size)) ||
1474 46728 av_dynarray_add_nofree(ptr, nb_elems, new_elem) < 0)
1475 return NULL;
1476 46728 return new_elem;
1477 }
1478
1479 5601 double get_rotation(const int32_t *displaymatrix)
1480 {
1481 5601 double theta = 0;
1482
1/2
✓ Branch 0 taken 5601 times.
✗ Branch 1 not taken.
5601 if (displaymatrix)
1483 5601 theta = -round(av_display_rotation_get(displaymatrix));
1484
1485 5601 theta -= 360*floor(theta/360 + 0.9/360);
1486
1487
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5601 times.
5601 if (fabs(theta - 90*round(theta/90)) > 2)
1488 av_log(NULL, AV_LOG_WARNING, "Odd rotation angle.\n"
1489 "If you want to help, upload a sample "
1490 "of this file to https://streams.videolan.org/upload/ "
1491 "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)");
1492
1493 5601 return theta;
1494 }
1495
1496 /* read file contents into a string */
1497 70 char *file_read(const char *filename)
1498 {
1499 70 AVIOContext *pb = NULL;
1500 70 int ret = avio_open(&pb, filename, AVIO_FLAG_READ);
1501 AVBPrint bprint;
1502 char *str;
1503
1504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (ret < 0) {
1505 av_log(NULL, AV_LOG_ERROR, "Error opening file %s.\n", filename);
1506 return NULL;
1507 }
1508
1509 70 av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
1510 70 ret = avio_read_to_bprint(pb, &bprint, SIZE_MAX);
1511 70 avio_closep(&pb);
1512
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (ret < 0) {
1513 av_bprint_finalize(&bprint, NULL);
1514 return NULL;
1515 }
1516 70 ret = av_bprint_finalize(&bprint, &str);
1517
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (ret < 0)
1518 return NULL;
1519 70 return str;
1520 }
1521
1522 7207 void remove_avoptions(AVDictionary **a, AVDictionary *b)
1523 {
1524 7207 const AVDictionaryEntry *t = NULL;
1525
1526
2/2
✓ Branch 1 taken 23892 times.
✓ Branch 2 taken 7207 times.
31099 while ((t = av_dict_iterate(b, t))) {
1527 23892 av_dict_set(a, t->key, NULL, AV_DICT_MATCH_CASE);
1528 }
1529 7207 }
1530
1531 22016 int check_avoptions(AVDictionary *m)
1532 {
1533 22016 const AVDictionaryEntry *t = av_dict_iterate(m, NULL);
1534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22016 times.
22016 if (t) {
1535 av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key);
1536 return AVERROR_OPTION_NOT_FOUND;
1537 }
1538
1539 22016 return 0;
1540 }
1541