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.

1719 lines
54KB

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