Merge branch 'lane-av1-mot': stop repricing an inter block's chroma trial 13x
Browse files at this commit2 changed files +137 -15
- crates/ec-av1/src/encode.rs +131 -15
- crates/ec-av1/src/mc.rs +6 -0
crates/ec-av1/src/encode.rsmodified+131 -15
| @@ -578,8 +578,12 @@ impl Plane<'_> { | |||
| 578 | - i32::from(prediction[row * side + col]); | 578 | - i32::from(prediction[row * side + col]); |
| 579 | } | 579 | } |
| 580 | } | 580 | } |
| 581 | + #[cfg(test)] | ||
| 582 | + let t = std::time::Instant::now(); | ||
| 581 | let levels = forward_and_quantize(&residual, side, 8, i32::from(base_q_idx), deadzone); | 583 | let levels = forward_and_quantize(&residual, side, 8, i32::from(base_q_idx), deadzone); |
| 582 | let coded = dequant_and_inverse(&levels, side, 8, i32::from(base_q_idx)); | 584 | let coded = dequant_and_inverse(&levels, side, 8, i32::from(base_q_idx)); |
| 585 | + #[cfg(test)] | ||
| 586 | + stage_add(2, t.elapsed()); | ||
| 583 | 587 | ||
| 584 | let mut reconstruction = vec![0u8; side * side]; | 588 | let mut reconstruction = vec![0u8; side * side]; |
| 585 | let mut sse = 0.0; | 589 | let mut sse = 0.0; |
| @@ -597,6 +601,8 @@ impl Plane<'_> { | |||
| 597 | // What the levels cost is priced through the same CDFs the tile writer | 601 | // What the levels cost is priced through the same CDFs the tile writer |
| 598 | // will code them with, so the search ranks modes -- and the partition | 602 | // will code them with, so the search ranks modes -- and the partition |
| 599 | // trial ranks trees -- by the bits they actually spend. | 603 | // trial ranks trees -- by the bits they actually spend. |
| 604 | + #[cfg(test)] | ||
| 605 | + let t = std::time::Instant::now(); | ||
| 600 | let bits = if cfg!(test) && std::env::var_os("EC_AV1_ESTIMATE").is_some() { | 606 | let bits = if cfg!(test) && std::env::var_os("EC_AV1_ESTIMATE").is_some() { |
| 601 | // What the search used before it could price a block exactly: a | 607 | // What the search used before it could price a block exactly: a |
| 602 | // level costs its magnitude's width plus a sign and a run. Kept | 608 | // level costs its magnitude's width plus a sign and a run. Kept |
| @@ -610,6 +616,8 @@ impl Plane<'_> { | |||
| 610 | } else { | 616 | } else { |
| 611 | crate::tile::coeff_bits(&levels, at.set) | 617 | crate::tile::coeff_bits(&levels, at.set) |
| 612 | }; | 618 | }; |
| 619 | + #[cfg(test)] | ||
| 620 | + stage_add(3, t.elapsed()); | ||
| 613 | Trial { | 621 | Trial { |
| 614 | levels, | 622 | levels, |
| 615 | reconstruction, | 623 | reconstruction, |
| @@ -662,8 +670,12 @@ impl Plane<'_> { | |||
| 662 | - i32::from(prediction[row * side + col]); | 670 | - i32::from(prediction[row * side + col]); |
| 663 | } | 671 | } |
| 664 | } | 672 | } |
| 673 | + #[cfg(test)] | ||
| 674 | + let t = std::time::Instant::now(); | ||
| 665 | let levels = forward_and_quantize(&residual, side, 8, i32::from(base_q_idx), deadzone); | 675 | let levels = forward_and_quantize(&residual, side, 8, i32::from(base_q_idx), deadzone); |
| 666 | let coded = dequant_and_inverse(&levels, side, 8, i32::from(base_q_idx)); | 676 | let coded = dequant_and_inverse(&levels, side, 8, i32::from(base_q_idx)); |
| 677 | + #[cfg(test)] | ||
| 678 | + stage_add(2, t.elapsed()); | ||
| 667 | let mut reconstruction = vec![0u8; side * side]; | 679 | let mut reconstruction = vec![0u8; side * side]; |
| 668 | let mut sse = 0.0; | 680 | let mut sse = 0.0; |
| 669 | for row in 0..side { | 681 | for row in 0..side { |
| @@ -677,6 +689,8 @@ impl Plane<'_> { | |||
| 677 | sse += error * error; | 689 | sse += error * error; |
| 678 | } | 690 | } |
| 679 | } | 691 | } |
| 692 | + #[cfg(test)] | ||
| 693 | + let t = std::time::Instant::now(); | ||
| 680 | let bits = if cfg!(test) && std::env::var_os("EC_AV1_ESTIMATE").is_some() { | 694 | let bits = if cfg!(test) && std::env::var_os("EC_AV1_ESTIMATE").is_some() { |
| 681 | levels | 695 | levels |
| 682 | .iter() | 696 | .iter() |
| @@ -686,6 +700,8 @@ impl Plane<'_> { | |||
| 686 | } else { | 700 | } else { |
| 687 | crate::tile::coeff_bits(&levels, set) | 701 | crate::tile::coeff_bits(&levels, set) |
| 688 | }; | 702 | }; |
| 703 | + #[cfg(test)] | ||
| 704 | + stage_add(3, t.elapsed()); | ||
| 689 | Trial { | 705 | Trial { |
| 690 | levels, | 706 | levels, |
| 691 | reconstruction, | 707 | reconstruction, |
| @@ -1123,7 +1139,44 @@ fn set_test_top_k_override(value: Option<usize>) { | |||
| 1123 | /// lever. Set from the sweep in the lane report if the quality gate clears it. | 1139 | /// lever. Set from the sweep in the lane report if the quality gate clears it. |
| 1124 | const PRUNE_TOP_K: Option<usize> = None; | 1140 | const PRUNE_TOP_K: Option<usize> = None; |
| 1125 | 1141 | ||
| 1142 | +/// Per-thread nanosecond counters `stage_timing_breakdown_inter` (and | ||
| 1143 | +/// [`crate::mc::predict`], through [`stage_add`]) accumulate into, so an | ||
| 1144 | +/// inter frame's cost can be attributed to a bucket without changing what | ||
| 1145 | +/// the search actually does. Index: 0 = whole-call time inside | ||
| 1146 | +/// [`motion::search`] (includes bucket 1's time, run from inside it), 1 = | ||
| 1147 | +/// [`crate::mc::predict`] (every call, from the motion search's own | ||
| 1148 | +/// candidates and from a committed inter block's final prediction alike), | ||
| 1149 | +/// 2 = `forward_and_quantize` + `dequant_and_inverse`, 3 = `coeff_bits`. | ||
| 1150 | +#[cfg(test)] | ||
| 1151 | +thread_local! { | ||
| 1152 | + static STAGE_NS: [std::cell::Cell<u64>; 4] = [ | ||
| 1153 | + std::cell::Cell::new(0), | ||
| 1154 | + std::cell::Cell::new(0), | ||
| 1155 | + std::cell::Cell::new(0), | ||
| 1156 | + std::cell::Cell::new(0), | ||
| 1157 | + ]; | ||
| 1158 | +} | ||
| 1159 | + | ||
| 1160 | +#[cfg(test)] | ||
| 1161 | +pub(crate) fn stage_add(bucket: usize, dur: std::time::Duration) { | ||
| 1162 | + STAGE_NS.with(|c| c[bucket].set(c[bucket].get() + dur.as_nanos() as u64)); | ||
| 1163 | +} | ||
| 1164 | + | ||
| 1165 | +#[cfg(test)] | ||
| 1166 | +fn stage_reset() { | ||
| 1167 | + STAGE_NS.with(|c| c.iter().for_each(|cell| cell.set(0))); | ||
| 1168 | +} | ||
| 1169 | + | ||
| 1170 | +#[cfg(test)] | ||
| 1171 | +fn stage_read() -> [std::time::Duration; 4] { | ||
| 1172 | + STAGE_NS.with(|c| { | ||
| 1173 | + c.each_ref() | ||
| 1174 | + .map(|cell| std::time::Duration::from_nanos(cell.get())) | ||
| 1175 | + }) | ||
| 1176 | +} | ||
| 1177 | + | ||
| 1126 | /// One mode's coding of one block, before it is committed. | 1178 | /// One mode's coding of one block, before it is committed. |
| 1179 | +#[derive(Clone)] | ||
| 1127 | struct Trial { | 1180 | struct Trial { |
| 1128 | levels: Vec<i32>, | 1181 | levels: Vec<i32>, |
| 1129 | reconstruction: Vec<u8>, | 1182 | reconstruction: Vec<u8>, |
| @@ -1669,20 +1722,15 @@ fn search_inter_block( | |||
| 1669 | } | 1722 | } |
| 1670 | }; | 1723 | }; |
| 1671 | 1724 | ||
| 1672 | - for &mode in search.modes { | 1725 | + // The chroma trial an intra candidate prices does not depend on `mode` |
| 1673 | - let luma_trial = luma.trial( | 1726 | + // at all -- both planes always code `DC_PRED` here (a coding choice this |
| 1674 | - At { | 1727 | + // search inherited, not this loop's to second-guess) at the same `reach: |
| 1675 | - x, | 1728 | + // Reach::none()` -- so it is the same call with the same inputs on every |
| 1676 | - y, | 1729 | + // one of the thirteen iterations below; run it once and reuse the result |
| 1677 | - side: BLOCK, | 1730 | + // rather than repeat an already-priced transform/quantize/entropy call |
| 1678 | - reach, | 1731 | + // twelve times for nothing (bit-identical: the value each iteration |
| 1679 | - set: luma_set, | 1732 | + // reads back is exactly what it used to recompute). |
| 1680 | - }, | 1733 | + let u_trial = chroma[0].trial( |
| 1681 | - mode, | ||
| 1682 | - search.base_q_idx, | ||
| 1683 | - search.deadzone, | ||
| 1684 | - ); | ||
| 1685 | - let u = chroma[0].trial( | ||
| 1686 | At { | 1734 | At { |
| 1687 | x: x / 2, | 1735 | x: x / 2, |
| 1688 | y: y / 2, | 1736 | y: y / 2, |
| @@ -1694,7 +1742,7 @@ fn search_inter_block( | |||
| 1694 | search.base_q_idx, | 1742 | search.base_q_idx, |
| 1695 | search.deadzone, | 1743 | search.deadzone, |
| 1696 | ); | 1744 | ); |
| 1697 | - let v = chroma[1].trial( | 1745 | + let v_trial = chroma[1].trial( |
| 1698 | At { | 1746 | At { |
| 1699 | x: x / 2, | 1747 | x: x / 2, |
| 1700 | y: y / 2, | 1748 | y: y / 2, |
| @@ -1706,6 +1754,22 @@ fn search_inter_block( | |||
| 1706 | search.base_q_idx, | 1754 | search.base_q_idx, |
| 1707 | search.deadzone, | 1755 | search.deadzone, |
| 1708 | ); | 1756 | ); |
| 1757 | + | ||
| 1758 | + for &mode in search.modes { | ||
| 1759 | + let luma_trial = luma.trial( | ||
| 1760 | + At { | ||
| 1761 | + x, | ||
| 1762 | + y, | ||
| 1763 | + side: BLOCK, | ||
| 1764 | + reach, | ||
| 1765 | + set: luma_set, | ||
| 1766 | + }, | ||
| 1767 | + mode, | ||
| 1768 | + search.base_q_idx, | ||
| 1769 | + search.deadzone, | ||
| 1770 | + ); | ||
| 1771 | + let u = u_trial.clone(); | ||
| 1772 | + let v = v_trial.clone(); | ||
| 1709 | let cost = luma_trial.sse | 1773 | let cost = luma_trial.sse |
| 1710 | + u.sse | 1774 | + u.sse |
| 1711 | + v.sse | 1775 | + v.sse |
| @@ -1843,6 +1907,8 @@ fn search_inter_block( | |||
| 1843 | } | 1907 | } |
| 1844 | 1908 | ||
| 1845 | let source_block = luma.source_block(x, y, BLOCK); | 1909 | let source_block = luma.source_block(x, y, BLOCK); |
| 1910 | + #[cfg(test)] | ||
| 1911 | + let t = std::time::Instant::now(); | ||
| 1846 | let found = motion::search( | 1912 | let found = motion::search( |
| 1847 | ref_luma.0, | 1913 | ref_luma.0, |
| 1848 | ref_luma.1, | 1914 | ref_luma.1, |
| @@ -1856,6 +1922,8 @@ fn search_inter_block( | |||
| 1856 | stack.pred_mv, | 1922 | stack.pred_mv, |
| 1857 | search.lambda, | 1923 | search.lambda, |
| 1858 | ); | 1924 | ); |
| 1925 | + #[cfg(test)] | ||
| 1926 | + stage_add(0, t.elapsed()); | ||
| 1859 | let mv = round_to_valid_mv(found.mv, stack.pred_mv); | 1927 | let mv = round_to_valid_mv(found.mv, stack.pred_mv); |
| 1860 | if let Some(mv_bits) = mv_residual_bits(mv, stack.pred_mv) { | 1928 | if let Some(mv_bits) = mv_residual_bits(mv, stack.pred_mv) { |
| 1861 | let luma_trial = mc_trial( | 1929 | let luma_trial = mc_trial( |
| @@ -3684,6 +3752,54 @@ mod tests { | |||
| 3684 | } | 3752 | } |
| 3685 | } | 3753 | } |
| 3686 | 3754 | ||
| 3755 | + /// Breaks one inter frame's own cost into the four [`STAGE_NS`] buckets | ||
| 3756 | + /// (motion search, [`crate::mc::predict`], transform+quantize, | ||
| 3757 | + /// `coeff_bits`), plus what is left over once those are subtracted -- | ||
| 3758 | + /// the thirteen-mode intra-candidate loop `search_inter_block` still | ||
| 3759 | + /// runs on every inter block (its own transform/quant/entropy calls are | ||
| 3760 | + /// already inside buckets 2/3, so "left over" here is everything NOT | ||
| 3761 | + /// timed: `crate::intra::predict` for those 13 modes, RD bookkeeping, | ||
| 3762 | + /// symbol-cost lookups outside `coeff_bits`). | ||
| 3763 | + /// | ||
| 3764 | + /// `cargo test -p ec-av1 --release stage_timing_breakdown_inter -- --ignored --nocapture`. | ||
| 3765 | + #[test] | ||
| 3766 | + #[ignore = "a perf probe, not a gate"] | ||
| 3767 | + fn stage_timing_breakdown_inter() { | ||
| 3768 | + use std::time::Instant; | ||
| 3769 | + | ||
| 3770 | + let (width, height) = (1280usize, 720); | ||
| 3771 | + let picture = test_card(width, height); | ||
| 3772 | + let key = encode_key_frame(&picture, 100, 0.5).unwrap(); | ||
| 3773 | + let padded_ref = key.reconstruction.padded_to(SUPERBLOCK); | ||
| 3774 | + let padded_pic = picture.padded_to(SUPERBLOCK); | ||
| 3775 | + | ||
| 3776 | + stage_reset(); | ||
| 3777 | + let t = Instant::now(); | ||
| 3778 | + let inter = | ||
| 3779 | + encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, (width, height)).unwrap(); | ||
| 3780 | + let total_t = t.elapsed(); | ||
| 3781 | + let [motion_search, interpolation, transform_quant, entropy] = stage_read(); | ||
| 3782 | + let accounted = motion_search + transform_quant + entropy; | ||
| 3783 | + // motion_search already contains interpolation's own time (every | ||
| 3784 | + // candidate the search costs calls crate::mc::predict), so it is not | ||
| 3785 | + // added again here -- interpolation is reported alongside as "of | ||
| 3786 | + // which interpolation", not as a fifth additive bucket. | ||
| 3787 | + let other = total_t.saturating_sub(accounted); | ||
| 3788 | + | ||
| 3789 | + eprintln!( | ||
| 3790 | + "\n=== inter frame {width}x{height}, one frame ===\n\ | ||
| 3791 | + total: {total_t:>9.2?} ({} bytes)\n\ | ||
| 3792 | + motion search (NEWMV): {motion_search:>9.2?} (of which interpolation \ | ||
| 3793 | + {interpolation:>9.2?})\n\ | ||
| 3794 | + transform + quantize: {transform_quant:>9.2?}\n\ | ||
| 3795 | + coeff_bits (entropy pricing): {entropy:>9.2?}\n\ | ||
| 3796 | + everything else (13-mode intra {other:>9.2?}\n\ | ||
| 3797 | + candidate loop's own predict/bookkeeping, NEARESTMV's own \ | ||
| 3798 | + predict, RD glue):", | ||
| 3799 | + inter.stream.len(), | ||
| 3800 | + ); | ||
| 3801 | + } | ||
| 3802 | + | ||
| 3687 | /// The quality gate [`Search::top_k`]'s lever lives or dies by: three real | 3803 | /// The quality gate [`Search::top_k`]'s lever lives or dies by: three real |
| 3688 | /// frames (a dark/flat one, a detailed one, a normal film one) at two | 3804 | /// frames (a dark/flat one, a detailed one, a normal film one) at two |
| 3689 | /// `base_q_idx` values, encoded with the mode search unpruned (`top_k = | 3805 | /// `base_q_idx` values, encoded with the mode search unpruned (`top_k = |
crates/ec-av1/src/mc.rsmodified+6 -0
| @@ -103,6 +103,9 @@ pub fn predict( | |||
| 103 | assert_eq!(dst.len(), block_w * block_h, "the destination is the block"); | 103 | assert_eq!(dst.len(), block_w * block_h, "the destination is the block"); |
| 104 | assert!(!reference.is_empty(), "a reference plane has samples"); | 104 | assert!(!reference.is_empty(), "a reference plane has samples"); |
| 105 | 105 | ||
| 106 | + #[cfg(test)] | ||
| 107 | + let stage_t = std::time::Instant::now(); | ||
| 108 | + | ||
| 106 | let x0 = x_q4.div_euclid(16); | 109 | let x0 = x_q4.div_euclid(16); |
| 107 | let xfrac = x_q4.rem_euclid(16) as usize; | 110 | let xfrac = x_q4.rem_euclid(16) as usize; |
| 108 | let y0 = y_q4.div_euclid(16); | 111 | let y0 = y_q4.div_euclid(16); |
| @@ -136,6 +139,9 @@ pub fn predict( | |||
| 136 | dst[row * block_w + col] = round2(sum, INTER_ROUND_1).clamp(0, 255) as u8; | 139 | dst[row * block_w + col] = round2(sum, INTER_ROUND_1).clamp(0, 255) as u8; |
| 137 | } | 140 | } |
| 138 | } | 141 | } |
| 142 | + | ||
| 143 | + #[cfg(test)] | ||
| 144 | + crate::encode::stage_add(1, stage_t.elapsed()); | ||
| 139 | } | 145 | } |
| 140 | 146 | ||
| 141 | #[cfg(test)] | 147 | #[cfg(test)] |