forge
Explore
Log in

Don't you dare go Hollow

The parameter sets say what the GPU actually codes

Browse files at this commit

a082e61a2efd581715451c5d8bae21d262e22b61Ahmet Kaan GümüşParent: c67649f
  • fix
    • the parameter sets say what the GPU actually codes
Show full message
🐛 fix(hevc): the parameter sets say what the GPU actually codes

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.rsmodified+14 -2
@@ -727,6 +727,10 @@ fn align16(v: u32) -> u32 {
727727 v.div_ceil(16) * 16
728728 }
729729
730+fn align64(v: u32) -> u32 {
731+ v.div_ceil(64) * 64
732+}
733+
730734 /// Fills in slice NAL header bytes the driver left at zero.
731735 ///
732736 /// Mesa's radeonsi encoder writes `forbidden_zero_bit`, `nal_ref_idc` and
@@ -838,9 +842,17 @@ impl EncSession {
838842 return None;
839843 }
840844 let (display, gbm) = open_devices()?;
845+ // A macroblock is 16 wide; an HEVC coding tree block is 64, and that
846+ // seat's parameter sets declare the picture at coding-tree size (see the
847+ // vendored `h265/predictor.rs`), so the surface has to carry every row
848+ // those sets promise.
849+ let align = match codec {
850+ EncCodec::Hevc => align64,
851+ _ => align16,
852+ };
841853 let coded = Resolution {
842- width: align16(width),
843- height: align16(height),
854+ width: align(width),
855+ height: align(height),
844856 };
845857 let framerate = ((fps_num as f64 / fps_den as f64).round() as i64).clamp(1, 240) as u32;
846858 let profile = match codec {
vendor/cros-codecs/src/encoder/stateless/h265/predictor.rsmodified+40 -1
@@ -37,6 +37,25 @@ pub(crate) const MAX_QP: u8 = 51;
3737 /// lower than `NumNegativePics` of any short term reference picture set, see H.265 7.4.3.2.1.
3838 const MAX_DEC_PIC_BUFFERING_MINUS1: u8 = 1;
3939
40+/// How deep a transform tree may be split below its coding unit. Zero -- the [`SpsBuilder`]
41+/// default -- means `split_transform_flag` is never present and a decoder infers no split at
42+/// all, which is a promise about the *coded* syntax that no backend here can keep: a hardware
43+/// encoder splits transforms wherever the residual asks for it, so the flags it writes are
44+/// then read as coefficient data and the picture falls apart from the first coding tree unit
45+/// on. The maximum the block sizes allow is `CtbLog2SizeY - MinTbLog2SizeY` = 6 - 2, and
46+/// declaring it constrains nothing: an encoder that splits less simply writes zero flags.
47+const MAX_TRANSFORM_HIERARCHY_DEPTH: u8 = 4;
48+
49+/// The luma coding tree block the [`SpsBuilder`] defaults describe: 1 << (`log2_min_luma_
50+/// coding_block_size_minus3` + 3 + `log2_diff_max_min_luma_coding_block_size`), neither of
51+/// which this predictor overrides. A backend codes whole coding tree blocks, so the picture
52+/// it hands back is that size in both directions and the conformance window is what crops it
53+/// back to the resolution somebody asked for. Aligning only to `MinCbSizeY`, as
54+/// [`SpsBuilder::resolution`] does on its own, leaves a 1080 line picture claiming a 56 line
55+/// bottom row where the encoder coded 64, and the last coding tree row of every frame is then
56+/// read as coefficient data.
57+const CTB_SIZE_Y: u32 = 64;
58+
4059 pub(crate) struct LowDelayH265Delegate {
4160 /// Current sequence VPS
4261 vps: Option<Rc<Vps>>,
@@ -106,14 +125,26 @@ impl<Picture, Reference> LowDelayH265<Picture, Reference> {
106125 short_term_ref_pic_set.delta_poc_s0[0] = -1;
107126 short_term_ref_pic_set.used_by_curr_pic_s0[0] = true;
108127
128+ let align = |v: u32| v.div_ceil(CTB_SIZE_Y) * CTB_SIZE_Y;
129+ let (coded_width, coded_height) =
130+ (align(config.resolution.width), align(config.resolution.height));
131+
109132 // H.265 Table 6-1, the encoder only supports 4:2:0 subsampling. Must be set before
110133 // `resolution()`, which derives the conformance window from it.
111134 let sps = SpsBuilder::new(Rc::clone(&vps))
112135 .seq_parameter_set_id(0)
113136 .chroma_format_idc(1)
114- .resolution(config.resolution.width, config.resolution.height)
137+ .resolution(coded_width, coded_height)
138+ .conformance_window(
139+ 0,
140+ coded_width - config.resolution.width,
141+ 0,
142+ coded_height - config.resolution.height,
143+ )
115144 .bit_depth_luma(8)
116145 .bit_depth_chroma(8)
146+ .max_transform_hierarchy_depth_inter(MAX_TRANSFORM_HIERARCHY_DEPTH)
147+ .max_transform_hierarchy_depth_intra(MAX_TRANSFORM_HIERARCHY_DEPTH)
117148 .max_dec_pic_buffering_minus1(MAX_DEC_PIC_BUFFERING_MINUS1)
118149 .max_num_reorder_pics(0)
119150 .max_latency_increase_plus1(0)
@@ -136,6 +167,14 @@ impl<Picture, Reference> LowDelayH265<Picture, Reference> {
136167 let pps = PpsBuilder::new(Rc::clone(&sps))
137168 .pic_parameter_set_id(0)
138169 .init_qp(init_qp as i8)
170+ // The driver's rate controller moves the quantizer between coding tree
171+ // blocks, and `cu_qp_delta_abs` is how a stream says so. Left off -- the
172+ // [`PpsBuilder`] default -- the deltas it writes are read as coefficient
173+ // data instead and the picture is noise from the first block on.
174+ .cu_qp_delta_enabled_flag(true)
175+ // One quantization group per coding tree block, which is what a rate
176+ // controller working in those units needs and nothing finer.
177+ .diff_cu_qp_delta_depth(0)
139178 .deblocking_filter_control_present_flag(true)
140179 .num_ref_idx_l0_default_active_minus1(0)
141180 // Unused, P slices rely only on list0