opt_common.c 48 KB

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