]>
Commit | Line | Data |
---|---|---|
16cca4d3 KS |
1 | use nihav_core::codecs::*; |
2 | use nihav_core::io::bitreader::*; | |
3 | use nihav_core::io::codebook::*; | |
4 | ||
5 | use super::SAMPLES; | |
6 | use super::mp3data::*; | |
7 | ||
8 | const MP3_MAX_BANDS: usize = MP3_BANDS_SHORT * 3 + 4; | |
9 | const IS_MODE: u8 = 1; | |
10 | const MS_MODE: u8 = 2; | |
11 | ||
12 | #[derive(Clone,Copy)] | |
13 | struct Granule { | |
14 | part2_3_length: usize, | |
15 | big_values: usize, | |
16 | global_gain: u8, | |
17 | scalefac_compress: usize, | |
18 | blocksplit: bool, | |
19 | block_type: u8, | |
20 | switch_point: bool, | |
21 | table_select: [u8; 3], | |
22 | subblock_gain: [u8; 3], | |
23 | region_address1: u8, | |
24 | region_address2: u8, | |
25 | preflag: bool, | |
26 | scalefac_scale: bool, | |
27 | count1table_select: bool, | |
28 | scalefac: [u8; MP3_MAX_BANDS], | |
29 | istereo: [u8; MP3_MAX_BANDS], | |
30 | ||
31 | lastcoded: usize, | |
32 | zero_part: usize, | |
33 | } | |
34 | ||
35 | impl Default for Granule { | |
36 | fn default() -> Self { unsafe { std::mem::MaybeUninit::zeroed().assume_init() } } | |
37 | } | |
38 | ||
39 | impl Granule { | |
40 | fn get_mpeg2_params(&self, bits: &mut [u8; 4], independent: bool) -> usize { | |
41 | if independent { | |
42 | match self.scalefac_compress { | |
43 | 0..=399 => { | |
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; | |
48 | 0 | |
49 | }, | |
50 | 400..=499 => { | |
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; | |
55 | bits[3] = 0; | |
56 | 1 | |
57 | }, | |
58 | _ => { | |
59 | let idx = self.scalefac_compress - 500; | |
60 | bits[0] = MP3_SCF_BITS3[idx * 2]; | |
61 | bits[1] = MP3_SCF_BITS3[idx * 2 + 1]; | |
62 | bits[2] = 0; | |
63 | bits[3] = 0; | |
64 | 2 | |
65 | }, | |
66 | } | |
67 | } else { | |
68 | bits[3] = 0; | |
69 | let idx = self.scalefac_compress >> 1; | |
70 | match idx { | |
71 | 0..=179 => { | |
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]; | |
75 | 3 | |
76 | }, | |
77 | 180..=243 => { | |
78 | let val = (idx - 180) as u8; | |
79 | bits[0] = val >> 4; | |
80 | bits[1] = (val >> 2) & 3; | |
81 | bits[2] = val & 3; | |
82 | 4 | |
83 | }, | |
84 | _ => { | |
85 | let idx = idx - 244; | |
86 | bits[0] = MP3_SCF_BITS6[idx * 2]; | |
87 | bits[1] = MP3_SCF_BITS6[idx * 2 + 1]; | |
88 | bits[2] = 0; | |
89 | 5 | |
90 | }, | |
91 | } | |
92 | } | |
93 | } | |
94 | } | |
95 | ||
96 | struct MDCTContext { | |
97 | win36: [f32; 36], | |
98 | win36f: [f32; 36], | |
99 | win12: [f32; 12], | |
100 | win12f: [f32; 12], | |
101 | tmp: [f32; 36], | |
102 | } | |
103 | ||
104 | impl MDCTContext { | |
105 | fn new() -> Self { | |
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]; | |
110 | ||
111 | for i in 0..36 { | |
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] }; | |
114 | } | |
115 | for i in 0..12 { | |
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] }; | |
118 | } | |
119 | ||
120 | Self { | |
121 | tmp: [0.0; 36], | |
122 | win36, win36f, win12, win12f | |
123 | } | |
124 | } | |
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); | |
130 | match block_type { | |
131 | 0 | 2 => { | |
132 | for j in 0..9 { | |
133 | dst[i + j] = delay[i + j] - self.tmp[8 - j] * win36[j]; | |
134 | delay[i + j] = self.tmp[j + 9] * win36[j + 18]; | |
135 | } | |
136 | for j in 9..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]; | |
139 | } | |
140 | }, | |
141 | 1 => { | |
142 | for j in 0..9 { | |
143 | dst[i + j] = delay[i + j] - self.tmp[8 - j] * win36[j]; | |
144 | } | |
145 | for j in 9..18 { | |
146 | dst[i + j] = delay[i + j] + self.tmp[j - 9] * win36[j]; | |
147 | } | |
148 | delay[i..][..6].copy_from_slice(&self.tmp[9..][..6]); | |
149 | if flip { | |
150 | for j in (1..6).step_by(2) { | |
151 | delay[i + j] = -delay[i + j]; | |
152 | } | |
153 | } | |
154 | for j in 6..9 { | |
155 | delay[i + j] = self.tmp[j + 9] * win12[j]; | |
156 | } | |
157 | for j in 9..12 { | |
158 | delay[i + j] = self.tmp[26 - j] * win12[j]; | |
159 | } | |
160 | for j in 12..18 { | |
161 | delay[i + j] = 0.0; | |
162 | } | |
163 | }, | |
164 | _ => { | |
165 | dst[i..][..6].copy_from_slice(&delay[i..][..6]); | |
166 | for j in 6..9 { | |
167 | dst[i + j] = delay[i + j] - self.tmp[8 - j] * win12[j - 6]; | |
168 | } | |
169 | for j in 9..12 { | |
170 | dst[i + j] = delay[i + j] + self.tmp[j - 9] * win12[j - 6]; | |
171 | } | |
172 | if !flip { | |
173 | for j in 12..18 { | |
174 | dst[i + j] = delay[i + j] + self.tmp[j - 9]; | |
175 | } | |
176 | } else { | |
177 | for j in 12..18 { | |
178 | dst[i + j] = delay[i + j] + if (j & 1) == 0 { self.tmp[j - 9] } else { -self.tmp[j - 9] }; | |
179 | } | |
180 | } | |
181 | for j in 0..9 { | |
182 | delay[i + j] = self.tmp[j + 9] * win36[j + 18]; | |
183 | } | |
184 | for j in 9..18 { | |
185 | delay[i + j] = self.tmp[26 - j] * win36[j + 18]; | |
186 | } | |
187 | }, | |
188 | }; | |
189 | ||
190 | flip = !flip; | |
191 | } | |
192 | } | |
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 }; | |
197 | for j in 0..3 { | |
198 | let tmp = &mut self.tmp[j * 12..]; | |
77e60c6a | 199 | dct12(&src[i + j..], tmp); |
16cca4d3 KS |
200 | for (el, &w) in tmp.iter_mut().zip(window.iter()) { |
201 | *el *= w; | |
202 | } | |
203 | } | |
204 | ||
205 | for j in 0..6 { | |
206 | dst[i + j] = delay[i + j]; | |
207 | delay[i + j] = self.tmp[j + 18] + self.tmp[j + 18 + 6]; | |
208 | } | |
209 | for j in 6..12 { | |
210 | dst[i + j] = delay[i + j] + self.tmp[j - 6]; | |
211 | delay[i + j] = self.tmp[j + 18 + 6]; | |
212 | } | |
213 | for j in 12..18 { | |
214 | dst[i + j] = delay[i + j] + self.tmp[j - 6] + self.tmp[j]; | |
215 | delay[i + j] = 0.0; | |
216 | } | |
217 | ||
218 | flip = !flip; | |
219 | } | |
220 | } | |
221 | } | |
222 | ||
223 | pub struct MP3Data { | |
224 | cb: MP3Codebooks, | |
225 | mdct: MDCTContext, | |
226 | granule: [[Granule; 2]; 2], | |
227 | scfsi: [[bool; 4]; 2], | |
228 | pub main_data_end: usize, | |
229 | is_mode: [i8; SAMPLES], | |
230 | pub mpeg1: bool, | |
231 | pub sf_idx: usize, | |
232 | ||
233 | delay: [[f32; SAMPLES / 2]; 2], | |
234 | tmp: [f32; SAMPLES / 2], | |
235 | } | |
236 | ||
237 | impl MP3Data { | |
238 | pub fn new() -> Self { | |
239 | Self { | |
240 | cb: MP3Codebooks::new(), | |
241 | mdct: MDCTContext::new(), | |
242 | granule: [[Granule::default(); 2]; 2], | |
243 | scfsi: [[false; 4]; 2], | |
244 | main_data_end: 0, | |
245 | is_mode: [0; SAMPLES], | |
246 | mpeg1: false, | |
247 | sf_idx: 0, | |
248 | ||
249 | delay: [[0.0; SAMPLES / 2]; 2], | |
250 | tmp: [0.0; SAMPLES / 2], | |
251 | } | |
252 | } | |
253 | pub fn reset(&mut self) { | |
254 | for dly in self.delay.iter_mut() { | |
255 | for el in dly.iter_mut() { | |
256 | *el = 0.0; | |
257 | } | |
258 | } | |
259 | } | |
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 | |
266 | } | |
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]; | |
270 | ||
271 | // calculate scales first | |
272 | if gr.block_type != 2 { | |
273 | let mut off = 0; | |
274 | let mut sb = 0; | |
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() { | |
279 | *el = scale; | |
280 | } | |
281 | ||
282 | if ch == 1 { | |
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() { | |
286 | *el = scf as i8; | |
287 | } | |
288 | } | |
289 | } | |
290 | ||
291 | sb += 1; | |
292 | off = end; | |
293 | } | |
294 | } else { | |
295 | let end_band = if self.mpeg1 { 8 } else { 6 }; | |
296 | ||
297 | let mut off = 0; | |
298 | let mut sb = 0; | |
299 | if gr.switch_point { | |
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() { | |
304 | *el = scale; | |
305 | } | |
306 | ||
307 | if ch == 1 { | |
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() { | |
311 | *el = scf as i8; | |
312 | } | |
313 | } | |
314 | } | |
315 | sb += 1; | |
316 | off = end; | |
317 | } | |
318 | } | |
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]; | |
323 | for win in 0..3 { | |
324 | let scale = Self::calc_scale(gr, sb, ssb, gr.subblock_gain[win]); | |
325 | for el in scales[off..][..band_size].iter_mut() { | |
326 | *el = scale; | |
327 | } | |
328 | ||
329 | if ch == 1 { | |
330 | if sb == MP3_BANDS_SHORT { | |
331 | gr.scalefac[ssb] = gr.scalefac[ssb - 3]; | |
332 | gr.istereo[ssb] = gr.istereo[ssb - 3]; | |
333 | } | |
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() { | |
337 | *el = scf as i8; | |
338 | } | |
339 | } | |
340 | } | |
341 | off += band_size; | |
342 | ssb += 1; | |
343 | } | |
344 | sb += 1; | |
345 | } | |
346 | } | |
347 | ||
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 { | |
352 | 36 | |
353 | } else { | |
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); | |
357 | ||
358 | for el in coeffs[..SAMPLES/2].iter_mut() { | |
359 | *el = 0.0; | |
360 | } | |
361 | ||
362 | // read coefficients | |
363 | gr.lastcoded = 0; | |
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); | |
367 | } | |
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); | |
371 | } | |
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); | |
375 | } | |
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 }; | |
379 | ||
380 | Ok(()) | |
381 | } | |
382 | pub fn read_mp3_side_data(&mut self, br: &mut BitReader, channels: usize) -> DecoderResult<()> { | |
383 | if self.mpeg1 { | |
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()?; | |
389 | } | |
390 | } | |
391 | } else { | |
392 | self.main_data_end = br.read(8)? as usize; | |
393 | let _private_bits = br.read(channels as u8)?; | |
394 | } | |
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()?; | |
403 | if gr.blocksplit { | |
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; | |
408 | match *tsel { | |
409 | 4 | 14 => return Err(DecoderError::InvalidData), | |
410 | _ => {}, | |
411 | }; | |
412 | } | |
413 | for gain in gr.subblock_gain.iter_mut() { | |
414 | *gain = br.read(3)? as u8; | |
415 | } | |
416 | gr.region_address1 = 7; | |
417 | gr.region_address2 = 13; | |
418 | } else { | |
419 | gr.block_type = 0; | |
420 | gr.switch_point = false; | |
421 | for tsel in gr.table_select.iter_mut() { | |
422 | *tsel = br.read(5)? as u8; | |
423 | match *tsel { | |
424 | 4 | 14 => return Err(DecoderError::InvalidData), | |
425 | _ => {}, | |
426 | }; | |
427 | } | |
428 | gr.region_address1 = br.read(4)? as u8; | |
429 | gr.region_address2 = br.read(3)? as u8; | |
430 | } | |
431 | if self.mpeg1 { | |
432 | gr.preflag = br.read_bool()?; | |
433 | } else { | |
434 | gr.preflag = false; | |
435 | } | |
436 | gr.scalefac_scale = br.read_bool()?; | |
437 | gr.count1table_select = br.read_bool()? | |
438 | } | |
439 | } | |
440 | Ok(()) | |
441 | } | |
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; | |
444 | for gr_no in 0..2 { | |
445 | for ch in 0..channels { | |
446 | data_end += self.granule[gr_no][ch].part2_3_length; | |
447 | ||
448 | if self.granule[gr_no][ch].block_type != 2 { | |
449 | if gr_no != 0 { | |
450 | self.granule[1][ch].scalefac = self.granule[0][ch].scalefac; | |
451 | } else { | |
452 | for scf in self.granule[gr_no][ch].scalefac.iter_mut() { | |
453 | *scf = 0; | |
454 | } | |
455 | } | |
456 | ||
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]; | |
460 | for cb in 0..11 { | |
461 | if !self.scfsi[ch][SCFSI_FROM_BAND[cb]] || (gr_no == 0) { | |
462 | gr.scalefac[cb] = br.read(bits1)? as u8; | |
463 | } | |
464 | } | |
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; | |
468 | } | |
469 | } | |
77e60c6a KS |
470 | for scf in gr.scalefac[MP3_BANDS..].iter_mut() { |
471 | *scf = 0; | |
472 | } | |
16cca4d3 KS |
473 | for is in gr.istereo.iter_mut() { |
474 | *is = 7; | |
475 | } | |
476 | } else { | |
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 }; | |
481 | ||
482 | for scf in gr.scalefac[..pivot].iter_mut() { | |
483 | *scf = br.read(bits1)? as u8; | |
484 | } | |
485 | for scf in gr.scalefac[pivot..][..18].iter_mut() { | |
486 | *scf = br.read(bits2)? as u8; | |
487 | } | |
77e60c6a KS |
488 | for scf in gr.scalefac[pivot + 18..].iter_mut() { |
489 | *scf = 0; | |
490 | } | |
16cca4d3 KS |
491 | for is in gr.istereo.iter_mut() { |
492 | *is = 7; | |
493 | } | |
494 | } | |
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)?; | |
498 | } | |
499 | } | |
500 | Ok(()) | |
501 | } | |
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; | |
507 | ||
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 }; | |
511 | ||
512 | gr.preflag = idx == 2; | |
513 | let ends = &MP3_SCF_ENDS[idx][idx2]; | |
514 | ||
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; | |
518 | } | |
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; | |
522 | } | |
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; | |
526 | } | |
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; | |
530 | } | |
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)?; | |
534 | } | |
535 | Ok(()) | |
536 | } | |
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 }; | |
540 | ||
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; | |
545 | } | |
546 | } | |
547 | ||
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]; | |
551 | ||
552 | if mode == 1 { // joint stereo | |
553 | let mut bound_band = [0; 2]; | |
554 | let mut num_bands = [MP3_BANDS + 1; 2]; | |
555 | ||
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; | |
564 | } | |
565 | if band_start[gr_no][band] < end_freq[gr_no][1] { | |
566 | bound_band[gr_no] = band; | |
567 | } | |
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() { | |
570 | *el = -1; | |
571 | } | |
572 | } | |
573 | } | |
574 | } else { | |
575 | let switch_off = if grans[1].switch_point { 3 } else { 0 }; | |
576 | let mut start = 0; | |
577 | let mut band = 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; | |
586 | } | |
587 | start = band_end[gr_no][band]; | |
588 | band += 1; | |
589 | } | |
590 | } | |
591 | for sb in switch_off..=MP3_BANDS_SHORT { | |
592 | let band_size = MP3_SFB_SHORT_SIZE[self.sf_idx][sb]; | |
593 | for _win in 0..3 { | |
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; | |
599 | } | |
600 | start += band_size; | |
601 | band += 1; | |
602 | } | |
603 | } | |
604 | num_bands[gr_no] = band; | |
605 | } | |
606 | } | |
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]]; | |
dc82a7f3 | 614 | let end = grans[0].zero_part.max(start); |
16cca4d3 KS |
615 | |
616 | if self.mpeg1 { | |
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]; | |
623 | *l -= t; | |
624 | *r = t; | |
625 | } | |
626 | } | |
627 | } else { | |
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 { | |
dc82a7f3 KS |
631 | let start = band_start[gr_no][band]; |
632 | let end = band_end[gr_no][band]; | |
16cca4d3 KS |
633 | apply_istereo(&mut coef0[start..end], &mut coef1[start..end], is_mode[start], iscale, (band_flags[gr_no][band] & MS_MODE) != 0); |
634 | } | |
635 | } | |
636 | } | |
dc82a7f3 | 637 | end_freq[gr_no][1] = end_freq[gr_no][1].max(end); |
16cca4d3 KS |
638 | } |
639 | } | |
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 { | |
648 | break; | |
649 | } | |
dc82a7f3 | 650 | if band_flags[gr_no][band] == MS_MODE { |
16cca4d3 KS |
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]); | |
654 | } | |
655 | } | |
656 | end_freq[gr_no][0] = end; | |
657 | end_freq[gr_no][1] = end; | |
658 | } | |
659 | } | |
660 | } | |
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); | |
667 | } else { | |
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; | |
672 | ||
77e60c6a | 673 | while sb <= MP3_BANDS_SHORT { |
16cca4d3 KS |
674 | let band_size = MP3_SFB_SHORT_SIZE[self.sf_idx][sb]; |
675 | for win in 0..3 { | |
676 | for i in 0..band_size { | |
677 | band_buf[win + i * 3] = src[off + win * band_size + i]; | |
678 | } | |
679 | } | |
680 | src[off..][..band_size * 3].copy_from_slice(&band_buf[..band_size * 3]); | |
681 | off += band_size * 3; | |
682 | sb += 1; | |
683 | } | |
684 | if gr.switch_point { | |
685 | dealias(src, switch_off); | |
686 | self.mdct.mdct36(src, &mut self.tmp, &mut self.delay[ch], switch_off, gr.block_type); | |
687 | } | |
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); | |
689 | } | |
690 | ||
691 | let dst = &mut output[ch][gr_no * 18..]; | |
692 | let end = (end_freq[gr_no][ch] + 17) / 18; | |
693 | for i in 0..end { | |
694 | for j in 0..18 { | |
695 | dst[j][i] = self.tmp[i * 18 + j]; | |
696 | } | |
697 | } | |
698 | for i in end..32 { | |
699 | for j in 0..18 { | |
700 | dst[j][i] = self.delay[ch][i * 18 + j]; | |
701 | } | |
702 | for el in self.delay[ch][i * 18..][..18].iter_mut() { | |
703 | *el = 0.0; | |
704 | } | |
705 | } | |
706 | } | |
707 | } | |
708 | } | |
709 | } | |
710 | ||
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) | |
713 | } | |
714 | ||
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)?; | |
721 | if val == 0 { | |
722 | continue; | |
723 | } | |
724 | let a = if (val >> 4) != 0xF || esc_bits == 0 { | |
725 | u32::from(val >> 4) | |
726 | } else { | |
727 | br.read(esc_bits)? + 15 | |
728 | }; | |
729 | if a != 0 { | |
730 | let a = mp3_unquant(a, scpair[0]); | |
731 | cpair[0] = if br.read_bool()? { -a } else { a }; | |
732 | } | |
733 | let b = if (val & 0xF) != 0xF || esc_bits == 0 { | |
734 | u32::from(val & 0xF) | |
735 | } else { | |
736 | br.read(esc_bits)? + 15 | |
737 | }; | |
738 | if b != 0 { | |
739 | let b = mp3_unquant(b, scpair[1]); | |
740 | cpair[1] = if br.read_bool()? { -b } else { b }; | |
741 | } | |
742 | lastcoded = start + (i + 1) * 2; | |
743 | } | |
744 | Ok(lastcoded) | |
745 | } | |
746 | ||
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 { | |
751 | return Ok((0, 0)); | |
752 | } | |
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; | |
b93ed625 | 755 | if br.tell() >= br_end || (br.tell() + 3 >= br_end && cb.is_none()) { |
16cca4d3 KS |
756 | break; |
757 | } | |
758 | let val = if let Some(cbook) = cb { br.read_cb(cbook)? } else { (br.read(4)? as u8) ^ 0xF }; | |
759 | if val == 0 { | |
760 | continue; | |
761 | } | |
762 | for j in 0..4 { | |
763 | if ((val >> (3 - j)) & 1) != 0 { | |
764 | cquad[j] = mp3_unquant(1, scquad[j]); | |
765 | if br.read_bool()? { | |
766 | cquad[j] = -cquad[j]; | |
767 | } | |
768 | lastcoded = start + i * 4 + j + 1; | |
769 | } | |
770 | } | |
771 | } | |
772 | Ok((lastcoded, zero_part)) | |
773 | } | |
774 | ||
775 | const DCT12_0: f32 = -0.92387953251128675613; | |
776 | const DCT12_1: f32 = -0.38268343236508977174; | |
777 | ||
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; | |
782 | ||
783 | fn dct12(src: &[f32], dst: &mut [f32]) { | |
77e60c6a KS |
784 | let t0 = src[0] - src[9] - src[12]; |
785 | let t1 = src[3] - src[6] - src[15]; | |
16cca4d3 KS |
786 | |
787 | dst[ 4] = t0 * DCT12_1 - t1 * DCT12_0; | |
788 | dst[ 7] = t0 * DCT12_0 + t1 * DCT12_1; | |
789 | dst[ 1] = -dst[4]; | |
77e60c6a | 790 | dst[10] = dst[7]; |
16cca4d3 | 791 | |
77e60c6a KS |
792 | let t0 = src[3] * DCT12_1 - src[12] * DCT12_0; |
793 | let t1 = src[3] * DCT12_0 + src[12] * DCT12_1; | |
16cca4d3 | 794 | |
77e60c6a KS |
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; | |
16cca4d3 KS |
799 | dst[ 2] = -dst[3]; |
800 | dst[ 0] = -dst[5]; | |
801 | dst[11] = dst[6]; | |
802 | dst[ 9] = dst[8]; | |
803 | } | |
804 | ||
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 | |
812 | ]; | |
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 | |
817 | ]; | |
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 | |
823 | ]; | |
824 | ||
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]; | |
832 | ||
833 | let t6 = t0 + t2; | |
834 | let t7 = (t0 - t2) * SDCT2_TWIDDLE[2]; | |
835 | let t8 = (t1 + t3) * SDCT2_TWIDDLE[3]; | |
836 | let t9 = t1 - t3; | |
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]; | |
841 | ||
842 | let ta = t6 + t4; | |
843 | let tb = (t9 + t5) * SDCT2_TWIDDLE[1]; | |
844 | ||
845 | let tc = buf[2] + buf[14]; | |
846 | let td = (buf[2] - buf[14]) * SDCT2_TWIDDLE[1]; | |
847 | ||
848 | let t6 = buf[8] + tc; | |
849 | let t9 = buf[8] * 2.0 - tc; | |
850 | let te = t9 + t2; | |
851 | let tf = t9 - t2; | |
852 | let t9 = t9 + t0; | |
853 | ||
854 | buf[ 0] = ta + t6; | |
855 | buf[ 2] = t8 - td - t1; | |
856 | buf[ 4] = t7 - te; | |
857 | buf[ 6] = tb; | |
858 | buf[ 8] = tf - t0; | |
859 | buf[10] = td - t3 - t1; | |
860 | buf[12] = ta - t6 * 2.0; | |
861 | buf[14] = t8 + t3 + td; | |
862 | buf[16] = t9 + t7; | |
863 | } | |
864 | ||
865 | fn dct36(src: &mut [f32], dst: &mut [f32]) { | |
866 | for (el, &w) in src.iter_mut().zip(DCT36_PRESCALE.iter()) { | |
867 | *el *= w; | |
868 | } | |
869 | ||
870 | let mut tmp = [0.0; 18]; | |
871 | for i in 0..9 { | |
872 | tmp[i * 2] = src[i] + src[17 - i]; | |
873 | tmp[i * 2 + 1] = (src[i] - src[17 - i]) * DCT36_TWIDDLE[i] * 2.0; | |
874 | } | |
875 | sdct_ii(&mut tmp); | |
876 | sdct_ii(&mut tmp[1..]); | |
877 | ||
878 | for i in (3..18).step_by(2) { | |
879 | tmp[i] -= tmp[i - 2]; | |
880 | } | |
881 | tmp[0] *= 0.5; | |
882 | for i in 1..18 { | |
883 | tmp[i] = tmp[i] * 0.5 - tmp[i - 1]; | |
884 | } | |
885 | for i in 0..18 { | |
886 | dst[i] = -tmp[17 - i] * 2.0; | |
887 | } | |
888 | } | |
889 | ||
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 | |
895 | ]; | |
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 | |
901 | ]; | |
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]; | |
906 | let b = buf[i + j]; | |
907 | let c0 = a * cs - b * ca; | |
908 | let c1 = a * ca + b * cs; | |
909 | buf[i - j - 1] = c0; | |
910 | buf[i + j] = c1; | |
911 | } | |
912 | } | |
913 | } | |
914 | ||
915 | fn apply_istereo(ch0: &mut [f32], ch1: &mut [f32], is_mode: i8, iscale: u8, ms_mode: bool) { | |
916 | match (is_mode, ms_mode) { | |
917 | (-1, true) => { | |
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; | |
921 | *c0 = a; | |
922 | *c1 = b; | |
923 | } | |
924 | }, | |
925 | (-1, false) => {}, | |
926 | (0, _) => { | |
927 | ch1.copy_from_slice(ch0); | |
928 | }, | |
929 | _ => { | |
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()) { | |
933 | *c1 = c0 * scale; | |
934 | } | |
935 | } else { | |
936 | for (c0, c1) in ch0.iter_mut().zip(ch1.iter_mut()) { | |
937 | *c1 = *c0; | |
938 | *c0 *= scale; | |
939 | } | |
940 | } | |
941 | }, | |
942 | }; | |
943 | } |