cmdutils.c 51 KB

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