[FFmpeg-devel] [PATCH] cmdutils: sort codec list.

Nicolas George nicolas.george at normalesup.org
Sat Sep 22 19:31:55 CEST 2012


The list is sorted first by type (video first)
and then alphabetically by name.

Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---
 cmdutils.c |   25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/cmdutils.c b/cmdutils.c
index bd4ba4a..558b332 100644
--- a/cmdutils.c
+++ b/cmdutils.c
@@ -890,6 +890,15 @@ static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
     return NULL;
 }
 
+static int compare_codec_desc(const void *a, const void *b)
+{
+    const AVCodecDescriptor * const *da = a;
+    const AVCodecDescriptor * const *db = b;
+
+    return (*da)->type != (*db)->type ? (*da)->type - (*db)->type :
+           strcmp((*da)->name, (*db)->name);
+}
+
 static void print_codecs_for_id(enum AVCodecID id, int encoder)
 {
     const AVCodec *codec = NULL;
@@ -905,6 +914,18 @@ static void print_codecs_for_id(enum AVCodecID id, int encoder)
 int show_codecs(void *optctx, const char *opt, const char *arg)
 {
     const AVCodecDescriptor *desc = NULL;
+    const AVCodecDescriptor **codecs;
+    unsigned nb_codecs = 0, i = 0;
+
+    while ((desc = avcodec_descriptor_next(desc)))
+        nb_codecs++;
+    if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs))))
+        return AVERROR(ENOMEM);
+    desc = NULL;
+    while ((desc = avcodec_descriptor_next(desc)))
+        codecs[i++] = desc;
+    av_assert0(i == nb_codecs);
+    qsort(codecs, nb_codecs, sizeof(*codecs), compare_codec_desc);
 
     printf("Codecs:\n"
            " D..... = Decoding supported\n"
@@ -916,8 +937,9 @@ int show_codecs(void *optctx, const char *opt, const char *arg)
            " ....L. = Lossy compression\n"
            " .....S = Lossless compression\n"
            " -------\n");
-    while ((desc = avcodec_descriptor_next(desc))) {
+    for (i = 0; i < nb_codecs; i++) {
         const AVCodec *codec = NULL;
+        desc = codecs[i];
 
         printf(" ");
         printf(avcodec_find_decoder(desc->id) ? "D" : ".");
@@ -948,6 +970,7 @@ int show_codecs(void *optctx, const char *opt, const char *arg)
 
         printf("\n");
     }
+    av_free(codecs);
     return 0;
 }
 
-- 
1.7.10.4



More information about the ffmpeg-devel mailing list