forge
Explore
Log in

Don't you dare go Hollow

Intra non-directional prediction loops once per mode, not per pixel

Browse files at this commit

29faade491c27651dfccc400e32718aa2176c28fAhmet Kaan GümüşParent: 6d124e3
  • perf
    • intra non-directional prediction loops once per mode, not per pixel
Show full message
perf(av1): intra non-directional prediction loops once per mode, not per pixel

The seven non-directional modes shared one pixel loop with a 13-arm
`match mode` inside it, so every sample re-ran the dispatch and DC_PRED
re-averaged both edges per sample. One loop per mode instead, with the
per-row terms (smooth weights, the bottom-left/top-right anchors, the
left column sample) hoisted out of the column loop.

Byte-exact: all four gate streams hash to their references at 1 thread.
-2.59% instructions on the 4K segment, -1.51% on the 1080p one.
crates/ec-av1/src/intra.rsmodified+63 -30
@@ -316,37 +316,70 @@ pub(crate) fn predict(
316316 // to one `weights` slice before.
317317 let weights_w = &SM_WEIGHTS[bw..bw * 2];
318318 let weights_h = &SM_WEIGHTS[bh..bh * 2];
319- for row in 0..bh {
320- for col in 0..bw {
321- let (r, c) = (row as i32, col as i32);
322- let last_w = bw as i32 - 1;
323- let last_h = bh as i32 - 1;
324- let value = match mode {
325- DC_PRED => dc(above, left, bw, bh, fctx),
326- V_PRED => edges.above(c),
327- H_PRED => edges.left(r),
328- SMOOTH_PRED => round2(
329- i32::from(weights_h[row]) * edges.above(c)
330- + (256 - i32::from(weights_h[row])) * edges.left(last_h)
331- + i32::from(weights_w[col]) * edges.left(r)
332- + (256 - i32::from(weights_w[col])) * edges.above(last_w),
333- 9,
334- ),
335- SMOOTH_V_PRED => round2(
336- i32::from(weights_h[row]) * edges.above(c)
337- + (256 - i32::from(weights_h[row])) * edges.left(last_h),
338- 8,
339- ),
340- SMOOTH_H_PRED => round2(
341- i32::from(weights_w[col]) * edges.left(r)
342- + (256 - i32::from(weights_w[col])) * edges.above(last_w),
343- 8,
344- ),
345- PAETH_PRED => paeth(edges.above(c), edges.left(r), edges.above(-1)),
346- other => panic!("intra mode {other} is not one this module predicts"),
347- };
348- dst[row * bw + col] = value.clamp(0, crate::decode::sample_max(fctx)) as u16;
319+ // lane-intra: one loop per mode rather than a `match mode` per pixel --
320+ // the old shape re-ran the whole dispatch (and, for `DC_PRED`, the edge
321+ // average itself) for every sample of every block.
322+ let max = crate::decode::sample_max(fctx);
323+ let above_row = &edges.above[1..];
324+ let left_col = &edges.left[1..];
325+ let corner = edges.above[0];
326+ match mode {
327+ DC_PRED => dst.fill(dc(above, left, bw, bh, fctx).clamp(0, max) as u16),
328+ V_PRED => {
329+ for row in dst.chunks_exact_mut(bw) {
330+ for (d, &a) in row.iter_mut().zip(&above_row[..bw]) {
331+ *d = a.clamp(0, max) as u16;
332+ }
333+ }
334+ }
335+ H_PRED => {
336+ for (row, &l) in dst.chunks_exact_mut(bw).zip(&left_col[..bh]) {
337+ row.fill(l.clamp(0, max) as u16);
338+ }
339+ }
340+ SMOOTH_PRED => {
341+ let below = left_col[bh - 1];
342+ let right = above_row[bw - 1];
343+ for (row, dstrow) in dst.chunks_exact_mut(bw).enumerate() {
344+ let wh = i32::from(weights_h[row]);
345+ let acc = (256 - wh) * below;
346+ let lr = left_col[row];
347+ for (col, d) in dstrow.iter_mut().enumerate() {
348+ let ww = i32::from(weights_w[col]);
349+ let v = round2(acc + wh * above_row[col] + ww * lr + (256 - ww) * right, 9);
350+ *d = v.clamp(0, max) as u16;
351+ }
352+ }
353+ }
354+ SMOOTH_V_PRED => {
355+ let below = left_col[bh - 1];
356+ for (row, dstrow) in dst.chunks_exact_mut(bw).enumerate() {
357+ let wh = i32::from(weights_h[row]);
358+ let acc = (256 - wh) * below;
359+ for (d, &a) in dstrow.iter_mut().zip(&above_row[..bw]) {
360+ *d = round2(acc + wh * a, 8).clamp(0, max) as u16;
361+ }
362+ }
363+ }
364+ SMOOTH_H_PRED => {
365+ let right = above_row[bw - 1];
366+ for (row, dstrow) in dst.chunks_exact_mut(bw).enumerate() {
367+ let lr = left_col[row];
368+ for (col, d) in dstrow.iter_mut().enumerate() {
369+ let ww = i32::from(weights_w[col]);
370+ *d = round2(ww * lr + (256 - ww) * right, 8).clamp(0, max) as u16;
371+ }
372+ }
373+ }
374+ PAETH_PRED => {
375+ for (row, dstrow) in dst.chunks_exact_mut(bw).enumerate() {
376+ let lr = left_col[row];
377+ for (d, &a) in dstrow.iter_mut().zip(&above_row[..bw]) {
378+ *d = paeth(a, lr, corner).clamp(0, max) as u16;
379+ }
380+ }
349381 }
382+ other => panic!("intra mode {other} is not one this module predicts"),
350383 }
351384 }
352385