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.

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