diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c
index fbbe23c2b81f1eeead625867a308644b22d6d903..4697e1d1d1524394ec175d6bf5a9b9eef8733747 100644
--- a/libavcodec/qsvenc.c
+++ b/libavcodec/qsvenc.c
@@ -972,7 +972,7 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q,
         ret = MFXVideoENCODE_EncodeFrameAsync(q->session, NULL, surf, bs, sync);
         if (ret == MFX_WRN_DEVICE_BUSY)
             av_usleep(1);
-    } while (ret > 0);
+    } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_WRN_IN_EXECUTION);
 
     if (ret < 0) {
         av_packet_unref(&new_pkt);