diff --git a/libavcodec/h264dec.c b/libavcodec/h264dec.c
index 1086eab8d3fc0a99b2bdee9a1463bdc091c386cf..faa502ed099ec5d758998d1dd599750d88bd0bfe 100644
--- a/libavcodec/h264dec.c
+++ b/libavcodec/h264dec.c
@@ -550,7 +550,7 @@ static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size)
                 break;
 
             if (avctx->active_thread_type & FF_THREAD_FRAME && !h->avctx->hwaccel &&
-                i >= nals_needed) {
+                i >= nals_needed && !h->setup_finished) {
                 ff_thread_finish_setup(avctx);
                 h->setup_finished = 1;
             }