diff --git a/libavcodec/huffyuvenc.c b/libavcodec/huffyuvenc.c
index 6b3ff7650511a24a6c22e72251874a90e9963a34..175b2567f4534a452af78ee030d461ce7a0df1f2 100644
--- a/libavcodec/huffyuvenc.c
+++ b/libavcodec/huffyuvenc.c
@@ -155,7 +155,7 @@ static av_cold int encode_init(AVCodecContext *avctx)
     s->version = 2;
 
     avctx->coded_frame = av_frame_alloc();
-    if (!avctx->coded_frame)
+    if (!avctx->extradata || !avctx->stats_out || !avctx->coded_frame)
         return AVERROR(ENOMEM);
 
     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;