Skip the all-zero re-inflate and the pricer's table restore
Browse files at this commit8e31689dbe4287432178abea964cfc0544991d12Parent: c72674b- perf
- skip the all-zero re-inflate and the pricer's table restore
2 changed files +58 -17
- crates/ec-av1/src/encode.rs +41 -15
- crates/ec-av1/src/tile.rs +17 -2
crates/ec-av1/src/encode.rsmodified+41 -15
| @@ -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 | ||
| @@ -1539,16 +1539,28 @@ impl Plane<'_> { | |||
| 1539 | let t = std::time::Instant::now(); | 1539 | let t = std::time::Instant::now(); |
| 1540 | let levels = | 1540 | let levels = |
| 1541 | forward_and_quantize_typed(residual, side, 8, i32::from(base_q_idx), deadzone, tx_type); | 1541 | forward_and_quantize_typed(residual, side, 8, i32::from(base_q_idx), deadzone, tx_type); |
| 1542 | - let coded = | 1542 | + // 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); | 1543 | + // no coefficient at all, and the square entry point re-inflates that |
| 1544 | + // case into `side * side` zeros (one allocate-and-memset per trial) | ||
| 1545 | + // only for the sum below to add them to the prediction. The `_wh` | ||
| 1546 | + // entry hands back the empty all-zero marker instead, and a zero | ||
| 1547 | + // residual reconstructs to the prediction itself -- `clamp(p + 0)` is | ||
| 1548 | + // `p` for a `u8`, so the bytes are the same. | ||
| 1549 | + let coded = dequant_and_inverse_typed_wh( | ||
| 1550 | + &levels, side, side, 8, i32::from(base_q_idx), 0, 0, tx_type, | ||
| 1551 | + ); | ||
| 1544 | #[cfg(test)] | 1552 | #[cfg(test)] |
| 1545 | stage_add(2, t.elapsed()); | 1553 | stage_add(2, t.elapsed()); |
| 1546 | 1554 | ||
| 1547 | - let reconstruction: Vec<u8> = prediction | 1555 | + let reconstruction: Vec<u8> = if coded.is_empty() { |
| 1548 | - .iter() | 1556 | + prediction.to_vec() |
| 1549 | - .zip(&coded) | 1557 | + } else { |
| 1550 | - .map(|(&p, &c)| (i32::from(p) + c).clamp(0, 255) as u8) | 1558 | + prediction |
| 1551 | - .collect(); | 1559 | + .iter() |
| 1560 | + .zip(&coded) | ||
| 1561 | + .map(|(&p, &c)| (i32::from(p) + c).clamp(0, 255) as u8) | ||
| 1562 | + .collect() | ||
| 1563 | + }; | ||
| 1552 | let sse = self.block_sse(x, y, side, &reconstruction); | 1564 | let sse = self.block_sse(x, y, side, &reconstruction); |
| 1553 | // What the levels cost is priced through the same CDFs the tile writer | 1565 | // 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 partition | 1566 | // will code them with, so the search ranks modes -- and the partition |
| @@ -1626,14 +1638,28 @@ impl Plane<'_> { | |||
| 1626 | #[cfg(test)] | 1638 | #[cfg(test)] |
| 1627 | let t = std::time::Instant::now(); | 1639 | let t = std::time::Instant::now(); |
| 1628 | let levels = forward_and_quantize(residual, side, 8, i32::from(base_q_idx), deadzone); | 1640 | 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)); | 1641 | + // The same all-zero shortcut [`Self::trial_typed`] takes. |
| 1642 | + let coded = dequant_and_inverse_typed_wh( | ||
| 1643 | + &levels, | ||
| 1644 | + side, | ||
| 1645 | + side, | ||
| 1646 | + 8, | ||
| 1647 | + i32::from(base_q_idx), | ||
| 1648 | + 0, | ||
| 1649 | + 0, | ||
| 1650 | + TxType::DctDct, | ||
| 1651 | + ); | ||
| 1630 | #[cfg(test)] | 1652 | #[cfg(test)] |
| 1631 | stage_add(2, t.elapsed()); | 1653 | stage_add(2, t.elapsed()); |
| 1632 | - let reconstruction: Vec<u8> = prediction | 1654 | + let reconstruction: Vec<u8> = if coded.is_empty() { |
| 1633 | - .iter() | 1655 | + prediction.to_vec() |
| 1634 | - .zip(&coded) | 1656 | + } else { |
| 1635 | - .map(|(&p, &c)| (i32::from(p) + c).clamp(0, 255) as u8) | 1657 | + prediction |
| 1636 | - .collect(); | 1658 | + .iter() |
| 1659 | + .zip(&coded) | ||
| 1660 | + .map(|(&p, &c)| (i32::from(p) + c).clamp(0, 255) as u8) | ||
| 1661 | + .collect() | ||
| 1662 | + }; | ||
| 1637 | let sse = self.block_sse(x, y, side, &reconstruction); | 1663 | let sse = self.block_sse(x, y, side, &reconstruction); |
| 1638 | #[cfg(test)] | 1664 | #[cfg(test)] |
| 1639 | let t = std::time::Instant::now(); | 1665 | let t = std::time::Instant::now(); |
| @@ -11039,7 +11065,7 @@ mod tests { | |||
| 11039 | 11065 | ||
| 11040 | let t = Instant::now(); | 11066 | let t = Instant::now(); |
| 11041 | for _ in 0..N { | 11067 | for _ in 0..N { |
| 11042 | - std::hint::black_box(dequant_and_inverse(&levels, side, 8, 100)); | 11068 | + std::hint::black_box(crate::transform::dequant_and_inverse(&levels, side, 8, 100)); |
| 11043 | } | 11069 | } |
| 11044 | let inverse_t = t.elapsed() / N; | 11070 | let inverse_t = t.elapsed() / N; |
| 11045 | 11071 | ||
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 | } |