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 commit1 changed file +46 -45
- crates/ec-av1/src/decode.rs +46 -45
crates/ec-av1/src/decode.rsmodified+46 -45
| @@ -5352,8 +5352,8 @@ fn eob_coeff_ctx(scan_idx: usize, area: usize) -> usize { | ||
| 5352 | 5352 | /// this used to run five times per coefficient. Nothing writes a padding |
| 5353 | 5353 | /// cell, so the values are the same zeros the test returned. |
| 5354 | 5354 | #[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 | |
| 5357 | 5357 | } |
| 5358 | 5358 | |
| 5359 | 5359 | /// 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 { | ||
| 5392 | 5392 | } |
| 5393 | 5393 | |
| 5394 | 5394 | fn base_ctx( |
| 5395 | - grid: &[i32], | |
| 5395 | + grid: &[u8], | |
| 5396 | 5396 | stride: usize, |
| 5397 | 5397 | row: usize, |
| 5398 | 5398 | col: usize, |
| @@ -5417,7 +5417,7 @@ fn base_ctx( | ||
| 5417 | 5417 | }; |
| 5418 | 5418 | let mag: i32 = offsets |
| 5419 | 5419 | .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)) | |
| 5421 | 5421 | .sum(); |
| 5422 | 5422 | let ctx = ((mag + 1) >> 1).min(4) as usize; |
| 5423 | 5423 | match class { |
| @@ -5460,14 +5460,14 @@ fn base_ctx( | ||
| 5460 | 5460 | } |
| 5461 | 5461 | } |
| 5462 | 5462 | |
| 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 { | |
| 5464 | 5464 | 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), | |
| 5468 | 5468 | }; |
| 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) | |
| 5471 | 5471 | + extra; |
| 5472 | 5472 | let mag = (((mag + 1) >> 1).min(6)) as usize; |
| 5473 | 5473 | if row == 0 && col == 0 { |
| @@ -5648,8 +5648,8 @@ const SCAN_4X16: [u16; 64] = [ | ||
| 5648 | 5648 | /// (`stride = w + 4`, four spare rows), so the edge test is the zeros the |
| 5649 | 5649 | /// padding already holds. |
| 5650 | 5650 | #[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 | |
| 5653 | 5653 | } |
| 5654 | 5654 | |
| 5655 | 5655 | /// [`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 { | ||
| 5659 | 5659 | /// `V_DCT`/`H_DCT` -- geometrically impossible at the two size pairs |
| 5660 | 5660 | /// [`decode_block_rect`] codes (see that function's own doc comment). |
| 5661 | 5661 | fn base_ctx_rect( |
| 5662 | - grid: &[i32], | |
| 5662 | + grid: &[u8], | |
| 5663 | 5663 | stride: usize, |
| 5664 | 5664 | w: usize, |
| 5665 | 5665 | h: usize, |
| @@ -5679,7 +5679,7 @@ fn base_ctx_rect( | ||
| 5679 | 5679 | }; |
| 5680 | 5680 | let mag: i32 = offsets |
| 5681 | 5681 | .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)) | |
| 5683 | 5683 | .sum(); |
| 5684 | 5684 | let ctx = ((mag + 1) >> 1).min(4) as usize; |
| 5685 | 5685 | match class { |
| @@ -5704,19 +5704,19 @@ fn base_ctx_rect( | ||
| 5704 | 5704 | /// neighbour-offset math itself is shape-independent, only the boundary |
| 5705 | 5705 | /// clamp needs the real bound in each axis. |
| 5706 | 5706 | fn br_ctx_rect( |
| 5707 | - grid: &[i32], | |
| 5707 | + grid: &[u8], | |
| 5708 | 5708 | stride: usize, |
| 5709 | 5709 | row: usize, |
| 5710 | 5710 | col: usize, |
| 5711 | 5711 | class: TxClass, |
| 5712 | 5712 | ) -> usize { |
| 5713 | 5713 | 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), | |
| 5717 | 5717 | }; |
| 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) | |
| 5720 | 5720 | + extra; |
| 5721 | 5721 | let mag = (((mag + 1) >> 1).min(6)) as usize; |
| 5722 | 5722 | if row == 0 && col == 0 { |
| @@ -6000,7 +6000,7 @@ fn read_coeffs( | ||
| 6000 | 6000 | // `row, col <= side - 1` -- stays inside `(side + 4) * (side + 4)`, at |
| 6001 | 6001 | // most `36 * 36`. Only `col < side`, `row < side` cells are ever written. |
| 6002 | 6002 | let stride = side + 4; |
| 6003 | - let mut levels_buf = [0i32; 36 * 36]; | |
| 6003 | + let mut levels_buf = [0u8; 36 * 36]; | |
| 6004 | 6004 | let levels = &mut levels_buf[..stride * stride]; |
| 6005 | 6005 | for scan_idx in (0..eob).rev() { |
| 6006 | 6006 | let pos = scan[scan_idx] as usize; |
| @@ -6069,7 +6069,13 @@ fn read_coeffs( | ||
| 6069 | 6069 | let (rng, _) = dec.debug_state(); |
| 6070 | 6070 | eprintln!("EC_COEFF_STEP tag=base_eob level={level} rng={rng}"); |
| 6071 | 6071 | } |
| 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; | |
| 6073 | 6079 | } |
| 6074 | 6080 | if ec_trace_coeff { |
| 6075 | 6081 | let (rng, _) = dec.debug_state(); |
| @@ -6078,7 +6084,7 @@ fn read_coeffs( | ||
| 6078 | 6084 | |
| 6079 | 6085 | for (c, &pos) in scan[..eob].iter().enumerate() { |
| 6080 | 6086 | 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; | |
| 6082 | 6088 | if level == 0 { |
| 6083 | 6089 | continue; |
| 6084 | 6090 | } |
| @@ -6095,7 +6101,7 @@ fn read_coeffs( | ||
| 6095 | 6101 | let (rng, _) = dec.debug_state(); |
| 6096 | 6102 | eprintln!("EC_COEFF_STEP tag=sign c={c} sign={} rng={rng}", negative as i32); |
| 6097 | 6103 | } |
| 6098 | - let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL { | |
| 6104 | + let level = if level > MAX_BR_LEVEL { | |
| 6099 | 6105 | let g = read_golomb(dec)?; |
| 6100 | 6106 | if trace { |
| 6101 | 6107 | eprintln!("TRACE golomb pos={pos} value={g}"); |
| @@ -6187,14 +6193,15 @@ fn read_coeffs_rect( | ||
| 6187 | 6193 | let n = stride * (h + 4); |
| 6188 | 6194 | debug_assert!(w.is_power_of_two() && n <= 36 * 36); |
| 6189 | 6195 | 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]; | |
| 6192 | 6199 | &mut b96[..n] |
| 6193 | 6200 | } else if n <= 256 { |
| 6194 | - b256 = [0i32; 256]; | |
| 6201 | + b256 = [0u8; 256]; | |
| 6195 | 6202 | &mut b256[..n] |
| 6196 | 6203 | } else { |
| 6197 | - b768 = [0i32; 768]; | |
| 6204 | + b768 = [0u8; 768]; | |
| 6198 | 6205 | &mut b768[..n] |
| 6199 | 6206 | }; |
| 6200 | 6207 | let wsh = w.trailing_zeros(); |
| @@ -6247,7 +6254,8 @@ fn read_coeffs_rect( | ||
| 6247 | 6254 | } else { |
| 6248 | 6255 | level |
| 6249 | 6256 | }; |
| 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; | |
| 6251 | 6259 | if rect_trace { |
| 6252 | 6260 | let (rng, _) = dec.debug_state(); |
| 6253 | 6261 | eprintln!("EC_COEFF_STEP tag=base c={scan_idx} pos={pos} level={level} rng={rng}"); |
| @@ -6259,7 +6267,7 @@ fn read_coeffs_rect( | ||
| 6259 | 6267 | } |
| 6260 | 6268 | for &pos in &scan[..eob] { |
| 6261 | 6269 | 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; | |
| 6263 | 6271 | if level == 0 { |
| 6264 | 6272 | continue; |
| 6265 | 6273 | } |
| @@ -6272,7 +6280,7 @@ fn read_coeffs_rect( | ||
| 6272 | 6280 | let (rng, _) = dec.debug_state(); |
| 6273 | 6281 | eprintln!("EC_COEFF_STEP tag=sign_rect pos={pos} sign={} dcctx={sign_ctx} rng={rng}", u8::from(negative)); |
| 6274 | 6282 | } |
| 6275 | - let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL { | |
| 6283 | + let level = if level > MAX_BR_LEVEL { | |
| 6276 | 6284 | let g = read_golomb(dec)?; |
| 6277 | 6285 | level + g as i32 |
| 6278 | 6286 | } else { |
| @@ -13408,19 +13416,12 @@ fn read_plane( | ||
| 13408 | 13416 | filter_intra, |
| 13409 | 13417 | smooth_neighbor, fctx, |
| 13410 | 13418 | ); |
| 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) | |
| 13424 | 13425 | } |
| 13425 | 13426 | |
| 13426 | 13427 | /// Decodes one whole-block coded superblock/quadrant/leaf: its mode, then its |
| @@ -35529,7 +35530,7 @@ mod tests { | ||
| 35529 | 35530 | /// corner held a coefficient off the first row/column. |
| 35530 | 35531 | #[test] |
| 35531 | 35532 | 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]; | |
| 35533 | 35534 | for (shape, table) in [ |
| 35534 | 35535 | (Some((64usize, 32usize)), &cdf::NZ_MAP_CTX_OFFSET_64X32), |
| 35535 | 35536 | (Some((32, 64)), &cdf::NZ_MAP_CTX_OFFSET_32X64), |