]>
Commit | Line | Data |
---|---|---|
9f9a08fb KS |
1 | use nihav_core::codecs::{DecoderResult, DecoderError}; |
2 | use nihav_core::io::bitreader::*; | |
3 | use super::mp2data::*; | |
4 | ||
5 | #[derive(Default)] | |
6 | pub struct MP2Data { | |
7 | pub mpeg1: bool, | |
8 | pub sf_idx: usize, | |
9 | pub br_idx: usize, | |
10 | alloc: [[u8; 32]; 2], | |
11 | scfsi: [[u8; 32]; 2], | |
12 | scale: [[[u8; 3]; 32]; 2], | |
13 | } | |
14 | ||
15 | fn unquant(val: u8, levels: i16, scale: f32) -> f32 { | |
16 | (((i16::from(val) << 1) + 1 - levels) as f32) * scale / (levels as f32) | |
17 | } | |
18 | ||
19 | fn read3samples(br: &mut BitReader, qbits: i8, scale: f32, band: usize, dst: &mut [[f32; 32]]) -> DecoderResult<()> { | |
20 | if qbits > 0 { | |
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); | |
26 | } else { | |
27 | let samplecode = br.read(-qbits as u8)? as usize; | |
28 | match qbits { | |
29 | -5 => { | |
30 | for i in 0..3 { | |
31 | dst[i][band] = unquant(GROUP3[samplecode][i], 3, scale); | |
32 | } | |
33 | }, | |
34 | -7 => { | |
35 | for i in 0..3 { | |
36 | dst[i][band] = unquant(GROUP5[samplecode][i], 5, scale); | |
37 | } | |
38 | }, | |
39 | -10 => { | |
40 | for i in 0..3 { | |
41 | dst[i][band] = unquant(GROUP9[samplecode][i], 9, scale); | |
42 | } | |
43 | }, | |
44 | _ => unreachable!(), | |
45 | }; | |
46 | } | |
47 | Ok(()) | |
48 | } | |
49 | ||
50 | fn read3samples_joint(br: &mut BitReader, qbits: i8, scale0: f32, scale1: f32, band: usize, gr: usize, dst: &mut [[[f32; 32]; 36]; 2]) -> DecoderResult<()> { | |
51 | if qbits > 0 { | |
52 | let bits = qbits as u8; | |
53 | let maxval = (1 << bits) - 1; | |
54 | for i in 0..3 { | |
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); | |
58 | } | |
59 | } else { | |
60 | let samplecode = br.read(-qbits as u8)? as usize; | |
61 | match qbits { | |
62 | -5 => { | |
63 | for i in 0..3 { | |
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); | |
66 | } | |
67 | }, | |
68 | -7 => { | |
69 | for i in 0..3 { | |
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); | |
72 | } | |
73 | }, | |
74 | -10 => { | |
75 | for i in 0..3 { | |
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); | |
78 | } | |
79 | }, | |
80 | _ => unreachable!(), | |
81 | }; | |
82 | } | |
83 | Ok(()) | |
84 | } | |
85 | ||
86 | impl MP2Data { | |
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 { | |
90 | if channels == 1 { | |
91 | (LAYER2_BITS[self.sf_idx][self.br_idx], LAYER2_ALLOC[self.br_idx]) | |
92 | } else { | |
93 | let br_idx = HALF_BITRATE_IDX[self.br_idx] as usize; | |
94 | (LAYER2_BITS[self.sf_idx][br_idx], LAYER2_ALLOC[br_idx]) | |
95 | } | |
96 | } else { | |
97 | (BITS_B2LFE, ALLOC_B2LFE) | |
98 | }; | |
99 | let mut sblimit = ba_bits.len() - 1; | |
100 | while sblimit > 0 && ba_bits[sblimit - 1] == 0 { | |
101 | sblimit -= 1; | |
102 | } | |
103 | if sblimit == 0 { | |
104 | return Err(DecoderError::Bug); | |
105 | } | |
106 | ||
107 | *out = [[[0.0; 32]; 36]; 2]; | |
108 | if channels == 1 { | |
109 | for (bits, &b) in self.alloc[0][..sblimit].iter_mut().zip(ba_bits.iter()) { | |
110 | *bits = br.read(b)? as u8; | |
111 | } | |
112 | for (scf, &alloc) in self.scfsi[0][..sblimit].iter_mut().zip(self.alloc[0].iter()) { | |
113 | if alloc != 0 { | |
114 | *scf = br.read(2)? as u8; | |
115 | } | |
116 | } | |
117 | for (band, scales) in self.scale[0].iter_mut().take(sblimit).enumerate() { | |
118 | if self.alloc[0][band] == 0 { | |
119 | continue; | |
120 | } | |
121 | scales[0] = br.read(6)? as u8; | |
122 | match self.scfsi[0][band] { | |
123 | 0 => { | |
124 | scales[1] = br.read(6)? as u8; | |
125 | scales[2] = br.read(6)? as u8; | |
126 | }, | |
127 | 1 => { | |
128 | scales[1] = scales[0]; | |
129 | scales[2] = br.read(6)? as u8; | |
130 | }, | |
131 | 2 => { | |
132 | scales[1] = scales[0]; | |
133 | scales[2] = scales[0]; | |
134 | }, | |
135 | _ => { | |
136 | scales[1] = br.read(6)? as u8; | |
137 | scales[2] = scales[1]; | |
138 | }, | |
139 | }; | |
140 | } | |
141 | ||
142 | for gr in 0..12 { | |
143 | for band in 0..sblimit { | |
144 | if self.alloc[0][band] == 0 { | |
145 | continue; | |
146 | } | |
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..])?; | |
150 | } | |
151 | } | |
152 | } else { | |
153 | let mut bound = if mode == 1 { | |
154 | (mode_extension as usize + 1) * 4 | |
155 | } else { | |
156 | sblimit | |
157 | }; | |
158 | while bound > 0 && ba_bits[bound - 1] == 0 { | |
159 | bound -= 1; | |
160 | } | |
161 | ||
162 | for band in 0..sblimit { | |
163 | self.alloc[0][band] = br.read(ba_bits[band])? as u8; | |
164 | if band < bound { | |
165 | self.alloc[1][band] = br.read(ba_bits[band])? as u8; | |
166 | } else { | |
167 | self.alloc[1][band] = self.alloc[0][band]; | |
168 | } | |
169 | } | |
170 | for band in 0..sblimit { | |
171 | for ch in 0..2 { | |
172 | if self.alloc[ch][band] != 0 { | |
173 | self.scfsi[ch][band] = br.read(2)? as u8; | |
174 | } | |
175 | } | |
176 | } | |
177 | for band in 0..sblimit { | |
178 | for ch in 0..2 { | |
179 | if self.alloc[ch][band] == 0 { | |
180 | continue; | |
181 | } | |
182 | let scales = &mut self.scale[ch][band]; | |
183 | scales[0] = br.read(6)? as u8; | |
184 | match self.scfsi[ch][band] { | |
185 | 0 => { | |
186 | scales[1] = br.read(6)? as u8; | |
187 | scales[2] = br.read(6)? as u8; | |
188 | }, | |
189 | 1 => { | |
190 | scales[1] = scales[0]; | |
191 | scales[2] = br.read(6)? as u8; | |
192 | }, | |
193 | 2 => { | |
194 | scales[1] = scales[0]; | |
195 | scales[2] = scales[0]; | |
196 | }, | |
197 | _ => { | |
198 | scales[1] = br.read(6)? as u8; | |
199 | scales[2] = scales[1]; | |
200 | }, | |
201 | }; | |
202 | } | |
203 | } | |
204 | for gr in 0..12 { | |
205 | for band in 0..bound { | |
206 | for ch in 0..2 { | |
207 | if self.alloc[ch][band] == 0 { | |
208 | continue; | |
209 | } | |
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..])?; | |
213 | } | |
214 | } | |
215 | for band in bound..sblimit { | |
216 | if self.alloc[0][band] == 0 { | |
217 | continue; | |
218 | } | |
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)?; | |
223 | } | |
224 | } | |
225 | } | |
226 | ||
227 | Ok(()) | |
228 | } | |
229 | pub fn reset(&mut self) { | |
230 | } | |
231 | } |