1 use nihav_core::codecs::*;
2 use nihav_core::io::bitreader::*;
3 use nihav_core::io::codebook::*;
8 const MP3_MAX_BANDS: usize = MP3_BANDS_SHORT * 3 + 4;
10 const MS_MODE: u8 = 2;
14 part2_3_length: usize,
17 scalefac_compress: usize,
21 table_select: [u8; 3],
22 subblock_gain: [u8; 3],
27 count1table_select: bool,
28 scalefac: [u8; MP3_MAX_BANDS],
29 istereo: [u8; MP3_MAX_BANDS],
35 impl Default for Granule {
36 fn default() -> Self { unsafe { std::mem::MaybeUninit::zeroed().assume_init() } }
40 fn get_mpeg2_params(&self, bits: &mut [u8; 4], independent: bool) -> usize {
42 match self.scalefac_compress {
44 bits[0] = MP3_SCF_BITS5[(self.scalefac_compress >> 4) * 2];
45 bits[1] = MP3_SCF_BITS5[(self.scalefac_compress >> 4) * 2 + 1];
46 bits[2] = ((self.scalefac_compress >> 2) & 3) as u8;
47 bits[3] = (self.scalefac_compress & 3) as u8;
51 let idx = self.scalefac_compress - 400;
52 bits[0] = MP3_SCF_BITS5[(idx >> 2) * 2];
53 bits[1] = MP3_SCF_BITS5[(idx >> 2) * 2 + 1];
54 bits[2] = (idx & 3) as u8;
59 let idx = self.scalefac_compress - 500;
60 bits[0] = MP3_SCF_BITS3[idx * 2];
61 bits[1] = MP3_SCF_BITS3[idx * 2 + 1];
69 let idx = self.scalefac_compress >> 1;
72 bits[0] = MP3_SCF_BITS6[idx * 3];
73 bits[1] = MP3_SCF_BITS6[idx * 3 + 1];
74 bits[2] = MP3_SCF_BITS6[idx * 3 + 2];
78 let val = (idx - 180) as u8;
80 bits[1] = (val >> 2) & 3;
86 bits[0] = MP3_SCF_BITS6[idx * 2];
87 bits[1] = MP3_SCF_BITS6[idx * 2 + 1];
106 let mut win36 = [0.0; 36];
107 let mut win36f = [0.0; 36];
108 let mut win12 = [0.0; 12];
109 let mut win12f = [0.0; 12];
112 win36 [i] = ((i as f32 + 0.5) * std::f32::consts::PI / 36.0).sin();
113 win36f[i] = if (i & 1) == 0 { win36[i] } else { -win36[i] };
116 win12 [i] = ((i as f32 + 0.5) * std::f32::consts::PI / 12.0).sin();
117 win12f[i] = if (i & 1) == 0 { win12[i] } else { -win12[i] };
122 win36, win36f, win12, win12f
125 fn mdct36(&mut self, src: &mut [f32], dst: &mut[f32], delay: &mut [f32], len: usize, block_type: u8) {
126 let mut flip = false;
127 for i in (0..len).step_by(18) {
128 let (win36, win12) = if flip { (&self.win36f, &self.win12f) } else { (&self.win36, &self.win12) };
129 dct36(&mut src[i..], &mut self.tmp);
133 dst[i + j] = delay[i + j] - self.tmp[8 - j] * win36[j];
134 delay[i + j] = self.tmp[j + 9] * win36[j + 18];
137 dst[i + j] = delay[i + j] + self.tmp[j - 9] * win36[j];
138 delay[i + j] = self.tmp[26 - j] * win36[j + 18];
143 dst[i + j] = delay[i + j] - self.tmp[8 - j] * win36[j];
146 dst[i + j] = delay[i + j] + self.tmp[j - 9] * win36[j];
148 delay[i..][..6].copy_from_slice(&self.tmp[9..][..6]);
150 for j in (1..6).step_by(2) {
151 delay[i + j] = -delay[i + j];
155 delay[i + j] = self.tmp[j + 9] * win12[j];
158 delay[i + j] = self.tmp[26 - j] * win12[j];
165 dst[i..][..6].copy_from_slice(&delay[i..][..6]);
167 dst[i + j] = delay[i + j] - self.tmp[8 - j] * win12[j - 6];
170 dst[i + j] = delay[i + j] + self.tmp[j - 9] * win12[j - 6];
174 dst[i + j] = delay[i + j] + self.tmp[j - 9];
178 dst[i + j] = delay[i + j] + if (j & 1) == 0 { self.tmp[j - 9] } else { -self.tmp[j - 9] };
182 delay[i + j] = self.tmp[j + 9] * win36[j + 18];
185 delay[i + j] = self.tmp[26 - j] * win36[j + 18];
193 fn mdct12(&mut self, src: &[f32], dst: &mut[f32], delay: &mut [f32], len: usize) {
194 let mut flip = false;
195 for i in (0..len).step_by(18) {
196 let window = if flip { &self.win12f } else { &self.win12 };
198 let tmp = &mut self.tmp[j * 12..];
199 dct12(&src[i + j..], tmp);
200 for (el, &w) in tmp.iter_mut().zip(window.iter()) {
206 dst[i + j] = delay[i + j];
207 delay[i + j] = self.tmp[j + 18] + self.tmp[j + 18 + 6];
210 dst[i + j] = delay[i + j] + self.tmp[j - 6];
211 delay[i + j] = self.tmp[j + 18 + 6];
214 dst[i + j] = delay[i + j] + self.tmp[j - 6] + self.tmp[j];
226 granule: [[Granule; 2]; 2],
227 scfsi: [[bool; 4]; 2],
228 pub main_data_end: usize,
229 is_mode: [i8; SAMPLES],
233 delay: [[f32; SAMPLES / 2]; 2],
234 tmp: [f32; SAMPLES / 2],
238 pub fn new() -> Self {
240 cb: MP3Codebooks::new(),
241 mdct: MDCTContext::new(),
242 granule: [[Granule::default(); 2]; 2],
243 scfsi: [[false; 4]; 2],
245 is_mode: [0; SAMPLES],
249 delay: [[0.0; SAMPLES / 2]; 2],
250 tmp: [0.0; SAMPLES / 2],
253 pub fn reset(&mut self) {
254 for dly in self.delay.iter_mut() {
255 for el in dly.iter_mut() {
260 fn calc_scale(gr: &Granule, sb: usize, ssb: usize, sblk_gain: u8) -> i8 {
261 (i32::from(gr.global_gain) - 64 - 146
262 - 8 * i32::from(sblk_gain)
263 - if gr.scalefac_scale { 4 } else { 2 } * (i32::from(gr.scalefac[ssb])
264 + if gr.preflag { i32::from(MP3_PREEMP_SCALES[sb]) } else { 0 })
265 ).min(127).max(-124) as i8
267 fn read_mp3_coeffs(&mut self, br: &mut BitReader, end: usize, gr_no: usize, ch: usize, coeffs: &mut [f32]) -> DecoderResult<()> {
268 let mut scales = [0; SAMPLES / 2];
269 let gr = &mut self.granule[gr_no][ch];
271 // calculate scales first
272 if gr.block_type != 2 {
275 while off < SAMPLES / 2 {
276 let end = MP3_SFB_LONG_OFFS[self.sf_idx][sb + 1];
277 let scale = Self::calc_scale(gr, sb, sb, 0);
278 for el in scales[off..end].iter_mut() {
283 let scf = gr.scalefac[sb.min(MP3_BANDS - 1)];
284 if scf != gr.istereo[sb] {
285 for el in self.is_mode[gr_no * SAMPLES/2..][off..end].iter_mut() {
295 let end_band = if self.mpeg1 { 8 } else { 6 };
300 while sb < end_band {
301 let end = MP3_SFB_LONG_OFFS[self.sf_idx][sb + 1];
302 let scale = Self::calc_scale(gr, sb, sb, 0);
303 for el in scales[off..end].iter_mut() {
308 let scf = gr.scalefac[sb.min(MP3_BANDS - 1)];
309 if scf != gr.istereo[sb] {
310 for el in self.is_mode[gr_no * SAMPLES/2..][off..end].iter_mut() {
319 let mut ssb = if gr.switch_point { 8 } else { 0 };
320 let mut sb = if gr.switch_point { 3 } else { 0 };
321 while sb <= MP3_BANDS_SHORT {
322 let band_size = MP3_SFB_SHORT_SIZE[self.sf_idx][sb];
324 let scale = Self::calc_scale(gr, sb, ssb, gr.subblock_gain[win]);
325 for el in scales[off..][..band_size].iter_mut() {
330 if sb == MP3_BANDS_SHORT {
331 gr.scalefac[ssb] = gr.scalefac[ssb - 3];
332 gr.istereo[ssb] = gr.istereo[ssb - 3];
334 let scf = gr.scalefac[ssb];
335 if scf != gr.istereo[ssb] {
336 for el in self.is_mode[gr_no * SAMPLES/2 + off..][..band_size].iter_mut() {
348 // prepare for coefficients decoding
349 let region1_start = if gr.block_type != 2 {
350 MP3_SFB_LONG_OFFS[self.sf_idx][gr.region_address1 as usize + 1]
351 } else if gr.switch_point {
354 MP3_SFB_SHORT_OFFS[self.sf_idx][3] * 3
355 }.min(gr.big_values);
356 let region2_start = MP3_SFB_LONG_OFFS[self.sf_idx][((gr.region_address1 + gr.region_address2 + 2) as usize).min(MP3_BANDS + 1)].min(gr.big_values);
358 for el in coeffs[..SAMPLES/2].iter_mut() {
364 if let Some((cb, esc_bits)) = self.cb.get_cb(gr.table_select[0]) {
365 let lc = read_region(br, end, coeffs, &scales, 0, region1_start, cb, esc_bits)?;
366 gr.lastcoded = gr.lastcoded.max(lc);
368 if let Some((cb, esc_bits)) = self.cb.get_cb(gr.table_select[1]) {
369 let lc = read_region(br, end, coeffs, &scales, region1_start, region2_start, cb, esc_bits)?;
370 gr.lastcoded = gr.lastcoded.max(lc);
372 if let Some((cb, esc_bits)) = self.cb.get_cb(gr.table_select[2]) {
373 let lc = read_region(br, end, coeffs, &scales, region2_start, gr.big_values, cb, esc_bits)?;
374 gr.lastcoded = gr.lastcoded.max(lc);
376 let (lc, zp) = read_region_quad(br, end, coeffs, &scales, gr.big_values, if !gr.count1table_select { Some(&self.cb.quad_cb) } else { None })?;
377 gr.lastcoded = gr.lastcoded.max(lc);
378 gr.zero_part = if zp > 0 { zp } else { gr.lastcoded };
382 pub fn read_mp3_side_data(&mut self, br: &mut BitReader, channels: usize) -> DecoderResult<()> {
384 self.main_data_end = br.read(9)? as usize;
385 let _private_bits = br.read(if channels == 1 { 5 } else { 3 })?;
386 for scfsis in self.scfsi[..channels].iter_mut() {
387 for scfsi in scfsis.iter_mut() {
388 *scfsi = br.read_bool()?;
392 self.main_data_end = br.read(8)? as usize;
393 let _private_bits = br.read(channels as u8)?;
395 let granules = if self.mpeg1 { 2 } else { 1 };
396 for grans in self.granule[..granules].iter_mut() {
397 for gr in grans[..channels].iter_mut() {
398 gr.part2_3_length = br.read(12)? as usize;
399 gr.big_values = (br.read(9)? as usize) * 2;
400 gr.global_gain = br.read(8)? as u8;
401 gr.scalefac_compress = br.read(if self.mpeg1 { 4 } else { 9 })? as usize;
402 gr.blocksplit = br.read_bool()?;
404 gr.block_type = br.read(2)? as u8;
405 gr.switch_point = br.read_bool()?;
406 for tsel in gr.table_select[..2].iter_mut() {
407 *tsel = br.read(5)? as u8;
409 4 | 14 => return Err(DecoderError::InvalidData),
413 for gain in gr.subblock_gain.iter_mut() {
414 *gain = br.read(3)? as u8;
416 gr.region_address1 = 7;
417 gr.region_address2 = 13;
420 gr.switch_point = false;
421 for tsel in gr.table_select.iter_mut() {
422 *tsel = br.read(5)? as u8;
424 4 | 14 => return Err(DecoderError::InvalidData),
428 gr.region_address1 = br.read(4)? as u8;
429 gr.region_address2 = br.read(3)? as u8;
432 gr.preflag = br.read_bool()?;
436 gr.scalefac_scale = br.read_bool()?;
437 gr.count1table_select = br.read_bool()?
442 pub fn decode_mpeg1_layer3(&mut self, br: &mut BitReader, coeffs: &mut [[f32; SAMPLES]; 2], channels: usize) -> DecoderResult<()> {
443 let mut data_end = 0;
445 for ch in 0..channels {
446 data_end += self.granule[gr_no][ch].part2_3_length;
448 if self.granule[gr_no][ch].block_type != 2 {
450 self.granule[1][ch].scalefac = self.granule[0][ch].scalefac;
452 for scf in self.granule[gr_no][ch].scalefac.iter_mut() {
457 let gr = &mut self.granule[gr_no][ch];
458 let bits1 = MP3_SCALEFAC_BITS1[gr.scalefac_compress];
459 let bits2 = MP3_SCALEFAC_BITS2[gr.scalefac_compress];
461 if !self.scfsi[ch][SCFSI_FROM_BAND[cb]] || (gr_no == 0) {
462 gr.scalefac[cb] = br.read(bits1)? as u8;
465 for cb in 11..MP3_BANDS {
466 if !self.scfsi[ch][SCFSI_FROM_BAND[cb]] || (gr_no == 0) {
467 gr.scalefac[cb] = br.read(bits2)? as u8;
470 for scf in gr.scalefac[MP3_BANDS..].iter_mut() {
473 for is in gr.istereo.iter_mut() {
477 let gr = &mut self.granule[gr_no][ch];
478 let bits1 = MP3_SCALEFAC_BITS1[gr.scalefac_compress];
479 let bits2 = MP3_SCALEFAC_BITS2[gr.scalefac_compress];
480 let pivot = if gr.blocksplit && gr.switch_point { 17 } else { 18 };
482 for scf in gr.scalefac[..pivot].iter_mut() {
483 *scf = br.read(bits1)? as u8;
485 for scf in gr.scalefac[pivot..][..18].iter_mut() {
486 *scf = br.read(bits2)? as u8;
488 for scf in gr.scalefac[pivot + 18..].iter_mut() {
491 for is in gr.istereo.iter_mut() {
495 self.read_mp3_coeffs(br, data_end, gr_no, ch, &mut coeffs[ch][gr_no * SAMPLES/2..])?;
496 validate!(br.tell() <= data_end);
497 br.seek(data_end as u32)?;
502 pub fn decode_mpeg2_layer3(&mut self, br: &mut BitReader, coeffs: &mut [[f32; SAMPLES]; 2], channels: usize, mode_ext: u8) -> DecoderResult<()> {
503 let mut data_end = 0;
504 for ch in 0..channels {
505 let gr = &mut self.granule[0][ch];
506 data_end += gr.part2_3_length;
508 let mut bits = [0; 4];
509 let idx = gr.get_mpeg2_params(&mut bits, (ch == 0) || ((mode_ext & IS_MODE) == 0));
510 let idx2 = if gr.block_type != 2 { 0 } else if !gr.switch_point { 1 } else { 2 };
512 gr.preflag = idx == 2;
513 let ends = &MP3_SCF_ENDS[idx][idx2];
515 for (scf, is) in gr.scalefac[0..ends[0]].iter_mut().zip(gr.istereo[0..ends[0]].iter_mut()) {
516 *scf = br.read(bits[0])? as u8;
517 *is = (1 << bits[0]) - 1;
519 for (scf, is) in gr.scalefac[ends[0]..ends[1]].iter_mut().zip(gr.istereo[ends[0]..ends[1]].iter_mut()) {
520 *scf = br.read(bits[1])? as u8;
521 *is = (1 << bits[1]) - 1;
523 for (scf, is) in gr.scalefac[ends[1]..ends[2]].iter_mut().zip(gr.istereo[ends[1]..ends[2]].iter_mut()) {
524 *scf = br.read(bits[2])? as u8;
525 *is = (1 << bits[2]) - 1;
527 for (scf, is) in gr.scalefac[ends[2]..ends[3]].iter_mut().zip(gr.istereo[ends[2]..ends[3]].iter_mut()) {
528 *scf = br.read(bits[3])? as u8;
529 *is = (1 << bits[3]) - 1;
531 self.read_mp3_coeffs(br, data_end, 0, ch, &mut coeffs[ch])?;
532 validate!(br.tell() <= data_end);
533 br.seek(data_end as u32)?;
537 pub fn synth(&mut self, coeffs: &mut [[f32; SAMPLES]; 2], output: &mut [[[f32; 32]; 36]; 2], mode: u8, mode_ext: u8) {
538 let channels = if mode == 3 { 1 } else { 2 };
539 let granules = if self.mpeg1 { 2 } else { 1 };
541 let mut end_freq = [[0; 2]; 2];
542 for gr_no in 0..granules {
543 for ch in 0..channels {
544 end_freq[gr_no][ch] = self.granule[gr_no][ch].lastcoded;
548 let mut band_flags = [[0; MP3_MAX_BANDS + 3]; 2];
549 let mut band_start = [[0; MP3_MAX_BANDS + 3]; 2];
550 let mut band_end = [[0; MP3_MAX_BANDS + 3]; 2];
552 if mode == 1 { // joint stereo
553 let mut bound_band = [0; 2];
554 let mut num_bands = [MP3_BANDS + 1; 2];
556 for (gr_no, grans) in self.granule[..granules].iter_mut().enumerate() {
557 if grans[1].block_type != 2 {
558 for band in 0..=MP3_BANDS {
559 band_flags[gr_no][band] = mode_ext;
560 band_start[gr_no][band] = MP3_SFB_LONG_OFFS[self.sf_idx][band];
561 band_end [gr_no][band] = MP3_SFB_LONG_OFFS[self.sf_idx][band + 1];
562 if (end_freq[gr_no][1] >= band_end[gr_no][band]) || (grans[1].scalefac[band.min(MP3_BANDS - 1)] == grans[1].istereo[band.min(MP3_BANDS - 1)]) {
563 band_flags[gr_no][band] &= !IS_MODE;
565 if band_start[gr_no][band] < end_freq[gr_no][1] {
566 bound_band[gr_no] = band;
568 if (band_flags[gr_no][band] & IS_MODE) == 0 {
569 for el in self.is_mode[gr_no * SAMPLES/2..][band_start[gr_no][band]..band_end[gr_no][band]].iter_mut() {
575 let switch_off = if grans[1].switch_point { 3 } else { 0 };
578 if grans[1].switch_point {
579 let long_bands = if self.mpeg1 { 8 } else { 6 };
580 for _ in 0..long_bands {
581 band_flags[gr_no][band] = mode_ext;
582 band_start[gr_no][band] = MP3_SFB_LONG_OFFS[self.sf_idx][band];
583 band_end [gr_no][band] = MP3_SFB_LONG_OFFS[self.sf_idx][band + 1];
584 if end_freq[gr_no][1] >= band_end[gr_no][band] {
585 band_flags[gr_no][band] &= !IS_MODE;
587 start = band_end[gr_no][band];
591 for sb in switch_off..=MP3_BANDS_SHORT {
592 let band_size = MP3_SFB_SHORT_SIZE[self.sf_idx][sb];
594 band_flags[gr_no][band] = mode_ext;
595 band_start[gr_no][band] = start;
596 band_end [gr_no][band] = start + band_size;
597 if end_freq[gr_no][1] >= band_end[gr_no][band] {
598 band_flags[gr_no][band] &= !IS_MODE;
604 num_bands[gr_no] = band;
607 if (mode_ext & IS_MODE) != 0 {
608 for (gr_no, grans) in self.granule[..granules].iter_mut().enumerate() {
609 let (coef0, coef1) = coeffs.split_at_mut(1);
610 let coef0 = &mut coef0[0][gr_no * SAMPLES/2..];
611 let coef1 = &mut coef1[0][gr_no * SAMPLES/2..];
612 let is_mode = &self.is_mode[gr_no * SAMPLES/2..];
613 let start = band_end[gr_no][bound_band[gr_no]];
614 let end = grans[0].zero_part.max(start);
617 let coef0 = &mut coef0[start..end];
618 let coef1 = &mut coef1[start..end];
619 let is_mode = &self.is_mode[gr_no * SAMPLES/2..][start..end];
620 for ((l, r), &is) in coef0.iter_mut().zip(coef1.iter_mut()).zip(is_mode.iter()) {
621 if is >= 0 && is < 7 {
622 let t = *l * MP3_ISTEREO_COEFFS[is as usize];
628 let iscale = (grans[1].scalefac_compress & 1) as u8;
629 for band in 0..num_bands[gr_no] {
630 if (band_flags[gr_no][band] & IS_MODE) != 0 {
631 let start = band_start[gr_no][band];
632 let end = band_end[gr_no][band];
633 apply_istereo(&mut coef0[start..end], &mut coef1[start..end], is_mode[start], iscale, (band_flags[gr_no][band] & MS_MODE) != 0);
637 end_freq[gr_no][1] = end_freq[gr_no][1].max(end);
640 if (mode_ext & MS_MODE) != 0 {
641 for (gr_no, grans) in self.granule[..granules].iter_mut().enumerate() {
642 let end = grans[0].zero_part.max(grans[1].zero_part);
643 let (coef0, coef1) = coeffs.split_at_mut(1);
644 let coef0 = &mut coef0[0][gr_no * SAMPLES/2..];
645 let coef1 = &mut coef1[0][gr_no * SAMPLES/2..];
646 for band in 0..num_bands[gr_no] {
647 if band_start[gr_no][band] >= end {
650 if band_flags[gr_no][band] == MS_MODE {
651 let start = band_start[gr_no][band];
652 let end = band_end[gr_no][band];
653 super::apply_ms(&mut coef0[start..end], &mut coef1[start..end]);
656 end_freq[gr_no][0] = end;
657 end_freq[gr_no][1] = end;
661 for (gr_no, grans) in self.granule[..granules].iter_mut().enumerate() {
662 for (ch, gr) in grans[..channels].iter_mut().enumerate() {
663 let src = &mut coeffs[ch][gr_no * SAMPLES/2..][..SAMPLES/2];
664 if gr.block_type != 2 {
665 dealias(src, SAMPLES/2);
666 self.mdct.mdct36(src, &mut self.tmp, &mut self.delay[ch], end_freq[gr_no][ch], gr.block_type);
668 let switch_off = if gr.switch_point { MP3_SFB_LONG_OFFS[self.sf_idx][if self.mpeg1 { 8 } else { 6 }] } else { 0 };
669 let mut band_buf = [0.0; 66 * 3];
670 let mut sb = if gr.switch_point { 3 } else { 0 };
671 let mut off = switch_off;
673 while sb <= MP3_BANDS_SHORT {
674 let band_size = MP3_SFB_SHORT_SIZE[self.sf_idx][sb];
676 for i in 0..band_size {
677 band_buf[win + i * 3] = src[off + win * band_size + i];
680 src[off..][..band_size * 3].copy_from_slice(&band_buf[..band_size * 3]);
681 off += band_size * 3;
685 dealias(src, switch_off);
686 self.mdct.mdct36(src, &mut self.tmp, &mut self.delay[ch], switch_off, gr.block_type);
688 self.mdct.mdct12(&src[switch_off..], &mut self.tmp[switch_off..], &mut self.delay[ch][switch_off..], end_freq[gr_no][ch] - switch_off);
691 let dst = &mut output[ch][gr_no * 18..];
692 let end = (end_freq[gr_no][ch] + 17) / 18;
695 dst[j][i] = self.tmp[i * 18 + j];
700 dst[j][i] = self.delay[ch][i * 18 + j];
702 for el in self.delay[ch][i * 18..][..18].iter_mut() {
711 fn mp3_unquant(val: u32, scale: i8) -> f32 {
712 (val as f32) * (val as f32).cbrt() * 2.0f32.powf((scale as f32) * 0.25)
715 #[allow(clippy::too_many_arguments)]
716 fn read_region(br: &mut BitReader, br_end: usize, coeffs: &mut [f32], scales: &[i8; SAMPLES/2], start: usize, end: usize, cb: &Codebook<u8>, esc_bits: u8) -> DecoderResult<usize> {
717 let mut lastcoded = 0;
718 for (i, (cpair, scpair)) in coeffs[start..end].chunks_exact_mut(2).zip(scales[start..end].chunks_exact(2)).enumerate() {
719 if br.tell() >= br_end { break; }
720 let val = br.read_cb(cb)?;
724 let a = if (val >> 4) != 0xF || esc_bits == 0 {
727 br.read(esc_bits)? + 15
730 let a = mp3_unquant(a, scpair[0]);
731 cpair[0] = if br.read_bool()? { -a } else { a };
733 let b = if (val & 0xF) != 0xF || esc_bits == 0 {
736 br.read(esc_bits)? + 15
739 let b = mp3_unquant(b, scpair[1]);
740 cpair[1] = if br.read_bool()? { -b } else { b };
742 lastcoded = start + (i + 1) * 2;
747 fn read_region_quad(br: &mut BitReader, br_end: usize, coeffs: &mut [f32], scales: &[i8; SAMPLES/2], start: usize, cb: Option<&Codebook<u8>>) -> DecoderResult<(usize, usize)> {
748 let mut lastcoded = 0;
749 let mut zero_part = 0;
750 if br.tell() >= br_end {
753 for (i, (cquad, scquad)) in coeffs[start..SAMPLES/2].chunks_exact_mut(4).zip(scales[start..].chunks_exact(4)).enumerate() {
754 zero_part = start + i * 4 + 4;
755 if br.tell() >= br_end || (br.tell() + 3 >= br_end && cb.is_none()) {
758 let val = if let Some(cbook) = cb { br.read_cb(cbook)? } else { (br.read(4)? as u8) ^ 0xF };
763 if ((val >> (3 - j)) & 1) != 0 {
764 cquad[j] = mp3_unquant(1, scquad[j]);
766 cquad[j] = -cquad[j];
768 lastcoded = start + i * 4 + j + 1;
772 Ok((lastcoded, zero_part))
775 const DCT12_0: f32 = -0.92387953251128675613;
776 const DCT12_1: f32 = -0.38268343236508977174;
778 const DCT12_2: f32 = -0.1305261922200516;
779 const DCT12_3: f32 = -0.6087614290087205;
780 const DCT12_4: f32 = 0.7933533402912348;
781 const DCT12_5: f32 = 0.9914448613738103;
783 fn dct12(src: &[f32], dst: &mut [f32]) {
784 let t0 = src[0] - src[9] - src[12];
785 let t1 = src[3] - src[6] - src[15];
787 dst[ 4] = t0 * DCT12_1 - t1 * DCT12_0;
788 dst[ 7] = t0 * DCT12_0 + t1 * DCT12_1;
792 let t0 = src[3] * DCT12_1 - src[12] * DCT12_0;
793 let t1 = src[3] * DCT12_0 + src[12] * DCT12_1;
795 dst[ 3] = src[0] * DCT12_2 + src[6] * DCT12_3 + src[9] * DCT12_4 + src[15] * DCT12_5 - t0;
796 dst[ 5] = src[0] * DCT12_3 - src[6] * DCT12_2 - src[9] * DCT12_5 + src[15] * DCT12_4 - t1;
797 dst[ 6] = -src[0] * DCT12_4 + src[6] * DCT12_5 - src[9] * DCT12_2 + src[15] * DCT12_3 - t0;
798 dst[ 8] = -src[0] * DCT12_5 - src[6] * DCT12_4 + src[9] * DCT12_3 + src[15] * DCT12_2 + t1;
805 const DCT36_PRESCALE: [f32; 18] = [
806 0.99904822158185776240, 0.99144486137381041114, 0.97629600711993336597,
807 0.95371695074822692114, 0.92387953251128675613, 0.88701083317822170105,
808 0.84339144581288570127, 0.79335334029123516458, 0.73727733681012404139,
809 0.67559020761566024435, 0.60876142900872063942, 0.53729960834682383185,
810 0.46174861323503393057, 0.38268343236508977174, 0.30070579950427312163,
811 0.21643961393810287977, 0.13052619222005159156, 0.04361938736533599979
813 const DCT36_TWIDDLE: [f32; 9] = [
814 0.99619469809174553229, 0.96592582628906828675, 0.90630778703664996324,
815 0.81915204428899178969, 0.70710678118654752440, 0.57357643635104609611,
816 0.42261826174069943619, 0.25881904510252076236, 0.08715574274765817357
818 const SDCT2_TWIDDLE: [f32; 7] = [
819 -2.0 * 0.98480775301220805936, -2.0 * 0.86602540378443864676,
820 -2.0 * 0.76604444311897803520, -2.0 * 0.64278760968653932633,
821 -2.0 * 0.34202014332566873305, -2.0 * 0.17364817766693034887,
822 2.0 * 0.93969262078590838404
825 fn sdct_ii(buf: &mut [f32]) {
826 let t0 = buf[ 6] + buf[10];
827 let t1 = buf[ 6] - buf[10];
828 let t2 = buf[12] + buf[ 4];
829 let t3 = buf[12] - buf[ 4];
830 let t4 = buf[16] + buf[ 0];
831 let t5 = buf[16] - buf[ 0];
834 let t7 = (t0 - t2) * SDCT2_TWIDDLE[2];
835 let t8 = (t1 + t3) * SDCT2_TWIDDLE[3];
837 let t0 = (t0 - t4) * SDCT2_TWIDDLE[5];
838 let t1 = (t1 - t5) * SDCT2_TWIDDLE[0];
839 let t2 = (t2 - t4) * SDCT2_TWIDDLE[6];
840 let t3 = (t3 + t5) * SDCT2_TWIDDLE[4];
843 let tb = (t9 + t5) * SDCT2_TWIDDLE[1];
845 let tc = buf[2] + buf[14];
846 let td = (buf[2] - buf[14]) * SDCT2_TWIDDLE[1];
848 let t6 = buf[8] + tc;
849 let t9 = buf[8] * 2.0 - tc;
855 buf[ 2] = t8 - td - t1;
859 buf[10] = td - t3 - t1;
860 buf[12] = ta - t6 * 2.0;
861 buf[14] = t8 + t3 + td;
865 fn dct36(src: &mut [f32], dst: &mut [f32]) {
866 for (el, &w) in src.iter_mut().zip(DCT36_PRESCALE.iter()) {
870 let mut tmp = [0.0; 18];
872 tmp[i * 2] = src[i] + src[17 - i];
873 tmp[i * 2 + 1] = (src[i] - src[17 - i]) * DCT36_TWIDDLE[i] * 2.0;
876 sdct_ii(&mut tmp[1..]);
878 for i in (3..18).step_by(2) {
879 tmp[i] -= tmp[i - 2];
883 tmp[i] = tmp[i] * 0.5 - tmp[i - 1];
886 dst[i] = -tmp[17 - i] * 2.0;
890 const ALIAS_COEFFS_S: [f32; 8] = [
891 0.85749292571254418689, 0.88174199731770518178,
892 0.94962864910273289205, 0.98331459249179014599,
893 0.99551781606758576429, 0.99916055817814750453,
894 0.99989919524444704627, 0.99999315507028023572
896 const ALIAS_COEFFS_A: [f32; 8] = [
897 -0.51449575542752651213, -0.47173196856497227225,
898 -0.31337745420390185437, -0.18191319961098117700,
899 -0.09457419252642064760, -0.04096558288530404768,
900 -0.01419856857247114805, -0.00369997467376003687
902 fn dealias(buf: &mut [f32], len: usize) {
903 for i in (18..len).step_by(18) {
904 for (j, (&cs, &ca)) in ALIAS_COEFFS_S.iter().zip(ALIAS_COEFFS_A.iter()).enumerate() {
905 let a = buf[i - j - 1];
907 let c0 = a * cs - b * ca;
908 let c1 = a * ca + b * cs;
915 fn apply_istereo(ch0: &mut [f32], ch1: &mut [f32], is_mode: i8, iscale: u8, ms_mode: bool) {
916 match (is_mode, ms_mode) {
918 for (c0, c1) in ch0.iter_mut().zip(ch1.iter_mut()) {
919 let a = (*c0 + *c1) * std::f32::consts::FRAC_1_SQRT_2;
920 let b = (*c0 - *c1) * std::f32::consts::FRAC_1_SQRT_2;
927 ch1.copy_from_slice(ch0);
930 let scale = mp3_unquant(1, -((is_mode + 1) >> 1) << iscale);
931 if (is_mode & 1) == 0 {
932 for (&c0, c1) in ch0.iter().zip(ch1.iter_mut()) {
936 for (c0, c1) in ch0.iter_mut().zip(ch1.iter_mut()) {