From ad21f2649200cf88e45b645162a367d8a6907a1f Mon Sep 17 00:00:00 2001 From: "He, Fan F" Date: Mon, 21 Mar 2022 08:33:14 +0000 Subject: [PATCH] libavcodec/qsvenc: enable LowDelayBRC and MaxFrameSizeI/MaxFrameSizeP for more accurate bitrate control Feature introduction of LowDelayBRC, MaxFrameSizeI and MaxFrameSizeP could be found here: https://github.com/Intel-Media-SDK/MediaSDK/blob/master/doc/mediasdk-man.md Signed-off-by: Dmitry Ermilov Signed-off-by: Fan F He --- doc/encoders.texi | 26 ++++++++++++++++++++++++++ libavcodec/qsvenc.c | 17 +++++++++++++++++ libavcodec/qsvenc.h | 10 ++++++++-- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/doc/encoders.texi b/doc/encoders.texi index a304f865a6..806cc430d4 100644 --- a/doc/encoders.texi +++ b/doc/encoders.texi @@ -3266,6 +3266,14 @@ Enable rate distortion optimization. @item @var{max_frame_size} Maximum encoded frame size in bytes. +@item @var{max_frame_size_i} +Maximum encoded frame size for I frames in bytes. If this value is set as larger +than zero, then for I frames the value set by max_frame_size is ignored. + +@item @var{max_frame_size_p} +Maximum encoded frame size for P frames in bytes. If this value is set as larger +than zero, then for P frames the value set by max_frame_size is ignored. + @item @var{max_slice_size} Maximum encoded slice size in bytes. @@ -3282,6 +3290,11 @@ Setting this flag enables macroblock level bitrate control that generally improves subjective visual quality. Enabling this flag may have negative impact on performance and objective visual quality metric. +@item @var{low_delay_brc} +Setting this flag turns on or off LowDelayBRC feautre in qsv plugin, which provides +more accurate bitrate control to minimize the variance of bitstream size frame +by frame. Value: -1-default 0-off 1-on + @item @var{adaptive_i} This flag controls insertion of I frames by the QSV encoder. Turn ON this flag to allow changing of frame type from P and B to I. @@ -3394,6 +3407,14 @@ Enable rate distortion optimization. @item @var{max_frame_size} Maximum encoded frame size in bytes. +@item @var{max_frame_size_i} +Maximum encoded frame size for I frames in bytes. If this value is set as larger +than zero, then for I frames the value set by max_frame_size is ignored. + +@item @var{max_frame_size_p} +Maximum encoded frame size for P frames in bytes. If this value is set as larger +than zero, then for P frames the value set by max_frame_size is ignored. + @item @var{max_slice_size} Maximum encoded slice size in bytes. @@ -3402,6 +3423,11 @@ Setting this flag enables macroblock level bitrate control that generally improves subjective visual quality. Enabling this flag may have negative impact on performance and objective visual quality metric. +@item @var{low_delay_brc} +Setting this flag turns on or off LowDelayBRC feautre in qsv plugin, which provides +more accurate bitrate control to minimize the variance of bitstream size frame +by frame. Value: -1-default 0-off 1-on + @item @var{p_strategy} Enable P-pyramid: 0-default 1-simple 2-pyramid(bf need to be set to 0). diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c index 55ce3d2499..d7441ac447 100644 --- a/libavcodec/qsvenc.c +++ b/libavcodec/qsvenc.c @@ -376,6 +376,13 @@ static void dump_video_param(AVCodecContext *avctx, QSVEncContext *q, #if QSV_VERSION_ATLEAST(1, 16) av_log(avctx, AV_LOG_VERBOSE, "IntRefCycleDist: %"PRId16"\n", co3->IntRefCycleDist); #endif +#if QSV_VERSION_ATLEAST(1, 23) + av_log(avctx, AV_LOG_VERBOSE, "LowDelayBRC: %s\n", print_threestate(co3->LowDelayBRC)); +#endif +#if QSV_VERSION_ATLEAST(1, 19) + av_log(avctx, AV_LOG_VERBOSE, "MaxFrameSizeI: %d; ", co3->MaxFrameSizeI); + av_log(avctx, AV_LOG_VERBOSE, "MaxFrameSizeP: %d\n", co3->MaxFrameSizeP); +#endif } static void dump_video_vp9_param(AVCodecContext *avctx, QSVEncContext *q, @@ -990,6 +997,16 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q) #if QSV_VERSION_ATLEAST(1, 16) if (q->int_ref_cycle_dist >= 0) q->extco3.IntRefCycleDist = q->int_ref_cycle_dist; +#endif +#if QSV_VERSION_ATLEAST(1, 23) + if (q->low_delay_brc >= 0) + q->extco3.LowDelayBRC = q->low_delay_brc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF; +#endif +#if QSV_VERSION_ATLEAST(1, 19) + if (q->max_frame_size_p >= 0) + q->extco3.MaxFrameSizeI = q->max_frame_size_i; + if (q->max_frame_size_p >= 0) + q->extco3.MaxFrameSizeP = q->max_frame_size_p; #endif } diff --git a/libavcodec/qsvenc.h b/libavcodec/qsvenc.h index 2bda858427..cb84723dfa 100644 --- a/libavcodec/qsvenc.h +++ b/libavcodec/qsvenc.h @@ -90,8 +90,10 @@ { "slower", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_2 }, INT_MIN, INT_MAX, VE, "preset" }, \ { "veryslow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_BEST_QUALITY }, INT_MIN, INT_MAX, VE, "preset" }, \ { "rdo", "Enable rate distortion optimization", OFFSET(qsv.rdo), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE }, \ -{ "max_frame_size", "Maximum encoded frame size in bytes", OFFSET(qsv.max_frame_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, UINT16_MAX, VE }, \ -{ "max_slice_size", "Maximum encoded slice size in bytes", OFFSET(qsv.max_slice_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, UINT16_MAX, VE }, \ +{ "max_frame_size", "Maximum encoded frame size in bytes", OFFSET(qsv.max_frame_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE }, \ +{ "max_frame_size_i", "Maximum encoded I frame size in bytes",OFFSET(qsv.max_frame_size_i), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE }, \ +{ "max_frame_size_p", "Maximum encoded P frame size in bytes",OFFSET(qsv.max_frame_size_p), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE }, \ +{ "max_slice_size", "Maximum encoded slice size in bytes", OFFSET(qsv.max_slice_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE }, \ { "bitrate_limit", "Toggle bitrate limitations", OFFSET(qsv.bitrate_limit), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE }, \ { "mbbrc", "MB level bitrate control", OFFSET(qsv.mbbrc), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE }, \ { "extbrc", "Extended bitrate control", OFFSET(qsv.extbrc), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE }, \ @@ -102,6 +104,7 @@ { "forced_idr", "Forcing I frames as IDR frames", OFFSET(qsv.forced_idr), AV_OPT_TYPE_BOOL,{ .i64 = 0 }, 0, 1, VE }, \ { "low_power", "enable low power mode(experimental: many limitations by mfx version, BRC modes, etc.)", OFFSET(qsv.low_power), AV_OPT_TYPE_BOOL, { .i64 = -1}, -1, 1, VE},\ { "dblk_idc", "This option disable deblocking. It has value in range 0~2.", OFFSET(qsv.dblk_idc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, VE}, \ +{ "low_delay_brc", "Allow to strictly obey avg frame size", OFFSET(qsv.low_delay_brc), AV_OPT_TYPE_BOOL,{ .i64 = -1 }, -1, 1, VE }, \ extern const AVCodecHWConfigInternal *const ff_qsv_enc_hw_configs[]; @@ -173,6 +176,8 @@ typedef struct QSVEncContext { int vcm; int rdo; int max_frame_size; + int max_frame_size_i; + int max_frame_size_p; int max_slice_size; int dblk_idc; @@ -212,6 +217,7 @@ typedef struct QSVEncContext { char *load_plugins; SetEncodeCtrlCB *set_encode_ctrl_cb; int forced_idr; + int low_delay_brc; } QSVEncContext; int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q);