From dc2d88c55ef361321ded597efbeaf4f8b2af4345 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 02:44:29 +0300 Subject: [PATCH] fix(av1): clamp intra reference reads to the true edge even off a block's own row/col libaom's av1_predict_intra_block (av1/common/reconintra.c) derives xr/yd -- the distance from a block's own right/bottom to the frame edge -- from mb_to_right_edge/mb_to_bottom_edge, which are built from mi_cols/mi_rows (the true, decodable extent), never any padded coding surface. n_top_px/ n_left_px is min(side, xr + side) even for a block whose own extent straddles that edge legally (has_half) -- prediction does not get real reconstructed pixels past the true edge for its own row/column either, only reach ever got that clamp before. Plane::edges() now clamps the own-portion the same way libaom's xr/yd do. Verified pixel-exact against aomdec on the 216x96 col-edge repro (mi(8,48)) that was silently wrong before (0 diffs, previously untested). Gate unchanged at 124/127 -- the remaining 3 reds (854x480, 1920x1080, non-multiple-size sequence) trace to a separate, not-yet-isolated defect: a uniform -3 luma offset across every straddling block in a column strip, first appearing at mi_row=8 mi_col=208 in 854x480 with a nonzero DC coefficient present, i.e. downstream of this fix (dequant/IDCT or mode choice, not reference-sample availability). --- crates/ec-av1/src/encode.rs | 44 ++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 89f9d8bf..eef6469c 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -506,30 +506,34 @@ impl Plane<'_> { side: usize, reach: Reach, ) -> (Option>, Option>, Option) { - // A block's own edge (no `reach`) reads samples reconstructed as part - // of *some* legally-coded block, whole or not, even where that - // block's own extent straddles the true frame edge -- `has_half` - // (spec `hasRows`/`hasCols`) lets a whole block stand there, and its - // full extent gets a real transform, not a garbage tail, so this - // reads no further than the padded coding surface. Only a `reach` - // extension crosses into a block that may never be coded at all past - // the true edge, so only it clamps to the true bound (`mi_cols`/ - // `mi_rows` * 4), replicating past it same as spec 7.11.2.2's - // `aboveLimit`/`leftLimit` clamp. + // Even a block's *own* edge is clamped to the true frame bound, not + // just a `reach` extension: libaom's `av1_predict_intra_block` + // (`av1/common/reconintra.c`) derives `xr`/`yd` -- the distance from + // this block's own right/bottom to the frame edge -- from + // `mb_to_right_edge`/`mb_to_bottom_edge`, which are built from + // `mi_cols`/`mi_rows` (the true, decodable extent), never from any + // padded coding surface. `n_top_px`/`n_left_px` (how many *real* + // samples the row/column above/left holds before the decoder starts + // repeating the last one, same clamp as spec 7.11.2.2's `aboveLimit`/ + // `leftLimit`) is `min(side, xr + side)` even for a block whose own + // extent straddles that edge legally (`has_half`) -- the transform + // still covers the whole block, but prediction does not get to read + // real reconstructed pixels past the true edge for its own row/column, + // only for the padded tail it invents. + let own_across = x + side.min(self.true_width.saturating_sub(x)); let across = if reach.above_right { - (x + 2 * side) - .min(self.width) - .min(self.true_width.max(x + side)) + own_across + side.min(self.true_width.saturating_sub(own_across)) } else { - (x + side).min(self.width) - }; + own_across + } + .min(self.width); + let own_down = y + side.min(self.true_height.saturating_sub(y)); let down = if reach.below_left { - (y + 2 * side) - .min(self.height) - .min(self.true_height.max(y + side)) + own_down + side.min(self.true_height.saturating_sub(own_down)) } else { - (y + side).min(self.height) - }; + own_down + } + .min(self.height); // `across`/`down` can still fall at or below `x`/`y` for a sub-block // whose own origin already sits past the true edge -- only reachable // through a partition trial priced for comparison on a split this