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