cmdutils.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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. struct SwsContext *sws_opts;
  49. AVDictionary *format_opts, *codec_opts;
  50. static const int this_year = 2011;
  51. void init_opts(void)
  52. {
  53. #if CONFIG_SWSCALE
  54. sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC, NULL, NULL, NULL);
  55. #endif
  56. }
  57. void uninit_opts(void)
  58. {
  59. #if CONFIG_SWSCALE
  60. sws_freeContext(sws_opts);
  61. sws_opts = NULL;
  62. #endif
  63. av_dict_free(&format_opts);
  64. av_dict_free(&codec_opts);
  65. }
  66. void log_callback_help(void* ptr, int level, const char* fmt, va_list vl)
  67. {
  68. vfprintf(stdout, fmt, vl);
  69. }
  70. double parse_number_or_die(const char *context, const char *numstr, int type, double min, double max)
  71. {
  72. char *tail;
  73. const char *error;
  74. double d = av_strtod(numstr, &tail);
  75. if (*tail)
  76. error= "Expected number for %s but found: %s\n";
  77. else if (d < min || d > max)
  78. error= "The value for %s was %s which is not within %f - %f\n";
  79. else if(type == OPT_INT64 && (int64_t)d != d)
  80. error= "Expected int64 for %s but found %s\n";
  81. else if (type == OPT_INT && (int)d != d)
  82. error= "Expected int for %s but found %s\n";
  83. else
  84. return d;
  85. av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
  86. exit_program(1);
  87. return 0;
  88. }
  89. int64_t parse_time_or_die(const char *context, const char *timestr, int is_duration)
  90. {
  91. int64_t us;
  92. if (av_parse_time(&us, timestr, is_duration) < 0) {
  93. av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
  94. is_duration ? "duration" : "date", context, timestr);
  95. exit_program(1);
  96. }
  97. return us;
  98. }
  99. void show_help_options(const OptionDef *options, const char *msg, int mask, int value)
  100. {
  101. const OptionDef *po;
  102. int first;
  103. first = 1;
  104. for(po = options; po->name != NULL; po++) {
  105. char buf[64];
  106. if ((po->flags & mask) == value) {
  107. if (first) {
  108. printf("%s", msg);
  109. first = 0;
  110. }
  111. av_strlcpy(buf, po->name, sizeof(buf));
  112. if (po->flags & HAS_ARG) {
  113. av_strlcat(buf, " ", sizeof(buf));
  114. av_strlcat(buf, po->argname, sizeof(buf));
  115. }
  116. printf("-%-17s %s\n", buf, po->help);
  117. }
  118. }
  119. }
  120. void show_help_children(const AVClass *class, int flags)
  121. {
  122. const AVClass *child = NULL;
  123. av_opt_show2(&class, NULL, flags, 0);
  124. printf("\n");
  125. while (child = av_opt_child_class_next(class, child))
  126. show_help_children(child, flags);
  127. }
  128. static const OptionDef* find_option(const OptionDef *po, const char *name){
  129. const char *p = strchr(name, ':');
  130. int len = p ? p - name : strlen(name);
  131. while (po->name != NULL) {
  132. if (!strncmp(name, po->name, len) && strlen(po->name) == len)
  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. int parse_option(void *optctx, const char *opt, const char *arg, const OptionDef *options)
  192. {
  193. const OptionDef *po;
  194. int bool_val = 1;
  195. int *dstcount;
  196. void *dst;
  197. po = find_option(options, opt);
  198. if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
  199. /* handle 'no' bool option */
  200. po = find_option(options, opt + 2);
  201. if (!(po->name && (po->flags & OPT_BOOL)))
  202. goto unknown_opt;
  203. bool_val = 0;
  204. }
  205. if (!po->name)
  206. po = find_option(options, "default");
  207. if (!po->name) {
  208. unknown_opt:
  209. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
  210. return AVERROR(EINVAL);
  211. }
  212. if (po->flags & HAS_ARG && !arg) {
  213. av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
  214. return AVERROR(EINVAL);
  215. }
  216. /* new-style options contain an offset into optctx, old-style address of
  217. * a global var*/
  218. dst = po->flags & (OPT_OFFSET|OPT_SPEC) ? (uint8_t*)optctx + po->u.off : po->u.dst_ptr;
  219. if (po->flags & OPT_SPEC) {
  220. SpecifierOpt **so = dst;
  221. char *p = strchr(opt, ':');
  222. dstcount = (int*)(so + 1);
  223. *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
  224. (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
  225. dst = &(*so)[*dstcount - 1].u;
  226. }
  227. if (po->flags & OPT_STRING) {
  228. char *str;
  229. str = av_strdup(arg);
  230. *(char**)dst = str;
  231. } else if (po->flags & OPT_BOOL) {
  232. *(int*)dst = bool_val;
  233. } else if (po->flags & OPT_INT) {
  234. *(int*)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
  235. } else if (po->flags & OPT_INT64) {
  236. *(int64_t*)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
  237. } else if (po->flags & OPT_TIME) {
  238. *(int64_t*)dst = parse_time_or_die(opt, arg, 1);
  239. } else if (po->flags & OPT_FLOAT) {
  240. *(float*)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
  241. } else if (po->flags & OPT_DOUBLE) {
  242. *(double*)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
  243. } else if (po->u.func_arg) {
  244. int ret = po->flags & OPT_FUNC2 ? po->u.func2_arg(optctx, opt, arg) :
  245. po->u.func_arg(opt, arg);
  246. if (ret < 0) {
  247. av_log(NULL, AV_LOG_ERROR, "Failed to set value '%s' for option '%s'\n", arg, opt);
  248. return ret;
  249. }
  250. }
  251. if (po->flags & OPT_EXIT)
  252. exit_program(0);
  253. return !!(po->flags & HAS_ARG);
  254. }
  255. void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
  256. void (* parse_arg_function)(void *, const char*))
  257. {
  258. const char *opt;
  259. int optindex, handleoptions = 1, ret;
  260. /* perform system-dependent conversions for arguments list */
  261. prepare_app_arguments(&argc, &argv);
  262. /* parse options */
  263. optindex = 1;
  264. while (optindex < argc) {
  265. opt = argv[optindex++];
  266. if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
  267. if (opt[1] == '-' && opt[2] == '\0') {
  268. handleoptions = 0;
  269. continue;
  270. }
  271. opt++;
  272. if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
  273. exit_program(1);
  274. optindex += ret;
  275. } else {
  276. if (parse_arg_function)
  277. parse_arg_function(optctx, opt);
  278. }
  279. }
  280. }
  281. /*
  282. * Return index of option opt in argv or 0 if not found.
  283. */
  284. static int locate_option(int argc, char **argv, const OptionDef *options, const char *optname)
  285. {
  286. const OptionDef *po;
  287. int i;
  288. for (i = 1; i < argc; i++) {
  289. const char *cur_opt = argv[i];
  290. if (*cur_opt++ != '-')
  291. continue;
  292. po = find_option(options, cur_opt);
  293. if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
  294. po = find_option(options, cur_opt + 2);
  295. if ((!po->name && !strcmp(cur_opt, optname)) ||
  296. (po->name && !strcmp(optname, po->name)))
  297. return i;
  298. if (!po || po->flags & HAS_ARG)
  299. i++;
  300. }
  301. return 0;
  302. }
  303. void parse_loglevel(int argc, char **argv, const OptionDef *options)
  304. {
  305. int idx = locate_option(argc, argv, options, "loglevel");
  306. if (!idx)
  307. idx = locate_option(argc, argv, options, "v");
  308. if (idx && argv[idx + 1])
  309. opt_loglevel("loglevel", argv[idx + 1]);
  310. }
  311. #define FLAGS(o) ((o)->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
  312. int opt_default(const char *opt, const char *arg)
  313. {
  314. const AVOption *oc, *of, *os;
  315. char opt_stripped[128];
  316. const char *p;
  317. const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class(), *sc;
  318. if (!(p = strchr(opt, ':')))
  319. p = opt + strlen(opt);
  320. av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
  321. if ((oc = av_opt_find(&cc, opt_stripped, NULL, 0, AV_OPT_SEARCH_CHILDREN|AV_OPT_SEARCH_FAKE_OBJ)) ||
  322. ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
  323. (oc = av_opt_find(&cc, opt+1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
  324. av_dict_set(&codec_opts, opt, arg, FLAGS(oc));
  325. if ((of = av_opt_find(&fc, opt, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
  326. av_dict_set(&format_opts, opt, arg, FLAGS(of));
  327. #if CONFIG_SWSCALE
  328. sc = sws_get_class();
  329. if ((os = av_opt_find(&sc, opt, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  330. // XXX we only support sws_flags, not arbitrary sws options
  331. int ret = av_opt_set(sws_opts, opt, arg, 0);
  332. if (ret < 0) {
  333. av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
  334. return ret;
  335. }
  336. }
  337. #endif
  338. if (oc || of || os)
  339. return 0;
  340. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
  341. return AVERROR_OPTION_NOT_FOUND;
  342. }
  343. int opt_loglevel(const char *opt, const char *arg)
  344. {
  345. const struct { const char *name; int level; } log_levels[] = {
  346. { "quiet" , AV_LOG_QUIET },
  347. { "panic" , AV_LOG_PANIC },
  348. { "fatal" , AV_LOG_FATAL },
  349. { "error" , AV_LOG_ERROR },
  350. { "warning", AV_LOG_WARNING },
  351. { "info" , AV_LOG_INFO },
  352. { "verbose", AV_LOG_VERBOSE },
  353. { "debug" , AV_LOG_DEBUG },
  354. };
  355. char *tail;
  356. int level;
  357. int i;
  358. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
  359. if (!strcmp(log_levels[i].name, arg)) {
  360. av_log_set_level(log_levels[i].level);
  361. return 0;
  362. }
  363. }
  364. level = strtol(arg, &tail, 10);
  365. if (*tail) {
  366. av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
  367. "Possible levels are numbers or:\n", arg);
  368. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
  369. av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
  370. exit_program(1);
  371. }
  372. av_log_set_level(level);
  373. return 0;
  374. }
  375. int opt_codec_debug(const char *opt, const char *arg)
  376. {
  377. av_log_set_level(AV_LOG_DEBUG);
  378. return opt_default(opt, arg);
  379. }
  380. int opt_timelimit(const char *opt, const char *arg)
  381. {
  382. #if HAVE_SETRLIMIT
  383. int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
  384. struct rlimit rl = { lim, lim + 1 };
  385. if (setrlimit(RLIMIT_CPU, &rl))
  386. perror("setrlimit");
  387. #else
  388. av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
  389. #endif
  390. return 0;
  391. }
  392. void print_error(const char *filename, int err)
  393. {
  394. char errbuf[128];
  395. const char *errbuf_ptr = errbuf;
  396. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  397. errbuf_ptr = strerror(AVUNERROR(err));
  398. av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
  399. }
  400. static int warned_cfg = 0;
  401. #define INDENT 1
  402. #define SHOW_VERSION 2
  403. #define SHOW_CONFIG 4
  404. #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
  405. if (CONFIG_##LIBNAME) { \
  406. const char *indent = flags & INDENT? " " : ""; \
  407. if (flags & SHOW_VERSION) { \
  408. unsigned int version = libname##_version(); \
  409. av_log(NULL, level, "%slib%-9s %2d.%3d.%2d / %2d.%3d.%2d\n",\
  410. indent, #libname, \
  411. LIB##LIBNAME##_VERSION_MAJOR, \
  412. LIB##LIBNAME##_VERSION_MINOR, \
  413. LIB##LIBNAME##_VERSION_MICRO, \
  414. version >> 16, version >> 8 & 0xff, version & 0xff); \
  415. } \
  416. if (flags & SHOW_CONFIG) { \
  417. const char *cfg = libname##_configuration(); \
  418. if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
  419. if (!warned_cfg) { \
  420. av_log(NULL, level, \
  421. "%sWARNING: library configuration mismatch\n", \
  422. indent); \
  423. warned_cfg = 1; \
  424. } \
  425. av_log(NULL, level, "%s%-11s configuration: %s\n", \
  426. indent, #libname, cfg); \
  427. } \
  428. } \
  429. } \
  430. static void print_all_libs_info(int flags, int level)
  431. {
  432. PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
  433. PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
  434. PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
  435. PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
  436. PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
  437. PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
  438. PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
  439. }
  440. void show_banner(void)
  441. {
  442. av_log(NULL, AV_LOG_INFO, "%s version " FFMPEG_VERSION ", Copyright (c) %d-%d the FFmpeg developers\n",
  443. program_name, program_birth_year, this_year);
  444. av_log(NULL, AV_LOG_INFO, " built on %s %s with %s %s\n",
  445. __DATE__, __TIME__, CC_TYPE, CC_VERSION);
  446. av_log(NULL, AV_LOG_INFO, " configuration: " FFMPEG_CONFIGURATION "\n");
  447. print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
  448. print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
  449. }
  450. int opt_version(const char *opt, const char *arg) {
  451. av_log_set_callback(log_callback_help);
  452. printf("%s " FFMPEG_VERSION "\n", program_name);
  453. print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
  454. return 0;
  455. }
  456. int opt_license(const char *opt, const char *arg)
  457. {
  458. printf(
  459. #if CONFIG_NONFREE
  460. "This version of %s has nonfree parts compiled in.\n"
  461. "Therefore it is not legally redistributable.\n",
  462. program_name
  463. #elif CONFIG_GPLV3
  464. "%s is free software; you can redistribute it and/or modify\n"
  465. "it under the terms of the GNU General Public License as published by\n"
  466. "the Free Software Foundation; either version 3 of the License, or\n"
  467. "(at your option) any later version.\n"
  468. "\n"
  469. "%s is distributed in the hope that it will be useful,\n"
  470. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  471. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  472. "GNU General Public License for more details.\n"
  473. "\n"
  474. "You should have received a copy of the GNU General Public License\n"
  475. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  476. program_name, program_name, program_name
  477. #elif CONFIG_GPL
  478. "%s is free software; you can redistribute it and/or modify\n"
  479. "it under the terms of the GNU General Public License as published by\n"
  480. "the Free Software Foundation; either version 2 of the License, or\n"
  481. "(at your option) any later version.\n"
  482. "\n"
  483. "%s is distributed in the hope that it will be useful,\n"
  484. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  485. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  486. "GNU General Public License for more details.\n"
  487. "\n"
  488. "You should have received a copy of the GNU General Public License\n"
  489. "along with %s; if not, write to the Free Software\n"
  490. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  491. program_name, program_name, program_name
  492. #elif CONFIG_LGPLV3
  493. "%s is free software; you can redistribute it and/or modify\n"
  494. "it under the terms of the GNU Lesser General Public License as published by\n"
  495. "the Free Software Foundation; either version 3 of the License, or\n"
  496. "(at your option) any later version.\n"
  497. "\n"
  498. "%s is distributed in the hope that it will be useful,\n"
  499. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  500. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  501. "GNU Lesser General Public License for more details.\n"
  502. "\n"
  503. "You should have received a copy of the GNU Lesser General Public License\n"
  504. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  505. program_name, program_name, program_name
  506. #else
  507. "%s is free software; you can redistribute it and/or\n"
  508. "modify it under the terms of the GNU Lesser General Public\n"
  509. "License as published by the Free Software Foundation; either\n"
  510. "version 2.1 of the License, or (at your option) any later version.\n"
  511. "\n"
  512. "%s is distributed in the hope that it will be useful,\n"
  513. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  514. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  515. "Lesser General Public License for more details.\n"
  516. "\n"
  517. "You should have received a copy of the GNU Lesser General Public\n"
  518. "License along with %s; if not, write to the Free Software\n"
  519. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  520. program_name, program_name, program_name
  521. #endif
  522. );
  523. return 0;
  524. }
  525. int opt_formats(const char *opt, const char *arg)
  526. {
  527. AVInputFormat *ifmt=NULL;
  528. AVOutputFormat *ofmt=NULL;
  529. const char *last_name;
  530. printf(
  531. "File formats:\n"
  532. " D. = Demuxing supported\n"
  533. " .E = Muxing supported\n"
  534. " --\n");
  535. last_name= "000";
  536. for(;;){
  537. int decode=0;
  538. int encode=0;
  539. const char *name=NULL;
  540. const char *long_name=NULL;
  541. while((ofmt= av_oformat_next(ofmt))) {
  542. if((name == NULL || strcmp(ofmt->name, name)<0) &&
  543. strcmp(ofmt->name, last_name)>0){
  544. name= ofmt->name;
  545. long_name= ofmt->long_name;
  546. encode=1;
  547. }
  548. }
  549. while((ifmt= av_iformat_next(ifmt))) {
  550. if((name == NULL || strcmp(ifmt->name, name)<0) &&
  551. strcmp(ifmt->name, last_name)>0){
  552. name= ifmt->name;
  553. long_name= ifmt->long_name;
  554. encode=0;
  555. }
  556. if(name && strcmp(ifmt->name, name)==0)
  557. decode=1;
  558. }
  559. if(name==NULL)
  560. break;
  561. last_name= name;
  562. printf(
  563. " %s%s %-15s %s\n",
  564. decode ? "D":" ",
  565. encode ? "E":" ",
  566. name,
  567. long_name ? long_name:" ");
  568. }
  569. return 0;
  570. }
  571. int opt_codecs(const char *opt, const char *arg)
  572. {
  573. AVCodec *p=NULL, *p2;
  574. const char *last_name;
  575. printf(
  576. "Codecs:\n"
  577. " D..... = Decoding supported\n"
  578. " .E.... = Encoding supported\n"
  579. " ..V... = Video codec\n"
  580. " ..A... = Audio codec\n"
  581. " ..S... = Subtitle codec\n"
  582. " ...S.. = Supports draw_horiz_band\n"
  583. " ....D. = Supports direct rendering method 1\n"
  584. " .....T = Supports weird frame truncation\n"
  585. " ------\n");
  586. last_name= "000";
  587. for(;;){
  588. int decode=0;
  589. int encode=0;
  590. int cap=0;
  591. const char *type_str;
  592. p2=NULL;
  593. while((p= av_codec_next(p))) {
  594. if((p2==NULL || strcmp(p->name, p2->name)<0) &&
  595. strcmp(p->name, last_name)>0){
  596. p2= p;
  597. decode= encode= cap=0;
  598. }
  599. if(p2 && strcmp(p->name, p2->name)==0){
  600. if(p->decode) decode=1;
  601. if(p->encode) encode=1;
  602. cap |= p->capabilities;
  603. }
  604. }
  605. if(p2==NULL)
  606. break;
  607. last_name= p2->name;
  608. switch(p2->type) {
  609. case AVMEDIA_TYPE_VIDEO:
  610. type_str = "V";
  611. break;
  612. case AVMEDIA_TYPE_AUDIO:
  613. type_str = "A";
  614. break;
  615. case AVMEDIA_TYPE_SUBTITLE:
  616. type_str = "S";
  617. break;
  618. default:
  619. type_str = "?";
  620. break;
  621. }
  622. printf(
  623. " %s%s%s%s%s%s %-15s %s",
  624. decode ? "D": (/*p2->decoder ? "d":*/" "),
  625. encode ? "E":" ",
  626. type_str,
  627. cap & CODEC_CAP_DRAW_HORIZ_BAND ? "S":" ",
  628. cap & CODEC_CAP_DR1 ? "D":" ",
  629. cap & CODEC_CAP_TRUNCATED ? "T":" ",
  630. p2->name,
  631. p2->long_name ? p2->long_name : "");
  632. /* if(p2->decoder && decode==0)
  633. printf(" use %s for decoding", p2->decoder->name);*/
  634. printf("\n");
  635. }
  636. printf("\n");
  637. printf(
  638. "Note, the names of encoders and decoders do not always match, so there are\n"
  639. "several cases where the above table shows encoder only or decoder only entries\n"
  640. "even though both encoding and decoding are supported. For example, the h263\n"
  641. "decoder corresponds to the h263 and h263p encoders, for file formats it is even\n"
  642. "worse.\n");
  643. return 0;
  644. }
  645. int opt_bsfs(const char *opt, const char *arg)
  646. {
  647. AVBitStreamFilter *bsf=NULL;
  648. printf("Bitstream filters:\n");
  649. while((bsf = av_bitstream_filter_next(bsf)))
  650. printf("%s\n", bsf->name);
  651. printf("\n");
  652. return 0;
  653. }
  654. int opt_protocols(const char *opt, const char *arg)
  655. {
  656. URLProtocol *up=NULL;
  657. printf("Supported file protocols:\n"
  658. "I.. = Input supported\n"
  659. ".O. = Output supported\n"
  660. "..S = Seek supported\n"
  661. "FLAGS NAME\n"
  662. "----- \n");
  663. while((up = av_protocol_next(up)))
  664. printf("%c%c%c %s\n",
  665. up->url_read ? 'I' : '.',
  666. up->url_write ? 'O' : '.',
  667. up->url_seek ? 'S' : '.',
  668. up->name);
  669. return 0;
  670. }
  671. int opt_filters(const char *opt, const char *arg)
  672. {
  673. AVFilter av_unused(**filter) = NULL;
  674. printf("Filters:\n");
  675. #if CONFIG_AVFILTER
  676. while ((filter = av_filter_next(filter)) && *filter)
  677. printf("%-16s %s\n", (*filter)->name, (*filter)->description);
  678. #endif
  679. return 0;
  680. }
  681. int opt_pix_fmts(const char *opt, const char *arg)
  682. {
  683. enum PixelFormat pix_fmt;
  684. printf(
  685. "Pixel formats:\n"
  686. "I.... = Supported Input format for conversion\n"
  687. ".O... = Supported Output format for conversion\n"
  688. "..H.. = Hardware accelerated format\n"
  689. "...P. = Paletted format\n"
  690. "....B = Bitstream format\n"
  691. "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
  692. "-----\n");
  693. #if !CONFIG_SWSCALE
  694. # define sws_isSupportedInput(x) 0
  695. # define sws_isSupportedOutput(x) 0
  696. #endif
  697. for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) {
  698. const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
  699. printf("%c%c%c%c%c %-16s %d %2d\n",
  700. sws_isSupportedInput (pix_fmt) ? 'I' : '.',
  701. sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
  702. pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
  703. pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
  704. pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
  705. pix_desc->name,
  706. pix_desc->nb_components,
  707. av_get_bits_per_pixel(pix_desc));
  708. }
  709. return 0;
  710. }
  711. int show_sample_fmts(const char *opt, const char *arg)
  712. {
  713. int i;
  714. char fmt_str[128];
  715. for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
  716. printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
  717. return 0;
  718. }
  719. int read_yesno(void)
  720. {
  721. int c = getchar();
  722. int yesno = (toupper(c) == 'Y');
  723. while (c != '\n' && c != EOF)
  724. c = getchar();
  725. return yesno;
  726. }
  727. int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
  728. {
  729. int ret;
  730. FILE *f = fopen(filename, "rb");
  731. if (!f) {
  732. av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, strerror(errno));
  733. return AVERROR(errno);
  734. }
  735. fseek(f, 0, SEEK_END);
  736. *size = ftell(f);
  737. fseek(f, 0, SEEK_SET);
  738. *bufptr = av_malloc(*size + 1);
  739. if (!*bufptr) {
  740. av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
  741. fclose(f);
  742. return AVERROR(ENOMEM);
  743. }
  744. ret = fread(*bufptr, 1, *size, f);
  745. if (ret < *size) {
  746. av_free(*bufptr);
  747. if (ferror(f)) {
  748. av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
  749. filename, strerror(errno));
  750. ret = AVERROR(errno);
  751. } else
  752. ret = AVERROR_EOF;
  753. } else {
  754. ret = 0;
  755. (*bufptr)[*size++] = '\0';
  756. }
  757. fclose(f);
  758. return ret;
  759. }
  760. FILE *get_preset_file(char *filename, size_t filename_size,
  761. const char *preset_name, int is_path, const char *codec_name)
  762. {
  763. FILE *f = NULL;
  764. int i;
  765. const char *base[3]= { getenv("FFMPEG_DATADIR"),
  766. getenv("HOME"),
  767. FFMPEG_DATADIR,
  768. };
  769. if (is_path) {
  770. av_strlcpy(filename, preset_name, filename_size);
  771. f = fopen(filename, "r");
  772. } else {
  773. #ifdef _WIN32
  774. char datadir[MAX_PATH], *ls;
  775. base[2] = NULL;
  776. if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
  777. {
  778. for (ls = datadir; ls < datadir + strlen(datadir); ls++)
  779. if (*ls == '\\') *ls = '/';
  780. if (ls = strrchr(datadir, '/'))
  781. {
  782. *ls = 0;
  783. strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
  784. base[2] = datadir;
  785. }
  786. }
  787. #endif
  788. for (i = 0; i < 3 && !f; i++) {
  789. if (!base[i])
  790. continue;
  791. snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i], i != 1 ? "" : "/.ffmpeg", preset_name);
  792. f = fopen(filename, "r");
  793. if (!f && codec_name) {
  794. snprintf(filename, filename_size,
  795. "%s%s/%s-%s.ffpreset", base[i], i != 1 ? "" : "/.ffmpeg", codec_name, preset_name);
  796. f = fopen(filename, "r");
  797. }
  798. }
  799. }
  800. return f;
  801. }
  802. int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
  803. {
  804. if (*spec <= '9' && *spec >= '0') /* opt:index */
  805. return strtol(spec, NULL, 0) == st->index;
  806. else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' || *spec == 't') { /* opt:[vasdt] */
  807. enum AVMediaType type;
  808. switch (*spec++) {
  809. case 'v': type = AVMEDIA_TYPE_VIDEO; break;
  810. case 'a': type = AVMEDIA_TYPE_AUDIO; break;
  811. case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
  812. case 'd': type = AVMEDIA_TYPE_DATA; break;
  813. case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
  814. default: abort(); // never reached, silence warning
  815. }
  816. if (type != st->codec->codec_type)
  817. return 0;
  818. if (*spec++ == ':') { /* possibly followed by :index */
  819. int i, index = strtol(spec, NULL, 0);
  820. for (i = 0; i < s->nb_streams; i++)
  821. if (s->streams[i]->codec->codec_type == type && index-- == 0)
  822. return i == st->index;
  823. return 0;
  824. }
  825. return 1;
  826. } else if (*spec == 'p' && *(spec + 1) == ':') {
  827. int prog_id, i, j;
  828. char *endptr;
  829. spec += 2;
  830. prog_id = strtol(spec, &endptr, 0);
  831. for (i = 0; i < s->nb_programs; i++) {
  832. if (s->programs[i]->id != prog_id)
  833. continue;
  834. if (*endptr++ == ':') {
  835. int stream_idx = strtol(endptr, NULL, 0);
  836. return (stream_idx >= 0 && stream_idx < s->programs[i]->nb_stream_indexes &&
  837. st->index == s->programs[i]->stream_index[stream_idx]);
  838. }
  839. for (j = 0; j < s->programs[i]->nb_stream_indexes; j++)
  840. if (st->index == s->programs[i]->stream_index[j])
  841. return 1;
  842. }
  843. return 0;
  844. } else if (!*spec) /* empty specifier, matches everything */
  845. return 1;
  846. av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
  847. return AVERROR(EINVAL);
  848. }
  849. AVDictionary *filter_codec_opts(AVDictionary *opts, enum CodecID codec_id, AVFormatContext *s, AVStream *st)
  850. {
  851. AVDictionary *ret = NULL;
  852. AVDictionaryEntry *t = NULL;
  853. AVCodec *codec = s->oformat ? avcodec_find_encoder(codec_id) : avcodec_find_decoder(codec_id);
  854. int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM : AV_OPT_FLAG_DECODING_PARAM;
  855. char prefix = 0;
  856. const AVClass *cc = avcodec_get_class();
  857. if (!codec)
  858. return NULL;
  859. switch (codec->type) {
  860. case AVMEDIA_TYPE_VIDEO: prefix = 'v'; flags |= AV_OPT_FLAG_VIDEO_PARAM; break;
  861. case AVMEDIA_TYPE_AUDIO: prefix = 'a'; flags |= AV_OPT_FLAG_AUDIO_PARAM; break;
  862. case AVMEDIA_TYPE_SUBTITLE: prefix = 's'; flags |= AV_OPT_FLAG_SUBTITLE_PARAM; break;
  863. }
  864. while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
  865. char *p = strchr(t->key, ':');
  866. /* check stream specification in opt name */
  867. if (p)
  868. switch (check_stream_specifier(s, st, p + 1)) {
  869. case 1: *p = 0; break;
  870. case 0: continue;
  871. default: return NULL;
  872. }
  873. if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
  874. (codec && codec->priv_class && av_opt_find(&codec->priv_class, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ)))
  875. av_dict_set(&ret, t->key, t->value, 0);
  876. else if (t->key[0] == prefix && av_opt_find(&cc, t->key+1, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ))
  877. av_dict_set(&ret, t->key+1, t->value, 0);
  878. if (p)
  879. *p = ':';
  880. }
  881. return ret;
  882. }
  883. AVDictionary **setup_find_stream_info_opts(AVFormatContext *s, AVDictionary *codec_opts)
  884. {
  885. int i;
  886. AVDictionary **opts;
  887. if (!s->nb_streams)
  888. return NULL;
  889. opts = av_mallocz(s->nb_streams * sizeof(*opts));
  890. if (!opts) {
  891. av_log(NULL, AV_LOG_ERROR, "Could not alloc memory for stream options.\n");
  892. return NULL;
  893. }
  894. for (i = 0; i < s->nb_streams; i++)
  895. opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id, s, s->streams[i]);
  896. return opts;
  897. }
  898. void *grow_array(void *array, int elem_size, int *size, int new_size)
  899. {
  900. if (new_size >= INT_MAX / elem_size) {
  901. av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
  902. exit_program(1);
  903. }
  904. if (*size < new_size) {
  905. uint8_t *tmp = av_realloc(array, new_size*elem_size);
  906. if (!tmp) {
  907. av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
  908. exit_program(1);
  909. }
  910. memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
  911. *size = new_size;
  912. return tmp;
  913. }
  914. return array;
  915. }