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 {
158158 }
159159 }
160160
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+
161185 /// How often each partition decision was taken since the last
162186 /// [`take_partition_hits`], so a gate can report how often a new partition
163187 /// fires rather than assume it does (gate-blind-to-feature). Index 0 is an
@@ -1624,8 +1648,19 @@ impl Reach {
16241648 /// What one symbol costs against a CDF, in bits.
16251649 pub(crate) fn symbol_bits(cdf: &[u16], symbol: usize) -> f64 {
16261650 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)]
16291664 }
16301665
16311666 /// What the tile writer spends to say a block is coded in each of the thirteen
@@ -1801,7 +1836,6 @@ fn code_square_inter(
18011836 search: &Search,
18021837 mode_bits: &[f64; 13],
18031838 reference: &Picture,
1804- ref_y8: &[u8],
18051839 stack: &MvStack, fctx: &crate::decode::FrameCtx,
18061840 ) -> (BlockCoeffs, f64) {
18071841 let (intra_block, intra_cost) = code_square(luma, chroma, (x, y), side, search, mode_bits, fctx);
@@ -1933,7 +1967,7 @@ fn code_square_inter(
19331967 if leaf_new_mv() {
19341968 let source_block = luma.source_block(x, y, side);
19351969 let found = motion::search(
1936- ref_y8,
1970+ ref_luma.0,
19371971 ref_luma.1,
19381972 ref_luma.2,
19391973 ref_luma.3,
@@ -1947,18 +1981,28 @@ fn code_square_inter(
19471981 );
19481982 let new_mv = round_to_valid_mv(found.mv, stack.pred_mv);
19491983 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+ };
19622006 let skip_new = luma_new.levels.iter().all(|&l| l == 0)
19632007 && u_new.levels.iter().all(|&l| l == 0)
19642008 && v_new.levels.iter().all(|&l| l == 0);
@@ -2859,7 +2903,12 @@ fn mc_trial(
28592903 // lane-hbd r4: `reference` (a DPB `Picture`'s plane) is `u16` now that
28602904 // `Picture` is widened; `prediction`/the encoder stay `u8` (encoder is
28612905 // 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];
28632912 mc::predict(
28642913 reference,
28652914 stride,
@@ -2869,10 +2918,14 @@ fn mc_trial(
28692918 y_q4,
28702919 side,
28712920 side,
2872- &mut prediction16, fctx,
2921+ prediction16, fctx,
28732922 );
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)
28762929 }
28772930
28782931 /// Codes one 32x32 block of an inter frame as whichever costs least of: each
@@ -2901,7 +2954,6 @@ fn search_inter_block(
29012954 search: &Search,
29022955 mode_bits: &[f64; 13],
29032956 reference: &Picture,
2904- ref_y8: &[u8],
29052957 stack: &MvStack, fctx: &crate::decode::FrameCtx,
29062958 ) -> (BlockCoeffs, f64) {
29072959 // `luma_set` is the INTRA candidates' table; the two inter candidates
@@ -3057,6 +3109,14 @@ fn search_inter_block(
30573109 // The NEARESTMV candidate: `skip` is decided below from whether its
30583110 // trial actually found a nonzero level, once its residual is priced
30593111 // 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;
30603120 {
30613121 let mv = stack.nearest_mv;
30623122 let luma_trial = mc_trial(
@@ -3123,6 +3183,7 @@ fn search_inter_block(
31233183 } else {
31243184 luma_trial.bits + u.bits + v.bits
31253185 });
3186+ nearest_trials = (luma_trial.clone(), u.clone(), v.clone());
31263187 consider(Candidate {
31273188 cost,
31283189 luma: luma_trial,
@@ -3141,7 +3202,7 @@ fn search_inter_block(
31413202 #[cfg(test)]
31423203 let t = std::time::Instant::now();
31433204 let found = motion::search(
3144- ref_y8,
3205+ ref_luma.0,
31453206 ref_luma.1,
31463207 ref_luma.2,
31473208 ref_luma.3,
@@ -3157,54 +3218,60 @@ fn search_inter_block(
31573218 stage_add(0, t.elapsed());
31583219 let mv = round_to_valid_mv(found.mv, stack.pred_mv);
31593220 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+ };
32083275 let drl_bits = if stack.entries.len() > 1 {
32093276 symbol_bits(&cdf::DRL_MODE[stack.drl_ctx[0]], 0)
32103277 } else {
@@ -3360,7 +3427,6 @@ pub(crate) fn encode_inter_frame(
33603427 // integer-`u8`-scale), so the DPB reference plane is narrowed for it --
33613428 // once per frame (lane-av1rd2), not once per block, which is a whole
33623429 // 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();
33643430 let mut grid = MiGrid::new(mi_cols, mi_rows);
33653431 let mut blocks = vec![Quadrant::Whole(BlockCoeffs::default()); cols * rows];
33663432
@@ -3407,7 +3473,6 @@ pub(crate) fn encode_inter_frame(
34073473 &search,
34083474 &mode_bits_table,
34093475 reference,
3410- &ref_y8,
34113476 &stack, fctx,
34123477 );
34133478 cost_whole += search.lambda * partition_bits(BLOCK, false);
@@ -3439,7 +3504,13 @@ pub(crate) fn encode_inter_frame(
34393504 header.mi_rows,
34403505 )
34413506 });
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())
34433514 .then(|| {
34443515 restore(&mut luma, &mut chroma, (x, y), BLOCK, &base);
34453516 let mut cost = search.lambda * partition_bits(BLOCK, true);
@@ -3459,8 +3530,7 @@ pub(crate) fn encode_inter_frame(
34593530 &search,
34603531 &mode_bits_table,
34613532 reference,
3462- &ref_y8,
3463- &stack, fctx,
3533+ &stack, fctx,
34643534 );
34653535 let leaf_cost = leaf_cost
34663536 + search.lambda * partition_bits(SUB, false);
@@ -3471,7 +3541,9 @@ pub(crate) fn encode_inter_frame(
34713541 // the writer codes as a real PARTITION_SPLIT
34723542 // at BLOCK_16X16 (lane-av1rd2). Same
34733543 // 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())
34753547 .then(|| {
34763548 restore(
34773549 &mut luma, &mut chroma, (lx, ly), SUB, &leaf_base,
@@ -3494,8 +3566,7 @@ pub(crate) fn encode_inter_frame(
34943566 &search,
34953567 &mode_bits_table,
34963568 reference,
3497- &ref_y8,
3498- &stack8, fctx,
3569+ &stack8, fctx,
34993570 );
35003571 cost8 += cost;
35013572 record_mi(&mut grid, mr, mc, 2, leaf8.inter);
@@ -3596,8 +3667,7 @@ pub(crate) fn encode_inter_frame(
35963667 &search,
35973668 &mode_bits_table,
35983669 reference,
3599- &ref_y8,
3600- &stack, fctx,
3670+ &stack, fctx,
36013671 );
36023672 for dr in 0..4 {
36033673 for dc in 0..4 {
@@ -3657,8 +3727,7 @@ pub(crate) fn encode_inter_frame(
36573727 &search,
36583728 &mode_bits_table,
36593729 reference,
3660- &ref_y8,
3661- &stack, fctx,
3730+ &stack, fctx,
36623731 );
36633732 for dr in 0..2 {
36643733 for dc in 0..2 {
crates/ec-av1/src/motion.rsmodified+25 -16
@@ -190,7 +190,7 @@ pub struct MotionSearch {
190190 /// empty (the same contracts [`predict`] has).
191191 #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor
192192 pub(crate) fn search(
193- reference: &[u8],
193+ reference: &[u16],
194194 stride: usize,
195195 ref_width: usize,
196196 ref_height: usize,
@@ -214,7 +214,7 @@ pub(crate) fn search(
214214 /// checks.
215215 #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor
216216 fn search_traced(
217- reference: &[u8],
217+ reference: &[u16],
218218 stride: usize,
219219 ref_width: usize,
220220 ref_height: usize,
@@ -247,7 +247,7 @@ fn search_traced(
247247 /// [`SEARCH_INITIAL_STEP_PEL`].
248248 #[allow(clippy::too_many_arguments)]
249249 fn search_traced_from_step(
250- reference: &[u8],
250+ reference: &[u16],
251251 stride: usize,
252252 ref_width: usize,
253253 ref_height: usize,
@@ -263,26 +263,31 @@ fn search_traced_from_step(
263263 assert_eq!(source.len(), block_w * block_h, "source is one block");
264264 assert!(!reference.is_empty(), "a reference plane has samples");
265265
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`).
271271 let mut dst16 = vec![0u16; block_w * block_h];
272272 let mut cost_of = |mv: (i32, i32)| -> f64 {
273273 let x_q4 = (block_x as i32) * 16 + mv.1 * Q4_PER_Q3;
274274 let y_q4 = (block_y as i32) * 16 + mv.0 * Q4_PER_Q3;
275275 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,
277277 &mut dst16, fctx,
278278 );
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
280285 .iter()
281286 .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())
283288 .sum();
284289 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)
286291 };
287292
288293 let mut trace = Vec::new();
@@ -405,6 +410,7 @@ mod tests {
405410 let width = 64;
406411 let height = 64;
407412 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();
408414 let block_w = 8;
409415 let block_h = 8;
410416 // Anchor the source block away from the plane's edges so every sign
@@ -433,7 +439,7 @@ mod tests {
433439 }
434440 }
435441 let result = search(
436- &plane,
442+ &plane16,
437443 width,
438444 width,
439445 height,
@@ -463,6 +469,7 @@ mod tests {
463469 let plane: Vec<u8> = (0..width * height)
464470 .map(|i| (2 * (i % width)) as u8)
465471 .collect();
472+ let plane16: Vec<u16> = plane.iter().map(|&v| u16::from(v)).collect();
466473 let block_w = 8;
467474 let block_h = 6;
468475 let anchor_x = 10;
@@ -481,7 +488,7 @@ mod tests {
481488 }
482489 }
483490 let result = search(
484- &plane,
491+ &plane16,
485492 width,
486493 width,
487494 height,
@@ -506,6 +513,7 @@ mod tests {
506513 let width = 48;
507514 let height = 48;
508515 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();
509517 let block_w = 8;
510518 let block_h = 8;
511519 let anchor_x = 20;
@@ -518,7 +526,7 @@ mod tests {
518526 }
519527 }
520528 let (_result, trace) = search_traced(
521- &plane,
529+ &plane16,
522530 width,
523531 width,
524532 height,
@@ -566,6 +574,7 @@ mod tests {
566574 let width = 96;
567575 let height = 96;
568576 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();
569578 let block_w = 8;
570579 let block_h = 8;
571580 let anchor_x = 48;
@@ -586,7 +595,7 @@ mod tests {
586595 }
587596 }
588597 let (result, trace) = search_traced_from_step(
589- &plane,
598+ &plane16,
590599 width,
591600 width,
592601 height,
crates/ec-av1/src/transform.rsmodified+12 -2
@@ -1540,8 +1540,18 @@ fn fft(a: &mut [Complex]) {
15401540 }
15411541 let mut len = 2;
15421542 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];
15451555 let mut i = 0;
15461556 while i < n {
15471557 let mut w = (1.0, 0.0);