opt_common.c 48 KB

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