1 use nihav_core::formats::*;
2 use nihav_core::frame::*;
3 use nihav_core::codecs::*;
4 use nihav_core::io::bitreader::*;
5 use nihav_core::io::byteio::*;
6 use nihav_core::io::codebook::*;
7 use nihav_codec_support::dsp::mdct::IMDCT;
10 #[derive(Clone,Copy,PartialEq)]
17 const ATRAC3_FRAME_SIZE: usize = 1024;
28 gain_lev: [[usize; 8]; 4],
29 gain_loc: [[usize; 8]; 4],
36 gain_lev: [[0; 8]; 4],
37 gain_loc: [[0; 8]; 4],
40 fn read(&mut self, br: &mut BitReader, bands: usize) -> DecoderResult<()> {
42 for band in 0..bands {
43 self.ngains[band] = br.read(3)? as usize;
44 for i in 0..self.ngains[band] {
45 self.gain_lev[band][i] = br.read(4)? as usize;
46 self.gain_loc[band][i] = br.read(5)? as usize;
54 data: [f32; ATRAC3_FRAME_SIZE],
55 delay: [f32; ATRAC3_FRAME_SIZE],
56 qmf_delay: [f32; 64 * 3],
61 gain_data: [GainData; 2],
72 data: [0.0; ATRAC3_FRAME_SIZE],
73 delay: [0.0; ATRAC3_FRAME_SIZE],
74 qmf_delay: [0.0; 64 * 3],
79 gain_data: [GainData::new(), GainData::new()],
84 tones: [Tone {pos: 0, ncoeffs: 0, coeffs: [0.0; 8]}; 64],
87 fn decode_unit(&mut self, br: &mut BitReader, codebooks: &[Codebook<u8>; 7], scalefactors: &[f32; 64]) -> DecoderResult<()> {
88 self.bands = (br.read(2)? as usize) + 1;
89 self.gain_data[self.block_no].read(br, self.bands)?;
90 self.data = [0.0; ATRAC3_FRAME_SIZE];
93 self.components = br.read(5)? as usize;
94 if self.components > 0 {
95 let selector = br.read(2)?;
96 validate!(selector != 2);
97 let mut mode0 = (selector & 1) != 0;
99 for _ in 0..self.components {
100 let mut flags: [bool; 4] = [false; 4];
101 for band in 0..self.bands {
102 flags[band] = br.read_bool()?;
104 let nvals = br.read(3)? as usize;
105 let quant = br.read(3)? as usize;
106 validate!(quant > 1);
108 mode0 = br.read_bool()?;
110 for j in 0..self.bands*4 {
111 if !flags[j >> 2] { continue; }
112 let count = br.read(3)? as usize;
113 validate!(self.ntones + count < 64);
115 let scale_idx = br.read(6)? as usize;
116 let tone = &mut self.tones[self.ntones];
117 tone.pos = (br.read(6)? as usize) + j * 64;
118 tone.ncoeffs = (ATRAC3_FRAME_SIZE - tone.pos).min(nvals + 1);
119 let scale = scalefactors[scale_idx] * ATRAC3_MAX_QUANT[quant];
120 read_coeffs(br, codebooks, &mut tone.coeffs[..tone.ncoeffs], quant, scale, mode0)?;
128 let mut quants: [usize; 32] = [0; 32];
129 let mut scf: [usize; 32] = [0; 32];
130 self.subbands = (br.read(5)? as usize) + 1;
131 let mode0 = br.read_bool()?;
132 for i in 0..self.subbands {
133 quants[i] = br.read(3)? as usize;
135 for i in 0..self.subbands {
136 if quants[i] == 0 { continue; }
137 scf[i] = br.read(6)? as usize;
140 self.data = [0.0; ATRAC3_FRAME_SIZE];
141 for i in 0..self.subbands {
142 if quants[i] == 0 { continue; }
143 let start = ATRAC3_SUBBANDS[i];
144 let end = ATRAC3_SUBBANDS[i + 1];
145 let scale = scalefactors[scf[i]] * ATRAC3_MAX_QUANT[quants[i]];
146 read_coeffs(br, codebooks, &mut self.data[start..end], quants[i], scale, mode0)?;
149 for i in 0..self.ntones {
150 let tone = &self.tones[i];
151 for j in 0..tone.ncoeffs {
152 self.data[tone.pos + j] += tone.coeffs[j];
157 fn synth(&mut self, dsp: &mut DSP) {
159 for (band, (data, delay)) in self.data.chunks_mut(256).zip(self.delay.chunks_mut(256)).enumerate() {
163 let t1 = data[255 - i];
168 dsp.synth_band(data);
169 dsp.apply_gains(data, delay, &mut self.gain_data, self.block_no, band);
170 delay.copy_from_slice(&dsp.tmp[256..512]);
175 fn do_qmf(&mut self, dsp: &mut DSP, dst: &mut [f32]) {
176 let mut qchunks = self.qmf_delay.chunks_mut(64);
177 let qmf0 = qchunks.next().unwrap();
178 let qmf1 = qchunks.next().unwrap();
179 let qmf2 = qchunks.next().unwrap();
181 let mut tchunks = self.data.chunks_mut(512);
182 let tmp0 = tchunks.next().unwrap();
183 let tmp1 = tchunks.next().unwrap();
184 dsp.do_qmf(tmp0, qmf0, false);
185 dsp.do_qmf(tmp1, qmf1, true);
187 dsp.do_qmf_out(dst, &self.data, qmf2);
191 const ATRAC3_DEFAULT_DELAY: usize = 2190;
192 const ATRAC3_WEIGHTING_DELAYS: [u8; 6] = [ 0, 7, 0, 7, 0, 7 ];
194 fn read_coeffs(br: &mut BitReader, cb: &[Codebook<u8>; 7], dst: &mut [f32], quant: usize, scale: f32, mode0: bool) -> DecoderResult<()> {
196 read_coeffs_mode0(br, dst, quant, scale)
197 } else if quant == 1 {
198 read_coeffs_mode1(br, &cb[0], dst, scale)
200 read_coeffs_other(br, &cb[quant - 1], dst, scale)
204 const ATRAC3_MODE0_CB: [f32; 4] = [ 0.0, 1.0, -2.0, -1.0 ];
205 const ATRAC3_MODE0_BITS: [u8; 8] = [ 0, 4, 3, 3, 4, 4, 5, 6 ];
207 fn read_coeffs_mode0(br: &mut BitReader, dst: &mut [f32], quant: usize, scale: f32) -> DecoderResult<()> {
208 let bits = ATRAC3_MODE0_BITS[quant];
210 for el in dst.iter_mut() {
211 let val = br.read_s(bits)? as f32;
215 for out in dst.chunks_mut(2) {
216 let val = br.read(4)? as usize;
217 out[0] = ATRAC3_MODE0_CB[val >> 2] * scale;
218 out[1] = ATRAC3_MODE0_CB[val & 3] * scale;
224 const ATRAC3_MODE1_CB: [f32; 9 * 2] = [
236 fn read_coeffs_mode1(br: &mut BitReader, cb: &Codebook<u8>, dst: &mut [f32], scale: f32) -> DecoderResult<()> {
237 for out in dst.chunks_mut(2) {
238 let val = br.read_cb(cb)? as usize;
239 out[0] = ATRAC3_MODE1_CB[val * 2 + 0] * scale;
240 out[1] = ATRAC3_MODE1_CB[val * 2 + 1] * scale;
245 fn read_coeffs_other(br: &mut BitReader, cb: &Codebook<u8>, dst: &mut [f32], scale: f32) -> DecoderResult<()> {
246 for el in dst.iter_mut() {
247 let val = (br.read_cb(cb)? as i8) + 1;
248 let sign = (val & 1) != 0;
249 let coef = f32::from(if sign { -(val >> 1) } else { val >> 1 });
259 gain_tab2: [f32; 32],
260 tmp: [f32; ATRAC3_FRAME_SIZE + 64],
265 let mut gain_tab: [f32; 16] = [0.0; 16];
266 let mut gain_tab2: [f32; 32] = [0.0; 32];
269 gain_tab[i] = 2.0f32.powf(4.0 - (i as f32));
272 gain_tab2[i] = 2.0f32.powf(((i as f32) - 15.0) * -0.125);
275 let mut tmpw: [f32; 256] = [0.0; 256];
276 for i in 0..tmpw.len() {
277 tmpw[i] = (((((i as f32) + 0.5) / 256.0 - 0.5) * consts::PI).sin() + 1.0) * 0.5;
280 let mut window: [f32; 512] = [0.0; 512];
281 for i in 0..tmpw.len() {
282 let w = tmpw[i] / (tmpw[i] * tmpw[i] + tmpw[255 - i] * tmpw[255 - i]);
284 window[512 - 1 - i] = w;
288 imdct: IMDCT::new(512, false),
289 tmp: [0.0; ATRAC3_FRAME_SIZE + 64],
290 gain_tab, gain_tab2, window,
293 fn synth_band(&mut self, src: &[f32]) {
294 let dst = &mut self.tmp;
295 self.imdct.imdct(src, dst);
297 dst[i] *= self.window[i];
300 fn apply_gains(&mut self, dst: &mut [f32], delay: &[f32], gain_data: &mut [GainData; 2], block_no: usize, band: usize) {
301 let prev_ngains = gain_data[block_no ^ 1].ngains[band];
302 let gain1 = if gain_data[block_no].ngains[band] > 0 {
303 self.gain_tab[gain_data[block_no].gain_lev[band][0]]
306 if prev_ngains == 0 {
308 dst[i] = self.tmp[i] * gain1 + delay[i];
313 gain_data[block_no ^ 1].gain_lev[band][prev_ngains] = 4;
314 gain_data[block_no ^ 1].gain_loc[band][prev_ngains] = 32;
317 for i in 0..prev_ngains {
318 let start = gain_data[block_no ^ 1].gain_loc[band][i] * 8;
321 let mut gain2 = self.gain_tab [gain_data[block_no ^ 1].gain_lev[band][i]];
322 let gain_inc = self.gain_tab2[gain_data[block_no ^ 1].gain_lev[band][i + 1] + 15 -
323 gain_data[block_no ^ 1].gain_lev[band][i]];
326 dst[off] = (self.tmp[off] * gain1 + delay[off]) * gain2;
330 dst[off] = (self.tmp[off] * gain1 + delay[off]) * gain2;
336 dst[i] = self.tmp[i] * gain1 + delay[i];
339 fn qmf_prepare(&mut self, src: &[f32], delay: &[f32], size: usize, swap: bool) {
341 self.tmp[i] = delay[i];
343 let (s0, s1) = if !swap {
344 (&src[0..], &src[size/2..])
346 (&src[size/2..], &src[0..])
348 for i in (0..size).step_by(4) {
349 self.tmp[46 + i + 0] = s0[i / 2 + 0] + s1[i / 2 + 0];
350 self.tmp[46 + i + 1] = s0[i / 2 + 0] - s1[i / 2 + 0];
351 self.tmp[46 + i + 2] = s0[i / 2 + 1] + s1[i / 2 + 1];
352 self.tmp[46 + i + 3] = s0[i / 2 + 1] - s1[i / 2 + 1];
355 fn qmf_synth(&mut self, dst: &mut [f32], size: usize) {
356 for i in (0..size).step_by(2) {
360 for j in (0..ATRAC3_QMF_FILTER.len()).step_by(2) {
361 acc0 += self.tmp[i + j + 0] * ATRAC3_QMF_FILTER[j + 0];
362 acc1 += self.tmp[i + j + 1] * ATRAC3_QMF_FILTER[j + 1];
364 dst[i + 0] = acc1 * consts::SQRT_2 / 256.0;
365 dst[i + 1] = acc0 * consts::SQRT_2 / 256.0;
368 fn do_qmf(&mut self, dst: &mut [f32], delay: &mut [f32], swap: bool) {
369 self.qmf_prepare(dst, delay, 512, swap);
370 self.qmf_synth(dst, 512);
372 delay[i] = self.tmp[512 + i];
375 fn do_qmf_out(&mut self, dst: &mut [f32], src: &[f32], delay: &mut [f32]) {
376 self.qmf_prepare(src, delay, 1024, false);
377 self.qmf_synth(dst, 1024);
379 delay[i] = self.tmp[1024 + i];
384 struct Atrac3Decoder {
385 info: NACodecInfoRef,
392 codebooks: [Codebook<u8>; 7],
394 ch_data: [Channel; 2],
395 scalefactors: [f32; 64],
397 mci_prev: [usize; 4],
399 mci_next: [usize; 4],
401 weighting_delay: [u8; 6],
406 struct Atrac3CodebookReader {
408 codes: &'static [u8],
410 impl CodebookDescReader<u8> for Atrac3CodebookReader {
411 fn bits(&mut self, idx: usize) -> u8 { self.bits[idx] }
412 fn code(&mut self, idx: usize) -> u32 { self.codes[idx] as u32 }
413 fn sym (&mut self, idx: usize) -> u8 { idx as u8 }
414 fn len(&mut self) -> usize { self.bits.len() }
419 let mut scalefactors: [f32; 64] = [0.0; 64];
420 for i in 0..scalefactors.len() {
421 scalefactors[i] = 2.0f32.powf(((i as f32) - 15.0) / 3.0);
424 let mut cb0 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[0], bits: ATRAC3_HUFF_BITS[0] };
425 let mut cb1 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[1], bits: ATRAC3_HUFF_BITS[1] };
426 let mut cb2 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[2], bits: ATRAC3_HUFF_BITS[2] };
427 let mut cb3 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[3], bits: ATRAC3_HUFF_BITS[3] };
428 let mut cb4 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[4], bits: ATRAC3_HUFF_BITS[4] };
429 let mut cb5 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[5], bits: ATRAC3_HUFF_BITS[5] };
430 let mut cb6 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[6], bits: ATRAC3_HUFF_BITS[6] };
432 info: NACodecInfo::new_dummy(),
433 chmap: NAChannelMap::new(),
440 ch_data: [Channel::new(), Channel::new()],
441 codebooks: [Codebook::new(&mut cb0, CodebookMode::MSB).unwrap(),
442 Codebook::new(&mut cb1, CodebookMode::MSB).unwrap(),
443 Codebook::new(&mut cb2, CodebookMode::MSB).unwrap(),
444 Codebook::new(&mut cb3, CodebookMode::MSB).unwrap(),
445 Codebook::new(&mut cb4, CodebookMode::MSB).unwrap(),
446 Codebook::new(&mut cb5, CodebookMode::MSB).unwrap(),
447 Codebook::new(&mut cb6, CodebookMode::MSB).unwrap() ],
453 weighting_delay: ATRAC3_WEIGHTING_DELAYS,
454 pkt_buf: Vec::with_capacity(65536),
458 fn rev_matrix(&mut self) {
460 let c0 = self.mci_prev[i];
461 let c1 = self.mci_cur[i];
464 let l0 = ATRAC3_MATRIX_COEFFS[c0 * 2 + 0];
465 let r0 = ATRAC3_MATRIX_COEFFS[c1 * 2 + 0];
466 let l1 = ATRAC3_MATRIX_COEFFS[c0 * 2 + 1];
467 let r1 = ATRAC3_MATRIX_COEFFS[c1 * 2 + 1];
469 let t0 = self.ch_data[0].data[idx + i * 256];
470 let t1 = self.ch_data[1].data[idx + i * 256];
471 let n1 = t0 * interp(l0, r0, idx) + t1 * interp(l1, r1, idx);
472 self.ch_data[0].data[idx + i * 256] = n1;
473 self.ch_data[1].data[idx + i * 256] = t0 * 2.0 - n1;
482 let t0 = self.ch_data[0].data[i];
483 let t1 = self.ch_data[1].data[i];
484 self.ch_data[0].data[i] = t1 * 2.0;
485 self.ch_data[1].data[i] = (t0 - t1) * 2.0;
490 let t0 = self.ch_data[0].data[i];
491 let t1 = self.ch_data[1].data[i];
492 self.ch_data[0].data[i] = (t0 + t1) * 2.0;
493 self.ch_data[1].data[i] = t1 * -2.0;
498 let t0 = self.ch_data[0].data[i];
499 let t1 = self.ch_data[1].data[i];
500 self.ch_data[0].data[i] = t0 + t1;
501 self.ch_data[1].data[i] = t0 - t1;
507 fn weigh_channels(&mut self) {
508 if (self.weighting_delay[1] == 7) && (self.weighting_delay[3] == 7) { return; }
510 if self.weighting_delay[1] == 7 {
513 let w = f32::from(self.weighting_delay[1]) / 7.0;
514 let iw = (2.0 - w * w).sqrt();
515 if self.weighting_delay[0] == 0 {
522 if self.weighting_delay[3] == 7 {
525 let w = f32::from(self.weighting_delay[3]) / 7.0;
526 let iw = (2.0 - w * w).sqrt();
527 if self.weighting_delay[2] == 0 {
537 self.ch_data[0].data[off + j] *= interp(pw[0], pw[1], j);
538 self.ch_data[1].data[off + j] *= interp(pw[0], pw[1], j);
541 self.ch_data[0].data[off + j] *= cw[0];
542 self.ch_data[1].data[off + j] *= cw[1];
548 fn interp(a: f32, b: f32, pos: usize) -> f32 {
549 a + ((pos as f32) / 8.0) * (b - a)
552 impl NADecoder for Atrac3Decoder {
553 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
554 if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() {
555 self.info = info.clone();
556 let edata = info.get_extradata().unwrap();
557 validate!(edata.len() >= 4);
559 let mut mr = MemoryReader::new_read(&edata);
560 let mut br = ByteReader::new(&mut mr);
564 let version = br.read_u32be()?;
565 validate!(version == 4);
566 self.samples = br.read_u16be()? as usize;
567 let delay = br.read_u16be()? as usize;
568 validate!(delay == ATRAC3_DEFAULT_DELAY);
569 let mode = br.read_u16be()?;
570 self.mode = match mode {
573 0x12 => Mode::JointStereo,
574 _ => return Err(DecoderError::InvalidData),
576 self.channels = if self.mode == Mode::Mono { 1 } else { 2 };
577 self.scrambled = true;
580 if edata.len() == 16 { br.read_skip(2)?; }
581 self.samples = br.read_u32be()? as usize;
582 let mode = br.read_u16be()?;
583 self.mode = if mode != 0 { Mode::JointStereo } else { Mode::Stereo };
586 let ffactor = br.read_u16be()? as usize;
587 validate!((ffactor > 0) && (ffactor <= 16));
588 // calculate block_align / channels / ffactor, validate it's 96/152/192
590 _ => { return Err(DecoderError::InvalidData) }
592 validate!(self.samples == ATRAC3_FRAME_SIZE * self.channels);
593 if self.mode == Mode::Mono {
594 self.chmap.add_channel(NAChannelType::C);
596 self.chmap.add_channel(NAChannelType::L);
597 self.chmap.add_channel(NAChannelType::R);
599 let ainfo = NAAudioInfo::new(ainfo.get_sample_rate(), self.channels as u8,
600 SND_F32P_FORMAT, self.samples);
601 self.info = info.replace_info(NACodecTypeInfo::Audio(ainfo));
604 Err(DecoderError::InvalidData)
607 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
608 let info = pkt.get_stream().get_info();
609 validate!(info.get_properties().is_audio());
610 let pktbuf = pkt.get_buffer();
611 validate!(pktbuf.len() > 5);
613 let frame_size = pktbuf.len();
614 self.pkt_buf.resize(frame_size, 0);
616 for (i, s) in pktbuf.iter().enumerate() {
617 self.pkt_buf[i] = s ^ ATRAC3_XOR_KEY[i & 3];
620 let dst = &mut self.pkt_buf[0..frame_size];
621 dst.copy_from_slice(pktbuf.as_slice());
625 let mut br = BitReader::new(&self.pkt_buf[0..frame_size], BitReaderMode::BE);
626 let id = br.read(6)?;
627 validate!(id == 0x28);
628 self.ch_data[0].decode_unit(&mut br, &self.codebooks, &self.scalefactors)?;
629 self.ch_data[0].synth(&mut self.dsp);
631 if self.channels == 2 {
633 if self.mode == Mode::JointStereo {
634 for i in 0..frame_size / 2 {
635 let b0 = self.pkt_buf[i];
636 let b1 = self.pkt_buf[frame_size - i - 1];
637 self.pkt_buf[i] = b1;
638 self.pkt_buf[frame_size - i - 1] = b0;
641 while (i < frame_size) && (self.pkt_buf[i] == 0xF8) { i += 1; }
642 validate!(frame_size - i > 4);
645 off = frame_size / 2;
647 let mut br = BitReader::new(&self.pkt_buf[off..frame_size], BitReaderMode::BE);
648 if self.mode == Mode::JointStereo {
649 let id = br.read(2)?;
650 validate!(id == 0x3);
652 let id = br.read(6)?;
653 validate!(id == 0x28);
655 if self.mode == Mode::JointStereo {
656 for i in 0..self.weighting_delay.len() - 2 {
657 self.weighting_delay[i] = self.weighting_delay[i + 2];
659 self.weighting_delay[4] = br.read(1)? as u8;
660 self.weighting_delay[5] = br.read(3)? as u8;
661 self.mci_prev = self.mci_cur;
662 self.mci_cur = self.mci_next;
664 self.mci_next[i] = br.read(2)? as usize;
667 self.ch_data[1].decode_unit(&mut br, &self.codebooks, &self.scalefactors)?;
668 self.ch_data[1].synth(&mut self.dsp);
670 if self.mode == Mode::JointStereo {
672 self.weigh_channels();
675 let ainfo = self.info.get_properties().get_audio_info().unwrap();
677 let abuf = alloc_audio_buffer(ainfo, ATRAC3_FRAME_SIZE, self.chmap.clone())?;
678 let mut adata = abuf.get_abuf_f32().unwrap();
679 let output = adata.get_data_mut().unwrap();
681 for ch in 0..self.channels {
682 let dpos = abuf.get_offset(ch);
683 self.ch_data[ch].do_qmf(&mut self.dsp, &mut output[dpos..][..ATRAC3_FRAME_SIZE]);
686 let mut frm = NAFrame::new_from_pkt(pkt, self.info.replace_info(NACodecTypeInfo::Audio(ainfo)), abuf);
687 frm.set_keyframe(true);
690 fn flush(&mut self) {
691 for ch_data in self.ch_data.iter_mut() {
692 ch_data.delay = [0.0; 1024];
693 ch_data.qmf_delay = [0.0; 64 * 3];
698 pub fn get_decoder() -> Box<dyn NADecoder + Send> {
699 Box::new(Atrac3Decoder::new())
704 use nihav_core::codecs::RegisteredDecoders;
705 use nihav_core::demuxers::RegisteredDemuxers;
706 use nihav_codec_support::test::dec_video::test_decode_audio;
707 use crate::generic_register_all_codecs;
708 use nihav_realmedia::realmedia_register_all_demuxers;
711 let mut dmx_reg = RegisteredDemuxers::new();
712 realmedia_register_all_demuxers(&mut dmx_reg);
713 let mut dec_reg = RegisteredDecoders::new();
714 generic_register_all_codecs(&mut dec_reg);
716 let file = "assets/RV/rv30_atrc_384x208_realproducer_plus_8.51.rm";
717 // let file = "assets/RV/rv20_svt_atrc_640x352_realproducer_plus_8.51.rm";
718 test_decode_audio("realmedia", file, Some(12000), None/*Some("atrac3")*/, &dmx_reg, &dec_reg);
722 const ATRAC3_XOR_KEY: [u8; 4] = [ 0x53, 0x7F, 0x61, 0x03 ];
724 const ATRAC3_HUFF_CODES: [&[u8]; 7] = [
725 &[ 0x00, 0x04, 0x05, 0x0C, 0x0D, 0x1C, 0x1D, 0x1E, 0x1F ],
726 &[ 0x00, 0x04, 0x05, 0x06, 0x07 ],
727 &[ 0x00, 0x04, 0x05, 0x0C, 0x0D, 0x0E, 0x0F ],
728 &[ 0x00, 0x04, 0x05, 0x0C, 0x0D, 0x1C, 0x1D, 0x1E, 0x1F ], // same as 0
729 &[ 0x00, 0x02, 0x03, 0x08, 0x09, 0x0A, 0x0B, 0x1C, 0x1D, 0x3C, 0x3D, 0x3E, 0x3F, 0x0C, 0x0D ],
730 &[ 0x00, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x14,
731 0x15, 0x16, 0x17, 0x18, 0x19, 0x34, 0x35, 0x36,
732 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x78, 0x79, 0x7A,
733 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x08, 0x09 ],
734 &[ 0x00, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
735 0x0F, 0x10, 0x11, 0x24, 0x25, 0x26, 0x27, 0x28,
736 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
737 0x31, 0x32, 0x33, 0x68, 0x69, 0x6A, 0x6B, 0x6C,
738 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74,
739 0x75, 0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2,
740 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA,
741 0xFB, 0xFC, 0xFD, 0xFE, 0xFF, 0x02, 0x03 ]
743 const ATRAC3_HUFF_BITS: [&[u8]; 7] = [
744 &[ 1, 3, 3, 4, 4, 5, 5, 5, 5 ],
746 &[ 1, 3, 3, 4, 4, 4, 4 ],
747 &[ 1, 3, 3, 4, 4, 5, 5, 5, 5 ],
748 &[ 2, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 4, 4 ],
749 &[ 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 4, 4 ],
750 &[ 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7,
751 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4 ],
754 const ATRAC3_MAX_QUANT: [f32; 8] = [ 0.0, 1.0/1.5, 1.0/2.5, 1.0/3.5, 1.0/4.5, 1.0/7.5, 1.0/15.5, 1.0/31.5 ];
756 const ATRAC3_SUBBANDS: [usize; 32 + 1] = [
757 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176,
758 192, 224, 256, 288, 320, 352, 384, 416, 448, 480, 512, 576, 640, 704, 768, 896,
762 const ATRAC3_MATRIX_COEFFS: [f32; 8] = [ 0.0, 2.0, 2.0, 2.0, 0.0, 0.0, 1.0, 1.0 ];
764 const ATRAC3_QMF_FILTER: [f32; 48] = [
765 -0.000029238139, -0.000184109580, -0.000112315138, 0.000602345390,
766 0.000484503806, -0.001705877949, -0.001041114796, 0.004068033770,
767 0.001566677820, -0.008430772461, -0.001512299757, 0.015680588782,
768 -0.000122339843, -0.026883240789, 0.004925364163, 0.043472178280,
769 -0.015603342094, -0.068180441856, 0.037618979812, 0.108652018011,
770 -0.087192758918, -0.198768734932, 0.264158189297, 0.928483188152,
771 0.928483188152, 0.264158189297, -0.198768734932, -0.087192758918,
772 0.108652018011, 0.037618979812, -0.068180441856, -0.015603342094,
773 0.043472178280, 0.004925364163, -0.026883240789, -0.000122339843,
774 0.015680588782, -0.001512299757, -0.008430772461, 0.001566677820,
775 0.004068033770, -0.001041114796, -0.001705877949, 0.000484503806,
776 0.000602345390, -0.000112315138, -0.000184109580, -0.000029238139