forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-av1speed' (715aa56a): encoder motion search reads the reference picture's u16 plane (was converting the whole plane per searched block), fft twiddles cached, trial scratch on the stack, NEWMV equal to NEARESTMV reuses its trials, integer SAD and a symbol price table; -45.6% encoder instructions with byte-identical output; key-frame top-K pruning and partition breakout measured as quality dead-ends and left inert

Browse files at this commit

0e574b50432983599867b33626436382187bf784Ahmet Kaan GümüşParent: a19201904ff5f8
crates/ec-av1/src/encode.rsmodified+151 -82
@@ -158,6 +158,30 @@ fn split_inter_blocks() -> bool {
158 }158 }
159 }159 }
160 160
161+/// libaom's `partition_search_breakout` shape: how many non-zero luma
162+/// coefficients per 256 luma samples a block may code and still be judged
163+/// well enough predicted that the split below it is not worth trying. `0`
164+/// disables the breakout (only the existing all-skip prune applies), which
165+/// is the default until the sweep below justifies otherwise.
166+///
167+/// Swept on the BD gate (`EC_AV1_SPLIT_BREAKOUT`, test builds only), which
168+/// is where this lane's wall would have gone: 1 cuts the three clips'
169+/// encode wall 6.3/6.0/6.2 s -> 3.6/3.2/4.4 s but costs +18.4/+24.0/+7.9
170+/// BD-rate points against libaom; 2 costs +23.4/+29.4/+18.8 and 4 costs
171+/// +31.7/+40.6/+27.8, for barely more speed. The split trial is where this
172+/// encoder's inter quality lives, so `0` ships and the knob stays for a
173+/// future explicit speed preset.
174+const SPLIT_BREAKOUT_COEFFS: usize = 0;
175+
176+/// [`SPLIT_BREAKOUT_COEFFS`], swept by `EC_AV1_SPLIT_BREAKOUT` in a test
177+/// build.
178+fn split_breakout_coeffs() -> usize {
179+ match std::env::var("EC_AV1_SPLIT_BREAKOUT").ok() {
180+ Some(v) if cfg!(test) => v.parse().unwrap_or(SPLIT_BREAKOUT_COEFFS),
181+ _ => SPLIT_BREAKOUT_COEFFS,
182+ }
183+}
184+
161 /// How often each partition decision was taken since the last185 /// How often each partition decision was taken since the last
162 /// [`take_partition_hits`], so a gate can report how often a new partition186 /// [`take_partition_hits`], so a gate can report how often a new partition
163 /// fires rather than assume it does (gate-blind-to-feature). Index 0 is an187 /// fires rather than assume it does (gate-blind-to-feature). Index 0 is an
@@ -1624,8 +1648,19 @@ impl Reach {
1624 /// What one symbol costs against a CDF, in bits.1648 /// What one symbol costs against a CDF, in bits.
1625 pub(crate) fn symbol_bits(cdf: &[u16], symbol: usize) -> f64 {1649 pub(crate) fn symbol_bits(cdf: &[u16], symbol: usize) -> f64 {
1626 let low = if symbol == 0 { 0 } else { cdf[symbol - 1] };1650 let low = if symbol == 0 { 0 } else { cdf[symbol - 1] };
1627- let probability = f64::from(cdf[symbol] - low) / 32768.0;1651+ let width = cdf[symbol] - low;
1628- -probability.log2()1652+ // lane-av1speed: a CDF interval is a 15-bit width, so there are only
1653+ // 32769 prices a symbol can carry; the `log2` behind each is computed
1654+ // once instead of per call (libm's `__log2_fma` was 5% of the encoder's
1655+ // profile -- this function is called for every symbol of every candidate
1656+ // the RD search prices). Each entry is exactly the expression this used
1657+ // to evaluate, so every price is bit-identical.
1658+ static PRICES: std::sync::OnceLock<Vec<f64>> = std::sync::OnceLock::new();
1659+ PRICES.get_or_init(|| {
1660+ (0..=32768u32)
1661+ .map(|w| -(f64::from(w) / 32768.0).log2())
1662+ .collect()
1663+ })[usize::from(width)]
1629 }1664 }
1630 1665
1631 /// What the tile writer spends to say a block is coded in each of the thirteen1666 /// What the tile writer spends to say a block is coded in each of the thirteen
@@ -1801,7 +1836,6 @@ fn code_square_inter(
1801 search: &Search,1836 search: &Search,
1802 mode_bits: &[f64; 13],1837 mode_bits: &[f64; 13],
1803 reference: &Picture,1838 reference: &Picture,
1804- ref_y8: &[u8],
1805 stack: &MvStack, fctx: &crate::decode::FrameCtx,1839 stack: &MvStack, fctx: &crate::decode::FrameCtx,
1806 ) -> (BlockCoeffs, f64) {1840 ) -> (BlockCoeffs, f64) {
1807 let (intra_block, intra_cost) = code_square(luma, chroma, (x, y), side, search, mode_bits, fctx);1841 let (intra_block, intra_cost) = code_square(luma, chroma, (x, y), side, search, mode_bits, fctx);
@@ -1933,7 +1967,7 @@ fn code_square_inter(
1933 if leaf_new_mv() {1967 if leaf_new_mv() {
1934 let source_block = luma.source_block(x, y, side);1968 let source_block = luma.source_block(x, y, side);
1935 let found = motion::search(1969 let found = motion::search(
1936- ref_y8,1970+ ref_luma.0,
1937 ref_luma.1,1971 ref_luma.1,
1938 ref_luma.2,1972 ref_luma.2,
1939 ref_luma.3,1973 ref_luma.3,
@@ -1947,18 +1981,28 @@ fn code_square_inter(
1947 );1981 );
1948 let new_mv = round_to_valid_mv(found.mv, stack.pred_mv);1982 let new_mv = round_to_valid_mv(found.mv, stack.pred_mv);
1949 if let Some(mv_bits) = mv_residual_bits(new_mv, stack.pred_mv) {1983 if let Some(mv_bits) = mv_residual_bits(new_mv, stack.pred_mv) {
1950- let luma_new = mc_trial(1984+ // Same reuse as `search_inter_block`: a NEWMV equal to the
1951- luma, x, y, side, new_mv, true, ref_luma.0, ref_luma.1, ref_luma.2, ref_luma.3,1985+ // stack's NEARESTMV is the same three trials, already run above.
1952- false, search.base_q_idx, search.deadzone, luma_set, fctx,1986+ let (luma_new, u_new, v_new) = if new_mv == mv {
1953- );1987+ (luma_trial.clone(), u.clone(), v.clone())
1954- let u_new = mc_trial(1988+ } else {
1955- &chroma[0], x / 2, y / 2, side / 2, new_mv, false, ref_u.0, ref_u.1, ref_u.2,1989+ (
1956- ref_u.3, false, search.base_q_idx, search.deadzone, chroma_set, fctx,1990+ mc_trial(
1957- );1991+ luma, x, y, side, new_mv, true, ref_luma.0, ref_luma.1, ref_luma.2,
1958- let v_new = mc_trial(1992+ ref_luma.3, false, search.base_q_idx, search.deadzone, luma_set, fctx,
1959- &chroma[1], x / 2, y / 2, side / 2, new_mv, false, ref_v.0, ref_v.1, ref_v.2,1993+ ),
1960- ref_v.3, false, search.base_q_idx, search.deadzone, chroma_set, fctx,1994+ mc_trial(
1961- );1995+ &chroma[0], x / 2, y / 2, side / 2, new_mv, false, ref_u.0, ref_u.1,
1996+ ref_u.2, ref_u.3, false, search.base_q_idx, search.deadzone, chroma_set,
1997+ fctx,
1998+ ),
1999+ mc_trial(
2000+ &chroma[1], x / 2, y / 2, side / 2, new_mv, false, ref_v.0, ref_v.1,
2001+ ref_v.2, ref_v.3, false, search.base_q_idx, search.deadzone, chroma_set,
2002+ fctx,
2003+ ),
2004+ )
2005+ };
1962 let skip_new = luma_new.levels.iter().all(|&l| l == 0)2006 let skip_new = luma_new.levels.iter().all(|&l| l == 0)
1963 && u_new.levels.iter().all(|&l| l == 0)2007 && u_new.levels.iter().all(|&l| l == 0)
1964 && v_new.levels.iter().all(|&l| l == 0);2008 && v_new.levels.iter().all(|&l| l == 0);
@@ -2859,7 +2903,12 @@ fn mc_trial(
2859 // lane-hbd r4: `reference` (a DPB `Picture`'s plane) is `u16` now that2903 // lane-hbd r4: `reference` (a DPB `Picture`'s plane) is `u16` now that
2860 // `Picture` is widened; `prediction`/the encoder stay `u8` (encoder is2904 // `Picture` is widened; `prediction`/the encoder stay `u8` (encoder is
2861 // 8-bit only this round, see `intra_predict_u8`'s doc comment).2905 // 8-bit only this round, see `intra_predict_u8`'s doc comment).
2862- let mut prediction16 = vec![0u16; side * side];2906+ // lane-av1speed: both buffers are per-candidate scratch on the hottest
2907+ // path in the encoder, so they live on the stack -- `side` is at most
2908+ // `BLOCK` (32) for luma and half that for chroma. Two heap allocations
2909+ // per motion-compensated trial otherwise.
2910+ let mut prediction16 = [0u16; BLOCK * BLOCK];
2911+ let prediction16 = &mut prediction16[..side * side];
2863 mc::predict(2912 mc::predict(
2864 reference,2913 reference,
2865 stride,2914 stride,
@@ -2869,10 +2918,14 @@ fn mc_trial(
2869 y_q4,2918 y_q4,
2870 side,2919 side,
2871 side,2920 side,
2872- &mut prediction16, fctx,2921+ prediction16, fctx,
2873 );2922 );
2874- let prediction: Vec<u8> = prediction16.iter().map(|&v| v as u8).collect();2923+ let mut prediction = [0u8; BLOCK * BLOCK];
2875- plane.code_from_prediction(x, y, side, &prediction, skip, base_q_idx, deadzone, set)2924+ let prediction = &mut prediction[..side * side];
2925+ for (dst, &src) in prediction.iter_mut().zip(prediction16.iter()) {
2926+ *dst = src as u8;
2927+ }
2928+ plane.code_from_prediction(x, y, side, prediction, skip, base_q_idx, deadzone, set)
2876 }2929 }
2877 2930
2878 /// Codes one 32x32 block of an inter frame as whichever costs least of: each2931 /// Codes one 32x32 block of an inter frame as whichever costs least of: each
@@ -2901,7 +2954,6 @@ fn search_inter_block(
2901 search: &Search,2954 search: &Search,
2902 mode_bits: &[f64; 13],2955 mode_bits: &[f64; 13],
2903 reference: &Picture,2956 reference: &Picture,
2904- ref_y8: &[u8],
2905 stack: &MvStack, fctx: &crate::decode::FrameCtx,2957 stack: &MvStack, fctx: &crate::decode::FrameCtx,
2906 ) -> (BlockCoeffs, f64) {2958 ) -> (BlockCoeffs, f64) {
2907 // `luma_set` is the INTRA candidates' table; the two inter candidates2959 // `luma_set` is the INTRA candidates' table; the two inter candidates
@@ -3057,6 +3109,14 @@ fn search_inter_block(
3057 // The NEARESTMV candidate: `skip` is decided below from whether its3109 // The NEARESTMV candidate: `skip` is decided below from whether its
3058 // trial actually found a nonzero level, once its residual is priced3110 // trial actually found a nonzero level, once its residual is priced
3059 // against the same CDFs the tile writer codes it with.3111 // against the same CDFs the tile writer codes it with.
3112+ //
3113+ // lane-av1speed: its three trials are kept (rather than moved straight
3114+ // into the candidate) so that a NEWMV landing on the very same vector --
3115+ // common on static content, where the stack's nearest vector is also
3116+ // what the search finds -- reuses them instead of running the same
3117+ // motion compensation, transform, quantize and reconstruct again. The
3118+ // reused trials are the same values, so the choice is bit-identical.
3119+ let nearest_trials;
3060 {3120 {
3061 let mv = stack.nearest_mv;3121 let mv = stack.nearest_mv;
3062 let luma_trial = mc_trial(3122 let luma_trial = mc_trial(
@@ -3123,6 +3183,7 @@ fn search_inter_block(
3123 } else {3183 } else {
3124 luma_trial.bits + u.bits + v.bits3184 luma_trial.bits + u.bits + v.bits
3125 });3185 });
3186+ nearest_trials = (luma_trial.clone(), u.clone(), v.clone());
3126 consider(Candidate {3187 consider(Candidate {
3127 cost,3188 cost,
3128 luma: luma_trial,3189 luma: luma_trial,
@@ -3141,7 +3202,7 @@ fn search_inter_block(
3141 #[cfg(test)]3202 #[cfg(test)]
3142 let t = std::time::Instant::now();3203 let t = std::time::Instant::now();
3143 let found = motion::search(3204 let found = motion::search(
3144- ref_y8,3205+ ref_luma.0,
3145 ref_luma.1,3206 ref_luma.1,
3146 ref_luma.2,3207 ref_luma.2,
3147 ref_luma.3,3208 ref_luma.3,
@@ -3157,54 +3218,60 @@ fn search_inter_block(
3157 stage_add(0, t.elapsed());3218 stage_add(0, t.elapsed());
3158 let mv = round_to_valid_mv(found.mv, stack.pred_mv);3219 let mv = round_to_valid_mv(found.mv, stack.pred_mv);
3159 if let Some(mv_bits) = mv_residual_bits(mv, stack.pred_mv) {3220 if let Some(mv_bits) = mv_residual_bits(mv, stack.pred_mv) {
3160- let luma_trial = mc_trial(3221+ let (luma_trial, u, v) = if mv == stack.nearest_mv {
3161- luma,3222+ nearest_trials
3162- x,3223+ } else {
3163- y,3224+ (
3164- BLOCK,3225+ mc_trial(
3165- mv,3226+ luma,
3166- true,3227+ x,
3167- ref_luma.0,3228+ y,
3168- ref_luma.1,3229+ BLOCK,
3169- ref_luma.2,3230+ mv,
3170- ref_luma.3,3231+ true,
3171- false,3232+ ref_luma.0,
3172- search.base_q_idx,3233+ ref_luma.1,
3173- search.deadzone,3234+ ref_luma.2,
3174- TxbSet::Luma32Inter, fctx,3235+ ref_luma.3,
3175- );3236+ false,
3176- let u = mc_trial(3237+ search.base_q_idx,
3177- &chroma[0],3238+ search.deadzone,
3178- x / 2,3239+ TxbSet::Luma32Inter, fctx,
3179- y / 2,3240+ ),
3180- BLOCK / 2,3241+ mc_trial(
3181- mv,3242+ &chroma[0],
3182- false,3243+ x / 2,
3183- ref_u.0,3244+ y / 2,
3184- ref_u.1,3245+ BLOCK / 2,
3185- ref_u.2,3246+ mv,
3186- ref_u.3,3247+ false,
3187- false,3248+ ref_u.0,
3188- search.base_q_idx,3249+ ref_u.1,
3189- search.deadzone,3250+ ref_u.2,
3190- chroma_set, fctx,3251+ ref_u.3,
3191- );3252+ false,
3192- let v = mc_trial(3253+ search.base_q_idx,
3193- &chroma[1],3254+ search.deadzone,
3194- x / 2,3255+ chroma_set, fctx,
3195- y / 2,3256+ ),
3196- BLOCK / 2,3257+ mc_trial(
3197- mv,3258+ &chroma[1],
3198- false,3259+ x / 2,
3199- ref_v.0,3260+ y / 2,
3200- ref_v.1,3261+ BLOCK / 2,
3201- ref_v.2,3262+ mv,
3202- ref_v.3,3263+ false,
3203- false,3264+ ref_v.0,
3204- search.base_q_idx,3265+ ref_v.1,
3205- search.deadzone,3266+ ref_v.2,
3206- chroma_set, fctx,3267+ ref_v.3,
3207- );3268+ false,
3269+ search.base_q_idx,
3270+ search.deadzone,
3271+ chroma_set, fctx,
3272+ ),
3273+ )
3274+ };
3208 let drl_bits = if stack.entries.len() > 1 {3275 let drl_bits = if stack.entries.len() > 1 {
3209 symbol_bits(&cdf::DRL_MODE[stack.drl_ctx[0]], 0)3276 symbol_bits(&cdf::DRL_MODE[stack.drl_ctx[0]], 0)
3210 } else {3277 } else {
@@ -3360,7 +3427,6 @@ pub(crate) fn encode_inter_frame(
3360 // integer-`u8`-scale), so the DPB reference plane is narrowed for it --3427 // integer-`u8`-scale), so the DPB reference plane is narrowed for it --
3361 // once per frame (lane-av1rd2), not once per block, which is a whole3428 // once per frame (lane-av1rd2), not once per block, which is a whole
3362 // plane's map per 32x32 block and now per leaf too.3429 // plane's map per 32x32 block and now per leaf too.
3363- let ref_y8: Vec<u8> = reference.y.iter().map(|&v| v as u8).collect();
3364 let mut grid = MiGrid::new(mi_cols, mi_rows);3430 let mut grid = MiGrid::new(mi_cols, mi_rows);
3365 let mut blocks = vec![Quadrant::Whole(BlockCoeffs::default()); cols * rows];3431 let mut blocks = vec![Quadrant::Whole(BlockCoeffs::default()); cols * rows];
3366 3432
@@ -3407,7 +3473,6 @@ pub(crate) fn encode_inter_frame(
3407 &search,3473 &search,
3408 &mode_bits_table,3474 &mode_bits_table,
3409 reference,3475 reference,
3410- &ref_y8,
3411 &stack, fctx,3476 &stack, fctx,
3412 );3477 );
3413 cost_whole += search.lambda * partition_bits(BLOCK, false);3478 cost_whole += search.lambda * partition_bits(BLOCK, false);
@@ -3439,7 +3504,13 @@ pub(crate) fn encode_inter_frame(
3439 header.mi_rows,3504 header.mi_rows,
3440 )3505 )
3441 });3506 });
3442- let split = (!block.skip && leaves_legal && split_inter_blocks())3507+ // The breakout above: a 32x32 block that codes almost
3508+ // nothing is not offered the split at all.
3509+ let breakout = split_breakout_coeffs();
3510+ let split = (!block.skip
3511+ && (breakout == 0 || block.luma.len() > 4 * breakout)
3512+ && leaves_legal
3513+ && split_inter_blocks())
3443 .then(|| {3514 .then(|| {
3444 restore(&mut luma, &mut chroma, (x, y), BLOCK, &base);3515 restore(&mut luma, &mut chroma, (x, y), BLOCK, &base);
3445 let mut cost = search.lambda * partition_bits(BLOCK, true);3516 let mut cost = search.lambda * partition_bits(BLOCK, true);
@@ -3459,8 +3530,7 @@ pub(crate) fn encode_inter_frame(
3459 &search,3530 &search,
3460 &mode_bits_table,3531 &mode_bits_table,
3461 reference,3532 reference,
3462- &ref_y8,3533+ &stack, fctx,
3463- &stack, fctx,
3464 );3534 );
3465 let leaf_cost = leaf_cost3535 let leaf_cost = leaf_cost
3466 + search.lambda * partition_bits(SUB, false);3536 + search.lambda * partition_bits(SUB, false);
@@ -3471,7 +3541,9 @@ pub(crate) fn encode_inter_frame(
3471 // the writer codes as a real PARTITION_SPLIT3541 // the writer codes as a real PARTITION_SPLIT
3472 // at BLOCK_16X16 (lane-av1rd2). Same3542 // at BLOCK_16X16 (lane-av1rd2). Same
3473 // skipped-block prune as the level above.3543 // skipped-block prune as the level above.
3474- let eight = (!leaf.skip && split_inter_8())3544+ let eight = (!leaf.skip
3545+ && (breakout == 0 || leaf.luma.len() > breakout)
3546+ && split_inter_8())
3475 .then(|| {3547 .then(|| {
3476 restore(3548 restore(
3477 &mut luma, &mut chroma, (lx, ly), SUB, &leaf_base,3549 &mut luma, &mut chroma, (lx, ly), SUB, &leaf_base,
@@ -3494,8 +3566,7 @@ pub(crate) fn encode_inter_frame(
3494 &search,3566 &search,
3495 &mode_bits_table,3567 &mode_bits_table,
3496 reference,3568 reference,
3497- &ref_y8,3569+ &stack8, fctx,
3498- &stack8, fctx,
3499 );3570 );
3500 cost8 += cost;3571 cost8 += cost;
3501 record_mi(&mut grid, mr, mc, 2, leaf8.inter);3572 record_mi(&mut grid, mr, mc, 2, leaf8.inter);
@@ -3596,8 +3667,7 @@ pub(crate) fn encode_inter_frame(
3596 &search,3667 &search,
3597 &mode_bits_table,3668 &mode_bits_table,
3598 reference,3669 reference,
3599- &ref_y8,3670+ &stack, fctx,
3600- &stack, fctx,
3601 );3671 );
3602 for dr in 0..4 {3672 for dr in 0..4 {
3603 for dc in 0..4 {3673 for dc in 0..4 {
@@ -3657,8 +3727,7 @@ pub(crate) fn encode_inter_frame(
3657 &search,3727 &search,
3658 &mode_bits_table,3728 &mode_bits_table,
3659 reference,3729 reference,
3660- &ref_y8,3730+ &stack, fctx,
3661- &stack, fctx,
3662 );3731 );
3663 for dr in 0..2 {3732 for dr in 0..2 {
3664 for dc in 0..2 {3733 for dc in 0..2 {
crates/ec-av1/src/motion.rsmodified+25 -16
@@ -190,7 +190,7 @@ pub struct MotionSearch {
190 /// empty (the same contracts [`predict`] has).190 /// empty (the same contracts [`predict`] has).
191 #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor191 #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor
192 pub(crate) fn search(192 pub(crate) fn search(
193- reference: &[u8],193+ reference: &[u16],
194 stride: usize,194 stride: usize,
195 ref_width: usize,195 ref_width: usize,
196 ref_height: usize,196 ref_height: usize,
@@ -214,7 +214,7 @@ pub(crate) fn search(
214 /// checks.214 /// checks.
215 #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor215 #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor
216 fn search_traced(216 fn search_traced(
217- reference: &[u8],217+ reference: &[u16],
218 stride: usize,218 stride: usize,
219 ref_width: usize,219 ref_width: usize,
220 ref_height: usize,220 ref_height: usize,
@@ -247,7 +247,7 @@ fn search_traced(
247 /// [`SEARCH_INITIAL_STEP_PEL`].247 /// [`SEARCH_INITIAL_STEP_PEL`].
248 #[allow(clippy::too_many_arguments)]248 #[allow(clippy::too_many_arguments)]
249 fn search_traced_from_step(249 fn search_traced_from_step(
250- reference: &[u8],250+ reference: &[u16],
251 stride: usize,251 stride: usize,
252 ref_width: usize,252 ref_width: usize,
253 ref_height: usize,253 ref_height: usize,
@@ -263,26 +263,31 @@ fn search_traced_from_step(
263 assert_eq!(source.len(), block_w * block_h, "source is one block");263 assert_eq!(source.len(), block_w * block_h, "source is one block");
264 assert!(!reference.is_empty(), "a reference plane has samples");264 assert!(!reference.is_empty(), "a reference plane has samples");
265 265
266- // lane-hbd r2: this encoder-side search stays `u8` (see266+ // lane-av1speed: `reference` arrives as the DPB picture's own `u16`
267- // `encode::intra_predict_u8`'s doc comment) -- `reference` is converted267+ // plane. It used to be handed the encoder's `u8` shadow copy and
268- // once here, outside the per-candidate closure, since `mc::predict` now268+ // converted here, whole-plane, on EVERY call -- one 245k-element
269- // takes `u16`.269+ // allocate-and-convert per searched block, which profiled as the single
270- let reference16: Vec<u16> = reference.iter().map(|&v| u16::from(v)).collect();270+ // largest symbol in the encoder (30% of `sequence_bench_sanity`).
271 let mut dst16 = vec![0u16; block_w * block_h];271 let mut dst16 = vec![0u16; block_w * block_h];
272 let mut cost_of = |mv: (i32, i32)| -> f64 {272 let mut cost_of = |mv: (i32, i32)| -> f64 {
273 let x_q4 = (block_x as i32) * 16 + mv.1 * Q4_PER_Q3;273 let x_q4 = (block_x as i32) * 16 + mv.1 * Q4_PER_Q3;
274 let y_q4 = (block_y as i32) * 16 + mv.0 * Q4_PER_Q3;274 let y_q4 = (block_y as i32) * 16 + mv.0 * Q4_PER_Q3;
275 predict(275 predict(
276- &reference16, stride, ref_width, ref_height, x_q4, y_q4, block_w, block_h,276+ reference, stride, ref_width, ref_height, x_q4, y_q4, block_w, block_h,
277 &mut dst16, fctx,277 &mut dst16, fctx,
278 );278 );
279- let sad: f64 = source279+ // lane-av1speed: accumulated as an integer (a block is at most
280+ // 32x32 samples of at most 255 each, nowhere near `u32`, and an
281+ // integer sum this small is exact in `f64` whatever order it is
282+ // summed in) instead of one `f64` conversion and dependent add per
283+ // sample. Same value, several times the throughput.
284+ let sad: u32 = source
280 .iter()285 .iter()
281 .zip(dst16.iter())286 .zip(dst16.iter())
282- .map(|(&a, &b)| f64::from((i32::from(a) - i32::from(b)).unsigned_abs()))287+ .map(|(&a, &b)| (i32::from(a) - i32::from(b)).unsigned_abs())
283 .sum();288 .sum();
284 let diff = (mv.0 - pred_mv.0, mv.1 - pred_mv.1);289 let diff = (mv.0 - pred_mv.0, mv.1 - pred_mv.1);
285- sad + lambda * mv_bits(diff)290+ f64::from(sad) + lambda * mv_bits(diff)
286 };291 };
287 292
288 let mut trace = Vec::new();293 let mut trace = Vec::new();
@@ -405,6 +410,7 @@ mod tests {
405 let width = 64;410 let width = 64;
406 let height = 64;411 let height = 64;
407 let (plane, _unused) = plane_and_block(width, height, 0, 0, 1, 1);412 let (plane, _unused) = plane_and_block(width, height, 0, 0, 1, 1);
413+ let plane16: Vec<u16> = plane.iter().map(|&v| u16::from(v)).collect();
408 let block_w = 8;414 let block_w = 8;
409 let block_h = 8;415 let block_h = 8;
410 // Anchor the source block away from the plane's edges so every sign416 // Anchor the source block away from the plane's edges so every sign
@@ -433,7 +439,7 @@ mod tests {
433 }439 }
434 }440 }
435 let result = search(441 let result = search(
436- &plane,442+ &plane16,
437 width,443 width,
438 width,444 width,
439 height,445 height,
@@ -463,6 +469,7 @@ mod tests {
463 let plane: Vec<u8> = (0..width * height)469 let plane: Vec<u8> = (0..width * height)
464 .map(|i| (2 * (i % width)) as u8)470 .map(|i| (2 * (i % width)) as u8)
465 .collect();471 .collect();
472+ let plane16: Vec<u16> = plane.iter().map(|&v| u16::from(v)).collect();
466 let block_w = 8;473 let block_w = 8;
467 let block_h = 6;474 let block_h = 6;
468 let anchor_x = 10;475 let anchor_x = 10;
@@ -481,7 +488,7 @@ mod tests {
481 }488 }
482 }489 }
483 let result = search(490 let result = search(
484- &plane,491+ &plane16,
485 width,492 width,
486 width,493 width,
487 height,494 height,
@@ -506,6 +513,7 @@ mod tests {
506 let width = 48;513 let width = 48;
507 let height = 48;514 let height = 48;
508 let (plane, _unused) = plane_and_block(width, height, 0, 0, 1, 1);515 let (plane, _unused) = plane_and_block(width, height, 0, 0, 1, 1);
516+ let plane16: Vec<u16> = plane.iter().map(|&v| u16::from(v)).collect();
509 let block_w = 8;517 let block_w = 8;
510 let block_h = 8;518 let block_h = 8;
511 let anchor_x = 20;519 let anchor_x = 20;
@@ -518,7 +526,7 @@ mod tests {
518 }526 }
519 }527 }
520 let (_result, trace) = search_traced(528 let (_result, trace) = search_traced(
521- &plane,529+ &plane16,
522 width,530 width,
523 width,531 width,
524 height,532 height,
@@ -566,6 +574,7 @@ mod tests {
566 let width = 96;574 let width = 96;
567 let height = 96;575 let height = 96;
568 let (plane, _unused) = plane_and_block(width, height, 0, 0, 1, 1);576 let (plane, _unused) = plane_and_block(width, height, 0, 0, 1, 1);
577+ let plane16: Vec<u16> = plane.iter().map(|&v| u16::from(v)).collect();
569 let block_w = 8;578 let block_w = 8;
570 let block_h = 8;579 let block_h = 8;
571 let anchor_x = 48;580 let anchor_x = 48;
@@ -586,7 +595,7 @@ mod tests {
586 }595 }
587 }596 }
588 let (result, trace) = search_traced_from_step(597 let (result, trace) = search_traced_from_step(
589- &plane,598+ &plane16,
590 width,599 width,
591 width,600 width,
592 height,601 height,
crates/ec-av1/src/transform.rsmodified+12 -2
@@ -1540,8 +1540,18 @@ fn fft(a: &mut [Complex]) {
1540 }1540 }
1541 let mut len = 2;1541 let mut len = 2;
1542 while len <= n {1542 while len <= n {
1543- let ang = -2.0 * std::f64::consts::PI / len as f64;1543+ // lane-av1speed: the stage twiddle depends on `len` alone, so the
1544- let wlen = (ang.cos(), ang.sin());1544+ // eight values this loop can ever ask for are computed once instead
1545+ // of two libm `sincos` calls per stage per call (`__sincos_fma` was
1546+ // 9% of the encoder's profile). Same values, so the recurrence below
1547+ // -- and every coefficient it produces -- is bit-identical.
1548+ static WLEN: std::sync::OnceLock<[(f64, f64); 7]> = std::sync::OnceLock::new();
1549+ let wlen = WLEN.get_or_init(|| {
1550+ std::array::from_fn(|i| {
1551+ let ang = -2.0 * std::f64::consts::PI / (2u32 << i) as f64;
1552+ (ang.cos(), ang.sin())
1553+ })
1554+ })[len.trailing_zeros() as usize - 1];
1545 let mut i = 0;1555 let mut i = 0;
1546 while i < n {1556 while i < n {
1547 let mut w = (1.0, 0.0);1557 let mut w = (1.0, 0.0);