[FFmpeg-devel] cmdutils: fix finding next codec for id

Submitted by Josh de Kock on Feb. 7, 2018, 12:14 p.m.

Details

Message ID 20180207121442.GA5624@tengokunosoto.local
State New
Headers show

Commit Message

Josh de Kock Feb. 7, 2018, 12:14 p.m.
---
 fftools/cmdutils.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

Comments

Mark Thompson Feb. 7, 2018, 7:58 p.m.
On 07/02/18 12:14, Josh de Kock wrote:
> ---
>  fftools/cmdutils.c | 11 +++++++----
>  1 file changed, 7 insertions(+), 4 deletions(-)
> 
> diff --git a/fftools/cmdutils.c b/fftools/cmdutils.c
> index 0b06ccc71a..8bb9952016 100644
> --- a/fftools/cmdutils.c
> +++ b/fftools/cmdutils.c
> @@ -1424,11 +1424,14 @@ static char get_media_type_char(enum AVMediaType type)
>  static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
>                                          int encoder)
>  {
> +    const AVCodec *next = NULL;
>      void *i = 0;
> -    while ((prev = av_codec_iterate(&i))) {
> -        if (prev->id == id &&
> -            (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
> -            return prev;
> +
> +    while ((next = av_codec_iterate(&i))) {
> +        if (next->id == id &&
> +            (!prev || (strcmp(next->name, prev->name) > 0)) &&
> +            (encoder ? av_codec_is_encoder(next) : av_codec_is_decoder(next)))
> +            return next;
>      }
>      return NULL;
>  }
> 

This is wrong - the strcmp() will skip over matching codecs found later which have names earlier in the alphabet.  For example, for H.264 once you've found "libx264" (which is first) you skip over all of the "h264_foo" codecs which should be found after it after it because 'h' < 'l'.

- Mark

Patch hide | download patch | download mbox

diff --git a/fftools/cmdutils.c b/fftools/cmdutils.c
index 0b06ccc71a..8bb9952016 100644
--- a/fftools/cmdutils.c
+++ b/fftools/cmdutils.c
@@ -1424,11 +1424,14 @@  static char get_media_type_char(enum AVMediaType type)
 static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
                                         int encoder)
 {
+    const AVCodec *next = NULL;
     void *i = 0;
-    while ((prev = av_codec_iterate(&i))) {
-        if (prev->id == id &&
-            (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
-            return prev;
+
+    while ((next = av_codec_iterate(&i))) {
+        if (next->id == id &&
+            (!prev || (strcmp(next->name, prev->name) > 0)) &&
+            (encoder ? av_codec_is_encoder(next) : av_codec_is_decoder(next)))
+            return next;
     }
     return NULL;
 }