Clamp intra reference-sample availability to the true frame edge, not the padded coding surface
Browse files at this commitf79d3fd299af924518ae8faccb9f2ca3dad4b8e5Parent: 82410a6- fix
- clamp intra reference-sample availability to the true frame edge, not the padded coding surface
1 changed file +42 -7
- crates/ec-av1/src/encode.rs +42 -7
crates/ec-av1/src/encode.rsmodified+42 -7
| @@ -475,6 +475,18 @@ struct Plane<'a> { | ||
| 475 | 475 | reconstruction: Vec<u8>, |
| 476 | 476 | width: usize, |
| 477 | 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 | 492 | impl Plane<'_> { |
| @@ -494,11 +506,18 @@ impl Plane<'_> { | ||
| 494 | 506 | side: usize, |
| 495 | 507 | reach: Reach, |
| 496 | 508 | ) -> (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(|| { | |
| 502 | 521 | (y..down) |
| 503 | 522 | .map(|row| self.reconstruction[row * self.width + x - 1]) |
| 504 | 523 | .collect::<Vec<_>>() |
| @@ -887,7 +906,7 @@ fn code_square( | ||
| 887 | 906 | x, |
| 888 | 907 | y, |
| 889 | 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 | 910 | set: luma_set, |
| 892 | 911 | }, |
| 893 | 912 | search, |
| @@ -1078,11 +1097,16 @@ pub(crate) fn encode_key_frame_inner( | ||
| 1078 | 1097 | header.render_width = render.0 as u32; |
| 1079 | 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 | 1103 | let mut luma = Plane { |
| 1082 | 1104 | source: &picture.y, |
| 1083 | 1105 | reconstruction: vec![128; picture.y.len()], |
| 1084 | 1106 | width: picture.width, |
| 1085 | 1107 | height: picture.height, |
| 1108 | + true_width, | |
| 1109 | + true_height, | |
| 1086 | 1110 | }; |
| 1087 | 1111 | let mut chroma = [ |
| 1088 | 1112 | Plane { |
| @@ -1090,12 +1114,16 @@ pub(crate) fn encode_key_frame_inner( | ||
| 1090 | 1114 | reconstruction: vec![128; picture.u.len()], |
| 1091 | 1115 | width: picture.width / 2, |
| 1092 | 1116 | height: picture.height / 2, |
| 1117 | + true_width: true_width / 2, | |
| 1118 | + true_height: true_height / 2, | |
| 1093 | 1119 | }, |
| 1094 | 1120 | Plane { |
| 1095 | 1121 | source: &picture.v, |
| 1096 | 1122 | reconstruction: vec![128; picture.v.len()], |
| 1097 | 1123 | width: picture.width / 2, |
| 1098 | 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 | 1493 | stack: &MvStack, |
| 1466 | 1494 | ) -> BlockCoeffs { |
| 1467 | 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 | 1498 | // skip / intra_inter symbol costs at a fixed context -- see this |
| 1471 | 1499 | // function's doc comment. |
| @@ -1789,11 +1817,14 @@ pub(crate) fn encode_inter_frame( | ||
| 1789 | 1817 | header.render_width = render.0 as u32; |
| 1790 | 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 | 1821 | let mut luma = Plane { |
| 1793 | 1822 | source: &picture.y, |
| 1794 | 1823 | reconstruction: vec![128; picture.y.len()], |
| 1795 | 1824 | width: picture.width, |
| 1796 | 1825 | height: picture.height, |
| 1826 | + true_width, | |
| 1827 | + true_height, | |
| 1797 | 1828 | }; |
| 1798 | 1829 | let mut chroma = [ |
| 1799 | 1830 | Plane { |
| @@ -1801,12 +1832,16 @@ pub(crate) fn encode_inter_frame( | ||
| 1801 | 1832 | reconstruction: vec![128; picture.u.len()], |
| 1802 | 1833 | width: picture.width / 2, |
| 1803 | 1834 | height: picture.height / 2, |
| 1835 | + true_width: true_width / 2, | |
| 1836 | + true_height: true_height / 2, | |
| 1804 | 1837 | }, |
| 1805 | 1838 | Plane { |
| 1806 | 1839 | source: &picture.v, |
| 1807 | 1840 | reconstruction: vec![128; picture.v.len()], |
| 1808 | 1841 | width: picture.width / 2, |
| 1809 | 1842 | height: picture.height / 2, |
| 1843 | + true_width: true_width / 2, | |
| 1844 | + true_height: true_height / 2, | |
| 1810 | 1845 | }, |
| 1811 | 1846 | ]; |
| 1812 | 1847 |