]> git.nihav.org Git - nihav.git/blame - nihav-mpeg/src/codecs/aac/tools.rs
avimux: do not record palette change chunks in OpenDML index
[nihav.git] / nihav-mpeg / src / codecs / aac / tools.rs
CommitLineData
999c25f7
KS
1use nihav_core::codecs::{DecoderResult, DecoderError};
2use nihav_core::io::bitreader::*;
3use std::f32::consts;
4use super::MAX_WINDOWS;
5
6#[derive(Clone,Copy)]
7pub struct LTPData {
8}
9
10impl 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); }
14unimplemented!("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)]
43pub 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
50impl 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
67pub const TNS_MAX_ORDER: usize = 20;
68const TNS_MAX_LONG_BANDS: [usize; 12] = [ 31, 31, 34, 40, 42, 51, 46, 46, 42, 42, 42, 39 ];
69const TNS_MAX_SHORT_BANDS: [usize; 12] = [ 9, 9, 10, 14, 14, 14, 14, 14, 14, 14, 14, 14 ];
70
71#[derive(Clone,Copy)]
72pub 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
80impl 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 {
d1d0b8bb 111 #[allow(clippy::needless_range_loop)]
999c25f7
KS
112 for i in 1..m {
113 b[i] = self.coef[i - 1] + tmp[m - 1] * self.coef[m - i - 1];
114 }
d1d0b8bb 115 self.coef[..m-1].copy_from_slice(&b[1..m]);
999c25f7
KS
116 self.coef[m - 1] = tmp[m - 1];
117 }
118 }
119 Ok(())
120 }
121}
122
123#[derive(Clone,Copy)]
124#[allow(dead_code)]
125pub struct TNSData {
126 pub n_filt: [usize; MAX_WINDOWS],
127 coef_res: [bool; MAX_WINDOWS],
128 pub coeffs: [[TNSCoeffs; 4]; MAX_WINDOWS],
129}
130
131impl TNSData {
132 pub fn read(br: &mut BitReader, long_win: bool, num_windows: usize, max_order: usize) -> DecoderResult<Option<Self>> {
133 let tns_data_present = br.read_bool()?;
134 if !tns_data_present { return Ok(None); }
135 let mut n_filt: [usize; MAX_WINDOWS] = [0; MAX_WINDOWS];
136 let mut coef_res: [bool; MAX_WINDOWS] = [false; MAX_WINDOWS];
137 let mut coeffs: [[TNSCoeffs; 4]; MAX_WINDOWS] = [[TNSCoeffs::new(); 4]; MAX_WINDOWS];
138 for w in 0..num_windows {
139 n_filt[w] = br.read(if long_win { 2 } else { 1 })? as usize;
140 if n_filt[w] != 0 {
141 coef_res[w] = br.read_bool()?;
142 }
143 for filt in 0..n_filt[w] {
144 coeffs[w][filt].read(br, long_win, coef_res[w], max_order)?;
145 }
146 }
147 Ok(Some(Self { n_filt, coef_res, coeffs }))
148 }
149 pub fn apply(&self, coeffs: &mut [f32; 1024], w: usize, f: usize, start: usize, end: usize) {
150 let order = self.coeffs[w][f].order;
151 let lpc = &self.coeffs[w][f].coef;
152 let mut state = [0.0f32; 64];
153 let mut sidx = 32;
154 if !self.coeffs[w][f].direction {
d1d0b8bb 155 for coef in coeffs[start..end].iter_mut() {
999c25f7 156 for i in 0..order {
d1d0b8bb 157 *coef -= state[(sidx + i) & 63] * lpc[i];
999c25f7
KS
158 }
159 sidx = (sidx + 63) & 63;
d1d0b8bb 160 state[sidx] = *coef;
999c25f7
KS
161 }
162 } else {
d1d0b8bb 163 for coef in coeffs[start..end].iter_mut().rev() {
999c25f7 164 for i in 0..order {
d1d0b8bb 165 *coef -= state[(sidx + i) & 63] * lpc[i];
999c25f7
KS
166 }
167 sidx = (sidx + 63) & 63;
d1d0b8bb 168 state[sidx] = *coef;
999c25f7
KS
169 }
170 }
171 }
172 pub fn get_max_bands(long_win: bool, srate_idx: usize) -> usize {
173 if long_win {
174 TNS_MAX_LONG_BANDS[srate_idx]
175 } else {
176 TNS_MAX_SHORT_BANDS[srate_idx]
177 }
178 }
179}
180
181#[derive(Clone,Copy)]
182#[allow(dead_code)]
183pub struct GainControlData {
184 max_band: u8,
185}
186
187impl GainControlData {
188 pub fn read(br: &mut BitReader) -> DecoderResult<Option<Self>> {
189 let gain_control_data_present = br.read_bool()?;
190 if !gain_control_data_present { return Ok(None); }
191unimplemented!("gain control data");
192/* self.max_band = br.read(2)? as u8;
193 if window_sequence == ONLY_LONG_SEQUENCE {
194 for bd in 0..max_band
195...
196 }
197 Ok(Some(Self { }))*/
198 }
199}