[FFmpeg-cvslog] avcodec/nvenc: use encoder level options for qmin/qmax

Timo Rothenpieler git at videolan.org
Sun Feb 2 21:04:20 EET 2025


ffmpeg | branch: master | Timo Rothenpieler <timo at rothenpieler.org> | Thu Jan  9 00:14:04 2025 +0100| [89b37b4dcb2d91f9e5fd457f89b2cd8c94a217f9] | committer: Timo Rothenpieler

avcodec/nvenc: use encoder level options for qmin/qmax

AV1 uses a vastly different range than what the global options permit,
and also for the other codecs the range of the global options is at
least misaligned.

Fixes #11365

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=89b37b4dcb2d91f9e5fd457f89b2cd8c94a217f9
---

 libavcodec/nvenc.c      | 49 ++++++++++++++++++++++++++++++-------------------
 libavcodec/nvenc.h      |  2 ++
 libavcodec/nvenc_av1.c  |  4 ++++
 libavcodec/nvenc_h264.c |  4 ++++
 libavcodec/nvenc_hevc.c |  4 ++++
 5 files changed, 44 insertions(+), 19 deletions(-)

diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c
index 3403fa8996..fc8678cdfe 100644
--- a/libavcodec/nvenc.c
+++ b/libavcodec/nvenc.c
@@ -884,8 +884,8 @@ static av_cold void set_constqp(AVCodecContext *avctx)
             rc->constQP.qpIntra = av_clip(ctx->cqp * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, qmax);
     }
 
-    avctx->qmin = -1;
-    avctx->qmax = -1;
+    avctx->qmin = ctx->qmin = -1;
+    avctx->qmax = ctx->qmax = -1;
 }
 
 static av_cold void set_vbr(AVCodecContext *avctx)
@@ -899,27 +899,37 @@ static av_cold void set_vbr(AVCodecContext *avctx)
     int qmax = 51;
 #endif
 
-    if (avctx->qmin >= 0 && avctx->qmax >= 0) {
+    if (avctx->qmin >= 0 || avctx->qmax >= 0)
+        av_log(avctx, AV_LOG_WARNING, "Passing qmin/qmax via global AVCodecContext options. Use encoder options instead.\n");
+
+    if (avctx->qmin >= 0 && ctx->qmin < 0)
+        ctx->qmin = avctx->qmin;
+    if (avctx->qmax >= 0 && ctx->qmax < 0)
+        ctx->qmax = avctx->qmax;
+    avctx->qmin = ctx->qmin;
+    avctx->qmax = ctx->qmax;
+
+    if (ctx->qmin >= 0 && ctx->qmax >= 0) {
         rc->enableMinQP = 1;
         rc->enableMaxQP = 1;
 
-        rc->minQP.qpInterB = avctx->qmin;
-        rc->minQP.qpInterP = avctx->qmin;
-        rc->minQP.qpIntra  = avctx->qmin;
+        rc->minQP.qpInterB = ctx->qmin;
+        rc->minQP.qpInterP = ctx->qmin;
+        rc->minQP.qpIntra  = ctx->qmin;
 
-        rc->maxQP.qpInterB = avctx->qmax;
-        rc->maxQP.qpInterP = avctx->qmax;
-        rc->maxQP.qpIntra = avctx->qmax;
+        rc->maxQP.qpInterB = ctx->qmax;
+        rc->maxQP.qpInterP = ctx->qmax;
+        rc->maxQP.qpIntra = ctx->qmax;
 
-        qp_inter_p = (avctx->qmax + 3 * avctx->qmin) / 4; // biased towards Qmin
-    } else if (avctx->qmin >= 0) {
+        qp_inter_p = (ctx->qmax + 3 * ctx->qmin) / 4; // biased towards Qmin
+    } else if (ctx->qmin >= 0) {
         rc->enableMinQP = 1;
 
-        rc->minQP.qpInterB = avctx->qmin;
-        rc->minQP.qpInterP = avctx->qmin;
-        rc->minQP.qpIntra = avctx->qmin;
+        rc->minQP.qpInterB = ctx->qmin;
+        rc->minQP.qpInterP = ctx->qmin;
+        rc->minQP.qpIntra = ctx->qmin;
 
-        qp_inter_p = avctx->qmin;
+        qp_inter_p = ctx->qmin;
     } else {
         qp_inter_p = 26; // default to 26
     }
@@ -965,8 +975,8 @@ static av_cold void set_lossless(AVCodecContext *avctx)
     rc->constQP.qpInterP = 0;
     rc->constQP.qpIntra  = 0;
 
-    avctx->qmin = -1;
-    avctx->qmax = -1;
+    avctx->qmin = ctx->qmin = -1;
+    avctx->qmax = ctx->qmax = -1;
 }
 
 static void nvenc_override_rate_control(AVCodecContext *avctx)
@@ -980,7 +990,7 @@ static void nvenc_override_rate_control(AVCodecContext *avctx)
         return;
 #ifndef NVENC_NO_DEPRECATED_RC
     case NV_ENC_PARAMS_RC_VBR_MINQP:
