1 use nihav_core::codecs::{DecoderResult, DecoderError};
2 use nihav_core::io::bitreader::*;
4 use super::MAX_WINDOWS;
11 pub fn read(br: &mut BitReader) -> DecoderResult<Option<Self>> {
12 let predictor_data_present = br.read_bool()?;
13 if !predictor_data_present { return Ok(None); }
14 unimplemented!("predictor data");
17 let predictor_reset = br.read_bool()?;
19 let predictor_reset_group_number = br.read(5)?;
21 for sfb in 0..max_sfb.min(PRED_SFB_MAX) {
22 prediction_used[sfb] = br.read_bool()?;
25 let ltp_data_present = br.read_bool()?;
30 let ltp_data_present = br.read_bool()?;
43 pub struct PulseData {
44 pub number_pulse: usize,
45 pub pulse_start_sfb: usize,
46 pub pulse_offset: [u8; 4],
47 pub pulse_amp: [u8; 4],
51 pub fn read(br: &mut BitReader) -> DecoderResult<Option<Self>> {
52 let pulse_data_present = br.read_bool()?;
53 if !pulse_data_present { return Ok(None); }
55 let number_pulse = (br.read(2)? as usize) + 1;
56 let pulse_start_sfb = br.read(6)? as usize;
57 let mut pulse_offset: [u8; 4] = [0; 4];
58 let mut pulse_amp: [u8; 4] = [0; 4];
59 for i in 0..number_pulse {
60 pulse_offset[i] = br.read(5)? as u8;
61 pulse_amp[i] = br.read(4)? as u8;
63 Ok(Some(Self{ number_pulse, pulse_start_sfb, pulse_offset, pulse_amp }))
67 pub const TNS_MAX_ORDER: usize = 20;
68 const TNS_MAX_LONG_BANDS: [usize; 12] = [ 31, 31, 34, 40, 42, 51, 46, 46, 42, 42, 42, 39 ];
69 const TNS_MAX_SHORT_BANDS: [usize; 12] = [ 9, 9, 10, 14, 14, 14, 14, 14, 14, 14, 14, 14 ];
72 pub struct TNSCoeffs {
77 pub coef: [f32; TNS_MAX_ORDER + 1],
81 pub fn new() -> Self {
83 length: 0, order: 0, direction: false, compress: false, coef: [0.0; TNS_MAX_ORDER + 1],
86 pub fn read(&mut self, br: &mut BitReader, long_win: bool, coef_res: bool, max_order: usize) -> DecoderResult<()> {
87 self.length = br.read(if long_win { 6 } else { 4 })? as usize;
88 self.order = br.read(if long_win { 5 } else { 3 })? as usize;
89 validate!(self.order <= max_order);
91 self.direction = br.read_bool()?;
92 self.compress = br.read_bool()?;
93 let mut coef_bits = 3;
94 if coef_res { coef_bits += 1; }
95 if self.compress { coef_bits -= 1; }
96 let sign_mask = 1 << (coef_bits - 1);
97 let neg_mask = !(sign_mask * 2 - 1);
99 let fac_base = if coef_res { 1 << 3 } else { 1 << 2 } as f32;
100 let iqfac = (fac_base - 0.5) / (consts::PI / 2.0);
101 let iqfac_m = (fac_base + 0.5) / (consts::PI / 2.0);
102 let mut tmp: [f32; TNS_MAX_ORDER] = [0.0; TNS_MAX_ORDER];
103 for el in tmp.iter_mut().take(self.order) {
104 let val = br.read(coef_bits)? as i8;
105 let c = f32::from(if (val & sign_mask) != 0 { val | neg_mask } else { val });
106 *el = (if c >= 0.0 { c / iqfac } else { c / iqfac_m }).sin();
108 // convert to LPC coefficients
109 let mut b: [f32; TNS_MAX_ORDER + 1] = [0.0; TNS_MAX_ORDER + 1];
110 for m in 1..=self.order {
112 b[i] = self.coef[i - 1] + tmp[m - 1] * self.coef[m - i - 1];
115 self.coef[i - 1] = b[i];
117 self.coef[m - 1] = tmp[m - 1];
124 #[derive(Clone,Copy)]
127 pub n_filt: [usize; MAX_WINDOWS],
128 coef_res: [bool; MAX_WINDOWS],
129 pub coeffs: [[TNSCoeffs; 4]; MAX_WINDOWS],
133 pub fn read(br: &mut BitReader, long_win: bool, num_windows: usize, max_order: usize) -> DecoderResult<Option<Self>> {
134 let tns_data_present = br.read_bool()?;
135 if !tns_data_present { return Ok(None); }
136 let mut n_filt: [usize; MAX_WINDOWS] = [0; MAX_WINDOWS];
137 let mut coef_res: [bool; MAX_WINDOWS] = [false; MAX_WINDOWS];
138 let mut coeffs: [[TNSCoeffs; 4]; MAX_WINDOWS] = [[TNSCoeffs::new(); 4]; MAX_WINDOWS];
139 for w in 0..num_windows {
140 n_filt[w] = br.read(if long_win { 2 } else { 1 })? as usize;
142 coef_res[w] = br.read_bool()?;
144 for filt in 0..n_filt[w] {
145 coeffs[w][filt].read(br, long_win, coef_res[w], max_order)?;
148 Ok(Some(Self { n_filt, coef_res, coeffs }))
150 pub fn apply(&self, coeffs: &mut [f32; 1024], w: usize, f: usize, start: usize, end: usize) {
151 let order = self.coeffs[w][f].order;
152 let lpc = &self.coeffs[w][f].coef;
153 let mut state = [0.0f32; 64];
155 if !self.coeffs[w][f].direction {
156 for m in start..end {
158 coeffs[m] -= state[(sidx + i) & 63] * lpc[i];
160 sidx = (sidx + 63) & 63;
161 state[sidx] = coeffs[m];
164 for m in (start..end).rev() {
166 coeffs[m] -= state[(sidx + i) & 63] * lpc[i];
168 sidx = (sidx + 63) & 63;
169 state[sidx] = coeffs[m];
173 pub fn get_max_bands(long_win: bool, srate_idx: usize) -> usize {
175 TNS_MAX_LONG_BANDS[srate_idx]
177 TNS_MAX_SHORT_BANDS[srate_idx]
182 #[derive(Clone,Copy)]
184 pub struct GainControlData {
188 impl GainControlData {
189 pub fn read(br: &mut BitReader) -> DecoderResult<Option<Self>> {
190 let gain_control_data_present = br.read_bool()?;
191 if !gain_control_data_present { return Ok(None); }
192 unimplemented!("gain control data");
193 /* self.max_band = br.read(2)? as u8;
194 if window_sequence == ONLY_LONG_SEQUENCE {
195 for bd in 0..max_band