diff --git a/include/common/bitdepth.rs b/include/common/bitdepth.rs index cee8269c3..610de3c7d 100644 --- a/include/common/bitdepth.rs +++ b/include/common/bitdepth.rs @@ -119,7 +119,8 @@ pub trait BitDepth: Clone + Copy { + ToPrimitive; type Scaling: AsRef<[u8]> + AsMut<[u8]> + ArrayDefault + Copy; - const SCALING_SIZE: usize; + const SCALING_BITS: usize; + const SCALING_SIZE: usize = 1 << Self::SCALING_BITS; type BitDepthMax; @@ -206,7 +207,7 @@ impl BitDepth for BitDepth8 { type Entry = i8; type Scaling = [u8; Self::SCALING_SIZE]; - const SCALING_SIZE: usize = 1 << 8; + const SCALING_BITS: usize = 8; type BitDepthMax = (); @@ -287,7 +288,7 @@ impl BitDepth for BitDepth16 { type Entry = i16; type Scaling = [u8; Self::SCALING_SIZE]; - const SCALING_SIZE: usize = 1 << 12; + const SCALING_BITS: usize = 12; type BitDepthMax = Self::Pixel; diff --git a/src/filmgrain.rs b/src/filmgrain.rs index fc7648324..e1b874ad6 100644 --- a/src/filmgrain.rs +++ b/src/filmgrain.rs @@ -792,8 +792,10 @@ unsafe fn fguv_32x32xn_rust( bd.bitdepth_max().as_::(), ); } + // `val` isn't out of bounds, so we can + // eliminate extra panicking code by bit-truncating `val`. let noise = round2( - scaling.as_ref()[val as usize] as c_int * grain, + scaling.as_ref()[val as usize % scaling.as_ref().len()] as c_int * grain, data.scaling_shift, ); *dst = iclip((*src).as_::() + noise, min_value, max_value).as_::();