cmdutils.c 75 KB

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