2 * Various utilities for command line tools
3 * Copyright (c) 2000-2003 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
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 "compat/va_copy.h"
33 #include "libavformat/avformat.h"
34 #include "libavfilter/avfilter.h"
35 #include "libavdevice/avdevice.h"
36 #include "libavresample/avresample.h"
37 #include "libswscale/swscale.h"
38 #include "libswresample/swresample.h"
40 #include "libpostproc/postprocess.h"
42 #include "libavutil/avassert.h"
43 #include "libavutil/avstring.h"
44 #include "libavutil/bprint.h"
45 #include "libavutil/mathematics.h"
46 #include "libavutil/imgutils.h"
47 #include "libavutil/parseutils.h"
48 #include "libavutil/pixdesc.h"
49 #include "libavutil/eval.h"
50 #include "libavutil/dict.h"
51 #include "libavutil/opt.h"
55 #include "libavformat/network.h"
57 #if HAVE_SYS_RESOURCE_H
59 #include <sys/resource.h>
62 static int init_report(const char *env);
64 struct SwsContext *sws_opts;
66 AVDictionary *format_opts, *codec_opts;
68 const int this_year = 2012;
70 static FILE *report_file;
76 sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
80 swr_opts = swr_alloc();
83 void uninit_opts(void)
86 sws_freeContext(sws_opts);
93 av_dict_free(&format_opts);
94 av_dict_free(&codec_opts);
97 void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
99 vfprintf(stdout, fmt, vl);
102 static void log_callback_report(void *ptr, int level, const char *fmt, va_list vl)
106 static int print_prefix = 1;
109 av_log_default_callback(ptr, level, fmt, vl);
110 av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
112 fputs(line, report_file);
116 double parse_number_or_die(const char *context, const char *numstr, int type,
117 double min, double max)
121 double d = av_strtod(numstr, &tail);
123 error = "Expected number for %s but found: %s\n";
124 else if (d < min || d > max)
125 error = "The value for %s was %s which is not within %f - %f\n";
126 else if (type == OPT_INT64 && (int64_t)d != d)
127 error = "Expected int64 for %s but found %s\n";
128 else if (type == OPT_INT && (int)d != d)
129 error = "Expected int for %s but found %s\n";
132 av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
137 int64_t parse_time_or_die(const char *context, const char *timestr,
141 if (av_parse_time(&us, timestr, is_duration) < 0) {
142 av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
143 is_duration ? "duration" : "date", context, timestr);
149 void show_help_options(const OptionDef *options, const char *msg, int req_flags,
150 int rej_flags, int alt_flags)
156 for (po = options; po->name != NULL; po++) {
159 if (((po->flags & req_flags) != req_flags) ||
160 (alt_flags && !(po->flags & alt_flags)) ||
161 (po->flags & rej_flags))
168 av_strlcpy(buf, po->name, sizeof(buf));
170 av_strlcat(buf, " ", sizeof(buf));
171 av_strlcat(buf, po->argname, sizeof(buf));
173 printf("-%-17s %s\n", buf, po->help);
178 void show_help_children(const AVClass *class, int flags)
180 const AVClass *child = NULL;
182 av_opt_show2(&class, NULL, flags, 0);
186 while (child = av_opt_child_class_next(class, child))
187 show_help_children(child, flags);
190 static const OptionDef *find_option(const OptionDef *po, const char *name)
192 const char *p = strchr(name, ':');
193 int len = p ? p - name : strlen(name);
195 while (po->name != NULL) {
196 if (!strncmp(name, po->name, len) && strlen(po->name) == len)
203 #if defined(_WIN32) && !defined(__MINGW32CE__)
205 #include <shellapi.h>
206 /* Will be leaked on exit */
207 static char** win32_argv_utf8 = NULL;
208 static int win32_argc = 0;
211 * Prepare command line arguments for executable.
212 * For Windows - perform wide-char to UTF-8 conversion.
213 * Input arguments should be main() function arguments.
214 * @param argc_ptr Arguments number (including executable)
215 * @param argv_ptr Arguments list.
217 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
221 int i, buffsize = 0, offset = 0;
223 if (win32_argv_utf8) {
224 *argc_ptr = win32_argc;
225 *argv_ptr = win32_argv_utf8;
230 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
231 if (win32_argc <= 0 || !argv_w)
234 /* determine the UTF-8 buffer size (including NULL-termination symbols) */
235 for (i = 0; i < win32_argc; i++)
236 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
237 NULL, 0, NULL, NULL);
239 win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
240 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
241 if (win32_argv_utf8 == NULL) {
246 for (i = 0; i < win32_argc; i++) {
247 win32_argv_utf8[i] = &argstr_flat[offset];
248 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
249 &argstr_flat[offset],
250 buffsize - offset, NULL, NULL);
252 win32_argv_utf8[i] = NULL;
255 *argc_ptr = win32_argc;
256 *argv_ptr = win32_argv_utf8;
259 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
263 #endif /* WIN32 && !__MINGW32CE__ */
265 int parse_option(void *optctx, const char *opt, const char *arg,
266 const OptionDef *options)
273 po = find_option(options, opt);
274 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
275 /* handle 'no' bool option */
276 po = find_option(options, opt + 2);
277 if ((po->name && (po->flags & OPT_BOOL)))
281 po = find_option(options, "default");
283 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
284 return AVERROR(EINVAL);
286 if (po->flags & HAS_ARG && !arg) {
287 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
288 return AVERROR(EINVAL);
291 /* new-style options contain an offset into optctx, old-style address of
293 dst = po->flags & (OPT_OFFSET | OPT_SPEC) ? (uint8_t *)optctx + po->u.off
296 if (po->flags & OPT_SPEC) {
297 SpecifierOpt **so = dst;
298 char *p = strchr(opt, ':');
300 dstcount = (int *)(so + 1);
301 *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
302 (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
303 dst = &(*so)[*dstcount - 1].u;
306 if (po->flags & OPT_STRING) {
308 str = av_strdup(arg);
311 } else if (po->flags & OPT_BOOL) {
312 *(int *)dst = bool_val;
313 } else if (po->flags & OPT_INT) {
314 *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
315 } else if (po->flags & OPT_INT64) {
316 *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
317 } else if (po->flags & OPT_TIME) {
318 *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
319 } else if (po->flags & OPT_FLOAT) {
320 *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
321 } else if (po->flags & OPT_DOUBLE) {
322 *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
323 } else if (po->u.func_arg) {
324 int ret = po->u.func_arg(optctx, opt, arg);
326 av_log(NULL, AV_LOG_ERROR,
327 "Failed to set value '%s' for option '%s'\n", arg, opt);
331 if (po->flags & OPT_EXIT)
333 return !!(po->flags & HAS_ARG);
336 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
337 void (*parse_arg_function)(void *, const char*))
340 int optindex, handleoptions = 1, ret;
342 /* perform system-dependent conversions for arguments list */
343 prepare_app_arguments(&argc, &argv);
347 while (optindex < argc) {
348 opt = argv[optindex++];
350 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
351 if (opt[1] == '-' && opt[2] == '\0') {
357 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
361 if (parse_arg_function)
362 parse_arg_function(optctx, opt);
367 int locate_option(int argc, char **argv, const OptionDef *options,
373 for (i = 1; i < argc; i++) {
374 const char *cur_opt = argv[i];
376 if (*cur_opt++ != '-')
379 po = find_option(options, cur_opt);
380 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
381 po = find_option(options, cur_opt + 2);
383 if ((!po->name && !strcmp(cur_opt, optname)) ||
384 (po->name && !strcmp(optname, po->name)))
387 if (po->flags & HAS_ARG)
393 static void dump_argument(const char *a)
395 const unsigned char *p;
398 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
399 *p == '_' || (*p >= 'a' && *p <= 'z')))
402 fputs(a, report_file);
405 fputc('"', report_file);
406 for (p = a; *p; p++) {
407 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
408 fprintf(report_file, "\\%c", *p);
409 else if (*p < ' ' || *p > '~')
410 fprintf(report_file, "\\x%02x", *p);
412 fputc(*p, report_file);
414 fputc('"', report_file);
417 void parse_loglevel(int argc, char **argv, const OptionDef *options)
419 int idx = locate_option(argc, argv, options, "loglevel");
422 idx = locate_option(argc, argv, options, "v");
423 if (idx && argv[idx + 1])
424 opt_loglevel(NULL, "loglevel", argv[idx + 1]);
425 idx = locate_option(argc, argv, options, "report");
426 if ((env = getenv("FFREPORT")) || idx) {
430 fprintf(report_file, "Command line:\n");
431 for (i = 0; i < argc; i++) {
432 dump_argument(argv[i]);
433 fputc(i < argc - 1 ? ' ' : '\n', report_file);
440 #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
441 int opt_default(void *optctx, const char *opt, const char *arg)
445 char opt_stripped[128];
447 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
448 const AVClass *sc, *swr_class;
450 if (!(p = strchr(opt, ':')))
451 p = opt + strlen(opt);
452 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
454 if ((o = av_opt_find(&cc, opt_stripped, NULL, 0,
455 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
456 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
457 (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) {
458 av_dict_set(&codec_opts, opt, arg, FLAGS);
461 if ((o = av_opt_find(&fc, opt, NULL, 0,
462 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
463 av_dict_set(&format_opts, opt, arg, FLAGS);
465 av_log(NULL, AV_LOG_VERBOSE, "Routing %s to codec and muxer layer\n", opt);
469 sc = sws_get_class();
470 if (!consumed && av_opt_find(&sc, opt, NULL, 0,
471 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
472 // XXX we only support sws_flags, not arbitrary sws options
473 int ret = av_opt_set(sws_opts, opt, arg, 0);
475 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
481 #if CONFIG_SWRESAMPLE
482 swr_class = swr_get_class();
483 if (!consumed && av_opt_find(&swr_class, opt, NULL, 0,
484 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
485 int ret = av_opt_set(swr_opts, opt, arg, 0);
487 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
496 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
497 return AVERROR_OPTION_NOT_FOUND;
500 int opt_loglevel(void *optctx, const char *opt, const char *arg)
502 const struct { const char *name; int level; } log_levels[] = {
503 { "quiet" , AV_LOG_QUIET },
504 { "panic" , AV_LOG_PANIC },
505 { "fatal" , AV_LOG_FATAL },
506 { "error" , AV_LOG_ERROR },
507 { "warning", AV_LOG_WARNING },
508 { "info" , AV_LOG_INFO },
509 { "verbose", AV_LOG_VERBOSE },
510 { "debug" , AV_LOG_DEBUG },
516 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
517 if (!strcmp(log_levels[i].name, arg)) {
518 av_log_set_level(log_levels[i].level);
523 level = strtol(arg, &tail, 10);
525 av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
526 "Possible levels are numbers or:\n", arg);
527 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
528 av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
531 av_log_set_level(level);
535 static void expand_filename_template(AVBPrint *bp, const char *template,
540 while ((c = *(template++))) {
542 if (!(c = *(template++)))
546 av_bprintf(bp, "%s", program_name);
549 av_bprintf(bp, "%04d%02d%02d-%02d%02d%02d",
550 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
551 tm->tm_hour, tm->tm_min, tm->tm_sec);
554 av_bprint_chars(bp, c, 1);
558 av_bprint_chars(bp, c, 1);
563 static int init_report(const char *env)
565 char *filename_template = NULL;
572 if (report_file) /* already opened */
575 tm = localtime(&now);
577 while (env && *env) {
578 if ((ret = av_opt_get_key_value(&env, "=", ":", 0, &key, &val)) < 0) {
580 av_log(NULL, AV_LOG_ERROR,
581 "Failed to parse FFREPORT environment variable: %s\n",
588 if (!strcmp(key, "file")) {
589 av_free(filename_template);
590 filename_template = val;
593 av_log(NULL, AV_LOG_ERROR, "Unknown key '%s' in FFREPORT\n", key);
599 av_bprint_init(&filename, 0, 1);
600 expand_filename_template(&filename,
601 av_x_if_null(filename_template, "%p-%t.log"), tm);
602 av_free(filename_template);
603 if (!av_bprint_is_complete(&filename)) {
604 av_log(NULL, AV_LOG_ERROR, "Out of memory building report file name\n");
605 return AVERROR(ENOMEM);
608 report_file = fopen(filename.str, "w");
610 av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
611 filename.str, strerror(errno));
612 return AVERROR(errno);
614 av_log_set_callback(log_callback_report);
615 av_log(NULL, AV_LOG_INFO,
616 "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
617 "Report written to \"%s\"\n",
619 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
620 tm->tm_hour, tm->tm_min, tm->tm_sec,
622 av_log_set_level(FFMAX(av_log_get_level(), AV_LOG_VERBOSE));
623 av_bprint_finalize(&filename, NULL);
627 int opt_report(const char *opt)
629 return init_report(NULL);
632 int opt_max_alloc(void *optctx, const char *opt, const char *arg)
637 max = strtol(arg, &tail, 10);
639 av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
646 int opt_cpuflags(void *optctx, const char *opt, const char *arg)
649 unsigned flags = av_get_cpu_flags();
651 if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
654 av_force_cpu_flags(flags);
658 int opt_codec_debug(void *optctx, const char *opt, const char *arg)
660 av_log_set_level(AV_LOG_DEBUG);
661 return opt_default(NULL, opt, arg);
664 int opt_timelimit(void *optctx, const char *opt, const char *arg)
667 int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
668 struct rlimit rl = { lim, lim + 1 };
669 if (setrlimit(RLIMIT_CPU, &rl))
672 av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
677 void print_error(const char *filename, int err)
680 const char *errbuf_ptr = errbuf;
682 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
683 errbuf_ptr = strerror(AVUNERROR(err));
684 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
687 static int warned_cfg = 0;
690 #define SHOW_VERSION 2
691 #define SHOW_CONFIG 4
692 #define SHOW_COPYRIGHT 8
694 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
695 if (CONFIG_##LIBNAME) { \
696 const char *indent = flags & INDENT? " " : ""; \
697 if (flags & SHOW_VERSION) { \
698 unsigned int version = libname##_version(); \
699 av_log(NULL, level, \
700 "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
702 LIB##LIBNAME##_VERSION_MAJOR, \
703 LIB##LIBNAME##_VERSION_MINOR, \
704 LIB##LIBNAME##_VERSION_MICRO, \
705 version >> 16, version >> 8 & 0xff, version & 0xff); \
707 if (flags & SHOW_CONFIG) { \
708 const char *cfg = libname##_configuration(); \
709 if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
711 av_log(NULL, level, \
712 "%sWARNING: library configuration mismatch\n", \
716 av_log(NULL, level, "%s%-11s configuration: %s\n", \
717 indent, #libname, cfg); \
722 static void print_all_libs_info(int flags, int level)
724 PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
725 PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
726 PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
727 PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
728 PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
729 // PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
730 PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
731 PRINT_LIB_INFO(swresample,SWRESAMPLE, flags, level);
733 PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
737 static void print_program_info(int flags, int level)
739 const char *indent = flags & INDENT? " " : "";
741 av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
742 if (flags & SHOW_COPYRIGHT)
743 av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
744 program_birth_year, this_year);
745 av_log(NULL, level, "\n");
746 av_log(NULL, level, "%sbuilt on %s %s with %s\n",
747 indent, __DATE__, __TIME__, CC_IDENT);
749 av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
752 void show_banner(int argc, char **argv, const OptionDef *options)
754 int idx = locate_option(argc, argv, options, "version");
758 print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
759 print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
760 print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
763 int show_version(void *optctx, const char *opt, const char *arg)
765 av_log_set_callback(log_callback_help);
766 print_program_info (0 , AV_LOG_INFO);
767 print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
772 int show_license(void *optctx, const char *opt, const char *arg)
776 "This version of %s has nonfree parts compiled in.\n"
777 "Therefore it is not legally redistributable.\n",
781 "%s is free software; you can redistribute it and/or modify\n"
782 "it under the terms of the GNU General Public License as published by\n"
783 "the Free Software Foundation; either version 3 of the License, or\n"
784 "(at your option) any later version.\n"
786 "%s is distributed in the hope that it will be useful,\n"
787 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
788 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
789 "GNU General Public License for more details.\n"
791 "You should have received a copy of the GNU General Public License\n"
792 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
793 program_name, program_name, program_name );
796 "%s is free software; you can redistribute it and/or modify\n"
797 "it under the terms of the GNU General Public License as published by\n"
798 "the Free Software Foundation; either version 2 of the License, or\n"
799 "(at your option) any later version.\n"
801 "%s is distributed in the hope that it will be useful,\n"
802 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
803 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
804 "GNU General Public License for more details.\n"
806 "You should have received a copy of the GNU General Public License\n"
807 "along with %s; if not, write to the Free Software\n"
808 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
809 program_name, program_name, program_name );
812 "%s is free software; you can redistribute it and/or modify\n"
813 "it under the terms of the GNU Lesser General Public License as published by\n"
814 "the Free Software Foundation; either version 3 of the License, or\n"
815 "(at your option) any later version.\n"
817 "%s is distributed in the hope that it will be useful,\n"
818 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
819 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
820 "GNU Lesser General Public License for more details.\n"
822 "You should have received a copy of the GNU Lesser General Public License\n"
823 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
824 program_name, program_name, program_name );
827 "%s is free software; you can redistribute it and/or\n"
828 "modify it under the terms of the GNU Lesser General Public\n"
829 "License as published by the Free Software Foundation; either\n"
830 "version 2.1 of the License, or (at your option) any later version.\n"
832 "%s is distributed in the hope that it will be useful,\n"
833 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
834 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
835 "Lesser General Public License for more details.\n"
837 "You should have received a copy of the GNU Lesser General Public\n"
838 "License along with %s; if not, write to the Free Software\n"
839 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
840 program_name, program_name, program_name );
846 int show_formats(void *optctx, const char *opt, const char *arg)
848 AVInputFormat *ifmt = NULL;
849 AVOutputFormat *ofmt = NULL;
850 const char *last_name;
852 printf("File formats:\n"
853 " D. = Demuxing supported\n"
854 " .E = Muxing supported\n"
860 const char *name = NULL;
861 const char *long_name = NULL;
863 while ((ofmt = av_oformat_next(ofmt))) {
864 if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
865 strcmp(ofmt->name, last_name) > 0) {
867 long_name = ofmt->long_name;
871 while ((ifmt = av_iformat_next(ifmt))) {
872 if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
873 strcmp(ifmt->name, last_name) > 0) {
875 long_name = ifmt->long_name;
878 if (name && strcmp(ifmt->name, name) == 0)
885 printf(" %s%s %-15s %s\n",
889 long_name ? long_name:" ");
894 #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
895 if (codec->field) { \
896 const type *p = codec->field; \
898 printf(" Supported " list_name ":"); \
899 while (*p != term) { \
901 printf(" %s", name); \
907 static void print_codec(const AVCodec *c)
909 int encoder = av_codec_is_encoder(c);
911 printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
912 c->long_name ? c->long_name : "");
914 if (c->type == AVMEDIA_TYPE_VIDEO) {
915 printf(" Threading capabilities: ");
916 switch (c->capabilities & (CODEC_CAP_FRAME_THREADS |
917 CODEC_CAP_SLICE_THREADS)) {
918 case CODEC_CAP_FRAME_THREADS |
919 CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
920 case CODEC_CAP_FRAME_THREADS: printf("frame"); break;
921 case CODEC_CAP_SLICE_THREADS: printf("slice"); break;
922 default: printf("no"); break;
927 if (c->supported_framerates) {
928 const AVRational *fps = c->supported_framerates;
930 printf(" Supported framerates:");
932 printf(" %d/%d", fps->num, fps->den);
937 PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
938 AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
939 PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
940 GET_SAMPLE_RATE_NAME);
941 PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
942 AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
943 PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
944 0, GET_CH_LAYOUT_DESC);
947 show_help_children(c->priv_class,
948 AV_OPT_FLAG_ENCODING_PARAM |
949 AV_OPT_FLAG_DECODING_PARAM);
953 static char get_media_type_char(enum AVMediaType type)
956 case AVMEDIA_TYPE_VIDEO: return 'V';
957 case AVMEDIA_TYPE_AUDIO: return 'A';
958 case AVMEDIA_TYPE_DATA: return 'D';
959 case AVMEDIA_TYPE_SUBTITLE: return 'S';
960 case AVMEDIA_TYPE_ATTACHMENT:return 'T';
965 static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
968 while ((prev = av_codec_next(prev))) {
969 if (prev->id == id &&
970 (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
976 static int compare_codec_desc(const void *a, const void *b)
978 const AVCodecDescriptor * const *da = a;
979 const AVCodecDescriptor * const *db = b;
981 return (*da)->type != (*db)->type ? (*da)->type - (*db)->type :
982 strcmp((*da)->name, (*db)->name);
985 static unsigned get_codecs_sorted(const AVCodecDescriptor ***rcodecs)
987 const AVCodecDescriptor *desc = NULL;
988 const AVCodecDescriptor **codecs;
989 unsigned nb_codecs = 0, i = 0;
991 while ((desc = avcodec_descriptor_next(desc)))
993 if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs)))) {
994 av_log(0, AV_LOG_ERROR, "Out of memory\n");
998 while ((desc = avcodec_descriptor_next(desc)))
1000 av_assert0(i == nb_codecs);
1001 qsort(codecs, nb_codecs, sizeof(*codecs), compare_codec_desc);
1006 static void print_codecs_for_id(enum AVCodecID id, int encoder)
1008 const AVCodec *codec = NULL;
1010 printf(" (%s: ", encoder ? "encoders" : "decoders");
1012 while ((codec = next_codec_for_id(id, codec, encoder)))
1013 printf("%s ", codec->name);
1018 int show_codecs(void *optctx, const char *opt, const char *arg)
1020 const AVCodecDescriptor **codecs;
1021 unsigned i, nb_codecs = get_codecs_sorted(&codecs);
1024 " D..... = Decoding supported\n"
1025 " .E.... = Encoding supported\n"
1026 " ..V... = Video codec\n"
1027 " ..A... = Audio codec\n"
1028 " ..S... = Subtitle codec\n"
1029 " ...I.. = Intra frame-only codec\n"
1030 " ....L. = Lossy compression\n"
1031 " .....S = Lossless compression\n"
1033 for (i = 0; i < nb_codecs; i++) {
1034 const AVCodecDescriptor *desc = codecs[i];
1035 const AVCodec *codec = NULL;
1038 printf(avcodec_find_decoder(desc->id) ? "D" : ".");
1039 printf(avcodec_find_encoder(desc->id) ? "E" : ".");
1041 printf("%c", get_media_type_char(desc->type));
1042 printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
1043 printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
1044 printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
1046 printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
1048 /* print decoders/encoders when there's more than one or their
1049 * names are different from codec name */
1050 while ((codec = next_codec_for_id(desc->id, codec, 0))) {
1051 if (strcmp(codec->name, desc->name)) {
1052 print_codecs_for_id(desc->id, 0);
1057 while ((codec = next_codec_for_id(desc->id, codec, 1))) {
1058 if (strcmp(codec->name, desc->name)) {
1059 print_codecs_for_id(desc->id, 1);
1070 static void print_codecs(int encoder)
1072 const AVCodecDescriptor **codecs;
1073 unsigned i, nb_codecs = get_codecs_sorted(&codecs);
1078 " S..... = Subtitle\n"
1079 " .F.... = Frame-level multithreading\n"
1080 " ..S... = Slice-level multithreading\n"
1081 " ...X.. = Codec is experimental\n"
1082 " ....B. = Supports draw_horiz_band\n"
1083 " .....D = Supports direct rendering method 1\n"
1085 encoder ? "Encoders" : "Decoders");
1086 for (i = 0; i < nb_codecs; i++) {
1087 const AVCodecDescriptor *desc = codecs[i];
1088 const AVCodec *codec = NULL;
1090 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1091 printf(" %c", get_media_type_char(desc->type));
1092 printf((codec->capabilities & CODEC_CAP_FRAME_THREADS) ? "F" : ".");
1093 printf((codec->capabilities & CODEC_CAP_SLICE_THREADS) ? "S" : ".");
1094 printf((codec->capabilities & CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
1095 printf((codec->capabilities & CODEC_CAP_DRAW_HORIZ_BAND)?"B" : ".");
1096 printf((codec->capabilities & CODEC_CAP_DR1) ? "D" : ".");
1098 printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
1099 if (strcmp(codec->name, desc->name))
1100 printf(" (codec %s)", desc->name);
1108 int show_decoders(void *optctx, const char *opt, const char *arg)
1114 int show_encoders(void *optctx, const char *opt, const char *arg)
1120 int show_bsfs(void *optctx, const char *opt, const char *arg)
1122 AVBitStreamFilter *bsf = NULL;
1124 printf("Bitstream filters:\n");
1125 while ((bsf = av_bitstream_filter_next(bsf)))
1126 printf("%s\n", bsf->name);
1131 int show_protocols(void *optctx, const char *opt, const char *arg)
1133 void *opaque = NULL;
1136 printf("Supported file protocols:\n"
1138 while ((name = avio_enum_protocols(&opaque, 0)))
1139 printf("%s\n", name);
1140 printf("Output:\n");
1141 while ((name = avio_enum_protocols(&opaque, 1)))
1142 printf("%s\n", name);
1146 int show_filters(void *optctx, const char *opt, const char *arg)
1148 AVFilter av_unused(**filter) = NULL;
1149 char descr[64], *descr_cur;
1151 const AVFilterPad *pad;
1153 printf("Filters:\n");
1155 while ((filter = av_filter_next(filter)) && *filter) {
1157 for (i = 0; i < 2; i++) {
1159 *(descr_cur++) = '-';
1160 *(descr_cur++) = '>';
1162 pad = i ? (*filter)->outputs : (*filter)->inputs;
1163 for (j = 0; pad && pad[j].name; j++) {
1164 if (descr_cur >= descr + sizeof(descr) - 4)
1166 *(descr_cur++) = get_media_type_char(pad[j].type);
1169 *(descr_cur++) = '|';
1172 printf("%-16s %-10s %s\n", (*filter)->name, descr, (*filter)->description);
1178 int show_pix_fmts(void *optctx, const char *opt, const char *arg)
1180 const AVPixFmtDescriptor *pix_desc = NULL;
1182 printf("Pixel formats:\n"
1183 "I.... = Supported Input format for conversion\n"
1184 ".O... = Supported Output format for conversion\n"
1185 "..H.. = Hardware accelerated format\n"
1186 "...P. = Paletted format\n"
1187 "....B = Bitstream format\n"
1188 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
1192 # define sws_isSupportedInput(x) 0
1193 # define sws_isSupportedOutput(x) 0
1196 while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
1197 enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
1200 printf("%c%c%c%c%c %-16s %d %2d\n",
1201 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
1202 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
1203 pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
1204 pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
1205 pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
1207 pix_desc->nb_components,
1208 av_get_bits_per_pixel(pix_desc));
1213 int show_layouts(void *optctx, const char *opt, const char *arg)
1217 const char *name, *descr;
1219 printf("Individual channels:\n"
1220 "NAME DESCRIPTION\n");
1221 for (i = 0; i < 63; i++) {
1222 name = av_get_channel_name((uint64_t)1 << i);
1225 descr = av_get_channel_description((uint64_t)1 << i);
1226 printf("%-12s%s\n", name, descr);
1228 printf("\nStandard channel layouts:\n"
1229 "NAME DECOMPOSITION\n");
1230 for (i = 0; !av_get_standard_channel_layout(i, &layout, &name); i++) {
1232 printf("%-12s", name);
1233 for (j = 1; j; j <<= 1)
1235 printf("%s%s", (layout & (j - 1)) ? "+" : "", av_get_channel_name(j));
1242 int show_sample_fmts(void *optctx, const char *opt, const char *arg)
1246 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
1247 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
1251 static void show_help_codec(const char *name, int encoder)
1253 const AVCodecDescriptor *desc;
1254 const AVCodec *codec;
1257 av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
1261 codec = encoder ? avcodec_find_encoder_by_name(name) :
1262 avcodec_find_decoder_by_name(name);
1266 else if ((desc = avcodec_descriptor_get_by_name(name))) {
1269 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1275 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, "
1276 "but no %s for it are available. FFmpeg might need to be "
1277 "recompiled with additional external libraries.\n",
1278 name, encoder ? "encoders" : "decoders");
1281 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n",
1286 static void show_help_demuxer(const char *name)
1288 const AVInputFormat *fmt = av_find_input_format(name);
1291 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1295 printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
1297 if (fmt->extensions)
1298 printf(" Common extensions: %s.\n", fmt->extensions);
1300 if (fmt->priv_class)
1301 show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
1304 static void show_help_muxer(const char *name)
1306 const AVCodecDescriptor *desc;
1307 const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
1310 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1314 printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
1316 if (fmt->extensions)
1317 printf(" Common extensions: %s.\n", fmt->extensions);
1319 printf(" Mime type: %s.\n", fmt->mime_type);
1320 if (fmt->video_codec != AV_CODEC_ID_NONE &&
1321 (desc = avcodec_descriptor_get(fmt->video_codec))) {
1322 printf(" Default video codec: %s.\n", desc->name);
1324 if (fmt->audio_codec != AV_CODEC_ID_NONE &&
1325 (desc = avcodec_descriptor_get(fmt->audio_codec))) {
1326 printf(" Default audio codec: %s.\n", desc->name);
1328 if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
1329 (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
1330 printf(" Default subtitle codec: %s.\n", desc->name);
1333 if (fmt->priv_class)
1334 show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
1337 int show_help(void *optctx, const char *opt, const char *arg)
1340 av_log_set_callback(log_callback_help);
1342 topic = av_strdup(arg ? arg : "");
1343 par = strchr(topic, '=');
1348 show_help_default(topic, par);
1349 } else if (!strcmp(topic, "decoder")) {
1350 show_help_codec(par, 0);
1351 } else if (!strcmp(topic, "encoder")) {
1352 show_help_codec(par, 1);
1353 } else if (!strcmp(topic, "demuxer")) {
1354 show_help_demuxer(par);
1355 } else if (!strcmp(topic, "muxer")) {
1356 show_help_muxer(par);
1358 show_help_default(topic, par);
1365 int read_yesno(void)
1368 int yesno = (toupper(c) == 'Y');
1370 while (c != '\n' && c != EOF)
1376 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
1379 FILE *f = fopen(filename, "rb");
1382 av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
1384 return AVERROR(errno);
1386 fseek(f, 0, SEEK_END);
1388 fseek(f, 0, SEEK_SET);
1389 if (*size == (size_t)-1) {
1390 av_log(NULL, AV_LOG_ERROR, "IO error: %s\n", strerror(errno));
1392 return AVERROR(errno);
1394 *bufptr = av_malloc(*size + 1);
1396 av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
1398 return AVERROR(ENOMEM);
1400 ret = fread(*bufptr, 1, *size, f);
1404 av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
1405 filename, strerror(errno));
1406 ret = AVERROR(errno);
1411 (*bufptr)[(*size)++] = '\0';
1418 FILE *get_preset_file(char *filename, size_t filename_size,
1419 const char *preset_name, int is_path,
1420 const char *codec_name)
1424 const char *base[3] = { getenv("FFMPEG_DATADIR"),
1429 av_strlcpy(filename, preset_name, filename_size);
1430 f = fopen(filename, "r");
1433 char datadir[MAX_PATH], *ls;
1436 if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
1438 for (ls = datadir; ls < datadir + strlen(datadir); ls++)
1439 if (*ls == '\\') *ls = '/';
1441 if (ls = strrchr(datadir, '/'))
1444 strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
1449 for (i = 0; i < 3 && !f; i++) {
1452 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
1453 i != 1 ? "" : "/.ffmpeg", preset_name);
1454 f = fopen(filename, "r");
1455 if (!f && codec_name) {
1456 snprintf(filename, filename_size,
1457 "%s%s/%s-%s.ffpreset",
1458 base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
1460 f = fopen(filename, "r");
1468 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1470 int ret = avformat_match_stream_specifier(s, st, spec);
1472 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1476 AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
1477 AVFormatContext *s, AVStream *st, AVCodec *codec)
1479 AVDictionary *ret = NULL;
1480 AVDictionaryEntry *t = NULL;
1481 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1482 : AV_OPT_FLAG_DECODING_PARAM;
1484 const AVClass *cc = avcodec_get_class();
1487 codec = s->oformat ? avcodec_find_encoder(codec_id)
1488 : avcodec_find_decoder(codec_id);
1492 switch (codec->type) {
1493 case AVMEDIA_TYPE_VIDEO:
1495 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1497 case AVMEDIA_TYPE_AUDIO:
1499 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1501 case AVMEDIA_TYPE_SUBTITLE:
1503 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1507 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
1508 char *p = strchr(t->key, ':');
1510 /* check stream specification in opt name */
1512 switch (check_stream_specifier(s, st, p + 1)) {
1513 case 1: *p = 0; break;
1515 default: return NULL;
1518 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1519 (codec && codec->priv_class &&
1520 av_opt_find(&codec->priv_class, t->key, NULL, flags,
1521 AV_OPT_SEARCH_FAKE_OBJ)))
1522 av_dict_set(&ret, t->key, t->value, 0);
1523 else if (t->key[0] == prefix &&
1524 av_opt_find(&cc, t->key + 1, NULL, flags,
1525 AV_OPT_SEARCH_FAKE_OBJ))
1526 av_dict_set(&ret, t->key + 1, t->value, 0);
1534 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
1535 AVDictionary *codec_opts)
1538 AVDictionary **opts;
1542 opts = av_mallocz(s->nb_streams * sizeof(*opts));
1544 av_log(NULL, AV_LOG_ERROR,
1545 "Could not alloc memory for stream options.\n");
1548 for (i = 0; i < s->nb_streams; i++)
1549 opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
1550 s, s->streams[i], NULL);
1554 void *grow_array(void *array, int elem_size, int *size, int new_size)
1556 if (new_size >= INT_MAX / elem_size) {
1557 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1560 if (*size < new_size) {
1561 uint8_t *tmp = av_realloc(array, new_size*elem_size);
1563 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1566 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
1573 static int alloc_buffer(FrameBuffer **pool, AVCodecContext *s, FrameBuffer **pbuf)
1575 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
1579 int h_chroma_shift, v_chroma_shift;
1580 int edge = 32; // XXX should be avcodec_get_edge_width(), but that fails on svq1
1581 int w = s->width, h = s->height;
1584 return AVERROR(EINVAL);
1585 pixel_size = desc->comp[0].step_minus1 + 1;
1587 buf = av_mallocz(sizeof(*buf));
1589 return AVERROR(ENOMEM);
1591 avcodec_align_dimensions(s, &w, &h);
1593 if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
1598 if ((ret = av_image_alloc(buf->base, buf->linesize, w, h,
1599 s->pix_fmt, 32)) < 0) {
1601 av_log(s, AV_LOG_ERROR, "alloc_buffer: av_image_alloc() failed\n");
1604 /* XXX this shouldn't be needed, but some tests break without this line
1605 * those decoders are buggy and need to be fixed.
1606 * the following tests fail:
1607 * cdgraphics, ansi, aasc, fraps-v1, qtrle-1bit
1609 memset(buf->base[0], 128, ret);
1611 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1612 for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
1613 const int h_shift = i==0 ? 0 : h_chroma_shift;
1614 const int v_shift = i==0 ? 0 : v_chroma_shift;
1615 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !buf->linesize[i] || !buf->base[i])
1616 buf->data[i] = buf->base[i];
1618 buf->data[i] = buf->base[i] +
1619 FFALIGN((buf->linesize[i]*edge >> v_shift) +
1620 (pixel_size*edge >> h_shift), 32);
1624 buf->pix_fmt = s->pix_fmt;
1631 int codec_get_buffer(AVCodecContext *s, AVFrame *frame)
1633 FrameBuffer **pool = s->opaque;
1637 if(av_image_check_size(s->width, s->height, 0, s) || s->pix_fmt<0) {
1638 av_log(s, AV_LOG_ERROR, "codec_get_buffer: image parameters invalid\n");
1642 if (!*pool && (ret = alloc_buffer(pool, s, pool)) < 0)
1648 if (buf->w != s->width || buf->h != s->height || buf->pix_fmt != s->pix_fmt) {
1649 av_freep(&buf->base[0]);
1651 if ((ret = alloc_buffer(pool, s, &buf)) < 0)
1654 av_assert0(!buf->refcount);
1657 frame->opaque = buf;
1658 frame->type = FF_BUFFER_TYPE_USER;
1659 frame->extended_data = frame->data;
1660 frame->pkt_pts = s->pkt ? s->pkt->pts : AV_NOPTS_VALUE;
1661 frame->width = buf->w;
1662 frame->height = buf->h;
1663 frame->format = buf->pix_fmt;
1664 frame->sample_aspect_ratio = s->sample_aspect_ratio;
1666 for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
1667 frame->base[i] = buf->base[i]; // XXX h264.c uses base though it shouldn't
1668 frame->data[i] = buf->data[i];
1669 frame->linesize[i] = buf->linesize[i];
1675 static void unref_buffer(FrameBuffer *buf)
1677 FrameBuffer **pool = buf->pool;
1679 av_assert0(buf->refcount > 0);
1681 if (!buf->refcount) {
1683 for(tmp= *pool; tmp; tmp= tmp->next)
1684 av_assert1(tmp != buf);
1691 void codec_release_buffer(AVCodecContext *s, AVFrame *frame)
1693 FrameBuffer *buf = frame->opaque;
1696 if(frame->type!=FF_BUFFER_TYPE_USER) {
1697 avcodec_default_release_buffer(s, frame);
1701 for (i = 0; i < FF_ARRAY_ELEMS(frame->data); i++)
1702 frame->data[i] = NULL;
1707 void filter_release_buffer(AVFilterBuffer *fb)
1709 FrameBuffer *buf = fb->priv;
1714 void free_buffer_pool(FrameBuffer **pool)
1716 FrameBuffer *buf = *pool;
1719 av_freep(&buf->base[0]);