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.

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