From f79d3fd299af924518ae8faccb9f2ca3dad4b8e5 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:22:56 +0300 Subject: [PATCH] fix(av1): clamp intra reference-sample availability to the true frame edge, not the padded coding surface Plane::width/height is the picture padded to a 32-aligned coding surface (Picture::padded_to); Reach::of and Plane::edges used it both to decide whether a directional mode's above-right/below-left extension is available and to clamp how far the above/left reference rows read, so a block near the true frame edge (but still inside the padded surface) read real padded-reconstruction samples a real decoder never has -- it stops at mi_cols*4 / mi_rows*4 (spec compute_image_size; libaom av1/common/reconintra.c build_intra_predictors clamps the same way via mb_to_right_edge/mb_to_bottom_edge) and replicates the last valid sample instead. Plane now carries that true bound (true_width/true_height, mi_cols*4/mi_rows*4) separately from its padded width/height, and Reach::of plus the edge-row clamp use it. A partition trial can still price a sub-block whose own origin already sits past that true bound (kept only for cost comparison against a split this frame goes on to refuse) -- across/down can now land at or below x/y for such a block, so edges() treats an empty read range as unavailable (None) rather than crashing on an empty "last sample" the same way y==0/ x==0 already do. --- crates/ec-av1/src/encode.rs | 49 +++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 44e040a1..47a607ed 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -475,6 +475,18 @@ struct Plane<'a> { reconstruction: Vec, width: usize, height: usize, + /// The frame's true, decodable extent in this plane's own units -- + /// `mi_cols`/`mi_rows` (spec `compute_image_size`) converted to samples, + /// not `width`/`height` above, which is the padded coding-surface size. + /// A decoder's `BlockDecoded` bookkeeping (spec 7.4) and its `mb_to_ + /// right_edge`/`mb_to_bottom_edge` clamps (libaom's + /// `av1/common/reconintra.c` `build_intra_predictors`) never see past + /// this bound, even though the padded reconstruction buffer holds real + /// (if never-decoded-by-a-real-decoder) samples out there -- reading + /// those instead of stopping here is what desyncs prediction at the true + /// edge. + true_width: usize, + true_height: usize, } impl Plane<'_> { @@ -494,11 +506,18 @@ impl Plane<'_> { side: usize, reach: Reach, ) -> (Option>, Option>, Option) { - let across = (x + if reach.above_right { 2 * side } else { side }).min(self.width); - let down = (y + if reach.below_left { 2 * side } else { side }).min(self.height); - let above = - (y > 0).then(|| self.reconstruction[(y - 1) * self.width + x..][..across - x].to_vec()); - let left = (x > 0).then(|| { + let across = (x + if reach.above_right { 2 * side } else { side }).min(self.true_width); + let down = (y + if reach.below_left { 2 * side } else { side }).min(self.true_height); + // `across`/`down` can fall at or below `x`/`y` for a block whose own + // origin already sits past the true edge -- only reachable through a + // partition trial priced for comparison on a split this frame's + // straddling quadrant will go on to refuse (see + // `a_picture_off_the_block_grid_is_refused`), never through a block + // this encoder actually commits. Such a block has no true-edge + // samples above or left of it at all, same as `y == 0`/`x == 0`. + let above = (y > 0 && across > x) + .then(|| self.reconstruction[(y - 1) * self.width + x..][..across - x].to_vec()); + let left = (x > 0 && down > y).then(|| { (y..down) .map(|row| self.reconstruction[row * self.width + x - 1]) .collect::>() @@ -887,7 +906,7 @@ fn code_square( x, y, side, - reach: Reach::of(side, x, y, luma.width, luma.height), + reach: Reach::of(side, x, y, luma.true_width, luma.true_height), set: luma_set, }, search, @@ -1078,11 +1097,16 @@ pub(crate) fn encode_key_frame_inner( header.render_width = render.0 as u32; header.render_height = render.1 as u32; + // Prediction reads no further than this, in each plane's own units -- + // see `Plane::true_width`/`true_height`. + let (true_width, true_height) = (header.mi_cols as usize * 4, header.mi_rows as usize * 4); let mut luma = Plane { source: &picture.y, reconstruction: vec![128; picture.y.len()], width: picture.width, height: picture.height, + true_width, + true_height, }; let mut chroma = [ Plane { @@ -1090,12 +1114,16 @@ pub(crate) fn encode_key_frame_inner( reconstruction: vec![128; picture.u.len()], width: picture.width / 2, height: picture.height / 2, + true_width: true_width / 2, + true_height: true_height / 2, }, Plane { source: &picture.v, reconstruction: vec![128; picture.v.len()], width: picture.width / 2, height: picture.height / 2, + true_width: true_width / 2, + true_height: true_height / 2, }, ]; @@ -1465,7 +1493,7 @@ fn search_inter_block( stack: &MvStack, ) -> BlockCoeffs { let (luma_set, chroma_set) = (TxbSet::Luma32, TxbSet::Chroma16); - let reach = Reach::of(BLOCK, x, y, luma.width, luma.height); + let reach = Reach::of(BLOCK, x, y, luma.true_width, luma.true_height); // skip / intra_inter symbol costs at a fixed context -- see this // function's doc comment. @@ -1789,11 +1817,14 @@ pub(crate) fn encode_inter_frame( header.render_width = render.0 as u32; header.render_height = render.1 as u32; + let (true_width, true_height) = (header.mi_cols as usize * 4, header.mi_rows as usize * 4); let mut luma = Plane { source: &picture.y, reconstruction: vec![128; picture.y.len()], width: picture.width, height: picture.height, + true_width, + true_height, }; let mut chroma = [ Plane { @@ -1801,12 +1832,16 @@ pub(crate) fn encode_inter_frame( reconstruction: vec![128; picture.u.len()], width: picture.width / 2, height: picture.height / 2, + true_width: true_width / 2, + true_height: true_height / 2, }, Plane { source: &picture.v, reconstruction: vec![128; picture.v.len()], width: picture.width / 2, height: picture.height / 2, + true_width: true_width / 2, + true_height: true_height / 2, }, ];