From 296a5012769ed5578ab0a95fafb8a490d2db5b88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 08:26:23 +0300 Subject: [PATCH] fix(h265): NxN partitions mark availability per partition, matching the encoder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit decode_cu_nxn deferred decoded[idx]=true to the post-loop mark_coded while the encoder flips availability immediately per partition — the decoder's mpm_at saw a same-CU sibling as unavailable-DC and derived a different MPM triple, desyncing every bin after. All odd sizes now round-trip byte-exact (70x50, 150x94, 41x33 added); full gate green. --- crates/ec-h265/src/decode.rs | 21 ++++++++++++++++++--- crates/ec-h265/tests/decode.rs | 26 +++++--------------------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/crates/ec-h265/src/decode.rs b/crates/ec-h265/src/decode.rs index 3e646d58..8b441f4a 100644 --- a/crates/ec-h265/src/decode.rs +++ b/crates/ec-h265/src/decode.rs @@ -512,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]); diff --git a/crates/ec-h265/tests/decode.rs b/crates/ec-h265/tests/decode.rs index 7c2b9c6e..4afb9f58 100644 --- a/crates/ec-h265/tests/decode.rs +++ b/crates/ec-h265/tests/decode.rs @@ -56,32 +56,16 @@ fn decode_matches_the_encoders_own_reconstruction_over_several_ctbs() { } #[test] -#[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); } + +#[test] +fn decode_matches_the_encoders_own_reconstruction_at_an_extreme_odd_size() { + roundtrip_at(41, 33); +}