forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-av1speed2' (e65b15a0): zero-residual short-circuits and stack intra trial buffers, byte-exact

Browse files at this commit

1cc5362998a0a29c1817b54d098e2138d0c0021eAhmet Kaan GümüşParent: bb5c61609d0739
-1.30% instructions at 1080p, 4K 4x2/8 threads 1.37 -> 1.41 fps. Fresh
threaded profile: tile search 72% (1080p) / 55% (4K), filter search 19% /
28%, capture decode 3.6% / 10.4%; top symbols write_coeffs, intra::predict,
symbol_fixed, the forward transform family.
crates/ec-av1/src/encode.rsmodified+110 -50
@@ -41,7 +41,7 @@ use crate::tile::{
4141 BlockCoeffs, Coeff, INTRA_MODE_CTX, InterInfo, InterMode, Quadrant, Superblock, partition_bits,
4242 };
4343 use crate::transform::{
44- TxType, dequant_and_inverse, dequant_and_inverse_typed, forward_and_quantize,
44+ TxType, dequant_and_inverse_typed_wh, forward_and_quantize,
4545 forward_and_quantize_typed,
4646 };
4747
@@ -689,7 +689,12 @@ fn intra_predict_u8(
689689 // a block is at most `BLOCK * BLOCK`. Same samples either way.
690690 let mut above_buf = [0u16; 2 * BLOCK];
691691 let mut left_buf = [0u16; 2 * BLOCK];
692- let mut dst_buf = [0u16; BLOCK * BLOCK];
692+ // lane-av1speed2: a `BLOCK * BLOCK` scratch is 2 KB zeroed on every mode
693+ // trial of every block, and a 4x4 block writes 32 bytes of it. The
694+ // predictor fills every sample it is handed, so the zeros are dead
695+ // either way -- size the scratch to the block instead of to the largest
696+ // block there is.
697+
693698 let widen = |src: &[u8], buf: &mut [u16; 2 * BLOCK]| {
694699 for (d, &v) in buf[..src.len()].iter_mut().zip(src) {
695700 *d = u16::from(v);
@@ -703,21 +708,30 @@ fn intra_predict_u8(
703708 }
704709 let above16 = above.map(|s| &above_buf[..s.len()]);
705710 let left16 = left.map(|s| &left_buf[..s.len()]);
706- let dst16 = &mut dst_buf[..dst.len()];
707- crate::intra::predict(
708- mode,
709- angle_delta,
710- above16,
711- left16,
712- corner.map(u16::from),
713- bw,
714- bh,
715- enable_edge_filter,
716- smooth_neighbor,
717- dst16, fctx,
718- );
719- for (d, &s) in dst.iter_mut().zip(dst16.iter()) {
720- *d = s as u8;
711+ let mut run = |dst16: &mut [u16]| {
712+ crate::intra::predict(
713+ mode,
714+ angle_delta,
715+ above16,
716+ left16,
717+ corner.map(u16::from),
718+ bw,
719+ bh,
720+ enable_edge_filter,
721+ smooth_neighbor,
722+ dst16, fctx,
723+ );
724+ for (d, &s) in dst.iter_mut().zip(dst16.iter()) {
725+ *d = s as u8;
726+ }
727+ };
728+ let n = bw * bh;
729+ if n <= 64 {
730+ run(&mut [0u16; 64][..n]);
731+ } else if n <= 256 {
732+ run(&mut [0u16; 256][..n]);
733+ } else {
734+ run(&mut [0u16; BLOCK * BLOCK][..n]);
721735 }
722736 }
723737
@@ -1392,13 +1406,19 @@ impl Plane<'_> {
13921406 /// they are not, or where the frame ends first, the edge is shorter and
13931407 /// the predictor repeats its last sample, exactly as the decoder's clamp
13941408 /// to `aboveLimit` and `leftLimit` does (spec 7.11.2.2).
1395- fn edges(
1409+ /// lane-av1speed2: the two edges used to be a `Vec` each -- a malloc and
1410+ /// a free per mode per block, on the search's hottest path -- while an
1411+ /// edge is at most `2 * side` samples, which is `2 * BLOCK`. Same
1412+ /// samples, on the caller's stack.
1413+ fn edges_into<'a>(
13961414 &self,
13971415 x: usize,
13981416 y: usize,
13991417 side: usize,
14001418 reach: Reach,
1401- ) -> (Option<Vec<u8>>, Option<Vec<u8>>, Option<u8>) {
1419+ above_buf: &'a mut [u8; 2 * BLOCK],
1420+ left_buf: &'a mut [u8; 2 * BLOCK],
1421+ ) -> (Option<&'a [u8]>, Option<&'a [u8]>, Option<u8>) {
14021422 // Even a block's *own* edge is clamped to the true frame bound, not
14031423 // just a `reach` extension: libaom's `av1_predict_intra_block`
14041424 // (`av1/common/reconintra.c`) derives `xr`/`yd` -- the distance from
@@ -1436,16 +1456,24 @@ impl Plane<'_> {
14361456 // samples above or left of it at all, same as `y == 0`/`x == 0`.
14371457 let across = across.min(self.tile_x1);
14381458 let down = down.min(self.tile_y1);
1439- let above = (y > self.tile_y0 && across > x)
1440- .then(|| self.reconstruction[(y - 1) * self.width + x..][..across - x].to_vec());
1441- let left = (x > self.tile_x0 && down > y).then(|| {
1442- (y..down)
1443- .map(|row| self.reconstruction[row * self.width + x - 1])
1444- .collect::<Vec<_>>()
1445- });
1459+ let above_n = (y > self.tile_y0 && across > x).then(|| across - x);
1460+ if let Some(n) = above_n {
1461+ above_buf[..n]
1462+ .copy_from_slice(&self.reconstruction[(y - 1) * self.width + x..][..n]);
1463+ }
1464+ let left_n = (x > self.tile_x0 && down > y).then(|| down - y);
1465+ if let Some(n) = left_n {
1466+ for (d, row) in left_buf[..n].iter_mut().zip(y..down) {
1467+ *d = self.reconstruction[row * self.width + x - 1];
1468+ }
1469+ }
14461470 let corner = (x > self.tile_x0 && y > self.tile_y0)
14471471 .then(|| self.reconstruction[(y - 1) * self.width + x - 1]);
1448- (above, left, corner)
1472+ (
1473+ above_n.map(|n| &above_buf[..n]),
1474+ left_n.map(|n| &left_buf[..n]),
1475+ corner,
1476+ )
14491477 }
14501478
14511479 /// Codes one block under one mode without committing it: hands back the
@@ -1507,7 +1535,9 @@ impl Plane<'_> {
15071535 let At {
15081536 x, y, side, reach, ..
15091537 } = at;
1510- let (above, left, corner) = self.edges(x, y, side, reach);
1538+ let (mut above_buf, mut left_buf) = ([0u8; 2 * BLOCK], [0u8; 2 * BLOCK]);
1539+ let (above, left, corner) =
1540+ self.edges_into(x, y, side, reach, &mut above_buf, &mut left_buf);
15111541 // Per-candidate scratch on the search's hottest path, so both buffers
15121542 // live on the stack -- `side` is at most `BLOCK` -- rather than
15131543 // costing a malloc/free pair per trial (`mc_trial` already does this).
@@ -1516,8 +1546,8 @@ impl Plane<'_> {
15161546 intra_predict_u8(
15171547 mode,
15181548 0,
1519- above.as_deref(),
1520- left.as_deref(),
1549+ above,
1550+ left,
15211551 corner,
15221552 side,
15231553 side,
@@ -1539,16 +1569,28 @@ impl Plane<'_> {
15391569 let t = std::time::Instant::now();
15401570 let levels =
15411571 forward_and_quantize_typed(residual, side, 8, i32::from(base_q_idx), deadzone, tx_type);
1542- let coded =
1543- dequant_and_inverse_typed(&levels, side, 8, i32::from(base_q_idx), 0, 0, tx_type);
1572+ // lane-av1speed2: 60% of the transform units of an inter stream carry
1573+ // no coefficient at all, and the square entry point re-inflates that
1574+ // case into `side * side` zeros (one allocate-and-memset per trial)
1575+ // only for the sum below to add them to the prediction. The `_wh`
1576+ // entry hands back the empty all-zero marker instead, and a zero
1577+ // residual reconstructs to the prediction itself -- `clamp(p + 0)` is
1578+ // `p` for a `u8`, so the bytes are the same.
1579+ let coded = dequant_and_inverse_typed_wh(
1580+ &levels, side, side, 8, i32::from(base_q_idx), 0, 0, tx_type,
1581+ );
15441582 #[cfg(test)]
15451583 stage_add(2, t.elapsed());
15461584
1547- let reconstruction: Vec<u8> = prediction
1548- .iter()
1549- .zip(&coded)
1550- .map(|(&p, &c)| (i32::from(p) + c).clamp(0, 255) as u8)
1551- .collect();
1585+ let reconstruction: Vec<u8> = if coded.is_empty() {
1586+ prediction.to_vec()
1587+ } else {
1588+ prediction
1589+ .iter()
1590+ .zip(&coded)
1591+ .map(|(&p, &c)| (i32::from(p) + c).clamp(0, 255) as u8)
1592+ .collect()
1593+ };
15521594 let sse = self.block_sse(x, y, side, &reconstruction);
15531595 // What the levels cost is priced through the same CDFs the tile writer
15541596 // will code them with, so the search ranks modes -- and the partition
@@ -1626,14 +1668,28 @@ impl Plane<'_> {
16261668 #[cfg(test)]
16271669 let t = std::time::Instant::now();
16281670 let levels = forward_and_quantize(residual, side, 8, i32::from(base_q_idx), deadzone);
1629- let coded = dequant_and_inverse(&levels, side, 8, i32::from(base_q_idx));
1671+ // The same all-zero shortcut [`Self::trial_typed`] takes.
1672+ let coded = dequant_and_inverse_typed_wh(
1673+ &levels,
1674+ side,
1675+ side,
1676+ 8,
1677+ i32::from(base_q_idx),
1678+ 0,
1679+ 0,
1680+ TxType::DctDct,
1681+ );
16301682 #[cfg(test)]
16311683 stage_add(2, t.elapsed());
1632- let reconstruction: Vec<u8> = prediction
1633- .iter()
1634- .zip(&coded)
1635- .map(|(&p, &c)| (i32::from(p) + c).clamp(0, 255) as u8)
1636- .collect();
1684+ let reconstruction: Vec<u8> = if coded.is_empty() {
1685+ prediction.to_vec()
1686+ } else {
1687+ prediction
1688+ .iter()
1689+ .zip(&coded)
1690+ .map(|(&p, &c)| (i32::from(p) + c).clamp(0, 255) as u8)
1691+ .collect()
1692+ };
16371693 let sse = self.block_sse(x, y, side, &reconstruction);
16381694 #[cfg(test)]
16391695 let t = std::time::Instant::now();
@@ -1894,7 +1950,9 @@ impl Plane<'_> {
18941950 let At {
18951951 x, y, side, reach, ..
18961952 } = at;
1897- let (above, left, corner) = self.edges(x, y, side, reach);
1953+ let (mut above_buf, mut left_buf) = ([0u8; 2 * BLOCK], [0u8; 2 * BLOCK]);
1954+ let (above, left, corner) =
1955+ self.edges_into(x, y, side, reach, &mut above_buf, &mut left_buf);
18981956 modes
18991957 .iter()
19001958 .map(|&mode| {
@@ -1902,8 +1960,8 @@ impl Plane<'_> {
19021960 intra_predict_u8(
19031961 mode,
19041962 0,
1905- above.as_deref(),
1906- left.as_deref(),
1963+ above,
1964+ left,
19071965 corner,
19081966 side,
19091967 side,
@@ -1936,7 +1994,9 @@ impl Plane<'_> {
19361994 let At {
19371995 x, y, side, reach, ..
19381996 } = at;
1939- let (above, left, corner) = self.edges(x, y, side, reach);
1997+ let (mut above_buf, mut left_buf) = ([0u8; 2 * BLOCK], [0u8; 2 * BLOCK]);
1998+ let (above, left, corner) =
1999+ self.edges_into(x, y, side, reach, &mut above_buf, &mut left_buf);
19402000 // Nothing ranks by the SAD pre-pass unless `top_k` prunes by it (or
19412001 // the census reports it), so summing it under the default unpruned
19422002 // search is `side * side` absolute differences per mode spent on a
@@ -1950,8 +2010,8 @@ impl Plane<'_> {
19502010 intra_predict_u8(
19512011 mode,
19522012 0,
1953- above.as_deref(),
1954- left.as_deref(),
2013+ above,
2014+ left,
19552015 corner,
19562016 side,
19572017 side,
@@ -11039,7 +11099,7 @@ mod tests {
1103911099
1104011100 let t = Instant::now();
1104111101 for _ in 0..N {
11042- std::hint::black_box(dequant_and_inverse(&levels, side, 8, 100));
11102+ std::hint::black_box(crate::transform::dequant_and_inverse(&levels, side, 8, 100));
1104311103 }
1104411104 let inverse_t = t.elapsed() / N;
1104511105
crates/ec-av1/src/tile.rsmodified+17 -2
@@ -4257,14 +4257,29 @@ pub(crate) fn coeff_bits(
42574257 write_coeffs(&mut enc, &mut coding, grid, scan, skip_ctx, sign_ctx, None);
42584258 enc.bits()
42594259 };
4260+ // lane-av1speed2: 60% of the transform units of an inter stream hold no
4261+ // coefficient, and for those `write_coeffs` codes ONE symbol -- the
4262+ // all-zero flag -- and returns. Restoring the eight tables it might have
4263+ // touched (~800 bytes of `copy_from_slice`, the largest `memmove` in the
4264+ // encoder's profile) buys nothing there, so price that symbol straight
4265+ // off the pristine table on a copy of its own row. Identical bits: the
4266+ // scratch's `txb_skip[skip_ctx]` was restored from exactly this row, and
4267+ // nothing after the flag runs.
4268+ let zero_price = |slot: &mut (Cdfs, Cdfs)| {
4269+ let mut row = slot.0.txb(set, DC_PRED).txb_skip[skip_ctx];
4270+ let mut enc = SymbolEncoder::pricer();
4271+ enc.symbol(1, &mut row);
4272+ enc.bits()
4273+ };
4274+ let all_zero = grid.iter().all(|&level| level == 0);
42604275 PRICING_BASE.with_borrow_mut(|base| {
42614276 if let Some(slot) = base.as_deref_mut().filter(|s| s.0.q_ctx == q_ctx) {
4262- return price(slot);
4277+ return if all_zero { zero_price(slot) } else { price(slot) };
42634278 }
42644279 PRICING.with_borrow_mut(|slots| {
42654280 let slot = slots[q_ctx.min(3)]
42664281 .get_or_insert_with(|| Box::new((Cdfs::new(q_ctx), Cdfs::new(q_ctx))));
4267- price(slot)
4282+ if all_zero { zero_price(slot) } else { price(slot) }
42684283 })
42694284 })
42704285 }