forge
Explore
Log in

Don't you dare go Hollow

A transform unit inside a block is not a block at that position

Browse files at this commit

b7ad53c810fc0775fe71b6244479ba8e0f1d0b6cAhmet Kaan GümüşParent: 851c4fb
  • fix
    • a transform unit inside a block is not a block at that position
Show full message
fix(av1): a transform unit inside a block is not a block at that position

`Reach::of(tx, tu_px, tu_py, ..)` answered the standalone-block
has_top_right/has_bottom_left tables for every transform unit of a
split-transform SQUARE block (decode_block's multi-TU branch and
decode_leaf8's 2x2 4x4 grid), where libaom's reconintra.c takes its
TU branches instead: bottom-left is 0 outright for col_off > 0, and
1 while the unit below-left is still inside the block. The rect path
(decode_rect_split, lane-rectsplit r1) already had the right rule
inline; it now shares it as Reach::of_tu.

Measured on the seed-46 gate stream: the TL 16x16 (D203, VERT_A,
tx depth 1) at px(128,64) has four 8x8 luma TUs; aomdec reports
n_bl=8,0,8,0 for them, ours granted bottom-left to the col_off=8
units and reconstructed a 25-pixel diagonal wedge at rows 68..71
that then propagated over the whole last superblock (2242 samples).

a_real_aomenc_stream_with_filter_intra_on_a_horz_vert_strip_decodes_pixel_exact
RED -> GREEN. New enumeration test
encode::tests::of_tu_matches_libaom_has_top_right_and_has_bottom_left_per_unit
checks of_tu against an MI-unit transcription of both libaom functions
over every (block, tx, row_off, col_off, block-level answer).
crates/ec-av1/src/decode.rsmodified+15 -16
@@ -4596,20 +4596,7 @@ fn decode_rect_split(
4596 // (`row_off + tx_high < plane_bh`) they are the left4596 // (`row_off + tx_high < plane_bh`) they are the left
4597 // neighbour's, already reconstructed; the last row falls back4597 // neighbour's, already reconstructed; the last row falls back
4598 // to the block-level answer.4598 // to the block-level answer.
4599- let tu_reach = Reach {4599+ let tu_reach = Reach::of_tu(bw, bh, col_off, row_off, tx, block_reach);
4600- above_right: if col_off + tx < bw {
4601- true
4602- } else {
4603- row_off == 0 && block_reach.above_right
4604- },
4605- below_left: if col_off > 0 {
4606- false
4607- } else if row_off + tx < bh {
4608- true
4609- } else {
4610- block_reach.below_left
4611- },
4612- };
4613 // The unit's own window on the block's palette prediction.4600 // The unit's own window on the block's palette prediction.
4614 if let Some(buf) = &m.palette_y {4601 if let Some(buf) = &m.palette_y {
4615 let tu_buf: Vec<u16> = (0..tx)4602 let tu_buf: Vec<u16> = (0..tx)
@@ -7075,7 +7062,19 @@ fn decode_block(
7075 let tu_px = px + tu_col * logical_tx;7062 let tu_px = px + tu_col * logical_tx;
7076 let tu_py = py + tu_row * logical_tx;7063 let tu_py = py + tu_row * logical_tx;
7077 let tu_around = neighbours.around_mi(tu_mi, logical_tx)[0];7064 let tu_around = neighbours.around_mi(tu_mi, logical_tx)[0];
7078- let tu_reach = Reach::of(logical_tx, tu_px, tu_py, y.width, y.height);7065+ // A transform unit inside a block is NOT a block at that
7066+ // position (lane-palette2 r12): `Reach::of` granted the
7067+ // top-right 8x8 unit of a 16x16 D203 block bottom-left
7068+ // pixels libaom refuses outright (`col_off > 0`), which is
7069+ // the diagonal wedge seed 46 reconstructed wrong.
7070+ let tu_reach = Reach::of_tu(
7071+ side,
7072+ side,
7073+ tu_col * logical_tx,
7074+ tu_row * logical_tx,
7075+ logical_tx,
7076+ reach,
7077+ );
7079 // This transform unit's own bsize (`logical_tx`) is smaller7078 // This transform unit's own bsize (`logical_tx`) is smaller
7080 // than the block it sits in (`side`), so `txb_skip_ctx` is7079 // than the block it sits in (`side`), so `txb_skip_ctx` is
7081 // the neighbour-magnitude table, not the lone-TU 0 (spec7080 // the neighbour-magnitude table, not the lone-TU 0 (spec
@@ -7443,7 +7442,7 @@ fn decode_leaf8(
7443 let tu_px = px + tu_col * 4;7442 let tu_px = px + tu_col * 4;
7444 let tu_py = py + tu_row * 4;7443 let tu_py = py + tu_row * 4;
7445 let tu_around = neighbours.around_mi(tu_mi, 4)[0];7444 let tu_around = neighbours.around_mi(tu_mi, 4)[0];
7446- let tu_reach = Reach::of(4, tu_px, tu_py, y.width, y.height);7445+ let tu_reach = Reach::of_tu(8, 8, tu_col * 4, tu_row * 4, 4, reach);
7447 let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1);7446 let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1);
7448 let tu_grid = read_plane(7447 let tu_grid = read_plane(
7449 dec,7448 dec,
crates/ec-av1/src/encode.rsmodified+99 -1
@@ -1008,7 +1008,7 @@ struct At {
1008 /// block whose transform covers it whole, whether the samples above its right1008 /// block whose transform covers it whole, whether the samples above its right
1009 /// (or below its left) are decoded depends only on where the block sits inside1009 /// (or below its left) are decoded depends only on where the block sits inside
1010 /// its 64x64 superblock, which is a pinned table per block size.1010 /// its 64x64 superblock, which is a pinned table per block size.
1011-#[derive(Clone, Copy)]1011+#[derive(Clone, Copy, Debug)]
1012 pub(crate) struct Reach {1012 pub(crate) struct Reach {
1013 pub(crate) above_right: bool,1013 pub(crate) above_right: bool,
1014 pub(crate) below_left: bool,1014 pub(crate) below_left: bool,
@@ -1164,6 +1164,44 @@ fn has_bl_rect_table(bw: usize, bh: usize) -> &'static [u8] {
1164 }1164 }
1165 1165
1166 impl Reach {1166 impl Reach {
1167+ /// `has_top_right`/`has_bottom_left` (libaom `reconintra.c`) for a
1168+ /// transform unit at `(col_off, row_off)` INSIDE a `bw`x`bh` block whose
1169+ /// own answer is `block` -- a different rule from the standalone-block
1170+ /// tables, because a unit's neighbours are mostly its own block's
1171+ /// already-reconstructed units:
1172+ /// * top-right: available while the unit to the right is still inside
1173+ /// the block (`col_off + tx < bw`); otherwise only the block's top row
1174+ /// of units reaches past it, through the block-level answer
1175+ /// (libaom's `row_off > 0` branch returns exactly
1176+ /// `col_off + txw < plane_bw`).
1177+ /// * bottom-left: a unit outside the block's left column has none
1178+ /// (libaom returns 0 for `col_off > 0`); inside the block
1179+ /// (`row_off + tx < bh`) they are the left unit's, already
1180+ /// reconstructed; the bottom row falls back to the block-level answer.
1181+ pub(crate) fn of_tu(
1182+ bw: usize,
1183+ bh: usize,
1184+ col_off: usize,
1185+ row_off: usize,
1186+ tx: usize,
1187+ block: Self,
1188+ ) -> Self {
1189+ Self {
1190+ above_right: if col_off + tx < bw {
1191+ true
1192+ } else {
1193+ row_off == 0 && block.above_right
1194+ },
1195+ below_left: if col_off > 0 {
1196+ false
1197+ } else if row_off + tx < bh {
1198+ true
1199+ } else {
1200+ block.below_left
1201+ },
1202+ }
1203+ }
1204+
1167 /// What a block of `side` samples at `(x, y)` may read past its own edges,1205 /// What a block of `side` samples at `(x, y)` may read past its own edges,
1168 /// in a frame of `width` by `height`.1206 /// in a frame of `width` by `height`.
1169 pub(crate) fn of(side: usize, x: usize, y: usize, width: usize, height: usize) -> Self {1207 pub(crate) fn of(side: usize, x: usize, y: usize, width: usize, height: usize) -> Self {
@@ -3712,6 +3750,66 @@ mod tests {
3712 /// swept in r6 produced a decodable 16x16-level VERT_B stream (every3750 /// swept in r6 produced a decodable 16x16-level VERT_B stream (every
3713 /// stream that partitions that small first hits the still-refused3751 /// stream that partitions that small first hits the still-refused
3714 /// HORZ_A/HORZ_B/VERT_A-below-16 or coded-rect-strip-below-16 arms).3752 /// HORZ_A/HORZ_B/VERT_A-below-16 or coded-rect-strip-below-16 arms).
3753+ /// [`Reach::of_tu`] against a from-scratch transcription of libaom's
3754+ /// `has_top_right`/`has_bottom_left` TU branches (`reconintra.c`, luma,
3755+ /// `ss_x = ss_y = 0`), which work in MI units where `of_tu` works in
3756+ /// pixels -- lane-palette2 r12: the square multi-TU path used the
3757+ /// standalone-block tables instead of this rule and granted a 16x16
3758+ /// D203 block's top-right 8x8 unit bottom-left pixels libaom refuses.
3759+ #[test]
3760+ fn of_tu_matches_libaom_has_top_right_and_has_bottom_left_per_unit() {
3761+ // libaom, in MI units: bw_unit/bh_unit = block size / 4,
3762+ // *_count_unit = tx size / 4.
3763+ fn libaom_tr(bw: usize, bh: usize, col_off: usize, row_off: usize, tx: usize, blk: bool) -> bool {
3764+ let (plane_bw_unit, count) = (bw / 4, tx / 4);
3765+ let (col_off_u, row_off_u) = (col_off / 4, row_off / 4);
3766+ let _ = bh;
3767+ if row_off_u > 0 {
3768+ col_off_u + count < plane_bw_unit
3769+ } else if col_off_u + count < plane_bw_unit {
3770+ true
3771+ } else {
3772+ blk
3773+ }
3774+ }
3775+ fn libaom_bl(bw: usize, bh: usize, col_off: usize, row_off: usize, tx: usize, blk: bool) -> bool {
3776+ let (plane_bh_unit, count) = (bh / 4, tx / 4);
3777+ let (col_off_u, row_off_u) = (col_off / 4, row_off / 4);
3778+ let _ = bw;
3779+ if col_off_u > 0 {
3780+ false
3781+ } else if row_off_u + count < plane_bh_unit {
3782+ true
3783+ } else {
3784+ blk
3785+ }
3786+ }
3787+ for &(bw, bh) in &[(8, 8), (16, 16), (32, 32), (64, 64), (16, 8), (8, 16), (32, 16), (16, 32), (64, 32), (32, 64)] {
3788+ for &tx in &[4usize, 8, 16, 32] {
3789+ if tx > bw.min(bh) {
3790+ continue;
3791+ }
3792+ for row_off in (0..bh).step_by(tx) {
3793+ for col_off in (0..bw).step_by(tx) {
3794+ for &blk in &[Reach { above_right: false, below_left: false }, Reach { above_right: true, below_left: true }] {
3795+ let got = Reach::of_tu(bw, bh, col_off, row_off, tx, blk);
3796+ assert_eq!(
3797+ got.above_right,
3798+ libaom_tr(bw, bh, col_off, row_off, tx, blk.above_right),
3799+ "above_right bw={bw} bh={bh} tx={tx} row_off={row_off} col_off={col_off} blk={blk:?}"
3800+ );
3801+ assert_eq!(
3802+ got.below_left,
3803+ libaom_bl(bw, bh, col_off, row_off, tx, blk.below_left),
3804+ "below_left bw={bw} bh={bh} tx={tx} row_off={row_off} col_off={col_off} blk={blk:?}"
3805+ );
3806+ }
3807+ }
3808+ }
3809+ }
3810+ }
3811+ }
3812+
3715 #[test]3813 #[test]
3716 fn vert_ab_partition_flips_below_left_for_the_top_right_8x8() {3814 fn vert_ab_partition_flips_below_left_for_the_top_right_8x8() {
3717 let (width, height) = (SUPERBLOCK * 3, SUPERBLOCK * 3);3815 let (width, height) = (SUPERBLOCK * 3, SUPERBLOCK * 3);