diff --git a/libavformat/mpjpegdec.c b/libavformat/mpjpegdec.c
index e2a2ece9c4a71394ace00119ef7f1c258ef5e200..fda4c3868030171a05efdf6d1af1690342bebc97 100644
--- a/libavformat/mpjpegdec.c
+++ b/libavformat/mpjpegdec.c
@@ -123,6 +123,8 @@ static int mpjpeg_read_header(AVFormatContext *s)
         return AVERROR_INVALIDDATA;
 
     st = avformat_new_stream(s, NULL);
+    if (!st)
+        return AVERROR(ENOMEM);
 
     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
     st->codec->codec_id   = AV_CODEC_ID_MJPEG;