1 use nihav_core::codecs::{DecoderResult, DecoderError};
2 use nihav_core::io::bitreader::*;
12 scale: [[[u8; 3]; 32]; 2],
15 fn unquant(val: u8, levels: i16, scale: f32) -> f32 {
16 (((i16::from(val) << 1) + 1 - levels) as f32) * scale / (levels as f32)
19 fn read3samples(br: &mut BitReader, qbits: i8, scale: f32, band: usize, dst: &mut [[f32; 32]]) -> DecoderResult<()> {
21 let bits = qbits as u8;
22 let maxval = (1 << bits) - 1;
23 dst[0][band] = unquant(br.read(bits)? as u8, maxval, scale);
24 dst[1][band] = unquant(br.read(bits)? as u8, maxval, scale);
25 dst[2][band] = unquant(br.read(bits)? as u8, maxval, scale);
27 let samplecode = br.read(-qbits as u8)? as usize;
31 dst[i][band] = unquant(GROUP3[samplecode][i], 3, scale);
36 dst[i][band] = unquant(GROUP5[samplecode][i], 5, scale);
41 dst[i][band] = unquant(GROUP9[samplecode][i], 9, scale);
50 fn read3samples_joint(br: &mut BitReader, qbits: i8, scale0: f32, scale1: f32, band: usize, gr: usize, dst: &mut [[[f32; 32]; 36]; 2]) -> DecoderResult<()> {
52 let bits = qbits as u8;
53 let maxval = (1 << bits) - 1;
55 let val = br.read(bits)? as u8;
56 dst[0][gr * 3 + i][band] = unquant(val, maxval, scale0);
57 dst[1][gr * 3 + i][band] = unquant(val, maxval, scale1);
60 let samplecode = br.read(-qbits as u8)? as usize;
64 dst[0][gr * 3 + i][band] = unquant(GROUP3[samplecode][i], 3, scale0);
65 dst[1][gr * 3 + i][band] = unquant(GROUP3[samplecode][i], 3, scale1);
70 dst[0][gr * 3 + i][band] = unquant(GROUP5[samplecode][i], 5, scale0);
71 dst[1][gr * 3 + i][band] = unquant(GROUP5[samplecode][i], 5, scale1);
76 dst[0][gr * 3 + i][band] = unquant(GROUP9[samplecode][i], 9, scale0);
77 dst[1][gr * 3 + i][band] = unquant(GROUP9[samplecode][i], 9, scale1);
87 pub fn new() -> Self { Self::default() }
88 pub fn read_layer2(&mut self, br: &mut BitReader, channels: usize, out: &mut [[[f32; 32]; 36]; 2], mode: u8, mode_extension: u8) -> DecoderResult<()> {
89 let (ba_bits, ba_quants) = if self.mpeg1 {
91 (LAYER2_BITS[self.sf_idx][self.br_idx], LAYER2_ALLOC[self.br_idx])
93 let br_idx = HALF_BITRATE_IDX[self.br_idx] as usize;
94 (LAYER2_BITS[self.sf_idx][br_idx], LAYER2_ALLOC[br_idx])
97 (BITS_B2LFE, ALLOC_B2LFE)
99 let mut sblimit = ba_bits.len() - 1;
100 while sblimit > 0 && ba_bits[sblimit - 1] == 0 {
104 return Err(DecoderError::Bug);
107 *out = [[[0.0; 32]; 36]; 2];
109 for (bits, &b) in self.alloc[0][..sblimit].iter_mut().zip(ba_bits.iter()) {
110 *bits = br.read(b)? as u8;
112 for (scf, &alloc) in self.scfsi[0][..sblimit].iter_mut().zip(self.alloc[0].iter()) {
114 *scf = br.read(2)? as u8;
117 for (band, scales) in self.scale[0].iter_mut().take(sblimit).enumerate() {
118 if self.alloc[0][band] == 0 {
121 scales[0] = br.read(6)? as u8;
122 match self.scfsi[0][band] {
124 scales[1] = br.read(6)? as u8;
125 scales[2] = br.read(6)? as u8;
128 scales[1] = scales[0];
129 scales[2] = br.read(6)? as u8;
132 scales[1] = scales[0];
133 scales[2] = scales[0];
136 scales[1] = br.read(6)? as u8;
137 scales[2] = scales[1];
143 for band in 0..sblimit {
144 if self.alloc[0][band] == 0 {
147 let scale = QUANTS[self.scale[0][band][gr / 4] as usize];
148 let qbits = ba_quants[band][self.alloc[0][band] as usize];
149 read3samples(br, qbits, scale, band, &mut out[0][gr * 3..])?;
153 let mut bound = if mode == 1 {
154 (mode_extension as usize + 1) * 4
158 while bound > 0 && ba_bits[bound - 1] == 0 {
162 for band in 0..sblimit {
163 self.alloc[0][band] = br.read(ba_bits[band])? as u8;
165 self.alloc[1][band] = br.read(ba_bits[band])? as u8;
167 self.alloc[1][band] = self.alloc[0][band];
170 for band in 0..sblimit {
172 if self.alloc[ch][band] != 0 {
173 self.scfsi[ch][band] = br.read(2)? as u8;
177 for band in 0..sblimit {
179 if self.alloc[ch][band] == 0 {
182 let scales = &mut self.scale[ch][band];
183 scales[0] = br.read(6)? as u8;
184 match self.scfsi[ch][band] {
186 scales[1] = br.read(6)? as u8;
187 scales[2] = br.read(6)? as u8;
190 scales[1] = scales[0];
191 scales[2] = br.read(6)? as u8;
194 scales[1] = scales[0];
195 scales[2] = scales[0];
198 scales[1] = br.read(6)? as u8;
199 scales[2] = scales[1];
205 for band in 0..bound {
207 if self.alloc[ch][band] == 0 {
210 let scale = QUANTS[self.scale[ch][band][gr / 4] as usize];
211 let qbits = ba_quants[band][self.alloc[ch][band] as usize];
212 read3samples(br, qbits, scale, band, &mut out[ch][gr * 3..])?;
215 for band in bound..sblimit {
216 if self.alloc[0][band] == 0 {
219 let scale0 = QUANTS[self.scale[0][band][gr / 4] as usize];
220 let scale1 = QUANTS[self.scale[1][band][gr / 4] as usize];
221 let qbits = ba_quants[band][self.alloc[0][band] as usize];
222 read3samples_joint(br, qbits, scale0, scale1, band, gr, out)?;
229 pub fn reset(&mut self) {