From d263fce2b209e86a5a1e8f1b6aa33430ecc2c187 Mon Sep 17 00:00:00 2001 From: Haihao Xiang Date: Thu, 1 Feb 2024 16:35:35 +0800 Subject: [PATCH] lavc/qsvenc: update the selection of bitrate control method The default method is changed to CQP Signed-off-by: Haihao Xiang --- Changelog | 1 + doc/encoders.texi | 7 +++-- libavcodec/qsvenc.c | 61 +++++++++++++++++++++++++++------------ libavcodec/qsvenc_av1.c | 2 +- libavcodec/qsvenc_h264.c | 2 +- libavcodec/qsvenc_hevc.c | 2 +- libavcodec/qsvenc_mpeg2.c | 2 +- libavcodec/qsvenc_vp9.c | 2 +- 8 files changed, 54 insertions(+), 25 deletions(-) diff --git a/Changelog b/Changelog index 610ee61dd6..6e03a49426 100644 --- a/Changelog +++ b/Changelog @@ -27,6 +27,7 @@ version : - a C11-compliant compiler is now required; note that this requirement will be bumped to C17 in the near future, so consider updating your build environment if it lacks C17 support +- Change the default bitrate control method from VBR to CQP for QSV encoders. version 6.1: - libaribcaption decoder diff --git a/doc/encoders.texi b/doc/encoders.texi index 9f477d7c53..5f7864770e 100644 --- a/doc/encoders.texi +++ b/doc/encoders.texi @@ -3338,8 +3338,8 @@ quality range is 1 to 51, with 1 being the best quality. @end itemize @item -Otherwise, a bitrate-based mode is used. For all of those, you should specify at -least the desired average bitrate with the @option{b} option. +Otherwise when the desired average bitrate is specified with the @option{b} +option, a bitrate-based mode is used. @itemize @minus @item @var{LA} - VBR with lookahead, when the @option{look_ahead} option is specified. @@ -3360,6 +3360,9 @@ than the average bitrate. @option{avbr_accuracy} and @option{avbr_convergence} are set to non-zero. This mode is available for H264 and HEVC on Windows. @end itemize + +@item +Otherwise the default ratecontrol method @var{CQP} is used. @end itemize Note that depending on your system, a different mode than the one you specified diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c index c63b72e384..3a8607fca6 100644 --- a/libavcodec/qsvenc.c +++ b/libavcodec/qsvenc.c @@ -597,6 +597,13 @@ static int select_rc_mode(AVCodecContext *avctx, QSVEncContext *q) else if (want_vcm) { rc_mode = MFX_RATECONTROL_VCM; rc_desc = "video conferencing mode (VCM)"; + + if (!avctx->bit_rate) { + av_log(avctx, AV_LOG_ERROR, "Using the %s ratecontrol method without " + "setting bitrate. Please use the b option to set the desired " + "bitrate.\n", rc_desc); + return AVERROR(EINVAL); + } } #endif else if (want_la) { @@ -606,32 +613,50 @@ static int select_rc_mode(AVCodecContext *avctx, QSVEncContext *q) if (avctx->global_quality > 0) { rc_mode = MFX_RATECONTROL_LA_ICQ; rc_desc = "intelligent constant quality with lookahead (LA_ICQ)"; + } else if (!avctx->bit_rate) { + av_log(avctx, AV_LOG_ERROR, "Using the %s ratecontrol method without " + "setting bitrate. Please use the b option to set the desired " + "bitrate.\n", rc_desc); + return AVERROR(EINVAL); } } else if (avctx->global_quality > 0 && !avctx->rc_max_rate) { rc_mode = MFX_RATECONTROL_ICQ; rc_desc = "intelligent constant quality (ICQ)"; } - else if (avctx->rc_max_rate == avctx->bit_rate) { - rc_mode = MFX_RATECONTROL_CBR; - rc_desc = "constant bitrate (CBR)"; - } + else if (avctx->bit_rate) { + if (avctx->rc_max_rate == avctx->bit_rate) { + rc_mode = MFX_RATECONTROL_CBR; + rc_desc = "constant bitrate (CBR)"; + } #if QSV_HAVE_AVBR - else if (!avctx->rc_max_rate && - (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_HEVC) && - q->avbr_accuracy && - q->avbr_convergence) { - rc_mode = MFX_RATECONTROL_AVBR; - rc_desc = "average variable bitrate (AVBR)"; - } + else if (!avctx->rc_max_rate && + (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_HEVC) && + q->avbr_accuracy && + q->avbr_convergence) { + rc_mode = MFX_RATECONTROL_AVBR; + rc_desc = "average variable bitrate (AVBR)"; + } #endif - else if (avctx->global_quality > 0) { - rc_mode = MFX_RATECONTROL_QVBR; - rc_desc = "constant quality with VBR algorithm (QVBR)"; - } - else { - rc_mode = MFX_RATECONTROL_VBR; - rc_desc = "variable bitrate (VBR)"; + else if (avctx->global_quality > 0) { + rc_mode = MFX_RATECONTROL_QVBR; + rc_desc = "constant quality with VBR algorithm (QVBR)"; + } else { + rc_mode = MFX_RATECONTROL_VBR; + rc_desc = "variable bitrate (VBR)"; + } + } else { + rc_mode = MFX_RATECONTROL_CQP; + rc_desc = "constant quantization parameter (CQP)"; + if (avctx->codec_id == AV_CODEC_ID_AV1) + avctx->global_quality = FF_QP2LAMBDA * 128; + else + avctx->global_quality = FF_QP2LAMBDA * 26; + av_log(avctx, AV_LOG_WARNING, "Using the constant quantization " + "parameter (CQP) by default. Please use the global_quality " + "option and other options for a quality-based mode or the b " + "option and other options for a bitrate-based mode if the " + "default is not the desired choice.\n"); } q->param.mfx.RateControlMethod = rc_mode; diff --git a/libavcodec/qsvenc_av1.c b/libavcodec/qsvenc_av1.c index 4b6e2545ca..33727bb07e 100644 --- a/libavcodec/qsvenc_av1.c +++ b/libavcodec/qsvenc_av1.c @@ -129,7 +129,7 @@ static const AVClass class = { }; static const FFCodecDefault qsv_enc_defaults[] = { - { "b", "1M" }, + { "b", "0" }, { "g", "-1" }, { "bf", "-1" }, { "refs", "0" }, diff --git a/libavcodec/qsvenc_h264.c b/libavcodec/qsvenc_h264.c index 9c4b65ceac..53e4e744b2 100644 --- a/libavcodec/qsvenc_h264.c +++ b/libavcodec/qsvenc_h264.c @@ -178,7 +178,7 @@ static const AVClass class = { }; static const FFCodecDefault qsv_enc_defaults[] = { - { "b", "1M" }, + { "b", "0" }, { "refs", "0" }, { "g", "-1" }, { "bf", "-1" }, diff --git a/libavcodec/qsvenc_hevc.c b/libavcodec/qsvenc_hevc.c index 004b34287b..4920b0bca4 100644 --- a/libavcodec/qsvenc_hevc.c +++ b/libavcodec/qsvenc_hevc.c @@ -374,7 +374,7 @@ static const AVClass class = { }; static const FFCodecDefault qsv_enc_defaults[] = { - { "b", "1M" }, + { "b", "0" }, { "refs", "0" }, { "g", "248" }, { "bf", "-1" }, diff --git a/libavcodec/qsvenc_mpeg2.c b/libavcodec/qsvenc_mpeg2.c index 9151c59fc9..fabf461fe3 100644 --- a/libavcodec/qsvenc_mpeg2.c +++ b/libavcodec/qsvenc_mpeg2.c @@ -82,7 +82,7 @@ static const AVClass class = { }; static const FFCodecDefault qsv_enc_defaults[] = { - { "b", "1M" }, + { "b", "0" }, { "refs", "0" }, // same as the x264 default { "g", "250" }, diff --git a/libavcodec/qsvenc_vp9.c b/libavcodec/qsvenc_vp9.c index 070567c73c..a760e4932e 100644 --- a/libavcodec/qsvenc_vp9.c +++ b/libavcodec/qsvenc_vp9.c @@ -93,7 +93,7 @@ static const AVClass class = { }; static const FFCodecDefault qsv_enc_defaults[] = { - { "b", "1M" }, + { "b", "0" }, { "refs", "0" }, { "g", "250" }, { "trellis", "-1" },