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 {
727 v.div_ceil(16) * 16727 v.div_ceil(16) * 16
728 }728 }
729 729
730+fn align64(v: u32) -> u32 {
731+ v.div_ceil(64) * 64
732+}
733+
730 /// Fills in slice NAL header bytes the driver left at zero.734 /// Fills in slice NAL header bytes the driver left at zero.
731 ///735 ///
732 /// Mesa's radeonsi encoder writes `forbidden_zero_bit`, `nal_ref_idc` and736 /// Mesa's radeonsi encoder writes `forbidden_zero_bit`, `nal_ref_idc` and
@@ -838,9 +842,17 @@ impl EncSession {
838 return None;842 return None;
839 }843 }
840 let (display, gbm) = open_devices()?;844 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+ };
841 let coded = Resolution {853 let coded = Resolution {
842- width: align16(width),854+ width: align(width),
843- height: align16(height),855+ height: align(height),
844 };856 };
845 let framerate = ((fps_num as f64 / fps_den as f64).round() as i64).clamp(1, 240) as u32;857 let framerate = ((fps_num as f64 / fps_den as f64).round() as i64).clamp(1, 240) as u32;
846 let profile = match codec {858 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;
37 /// lower than `NumNegativePics` of any short term reference picture set, see H.265 7.4.3.2.1.37 /// lower than `NumNegativePics` of any short term reference picture set, see H.265 7.4.3.2.1.
38 const MAX_DEC_PIC_BUFFERING_MINUS1: u8 = 1;38 const MAX_DEC_PIC_BUFFERING_MINUS1: u8 = 1;
39 39
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+
40 pub(crate) struct LowDelayH265Delegate {59 pub(crate) struct LowDelayH265Delegate {
41 /// Current sequence VPS60 /// Current sequence VPS
42 vps: Option<Rc<Vps>>,61 vps: Option<Rc<Vps>>,
@@ -106,14 +125,26 @@ impl<Picture, Reference> LowDelayH265<Picture, Reference> {
106 short_term_ref_pic_set.delta_poc_s0[0] = -1;125 short_term_ref_pic_set.delta_poc_s0[0] = -1;
107 short_term_ref_pic_set.used_by_curr_pic_s0[0] = true;126 short_term_ref_pic_set.used_by_curr_pic_s0[0] = true;
108 127
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+
109 // H.265 Table 6-1, the encoder only supports 4:2:0 subsampling. Must be set before132 // H.265 Table 6-1, the encoder only supports 4:2:0 subsampling. Must be set before
110 // `resolution()`, which derives the conformance window from it.133 // `resolution()`, which derives the conformance window from it.
111 let sps = SpsBuilder::new(Rc::clone(&vps))134 let sps = SpsBuilder::new(Rc::clone(&vps))
112 .seq_parameter_set_id(0)135 .seq_parameter_set_id(0)
113 .chroma_format_idc(1)136 .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+ )
115 .bit_depth_luma(8)144 .bit_depth_luma(8)
116 .bit_depth_chroma(8)145 .bit_depth_chroma(8)
146+ .max_transform_hierarchy_depth_inter(MAX_TRANSFORM_HIERARCHY_DEPTH)
147+ .max_transform_hierarchy_depth_intra(MAX_TRANSFORM_HIERARCHY_DEPTH)
117 .max_dec_pic_buffering_minus1(MAX_DEC_PIC_BUFFERING_MINUS1)148 .max_dec_pic_buffering_minus1(MAX_DEC_PIC_BUFFERING_MINUS1)
118 .max_num_reorder_pics(0)149 .max_num_reorder_pics(0)
119 .max_latency_increase_plus1(0)150 .max_latency_increase_plus1(0)
@@ -136,6 +167,14 @@ impl<Picture, Reference> LowDelayH265<Picture, Reference> {
136 let pps = PpsBuilder::new(Rc::clone(&sps))167 let pps = PpsBuilder::new(Rc::clone(&sps))
137 .pic_parameter_set_id(0)168 .pic_parameter_set_id(0)
138 .init_qp(init_qp as i8)169 .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)
139 .deblocking_filter_control_present_flag(true)178 .deblocking_filter_control_present_flag(true)
140 .num_ref_idx_l0_default_active_minus1(0)179 .num_ref_idx_l0_default_active_minus1(0)
141 // Unused, P slices rely only on list0180 // Unused, P slices rely only on list0