From a082e61a2efd581715451c5d8bae21d262e22b61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 13 Aug 2026 09:09:04 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(hevc):=20the=20parameter=20s?= =?UTF-8?q?ets=20say=20what=20the=20GPU=20actually=20codes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The hardware seat's pictures were a band of noise over flat green while the frame count decoded clean, on every real 1080p and 4K source and on none of the synthetic ones. Three promises the synthesized parameter sets made that the driver never kept, each of which desyncs CABAC and turns the rest of the picture into coefficient garbage: `max_transform_hierarchy_depth_intra`/`_inter` were the builder's default 0, which says `split_transform_flag` is never coded and a decoder must infer no split. The encoder splits transform trees wherever the residual asks for it, so from the first coding tree unit on its split flags were read as residual. Measured alone: SSIM 0.17 against the source, 371 decoder complaints. `cu_qp_delta_enabled_flag` was off, so `cu_qp_delta_abs` had nowhere to be written -- but the driver's rate controller moves the quantizer between coding tree blocks and codes the delta regardless. Measured alone: SSIM 0.17. And the picture was declared at `MinCbSizeY`, so 1080 lines claimed a 56 line bottom coding tree row where the encoder had coded a full 64. The sets now declare the coding-tree-aligned size and crop back with the conformance window, which is what the working reference encoders on this driver do, and the surface is allocated to match. Flat gradients and a screen capture pass either way -- they split no transforms and give the rate controller nothing to do -- which is exactly how this reached a default-on seat. --- crates/engine-hw/src/lib.rs | 16 +++++++- .../src/encoder/stateless/h265/predictor.rs | 41 ++++++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/crates/engine-hw/src/lib.rs b/crates/engine-hw/src/lib.rs index 8ad361d..52d26b0 100644 --- a/crates/engine-hw/src/lib.rs +++ b/crates/engine-hw/src/lib.rs @@ -727,6 +727,10 @@ fn align16(v: u32) -> u32 { v.div_ceil(16) * 16 } +fn align64(v: u32) -> u32 { + v.div_ceil(64) * 64 +} + /// Fills in slice NAL header bytes the driver left at zero. /// /// Mesa's radeonsi encoder writes `forbidden_zero_bit`, `nal_ref_idc` and @@ -838,9 +842,17 @@ impl EncSession { return None; } let (display, gbm) = open_devices()?; + // A macroblock is 16 wide; an HEVC coding tree block is 64, and that + // seat's parameter sets declare the picture at coding-tree size (see the + // vendored `h265/predictor.rs`), so the surface has to carry every row + // those sets promise. + let align = match codec { + EncCodec::Hevc => align64, + _ => align16, + }; let coded = Resolution { - width: align16(width), - height: align16(height), + width: align(width), + height: align(height), }; let framerate = ((fps_num as f64 / fps_den as f64).round() as i64).clamp(1, 240) as u32; let profile = match codec { diff --git a/vendor/cros-codecs/src/encoder/stateless/h265/predictor.rs b/vendor/cros-codecs/src/encoder/stateless/h265/predictor.rs index 1675da4..f1a9c68 100644 --- a/vendor/cros-codecs/src/encoder/stateless/h265/predictor.rs +++ b/vendor/cros-codecs/src/encoder/stateless/h265/predictor.rs @@ -37,6 +37,25 @@ pub(crate) const MAX_QP: u8 = 51; /// lower than `NumNegativePics` of any short term reference picture set, see H.265 7.4.3.2.1. const MAX_DEC_PIC_BUFFERING_MINUS1: u8 = 1; +/// How deep a transform tree may be split below its coding unit. Zero -- the [`SpsBuilder`] +/// default -- means `split_transform_flag` is never present and a decoder infers no split at +/// all, which is a promise about the *coded* syntax that no backend here can keep: a hardware +/// encoder splits transforms wherever the residual asks for it, so the flags it writes are +/// then read as coefficient data and the picture falls apart from the first coding tree unit +/// on. The maximum the block sizes allow is `CtbLog2SizeY - MinTbLog2SizeY` = 6 - 2, and +/// declaring it constrains nothing: an encoder that splits less simply writes zero flags. +const MAX_TRANSFORM_HIERARCHY_DEPTH: u8 = 4; + +/// The luma coding tree block the [`SpsBuilder`] defaults describe: 1 << (`log2_min_luma_ +/// coding_block_size_minus3` + 3 + `log2_diff_max_min_luma_coding_block_size`), neither of +/// which this predictor overrides. A backend codes whole coding tree blocks, so the picture +/// it hands back is that size in both directions and the conformance window is what crops it +/// back to the resolution somebody asked for. Aligning only to `MinCbSizeY`, as +/// [`SpsBuilder::resolution`] does on its own, leaves a 1080 line picture claiming a 56 line +/// bottom row where the encoder coded 64, and the last coding tree row of every frame is then +/// read as coefficient data. +const CTB_SIZE_Y: u32 = 64; + pub(crate) struct LowDelayH265Delegate { /// Current sequence VPS vps: Option>, @@ -106,14 +125,26 @@ impl LowDelayH265 { short_term_ref_pic_set.delta_poc_s0[0] = -1; short_term_ref_pic_set.used_by_curr_pic_s0[0] = true; + let align = |v: u32| v.div_ceil(CTB_SIZE_Y) * CTB_SIZE_Y; + let (coded_width, coded_height) = + (align(config.resolution.width), align(config.resolution.height)); + // H.265 Table 6-1, the encoder only supports 4:2:0 subsampling. Must be set before // `resolution()`, which derives the conformance window from it. let sps = SpsBuilder::new(Rc::clone(&vps)) .seq_parameter_set_id(0) .chroma_format_idc(1) - .resolution(config.resolution.width, config.resolution.height) + .resolution(coded_width, coded_height) + .conformance_window( + 0, + coded_width - config.resolution.width, + 0, + coded_height - config.resolution.height, + ) .bit_depth_luma(8) .bit_depth_chroma(8) + .max_transform_hierarchy_depth_inter(MAX_TRANSFORM_HIERARCHY_DEPTH) + .max_transform_hierarchy_depth_intra(MAX_TRANSFORM_HIERARCHY_DEPTH) .max_dec_pic_buffering_minus1(MAX_DEC_PIC_BUFFERING_MINUS1) .max_num_reorder_pics(0) .max_latency_increase_plus1(0) @@ -136,6 +167,14 @@ impl LowDelayH265 { let pps = PpsBuilder::new(Rc::clone(&sps)) .pic_parameter_set_id(0) .init_qp(init_qp as i8) + // The driver's rate controller moves the quantizer between coding tree + // blocks, and `cu_qp_delta_abs` is how a stream says so. Left off -- the + // [`PpsBuilder`] default -- the deltas it writes are read as coefficient + // data instead and the picture is noise from the first block on. + .cu_qp_delta_enabled_flag(true) + // One quantization group per coding tree block, which is what a rate + // controller working in those units needs and nothing finer. + .diff_cu_qp_delta_depth(0) .deblocking_filter_control_present_flag(true) .num_ref_idx_l0_default_active_minus1(0) // Unused, P slices rely only on list0