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
884 rc->constQP.qpIntra = av_clip(ctx->cqp * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, qmax);884 rc->constQP.qpIntra = av_clip(ctx->cqp * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, qmax);
885 }885 }
886 886
887 avctx->qmin = -1;887 avctx->qmin = ctx->qmin = -1;
888 avctx->qmax = -1;888 avctx->qmax = ctx->qmax = -1;
889}889}
890 890
891static av_cold void set_vbr(AVCodecContext *avctx)891static av_cold void set_vbr(AVCodecContext *avctx)
@@ -899,27 +899,37 @@ static av_cold void set_vbr(AVCodecContext *avctx) · 7 unmodified lines
899 int qmax = 51;899 int qmax = 51;
900#endif900#endif
901 901
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) {
903 rc->enableMinQP = 1;913 rc->enableMinQP = 1;
904 rc->enableMaxQP = 1;914 rc->enableMaxQP = 1;
905 915
906 rc->minQP.qpInterB = avctx->qmin;916 rc->minQP.qpInterB = ctx->qmin;
907 rc->minQP.qpInterP = avctx->qmin;917 rc->minQP.qpInterP = ctx->qmin;
908 rc->minQP.qpIntra = avctx->qmin;918 rc->minQP.qpIntra = ctx->qmin;
909 919
910 rc->maxQP.qpInterB = avctx->qmax;920 rc->maxQP.qpInterB = ctx->qmax;
911 rc->maxQP.qpInterP = avctx->qmax;921 rc->maxQP.qpInterP = ctx->qmax;
912 rc->maxQP.qpIntra = avctx->qmax;922 rc->maxQP.qpIntra = ctx->qmax;
913 923
914 qp_inter_p = (avctx->qmax + 3 * avctx->qmin) / 4; // biased towards Qmin924 qp_inter_p = (ctx->qmax + 3 * ctx->qmin) / 4; // biased towards Qmin
915 } else if (avctx->qmin >= 0) {925 } else if (ctx->qmin >= 0) {
916 rc->enableMinQP = 1;926 rc->enableMinQP = 1;
917 927
918 rc->minQP.qpInterB = avctx->qmin;928 rc->minQP.qpInterB = ctx->qmin;
919 rc->minQP.qpInterP = avctx->qmin;929 rc->minQP.qpInterP = ctx->qmin;
920 rc->minQP.qpIntra = avctx->qmin;930 rc->minQP.qpIntra = ctx->qmin;
921 931
922 qp_inter_p = avctx->qmin;932 qp_inter_p = ctx->qmin;
923 } else {933 } else {
924 qp_inter_p = 26; // default to 26934 qp_inter_p = 26; // default to 26
925 }935 }
@@ -965,8 +975,8 @@ static av_cold void set_lossless(AVCodecContext *avctx) · 39 unmodified lines
965 rc->constQP.qpInterP = 0;975 rc->constQP.qpInterP = 0;
966 rc->constQP.qpIntra = 0;976 rc->constQP.qpIntra = 0;
967 977
968 avctx->qmin = -1;978 avctx->qmin = ctx->qmin = -1;
969 avctx->qmax = -1;979 avctx->qmax = ctx->qmax = -1;
970}980}
971 981
972static void nvenc_override_rate_control(AVCodecContext *avctx)982static void nvenc_override_rate_control(AVCodecContext *avctx)
@@ -980,7 +990,7 @@ static void nvenc_override_rate_control(AVCodecContext *avctx) · 7 unmodified lines
980 return;990 return;
981#ifndef NVENC_NO_DEPRECATED_RC991#ifndef NVENC_NO_DEPRECATED_RC
982 case NV_ENC_PARAMS_RC_VBR_MINQP:992 case NV_ENC_PARAMS_RC_VBR_MINQP:
983 if (avctx->qmin < 0) {993 if (avctx->qmin < 0 && ctx->qmin < 0) {
984 av_log(avctx, AV_LOG_WARNING,994 av_log(avctx, AV_LOG_WARNING,
985 "The variable bitrate rate-control requires "995 "The variable bitrate rate-control requires "
986 "the 'qmin' option set.\n");996 "the 'qmin' option set.\n");
@@ -1103,7 +1113,8 @@ static av_cold int nvenc_setup_rate_control(AVCodecContext *avctx) · 116 unmodified lines
1103 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;1113 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
1104 } else if (ctx->twopass) {1114 } else if (ctx->twopass) {
1105 ctx->rc = NV_ENC_PARAMS_RC_VBR_HQ;1115 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)) {
1107 ctx->rc = NV_ENC_PARAMS_RC_VBR_MINQP;1118 ctx->rc = NV_ENC_PARAMS_RC_VBR_MINQP;
1108 }1119 }
1109 }1120 }
libavcodec/nvenc.h+2−0
@@ -271,6 +271,8 @@ typedef struct NvencContext · 270 unmodified lines
271 float quality;271 float quality;
272 int aud;272 int aud;
273 int bluray_compat;273 int bluray_compat;
274 int qmin;
275 int qmax;
274 int init_qp_p;276 int init_qp_p;
275 int init_qp_b;277 int init_qp_b;
276 int init_qp_i;278 int init_qp_i;
libavcodec/nvenc_av1.c+4−0
@@ -119,6 +119,10 @@ static const AVOption options[] = { · 118 unmodified lines
119 OFFSET(qp_cb_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, -12, 12, VE },119 OFFSET(qp_cb_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, -12, 12, VE },
120 { "qp_cr_offset", "Quantization parameter offset for cr channel",120 { "qp_cr_offset", "Quantization parameter offset for cr channel",
121 OFFSET(qp_cr_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, -12, 12, VE },121 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 },
122 { "no-scenecut", "When lookahead is enabled, set this to 1 to disable adaptive I-frame insertion at scene cuts",126 { "no-scenecut", "When lookahead is enabled, set this to 1 to disable adaptive I-frame insertion at scene cuts",
123 OFFSET(no_scenecut), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },127 OFFSET(no_scenecut), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
124 { "forced-idr", "If forcing keyframes, force them as IDR frames.",128 { "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
174 OFFSET(qp_cb_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, -12, 12, VE },174 OFFSET(qp_cb_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, -12, 12, VE },
175 { "qp_cr_offset", "Quantization parameter offset for cr channel",175 { "qp_cr_offset", "Quantization parameter offset for cr channel",
176 OFFSET(qp_cr_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, -12, 12, VE },176 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 },
177 { "weighted_pred","Set 1 to enable weighted prediction",181 { "weighted_pred","Set 1 to enable weighted prediction",
178 OFFSET(weighted_pred),AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },182 OFFSET(weighted_pred),AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
179 { "coder", "Coder type", OFFSET(coder), AV_OPT_TYPE_INT, { .i64 = -1 },-1, 2, VE, .unit = "coder" },183 { "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
158 OFFSET(qp_cb_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, -12, 12, VE },158 OFFSET(qp_cb_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, -12, 12, VE },
159 { "qp_cr_offset", "Quantization parameter offset for cr channel",159 { "qp_cr_offset", "Quantization parameter offset for cr channel",
160 OFFSET(qp_cr_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, -12, 12, VE },160 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 },
161 { "weighted_pred","Set 1 to enable weighted prediction",165 { "weighted_pred","Set 1 to enable weighted prediction",
162 OFFSET(weighted_pred),AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },166 OFFSET(weighted_pred),AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
163#ifdef NVENC_HAVE_HEVC_BFRAME_REF_MODE167#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