diff options
Diffstat (limited to 'cmdutils.c')
-rw-r--r-- | cmdutils.c | 671 |
1 files changed, 503 insertions, 168 deletions
diff --git a/cmdutils.c b/cmdutils.c index 062d7ec2f8..79870cedde 100644 --- a/cmdutils.c +++ b/cmdutils.c @@ -2,20 +2,20 @@ * Various utilities for command line tools * Copyright (c) 2000-2003 Fabrice Bellard * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -29,13 +29,17 @@ references to libraries that are not being built. */ #include "config.h" +#include "compat/va_copy.h" #include "libavformat/avformat.h" #include "libavfilter/avfilter.h" #include "libavdevice/avdevice.h" #include "libavresample/avresample.h" #include "libswscale/swscale.h" +#include "libswresample/swresample.h" +#include "libpostproc/postprocess.h" #include "libavutil/avassert.h" #include "libavutil/avstring.h" +#include "libavutil/bprint.h" #include "libavutil/mathematics.h" #include "libavutil/imgutils.h" #include "libavutil/parseutils.h" @@ -52,18 +56,27 @@ #include <sys/time.h> #include <sys/resource.h> #endif +#if CONFIG_OPENCL +#include "libavutil/opencl.h" +#endif + + +static int init_report(const char *env); struct SwsContext *sws_opts; +AVDictionary *swr_opts; AVDictionary *format_opts, *codec_opts, *resample_opts; -static const int this_year = 2013; +const int this_year = 2013; + +static FILE *report_file; void init_opts(void) { -#if CONFIG_SWSCALE - sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC, + + if(CONFIG_SWSCALE) + sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC, NULL, NULL, NULL); -#endif } void uninit_opts(void) @@ -72,6 +85,8 @@ void uninit_opts(void) sws_freeContext(sws_opts); sws_opts = NULL; #endif + + av_dict_free(&swr_opts); av_dict_free(&format_opts); av_dict_free(&codec_opts); av_dict_free(&resample_opts); @@ -82,6 +97,20 @@ void log_callback_help(void *ptr, int level, const char *fmt, va_list vl) vfprintf(stdout, fmt, vl); } +static void log_callback_report(void *ptr, int level, const char *fmt, va_list vl) +{ + va_list vl2; + char line[1024]; + static int print_prefix = 1; + + va_copy(vl2, vl); + av_log_default_callback(ptr, level, fmt, vl); + av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix); + va_end(vl2); + fputs(line, report_file); + fflush(report_file); +} + static void (*program_exit)(int ret); void register_exit(void (*cb)(int ret)) @@ -162,8 +191,10 @@ void show_help_options(const OptionDef *options, const char *msg, int req_flags, void show_help_children(const AVClass *class, int flags) { const AVClass *child = NULL; - av_opt_show2(&class, NULL, flags, 0); - printf("\n"); + if (class->option) { + av_opt_show2(&class, NULL, flags, 0); + printf("\n"); + } while (child = av_opt_child_class_next(class, child)) show_help_children(child, flags); @@ -285,7 +316,8 @@ static int write_option(void *optctx, const OptionDef *po, const char *opt, int ret = po->u.func_arg(optctx, opt, arg); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, - "Failed to set value '%s' for option '%s'\n", arg, opt); + "Failed to set value '%s' for option '%s': %s\n", + arg, opt, av_err2str(ret)); return ret; } } @@ -412,65 +444,139 @@ int locate_option(int argc, char **argv, const OptionDef *options, (po->name && !strcmp(optname, po->name))) return i; - if (!po || po->flags & HAS_ARG) + if (po->flags & HAS_ARG) i++; } return 0; } +static void dump_argument(const char *a) +{ + const unsigned char *p; + + for (p = a; *p; p++) + if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') || + *p == '_' || (*p >= 'a' && *p <= 'z'))) + break; + if (!*p) { + fputs(a, report_file); + return; + } + fputc('"', report_file); + for (p = a; *p; p++) { + if (*p == '\\' || *p == '"' || *p == '$' || *p == '`') + fprintf(report_file, "\\%c", *p); + else if (*p < ' ' || *p > '~') + fprintf(report_file, "\\x%02x", *p); + else + fputc(*p, report_file); + } + fputc('"', report_file); +} + void parse_loglevel(int argc, char **argv, const OptionDef *options) { int idx = locate_option(argc, argv, options, "loglevel"); + const char *env; if (!idx) idx = locate_option(argc, argv, options, "v"); if (idx && argv[idx + 1]) opt_loglevel(NULL, "loglevel", argv[idx + 1]); + idx = locate_option(argc, argv, options, "report"); + if ((env = getenv("FFREPORT")) || idx) { + init_report(env); + if (report_file) { + int i; + fprintf(report_file, "Command line:\n"); + for (i = 0; i < argc; i++) { + dump_argument(argv[i]); + fputc(i < argc - 1 ? ' ' : '\n', report_file); + } + fflush(report_file); + } + } +} + +static const AVOption *opt_find(void *obj, const char *name, const char *unit, + int opt_flags, int search_flags) +{ + const AVOption *o = av_opt_find(obj, name, unit, opt_flags, search_flags); + if(o && !o->flags) + return NULL; + return o; } #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0 int opt_default(void *optctx, const char *opt, const char *arg) { const AVOption *o; + int consumed = 0; char opt_stripped[128]; const char *p; const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class(); #if CONFIG_AVRESAMPLE const AVClass *rc = avresample_get_class(); #endif -#if CONFIG_SWSCALE - const AVClass *sc = sws_get_class(); -#endif + const AVClass *sc, *swr_class; + + if (!strcmp(opt, "debug") || !strcmp(opt, "fdebug")) + av_log_set_level(AV_LOG_DEBUG); if (!(p = strchr(opt, ':'))) p = opt + strlen(opt); av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1)); - if ((o = av_opt_find(&cc, opt_stripped, NULL, 0, + if ((o = opt_find(&cc, opt_stripped, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) || ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') && - (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) + (o = opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) { av_dict_set(&codec_opts, opt, arg, FLAGS); - else if ((o = av_opt_find(&fc, opt, NULL, 0, - AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) + consumed = 1; + } + if ((o = opt_find(&fc, opt, NULL, 0, + AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) { av_dict_set(&format_opts, opt, arg, FLAGS); -#if CONFIG_AVRESAMPLE - else if ((o = av_opt_find(&rc, opt, NULL, 0, - AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) - av_dict_set(&resample_opts, opt, arg, FLAGS); -#endif + if (consumed) + av_log(NULL, AV_LOG_VERBOSE, "Routing option %s to both codec and muxer layer\n", opt); + consumed = 1; + } #if CONFIG_SWSCALE - else if ((o = av_opt_find(&sc, opt, NULL, 0, - AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) { + sc = sws_get_class(); + if (!consumed && opt_find(&sc, opt, NULL, 0, + AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) { // XXX we only support sws_flags, not arbitrary sws options int ret = av_opt_set(sws_opts, opt, arg, 0); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt); return ret; } + consumed = 1; + } +#endif +#if CONFIG_SWRESAMPLE + swr_class = swr_get_class(); + if (!consumed && (o=opt_find(&swr_class, opt, NULL, 0, + AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) { + struct SwrContext *swr = swr_alloc(); + int ret = av_opt_set(swr, opt, arg, 0); + swr_free(&swr); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt); + return ret; + } + av_dict_set(&swr_opts, opt, arg, FLAGS); + consumed = 1; + } +#endif +#if CONFIG_AVRESAMPLE + if ((o=opt_find(&rc, opt, NULL, 0, + AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) { + av_dict_set(&resample_opts, opt, arg, FLAGS); + consumed = 1; } #endif - if (o) + if (consumed) return 0; return AVERROR_OPTION_NOT_FOUND; } @@ -515,6 +621,7 @@ static void finish_group(OptionParseContext *octx, int group_idx, #if CONFIG_SWSCALE g->sws_opts = sws_opts; #endif + g->swr_opts = swr_opts; g->codec_opts = codec_opts; g->format_opts = format_opts; g->resample_opts = resample_opts; @@ -525,6 +632,7 @@ static void finish_group(OptionParseContext *octx, int group_idx, #if CONFIG_SWSCALE sws_opts = NULL; #endif + swr_opts = NULL; init_opts(); memset(&octx->cur_group, 0, sizeof(octx->cur_group)); @@ -582,6 +690,7 @@ void uninit_parse_context(OptionParseContext *octx) #if CONFIG_SWSCALE sws_freeContext(l->groups[j].sws_opts); #endif + av_dict_free(&l->groups[j].swr_opts); } av_freep(&l->groups); } @@ -598,6 +707,7 @@ int split_commandline(OptionParseContext *octx, int argc, char *argv[], const OptionGroupDef *groups, int nb_groups) { int optindex = 1; + int dashdash = -2; /* perform system-dependent conversions for arguments list */ prepare_app_arguments(&argc, &argv); @@ -612,8 +722,12 @@ int split_commandline(OptionParseContext *octx, int argc, char *argv[], av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt); + if (opt[0] == '-' && opt[1] == '-' && !opt[2]) { + dashdash = optindex; + continue; + } /* unnamed group separators, e.g. output filename */ - if (opt[0] != '-' || !opt[1]) { + if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) { finish_group(octx, 0, opt); av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name); continue; @@ -710,6 +824,13 @@ int opt_loglevel(void *optctx, const char *opt, const char *arg) int level; int i; + tail = strstr(arg, "repeat"); + av_log_set_flags(tail ? 0 : AV_LOG_SKIP_REPEATED); + if (tail == arg) + arg += 6 + (arg[6]=='+'); + if(tail && !*arg) + return 0; + for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) { if (!strcmp(log_levels[i].name, arg)) { av_log_set_level(log_levels[i].level); @@ -729,6 +850,128 @@ int opt_loglevel(void *optctx, const char *opt, const char *arg) return 0; } +static void expand_filename_template(AVBPrint *bp, const char *template, + struct tm *tm) +{ + int c; + + while ((c = *(template++))) { + if (c == '%') { + if (!(c = *(template++))) + break; + switch (c) { + case 'p': + av_bprintf(bp, "%s", program_name); + break; + case 't': + av_bprintf(bp, "%04d%02d%02d-%02d%02d%02d", + tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, + tm->tm_hour, tm->tm_min, tm->tm_sec); + break; + case '%': + av_bprint_chars(bp, c, 1); + break; + } + } else { + av_bprint_chars(bp, c, 1); + } + } +} + +static int init_report(const char *env) +{ + char *filename_template = NULL; + char *key, *val; + int ret, count = 0; + time_t now; + struct tm *tm; + AVBPrint filename; + + if (report_file) /* already opened */ + return 0; + time(&now); + tm = localtime(&now); + + while (env && *env) { + if ((ret = av_opt_get_key_value(&env, "=", ":", 0, &key, &val)) < 0) { + if (count) + av_log(NULL, AV_LOG_ERROR, + "Failed to parse FFREPORT environment variable: %s\n", + av_err2str(ret)); + break; + } + if (*env) + env++; + count++; + if (!strcmp(key, "file")) { + av_free(filename_template); + filename_template = val; + val = NULL; + } else { + av_log(NULL, AV_LOG_ERROR, "Unknown key '%s' in FFREPORT\n", key); + } + av_free(val); + av_free(key); + } + + av_bprint_init(&filename, 0, 1); + expand_filename_template(&filename, + av_x_if_null(filename_template, "%p-%t.log"), tm); + av_free(filename_template); + if (!av_bprint_is_complete(&filename)) { + av_log(NULL, AV_LOG_ERROR, "Out of memory building report file name\n"); + return AVERROR(ENOMEM); + } + + report_file = fopen(filename.str, "w"); + if (!report_file) { + av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n", + filename.str, strerror(errno)); + return AVERROR(errno); + } + av_log_set_callback(log_callback_report); + av_log(NULL, AV_LOG_INFO, + "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n" + "Report written to \"%s\"\n", + program_name, + tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, + tm->tm_hour, tm->tm_min, tm->tm_sec, + filename.str); + av_bprint_finalize(&filename, NULL); + return 0; +} + +int opt_report(const char *opt) +{ + return init_report(NULL); +} + +int opt_max_alloc(void *optctx, const char *opt, const char *arg) +{ + char *tail; + size_t max; + + max = strtol(arg, &tail, 10); + if (*tail) { + av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg); + exit_program(1); + } + av_max_alloc(max); + return 0; +} + +int opt_cpuflags(void *optctx, const char *opt, const char *arg) +{ + int ret; + unsigned flags = av_get_cpu_flags(); + + if ((ret = av_parse_cpu_caps(&flags, arg)) < 0) + return ret; + + av_force_cpu_flags(flags); + return 0; +} + int opt_timelimit(void *optctx, const char *opt, const char *arg) { #if HAVE_SETRLIMIT @@ -742,6 +985,26 @@ int opt_timelimit(void *optctx, const char *opt, const char *arg) return 0; } +#if CONFIG_OPENCL +int opt_opencl(void *optctx, const char *opt, const char *arg) +{ + char *key, *value; + const char *opts = arg; + int ret = 0; + while (*opts) { + ret = av_opt_get_key_value(&opts, "=", ":", 0, &key, &value); + if (ret < 0) + return ret; + ret = av_opencl_set_option(key, value); + if (ret < 0) + return ret; + if (*opts) + opts++; + } + return ret; +} +#endif + void print_error(const char *filename, int err) { char errbuf[128]; @@ -757,6 +1020,7 @@ static int warned_cfg = 0; #define INDENT 1 #define SHOW_VERSION 2 #define SHOW_CONFIG 4 +#define SHOW_COPYRIGHT 8 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \ if (CONFIG_##LIBNAME) { \ @@ -764,7 +1028,7 @@ static int warned_cfg = 0; if (flags & SHOW_VERSION) { \ unsigned int version = libname##_version(); \ av_log(NULL, level, \ - "%slib%-10s %2d.%3d.%2d / %2d.%3d.%2d\n", \ + "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \ indent, #libname, \ LIB##LIBNAME##_VERSION_MAJOR, \ LIB##LIBNAME##_VERSION_MINOR, \ @@ -773,7 +1037,7 @@ static int warned_cfg = 0; } \ if (flags & SHOW_CONFIG) { \ const char *cfg = libname##_configuration(); \ - if (strcmp(LIBAV_CONFIGURATION, cfg)) { \ + if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \ if (!warned_cfg) { \ av_log(NULL, level, \ "%sWARNING: library configuration mismatch\n", \ @@ -795,24 +1059,40 @@ static void print_all_libs_info(int flags, int level) PRINT_LIB_INFO(avfilter, AVFILTER, flags, level); PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level); PRINT_LIB_INFO(swscale, SWSCALE, flags, level); + PRINT_LIB_INFO(swresample,SWRESAMPLE, flags, level); + PRINT_LIB_INFO(postproc, POSTPROC, flags, level); } -void show_banner(void) +static void print_program_info(int flags, int level) { - av_log(NULL, AV_LOG_INFO, - "%s version " LIBAV_VERSION ", Copyright (c) %d-%d the Libav developers\n", - program_name, program_birth_year, this_year); - av_log(NULL, AV_LOG_INFO, " built on %s %s with %s\n", - __DATE__, __TIME__, CC_IDENT); - av_log(NULL, AV_LOG_VERBOSE, " configuration: " LIBAV_CONFIGURATION "\n"); - print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_VERBOSE); - print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_VERBOSE); + const char *indent = flags & INDENT? " " : ""; + + av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name); + if (flags & SHOW_COPYRIGHT) + av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers", + program_birth_year, this_year); + av_log(NULL, level, "\n"); + av_log(NULL, level, "%sbuilt on %s %s with %s\n", + indent, __DATE__, __TIME__, CC_IDENT); + + av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent); +} + +void show_banner(int argc, char **argv, const OptionDef *options) +{ + int idx = locate_option(argc, argv, options, "version"); + if (idx) + return; + + print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO); + print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO); + print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO); } int show_version(void *optctx, const char *opt, const char *arg) { av_log_set_callback(log_callback_help); - printf("%s " LIBAV_VERSION "\n", program_name); + print_program_info (0 , AV_LOG_INFO); print_all_libs_info(SHOW_VERSION, AV_LOG_INFO); return 0; @@ -820,12 +1100,13 @@ int show_version(void *optctx, const char *opt, const char *arg) int show_license(void *optctx, const char *opt, const char *arg) { - printf( #if CONFIG_NONFREE + printf( "This version of %s has nonfree parts compiled in.\n" "Therefore it is not legally redistributable.\n", - program_name + program_name ); #elif CONFIG_GPLV3 + printf( "%s is free software; you can redistribute it and/or modify\n" "it under the terms of the GNU General Public License as published by\n" "the Free Software Foundation; either version 3 of the License, or\n" @@ -838,8 +1119,9 @@ int show_license(void *optctx, const char *opt, const char *arg) "\n" "You should have received a copy of the GNU General Public License\n" "along with %s. If not, see <http://www.gnu.org/licenses/>.\n", - program_name, program_name, program_name + program_name, program_name, program_name ); #elif CONFIG_GPL + printf( "%s is free software; you can redistribute it and/or modify\n" "it under the terms of the GNU General Public License as published by\n" "the Free Software Foundation; either version 2 of the License, or\n" @@ -853,8 +1135,9 @@ int show_license(void *optctx, const char *opt, const char *arg) "You should have received a copy of the GNU General Public License\n" "along with %s; if not, write to the Free Software\n" "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n", - program_name, program_name, program_name + program_name, program_name, program_name ); #elif CONFIG_LGPLV3 + printf( "%s is free software; you can redistribute it and/or modify\n" "it under the terms of the GNU Lesser General Public License as published by\n" "the Free Software Foundation; either version 3 of the License, or\n" @@ -867,8 +1150,9 @@ int show_license(void *optctx, const char *opt, const char *arg) "\n" "You should have received a copy of the GNU Lesser General Public License\n" "along with %s. If not, see <http://www.gnu.org/licenses/>.\n", - program_name, program_name, program_name + program_name, program_name, program_name ); #else + printf( "%s is free software; you can redistribute it and/or\n" "modify it under the terms of the GNU Lesser General Public\n" "License as published by the Free Software Foundation; either\n" @@ -882,9 +1166,8 @@ int show_license(void *optctx, const char *opt, const char *arg) "You should have received a copy of the GNU Lesser General Public\n" "License along with %s; if not, write to the Free Software\n" "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n", - program_name, program_name, program_name + program_name, program_name, program_name ); #endif - ); return 0; } @@ -939,7 +1222,7 @@ int show_formats(void *optctx, const char *opt, const char *arg) #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \ if (codec->field) { \ - const type *p = c->field; \ + const type *p = codec->field; \ \ printf(" Supported " list_name ":"); \ while (*p != term) { \ @@ -957,7 +1240,8 @@ static void print_codec(const AVCodec *c) printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name, c->long_name ? c->long_name : ""); - if (c->type == AVMEDIA_TYPE_VIDEO) { + if (c->type == AVMEDIA_TYPE_VIDEO || + c->type == AVMEDIA_TYPE_AUDIO) { printf(" Threading capabilities: "); switch (c->capabilities & (CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS)) { @@ -1001,7 +1285,9 @@ static char get_media_type_char(enum AVMediaType type) switch (type) { case AVMEDIA_TYPE_VIDEO: return 'V'; case AVMEDIA_TYPE_AUDIO: return 'A'; + case AVMEDIA_TYPE_DATA: return 'D'; case AVMEDIA_TYPE_SUBTITLE: return 'S'; + case AVMEDIA_TYPE_ATTACHMENT:return 'T'; default: return '?'; } } @@ -1017,6 +1303,36 @@ static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev, return NULL; } +static int compare_codec_desc(const void *a, const void *b) +{ + const AVCodecDescriptor * const *da = a; + const AVCodecDescriptor * const *db = b; + + return (*da)->type != (*db)->type ? (*da)->type - (*db)->type : + strcmp((*da)->name, (*db)->name); +} + +static unsigned get_codecs_sorted(const AVCodecDescriptor ***rcodecs) +{ + const AVCodecDescriptor *desc = NULL; + const AVCodecDescriptor **codecs; + unsigned nb_codecs = 0, i = 0; + + while ((desc = avcodec_descriptor_next(desc))) + nb_codecs++; + if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs)))) { + av_log(NULL, AV_LOG_ERROR, "Out of memory\n"); + exit_program(1); + } + desc = NULL; + while ((desc = avcodec_descriptor_next(desc))) + codecs[i++] = desc; + av_assert0(i == nb_codecs); + qsort(codecs, nb_codecs, sizeof(*codecs), compare_codec_desc); + *rcodecs = codecs; + return nb_codecs; +} + static void print_codecs_for_id(enum AVCodecID id, int encoder) { const AVCodec *codec = NULL; @@ -1031,7 +1347,8 @@ static void print_codecs_for_id(enum AVCodecID id, int encoder) int show_codecs(void *optctx, const char *opt, const char *arg) { - const AVCodecDescriptor *desc = NULL; + const AVCodecDescriptor **codecs; + unsigned i, nb_codecs = get_codecs_sorted(&codecs); printf("Codecs:\n" " D..... = Decoding supported\n" @@ -1043,9 +1360,11 @@ int show_codecs(void *optctx, const char *opt, const char *arg) " ....L. = Lossy compression\n" " .....S = Lossless compression\n" " -------\n"); - while ((desc = avcodec_descriptor_next(desc))) { + for (i = 0; i < nb_codecs; i++) { + const AVCodecDescriptor *desc = codecs[i]; const AVCodec *codec = NULL; + printf(" "); printf(avcodec_find_decoder(desc->id) ? "D" : "."); printf(avcodec_find_encoder(desc->id) ? "E" : "."); @@ -1074,30 +1393,37 @@ int show_codecs(void *optctx, const char *opt, const char *arg) printf("\n"); } + av_free(codecs); return 0; } static void print_codecs(int encoder) { - const AVCodecDescriptor *desc = NULL; + const AVCodecDescriptor **codecs; + unsigned i, nb_codecs = get_codecs_sorted(&codecs); printf("%s:\n" - " V... = Video\n" - " A... = Audio\n" - " S... = Subtitle\n" - " .F.. = Frame-level multithreading\n" - " ..S. = Slice-level multithreading\n" - " ...X = Codec is experimental\n" - " ---\n", + " V..... = Video\n" + " A..... = Audio\n" + " S..... = Subtitle\n" + " .F.... = Frame-level multithreading\n" + " ..S... = Slice-level multithreading\n" + " ...X.. = Codec is experimental\n" + " ....B. = Supports draw_horiz_band\n" + " .....D = Supports direct rendering method 1\n" + " ------\n", encoder ? "Encoders" : "Decoders"); - while ((desc = avcodec_descriptor_next(desc))) { + for (i = 0; i < nb_codecs; i++) { + const AVCodecDescriptor *desc = codecs[i]; const AVCodec *codec = NULL; while ((codec = next_codec_for_id(desc->id, codec, encoder))) { - printf("%c", get_media_type_char(desc->type)); + printf(" %c", get_media_type_char(desc->type)); printf((codec->capabilities & CODEC_CAP_FRAME_THREADS) ? "F" : "."); printf((codec->capabilities & CODEC_CAP_SLICE_THREADS) ? "S" : "."); printf((codec->capabilities & CODEC_CAP_EXPERIMENTAL) ? "X" : "."); + printf((codec->capabilities & CODEC_CAP_DRAW_HORIZ_BAND)?"B" : "."); + printf((codec->capabilities & CODEC_CAP_DR1) ? "D" : "."); printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : ""); if (strcmp(codec->name, desc->name)) @@ -1106,6 +1432,7 @@ static void print_codecs(int encoder) printf("\n"); } } + av_free(codecs); } int show_decoders(void *optctx, const char *opt, const char *arg) @@ -1149,11 +1476,41 @@ int show_protocols(void *optctx, const char *opt, const char *arg) int show_filters(void *optctx, const char *opt, const char *arg) { const AVFilter av_unused(*filter) = NULL; - - printf("Filters:\n"); + char descr[64], *descr_cur; + int i, j; + const AVFilterPad *pad; + + printf("Filters:\n" + " T. = Timeline support\n" + " .S = Slice threading\n" + " A = Audio input/output\n" + " V = Video input/output\n" + " N = Dynamic number and/or type of input/output\n" + " | = Source or sink filter\n"); #if CONFIG_AVFILTER - while ((filter = avfilter_next(filter))) - printf("%-16s %s\n", filter->name, filter->description); + while ((filter = avfilter_next(filter))) { + descr_cur = descr; + for (i = 0; i < 2; i++) { + if (i) { + *(descr_cur++) = '-'; + *(descr_cur++) = '>'; + } + pad = i ? filter->outputs : filter->inputs; + for (j = 0; pad && pad[j].name; j++) { + if (descr_cur >= descr + sizeof(descr) - 4) + break; + *(descr_cur++) = get_media_type_char(pad[j].type); + } + if (!j) + *(descr_cur++) = ((!i && (filter->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)) || + ( i && (filter->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS))) ? 'N' : '|'; + } + *descr_cur = 0; + printf(" %c%c %-16s %-10s %s\n", + filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE ? 'T' : '.', + filter->flags & AVFILTER_FLAG_SLICE_THREADS ? 'S' : '.', + filter->name, descr, filter->description); + } #endif return 0; } @@ -1191,6 +1548,35 @@ int show_pix_fmts(void *optctx, const char *opt, const char *arg) return 0; } +int show_layouts(void *optctx, const char *opt, const char *arg) +{ + int i = 0; + uint64_t layout, j; + const char *name, *descr; + + printf("Individual channels:\n" + "NAME DESCRIPTION\n"); + for (i = 0; i < 63; i++) { + name = av_get_channel_name((uint64_t)1 << i); + if (!name) + continue; + descr = av_get_channel_description((uint64_t)1 << i); + printf("%-12s%s\n", name, descr); + } + printf("\nStandard channel layouts:\n" + "NAME DECOMPOSITION\n"); + for (i = 0; !av_get_standard_channel_layout(i, &layout, &name); i++) { + if (name) { + printf("%-12s", name); + for (j = 1; j; j <<= 1) + if ((layout & j)) + printf("%s%s", (layout & (j - 1)) ? "+" : "", av_get_channel_name(j)); + printf("\n"); + } + } + return 0; +} + int show_sample_fmts(void *optctx, const char *opt, const char *arg) { int i; @@ -1224,13 +1610,13 @@ static void show_help_codec(const char *name, int encoder) } if (!printed) { - av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to Libav, " - "but no %s for it are available. Libav might need to be " + av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, " + "but no %s for it are available. FFmpeg might need to be " "recompiled with additional external libraries.\n", name, encoder ? "encoders" : "decoders"); } } else { - av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by Libav.\n", + av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n", name); } } @@ -1289,6 +1675,7 @@ static void show_help_muxer(const char *name) #if CONFIG_AVFILTER static void show_help_filter(const char *name) { +#if CONFIG_AVFILTER const AVFilter *f = avfilter_get_by_name(name); int i, count; @@ -1300,7 +1687,9 @@ static void show_help_filter(const char *name) return; } - printf("Filter %s [%s]:\n", f->name, f->description); + printf("Filter %s\n", f->name); + if (f->description) + printf(" %s\n", f->description); if (f->flags & AVFILTER_FLAG_SLICE_THREADS) printf(" slice threading supported\n"); @@ -1308,24 +1697,34 @@ static void show_help_filter(const char *name) printf(" Inputs:\n"); count = avfilter_pad_count(f->inputs); for (i = 0; i < count; i++) { - printf(" %d %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i), + printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i), media_type_string(avfilter_pad_get_type(f->inputs, i))); } if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS) printf(" dynamic (depending on the options)\n"); + else if (!count) + printf(" none (source filter)\n"); printf(" Outputs:\n"); count = avfilter_pad_count(f->outputs); for (i = 0; i < count; i++) { - printf(" %d %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i), + printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i), media_type_string(avfilter_pad_get_type(f->outputs, i))); } if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS) printf(" dynamic (depending on the options)\n"); + else if (!count) + printf(" none (sink filter)\n"); if (f->priv_class) - show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM | + show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM); + if (f->flags & AVFILTER_FLAG_SUPPORT_TIMELINE) + printf("This filter has support for timeline through the 'enable' option.\n"); +#else + av_log(NULL, AV_LOG_ERROR, "Build without libavfilter; " + "can not to satisfy request\n"); +#endif } #endif @@ -1385,6 +1784,11 @@ int cmdutils_read_file(const char *filename, char **bufptr, size_t *size) fseek(f, 0, SEEK_END); *size = ftell(f); fseek(f, 0, SEEK_SET); + if (*size == (size_t)-1) { + av_log(NULL, AV_LOG_ERROR, "IO error: %s\n", strerror(errno)); + fclose(f); + return AVERROR(errno); + } *bufptr = av_malloc(*size + 1); if (!*bufptr) { av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n"); @@ -1409,58 +1813,47 @@ int cmdutils_read_file(const char *filename, char **bufptr, size_t *size) return ret; } -void init_pts_correction(PtsCorrectionContext *ctx) -{ - ctx->num_faulty_pts = ctx->num_faulty_dts = 0; - ctx->last_pts = ctx->last_dts = INT64_MIN; -} - -int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t reordered_pts, - int64_t dts) -{ - int64_t pts = AV_NOPTS_VALUE; - - if (dts != AV_NOPTS_VALUE) { - ctx->num_faulty_dts += dts <= ctx->last_dts; - ctx->last_dts = dts; - } - if (reordered_pts != AV_NOPTS_VALUE) { - ctx->num_faulty_pts += reordered_pts <= ctx->last_pts; - ctx->last_pts = reordered_pts; - } - if ((ctx->num_faulty_pts<=ctx->num_faulty_dts || dts == AV_NOPTS_VALUE) - && reordered_pts != AV_NOPTS_VALUE) - pts = reordered_pts; - else - pts = dts; - - return pts; -} - FILE *get_preset_file(char *filename, size_t filename_size, const char *preset_name, int is_path, const char *codec_name) { FILE *f = NULL; int i; - const char *base[3] = { getenv("AVCONV_DATADIR"), + const char *base[3] = { getenv("FFMPEG_DATADIR"), getenv("HOME"), - AVCONV_DATADIR, }; + FFMPEG_DATADIR, }; if (is_path) { av_strlcpy(filename, preset_name, filename_size); f = fopen(filename, "r"); } else { +#ifdef _WIN32 + char datadir[MAX_PATH], *ls; + base[2] = NULL; + + if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1)) + { + for (ls = datadir; ls < datadir + strlen(datadir); ls++) + if (*ls == '\\') *ls = '/'; + + if (ls = strrchr(datadir, '/')) + { + *ls = 0; + strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir)); + base[2] = datadir; + } + } +#endif for (i = 0; i < 3 && !f; i++) { if (!base[i]) continue; - snprintf(filename, filename_size, "%s%s/%s.avpreset", base[i], - i != 1 ? "" : "/.avconv", preset_name); + snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i], + i != 1 ? "" : "/.ffmpeg", preset_name); f = fopen(filename, "r"); if (!f && codec_name) { snprintf(filename, filename_size, - "%s%s/%s-%s.avpreset", - base[i], i != 1 ? "" : "/.avconv", codec_name, + "%s%s/%s-%s.ffpreset", + base[i], i != 1 ? "" : "/.ffmpeg", codec_name, preset_name); f = fopen(filename, "r"); } @@ -1472,56 +1865,10 @@ FILE *get_preset_file(char *filename, size_t filename_size, int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec) { - if (*spec <= '9' && *spec >= '0') /* opt:index */ - return strtol(spec, NULL, 0) == st->index; - else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' || - *spec == 't') { /* opt:[vasdt] */ - enum AVMediaType type; - - switch (*spec++) { - case 'v': type = AVMEDIA_TYPE_VIDEO; break; - case 'a': type = AVMEDIA_TYPE_AUDIO; break; - case 's': type = AVMEDIA_TYPE_SUBTITLE; break; - case 'd': type = AVMEDIA_TYPE_DATA; break; - case 't': type = AVMEDIA_TYPE_ATTACHMENT; break; - default: av_assert0(0); - } - if (type != st->codec->codec_type) - return 0; - if (*spec++ == ':') { /* possibly followed by :index */ - int i, index = strtol(spec, NULL, 0); - for (i = 0; i < s->nb_streams; i++) - if (s->streams[i]->codec->codec_type == type && index-- == 0) - return i == st->index; - return 0; - } - return 1; - } else if (*spec == 'p' && *(spec + 1) == ':') { - int prog_id, i, j; - char *endptr; - spec += 2; - prog_id = strtol(spec, &endptr, 0); - for (i = 0; i < s->nb_programs; i++) { - if (s->programs[i]->id != prog_id) - continue; - - if (*endptr++ == ':') { - int stream_idx = strtol(endptr, NULL, 0); - return stream_idx >= 0 && - stream_idx < s->programs[i]->nb_stream_indexes && - st->index == s->programs[i]->stream_index[stream_idx]; - } - - for (j = 0; j < s->programs[i]->nb_stream_indexes; j++) - if (st->index == s->programs[i]->stream_index[j]) - return 1; - } - return 0; - } else if (!*spec) /* empty specifier, matches everything */ - return 1; - - av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec); - return AVERROR(EINVAL); + int ret = avformat_match_stream_specifier(s, st, spec); + if (ret < 0) + av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec); + return ret; } AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id, @@ -1618,15 +1965,3 @@ void *grow_array(void *array, int elem_size, int *size, int new_size) } return array; } - -const char *media_type_string(enum AVMediaType media_type) -{ - switch (media_type) { - case AVMEDIA_TYPE_VIDEO: return "video"; - case AVMEDIA_TYPE_AUDIO: return "audio"; - case AVMEDIA_TYPE_DATA: return "data"; - case AVMEDIA_TYPE_SUBTITLE: return "subtitle"; - case AVMEDIA_TYPE_ATTACHMENT: return "attachment"; - default: return "unknown"; - } -} |