| 1 | use nihav_core::codecs::{DecoderResult, DecoderError}; |
| 2 | use nihav_core::io::bitreader::*; |
| 3 | use std::f32::consts; |
| 4 | use super::MAX_WINDOWS; |
| 5 | |
| 6 | #[derive(Clone,Copy)] |
| 7 | pub struct LTPData { |
| 8 | } |
| 9 | |
| 10 | impl LTPData { |
| 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"); |
| 15 | /* |
| 16 | if is_main { |
| 17 | let predictor_reset = br.read_bool()?; |
| 18 | if predictor_reset { |
| 19 | let predictor_reset_group_number = br.read(5)?; |
| 20 | } |
| 21 | for sfb in 0..max_sfb.min(PRED_SFB_MAX) { |
| 22 | prediction_used[sfb] = br.read_bool()?; |
| 23 | } |
| 24 | } else { |
| 25 | let ltp_data_present = br.read_bool()?; |
| 26 | if ltp_data_present { |
| 27 | //ltp data |
| 28 | } |
| 29 | if common_window { |
| 30 | let ltp_data_present = br.read_bool()?; |
| 31 | if ltp_data_present { |
| 32 | //ltp data |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | Ok(Some(Self { })) |
| 37 | */ |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | #[derive(Clone,Copy)] |
| 42 | #[allow(dead_code)] |
| 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], |
| 48 | } |
| 49 | |
| 50 | impl PulseData { |
| 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); } |
| 54 | |
| 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; |
| 62 | } |
| 63 | Ok(Some(Self{ number_pulse, pulse_start_sfb, pulse_offset, pulse_amp })) |
| 64 | } |
| 65 | } |
| 66 | |
| 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 ]; |
| 70 | |
| 71 | #[derive(Clone,Copy)] |
| 72 | pub struct TNSCoeffs { |
| 73 | pub length: usize, |
| 74 | pub order: usize, |
| 75 | pub direction: bool, |
| 76 | pub compress: bool, |
| 77 | pub coef: [f32; TNS_MAX_ORDER + 1], |
| 78 | } |
| 79 | |
| 80 | impl TNSCoeffs { |
| 81 | pub fn new() -> Self { |
| 82 | Self { |
| 83 | length: 0, order: 0, direction: false, compress: false, coef: [0.0; TNS_MAX_ORDER + 1], |
| 84 | } |
| 85 | } |
| 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); |
| 90 | if self.order > 0 { |
| 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); |
| 98 | |
| 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(); |
| 107 | } |
| 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 { |
| 111 | for i in 1..m { |
| 112 | b[i] = self.coef[i - 1] + tmp[m - 1] * self.coef[m - i - 1]; |
| 113 | } |
| 114 | for i in 1..m { |
| 115 | self.coef[i - 1] = b[i]; |
| 116 | } |
| 117 | self.coef[m - 1] = tmp[m - 1]; |
| 118 | } |
| 119 | } |
| 120 | Ok(()) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | #[derive(Clone,Copy)] |
| 125 | #[allow(dead_code)] |
| 126 | pub struct TNSData { |
| 127 | pub n_filt: [usize; MAX_WINDOWS], |
| 128 | coef_res: [bool; MAX_WINDOWS], |
| 129 | pub coeffs: [[TNSCoeffs; 4]; MAX_WINDOWS], |
| 130 | } |
| 131 | |
| 132 | impl TNSData { |
| 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; |
| 141 | if n_filt[w] != 0 { |
| 142 | coef_res[w] = br.read_bool()?; |
| 143 | } |
| 144 | for filt in 0..n_filt[w] { |
| 145 | coeffs[w][filt].read(br, long_win, coef_res[w], max_order)?; |
| 146 | } |
| 147 | } |
| 148 | Ok(Some(Self { n_filt, coef_res, coeffs })) |
| 149 | } |
| 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]; |
| 154 | let mut sidx = 32; |
| 155 | if !self.coeffs[w][f].direction { |
| 156 | for m in start..end { |
| 157 | for i in 0..order { |
| 158 | coeffs[m] -= state[(sidx + i) & 63] * lpc[i]; |
| 159 | } |
| 160 | sidx = (sidx + 63) & 63; |
| 161 | state[sidx] = coeffs[m]; |
| 162 | } |
| 163 | } else { |
| 164 | for m in (start..end).rev() { |
| 165 | for i in 0..order { |
| 166 | coeffs[m] -= state[(sidx + i) & 63] * lpc[i]; |
| 167 | } |
| 168 | sidx = (sidx + 63) & 63; |
| 169 | state[sidx] = coeffs[m]; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | pub fn get_max_bands(long_win: bool, srate_idx: usize) -> usize { |
| 174 | if long_win { |
| 175 | TNS_MAX_LONG_BANDS[srate_idx] |
| 176 | } else { |
| 177 | TNS_MAX_SHORT_BANDS[srate_idx] |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | #[derive(Clone,Copy)] |
| 183 | #[allow(dead_code)] |
| 184 | pub struct GainControlData { |
| 185 | max_band: u8, |
| 186 | } |
| 187 | |
| 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 |
| 196 | ... |
| 197 | } |
| 198 | Ok(Some(Self { }))*/ |
| 199 | } |
| 200 | } |