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(
54785478 }
54795479 }
54805480
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)]
54815529 fn br_ctx(grid: &[u8], stride: usize, row: usize, col: usize, class: TxClass) -> usize {
54825530 let extra = match class {
54835531 TxClass::TwoD => neighbour(grid, stride, row + 1, col + 1),
@@ -5721,6 +5769,10 @@ fn base_ctx_rect(
57215769 /// [`br_ctx`]'s `TxClass::TwoD` arm widened to `(w, h)` (lane-rectwire): the
57225770 /// neighbour-offset math itself is shape-independent, only the boundary
57235771 /// 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)]
57245776 fn br_ctx_rect(
57255777 grid: &[u8],
57265778 stride: usize,
@@ -6022,9 +6074,21 @@ fn read_coeffs(
60226074 let stride = side + 4;
60236075 let mut levels_buf = [0u8; 36 * 36];
60246076 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);
60256088 for scan_idx in (0..eob).rev() {
60266089 let pos = scan[scan_idx] as usize;
60276090 let (row, col) = (pos >> sh, pos & col_mask);
6091+ let idx = row * stride + col;
60286092 let level = if scan_idx == eob - 1 {
60296093 let ctx = eob_coeff_ctx(scan_idx, side * side);
60306094 let v = dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1;
@@ -6035,7 +6099,16 @@ fn read_coeffs(
60356099 }
60366100 v
60376101 } 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+ };
60396112 let v = dec.symbol(&mut coding.base[ctx]) as i32;
60406113 if ec_trace_coeff {
60416114 let (rng, _) = dec.debug_state();
@@ -6054,7 +6127,19 @@ fn read_coeffs(
60546127 v
60556128 };
60566129 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+ };
60586143 let mut level = level;
60596144 let mut sent = 0;
60606145 loop {
@@ -6095,19 +6180,21 @@ fn read_coeffs(
60956180 // frame is bytes -- the five-tap sum in [`base_ctx`]/[`br_ctx`] is a
60966181 // byte gather (lane-coefbuf).
60976182 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+ }
60996188 }
61006189 if ec_trace_coeff {
61016190 let (rng, _) = dec.debug_state();
61026191 eprintln!("EC_COEFF_STEP tag=after_bases rng={rng}");
61036192 }
61046193
6105- for (c, &pos) in scan[..eob].iter().enumerate() {
6106- let pos = pos as usize;
6107- let level = levels[(pos >> sh) * stride + (pos & col_mask)] as i32;
6108- if level == 0 {
6109- continue;
6110- }
6194+ for &packed in nz_buf[..nz_n].iter().rev() {
6195+ let c = (packed >> 4) as usize;
6196+ let pos = scan[c] as usize;
6197+ let level = (packed & 15) as i32;
61116198 let negative = if pos == 0 {
61126199 let v = dec.symbol(&mut coding.dc_sign[sign_ctx]) == 1;
61136200 if trace {
@@ -6226,9 +6313,15 @@ fn read_coeffs_rect(
62266313 };
62276314 let wsh = w.trailing_zeros();
62286315 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);
62296321 for scan_idx in (0..eob).rev() {
62306322 let pos = scan[scan_idx] as usize;
62316323 let (row, col) = (pos >> wsh, pos & wmask);
6324+ let idx = row * stride + col;
62326325 // libaom's column-major `coeff_idx`, for ladder comparison.
62336326 let _apos = col * h + row;
62346327 let level = if scan_idx == eob - 1 {
@@ -6242,7 +6335,22 @@ fn read_coeffs_rect(
62426335 }
62436336 v
62446337 } 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+ };
62466354 let v = dec.symbol(&mut coding.base[ctx]) as i32;
62476355 if rect_trace {
62486356 eprintln!(
@@ -6253,7 +6361,19 @@ fn read_coeffs_rect(
62536361 v
62546362 };
62556363 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+ };
62576377 let mut level = level;
62586378 let mut sent = 0;
62596379 loop {
@@ -6275,7 +6395,11 @@ fn read_coeffs_rect(
62756395 level
62766396 };
62776397 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+ }
62796403 if rect_trace {
62806404 let (rng, _) = dec.debug_state();
62816405 eprintln!("EC_COEFF_STEP tag=base c={scan_idx} pos={pos} level={level} rng={rng}");
@@ -6285,12 +6409,9 @@ fn read_coeffs_rect(
62856409 let (rng, _) = dec.debug_state();
62866410 eprintln!("EC_COEFF_STEP tag=after_bases rng={rng}");
62876411 }
6288- for &pos in &scan[..eob] {
6289- let pos = pos as usize;
6290- let level = levels[(pos >> wsh) * stride + (pos & wmask)] as i32;
6291- if level == 0 {
6292- continue;
6293- }
6412+ for &packed in nz_buf[..nz_n].iter().rev() {
6413+ let pos = scan[(packed >> 4) as usize] as usize;
6414+ let level = (packed & 15) as i32;
62946415 let negative = if pos == 0 {
62956416 dec.symbol(&mut coding.dc_sign[sign_ctx]) == 1
62966417 } else {
@@ -35966,6 +36087,60 @@ mod tests {
3596636087 /// `[row][col]`, which is the transpose (the OTHER shape's rule, with 11
3596736088 /// and 16 swapped): it desynced the first superblock strip whose luma
3596836089 /// 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+
3596936144 #[test]
3597036145 fn base_ctx_rect_offsets_match_the_transcribed_tables_over_the_whole_domain() {
3597136146 let grid = [0u8; 36 * 36];
crates/ec-av1/src/msac.rsmodified+5 -0
@@ -429,6 +429,11 @@ impl<'a> SymbolDecoder<'a> {
429429
430430 /// Reads `bits` raw bits, most significant first — the `L(n)` descriptor
431431 /// 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]
432437 pub fn literal(&mut self, bits: u32) -> u32 {
433438 let mut v = 0;
434439 for _ in 0..bits {