forge
Explore
Log in

Don't you dare go Hollow

Directional intra hoists the zone, the derivative and the blend out of the pixel loop

Browse files at this commit

e7ac08e9648f5494a1b2a3945a5e75e6b9d345eeAhmet Kaan GümüşParent: 29faade
  • perf
    • directional intra hoists the zone, the derivative and the blend out of the pixel loop
Show full message
perf(av1): directional intra hoists the zone, the derivative and the blend out of the pixel loop

The z1/z2/z3 walk decided its zone, looked the derivative up in a
27-arm match and called the edge blend through a `&dyn Fn` once per
sample. Three loops, one per zone, with the derivative and the base
constants per block and z1's shift per row; the blend indexes the edge
slice directly.

Byte-exact on all four gate streams. -0.25% instructions on the 4K
segment, -0.47% on the 1080p one (cumulative -2.84% / -1.97%).
crates/ec-av1/src/intra.rsmodified+58 -43
@@ -591,57 +591,72 @@ fn directional(
591 let left_at = |p: i32| left_buf[(p + left_off) as usize];591 let left_at = |p: i32| left_buf[(p + left_off) as usize];
592 let up_a = i32::from(upsample_above);592 let up_a = i32::from(upsample_above);
593 let up_l = i32::from(upsample_left);593 let up_l = i32::from(upsample_left);
594- let blend = |edge: &dyn Fn(i32) -> i32, base: i32, shift: i32| {594+ let max = crate::decode::sample_max(fctx);
595- round2(edge(base) * (32 - shift) + edge(base + 1) * shift, 5)595+ // lane-intra: the zone test, the derivative lookup (a 27-arm match) and
596+ // the blend's `&dyn Fn` indirection all used to sit inside the pixel
597+ // loop; they are per-block (per-row for the shift) constants.
598+ let blend = |edge: &[i32], off: i32, base: i32, shift: i32| {
599+ round2(edge[(base + off) as usize] * (32 - shift) + edge[(base + off + 1) as usize] * shift, 5)
596 };600 };
597-601+ if angle < 90 {
598- for row in 0..h {602+ let dx = dr_intra_derivative(angle);
599- for col in 0..w {603+ let max_base = (reach - 1) << up_a;
600- let value = if angle < 90 {604+ let frac_bits = 6 - up_a;
601- let dx = dr_intra_derivative(angle);605+ for (row, dstrow) in dst.chunks_exact_mut(bw).enumerate() {
602- let max_base = (reach - 1) << up_a;606+ let x = dx * (row as i32 + 1);
603- let frac_bits = 6 - up_a;607+ let base0 = x >> frac_bits;
604- let x = dx * (row + 1);608+ let shift = ((x << up_a) & 0x3F) >> 1;
605- let base = (x >> frac_bits) + (col << up_a);609+ for (col, d) in dstrow.iter_mut().enumerate() {
606- let shift = ((x << up_a) & 0x3F) >> 1;610+ let base = base0 + ((col as i32) << up_a);
607- if base < max_base {611+ let value = if base < max_base {
608- blend(&above_at, base, shift)612+ blend(above_buf, above_off, base, shift)
609 } else {613 } else {
610 above_at(max_base)614 above_at(max_base)
611- }615+ };
612- } else if angle > 180 {616+ *d = value.clamp(0, max) as u16;
613- let dy = dr_intra_derivative(270 - angle);617+ }
614- let max_base = (reach - 1) << up_l;618+ }
615- let frac_bits = 6 - up_l;619+ } else if angle > 180 {
616- let y = dy * (col + 1);620+ let dy = dr_intra_derivative(270 - angle);
617- let base = (y >> frac_bits) + (row << up_l);621+ let max_base = (reach - 1) << up_l;
622+ let frac_bits = 6 - up_l;
623+ for (row, dstrow) in dst.chunks_exact_mut(bw).enumerate() {
624+ let r = (row as i32) << up_l;
625+ for (col, d) in dstrow.iter_mut().enumerate() {
626+ let y = dy * (col as i32 + 1);
627+ let base = (y >> frac_bits) + r;
618 let shift = ((y << up_l) & 0x3F) >> 1;628 let shift = ((y << up_l) & 0x3F) >> 1;
619- if base < max_base {629+ let value = if base < max_base {
620- blend(&left_at, base, shift)630+ blend(left_buf, left_off, base, shift)
621 } else {631 } else {
622 left_at(max_base)632 left_at(max_base)
623- }633+ };
624- } else {634+ *d = value.clamp(0, max) as u16;
625- // The two zones meet here: a ray that leaves through the row635+ }
626- // above is read there, and one that leaves through the column636+ }
627- // to the left is read there instead.637+ } else {
628- let dx = dr_intra_derivative(180 - angle);638+ // The two zones meet here: a ray that leaves through the row above is
629- let min_base = -(1 << up_a);639+ // read there, and one that leaves through the column to the left is
630- let frac_bits_x = 6 - up_a;640+ // read there instead.
631- let y = row + 1;641+ let dx = dr_intra_derivative(180 - angle);
632- let x = (col << 6) - y * dx;642+ let dy = dr_intra_derivative(angle - 90);
643+ let min_base = -(1 << up_a);
644+ let frac_bits_x = 6 - up_a;
645+ let frac_bits_y = 6 - up_l;
646+ for (row, dstrow) in dst.chunks_exact_mut(bw).enumerate() {
647+ let ydx = (row as i32 + 1) * dx;
648+ let yrow = (row as i32) << 6;
649+ for (col, d) in dstrow.iter_mut().enumerate() {
650+ let x = ((col as i32) << 6) - ydx;
633 let base = x >> frac_bits_x;651 let base = x >> frac_bits_x;
634- if base >= min_base {652+ let value = if base >= min_base {
635- blend(&above_at, base, ((x << up_a) & 0x3F) >> 1)653+ blend(above_buf, above_off, base, ((x << up_a) & 0x3F) >> 1)
636 } else {654 } else {
637- let dy = dr_intra_derivative(angle - 90);655+ let y2 = yrow - (col as i32 + 1) * dy;
638- let frac_bits_y = 6 - up_l;656+ blend(left_buf, left_off, y2 >> frac_bits_y, ((y2 << up_l) & 0x3F) >> 1)
639- let x2 = col + 1;657+ };
640- let y2 = (row << 6) - x2 * dy;658+ *d = value.clamp(0, max) as u16;
641- blend(&left_at, y2 >> frac_bits_y, ((y2 << up_l) & 0x3F) >> 1)659+ }
642- }
643- };
644- dst[(row * w + col) as usize] = value.clamp(0, crate::decode::sample_max(fctx)) as u16;
645 }660 }
646 }661 }
647 }662 }