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],
263 #[allow(clippy::manual_memcpy)]
266 let mut gain_tab: [f32; 16] = [0.0; 16];
267 let mut gain_tab2: [f32; 32] = [0.0; 32];
270 gain_tab[i] = 2.0f32.powf(4.0 - (i as f32));
273 gain_tab2[i] = 2.0f32.powf(((i as f32) - 15.0) * -0.125);
276 let mut tmpw: [f32; 256] = [0.0; 256];
277 for i in 0..tmpw.len() {
278 tmpw[i] = (((((i as f32) + 0.5) / 256.0 - 0.5) * consts::PI).sin() + 1.0) * 0.5;
281 let mut window: [f32; 512] = [0.0; 512];
282 for i in 0..tmpw.len() {
283 let w = tmpw[i] / (tmpw[i] * tmpw[i] + tmpw[255 - i] * tmpw[255 - i]);
285 window[512 - 1 - i] = w;
289 imdct: IMDCT::new(512, false),
290 tmp: [0.0; ATRAC3_FRAME_SIZE + 64],
291 gain_tab, gain_tab2, window,
294 fn synth_band(&mut self, src: &[f32]) {
295 let dst = &mut self.tmp;
296 self.imdct.imdct(src, dst);
298 dst[i] *= self.window[i];
301 fn apply_gains(&mut self, dst: &mut [f32], delay: &[f32], gain_data: &mut [GainData; 2], block_no: usize, band: usize) {
302 let prev_ngains = gain_data[block_no ^ 1].ngains[band];
303 let gain1 = if gain_data[block_no].ngains[band] > 0 {
304 self.gain_tab[gain_data[block_no].gain_lev[band][0]]
307 if prev_ngains == 0 {
309 dst[i] = self.tmp[i] * gain1 + delay[i];
314 gain_data[block_no ^ 1].gain_lev[band][prev_ngains] = 4;
315 gain_data[block_no ^ 1].gain_loc[band][prev_ngains] = 32;
318 for i in 0..prev_ngains {
319 let start = gain_data[block_no ^ 1].gain_loc[band][i] * 8;
322 let mut gain2 = self.gain_tab [gain_data[block_no ^ 1].gain_lev[band][i]];
323 let gain_inc = self.gain_tab2[gain_data[block_no ^ 1].gain_lev[band][i + 1] + 15 -
324 gain_data[block_no ^ 1].gain_lev[band][i]];
327 dst[off] = (self.tmp[off] * gain1 + delay[off]) * gain2;
331 dst[off] = (self.tmp[off] * gain1 + delay[off]) * gain2;
337 dst[i] = self.tmp[i] * gain1 + delay[i];
340 fn qmf_prepare(&mut self, src: &[f32], delay: &[f32], size: usize, swap: bool) {
342 self.tmp[i] = delay[i];
344 let (s0, s1) = if !swap {
345 (&src[0..], &src[size/2..])
347 (&src[size/2..], &src[0..])
349 for i in (0..size).step_by(4) {
350 self.tmp[46 + i + 0] = s0[i / 2 + 0] + s1[i / 2 + 0];
351 self.tmp[46 + i + 1] = s0[i / 2 + 0] - s1[i / 2 + 0];
352 self.tmp[46 + i + 2] = s0[i / 2 + 1] + s1[i / 2 + 1];
353 self.tmp[46 + i + 3] = s0[i / 2 + 1] - s1[i / 2 + 1];
356 fn qmf_synth(&mut self, dst: &mut [f32], size: usize) {
357 for i in (0..size).step_by(2) {
361 for j in (0..ATRAC3_QMF_FILTER.len()).step_by(2) {
362 acc0 += self.tmp[i + j + 0] * ATRAC3_QMF_FILTER[j + 0];
363 acc1 += self.tmp[i + j + 1] * ATRAC3_QMF_FILTER[j + 1];
365 dst[i + 0] = acc1 * consts::SQRT_2 / 256.0;
366 dst[i + 1] = acc0 * consts::SQRT_2 / 256.0;
369 fn do_qmf(&mut self, dst: &mut [f32], delay: &mut [f32], swap: bool) {
370 self.qmf_prepare(dst, delay, 512, swap);
371 self.qmf_synth(dst, 512);
373 delay[i] = self.tmp[512 + i];
376 fn do_qmf_out(&mut self, dst: &mut [f32], src: &[f32], delay: &mut [f32]) {
377 self.qmf_prepare(src, delay, 1024, false);
378 self.qmf_synth(dst, 1024);
380 delay[i] = self.tmp[1024 + i];
385 struct Atrac3Decoder {
386 info: NACodecInfoRef,
393 codebooks: [Codebook<u8>; 7],
395 ch_data: [Channel; 2],
396 scalefactors: [f32; 64],
398 mci_prev: [usize; 4],
400 mci_next: [usize; 4],
402 weighting_delay: [u8; 6],
407 struct Atrac3CodebookReader {
409 codes: &'static [u8],
411 impl CodebookDescReader<u8> for Atrac3CodebookReader {
412 fn bits(&mut self, idx: usize) -> u8 { self.bits[idx] }
413 fn code(&mut self, idx: usize) -> u32 { u32::from(self.codes[idx]) }
414 fn sym (&mut self, idx: usize) -> u8 { idx as u8 }
415 fn len(&mut self) -> usize { self.bits.len() }
420 let mut scalefactors: [f32; 64] = [0.0; 64];
421 for i in 0..scalefactors.len() {
422 scalefactors[i] = 2.0f32.powf(((i as f32) - 15.0) / 3.0);
425 let mut cb0 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[0], bits: ATRAC3_HUFF_BITS[0] };
426 let mut cb1 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[1], bits: ATRAC3_HUFF_BITS[1] };
427 let mut cb2 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[2], bits: ATRAC3_HUFF_BITS[2] };
428 let mut cb3 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[3], bits: ATRAC3_HUFF_BITS[3] };
429 let mut cb4 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[4], bits: ATRAC3_HUFF_BITS[4] };
430 let mut cb5 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[5], bits: ATRAC3_HUFF_BITS[5] };
431 let mut cb6 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[6], bits: ATRAC3_HUFF_BITS[6] };
433 info: NACodecInfo::new_dummy(),
434 chmap: NAChannelMap::new(),
441 ch_data: [Channel::new(), Channel::new()],
442 codebooks: [Codebook::new(&mut cb0, CodebookMode::MSB).unwrap(),
443 Codebook::new(&mut cb1, CodebookMode::MSB).unwrap(),
444 Codebook::new(&mut cb2, CodebookMode::MSB).unwrap(),
445 Codebook::new(&mut cb3, CodebookMode::MSB).unwrap(),
446 Codebook::new(&mut cb4, CodebookMode::MSB).unwrap(),
447 Codebook::new(&mut cb5, CodebookMode::MSB).unwrap(),
448 Codebook::new(&mut cb6, CodebookMode::MSB).unwrap() ],
454 weighting_delay: ATRAC3_WEIGHTING_DELAYS,
455 pkt_buf: Vec::with_capacity(65536),
459 #[allow(clippy::identity_op)]
460 fn rev_matrix(&mut self) {
462 let c0 = self.mci_prev[i];
463 let c1 = self.mci_cur[i];
466 let l0 = ATRAC3_MATRIX_COEFFS[c0 * 2 + 0];
467 let r0 = ATRAC3_MATRIX_COEFFS[c1 * 2 + 0];
468 let l1 = ATRAC3_MATRIX_COEFFS[c0 * 2 + 1];
469 let r1 = ATRAC3_MATRIX_COEFFS[c1 * 2 + 1];
471 let t0 = self.ch_data[0].data[idx + i * 256];
472 let t1 = self.ch_data[1].data[idx + i * 256];
473 let n1 = t0 * interp(l0, r0, idx) + t1 * interp(l1, r1, idx);
474 self.ch_data[0].data[idx + i * 256] = n1;
475 self.ch_data[1].data[idx + i * 256] = t0 * 2.0 - n1;
484 let t0 = self.ch_data[0].data[i];
485 let t1 = self.ch_data[1].data[i];
486 self.ch_data[0].data[i] = t1 * 2.0;
487 self.ch_data[1].data[i] = (t0 - t1) * 2.0;
492 let t0 = self.ch_data[0].data[i];
493 let t1 = self.ch_data[1].data[i];
494 self.ch_data[0].data[i] = (t0 + t1) * 2.0;
495 self.ch_data[1].data[i] = t1 * -2.0;
500 let t0 = self.ch_data[0].data[i];
501 let t1 = self.ch_data[1].data[i];
502 self.ch_data[0].data[i] = t0 + t1;
503 self.ch_data[1].data[i] = t0 - t1;
509 fn weigh_channels(&mut self) {
510 if (self.weighting_delay[1] == 7) && (self.weighting_delay[3] == 7) { return; }
512 if self.weighting_delay[1] == 7 {
515 let w = f32::from(self.weighting_delay[1]) / 7.0;
516 let iw = (2.0 - w * w).sqrt();
517 if self.weighting_delay[0] == 0 {
524 if self.weighting_delay[3] == 7 {
527 let w = f32::from(self.weighting_delay[3]) / 7.0;
528 let iw = (2.0 - w * w).sqrt();
529 if self.weighting_delay[2] == 0 {
539 self.ch_data[0].data[off + j] *= interp(pw[0], pw[1], j);
540 self.ch_data[1].data[off + j] *= interp(pw[0], pw[1], j);
543 self.ch_data[0].data[off + j] *= cw[0];
544 self.ch_data[1].data[off + j] *= cw[1];
550 fn interp(a: f32, b: f32, pos: usize) -> f32 {
551 a + ((pos as f32) / 8.0) * (b - a)
554 impl NADecoder for Atrac3Decoder {
555 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
556 if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() {
557 self.info = info.clone();
558 let edata = info.get_extradata().unwrap();
559 validate!(edata.len() >= 4);
561 let mut mr = MemoryReader::new_read(&edata);
562 let mut br = ByteReader::new(&mut mr);
566 let version = br.read_u32be()?;
567 validate!(version == 4);
568 self.samples = br.read_u16be()? as usize;
569 let delay = br.read_u16be()? as usize;
570 validate!(delay == ATRAC3_DEFAULT_DELAY);
571 let mode = br.read_u16be()?;
572 self.mode = match mode {
575 0x12 => Mode::JointStereo,
576 _ => return Err(DecoderError::InvalidData),
578 self.channels = if self.mode == Mode::Mono { 1 } else { 2 };
579 self.scrambled = true;
582 if edata.len() == 16 { br.read_skip(2)?; }
583 self.samples = br.read_u32be()? as usize;
584 let mode = br.read_u16be()?;
585 self.mode = if mode != 0 { Mode::JointStereo } else { Mode::Stereo };
588 let ffactor = br.read_u16be()? as usize;
589 validate!((ffactor > 0) && (ffactor <= 16));
590 // calculate block_align / channels / ffactor, validate it's 96/152/192
592 _ => { return Err(DecoderError::InvalidData) }
594 validate!(self.samples == ATRAC3_FRAME_SIZE * self.channels);
595 if self.mode == Mode::Mono {
596 self.chmap.add_channel(NAChannelType::C);
598 self.chmap.add_channel(NAChannelType::L);
599 self.chmap.add_channel(NAChannelType::R);
601 let ainfo = NAAudioInfo::new(ainfo.get_sample_rate(), self.channels as u8,
602 SND_F32P_FORMAT, self.samples);
603 self.info = info.replace_info(NACodecTypeInfo::Audio(ainfo));
606 Err(DecoderError::InvalidData)
609 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
610 let info = pkt.get_stream().get_info();
611 validate!(info.get_properties().is_audio());
612 let pktbuf = pkt.get_buffer();
613 validate!(pktbuf.len() > 5);
615 let frame_size = pktbuf.len();
616 self.pkt_buf.resize(frame_size, 0);
618 for (i, s) in pktbuf.iter().enumerate() {
619 self.pkt_buf[i] = s ^ ATRAC3_XOR_KEY[i & 3];
622 let dst = &mut self.pkt_buf[0..frame_size];
623 dst.copy_from_slice(pktbuf.as_slice());
627 let mut br = BitReader::new(&self.pkt_buf[0..frame_size], BitReaderMode::BE);
628 let id = br.read(6)?;
629 validate!(id == 0x28);
630 self.ch_data[0].decode_unit(&mut br, &self.codebooks, &self.scalefactors)?;
631 self.ch_data[0].synth(&mut self.dsp);
633 if self.channels == 2 {
635 if self.mode == Mode::JointStereo {
636 for i in 0..frame_size / 2 {
637 let b0 = self.pkt_buf[i];
638 let b1 = self.pkt_buf[frame_size - i - 1];
639 self.pkt_buf[i] = b1;
640 self.pkt_buf[frame_size - i - 1] = b0;
643 while (i < frame_size) && (self.pkt_buf[i] == 0xF8) { i += 1; }
644 validate!(frame_size - i > 4);
647 off = frame_size / 2;
649 let mut br = BitReader::new(&self.pkt_buf[off..frame_size], BitReaderMode::BE);
650 if self.mode == Mode::JointStereo {
651 let id = br.read(2)?;
652 validate!(id == 0x3);
654 let id = br.read(6)?;
655 validate!(id == 0x28);
657 if self.mode == Mode::JointStereo {
658 for i in 0..self.weighting_delay.len() - 2 {
659 self.weighting_delay[i] = self.weighting_delay[i + 2];
661 self.weighting_delay[4] = br.read(1)? as u8;
662 self.weighting_delay[5] = br.read(3)? as u8;
663 self.mci_prev = self.mci_cur;
664 self.mci_cur = self.mci_next;
666 self.mci_next[i] = br.read(2)? as usize;
669 self.ch_data[1].decode_unit(&mut br, &self.codebooks, &self.scalefactors)?;
670 self.ch_data[1].synth(&mut self.dsp);
672 if self.mode == Mode::JointStereo {
674 self.weigh_channels();
677 let ainfo = self.info.get_properties().get_audio_info().unwrap();
679 let abuf = alloc_audio_buffer(ainfo, ATRAC3_FRAME_SIZE, self.chmap.clone())?;
680 let mut adata = abuf.get_abuf_f32().unwrap();
681 let output = adata.get_data_mut().unwrap();
683 for ch in 0..self.channels {
684 let dpos = abuf.get_offset(ch);
685 self.ch_data[ch].do_qmf(&mut self.dsp, &mut output[dpos..][..ATRAC3_FRAME_SIZE]);
688 let mut frm = NAFrame::new_from_pkt(pkt, self.info.replace_info(NACodecTypeInfo::Audio(ainfo)), abuf);
689 frm.set_keyframe(true);
692 fn flush(&mut self) {
693 for ch_data in self.ch_data.iter_mut() {
694 ch_data.delay = [0.0; 1024];
695 ch_data.qmf_delay = [0.0; 64 * 3];
700 impl NAOptionHandler for Atrac3Decoder {
701 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
702 fn set_options(&mut self, _options: &[NAOption]) { }
703 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
706 pub fn get_decoder() -> Box<dyn NADecoder + Send> {
707 Box::new(Atrac3Decoder::new())
712 use nihav_core::codecs::RegisteredDecoders;
713 use nihav_core::demuxers::RegisteredDemuxers;
714 use nihav_codec_support::test::dec_video::test_decode_audio;
715 use crate::generic_register_all_decoders;
716 use nihav_realmedia::realmedia_register_all_demuxers;
719 let mut dmx_reg = RegisteredDemuxers::new();
720 realmedia_register_all_demuxers(&mut dmx_reg);
721 let mut dec_reg = RegisteredDecoders::new();
722 generic_register_all_decoders(&mut dec_reg);
724 // samples from private collection
725 let file = "assets/RV/rv30_atrc_384x208_realproducer_plus_8.51.rm";
726 // let file = "assets/RV/rv20_svt_atrc_640x352_realproducer_plus_8.51.rm";
727 test_decode_audio("realmedia", file, Some(12000), None/*Some("atrac3")*/, &dmx_reg, &dec_reg);
731 const ATRAC3_XOR_KEY: [u8; 4] = [ 0x53, 0x7F, 0x61, 0x03 ];
733 const ATRAC3_HUFF_CODES: [&[u8]; 7] = [
734 &[ 0x00, 0x04, 0x05, 0x0C, 0x0D, 0x1C, 0x1D, 0x1E, 0x1F ],
735 &[ 0x00, 0x04, 0x05, 0x06, 0x07 ],
736 &[ 0x00, 0x04, 0x05, 0x0C, 0x0D, 0x0E, 0x0F ],
737 &[ 0x00, 0x04, 0x05, 0x0C, 0x0D, 0x1C, 0x1D, 0x1E, 0x1F ], // same as 0
738 &[ 0x00, 0x02, 0x03, 0x08, 0x09, 0x0A, 0x0B, 0x1C, 0x1D, 0x3C, 0x3D, 0x3E, 0x3F, 0x0C, 0x0D ],
739 &[ 0x00, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x14,
740 0x15, 0x16, 0x17, 0x18, 0x19, 0x34, 0x35, 0x36,
741 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x78, 0x79, 0x7A,
742 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x08, 0x09 ],
743 &[ 0x00, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
744 0x0F, 0x10, 0x11, 0x24, 0x25, 0x26, 0x27, 0x28,
745 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
746 0x31, 0x32, 0x33, 0x68, 0x69, 0x6A, 0x6B, 0x6C,
747 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74,
748 0x75, 0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2,
749 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA,
750 0xFB, 0xFC, 0xFD, 0xFE, 0xFF, 0x02, 0x03 ]
752 const ATRAC3_HUFF_BITS: [&[u8]; 7] = [
753 &[ 1, 3, 3, 4, 4, 5, 5, 5, 5 ],
755 &[ 1, 3, 3, 4, 4, 4, 4 ],
756 &[ 1, 3, 3, 4, 4, 5, 5, 5, 5 ],
757 &[ 2, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 4, 4 ],
758 &[ 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 ],
759 &[ 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,
760 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 ],
763 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 ];
765 const ATRAC3_SUBBANDS: [usize; 32 + 1] = [
766 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176,
767 192, 224, 256, 288, 320, 352, 384, 416, 448, 480, 512, 576, 640, 704, 768, 896,
771 const ATRAC3_MATRIX_COEFFS: [f32; 8] = [ 0.0, 2.0, 2.0, 2.0, 0.0, 0.0, 1.0, 1.0 ];
773 const ATRAC3_QMF_FILTER: [f32; 48] = [
774 -0.000029238139, -0.000184109580, -0.000112315138, 0.000602345390,
775 0.000484503806, -0.001705877949, -0.001041114796, 0.004068033770,
776 0.001566677820, -0.008430772461, -0.001512299757, 0.015680588782,
777 -0.000122339843, -0.026883240789, 0.004925364163, 0.043472178280,
778 -0.015603342094, -0.068180441856, 0.037618979812, 0.108652018011,
779 -0.087192758918, -0.198768734932, 0.264158189297, 0.928483188152,
780 0.928483188152, 0.264158189297, -0.198768734932, -0.087192758918,
781 0.108652018011, 0.037618979812, -0.068180441856, -0.015603342094,
782 0.043472178280, 0.004925364163, -0.026883240789, -0.000122339843,
783 0.015680588782, -0.001512299757, -0.008430772461, 0.001566677820,
784 0.004068033770, -0.001041114796, -0.001705877949, 0.000484503806,
785 0.000602345390, -0.000112315138, -0.000184109580, -0.000029238139