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.

979 lines
33KB

  1. /*
  2. * Various utilities for command line tools
  3. * Copyright (c) 2000-2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "libavformat/avformat.h"
  30. #include "libavfilter/avfilter.h"
  31. #include "libavdevice/avdevice.h"
  32. #include "libswscale/swscale.h"
  33. #include "libpostproc/postprocess.h"
  34. #include "libavutil/avstring.h"
  35. #include "libavutil/parseutils.h"
  36. #include "libavutil/pixdesc.h"
  37. #include "libavutil/eval.h"
  38. #include "libavutil/dict.h"
  39. #include "libavutil/opt.h"
  40. #include "cmdutils.h"
  41. #include "version.h"
  42. #if CONFIG_NETWORK
  43. #include "libavformat/network.h"
  44. #endif
  45. #if HAVE_SYS_RESOURCE_H
  46. #include <sys/resource.h>
  47. #endif
  48. const char **opt_names;
  49. const char **opt_values;
  50. static int opt_name_count;
  51. AVCodecContext *avcodec_opts[AVMEDIA_TYPE_NB];
  52. AVFormatContext *avformat_opts;
  53. struct SwsContext *sws_opts;
  54. AVDictionary *format_opts, *codec_opts;
  55. static const int this_year = 2011;
  56. void init_opts(void)
  57. {
  58. int i;
  59. for (i = 0; i < AVMEDIA_TYPE_NB; i++)
  60. avcodec_opts[i] = avcodec_alloc_context3(NULL);
  61. avformat_opts = avformat_alloc_context();
  62. #if CONFIG_SWSCALE
  63. sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC, NULL, NULL, NULL);
  64. #endif
  65. }
  66. void uninit_opts(void)
  67. {
  68. int i;
  69. for (i = 0; i < AVMEDIA_TYPE_NB; i++)
  70. av_freep(&avcodec_opts[i]);
  71. av_freep(&avformat_opts->key);
  72. av_freep(&avformat_opts);
  73. #if CONFIG_SWSCALE
  74. sws_freeContext(sws_opts);
  75. sws_opts = NULL;
  76. #endif
  77. for (i = 0; i < opt_name_count; i++) {
  78. //opt_values are only stored for codec-specific options in which case
  79. //both the name and value are dup'd
  80. if (opt_values[i]) {
  81. av_freep(&opt_names[i]);
  82. av_freep(&opt_values[i]);
  83. }
  84. }
  85. av_freep(&opt_names);
  86. av_freep(&opt_values);
  87. opt_name_count = 0;
  88. av_dict_free(&format_opts);
  89. av_dict_free(&codec_opts);
  90. }
  91. void log_callback_help(void* ptr, int level, const char* fmt, va_list vl)
  92. {
  93. vfprintf(stdout, fmt, vl);
  94. }
  95. double parse_number_or_die(const char *context, const char *numstr, int type, double min, double max)
  96. {
  97. char *tail;
  98. const char *error;
  99. double d = av_strtod(numstr, &tail);
  100. if (*tail)
  101. error= "Expected number for %s but found: %s\n";
  102. else if (d < min || d > max)
  103. error= "The value for %s was %s which is not within %f - %f\n";
  104. else if(type == OPT_INT64 && (int64_t)d != d)
  105. error= "Expected int64 for %s but found %s\n";
  106. else if (type == OPT_INT && (int)d != d)
  107. error= "Expected int for %s but found %s\n";
  108. else
  109. return d;
  110. fprintf(stderr, error, context, numstr, min, max);
  111. exit(1);
  112. }
  113. int64_t parse_time_or_die(const char *context, const char *timestr, int is_duration)
  114. {
  115. int64_t us;
  116. if (av_parse_time(&us, timestr, is_duration) < 0) {
  117. fprintf(stderr, "Invalid %s specification for %s: %s\n",
  118. is_duration ? "duration" : "date", context, timestr);
  119. exit(1);
  120. }
  121. return us;
  122. }
  123. void show_help_options(const OptionDef *options, const char *msg, int mask, int value)
  124. {
  125. const OptionDef *po;
  126. int first;
  127. first = 1;
  128. for(po = options; po->name != NULL; po++) {
  129. char buf[64];
  130. if ((po->flags & mask) == value) {
  131. if (first) {
  132. printf("%s", msg);
  133. first = 0;
  134. }
  135. av_strlcpy(buf, po->name, sizeof(buf));
  136. if (po->flags & HAS_ARG) {
  137. av_strlcat(buf, " ", sizeof(buf));
  138. av_strlcat(buf, po->argname, sizeof(buf));
  139. }
  140. printf("-%-17s %s\n", buf, po->help);
  141. }
  142. }
  143. }
  144. static const OptionDef* find_option(const OptionDef *po, const char *name){
  145. while (po->name != NULL) {
  146. if (!strcmp(name, po->name))
  147. break;
  148. po++;
  149. }
  150. return po;
  151. }
  152. #if defined(_WIN32) && !defined(__MINGW32CE__)
  153. #include <windows.h>
  154. /* Will be leaked on exit */
  155. static char** win32_argv_utf8 = NULL;
  156. static int win32_argc = 0;
  157. /**
  158. * Prepare command line arguments for executable.
  159. * For Windows - perform wide-char to UTF-8 conversion.
  160. * Input arguments should be main() function arguments.
  161. * @param argc_ptr Arguments number (including executable)
  162. * @param argv_ptr Arguments list.
  163. */
  164. static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
  165. {
  166. char *argstr_flat;
  167. wchar_t **argv_w;
  168. int i, buffsize = 0, offset = 0;
  169. if (win32_argv_utf8) {
  170. *argc_ptr = win32_argc;
  171. *argv_ptr = win32_argv_utf8;
  172. return;
  173. }
  174. win32_argc = 0;
  175. argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
  176. if (win32_argc <= 0 || !argv_w)
  177. return;
  178. /* determine the UTF-8 buffer size (including NULL-termination symbols) */
  179. for (i = 0; i < win32_argc; i++)
  180. buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
  181. NULL, 0, NULL, NULL);
  182. win32_argv_utf8 = av_mallocz(sizeof(char*) * (win32_argc + 1) + buffsize);
  183. argstr_flat = (char*)win32_argv_utf8 + sizeof(char*) * (win32_argc + 1);
  184. if (win32_argv_utf8 == NULL) {
  185. LocalFree(argv_w);
  186. return;
  187. }
  188. for (i = 0; i < win32_argc; i++) {
  189. win32_argv_utf8[i] = &argstr_flat[offset];
  190. offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
  191. &argstr_flat[offset],
  192. buffsize - offset, NULL, NULL);
  193. }
  194. win32_argv_utf8[i] = NULL;
  195. LocalFree(argv_w);
  196. *argc_ptr = win32_argc;
  197. *argv_ptr = win32_argv_utf8;
  198. }
  199. #else
  200. static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
  201. {
  202. /* nothing to do */
  203. }
  204. #endif /* WIN32 && !__MINGW32CE__ */
  205. void parse_options(int argc, char **argv, const OptionDef *options,
  206. void (* parse_arg_function)(const char*))
  207. {
  208. const char *opt, *arg;
  209. int optindex, handleoptions=1;
  210. const OptionDef *po;
  211. /* perform system-dependent conversions for arguments list */
  212. prepare_app_arguments(&argc, &argv);
  213. /* parse options */
  214. optindex = 1;
  215. while (optindex < argc) {
  216. opt = argv[optindex++];
  217. if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
  218. int bool_val = 1;
  219. if (opt[1] == '-' && opt[2] == '\0') {
  220. handleoptions = 0;
  221. continue;
  222. }
  223. opt++;
  224. po= find_option(options, opt);
  225. if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
  226. /* handle 'no' bool option */
  227. po = find_option(options, opt + 2);
  228. if (!(po->name && (po->flags & OPT_BOOL)))
  229. goto unknown_opt;
  230. bool_val = 0;
  231. }
  232. if (!po->name)
  233. po= find_option(options, "default");
  234. if (!po->name) {
  235. unknown_opt:
  236. fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
  237. exit(1);
  238. }
  239. arg = NULL;
  240. if (po->flags & HAS_ARG) {
  241. arg = argv[optindex++];
  242. if (!arg) {
  243. fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
  244. exit(1);
  245. }
  246. }
  247. if (po->flags & OPT_STRING) {
  248. char *str;
  249. str = av_strdup(arg);
  250. *po->u.str_arg = str;
  251. } else if (po->flags & OPT_BOOL) {
  252. *po->u.int_arg = bool_val;
  253. } else if (po->flags & OPT_INT) {
  254. *po->u.int_arg = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
  255. } else if (po->flags & OPT_INT64) {
  256. *po->u.int64_arg = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
  257. } else if (po->flags & OPT_FLOAT) {
  258. *po->u.float_arg = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
  259. } else if (po->u.func_arg) {
  260. if (po->u.func_arg(opt, arg) < 0) {
  261. fprintf(stderr, "%s: failed to set value '%s' for option '%s'\n", argv[0], arg, opt);
  262. exit(1);
  263. }
  264. }
  265. if(po->flags & OPT_EXIT)
  266. exit(0);
  267. } else {
  268. if (parse_arg_function)
  269. parse_arg_function(opt);
  270. }
  271. }
  272. }
  273. #define FLAGS (o->type == FF_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
  274. static int opt_default2(const char *opt, const char *arg)
  275. {
  276. const AVOption *o;
  277. if ((o = av_opt_find(avcodec_opts[0], opt, NULL, 0, AV_OPT_SEARCH_CHILDREN)) ||
  278. ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
  279. (o = av_opt_find(avcodec_opts[0], opt+1, NULL, 0, 0))))
  280. av_dict_set(&codec_opts, opt, arg, FLAGS);
  281. else if ((o = av_opt_find(avformat_opts, opt, NULL, 0, AV_OPT_SEARCH_CHILDREN)))
  282. av_dict_set(&format_opts, opt, arg, FLAGS);
  283. else if ((o = av_opt_find(sws_opts, opt, NULL, 0, AV_OPT_SEARCH_CHILDREN))) {
  284. // XXX we only support sws_flags, not arbitrary sws options
  285. int ret = av_set_string3(sws_opts, opt, arg, 1, NULL);
  286. if (ret < 0) {
  287. av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
  288. return ret;
  289. }
  290. }
  291. if (o)
  292. return 0;
  293. fprintf(stderr, "Unrecognized option '%s'\n", opt);
  294. return AVERROR_OPTION_NOT_FOUND;
  295. }
  296. int opt_default(const char *opt, const char *arg){
  297. int type;
  298. int ret= 0;
  299. const AVOption *o= NULL;
  300. int opt_types[]={AV_OPT_FLAG_VIDEO_PARAM, AV_OPT_FLAG_AUDIO_PARAM, 0, AV_OPT_FLAG_SUBTITLE_PARAM, 0};
  301. for(type=0; *avcodec_opts && type<AVMEDIA_TYPE_NB && ret>= 0; type++){
  302. const AVOption *o2 = av_opt_find(avcodec_opts[0], opt, NULL, opt_types[type], 0);
  303. if(o2)
  304. ret = av_set_string3(avcodec_opts[type], opt, arg, 1, &o);
  305. }
  306. if(!o && avformat_opts)
  307. ret = av_set_string3(avformat_opts, opt, arg, 1, &o);
  308. if(!o && sws_opts)
  309. ret = av_set_string3(sws_opts, opt, arg, 1, &o);
  310. if(!o){
  311. if (opt[0] == 'a' && avcodec_opts[AVMEDIA_TYPE_AUDIO])
  312. ret = av_set_string3(avcodec_opts[AVMEDIA_TYPE_AUDIO], opt+1, arg, 1, &o);
  313. else if(opt[0] == 'v' && avcodec_opts[AVMEDIA_TYPE_VIDEO])
  314. ret = av_set_string3(avcodec_opts[AVMEDIA_TYPE_VIDEO], opt+1, arg, 1, &o);
  315. else if(opt[0] == 's' && avcodec_opts[AVMEDIA_TYPE_SUBTITLE])
  316. ret = av_set_string3(avcodec_opts[AVMEDIA_TYPE_SUBTITLE], opt+1, arg, 1, &o);
  317. }
  318. if (o && ret < 0) {
  319. fprintf(stderr, "Invalid value '%s' for option '%s'\n", arg, opt);
  320. exit(1);
  321. }
  322. if (!o) {
  323. AVCodec *p = NULL;
  324. AVOutputFormat *oformat = NULL;
  325. while ((p=av_codec_next(p))){
  326. const AVClass *c = p->priv_class;
  327. if(c && av_opt_find(&c, opt, NULL, 0, 0))
  328. break;
  329. }
  330. if (!p) {
  331. while ((oformat = av_oformat_next(oformat))) {
  332. const AVClass *c = oformat->priv_class;
  333. if (c && av_opt_find(&c, opt, NULL, 0, 0))
  334. break;
  335. }
  336. }
  337. }
  338. if ((ret = opt_default2(opt, arg)) < 0)
  339. return ret;
  340. // av_log(NULL, AV_LOG_ERROR, "%s:%s: %f 0x%0X\n", opt, arg, av_get_double(avcodec_opts, opt, NULL), (int)av_get_int(avcodec_opts, opt, NULL));
  341. //FIXME we should always use avcodec_opts, ... for storing options so there will not be any need to keep track of what i set over this
  342. opt_values= av_realloc(opt_values, sizeof(void*)*(opt_name_count+1));
  343. opt_values[opt_name_count]= o ? NULL : av_strdup(arg);
  344. opt_names= av_realloc(opt_names, sizeof(void*)*(opt_name_count+1));
  345. opt_names[opt_name_count++]= o ? o->name : av_strdup(opt);
  346. if ((*avcodec_opts && avcodec_opts[0]->debug) || (avformat_opts && avformat_opts->debug))
  347. av_log_set_level(AV_LOG_DEBUG);
  348. return 0;
  349. }
  350. int opt_loglevel(const char *opt, const char *arg)
  351. {
  352. const struct { const char *name; int level; } log_levels[] = {
  353. { "quiet" , AV_LOG_QUIET },
  354. { "panic" , AV_LOG_PANIC },
  355. { "fatal" , AV_LOG_FATAL },
  356. { "error" , AV_LOG_ERROR },
  357. { "warning", AV_LOG_WARNING },
  358. { "info" , AV_LOG_INFO },
  359. { "verbose", AV_LOG_VERBOSE },
  360. { "debug" , AV_LOG_DEBUG },
  361. };
  362. char *tail;
  363. int level;
  364. int i;
  365. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
  366. if (!strcmp(log_levels[i].name, arg)) {
  367. av_log_set_level(log_levels[i].level);
  368. return 0;
  369. }
  370. }
  371. level = strtol(arg, &tail, 10);
  372. if (*tail) {
  373. fprintf(stderr, "Invalid loglevel \"%s\". "
  374. "Possible levels are numbers or:\n", arg);
  375. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
  376. fprintf(stderr, "\"%s\"\n", log_levels[i].name);
  377. exit(1);
  378. }
  379. av_log_set_level(level);
  380. return 0;
  381. }
  382. int opt_timelimit(const char *opt, const char *arg)
  383. {
  384. #if HAVE_SETRLIMIT
  385. int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
  386. struct rlimit rl = { lim, lim + 1 };
  387. if (setrlimit(RLIMIT_CPU, &rl))
  388. perror("setrlimit");
  389. #else
  390. fprintf(stderr, "Warning: -%s not implemented on this OS\n", opt);
  391. #endif
  392. return 0;
  393. }
  394. void set_context_opts(void *ctx, void *opts_ctx, int flags, AVCodec *codec)
  395. {
  396. int i;
  397. void *priv_ctx=NULL;
  398. if(!strcmp("AVCodecContext", (*(AVClass**)ctx)->class_name)){
  399. AVCodecContext *avctx= ctx;
  400. if(codec && codec->priv_class && avctx->priv_data){
  401. priv_ctx= avctx->priv_data;
  402. }
  403. } else if (!strcmp("AVFormatContext", (*(AVClass**)ctx)->class_name)) {
  404. AVFormatContext *avctx = ctx;
  405. if (avctx->oformat && avctx->oformat->priv_class) {
  406. priv_ctx = avctx->priv_data;
  407. }
  408. }
  409. for(i=0; i<opt_name_count; i++){
  410. char buf[256];
  411. const AVOption *opt;
  412. const char *str= av_get_string(opts_ctx, opt_names[i], &opt, buf, sizeof(buf));
  413. /* if an option with name opt_names[i] is present in opts_ctx then str is non-NULL */
  414. if(str && ((opt->flags & flags) == flags))
  415. av_set_string3(ctx, opt_names[i], str, 1, NULL);
  416. /* We need to use a differnt system to pass options to the private context because
  417. it is not known which codec and thus context kind that will be when parsing options
  418. we thus use opt_values directly instead of opts_ctx */
  419. if(!str && priv_ctx && av_get_string(priv_ctx, opt_names[i], &opt, buf, sizeof(buf))){
  420. av_set_string3(priv_ctx, opt_names[i], opt_values[i], 1, NULL);
  421. }
  422. }
  423. }
  424. void print_error(const char *filename, int err)
  425. {
  426. char errbuf[128];
  427. const char *errbuf_ptr = errbuf;
  428. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  429. errbuf_ptr = strerror(AVUNERROR(err));
  430. fprintf(stderr, "%s: %s\n", filename, errbuf_ptr);
  431. }
  432. static int warned_cfg = 0;
  433. #define INDENT 1
  434. #define SHOW_VERSION 2
  435. #define SHOW_CONFIG 4
  436. #define PRINT_LIB_INFO(outstream,libname,LIBNAME,flags) \
  437. if (CONFIG_##LIBNAME) { \
  438. const char *indent = flags & INDENT? " " : ""; \
  439. if (flags & SHOW_VERSION) { \
  440. unsigned int version = libname##_version(); \
  441. fprintf(outstream, "%slib%-9s %2d.%3d.%2d / %2d.%3d.%2d\n", \
  442. indent, #libname, \
  443. LIB##LIBNAME##_VERSION_MAJOR, \
  444. LIB##LIBNAME##_VERSION_MINOR, \
  445. LIB##LIBNAME##_VERSION_MICRO, \
  446. version >> 16, version >> 8 & 0xff, version & 0xff); \
  447. } \
  448. if (flags & SHOW_CONFIG) { \
  449. const char *cfg = libname##_configuration(); \
  450. if (strcmp(LIBAV_CONFIGURATION, cfg)) { \
  451. if (!warned_cfg) { \
  452. fprintf(outstream, \
  453. "%sWARNING: library configuration mismatch\n", \
  454. indent); \
  455. warned_cfg = 1; \
  456. } \
  457. fprintf(stderr, "%s%-11s configuration: %s\n", \
  458. indent, #libname, cfg); \
  459. } \
  460. } \
  461. } \
  462. static void print_all_libs_info(FILE* outstream, int flags)
  463. {
  464. PRINT_LIB_INFO(outstream, avutil, AVUTIL, flags);
  465. PRINT_LIB_INFO(outstream, avcodec, AVCODEC, flags);
  466. PRINT_LIB_INFO(outstream, avformat, AVFORMAT, flags);
  467. PRINT_LIB_INFO(outstream, avdevice, AVDEVICE, flags);
  468. PRINT_LIB_INFO(outstream, avfilter, AVFILTER, flags);
  469. PRINT_LIB_INFO(outstream, swscale, SWSCALE, flags);
  470. PRINT_LIB_INFO(outstream, postproc, POSTPROC, flags);
  471. }
  472. void show_banner(void)
  473. {
  474. fprintf(stderr, "%s version " LIBAV_VERSION ", Copyright (c) %d-%d the Libav developers\n",
  475. program_name, program_birth_year, this_year);
  476. fprintf(stderr, " built on %s %s with %s %s\n",
  477. __DATE__, __TIME__, CC_TYPE, CC_VERSION);
  478. fprintf(stderr, " configuration: " LIBAV_CONFIGURATION "\n");
  479. print_all_libs_info(stderr, INDENT|SHOW_CONFIG);
  480. print_all_libs_info(stderr, INDENT|SHOW_VERSION);
  481. }
  482. void show_version(void) {
  483. printf("%s " LIBAV_VERSION "\n", program_name);
  484. print_all_libs_info(stdout, SHOW_VERSION);
  485. }
  486. void show_license(void)
  487. {
  488. printf(
  489. #if CONFIG_NONFREE
  490. "This version of %s has nonfree parts compiled in.\n"
  491. "Therefore it is not legally redistributable.\n",
  492. program_name
  493. #elif CONFIG_GPLV3
  494. "%s is free software; you can redistribute it and/or modify\n"
  495. "it under the terms of the GNU General Public License as published by\n"
  496. "the Free Software Foundation; either version 3 of the License, or\n"
  497. "(at your option) any later version.\n"
  498. "\n"
  499. "%s is distributed in the hope that it will be useful,\n"
  500. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  501. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  502. "GNU General Public License for more details.\n"
  503. "\n"
  504. "You should have received a copy of the GNU General Public License\n"
  505. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  506. program_name, program_name, program_name
  507. #elif CONFIG_GPL
  508. "%s is free software; you can redistribute it and/or modify\n"
  509. "it under the terms of the GNU General Public License as published by\n"
  510. "the Free Software Foundation; either version 2 of the License, or\n"
  511. "(at your option) any later version.\n"
  512. "\n"
  513. "%s is distributed in the hope that it will be useful,\n"
  514. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  515. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  516. "GNU General Public License for more details.\n"
  517. "\n"
  518. "You should have received a copy of the GNU General Public License\n"
  519. "along with %s; if not, write to the Free Software\n"
  520. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  521. program_name, program_name, program_name
  522. #elif CONFIG_LGPLV3
  523. "%s is free software; you can redistribute it and/or modify\n"
  524. "it under the terms of the GNU Lesser General Public License as published by\n"
  525. "the Free Software Foundation; either version 3 of the License, or\n"
  526. "(at your option) any later version.\n"
  527. "\n"
  528. "%s is distributed in the hope that it will be useful,\n"
  529. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  530. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  531. "GNU Lesser General Public License for more details.\n"
  532. "\n"
  533. "You should have received a copy of the GNU Lesser General Public License\n"
  534. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  535. program_name, program_name, program_name
  536. #else
  537. "%s is free software; you can redistribute it and/or\n"
  538. "modify it under the terms of the GNU Lesser General Public\n"
  539. "License as published by the Free Software Foundation; either\n"
  540. "version 2.1 of the License, or (at your option) any later version.\n"
  541. "\n"
  542. "%s is distributed in the hope that it will be useful,\n"
  543. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  544. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  545. "Lesser General Public License for more details.\n"
  546. "\n"
  547. "You should have received a copy of the GNU Lesser General Public\n"
  548. "License along with %s; if not, write to the Free Software\n"
  549. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  550. program_name, program_name, program_name
  551. #endif
  552. );
  553. }
  554. void show_formats(void)
  555. {
  556. AVInputFormat *ifmt=NULL;
  557. AVOutputFormat *ofmt=NULL;
  558. const char *last_name;
  559. printf(
  560. "File formats:\n"
  561. " D. = Demuxing supported\n"
  562. " .E = Muxing supported\n"
  563. " --\n");
  564. last_name= "000";
  565. for(;;){
  566. int decode=0;
  567. int encode=0;
  568. const char *name=NULL;
  569. const char *long_name=NULL;
  570. while((ofmt= av_oformat_next(ofmt))) {
  571. if((name == NULL || strcmp(ofmt->name, name)<0) &&
  572. strcmp(ofmt->name, last_name)>0){
  573. name= ofmt->name;
  574. long_name= ofmt->long_name;
  575. encode=1;
  576. }
  577. }
  578. while((ifmt= av_iformat_next(ifmt))) {
  579. if((name == NULL || strcmp(ifmt->name, name)<0) &&
  580. strcmp(ifmt->name, last_name)>0){
  581. name= ifmt->name;
  582. long_name= ifmt->long_name;
  583. encode=0;
  584. }
  585. if(name && strcmp(ifmt->name, name)==0)
  586. decode=1;
  587. }
  588. if(name==NULL)
  589. break;
  590. last_name= name;
  591. printf(
  592. " %s%s %-15s %s\n",
  593. decode ? "D":" ",
  594. encode ? "E":" ",
  595. name,
  596. long_name ? long_name:" ");
  597. }
  598. }
  599. void show_codecs(void)
  600. {
  601. AVCodec *p=NULL, *p2;
  602. const char *last_name;
  603. printf(
  604. "Codecs:\n"
  605. " D..... = Decoding supported\n"
  606. " .E.... = Encoding supported\n"
  607. " ..V... = Video codec\n"
  608. " ..A... = Audio codec\n"
  609. " ..S... = Subtitle codec\n"
  610. " ...S.. = Supports draw_horiz_band\n"
  611. " ....D. = Supports direct rendering method 1\n"
  612. " .....T = Supports weird frame truncation\n"
  613. " ------\n");
  614. last_name= "000";
  615. for(;;){
  616. int decode=0;
  617. int encode=0;
  618. int cap=0;
  619. const char *type_str;
  620. p2=NULL;
  621. while((p= av_codec_next(p))) {
  622. if((p2==NULL || strcmp(p->name, p2->name)<0) &&
  623. strcmp(p->name, last_name)>0){
  624. p2= p;
  625. decode= encode= cap=0;
  626. }
  627. if(p2 && strcmp(p->name, p2->name)==0){
  628. if(p->decode) decode=1;
  629. if(p->encode) encode=1;
  630. cap |= p->capabilities;
  631. }
  632. }
  633. if(p2==NULL)
  634. break;
  635. last_name= p2->name;
  636. switch(p2->type) {
  637. case AVMEDIA_TYPE_VIDEO:
  638. type_str = "V";
  639. break;
  640. case AVMEDIA_TYPE_AUDIO:
  641. type_str = "A";
  642. break;
  643. case AVMEDIA_TYPE_SUBTITLE:
  644. type_str = "S";
  645. break;
  646. default:
  647. type_str = "?";
  648. break;
  649. }
  650. printf(
  651. " %s%s%s%s%s%s %-15s %s",
  652. decode ? "D": (/*p2->decoder ? "d":*/" "),
  653. encode ? "E":" ",
  654. type_str,
  655. cap & CODEC_CAP_DRAW_HORIZ_BAND ? "S":" ",
  656. cap & CODEC_CAP_DR1 ? "D":" ",
  657. cap & CODEC_CAP_TRUNCATED ? "T":" ",
  658. p2->name,
  659. p2->long_name ? p2->long_name : "");
  660. /* if(p2->decoder && decode==0)
  661. printf(" use %s for decoding", p2->decoder->name);*/
  662. printf("\n");
  663. }
  664. printf("\n");
  665. printf(
  666. "Note, the names of encoders and decoders do not always match, so there are\n"
  667. "several cases where the above table shows encoder only or decoder only entries\n"
  668. "even though both encoding and decoding are supported. For example, the h263\n"
  669. "decoder corresponds to the h263 and h263p encoders, for file formats it is even\n"
  670. "worse.\n");
  671. }
  672. void show_bsfs(void)
  673. {
  674. AVBitStreamFilter *bsf=NULL;
  675. printf("Bitstream filters:\n");
  676. while((bsf = av_bitstream_filter_next(bsf)))
  677. printf("%s\n", bsf->name);
  678. printf("\n");
  679. }
  680. void show_protocols(void)
  681. {
  682. void *opaque = NULL;
  683. const char *name;
  684. printf("Supported file protocols:\n"
  685. "Input:\n");
  686. while ((name = avio_enum_protocols(&opaque, 0)))
  687. printf("%s\n", name);
  688. printf("Output:\n");
  689. while ((name = avio_enum_protocols(&opaque, 1)))
  690. printf("%s\n", name);
  691. }
  692. void show_filters(void)
  693. {
  694. AVFilter av_unused(**filter) = NULL;
  695. printf("Filters:\n");
  696. #if CONFIG_AVFILTER
  697. while ((filter = av_filter_next(filter)) && *filter)
  698. printf("%-16s %s\n", (*filter)->name, (*filter)->description);
  699. #endif
  700. }
  701. void show_pix_fmts(void)
  702. {
  703. enum PixelFormat pix_fmt;
  704. printf(
  705. "Pixel formats:\n"
  706. "I.... = Supported Input format for conversion\n"
  707. ".O... = Supported Output format for conversion\n"
  708. "..H.. = Hardware accelerated format\n"
  709. "...P. = Paletted format\n"
  710. "....B = Bitstream format\n"
  711. "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
  712. "-----\n");
  713. #if !CONFIG_SWSCALE
  714. # define sws_isSupportedInput(x) 0
  715. # define sws_isSupportedOutput(x) 0
  716. #endif
  717. for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) {
  718. const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
  719. printf("%c%c%c%c%c %-16s %d %2d\n",
  720. sws_isSupportedInput (pix_fmt) ? 'I' : '.',
  721. sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
  722. pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
  723. pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
  724. pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
  725. pix_desc->name,
  726. pix_desc->nb_components,
  727. av_get_bits_per_pixel(pix_desc));
  728. }
  729. }
  730. int read_yesno(void)
  731. {
  732. int c = getchar();
  733. int yesno = (toupper(c) == 'Y');
  734. while (c != '\n' && c != EOF)
  735. c = getchar();
  736. return yesno;
  737. }
  738. int read_file(const char *filename, char **bufptr, size_t *size)
  739. {
  740. FILE *f = fopen(filename, "rb");
  741. if (!f) {
  742. fprintf(stderr, "Cannot read file '%s': %s\n", filename, strerror(errno));
  743. return AVERROR(errno);
  744. }
  745. fseek(f, 0, SEEK_END);
  746. *size = ftell(f);
  747. fseek(f, 0, SEEK_SET);
  748. *bufptr = av_malloc(*size + 1);
  749. if (!*bufptr) {
  750. fprintf(stderr, "Could not allocate file buffer\n");
  751. fclose(f);
  752. return AVERROR(ENOMEM);
  753. }
  754. fread(*bufptr, 1, *size, f);
  755. (*bufptr)[*size++] = '\0';
  756. fclose(f);
  757. return 0;
  758. }
  759. void init_pts_correction(PtsCorrectionContext *ctx)
  760. {
  761. ctx->num_faulty_pts = ctx->num_faulty_dts = 0;
  762. ctx->last_pts = ctx->last_dts = INT64_MIN;
  763. }
  764. int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t reordered_pts, int64_t dts)
  765. {
  766. int64_t pts = AV_NOPTS_VALUE;
  767. if (dts != AV_NOPTS_VALUE) {
  768. ctx->num_faulty_dts += dts <= ctx->last_dts;
  769. ctx->last_dts = dts;
  770. }
  771. if (reordered_pts != AV_NOPTS_VALUE) {
  772. ctx->num_faulty_pts += reordered_pts <= ctx->last_pts;
  773. ctx->last_pts = reordered_pts;
  774. }
  775. if ((ctx->num_faulty_pts<=ctx->num_faulty_dts || dts == AV_NOPTS_VALUE)
  776. && reordered_pts != AV_NOPTS_VALUE)
  777. pts = reordered_pts;
  778. else
  779. pts = dts;
  780. return pts;
  781. }
  782. FILE *get_preset_file(char *filename, size_t filename_size,
  783. const char *preset_name, int is_path, const char *codec_name)
  784. {
  785. FILE *f = NULL;
  786. int i;
  787. const char *base[3]= { getenv("FFMPEG_DATADIR"),
  788. getenv("HOME"),
  789. FFMPEG_DATADIR,
  790. };
  791. if (is_path) {
  792. av_strlcpy(filename, preset_name, filename_size);
  793. f = fopen(filename, "r");
  794. } else {
  795. for (i = 0; i < 3 && !f; i++) {
  796. if (!base[i])
  797. continue;
  798. snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i], i != 1 ? "" : "/.ffmpeg", preset_name);
  799. f = fopen(filename, "r");
  800. if (!f && codec_name) {
  801. snprintf(filename, filename_size,
  802. "%s%s/%s-%s.ffpreset", base[i], i != 1 ? "" : "/.ffmpeg", codec_name, preset_name);
  803. f = fopen(filename, "r");
  804. }
  805. }
  806. }
  807. return f;
  808. }
  809. AVDictionary *filter_codec_opts(AVDictionary *opts, enum CodecID codec_id, int encoder)
  810. {
  811. AVDictionary *ret = NULL;
  812. AVDictionaryEntry *t = NULL;
  813. AVCodec *codec = encoder ? avcodec_find_encoder(codec_id) : avcodec_find_decoder(codec_id);
  814. int flags = encoder ? AV_OPT_FLAG_ENCODING_PARAM : AV_OPT_FLAG_DECODING_PARAM;
  815. char prefix = 0;
  816. if (!codec)
  817. return NULL;
  818. switch (codec->type) {
  819. case AVMEDIA_TYPE_VIDEO: prefix = 'v'; flags |= AV_OPT_FLAG_VIDEO_PARAM; break;
  820. case AVMEDIA_TYPE_AUDIO: prefix = 'a'; flags |= AV_OPT_FLAG_AUDIO_PARAM; break;
  821. case AVMEDIA_TYPE_SUBTITLE: prefix = 's'; flags |= AV_OPT_FLAG_SUBTITLE_PARAM; break;
  822. }
  823. while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
  824. if (av_opt_find(avcodec_opts[0], t->key, NULL, flags, 0) ||
  825. (codec && codec->priv_class && av_opt_find(&codec->priv_class, t->key, NULL, flags, 0)))
  826. av_dict_set(&ret, t->key, t->value, 0);
  827. else if (t->key[0] == prefix && av_opt_find(avcodec_opts[0], t->key+1, NULL, flags, 0))
  828. av_dict_set(&ret, t->key+1, t->value, 0);
  829. }
  830. return ret;
  831. }
  832. #if CONFIG_AVFILTER
  833. static int ffsink_init(AVFilterContext *ctx, const char *args, void *opaque)
  834. {
  835. FFSinkContext *priv = ctx->priv;
  836. if (!opaque)
  837. return AVERROR(EINVAL);
  838. *priv = *(FFSinkContext *)opaque;
  839. return 0;
  840. }
  841. static void null_end_frame(AVFilterLink *inlink) { }
  842. static int ffsink_query_formats(AVFilterContext *ctx)
  843. {
  844. FFSinkContext *priv = ctx->priv;
  845. enum PixelFormat pix_fmts[] = { priv->pix_fmt, PIX_FMT_NONE };
  846. avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
  847. return 0;
  848. }
  849. AVFilter ffsink = {
  850. .name = "ffsink",
  851. .priv_size = sizeof(FFSinkContext),
  852. .init = ffsink_init,
  853. .query_formats = ffsink_query_formats,
  854. .inputs = (AVFilterPad[]) {{ .name = "default",
  855. .type = AVMEDIA_TYPE_VIDEO,
  856. .end_frame = null_end_frame,
  857. .min_perms = AV_PERM_READ, },
  858. { .name = NULL }},
  859. .outputs = (AVFilterPad[]) {{ .name = NULL }},
  860. };
  861. int get_filtered_video_frame(AVFilterContext *ctx, AVFrame *frame,
  862. AVFilterBufferRef **picref_ptr, AVRational *tb)
  863. {
  864. int ret;
  865. AVFilterBufferRef *picref;
  866. if ((ret = avfilter_request_frame(ctx->inputs[0])) < 0)
  867. return ret;
  868. if (!(picref = ctx->inputs[0]->cur_buf))
  869. return AVERROR(ENOENT);
  870. *picref_ptr = picref;
  871. ctx->inputs[0]->cur_buf = NULL;
  872. *tb = ctx->inputs[0]->time_base;
  873. memcpy(frame->data, picref->data, sizeof(frame->data));
  874. memcpy(frame->linesize, picref->linesize, sizeof(frame->linesize));
  875. frame->interlaced_frame = picref->video->interlaced;
  876. frame->top_field_first = picref->video->top_field_first;
  877. frame->key_frame = picref->video->key_frame;
  878. frame->pict_type = picref->video->pict_type;
  879. return 1;
  880. }
  881. #endif /* CONFIG_AVFILTER */