diff --git a/crates/ec-h265/src/ctu.rs b/crates/ec-h265/src/ctu.rs index 0bb8fad6..746a8218 100644 --- a/crates/ec-h265/src/ctu.rs +++ b/crates/ec-h265/src/ctu.rs @@ -1208,8 +1208,16 @@ impl<'a> CtuEncoder<'a> { let mut sorted = *mpm; sorted.sort_unstable(); let mut rem = mode; + // The inverse of the decoder's iterative reconstruction + // (`for m in sorted (ascending): if mode >= m { mode += 1 }`) + // has to walk descending and compare the *running* `rem`, + // not the original `mode`: decrementing across one candidate + // can drop `rem` at or below the next one down, which then + // must also count. Comparing against the fixed `mode` missed + // that second step whenever two MPM candidates sat on either + // side of it. for &m in sorted.iter().rev() { - if mode > m { + if rem > m { rem -= 1; } } diff --git a/crates/ec-h265/src/decode.rs b/crates/ec-h265/src/decode.rs index b10a51ac..8b441f4a 100644 --- a/crates/ec-h265/src/decode.rs +++ b/crates/ec-h265/src/decode.rs @@ -101,8 +101,15 @@ fn decode_i_slice( header: &SliceHeader, slice_data: &[u8], ) -> Result { - let width = sps.pic_width_in_ctbs() as usize * sps.ctb_size() as usize; - let height = sps.pic_height_in_ctbs() as usize * sps.ctb_size() as usize; + // The coded picture, not the CTB-grid-rounded size: HEVC's last CTU in a + // row/column is legally partial (7.3.8.4's boundary-forced split exists + // precisely for this), and the encoder's own boundary checks are against + // `pic_width_in_luma_samples`/`pic_height_in_luma_samples`, not the next + // multiple of the CTB size. Rounding up here desynchronised the decoder's + // split_cu_flag inference from the encoder's whenever the two bases + // differed — every size that is not itself an exact CTB-size multiple. + let width = sps.pic_width_in_luma_samples as usize; + let height = sps.pic_height_in_luma_samples as usize; let ctb = sps.ctb_size() as usize; let cols = width.div_ceil(ctb); let rows = height.div_ceil(ctb); @@ -505,10 +512,25 @@ impl CtuDecoder { mode }; modes[part] = mode; - // Mode only, so the next partition's MPM sees it; pixels follow - // once the residual syntax below is read. + // Mode and availability, so the next partition's `mpm_at` sees + // this one as a real coded neighbour — the encoder's own + // mode-decide loop calls `mark_coded` (which flips availability) + // immediately per partition for exactly this reason (8.4.2: an + // NxN CU's later partitions see its earlier ones as available). + // Marking modes here without also marking `decoded` left the + // second/third/fourth partition's left/above neighbour reading + // as unavailable (substituted DC) on the decoder side while the + // encoder's MPM saw the real mode, decoding the wrong MPM triple + // and desyncing every bin after it — this is what an NxN CU + // forced into existence by a boundary split (rather than chosen + // by `intra_nxn`'s RD search) actually exercises differently: + // the RD search rarely keeps NxN once a real neighbour is + // available on all four sides, so the bug went unseen until a + // picture edge forced it. let stride = self.width / 4; - self.modes[(py / 4) * stride + px / 4] = mode; + let idx = (py / 4) * stride + px / 4; + self.modes[idx] = mode; + self.decoded[idx] = true; } let chroma_mode = self.decode_chroma_mode(dec, modes[0]); @@ -522,7 +544,13 @@ impl CtuDecoder { self.mark_coded(px, py, 4, modes[part], depth); self.mark_tu(px, py, 4); if part == 3 { - self.reconstruct_chroma(x, y, 8, chroma_mode, 2, cb_cbf, cr_cbf, dec); + // `reconstruct_chroma`'s `log2_luma` is the covering luma + // block's log2 (it derives `clog2 = log2_luma - 1` for the + // transform-split convention); an NxN CU's covering block is + // the whole 8x8 CU (`MIN_CB_LOG2`), not a 4x4 partition — + // passing `2` here derived `clog2 = 1` and underflowed + // `decode_residual`'s `log2_size - 2`. + self.reconstruct_chroma(x, y, 8, chroma_mode, MIN_CB_LOG2, cb_cbf, cr_cbf, dec); } } } diff --git a/crates/ec-h265/tests/decode.rs b/crates/ec-h265/tests/decode.rs index 5e695abf..4afb9f58 100644 --- a/crates/ec-h265/tests/decode.rs +++ b/crates/ec-h265/tests/decode.rs @@ -24,10 +24,7 @@ fn roundtrip_at(width: u32, height: u32) { }); let sps = enc.sps(); - assert_eq!( - decoded.width, - sps.pic_width_in_ctbs() as usize * sps.ctb_size() as usize - ); + assert_eq!(decoded.width, sps.pic_width_in_luma_samples as usize); // Compare over the displayed region only: `recon` is cropped by the // conformance window, `decoded` is the full coded (padded) picture. @@ -59,10 +56,16 @@ fn decode_matches_the_encoders_own_reconstruction_over_several_ctbs() { } #[test] -#[ignore = "r1 checkpoint: fails with 'end_of_subset_one_bit was not set at a WPP \ -row boundary' — the decoder's WPP substream handling at a sub-CTB-tail row is \ -wrong or the single-slice no-WPP case is misdetected; whole-CTB sizes decode \ -byte-exact. Next round fixes this, then un-ignores."] fn decode_matches_the_encoders_own_reconstruction_at_an_odd_padded_size() { roundtrip_at(70, 50); } + +#[test] +fn decode_matches_the_encoders_own_reconstruction_at_another_odd_size() { + roundtrip_at(150, 94); +} + +#[test] +fn decode_matches_the_encoders_own_reconstruction_at_an_extreme_odd_size() { + roundtrip_at(41, 33); +}