forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-perf2' (10a21e5e): separable MC kernels restructured (contiguous passes, i16 horizontal multiply, whole-pel shortcuts, unscaled router for compound); byte-exact on 1080p and 4K dumps, mc self time 38% to 15%

Browse files at this commit

6352a70c9503c871538f4902c7705704798af9acAhmet Kaan GümüşParent: 2b9468dc8bf4a0
crates/ec-av1/src/mc.rsmodified+248 -73
@@ -243,6 +243,132 @@ fn sample(
243 i32::from(reference[cy * stride + cx])243 i32::from(reference[cy * stride + cx])
244 }244 }
245 245
246+/// lane-perf2: one row of the separable filter's horizontal pass, dav1d
247+/// style. When the row's whole 8-tap footprint (`x0-3 .. x0+block_w+4`) lies
248+/// inside the reference's true extent -- the overwhelmingly common case --
249+/// the row is one contiguous slice and the taps are a fixed `[i32; 8]`, so
250+/// the per-sample `Clip3` and the per-tap table load both leave the inner
251+/// loop and it auto-vectorises. The edge case keeps the exact spec form
252+/// (`sample`'s clamp per tap); both arms compute the same sums.
253+#[inline]
254+fn hpass_row(reference: &[u16], row_base: usize, true_width: usize, x0: i32, taps: &[i32; 8], out: &mut [i32]) {
255+ let block_w = out.len();
256+ let start = x0 - 3;
257+ if start >= 0 && start as usize + block_w + 7 <= true_width {
258+ let s = row_base + start as usize;
259+ if let Some(src) = reference.get(s..s + block_w + 7) {
260+ // Both factors are 16-bit (a tap is |x| <= 128, a sample fits the
261+ // bit depth), so the products are exactly the i32 ones above but
262+ // the widening multiply is one instruction per pair.
263+ let t16 = [
264+ taps[0] as i16, taps[1] as i16, taps[2] as i16, taps[3] as i16,
265+ taps[4] as i16, taps[5] as i16, taps[6] as i16, taps[7] as i16,
266+ ];
267+ for (o, w) in out.iter_mut().zip(src.windows(8)) {
268+ let mut sum = 0i32;
269+ for t in 0..8 {
270+ sum += i32::from(t16[t]) * i32::from(w[t] as i16);
271+ }
272+ *o = round2(sum, INTER_ROUND_0);
273+ }
274+ return;
275+ }
276+ }
277+ for (c, o) in out.iter_mut().enumerate() {
278+ let mut sum = 0i32;
279+ for (t, &tap) in taps.iter().enumerate() {
280+ let x = (x0 + c as i32 + t as i32 - 3).clamp(0, true_width as i32 - 1) as usize;
281+ sum += tap * i32::from(reference[row_base + x]);
282+ }
283+ *o = round2(sum, INTER_ROUND_0);
284+ }
285+}
286+
287+/// lane-perf2: the unscaled horizontal pass, `block_h + 7` rows of
288+/// [`hpass_row`] with the vertical clamp hoisted out of the row.
289+#[allow(clippy::too_many_arguments)]
290+fn horizontal_pass_unscaled(
291+ reference: &[u16],
292+ stride: usize,
293+ true_width: usize,
294+ true_height: usize,
295+ x0: i32,
296+ y0: i32,
297+ taps: &[i32; 8],
298+ block_w: usize,
299+ rows: usize,
300+ intermediate: &mut [i32],
301+) {
302+ // Fraction 0 is the identity tap (`128` at slot 3), and `Round2(128 * s,
303+ // InterRound0) == 16 * s` exactly, so a whole-pel horizontal position
304+ // needs no filter at all -- one widened row read.
305+ let identity = taps[3] == 128 && taps.iter().enumerate().all(|(i, &t)| i == 3 || t == 0);
306+ for r in 0..rows {
307+ let y = (y0 - 3 + r as i32).clamp(0, true_height as i32 - 1) as usize;
308+ let out = &mut intermediate[r * block_w..(r + 1) * block_w];
309+ if identity {
310+ let gain = 128 >> INTER_ROUND_0;
311+ if x0 >= 0 && x0 as usize + block_w <= true_width {
312+ if let Some(src) = reference.get(y * stride + x0 as usize..y * stride + x0 as usize + block_w) {
313+ for (o, &s) in out.iter_mut().zip(src) {
314+ *o = gain * i32::from(s);
315+ }
316+ continue;
317+ }
318+ }
319+ for (c, o) in out.iter_mut().enumerate() {
320+ let x = (x0 + c as i32).clamp(0, true_width as i32 - 1) as usize;
321+ *o = gain * i32::from(reference[y * stride + x]);
322+ }
323+ continue;
324+ }
325+ hpass_row(reference, y * stride, true_width, x0, taps, out);
326+ }
327+}
328+
329+/// lane-perf2: one output row of the vertical pass, accumulated tap-major so
330+/// the inner loop walks two contiguous `i32` slices (vectorisable) instead of
331+/// striding the intermediate by `block_w` per tap. A zero tap is skipped --
332+/// adding `0 * x` is exactly nothing, and the 4-tap kernels zero four of the
333+/// eight slots. `acc` is left holding the unrounded sums.
334+#[inline]
335+fn vpass_row(intermediate: &[i32], block_w: usize, row: usize, taps: &[i32; 8], acc: &mut [i32]) {
336+ acc.fill(0);
337+ for (t, &tap) in taps.iter().enumerate() {
338+ if tap == 0 {
339+ continue;
340+ }
341+ let base = (row + t) * block_w;
342+ for (a, &s) in acc.iter_mut().zip(&intermediate[base..base + block_w]) {
343+ *a += tap * s;
344+ }
345+ }
346+}
347+
348+thread_local! {
349+ /// lane-perf2: the separable filter's intermediate buffer, reused across
350+ /// calls -- a 4K inter frame runs `predict*` hundreds of thousands of
351+ /// times and each call used to `vec![0i32; (block_h+7) * block_w]`. The
352+ /// horizontal pass writes every element it later reads, so no clearing is
353+ /// needed; the tail `block_w` elements are the vertical pass's row
354+ /// accumulator.
355+ static MC_SCRATCH: std::cell::RefCell<Vec<i32>> = const { std::cell::RefCell::new(Vec::new()) };
356+}
357+
358+/// Runs `f` on a scratch slice of `rows * block_w` intermediate elements plus
359+/// a `block_w` accumulator (see [`MC_SCRATCH`]).
360+fn with_scratch<R>(rows: usize, block_w: usize, f: impl FnOnce(&mut [i32], &mut [i32]) -> R) -> R {
361+ MC_SCRATCH.with(|s| {
362+ let mut buf = s.borrow_mut();
363+ let need = rows * block_w + block_w;
364+ if buf.len() < need {
365+ buf.resize(need, 0);
366+ }
367+ let (inter, acc) = buf[..need].split_at_mut(rows * block_w);
368+ f(inter, acc)
369+ })
370+}
371+
246 /// Predicts one `block_w * block_h` block into `dst`, row-major, from372 /// Predicts one `block_w * block_h` block into `dst`, row-major, from
247 /// `reference` (row-major, stride `stride`, true content373 /// `reference` (row-major, stride `stride`, true content
248 /// `true_width` x `true_height` -- a reference frame's decoded picture374 /// `true_width` x `true_height` -- a reference frame's decoded picture
@@ -395,11 +521,19 @@ pub fn predict_with_filters_kern(
395 // it whole-pel candidates from stage 1).521 // it whole-pel candidates from stage 1).
396 if xfrac == 0 && yfrac == 0 {522 if xfrac == 0 && yfrac == 0 {
397 for row in 0..block_h {523 for row in 0..block_h {
398- let y = y0 + row as i32;524+ let y = (y0 + row as i32).clamp(0, true_height as i32 - 1) as usize;
399- for col in 0..block_w {525+ let row_base = y * stride;
400- let x = x0 + col as i32;526+ let out = &mut dst[row * block_w..(row + 1) * block_w];
401- dst[row * block_w + col] =527+ // lane-perf2: an interior row is one contiguous copy.
402- sample(reference, stride, true_width, true_height, x, y) as u16;528+ if x0 >= 0 && x0 as usize + block_w <= true_width {
529+ if let Some(src) = reference.get(row_base + x0 as usize..row_base + x0 as usize + block_w) {
530+ out.copy_from_slice(src);
531+ continue;
532+ }
533+ }
534+ for (col, o) in out.iter_mut().enumerate() {
535+ let x = (x0 + col as i32).clamp(0, true_width as i32 - 1) as usize;
536+ *o = reference[row_base + x];
403 }537 }
404 }538 }
405 #[cfg(test)]539 #[cfg(test)]
@@ -423,29 +557,31 @@ pub fn predict_with_filters_kern(
423 557
424 // The vertical pass reads 3 rows above and 4 below the block, so the558 // The vertical pass reads 3 rows above and 4 below the block, so the
425 // horizontal pass must produce that many extra intermediate rows.559 // horizontal pass must produce that many extra intermediate rows.
426- let rows = block_h + 7;560+ // A whole-pel vertical position needs neither the 3 rows above nor the 4
427- let mut intermediate = vec![0i32; rows * block_w];561+ // below, and its vertical pass is the identity tap: `Round2(128 * a,
428- for r in 0..rows {562+ // InterRound1) == Round2(a, InterRound1 - 7)`. Passing `y0 + 3` puts the
429- let y = y0 - 3 + r as i32;563+ // block's own first row where the pass would have put the 4th.
430- for c in 0..block_w {564+ let rows = if yfrac == 0 { block_h } else { block_h + 7 };
431- let mut sum = 0;565+ let max = crate::decode::sample_max();
432- for (t, &tap) in h_filter.iter().enumerate() {566+ with_scratch(rows, block_w, |intermediate, acc| {
433- let x = x0 + c as i32 + t as i32 - 3;567+ horizontal_pass_unscaled(
434- sum += tap * sample(reference, stride, true_width, true_height, x, y);568+ reference, stride, true_width, true_height, x0,
569+ if yfrac == 0 { y0 + 3 } else { y0 }, h_filter, block_w, rows,
570+ intermediate,
571+ );
572+ if yfrac == 0 {
573+ for (d, &a) in dst.iter_mut().zip(intermediate.iter()) {
574+ *d = round2(a, INTER_ROUND_1 - 7).clamp(0, max) as u16;
435 }575 }
436- intermediate[r * block_w + c] = round2(sum, INTER_ROUND_0);576+ return;
437 }577 }
438- }578+ for row in 0..block_h {
439-579+ vpass_row(intermediate, block_w, row, v_filter, acc);
440- for row in 0..block_h {580+ for (d, &a) in dst[row * block_w..(row + 1) * block_w].iter_mut().zip(acc.iter()) {
441- for col in 0..block_w {581+ *d = round2(a, INTER_ROUND_1).clamp(0, max) as u16;
442- let mut sum = 0;
443- for (t, &tap) in v_filter.iter().enumerate() {
444- sum += tap * intermediate[(row + t) * block_w + col];
445 }582 }
446- dst[row * block_w + col] = round2(sum, INTER_ROUND_1).clamp(0, crate::decode::sample_max()) as u16;
447 }583 }
448- }584+ });
449 585
450 #[cfg(test)]586 #[cfg(test)]
451 crate::encode::stage_add(1, stage_t.elapsed());587 crate::encode::stage_add(1, stage_t.elapsed());
@@ -590,6 +726,58 @@ fn horizontal_scaled_pass(
590 }726 }
591 }727 }
592 728
729+/// lane-perf2: the horizontal pass every two-pass entry point takes.
730+/// `horizontal_scaled_pass` was carrying the UNSCALED work too (every
731+/// compound prediction goes through it whatever the reference's scale), and
732+/// its per-column walk re-derives an integer position and a filter phase
733+/// that are constant at `REF_NO_SCALE`, reads the reference column-major,
734+/// and clamps per tap -- 14% of decode self time. At `REF_NO_SCALE` the
735+/// scaled walk reduces algebraically to `x0 = x_q4 / 16`, phase `x_q4 % 16`
736+/// (`pos_x_q10 == x_q4 * 64 + 32`, so `int_pel == x0` and
737+/// `(x_qn & 1023) >> 6 == xfrac` for every column), which is exactly
738+/// [`horizontal_pass_unscaled`] -- and
739+/// `predict_scaled_at_no_scale_matches_predict_with_filters` pins that
740+/// equality end to end. A genuinely scaled reference (superres) still takes
741+/// the general path.
742+#[allow(clippy::too_many_arguments)]
743+fn horizontal_pass(
744+ reference: &[u16],
745+ stride: usize,
746+ true_width: usize,
747+ true_height: usize,
748+ x_q4: i32,
749+ y0: i32,
750+ x_scale_fp: i64,
751+ block_w: usize,
752+ kern_w: usize,
753+ rows: usize,
754+ h_kind: InterpFilterKind,
755+ intermediate: &mut [i32],
756+) {
757+ if x_scale_fp == REF_NO_SCALE {
758+ let (h_wide, h_narrow) = h_kind.tables();
759+ let xfrac = x_q4.rem_euclid(16) as usize;
760+ let taps = if kern_w <= 4 { &h_narrow[xfrac] } else { &h_wide[xfrac] };
761+ horizontal_pass_unscaled(
762+ reference,
763+ stride,
764+ true_width,
765+ true_height,
766+ x_q4.div_euclid(16),
767+ y0,
768+ taps,
769+ block_w,
770+ rows,
771+ intermediate,
772+ );
773+ return;
774+ }
775+ horizontal_scaled_pass(
776+ reference, stride, true_width, true_height, x_q4, y0, x_scale_fp, block_w, kern_w,
777+ rows, h_kind, intermediate,
778+ );
779+}
780+
593 /// [`predict_with_filters`]'s scaled-reference counterpart (spec 7.11.3.3):781 /// [`predict_with_filters`]'s scaled-reference counterpart (spec 7.11.3.3):
594 /// used only when the stored reference's luma width differs from the current782 /// used only when the stored reference's luma width differs from the current
595 /// frame's (`use_superres` on a non-key frame). `x_scale_fp` is783 /// frame's (`use_superres` on a non-key frame). `x_scale_fp` is
@@ -663,33 +851,27 @@ pub fn predict_scaled_kern(
663 &v_wide[yfrac]851 &v_wide[yfrac]
664 };852 };
665 853
666- let rows = block_h + 7;854+ let rows = if yfrac == 0 { block_h } else { block_h + 7 };
667- let mut intermediate = vec![0i32; rows * block_w];855+ let max = crate::decode::sample_max();
668- horizontal_scaled_pass(856+ with_scratch(rows, block_w, |intermediate, acc| {
669- reference,857+ horizontal_pass(
670- stride,858+ reference, stride, true_width, true_height, x_q4,
671- true_width,859+ if yfrac == 0 { y0 + 3 } else { y0 }, x_scale_fp, block_w,
672- true_height,860+ kern_w, rows, h_kind, intermediate,
673- x_q4,861+ );
674- y0,862+ if yfrac == 0 {
675- x_scale_fp,863+ for (d, &a) in dst.iter_mut().zip(intermediate.iter()) {
676- block_w,864+ *d = round2(a, INTER_ROUND_1 - 7).clamp(0, max) as u16;
677- kern_w,
678- rows,
679- h_kind,
680- &mut intermediate,
681- );
682-
683- for row in 0..block_h {
684- for col in 0..block_w {
685- let mut sum = 0;
686- for (t, &tap) in v_filter.iter().enumerate() {
687- sum += tap * intermediate[(row + t) * block_w + col];
688 }865 }
689- dst[row * block_w + col] =866+ return;
690- round2(sum, INTER_ROUND_1).clamp(0, crate::decode::sample_max()) as u16;
691 }867 }
692- }868+ for row in 0..block_h {
869+ vpass_row(intermediate, block_w, row, v_filter, acc);
870+ for (d, &a) in dst[row * block_w..(row + 1) * block_w].iter_mut().zip(acc.iter()) {
871+ *d = round2(a, INTER_ROUND_1).clamp(0, max) as u16;
872+ }
873+ }
874+ });
693 }875 }
694 876
695 /// `InterRound1` for a compound ref (spec 7.11.3.2's `isCompound` branch,877 /// `InterRound1` for a compound ref (spec 7.11.3.2's `isCompound` branch,
@@ -786,32 +968,25 @@ pub fn predict_compound_intermediate_kern(
786 &v_wide[yfrac]968 &v_wide[yfrac]
787 };969 };
788 970
789- let rows = block_h + 7;971+ let rows = if yfrac == 0 { block_h } else { block_h + 7 };
790- let mut intermediate = vec![0i32; rows * block_w];972+ with_scratch(rows, block_w, |intermediate, acc| {
791- horizontal_scaled_pass(973+ horizontal_pass(
792- reference,974+ reference, stride, true_width, true_height, x_q4,
793- stride,975+ if yfrac == 0 { y0 + 3 } else { y0 }, x_scale_fp, block_w,
794- true_width,976+ kern_w, rows, h_kind, intermediate,
795- true_height,977+ );
796- x_q4,978+ if yfrac == 0 {
797- y0,979+ // `Round2(128 * a, INTER_ROUND_1_COMPOUND) == a`.
798- x_scale_fp,980+ dst.copy_from_slice(&intermediate[..dst.len()]);
799- block_w,981+ return;
800- kern_w,982+ }
801- rows,983+ for row in 0..block_h {
802- h_kind,984+ vpass_row(intermediate, block_w, row, v_filter, acc);
803- &mut intermediate,985+ for (d, &a) in dst[row * block_w..(row + 1) * block_w].iter_mut().zip(acc.iter()) {
804- );986+ *d = round2(a, INTER_ROUND_1_COMPOUND);
805-
806- for row in 0..block_h {
807- for col in 0..block_w {
808- let mut sum = 0;
809- for (t, &tap) in v_filter.iter().enumerate() {
810- sum += tap * intermediate[(row + t) * block_w + col];
811 }987 }
812- dst[row * block_w + col] = round2(sum, INTER_ROUND_1_COMPOUND);
813 }988 }
814- }989+ });
815 }990 }
816 991
817 /// Blends two [`predict_compound_intermediate`] outputs into a finished992 /// Blends two [`predict_compound_intermediate`] outputs into a finished