forge
Explore
Log in

Don't you dare go Hollow

Rect leaves read has_tr/has_bl from the wrong block-size table

Browse files at this commit

ae5d89dbd7be563c1c840966d601058530424879Ahmet Kaan GümüşParent: 7f6f585
  • fix
    • rect leaves read has_tr/has_bl from the wrong block-size table
    • mi-exact intra mode neighbours (cherry-pick lane-sub8 1841406)
Show full message
fix(av1): rect leaves read has_tr/has_bl from the wrong block-size table; mi-exact intra mode neighbours (cherry-pick lane-sub8 1841406)

Two defects, both on the sub-16x16 rect strip path:

1. `Reach::of_rect` looked up `HAS_TOP_RIGHT_RECT`/`HAS_BOTTOM_LEFT_RECT`
   indexed by `bw == 16` -- tables transcribed for the 32x16/16x32 strips
   only. A 16x8 block therefore read has_tr_16x32/has_bl_16x32 (4 bytes)
   where libaom reads has_tr_16x8/has_bl_16x8 (16 bytes), and an 8x16 read
   the 32x16 rows. `top_right_rect`/`bottom_left_rect` are now libaom
   `has_top_right`/`has_bottom_left` verbatim at block granularity, with the
   16x8/8x16 tables added. Symptom: a D203 16x8 strip's prediction was exact
   until the ray first reads BELOW its own height, then diverged (first diff
   at (44,18) of a 64x64 key frame, growing to the whole lower-right frame).

2. Cherry-picked lane-sub8 1841406's mi-granular intra-mode neighbour map
   (`sub8_mode_col/row`, `record_mode_mi`, `mode_above_mi`/`mode_left_mi`,
   plus `has_tr_4x4`/`has_bl_4x4` in encode.rs); the split-4x4 hunks are
   dropped, this branch has no `decode_leaf_split4`. `decode_leaf_rect` now
   reads through that map and records its own bw x bh span, which fixes the
   wrong `kf_y_mode` CDF row r3 located at leaf mi=(4,12).

