cmdutils.c 71 KB

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