aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKostya Shishkov <kostya.shishkov@gmail.com>2021-08-07 18:36:42 +0200
committerKostya Shishkov <kostya.shishkov@gmail.com>2021-08-07 18:37:00 +0200
commit4ee6b98f1975795210f654d3365aac1be0043ceb (patch)
tree946fd702619f50b32ee49b8a756e961d567326c5
parentd6c81913b6633bb6a44aad6f8ca06cee55e26660 (diff)
downloadnihav-4ee6b98f1975795210f654d3365aac1be0043ceb.tar.gz
codec_support: add compress_sample() for IMA ADPCM state
-rw-r--r--nihav-codec-support/src/codecs/imaadpcm.rs7
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 {