History

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

Timo Rothenpieler committed 1 year ago

Commit 89b37b41 parent a583f7e5 files · +44 −19Browse files

Unified · Split

5 files
  1. libavcodec/nvenc.c +30 −19
  2. libavcodec/nvenc.h +2 −0
  3. libavcodec/nvenc_av1.c +4 −0
  4. libavcodec/nvenc_h264.c +4 −0
  5. libavcodec/nvenc_hevc.c +4 −0
libavcodec/nvenc.c+30−19
@@ -884,8 +884,8 @@ static av_cold void set_constqp(AVCodecContext *avctx) · 883 unmodified lines
884884 rc->constQP.qpIntra = av_clip(ctx->cqp * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, qmax);
885885 }
886886
887− avctx->qmin = -1;
888− avctx->qmax = -1;
887+ avctx->qmin = ctx->qmin = -1;
888+ avctx->qmax = ctx->qmax = -1;
889889 }
890890
891891 static av_cold void set_vbr(AVCodecContext *avctx)
@@ -899,27 +899,37 @@ static av_cold void set_vbr(AVCodecContext *avctx) · 7 unmodified lines
899899 int qmax = 51;
900900 #endif
901901
902− if (avctx->qmin >= 0 && avctx->qmax >= 0) {
902+ if (avctx->qmin >= 0 || avctx->qmax >= 0)
903+ av_log(avctx, AV_LOG_WARNING, "Passing qmin/qmax via global AVCodecContext options. Use encoder options instead.\n");
904+
905+ if (avctx->qmin >= 0 && ctx->qmin < 0)
906+ ctx->qmin = avctx->qmin;
907+ if (avctx->qmax >= 0 && ctx->qmax < 0)
908+ ctx->qmax = avctx->qmax;
909+ avctx->qmin = ctx->qmin;
910+ avctx->qmax = ctx->qmax;
911+
912+ if (ctx->qmin >= 0 && ctx->qmax >= 0) {
903913 rc->enableMinQP = 1;
904914 rc->enableMaxQP = 1;
905915
906− rc->minQP.qpInterB = avctx->qmin;
907− rc->minQP.qpInterP = avctx->qmin;
908− rc->minQP.qpIntra = avctx->qmin;
916+ rc->minQP.qpInterB = ctx->qmin;
917+ rc->minQP.qpInterP = ctx->qmin;
918+ rc->minQP.qpIntra = ctx->qmin;
909919
910− rc->maxQP.qpInterB = avctx->qmax;
911− rc->maxQP.qpInterP = avctx->qmax;
912− rc->maxQP.qpIntra = avctx->qmax;
920+ rc->maxQP.qpInterB = ctx->qmax;
921+ rc->maxQP.qpInterP = ctx->qmax;
922+ rc->maxQP.qpIntra = ctx->qmax;
913923
914− qp_inter_p = (avctx->qmax + 3 * avctx->qmin) / 4; // biased towards Qmin
915− } else if (avctx->qmin >= 0) {
924+ qp_inter_p = (ctx->qmax + 3 * ctx->qmin) / 4; // biased towards Qmin
925+ } else if (ctx->qmin >= 0) {
916926 rc->enableMinQP = 1;
917927
918− rc->minQP.qpInterB = avctx->qmin;
919− rc->minQP.qpInterP = avctx->qmin;
920− rc->minQP.qpIntra = avctx->qmin;
928+ rc->minQP.qpInterB = ctx->qmin;
929+ rc->minQP.qpInterP = ctx->qmin;
930+ rc->minQP.qpIntra = ctx->qmin;
921931
922− qp_inter_p = avctx->qmin;
932+ qp_inter_p = ctx->qmin;
923933 } else {
924934 qp_inter_p = 26; // default to 26
925935 }
@@ -965,8 +975,8 @@ static av_cold void set_lossless(AVCodecContext *avctx) · 39 unmodified lines
965975 rc->constQP.qpInterP = 0;
966976 rc->constQP.qpIntra = 0;
967977
968− avctx->qmin = -1;
969− avctx->qmax = -1;
978+ avctx->qmin = ctx->qmin = -1;
979+ avctx->qmax = ctx->qmax = -1;
970980 }
971981
972982 static void nvenc_override_rate_control(AVCodecContext *avctx)
@@ -980,7 +990,7 @@ static void nvenc_override_rate_control(AVCodecContext *avctx) · 7 unmodified lines
980990 return;
981991 #ifndef NVENC_NO_DEPRECATED_RC
982992 case NV_ENC_PARAMS_RC_VBR_MINQP:
983− if (avctx->qmin < 0) {
993+ if (avctx->qmin < 0 && ctx->qmin < 0) {
984994 av_log(avctx, AV_LOG_WARNING,
985995 "The variable bitrate rate-control requires "
986996 "the 'qmin' option set.\n");
@@ -1103,7 +1113,8 @@ static av_cold int nvenc_setup_rate_control(AVCodecContext *avctx) · 116 unmodified lines
11031113 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
11041114 } else if (ctx->twopass) {
11051115 ctx->rc = NV_ENC_PARAMS_RC_VBR_HQ;
1106− } else if (avctx->qmin >= 0 && avctx->qmax >= 0) {
1116+ } else if ((avctx->qmin >= 0 && avctx->qmax >= 0) ||
1117+ (ctx->qmin >= 0 && ctx->qmax >= 0)) {
11071118 ctx->rc = NV_ENC_PARAMS_RC_VBR_MINQP;
11081119 }
11091120 }
libavcodec/nvenc.h+2−0
@@ -271,6 +271,8 @@ typedef struct NvencContext · 270 unmodified lines
271271 float quality;
272272 int aud;
273273 int bluray_compat;
274+ int qmin;
275+ int qmax;
274276 int init_qp_p;
275277 int init_qp_b;
276278 int init_qp_i;
libavcodec/nvenc_av1.c+4−0
@@ -119,6 +119,10 @@ static const AVOption options[] = { · 118 unmodified lines
119119 OFFSET(qp_cb_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, -12, 12, VE },
120120 { "qp_cr_offset", "Quantization parameter offset for cr channel",
121121 OFFSET(qp_cr_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, -12, 12, VE },
122+ { "qmin", "Specifies the minimum QP used for rate control",
123+ OFFSET(qmin), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 255, VE },
124+ { "qmax", "Specifies the maximum QP used for rate control",
125+ OFFSET(qmax), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 255, VE },
122126 { "no-scenecut", "When lookahead is enabled, set this to 1 to disable adaptive I-frame insertion at scene cuts",
123127 OFFSET(no_scenecut), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
124128 { "forced-idr", "If forcing keyframes, force them as IDR frames.",
libavcodec/nvenc_h264.c+4−0
@@ -174,6 +174,10 @@ static const AVOption options[] = { · 173 unmodified lines
174174 OFFSET(qp_cb_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, -12, 12, VE },
175175 { "qp_cr_offset", "Quantization parameter offset for cr channel",
176176 OFFSET(qp_cr_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, -12, 12, VE },
177+ { "qmin", "Specifies the minimum QP used for rate control",
178+ OFFSET(qmin), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 51, VE },
179+ { "qmax", "Specifies the maximum QP used for rate control",
180+ OFFSET(qmax), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 51, VE },
177181 { "weighted_pred","Set 1 to enable weighted prediction",
178182 OFFSET(weighted_pred),AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
179183 { "coder", "Coder type", OFFSET(coder), AV_OPT_TYPE_INT, { .i64 = -1 },-1, 2, VE, .unit = "coder" },
libavcodec/nvenc_hevc.c+4−0
@@ -158,6 +158,10 @@ static const AVOption options[] = { · 157 unmodified lines
158158 OFFSET(qp_cb_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, -12, 12, VE },
159159 { "qp_cr_offset", "Quantization parameter offset for cr channel",
160160 OFFSET(qp_cr_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, -12, 12, VE },
161+ { "qmin", "Specifies the minimum QP used for rate control",
162+ OFFSET(qmin), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 51, VE },
163+ { "qmax", "Specifies the maximum QP used for rate control",
164+ OFFSET(qmax), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 51, VE },
161165 { "weighted_pred","Set 1 to enable weighted prediction",
162166 OFFSET(weighted_pred),AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
163167 #ifdef NVENC_HAVE_HEVC_BFRAME_REF_MODE

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t