cmdutils.c 74 KB

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