Gate `a_real_aomenc_stream_with_a_coded_rect_strip_below_16x16_decodes_pixel_exact`
is green: pixel-exact vs ffmpeg, rect_leaf_coeff_hits=8 (6 non-skip 16x8 +
1 non-skip 8x16 luma transforms). Its recipe moved to mandelbrot start_x=-0.6
cq16 with --enable-filter-intra=0 (filter intra on a strip is a separate,
still-refused predictor).
crates/ec-av1/examples/decode_probe.rsmodified+16 -1
@@ -32,7 +32,22 @@ fn main() {
32 Ok(frames) if frames.is_empty() => {32 Ok(frames) if frames.is_empty() => {
33 println!("OK but EMPTY: no frames -- is {path} an IVF rather than a raw OBU stream?");33 println!("OK but EMPTY: no frames -- is {path} an IVF rather than a raw OBU stream?");
34 }34 }
35- Ok(frames) => println!("OK: {} frames decoded", frames.len()),35+ Ok(frames) => {
36+ println!("OK: {} frames decoded", frames.len());
37+ // Optional second arg: dump the decoded planes as raw yuv420p so a
38+ // pixel diff against `ffmpeg -i s.obu -f rawvideo` needs no test harness.
39+ if let Some(out) = std::env::args().nth(2) {
40+ // 8-bit only: planes are u16, take the low byte.
41+ let mut buf: Vec<u8> = Vec::new();
42+ for f in &frames {
43+ for p in [&f.y, &f.u, &f.v] {
44+ buf.extend(p.iter().map(|&s| s as u8));
45+ }
46+ }
47+ std::fs::write(&out, &buf).expect("writing raw planes");
48+ println!("wrote {} bytes of yuv420p to {out}", buf.len());
49+ }
50+ }
36 Err(e) => println!("REFUSED: {e}"),51 Err(e) => println!("REFUSED: {e}"),
37 }52 }
38 }53 }
crates/ec-av1/src/decode.rsmodified+82 -0
@@ -1826,6 +1826,19 @@ struct Neighbours {
1826 /// one was coded at 8x8.1826 /// one was coded at 8x8.
1827 above_side_mi: Vec<usize>,1827 above_side_mi: Vec<usize>,
1828 left_side_mi: Vec<usize>,1828 left_side_mi: Vec<usize>,
1829+ /// The luma `mode` of the last `BLOCK_4X4` leaf decoded at each mi column
1830+ /// / row, with the mi position it was written at
1831+ /// (`(usize::MAX, _)` = never written). `above_mode`/`left_mode` are on
1832+ /// the coarse 16x16 grid, which cannot distinguish the two 4x4 leaves an
1833+ /// 8x8 `PARTITION_SPLIT` puts in one mi column; libaom's intra-mode
1834+ /// context reads `mi(row, col-1)`/`mi(row-1, col)` exactly, so a sub-8x8
1835+ /// leaf whose neighbour is another split group needs that group's leaf 3
1836+ /// (bottom-right), not the leaf the coarse slot happens to hold. Written
1837+ /// and read only by [`decode_leaf_split4`]; the position guard makes it
1838+ /// fall back to the coarse slot whenever the real neighbour was a block
1839+ /// of 8x8 or larger (whose one mode is correct for every row it spans).
1840+ sub8_mode_col: Vec<(usize, usize)>,
1841+ sub8_mode_row: Vec<(usize, usize)>,
1829 /// Whether the block above/left of this column/row was coded `skip` --1842 /// Whether the block above/left of this column/row was coded `skip` --
1830 /// an inter frame's own `skip` context (spec `SkipContext`), which the1843 /// an inter frame's own `skip` context (spec `SkipContext`), which the
1831 /// key-frame writer never tracks (its skip context is always zero); a1844 /// key-frame writer never tracks (its skip context is always zero); a
@@ -1953,6 +1966,8 @@ impl Neighbours {
1953 left_side: vec![SB; rows],1966 left_side: vec![SB; rows],
1954 above_side_mi: vec![SB; cols * (SUB / MI)],1967 above_side_mi: vec![SB; cols * (SUB / MI)],
1955 left_side_mi: vec![SB; rows * (SUB / MI)],1968 left_side_mi: vec![SB; rows * (SUB / MI)],
1969+ sub8_mode_col: vec![(usize::MAX, 0); cols * (SUB / MI)],
1970+ sub8_mode_row: vec![(usize::MAX, 0); rows * (SUB / MI)],
1956 above_skip: vec![false; cols],1971 above_skip: vec![false; cols],
1957 left_skip: vec![false; rows],1972 left_skip: vec![false; rows],
1958 above_skip_mode: vec![false; cols],1973 above_skip_mode: vec![false; cols],
@@ -2514,6 +2529,46 @@ impl Neighbours {
2514 /// [`Self::partition_ctx`]) are the neighbour's own width/height per the2529 /// [`Self::partition_ctx`]) are the neighbour's own width/height per the
2515 /// spec's per-edge partition-context rule, so a true rectangular block2530 /// spec's per-edge partition-context rule, so a true rectangular block
2516 /// naturally wants two different values here rather than one.2531 /// naturally wants two different values here rather than one.
2532+ /// Writes this block's luma `mode` into the mi-granular
2533+ /// [`Self::sub8_mode_col`]/[`Self::sub8_mode_row`] maps, keyed by the mi
2534+ /// position a *later* neighbour would read it from: the block's LAST mi
2535+ /// row for every column it spans (what the block below reads via
2536+ /// `mi(row-1, col)`) and its LAST mi column for every row (what the
2537+ /// block to the right reads via `mi(row, col-1)`). Only sub-8x8 leaves
2538+ /// read these back, and only when the recorded position is exactly their
2539+ /// own neighbour -- see [`Self::sub8_mode_col`]'s doc.
2540+ fn record_mode_mi(&mut self, mi_r: usize, mi_c: usize, mi_w: usize, mi_h: usize, mode: usize) {
2541+ let (last_r, last_c) = (mi_r + mi_h - 1, mi_c + mi_w - 1);
2542+ for cell in 0..mi_w {
2543+ if let Some(slot) = self.sub8_mode_col.get_mut(mi_c + cell) {
2544+ *slot = (last_r, mode);
2545+ }
2546+ }
2547+ for cell in 0..mi_h {
2548+ if let Some(slot) = self.sub8_mode_row.get_mut(mi_r + cell) {
2549+ *slot = (last_c, mode);
2550+ }
2551+ }
2552+ }
2553+
2554+ /// The mi-exact above/left neighbour luma mode for a block whose top-left
2555+ /// mi is `(mi_r, mi_c)`, or `None` when no block has recorded that exact
2556+ /// mi position (then the coarse [`SUB`]-grid slot is right by
2557+ /// construction). See [`Self::record_mode_mi`].
2558+ fn mode_above_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> {
2559+ match self.sub8_mode_col.get(mi_c) {
2560+ Some(&(row, m)) if mi_r > 0 && row == mi_r - 1 => Some(m),
2561+ _ => None,
2562+ }
2563+ }
2564+
2565+ fn mode_left_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> {
2566+ match self.sub8_mode_row.get(mi_r) {
2567+ Some(&(col, m)) if mi_c > 0 && col == mi_c - 1 => Some(m),
2568+ _ => None,
2569+ }
2570+ }
2571+
2517 fn record_rect(2572 fn record_rect(
2518 &mut self,2573 &mut self,
2519 at: (usize, usize),2574 at: (usize, usize),
@@ -2534,6 +2589,7 @@ impl Neighbours {
2534 self.left_uv_mode[r + cell] = uv_mode;2589 self.left_uv_mode[r + cell] = uv_mode;
2535 self.left_side[r + cell] = h;2590 self.left_side[r + cell] = h;
2536 }2591 }
2592+ self.record_mode_mi(r * (SUB / MI), c * (SUB / MI), w / MI, h / MI, mode);
2537 self.record_mi_rect((r * (SUB / MI), c * (SUB / MI)), w, h, grids);2593 self.record_mi_rect((r * (SUB / MI), c * (SUB / MI)), w, h, grids);
2538 }2594 }
2539 2595
@@ -2644,6 +2700,7 @@ impl Neighbours {
2644 }2700 }
2645 let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI));2701 let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI));
2646 let side_mi = side / MI;2702 let side_mi = side / MI;
2703+ self.record_mode_mi(mi_r, mi_c, side_mi, side_mi, mode);
2647 for cell in 0..side_mi {2704 for cell in 0..side_mi {
2648 self.left_side_mi[mi_r + cell] = side;2705 self.left_side_mi[mi_r + cell] = side;
2649 self.above_side_mi[mi_c + cell] = side;2706 self.above_side_mi[mi_c + cell] = side;
@@ -3590,6 +3647,16 @@ fn decode_leaf_rect(
3590 left_mode = pmode;3647 left_mode = pmode;
3591 }3648 }
3592 }3649 }
3650+ // Same mi-exact override [`decode_leaf8`] takes: one coarse 16x16 slot
3651+ // cannot hold the modes a split (or a pair of strips) leaves behind, so
3652+ // when the map holds the block at exactly this strip's above/left mi it
3653+ // is the neighbour libaom's `above_mi`/`left_mi` reads.
3654+ if let Some(m) = neighbours.mode_above_mi(leaf_mi.0, leaf_mi.1) {
3655+ above_mode = m;
3656+ }
3657+ if let Some(m) = neighbours.mode_left_mi(leaf_mi.0, leaf_mi.1) {
3658+ left_mode = m;
3659+ }
3593 let smooth_neighbor =3660 let smooth_neighbor =
3594 is_smooth_mode(above_mode) || is_smooth_mode(left_mode);3661 is_smooth_mode(above_mode) || is_smooth_mode(left_mode);
3595 if smooth_neighbor {3662 if smooth_neighbor {
@@ -3726,6 +3793,9 @@ fn decode_leaf_rect(
3726 neighbours.record_mi_rect(leaf_mi, bw, bh, &[luma_levels, u_levels, v_levels]);3793 neighbours.record_mi_rect(leaf_mi, bw, bh, &[luma_levels, u_levels, v_levels]);
3727 neighbours.fill_skip_grid_rect(leaf_mi, mi_w, mi_h, skip);3794 neighbours.fill_skip_grid_rect(leaf_mi, mi_w, mi_h, skip);
3728 neighbours.fill_lf_grid_rect(leaf_mi, mi_w, mi_h, bw as u8, bh as u8, 0);3795 neighbours.fill_lf_grid_rect(leaf_mi, mi_w, mi_h, bw as u8, bh as u8, 0);
3796+ // A rect leaf spans mi_w x mi_h, so its mode reaches several columns and
3797+ // rows -- record the whole span, not one cell.
3798+ neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, mi_w, mi_h, mode);
3729 Ok(mode)3799 Ok(mode)
3730 }3800 }
3731 3801
@@ -5309,6 +5379,16 @@ fn decode_leaf8(
5309 left_mode = pmode;5379 left_mode = pmode;
5310 }5380 }
5311 }5381 }
5382+ // The mi-exact neighbour wins over both the coarse 16x16 slot and
5383+ // `prev_leaf`: one 16x16 slot cannot hold the two different modes its
5384+ // two 8x8 (or four 4x4) columns leave behind, so a leaf whose above/left
5385+ // neighbour sits in a split cell read the wrong `kf_y_mode` row.
5386+ if let Some(m) = neighbours.mode_above_mi(leaf_mi.0, leaf_mi.1) {
5387+ above_mode = m;
5388+ }
5389+ if let Some(m) = neighbours.mode_left_mi(leaf_mi.0, leaf_mi.1) {
5390+ left_mode = m;
5391+ }
5312 // See [`PlaneBuf::reconstruct`]'s doc: chroma stays `false` (exact) until5392 // See [`PlaneBuf::reconstruct`]'s doc: chroma stays `false` (exact) until
5313 // a smooth/paeth `uv_mode` can reach decode at all.5393 // a smooth/paeth `uv_mode` can reach decode at all.
5314 let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode);5394 let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode);
@@ -5596,11 +5676,13 @@ fn decode_leaf8(
5596 }5676 }
5597 neighbours.fill_skip_grid(leaf_mi, 2, skip);5677 neighbours.fill_skip_grid(leaf_mi, 2, skip);
5598 neighbours.fill_lf_grid(leaf_mi, 2, 4, 0);5678 neighbours.fill_lf_grid(leaf_mi, 2, 4, 0);
5679+ neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode);
5599 return Ok(mode);5680 return Ok(mode);
5600 }5681 }
5601 neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]);5682 neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]);
5602 neighbours.fill_skip_grid(leaf_mi, 2, skip);5683 neighbours.fill_skip_grid(leaf_mi, 2, skip);
5603 neighbours.fill_lf_grid(leaf_mi, 2, 8, 0);5684 neighbours.fill_lf_grid(leaf_mi, 2, 8, 0);
5685+ neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode);
5604 Ok(mode)5686 Ok(mode)
5605 }5687 }
5606 5688
crates/ec-av1/src/encode.rsmodified+96 -35
@@ -1020,7 +1020,7 @@ pub(crate) struct Reach {
1020 /// it. Indexed by the block's position in the superblock, in blocks of its own1020 /// it. Indexed by the block's position in the superblock, in blocks of its own
1021 /// size, as `row * (128 / side) + col` (`Reach::table_stride`) -- libaom's1021 /// size, as `row * (128 / side) + col` (`Reach::table_stride`) -- libaom's
1022 /// 128, not this crate's 64 superblock -- bit by bit from the low end.1022 /// 128, not this crate's 64 superblock -- bit by bit from the low end.
1023-const HAS_TOP_RIGHT: [&[u8]; 3] = [1023+const HAS_TOP_RIGHT: [&[u8]; 4] = [
1024 &[255, 85, 119, 85, 127, 85, 119, 85],1024 &[255, 85, 119, 85, 127, 85, 119, 85],
1025 &[95, 87],1025 &[95, 87],
1026 // has_tr_8x8 (libaom av1/common/reconintra.c) -- table_stride(8) == 161026 // has_tr_8x8 (libaom av1/common/reconintra.c) -- table_stride(8) == 16
@@ -1029,11 +1029,28 @@ const HAS_TOP_RIGHT: [&[u8]; 3] = [
1029 255, 255, 85, 85, 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85, 255, 127, 85, 85,1029 255, 255, 85, 85, 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85, 255, 127, 85, 85,
1030 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85,1030 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85,
1031 ],1031 ],
1032+
1033+ // has_tr_4x4 (libaom av1/common/reconintra.c, transcribed verbatim) --
1034+ // table_stride(4) == 32 (128 / 4), so 32 bits per row, 32 rows, 128
1035+ // bytes. Sub-8x8 leaves (lane-sub8) are the only blocks that reach here
1036+ // with side 4 outside a TX4 split; without their own row they clamped
1037+ // into the 8x8 table and read above-right samples the decoder has not
1038+ // written yet (all-zero pixels in the bottom-right triangle of a
1039+ // directional 4x4 leaf).
1040+ &[
1041+ 255, 255, 255, 255, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85, 127, 127, 127, 127,
1042+ 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85, 255, 127, 255, 127, 85, 85, 85, 85,
1043+ 119, 119, 119, 119, 85, 85, 85, 85, 127, 127, 127, 127, 85, 85, 85, 85, 119, 119, 119, 119,
1044+ 85, 85, 85, 85, 255, 255, 255, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85,
1045+ 127, 127, 127, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85, 255, 127, 255, 127,
1046+ 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85, 127, 127, 127, 127, 85, 85, 85, 85,
1047+ 119, 119, 119, 119, 85, 85, 85, 85,
1048+ ],
1032 ];1049 ];
1033 1050
1034 /// `has_bl_16x16` / `has_bl_32x32`: the same, for the block below and to the1051 /// `has_bl_16x16` / `has_bl_32x32`: the same, for the block below and to the
1035 /// left.1052 /// left.
1036-const HAS_BOTTOM_LEFT: [&[u8]; 3] = [1053+const HAS_BOTTOM_LEFT: [&[u8]; 4] = [
1037 &[84, 16, 84, 0, 84, 16, 84, 0],1054 &[84, 16, 84, 0, 84, 16, 84, 0],
1038 &[4, 4],1055 &[4, 4],
1039 // has_bl_8x8 (libaom av1/common/reconintra.c)1056 // has_bl_8x8 (libaom av1/common/reconintra.c)
@@ -1041,6 +1058,16 @@ const HAS_BOTTOM_LEFT: [&[u8]; 3] = [
1041 84, 85, 16, 17, 84, 85, 0, 1, 84, 85, 16, 17, 84, 85, 0, 0, 84, 85, 16, 17, 84, 85, 0, 1,1058 84, 85, 16, 17, 84, 85, 0, 1, 84, 85, 16, 17, 84, 85, 0, 0, 84, 85, 16, 17, 84, 85, 0, 1,
1042 84, 85, 16, 17, 84, 85, 0, 0,1059 84, 85, 16, 17, 84, 85, 0, 0,
1043 ],1060 ],
1061+
1062+ // has_bl_4x4 (same source, same order).
1063+ &[
1064+ 84, 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0, 1, 1, 1, 84, 85, 85, 85, 16, 17, 17, 17,
1065+ 84, 85, 85, 85, 0, 0, 1, 0, 84, 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0, 1, 1, 1, 84,
1066+ 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0, 0, 0, 0, 84, 85, 85, 85, 16, 17, 17, 17, 84,
1067+ 85, 85, 85, 0, 1, 1, 1, 84, 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0, 0, 1, 0, 84, 85,
1068+ 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0, 1, 1, 1, 84, 85, 85, 85, 16, 17, 17, 17, 84, 85,
1069+ 85, 85, 0, 0, 0, 0,
1070+ ],
1044 ];1071 ];
1045 1072
1046 /// `has_tr_32x16`/`has_tr_16x32` (libaom `reconintra.c`) -- lane-intradisp1073 /// `has_tr_32x16`/`has_tr_16x32` (libaom `reconintra.c`) -- lane-intradisp
@@ -1051,6 +1078,34 @@ const HAS_TOP_RIGHT_RECT: [&[u8]; 2] = [&[15, 5, 7, 5], &[255, 119, 127, 119]];
1051 /// `has_bl_32x16`/`has_bl_16x32`, same source, same order.1078 /// `has_bl_32x16`/`has_bl_16x32`, same source, same order.
1052 const HAS_BOTTOM_LEFT_RECT: [&[u8]; 2] = [&[78, 14, 78, 14], &[16, 0, 16, 0]];1079 const HAS_BOTTOM_LEFT_RECT: [&[u8]; 2] = [&[78, 14, 78, 14], &[16, 0, 16, 0]];
1053 1080
1081+/// libaom `reconintra.c`'s `has_tr_*`/`has_bl_*` for the four rectangular
1082+/// block sizes this decoder codes, transcribed verbatim (lane-rectx r4).
1083+/// The 16x8/8x16 rows are what the 32x16/16x32 pair above cannot stand in
1084+/// for: they are 16 bytes, not 4, and their bit pattern is a different
1085+/// coding order entirely -- reading the 16x32 table for a 16x8 block gave
1086+/// the wrong bottom-left availability and a directional (D203) strip whose
1087+/// prediction diverged exactly where it starts reading below its own height.
1088+/// `has_tr_vert_tables`/`has_bl_vert_tables` hold the SAME pointers as these
1089+/// for every W<H entry, and are NULL for W>H (no VERT_A/B ever produces one),
1090+/// so the partition type does not select a different table at these sizes.
1091+fn has_tr_rect_table(bw: usize, bh: usize) -> &'static [u8] {
1092+ match (bw, bh) {
1093+ (16, 8) => &[255, 0, 85, 0, 119, 0, 85, 0, 127, 0, 85, 0, 119, 0, 85, 0],
1094+ (8, 16) => &[255, 255, 119, 119, 127, 127, 119, 119, 255, 127, 119, 119, 127, 127, 119, 119],
1095+ (32, 16) => &[15, 5, 7, 5],
1096+ _ => &[255, 119, 127, 119],
1097+ }
1098+}
1099+
1100+fn has_bl_rect_table(bw: usize, bh: usize) -> &'static [u8] {
1101+ match (bw, bh) {
1102+ (16, 8) => &[254, 84, 254, 16, 254, 84, 254, 0, 254, 84, 254, 16, 254, 84, 254, 0],
1103+ (8, 16) => &[16, 17, 0, 1, 16, 17, 0, 0, 16, 17, 0, 1, 16, 17, 0, 0],
1104+ (32, 16) => &[78, 14, 78, 14],
1105+ _ => &[16, 0, 16, 0],
1106+ }
1107+}
1108+
1054 impl Reach {1109 impl Reach {
1055 /// What a block of `side` samples at `(x, y)` may read past its own edges,1110 /// What a block of `side` samples at `(x, y)` may read past its own edges,
1056 /// in a frame of `width` by `height`.1111 /// in a frame of `width` by `height`.
@@ -1080,37 +1135,49 @@ impl Reach {
1080 }1135 }
1081 }1136 }
1082 1137
1083- fn position_rect(bw: usize, bh: usize, x: usize, y: usize) -> (usize, usize, usize, usize) {1138+ /// libaom `has_top_right` at block granularity (`row_off`/`col_off` 0,
1084- (1139+ /// the transform covering the whole block, luma): everything before the
1085- (y % SUPERBLOCK) / bh,1140+ /// table lookup is that function's own early-exit ladder.
1086- (x % SUPERBLOCK) / bw,
1087- SUPERBLOCK / bh,
1088- SUPERBLOCK / bw,
1089- )
1090- }
1091-
1092 fn top_right_rect(bw: usize, bh: usize, x: usize, y: usize) -> bool {1141 fn top_right_rect(bw: usize, bh: usize, x: usize, y: usize) -> bool {
1093- let (row, col, _per_row, per_col) = Self::position_rect(bw, bh, x, y);1142+ const SB_MI: usize = SUPERBLOCK / 4;
1094- if row == 0 {1143+ let (mi_row, mi_col) = (y / 4, x / 4);
1144+ let bw_log2 = (bw / 4).trailing_zeros() as usize;
1145+ let bh_log2 = (bh / 4).trailing_zeros() as usize;
1146+ let blk_row = (mi_row & (SB_MI - 1)) >> bh_log2;
1147+ let blk_col = (mi_col & (SB_MI - 1)) >> bw_log2;
1148+ // Top row of the superblock: the top-right pixels are in the (already
1149+ // decoded) superblock above.
1150+ if blk_row == 0 {
1095 return true;1151 return true;
1096 }1152 }
1097- if col + 1 == per_col {1153+ // Rightmost column: they fall in the superblock to the right.
1154+ if ((blk_col + 1) << bw_log2) >= SB_MI {
1098 return false;1155 return false;
1099 }1156 }
1100- let table = HAS_TOP_RIGHT_RECT[usize::from(bw == 16)];1157+ let table = has_tr_rect_table(bw, bh);
1101- Self::bit(table, (row * Self::table_stride(bw) + col) % (table.len() * 8))1158+ Self::bit(table, (blk_row << (4 - bw_log2)) + blk_col)
1102 }1159 }
1103 1160
1161+ /// libaom `has_bottom_left`, same granularity as [`Self::top_right_rect`].
1104 fn bottom_left_rect(bw: usize, bh: usize, x: usize, y: usize) -> bool {1162 fn bottom_left_rect(bw: usize, bh: usize, x: usize, y: usize) -> bool {
1105- let (row, col, per_row, _per_col) = Self::position_rect(bw, bh, x, y);1163+ const SB_MI: usize = SUPERBLOCK / 4;
1106- if col == 0 {1164+ let (mi_row, mi_col) = (y / 4, x / 4);
1107- return row * bh + bh < SUPERBLOCK;1165+ let bw_log2 = (bw / 4).trailing_zeros() as usize;
1108- }1166+ let bh_log2 = (bh / 4).trailing_zeros() as usize;
1109- if row + 1 == per_row {1167+ let blk_row = (mi_row & (SB_MI - 1)) >> bh_log2;
1168+ let blk_col = (mi_col & (SB_MI - 1)) >> bw_log2;
1169+ // Leftmost column of the superblock: the bottom-left pixels are in
1170+ // the left superblock iff they stay inside its height.
1171+ if blk_col == 0 {
1172+ return (blk_row << bh_log2) + (bh / 4) < SB_MI;
1173+ }
1174+ // Bottom row (and not the leftmost column): they fall in the
1175+ // superblock below, which is not decoded yet.
1176+ if ((blk_row + 1) << bh_log2) >= SB_MI {
1110 return false;1177 return false;
1111 }1178 }
1112- let table = HAS_BOTTOM_LEFT_RECT[usize::from(bw == 16)];1179+ let table = has_bl_rect_table(bw, bh);
1113- Self::bit(table, (row * Self::table_stride(bw) + col) % (table.len() * 8))1180+ Self::bit(table, (blk_row << (4 - bw_log2)) + blk_col)
1114 }1181 }
1115 1182
1116 /// Neither, which is all a mode that reads no further than its own edges1183 /// Neither, which is all a mode that reads no further than its own edges
@@ -1134,17 +1201,9 @@ impl Reach {
1134 if col + 1 == per_side {1201 if col + 1 == per_side {
1135 return false;1202 return false;
1136 }1203 }
1137- // corner-cut: `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` only carry dedicated1204+ // lane-sub8 r4: side 4 now has its own `has_tr_4x4`/`has_bl_4x4`
1138- // rows for side 8/16/32/64 (libaom's own `has_tr_8x8`/`has_tr_16x16`1205+ // row (the corner-cut this comment used to describe); sides 16/32/64
1139- // families); a `TxMode::Select` TX4 transform unit reaches here with1206+ // share row 0 exactly as libaom's tables repeat.
1140- // side 4, whose real table (`has_tr_4x4`, 128 bytes) was never
1141- // transcribed. Clamping into the 16/32 table's own bit range keeps
1142- // this in-bounds and correct at every position that table already
1143- // covers periodically -- wrong only at the small remainder past its
1144- // length, a reach over-/under-estimate (extra or missing above-right
1145- // reference pixels), never a stream desync (Reach never gates a
1146- // symbol read). Ceiling: transcribe `has_tr_4x4`/`has_bl_4x4`
1147- // verbatim and give TX4 its own `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` row.
1148 let table = HAS_TOP_RIGHT[Self::table(side)];1207 let table = HAS_TOP_RIGHT[Self::table(side)];
1149 Self::bit(1208 Self::bit(
1150 table,1209 table,
@@ -1164,7 +1223,7 @@ impl Reach {
1164 if row + 1 == per_side {1223 if row + 1 == per_side {
1165 return false;1224 return false;
1166 }1225 }
1167- // corner-cut: see `top_right`'s note -- same clamp, same ceiling.1226+ // See `top_right`'s note.
1168 let table = HAS_BOTTOM_LEFT[Self::table(side)];1227 let table = HAS_BOTTOM_LEFT[Self::table(side)];
1169 Self::bit(1228 Self::bit(
1170 table,1229 table,
@@ -1200,6 +1259,8 @@ impl Reach {
1200 11259 1
1201 } else if side == 8 {1260 } else if side == 8 {
1202 21261 2
1262+ } else if side == 4 {
1263+ 3
1203 } else {1264 } else {
1204 01265 0
1205 }1266 }
crates/ec-av1/src/stream.rsmodified+7 -2
@@ -5370,7 +5370,7 @@ mod tests {
5370 "-f",5370 "-f",
5371 "lavfi",5371 "lavfi",
5372 "-i",5372 "-i",
5373- &format!("mandelbrot=size={width}x{height}:start_x=0.4"),5373+ &format!("mandelbrot=size={width}x{height}:start_x=-0.6"),
5374 "-pix_fmt",5374 "-pix_fmt",
5375 "yuv420p",5375 "yuv420p",
5376 "-t",5376 "-t",
@@ -5393,7 +5393,7 @@ mod tests {
5393 "--codec=av1",5393 "--codec=av1",
5394 "--passes=1",5394 "--passes=1",
5395 "--end-usage=q",5395 "--end-usage=q",
5396- "--cq-level=24",5396+ "--cq-level=16",
5397 "--cpu-used=4",5397 "--cpu-used=4",
5398 "--threads=1",5398 "--threads=1",
5399 "--row-mt=0",5399 "--row-mt=0",
@@ -5403,6 +5403,11 @@ mod tests {
5403 "--enable-ab-partitions=0",5403 "--enable-ab-partitions=0",
5404 "--enable-1to4-partitions=0",5404 "--enable-1to4-partitions=0",
5405 "--enable-tx-size-search=0",5405 "--enable-tx-size-search=0",
5406+ // Filter intra on a strip is a separate, still-refused
5407+ // predictor (`filter intra on a HORZ/VERT strip`); this
5408+ // gate is about the rect RESIDUAL path, so it is off here
5409+ // and covered by the square filter-intra gate instead.
5410+ "--enable-filter-intra=0",
5406 "--reduced-tx-type-set=1",5411 "--reduced-tx-type-set=1",
5407 "--min-partition-size=8",5412 "--min-partition-size=8",
5408 "--max-partition-size=32",5413 "--max-partition-size=32",