forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-av1-pad854': Reach tables indexed at libaom's 128px stride

Browse files at this commit

af9550052a8ca1ddb593bc5a5f0bf8be5589b428Ahmet Kaan GümüşParent: ec4c3f6d1d8e10
854x480 padded key frame closed: HAS_TOP_RIGHT/HAS_BOTTOM_LEFT are libaom's
has_tr_*/has_bl_* tables, indexed there with row stride 128/side (its
compile-time max superblock), not our 64/side — one 16x16 read bit 10
instead of bit 18 and lost its bottom-left neighbour, drifting the
reconstruction by one blend step. Boundary checks stay 64-relative.
New sweep test cross-checks Reach against a fresh transcription of
libaom's has_top_right/has_bottom_left at every position.

Gate: cargo test -p ec-av1 --release -- --include-ignored 122 passed / 0 failed.
crates/ec-av1/src/encode.rsmodified+126 -22
@@ -750,7 +750,8 @@ struct Reach {
750 /// libaom): for a block that is neither in the superblock's top row nor its750 /// libaom): for a block that is neither in the superblock's top row nor its
751 /// rightmost column, whether the block above and to its right is coded before751 /// rightmost column, whether the block above and to its right is coded before
752 /// it. Indexed by the block's position in the superblock, in blocks of its own752 /// it. Indexed by the block's position in the superblock, in blocks of its own
753-/// size, as `row * (16 >> log2 mi width) + col`, bit by bit from the low end.753+/// size, as `row * (128 / side) + col` (`Reach::table_stride`) -- libaom's
754+/// 128, not this crate's 64 superblock -- bit by bit from the low end.
754 const HAS_TOP_RIGHT: [&[u8]; 2] = [&[255, 85, 119, 85, 127, 85, 119, 85], &[95, 87]];755 const HAS_TOP_RIGHT: [&[u8]; 2] = [&[255, 85, 119, 85, 127, 85, 119, 85], &[95, 87]];
755 756
756 /// `has_bl_16x16` / `has_bl_32x32`: the same, for the block below and to the757 /// `has_bl_16x16` / `has_bl_32x32`: the same, for the block below and to the
@@ -788,7 +789,10 @@ impl Reach {
788 if col + 1 == per_side {789 if col + 1 == per_side {
789 return false;790 return false;
790 }791 }
791- Self::bit(HAS_TOP_RIGHT[Self::table(side)], row * per_side + col)792+ Self::bit(
793+ HAS_TOP_RIGHT[Self::table(side)],
794+ row * Self::table_stride(side) + col,
795+ )
792 }796 }
793 797
794 /// `has_bottom_left` for such a block: the leftmost column of a superblock798 /// `has_bottom_left` for such a block: the leftmost column of a superblock
@@ -803,7 +807,10 @@ impl Reach {
803 if row + 1 == per_side {807 if row + 1 == per_side {
804 return false;808 return false;
805 }809 }
806- Self::bit(HAS_BOTTOM_LEFT[Self::table(side)], row * per_side + col)810+ Self::bit(
811+ HAS_BOTTOM_LEFT[Self::table(side)],
812+ row * Self::table_stride(side) + col,
813+ )
807 }814 }
808 815
809 /// Where a block sits inside its superblock, in blocks of its own size,816 /// Where a block sits inside its superblock, in blocks of its own size,
@@ -816,6 +823,18 @@ impl Reach {
816 )823 )
817 }824 }
818 825
826+ /// The row stride `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` index by: libaom pins
827+ /// these tables to its compile-time maximum superblock (128, spec
828+ /// `MAX_MIB_SIZE_LOG2` = 5 in 4-pixel units), not to whichever superblock
829+ /// size an encode actually uses, so a block's row in the bit index steps
830+ /// by 128 / `side` even though this crate only ever codes a 64
831+ /// superblock -- using `per_side` (`SUPERBLOCK` / `side`, 64-relative)
832+ /// here instead indexes the wrong bit for every block size but the one
833+ /// where 64 and 128 give the same stride.
834+ fn table_stride(side: usize) -> usize {
835+ 128 / side
836+ }
837+
819 /// Which of the two block sizes the encoder codes a table row is for.838 /// Which of the two block sizes the encoder codes a table row is for.
820 fn table(side: usize) -> usize {839 fn table(side: usize) -> usize {
821 usize::from(side == BLOCK)840 usize::from(side == BLOCK)
@@ -2156,9 +2175,9 @@ mod tests {
2156 eprintln!("SKIP a_frame_round_trips_at_its_own_size: no ffmpeg");2175 eprintln!("SKIP a_frame_round_trips_at_its_own_size: no ffmpeg");
2157 return;2176 return;
2158 }2177 }
2159- // 854x480 is deliberately not in this list -- it hits an open,2178+ // 854x480 is covered separately by
2160- // content-dependent desync unrelated to padding wiring; see2179+ // `an_854x480_picture_round_trips_through_its_padding`, which its own
2161- // `open_defect_854x480_padded_key_frame_desyncs`.2180+ // doc comment explains was worth keeping as its own test.
2162 for &(width, height) in &[(1920usize, 1080usize), (640, 352), (1280, 720)] {2181 for &(width, height) in &[(1920usize, 1080usize), (640, 352), (1280, 720)] {
2163 let picture = test_card(width, height);2182 let picture = test_card(width, height);
2164 let encoded = encode_key_frame(&picture, 100, 0.5).unwrap();2183 let encoded = encode_key_frame(&picture, 100, 0.5).unwrap();
@@ -2187,23 +2206,25 @@ mod tests {
2187 }2206 }
2188 }2207 }
2189 2208
2190- /// Reproduces an open defect this lane's sweep found: 854x480 (padded to2209+ /// 854x480 (padded to 864x480, an edge-replication padded key frame) used
2191- /// 864x480, an *edge-replication* padded key frame -- not a preexisting2210+ /// to diverge from ffmpeg's decode at luma (row 161, col 369): a debug
2192- /// bug, since 864x480 coded directly with no padding involved is2211+ /// `aomdec` trace of `has_top_right`/`has_bottom_left` at that exact
2193- /// bit-exact) desyncs against ffmpeg's decode starting at luma (row 161,2212+ /// block (a 16x16 at mi (40, 88)) showed libaom indexing its pinned
2194- /// col 369), well inside the real (non-padded) region, not at the2213+ /// `has_bl_16x16` table at bit 18 (`row * 8 + col`, row=2 col=2), while
2195- /// padding edge. 640x352, 1280x720, 854x480's own height (480, already2214+ /// `Reach::bottom_left` indexed the same table at bit 10 (`row * 4 +
2196- /// 32-aligned) and 1920x1080 all round-trip clean, and the padding/crop2215+ /// col`) -- different bits of an 8-byte table, one true, one false. The
2197- /// plumbing itself is exercised bit-exact by2216+ /// stride libaom indexes by is fixed at its compile-time maximum
2198- /// `a_frame_round_trips_at_its_own_size` and the 64x64-superblock2217+ /// superblock (128px, `MAX_MIB_SIZE_LOG2` = 5), not the actual superblock
2199- /// sequence case, so this looks like a content-triggered desync inside2218+ /// size a stream uses, so the right stride for a 16x16 or 32x32 block is
2200- /// the coefficient/intra coding this lane was told not to touch2219+ /// `128 / side`, not `SUPERBLOCK / side` (64-relative) -- `Reach::of`'s
2201- /// (`tile.rs`), not a padding-wiring bug. Left `#[ignore]`d rather than2220+ /// row/col position within the (correctly 64-relative) superblock stayed
2202- /// silently dropped from the charter's size list -- next lane should2221+ /// right, only the table's own row stride was wrong. Fixed in
2203- /// start from the row/col this test prints.2222+ /// `Reach::table_stride`; not an arithmetic-coder desync (an
2223+ /// EC_RNG-per-symbol trace against the same debug decoder found the
2224+ /// bitstream byte-for-byte identical to libaom's across all 51188
2225+ /// symbols of this frame before this bug was found).
2204 #[test]2226 #[test]
2205- #[ignore = "open defect: content-dependent desync at 854x480 padded, not padding wiring -- see doc comment"]2227+ fn an_854x480_picture_round_trips_through_its_padding() {
2206- fn open_defect_854x480_padded_key_frame_desyncs() {
2207 if !have_ffmpeg() {2228 if !have_ffmpeg() {
2208 return;2229 return;
2209 }2230 }
@@ -2219,6 +2240,89 @@ mod tests {
2219 assert_eq!(cropped.y, encoded.reconstruction.y, "854x480: luma");2240 assert_eq!(cropped.y, encoded.reconstruction.y, "854x480: luma");
2220 }2241 }
2221 2242
2243+ /// `Reach::top_right`/`bottom_left` against a from-scratch transcription
2244+ /// of libaom's `has_top_right`/`has_bottom_left` (`av1/common/
2245+ /// reconintra.c`), for every 16x16 and 32x32 block position across three
2246+ /// superblocks square -- interior and every superblock edge (top row,
2247+ /// left column, right column, bottom row) both sizes reach. Written to
2248+ /// catch the class the `table_stride` bug was: an index stride silently
2249+ /// wrong for one block size while looking plausible for the other.
2250+ #[test]
2251+ fn reach_matches_libaom_has_top_right_and_has_bottom_left() {
2252+ // Transcribed from has_top_right/has_bottom_left's row_off==0,
2253+ // col_off==0 (whole-transform) path, with MAX_MIB_SIZE_LOG2 = 5 (a
2254+ // 128px reference grid) pinned as libaom pins it, independent of the
2255+ // 64px superblock this crate actually codes.
2256+ fn libaom_top_right(side: usize, x: usize, y: usize, width: usize, height: usize) -> bool {
2257+ if y == 0 || x + side >= width {
2258+ return false;
2259+ }
2260+ let (row, col, per_side) = (
2261+ (y % SUPERBLOCK) / side,
2262+ (x % SUPERBLOCK) / side,
2263+ SUPERBLOCK / side,
2264+ );
2265+ if row == 0 {
2266+ return true;
2267+ }
2268+ if col + 1 == per_side {
2269+ return false;
2270+ }
2271+ let stride = 128 / side;
2272+ let index = row * stride + col;
2273+ let table = if side == BLOCK {
2274+ [95u8, 87].to_vec()
2275+ } else {
2276+ vec![255, 85, 119, 85, 127, 85, 119, 85]
2277+ };
2278+ (table[index / 8] >> (index % 8)) & 1 != 0
2279+ }
2280+
2281+ fn libaom_bottom_left(side: usize, x: usize, y: usize, height: usize) -> bool {
2282+ if x == 0 || y + side >= height {
2283+ return false;
2284+ }
2285+ let (row, col, per_side) = (
2286+ (y % SUPERBLOCK) / side,
2287+ (x % SUPERBLOCK) / side,
2288+ SUPERBLOCK / side,
2289+ );
2290+ if col == 0 {
2291+ return row * side + side < SUPERBLOCK;
2292+ }
2293+ if row + 1 == per_side {
2294+ return false;
2295+ }
2296+ let stride = 128 / side;
2297+ let index = row * stride + col;
2298+ let table = if side == BLOCK {
2299+ [4u8, 4].to_vec()
2300+ } else {
2301+ vec![84, 16, 84, 0, 84, 16, 84, 0]
2302+ };
2303+ (table[index / 8] >> (index % 8)) & 1 != 0
2304+ }
2305+
2306+ let (width, height) = (SUPERBLOCK * 3, SUPERBLOCK * 3);
2307+ for side in [16usize, BLOCK] {
2308+ for y in (0..height).step_by(side) {
2309+ for x in (0..width).step_by(side) {
2310+ let reach = Reach::of(side, x, y, width, height);
2311+ assert_eq!(
2312+ reach.above_right,
2313+ libaom_top_right(side, x, y, width, height),
2314+ "side={side} x={x} y={y}: above_right"
2315+ );
2316+ assert_eq!(
2317+ reach.below_left,
2318+ libaom_bottom_left(side, x, y, height),
2319+ "side={side} x={x} y={y}: below_left"
2320+ );
2321+ }
2322+ }
2323+ }
2324+ }
2325+
2222 /// An odd width or height is refused by name, not by however the padder2326 /// An odd width or height is refused by name, not by however the padder
2223 /// or the block coder would happen to fail on it.2327 /// or the block coder would happen to fail on it.
2224 #[test]2328 #[test]