forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-intra' (9611873e): intra prediction one loop per mode instead of a 13-arm match per sample, directional walk hoisted per zone, edge buffers on the stack; byte-exact, -2.9% instructions on the 4K segment, -2.1% on 1080p

Browse files at this commit

84ddd14411395665cdd16f1b3fc0dc396b858268Ahmet Kaan GümüşParent: dc919eaee5b8d8
crates/ec-av1/src/intra.rsmodified+140 -83
@@ -136,8 +136,11 @@ const ANGLE_STEP: i32 = 3;
136 /// Both arrays hold the corner first, so the spec's index `-1` is index 0 here136 /// Both arrays hold the corner first, so the spec's index `-1` is index 0 here
137 /// and they run out to the spec's `w + h - 1`.137 /// and they run out to the spec's `w + h - 1`.
138 struct Edges {138 struct Edges {
139- above: Vec<i32>,139+ // lane-intra: `bw + bh + 1` is at most 129 samples, so both edges live on
140- left: Vec<i32>,140+ // the stack -- two heap allocations per predicted block otherwise, and
141+ // most blocks a stream predicts are 4x4 to 16x16.
142+ above: [i32; 129],
143+ left: [i32; 129],
141 }144 }
142 145
143 impl Edges {146 impl Edges {
@@ -172,16 +175,21 @@ impl Edges {
172 // first and the samples are extended/truncated to `want` in place, so175 // first and the samples are extended/truncated to `want` in place, so
173 // the contents are what `extend` + `with_corner` produced before.176 // the contents are what `extend` + `with_corner` produced before.
174 let build_edge = |samples: Option<&[u16]>, other: Option<&[u16]>, none_fill: i32| {177 let build_edge = |samples: Option<&[u16]>, other: Option<&[u16]>, none_fill: i32| {
175- let mut v: Vec<i32> = Vec::with_capacity(want + 1);178+ let mut v = [0i32; 129];
176- v.push(corner);179+ v[0] = corner;
177 match (samples, other) {180 match (samples, other) {
178 (Some(s), _) => {181 (Some(s), _) => {
179- v.extend(s.iter().map(|&x| i32::from(x)));182+ let n = s.len().min(want);
180- let last = *v.last().expect("an edge that exists has samples");183+ for (d, &x) in v[1..=n].iter_mut().zip(s.iter()) {
181- v.resize(want + 1, last);184+ *d = i32::from(x);
185+ }
186+ if n < want {
187+ let last = i32::from(*s.last().expect("an edge that exists has samples"));
188+ v[n + 1..=want].fill(last);
189+ }
182 }190 }
183- (None, Some(o)) => v.resize(want + 1, i32::from(o[0])),191+ (None, Some(o)) => v[1..=want].fill(i32::from(o[0])),
184- (None, None) => v.resize(want + 1, none_fill),192+ (None, None) => v[1..=want].fill(none_fill),
185 }193 }
186 v194 v
187 };195 };
@@ -316,37 +324,70 @@ pub(crate) fn predict(
316 // to one `weights` slice before.324 // to one `weights` slice before.
317 let weights_w = &SM_WEIGHTS[bw..bw * 2];325 let weights_w = &SM_WEIGHTS[bw..bw * 2];
318 let weights_h = &SM_WEIGHTS[bh..bh * 2];326 let weights_h = &SM_WEIGHTS[bh..bh * 2];
319- for row in 0..bh {327+ // lane-intra: one loop per mode rather than a `match mode` per pixel --
320- for col in 0..bw {328+ // the old shape re-ran the whole dispatch (and, for `DC_PRED`, the edge
321- let (r, c) = (row as i32, col as i32);329+ // average itself) for every sample of every block.
322- let last_w = bw as i32 - 1;330+ let max = crate::decode::sample_max(fctx);
323- let last_h = bh as i32 - 1;331+ let above_row = &edges.above[1..];
324- let value = match mode {332+ let left_col = &edges.left[1..];
325- DC_PRED => dc(above, left, bw, bh, fctx),333+ let corner = edges.above[0];
326- V_PRED => edges.above(c),334+ match mode {
327- H_PRED => edges.left(r),335+ DC_PRED => dst.fill(dc(above, left, bw, bh, fctx).clamp(0, max) as u16),
328- SMOOTH_PRED => round2(336+ V_PRED => {
329- i32::from(weights_h[row]) * edges.above(c)337+ for row in dst.chunks_exact_mut(bw) {
330- + (256 - i32::from(weights_h[row])) * edges.left(last_h)338+ for (d, &a) in row.iter_mut().zip(&above_row[..bw]) {
331- + i32::from(weights_w[col]) * edges.left(r)339+ *d = a.clamp(0, max) as u16;
332- + (256 - i32::from(weights_w[col])) * edges.above(last_w),340+ }
333- 9,341+ }
334- ),342+ }
335- SMOOTH_V_PRED => round2(343+ H_PRED => {
336- i32::from(weights_h[row]) * edges.above(c)344+ for (row, &l) in dst.chunks_exact_mut(bw).zip(&left_col[..bh]) {
337- + (256 - i32::from(weights_h[row])) * edges.left(last_h),345+ row.fill(l.clamp(0, max) as u16);
338- 8,346+ }
339- ),347+ }
340- SMOOTH_H_PRED => round2(348+ SMOOTH_PRED => {
341- i32::from(weights_w[col]) * edges.left(r)349+ let below = left_col[bh - 1];
342- + (256 - i32::from(weights_w[col])) * edges.above(last_w),350+ let right = above_row[bw - 1];
343- 8,351+ for (row, dstrow) in dst.chunks_exact_mut(bw).enumerate() {
344- ),352+ let wh = i32::from(weights_h[row]);
345- PAETH_PRED => paeth(edges.above(c), edges.left(r), edges.above(-1)),353+ let acc = (256 - wh) * below;
346- other => panic!("intra mode {other} is not one this module predicts"),354+ let lr = left_col[row];
347- };355+ for (col, d) in dstrow.iter_mut().enumerate() {
348- dst[row * bw + col] = value.clamp(0, crate::decode::sample_max(fctx)) as u16;356+ let ww = i32::from(weights_w[col]);
357+ let v = round2(acc + wh * above_row[col] + ww * lr + (256 - ww) * right, 9);
358+ *d = v.clamp(0, max) as u16;
359+ }
360+ }
361+ }
362+ SMOOTH_V_PRED => {
363+ let below = left_col[bh - 1];
364+ for (row, dstrow) in dst.chunks_exact_mut(bw).enumerate() {
365+ let wh = i32::from(weights_h[row]);
366+ let acc = (256 - wh) * below;
367+ for (d, &a) in dstrow.iter_mut().zip(&above_row[..bw]) {
368+ *d = round2(acc + wh * a, 8).clamp(0, max) as u16;
369+ }
370+ }
371+ }
372+ SMOOTH_H_PRED => {
373+ let right = above_row[bw - 1];
374+ for (row, dstrow) in dst.chunks_exact_mut(bw).enumerate() {
375+ let lr = left_col[row];
376+ for (col, d) in dstrow.iter_mut().enumerate() {
377+ let ww = i32::from(weights_w[col]);
378+ *d = round2(ww * lr + (256 - ww) * right, 8).clamp(0, max) as u16;
379+ }
380+ }
349 }381 }
382+ PAETH_PRED => {
383+ for (row, dstrow) in dst.chunks_exact_mut(bw).enumerate() {
384+ let lr = left_col[row];
385+ for (d, &a) in dstrow.iter_mut().zip(&above_row[..bw]) {
386+ *d = paeth(a, lr, corner).clamp(0, max) as u16;
387+ }
388+ }
389+ }
390+ other => panic!("intra mode {other} is not one this module predicts"),
350 }391 }
351 }392 }
352 393
@@ -446,7 +487,8 @@ fn filter_intra_edge(buf: &mut [i32], strength: i32) {
446 const KERNEL: [[i32; 5]; 3] = [[0, 4, 8, 4, 0], [0, 5, 6, 5, 0], [2, 4, 4, 4, 2]];487 const KERNEL: [[i32; 5]; 3] = [[0, 4, 8, 4, 0], [0, 5, 6, 5, 0], [2, 4, 4, 4, 2]];
447 let filt = usize::try_from(strength - 1).expect("a filter strength is never negative");488 let filt = usize::try_from(strength - 1).expect("a filter strength is never negative");
448 let sz = buf.len() as i32;489 let sz = buf.len() as i32;
449- let edge = buf.to_vec();490+ let mut edge = [0i32; 129];
491+ edge[..buf.len()].copy_from_slice(buf);
450 for i in 1..buf.len() {492 for i in 1..buf.len() {
451 let mut s = 0;493 let mut s = 0;
452 for (j, &tap) in KERNEL[filt].iter().enumerate() {494 for (j, &tap) in KERNEL[filt].iter().enumerate() {
@@ -558,57 +600,72 @@ fn directional(
558 let left_at = |p: i32| left_buf[(p + left_off) as usize];600 let left_at = |p: i32| left_buf[(p + left_off) as usize];
559 let up_a = i32::from(upsample_above);601 let up_a = i32::from(upsample_above);
560 let up_l = i32::from(upsample_left);602 let up_l = i32::from(upsample_left);
561- let blend = |edge: &dyn Fn(i32) -> i32, base: i32, shift: i32| {603+ let max = crate::decode::sample_max(fctx);
562- round2(edge(base) * (32 - shift) + edge(base + 1) * shift, 5)604+ // lane-intra: the zone test, the derivative lookup (a 27-arm match) and
605+ // the blend's `&dyn Fn` indirection all used to sit inside the pixel
606+ // loop; they are per-block (per-row for the shift) constants.
607+ let blend = |edge: &[i32], off: i32, base: i32, shift: i32| {
608+ round2(edge[(base + off) as usize] * (32 - shift) + edge[(base + off + 1) as usize] * shift, 5)
563 };609 };
564-610+ if angle < 90 {
565- for row in 0..h {611+ let dx = dr_intra_derivative(angle);
566- for col in 0..w {612+ let max_base = (reach - 1) << up_a;
567- let value = if angle < 90 {613+ let frac_bits = 6 - up_a;
568- let dx = dr_intra_derivative(angle);614+ for (row, dstrow) in dst.chunks_exact_mut(bw).enumerate() {
569- let max_base = (reach - 1) << up_a;615+ let x = dx * (row as i32 + 1);
570- let frac_bits = 6 - up_a;616+ let base0 = x >> frac_bits;
571- let x = dx * (row + 1);617+ let shift = ((x << up_a) & 0x3F) >> 1;
572- let base = (x >> frac_bits) + (col << up_a);618+ for (col, d) in dstrow.iter_mut().enumerate() {
573- let shift = ((x << up_a) & 0x3F) >> 1;619+ let base = base0 + ((col as i32) << up_a);
574- if base < max_base {620+ let value = if base < max_base {
575- blend(&above_at, base, shift)621+ blend(above_buf, above_off, base, shift)
576 } else {622 } else {
577 above_at(max_base)623 above_at(max_base)
578- }624+ };
579- } else if angle > 180 {625+ *d = value.clamp(0, max) as u16;
580- let dy = dr_intra_derivative(270 - angle);626+ }
581- let max_base = (reach - 1) << up_l;627+ }
582- let frac_bits = 6 - up_l;628+ } else if angle > 180 {
583- let y = dy * (col + 1);629+ let dy = dr_intra_derivative(270 - angle);
584- let base = (y >> frac_bits) + (row << up_l);630+ let max_base = (reach - 1) << up_l;
631+ let frac_bits = 6 - up_l;
632+ for (row, dstrow) in dst.chunks_exact_mut(bw).enumerate() {
633+ let r = (row as i32) << up_l;
634+ for (col, d) in dstrow.iter_mut().enumerate() {
635+ let y = dy * (col as i32 + 1);
636+ let base = (y >> frac_bits) + r;
585 let shift = ((y << up_l) & 0x3F) >> 1;637 let shift = ((y << up_l) & 0x3F) >> 1;
586- if base < max_base {638+ let value = if base < max_base {
587- blend(&left_at, base, shift)639+ blend(left_buf, left_off, base, shift)
588 } else {640 } else {
589 left_at(max_base)641 left_at(max_base)
590- }642+ };
591- } else {643+ *d = value.clamp(0, max) as u16;
592- // The two zones meet here: a ray that leaves through the row644+ }
593- // above is read there, and one that leaves through the column645+ }
594- // to the left is read there instead.646+ } else {
595- let dx = dr_intra_derivative(180 - angle);647+ // The two zones meet here: a ray that leaves through the row above is
596- let min_base = -(1 << up_a);648+ // read there, and one that leaves through the column to the left is
597- let frac_bits_x = 6 - up_a;649+ // read there instead.
598- let y = row + 1;650+ let dx = dr_intra_derivative(180 - angle);
599- let x = (col << 6) - y * dx;651+ let dy = dr_intra_derivative(angle - 90);
652+ let min_base = -(1 << up_a);
653+ let frac_bits_x = 6 - up_a;
654+ let frac_bits_y = 6 - up_l;
655+ for (row, dstrow) in dst.chunks_exact_mut(bw).enumerate() {
656+ let ydx = (row as i32 + 1) * dx;
657+ let yrow = (row as i32) << 6;
658+ for (col, d) in dstrow.iter_mut().enumerate() {
659+ let x = ((col as i32) << 6) - ydx;
600 let base = x >> frac_bits_x;660 let base = x >> frac_bits_x;
601- if base >= min_base {661+ let value = if base >= min_base {
602- blend(&above_at, base, ((x << up_a) & 0x3F) >> 1)662+ blend(above_buf, above_off, base, ((x << up_a) & 0x3F) >> 1)
603 } else {663 } else {
604- let dy = dr_intra_derivative(angle - 90);664+ let y2 = yrow - (col as i32 + 1) * dy;
605- let frac_bits_y = 6 - up_l;665+ blend(left_buf, left_off, y2 >> frac_bits_y, ((y2 << up_l) & 0x3F) >> 1)
606- let x2 = col + 1;666+ };
607- let y2 = (row << 6) - x2 * dy;667+ *d = value.clamp(0, max) as u16;
608- blend(&left_at, y2 >> frac_bits_y, ((y2 << up_l) & 0x3F) >> 1)668+ }
609- }
610- };
611- dst[(row * w + col) as usize] = value.clamp(0, crate::decode::sample_max(fctx)) as u16;
612 }669 }
613 }670 }
614 }671 }