diff --git a/libavcodec/h264.c b/libavcodec/h264.c
index b42468a781fead660db3e6a4fb7e51dc161343ee..dc4acb8379598b26e96e57f97ab66df60194af6d 100644
--- a/libavcodec/h264.c
+++ b/libavcodec/h264.c
@@ -3685,6 +3685,8 @@ static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){
             switch (hx->nal_unit_type) {
                 case NAL_SPS:
                 case NAL_PPS:
+                case NAL_IDR_SLICE:
+                case NAL_SLICE:
                     nals_needed = nal_index;
             }
             continue;