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::{
41 BlockCoeffs, Coeff, INTRA_MODE_CTX, InterInfo, InterMode, Quadrant, Superblock, partition_bits,41 BlockCoeffs, Coeff, INTRA_MODE_CTX, InterInfo, InterMode, Quadrant, Superblock, partition_bits,
42 };42 };
43 use crate::transform::{43 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,
45 forward_and_quantize_typed,45 forward_and_quantize_typed,
46 };46 };
47 47
@@ -689,7 +689,12 @@ fn intra_predict_u8(
689 // a block is at most `BLOCK * BLOCK`. Same samples either way.689 // a block is at most `BLOCK * BLOCK`. Same samples either way.
690 let mut above_buf = [0u16; 2 * BLOCK];690 let mut above_buf = [0u16; 2 * BLOCK];
691 let mut left_buf = [0u16; 2 * BLOCK];691 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+
693 let widen = |src: &[u8], buf: &mut [u16; 2 * BLOCK]| {698 let widen = |src: &[u8], buf: &mut [u16; 2 * BLOCK]| {
694 for (d, &v) in buf[..src.len()].iter_mut().zip(src) {699 for (d, &v) in buf[..src.len()].iter_mut().zip(src) {
695 *d = u16::from(v);700 *d = u16::from(v);
@@ -703,21 +708,30 @@ fn intra_predict_u8(
703 }708 }
704 let above16 = above.map(|s| &above_buf[..s.len()]);709 let above16 = above.map(|s| &above_buf[..s.len()]);
705 let left16 = left.map(|s| &left_buf[..s.len()]);710 let left16 = left.map(|s| &left_buf[..s.len()]);
706- let dst16 = &mut dst_buf[..dst.len()];711+ let mut run = |dst16: &mut [u16]| {
707- crate::intra::predict(712+ crate::intra::predict(
708- mode,713+ mode,
709- angle_delta,714+ angle_delta,
710- above16,715+ above16,
711- left16,716+ left16,
712- corner.map(u16::from),717+ corner.map(u16::from),
713- bw,718+ bw,
714- bh,719+ bh,
715- enable_edge_filter,720+ enable_edge_filter,
716- smooth_neighbor,721+ smooth_neighbor,
717- dst16, fctx,722+ dst16, fctx,
718- );723+ );
719- for (d, &s) in dst.iter_mut().zip(dst16.iter()) {724+ for (d, &s) in dst.iter_mut().zip(dst16.iter()) {
720- *d = s as u8;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]);
721 }735 }
722 }736 }
723 737
@@ -1392,13 +1406,19 @@ impl Plane<'_> {
1392 /// they are not, or where the frame ends first, the edge is shorter and1406 /// they are not, or where the frame ends first, the edge is shorter and
1393 /// the predictor repeats its last sample, exactly as the decoder's clamp1407 /// the predictor repeats its last sample, exactly as the decoder's clamp
1394 /// to `aboveLimit` and `leftLimit` does (spec 7.11.2.2).1408 /// 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>(
1396 &self,1414 &self,
1397 x: usize,1415 x: usize,
1398 y: usize,1416 y: usize,
1399 side: usize,1417 side: usize,
1400 reach: Reach,1418 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>) {
1402 // Even a block's *own* edge is clamped to the true frame bound, not1422 // Even a block's *own* edge is clamped to the true frame bound, not
1403 // just a `reach` extension: libaom's `av1_predict_intra_block`1423 // just a `reach` extension: libaom's `av1_predict_intra_block`
1404 // (`av1/common/reconintra.c`) derives `xr`/`yd` -- the distance from1424 // (`av1/common/reconintra.c`) derives `xr`/`yd` -- the distance from
@@ -1436,16 +1456,24 @@ impl Plane<'_> {
1436 // samples above or left of it at all, same as `y == 0`/`x == 0`.1456 // samples above or left of it at all, same as `y == 0`/`x == 0`.
1437 let across = across.min(self.tile_x1);1457 let across = across.min(self.tile_x1);
1438 let down = down.min(self.tile_y1);1458 let down = down.min(self.tile_y1);
1439- let above = (y > self.tile_y0 && across > x)1459+ let above_n = (y > self.tile_y0 && across > x).then(|| across - x);
1440- .then(|| self.reconstruction[(y - 1) * self.width + x..][..across - x].to_vec());1460+ if let Some(n) = above_n {
1441- let left = (x > self.tile_x0 && down > y).then(|| {1461+ above_buf[..n]
1442- (y..down)1462+ .copy_from_slice(&self.reconstruction[(y - 1) * self.width + x..][..n]);
1443- .map(|row| self.reconstruction[row * self.width + x - 1])1463+ }
1444- .collect::<Vec<_>>()1464+ let left_n = (x > self.tile_x0 && down > y).then(|| down - y);
1445- });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+ }
1446 let corner = (x > self.tile_x0 && y > self.tile_y0)1470 let corner = (x > self.tile_x0 && y > self.tile_y0)
1447 .then(|| self.reconstruction[(y - 1) * self.width + x - 1]);1471 .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+ )
1449 }1477 }
1450 1478
1451 /// Codes one block under one mode without committing it: hands back the1479 /// Codes one block under one mode without committing it: hands back the
@@ -1507,7 +1535,9 @@ impl Plane<'_> {
1507 let At {1535 let At {
1508 x, y, side, reach, ..1536 x, y, side, reach, ..
1509 } = at;1537 } = 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);
1511 // Per-candidate scratch on the search's hottest path, so both buffers1541 // Per-candidate scratch on the search's hottest path, so both buffers
1512 // live on the stack -- `side` is at most `BLOCK` -- rather than1542 // live on the stack -- `side` is at most `BLOCK` -- rather than
1513 // costing a malloc/free pair per trial (`mc_trial` already does this).1543 // costing a malloc/free pair per trial (`mc_trial` already does this).
@@ -1516,8 +1546,8 @@ impl Plane<'_> {
1516 intra_predict_u8(1546 intra_predict_u8(
1517 mode,1547 mode,
1518 0,1548 0,
1519- above.as_deref(),1549+ above,
1520- left.as_deref(),1550+ left,
1521 corner,1551 corner,
1522 side,1552 side,
1523 side,1553 side,
@@ -1539,16 +1569,28 @@ impl Plane<'_> {
1539 let t = std::time::Instant::now();1569 let t = std::time::Instant::now();
1540 let levels =1570 let levels =
1541 forward_and_quantize_typed(residual, side, 8, i32::from(base_q_idx), deadzone, tx_type);1571 forward_and_quantize_typed(residual, side, 8, i32::from(base_q_idx), deadzone, tx_type);
1542- let coded =1572+ // lane-av1speed2: 60% of the transform units of an inter stream carry
1543- dequant_and_inverse_typed(&levels, side, 8, i32::from(base_q_idx), 0, 0, tx_type);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+ );
1544 #[cfg(test)]1582 #[cfg(test)]
1545 stage_add(2, t.elapsed());1583 stage_add(2, t.elapsed());
1546 1584
1547- let reconstruction: Vec<u8> = prediction1585+ let reconstruction: Vec<u8> = if coded.is_empty() {
1548- .iter()1586+ prediction.to_vec()
1549- .zip(&coded)1587+ } else {
1550- .map(|(&p, &c)| (i32::from(p) + c).clamp(0, 255) as u8)1588+ prediction
1551- .collect();1589+ .iter()
1590+ .zip(&coded)
1591+ .map(|(&p, &c)| (i32::from(p) + c).clamp(0, 255) as u8)
1592+ .collect()
1593+ };
1552 let sse = self.block_sse(x, y, side, &reconstruction);1594 let sse = self.block_sse(x, y, side, &reconstruction);
1553 // What the levels cost is priced through the same CDFs the tile writer1595 // What the levels cost is priced through the same CDFs the tile writer
1554 // will code them with, so the search ranks modes -- and the partition1596 // will code them with, so the search ranks modes -- and the partition
@@ -1626,14 +1668,28 @@ impl Plane<'_> {
1626 #[cfg(test)]1668 #[cfg(test)]
1627 let t = std::time::Instant::now();1669 let t = std::time::Instant::now();
1628 let levels = forward_and_quantize(residual, side, 8, i32::from(base_q_idx), deadzone);1670 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+ );
1630 #[cfg(test)]1682 #[cfg(test)]
1631 stage_add(2, t.elapsed());1683 stage_add(2, t.elapsed());
1632- let reconstruction: Vec<u8> = prediction1684+ let reconstruction: Vec<u8> = if coded.is_empty() {
1633- .iter()1685+ prediction.to_vec()
1634- .zip(&coded)1686+ } else {
1635- .map(|(&p, &c)| (i32::from(p) + c).clamp(0, 255) as u8)1687+ prediction
1636- .collect();1688+ .iter()
1689+ .zip(&coded)
1690+ .map(|(&p, &c)| (i32::from(p) + c).clamp(0, 255) as u8)
1691+ .collect()
1692+ };
1637 let sse = self.block_sse(x, y, side, &reconstruction);1693 let sse = self.block_sse(x, y, side, &reconstruction);
1638 #[cfg(test)]1694 #[cfg(test)]
1639 let t = std::time::Instant::now();1695 let t = std::time::Instant::now();
@@ -1894,7 +1950,9 @@ impl Plane<'_> {
1894 let At {1950 let At {
1895 x, y, side, reach, ..1951 x, y, side, reach, ..
1896 } = at;1952 } = 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);
1898 modes1956 modes
1899 .iter()1957 .iter()
1900 .map(|&mode| {1958 .map(|&mode| {
@@ -1902,8 +1960,8 @@ impl Plane<'_> {
1902 intra_predict_u8(1960 intra_predict_u8(
1903 mode,1961 mode,
1904 0,1962 0,
1905- above.as_deref(),1963+ above,
1906- left.as_deref(),1964+ left,
1907 corner,1965 corner,
1908 side,1966 side,
1909 side,1967 side,
@@ -1936,7 +1994,9 @@ impl Plane<'_> {
1936 let At {1994 let At {
1937 x, y, side, reach, ..1995 x, y, side, reach, ..
1938 } = at;1996 } = 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);
1940 // Nothing ranks by the SAD pre-pass unless `top_k` prunes by it (or2000 // Nothing ranks by the SAD pre-pass unless `top_k` prunes by it (or
1941 // the census reports it), so summing it under the default unpruned2001 // the census reports it), so summing it under the default unpruned
1942 // search is `side * side` absolute differences per mode spent on a2002 // search is `side * side` absolute differences per mode spent on a
@@ -1950,8 +2010,8 @@ impl Plane<'_> {
1950 intra_predict_u8(2010 intra_predict_u8(
1951 mode,2011 mode,
1952 0,2012 0,
1953- above.as_deref(),2013+ above,
1954- left.as_deref(),2014+ left,
1955 corner,2015 corner,
1956 side,2016 side,
1957 side,2017 side,
@@ -11039,7 +11099,7 @@ mod tests {
11039 11099
11040 let t = Instant::now();11100 let t = Instant::now();
11041 for _ in 0..N {11101 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));
11043 }11103 }
11044 let inverse_t = t.elapsed() / N;11104 let inverse_t = t.elapsed() / N;
11045 11105
crates/ec-av1/src/tile.rsmodified+17 -2
@@ -4257,14 +4257,29 @@ pub(crate) fn coeff_bits(
4257 write_coeffs(&mut enc, &mut coding, grid, scan, skip_ctx, sign_ctx, None);4257 write_coeffs(&mut enc, &mut coding, grid, scan, skip_ctx, sign_ctx, None);
4258 enc.bits()4258 enc.bits()
4259 };4259 };
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);
4260 PRICING_BASE.with_borrow_mut(|base| {4275 PRICING_BASE.with_borrow_mut(|base| {
4261 if let Some(slot) = base.as_deref_mut().filter(|s| s.0.q_ctx == q_ctx) {4276 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) };
4263 }4278 }
4264 PRICING.with_borrow_mut(|slots| {4279 PRICING.with_borrow_mut(|slots| {
4265 let slot = slots[q_ctx.min(3)]4280 let slot = slots[q_ctx.min(3)]
4266 .get_or_insert_with(|| Box::new((Cdfs::new(q_ctx), Cdfs::new(q_ctx))));4281 .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) }
4268 })4283 })
4269 })4284 })
4270 }4285 }