Merge branch 'lane-av1speed2' (e65b15a0): zero-residual short-circuits and stack intra trial buffers, byte-exact
Browse files at this commit2 changed files +127 -52
- crates/ec-av1/src/encode.rs +110 -50
- crates/ec-av1/src/tile.rs +17 -2
crates/ec-av1/src/encode.rsmodified+110 -50
| @@ -41,7 +41,7 @@ use crate::tile::{ | ||
| 41 | 41 | BlockCoeffs, Coeff, INTRA_MODE_CTX, InterInfo, InterMode, Quadrant, Superblock, partition_bits, |
| 42 | 42 | }; |
| 43 | 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 | 45 | forward_and_quantize_typed, |
| 46 | 46 | }; |
| 47 | 47 | |
| @@ -689,7 +689,12 @@ fn intra_predict_u8( | ||
| 689 | 689 | // a block is at most `BLOCK * BLOCK`. Same samples either way. |
| 690 | 690 | let mut above_buf = [0u16; 2 * BLOCK]; |
| 691 | 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 | 698 | let widen = |src: &[u8], buf: &mut [u16; 2 * BLOCK]| { |
| 694 | 699 | for (d, &v) in buf[..src.len()].iter_mut().zip(src) { |
| 695 | 700 | *d = u16::from(v); |
| @@ -703,21 +708,30 @@ fn intra_predict_u8( | ||
| 703 | 708 | } |
| 704 | 709 | let above16 = above.map(|s| &above_buf[..s.len()]); |
| 705 | 710 | 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]); | |
| 721 | 735 | } |
| 722 | 736 | } |
| 723 | 737 | |
| @@ -1392,13 +1406,19 @@ impl Plane<'_> { | ||
| 1392 | 1406 | /// they are not, or where the frame ends first, the edge is shorter and |
| 1393 | 1407 | /// the predictor repeats its last sample, exactly as the decoder's clamp |
| 1394 | 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 | 1414 | &self, |
| 1397 | 1415 | x: usize, |
| 1398 | 1416 | y: usize, |
| 1399 | 1417 | side: usize, |
| 1400 | 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 | 1422 | // Even a block's *own* edge is clamped to the true frame bound, not |
| 1403 | 1423 | // just a `reach` extension: libaom's `av1_predict_intra_block` |
| 1404 | 1424 | // (`av1/common/reconintra.c`) derives `xr`/`yd` -- the distance from |
| @@ -1436,16 +1456,24 @@ impl Plane<'_> { | ||
| 1436 | 1456 | // samples above or left of it at all, same as `y == 0`/`x == 0`. |
| 1437 | 1457 | let across = across.min(self.tile_x1); |
| 1438 | 1458 | 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 | + } | |
| 1446 | 1470 | let corner = (x > self.tile_x0 && y > self.tile_y0) |
| 1447 | 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 | 1479 | /// Codes one block under one mode without committing it: hands back the |
| @@ -1507,7 +1535,9 @@ impl Plane<'_> { | ||
| 1507 | 1535 | let At { |
| 1508 | 1536 | x, y, side, reach, .. |
| 1509 | 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 | 1541 | // Per-candidate scratch on the search's hottest path, so both buffers |
| 1512 | 1542 | // live on the stack -- `side` is at most `BLOCK` -- rather than |
| 1513 | 1543 | // costing a malloc/free pair per trial (`mc_trial` already does this). |
| @@ -1516,8 +1546,8 @@ impl Plane<'_> { | ||
| 1516 | 1546 | intra_predict_u8( |
| 1517 | 1547 | mode, |
| 1518 | 1548 | 0, |
| 1519 | - above.as_deref(), | |
| 1520 | - left.as_deref(), | |
| 1549 | + above, | |
| 1550 | + left, | |
| 1521 | 1551 | corner, |
| 1522 | 1552 | side, |
| 1523 | 1553 | side, |
| @@ -1539,16 +1569,28 @@ impl Plane<'_> { | ||
| 1539 | 1569 | let t = std::time::Instant::now(); |
| 1540 | 1570 | let levels = |
| 1541 | 1571 | 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 | + ); | |
| 1544 | 1582 | #[cfg(test)] |
| 1545 | 1583 | stage_add(2, t.elapsed()); |
| 1546 | 1584 | |
| 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 | + }; | |
| 1552 | 1594 | let sse = self.block_sse(x, y, side, &reconstruction); |
| 1553 | 1595 | // What the levels cost is priced through the same CDFs the tile writer |
| 1554 | 1596 | // will code them with, so the search ranks modes -- and the partition |
| @@ -1626,14 +1668,28 @@ impl Plane<'_> { | ||
| 1626 | 1668 | #[cfg(test)] |
| 1627 | 1669 | let t = std::time::Instant::now(); |
| 1628 | 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 | 1682 | #[cfg(test)] |
| 1631 | 1683 | 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 | + }; | |
| 1637 | 1693 | let sse = self.block_sse(x, y, side, &reconstruction); |
| 1638 | 1694 | #[cfg(test)] |
| 1639 | 1695 | let t = std::time::Instant::now(); |
| @@ -1894,7 +1950,9 @@ impl Plane<'_> { | ||
| 1894 | 1950 | let At { |
| 1895 | 1951 | x, y, side, reach, .. |
| 1896 | 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 | 1956 | modes |
| 1899 | 1957 | .iter() |
| 1900 | 1958 | .map(|&mode| { |
| @@ -1902,8 +1960,8 @@ impl Plane<'_> { | ||
| 1902 | 1960 | intra_predict_u8( |
| 1903 | 1961 | mode, |
| 1904 | 1962 | 0, |
| 1905 | - above.as_deref(), | |
| 1906 | - left.as_deref(), | |
| 1963 | + above, | |
| 1964 | + left, | |
| 1907 | 1965 | corner, |
| 1908 | 1966 | side, |
| 1909 | 1967 | side, |
| @@ -1936,7 +1994,9 @@ impl Plane<'_> { | ||
| 1936 | 1994 | let At { |
| 1937 | 1995 | x, y, side, reach, .. |
| 1938 | 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 | 2000 | // Nothing ranks by the SAD pre-pass unless `top_k` prunes by it (or |
| 1941 | 2001 | // the census reports it), so summing it under the default unpruned |
| 1942 | 2002 | // search is `side * side` absolute differences per mode spent on a |
| @@ -1950,8 +2010,8 @@ impl Plane<'_> { | ||
| 1950 | 2010 | intra_predict_u8( |
| 1951 | 2011 | mode, |
| 1952 | 2012 | 0, |
| 1953 | - above.as_deref(), | |
| 1954 | - left.as_deref(), | |
| 2013 | + above, | |
| 2014 | + left, | |
| 1955 | 2015 | corner, |
| 1956 | 2016 | side, |
| 1957 | 2017 | side, |
| @@ -11039,7 +11099,7 @@ mod tests { | ||
| 11039 | 11099 | |
| 11040 | 11100 | let t = Instant::now(); |
| 11041 | 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 | 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 | 4257 | write_coeffs(&mut enc, &mut coding, grid, scan, skip_ctx, sign_ctx, None); |
| 4258 | 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 | 4275 | PRICING_BASE.with_borrow_mut(|base| { |
| 4261 | 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 | 4279 | PRICING.with_borrow_mut(|slots| { |
| 4265 | 4280 | let slot = slots[q_ctx.min(3)] |
| 4266 | 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 | } |