[FFmpeg-devel] [PATCH 3/4] ffmpeg: add get_best_input_stream_index function

Clément Bœsch ubitux at gmail.com
Thu Apr 5 11:01:29 CEST 2012


On Sat, Mar 24, 2012 at 12:49:49PM +0100, Stefano Sabatini wrote:
> On date Thursday 2012-03-22 16:50:40 +0100, Clément Bœsch encoded:
> > From: Matthieu Bouron <matthieu.bouron at smartjog.com>
> > 
> > ---
> >  ffmpeg.c |   76 +++++++++++++++++++++++++++++++++++++++----------------------
> >  1 files changed, 48 insertions(+), 28 deletions(-)
> > 
> > diff --git a/ffmpeg.c b/ffmpeg.c
> > index 32286e2..305fdfa 100644
> > --- a/ffmpeg.c
> > +++ b/ffmpeg.c
> > @@ -3185,6 +3185,47 @@ static int transcode(OutputFile *output_files, int nb_output_files,
> >      return ret;
> >  }
> >  
> > +static int get_best_input_stream_index(enum AVMediaType type)
> > +{
> > +    int i, idx = -1, area = 0, channels = 0;
> > +
> > +    switch (type) {
> > +    case AVMEDIA_TYPE_VIDEO:
> > +        /* video: highest resolution */
> > +        for (i = 0; i < nb_input_streams; i++) {
> > +            InputStream *ist = &input_streams[i];
> > +            if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
> > +                ist->st->codec->width * ist->st->codec->height > area) {
> > +                area = ist->st->codec->width * ist->st->codec->height;
> > +                idx = i;
> > +            }
> > +        }
> > +        break;
> > +    case AVMEDIA_TYPE_AUDIO:
> > +        /* audio: most channels */
> 
> While at it also please use more idiot-proof comments, for example
> this could be: "select the audio stream with most channels"
> 

Added, rebased.

> [...]
> 
> Looks nice otherwise.

-- 
Clément B.
-------------- next part --------------
From 6ee470ab297c3137c47cce40839a639d3af166c2 Mon Sep 17 00:00:00 2001
From: Matthieu Bouron <matthieu.bouron at smartjog.com>
Date: Wed, 21 Mar 2012 18:59:36 +0100
Subject: [PATCH 3/4] ffmpeg: add get_best_input_stream_index() function.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Matthieu Bouron <matthieu.bouron at smartjog.com>
Signed-off-by: Clément Bœsch <clement.boesch at smartjog.com>
---
 ffmpeg.c |   78 ++++++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 48 insertions(+), 30 deletions(-)

diff --git a/ffmpeg.c b/ffmpeg.c
index 1e1a34c..bb4357f 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -3233,6 +3233,47 @@ static int transcode(OutputFile *output_files, int nb_output_files,
     return ret;
 }
 
+static int get_best_input_stream_index(enum AVMediaType type)
+{
+    int i, idx = -1, area = 0, channels = 0;
+
+    switch (type) {
+    case AVMEDIA_TYPE_VIDEO:
+        /* select video stream with the highest resolution */
+        for (i = 0; i < nb_input_streams; i++) {
+            const InputStream *ist = &input_streams[i];
+            if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
+                ist->st->codec->width * ist->st->codec->height > area) {
+                area = ist->st->codec->width * ist->st->codec->height;
+                idx = i;
+            }
+        }
+        break;
+    case AVMEDIA_TYPE_AUDIO:
+        /* select the audio stream with most channels */
+        for (i = 0; i < nb_input_streams; i++) {
+            const InputStream *ist = &input_streams[i];
+            if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
+                ist->st->codec->channels > channels) {
+                channels = ist->st->codec->channels;
+                idx = i;
+            }
+        }
+        break;
+    case AVMEDIA_TYPE_SUBTITLE:
+        /* pick the first subtitles stream */
+        for (i = 0; i < nb_input_streams; i++) {
+            const InputStream *ist = &input_streams[i];
+            if (ist->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
+                idx = i;
+                break;
+            }
+        }
+    }
+
+    return idx;
+}
+
 static int opt_frame_crop(const char *opt, const char *arg)
 {
     av_log(NULL, AV_LOG_FATAL, "Option '%s' has been removed, use the crop filter instead\n", opt);
@@ -4408,44 +4449,21 @@ static void opt_output_file(void *optctx, const char *filename)
     } else if (!o->nb_stream_maps) {
         /* pick the "best" stream of each type */
 
-        /* video: highest resolution */
         if (!o->video_disable && oc->oformat->video_codec != CODEC_ID_NONE) {
-            int area = 0, idx = -1;
-            for (i = 0; i < nb_input_streams; i++) {
-                ist = &input_streams[i];
-                if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
-                    ist->st->codec->width * ist->st->codec->height > area) {
-                    area = ist->st->codec->width * ist->st->codec->height;
-                    idx = i;
-                }
-            }
-            if (idx >= 0)
-                new_video_stream(o, oc, idx);
+            if ((i = get_best_input_stream_index(AVMEDIA_TYPE_VIDEO)) >= 0)
+                new_video_stream(o, oc, i);
         }
 
-        /* audio: most channels */
         if (!o->audio_disable && oc->oformat->audio_codec != CODEC_ID_NONE) {
-            int channels = 0, idx = -1;
-            for (i = 0; i < nb_input_streams; i++) {
-                ist = &input_streams[i];
-                if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
-                    ist->st->codec->channels > channels) {
-                    channels = ist->st->codec->channels;
-                    idx = i;
-                }
-            }
-            if (idx >= 0)
-                new_audio_stream(o, oc, idx);
+            if ((i = get_best_input_stream_index(AVMEDIA_TYPE_AUDIO)) >= 0)
+               new_audio_stream(o, oc, i);
         }
 
-        /* subtitles: pick first */
         if (!o->subtitle_disable && (oc->oformat->subtitle_codec != CODEC_ID_NONE || subtitle_codec_name)) {
-            for (i = 0; i < nb_input_streams; i++)
-                if (input_streams[i].st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
-                    new_subtitle_stream(o, oc, i);
-                    break;
-                }
+            if ((i = get_best_input_stream_index(AVMEDIA_TYPE_SUBTITLE)) >= 0)
+                new_subtitle_stream(o, oc, i);
         }
+
         /* do something with data? */
     } else {
         for (i = 0; i < o->nb_stream_maps; i++) {
-- 
1.7.9.1

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20120405/9965047e/attachment.asc>


More information about the ffmpeg-devel mailing list