2 * Various utilities for command line tools
3 * Copyright (c) 2000-2003 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 /* Include only the enabled headers since some compilers (namely, Sun
28 Studio) will not omit unused inline functions and create undefined
29 references to libraries that are not being built. */
32 #include "libavformat/avformat.h"
33 #include "libavfilter/avfilter.h"
34 #include "libavdevice/avdevice.h"
35 #include "libavresample/avresample.h"
36 #include "libswscale/swscale.h"
37 #include "libavutil/avassert.h"
38 #include "libavutil/avstring.h"
39 #include "libavutil/mathematics.h"
40 #include "libavutil/imgutils.h"
41 #include "libavutil/parseutils.h"
42 #include "libavutil/pixdesc.h"
43 #include "libavutil/eval.h"
44 #include "libavutil/dict.h"
45 #include "libavutil/opt.h"
49 #include "libavformat/network.h"
51 #if HAVE_SYS_RESOURCE_H
53 #include <sys/resource.h>
56 struct SwsContext
*sws_opts
;
57 AVDictionary
*format_opts
, *codec_opts
;
59 static const int this_year
= 2013;
64 sws_opts
= sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC
,
69 void uninit_opts(void)
72 sws_freeContext(sws_opts
);
75 av_dict_free(&format_opts
);
76 av_dict_free(&codec_opts
);
79 void log_callback_help(void *ptr
, int level
, const char *fmt
, va_list vl
)
81 vfprintf(stdout
, fmt
, vl
);
84 double parse_number_or_die(const char *context
, const char *numstr
, int type
,
85 double min
, double max
)
89 double d
= av_strtod(numstr
, &tail
);
91 error
= "Expected number for %s but found: %s\n";
92 else if (d
< min
|| d
> max
)
93 error
= "The value for %s was %s which is not within %f - %f\n";
94 else if (type
== OPT_INT64
&& (int64_t)d
!= d
)
95 error
= "Expected int64 for %s but found %s\n";
96 else if (type
== OPT_INT
&& (int)d
!= d
)
97 error
= "Expected int for %s but found %s\n";
100 av_log(NULL
, AV_LOG_FATAL
, error
, context
, numstr
, min
, max
);
105 int64_t parse_time_or_die(const char *context
, const char *timestr
,
109 if (av_parse_time(&us
, timestr
, is_duration
) < 0) {
110 av_log(NULL
, AV_LOG_FATAL
, "Invalid %s specification for %s: %s\n",
111 is_duration
? "duration" : "date", context
, timestr
);
117 void show_help_options(const OptionDef
*options
, const char *msg
, int req_flags
,
118 int rej_flags
, int alt_flags
)
124 for (po
= options
; po
->name
!= NULL
; po
++) {
127 if (((po
->flags
& req_flags
) != req_flags
) ||
128 (alt_flags
&& !(po
->flags
& alt_flags
)) ||
129 (po
->flags
& rej_flags
))
136 av_strlcpy(buf
, po
->name
, sizeof(buf
));
138 av_strlcat(buf
, " ", sizeof(buf
));
139 av_strlcat(buf
, po
->argname
, sizeof(buf
));
141 printf("-%-17s %s\n", buf
, po
->help
);
146 void show_help_children(const AVClass
*class, int flags
)
148 const AVClass
*child
= NULL
;
149 av_opt_show2(&class, NULL
, flags
, 0);
152 while (child
= av_opt_child_class_next(class, child
))
153 show_help_children(child
, flags
);
156 static const OptionDef
*find_option(const OptionDef
*po
, const char *name
)
158 const char *p
= strchr(name
, ':');
159 int len
= p
? p
- name
: strlen(name
);
161 while (po
->name
!= NULL
) {
162 if (!strncmp(name
, po
->name
, len
) && strlen(po
->name
) == len
)
169 #if HAVE_COMMANDLINETOARGVW
171 #include <shellapi.h>
172 /* Will be leaked on exit */
173 static char** win32_argv_utf8
= NULL
;
174 static int win32_argc
= 0;
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.
183 static void prepare_app_arguments(int *argc_ptr
, char ***argv_ptr
)
187 int i
, buffsize
= 0, offset
= 0;
189 if (win32_argv_utf8
) {
190 *argc_ptr
= win32_argc
;
191 *argv_ptr
= win32_argv_utf8
;
196 argv_w
= CommandLineToArgvW(GetCommandLineW(), &win32_argc
);
197 if (win32_argc
<= 0 || !argv_w
)
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
);
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
== NULL
) {
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
);
218 win32_argv_utf8
[i
] = NULL
;
221 *argc_ptr
= win32_argc
;
222 *argv_ptr
= win32_argv_utf8
;
225 static inline void prepare_app_arguments(int *argc_ptr
, char ***argv_ptr
)
229 #endif /* HAVE_COMMANDLINETOARGVW */
231 static int write_option(void *optctx
, const OptionDef
*po
, const char *opt
,
234 /* new-style options contain an offset into optctx, old-style address of
236 void *dst
= po
->flags
& (OPT_OFFSET
| OPT_SPEC
) ?
237 (uint8_t *)optctx
+ po
->u
.off
: po
->u
.dst_ptr
;
240 if (po
->flags
& OPT_SPEC
) {
241 SpecifierOpt
**so
= dst
;
242 char *p
= strchr(opt
, ':');
244 dstcount
= (int *)(so
+ 1);
245 *so
= grow_array(*so
, sizeof(**so
), dstcount
, *dstcount
+ 1);
246 (*so
)[*dstcount
- 1].specifier
= av_strdup(p
? p
+ 1 : "");
247 dst
= &(*so
)[*dstcount
- 1].u
;
250 if (po
->flags
& OPT_STRING
) {
252 str
= av_strdup(arg
);
255 } else if (po
->flags
& OPT_BOOL
|| po
->flags
& OPT_INT
) {
256 *(int *)dst
= parse_number_or_die(opt
, arg
, OPT_INT64
, INT_MIN
, INT_MAX
);
257 } else if (po
->flags
& OPT_INT64
) {
258 *(int64_t *)dst
= parse_number_or_die(opt
, arg
, OPT_INT64
, INT64_MIN
, INT64_MAX
);
259 } else if (po
->flags
& OPT_TIME
) {
260 *(int64_t *)dst
= parse_time_or_die(opt
, arg
, 1);
261 } else if (po
->flags
& OPT_FLOAT
) {
262 *(float *)dst
= parse_number_or_die(opt
, arg
, OPT_FLOAT
, -INFINITY
, INFINITY
);
263 } else if (po
->flags
& OPT_DOUBLE
) {
264 *(double *)dst
= parse_number_or_die(opt
, arg
, OPT_DOUBLE
, -INFINITY
, INFINITY
);
265 } else if (po
->u
.func_arg
) {
266 int ret
= po
->u
.func_arg(optctx
, opt
, arg
);
268 av_log(NULL
, AV_LOG_ERROR
,
269 "Failed to set value '%s' for option '%s'\n", arg
, opt
);
273 if (po
->flags
& OPT_EXIT
)
279 int parse_option(void *optctx
, const char *opt
, const char *arg
,
280 const OptionDef
*options
)
285 po
= find_option(options
, opt
);
286 if (!po
->name
&& opt
[0] == 'n' && opt
[1] == 'o') {
287 /* handle 'no' bool option */
288 po
= find_option(options
, opt
+ 2);
289 if ((po
->name
&& (po
->flags
& OPT_BOOL
)))
291 } else if (po
->flags
& OPT_BOOL
)
295 po
= find_option(options
, "default");
297 av_log(NULL
, AV_LOG_ERROR
, "Unrecognized option '%s'\n", opt
);
298 return AVERROR(EINVAL
);
300 if (po
->flags
& HAS_ARG
&& !arg
) {
301 av_log(NULL
, AV_LOG_ERROR
, "Missing argument for option '%s'\n", opt
);
302 return AVERROR(EINVAL
);
305 ret
= write_option(optctx
, po
, opt
, arg
);
309 return !!(po
->flags
& HAS_ARG
);
312 void parse_options(void *optctx
, int argc
, char **argv
, const OptionDef
*options
,
313 void (*parse_arg_function
)(void *, const char*))
316 int optindex
, handleoptions
= 1, ret
;
318 /* perform system-dependent conversions for arguments list */
319 prepare_app_arguments(&argc
, &argv
);
323 while (optindex
< argc
) {
324 opt
= argv
[optindex
++];
326 if (handleoptions
&& opt
[0] == '-' && opt
[1] != '\0') {
327 if (opt
[1] == '-' && opt
[2] == '\0') {
333 if ((ret
= parse_option(optctx
, opt
, argv
[optindex
], options
)) < 0)
337 if (parse_arg_function
)
338 parse_arg_function(optctx
, opt
);
343 int parse_optgroup(void *optctx
, OptionGroup
*g
)
347 av_log(NULL
, AV_LOG_DEBUG
, "Parsing a group of options: %s %s.\n",
348 g
->group_def
->name
, g
->arg
);
350 for (i
= 0; i
< g
->nb_opts
; i
++) {
351 Option
*o
= &g
->opts
[i
];
353 av_log(NULL
, AV_LOG_DEBUG
, "Applying option %s (%s) with argument %s.\n",
354 o
->key
, o
->opt
->help
, o
->val
);
356 ret
= write_option(optctx
, o
->opt
, o
->key
, o
->val
);
361 av_log(NULL
, AV_LOG_DEBUG
, "Successfully parsed a group of options.\n");
366 int locate_option(int argc
, char **argv
, const OptionDef
*options
,
372 for (i
= 1; i
< argc
; i
++) {
373 const char *cur_opt
= argv
[i
];
375 if (*cur_opt
++ != '-')
378 po
= find_option(options
, cur_opt
);
379 if (!po
->name
&& cur_opt
[0] == 'n' && cur_opt
[1] == 'o')
380 po
= find_option(options
, cur_opt
+ 2);
382 if ((!po
->name
&& !strcmp(cur_opt
, optname
)) ||
383 (po
->name
&& !strcmp(optname
, po
->name
)))
386 if (!po
|| po
->flags
& HAS_ARG
)
392 void parse_loglevel(int argc
, char **argv
, const OptionDef
*options
)
394 int idx
= locate_option(argc
, argv
, options
, "loglevel");
396 idx
= locate_option(argc
, argv
, options
, "v");
397 if (idx
&& argv
[idx
+ 1])
398 opt_loglevel(NULL
, "loglevel", argv
[idx
+ 1]);
401 #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
402 int opt_default(void *optctx
, const char *opt
, const char *arg
)
405 char opt_stripped
[128];
407 const AVClass
*cc
= avcodec_get_class(), *fc
= avformat_get_class();
409 const AVClass
*sc
= sws_get_class();
412 if (!(p
= strchr(opt
, ':')))
413 p
= opt
+ strlen(opt
);
414 av_strlcpy(opt_stripped
, opt
, FFMIN(sizeof(opt_stripped
), p
- opt
+ 1));
416 if ((o
= av_opt_find(&cc
, opt_stripped
, NULL
, 0,
417 AV_OPT_SEARCH_CHILDREN
| AV_OPT_SEARCH_FAKE_OBJ
)) ||
418 ((opt
[0] == 'v' || opt
[0] == 'a' || opt
[0] == 's') &&
419 (o
= av_opt_find(&cc
, opt
+ 1, NULL
, 0, AV_OPT_SEARCH_FAKE_OBJ
))))
420 av_dict_set(&codec_opts
, opt
, arg
, FLAGS
);
421 else if ((o
= av_opt_find(&fc
, opt
, NULL
, 0,
422 AV_OPT_SEARCH_CHILDREN
| AV_OPT_SEARCH_FAKE_OBJ
)))
423 av_dict_set(&format_opts
, opt
, arg
, FLAGS
);
425 else if ((o
= av_opt_find(&sc
, opt
, NULL
, 0,
426 AV_OPT_SEARCH_CHILDREN
| AV_OPT_SEARCH_FAKE_OBJ
))) {
427 // XXX we only support sws_flags, not arbitrary sws options
428 int ret
= av_opt_set(sws_opts
, opt
, arg
, 0);
430 av_log(NULL
, AV_LOG_ERROR
, "Error setting option %s.\n", opt
);
438 return AVERROR_OPTION_NOT_FOUND
;
442 * Check whether given option is a group separator.
444 * @return index of the group definition that matched or -1 if none
446 static int match_group_separator(const OptionGroupDef
*groups
, int nb_groups
,
451 for (i
= 0; i
< nb_groups
; i
++) {
452 const OptionGroupDef
*p
= &groups
[i
];
453 if (p
->sep
&& !strcmp(p
->sep
, opt
))
461 * Finish parsing an option group.
463 * @param group_idx which group definition should this group belong to
464 * @param arg argument of the group delimiting option
466 static void finish_group(OptionParseContext
*octx
, int group_idx
,
469 OptionGroupList
*l
= &octx
->groups
[group_idx
];
472 GROW_ARRAY(l
->groups
, l
->nb_groups
);
473 g
= &l
->groups
[l
->nb_groups
- 1];
475 *g
= octx
->cur_group
;
477 g
->group_def
= l
->group_def
;
479 g
->sws_opts
= sws_opts
;
481 g
->codec_opts
= codec_opts
;
482 g
->format_opts
= format_opts
;
491 memset(&octx
->cur_group
, 0, sizeof(octx
->cur_group
));
495 * Add an option instance to currently parsed group.
497 static void add_opt(OptionParseContext
*octx
, const OptionDef
*opt
,
498 const char *key
, const char *val
)
500 int global
= !(opt
->flags
& (OPT_PERFILE
| OPT_SPEC
| OPT_OFFSET
));
501 OptionGroup
*g
= global
? &octx
->global_opts
: &octx
->cur_group
;
503 GROW_ARRAY(g
->opts
, g
->nb_opts
);
504 g
->opts
[g
->nb_opts
- 1].opt
= opt
;
505 g
->opts
[g
->nb_opts
- 1].key
= key
;
506 g
->opts
[g
->nb_opts
- 1].val
= val
;
509 static void init_parse_context(OptionParseContext
*octx
,
510 const OptionGroupDef
*groups
, int nb_groups
)
512 static const OptionGroupDef global_group
= { "global" };
515 memset(octx
, 0, sizeof(*octx
));
517 octx
->nb_groups
= nb_groups
;
518 octx
->groups
= av_mallocz(sizeof(*octx
->groups
) * octx
->nb_groups
);
522 for (i
= 0; i
< octx
->nb_groups
; i
++)
523 octx
->groups
[i
].group_def
= &groups
[i
];
525 octx
->global_opts
.group_def
= &global_group
;
526 octx
->global_opts
.arg
= "";
531 void uninit_parse_context(OptionParseContext
*octx
)
535 for (i
= 0; i
< octx
->nb_groups
; i
++) {
536 OptionGroupList
*l
= &octx
->groups
[i
];
538 for (j
= 0; j
< l
->nb_groups
; j
++) {
539 av_freep(&l
->groups
[j
].opts
);
540 av_dict_free(&l
->groups
[j
].codec_opts
);
541 av_dict_free(&l
->groups
[j
].format_opts
);
543 sws_freeContext(l
->groups
[j
].sws_opts
);
546 av_freep(&l
->groups
);
548 av_freep(&octx
->groups
);
550 av_freep(&octx
->cur_group
.opts
);
551 av_freep(&octx
->global_opts
.opts
);
556 int split_commandline(OptionParseContext
*octx
, int argc
, char *argv
[],
557 const OptionDef
*options
,
558 const OptionGroupDef
*groups
, int nb_groups
)
562 /* perform system-dependent conversions for arguments list */
563 prepare_app_arguments(&argc
, &argv
);
565 init_parse_context(octx
, groups
, nb_groups
);
566 av_log(NULL
, AV_LOG_DEBUG
, "Splitting the commandline.\n");
568 while (optindex
< argc
) {
569 const char *opt
= argv
[optindex
++], *arg
;
573 av_log(NULL
, AV_LOG_DEBUG
, "Reading option '%s' ...", opt
);
575 /* unnamed group separators, e.g. output filename */
576 if (opt
[0] != '-' || !opt
[1]) {
577 finish_group(octx
, 0, opt
);
578 av_log(NULL
, AV_LOG_DEBUG
, " matched as %s.\n", groups
[0].name
);
583 #define GET_ARG(arg) \
585 arg = argv[optindex++]; \
587 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
588 return AVERROR(EINVAL); \
592 /* named group separators, e.g. -i */
593 if ((ret
= match_group_separator(groups
, nb_groups
, opt
)) >= 0) {
595 finish_group(octx
, ret
, arg
);
596 av_log(NULL
, AV_LOG_DEBUG
, " matched as %s with argument '%s'.\n",
597 groups
[ret
].name
, arg
);
602 po
= find_option(options
, opt
);
604 if (po
->flags
& OPT_EXIT
) {
605 /* optional argument, e.g. -h */
606 arg
= argv
[optindex
++];
607 } else if (po
->flags
& HAS_ARG
) {
613 add_opt(octx
, po
, opt
, arg
);
614 av_log(NULL
, AV_LOG_DEBUG
, " matched as option '%s' (%s) with "
615 "argument '%s'.\n", po
->name
, po
->help
, arg
);
620 if (argv
[optindex
]) {
621 ret
= opt_default(NULL
, opt
, argv
[optindex
]);
623 av_log(NULL
, AV_LOG_DEBUG
, " matched as AVOption '%s' with "
624 "argument '%s'.\n", opt
, argv
[optindex
]);
627 } else if (ret
!= AVERROR_OPTION_NOT_FOUND
) {
628 av_log(NULL
, AV_LOG_ERROR
, "Error parsing option '%s' "
629 "with argument '%s'.\n", opt
, argv
[optindex
]);
634 /* boolean -nofoo options */
635 if (opt
[0] == 'n' && opt
[1] == 'o' &&
636 (po
= find_option(options
, opt
+ 2)) &&
637 po
->name
&& po
->flags
& OPT_BOOL
) {
638 add_opt(octx
, po
, opt
, "0");
639 av_log(NULL
, AV_LOG_DEBUG
, " matched as option '%s' (%s) with "
640 "argument 0.\n", po
->name
, po
->help
);
644 av_log(NULL
, AV_LOG_ERROR
, "Unrecognized option '%s'.\n", opt
);
645 return AVERROR_OPTION_NOT_FOUND
;
648 if (octx
->cur_group
.nb_opts
|| codec_opts
|| format_opts
)
649 av_log(NULL
, AV_LOG_WARNING
, "Trailing options were found on the "
652 av_log(NULL
, AV_LOG_DEBUG
, "Finished splitting the commandline.\n");
657 int opt_loglevel(void *optctx
, const char *opt
, const char *arg
)
659 const struct { const char *name
; int level
; } log_levels
[] = {
660 { "quiet" , AV_LOG_QUIET
},
661 { "panic" , AV_LOG_PANIC
},
662 { "fatal" , AV_LOG_FATAL
},
663 { "error" , AV_LOG_ERROR
},
664 { "warning", AV_LOG_WARNING
},
665 { "info" , AV_LOG_INFO
},
666 { "verbose", AV_LOG_VERBOSE
},
667 { "debug" , AV_LOG_DEBUG
},
673 for (i
= 0; i
< FF_ARRAY_ELEMS(log_levels
); i
++) {
674 if (!strcmp(log_levels
[i
].name
, arg
)) {
675 av_log_set_level(log_levels
[i
].level
);
680 level
= strtol(arg
, &tail
, 10);
682 av_log(NULL
, AV_LOG_FATAL
, "Invalid loglevel \"%s\". "
683 "Possible levels are numbers or:\n", arg
);
684 for (i
= 0; i
< FF_ARRAY_ELEMS(log_levels
); i
++)
685 av_log(NULL
, AV_LOG_FATAL
, "\"%s\"\n", log_levels
[i
].name
);
688 av_log_set_level(level
);
692 int opt_timelimit(void *optctx
, const char *opt
, const char *arg
)
695 int lim
= parse_number_or_die(opt
, arg
, OPT_INT64
, 0, INT_MAX
);
696 struct rlimit rl
= { lim
, lim
+ 1 };
697 if (setrlimit(RLIMIT_CPU
, &rl
))
700 av_log(NULL
, AV_LOG_WARNING
, "-%s not implemented on this OS\n", opt
);
705 void print_error(const char *filename
, int err
)
708 const char *errbuf_ptr
= errbuf
;
710 if (av_strerror(err
, errbuf
, sizeof(errbuf
)) < 0)
711 errbuf_ptr
= strerror(AVUNERROR(err
));
712 av_log(NULL
, AV_LOG_ERROR
, "%s: %s\n", filename
, errbuf_ptr
);
715 static int warned_cfg
= 0;
718 #define SHOW_VERSION 2
719 #define SHOW_CONFIG 4
721 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
722 if (CONFIG_##LIBNAME) { \
723 const char *indent = flags & INDENT? " " : ""; \
724 if (flags & SHOW_VERSION) { \
725 unsigned int version = libname##_version(); \
726 av_log(NULL, level, \
727 "%slib%-10s %2d.%3d.%2d / %2d.%3d.%2d\n", \
729 LIB##LIBNAME##_VERSION_MAJOR, \
730 LIB##LIBNAME##_VERSION_MINOR, \
731 LIB##LIBNAME##_VERSION_MICRO, \
732 version >> 16, version >> 8 & 0xff, version & 0xff); \
734 if (flags & SHOW_CONFIG) { \
735 const char *cfg = libname##_configuration(); \
736 if (strcmp(LIBAV_CONFIGURATION, cfg)) { \
738 av_log(NULL, level, \
739 "%sWARNING: library configuration mismatch\n", \
743 av_log(NULL, level, "%s%-11s configuration: %s\n", \
744 indent, #libname, cfg); \
749 static void print_all_libs_info(int flags, int level)
751 PRINT_LIB_INFO(avutil
, AVUTIL
, flags
, level
);
752 PRINT_LIB_INFO(avcodec
, AVCODEC
, flags
, level
);
753 PRINT_LIB_INFO(avformat
, AVFORMAT
, flags
, level
);
754 PRINT_LIB_INFO(avdevice
, AVDEVICE
, flags
, level
);
755 PRINT_LIB_INFO(avfilter
, AVFILTER
, flags
, level
);
756 PRINT_LIB_INFO(avresample
, AVRESAMPLE
, flags
, level
);
757 PRINT_LIB_INFO(swscale
, SWSCALE
, flags
, level
);
760 void show_banner(void)
762 av_log(NULL
, AV_LOG_INFO
,
763 "%s version " LIBAV_VERSION
", Copyright (c) %d-%d the Libav developers\n",
764 program_name
, program_birth_year
, this_year
);
765 av_log(NULL
, AV_LOG_INFO
, " built on %s %s with %s\n",
766 __DATE__
, __TIME__
, CC_IDENT
);
767 av_log(NULL
, AV_LOG_VERBOSE
, " configuration: " LIBAV_CONFIGURATION
"\n");
768 print_all_libs_info(INDENT
|SHOW_CONFIG
, AV_LOG_VERBOSE
);
769 print_all_libs_info(INDENT
|SHOW_VERSION
, AV_LOG_VERBOSE
);
772 int show_version(void *optctx
, const char *opt
, const char *arg
)
774 av_log_set_callback(log_callback_help
);
775 printf("%s " LIBAV_VERSION
"\n", program_name
);
776 print_all_libs_info(SHOW_VERSION
, AV_LOG_INFO
);
781 int show_license(void *optctx
, const char *opt
, const char *arg
)
785 "This version of %s has nonfree parts compiled in.\n"
786 "Therefore it is not legally redistributable.\n",
789 "%s is free software; you can redistribute it and/or modify\n"
790 "it under the terms of the GNU General Public License as published by\n"
791 "the Free Software Foundation; either version 3 of the License, or\n"
792 "(at your option) any later version.\n"
794 "%s is distributed in the hope that it will be useful,\n"
795 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
796 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
797 "GNU General Public License for more details.\n"
799 "You should have received a copy of the GNU General Public License\n"
800 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
801 program_name
, program_name
, program_name
803 "%s is free software; you can redistribute it and/or modify\n"
804 "it under the terms of the GNU General Public License as published by\n"
805 "the Free Software Foundation; either version 2 of the License, or\n"
806 "(at your option) any later version.\n"
808 "%s is distributed in the hope that it will be useful,\n"
809 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
810 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
811 "GNU General Public License for more details.\n"
813 "You should have received a copy of the GNU General Public License\n"
814 "along with %s; if not, write to the Free Software\n"
815 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
816 program_name
, program_name
, program_name
818 "%s is free software; you can redistribute it and/or modify\n"
819 "it under the terms of the GNU Lesser General Public License as published by\n"
820 "the Free Software Foundation; either version 3 of the License, or\n"
821 "(at your option) any later version.\n"
823 "%s is distributed in the hope that it will be useful,\n"
824 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
825 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
826 "GNU Lesser General Public License for more details.\n"
828 "You should have received a copy of the GNU Lesser General Public License\n"
829 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
830 program_name
, program_name
, program_name
832 "%s is free software; you can redistribute it and/or\n"
833 "modify it under the terms of the GNU Lesser General Public\n"
834 "License as published by the Free Software Foundation; either\n"
835 "version 2.1 of the License, or (at your option) any later version.\n"
837 "%s is distributed in the hope that it will be useful,\n"
838 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
839 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
840 "Lesser General Public License for more details.\n"
842 "You should have received a copy of the GNU Lesser General Public\n"
843 "License along with %s; if not, write to the Free Software\n"
844 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
845 program_name
, program_name
, program_name
852 int show_formats(void *optctx
, const char *opt
, const char *arg
)
854 AVInputFormat
*ifmt
= NULL
;
855 AVOutputFormat
*ofmt
= NULL
;
856 const char *last_name
;
858 printf("File formats:\n"
859 " D. = Demuxing supported\n"
860 " .E = Muxing supported\n"
866 const char *name
= NULL
;
867 const char *long_name
= NULL
;
869 while ((ofmt
= av_oformat_next(ofmt
))) {
870 if ((name
== NULL
|| strcmp(ofmt
->name
, name
) < 0) &&
871 strcmp(ofmt
->name
, last_name
) > 0) {
873 long_name
= ofmt
->long_name
;
877 while ((ifmt
= av_iformat_next(ifmt
))) {
878 if ((name
== NULL
|| strcmp(ifmt
->name
, name
) < 0) &&
879 strcmp(ifmt
->name
, last_name
) > 0) {
881 long_name
= ifmt
->long_name
;
884 if (name
&& strcmp(ifmt
->name
, name
) == 0)
891 printf(" %s%s %-15s %s\n",
895 long_name
? long_name
:" ");
900 #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
901 if (codec->field) { \
902 const type *p = c->field; \
904 printf(" Supported " list_name ":"); \
905 while (*p != term) { \
907 printf(" %s", name); \
913 static void print_codec(const AVCodec *c)
915 int encoder
= av_codec_is_encoder(c
);
917 printf("%s %s [%s]:\n", encoder
? "Encoder" : "Decoder", c
->name
,
918 c
->long_name
? c
->long_name
: "");
920 if (c
->type
== AVMEDIA_TYPE_VIDEO
) {
921 printf(" Threading capabilities: ");
922 switch (c
->capabilities
& (CODEC_CAP_FRAME_THREADS
|
923 CODEC_CAP_SLICE_THREADS
)) {
924 case CODEC_CAP_FRAME_THREADS
|
925 CODEC_CAP_SLICE_THREADS
: printf("frame and slice"); break;
926 case CODEC_CAP_FRAME_THREADS
: printf("frame"); break;
927 case CODEC_CAP_SLICE_THREADS
: printf("slice"); break;
928 default: printf("no"); break;
933 if (c
->supported_framerates
) {
934 const AVRational
*fps
= c
->supported_framerates
;
936 printf(" Supported framerates:");
938 printf(" %d/%d", fps
->num
, fps
->den
);
943 PRINT_CODEC_SUPPORTED(c
, pix_fmts
, enum AVPixelFormat
, "pixel formats",
944 AV_PIX_FMT_NONE
, GET_PIX_FMT_NAME
);
945 PRINT_CODEC_SUPPORTED(c
, supported_samplerates
, int, "sample rates", 0,
946 GET_SAMPLE_RATE_NAME
);
947 PRINT_CODEC_SUPPORTED(c
, sample_fmts
, enum AVSampleFormat
, "sample formats",
948 AV_SAMPLE_FMT_NONE
, GET_SAMPLE_FMT_NAME
);
949 PRINT_CODEC_SUPPORTED(c
, channel_layouts
, uint64_t, "channel layouts",
950 0, GET_CH_LAYOUT_DESC
);
953 show_help_children(c
->priv_class
,
954 AV_OPT_FLAG_ENCODING_PARAM
|
955 AV_OPT_FLAG_DECODING_PARAM
);
959 static char get_media_type_char(enum AVMediaType type
)
962 case AVMEDIA_TYPE_VIDEO
: return 'V';
963 case AVMEDIA_TYPE_AUDIO
: return 'A';
964 case AVMEDIA_TYPE_SUBTITLE
: return 'S';
969 static const AVCodec
*next_codec_for_id(enum AVCodecID id
, const AVCodec
*prev
,
972 while ((prev
= av_codec_next(prev
))) {
973 if (prev
->id
== id
&&
974 (encoder
? av_codec_is_encoder(prev
) : av_codec_is_decoder(prev
)))
980 static void print_codecs_for_id(enum AVCodecID id
, int encoder
)
982 const AVCodec
*codec
= NULL
;
984 printf(" (%s: ", encoder
? "encoders" : "decoders");
986 while ((codec
= next_codec_for_id(id
, codec
, encoder
)))
987 printf("%s ", codec
->name
);
992 int show_codecs(void *optctx
, const char *opt
, const char *arg
)
994 const AVCodecDescriptor
*desc
= NULL
;
997 " D..... = Decoding supported\n"
998 " .E.... = Encoding supported\n"
999 " ..V... = Video codec\n"
1000 " ..A... = Audio codec\n"
1001 " ..S... = Subtitle codec\n"
1002 " ...I.. = Intra frame-only codec\n"
1003 " ....L. = Lossy compression\n"
1004 " .....S = Lossless compression\n"
1006 while ((desc
= avcodec_descriptor_next(desc
))) {
1007 const AVCodec
*codec
= NULL
;
1009 printf(avcodec_find_decoder(desc
->id
) ? "D" : ".");
1010 printf(avcodec_find_encoder(desc
->id
) ? "E" : ".");
1012 printf("%c", get_media_type_char(desc
->type
));
1013 printf((desc
->props
& AV_CODEC_PROP_INTRA_ONLY
) ? "I" : ".");
1014 printf((desc
->props
& AV_CODEC_PROP_LOSSY
) ? "L" : ".");
1015 printf((desc
->props
& AV_CODEC_PROP_LOSSLESS
) ? "S" : ".");
1017 printf(" %-20s %s", desc
->name
, desc
->long_name
? desc
->long_name
: "");
1019 /* print decoders/encoders when there's more than one or their
1020 * names are different from codec name */
1021 while ((codec
= next_codec_for_id(desc
->id
, codec
, 0))) {
1022 if (strcmp(codec
->name
, desc
->name
)) {
1023 print_codecs_for_id(desc
->id
, 0);
1028 while ((codec
= next_codec_for_id(desc
->id
, codec
, 1))) {
1029 if (strcmp(codec
->name
, desc
->name
)) {
1030 print_codecs_for_id(desc
->id
, 1);
1040 static void print_codecs(int encoder
)
1042 const AVCodecDescriptor
*desc
= NULL
;
1047 " S... = Subtitle\n"
1048 " .F.. = Frame-level multithreading\n"
1049 " ..S. = Slice-level multithreading\n"
1050 " ...X = Codec is experimental\n"
1052 encoder
? "Encoders" : "Decoders");
1053 while ((desc
= avcodec_descriptor_next(desc
))) {
1054 const AVCodec
*codec
= NULL
;
1056 while ((codec
= next_codec_for_id(desc
->id
, codec
, encoder
))) {
1057 printf("%c", get_media_type_char(desc
->type
));
1058 printf((codec
->capabilities
& CODEC_CAP_FRAME_THREADS
) ? "F" : ".");
1059 printf((codec
->capabilities
& CODEC_CAP_SLICE_THREADS
) ? "S" : ".");
1060 printf((codec
->capabilities
& CODEC_CAP_EXPERIMENTAL
) ? "X" : ".");
1062 printf(" %-20s %s", codec
->name
, codec
->long_name
? codec
->long_name
: "");
1063 if (strcmp(codec
->name
, desc
->name
))
1064 printf(" (codec %s)", desc
->name
);
1071 int show_decoders(void *optctx
, const char *opt
, const char *arg
)
1077 int show_encoders(void *optctx
, const char *opt
, const char *arg
)
1083 int show_bsfs(void *optctx
, const char *opt
, const char *arg
)
1085 AVBitStreamFilter
*bsf
= NULL
;
1087 printf("Bitstream filters:\n");
1088 while ((bsf
= av_bitstream_filter_next(bsf
)))
1089 printf("%s\n", bsf
->name
);
1094 int show_protocols(void *optctx
, const char *opt
, const char *arg
)
1096 void *opaque
= NULL
;
1099 printf("Supported file protocols:\n"
1101 while ((name
= avio_enum_protocols(&opaque
, 0)))
1102 printf("%s\n", name
);
1103 printf("Output:\n");
1104 while ((name
= avio_enum_protocols(&opaque
, 1)))
1105 printf("%s\n", name
);
1109 int show_filters(void *optctx
, const char *opt
, const char *arg
)
1111 AVFilter
av_unused(**filter
) = NULL
;
1113 printf("Filters:\n");
1115 while ((filter
= av_filter_next(filter
)) && *filter
)
1116 printf("%-16s %s\n", (*filter
)->name
, (*filter
)->description
);
1121 int show_pix_fmts(void *optctx
, const char *opt
, const char *arg
)
1123 const AVPixFmtDescriptor
*pix_desc
= NULL
;
1125 printf("Pixel formats:\n"
1126 "I.... = Supported Input format for conversion\n"
1127 ".O... = Supported Output format for conversion\n"
1128 "..H.. = Hardware accelerated format\n"
1129 "...P. = Paletted format\n"
1130 "....B = Bitstream format\n"
1131 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
1135 # define sws_isSupportedInput(x) 0
1136 # define sws_isSupportedOutput(x) 0
1139 while ((pix_desc
= av_pix_fmt_desc_next(pix_desc
))) {
1140 enum AVPixelFormat pix_fmt
= av_pix_fmt_desc_get_id(pix_desc
);
1141 printf("%c%c%c%c%c %-16s %d %2d\n",
1142 sws_isSupportedInput (pix_fmt
) ? 'I' : '.',
1143 sws_isSupportedOutput(pix_fmt
) ? 'O' : '.',
1144 pix_desc
->flags
& PIX_FMT_HWACCEL
? 'H' : '.',
1145 pix_desc
->flags
& PIX_FMT_PAL
? 'P' : '.',
1146 pix_desc
->flags
& PIX_FMT_BITSTREAM
? 'B' : '.',
1148 pix_desc
->nb_components
,
1149 av_get_bits_per_pixel(pix_desc
));
1154 int show_sample_fmts(void *optctx
, const char *opt
, const char *arg
)
1158 for (i
= -1; i
< AV_SAMPLE_FMT_NB
; i
++)
1159 printf("%s\n", av_get_sample_fmt_string(fmt_str
, sizeof(fmt_str
), i
));
1163 static void show_help_codec(const char *name
, int encoder
)
1165 const AVCodecDescriptor
*desc
;
1166 const AVCodec
*codec
;
1169 av_log(NULL
, AV_LOG_ERROR
, "No codec name specified.\n");
1173 codec
= encoder
? avcodec_find_encoder_by_name(name
) :
1174 avcodec_find_decoder_by_name(name
);
1178 else if ((desc
= avcodec_descriptor_get_by_name(name
))) {
1181 while ((codec
= next_codec_for_id(desc
->id
, codec
, encoder
))) {
1187 av_log(NULL
, AV_LOG_ERROR
, "Codec '%s' is known to Libav, "
1188 "but no %s for it are available. Libav might need to be "
1189 "recompiled with additional external libraries.\n",
1190 name
, encoder
? "encoders" : "decoders");
1193 av_log(NULL
, AV_LOG_ERROR
, "Codec '%s' is not recognized by Libav.\n",
1198 static void show_help_demuxer(const char *name
)
1200 const AVInputFormat
*fmt
= av_find_input_format(name
);
1203 av_log(NULL
, AV_LOG_ERROR
, "Unknown format '%s'.\n", name
);
1207 printf("Demuxer %s [%s]:\n", fmt
->name
, fmt
->long_name
);
1209 if (fmt
->extensions
)
1210 printf(" Common extensions: %s.\n", fmt
->extensions
);
1212 if (fmt
->priv_class
)
1213 show_help_children(fmt
->priv_class
, AV_OPT_FLAG_DECODING_PARAM
);
1216 static void show_help_muxer(const char *name
)
1218 const AVCodecDescriptor
*desc
;
1219 const AVOutputFormat
*fmt
= av_guess_format(name
, NULL
, NULL
);
1222 av_log(NULL
, AV_LOG_ERROR
, "Unknown format '%s'.\n", name
);
1226 printf("Muxer %s [%s]:\n", fmt
->name
, fmt
->long_name
);
1228 if (fmt
->extensions
)
1229 printf(" Common extensions: %s.\n", fmt
->extensions
);
1231 printf(" Mime type: %s.\n", fmt
->mime_type
);
1232 if (fmt
->video_codec
!= AV_CODEC_ID_NONE
&&
1233 (desc
= avcodec_descriptor_get(fmt
->video_codec
))) {
1234 printf(" Default video codec: %s.\n", desc
->name
);
1236 if (fmt
->audio_codec
!= AV_CODEC_ID_NONE
&&
1237 (desc
= avcodec_descriptor_get(fmt
->audio_codec
))) {
1238 printf(" Default audio codec: %s.\n", desc
->name
);
1240 if (fmt
->subtitle_codec
!= AV_CODEC_ID_NONE
&&
1241 (desc
= avcodec_descriptor_get(fmt
->subtitle_codec
))) {
1242 printf(" Default subtitle codec: %s.\n", desc
->name
);
1245 if (fmt
->priv_class
)
1246 show_help_children(fmt
->priv_class
, AV_OPT_FLAG_ENCODING_PARAM
);
1249 int show_help(void *optctx
, const char *opt
, const char *arg
)
1252 av_log_set_callback(log_callback_help
);
1254 topic
= av_strdup(arg
? arg
: "");
1255 par
= strchr(topic
, '=');
1260 show_help_default(topic
, par
);
1261 } else if (!strcmp(topic
, "decoder")) {
1262 show_help_codec(par
, 0);
1263 } else if (!strcmp(topic
, "encoder")) {
1264 show_help_codec(par
, 1);
1265 } else if (!strcmp(topic
, "demuxer")) {
1266 show_help_demuxer(par
);
1267 } else if (!strcmp(topic
, "muxer")) {
1268 show_help_muxer(par
);
1270 show_help_default(topic
, par
);
1277 int read_yesno(void)
1280 int yesno
= (toupper(c
) == 'Y');
1282 while (c
!= '\n' && c
!= EOF
)
1288 int cmdutils_read_file(const char *filename
, char **bufptr
, size_t *size
)
1291 FILE *f
= fopen(filename
, "rb");
1294 av_log(NULL
, AV_LOG_ERROR
, "Cannot read file '%s': %s\n", filename
,
1296 return AVERROR(errno
);
1298 fseek(f
, 0, SEEK_END
);
1300 fseek(f
, 0, SEEK_SET
);
1301 *bufptr
= av_malloc(*size
+ 1);
1303 av_log(NULL
, AV_LOG_ERROR
, "Could not allocate file buffer\n");
1305 return AVERROR(ENOMEM
);
1307 ret
= fread(*bufptr
, 1, *size
, f
);
1311 av_log(NULL
, AV_LOG_ERROR
, "Error while reading file '%s': %s\n",
1312 filename
, strerror(errno
));
1313 ret
= AVERROR(errno
);
1318 (*bufptr
)[(*size
)++] = '\0';
1325 void init_pts_correction(PtsCorrectionContext
*ctx
)
1327 ctx
->num_faulty_pts
= ctx
->num_faulty_dts
= 0;
1328 ctx
->last_pts
= ctx
->last_dts
= INT64_MIN
;
1331 int64_t guess_correct_pts(PtsCorrectionContext
*ctx
, int64_t reordered_pts
,
1334 int64_t pts
= AV_NOPTS_VALUE
;
1336 if (dts
!= AV_NOPTS_VALUE
) {
1337 ctx
->num_faulty_dts
+= dts
<= ctx
->last_dts
;
1338 ctx
->last_dts
= dts
;
1340 if (reordered_pts
!= AV_NOPTS_VALUE
) {
1341 ctx
->num_faulty_pts
+= reordered_pts
<= ctx
->last_pts
;
1342 ctx
->last_pts
= reordered_pts
;
1344 if ((ctx
->num_faulty_pts
<=ctx
->num_faulty_dts
|| dts
== AV_NOPTS_VALUE
)
1345 && reordered_pts
!= AV_NOPTS_VALUE
)
1346 pts
= reordered_pts
;
1353 FILE *get_preset_file(char *filename
, size_t filename_size
,
1354 const char *preset_name
, int is_path
,
1355 const char *codec_name
)
1359 const char *base
[3] = { getenv("AVCONV_DATADIR"),
1364 av_strlcpy(filename
, preset_name
, filename_size
);
1365 f
= fopen(filename
, "r");
1367 for (i
= 0; i
< 3 && !f
; i
++) {
1370 snprintf(filename
, filename_size
, "%s%s/%s.avpreset", base
[i
],
1371 i
!= 1 ? "" : "/.avconv", preset_name
);
1372 f
= fopen(filename
, "r");
1373 if (!f
&& codec_name
) {
1374 snprintf(filename
, filename_size
,
1375 "%s%s/%s-%s.avpreset",
1376 base
[i
], i
!= 1 ? "" : "/.avconv", codec_name
,
1378 f
= fopen(filename
, "r");
1386 int check_stream_specifier(AVFormatContext
*s
, AVStream
*st
, const char *spec
)
1388 if (*spec
<= '9' && *spec
>= '0') /* opt:index */
1389 return strtol(spec
, NULL
, 0) == st
->index
;
1390 else if (*spec
== 'v' || *spec
== 'a' || *spec
== 's' || *spec
== 'd' ||
1391 *spec
== 't') { /* opt:[vasdt] */
1392 enum AVMediaType type
;
1395 case 'v': type
= AVMEDIA_TYPE_VIDEO
; break;
1396 case 'a': type
= AVMEDIA_TYPE_AUDIO
; break;
1397 case 's': type
= AVMEDIA_TYPE_SUBTITLE
; break;
1398 case 'd': type
= AVMEDIA_TYPE_DATA
; break;
1399 case 't': type
= AVMEDIA_TYPE_ATTACHMENT
; break;
1400 default: av_assert0(0);
1402 if (type
!= st
->codec
->codec_type
)
1404 if (*spec
++ == ':') { /* possibly followed by :index */
1405 int i
, index
= strtol(spec
, NULL
, 0);
1406 for (i
= 0; i
< s
->nb_streams
; i
++)
1407 if (s
->streams
[i
]->codec
->codec_type
== type
&& index
-- == 0)
1408 return i
== st
->index
;
1412 } else if (*spec
== 'p' && *(spec
+ 1) == ':') {
1416 prog_id
= strtol(spec
, &endptr
, 0);
1417 for (i
= 0; i
< s
->nb_programs
; i
++) {
1418 if (s
->programs
[i
]->id
!= prog_id
)
1421 if (*endptr
++ == ':') {
1422 int stream_idx
= strtol(endptr
, NULL
, 0);
1423 return stream_idx
>= 0 &&
1424 stream_idx
< s
->programs
[i
]->nb_stream_indexes
&&
1425 st
->index
== s
->programs
[i
]->stream_index
[stream_idx
];
1428 for (j
= 0; j
< s
->programs
[i
]->nb_stream_indexes
; j
++)
1429 if (st
->index
== s
->programs
[i
]->stream_index
[j
])
1433 } else if (!*spec
) /* empty specifier, matches everything */
1436 av_log(s
, AV_LOG_ERROR
, "Invalid stream specifier: %s.\n", spec
);
1437 return AVERROR(EINVAL
);
1440 AVDictionary
*filter_codec_opts(AVDictionary
*opts
, enum AVCodecID codec_id
,
1441 AVFormatContext
*s
, AVStream
*st
, AVCodec
*codec
)
1443 AVDictionary
*ret
= NULL
;
1444 AVDictionaryEntry
*t
= NULL
;
1445 int flags
= s
->oformat
? AV_OPT_FLAG_ENCODING_PARAM
1446 : AV_OPT_FLAG_DECODING_PARAM
;
1448 const AVClass
*cc
= avcodec_get_class();
1451 codec
= s
->oformat
? avcodec_find_encoder(codec_id
)
1452 : avcodec_find_decoder(codec_id
);
1456 switch (codec
->type
) {
1457 case AVMEDIA_TYPE_VIDEO
:
1459 flags
|= AV_OPT_FLAG_VIDEO_PARAM
;
1461 case AVMEDIA_TYPE_AUDIO
:
1463 flags
|= AV_OPT_FLAG_AUDIO_PARAM
;
1465 case AVMEDIA_TYPE_SUBTITLE
:
1467 flags
|= AV_OPT_FLAG_SUBTITLE_PARAM
;
1471 while (t
= av_dict_get(opts
, "", t
, AV_DICT_IGNORE_SUFFIX
)) {
1472 char *p
= strchr(t
->key
, ':');
1474 /* check stream specification in opt name */
1476 switch (check_stream_specifier(s
, st
, p
+ 1)) {
1477 case 1: *p
= 0; break;
1479 default: return NULL
;
1482 if (av_opt_find(&cc
, t
->key
, NULL
, flags
, AV_OPT_SEARCH_FAKE_OBJ
) ||
1483 (codec
&& codec
->priv_class
&&
1484 av_opt_find(&codec
->priv_class
, t
->key
, NULL
, flags
,
1485 AV_OPT_SEARCH_FAKE_OBJ
)))
1486 av_dict_set(&ret
, t
->key
, t
->value
, 0);
1487 else if (t
->key
[0] == prefix
&&
1488 av_opt_find(&cc
, t
->key
+ 1, NULL
, flags
,
1489 AV_OPT_SEARCH_FAKE_OBJ
))
1490 av_dict_set(&ret
, t
->key
+ 1, t
->value
, 0);
1498 AVDictionary
**setup_find_stream_info_opts(AVFormatContext
*s
,
1499 AVDictionary
*codec_opts
)
1502 AVDictionary
**opts
;
1506 opts
= av_mallocz(s
->nb_streams
* sizeof(*opts
));
1508 av_log(NULL
, AV_LOG_ERROR
,
1509 "Could not alloc memory for stream options.\n");
1512 for (i
= 0; i
< s
->nb_streams
; i
++)
1513 opts
[i
] = filter_codec_opts(codec_opts
, s
->streams
[i
]->codec
->codec_id
,
1514 s
, s
->streams
[i
], NULL
);
1518 void *grow_array(void *array
, int elem_size
, int *size
, int new_size
)
1520 if (new_size
>= INT_MAX
/ elem_size
) {
1521 av_log(NULL
, AV_LOG_ERROR
, "Array too big.\n");
1524 if (*size
< new_size
) {
1525 uint8_t *tmp
= av_realloc(array
, new_size
*elem_size
);
1527 av_log(NULL
, AV_LOG_ERROR
, "Could not alloc buffer.\n");
1530 memset(tmp
+ *size
*elem_size
, 0, (new_size
-*size
) * elem_size
);
1537 static int alloc_buffer(FrameBuffer
**pool
, AVCodecContext
*s
, FrameBuffer
**pbuf
)
1539 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(s
->pix_fmt
);
1543 int h_chroma_shift
, v_chroma_shift
;
1544 int edge
= 32; // XXX should be avcodec_get_edge_width(), but that fails on svq1
1545 int w
= s
->width
, h
= s
->height
;
1548 return AVERROR(EINVAL
);
1549 pixel_size
= desc
->comp
[0].step_minus1
+ 1;
1551 buf
= av_mallocz(sizeof(*buf
));
1553 return AVERROR(ENOMEM
);
1555 if (!(s
->flags
& CODEC_FLAG_EMU_EDGE
)) {
1560 avcodec_align_dimensions(s
, &w
, &h
);
1561 if ((ret
= av_image_alloc(buf
->base
, buf
->linesize
, w
, h
,
1562 s
->pix_fmt
, 32)) < 0) {
1566 /* XXX this shouldn't be needed, but some tests break without this line
1567 * those decoders are buggy and need to be fixed.
1568 * the following tests fail:
1569 * cdgraphics, ansi, aasc, fraps-v1, qtrle-1bit
1571 memset(buf
->base
[0], 128, ret
);
1573 av_pix_fmt_get_chroma_sub_sample(s
->pix_fmt
,
1574 &h_chroma_shift
, &v_chroma_shift
);
1576 for (i
= 0; i
< FF_ARRAY_ELEMS(buf
->data
); i
++) {
1577 const int h_shift
= i
==0 ? 0 : h_chroma_shift
;
1578 const int v_shift
= i
==0 ? 0 : v_chroma_shift
;
1579 if (s
->flags
& CODEC_FLAG_EMU_EDGE
)
1580 buf
->data
[i
] = buf
->base
[i
];
1581 else if (buf
->base
[i
])
1582 buf
->data
[i
] = buf
->base
[i
] +
1583 FFALIGN((buf
->linesize
[i
]*edge
>> v_shift
) +
1584 (pixel_size
*edge
>> h_shift
), 32);
1588 buf
->pix_fmt
= s
->pix_fmt
;
1595 int codec_get_buffer(AVCodecContext
*s
, AVFrame
*frame
)
1597 FrameBuffer
**pool
= s
->opaque
;
1601 if (!*pool
&& (ret
= alloc_buffer(pool
, s
, pool
)) < 0)
1607 if (buf
->w
!= s
->width
|| buf
->h
!= s
->height
|| buf
->pix_fmt
!= s
->pix_fmt
) {
1608 av_freep(&buf
->base
[0]);
1610 if ((ret
= alloc_buffer(pool
, s
, &buf
)) < 0)
1615 frame
->opaque
= buf
;
1616 frame
->type
= FF_BUFFER_TYPE_USER
;
1617 frame
->extended_data
= frame
->data
;
1619 for (i
= 0; i
< FF_ARRAY_ELEMS(buf
->data
); i
++) {
1620 frame
->base
[i
] = buf
->base
[i
]; // XXX h264.c uses base though it shouldn't
1621 frame
->data
[i
] = buf
->data
[i
];
1622 frame
->linesize
[i
] = buf
->linesize
[i
];
1628 static void unref_buffer(FrameBuffer
*buf
)
1630 FrameBuffer
**pool
= buf
->pool
;
1632 av_assert0(buf
->refcount
);
1634 if (!buf
->refcount
) {
1640 void codec_release_buffer(AVCodecContext
*s
, AVFrame
*frame
)
1642 FrameBuffer
*buf
= frame
->opaque
;
1645 for (i
= 0; i
< FF_ARRAY_ELEMS(frame
->data
); i
++)
1646 frame
->data
[i
] = NULL
;
1651 void filter_release_buffer(AVFilterBuffer
*fb
)
1653 FrameBuffer
*buf
= fb
->priv
;
1658 void free_buffer_pool(FrameBuffer
**pool
)
1660 FrameBuffer
*buf
= *pool
;
1663 av_freep(&buf
->base
[0]);