[FFmpeg-devel] [PATCH] avcodec/nvenc: support dynamic resolution change

leozhang leozhang at qiyi.com
Fri Aug 7 11:18:28 EEST 2020


Allow dynamic resolution change, this is useful for real time video communication application.

Use below commands to test it,
ffmpeg -i reinit-large_420_8-to-small_420_8.h264 -noautoscale -c:v hevc_nvenc out.265 -loglevel verbose -y
ffmpeg -i reinit-large_420_8-to-small_420_8.h264 -noautoscale -c:v h264_nvenc out.264 -loglevel verbose -y

Signed-off-by: leozhang <leozhang at qiyi.com>
---
 libavcodec/nvenc.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c
index 8f5036b..68e2a35 100644
--- a/libavcodec/nvenc.c
+++ b/libavcodec/nvenc.c
@@ -2051,7 +2051,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;
@@ -2071,6 +2071,21 @@ static void reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame)
         reconfig_dar = 1;
     }
 
+    if (frame && (frame->width != ctx->init_encode_params.encodeWidth ||
+                  frame->height != ctx->init_encode_params.encodeHeight)) {
+        av_log(avctx, AV_LOG_VERBOSE,
+               "input frame changed from %dx%d -> %dx%d\n",
+               ctx->init_encode_params.encodeWidth,
+               ctx->init_encode_params.encodeHeight,
+               frame->width, frame->height);
+
+        params.reInitEncodeParams.encodeWidth  = frame->width;
+        params.reInitEncodeParams.encodeHeight = frame->height;
+
+        needs_reconfig = 1;
+        reconfig_res = 1;
+    }
+
     if (ctx->rc != NV_ENC_PARAMS_RC_CONSTQP && ctx->support_dyn_bitrate) {
         if (avctx->bit_rate > 0 && params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate != avctx->bit_rate) {
             av_log(avctx, AV_LOG_VERBOSE,
@@ -2124,6 +2139,11 @@ static void reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame)
                 ctx->init_encode_params.darWidth = dw;
             }
 
+            if (reconfig_res) {
+                avctx->width  = ctx->init_encode_params.encodeWidth  = params.reInitEncodeParams.encodeWidth;
+                avctx->height = ctx->init_encode_params.encodeHeight = params.reInitEncodeParams.encodeHeight;
+            }
+
             if (reconfig_bitrate) {
                 ctx->encode_config.rcParams.averageBitRate = params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate;
                 ctx->encode_config.rcParams.maxBitRate = params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate;
-- 
1.8.3.1



More information about the ffmpeg-devel mailing list