FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/cmdutils.c
Date: 2024-07-26 21:54:09
Exec Total Coverage
Lines: 423 584 72.4%
Functions: 30 36 83.3%
Branches: 291 472 61.7%

Line Branch Exec Source
1 /*
2 * Various utilities for command line tools
3 * Copyright (c) 2000-2003 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <string.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <math.h>
27
28 /* Include only the enabled headers since some compilers (namely, Sun
29 Studio) will not omit unused inline functions and create undefined
30 references to libraries that are not being built. */
31
32 #include "config.h"
33 #include "compat/va_copy.h"
34 #include "libavformat/avformat.h"
35 #include "libswscale/swscale.h"
36 #include "libswresample/swresample.h"
37 #include "libavutil/avassert.h"
38 #include "libavutil/avstring.h"
39 #include "libavutil/bprint.h"
40 #include "libavutil/display.h"
41 #include "libavutil/getenv_utf8.h"
42 #include "libavutil/libm.h"
43 #include "libavutil/mem.h"
44 #include "libavutil/parseutils.h"
45 #include "libavutil/eval.h"
46 #include "libavutil/dict.h"
47 #include "libavutil/opt.h"
48 #include "cmdutils.h"
49 #include "fopen_utf8.h"
50 #include "opt_common.h"
51 #ifdef _WIN32
52 #include <windows.h>
53 #include "compat/w32dlfcn.h"
54 #endif
55
56 AVDictionary *sws_dict;
57 AVDictionary *swr_opts;
58 AVDictionary *format_opts, *codec_opts;
59
60 int hide_banner = 0;
61
62 13708 void uninit_opts(void)
63 {
64 13708 av_dict_free(&swr_opts);
65 13708 av_dict_free(&sws_dict);
66 13708 av_dict_free(&format_opts);
67 13708 av_dict_free(&codec_opts);
68 13708 }
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 6929 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 6929 }
83
84 31654 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 31654 double d = av_strtod(numstr, &tail);
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31654 times.
31654 if (*tail)
91 error = "Expected number for %s but found: %s\n";
92
2/4
✓ Branch 0 taken 31654 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 31654 times.
31654 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 31361 times.
✓ Branch 1 taken 293 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 31361 times.
31654 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 31654 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
31654 else if (type == OPT_TYPE_INT && (int)d != d)
97 error = "Expected int for %s but found %s\n";
98 else {
99 31654 *dst = d;
100 31654 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 1035043 static const OptionDef *find_option(const OptionDef *po, const char *name)
154 {
155
2/2
✓ Branch 0 taken 318 times.
✓ Branch 1 taken 1034725 times.
1035043 if (*name == '/')
156 318 name++;
157
158
2/2
✓ Branch 0 taken 148362083 times.
✓ Branch 1 taken 587888 times.
148949971 while (po->name) {
159 const char *end;
160
6/6
✓ Branch 1 taken 1049268 times.
✓ Branch 2 taken 147312815 times.
✓ Branch 3 taken 648749 times.
✓ Branch 4 taken 400519 times.
✓ Branch 5 taken 602113 times.
✓ Branch 6 taken 46636 times.
148362083 if (av_strstart(name, po->name, &end) && (!*end || *end == ':'))
161 break;
162 147914928 po++;
163 }
164 1035043 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 6929 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
226 {
227 /* nothing to do */
228 6929 }
229 #endif /* HAVE_COMMANDLINETOARGVW */
230
231 433495 static int opt_has_arg(const OptionDef *o)
232 {
233
2/2
✓ Branch 0 taken 138433 times.
✓ Branch 1 taken 295062 times.
433495 if (o->type == OPT_TYPE_BOOL)
234 138433 return 0;
235
2/2
✓ Branch 0 taken 99117 times.
✓ Branch 1 taken 195945 times.
295062 if (o->type == OPT_TYPE_FUNC)
236 99117 return !!(o->flags & OPT_FUNC_ARG);
237 195945 return 1;
238 }
239
240 81232 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 162464 void *dst = po->flags & OPT_FLAG_OFFSET ?
246
2/2
✓ Branch 0 taken 40415 times.
✓ Branch 1 taken 40817 times.
81232 (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
247 81232 char *arg_allocated = NULL;
248
249 81232 SpecifierOptList *sol = NULL;
250 double num;
251 81232 int ret = 0;
252
253
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 81179 times.
81232 if (*opt == '/') {
254 53 opt++;
255
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if (po->type == OPT_TYPE_BOOL) {
257 av_log(NULL, AV_LOG_FATAL,
258 "Requested to load an argument from file for a bool option '%s'\n",
259 po->name);
260 return AVERROR(EINVAL);
261 }
262
263 53 arg_allocated = file_read(arg);
264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if (!arg_allocated) {
265 av_log(NULL, AV_LOG_FATAL,
266 "Error reading the value for option '%s' from file: %s\n",
267 opt, arg);
268 return AVERROR(EINVAL);
269 }
270
271 53 arg = arg_allocated;
272 }
273
274
2/2
✓ Branch 0 taken 28040 times.
✓ Branch 1 taken 53192 times.
81232 if (po->flags & OPT_FLAG_SPEC) {
275 28040 char *p = strchr(opt, ':');
276 char *str;
277
278 28040 sol = dst;
279 28040 ret = GROW_ARRAY(sol->opt, sol->nb_opt);
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28040 times.
28040 if (ret < 0)
281 goto finish;
282
283
2/2
✓ Branch 0 taken 14662 times.
✓ Branch 1 taken 13378 times.
28040 str = av_strdup(p ? p + 1 : "");
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28040 times.
28040 if (!str) {
285 ret = AVERROR(ENOMEM);
286 goto finish;
287 }
288 28040 sol->opt[sol->nb_opt - 1].specifier = str;
289 28040 dst = &sol->opt[sol->nb_opt - 1].u;
290 }
291
292
2/2
✓ Branch 0 taken 32878 times.
✓ Branch 1 taken 48354 times.
81232 if (po->type == OPT_TYPE_STRING) {
293 char *str;
294
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 32858 times.
32878 if (arg_allocated) {
295 20 str = arg_allocated;
296 20 arg_allocated = NULL;
297 } else
298 32858 str = av_strdup(arg);
299 32878 av_freep(dst);
300
301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32878 times.
32878 if (!str) {
302 ret = AVERROR(ENOMEM);
303 goto finish;
304 }
305
306 32878 *(char **)dst = str;
307
4/4
✓ Branch 0 taken 21924 times.
✓ Branch 1 taken 26430 times.
✓ Branch 2 taken 129 times.
✓ Branch 3 taken 21795 times.
48354 } else if (po->type == OPT_TYPE_BOOL || po->type == OPT_TYPE_INT) {
308 26559 ret = parse_number(opt, arg, OPT_TYPE_INT64, INT_MIN, INT_MAX, &num);
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26559 times.
26559 if (ret < 0)
310 goto finish;
311
312 26559 *(int *)dst = num;
313
2/2
✓ Branch 0 taken 4802 times.
✓ Branch 1 taken 16993 times.
21795 } else if (po->type == OPT_TYPE_INT64) {
314 4802 ret = parse_number(opt, arg, OPT_TYPE_INT64, INT64_MIN, (double)INT64_MAX, &num);
315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4802 times.
4802 if (ret < 0)
316 goto finish;
317
318 4802 *(int64_t *)dst = num;
319
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 16777 times.
16993 } else if (po->type == OPT_TYPE_TIME) {
320 216 ret = av_parse_time(dst, arg, 1);
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
216 if (ret < 0) {
322 av_log(NULL, AV_LOG_ERROR, "Invalid duration for option %s: %s\n",
323 opt, arg);
324 goto finish;
325 }
326
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 16774 times.
16777 } else if (po->type == OPT_TYPE_FLOAT) {
327 3 ret = parse_number(opt, arg, OPT_TYPE_FLOAT, -INFINITY, INFINITY, &num);
328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
329 goto finish;
330
331 3 *(float *)dst = num;
332
2/2
✓ Branch 0 taken 290 times.
✓ Branch 1 taken 16484 times.
16774 } else if (po->type == OPT_TYPE_DOUBLE) {
333 290 ret = parse_number(opt, arg, OPT_TYPE_DOUBLE, -INFINITY, INFINITY, &num);
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 290 times.
290 if (ret < 0)
335 goto finish;
336
337 290 *(double *)dst = num;
338 } else {
339
2/4
✓ Branch 0 taken 16484 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16484 times.
16484 av_assert0(po->type == OPT_TYPE_FUNC && po->u.func_arg);
340
341 16484 ret = po->u.func_arg(optctx, opt, arg);
342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16484 times.
16484 if (ret < 0) {
343 av_log(NULL, AV_LOG_ERROR,
344 "Failed to set value '%s' for option '%s': %s\n",
345 arg, opt, av_err2str(ret));
346 goto finish;
347 }
348 }
349
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 81231 times.
81232 if (po->flags & OPT_EXIT) {
350 1 ret = AVERROR_EXIT;
351 1 goto finish;
352 }
353
354
2/2
✓ Branch 0 taken 53191 times.
✓ Branch 1 taken 28040 times.
81231 if (sol) {
355 28040 sol->type = po->type;
356
2/2
✓ Branch 0 taken 2406 times.
✓ Branch 1 taken 25634 times.
30446 sol->opt_canon = (po->flags & OPT_HAS_CANON) ?
357 2406 find_option(defs, po->u1.name_canon) : po;
358 }
359
360 53191 finish:
361 81232 av_freep(&arg_allocated);
362 81232 return ret;
363 }
364
365 9040 int parse_option(void *optctx, const char *opt, const char *arg,
366 const OptionDef *options)
367 {
368 static const OptionDef opt_avoptions = {
369 .name = "AVOption passthrough",
370 .type = OPT_TYPE_FUNC,
371 .flags = OPT_FUNC_ARG,
372 .u.func_arg = opt_default,
373 };
374
375 const OptionDef *po;
376 int ret;
377
378 9040 po = find_option(options, opt);
379
5/6
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9031 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
9040 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
380 /* handle 'no' bool option */
381 1 po = find_option(options, opt + 2);
382
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if ((po->name && po->type == OPT_TYPE_BOOL))
383 1 arg = "0";
384
2/2
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 8897 times.
9039 } else if (po->type == OPT_TYPE_BOOL)
385 142 arg = "1";
386
387
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 9032 times.
9040 if (!po->name)
388 8 po = &opt_avoptions;
389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9040 times.
9040 if (!po->name) {
390 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
391 return AVERROR(EINVAL);
392 }
393
3/4
✓ Branch 1 taken 8791 times.
✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8791 times.
9040 if (opt_has_arg(po) && !arg) {
394 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
395 return AVERROR(EINVAL);
396 }
397
398 9040 ret = write_option(optctx, po, opt, arg, options);
399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9040 times.
9040 if (ret < 0)
400 return ret;
401
402 9040 return opt_has_arg(po);
403 }
404
405 150 int parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
406 int (*parse_arg_function)(void *, const char*))
407 {
408 const char *opt;
409 150 int optindex, handleoptions = 1, ret;
410
411 /* perform system-dependent conversions for arguments list */
412 150 prepare_app_arguments(&argc, &argv);
413
414 /* parse options */
415 150 optindex = 1;
416
2/2
✓ Branch 0 taken 666 times.
✓ Branch 1 taken 150 times.
816 while (optindex < argc) {
417 666 opt = argv[optindex++];
418
419
4/6
✓ Branch 0 taken 666 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 526 times.
✓ Branch 3 taken 140 times.
✓ Branch 4 taken 526 times.
✗ Branch 5 not taken.
666 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
420
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 526 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
526 if (opt[1] == '-' && opt[2] == '\0') {
421 handleoptions = 0;
422 continue;
423 }
424 526 opt++;
425
426
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 526 times.
526 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
427 return ret;
428 526 optindex += ret;
429 } else {
430
1/2
✓ Branch 0 taken 140 times.
✗ Branch 1 not taken.
140 if (parse_arg_function) {
431 140 ret = parse_arg_function(optctx, opt);
432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 140 times.
140 if (ret < 0)
433 return ret;
434 }
435 }
436 }
437
438 150 return 0;
439 }
440
441 20369 int parse_optgroup(void *optctx, OptionGroup *g, const OptionDef *defs)
442 {
443 int i, ret;
444
445 20369 av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
446 20369 g->group_def->name, g->arg);
447
448
2/2
✓ Branch 0 taken 72192 times.
✓ Branch 1 taken 20368 times.
92560 for (i = 0; i < g->nb_opts; i++) {
449 72192 Option *o = &g->opts[i];
450
451
2/2
✓ Branch 0 taken 40906 times.
✓ Branch 1 taken 31286 times.
72192 if (g->group_def->flags &&
452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40906 times.
40906 !(g->group_def->flags & o->opt->flags)) {
453 av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
454 "%s %s -- you are trying to apply an input option to an "
455 "output file or vice versa. Move this option before the "
456 "file it belongs to.\n", o->key, o->opt->help,
457 g->group_def->name, g->arg);
458 return AVERROR(EINVAL);
459 }
460
461 72192 av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
462 72192 o->key, o->opt->help, o->val);
463
464 72192 ret = write_option(optctx, o->opt, o->key, o->val, defs);
465
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 72191 times.
72192 if (ret < 0)
466 1 return ret;
467 }
468
469 20368 av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
470
471 20368 return 0;
472 }
473
474 34645 int locate_option(int argc, char **argv, const OptionDef *options,
475 const char *optname)
476 {
477 const OptionDef *po;
478 int i;
479
480
2/2
✓ Branch 0 taken 781394 times.
✓ Branch 1 taken 34629 times.
816023 for (i = 1; i < argc; i++) {
481 781394 const char *cur_opt = argv[i];
482
483
2/2
✓ Branch 0 taken 21582 times.
✓ Branch 1 taken 759812 times.
781394 if (*cur_opt++ != '-')
484 21582 continue;
485
486 759812 po = find_option(options, cur_opt);
487
5/6
✓ Branch 0 taken 497717 times.
✓ Branch 1 taken 262095 times.
✓ Branch 2 taken 101470 times.
✓ Branch 3 taken 396247 times.
✓ Branch 4 taken 101470 times.
✗ Branch 5 not taken.
759812 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
488 101470 po = find_option(options, cur_opt + 2);
489
490
3/4
✓ Branch 0 taken 396287 times.
✓ Branch 1 taken 363525 times.
✓ Branch 2 taken 396287 times.
✗ Branch 3 not taken.
759812 if ((!po->name && !strcmp(cur_opt, optname)) ||
491
4/4
✓ Branch 0 taken 363525 times.
✓ Branch 1 taken 396287 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 363509 times.
759812 (po->name && !strcmp(optname, po->name)))
492 16 return i;
493
494
4/4
✓ Branch 0 taken 363509 times.
✓ Branch 1 taken 396287 times.
✓ Branch 3 taken 230829 times.
✓ Branch 4 taken 132680 times.
759796 if (!po->name || opt_has_arg(po))
495 627116 i++;
496 }
497 34629 return 0;
498 }
499
500 static void dump_argument(FILE *report_file, const char *a)
501 {
502 const unsigned char *p;
503
504 for (p = a; *p; p++)
505 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
506 *p == '_' || (*p >= 'a' && *p <= 'z')))
507 break;
508 if (!*p) {
509 fputs(a, report_file);
510 return;
511 }
512 fputc('"', report_file);
513 for (p = a; *p; p++) {
514 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
515 fprintf(report_file, "\\%c", *p);
516 else if (*p < ' ' || *p > '~')
517 fprintf(report_file, "\\x%02x", *p);
518 else
519 fputc(*p, report_file);
520 }
521 fputc('"', report_file);
522 }
523
524 6929 static void check_options(const OptionDef *po)
525 {
526
2/2
✓ Branch 0 taken 1298360 times.
✓ Branch 1 taken 6929 times.
1305289 while (po->name) {
527
2/2
✓ Branch 0 taken 799922 times.
✓ Branch 1 taken 498438 times.
1298360 if (po->flags & OPT_PERFILE)
528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 799922 times.
799922 av_assert0(po->flags & (OPT_INPUT | OPT_OUTPUT | OPT_DECODER));
529
530
2/2
✓ Branch 0 taken 516075 times.
✓ Branch 1 taken 782285 times.
1298360 if (po->type == OPT_TYPE_FUNC)
531
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 516075 times.
516075 av_assert0(!(po->flags & (OPT_FLAG_OFFSET | OPT_FLAG_SPEC)));
532
533 // OPT_FUNC_ARG can only be ser for OPT_TYPE_FUNC
534
3/4
✓ Branch 0 taken 782285 times.
✓ Branch 1 taken 516075 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 782285 times.
1298360 av_assert0((po->type == OPT_TYPE_FUNC) || !(po->flags & OPT_FUNC_ARG));
535
536 1298360 po++;
537 }
538 6929 }
539
540 6929 void parse_loglevel(int argc, char **argv, const OptionDef *options)
541 {
542 6929 int idx = locate_option(argc, argv, options, "loglevel");
543 char *env;
544
545 6929 check_options(options);
546
547
1/2
✓ Branch 0 taken 6929 times.
✗ Branch 1 not taken.
6929 if (!idx)
548 6929 idx = locate_option(argc, argv, options, "v");
549
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6913 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
6929 if (idx && argv[idx + 1])
550 16 opt_loglevel(NULL, "loglevel", argv[idx + 1]);
551 6929 idx = locate_option(argc, argv, options, "report");
552 6929 env = getenv_utf8("FFREPORT");
553
2/4
✓ Branch 0 taken 6929 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6929 times.
6929 if (env || idx) {
554 FILE *report_file = NULL;
555 init_report(env, &report_file);
556 if (report_file) {
557 int i;
558 fprintf(report_file, "Command line:\n");
559 for (i = 0; i < argc; i++) {
560 dump_argument(report_file, argv[i]);
561 fputc(i < argc - 1 ? ' ' : '\n', report_file);
562 }
563 fflush(report_file);
564 }
565 }
566 6929 freeenv_utf8(env);
567 6929 idx = locate_option(argc, argv, options, "hide_banner");
568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6929 times.
6929 if (idx)
569 hide_banner = 1;
570 6929 }
571
572 245009 static const AVOption *opt_find(void *obj, const char *name, const char *unit,
573 int opt_flags, int search_flags)
574 {
575 245009 const AVOption *o = av_opt_find(obj, name, unit, opt_flags, search_flags);
576
4/4
✓ Branch 0 taken 69945 times.
✓ Branch 1 taken 175064 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 69943 times.
245009 if(o && !o->flags)
577 2 return NULL;
578 245007 return o;
579 }
580
581 #define FLAGS ((o->type == AV_OPT_TYPE_FLAGS && (arg[0]=='-' || arg[0]=='+')) ? AV_DICT_APPEND : 0)
582 90130 int opt_default(void *optctx, const char *opt, const char *arg)
583 {
584 const AVOption *o;
585 90130 int consumed = 0;
586 char opt_stripped[128];
587 const char *p;
588 90130 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
589 #if CONFIG_SWSCALE
590 90130 const AVClass *sc = sws_get_class();
591 #endif
592 #if CONFIG_SWRESAMPLE
593 90130 const AVClass *swr_class = swr_get_class();
594 #endif
595
596
2/4
✓ Branch 0 taken 90130 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 90130 times.
90130 if (!strcmp(opt, "debug") || !strcmp(opt, "fdebug"))
597 av_log_set_level(AV_LOG_DEBUG);
598
599
2/2
✓ Branch 0 taken 90118 times.
✓ Branch 1 taken 12 times.
90130 if (!(p = strchr(opt, ':')))
600 90118 p = opt + strlen(opt);
601 90130 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
602
603
2/2
✓ Branch 1 taken 44389 times.
✓ Branch 2 taken 45741 times.
90130 if ((o = opt_find(&cc, opt_stripped, NULL, 0,
604 44389 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
605
8/8
✓ Branch 0 taken 44386 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 44374 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 12094 times.
✓ Branch 5 taken 32280 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 12108 times.
56498 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
606 12109 (o = opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) {
607
5/6
✓ Branch 0 taken 18483 times.
✓ Branch 1 taken 27259 times.
✓ Branch 2 taken 18483 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12045 times.
✓ Branch 5 taken 6438 times.
45742 av_dict_set(&codec_opts, opt, arg, FLAGS);
608 45742 consumed = 1;
609 }
610
2/2
✓ Branch 1 taken 12131 times.
✓ Branch 2 taken 77999 times.
90130 if ((o = opt_find(&fc, opt, NULL, 0,
611 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
612
5/6
✓ Branch 0 taken 11891 times.
✓ Branch 1 taken 240 times.
✓ Branch 2 taken 11891 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11883 times.
✓ Branch 5 taken 8 times.
12131 av_dict_set(&format_opts, opt, arg, FLAGS);
613
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 12033 times.
12131 if (consumed)
614 98 av_log(NULL, AV_LOG_VERBOSE, "Routing option %s to both codec and muxer layer\n", opt);
615 12131 consumed = 1;
616 }
617 #if CONFIG_SWSCALE
618
4/4
✓ Branch 0 taken 32355 times.
✓ Branch 1 taken 57775 times.
✓ Branch 3 taken 12070 times.
✓ Branch 4 taken 20285 times.
90130 if (!consumed && (o = opt_find(&sc, opt, NULL, 0,
619 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
620
2/4
✓ Branch 0 taken 12070 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12070 times.
✗ Branch 3 not taken.
12070 if (!strcmp(opt, "srcw") || !strcmp(opt, "srch") ||
621
2/4
✓ Branch 0 taken 12070 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12070 times.
✗ Branch 3 not taken.
12070 !strcmp(opt, "dstw") || !strcmp(opt, "dsth") ||
622
2/4
✓ Branch 0 taken 12070 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12070 times.
12070 !strcmp(opt, "src_format") || !strcmp(opt, "dst_format")) {
623 av_log(NULL, AV_LOG_ERROR, "Directly using swscale dimensions/format options is not supported, please use the -s or -pix_fmt options\n");
624 return AVERROR(EINVAL);
625 }
626
4/6
✓ Branch 0 taken 12070 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12070 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11885 times.
✓ Branch 5 taken 185 times.
12070 av_dict_set(&sws_dict, opt, arg, FLAGS);
627
628 12070 consumed = 1;
629 }
630 #else
631 if (!consumed && !strcmp(opt, "sws_flags")) {
632 av_log(NULL, AV_LOG_WARNING, "Ignoring %s %s, due to disabled swscale\n", opt, arg);
633 consumed = 1;
634 }
635 #endif
636 #if CONFIG_SWRESAMPLE
637
3/4
✓ Branch 0 taken 20285 times.
✓ Branch 1 taken 69845 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20285 times.
90130 if (!consumed && (o=opt_find(&swr_class, opt, NULL, 0,
638 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
639 av_dict_set(&swr_opts, opt, arg, FLAGS);
640 consumed = 1;
641 }
642 #endif
643
644
2/2
✓ Branch 0 taken 69845 times.
✓ Branch 1 taken 20285 times.
90130 if (consumed)
645 69845 return 0;
646 20285 return AVERROR_OPTION_NOT_FOUND;
647 }
648
649 /*
650 * Check whether given option is a group separator.
651 *
652 * @return index of the group definition that matched or -1 if none
653 */
654 148839 static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
655 const char *opt)
656 {
657 int i;
658
659
2/2
✓ Branch 0 taken 439708 times.
✓ Branch 1 taken 142029 times.
581737 for (i = 0; i < nb_groups; i++) {
660 439708 const OptionGroupDef *p = &groups[i];
661
4/4
✓ Branch 0 taken 290869 times.
✓ Branch 1 taken 148839 times.
✓ Branch 2 taken 6810 times.
✓ Branch 3 taken 284059 times.
439708 if (p->sep && !strcmp(p->sep, opt))
662 6810 return i;
663 }
664
665 142029 return -1;
666 }
667
668 /*
669 * Finish parsing an option group.
670 *
671 * @param group_idx which group definition should this group belong to
672 * @param arg argument of the group delimiting option
673 */
674 13590 static int finish_group(OptionParseContext *octx, int group_idx,
675 const char *arg)
676 {
677 13590 OptionGroupList *l = &octx->groups[group_idx];
678 OptionGroup *g;
679 int ret;
680
681 13590 ret = GROW_ARRAY(l->groups, l->nb_groups);
682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13590 times.
13590 if (ret < 0)
683 return ret;
684
685 13590 g = &l->groups[l->nb_groups - 1];
686
687 13590 *g = octx->cur_group;
688 13590 g->arg = arg;
689 13590 g->group_def = l->group_def;
690 13590 g->sws_dict = sws_dict;
691 13590 g->swr_opts = swr_opts;
692 13590 g->codec_opts = codec_opts;
693 13590 g->format_opts = format_opts;
694
695 13590 codec_opts = NULL;
696 13590 format_opts = NULL;
697 13590 sws_dict = NULL;
698 13590 swr_opts = NULL;
699
700 13590 memset(&octx->cur_group, 0, sizeof(octx->cur_group));
701
702 13590 return ret;
703 }
704
705 /*
706 * Add an option instance to currently parsed group.
707 */
708 72192 static int add_opt(OptionParseContext *octx, const OptionDef *opt,
709 const char *key, const char *val)
710 {
711 72192 int global = !(opt->flags & OPT_PERFILE);
712
2/2
✓ Branch 0 taken 31286 times.
✓ Branch 1 taken 40906 times.
72192 OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
713 int ret;
714
715 72192 ret = GROW_ARRAY(g->opts, g->nb_opts);
716
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72192 times.
72192 if (ret < 0)
717 return ret;
718
719 72192 g->opts[g->nb_opts - 1].opt = opt;
720 72192 g->opts[g->nb_opts - 1].key = key;
721 72192 g->opts[g->nb_opts - 1].val = val;
722
723 72192 return 0;
724 }
725
726 6779 static int init_parse_context(OptionParseContext *octx,
727 const OptionGroupDef *groups, int nb_groups)
728 {
729 static const OptionGroupDef global_group = { "global" };
730 int i;
731
732 6779 memset(octx, 0, sizeof(*octx));
733
734 6779 octx->groups = av_calloc(nb_groups, sizeof(*octx->groups));
735
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6779 times.
6779 if (!octx->groups)
736 return AVERROR(ENOMEM);
737 6779 octx->nb_groups = nb_groups;
738
739
2/2
✓ Branch 0 taken 20337 times.
✓ Branch 1 taken 6779 times.
27116 for (i = 0; i < octx->nb_groups; i++)
740 20337 octx->groups[i].group_def = &groups[i];
741
742 6779 octx->global_opts.group_def = &global_group;
743 6779 octx->global_opts.arg = "";
744
745 6779 return 0;
746 }
747
748 6779 void uninit_parse_context(OptionParseContext *octx)
749 {
750 int i, j;
751
752
2/2
✓ Branch 0 taken 20337 times.
✓ Branch 1 taken 6779 times.
27116 for (i = 0; i < octx->nb_groups; i++) {
753 20337 OptionGroupList *l = &octx->groups[i];
754
755
2/2
✓ Branch 0 taken 13590 times.
✓ Branch 1 taken 20337 times.
33927 for (j = 0; j < l->nb_groups; j++) {
756 13590 av_freep(&l->groups[j].opts);
757 13590 av_dict_free(&l->groups[j].codec_opts);
758 13590 av_dict_free(&l->groups[j].format_opts);
759
760 13590 av_dict_free(&l->groups[j].sws_dict);
761 13590 av_dict_free(&l->groups[j].swr_opts);
762 }
763 20337 av_freep(&l->groups);
764 }
765 6779 av_freep(&octx->groups);
766
767 6779 av_freep(&octx->cur_group.opts);
768 6779 av_freep(&octx->global_opts.opts);
769
770 6779 uninit_opts();
771 6779 }
772
773 6779 int split_commandline(OptionParseContext *octx, int argc, char *argv[],
774 const OptionDef *options,
775 const OptionGroupDef *groups, int nb_groups)
776 {
777 int ret;
778 6779 int optindex = 1;
779 6779 int dashdash = -2;
780
781 /* perform system-dependent conversions for arguments list */
782 6779 prepare_app_arguments(&argc, &argv);
783
784 6779 ret = init_parse_context(octx, groups, nb_groups);
785
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6779 times.
6779 if (ret < 0)
786 return ret;
787
788 6779 av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
789
790
2/2
✓ Branch 0 taken 155619 times.
✓ Branch 1 taken 6779 times.
162398 while (optindex < argc) {
791 155619 const char *opt = argv[optindex++], *arg;
792 const OptionDef *po;
793 int ret, group_idx;
794
795 155619 av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
796
797
3/6
✓ Branch 0 taken 151444 times.
✓ Branch 1 taken 4175 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 151444 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
155619 if (opt[0] == '-' && opt[1] == '-' && !opt[2]) {
798 dashdash = optindex;
799 continue;
800 }
801 /* unnamed group separators, e.g. output filename */
802
5/6
✓ Branch 0 taken 151444 times.
✓ Branch 1 taken 4175 times.
✓ Branch 2 taken 148839 times.
✓ Branch 3 taken 2605 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 148839 times.
155619 if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) {
803 6780 ret = finish_group(octx, 0, opt);
804
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6780 times.
6780 if (ret < 0)
805 return ret;
806
807 6780 av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
808 6780 continue;
809 }
810 148839 opt++;
811
812 #define GET_ARG(arg) \
813 do { \
814 arg = argv[optindex++]; \
815 if (!arg) { \
816 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
817 return AVERROR(EINVAL); \
818 } \
819 } while (0)
820
821 /* named group separators, e.g. -i */
822 148839 group_idx = match_group_separator(groups, nb_groups, opt);
823
2/2
✓ Branch 0 taken 6810 times.
✓ Branch 1 taken 142029 times.
148839 if (group_idx >= 0) {
824
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6810 times.
6810 GET_ARG(arg);
825 6810 ret = finish_group(octx, group_idx, arg);
826
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6810 times.
6810 if (ret < 0)
827 return ret;
828
829 6810 av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
830 6810 groups[group_idx].name, arg);
831 6810 continue;
832 }
833
834 /* normal options */
835 142029 po = find_option(options, opt);
836
2/2
✓ Branch 0 taken 51907 times.
✓ Branch 1 taken 90122 times.
142029 if (po->name) {
837
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 51906 times.
51907 if (po->flags & OPT_EXIT) {
838 /* optional argument, e.g. -h */
839 1 arg = argv[optindex++];
840
2/2
✓ Branch 1 taken 45904 times.
✓ Branch 2 taken 6002 times.
51906 } else if (opt_has_arg(po)) {
841
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45904 times.
45904 GET_ARG(arg);
842 } else {
843 6002 arg = "1";
844 }
845
846 51907 ret = add_opt(octx, po, opt, arg);
847
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51907 times.
51907 if (ret < 0)
848 return ret;
849
850 51907 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
851 51907 "argument '%s'.\n", po->name, po->help, arg);
852 51907 continue;
853 }
854
855 /* AVOptions */
856
1/2
✓ Branch 0 taken 90122 times.
✗ Branch 1 not taken.
90122 if (argv[optindex]) {
857 90122 ret = opt_default(NULL, opt, argv[optindex]);
858
2/2
✓ Branch 0 taken 69837 times.
✓ Branch 1 taken 20285 times.
90122 if (ret >= 0) {
859 69837 av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
860 69837 "argument '%s'.\n", opt, argv[optindex]);
861 69837 optindex++;
862 69837 continue;
863
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20285 times.
20285 } else if (ret != AVERROR_OPTION_NOT_FOUND) {
864 av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
865 "with argument '%s'.\n", opt, argv[optindex]);
866 return ret;
867 }
868 }
869
870 /* boolean -nofoo options */
871
3/6
✓ Branch 0 taken 20285 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20285 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20285 times.
✗ Branch 5 not taken.
40570 if (opt[0] == 'n' && opt[1] == 'o' &&
872 20285 (po = find_option(options, opt + 2)) &&
873
2/4
✓ Branch 0 taken 20285 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20285 times.
✗ Branch 3 not taken.
20285 po->name && po->type == OPT_TYPE_BOOL) {
874 20285 ret = add_opt(octx, po, opt, "0");
875
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20285 times.
20285 if (ret < 0)
876 return ret;
877
878 20285 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
879 20285 "argument 0.\n", po->name, po->help);
880 20285 continue;
881 }
882
883 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
884 return AVERROR_OPTION_NOT_FOUND;
885 }
886
887
3/6
✓ Branch 0 taken 6779 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6779 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6779 times.
6779 if (octx->cur_group.nb_opts || codec_opts || format_opts)
888 av_log(NULL, AV_LOG_WARNING, "Trailing option(s) found in the "
889 "command: may be ignored.\n");
890
891 6779 av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
892
893 6779 return 0;
894 }
895
896 int read_yesno(void)
897 {
898 int c = getchar();
899 int yesno = (av_toupper(c) == 'Y');
900
901 while (c != '\n' && c != EOF)
902 c = getchar();
903
904 return yesno;
905 }
906
907 FILE *get_preset_file(char *filename, size_t filename_size,
908 const char *preset_name, int is_path,
909 const char *codec_name)
910 {
911 FILE *f = NULL;
912 int i;
913 #if HAVE_GETMODULEHANDLE && defined(_WIN32)
914 char *datadir = NULL;
915 #endif
916 char *env_home = getenv_utf8("HOME");
917 char *env_ffmpeg_datadir = getenv_utf8("FFMPEG_DATADIR");
918 const char *base[3] = { env_ffmpeg_datadir,
919 env_home, /* index=1(HOME) is special: search in a .ffmpeg subfolder */
920 FFMPEG_DATADIR, };
921
922 if (is_path) {
923 av_strlcpy(filename, preset_name, filename_size);
924 f = fopen_utf8(filename, "r");
925 } else {
926 #if HAVE_GETMODULEHANDLE && defined(_WIN32)
927 wchar_t *datadir_w = get_module_filename(NULL);
928 base[2] = NULL;
929
930 if (wchartoutf8(datadir_w, &datadir))
931 datadir = NULL;
932 av_free(datadir_w);
933
934 if (datadir)
935 {
936 char *ls;
937 for (ls = datadir; *ls; ls++)
938 if (*ls == '\\') *ls = '/';
939
940 if (ls = strrchr(datadir, '/'))
941 {
942 ptrdiff_t datadir_len = ls - datadir;
943 size_t desired_size = datadir_len + strlen("/ffpresets") + 1;
944 char *new_datadir = av_realloc_array(
945 datadir, desired_size, sizeof *datadir);
946 if (new_datadir) {
947 datadir = new_datadir;
948 datadir[datadir_len] = 0;
949 strncat(datadir, "/ffpresets", desired_size - 1 - datadir_len);
950 base[2] = datadir;
951 }
952 }
953 }
954 #endif
955 for (i = 0; i < 3 && !f; i++) {
956 if (!base[i])
957 continue;
958 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
959 i != 1 ? "" : "/.ffmpeg", preset_name);
960 f = fopen_utf8(filename, "r");
961 if (!f && codec_name) {
962 snprintf(filename, filename_size,
963 "%s%s/%s-%s.ffpreset",
964 base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
965 preset_name);
966 f = fopen_utf8(filename, "r");
967 }
968 }
969 }
970
971 #if HAVE_GETMODULEHANDLE && defined(_WIN32)
972 av_free(datadir);
973 #endif
974 freeenv_utf8(env_ffmpeg_datadir);
975 freeenv_utf8(env_home);
976 return f;
977 }
978
979 29837 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
980 {
981 29837 int ret = avformat_match_stream_specifier(s, st, spec);
982
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29837 times.
29837 if (ret < 0)
983 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
984 29837 return ret;
985 }
986
987 22121 int filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_id,
988 AVFormatContext *s, AVStream *st, const AVCodec *codec,
989 AVDictionary **dst, AVDictionary **opts_used)
990 {
991 22121 AVDictionary *ret = NULL;
992 22121 const AVDictionaryEntry *t = NULL;
993 44242 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
994
2/2
✓ Branch 0 taken 7133 times.
✓ Branch 1 taken 14988 times.
22121 : AV_OPT_FLAG_DECODING_PARAM;
995 22121 char prefix = 0;
996 22121 const AVClass *cc = avcodec_get_class();
997
998
4/4
✓ Branch 0 taken 16813 times.
✓ Branch 1 taken 4985 times.
✓ Branch 2 taken 240 times.
✓ Branch 3 taken 83 times.
22121 switch (st->codecpar->codec_type) {
999 16813 case AVMEDIA_TYPE_VIDEO:
1000 16813 prefix = 'v';
1001 16813 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1002 16813 break;
1003 4985 case AVMEDIA_TYPE_AUDIO:
1004 4985 prefix = 'a';
1005 4985 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1006 4985 break;
1007 240 case AVMEDIA_TYPE_SUBTITLE:
1008 240 prefix = 's';
1009 240 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1010 240 break;
1011 }
1012
1013
2/2
✓ Branch 1 taken 63530 times.
✓ Branch 2 taken 22121 times.
85651 while (t = av_dict_iterate(opts, t)) {
1014 const AVClass *priv_class;
1015 63530 char *p = strchr(t->key, ':');
1016 63530 int used = 0;
1017
1018 /* check stream specification in opt name */
1019
2/2
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 63242 times.
63530 if (p) {
1020 288 int err = check_stream_specifier(s, st, p + 1);
1021
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 288 times.
288 if (err < 0) {
1022 av_dict_free(&ret);
1023 return err;
1024
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 210 times.
288 } else if (!err)
1025 78 continue;
1026
1027 210 *p = 0;
1028 }
1029
1030
4/4
✓ Branch 1 taken 2661 times.
✓ Branch 2 taken 60791 times.
✓ Branch 3 taken 1535 times.
✓ Branch 4 taken 1126 times.
63452 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1031 1535 !codec ||
1032
4/4
✓ Branch 0 taken 669 times.
✓ Branch 1 taken 866 times.
✓ Branch 2 taken 307 times.
✓ Branch 3 taken 362 times.
2204 ((priv_class = codec->priv_class) &&
1033 669 av_opt_find(&priv_class, t->key, NULL, flags,
1034 AV_OPT_SEARCH_FAKE_OBJ))) {
1035 62224 av_dict_set(&ret, t->key, t->value, 0);
1036 62224 used = 1;
1037
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1225 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
1231 } else if (t->key[0] == prefix &&
1038 3 av_opt_find(&cc, t->key + 1, NULL, flags,
1039 AV_OPT_SEARCH_FAKE_OBJ)) {
1040 1 av_dict_set(&ret, t->key + 1, t->value, 0);
1041 1 used = 1;
1042 }
1043
1044
2/2
✓ Branch 0 taken 210 times.
✓ Branch 1 taken 63242 times.
63452 if (p)
1045 210 *p = ':';
1046
1047
4/4
✓ Branch 0 taken 62225 times.
✓ Branch 1 taken 1227 times.
✓ Branch 2 taken 39402 times.
✓ Branch 3 taken 22823 times.
63452 if (used && opts_used)
1048 39402 av_dict_set(opts_used, t->key, "", 0);
1049 }
1050
1051 22121 *dst = ret;
1052 22121 return 0;
1053 }
1054
1055 6957 int setup_find_stream_info_opts(AVFormatContext *s,
1056 AVDictionary *codec_opts,
1057 AVDictionary ***dst)
1058 {
1059 int ret;
1060 AVDictionary **opts;
1061
1062 6957 *dst = NULL;
1063
1064
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 6872 times.
6957 if (!s->nb_streams)
1065 85 return 0;
1066
1067 6872 opts = av_calloc(s->nb_streams, sizeof(*opts));
1068
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6872 times.
6872 if (!opts)
1069 return AVERROR(ENOMEM);
1070
1071
2/2
✓ Branch 0 taken 7470 times.
✓ Branch 1 taken 6872 times.
14342 for (int i = 0; i < s->nb_streams; i++) {
1072 7470 ret = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id,
1073 7470 s, s->streams[i], NULL, &opts[i], NULL);
1074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7470 times.
7470 if (ret < 0)
1075 goto fail;
1076 }
1077 6872 *dst = opts;
1078 6872 return 0;
1079 fail:
1080 for (int i = 0; i < s->nb_streams; i++)
1081 av_dict_free(&opts[i]);
1082 av_freep(&opts);
1083 return ret;
1084 }
1085
1086 201026 int grow_array(void **array, int elem_size, int *size, int new_size)
1087 {
1088
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 201026 times.
201026 if (new_size >= INT_MAX / elem_size) {
1089 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1090 return AVERROR(ERANGE);
1091 }
1092
1/2
✓ Branch 0 taken 201026 times.
✗ Branch 1 not taken.
201026 if (*size < new_size) {
1093 201026 uint8_t *tmp = av_realloc_array(*array, new_size, elem_size);
1094
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 201026 times.
201026 if (!tmp)
1095 return AVERROR(ENOMEM);
1096 201026 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
1097 201026 *size = new_size;
1098 201026 *array = tmp;
1099 201026 return 0;
1100 }
1101 return 0;
1102 }
1103
1104 40906 void *allocate_array_elem(void *ptr, size_t elem_size, int *nb_elems)
1105 {
1106 void *new_elem;
1107
1108
2/4
✓ Branch 1 taken 40906 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 40906 times.
81812 if (!(new_elem = av_mallocz(elem_size)) ||
1109 40906 av_dynarray_add_nofree(ptr, nb_elems, new_elem) < 0)
1110 return NULL;
1111 40906 return new_elem;
1112 }
1113
1114 5208 double get_rotation(const int32_t *displaymatrix)
1115 {
1116 5208 double theta = 0;
1117
1/2
✓ Branch 0 taken 5208 times.
✗ Branch 1 not taken.
5208 if (displaymatrix)
1118 5208 theta = -round(av_display_rotation_get(displaymatrix));
1119
1120 5208 theta -= 360*floor(theta/360 + 0.9/360);
1121
1122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5208 times.
5208 if (fabs(theta - 90*round(theta/90)) > 2)
1123 av_log(NULL, AV_LOG_WARNING, "Odd rotation angle.\n"
1124 "If you want to help, upload a sample "
1125 "of this file to https://streams.videolan.org/upload/ "
1126 "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)");
1127
1128 5208 return theta;
1129 }
1130
1131 /* read file contents into a string */
1132 57 char *file_read(const char *filename)
1133 {
1134 57 AVIOContext *pb = NULL;
1135 57 int ret = avio_open(&pb, filename, AVIO_FLAG_READ);
1136 AVBPrint bprint;
1137 char *str;
1138
1139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
57 if (ret < 0) {
1140 av_log(NULL, AV_LOG_ERROR, "Error opening file %s.\n", filename);
1141 return NULL;
1142 }
1143
1144 57 av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
1145 57 ret = avio_read_to_bprint(pb, &bprint, SIZE_MAX);
1146 57 avio_closep(&pb);
1147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
57 if (ret < 0) {
1148 av_bprint_finalize(&bprint, NULL);
1149 return NULL;
1150 }
1151 57 ret = av_bprint_finalize(&bprint, &str);
1152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
57 if (ret < 0)
1153 return NULL;
1154 57 return str;
1155 }
1156
1157 6809 void remove_avoptions(AVDictionary **a, AVDictionary *b)
1158 {
1159 6809 const AVDictionaryEntry *t = NULL;
1160
1161
2/2
✓ Branch 1 taken 21693 times.
✓ Branch 2 taken 6809 times.
28502 while ((t = av_dict_iterate(b, t))) {
1162 21693 av_dict_set(a, t->key, NULL, AV_DICT_MATCH_CASE);
1163 }
1164 6809 }
1165
1166 19701 int check_avoptions(AVDictionary *m)
1167 {
1168 19701 const AVDictionaryEntry *t = av_dict_iterate(m, NULL);
1169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19701 times.
19701 if (t) {
1170 av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key);
1171 return AVERROR_OPTION_NOT_FOUND;
1172 }
1173
1174 19701 return 0;
1175 }
1176