forge
Explore
Log in

Don't you dare go Hollow

Clamp intra reference-sample availability to the true frame edge, not the padded coding surface

Browse files at this commit

f79d3fd299af924518ae8faccb9f2ca3dad4b8e5Ahmet Kaan GümüşParent: 82410a6
  • fix
    • clamp intra reference-sample availability to the true frame edge, not the padded coding surface
Show full message
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.rsmodified+42 -7
@@ -475,6 +475,18 @@ struct Plane<'a> {
475475 reconstruction: Vec<u8>,
476476 width: usize,
477477 height: usize,
478+ /// The frame's true, decodable extent in this plane's own units --
479+ /// `mi_cols`/`mi_rows` (spec `compute_image_size`) converted to samples,
480+ /// not `width`/`height` above, which is the padded coding-surface size.
481+ /// A decoder's `BlockDecoded` bookkeeping (spec 7.4) and its `mb_to_
482+ /// right_edge`/`mb_to_bottom_edge` clamps (libaom's
483+ /// `av1/common/reconintra.c` `build_intra_predictors`) never see past
484+ /// this bound, even though the padded reconstruction buffer holds real
485+ /// (if never-decoded-by-a-real-decoder) samples out there -- reading
486+ /// those instead of stopping here is what desyncs prediction at the true
487+ /// edge.
488+ true_width: usize,
489+ true_height: usize,
478490 }
479491
480492 impl Plane<'_> {
@@ -494,11 +506,18 @@ impl Plane<'_> {
494506 side: usize,
495507 reach: Reach,
496508 ) -> (Option<Vec<u8>>, Option<Vec<u8>>, Option<u8>) {
497- let across = (x + if reach.above_right { 2 * side } else { side }).min(self.width);
498- let down = (y + if reach.below_left { 2 * side } else { side }).min(self.height);
499- let above =
500- (y > 0).then(|| self.reconstruction[(y - 1) * self.width + x..][..across - x].to_vec());
501- let left = (x > 0).then(|| {
509+ let across = (x + if reach.above_right { 2 * side } else { side }).min(self.true_width);
510+ let down = (y + if reach.below_left { 2 * side } else { side }).min(self.true_height);
511+ // `across`/`down` can fall at or below `x`/`y` for a block whose own
512+ // origin already sits past the true edge -- only reachable through a
513+ // partition trial priced for comparison on a split this frame's
514+ // straddling quadrant will go on to refuse (see
515+ // `a_picture_off_the_block_grid_is_refused`), never through a block
516+ // this encoder actually commits. Such a block has no true-edge
517+ // samples above or left of it at all, same as `y == 0`/`x == 0`.
518+ let above = (y > 0 && across > x)
519+ .then(|| self.reconstruction[(y - 1) * self.width + x..][..across - x].to_vec());
520+ let left = (x > 0 && down > y).then(|| {
502521 (y..down)
503522 .map(|row| self.reconstruction[row * self.width + x - 1])
504523 .collect::<Vec<_>>()
@@ -887,7 +906,7 @@ fn code_square(
887906 x,
888907 y,
889908 side,
890- reach: Reach::of(side, x, y, luma.width, luma.height),
909+ reach: Reach::of(side, x, y, luma.true_width, luma.true_height),
891910 set: luma_set,
892911 },
893912 search,
@@ -1078,11 +1097,16 @@ pub(crate) fn encode_key_frame_inner(
10781097 header.render_width = render.0 as u32;
10791098 header.render_height = render.1 as u32;
10801099
1100+ // Prediction reads no further than this, in each plane's own units --
1101+ // see `Plane::true_width`/`true_height`.
1102+ let (true_width, true_height) = (header.mi_cols as usize * 4, header.mi_rows as usize * 4);
10811103 let mut luma = Plane {
10821104 source: &picture.y,
10831105 reconstruction: vec![128; picture.y.len()],
10841106 width: picture.width,
10851107 height: picture.height,
1108+ true_width,
1109+ true_height,
10861110 };
10871111 let mut chroma = [
10881112 Plane {
@@ -1090,12 +1114,16 @@ pub(crate) fn encode_key_frame_inner(
10901114 reconstruction: vec![128; picture.u.len()],
10911115 width: picture.width / 2,
10921116 height: picture.height / 2,
1117+ true_width: true_width / 2,
1118+ true_height: true_height / 2,
10931119 },
10941120 Plane {
10951121 source: &picture.v,
10961122 reconstruction: vec![128; picture.v.len()],
10971123 width: picture.width / 2,
10981124 height: picture.height / 2,
1125+ true_width: true_width / 2,
1126+ true_height: true_height / 2,
10991127 },
11001128 ];
11011129
@@ -1465,7 +1493,7 @@ fn search_inter_block(
14651493 stack: &MvStack,
14661494 ) -> BlockCoeffs {
14671495 let (luma_set, chroma_set) = (TxbSet::Luma32, TxbSet::Chroma16);
1468- let reach = Reach::of(BLOCK, x, y, luma.width, luma.height);
1496+ let reach = Reach::of(BLOCK, x, y, luma.true_width, luma.true_height);
14691497
14701498 // skip / intra_inter symbol costs at a fixed context -- see this
14711499 // function's doc comment.
@@ -1789,11 +1817,14 @@ pub(crate) fn encode_inter_frame(
17891817 header.render_width = render.0 as u32;
17901818 header.render_height = render.1 as u32;
17911819
1820+ let (true_width, true_height) = (header.mi_cols as usize * 4, header.mi_rows as usize * 4);
17921821 let mut luma = Plane {
17931822 source: &picture.y,
17941823 reconstruction: vec![128; picture.y.len()],
17951824 width: picture.width,
17961825 height: picture.height,
1826+ true_width,
1827+ true_height,
17971828 };
17981829 let mut chroma = [
17991830 Plane {
@@ -1801,12 +1832,16 @@ pub(crate) fn encode_inter_frame(
18011832 reconstruction: vec![128; picture.u.len()],
18021833 width: picture.width / 2,
18031834 height: picture.height / 2,
1835+ true_width: true_width / 2,
1836+ true_height: true_height / 2,
18041837 },
18051838 Plane {
18061839 source: &picture.v,
18071840 reconstruction: vec![128; picture.v.len()],
18081841 width: picture.width / 2,
18091842 height: picture.height / 2,
1843+ true_width: true_width / 2,
1844+ true_height: true_height / 2,
18101845 },
18111846 ];
18121847