cmdutils.c 28 KB

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