forge
Explore
Log in

Don't you dare go Hollow

MPM reads syntax-level modes, not pixel availability

Browse files at this commit

c6dece13708caeb29a550b9e8e0e76dc35bc77f5Ahmet Kaan GümüşParent: a5a892b
  • fix
    • MPM reads syntax-level modes, not pixel availability
Show full message
fix(h265): MPM reads syntax-level modes, not pixel availability

r4's fix marked NxN partitions pixel-available before reconstruction,
which let prediction-reference construction (8.4.4.2.1) see samples that
do not exist yet — exposed by the merged proxy-ranking encoder's mode
choices at 70x50's bottom edge. MPM now reads the assigned-mode grid
directly (8.4.2 needs only syntax-level modes, signalled for all four
partitions before any reconstructs); pixel availability stays honest.
crates/ec-h265/src/decode.rsmodified+37 -25
@@ -294,17 +294,31 @@ impl CtuDecoder {
294 }294 }
295 }295 }
296 296
297+ /// The mode at a position if one has been assigned, independent of
298+ /// whether its *pixels* have been reconstructed yet: an NxN coding unit's
299+ /// `prev_intra_luma_pred_flag`s (and then every remainder) are all
300+ /// signalled before any of its four partitions is reconstructed
301+ /// (7.3.8.5), so by the time partition 2's mode is decoded, partition 1's
302+ /// mode is already known even though its transform tree has not run —
303+ /// MPM only needs the syntax-level mode (8.4.2), not the reconstructed
304+ /// sample `available()` gates for prediction reference construction
305+ /// (8.4.4.2.1). Using the `modes` sentinel directly (unset stays 255)
306+ /// keeps those two notions of "available" from being conflated: pixel
307+ /// availability still comes from `decoded`, set only once a position is
308+ /// actually reconstructed.
309+ fn mode_if_known(&self, x: isize, y: isize) -> Option<u8> {
310+ if x < 0 || y < 0 || x >= self.width as isize || y >= self.height as isize {
311+ return None;
312+ }
313+ let m = self.modes[(y as usize / 4) * (self.width / 4) + x as usize / 4];
314+ if m == 255 { None } else { Some(m) }
315+ }
316+
297 fn mpm_at(&self, x: usize, y: usize) -> [u8; 3] {317 fn mpm_at(&self, x: usize, y: usize) -> [u8; 3] {
298- let left = if self.available(x as isize - 1, y as isize) {318+ let left = self.mode_if_known(x as isize - 1, y as isize);
299- let m = self.mode_at(x - 1, y);
300- if m == 255 { None } else { Some(m) }
301- } else {
302- None
303- };
304 let band_y0 = (y / self.ctb_size) * self.ctb_size;319 let band_y0 = (y / self.ctb_size) * self.ctb_size;
305- let above = if y > band_y0 && self.available(x as isize, y as isize - 1) {320+ let above = if y > band_y0 {
306- let m = self.mode_at(x, y - 1);321+ self.mode_if_known(x as isize, y as isize - 1)
307- if m == 255 { None } else { Some(m) }
308 } else {322 } else {
309 None323 None
310 };324 };
@@ -512,25 +526,23 @@ impl CtuDecoder {
512 mode526 mode
513 };527 };
514 modes[part] = mode;528 modes[part] = mode;
515- // Mode and availability, so the next partition's `mpm_at` sees529+ // Mode only, so the next partition's `mpm_at` (via
516- // this one as a real coded neighbour — the encoder's own530+ // `mode_if_known`) sees this one's mode without its pixels being
517- // mode-decide loop calls `mark_coded` (which flips availability)531+ // reconstructed yet — 8.4.2's candIntraPredMode only needs the
518- // immediately per partition for exactly this reason (8.4.2: an532+ // neighbour's syntax-level mode, which an NxN CU's own earlier
519- // NxN CU's later partitions see its earlier ones as available).533+ // partition already has despite its transform tree not having
520- // Marking modes here without also marking `decoded` left the534+ // run (7.3.8.5 signals all four modes before any residual).
521- // second/third/fourth partition's left/above neighbour reading535+ // Marking `self.decoded` here too (as an earlier version of this
522- // as unavailable (substituted DC) on the decoder side while the536+ // loop did) went further than that: it made partition 2 and 3's
523- // encoder's MPM saw the real mode, decoding the wrong MPM triple537+ // *pixels* look available to partition 1's own reference-sample
524- // and desyncing every bin after it — this is what an NxN CU538+ // construction (8.4.4.2.1) before the second loop below had
525- // forced into existence by a boundary split (rather than chosen539+ // reconstructed them, reading zero-initialised `y_buf` instead of
526- // by `intra_nxn`'s RD search) actually exercises differently:540+ // the correctly-substituted "not yet available" edge case — a
527- // the RD search rarely keeps NxN once a real neighbour is541+ // one-CU-late reconstruction that stayed invisible until a
528- // available on all four sides, so the bug went unseen until a542+ // picture edge forced this exact NxN+RQT combination.
529- // picture edge forced it.
530 let stride = self.width / 4;543 let stride = self.width / 4;
531 let idx = (py / 4) * stride + px / 4;544 let idx = (py / 4) * stride + px / 4;
532 self.modes[idx] = mode;545 self.modes[idx] = mode;
533- self.decoded[idx] = true;
534 }546 }
535 547
536 let chroma_mode = self.decode_chroma_mode(dec, modes[0]);548 let chroma_mode = self.decode_chroma_mode(dec, modes[0]);