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 commit3 changed files +188 -100
- crates/ec-av1/src/encode.rs +151 -82
- crates/ec-av1/src/motion.rs +25 -16
- crates/ec-av1/src/transform.rs +12 -2
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 | 185 | /// How often each partition decision was taken since the last |
| 162 | 186 | /// [`take_partition_hits`], so a gate can report how often a new partition |
| 163 | 187 | /// fires rather than assume it does (gate-blind-to-feature). Index 0 is an |
| @@ -1624,8 +1648,19 @@ impl Reach { | ||
| 1624 | 1648 | /// What one symbol costs against a CDF, in bits. |
| 1625 | 1649 | pub(crate) fn symbol_bits(cdf: &[u16], symbol: usize) -> f64 { |
| 1626 | 1650 | let low = if symbol == 0 { 0 } else { cdf[symbol - 1] }; |
| 1627 | - let probability = f64::from(cdf[symbol] - low) / 32768.0; | |
| 1628 | - -probability.log2() | |
| 1651 | + let width = cdf[symbol] - low; | |
| 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 | 1666 | /// 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 | 1836 | search: &Search, |
| 1802 | 1837 | mode_bits: &[f64; 13], |
| 1803 | 1838 | reference: &Picture, |
| 1804 | - ref_y8: &[u8], | |
| 1805 | 1839 | stack: &MvStack, fctx: &crate::decode::FrameCtx, |
| 1806 | 1840 | ) -> (BlockCoeffs, f64) { |
| 1807 | 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 | 1967 | if leaf_new_mv() { |
| 1934 | 1968 | let source_block = luma.source_block(x, y, side); |
| 1935 | 1969 | let found = motion::search( |
| 1936 | - ref_y8, | |
| 1970 | + ref_luma.0, | |
| 1937 | 1971 | ref_luma.1, |
| 1938 | 1972 | ref_luma.2, |
| 1939 | 1973 | ref_luma.3, |
| @@ -1947,18 +1981,28 @@ fn code_square_inter( | ||
| 1947 | 1981 | ); |
| 1948 | 1982 | let new_mv = round_to_valid_mv(found.mv, stack.pred_mv); |
| 1949 | 1983 | if let Some(mv_bits) = mv_residual_bits(new_mv, stack.pred_mv) { |
| 1950 | - let luma_new = mc_trial( | |
| 1951 | - luma, x, y, side, new_mv, true, ref_luma.0, ref_luma.1, ref_luma.2, ref_luma.3, | |
| 1952 | - false, search.base_q_idx, search.deadzone, luma_set, fctx, | |
| 1953 | - ); | |
| 1954 | - let u_new = mc_trial( | |
| 1955 | - &chroma[0], x / 2, y / 2, side / 2, new_mv, false, ref_u.0, ref_u.1, ref_u.2, | |
| 1956 | - ref_u.3, false, search.base_q_idx, search.deadzone, chroma_set, fctx, | |
| 1957 | - ); | |
| 1958 | - let v_new = mc_trial( | |
| 1959 | - &chroma[1], x / 2, y / 2, side / 2, new_mv, false, ref_v.0, ref_v.1, ref_v.2, | |
| 1960 | - ref_v.3, false, search.base_q_idx, search.deadzone, chroma_set, fctx, | |
| 1961 | - ); | |
| 1984 | + // Same reuse as `search_inter_block`: a NEWMV equal to the | |
| 1985 | + // stack's NEARESTMV is the same three trials, already run above. | |
| 1986 | + let (luma_new, u_new, v_new) = if new_mv == mv { | |
| 1987 | + (luma_trial.clone(), u.clone(), v.clone()) | |
| 1988 | + } else { | |
| 1989 | + ( | |
| 1990 | + mc_trial( | |
| 1991 | + luma, x, y, side, new_mv, true, ref_luma.0, ref_luma.1, ref_luma.2, | |
| 1992 | + ref_luma.3, false, search.base_q_idx, search.deadzone, luma_set, fctx, | |
| 1993 | + ), | |
| 1994 | + mc_trial( | |
| 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 | 2006 | let skip_new = luma_new.levels.iter().all(|&l| l == 0) |
| 1963 | 2007 | && u_new.levels.iter().all(|&l| l == 0) |
| 1964 | 2008 | && v_new.levels.iter().all(|&l| l == 0); |
| @@ -2859,7 +2903,12 @@ fn mc_trial( | ||
| 2859 | 2903 | // lane-hbd r4: `reference` (a DPB `Picture`'s plane) is `u16` now that |
| 2860 | 2904 | // `Picture` is widened; `prediction`/the encoder stay `u8` (encoder is |
| 2861 | 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 | 2912 | mc::predict( |
| 2864 | 2913 | reference, |
| 2865 | 2914 | stride, |
| @@ -2869,10 +2918,14 @@ fn mc_trial( | ||
| 2869 | 2918 | y_q4, |
| 2870 | 2919 | side, |
| 2871 | 2920 | side, |
| 2872 | - &mut prediction16, fctx, | |
| 2921 | + prediction16, fctx, | |
| 2873 | 2922 | ); |
| 2874 | - let prediction: Vec<u8> = prediction16.iter().map(|&v| v as u8).collect(); | |
| 2875 | - plane.code_from_prediction(x, y, side, &prediction, skip, base_q_idx, deadzone, set) | |
| 2923 | + let mut prediction = [0u8; BLOCK * BLOCK]; | |
| 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 | 2931 | /// Codes one 32x32 block of an inter frame as whichever costs least of: each |
| @@ -2901,7 +2954,6 @@ fn search_inter_block( | ||
| 2901 | 2954 | search: &Search, |
| 2902 | 2955 | mode_bits: &[f64; 13], |
| 2903 | 2956 | reference: &Picture, |
| 2904 | - ref_y8: &[u8], | |
| 2905 | 2957 | stack: &MvStack, fctx: &crate::decode::FrameCtx, |
| 2906 | 2958 | ) -> (BlockCoeffs, f64) { |
| 2907 | 2959 | // `luma_set` is the INTRA candidates' table; the two inter candidates |
| @@ -3057,6 +3109,14 @@ fn search_inter_block( | ||
| 3057 | 3109 | // The NEARESTMV candidate: `skip` is decided below from whether its |
| 3058 | 3110 | // trial actually found a nonzero level, once its residual is priced |
| 3059 | 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 | 3121 | let mv = stack.nearest_mv; |
| 3062 | 3122 | let luma_trial = mc_trial( |
| @@ -3123,6 +3183,7 @@ fn search_inter_block( | ||
| 3123 | 3183 | } else { |
| 3124 | 3184 | luma_trial.bits + u.bits + v.bits |
| 3125 | 3185 | }); |
| 3186 | + nearest_trials = (luma_trial.clone(), u.clone(), v.clone()); | |
| 3126 | 3187 | consider(Candidate { |
| 3127 | 3188 | cost, |
| 3128 | 3189 | luma: luma_trial, |
| @@ -3141,7 +3202,7 @@ fn search_inter_block( | ||
| 3141 | 3202 | #[cfg(test)] |
| 3142 | 3203 | let t = std::time::Instant::now(); |
| 3143 | 3204 | let found = motion::search( |
| 3144 | - ref_y8, | |
| 3205 | + ref_luma.0, | |
| 3145 | 3206 | ref_luma.1, |
| 3146 | 3207 | ref_luma.2, |
| 3147 | 3208 | ref_luma.3, |
| @@ -3157,54 +3218,60 @@ fn search_inter_block( | ||
| 3157 | 3218 | stage_add(0, t.elapsed()); |
| 3158 | 3219 | let mv = round_to_valid_mv(found.mv, stack.pred_mv); |
| 3159 | 3220 | if let Some(mv_bits) = mv_residual_bits(mv, stack.pred_mv) { |
| 3160 | - let luma_trial = mc_trial( | |
| 3161 | - luma, | |
| 3162 | - x, | |
| 3163 | - y, | |
| 3164 | - BLOCK, | |
| 3165 | - mv, | |
| 3166 | - true, | |
| 3167 | - ref_luma.0, | |
| 3168 | - ref_luma.1, | |
| 3169 | - ref_luma.2, | |
| 3170 | - ref_luma.3, | |
| 3171 | - false, | |
| 3172 | - search.base_q_idx, | |
| 3173 | - search.deadzone, | |
| 3174 | - TxbSet::Luma32Inter, fctx, | |
| 3175 | - ); | |
| 3176 | - let u = mc_trial( | |
| 3177 | - &chroma[0], | |
| 3178 | - x / 2, | |
| 3179 | - y / 2, | |
| 3180 | - BLOCK / 2, | |
| 3181 | - mv, | |
| 3182 | - false, | |
| 3183 | - ref_u.0, | |
| 3184 | - ref_u.1, | |
| 3185 | - ref_u.2, | |
| 3186 | - ref_u.3, | |
| 3187 | - false, | |
| 3188 | - search.base_q_idx, | |
| 3189 | - search.deadzone, | |
| 3190 | - chroma_set, fctx, | |
| 3191 | - ); | |
| 3192 | - let v = mc_trial( | |
| 3193 | - &chroma[1], | |
| 3194 | - x / 2, | |
| 3195 | - y / 2, | |
| 3196 | - BLOCK / 2, | |
| 3197 | - mv, | |
| 3198 | - false, | |
| 3199 | - ref_v.0, | |
| 3200 | - ref_v.1, | |
| 3201 | - ref_v.2, | |
| 3202 | - ref_v.3, | |
| 3203 | - false, | |
| 3204 | - search.base_q_idx, | |
| 3205 | - search.deadzone, | |
| 3206 | - chroma_set, fctx, | |
| 3207 | - ); | |
| 3221 | + let (luma_trial, u, v) = if mv == stack.nearest_mv { | |
| 3222 | + nearest_trials | |
| 3223 | + } else { | |
| 3224 | + ( | |
| 3225 | + mc_trial( | |
| 3226 | + luma, | |
| 3227 | + x, | |
| 3228 | + y, | |
| 3229 | + BLOCK, | |
| 3230 | + mv, | |
| 3231 | + true, | |
| 3232 | + ref_luma.0, | |
| 3233 | + ref_luma.1, | |
| 3234 | + ref_luma.2, | |
| 3235 | + ref_luma.3, | |
| 3236 | + false, | |
| 3237 | + search.base_q_idx, | |
| 3238 | + search.deadzone, | |
| 3239 | + TxbSet::Luma32Inter, fctx, | |
| 3240 | + ), | |
| 3241 | + mc_trial( | |
| 3242 | + &chroma[0], | |
| 3243 | + x / 2, | |
| 3244 | + y / 2, | |
| 3245 | + BLOCK / 2, | |
| 3246 | + mv, | |
| 3247 | + false, | |
| 3248 | + ref_u.0, | |
| 3249 | + ref_u.1, | |
| 3250 | + ref_u.2, | |
| 3251 | + ref_u.3, | |
| 3252 | + false, | |
| 3253 | + search.base_q_idx, | |
| 3254 | + search.deadzone, | |
| 3255 | + chroma_set, fctx, | |
| 3256 | + ), | |
| 3257 | + mc_trial( | |
| 3258 | + &chroma[1], | |
| 3259 | + x / 2, | |
| 3260 | + y / 2, | |
| 3261 | + BLOCK / 2, | |
| 3262 | + mv, | |
| 3263 | + false, | |
| 3264 | + ref_v.0, | |
| 3265 | + ref_v.1, | |
| 3266 | + ref_v.2, | |
| 3267 | + ref_v.3, | |
| 3268 | + false, | |
| 3269 | + search.base_q_idx, | |
| 3270 | + search.deadzone, | |
| 3271 | + chroma_set, fctx, | |
| 3272 | + ), | |
| 3273 | + ) | |
| 3274 | + }; | |
| 3208 | 3275 | let drl_bits = if stack.entries.len() > 1 { |
| 3209 | 3276 | symbol_bits(&cdf::DRL_MODE[stack.drl_ctx[0]], 0) |
| 3210 | 3277 | } else { |
| @@ -3360,7 +3427,6 @@ pub(crate) fn encode_inter_frame( | ||
| 3360 | 3427 | // integer-`u8`-scale), so the DPB reference plane is narrowed for it -- |
| 3361 | 3428 | // once per frame (lane-av1rd2), not once per block, which is a whole |
| 3362 | 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 | 3430 | let mut grid = MiGrid::new(mi_cols, mi_rows); |
| 3365 | 3431 | let mut blocks = vec![Quadrant::Whole(BlockCoeffs::default()); cols * rows]; |
| 3366 | 3432 | |
| @@ -3407,7 +3473,6 @@ pub(crate) fn encode_inter_frame( | ||
| 3407 | 3473 | &search, |
| 3408 | 3474 | &mode_bits_table, |
| 3409 | 3475 | reference, |
| 3410 | - &ref_y8, | |
| 3411 | 3476 | &stack, fctx, |
| 3412 | 3477 | ); |
| 3413 | 3478 | cost_whole += search.lambda * partition_bits(BLOCK, false); |
| @@ -3439,7 +3504,13 @@ pub(crate) fn encode_inter_frame( | ||
| 3439 | 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 | 3514 | .then(|| { |
| 3444 | 3515 | restore(&mut luma, &mut chroma, (x, y), BLOCK, &base); |
| 3445 | 3516 | let mut cost = search.lambda * partition_bits(BLOCK, true); |
| @@ -3459,8 +3530,7 @@ pub(crate) fn encode_inter_frame( | ||
| 3459 | 3530 | &search, |
| 3460 | 3531 | &mode_bits_table, |
| 3461 | 3532 | reference, |
| 3462 | - &ref_y8, | |
| 3463 | - &stack, fctx, | |
| 3533 | + &stack, fctx, | |
| 3464 | 3534 | ); |
| 3465 | 3535 | let leaf_cost = leaf_cost |
| 3466 | 3536 | + search.lambda * partition_bits(SUB, false); |
| @@ -3471,7 +3541,9 @@ pub(crate) fn encode_inter_frame( | ||
| 3471 | 3541 | // the writer codes as a real PARTITION_SPLIT |
| 3472 | 3542 | // at BLOCK_16X16 (lane-av1rd2). Same |
| 3473 | 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 | 3547 | .then(|| { |
| 3476 | 3548 | restore( |
| 3477 | 3549 | &mut luma, &mut chroma, (lx, ly), SUB, &leaf_base, |
| @@ -3494,8 +3566,7 @@ pub(crate) fn encode_inter_frame( | ||
| 3494 | 3566 | &search, |
| 3495 | 3567 | &mode_bits_table, |
| 3496 | 3568 | reference, |
| 3497 | - &ref_y8, | |
| 3498 | - &stack8, fctx, | |
| 3569 | + &stack8, fctx, | |
| 3499 | 3570 | ); |
| 3500 | 3571 | cost8 += cost; |
| 3501 | 3572 | record_mi(&mut grid, mr, mc, 2, leaf8.inter); |
| @@ -3596,8 +3667,7 @@ pub(crate) fn encode_inter_frame( | ||
| 3596 | 3667 | &search, |
| 3597 | 3668 | &mode_bits_table, |
| 3598 | 3669 | reference, |
| 3599 | - &ref_y8, | |
| 3600 | - &stack, fctx, | |
| 3670 | + &stack, fctx, | |
| 3601 | 3671 | ); |
| 3602 | 3672 | for dr in 0..4 { |
| 3603 | 3673 | for dc in 0..4 { |
| @@ -3657,8 +3727,7 @@ pub(crate) fn encode_inter_frame( | ||
| 3657 | 3727 | &search, |
| 3658 | 3728 | &mode_bits_table, |
| 3659 | 3729 | reference, |
| 3660 | - &ref_y8, | |
| 3661 | - &stack, fctx, | |
| 3730 | + &stack, fctx, | |
| 3662 | 3731 | ); |
| 3663 | 3732 | for dr in 0..2 { |
| 3664 | 3733 | for dc in 0..2 { |
crates/ec-av1/src/motion.rsmodified+25 -16
| @@ -190,7 +190,7 @@ pub struct MotionSearch { | ||
| 190 | 190 | /// empty (the same contracts [`predict`] has). |
| 191 | 191 | #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor |
| 192 | 192 | pub(crate) fn search( |
| 193 | - reference: &[u8], | |
| 193 | + reference: &[u16], | |
| 194 | 194 | stride: usize, |
| 195 | 195 | ref_width: usize, |
| 196 | 196 | ref_height: usize, |
| @@ -214,7 +214,7 @@ pub(crate) fn search( | ||
| 214 | 214 | /// checks. |
| 215 | 215 | #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor |
| 216 | 216 | fn search_traced( |
| 217 | - reference: &[u8], | |
| 217 | + reference: &[u16], | |
| 218 | 218 | stride: usize, |
| 219 | 219 | ref_width: usize, |
| 220 | 220 | ref_height: usize, |
| @@ -247,7 +247,7 @@ fn search_traced( | ||
| 247 | 247 | /// [`SEARCH_INITIAL_STEP_PEL`]. |
| 248 | 248 | #[allow(clippy::too_many_arguments)] |
| 249 | 249 | fn search_traced_from_step( |
| 250 | - reference: &[u8], | |
| 250 | + reference: &[u16], | |
| 251 | 251 | stride: usize, |
| 252 | 252 | ref_width: usize, |
| 253 | 253 | ref_height: usize, |
| @@ -263,26 +263,31 @@ fn search_traced_from_step( | ||
| 263 | 263 | assert_eq!(source.len(), block_w * block_h, "source is one block"); |
| 264 | 264 | assert!(!reference.is_empty(), "a reference plane has samples"); |
| 265 | 265 | |
| 266 | - // lane-hbd r2: this encoder-side search stays `u8` (see | |
| 267 | - // `encode::intra_predict_u8`'s doc comment) -- `reference` is converted | |
| 268 | - // once here, outside the per-candidate closure, since `mc::predict` now | |
| 269 | - // takes `u16`. | |
| 270 | - let reference16: Vec<u16> = reference.iter().map(|&v| u16::from(v)).collect(); | |
| 266 | + // lane-av1speed: `reference` arrives as the DPB picture's own `u16` | |
| 267 | + // plane. It used to be handed the encoder's `u8` shadow copy and | |
| 268 | + // converted here, whole-plane, on EVERY call -- one 245k-element | |
| 269 | + // allocate-and-convert per searched block, which profiled as the single | |
| 270 | + // largest symbol in the encoder (30% of `sequence_bench_sanity`). | |
| 271 | 271 | let mut dst16 = vec![0u16; block_w * block_h]; |
| 272 | 272 | let mut cost_of = |mv: (i32, i32)| -> f64 { |
| 273 | 273 | let x_q4 = (block_x as i32) * 16 + mv.1 * Q4_PER_Q3; |
| 274 | 274 | let y_q4 = (block_y as i32) * 16 + mv.0 * Q4_PER_Q3; |
| 275 | 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 | 277 | &mut dst16, fctx, |
| 278 | 278 | ); |
| 279 | - let sad: f64 = source | |
| 279 | + // 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 | 285 | .iter() |
| 281 | 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 | 288 | .sum(); |
| 284 | 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 | 293 | let mut trace = Vec::new(); |
| @@ -405,6 +410,7 @@ mod tests { | ||
| 405 | 410 | let width = 64; |
| 406 | 411 | let height = 64; |
| 407 | 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 | 414 | let block_w = 8; |
| 409 | 415 | let block_h = 8; |
| 410 | 416 | // Anchor the source block away from the plane's edges so every sign |
| @@ -433,7 +439,7 @@ mod tests { | ||
| 433 | 439 | } |
| 434 | 440 | } |
| 435 | 441 | let result = search( |
| 436 | - &plane, | |
| 442 | + &plane16, | |
| 437 | 443 | width, |
| 438 | 444 | width, |
| 439 | 445 | height, |
| @@ -463,6 +469,7 @@ mod tests { | ||
| 463 | 469 | let plane: Vec<u8> = (0..width * height) |
| 464 | 470 | .map(|i| (2 * (i % width)) as u8) |
| 465 | 471 | .collect(); |
| 472 | + let plane16: Vec<u16> = plane.iter().map(|&v| u16::from(v)).collect(); | |
| 466 | 473 | let block_w = 8; |
| 467 | 474 | let block_h = 6; |
| 468 | 475 | let anchor_x = 10; |
| @@ -481,7 +488,7 @@ mod tests { | ||
| 481 | 488 | } |
| 482 | 489 | } |
| 483 | 490 | let result = search( |
| 484 | - &plane, | |
| 491 | + &plane16, | |
| 485 | 492 | width, |
| 486 | 493 | width, |
| 487 | 494 | height, |
| @@ -506,6 +513,7 @@ mod tests { | ||
| 506 | 513 | let width = 48; |
| 507 | 514 | let height = 48; |
| 508 | 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 | 517 | let block_w = 8; |
| 510 | 518 | let block_h = 8; |
| 511 | 519 | let anchor_x = 20; |
| @@ -518,7 +526,7 @@ mod tests { | ||
| 518 | 526 | } |
| 519 | 527 | } |
| 520 | 528 | let (_result, trace) = search_traced( |
| 521 | - &plane, | |
| 529 | + &plane16, | |
| 522 | 530 | width, |
| 523 | 531 | width, |
| 524 | 532 | height, |
| @@ -566,6 +574,7 @@ mod tests { | ||
| 566 | 574 | let width = 96; |
| 567 | 575 | let height = 96; |
| 568 | 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 | 578 | let block_w = 8; |
| 570 | 579 | let block_h = 8; |
| 571 | 580 | let anchor_x = 48; |
| @@ -586,7 +595,7 @@ mod tests { | ||
| 586 | 595 | } |
| 587 | 596 | } |
| 588 | 597 | let (result, trace) = search_traced_from_step( |
| 589 | - &plane, | |
| 598 | + &plane16, | |
| 590 | 599 | width, |
| 591 | 600 | width, |
| 592 | 601 | height, |
crates/ec-av1/src/transform.rsmodified+12 -2
| @@ -1540,8 +1540,18 @@ fn fft(a: &mut [Complex]) { | ||
| 1540 | 1540 | } |
| 1541 | 1541 | let mut len = 2; |
| 1542 | 1542 | while len <= n { |
| 1543 | - let ang = -2.0 * std::f64::consts::PI / len as f64; | |
| 1544 | - let wlen = (ang.cos(), ang.sin()); | |
| 1543 | + // lane-av1speed: the stage twiddle depends on `len` alone, so the | |
| 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 | 1555 | let mut i = 0; |
| 1546 | 1556 | while i < n { |
| 1547 | 1557 | let mut w = (1.0, 0.0); |