forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-coefbuf' (af6174b0): coefficient context frame stores u8 magnitudes, per-intra-unit residual grid clone removed; byte-exact, -0.44% instructions

Browse files at this commit

59bce733d2f7a2a9c59533019dbcc0bc77b7fe03Ahmet Kaan GümüşParent: 38d2f82ad3e7b0
crates/ec-av1/src/decode.rsmodified+46 -45
@@ -5352,8 +5352,8 @@ fn eob_coeff_ctx(scan_idx: usize, area: usize) -> usize {
53525352 /// this used to run five times per coefficient. Nothing writes a padding
53535353 /// cell, so the values are the same zeros the test returned.
53545354 #[inline]
5355-fn neighbour(grid: &[i32], stride: usize, row: usize, col: usize) -> i32 {
5356- grid[row * stride + col]
5355+fn neighbour(grid: &[u8], stride: usize, row: usize, col: usize) -> i32 {
5356+ grid[row * stride + col] as i32
53575357 }
53585358
53595359 /// spec 5.11.39's three `TxClass`es (`tx_type_to_class`, libaom
@@ -5392,7 +5392,7 @@ fn nz_map_ctx_offset_1d(i: usize) -> usize {
53925392 }
53935393
53945394 fn base_ctx(
5395- grid: &[i32],
5395+ grid: &[u8],
53965396 stride: usize,
53975397 row: usize,
53985398 col: usize,
@@ -5417,7 +5417,7 @@ fn base_ctx(
54175417 };
54185418 let mag: i32 = offsets
54195419 .iter()
5420- .map(|&(dr, dc)| neighbour(grid, stride, row + dr, col + dc).abs().min(3))
5420+ .map(|&(dr, dc)| neighbour(grid, stride, row + dr, col + dc).min(3))
54215421 .sum();
54225422 let ctx = ((mag + 1) >> 1).min(4) as usize;
54235423 match class {
@@ -5460,14 +5460,14 @@ fn base_ctx(
54605460 }
54615461 }
54625462
5463-fn br_ctx(grid: &[i32], stride: usize, row: usize, col: usize, class: TxClass) -> usize {
5463+fn br_ctx(grid: &[u8], stride: usize, row: usize, col: usize, class: TxClass) -> usize {
54645464 let extra = match class {
5465- TxClass::TwoD => neighbour(grid, stride, row + 1, col + 1).abs(),
5466- TxClass::Horiz => neighbour(grid, stride, row, col + 2).abs(),
5467- TxClass::Vert => neighbour(grid, stride, row + 2, col).abs(),
5465+ TxClass::TwoD => neighbour(grid, stride, row + 1, col + 1),
5466+ TxClass::Horiz => neighbour(grid, stride, row, col + 2),
5467+ TxClass::Vert => neighbour(grid, stride, row + 2, col),
54685468 };
5469- let mag = neighbour(grid, stride, row + 1, col).abs()
5470- + neighbour(grid, stride, row, col + 1).abs()
5469+ let mag = neighbour(grid, stride, row + 1, col)
5470+ + neighbour(grid, stride, row, col + 1)
54715471 + extra;
54725472 let mag = (((mag + 1) >> 1).min(6)) as usize;
54735473 if row == 0 && col == 0 {
@@ -5648,8 +5648,8 @@ const SCAN_4X16: [u16; 64] = [
56485648 /// (`stride = w + 4`, four spare rows), so the edge test is the zeros the
56495649 /// padding already holds.
56505650 #[inline]
5651-fn neighbour_rect(grid: &[i32], stride: usize, row: usize, col: usize) -> i32 {
5652- grid[row * stride + col]
5651+fn neighbour_rect(grid: &[u8], stride: usize, row: usize, col: usize) -> i32 {
5652+ grid[row * stride + col] as i32
56535653 }
56545654
56555655 /// [`base_ctx`]'s `TxClass::TwoD` arm widened to `(w, h)` (lane-rectwire):
@@ -5659,7 +5659,7 @@ fn neighbour_rect(grid: &[i32], stride: usize, row: usize, col: usize) -> i32 {
56595659 /// `V_DCT`/`H_DCT` -- geometrically impossible at the two size pairs
56605660 /// [`decode_block_rect`] codes (see that function's own doc comment).
56615661 fn base_ctx_rect(
5662- grid: &[i32],
5662+ grid: &[u8],
56635663 stride: usize,
56645664 w: usize,
56655665 h: usize,
@@ -5679,7 +5679,7 @@ fn base_ctx_rect(
56795679 };
56805680 let mag: i32 = offsets
56815681 .iter()
5682- .map(|&(dr, dc)| neighbour_rect(grid, stride, row + dr, col + dc).abs().min(3))
5682+ .map(|&(dr, dc)| neighbour_rect(grid, stride, row + dr, col + dc).min(3))
56835683 .sum();
56845684 let ctx = ((mag + 1) >> 1).min(4) as usize;
56855685 match class {
@@ -5704,19 +5704,19 @@ fn base_ctx_rect(
57045704 /// neighbour-offset math itself is shape-independent, only the boundary
57055705 /// clamp needs the real bound in each axis.
57065706 fn br_ctx_rect(
5707- grid: &[i32],
5707+ grid: &[u8],
57085708 stride: usize,
57095709 row: usize,
57105710 col: usize,
57115711 class: TxClass,
57125712 ) -> usize {
57135713 let extra = match class {
5714- TxClass::TwoD => neighbour_rect(grid, stride, row + 1, col + 1).abs(),
5715- TxClass::Horiz => neighbour_rect(grid, stride, row, col + 2).abs(),
5716- TxClass::Vert => neighbour_rect(grid, stride, row + 2, col).abs(),
5714+ TxClass::TwoD => neighbour_rect(grid, stride, row + 1, col + 1),
5715+ TxClass::Horiz => neighbour_rect(grid, stride, row, col + 2),
5716+ TxClass::Vert => neighbour_rect(grid, stride, row + 2, col),
57175717 };
5718- let mag = neighbour_rect(grid, stride, row + 1, col).abs()
5719- + neighbour_rect(grid, stride, row, col + 1).abs()
5718+ let mag = neighbour_rect(grid, stride, row + 1, col)
5719+ + neighbour_rect(grid, stride, row, col + 1)
57205720 + extra;
57215721 let mag = (((mag + 1) >> 1).min(6)) as usize;
57225722 if row == 0 && col == 0 {
@@ -6000,7 +6000,7 @@ fn read_coeffs(
60006000 // `row, col <= side - 1` -- stays inside `(side + 4) * (side + 4)`, at
60016001 // most `36 * 36`. Only `col < side`, `row < side` cells are ever written.
60026002 let stride = side + 4;
6003- let mut levels_buf = [0i32; 36 * 36];
6003+ let mut levels_buf = [0u8; 36 * 36];
60046004 let levels = &mut levels_buf[..stride * stride];
60056005 for scan_idx in (0..eob).rev() {
60066006 let pos = scan[scan_idx] as usize;
@@ -6069,7 +6069,13 @@ fn read_coeffs(
60696069 let (rng, _) = dec.debug_state();
60706070 eprintln!("EC_COEFF_STEP tag=base_eob level={level} rng={rng}");
60716071 }
6072- levels[row * stride + col] = level;
6072+ // Every coded level here is a magnitude in `0..=15` (base <= 3 plus
6073+ // at most `COEFF_BASE_RANGE` from the `br` loop; the Golomb tail is
6074+ // added in the sign pass below, straight into `grid`), so the context
6075+ // frame is bytes -- the five-tap sum in [`base_ctx`]/[`br_ctx`] is a
6076+ // byte gather (lane-coefbuf).
6077+ debug_assert!(level >= 0 && level <= 15, "level {level} past the u8 frame");
6078+ levels[row * stride + col] = level as u8;
60736079 }
60746080 if ec_trace_coeff {
60756081 let (rng, _) = dec.debug_state();
@@ -6078,7 +6084,7 @@ fn read_coeffs(
60786084
60796085 for (c, &pos) in scan[..eob].iter().enumerate() {
60806086 let pos = pos as usize;
6081- let level = levels[(pos >> sh) * stride + (pos & col_mask)];
6087+ let level = levels[(pos >> sh) * stride + (pos & col_mask)] as i32;
60826088 if level == 0 {
60836089 continue;
60846090 }
@@ -6095,7 +6101,7 @@ fn read_coeffs(
60956101 let (rng, _) = dec.debug_state();
60966102 eprintln!("EC_COEFF_STEP tag=sign c={c} sign={} rng={rng}", negative as i32);
60976103 }
6098- let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL {
6104+ let level = if level > MAX_BR_LEVEL {
60996105 let g = read_golomb(dec)?;
61006106 if trace {
61016107 eprintln!("TRACE golomb pos={pos} value={g}");
@@ -6187,14 +6193,15 @@ fn read_coeffs_rect(
61876193 let n = stride * (h + 4);
61886194 debug_assert!(w.is_power_of_two() && n <= 36 * 36);
61896195 let (mut b96, mut b256, mut b768);
6190- let levels: &mut [i32] = if n <= 96 {
6191- b96 = [0i32; 96];
6196+ // Byte levels, same `0..=15` bound [`read_coeffs`] states.
6197+ let levels: &mut [u8] = if n <= 96 {
6198+ b96 = [0u8; 96];
61926199 &mut b96[..n]
61936200 } else if n <= 256 {
6194- b256 = [0i32; 256];
6201+ b256 = [0u8; 256];
61956202 &mut b256[..n]
61966203 } else {
6197- b768 = [0i32; 768];
6204+ b768 = [0u8; 768];
61986205 &mut b768[..n]
61996206 };
62006207 let wsh = w.trailing_zeros();
@@ -6247,7 +6254,8 @@ fn read_coeffs_rect(
62476254 } else {
62486255 level
62496256 };
6250- levels[row * stride + col] = level;
6257+ debug_assert!(level >= 0 && level <= 15, "level {level} past the u8 frame");
6258+ levels[row * stride + col] = level as u8;
62516259 if rect_trace {
62526260 let (rng, _) = dec.debug_state();
62536261 eprintln!("EC_COEFF_STEP tag=base c={scan_idx} pos={pos} level={level} rng={rng}");
@@ -6259,7 +6267,7 @@ fn read_coeffs_rect(
62596267 }
62606268 for &pos in &scan[..eob] {
62616269 let pos = pos as usize;
6262- let level = levels[(pos >> wsh) * stride + (pos & wmask)];
6270+ let level = levels[(pos >> wsh) * stride + (pos & wmask)] as i32;
62636271 if level == 0 {
62646272 continue;
62656273 }
@@ -6272,7 +6280,7 @@ fn read_coeffs_rect(
62726280 let (rng, _) = dec.debug_state();
62736281 eprintln!("EC_COEFF_STEP tag=sign_rect pos={pos} sign={} dcctx={sign_ctx} rng={rng}", u8::from(negative));
62746282 }
6275- let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL {
6283+ let level = if level > MAX_BR_LEVEL {
62766284 let g = read_golomb(dec)?;
62776285 level + g as i32
62786286 } else {
@@ -13408,19 +13416,12 @@ fn read_plane(
1340813416 filter_intra,
1340913417 smooth_neighbor, fctx,
1341013418 );
13411- Ok(if tx_side == side {
13412- residual_grid_placeholder(&levels, side)
13413- } else {
13414- levels
13415- })
13416-}
13417-
13418-/// `record`'s neighbour state only reads whether *the coded grid* (not the
13419-/// residual) has nonzero entries, so the levels this function already built
13420-/// are what it wants back; this helper exists only to name that clearly at
13421-/// the one call site where `tx_side == side`.
13422-fn residual_grid_placeholder(levels: &[i32], _tx_side: usize) -> Vec<i32> {
13423- levels.to_vec()
13419+ // `record`'s neighbour state only reads whether *the coded grid* (not the
13420+ // residual) has nonzero entries, so the levels this function already
13421+ // built are what it wants back -- both arms of the old `tx_side == side`
13422+ // branch returned exactly that, one of them through a full `to_vec` copy
13423+ // of the grid per intra transform unit (lane-coefbuf).
13424+ Ok(levels)
1342413425 }
1342513426
1342613427 /// Decodes one whole-block coded superblock/quadrant/leaf: its mode, then its
@@ -35529,7 +35530,7 @@ mod tests {
3552935530 /// corner held a coefficient off the first row/column.
3553035531 #[test]
3553135532 fn base_ctx_rect_offsets_match_the_transcribed_tables_over_the_whole_domain() {
35532- let grid = [0i32; 36 * 36];
35533+ let grid = [0u8; 36 * 36];
3553335534 for (shape, table) in [
3553435535 (Some((64usize, 32usize)), &cdf::NZ_MAP_CTX_OFFSET_64X32),
3553535536 (Some((32, 64)), &cdf::NZ_MAP_CTX_OFFSET_32X64),