Merge branch 'lane-perf9' (45593d8b): film-grain chroma AVX2, sgrproj box sums sliding vertically, deblock edge_params hoists segmentation_enabled; byte-exact on four inputs, 4.6% fewer instructions at 4K
Browse files at this commit3 changed files +278 -49
- crates/ec-av1/src/decode.rs +20 -13
- crates/ec-av1/src/film_grain.rs +221 -21
- crates/ec-av1/src/restoration.rs +37 -15
crates/ec-av1/src/decode.rsmodified+20 -13
| @@ -16140,6 +16140,9 @@ fn lf_level( | |||
| 16140 | // Feature index: `SEG_LVL_ALT_LF_Y_V + dir` for luma, `_U`/`_V` (3/4) | 16140 | // Feature index: `SEG_LVL_ALT_LF_Y_V + dir` for luma, `_U`/`_V` (3/4) |
| 16141 | // for chroma. | 16141 | // for chroma. |
| 16142 | let feature = if plane_idx == 0 { SEG_LVL_ALT_LF_Y_V + dir } else { plane_idx + 2 }; | 16142 | let feature = if plane_idx == 0 { SEG_LVL_ALT_LF_Y_V + dir } else { plane_idx + 2 }; |
| 16143 | + // lane-perf9: `segment_id` is `0` and this lookup is skipped entirely | ||
| 16144 | + // when the frame codes no segmentation (`seg_lf_active`), which is what | ||
| 16145 | + // spares the deblock loop two thread-local reads per 4x4 edge. | ||
| 16143 | let base = match seg_feature(usize::from(segment_id), feature) { | 16146 | let base = match seg_feature(usize::from(segment_id), feature) { |
| 16144 | Some(data) => (base + data).clamp(0, 63), | 16147 | Some(data) => (base + data).clamp(0, 63), |
| 16145 | None => base, | 16148 | None => base, |
| @@ -16173,6 +16176,11 @@ fn edge_params( | |||
| 16173 | chroma: bool, | 16176 | chroma: bool, |
| 16174 | x0: usize, | 16177 | x0: usize, |
| 16175 | y0: usize, | 16178 | y0: usize, |
| 16179 | + // lane-perf9: `segmentation_enabled`, read once per plane pass instead of | ||
| 16180 | + // four thread-local reads (`SEG_MI_DIMS`, `SEG_IDS`, twice each) plus two | ||
| 16181 | + // `SEG` borrows per 4x4 edge. With it clear, every segment id is 0 and no | ||
| 16182 | + // `SEG_LVL_ALT_LF_*` feature can be active, so the levels are unchanged. | ||
| 16183 | + seg_lf_active: bool, | ||
| 16176 | ) -> Option<(u8, i32)> { | 16184 | ) -> Option<(u8, i32)> { |
| 16177 | let (mi_r, mi_c) = (plane_to_mi(chroma, y0), plane_to_mi(chroma, x0)); | 16185 | let (mi_r, mi_c) = (plane_to_mi(chroma, y0), plane_to_mi(chroma, x0)); |
| 16178 | let cur_tx = if dir == 0 { | 16186 | let cur_tx = if dir == 0 { |
| @@ -16220,15 +16228,13 @@ fn edge_params( | |||
| 16220 | { | 16228 | { |
| 16221 | return None; | 16229 | return None; |
| 16222 | } | 16230 | } |
| 16223 | - let cur_level = lf_level(lf, plane_idx, dir, cur_ref, cur_delta_lf, segment_id_at(mi_r, mi_c)); | 16231 | + let (cur_seg, pv_seg) = if seg_lf_active { |
| 16224 | - let pv_level = lf_level( | 16232 | + (segment_id_at(mi_r, mi_c), segment_id_at(pv_mi_r, pv_mi_c)) |
| 16225 | - lf, | 16233 | + } else { |
| 16226 | - plane_idx, | 16234 | + (0, 0) |
| 16227 | - dir, | 16235 | + }; |
| 16228 | - pv_ref, | 16236 | + let cur_level = lf_level(lf, plane_idx, dir, cur_ref, cur_delta_lf, cur_seg); |
| 16229 | - pv_delta_lf, | 16237 | + let pv_level = lf_level(lf, plane_idx, dir, pv_ref, pv_delta_lf, pv_seg); |
| 16230 | - segment_id_at(pv_mi_r, pv_mi_c), | ||
| 16231 | - ); | ||
| 16232 | if cur_level == 0 && pv_level == 0 { | 16238 | if cur_level == 0 && pv_level == 0 { |
| 16233 | return None; | 16239 | return None; |
| 16234 | } | 16240 | } |
| @@ -16976,13 +16982,14 @@ fn deblock_plane( | |||
| 16976 | // dependence is on ascending `x0` within a row, which stays the outer | 16982 | // dependence is on ascending `x0` within a row, which stays the outer |
| 16977 | // loop's order, and rows remain independent (horizontal pass mirrored). | 16983 | // loop's order, and rows remain independent (horizontal pass mirrored). |
| 16978 | let rows = plane.data.len() / stride.max(1); | 16984 | let rows = plane.data.len() / stride.max(1); |
| 16985 | + let seg_lf_active = SEG.with(|s| s.borrow().enabled); | ||
| 16979 | let mut x0 = 4usize; | 16986 | let mut x0 = 4usize; |
| 16980 | while x0 < tw { | 16987 | while x0 < tw { |
| 16981 | let mut y0 = 0usize; | 16988 | let mut y0 = 0usize; |
| 16982 | while y0 < th { | 16989 | while y0 < th { |
| 16983 | if y0 + 16 <= th && x0 >= 8 && x0 + 8 <= stride { | 16990 | if y0 + 16 <= th && x0 >= 8 && x0 + 8 <= stride { |
| 16984 | let ps: [Option<(u8, i32)>; 4] = | 16991 | let ps: [Option<(u8, i32)>; 4] = |
| 16985 | - std::array::from_fn(|k| edge_params(lf, n, plane_idx, 0, chroma, x0, y0 + 4 * k)); | 16992 | + std::array::from_fn(|k| edge_params(lf, n, plane_idx, 0, chroma, x0, y0 + 4 * k, seg_lf_active)); |
| 16986 | if let Some((len, level)) = ps[0] { | 16993 | if let Some((len, level)) = ps[0] { |
| 16987 | if ps.iter().all(|p| *p == ps[0]) | 16994 | if ps.iter().all(|p| *p == ps[0]) |
| 16988 | && filter_edge16( | 16995 | && filter_edge16( |
| @@ -17015,7 +17022,7 @@ fn deblock_plane( | |||
| 17015 | y0 += 16; | 17022 | y0 += 16; |
| 17016 | continue; | 17023 | continue; |
| 17017 | } | 17024 | } |
| 17018 | - if let Some((len, level)) = edge_params(lf, n, plane_idx, 0, chroma, x0, y0) { | 17025 | + if let Some((len, level)) = edge_params(lf, n, plane_idx, 0, chroma, x0, y0, seg_lf_active) { |
| 17019 | if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE_V") && plane_idx == 0 { | 17026 | if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE_V") && plane_idx == 0 { |
| 17020 | eprintln!("VEDGE x0={x0} y0={y0} len={len} level={level}"); | 17027 | eprintln!("VEDGE x0={x0} y0={y0} len={len} level={level}"); |
| 17021 | } | 17028 | } |
| @@ -17039,7 +17046,7 @@ fn deblock_plane( | |||
| 17039 | while x0 < tw { | 17046 | while x0 < tw { |
| 17040 | if x0 + 16 <= tw && y0 >= 8 && y0 + 7 <= rows { | 17047 | if x0 + 16 <= tw && y0 >= 8 && y0 + 7 <= rows { |
| 17041 | let ps: [Option<(u8, i32)>; 4] = | 17048 | let ps: [Option<(u8, i32)>; 4] = |
| 17042 | - std::array::from_fn(|k| edge_params(lf, n, plane_idx, 1, chroma, x0 + 4 * k, y0)); | 17049 | + std::array::from_fn(|k| edge_params(lf, n, plane_idx, 1, chroma, x0 + 4 * k, y0, seg_lf_active)); |
| 17043 | if let Some((len, level)) = ps[0] { | 17050 | if let Some((len, level)) = ps[0] { |
| 17044 | if ps.iter().all(|p| *p == ps[0]) | 17051 | if ps.iter().all(|p| *p == ps[0]) |
| 17045 | && filter_edge16( | 17052 | && filter_edge16( |
| @@ -17072,7 +17079,7 @@ fn deblock_plane( | |||
| 17072 | x0 += 16; | 17079 | x0 += 16; |
| 17073 | continue; | 17080 | continue; |
| 17074 | } | 17081 | } |
| 17075 | - if let Some((len, level)) = edge_params(lf, n, plane_idx, 1, chroma, x0, y0) { | 17082 | + if let Some((len, level)) = edge_params(lf, n, plane_idx, 1, chroma, x0, y0, seg_lf_active) { |
| 17076 | if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE") | 17083 | if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE") |
| 17077 | && plane_idx == 0 | 17084 | && plane_idx == 0 |
| 17078 | && x0 == 44 | 17085 | && x0 == 44 |
crates/ec-av1/src/film_grain.rsmodified+221 -21
| @@ -408,30 +408,25 @@ fn add_noise_to_block( | |||
| 408 | let gcr = &cr_grain[grow..grow + cw]; | 408 | let gcr = &cr_grain[grow..grow + cw]; |
| 409 | let urow = &mut u_plane[crow..crow + cw]; | 409 | let urow = &mut u_plane[crow..crow + cw]; |
| 410 | let vrow = &mut v_plane[crow..crow + cw]; | 410 | let vrow = &mut v_plane[crow..crow + cw]; |
| 411 | - for ((((lpair, u), v), &gb), &gr) in luma | 411 | + // lane-perf9: one plane at a time, so the row is a straight |
| 412 | - .chunks_exact(2) | 412 | + // vectorisable kernel (the per-sample `apply_cb`/`apply_cr` tests |
| 413 | - .zip(urow.iter_mut()) | 413 | + // were loop-invariant anyway). |
| 414 | - .zip(vrow.iter_mut()) | 414 | + let params = ChromaNoise { |
| 415 | - .zip(gcb.iter()) | 415 | + index_max, |
| 416 | - .zip(gcr.iter()) | 416 | + rounding_offset, |
| 417 | - { | 417 | + scaling_shift, |
| 418 | - let average_luma = (lpair[0] as i32 + lpair[1] as i32 + 1) >> 1; | 418 | + min_chroma, |
| 419 | + max_chroma, | ||
| 420 | + }; | ||
| 419 | if apply_cb { | 421 | if apply_cb { |
| 420 | - let scaled = (((average_luma * cb_luma_mult + cb_mult * *u as i32) >> 6) | 422 | + chroma_noise_row( |
| 421 | - + cb_offset) | 423 | + urow, luma, gcb, scaling_cb, cb_luma_mult, cb_mult, cb_offset, params, |
| 422 | - .clamp(0, index_max); | 424 | + ); |
| 423 | - let delta = | ||
| 424 | - (scaling_cb[scaled as usize] * gb + rounding_offset) >> scaling_shift; | ||
| 425 | - *u = ((*u as i32 + delta).clamp(min_chroma, max_chroma)) as u16; | ||
| 426 | } | 425 | } |
| 427 | if apply_cr { | 426 | if apply_cr { |
| 428 | - let scaled = (((average_luma * cr_luma_mult + cr_mult * *v as i32) >> 6) | 427 | + chroma_noise_row( |
| 429 | - + cr_offset) | 428 | + vrow, luma, gcr, scaling_cr, cr_luma_mult, cr_mult, cr_offset, params, |
| 430 | - .clamp(0, index_max); | 429 | + ); |
| 431 | - let delta = | ||
| 432 | - (scaling_cr[scaled as usize] * gr + rounding_offset) >> scaling_shift; | ||
| 433 | - *v = ((*v as i32 + delta).clamp(min_chroma, max_chroma)) as u16; | ||
| 434 | - } | ||
| 435 | } | 430 | } |
| 436 | } | 431 | } |
| 437 | } | 432 | } |
| @@ -482,6 +477,70 @@ fn luma_noise_row( | |||
| 482 | ); | 477 | ); |
| 483 | } | 478 | } |
| 484 | 479 | ||
| 480 | +/// The loop-invariant half of [`chroma_noise_row`]'s spec 7.18.3.5 arithmetic | ||
| 481 | +/// (the same values for both chroma planes and every row of a block). | ||
| 482 | +#[derive(Clone, Copy)] | ||
| 483 | +struct ChromaNoise { | ||
| 484 | + index_max: i32, | ||
| 485 | + rounding_offset: i32, | ||
| 486 | + scaling_shift: i32, | ||
| 487 | + min_chroma: i32, | ||
| 488 | + max_chroma: i32, | ||
| 489 | +} | ||
| 490 | + | ||
| 491 | +/// One chroma row of spec 7.18.3.5's `add_noise_to_block`: `luma` is the | ||
| 492 | +/// block's own luma row pair-averaged 4:2:0 (`luma.len() == 2 * | ||
| 493 | +/// chroma.len()`), `grain` and `scaling` are the plane's own. | ||
| 494 | +/// | ||
| 495 | +/// lane-perf9: AVX2 does 8 chroma samples at a time, the luma pair sum being | ||
| 496 | +/// one `vpmaddwd` against ones; every other CPU runs the scalar loop, which | ||
| 497 | +/// stays the reference (`simd_matches_scalar_chroma_noise_row` pins the pair). | ||
| 498 | +#[allow(unsafe_code)] | ||
| 499 | +#[allow(clippy::too_many_arguments)] | ||
| 500 | +fn chroma_noise_row( | ||
| 501 | + chroma: &mut [u16], | ||
| 502 | + luma: &[u16], | ||
| 503 | + grain: &[i32], | ||
| 504 | + scaling: &[i32], | ||
| 505 | + luma_mult: i32, | ||
| 506 | + mult: i32, | ||
| 507 | + offset: i32, | ||
| 508 | + p: ChromaNoise, | ||
| 509 | +) { | ||
| 510 | + #[cfg(target_arch = "x86_64")] | ||
| 511 | + if crate::mc::simd_level() == crate::mc::SimdLevel::Avx2 { | ||
| 512 | + // SAFETY: reached only when the CPU reports avx2; the kernel reads | ||
| 513 | + // `luma[..2 * n]`/`grain[..n]` for `n` samples it writes, and gathers | ||
| 514 | + // only in-range LUT entries. | ||
| 515 | + unsafe { | ||
| 516 | + simd::chroma_noise_row_avx2(chroma, luma, grain, scaling, luma_mult, mult, offset, p) | ||
| 517 | + }; | ||
| 518 | + return; | ||
| 519 | + } | ||
| 520 | + chroma_noise_row_scalar(chroma, luma, grain, scaling, luma_mult, mult, offset, p); | ||
| 521 | +} | ||
| 522 | + | ||
| 523 | +/// The scalar reference for [`chroma_noise_row`]. | ||
| 524 | +#[allow(clippy::too_many_arguments)] | ||
| 525 | +fn chroma_noise_row_scalar( | ||
| 526 | + chroma: &mut [u16], | ||
| 527 | + luma: &[u16], | ||
| 528 | + grain: &[i32], | ||
| 529 | + scaling: &[i32], | ||
| 530 | + luma_mult: i32, | ||
| 531 | + mult: i32, | ||
| 532 | + offset: i32, | ||
| 533 | + p: ChromaNoise, | ||
| 534 | +) { | ||
| 535 | + for ((c, lpair), &g) in chroma.iter_mut().zip(luma.chunks_exact(2)).zip(grain) { | ||
| 536 | + let average_luma = (lpair[0] as i32 + lpair[1] as i32 + 1) >> 1; | ||
| 537 | + let scaled = | ||
| 538 | + (((average_luma * luma_mult + mult * *c as i32) >> 6) + offset).clamp(0, p.index_max); | ||
| 539 | + let delta = (scaling[scaled as usize] * g + p.rounding_offset) >> p.scaling_shift; | ||
| 540 | + *c = ((*c as i32 + delta).clamp(p.min_chroma, p.max_chroma)) as u16; | ||
| 541 | + } | ||
| 542 | +} | ||
| 543 | + | ||
| 485 | /// The scalar reference for [`luma_noise_row`]. | 544 | /// The scalar reference for [`luma_noise_row`]. |
| 486 | fn luma_noise_row_scalar( | 545 | fn luma_noise_row_scalar( |
| 487 | pixels: &mut [u16], | 546 | pixels: &mut [u16], |
| @@ -559,6 +618,72 @@ mod simd { | |||
| 559 | max_luma, | 618 | max_luma, |
| 560 | ); | 619 | ); |
| 561 | } | 620 | } |
| 621 | + | ||
| 622 | + /// [`super::chroma_noise_row`]'s AVX2 kernel, 8 chroma samples per | ||
| 623 | + /// iteration. `vpmaddwd` against a vector of ones is the 4:2:0 luma pair | ||
| 624 | + /// sum (the 16 luma samples of the pairs are non-negative and below | ||
| 625 | + /// `1 << 15` at every supported bit depth, so the signed multiply is | ||
| 626 | + /// exact), and the LUT index is clamped exactly as the scalar form | ||
| 627 | + /// clamps it. | ||
| 628 | + /// | ||
| 629 | + /// # Safety | ||
| 630 | + /// Requires AVX2; `luma` must be at least `2 * chroma.len()` long, | ||
| 631 | + /// `grain` at least `chroma.len()`, and `scaling` non-empty. | ||
| 632 | + #[target_feature(enable = "avx2")] | ||
| 633 | + #[allow(clippy::too_many_arguments)] | ||
| 634 | + pub unsafe fn chroma_noise_row_avx2( | ||
| 635 | + chroma: &mut [u16], | ||
| 636 | + luma: &[u16], | ||
| 637 | + grain: &[i32], | ||
| 638 | + scaling: &[i32], | ||
| 639 | + luma_mult: i32, | ||
| 640 | + mult: i32, | ||
| 641 | + offset: i32, | ||
| 642 | + p: super::ChromaNoise, | ||
| 643 | + ) { | ||
| 644 | + let n = chroma.len().min(grain.len()).min(luma.len() / 2); | ||
| 645 | + let ones = _mm256_set1_epi16(1); | ||
| 646 | + let vluma_mult = _mm256_set1_epi32(luma_mult); | ||
| 647 | + let vmult = _mm256_set1_epi32(mult); | ||
| 648 | + let voffset = _mm256_set1_epi32(offset); | ||
| 649 | + let vindex_max = _mm256_set1_epi32(p.index_max); | ||
| 650 | + let last = _mm256_set1_epi32(scaling.len() as i32 - 1); | ||
| 651 | + let zero = _mm256_setzero_si256(); | ||
| 652 | + let rnd = _mm256_set1_epi32(p.rounding_offset); | ||
| 653 | + let sh = _mm_cvtsi32_si128(p.scaling_shift); | ||
| 654 | + let lo = _mm256_set1_epi32(p.min_chroma); | ||
| 655 | + let hi = _mm256_set1_epi32(p.max_chroma); | ||
| 656 | + let cp = chroma.as_mut_ptr(); | ||
| 657 | + let lp = luma.as_ptr(); | ||
| 658 | + let gp = grain.as_ptr(); | ||
| 659 | + let mut c = 0usize; | ||
| 660 | + while c + 8 <= n { | ||
| 661 | + let l = _mm256_loadu_si256(lp.add(c * 2).cast()); | ||
| 662 | + let pair_sum = _mm256_madd_epi16(l, ones); | ||
| 663 | + let avg = _mm256_srai_epi32(_mm256_add_epi32(pair_sum, _mm256_set1_epi32(1)), 1); | ||
| 664 | + let c16 = _mm_loadu_si128(cp.add(c).cast()); | ||
| 665 | + let c32 = _mm256_cvtepu16_epi32(c16); | ||
| 666 | + let mixed = _mm256_add_epi32( | ||
| 667 | + _mm256_mullo_epi32(avg, vluma_mult), | ||
| 668 | + _mm256_mullo_epi32(c32, vmult), | ||
| 669 | + ); | ||
| 670 | + let scaled = _mm256_add_epi32(_mm256_srai_epi32(mixed, 6), voffset); | ||
| 671 | + let clamped = _mm256_max_epi32(_mm256_min_epi32(scaled, vindex_max), zero); | ||
| 672 | + let idx = _mm256_min_epi32(clamped, last); | ||
| 673 | + let s = _mm256_i32gather_epi32(scaling.as_ptr(), idx, 4); | ||
| 674 | + let g = _mm256_loadu_si256(gp.add(c).cast()); | ||
| 675 | + let delta = _mm256_sra_epi32(_mm256_add_epi32(_mm256_mullo_epi32(s, g), rnd), sh); | ||
| 676 | + let v = _mm256_max_epi32(_mm256_min_epi32(_mm256_add_epi32(c32, delta), hi), lo); | ||
| 677 | + // Same packing argument as the luma kernel: every lane already | ||
| 678 | + // sits inside `[min_chroma, max_chroma]`. | ||
| 679 | + let packed = _mm256_permute4x64_epi64(_mm256_packus_epi32(v, v), 0b0000_1000); | ||
| 680 | + _mm_storeu_si128(cp.add(c).cast(), _mm256_castsi256_si128(packed)); | ||
| 681 | + c += 8; | ||
| 682 | + } | ||
| 683 | + super::chroma_noise_row_scalar( | ||
| 684 | + &mut chroma[c..], &luma[c * 2..], &grain[c..], scaling, luma_mult, mult, offset, p, | ||
| 685 | + ); | ||
| 686 | + } | ||
| 562 | } | 687 | } |
| 563 | 688 | ||
| 564 | /// `ver_boundary_overlap` (spec 7.18.3.5), in place on `dst` (== the C's | 689 | /// `ver_boundary_overlap` (spec 7.18.3.5), in place on `dst` (== the C's |
| @@ -1153,4 +1278,79 @@ mod perf5_tests { | |||
| 1153 | } | 1278 | } |
| 1154 | assert_eq!(checked, 3 * 4 * 2 * 10); | 1279 | assert_eq!(checked, 3 * 4 * 2 * 10); |
| 1155 | } | 1280 | } |
| 1281 | + | ||
| 1282 | + /// [`super::chroma_noise_row`]'s AVX2 kernel against its scalar | ||
| 1283 | + /// reference, over the parameter ranges a real stream reaches: every | ||
| 1284 | + /// grain bit depth, both clip ranges, the `chroma_scaling_from_luma` | ||
| 1285 | + /// multipliers (0/64/0) and coded ones, and lengths either side of the | ||
| 1286 | + /// 8-sample vector step. | ||
| 1287 | + #[test] | ||
| 1288 | + fn simd_matches_scalar_chroma_noise_row() { | ||
| 1289 | + let mut state = 0x0f1e_2d3c_4b5a_6978u64; | ||
| 1290 | + let mut rng = move || { | ||
| 1291 | + state ^= state << 13; | ||
| 1292 | + state ^= state >> 7; | ||
| 1293 | + state ^= state << 17; | ||
| 1294 | + state | ||
| 1295 | + }; | ||
| 1296 | + let mut checked = 0usize; | ||
| 1297 | + for bd_shift in 0..3u32 { | ||
| 1298 | + let lut_len = 256usize << bd_shift; | ||
| 1299 | + let sample_max = (lut_len - 1) as u16; | ||
| 1300 | + let index_max = (lut_len - 1) as i32; | ||
| 1301 | + let scaling: Vec<i32> = (0..lut_len).map(|_| (rng() % 256) as i32).collect(); | ||
| 1302 | + for scaling_shift in [8i32, 11] { | ||
| 1303 | + for &(luma_mult, mult, offset) in &[ | ||
| 1304 | + (64i32, 0i32, 0i32), | ||
| 1305 | + (-128, 127, -(1 << (8 + bd_shift))), | ||
| 1306 | + (17, -64, 42), | ||
| 1307 | + ] { | ||
| 1308 | + for &(lo, hi) in | ||
| 1309 | + &[(0i32, index_max), (16 << bd_shift, 240 << bd_shift)] | ||
| 1310 | + { | ||
| 1311 | + for len in [1usize, 5, 8, 9, 16, 17, 32] { | ||
| 1312 | + let p = super::ChromaNoise { | ||
| 1313 | + index_max, | ||
| 1314 | + rounding_offset: 1i32 << (scaling_shift - 1), | ||
| 1315 | + scaling_shift, | ||
| 1316 | + min_chroma: lo, | ||
| 1317 | + max_chroma: hi, | ||
| 1318 | + }; | ||
| 1319 | + let chroma: Vec<u16> = (0..len) | ||
| 1320 | + .map(|_| (rng() % (u64::from(sample_max) + 1)) as u16) | ||
| 1321 | + .collect(); | ||
| 1322 | + let luma: Vec<u16> = (0..len * 2) | ||
| 1323 | + .map(|_| (rng() % (u64::from(sample_max) + 1)) as u16) | ||
| 1324 | + .collect(); | ||
| 1325 | + let grain: Vec<i32> = | ||
| 1326 | + (0..len).map(|_| (rng() % 513) as i32 - 256).collect(); | ||
| 1327 | + let mut want = chroma.clone(); | ||
| 1328 | + super::chroma_noise_row_scalar( | ||
| 1329 | + &mut want, &luma, &grain, &scaling, luma_mult, mult, offset, p, | ||
| 1330 | + ); | ||
| 1331 | + let mut got = chroma.clone(); | ||
| 1332 | + super::chroma_noise_row( | ||
| 1333 | + &mut got, &luma, &grain, &scaling, luma_mult, mult, offset, p, | ||
| 1334 | + ); | ||
| 1335 | + assert_eq!(got, want, "dispatch len={len} mult={mult}"); | ||
| 1336 | + #[cfg(target_arch = "x86_64")] | ||
| 1337 | + if std::arch::is_x86_feature_detected!("avx2") { | ||
| 1338 | + let mut avx = chroma.clone(); | ||
| 1339 | + #[allow(unsafe_code)] | ||
| 1340 | + unsafe { | ||
| 1341 | + super::simd::chroma_noise_row_avx2( | ||
| 1342 | + &mut avx, &luma, &grain, &scaling, luma_mult, mult, | ||
| 1343 | + offset, p, | ||
| 1344 | + ) | ||
| 1345 | + }; | ||
| 1346 | + assert_eq!(avx, want, "avx2 len={len} mult={mult}"); | ||
| 1347 | + } | ||
| 1348 | + checked += 1; | ||
| 1349 | + } | ||
| 1350 | + } | ||
| 1351 | + } | ||
| 1352 | + } | ||
| 1353 | + } | ||
| 1354 | + assert_eq!(checked, 3 * 2 * 3 * 2 * 7); | ||
| 1355 | + } | ||
| 1156 | } | 1356 | } |
crates/ec-av1/src/restoration.rsmodified+37 -15
| @@ -751,30 +751,52 @@ fn compute_ab( | |||
| 751 | let mut col_b = vec![0i64; ncols]; | 751 | let mut col_b = vec![0i64; ncols]; |
| 752 | // Hoisted out of the per-point body: one thread-local read per call. | 752 | // Hoisted out of the per-point body: one thread-local read per call. |
| 753 | let bd_shift = u32::from(crate::decode::bit_depth()).saturating_sub(8); | 753 | let bd_shift = u32::from(crate::decode::bit_depth()).saturating_sub(8); |
| 754 | + let base_col = h_start as i64 - 1 - r as i64; | ||
| 755 | + // lane-perf9: the column sums slide down the grid too. Grid row `gi+1`'s | ||
| 756 | + // window is row `gi`'s minus its top source row plus one new bottom row, | ||
| 757 | + // so each source sample is read once per grid row instead of (2r+1) | ||
| 758 | + // times -- the same exactness argument as the separable split above | ||
| 759 | + // (i64 addition is associative, and subtracting a row that was added is | ||
| 760 | + // exact), and `lr_sample`'s stripe-boundary test, which is what makes a | ||
| 761 | + // sample read expensive, is a pure function of `(row, col)`. | ||
| 754 | for gi in 0..gh { | 762 | for gi in 0..gh { |
| 755 | let i = gi as i64 - 1; | 763 | let i = gi as i64 - 1; |
| 756 | - let base_col = h_start as i64 - 1 - r as i64; | 764 | + { |
| 765 | + let mut accumulate = |row: i64, sign: i64| { | ||
| 757 | for k in 0..ncols { | 766 | for k in 0..ncols { |
| 758 | - let col = base_col + k as i64; | ||
| 759 | - let (mut ca, mut cb) = (0i64, 0i64); | ||
| 760 | - for dr in -r..=r { | ||
| 761 | - let row = v_start as i64 + i + dr as i64; | ||
| 762 | let px = lr_sample( | 767 | let px = lr_sample( |
| 763 | - cdef, deblocked, stride, plane_w, plane_h, v_start, v_end, row, col, | 768 | + cdef, |
| 769 | + deblocked, | ||
| 770 | + stride, | ||
| 771 | + plane_w, | ||
| 772 | + plane_h, | ||
| 773 | + v_start, | ||
| 774 | + v_end, | ||
| 775 | + row, | ||
| 776 | + base_col + k as i64, | ||
| 764 | ) as i64; | 777 | ) as i64; |
| 765 | - ca += px * px; | 778 | + col_a[k] += sign * px * px; |
| 766 | - cb += px; | 779 | + col_b[k] += sign * px; |
| 780 | + } | ||
| 781 | + }; | ||
| 782 | + if gi == 0 { | ||
| 783 | + for dr in -r..=r { | ||
| 784 | + accumulate(v_start as i64 + i + dr as i64, 1); | ||
| 785 | + } | ||
| 786 | + } else { | ||
| 787 | + accumulate(v_start as i64 + i - 1 - r as i64, -1); | ||
| 788 | + accumulate(v_start as i64 + i + r as i64, 1); | ||
| 767 | } | 789 | } |
| 768 | - col_a[k] = ca; | ||
| 769 | - col_b[k] = cb; | ||
| 770 | } | 790 | } |
| 791 | + // The horizontal sum slides the same way (one add and one subtract | ||
| 792 | + // per grid point instead of 2r+1 adds). | ||
| 793 | + let mut a = col_a[..2 * ru + 1].iter().sum::<i64>(); | ||
| 794 | + let mut b = col_b[..2 * ru + 1].iter().sum::<i64>(); | ||
| 771 | for gj in 0..gw { | 795 | for gj in 0..gw { |
| 772 | let j = gj as i64 - 1; | 796 | let j = gj as i64 - 1; |
| 773 | - let mut a = 0i64; | 797 | + if gj > 0 { |
| 774 | - let mut b = 0i64; | 798 | + a += col_a[gj + 2 * ru] - col_a[gj - 1]; |
| 775 | - for k in gj..gj + 2 * ru + 1 { | 799 | + b += col_b[gj + 2 * ru] - col_b[gj - 1]; |
| 776 | - a += col_a[k]; | ||
| 777 | - b += col_b[k]; | ||
| 778 | } | 800 | } |
| 779 | // libaom `restoration.c:660` -- the box sums are brought back | 801 | // libaom `restoration.c:660` -- the box sums are brought back |
| 780 | // to the 8-bit scale before `p` is formed (`a` is a sum of | 802 | // to the 8-bit scale before `p` is formed (`a` is a sum of |