00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <string.h>
00023 #include <stdlib.h>
00024 #include <errno.h>
00025 #include <math.h>
00026
00027
00028
00029
00030
00031 #include "config.h"
00032 #include "compat/va_copy.h"
00033 #include "libavformat/avformat.h"
00034 #include "libavfilter/avfilter.h"
00035 #include "libavdevice/avdevice.h"
00036 #include "libavresample/avresample.h"
00037 #include "libswscale/swscale.h"
00038 #include "libswresample/swresample.h"
00039 #if CONFIG_POSTPROC
00040 #include "libpostproc/postprocess.h"
00041 #endif
00042 #include "libavutil/avassert.h"
00043 #include "libavutil/avstring.h"
00044 #include "libavutil/bprint.h"
00045 #include "libavutil/mathematics.h"
00046 #include "libavutil/imgutils.h"
00047 #include "libavutil/parseutils.h"
00048 #include "libavutil/pixdesc.h"
00049 #include "libavutil/eval.h"
00050 #include "libavutil/dict.h"
00051 #include "libavutil/opt.h"
00052 #include "cmdutils.h"
00053 #include "version.h"
00054 #if CONFIG_NETWORK
00055 #include "libavformat/network.h"
00056 #endif
00057 #if HAVE_SYS_RESOURCE_H
00058 #include <sys/time.h>
00059 #include <sys/resource.h>
00060 #endif
00061
00062 static int init_report(const char *env);
00063
00064 struct SwsContext *sws_opts;
00065 SwrContext *swr_opts;
00066 AVDictionary *format_opts, *codec_opts;
00067
00068 const int this_year = 2012;
00069
00070 static FILE *report_file;
00071
00072 void init_opts(void)
00073 {
00074
00075 if(CONFIG_SWSCALE)
00076 sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
00077 NULL, NULL, NULL);
00078
00079 if(CONFIG_SWRESAMPLE)
00080 swr_opts = swr_alloc();
00081 }
00082
00083 void uninit_opts(void)
00084 {
00085 #if CONFIG_SWSCALE
00086 sws_freeContext(sws_opts);
00087 sws_opts = NULL;
00088 #endif
00089
00090 if(CONFIG_SWRESAMPLE)
00091 swr_free(&swr_opts);
00092
00093 av_dict_free(&format_opts);
00094 av_dict_free(&codec_opts);
00095 }
00096
00097 void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
00098 {
00099 vfprintf(stdout, fmt, vl);
00100 }
00101
00102 static void log_callback_report(void *ptr, int level, const char *fmt, va_list vl)
00103 {
00104 va_list vl2;
00105 char line[1024];
00106 static int print_prefix = 1;
00107
00108 va_copy(vl2, vl);
00109 av_log_default_callback(ptr, level, fmt, vl);
00110 av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
00111 va_end(vl2);
00112 fputs(line, report_file);
00113 fflush(report_file);
00114 }
00115
00116 double parse_number_or_die(const char *context, const char *numstr, int type,
00117 double min, double max)
00118 {
00119 char *tail;
00120 const char *error;
00121 double d = av_strtod(numstr, &tail);
00122 if (*tail)
00123 error = "Expected number for %s but found: %s\n";
00124 else if (d < min || d > max)
00125 error = "The value for %s was %s which is not within %f - %f\n";
00126 else if (type == OPT_INT64 && (int64_t)d != d)
00127 error = "Expected int64 for %s but found %s\n";
00128 else if (type == OPT_INT && (int)d != d)
00129 error = "Expected int for %s but found %s\n";
00130 else
00131 return d;
00132 av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
00133 exit(1);
00134 return 0;
00135 }
00136
00137 int64_t parse_time_or_die(const char *context, const char *timestr,
00138 int is_duration)
00139 {
00140 int64_t us;
00141 if (av_parse_time(&us, timestr, is_duration) < 0) {
00142 av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
00143 is_duration ? "duration" : "date", context, timestr);
00144 exit(1);
00145 }
00146 return us;
00147 }
00148
00149 void show_help_options(const OptionDef *options, const char *msg, int req_flags,
00150 int rej_flags, int alt_flags)
00151 {
00152 const OptionDef *po;
00153 int first;
00154
00155 first = 1;
00156 for (po = options; po->name != NULL; po++) {
00157 char buf[64];
00158
00159 if (((po->flags & req_flags) != req_flags) ||
00160 (alt_flags && !(po->flags & alt_flags)) ||
00161 (po->flags & rej_flags))
00162 continue;
00163
00164 if (first) {
00165 printf("%s\n", msg);
00166 first = 0;
00167 }
00168 av_strlcpy(buf, po->name, sizeof(buf));
00169 if (po->argname) {
00170 av_strlcat(buf, " ", sizeof(buf));
00171 av_strlcat(buf, po->argname, sizeof(buf));
00172 }
00173 printf("-%-17s %s\n", buf, po->help);
00174 }
00175 printf("\n");
00176 }
00177
00178 void show_help_children(const AVClass *class, int flags)
00179 {
00180 const AVClass *child = NULL;
00181 if (class->option) {
00182 av_opt_show2(&class, NULL, flags, 0);
00183 printf("\n");
00184 }
00185
00186 while (child = av_opt_child_class_next(class, child))
00187 show_help_children(child, flags);
00188 }
00189
00190 static const OptionDef *find_option(const OptionDef *po, const char *name)
00191 {
00192 const char *p = strchr(name, ':');
00193 int len = p ? p - name : strlen(name);
00194
00195 while (po->name != NULL) {
00196 if (!strncmp(name, po->name, len) && strlen(po->name) == len)
00197 break;
00198 po++;
00199 }
00200 return po;
00201 }
00202
00203 #if HAVE_COMMANDLINETOARGVW
00204 #include <windows.h>
00205 #include <shellapi.h>
00206
00207 static char** win32_argv_utf8 = NULL;
00208 static int win32_argc = 0;
00209
00217 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
00218 {
00219 char *argstr_flat;
00220 wchar_t **argv_w;
00221 int i, buffsize = 0, offset = 0;
00222
00223 if (win32_argv_utf8) {
00224 *argc_ptr = win32_argc;
00225 *argv_ptr = win32_argv_utf8;
00226 return;
00227 }
00228
00229 win32_argc = 0;
00230 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
00231 if (win32_argc <= 0 || !argv_w)
00232 return;
00233
00234
00235 for (i = 0; i < win32_argc; i++)
00236 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
00237 NULL, 0, NULL, NULL);
00238
00239 win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
00240 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
00241 if (win32_argv_utf8 == NULL) {
00242 LocalFree(argv_w);
00243 return;
00244 }
00245
00246 for (i = 0; i < win32_argc; i++) {
00247 win32_argv_utf8[i] = &argstr_flat[offset];
00248 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
00249 &argstr_flat[offset],
00250 buffsize - offset, NULL, NULL);
00251 }
00252 win32_argv_utf8[i] = NULL;
00253 LocalFree(argv_w);
00254
00255 *argc_ptr = win32_argc;
00256 *argv_ptr = win32_argv_utf8;
00257 }
00258 #else
00259 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
00260 {
00261
00262 }
00263 #endif
00264
00265 int parse_option(void *optctx, const char *opt, const char *arg,
00266 const OptionDef *options)
00267 {
00268 const OptionDef *po;
00269 int bool_val = 1;
00270 int *dstcount;
00271 void *dst;
00272
00273 po = find_option(options, opt);
00274 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
00275
00276 po = find_option(options, opt + 2);
00277 if ((po->name && (po->flags & OPT_BOOL)))
00278 bool_val = 0;
00279 }
00280 if (!po->name)
00281 po = find_option(options, "default");
00282 if (!po->name) {
00283 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
00284 return AVERROR(EINVAL);
00285 }
00286 if (po->flags & HAS_ARG && !arg) {
00287 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
00288 return AVERROR(EINVAL);
00289 }
00290
00291
00292
00293 dst = po->flags & (OPT_OFFSET | OPT_SPEC) ? (uint8_t *)optctx + po->u.off
00294 : po->u.dst_ptr;
00295
00296 if (po->flags & OPT_SPEC) {
00297 SpecifierOpt **so = dst;
00298 char *p = strchr(opt, ':');
00299
00300 dstcount = (int *)(so + 1);
00301 *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
00302 (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
00303 dst = &(*so)[*dstcount - 1].u;
00304 }
00305
00306 if (po->flags & OPT_STRING) {
00307 char *str;
00308 str = av_strdup(arg);
00309
00310 *(char **)dst = str;
00311 } else if (po->flags & OPT_BOOL) {
00312 *(int *)dst = bool_val;
00313 } else if (po->flags & OPT_INT) {
00314 *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
00315 } else if (po->flags & OPT_INT64) {
00316 *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
00317 } else if (po->flags & OPT_TIME) {
00318 *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
00319 } else if (po->flags & OPT_FLOAT) {
00320 *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
00321 } else if (po->flags & OPT_DOUBLE) {
00322 *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
00323 } else if (po->u.func_arg) {
00324 int ret = po->u.func_arg(optctx, opt, arg);
00325 if (ret < 0) {
00326 av_log(NULL, AV_LOG_ERROR,
00327 "Failed to set value '%s' for option '%s'\n", arg, opt);
00328 return ret;
00329 }
00330 }
00331 if (po->flags & OPT_EXIT)
00332 exit(0);
00333 return !!(po->flags & HAS_ARG);
00334 }
00335
00336 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
00337 void (*parse_arg_function)(void *, const char*))
00338 {
00339 const char *opt;
00340 int optindex, handleoptions = 1, ret;
00341
00342
00343 prepare_app_arguments(&argc, &argv);
00344
00345
00346 optindex = 1;
00347 while (optindex < argc) {
00348 opt = argv[optindex++];
00349
00350 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
00351 if (opt[1] == '-' && opt[2] == '\0') {
00352 handleoptions = 0;
00353 continue;
00354 }
00355 opt++;
00356
00357 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
00358 exit(1);
00359 optindex += ret;
00360 } else {
00361 if (parse_arg_function)
00362 parse_arg_function(optctx, opt);
00363 }
00364 }
00365 }
00366
00367 int locate_option(int argc, char **argv, const OptionDef *options,
00368 const char *optname)
00369 {
00370 const OptionDef *po;
00371 int i;
00372
00373 for (i = 1; i < argc; i++) {
00374 const char *cur_opt = argv[i];
00375
00376 if (*cur_opt++ != '-')
00377 continue;
00378
00379 po = find_option(options, cur_opt);
00380 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
00381 po = find_option(options, cur_opt + 2);
00382
00383 if ((!po->name && !strcmp(cur_opt, optname)) ||
00384 (po->name && !strcmp(optname, po->name)))
00385 return i;
00386
00387 if (po->flags & HAS_ARG)
00388 i++;
00389 }
00390 return 0;
00391 }
00392
00393 static void dump_argument(const char *a)
00394 {
00395 const unsigned char *p;
00396
00397 for (p = a; *p; p++)
00398 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
00399 *p == '_' || (*p >= 'a' && *p <= 'z')))
00400 break;
00401 if (!*p) {
00402 fputs(a, report_file);
00403 return;
00404 }
00405 fputc('"', report_file);
00406 for (p = a; *p; p++) {
00407 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
00408 fprintf(report_file, "\\%c", *p);
00409 else if (*p < ' ' || *p > '~')
00410 fprintf(report_file, "\\x%02x", *p);
00411 else
00412 fputc(*p, report_file);
00413 }
00414 fputc('"', report_file);
00415 }
00416
00417 void parse_loglevel(int argc, char **argv, const OptionDef *options)
00418 {
00419 int idx = locate_option(argc, argv, options, "loglevel");
00420 const char *env;
00421 if (!idx)
00422 idx = locate_option(argc, argv, options, "v");
00423 if (idx && argv[idx + 1])
00424 opt_loglevel(NULL, "loglevel", argv[idx + 1]);
00425 idx = locate_option(argc, argv, options, "report");
00426 if ((env = getenv("FFREPORT")) || idx) {
00427 init_report(env);
00428 if (report_file) {
00429 int i;
00430 fprintf(report_file, "Command line:\n");
00431 for (i = 0; i < argc; i++) {
00432 dump_argument(argv[i]);
00433 fputc(i < argc - 1 ? ' ' : '\n', report_file);
00434 }
00435 fflush(report_file);
00436 }
00437 }
00438 }
00439
00440 #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
00441 int opt_default(void *optctx, const char *opt, const char *arg)
00442 {
00443 const AVOption *o;
00444 int consumed = 0;
00445 char opt_stripped[128];
00446 const char *p;
00447 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
00448 const AVClass *sc, *swr_class;
00449
00450 if (!(p = strchr(opt, ':')))
00451 p = opt + strlen(opt);
00452 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
00453
00454 if ((o = av_opt_find(&cc, opt_stripped, NULL, 0,
00455 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
00456 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
00457 (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) {
00458 av_dict_set(&codec_opts, opt, arg, FLAGS);
00459 consumed = 1;
00460 }
00461 if ((o = av_opt_find(&fc, opt, NULL, 0,
00462 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
00463 av_dict_set(&format_opts, opt, arg, FLAGS);
00464 if(consumed)
00465 av_log(NULL, AV_LOG_VERBOSE, "Routing %s to codec and muxer layer\n", opt);
00466 consumed = 1;
00467 }
00468 #if CONFIG_SWSCALE
00469 sc = sws_get_class();
00470 if (!consumed && av_opt_find(&sc, opt, NULL, 0,
00471 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
00472
00473 int ret = av_opt_set(sws_opts, opt, arg, 0);
00474 if (ret < 0) {
00475 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
00476 return ret;
00477 }
00478 consumed = 1;
00479 }
00480 #endif
00481 #if CONFIG_SWRESAMPLE
00482 swr_class = swr_get_class();
00483 if (!consumed && av_opt_find(&swr_class, opt, NULL, 0,
00484 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
00485 int ret = av_opt_set(swr_opts, opt, arg, 0);
00486 if (ret < 0) {
00487 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
00488 return ret;
00489 }
00490 consumed = 1;
00491 }
00492 #endif
00493
00494 if (consumed)
00495 return 0;
00496 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
00497 return AVERROR_OPTION_NOT_FOUND;
00498 }
00499
00500 int opt_loglevel(void *optctx, const char *opt, const char *arg)
00501 {
00502 const struct { const char *name; int level; } log_levels[] = {
00503 { "quiet" , AV_LOG_QUIET },
00504 { "panic" , AV_LOG_PANIC },
00505 { "fatal" , AV_LOG_FATAL },
00506 { "error" , AV_LOG_ERROR },
00507 { "warning", AV_LOG_WARNING },
00508 { "info" , AV_LOG_INFO },
00509 { "verbose", AV_LOG_VERBOSE },
00510 { "debug" , AV_LOG_DEBUG },
00511 };
00512 char *tail;
00513 int level;
00514 int i;
00515
00516 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
00517 if (!strcmp(log_levels[i].name, arg)) {
00518 av_log_set_level(log_levels[i].level);
00519 return 0;
00520 }
00521 }
00522
00523 level = strtol(arg, &tail, 10);
00524 if (*tail) {
00525 av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
00526 "Possible levels are numbers or:\n", arg);
00527 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
00528 av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
00529 exit(1);
00530 }
00531 av_log_set_level(level);
00532 return 0;
00533 }
00534
00535 static void expand_filename_template(AVBPrint *bp, const char *template,
00536 struct tm *tm)
00537 {
00538 int c;
00539
00540 while ((c = *(template++))) {
00541 if (c == '%') {
00542 if (!(c = *(template++)))
00543 break;
00544 switch (c) {
00545 case 'p':
00546 av_bprintf(bp, "%s", program_name);
00547 break;
00548 case 't':
00549 av_bprintf(bp, "%04d%02d%02d-%02d%02d%02d",
00550 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
00551 tm->tm_hour, tm->tm_min, tm->tm_sec);
00552 break;
00553 case '%':
00554 av_bprint_chars(bp, c, 1);
00555 break;
00556 }
00557 } else {
00558 av_bprint_chars(bp, c, 1);
00559 }
00560 }
00561 }
00562
00563 static int init_report(const char *env)
00564 {
00565 char *filename_template = NULL;
00566 char *key, *val;
00567 int ret, count = 0;
00568 time_t now;
00569 struct tm *tm;
00570 AVBPrint filename;
00571
00572 if (report_file)
00573 return 0;
00574 time(&now);
00575 tm = localtime(&now);
00576
00577 while (env && *env) {
00578 if ((ret = av_opt_get_key_value(&env, "=", ":", 0, &key, &val)) < 0) {
00579 if (count)
00580 av_log(NULL, AV_LOG_ERROR,
00581 "Failed to parse FFREPORT environment variable: %s\n",
00582 av_err2str(ret));
00583 break;
00584 }
00585 if (*env)
00586 env++;
00587 count++;
00588 if (!strcmp(key, "file")) {
00589 av_free(filename_template);
00590 filename_template = val;
00591 val = NULL;
00592 } else {
00593 av_log(NULL, AV_LOG_ERROR, "Unknown key '%s' in FFREPORT\n", key);
00594 }
00595 av_free(val);
00596 av_free(key);
00597 }
00598
00599 av_bprint_init(&filename, 0, 1);
00600 expand_filename_template(&filename,
00601 av_x_if_null(filename_template, "%p-%t.log"), tm);
00602 av_free(filename_template);
00603 if (!av_bprint_is_complete(&filename)) {
00604 av_log(NULL, AV_LOG_ERROR, "Out of memory building report file name\n");
00605 return AVERROR(ENOMEM);
00606 }
00607
00608 report_file = fopen(filename.str, "w");
00609 if (!report_file) {
00610 av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
00611 filename.str, strerror(errno));
00612 return AVERROR(errno);
00613 }
00614 av_log_set_callback(log_callback_report);
00615 av_log(NULL, AV_LOG_INFO,
00616 "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
00617 "Report written to \"%s\"\n",
00618 program_name,
00619 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
00620 tm->tm_hour, tm->tm_min, tm->tm_sec,
00621 filename.str);
00622 av_log_set_level(FFMAX(av_log_get_level(), AV_LOG_VERBOSE));
00623 av_bprint_finalize(&filename, NULL);
00624 return 0;
00625 }
00626
00627 int opt_report(const char *opt)
00628 {
00629 return init_report(NULL);
00630 }
00631
00632 int opt_max_alloc(void *optctx, const char *opt, const char *arg)
00633 {
00634 char *tail;
00635 size_t max;
00636
00637 max = strtol(arg, &tail, 10);
00638 if (*tail) {
00639 av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
00640 exit(1);
00641 }
00642 av_max_alloc(max);
00643 return 0;
00644 }
00645
00646 int opt_cpuflags(void *optctx, const char *opt, const char *arg)
00647 {
00648 int ret;
00649 unsigned flags = av_get_cpu_flags();
00650
00651 if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
00652 return ret;
00653
00654 av_force_cpu_flags(flags);
00655 return 0;
00656 }
00657
00658 int opt_codec_debug(void *optctx, const char *opt, const char *arg)
00659 {
00660 av_log_set_level(AV_LOG_DEBUG);
00661 return opt_default(NULL, opt, arg);
00662 }
00663
00664 int opt_timelimit(void *optctx, const char *opt, const char *arg)
00665 {
00666 #if HAVE_SETRLIMIT
00667 int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
00668 struct rlimit rl = { lim, lim + 1 };
00669 if (setrlimit(RLIMIT_CPU, &rl))
00670 perror("setrlimit");
00671 #else
00672 av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
00673 #endif
00674 return 0;
00675 }
00676
00677 void print_error(const char *filename, int err)
00678 {
00679 char errbuf[128];
00680 const char *errbuf_ptr = errbuf;
00681
00682 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
00683 errbuf_ptr = strerror(AVUNERROR(err));
00684 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
00685 }
00686
00687 static int warned_cfg = 0;
00688
00689 #define INDENT 1
00690 #define SHOW_VERSION 2
00691 #define SHOW_CONFIG 4
00692 #define SHOW_COPYRIGHT 8
00693
00694 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
00695 if (CONFIG_##LIBNAME) { \
00696 const char *indent = flags & INDENT? " " : ""; \
00697 if (flags & SHOW_VERSION) { \
00698 unsigned int version = libname##_version(); \
00699 av_log(NULL, level, \
00700 "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
00701 indent, #libname, \
00702 LIB##LIBNAME##_VERSION_MAJOR, \
00703 LIB##LIBNAME##_VERSION_MINOR, \
00704 LIB##LIBNAME##_VERSION_MICRO, \
00705 version >> 16, version >> 8 & 0xff, version & 0xff); \
00706 } \
00707 if (flags & SHOW_CONFIG) { \
00708 const char *cfg = libname##_configuration(); \
00709 if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
00710 if (!warned_cfg) { \
00711 av_log(NULL, level, \
00712 "%sWARNING: library configuration mismatch\n", \
00713 indent); \
00714 warned_cfg = 1; \
00715 } \
00716 av_log(NULL, level, "%s%-11s configuration: %s\n", \
00717 indent, #libname, cfg); \
00718 } \
00719 } \
00720 } \
00721
00722 static void print_all_libs_info(int flags, int level)
00723 {
00724 PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
00725 PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
00726 PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
00727 PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
00728 PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
00729
00730 PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
00731 PRINT_LIB_INFO(swresample,SWRESAMPLE, flags, level);
00732 #if CONFIG_POSTPROC
00733 PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
00734 #endif
00735 }
00736
00737 static void print_program_info(int flags, int level)
00738 {
00739 const char *indent = flags & INDENT? " " : "";
00740
00741 av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
00742 if (flags & SHOW_COPYRIGHT)
00743 av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
00744 program_birth_year, this_year);
00745 av_log(NULL, level, "\n");
00746 av_log(NULL, level, "%sbuilt on %s %s with %s\n",
00747 indent, __DATE__, __TIME__, CC_IDENT);
00748
00749 av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
00750 }
00751
00752 void show_banner(int argc, char **argv, const OptionDef *options)
00753 {
00754 int idx = locate_option(argc, argv, options, "version");
00755 if (idx)
00756 return;
00757
00758 print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
00759 print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
00760 print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
00761 }
00762
00763 int show_version(void *optctx, const char *opt, const char *arg)
00764 {
00765 av_log_set_callback(log_callback_help);
00766 print_program_info (0 , AV_LOG_INFO);
00767 print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
00768
00769 return 0;
00770 }
00771
00772 int show_license(void *optctx, const char *opt, const char *arg)
00773 {
00774 #if CONFIG_NONFREE
00775 printf(
00776 "This version of %s has nonfree parts compiled in.\n"
00777 "Therefore it is not legally redistributable.\n",
00778 program_name );
00779 #elif CONFIG_GPLV3
00780 printf(
00781 "%s is free software; you can redistribute it and/or modify\n"
00782 "it under the terms of the GNU General Public License as published by\n"
00783 "the Free Software Foundation; either version 3 of the License, or\n"
00784 "(at your option) any later version.\n"
00785 "\n"
00786 "%s is distributed in the hope that it will be useful,\n"
00787 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
00788 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
00789 "GNU General Public License for more details.\n"
00790 "\n"
00791 "You should have received a copy of the GNU General Public License\n"
00792 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
00793 program_name, program_name, program_name );
00794 #elif CONFIG_GPL
00795 printf(
00796 "%s is free software; you can redistribute it and/or modify\n"
00797 "it under the terms of the GNU General Public License as published by\n"
00798 "the Free Software Foundation; either version 2 of the License, or\n"
00799 "(at your option) any later version.\n"
00800 "\n"
00801 "%s is distributed in the hope that it will be useful,\n"
00802 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
00803 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
00804 "GNU General Public License for more details.\n"
00805 "\n"
00806 "You should have received a copy of the GNU General Public License\n"
00807 "along with %s; if not, write to the Free Software\n"
00808 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
00809 program_name, program_name, program_name );
00810 #elif CONFIG_LGPLV3
00811 printf(
00812 "%s is free software; you can redistribute it and/or modify\n"
00813 "it under the terms of the GNU Lesser General Public License as published by\n"
00814 "the Free Software Foundation; either version 3 of the License, or\n"
00815 "(at your option) any later version.\n"
00816 "\n"
00817 "%s is distributed in the hope that it will be useful,\n"
00818 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
00819 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
00820 "GNU Lesser General Public License for more details.\n"
00821 "\n"
00822 "You should have received a copy of the GNU Lesser General Public License\n"
00823 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
00824 program_name, program_name, program_name );
00825 #else
00826 printf(
00827 "%s is free software; you can redistribute it and/or\n"
00828 "modify it under the terms of the GNU Lesser General Public\n"
00829 "License as published by the Free Software Foundation; either\n"
00830 "version 2.1 of the License, or (at your option) any later version.\n"
00831 "\n"
00832 "%s is distributed in the hope that it will be useful,\n"
00833 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
00834 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
00835 "Lesser General Public License for more details.\n"
00836 "\n"
00837 "You should have received a copy of the GNU Lesser General Public\n"
00838 "License along with %s; if not, write to the Free Software\n"
00839 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
00840 program_name, program_name, program_name );
00841 #endif
00842
00843 return 0;
00844 }
00845
00846 int show_formats(void *optctx, const char *opt, const char *arg)
00847 {
00848 AVInputFormat *ifmt = NULL;
00849 AVOutputFormat *ofmt = NULL;
00850 const char *last_name;
00851
00852 printf("File formats:\n"
00853 " D. = Demuxing supported\n"
00854 " .E = Muxing supported\n"
00855 " --\n");
00856 last_name = "000";
00857 for (;;) {
00858 int decode = 0;
00859 int encode = 0;
00860 const char *name = NULL;
00861 const char *long_name = NULL;
00862
00863 while ((ofmt = av_oformat_next(ofmt))) {
00864 if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
00865 strcmp(ofmt->name, last_name) > 0) {
00866 name = ofmt->name;
00867 long_name = ofmt->long_name;
00868 encode = 1;
00869 }
00870 }
00871 while ((ifmt = av_iformat_next(ifmt))) {
00872 if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
00873 strcmp(ifmt->name, last_name) > 0) {
00874 name = ifmt->name;
00875 long_name = ifmt->long_name;
00876 encode = 0;
00877 }
00878 if (name && strcmp(ifmt->name, name) == 0)
00879 decode = 1;
00880 }
00881 if (name == NULL)
00882 break;
00883 last_name = name;
00884
00885 printf(" %s%s %-15s %s\n",
00886 decode ? "D" : " ",
00887 encode ? "E" : " ",
00888 name,
00889 long_name ? long_name:" ");
00890 }
00891 return 0;
00892 }
00893
00894 #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
00895 if (codec->field) { \
00896 const type *p = codec->field; \
00897 \
00898 printf(" Supported " list_name ":"); \
00899 while (*p != term) { \
00900 get_name(*p); \
00901 printf(" %s", name); \
00902 p++; \
00903 } \
00904 printf("\n"); \
00905 } \
00906
00907 static void print_codec(const AVCodec *c)
00908 {
00909 int encoder = av_codec_is_encoder(c);
00910
00911 printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
00912 c->long_name ? c->long_name : "");
00913
00914 if (c->type == AVMEDIA_TYPE_VIDEO) {
00915 printf(" Threading capabilities: ");
00916 switch (c->capabilities & (CODEC_CAP_FRAME_THREADS |
00917 CODEC_CAP_SLICE_THREADS)) {
00918 case CODEC_CAP_FRAME_THREADS |
00919 CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
00920 case CODEC_CAP_FRAME_THREADS: printf("frame"); break;
00921 case CODEC_CAP_SLICE_THREADS: printf("slice"); break;
00922 default: printf("no"); break;
00923 }
00924 printf("\n");
00925 }
00926
00927 if (c->supported_framerates) {
00928 const AVRational *fps = c->supported_framerates;
00929
00930 printf(" Supported framerates:");
00931 while (fps->num) {
00932 printf(" %d/%d", fps->num, fps->den);
00933 fps++;
00934 }
00935 printf("\n");
00936 }
00937 PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
00938 AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
00939 PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
00940 GET_SAMPLE_RATE_NAME);
00941 PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
00942 AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
00943 PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
00944 0, GET_CH_LAYOUT_DESC);
00945
00946 if (c->priv_class) {
00947 show_help_children(c->priv_class,
00948 AV_OPT_FLAG_ENCODING_PARAM |
00949 AV_OPT_FLAG_DECODING_PARAM);
00950 }
00951 }
00952
00953 static char get_media_type_char(enum AVMediaType type)
00954 {
00955 switch (type) {
00956 case AVMEDIA_TYPE_VIDEO: return 'V';
00957 case AVMEDIA_TYPE_AUDIO: return 'A';
00958 case AVMEDIA_TYPE_DATA: return 'D';
00959 case AVMEDIA_TYPE_SUBTITLE: return 'S';
00960 case AVMEDIA_TYPE_ATTACHMENT:return 'T';
00961 default: return '?';
00962 }
00963 }
00964
00965 static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
00966 int encoder)
00967 {
00968 while ((prev = av_codec_next(prev))) {
00969 if (prev->id == id &&
00970 (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
00971 return prev;
00972 }
00973 return NULL;
00974 }
00975
00976 static int compare_codec_desc(const void *a, const void *b)
00977 {
00978 const AVCodecDescriptor * const *da = a;
00979 const AVCodecDescriptor * const *db = b;
00980
00981 return (*da)->type != (*db)->type ? (*da)->type - (*db)->type :
00982 strcmp((*da)->name, (*db)->name);
00983 }
00984
00985 static unsigned get_codecs_sorted(const AVCodecDescriptor ***rcodecs)
00986 {
00987 const AVCodecDescriptor *desc = NULL;
00988 const AVCodecDescriptor **codecs;
00989 unsigned nb_codecs = 0, i = 0;
00990
00991 while ((desc = avcodec_descriptor_next(desc)))
00992 nb_codecs++;
00993 if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs)))) {
00994 av_log(NULL, AV_LOG_ERROR, "Out of memory\n");
00995 exit(1);
00996 }
00997 desc = NULL;
00998 while ((desc = avcodec_descriptor_next(desc)))
00999 codecs[i++] = desc;
01000 av_assert0(i == nb_codecs);
01001 qsort(codecs, nb_codecs, sizeof(*codecs), compare_codec_desc);
01002 *rcodecs = codecs;
01003 return nb_codecs;
01004 }
01005
01006 static void print_codecs_for_id(enum AVCodecID id, int encoder)
01007 {
01008 const AVCodec *codec = NULL;
01009
01010 printf(" (%s: ", encoder ? "encoders" : "decoders");
01011
01012 while ((codec = next_codec_for_id(id, codec, encoder)))
01013 printf("%s ", codec->name);
01014
01015 printf(")");
01016 }
01017
01018 int show_codecs(void *optctx, const char *opt, const char *arg)
01019 {
01020 const AVCodecDescriptor **codecs;
01021 unsigned i, nb_codecs = get_codecs_sorted(&codecs);
01022
01023 printf("Codecs:\n"
01024 " D..... = Decoding supported\n"
01025 " .E.... = Encoding supported\n"
01026 " ..V... = Video codec\n"
01027 " ..A... = Audio codec\n"
01028 " ..S... = Subtitle codec\n"
01029 " ...I.. = Intra frame-only codec\n"
01030 " ....L. = Lossy compression\n"
01031 " .....S = Lossless compression\n"
01032 " -------\n");
01033 for (i = 0; i < nb_codecs; i++) {
01034 const AVCodecDescriptor *desc = codecs[i];
01035 const AVCodec *codec = NULL;
01036
01037 printf(" ");
01038 printf(avcodec_find_decoder(desc->id) ? "D" : ".");
01039 printf(avcodec_find_encoder(desc->id) ? "E" : ".");
01040
01041 printf("%c", get_media_type_char(desc->type));
01042 printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
01043 printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
01044 printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
01045
01046 printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
01047
01048
01049
01050 while ((codec = next_codec_for_id(desc->id, codec, 0))) {
01051 if (strcmp(codec->name, desc->name)) {
01052 print_codecs_for_id(desc->id, 0);
01053 break;
01054 }
01055 }
01056 codec = NULL;
01057 while ((codec = next_codec_for_id(desc->id, codec, 1))) {
01058 if (strcmp(codec->name, desc->name)) {
01059 print_codecs_for_id(desc->id, 1);
01060 break;
01061 }
01062 }
01063
01064 printf("\n");
01065 }
01066 av_free(codecs);
01067 return 0;
01068 }
01069
01070 static void print_codecs(int encoder)
01071 {
01072 const AVCodecDescriptor **codecs;
01073 unsigned i, nb_codecs = get_codecs_sorted(&codecs);
01074
01075 printf("%s:\n"
01076 " V..... = Video\n"
01077 " A..... = Audio\n"
01078 " S..... = Subtitle\n"
01079 " .F.... = Frame-level multithreading\n"
01080 " ..S... = Slice-level multithreading\n"
01081 " ...X.. = Codec is experimental\n"
01082 " ....B. = Supports draw_horiz_band\n"
01083 " .....D = Supports direct rendering method 1\n"
01084 " ------\n",
01085 encoder ? "Encoders" : "Decoders");
01086 for (i = 0; i < nb_codecs; i++) {
01087 const AVCodecDescriptor *desc = codecs[i];
01088 const AVCodec *codec = NULL;
01089
01090 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
01091 printf(" %c", get_media_type_char(desc->type));
01092 printf((codec->capabilities & CODEC_CAP_FRAME_THREADS) ? "F" : ".");
01093 printf((codec->capabilities & CODEC_CAP_SLICE_THREADS) ? "S" : ".");
01094 printf((codec->capabilities & CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
01095 printf((codec->capabilities & CODEC_CAP_DRAW_HORIZ_BAND)?"B" : ".");
01096 printf((codec->capabilities & CODEC_CAP_DR1) ? "D" : ".");
01097
01098 printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
01099 if (strcmp(codec->name, desc->name))
01100 printf(" (codec %s)", desc->name);
01101
01102 printf("\n");
01103 }
01104 }
01105 av_free(codecs);
01106 }
01107
01108 int show_decoders(void *optctx, const char *opt, const char *arg)
01109 {
01110 print_codecs(0);
01111 return 0;
01112 }
01113
01114 int show_encoders(void *optctx, const char *opt, const char *arg)
01115 {
01116 print_codecs(1);
01117 return 0;
01118 }
01119
01120 int show_bsfs(void *optctx, const char *opt, const char *arg)
01121 {
01122 AVBitStreamFilter *bsf = NULL;
01123
01124 printf("Bitstream filters:\n");
01125 while ((bsf = av_bitstream_filter_next(bsf)))
01126 printf("%s\n", bsf->name);
01127 printf("\n");
01128 return 0;
01129 }
01130
01131 int show_protocols(void *optctx, const char *opt, const char *arg)
01132 {
01133 void *opaque = NULL;
01134 const char *name;
01135
01136 printf("Supported file protocols:\n"
01137 "Input:\n");
01138 while ((name = avio_enum_protocols(&opaque, 0)))
01139 printf("%s\n", name);
01140 printf("Output:\n");
01141 while ((name = avio_enum_protocols(&opaque, 1)))
01142 printf("%s\n", name);
01143 return 0;
01144 }
01145
01146 int show_filters(void *optctx, const char *opt, const char *arg)
01147 {
01148 AVFilter av_unused(**filter) = NULL;
01149 char descr[64], *descr_cur;
01150 int i, j;
01151 const AVFilterPad *pad;
01152
01153 printf("Filters:\n");
01154 #if CONFIG_AVFILTER
01155 while ((filter = av_filter_next(filter)) && *filter) {
01156 descr_cur = descr;
01157 for (i = 0; i < 2; i++) {
01158 if (i) {
01159 *(descr_cur++) = '-';
01160 *(descr_cur++) = '>';
01161 }
01162 pad = i ? (*filter)->outputs : (*filter)->inputs;
01163 for (j = 0; pad && pad[j].name; j++) {
01164 if (descr_cur >= descr + sizeof(descr) - 4)
01165 break;
01166 *(descr_cur++) = get_media_type_char(pad[j].type);
01167 }
01168 if (!j)
01169 *(descr_cur++) = '|';
01170 }
01171 *descr_cur = 0;
01172 printf("%-16s %-10s %s\n", (*filter)->name, descr, (*filter)->description);
01173 }
01174 #endif
01175 return 0;
01176 }
01177
01178 int show_pix_fmts(void *optctx, const char *opt, const char *arg)
01179 {
01180 const AVPixFmtDescriptor *pix_desc = NULL;
01181
01182 printf("Pixel formats:\n"
01183 "I.... = Supported Input format for conversion\n"
01184 ".O... = Supported Output format for conversion\n"
01185 "..H.. = Hardware accelerated format\n"
01186 "...P. = Paletted format\n"
01187 "....B = Bitstream format\n"
01188 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
01189 "-----\n");
01190
01191 #if !CONFIG_SWSCALE
01192 # define sws_isSupportedInput(x) 0
01193 # define sws_isSupportedOutput(x) 0
01194 #endif
01195
01196 while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
01197 enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
01198 if(!pix_desc->name)
01199 continue;
01200 printf("%c%c%c%c%c %-16s %d %2d\n",
01201 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
01202 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
01203 pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
01204 pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
01205 pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
01206 pix_desc->name,
01207 pix_desc->nb_components,
01208 av_get_bits_per_pixel(pix_desc));
01209 }
01210 return 0;
01211 }
01212
01213 int show_layouts(void *optctx, const char *opt, const char *arg)
01214 {
01215 int i = 0;
01216 uint64_t layout, j;
01217 const char *name, *descr;
01218
01219 printf("Individual channels:\n"
01220 "NAME DESCRIPTION\n");
01221 for (i = 0; i < 63; i++) {
01222 name = av_get_channel_name((uint64_t)1 << i);
01223 if (!name)
01224 continue;
01225 descr = av_get_channel_description((uint64_t)1 << i);
01226 printf("%-12s%s\n", name, descr);
01227 }
01228 printf("\nStandard channel layouts:\n"
01229 "NAME DECOMPOSITION\n");
01230 for (i = 0; !av_get_standard_channel_layout(i, &layout, &name); i++) {
01231 if (name) {
01232 printf("%-12s", name);
01233 for (j = 1; j; j <<= 1)
01234 if ((layout & j))
01235 printf("%s%s", (layout & (j - 1)) ? "+" : "", av_get_channel_name(j));
01236 printf("\n");
01237 }
01238 }
01239 return 0;
01240 }
01241
01242 int show_sample_fmts(void *optctx, const char *opt, const char *arg)
01243 {
01244 int i;
01245 char fmt_str[128];
01246 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
01247 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
01248 return 0;
01249 }
01250
01251 static void show_help_codec(const char *name, int encoder)
01252 {
01253 const AVCodecDescriptor *desc;
01254 const AVCodec *codec;
01255
01256 if (!name) {
01257 av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
01258 return;
01259 }
01260
01261 codec = encoder ? avcodec_find_encoder_by_name(name) :
01262 avcodec_find_decoder_by_name(name);
01263
01264 if (codec)
01265 print_codec(codec);
01266 else if ((desc = avcodec_descriptor_get_by_name(name))) {
01267 int printed = 0;
01268
01269 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
01270 printed = 1;
01271 print_codec(codec);
01272 }
01273
01274 if (!printed) {
01275 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, "
01276 "but no %s for it are available. FFmpeg might need to be "
01277 "recompiled with additional external libraries.\n",
01278 name, encoder ? "encoders" : "decoders");
01279 }
01280 } else {
01281 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n",
01282 name);
01283 }
01284 }
01285
01286 static void show_help_demuxer(const char *name)
01287 {
01288 const AVInputFormat *fmt = av_find_input_format(name);
01289
01290 if (!fmt) {
01291 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
01292 return;
01293 }
01294
01295 printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
01296
01297 if (fmt->extensions)
01298 printf(" Common extensions: %s.\n", fmt->extensions);
01299
01300 if (fmt->priv_class)
01301 show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
01302 }
01303
01304 static void show_help_muxer(const char *name)
01305 {
01306 const AVCodecDescriptor *desc;
01307 const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
01308
01309 if (!fmt) {
01310 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
01311 return;
01312 }
01313
01314 printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
01315
01316 if (fmt->extensions)
01317 printf(" Common extensions: %s.\n", fmt->extensions);
01318 if (fmt->mime_type)
01319 printf(" Mime type: %s.\n", fmt->mime_type);
01320 if (fmt->video_codec != AV_CODEC_ID_NONE &&
01321 (desc = avcodec_descriptor_get(fmt->video_codec))) {
01322 printf(" Default video codec: %s.\n", desc->name);
01323 }
01324 if (fmt->audio_codec != AV_CODEC_ID_NONE &&
01325 (desc = avcodec_descriptor_get(fmt->audio_codec))) {
01326 printf(" Default audio codec: %s.\n", desc->name);
01327 }
01328 if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
01329 (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
01330 printf(" Default subtitle codec: %s.\n", desc->name);
01331 }
01332
01333 if (fmt->priv_class)
01334 show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
01335 }
01336
01337 int show_help(void *optctx, const char *opt, const char *arg)
01338 {
01339 char *topic, *par;
01340 av_log_set_callback(log_callback_help);
01341
01342 topic = av_strdup(arg ? arg : "");
01343 par = strchr(topic, '=');
01344 if (par)
01345 *par++ = 0;
01346
01347 if (!*topic) {
01348 show_help_default(topic, par);
01349 } else if (!strcmp(topic, "decoder")) {
01350 show_help_codec(par, 0);
01351 } else if (!strcmp(topic, "encoder")) {
01352 show_help_codec(par, 1);
01353 } else if (!strcmp(topic, "demuxer")) {
01354 show_help_demuxer(par);
01355 } else if (!strcmp(topic, "muxer")) {
01356 show_help_muxer(par);
01357 } else {
01358 show_help_default(topic, par);
01359 }
01360
01361 av_freep(&topic);
01362 return 0;
01363 }
01364
01365 int read_yesno(void)
01366 {
01367 int c = getchar();
01368 int yesno = (toupper(c) == 'Y');
01369
01370 while (c != '\n' && c != EOF)
01371 c = getchar();
01372
01373 return yesno;
01374 }
01375
01376 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
01377 {
01378 int ret;
01379 FILE *f = fopen(filename, "rb");
01380
01381 if (!f) {
01382 av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
01383 strerror(errno));
01384 return AVERROR(errno);
01385 }
01386 fseek(f, 0, SEEK_END);
01387 *size = ftell(f);
01388 fseek(f, 0, SEEK_SET);
01389 if (*size == (size_t)-1) {
01390 av_log(NULL, AV_LOG_ERROR, "IO error: %s\n", strerror(errno));
01391 fclose(f);
01392 return AVERROR(errno);
01393 }
01394 *bufptr = av_malloc(*size + 1);
01395 if (!*bufptr) {
01396 av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
01397 fclose(f);
01398 return AVERROR(ENOMEM);
01399 }
01400 ret = fread(*bufptr, 1, *size, f);
01401 if (ret < *size) {
01402 av_free(*bufptr);
01403 if (ferror(f)) {
01404 av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
01405 filename, strerror(errno));
01406 ret = AVERROR(errno);
01407 } else
01408 ret = AVERROR_EOF;
01409 } else {
01410 ret = 0;
01411 (*bufptr)[(*size)++] = '\0';
01412 }
01413
01414 fclose(f);
01415 return ret;
01416 }
01417
01418 FILE *get_preset_file(char *filename, size_t filename_size,
01419 const char *preset_name, int is_path,
01420 const char *codec_name)
01421 {
01422 FILE *f = NULL;
01423 int i;
01424 const char *base[3] = { getenv("FFMPEG_DATADIR"),
01425 getenv("HOME"),
01426 FFMPEG_DATADIR, };
01427
01428 if (is_path) {
01429 av_strlcpy(filename, preset_name, filename_size);
01430 f = fopen(filename, "r");
01431 } else {
01432 #ifdef _WIN32
01433 char datadir[MAX_PATH], *ls;
01434 base[2] = NULL;
01435
01436 if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
01437 {
01438 for (ls = datadir; ls < datadir + strlen(datadir); ls++)
01439 if (*ls == '\\') *ls = '/';
01440
01441 if (ls = strrchr(datadir, '/'))
01442 {
01443 *ls = 0;
01444 strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
01445 base[2] = datadir;
01446 }
01447 }
01448 #endif
01449 for (i = 0; i < 3 && !f; i++) {
01450 if (!base[i])
01451 continue;
01452 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
01453 i != 1 ? "" : "/.ffmpeg", preset_name);
01454 f = fopen(filename, "r");
01455 if (!f && codec_name) {
01456 snprintf(filename, filename_size,
01457 "%s%s/%s-%s.ffpreset",
01458 base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
01459 preset_name);
01460 f = fopen(filename, "r");
01461 }
01462 }
01463 }
01464
01465 return f;
01466 }
01467
01468 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
01469 {
01470 int ret = avformat_match_stream_specifier(s, st, spec);
01471 if (ret < 0)
01472 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
01473 return ret;
01474 }
01475
01476 AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
01477 AVFormatContext *s, AVStream *st, AVCodec *codec)
01478 {
01479 AVDictionary *ret = NULL;
01480 AVDictionaryEntry *t = NULL;
01481 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
01482 : AV_OPT_FLAG_DECODING_PARAM;
01483 char prefix = 0;
01484 const AVClass *cc = avcodec_get_class();
01485
01486 if (!codec)
01487 codec = s->oformat ? avcodec_find_encoder(codec_id)
01488 : avcodec_find_decoder(codec_id);
01489 if (!codec)
01490 return NULL;
01491
01492 switch (codec->type) {
01493 case AVMEDIA_TYPE_VIDEO:
01494 prefix = 'v';
01495 flags |= AV_OPT_FLAG_VIDEO_PARAM;
01496 break;
01497 case AVMEDIA_TYPE_AUDIO:
01498 prefix = 'a';
01499 flags |= AV_OPT_FLAG_AUDIO_PARAM;
01500 break;
01501 case AVMEDIA_TYPE_SUBTITLE:
01502 prefix = 's';
01503 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
01504 break;
01505 }
01506
01507 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
01508 char *p = strchr(t->key, ':');
01509
01510
01511 if (p)
01512 switch (check_stream_specifier(s, st, p + 1)) {
01513 case 1: *p = 0; break;
01514 case 0: continue;
01515 default: return NULL;
01516 }
01517
01518 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
01519 (codec && codec->priv_class &&
01520 av_opt_find(&codec->priv_class, t->key, NULL, flags,
01521 AV_OPT_SEARCH_FAKE_OBJ)))
01522 av_dict_set(&ret, t->key, t->value, 0);
01523 else if (t->key[0] == prefix &&
01524 av_opt_find(&cc, t->key + 1, NULL, flags,
01525 AV_OPT_SEARCH_FAKE_OBJ))
01526 av_dict_set(&ret, t->key + 1, t->value, 0);
01527
01528 if (p)
01529 *p = ':';
01530 }
01531 return ret;
01532 }
01533
01534 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
01535 AVDictionary *codec_opts)
01536 {
01537 int i;
01538 AVDictionary **opts;
01539
01540 if (!s->nb_streams)
01541 return NULL;
01542 opts = av_mallocz(s->nb_streams * sizeof(*opts));
01543 if (!opts) {
01544 av_log(NULL, AV_LOG_ERROR,
01545 "Could not alloc memory for stream options.\n");
01546 return NULL;
01547 }
01548 for (i = 0; i < s->nb_streams; i++)
01549 opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
01550 s, s->streams[i], NULL);
01551 return opts;
01552 }
01553
01554 void *grow_array(void *array, int elem_size, int *size, int new_size)
01555 {
01556 if (new_size >= INT_MAX / elem_size) {
01557 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
01558 exit(1);
01559 }
01560 if (*size < new_size) {
01561 uint8_t *tmp = av_realloc(array, new_size*elem_size);
01562 if (!tmp) {
01563 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
01564 exit(1);
01565 }
01566 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
01567 *size = new_size;
01568 return tmp;
01569 }
01570 return array;
01571 }
01572
01573 static int alloc_buffer(FrameBuffer **pool, AVCodecContext *s, FrameBuffer **pbuf)
01574 {
01575 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
01576 FrameBuffer *buf;
01577 int i, ret;
01578 int pixel_size;
01579 int h_chroma_shift, v_chroma_shift;
01580 int edge = 32;
01581 int w = s->width, h = s->height;
01582
01583 if (!desc)
01584 return AVERROR(EINVAL);
01585 pixel_size = desc->comp[0].step_minus1 + 1;
01586
01587 buf = av_mallocz(sizeof(*buf));
01588 if (!buf)
01589 return AVERROR(ENOMEM);
01590
01591 avcodec_align_dimensions(s, &w, &h);
01592
01593 if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
01594 w += 2*edge;
01595 h += 2*edge;
01596 }
01597
01598 if ((ret = av_image_alloc(buf->base, buf->linesize, w, h,
01599 s->pix_fmt, 32)) < 0) {
01600 av_freep(&buf);
01601 av_log(s, AV_LOG_ERROR, "alloc_buffer: av_image_alloc() failed\n");
01602 return ret;
01603 }
01604
01605
01606
01607
01608
01609 memset(buf->base[0], 128, ret);
01610
01611 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
01612 for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
01613 const int h_shift = i==0 ? 0 : h_chroma_shift;
01614 const int v_shift = i==0 ? 0 : v_chroma_shift;
01615 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !buf->linesize[i] || !buf->base[i])
01616 buf->data[i] = buf->base[i];
01617 else
01618 buf->data[i] = buf->base[i] +
01619 FFALIGN((buf->linesize[i]*edge >> v_shift) +
01620 (pixel_size*edge >> h_shift), 32);
01621 }
01622 buf->w = s->width;
01623 buf->h = s->height;
01624 buf->pix_fmt = s->pix_fmt;
01625 buf->pool = pool;
01626
01627 *pbuf = buf;
01628 return 0;
01629 }
01630
01631 int codec_get_buffer(AVCodecContext *s, AVFrame *frame)
01632 {
01633 FrameBuffer **pool = s->opaque;
01634 FrameBuffer *buf;
01635 int ret, i;
01636
01637 if(av_image_check_size(s->width, s->height, 0, s) || s->pix_fmt<0) {
01638 av_log(s, AV_LOG_ERROR, "codec_get_buffer: image parameters invalid\n");
01639 return -1;
01640 }
01641
01642 if (!*pool && (ret = alloc_buffer(pool, s, pool)) < 0)
01643 return ret;
01644
01645 buf = *pool;
01646 *pool = buf->next;
01647 buf->next = NULL;
01648 if (buf->w != s->width || buf->h != s->height || buf->pix_fmt != s->pix_fmt) {
01649 av_freep(&buf->base[0]);
01650 av_free(buf);
01651 if ((ret = alloc_buffer(pool, s, &buf)) < 0)
01652 return ret;
01653 }
01654 av_assert0(!buf->refcount);
01655 buf->refcount++;
01656
01657 frame->opaque = buf;
01658 frame->type = FF_BUFFER_TYPE_USER;
01659 frame->extended_data = frame->data;
01660
01661 for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
01662 frame->base[i] = buf->base[i];
01663 frame->data[i] = buf->data[i];
01664 frame->linesize[i] = buf->linesize[i];
01665 }
01666
01667 return 0;
01668 }
01669
01670 static void unref_buffer(FrameBuffer *buf)
01671 {
01672 FrameBuffer **pool = buf->pool;
01673
01674 av_assert0(buf->refcount > 0);
01675 buf->refcount--;
01676 if (!buf->refcount) {
01677 FrameBuffer *tmp;
01678 for(tmp= *pool; tmp; tmp= tmp->next)
01679 av_assert1(tmp != buf);
01680
01681 buf->next = *pool;
01682 *pool = buf;
01683 }
01684 }
01685
01686 void codec_release_buffer(AVCodecContext *s, AVFrame *frame)
01687 {
01688 FrameBuffer *buf = frame->opaque;
01689 int i;
01690
01691 if(frame->type!=FF_BUFFER_TYPE_USER) {
01692 avcodec_default_release_buffer(s, frame);
01693 return;
01694 }
01695
01696 for (i = 0; i < FF_ARRAY_ELEMS(frame->data); i++)
01697 frame->data[i] = NULL;
01698
01699 unref_buffer(buf);
01700 }
01701
01702 void filter_release_buffer(AVFilterBuffer *fb)
01703 {
01704 FrameBuffer *buf = fb->priv;
01705 av_free(fb);
01706 unref_buffer(buf);
01707 }
01708
01709 void free_buffer_pool(FrameBuffer **pool)
01710 {
01711 FrameBuffer *buf = *pool;
01712 while (buf) {
01713 *pool = buf->next;
01714 av_freep(&buf->base[0]);
01715 av_free(buf);
01716 buf = *pool;
01717 }
01718 }