From: Kostya Shishkov Date: Thu, 16 Apr 2020 07:52:50 +0000 (+0200) Subject: move IMA ADPCM decoder into codec_support X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=e781ccc3c7149249c03885e41da02c50faf745bd;p=nihav.git move IMA ADPCM decoder into codec_support --- diff --git a/nihav-codec-support/src/codecs/imaadpcm.rs b/nihav-codec-support/src/codecs/imaadpcm.rs new file mode 100644 index 0000000..6ff92eb --- /dev/null +++ b/nihav-codec-support/src/codecs/imaadpcm.rs @@ -0,0 +1,60 @@ +//! IMA ADPCM decoding functionality. + +///! IMA ADPCM step change table. +pub const IMA_STEPS: [i8; 16] = [ + -1, -1, -1, -1, 2, 4, 6, 8, + -1, -1, -1, -1, 2, 4, 6, 8 +]; + +///! IMA ADPCM step size table. +pub const IMA_STEP_TABLE: [i32; 89] = [ + 7, 8, 9, 10, 11, 12, 13, 14, + 16, 17, 19, 21, 23, 25, 28, 31, + 34, 37, 41, 45, 50, 55, 60, 66, + 73, 80, 88, 97, 107, 118, 130, 143, + 157, 173, 190, 209, 230, 253, 279, 307, + 337, 371, 408, 449, 494, 544, 598, 658, + 724, 796, 876, 963, 1060, 1166, 1282, 1411, + 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, + 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, + 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, + 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 +]; + +///! Maximum step value for IMA ADPCM. +pub const IMA_MAX_STEP: u8 = 88; + +///! Decoder for IMA ADPCM. +pub struct IMAState { + ///! Current sample value. + pub predictor: i32, + ///! Current step index. + pub step: usize, +} + +impl IMAState { + ///! Constructs a new instance of `IMAState`. + pub fn new() -> Self { + Self { + predictor: 0, + step: 0, + } + } + ///! Re-initialises decoder with new predictor and step values. + pub fn reset(&mut self, predictor: i16, step: u8) { + self.predictor = i32::from(predictor); + self.step = step.min(IMA_MAX_STEP) as usize; + } + ///! Computes a new sample value from an input nibble. + pub fn expand_sample(&mut self, nibble: u8) -> i16 { + let istep = (self.step as isize) + (IMA_STEPS[(nibble & 0xF) as usize] as isize); + let sign = (nibble & 8) != 0; + let diff = (i32::from(2 * (nibble & 7) + 1) * IMA_STEP_TABLE[self.step]) >> 3; + let sample = if !sign { self.predictor + diff } else { self.predictor - diff }; + self.predictor = sample.max(i32::from(std::i16::MIN)).min(i32::from(std::i16::MAX)); + self.step = istep.max(0).min(IMA_MAX_STEP as isize) as usize; + self.predictor as i16 + } +} + + diff --git a/nihav-codec-support/src/codecs/mod.rs b/nihav-codec-support/src/codecs/mod.rs index 4353ad9..0d5307c 100644 --- a/nihav-codec-support/src/codecs/mod.rs +++ b/nihav-codec-support/src/codecs/mod.rs @@ -311,3 +311,5 @@ pub const ZIGZAG: [usize; 64] = [ 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63 ]; + +pub mod imaadpcm; diff --git a/nihav-duck/src/codecs/dkadpcm.rs b/nihav-duck/src/codecs/dkadpcm.rs index e71dd15..65576e8 100644 --- a/nihav-duck/src/codecs/dkadpcm.rs +++ b/nihav-duck/src/codecs/dkadpcm.rs @@ -1,35 +1,8 @@ use nihav_core::codecs::*; use nihav_core::io::byteio::*; +use nihav_codec_support::codecs::imaadpcm::*; use std::str::FromStr; -const IMA_MAX_STEP: u8 = 88; -struct IMAState { - predictor: i32, - step: usize, -} - -impl IMAState { - fn new() -> Self { - Self { - predictor: 0, - step: 0, - } - } - fn reset(&mut self, predictor: i16, step: u8) { - self.predictor = i32::from(predictor); - self.step = step.min(IMA_MAX_STEP) as usize; - } - fn expand_sample(&mut self, nibble: u8) -> i16 { - let istep = (self.step as isize) + (IMA_STEPS[nibble as usize] as isize); - let sign = (nibble & 8) != 0; - let diff = (i32::from(2 * (nibble & 7) + 1) * IMA_STEP_TABLE[self.step]) >> 3; - let sample = if !sign { self.predictor + diff } else { self.predictor - diff }; - self.predictor = sample.max(i32::from(std::i16::MIN)).min(i32::from(std::i16::MAX)); - self.step = istep.max(0).min(IMA_MAX_STEP as isize) as usize; - self.predictor as i16 - } -} - struct DuckADPCMDecoder { ainfo: NAAudioInfo, chmap: NAChannelMap, @@ -207,22 +180,3 @@ mod test { ExpectedTestResult::MD5([0x04e40d15, 0xf65b3427, 0x1dd5181f, 0xf321b56f])); } } - -const IMA_STEPS: [i8; 16] = [ - -1, -1, -1, -1, 2, 4, 6, 8, - -1, -1, -1, -1, 2, 4, 6, 8 -]; - -const IMA_STEP_TABLE: [i32; 89] = [ - 7, 8, 9, 10, 11, 12, 13, 14, - 16, 17, 19, 21, 23, 25, 28, 31, - 34, 37, 41, 45, 50, 55, 60, 66, - 73, 80, 88, 97, 107, 118, 130, 143, - 157, 173, 190, 209, 230, 253, 279, 307, - 337, 371, 408, 449, 494, 544, 598, 658, - 724, 796, 876, 963, 1060, 1166, 1282, 1411, - 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, - 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, - 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, - 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 -];