diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
index ffd6cb1c25..b2782e6f9e 100644
--- a/libavcodec/vaapi_encode.c
+++ b/libavcodec/vaapi_encode.c
@@ -365,6 +365,17 @@ static int vaapi_encode_issue(AVCodecContext *avctx,
             goto fail;
     }
 
+#if VA_CHECK_VERSION(1, 5, 0)
+    if (ctx->max_frame_size) {
+        err = vaapi_encode_make_misc_param_buffer(avctx, pic,
+                                            VAEncMiscParameterTypeMultiPassFrameSize,
+                                            &ctx->mfs_params,
+                                            sizeof(ctx->mfs_params));
+        if (err < 0)
+            goto fail;
+    }
+#endif
+
     if (pic->type == PICTURE_TYPE_IDR) {
         if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
             ctx->codec->write_sequence_header) {
@@ -1869,6 +1880,54 @@ rc_mode_found:
     return 0;
 }
 
+static av_cold int vaapi_encode_init_max_frame_size(AVCodecContext *avctx)
+{
+#if VA_CHECK_VERSION(1, 5, 0)
+    VAAPIEncodeContext  *ctx = avctx->priv_data;
+    VAConfigAttrib      attr = { VAConfigAttribMaxFrameSize };
+    VAStatus vas;
+    int i;
+
+    vas = vaGetConfigAttributes(ctx->hwctx->display,
+                                    ctx->va_profile,
+                                    ctx->va_entrypoint,
+                                    &attr, 1);
+    if (vas != VA_STATUS_SUCCESS) {
+        ctx->max_frame_size = 0;
+        av_log(avctx, AV_LOG_ERROR, "Failed to query max frame size "
+               "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
+        return AVERROR_EXTERNAL;
+    }
+
+    if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
+        ctx->max_frame_size = 0;
+        av_log(avctx, AV_LOG_WARNING, "Max frame size attribute "
+               "is not supported.\n");
+    } else {
+        ctx->delta_qp = av_calloc(MFS_NUM_PASSES, sizeof(uint8_t));
+        if (!ctx->delta_qp) {
+            return AVERROR(ENOMEM);
+        }
+        for (i = 0; i <MFS_NUM_PASSES; i++)
+            ctx->delta_qp[i] = MFS_DELTA_QP;
+
+        ctx->mfs_params = (VAEncMiscParameterBufferMultiPassFrameSize){
+            .max_frame_size = ctx->max_frame_size,
+            .num_passes     = MFS_NUM_PASSES,
+            .delta_qp       = ctx->delta_qp,
+        };
+
+        av_log(avctx, AV_LOG_VERBOSE, "Max Frame Size: %d bytes.\n ",
+               ctx->max_frame_size);
+    }
+#else
+    av_log(avctx, AV_LOG_WARNING, "Max Frame Size is "
+                                    "not supported with this VA version.\n");
+#endif
+
+    return 0;
+}
+
 static av_cold int vaapi_encode_init_gop_structure(AVCodecContext *avctx)
 {
     VAAPIEncodeContext *ctx = avctx->priv_data;
@@ -2475,6 +2534,12 @@ av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
             goto fail;
     }
 
+    if (ctx->max_frame_size) {
+        err = vaapi_encode_init_max_frame_size(avctx);
+        if (err < 0)
+            goto fail;
+    }
+
     vas = vaCreateConfig(ctx->hwctx->display,
                          ctx->va_profile, ctx->va_entrypoint,
                          ctx->config_attributes, ctx->nb_config_attributes,
@@ -2618,6 +2683,8 @@ av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
 
     av_frame_free(&ctx->frame);
 
+    if (ctx->delta_qp)
+        av_freep(&ctx->delta_qp);
     av_freep(&ctx->codec_sequence_params);
     av_freep(&ctx->codec_picture_params);
     av_fifo_freep2(&ctx->encode_fifo);
diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h
index 1b40819c69..72f9256a8e 100644
--- a/libavcodec/vaapi_encode.h
+++ b/libavcodec/vaapi_encode.h
@@ -60,6 +60,11 @@ enum {
     PICTURE_TYPE_B   = 3,
 };
 
+enum {
+    MFS_NUM_PASSES = 4,
+    MFS_DELTA_QP   = 1,
+};
+
 typedef struct VAAPIEncodeSlice {
     int             index;
     int             row_start;
@@ -191,6 +196,9 @@ typedef struct VAAPIEncodeContext {
     // Desired B frame reference depth.
     int             desired_b_depth;
 
+    // Max Frame Size
+    int             max_frame_size;
+
     // Explicitly set RC mode (otherwise attempt to pick from
     // available modes).
     int             explicit_rc_mode;
@@ -271,7 +279,9 @@ typedef struct VAAPIEncodeContext {
 #if VA_CHECK_VERSION(0, 36, 0)
     VAEncMiscParameterBufferQualityLevel quality_params;
 #endif
-
+#if VA_CHECK_VERSION(1, 3, 0)
+    VAEncMiscParameterBufferMultiPassFrameSize mfs_params;
+#endif
     // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
     void           *codec_sequence_params;
 
@@ -331,6 +341,7 @@ typedef struct VAAPIEncodeContext {
     int idr_counter;
     int gop_counter;
     int end_of_stream;
+    uint8_t *delta_qp;
 
     // Whether the driver supports ROI at all.
     int             roi_allowed;
@@ -470,7 +481,11 @@ int ff_vaapi_encode_close(AVCodecContext *avctx);
       "Increase this to improve single channel performance. This option " \
       "doesn't work if driver doesn't implement vaSyncBuffer function.", \
       OFFSET(common.async_depth), AV_OPT_TYPE_INT, \
-      { .i64 = 2 }, 1, MAX_ASYNC_DEPTH, FLAGS }
+      { .i64 = 2 }, 1, MAX_ASYNC_DEPTH, FLAGS }, \
+    { "max_frame_size", \
+      "Maximum frame size (in bytes)",\
+      OFFSET(common.max_frame_size), AV_OPT_TYPE_INT, \
+      { .i64 = 0 }, 0, INT_MAX, FLAGS }
 
 #define VAAPI_ENCODE_RC_MODE(name, desc) \
     { #name, desc, 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_ ## name }, \
