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