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.

1967 lines
61KB

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