opt_common.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  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, config, type, name, elem, fmt, ...) \
  233. do { \
  234. int num = 0; \
  235. const type *elem = NULL; \
  236. avcodec_get_supported_config(NULL, codec, config, 0, \
  237. (const void **) &elem, &num); \
  238. if (elem) { \
  239. printf(" Supported " name ":"); \
  240. for (int i = 0; i < num; i++) { \
  241. printf(" " fmt, __VA_ARGS__); \
  242. elem++; \
  243. } \
  244. printf("\n"); \
  245. } \
  246. } while (0)
  247. static const char *get_channel_layout_desc(const AVChannelLayout *layout, AVBPrint *bp)
  248. {
  249. int ret;
  250. av_bprint_clear(bp);
  251. ret = av_channel_layout_describe_bprint(layout, bp);
  252. if (!av_bprint_is_complete(bp) || ret < 0)
  253. return "unknown/invalid";
  254. return bp->str;
  255. }
  256. static void print_codec(const AVCodec *c)
  257. {
  258. int encoder = av_codec_is_encoder(c);
  259. AVBPrint desc;
  260. printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
  261. c->long_name ? c->long_name : "");
  262. printf(" General capabilities: ");
  263. if (c->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND)
  264. printf("horizband ");
  265. if (c->capabilities & AV_CODEC_CAP_DR1)
  266. printf("dr1 ");
  267. if (c->capabilities & AV_CODEC_CAP_DELAY)
  268. printf("delay ");
  269. if (c->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)
  270. printf("small ");
  271. if (c->capabilities & AV_CODEC_CAP_EXPERIMENTAL)
  272. printf("exp ");
  273. if (c->capabilities & AV_CODEC_CAP_CHANNEL_CONF)
  274. printf("chconf ");
  275. if (c->capabilities & AV_CODEC_CAP_PARAM_CHANGE)
  276. printf("paramchange ");
  277. if (c->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
  278. printf("variable ");
  279. if (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
  280. AV_CODEC_CAP_SLICE_THREADS |
  281. AV_CODEC_CAP_OTHER_THREADS))
  282. printf("threads ");
  283. if (c->capabilities & AV_CODEC_CAP_AVOID_PROBING)
  284. printf("avoidprobe ");
  285. if (c->capabilities & AV_CODEC_CAP_HARDWARE)
  286. printf("hardware ");
  287. if (c->capabilities & AV_CODEC_CAP_HYBRID)
  288. printf("hybrid ");
  289. if (!c->capabilities)
  290. printf("none");
  291. printf("\n");
  292. if (c->type == AVMEDIA_TYPE_VIDEO ||
  293. c->type == AVMEDIA_TYPE_AUDIO) {
  294. printf(" Threading capabilities: ");
  295. switch (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
  296. AV_CODEC_CAP_SLICE_THREADS |
  297. AV_CODEC_CAP_OTHER_THREADS)) {
  298. case AV_CODEC_CAP_FRAME_THREADS |
  299. AV_CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
  300. case AV_CODEC_CAP_FRAME_THREADS: printf("frame"); break;
  301. case AV_CODEC_CAP_SLICE_THREADS: printf("slice"); break;
  302. case AV_CODEC_CAP_OTHER_THREADS: printf("other"); break;
  303. default: printf("none"); break;
  304. }
  305. printf("\n");
  306. }
  307. if (avcodec_get_hw_config(c, 0)) {
  308. printf(" Supported hardware devices: ");
  309. for (int i = 0;; i++) {
  310. const AVCodecHWConfig *config = avcodec_get_hw_config(c, i);
  311. const char *name;
  312. if (!config)
  313. break;
  314. name = av_hwdevice_get_type_name(config->device_type);
  315. if (name)
  316. printf("%s ", name);
  317. }
  318. printf("\n");
  319. }
  320. PRINT_CODEC_SUPPORTED(c, AV_CODEC_CONFIG_FRAME_RATE, AVRational, "framerates",
  321. fps, "%d/%d", fps->num, fps->den);
  322. PRINT_CODEC_SUPPORTED(c, AV_CODEC_CONFIG_PIX_FORMAT, enum AVPixelFormat,
  323. "pixel formats", fmt, "%s", av_get_pix_fmt_name(*fmt));
  324. PRINT_CODEC_SUPPORTED(c, AV_CODEC_CONFIG_SAMPLE_RATE, int, "sample rates",
  325. rate, "%d", *rate);
  326. PRINT_CODEC_SUPPORTED(c, AV_CODEC_CONFIG_SAMPLE_FORMAT, enum AVSampleFormat,
  327. "sample formats", fmt, "%s", av_get_sample_fmt_name(*fmt));
  328. av_bprint_init(&desc, 0, AV_BPRINT_SIZE_AUTOMATIC);
  329. PRINT_CODEC_SUPPORTED(c, AV_CODEC_CONFIG_CHANNEL_LAYOUT, AVChannelLayout,
  330. "channel layouts", layout, "%s",
  331. get_channel_layout_desc(layout, &desc));
  332. av_bprint_finalize(&desc, NULL);
  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. if (bsf->codec_ids) {
  496. const enum AVCodecID *id = bsf->codec_ids;
  497. printf(" Supported codecs:");
  498. while (*id != AV_CODEC_ID_NONE) {
  499. printf(" %s", avcodec_descriptor_get(*id)->name);
  500. id++;
  501. }
  502. printf("\n");
  503. }
  504. if (bsf->priv_class)
  505. show_help_children(bsf->priv_class, AV_OPT_FLAG_BSF_PARAM);
  506. }
  507. int show_help(void *optctx, const char *opt, const char *arg)
  508. {
  509. char *topic, *par;
  510. av_log_set_callback(log_callback_help);
  511. topic = av_strdup(arg ? arg : "");
  512. if (!topic)
  513. return AVERROR(ENOMEM);
  514. par = strchr(topic, '=');
  515. if (par)
  516. *par++ = 0;
  517. if (!*topic) {
  518. show_help_default(topic, par);
  519. } else if (!strcmp(topic, "decoder")) {
  520. show_help_codec(par, 0);
  521. } else if (!strcmp(topic, "encoder")) {
  522. show_help_codec(par, 1);
  523. } else if (!strcmp(topic, "demuxer")) {
  524. show_help_demuxer(par);
  525. } else if (!strcmp(topic, "muxer")) {
  526. show_help_muxer(par);
  527. } else if (!strcmp(topic, "protocol")) {
  528. show_help_protocol(par);
  529. #if CONFIG_AVFILTER
  530. } else if (!strcmp(topic, "filter")) {
  531. show_help_filter(par);
  532. #endif
  533. } else if (!strcmp(topic, "bsf")) {
  534. show_help_bsf(par);
  535. } else {
  536. show_help_default(topic, par);
  537. }
  538. av_freep(&topic);
  539. return 0;
  540. }
  541. static void print_codecs_for_id(enum AVCodecID id, int encoder)
  542. {
  543. void *iter = NULL;
  544. const AVCodec *codec;
  545. printf(" (%s:", encoder ? "encoders" : "decoders");
  546. while ((codec = next_codec_for_id(id, &iter, encoder)))
  547. printf(" %s", codec->name);
  548. printf(")");
  549. }
  550. static int compare_codec_desc(const void *a, const void *b)
  551. {
  552. const AVCodecDescriptor * const *da = a;
  553. const AVCodecDescriptor * const *db = b;
  554. return (*da)->type != (*db)->type ? FFDIFFSIGN((*da)->type, (*db)->type) :
  555. strcmp((*da)->name, (*db)->name);
  556. }
  557. static int get_codecs_sorted(const AVCodecDescriptor ***rcodecs)
  558. {
  559. const AVCodecDescriptor *desc = NULL;
  560. const AVCodecDescriptor **codecs;
  561. unsigned nb_codecs = 0, i = 0;
  562. while ((desc = avcodec_descriptor_next(desc)))
  563. nb_codecs++;
  564. if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs))))
  565. return AVERROR(ENOMEM);
  566. desc = NULL;
  567. while ((desc = avcodec_descriptor_next(desc)))
  568. codecs[i++] = desc;
  569. av_assert0(i == nb_codecs);
  570. qsort(codecs, nb_codecs, sizeof(*codecs), compare_codec_desc);
  571. *rcodecs = codecs;
  572. return nb_codecs;
  573. }
  574. static char get_media_type_char(enum AVMediaType type)
  575. {
  576. switch (type) {
  577. case AVMEDIA_TYPE_VIDEO: return 'V';
  578. case AVMEDIA_TYPE_AUDIO: return 'A';
  579. case AVMEDIA_TYPE_DATA: return 'D';
  580. case AVMEDIA_TYPE_SUBTITLE: return 'S';
  581. case AVMEDIA_TYPE_ATTACHMENT:return 'T';
  582. default: return '?';
  583. }
  584. }
  585. int show_codecs(void *optctx, const char *opt, const char *arg)
  586. {
  587. const AVCodecDescriptor **codecs;
  588. unsigned i;
  589. int nb_codecs = get_codecs_sorted(&codecs);
  590. if (nb_codecs < 0)
  591. return nb_codecs;
  592. printf("Codecs:\n"
  593. " D..... = Decoding supported\n"
  594. " .E.... = Encoding supported\n"
  595. " ..V... = Video codec\n"
  596. " ..A... = Audio codec\n"
  597. " ..S... = Subtitle codec\n"
  598. " ..D... = Data codec\n"
  599. " ..T... = Attachment codec\n"
  600. " ...I.. = Intra frame-only codec\n"
  601. " ....L. = Lossy compression\n"
  602. " .....S = Lossless compression\n"
  603. " -------\n");
  604. for (i = 0; i < nb_codecs; i++) {
  605. const AVCodecDescriptor *desc = codecs[i];
  606. const AVCodec *codec;
  607. void *iter = NULL;
  608. if (strstr(desc->name, "_deprecated"))
  609. continue;
  610. printf(" %c%c%c%c%c%c",
  611. avcodec_find_decoder(desc->id) ? 'D' : '.',
  612. avcodec_find_encoder(desc->id) ? 'E' : '.',
  613. get_media_type_char(desc->type),
  614. (desc->props & AV_CODEC_PROP_INTRA_ONLY) ? 'I' : '.',
  615. (desc->props & AV_CODEC_PROP_LOSSY) ? 'L' : '.',
  616. (desc->props & AV_CODEC_PROP_LOSSLESS) ? 'S' : '.');
  617. printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
  618. /* print decoders/encoders when there's more than one or their
  619. * names are different from codec name */
  620. while ((codec = next_codec_for_id(desc->id, &iter, 0))) {
  621. if (strcmp(codec->name, desc->name)) {
  622. print_codecs_for_id(desc->id, 0);
  623. break;
  624. }
  625. }
  626. iter = NULL;
  627. while ((codec = next_codec_for_id(desc->id, &iter, 1))) {
  628. if (strcmp(codec->name, desc->name)) {
  629. print_codecs_for_id(desc->id, 1);
  630. break;
  631. }
  632. }
  633. printf("\n");
  634. }
  635. av_free(codecs);
  636. return 0;
  637. }
  638. static int print_codecs(int encoder)
  639. {
  640. const AVCodecDescriptor **codecs;
  641. int i, nb_codecs = get_codecs_sorted(&codecs);
  642. if (nb_codecs < 0)
  643. return nb_codecs;
  644. printf("%s:\n"
  645. " V..... = Video\n"
  646. " A..... = Audio\n"
  647. " S..... = Subtitle\n"
  648. " .F.... = Frame-level multithreading\n"
  649. " ..S... = Slice-level multithreading\n"
  650. " ...X.. = Codec is experimental\n"
  651. " ....B. = Supports draw_horiz_band\n"
  652. " .....D = Supports direct rendering method 1\n"
  653. " ------\n",
  654. encoder ? "Encoders" : "Decoders");
  655. for (i = 0; i < nb_codecs; i++) {
  656. const AVCodecDescriptor *desc = codecs[i];
  657. const AVCodec *codec;
  658. void *iter = NULL;
  659. while ((codec = next_codec_for_id(desc->id, &iter, encoder))) {
  660. printf(" %c%c%c%c%c%c",
  661. get_media_type_char(desc->type),
  662. (codec->capabilities & AV_CODEC_CAP_FRAME_THREADS) ? 'F' : '.',
  663. (codec->capabilities & AV_CODEC_CAP_SLICE_THREADS) ? 'S' : '.',
  664. (codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) ? 'X' : '.',
  665. (codec->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND) ? 'B' : '.',
  666. (codec->capabilities & AV_CODEC_CAP_DR1) ? 'D' : '.');
  667. printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
  668. if (strcmp(codec->name, desc->name))
  669. printf(" (codec %s)", desc->name);
  670. printf("\n");
  671. }
  672. }
  673. av_free(codecs);
  674. return 0;
  675. }
  676. int show_decoders(void *optctx, const char *opt, const char *arg)
  677. {
  678. return print_codecs(0);
  679. }
  680. int show_encoders(void *optctx, const char *opt, const char *arg)
  681. {
  682. return print_codecs(1);
  683. }
  684. int show_bsfs(void *optctx, const char *opt, const char *arg)
  685. {
  686. const AVBitStreamFilter *bsf = NULL;
  687. void *opaque = NULL;
  688. printf("Bitstream filters:\n");
  689. while ((bsf = av_bsf_iterate(&opaque)))
  690. printf("%s\n", bsf->name);
  691. printf("\n");
  692. return 0;
  693. }
  694. int show_filters(void *optctx, const char *opt, const char *arg)
  695. {
  696. #if CONFIG_AVFILTER
  697. const AVFilter *filter = NULL;
  698. char descr[64], *descr_cur;
  699. void *opaque = NULL;
  700. int i, j;
  701. const AVFilterPad *pad;
  702. printf("Filters:\n"
  703. " T.. = Timeline support\n"
  704. " .S. = Slice threading\n"
  705. " A = Audio input/output\n"
  706. " V = Video input/output\n"
  707. " N = Dynamic number and/or type of input/output\n"
  708. " | = Source or sink filter\n");
  709. while ((filter = av_filter_iterate(&opaque))) {
  710. descr_cur = descr;
  711. for (i = 0; i < 2; i++) {
  712. unsigned nb_pads;
  713. if (i) {
  714. *(descr_cur++) = '-';
  715. *(descr_cur++) = '>';
  716. }
  717. pad = i ? filter->outputs : filter->inputs;
  718. nb_pads = avfilter_filter_pad_count(filter, i);
  719. for (j = 0; j < nb_pads; j++) {
  720. if (descr_cur >= descr + sizeof(descr) - 4)
  721. break;
  722. *(descr_cur++) = get_media_type_char(avfilter_pad_get_type(pad, j));
  723. }
  724. if (!j)
  725. *(descr_cur++) = ((!i && (filter->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)) ||
  726. ( i && (filter->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS))) ? 'N' : '|';
  727. }
  728. *descr_cur = 0;
  729. printf(" %c%c %-17s %-10s %s\n",
  730. filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE ? 'T' : '.',
  731. filter->flags & AVFILTER_FLAG_SLICE_THREADS ? 'S' : '.',
  732. filter->name, descr, filter->description);
  733. }
  734. #else
  735. printf("No filters available: libavfilter disabled\n");
  736. #endif
  737. return 0;
  738. }
  739. static int is_device(const AVClass *avclass)
  740. {
  741. if (!avclass)
  742. return 0;
  743. return AV_IS_INPUT_DEVICE(avclass->category) || AV_IS_OUTPUT_DEVICE(avclass->category);
  744. }
  745. static int show_formats_devices(void *optctx, const char *opt, const char *arg, int device_only, int muxdemuxers)
  746. {
  747. void *ifmt_opaque = NULL;
  748. const AVInputFormat *ifmt = NULL;
  749. void *ofmt_opaque = NULL;
  750. const AVOutputFormat *ofmt = NULL;
  751. const char *last_name;
  752. int is_dev;
  753. const char *is_device_placeholder = device_only ? "" : ".";
  754. printf("%s:\n"
  755. " D.%s = Demuxing supported\n"
  756. " .E%s = Muxing supported\n"
  757. "%s"
  758. " ---\n",
  759. device_only ? "Devices" : "Formats",
  760. is_device_placeholder, is_device_placeholder,
  761. device_only ? "": " ..d = Is a device\n");
  762. last_name = "000";
  763. for (;;) {
  764. int decode = 0;
  765. int encode = 0;
  766. int device = 0;
  767. const char *name = NULL;
  768. const char *long_name = NULL;
  769. if (muxdemuxers !=SHOW_DEMUXERS) {
  770. ofmt_opaque = NULL;
  771. while ((ofmt = av_muxer_iterate(&ofmt_opaque))) {
  772. is_dev = is_device(ofmt->priv_class);
  773. if (!is_dev && device_only)
  774. continue;
  775. if ((!name || strcmp(ofmt->name, name) < 0) &&
  776. strcmp(ofmt->name, last_name) > 0) {
  777. name = ofmt->name;
  778. long_name = ofmt->long_name;
  779. encode = 1;
  780. device = is_dev;
  781. }
  782. }
  783. }
  784. if (muxdemuxers != SHOW_MUXERS) {
  785. ifmt_opaque = NULL;
  786. while ((ifmt = av_demuxer_iterate(&ifmt_opaque))) {
  787. is_dev = is_device(ifmt->priv_class);
  788. if (!is_dev && device_only)
  789. continue;
  790. if ((!name || strcmp(ifmt->name, name) < 0) &&
  791. strcmp(ifmt->name, last_name) > 0) {
  792. name = ifmt->name;
  793. long_name = ifmt->long_name;
  794. encode = 0;
  795. device = is_dev;
  796. }
  797. if (name && strcmp(ifmt->name, name) == 0) {
  798. decode = 1;
  799. device = is_dev;
  800. }
  801. }
  802. }
  803. if (!name)
  804. break;
  805. last_name = name;
  806. printf(" %c%c%s %-15s %s\n",
  807. decode ? 'D' : ' ',
  808. encode ? 'E' : ' ',
  809. device_only ? "" : (device ? "d" : " "),
  810. name,
  811. long_name ? long_name : " ");
  812. }
  813. return 0;
  814. }
  815. int show_formats(void *optctx, const char *opt, const char *arg)
  816. {
  817. return show_formats_devices(optctx, opt, arg, 0, SHOW_DEFAULT);
  818. }
  819. int show_muxers(void *optctx, const char *opt, const char *arg)
  820. {
  821. return show_formats_devices(optctx, opt, arg, 0, SHOW_MUXERS);
  822. }
  823. int show_demuxers(void *optctx, const char *opt, const char *arg)
  824. {
  825. return show_formats_devices(optctx, opt, arg, 0, SHOW_DEMUXERS);
  826. }
  827. int show_devices(void *optctx, const char *opt, const char *arg)
  828. {
  829. return show_formats_devices(optctx, opt, arg, 1, SHOW_DEFAULT);
  830. }
  831. int show_protocols(void *optctx, const char *opt, const char *arg)
  832. {
  833. void *opaque = NULL;
  834. const char *name;
  835. printf("Supported file protocols:\n"
  836. "Input:\n");
  837. while ((name = avio_enum_protocols(&opaque, 0)))
  838. printf(" %s\n", name);
  839. printf("Output:\n");
  840. while ((name = avio_enum_protocols(&opaque, 1)))
  841. printf(" %s\n", name);
  842. return 0;
  843. }
  844. int show_colors(void *optctx, const char *opt, const char *arg)
  845. {
  846. const char *name;
  847. const uint8_t *rgb;
  848. int i;
  849. printf("%-32s #RRGGBB\n", "name");
  850. for (i = 0; name = av_get_known_color_name(i, &rgb); i++)
  851. printf("%-32s #%02x%02x%02x\n", name, rgb[0], rgb[1], rgb[2]);
  852. return 0;
  853. }
  854. int show_pix_fmts(void *optctx, const char *opt, const char *arg)
  855. {
  856. const AVPixFmtDescriptor *pix_desc = NULL;
  857. printf("Pixel formats:\n"
  858. "I.... = Supported Input format for conversion\n"
  859. ".O... = Supported Output format for conversion\n"
  860. "..H.. = Hardware accelerated format\n"
  861. "...P. = Paletted format\n"
  862. "....B = Bitstream format\n"
  863. "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL BIT_DEPTHS\n"
  864. "-----\n");
  865. #if !CONFIG_SWSCALE
  866. # define sws_isSupportedInput(x) 0
  867. # define sws_isSupportedOutput(x) 0
  868. #endif
  869. while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
  870. enum AVPixelFormat av_unused pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
  871. printf("%c%c%c%c%c %-16s %d %3d %d",
  872. sws_isSupportedInput (pix_fmt) ? 'I' : '.',
  873. sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
  874. pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? 'H' : '.',
  875. pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? 'P' : '.',
  876. pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
  877. pix_desc->name,
  878. pix_desc->nb_components,
  879. av_get_bits_per_pixel(pix_desc),
  880. pix_desc->comp[0].depth);
  881. for (unsigned i = 1; i < pix_desc->nb_components; i++)
  882. printf("-%d", pix_desc->comp[i].depth);
  883. printf("\n");
  884. }
  885. return 0;
  886. }
  887. int show_layouts(void *optctx, const char *opt, const char *arg)
  888. {
  889. const AVChannelLayout *ch_layout;
  890. void *iter = NULL;
  891. char buf[128], buf2[128];
  892. int i = 0;
  893. printf("Individual channels:\n"
  894. "NAME DESCRIPTION\n");
  895. for (i = 0; i < 63; i++) {
  896. av_channel_name(buf, sizeof(buf), i);
  897. if (strstr(buf, "USR"))
  898. continue;
  899. av_channel_description(buf2, sizeof(buf2), i);
  900. printf("%-14s %s\n", buf, buf2);
  901. }
  902. printf("\nStandard channel layouts:\n"
  903. "NAME DECOMPOSITION\n");
  904. while (ch_layout = av_channel_layout_standard(&iter)) {
  905. av_channel_layout_describe(ch_layout, buf, sizeof(buf));
  906. printf("%-14s ", buf);
  907. for (i = 0; i < 63; i++) {
  908. int idx = av_channel_layout_index_from_channel(ch_layout, i);
  909. if (idx >= 0) {
  910. av_channel_name(buf2, sizeof(buf2), i);
  911. printf("%s%s", idx ? "+" : "", buf2);
  912. }
  913. }
  914. printf("\n");
  915. }
  916. return 0;
  917. }
  918. int show_sample_fmts(void *optctx, const char *opt, const char *arg)
  919. {
  920. int i;
  921. char fmt_str[128];
  922. for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
  923. printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
  924. return 0;
  925. }
  926. int show_dispositions(void *optctx, const char *opt, const char *arg)
  927. {
  928. for (int i = 0; i < 32; i++) {
  929. const char *str = av_disposition_to_string(1U << i);
  930. if (str)
  931. printf("%s\n", str);
  932. }
  933. return 0;
  934. }
  935. int opt_cpuflags(void *optctx, const char *opt, const char *arg)
  936. {
  937. int ret;
  938. unsigned flags = av_get_cpu_flags();
  939. if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
  940. return ret;
  941. av_force_cpu_flags(flags);
  942. return 0;
  943. }
  944. int opt_cpucount(void *optctx, const char *opt, const char *arg)
  945. {
  946. int ret;
  947. int count;
  948. static const AVOption opts[] = {
  949. {"count", NULL, 0, AV_OPT_TYPE_INT, { .i64 = -1}, -1, INT_MAX},
  950. {NULL},
  951. };
  952. static const AVClass class = {
  953. .class_name = "cpucount",
  954. .item_name = av_default_item_name,
  955. .option = opts,
  956. .version = LIBAVUTIL_VERSION_INT,
  957. };
  958. const AVClass *pclass = &class;
  959. ret = av_opt_eval_int(&pclass, opts, arg, &count);
  960. if (!ret) {
  961. av_cpu_force_count(count);
  962. }
  963. return ret;
  964. }
  965. static void expand_filename_template(AVBPrint *bp, const char *template,
  966. struct tm *tm)
  967. {
  968. int c;
  969. while ((c = *(template++))) {
  970. if (c == '%') {
  971. if (!(c = *(template++)))
  972. break;
  973. switch (c) {
  974. case 'p':
  975. av_bprintf(bp, "%s", program_name);
  976. break;
  977. case 't':
  978. av_bprintf(bp, "%04d%02d%02d-%02d%02d%02d",
  979. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  980. tm->tm_hour, tm->tm_min, tm->tm_sec);
  981. break;
  982. case '%':
  983. av_bprint_chars(bp, c, 1);
  984. break;
  985. }
  986. } else {
  987. av_bprint_chars(bp, c, 1);
  988. }
  989. }
  990. }
  991. static void log_callback_report(void *ptr, int level, const char *fmt, va_list vl)
  992. {
  993. va_list vl2;
  994. char line[1024];
  995. static int print_prefix = 1;
  996. va_copy(vl2, vl);
  997. av_log_default_callback(ptr, level, fmt, vl);
  998. av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
  999. va_end(vl2);
  1000. if (report_file_level >= level) {
  1001. fputs(line, report_file);
  1002. fflush(report_file);
  1003. }
  1004. }
  1005. int init_report(const char *env, FILE **file)
  1006. {
  1007. char *filename_template = NULL;
  1008. char *key, *val;
  1009. int ret, count = 0;
  1010. int prog_loglevel, envlevel = 0;
  1011. time_t now;
  1012. struct tm *tm;
  1013. AVBPrint filename;
  1014. if (report_file) /* already opened */
  1015. return 0;
  1016. time(&now);
  1017. tm = localtime(&now);
  1018. while (env && *env) {
  1019. if ((ret = av_opt_get_key_value(&env, "=", ":", 0, &key, &val)) < 0) {
  1020. if (count)
  1021. av_log(NULL, AV_LOG_ERROR,
  1022. "Failed to parse FFREPORT environment variable: %s\n",
  1023. av_err2str(ret));
  1024. break;
  1025. }
  1026. if (*env)
  1027. env++;
  1028. count++;
  1029. if (!strcmp(key, "file")) {
  1030. av_free(filename_template);
  1031. filename_template = val;
  1032. val = NULL;
  1033. } else if (!strcmp(key, "level")) {
  1034. char *tail;
  1035. report_file_level = strtol(val, &tail, 10);
  1036. if (*tail) {
  1037. av_log(NULL, AV_LOG_FATAL, "Invalid report file level\n");
  1038. av_free(key);
  1039. av_free(val);
  1040. av_free(filename_template);
  1041. return AVERROR(EINVAL);
  1042. }
  1043. envlevel = 1;
  1044. } else {
  1045. av_log(NULL, AV_LOG_ERROR, "Unknown key '%s' in FFREPORT\n", key);
  1046. }
  1047. av_free(val);
  1048. av_free(key);
  1049. }
  1050. av_bprint_init(&filename, 0, AV_BPRINT_SIZE_AUTOMATIC);
  1051. expand_filename_template(&filename,
  1052. av_x_if_null(filename_template, "%p-%t.log"), tm);
  1053. av_free(filename_template);
  1054. if (!av_bprint_is_complete(&filename)) {
  1055. av_log(NULL, AV_LOG_ERROR, "Out of memory building report file name\n");
  1056. return AVERROR(ENOMEM);
  1057. }
  1058. prog_loglevel = av_log_get_level();
  1059. if (!envlevel)
  1060. report_file_level = FFMAX(report_file_level, prog_loglevel);
  1061. report_file = fopen(filename.str, "w");
  1062. if (!report_file) {
  1063. int ret = AVERROR(errno);
  1064. av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
  1065. filename.str, strerror(errno));
  1066. return ret;
  1067. }
  1068. av_log_set_callback(log_callback_report);
  1069. av_log(NULL, AV_LOG_INFO,
  1070. "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
  1071. "Report written to \"%s\"\n"
  1072. "Log level: %d\n",
  1073. program_name,
  1074. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  1075. tm->tm_hour, tm->tm_min, tm->tm_sec,
  1076. filename.str, report_file_level);
  1077. av_bprint_finalize(&filename, NULL);
  1078. if (file)
  1079. *file = report_file;
  1080. return 0;
  1081. }
  1082. int opt_report(void *optctx, const char *opt, const char *arg)
  1083. {
  1084. return init_report(NULL, NULL);
  1085. }
  1086. int opt_max_alloc(void *optctx, const char *opt, const char *arg)
  1087. {
  1088. char *tail;
  1089. size_t max;
  1090. max = strtol(arg, &tail, 10);
  1091. if (*tail) {
  1092. av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
  1093. return AVERROR(EINVAL);
  1094. }
  1095. av_max_alloc(max);
  1096. return 0;
  1097. }
  1098. int opt_loglevel(void *optctx, const char *opt, const char *arg)
  1099. {
  1100. const struct { const char *name; int level; } log_levels[] = {
  1101. { "quiet" , AV_LOG_QUIET },
  1102. { "panic" , AV_LOG_PANIC },
  1103. { "fatal" , AV_LOG_FATAL },
  1104. { "error" , AV_LOG_ERROR },
  1105. { "warning", AV_LOG_WARNING },
  1106. { "info" , AV_LOG_INFO },
  1107. { "verbose", AV_LOG_VERBOSE },
  1108. { "debug" , AV_LOG_DEBUG },
  1109. { "trace" , AV_LOG_TRACE },
  1110. };
  1111. const char *token;
  1112. char *tail;
  1113. int flags = av_log_get_flags();
  1114. int level = av_log_get_level();
  1115. int cmd, i = 0;
  1116. av_assert0(arg);
  1117. while (*arg) {
  1118. token = arg;
  1119. if (*token == '+' || *token == '-') {
  1120. cmd = *token++;
  1121. } else {
  1122. cmd = 0;
  1123. }
  1124. if (!i && !cmd) {
  1125. flags = 0; /* missing relative prefix, build absolute value */
  1126. }
  1127. if (av_strstart(token, "repeat", &arg)) {
  1128. if (cmd == '-') {
  1129. flags |= AV_LOG_SKIP_REPEATED;
  1130. } else {
  1131. flags &= ~AV_LOG_SKIP_REPEATED;
  1132. }
  1133. } else if (av_strstart(token, "level", &arg)) {
  1134. if (cmd == '-') {
  1135. flags &= ~AV_LOG_PRINT_LEVEL;
  1136. } else {
  1137. flags |= AV_LOG_PRINT_LEVEL;
  1138. }
  1139. } else {
  1140. break;
  1141. }
  1142. i++;
  1143. }
  1144. if (!*arg) {
  1145. goto end;
  1146. } else if (*arg == '+') {
  1147. arg++;
  1148. } else if (!i) {
  1149. flags = av_log_get_flags(); /* level value without prefix, reset flags */
  1150. }
  1151. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
  1152. if (!strcmp(log_levels[i].name, arg)) {
  1153. level = log_levels[i].level;
  1154. goto end;
  1155. }
  1156. }
  1157. level = strtol(arg, &tail, 10);
  1158. if (*tail) {
  1159. av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
  1160. "Possible levels are numbers or:\n", arg);
  1161. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
  1162. av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
  1163. return AVERROR(EINVAL);
  1164. }
  1165. end:
  1166. av_log_set_flags(flags);
  1167. av_log_set_level(level);
  1168. return 0;
  1169. }
  1170. #if CONFIG_AVDEVICE
  1171. static void print_device_list(const AVDeviceInfoList *device_list)
  1172. {
  1173. // print devices
  1174. for (int i = 0; i < device_list->nb_devices; i++) {
  1175. const AVDeviceInfo *device = device_list->devices[i];
  1176. printf("%c %s [%s] (", device_list->default_device == i ? '*' : ' ',
  1177. device->device_name, device->device_description);
  1178. if (device->nb_media_types > 0) {
  1179. for (int j = 0; j < device->nb_media_types; ++j) {
  1180. const char* media_type = av_get_media_type_string(device->media_types[j]);
  1181. if (j > 0)
  1182. printf(", ");
  1183. printf("%s", media_type ? media_type : "unknown");
  1184. }
  1185. } else {
  1186. printf("none");
  1187. }
  1188. printf(")\n");
  1189. }
  1190. }
  1191. static int print_device_sources(const AVInputFormat *fmt, AVDictionary *opts)
  1192. {
  1193. int ret;
  1194. AVDeviceInfoList *device_list = NULL;
  1195. if (!fmt || !fmt->priv_class || !AV_IS_INPUT_DEVICE(fmt->priv_class->category))
  1196. return AVERROR(EINVAL);
  1197. printf("Auto-detected sources for %s:\n", fmt->name);
  1198. if ((ret = avdevice_list_input_sources(fmt, NULL, opts, &device_list)) < 0) {
  1199. printf("Cannot list sources: %s\n", av_err2str(ret));
  1200. goto fail;
  1201. }
  1202. print_device_list(device_list);
  1203. fail:
  1204. avdevice_free_list_devices(&device_list);
  1205. return ret;
  1206. }
  1207. static int print_device_sinks(const AVOutputFormat *fmt, AVDictionary *opts)
  1208. {
  1209. int ret;
  1210. AVDeviceInfoList *device_list = NULL;
  1211. if (!fmt || !fmt->priv_class || !AV_IS_OUTPUT_DEVICE(fmt->priv_class->category))
  1212. return AVERROR(EINVAL);
  1213. printf("Auto-detected sinks for %s:\n", fmt->name);
  1214. if ((ret = avdevice_list_output_sinks(fmt, NULL, opts, &device_list)) < 0) {
  1215. printf("Cannot list sinks: %s\n", av_err2str(ret));
  1216. goto fail;
  1217. }
  1218. print_device_list(device_list);
  1219. fail:
  1220. avdevice_free_list_devices(&device_list);
  1221. return ret;
  1222. }
  1223. static int show_sinks_sources_parse_arg(const char *arg, char **dev, AVDictionary **opts)
  1224. {
  1225. int ret;
  1226. if (arg) {
  1227. char *opts_str = NULL;
  1228. av_assert0(dev && opts);
  1229. *dev = av_strdup(arg);
  1230. if (!*dev)
  1231. return AVERROR(ENOMEM);
  1232. if ((opts_str = strchr(*dev, ','))) {
  1233. *(opts_str++) = '\0';
  1234. if (opts_str[0] && ((ret = av_dict_parse_string(opts, opts_str, "=", ":", 0)) < 0)) {
  1235. av_freep(dev);
  1236. return ret;
  1237. }
  1238. }
  1239. } else
  1240. printf("\nDevice name is not provided.\n"
  1241. "You can pass devicename[,opt1=val1[,opt2=val2...]] as an argument.\n\n");
  1242. return 0;
  1243. }
  1244. int show_sources(void *optctx, const char *opt, const char *arg)
  1245. {
  1246. const AVInputFormat *fmt = NULL;
  1247. char *dev = NULL;
  1248. AVDictionary *opts = NULL;
  1249. int ret = 0;
  1250. int error_level = av_log_get_level();
  1251. av_log_set_level(AV_LOG_WARNING);
  1252. if ((ret = show_sinks_sources_parse_arg(arg, &dev, &opts)) < 0)
  1253. goto fail;
  1254. do {
  1255. fmt = av_input_audio_device_next(fmt);
  1256. if (fmt) {
  1257. if (!strcmp(fmt->name, "lavfi"))
  1258. continue; //it's pointless to probe lavfi
  1259. if (dev && !av_match_name(dev, fmt->name))
  1260. continue;
  1261. print_device_sources(fmt, opts);
  1262. }
  1263. } while (fmt);
  1264. do {
  1265. fmt = av_input_video_device_next(fmt);
  1266. if (fmt) {
  1267. if (dev && !av_match_name(dev, fmt->name))
  1268. continue;
  1269. print_device_sources(fmt, opts);
  1270. }
  1271. } while (fmt);
  1272. fail:
  1273. av_dict_free(&opts);
  1274. av_free(dev);
  1275. av_log_set_level(error_level);
  1276. return ret;
  1277. }
  1278. int show_sinks(void *optctx, const char *opt, const char *arg)
  1279. {
  1280. const AVOutputFormat *fmt = NULL;
  1281. char *dev = NULL;
  1282. AVDictionary *opts = NULL;
  1283. int ret = 0;
  1284. int error_level = av_log_get_level();
  1285. av_log_set_level(AV_LOG_WARNING);
  1286. if ((ret = show_sinks_sources_parse_arg(arg, &dev, &opts)) < 0)
  1287. goto fail;
  1288. do {
  1289. fmt = av_output_audio_device_next(fmt);
  1290. if (fmt) {
  1291. if (dev && !av_match_name(dev, fmt->name))
  1292. continue;
  1293. print_device_sinks(fmt, opts);
  1294. }
  1295. } while (fmt);
  1296. do {
  1297. fmt = av_output_video_device_next(fmt);
  1298. if (fmt) {
  1299. if (dev && !av_match_name(dev, fmt->name))
  1300. continue;
  1301. print_device_sinks(fmt, opts);
  1302. }
  1303. } while (fmt);
  1304. fail:
  1305. av_dict_free(&opts);
  1306. av_free(dev);
  1307. av_log_set_level(error_level);
  1308. return ret;
  1309. }
  1310. #endif /* CONFIG_AVDEVICE */