FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/cmdutils.c
Date: 2026-01-16 07:34:38
Exec Total Coverage
Lines: 546 885 61.7%
Functions: 34 42 81.0%
Branches: 408 755 54.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 17115 void uninit_opts(void)
63 {
64 17115 av_dict_free(&swr_opts);
65 17115 av_dict_free(&sws_dict);
66 17115 av_dict_free(&format_opts);
67 17115 av_dict_free(&codec_opts);
68 17115 }
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 8643 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 8643 }
83
84 38693 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 38693 double d = av_strtod(numstr, &tail);
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38693 times.
38693 if (*tail)
91 error = "Expected number for %s but found: %s\n";
92
2/4
✓ Branch 0 taken 38693 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 38693 times.
38693 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 38399 times.
✓ Branch 1 taken 294 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 38399 times.
38693 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 38693 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
38693 else if (type == OPT_TYPE_INT && (int)d != d)
97 error = "Expected int for %s but found %s\n";
98 else {
99 38693 *dst = d;
100 38693 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 1308531 static const OptionDef *find_option(const OptionDef *po, const char *name)
154 {
155
2/2
✓ Branch 0 taken 396 times.
✓ Branch 1 taken 1308135 times.
1308531 if (*name == '/')
156 396 name++;
157
158
2/2
✓ Branch 0 taken 194655705 times.
✓ Branch 1 taken 745036 times.
195400741 while (po->name) {
159 const char *end;
160
6/6
✓ Branch 1 taken 1332954 times.
✓ Branch 2 taken 193322751 times.
✓ Branch 3 taken 821480 times.
✓ Branch 4 taken 511474 times.
✓ Branch 5 taken 769459 times.
✓ Branch 6 taken 52021 times.
194655705 if (av_strstart(name, po->name, &end) && (!*end || *end == ':'))
161 break;
162 194092210 po++;
163 }
164 1308531 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 8643 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
226 {
227 /* nothing to do */
228 8643 }
229 #endif /* HAVE_COMMANDLINETOARGVW */
230
231 546251 static int opt_has_arg(const OptionDef *o)
232 {
233
2/2
✓ Branch 0 taken 171903 times.
✓ Branch 1 taken 374348 times.
546251 if (o->type == OPT_TYPE_BOOL)
234 171903 return 0;
235
2/2
✓ Branch 0 taken 131337 times.
✓ Branch 1 taken 243011 times.
374348 if (o->type == OPT_TYPE_FUNC)
236 131337 return !!(o->flags & OPT_FUNC_ARG);
237 243011 return 1;
238 }
239
240 101759 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 203518 void *dst = po->flags & OPT_FLAG_OFFSET ?
246
2/2
✓ Branch 0 taken 49167 times.
✓ Branch 1 taken 52592 times.
101759 (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
247 101759 char *arg_allocated = NULL;
248
249 101759 enum OptionType so_type = po->type;
250
251 101759 SpecifierOptList *sol = NULL;
252 double num;
253 101759 int ret = 0;
254
255
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 101693 times.
101759 if (*opt == '/') {
256 66 opt++;
257
258
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 66 times.
66 if (!opt_has_arg(po)) {
259 av_log(NULL, AV_LOG_FATAL,
260 "Requested to load an argument from file for an option '%s'"
261 " which does not take an argument\n",
262 po->name);
263 return AVERROR(EINVAL);
264 }
265
266 66 arg_allocated = read_file_to_string(arg);
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (!arg_allocated) {
268 av_log(NULL, AV_LOG_FATAL,
269 "Error reading the value for option '%s' from file: %s\n",
270 opt, arg);
271 return AVERROR(EINVAL);
272 }
273
274 66 arg = arg_allocated;
275 }
276
277
2/2
✓ Branch 0 taken 33407 times.
✓ Branch 1 taken 68352 times.
101759 if (po->flags & OPT_FLAG_SPEC) {
278 33407 char *p = strchr(opt, ':');
279 char *str;
280
281 33407 sol = dst;
282 33407 ret = GROW_ARRAY(sol->opt, sol->nb_opt);
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33407 times.
33407 if (ret < 0)
284 goto finish;
285
286
2/2
✓ Branch 0 taken 16597 times.
✓ Branch 1 taken 16810 times.
33407 str = av_strdup(p ? p + 1 : "");
287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33407 times.
33407 if (!str) {
288 ret = AVERROR(ENOMEM);
289 goto finish;
290 }
291 33407 sol->opt[sol->nb_opt - 1].specifier = str;
292
293
2/2
✓ Branch 0 taken 33151 times.
✓ Branch 1 taken 256 times.
33407 if (po->flags & OPT_FLAG_PERSTREAM) {
294 33151 ret = stream_specifier_parse(&sol->opt[sol->nb_opt - 1].stream_spec,
295 str, 0, NULL);
296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33151 times.
33151 if (ret < 0)
297 goto finish;
298 }
299
300 33407 dst = &sol->opt[sol->nb_opt - 1].u;
301 }
302
303
2/2
✓ Branch 0 taken 39805 times.
✓ Branch 1 taken 61954 times.
101759 if (po->type == OPT_TYPE_STRING) {
304 char *str;
305
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 39778 times.
39805 if (arg_allocated) {
306 27 str = arg_allocated;
307 27 arg_allocated = NULL;
308 } else
309 39778 str = av_strdup(arg);
310 39805 av_freep(dst);
311
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39805 times.
39805 if (!str) {
313 ret = AVERROR(ENOMEM);
314 goto finish;
315 }
316
317 39805 *(char **)dst = str;
318
4/4
✓ Branch 0 taken 29103 times.
✓ Branch 1 taken 32851 times.
✓ Branch 2 taken 138 times.
✓ Branch 3 taken 28965 times.
61954 } else if (po->type == OPT_TYPE_BOOL || po->type == OPT_TYPE_INT) {
319 32989 ret = parse_number(opt, arg, OPT_TYPE_INT64, INT_MIN, INT_MAX, &num);
320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32989 times.
32989 if (ret < 0)
321 goto finish;
322
323 32989 *(int *)dst = num;
324 32989 so_type = OPT_TYPE_INT;
325
2/2
✓ Branch 0 taken 5410 times.
✓ Branch 1 taken 23555 times.
28965 } else if (po->type == OPT_TYPE_INT64) {
326 5410 ret = parse_number(opt, arg, OPT_TYPE_INT64, INT64_MIN, (double)INT64_MAX, &num);
327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5410 times.
5410 if (ret < 0)
328 goto finish;
329
330 5410 *(int64_t *)dst = num;
331
2/2
✓ Branch 0 taken 1338 times.
✓ Branch 1 taken 22217 times.
23555 } else if (po->type == OPT_TYPE_TIME) {
332 1338 ret = av_parse_time(dst, arg, 1);
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1338 times.
1338 if (ret < 0) {
334 av_log(NULL, AV_LOG_ERROR, "Invalid duration for option %s: %s\n",
335 opt, arg);
336 goto finish;
337 }
338 1338 so_type = OPT_TYPE_INT64;
339
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 22214 times.
22217 } else if (po->type == OPT_TYPE_FLOAT) {
340 3 ret = parse_number(opt, arg, OPT_TYPE_FLOAT, -INFINITY, INFINITY, &num);
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
342 goto finish;
343
344 3 *(float *)dst = num;
345
2/2
✓ Branch 0 taken 291 times.
✓ Branch 1 taken 21923 times.
22214 } else if (po->type == OPT_TYPE_DOUBLE) {
346 291 ret = parse_number(opt, arg, OPT_TYPE_DOUBLE, -INFINITY, INFINITY, &num);
347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 291 times.
291 if (ret < 0)
348 goto finish;
349
350 291 *(double *)dst = num;
351 } else {
352
2/4
✓ Branch 0 taken 21923 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 21923 times.
21923 av_assert0(po->type == OPT_TYPE_FUNC && po->u.func_arg);
353
354 21923 ret = po->u.func_arg(optctx, opt, arg);
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21923 times.
21923 if (ret < 0) {
356 if ((strcmp(opt, "init_hw_device") != 0) || (strcmp(arg, "list") != 0)) {
357 av_log(NULL, AV_LOG_ERROR,
358 "Failed to set value '%s' for option '%s': %s\n",
359 arg, opt, av_err2str(ret));
360 }
361 goto finish;
362 }
363 }
364
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 101758 times.
101759 if (po->flags & OPT_EXIT) {
365 1 ret = AVERROR_EXIT;
366 1 goto finish;
367 }
368
369
2/2
✓ Branch 0 taken 68351 times.
✓ Branch 1 taken 33407 times.
101758 if (sol) {
370 33407 sol->type = so_type;
371
2/2
✓ Branch 0 taken 2508 times.
✓ Branch 1 taken 30899 times.
35915 sol->opt_canon = (po->flags & OPT_HAS_CANON) ?
372 2508 find_option(defs, po->u1.name_canon) : po;
373 }
374
375 68351 finish:
376 101759 av_freep(&arg_allocated);
377 101759 return ret;
378 }
379
380 10455 int parse_option(void *optctx, const char *opt, const char *arg,
381 const OptionDef *options)
382 {
383 static const OptionDef opt_avoptions = {
384 .name = "AVOption passthrough",
385 .type = OPT_TYPE_FUNC,
386 .flags = OPT_FUNC_ARG,
387 .u.func_arg = opt_default,
388 };
389
390 const OptionDef *po;
391 int ret;
392
393 10455 po = find_option(options, opt);
394
5/6
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 10328 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 126 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
10455 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
395 /* handle 'no' bool option */
396 1 po = find_option(options, opt + 2);
397
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))
398 1 arg = "0";
399
2/2
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 10292 times.
10454 } else if (po->type == OPT_TYPE_BOOL)
400 162 arg = "1";
401
402
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 10329 times.
10455 if (!po->name)
403 126 po = &opt_avoptions;
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10455 times.
10455 if (!po->name) {
405 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
406 return AVERROR(EINVAL);
407 }
408
3/4
✓ Branch 1 taken 10179 times.
✓ Branch 2 taken 276 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10179 times.
10455 if (opt_has_arg(po) && !arg) {
409 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
410 return AVERROR(EINVAL);
411 }
412
413 10455 ret = write_option(optctx, po, opt, arg, options);
414
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10455 times.
10455 if (ret < 0)
415 return ret;
416
417 10455 return opt_has_arg(po);
418 }
419
420 171 int parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
421 int (*parse_arg_function)(void *, const char*))
422 {
423 const char *opt;
424 171 int optindex, handleoptions = 1, ret;
425
426 /* perform system-dependent conversions for arguments list */
427 171 prepare_app_arguments(&argc, &argv);
428
429 /* parse options */
430 171 optindex = 1;
431
2/2
✓ Branch 0 taken 857 times.
✓ Branch 1 taken 171 times.
1028 while (optindex < argc) {
432 857 opt = argv[optindex++];
433
434
4/6
✓ Branch 0 taken 857 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 696 times.
✓ Branch 3 taken 161 times.
✓ Branch 4 taken 696 times.
✗ Branch 5 not taken.
857 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
435
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 696 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
696 if (opt[1] == '-' && opt[2] == '\0') {
436 handleoptions = 0;
437 continue;
438 }
439 696 opt++;
440
441
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 696 times.
696 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
442 return ret;
443 696 optindex += ret;
444 } else {
445
1/2
✓ Branch 0 taken 161 times.
✗ Branch 1 not taken.
161 if (parse_arg_function) {
446 161 ret = parse_arg_function(optctx, opt);
447
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 161 times.
161 if (ret < 0)
448 return ret;
449 }
450 }
451 }
452
453 171 return 0;
454 }
455
456 24332 int parse_optgroup(void *optctx, OptionGroup *g, const OptionDef *defs)
457 {
458 int i, ret;
459
460 24332 av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
461 24332 g->group_def->name, g->arg);
462
463
2/2
✓ Branch 0 taken 91304 times.
✓ Branch 1 taken 24331 times.
115635 for (i = 0; i < g->nb_opts; i++) {
464 91304 Option *o = &g->opts[i];
465
466
2/2
✓ Branch 0 taken 49761 times.
✓ Branch 1 taken 41543 times.
91304 if (g->group_def->flags &&
467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49761 times.
49761 !(g->group_def->flags & o->opt->flags)) {
468 av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
469 "%s %s -- you are trying to apply an input option to an "
470 "output file or vice versa. Move this option before the "
471 "file it belongs to.\n", o->key, o->opt->help,
472 g->group_def->name, g->arg);
473 return AVERROR(EINVAL);
474 }
475
476 91304 av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
477 91304 o->key, o->opt->help, o->val);
478
479 91304 ret = write_option(optctx, o->opt, o->key, o->val, defs);
480
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 91303 times.
91304 if (ret < 0)
481 1 return ret;
482 }
483
484 24331 av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
485
486 24331 return 0;
487 }
488
489 43214 int locate_option(int argc, char **argv, const OptionDef *options,
490 const char *optname)
491 {
492 const OptionDef *po;
493 int i;
494
495
2/2
✓ Branch 0 taken 1002690 times.
✓ Branch 1 taken 43195 times.
1045885 for (i = 1; i < argc; i++) {
496 1002690 const char *cur_opt = argv[i];
497
498
4/4
✓ Branch 0 taken 973054 times.
✓ Branch 1 taken 29636 times.
✓ Branch 2 taken 13537 times.
✓ Branch 3 taken 959517 times.
1002690 if (!(cur_opt[0] == '-' && cur_opt[1]))
499 43173 continue;
500 959517 cur_opt++;
501
502 959517 po = find_option(options, cur_opt);
503
5/6
✓ Branch 0 taken 626983 times.
✓ Branch 1 taken 332534 times.
✓ Branch 2 taken 126859 times.
✓ Branch 3 taken 500124 times.
✓ Branch 4 taken 126859 times.
✗ Branch 5 not taken.
959517 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
504 126859 po = find_option(options, cur_opt + 2);
505
506
3/4
✓ Branch 0 taken 500164 times.
✓ Branch 1 taken 459353 times.
✓ Branch 2 taken 500164 times.
✗ Branch 3 not taken.
959517 if ((!po->name && !strcmp(cur_opt, optname)) ||
507
4/4
✓ Branch 0 taken 459353 times.
✓ Branch 1 taken 500164 times.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 459334 times.
959517 (po->name && !strcmp(optname, po->name)))
508 19 return i;
509
510
4/4
✓ Branch 0 taken 459334 times.
✓ Branch 1 taken 500164 times.
✓ Branch 3 taken 294512 times.
✓ Branch 4 taken 164822 times.
959498 if (!po->name || opt_has_arg(po))
511 794676 i++;
512 }
513 43195 return 0;
514 }
515
516 static void dump_argument(FILE *report_file, const char *a)
517 {
518 const unsigned char *p;
519
520 for (p = a; *p; p++)
521 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
522 *p == '_' || (*p >= 'a' && *p <= 'z')))
523 break;
524 if (!*p) {
525 fputs(a, report_file);
526 return;
527 }
528 fputc('"', report_file);
529 for (p = a; *p; p++) {
530 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
531 fprintf(report_file, "\\%c", *p);
532 else if (*p < ' ' || *p > '~')
533 fprintf(report_file, "\\x%02x", *p);
534 else
535 fputc(*p, report_file);
536 }
537 fputc('"', report_file);
538 }
539
540 8643 static void check_options(const OptionDef *po)
541 {
542
2/2
✓ Branch 0 taken 1681467 times.
✓ Branch 1 taken 8643 times.
1690110 while (po->name) {
543
2/2
✓ Branch 0 taken 1016640 times.
✓ Branch 1 taken 664827 times.
1681467 if (po->flags & OPT_PERFILE)
544
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1016640 times.
1016640 av_assert0(po->flags & (OPT_INPUT | OPT_OUTPUT | OPT_DECODER));
545
546
2/2
✓ Branch 0 taken 653106 times.
✓ Branch 1 taken 1028361 times.
1681467 if (po->type == OPT_TYPE_FUNC)
547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 653106 times.
653106 av_assert0(!(po->flags & (OPT_FLAG_OFFSET | OPT_FLAG_SPEC)));
548
549 // OPT_FUNC_ARG can only be ser for OPT_TYPE_FUNC
550
3/4
✓ Branch 0 taken 1028361 times.
✓ Branch 1 taken 653106 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1028361 times.
1681467 av_assert0((po->type == OPT_TYPE_FUNC) || !(po->flags & OPT_FUNC_ARG));
551
552 1681467 po++;
553 }
554 8643 }
555
556 8643 void parse_loglevel(int argc, char **argv, const OptionDef *options)
557 {
558 int idx;
559 char *env;
560
561 8643 check_options(options);
562
563 8643 idx = locate_option(argc, argv, options, "loglevel");
564
2/2
✓ Branch 0 taken 8642 times.
✓ Branch 1 taken 1 times.
8643 if (!idx)
565 8642 idx = locate_option(argc, argv, options, "v");
566
3/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 8625 times.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
8643 if (idx && argv[idx + 1])
567 18 opt_loglevel(NULL, "loglevel", argv[idx + 1]);
568 8643 idx = locate_option(argc, argv, options, "report");
569 8643 env = getenv_utf8("FFREPORT");
570
2/4
✓ Branch 0 taken 8643 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8643 times.
8643 if (env || idx) {
571 FILE *report_file = NULL;
572 init_report(env, &report_file);
573 if (report_file) {
574 int i;
575 fprintf(report_file, "Command line:\n");
576 for (i = 0; i < argc; i++) {
577 dump_argument(report_file, argv[i]);
578 fputc(i < argc - 1 ? ' ' : '\n', report_file);
579 }
580 fflush(report_file);
581 }
582 }
583 8643 freeenv_utf8(env);
584 8643 idx = locate_option(argc, argv, options, "hide_banner");
585
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8642 times.
8643 if (idx)
586 1 hide_banner = 1;
587 8643 }
588
589 318391 static const AVOption *opt_find(void *obj, const char *name, const char *unit,
590 int opt_flags, int search_flags)
591 {
592 318391 const AVOption *o = av_opt_find(obj, name, unit, opt_flags, search_flags);
593
4/4
✓ Branch 0 taken 92731 times.
✓ Branch 1 taken 225660 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 92725 times.
318391 if(o && !o->flags)
594 6 return NULL;
595 318385 return o;
596 }
597
598 #define FLAGS ((o->type == AV_OPT_TYPE_FLAGS && (arg[0]=='-' || arg[0]=='+')) ? AV_DICT_APPEND : 0)
599 118012 int opt_default(void *optctx, const char *opt, const char *arg)
600 {
601 const AVOption *o;
602 118012 int consumed = 0;
603 char opt_stripped[128];
604 const char *p;
605 118012 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
606 #if CONFIG_SWSCALE
607 118012 const AVClass *sc = sws_get_class();
608 #endif
609 #if CONFIG_SWRESAMPLE
610 118012 const AVClass *swr_class = swr_get_class();
611 #endif
612
613
2/4
✓ Branch 0 taken 118012 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 118012 times.
118012 if (!strcmp(opt, "debug") || !strcmp(opt, "fdebug"))
614 av_log_set_level(AV_LOG_DEBUG);
615
616
2/2
✓ Branch 0 taken 118000 times.
✓ Branch 1 taken 12 times.
118012 if (!(p = strchr(opt, ':')))
617 118000 p = opt + strlen(opt);
618 118012 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
619
620
2/2
✓ Branch 1 taken 56925 times.
✓ Branch 2 taken 61087 times.
118012 if ((o = opt_find(&cc, opt_stripped, NULL, 0,
621 56925 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
622
8/8
✓ Branch 0 taken 56920 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 56908 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 15825 times.
✓ Branch 5 taken 41083 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 15841 times.
72767 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
623 15842 (o = opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) {
624
5/6
✓ Branch 0 taken 22906 times.
✓ Branch 1 taken 38182 times.
✓ Branch 2 taken 22906 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15746 times.
✓ Branch 5 taken 7160 times.
61088 av_dict_set(&codec_opts, opt, arg, FLAGS);
625 61088 consumed = 1;
626 }
627
2/2
✓ Branch 1 taken 15838 times.
✓ Branch 2 taken 102174 times.
118012 if ((o = opt_find(&fc, opt, NULL, 0,
628 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
629
5/6
✓ Branch 0 taken 15593 times.
✓ Branch 1 taken 245 times.
✓ Branch 2 taken 15593 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15585 times.
✓ Branch 5 taken 8 times.
15838 av_dict_set(&format_opts, opt, arg, FLAGS);
630
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 15762 times.
15838 if (consumed)
631 76 av_log(NULL, AV_LOG_VERBOSE, "Routing option %s to both codec and muxer layer\n", opt);
632 15838 consumed = 1;
633 }
634 #if CONFIG_SWSCALE
635
4/4
✓ Branch 0 taken 41162 times.
✓ Branch 1 taken 76850 times.
✓ Branch 3 taken 15799 times.
✓ Branch 4 taken 25363 times.
118012 if (!consumed && (o = opt_find(&sc, opt, NULL, 0,
636 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
637
2/4
✓ Branch 0 taken 15799 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15799 times.
✗ Branch 3 not taken.
15799 if (!strcmp(opt, "srcw") || !strcmp(opt, "srch") ||
638
2/4
✓ Branch 0 taken 15799 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15799 times.
✗ Branch 3 not taken.
15799 !strcmp(opt, "dstw") || !strcmp(opt, "dsth") ||
639
2/4
✓ Branch 0 taken 15799 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15799 times.
15799 !strcmp(opt, "src_format") || !strcmp(opt, "dst_format")) {
640 av_log(NULL, AV_LOG_ERROR, "Directly using swscale dimensions/format options is not supported, please use the -s or -pix_fmt options\n");
641 return AVERROR(EINVAL);
642 }
643
4/6
✓ Branch 0 taken 15799 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15799 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15584 times.
✓ Branch 5 taken 215 times.
15799 av_dict_set(&sws_dict, opt, arg, FLAGS);
644
645 15799 consumed = 1;
646 }
647 #else
648 if (!consumed && !strcmp(opt, "sws_flags")) {
649 av_log(NULL, AV_LOG_WARNING, "Ignoring %s %s, due to disabled swscale\n", opt, arg);
650 consumed = 1;
651 }
652 #endif
653 #if CONFIG_SWRESAMPLE
654
3/4
✓ Branch 0 taken 25363 times.
✓ Branch 1 taken 92649 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 25363 times.
118012 if (!consumed && (o=opt_find(&swr_class, opt, NULL, 0,
655 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
656 av_dict_set(&swr_opts, opt, arg, FLAGS);
657 consumed = 1;
658 }
659 #endif
660
661
2/2
✓ Branch 0 taken 92649 times.
✓ Branch 1 taken 25363 times.
118012 if (consumed)
662 92649 return 0;
663 25363 return AVERROR_OPTION_NOT_FOUND;
664 }
665
666 /*
667 * Check whether given option is a group separator.
668 *
669 * @return index of the group definition that matched or -1 if none
670 */
671 191214 static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
672 const char *opt)
673 {
674 int i;
675
676
2/2
✓ Branch 0 taken 566257 times.
✓ Branch 1 taken 183828 times.
750085 for (i = 0; i < nb_groups; i++) {
677 566257 const OptionGroupDef *p = &groups[i];
678
4/4
✓ Branch 0 taken 375043 times.
✓ Branch 1 taken 191214 times.
✓ Branch 2 taken 7386 times.
✓ Branch 3 taken 367657 times.
566257 if (p->sep && !strcmp(p->sep, opt))
679 7386 return i;
680 }
681
682 183828 return -1;
683 }
684
685 /*
686 * Finish parsing an option group.
687 *
688 * @param group_idx which group definition should this group belong to
689 * @param arg argument of the group delimiting option
690 */
691 15861 static int finish_group(OptionParseContext *octx, int group_idx,
692 const char *arg)
693 {
694 15861 OptionGroupList *l = &octx->groups[group_idx];
695 OptionGroup *g;
696 int ret;
697
698 15861 ret = GROW_ARRAY(l->groups, l->nb_groups);
699
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15861 times.
15861 if (ret < 0)
700 return ret;
701
702 15861 g = &l->groups[l->nb_groups - 1];
703
704 15861 *g = octx->cur_group;
705 15861 g->arg = arg;
706 15861 g->group_def = l->group_def;
707 15861 g->sws_dict = sws_dict;
708 15861 g->swr_opts = swr_opts;
709 15861 g->codec_opts = codec_opts;
710 15861 g->format_opts = format_opts;
711
712 15861 codec_opts = NULL;
713 15861 format_opts = NULL;
714 15861 sws_dict = NULL;
715 15861 swr_opts = NULL;
716
717 15861 memset(&octx->cur_group, 0, sizeof(octx->cur_group));
718
719 15861 return ret;
720 }
721
722 /*
723 * Add an option instance to currently parsed group.
724 */
725 91305 static int add_opt(OptionParseContext *octx, const OptionDef *opt,
726 const char *key, const char *val)
727 {
728 91305 int global = !(opt->flags & OPT_PERFILE);
729
2/2
✓ Branch 0 taken 41543 times.
✓ Branch 1 taken 49762 times.
91305 OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
730 int ret;
731
732 91305 ret = GROW_ARRAY(g->opts, g->nb_opts);
733
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 91305 times.
91305 if (ret < 0)
734 return ret;
735
736 91305 g->opts[g->nb_opts - 1].opt = opt;
737 91305 g->opts[g->nb_opts - 1].key = key;
738 91305 g->opts[g->nb_opts - 1].val = val;
739
740 91305 return 0;
741 }
742
743 8472 static int init_parse_context(OptionParseContext *octx,
744 const OptionGroupDef *groups, int nb_groups)
745 {
746 static const OptionGroupDef global_group = { "global" };
747 int i;
748
749 8472 memset(octx, 0, sizeof(*octx));
750
751 8472 octx->groups = av_calloc(nb_groups, sizeof(*octx->groups));
752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8472 times.
8472 if (!octx->groups)
753 return AVERROR(ENOMEM);
754 8472 octx->nb_groups = nb_groups;
755
756
2/2
✓ Branch 0 taken 25416 times.
✓ Branch 1 taken 8472 times.
33888 for (i = 0; i < octx->nb_groups; i++)
757 25416 octx->groups[i].group_def = &groups[i];
758
759 8472 octx->global_opts.group_def = &global_group;
760 8472 octx->global_opts.arg = "";
761
762 8472 return 0;
763 }
764
765 8472 void uninit_parse_context(OptionParseContext *octx)
766 {
767 int i, j;
768
769
2/2
✓ Branch 0 taken 25416 times.
✓ Branch 1 taken 8472 times.
33888 for (i = 0; i < octx->nb_groups; i++) {
770 25416 OptionGroupList *l = &octx->groups[i];
771
772
2/2
✓ Branch 0 taken 15861 times.
✓ Branch 1 taken 25416 times.
41277 for (j = 0; j < l->nb_groups; j++) {
773 15861 av_freep(&l->groups[j].opts);
774 15861 av_dict_free(&l->groups[j].codec_opts);
775 15861 av_dict_free(&l->groups[j].format_opts);
776
777 15861 av_dict_free(&l->groups[j].sws_dict);
778 15861 av_dict_free(&l->groups[j].swr_opts);
779 }
780 25416 av_freep(&l->groups);
781 }
782 8472 av_freep(&octx->groups);
783
784 8472 av_freep(&octx->cur_group.opts);
785 8472 av_freep(&octx->global_opts.opts);
786
787 8472 uninit_opts();
788 8472 }
789
790 8472 int split_commandline(OptionParseContext *octx, int argc, char *argv[],
791 const OptionDef *options,
792 const OptionGroupDef *groups, int nb_groups)
793 {
794 int ret;
795 8472 int optindex = 1;
796 8472 int dashdash = -2;
797
798 /* perform system-dependent conversions for arguments list */
799 8472 prepare_app_arguments(&argc, &argv);
800
801 8472 ret = init_parse_context(octx, groups, nb_groups);
802
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8472 times.
8472 if (ret < 0)
803 return ret;
804
805 8472 av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
806
807
2/2
✓ Branch 0 taken 199689 times.
✓ Branch 1 taken 8472 times.
208161 while (optindex < argc) {
808 199689 const char *opt = argv[optindex++], *arg;
809 const OptionDef *po;
810 int group_idx;
811
812 199689 av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
813
814
3/6
✓ Branch 0 taken 193922 times.
✓ Branch 1 taken 5767 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 193922 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
199689 if (opt[0] == '-' && opt[1] == '-' && !opt[2]) {
815 dashdash = optindex;
816 continue;
817 }
818 /* unnamed group separators, e.g. output filename */
819
5/6
✓ Branch 0 taken 193922 times.
✓ Branch 1 taken 5767 times.
✓ Branch 2 taken 191214 times.
✓ Branch 3 taken 2708 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 191214 times.
199689 if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) {
820 8475 ret = finish_group(octx, 0, opt);
821
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8475 times.
8475 if (ret < 0)
822 return ret;
823
824 8475 av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
825 8475 continue;
826 }
827 191214 opt++;
828
829 #define GET_ARG(arg) \
830 do { \
831 arg = argv[optindex++]; \
832 if (!arg) { \
833 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
834 return AVERROR(EINVAL); \
835 } \
836 } while (0)
837
838 /* named group separators, e.g. -i */
839 191214 group_idx = match_group_separator(groups, nb_groups, opt);
840
2/2
✓ Branch 0 taken 7386 times.
✓ Branch 1 taken 183828 times.
191214 if (group_idx >= 0) {
841
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7386 times.
7386 GET_ARG(arg);
842 7386 ret = finish_group(octx, group_idx, arg);
843
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7386 times.
7386 if (ret < 0)
844 return ret;
845
846 7386 av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
847 7386 groups[group_idx].name, arg);
848 7386 continue;
849 }
850
851 /* normal options */
852 183828 po = find_option(options, opt);
853
2/2
✓ Branch 0 taken 65942 times.
✓ Branch 1 taken 117886 times.
183828 if (po->name) {
854
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 65941 times.
65942 if (po->flags & OPT_EXIT) {
855 /* optional argument, e.g. -h */
856 1 arg = argv[optindex++];
857
2/2
✓ Branch 1 taken 58616 times.
✓ Branch 2 taken 7325 times.
65941 } else if (opt_has_arg(po)) {
858
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58616 times.
58616 GET_ARG(arg);
859 } else {
860 7325 arg = "1";
861 }
862
863 65942 ret = add_opt(octx, po, opt, arg);
864
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65942 times.
65942 if (ret < 0)
865 return ret;
866
867 65942 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
868 65942 "argument '%s'.\n", po->name, po->help, arg);
869 65942 continue;
870 }
871
872 /* AVOptions */
873
1/2
✓ Branch 0 taken 117886 times.
✗ Branch 1 not taken.
117886 if (argv[optindex]) {
874 117886 ret = opt_default(NULL, opt, argv[optindex]);
875
2/2
✓ Branch 0 taken 92523 times.
✓ Branch 1 taken 25363 times.
117886 if (ret >= 0) {
876 92523 av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
877 92523 "argument '%s'.\n", opt, argv[optindex]);
878 92523 optindex++;
879 92523 continue;
880
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25363 times.
25363 } else if (ret != AVERROR_OPTION_NOT_FOUND) {
881 av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
882 "with argument '%s'.\n", opt, argv[optindex]);
883 return ret;
884 }
885 }
886
887 /* boolean -nofoo options */
888
3/6
✓ Branch 0 taken 25363 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25363 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 25363 times.
✗ Branch 5 not taken.
50726 if (opt[0] == 'n' && opt[1] == 'o' &&
889 25363 (po = find_option(options, opt + 2)) &&
890
2/4
✓ Branch 0 taken 25363 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25363 times.
✗ Branch 3 not taken.
25363 po->name && po->type == OPT_TYPE_BOOL) {
891 25363 ret = add_opt(octx, po, opt, "0");
892
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25363 times.
25363 if (ret < 0)
893 return ret;
894
895 25363 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
896 25363 "argument 0.\n", po->name, po->help);
897 25363 continue;
898 }
899
900 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
901 return AVERROR_OPTION_NOT_FOUND;
902 }
903
904
3/6
✓ Branch 0 taken 8472 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8472 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 8472 times.
8472 if (octx->cur_group.nb_opts || codec_opts || format_opts)
905 av_log(NULL, AV_LOG_WARNING, "Trailing option(s) found in the "
906 "command: may be ignored.\n");
907
908 8472 av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
909
910 8472 return 0;
911 }
912
913 int read_yesno(void)
914 {
915 int c = getchar();
916 int yesno = (av_toupper(c) == 'Y');
917
918 while (c != '\n' && c != EOF)
919 c = getchar();
920
921 return yesno;
922 }
923
924 FILE *get_preset_file(char *filename, size_t filename_size,
925 const char *preset_name, int is_path,
926 const char *codec_name)
927 {
928 FILE *f = NULL;
929 int i;
930 #if HAVE_GETMODULEHANDLE && defined(_WIN32)
931 char *datadir = NULL;
932 #endif
933 char *env_home = getenv_utf8("HOME");
934 char *env_ffmpeg_datadir = getenv_utf8("FFMPEG_DATADIR");
935 const char *base[3] = { env_ffmpeg_datadir,
936 env_home, /* index=1(HOME) is special: search in a .ffmpeg subfolder */
937 FFMPEG_DATADIR, };
938
939 if (is_path) {
940 av_strlcpy(filename, preset_name, filename_size);
941 f = fopen_utf8(filename, "r");
942 } else {
943 #if HAVE_GETMODULEHANDLE && defined(_WIN32)
944 wchar_t *datadir_w = get_module_filename(NULL);
945 base[2] = NULL;
946
947 if (wchartoutf8(datadir_w, &datadir))
948 datadir = NULL;
949 av_free(datadir_w);
950
951 if (datadir)
952 {
953 char *ls;
954 for (ls = datadir; *ls; ls++)
955 if (*ls == '\\') *ls = '/';
956
957 if (ls = strrchr(datadir, '/'))
958 {
959 ptrdiff_t datadir_len = ls - datadir;
960 size_t desired_size = datadir_len + strlen("/ffpresets") + 1;
961 char *new_datadir = av_realloc_array(
962 datadir, desired_size, sizeof *datadir);
963 if (new_datadir) {
964 datadir = new_datadir;
965 strcpy(datadir + datadir_len, "/ffpresets");
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 17172 int cmdutils_isalnum(char c)
996 {
997
4/4
✓ Branch 0 taken 246 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 49 times.
✓ Branch 3 taken 17122 times.
17172 return (c >= '0' && c <= '9') ||
998
5/6
✓ Branch 0 taken 247 times.
✓ Branch 1 taken 16925 times.
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 48 times.
✓ Branch 5 taken 17123 times.
34392 (c >= 'A' && c <= 'Z') ||
999
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 (c >= 'a' && c <= 'z');
1000 }
1001
1002 34225 void stream_specifier_uninit(StreamSpecifier *ss)
1003 {
1004 34225 av_freep(&ss->meta_key);
1005 34225 av_freep(&ss->meta_val);
1006 34225 av_freep(&ss->remainder);
1007
1008 34225 memset(ss, 0, sizeof(*ss));
1009 34225 }
1010
1011 34221 int stream_specifier_parse(StreamSpecifier *ss, const char *spec,
1012 int allow_remainder, void *logctx)
1013 {
1014 char *endptr;
1015 int ret;
1016
1017 34221 memset(ss, 0, sizeof(*ss));
1018
1019 34221 ss->idx = -1;
1020 34221 ss->media_type = AVMEDIA_TYPE_UNKNOWN;
1021 34221 ss->stream_list = STREAM_LIST_ALL;
1022
1023 34221 av_log(logctx, AV_LOG_TRACE, "Parsing stream specifier: %s\n", spec);
1024
1025
2/2
✓ Branch 0 taken 17702 times.
✓ Branch 1 taken 33679 times.
85602 while (*spec) {
1026
3/4
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 17252 times.
✓ Branch 2 taken 450 times.
✗ Branch 3 not taken.
17702 if (*spec <= '9' && *spec >= '0') { /* opt:index */
1027 450 ss->idx = strtol(spec, &endptr, 0);
1028
1029
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 450 times.
450 av_assert0(endptr > spec);
1030 450 spec = endptr;
1031
1032 450 av_log(logctx, AV_LOG_TRACE,
1033 "Parsed index: %d; remainder: %s\n", ss->idx, spec);
1034
1035 // this terminates the specifier
1036 450 break;
1037
6/6
✓ Branch 0 taken 1738 times.
✓ Branch 1 taken 15514 times.
✓ Branch 2 taken 170 times.
✓ Branch 3 taken 1568 times.
✓ Branch 4 taken 117 times.
✓ Branch 5 taken 53 times.
17252 } else if ((*spec == 'v' || *spec == 'a' || *spec == 's' ||
1038
7/8
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 111 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 111 times.
✓ Branch 6 taken 17118 times.
✓ Branch 7 taken 23 times.
17258 *spec == 'd' || *spec == 't' || *spec == 'V') &&
1039 17141 !cmdutils_isalnum(*(spec + 1))) { /* opt:[vasdtV] */
1040
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17118 times.
17118 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
5/7
✓ Branch 0 taken 15499 times.
✓ Branch 1 taken 1563 times.
✓ Branch 2 taken 51 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
17118 switch (*spec++) {
1047 15499 case 'v': ss->media_type = AVMEDIA_TYPE_VIDEO; break;
1048 1563 case 'a': ss->media_type = AVMEDIA_TYPE_AUDIO; break;
1049 51 case 's': ss->media_type = AVMEDIA_TYPE_SUBTITLE; break;
1050 2 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 17118 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 41 times.
✓ Branch 1 taken 93 times.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
134 } else if (*spec == 'g' && *(spec + 1) == ':') {
1060
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 if (ss->stream_list != STREAM_LIST_ALL)
1061 goto multiple_stream_lists;
1062
1063 41 spec += 2;
1064
5/6
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
41 if (*spec == '#' || (*spec == 'i' && *(spec + 1) == ':')) {
1065 5 ss->stream_list = STREAM_LIST_GROUP_ID;
1066
1067
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
5 spec += 1 + (*spec == 'i');
1068 } else
1069 36 ss->stream_list = STREAM_LIST_GROUP_IDX;
1070
1071 41 ss->list_id = strtol(spec, &endptr, 0);
1072
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 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 41 spec = endptr;
1078
1079 41 av_log(logctx, AV_LOG_TRACE, "Parsed stream group %s: %"PRId64"; remainder: %s\n",
1080
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 36 times.
41 ss->stream_list == STREAM_LIST_GROUP_ID ? "ID" : "index", ss->list_id, spec);
1081
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
93 } 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 92 times.
93 } 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 92 times.
✗ Branch 1 not taken.
92 } else if (*spec == '#' ||
1136
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
92 (*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
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 91 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
92 } 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 92 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
92 } 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 229 times.
✓ Branch 1 taken 16931 times.
17160 if (*spec == ':')
1191 229 spec++;
1192 }
1193
1194
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 34129 times.
34221 if (*spec) {
1195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
92 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 92 times.
92 if (*spec == ':')
1204 spec++;
1205
1206 92 ss->remainder = av_strdup(spec);
1207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
92 if (!ss->remainder) {
1208 ret = AVERROR(EINVAL);
1209 goto fail;
1210 }
1211 }
1212
1213 34221 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 36016 unsigned stream_specifier_match(const StreamSpecifier *ss,
1227 const AVFormatContext *s, const AVStream *st,
1228 void *logctx)
1229 {
1230 36016 const AVStreamGroup *g = NULL;
1231 36016 const AVProgram *p = NULL;
1232 36016 int start_stream = 0, nb_streams;
1233 36016 int nb_matched = 0;
1234
1235
3/6
✗ Branch 0 not taken.
✓ Branch 1 taken 35917 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
✓ Branch 4 taken 82 times.
✗ Branch 5 not taken.
36016 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 35917 case STREAM_LIST_ALL:
1246
2/2
✓ Branch 0 taken 35211 times.
✓ Branch 1 taken 706 times.
35917 start_stream = ss->idx >= 0 ? 0 : st->index;
1247 35917 nb_streams = st->index + 1;
1248 35917 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 17 case STREAM_LIST_GROUP_ID:
1264
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 for (unsigned i = 0; i < s->nb_stream_groups; i++) {
1265
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 9 times.
26 if (ss->list_id == s->stream_groups[i]->id) {
1266 17 g = s->stream_groups[i];
1267 17 break;
1268 }
1269 }
1270 // fall-through
1271 case STREAM_LIST_GROUP_IDX:
1272
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 17 times.
99 if (ss->stream_list == STREAM_LIST_GROUP_IDX &&
1273
2/4
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 82 times.
✗ Branch 3 not taken.
82 ss->list_id >= 0 && ss->list_id < s->nb_stream_groups)
1274 82 g = s->stream_groups[ss->list_id];
1275
1276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 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 99 nb_streams = g->nb_streams;
1284 99 break;
1285 default: av_assert0(0);
1286 }
1287
1288
2/2
✓ Branch 0 taken 37568 times.
✓ Branch 1 taken 474 times.
38042 for (int i = start_stream; i < nb_streams; i++) {
1289
3/4
✓ Branch 0 taken 201 times.
✓ Branch 1 taken 37367 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 37367 times.
37568 const AVStream *candidate = s->streams[g ? g->streams[i]->index :
1290 p ? p->stream_index[i] : i];
1291
1292
2/2
✓ Branch 0 taken 21465 times.
✓ Branch 1 taken 16103 times.
37568 if (ss->media_type != AVMEDIA_TYPE_UNKNOWN &&
1293
2/2
✓ Branch 0 taken 20752 times.
✓ Branch 1 taken 713 times.
21465 (ss->media_type != candidate->codecpar->codec_type ||
1294
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 20752 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
20752 (ss->no_apic && (candidate->disposition & AV_DISPOSITION_ATTACHED_PIC))))
1295 713 continue;
1296
1297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36855 times.
36855 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 36855 times.
36855 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 36800 times.
36855 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 35542 times.
✓ Branch 1 taken 1269 times.
36811 if (st == candidate)
1330
4/4
✓ Branch 0 taken 689 times.
✓ Branch 1 taken 34853 times.
✓ Branch 2 taken 237 times.
✓ Branch 3 taken 452 times.
35542 return ss->idx < 0 || ss->idx == nb_matched;
1331
1332 1269 nb_matched++;
1333 }
1334
1335 474 return 0;
1336 }
1337
1338 521 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1339 {
1340 StreamSpecifier ss;
1341 int ret;
1342
1343 521 ret = stream_specifier_parse(&ss, spec, 0, NULL);
1344
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 521 times.
521 if (ret < 0)
1345 return ret;
1346
1347 521 ret = stream_specifier_match(&ss, s, st, NULL);
1348 521 stream_specifier_uninit(&ss);
1349 521 return ret;
1350 }
1351
1352 unsigned stream_group_specifier_match(const StreamSpecifier *ss,
1353 const AVFormatContext *s, const AVStreamGroup *stg,
1354 void *logctx)
1355 {
1356 int start_stream_group = 0, nb_stream_groups;
1357 int nb_matched = 0;
1358
1359 if (ss->idx >= 0)
1360 return 0;
1361
1362 switch (ss->stream_list) {
1363 case STREAM_LIST_STREAM_ID:
1364 case STREAM_LIST_ALL:
1365 case STREAM_LIST_PROGRAM:
1366 return 0;
1367 case STREAM_LIST_GROUP_ID:
1368 // <n-th> stream with given ID makes no sense and should be impossible to request
1369 av_assert0(ss->idx < 0);
1370 // return early if we know for sure the stream does not match
1371 if (stg->id != ss->list_id)
1372 return 0;
1373 start_stream_group = stg->index;
1374 nb_stream_groups = stg->index + 1;
1375 break;
1376 case STREAM_LIST_GROUP_IDX:
1377 start_stream_group = ss->list_id >= 0 ? 0 : stg->index;
1378 nb_stream_groups = stg->index + 1;
1379 break;
1380 default: av_assert0(0);
1381 }
1382
1383 for (int i = start_stream_group; i < nb_stream_groups; i++) {
1384 const AVStreamGroup *candidate = s->stream_groups[i];
1385
1386 if (ss->meta_key) {
1387 const AVDictionaryEntry *tag = av_dict_get(candidate->metadata,
1388 ss->meta_key, NULL, 0);
1389
1390 if (!tag)
1391 continue;
1392 if (ss->meta_val && strcmp(tag->value, ss->meta_val))
1393 continue;
1394 }
1395
1396 if (ss->usable_only) {
1397 switch (candidate->type) {
1398 case AV_STREAM_GROUP_PARAMS_TILE_GRID: {
1399 const AVStreamGroupTileGrid *tg = candidate->params.tile_grid;
1400 if (!tg->coded_width || !tg->coded_height || !tg->nb_tiles ||
1401 !tg->width || !tg->height || !tg->nb_tiles)
1402 continue;
1403 break;
1404 }
1405 default:
1406 continue;
1407 }
1408 }
1409
1410 if (ss->disposition &&
1411 (candidate->disposition & ss->disposition) != ss->disposition)
1412 continue;
1413
1414 if (stg == candidate)
1415 return ss->list_id < 0 || ss->list_id == nb_matched;
1416
1417 nb_matched++;
1418 }
1419
1420 return 0;
1421 }
1422
1423 25358 int filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_id,
1424 AVFormatContext *s, AVStream *st, const AVCodec *codec,
1425 AVDictionary **dst, AVDictionary **opts_used)
1426 {
1427 25358 AVDictionary *ret = NULL;
1428 25358 const AVDictionaryEntry *t = NULL;
1429 50716 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1430
2/2
✓ Branch 0 taken 8979 times.
✓ Branch 1 taken 16379 times.
25358 : AV_OPT_FLAG_DECODING_PARAM;
1431 25358 char prefix = 0;
1432 25358 const AVClass *cc = avcodec_get_class();
1433
1434
4/4
✓ Branch 0 taken 19670 times.
✓ Branch 1 taken 5332 times.
✓ Branch 2 taken 267 times.
✓ Branch 3 taken 89 times.
25358 switch (st->codecpar->codec_type) {
1435 19670 case AVMEDIA_TYPE_VIDEO:
1436 19670 prefix = 'v';
1437 19670 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1438 19670 break;
1439 5332 case AVMEDIA_TYPE_AUDIO:
1440 5332 prefix = 'a';
1441 5332 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1442 5332 break;
1443 267 case AVMEDIA_TYPE_SUBTITLE:
1444 267 prefix = 's';
1445 267 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1446 267 break;
1447 }
1448
1449
2/2
✓ Branch 1 taken 78444 times.
✓ Branch 2 taken 25358 times.
103802 while (t = av_dict_iterate(opts, t)) {
1450 const AVClass *priv_class;
1451 78444 char *p = strchr(t->key, ':');
1452 78444 int used = 0;
1453
1454 /* check stream specification in opt name */
1455
2/2
✓ Branch 0 taken 291 times.
✓ Branch 1 taken 78153 times.
78444 if (p) {
1456 291 int err = check_stream_specifier(s, st, p + 1);
1457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 291 times.
291 if (err < 0) {
1458 av_dict_free(&ret);
1459 return err;
1460
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 211 times.
291 } else if (!err)
1461 80 continue;
1462
1463 211 *p = 0;
1464 }
1465
1466
4/4
✓ Branch 1 taken 2961 times.
✓ Branch 2 taken 75403 times.
✓ Branch 3 taken 1693 times.
✓ Branch 4 taken 1268 times.
78364 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1467 1693 !codec ||
1468
4/4
✓ Branch 0 taken 802 times.
✓ Branch 1 taken 891 times.
✓ Branch 2 taken 353 times.
✓ Branch 3 taken 449 times.
2495 ((priv_class = codec->priv_class) &&
1469 802 av_opt_find(&priv_class, t->key, NULL, flags,
1470 AV_OPT_SEARCH_FAKE_OBJ))) {
1471 77024 av_dict_set(&ret, t->key, t->value, 0);
1472 77024 used = 1;
1473
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1335 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 4 times.
1345 } else if (t->key[0] == prefix &&
1474 5 av_opt_find(&cc, t->key + 1, NULL, flags,
1475 AV_OPT_SEARCH_FAKE_OBJ)) {
1476 1 av_dict_set(&ret, t->key + 1, t->value, 0);
1477 1 used = 1;
1478 }
1479
1480
2/2
✓ Branch 0 taken 211 times.
✓ Branch 1 taken 78153 times.
78364 if (p)
1481 211 *p = ':';
1482
1483
4/4
✓ Branch 0 taken 77025 times.
✓ Branch 1 taken 1339 times.
✓ Branch 2 taken 50740 times.
✓ Branch 3 taken 26285 times.
78364 if (used && opts_used)
1484 50740 av_dict_set(opts_used, t->key, "", 0);
1485 }
1486
1487 25358 *dst = ret;
1488 25358 return 0;
1489 }
1490
1491 7553 int setup_find_stream_info_opts(AVFormatContext *s,
1492 AVDictionary *local_codec_opts,
1493 AVDictionary ***dst)
1494 {
1495 int ret;
1496 AVDictionary **opts;
1497
1498 7553 *dst = NULL;
1499
1500
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 7463 times.
7553 if (!s->nb_streams)
1501 90 return 0;
1502
1503 7463 opts = av_calloc(s->nb_streams, sizeof(*opts));
1504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7463 times.
7463 if (!opts)
1505 return AVERROR(ENOMEM);
1506
1507
2/2
✓ Branch 0 taken 8152 times.
✓ Branch 1 taken 7463 times.
15615 for (int i = 0; i < s->nb_streams; i++) {
1508 8152 ret = filter_codec_opts(local_codec_opts, s->streams[i]->codecpar->codec_id,
1509 8152 s, s->streams[i], NULL, &opts[i], NULL);
1510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8152 times.
8152 if (ret < 0)
1511 goto fail;
1512 }
1513 7463 *dst = opts;
1514 7463 return 0;
1515 fail:
1516 for (int i = 0; i < s->nb_streams; i++)
1517 av_dict_free(&opts[i]);
1518 av_freep(&opts);
1519 return ret;
1520 }
1521
1522 256019 int grow_array(void **array, int elem_size, int *size, int new_size)
1523 {
1524
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 256019 times.
256019 if (new_size >= INT_MAX / elem_size) {
1525 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1526 return AVERROR(ERANGE);
1527 }
1528
1/2
✓ Branch 0 taken 256019 times.
✗ Branch 1 not taken.
256019 if (*size < new_size) {
1529 256019 uint8_t *tmp = av_realloc_array(*array, new_size, elem_size);
1530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 256019 times.
256019 if (!tmp)
1531 return AVERROR(ENOMEM);
1532 256019 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
1533 256019 *size = new_size;
1534 256019 *array = tmp;
1535 256019 return 0;
1536 }
1537 return 0;
1538 }
1539
1540 48034 void *allocate_array_elem(void *ptr, size_t elem_size, int *nb_elems)
1541 {
1542 void *new_elem;
1543
1544 48034 new_elem = av_mallocz(elem_size);
1545
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48034 times.
48034 if (!new_elem)
1546 return NULL;
1547
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 48034 times.
48034 if (av_dynarray_add_nofree(ptr, nb_elems, new_elem) < 0)
1548 av_freep(&new_elem);
1549
1550 48034 return new_elem;
1551 }
1552
1553 5738 double get_rotation(const int32_t *displaymatrix)
1554 {
1555 5738 double theta = 0;
1556
1/2
✓ Branch 0 taken 5738 times.
✗ Branch 1 not taken.
5738 if (displaymatrix)
1557 5738 theta = -round(av_display_rotation_get(displaymatrix));
1558
1559 5738 theta -= 360*floor(theta/360 + 0.9/360);
1560
1561
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5738 times.
5738 if (fabs(theta - 90*round(theta/90)) > 2)
1562 av_log(NULL, AV_LOG_WARNING, "Odd rotation angle.\n"
1563 "If you want to help, upload a sample "
1564 "of this file to https://streams.videolan.org/upload/ "
1565 "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)");
1566
1567 5738 return theta;
1568 }
1569
1570 /* read file contents into a string */
1571 74 char *read_file_to_string(const char *filename)
1572 {
1573 74 AVIOContext *pb = NULL;
1574 74 int ret = avio_open(&pb, filename, AVIO_FLAG_READ);
1575 AVBPrint bprint;
1576 char *str;
1577
1578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
74 if (ret < 0) {
1579 av_log(NULL, AV_LOG_ERROR, "Error opening file %s.\n", filename);
1580 return NULL;
1581 }
1582
1583 74 av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
1584 74 ret = avio_read_to_bprint(pb, &bprint, SIZE_MAX);
1585 74 avio_closep(&pb);
1586
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
74 if (ret < 0) {
1587 av_bprint_finalize(&bprint, NULL);
1588 return NULL;
1589 }
1590 74 ret = av_bprint_finalize(&bprint, &str);
1591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
74 if (ret < 0)
1592 return NULL;
1593 74 return str;
1594 }
1595
1596 7384 void remove_avoptions(AVDictionary **a, AVDictionary *b)
1597 {
1598 7384 const AVDictionaryEntry *t = NULL;
1599
1600
2/2
✓ Branch 1 taken 24497 times.
✓ Branch 2 taken 7384 times.
31881 while ((t = av_dict_iterate(b, t))) {
1601 24497 av_dict_set(a, t->key, NULL, AV_DICT_MATCH_CASE);
1602 }
1603 7384 }
1604
1605 22562 int check_avoptions(AVDictionary *m)
1606 {
1607 22562 const AVDictionaryEntry *t = av_dict_iterate(m, NULL);
1608
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22562 times.
22562 if (t) {
1609 av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key);
1610 return AVERROR_OPTION_NOT_FOUND;
1611 }
1612
1613 22562 return 0;
1614 }
1615
1616 void dump_dictionary(void *ctx, const AVDictionary *m,
1617 const char *name, const char *indent,
1618 int log_level)
1619 {
1620 const AVDictionaryEntry *tag = NULL;
1621
1622 if (!m)
1623 return;
1624
1625 av_log(ctx, log_level, "%s%s:\n", indent, name);
1626 while ((tag = av_dict_iterate(m, tag))) {
1627 const char *p = tag->value;
1628 av_log(ctx, log_level, "%s %-16s: ", indent, tag->key);
1629 while (*p) {
1630 size_t len = strcspn(p, "\x8\xa\xb\xc\xd");
1631 av_log(ctx, log_level, "%.*s", (int)(FFMIN(255, len)), p);
1632 p += len;
1633 if (*p == 0xd) av_log(ctx, log_level, " ");
1634 if (*p == 0xa) av_log(ctx, log_level, "\n%s %-16s: ", indent, "");
1635 if (*p) p++;
1636 }
1637 av_log(ctx, log_level, "\n");
1638 }
1639 }
1640