forge
Explore
Log in

Don't you dare go Hollow

Coefficient reading walks a non-zero list and flat neighbour taps

Browse files at this commit

de3e402438e8c20d6c49e6528da2df6f7959ddb4Ahmet Kaan GümüşParent: 2426e3d
  • perf
    • coefficient reading walks a non-zero list and flat neighbour taps
Show full message
perf(av1): coefficient reading walks a non-zero list and flat neighbour taps

The sign pass re-walked `scan[..eob]`, redid the `pos >> sh` / `& mask`
decomposition and loaded each level back out of the padded frame only to
skip the zeros. The base pass already knows both, so it now records
`(scan_idx << 4) | level` per non-zero coefficient and the sign pass walks
that list in reverse -- same coefficients, same order, same trace output.

The five base-context neighbour taps and the three base-range ones are a
function of the tx class alone, which is fixed for a whole transform unit:
`flat_taps` resolves them once per unit as flat offsets into the padded
level frame, so the per-coefficient path loses the class match and the
`(dr, dc)` -> `dr * stride + dc` arithmetic. Both readers (square and rect)
share the new `base_mag`/`br_mag`; `br_ctx`/`br_ctx_rect` stay as the
`(row, col)` form the new test pins the flat one against, over every class,
every square size and the whole `(row, col)` domain.

`SymbolDecoder::literal` was a real call per raw bit across a codegen-unit
boundary (this crate builds without LTO) -- the sign pass and the Golomb
tail read one bit at a time; it is `#[inline]` now.

