You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1666 lines
52KB

  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 "compat/va_copy.h"
  30. #include "libavformat/avformat.h"
  31. #include "libavfilter/avfilter.h"
  32. #include "libavdevice/avdevice.h"
  33. #include "libavresample/avresample.h"
  34. #include "libswscale/swscale.h"
  35. #include "libswresample/swresample.h"
  36. #if CONFIG_POSTPROC
  37. #include "libpostproc/postprocess.h"
  38. #endif
  39. #include "libavutil/avassert.h"
  40. #include "libavutil/avstring.h"
  41. #include "libavutil/mathematics.h"
  42. #include "libavutil/imgutils.h"
  43. #include "libavutil/parseutils.h"
  44. #include "libavutil/pixdesc.h"
  45. #include "libavutil/eval.h"
  46. #include "libavutil/dict.h"
  47. #include "libavutil/opt.h"
  48. #include "cmdutils.h"
  49. #include "version.h"
  50. #if CONFIG_NETWORK
  51. #include "libavformat/network.h"
  52. #endif
  53. #if HAVE_SYS_RESOURCE_H
  54. #include <sys/time.h>
  55. #include <sys/resource.h>
  56. #endif
  57. struct SwsContext *sws_opts;
  58. SwrContext *swr_opts;
  59. AVDictionary *format_opts, *codec_opts;
  60. const int this_year = 2012;
  61. static FILE *report_file;
  62. void init_opts(void)
  63. {
  64. if(CONFIG_SWSCALE)
  65. sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
  66. NULL, NULL, NULL);
  67. if(CONFIG_SWRESAMPLE)
  68. swr_opts = swr_alloc();
  69. }
  70. void uninit_opts(void)
  71. {
  72. #if CONFIG_SWSCALE
  73. sws_freeContext(sws_opts);
  74. sws_opts = NULL;
  75. #endif
  76. if(CONFIG_SWRESAMPLE)
  77. swr_free(&swr_opts);
  78. av_dict_free(&format_opts);
  79. av_dict_free(&codec_opts);
  80. }
  81. void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
  82. {
  83. vfprintf(stdout, fmt, vl);
  84. }
  85. static void log_callback_report(void *ptr, int level, const char *fmt, va_list vl)
  86. {
  87. va_list vl2;
  88. char line[1024];
  89. static int print_prefix = 1;
  90. va_copy(vl2, vl);
  91. av_log_default_callback(ptr, level, fmt, vl);
  92. av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
  93. va_end(vl2);
  94. fputs(line, report_file);
  95. fflush(report_file);
  96. }
  97. double parse_number_or_die(const char *context, const char *numstr, int type,
  98. double min, double max)
  99. {
  100. char *tail;
  101. const char *error;
  102. double d = av_strtod(numstr, &tail);
  103. if (*tail)
  104. error = "Expected number for %s but found: %s\n";
  105. else if (d < min || d > max)
  106. error = "The value for %s was %s which is not within %f - %f\n";
  107. else if (type == OPT_INT64 && (int64_t)d != d)
  108. error = "Expected int64 for %s but found %s\n";
  109. else if (type == OPT_INT && (int)d != d)
  110. error = "Expected int for %s but found %s\n";
  111. else
  112. return d;
  113. av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
  114. exit(1);
  115. return 0;
  116. }
  117. int64_t parse_time_or_die(const char *context, const char *timestr,
  118. int is_duration)
  119. {
  120. int64_t us;
  121. if (av_parse_time(&us, timestr, is_duration) < 0) {
  122. av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
  123. is_duration ? "duration" : "date", context, timestr);
  124. exit(1);
  125. }
  126. return us;
  127. }
  128. void show_help_options(const OptionDef *options, const char *msg, int req_flags,
  129. int rej_flags, int alt_flags)
  130. {
  131. const OptionDef *po;
  132. int first;
  133. first = 1;
  134. for (po = options; po->name != NULL; po++) {
  135. char buf[64];
  136. if (((po->flags & req_flags) != req_flags) ||
  137. (alt_flags && !(po->flags & alt_flags)) ||
  138. (po->flags & rej_flags))
  139. continue;
  140. if (first) {
  141. printf("%s\n", msg);
  142. first = 0;
  143. }
  144. av_strlcpy(buf, po->name, sizeof(buf));
  145. if (po->argname) {
  146. av_strlcat(buf, " ", sizeof(buf));
  147. av_strlcat(buf, po->argname, sizeof(buf));
  148. }
  149. printf("-%-17s %s\n", buf, po->help);
  150. }
  151. printf("\n");
  152. }
  153. void show_help_children(const AVClass *class, int flags)
  154. {
  155. const AVClass *child = NULL;
  156. if (class->option) {
  157. av_opt_show2(&class, NULL, flags, 0);
  158. printf("\n");
  159. }
  160. while (child = av_opt_child_class_next(class, child))
  161. show_help_children(child, flags);
  162. }
  163. static const OptionDef *find_option(const OptionDef *po, const char *name)
  164. {
  165. const char *p = strchr(name, ':');
  166. int len = p ? p - name : strlen(name);
  167. while (po->name != NULL) {
  168. if (!strncmp(name, po->name, len) && strlen(po->name) == len)
  169. break;
  170. po++;
  171. }
  172. return po;
  173. }
  174. #if defined(_WIN32) && !defined(__MINGW32CE__)
  175. #include <windows.h>
  176. #include <shellapi.h>
  177. /* Will be leaked on exit */
  178. static char** win32_argv_utf8 = NULL;
  179. static int win32_argc = 0;
  180. /**
  181. * Prepare command line arguments for executable.
  182. * For Windows - perform wide-char to UTF-8 conversion.
  183. * Input arguments should be main() function arguments.
  184. * @param argc_ptr Arguments number (including executable)
  185. * @param argv_ptr Arguments list.
  186. */
  187. static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
  188. {
  189. char *argstr_flat;
  190. wchar_t **argv_w;
  191. int i, buffsize = 0, offset = 0;
  192. if (win32_argv_utf8) {
  193. *argc_ptr = win32_argc;
  194. *argv_ptr = win32_argv_utf8;
  195. return;
  196. }
  197. win32_argc = 0;
  198. argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
  199. if (win32_argc <= 0 || !argv_w)
  200. return;
  201. /* determine the UTF-8 buffer size (including NULL-termination symbols) */
  202. for (i = 0; i < win32_argc; i++)
  203. buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
  204. NULL, 0, NULL, NULL);
  205. win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
  206. argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
  207. if (win32_argv_utf8 == NULL) {
  208. LocalFree(argv_w);
  209. return;
  210. }
  211. for (i = 0; i < win32_argc; i++) {
  212. win32_argv_utf8[i] = &argstr_flat[offset];
  213. offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
  214. &argstr_flat[offset],
  215. buffsize - offset, NULL, NULL);
  216. }
  217. win32_argv_utf8[i] = NULL;
  218. LocalFree(argv_w);
  219. *argc_ptr = win32_argc;
  220. *argv_ptr = win32_argv_utf8;
  221. }
  222. #else
  223. static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
  224. {
  225. /* nothing to do */
  226. }
  227. #endif /* WIN32 && !__MINGW32CE__ */
  228. int parse_option(void *optctx, const char *opt, const char *arg,
  229. const OptionDef *options)
  230. {
  231. const OptionDef *po;
  232. int bool_val = 1;
  233. int *dstcount;
  234. void *dst;
  235. po = find_option(options, opt);
  236. if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
  237. /* handle 'no' bool option */
  238. po = find_option(options, opt + 2);
  239. if ((po->name && (po->flags & OPT_BOOL)))
  240. bool_val = 0;
  241. }
  242. if (!po->name)
  243. po = find_option(options, "default");
  244. if (!po->name) {
  245. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
  246. return AVERROR(EINVAL);
  247. }
  248. if (po->flags & HAS_ARG && !arg) {
  249. av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
  250. return AVERROR(EINVAL);
  251. }
  252. /* new-style options contain an offset into optctx, old-style address of
  253. * a global var*/
  254. dst = po->flags & (OPT_OFFSET | OPT_SPEC) ? (uint8_t *)optctx + po->u.off
  255. : po->u.dst_ptr;
  256. if (po->flags & OPT_SPEC) {
  257. SpecifierOpt **so = dst;
  258. char *p = strchr(opt, ':');
  259. dstcount = (int *)(so + 1);
  260. *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
  261. (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
  262. dst = &(*so)[*dstcount - 1].u;
  263. }
  264. if (po->flags & OPT_STRING) {
  265. char *str;
  266. str = av_strdup(arg);
  267. // av_freep(dst);
  268. *(char **)dst = str;
  269. } else if (po->flags & OPT_BOOL) {
  270. *(int *)dst = bool_val;
  271. } else if (po->flags & OPT_INT) {
  272. *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
  273. } else if (po->flags & OPT_INT64) {
  274. *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
  275. } else if (po->flags & OPT_TIME) {
  276. *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
  277. } else if (po->flags & OPT_FLOAT) {
  278. *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
  279. } else if (po->flags & OPT_DOUBLE) {
  280. *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
  281. } else if (po->u.func_arg) {
  282. int ret = po->u.func_arg(optctx, opt, arg);
  283. if (ret < 0) {
  284. av_log(NULL, AV_LOG_ERROR,
  285. "Failed to set value '%s' for option '%s'\n", arg, opt);
  286. return ret;
  287. }
  288. }
  289. if (po->flags & OPT_EXIT)
  290. exit(0);
  291. return !!(po->flags & HAS_ARG);
  292. }
  293. void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
  294. void (*parse_arg_function)(void *, const char*))
  295. {
  296. const char *opt;
  297. int optindex, handleoptions = 1, ret;
  298. /* perform system-dependent conversions for arguments list */
  299. prepare_app_arguments(&argc, &argv);
  300. /* parse options */
  301. optindex = 1;
  302. while (optindex < argc) {
  303. opt = argv[optindex++];
  304. if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
  305. if (opt[1] == '-' && opt[2] == '\0') {
  306. handleoptions = 0;
  307. continue;
  308. }
  309. opt++;
  310. if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
  311. exit(1);
  312. optindex += ret;
  313. } else {
  314. if (parse_arg_function)
  315. parse_arg_function(optctx, opt);
  316. }
  317. }
  318. }
  319. int locate_option(int argc, char **argv, const OptionDef *options,
  320. const char *optname)
  321. {
  322. const OptionDef *po;
  323. int i;
  324. for (i = 1; i < argc; i++) {
  325. const char *cur_opt = argv[i];
  326. if (*cur_opt++ != '-')
  327. continue;
  328. po = find_option(options, cur_opt);
  329. if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
  330. po = find_option(options, cur_opt + 2);
  331. if ((!po->name && !strcmp(cur_opt, optname)) ||
  332. (po->name && !strcmp(optname, po->name)))
  333. return i;
  334. if (po->flags & HAS_ARG)
  335. i++;
  336. }
  337. return 0;
  338. }
  339. static void dump_argument(const char *a)
  340. {
  341. const unsigned char *p;
  342. for (p = a; *p; p++)
  343. if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
  344. *p == '_' || (*p >= 'a' && *p <= 'z')))
  345. break;
  346. if (!*p) {
  347. fputs(a, report_file);
  348. return;
  349. }
  350. fputc('"', report_file);
  351. for (p = a; *p; p++) {
  352. if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
  353. fprintf(report_file, "\\%c", *p);
  354. else if (*p < ' ' || *p > '~')
  355. fprintf(report_file, "\\x%02x", *p);
  356. else
  357. fputc(*p, report_file);
  358. }
  359. fputc('"', report_file);
  360. }
  361. void parse_loglevel(int argc, char **argv, const OptionDef *options)
  362. {
  363. int idx = locate_option(argc, argv, options, "loglevel");
  364. if (!idx)
  365. idx = locate_option(argc, argv, options, "v");
  366. if (idx && argv[idx + 1])
  367. opt_loglevel(NULL, "loglevel", argv[idx + 1]);
  368. idx = locate_option(argc, argv, options, "report");
  369. if (idx || getenv("FFREPORT")) {
  370. opt_report("report");
  371. if (report_file) {
  372. int i;
  373. fprintf(report_file, "Command line:\n");
  374. for (i = 0; i < argc; i++) {
  375. dump_argument(argv[i]);
  376. fputc(i < argc - 1 ? ' ' : '\n', report_file);
  377. }
  378. fflush(report_file);
  379. }
  380. }
  381. }
  382. #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
  383. int opt_default(void *optctx, const char *opt, const char *arg)
  384. {
  385. const AVOption *o;
  386. int consumed = 0;
  387. char opt_stripped[128];
  388. const char *p;
  389. const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
  390. const AVClass *sc, *swr_class;
  391. if (!(p = strchr(opt, ':')))
  392. p = opt + strlen(opt);
  393. av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
  394. if ((o = av_opt_find(&cc, opt_stripped, NULL, 0,
  395. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
  396. ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
  397. (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) {
  398. av_dict_set(&codec_opts, opt, arg, FLAGS);
  399. consumed = 1;
  400. }
  401. if ((o = av_opt_find(&fc, opt, NULL, 0,
  402. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  403. av_dict_set(&format_opts, opt, arg, FLAGS);
  404. if(consumed)
  405. av_log(NULL, AV_LOG_VERBOSE, "Routing %s to codec and muxer layer\n", opt);
  406. consumed = 1;
  407. }
  408. #if CONFIG_SWSCALE
  409. sc = sws_get_class();
  410. if (!consumed && av_opt_find(&sc, opt, NULL, 0,
  411. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
  412. // XXX we only support sws_flags, not arbitrary sws options
  413. int ret = av_opt_set(sws_opts, opt, arg, 0);
  414. if (ret < 0) {
  415. av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
  416. return ret;
  417. }
  418. consumed = 1;
  419. }
  420. #endif
  421. #if CONFIG_SWRESAMPLE
  422. swr_class = swr_get_class();
  423. if (!consumed && av_opt_find(&swr_class, opt, NULL, 0,
  424. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
  425. int ret = av_opt_set(swr_opts, opt, arg, 0);
  426. if (ret < 0) {
  427. av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
  428. return ret;
  429. }
  430. consumed = 1;
  431. }
  432. #endif
  433. if (consumed)
  434. return 0;
  435. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
  436. return AVERROR_OPTION_NOT_FOUND;
  437. }
  438. int opt_loglevel(void *optctx, const char *opt, const char *arg)
  439. {
  440. const struct { const char *name; int level; } log_levels[] = {
  441. { "quiet" , AV_LOG_QUIET },
  442. { "panic" , AV_LOG_PANIC },
  443. { "fatal" , AV_LOG_FATAL },
  444. { "error" , AV_LOG_ERROR },
  445. { "warning", AV_LOG_WARNING },
  446. { "info" , AV_LOG_INFO },
  447. { "verbose", AV_LOG_VERBOSE },
  448. { "debug" , AV_LOG_DEBUG },
  449. };
  450. char *tail;
  451. int level;
  452. int i;
  453. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
  454. if (!strcmp(log_levels[i].name, arg)) {
  455. av_log_set_level(log_levels[i].level);
  456. return 0;
  457. }
  458. }
  459. level = strtol(arg, &tail, 10);
  460. if (*tail) {
  461. av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
  462. "Possible levels are numbers or:\n", arg);
  463. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
  464. av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
  465. exit(1);
  466. }
  467. av_log_set_level(level);
  468. return 0;
  469. }
  470. int opt_report_file(void *optctx, const char *opt, const char *arg)
  471. {
  472. report_file = fopen(arg, "w");
  473. if (!report_file) {
  474. av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
  475. arg, strerror(errno));
  476. return AVERROR(errno);
  477. }
  478. return 0;
  479. }
  480. int opt_report(const char *opt)
  481. {
  482. char filename[64];
  483. time_t now;
  484. struct tm *tm;
  485. if (report_file) /* already opened */
  486. return 0;
  487. time(&now);
  488. tm = localtime(&now);
  489. snprintf(filename, sizeof(filename), "%s-%04d%02d%02d-%02d%02d%02d.log",
  490. program_name,
  491. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  492. tm->tm_hour, tm->tm_min, tm->tm_sec);
  493. report_file = fopen(filename, "w");
  494. if (!report_file) {
  495. av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
  496. filename, strerror(errno));
  497. return AVERROR(errno);
  498. }
  499. av_log_set_callback(log_callback_report);
  500. av_log(NULL, AV_LOG_INFO,
  501. "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
  502. "Report written to \"%s\"\n",
  503. program_name,
  504. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  505. tm->tm_hour, tm->tm_min, tm->tm_sec,
  506. filename);
  507. av_log_set_level(FFMAX(av_log_get_level(), AV_LOG_VERBOSE));
  508. return 0;
  509. }
  510. int opt_max_alloc(void *optctx, const char *opt, const char *arg)
  511. {
  512. char *tail;
  513. size_t max;
  514. max = strtol(arg, &tail, 10);
  515. if (*tail) {
  516. av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
  517. exit(1);
  518. }
  519. av_max_alloc(max);
  520. return 0;
  521. }
  522. int opt_cpuflags(void *optctx, const char *opt, const char *arg)
  523. {
  524. int ret;
  525. unsigned flags = av_get_cpu_flags();
  526. if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
  527. return ret;
  528. av_force_cpu_flags(flags);
  529. return 0;
  530. }
  531. int opt_codec_debug(void *optctx, const char *opt, const char *arg)
  532. {
  533. av_log_set_level(AV_LOG_DEBUG);
  534. return opt_default(NULL, opt, arg);
  535. }
  536. int opt_timelimit(void *optctx, const char *opt, const char *arg)
  537. {
  538. #if HAVE_SETRLIMIT
  539. int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
  540. struct rlimit rl = { lim, lim + 1 };
  541. if (setrlimit(RLIMIT_CPU, &rl))
  542. perror("setrlimit");
  543. #else
  544. av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
  545. #endif
  546. return 0;
  547. }
  548. void print_error(const char *filename, int err)
  549. {
  550. char errbuf[128];
  551. const char *errbuf_ptr = errbuf;
  552. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  553. errbuf_ptr = strerror(AVUNERROR(err));
  554. av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
  555. }
  556. static int warned_cfg = 0;
  557. #define INDENT 1
  558. #define SHOW_VERSION 2
  559. #define SHOW_CONFIG 4
  560. #define SHOW_COPYRIGHT 8
  561. #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
  562. if (CONFIG_##LIBNAME) { \
  563. const char *indent = flags & INDENT? " " : ""; \
  564. if (flags & SHOW_VERSION) { \
  565. unsigned int version = libname##_version(); \
  566. av_log(NULL, level, \
  567. "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
  568. indent, #libname, \
  569. LIB##LIBNAME##_VERSION_MAJOR, \
  570. LIB##LIBNAME##_VERSION_MINOR, \
  571. LIB##LIBNAME##_VERSION_MICRO, \
  572. version >> 16, version >> 8 & 0xff, version & 0xff); \
  573. } \
  574. if (flags & SHOW_CONFIG) { \
  575. const char *cfg = libname##_configuration(); \
  576. if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
  577. if (!warned_cfg) { \
  578. av_log(NULL, level, \
  579. "%sWARNING: library configuration mismatch\n", \
  580. indent); \
  581. warned_cfg = 1; \
  582. } \
  583. av_log(NULL, level, "%s%-11s configuration: %s\n", \
  584. indent, #libname, cfg); \
  585. } \
  586. } \
  587. } \
  588. static void print_all_libs_info(int flags, int level)
  589. {
  590. PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
  591. PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
  592. PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
  593. PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
  594. PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
  595. // PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
  596. PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
  597. PRINT_LIB_INFO(swresample,SWRESAMPLE, flags, level);
  598. #if CONFIG_POSTPROC
  599. PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
  600. #endif
  601. }
  602. static void print_program_info(int flags, int level)
  603. {
  604. const char *indent = flags & INDENT? " " : "";
  605. av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
  606. if (flags & SHOW_COPYRIGHT)
  607. av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
  608. program_birth_year, this_year);
  609. av_log(NULL, level, "\n");
  610. av_log(NULL, level, "%sbuilt on %s %s with %s\n",
  611. indent, __DATE__, __TIME__, CC_IDENT);
  612. av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
  613. }
  614. void show_banner(int argc, char **argv, const OptionDef *options)
  615. {
  616. int idx = locate_option(argc, argv, options, "version");
  617. if (idx)
  618. return;
  619. print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
  620. print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
  621. print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
  622. }
  623. int show_version(void *optctx, const char *opt, const char *arg)
  624. {
  625. av_log_set_callback(log_callback_help);
  626. print_program_info (0 , AV_LOG_INFO);
  627. print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
  628. return 0;
  629. }
  630. int show_license(void *optctx, const char *opt, const char *arg)
  631. {
  632. #if CONFIG_NONFREE
  633. printf(
  634. "This version of %s has nonfree parts compiled in.\n"
  635. "Therefore it is not legally redistributable.\n",
  636. program_name );
  637. #elif CONFIG_GPLV3
  638. printf(
  639. "%s is free software; you can redistribute it and/or modify\n"
  640. "it under the terms of the GNU General Public License as published by\n"
  641. "the Free Software Foundation; either version 3 of the License, or\n"
  642. "(at your option) any later version.\n"
  643. "\n"
  644. "%s is distributed in the hope that it will be useful,\n"
  645. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  646. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  647. "GNU General Public License for more details.\n"
  648. "\n"
  649. "You should have received a copy of the GNU General Public License\n"
  650. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  651. program_name, program_name, program_name );
  652. #elif CONFIG_GPL
  653. printf(
  654. "%s is free software; you can redistribute it and/or modify\n"
  655. "it under the terms of the GNU General Public License as published by\n"
  656. "the Free Software Foundation; either version 2 of the License, or\n"
  657. "(at your option) any later version.\n"
  658. "\n"
  659. "%s is distributed in the hope that it will be useful,\n"
  660. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  661. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  662. "GNU General Public License for more details.\n"
  663. "\n"
  664. "You should have received a copy of the GNU General Public License\n"
  665. "along with %s; if not, write to the Free Software\n"
  666. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  667. program_name, program_name, program_name );
  668. #elif CONFIG_LGPLV3
  669. printf(
  670. "%s is free software; you can redistribute it and/or modify\n"
  671. "it under the terms of the GNU Lesser General Public License as published by\n"
  672. "the Free Software Foundation; either version 3 of the License, or\n"
  673. "(at your option) any later version.\n"
  674. "\n"
  675. "%s is distributed in the hope that it will be useful,\n"
  676. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  677. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  678. "GNU Lesser General Public License for more details.\n"
  679. "\n"
  680. "You should have received a copy of the GNU Lesser General Public License\n"
  681. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  682. program_name, program_name, program_name );
  683. #else
  684. printf(
  685. "%s is free software; you can redistribute it and/or\n"
  686. "modify it under the terms of the GNU Lesser General Public\n"
  687. "License as published by the Free Software Foundation; either\n"
  688. "version 2.1 of the License, or (at your option) any later version.\n"
  689. "\n"
  690. "%s is distributed in the hope that it will be useful,\n"
  691. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  692. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  693. "Lesser General Public License for more details.\n"
  694. "\n"
  695. "You should have received a copy of the GNU Lesser General Public\n"
  696. "License along with %s; if not, write to the Free Software\n"
  697. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  698. program_name, program_name, program_name );
  699. #endif
  700. return 0;
  701. }
  702. int show_formats(void *optctx, const char *opt, const char *arg)
  703. {
  704. AVInputFormat *ifmt = NULL;
  705. AVOutputFormat *ofmt = NULL;
  706. const char *last_name;
  707. printf("File formats:\n"
  708. " D. = Demuxing supported\n"
  709. " .E = Muxing supported\n"
  710. " --\n");
  711. last_name = "000";
  712. for (;;) {
  713. int decode = 0;
  714. int encode = 0;
  715. const char *name = NULL;
  716. const char *long_name = NULL;
  717. while ((ofmt = av_oformat_next(ofmt))) {
  718. if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
  719. strcmp(ofmt->name, last_name) > 0) {
  720. name = ofmt->name;
  721. long_name = ofmt->long_name;
  722. encode = 1;
  723. }
  724. }
  725. while ((ifmt = av_iformat_next(ifmt))) {
  726. if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
  727. strcmp(ifmt->name, last_name) > 0) {
  728. name = ifmt->name;
  729. long_name = ifmt->long_name;
  730. encode = 0;
  731. }
  732. if (name && strcmp(ifmt->name, name) == 0)
  733. decode = 1;
  734. }
  735. if (name == NULL)
  736. break;
  737. last_name = name;
  738. printf(" %s%s %-15s %s\n",
  739. decode ? "D" : " ",
  740. encode ? "E" : " ",
  741. name,
  742. long_name ? long_name:" ");
  743. }
  744. return 0;
  745. }
  746. #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
  747. if (codec->field) { \
  748. const type *p = c->field; \
  749. \
  750. printf(" Supported " list_name ":"); \
  751. while (*p != term) { \
  752. get_name(*p); \
  753. printf(" %s", name); \
  754. p++; \
  755. } \
  756. printf("\n"); \
  757. } \
  758. static void print_codec(const AVCodec *c)
  759. {
  760. int encoder = av_codec_is_encoder(c);
  761. printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
  762. c->long_name ? c->long_name : "");
  763. if (c->type == AVMEDIA_TYPE_VIDEO) {
  764. printf(" Threading capabilities: ");
  765. switch (c->capabilities & (CODEC_CAP_FRAME_THREADS |
  766. CODEC_CAP_SLICE_THREADS)) {
  767. case CODEC_CAP_FRAME_THREADS |
  768. CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
  769. case CODEC_CAP_FRAME_THREADS: printf("frame"); break;
  770. case CODEC_CAP_SLICE_THREADS: printf("slice"); break;
  771. default: printf("no"); break;
  772. }
  773. printf("\n");
  774. }
  775. if (c->supported_framerates) {
  776. const AVRational *fps = c->supported_framerates;
  777. printf(" Supported framerates:");
  778. while (fps->num) {
  779. printf(" %d/%d", fps->num, fps->den);
  780. fps++;
  781. }
  782. printf("\n");
  783. }
  784. PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
  785. AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
  786. PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
  787. GET_SAMPLE_RATE_NAME);
  788. PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
  789. AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
  790. PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
  791. 0, GET_CH_LAYOUT_DESC);
  792. if (c->priv_class) {
  793. show_help_children(c->priv_class,
  794. AV_OPT_FLAG_ENCODING_PARAM |
  795. AV_OPT_FLAG_DECODING_PARAM);
  796. }
  797. }
  798. static char get_media_type_char(enum AVMediaType type)
  799. {
  800. switch (type) {
  801. case AVMEDIA_TYPE_VIDEO: return 'V';
  802. case AVMEDIA_TYPE_AUDIO: return 'A';
  803. case AVMEDIA_TYPE_DATA: return 'D';
  804. case AVMEDIA_TYPE_SUBTITLE: return 'S';
  805. case AVMEDIA_TYPE_ATTACHMENT:return 'T';
  806. default: return '?';
  807. }
  808. }
  809. static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
  810. int encoder)
  811. {
  812. while ((prev = av_codec_next(prev))) {
  813. if (prev->id == id &&
  814. (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
  815. return prev;
  816. }
  817. return NULL;
  818. }
  819. static int compare_codec_desc(const void *a, const void *b)
  820. {
  821. const AVCodecDescriptor * const *da = a;
  822. const AVCodecDescriptor * const *db = b;
  823. return (*da)->type != (*db)->type ? (*da)->type - (*db)->type :
  824. strcmp((*da)->name, (*db)->name);
  825. }
  826. static unsigned get_codecs_sorted(const AVCodecDescriptor ***rcodecs)
  827. {
  828. const AVCodecDescriptor *desc = NULL;
  829. const AVCodecDescriptor **codecs;
  830. unsigned nb_codecs = 0, i = 0;
  831. while ((desc = avcodec_descriptor_next(desc)))
  832. nb_codecs++;
  833. if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs)))) {
  834. av_log(0, AV_LOG_ERROR, "Out of memory\n");
  835. exit(1);
  836. }
  837. desc = NULL;
  838. while ((desc = avcodec_descriptor_next(desc)))
  839. codecs[i++] = desc;
  840. av_assert0(i == nb_codecs);
  841. qsort(codecs, nb_codecs, sizeof(*codecs), compare_codec_desc);
  842. *rcodecs = codecs;
  843. return nb_codecs;
  844. }
  845. static void print_codecs_for_id(enum AVCodecID id, int encoder)
  846. {
  847. const AVCodec *codec = NULL;
  848. printf(" (%s: ", encoder ? "encoders" : "decoders");
  849. while ((codec = next_codec_for_id(id, codec, encoder)))
  850. printf("%s ", codec->name);
  851. printf(")");
  852. }
  853. int show_codecs(void *optctx, const char *opt, const char *arg)
  854. {
  855. const AVCodecDescriptor **codecs;
  856. unsigned i, nb_codecs = get_codecs_sorted(&codecs);
  857. printf("Codecs:\n"
  858. " D..... = Decoding supported\n"
  859. " .E.... = Encoding supported\n"
  860. " ..V... = Video codec\n"
  861. " ..A... = Audio codec\n"
  862. " ..S... = Subtitle codec\n"
  863. " ...I.. = Intra frame-only codec\n"
  864. " ....L. = Lossy compression\n"
  865. " .....S = Lossless compression\n"
  866. " -------\n");
  867. for (i = 0; i < nb_codecs; i++) {
  868. const AVCodecDescriptor *desc = codecs[i];
  869. const AVCodec *codec = NULL;
  870. printf(" ");
  871. printf(avcodec_find_decoder(desc->id) ? "D" : ".");
  872. printf(avcodec_find_encoder(desc->id) ? "E" : ".");
  873. printf("%c", get_media_type_char(desc->type));
  874. printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
  875. printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
  876. printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
  877. printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
  878. /* print decoders/encoders when there's more than one or their
  879. * names are different from codec name */
  880. while ((codec = next_codec_for_id(desc->id, codec, 0))) {
  881. if (strcmp(codec->name, desc->name)) {
  882. print_codecs_for_id(desc->id, 0);
  883. break;
  884. }
  885. }
  886. codec = NULL;
  887. while ((codec = next_codec_for_id(desc->id, codec, 1))) {
  888. if (strcmp(codec->name, desc->name)) {
  889. print_codecs_for_id(desc->id, 1);
  890. break;
  891. }
  892. }
  893. printf("\n");
  894. }
  895. av_free(codecs);
  896. return 0;
  897. }
  898. static void print_codecs(int encoder)
  899. {
  900. const AVCodecDescriptor **codecs;
  901. unsigned i, nb_codecs = get_codecs_sorted(&codecs);
  902. printf("%s:\n"
  903. " V..... = Video\n"
  904. " A..... = Audio\n"
  905. " S..... = Subtitle\n"
  906. " .F.... = Frame-level multithreading\n"
  907. " ..S... = Slice-level multithreading\n"
  908. " ...X.. = Codec is experimental\n"
  909. " ....B. = Supports draw_horiz_band\n"
  910. " .....D = Supports direct rendering method 1\n"
  911. " ------\n",
  912. encoder ? "Encoders" : "Decoders");
  913. for (i = 0; i < nb_codecs; i++) {
  914. const AVCodecDescriptor *desc = codecs[i];
  915. const AVCodec *codec = NULL;
  916. while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
  917. printf(" %c", get_media_type_char(desc->type));
  918. printf((codec->capabilities & CODEC_CAP_FRAME_THREADS) ? "F" : ".");
  919. printf((codec->capabilities & CODEC_CAP_SLICE_THREADS) ? "S" : ".");
  920. printf((codec->capabilities & CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
  921. printf((codec->capabilities & CODEC_CAP_DRAW_HORIZ_BAND)?"B" : ".");
  922. printf((codec->capabilities & CODEC_CAP_DR1) ? "D" : ".");
  923. printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
  924. if (strcmp(codec->name, desc->name))
  925. printf(" (codec %s)", desc->name);
  926. printf("\n");
  927. }
  928. }
  929. av_free(codecs);
  930. }
  931. int show_decoders(void *optctx, const char *opt, const char *arg)
  932. {
  933. print_codecs(0);
  934. return 0;
  935. }
  936. int show_encoders(void *optctx, const char *opt, const char *arg)
  937. {
  938. print_codecs(1);
  939. return 0;
  940. }
  941. int show_bsfs(void *optctx, const char *opt, const char *arg)
  942. {
  943. AVBitStreamFilter *bsf = NULL;
  944. printf("Bitstream filters:\n");
  945. while ((bsf = av_bitstream_filter_next(bsf)))
  946. printf("%s\n", bsf->name);
  947. printf("\n");
  948. return 0;
  949. }
  950. int show_protocols(void *optctx, const char *opt, const char *arg)
  951. {
  952. void *opaque = NULL;
  953. const char *name;
  954. printf("Supported file protocols:\n"
  955. "Input:\n");
  956. while ((name = avio_enum_protocols(&opaque, 0)))
  957. printf("%s\n", name);
  958. printf("Output:\n");
  959. while ((name = avio_enum_protocols(&opaque, 1)))
  960. printf("%s\n", name);
  961. return 0;
  962. }
  963. int show_filters(void *optctx, const char *opt, const char *arg)
  964. {
  965. AVFilter av_unused(**filter) = NULL;
  966. char descr[64], *descr_cur;
  967. int i, j;
  968. const AVFilterPad *pad;
  969. printf("Filters:\n");
  970. #if CONFIG_AVFILTER
  971. while ((filter = av_filter_next(filter)) && *filter) {
  972. descr_cur = descr;
  973. for (i = 0; i < 2; i++) {
  974. if (i) {
  975. *(descr_cur++) = '-';
  976. *(descr_cur++) = '>';
  977. }
  978. pad = i ? (*filter)->outputs : (*filter)->inputs;
  979. for (j = 0; pad && pad[j].name; j++) {
  980. if (descr_cur >= descr + sizeof(descr) - 4)
  981. break;
  982. *(descr_cur++) = get_media_type_char(pad[j].type);
  983. }
  984. if (!j)
  985. *(descr_cur++) = '|';
  986. }
  987. *descr_cur = 0;
  988. printf("%-16s %-10s %s\n", (*filter)->name, descr, (*filter)->description);
  989. }
  990. #endif
  991. return 0;
  992. }
  993. int show_pix_fmts(void *optctx, const char *opt, const char *arg)
  994. {
  995. const AVPixFmtDescriptor *pix_desc = NULL;
  996. printf("Pixel formats:\n"
  997. "I.... = Supported Input format for conversion\n"
  998. ".O... = Supported Output format for conversion\n"
  999. "..H.. = Hardware accelerated format\n"
  1000. "...P. = Paletted format\n"
  1001. "....B = Bitstream format\n"
  1002. "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
  1003. "-----\n");
  1004. #if !CONFIG_SWSCALE
  1005. # define sws_isSupportedInput(x) 0
  1006. # define sws_isSupportedOutput(x) 0
  1007. #endif
  1008. while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
  1009. enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
  1010. if(!pix_desc->name)
  1011. continue;
  1012. printf("%c%c%c%c%c %-16s %d %2d\n",
  1013. sws_isSupportedInput (pix_fmt) ? 'I' : '.',
  1014. sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
  1015. pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
  1016. pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
  1017. pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
  1018. pix_desc->name,
  1019. pix_desc->nb_components,
  1020. av_get_bits_per_pixel(pix_desc));
  1021. }
  1022. return 0;
  1023. }
  1024. int show_layouts(void *optctx, const char *opt, const char *arg)
  1025. {
  1026. int i = 0;
  1027. uint64_t layout, j;
  1028. const char *name, *descr;
  1029. printf("Individual channels:\n"
  1030. "NAME DESCRIPTION\n");
  1031. for (i = 0; i < 63; i++) {
  1032. name = av_get_channel_name((uint64_t)1 << i);
  1033. if (!name)
  1034. continue;
  1035. descr = av_get_channel_description((uint64_t)1 << i);
  1036. printf("%-12s%s\n", name, descr);
  1037. }
  1038. printf("\nStandard channel layouts:\n"
  1039. "NAME DECOMPOSITION\n");
  1040. for (i = 0; !av_get_standard_channel_layout(i, &layout, &name); i++) {
  1041. if (name) {
  1042. printf("%-12s", name);
  1043. for (j = 1; j; j <<= 1)
  1044. if ((layout & j))
  1045. printf("%s%s", (layout & (j - 1)) ? "+" : "", av_get_channel_name(j));
  1046. printf("\n");
  1047. }
  1048. }
  1049. return 0;
  1050. }
  1051. int show_sample_fmts(void *optctx, const char *opt, const char *arg)
  1052. {
  1053. int i;
  1054. char fmt_str[128];
  1055. for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
  1056. printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
  1057. return 0;
  1058. }
  1059. static void show_help_codec(const char *name, int encoder)
  1060. {
  1061. const AVCodecDescriptor *desc;
  1062. const AVCodec *codec;
  1063. if (!name) {
  1064. av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
  1065. return;
  1066. }
  1067. codec = encoder ? avcodec_find_encoder_by_name(name) :
  1068. avcodec_find_decoder_by_name(name);
  1069. if (codec)
  1070. print_codec(codec);
  1071. else if ((desc = avcodec_descriptor_get_by_name(name))) {
  1072. int printed = 0;
  1073. while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
  1074. printed = 1;
  1075. print_codec(codec);
  1076. }
  1077. if (!printed) {
  1078. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, "
  1079. "but no %s for it are available. FFmpeg might need to be "
  1080. "recompiled with additional external libraries.\n",
  1081. name, encoder ? "encoders" : "decoders");
  1082. }
  1083. } else {
  1084. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n",
  1085. name);
  1086. }
  1087. }
  1088. static void show_help_demuxer(const char *name)
  1089. {
  1090. const AVInputFormat *fmt = av_find_input_format(name);
  1091. if (!fmt) {
  1092. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1093. return;
  1094. }
  1095. printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
  1096. if (fmt->extensions)
  1097. printf(" Common extensions: %s.\n", fmt->extensions);
  1098. if (fmt->priv_class)
  1099. show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
  1100. }
  1101. static void show_help_muxer(const char *name)
  1102. {
  1103. const AVCodecDescriptor *desc;
  1104. const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
  1105. if (!fmt) {
  1106. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1107. return;
  1108. }
  1109. printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
  1110. if (fmt->extensions)
  1111. printf(" Common extensions: %s.\n", fmt->extensions);
  1112. if (fmt->mime_type)
  1113. printf(" Mime type: %s.\n", fmt->mime_type);
  1114. if (fmt->video_codec != AV_CODEC_ID_NONE &&
  1115. (desc = avcodec_descriptor_get(fmt->video_codec))) {
  1116. printf(" Default video codec: %s.\n", desc->name);
  1117. }
  1118. if (fmt->audio_codec != AV_CODEC_ID_NONE &&
  1119. (desc = avcodec_descriptor_get(fmt->audio_codec))) {
  1120. printf(" Default audio codec: %s.\n", desc->name);
  1121. }
  1122. if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
  1123. (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
  1124. printf(" Default subtitle codec: %s.\n", desc->name);
  1125. }
  1126. if (fmt->priv_class)
  1127. show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
  1128. }
  1129. int show_help(void *optctx, const char *opt, const char *arg)
  1130. {
  1131. char *topic, *par;
  1132. av_log_set_callback(log_callback_help);
  1133. topic = av_strdup(arg ? arg : "");
  1134. par = strchr(topic, '=');
  1135. if (par)
  1136. *par++ = 0;
  1137. if (!*topic) {
  1138. show_help_default(topic, par);
  1139. } else if (!strcmp(topic, "decoder")) {
  1140. show_help_codec(par, 0);
  1141. } else if (!strcmp(topic, "encoder")) {
  1142. show_help_codec(par, 1);
  1143. } else if (!strcmp(topic, "demuxer")) {
  1144. show_help_demuxer(par);
  1145. } else if (!strcmp(topic, "muxer")) {
  1146. show_help_muxer(par);
  1147. } else {
  1148. show_help_default(topic, par);
  1149. }
  1150. av_freep(&topic);
  1151. return 0;
  1152. }
  1153. int read_yesno(void)
  1154. {
  1155. int c = getchar();
  1156. int yesno = (toupper(c) == 'Y');
  1157. while (c != '\n' && c != EOF)
  1158. c = getchar();
  1159. return yesno;
  1160. }
  1161. int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
  1162. {
  1163. int ret;
  1164. FILE *f = fopen(filename, "rb");
  1165. if (!f) {
  1166. av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
  1167. strerror(errno));
  1168. return AVERROR(errno);
  1169. }
  1170. fseek(f, 0, SEEK_END);
  1171. *size = ftell(f);
  1172. fseek(f, 0, SEEK_SET);
  1173. if (*size == (size_t)-1) {
  1174. av_log(NULL, AV_LOG_ERROR, "IO error: %s\n", strerror(errno));
  1175. fclose(f);
  1176. return AVERROR(errno);
  1177. }
  1178. *bufptr = av_malloc(*size + 1);
  1179. if (!*bufptr) {
  1180. av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
  1181. fclose(f);
  1182. return AVERROR(ENOMEM);
  1183. }
  1184. ret = fread(*bufptr, 1, *size, f);
  1185. if (ret < *size) {
  1186. av_free(*bufptr);
  1187. if (ferror(f)) {
  1188. av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
  1189. filename, strerror(errno));
  1190. ret = AVERROR(errno);
  1191. } else
  1192. ret = AVERROR_EOF;
  1193. } else {
  1194. ret = 0;
  1195. (*bufptr)[(*size)++] = '\0';
  1196. }
  1197. fclose(f);
  1198. return ret;
  1199. }
  1200. FILE *get_preset_file(char *filename, size_t filename_size,
  1201. const char *preset_name, int is_path,
  1202. const char *codec_name)
  1203. {
  1204. FILE *f = NULL;
  1205. int i;
  1206. const char *base[3] = { getenv("FFMPEG_DATADIR"),
  1207. getenv("HOME"),
  1208. FFMPEG_DATADIR, };
  1209. if (is_path) {
  1210. av_strlcpy(filename, preset_name, filename_size);
  1211. f = fopen(filename, "r");
  1212. } else {
  1213. #ifdef _WIN32
  1214. char datadir[MAX_PATH], *ls;
  1215. base[2] = NULL;
  1216. if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
  1217. {
  1218. for (ls = datadir; ls < datadir + strlen(datadir); ls++)
  1219. if (*ls == '\\') *ls = '/';
  1220. if (ls = strrchr(datadir, '/'))
  1221. {
  1222. *ls = 0;
  1223. strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
  1224. base[2] = datadir;
  1225. }
  1226. }
  1227. #endif
  1228. for (i = 0; i < 3 && !f; i++) {
  1229. if (!base[i])
  1230. continue;
  1231. snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
  1232. i != 1 ? "" : "/.ffmpeg", preset_name);
  1233. f = fopen(filename, "r");
  1234. if (!f && codec_name) {
  1235. snprintf(filename, filename_size,
  1236. "%s%s/%s-%s.ffpreset",
  1237. base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
  1238. preset_name);
  1239. f = fopen(filename, "r");
  1240. }
  1241. }
  1242. }
  1243. return f;
  1244. }
  1245. int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
  1246. {
  1247. int ret = avformat_match_stream_specifier(s, st, spec);
  1248. if (ret < 0)
  1249. av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
  1250. return ret;
  1251. }
  1252. AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
  1253. AVFormatContext *s, AVStream *st, AVCodec *codec)
  1254. {
  1255. AVDictionary *ret = NULL;
  1256. AVDictionaryEntry *t = NULL;
  1257. int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
  1258. : AV_OPT_FLAG_DECODING_PARAM;
  1259. char prefix = 0;
  1260. const AVClass *cc = avcodec_get_class();
  1261. if (!codec)
  1262. codec = s->oformat ? avcodec_find_encoder(codec_id)
  1263. : avcodec_find_decoder(codec_id);
  1264. if (!codec)
  1265. return NULL;
  1266. switch (codec->type) {
  1267. case AVMEDIA_TYPE_VIDEO:
  1268. prefix = 'v';
  1269. flags |= AV_OPT_FLAG_VIDEO_PARAM;
  1270. break;
  1271. case AVMEDIA_TYPE_AUDIO:
  1272. prefix = 'a';
  1273. flags |= AV_OPT_FLAG_AUDIO_PARAM;
  1274. break;
  1275. case AVMEDIA_TYPE_SUBTITLE:
  1276. prefix = 's';
  1277. flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
  1278. break;
  1279. }
  1280. while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
  1281. char *p = strchr(t->key, ':');
  1282. /* check stream specification in opt name */
  1283. if (p)
  1284. switch (check_stream_specifier(s, st, p + 1)) {
  1285. case 1: *p = 0; break;
  1286. case 0: continue;
  1287. default: return NULL;
  1288. }
  1289. if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
  1290. (codec && codec->priv_class &&
  1291. av_opt_find(&codec->priv_class, t->key, NULL, flags,
  1292. AV_OPT_SEARCH_FAKE_OBJ)))
  1293. av_dict_set(&ret, t->key, t->value, 0);
  1294. else if (t->key[0] == prefix &&
  1295. av_opt_find(&cc, t->key + 1, NULL, flags,
  1296. AV_OPT_SEARCH_FAKE_OBJ))
  1297. av_dict_set(&ret, t->key + 1, t->value, 0);
  1298. if (p)
  1299. *p = ':';
  1300. }
  1301. return ret;
  1302. }
  1303. AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
  1304. AVDictionary *codec_opts)
  1305. {
  1306. int i;
  1307. AVDictionary **opts;
  1308. if (!s->nb_streams)
  1309. return NULL;
  1310. opts = av_mallocz(s->nb_streams * sizeof(*opts));
  1311. if (!opts) {
  1312. av_log(NULL, AV_LOG_ERROR,
  1313. "Could not alloc memory for stream options.\n");
  1314. return NULL;
  1315. }
  1316. for (i = 0; i < s->nb_streams; i++)
  1317. opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
  1318. s, s->streams[i], NULL);
  1319. return opts;
  1320. }
  1321. void *grow_array(void *array, int elem_size, int *size, int new_size)
  1322. {
  1323. if (new_size >= INT_MAX / elem_size) {
  1324. av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
  1325. exit(1);
  1326. }
  1327. if (*size < new_size) {
  1328. uint8_t *tmp = av_realloc(array, new_size*elem_size);
  1329. if (!tmp) {
  1330. av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
  1331. exit(1);
  1332. }
  1333. memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
  1334. *size = new_size;
  1335. return tmp;
  1336. }
  1337. return array;
  1338. }
  1339. static int alloc_buffer(FrameBuffer **pool, AVCodecContext *s, FrameBuffer **pbuf)
  1340. {
  1341. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  1342. FrameBuffer *buf;
  1343. int i, ret;
  1344. int pixel_size;
  1345. int h_chroma_shift, v_chroma_shift;
  1346. int edge = 32; // XXX should be avcodec_get_edge_width(), but that fails on svq1
  1347. int w = s->width, h = s->height;
  1348. if (!desc)
  1349. return AVERROR(EINVAL);
  1350. pixel_size = desc->comp[0].step_minus1 + 1;
  1351. buf = av_mallocz(sizeof(*buf));
  1352. if (!buf)
  1353. return AVERROR(ENOMEM);
  1354. avcodec_align_dimensions(s, &w, &h);
  1355. if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
  1356. w += 2*edge;
  1357. h += 2*edge;
  1358. }
  1359. if ((ret = av_image_alloc(buf->base, buf->linesize, w, h,
  1360. s->pix_fmt, 32)) < 0) {
  1361. av_freep(&buf);
  1362. av_log(s, AV_LOG_ERROR, "alloc_buffer: av_image_alloc() failed\n");
  1363. return ret;
  1364. }
  1365. /* XXX this shouldn't be needed, but some tests break without this line
  1366. * those decoders are buggy and need to be fixed.
  1367. * the following tests fail:
  1368. * cdgraphics, ansi, aasc, fraps-v1, qtrle-1bit
  1369. */
  1370. memset(buf->base[0], 128, ret);
  1371. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1372. for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
  1373. const int h_shift = i==0 ? 0 : h_chroma_shift;
  1374. const int v_shift = i==0 ? 0 : v_chroma_shift;
  1375. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !buf->linesize[i] || !buf->base[i])
  1376. buf->data[i] = buf->base[i];
  1377. else
  1378. buf->data[i] = buf->base[i] +
  1379. FFALIGN((buf->linesize[i]*edge >> v_shift) +
  1380. (pixel_size*edge >> h_shift), 32);
  1381. }
  1382. buf->w = s->width;
  1383. buf->h = s->height;
  1384. buf->pix_fmt = s->pix_fmt;
  1385. buf->pool = pool;
  1386. *pbuf = buf;
  1387. return 0;
  1388. }
  1389. int codec_get_buffer(AVCodecContext *s, AVFrame *frame)
  1390. {
  1391. FrameBuffer **pool = s->opaque;
  1392. FrameBuffer *buf;
  1393. int ret, i;
  1394. if(av_image_check_size(s->width, s->height, 0, s) || s->pix_fmt<0) {
  1395. av_log(s, AV_LOG_ERROR, "codec_get_buffer: image parameters invalid\n");
  1396. return -1;
  1397. }
  1398. if (!*pool && (ret = alloc_buffer(pool, s, pool)) < 0)
  1399. return ret;
  1400. buf = *pool;
  1401. *pool = buf->next;
  1402. buf->next = NULL;
  1403. if (buf->w != s->width || buf->h != s->height || buf->pix_fmt != s->pix_fmt) {
  1404. av_freep(&buf->base[0]);
  1405. av_free(buf);
  1406. if ((ret = alloc_buffer(pool, s, &buf)) < 0)
  1407. return ret;
  1408. }
  1409. av_assert0(!buf->refcount);
  1410. buf->refcount++;
  1411. frame->opaque = buf;
  1412. frame->type = FF_BUFFER_TYPE_USER;
  1413. frame->extended_data = frame->data;
  1414. frame->pkt_pts = s->pkt ? s->pkt->pts : AV_NOPTS_VALUE;
  1415. frame->width = buf->w;
  1416. frame->height = buf->h;
  1417. frame->format = buf->pix_fmt;
  1418. frame->sample_aspect_ratio = s->sample_aspect_ratio;
  1419. for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
  1420. frame->base[i] = buf->base[i]; // XXX h264.c uses base though it shouldn't
  1421. frame->data[i] = buf->data[i];
  1422. frame->linesize[i] = buf->linesize[i];
  1423. }
  1424. return 0;
  1425. }
  1426. static void unref_buffer(FrameBuffer *buf)
  1427. {
  1428. FrameBuffer **pool = buf->pool;
  1429. av_assert0(buf->refcount > 0);
  1430. buf->refcount--;
  1431. if (!buf->refcount) {
  1432. FrameBuffer *tmp;
  1433. for(tmp= *pool; tmp; tmp= tmp->next)
  1434. av_assert1(tmp != buf);
  1435. buf->next = *pool;
  1436. *pool = buf;
  1437. }
  1438. }
  1439. void codec_release_buffer(AVCodecContext *s, AVFrame *frame)
  1440. {
  1441. FrameBuffer *buf = frame->opaque;
  1442. int i;
  1443. if(frame->type!=FF_BUFFER_TYPE_USER) {
  1444. avcodec_default_release_buffer(s, frame);
  1445. return;
  1446. }
  1447. for (i = 0; i < FF_ARRAY_ELEMS(frame->data); i++)
  1448. frame->data[i] = NULL;
  1449. unref_buffer(buf);
  1450. }
  1451. void filter_release_buffer(AVFilterBuffer *fb)
  1452. {
  1453. FrameBuffer *buf = fb->priv;
  1454. av_free(fb);
  1455. unref_buffer(buf);
  1456. }
  1457. void free_buffer_pool(FrameBuffer **pool)
  1458. {
  1459. FrameBuffer *buf = *pool;
  1460. while (buf) {
  1461. *pool = buf->next;
  1462. av_freep(&buf->base[0]);
  1463. av_free(buf);
  1464. buf = *pool;
  1465. }
  1466. }