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> {
475 reconstruction: Vec<u8>,475 reconstruction: Vec<u8>,
476 width: usize,476 width: usize,
477 height: usize,477 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,
478 }490 }
479 491
480 impl Plane<'_> {492 impl Plane<'_> {
@@ -494,11 +506,18 @@ impl Plane<'_> {
494 side: usize,506 side: usize,
495 reach: Reach,507 reach: Reach,
496 ) -> (Option<Vec<u8>>, Option<Vec<u8>>, Option<u8>) {508 ) -> (Option<Vec<u8>>, Option<Vec<u8>>, Option<u8>) {
497- let across = (x + if reach.above_right { 2 * side } else { side }).min(self.width);509+ let across = (x + if reach.above_right { 2 * side } else { side }).min(self.true_width);
498- let down = (y + if reach.below_left { 2 * side } else { side }).min(self.height);510+ let down = (y + if reach.below_left { 2 * side } else { side }).min(self.true_height);
499- let above =511+ // `across`/`down` can fall at or below `x`/`y` for a block whose own
500- (y > 0).then(|| self.reconstruction[(y - 1) * self.width + x..][..across - x].to_vec());512+ // origin already sits past the true edge -- only reachable through a
501- let left = (x > 0).then(|| {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(|| {
502 (y..down)521 (y..down)
503 .map(|row| self.reconstruction[row * self.width + x - 1])522 .map(|row| self.reconstruction[row * self.width + x - 1])
504 .collect::<Vec<_>>()523 .collect::<Vec<_>>()
@@ -887,7 +906,7 @@ fn code_square(
887 x,906 x,
888 y,907 y,
889 side,908 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),
891 set: luma_set,910 set: luma_set,
892 },911 },
893 search,912 search,
@@ -1078,11 +1097,16 @@ pub(crate) fn encode_key_frame_inner(
1078 header.render_width = render.0 as u32;1097 header.render_width = render.0 as u32;
1079 header.render_height = render.1 as u32;1098 header.render_height = render.1 as u32;
1080 1099
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);
1081 let mut luma = Plane {1103 let mut luma = Plane {
1082 source: &picture.y,1104 source: &picture.y,
1083 reconstruction: vec![128; picture.y.len()],1105 reconstruction: vec![128; picture.y.len()],
1084 width: picture.width,1106 width: picture.width,
1085 height: picture.height,1107 height: picture.height,
1108+ true_width,
1109+ true_height,
1086 };1110 };
1087 let mut chroma = [1111 let mut chroma = [
1088 Plane {1112 Plane {
@@ -1090,12 +1114,16 @@ pub(crate) fn encode_key_frame_inner(
1090 reconstruction: vec![128; picture.u.len()],1114 reconstruction: vec![128; picture.u.len()],
1091 width: picture.width / 2,1115 width: picture.width / 2,
1092 height: picture.height / 2,1116 height: picture.height / 2,
1117+ true_width: true_width / 2,
1118+ true_height: true_height / 2,
1093 },1119 },
1094 Plane {1120 Plane {
1095 source: &picture.v,1121 source: &picture.v,
1096 reconstruction: vec![128; picture.v.len()],1122 reconstruction: vec![128; picture.v.len()],
1097 width: picture.width / 2,1123 width: picture.width / 2,
1098 height: picture.height / 2,1124 height: picture.height / 2,
1125+ true_width: true_width / 2,
1126+ true_height: true_height / 2,
1099 },1127 },
1100 ];1128 ];
1101 1129
@@ -1465,7 +1493,7 @@ fn search_inter_block(
1465 stack: &MvStack,1493 stack: &MvStack,
1466 ) -> BlockCoeffs {1494 ) -> BlockCoeffs {
1467 let (luma_set, chroma_set) = (TxbSet::Luma32, TxbSet::Chroma16);1495 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);
1469 1497
1470 // skip / intra_inter symbol costs at a fixed context -- see this1498 // skip / intra_inter symbol costs at a fixed context -- see this
1471 // function's doc comment.1499 // function's doc comment.
@@ -1789,11 +1817,14 @@ pub(crate) fn encode_inter_frame(
1789 header.render_width = render.0 as u32;1817 header.render_width = render.0 as u32;
1790 header.render_height = render.1 as u32;1818 header.render_height = render.1 as u32;
1791 1819
1820+ let (true_width, true_height) = (header.mi_cols as usize * 4, header.mi_rows as usize * 4);
1792 let mut luma = Plane {1821 let mut luma = Plane {
1793 source: &picture.y,1822 source: &picture.y,
1794 reconstruction: vec![128; picture.y.len()],1823 reconstruction: vec![128; picture.y.len()],
1795 width: picture.width,1824 width: picture.width,
1796 height: picture.height,1825 height: picture.height,
1826+ true_width,
1827+ true_height,
1797 };1828 };
1798 let mut chroma = [1829 let mut chroma = [
1799 Plane {1830 Plane {
@@ -1801,12 +1832,16 @@ pub(crate) fn encode_inter_frame(
1801 reconstruction: vec![128; picture.u.len()],1832 reconstruction: vec![128; picture.u.len()],
1802 width: picture.width / 2,1833 width: picture.width / 2,
1803 height: picture.height / 2,1834 height: picture.height / 2,
1835+ true_width: true_width / 2,
1836+ true_height: true_height / 2,
1804 },1837 },
1805 Plane {1838 Plane {
1806 source: &picture.v,1839 source: &picture.v,
1807 reconstruction: vec![128; picture.v.len()],1840 reconstruction: vec![128; picture.v.len()],
1808 width: picture.width / 2,1841 width: picture.width / 2,
1809 height: picture.height / 2,1842 height: picture.height / 2,
1843+ true_width: true_width / 2,
1844+ true_height: true_height / 2,
1810 },1845 },
1811 ];1846 ];
1812 1847