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.

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