cmdutils.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. /*
  2. * Various utilities for command line tools
  3. * Copyright (c) 2000-2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <errno.h>
  24. #include <math.h>
  25. /* Include only the enabled headers since some compilers (namely, Sun
  26. Studio) will not omit unused inline functions and create undefined
  27. references to libraries that are not being built. */
  28. #include "config.h"
  29. #include "libavformat/avformat.h"
  30. #include "libavfilter/avfilter.h"
  31. #include "libavdevice/avdevice.h"
  32. #include "libswscale/swscale.h"
  33. #include "libpostproc/postprocess.h"
  34. #include "libavutil/avstring.h"
  35. #include "libavutil/parseutils.h"
  36. #include "libavutil/pixdesc.h"
  37. #include "libavutil/eval.h"
  38. #include "libavutil/dict.h"
  39. #include "libavutil/opt.h"
  40. #include "cmdutils.h"
  41. #include "version.h"
  42. #if CONFIG_NETWORK
  43. #include "libavformat/network.h"
  44. #endif
  45. #if HAVE_SYS_RESOURCE_H
  46. #include <sys/resource.h>
  47. #endif
  48. const char **opt_names;
  49. const char **opt_values;
  50. static int opt_name_count;
  51. AVCodecContext *avcodec_opts[AVMEDIA_TYPE_NB];
  52. AVFormatContext *avformat_opts;
  53. struct SwsContext *sws_opts;
  54. AVDictionary *format_opts, *video_opts, *audio_opts, *sub_opts;
  55. static const int this_year = 2013;
  56. void init_opts(void)
  57. {
  58. int i;
  59. for (i = 0; i < AVMEDIA_TYPE_NB; i++)
  60. avcodec_opts[i] = avcodec_alloc_context2(i);
  61. avformat_opts = avformat_alloc_context();
  62. #if CONFIG_SWSCALE
  63. sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC, NULL, NULL, NULL);
  64. #endif
  65. }
  66. void uninit_opts(void)
  67. {
  68. int i;
  69. for (i = 0; i < AVMEDIA_TYPE_NB; i++)
  70. av_freep(&avcodec_opts[i]);
  71. av_freep(&avformat_opts->key);
  72. av_freep(&avformat_opts);
  73. #if CONFIG_SWSCALE
  74. sws_freeContext(sws_opts);
  75. sws_opts = NULL;
  76. #endif
  77. for (i = 0; i < opt_name_count; i++) {
  78. av_freep(&opt_names[i]);
  79. av_freep(&opt_values[i]);
  80. }
  81. av_freep(&opt_names);
  82. av_freep(&opt_values);
  83. opt_name_count = 0;
  84. av_dict_free(&format_opts);
  85. av_dict_free(&video_opts);
  86. av_dict_free(&audio_opts);
  87. av_dict_free(&sub_opts);
  88. }
  89. void log_callback_help(void* ptr, int level, const char* fmt, va_list vl)
  90. {
  91. vfprintf(stdout, fmt, vl);
  92. }
  93. double parse_number_or_die(const char *context, const char *numstr, int type, double min, double max)
  94. {
  95. char *tail;
  96. const char *error;
  97. double d = av_strtod(numstr, &tail);
  98. if (*tail)
  99. error= "Expected number for %s but found: %s\n";
  100. else if (d < min || d > max)
  101. error= "The value for %s was %s which is not within %f - %f\n";
  102. else if(type == OPT_INT64 && (int64_t)d != d)
  103. error= "Expected int64 for %s but found %s\n";
  104. else if (type == OPT_INT && (int)d != d)
  105. error= "Expected int for %s but found %s\n";
  106. else
  107. return d;
  108. fprintf(stderr, error, context, numstr, min, max);
  109. exit(1);
  110. }
  111. int64_t parse_time_or_die(const char *context, const char *timestr, int is_duration)
  112. {
  113. int64_t us;
  114. if (av_parse_time(&us, timestr, is_duration) < 0) {
  115. fprintf(stderr, "Invalid %s specification for %s: %s\n",
  116. is_duration ? "duration" : "date", context, timestr);
  117. exit(1);
  118. }
  119. return us;
  120. }
  121. void show_help_options(const OptionDef *options, const char *msg, int mask, int value)
  122. {
  123. const OptionDef *po;
  124. int first;
  125. first = 1;
  126. for(po = options; po->name != NULL; po++) {
  127. char buf[64];
  128. if ((po->flags & mask) == value) {
  129. if (first) {
  130. printf("%s", msg);
  131. first = 0;
  132. }
  133. av_strlcpy(buf, po->name, sizeof(buf));
  134. if (po->flags & HAS_ARG) {
  135. av_strlcat(buf, " ", sizeof(buf));
  136. av_strlcat(buf, po->argname, sizeof(buf));
  137. }
  138. printf("-%-17s %s\n", buf, po->help);
  139. }
  140. }
  141. }
  142. static const OptionDef* find_option(const OptionDef *po, const char *name){
  143. while (po->name != NULL) {
  144. if (!strcmp(name, po->name))
  145. break;
  146. po++;
  147. }
  148. return po;
  149. }
  150. #if defined(_WIN32) && !defined(__MINGW32CE__)
  151. #include <windows.h>
  152. /* Will be leaked on exit */
  153. static char** win32_argv_utf8 = NULL;
  154. static int win32_argc = 0;
  155. /**
  156. * Prepare command line arguments for executable.
  157. * For Windows - perform wide-char to UTF-8 conversion.
  158. * Input arguments should be main() function arguments.
  159. * @param argc_ptr Arguments number (including executable)
  160. * @param argv_ptr Arguments list.
  161. */
  162. static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
  163. {
  164. char *argstr_flat;
  165. wchar_t **argv_w;
  166. int i, buffsize = 0, offset = 0;
  167. if (win32_argv_utf8) {
  168. *argc_ptr = win32_argc;
  169. *argv_ptr = win32_argv_utf8;
  170. return;
  171. }
  172. win32_argc = 0;
  173. argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
  174. if (win32_argc <= 0 || !argv_w)
  175. return;
  176. /* determine the UTF-8 buffer size (including NULL-termination symbols) */
  177. for (i = 0; i < win32_argc; i++)
  178. buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
  179. NULL, 0, NULL, NULL);
  180. win32_argv_utf8 = av_mallocz(sizeof(char*) * (win32_argc + 1) + buffsize);
  181. argstr_flat = (char*)win32_argv_utf8 + sizeof(char*) * (win32_argc + 1);
  182. if (win32_argv_utf8 == NULL) {
  183. LocalFree(argv_w);
  184. return;
  185. }
  186. for (i = 0; i < win32_argc; i++) {
  187. win32_argv_utf8[i] = &argstr_flat[offset];
  188. offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
  189. &argstr_flat[offset],
  190. buffsize - offset, NULL, NULL);
  191. }
  192. win32_argv_utf8[i] = NULL;
  193. LocalFree(argv_w);
  194. *argc_ptr = win32_argc;
  195. *argv_ptr = win32_argv_utf8;
  196. }
  197. #else
  198. static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
  199. {
  200. /* nothing to do */
  201. }
  202. #endif /* WIN32 && !__MINGW32CE__ */
  203. void parse_options(int argc, char **argv, const OptionDef *options,
  204. int (* parse_arg_function)(const char *opt, const char *arg))
  205. {
  206. const char *opt, *arg;
  207. int optindex, handleoptions=1;
  208. const OptionDef *po;
  209. /* perform system-dependent conversions for arguments list */
  210. prepare_app_arguments(&argc, &argv);
  211. /* parse options */
  212. optindex = 1;
  213. while (optindex < argc) {
  214. opt = argv[optindex++];
  215. if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
  216. int bool_val = 1;
  217. if (opt[1] == '-' && opt[2] == '\0') {
  218. handleoptions = 0;
  219. continue;
  220. }
  221. opt++;
  222. po= find_option(options, opt);
  223. if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
  224. /* handle 'no' bool option */
  225. po = find_option(options, opt + 2);
  226. if (!(po->name && (po->flags & OPT_BOOL)))
  227. goto unknown_opt;
  228. bool_val = 0;
  229. }
  230. if (!po->name)
  231. po= find_option(options, "default");
  232. if (!po->name) {
  233. unknown_opt:
  234. fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
  235. exit(1);
  236. }
  237. arg = NULL;
  238. if (po->flags & HAS_ARG) {
  239. arg = argv[optindex++];
  240. if (!arg) {
  241. fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
  242. exit(1);
  243. }
  244. }
  245. if (po->flags & OPT_STRING) {
  246. char *str;
  247. str = av_strdup(arg);
  248. *po->u.str_arg = str;
  249. } else if (po->flags & OPT_BOOL) {
  250. *po->u.int_arg = bool_val;
  251. } else if (po->flags & OPT_INT) {
  252. *po->u.int_arg = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
  253. } else if (po->flags & OPT_INT64) {
  254. *po->u.int64_arg = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
  255. } else if (po->flags & OPT_FLOAT) {
  256. *po->u.float_arg = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
  257. } else if (po->u.func_arg) {
  258. if (po->u.func_arg(opt, arg) < 0) {
  259. fprintf(stderr, "%s: failed to set value '%s' for option '%s'\n", argv[0], arg, opt);
  260. exit(1);
  261. }
  262. }
  263. if(po->flags & OPT_EXIT)
  264. exit(0);
  265. } else {
  266. if (parse_arg_function) {
  267. if (parse_arg_function(NULL, opt) < 0)
  268. exit(1);
  269. }
  270. }
  271. }
  272. }
  273. #define FLAGS (o->type == FF_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
  274. #define SET_PREFIXED_OPTS(ch, flag, output) \
  275. if (opt[0] == ch && avcodec_opts[0] && (o = av_opt_find(avcodec_opts[0], opt+1, NULL, flag, 0)))\
  276. av_dict_set(&output, opt+1, arg, FLAGS);
  277. static int opt_default2(const char *opt, const char *arg)
  278. {
  279. const AVOption *o;
  280. if ((o = av_opt_find(avcodec_opts[0], opt, NULL, 0, AV_OPT_SEARCH_CHILDREN))) {
  281. if (o->flags & AV_OPT_FLAG_VIDEO_PARAM)
  282. av_dict_set(&video_opts, opt, arg, FLAGS);
  283. if (o->flags & AV_OPT_FLAG_AUDIO_PARAM)
  284. av_dict_set(&audio_opts, opt, arg, FLAGS);
  285. if (o->flags & AV_OPT_FLAG_SUBTITLE_PARAM)
  286. av_dict_set(&sub_opts, opt, arg, FLAGS);
  287. } else if ((o = av_opt_find(avformat_opts, opt, NULL, 0, AV_OPT_SEARCH_CHILDREN)))
  288. av_dict_set(&format_opts, opt, arg, FLAGS);
  289. else if ((o = av_opt_find(sws_opts, opt, NULL, 0, AV_OPT_SEARCH_CHILDREN))) {
  290. // XXX we only support sws_flags, not arbitrary sws options
  291. int ret = av_set_string3(sws_opts, opt, arg, 1, NULL);
  292. if (ret < 0) {
  293. av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
  294. return ret;
  295. }
  296. }
  297. if (!o) {
  298. SET_PREFIXED_OPTS('v', AV_OPT_FLAG_VIDEO_PARAM, video_opts)
  299. SET_PREFIXED_OPTS('a', AV_OPT_FLAG_AUDIO_PARAM, audio_opts)
  300. SET_PREFIXED_OPTS('s', AV_OPT_FLAG_SUBTITLE_PARAM, sub_opts)
  301. }
  302. if (o)
  303. return 0;
  304. fprintf(stderr, "Unrecognized option '%s'\n", opt);
  305. return AVERROR_OPTION_NOT_FOUND;
  306. }
  307. int opt_default(const char *opt, const char *arg){
  308. int type;
  309. int ret= 0;
  310. const AVOption *o= NULL;
  311. int opt_types[]={AV_OPT_FLAG_VIDEO_PARAM, AV_OPT_FLAG_AUDIO_PARAM, 0, AV_OPT_FLAG_SUBTITLE_PARAM, 0};
  312. AVCodec *p = NULL;
  313. AVOutputFormat *oformat = NULL;
  314. AVInputFormat *iformat = NULL;
  315. while ((p = av_codec_next(p))) {
  316. const AVClass *c = p->priv_class;
  317. if (c && av_find_opt(&c, opt, NULL, 0, 0))
  318. break;
  319. }
  320. if (p)
  321. goto out;
  322. while ((oformat = av_oformat_next(oformat))) {
  323. const AVClass *c = oformat->priv_class;
  324. if (c && av_find_opt(&c, opt, NULL, 0, 0))
  325. break;
  326. }
  327. if (oformat)
  328. goto out;
  329. while ((iformat = av_iformat_next(iformat))) {
  330. const AVClass *c = iformat->priv_class;
  331. if (c && av_find_opt(&c, opt, NULL, 0, 0))
  332. break;
  333. }
  334. if (iformat)
  335. goto out;
  336. for(type=0; *avcodec_opts && type<AVMEDIA_TYPE_NB && ret>= 0; type++){
  337. const AVOption *o2 = av_opt_find(avcodec_opts[0], opt, NULL, opt_types[type], 0);
  338. if(o2)
  339. ret = av_set_string3(avcodec_opts[type], opt, arg, 1, &o);
  340. }
  341. if(!o && avformat_opts)
  342. ret = av_set_string3(avformat_opts, opt, arg, 1, &o);
  343. if(!o && sws_opts)
  344. ret = av_set_string3(sws_opts, opt, arg, 1, &o);
  345. if(!o){
  346. if (opt[0] == 'a' && avcodec_opts[AVMEDIA_TYPE_AUDIO])
  347. ret = av_set_string3(avcodec_opts[AVMEDIA_TYPE_AUDIO], opt+1, arg, 1, &o);
  348. else if(opt[0] == 'v' && avcodec_opts[AVMEDIA_TYPE_VIDEO])
  349. ret = av_set_string3(avcodec_opts[AVMEDIA_TYPE_VIDEO], opt+1, arg, 1, &o);
  350. else if(opt[0] == 's' && avcodec_opts[AVMEDIA_TYPE_SUBTITLE])
  351. ret = av_set_string3(avcodec_opts[AVMEDIA_TYPE_SUBTITLE], opt+1, arg, 1, &o);
  352. if (ret >= 0)
  353. opt += 1;
  354. }
  355. if (o && ret < 0) {
  356. fprintf(stderr, "Invalid value '%s' for option '%s'\n", arg, opt);
  357. exit(1);
  358. }
  359. if (!o) {
  360. fprintf(stderr, "Unrecognized option '%s'\n", opt);
  361. exit(1);
  362. }
  363. out:
  364. if ((ret = opt_default2(opt, arg)) < 0)
  365. return ret;
  366. // av_log(NULL, AV_LOG_ERROR, "%s:%s: %f 0x%0X\n", opt, arg, av_get_double(avcodec_opts, opt, NULL), (int)av_get_int(avcodec_opts, opt, NULL));
  367. opt_values= av_realloc(opt_values, sizeof(void*)*(opt_name_count+1));
  368. opt_values[opt_name_count] = av_strdup(arg);
  369. opt_names= av_realloc(opt_names, sizeof(void*)*(opt_name_count+1));
  370. opt_names[opt_name_count++] = av_strdup(opt);
  371. if ((*avcodec_opts && avcodec_opts[0]->debug) || (avformat_opts && avformat_opts->debug))
  372. av_log_set_level(AV_LOG_DEBUG);
  373. return 0;
  374. }
  375. int opt_loglevel(const char *opt, const char *arg)
  376. {
  377. const struct { const char *name; int level; } log_levels[] = {
  378. { "quiet" , AV_LOG_QUIET },
  379. { "panic" , AV_LOG_PANIC },
  380. { "fatal" , AV_LOG_FATAL },
  381. { "error" , AV_LOG_ERROR },
  382. { "warning", AV_LOG_WARNING },
  383. { "info" , AV_LOG_INFO },
  384. { "verbose", AV_LOG_VERBOSE },
  385. { "debug" , AV_LOG_DEBUG },
  386. };
  387. char *tail;
  388. int level;
  389. int i;
  390. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
  391. if (!strcmp(log_levels[i].name, arg)) {
  392. av_log_set_level(log_levels[i].level);
  393. return 0;
  394. }
  395. }
  396. level = strtol(arg, &tail, 10);
  397. if (*tail) {
  398. fprintf(stderr, "Invalid loglevel \"%s\". "
  399. "Possible levels are numbers or:\n", arg);
  400. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
  401. fprintf(stderr, "\"%s\"\n", log_levels[i].name);
  402. exit(1);
  403. }
  404. av_log_set_level(level);
  405. return 0;
  406. }
  407. int opt_timelimit(const char *opt, const char *arg)
  408. {
  409. #if HAVE_SETRLIMIT
  410. int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
  411. struct rlimit rl = { lim, lim + 1 };
  412. if (setrlimit(RLIMIT_CPU, &rl))
  413. perror("setrlimit");
  414. #else
  415. fprintf(stderr, "Warning: -%s not implemented on this OS\n", opt);
  416. #endif
  417. return 0;
  418. }
  419. static void *alloc_priv_context(int size, const AVClass *class)
  420. {
  421. void *p = av_mallocz(size);
  422. if (p) {
  423. *(const AVClass **)p = class;
  424. av_opt_set_defaults(p);
  425. }
  426. return p;
  427. }
  428. void set_context_opts(void *ctx, void *opts_ctx, int flags, AVCodec *codec)
  429. {
  430. int i;
  431. void *priv_ctx=NULL;
  432. if(!strcmp("AVCodecContext", (*(AVClass**)ctx)->class_name)){
  433. AVCodecContext *avctx= ctx;
  434. if(codec && codec->priv_class){
  435. if(!avctx->priv_data && codec->priv_data_size)
  436. avctx->priv_data= alloc_priv_context(codec->priv_data_size, codec->priv_class);
  437. priv_ctx= avctx->priv_data;
  438. }
  439. } else if (!strcmp("AVFormatContext", (*(AVClass**)ctx)->class_name)) {
  440. AVFormatContext *avctx = ctx;
  441. if (avctx->oformat && avctx->oformat->priv_class) {
  442. priv_ctx = avctx->priv_data;
  443. } else if (avctx->iformat && avctx->iformat->priv_class) {
  444. priv_ctx = avctx->priv_data;
  445. }
  446. }
  447. for(i=0; i<opt_name_count; i++){
  448. char buf[256];
  449. const AVOption *opt;
  450. const char *str;
  451. if (priv_ctx) {
  452. if (av_find_opt(priv_ctx, opt_names[i], NULL, flags, flags)) {
  453. if (av_set_string3(priv_ctx, opt_names[i], opt_values[i], 1, NULL) < 0) {
  454. fprintf(stderr, "Invalid value '%s' for option '%s'\n",
  455. opt_names[i], opt_values[i]);
  456. exit(1);
  457. }
  458. } else
  459. goto global;
  460. } else {
  461. global:
  462. str = av_get_string(opts_ctx, opt_names[i], &opt, buf, sizeof(buf));
  463. /* if an option with name opt_names[i] is present in opts_ctx then str is non-NULL */
  464. if (str && ((opt->flags & flags) == flags))
  465. av_set_string3(ctx, opt_names[i], str, 1, NULL);
  466. }
  467. }
  468. }
  469. void print_error(const char *filename, int err)
  470. {
  471. char errbuf[128];
  472. const char *errbuf_ptr = errbuf;
  473. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  474. errbuf_ptr = strerror(AVUNERROR(err));
  475. fprintf(stderr, "%s: %s\n", filename, errbuf_ptr);
  476. }
  477. static int warned_cfg = 0;
  478. #define INDENT 1
  479. #define SHOW_VERSION 2
  480. #define SHOW_CONFIG 4
  481. #define PRINT_LIB_INFO(outstream,libname,LIBNAME,flags) \
  482. if (CONFIG_##LIBNAME) { \
  483. const char *indent = flags & INDENT? " " : ""; \
  484. if (flags & SHOW_VERSION) { \
  485. unsigned int version = libname##_version(); \
  486. fprintf(outstream, "%slib%-9s %2d.%3d.%2d / %2d.%3d.%2d\n", \
  487. indent, #libname, \
  488. LIB##LIBNAME##_VERSION_MAJOR, \
  489. LIB##LIBNAME##_VERSION_MINOR, \
  490. LIB##LIBNAME##_VERSION_MICRO, \
  491. version >> 16, version >> 8 & 0xff, version & 0xff); \
  492. } \
  493. if (flags & SHOW_CONFIG) { \
  494. const char *cfg = libname##_configuration(); \
  495. if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
  496. if (!warned_cfg) { \
  497. fprintf(outstream, \
  498. "%sWARNING: library configuration mismatch\n", \
  499. indent); \
  500. warned_cfg = 1; \
  501. } \
  502. fprintf(stderr, "%s%-11s configuration: %s\n", \
  503. indent, #libname, cfg); \
  504. } \
  505. } \
  506. } \
  507. static void print_all_libs_info(FILE* outstream, int flags)
  508. {
  509. PRINT_LIB_INFO(outstream, avutil, AVUTIL, flags);
  510. PRINT_LIB_INFO(outstream, avcodec, AVCODEC, flags);
  511. PRINT_LIB_INFO(outstream, avformat, AVFORMAT, flags);
  512. PRINT_LIB_INFO(outstream, avdevice, AVDEVICE, flags);
  513. PRINT_LIB_INFO(outstream, avfilter, AVFILTER, flags);
  514. PRINT_LIB_INFO(outstream, swscale, SWSCALE, flags);
  515. PRINT_LIB_INFO(outstream, postproc, POSTPROC, flags);
  516. }
  517. void show_banner(void)
  518. {
  519. fprintf(stderr, "%s version " FFMPEG_VERSION ", Copyright (c) %d-%d the FFmpeg developers\n",
  520. program_name, program_birth_year, this_year);
  521. fprintf(stderr, " built on %s %s with %s %s\n",
  522. __DATE__, __TIME__, CC_TYPE, CC_VERSION);
  523. fprintf(stderr, " configuration: " FFMPEG_CONFIGURATION "\n");
  524. print_all_libs_info(stderr, INDENT|SHOW_CONFIG);
  525. print_all_libs_info(stderr, INDENT|SHOW_VERSION);
  526. }
  527. int opt_version(const char *opt, const char *arg) {
  528. printf("%s " FFMPEG_VERSION "\n", program_name);
  529. print_all_libs_info(stdout, SHOW_VERSION);
  530. return 0;
  531. }
  532. int opt_license(const char *opt, const char *arg)
  533. {
  534. printf(
  535. #if CONFIG_NONFREE
  536. "This version of %s has nonfree parts compiled in.\n"
  537. "Therefore it is not legally redistributable.\n",
  538. program_name
  539. #elif CONFIG_GPLV3
  540. "%s is free software; you can redistribute it and/or modify\n"
  541. "it under the terms of the GNU General Public License as published by\n"
  542. "the Free Software Foundation; either version 3 of the License, or\n"
  543. "(at your option) any later version.\n"
  544. "\n"
  545. "%s is distributed in the hope that it will be useful,\n"
  546. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  547. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  548. "GNU General Public License for more details.\n"
  549. "\n"
  550. "You should have received a copy of the GNU General Public License\n"
  551. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  552. program_name, program_name, program_name
  553. #elif CONFIG_GPL
  554. "%s is free software; you can redistribute it and/or modify\n"
  555. "it under the terms of the GNU General Public License as published by\n"
  556. "the Free Software Foundation; either version 2 of the License, or\n"
  557. "(at your option) any later version.\n"
  558. "\n"
  559. "%s is distributed in the hope that it will be useful,\n"
  560. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  561. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  562. "GNU General Public License for more details.\n"
  563. "\n"
  564. "You should have received a copy of the GNU General Public License\n"
  565. "along with %s; if not, write to the Free Software\n"
  566. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  567. program_name, program_name, program_name
  568. #elif CONFIG_LGPLV3
  569. "%s is free software; you can redistribute it and/or modify\n"
  570. "it under the terms of the GNU Lesser General Public License as published by\n"
  571. "the Free Software Foundation; either version 3 of the License, or\n"
  572. "(at your option) any later version.\n"
  573. "\n"
  574. "%s is distributed in the hope that it will be useful,\n"
  575. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  576. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  577. "GNU Lesser General Public License for more details.\n"
  578. "\n"
  579. "You should have received a copy of the GNU Lesser General Public License\n"
  580. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  581. program_name, program_name, program_name
  582. #else
  583. "%s is free software; you can redistribute it and/or\n"
  584. "modify it under the terms of the GNU Lesser General Public\n"
  585. "License as published by the Free Software Foundation; either\n"
  586. "version 2.1 of the License, or (at your option) any later version.\n"
  587. "\n"
  588. "%s is distributed in the hope that it will be useful,\n"
  589. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  590. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  591. "Lesser General Public License for more details.\n"
  592. "\n"
  593. "You should have received a copy of the GNU Lesser General Public\n"
  594. "License along with %s; if not, write to the Free Software\n"
  595. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  596. program_name, program_name, program_name
  597. #endif
  598. );
  599. return 0;
  600. }
  601. int opt_formats(const char *opt, const char *arg)
  602. {
  603. AVInputFormat *ifmt=NULL;
  604. AVOutputFormat *ofmt=NULL;
  605. const char *last_name;
  606. printf(
  607. "File formats:\n"
  608. " D. = Demuxing supported\n"
  609. " .E = Muxing supported\n"
  610. " --\n");
  611. last_name= "000";
  612. for(;;){
  613. int decode=0;
  614. int encode=0;
  615. const char *name=NULL;
  616. const char *long_name=NULL;
  617. while((ofmt= av_oformat_next(ofmt))) {
  618. if((name == NULL || strcmp(ofmt->name, name)<0) &&
  619. strcmp(ofmt->name, last_name)>0){
  620. name= ofmt->name;
  621. long_name= ofmt->long_name;
  622. encode=1;
  623. }
  624. }
  625. while((ifmt= av_iformat_next(ifmt))) {
  626. if((name == NULL || strcmp(ifmt->name, name)<0) &&
  627. strcmp(ifmt->name, last_name)>0){
  628. name= ifmt->name;
  629. long_name= ifmt->long_name;
  630. encode=0;
  631. }
  632. if(name && strcmp(ifmt->name, name)==0)
  633. decode=1;
  634. }
  635. if(name==NULL)
  636. break;
  637. last_name= name;
  638. printf(
  639. " %s%s %-15s %s\n",
  640. decode ? "D":" ",
  641. encode ? "E":" ",
  642. name,
  643. long_name ? long_name:" ");
  644. }
  645. return 0;
  646. }
  647. int opt_codecs(const char *opt, const char *arg)
  648. {
  649. AVCodec *p=NULL, *p2;
  650. const char *last_name;
  651. printf(
  652. "Codecs:\n"
  653. " D..... = Decoding supported\n"
  654. " .E.... = Encoding supported\n"
  655. " ..V... = Video codec\n"
  656. " ..A... = Audio codec\n"
  657. " ..S... = Subtitle codec\n"
  658. " ...S.. = Supports draw_horiz_band\n"
  659. " ....D. = Supports direct rendering method 1\n"
  660. " .....T = Supports weird frame truncation\n"
  661. " ------\n");
  662. last_name= "000";
  663. for(;;){
  664. int decode=0;
  665. int encode=0;
  666. int cap=0;
  667. const char *type_str;
  668. p2=NULL;
  669. while((p= av_codec_next(p))) {
  670. if((p2==NULL || strcmp(p->name, p2->name)<0) &&
  671. strcmp(p->name, last_name)>0){
  672. p2= p;
  673. decode= encode= cap=0;
  674. }
  675. if(p2 && strcmp(p->name, p2->name)==0){
  676. if(p->decode) decode=1;
  677. if(p->encode) encode=1;
  678. cap |= p->capabilities;
  679. }
  680. }
  681. if(p2==NULL)
  682. break;
  683. last_name= p2->name;
  684. switch(p2->type) {
  685. case AVMEDIA_TYPE_VIDEO:
  686. type_str = "V";
  687. break;
  688. case AVMEDIA_TYPE_AUDIO:
  689. type_str = "A";
  690. break;
  691. case AVMEDIA_TYPE_SUBTITLE:
  692. type_str = "S";
  693. break;
  694. default:
  695. type_str = "?";
  696. break;
  697. }
  698. printf(
  699. " %s%s%s%s%s%s %-15s %s",
  700. decode ? "D": (/*p2->decoder ? "d":*/" "),
  701. encode ? "E":" ",
  702. type_str,
  703. cap & CODEC_CAP_DRAW_HORIZ_BAND ? "S":" ",
  704. cap & CODEC_CAP_DR1 ? "D":" ",
  705. cap & CODEC_CAP_TRUNCATED ? "T":" ",
  706. p2->name,
  707. p2->long_name ? p2->long_name : "");
  708. /* if(p2->decoder && decode==0)
  709. printf(" use %s for decoding", p2->decoder->name);*/
  710. printf("\n");
  711. }
  712. printf("\n");
  713. printf(
  714. "Note, the names of encoders and decoders do not always match, so there are\n"
  715. "several cases where the above table shows encoder only or decoder only entries\n"
  716. "even though both encoding and decoding are supported. For example, the h263\n"
  717. "decoder corresponds to the h263 and h263p encoders, for file formats it is even\n"
  718. "worse.\n");
  719. return 0;
  720. }
  721. int opt_bsfs(const char *opt, const char *arg)
  722. {
  723. AVBitStreamFilter *bsf=NULL;
  724. printf("Bitstream filters:\n");
  725. while((bsf = av_bitstream_filter_next(bsf)))
  726. printf("%s\n", bsf->name);
  727. printf("\n");
  728. return 0;
  729. }
  730. int opt_protocols(const char *opt, const char *arg)
  731. {
  732. URLProtocol *up=NULL;
  733. printf("Supported file protocols:\n"
  734. "I.. = Input supported\n"
  735. ".O. = Output supported\n"
  736. "..S = Seek supported\n"
  737. "FLAGS NAME\n"
  738. "----- \n");
  739. while((up = av_protocol_next(up)))
  740. printf("%c%c%c %s\n",
  741. up->url_read ? 'I' : '.',
  742. up->url_write ? 'O' : '.',
  743. up->url_seek ? 'S' : '.',
  744. up->name);
  745. return 0;
  746. }
  747. int opt_filters(const char *opt, const char *arg)
  748. {
  749. AVFilter av_unused(**filter) = NULL;
  750. printf("Filters:\n");
  751. #if CONFIG_AVFILTER
  752. while ((filter = av_filter_next(filter)) && *filter)
  753. printf("%-16s %s\n", (*filter)->name, (*filter)->description);
  754. #endif
  755. return 0;
  756. }
  757. int opt_pix_fmts(const char *opt, const char *arg)
  758. {
  759. enum PixelFormat pix_fmt;
  760. printf(
  761. "Pixel formats:\n"
  762. "I.... = Supported Input format for conversion\n"
  763. ".O... = Supported Output format for conversion\n"
  764. "..H.. = Hardware accelerated format\n"
  765. "...P. = Paletted format\n"
  766. "....B = Bitstream format\n"
  767. "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
  768. "-----\n");
  769. #if !CONFIG_SWSCALE
  770. # define sws_isSupportedInput(x) 0
  771. # define sws_isSupportedOutput(x) 0
  772. #endif
  773. for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) {
  774. const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
  775. printf("%c%c%c%c%c %-16s %d %2d\n",
  776. sws_isSupportedInput (pix_fmt) ? 'I' : '.',
  777. sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
  778. pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
  779. pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
  780. pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
  781. pix_desc->name,
  782. pix_desc->nb_components,
  783. av_get_bits_per_pixel(pix_desc));
  784. }
  785. return 0;
  786. }
  787. int read_yesno(void)
  788. {
  789. int c = getchar();
  790. int yesno = (toupper(c) == 'Y');
  791. while (c != '\n' && c != EOF)
  792. c = getchar();
  793. return yesno;
  794. }
  795. int read_file(const char *filename, char **bufptr, size_t *size)
  796. {
  797. FILE *f = fopen(filename, "rb");
  798. if (!f) {
  799. fprintf(stderr, "Cannot read file '%s': %s\n", filename, strerror(errno));
  800. return AVERROR(errno);
  801. }
  802. fseek(f, 0, SEEK_END);
  803. *size = ftell(f);
  804. fseek(f, 0, SEEK_SET);
  805. *bufptr = av_malloc(*size + 1);
  806. if (!*bufptr) {
  807. fprintf(stderr, "Could not allocate file buffer\n");
  808. fclose(f);
  809. return AVERROR(ENOMEM);
  810. }
  811. fread(*bufptr, 1, *size, f);
  812. (*bufptr)[*size++] = '\0';
  813. fclose(f);
  814. return 0;
  815. }
  816. FILE *get_preset_file(char *filename, size_t filename_size,
  817. const char *preset_name, int is_path, const char *codec_name)
  818. {
  819. FILE *f = NULL;
  820. int i;
  821. const char *base[3]= { getenv("FFMPEG_DATADIR"),
  822. getenv("HOME"),
  823. FFMPEG_DATADIR,
  824. };
  825. if (is_path) {
  826. av_strlcpy(filename, preset_name, filename_size);
  827. f = fopen(filename, "r");
  828. } else {
  829. #ifdef _WIN32
  830. char datadir[MAX_PATH], *ls;
  831. base[2] = NULL;
  832. if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
  833. {
  834. for (ls = datadir; ls < datadir + strlen(datadir); ls++)
  835. if (*ls == '\\') *ls = '/';
  836. if (ls = strrchr(datadir, '/'))
  837. {
  838. *ls = 0;
  839. strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
  840. base[2] = datadir;
  841. }
  842. }
  843. #endif
  844. for (i = 0; i < 3 && !f; i++) {
  845. if (!base[i])
  846. continue;
  847. snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i], i != 1 ? "" : "/.ffmpeg", preset_name);
  848. f = fopen(filename, "r");
  849. if (!f && codec_name) {
  850. snprintf(filename, filename_size,
  851. "%s%s/%s-%s.ffpreset", base[i], i != 1 ? "" : "/.ffmpeg", codec_name, preset_name);
  852. f = fopen(filename, "r");
  853. }
  854. }
  855. }
  856. return f;
  857. }