[FFmpeg-devel] [PATCH] avformat/flvdec: Ignore the first two data/subtitle streams.

Josh Allmann joshua.allmann at gmail.com
Fri May 14 02:37:57 EEST 2021


Previously, one or the other would have been ignored, but not both.
Since the probe terminates at three streams, it could exit
prematurely if both data and subtitles are present along with
slightly trailing media, usually video trailing audio.

Trailing media is common in RTMP, and encoders write strange metadata.
---
 libavformat/flvdec.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/libavformat/flvdec.c b/libavformat/flvdec.c
index 4b9f46902b..1be8d98618 100644
--- a/libavformat/flvdec.c
+++ b/libavformat/flvdec.c
@@ -134,18 +134,32 @@ static void add_keyframes_index(AVFormatContext *s)
     }
 }
 
+static int is_ignorable(enum AVMediaType codec_type)
+{
+    switch(codec_type) {
+    case AVMEDIA_TYPE_SUBTITLE:
+    case AVMEDIA_TYPE_DATA:
+        return 1;
+    }
+    return 0;
+}
+
 static AVStream *create_stream(AVFormatContext *s, int codec_type)
 {
+    int streams_to_ignore = 0, nb_streams = 0;
     FLVContext *flv   = s->priv_data;
     AVStream *st = avformat_new_stream(s, NULL);
     if (!st)
         return NULL;
     st->codecpar->codec_type = codec_type;
-    if (s->nb_streams>=3 ||(   s->nb_streams==2
-                           && s->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE
-                           && s->streams[1]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE
-                           && s->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_DATA
-                           && s->streams[1]->codecpar->codec_type != AVMEDIA_TYPE_DATA))
+
+    if (s->nb_streams >= 1)
+        streams_to_ignore += is_ignorable(s->streams[0]->codecpar->codec_type);
+    if (s->nb_streams >= 2)
+        streams_to_ignore += is_ignorable(s->streams[1]->codecpar->codec_type);
+
+    nb_streams = s->nb_streams - streams_to_ignore;
+    if (nb_streams >= 2)
         s->ctx_flags &= ~AVFMTCTX_NOHEADER;
     if (codec_type == AVMEDIA_TYPE_AUDIO) {
         st->codecpar->bit_rate = flv->audio_bit_rate;
-- 
2.17.1



More information about the ffmpeg-devel mailing list