PR #26325 opened by dongnguyenminhanh URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26325 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26325.patch
Adds a preset option to the 3 D3D12 encoders, mapping to the Direct3D 12 QualityVsSpeed rate control setting. This lets callers trade encode speed against quality independently of the existing bitrate and QP options. QualityVsSpeed is a vendor neutral D3D12 field rather than a fixed set of named tiers. The number of levels and what each level means are defined by the driver, so the option is a plain integer rather than named constants, following the same approach VAAPI uses for its quality_level. The value is validated at runtime against the driver reported MaxQualityVsSpeed and clamped with a warning if it exceeds the limit. Defaults to -1, meaning unset, so QualityVsSpeed and the associated rate control flags are only touched if the user passes the option. Existing behaviour is unchanged otherwise. Gated behind a configure probe since the required SDK structures and flags are not present in older Windows SDK versions. Tested on AMD RX 7800 XT with Windows SDK 10.0.26100.0. Builds clean with no new warnings, and the option appears identically on h264_d3d12va, hevc_d3d12va and av1_d3d12va. >From ec7d37c564e992c0cc167814ee59d48d03df4e6d Mon Sep 17 00:00:00 2001 From: Dong Nguyen <[email protected]> Date: Tue, 24 Nov 2026 12:22:10 -0500 Subject: [PATCH] avcodec/d3d12va_encode: add preset option for QualityVsSpeed rate control Add a "preset" option to h264_d3d12va, hevc_d3d12va and av1_d3d12va that maps to the Direct3D 12 QualityVsSpeed rate control setting. This lets a caller trade encode speed against quality on hardware that exposes QualityVsSpeed, independent of the existing bitrate and QP based rate control options. QualityVsSpeed is a vendor neutral Direct3D 12 field, not a fixed set of named tiers. The number of levels and what each level actually does are defined by the GPU vendor and driver, so the option is a plain integer rather than named presets like nvenc or amf use for their own, vendor specific preset enums. Lower values favor speed and higher values favor quality, per the Direct3D 12 specification, but the exact meaning and the maximum value are left to the driver. The option defaults to -1, meaning unset. In that state the code never touches QualityVsSpeed or the ENABLE_QUALITY_VS_SPEED rate control flag, so existing behavior for all three encoders is unchanged unless a caller explicitly sets the option. When set, the value is validated at runtime against the driver reported D3D12_FEATURE_DATA_VIDEO_ENCODER_SUPPORT1.MaxQualityVsSpeed, since the valid range is vendor and driver defined and cannot be known at compile time. Out of range values are clamped to the reported maximum with a warning rather than failing the encode, matching the existing vaapi_encode quality level handling. If the driver does not report QualityVsSpeed support for the current configuration, the option is ignored with a single warning and rate control falls back to its previous behavior. QualityVsSpeed and the D3D12_FEATURE_DATA_VIDEO_ENCODER_SUPPORT1 query it depends on are only available in newer Direct3D 12 SDK headers, so support is gated behind a new configure probe, d3d12_quality_vs_speed. On older SDKs the option is still accepted but has no effect, and a warning is logged if a caller sets it. Signed-off-by: Dong Nguyen <[email protected]> --- configure | 2 + libavcodec/d3d12va_encode.c | 115 +++++++++++++++++++++++++++++++ libavcodec/d3d12va_encode.h | 36 +++++++++- libavcodec/d3d12va_encode_av1.c | 14 ++++ libavcodec/d3d12va_encode_h264.c | 46 +++++++++++++ libavcodec/d3d12va_encode_hevc.c | 46 +++++++++++++ 6 files changed, 258 insertions(+), 1 deletion(-) diff --git a/configure b/configure index 69793448886..0a0939287d8 100755 --- a/configure +++ b/configure @@ -2707,6 +2707,7 @@ CONFIG_EXTRA=" celp_math d3d12_intra_refresh d3d12_motion_estimator + d3d12_quality_vs_speed d3d12_video_process_reference_info d3d12va_encode d3d12va_me_precision_eighth_pixel @@ -7078,6 +7079,7 @@ test_code cc "windows.h d3d12video.h" "D3D12_FEATURE_DATA_VIDEO_ENCODER_RESOURCE test_code cc "windows.h d3d12video.h" "D3D12_VIDEO_ENCODER_CODEC c = D3D12_VIDEO_ENCODER_CODEC_AV1; (void)c;" && enable d3d12va_av1_headers test_code cc "windows.h d3d12video.h" "D3D12_FEATURE_DATA_VIDEO_ENCODER_INTRA_REFRESH_MODE check = { 0 };" && enable d3d12_intra_refresh test_code cc "windows.h d3d12video.h" "D3D12_VIDEO_ENCODER_MOTION_ESTIMATION_PRECISION_MODE m = D3D12_VIDEO_ENCODER_MOTION_ESTIMATION_PRECISION_MODE_EIGHTH_PIXEL; (void)m;" && enable d3d12va_me_precision_eighth_pixel +test_code cc "windows.h d3d12video.h" "D3D12_FEATURE_DATA_VIDEO_ENCODER_SUPPORT1 check = { 0 }; (void)check.MaxQualityVsSpeed; D3D12_VIDEO_ENCODER_RATE_CONTROL_CQP1 cqp1 = { 0 }; (void)cqp1.QualityVsSpeed; D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAGS rcf = D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_EXTENSION1_SUPPORT | D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_QUALITY_VS_SPEED; (void)rcf; D3D12_VIDEO_ENCODER_SUPPORT_FLAGS sf = D3D12_VIDEO_ENCODER_SUPPORT_FLAG_RATE_CONTROL_EXTENSION1_SUPPORT | D3D12_VIDEO_ENCODER_SUPPORT_FLAG_RATE_CONTROL_QUALITY_VS_SPEED_AVAILABLE; (void)sf;" && enable d3d12_quality_vs_speed test_code cc "windows.h d3d12video.h" "D3D12_FEATURE_DATA_VIDEO_MOTION_ESTIMATOR check = { 0 };" && enable d3d12_motion_estimator test_code cc "windows.h d3d12video.h" "D3D12_FEATURE_DATA_VIDEO_PROCESS_REFERENCE_INFO check = { 0 };" && enable d3d12_video_process_reference_info check_type "windows.h" "DPI_AWARENESS_CONTEXT" -D_WIN32_WINNT=0x0A00 diff --git a/libavcodec/d3d12va_encode.c b/libavcodec/d3d12va_encode.c index 19a6901c357..d963333bf4d 100644 --- a/libavcodec/d3d12va_encode.c +++ b/libavcodec/d3d12va_encode.c @@ -779,6 +779,121 @@ static int d3d12va_encode_free_rc_params(AVCodecContext *avctx) return 0; } +#if CONFIG_D3D12_QUALITY_VS_SPEED +int ff_d3d12va_encode_apply_quality_vs_speed(AVCodecContext *avctx, + D3D12_VIDEO_ENCODER_SUPPORT_FLAGS support_flags, + UINT max_quality_vs_speed) +{ + D3D12VAEncodeContext *ctx = avctx->priv_data; + UINT quality_vs_speed; + void *old_ptr; + + if (ctx->quality_vs_speed < 0) + return 0; + + if (!(support_flags & D3D12_VIDEO_ENCODER_SUPPORT_FLAG_RATE_CONTROL_EXTENSION1_SUPPORT) || + !(support_flags & D3D12_VIDEO_ENCODER_SUPPORT_FLAG_RATE_CONTROL_QUALITY_VS_SPEED_AVAILABLE)) { + av_log(avctx, AV_LOG_WARNING, "Driver does not support the \"preset\" option " + "(QualityVsSpeed) for the current configuration; ignoring.\n"); + return 0; + } + + quality_vs_speed = ctx->quality_vs_speed; + if (quality_vs_speed > max_quality_vs_speed) { + av_log(avctx, AV_LOG_WARNING, "Invalid preset level: valid range is 0-%u, using %u.\n", + max_quality_vs_speed, max_quality_vs_speed); + quality_vs_speed = max_quality_vs_speed; + } + + // All union members alias the same storage, so any member name reads the + // currently-active pointer regardless of which mode allocated it - the + // same assumption d3d12va_encode_free_rc_params() already relies on to + // free whichever variant is active via the base (non-"1") member name. + old_ptr = (void *)ctx->rc.ConfigParams.pConfiguration_CQP; + + switch (ctx->rc.Mode) { + case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CQP: { + D3D12_VIDEO_ENCODER_RATE_CONTROL_CQP1 *cqp_ctl; + cqp_ctl = av_mallocz(sizeof(*cqp_ctl)); + if (!cqp_ctl) + return AVERROR(ENOMEM); + memcpy(cqp_ctl, old_ptr, sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_CQP)); + cqp_ctl->QualityVsSpeed = quality_vs_speed; + ctx->rc.ConfigParams.pConfiguration_CQP1 = cqp_ctl; + ctx->rc.ConfigParams.DataSize = sizeof(*cqp_ctl); + break; + } + case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CBR: { + D3D12_VIDEO_ENCODER_RATE_CONTROL_CBR1 *cbr_ctl; + cbr_ctl = av_mallocz(sizeof(*cbr_ctl)); + if (!cbr_ctl) + return AVERROR(ENOMEM); + memcpy(cbr_ctl, old_ptr, sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_CBR)); + cbr_ctl->QualityVsSpeed = quality_vs_speed; + ctx->rc.ConfigParams.pConfiguration_CBR1 = cbr_ctl; + ctx->rc.ConfigParams.DataSize = sizeof(*cbr_ctl); + break; + } + case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_VBR: { + D3D12_VIDEO_ENCODER_RATE_CONTROL_VBR1 *vbr_ctl; + vbr_ctl = av_mallocz(sizeof(*vbr_ctl)); + if (!vbr_ctl) + return AVERROR(ENOMEM); + memcpy(vbr_ctl, old_ptr, sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_VBR)); + vbr_ctl->QualityVsSpeed = quality_vs_speed; + ctx->rc.ConfigParams.pConfiguration_VBR1 = vbr_ctl; + ctx->rc.ConfigParams.DataSize = sizeof(*vbr_ctl); + break; + } + case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_QVBR: { + D3D12_VIDEO_ENCODER_RATE_CONTROL_QVBR1 *qvbr_ctl; + // QVBR1 inserts VBVCapacity/InitialVBVFullness ahead of QualityVsSpeed, + // unlike CQP1/CBR1/VBR1 where QualityVsSpeed is simply appended. Those + // two fields are only meaningful when ENABLE_VBV_SIZES is set, which + // d3d12va_encode_init_rate_control() never does for QVBR - so leaving + // them zeroed by av_mallocz is safe *today*. Guard that assumption so + // a future QVBR+VBV change doesn't silently leave them unset here. + av_assert0(!(ctx->rc.Flags & D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_VBV_SIZES)); + qvbr_ctl = av_mallocz(sizeof(*qvbr_ctl)); + if (!qvbr_ctl) + return AVERROR(ENOMEM); + memcpy(qvbr_ctl, old_ptr, sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_QVBR)); + qvbr_ctl->QualityVsSpeed = quality_vs_speed; + ctx->rc.ConfigParams.pConfiguration_QVBR1 = qvbr_ctl; + ctx->rc.ConfigParams.DataSize = sizeof(*qvbr_ctl); + break; + } + default: + av_log(avctx, AV_LOG_WARNING, "\"preset\" option is not supported with the " + "current rate control mode; ignoring.\n"); + return 0; + } + + ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_EXTENSION1_SUPPORT | + D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_QUALITY_VS_SPEED; + + av_free(old_ptr); + + av_log(avctx, AV_LOG_VERBOSE, "QualityVsSpeed: %u.\n", quality_vs_speed); + + return 0; +} +#else +int ff_d3d12va_encode_apply_quality_vs_speed(AVCodecContext *avctx, + D3D12_VIDEO_ENCODER_SUPPORT_FLAGS support_flags, + UINT max_quality_vs_speed) +{ + D3D12VAEncodeContext *ctx = avctx->priv_data; + + if (ctx->quality_vs_speed >= 0) + av_log(avctx, AV_LOG_WARNING, "The \"preset\" option requires a newer Direct3D 12 " + "SDK (QualityVsSpeed/D3D12_FEATURE_VIDEO_ENCODER_SUPPORT1) than this build " + "was compiled against; ignoring.\n"); + + return 0; +} +#endif + static int d3d12va_encode_init(AVCodecContext *avctx, FFHWBaseEncodePicture *pic) { D3D12VAEncodeContext *ctx = avctx->priv_data; diff --git a/libavcodec/d3d12va_encode.h b/libavcodec/d3d12va_encode.h index bfc5dd55786..90324916859 100644 --- a/libavcodec/d3d12va_encode.h +++ b/libavcodec/d3d12va_encode.h @@ -183,6 +183,14 @@ typedef struct D3D12VAEncodeContext { */ int rc_quality; + /** + * Explicitly-set quality/speed tradeoff level, for use with the + * "preset" option. Raw driver-defined QualityVsSpeed index; meaning, + * direction and valid range are GPU vendor- and driver-defined. + * -1 means unset: QualityVsSpeed is left untouched. + */ + int quality_vs_speed; + /** * Chosen encoding profile details. */ @@ -383,6 +391,24 @@ int ff_d3d12va_encode_close(AVCodecContext *avctx); void ff_d3d12va_encode_check_encoder_feature_flags(void *log_ctx, D3D12_VIDEO_ENCODER_VALIDATION_FLAGS flags); +/** + * Apply the user-requested "preset" (QualityVsSpeed) value to the + * already-built rate control configuration, if one was requested. + * + * Must be called after ctx->rc has been fully populated (i.e. after + * d3d12va_encode_init_rate_control and, for CQP mode, after the codec's + * configure() callback), and after the caller has queried + * D3D12_FEATURE_VIDEO_ENCODER_SUPPORT1 for the current configuration. + * + * No-op if the user never set the "preset" option. Clamps with a warning + * rather than failing if the requested value exceeds max_quality_vs_speed, + * and warns and does nothing if support_flags indicates the driver does + * not support QualityVsSpeed for the current configuration. + */ +int ff_d3d12va_encode_apply_quality_vs_speed(AVCodecContext *avctx, + D3D12_VIDEO_ENCODER_SUPPORT_FLAGS support_flags, + UINT max_quality_vs_speed); + #define D3D12VA_ENCODE_INTRA_REFRESH_MODE(name, mode, desc) \ { #name, desc, 0, AV_OPT_TYPE_CONST, { .i64 = D3D12_VIDEO_ENCODER_INTRA_REFRESH_MODE_ ## mode }, \ 0, 0, FLAGS, .unit = "intra_refresh_mode" } @@ -434,7 +460,15 @@ void ff_d3d12va_encode_check_encoder_feature_flags(void *log_ctx, D3D12VA_ENCODE_ME_PRECISION_MODE(full_pixel, FULL_PIXEL, Full), \ D3D12VA_ENCODE_ME_PRECISION_MODE(half_pixel, HALF_PIXEL, Half), \ D3D12VA_ENCODE_ME_PRECISION_MODE(quarter_pixel, QUARTER_PIXEL, Quarter) \ - FFPP_D3D12VA_ME_PRECISION_EIGHTH_PIXEL + FFPP_D3D12VA_ME_PRECISION_EIGHTH_PIXEL, \ + { "preset", \ + "Encoder quality/speed tradeoff level (maps to the Direct3D 12 " \ + "QualityVsSpeed rate control setting: lower values favor speed, " \ + "higher values favor quality; the exact meaning, number of steps " \ + "and maximum value are defined by the GPU vendor and driver, and " \ + "are validated against the driver-reported limit at runtime)", \ + OFFSET(common.quality_vs_speed), AV_OPT_TYPE_INT, \ + { .i64 = -1 }, -1, INT_MAX, FLAGS } #define D3D12VA_ENCODE_RC_MODE(name, desc) \ { #name, desc, 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_ ## name }, \ diff --git a/libavcodec/d3d12va_encode_av1.c b/libavcodec/d3d12va_encode_av1.c index 0b89c02bf21..ede15f686cb 100644 --- a/libavcodec/d3d12va_encode_av1.c +++ b/libavcodec/d3d12va_encode_av1.c @@ -814,6 +814,20 @@ static int d3d12va_encode_av1_init_sequence_params(AVCodecContext *avctx) "precision mode limits.\n"); } + { + D3D12_VIDEO_ENCODER_SUPPORT_FLAGS qvs_flags = 0; + UINT qvs_max = 0; + +#if CONFIG_D3D12_QUALITY_VS_SPEED + qvs_flags = support.SupportFlags; + qvs_max = support.MaxQualityVsSpeed; +#endif + + err = ff_d3d12va_encode_apply_quality_vs_speed(avctx, qvs_flags, qvs_max); + if (err < 0) + return err; + } + memset(seqheader_obu, 0, sizeof(*seqheader_obu)); seq->seq_profile = profile; seq->seq_level_idx[0] = level.Level; diff --git a/libavcodec/d3d12va_encode_h264.c b/libavcodec/d3d12va_encode_h264.c index 55ed2490647..69b21c97ed6 100644 --- a/libavcodec/d3d12va_encode_h264.c +++ b/libavcodec/d3d12va_encode_h264.c @@ -23,6 +23,7 @@ #include "libavutil/pixdesc.h" #include "libavutil/hwcontext_d3d12va_internal.h" +#include "config_components.h" #include "avcodec.h" #include "cbs.h" #include "cbs_h264.h" @@ -237,6 +238,51 @@ static int d3d12va_encode_h264_init_sequence_params(AVCodecContext *avctx) "precision mode limits.\n"); } + { + D3D12_VIDEO_ENCODER_SUPPORT_FLAGS qvs_flags = 0; + UINT qvs_max = 0; + +#if CONFIG_D3D12_QUALITY_VS_SPEED + if (ctx->quality_vs_speed >= 0) { + HRESULT qvs_hr; + D3D12_FEATURE_DATA_VIDEO_ENCODER_SUPPORT1 qvs_support = { + .NodeIndex = 0, + .Codec = D3D12_VIDEO_ENCODER_CODEC_H264, + .InputFormat = hwctx->format, + .RateControl = ctx->rc, + .IntraRefresh = ctx->intra_refresh.Mode, + .SubregionFrameEncoding = D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_FULL_FRAME, + .ResolutionsListCount = 1, + .pResolutionList = &ctx->resolution, + .CodecGopSequence = ctx->gop, + .MaxReferenceFramesInDPB = MAX_DPB_SIZE - 1, + .CodecConfiguration = ctx->codec_conf, + .SuggestedProfile.DataSize = sizeof(D3D12_VIDEO_ENCODER_PROFILE_H264), + .SuggestedProfile.pH264Profile = &profile, + .SuggestedLevel.DataSize = sizeof(D3D12_VIDEO_ENCODER_LEVELS_H264), + .SuggestedLevel.pH264LevelSetting = &level, + .pResolutionDependentSupport = &ctx->res_limits, + }; + + qvs_hr = ID3D12VideoDevice3_CheckFeatureSupport(ctx->video_device3, D3D12_FEATURE_VIDEO_ENCODER_SUPPORT1, + &qvs_support, sizeof(qvs_support)); + if (SUCCEEDED(qvs_hr)) { + qvs_flags = qvs_support.SupportFlags; + qvs_max = qvs_support.MaxQualityVsSpeed; + } else { + // Not user-facing: ff_d3d12va_encode_apply_quality_vs_speed() + // owns the single "preset" warning, logged below once it sees + // qvs_flags left at 0. + av_log(avctx, AV_LOG_DEBUG, "Failed to query QualityVsSpeed capability (%lx).\n", (long)qvs_hr); + } + } +#endif + + err = ff_d3d12va_encode_apply_quality_vs_speed(avctx, qvs_flags, qvs_max); + if (err < 0) + return err; + } + desc = av_pix_fmt_desc_get(base_ctx->input_frames->sw_format); av_assert0(desc); diff --git a/libavcodec/d3d12va_encode_hevc.c b/libavcodec/d3d12va_encode_hevc.c index d2a862ac8bd..c13f86a1199 100644 --- a/libavcodec/d3d12va_encode_hevc.c +++ b/libavcodec/d3d12va_encode_hevc.c @@ -25,6 +25,7 @@ #include "libavutil/pixdesc.h" #include "libavutil/hwcontext_d3d12va_internal.h" +#include "config_components.h" #include "avcodec.h" #include "cbs.h" #include "cbs_h265.h" @@ -308,6 +309,51 @@ static int d3d12va_encode_hevc_init_sequence_params(AVCodecContext *avctx) "precision mode limits.\n"); } + { + D3D12_VIDEO_ENCODER_SUPPORT_FLAGS qvs_flags = 0; + UINT qvs_max = 0; + +#if CONFIG_D3D12_QUALITY_VS_SPEED + if (ctx->quality_vs_speed >= 0) { + HRESULT qvs_hr; + D3D12_FEATURE_DATA_VIDEO_ENCODER_SUPPORT1 qvs_support = { + .NodeIndex = 0, + .Codec = D3D12_VIDEO_ENCODER_CODEC_HEVC, + .InputFormat = hwctx->format, + .RateControl = ctx->rc, + .IntraRefresh = ctx->intra_refresh.Mode, + .SubregionFrameEncoding = D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_FULL_FRAME, + .ResolutionsListCount = 1, + .pResolutionList = &ctx->resolution, + .CodecGopSequence = ctx->gop, + .MaxReferenceFramesInDPB = MAX_DPB_SIZE - 1, + .CodecConfiguration = ctx->codec_conf, + .SuggestedProfile.DataSize = sizeof(D3D12_VIDEO_ENCODER_PROFILE_HEVC), + .SuggestedProfile.pHEVCProfile = &profile, + .SuggestedLevel.DataSize = sizeof(D3D12_VIDEO_ENCODER_LEVEL_TIER_CONSTRAINTS_HEVC), + .SuggestedLevel.pHEVCLevelSetting = &level, + .pResolutionDependentSupport = &ctx->res_limits, + }; + + qvs_hr = ID3D12VideoDevice3_CheckFeatureSupport(ctx->video_device3, D3D12_FEATURE_VIDEO_ENCODER_SUPPORT1, + &qvs_support, sizeof(qvs_support)); + if (SUCCEEDED(qvs_hr)) { + qvs_flags = qvs_support.SupportFlags; + qvs_max = qvs_support.MaxQualityVsSpeed; + } else { + // Not user-facing: ff_d3d12va_encode_apply_quality_vs_speed() + // owns the single "preset" warning, logged below once it sees + // qvs_flags left at 0. + av_log(avctx, AV_LOG_DEBUG, "Failed to query QualityVsSpeed capability (%lx).\n", (long)qvs_hr); + } + } +#endif + + err = ff_d3d12va_encode_apply_quality_vs_speed(avctx, qvs_flags, qvs_max); + if (err < 0) + return err; + } + desc = av_pix_fmt_desc_get(base_ctx->input_frames->sw_format); av_assert0(desc); -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
