FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/cmdutils.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 411 573 71.7%
Functions: 28 35 80.0%
Branches: 288 468 61.5%

Line Branch Exec Source
1 /*
2 * Various utilities for command line tools
3 * Copyright (c) 2000-2003 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <string.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <math.h>
27
28 /* Include only the enabled headers since some compilers (namely, Sun
29 Studio) will not omit unused inline functions and create undefined
30 references to libraries that are not being built. */
31
32 #include "config.h"
33 #include "compat/va_copy.h"
34 #include "libavformat/avformat.h"
35 #include "libswscale/swscale.h"
36 #include "libswscale/version.h"
37 #include "libswresample/swresample.h"
38 #include "libavutil/avassert.h"
39 #include "libavutil/avstring.h"
40 #include "libavutil/bprint.h"
41 #include "libavutil/channel_layout.h"
42 #include "libavutil/display.h"
43 #include "libavutil/getenv_utf8.h"
44 #include "libavutil/mathematics.h"
45 #include "libavutil/imgutils.h"
46 #include "libavutil/libm.h"
47 #include "libavutil/parseutils.h"
48 #include "libavutil/eval.h"
49 #include "libavutil/dict.h"
50 #include "libavutil/opt.h"
51 #include "cmdutils.h"
52 #include "fopen_utf8.h"
53 #include "opt_common.h"
54 #ifdef _WIN32
55 #include <windows.h>
56 #include "compat/w32dlfcn.h"
57 #endif
58
59 AVDictionary *sws_dict;
60 AVDictionary *swr_opts;
61 AVDictionary *format_opts, *codec_opts;
62
63 int hide_banner = 0;
64
65 13616 void uninit_opts(void)
66 {
67 13616 av_dict_free(&swr_opts);
68 13616 av_dict_free(&sws_dict);
69 13616 av_dict_free(&format_opts);
70 13616 av_dict_free(&codec_opts);
71 13616 }
72
73 void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
74 {
75 vfprintf(stdout, fmt, vl);
76 }
77
78 6875 void init_dynload(void)
79 {
80 #if HAVE_SETDLLDIRECTORY && defined(_WIN32)
81 /* Calling SetDllDirectory with the empty string (but not NULL) removes the
82 * current working directory from the DLL search path as a security pre-caution. */
83 SetDllDirectory("");
84 #endif
85 6875 }
86
87 31482 int parse_number(const char *context, const char *numstr, enum OptionType type,
88 double min, double max, double *dst)
89 {
90 char *tail;
91 const char *error;
92 31482 double d = av_strtod(numstr, &tail);
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31482 times.
31482 if (*tail)
94 error = "Expected number for %s but found: %s\n";
95
2/4
✓ Branch 0 taken 31482 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 31482 times.
31482 else if (d < min || d > max)
96 error = "The value for %s was %s which is not within %f - %f\n";
97
3/4
✓ Branch 0 taken 31190 times.
✓ Branch 1 taken 292 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 31190 times.
31482 else if (type == OPT_TYPE_INT64 && (int64_t)d != d)
98 error = "Expected int64 for %s but found %s\n";
99
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 31482 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
31482 else if (type == OPT_TYPE_INT && (int)d != d)
100 error = "Expected int for %s but found %s\n";
101 else {
102 31482 *dst = d;
103 31482 return 0;
104 }
105
106 av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
107 return AVERROR(EINVAL);
108 }
109
110 void show_help_options(const OptionDef *options, const char *msg, int req_flags,
111 int rej_flags)
112 {
113 const OptionDef *po;
114 int first;
115
116 first = 1;
117 for (po = options; po->name; po++) {
118 char buf[128];
119
120 if (((po->flags & req_flags) != req_flags) ||
121 (po->flags & rej_flags))
122 continue;
123
124 if (first) {
125 printf("%s\n", msg);
126 first = 0;
127 }
128 av_strlcpy(buf, po->name, sizeof(buf));
129
130 if (po->flags & OPT_FLAG_PERSTREAM)
131 av_strlcat(buf, "[:<stream_spec>]", sizeof(buf));
132 else if (po->flags & OPT_FLAG_SPEC)
133 av_strlcat(buf, "[:<spec>]", sizeof(buf));
134
135 if (po->argname)
136 av_strlcatf(buf, sizeof(buf), " <%s>", po->argname);
137
138 printf("-%-17s %s\n", buf, po->help);
139 }
140 printf("\n");
141 }
142
143 void show_help_children(const AVClass *class, int flags)
144 {
145 void *iter = NULL;
146 const AVClass *child;
147 if (class->option) {
148 av_opt_show2(&class, NULL, flags, 0);
149 printf("\n");
150 }
151
152 while (child = av_opt_child_class_iterate(class, &iter))
153 show_help_children(child, flags);
154 }
155
156 1028850 static const OptionDef *find_option(const OptionDef *po, const char *name)
157 {
158
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 1028598 times.
1028850 if (*name == '/')
159 252 name++;
160
161
2/2
✓ Branch 0 taken 148408041 times.
✓ Branch 1 taken 585165 times.
148993206 while (po->name) {
162 const char *end;
163
6/6
✓ Branch 1 taken 1042832 times.
✓ Branch 2 taken 147365209 times.
✓ Branch 3 taken 645360 times.
✓ Branch 4 taken 397472 times.
✓ Branch 5 taken 599147 times.
✓ Branch 6 taken 46213 times.
148408041 if (av_strstart(name, po->name, &end) && (!*end || *end == ':'))
164 break;
165 147964356 po++;
166 }
167 1028850 return po;
168 }
169
170 /* _WIN32 means using the windows libc - cygwin doesn't define that
171 * by default. HAVE_COMMANDLINETOARGVW is true on cygwin, while
172 * it doesn't provide the actual command line via GetCommandLineW(). */
173 #if HAVE_COMMANDLINETOARGVW && defined(_WIN32)
174 #include <shellapi.h>
175 /* Will be leaked on exit */
176 static char** win32_argv_utf8 = NULL;
177 static int win32_argc = 0;
178
179 /**
180 * Prepare command line arguments for executable.
181 * For Windows - perform wide-char to UTF-8 conversion.
182 * Input arguments should be main() function arguments.
183 * @param argc_ptr Arguments number (including executable)
184 * @param argv_ptr Arguments list.
185 */
186 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
187 {
188 char *argstr_flat;
189 wchar_t **argv_w;
190 int i, buffsize = 0, offset = 0;
191
192 if (win32_argv_utf8) {
193 *argc_ptr = win32_argc;
194 *argv_ptr = win32_argv_utf8;
195 return;
196 }
197
198 win32_argc = 0;
199 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
200 if (win32_argc <= 0 || !argv_w)
201 return;
202
203 /* determine the UTF-8 buffer size (including NULL-termination symbols) */
204 for (i = 0; i < win32_argc; i++)
205 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
206 NULL, 0, NULL, NULL);
207
208 win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
209 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
210 if (!win32_argv_utf8) {
211 LocalFree(argv_w);
212 return;
213 }
214
215 for (i = 0; i < win32_argc; i++) {
216 win32_argv_utf8[i] = &argstr_flat[offset];
217 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
218 &argstr_flat[offset],
219 buffsize - offset, NULL, NULL);
220 }
221 win32_argv_utf8[i] = NULL;
222 LocalFree(argv_w);
223
224 *argc_ptr = win32_argc;
225 *argv_ptr = win32_argv_utf8;
226 }
227 #else
228 6875 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
229 {
230 /* nothing to do */
231 6875 }
232 #endif /* HAVE_COMMANDLINETOARGVW */
233
234 430065 static int opt_has_arg(const OptionDef *o)
235 {
236
2/2
✓ Branch 0 taken 137513 times.
✓ Branch 1 taken 292552 times.
430065 if (o->type == OPT_TYPE_BOOL)
237 137513 return 0;
238
2/2
✓ Branch 0 taken 97834 times.
✓ Branch 1 taken 194718 times.
292552 if (o->type == OPT_TYPE_FUNC)
239 97834 return !!(o->flags & OPT_FUNC_ARG);
240 194718 return 1;
241 }
242
243 80585 static int write_option(void *optctx, const OptionDef *po, const char *opt,
244 const char *arg, const OptionDef *defs)
245 {
246 /* new-style options contain an offset into optctx, old-style address of
247 * a global var*/
248 161170 void *dst = po->flags & OPT_FLAG_OFFSET ?
249
2/2
✓ Branch 0 taken 40140 times.
✓ Branch 1 taken 40445 times.
80585 (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
250 80585 char *arg_allocated = NULL;
251
252 80585 SpecifierOptList *sol = NULL;
253 double num;
254 80585 int ret = 0;
255
256
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 80543 times.
80585 if (*opt == '/') {
257 42 opt++;
258
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (po->type == OPT_TYPE_BOOL) {
260 av_log(NULL, AV_LOG_FATAL,
261 "Requested to load an argument from file for a bool option '%s'\n",
262 po->name);
263 return AVERROR(EINVAL);
264 }
265
266 42 arg_allocated = file_read(arg);
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 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 42 arg = arg_allocated;
275 }
276
277
2/2
✓ Branch 0 taken 27833 times.
✓ Branch 1 taken 52752 times.
80585 if (po->flags & OPT_FLAG_SPEC) {
278 27833 char *p = strchr(opt, ':');
279 char *str;
280
281 27833 sol = dst;
282 27833 ret = GROW_ARRAY(sol->opt, sol->nb_opt);
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27833 times.
27833 if (ret < 0)
284 goto finish;
285
286
2/2
✓ Branch 0 taken 14516 times.
✓ Branch 1 taken 13317 times.
27833 str = av_strdup(p ? p + 1 : "");
287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27833 times.
27833 if (!str) {
288 ret = AVERROR(ENOMEM);
289 goto finish;
290 }
291 27833 sol->opt[sol->nb_opt - 1].specifier = str;
292 27833 dst = &sol->opt[sol->nb_opt - 1].u;
293 }
294
295
2/2
✓ Branch 0 taken 32621 times.
✓ Branch 1 taken 47964 times.
80585 if (po->type == OPT_TYPE_STRING) {
296 char *str;
297
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 32609 times.
32621 if (arg_allocated) {
298 12 str = arg_allocated;
299 12 arg_allocated = NULL;
300 } else
301 32609 str = av_strdup(arg);
302 32621 av_freep(dst);
303
304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32621 times.
32621 if (!str) {
305 ret = AVERROR(ENOMEM);
306 goto finish;
307 }
308
309 32621 *(char **)dst = str;
310
4/4
✓ Branch 0 taken 21704 times.
✓ Branch 1 taken 26260 times.
✓ Branch 2 taken 129 times.
✓ Branch 3 taken 21575 times.
47964 } else if (po->type == OPT_TYPE_BOOL || po->type == OPT_TYPE_INT) {
311 26389 ret = parse_number(opt, arg, OPT_TYPE_INT64, INT_MIN, INT_MAX, &num);
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26389 times.
26389 if (ret < 0)
313 goto finish;
314
315 26389 *(int *)dst = num;
316
2/2
✓ Branch 0 taken 4801 times.
✓ Branch 1 taken 16774 times.
21575 } else if (po->type == OPT_TYPE_INT64) {
317 4801 ret = parse_number(opt, arg, OPT_TYPE_INT64, INT64_MIN, INT64_MAX, &num);
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4801 times.
4801 if (ret < 0)
319 goto finish;
320
321 4801 *(int64_t *)dst = num;
322
2/2
✓ Branch 0 taken 209 times.
✓ Branch 1 taken 16565 times.
16774 } else if (po->type == OPT_TYPE_TIME) {
323 209 ret = av_parse_time(dst, arg, 1);
324
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 209 times.
209 if (ret < 0) {
325 av_log(NULL, AV_LOG_ERROR, "Invalid duration for option %s: %s\n",
326 opt, arg);
327 goto finish;
328 }
329
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 16562 times.
16565 } else if (po->type == OPT_TYPE_FLOAT) {
330 3 ret = parse_number(opt, arg, OPT_TYPE_FLOAT, -INFINITY, INFINITY, &num);
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
332 goto finish;
333
334 3 *(float *)dst = num;
335
2/2
✓ Branch 0 taken 289 times.
✓ Branch 1 taken 16273 times.
16562 } else if (po->type == OPT_TYPE_DOUBLE) {
336 289 ret = parse_number(opt, arg, OPT_TYPE_DOUBLE, -INFINITY, INFINITY, &num);
337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 289 times.
289 if (ret < 0)
338 goto finish;
339
340 289 *(double *)dst = num;
341 } else {
342 int ret;
343
344
2/4
✓ Branch 0 taken 16273 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16273 times.
16273 av_assert0(po->type == OPT_TYPE_FUNC && po->u.func_arg);
345
346 16273 ret = po->u.func_arg(optctx, opt, arg);
347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16273 times.
16273 if (ret < 0) {
348 av_log(NULL, AV_LOG_ERROR,
349 "Failed to set value '%s' for option '%s': %s\n",
350 arg, opt, av_err2str(ret));
351 goto finish;
352 }
353 }
354
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 80584 times.
80585 if (po->flags & OPT_EXIT) {
355 1 ret = AVERROR_EXIT;
356 1 goto finish;
357 }
358
359
2/2
✓ Branch 0 taken 52751 times.
✓ Branch 1 taken 27833 times.
80584 if (sol) {
360 27833 sol->type = po->type;
361
2/2
✓ Branch 0 taken 2347 times.
✓ Branch 1 taken 25486 times.
30180 sol->opt_canon = (po->flags & OPT_HAS_CANON) ?
362 2347 find_option(defs, po->u1.name_canon) : po;
363 }
364
365 52751 finish:
366 80585 av_freep(&arg_allocated);
367 80585 return ret;
368 }
369
370 8908 int parse_option(void *optctx, const char *opt, const char *arg,
371 const OptionDef *options)
372 {
373 static const OptionDef opt_avoptions = {
374 .name = "AVOption passthrough",
375 .type = OPT_TYPE_FUNC,
376 .flags = OPT_FUNC_ARG,
377 .u.func_arg = opt_default,
378 };
379
380 const OptionDef *po;
381 int ret;
382
383 8908 po = find_option(options, opt);
384
5/6
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8900 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
8908 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
385 /* handle 'no' bool option */
386 1 po = find_option(options, opt + 2);
387
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))
388 1 arg = "0";
389
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 8779 times.
8907 } else if (po->type == OPT_TYPE_BOOL)
390 128 arg = "1";
391
392
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 8901 times.
8908 if (!po->name)
393 7 po = &opt_avoptions;
394
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8908 times.
8908 if (!po->name) {
395 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
396 return AVERROR(EINVAL);
397 }
398
3/4
✓ Branch 1 taken 8678 times.
✓ Branch 2 taken 230 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8678 times.
8908 if (opt_has_arg(po) && !arg) {
399 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
400 return AVERROR(EINVAL);
401 }
402
403 8908 ret = write_option(optctx, po, opt, arg, options);
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8908 times.
8908 if (ret < 0)
405 return ret;
406
407 8908 return opt_has_arg(po);
408 }
409
410 134 int parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
411 int (*parse_arg_function)(void *, const char*))
412 {
413 const char *opt;
414 134 int optindex, handleoptions = 1, ret;
415
416 /* perform system-dependent conversions for arguments list */
417 134 prepare_app_arguments(&argc, &argv);
418
419 /* parse options */
420 134 optindex = 1;
421
2/2
✓ Branch 0 taken 611 times.
✓ Branch 1 taken 134 times.
745 while (optindex < argc) {
422 611 opt = argv[optindex++];
423
424
4/6
✓ Branch 0 taken 611 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 487 times.
✓ Branch 3 taken 124 times.
✓ Branch 4 taken 487 times.
✗ Branch 5 not taken.
611 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
425
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 487 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
487 if (opt[1] == '-' && opt[2] == '\0') {
426 handleoptions = 0;
427 continue;
428 }
429 487 opt++;
430
431
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 487 times.
487 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
432 return ret;
433 487 optindex += ret;
434 } else {
435
1/2
✓ Branch 0 taken 124 times.
✗ Branch 1 not taken.
124 if (parse_arg_function) {
436 124 ret = parse_arg_function(optctx, opt);
437
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124 times.
124 if (ret < 0)
438 return ret;
439 }
440 }
441 }
442
443 134 return 0;
444 }
445
446 20254 int parse_optgroup(void *optctx, OptionGroup *g, const OptionDef *defs)
447 {
448 int i, ret;
449
450 20254 av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
451 20254 g->group_def->name, g->arg);
452
453
2/2
✓ Branch 0 taken 71677 times.
✓ Branch 1 taken 20253 times.
91930 for (i = 0; i < g->nb_opts; i++) {
454 71677 Option *o = &g->opts[i];
455
456
2/2
✓ Branch 0 taken 40588 times.
✓ Branch 1 taken 31089 times.
71677 if (g->group_def->flags &&
457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40588 times.
40588 !(g->group_def->flags & o->opt->flags)) {
458 av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
459 "%s %s -- you are trying to apply an input option to an "
460 "output file or vice versa. Move this option before the "
461 "file it belongs to.\n", o->key, o->opt->help,
462 g->group_def->name, g->arg);
463 return AVERROR(EINVAL);
464 }
465
466 71677 av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
467 71677 o->key, o->opt->help, o->val);
468
469 71677 ret = write_option(optctx, o->opt, o->key, o->val, defs);
470
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 71676 times.
71677 if (ret < 0)
471 1 return ret;
472 }
473
474 20253 av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
475
476 20253 return 0;
477 }
478
479 34375 int locate_option(int argc, char **argv, const OptionDef *options,
480 const char *optname)
481 {
482 const OptionDef *po;
483 int i;
484
485
2/2
✓ Branch 0 taken 776744 times.
✓ Branch 1 taken 34359 times.
811103 for (i = 1; i < argc; i++) {
486 776744 const char *cur_opt = argv[i];
487
488
2/2
✓ Branch 0 taken 21452 times.
✓ Branch 1 taken 755292 times.
776744 if (*cur_opt++ != '-')
489 21452 continue;
490
491 755292 po = find_option(options, cur_opt);
492
5/6
✓ Branch 0 taken 495392 times.
✓ Branch 1 taken 259900 times.
✓ Branch 2 taken 100900 times.
✓ Branch 3 taken 394492 times.
✓ Branch 4 taken 100900 times.
✗ Branch 5 not taken.
755292 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
493 100900 po = find_option(options, cur_opt + 2);
494
495
3/4
✓ Branch 0 taken 394532 times.
✓ Branch 1 taken 360760 times.
✓ Branch 2 taken 394532 times.
✗ Branch 3 not taken.
755292 if ((!po->name && !strcmp(cur_opt, optname)) ||
496
4/4
✓ Branch 0 taken 360760 times.
✓ Branch 1 taken 394532 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 360744 times.
755292 (po->name && !strcmp(optname, po->name)))
497 16 return i;
498
499
4/4
✓ Branch 0 taken 360744 times.
✓ Branch 1 taken 394532 times.
✓ Branch 3 taken 228939 times.
✓ Branch 4 taken 131805 times.
755276 if (!po->name || opt_has_arg(po))
500 623471 i++;
501 }
502 34359 return 0;
503 }
504
505 static void dump_argument(FILE *report_file, const char *a)
506 {
507 const unsigned char *p;
508
509 for (p = a; *p; p++)
510 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
511 *p == '_' || (*p >= 'a' && *p <= 'z')))
512 break;
513 if (!*p) {
514 fputs(a, report_file);
515 return;
516 }
517 fputc('"', report_file);
518 for (p = a; *p; p++) {
519 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
520 fprintf(report_file, "\\%c", *p);
521 else if (*p < ' ' || *p > '~')
522 fprintf(report_file, "\\x%02x", *p);
523 else
524 fputc(*p, report_file);
525 }
526 fputc('"', report_file);
527 }
528
529 6875 static void check_options(const OptionDef *po)
530 {
531
2/2
✓ Branch 0 taken 1296643 times.
✓ Branch 1 taken 6875 times.
1303518 while (po->name) {
532
2/2
✓ Branch 0 taken 795438 times.
✓ Branch 1 taken 501205 times.
1296643 if (po->flags & OPT_PERFILE)
533
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 795438 times.
795438 av_assert0(po->flags & (OPT_INPUT | OPT_OUTPUT));
534
535
2/2
✓ Branch 0 taken 519016 times.
✓ Branch 1 taken 777627 times.
1296643 if (po->type == OPT_TYPE_FUNC)
536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 519016 times.
519016 av_assert0(!(po->flags & (OPT_FLAG_OFFSET | OPT_FLAG_SPEC)));
537
538 // OPT_FUNC_ARG can only be ser for OPT_TYPE_FUNC
539
3/4
✓ Branch 0 taken 777627 times.
✓ Branch 1 taken 519016 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 777627 times.
1296643 av_assert0((po->type == OPT_TYPE_FUNC) || !(po->flags & OPT_FUNC_ARG));
540
541 1296643 po++;
542 }
543 6875 }
544
545 6875 void parse_loglevel(int argc, char **argv, const OptionDef *options)
546 {
547 6875 int idx = locate_option(argc, argv, options, "loglevel");
548 char *env;
549
550 6875 check_options(options);
551
552
1/2
✓ Branch 0 taken 6875 times.
✗ Branch 1 not taken.
6875 if (!idx)
553 6875 idx = locate_option(argc, argv, options, "v");
554
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6859 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
6875 if (idx && argv[idx + 1])
555 16 opt_loglevel(NULL, "loglevel", argv[idx + 1]);
556 6875 idx = locate_option(argc, argv, options, "report");
557 6875 env = getenv_utf8("FFREPORT");
558
2/4
✓ Branch 0 taken 6875 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6875 times.
6875 if (env || idx) {
559 FILE *report_file = NULL;
560 init_report(env, &report_file);
561 if (report_file) {
562 int i;
563 fprintf(report_file, "Command line:\n");
564 for (i = 0; i < argc; i++) {
565 dump_argument(report_file, argv[i]);
566 fputc(i < argc - 1 ? ' ' : '\n', report_file);
567 }
568 fflush(report_file);
569 }
570 }
571 6875 freeenv_utf8(env);
572 6875 idx = locate_option(argc, argv, options, "hide_banner");
573
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6875 times.
6875 if (idx)
574 hide_banner = 1;
575 6875 }
576
577 243825 static const AVOption *opt_find(void *obj, const char *name, const char *unit,
578 int opt_flags, int search_flags)
579 {
580 243825 const AVOption *o = av_opt_find(obj, name, unit, opt_flags, search_flags);
581
4/4
✓ Branch 0 taken 69635 times.
✓ Branch 1 taken 174190 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 69633 times.
243825 if(o && !o->flags)
582 2 return NULL;
583 243823 return o;
584 }
585
586 #define FLAGS (o->type == AV_OPT_TYPE_FLAGS && (arg[0]=='-' || arg[0]=='+')) ? AV_DICT_APPEND : 0
587 89732 int opt_default(void *optctx, const char *opt, const char *arg)
588 {
589 const AVOption *o;
590 89732 int consumed = 0;
591 char opt_stripped[128];
592 const char *p;
593 89732 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
594 #if CONFIG_SWSCALE
595 89732 const AVClass *sc = sws_get_class();
596 #endif
597 #if CONFIG_SWRESAMPLE
598 89732 const AVClass *swr_class = swr_get_class();
599 #endif
600
601
2/4
✓ Branch 0 taken 89732 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 89732 times.
89732 if (!strcmp(opt, "debug") || !strcmp(opt, "fdebug"))
602 av_log_set_level(AV_LOG_DEBUG);
603
604
2/2
✓ Branch 0 taken 89720 times.
✓ Branch 1 taken 12 times.
89732 if (!(p = strchr(opt, ':')))
605 89720 p = opt + strlen(opt);
606 89732 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
607
608
2/2
✓ Branch 1 taken 44112 times.
✓ Branch 2 taken 45620 times.
89732 if ((o = opt_find(&cc, opt_stripped, NULL, 0,
609 44112 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
610
8/8
✓ Branch 0 taken 44109 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 44097 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 12014 times.
✓ Branch 5 taken 32083 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 12028 times.
56141 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
611 12029 (o = opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) {
612
5/6
✓ Branch 0 taken 18561 times.
✓ Branch 1 taken 27060 times.
✓ Branch 2 taken 18561 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11960 times.
✓ Branch 5 taken 6601 times.
45621 av_dict_set(&codec_opts, opt, arg, FLAGS);
613 45621 consumed = 1;
614 }
615
2/2
✓ Branch 1 taken 12022 times.
✓ Branch 2 taken 77710 times.
89732 if ((o = opt_find(&fc, opt, NULL, 0,
616 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
617
5/6
✓ Branch 0 taken 11815 times.
✓ Branch 1 taken 207 times.
✓ Branch 2 taken 11815 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11807 times.
✓ Branch 5 taken 8 times.
12022 av_dict_set(&format_opts, opt, arg, FLAGS);
618
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 11950 times.
12022 if (consumed)
619 72 av_log(NULL, AV_LOG_VERBOSE, "Routing option %s to both codec and muxer layer\n", opt);
620 12022 consumed = 1;
621 }
622 #if CONFIG_SWSCALE
623
4/4
✓ Branch 0 taken 32161 times.
✓ Branch 1 taken 57571 times.
✓ Branch 3 taken 11990 times.
✓ Branch 4 taken 20171 times.
89732 if (!consumed && (o = opt_find(&sc, opt, NULL, 0,
624 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
625
2/4
✓ Branch 0 taken 11990 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11990 times.
✗ Branch 3 not taken.
11990 if (!strcmp(opt, "srcw") || !strcmp(opt, "srch") ||
626
2/4
✓ Branch 0 taken 11990 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11990 times.
✗ Branch 3 not taken.
11990 !strcmp(opt, "dstw") || !strcmp(opt, "dsth") ||
627
2/4
✓ Branch 0 taken 11990 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11990 times.
11990 !strcmp(opt, "src_format") || !strcmp(opt, "dst_format")) {
628 av_log(NULL, AV_LOG_ERROR, "Directly using swscale dimensions/format options is not supported, please use the -s or -pix_fmt options\n");
629 return AVERROR(EINVAL);
630 }
631
4/6
✓ Branch 0 taken 11990 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11990 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11805 times.
✓ Branch 5 taken 185 times.
11990 av_dict_set(&sws_dict, opt, arg, FLAGS);
632
633 11990 consumed = 1;
634 }
635 #else
636 if (!consumed && !strcmp(opt, "sws_flags")) {
637 av_log(NULL, AV_LOG_WARNING, "Ignoring %s %s, due to disabled swscale\n", opt, arg);
638 consumed = 1;
639 }
640 #endif
641 #if CONFIG_SWRESAMPLE
642
3/4
✓ Branch 0 taken 20171 times.
✓ Branch 1 taken 69561 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20171 times.
89732 if (!consumed && (o=opt_find(&swr_class, opt, NULL, 0,
643 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
644 av_dict_set(&swr_opts, opt, arg, FLAGS);
645 consumed = 1;
646 }
647 #endif
648
649
2/2
✓ Branch 0 taken 69561 times.
✓ Branch 1 taken 20171 times.
89732 if (consumed)
650 69561 return 0;
651 20171 return AVERROR_OPTION_NOT_FOUND;
652 }
653
654 /*
655 * Check whether given option is a group separator.
656 *
657 * @return index of the group definition that matched or -1 if none
658 */
659 148002 static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
660 const char *opt)
661 {
662 int i;
663
664
2/2
✓ Branch 0 taken 296004 times.
✓ Branch 1 taken 141231 times.
437235 for (i = 0; i < nb_groups; i++) {
665 296004 const OptionGroupDef *p = &groups[i];
666
4/4
✓ Branch 0 taken 148002 times.
✓ Branch 1 taken 148002 times.
✓ Branch 2 taken 6771 times.
✓ Branch 3 taken 141231 times.
296004 if (p->sep && !strcmp(p->sep, opt))
667 6771 return i;
668 }
669
670 141231 return -1;
671 }
672
673 /*
674 * Finish parsing an option group.
675 *
676 * @param group_idx which group definition should this group belong to
677 * @param arg argument of the group delimiting option
678 */
679 13513 static int finish_group(OptionParseContext *octx, int group_idx,
680 const char *arg)
681 {
682 13513 OptionGroupList *l = &octx->groups[group_idx];
683 OptionGroup *g;
684 int ret;
685
686 13513 ret = GROW_ARRAY(l->groups, l->nb_groups);
687
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13513 times.
13513 if (ret < 0)
688 return ret;
689
690 13513 g = &l->groups[l->nb_groups - 1];
691
692 13513 *g = octx->cur_group;
693 13513 g->arg = arg;
694 13513 g->group_def = l->group_def;
695 13513 g->sws_dict = sws_dict;
696 13513 g->swr_opts = swr_opts;
697 13513 g->codec_opts = codec_opts;
698 13513 g->format_opts = format_opts;
699
700 13513 codec_opts = NULL;
701 13513 format_opts = NULL;
702 13513 sws_dict = NULL;
703 13513 swr_opts = NULL;
704
705 13513 memset(&octx->cur_group, 0, sizeof(octx->cur_group));
706
707 13513 return ret;
708 }
709
710 /*
711 * Add an option instance to currently parsed group.
712 */
713 71677 static int add_opt(OptionParseContext *octx, const OptionDef *opt,
714 const char *key, const char *val)
715 {
716 71677 int global = !(opt->flags & OPT_PERFILE);
717
2/2
✓ Branch 0 taken 31089 times.
✓ Branch 1 taken 40588 times.
71677 OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
718 int ret;
719
720 71677 ret = GROW_ARRAY(g->opts, g->nb_opts);
721
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71677 times.
71677 if (ret < 0)
722 return ret;
723
724 71677 g->opts[g->nb_opts - 1].opt = opt;
725 71677 g->opts[g->nb_opts - 1].key = key;
726 71677 g->opts[g->nb_opts - 1].val = val;
727
728 71677 return 0;
729 }
730
731 6741 static int init_parse_context(OptionParseContext *octx,
732 const OptionGroupDef *groups, int nb_groups)
733 {
734 static const OptionGroupDef global_group = { "global" };
735 int i;
736
737 6741 memset(octx, 0, sizeof(*octx));
738
739 6741 octx->groups = av_calloc(nb_groups, sizeof(*octx->groups));
740
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6741 times.
6741 if (!octx->groups)
741 return AVERROR(ENOMEM);
742 6741 octx->nb_groups = nb_groups;
743
744
2/2
✓ Branch 0 taken 13482 times.
✓ Branch 1 taken 6741 times.
20223 for (i = 0; i < octx->nb_groups; i++)
745 13482 octx->groups[i].group_def = &groups[i];
746
747 6741 octx->global_opts.group_def = &global_group;
748 6741 octx->global_opts.arg = "";
749
750 6741 return 0;
751 }
752
753 6741 void uninit_parse_context(OptionParseContext *octx)
754 {
755 int i, j;
756
757
2/2
✓ Branch 0 taken 13482 times.
✓ Branch 1 taken 6741 times.
20223 for (i = 0; i < octx->nb_groups; i++) {
758 13482 OptionGroupList *l = &octx->groups[i];
759
760
2/2
✓ Branch 0 taken 13513 times.
✓ Branch 1 taken 13482 times.
26995 for (j = 0; j < l->nb_groups; j++) {
761 13513 av_freep(&l->groups[j].opts);
762 13513 av_dict_free(&l->groups[j].codec_opts);
763 13513 av_dict_free(&l->groups[j].format_opts);
764
765 13513 av_dict_free(&l->groups[j].sws_dict);
766 13513 av_dict_free(&l->groups[j].swr_opts);
767 }
768 13482 av_freep(&l->groups);
769 }
770 6741 av_freep(&octx->groups);
771
772 6741 av_freep(&octx->cur_group.opts);
773 6741 av_freep(&octx->global_opts.opts);
774
775 6741 uninit_opts();
776 6741 }
777
778 6741 int split_commandline(OptionParseContext *octx, int argc, char *argv[],
779 const OptionDef *options,
780 const OptionGroupDef *groups, int nb_groups)
781 {
782 int ret;
783 6741 int optindex = 1;
784 6741 int dashdash = -2;
785
786 /* perform system-dependent conversions for arguments list */
787 6741 prepare_app_arguments(&argc, &argv);
788
789 6741 ret = init_parse_context(octx, groups, nb_groups);
790
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6741 times.
6741 if (ret < 0)
791 return ret;
792
793 6741 av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
794
795
2/2
✓ Branch 0 taken 154744 times.
✓ Branch 1 taken 6741 times.
161485 while (optindex < argc) {
796 154744 const char *opt = argv[optindex++], *arg;
797 const OptionDef *po;
798 int ret;
799
800 154744 av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
801
802
3/6
✓ Branch 0 taken 150578 times.
✓ Branch 1 taken 4166 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 150578 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
154744 if (opt[0] == '-' && opt[1] == '-' && !opt[2]) {
803 dashdash = optindex;
804 continue;
805 }
806 /* unnamed group separators, e.g. output filename */
807
5/6
✓ Branch 0 taken 150578 times.
✓ Branch 1 taken 4166 times.
✓ Branch 2 taken 148002 times.
✓ Branch 3 taken 2576 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 148002 times.
154744 if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) {
808 6742 ret = finish_group(octx, 0, opt);
809
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6742 times.
6742 if (ret < 0)
810 return ret;
811
812 6742 av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
813 6742 continue;
814 }
815 148002 opt++;
816
817 #define GET_ARG(arg) \
818 do { \
819 arg = argv[optindex++]; \
820 if (!arg) { \
821 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
822 return AVERROR(EINVAL); \
823 } \
824 } while (0)
825
826 /* named group separators, e.g. -i */
827
2/2
✓ Branch 1 taken 6771 times.
✓ Branch 2 taken 141231 times.
148002 if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
828
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6771 times.
6771 GET_ARG(arg);
829 6771 ret = finish_group(octx, ret, arg);
830
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6771 times.
6771 if (ret < 0)
831 return ret;
832
833 6771 av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
834 6771 groups[ret].name, arg);
835 6771 continue;
836 }
837
838 /* normal options */
839 141231 po = find_option(options, opt);
840
2/2
✓ Branch 0 taken 51506 times.
✓ Branch 1 taken 89725 times.
141231 if (po->name) {
841
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 51505 times.
51506 if (po->flags & OPT_EXIT) {
842 /* optional argument, e.g. -h */
843 1 arg = argv[optindex++];
844
2/2
✓ Branch 1 taken 45545 times.
✓ Branch 2 taken 5960 times.
51505 } else if (opt_has_arg(po)) {
845
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45545 times.
45545 GET_ARG(arg);
846 } else {
847 5960 arg = "1";
848 }
849
850 51506 ret = add_opt(octx, po, opt, arg);
851
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51506 times.
51506 if (ret < 0)
852 return ret;
853
854 51506 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
855 51506 "argument '%s'.\n", po->name, po->help, arg);
856 51506 continue;
857 }
858
859 /* AVOptions */
860
1/2
✓ Branch 0 taken 89725 times.
✗ Branch 1 not taken.
89725 if (argv[optindex]) {
861 89725 ret = opt_default(NULL, opt, argv[optindex]);
862
2/2
✓ Branch 0 taken 69554 times.
✓ Branch 1 taken 20171 times.
89725 if (ret >= 0) {
863 69554 av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
864 69554 "argument '%s'.\n", opt, argv[optindex]);
865 69554 optindex++;
866 69554 continue;
867
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20171 times.
20171 } else if (ret != AVERROR_OPTION_NOT_FOUND) {
868 av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
869 "with argument '%s'.\n", opt, argv[optindex]);
870 return ret;
871 }
872 }
873
874 /* boolean -nofoo options */
875
3/6
✓ Branch 0 taken 20171 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20171 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20171 times.
✗ Branch 5 not taken.
40342 if (opt[0] == 'n' && opt[1] == 'o' &&
876 20171 (po = find_option(options, opt + 2)) &&
877
2/4
✓ Branch 0 taken 20171 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20171 times.
✗ Branch 3 not taken.
20171 po->name && po->type == OPT_TYPE_BOOL) {
878 20171 ret = add_opt(octx, po, opt, "0");
879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20171 times.
20171 if (ret < 0)
880 return ret;
881
882 20171 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
883 20171 "argument 0.\n", po->name, po->help);
884 20171 continue;
885 }
886
887 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
888 return AVERROR_OPTION_NOT_FOUND;
889 }
890
891
3/6
✓ Branch 0 taken 6741 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6741 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6741 times.
6741 if (octx->cur_group.nb_opts || codec_opts || format_opts)
892 av_log(NULL, AV_LOG_WARNING, "Trailing option(s) found in the "
893 "command: may be ignored.\n");
894
895 6741 av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
896
897 6741 return 0;
898 }
899
900 void print_error(const char *filename, int err)
901 {
902 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, av_err2str(err));
903 }
904
905 int read_yesno(void)
906 {
907 int c = getchar();
908 int yesno = (av_toupper(c) == 'Y');
909
910 while (c != '\n' && c != EOF)
911 c = getchar();
912
913 return yesno;
914 }
915
916 FILE *get_preset_file(char *filename, size_t filename_size,
917 const char *preset_name, int is_path,
918 const char *codec_name)
919 {
920 FILE *f = NULL;
921 int i;
922 #if HAVE_GETMODULEHANDLE && defined(_WIN32)
923 char *datadir = NULL;
924 #endif
925 char *env_home = getenv_utf8("HOME");
926 char *env_ffmpeg_datadir = getenv_utf8("FFMPEG_DATADIR");
927 const char *base[3] = { env_ffmpeg_datadir,
928 env_home, /* index=1(HOME) is special: search in a .ffmpeg subfolder */
929 FFMPEG_DATADIR, };
930
931 if (is_path) {
932 av_strlcpy(filename, preset_name, filename_size);
933 f = fopen_utf8(filename, "r");
934 } else {
935 #if HAVE_GETMODULEHANDLE && defined(_WIN32)
936 wchar_t *datadir_w = get_module_filename(NULL);
937 base[2] = NULL;
938
939 if (wchartoutf8(datadir_w, &datadir))
940 datadir = NULL;
941 av_free(datadir_w);
942
943 if (datadir)
944 {
945 char *ls;
946 for (ls = datadir; *ls; ls++)
947 if (*ls == '\\') *ls = '/';
948
949 if (ls = strrchr(datadir, '/'))
950 {
951 ptrdiff_t datadir_len = ls - datadir;
952 size_t desired_size = datadir_len + strlen("/ffpresets") + 1;
953 char *new_datadir = av_realloc_array(
954 datadir, desired_size, sizeof *datadir);
955 if (new_datadir) {
956 datadir = new_datadir;
957 datadir[datadir_len] = 0;
958 strncat(datadir, "/ffpresets", desired_size - 1 - datadir_len);
959 base[2] = datadir;
960 }
961 }
962 }
963 #endif
964 for (i = 0; i < 3 && !f; i++) {
965 if (!base[i])
966 continue;
967 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
968 i != 1 ? "" : "/.ffmpeg", preset_name);
969 f = fopen_utf8(filename, "r");
970 if (!f && codec_name) {
971 snprintf(filename, filename_size,
972 "%s%s/%s-%s.ffpreset",
973 base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
974 preset_name);
975 f = fopen_utf8(filename, "r");
976 }
977 }
978 }
979
980 #if HAVE_GETMODULEHANDLE && defined(_WIN32)
981 av_free(datadir);
982 #endif
983 freeenv_utf8(env_ffmpeg_datadir);
984 freeenv_utf8(env_home);
985 return f;
986 }
987
988 29515 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
989 {
990 29515 int ret = avformat_match_stream_specifier(s, st, spec);
991
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29515 times.
29515 if (ret < 0)
992 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
993 29515 return ret;
994 }
995
996 21865 int filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_id,
997 AVFormatContext *s, AVStream *st, const AVCodec *codec,
998 AVDictionary **dst)
999 {
1000 21865 AVDictionary *ret = NULL;
1001 21865 const AVDictionaryEntry *t = NULL;
1002 43730 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1003
2/2
✓ Branch 0 taken 7051 times.
✓ Branch 1 taken 14814 times.
21865 : AV_OPT_FLAG_DECODING_PARAM;
1004 21865 char prefix = 0;
1005 21865 const AVClass *cc = avcodec_get_class();
1006
1007
2/2
✓ Branch 0 taken 8013 times.
✓ Branch 1 taken 13852 times.
21865 if (!codec)
1008 8013 codec = s->oformat ? avcodec_find_encoder(codec_id)
1009
2/2
✓ Branch 0 taken 596 times.
✓ Branch 1 taken 7417 times.
8013 : avcodec_find_decoder(codec_id);
1010
1011
4/4
✓ Branch 0 taken 16716 times.
✓ Branch 1 taken 4767 times.
✓ Branch 2 taken 243 times.
✓ Branch 3 taken 139 times.
21865 switch (st->codecpar->codec_type) {
1012 16716 case AVMEDIA_TYPE_VIDEO:
1013 16716 prefix = 'v';
1014 16716 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1015 16716 break;
1016 4767 case AVMEDIA_TYPE_AUDIO:
1017 4767 prefix = 'a';
1018 4767 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1019 4767 break;
1020 243 case AVMEDIA_TYPE_SUBTITLE:
1021 243 prefix = 's';
1022 243 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1023 243 break;
1024 }
1025
1026
2/2
✓ Branch 1 taken 63316 times.
✓ Branch 2 taken 21865 times.
85181 while (t = av_dict_iterate(opts, t)) {
1027 const AVClass *priv_class;
1028 63316 char *p = strchr(t->key, ':');
1029
1030 /* check stream specification in opt name */
1031
2/2
✓ Branch 0 taken 286 times.
✓ Branch 1 taken 63030 times.
63316 if (p) {
1032 286 int err = check_stream_specifier(s, st, p + 1);
1033
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 286 times.
286 if (err < 0) {
1034 av_dict_free(&ret);
1035 return err;
1036
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 209 times.
286 } else if (!err)
1037 77 continue;
1038
1039 209 *p = 0;
1040 }
1041
1042
4/4
✓ Branch 1 taken 2499 times.
✓ Branch 2 taken 60740 times.
✓ Branch 3 taken 2126 times.
✓ Branch 4 taken 373 times.
63239 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1043 2126 !codec ||
1044
4/4
✓ Branch 0 taken 847 times.
✓ Branch 1 taken 1279 times.
✓ Branch 2 taken 370 times.
✓ Branch 3 taken 477 times.
2973 ((priv_class = codec->priv_class) &&
1045 847 av_opt_find(&priv_class, t->key, NULL, flags,
1046 AV_OPT_SEARCH_FAKE_OBJ)))
1047 61483 av_dict_set(&ret, t->key, t->value, 0);
1048
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1753 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
1759 else if (t->key[0] == prefix &&
1049 3 av_opt_find(&cc, t->key + 1, NULL, flags,
1050 AV_OPT_SEARCH_FAKE_OBJ))
1051 1 av_dict_set(&ret, t->key + 1, t->value, 0);
1052
1053
2/2
✓ Branch 0 taken 209 times.
✓ Branch 1 taken 63030 times.
63239 if (p)
1054 209 *p = ':';
1055 }
1056
1057 21865 *dst = ret;
1058 21865 return 0;
1059 }
1060
1061 6903 int setup_find_stream_info_opts(AVFormatContext *s,
1062 AVDictionary *codec_opts,
1063 AVDictionary ***dst)
1064 {
1065 int ret;
1066 AVDictionary **opts;
1067
1068 6903 *dst = NULL;
1069
1070
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 6819 times.
6903 if (!s->nb_streams)
1071 84 return 0;
1072
1073 6819 opts = av_calloc(s->nb_streams, sizeof(*opts));
1074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6819 times.
6819 if (!opts)
1075 return AVERROR(ENOMEM);
1076
1077
2/2
✓ Branch 0 taken 7346 times.
✓ Branch 1 taken 6819 times.
14165 for (int i = 0; i < s->nb_streams; i++) {
1078 7346 ret = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id,
1079 7346 s, s->streams[i], NULL, &opts[i]);
1080
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7346 times.
7346 if (ret < 0)
1081 goto fail;
1082 }
1083 6819 *dst = opts;
1084 6819 return 0;
1085 fail:
1086 for (int i = 0; i < s->nb_streams; i++)
1087 av_dict_free(&opts[i]);
1088 av_freep(&opts);
1089 return ret;
1090 }
1091
1092 193115 int grow_array(void **array, int elem_size, int *size, int new_size)
1093 {
1094
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 193115 times.
193115 if (new_size >= INT_MAX / elem_size) {
1095 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1096 return AVERROR(ERANGE);
1097 }
1098
1/2
✓ Branch 0 taken 193115 times.
✗ Branch 1 not taken.
193115 if (*size < new_size) {
1099 193115 uint8_t *tmp = av_realloc_array(*array, new_size, elem_size);
1100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 193115 times.
193115 if (!tmp)
1101 return AVERROR(ENOMEM);
1102 193115 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
1103 193115 *size = new_size;
1104 193115 *array = tmp;
1105 193115 return 0;
1106 }
1107 return 0;
1108 }
1109
1110 46996 void *allocate_array_elem(void *ptr, size_t elem_size, int *nb_elems)
1111 {
1112 void *new_elem;
1113
1114
2/4
✓ Branch 1 taken 46996 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 46996 times.
93992 if (!(new_elem = av_mallocz(elem_size)) ||
1115 46996 av_dynarray_add_nofree(ptr, nb_elems, new_elem) < 0)
1116 return NULL;
1117 46996 return new_elem;
1118 }
1119
1120 5194 double get_rotation(const int32_t *displaymatrix)
1121 {
1122 5194 double theta = 0;
1123
1/2
✓ Branch 0 taken 5194 times.
✗ Branch 1 not taken.
5194 if (displaymatrix)
1124 5194 theta = -round(av_display_rotation_get(displaymatrix));
1125
1126 5194 theta -= 360*floor(theta/360 + 0.9/360);
1127
1128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5194 times.
5194 if (fabs(theta - 90*round(theta/90)) > 2)
1129 av_log(NULL, AV_LOG_WARNING, "Odd rotation angle.\n"
1130 "If you want to help, upload a sample "
1131 "of this file to https://streams.videolan.org/upload/ "
1132 "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)");
1133
1134 5194 return theta;
1135 }
1136
1137 /* read file contents into a string */
1138 46 char *file_read(const char *filename)
1139 {
1140 46 AVIOContext *pb = NULL;
1141 46 int ret = avio_open(&pb, filename, AVIO_FLAG_READ);
1142 AVBPrint bprint;
1143 char *str;
1144
1145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (ret < 0) {
1146 av_log(NULL, AV_LOG_ERROR, "Error opening file %s.\n", filename);
1147 return NULL;
1148 }
1149
1150 46 av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
1151 46 ret = avio_read_to_bprint(pb, &bprint, SIZE_MAX);
1152 46 avio_closep(&pb);
1153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (ret < 0) {
1154 av_bprint_finalize(&bprint, NULL);
1155 return NULL;
1156 }
1157 46 ret = av_bprint_finalize(&bprint, &str);
1158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (ret < 0)
1159 return NULL;
1160 46 return str;
1161 }
1162