From e9f61dd24a78c4a42537ab129a14a37107e8b5a0 Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Sat, 30 Sep 2023 11:59:51 +0200 Subject: [PATCH] binkaud: split out common data definitions for the future encoder --- nihav-rad/src/codecs/binkaud.rs | 30 ++++----------------------- nihav-rad/src/codecs/binkauddata.rs | 32 +++++++++++++++++++++++++++++ nihav-rad/src/codecs/mod.rs | 2 ++ 3 files changed, 38 insertions(+), 26 deletions(-) create mode 100644 nihav-rad/src/codecs/binkauddata.rs diff --git a/nihav-rad/src/codecs/binkaud.rs b/nihav-rad/src/codecs/binkaud.rs index 7848e7e..b424c0e 100644 --- a/nihav-rad/src/codecs/binkaud.rs +++ b/nihav-rad/src/codecs/binkaud.rs @@ -2,17 +2,16 @@ use nihav_core::codecs::*; use nihav_codec_support::dsp::dct::*; use nihav_codec_support::dsp::fft::*; use nihav_core::io::bitreader::*; -use std::f32::consts; use std::str::FromStr; +use super::binkauddata::*; + enum Transform { None, DCT(DCT), RDFT(RDFT), } -const MAX_BANDS: usize = 25; - struct BinkAudioDecoder { ainfo: NAAudioInfo, chmap: NAChannelMap, @@ -60,10 +59,6 @@ fn overlap(a: &[f32], b: &[f32], dst: &mut [f32], len: usize, step: usize) { impl BinkAudioDecoder { fn new(use_dct: bool) -> Self { - let mut quants: [f32; 96] = [0.0; 96]; - for i in 0..quants.len() { - quants[i] = ((i as f32) * 0.0664 / consts::LOG10_E).exp(); - } Self { ainfo: NAAudioInfo::new(0, 1, SND_F32P_FORMAT, 0), chmap: NAChannelMap::new(), @@ -71,7 +66,7 @@ impl BinkAudioDecoder { transform: Transform::None, version_b: false, len: 0, - quants, + quants: get_quants_table(), bands: [MAX_BANDS + 1; 26], num_bands: 0, duration: 0, @@ -169,15 +164,6 @@ impl BinkAudioDecoder { } } -const CRITICAL_FREQS: [usize; MAX_BANDS] = [ - 100, 200, 300, 400, 510, 630, 770, 920, - 1080, 1270, 1480, 1720, 2000, 2320, 2700, 3150, - 3700, 4400, 5300, 6400, 7700, 9500, 12000, 15500, - 24500 -]; - -const RUN_TAB: [usize; 16] = [ 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64 ]; - impl NADecoder for BinkAudioDecoder { fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> { if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() { @@ -214,15 +200,7 @@ impl NADecoder for BinkAudioDecoder { (2.0 / (self.len as f32)).sqrt() / 1024.0 }; let s_srate = if single { srate } else { srate >> 1 } as usize; - self.num_bands = 1; - while self.num_bands < CRITICAL_FREQS.len() && CRITICAL_FREQS[self.num_bands - 1] < s_srate { - self.num_bands += 1; - } - self.bands[0] = 2; - for i in 1..self.num_bands { - self.bands[i] = (CRITICAL_FREQS[i - 1] * self.len / s_srate) & !1; - } - self.bands[self.num_bands] = self.len; + init_bands(s_srate, self.len, &mut self.num_bands, &mut self.bands); self.first_frm = true; Ok(()) diff --git a/nihav-rad/src/codecs/binkauddata.rs b/nihav-rad/src/codecs/binkauddata.rs new file mode 100644 index 0000000..1cdce60 --- /dev/null +++ b/nihav-rad/src/codecs/binkauddata.rs @@ -0,0 +1,32 @@ +use std::f32::consts; + +pub const MAX_BANDS: usize = 25; + +const CRITICAL_FREQS: [usize; MAX_BANDS] = [ + 100, 200, 300, 400, 510, 630, 770, 920, + 1080, 1270, 1480, 1720, 2000, 2320, 2700, 3150, + 3700, 4400, 5300, 6400, 7700, 9500, 12000, 15500, + 24500 +]; + +pub const RUN_TAB: [usize; 16] = [ 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64 ]; + +pub fn init_bands(srate: usize, len: usize, num_bands: &mut usize, bands: &mut [usize; MAX_BANDS + 1]) { + *num_bands = 1; + while *num_bands < CRITICAL_FREQS.len() && CRITICAL_FREQS[*num_bands - 1] < srate { + *num_bands += 1; + } + bands[0] = 2; + for i in 1..*num_bands { + bands[i] = (CRITICAL_FREQS[i - 1] * len / srate) & !1; + } + bands[*num_bands] = len; +} + +pub fn get_quants_table() -> [f32; 96] { + let mut quants: [f32; 96] = [0.0; 96]; + for i in 0..quants.len() { + quants[i] = ((i as f32) * 0.0664 / consts::LOG10_E).exp(); + } + quants +} diff --git a/nihav-rad/src/codecs/mod.rs b/nihav-rad/src/codecs/mod.rs index 14db405..e8fbe86 100644 --- a/nihav-rad/src/codecs/mod.rs +++ b/nihav-rad/src/codecs/mod.rs @@ -13,6 +13,8 @@ macro_rules! validate { mod smacker; #[cfg(feature="decoder_binkaud")] mod binkaud; +#[cfg(feature="decoder_binkaud")] +mod binkauddata; #[cfg(feature="decoder_binkvid")] mod binkvid; #[cfg(any(feature="decoder_binkvid", feature="encoder_binkvid"))] -- 2.39.5