opt_common.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  1. /*
  2. * Option handlers shared between the tools.
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "config.h"
  21. #include <stdio.h>
  22. #include "cmdutils.h"
  23. #include "opt_common.h"
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/bprint.h"
  27. #include "libavutil/channel_layout.h"
  28. #include "libavutil/cpu.h"
  29. #include "libavutil/dict.h"
  30. #include "libavutil/error.h"
  31. #include "libavutil/ffversion.h"
  32. #include "libavutil/log.h"
  33. #include "libavutil/mem.h"
  34. #include "libavutil/parseutils.h"
  35. #include "libavutil/pixdesc.h"
  36. #include "libavutil/version.h"
  37. #include "libavcodec/avcodec.h"
  38. #include "libavcodec/bsf.h"
  39. #include "libavcodec/codec.h"
  40. #include "libavcodec/codec_desc.h"
  41. #include "libavcodec/version.h"
  42. #include "libavformat/avformat.h"
  43. #include "libavformat/version.h"
  44. #include "libavdevice/avdevice.h"
  45. #include "libavdevice/version.h"
  46. #include "libavfilter/avfilter.h"
  47. #include "libavfilter/version.h"
  48. #include "libswscale/swscale.h"
  49. #include "libswscale/version.h"
  50. #include "libswresample/swresample.h"
  51. #include "libswresample/version.h"
  52. #include "libpostproc/postprocess.h"
  53. #include "libpostproc/version.h"
  54. enum show_muxdemuxers {
  55. SHOW_DEFAULT,
  56. SHOW_DEMUXERS,
  57. SHOW_MUXERS,
  58. };
  59. static FILE *report_file;
  60. static int report_file_level = AV_LOG_DEBUG;
  61. int show_license(void *optctx, const char *opt, const char *arg)
  62. {
  63. #if CONFIG_NONFREE
  64. printf(
  65. "This version of %s has nonfree parts compiled in.\n"
  66. "Therefore it is not legally redistributable.\n",
  67. program_name );
  68. #elif CONFIG_GPLV3
  69. printf(
  70. "%s is free software; you can redistribute it and/or modify\n"
  71. "it under the terms of the GNU General Public License as published by\n"
  72. "the Free Software Foundation; either version 3 of the License, or\n"
  73. "(at your option) any later version.\n"
  74. "\n"
  75. "%s is distributed in the hope that it will be useful,\n"
  76. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  77. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  78. "GNU General Public License for more details.\n"
  79. "\n"
  80. "You should have received a copy of the GNU General Public License\n"
  81. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  82. program_name, program_name, program_name );
  83. #elif CONFIG_GPL
  84. printf(
  85. "%s is free software; you can redistribute it and/or modify\n"
  86. "it under the terms of the GNU General Public License as published by\n"
  87. "the Free Software Foundation; either version 2 of the License, or\n"
  88. "(at your option) any later version.\n"
  89. "\n"
  90. "%s is distributed in the hope that it will be useful,\n"
  91. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  92. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  93. "GNU General Public License for more details.\n"
  94. "\n"
  95. "You should have received a copy of the GNU General Public License\n"
  96. "along with %s; if not, write to the Free Software\n"
  97. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  98. program_name, program_name, program_name );
  99. #elif CONFIG_LGPLV3
  100. printf(
  101. "%s is free software; you can redistribute it and/or modify\n"
  102. "it under the terms of the GNU Lesser General Public License as published by\n"
  103. "the Free Software Foundation; either version 3 of the License, or\n"
  104. "(at your option) any later version.\n"
  105. "\n"
  106. "%s is distributed in the hope that it will be useful,\n"
  107. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  108. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  109. "GNU Lesser General Public License for more details.\n"
  110. "\n"
  111. "You should have received a copy of the GNU Lesser General Public License\n"
  112. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  113. program_name, program_name, program_name );
  114. #else
  115. printf(
  116. "%s is free software; you can redistribute it and/or\n"
  117. "modify it under the terms of the GNU Lesser General Public\n"
  118. "License as published by the Free Software Foundation; either\n"
  119. "version 2.1 of the License, or (at your option) any later version.\n"
  120. "\n"
  121. "%s is distributed in the hope that it will be useful,\n"
  122. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  123. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  124. "Lesser General Public License for more details.\n"
  125. "\n"
  126. "You should have received a copy of the GNU Lesser General Public\n"
  127. "License along with %s; if not, write to the Free Software\n"
  128. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  129. program_name, program_name, program_name );
  130. #endif
  131. return 0;
  132. }
  133. static int warned_cfg = 0;
  134. #define INDENT 1
  135. #define SHOW_VERSION 2
  136. #define SHOW_CONFIG 4
  137. #define SHOW_COPYRIGHT 8
  138. #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
  139. if (CONFIG_##LIBNAME) { \
  140. const char *indent = flags & INDENT? " " : ""; \
  141. if (flags & SHOW_VERSION) { \
  142. unsigned int version = libname##_version(); \
  143. av_log(NULL, level, \
  144. "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
  145. indent, #libname, \
  146. LIB##LIBNAME##_VERSION_MAJOR, \
  147. LIB##LIBNAME##_VERSION_MINOR, \
  148. LIB##LIBNAME##_VERSION_MICRO, \
  149. AV_VERSION_MAJOR(version), AV_VERSION_MINOR(version),\
  150. AV_VERSION_MICRO(version)); \
  151. } \
  152. if (flags & SHOW_CONFIG) { \
  153. const char *cfg = libname##_configuration(); \
  154. if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
  155. if (!warned_cfg) { \
  156. av_log(NULL, level, \
  157. "%sWARNING: library configuration mismatch\n", \
  158. indent); \
  159. warned_cfg = 1; \
  160. } \
  161. av_log(NULL, level, "%s%-11s configuration: %s\n", \
  162. indent, #libname, cfg); \
  163. } \
  164. } \
  165. } \
  166. static void print_all_libs_info(int flags, int level)
  167. {
  168. PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
  169. PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
  170. PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
  171. PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
  172. PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
  173. PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
  174. PRINT_LIB_INFO(swresample, SWRESAMPLE, flags, level);
  175. PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
  176. }
  177. static void print_program_info(int flags, int level)
  178. {
  179. const char *indent = flags & INDENT? " " : "";
  180. av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
  181. if (flags & SHOW_COPYRIGHT)
  182. av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
  183. program_birth_year, CONFIG_THIS_YEAR);
  184. av_log(NULL, level, "\n");
  185. av_log(NULL, level, "%sbuilt with %s\n", indent, CC_IDENT);
  186. av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
  187. }
  188. static void print_buildconf(int flags, int level)
  189. {
  190. const char *indent = flags & INDENT ? " " : "";
  191. char str[] = { FFMPEG_CONFIGURATION };
  192. char *conflist, *remove_tilde, *splitconf;
  193. // Change all the ' --' strings to '~--' so that
  194. // they can be identified as tokens.
  195. while ((conflist = strstr(str, " --")) != NULL) {
  196. conflist[0] = '~';
  197. }
  198. // Compensate for the weirdness this would cause
  199. // when passing 'pkg-config --static'.
  200. while ((remove_tilde = strstr(str, "pkg-config~")) != NULL) {
  201. remove_tilde[sizeof("pkg-config~") - 2] = ' ';
  202. }
  203. splitconf = strtok(str, "~");
  204. av_log(NULL, level, "\n%sconfiguration:\n", indent);
  205. while (splitconf != NULL) {
  206. av_log(NULL, level, "%s%s%s\n", indent, indent, splitconf);
  207. splitconf = strtok(NULL, "~");
  208. }
  209. }
  210. void show_banner(int argc, char **argv, const OptionDef *options)
  211. {
  212. int idx = locate_option(argc, argv, options, "version");
  213. if (hide_banner || idx)
  214. return;
  215. print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
  216. print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
  217. print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
  218. }
  219. int show_version(void *optctx, const char *opt, const char *arg)
  220. {
  221. av_log_set_callback(log_callback_help);
  222. print_program_info (SHOW_COPYRIGHT, AV_LOG_INFO);
  223. print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
  224. return 0;
  225. }
  226. int show_buildconf(void *optctx, const char *opt, const char *arg)
  227. {
  228. av_log_set_callback(log_callback_help);
  229. print_buildconf (INDENT|0, AV_LOG_INFO);
  230. return 0;
  231. }
  232. #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
  233. if (codec->field) { \
  234. const type *p = codec->field; \
  235. \
  236. printf(" Supported " list_name ":"); \
  237. while (*p != term) { \
  238. get_name(*p); \
  239. printf(" %s", name); \
  240. p++; \
  241. } \
  242. printf("\n"); \
  243. } \
  244. static void print_codec(const AVCodec *c)
  245. {
  246. int encoder = av_codec_is_encoder(c);
  247. printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
  248. c->long_name ? c->long_name : "");
  249. printf(" General capabilities: ");
  250. if (c->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND)
  251. printf("horizband ");
  252. if (c->capabilities & AV_CODEC_CAP_DR1)
  253. printf("dr1 ");
  254. if (c->capabilities & AV_CODEC_CAP_DELAY)
  255. printf("delay ");
  256. if (c->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)
  257. printf("small ");
  258. if (c->capabilities & AV_CODEC_CAP_EXPERIMENTAL)
  259. printf("exp ");
  260. if (c->capabilities & AV_CODEC_CAP_CHANNEL_CONF)
  261. printf("chconf ");
  262. if (c->capabilities & AV_CODEC_CAP_PARAM_CHANGE)
  263. printf("paramchange ");
  264. if (c->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
  265. printf("variable ");
  266. if (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
  267. AV_CODEC_CAP_SLICE_THREADS |
  268. AV_CODEC_CAP_OTHER_THREADS))
  269. printf("threads ");
  270. if (c->capabilities & AV_CODEC_CAP_AVOID_PROBING)
  271. printf("avoidprobe ");
  272. if (c->capabilities & AV_CODEC_CAP_HARDWARE)
  273. printf("hardware ");
  274. if (c->capabilities & AV_CODEC_CAP_HYBRID)
  275. printf("hybrid ");
  276. if (!c->capabilities)
  277. printf("none");
  278. printf("\n");
  279. if (c->type == AVMEDIA_TYPE_VIDEO ||
  280. c->type == AVMEDIA_TYPE_AUDIO) {
  281. printf(" Threading capabilities: ");
  282. switch (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
  283. AV_CODEC_CAP_SLICE_THREADS |
  284. AV_CODEC_CAP_OTHER_THREADS)) {
  285. case AV_CODEC_CAP_FRAME_THREADS |
  286. AV_CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
  287. case AV_CODEC_CAP_FRAME_THREADS: printf("frame"); break;
  288. case AV_CODEC_CAP_SLICE_THREADS: printf("slice"); break;
  289. case AV_CODEC_CAP_OTHER_THREADS: printf("other"); break;
  290. default: printf("none"); break;
  291. }
  292. printf("\n");
  293. }
  294. if (avcodec_get_hw_config(c, 0)) {
  295. printf(" Supported hardware devices: ");
  296. for (int i = 0;; i++) {
  297. const AVCodecHWConfig *config = avcodec_get_hw_config(c, i);
  298. const char *name;
  299. if (!config)
  300. break;
  301. name = av_hwdevice_get_type_name(config->device_type);
  302. if (name)
  303. printf("%s ", name);
  304. }
  305. printf("\n");
  306. }
  307. if (c->supported_framerates) {
  308. const AVRational *fps = c->supported_framerates;
  309. printf(" Supported framerates:");
  310. while (fps->num) {
  311. printf(" %d/%d", fps->num, fps->den);
  312. fps++;
  313. }
  314. printf("\n");
  315. }
  316. PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
  317. AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
  318. PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
  319. GET_SAMPLE_RATE_NAME);
  320. PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
  321. AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
  322. if (c->ch_layouts) {
  323. const AVChannelLayout *p = c->ch_layouts;
  324. printf(" Supported channel layouts:");
  325. while (p->nb_channels) {
  326. char name[128];
  327. av_channel_layout_describe(p, name, sizeof(name));
  328. printf(" %s", name);
  329. p++;
  330. }
  331. printf("\n");
  332. }
  333. if (c->priv_class) {
  334. show_help_children(c->priv_class,
  335. AV_OPT_FLAG_ENCODING_PARAM |
  336. AV_OPT_FLAG_DECODING_PARAM);
  337. }
  338. }
  339. static const AVCodec *next_codec_for_id(enum AVCodecID id, void **iter,
  340. int encoder)
  341. {
  342. const AVCodec *c;
  343. while ((c = av_codec_iterate(iter))) {
  344. if (c->id == id &&
  345. (encoder ? av_codec_is_encoder(c) : av_codec_is_decoder(c)))
  346. return c;
  347. }
  348. return NULL;
  349. }
  350. static void show_help_codec(const char *name, int encoder)
  351. {
  352. const AVCodecDescriptor *desc;
  353. const AVCodec *codec;
  354. if (!name) {
  355. av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
  356. return;
  357. }
  358. codec = encoder ? avcodec_find_encoder_by_name(name) :
  359. avcodec_find_decoder_by_name(name);
  360. if (codec)
  361. print_codec(codec);
  362. else if ((desc = avcodec_descriptor_get_by_name(name))) {
  363. void *iter = NULL;
  364. int printed = 0;
  365. while ((codec = next_codec_for_id(desc->id, &iter, encoder))) {
  366. printed = 1;
  367. print_codec(codec);
  368. }
  369. if (!printed) {
  370. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, "
  371. "but no %s for it are available. FFmpeg might need to be "
  372. "recompiled with additional external libraries.\n",
  373. name, encoder ? "encoders" : "decoders");
  374. }
  375. } else {
  376. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n",
  377. name);
  378. }
  379. }
  380. static void show_help_demuxer(const char *name)
  381. {
  382. const AVInputFormat *fmt = av_find_input_format(name);
  383. if (!fmt) {
  384. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  385. return;
  386. }
  387. printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
  388. if (fmt->extensions)
  389. printf(" Common extensions: %s.\n", fmt->extensions);
  390. if (fmt->priv_class)
  391. show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
  392. }
  393. static void show_help_protocol(const char *name)
  394. {
  395. const AVClass *proto_class;
  396. if (!name) {
  397. av_log(NULL, AV_LOG_ERROR, "No protocol name specified.\n");
  398. return;
  399. }
  400. proto_class = avio_protocol_get_class(name);
  401. if (!proto_class) {
  402. av_log(NULL, AV_LOG_ERROR, "Unknown protocol '%s'.\n", name);
  403. return;
  404. }
  405. show_help_children(proto_class, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM);
  406. }
  407. static void show_help_muxer(const char *name)
  408. {
  409. const AVCodecDescriptor *desc;
  410. const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
  411. if (!fmt) {
  412. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  413. return;
  414. }
  415. printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
  416. if (fmt->extensions)
  417. printf(" Common extensions: %s.\n", fmt->extensions);
  418. if (fmt->mime_type)
  419. printf(" Mime type: %s.\n", fmt->mime_type);
  420. if (fmt->video_codec != AV_CODEC_ID_NONE &&
  421. (desc = avcodec_descriptor_get(fmt->video_codec))) {
  422. printf(" Default video codec: %s.\n", desc->name);
  423. }
  424. if (fmt->audio_codec != AV_CODEC_ID_NONE &&
  425. (desc = avcodec_descriptor_get(fmt->audio_codec))) {
  426. printf(" Default audio codec: %s.\n", desc->name);
  427. }
  428. if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
  429. (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
  430. printf(" Default subtitle codec: %s.\n", desc->name);
  431. }
  432. if (fmt->priv_class)
  433. show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
  434. }
  435. #if CONFIG_AVFILTER
  436. static void show_help_filter(const char *name)
  437. {
  438. #if CONFIG_AVFILTER
  439. const AVFilter *f = avfilter_get_by_name(name);
  440. int i, count;
  441. if (!name) {
  442. av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
  443. return;
  444. } else if (!f) {
  445. av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
  446. return;
  447. }
  448. printf("Filter %s\n", f->name);
  449. if (f->description)
  450. printf(" %s\n", f->description);
  451. if (f->flags & AVFILTER_FLAG_SLICE_THREADS)
  452. printf(" slice threading supported\n");
  453. printf(" Inputs:\n");
  454. count = avfilter_filter_pad_count(f, 0);
  455. for (i = 0; i < count; i++) {
  456. printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
  457. av_get_media_type_string(avfilter_pad_get_type(f->inputs, i)));
  458. }
  459. if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
  460. printf(" dynamic (depending on the options)\n");
  461. else if (!count)
  462. printf(" none (source filter)\n");
  463. printf(" Outputs:\n");
  464. count = avfilter_filter_pad_count(f, 1);
  465. for (i = 0; i < count; i++) {
  466. printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
  467. av_get_media_type_string(avfilter_pad_get_type(f->outputs, i)));
  468. }
  469. if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
  470. printf(" dynamic (depending on the options)\n");
  471. else if (!count)
  472. printf(" none (sink filter)\n");
  473. if (f->priv_class)
  474. show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM |
  475. AV_OPT_FLAG_AUDIO_PARAM);
  476. if (f->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)
  477. printf("This filter has support for timeline through the 'enable' option.\n");
  478. #else
  479. av_log(NULL, AV_LOG_ERROR, "Build without libavfilter; "
  480. "can not to satisfy request\n");
  481. #endif
  482. }
  483. #endif
  484. static void show_help_bsf(const char *name)
  485. {
  486. const AVBitStreamFilter *bsf = av_bsf_get_by_name(name);
  487. if (!name) {
  488. av_log(NULL, AV_LOG_ERROR, "No bitstream filter name specified.\n");
  489. return;
  490. } else if (!bsf) {
  491. av_log(NULL, AV_LOG_ERROR, "Unknown bit stream filter '%s'.\n", name);
  492. return;
  493. }
  494. printf("Bit stream filter %s\n", bsf->name);
  495. PRINT_CODEC_SUPPORTED(bsf, codec_ids, enum AVCodecID, "codecs",
  496. AV_CODEC_ID_NONE, GET_CODEC_NAME);
  497. if (bsf->priv_class)
  498. show_help_children(bsf->priv_class, AV_OPT_FLAG_BSF_PARAM);
  499. }
  500. int show_help(void *optctx, const char *opt, const char *arg)
  501. {
  502. char *topic, *par;
  503. av_log_set_callback(log_callback_help);
  504. topic = av_strdup(arg ? arg : "");
  505. if (!topic)
  506. return AVERROR(ENOMEM);
  507. par = strchr(topic, '=');
  508. if (par)
  509. *par++ = 0;
  510. if (!*topic) {
  511. show_help_default(topic, par);
  512. } else if (!strcmp(topic, "decoder")) {
  513. show_help_codec(par, 0);
  514. } else if (!strcmp(topic, "encoder")) {
  515. show_help_codec(par, 1);
  516. } else if (!strcmp(topic, "demuxer")) {
  517. show_help_demuxer(par);
  518. } else if (!strcmp(topic, "muxer")) {
  519. show_help_muxer(par);
  520. } else if (!strcmp(topic, "protocol")) {
  521. show_help_protocol(par);
  522. #if CONFIG_AVFILTER
  523. } else if (!strcmp(topic, "filter")) {
  524. show_help_filter(par);
  525. #endif
  526. } else if (!strcmp(topic, "bsf")) {
  527. show_help_bsf(par);
  528. } else {
  529. show_help_default(topic, par);
  530. }
  531. av_freep(&topic);
  532. return 0;
  533. }
  534. static void print_codecs_for_id(enum AVCodecID id, int encoder)
  535. {
  536. void *iter = NULL;
  537. const AVCodec *codec;
  538. printf(" (%s:", encoder ? "encoders" : "decoders");
  539. while ((codec = next_codec_for_id(id, &iter, encoder)))
  540. printf(" %s", codec->name);
  541. printf(")");
  542. }
  543. static int compare_codec_desc(const void *a, const void *b)
  544. {
  545. const AVCodecDescriptor * const *da = a;
  546. const AVCodecDescriptor * const *db = b;
  547. return (*da)->type != (*db)->type ? FFDIFFSIGN((*da)->type, (*db)->type) :
  548. strcmp((*da)->name, (*db)->name);
  549. }
  550. static int get_codecs_sorted(const AVCodecDescriptor ***rcodecs)
  551. {
  552. const AVCodecDescriptor *desc = NULL;
  553. const AVCodecDescriptor **codecs;
  554. unsigned nb_codecs = 0, i = 0;
  555. while ((desc = avcodec_descriptor_next(desc)))
  556. nb_codecs++;
  557. if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs))))
  558. return AVERROR(ENOMEM);
  559. desc = NULL;
  560. while ((desc = avcodec_descriptor_next(desc)))
  561. codecs[i++] = desc;
  562. av_assert0(i == nb_codecs);
  563. qsort(codecs, nb_codecs, sizeof(*codecs), compare_codec_desc);
  564. *rcodecs = codecs;
  565. return nb_codecs;
  566. }
  567. static char get_media_type_char(enum AVMediaType type)
  568. {
  569. switch (type) {
  570. case AVMEDIA_TYPE_VIDEO: return 'V';
  571. case AVMEDIA_TYPE_AUDIO: return 'A';
  572. case AVMEDIA_TYPE_DATA: return 'D';
  573. case AVMEDIA_TYPE_SUBTITLE: return 'S';
  574. case AVMEDIA_TYPE_ATTACHMENT:return 'T';
  575. default: return '?';
  576. }
  577. }
  578. int show_codecs(void *optctx, const char *opt, const char *arg)
  579. {
  580. const AVCodecDescriptor **codecs;
  581. unsigned i;
  582. int nb_codecs = get_codecs_sorted(&codecs);
  583. if (nb_codecs < 0)
  584. return nb_codecs;
  585. printf("Codecs:\n"
  586. " D..... = Decoding supported\n"
  587. " .E.... = Encoding supported\n"
  588. " ..V... = Video codec\n"
  589. " ..A... = Audio codec\n"
  590. " ..S... = Subtitle codec\n"
  591. " ..D... = Data codec\n"
  592. " ..T... = Attachment codec\n"
  593. " ...I.. = Intra frame-only codec\n"
  594. " ....L. = Lossy compression\n"
  595. " .....S = Lossless compression\n"
  596. " -------\n");
  597. for (i = 0; i < nb_codecs; i++) {
  598. const AVCodecDescriptor *desc = codecs[i];
  599. const AVCodec *codec;
  600. void *iter = NULL;
  601. if (strstr(desc->name, "_deprecated"))
  602. continue;
  603. printf(" %c%c%c%c%c%c",
  604. avcodec_find_decoder(desc->id) ? 'D' : '.',
  605. avcodec_find_encoder(desc->id) ? 'E' : '.',
  606. get_media_type_char(desc->type),
  607. (desc->props & AV_CODEC_PROP_INTRA_ONLY) ? 'I' : '.',
  608. (desc->props & AV_CODEC_PROP_LOSSY) ? 'L' : '.',
  609. (desc->props & AV_CODEC_PROP_LOSSLESS) ? 'S' : '.');
  610. printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
  611. /* print decoders/encoders when there's more than one or their
  612. * names are different from codec name */
  613. while ((codec = next_codec_for_id(desc->id, &iter, 0))) {
  614. if (strcmp(codec->name, desc->name)) {
  615. print_codecs_for_id(desc->id, 0);
  616. break;
  617. }
  618. }
  619. iter = NULL;
  620. while ((codec = next_codec_for_id(desc->id, &iter, 1))) {
  621. if (strcmp(codec->name, desc->name)) {
  622. print_codecs_for_id(desc->id, 1);
  623. break;
  624. }
  625. }
  626. printf("\n");
  627. }
  628. av_free(codecs);
  629. return 0;
  630. }
  631. static int print_codecs(int encoder)
  632. {
  633. const AVCodecDescriptor **codecs;
  634. int i, nb_codecs = get_codecs_sorted(&codecs);
  635. if (nb_codecs < 0)
  636. return nb_codecs;
  637. printf("%s:\n"
  638. " V..... = Video\n"
  639. " A..... = Audio\n"
  640. " S..... = Subtitle\n"
  641. " .F.... = Frame-level multithreading\n"
  642. " ..S... = Slice-level multithreading\n"
  643. " ...X.. = Codec is experimental\n"
  644. " ....B. = Supports draw_horiz_band\n"
  645. " .....D = Supports direct rendering method 1\n"
  646. " ------\n",
  647. encoder ? "Encoders" : "Decoders");
  648. for (i = 0; i < nb_codecs; i++) {
  649. const AVCodecDescriptor *desc = codecs[i];
  650. const AVCodec *codec;
  651. void *iter = NULL;
  652. while ((codec = next_codec_for_id(desc->id, &iter, encoder))) {
  653. printf(" %c%c%c%c%c%c",
  654. get_media_type_char(desc->type),
  655. (codec->capabilities & AV_CODEC_CAP_FRAME_THREADS) ? 'F' : '.',
  656. (codec->capabilities & AV_CODEC_CAP_SLICE_THREADS) ? 'S' : '.',
  657. (codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) ? 'X' : '.',
  658. (codec->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND) ? 'B' : '.',
  659. (codec->capabilities & AV_CODEC_CAP_DR1) ? 'D' : '.');
  660. printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
  661. if (strcmp(codec->name, desc->name))
  662. printf(" (codec %s)", desc->name);
  663. printf("\n");
  664. }
  665. }
  666. av_free(codecs);
  667. return 0;
  668. }
  669. int show_decoders(void *optctx, const char *opt, const char *arg)
  670. {
  671. return print_codecs(0);
  672. }
  673. int show_encoders(void *optctx, const char *opt, const char *arg)
  674. {
  675. return print_codecs(1);
  676. }
  677. int show_bsfs(void *optctx, const char *opt, const char *arg)
  678. {
  679. const AVBitStreamFilter *bsf = NULL;
  680. void *opaque = NULL;
  681. printf("Bitstream filters:\n");
  682. while ((bsf = av_bsf_iterate(&opaque)))
  683. printf("%s\n", bsf->name);
  684. printf("\n");
  685. return 0;
  686. }
  687. int show_filters(void *optctx, const char *opt, const char *arg)
  688. {
  689. #if CONFIG_AVFILTER
  690. const AVFilter *filter = NULL;
  691. char descr[64], *descr_cur;
  692. void *opaque = NULL;
  693. int i, j;
  694. const AVFilterPad *pad;
  695. printf("Filters:\n"
  696. " T.. = Timeline support\n"
  697. " .S. = Slice threading\n"
  698. " ..C = Command support\n"
  699. " A = Audio input/output\n"
  700. " V = Video input/output\n"
  701. " N = Dynamic number and/or type of input/output\n"
  702. " | = Source or sink filter\n");
  703. while ((filter = av_filter_iterate(&opaque))) {
  704. descr_cur = descr;
  705. for (i = 0; i < 2; i++) {
  706. unsigned nb_pads;
  707. if (i) {
  708. *(descr_cur++) = '-';
  709. *(descr_cur++) = '>';
  710. }
  711. pad = i ? filter->outputs : filter->inputs;
  712. nb_pads = avfilter_filter_pad_count(filter, i);
  713. for (j = 0; j < nb_pads; j++) {
  714. if (descr_cur >= descr + sizeof(descr) - 4)
  715. break;
  716. *(descr_cur++) = get_media_type_char(avfilter_pad_get_type(pad, j));
  717. }
  718. if (!j)
  719. *(descr_cur++) = ((!i && (filter->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)) ||
  720. ( i && (filter->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS))) ? 'N' : '|';
  721. }
  722. *descr_cur = 0;
  723. printf(" %c%c%c %-17s %-10s %s\n",
  724. filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE ? 'T' : '.',
  725. filter->flags & AVFILTER_FLAG_SLICE_THREADS ? 'S' : '.',
  726. filter->process_command ? 'C' : '.',
  727. filter->name, descr, filter->description);
  728. }
  729. #else
  730. printf("No filters available: libavfilter disabled\n");
  731. #endif
  732. return 0;
  733. }
  734. static int is_device(const AVClass *avclass)
  735. {
  736. if (!avclass)
  737. return 0;
  738. return AV_IS_INPUT_DEVICE(avclass->category) || AV_IS_OUTPUT_DEVICE(avclass->category);
  739. }
  740. static int show_formats_devices(void *optctx, const char *opt, const char *arg, int device_only, int muxdemuxers)
  741. {
  742. void *ifmt_opaque = NULL;
  743. const AVInputFormat *ifmt = NULL;
  744. void *ofmt_opaque = NULL;
  745. const AVOutputFormat *ofmt = NULL;
  746. const char *last_name;
  747. int is_dev;
  748. const char *is_device_placeholder = device_only ? "" : ".";
  749. printf("%s:\n"
  750. " D.%s = Demuxing supported\n"
  751. " .E%s = Muxing supported\n"
  752. "%s"
  753. " ---\n",
  754. device_only ? "Devices" : "Formats",
  755. is_device_placeholder, is_device_placeholder,
  756. device_only ? "": " ..d = Is a device\n");
  757. last_name = "000";
  758. for (;;) {
  759. int decode = 0;
  760. int encode = 0;
  761. int device = 0;
  762. const char *name = NULL;
  763. const char *long_name = NULL;
  764. if (muxdemuxers !=SHOW_DEMUXERS) {
  765. ofmt_opaque = NULL;
  766. while ((ofmt = av_muxer_iterate(&ofmt_opaque))) {
  767. is_dev = is_device(ofmt->priv_class);
  768. if (!is_dev && device_only)
  769. continue;
  770. if ((!name || strcmp(ofmt->name, name) < 0) &&
  771. strcmp(ofmt->name, last_name) > 0) {
  772. name = ofmt->name;
  773. long_name = ofmt->long_name;
  774. encode = 1;
  775. device = is_dev;
  776. }
  777. }
  778. }
  779. if (muxdemuxers != SHOW_MUXERS) {
  780. ifmt_opaque = NULL;
  781. while ((ifmt = av_demuxer_iterate(&ifmt_opaque))) {
  782. is_dev = is_device(ifmt->priv_class);
  783. if (!is_dev && device_only)
  784. continue;
  785. if ((!name || strcmp(ifmt->name, name) < 0) &&
  786. strcmp(ifmt->name, last_name) > 0) {
  787. name = ifmt->name;
  788. long_name = ifmt->long_name;
  789. encode = 0;
  790. device = is_dev;
  791. }
  792. if (name && strcmp(ifmt->name, name) == 0) {
  793. decode = 1;
  794. device = is_dev;
  795. }
  796. }
  797. }
  798. if (!name)
  799. break;
  800. last_name = name;
  801. printf(" %c%c%s %-15s %s\n",
  802. decode ? 'D' : ' ',
  803. encode ? 'E' : ' ',
  804. device_only ? "" : (device ? "d" : " "),
  805. name,
  806. long_name ? long_name : " ");
  807. }
  808. return 0;
  809. }
  810. int show_formats(void *optctx, const char *opt, const char *arg)
  811. {
  812. return show_formats_devices(optctx, opt, arg, 0, SHOW_DEFAULT);
  813. }
  814. int show_muxers(void *optctx, const char *opt, const char *arg)
  815. {
  816. return show_formats_devices(optctx, opt, arg, 0, SHOW_MUXERS);
  817. }
  818. int show_demuxers(void *optctx, const char *opt, const char *arg)
  819. {
  820. return show_formats_devices(optctx, opt, arg, 0, SHOW_DEMUXERS);
  821. }
  822. int show_devices(void *optctx, const char *opt, const char *arg)
  823. {
  824. return show_formats_devices(optctx, opt, arg, 1, SHOW_DEFAULT);
  825. }
  826. int show_protocols(void *optctx, const char *opt, const char *arg)
  827. {
  828. void *opaque = NULL;
  829. const char *name;
  830. printf("Supported file protocols:\n"
  831. "Input:\n");
  832. while ((name = avio_enum_protocols(&opaque, 0)))
  833. printf(" %s\n", name);
  834. printf("Output:\n");
  835. while ((name = avio_enum_protocols(&opaque, 1)))
  836. printf(" %s\n", name);
  837. return 0;
  838. }
  839. int show_colors(void *optctx, const char *opt, const char *arg)
  840. {
  841. const char *name;
  842. const uint8_t *rgb;
  843. int i;
  844. printf("%-32s #RRGGBB\n", "name");
  845. for (i = 0; name = av_get_known_color_name(i, &rgb); i++)
  846. printf("%-32s #%02x%02x%02x\n", name, rgb[0], rgb[1], rgb[2]);
  847. return 0;
  848. }
  849. int show_pix_fmts(void *optctx, const char *opt, const char *arg)
  850. {
  851. const AVPixFmtDescriptor *pix_desc = NULL;
  852. printf("Pixel formats:\n"
  853. "I.... = Supported Input format for conversion\n"
  854. ".O... = Supported Output format for conversion\n"
  855. "..H.. = Hardware accelerated format\n"
  856. "...P. = Paletted format\n"
  857. "....B = Bitstream format\n"
  858. "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL BIT_DEPTHS\n"
  859. "-----\n");
  860. #if !CONFIG_SWSCALE
  861. # define sws_isSupportedInput(x) 0
  862. # define sws_isSupportedOutput(x) 0
  863. #endif
  864. while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
  865. enum AVPixelFormat av_unused pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
  866. printf("%c%c%c%c%c %-16s %d %3d %d",
  867. sws_isSupportedInput (pix_fmt) ? 'I' : '.',
  868. sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
  869. pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? 'H' : '.',
  870. pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? 'P' : '.',
  871. pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
  872. pix_desc->name,
  873. pix_desc->nb_components,
  874. av_get_bits_per_pixel(pix_desc),
  875. pix_desc->comp[0].depth);
  876. for (unsigned i = 1; i < pix_desc->nb_components; i++)
  877. printf("-%d", pix_desc->comp[i].depth);
  878. printf("\n");
  879. }
  880. return 0;
  881. }
  882. int show_layouts(void *optctx, const char *opt, const char *arg)
  883. {
  884. const AVChannelLayout *ch_layout;
  885. void *iter = NULL;
  886. char buf[128], buf2[128];
  887. int i = 0;
  888. printf("Individual channels:\n"
  889. "NAME DESCRIPTION\n");
  890. for (i = 0; i < 63; i++) {
  891. av_channel_name(buf, sizeof(buf), i);
  892. if (strstr(buf, "USR"))
  893. continue;
  894. av_channel_description(buf2, sizeof(buf2), i);
  895. printf("%-14s %s\n", buf, buf2);
  896. }
  897. printf("\nStandard channel layouts:\n"
  898. "NAME DECOMPOSITION\n");
  899. while (ch_layout = av_channel_layout_standard(&iter)) {
  900. av_channel_layout_describe(ch_layout, buf, sizeof(buf));
  901. printf("%-14s ", buf);
  902. for (i = 0; i < 63; i++) {
  903. int idx = av_channel_layout_index_from_channel(ch_layout, i);
  904. if (idx >= 0) {
  905. av_channel_name(buf2, sizeof(buf2), i);
  906. printf("%s%s", idx ? "+" : "", buf2);
  907. }
  908. }
  909. printf("\n");
  910. }
  911. return 0;
  912. }
  913. int show_sample_fmts(void *optctx, const char *opt, const char *arg)
  914. {
  915. int i;
  916. char fmt_str[128];
  917. for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
  918. printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
  919. return 0;
  920. }
  921. int show_dispositions(void *optctx, const char *opt, const char *arg)
  922. {
  923. for (int i = 0; i < 32; i++) {
  924. const char *str = av_disposition_to_string(1U << i);
  925. if (str)
  926. printf("%s\n", str);
  927. }
  928. return 0;
  929. }
  930. int opt_cpuflags(void *optctx, const char *opt, const char *arg)
  931. {
  932. int ret;
  933. unsigned flags = av_get_cpu_flags();
  934. if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
  935. return ret;
  936. av_force_cpu_flags(flags);
  937. return 0;
  938. }
  939. int opt_cpucount(void *optctx, const char *opt, const char *arg)
  940. {
  941. int ret;
  942. int count;
  943. static const AVOption opts[] = {
  944. {"count", NULL, 0, AV_OPT_TYPE_INT, { .i64 = -1}, -1, INT_MAX},
  945. {NULL},
  946. };
  947. static const AVClass class = {
  948. .class_name = "cpucount",
  949. .item_name = av_default_item_name,
  950. .option = opts,
  951. .version = LIBAVUTIL_VERSION_INT,
  952. };
  953. const AVClass *pclass = &class;
  954. ret = av_opt_eval_int(&pclass, opts, arg, &count);
  955. if (!ret) {
  956. av_cpu_force_count(count);
  957. }
  958. return ret;
  959. }
  960. static void expand_filename_template(AVBPrint *bp, const char *template,
  961. struct tm *tm)
  962. {
  963. int c;
  964. while ((c = *(template++))) {
  965. if (c == '%') {
  966. if (!(c = *(template++)))
  967. break;
  968. switch (c) {
  969. case 'p':
  970. av_bprintf(bp, "%s", program_name);
  971. break;
  972. case 't':
  973. av_bprintf(bp, "%04d%02d%02d-%02d%02d%02d",
  974. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  975. tm->tm_hour, tm->tm_min, tm->tm_sec);
  976. break;
  977. case '%':
  978. av_bprint_chars(bp, c, 1);
  979. break;
  980. }
  981. } else {
  982. av_bprint_chars(bp, c, 1);
  983. }
  984. }
  985. }
  986. static void log_callback_report(void *ptr, int level, const char *fmt, va_list vl)
  987. {
  988. va_list vl2;
  989. char line[1024];
  990. static int print_prefix = 1;
  991. va_copy(vl2, vl);
  992. av_log_default_callback(ptr, level, fmt, vl);
  993. av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
  994. va_end(vl2);
  995. if (report_file_level >= level) {
  996. fputs(line, report_file);
  997. fflush(report_file);
  998. }
  999. }
  1000. int init_report(const char *env, FILE **file)
  1001. {
  1002. char *filename_template = NULL;
  1003. char *key, *val;
  1004. int ret, count = 0;
  1005. int prog_loglevel, envlevel = 0;
  1006. time_t now;
  1007. struct tm *tm;
  1008. AVBPrint filename;
  1009. if (report_file) /* already opened */
  1010. return 0;
  1011. time(&now);
  1012. tm = localtime(&now);
  1013. while (env && *env) {
  1014. if ((ret = av_opt_get_key_value(&env, "=", ":", 0, &key, &val)) < 0) {
  1015. if (count)
  1016. av_log(NULL, AV_LOG_ERROR,
  1017. "Failed to parse FFREPORT environment variable: %s\n",
  1018. av_err2str(ret));
  1019. break;
  1020. }
  1021. if (*env)
  1022. env++;
  1023. count++;
  1024. if (!strcmp(key, "file")) {
  1025. av_free(filename_template);
  1026. filename_template = val;
  1027. val = NULL;
  1028. } else if (!strcmp(key, "level")) {
  1029. char *tail;
  1030. report_file_level = strtol(val, &tail, 10);
  1031. if (*tail) {
  1032. av_log(NULL, AV_LOG_FATAL, "Invalid report file level\n");
  1033. av_free(key);
  1034. av_free(val);
  1035. av_free(filename_template);
  1036. return AVERROR(EINVAL);
  1037. }
  1038. envlevel = 1;
  1039. } else {
  1040. av_log(NULL, AV_LOG_ERROR, "Unknown key '%s' in FFREPORT\n", key);
  1041. }
  1042. av_free(val);
  1043. av_free(key);
  1044. }
  1045. av_bprint_init(&filename, 0, AV_BPRINT_SIZE_AUTOMATIC);
  1046. expand_filename_template(&filename,
  1047. av_x_if_null(filename_template, "%p-%t.log"), tm);
  1048. av_free(filename_template);
  1049. if (!av_bprint_is_complete(&filename)) {
  1050. av_log(NULL, AV_LOG_ERROR, "Out of memory building report file name\n");
  1051. return AVERROR(ENOMEM);
  1052. }
  1053. prog_loglevel = av_log_get_level();
  1054. if (!envlevel)
  1055. report_file_level = FFMAX(report_file_level, prog_loglevel);
  1056. report_file = fopen(filename.str, "w");
  1057. if (!report_file) {
  1058. int ret = AVERROR(errno);
  1059. av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
  1060. filename.str, strerror(errno));
  1061. return ret;
  1062. }
  1063. av_log_set_callback(log_callback_report);
  1064. av_log(NULL, AV_LOG_INFO,
  1065. "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
  1066. "Report written to \"%s\"\n"
  1067. "Log level: %d\n",
  1068. program_name,
  1069. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  1070. tm->tm_hour, tm->tm_min, tm->tm_sec,
  1071. filename.str, report_file_level);
  1072. av_bprint_finalize(&filename, NULL);
  1073. if (file)
  1074. *file = report_file;
  1075. return 0;
  1076. }
  1077. int opt_report(void *optctx, const char *opt, const char *arg)
  1078. {
  1079. return init_report(NULL, NULL);
  1080. }
  1081. int opt_max_alloc(void *optctx, const char *opt, const char *arg)
  1082. {
  1083. char *tail;
  1084. size_t max;
  1085. max = strtol(arg, &tail, 10);
  1086. if (*tail) {
  1087. av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
  1088. return AVERROR(EINVAL);
  1089. }
  1090. av_max_alloc(max);
  1091. return 0;
  1092. }
  1093. int opt_loglevel(void *optctx, const char *opt, const char *arg)
  1094. {
  1095. const struct { const char *name; int level; } log_levels[] = {
  1096. { "quiet" , AV_LOG_QUIET },
  1097. { "panic" , AV_LOG_PANIC },
  1098. { "fatal" , AV_LOG_FATAL },
  1099. { "error" , AV_LOG_ERROR },
  1100. { "warning", AV_LOG_WARNING },
  1101. { "info" , AV_LOG_INFO },
  1102. { "verbose", AV_LOG_VERBOSE },
  1103. { "debug" , AV_LOG_DEBUG },
  1104. { "trace" , AV_LOG_TRACE },
  1105. };
  1106. const char *token;
  1107. char *tail;
  1108. int flags = av_log_get_flags();
  1109. int level = av_log_get_level();
  1110. int cmd, i = 0;
  1111. av_assert0(arg);
  1112. while (*arg) {
  1113. token = arg;
  1114. if (*token == '+' || *token == '-') {
  1115. cmd = *token++;
  1116. } else {
  1117. cmd = 0;
  1118. }
  1119. if (!i && !cmd) {
  1120. flags = 0; /* missing relative prefix, build absolute value */
  1121. }
  1122. if (av_strstart(token, "repeat", &arg)) {
  1123. if (cmd == '-') {
  1124. flags |= AV_LOG_SKIP_REPEATED;
  1125. } else {
  1126. flags &= ~AV_LOG_SKIP_REPEATED;
  1127. }
  1128. } else if (av_strstart(token, "level", &arg)) {
  1129. if (cmd == '-') {
  1130. flags &= ~AV_LOG_PRINT_LEVEL;
  1131. } else {
  1132. flags |= AV_LOG_PRINT_LEVEL;
  1133. }
  1134. } else {
  1135. break;
  1136. }
  1137. i++;
  1138. }
  1139. if (!*arg) {
  1140. goto end;
  1141. } else if (*arg == '+') {
  1142. arg++;
  1143. } else if (!i) {
  1144. flags = av_log_get_flags(); /* level value without prefix, reset flags */
  1145. }
  1146. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
  1147. if (!strcmp(log_levels[i].name, arg)) {
  1148. level = log_levels[i].level;
  1149. goto end;
  1150. }
  1151. }
  1152. level = strtol(arg, &tail, 10);
  1153. if (*tail) {
  1154. av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
  1155. "Possible levels are numbers or:\n", arg);
  1156. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
  1157. av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
  1158. return AVERROR(EINVAL);
  1159. }
  1160. end:
  1161. av_log_set_flags(flags);
  1162. av_log_set_level(level);
  1163. return 0;
  1164. }
  1165. #if CONFIG_AVDEVICE
  1166. static void print_device_list(const AVDeviceInfoList *device_list)
  1167. {
  1168. // print devices
  1169. for (int i = 0; i < device_list->nb_devices; i++) {
  1170. const AVDeviceInfo *device = device_list->devices[i];
  1171. printf("%c %s [%s] (", device_list->default_device == i ? '*' : ' ',
  1172. device->device_name, device->device_description);
  1173. if (device->nb_media_types > 0) {
  1174. for (int j = 0; j < device->nb_media_types; ++j) {
  1175. const char* media_type = av_get_media_type_string(device->media_types[j]);
  1176. if (j > 0)
  1177. printf(", ");
  1178. printf("%s", media_type ? media_type : "unknown");
  1179. }
  1180. } else {
  1181. printf("none");
  1182. }
  1183. printf(")\n");
  1184. }
  1185. }
  1186. static int print_device_sources(const AVInputFormat *fmt, AVDictionary *opts)
  1187. {
  1188. int ret;
  1189. AVDeviceInfoList *device_list = NULL;
  1190. if (!fmt || !fmt->priv_class || !AV_IS_INPUT_DEVICE(fmt->priv_class->category))
  1191. return AVERROR(EINVAL);
  1192. printf("Auto-detected sources for %s:\n", fmt->name);
  1193. if ((ret = avdevice_list_input_sources(fmt, NULL, opts, &device_list)) < 0) {
  1194. printf("Cannot list sources: %s\n", av_err2str(ret));
  1195. goto fail;
  1196. }
  1197. print_device_list(device_list);
  1198. fail:
  1199. avdevice_free_list_devices(&device_list);
  1200. return ret;
  1201. }
  1202. static int print_device_sinks(const AVOutputFormat *fmt, AVDictionary *opts)
  1203. {
  1204. int ret;
  1205. AVDeviceInfoList *device_list = NULL;
  1206. if (!fmt || !fmt->priv_class || !AV_IS_OUTPUT_DEVICE(fmt->priv_class->category))
  1207. return AVERROR(EINVAL);
  1208. printf("Auto-detected sinks for %s:\n", fmt->name);
  1209. if ((ret = avdevice_list_output_sinks(fmt, NULL, opts, &device_list)) < 0) {
  1210. printf("Cannot list sinks: %s\n", av_err2str(ret));
  1211. goto fail;
  1212. }
  1213. print_device_list(device_list);
  1214. fail:
  1215. avdevice_free_list_devices(&device_list);
  1216. return ret;
  1217. }
  1218. static int show_sinks_sources_parse_arg(const char *arg, char **dev, AVDictionary **opts)
  1219. {
  1220. int ret;
  1221. if (arg) {
  1222. char *opts_str = NULL;
  1223. av_assert0(dev && opts);
  1224. *dev = av_strdup(arg);
  1225. if (!*dev)
  1226. return AVERROR(ENOMEM);
  1227. if ((opts_str = strchr(*dev, ','))) {
  1228. *(opts_str++) = '\0';
  1229. if (opts_str[0] && ((ret = av_dict_parse_string(opts, opts_str, "=", ":", 0)) < 0)) {
  1230. av_freep(dev);
  1231. return ret;
  1232. }
  1233. }
  1234. } else
  1235. printf("\nDevice name is not provided.\n"
  1236. "You can pass devicename[,opt1=val1[,opt2=val2...]] as an argument.\n\n");
  1237. return 0;
  1238. }
  1239. int show_sources(void *optctx, const char *opt, const char *arg)
  1240. {
  1241. const AVInputFormat *fmt = NULL;
  1242. char *dev = NULL;
  1243. AVDictionary *opts = NULL;
  1244. int ret = 0;
  1245. int error_level = av_log_get_level();
  1246. av_log_set_level(AV_LOG_WARNING);
  1247. if ((ret = show_sinks_sources_parse_arg(arg, &dev, &opts)) < 0)
  1248. goto fail;
  1249. do {
  1250. fmt = av_input_audio_device_next(fmt);
  1251. if (fmt) {
  1252. if (!strcmp(fmt->name, "lavfi"))
  1253. continue; //it's pointless to probe lavfi
  1254. if (dev && !av_match_name(dev, fmt->name))
  1255. continue;
  1256. print_device_sources(fmt, opts);
  1257. }
  1258. } while (fmt);
  1259. do {
  1260. fmt = av_input_video_device_next(fmt);
  1261. if (fmt) {
  1262. if (dev && !av_match_name(dev, fmt->name))
  1263. continue;
  1264. print_device_sources(fmt, opts);
  1265. }
  1266. } while (fmt);
  1267. fail:
  1268. av_dict_free(&opts);
  1269. av_free(dev);
  1270. av_log_set_level(error_level);
  1271. return ret;
  1272. }
  1273. int show_sinks(void *optctx, const char *opt, const char *arg)
  1274. {
  1275. const AVOutputFormat *fmt = NULL;
  1276. char *dev = NULL;
  1277. AVDictionary *opts = NULL;
  1278. int ret = 0;
  1279. int error_level = av_log_get_level();
  1280. av_log_set_level(AV_LOG_WARNING);
  1281. if ((ret = show_sinks_sources_parse_arg(arg, &dev, &opts)) < 0)
  1282. goto fail;
  1283. do {
  1284. fmt = av_output_audio_device_next(fmt);
  1285. if (fmt) {
  1286. if (dev && !av_match_name(dev, fmt->name))
  1287. continue;
  1288. print_device_sinks(fmt, opts);
  1289. }
  1290. } while (fmt);
  1291. do {
  1292. fmt = av_output_video_device_next(fmt);
  1293. if (fmt) {
  1294. if (dev && !av_match_name(dev, fmt->name))
  1295. continue;
  1296. print_device_sinks(fmt, opts);
  1297. }
  1298. } while (fmt);
  1299. fail:
  1300. av_dict_free(&opts);
  1301. av_free(dev);
  1302. av_log_set_level(error_level);
  1303. return ret;
  1304. }
  1305. #endif /* CONFIG_AVDEVICE */