diff options
author | Kostya Shishkov <kostya.shishkov@gmail.com> | 2021-08-07 18:36:42 +0200 |
---|---|---|
committer | Kostya Shishkov <kostya.shishkov@gmail.com> | 2021-08-07 18:37:00 +0200 |
commit | 4ee6b98f1975795210f654d3365aac1be0043ceb (patch) | |
tree | 946fd702619f50b32ee49b8a756e961d567326c5 | |
parent | d6c81913b6633bb6a44aad6f8ca06cee55e26660 (diff) | |
download | nihav-4ee6b98f1975795210f654d3365aac1be0043ceb.tar.gz |
codec_support: add compress_sample() for IMA ADPCM state
-rw-r--r-- | nihav-codec-support/src/codecs/imaadpcm.rs | 7 |
1 files changed, 7 insertions, 0 deletions
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 { |