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