opt_common.c 47 KB

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