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