-        if (avctx->qmin < 0) {
+        if (avctx->qmin < 0 && ctx->qmin < 0) {
             av_log(avctx, AV_LOG_WARNING,
                    "The variable bitrate rate-control requires "
                    "the 'qmin' option set.\n");
@@ -1103,7 +1113,8 @@ static av_cold int nvenc_setup_rate_control(AVCodecContext *avctx)
             ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
         } else if (ctx->twopass) {
             ctx->rc = NV_ENC_PARAMS_RC_VBR_HQ;
-        } else if (avctx->qmin >= 0 && avctx->qmax >= 0) {
+        } else if ((avctx->qmin >= 0 && avctx->qmax >= 0) ||
+                   (ctx->qmin >= 0 && ctx->qmax >= 0)) {
             ctx->rc = NV_ENC_PARAMS_RC_VBR_MINQP;
         }
     }
diff --git a/libavcodec/nvenc.h b/libavcodec/nvenc.h
index 6f7f8d4e7f..341a242cf7 100644
--- a/libavcodec/nvenc.h
+++ b/libavcodec/nvenc.h
@@ -271,6 +271,8 @@ typedef struct NvencContext
     float quality;
     int aud;
     int bluray_compat;
+    int qmin;
+    int qmax;
     int init_qp_p;
     int init_qp_b;
     int init_qp_i;
diff --git a/libavcodec/nvenc_av1.c b/libavcodec/nvenc_av1.c
index 5dd27fe872..ceb046e4be 100644
--- a/libavcodec/nvenc_av1.c
+++ b/libavcodec/nvenc_av1.c
@@ -119,6 +119,10 @@ static const AVOption options[] = {
                                                             OFFSET(qp_cb_offset), AV_OPT_TYPE_INT,   { .i64 = 0 }, -12, 12, VE },
     { "qp_cr_offset", "Quantization parameter offset for cr channel",
                                                             OFFSET(qp_cr_offset), AV_OPT_TYPE_INT,   { .i64 = 0 }, -12, 12, VE },
+    { "qmin",         "Specifies the minimum QP used for rate control",
+                                                            OFFSET(qmin),         AV_OPT_TYPE_INT,   { .i64 = -1 }, -1, 255, VE },
+    { "qmax",         "Specifies the maximum QP used for rate control",
+                                                            OFFSET(qmax),         AV_OPT_TYPE_INT,   { .i64 = -1 }, -1, 255, VE },
     { "no-scenecut",  "When lookahead is enabled, set this to 1 to disable adaptive I-frame insertion at scene cuts",
                                                             OFFSET(no_scenecut),  AV_OPT_TYPE_BOOL,  { .i64 = 0 }, 0, 1, VE },
     { "forced-idr",   "If forcing keyframes, force them as IDR frames.",
diff --git a/libavcodec/nvenc_h264.c b/libavcodec/nvenc_h264.c
index 5e9f73412f..c7267a082a 100644
--- a/libavcodec/nvenc_h264.c
+++ b/libavcodec/nvenc_h264.c
@@ -174,6 +174,10 @@ static const AVOption options[] = {
                                                             OFFSET(qp_cb_offset), AV_OPT_TYPE_INT,   { .i64 = 0 }, -12, 12, VE },
     { "qp_cr_offset", "Quantization parameter offset for cr channel",
                                                             OFFSET(qp_cr_offset), AV_OPT_TYPE_INT,   { .i64 = 0 }, -12, 12, VE },
+    { "qmin",         "Specifies the minimum QP used for rate control",
+                                                            OFFSET(qmin),         AV_OPT_TYPE_INT,   { .i64 = -1 }, -1, 51, VE },
+    { "qmax",         "Specifies the maximum QP used for rate control",
+                                                            OFFSET(qmax),         AV_OPT_TYPE_INT,   { .i64 = -1 }, -1, 51, VE },
     { "weighted_pred","Set 1 to enable weighted prediction",
                                                             OFFSET(weighted_pred),AV_OPT_TYPE_INT,   { .i64 = 0 }, 0, 1, VE },
     { "coder",        "Coder type",                         OFFSET(coder),        AV_OPT_TYPE_INT,   { .i64 = -1                                         },-1, 2, VE, .unit = "coder" },
diff --git a/libavcodec/nvenc_hevc.c b/libavcodec/nvenc_hevc.c
index 5696e14dd4..d92b4272b1 100644
--- a/libavcodec/nvenc_hevc.c
+++ b/libavcodec/nvenc_hevc.c
@@ -158,6 +158,10 @@ static const AVOption options[] = {
                                                             OFFSET(qp_cb_offset), AV_OPT_TYPE_INT,   { .i64 = 0 }, -12, 12, VE },
     { "qp_cr_offset", "Quantization parameter offset for cr channel",
                                                             OFFSET(qp_cr_offset), AV_OPT_TYPE_INT,   { .i64 = 0 }, -12, 12, VE },
+    { "qmin",         "Specifies the minimum QP used for rate control",
+                                                            OFFSET(qmin),         AV_OPT_TYPE_INT,   { .i64 = -1 }, -1, 51, VE },
+    { "qmax",         "Specifies the maximum QP used for rate control",
+                                                            OFFSET(qmax),         AV_OPT_TYPE_INT,   { .i64 = -1 }, -1, 51, VE },
     { "weighted_pred","Set 1 to enable weighted prediction",
                                                             OFFSET(weighted_pred),AV_OPT_TYPE_INT,   { .i64 = 0 }, 0, 1, VE },
 #ifdef NVENC_HAVE_HEVC_BFRAME_REF_MODE



More information about the ffmpeg-cvslog mailing list