[FFmpeg-devel] [PATCH 8/9] avcodec/nvenc: use encoder level options for qmin/qmax
Zhao Zhili
quinkblack at foxmail.com
Fri Jan 31 08:16:06 EET 2025
> On Jan 31, 2025, at 03:40, Timo Rothenpieler <timo at rothenpieler.org> wrote:
>
> 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.
It ’s simpler to update qmin upper threshold than add qmin/qmax for each encoder.
And there is no mismatch issue between global options and local options.
>
> Fixes #11365
> ---
> 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 a528f1ce93..a5c507150c 100644
> --- a/libavcodec/nvenc.c
> +++ b/libavcodec/nvenc.c
> @@ -893,8 +893,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)
> @@ -908,27 +908,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
> }
> @@ -974,8 +984,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)
> @@ -989,7 +999,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");
> @@ -1112,7 +1122,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 77aef3f188..f84adbdd55 100644
> --- a/libavcodec/nvenc.h
> +++ b/libavcodec/nvenc.h
> @@ -273,6 +273,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 f821eaee50..f1b7062902 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
> --
> 2.45.2
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request at ffmpeg.org with subject "unsubscribe".
More information about the ffmpeg-devel
mailing list