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.

1655 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(const char *opt)
  471. {
  472. char filename[64];
  473. time_t now;
  474. struct tm *tm;
  475. if (report_file) /* already opened */
  476. return 0;
  477. time(&now);
  478. tm = localtime(&now);
  479. snprintf(filename, sizeof(filename), "%s-%04d%02d%02d-%02d%02d%02d.log",
  480. program_name,
  481. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  482. tm->tm_hour, tm->tm_min, tm->tm_sec);
  483. report_file = fopen(filename, "w");
  484. if (!report_file) {
  485. av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
  486. filename, strerror(errno));
  487. return AVERROR(errno);
  488. }
  489. av_log_set_callback(log_callback_report);
  490. av_log(NULL, AV_LOG_INFO,
  491. "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
  492. "Report written to \"%s\"\n",
  493. program_name,
  494. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  495. tm->tm_hour, tm->tm_min, tm->tm_sec,
  496. filename);
  497. av_log_set_level(FFMAX(av_log_get_level(), AV_LOG_VERBOSE));
  498. return 0;
  499. }
  500. int opt_max_alloc(void *optctx, const char *opt, const char *arg)
  501. {
  502. char *tail;
  503. size_t max;
  504. max = strtol(arg, &tail, 10);
  505. if (*tail) {
  506. av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
  507. exit(1);
  508. }
  509. av_max_alloc(max);
  510. return 0;
  511. }
  512. int opt_cpuflags(void *optctx, const char *opt, const char *arg)
  513. {
  514. int ret;
  515. unsigned flags = av_get_cpu_flags();
  516. if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
  517. return ret;
  518. av_force_cpu_flags(flags);
  519. return 0;
  520. }
  521. int opt_codec_debug(void *optctx, const char *opt, const char *arg)
  522. {
  523. av_log_set_level(AV_LOG_DEBUG);
  524. return opt_default(NULL, opt, arg);
  525. }
  526. int opt_timelimit(void *optctx, const char *opt, const char *arg)
  527. {
  528. #if HAVE_SETRLIMIT
  529. int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
  530. struct rlimit rl = { lim, lim + 1 };
  531. if (setrlimit(RLIMIT_CPU, &rl))
  532. perror("setrlimit");
  533. #else
  534. av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
  535. #endif
  536. return 0;
  537. }
  538. void print_error(const char *filename, int err)
  539. {
  540. char errbuf[128];
  541. const char *errbuf_ptr = errbuf;
  542. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  543. errbuf_ptr = strerror(AVUNERROR(err));
  544. av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
  545. }
  546. static int warned_cfg = 0;
  547. #define INDENT 1
  548. #define SHOW_VERSION 2
  549. #define SHOW_CONFIG 4
  550. #define SHOW_COPYRIGHT 8
  551. #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
  552. if (CONFIG_##LIBNAME) { \
  553. const char *indent = flags & INDENT? " " : ""; \
  554. if (flags & SHOW_VERSION) { \
  555. unsigned int version = libname##_version(); \
  556. av_log(NULL, level, \
  557. "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
  558. indent, #libname, \
  559. LIB##LIBNAME##_VERSION_MAJOR, \
  560. LIB##LIBNAME##_VERSION_MINOR, \
  561. LIB##LIBNAME##_VERSION_MICRO, \
  562. version >> 16, version >> 8 & 0xff, version & 0xff); \
  563. } \
  564. if (flags & SHOW_CONFIG) { \
  565. const char *cfg = libname##_configuration(); \
  566. if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
  567. if (!warned_cfg) { \
  568. av_log(NULL, level, \
  569. "%sWARNING: library configuration mismatch\n", \
  570. indent); \
  571. warned_cfg = 1; \
  572. } \
  573. av_log(NULL, level, "%s%-11s configuration: %s\n", \
  574. indent, #libname, cfg); \
  575. } \
  576. } \
  577. } \
  578. static void print_all_libs_info(int flags, int level)
  579. {
  580. PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
  581. PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
  582. PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
  583. PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
  584. PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
  585. // PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
  586. PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
  587. PRINT_LIB_INFO(swresample,SWRESAMPLE, flags, level);
  588. #if CONFIG_POSTPROC
  589. PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
  590. #endif
  591. }
  592. static void print_program_info(int flags, int level)
  593. {
  594. const char *indent = flags & INDENT? " " : "";
  595. av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
  596. if (flags & SHOW_COPYRIGHT)
  597. av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
  598. program_birth_year, this_year);
  599. av_log(NULL, level, "\n");
  600. av_log(NULL, level, "%sbuilt on %s %s with %s\n",
  601. indent, __DATE__, __TIME__, CC_IDENT);
  602. av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
  603. }
  604. void show_banner(int argc, char **argv, const OptionDef *options)
  605. {
  606. int idx = locate_option(argc, argv, options, "version");
  607. if (idx)
  608. return;
  609. print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
  610. print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
  611. print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
  612. }
  613. int show_version(void *optctx, const char *opt, const char *arg)
  614. {
  615. av_log_set_callback(log_callback_help);
  616. print_program_info (0 , AV_LOG_INFO);
  617. print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
  618. return 0;
  619. }
  620. int show_license(void *optctx, const char *opt, const char *arg)
  621. {
  622. #if CONFIG_NONFREE
  623. printf(
  624. "This version of %s has nonfree parts compiled in.\n"
  625. "Therefore it is not legally redistributable.\n",
  626. program_name );
  627. #elif CONFIG_GPLV3
  628. printf(
  629. "%s is free software; you can redistribute it and/or modify\n"
  630. "it under the terms of the GNU General Public License as published by\n"
  631. "the Free Software Foundation; either version 3 of the License, or\n"
  632. "(at your option) any later version.\n"
  633. "\n"
  634. "%s is distributed in the hope that it will be useful,\n"
  635. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  636. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  637. "GNU General Public License for more details.\n"
  638. "\n"
  639. "You should have received a copy of the GNU General Public License\n"
  640. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  641. program_name, program_name, program_name );
  642. #elif CONFIG_GPL
  643. printf(
  644. "%s is free software; you can redistribute it and/or modify\n"
  645. "it under the terms of the GNU General Public License as published by\n"
  646. "the Free Software Foundation; either version 2 of the License, or\n"
  647. "(at your option) any later version.\n"
  648. "\n"
  649. "%s is distributed in the hope that it will be useful,\n"
  650. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  651. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  652. "GNU General Public License for more details.\n"
  653. "\n"
  654. "You should have received a copy of the GNU General Public License\n"
  655. "along with %s; if not, write to the Free Software\n"
  656. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  657. program_name, program_name, program_name );
  658. #elif CONFIG_LGPLV3
  659. printf(
  660. "%s is free software; you can redistribute it and/or modify\n"
  661. "it under the terms of the GNU Lesser General Public License as published by\n"
  662. "the Free Software Foundation; either version 3 of the License, or\n"
  663. "(at your option) any later version.\n"
  664. "\n"
  665. "%s is distributed in the hope that it will be useful,\n"
  666. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  667. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  668. "GNU Lesser General Public License for more details.\n"
  669. "\n"
  670. "You should have received a copy of the GNU Lesser General Public License\n"
  671. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  672. program_name, program_name, program_name );
  673. #else
  674. printf(
  675. "%s is free software; you can redistribute it and/or\n"
  676. "modify it under the terms of the GNU Lesser General Public\n"
  677. "License as published by the Free Software Foundation; either\n"
  678. "version 2.1 of the License, or (at your option) any later version.\n"
  679. "\n"
  680. "%s is distributed in the hope that it will be useful,\n"
  681. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  682. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  683. "Lesser General Public License for more details.\n"
  684. "\n"
  685. "You should have received a copy of the GNU Lesser General Public\n"
  686. "License along with %s; if not, write to the Free Software\n"
  687. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  688. program_name, program_name, program_name );
  689. #endif
  690. return 0;
  691. }
  692. int show_formats(void *optctx, const char *opt, const char *arg)
  693. {
  694. AVInputFormat *ifmt = NULL;
  695. AVOutputFormat *ofmt = NULL;
  696. const char *last_name;
  697. printf("File formats:\n"
  698. " D. = Demuxing supported\n"
  699. " .E = Muxing supported\n"
  700. " --\n");
  701. last_name = "000";
  702. for (;;) {
  703. int decode = 0;
  704. int encode = 0;
  705. const char *name = NULL;
  706. const char *long_name = NULL;
  707. while ((ofmt = av_oformat_next(ofmt))) {
  708. if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
  709. strcmp(ofmt->name, last_name) > 0) {
  710. name = ofmt->name;
  711. long_name = ofmt->long_name;
  712. encode = 1;
  713. }
  714. }
  715. while ((ifmt = av_iformat_next(ifmt))) {
  716. if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
  717. strcmp(ifmt->name, last_name) > 0) {
  718. name = ifmt->name;
  719. long_name = ifmt->long_name;
  720. encode = 0;
  721. }
  722. if (name && strcmp(ifmt->name, name) == 0)
  723. decode = 1;
  724. }
  725. if (name == NULL)
  726. break;
  727. last_name = name;
  728. printf(" %s%s %-15s %s\n",
  729. decode ? "D" : " ",
  730. encode ? "E" : " ",
  731. name,
  732. long_name ? long_name:" ");
  733. }
  734. return 0;
  735. }
  736. #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
  737. if (codec->field) { \
  738. const type *p = c->field; \
  739. \
  740. printf(" Supported " list_name ":"); \
  741. while (*p != term) { \
  742. get_name(*p); \
  743. printf(" %s", name); \
  744. p++; \
  745. } \
  746. printf("\n"); \
  747. } \
  748. static void print_codec(const AVCodec *c)
  749. {
  750. int encoder = av_codec_is_encoder(c);
  751. printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
  752. c->long_name ? c->long_name : "");
  753. if (c->type == AVMEDIA_TYPE_VIDEO) {
  754. printf(" Threading capabilities: ");
  755. switch (c->capabilities & (CODEC_CAP_FRAME_THREADS |
  756. CODEC_CAP_SLICE_THREADS)) {
  757. case CODEC_CAP_FRAME_THREADS |
  758. CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
  759. case CODEC_CAP_FRAME_THREADS: printf("frame"); break;
  760. case CODEC_CAP_SLICE_THREADS: printf("slice"); break;
  761. default: printf("no"); break;
  762. }
  763. printf("\n");
  764. }
  765. if (c->supported_framerates) {
  766. const AVRational *fps = c->supported_framerates;
  767. printf(" Supported framerates:");
  768. while (fps->num) {
  769. printf(" %d/%d", fps->num, fps->den);
  770. fps++;
  771. }
  772. printf("\n");
  773. }
  774. PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
  775. AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
  776. PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
  777. GET_SAMPLE_RATE_NAME);
  778. PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
  779. AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
  780. PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
  781. 0, GET_CH_LAYOUT_DESC);
  782. if (c->priv_class) {
  783. show_help_children(c->priv_class,
  784. AV_OPT_FLAG_ENCODING_PARAM |
  785. AV_OPT_FLAG_DECODING_PARAM);
  786. }
  787. }
  788. static char get_media_type_char(enum AVMediaType type)
  789. {
  790. switch (type) {
  791. case AVMEDIA_TYPE_VIDEO: return 'V';
  792. case AVMEDIA_TYPE_AUDIO: return 'A';
  793. case AVMEDIA_TYPE_DATA: return 'D';
  794. case AVMEDIA_TYPE_SUBTITLE: return 'S';
  795. case AVMEDIA_TYPE_ATTACHMENT:return 'T';
  796. default: return '?';
  797. }
  798. }
  799. static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
  800. int encoder)
  801. {
  802. while ((prev = av_codec_next(prev))) {
  803. if (prev->id == id &&
  804. (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
  805. return prev;
  806. }
  807. return NULL;
  808. }
  809. static int compare_codec_desc(const void *a, const void *b)
  810. {
  811. const AVCodecDescriptor * const *da = a;
  812. const AVCodecDescriptor * const *db = b;
  813. return (*da)->type != (*db)->type ? (*da)->type - (*db)->type :
  814. strcmp((*da)->name, (*db)->name);
  815. }
  816. static unsigned get_codecs_sorted(const AVCodecDescriptor ***rcodecs)
  817. {
  818. const AVCodecDescriptor *desc = NULL;
  819. const AVCodecDescriptor **codecs;
  820. unsigned nb_codecs = 0, i = 0;
  821. while ((desc = avcodec_descriptor_next(desc)))
  822. nb_codecs++;
  823. if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs)))) {
  824. av_log(0, AV_LOG_ERROR, "Out of memory\n");
  825. exit(1);
  826. }
  827. desc = NULL;
  828. while ((desc = avcodec_descriptor_next(desc)))
  829. codecs[i++] = desc;
  830. av_assert0(i == nb_codecs);
  831. qsort(codecs, nb_codecs, sizeof(*codecs), compare_codec_desc);
  832. *rcodecs = codecs;
  833. return nb_codecs;
  834. }
  835. static void print_codecs_for_id(enum AVCodecID id, int encoder)
  836. {
  837. const AVCodec *codec = NULL;
  838. printf(" (%s: ", encoder ? "encoders" : "decoders");
  839. while ((codec = next_codec_for_id(id, codec, encoder)))
  840. printf("%s ", codec->name);
  841. printf(")");
  842. }
  843. int show_codecs(void *optctx, const char *opt, const char *arg)
  844. {
  845. const AVCodecDescriptor **codecs;
  846. unsigned i, nb_codecs = get_codecs_sorted(&codecs);
  847. printf("Codecs:\n"
  848. " D..... = Decoding supported\n"
  849. " .E.... = Encoding supported\n"
  850. " ..V... = Video codec\n"
  851. " ..A... = Audio codec\n"
  852. " ..S... = Subtitle codec\n"
  853. " ...I.. = Intra frame-only codec\n"
  854. " ....L. = Lossy compression\n"
  855. " .....S = Lossless compression\n"
  856. " -------\n");
  857. for (i = 0; i < nb_codecs; i++) {
  858. const AVCodecDescriptor *desc = codecs[i];
  859. const AVCodec *codec = NULL;
  860. printf(" ");
  861. printf(avcodec_find_decoder(desc->id) ? "D" : ".");
  862. printf(avcodec_find_encoder(desc->id) ? "E" : ".");
  863. printf("%c", get_media_type_char(desc->type));
  864. printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
  865. printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
  866. printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
  867. printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
  868. /* print decoders/encoders when there's more than one or their
  869. * names are different from codec name */
  870. while ((codec = next_codec_for_id(desc->id, codec, 0))) {
  871. if (strcmp(codec->name, desc->name)) {
  872. print_codecs_for_id(desc->id, 0);
  873. break;
  874. }
  875. }
  876. codec = NULL;
  877. while ((codec = next_codec_for_id(desc->id, codec, 1))) {
  878. if (strcmp(codec->name, desc->name)) {
  879. print_codecs_for_id(desc->id, 1);
  880. break;
  881. }
  882. }
  883. printf("\n");
  884. }
  885. av_free(codecs);
  886. return 0;
  887. }
  888. static void print_codecs(int encoder)
  889. {
  890. const AVCodecDescriptor **codecs;
  891. unsigned i, nb_codecs = get_codecs_sorted(&codecs);
  892. printf("%s:\n"
  893. " V..... = Video\n"
  894. " A..... = Audio\n"
  895. " S..... = Subtitle\n"
  896. " .F.... = Frame-level multithreading\n"
  897. " ..S... = Slice-level multithreading\n"
  898. " ...X.. = Codec is experimental\n"
  899. " ....B. = Supports draw_horiz_band\n"
  900. " .....D = Supports direct rendering method 1\n"
  901. " ------\n",
  902. encoder ? "Encoders" : "Decoders");
  903. for (i = 0; i < nb_codecs; i++) {
  904. const AVCodecDescriptor *desc = codecs[i];
  905. const AVCodec *codec = NULL;
  906. while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
  907. printf(" %c", get_media_type_char(desc->type));
  908. printf((codec->capabilities & CODEC_CAP_FRAME_THREADS) ? "F" : ".");
  909. printf((codec->capabilities & CODEC_CAP_SLICE_THREADS) ? "S" : ".");
  910. printf((codec->capabilities & CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
  911. printf((codec->capabilities & CODEC_CAP_DRAW_HORIZ_BAND)?"B" : ".");
  912. printf((codec->capabilities & CODEC_CAP_DR1) ? "D" : ".");
  913. printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
  914. if (strcmp(codec->name, desc->name))
  915. printf(" (codec %s)", desc->name);
  916. printf("\n");
  917. }
  918. }
  919. av_free(codecs);
  920. }
  921. int show_decoders(void *optctx, const char *opt, const char *arg)
  922. {
  923. print_codecs(0);
  924. return 0;
  925. }
  926. int show_encoders(void *optctx, const char *opt, const char *arg)
  927. {
  928. print_codecs(1);
  929. return 0;
  930. }
  931. int show_bsfs(void *optctx, const char *opt, const char *arg)
  932. {
  933. AVBitStreamFilter *bsf = NULL;
  934. printf("Bitstream filters:\n");
  935. while ((bsf = av_bitstream_filter_next(bsf)))
  936. printf("%s\n", bsf->name);
  937. printf("\n");
  938. return 0;
  939. }
  940. int show_protocols(void *optctx, const char *opt, const char *arg)
  941. {
  942. void *opaque = NULL;
  943. const char *name;
  944. printf("Supported file protocols:\n"
  945. "Input:\n");
  946. while ((name = avio_enum_protocols(&opaque, 0)))
  947. printf("%s\n", name);
  948. printf("Output:\n");
  949. while ((name = avio_enum_protocols(&opaque, 1)))
  950. printf("%s\n", name);
  951. return 0;
  952. }
  953. int show_filters(void *optctx, const char *opt, const char *arg)
  954. {
  955. AVFilter av_unused(**filter) = NULL;
  956. char descr[64], *descr_cur;
  957. int i, j;
  958. const AVFilterPad *pad;
  959. printf("Filters:\n");
  960. #if CONFIG_AVFILTER
  961. while ((filter = av_filter_next(filter)) && *filter) {
  962. descr_cur = descr;
  963. for (i = 0; i < 2; i++) {
  964. if (i) {
  965. *(descr_cur++) = '-';
  966. *(descr_cur++) = '>';
  967. }
  968. pad = i ? (*filter)->outputs : (*filter)->inputs;
  969. for (j = 0; pad && pad[j].name; j++) {
  970. if (descr_cur >= descr + sizeof(descr) - 4)
  971. break;
  972. *(descr_cur++) = get_media_type_char(pad[j].type);
  973. }
  974. if (!j)
  975. *(descr_cur++) = '|';
  976. }
  977. *descr_cur = 0;
  978. printf("%-16s %-10s %s\n", (*filter)->name, descr, (*filter)->description);
  979. }
  980. #endif
  981. return 0;
  982. }
  983. int show_pix_fmts(void *optctx, const char *opt, const char *arg)
  984. {
  985. const AVPixFmtDescriptor *pix_desc = NULL;
  986. printf("Pixel formats:\n"
  987. "I.... = Supported Input format for conversion\n"
  988. ".O... = Supported Output format for conversion\n"
  989. "..H.. = Hardware accelerated format\n"
  990. "...P. = Paletted format\n"
  991. "....B = Bitstream format\n"
  992. "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
  993. "-----\n");
  994. #if !CONFIG_SWSCALE
  995. # define sws_isSupportedInput(x) 0
  996. # define sws_isSupportedOutput(x) 0
  997. #endif
  998. while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
  999. enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
  1000. if(!pix_desc->name)
  1001. continue;
  1002. printf("%c%c%c%c%c %-16s %d %2d\n",
  1003. sws_isSupportedInput (pix_fmt) ? 'I' : '.',
  1004. sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
  1005. pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
  1006. pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
  1007. pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
  1008. pix_desc->name,
  1009. pix_desc->nb_components,
  1010. av_get_bits_per_pixel(pix_desc));
  1011. }
  1012. return 0;
  1013. }
  1014. int show_layouts(void *optctx, const char *opt, const char *arg)
  1015. {
  1016. int i = 0;
  1017. uint64_t layout, j;
  1018. const char *name, *descr;
  1019. printf("Individual channels:\n"
  1020. "NAME DESCRIPTION\n");
  1021. for (i = 0; i < 63; i++) {
  1022. name = av_get_channel_name((uint64_t)1 << i);
  1023. if (!name)
  1024. continue;
  1025. descr = av_get_channel_description((uint64_t)1 << i);
  1026. printf("%-12s%s\n", name, descr);
  1027. }
  1028. printf("\nStandard channel layouts:\n"
  1029. "NAME DECOMPOSITION\n");
  1030. for (i = 0; !av_get_standard_channel_layout(i, &layout, &name); i++) {
  1031. if (name) {
  1032. printf("%-12s", name);
  1033. for (j = 1; j; j <<= 1)
  1034. if ((layout & j))
  1035. printf("%s%s", (layout & (j - 1)) ? "+" : "", av_get_channel_name(j));
  1036. printf("\n");
  1037. }
  1038. }
  1039. return 0;
  1040. }
  1041. int show_sample_fmts(void *optctx, const char *opt, const char *arg)
  1042. {
  1043. int i;
  1044. char fmt_str[128];
  1045. for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
  1046. printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
  1047. return 0;
  1048. }
  1049. static void show_help_codec(const char *name, int encoder)
  1050. {
  1051. const AVCodecDescriptor *desc;
  1052. const AVCodec *codec;
  1053. if (!name) {
  1054. av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
  1055. return;
  1056. }
  1057. codec = encoder ? avcodec_find_encoder_by_name(name) :
  1058. avcodec_find_decoder_by_name(name);
  1059. if (codec)
  1060. print_codec(codec);
  1061. else if ((desc = avcodec_descriptor_get_by_name(name))) {
  1062. int printed = 0;
  1063. while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
  1064. printed = 1;
  1065. print_codec(codec);
  1066. }
  1067. if (!printed) {
  1068. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, "
  1069. "but no %s for it are available. FFmpeg might need to be "
  1070. "recompiled with additional external libraries.\n",
  1071. name, encoder ? "encoders" : "decoders");
  1072. }
  1073. } else {
  1074. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n",
  1075. name);
  1076. }
  1077. }
  1078. static void show_help_demuxer(const char *name)
  1079. {
  1080. const AVInputFormat *fmt = av_find_input_format(name);
  1081. if (!fmt) {
  1082. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1083. return;
  1084. }
  1085. printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
  1086. if (fmt->extensions)
  1087. printf(" Common extensions: %s.\n", fmt->extensions);
  1088. if (fmt->priv_class)
  1089. show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
  1090. }
  1091. static void show_help_muxer(const char *name)
  1092. {
  1093. const AVCodecDescriptor *desc;
  1094. const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
  1095. if (!fmt) {
  1096. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1097. return;
  1098. }
  1099. printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
  1100. if (fmt->extensions)
  1101. printf(" Common extensions: %s.\n", fmt->extensions);
  1102. if (fmt->mime_type)
  1103. printf(" Mime type: %s.\n", fmt->mime_type);
  1104. if (fmt->video_codec != AV_CODEC_ID_NONE &&
  1105. (desc = avcodec_descriptor_get(fmt->video_codec))) {
  1106. printf(" Default video codec: %s.\n", desc->name);
  1107. }
  1108. if (fmt->audio_codec != AV_CODEC_ID_NONE &&
  1109. (desc = avcodec_descriptor_get(fmt->audio_codec))) {
  1110. printf(" Default audio codec: %s.\n", desc->name);
  1111. }
  1112. if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
  1113. (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
  1114. printf(" Default subtitle codec: %s.\n", desc->name);
  1115. }
  1116. if (fmt->priv_class)
  1117. show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
  1118. }
  1119. int show_help(void *optctx, const char *opt, const char *arg)
  1120. {
  1121. char *topic, *par;
  1122. av_log_set_callback(log_callback_help);
  1123. topic = av_strdup(arg ? arg : "");
  1124. par = strchr(topic, '=');
  1125. if (par)
  1126. *par++ = 0;
  1127. if (!*topic) {
  1128. show_help_default(topic, par);
  1129. } else if (!strcmp(topic, "decoder")) {
  1130. show_help_codec(par, 0);
  1131. } else if (!strcmp(topic, "encoder")) {
  1132. show_help_codec(par, 1);
  1133. } else if (!strcmp(topic, "demuxer")) {
  1134. show_help_demuxer(par);
  1135. } else if (!strcmp(topic, "muxer")) {
  1136. show_help_muxer(par);
  1137. } else {
  1138. show_help_default(topic, par);
  1139. }
  1140. av_freep(&topic);
  1141. return 0;
  1142. }
  1143. int read_yesno(void)
  1144. {
  1145. int c = getchar();
  1146. int yesno = (toupper(c) == 'Y');
  1147. while (c != '\n' && c != EOF)
  1148. c = getchar();
  1149. return yesno;
  1150. }
  1151. int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
  1152. {
  1153. int ret;
  1154. FILE *f = fopen(filename, "rb");
  1155. if (!f) {
  1156. av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
  1157. strerror(errno));
  1158. return AVERROR(errno);
  1159. }
  1160. fseek(f, 0, SEEK_END);
  1161. *size = ftell(f);
  1162. fseek(f, 0, SEEK_SET);
  1163. if (*size == (size_t)-1) {
  1164. av_log(NULL, AV_LOG_ERROR, "IO error: %s\n", strerror(errno));
  1165. fclose(f);
  1166. return AVERROR(errno);
  1167. }
  1168. *bufptr = av_malloc(*size + 1);
  1169. if (!*bufptr) {
  1170. av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
  1171. fclose(f);
  1172. return AVERROR(ENOMEM);
  1173. }
  1174. ret = fread(*bufptr, 1, *size, f);
  1175. if (ret < *size) {
  1176. av_free(*bufptr);
  1177. if (ferror(f)) {
  1178. av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
  1179. filename, strerror(errno));
  1180. ret = AVERROR(errno);
  1181. } else
  1182. ret = AVERROR_EOF;
  1183. } else {
  1184. ret = 0;
  1185. (*bufptr)[(*size)++] = '\0';
  1186. }
  1187. fclose(f);
  1188. return ret;
  1189. }
  1190. FILE *get_preset_file(char *filename, size_t filename_size,
  1191. const char *preset_name, int is_path,
  1192. const char *codec_name)
  1193. {
  1194. FILE *f = NULL;
  1195. int i;
  1196. const char *base[3] = { getenv("FFMPEG_DATADIR"),
  1197. getenv("HOME"),
  1198. FFMPEG_DATADIR, };
  1199. if (is_path) {
  1200. av_strlcpy(filename, preset_name, filename_size);
  1201. f = fopen(filename, "r");
  1202. } else {
  1203. #ifdef _WIN32
  1204. char datadir[MAX_PATH], *ls;
  1205. base[2] = NULL;
  1206. if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
  1207. {
  1208. for (ls = datadir; ls < datadir + strlen(datadir); ls++)
  1209. if (*ls == '\\') *ls = '/';
  1210. if (ls = strrchr(datadir, '/'))
  1211. {
  1212. *ls = 0;
  1213. strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
  1214. base[2] = datadir;
  1215. }
  1216. }
  1217. #endif
  1218. for (i = 0; i < 3 && !f; i++) {
  1219. if (!base[i])
  1220. continue;
  1221. snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
  1222. i != 1 ? "" : "/.ffmpeg", preset_name);
  1223. f = fopen(filename, "r");
  1224. if (!f && codec_name) {
  1225. snprintf(filename, filename_size,
  1226. "%s%s/%s-%s.ffpreset",
  1227. base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
  1228. preset_name);
  1229. f = fopen(filename, "r");
  1230. }
  1231. }
  1232. }
  1233. return f;
  1234. }
  1235. int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
  1236. {
  1237. int ret = avformat_match_stream_specifier(s, st, spec);
  1238. if (ret < 0)
  1239. av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
  1240. return ret;
  1241. }
  1242. AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
  1243. AVFormatContext *s, AVStream *st, AVCodec *codec)
  1244. {
  1245. AVDictionary *ret = NULL;
  1246. AVDictionaryEntry *t = NULL;
  1247. int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
  1248. : AV_OPT_FLAG_DECODING_PARAM;
  1249. char prefix = 0;
  1250. const AVClass *cc = avcodec_get_class();
  1251. if (!codec)
  1252. codec = s->oformat ? avcodec_find_encoder(codec_id)
  1253. : avcodec_find_decoder(codec_id);
  1254. if (!codec)
  1255. return NULL;
  1256. switch (codec->type) {
  1257. case AVMEDIA_TYPE_VIDEO:
  1258. prefix = 'v';
  1259. flags |= AV_OPT_FLAG_VIDEO_PARAM;
  1260. break;
  1261. case AVMEDIA_TYPE_AUDIO:
  1262. prefix = 'a';
  1263. flags |= AV_OPT_FLAG_AUDIO_PARAM;
  1264. break;
  1265. case AVMEDIA_TYPE_SUBTITLE:
  1266. prefix = 's';
  1267. flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
  1268. break;
  1269. }
  1270. while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
  1271. char *p = strchr(t->key, ':');
  1272. /* check stream specification in opt name */
  1273. if (p)
  1274. switch (check_stream_specifier(s, st, p + 1)) {
  1275. case 1: *p = 0; break;
  1276. case 0: continue;
  1277. default: return NULL;
  1278. }
  1279. if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
  1280. (codec && codec->priv_class &&
  1281. av_opt_find(&codec->priv_class, t->key, NULL, flags,
  1282. AV_OPT_SEARCH_FAKE_OBJ)))
  1283. av_dict_set(&ret, t->key, t->value, 0);
  1284. else if (t->key[0] == prefix &&
  1285. av_opt_find(&cc, t->key + 1, NULL, flags,
  1286. AV_OPT_SEARCH_FAKE_OBJ))
  1287. av_dict_set(&ret, t->key + 1, t->value, 0);
  1288. if (p)
  1289. *p = ':';
  1290. }
  1291. return ret;
  1292. }
  1293. AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
  1294. AVDictionary *codec_opts)
  1295. {
  1296. int i;
  1297. AVDictionary **opts;
  1298. if (!s->nb_streams)
  1299. return NULL;
  1300. opts = av_mallocz(s->nb_streams * sizeof(*opts));
  1301. if (!opts) {
  1302. av_log(NULL, AV_LOG_ERROR,
  1303. "Could not alloc memory for stream options.\n");
  1304. return NULL;
  1305. }
  1306. for (i = 0; i < s->nb_streams; i++)
  1307. opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
  1308. s, s->streams[i], NULL);
  1309. return opts;
  1310. }
  1311. void *grow_array(void *array, int elem_size, int *size, int new_size)
  1312. {
  1313. if (new_size >= INT_MAX / elem_size) {
  1314. av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
  1315. exit(1);
  1316. }
  1317. if (*size < new_size) {
  1318. uint8_t *tmp = av_realloc(array, new_size*elem_size);
  1319. if (!tmp) {
  1320. av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
  1321. exit(1);
  1322. }
  1323. memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
  1324. *size = new_size;
  1325. return tmp;
  1326. }
  1327. return array;
  1328. }
  1329. static int alloc_buffer(FrameBuffer **pool, AVCodecContext *s, FrameBuffer **pbuf)
  1330. {
  1331. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  1332. FrameBuffer *buf;
  1333. int i, ret;
  1334. int pixel_size;
  1335. int h_chroma_shift, v_chroma_shift;
  1336. int edge = 32; // XXX should be avcodec_get_edge_width(), but that fails on svq1
  1337. int w = s->width, h = s->height;
  1338. if (!desc)
  1339. return AVERROR(EINVAL);
  1340. pixel_size = desc->comp[0].step_minus1 + 1;
  1341. buf = av_mallocz(sizeof(*buf));
  1342. if (!buf)
  1343. return AVERROR(ENOMEM);
  1344. avcodec_align_dimensions(s, &w, &h);
  1345. if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
  1346. w += 2*edge;
  1347. h += 2*edge;
  1348. }
  1349. if ((ret = av_image_alloc(buf->base, buf->linesize, w, h,
  1350. s->pix_fmt, 32)) < 0) {
  1351. av_freep(&buf);
  1352. av_log(s, AV_LOG_ERROR, "alloc_buffer: av_image_alloc() failed\n");
  1353. return ret;
  1354. }
  1355. /* XXX this shouldn't be needed, but some tests break without this line
  1356. * those decoders are buggy and need to be fixed.
  1357. * the following tests fail:
  1358. * cdgraphics, ansi, aasc, fraps-v1, qtrle-1bit
  1359. */
  1360. memset(buf->base[0], 128, ret);
  1361. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1362. for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
  1363. const int h_shift = i==0 ? 0 : h_chroma_shift;
  1364. const int v_shift = i==0 ? 0 : v_chroma_shift;
  1365. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !buf->linesize[i] || !buf->base[i])
  1366. buf->data[i] = buf->base[i];
  1367. else
  1368. buf->data[i] = buf->base[i] +
  1369. FFALIGN((buf->linesize[i]*edge >> v_shift) +
  1370. (pixel_size*edge >> h_shift), 32);
  1371. }
  1372. buf->w = s->width;
  1373. buf->h = s->height;
  1374. buf->pix_fmt = s->pix_fmt;
  1375. buf->pool = pool;
  1376. *pbuf = buf;
  1377. return 0;
  1378. }
  1379. int codec_get_buffer(AVCodecContext *s, AVFrame *frame)
  1380. {
  1381. FrameBuffer **pool = s->opaque;
  1382. FrameBuffer *buf;
  1383. int ret, i;
  1384. if(av_image_check_size(s->width, s->height, 0, s) || s->pix_fmt<0) {
  1385. av_log(s, AV_LOG_ERROR, "codec_get_buffer: image parameters invalid\n");
  1386. return -1;
  1387. }
  1388. if (!*pool && (ret = alloc_buffer(pool, s, pool)) < 0)
  1389. return ret;
  1390. buf = *pool;
  1391. *pool = buf->next;
  1392. buf->next = NULL;
  1393. if (buf->w != s->width || buf->h != s->height || buf->pix_fmt != s->pix_fmt) {
  1394. av_freep(&buf->base[0]);
  1395. av_free(buf);
  1396. if ((ret = alloc_buffer(pool, s, &buf)) < 0)
  1397. return ret;
  1398. }
  1399. av_assert0(!buf->refcount);
  1400. buf->refcount++;
  1401. frame->opaque = buf;
  1402. frame->type = FF_BUFFER_TYPE_USER;
  1403. frame->extended_data = frame->data;
  1404. frame->pkt_pts = s->pkt ? s->pkt->pts : AV_NOPTS_VALUE;
  1405. frame->width = buf->w;
  1406. frame->height = buf->h;
  1407. frame->format = buf->pix_fmt;
  1408. frame->sample_aspect_ratio = s->sample_aspect_ratio;
  1409. for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
  1410. frame->base[i] = buf->base[i]; // XXX h264.c uses base though it shouldn't
  1411. frame->data[i] = buf->data[i];
  1412. frame->linesize[i] = buf->linesize[i];
  1413. }
  1414. return 0;
  1415. }
  1416. static void unref_buffer(FrameBuffer *buf)
  1417. {
  1418. FrameBuffer **pool = buf->pool;
  1419. av_assert0(buf->refcount > 0);
  1420. buf->refcount--;
  1421. if (!buf->refcount) {
  1422. FrameBuffer *tmp;
  1423. for(tmp= *pool; tmp; tmp= tmp->next)
  1424. av_assert1(tmp != buf);
  1425. buf->next = *pool;
  1426. *pool = buf;
  1427. }
  1428. }
  1429. void codec_release_buffer(AVCodecContext *s, AVFrame *frame)
  1430. {
  1431. FrameBuffer *buf = frame->opaque;
  1432. int i;
  1433. if(frame->type!=FF_BUFFER_TYPE_USER) {
  1434. avcodec_default_release_buffer(s, frame);
  1435. return;
  1436. }
  1437. for (i = 0; i < FF_ARRAY_ELEMS(frame->data); i++)
  1438. frame->data[i] = NULL;
  1439. unref_buffer(buf);
  1440. }
  1441. void filter_release_buffer(AVFilterBuffer *fb)
  1442. {
  1443. FrameBuffer *buf = fb->priv;
  1444. av_free(fb);
  1445. unref_buffer(buf);
  1446. }
  1447. void free_buffer_pool(FrameBuffer **pool)
  1448. {
  1449. FrameBuffer *buf = *pool;
  1450. while (buf) {
  1451. *pool = buf->next;
  1452. av_freep(&buf->base[0]);
  1453. av_free(buf);
  1454. buf = *pool;
  1455. }
  1456. }