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.

1636 lines
51KB

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