diff --git a/crates/ec-h265/src/ctu.rs b/crates/ec-h265/src/ctu.rs index 27f991ef..36425098 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..3e646d58 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); @@ -522,7 +529,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..7c2b9c6e 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,32 @@ 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."] +#[ignore = "r3 checkpoint: fails with 'end_of_slice_segment_flag 0 at CTB (2,1), \ +expected 1' — narrowed (not yet root-caused) to the interaction of the NxN \ +intra partition (PART_NxN, an 8x8 CU split into four 4x4 PUs) with a \ +boundary-forced coding-unit: forcing every 8x8 leaf to code as 2Nx2N \ +(`intra_nxn: false`) round-trips clean; forcing every 8x8 leaf to NxN \ +(EC_H265_FORCE_NXN local hack, removed) round-trips clean on an exact-CTB-\ +multiple picture (64x64, ctb 64) but reproduces this same failure on a \ +picture whose last CTU is boundary-forced-split down to 8x8 (40x50, ctb 64, \ +no WPP — cols=1 rules out the WPP substream code entirely). So: NxN syntax \ +itself is right (it round-trips away from a picture edge); the bug is in \ +what an 8x8 CU sees or does differently when the quadtree reached it via \ +the boundary force-split path (7.3.8.4, no split_cu_flag bin) rather than \ +via a decoded split_cu_flag==0 — likely `mpm_at`/`available`'s neighbour \ +lookup, since that is the only per-CU state NxN reads that a forced-split \ +ancestor could leave in a different shape than a normal split would. Next \ +round: trace `available()`/`mode_at()` at the forced-split leaf specifically \ +(cheap, no RD-search noise, since EC_H265_FORCE_NXN made this deterministic \ +without touching the passing 2Nx2N path)."] fn decode_matches_the_encoders_own_reconstruction_at_an_odd_padded_size() { roundtrip_at(70, 50); } + +#[test] +#[ignore = "r3 checkpoint: same root cause as \ +decode_matches_the_encoders_own_reconstruction_at_an_odd_padded_size — NxN \ +at a boundary-forced 8x8 leaf; see that test's ignore reason."] +fn decode_matches_the_encoders_own_reconstruction_at_another_odd_size() { + roundtrip_at(150, 94); +}