cmdutils.c 74 KB

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