forge
Explore
Log in

Don't you dare go Hollow

Whole-pel axes skip the pass they do not need

Browse files at this commit

c8bf4a0588e7d34225784d6fae2db9758af11b84Ahmet Kaan GümüşParent: dcac6b3
  • perf
    • whole-pel axes skip the pass they do not need
Show full message
perf(av1): whole-pel axes skip the pass they do not need

A motion vector with a whole-pel component still ran the full two-pass
filter. Fraction 0 is the identity tap, so each axis has an exact shortcut:
Round2(128 * s, InterRound0) == 16 * s horizontally, and vertically
Round2(128 * a, InterRound1) == Round2(a, InterRound1 - 7) (== a for the
compound round), which also means the 3 rows above and 4 below the block
never have to be filtered -- a 4-tall chroma block drops from 11 horizontal
rows to 4.

Byte-exact: 1080p 10-bit and 2160p HDR EC_PROBE_OUT16 dumps both cmp
identical to the pre-lane binary. Interleaved medians on the 1080p segment,
6 pairs against the previous commit: 19.08 s -> 17.42 s (faster in 5 of 6).
crates/ec-av1/src/mc.rsmodified+52 -14
@@ -299,16 +299,30 @@ fn horizontal_pass_unscaled(
299299 rows: usize,
300300 intermediate: &mut [i32],
301301 ) {
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);
302306 for r in 0..rows {
303307 let y = (y0 - 3 + r as i32).clamp(0, true_height as i32 - 1) as usize;
304- hpass_row(
305- reference,
306- y * stride,
307- true_width,
308- x0,
309- taps,
310- &mut intermediate[r * block_w..(r + 1) * block_w],
311- );
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);
312326 }
313327 }
314328
@@ -543,13 +557,24 @@ pub fn predict_with_filters_kern(
543557
544558 // The vertical pass reads 3 rows above and 4 below the block, so the
545559 // horizontal pass must produce that many extra intermediate rows.
546- let rows = block_h + 7;
560+ // A whole-pel vertical position needs neither the 3 rows above nor the 4
561+ // below, and its vertical pass is the identity tap: `Round2(128 * a,
562+ // InterRound1) == Round2(a, InterRound1 - 7)`. Passing `y0 + 3` puts the
563+ // block's own first row where the pass would have put the 4th.
564+ let rows = if yfrac == 0 { block_h } else { block_h + 7 };
547565 let max = crate::decode::sample_max();
548566 with_scratch(rows, block_w, |intermediate, acc| {
549567 horizontal_pass_unscaled(
550- reference, stride, true_width, true_height, x0, y0, h_filter, block_w, rows,
568+ reference, stride, true_width, true_height, x0,
569+ if yfrac == 0 { y0 + 3 } else { y0 }, h_filter, block_w, rows,
551570 intermediate,
552571 );
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;
575+ }
576+ return;
577+ }
553578 for row in 0..block_h {
554579 vpass_row(intermediate, block_w, row, v_filter, acc);
555580 for (d, &a) in dst[row * block_w..(row + 1) * block_w].iter_mut().zip(acc.iter()) {
@@ -826,13 +851,20 @@ pub fn predict_scaled_kern(
826851 &v_wide[yfrac]
827852 };
828853
829- let rows = block_h + 7;
854+ let rows = if yfrac == 0 { block_h } else { block_h + 7 };
830855 let max = crate::decode::sample_max();
831856 with_scratch(rows, block_w, |intermediate, acc| {
832857 horizontal_pass(
833- reference, stride, true_width, true_height, x_q4, y0, x_scale_fp, block_w,
858+ reference, stride, true_width, true_height, x_q4,
859+ if yfrac == 0 { y0 + 3 } else { y0 }, x_scale_fp, block_w,
834860 kern_w, rows, h_kind, intermediate,
835861 );
862+ if yfrac == 0 {
863+ for (d, &a) in dst.iter_mut().zip(intermediate.iter()) {
864+ *d = round2(a, INTER_ROUND_1 - 7).clamp(0, max) as u16;
865+ }
866+ return;
867+ }
836868 for row in 0..block_h {
837869 vpass_row(intermediate, block_w, row, v_filter, acc);
838870 for (d, &a) in dst[row * block_w..(row + 1) * block_w].iter_mut().zip(acc.iter()) {
@@ -936,12 +968,18 @@ pub fn predict_compound_intermediate_kern(
936968 &v_wide[yfrac]
937969 };
938970
939- let rows = block_h + 7;
971+ let rows = if yfrac == 0 { block_h } else { block_h + 7 };
940972 with_scratch(rows, block_w, |intermediate, acc| {
941973 horizontal_pass(
942- reference, stride, true_width, true_height, x_q4, y0, x_scale_fp, block_w,
974+ reference, stride, true_width, true_height, x_q4,
975+ if yfrac == 0 { y0 + 3 } else { y0 }, x_scale_fp, block_w,
943976 kern_w, rows, h_kind, intermediate,
944977 );
978+ if yfrac == 0 {
979+ // `Round2(128 * a, INTER_ROUND_1_COMPOUND) == a`.
980+ dst.copy_from_slice(&intermediate[..dst.len()]);
981+ return;
982+ }
945983 for row in 0..block_h {
946984 vpass_row(intermediate, block_w, row, v_filter, acc);
947985 for (d, &a) in dst[row * block_w..(row + 1) * block_w].iter_mut().zip(acc.iter()) {