[FFmpeg-devel] [PATCH v3]avocdec/nvenc: Reconfigure bitrate on the fly

pkv.stream pkv.stream at gmail.com
Thu May 3 05:04:38 EEST 2018


Le 03/05/2018 à 12:21 AM, Timo Rothenpieler a écrit :
>> Hi
>> thanks for your feedback.
>> I was not sure of whether to put the capability check with the others 
>> in nvenc_check_capabilities because if the cap is missing this would 
>> remove the encoder altogether:
>>
>> ex: ln. 453 :     if ((ret = nvenc_check_capabilities(avctx)) < 0)
>>                          goto fail3;
>>
>> I'll move it there then or if you think it's preferrable, I can move 
>> it to ff_nvenc_encode_init and store it.
>> Which option do you prefer ? I'll resubmit and rebase once the other 
>> patch is committed.
>
> Just add a new field to the struct NvencContext.
> And there is no need to fail on this check, store its result and go 
> on, unless it's < 0, which indicates an actual error.
>
> There's also no need to check for a changed bitrate outside of the 
> call to reconfig_encoder, when making the same check inside of it again.
>
Hi
version 2 missed one occurence of:
  nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE)
(two checks are needed at two different places along with 
nvenc_check_capabilities(avctx) ).
Sorry for the double posting.
Thanks
-------------- next part --------------
From 20790a867b3c799300622176b506c8ed384961c6 Mon Sep 17 00:00:00 2001
From: pkviet <pkv.stream at gmail.com>
Date: Thu, 3 May 2018 02:15:52 +0200
Subject: [PATCH] avcodec/nvenc: Enable dynamic bitrate

The patch enables dynamic bitrate through ReconfigureEncoder method
from nvenc API.
This is useful for live streaming in case of network congestion.

Signed-off-by: pkviet <pkv.stream at gmail.com>
---
 libavcodec/nvenc.c | 19 +++++++++++++++++--
 libavcodec/nvenc.h |  1 +
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c
index 3313c376fe..d0598d379e 100644
--- a/libavcodec/nvenc.c
+++ b/libavcodec/nvenc.c
@@ -457,6 +457,8 @@ static av_cold int nvenc_check_device(AVCodecContext *avctx, int idx)
     if ((ret = nvenc_check_capabilities(avctx)) < 0)
         goto fail3;
 
+    ctx->dynamic_bitrate = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE);
+
     av_log(avctx, loglevel, "supports NVENC\n");
 
     dl_fn->nvenc_device_count++;
@@ -550,6 +552,8 @@ static av_cold int nvenc_setup_device(AVCodecContext *avctx)
             av_log(avctx, AV_LOG_FATAL, "Provided device doesn't support required NVENC features\n");
             return ret;
         }
+        ctx->dynamic_bitrate = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE);
+
     } else {
         int i, nb_devices = 0;
 
@@ -1951,8 +1955,20 @@ static int reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame)
 
         params.reInitEncodeParams.darHeight = dh;
         params.reInitEncodeParams.darWidth = dw;
+        ctx->init_encode_params.darHeight = dh;
+        ctx->init_encode_params.darWidth = dw;
+        needs_reconfig = 1;
+    }
 
+    if (ctx->dynamic_bitrate > 0 && ctx->rc != NV_ENC_PARAMS_RC_CONSTQP
+        && ctx->encode_config.rcParams.averageBitRate != avctx->bit_rate) {
+        params.version = NV_ENC_RECONFIGURE_PARAMS_VER;
+        params.resetEncoder = 1;
+        params.forceIDR = 1;
+        params.reInitEncodeParams = ctx->init_encode_params;
+        params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate = avctx->bit_rate;
         needs_reconfig = 1;
+        needs_encode_config = 1;
     }
 
     if (!needs_encode_config)
@@ -1963,8 +1979,7 @@ static int reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame)
         if (ret != NV_ENC_SUCCESS) {
             nvenc_print_error(avctx, ret, "failed to reconfigure nvenc");
         } else {
-            ctx->init_encode_params.darHeight = dh;
-            ctx->init_encode_params.darWidth = dw;
+            av_log(avctx, AV_LOG_INFO, "nvencReconfigureEncoder succeeded\n");
         }
     }
 
diff --git a/libavcodec/nvenc.h b/libavcodec/nvenc.h
index c7506d6a15..fd2350aaa6 100644
--- a/libavcodec/nvenc.h
+++ b/libavcodec/nvenc.h
@@ -184,6 +184,7 @@ typedef struct NvencContext
     int weighted_pred;
     int coder;
     int b_ref_mode;
+    int dynamic_bitrate;
 } NvencContext;
 
 int ff_nvenc_encode_init(AVCodecContext *avctx);
-- 
2.16.2.windows.1



More information about the ffmpeg-devel mailing list