From 8c7cbe4d3a6d4fa26cc5c7d60292a9ca49be56bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 4 Sep 2026 13:16:34 +0300 Subject: [PATCH] perf(av1): unscaled compound MC leaves the scaled walk horizontal_scaled_pass was 14% of decode self time, and almost none of that was scaling: every compound prediction routed through it whatever the reference's scale, so an unscaled block paid a per-column integer-position derivation, a per-column filter-phase lookup, a column-major read of the reference and a per-tap clamp. At REF_NO_SCALE that walk reduces algebraically to the fixed x0/xfrac split (pos_x_q10 == x_q4 * 64 + 32 makes int_pel == x_q4 / 16 and (x_qn & 1023) >> 6 == x_q4 % 16 for every column), so a new horizontal_pass router sends it to the contiguous unscaled pass and leaves the general walk for superres. Both vertical passes now accumulate tap-major over contiguous slices and reuse the thread-local scratch, as the single-reference one does. Byte-exact: 1080p 10-bit (299 frames) and 2160p HDR EC_PROBE_OUT16 dumps both cmp identical to the pre-lane binary. Interleaved medians on the 1080p segment: 23.39 s base -> 18.21 s (12.8 -> 16.4 fps). --- crates/ec-av1/src/mc.rs | 120 ++++++++++++++++++++++++---------------- 1 file changed, 73 insertions(+), 47 deletions(-) diff --git a/crates/ec-av1/src/mc.rs b/crates/ec-av1/src/mc.rs index f7d25004..ced1efe6 100644 --- a/crates/ec-av1/src/mc.rs +++ b/crates/ec-av1/src/mc.rs @@ -694,6 +694,58 @@ fn horizontal_scaled_pass( } } +/// lane-perf2: the horizontal pass every two-pass entry point takes. +/// `horizontal_scaled_pass` was carrying the UNSCALED work too (every +/// compound prediction goes through it whatever the reference's scale), and +/// its per-column walk re-derives an integer position and a filter phase +/// that are constant at `REF_NO_SCALE`, reads the reference column-major, +/// and clamps per tap -- 14% of decode self time. At `REF_NO_SCALE` the +/// scaled walk reduces algebraically to `x0 = x_q4 / 16`, phase `x_q4 % 16` +/// (`pos_x_q10 == x_q4 * 64 + 32`, so `int_pel == x0` and +/// `(x_qn & 1023) >> 6 == xfrac` for every column), which is exactly +/// [`horizontal_pass_unscaled`] -- and +/// `predict_scaled_at_no_scale_matches_predict_with_filters` pins that +/// equality end to end. A genuinely scaled reference (superres) still takes +/// the general path. +#[allow(clippy::too_many_arguments)] +fn horizontal_pass( + reference: &[u16], + stride: usize, + true_width: usize, + true_height: usize, + x_q4: i32, + y0: i32, + x_scale_fp: i64, + block_w: usize, + kern_w: usize, + rows: usize, + h_kind: InterpFilterKind, + intermediate: &mut [i32], +) { + if x_scale_fp == REF_NO_SCALE { + let (h_wide, h_narrow) = h_kind.tables(); + let xfrac = x_q4.rem_euclid(16) as usize; + let taps = if kern_w <= 4 { &h_narrow[xfrac] } else { &h_wide[xfrac] }; + horizontal_pass_unscaled( + reference, + stride, + true_width, + true_height, + x_q4.div_euclid(16), + y0, + taps, + block_w, + rows, + intermediate, + ); + return; + } + horizontal_scaled_pass( + reference, stride, true_width, true_height, x_q4, y0, x_scale_fp, block_w, kern_w, + rows, h_kind, intermediate, + ); +} + /// [`predict_with_filters`]'s scaled-reference counterpart (spec 7.11.3.3): /// used only when the stored reference's luma width differs from the current /// frame's (`use_superres` on a non-key frame). `x_scale_fp` is @@ -768,32 +820,19 @@ pub fn predict_scaled_kern( }; let rows = block_h + 7; - let mut intermediate = vec![0i32; rows * block_w]; - horizontal_scaled_pass( - reference, - stride, - true_width, - true_height, - x_q4, - y0, - x_scale_fp, - block_w, - kern_w, - rows, - h_kind, - &mut intermediate, - ); - - for row in 0..block_h { - for col in 0..block_w { - let mut sum = 0; - for (t, &tap) in v_filter.iter().enumerate() { - sum += tap * intermediate[(row + t) * block_w + col]; + let max = crate::decode::sample_max(); + with_scratch(rows, block_w, |intermediate, acc| { + horizontal_pass( + reference, stride, true_width, true_height, x_q4, y0, x_scale_fp, block_w, + kern_w, rows, h_kind, intermediate, + ); + for row in 0..block_h { + vpass_row(intermediate, block_w, row, v_filter, acc); + for (d, &a) in dst[row * block_w..(row + 1) * block_w].iter_mut().zip(acc.iter()) { + *d = round2(a, INTER_ROUND_1).clamp(0, max) as u16; } - dst[row * block_w + col] = - round2(sum, INTER_ROUND_1).clamp(0, crate::decode::sample_max()) as u16; } - } + }); } /// `InterRound1` for a compound ref (spec 7.11.3.2's `isCompound` branch, @@ -891,31 +930,18 @@ pub fn predict_compound_intermediate_kern( }; let rows = block_h + 7; - let mut intermediate = vec![0i32; rows * block_w]; - horizontal_scaled_pass( - reference, - stride, - true_width, - true_height, - x_q4, - y0, - x_scale_fp, - block_w, - kern_w, - rows, - h_kind, - &mut intermediate, - ); - - for row in 0..block_h { - for col in 0..block_w { - let mut sum = 0; - for (t, &tap) in v_filter.iter().enumerate() { - sum += tap * intermediate[(row + t) * block_w + col]; + with_scratch(rows, block_w, |intermediate, acc| { + horizontal_pass( + reference, stride, true_width, true_height, x_q4, y0, x_scale_fp, block_w, + kern_w, rows, h_kind, intermediate, + ); + for row in 0..block_h { + vpass_row(intermediate, block_w, row, v_filter, acc); + for (d, &a) in dst[row * block_w..(row + 1) * block_w].iter_mut().zip(acc.iter()) { + *d = round2(a, INTER_ROUND_1_COMPOUND); } - dst[row * block_w + col] = round2(sum, INTER_ROUND_1_COMPOUND); } - } + }); } /// Blends two [`predict_compound_intermediate`] outputs into a finished