X-Git-Url: https://git.nihav.org/?p=nihav.git;a=blobdiff_plain;f=nihav-codec-support%2Fsrc%2Fcodecs%2Fimaadpcm.rs;h=c2e7fd042bb9eec7a21d09b9ae0a2e075bd0014e;hp=a2da1d316bc90dd42abddce01d26941b4d3a00ff;hb=4ee6b98f1975795210f654d3365aac1be0043ceb;hpb=03011b993dc4873b39d981f62abc01591a0544f7 diff --git a/nihav-codec-support/src/codecs/imaadpcm.rs b/nihav-codec-support/src/codecs/imaadpcm.rs index a2da1d3..c2e7fd0 100644 --- a/nihav-codec-support/src/codecs/imaadpcm.rs +++ b/nihav-codec-support/src/codecs/imaadpcm.rs @@ -24,6 +24,7 @@ pub const IMA_STEP_TABLE: [i32; 89] = [ ///! Maximum step value for IMA ADPCM. pub const IMA_MAX_STEP: u8 = 88; +#[derive(Clone,Copy,Debug)] ///! Decoder for IMA ADPCM. pub struct IMAState { ///! Current sample value. @@ -55,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 {