codec_support: add compress_sample() for IMA ADPCM state
authorKostya Shishkov <kostya.shishkov@gmail.com>
Sat, 7 Aug 2021 16:36:42 +0000 (18:36 +0200)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Sat, 7 Aug 2021 16:37:00 +0000 (18:37 +0200)
nihav-codec-support/src/codecs/imaadpcm.rs

index ac2d082aadf0d73f8a3b95eb8645ea50b69612f4..c2e7fd042bb9eec7a21d09b9ae0a2e075bd0014e 100644 (file)
@@ -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 {