instructions:u at EC_AV1_THREADS=1: 4K segment -1.01%, 1080p segment -1.42%.
crates/ec-av1/src/decode.rsmodified+193 -18
@@ -5478,6 +5478,54 @@ fn base_ctx(
5478 }5478 }
5479 }5479 }
5480 5480
5481+/// The five `base_ctx` neighbour taps and the three `br_ctx` ones as FLAT
5482+/// offsets into the padded level frame (lane-coefread): the tap pattern is a
5483+/// function of the tx class alone, which is fixed for a whole transform unit,
5484+/// so resolving it once per unit takes the class match and the `(dr, dc)`
5485+/// -> `dr * stride + dc` arithmetic out of the per-coefficient path. Same
5486+/// taps as [`base_ctx`]/[`br_ctx`] read, stated once;
5487+/// `flat_taps_match_the_row_col_neighbour_reads` pins them against the
5488+/// original `(dr, dc)` form.
5489+fn flat_taps(stride: usize, class: TxClass) -> ([usize; 5], [usize; 3]) {
5490+ match class {
5491+ TxClass::TwoD => (
5492+ [stride, 1, stride + 1, 2 * stride, 2],
5493+ [stride, 1, stride + 1],
5494+ ),
5495+ TxClass::Horiz => ([stride, 1, 2, 3, 4], [stride, 1, 2]),
5496+ TxClass::Vert => (
5497+ [stride, 1, 2 * stride, 3 * stride, 4 * stride],
5498+ [stride, 1, 2 * stride],
5499+ ),
5500+ }
5501+}
5502+
5503+/// [`base_ctx`]'s neighbour sum, off flat taps: `((sum of five clamped
5504+/// neighbours) + 1) >> 1`, clamped to 4.
5505+#[inline(always)]
5506+fn base_mag(grid: &[u8], idx: usize, taps: &[usize; 5]) -> usize {
5507+ let mut mag = 0u32;
5508+ for &t in taps {
5509+ mag += u32::from(grid[idx + t]).min(3);
5510+ }
5511+ (((mag + 1) >> 1) as usize).min(4)
5512+}
5513+
5514+/// [`br_ctx`]'s neighbour sum, off flat taps.
5515+#[inline(always)]
5516+fn br_mag(grid: &[u8], idx: usize, taps: &[usize; 3]) -> usize {
5517+ let mut mag = 0u32;
5518+ for &t in taps {
5519+ mag += u32::from(grid[idx + t]);
5520+ }
5521+ (((mag + 1) >> 1) as usize).min(6)
5522+}
5523+
5524+/// lane-coefread: superseded on the decode path by [`br_mag`] plus the
5525+/// origin-offset tail the two coefficient readers state inline; kept as the
5526+/// `(row, col)` form `flat_taps_match_the_row_col_neighbour_reads` pins the
5527+/// flat one against.
5528+#[cfg(test)]
5481 fn br_ctx(grid: &[u8], stride: usize, row: usize, col: usize, class: TxClass) -> usize {5529 fn br_ctx(grid: &[u8], stride: usize, row: usize, col: usize, class: TxClass) -> usize {
5482 let extra = match class {5530 let extra = match class {
5483 TxClass::TwoD => neighbour(grid, stride, row + 1, col + 1),5531 TxClass::TwoD => neighbour(grid, stride, row + 1, col + 1),
@@ -5721,6 +5769,10 @@ fn base_ctx_rect(
5721 /// [`br_ctx`]'s `TxClass::TwoD` arm widened to `(w, h)` (lane-rectwire): the5769 /// [`br_ctx`]'s `TxClass::TwoD` arm widened to `(w, h)` (lane-rectwire): the
5722 /// neighbour-offset math itself is shape-independent, only the boundary5770 /// neighbour-offset math itself is shape-independent, only the boundary
5723 /// clamp needs the real bound in each axis.5771 /// clamp needs the real bound in each axis.
5772+/// lane-coefread: superseded by [`br_mag`] on the rect decode path too --
5773+/// its body was character-for-character [`br_ctx`]'s, which
5774+/// `flat_taps_match_the_row_col_neighbour_reads` now asserts.
5775+#[cfg(test)]
5724 fn br_ctx_rect(5776 fn br_ctx_rect(
5725 grid: &[u8],5777 grid: &[u8],
5726 stride: usize,5778 stride: usize,
@@ -6022,9 +6074,21 @@ fn read_coeffs(
6022 let stride = side + 4;6074 let stride = side + 4;
6023 let mut levels_buf = [0u8; 36 * 36];6075 let mut levels_buf = [0u8; 36 * 36];
6024 let levels = &mut levels_buf[..stride * stride];6076 let levels = &mut levels_buf[..stride * stride];
6077+ // lane-coefread: the sign pass used to re-walk `scan[..eob]`, redo the
6078+ // `pos >> sh` / `& mask` decomposition and load the level back out of the
6079+ // padded frame just to skip the zeros. The base pass already knows both,
6080+ // so it records `(scan_idx << 4) | level` for each NON-ZERO coefficient
6081+ // (`scan_idx < 1024`, `level <= 15` -- both loops assert it), and the sign
6082+ // pass walks that list in reverse (the base pass runs in reverse scan
6083+ // order, so reversing it is forward scan order: the same coefficients, in
6084+ // the same order, with the same `c` the trace prints).
6085+ let mut nz_buf = [0u16; 1024];
6086+ let mut nz_n = 0usize;
6087+ let (base_taps, br_taps) = flat_taps(stride, class);
6025 for scan_idx in (0..eob).rev() {6088 for scan_idx in (0..eob).rev() {
6026 let pos = scan[scan_idx] as usize;6089 let pos = scan[scan_idx] as usize;
6027 let (row, col) = (pos >> sh, pos & col_mask);6090 let (row, col) = (pos >> sh, pos & col_mask);
6091+ let idx = row * stride + col;
6028 let level = if scan_idx == eob - 1 {6092 let level = if scan_idx == eob - 1 {
6029 let ctx = eob_coeff_ctx(scan_idx, side * side);6093 let ctx = eob_coeff_ctx(scan_idx, side * side);
6030 let v = dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1;6094 let v = dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1;
@@ -6035,7 +6099,16 @@ fn read_coeffs(
6035 }6099 }
6036 v6100 v
6037 } else {6101 } else {
6038- let ctx = base_ctx(levels, stride, row, col, class, rect_shape);6102+ let ctx = if class == TxClass::TwoD && rect_shape.is_none() {
6103+ if row == 0 && col == 0 {
6104+ 0
6105+ } else {
6106+ base_mag(levels, idx, &base_taps)
6107+ + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize
6108+ }
6109+ } else {
6110+ base_ctx(levels, stride, row, col, class, rect_shape)
6111+ };
6039 let v = dec.symbol(&mut coding.base[ctx]) as i32;6112 let v = dec.symbol(&mut coding.base[ctx]) as i32;
6040 if ec_trace_coeff {6113 if ec_trace_coeff {
6041 let (rng, _) = dec.debug_state();6114 let (rng, _) = dec.debug_state();
@@ -6054,7 +6127,19 @@ fn read_coeffs(
6054 v6127 v
6055 };6128 };
6056 let level = if level > NUM_BASE_LEVELS {6129 let level = if level > NUM_BASE_LEVELS {
6057- let ctx = br_ctx(levels, stride, row, col, class);6130+ let ctx = {
6131+ let mag = br_mag(levels, idx, &br_taps);
6132+ if row == 0 && col == 0 {
6133+ mag
6134+ } else {
6135+ let near_origin = match class {
6136+ TxClass::TwoD => row < 2 && col < 2,
6137+ TxClass::Horiz => col == 0,
6138+ TxClass::Vert => row == 0,
6139+ };
6140+ if near_origin { mag + 7 } else { mag + 14 }
6141+ }
6142+ };
6058 let mut level = level;6143 let mut level = level;
6059 let mut sent = 0;6144 let mut sent = 0;
6060 loop {6145 loop {
@@ -6095,19 +6180,21 @@ fn read_coeffs(
6095 // frame is bytes -- the five-tap sum in [`base_ctx`]/[`br_ctx`] is a6180 // frame is bytes -- the five-tap sum in [`base_ctx`]/[`br_ctx`] is a
6096 // byte gather (lane-coefbuf).6181 // byte gather (lane-coefbuf).
6097 debug_assert!(level >= 0 && level <= 15, "level {level} past the u8 frame");6182 debug_assert!(level >= 0 && level <= 15, "level {level} past the u8 frame");
6098- levels[row * stride + col] = level as u8;6183+ levels[idx] = level as u8;
6184+ if level != 0 {
6185+ nz_buf[nz_n] = ((scan_idx as u16) << 4) | level as u16;
6186+ nz_n += 1;
6187+ }
6099 }6188 }
6100 if ec_trace_coeff {6189 if ec_trace_coeff {
6101 let (rng, _) = dec.debug_state();6190 let (rng, _) = dec.debug_state();
6102 eprintln!("EC_COEFF_STEP tag=after_bases rng={rng}");6191 eprintln!("EC_COEFF_STEP tag=after_bases rng={rng}");
6103 }6192 }
6104 6193
6105- for (c, &pos) in scan[..eob].iter().enumerate() {6194+ for &packed in nz_buf[..nz_n].iter().rev() {
6106- let pos = pos as usize;6195+ let c = (packed >> 4) as usize;
6107- let level = levels[(pos >> sh) * stride + (pos & col_mask)] as i32;6196+ let pos = scan[c] as usize;
6108- if level == 0 {6197+ let level = (packed & 15) as i32;
6109- continue;
6110- }
6111 let negative = if pos == 0 {6198 let negative = if pos == 0 {
6112 let v = dec.symbol(&mut coding.dc_sign[sign_ctx]) == 1;6199 let v = dec.symbol(&mut coding.dc_sign[sign_ctx]) == 1;
6113 if trace {6200 if trace {
@@ -6226,9 +6313,15 @@ fn read_coeffs_rect(
6226 };6313 };
6227 let wsh = w.trailing_zeros();6314 let wsh = w.trailing_zeros();
6228 let wmask = w - 1;6315 let wmask = w - 1;
6316+ // lane-coefread: the non-zero list [`read_coeffs`] documents.
6317+ let mut nz_buf = [0u16; 1024];
6318+ let mut nz_n = 0usize;
6319+ let (base_taps, br_taps) = flat_taps(stride, class);
6320+ let (wlt, wgt) = (w < h, w > h);
6229 for scan_idx in (0..eob).rev() {6321 for scan_idx in (0..eob).rev() {
6230 let pos = scan[scan_idx] as usize;6322 let pos = scan[scan_idx] as usize;
6231 let (row, col) = (pos >> wsh, pos & wmask);6323 let (row, col) = (pos >> wsh, pos & wmask);
6324+ let idx = row * stride + col;
6232 // libaom's column-major `coeff_idx`, for ladder comparison.6325 // libaom's column-major `coeff_idx`, for ladder comparison.
6233 let _apos = col * h + row;6326 let _apos = col * h + row;
6234 let level = if scan_idx == eob - 1 {6327 let level = if scan_idx == eob - 1 {
@@ -6242,7 +6335,22 @@ fn read_coeffs_rect(
6242 }6335 }
6243 v6336 v
6244 } else {6337 } else {
6245- let ctx = base_ctx_rect(levels, stride, w, h, row, col, class);6338+ let ctx = if class == TxClass::TwoD {
6339+ if row == 0 && col == 0 {
6340+ 0
6341+ } else {
6342+ let m = base_mag(levels, idx, &base_taps);
6343+ if wlt && row < 2 {
6344+ m + 11
6345+ } else if wgt && col < 2 {
6346+ m + 16
6347+ } else {
6348+ m + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize
6349+ }
6350+ }
6351+ } else {
6352+ base_ctx_rect(levels, stride, w, h, row, col, class)
6353+ };
6246 let v = dec.symbol(&mut coding.base[ctx]) as i32;6354 let v = dec.symbol(&mut coding.base[ctx]) as i32;
6247 if rect_trace {6355 if rect_trace {
6248 eprintln!(6356 eprintln!(
@@ -6253,7 +6361,19 @@ fn read_coeffs_rect(
6253 v6361 v
6254 };6362 };
6255 let level = if level > NUM_BASE_LEVELS {6363 let level = if level > NUM_BASE_LEVELS {
6256- let ctx = br_ctx_rect(levels, stride, row, col, class);6364+ let ctx = {
6365+ let mag = br_mag(levels, idx, &br_taps);
6366+ if row == 0 && col == 0 {
6367+ mag
6368+ } else {
6369+ let near_origin = match class {
6370+ TxClass::TwoD => row < 2 && col < 2,
6371+ TxClass::Horiz => col == 0,
6372+ TxClass::Vert => row == 0,
6373+ };
6374+ if near_origin { mag + 7 } else { mag + 14 }
6375+ }
6376+ };
6257 let mut level = level;6377 let mut level = level;
6258 let mut sent = 0;6378 let mut sent = 0;
6259 loop {6379 loop {
@@ -6275,7 +6395,11 @@ fn read_coeffs_rect(
6275 level6395 level
6276 };6396 };
6277 debug_assert!(level >= 0 && level <= 15, "level {level} past the u8 frame");6397 debug_assert!(level >= 0 && level <= 15, "level {level} past the u8 frame");
6278- levels[row * stride + col] = level as u8;6398+ levels[idx] = level as u8;
6399+ if level != 0 {
6400+ nz_buf[nz_n] = ((scan_idx as u16) << 4) | level as u16;
6401+ nz_n += 1;
6402+ }
6279 if rect_trace {6403 if rect_trace {
6280 let (rng, _) = dec.debug_state();6404 let (rng, _) = dec.debug_state();
6281 eprintln!("EC_COEFF_STEP tag=base c={scan_idx} pos={pos} level={level} rng={rng}");6405 eprintln!("EC_COEFF_STEP tag=base c={scan_idx} pos={pos} level={level} rng={rng}");
@@ -6285,12 +6409,9 @@ fn read_coeffs_rect(
6285 let (rng, _) = dec.debug_state();6409 let (rng, _) = dec.debug_state();
6286 eprintln!("EC_COEFF_STEP tag=after_bases rng={rng}");6410 eprintln!("EC_COEFF_STEP tag=after_bases rng={rng}");
6287 }6411 }
6288- for &pos in &scan[..eob] {6412+ for &packed in nz_buf[..nz_n].iter().rev() {
6289- let pos = pos as usize;6413+ let pos = scan[(packed >> 4) as usize] as usize;
6290- let level = levels[(pos >> wsh) * stride + (pos & wmask)] as i32;6414+ let level = (packed & 15) as i32;
6291- if level == 0 {
6292- continue;
6293- }
6294 let negative = if pos == 0 {6415 let negative = if pos == 0 {
6295 dec.symbol(&mut coding.dc_sign[sign_ctx]) == 16416 dec.symbol(&mut coding.dc_sign[sign_ctx]) == 1
6296 } else {6417 } else {
@@ -35966,6 +36087,60 @@ mod tests {
35966 /// `[row][col]`, which is the transpose (the OTHER shape's rule, with 1136087 /// `[row][col]`, which is the transpose (the OTHER shape's rule, with 11
35967 /// and 16 swapped): it desynced the first superblock strip whose luma36088 /// and 16 swapped): it desynced the first superblock strip whose luma
35968 /// corner held a coefficient off the first row/column.36089 /// corner held a coefficient off the first row/column.
36090+ /// lane-coefread: the per-coefficient neighbour gather now reads FLAT
36091+ /// offsets ([`flat_taps`]) instead of `(row + dr, col + dc)`. Same cells,
36092+ /// every class, every square size, over the whole `(row, col)` domain.
36093+ #[test]
36094+ fn flat_taps_match_the_row_col_neighbour_reads() {
36095+ for &side in &[4usize, 8, 16, 32] {
36096+ let stride = side + 4;
36097+ let mut grid = vec![0u8; stride * stride];
36098+ let mut x = 12345u32;
36099+ for c in grid.iter_mut() {
36100+ x = x.wrapping_mul(1103515245).wrapping_add(12345);
36101+ *c = ((x >> 16) & 15) as u8;
36102+ }
36103+ for class in [TxClass::TwoD, TxClass::Horiz, TxClass::Vert] {
36104+ let (bt, rt) = flat_taps(stride, class);
36105+ for row in 0..side {
36106+ for col in 0..side {
36107+ let idx = row * stride + col;
36108+ if !(class == TxClass::TwoD && row == 0 && col == 0) {
36109+ let want = base_ctx(&grid, stride, row, col, class, None);
36110+ let got = base_mag(&grid, idx, &bt)
36111+ + match class {
36112+ TxClass::TwoD => {
36113+ cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize
36114+ }
36115+ TxClass::Horiz => nz_map_ctx_offset_1d(col.min(31)),
36116+ TxClass::Vert => nz_map_ctx_offset_1d(row.min(31)),
36117+ };
36118+ assert_eq!(got, want, "base side {side} at {row},{col}");
36119+ }
36120+ let want = br_ctx(&grid, stride, row, col, class);
36121+ let mag = br_mag(&grid, idx, &rt);
36122+ let got = if row == 0 && col == 0 {
36123+ mag
36124+ } else {
36125+ let near = match class {
36126+ TxClass::TwoD => row < 2 && col < 2,
36127+ TxClass::Horiz => col == 0,
36128+ TxClass::Vert => row == 0,
36129+ };
36130+ if near { mag + 7 } else { mag + 14 }
36131+ };
36132+ assert_eq!(got, want, "br side {side} at {row},{col}");
36133+ assert_eq!(
36134+ br_ctx_rect(&grid, stride, row, col, class),
36135+ want,
36136+ "br_rect side {side} at {row},{col}"
36137+ );
36138+ }
36139+ }
36140+ }
36141+ }
36142+ }
36143+
35969 #[test]36144 #[test]
35970 fn base_ctx_rect_offsets_match_the_transcribed_tables_over_the_whole_domain() {36145 fn base_ctx_rect_offsets_match_the_transcribed_tables_over_the_whole_domain() {
35971 let grid = [0u8; 36 * 36];36146 let grid = [0u8; 36 * 36];
crates/ec-av1/src/msac.rsmodified+5 -0
@@ -429,6 +429,11 @@ impl<'a> SymbolDecoder<'a> {
429 429
430 /// Reads `bits` raw bits, most significant first — the `L(n)` descriptor430 /// Reads `bits` raw bits, most significant first — the `L(n)` descriptor
431 /// of spec 4.10.4.431 /// of spec 4.10.4.
432+ ///
433+ /// lane-coefread: `#[inline]`, because without it this was a real CALL per
434+ /// raw bit across a codegen-unit boundary (the crate builds without LTO) --
435+ /// the coefficient sign pass and the Golomb tail read one bit at a time.
436+ #[inline]
432 pub fn literal(&mut self, bits: u32) -> u32 {437 pub fn literal(&mut self, bits: u32) -> u32 {
433 let mut v = 0;438 let mut v = 0;
434 for _ in 0..bits {439 for _ in 0..bits {