]> git.nihav.org Git - nihav.git/blame - nihav-commonfmt/src/codecs/aac.rs
smacker-audio: use interleaved output format as it is stored that way too
[nihav.git] / nihav-commonfmt / src / codecs / aac.rs
CommitLineData
38953fb5
KS
1use nihav_core::formats::*;
2use nihav_core::frame::*;
3use nihav_core::codecs::*;
b4d5b851
KS
4use nihav_codec_support::dsp::mdct::IMDCT;
5use nihav_codec_support::dsp::window::*;
38953fb5
KS
6use nihav_core::io::bitreader::*;
7use nihav_core::io::codebook::*;
5a932f64 8use std::fmt;
38953fb5 9use nihav_core::io::intcode::*;
5a932f64
KS
10use std::mem;
11use std::ptr;
12use std::str::FromStr;
13use std::f32::consts;
14
15#[allow(non_camel_case_types)]
16#[derive(Clone,Copy,PartialEq)]
17enum M4AType {
18 None,
19 Main,
20 LC,
21 SSR,
22 LTP,
23 SBR,
24 Scalable,
25 TwinVQ,
26 CELP,
27 HVXC,
28 TTSI,
29 MainSynth,
30 WavetableSynth,
31 GeneralMIDI,
32 Algorithmic,
33 ER_AAC_LC,
34 ER_AAC_LTP,
35 ER_AAC_Scalable,
36 ER_TwinVQ,
37 ER_BSAC,
38 ER_AAC_LD,
39 ER_CELP,
40 ER_HVXC,
41 ER_HILN,
42 ER_Parametric,
43 SSC,
44 PS,
45 MPEGSurround,
46 Layer1,
47 Layer2,
48 Layer3,
49 DST,
50 ALS,
51 SLS,
52 SLSNonCore,
53 ER_AAC_ELD,
54 SMRSimple,
55 SMRMain,
56 Reserved,
57 Unknown,
58}
59
60const M4A_TYPES: &[M4AType] = &[
61 M4AType::None, M4AType::Main, M4AType::LC, M4AType::SSR,
62 M4AType::LTP, M4AType::SBR, M4AType::Scalable, M4AType::TwinVQ,
63 M4AType::CELP, M4AType::HVXC, M4AType::Reserved, M4AType::Reserved,
64 M4AType::TTSI, M4AType::MainSynth, M4AType::WavetableSynth, M4AType::GeneralMIDI,
65 M4AType::Algorithmic, M4AType::ER_AAC_LC, M4AType::Reserved, M4AType::ER_AAC_LTP,
66 M4AType::ER_AAC_Scalable, M4AType::ER_TwinVQ, M4AType::ER_BSAC, M4AType::ER_AAC_LD,
67 M4AType::ER_CELP, M4AType::ER_HVXC, M4AType::ER_HILN, M4AType::ER_Parametric,
68 M4AType::SSC, M4AType::PS, M4AType::MPEGSurround, M4AType::Reserved /*escape*/,
69 M4AType::Layer1, M4AType::Layer2, M4AType::Layer3, M4AType::DST,
70 M4AType::ALS, M4AType::SLS, M4AType::SLSNonCore, M4AType::ER_AAC_ELD,
71 M4AType::SMRSimple, M4AType::SMRMain,
72];
73const M4A_TYPE_NAMES: &[&str] = &[
74 "None", "AAC Main", "AAC LC", "AAC SSR", "AAC LTP", "SBR", "AAC Scalable", "TwinVQ", "CELP", "HVXC",
75 /*"(reserved10)", "(reserved11)", */ "TTSI",
76 "Main synthetic", "Wavetable synthesis", "General MIDI", "Algorithmic Synthesis and Audio FX",
77 "ER AAC LC", /*"(reserved18)",*/ "ER AAC LTP", "ER AAC Scalable", "ER TwinVQ", "ER BSAC", "ER AAC LD",
78 "ER CELP", "ER HVXC", "ER HILN", "ER Parametric", "SSC", "PS", "MPEG Surround", /*"(escape)",*/
79 "Layer-1", "Layer-2", "Layer-3", "DST", "ALS", "SLS", "SLS non-core", "ER AAC ELD", "SMR Simple", "SMR Main",
80 "(reserved)", "(unknown)",
81];
82
83impl fmt::Display for M4AType {
84 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
85 write!(f, "{}", M4A_TYPE_NAMES[*self as usize])
86 }
87}
88
89const AAC_SAMPLE_RATES: [u32; 16] = [
90 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
91 16000, 12000, 11025, 8000, 7350, 0, 0, 0
92];
93
94const AAC_CHANNELS: [usize; 8] = [ 0, 1, 2, 3, 4, 5, 6, 8 ];
95
96struct M4AInfo {
97 otype: M4AType,
98 srate: u32,
99 channels: usize,
100 samples: usize,
101 sbr_ps_info: Option<(u32, usize)>,
102 sbr_present: bool,
103 ps_present: bool,
104}
105
106impl M4AInfo {
107 fn new() -> Self {
108 Self {
109 otype: M4AType::None,
110 srate: 0,
111 channels: 0,
112 samples: 0,
113 sbr_ps_info: Option::None,
114 sbr_present: false,
115 ps_present: false,
116 }
117 }
118 fn read_object_type(br: &mut BitReader) -> DecoderResult<M4AType> {
119 let otypeidx;
120 if br.peek(5) == 31 {
121 br.skip(5)?;
122 otypeidx = (br.read(6)? as usize) + 32;
123 } else {
124 otypeidx = br.read(5)? as usize;
125 }
126 if otypeidx >= M4A_TYPES.len() {
127 Ok(M4AType::Unknown)
128 } else {
129 Ok(M4A_TYPES[otypeidx])
130 }
131 }
132 fn read_sampling_frequency(br: &mut BitReader) -> DecoderResult<u32> {
133 if br.peek(4) == 15 {
134 let srate = br.read(24)?;
135 Ok(srate)
136 } else {
137 let srate_idx = br.read(4)? as usize;
138 Ok(AAC_SAMPLE_RATES[srate_idx])
139 }
140 }
141 fn read_channel_config(br: &mut BitReader) -> DecoderResult<usize> {
142 let chidx = br.read(4)? as usize;
143 if chidx < AAC_CHANNELS.len() {
144 Ok(AAC_CHANNELS[chidx])
145 } else {
146 Ok(chidx)
147 }
148 }
149 fn read(&mut self, src: &[u8]) -> DecoderResult<()> {
fa90ccfb 150 let mut br = BitReader::new(src, BitReaderMode::BE);
5a932f64
KS
151 self.otype = Self::read_object_type(&mut br)?;
152 self.srate = Self::read_sampling_frequency(&mut br)?;
153 validate!(self.srate > 0);
154 self.channels = Self::read_channel_config(&mut br)?;
155
156 if (self.otype == M4AType::SBR) || (self.otype == M4AType::PS) {
157 let ext_srate = Self::read_sampling_frequency(&mut br)?;
158 self.otype = Self::read_object_type(&mut br)?;
159 let ext_chans;
160 if self.otype == M4AType::ER_BSAC {
161 ext_chans = Self::read_channel_config(&mut br)?;
162 } else {
163 ext_chans = 0;
164 }
165 self.sbr_ps_info = Some((ext_srate, ext_chans));
166 }
167
168 match self.otype {
169 M4AType::Main | M4AType::LC | M4AType::SSR | M4AType::Scalable | M4AType::TwinVQ |
170 M4AType::ER_AAC_LC | M4AType::ER_AAC_LTP | M4AType::ER_AAC_Scalable | M4AType::ER_TwinVQ |
171 M4AType::ER_BSAC | M4AType::ER_AAC_LD => {
172 // GASpecificConfig
173 let short_frame = br.read_bool()?;
174 self.samples = if short_frame { 960 } else { 1024 };
175 let depends_on_core = br.read_bool()?;
176 if depends_on_core {
177 let _delay = br.read(14)?;
178 }
179 let extension_flag = br.read_bool()?;
180 if self.channels == 0 {
181 unimplemented!("program config element");
182 }
183 if (self.otype == M4AType::Scalable) || (self.otype == M4AType::ER_AAC_Scalable) {
184 let _layer = br.read(3)?;
185 }
186 if extension_flag {
187 if self.otype == M4AType::ER_BSAC {
188 let _num_subframes = br.read(5)? as usize;
189 let _layer_length = br.read(11)?;
190 }
191 if (self.otype == M4AType::ER_AAC_LC) ||
192 (self.otype == M4AType::ER_AAC_LTP) ||
193 (self.otype == M4AType::ER_AAC_Scalable) ||
194 (self.otype == M4AType::ER_AAC_LD) {
195 let _section_data_resilience = br.read_bool()?;
196 let _scalefactors_resilience = br.read_bool()?;
197 let _spectral_data_resilience = br.read_bool()?;
198 }
199 let extension_flag3 = br.read_bool()?;
200 if extension_flag3 {
201 unimplemented!("version3 extensions");
202 }
203 }
204 },
205 M4AType::CELP => { unimplemented!("CELP config"); },
206 M4AType::HVXC => { unimplemented!("HVXC config"); },
207 M4AType::TTSI => { unimplemented!("TTS config"); },
208 M4AType::MainSynth | M4AType::WavetableSynth | M4AType::GeneralMIDI | M4AType::Algorithmic => { unimplemented!("structured audio config"); },
209 M4AType::ER_CELP => { unimplemented!("ER CELP config"); },
210 M4AType::ER_HVXC => { unimplemented!("ER HVXC config"); },
211 M4AType::ER_HILN | M4AType::ER_Parametric => { unimplemented!("parametric config"); },
212 M4AType::SSC => { unimplemented!("SSC config"); },
213 M4AType::MPEGSurround => {
214 br.skip(1)?; // sacPayloadEmbedding
215 unimplemented!("MPEG Surround config");
216 },
217 M4AType::Layer1 | M4AType::Layer2 | M4AType::Layer3 => { unimplemented!("MPEG Layer 1/2/3 config"); },
218 M4AType::DST => { unimplemented!("DST config"); },
219 M4AType::ALS => {
220 br.skip(5)?; // fillBits
221 unimplemented!("ALS config");
222 },
223 M4AType::SLS | M4AType::SLSNonCore => { unimplemented!("SLS config"); },
224 M4AType::ER_AAC_ELD => { unimplemented!("ELD config"); },
225 M4AType::SMRSimple | M4AType::SMRMain => { unimplemented!("symbolic music config"); },
226 _ => {},
227 };
228 match self.otype {
229 M4AType::ER_AAC_LC | M4AType::ER_AAC_LTP | M4AType::ER_AAC_Scalable | M4AType::ER_TwinVQ |
230 M4AType::ER_BSAC | M4AType::ER_AAC_LD | M4AType::ER_CELP | M4AType::ER_HVXC |
231 M4AType::ER_HILN | M4AType::ER_Parametric | M4AType::ER_AAC_ELD => {
232 let ep_config = br.read(2)?;
233 if (ep_config == 2) || (ep_config == 3) {
234 unimplemented!("error protection config");
235 }
236 if ep_config == 3 {
237 let direct_mapping = br.read_bool()?;
238 validate!(direct_mapping);
239 }
240 },
241 _ => {},
242 };
243 if self.sbr_ps_info.is_some() && (br.left() >= 16) {
244 let sync = br.read(11)?;
245 if sync == 0x2B7 {
246 let ext_otype = Self::read_object_type(&mut br)?;
247 if ext_otype == M4AType::SBR {
248 self.sbr_present = br.read_bool()?;
249 if self.sbr_present {
250 let _ext_srate = Self::read_sampling_frequency(&mut br)?;
251 if br.left() >= 12 {
252 let sync = br.read(11)?;
253 if sync == 0x548 {
254 self.ps_present = br.read_bool()?;
255 }
256 }
257 }
258 }
259 if ext_otype == M4AType::PS {
260 self.sbr_present = br.read_bool()?;
261 if self.sbr_present {
262 let _ext_srate = Self::read_sampling_frequency(&mut br)?;
263 }
264 let _ext_channels = br.read(4)?;
265 }
266 }
267 }
268
269 Ok(())
270 }
271}
272
273impl fmt::Display for M4AInfo {
274 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
275 write!(f, "MPEG 4 Audio {}, {} Hz, {} channels, {} samples per frame",
276 self.otype, self.srate, self.channels, self.samples)
277 }
278}
279
280const MAX_WINDOWS: usize = 8;
281const MAX_SFBS: usize = 64;
282
283#[derive(Clone,Copy)]
284struct ICSInfo {
285 window_sequence: u8,
286 prev_window_sequence: u8,
287 window_shape: bool,
288 prev_window_shape: bool,
289 scale_factor_grouping: [bool; MAX_WINDOWS],
290 group_start: [usize; MAX_WINDOWS],
291 window_groups: usize,
292 num_windows: usize,
293 max_sfb: usize,
294 predictor_data: Option<LTPData>,
295 long_win: bool,
296}
297
298const ONLY_LONG_SEQUENCE: u8 = 0;
299const LONG_START_SEQUENCE: u8 = 1;
300const EIGHT_SHORT_SEQUENCE: u8 = 2;
301const LONG_STOP_SEQUENCE: u8 = 3;
302
303impl ICSInfo {
304 fn new() -> Self {
305 Self {
306 window_sequence: 0,
307 prev_window_sequence: 0,
308 window_shape: false,
309 prev_window_shape: false,
310 scale_factor_grouping: [false; MAX_WINDOWS],
311 group_start: [0; MAX_WINDOWS],
312 num_windows: 0,
313 window_groups: 0,
314 max_sfb: 0,
315 predictor_data: None,
316 long_win: true,
317 }
318 }
319 fn decode_ics_info(&mut self, br: &mut BitReader) -> DecoderResult<()> {
320 self.prev_window_sequence = self.window_sequence;
321 self.prev_window_shape = self.window_shape;
322 let ics_reserved_bit = br.read(1)?;
323 validate!(ics_reserved_bit == 0);
324 self.window_sequence = br.read(2)? as u8;
325 match self.prev_window_sequence {
326 ONLY_LONG_SEQUENCE | LONG_STOP_SEQUENCE => {
327 validate!((self.window_sequence == ONLY_LONG_SEQUENCE) ||
328 (self.window_sequence == LONG_START_SEQUENCE));
329 },
330 LONG_START_SEQUENCE | EIGHT_SHORT_SEQUENCE => {
331 validate!((self.window_sequence == EIGHT_SHORT_SEQUENCE) ||
332 (self.window_sequence == LONG_STOP_SEQUENCE));
333 },
334 _ => {},
335 };
336 self.window_shape = br.read_bool()?;
337 self.window_groups = 1;
338 if self.window_sequence == EIGHT_SHORT_SEQUENCE {
339 self.long_win = false;
340 self.num_windows = 8;
341 self.max_sfb = br.read(4)? as usize;
342 for i in 0..MAX_WINDOWS-1 {
343 self.scale_factor_grouping[i] = br.read_bool()?;
344 if !self.scale_factor_grouping[i] {
345 self.group_start[self.window_groups] = i + 1;
346 self.window_groups += 1;
347 }
348 }
349 } else {
350 self.long_win = true;
351 self.num_windows = 1;
352 self.max_sfb = br.read(6)? as usize;
353 self.predictor_data = LTPData::read(br)?;
354 }
355 Ok(())
356 }
357 fn get_group_start(&self, g: usize) -> usize {
358 if g == 0 {
359 0
360 } else if g >= self.window_groups {
361 if self.long_win { 1 } else { 8 }
362 } else {
363 self.group_start[g]
364 }
365 }
366}
367
368#[derive(Clone,Copy)]
369struct LTPData {
370}
371
372impl LTPData {
373 fn read(br: &mut BitReader) -> DecoderResult<Option<Self>> {
374 let predictor_data_present = br.read_bool()?;
375 if !predictor_data_present { return Ok(None); }
376unimplemented!("predictor data");
377/*
378 if is_main {
379 let predictor_reset = br.read_bool()?;
380 if predictor_reset {
381 let predictor_reset_group_number = br.read(5)?;
382 }
383 for sfb in 0..max_sfb.min(PRED_SFB_MAX) {
384 prediction_used[sfb] = br.read_bool()?;
385 }
386 } else {
387 let ltp_data_present = br.read_bool()?;
388 if ltp_data_present {
389 //ltp data
390 }
391 if common_window {
392 let ltp_data_present = br.read_bool()?;
393 if ltp_data_present {
394 //ltp data
395 }
190f8cf0 396 }
5a932f64
KS
397 }
398 Ok(Some(Self { }))
399*/
400 }
401}
402
403#[derive(Clone,Copy)]
404#[allow(dead_code)]
405struct PulseData {
406 number_pulse: usize,
407 pulse_start_sfb: usize,
408 pulse_offset: [u8; 4],
409 pulse_amp: [u8; 4],
410}
411
412impl PulseData {
413 fn read(br: &mut BitReader) -> DecoderResult<Option<Self>> {
414 let pulse_data_present = br.read_bool()?;
415 if !pulse_data_present { return Ok(None); }
416
417 let number_pulse = (br.read(2)? as usize) + 1;
418 let pulse_start_sfb = br.read(6)? as usize;
419 let mut pulse_offset: [u8; 4] = [0; 4];
420 let mut pulse_amp: [u8; 4] = [0; 4];
421 for i in 0..number_pulse {
422 pulse_offset[i] = br.read(5)? as u8;
423 pulse_amp[i] = br.read(4)? as u8;
424 }
425 Ok(Some(Self{ number_pulse, pulse_start_sfb, pulse_offset, pulse_amp }))
426 }
427}
428
429const TNS_MAX_ORDER: usize = 20;
430const TNS_MAX_LONG_BANDS: [usize; 12] = [ 31, 31, 34, 40, 42, 51, 46, 46, 42, 42, 42, 39 ];
431const TNS_MAX_SHORT_BANDS: [usize; 12] = [ 9, 9, 10, 14, 14, 14, 14, 14, 14, 14, 14, 14 ];
432
433#[derive(Clone,Copy)]
434struct TNSCoeffs {
435 length: usize,
436 order: usize,
437 direction: bool,
438 compress: bool,
439 coef: [f32; TNS_MAX_ORDER + 1],
440}
441
442impl TNSCoeffs {
443 fn new() -> Self {
444 Self {
445 length: 0, order: 0, direction: false, compress: false, coef: [0.0; TNS_MAX_ORDER + 1],
446 }
447 }
448 fn read(&mut self, br: &mut BitReader, long_win: bool, coef_res: bool, max_order: usize) -> DecoderResult<()> {
449 self.length = br.read(if long_win { 6 } else { 4 })? as usize;
450 self.order = br.read(if long_win { 5 } else { 3 })? as usize;
451 validate!(self.order <= max_order);
452 if self.order > 0 {
453 self.direction = br.read_bool()?;
454 self.compress = br.read_bool()?;
455 let mut coef_bits = 3;
456 if coef_res { coef_bits += 1; }
457 if self.compress { coef_bits -= 1; }
458 let sign_mask = 1 << (coef_bits - 1);
459 let neg_mask = !(sign_mask * 2 - 1);
460
461 let fac_base = if coef_res { 1 << 3 } else { 1 << 2 } as f32;
462 let iqfac = (fac_base - 0.5) / (consts::PI / 2.0);
463 let iqfac_m = (fac_base + 0.5) / (consts::PI / 2.0);
464 let mut tmp: [f32; TNS_MAX_ORDER] = [0.0; TNS_MAX_ORDER];
c83013a1 465 for el in tmp.iter_mut().take(self.order) {
5a932f64 466 let val = br.read(coef_bits)? as i8;
c83013a1
KS
467 let c = f32::from(if (val & sign_mask) != 0 { val | neg_mask } else { val });
468 *el = (if c >= 0.0 { c / iqfac } else { c / iqfac_m }).sin();
5a932f64
KS
469 }
470 // convert to LPC coefficients
471 let mut b: [f32; TNS_MAX_ORDER + 1] = [0.0; TNS_MAX_ORDER + 1];
c83013a1 472 for m in 1..=self.order {
5a932f64
KS
473 for i in 1..m {
474 b[i] = self.coef[i - 1] + tmp[m - 1] * self.coef[m - i - 1];
475 }
476 for i in 1..m {
477 self.coef[i - 1] = b[i];
478 }
479 self.coef[m - 1] = tmp[m - 1];
480 }
481 }
482 Ok(())
483 }
484}
485
486#[derive(Clone,Copy)]
487#[allow(dead_code)]
488struct TNSData {
489 n_filt: [usize; MAX_WINDOWS],
490 coef_res: [bool; MAX_WINDOWS],
491 coeffs: [[TNSCoeffs; 4]; MAX_WINDOWS],
492}
493
494impl TNSData {
495 fn read(br: &mut BitReader, long_win: bool, num_windows: usize, max_order: usize) -> DecoderResult<Option<Self>> {
496 let tns_data_present = br.read_bool()?;
497 if !tns_data_present { return Ok(None); }
498 let mut n_filt: [usize; MAX_WINDOWS] = [0; MAX_WINDOWS];
499 let mut coef_res: [bool; MAX_WINDOWS] = [false; MAX_WINDOWS];
500 let mut coeffs: [[TNSCoeffs; 4]; MAX_WINDOWS] = [[TNSCoeffs::new(); 4]; MAX_WINDOWS];
501 for w in 0..num_windows {
502 n_filt[w] = br.read(if long_win { 2 } else { 1 })? as usize;
503 if n_filt[w] != 0 {
504 coef_res[w] = br.read_bool()?;
505 }
506 for filt in 0..n_filt[w] {
507 coeffs[w][filt].read(br, long_win, coef_res[w], max_order)?;
508 }
509 }
510 Ok(Some(Self { n_filt, coef_res, coeffs }))
511 }
512}
513
514#[derive(Clone,Copy)]
515#[allow(dead_code)]
516struct GainControlData {
517 max_band: u8,
518}
519
520impl GainControlData {
521 fn read(br: &mut BitReader) -> DecoderResult<Option<Self>> {
522 let gain_control_data_present = br.read_bool()?;
523 if !gain_control_data_present { return Ok(None); }
524unimplemented!("gain control data");
525/* self.max_band = br.read(2)? as u8;
526 if window_sequence == ONLY_LONG_SEQUENCE {
527 for bd in 0..max_band
528...
529 }
530 Ok(Some(Self { }))*/
531 }
532}
533
534const ZERO_HCB: u8 = 0;
535const FIRST_PAIR_HCB: u8 = 5;
536const ESC_HCB: u8 = 11;
537const RESERVED_HCB: u8 = 12;
538const NOISE_HCB: u8 = 13;
539const INTENSITY_HCB2: u8 = 14;
540const INTENSITY_HCB: u8 = 15;
541
542struct Codebooks {
543 scale_cb: Codebook<i8>,
544 spec_cb: [Codebook<u16>; 11],
545}
546
547fn scale_map(idx: usize) -> i8 { (idx as i8) - 60 }
548fn cb_map(idx: usize) -> u16 { idx as u16 }
549
550impl Codebooks {
551 fn new() -> Self {
552 let mut coderead = TableCodebookDescReader::new(AAC_SCF_CODEBOOK_CODES, AAC_SCF_CODEBOOK_BITS, scale_map);
553 let scale_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap();
554 let mut spec_cb: [Codebook<u16>; 11];
555 unsafe {
556 spec_cb = mem::uninitialized();
557 for i in 0..AAC_SPEC_CODES.len() {
558 let mut coderead = TableCodebookDescReader::new(AAC_SPEC_CODES[i], AAC_SPEC_BITS[i], cb_map);
559 ptr::write(&mut spec_cb[i], Codebook::new(&mut coderead, CodebookMode::MSB).unwrap());
560 }
561 }
562 Self { scale_cb, spec_cb }
563 }
564}
565
566#[derive(Clone)]
567struct ICS {
568 global_gain: u8,
569 info: ICSInfo,
570 pulse_data: Option<PulseData>,
571 tns_data: Option<TNSData>,
572 gain_control: Option<GainControlData>,
573 sect_cb: [[u8; MAX_SFBS]; MAX_WINDOWS],
574 sect_len: [[usize; MAX_SFBS]; MAX_WINDOWS],
575 sfb_cb: [[u8; MAX_SFBS]; MAX_WINDOWS],
576 num_sec: [usize; MAX_WINDOWS],
577 scales: [[u8; MAX_SFBS]; MAX_WINDOWS],
578 sbinfo: GASubbandInfo,
579 coeffs: [f32; 1024],
580 delay: [f32; 1024],
581}
582
583const INTENSITY_SCALE_MIN: i16 = -155;
584const NOISE_SCALE_MIN: i16 = -100;
585impl ICS {
586 fn new(sbinfo: GASubbandInfo) -> Self {
587 Self {
588 global_gain: 0,
589 info: ICSInfo::new(),
590 pulse_data: None,
591 tns_data: None,
592 gain_control: None,
593 sect_cb: [[0; MAX_SFBS]; MAX_WINDOWS],
594 sect_len: [[0; MAX_SFBS]; MAX_WINDOWS],
595 sfb_cb: [[0; MAX_SFBS]; MAX_WINDOWS],
596 scales: [[0; MAX_SFBS]; MAX_WINDOWS],
597 num_sec: [0; MAX_WINDOWS],
598 sbinfo,
599 coeffs: [0.0; 1024],
600 delay: [0.0; 1024],
601 }
602 }
603 fn decode_section_data(&mut self, br: &mut BitReader, may_have_intensity: bool) -> DecoderResult<()> {
604 let sect_bits = if self.info.long_win { 5 } else { 3 };
605 let sect_esc_val = (1 << sect_bits) - 1;
606
607 for g in 0..self.info.window_groups {
608 let mut k = 0;
609 let mut l = 0;
610 while k < self.info.max_sfb {
611 self.sect_cb[g][l] = br.read(4)? as u8;
612 self.sect_len[g][l] = 0;
613 validate!(self.sect_cb[g][l] != RESERVED_HCB);
614 if ((self.sect_cb[g][l] == INTENSITY_HCB) || (self.sect_cb[g][l] == INTENSITY_HCB2)) && !may_have_intensity {
615 return Err(DecoderError::InvalidData);
616 }
617 loop {
618 let sect_len_incr = br.read(sect_bits)? as usize;
619 self.sect_len[g][l] += sect_len_incr;
620 if sect_len_incr < sect_esc_val { break; }
621 }
622 validate!(k + self.sect_len[g][l] <= self.info.max_sfb);
623 for _ in 0..self.sect_len[g][l] {
624 self.sfb_cb[g][k] = self.sect_cb[g][l];
625 k += 1;
626 }
627 l += 1;
628 }
629 self.num_sec[g] = l;
630 }
631 Ok(())
632 }
633 fn is_intensity(&self, g: usize, sfb: usize) -> bool {
634 (self.sfb_cb[g][sfb] == INTENSITY_HCB) || (self.sfb_cb[g][sfb] == INTENSITY_HCB2)
635 }
636 fn get_intensity_dir(&self, g: usize, sfb: usize) -> bool {
637 self.sfb_cb[g][sfb] == INTENSITY_HCB
638 }
639 fn decode_scale_factor_data(&mut self, br: &mut BitReader, codebooks: &Codebooks) -> DecoderResult<()> {
640 let mut noise_pcm_flag = true;
c83013a1 641 let mut scf_normal = i16::from(self.global_gain);
5a932f64
KS
642 let mut scf_intensity = 0i16;
643 let mut scf_noise = 0i16;
644 for g in 0..self.info.window_groups {
645 for sfb in 0..self.info.max_sfb {
646 if self.sfb_cb[g][sfb] != ZERO_HCB {
647 if self.is_intensity(g, sfb) {
c83013a1 648 let diff = i16::from(br.read_cb(&codebooks.scale_cb)?);
5a932f64
KS
649 scf_intensity += diff;
650 validate!((scf_intensity >= INTENSITY_SCALE_MIN) && (scf_intensity < INTENSITY_SCALE_MIN + 256));
651 self.scales[g][sfb] = (scf_intensity - INTENSITY_SCALE_MIN) as u8;
c83013a1
KS
652 } else if self.sfb_cb[g][sfb] == NOISE_HCB {
653 if noise_pcm_flag {
654 noise_pcm_flag = false;
655 scf_noise = (br.read(9)? as i16) - 256 + i16::from(self.global_gain) - 90;
5a932f64 656 } else {
c83013a1 657 scf_noise += i16::from(br.read_cb(&codebooks.scale_cb)?);
5a932f64 658 }
c83013a1
KS
659 validate!((scf_noise >= NOISE_SCALE_MIN) && (scf_noise < NOISE_SCALE_MIN + 256));
660 self.scales[g][sfb] = (scf_noise - NOISE_SCALE_MIN) as u8;
661 } else {
662 scf_normal += i16::from(br.read_cb(&codebooks.scale_cb)?);
663 validate!((scf_normal >= 0) && (scf_normal < 255));
664 self.scales[g][sfb] = scf_normal as u8;
5a932f64
KS
665 }
666 }
667 }
668 }
669 Ok(())
670 }
671 fn get_band_start(&self, swb: usize) -> usize {
672 if self.info.long_win {
673 self.sbinfo.long_bands[swb]
674 } else {
675 self.sbinfo.short_bands[swb]
676 }
677 }
678 fn get_num_bands(&self) -> usize {
679 if self.info.long_win {
680 self.sbinfo.long_bands.len() - 1
681 } else {
682 self.sbinfo.short_bands.len() - 1
683 }
684 }
685 fn decode_spectrum(&mut self, br: &mut BitReader, codebooks: &Codebooks) -> DecoderResult<()> {
686 self.coeffs = [0.0; 1024];
687 for g in 0..self.info.window_groups {
688 let cur_w = self.info.get_group_start(g);
689 let next_w = self.info.get_group_start(g + 1);
690 for sfb in 0..self.info.max_sfb {
691 let start = self.get_band_start(sfb);
692 let end = self.get_band_start(sfb + 1);
693 let cb_idx = self.sfb_cb[g][sfb];
694 for w in cur_w..next_w {
695 let dst = &mut self.coeffs[start + w*128..end + w*128];
696 match cb_idx {
697 ZERO_HCB => { /* zeroes */ },
698 NOISE_HCB => { /* noise */ },
699 INTENSITY_HCB | INTENSITY_HCB2 => { /* intensity */ },
700 _ => {
701 let unsigned = AAC_UNSIGNED_CODEBOOK[(cb_idx - 1) as usize];
702 let scale = get_scale(self.scales[g][sfb]);
703 let cb = &codebooks.spec_cb[(cb_idx - 1) as usize];
704 if cb_idx < FIRST_PAIR_HCB {
705 decode_quads(br, cb, unsigned, scale, dst)?;
706 } else {
707 decode_pairs(br, cb, unsigned, cb_idx == ESC_HCB,
708 AAC_CODEBOOK_MODULO[(cb_idx - FIRST_PAIR_HCB) as usize], scale, dst)?;
709 }
710 },
711 };
712 }
713 }
714 }
715 Ok(())
716 }
717 fn place_pulses(&mut self) {
718 if let Some(ref pdata) = self.pulse_data {
719 if pdata.pulse_start_sfb >= self.sbinfo.long_bands.len() - 1 { return; }
720 let mut k = self.get_band_start(pdata.pulse_start_sfb);
721 let mut band = pdata.pulse_start_sfb;
722 for pno in 0..pdata.number_pulse {
723 k += pdata.pulse_offset[pno] as usize;
724 if k >= 1024 { return; }
725 while self.get_band_start(band + 1) <= k { band += 1; }
726 let scale = get_scale(self.scales[0][band]);
727 let mut base = self.coeffs[k];
728 if base != 0.0 {
729 base = requant(self.coeffs[k], scale);
730 }
731 if base > 0.0 {
c83013a1 732 base += f32::from(pdata.pulse_amp[pno]);
5a932f64 733 } else {
c83013a1 734 base -= f32::from(pdata.pulse_amp[pno]);
5a932f64
KS
735 }
736 self.coeffs[k] = iquant(base) * scale;
737 }
738 }
739 }
740 fn decode_ics(&mut self, br: &mut BitReader, codebooks: &Codebooks, m4atype: M4AType, common_window: bool, may_have_intensity: bool) -> DecoderResult<()> {
741 self.global_gain = br.read(8)? as u8;
742 if !common_window {
743 self.info.decode_ics_info(br)?;
744 }
745 self.decode_section_data(br, may_have_intensity)?;
746 self.decode_scale_factor_data(br, codebooks)?;
747 self.pulse_data = PulseData::read(br)?;
748 validate!(self.pulse_data.is_none() || self.info.long_win);
749 let tns_max_order;
750 if !self.info.long_win {
751 tns_max_order = 7;
752 } else if m4atype == M4AType::LC {
753 tns_max_order = 12;
754 } else {
755 tns_max_order = TNS_MAX_ORDER;
756 }
757 self.tns_data = TNSData::read(br, self.info.long_win, self.info.num_windows, tns_max_order)?;
758 if m4atype == M4AType::SSR {
759 self.gain_control = GainControlData::read(br)?;
760 } else {
761 let gain_control_data_present = br.read_bool()?;
762 validate!(!gain_control_data_present);
763 }
764 self.decode_spectrum(br, codebooks)?;
765 Ok(())
766 }
767 fn synth_channel(&mut self, dsp: &mut DSP, dst: &mut [f32], srate_idx: usize) {
768 self.place_pulses();
769 if let Some(ref tns_data) = self.tns_data {
770 let tns_max_bands = (if self.info.long_win {
771 TNS_MAX_LONG_BANDS[srate_idx]
772 } else {
773 TNS_MAX_SHORT_BANDS[srate_idx]
774 }).min(self.info.max_sfb);
775 for w in 0..self.info.num_windows {
776 let mut bottom = self.get_num_bands();
777 for f in 0..tns_data.n_filt[w] {
778 let top = bottom;
779 bottom = if top >= tns_data.coeffs[w][f].length { top - tns_data.coeffs[w][f].length } else { 0 };
780 let order = tns_data.coeffs[w][f].order;
781 if order == 0 { continue; }
782 let start = w * 128 + self.get_band_start(tns_max_bands.min(bottom));
783 let end = w * 128 + self.get_band_start(tns_max_bands.min(top));
784 let lpc = &tns_data.coeffs[w][f].coef;
785 if !tns_data.coeffs[w][f].direction {
786 for m in start..end {
787 for i in 0..order.min(m) {
788 self.coeffs[m] -= self.coeffs[m - i - 1] * lpc[i];
789 }
790 }
791 } else {
792 for m in (start..end).rev() {
793 for i in 0..order.min(m) {
794 self.coeffs[m] -= self.coeffs[m + i - 1] * lpc[i];
795 }
796 }
797 }
798 }
799 }
800 }
801 dsp.synth(&self.coeffs, &mut self.delay, self.info.window_sequence, self.info.window_shape, self.info.prev_window_shape, dst);
802 }
803}
804
805fn get_scale(scale: u8) -> f32 {
c83013a1 806 2.0f32.powf(0.25 * (f32::from(scale) - 100.0 - 56.0))
5a932f64
KS
807}
808fn iquant(val: f32) -> f32 {
809 if val < 0.0 {
810 -((-val).powf(4.0 / 3.0))
811 } else {
812 val.powf(4.0 / 3.0)
813 }
814}
815fn requant(val: f32, scale: f32) -> f32 {
816 if scale == 0.0 { return 0.0; }
817 let bval = val / scale;
818 if bval >= 0.0 {
819 val.powf(3.0 / 4.0)
820 } else {
821 -((-val).powf(3.0 / 4.0))
822 }
823}
824fn decode_quads(br: &mut BitReader, cb: &Codebook<u16>, unsigned: bool, scale: f32, dst: &mut [f32]) -> DecoderResult<()> {
825 for out in dst.chunks_mut(4) {
826 let cw = br.read_cb(cb)? as usize;
827 if unsigned {
828 for i in 0..4 {
829 let val = AAC_QUADS[cw][i];
830 if val != 0 {
831 if br.read_bool()? {
c83013a1 832 out[i] = iquant(-f32::from(val)) * scale;
5a932f64 833 } else {
c83013a1 834 out[i] = iquant( f32::from(val)) * scale;
5a932f64
KS
835 }
836 }
837 }
838 } else {
839 for i in 0..4 {
c83013a1 840 out[i] = iquant(f32::from(AAC_QUADS[cw][i] - 1)) * scale;
5a932f64
KS
841 }
842 }
843 }
844 Ok(())
845}
846fn decode_pairs(br: &mut BitReader, cb: &Codebook<u16>, unsigned: bool, escape: bool, modulo: u16, scale: f32, dst: &mut [f32]) -> DecoderResult<()> {
847 for out in dst.chunks_mut(2) {
848 let cw = br.read_cb(cb)?;
849 let mut x = (cw / modulo) as i16;
850 let mut y = (cw % modulo) as i16;
851 if unsigned {
852 if x != 0 && br.read_bool()? {
853 x = -x;
854 }
855 if y != 0 && br.read_bool()? {
856 y = -y;
857 }
858 } else {
859 x -= (modulo >> 1) as i16;
860 y -= (modulo >> 1) as i16;
861 }
862 if escape {
863 if (x == 16) || (x == -16) {
864 x += read_escape(br, x > 0)?;
865 }
866 if (y == 16) || (y == -16) {
867 y += read_escape(br, y > 0)?;
868 }
869 }
c83013a1
KS
870 out[0] = iquant(f32::from(x)) * scale;
871 out[1] = iquant(f32::from(y)) * scale;
5a932f64
KS
872 }
873 Ok(())
874}
875fn read_escape(br: &mut BitReader, sign: bool) -> DecoderResult<i16> {
876 let prefix = br.read_code(UintCodeType::UnaryOnes)? as u8;
877 validate!(prefix < 9);
878 let bits = br.read(prefix + 4)? as i16;
879 if sign {
880 Ok(bits)
881 } else {
882 Ok(-bits)
883 }
884}
885
886#[derive(Clone)]
887struct ChannelPair {
888 pair: bool,
889 channel: usize,
890 common_window: bool,
891 ms_mask_present: u8,
892 ms_used: [[bool; MAX_SFBS]; MAX_WINDOWS],
893 ics: [ICS; 2],
894}
895
896impl ChannelPair {
897 fn new(pair: bool, channel: usize, sbinfo: GASubbandInfo) -> Self {
898 Self {
899 pair, channel,
900 common_window: false,
901 ms_mask_present: 0,
902 ms_used: [[false; MAX_SFBS]; MAX_WINDOWS],
903 ics: [ICS::new(sbinfo), ICS::new(sbinfo)],
904 }
905 }
906 fn decode_ga_sce(&mut self, br: &mut BitReader, codebooks: &Codebooks, m4atype: M4AType) -> DecoderResult<()> {
907 self.ics[0].decode_ics(br, codebooks, m4atype, false, false)?;
908 Ok(())
909 }
910 fn decode_ga_cpe(&mut self, br: &mut BitReader, codebooks: &Codebooks, m4atype: M4AType) -> DecoderResult<()> {
911 let common_window = br.read_bool()?;
912 self.common_window = common_window;
913 if common_window {
914 self.ics[0].info.decode_ics_info(br)?;
915 self.ms_mask_present = br.read(2)? as u8;
916 validate!(self.ms_mask_present != 3);
917 if self.ms_mask_present == 1 {
918 for g in 0..self.ics[0].info.window_groups {
919 for sfb in 0..self.ics[0].info.max_sfb {
920 self.ms_used[g][sfb] = br.read_bool()?;
921 }
922 }
923 }
924 self.ics[1].info = self.ics[0].info;
925 }
926 self.ics[0].decode_ics(br, codebooks, m4atype, common_window, true)?;
927 self.ics[1].decode_ics(br, codebooks, m4atype, common_window, false)?;
928 if common_window && self.ms_mask_present != 0 {
929 let mut g = 0;
930 for w in 0..self.ics[0].info.num_windows {
931 if w > 0 && self.ics[0].info.scale_factor_grouping[w - 1] {
932 g += 1;
933 }
934 for sfb in 0..self.ics[0].info.max_sfb {
935 let start = w * 128 + self.ics[0].get_band_start(sfb);
936 let end = w * 128 + self.ics[0].get_band_start(sfb + 1);
937 if self.ics[0].is_intensity(g, sfb) {
938 let invert = (self.ms_mask_present == 1) && self.ms_used[g][sfb];
939 let dir = self.ics[0].get_intensity_dir(g, sfb) ^ invert;
c83013a1 940 let scale = 0.5f32.powf(0.25 * (f32::from(self.ics[0].scales[g][sfb]) + f32::from(INTENSITY_SCALE_MIN)));
5a932f64
KS
941 if !dir {
942 for i in start..end {
943 self.ics[1].coeffs[i] = scale * self.ics[0].coeffs[i];
944 }
945 } else {
946 for i in start..end {
947 self.ics[1].coeffs[i] = -scale * self.ics[0].coeffs[i];
948 }
949 }
950 } else if (self.ms_mask_present == 2) || self.ms_used[g][sfb] {
951 for i in start..end {
952 let tmp = self.ics[0].coeffs[i] - self.ics[1].coeffs[i];
953 self.ics[0].coeffs[i] += self.ics[1].coeffs[i];
954 self.ics[1].coeffs[i] = tmp;
955 }
956 }
957 }
958 }
959 }
960 Ok(())
961 }
962 fn synth_audio(&mut self, dsp: &mut DSP, abuf: &mut NABufferType, srate_idx: usize) {
963 let mut adata = abuf.get_abuf_f32().unwrap();
1a967e6b 964 let output = adata.get_data_mut().unwrap();
5a932f64
KS
965 let off0 = abuf.get_offset(self.channel);
966 let off1 = abuf.get_offset(self.channel + 1);
967 self.ics[0].synth_channel(dsp, &mut output[off0..], srate_idx);
968 if self.pair {
969 self.ics[1].synth_channel(dsp, &mut output[off1..], srate_idx);
970 }
971 }
972}
973
974struct DSP {
975 kbd_long_win: [f32; 1024],
976 kbd_short_win: [f32; 128],
977 sine_long_win: [f32; 1024],
978 sine_short_win: [f32; 128],
979 imdct_long: IMDCT,
980 imdct_short: IMDCT,
981 tmp: [f32; 2048],
982 ew_buf: [f32; 1152],
983}
984
985const SHORT_WIN_POINT0: usize = 512 - 64;
986const SHORT_WIN_POINT1: usize = 512 + 64;
987
988impl DSP {
989 fn new() -> Self {
990 let mut kbd_long_win: [f32; 1024] = [0.0; 1024];
991 let mut kbd_short_win: [f32; 128] = [0.0; 128];
992 generate_window(WindowType::KaiserBessel(4.0), 1.0, 1024, true, &mut kbd_long_win);
993 generate_window(WindowType::KaiserBessel(6.0), 1.0, 128, true, &mut kbd_short_win);
994 let mut sine_long_win: [f32; 1024] = [0.0; 1024];
995 let mut sine_short_win: [f32; 128] = [0.0; 128];
996 generate_window(WindowType::Sine, 1.0, 1024, true, &mut sine_long_win);
997 generate_window(WindowType::Sine, 1.0, 128, true, &mut sine_short_win);
998 Self {
999 kbd_long_win, kbd_short_win,
1000 sine_long_win, sine_short_win,
9e78289c
KS
1001 imdct_long: IMDCT::new(1024 * 2, true),
1002 imdct_short: IMDCT::new(128 * 2, true),
5a932f64
KS
1003 tmp: [0.0; 2048], ew_buf: [0.0; 1152],
1004 }
1005 }
1006 fn synth(&mut self, coeffs: &[f32; 1024], delay: &mut [f32; 1024], seq: u8, window_shape: bool, prev_window_shape: bool, dst: &mut [f32]) {
1007 let long_win = if window_shape { &self.kbd_long_win } else { &self.sine_long_win };
1008 let short_win = if window_shape { &self.kbd_short_win } else { &self.sine_short_win };
1009 let left_long_win = if prev_window_shape { &self.kbd_long_win } else { &self.sine_long_win };
1010 let left_short_win = if prev_window_shape { &self.kbd_short_win } else { &self.sine_short_win };
1011 if seq != EIGHT_SHORT_SEQUENCE {
1012 self.imdct_long.imdct(coeffs, &mut self.tmp);
1013 } else {
1014 for (ain, aout) in coeffs.chunks(128).zip(self.tmp.chunks_mut(256)) {
1015 self.imdct_short.imdct(ain, aout);
1016 }
1017 self.ew_buf = [0.0; 1152];
1018 for (w, src) in self.tmp.chunks(256).enumerate() {
1019 if w > 0 {
1020 for i in 0..128 {
1021 self.ew_buf[w * 128 + i] += src[i] * short_win[i];
1022 }
1023 } else { // to be left-windowed
1024 for i in 0..128 {
1025 self.ew_buf[i] = src[i];
1026 }
1027 }
1028 for i in 0..128 {
1029 self.ew_buf[w * 128 + i + 128] += src[i + 128] * short_win[127 - i];
1030 }
1031 }
1032 }
1033 if seq == ONLY_LONG_SEQUENCE { // should be the most common case
1034 for i in 0..1024 {
1035 dst[i] = delay[i] + self.tmp[i] * left_long_win[i];
1036 delay[i] = self.tmp[i + 1024] * long_win[1023 - i];
1037 }
1038 return;
1039 }
1040 // output new data
1041 match seq {
1042 ONLY_LONG_SEQUENCE | LONG_START_SEQUENCE => {
1043 for i in 0..1024 {
1044 dst[i] = self.tmp[i] * left_long_win[i] + delay[i];
1045 }
1046 },
1047 EIGHT_SHORT_SEQUENCE => {
1048 for i in 0..SHORT_WIN_POINT0 {
1049 dst[i] = delay[i];
1050 }
1051 for i in SHORT_WIN_POINT0..SHORT_WIN_POINT1 {
1052 let j = i - SHORT_WIN_POINT0;
1053 dst[i] = delay[i] + self.ew_buf[j] * left_short_win[j];
1054 }
1055 for i in SHORT_WIN_POINT1..1024 {
1056 let j = i - SHORT_WIN_POINT0;
1057 dst[i] = self.ew_buf[j];
1058 }
1059 },
1060 LONG_STOP_SEQUENCE => {
1061 for i in 0..SHORT_WIN_POINT0 {
1062 dst[i] = delay[i];
1063 }
1064 for i in SHORT_WIN_POINT0..SHORT_WIN_POINT1 {
1065 dst[i] = delay[i] + self.tmp[i] * left_short_win[i - SHORT_WIN_POINT0];
1066 }
1067 for i in SHORT_WIN_POINT1..1024 {
1068 dst[i] = self.tmp[i];
1069 }
1070 },
1071 _ => unreachable!(""),
1072 };
1073 // save delay
1074 match seq {
1075 ONLY_LONG_SEQUENCE | LONG_STOP_SEQUENCE => {
1076 for i in 0..1024 {
1077 delay[i] = self.tmp[i + 1024] * long_win[1023 - i];
1078 }
1079 },
1080 EIGHT_SHORT_SEQUENCE => {
1081 for i in 0..SHORT_WIN_POINT1 { // last part is already windowed
1082 delay[i] = self.ew_buf[i + 512+64];
1083 }
1084 for i in SHORT_WIN_POINT1..1024 {
1085 delay[i] = 0.0;
1086 }
1087 },
1088 LONG_START_SEQUENCE => {
1089 for i in 0..SHORT_WIN_POINT0 {
1090 delay[i] = self.tmp[i + 1024];
1091 }
1092 for i in SHORT_WIN_POINT0..SHORT_WIN_POINT1 {
1093 delay[i] = self.tmp[i + 1024] * short_win[127 - (i - SHORT_WIN_POINT0)];
1094 }
1095 for i in SHORT_WIN_POINT1..1024 {
1096 delay[i] = 0.0;
1097 }
1098 },
1099 _ => unreachable!(""),
1100 };
1101 }
1102}
1103
1104struct AACDecoder {
2422d969 1105 info: NACodecInfoRef,
5a932f64
KS
1106 chmap: NAChannelMap,
1107 m4ainfo: M4AInfo,
1108 pairs: Vec<ChannelPair>,
1109 codebooks: Codebooks,
1110 dsp: DSP,
1111 sbinfo: GASubbandInfo,
1112}
1113
1114impl AACDecoder {
1115 fn new() -> Self {
1116 AACDecoder {
1117 info: NACodecInfo::new_dummy(),
1118 chmap: NAChannelMap::new(),
1119 m4ainfo: M4AInfo::new(),
1120 pairs: Vec::new(),
1121 codebooks: Codebooks::new(),
1122 dsp: DSP::new(),
1123 sbinfo: AAC_SUBBAND_INFO[0],
1124 }
1125 }
1126 fn set_pair(&mut self, pair_no: usize, channel: usize, pair: bool) -> DecoderResult<()> {
1127 if self.pairs.len() <= pair_no {
1128 self.pairs.push(ChannelPair::new(pair, channel, self.sbinfo));
1129 } else {
1130 validate!(self.pairs[pair_no].channel == channel);
1131 validate!(self.pairs[pair_no].pair == pair);
1132 }
1133 validate!(if pair { channel + 1 } else { channel } < self.m4ainfo.channels);
1134 Ok(())
1135 }
1136 fn decode_ga(&mut self, br: &mut BitReader, abuf: &mut NABufferType) -> DecoderResult<()> {
1137 let mut cur_pair = 0;
1138 let mut cur_ch = 0;
1139 while br.left() > 3 {
1140 let id = br.read(3)?;
1141 match id {
1142 0 => { // ID_SCE
1143 let _tag = br.read(4)?;
1144 self.set_pair(cur_pair, cur_ch, false)?;
1145 self.pairs[cur_pair].decode_ga_sce(br, &self.codebooks, self.m4ainfo.otype)?;
1146 cur_pair += 1;
1147 cur_ch += 1;
1148 },
1149 1 => { // ID_CPE
1150 let _tag = br.read(4)?;
1151 self.set_pair(cur_pair, cur_ch, true)?;
1152 self.pairs[cur_pair].decode_ga_cpe(br, &self.codebooks, self.m4ainfo.otype)?;
1153 cur_pair += 1;
1154 cur_ch += 2;
1155 },
1156 2 => { // ID_CCE
1157 unimplemented!("coupling channel element");
1158 },
1159 3 => { // ID_LFE
1160 let _tag = br.read(4)?;
1161 self.set_pair(cur_pair, cur_ch, false)?;
1162 self.pairs[cur_pair].decode_ga_sce(br, &self.codebooks, self.m4ainfo.otype)?;
1163 cur_pair += 1;
1164 cur_ch += 1;
1165 },
1166 4 => { // ID_DSE
1167 let _id = br.read(4)?;
1168 let align = br.read_bool()?;
1169 let mut count = br.read(8)? as u32;
1170 if count == 255 { count += br.read(8)? as u32; }
1171 if align { br.align(); }
1172 br.skip(count * 8)?; // no SBR payload or such
1173 },
1174 5 => { // ID_PCE
1175 unimplemented!("program config");
1176 },
1177 6 => { // ID_FIL
1178 let mut count = br.read(4)? as usize;
1179 if count == 15 {
1180 count += br.read(8)? as usize;
1181 count -= 1;
1182 }
1183 for _ in 0..count {
1184 // ext payload
1185 br.skip(8)?;
1186 }
1187 },
1188 7 => { // ID_TERM
1189 break;
1190 },
1191 _ => { unreachable!(); },
1192 };
1193 }
1194 let srate_idx = GASubbandInfo::find_idx(self.m4ainfo.srate);
1195 for pair in 0..cur_pair {
1196 self.pairs[pair].synth_audio(&mut self.dsp, abuf, srate_idx);
1197 }
1198 Ok(())
1199 }
1200}
1201
1202impl NADecoder for AACDecoder {
01613464 1203 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
5a932f64
KS
1204 if let NACodecTypeInfo::Audio(_) = info.get_properties() {
1205 let edata = info.get_extradata().unwrap();
1206 validate!(edata.len() >= 2);
1207
1208//print!("edata:"); for s in edata.iter() { print!(" {:02X}", *s);}println!("");
1209 self.m4ainfo.read(&edata)?;
1210
1211 //println!("{}", self.m4ainfo);
1212 if (self.m4ainfo.otype != M4AType::LC) || (self.m4ainfo.channels > 2) || (self.m4ainfo.samples != 1024) {
1213 return Err(DecoderError::NotImplemented);
1214 }
1215 self.sbinfo = GASubbandInfo::find(self.m4ainfo.srate);
1216
1217 let ainfo = NAAudioInfo::new(self.m4ainfo.srate, self.m4ainfo.channels as u8,
1218 SND_F32P_FORMAT, self.m4ainfo.samples);
1219 self.info = info.replace_info(NACodecTypeInfo::Audio(ainfo));
1220
1221 if self.m4ainfo.channels >= DEFAULT_CHANNEL_MAP.len() {
1222 return Err(DecoderError::NotImplemented);
1223 }
1224 let chmap_str = DEFAULT_CHANNEL_MAP[self.m4ainfo.channels];
c83013a1 1225 if chmap_str.is_empty() { return Err(DecoderError::NotImplemented); }
5a932f64
KS
1226 self.chmap = NAChannelMap::from_str(chmap_str).unwrap();
1227
1228 Ok(())
1229 } else {
1230 Err(DecoderError::InvalidData)
1231 }
1232 }
01613464 1233 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
5a932f64
KS
1234 let info = pkt.get_stream().get_info();
1235 validate!(info.get_properties().is_audio());
1236 let pktbuf = pkt.get_buffer();
1237
1238 let ainfo = self.info.get_properties().get_audio_info().unwrap();
1239 let mut abuf = alloc_audio_buffer(ainfo, self.m4ainfo.samples, self.chmap.clone())?;
1240
fa90ccfb 1241 let mut br = BitReader::new(&pktbuf, BitReaderMode::BE);
5a932f64
KS
1242 match self.m4ainfo.otype {
1243 M4AType::LC => {
1244 self.decode_ga(&mut br, &mut abuf)?;
1245 },
1246 _ => { unimplemented!(""); }
1247 }
1248
1249 let mut frm = NAFrame::new_from_pkt(pkt, self.info.replace_info(NACodecTypeInfo::Audio(ainfo)), abuf);
1250 frm.set_keyframe(true);
171860fc 1251 Ok(frm.into_ref())
5a932f64 1252 }
f9be4e75
KS
1253 fn flush(&mut self) {
1254 for pair in self.pairs.iter_mut() {
1255 pair.ics[0].delay = [0.0; 1024];
1256 pair.ics[1].delay = [0.0; 1024];
1257 }
1258 }
5a932f64
KS
1259}
1260
08a1fab7 1261pub fn get_decoder() -> Box<dyn NADecoder + Send> {
5a932f64
KS
1262 Box::new(AACDecoder::new())
1263}
1264
1265#[cfg(test)]
1266mod test {
4f6124ac
KS
1267 use nihav_core::codecs::RegisteredDecoders;
1268 use nihav_core::demuxers::RegisteredDemuxers;
ce742854 1269 use nihav_codec_support::test::dec_video::test_decode_audio;
e64739f8
KS
1270 use crate::generic_register_all_codecs;
1271 use nihav_realmedia::realmedia_register_all_demuxers;
5a932f64
KS
1272 #[test]
1273 fn test_aac() {
4f6124ac
KS
1274 let mut dmx_reg = RegisteredDemuxers::new();
1275 realmedia_register_all_demuxers(&mut dmx_reg);
1276 let mut dec_reg = RegisteredDecoders::new();
1277 generic_register_all_codecs(&mut dec_reg);
1278
5a932f64
KS
1279// let file = "assets/RV/rv40_weighted_mc.rmvb";
1280 let file = "assets/RV/rv40_weighted_mc_2.rmvb";
5580b11b 1281 test_decode_audio("realmedia", file, Some(12000), None/*Some("aac")*/, &dmx_reg, &dec_reg);
5a932f64
KS
1282 }
1283}
1284
1285const AAC_SCF_CODEBOOK_BITS: &[u8] = &[
1286 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
1287 19, 19, 19, 18, 19, 18, 17, 17, 16, 17, 16, 16, 16, 16, 15, 15,
1288 14, 14, 14, 14, 14, 14, 13, 13, 12, 12, 12, 11, 12, 11, 10, 10,
1289 10, 9, 9, 8, 8, 8, 7, 6, 6, 5, 4, 3, 1, 4, 4, 5,
1290 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 10, 11, 11, 11, 11, 12,
1291 12, 13, 13, 13, 14, 14, 16, 15, 16, 15, 18, 19, 19, 19, 19, 19,
1292 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
1293 19, 19, 19, 19, 19, 19, 19, 19, 19
1294];
1295
1296const AAC_SCF_CODEBOOK_CODES: &[u32] = &[
1297 0x3FFE8, 0x3FFE6, 0x3FFE7, 0x3FFE5, 0x7FFF5, 0x7FFF1, 0x7FFED, 0x7FFF6,
1298 0x7FFEE, 0x7FFEF, 0x7FFF0, 0x7FFFC, 0x7FFFD, 0x7FFFF, 0x7FFFE, 0x7FFF7,
1299 0x7FFF8, 0x7FFFB, 0x7FFF9, 0x3FFE4, 0x7FFFA, 0x3FFE3, 0x1FFEF, 0x1FFF0,
1300 0x0FFF5, 0x1FFEE, 0x0FFF2, 0x0FFF3, 0x0FFF4, 0x0FFF1, 0x07FF6, 0x07FF7,
1301 0x03FF9, 0x03FF5, 0x03FF7, 0x03FF3, 0x03FF6, 0x03FF2, 0x01FF7, 0x01FF5,
1302 0x00FF9, 0x00FF7, 0x00FF6, 0x007F9, 0x00FF4, 0x007F8, 0x003F9, 0x003F7,
1303 0x003F5, 0x001F8, 0x001F7, 0x000FA, 0x000F8, 0x000F6, 0x00079, 0x0003A,
1304 0x00038, 0x0001A, 0x0000B, 0x00004, 0x00000, 0x0000A, 0x0000C, 0x0001B,
1305 0x00039, 0x0003B, 0x00078, 0x0007A, 0x000F7, 0x000F9, 0x001F6, 0x001F9,
1306 0x003F4, 0x003F6, 0x003F8, 0x007F5, 0x007F4, 0x007F6, 0x007F7, 0x00FF5,
1307 0x00FF8, 0x01FF4, 0x01FF6, 0x01FF8, 0x03FF8, 0x03FF4, 0x0FFF0, 0x07FF4,
1308 0x0FFF6, 0x07FF5, 0x3FFE2, 0x7FFD9, 0x7FFDA, 0x7FFDB, 0x7FFDC, 0x7FFDD,
1309 0x7FFDE, 0x7FFD8, 0x7FFD2, 0x7FFD3, 0x7FFD4, 0x7FFD5, 0x7FFD6, 0x7FFF2,
1310 0x7FFDF, 0x7FFE7, 0x7FFE8, 0x7FFE9, 0x7FFEA, 0x7FFEB, 0x7FFE6, 0x7FFE0,
1311 0x7FFE1, 0x7FFE2, 0x7FFE3, 0x7FFE4, 0x7FFE5, 0x7FFD7, 0x7FFEC, 0x7FFF4,
1312 0x7FFF3
1313];
1314
1315const AAC_SPEC_CB1_BITS: &[u8] = &[
1316 11, 9, 11, 10, 7, 10, 11, 9, 11, 10, 7, 10, 7, 5, 7, 9,
1317 7, 10, 11, 9, 11, 9, 7, 9, 11, 9, 11, 9, 7, 9, 7, 5,
1318 7, 9, 7, 9, 7, 5, 7, 5, 1, 5, 7, 5, 7, 9, 7, 9,
1319 7, 5, 7, 9, 7, 9, 11, 9, 11, 9, 7, 9, 11, 9, 11, 10,
1320 7, 9, 7, 5, 7, 9, 7, 10, 11, 9, 11, 10, 7, 9, 11, 9,
1321 11
1322];
1323const AAC_SPEC_CB1_CODES: &[u16] = &[
1324 0x7f8, 0x1f1, 0x7fd, 0x3f5, 0x068, 0x3f0, 0x7f7, 0x1ec,
1325 0x7f5, 0x3f1, 0x072, 0x3f4, 0x074, 0x011, 0x076, 0x1eb,
1326 0x06c, 0x3f6, 0x7fc, 0x1e1, 0x7f1, 0x1f0, 0x061, 0x1f6,
1327 0x7f2, 0x1ea, 0x7fb, 0x1f2, 0x069, 0x1ed, 0x077, 0x017,
1328 0x06f, 0x1e6, 0x064, 0x1e5, 0x067, 0x015, 0x062, 0x012,
1329 0x000, 0x014, 0x065, 0x016, 0x06d, 0x1e9, 0x063, 0x1e4,
1330 0x06b, 0x013, 0x071, 0x1e3, 0x070, 0x1f3, 0x7fe, 0x1e7,
1331 0x7f3, 0x1ef, 0x060, 0x1ee, 0x7f0, 0x1e2, 0x7fa, 0x3f3,
1332 0x06a, 0x1e8, 0x075, 0x010, 0x073, 0x1f4, 0x06e, 0x3f7,
1333 0x7f6, 0x1e0, 0x7f9, 0x3f2, 0x066, 0x1f5, 0x7ff, 0x1f7,
1334 0x7f4
1335];
1336const AAC_SPEC_CB2_BITS: &[u8] = &[
1337 9, 7, 9, 8, 6, 8, 9, 8, 9, 8, 6, 7, 6, 5, 6, 7,
1338 6, 8, 9, 7, 8, 8, 6, 8, 9, 7, 9, 8, 6, 7, 6, 5,
1339 6, 7, 6, 8, 6, 5, 6, 5, 3, 5, 6, 5, 6, 8, 6, 7,
1340 6, 5, 6, 8, 6, 8, 9, 7, 9, 8, 6, 8, 8, 7, 9, 8,
1341 6, 7, 6, 4, 6, 8, 6, 7, 9, 7, 9, 7, 6, 8, 9, 7,
1342 9
1343];
1344const AAC_SPEC_CB2_CODES: &[u16] = &[
1345 0x1f3, 0x06f, 0x1fd, 0x0eb, 0x023, 0x0ea, 0x1f7, 0x0e8,
1346 0x1fa, 0x0f2, 0x02d, 0x070, 0x020, 0x006, 0x02b, 0x06e,
1347 0x028, 0x0e9, 0x1f9, 0x066, 0x0f8, 0x0e7, 0x01b, 0x0f1,
1348 0x1f4, 0x06b, 0x1f5, 0x0ec, 0x02a, 0x06c, 0x02c, 0x00a,
1349 0x027, 0x067, 0x01a, 0x0f5, 0x024, 0x008, 0x01f, 0x009,
1350 0x000, 0x007, 0x01d, 0x00b, 0x030, 0x0ef, 0x01c, 0x064,
1351 0x01e, 0x00c, 0x029, 0x0f3, 0x02f, 0x0f0, 0x1fc, 0x071,
1352 0x1f2, 0x0f4, 0x021, 0x0e6, 0x0f7, 0x068, 0x1f8, 0x0ee,
1353 0x022, 0x065, 0x031, 0x002, 0x026, 0x0ed, 0x025, 0x06a,
1354 0x1fb, 0x072, 0x1fe, 0x069, 0x02e, 0x0f6, 0x1ff, 0x06d,
1355 0x1f6
1356];
1357const AAC_SPEC_CB3_BITS: &[u8] = &[
1358 1, 4, 8, 4, 5, 8, 9, 9, 10, 4, 6, 9, 6, 6, 9, 9,
1359 9, 10, 9, 10, 13, 9, 9, 11, 11, 10, 12, 4, 6, 10, 6, 7,
1360 10, 10, 10, 12, 5, 7, 11, 6, 7, 10, 9, 9, 11, 9, 10, 13,
1361 8, 9, 12, 10, 11, 12, 8, 10, 15, 9, 11, 15, 13, 14, 16, 8,
1362 10, 14, 9, 10, 14, 12, 12, 15, 11, 12, 16, 10, 11, 15, 12, 12,
1363 15
1364];
1365const AAC_SPEC_CB3_CODES: &[u16] = &[
1366 0x0000, 0x0009, 0x00ef, 0x000b, 0x0019, 0x00f0, 0x01eb, 0x01e6,
1367 0x03f2, 0x000a, 0x0035, 0x01ef, 0x0034, 0x0037, 0x01e9, 0x01ed,
1368 0x01e7, 0x03f3, 0x01ee, 0x03ed, 0x1ffa, 0x01ec, 0x01f2, 0x07f9,
1369 0x07f8, 0x03f8, 0x0ff8, 0x0008, 0x0038, 0x03f6, 0x0036, 0x0075,
1370 0x03f1, 0x03eb, 0x03ec, 0x0ff4, 0x0018, 0x0076, 0x07f4, 0x0039,
1371 0x0074, 0x03ef, 0x01f3, 0x01f4, 0x07f6, 0x01e8, 0x03ea, 0x1ffc,
1372 0x00f2, 0x01f1, 0x0ffb, 0x03f5, 0x07f3, 0x0ffc, 0x00ee, 0x03f7,
1373 0x7ffe, 0x01f0, 0x07f5, 0x7ffd, 0x1ffb, 0x3ffa, 0xffff, 0x00f1,
1374 0x03f0, 0x3ffc, 0x01ea, 0x03ee, 0x3ffb, 0x0ff6, 0x0ffa, 0x7ffc,
1375 0x07f2, 0x0ff5, 0xfffe, 0x03f4, 0x07f7, 0x7ffb, 0x0ff7, 0x0ff9,
1376 0x7ffa
1377];
1378const AAC_SPEC_CB4_BITS: &[u8] = &[
1379 4, 5, 8, 5, 4, 8, 9, 8, 11, 5, 5, 8, 5, 4, 8, 8,
1380 7, 10, 9, 8, 11, 8, 8, 10, 11, 10, 11, 4, 5, 8, 4, 4,
1381 8, 8, 8, 10, 4, 4, 8, 4, 4, 7, 8, 7, 9, 8, 8, 10,
1382 7, 7, 9, 10, 9, 10, 8, 8, 11, 8, 7, 10, 11, 10, 12, 8,
1383 7, 10, 7, 7, 9, 10, 9, 11, 11, 10, 12, 10, 9, 11, 11, 10,
1384 11
1385];
1386const AAC_SPEC_CB4_CODES: &[u16] = &[
1387 0x007, 0x016, 0x0f6, 0x018, 0x008, 0x0ef, 0x1ef, 0x0f3,
1388 0x7f8, 0x019, 0x017, 0x0ed, 0x015, 0x001, 0x0e2, 0x0f0,
1389 0x070, 0x3f0, 0x1ee, 0x0f1, 0x7fa, 0x0ee, 0x0e4, 0x3f2,
1390 0x7f6, 0x3ef, 0x7fd, 0x005, 0x014, 0x0f2, 0x009, 0x004,
1391 0x0e5, 0x0f4, 0x0e8, 0x3f4, 0x006, 0x002, 0x0e7, 0x003,
1392 0x000, 0x06b, 0x0e3, 0x069, 0x1f3, 0x0eb, 0x0e6, 0x3f6,
1393 0x06e, 0x06a, 0x1f4, 0x3ec, 0x1f0, 0x3f9, 0x0f5, 0x0ec,
1394 0x7fb, 0x0ea, 0x06f, 0x3f7, 0x7f9, 0x3f3, 0xfff, 0x0e9,
1395 0x06d, 0x3f8, 0x06c, 0x068, 0x1f5, 0x3ee, 0x1f2, 0x7f4,
1396 0x7f7, 0x3f1, 0xffe, 0x3ed, 0x1f1, 0x7f5, 0x7fe, 0x3f5,
1397 0x7fc
1398];
1399const AAC_SPEC_CB5_BITS: &[u8] = &[
1400 13, 12, 11, 11, 10, 11, 11, 12, 13, 12, 11, 10, 9, 8, 9, 10,
1401 11, 12, 12, 10, 9, 8, 7, 8, 9, 10, 11, 11, 9, 8, 5, 4,
1402 5, 8, 9, 11, 10, 8, 7, 4, 1, 4, 7, 8, 11, 11, 9, 8,
1403 5, 4, 5, 8, 9, 11, 11, 10, 9, 8, 7, 8, 9, 10, 11, 12,
1404 11, 10, 9, 8, 9, 10, 11, 12, 13, 12, 12, 11, 10, 10, 11, 12,
1405 13
1406];
1407const AAC_SPEC_CB5_CODES: &[u16] = &[
1408 0x1fff, 0x0ff7, 0x07f4, 0x07e8, 0x03f1, 0x07ee, 0x07f9, 0x0ff8,
1409 0x1ffd, 0x0ffd, 0x07f1, 0x03e8, 0x01e8, 0x00f0, 0x01ec, 0x03ee,
1410 0x07f2, 0x0ffa, 0x0ff4, 0x03ef, 0x01f2, 0x00e8, 0x0070, 0x00ec,
1411 0x01f0, 0x03ea, 0x07f3, 0x07eb, 0x01eb, 0x00ea, 0x001a, 0x0008,
1412 0x0019, 0x00ee, 0x01ef, 0x07ed, 0x03f0, 0x00f2, 0x0073, 0x000b,
1413 0x0000, 0x000a, 0x0071, 0x00f3, 0x07e9, 0x07ef, 0x01ee, 0x00ef,
1414 0x0018, 0x0009, 0x001b, 0x00eb, 0x01e9, 0x07ec, 0x07f6, 0x03eb,
1415 0x01f3, 0x00ed, 0x0072, 0x00e9, 0x01f1, 0x03ed, 0x07f7, 0x0ff6,
1416 0x07f0, 0x03e9, 0x01ed, 0x00f1, 0x01ea, 0x03ec, 0x07f8, 0x0ff9,
1417 0x1ffc, 0x0ffc, 0x0ff5, 0x07ea, 0x03f3, 0x03f2, 0x07f5, 0x0ffb,
1418 0x1ffe
1419];
1420const AAC_SPEC_CB6_BITS: &[u8] = &[
1421 11, 10, 9, 9, 9, 9, 9, 10, 11, 10, 9, 8, 7, 7, 7, 8,
1422 9, 10, 9, 8, 6, 6, 6, 6, 6, 8, 9, 9, 7, 6, 4, 4,
1423 4, 6, 7, 9, 9, 7, 6, 4, 4, 4, 6, 7, 9, 9, 7, 6,
1424 4, 4, 4, 6, 7, 9, 9, 8, 6, 6, 6, 6, 6, 8, 9, 10,
1425 9, 8, 7, 7, 7, 7, 8, 10, 11, 10, 9, 9, 9, 9, 9, 10,
1426 11
1427];
1428const AAC_SPEC_CB6_CODES: &[u16] = &[
1429 0x7fe, 0x3fd, 0x1f1, 0x1eb, 0x1f4, 0x1ea, 0x1f0, 0x3fc,
1430 0x7fd, 0x3f6, 0x1e5, 0x0ea, 0x06c, 0x071, 0x068, 0x0f0,
1431 0x1e6, 0x3f7, 0x1f3, 0x0ef, 0x032, 0x027, 0x028, 0x026,
1432 0x031, 0x0eb, 0x1f7, 0x1e8, 0x06f, 0x02e, 0x008, 0x004,
1433 0x006, 0x029, 0x06b, 0x1ee, 0x1ef, 0x072, 0x02d, 0x002,
1434 0x000, 0x003, 0x02f, 0x073, 0x1fa, 0x1e7, 0x06e, 0x02b,
1435 0x007, 0x001, 0x005, 0x02c, 0x06d, 0x1ec, 0x1f9, 0x0ee,
1436 0x030, 0x024, 0x02a, 0x025, 0x033, 0x0ec, 0x1f2, 0x3f8,
1437 0x1e4, 0x0ed, 0x06a, 0x070, 0x069, 0x074, 0x0f1, 0x3fa,
1438 0x7ff, 0x3f9, 0x1f6, 0x1ed, 0x1f8, 0x1e9, 0x1f5, 0x3fb,
1439 0x7fc
1440];
1441const AAC_SPEC_CB7_BITS: &[u8] = &[
1442 1, 3, 6, 7, 8, 9, 10, 11, 3, 4, 6, 7, 8, 8, 9, 9,
1443 6, 6, 7, 8, 8, 9, 9, 10, 7, 7, 8, 8, 9, 9, 10, 10,
1444 8, 8, 9, 9, 10, 10, 10, 11, 9, 8, 9, 9, 10, 10, 11, 11,
1445 10, 9, 9, 10, 10, 11, 12, 12, 11, 10, 10, 10, 11, 11, 12, 12
1446];
1447const AAC_SPEC_CB7_CODES: &[u16] = &[
1448 0x000, 0x005, 0x037, 0x074, 0x0f2, 0x1eb, 0x3ed, 0x7f7,
1449 0x004, 0x00c, 0x035, 0x071, 0x0ec, 0x0ee, 0x1ee, 0x1f5,
1450 0x036, 0x034, 0x072, 0x0ea, 0x0f1, 0x1e9, 0x1f3, 0x3f5,
1451 0x073, 0x070, 0x0eb, 0x0f0, 0x1f1, 0x1f0, 0x3ec, 0x3fa,
1452 0x0f3, 0x0ed, 0x1e8, 0x1ef, 0x3ef, 0x3f1, 0x3f9, 0x7fb,
1453 0x1ed, 0x0ef, 0x1ea, 0x1f2, 0x3f3, 0x3f8, 0x7f9, 0x7fc,
1454 0x3ee, 0x1ec, 0x1f4, 0x3f4, 0x3f7, 0x7f8, 0xffd, 0xffe,
1455 0x7f6, 0x3f0, 0x3f2, 0x3f6, 0x7fa, 0x7fd, 0xffc, 0xfff
1456];
1457const AAC_SPEC_CB8_BITS: &[u8] = &[
1458 5, 4, 5, 6, 7, 8, 9, 10, 4, 3, 4, 5, 6, 7, 7, 8,
1459 5, 4, 4, 5, 6, 7, 7, 8, 6, 5, 5, 6, 6, 7, 8, 8,
1460 7, 6, 6, 6, 7, 7, 8, 9, 8, 7, 6, 7, 7, 8, 8, 10,
1461 9, 7, 7, 8, 8, 8, 9, 9, 10, 8, 8, 8, 9, 9, 9, 10
1462];
1463const AAC_SPEC_CB8_CODES: &[u16] = &[
1464 0x00e, 0x005, 0x010, 0x030, 0x06f, 0x0f1, 0x1fa, 0x3fe,
1465 0x003, 0x000, 0x004, 0x012, 0x02c, 0x06a, 0x075, 0x0f8,
1466 0x00f, 0x002, 0x006, 0x014, 0x02e, 0x069, 0x072, 0x0f5,
1467 0x02f, 0x011, 0x013, 0x02a, 0x032, 0x06c, 0x0ec, 0x0fa,
1468 0x071, 0x02b, 0x02d, 0x031, 0x06d, 0x070, 0x0f2, 0x1f9,
1469 0x0ef, 0x068, 0x033, 0x06b, 0x06e, 0x0ee, 0x0f9, 0x3fc,
1470 0x1f8, 0x074, 0x073, 0x0ed, 0x0f0, 0x0f6, 0x1f6, 0x1fd,
1471 0x3fd, 0x0f3, 0x0f4, 0x0f7, 0x1f7, 0x1fb, 0x1fc, 0x3ff
1472];
1473const AAC_SPEC_CB9_BITS: &[u8] = &[
1474 1, 3, 6, 8, 9, 10, 10, 11, 11, 12, 12, 13, 13, 3, 4, 6,
1475 7, 8, 8, 9, 10, 10, 10, 11, 12, 12, 6, 6, 7, 8, 8, 9,
1476 10, 10, 10, 11, 12, 12, 12, 8, 7, 8, 9, 9, 10, 10, 11, 11,
1477 11, 12, 12, 13, 9, 8, 9, 9, 10, 10, 11, 11, 11, 12, 12, 12,
1478 13, 10, 9, 9, 10, 11, 11, 11, 12, 11, 12, 12, 13, 13, 11, 9,
1479 10, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 11, 10, 10, 11, 11,
1480 12, 12, 13, 13, 13, 13, 13, 13, 11, 10, 10, 11, 11, 11, 12, 12,
1481 13, 13, 14, 13, 14, 11, 10, 11, 11, 12, 12, 12, 12, 13, 13, 14,
1482 14, 14, 12, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 12,
1483 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 15, 15, 13, 12, 12, 12,
1484 13, 13, 13, 13, 14, 14, 14, 14, 15
1485];
1486const AAC_SPEC_CB9_CODES: &[u16] = &[
1487 0x0000, 0x0005, 0x0037, 0x00e7, 0x01de, 0x03ce, 0x03d9, 0x07c8,
1488 0x07cd, 0x0fc8, 0x0fdd, 0x1fe4, 0x1fec, 0x0004, 0x000c, 0x0035,
1489 0x0072, 0x00ea, 0x00ed, 0x01e2, 0x03d1, 0x03d3, 0x03e0, 0x07d8,
1490 0x0fcf, 0x0fd5, 0x0036, 0x0034, 0x0071, 0x00e8, 0x00ec, 0x01e1,
1491 0x03cf, 0x03dd, 0x03db, 0x07d0, 0x0fc7, 0x0fd4, 0x0fe4, 0x00e6,
1492 0x0070, 0x00e9, 0x01dd, 0x01e3, 0x03d2, 0x03dc, 0x07cc, 0x07ca,
1493 0x07de, 0x0fd8, 0x0fea, 0x1fdb, 0x01df, 0x00eb, 0x01dc, 0x01e6,
1494 0x03d5, 0x03de, 0x07cb, 0x07dd, 0x07dc, 0x0fcd, 0x0fe2, 0x0fe7,
1495 0x1fe1, 0x03d0, 0x01e0, 0x01e4, 0x03d6, 0x07c5, 0x07d1, 0x07db,
1496 0x0fd2, 0x07e0, 0x0fd9, 0x0feb, 0x1fe3, 0x1fe9, 0x07c4, 0x01e5,
1497 0x03d7, 0x07c6, 0x07cf, 0x07da, 0x0fcb, 0x0fda, 0x0fe3, 0x0fe9,
1498 0x1fe6, 0x1ff3, 0x1ff7, 0x07d3, 0x03d8, 0x03e1, 0x07d4, 0x07d9,
1499 0x0fd3, 0x0fde, 0x1fdd, 0x1fd9, 0x1fe2, 0x1fea, 0x1ff1, 0x1ff6,
1500 0x07d2, 0x03d4, 0x03da, 0x07c7, 0x07d7, 0x07e2, 0x0fce, 0x0fdb,
1501 0x1fd8, 0x1fee, 0x3ff0, 0x1ff4, 0x3ff2, 0x07e1, 0x03df, 0x07c9,
1502 0x07d6, 0x0fca, 0x0fd0, 0x0fe5, 0x0fe6, 0x1feb, 0x1fef, 0x3ff3,
1503 0x3ff4, 0x3ff5, 0x0fe0, 0x07ce, 0x07d5, 0x0fc6, 0x0fd1, 0x0fe1,
1504 0x1fe0, 0x1fe8, 0x1ff0, 0x3ff1, 0x3ff8, 0x3ff6, 0x7ffc, 0x0fe8,
1505 0x07df, 0x0fc9, 0x0fd7, 0x0fdc, 0x1fdc, 0x1fdf, 0x1fed, 0x1ff5,
1506 0x3ff9, 0x3ffb, 0x7ffd, 0x7ffe, 0x1fe7, 0x0fcc, 0x0fd6, 0x0fdf,
1507 0x1fde, 0x1fda, 0x1fe5, 0x1ff2, 0x3ffa, 0x3ff7, 0x3ffc, 0x3ffd,
1508 0x7fff
1509];
1510const AAC_SPEC_CB10_BITS: &[u8] = &[
1511 6, 5, 6, 6, 7, 8, 9, 10, 10, 10, 11, 11, 12, 5, 4, 4,
1512 5, 6, 7, 7, 8, 8, 9, 10, 10, 11, 6, 4, 5, 5, 6, 6,
1513 7, 8, 8, 9, 9, 10, 10, 6, 5, 5, 5, 6, 7, 7, 8, 8,
1514 9, 9, 10, 10, 7, 6, 6, 6, 6, 7, 7, 8, 8, 9, 9, 10,
1515 10, 8, 7, 6, 7, 7, 7, 8, 8, 8, 9, 10, 10, 11, 9, 7,
1516 7, 7, 7, 8, 8, 9, 9, 9, 10, 10, 11, 9, 8, 8, 8, 8,
1517 8, 9, 9, 9, 10, 10, 11, 11, 9, 8, 8, 8, 8, 8, 9, 9,
1518 10, 10, 10, 11, 11, 10, 9, 9, 9, 9, 9, 9, 10, 10, 10, 11,
1519 11, 12, 10, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 12, 11,
1520 10, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 12, 11, 10, 10, 10,
1521 10, 10, 10, 11, 11, 12, 12, 12, 12
1522];
1523const AAC_SPEC_CB10_CODES: &[u16] = &[
1524 0x022, 0x008, 0x01d, 0x026, 0x05f, 0x0d3, 0x1cf, 0x3d0,
1525 0x3d7, 0x3ed, 0x7f0, 0x7f6, 0xffd, 0x007, 0x000, 0x001,
1526 0x009, 0x020, 0x054, 0x060, 0x0d5, 0x0dc, 0x1d4, 0x3cd,
1527 0x3de, 0x7e7, 0x01c, 0x002, 0x006, 0x00c, 0x01e, 0x028,
1528 0x05b, 0x0cd, 0x0d9, 0x1ce, 0x1dc, 0x3d9, 0x3f1, 0x025,
1529 0x00b, 0x00a, 0x00d, 0x024, 0x057, 0x061, 0x0cc, 0x0dd,
1530 0x1cc, 0x1de, 0x3d3, 0x3e7, 0x05d, 0x021, 0x01f, 0x023,
1531 0x027, 0x059, 0x064, 0x0d8, 0x0df, 0x1d2, 0x1e2, 0x3dd,
1532 0x3ee, 0x0d1, 0x055, 0x029, 0x056, 0x058, 0x062, 0x0ce,
1533 0x0e0, 0x0e2, 0x1da, 0x3d4, 0x3e3, 0x7eb, 0x1c9, 0x05e,
1534 0x05a, 0x05c, 0x063, 0x0ca, 0x0da, 0x1c7, 0x1ca, 0x1e0,
1535 0x3db, 0x3e8, 0x7ec, 0x1e3, 0x0d2, 0x0cb, 0x0d0, 0x0d7,
1536 0x0db, 0x1c6, 0x1d5, 0x1d8, 0x3ca, 0x3da, 0x7ea, 0x7f1,
1537 0x1e1, 0x0d4, 0x0cf, 0x0d6, 0x0de, 0x0e1, 0x1d0, 0x1d6,
1538 0x3d1, 0x3d5, 0x3f2, 0x7ee, 0x7fb, 0x3e9, 0x1cd, 0x1c8,
1539 0x1cb, 0x1d1, 0x1d7, 0x1df, 0x3cf, 0x3e0, 0x3ef, 0x7e6,
1540 0x7f8, 0xffa, 0x3eb, 0x1dd, 0x1d3, 0x1d9, 0x1db, 0x3d2,
1541 0x3cc, 0x3dc, 0x3ea, 0x7ed, 0x7f3, 0x7f9, 0xff9, 0x7f2,
1542 0x3ce, 0x1e4, 0x3cb, 0x3d8, 0x3d6, 0x3e2, 0x3e5, 0x7e8,
1543 0x7f4, 0x7f5, 0x7f7, 0xffb, 0x7fa, 0x3ec, 0x3df, 0x3e1,
1544 0x3e4, 0x3e6, 0x3f0, 0x7e9, 0x7ef, 0xff8, 0xffe, 0xffc,
1545 0xfff
1546];
1547const AAC_SPEC_CB11_BITS: &[u8] = &[
1548 4, 5, 6, 7, 8, 8, 9, 10, 10, 10, 11, 11, 12, 11, 12, 12,
1549 10, 5, 4, 5, 6, 7, 7, 8, 8, 9, 9, 9, 10, 10, 10, 10,
1550 11, 8, 6, 5, 5, 6, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10,
1551 10, 10, 8, 7, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9, 9, 10,
1552 10, 10, 10, 8, 8, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9,
1553 10, 10, 10, 10, 8, 8, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9,
1554 9, 10, 10, 10, 10, 8, 9, 8, 8, 8, 8, 8, 8, 8, 9, 9,
1555 9, 10, 10, 10, 10, 10, 8, 9, 8, 8, 8, 8, 8, 8, 9, 9,
1556 9, 10, 10, 10, 10, 10, 10, 8, 10, 9, 8, 8, 9, 9, 9, 9,
1557 9, 10, 10, 10, 10, 10, 10, 11, 8, 10, 9, 9, 9, 9, 9, 9,
1558 9, 10, 10, 10, 10, 10, 10, 11, 11, 8, 11, 9, 9, 9, 9, 9,
1559 9, 10, 10, 10, 10, 10, 11, 10, 11, 11, 8, 11, 10, 9, 9, 10,
1560 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 8, 11, 10, 10, 10,
1561 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 9, 11, 10, 9,
1562 9, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 9, 11, 10,
1563 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 9, 12,
1564 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 12, 12, 9,
1565 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9,
1566 5
1567];
1568const AAC_SPEC_CB11_CODES: &[u16] = &[
1569 0x000, 0x006, 0x019, 0x03d, 0x09c, 0x0c6, 0x1a7, 0x390,
1570 0x3c2, 0x3df, 0x7e6, 0x7f3, 0xffb, 0x7ec, 0xffa, 0xffe,
1571 0x38e, 0x005, 0x001, 0x008, 0x014, 0x037, 0x042, 0x092,
1572 0x0af, 0x191, 0x1a5, 0x1b5, 0x39e, 0x3c0, 0x3a2, 0x3cd,
1573 0x7d6, 0x0ae, 0x017, 0x007, 0x009, 0x018, 0x039, 0x040,
1574 0x08e, 0x0a3, 0x0b8, 0x199, 0x1ac, 0x1c1, 0x3b1, 0x396,
1575 0x3be, 0x3ca, 0x09d, 0x03c, 0x015, 0x016, 0x01a, 0x03b,
1576 0x044, 0x091, 0x0a5, 0x0be, 0x196, 0x1ae, 0x1b9, 0x3a1,
1577 0x391, 0x3a5, 0x3d5, 0x094, 0x09a, 0x036, 0x038, 0x03a,
1578 0x041, 0x08c, 0x09b, 0x0b0, 0x0c3, 0x19e, 0x1ab, 0x1bc,
1579 0x39f, 0x38f, 0x3a9, 0x3cf, 0x093, 0x0bf, 0x03e, 0x03f,
1580 0x043, 0x045, 0x09e, 0x0a7, 0x0b9, 0x194, 0x1a2, 0x1ba,
1581 0x1c3, 0x3a6, 0x3a7, 0x3bb, 0x3d4, 0x09f, 0x1a0, 0x08f,
1582 0x08d, 0x090, 0x098, 0x0a6, 0x0b6, 0x0c4, 0x19f, 0x1af,
1583 0x1bf, 0x399, 0x3bf, 0x3b4, 0x3c9, 0x3e7, 0x0a8, 0x1b6,
1584 0x0ab, 0x0a4, 0x0aa, 0x0b2, 0x0c2, 0x0c5, 0x198, 0x1a4,
1585 0x1b8, 0x38c, 0x3a4, 0x3c4, 0x3c6, 0x3dd, 0x3e8, 0x0ad,
1586 0x3af, 0x192, 0x0bd, 0x0bc, 0x18e, 0x197, 0x19a, 0x1a3,
1587 0x1b1, 0x38d, 0x398, 0x3b7, 0x3d3, 0x3d1, 0x3db, 0x7dd,
1588 0x0b4, 0x3de, 0x1a9, 0x19b, 0x19c, 0x1a1, 0x1aa, 0x1ad,
1589 0x1b3, 0x38b, 0x3b2, 0x3b8, 0x3ce, 0x3e1, 0x3e0, 0x7d2,
1590 0x7e5, 0x0b7, 0x7e3, 0x1bb, 0x1a8, 0x1a6, 0x1b0, 0x1b2,
1591 0x1b7, 0x39b, 0x39a, 0x3ba, 0x3b5, 0x3d6, 0x7d7, 0x3e4,
1592 0x7d8, 0x7ea, 0x0ba, 0x7e8, 0x3a0, 0x1bd, 0x1b4, 0x38a,
1593 0x1c4, 0x392, 0x3aa, 0x3b0, 0x3bc, 0x3d7, 0x7d4, 0x7dc,
1594 0x7db, 0x7d5, 0x7f0, 0x0c1, 0x7fb, 0x3c8, 0x3a3, 0x395,
1595 0x39d, 0x3ac, 0x3ae, 0x3c5, 0x3d8, 0x3e2, 0x3e6, 0x7e4,
1596 0x7e7, 0x7e0, 0x7e9, 0x7f7, 0x190, 0x7f2, 0x393, 0x1be,
1597 0x1c0, 0x394, 0x397, 0x3ad, 0x3c3, 0x3c1, 0x3d2, 0x7da,
1598 0x7d9, 0x7df, 0x7eb, 0x7f4, 0x7fa, 0x195, 0x7f8, 0x3bd,
1599 0x39c, 0x3ab, 0x3a8, 0x3b3, 0x3b9, 0x3d0, 0x3e3, 0x3e5,
1600 0x7e2, 0x7de, 0x7ed, 0x7f1, 0x7f9, 0x7fc, 0x193, 0xffd,
1601 0x3dc, 0x3b6, 0x3c7, 0x3cc, 0x3cb, 0x3d9, 0x3da, 0x7d3,
1602 0x7e1, 0x7ee, 0x7ef, 0x7f5, 0x7f6, 0xffc, 0xfff, 0x19d,
1603 0x1c2, 0x0b5, 0x0a1, 0x096, 0x097, 0x095, 0x099, 0x0a0,
1604 0x0a2, 0x0ac, 0x0a9, 0x0b1, 0x0b3, 0x0bb, 0x0c0, 0x18f,
1605 0x004
1606];
1607
1608const AAC_SPEC_BITS: [&[u8]; 11] = [
1609 AAC_SPEC_CB1_BITS, AAC_SPEC_CB2_BITS, AAC_SPEC_CB3_BITS, AAC_SPEC_CB4_BITS,
1610 AAC_SPEC_CB5_BITS, AAC_SPEC_CB6_BITS, AAC_SPEC_CB7_BITS, AAC_SPEC_CB8_BITS,
1611 AAC_SPEC_CB9_BITS, AAC_SPEC_CB10_BITS, AAC_SPEC_CB11_BITS
1612];
1613const AAC_SPEC_CODES: [&[u16]; 11] = [
1614 AAC_SPEC_CB1_CODES, AAC_SPEC_CB2_CODES, AAC_SPEC_CB3_CODES, AAC_SPEC_CB4_CODES,
1615 AAC_SPEC_CB5_CODES, AAC_SPEC_CB6_CODES, AAC_SPEC_CB7_CODES, AAC_SPEC_CB8_CODES,
1616 AAC_SPEC_CB9_CODES, AAC_SPEC_CB10_CODES, AAC_SPEC_CB11_CODES
1617];
1618const AAC_UNSIGNED_CODEBOOK: [bool; 11] = [
1619 false, false, true, true, false, false, true, true, true, true, true
1620];
1621const AAC_CODEBOOK_MODULO: [u16; 7] = [
1622 9, 9, 8, 8, 13, 13, 17
1623];
1624
1625const AAC_QUADS: [[i8; 4]; 81] = [
1626 [ 0, 0, 0, 0 ], [ 0, 0, 0, 1 ], [ 0, 0, 0, 2 ],
1627 [ 0, 0, 1, 0 ], [ 0, 0, 1, 1 ], [ 0, 0, 1, 2 ],
1628 [ 0, 0, 2, 0 ], [ 0, 0, 2, 1 ], [ 0, 0, 2, 2 ],
1629 [ 0, 1, 0, 0 ], [ 0, 1, 0, 1 ], [ 0, 1, 0, 2 ],
1630 [ 0, 1, 1, 0 ], [ 0, 1, 1, 1 ], [ 0, 1, 1, 2 ],
1631 [ 0, 1, 2, 0 ], [ 0, 1, 2, 1 ], [ 0, 1, 2, 2 ],
1632 [ 0, 2, 0, 0 ], [ 0, 2, 0, 1 ], [ 0, 2, 0, 2 ],
1633 [ 0, 2, 1, 0 ], [ 0, 2, 1, 1 ], [ 0, 2, 1, 2 ],
1634 [ 0, 2, 2, 0 ], [ 0, 2, 2, 1 ], [ 0, 2, 2, 2 ],
1635 [ 1, 0, 0, 0 ], [ 1, 0, 0, 1 ], [ 1, 0, 0, 2 ],
1636 [ 1, 0, 1, 0 ], [ 1, 0, 1, 1 ], [ 1, 0, 1, 2 ],
1637 [ 1, 0, 2, 0 ], [ 1, 0, 2, 1 ], [ 1, 0, 2, 2 ],
1638 [ 1, 1, 0, 0 ], [ 1, 1, 0, 1 ], [ 1, 1, 0, 2 ],
1639 [ 1, 1, 1, 0 ], [ 1, 1, 1, 1 ], [ 1, 1, 1, 2 ],
1640 [ 1, 1, 2, 0 ], [ 1, 1, 2, 1 ], [ 1, 1, 2, 2 ],
1641 [ 1, 2, 0, 0 ], [ 1, 2, 0, 1 ], [ 1, 2, 0, 2 ],
1642 [ 1, 2, 1, 0 ], [ 1, 2, 1, 1 ], [ 1, 2, 1, 2 ],
1643 [ 1, 2, 2, 0 ], [ 1, 2, 2, 1 ], [ 1, 2, 2, 2 ],
1644 [ 2, 0, 0, 0 ], [ 2, 0, 0, 1 ], [ 2, 0, 0, 2 ],
1645 [ 2, 0, 1, 0 ], [ 2, 0, 1, 1 ], [ 2, 0, 1, 2 ],
1646 [ 2, 0, 2, 0 ], [ 2, 0, 2, 1 ], [ 2, 0, 2, 2 ],
1647 [ 2, 1, 0, 0 ], [ 2, 1, 0, 1 ], [ 2, 1, 0, 2 ],
1648 [ 2, 1, 1, 0 ], [ 2, 1, 1, 1 ], [ 2, 1, 1, 2 ],
1649 [ 2, 1, 2, 0 ], [ 2, 1, 2, 1 ], [ 2, 1, 2, 2 ],
1650 [ 2, 2, 0, 0 ], [ 2, 2, 0, 1 ], [ 2, 2, 0, 2 ],
1651 [ 2, 2, 1, 0 ], [ 2, 2, 1, 1 ], [ 2, 2, 1, 2 ],
1652 [ 2, 2, 2, 0 ], [ 2, 2, 2, 1 ], [ 2, 2, 2, 2 ],
1653];
1654
1655const DEFAULT_CHANNEL_MAP: [&str; 9] = [
1656 "",
1657 "C",
1658 "L,R",
1659 "C,L,R",
1660 "C,L,R,Cs",
1661 "C,L,R,Ls,Rs",
1662 "C,L,R,Ls,Rs,LFE",
1663 "",
1664 "C,L,R,Ls,Rs,Lss,Rss,LFE",
1665];
1666
1667const SWB_OFFSET_48K_LONG: [usize; 49+1] = [
1668 0, 4, 8, 12, 16, 20, 24, 28,
1669 32, 36, 40, 48, 56, 64, 72, 80,
1670 88, 96, 108, 120, 132, 144, 160, 176,
1671 196, 216, 240, 264, 292, 320, 352, 384,
1672 416, 448, 480, 512, 544, 576, 608, 640,
1673 672, 704, 736, 768, 800, 832, 864, 896,
1674 928, 1024
1675];
1676const SWB_OFFSET_48K_SHORT: [usize; 14+1] = [
1677 0, 4, 8, 12, 16, 20, 28, 36, 44, 56, 68, 80, 96, 112, 128
1678];
1679const SWB_OFFSET_32K_LONG: [usize; 51+1] = [
1680 0, 4, 8, 12, 16, 20, 24, 28,
1681 32, 36, 40, 48, 56, 64, 72, 80,
1682 88, 96, 108, 120, 132, 144, 160, 176,
1683 196, 216, 240, 264, 292, 320, 352, 384,
1684 416, 448, 480, 512, 544, 576, 608, 640,
1685 672, 704, 736, 768, 800, 832, 864, 896,
1686 928, 960, 992, 1024
1687];
1688const SWB_OFFSET_8K_LONG: [usize; 40+1] = [
1689 0, 12, 24, 36, 48, 60, 72, 84,
1690 96, 108, 120, 132, 144, 156, 172, 188,
1691 204, 220, 236, 252, 268, 288, 308, 328,
1692 348, 372, 396, 420, 448, 476, 508, 544,
1693 580, 620, 664, 712, 764, 820, 880, 944,
1694 1024
1695];
1696const SWB_OFFSET_8K_SHORT: [usize; 15+1] = [
1697 0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 60, 72, 88, 108, 128
1698];
1699const SWB_OFFSET_16K_LONG: [usize; 43+1] = [
1700 0, 8, 16, 24, 32, 40, 48, 56,
1701 64, 72, 80, 88, 100, 112, 124, 136,
1702 148, 160, 172, 184, 196, 212, 228, 244,
1703 260, 280, 300, 320, 344, 368, 396, 424,
1704 456, 492, 532, 572, 616, 664, 716, 772,
1705 832, 896, 960, 1024
1706];
1707const SWB_OFFSET_16K_SHORT: [usize; 15+1] = [
1708 0, 4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 60, 72, 88, 108, 128
1709];
1710const SWB_OFFSET_24K_LONG: [usize; 47+1] = [
1711 0, 4, 8, 12, 16, 20, 24, 28,
1712 32, 36, 40, 44, 52, 60, 68, 76,
1713 84, 92, 100, 108, 116, 124, 136, 148,
1714 160, 172, 188, 204, 220, 240, 260, 284,
1715 308, 336, 364, 396, 432, 468, 508, 552,
1716 600, 652, 704, 768, 832, 896, 960, 1024
1717];
1718const SWB_OFFSET_24K_SHORT: [usize; 15+1] = [
1719 0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 64, 76, 92, 108, 128
1720];
1721const SWB_OFFSET_64K_LONG: [usize; 47+1] = [
1722 0, 4, 8, 12, 16, 20, 24, 28,
1723 32, 36, 40, 44, 48, 52, 56, 64,
1724 72, 80, 88, 100, 112, 124, 140, 156,
1725 172, 192, 216, 240, 268, 304, 344, 384,
1726 424, 464, 504, 544, 584, 624, 664, 704,
1727 744, 784, 824, 864, 904, 944, 984, 1024
1728];
1729const SWB_OFFSET_64K_SHORT: [usize; 12+1] = [
1730 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128
1731];
1732const SWB_OFFSET_96K_LONG: [usize; 41+1] = [
1733 0, 4, 8, 12, 16, 20, 24, 28,
1734 32, 36, 40, 44, 48, 52, 56, 64,
1735 72, 80, 88, 96, 108, 120, 132, 144,
1736 156, 172, 188, 212, 240, 276, 320, 384,
1737 448, 512, 576, 640, 704, 768, 832, 896,
1738 960, 1024
1739];
1740
1741#[derive(Clone,Copy)]
1742struct GASubbandInfo {
1743 min_srate: u32,
1744 long_bands: &'static [usize],
1745 short_bands: &'static [usize],
1746}
1747
1748impl GASubbandInfo {
1749 fn find(srate: u32) -> GASubbandInfo {
c83013a1
KS
1750 for sbi in AAC_SUBBAND_INFO.iter() {
1751 if srate >= sbi.min_srate {
1752 return *sbi;
5a932f64
KS
1753 }
1754 }
1755 unreachable!("")
1756 }
1757 fn find_idx(srate: u32) -> usize {
c83013a1
KS
1758 for (i, sbi) in AAC_SUBBAND_INFO.iter().enumerate() {
1759 if srate >= sbi.min_srate {
5a932f64
KS
1760 return i;
1761 }
1762 }
1763 unreachable!("")
1764 }
1765}
1766
1767const AAC_SUBBAND_INFO: [GASubbandInfo; 12] = [
1768 GASubbandInfo { min_srate: 92017, long_bands: &SWB_OFFSET_96K_LONG, short_bands: &SWB_OFFSET_64K_SHORT }, //96K
1769 GASubbandInfo { min_srate: 75132, long_bands: &SWB_OFFSET_96K_LONG, short_bands: &SWB_OFFSET_64K_SHORT }, //88.2K
1770 GASubbandInfo { min_srate: 55426, long_bands: &SWB_OFFSET_64K_LONG, short_bands: &SWB_OFFSET_64K_SHORT }, //64K
1771 GASubbandInfo { min_srate: 46009, long_bands: &SWB_OFFSET_48K_LONG, short_bands: &SWB_OFFSET_48K_SHORT }, //48K
1772 GASubbandInfo { min_srate: 37566, long_bands: &SWB_OFFSET_48K_LONG, short_bands: &SWB_OFFSET_48K_SHORT }, //44.1K
1773 GASubbandInfo { min_srate: 27713, long_bands: &SWB_OFFSET_32K_LONG, short_bands: &SWB_OFFSET_48K_SHORT }, //32K
1774 GASubbandInfo { min_srate: 23004, long_bands: &SWB_OFFSET_24K_LONG, short_bands: &SWB_OFFSET_24K_SHORT }, //24K
1775 GASubbandInfo { min_srate: 18783, long_bands: &SWB_OFFSET_24K_LONG, short_bands: &SWB_OFFSET_24K_SHORT }, //22.05K
1776 GASubbandInfo { min_srate: 13856, long_bands: &SWB_OFFSET_16K_LONG, short_bands: &SWB_OFFSET_16K_SHORT }, //16K
1777 GASubbandInfo { min_srate: 11502, long_bands: &SWB_OFFSET_16K_LONG, short_bands: &SWB_OFFSET_16K_SHORT }, //12K
1778 GASubbandInfo { min_srate: 9391, long_bands: &SWB_OFFSET_16K_LONG, short_bands: &SWB_OFFSET_16K_SHORT }, //11.025K
1779 GASubbandInfo { min_srate: 0, long_bands: &SWB_OFFSET_8K_LONG, short_bands: &SWB_OFFSET_8K_SHORT }, //8K
1780];