From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id EE0044B882 for ; Thu, 30 Jan 2025 19:43:22 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DB2C268C1AF; Thu, 30 Jan 2025 21:41:54 +0200 (EET) Received: from btbn.de (btbn.de [144.76.60.213]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F346468C121 for ; Thu, 30 Jan 2025 21:41:41 +0200 (EET) Received: from [authenticated] by btbn.de (Postfix) with ESMTPSA id 1C540296C1114; Thu, 30 Jan 2025 20:41:38 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rothenpieler.org; s=mail; t=1738266098; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IX3v1MRDWQ2X6Db8NYqDmvuDd/rx2sa83thJwqbuoi0=; b=Vk9z6WKm0KfAIP+wXkVbm7jB1Z0D8YQV0YF3XzjCkMzrA81Vgs88+unDnvcshCYu2SFyxY AyokBl6rL5AHVH0fdCwP+JmIMJXAnqtgcaixjX8ZdkXtaEk9iokU5weOvTbHTQ+DUyIyrT MYLWj4Q1D736qGlPcokj8JUjJxg5UQHn8zuC+qZCQ2CUi3SlAB+JSVlhBamdPnZWfJMGuf 4mnYlQidTw8TRt89KloDqWyae1mIbP+8aQYzwjD3O989KXhzPVqS4I2lSqZ1SdTdJF5h/E /K/9TLHF0mDa/cwDU04P0VEQEVUbFS3hKNVweUgOfo2XDGpxHjsl7fGr/ztcWA== From: Timo Rothenpieler To: ffmpeg-devel@ffmpeg.org Date: Thu, 30 Jan 2025 20:40:45 +0100 Message-ID: <20250130194124.21836-9-timo@rothenpieler.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250130194124.21836-1-timo@rothenpieler.org> References: <20250130194124.21836-1-timo@rothenpieler.org> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 8/9] avcodec/nvenc: use encoder level options for qmin/qmax X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Timo Rothenpieler Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: 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 --- 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@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".