AVX2 film-grain chroma rows
Browse files at this commit8d96c7fbf0e893b50d41940ffd8ab9e18896ba02Parent: 68c67bf- perf
- AVX2 film-grain chroma rows
1 changed file +224 -24
- crates/ec-av1/src/film_grain.rs +224 -24
crates/ec-av1/src/film_grain.rsmodified+224 -24
| @@ -408,30 +408,25 @@ fn add_noise_to_block( | ||
| 408 | 408 | let gcr = &cr_grain[grow..grow + cw]; |
| 409 | 409 | let urow = &mut u_plane[crow..crow + cw]; |
| 410 | 410 | let vrow = &mut v_plane[crow..crow + cw]; |
| 411 | - for ((((lpair, u), v), &gb), &gr) in luma | |
| 412 | - .chunks_exact(2) | |
| 413 | - .zip(urow.iter_mut()) | |
| 414 | - .zip(vrow.iter_mut()) | |
| 415 | - .zip(gcb.iter()) | |
| 416 | - .zip(gcr.iter()) | |
| 417 | - { | |
| 418 | - let average_luma = (lpair[0] as i32 + lpair[1] as i32 + 1) >> 1; | |
| 419 | - if apply_cb { | |
| 420 | - let scaled = (((average_luma * cb_luma_mult + cb_mult * *u as i32) >> 6) | |
| 421 | - + cb_offset) | |
| 422 | - .clamp(0, index_max); | |
| 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 | - } | |
| 427 | - if apply_cr { | |
| 428 | - let scaled = (((average_luma * cr_luma_mult + cr_mult * *v as i32) >> 6) | |
| 429 | - + cr_offset) | |
| 430 | - .clamp(0, index_max); | |
| 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 | - } | |
| 411 | + // lane-perf9: one plane at a time, so the row is a straight | |
| 412 | + // vectorisable kernel (the per-sample `apply_cb`/`apply_cr` tests | |
| 413 | + // were loop-invariant anyway). | |
| 414 | + let params = ChromaNoise { | |
| 415 | + index_max, | |
| 416 | + rounding_offset, | |
| 417 | + scaling_shift, | |
| 418 | + min_chroma, | |
| 419 | + max_chroma, | |
| 420 | + }; | |
| 421 | + if apply_cb { | |
| 422 | + chroma_noise_row( | |
| 423 | + urow, luma, gcb, scaling_cb, cb_luma_mult, cb_mult, cb_offset, params, | |
| 424 | + ); | |
| 425 | + } | |
| 426 | + if apply_cr { | |
| 427 | + chroma_noise_row( | |
| 428 | + vrow, luma, gcr, scaling_cr, cr_luma_mult, cr_mult, cr_offset, params, | |
| 429 | + ); | |
| 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 | 544 | /// The scalar reference for [`luma_noise_row`]. |
| 486 | 545 | fn luma_noise_row_scalar( |
| 487 | 546 | pixels: &mut [u16], |
| @@ -559,6 +618,72 @@ mod simd { | ||
| 559 | 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 | 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 | 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 | } |