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.

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