cmdutils.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274
  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 <stdint.h>
  23. #include <stdlib.h>
  24. #include <errno.h>
  25. #include <math.h>
  26. /* Include only the enabled headers since some compilers (namely, Sun
  27. Studio) will not omit unused inline functions and create undefined
  28. references to libraries that are not being built. */
  29. #include "config.h"
  30. #include "compat/va_copy.h"
  31. #include "libavformat/avformat.h"
  32. #include "libavfilter/avfilter.h"
  33. #include "libavdevice/avdevice.h"
  34. #include "libavresample/avresample.h"
  35. #include "libswscale/swscale.h"
  36. #include "libswresample/swresample.h"
  37. #include "libpostproc/postprocess.h"
  38. #include "libavutil/avassert.h"
  39. #include "libavutil/avstring.h"
  40. #include "libavutil/bprint.h"
  41. #include "libavutil/display.h"
  42. #include "libavutil/mathematics.h"
  43. #include "libavutil/imgutils.h"
  44. #include "libavutil/libm.h"
  45. #include "libavutil/parseutils.h"
  46. #include "libavutil/pixdesc.h"
  47. #include "libavutil/eval.h"
  48. #include "libavutil/dict.h"
  49. #include "libavutil/opt.h"
  50. #include "libavutil/cpu.h"
  51. #include "libavutil/ffversion.h"
  52. #include "libavutil/version.h"
  53. #include "cmdutils.h"
  54. #if CONFIG_NETWORK
  55. #include "libavformat/network.h"
  56. #endif
  57. #if HAVE_SYS_RESOURCE_H
  58. #include <sys/time.h>
  59. #include <sys/resource.h>
  60. #endif
  61. #ifdef _WIN32
  62. #include <windows.h>
  63. #endif
  64. static int init_report(const char *env);
  65. AVDictionary *sws_dict;
  66. AVDictionary *swr_opts;
  67. AVDictionary *format_opts, *codec_opts, *resample_opts;
  68. static FILE *report_file;
  69. static int report_file_level = AV_LOG_DEBUG;
  70. int hide_banner = 0;
  71. enum show_muxdemuxers {
  72. SHOW_DEFAULT,
  73. SHOW_DEMUXERS,
  74. SHOW_MUXERS,
  75. };
  76. void init_opts(void)
  77. {
  78. av_dict_set(&sws_dict, "flags", "bicubic", 0);
  79. }
  80. void uninit_opts(void)
  81. {
  82. av_dict_free(&swr_opts);
  83. av_dict_free(&sws_dict);
  84. av_dict_free(&format_opts);
  85. av_dict_free(&codec_opts);
  86. av_dict_free(&resample_opts);
  87. }
  88. void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
  89. {
  90. vfprintf(stdout, fmt, vl);
  91. }
  92. static void log_callback_report(void *ptr, int level, const char *fmt, va_list vl)
  93. {
  94. va_list vl2;
  95. char line[1024];
  96. static int print_prefix = 1;
  97. va_copy(vl2, vl);
  98. av_log_default_callback(ptr, level, fmt, vl);
  99. av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
  100. va_end(vl2);
  101. if (report_file_level >= level) {
  102. fputs(line, report_file);
  103. fflush(report_file);
  104. }
  105. }
  106. void init_dynload(void)
  107. {
  108. #ifdef _WIN32
  109. /* Calling SetDllDirectory with the empty string (but not NULL) removes the
  110. * current working directory from the DLL search path as a security pre-caution. */
  111. SetDllDirectory("");
  112. #endif
  113. }
  114. static void (*program_exit)(int ret);
  115. void register_exit(void (*cb)(int ret))
  116. {
  117. program_exit = cb;
  118. }
  119. void exit_program(int ret)
  120. {
  121. if (program_exit)
  122. program_exit(ret);
  123. exit(ret);
  124. }
  125. double parse_number_or_die(const char *context, const char *numstr, int type,
  126. double min, double max)
  127. {
  128. char *tail;
  129. const char *error;
  130. double d = av_strtod(numstr, &tail);
  131. if (*tail)
  132. error = "Expected number for %s but found: %s\n";
  133. else if (d < min || d > max)
  134. error = "The value for %s was %s which is not within %f - %f\n";
  135. else if (type == OPT_INT64 && (int64_t)d != d)
  136. error = "Expected int64 for %s but found %s\n";
  137. else if (type == OPT_INT && (int)d != d)
  138. error = "Expected int for %s but found %s\n";
  139. else
  140. return d;
  141. av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
  142. exit_program(1);
  143. return 0;
  144. }
  145. int64_t parse_time_or_die(const char *context, const char *timestr,
  146. int is_duration)
  147. {
  148. int64_t us;
  149. if (av_parse_time(&us, timestr, is_duration) < 0) {
  150. av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
  151. is_duration ? "duration" : "date", context, timestr);
  152. exit_program(1);
  153. }
  154. return us;
  155. }
  156. void show_help_options(const OptionDef *options, const char *msg, int req_flags,
  157. int rej_flags, int alt_flags)
  158. {
  159. const OptionDef *po;
  160. int first;
  161. first = 1;
  162. for (po = options; po->name; po++) {
  163. char buf[64];
  164. if (((po->flags & req_flags) != req_flags) ||
  165. (alt_flags && !(po->flags & alt_flags)) ||
  166. (po->flags & rej_flags))
  167. continue;
  168. if (first) {
  169. printf("%s\n", msg);
  170. first = 0;
  171. }
  172. av_strlcpy(buf, po->name, sizeof(buf));
  173. if (po->argname) {
  174. av_strlcat(buf, " ", sizeof(buf));
  175. av_strlcat(buf, po->argname, sizeof(buf));
  176. }
  177. printf("-%-17s %s\n", buf, po->help);
  178. }
  179. printf("\n");
  180. }
  181. void show_help_children(const AVClass *class, int flags)
  182. {
  183. const AVClass *child = NULL;
  184. if (class->option) {
  185. av_opt_show2(&class, NULL, flags, 0);
  186. printf("\n");
  187. }
  188. while (child = av_opt_child_class_next(class, child))
  189. show_help_children(child, flags);
  190. }
  191. static const OptionDef *find_option(const OptionDef *po, const char *name)
  192. {
  193. const char *p = strchr(name, ':');
  194. int len = p ? p - name : strlen(name);
  195. while (po->name) {
  196. if (!strncmp(name, po->name, len) && strlen(po->name) == len)
  197. break;
  198. po++;
  199. }
  200. return po;
  201. }
  202. /* _WIN32 means using the windows libc - cygwin doesn't define that
  203. * by default. HAVE_COMMANDLINETOARGVW is true on cygwin, while
  204. * it doesn't provide the actual command line via GetCommandLineW(). */
  205. #if HAVE_COMMANDLINETOARGVW && defined(_WIN32)
  206. #include <shellapi.h>
  207. /* Will be leaked on exit */
  208. static char** win32_argv_utf8 = NULL;
  209. static int win32_argc = 0;
  210. /**
  211. * Prepare command line arguments for executable.
  212. * For Windows - perform wide-char to UTF-8 conversion.
  213. * Input arguments should be main() function arguments.
  214. * @param argc_ptr Arguments number (including executable)
  215. * @param argv_ptr Arguments list.
  216. */
  217. static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
  218. {
  219. char *argstr_flat;
  220. wchar_t **argv_w;
  221. int i, buffsize = 0, offset = 0;
  222. if (win32_argv_utf8) {
  223. *argc_ptr = win32_argc;
  224. *argv_ptr = win32_argv_utf8;
  225. return;
  226. }
  227. win32_argc = 0;
  228. argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
  229. if (win32_argc <= 0 || !argv_w)
  230. return;
  231. /* determine the UTF-8 buffer size (including NULL-termination symbols) */
  232. for (i = 0; i < win32_argc; i++)
  233. buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
  234. NULL, 0, NULL, NULL);
  235. win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
  236. argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
  237. if (!win32_argv_utf8) {
  238. LocalFree(argv_w);
  239. return;
  240. }
  241. for (i = 0; i < win32_argc; i++) {
  242. win32_argv_utf8[i] = &argstr_flat[offset];
  243. offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
  244. &argstr_flat[offset],
  245. buffsize - offset, NULL, NULL);
  246. }
  247. win32_argv_utf8[i] = NULL;
  248. LocalFree(argv_w);
  249. *argc_ptr = win32_argc;
  250. *argv_ptr = win32_argv_utf8;
  251. }
  252. #else
  253. static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
  254. {
  255. /* nothing to do */
  256. }
  257. #endif /* HAVE_COMMANDLINETOARGVW */
  258. static int write_option(void *optctx, const OptionDef *po, const char *opt,
  259. const char *arg)
  260. {
  261. /* new-style options contain an offset into optctx, old-style address of
  262. * a global var*/
  263. void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ?
  264. (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
  265. int *dstcount;
  266. if (po->flags & OPT_SPEC) {
  267. SpecifierOpt **so = dst;
  268. char *p = strchr(opt, ':');
  269. char *str;
  270. dstcount = (int *)(so + 1);
  271. *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
  272. str = av_strdup(p ? p + 1 : "");
  273. if (!str)
  274. return AVERROR(ENOMEM);
  275. (*so)[*dstcount - 1].specifier = str;
  276. dst = &(*so)[*dstcount - 1].u;
  277. }
  278. if (po->flags & OPT_STRING) {
  279. char *str;
  280. str = av_strdup(arg);
  281. av_freep(dst);
  282. if (!str)
  283. return AVERROR(ENOMEM);
  284. *(char **)dst = str;
  285. } else if (po->flags & OPT_BOOL || po->flags & OPT_INT) {
  286. *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
  287. } else if (po->flags & OPT_INT64) {
  288. *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
  289. } else if (po->flags & OPT_TIME) {
  290. *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
  291. } else if (po->flags & OPT_FLOAT) {
  292. *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
  293. } else if (po->flags & OPT_DOUBLE) {
  294. *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
  295. } else if (po->u.func_arg) {
  296. int ret = po->u.func_arg(optctx, opt, arg);
  297. if (ret < 0) {
  298. av_log(NULL, AV_LOG_ERROR,
  299. "Failed to set value '%s' for option '%s': %s\n",
  300. arg, opt, av_err2str(ret));
  301. return ret;
  302. }
  303. }
  304. if (po->flags & OPT_EXIT)
  305. exit_program(0);
  306. return 0;
  307. }
  308. int parse_option(void *optctx, const char *opt, const char *arg,
  309. const OptionDef *options)
  310. {
  311. const OptionDef *po;
  312. int ret;
  313. po = find_option(options, opt);
  314. if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
  315. /* handle 'no' bool option */
  316. po = find_option(options, opt + 2);
  317. if ((po->name && (po->flags & OPT_BOOL)))
  318. arg = "0";
  319. } else if (po->flags & OPT_BOOL)
  320. arg = "1";
  321. if (!po->name)
  322. po = find_option(options, "default");
  323. if (!po->name) {
  324. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
  325. return AVERROR(EINVAL);
  326. }
  327. if (po->flags & HAS_ARG && !arg) {
  328. av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
  329. return AVERROR(EINVAL);
  330. }
  331. ret = write_option(optctx, po, opt, arg);
  332. if (ret < 0)
  333. return ret;
  334. return !!(po->flags & HAS_ARG);
  335. }
  336. void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
  337. void (*parse_arg_function)(void *, const char*))
  338. {
  339. const char *opt;
  340. int optindex, handleoptions = 1, ret;
  341. /* perform system-dependent conversions for arguments list */
  342. prepare_app_arguments(&argc, &argv);
  343. /* parse options */
  344. optindex = 1;
  345. while (optindex < argc) {
  346. opt = argv[optindex++];
  347. if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
  348. if (opt[1] == '-' && opt[2] == '\0') {
  349. handleoptions = 0;
  350. continue;
  351. }
  352. opt++;
  353. if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
  354. exit_program(1);
  355. optindex += ret;
  356. } else {
  357. if (parse_arg_function)
  358. parse_arg_function(optctx, opt);
  359. }
  360. }
  361. }
  362. int parse_optgroup(void *optctx, OptionGroup *g)
  363. {
  364. int i, ret;
  365. av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
  366. g->group_def->name, g->arg);
  367. for (i = 0; i < g->nb_opts; i++) {
  368. Option *o = &g->opts[i];
  369. if (g->group_def->flags &&
  370. !(g->group_def->flags & o->opt->flags)) {
  371. av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
  372. "%s %s -- you are trying to apply an input option to an "
  373. "output file or vice versa. Move this option before the "
  374. "file it belongs to.\n", o->key, o->opt->help,
  375. g->group_def->name, g->arg);
  376. return AVERROR(EINVAL);
  377. }
  378. av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
  379. o->key, o->opt->help, o->val);
  380. ret = write_option(optctx, o->opt, o->key, o->val);
  381. if (ret < 0)
  382. return ret;
  383. }
  384. av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
  385. return 0;
  386. }
  387. int locate_option(int argc, char **argv, const OptionDef *options,
  388. const char *optname)
  389. {
  390. const OptionDef *po;
  391. int i;
  392. for (i = 1; i < argc; i++) {
  393. const char *cur_opt = argv[i];
  394. if (*cur_opt++ != '-')
  395. continue;
  396. po = find_option(options, cur_opt);
  397. if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
  398. po = find_option(options, cur_opt + 2);
  399. if ((!po->name && !strcmp(cur_opt, optname)) ||
  400. (po->name && !strcmp(optname, po->name)))
  401. return i;
  402. if (!po->name || po->flags & HAS_ARG)
  403. i++;
  404. }
  405. return 0;
  406. }
  407. static void dump_argument(const char *a)
  408. {
  409. const unsigned char *p;
  410. for (p = a; *p; p++)
  411. if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
  412. *p == '_' || (*p >= 'a' && *p <= 'z')))
  413. break;
  414. if (!*p) {
  415. fputs(a, report_file);
  416. return;
  417. }
  418. fputc('"', report_file);
  419. for (p = a; *p; p++) {
  420. if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
  421. fprintf(report_file, "\\%c", *p);
  422. else if (*p < ' ' || *p > '~')
  423. fprintf(report_file, "\\x%02x", *p);
  424. else
  425. fputc(*p, report_file);
  426. }
  427. fputc('"', report_file);
  428. }
  429. static void check_options(const OptionDef *po)
  430. {
  431. while (po->name) {
  432. if (po->flags & OPT_PERFILE)
  433. av_assert0(po->flags & (OPT_INPUT | OPT_OUTPUT));
  434. po++;
  435. }
  436. }
  437. void parse_loglevel(int argc, char **argv, const OptionDef *options)
  438. {
  439. int idx = locate_option(argc, argv, options, "loglevel");
  440. const char *env;
  441. check_options(options);
  442. if (!idx)
  443. idx = locate_option(argc, argv, options, "v");
  444. if (idx && argv[idx + 1])
  445. opt_loglevel(NULL, "loglevel", argv[idx + 1]);
  446. idx = locate_option(argc, argv, options, "report");
  447. if ((env = getenv("FFREPORT")) || idx) {
  448. init_report(env);
  449. if (report_file) {
  450. int i;
  451. fprintf(report_file, "Command line:\n");
  452. for (i = 0; i < argc; i++) {
  453. dump_argument(argv[i]);
  454. fputc(i < argc - 1 ? ' ' : '\n', report_file);
  455. }
  456. fflush(report_file);
  457. }
  458. }
  459. idx = locate_option(argc, argv, options, "hide_banner");
  460. if (idx)
  461. hide_banner = 1;
  462. }
  463. static const AVOption *opt_find(void *obj, const char *name, const char *unit,
  464. int opt_flags, int search_flags)
  465. {
  466. const AVOption *o = av_opt_find(obj, name, unit, opt_flags, search_flags);
  467. if(o && !o->flags)
  468. return NULL;
  469. return o;
  470. }
  471. #define FLAGS (o->type == AV_OPT_TYPE_FLAGS && (arg[0]=='-' || arg[0]=='+')) ? AV_DICT_APPEND : 0
  472. int opt_default(void *optctx, const char *opt, const char *arg)
  473. {
  474. const AVOption *o;
  475. int consumed = 0;
  476. char opt_stripped[128];
  477. const char *p;
  478. const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
  479. #if CONFIG_AVRESAMPLE
  480. const AVClass *rc = avresample_get_class();
  481. #endif
  482. #if CONFIG_SWSCALE
  483. const AVClass *sc = sws_get_class();
  484. #endif
  485. #if CONFIG_SWRESAMPLE
  486. const AVClass *swr_class = swr_get_class();
  487. #endif
  488. if (!strcmp(opt, "debug") || !strcmp(opt, "fdebug"))
  489. av_log_set_level(AV_LOG_DEBUG);
  490. if (!(p = strchr(opt, ':')))
  491. p = opt + strlen(opt);
  492. av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
  493. if ((o = opt_find(&cc, opt_stripped, NULL, 0,
  494. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
  495. ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
  496. (o = opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) {
  497. av_dict_set(&codec_opts, opt, arg, FLAGS);
  498. consumed = 1;
  499. }
  500. if ((o = opt_find(&fc, opt, NULL, 0,
  501. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  502. av_dict_set(&format_opts, opt, arg, FLAGS);
  503. if (consumed)
  504. av_log(NULL, AV_LOG_VERBOSE, "Routing option %s to both codec and muxer layer\n", opt);
  505. consumed = 1;
  506. }
  507. #if CONFIG_SWSCALE
  508. if (!consumed && (o = opt_find(&sc, opt, NULL, 0,
  509. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  510. struct SwsContext *sws = sws_alloc_context();
  511. int ret = av_opt_set(sws, opt, arg, 0);
  512. sws_freeContext(sws);
  513. if (!strcmp(opt, "srcw") || !strcmp(opt, "srch") ||
  514. !strcmp(opt, "dstw") || !strcmp(opt, "dsth") ||
  515. !strcmp(opt, "src_format") || !strcmp(opt, "dst_format")) {
  516. av_log(NULL, AV_LOG_ERROR, "Directly using swscale dimensions/format options is not supported, please use the -s or -pix_fmt options\n");
  517. return AVERROR(EINVAL);
  518. }
  519. if (ret < 0) {
  520. av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
  521. return ret;
  522. }
  523. av_dict_set(&sws_dict, opt, arg, FLAGS);
  524. consumed = 1;
  525. }
  526. #else
  527. if (!consumed && !strcmp(opt, "sws_flags")) {
  528. av_log(NULL, AV_LOG_WARNING, "Ignoring %s %s, due to disabled swscale\n", opt, arg);
  529. consumed = 1;
  530. }
  531. #endif
  532. #if CONFIG_SWRESAMPLE
  533. if (!consumed && (o=opt_find(&swr_class, opt, NULL, 0,
  534. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  535. struct SwrContext *swr = swr_alloc();
  536. int ret = av_opt_set(swr, opt, arg, 0);
  537. swr_free(&swr);
  538. if (ret < 0) {
  539. av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
  540. return ret;
  541. }
  542. av_dict_set(&swr_opts, opt, arg, FLAGS);
  543. consumed = 1;
  544. }
  545. #endif
  546. #if CONFIG_AVRESAMPLE
  547. if ((o=opt_find(&rc, opt, NULL, 0,
  548. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  549. av_dict_set(&resample_opts, opt, arg, FLAGS);
  550. consumed = 1;
  551. }
  552. #endif
  553. if (consumed)
  554. return 0;
  555. return AVERROR_OPTION_NOT_FOUND;
  556. }
  557. /*
  558. * Check whether given option is a group separator.
  559. *
  560. * @return index of the group definition that matched or -1 if none
  561. */
  562. static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
  563. const char *opt)
  564. {
  565. int i;
  566. for (i = 0; i < nb_groups; i++) {
  567. const OptionGroupDef *p = &groups[i];
  568. if (p->sep && !strcmp(p->sep, opt))
  569. return i;
  570. }
  571. return -1;
  572. }
  573. /*
  574. * Finish parsing an option group.
  575. *
  576. * @param group_idx which group definition should this group belong to
  577. * @param arg argument of the group delimiting option
  578. */
  579. static void finish_group(OptionParseContext *octx, int group_idx,
  580. const char *arg)
  581. {
  582. OptionGroupList *l = &octx->groups[group_idx];
  583. OptionGroup *g;
  584. GROW_ARRAY(l->groups, l->nb_groups);
  585. g = &l->groups[l->nb_groups - 1];
  586. *g = octx->cur_group;
  587. g->arg = arg;
  588. g->group_def = l->group_def;
  589. g->sws_dict = sws_dict;
  590. g->swr_opts = swr_opts;
  591. g->codec_opts = codec_opts;
  592. g->format_opts = format_opts;
  593. g->resample_opts = resample_opts;
  594. codec_opts = NULL;
  595. format_opts = NULL;
  596. resample_opts = NULL;
  597. sws_dict = NULL;
  598. swr_opts = NULL;
  599. init_opts();
  600. memset(&octx->cur_group, 0, sizeof(octx->cur_group));
  601. }
  602. /*
  603. * Add an option instance to currently parsed group.
  604. */
  605. static void add_opt(OptionParseContext *octx, const OptionDef *opt,
  606. const char *key, const char *val)
  607. {
  608. int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET));
  609. OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
  610. GROW_ARRAY(g->opts, g->nb_opts);
  611. g->opts[g->nb_opts - 1].opt = opt;
  612. g->opts[g->nb_opts - 1].key = key;
  613. g->opts[g->nb_opts - 1].val = val;
  614. }
  615. static void init_parse_context(OptionParseContext *octx,
  616. const OptionGroupDef *groups, int nb_groups)
  617. {
  618. static const OptionGroupDef global_group = { "global" };
  619. int i;
  620. memset(octx, 0, sizeof(*octx));
  621. octx->nb_groups = nb_groups;
  622. octx->groups = av_mallocz_array(octx->nb_groups, sizeof(*octx->groups));
  623. if (!octx->groups)
  624. exit_program(1);
  625. for (i = 0; i < octx->nb_groups; i++)
  626. octx->groups[i].group_def = &groups[i];
  627. octx->global_opts.group_def = &global_group;
  628. octx->global_opts.arg = "";
  629. init_opts();
  630. }
  631. void uninit_parse_context(OptionParseContext *octx)
  632. {
  633. int i, j;
  634. for (i = 0; i < octx->nb_groups; i++) {
  635. OptionGroupList *l = &octx->groups[i];
  636. for (j = 0; j < l->nb_groups; j++) {
  637. av_freep(&l->groups[j].opts);
  638. av_dict_free(&l->groups[j].codec_opts);
  639. av_dict_free(&l->groups[j].format_opts);
  640. av_dict_free(&l->groups[j].resample_opts);
  641. av_dict_free(&l->groups[j].sws_dict);
  642. av_dict_free(&l->groups[j].swr_opts);
  643. }
  644. av_freep(&l->groups);
  645. }
  646. av_freep(&octx->groups);
  647. av_freep(&octx->cur_group.opts);
  648. av_freep(&octx->global_opts.opts);
  649. uninit_opts();
  650. }
  651. int split_commandline(OptionParseContext *octx, int argc, char *argv[],
  652. const OptionDef *options,
  653. const OptionGroupDef *groups, int nb_groups)
  654. {
  655. int optindex = 1;
  656. int dashdash = -2;
  657. /* perform system-dependent conversions for arguments list */
  658. prepare_app_arguments(&argc, &argv);
  659. init_parse_context(octx, groups, nb_groups);
  660. av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
  661. while (optindex < argc) {
  662. const char *opt = argv[optindex++], *arg;
  663. const OptionDef *po;
  664. int ret;
  665. av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
  666. if (opt[0] == '-' && opt[1] == '-' && !opt[2]) {
  667. dashdash = optindex;
  668. continue;
  669. }
  670. /* unnamed group separators, e.g. output filename */
  671. if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) {
  672. finish_group(octx, 0, opt);
  673. av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
  674. continue;
  675. }
  676. opt++;
  677. #define GET_ARG(arg) \
  678. do { \
  679. arg = argv[optindex++]; \
  680. if (!arg) { \
  681. av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
  682. return AVERROR(EINVAL); \
  683. } \
  684. } while (0)
  685. /* named group separators, e.g. -i */
  686. if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
  687. GET_ARG(arg);
  688. finish_group(octx, ret, arg);
  689. av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
  690. groups[ret].name, arg);
  691. continue;
  692. }
  693. /* normal options */
  694. po = find_option(options, opt);
  695. if (po->name) {
  696. if (po->flags & OPT_EXIT) {
  697. /* optional argument, e.g. -h */
  698. arg = argv[optindex++];
  699. } else if (po->flags & HAS_ARG) {
  700. GET_ARG(arg);
  701. } else {
  702. arg = "1";
  703. }
  704. add_opt(octx, po, opt, arg);
  705. av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
  706. "argument '%s'.\n", po->name, po->help, arg);
  707. continue;
  708. }
  709. /* AVOptions */
  710. if (argv[optindex]) {
  711. ret = opt_default(NULL, opt, argv[optindex]);
  712. if (ret >= 0) {
  713. av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
  714. "argument '%s'.\n", opt, argv[optindex]);
  715. optindex++;
  716. continue;
  717. } else if (ret != AVERROR_OPTION_NOT_FOUND) {
  718. av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
  719. "with argument '%s'.\n", opt, argv[optindex]);
  720. return ret;
  721. }
  722. }
  723. /* boolean -nofoo options */
  724. if (opt[0] == 'n' && opt[1] == 'o' &&
  725. (po = find_option(options, opt + 2)) &&
  726. po->name && po->flags & OPT_BOOL) {
  727. add_opt(octx, po, opt, "0");
  728. av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
  729. "argument 0.\n", po->name, po->help);
  730. continue;
  731. }
  732. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
  733. return AVERROR_OPTION_NOT_FOUND;
  734. }
  735. if (octx->cur_group.nb_opts || codec_opts || format_opts || resample_opts)
  736. av_log(NULL, AV_LOG_WARNING, "Trailing options were found on the "
  737. "commandline.\n");
  738. av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
  739. return 0;
  740. }
  741. int opt_cpuflags(void *optctx, const char *opt, const char *arg)
  742. {
  743. int ret;
  744. unsigned flags = av_get_cpu_flags();
  745. if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
  746. return ret;
  747. av_force_cpu_flags(flags);
  748. return 0;
  749. }
  750. int opt_loglevel(void *optctx, const char *opt, const char *arg)
  751. {
  752. const struct { const char *name; int level; } log_levels[] = {
  753. { "quiet" , AV_LOG_QUIET },
  754. { "panic" , AV_LOG_PANIC },
  755. { "fatal" , AV_LOG_FATAL },
  756. { "error" , AV_LOG_ERROR },
  757. { "warning", AV_LOG_WARNING },
  758. { "info" , AV_LOG_INFO },
  759. { "verbose", AV_LOG_VERBOSE },
  760. { "debug" , AV_LOG_DEBUG },
  761. { "trace" , AV_LOG_TRACE },
  762. };
  763. char *tail;
  764. int level;
  765. int flags;
  766. int i;
  767. flags = av_log_get_flags();
  768. tail = strstr(arg, "repeat");
  769. if (tail)
  770. flags &= ~AV_LOG_SKIP_REPEATED;
  771. else
  772. flags |= AV_LOG_SKIP_REPEATED;
  773. av_log_set_flags(flags);
  774. if (tail == arg)
  775. arg += 6 + (arg[6]=='+');
  776. if(tail && !*arg)
  777. return 0;
  778. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
  779. if (!strcmp(log_levels[i].name, arg)) {
  780. av_log_set_level(log_levels[i].level);
  781. return 0;
  782. }
  783. }
  784. level = strtol(arg, &tail, 10);
  785. if (*tail) {
  786. av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
  787. "Possible levels are numbers or:\n", arg);
  788. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
  789. av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
  790. exit_program(1);
  791. }
  792. av_log_set_level(level);
  793. return 0;
  794. }
  795. static void expand_filename_template(AVBPrint *bp, const char *template,
  796. struct tm *tm)
  797. {
  798. int c;
  799. while ((c = *(template++))) {
  800. if (c == '%') {
  801. if (!(c = *(template++)))
  802. break;
  803. switch (c) {
  804. case 'p':
  805. av_bprintf(bp, "%s", program_name);
  806. break;
  807. case 't':
  808. av_bprintf(bp, "%04d%02d%02d-%02d%02d%02d",
  809. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  810. tm->tm_hour, tm->tm_min, tm->tm_sec);
  811. break;
  812. case '%':
  813. av_bprint_chars(bp, c, 1);
  814. break;
  815. }
  816. } else {
  817. av_bprint_chars(bp, c, 1);
  818. }
  819. }
  820. }
  821. static int init_report(const char *env)
  822. {
  823. char *filename_template = NULL;
  824. char *key, *val;
  825. int ret, count = 0;
  826. time_t now;
  827. struct tm *tm;
  828. AVBPrint filename;
  829. if (report_file) /* already opened */
  830. return 0;
  831. time(&now);
  832. tm = localtime(&now);
  833. while (env && *env) {
  834. if ((ret = av_opt_get_key_value(&env, "=", ":", 0, &key, &val)) < 0) {
  835. if (count)
  836. av_log(NULL, AV_LOG_ERROR,
  837. "Failed to parse FFREPORT environment variable: %s\n",
  838. av_err2str(ret));
  839. break;
  840. }
  841. if (*env)
  842. env++;
  843. count++;
  844. if (!strcmp(key, "file")) {
  845. av_free(filename_template);
  846. filename_template = val;
  847. val = NULL;
  848. } else if (!strcmp(key, "level")) {
  849. char *tail;
  850. report_file_level = strtol(val, &tail, 10);
  851. if (*tail) {
  852. av_log(NULL, AV_LOG_FATAL, "Invalid report file level\n");
  853. exit_program(1);
  854. }
  855. } else {
  856. av_log(NULL, AV_LOG_ERROR, "Unknown key '%s' in FFREPORT\n", key);
  857. }
  858. av_free(val);
  859. av_free(key);
  860. }
  861. av_bprint_init(&filename, 0, 1);
  862. expand_filename_template(&filename,
  863. av_x_if_null(filename_template, "%p-%t.log"), tm);
  864. av_free(filename_template);
  865. if (!av_bprint_is_complete(&filename)) {
  866. av_log(NULL, AV_LOG_ERROR, "Out of memory building report file name\n");
  867. return AVERROR(ENOMEM);
  868. }
  869. report_file = fopen(filename.str, "w");
  870. if (!report_file) {
  871. int ret = AVERROR(errno);
  872. av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
  873. filename.str, strerror(errno));
  874. return ret;
  875. }
  876. av_log_set_callback(log_callback_report);
  877. av_log(NULL, AV_LOG_INFO,
  878. "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
  879. "Report written to \"%s\"\n",
  880. program_name,
  881. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  882. tm->tm_hour, tm->tm_min, tm->tm_sec,
  883. filename.str);
  884. av_bprint_finalize(&filename, NULL);
  885. return 0;
  886. }
  887. int opt_report(const char *opt)
  888. {
  889. return init_report(NULL);
  890. }
  891. int opt_max_alloc(void *optctx, const char *opt, const char *arg)
  892. {
  893. char *tail;
  894. size_t max;
  895. max = strtol(arg, &tail, 10);
  896. if (*tail) {
  897. av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
  898. exit_program(1);
  899. }
  900. av_max_alloc(max);
  901. return 0;
  902. }
  903. int opt_timelimit(void *optctx, const char *opt, const char *arg)
  904. {
  905. #if HAVE_SETRLIMIT
  906. int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
  907. struct rlimit rl = { lim, lim + 1 };
  908. if (setrlimit(RLIMIT_CPU, &rl))
  909. perror("setrlimit");
  910. #else
  911. av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
  912. #endif
  913. return 0;
  914. }
  915. void print_error(const char *filename, int err)
  916. {
  917. char errbuf[128];
  918. const char *errbuf_ptr = errbuf;
  919. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  920. errbuf_ptr = strerror(AVUNERROR(err));
  921. av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
  922. }
  923. static int warned_cfg = 0;
  924. #define INDENT 1
  925. #define SHOW_VERSION 2
  926. #define SHOW_CONFIG 4
  927. #define SHOW_COPYRIGHT 8
  928. #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
  929. if (CONFIG_##LIBNAME) { \
  930. const char *indent = flags & INDENT? " " : ""; \
  931. if (flags & SHOW_VERSION) { \
  932. unsigned int version = libname##_version(); \
  933. av_log(NULL, level, \
  934. "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
  935. indent, #libname, \
  936. LIB##LIBNAME##_VERSION_MAJOR, \
  937. LIB##LIBNAME##_VERSION_MINOR, \
  938. LIB##LIBNAME##_VERSION_MICRO, \
  939. AV_VERSION_MAJOR(version), AV_VERSION_MINOR(version),\
  940. AV_VERSION_MICRO(version)); \
  941. } \
  942. if (flags & SHOW_CONFIG) { \
  943. const char *cfg = libname##_configuration(); \
  944. if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
  945. if (!warned_cfg) { \
  946. av_log(NULL, level, \
  947. "%sWARNING: library configuration mismatch\n", \
  948. indent); \
  949. warned_cfg = 1; \
  950. } \
  951. av_log(NULL, level, "%s%-11s configuration: %s\n", \
  952. indent, #libname, cfg); \
  953. } \
  954. } \
  955. } \
  956. static void print_all_libs_info(int flags, int level)
  957. {
  958. PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
  959. PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
  960. PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
  961. PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
  962. PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
  963. PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
  964. PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
  965. PRINT_LIB_INFO(swresample, SWRESAMPLE, flags, level);
  966. PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
  967. }
  968. static void print_program_info(int flags, int level)
  969. {
  970. const char *indent = flags & INDENT? " " : "";
  971. av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
  972. if (flags & SHOW_COPYRIGHT)
  973. av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
  974. program_birth_year, CONFIG_THIS_YEAR);
  975. av_log(NULL, level, "\n");
  976. av_log(NULL, level, "%sbuilt with %s\n", indent, CC_IDENT);
  977. av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
  978. }
  979. static void print_buildconf(int flags, int level)
  980. {
  981. const char *indent = flags & INDENT ? " " : "";
  982. char str[] = { FFMPEG_CONFIGURATION };
  983. char *conflist, *remove_tilde, *splitconf;
  984. // Change all the ' --' strings to '~--' so that
  985. // they can be identified as tokens.
  986. while ((conflist = strstr(str, " --")) != NULL) {
  987. strncpy(conflist, "~--", 3);
  988. }
  989. // Compensate for the weirdness this would cause
  990. // when passing 'pkg-config --static'.
  991. while ((remove_tilde = strstr(str, "pkg-config~")) != NULL) {
  992. strncpy(remove_tilde, "pkg-config ", 11);
  993. }
  994. splitconf = strtok(str, "~");
  995. av_log(NULL, level, "\n%sconfiguration:\n", indent);
  996. while (splitconf != NULL) {
  997. av_log(NULL, level, "%s%s%s\n", indent, indent, splitconf);
  998. splitconf = strtok(NULL, "~");
  999. }
  1000. }
  1001. void show_banner(int argc, char **argv, const OptionDef *options)
  1002. {
  1003. int idx = locate_option(argc, argv, options, "version");
  1004. if (hide_banner || idx)
  1005. return;
  1006. print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
  1007. print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
  1008. print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
  1009. }
  1010. int show_version(void *optctx, const char *opt, const char *arg)
  1011. {
  1012. av_log_set_callback(log_callback_help);
  1013. print_program_info (SHOW_COPYRIGHT, AV_LOG_INFO);
  1014. print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
  1015. return 0;
  1016. }
  1017. int show_buildconf(void *optctx, const char *opt, const char *arg)
  1018. {
  1019. av_log_set_callback(log_callback_help);
  1020. print_buildconf (INDENT|0, AV_LOG_INFO);
  1021. return 0;
  1022. }
  1023. int show_license(void *optctx, const char *opt, const char *arg)
  1024. {
  1025. #if CONFIG_NONFREE
  1026. printf(
  1027. "This version of %s has nonfree parts compiled in.\n"
  1028. "Therefore it is not legally redistributable.\n",
  1029. program_name );
  1030. #elif CONFIG_GPLV3
  1031. printf(
  1032. "%s is free software; you can redistribute it and/or modify\n"
  1033. "it under the terms of the GNU General Public License as published by\n"
  1034. "the Free Software Foundation; either version 3 of the License, or\n"
  1035. "(at your option) any later version.\n"
  1036. "\n"
  1037. "%s is distributed in the hope that it will be useful,\n"
  1038. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1039. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1040. "GNU General Public License for more details.\n"
  1041. "\n"
  1042. "You should have received a copy of the GNU General Public License\n"
  1043. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  1044. program_name, program_name, program_name );
  1045. #elif CONFIG_GPL
  1046. printf(
  1047. "%s is free software; you can redistribute it and/or modify\n"
  1048. "it under the terms of the GNU General Public License as published by\n"
  1049. "the Free Software Foundation; either version 2 of the License, or\n"
  1050. "(at your option) any later version.\n"
  1051. "\n"
  1052. "%s is distributed in the hope that it will be useful,\n"
  1053. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1054. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1055. "GNU General Public License for more details.\n"
  1056. "\n"
  1057. "You should have received a copy of the GNU General Public License\n"
  1058. "along with %s; if not, write to the Free Software\n"
  1059. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  1060. program_name, program_name, program_name );
  1061. #elif CONFIG_LGPLV3
  1062. printf(
  1063. "%s is free software; you can redistribute it and/or modify\n"
  1064. "it under the terms of the GNU Lesser General Public License as published by\n"
  1065. "the Free Software Foundation; either version 3 of the License, or\n"
  1066. "(at your option) any later version.\n"
  1067. "\n"
  1068. "%s is distributed in the hope that it will be useful,\n"
  1069. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1070. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1071. "GNU Lesser General Public License for more details.\n"
  1072. "\n"
  1073. "You should have received a copy of the GNU Lesser General Public License\n"
  1074. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  1075. program_name, program_name, program_name );
  1076. #else
  1077. printf(
  1078. "%s is free software; you can redistribute it and/or\n"
  1079. "modify it under the terms of the GNU Lesser General Public\n"
  1080. "License as published by the Free Software Foundation; either\n"
  1081. "version 2.1 of the License, or (at your option) any later version.\n"
  1082. "\n"
  1083. "%s is distributed in the hope that it will be useful,\n"
  1084. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1085. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  1086. "Lesser General Public License for more details.\n"
  1087. "\n"
  1088. "You should have received a copy of the GNU Lesser General Public\n"
  1089. "License along with %s; if not, write to the Free Software\n"
  1090. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  1091. program_name, program_name, program_name );
  1092. #endif
  1093. return 0;
  1094. }
  1095. static int is_device(const AVClass *avclass)
  1096. {
  1097. if (!avclass)
  1098. return 0;
  1099. return AV_IS_INPUT_DEVICE(avclass->category) || AV_IS_OUTPUT_DEVICE(avclass->category);
  1100. }
  1101. static int show_formats_devices(void *optctx, const char *opt, const char *arg, int device_only, int muxdemuxers)
  1102. {
  1103. AVInputFormat *ifmt = NULL;
  1104. AVOutputFormat *ofmt = NULL;
  1105. const char *last_name;
  1106. int is_dev;
  1107. printf("%s\n"
  1108. " D. = Demuxing supported\n"
  1109. " .E = Muxing supported\n"
  1110. " --\n", device_only ? "Devices:" : "File formats:");
  1111. last_name = "000";
  1112. for (;;) {
  1113. int decode = 0;
  1114. int encode = 0;
  1115. const char *name = NULL;
  1116. const char *long_name = NULL;
  1117. if (muxdemuxers !=SHOW_DEMUXERS) {
  1118. while ((ofmt = av_oformat_next(ofmt))) {
  1119. is_dev = is_device(ofmt->priv_class);
  1120. if (!is_dev && device_only)
  1121. continue;
  1122. if ((!name || strcmp(ofmt->name, name) < 0) &&
  1123. strcmp(ofmt->name, last_name) > 0) {
  1124. name = ofmt->name;
  1125. long_name = ofmt->long_name;
  1126. encode = 1;
  1127. }
  1128. }
  1129. }
  1130. if (muxdemuxers != SHOW_MUXERS) {
  1131. while ((ifmt = av_iformat_next(ifmt))) {
  1132. is_dev = is_device(ifmt->priv_class);
  1133. if (!is_dev && device_only)
  1134. continue;
  1135. if ((!name || strcmp(ifmt->name, name) < 0) &&
  1136. strcmp(ifmt->name, last_name) > 0) {
  1137. name = ifmt->name;
  1138. long_name = ifmt->long_name;
  1139. encode = 0;
  1140. }
  1141. if (name && strcmp(ifmt->name, name) == 0)
  1142. decode = 1;
  1143. }
  1144. }
  1145. if (!name)
  1146. break;
  1147. last_name = name;
  1148. printf(" %s%s %-15s %s\n",
  1149. decode ? "D" : " ",
  1150. encode ? "E" : " ",
  1151. name,
  1152. long_name ? long_name:" ");
  1153. }
  1154. return 0;
  1155. }
  1156. int show_formats(void *optctx, const char *opt, const char *arg)
  1157. {
  1158. return show_formats_devices(optctx, opt, arg, 0, SHOW_DEFAULT);
  1159. }
  1160. int show_muxers(void *optctx, const char *opt, const char *arg)
  1161. {
  1162. return show_formats_devices(optctx, opt, arg, 0, SHOW_MUXERS);
  1163. }
  1164. int show_demuxers(void *optctx, const char *opt, const char *arg)
  1165. {
  1166. return show_formats_devices(optctx, opt, arg, 0, SHOW_DEMUXERS);
  1167. }
  1168. int show_devices(void *optctx, const char *opt, const char *arg)
  1169. {
  1170. return show_formats_devices(optctx, opt, arg, 1, SHOW_DEFAULT);
  1171. }
  1172. #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
  1173. if (codec->field) { \
  1174. const type *p = codec->field; \
  1175. \
  1176. printf(" Supported " list_name ":"); \
  1177. while (*p != term) { \
  1178. get_name(*p); \
  1179. printf(" %s", name); \
  1180. p++; \
  1181. } \
  1182. printf("\n"); \
  1183. } \
  1184. static void print_codec(const AVCodec *c)
  1185. {
  1186. int encoder = av_codec_is_encoder(c);
  1187. printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
  1188. c->long_name ? c->long_name : "");
  1189. printf(" General capabilities: ");
  1190. if (c->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND)
  1191. printf("horizband ");
  1192. if (c->capabilities & AV_CODEC_CAP_DR1)
  1193. printf("dr1 ");
  1194. if (c->capabilities & AV_CODEC_CAP_TRUNCATED)
  1195. printf("trunc ");
  1196. if (c->capabilities & AV_CODEC_CAP_DELAY)
  1197. printf("delay ");
  1198. if (c->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)
  1199. printf("small ");
  1200. if (c->capabilities & AV_CODEC_CAP_SUBFRAMES)
  1201. printf("subframes ");
  1202. if (c->capabilities & AV_CODEC_CAP_EXPERIMENTAL)
  1203. printf("exp ");
  1204. if (c->capabilities & AV_CODEC_CAP_CHANNEL_CONF)
  1205. printf("chconf ");
  1206. if (c->capabilities & AV_CODEC_CAP_PARAM_CHANGE)
  1207. printf("paramchange ");
  1208. if (c->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
  1209. printf("variable ");
  1210. if (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
  1211. AV_CODEC_CAP_SLICE_THREADS |
  1212. AV_CODEC_CAP_AUTO_THREADS))
  1213. printf("threads ");
  1214. if (!c->capabilities)
  1215. printf("none");
  1216. printf("\n");
  1217. if (c->type == AVMEDIA_TYPE_VIDEO ||
  1218. c->type == AVMEDIA_TYPE_AUDIO) {
  1219. printf(" Threading capabilities: ");
  1220. switch (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
  1221. AV_CODEC_CAP_SLICE_THREADS |
  1222. AV_CODEC_CAP_AUTO_THREADS)) {
  1223. case AV_CODEC_CAP_FRAME_THREADS |
  1224. AV_CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
  1225. case AV_CODEC_CAP_FRAME_THREADS: printf("frame"); break;
  1226. case AV_CODEC_CAP_SLICE_THREADS: printf("slice"); break;
  1227. case AV_CODEC_CAP_AUTO_THREADS : printf("auto"); break;
  1228. default: printf("none"); break;
  1229. }
  1230. printf("\n");
  1231. }
  1232. if (c->supported_framerates) {
  1233. const AVRational *fps = c->supported_framerates;
  1234. printf(" Supported framerates:");
  1235. while (fps->num) {
  1236. printf(" %d/%d", fps->num, fps->den);
  1237. fps++;
  1238. }
  1239. printf("\n");
  1240. }
  1241. PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
  1242. AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
  1243. PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
  1244. GET_SAMPLE_RATE_NAME);
  1245. PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
  1246. AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
  1247. PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
  1248. 0, GET_CH_LAYOUT_DESC);
  1249. if (c->priv_class) {
  1250. show_help_children(c->priv_class,
  1251. AV_OPT_FLAG_ENCODING_PARAM |
  1252. AV_OPT_FLAG_DECODING_PARAM);
  1253. }
  1254. }
  1255. static char get_media_type_char(enum AVMediaType type)
  1256. {
  1257. switch (type) {
  1258. case AVMEDIA_TYPE_VIDEO: return 'V';
  1259. case AVMEDIA_TYPE_AUDIO: return 'A';
  1260. case AVMEDIA_TYPE_DATA: return 'D';
  1261. case AVMEDIA_TYPE_SUBTITLE: return 'S';
  1262. case AVMEDIA_TYPE_ATTACHMENT:return 'T';
  1263. default: return '?';
  1264. }
  1265. }
  1266. static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
  1267. int encoder)
  1268. {
  1269. while ((prev = av_codec_next(prev))) {
  1270. if (prev->id == id &&
  1271. (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
  1272. return prev;
  1273. }
  1274. return NULL;
  1275. }
  1276. static int compare_codec_desc(const void *a, const void *b)
  1277. {
  1278. const AVCodecDescriptor * const *da = a;
  1279. const AVCodecDescriptor * const *db = b;
  1280. return (*da)->type != (*db)->type ? FFDIFFSIGN((*da)->type, (*db)->type) :
  1281. strcmp((*da)->name, (*db)->name);
  1282. }
  1283. static unsigned get_codecs_sorted(const AVCodecDescriptor ***rcodecs)
  1284. {
  1285. const AVCodecDescriptor *desc = NULL;
  1286. const AVCodecDescriptor **codecs;
  1287. unsigned nb_codecs = 0, i = 0;
  1288. while ((desc = avcodec_descriptor_next(desc)))
  1289. nb_codecs++;
  1290. if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs)))) {
  1291. av_log(NULL, AV_LOG_ERROR, "Out of memory\n");
  1292. exit_program(1);
  1293. }
  1294. desc = NULL;
  1295. while ((desc = avcodec_descriptor_next(desc)))
  1296. codecs[i++] = desc;
  1297. av_assert0(i == nb_codecs);
  1298. qsort(codecs, nb_codecs, sizeof(*codecs), compare_codec_desc);
  1299. *rcodecs = codecs;
  1300. return nb_codecs;
  1301. }
  1302. static void print_codecs_for_id(enum AVCodecID id, int encoder)
  1303. {
  1304. const AVCodec *codec = NULL;
  1305. printf(" (%s: ", encoder ? "encoders" : "decoders");
  1306. while ((codec = next_codec_for_id(id, codec, encoder)))
  1307. printf("%s ", codec->name);
  1308. printf(")");
  1309. }
  1310. int show_codecs(void *optctx, const char *opt, const char *arg)
  1311. {
  1312. const AVCodecDescriptor **codecs;
  1313. unsigned i, nb_codecs = get_codecs_sorted(&codecs);
  1314. printf("Codecs:\n"
  1315. " D..... = Decoding supported\n"
  1316. " .E.... = Encoding supported\n"
  1317. " ..V... = Video codec\n"
  1318. " ..A... = Audio codec\n"
  1319. " ..S... = Subtitle codec\n"
  1320. " ...I.. = Intra frame-only codec\n"
  1321. " ....L. = Lossy compression\n"
  1322. " .....S = Lossless compression\n"
  1323. " -------\n");
  1324. for (i = 0; i < nb_codecs; i++) {
  1325. const AVCodecDescriptor *desc = codecs[i];
  1326. const AVCodec *codec = NULL;
  1327. if (strstr(desc->name, "_deprecated"))
  1328. continue;
  1329. printf(" ");
  1330. printf(avcodec_find_decoder(desc->id) ? "D" : ".");
  1331. printf(avcodec_find_encoder(desc->id) ? "E" : ".");
  1332. printf("%c", get_media_type_char(desc->type));
  1333. printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
  1334. printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
  1335. printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
  1336. printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
  1337. /* print decoders/encoders when there's more than one or their
  1338. * names are different from codec name */
  1339. while ((codec = next_codec_for_id(desc->id, codec, 0))) {
  1340. if (strcmp(codec->name, desc->name)) {
  1341. print_codecs_for_id(desc->id, 0);
  1342. break;
  1343. }
  1344. }
  1345. codec = NULL;
  1346. while ((codec = next_codec_for_id(desc->id, codec, 1))) {
  1347. if (strcmp(codec->name, desc->name)) {
  1348. print_codecs_for_id(desc->id, 1);
  1349. break;
  1350. }
  1351. }
  1352. printf("\n");
  1353. }
  1354. av_free(codecs);
  1355. return 0;
  1356. }
  1357. static void print_codecs(int encoder)
  1358. {
  1359. const AVCodecDescriptor **codecs;
  1360. unsigned i, nb_codecs = get_codecs_sorted(&codecs);
  1361. printf("%s:\n"
  1362. " V..... = Video\n"
  1363. " A..... = Audio\n"
  1364. " S..... = Subtitle\n"
  1365. " .F.... = Frame-level multithreading\n"
  1366. " ..S... = Slice-level multithreading\n"
  1367. " ...X.. = Codec is experimental\n"
  1368. " ....B. = Supports draw_horiz_band\n"
  1369. " .....D = Supports direct rendering method 1\n"
  1370. " ------\n",
  1371. encoder ? "Encoders" : "Decoders");
  1372. for (i = 0; i < nb_codecs; i++) {
  1373. const AVCodecDescriptor *desc = codecs[i];
  1374. const AVCodec *codec = NULL;
  1375. while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
  1376. printf(" %c", get_media_type_char(desc->type));
  1377. printf((codec->capabilities & AV_CODEC_CAP_FRAME_THREADS) ? "F" : ".");
  1378. printf((codec->capabilities & AV_CODEC_CAP_SLICE_THREADS) ? "S" : ".");
  1379. printf((codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
  1380. printf((codec->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND)?"B" : ".");
  1381. printf((codec->capabilities & AV_CODEC_CAP_DR1) ? "D" : ".");
  1382. printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
  1383. if (strcmp(codec->name, desc->name))
  1384. printf(" (codec %s)", desc->name);
  1385. printf("\n");
  1386. }
  1387. }
  1388. av_free(codecs);
  1389. }
  1390. int show_decoders(void *optctx, const char *opt, const char *arg)
  1391. {
  1392. print_codecs(0);
  1393. return 0;
  1394. }
  1395. int show_encoders(void *optctx, const char *opt, const char *arg)
  1396. {
  1397. print_codecs(1);
  1398. return 0;
  1399. }
  1400. int show_bsfs(void *optctx, const char *opt, const char *arg)
  1401. {
  1402. const AVBitStreamFilter *bsf = NULL;
  1403. void *opaque = NULL;
  1404. printf("Bitstream filters:\n");
  1405. while ((bsf = av_bsf_next(&opaque)))
  1406. printf("%s\n", bsf->name);
  1407. printf("\n");
  1408. return 0;
  1409. }
  1410. int show_protocols(void *optctx, const char *opt, const char *arg)
  1411. {
  1412. void *opaque = NULL;
  1413. const char *name;
  1414. printf("Supported file protocols:\n"
  1415. "Input:\n");
  1416. while ((name = avio_enum_protocols(&opaque, 0)))
  1417. printf(" %s\n", name);
  1418. printf("Output:\n");
  1419. while ((name = avio_enum_protocols(&opaque, 1)))
  1420. printf(" %s\n", name);
  1421. return 0;
  1422. }
  1423. int show_filters(void *optctx, const char *opt, const char *arg)
  1424. {
  1425. #if CONFIG_AVFILTER
  1426. const AVFilter *filter = NULL;
  1427. char descr[64], *descr_cur;
  1428. int i, j;
  1429. const AVFilterPad *pad;
  1430. printf("Filters:\n"
  1431. " T.. = Timeline support\n"
  1432. " .S. = Slice threading\n"
  1433. " ..C = Command support\n"
  1434. " A = Audio input/output\n"
  1435. " V = Video input/output\n"
  1436. " N = Dynamic number and/or type of input/output\n"
  1437. " | = Source or sink filter\n");
  1438. while ((filter = avfilter_next(filter))) {
  1439. descr_cur = descr;
  1440. for (i = 0; i < 2; i++) {
  1441. if (i) {
  1442. *(descr_cur++) = '-';
  1443. *(descr_cur++) = '>';
  1444. }
  1445. pad = i ? filter->outputs : filter->inputs;
  1446. for (j = 0; pad && avfilter_pad_get_name(pad, j); j++) {
  1447. if (descr_cur >= descr + sizeof(descr) - 4)
  1448. break;
  1449. *(descr_cur++) = get_media_type_char(avfilter_pad_get_type(pad, j));
  1450. }
  1451. if (!j)
  1452. *(descr_cur++) = ((!i && (filter->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)) ||
  1453. ( i && (filter->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS))) ? 'N' : '|';
  1454. }
  1455. *descr_cur = 0;
  1456. printf(" %c%c%c %-17s %-10s %s\n",
  1457. filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE ? 'T' : '.',
  1458. filter->flags & AVFILTER_FLAG_SLICE_THREADS ? 'S' : '.',
  1459. filter->process_command ? 'C' : '.',
  1460. filter->name, descr, filter->description);
  1461. }
  1462. #else
  1463. printf("No filters available: libavfilter disabled\n");
  1464. #endif
  1465. return 0;
  1466. }
  1467. int show_colors(void *optctx, const char *opt, const char *arg)
  1468. {
  1469. const char *name;
  1470. const uint8_t *rgb;
  1471. int i;
  1472. printf("%-32s #RRGGBB\n", "name");
  1473. for (i = 0; name = av_get_known_color_name(i, &rgb); i++)
  1474. printf("%-32s #%02x%02x%02x\n", name, rgb[0], rgb[1], rgb[2]);
  1475. return 0;
  1476. }
  1477. int show_pix_fmts(void *optctx, const char *opt, const char *arg)
  1478. {
  1479. const AVPixFmtDescriptor *pix_desc = NULL;
  1480. printf("Pixel formats:\n"
  1481. "I.... = Supported Input format for conversion\n"
  1482. ".O... = Supported Output format for conversion\n"
  1483. "..H.. = Hardware accelerated format\n"
  1484. "...P. = Paletted format\n"
  1485. "....B = Bitstream format\n"
  1486. "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
  1487. "-----\n");
  1488. #if !CONFIG_SWSCALE
  1489. # define sws_isSupportedInput(x) 0
  1490. # define sws_isSupportedOutput(x) 0
  1491. #endif
  1492. while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
  1493. enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
  1494. printf("%c%c%c%c%c %-16s %d %2d\n",
  1495. sws_isSupportedInput (pix_fmt) ? 'I' : '.',
  1496. sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
  1497. pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? 'H' : '.',
  1498. pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? 'P' : '.',
  1499. pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
  1500. pix_desc->name,
  1501. pix_desc->nb_components,
  1502. av_get_bits_per_pixel(pix_desc));
  1503. }
  1504. return 0;
  1505. }
  1506. int show_layouts(void *optctx, const char *opt, const char *arg)
  1507. {
  1508. int i = 0;
  1509. uint64_t layout, j;
  1510. const char *name, *descr;
  1511. printf("Individual channels:\n"
  1512. "NAME DESCRIPTION\n");
  1513. for (i = 0; i < 63; i++) {
  1514. name = av_get_channel_name((uint64_t)1 << i);
  1515. if (!name)
  1516. continue;
  1517. descr = av_get_channel_description((uint64_t)1 << i);
  1518. printf("%-14s %s\n", name, descr);
  1519. }
  1520. printf("\nStandard channel layouts:\n"
  1521. "NAME DECOMPOSITION\n");
  1522. for (i = 0; !av_get_standard_channel_layout(i, &layout, &name); i++) {
  1523. if (name) {
  1524. printf("%-14s ", name);
  1525. for (j = 1; j; j <<= 1)
  1526. if ((layout & j))
  1527. printf("%s%s", (layout & (j - 1)) ? "+" : "", av_get_channel_name(j));
  1528. printf("\n");
  1529. }
  1530. }
  1531. return 0;
  1532. }
  1533. int show_sample_fmts(void *optctx, const char *opt, const char *arg)
  1534. {
  1535. int i;
  1536. char fmt_str[128];
  1537. for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
  1538. printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
  1539. return 0;
  1540. }
  1541. static void show_help_codec(const char *name, int encoder)
  1542. {
  1543. const AVCodecDescriptor *desc;
  1544. const AVCodec *codec;
  1545. if (!name) {
  1546. av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
  1547. return;
  1548. }
  1549. codec = encoder ? avcodec_find_encoder_by_name(name) :
  1550. avcodec_find_decoder_by_name(name);
  1551. if (codec)
  1552. print_codec(codec);
  1553. else if ((desc = avcodec_descriptor_get_by_name(name))) {
  1554. int printed = 0;
  1555. while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
  1556. printed = 1;
  1557. print_codec(codec);
  1558. }
  1559. if (!printed) {
  1560. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, "
  1561. "but no %s for it are available. FFmpeg might need to be "
  1562. "recompiled with additional external libraries.\n",
  1563. name, encoder ? "encoders" : "decoders");
  1564. }
  1565. } else {
  1566. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n",
  1567. name);
  1568. }
  1569. }
  1570. static void show_help_demuxer(const char *name)
  1571. {
  1572. const AVInputFormat *fmt = av_find_input_format(name);
  1573. if (!fmt) {
  1574. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1575. return;
  1576. }
  1577. printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
  1578. if (fmt->extensions)
  1579. printf(" Common extensions: %s.\n", fmt->extensions);
  1580. if (fmt->priv_class)
  1581. show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
  1582. }
  1583. static void show_help_muxer(const char *name)
  1584. {
  1585. const AVCodecDescriptor *desc;
  1586. const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
  1587. if (!fmt) {
  1588. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1589. return;
  1590. }
  1591. printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
  1592. if (fmt->extensions)
  1593. printf(" Common extensions: %s.\n", fmt->extensions);
  1594. if (fmt->mime_type)
  1595. printf(" Mime type: %s.\n", fmt->mime_type);
  1596. if (fmt->video_codec != AV_CODEC_ID_NONE &&
  1597. (desc = avcodec_descriptor_get(fmt->video_codec))) {
  1598. printf(" Default video codec: %s.\n", desc->name);
  1599. }
  1600. if (fmt->audio_codec != AV_CODEC_ID_NONE &&
  1601. (desc = avcodec_descriptor_get(fmt->audio_codec))) {
  1602. printf(" Default audio codec: %s.\n", desc->name);
  1603. }
  1604. if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
  1605. (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
  1606. printf(" Default subtitle codec: %s.\n", desc->name);
  1607. }
  1608. if (fmt->priv_class)
  1609. show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
  1610. }
  1611. #if CONFIG_AVFILTER
  1612. static void show_help_filter(const char *name)
  1613. {
  1614. #if CONFIG_AVFILTER
  1615. const AVFilter *f = avfilter_get_by_name(name);
  1616. int i, count;
  1617. if (!name) {
  1618. av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
  1619. return;
  1620. } else if (!f) {
  1621. av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
  1622. return;
  1623. }
  1624. printf("Filter %s\n", f->name);
  1625. if (f->description)
  1626. printf(" %s\n", f->description);
  1627. if (f->flags & AVFILTER_FLAG_SLICE_THREADS)
  1628. printf(" slice threading supported\n");
  1629. printf(" Inputs:\n");
  1630. count = avfilter_pad_count(f->inputs);
  1631. for (i = 0; i < count; i++) {
  1632. printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
  1633. media_type_string(avfilter_pad_get_type(f->inputs, i)));
  1634. }
  1635. if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
  1636. printf(" dynamic (depending on the options)\n");
  1637. else if (!count)
  1638. printf(" none (source filter)\n");
  1639. printf(" Outputs:\n");
  1640. count = avfilter_pad_count(f->outputs);
  1641. for (i = 0; i < count; i++) {
  1642. printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
  1643. media_type_string(avfilter_pad_get_type(f->outputs, i)));
  1644. }
  1645. if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
  1646. printf(" dynamic (depending on the options)\n");
  1647. else if (!count)
  1648. printf(" none (sink filter)\n");
  1649. if (f->priv_class)
  1650. show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM |
  1651. AV_OPT_FLAG_AUDIO_PARAM);
  1652. if (f->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)
  1653. printf("This filter has support for timeline through the 'enable' option.\n");
  1654. #else
  1655. av_log(NULL, AV_LOG_ERROR, "Build without libavfilter; "
  1656. "can not to satisfy request\n");
  1657. #endif
  1658. }
  1659. #endif
  1660. int show_help(void *optctx, const char *opt, const char *arg)
  1661. {
  1662. char *topic, *par;
  1663. av_log_set_callback(log_callback_help);
  1664. topic = av_strdup(arg ? arg : "");
  1665. if (!topic)
  1666. return AVERROR(ENOMEM);
  1667. par = strchr(topic, '=');
  1668. if (par)
  1669. *par++ = 0;
  1670. if (!*topic) {
  1671. show_help_default(topic, par);
  1672. } else if (!strcmp(topic, "decoder")) {
  1673. show_help_codec(par, 0);
  1674. } else if (!strcmp(topic, "encoder")) {
  1675. show_help_codec(par, 1);
  1676. } else if (!strcmp(topic, "demuxer")) {
  1677. show_help_demuxer(par);
  1678. } else if (!strcmp(topic, "muxer")) {
  1679. show_help_muxer(par);
  1680. #if CONFIG_AVFILTER
  1681. } else if (!strcmp(topic, "filter")) {
  1682. show_help_filter(par);
  1683. #endif
  1684. } else {
  1685. show_help_default(topic, par);
  1686. }
  1687. av_freep(&topic);
  1688. return 0;
  1689. }
  1690. int read_yesno(void)
  1691. {
  1692. int c = getchar();
  1693. int yesno = (av_toupper(c) == 'Y');
  1694. while (c != '\n' && c != EOF)
  1695. c = getchar();
  1696. return yesno;
  1697. }
  1698. FILE *get_preset_file(char *filename, size_t filename_size,
  1699. const char *preset_name, int is_path,
  1700. const char *codec_name)
  1701. {
  1702. FILE *f = NULL;
  1703. int i;
  1704. const char *base[3] = { getenv("FFMPEG_DATADIR"),
  1705. getenv("HOME"),
  1706. FFMPEG_DATADIR, };
  1707. if (is_path) {
  1708. av_strlcpy(filename, preset_name, filename_size);
  1709. f = fopen(filename, "r");
  1710. } else {
  1711. #ifdef _WIN32
  1712. char datadir[MAX_PATH], *ls;
  1713. base[2] = NULL;
  1714. if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
  1715. {
  1716. for (ls = datadir; ls < datadir + strlen(datadir); ls++)
  1717. if (*ls == '\\') *ls = '/';
  1718. if (ls = strrchr(datadir, '/'))
  1719. {
  1720. *ls = 0;
  1721. strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
  1722. base[2] = datadir;
  1723. }
  1724. }
  1725. #endif
  1726. for (i = 0; i < 3 && !f; i++) {
  1727. if (!base[i])
  1728. continue;
  1729. snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
  1730. i != 1 ? "" : "/.ffmpeg", preset_name);
  1731. f = fopen(filename, "r");
  1732. if (!f && codec_name) {
  1733. snprintf(filename, filename_size,
  1734. "%s%s/%s-%s.ffpreset",
  1735. base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
  1736. preset_name);
  1737. f = fopen(filename, "r");
  1738. }
  1739. }
  1740. }
  1741. return f;
  1742. }
  1743. int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
  1744. {
  1745. int ret = avformat_match_stream_specifier(s, st, spec);
  1746. if (ret < 0)
  1747. av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
  1748. return ret;
  1749. }
  1750. AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
  1751. AVFormatContext *s, AVStream *st, AVCodec *codec)
  1752. {
  1753. AVDictionary *ret = NULL;
  1754. AVDictionaryEntry *t = NULL;
  1755. int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
  1756. : AV_OPT_FLAG_DECODING_PARAM;
  1757. char prefix = 0;
  1758. const AVClass *cc = avcodec_get_class();
  1759. if (!codec)
  1760. codec = s->oformat ? avcodec_find_encoder(codec_id)
  1761. : avcodec_find_decoder(codec_id);
  1762. switch (st->codecpar->codec_type) {
  1763. case AVMEDIA_TYPE_VIDEO:
  1764. prefix = 'v';
  1765. flags |= AV_OPT_FLAG_VIDEO_PARAM;
  1766. break;
  1767. case AVMEDIA_TYPE_AUDIO:
  1768. prefix = 'a';
  1769. flags |= AV_OPT_FLAG_AUDIO_PARAM;
  1770. break;
  1771. case AVMEDIA_TYPE_SUBTITLE:
  1772. prefix = 's';
  1773. flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
  1774. break;
  1775. }
  1776. while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
  1777. char *p = strchr(t->key, ':');
  1778. /* check stream specification in opt name */
  1779. if (p)
  1780. switch (check_stream_specifier(s, st, p + 1)) {
  1781. case 1: *p = 0; break;
  1782. case 0: continue;
  1783. default: exit_program(1);
  1784. }
  1785. if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
  1786. !codec ||
  1787. (codec->priv_class &&
  1788. av_opt_find(&codec->priv_class, t->key, NULL, flags,
  1789. AV_OPT_SEARCH_FAKE_OBJ)))
  1790. av_dict_set(&ret, t->key, t->value, 0);
  1791. else if (t->key[0] == prefix &&
  1792. av_opt_find(&cc, t->key + 1, NULL, flags,
  1793. AV_OPT_SEARCH_FAKE_OBJ))
  1794. av_dict_set(&ret, t->key + 1, t->value, 0);
  1795. if (p)
  1796. *p = ':';
  1797. }
  1798. return ret;
  1799. }
  1800. AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
  1801. AVDictionary *codec_opts)
  1802. {
  1803. int i;
  1804. AVDictionary **opts;
  1805. if (!s->nb_streams)
  1806. return NULL;
  1807. opts = av_mallocz_array(s->nb_streams, sizeof(*opts));
  1808. if (!opts) {
  1809. av_log(NULL, AV_LOG_ERROR,
  1810. "Could not alloc memory for stream options.\n");
  1811. return NULL;
  1812. }
  1813. for (i = 0; i < s->nb_streams; i++)
  1814. opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id,
  1815. s, s->streams[i], NULL);
  1816. return opts;
  1817. }
  1818. void *grow_array(void *array, int elem_size, int *size, int new_size)
  1819. {
  1820. if (new_size >= INT_MAX / elem_size) {
  1821. av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
  1822. exit_program(1);
  1823. }
  1824. if (*size < new_size) {
  1825. uint8_t *tmp = av_realloc_array(array, new_size, elem_size);
  1826. if (!tmp) {
  1827. av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
  1828. exit_program(1);
  1829. }
  1830. memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
  1831. *size = new_size;
  1832. return tmp;
  1833. }
  1834. return array;
  1835. }
  1836. double get_rotation(AVStream *st)
  1837. {
  1838. uint8_t* displaymatrix = av_stream_get_side_data(st,
  1839. AV_PKT_DATA_DISPLAYMATRIX, NULL);
  1840. double theta = 0;
  1841. if (displaymatrix)
  1842. theta = -av_display_rotation_get((int32_t*) displaymatrix);
  1843. theta -= 360*floor(theta/360 + 0.9/360);
  1844. if (fabs(theta - 90*round(theta/90)) > 2)
  1845. av_log(NULL, AV_LOG_WARNING, "Odd rotation angle.\n"
  1846. "If you want to help, upload a sample "
  1847. "of this file to ftp://upload.ffmpeg.org/incoming/ "
  1848. "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)");
  1849. return theta;
  1850. }
  1851. #if CONFIG_AVDEVICE
  1852. static int print_device_sources(AVInputFormat *fmt, AVDictionary *opts)
  1853. {
  1854. int ret, i;
  1855. AVDeviceInfoList *device_list = NULL;
  1856. if (!fmt || !fmt->priv_class || !AV_IS_INPUT_DEVICE(fmt->priv_class->category))
  1857. return AVERROR(EINVAL);
  1858. printf("Auto-detected sources for %s:\n", fmt->name);
  1859. if (!fmt->get_device_list) {
  1860. ret = AVERROR(ENOSYS);
  1861. printf("Cannot list sources. Not implemented.\n");
  1862. goto fail;
  1863. }
  1864. if ((ret = avdevice_list_input_sources(fmt, NULL, opts, &device_list)) < 0) {
  1865. printf("Cannot list sources.\n");
  1866. goto fail;
  1867. }
  1868. for (i = 0; i < device_list->nb_devices; i++) {
  1869. printf("%s %s [%s]\n", device_list->default_device == i ? "*" : " ",
  1870. device_list->devices[i]->device_name, device_list->devices[i]->device_description);
  1871. }
  1872. fail:
  1873. avdevice_free_list_devices(&device_list);
  1874. return ret;
  1875. }
  1876. static int print_device_sinks(AVOutputFormat *fmt, AVDictionary *opts)
  1877. {
  1878. int ret, i;
  1879. AVDeviceInfoList *device_list = NULL;
  1880. if (!fmt || !fmt->priv_class || !AV_IS_OUTPUT_DEVICE(fmt->priv_class->category))
  1881. return AVERROR(EINVAL);
  1882. printf("Auto-detected sinks for %s:\n", fmt->name);
  1883. if (!fmt->get_device_list) {
  1884. ret = AVERROR(ENOSYS);
  1885. printf("Cannot list sinks. Not implemented.\n");
  1886. goto fail;
  1887. }
  1888. if ((ret = avdevice_list_output_sinks(fmt, NULL, opts, &device_list)) < 0) {
  1889. printf("Cannot list sinks.\n");
  1890. goto fail;
  1891. }
  1892. for (i = 0; i < device_list->nb_devices; i++) {
  1893. printf("%s %s [%s]\n", device_list->default_device == i ? "*" : " ",
  1894. device_list->devices[i]->device_name, device_list->devices[i]->device_description);
  1895. }
  1896. fail:
  1897. avdevice_free_list_devices(&device_list);
  1898. return ret;
  1899. }
  1900. static int show_sinks_sources_parse_arg(const char *arg, char **dev, AVDictionary **opts)
  1901. {
  1902. int ret;
  1903. if (arg) {
  1904. char *opts_str = NULL;
  1905. av_assert0(dev && opts);
  1906. *dev = av_strdup(arg);
  1907. if (!*dev)
  1908. return AVERROR(ENOMEM);
  1909. if ((opts_str = strchr(*dev, ','))) {
  1910. *(opts_str++) = '\0';
  1911. if (opts_str[0] && ((ret = av_dict_parse_string(opts, opts_str, "=", ":", 0)) < 0)) {
  1912. av_freep(dev);
  1913. return ret;
  1914. }
  1915. }
  1916. } else
  1917. printf("\nDevice name is not provided.\n"
  1918. "You can pass devicename[,opt1=val1[,opt2=val2...]] as an argument.\n\n");
  1919. return 0;
  1920. }
  1921. int show_sources(void *optctx, const char *opt, const char *arg)
  1922. {
  1923. AVInputFormat *fmt = NULL;
  1924. char *dev = NULL;
  1925. AVDictionary *opts = NULL;
  1926. int ret = 0;
  1927. int error_level = av_log_get_level();
  1928. av_log_set_level(AV_LOG_ERROR);
  1929. if ((ret = show_sinks_sources_parse_arg(arg, &dev, &opts)) < 0)
  1930. goto fail;
  1931. do {
  1932. fmt = av_input_audio_device_next(fmt);
  1933. if (fmt) {
  1934. if (!strcmp(fmt->name, "lavfi"))
  1935. continue; //it's pointless to probe lavfi
  1936. if (dev && !av_match_name(dev, fmt->name))
  1937. continue;
  1938. print_device_sources(fmt, opts);
  1939. }
  1940. } while (fmt);
  1941. do {
  1942. fmt = av_input_video_device_next(fmt);
  1943. if (fmt) {
  1944. if (dev && !av_match_name(dev, fmt->name))
  1945. continue;
  1946. print_device_sources(fmt, opts);
  1947. }
  1948. } while (fmt);
  1949. fail:
  1950. av_dict_free(&opts);
  1951. av_free(dev);
  1952. av_log_set_level(error_level);
  1953. return ret;
  1954. }
  1955. int show_sinks(void *optctx, const char *opt, const char *arg)
  1956. {
  1957. AVOutputFormat *fmt = NULL;
  1958. char *dev = NULL;
  1959. AVDictionary *opts = NULL;
  1960. int ret = 0;
  1961. int error_level = av_log_get_level();
  1962. av_log_set_level(AV_LOG_ERROR);
  1963. if ((ret = show_sinks_sources_parse_arg(arg, &dev, &opts)) < 0)
  1964. goto fail;
  1965. do {
  1966. fmt = av_output_audio_device_next(fmt);
  1967. if (fmt) {
  1968. if (dev && !av_match_name(dev, fmt->name))
  1969. continue;
  1970. print_device_sinks(fmt, opts);
  1971. }
  1972. } while (fmt);
  1973. do {
  1974. fmt = av_output_video_device_next(fmt);
  1975. if (fmt) {
  1976. if (dev && !av_match_name(dev, fmt->name))
  1977. continue;
  1978. print_device_sinks(fmt, opts);
  1979. }
  1980. } while (fmt);
  1981. fail:
  1982. av_dict_free(&opts);
  1983. av_free(dev);
  1984. av_log_set_level(error_level);
  1985. return ret;
  1986. }
  1987. #endif