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 {
294294 }
295295 }
296296
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+
297317 fn mpm_at(&self, x: usize, y: usize) -> [u8; 3] {
298- let left = if self.available(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- };
318+ let left = self.mode_if_known(x as isize - 1, y as isize);
304319 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) {
306- let m = self.mode_at(x, y - 1);
307- if m == 255 { None } else { Some(m) }
320+ let above = if y > band_y0 {
321+ self.mode_if_known(x as isize, y as isize - 1)
308322 } else {
309323 None
310324 };
@@ -512,25 +526,23 @@ impl CtuDecoder {
512526 mode
513527 };
514528 modes[part] = mode;
515- // Mode and availability, so the next partition's `mpm_at` sees
516- // this one as a real coded neighbour — the encoder's own
517- // mode-decide loop calls `mark_coded` (which flips availability)
518- // immediately per partition for exactly this reason (8.4.2: an
519- // NxN CU's later partitions see its earlier ones as available).
520- // Marking modes here without also marking `decoded` left the
521- // second/third/fourth partition's left/above neighbour reading
522- // as unavailable (substituted DC) on the decoder side while the
523- // encoder's MPM saw the real mode, decoding the wrong MPM triple
524- // and desyncing every bin after it — this is what an NxN CU
525- // forced into existence by a boundary split (rather than chosen
526- // by `intra_nxn`'s RD search) actually exercises differently:
527- // the RD search rarely keeps NxN once a real neighbour is
528- // available on all four sides, so the bug went unseen until a
529- // picture edge forced it.
529+ // Mode only, so the next partition's `mpm_at` (via
530+ // `mode_if_known`) sees this one's mode without its pixels being
531+ // reconstructed yet — 8.4.2's candIntraPredMode only needs the
532+ // neighbour's syntax-level mode, which an NxN CU's own earlier
533+ // partition already has despite its transform tree not having
534+ // run (7.3.8.5 signals all four modes before any residual).
535+ // Marking `self.decoded` here too (as an earlier version of this
536+ // loop did) went further than that: it made partition 2 and 3's
537+ // *pixels* look available to partition 1's own reference-sample
538+ // construction (8.4.4.2.1) before the second loop below had
539+ // reconstructed them, reading zero-initialised `y_buf` instead of
540+ // the correctly-substituted "not yet available" edge case — a
541+ // one-CU-late reconstruction that stayed invisible until a
542+ // picture edge forced this exact NxN+RQT combination.
530543 let stride = self.width / 4;
531544 let idx = (py / 4) * stride + px / 4;
532545 self.modes[idx] = mode;
533- self.decoded[idx] = true;
534546 }
535547
536548 let chroma_mode = self.decode_chroma_mode(dec, modes[0]);