diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 0ce22ec4fa..7087f82ce1 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -3357,6 +3357,12 @@ typedef struct AVCodecContext {
      * - encoding: unused
      */
     int discard_damaged_percentage;
+
+    /*
+     * Video encoding only. Sets the maximum picture size for encoders that
+     * support adjusting the picture size dynamically during encoding.
+     */
+     int max_width, max_height;
 } AVCodecContext;
 
 #if FF_API_CODEC_GET_SET
diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c
index 304a684e0c..414707dcfd 100644
--- a/libavcodec/nvenc.c
+++ b/libavcodec/nvenc.c
@@ -384,6 +384,7 @@ static int nvenc_check_capabilities(AVCodecContext *avctx)
 #endif
 
     ctx->support_dyn_bitrate = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE);
+    ctx->support_dyn_res = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_RES_CHANGE);
 
     return 0;
 }
@@ -1121,6 +1122,8 @@ static av_cold int nvenc_setup_encoder(AVCodecContext *avctx)
 
     ctx->init_encode_params.encodeHeight = avctx->height;
     ctx->init_encode_params.encodeWidth = avctx->width;
+    ctx->init_encode_params.maxEncodeHeight = avctx->max_height;
+    ctx->init_encode_params.maxEncodeWidth = avctx->max_width;
 
     ctx->init_encode_params.encodeConfig = &ctx->encode_config;
 
@@ -1276,8 +1279,8 @@ static av_cold int nvenc_alloc_surface(AVCodecContext *avctx, int idx)
         }
 
         allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
-        allocSurf.width = avctx->width;
-        allocSurf.height = avctx->height;
+        allocSurf.width = FFMAX(avctx->max_width, avctx->width);
+        allocSurf.height = FFMAX(avctx->max_height, avctx->height);
         allocSurf.bufferFmt = ctx->surfaces[idx].format;
 
         nv_status = p_nvenc->nvEncCreateInputBuffer(ctx->nvencoder, &allocSurf);
@@ -1926,7 +1929,7 @@ static void reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame)
     NV_ENC_RECONFIGURE_PARAMS params = { 0 };
     int needs_reconfig = 0;
     int needs_encode_config = 0;
-    int reconfig_bitrate = 0, reconfig_dar = 0;
+    int reconfig_bitrate = 0, reconfig_dar = 0, reconfig_res = 0;
     int dw, dh;
 
     params.version = NV_ENC_RECONFIGURE_PARAMS_VER;
@@ -1986,6 +1989,24 @@ static void reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame)
         }
     }
 
+    if (ctx->support_dyn_res && ctx->init_encode_params.maxEncodeWidth && ctx->init_encode_params.maxEncodeHeight) {
+        if (params.reInitEncodeParams.encodeWidth != avctx->width || params.reInitEncodeParams.encodeHeight != avctx->height) {
+            av_log(avctx, AV_LOG_VERBOSE,
+                "resolution change: %d x %d -> %d x %d\n",
+                params.reInitEncodeParams.encodeWidth,
+                params.reInitEncodeParams.encodeHeight,
+                (uint32_t)avctx->width,
+                (uint32_t)avctx->height);
+
+            params.reInitEncodeParams.encodeWidth = avctx->width;
+            params.reInitEncodeParams.encodeHeight = avctx->height;
+            params.forceIDR = 1;
+
+            needs_reconfig = 1;
+            reconfig_res = 1;
+        }
+    }
+
     if (!needs_encode_config)
         params.reInitEncodeParams.encodeConfig = NULL;
 
@@ -2005,6 +2026,10 @@ static void reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame)
                 ctx->encode_config.rcParams.vbvBufferSize = params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize;
             }
 
+            if (reconfig_res) {
+                ctx->init_encode_params.encodeWidth = params.reInitEncodeParams.encodeWidth;
+                ctx->init_encode_params.encodeHeight = params.reInitEncodeParams.encodeHeight;
+            }
         }
     }
 }
diff --git a/libavcodec/nvenc.h b/libavcodec/nvenc.h
index ddd6168409..f3b959b31a 100644
--- a/libavcodec/nvenc.h
+++ b/libavcodec/nvenc.h
@@ -158,6 +158,7 @@ typedef struct NvencContext
     int first_packet_output;
 
     int support_dyn_bitrate;
+    int support_dyn_res;
 
     void *nvencoder;
 
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 309e4111cb..a217283b03 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -29,7 +29,7 @@
 
 #define LIBAVCODEC_VERSION_MAJOR  58
 #define LIBAVCODEC_VERSION_MINOR  47
-#define LIBAVCODEC_VERSION_MICRO 103
+#define LIBAVCODEC_VERSION_MICRO 104
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
                                                LIBAVCODEC_VERSION_MINOR, \
