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

Clément Bœsch ubitux at gmail.com
Thu Mar 22 16:50:40 CET 2012


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 */
+        for (i = 0; i < nb_input_streams; i++) {
+            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:
+        /* subtitles: pick first */
+        for (i = 0; i < nb_input_streams; i++) {
+            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);
@@ -4360,42 +4401,21 @@ static void opt_output_file(void *optctx, const char *filename)
             input_streams[index].st->discard = AVDISCARD_NONE;\
         }
 
-        /* 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;
-                }
-            }
-            NEW_STREAM(video, idx);
+            if ((i = get_best_input_stream_index(AVMEDIA_TYPE_VIDEO)) >= 0)
+                NEW_STREAM(video, 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;
-                }
-            }
-            NEW_STREAM(audio, idx);
+            if ((i = get_best_input_stream_index(AVMEDIA_TYPE_AUDIO)) >= 0)
+               NEW_STREAM(audio, 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_STREAM(subtitle, i);
-                    break;
-                }
+            if ((i = get_best_input_stream_index(AVMEDIA_TYPE_SUBTITLE)) >= 0)
+                NEW_STREAM(subtitle, i);
         }
+
         /* do something with data? */
     } else {
         for (i = 0; i < o->nb_stream_maps; i++) {
-- 
1.7.9.1



More information about the ffmpeg-devel mailing list