Wiener restoration runs on the motion-compensation kernels
Browse files at this commit91d30feee2c3a54bbd98ae0c796a33849e07b50cParent: 1332eb0- perf
- Wiener restoration runs on the motion-compensation kernels
2 changed files +83 -44
- crates/ec-av1/src/mc.rs +2 -2
- crates/ec-av1/src/restoration.rs +81 -42
crates/ec-av1/src/mc.rsmodified+2 -2
| @@ -281,7 +281,7 @@ pub(crate) fn simd_level() -> SimdLevel { | |||
| 281 | /// the same `Round2(sum, InterRound0)`. | 281 | /// the same `Round2(sum, InterRound0)`. |
| 282 | #[allow(unsafe_code)] | 282 | #[allow(unsafe_code)] |
| 283 | #[inline] | 283 | #[inline] |
| 284 | -fn hpass_contig(src: &[u16], t16: &[i16; 8], out: &mut [i32]) { | 284 | +pub(crate) fn hpass_contig(src: &[u16], t16: &[i16; 8], out: &mut [i32]) { |
| 285 | #[cfg(target_arch = "x86_64")] | 285 | #[cfg(target_arch = "x86_64")] |
| 286 | { | 286 | { |
| 287 | match simd_level() { | 287 | match simd_level() { |
| @@ -586,7 +586,7 @@ fn horizontal_pass_unscaled( | |||
| 586 | /// eight slots. `acc` is left holding the unrounded sums. | 586 | /// eight slots. `acc` is left holding the unrounded sums. |
| 587 | #[inline] | 587 | #[inline] |
| 588 | #[allow(unsafe_code)] | 588 | #[allow(unsafe_code)] |
| 589 | -fn vpass_row(intermediate: &[i32], block_w: usize, row: usize, taps: &[i32; 8], acc: &mut [i32]) { | 589 | +pub(crate) fn vpass_row(intermediate: &[i32], block_w: usize, row: usize, taps: &[i32; 8], acc: &mut [i32]) { |
| 590 | #[cfg(target_arch = "x86_64")] | 590 | #[cfg(target_arch = "x86_64")] |
| 591 | { | 591 | { |
| 592 | match simd_level() { | 592 | match simd_level() { |
crates/ec-av1/src/restoration.rsmodified+81 -42
| @@ -494,11 +494,33 @@ fn lr_sample( | |||
| 494 | col: i64, | 494 | col: i64, |
| 495 | ) -> i32 { | 495 | ) -> i32 { |
| 496 | let col_c = col.clamp(0, plane_w as i64 - 1) as usize; | 496 | let col_c = col.clamp(0, plane_w as i64 - 1) as usize; |
| 497 | - if row >= stripe_v_start as i64 && row < stripe_v_end as i64 { | 497 | + i32::from( |
| 498 | - i32::from(cdef[row as usize * stride + col_c]) | 498 | + lr_src_row(cdef, deblocked, stride, plane_w, plane_h, stripe_v_start, stripe_v_end, row) |
| 499 | + [col_c], | ||
| 500 | + ) | ||
| 501 | +} | ||
| 502 | + | ||
| 503 | +/// The plane row [`lr_sample`] reads, resolved once. Which buffer and which | ||
| 504 | +/// row a sample comes from depends only on `row` -- the column only ever | ||
| 505 | +/// clamps -- so a whole-row consumer (the Wiener horizontal pass) resolves | ||
| 506 | +/// the substitution once per row instead of once per tap. [`lr_sample`] is | ||
| 507 | +/// this function plus that clamp, so the rule has one statement. | ||
| 508 | +#[allow(clippy::too_many_arguments)] | ||
| 509 | +fn lr_src_row<'a>( | ||
| 510 | + cdef: &'a [u16], | ||
| 511 | + deblocked: &'a [u16], | ||
| 512 | + stride: usize, | ||
| 513 | + plane_w: usize, | ||
| 514 | + plane_h: usize, | ||
| 515 | + stripe_v_start: usize, | ||
| 516 | + stripe_v_end: usize, | ||
| 517 | + row: i64, | ||
| 518 | +) -> &'a [u16] { | ||
| 519 | + let (buf, src_row) = if row >= stripe_v_start as i64 && row < stripe_v_end as i64 { | ||
| 520 | + (cdef, row as usize) | ||
| 499 | } else if row < stripe_v_start as i64 { | 521 | } else if row < stripe_v_start as i64 { |
| 500 | if stripe_v_start == 0 { | 522 | if stripe_v_start == 0 { |
| 501 | - i32::from(cdef[col_c]) | 523 | + (cdef, 0) |
| 502 | } else { | 524 | } else { |
| 503 | // libaom saves 2 deblocked context lines and duplicates the | 525 | // libaom saves 2 deblocked context lines and duplicates the |
| 504 | // outer one to fill a 3-row border (`RESTORATION_BORDER`): | 526 | // outer one to fill a 3-row border (`RESTORATION_BORDER`): |
| @@ -506,23 +528,21 @@ fn lr_sample( | |||
| 506 | // rows `stripe_v_start-2`/`-3` both read deblocked row | 528 | // rows `stripe_v_start-2`/`-3` both read deblocked row |
| 507 | // `stripe_v_start-2`. | 529 | // `stripe_v_start-2`. |
| 508 | let dist = stripe_v_start as i64 - 1 - row; | 530 | let dist = stripe_v_start as i64 - 1 - row; |
| 509 | - let src_row = if dist == 0 { | 531 | + ( |
| 510 | - stripe_v_start - 1 | 532 | + deblocked, |
| 511 | - } else { | 533 | + if dist == 0 { stripe_v_start - 1 } else { stripe_v_start.saturating_sub(2) }, |
| 512 | - stripe_v_start.saturating_sub(2) | 534 | + ) |
| 513 | - }; | ||
| 514 | - i32::from(deblocked[src_row * stride + col_c]) | ||
| 515 | } | 535 | } |
| 516 | } else if stripe_v_end == plane_h { | 536 | } else if stripe_v_end == plane_h { |
| 517 | - i32::from(cdef[(plane_h - 1) * stride + col_c]) | 537 | + (cdef, plane_h - 1) |
| 518 | } else { | 538 | } else { |
| 519 | // Mirror image of the above: rows `stripe_v_end`/`stripe_v_end+1` | 539 | // Mirror image of the above: rows `stripe_v_end`/`stripe_v_end+1` |
| 520 | // read their own deblocked row, row `stripe_v_end+2` duplicates | 540 | // read their own deblocked row, row `stripe_v_end+2` duplicates |
| 521 | // `stripe_v_end+1`. | 541 | // `stripe_v_end+1`. |
| 522 | let dist = (row - stripe_v_end as i64).min(1) as usize; | 542 | let dist = (row - stripe_v_end as i64).min(1) as usize; |
| 523 | - let src_row = (stripe_v_end + dist).min(plane_h - 1); | 543 | + (deblocked, (stripe_v_end + dist).min(plane_h - 1)) |
| 524 | - i32::from(deblocked[src_row * stride + col_c]) | 544 | + }; |
| 525 | - } | 545 | + &buf[src_row * stride..src_row * stride + plane_w] |
| 526 | } | 546 | } |
| 527 | 547 | ||
| 528 | /// Wiener pixel filter (spec 7.17.4, libaom `wiener_filter_stripe` -> | 548 | /// Wiener pixel filter (spec 7.17.4, libaom `wiener_filter_stripe` -> |
| @@ -566,45 +586,58 @@ fn apply_wiener_stripe( | |||
| 566 | // this was a fresh allocation per stripe. Every entry is written below. | 586 | // this was a fresh allocation per stripe. Every entry is written below. |
| 567 | let mut inter = WIENER_INTER_SCRATCH.with(|c| std::mem::take(&mut *c.borrow_mut())); | 587 | let mut inter = WIENER_INTER_SCRATCH.with(|c| std::mem::take(&mut *c.borrow_mut())); |
| 568 | inter.clear(); | 588 | inter.clear(); |
| 569 | - inter.resize(rows * w, 0i32); | 589 | + // lane-perf8: `mc::vpass_row` reads a fixed 8 taps from `row`, so the |
| 590 | + // last vertical output needs two rows of padding past the `h + 6` the | ||
| 591 | + // 7-tap kernel actually uses. Tap 7 is zero, so they never contribute. | ||
| 592 | + inter.resize((h + 8) * w, 0i32); | ||
| 593 | + // lane-perf8: this pass IS `mc.rs`'s horizontal one -- a 7-tap `i16` | ||
| 594 | + // convolution over contiguous samples rounded by `Round2(_, 3)`, which | ||
| 595 | + // is `INTER_ROUND_0` -- so it runs on that module's kernels with the | ||
| 596 | + // eighth tap zeroed, instead of a scalar `lr_sample` call per tap. The | ||
| 597 | + // stripe substitution is resolved once per row by `lr_src_row`, and only | ||
| 598 | + // a unit touching the plane's left or right edge needs the clamped copy. | ||
| 599 | + let hf = &info.hfilter; | ||
| 600 | + let t16: [i16; 8] = | ||
| 601 | + [hf[0] as i16, hf[1] as i16, hf[2] as i16, hf[3] as i16, hf[4] as i16, hf[5] as i16, hf[6] as i16, 0]; | ||
| 602 | + let (lo_bound, hi_bound) = (-wiener_bias as i32, (wiener_limit - 1 - wiener_bias) as i32); | ||
| 603 | + let mut hbuf = WIENER_ROW_SCRATCH.with(|c| std::mem::take(&mut *c.borrow_mut())); | ||
| 570 | for r in 0..rows { | 604 | for r in 0..rows { |
| 571 | let row = v_start as i64 - 3 + r as i64; | 605 | let row = v_start as i64 - 3 + r as i64; |
| 572 | - for c in 0..w { | 606 | + let src = lr_src_row(cdef, deblocked, stride, plane_w, plane_h, v_start, v_end, row); |
| 573 | - let col = h_start as i64 + c as i64; | 607 | + let lo = h_start as i64 - 3; |
| 574 | - let mut sum = 0i64; | 608 | + let out = &mut inter[r * w..r * w + w]; |
| 575 | - for (t, &tap) in info.hfilter.iter().enumerate() { | 609 | + if lo >= 0 && lo as usize + w + 7 <= plane_w { |
| 576 | - let x = col - 3 + t as i64; | 610 | + crate::mc::hpass_contig(&src[lo as usize..], &t16, out); |
| 577 | - sum += tap as i64 | 611 | + } else { |
| 578 | - * lr_sample(cdef, deblocked, stride, plane_w, plane_h, v_start, v_end, row, x) as i64; | 612 | + hbuf.clear(); |
| 579 | - } | 613 | + hbuf.extend( |
| 580 | - // libaom's `highbd_convolve_add_src_horiz_hip` clamps its | 614 | + (0..w + 7).map(|k| src[(lo + k as i64).clamp(0, plane_w as i64 - 1) as usize]), |
| 581 | - // *biased* intermediate to `[0, WIENER_CLAMP_LIMIT(3, bd) - 1]` | 615 | + ); |
| 582 | - // (`convolve.h:43`, `(1 << (bd + 1 + FILTER_BITS - round0))`). | 616 | + crate::mc::hpass_contig(&hbuf, &t16, out); |
| 583 | - // Its bias over this crate's unbiased sum is exactly | 617 | + } |
| 584 | - // `(1 << (bd + FILTER_BITS - 1)) >> round0 == 1 << (bd + 3)` | 618 | + for v in out.iter_mut() { |
| 585 | - // (the `rounding` term of that function, the rest cancelling | 619 | + *v = (*v).clamp(lo_bound, hi_bound); |
| 586 | - // against `WienerInfo`'s derived centre tap as the doc comment | ||
| 587 | - // above derives), so the same clamp in *this* domain is the | ||
| 588 | - // bound shifted down by that bias. At 8-bit neither end is ever | ||
| 589 | - // reached by real content, which is why the old fixed `[0, 8191]` | ||
| 590 | - // survived every 8-bit gate; at 10-bit the upper end is | ||
| 591 | - // `128 * 1023 >> 3 == 16368`, well past `8191`, so a fixed | ||
| 592 | - // 8-bit limit silently truncated most 10-bit Wiener pixels. | ||
| 593 | - inter[r * w + c] = round2(sum, 3).clamp(-wiener_bias, wiener_limit - 1 - wiener_bias) as i32; | ||
| 594 | } | 620 | } |
| 595 | } | 621 | } |
| 622 | + WIENER_ROW_SCRATCH.with(|c| *c.borrow_mut() = hbuf); | ||
| 596 | // lane-perf7: the clamp bound is the frame's, so it is read once here | 623 | // lane-perf7: the clamp bound is the frame's, so it is read once here |
| 597 | // instead of taking a thread-local read per output sample. | 624 | // instead of taking a thread-local read per output sample. |
| 598 | let smax = i64::from(crate::decode::sample_max()); | 625 | let smax = i64::from(crate::decode::sample_max()); |
| 626 | + // The vertical pass is `mc.rs`'s too, same 8th-tap-zero trick: it leaves | ||
| 627 | + // the accumulator unrounded, and `Round2(_, 11)` is `INTER_ROUND_1`. | ||
| 628 | + let vf = &info.vfilter; | ||
| 629 | + let taps: [i32; 8] = [vf[0], vf[1], vf[2], vf[3], vf[4], vf[5], vf[6], 0]; | ||
| 630 | + let mut acc = WIENER_ACC_SCRATCH.with(|c| std::mem::take(&mut *c.borrow_mut())); | ||
| 631 | + acc.clear(); | ||
| 632 | + acc.resize(w, 0i32); | ||
| 599 | for r in 0..h { | 633 | for r in 0..h { |
| 600 | - for c in 0..w { | 634 | + crate::mc::vpass_row(&inter, w, r, &taps, &mut acc); |
| 601 | - let mut sum = 0i64; | 635 | + let dst = &mut out[(v_start + r) * stride + h_start..][..w]; |
| 602 | - for (t, &tap) in info.vfilter.iter().enumerate() { | 636 | + for (o, &a) in dst.iter_mut().zip(acc.iter()) { |
| 603 | - sum += tap as i64 * inter[(r + t) * w + c] as i64; | 637 | + *o = round2(i64::from(a), 11).clamp(0, smax) as u16; |
| 604 | - } | ||
| 605 | - out[(v_start + r) * stride + h_start + c] = round2(sum, 11).clamp(0, smax) as u16; | ||
| 606 | } | 638 | } |
| 607 | } | 639 | } |
| 640 | + WIENER_ACC_SCRATCH.with(|c| *c.borrow_mut() = acc); | ||
| 608 | WIENER_INTER_SCRATCH.with(|c| *c.borrow_mut() = inter); | 641 | WIENER_INTER_SCRATCH.with(|c| *c.borrow_mut() = inter); |
| 609 | } | 642 | } |
| 610 | 643 | ||
| @@ -613,6 +646,12 @@ thread_local! { | |||
| 613 | /// calls so a stripe does not allocate (lane-perf7). | 646 | /// calls so a stripe does not allocate (lane-perf7). |
| 614 | static WIENER_INTER_SCRATCH: std::cell::RefCell<Vec<i32>> = | 647 | static WIENER_INTER_SCRATCH: std::cell::RefCell<Vec<i32>> = |
| 615 | const { std::cell::RefCell::new(Vec::new()) }; | 648 | const { std::cell::RefCell::new(Vec::new()) }; |
| 649 | + /// The clamped-column copy the horizontal pass needs at a plane edge. | ||
| 650 | + static WIENER_ROW_SCRATCH: std::cell::RefCell<Vec<u16>> = | ||
| 651 | + const { std::cell::RefCell::new(Vec::new()) }; | ||
| 652 | + /// The vertical pass's unrounded accumulator row. | ||
| 653 | + static WIENER_ACC_SCRATCH: std::cell::RefCell<Vec<i32>> = | ||
| 654 | + const { std::cell::RefCell::new(Vec::new()) }; | ||
| 616 | /// [`compute_ab`]'s two `(A, B)` grids, likewise (lane-perf7). | 655 | /// [`compute_ab`]'s two `(A, B)` grids, likewise (lane-perf7). |
| 617 | #[allow(clippy::type_complexity)] | 656 | #[allow(clippy::type_complexity)] |
| 618 | static SGR_AB_SCRATCH: std::cell::RefCell<(Vec<(i32, i64)>, Vec<(i32, i64)>)> = | 657 | static SGR_AB_SCRATCH: std::cell::RefCell<(Vec<(i32, i64)>, Vec<(i32, i64)>)> = |