forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-grain' (643c1001): film grain noise kernels take the whole block (row loop inside, 4-wide tail step) -- per-row AVX2 on 15-wide chroma blocks paid the prologue per row and sent half the samples to the scalar tail; byte-exact, -2.7% instructions on the 4K segment

Browse files at this commit

9346288376c4b9affcfdece94090bb82a0fc75a3Ahmet Kaan GümüşParent: 184abb34786458
crates/ec-av1/src/film_grain.rsmodified+319 -103
@@ -395,52 +395,68 @@ fn add_noise_to_block(
395 let cw = half_w as usize;395 let cw = half_w as usize;
396 // U and V are distinct fields, so a disjoint borrow of each is396 // U and V are distinct fields, so a disjoint borrow of each is
397 // needed alongside the immutable read of Y.397 // needed alongside the immutable read of Y.
398- let Picture { y: ref luma_plane, u: ref mut u_plane, v: ref mut v_plane, .. } =398+ let Picture { y: ref luma_plane, u: ref mut u_plane, v: ref mut v_plane, .. } = *picture;
399- *picture;399+ let lrow0 = (py0 * y_stride + px0) as usize;
400- for i in 0..half_h {400+ let crow0 = (cy0 * c_stride + cx0) as usize;
401- let lrow = ((py0 + i * 2) * y_stride + px0) as usize;401+ let grow0 = (cg_row0 * cg_stride + cg_col0) as usize;
402- let crow = ((cy0 + i) * c_stride + cx0) as usize;402+ let luma = &luma_plane[lrow0..];
403- let grow = ((cg_row0 + i) * cg_stride + cg_col0) as usize;403+ let params = ChromaNoise { index_max, rounding_offset, scaling_shift, min_chroma, max_chroma };
404- let luma = &luma_plane[lrow..lrow + cw * 2];404+ // lane-grain: the whole block in one call, so the kernel's AVX2
405- let gcb = &cb_grain[grow..grow + cw];405+ // prologue is paid once per block instead of once per 15-sample row.
406- let gcr = &cr_grain[grow..grow + cw];406+ if apply_cb {
407- let urow = &mut u_plane[crow..crow + cw];407+ chroma_noise_rows(
408- let vrow = &mut v_plane[crow..crow + cw];408+ &mut u_plane[crow0..],
409- // lane-perf9: one plane at a time, so the row is a straight409+ c_stride as usize,
410- // vectorisable kernel (the per-sample `apply_cb`/`apply_cr` tests410+ luma,
411- // were loop-invariant anyway).411+ 2 * y_stride as usize,
412- let params = ChromaNoise {412+ &cb_grain[grow0..],
413- index_max,413+ cg_stride as usize,
414- rounding_offset,414+ half_h as usize,
415- scaling_shift,415+ cw,
416- min_chroma,416+ scaling_cb,
417- max_chroma,417+ cb_luma_mult,
418- };418+ cb_mult,
419- if apply_cb {419+ cb_offset,
420- chroma_noise_row(420+ params,
421- urow, luma, gcb, scaling_cb, cb_luma_mult, cb_mult, cb_offset, params,421+ );
422- );422+ }
423- }423+ if apply_cr {
424- if apply_cr {424+ chroma_noise_rows(
425- chroma_noise_row(425+ &mut v_plane[crow0..],
426- vrow, luma, gcr, scaling_cr, cr_luma_mult, cr_mult, cr_offset, params,426+ c_stride as usize,
427- );427+ luma,
428- }428+ 2 * y_stride as usize,
429+ &cr_grain[grow0..],
430+ cg_stride as usize,
431+ half_h as usize,
432+ cw,
433+ scaling_cr,
434+ cr_luma_mult,
435+ cr_mult,
436+ cr_offset,
437+ params,
438+ );
429 }439 }
430 }440 }
431 if apply_y {441 if apply_y {
432 // Both operands are contiguous along a row, so the row bases are442 // Both operands are contiguous along a row, so the row bases are
433 // computed once instead of a multiply-add per sample.443 // computed once instead of a multiply-add per sample.
434 let width = (half_w * 2) as usize;444 let width = (half_w * 2) as usize;
435- for i in 0..half_h * 2 {445+ let prow0 = (py0 * y_stride + px0) as usize;
436- let prow = ((py0 + i) * y_stride + px0) as usize;446+ let grow0 = (lg_row0 * lg_stride + lg_col0) as usize;
437- let grow = ((lg_row0 + i) * lg_stride + lg_col0) as usize;447+ luma_noise_rows(
438- let pixels = &mut picture.y[prow..prow + width];448+ &mut picture.y[prow0..],
439- let grain = &luma_grain[grow..grow + width];449+ y_stride as usize,
440- luma_noise_row(450+ &luma_grain[grow0..],
441- pixels, grain, scaling_y, rounding_offset, scaling_shift, min_luma, max_luma,451+ lg_stride as usize,
442- );452+ (half_h * 2) as usize,
443- }453+ width,
454+ scaling_y,
455+ rounding_offset,
456+ scaling_shift,
457+ min_luma,
458+ max_luma,
459+ );
444 }460 }
445 }461 }
446 462
@@ -458,21 +474,61 @@ fn luma_noise_row(
458 scaling_shift: i32,474 scaling_shift: i32,
459 min_luma: i32,475 min_luma: i32,
460 max_luma: i32,476 max_luma: i32,
477+) {
478+ let n = pixels.len().min(grain.len());
479+ luma_noise_rows(
480+ pixels, n, grain, n, 1, n, scaling, rounding_offset, scaling_shift, min_luma, max_luma,
481+ );
482+}
483+
484+/// [`luma_noise_row`] over `rows` consecutive rows of a block: `pixels` and
485+/// `grain` start at the block origin and step by their own strides.
486+///
487+/// lane-grain: the AVX2 prologue (10 broadcasts) used to be paid once per
488+/// 30-sample row, and the `30 % 8 == 6` / `15 % 8 == 7` tails handed a fifth
489+/// of the luma and half of the chroma samples back to the scalar loop -- the
490+/// 32x32 grain block is 30 wide (32 minus the 2-column overlap), never a
491+/// multiple of 8. Hoisting the row loop into the kernel pays the prologue
492+/// once per block and lets the tail take a 4-wide SSE step.
493+#[allow(unsafe_code)]
494+#[allow(clippy::too_many_arguments)]
495+fn luma_noise_rows(
496+ pixels: &mut [u16],
497+ p_stride: usize,
498+ grain: &[i32],
499+ g_stride: usize,
500+ rows: usize,
501+ n: usize,
502+ scaling: &[i32],
503+ rounding_offset: i32,
504+ scaling_shift: i32,
505+ min_luma: i32,
506+ max_luma: i32,
461 ) {507 ) {
462 #[cfg(target_arch = "x86_64")]508 #[cfg(target_arch = "x86_64")]
463- if crate::mc::simd_level() == crate::mc::SimdLevel::Avx2 {509+ if n >= 4 && crate::mc::simd_level() == crate::mc::SimdLevel::Avx2 {
464 // SAFETY: reached only when the CPU reports avx2; the kernel reads510 // SAFETY: reached only when the CPU reports avx2; the kernel reads
465- // `grain[..pixels.len()]` and gathers only in-range LUT entries.511+ // `grain`/`pixels` only inside the `rows x n` window the caller
512+ // sliced, and gathers only in-range LUT entries.
466 unsafe {513 unsafe {
467- simd::luma_noise_row_avx2(514+ simd::luma_noise_rows_avx2(
468- pixels, grain, scaling, rounding_offset, scaling_shift, min_luma, max_luma,515+ pixels, p_stride, grain, g_stride, rows, n, scaling, rounding_offset,
516+ scaling_shift, min_luma, max_luma,
469 )517 )
470 };518 };
471 return;519 return;
472 }520 }
473- luma_noise_row_scalar(521+ for r in 0..rows {
474- pixels, grain, scaling, rounding_offset, scaling_shift, min_luma, max_luma,522+ luma_noise_row_scalar(
475- );523+ &mut pixels[r * p_stride..r * p_stride + n],
524+ &grain[r * g_stride..r * g_stride + n],
525+ scaling,
526+ rounding_offset,
527+ scaling_shift,
528+ min_luma,
529+ max_luma,
530+ );
531+ }
476 }532 }
477 533
478 /// The loop-invariant half of [`chroma_noise_row`]'s spec 7.18.3.5 arithmetic534 /// The loop-invariant half of [`chroma_noise_row`]'s spec 7.18.3.5 arithmetic
@@ -504,18 +560,57 @@ fn chroma_noise_row(
504 mult: i32,560 mult: i32,
505 offset: i32,561 offset: i32,
506 p: ChromaNoise,562 p: ChromaNoise,
563+) {
564+ let n = chroma.len().min(grain.len()).min(luma.len() / 2);
565+ chroma_noise_rows(
566+ chroma, n, luma, 2 * n, grain, n, 1, n, scaling, luma_mult, mult, offset, p,
567+ );
568+}
569+
570+/// [`chroma_noise_row`] over `rows` consecutive chroma rows of a block; see
571+/// [`luma_noise_rows`] for why the row loop lives inside the kernel.
572+#[allow(unsafe_code)]
573+#[allow(clippy::too_many_arguments)]
574+fn chroma_noise_rows(
575+ chroma: &mut [u16],
576+ c_stride: usize,
577+ luma: &[u16],
578+ l_stride: usize,
579+ grain: &[i32],
580+ g_stride: usize,
581+ rows: usize,
582+ n: usize,
583+ scaling: &[i32],
584+ luma_mult: i32,
585+ mult: i32,
586+ offset: i32,
587+ p: ChromaNoise,
507 ) {588 ) {
508 #[cfg(target_arch = "x86_64")]589 #[cfg(target_arch = "x86_64")]
509- if crate::mc::simd_level() == crate::mc::SimdLevel::Avx2 {590+ if n >= 4 && crate::mc::simd_level() == crate::mc::SimdLevel::Avx2 {
510 // SAFETY: reached only when the CPU reports avx2; the kernel reads591 // SAFETY: reached only when the CPU reports avx2; the kernel reads
511- // `luma[..2 * n]`/`grain[..n]` for `n` samples it writes, and gathers592+ // `luma`/`grain`/`chroma` only inside the window the caller sliced,
512- // only in-range LUT entries.593+ // and gathers only in-range LUT entries.
513 unsafe {594 unsafe {
514- simd::chroma_noise_row_avx2(chroma, luma, grain, scaling, luma_mult, mult, offset, p)595+ simd::chroma_noise_rows_avx2(
596+ chroma, c_stride, luma, l_stride, grain, g_stride, rows, n, scaling, luma_mult,
597+ mult, offset, p,
598+ )
515 };599 };
516 return;600 return;
517 }601 }
518- chroma_noise_row_scalar(chroma, luma, grain, scaling, luma_mult, mult, offset, p);602+ for r in 0..rows {
603+ chroma_noise_row_scalar(
604+ &mut chroma[r * c_stride..r * c_stride + n],
605+ &luma[r * l_stride..r * l_stride + 2 * n],
606+ &grain[r * g_stride..r * g_stride + n],
607+ scaling,
608+ luma_mult,
609+ mult,
610+ offset,
611+ p,
612+ );
613+ }
519 }614 }
520 615
521 /// The scalar reference for [`chroma_noise_row`].616 /// The scalar reference for [`chroma_noise_row`].
@@ -585,36 +680,85 @@ mod simd {
585 max_luma: i32,680 max_luma: i32,
586 ) {681 ) {
587 let n = pixels.len().min(grain.len());682 let n = pixels.len().min(grain.len());
683+ luma_noise_rows_avx2(
684+ pixels, n, grain, n, 1, n, scaling, rounding_offset, scaling_shift, min_luma, max_luma,
685+ );
686+ }
687+
688+ /// [`super::luma_noise_rows`]'s AVX2 kernel: `rows` rows of `n` samples,
689+ /// 8 per iteration, then one 4-wide SSE step, then at most 3 scalar
690+ /// samples. The prologue is paid once for the whole block.
691+ ///
692+ /// # Safety
693+ /// Requires AVX2; `pixels`/`grain` must cover the `rows x n` window at
694+ /// their strides and `scaling` must be non-empty.
695+ #[target_feature(enable = "avx2")]
696+ #[allow(clippy::too_many_arguments)]
697+ pub unsafe fn luma_noise_rows_avx2(
698+ pixels: &mut [u16],
699+ p_stride: usize,
700+ grain: &[i32],
701+ g_stride: usize,
702+ rows: usize,
703+ n: usize,
704+ scaling: &[i32],
705+ rounding_offset: i32,
706+ scaling_shift: i32,
707+ min_luma: i32,
708+ max_luma: i32,
709+ ) {
588 let last = _mm256_set1_epi32(scaling.len() as i32 - 1);710 let last = _mm256_set1_epi32(scaling.len() as i32 - 1);
589 let rnd = _mm256_set1_epi32(rounding_offset);711 let rnd = _mm256_set1_epi32(rounding_offset);
590 let sh = _mm_cvtsi32_si128(scaling_shift);712 let sh = _mm_cvtsi32_si128(scaling_shift);
591 let lo = _mm256_set1_epi32(min_luma);713 let lo = _mm256_set1_epi32(min_luma);
592 let hi = _mm256_set1_epi32(max_luma);714 let hi = _mm256_set1_epi32(max_luma);
593- let pp = pixels.as_mut_ptr();715+ let last4 = _mm256_castsi256_si128(last);
594- let gp = grain.as_ptr();716+ let rnd4 = _mm256_castsi256_si128(rnd);
595- let mut c = 0usize;717+ let lo4 = _mm256_castsi256_si128(lo);
596- while c + 8 <= n {718+ let hi4 = _mm256_castsi256_si128(hi);
597- let p16 = _mm_loadu_si128(pp.add(c).cast());719+ let sp = scaling.as_ptr();
598- let p32 = _mm256_cvtepu16_epi32(p16);720+ for r in 0..rows {
599- let idx = _mm256_min_epi32(p32, last);721+ let pp = pixels.as_mut_ptr().add(r * p_stride);
600- let s = _mm256_i32gather_epi32(scaling.as_ptr(), idx, 4);722+ let gp = grain.as_ptr().add(r * g_stride);
601- let g = _mm256_loadu_si256(gp.add(c).cast());723+ let mut c = 0usize;
602- let delta = _mm256_sra_epi32(_mm256_add_epi32(_mm256_mullo_epi32(s, g), rnd), sh);724+ while c + 8 <= n {
603- let v = _mm256_max_epi32(725+ let p16 = _mm_loadu_si128(pp.add(c).cast());
604- _mm256_min_epi32(_mm256_add_epi32(p32, delta), hi),726+ let p32 = _mm256_cvtepu16_epi32(p16);
605- lo,727+ let idx = _mm256_min_epi32(p32, last);
728+ let s = _mm256_i32gather_epi32(sp, idx, 4);
729+ let g = _mm256_loadu_si256(gp.add(c).cast());
730+ let delta = _mm256_sra_epi32(_mm256_add_epi32(_mm256_mullo_epi32(s, g), rnd), sh);
731+ let v = _mm256_max_epi32(_mm256_min_epi32(_mm256_add_epi32(p32, delta), hi), lo);
732+ // Every lane is already inside `[min_luma, max_luma]`, so the
733+ // unsigned saturation of `packus` is exact; `permute4x64`
734+ // undoes the pack's per-128-bit-lane interleave.
735+ let packed = _mm256_permute4x64_epi64(_mm256_packus_epi32(v, v), 0b0000_1000);
736+ _mm_storeu_si128(pp.add(c).cast(), _mm256_castsi256_si128(packed));
737+ c += 8;
738+ }
739+ if c + 4 <= n {
740+ let p16 = _mm_loadl_epi64(pp.add(c).cast());
741+ let p32 = _mm_cvtepu16_epi32(p16);
742+ let idx = _mm_min_epi32(p32, last4);
743+ let s = _mm_i32gather_epi32(sp, idx, 4);
744+ let g = _mm_loadu_si128(gp.add(c).cast());
745+ let delta = _mm_sra_epi32(_mm_add_epi32(_mm_mullo_epi32(s, g), rnd4), sh);
746+ let v = _mm_max_epi32(_mm_min_epi32(_mm_add_epi32(p32, delta), hi4), lo4);
747+ _mm_storel_epi64(pp.add(c).cast(), _mm_packus_epi32(v, v));
748+ c += 4;
749+ }
750+ let base = r * p_stride;
751+ let gbase = r * g_stride;
752+ super::luma_noise_row_scalar(
753+ &mut pixels[base + c..base + n],
754+ &grain[gbase + c..gbase + n],
755+ scaling,
756+ rounding_offset,
757+ scaling_shift,
758+ min_luma,
759+ max_luma,
606 );760 );
607- // Every lane is already inside `[min_luma, max_luma]`, so the
608- // unsigned saturation of `packus` is exact; `permute4x64` undoes
609- // the pack's per-128-bit-lane interleave.
610- let packed = _mm256_permute4x64_epi64(_mm256_packus_epi32(v, v), 0b0000_1000);
611- _mm_storeu_si128(pp.add(c).cast(), _mm256_castsi256_si128(packed));
612- c += 8;
613 }761 }
614- super::luma_noise_row_scalar(
615- &mut pixels[c..], &grain[c..], scaling, rounding_offset, scaling_shift, min_luma,
616- max_luma,
617- );
618 }762 }
619 763
620 /// [`super::chroma_noise_row`]'s AVX2 kernel, 8 chroma samples per764 /// [`super::chroma_noise_row`]'s AVX2 kernel, 8 chroma samples per
@@ -640,7 +784,37 @@ mod simd {
640 p: super::ChromaNoise,784 p: super::ChromaNoise,
641 ) {785 ) {
642 let n = chroma.len().min(grain.len()).min(luma.len() / 2);786 let n = chroma.len().min(grain.len()).min(luma.len() / 2);
787+ chroma_noise_rows_avx2(
788+ chroma, n, luma, 2 * n, grain, n, 1, n, scaling, luma_mult, mult, offset, p,
789+ );
790+ }
791+
792+ /// [`super::chroma_noise_rows`]'s AVX2 kernel: `rows` chroma rows of `n`
793+ /// samples, 8 per iteration, then one 4-wide SSE step, then at most 3
794+ /// scalar samples.
795+ ///
796+ /// # Safety
797+ /// Requires AVX2; the three slices must cover the `rows x n` window
798+ /// (`2 * n` for luma) at their strides and `scaling` must be non-empty.
799+ #[target_feature(enable = "avx2")]
800+ #[allow(clippy::too_many_arguments)]
801+ pub unsafe fn chroma_noise_rows_avx2(
802+ chroma: &mut [u16],
803+ c_stride: usize,
804+ luma: &[u16],
805+ l_stride: usize,
806+ grain: &[i32],
807+ g_stride: usize,
808+ rows: usize,
809+ n: usize,
810+ scaling: &[i32],
811+ luma_mult: i32,
812+ mult: i32,
813+ offset: i32,
814+ p: super::ChromaNoise,
815+ ) {
643 let ones = _mm256_set1_epi16(1);816 let ones = _mm256_set1_epi16(1);
817+ let one = _mm256_set1_epi32(1);
644 let vluma_mult = _mm256_set1_epi32(luma_mult);818 let vluma_mult = _mm256_set1_epi32(luma_mult);
645 let vmult = _mm256_set1_epi32(mult);819 let vmult = _mm256_set1_epi32(mult);
646 let voffset = _mm256_set1_epi32(offset);820 let voffset = _mm256_set1_epi32(offset);
@@ -651,36 +825,78 @@ mod simd {
651 let sh = _mm_cvtsi32_si128(p.scaling_shift);825 let sh = _mm_cvtsi32_si128(p.scaling_shift);
652 let lo = _mm256_set1_epi32(p.min_chroma);826 let lo = _mm256_set1_epi32(p.min_chroma);
653 let hi = _mm256_set1_epi32(p.max_chroma);827 let hi = _mm256_set1_epi32(p.max_chroma);
654- let cp = chroma.as_mut_ptr();828+ let ones4 = _mm256_castsi256_si128(ones);
655- let lp = luma.as_ptr();829+ let one4 = _mm256_castsi256_si128(one);
656- let gp = grain.as_ptr();830+ let vluma_mult4 = _mm256_castsi256_si128(vluma_mult);
657- let mut c = 0usize;831+ let vmult4 = _mm256_castsi256_si128(vmult);
658- while c + 8 <= n {832+ let voffset4 = _mm256_castsi256_si128(voffset);
659- let l = _mm256_loadu_si256(lp.add(c * 2).cast());833+ let vindex_max4 = _mm256_castsi256_si128(vindex_max);
660- let pair_sum = _mm256_madd_epi16(l, ones);834+ let last4 = _mm256_castsi256_si128(last);
661- let avg = _mm256_srai_epi32(_mm256_add_epi32(pair_sum, _mm256_set1_epi32(1)), 1);835+ let zero4 = _mm256_castsi256_si128(zero);
662- let c16 = _mm_loadu_si128(cp.add(c).cast());836+ let rnd4 = _mm256_castsi256_si128(rnd);
663- let c32 = _mm256_cvtepu16_epi32(c16);837+ let lo4 = _mm256_castsi256_si128(lo);
664- let mixed = _mm256_add_epi32(838+ let hi4 = _mm256_castsi256_si128(hi);
665- _mm256_mullo_epi32(avg, vluma_mult),839+ let sp = scaling.as_ptr();
666- _mm256_mullo_epi32(c32, vmult),840+ for r in 0..rows {
841+ let cp = chroma.as_mut_ptr().add(r * c_stride);
842+ let lp = luma.as_ptr().add(r * l_stride);
843+ let gp = grain.as_ptr().add(r * g_stride);
844+ let mut c = 0usize;
845+ while c + 8 <= n {
846+ let l = _mm256_loadu_si256(lp.add(c * 2).cast());
847+ let pair_sum = _mm256_madd_epi16(l, ones);
848+ let avg = _mm256_srai_epi32(_mm256_add_epi32(pair_sum, one), 1);
849+ let c16 = _mm_loadu_si128(cp.add(c).cast());
850+ let c32 = _mm256_cvtepu16_epi32(c16);
851+ let mixed = _mm256_add_epi32(
852+ _mm256_mullo_epi32(avg, vluma_mult),
853+ _mm256_mullo_epi32(c32, vmult),
854+ );
855+ let scaled = _mm256_add_epi32(_mm256_srai_epi32(mixed, 6), voffset);
856+ let clamped = _mm256_max_epi32(_mm256_min_epi32(scaled, vindex_max), zero);
857+ let idx = _mm256_min_epi32(clamped, last);
858+ let s = _mm256_i32gather_epi32(sp, idx, 4);
859+ let g = _mm256_loadu_si256(gp.add(c).cast());
860+ let delta = _mm256_sra_epi32(_mm256_add_epi32(_mm256_mullo_epi32(s, g), rnd), sh);
861+ let v = _mm256_max_epi32(_mm256_min_epi32(_mm256_add_epi32(c32, delta), hi), lo);
862+ // Same packing argument as the luma kernel: every lane already
863+ // sits inside `[min_chroma, max_chroma]`.
864+ let packed = _mm256_permute4x64_epi64(_mm256_packus_epi32(v, v), 0b0000_1000);
865+ _mm_storeu_si128(cp.add(c).cast(), _mm256_castsi256_si128(packed));
866+ c += 8;
867+ }
868+ if c + 4 <= n {
869+ let l = _mm_loadu_si128(lp.add(c * 2).cast());
870+ let pair_sum = _mm_madd_epi16(l, ones4);
871+ let avg = _mm_srai_epi32(_mm_add_epi32(pair_sum, one4), 1);
872+ let c16 = _mm_loadl_epi64(cp.add(c).cast());
873+ let c32 = _mm_cvtepu16_epi32(c16);
874+ let mixed =
875+ _mm_add_epi32(_mm_mullo_epi32(avg, vluma_mult4), _mm_mullo_epi32(c32, vmult4));
876+ let scaled = _mm_add_epi32(_mm_srai_epi32(mixed, 6), voffset4);
877+ let clamped = _mm_max_epi32(_mm_min_epi32(scaled, vindex_max4), zero4);
878+ let idx = _mm_min_epi32(clamped, last4);
879+ let s = _mm_i32gather_epi32(sp, idx, 4);
880+ let g = _mm_loadu_si128(gp.add(c).cast());
881+ let delta = _mm_sra_epi32(_mm_add_epi32(_mm_mullo_epi32(s, g), rnd4), sh);
882+ let v = _mm_max_epi32(_mm_min_epi32(_mm_add_epi32(c32, delta), hi4), lo4);
883+ _mm_storel_epi64(cp.add(c).cast(), _mm_packus_epi32(v, v));
884+ c += 4;
885+ }
886+ let cbase = r * c_stride;
887+ let lbase = r * l_stride;
888+ let gbase = r * g_stride;
889+ super::chroma_noise_row_scalar(
890+ &mut chroma[cbase + c..cbase + n],
891+ &luma[lbase + c * 2..lbase + n * 2],
892+ &grain[gbase + c..gbase + n],
893+ scaling,
894+ luma_mult,
895+ mult,
896+ offset,
897+ p,
667 );898 );
668- let scaled = _mm256_add_epi32(_mm256_srai_epi32(mixed, 6), voffset);
669- let clamped = _mm256_max_epi32(_mm256_min_epi32(scaled, vindex_max), zero);
670- let idx = _mm256_min_epi32(clamped, last);
671- let s = _mm256_i32gather_epi32(scaling.as_ptr(), idx, 4);
672- let g = _mm256_loadu_si256(gp.add(c).cast());
673- let delta = _mm256_sra_epi32(_mm256_add_epi32(_mm256_mullo_epi32(s, g), rnd), sh);
674- let v = _mm256_max_epi32(_mm256_min_epi32(_mm256_add_epi32(c32, delta), hi), lo);
675- // Same packing argument as the luma kernel: every lane already
676- // sits inside `[min_chroma, max_chroma]`.
677- let packed = _mm256_permute4x64_epi64(_mm256_packus_epi32(v, v), 0b0000_1000);
678- _mm_storeu_si128(cp.add(c).cast(), _mm256_castsi256_si128(packed));
679- c += 8;
680 }899 }
681- super::chroma_noise_row_scalar(
682- &mut chroma[c..], &luma[c * 2..], &grain[c..], scaling, luma_mult, mult, offset, p,
683- );
684 }900 }
685 }901 }
686 902