From a3ad68d36c0bff87c525035b3f745bb274b00d41 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Mon, 13 Aug 2012 20:06:25 +0200 Subject: [PATCH] cmdutils: extend -h to allow printing codec details. --- avconv_filter.c | 14 ---- avconv_opt.c | 5 +- avplay.c | 6 +- avprobe.c | 3 +- avserver.c | 2 +- cmdutils.c | 120 ++++++++++++++++++++++++++++++++++- cmdutils.h | 30 +++++++++ cmdutils_common_opts.h | 8 +-- doc/avtools-common-opts.texi | 16 ++++- 9 files changed, 172 insertions(+), 32 deletions(-) diff --git a/avconv_filter.c b/avconv_filter.c index d0ab4dcaf8..4e5c1ecb03 100644 --- a/avconv_filter.c +++ b/avconv_filter.c @@ -59,29 +59,15 @@ static char *choose_ ## var ## s(OutputStream *ost) \ return NULL; \ } -#define GET_PIX_FMT_NAME(pix_fmt)\ - const char *name = av_get_pix_fmt_name(pix_fmt); - DEF_CHOOSE_FORMAT(enum PixelFormat, pix_fmt, pix_fmts, PIX_FMT_NONE, GET_PIX_FMT_NAME, ":") -#define GET_SAMPLE_FMT_NAME(sample_fmt)\ - const char *name = av_get_sample_fmt_name(sample_fmt) - DEF_CHOOSE_FORMAT(enum AVSampleFormat, sample_fmt, sample_fmts, AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME, ",") -#define GET_SAMPLE_RATE_NAME(rate)\ - char name[16];\ - snprintf(name, sizeof(name), "%d", rate); - DEF_CHOOSE_FORMAT(int, sample_rate, supported_samplerates, 0, GET_SAMPLE_RATE_NAME, ",") -#define GET_CH_LAYOUT_NAME(ch_layout)\ - char name[16];\ - snprintf(name, sizeof(name), "0x%"PRIx64, ch_layout); - DEF_CHOOSE_FORMAT(uint64_t, channel_layout, channel_layouts, 0, GET_CH_LAYOUT_NAME, ",") diff --git a/avconv_opt.c b/avconv_opt.c index ac1a65ecfb..ef591a462a 100644 --- a/avconv_opt.c +++ b/avconv_opt.c @@ -1789,10 +1789,10 @@ static int opt_filter_complex(const char *opt, const char *arg) return 0; } -static int show_help(const char *opt, const char *arg) +void show_help_default(const char *opt, const char *arg) { int flags = AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM; - av_log_set_callback(log_callback_help); + show_usage(); show_help_options(options, "Main options:", 0, OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_SUBTITLE); @@ -1812,7 +1812,6 @@ static int show_help(const char *opt, const char *arg) show_help_children(avcodec_get_class(), flags); show_help_children(avformat_get_class(), flags); show_help_children(sws_get_class(), flags); - return 0; } void show_usage(void) diff --git a/avplay.c b/avplay.c index 64d7b3da33..4322802e2c 100644 --- a/avplay.c +++ b/avplay.c @@ -224,8 +224,6 @@ typedef struct VideoState { int refresh; } VideoState; -static int show_help(const char *opt, const char *arg); - /* options specified by the user */ static AVInputFormat *file_iformat; static const char *input_filename; @@ -2922,7 +2920,7 @@ static void show_usage(void) printf("\n"); } -static int show_help(const char *opt, const char *arg) +void show_help_default(const char *opt, const char *arg) { av_log_set_callback(log_callback_help); show_usage(); @@ -2947,8 +2945,6 @@ static int show_help(const char *opt, const char *arg) "down/up seek backward/forward 1 minute\n" "mouse click seek to percentage in file corresponding to fraction of width\n" ); - - return 0; } static void opt_input_file(void *optctx, const char *filename) diff --git a/avprobe.c b/avprobe.c index 0116acb005..b708cb6683 100644 --- a/avprobe.c +++ b/avprobe.c @@ -868,14 +868,13 @@ static void opt_input_file(void *optctx, const char *arg) input_filename = arg; } -static int show_help(const char *opt, const char *arg) +void show_help_default(const char *opt, const char *arg) { av_log_set_callback(log_callback_help); show_usage(); show_help_options(options, "Main options:", 0, 0); printf("\n"); show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM); - return 0; } static void opt_pretty(void) diff --git a/avserver.c b/avserver.c index de050a6cce..c97b4ca213 100644 --- a/avserver.c +++ b/avserver.c @@ -4629,7 +4629,7 @@ static void opt_debug(void) logfilename[0] = '-'; } -static void show_help(void) +void show_help_default(const char *opt, const char *arg) { printf("usage: avserver [options]\n" "Hyper fast multi format Audio/Video streaming server\n"); diff --git a/cmdutils.c b/cmdutils.c index bc5cb8aae2..55cf3f082a 100644 --- a/cmdutils.c +++ b/cmdutils.c @@ -132,7 +132,7 @@ void show_help_options(const OptionDef *options, const char *msg, int req_flags, first = 0; } av_strlcpy(buf, po->name, sizeof(buf)); - if (po->flags & HAS_ARG) { + if (po->argname) { av_strlcat(buf, " ", sizeof(buf)); av_strlcat(buf, po->argname, sizeof(buf)); } @@ -642,6 +642,65 @@ int show_formats(const char *opt, const char *arg) return 0; } +#define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \ + if (codec->field) { \ + const type *p = c->field; \ + \ + printf(" Supported " list_name ":"); \ + while (*p != term) { \ + get_name(*p); \ + printf(" %s", name); \ + p++; \ + } \ + printf("\n"); \ + } \ + +static void print_codec(const AVCodec *c) +{ + int encoder = av_codec_is_encoder(c); + + printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name, + c->long_name ? c->long_name : ""); + + if (c->type == AVMEDIA_TYPE_VIDEO) { + printf(" Threading capabilities: "); + switch (c->capabilities & (CODEC_CAP_FRAME_THREADS | + CODEC_CAP_SLICE_THREADS)) { + case CODEC_CAP_FRAME_THREADS | + CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break; + case CODEC_CAP_FRAME_THREADS: printf("frame"); break; + case CODEC_CAP_SLICE_THREADS: printf("slice"); break; + default: printf("no"); break; + } + printf("\n"); + } + + if (c->supported_framerates) { + const AVRational *fps = c->supported_framerates; + + printf(" Supported framerates:"); + while (fps->num) { + printf(" %d/%d", fps->num, fps->den); + fps++; + } + printf("\n"); + } + PRINT_CODEC_SUPPORTED(c, pix_fmts, enum PixelFormat, "pixel formats", + PIX_FMT_NONE, GET_PIX_FMT_NAME); + PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0, + GET_SAMPLE_RATE_NAME); + PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats", + AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME); + PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts", + 0, GET_CH_LAYOUT_DESC); + + if (c->priv_class) { + show_help_children(c->priv_class, + AV_OPT_FLAG_ENCODING_PARAM | + AV_OPT_FLAG_DECODING_PARAM); + } +} + static char get_media_type_char(enum AVMediaType type) { switch (type) { @@ -842,6 +901,65 @@ int show_sample_fmts(const char *opt, const char *arg) return 0; } +static void show_help_codec(const char *name, int encoder) +{ + const AVCodecDescriptor *desc; + const AVCodec *codec; + + if (!name) { + av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n"); + return; + } + + codec = encoder ? avcodec_find_encoder_by_name(name) : + avcodec_find_decoder_by_name(name); + + if (codec) + print_codec(codec); + else if ((desc = avcodec_descriptor_get_by_name(name))) { + int printed = 0; + + while ((codec = next_codec_for_id(desc->id, codec, encoder))) { + printed = 1; + print_codec(codec); + } + + if (!printed) { + av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to Libav, " + "but no %s for it are available. Libav might need to be " + "recompiled with additional external libraries.\n", + name, encoder ? "encoders" : "decoders"); + } + } else { + av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by Libav.\n", + name); + } +} + +int show_help(const char *opt, const char *arg) +{ + char *topic, *par; + av_log_set_callback(log_callback_help); + + topic = av_strdup(arg ? arg : ""); + par = strchr(topic, '='); + if (par) + *par++ = 0; + + if (!*topic) { + show_help_default(topic, par); + } else if (!strcmp(topic, "decoder")) { + show_help_codec(par, 0); + } else if (!strcmp(topic, "encoder")) { + show_help_codec(par, 1); + } else { + show_help_default(topic, par); + } + + av_freep(&topic); + return 0; +} + int read_yesno(void) { int c = getchar(); diff --git a/cmdutils.h b/cmdutils.h index eb8a22a003..39125ce4ba 100644 --- a/cmdutils.h +++ b/cmdutils.h @@ -170,6 +170,17 @@ void show_help_options(const OptionDef *options, const char *msg, int req_flags, */ void show_help_children(const AVClass *class, int flags); +/** + * Per-avtool specific help handler. Implemented in each + * avtool, called by show_help(). + */ +void show_help_default(const char *opt, const char *arg); + +/** + * Generic -h handler common to all avtools. + */ +int show_help(const char *opt, const char *arg); + /** * Parse the command line arguments. * @@ -446,4 +457,23 @@ void filter_release_buffer(AVFilterBuffer *fb); * buffers have been released. */ void free_buffer_pool(FrameBuffer **pool); + +#define GET_PIX_FMT_NAME(pix_fmt)\ + const char *name = av_get_pix_fmt_name(pix_fmt); + +#define GET_SAMPLE_FMT_NAME(sample_fmt)\ + const char *name = av_get_sample_fmt_name(sample_fmt) + +#define GET_SAMPLE_RATE_NAME(rate)\ + char name[16];\ + snprintf(name, sizeof(name), "%d", rate); + +#define GET_CH_LAYOUT_NAME(ch_layout)\ + char name[16];\ + snprintf(name, sizeof(name), "0x%"PRIx64, ch_layout); + +#define GET_CH_LAYOUT_DESC(ch_layout)\ + char name[128];\ + av_get_channel_layout_string(name, sizeof(name), 0, ch_layout); + #endif /* LIBAV_CMDUTILS_H */ diff --git a/cmdutils_common_opts.h b/cmdutils_common_opts.h index d66a82ecbc..619cd896ce 100644 --- a/cmdutils_common_opts.h +++ b/cmdutils_common_opts.h @@ -1,8 +1,8 @@ { "L" , OPT_EXIT, {.func_arg = show_license}, "show license" }, - { "h" , OPT_EXIT, {.func_arg = show_help}, "show help" }, - { "?" , OPT_EXIT, {.func_arg = show_help}, "show help" }, - { "help" , OPT_EXIT, {.func_arg = show_help}, "show help" }, - { "-help" , OPT_EXIT, {.func_arg = show_help}, "show help" }, + { "h" , OPT_EXIT, {.func_arg = show_help}, "show help", "topic" }, + { "?" , OPT_EXIT, {.func_arg = show_help}, "show help", "topic" }, + { "help" , OPT_EXIT, {.func_arg = show_help}, "show help", "topic" }, + { "-help" , OPT_EXIT, {.func_arg = show_help}, "show help", "topic" }, { "version" , OPT_EXIT, {.func_arg = show_version}, "show version" }, { "formats" , OPT_EXIT, {.func_arg = show_formats }, "show available formats" }, { "codecs" , OPT_EXIT, {.func_arg = show_codecs }, "show available codecs" }, diff --git a/doc/avtools-common-opts.texi b/doc/avtools-common-opts.texi index 375e4b041c..eefb37bfcf 100644 --- a/doc/avtools-common-opts.texi +++ b/doc/avtools-common-opts.texi @@ -51,8 +51,20 @@ These options are shared amongst the av* tools. @item -L Show license. -@item -h, -?, -help, --help -Show help. +@item -h, -?, -help, --help [@var{arg}] +Show help. An optional parameter may be specified to print help about a specific +item. + +Possible values of @var{arg} are: +@table @option +@item decoder=@var{decoder_name} +Print detailed information about the decoder named @var{decoder_name}. Use the +@option{-decoders} option to get a list of all decoders. + +@item encoder=@var{encoder_name} +Print detailed information about the encoder named @var{encoder_name}. Use the +@option{-encoders} option to get a list of all encoders. +@end table @item -version Show version.