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 "libavformat/avformat.h"
00033 #include "libavfilter/avfilter.h"
00034 #include "libavdevice/avdevice.h"
00035 #include "libswscale/swscale.h"
00036 #include "libswresample/swresample.h"
00037 #if CONFIG_POSTPROC
00038 #include "libpostproc/postprocess.h"
00039 #endif
00040 #include "libavutil/avstring.h"
00041 #include "libavutil/mathematics.h"
00042 #include "libavutil/parseutils.h"
00043 #include "libavutil/pixdesc.h"
00044 #include "libavutil/eval.h"
00045 #include "libavutil/dict.h"
00046 #include "libavutil/opt.h"
00047 #include "cmdutils.h"
00048 #include "version.h"
00049 #if CONFIG_NETWORK
00050 #include "libavformat/network.h"
00051 #endif
00052 #if HAVE_SYS_RESOURCE_H
00053 #include <sys/resource.h>
00054 #endif
00055
00056 struct SwsContext *sws_opts;
00057 AVDictionary *format_opts, *codec_opts;
00058
00059 const int this_year = 2012;
00060
00061 static FILE *report_file;
00062
00063 void init_opts(void)
00064 {
00065 #if CONFIG_SWSCALE
00066 sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
00067 NULL, NULL, NULL);
00068 #endif
00069 }
00070
00071 void uninit_opts(void)
00072 {
00073 #if CONFIG_SWSCALE
00074 sws_freeContext(sws_opts);
00075 sws_opts = NULL;
00076 #endif
00077 av_dict_free(&format_opts);
00078 av_dict_free(&codec_opts);
00079 }
00080
00081 void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
00082 {
00083 vfprintf(stdout, fmt, vl);
00084 }
00085
00086 static void log_callback_report(void *ptr, int level, const char *fmt, va_list vl)
00087 {
00088 va_list vl2;
00089 char line[1024];
00090 static int print_prefix = 1;
00091
00092 va_copy(vl2, vl);
00093 av_log_default_callback(ptr, level, fmt, vl);
00094 av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
00095 va_end(vl2);
00096 fputs(line, report_file);
00097 fflush(report_file);
00098 }
00099
00100 double parse_number_or_die(const char *context, const char *numstr, int type,
00101 double min, double max)
00102 {
00103 char *tail;
00104 const char *error;
00105 double d = av_strtod(numstr, &tail);
00106 if (*tail)
00107 error = "Expected number for %s but found: %s\n";
00108 else if (d < min || d > max)
00109 error = "The value for %s was %s which is not within %f - %f\n";
00110 else if (type == OPT_INT64 && (int64_t)d != d)
00111 error = "Expected int64 for %s but found %s\n";
00112 else if (type == OPT_INT && (int)d != d)
00113 error = "Expected int for %s but found %s\n";
00114 else
00115 return d;
00116 av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
00117 exit_program(1);
00118 return 0;
00119 }
00120
00121 int64_t parse_time_or_die(const char *context, const char *timestr,
00122 int is_duration)
00123 {
00124 int64_t us;
00125 if (av_parse_time(&us, timestr, is_duration) < 0) {
00126 av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
00127 is_duration ? "duration" : "date", context, timestr);
00128 exit_program(1);
00129 }
00130 return us;
00131 }
00132
00133 void show_help_options(const OptionDef *options, const char *msg, int mask,
00134 int value)
00135 {
00136 const OptionDef *po;
00137 int first;
00138
00139 first = 1;
00140 for (po = options; po->name != NULL; po++) {
00141 char buf[64];
00142 if ((po->flags & mask) == value) {
00143 if (first) {
00144 printf("%s", msg);
00145 first = 0;
00146 }
00147 av_strlcpy(buf, po->name, sizeof(buf));
00148 if (po->flags & HAS_ARG) {
00149 av_strlcat(buf, " ", sizeof(buf));
00150 av_strlcat(buf, po->argname, sizeof(buf));
00151 }
00152 printf("-%-17s %s\n", buf, po->help);
00153 }
00154 }
00155 }
00156
00157 void show_help_children(const AVClass *class, int flags)
00158 {
00159 const AVClass *child = NULL;
00160 av_opt_show2(&class, NULL, flags, 0);
00161 printf("\n");
00162
00163 while (child = av_opt_child_class_next(class, child))
00164 show_help_children(child, flags);
00165 }
00166
00167 static const OptionDef *find_option(const OptionDef *po, const char *name)
00168 {
00169 const char *p = strchr(name, ':');
00170 int len = p ? p - name : strlen(name);
00171
00172 while (po->name != NULL) {
00173 if (!strncmp(name, po->name, len) && strlen(po->name) == len)
00174 break;
00175 po++;
00176 }
00177 return po;
00178 }
00179
00180 #if defined(_WIN32) && !defined(__MINGW32CE__)
00181 #include <windows.h>
00182
00183 static char** win32_argv_utf8 = NULL;
00184 static int win32_argc = 0;
00185
00193 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
00194 {
00195 char *argstr_flat;
00196 wchar_t **argv_w;
00197 int i, buffsize = 0, offset = 0;
00198
00199 if (win32_argv_utf8) {
00200 *argc_ptr = win32_argc;
00201 *argv_ptr = win32_argv_utf8;
00202 return;
00203 }
00204
00205 win32_argc = 0;
00206 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
00207 if (win32_argc <= 0 || !argv_w)
00208 return;
00209
00210
00211 for (i = 0; i < win32_argc; i++)
00212 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
00213 NULL, 0, NULL, NULL);
00214
00215 win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
00216 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
00217 if (win32_argv_utf8 == NULL) {
00218 LocalFree(argv_w);
00219 return;
00220 }
00221
00222 for (i = 0; i < win32_argc; i++) {
00223 win32_argv_utf8[i] = &argstr_flat[offset];
00224 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
00225 &argstr_flat[offset],
00226 buffsize - offset, NULL, NULL);
00227 }
00228 win32_argv_utf8[i] = NULL;
00229 LocalFree(argv_w);
00230
00231 *argc_ptr = win32_argc;
00232 *argv_ptr = win32_argv_utf8;
00233 }
00234 #else
00235 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
00236 {
00237
00238 }
00239 #endif
00240
00241 int parse_option(void *optctx, const char *opt, const char *arg,
00242 const OptionDef *options)
00243 {
00244 const OptionDef *po;
00245 int bool_val = 1;
00246 int *dstcount;
00247 void *dst;
00248
00249 po = find_option(options, opt);
00250 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
00251
00252 po = find_option(options, opt + 2);
00253 if (!(po->name && (po->flags & OPT_BOOL)))
00254 goto unknown_opt;
00255 bool_val = 0;
00256 }
00257 if (!po->name)
00258 po = find_option(options, "default");
00259 if (!po->name) {
00260 unknown_opt:
00261 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
00262 return AVERROR(EINVAL);
00263 }
00264 if (po->flags & HAS_ARG && !arg) {
00265 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
00266 return AVERROR(EINVAL);
00267 }
00268
00269
00270
00271 dst = po->flags & (OPT_OFFSET | OPT_SPEC) ? (uint8_t *)optctx + po->u.off
00272 : po->u.dst_ptr;
00273
00274 if (po->flags & OPT_SPEC) {
00275 SpecifierOpt **so = dst;
00276 char *p = strchr(opt, ':');
00277
00278 dstcount = (int *)(so + 1);
00279 *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
00280 (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
00281 dst = &(*so)[*dstcount - 1].u;
00282 }
00283
00284 if (po->flags & OPT_STRING) {
00285 char *str;
00286 str = av_strdup(arg);
00287 *(char **)dst = str;
00288 } else if (po->flags & OPT_BOOL) {
00289 *(int *)dst = bool_val;
00290 } else if (po->flags & OPT_INT) {
00291 *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
00292 } else if (po->flags & OPT_INT64) {
00293 *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
00294 } else if (po->flags & OPT_TIME) {
00295 *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
00296 } else if (po->flags & OPT_FLOAT) {
00297 *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
00298 } else if (po->flags & OPT_DOUBLE) {
00299 *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
00300 } else if (po->u.func_arg) {
00301 int ret = po->flags & OPT_FUNC2 ? po->u.func2_arg(optctx, opt, arg)
00302 : po->u.func_arg(opt, arg);
00303 if (ret < 0) {
00304 av_log(NULL, AV_LOG_ERROR,
00305 "Failed to set value '%s' for option '%s'\n", arg, opt);
00306 return ret;
00307 }
00308 }
00309 if (po->flags & OPT_EXIT)
00310 exit_program(0);
00311 return !!(po->flags & HAS_ARG);
00312 }
00313
00314 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
00315 void (*parse_arg_function)(void *, const char*))
00316 {
00317 const char *opt;
00318 int optindex, handleoptions = 1, ret;
00319
00320
00321 prepare_app_arguments(&argc, &argv);
00322
00323
00324 optindex = 1;
00325 while (optindex < argc) {
00326 opt = argv[optindex++];
00327
00328 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
00329 if (opt[1] == '-' && opt[2] == '\0') {
00330 handleoptions = 0;
00331 continue;
00332 }
00333 opt++;
00334
00335 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
00336 exit_program(1);
00337 optindex += ret;
00338 } else {
00339 if (parse_arg_function)
00340 parse_arg_function(optctx, opt);
00341 }
00342 }
00343 }
00344
00345
00346
00347
00348 static int locate_option(int argc, char **argv, const OptionDef *options,
00349 const char *optname)
00350 {
00351 const OptionDef *po;
00352 int i;
00353
00354 for (i = 1; i < argc; i++) {
00355 const char *cur_opt = argv[i];
00356
00357 if (*cur_opt++ != '-')
00358 continue;
00359
00360 po = find_option(options, cur_opt);
00361 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
00362 po = find_option(options, cur_opt + 2);
00363
00364 if ((!po->name && !strcmp(cur_opt, optname)) ||
00365 (po->name && !strcmp(optname, po->name)))
00366 return i;
00367
00368 if (!po || po->flags & HAS_ARG)
00369 i++;
00370 }
00371 return 0;
00372 }
00373
00374 static void dump_argument(const char *a)
00375 {
00376 const unsigned char *p;
00377
00378 for (p = a; *p; p++)
00379 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
00380 *p == '_' || (*p >= 'a' && *p <= 'z')))
00381 break;
00382 if (!*p) {
00383 fputs(a, report_file);
00384 return;
00385 }
00386 fputc('"', report_file);
00387 for (p = a; *p; p++) {
00388 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
00389 fprintf(report_file, "\\%c", *p);
00390 else if (*p < ' ' || *p > '~')
00391 fprintf(report_file, "\\x%02x", *p);
00392 else
00393 fputc(*p, report_file);
00394 }
00395 fputc('"', report_file);
00396 }
00397
00398 void parse_loglevel(int argc, char **argv, const OptionDef *options)
00399 {
00400 int idx = locate_option(argc, argv, options, "loglevel");
00401 if (!idx)
00402 idx = locate_option(argc, argv, options, "v");
00403 if (idx && argv[idx + 1])
00404 opt_loglevel("loglevel", argv[idx + 1]);
00405 idx = locate_option(argc, argv, options, "report");
00406 if (idx || getenv("FFREPORT")) {
00407 opt_report("report");
00408 if (report_file) {
00409 int i;
00410 fprintf(report_file, "Command line:\n");
00411 for (i = 0; i < argc; i++) {
00412 dump_argument(argv[i]);
00413 fputc(i < argc - 1 ? ' ' : '\n', report_file);
00414 }
00415 fflush(report_file);
00416 }
00417 }
00418 }
00419
00420 #define FLAGS(o) ((o)->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
00421 int opt_default(const char *opt, const char *arg)
00422 {
00423 const AVOption *oc, *of, *os;
00424 char opt_stripped[128];
00425 const char *p;
00426 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class(), *sc;
00427
00428 if (!(p = strchr(opt, ':')))
00429 p = opt + strlen(opt);
00430 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
00431
00432 if ((oc = av_opt_find(&cc, opt_stripped, NULL, 0,
00433 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
00434 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
00435 (oc = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
00436 av_dict_set(&codec_opts, opt, arg, FLAGS(oc));
00437 if ((of = av_opt_find(&fc, opt, NULL, 0,
00438 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
00439 av_dict_set(&format_opts, opt, arg, FLAGS(of));
00440 #if CONFIG_SWSCALE
00441 sc = sws_get_class();
00442 if ((os = av_opt_find(&sc, opt, NULL, 0,
00443 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
00444
00445 int ret = av_opt_set(sws_opts, opt, arg, 0);
00446 if (ret < 0) {
00447 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
00448 return ret;
00449 }
00450 }
00451 #endif
00452
00453 if (oc || of || os)
00454 return 0;
00455 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
00456 return AVERROR_OPTION_NOT_FOUND;
00457 }
00458
00459 int opt_loglevel(const char *opt, const char *arg)
00460 {
00461 const struct { const char *name; int level; } log_levels[] = {
00462 { "quiet" , AV_LOG_QUIET },
00463 { "panic" , AV_LOG_PANIC },
00464 { "fatal" , AV_LOG_FATAL },
00465 { "error" , AV_LOG_ERROR },
00466 { "warning", AV_LOG_WARNING },
00467 { "info" , AV_LOG_INFO },
00468 { "verbose", AV_LOG_VERBOSE },
00469 { "debug" , AV_LOG_DEBUG },
00470 };
00471 char *tail;
00472 int level;
00473 int i;
00474
00475 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
00476 if (!strcmp(log_levels[i].name, arg)) {
00477 av_log_set_level(log_levels[i].level);
00478 return 0;
00479 }
00480 }
00481
00482 level = strtol(arg, &tail, 10);
00483 if (*tail) {
00484 av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
00485 "Possible levels are numbers or:\n", arg);
00486 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
00487 av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
00488 exit_program(1);
00489 }
00490 av_log_set_level(level);
00491 return 0;
00492 }
00493
00494 int opt_report(const char *opt)
00495 {
00496 char filename[64];
00497 time_t now;
00498 struct tm *tm;
00499
00500 if (report_file)
00501 return 0;
00502 time(&now);
00503 tm = localtime(&now);
00504 snprintf(filename, sizeof(filename), "%s-%04d%02d%02d-%02d%02d%02d.log",
00505 program_name,
00506 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
00507 tm->tm_hour, tm->tm_min, tm->tm_sec);
00508 report_file = fopen(filename, "w");
00509 if (!report_file) {
00510 av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
00511 filename, strerror(errno));
00512 return AVERROR(errno);
00513 }
00514 av_log_set_callback(log_callback_report);
00515 av_log(NULL, AV_LOG_INFO,
00516 "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
00517 "Report written to \"%s\"\n",
00518 program_name,
00519 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
00520 tm->tm_hour, tm->tm_min, tm->tm_sec,
00521 filename);
00522 av_log_set_level(FFMAX(av_log_get_level(), AV_LOG_VERBOSE));
00523 return 0;
00524 }
00525
00526 int opt_max_alloc(const char *opt, const char *arg)
00527 {
00528 char *tail;
00529 size_t max;
00530
00531 max = strtol(arg, &tail, 10);
00532 if (*tail) {
00533 av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
00534 exit_program(1);
00535 }
00536 av_max_alloc(max);
00537 return 0;
00538 }
00539
00540 int opt_codec_debug(const char *opt, const char *arg)
00541 {
00542 av_log_set_level(AV_LOG_DEBUG);
00543 return opt_default(opt, arg);
00544 }
00545
00546 int opt_timelimit(const char *opt, const char *arg)
00547 {
00548 #if HAVE_SETRLIMIT
00549 int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
00550 struct rlimit rl = { lim, lim + 1 };
00551 if (setrlimit(RLIMIT_CPU, &rl))
00552 perror("setrlimit");
00553 #else
00554 av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
00555 #endif
00556 return 0;
00557 }
00558
00559 void print_error(const char *filename, int err)
00560 {
00561 char errbuf[128];
00562 const char *errbuf_ptr = errbuf;
00563
00564 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
00565 errbuf_ptr = strerror(AVUNERROR(err));
00566 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
00567 }
00568
00569 static int warned_cfg = 0;
00570
00571 #define INDENT 1
00572 #define SHOW_VERSION 2
00573 #define SHOW_CONFIG 4
00574 #define SHOW_COPYRIGHT 8
00575
00576 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
00577 if (CONFIG_##LIBNAME) { \
00578 const char *indent = flags & INDENT? " " : ""; \
00579 if (flags & SHOW_VERSION) { \
00580 unsigned int version = libname##_version(); \
00581 av_log(NULL, level, "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n",\
00582 indent, #libname, \
00583 LIB##LIBNAME##_VERSION_MAJOR, \
00584 LIB##LIBNAME##_VERSION_MINOR, \
00585 LIB##LIBNAME##_VERSION_MICRO, \
00586 version >> 16, version >> 8 & 0xff, version & 0xff); \
00587 } \
00588 if (flags & SHOW_CONFIG) { \
00589 const char *cfg = libname##_configuration(); \
00590 if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
00591 if (!warned_cfg) { \
00592 av_log(NULL, level, \
00593 "%sWARNING: library configuration mismatch\n", \
00594 indent); \
00595 warned_cfg = 1; \
00596 } \
00597 av_log(NULL, level, "%s%-11s configuration: %s\n", \
00598 indent, #libname, cfg); \
00599 } \
00600 } \
00601 } \
00602
00603 static void print_all_libs_info(int flags, int level)
00604 {
00605 PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
00606 PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
00607 PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
00608 PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
00609 PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
00610 PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
00611 PRINT_LIB_INFO(swresample,SWRESAMPLE, flags, level);
00612 #if CONFIG_POSTPROC
00613 PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
00614 #endif
00615 }
00616
00617 static void print_program_info(int flags, int level)
00618 {
00619 const char *indent = flags & INDENT? " " : "";
00620
00621 av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
00622 if (flags & SHOW_COPYRIGHT)
00623 av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
00624 program_birth_year, this_year);
00625 av_log(NULL, level, "\n");
00626 av_log(NULL, level, "%sbuilt on %s %s with %s %s\n",
00627 indent, __DATE__, __TIME__, CC_TYPE, CC_VERSION);
00628 av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
00629 }
00630
00631 void show_banner(int argc, char **argv, const OptionDef *options)
00632 {
00633 int idx = locate_option(argc, argv, options, "version");
00634 if (idx)
00635 return;
00636
00637 print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
00638 print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
00639 print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
00640 }
00641
00642 int opt_version(const char *opt, const char *arg) {
00643 av_log_set_callback(log_callback_help);
00644 print_program_info (0 , AV_LOG_INFO);
00645 print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
00646 return 0;
00647 }
00648
00649 int opt_license(const char *opt, const char *arg)
00650 {
00651 printf(
00652 #if CONFIG_NONFREE
00653 "This version of %s has nonfree parts compiled in.\n"
00654 "Therefore it is not legally redistributable.\n",
00655 program_name
00656 #elif CONFIG_GPLV3
00657 "%s is free software; you can redistribute it and/or modify\n"
00658 "it under the terms of the GNU General Public License as published by\n"
00659 "the Free Software Foundation; either version 3 of the License, or\n"
00660 "(at your option) any later version.\n"
00661 "\n"
00662 "%s is distributed in the hope that it will be useful,\n"
00663 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
00664 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
00665 "GNU General Public License for more details.\n"
00666 "\n"
00667 "You should have received a copy of the GNU General Public License\n"
00668 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
00669 program_name, program_name, program_name
00670 #elif CONFIG_GPL
00671 "%s is free software; you can redistribute it and/or modify\n"
00672 "it under the terms of the GNU General Public License as published by\n"
00673 "the Free Software Foundation; either version 2 of the License, or\n"
00674 "(at your option) any later version.\n"
00675 "\n"
00676 "%s is distributed in the hope that it will be useful,\n"
00677 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
00678 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
00679 "GNU General Public License for more details.\n"
00680 "\n"
00681 "You should have received a copy of the GNU General Public License\n"
00682 "along with %s; if not, write to the Free Software\n"
00683 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
00684 program_name, program_name, program_name
00685 #elif CONFIG_LGPLV3
00686 "%s is free software; you can redistribute it and/or modify\n"
00687 "it under the terms of the GNU Lesser General Public License as published by\n"
00688 "the Free Software Foundation; either version 3 of the License, or\n"
00689 "(at your option) any later version.\n"
00690 "\n"
00691 "%s is distributed in the hope that it will be useful,\n"
00692 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
00693 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
00694 "GNU Lesser General Public License for more details.\n"
00695 "\n"
00696 "You should have received a copy of the GNU Lesser General Public License\n"
00697 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
00698 program_name, program_name, program_name
00699 #else
00700 "%s is free software; you can redistribute it and/or\n"
00701 "modify it under the terms of the GNU Lesser General Public\n"
00702 "License as published by the Free Software Foundation; either\n"
00703 "version 2.1 of the License, or (at your option) any later version.\n"
00704 "\n"
00705 "%s is distributed in the hope that it will be useful,\n"
00706 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
00707 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
00708 "Lesser General Public License for more details.\n"
00709 "\n"
00710 "You should have received a copy of the GNU Lesser General Public\n"
00711 "License along with %s; if not, write to the Free Software\n"
00712 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
00713 program_name, program_name, program_name
00714 #endif
00715 );
00716 return 0;
00717 }
00718
00719 int opt_formats(const char *opt, const char *arg)
00720 {
00721 AVInputFormat *ifmt = NULL;
00722 AVOutputFormat *ofmt = NULL;
00723 const char *last_name;
00724
00725 printf("File formats:\n"
00726 " D. = Demuxing supported\n"
00727 " .E = Muxing supported\n"
00728 " --\n");
00729 last_name = "000";
00730 for (;;) {
00731 int decode = 0;
00732 int encode = 0;
00733 const char *name = NULL;
00734 const char *long_name = NULL;
00735
00736 while ((ofmt = av_oformat_next(ofmt))) {
00737 if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
00738 strcmp(ofmt->name, last_name) > 0) {
00739 name = ofmt->name;
00740 long_name = ofmt->long_name;
00741 encode = 1;
00742 }
00743 }
00744 while ((ifmt = av_iformat_next(ifmt))) {
00745 if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
00746 strcmp(ifmt->name, last_name) > 0) {
00747 name = ifmt->name;
00748 long_name = ifmt->long_name;
00749 encode = 0;
00750 }
00751 if (name && strcmp(ifmt->name, name) == 0)
00752 decode = 1;
00753 }
00754 if (name == NULL)
00755 break;
00756 last_name = name;
00757
00758 printf(" %s%s %-15s %s\n",
00759 decode ? "D" : " ",
00760 encode ? "E" : " ",
00761 name,
00762 long_name ? long_name:" ");
00763 }
00764 return 0;
00765 }
00766
00767 static char get_media_type_char(enum AVMediaType type)
00768 {
00769 static const char map[AVMEDIA_TYPE_NB] = {
00770 [AVMEDIA_TYPE_VIDEO] = 'V',
00771 [AVMEDIA_TYPE_AUDIO] = 'A',
00772 [AVMEDIA_TYPE_DATA] = 'D',
00773 [AVMEDIA_TYPE_SUBTITLE] = 'S',
00774 [AVMEDIA_TYPE_ATTACHMENT] = 'T',
00775 };
00776 return type >= 0 && type < AVMEDIA_TYPE_NB && map[type] ? map[type] : '?';
00777 }
00778
00779 int opt_codecs(const char *opt, const char *arg)
00780 {
00781 AVCodec *p = NULL, *p2;
00782 const char *last_name;
00783 printf("Codecs:\n"
00784 " D..... = Decoding supported\n"
00785 " .E.... = Encoding supported\n"
00786 " ..V... = Video codec\n"
00787 " ..A... = Audio codec\n"
00788 " ..S... = Subtitle codec\n"
00789 " ...S.. = Supports draw_horiz_band\n"
00790 " ....D. = Supports direct rendering method 1\n"
00791 " .....T = Supports weird frame truncation\n"
00792 " ------\n");
00793 last_name= "000";
00794 for (;;) {
00795 int decode = 0;
00796 int encode = 0;
00797 int cap = 0;
00798
00799 p2 = NULL;
00800 while ((p = av_codec_next(p))) {
00801 if ((p2 == NULL || strcmp(p->name, p2->name) < 0) &&
00802 strcmp(p->name, last_name) > 0) {
00803 p2 = p;
00804 decode = encode = cap = 0;
00805 }
00806 if (p2 && strcmp(p->name, p2->name) == 0) {
00807 if (p->decode)
00808 decode = 1;
00809 if (p->encode || p->encode2)
00810 encode = 1;
00811 cap |= p->capabilities;
00812 }
00813 }
00814 if (p2 == NULL)
00815 break;
00816 last_name = p2->name;
00817
00818 printf(" %s%s%c%s%s%s %-15s %s",
00819 decode ? "D" : ( " "),
00820 encode ? "E" : " ",
00821 get_media_type_char(p2->type),
00822 cap & CODEC_CAP_DRAW_HORIZ_BAND ? "S" : " ",
00823 cap & CODEC_CAP_DR1 ? "D" : " ",
00824 cap & CODEC_CAP_TRUNCATED ? "T" : " ",
00825 p2->name,
00826 p2->long_name ? p2->long_name : "");
00827 #if 0
00828 if (p2->decoder && decode == 0)
00829 printf(" use %s for decoding", p2->decoder->name);
00830 #endif
00831 printf("\n");
00832 }
00833 printf("\n");
00834 printf("Note, the names of encoders and decoders do not always match, so there are\n"
00835 "several cases where the above table shows encoder only or decoder only entries\n"
00836 "even though both encoding and decoding are supported. For example, the h263\n"
00837 "decoder corresponds to the h263 and h263p encoders, for file formats it is even\n"
00838 "worse.\n");
00839 return 0;
00840 }
00841
00842 int opt_bsfs(const char *opt, const char *arg)
00843 {
00844 AVBitStreamFilter *bsf = NULL;
00845
00846 printf("Bitstream filters:\n");
00847 while ((bsf = av_bitstream_filter_next(bsf)))
00848 printf("%s\n", bsf->name);
00849 printf("\n");
00850 return 0;
00851 }
00852
00853 int opt_protocols(const char *opt, const char *arg)
00854 {
00855 URLProtocol *up=NULL;
00856
00857 printf("Supported file protocols:\n"
00858 "I.. = Input supported\n"
00859 ".O. = Output supported\n"
00860 "..S = Seek supported\n"
00861 "FLAGS NAME\n"
00862 "----- \n");
00863 while((up = av_protocol_next(up)))
00864 printf("%c%c%c %s\n",
00865 up->url_read ? 'I' : '.',
00866 up->url_write ? 'O' : '.',
00867 up->url_seek ? 'S' : '.',
00868 up->name);
00869 return 0;
00870 }
00871
00872 int opt_filters(const char *opt, const char *arg)
00873 {
00874 AVFilter av_unused(**filter) = NULL;
00875 char descr[64], *descr_cur;
00876 int i, j;
00877 const AVFilterPad *pad;
00878
00879 printf("Filters:\n");
00880 #if CONFIG_AVFILTER
00881 while ((filter = av_filter_next(filter)) && *filter) {
00882 descr_cur = descr;
00883 for (i = 0; i < 2; i++) {
00884 if (i) {
00885 *(descr_cur++) = '-';
00886 *(descr_cur++) = '>';
00887 }
00888 pad = i ? (*filter)->outputs : (*filter)->inputs;
00889 for (j = 0; pad[j].name; j++) {
00890 if (descr_cur >= descr + sizeof(descr) - 4)
00891 break;
00892 *(descr_cur++) = get_media_type_char(pad[j].type);
00893 }
00894 if (!j)
00895 *(descr_cur++) = '|';
00896 }
00897 *descr_cur = 0;
00898 printf("%-16s %-10s %s\n", (*filter)->name, descr, (*filter)->description);
00899 }
00900 #endif
00901 return 0;
00902 }
00903
00904 int opt_pix_fmts(const char *opt, const char *arg)
00905 {
00906 enum PixelFormat pix_fmt;
00907
00908 printf("Pixel formats:\n"
00909 "I.... = Supported Input format for conversion\n"
00910 ".O... = Supported Output format for conversion\n"
00911 "..H.. = Hardware accelerated format\n"
00912 "...P. = Paletted format\n"
00913 "....B = Bitstream format\n"
00914 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
00915 "-----\n");
00916
00917 #if !CONFIG_SWSCALE
00918 # define sws_isSupportedInput(x) 0
00919 # define sws_isSupportedOutput(x) 0
00920 #endif
00921
00922 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) {
00923 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
00924 if(!pix_desc->name)
00925 continue;
00926 printf("%c%c%c%c%c %-16s %d %2d\n",
00927 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
00928 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
00929 pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
00930 pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
00931 pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
00932 pix_desc->name,
00933 pix_desc->nb_components,
00934 av_get_bits_per_pixel(pix_desc));
00935 }
00936 return 0;
00937 }
00938
00939 int show_sample_fmts(const char *opt, const char *arg)
00940 {
00941 int i;
00942 char fmt_str[128];
00943 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
00944 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
00945 return 0;
00946 }
00947
00948 int read_yesno(void)
00949 {
00950 int c = getchar();
00951 int yesno = (toupper(c) == 'Y');
00952
00953 while (c != '\n' && c != EOF)
00954 c = getchar();
00955
00956 return yesno;
00957 }
00958
00959 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
00960 {
00961 int ret;
00962 FILE *f = fopen(filename, "rb");
00963
00964 if (!f) {
00965 av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
00966 strerror(errno));
00967 return AVERROR(errno);
00968 }
00969 fseek(f, 0, SEEK_END);
00970 *size = ftell(f);
00971 fseek(f, 0, SEEK_SET);
00972 *bufptr = av_malloc(*size + 1);
00973 if (!*bufptr) {
00974 av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
00975 fclose(f);
00976 return AVERROR(ENOMEM);
00977 }
00978 ret = fread(*bufptr, 1, *size, f);
00979 if (ret < *size) {
00980 av_free(*bufptr);
00981 if (ferror(f)) {
00982 av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
00983 filename, strerror(errno));
00984 ret = AVERROR(errno);
00985 } else
00986 ret = AVERROR_EOF;
00987 } else {
00988 ret = 0;
00989 (*bufptr)[*size++] = '\0';
00990 }
00991
00992 fclose(f);
00993 return ret;
00994 }
00995
00996 FILE *get_preset_file(char *filename, size_t filename_size,
00997 const char *preset_name, int is_path,
00998 const char *codec_name)
00999 {
01000 FILE *f = NULL;
01001 int i;
01002 const char *base[3] = { getenv("FFMPEG_DATADIR"),
01003 getenv("HOME"),
01004 FFMPEG_DATADIR, };
01005
01006 if (is_path) {
01007 av_strlcpy(filename, preset_name, filename_size);
01008 f = fopen(filename, "r");
01009 } else {
01010 #ifdef _WIN32
01011 char datadir[MAX_PATH], *ls;
01012 base[2] = NULL;
01013
01014 if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
01015 {
01016 for (ls = datadir; ls < datadir + strlen(datadir); ls++)
01017 if (*ls == '\\') *ls = '/';
01018
01019 if (ls = strrchr(datadir, '/'))
01020 {
01021 *ls = 0;
01022 strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
01023 base[2] = datadir;
01024 }
01025 }
01026 #endif
01027 for (i = 0; i < 3 && !f; i++) {
01028 if (!base[i])
01029 continue;
01030 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
01031 i != 1 ? "" : "/.ffmpeg", preset_name);
01032 f = fopen(filename, "r");
01033 if (!f && codec_name) {
01034 snprintf(filename, filename_size,
01035 "%s%s/%s-%s.ffpreset",
01036 base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
01037 preset_name);
01038 f = fopen(filename, "r");
01039 }
01040 }
01041 }
01042
01043 return f;
01044 }
01045
01046 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
01047 {
01048 if (*spec <= '9' && *spec >= '0')
01049 return strtol(spec, NULL, 0) == st->index;
01050 else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
01051 *spec == 't') {
01052 enum AVMediaType type;
01053
01054 switch (*spec++) {
01055 case 'v': type = AVMEDIA_TYPE_VIDEO; break;
01056 case 'a': type = AVMEDIA_TYPE_AUDIO; break;
01057 case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
01058 case 'd': type = AVMEDIA_TYPE_DATA; break;
01059 case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
01060 default: abort();
01061 }
01062 if (type != st->codec->codec_type)
01063 return 0;
01064 if (*spec++ == ':') {
01065 int i, index = strtol(spec, NULL, 0);
01066 for (i = 0; i < s->nb_streams; i++)
01067 if (s->streams[i]->codec->codec_type == type && index-- == 0)
01068 return i == st->index;
01069 return 0;
01070 }
01071 return 1;
01072 } else if (*spec == 'p' && *(spec + 1) == ':') {
01073 int prog_id, i, j;
01074 char *endptr;
01075 spec += 2;
01076 prog_id = strtol(spec, &endptr, 0);
01077 for (i = 0; i < s->nb_programs; i++) {
01078 if (s->programs[i]->id != prog_id)
01079 continue;
01080
01081 if (*endptr++ == ':') {
01082 int stream_idx = strtol(endptr, NULL, 0);
01083 return stream_idx >= 0 &&
01084 stream_idx < s->programs[i]->nb_stream_indexes &&
01085 st->index == s->programs[i]->stream_index[stream_idx];
01086 }
01087
01088 for (j = 0; j < s->programs[i]->nb_stream_indexes; j++)
01089 if (st->index == s->programs[i]->stream_index[j])
01090 return 1;
01091 }
01092 return 0;
01093 } else if (!*spec)
01094 return 1;
01095
01096 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
01097 return AVERROR(EINVAL);
01098 }
01099
01100 AVDictionary *filter_codec_opts(AVDictionary *opts, AVCodec *codec,
01101 AVFormatContext *s, AVStream *st)
01102 {
01103 AVDictionary *ret = NULL;
01104 AVDictionaryEntry *t = NULL;
01105 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
01106 : AV_OPT_FLAG_DECODING_PARAM;
01107 char prefix = 0;
01108 const AVClass *cc = avcodec_get_class();
01109
01110 if (!codec)
01111 return NULL;
01112
01113 switch (codec->type) {
01114 case AVMEDIA_TYPE_VIDEO:
01115 prefix = 'v';
01116 flags |= AV_OPT_FLAG_VIDEO_PARAM;
01117 break;
01118 case AVMEDIA_TYPE_AUDIO:
01119 prefix = 'a';
01120 flags |= AV_OPT_FLAG_AUDIO_PARAM;
01121 break;
01122 case AVMEDIA_TYPE_SUBTITLE:
01123 prefix = 's';
01124 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
01125 break;
01126 }
01127
01128 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
01129 char *p = strchr(t->key, ':');
01130
01131
01132 if (p)
01133 switch (check_stream_specifier(s, st, p + 1)) {
01134 case 1: *p = 0; break;
01135 case 0: continue;
01136 default: return NULL;
01137 }
01138
01139 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
01140 (codec && codec->priv_class &&
01141 av_opt_find(&codec->priv_class, t->key, NULL, flags,
01142 AV_OPT_SEARCH_FAKE_OBJ)))
01143 av_dict_set(&ret, t->key, t->value, 0);
01144 else if (t->key[0] == prefix &&
01145 av_opt_find(&cc, t->key + 1, NULL, flags,
01146 AV_OPT_SEARCH_FAKE_OBJ))
01147 av_dict_set(&ret, t->key + 1, t->value, 0);
01148
01149 if (p)
01150 *p = ':';
01151 }
01152 return ret;
01153 }
01154
01155 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
01156 AVDictionary *codec_opts)
01157 {
01158 int i;
01159 AVDictionary **opts;
01160
01161 if (!s->nb_streams)
01162 return NULL;
01163 opts = av_mallocz(s->nb_streams * sizeof(*opts));
01164 if (!opts) {
01165 av_log(NULL, AV_LOG_ERROR,
01166 "Could not alloc memory for stream options.\n");
01167 return NULL;
01168 }
01169 for (i = 0; i < s->nb_streams; i++)
01170 opts[i] = filter_codec_opts(codec_opts, avcodec_find_decoder(s->streams[i]->codec->codec_id),
01171 s, s->streams[i]);
01172 return opts;
01173 }
01174
01175 void *grow_array(void *array, int elem_size, int *size, int new_size)
01176 {
01177 if (new_size >= INT_MAX / elem_size) {
01178 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
01179 exit_program(1);
01180 }
01181 if (*size < new_size) {
01182 uint8_t *tmp = av_realloc(array, new_size*elem_size);
01183 if (!tmp) {
01184 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
01185 exit_program(1);
01186 }
01187 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
01188 *size = new_size;
01189 return tmp;
01190 }
01191 return array;
01192 }