diff --git a/libavformat/avidec.c b/libavformat/avidec.c
index 96edfa88ea5177e7edd88ab2940dc1175692a830..2c81267e12fdbc2232dd3f5c8a664a076b6d04f0 100644
--- a/libavformat/avidec.c
+++ b/libavformat/avidec.c
@@ -1101,6 +1101,8 @@ static int read_gab2_sub(AVFormatContext *s, AVStream *st, AVPacket *pkt)
             goto error;
 
         if (!avformat_open_input(&ast->sub_ctx, "", sub_demuxer, NULL)) {
+            if (ast->sub_ctx->nb_streams != 1)
+                goto error;
             ff_read_packet(ast->sub_ctx, &ast->sub_pkt);
             avcodec_parameters_copy(st->codecpar, ast->sub_ctx->streams[0]->codecpar);
             time_base = ast->sub_ctx->streams[0]->time_base;