From: Kostya Shishkov Date: Sat, 3 Oct 2020 10:13:33 +0000 (+0200) Subject: ape: move dotproduct+filter adaptation into a separate function X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=d33a2721fd23f2d9d9368b741be84ceded87d49a;p=nihav.git ape: move dotproduct+filter adaptation into a separate function --- diff --git a/nihav-llaudio/src/codecs/apepred.rs b/nihav-llaudio/src/codecs/apepred.rs index 6aad207..547164c 100644 --- a/nihav-llaudio/src/codecs/apepred.rs +++ b/nihav-llaudio/src/codecs/apepred.rs @@ -88,6 +88,19 @@ struct NFilterContext { new: bool, } +fn adapt_loop(filt: &mut [i32], coeffs: &[i32], adapt: &[i32], val: i32) -> i32 { + let mut sum = 0i32; + for (coef, (res, adapt)) in filt.iter_mut().zip(coeffs.iter().zip(adapt.iter())) { + sum += *coef * *res; + if val < 0 { + *coef += *adapt; + } else if val > 0 { + *coef -= *adapt; + } + } + sum +} + impl NFilterContext { fn new(ord16: u8, bits: u8, new: bool) -> Self { let order = ord16 as usize * 16; @@ -110,15 +123,9 @@ impl NFilterContext { let mut adapt_pos = self.order; let mut delay_pos = self.order * 2; for el in dst.iter_mut() { - let mut sum = 0i32; - for (i, coef) in self.coeffs.iter_mut().enumerate() { - sum += *coef * self.buf[delay_pos - self.order + i]; - if *el < 0 { - *coef += self.buf[adapt_pos - self.order + i]; - } else if *el > 0 { - *coef -= self.buf[adapt_pos - self.order + i]; - } - } + let sum = adapt_loop(&mut self.coeffs, + &self.buf[delay_pos - self.order..], + &self.buf[adapt_pos - self.order..], *el); let pred = (sum + (1 << (self.bits - 1))) >> self.bits; let val = *el + pred; *el = val;