From: Kostya Shishkov Date: Sat, 7 Aug 2021 16:36:42 +0000 (+0200) Subject: codec_support: add compress_sample() for IMA ADPCM state X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=4ee6b98f1975795210f654d3365aac1be0043ceb;p=nihav.git codec_support: add compress_sample() for IMA ADPCM state --- diff --git a/nihav-codec-support/src/codecs/imaadpcm.rs b/nihav-codec-support/src/codecs/imaadpcm.rs index ac2d082..c2e7fd0 100644 --- a/nihav-codec-support/src/codecs/imaadpcm.rs +++ b/nihav-codec-support/src/codecs/imaadpcm.rs @@ -56,6 +56,13 @@ impl IMAState { self.step = istep.max(0).min(IMA_MAX_STEP as isize) as usize; self.predictor as i16 } + ///! Computes an encoded nibble from an input sample. + pub fn compress_sample(&self, sample: i16) -> u8 { + let diff = i32::from(sample) - self.predictor; + let sign = if diff >= 0 { 0 } else { 8 }; + let nib = (diff.abs() * 4 / IMA_STEP_TABLE[self.step]).min(7) as u8; + nib | sign + } } impl Default for IMAState {