# DEGRAIN KNOWLEDGE BASE
**Generated:** 2026-04-17
## OVERVIEW
Temporal denoising implementations. Weighted averaging of compensated reference frames.
## STRUCTURE
```
src/degrain/
├── rust.rs # Scalar implementation
├── avx2.rs # AVX2 SIMD implementation
├── tests.rs # Test generator macro (degrain_tests!)
└── degrain.rs # Module root, dispatch
```
## WHERE TO LOOK
| Scalar degrain | `rust.rs` | Weighted averaging, `T::from(sum >> 8).expect("fits in T")` |
| AVX2 degrain | `avx2.rs` | SIMD-optimized weighted accumulation, compiled with `avx2` feature |
| Test coverage | `tests.rs` | `degrain_tests!` macro for both backends |
| Filter entry | `src/filters/mv_degrain.rs` | Generic `<RADIUS>` struct, Degrain1-6 registrations |
## CONVENTIONS
- Generic over pixel type `T: Pixel`.
- `avx2` feature gates SIMD degrain compilation; `--no-default-features` exercises scalar-only build.
- `select_degrain::<T, RADIUS>(w, h)` requires `RADIUS * 2` refs, strides, and weights.
- Strides are byte-based: `width * size_of::<T>()`.
- Output conversion: `T::from(sum >> 8).expect("fits in T")` — weights must avoid overflow.
- Safe benchmark baseline: `w_src = 256`, all ref weights `0`.
## ANTI-PATTERNS
- Overflow in weighted sum (weights too large for `T::from(sum >> 8)`).
- Incorrect stride calculation (pixel count vs byte count).
- Missing reference frame count validation (`RADIUS * 2` required).