]>
Commit | Line | Data |
---|---|---|
1 | use nihav_core::codecs::*; | |
2 | use nihav_core::io::codebook::*; | |
3 | use nihav_codec_support::dsp::qmf::QMF; | |
4 | use super::qdmcommon::*; | |
5 | use super::qdm2::TONE_SCALES; | |
6 | ||
7 | const MAX_BANDS: usize = 30; | |
8 | ||
9 | const NOISE_TAB_LEN: usize = 3840; | |
10 | ||
11 | struct NoiseGen { | |
12 | noise_tab: [f32; NOISE_TAB_LEN], | |
13 | idx: usize, | |
14 | } | |
15 | ||
16 | impl NoiseGen { | |
17 | fn new() -> Self { | |
18 | let mut noise_tab = [0.0; NOISE_TAB_LEN]; | |
19 | let mut rnd = RNG::new(); | |
20 | for el in noise_tab.iter_mut() { | |
21 | *el = rnd.next_float() * 1.3; | |
22 | } | |
23 | ||
24 | Self { noise_tab, idx: 0 } | |
25 | } | |
26 | fn next(&mut self, band: usize) -> f32 { | |
27 | let ret = self.noise_tab[self.idx]; | |
28 | self.idx += 1; | |
29 | if self.idx >= NOISE_TAB_LEN { | |
30 | self.idx -= NOISE_TAB_LEN; | |
31 | } | |
32 | ret * SB_NOISE_ATTENUATION[band] | |
33 | } | |
34 | } | |
35 | ||
36 | struct Tables { | |
37 | noise_samples: [f32; 128], | |
38 | mod3: [[u8; 5]; 243], | |
39 | mod5: [[u8; 3]; 125], | |
40 | } | |
41 | ||
42 | impl Tables { | |
43 | fn new() -> Self { | |
44 | let mut noise_samples = [0.0; 128]; | |
45 | let mut rnd = RNG::new(); | |
46 | for el in noise_samples.iter_mut() { | |
47 | *el = rnd.next_float(); | |
48 | } | |
49 | ||
50 | let mut mod3 = [[0u8; 5]; 243]; | |
51 | for (i, row) in mod3.iter_mut().enumerate() { | |
52 | let mut base = 81u8; | |
53 | let mut low = i as u8; | |
54 | for el in row.iter_mut() { | |
55 | *el = low / base; | |
56 | low %= base; | |
57 | base /= 3; | |
58 | } | |
59 | } | |
60 | ||
61 | let mut mod5 = [[0u8; 3]; 125]; | |
62 | for (i, row) in mod5.iter_mut().enumerate() { | |
63 | let mut base = 25u8; | |
64 | let mut low = i as u8; | |
65 | for el in row.iter_mut() { | |
66 | *el = low / base; | |
67 | low %= base; | |
68 | base /= 5; | |
69 | } | |
70 | } | |
71 | ||
72 | Self { | |
73 | noise_samples, mod3, mod5, | |
74 | } | |
75 | } | |
76 | } | |
77 | ||
78 | struct Codebooks { | |
79 | level_cb: Codebook<u8>, | |
80 | level_diff_cb: Codebook<u8>, | |
81 | run_cb: Codebook<u8>, | |
82 | tone_idx_mid_cb: Codebook<u8>, | |
83 | tone_idx_high1_cb: Codebook<u8>, | |
84 | tone_idx_high2_cb: Codebook<u8>, | |
85 | type30_codes_cb: Codebook<u8>, | |
86 | type34_codes_cb: Codebook<u8>, | |
87 | } | |
88 | ||
89 | fn map_idx(idx: usize) -> u8 { idx as u8 } | |
90 | ||
91 | macro_rules! create_codebook { | |
92 | ($codes: expr, $bits: expr) => ({ | |
93 | let mut cbr = TableCodebookDescReader::new($codes, $bits, map_idx); | |
94 | Codebook::new(&mut cbr, CodebookMode::LSB).unwrap() | |
95 | }) | |
96 | } | |
97 | ||
98 | impl Codebooks { | |
99 | fn new() -> Self { | |
100 | let level_cb = create_codebook!(LEVEL_CODES, LEVEL_BITS); | |
101 | let level_diff_cb = create_codebook!(LEVEL_DIFF_CODES, LEVEL_DIFF_BITS); | |
102 | let run_cb = create_codebook!(RUN_CODES, RUN_BITS); | |
103 | ||
104 | let tone_idx_mid_cb = create_codebook!(TONE_IDX_MID_CODES, TONE_IDX_MID_BITS); | |
105 | let tone_idx_high1_cb = create_codebook!(TONE_IDX_HIGH1_CODES, TONE_IDX_HIGH1_BITS); | |
106 | let tone_idx_high2_cb = create_codebook!(TONE_IDX_HIGH2_CODES, TONE_IDX_HIGH2_BITS); | |
107 | ||
108 | let type30_codes_cb = create_codebook!(TYPE30_CODES, TYPE30_BITS); | |
109 | let type34_codes_cb = create_codebook!(TYPE34_CODES, TYPE34_BITS); | |
110 | Self { | |
111 | level_cb, level_diff_cb, run_cb, | |
112 | tone_idx_mid_cb, tone_idx_high1_cb, tone_idx_high2_cb, | |
113 | type30_codes_cb, type34_codes_cb, | |
114 | } | |
115 | } | |
116 | } | |
117 | ||
118 | pub struct QDM2QMF { | |
119 | qmf: [QMF; 2], | |
120 | cbs: Codebooks, | |
121 | tables: Tables, | |
122 | noisegen: NoiseGen, | |
123 | ||
124 | pub is_intra: bool, | |
125 | ||
126 | num_bands: usize, | |
127 | subsampling: u8, | |
128 | cm_selector: usize, | |
129 | coef_per_sb_sel: usize, | |
130 | channels: usize, | |
131 | ||
132 | grid_2_quant: [[[i8; 8]; 10]; 2], | |
133 | grid_1_quant: [[[[i8; 8]; 8]; 3]; 2], | |
134 | grid_3_quant: [[i8; MAX_BANDS]; 2], | |
135 | tone_idx_mid: [[[i8; 8]; MAX_BANDS]; 2], | |
136 | tone_idx_base: [[[i8; 8]; MAX_BANDS]; 2], | |
137 | tone_idx: [[[i8; 64]; MAX_BANDS]; 2], | |
138 | quant_weight: [[[u8; 64]; MAX_BANDS]; 2], | |
139 | sb_samples: [[[f32; 32]; 128]; 2], | |
140 | tone_scale: [[[f32; 64]; MAX_BANDS]; 2], | |
141 | } | |
142 | ||
143 | impl QDM2QMF { | |
144 | pub fn new() -> Self { | |
145 | Self { | |
146 | qmf: [QMF::new(), QMF::new()], | |
147 | cbs: Codebooks::new(), | |
148 | tables: Tables::new(), | |
149 | noisegen: NoiseGen::new(), | |
150 | ||
151 | num_bands: 0, | |
152 | subsampling: 0, | |
153 | cm_selector: 0, | |
154 | coef_per_sb_sel: 0, | |
155 | channels: 0, | |
156 | is_intra: false, | |
157 | ||
158 | grid_2_quant: [[[0; 8]; 10]; 2], | |
159 | grid_1_quant: [[[[0; 8]; 8]; 3]; 2], | |
160 | grid_3_quant: [[0; MAX_BANDS]; 2], | |
161 | tone_idx_mid: [[[0; 8]; MAX_BANDS]; 2], | |
162 | tone_idx_base: [[[0; 8]; MAX_BANDS]; 2], | |
163 | tone_idx: [[[0; 64]; MAX_BANDS]; 2], | |
164 | quant_weight: [[[0; 64]; MAX_BANDS]; 2], | |
165 | sb_samples: [[[0.0; 32]; 128]; 2], | |
166 | tone_scale: [[[0.0; 64]; MAX_BANDS]; 2], | |
167 | } | |
168 | } | |
169 | pub fn set_ch_and_subsampling(&mut self, channels: usize, subsampling: u8, full_bitrate: u32) { | |
170 | self.channels = channels; | |
171 | self.subsampling = subsampling; | |
172 | self.num_bands = (8 << self.subsampling).min(MAX_BANDS); | |
173 | ||
174 | let br = match self.subsampling * 2 + (channels as u8) - 1 { | |
175 | 0 => 40, | |
176 | 1 => 48, | |
177 | 2 => 56, | |
178 | 3 => 72, | |
179 | 4 => 80, | |
180 | 5 => 100, | |
181 | _ => unreachable!(), | |
182 | }; | |
183 | self.cm_selector = if br * 1000 < full_bitrate { | |
184 | 1 | |
185 | } else if br * 1440 < full_bitrate { | |
186 | 2 | |
187 | } else if br * 1760 < full_bitrate { | |
188 | 3 | |
189 | } else if br * 2240 < full_bitrate { | |
190 | 4 | |
191 | } else { | |
192 | 0 | |
193 | }; | |
194 | self.coef_per_sb_sel = if full_bitrate <= 8000 { 0 } else if full_bitrate < 16000 { 1 } else { 2 }; | |
195 | } | |
196 | fn average_grid_quants(&mut self) { | |
197 | let ncoef = (COEFFS_PER_SB_AVG[self.coef_per_sb_sel][self.subsampling as usize] as usize) + 1; | |
198 | for ch in 0..self.channels { | |
199 | for i in 0..ncoef { | |
200 | let mut sum = 0; | |
201 | for el in self.grid_2_quant[ch][i].iter() { | |
202 | sum += i16::from(*el); | |
203 | } | |
204 | sum /= 8; | |
205 | if sum > 0 { | |
206 | sum -= 1; | |
207 | } | |
208 | self.grid_2_quant[ch][i] = [sum as i8; 8]; | |
209 | } | |
210 | } | |
211 | } | |
212 | fn read_array(br: &mut QdmBitReader, dst: &mut [i8; 8], codebooks: &Codebooks) -> DecoderResult<()> { | |
213 | if br.left() < 15 { return Ok(()); } | |
214 | let mut last = br.read_code(&codebooks.level_cb)? as i32; | |
215 | dst[0] = last as i8; | |
216 | let mut idx = 1; | |
217 | while idx < 8 { | |
218 | let len = (br.read_code(&codebooks.run_cb)? as usize) + 1; | |
219 | let diff = br.read_code(&codebooks.level_diff_cb)? as i32; | |
220 | let diff = to_signed(diff); | |
221 | let val = last + diff; | |
222 | validate!(len + idx <= 8); | |
223 | for i in 1..=len { | |
224 | dst[idx] = (last + (i as i32) * diff / (len as i32)) as i8; | |
225 | idx += 1; | |
226 | } | |
227 | last = val; | |
228 | } | |
229 | Ok(()) | |
230 | } | |
231 | pub fn read_type_9(&mut self, br: &mut QdmBitReader) -> DecoderResult<()> { | |
232 | let nbands = (COEFFS_PER_SB_AVG[self.coef_per_sb_sel][self.subsampling as usize] as usize) + 1; | |
233 | for i in 1..nbands { | |
234 | for ch in 0..self.channels { | |
235 | Self::read_array(br, &mut self.grid_2_quant[ch][i], &self.cbs)?; | |
236 | } | |
237 | } | |
238 | for ch in 0..self.channels { | |
239 | self.grid_2_quant[ch][0] = [0; 8]; | |
240 | } | |
241 | Ok(()) | |
242 | } | |
243 | pub fn read_type_10(&mut self, br: &mut QdmBitReader) -> DecoderResult<()> { | |
244 | for ch in 0..self.channels { | |
245 | let _ret = Self::read_array(br, &mut self.grid_2_quant[ch][0], &self.cbs); | |
246 | if br.left() < 16 { | |
247 | self.grid_2_quant[ch][0] = [0; 8]; | |
248 | break; | |
249 | } | |
250 | } | |
251 | ||
252 | let n = (self.subsampling as usize) + 1; | |
253 | for band in 0..n { | |
254 | for ch in 0..self.channels { | |
255 | for i in 0..8 { | |
256 | if br.read_bool() { | |
257 | for el in self.grid_1_quant[ch][band][i].iter_mut() { | |
258 | *el = br.read_code(&self.cbs.tone_idx_high1_cb)? as i8; | |
259 | } | |
260 | } else { | |
261 | self.grid_1_quant[ch][band][i] = [0; 8]; | |
262 | } | |
263 | } | |
264 | } | |
265 | } | |
266 | for band in 0..self.num_bands - 4 { | |
267 | for ch in 0..self.channels { | |
268 | if br.left() < 16 { break; } | |
269 | self.grid_3_quant[ch][band] = br.read_code(&self.cbs.tone_idx_high2_cb)? as i8; | |
270 | if band > 19 { | |
271 | self.grid_3_quant[ch][band] -= 16; | |
272 | } else { | |
273 | self.tone_idx_mid[ch][band] = [-16; 8]; | |
274 | } | |
275 | } | |
276 | } | |
277 | for band in 0..self.num_bands - 5 { | |
278 | for ch in 0..self.channels { | |
279 | for i in 0..8 { | |
280 | if br.left() < 16 { break; } | |
281 | self.tone_idx_mid[ch][band][i] = (br.read_code(&self.cbs.tone_idx_mid_cb)? as i8) - 32; | |
282 | } | |
283 | } | |
284 | } | |
285 | ||
286 | self.set_tone_scales(true); | |
287 | Ok(()) | |
288 | } | |
289 | fn inc_quant_weight(&mut self, band: usize) -> bool { | |
290 | let rlen = 128 / self.channels; | |
291 | for ch in 0..self.channels { | |
292 | let mut idx = 0; | |
293 | while idx < rlen { | |
294 | if self.quant_weight[ch][band][idx] < 8 { | |
295 | return false; | |
296 | } | |
297 | let (val, run) = match self.quant_weight[ch][band][idx] { | |
298 | 8 => (10, 16), | |
299 | 10 => (16, 1), | |
300 | 16 => (24, 5), | |
301 | 24 => (30, 3), | |
302 | 30 => (30, 1), | |
303 | _ => (8, 1), | |
304 | }; | |
305 | let len = run.min(rlen - idx); | |
306 | let ref_val = self.quant_weight[ch][band][idx]; | |
307 | for _ in 0..len { | |
308 | if self.quant_weight[ch][band + idx / 64][idx % 64] > ref_val { | |
309 | self.quant_weight[ch][band][idx] = val; | |
310 | unimplemented!(); | |
311 | } | |
312 | idx += 1; | |
313 | } | |
314 | } | |
315 | } | |
316 | true | |
317 | } | |
318 | fn set_tone_scales(&mut self, has_data: bool) { | |
319 | const LAST_COEFF: [usize; 3] = [ 4, 7, 10 ]; | |
320 | ||
321 | let csel = self.coef_per_sb_sel; | |
322 | for ch in 0..self.channels { | |
323 | for band in 0..MAX_BANDS { | |
324 | for i in 0..8 { | |
325 | let csb = COEFFS_PER_SB_DEQUANT[csel][band] as usize; | |
326 | let mut q = i32::from(self.grid_2_quant[ch][csb][i]) * i32::from(DEQUANT[csel][csb][band]); | |
327 | if csb < LAST_COEFF[csel] - 1 { | |
328 | q += i32::from(self.grid_2_quant[ch][csb + 1][i]) * i32::from(DEQUANT[csel][csb + 1][band]); | |
329 | } | |
330 | if q < 0 { | |
331 | q += 255; | |
332 | } | |
333 | self.tone_idx_base[ch][band][i] = (q / 256) as i8; | |
334 | } | |
335 | } | |
336 | } | |
337 | if !self.is_intra && !has_data { | |
338 | for band in 0..self.num_bands { | |
339 | for ch in 0..self.channels { | |
340 | for i in 0..64 { | |
341 | self.tone_idx[ch][band][i] = self.tone_idx[ch][band][i / 8]; | |
342 | if self.tone_idx[ch][band][i] >= 0 { | |
343 | self.tone_scale[ch][band][i] = TONE_SCALES[0][(self.tone_idx[ch][band][i] & 0x3F) as usize]; | |
344 | } else { | |
345 | self.tone_scale[ch][band][i] = 0.0; | |
346 | } | |
347 | } | |
348 | } | |
349 | } | |
350 | } else { | |
351 | for band in 0..self.num_bands { | |
352 | for ch in 0..self.channels { | |
353 | for i in 0..64 { | |
354 | let mut q = self.tone_idx_base[ch][band][i / 8]; | |
355 | if band >= 4 { | |
356 | q = q.wrapping_sub(self.grid_1_quant[ch][(band / 8).min(2)][i / 8][i % 8]); | |
357 | if band < 24 { | |
358 | q = q.wrapping_sub(self.tone_idx_mid[ch][band - 4][i / 8]); | |
359 | } | |
360 | q = q.wrapping_sub(self.grid_3_quant[ch][band - 4]); | |
361 | } | |
362 | self.tone_idx[ch][band][i] = q; | |
363 | if q > 0 || (self.is_intra && q == 0) { | |
364 | self.tone_scale[ch][band][i] = TONE_SCALES[0][(q & 0x3F) as usize]; | |
365 | } else { | |
366 | self.tone_scale[ch][band][i] = 0.0; | |
367 | } | |
368 | } | |
369 | } | |
370 | } | |
371 | } | |
372 | } | |
373 | #[allow(clippy::cognitive_complexity)] | |
374 | fn read_noise_band(&mut self, br: &mut QdmBitReader, ch: usize, band: usize, samples: &mut [f32; 10], signs: &[bool; 16], jstereo: bool) -> DecoderResult<()> { | |
375 | let mut type34_first = true; | |
376 | let mut type34_pred = 0.0; | |
377 | let mut type34_scale = 0.0; | |
378 | let zero_coding = br.read_bool(); | |
379 | let mut idx = 0; | |
380 | while idx < 128 { | |
381 | let len; | |
382 | match self.quant_weight[ch][band][idx / 2] { | |
383 | 8 => { | |
384 | if br.left() >= 10 { | |
385 | if zero_coding { | |
386 | for i in 0..5 { | |
387 | if idx + i * 2 >= 128 { break; } | |
388 | samples[i * 2] = if br.read_bool() { | |
389 | let ix = (br.read(1) as usize) * 2; | |
390 | QUANT_1BIT[jstereo as usize][ix] | |
391 | } else { 0.0 }; | |
392 | } | |
393 | } else { | |
394 | let idx = br.read(8) as usize; | |
395 | validate!(idx < self.tables.mod3.len()); | |
396 | for i in 0..5 { | |
397 | let k = self.tables.mod3[idx][i] as usize; | |
398 | samples[i * 2] = QUANT_1BIT[jstereo as usize][k]; | |
399 | } | |
400 | } | |
401 | for el in samples.chunks_mut(2) { | |
402 | el[1] = self.noisegen.next(band); | |
403 | } | |
404 | } else { | |
405 | for el in samples.iter_mut() { | |
406 | *el = self.noisegen.next(band); | |
407 | } | |
408 | } | |
409 | len = 10; | |
410 | }, | |
411 | 10 => { | |
412 | if br.left() > 0 { | |
413 | let mut scale = if br.read_bool() { -0.81 } else { 0.81 }; | |
414 | scale -= self.tables.noise_samples[((band + 1) * (idx + 5 * ch + 1)) & 0x7F] * 9.0 / 40.0; | |
415 | samples[0] = scale; | |
416 | } else { | |
417 | samples[0] = self.noisegen.next(band); | |
418 | } | |
419 | len = 1; | |
420 | }, | |
421 | 16 => { | |
422 | if br.left() >= 10 { | |
423 | if zero_coding { | |
424 | for i in 0..5 { | |
425 | if idx + i >= 128 { break; } | |
426 | samples[i] = if br.read_bool() { | |
427 | let ix = (br.read(1) as usize) * 2; | |
428 | QUANT_1BIT[jstereo as usize][ix] | |
429 | } else { 0.0 }; | |
430 | } | |
431 | } else { | |
432 | let idx = br.read(8) as usize; | |
433 | validate!(idx < self.tables.mod3.len()); | |
434 | for i in 0..5 { | |
435 | let k = self.tables.mod3[idx][i] as usize; | |
436 | samples[i] = QUANT_1BIT[jstereo as usize][k]; | |
437 | } | |
438 | } | |
439 | } else { | |
440 | for el in samples.iter_mut().take(5) { | |
441 | *el = self.noisegen.next(band); | |
442 | } | |
443 | } | |
444 | len = 5; | |
445 | }, | |
446 | 24 => { | |
447 | if br.left() >= 7 { | |
448 | let idx = br.read(7) as usize; | |
449 | validate!(idx < self.tables.mod5.len()); | |
450 | for i in 0..3 { | |
451 | let k = self.tables.mod5[idx][i] as usize; | |
452 | samples[i] = ((k as f32) - 2.0) * 0.5; | |
453 | } | |
454 | } else { | |
455 | for el in samples.iter_mut().take(3) { | |
456 | *el = self.noisegen.next(band); | |
457 | } | |
458 | } | |
459 | len = 3; | |
460 | }, | |
461 | 30 => { | |
462 | if br.left() >= 4 { | |
463 | let idx = br.read_code(&self.cbs.type30_codes_cb).unwrap_or(99) as usize; | |
464 | if idx < QUANT_TYPE30.len() - 1 { | |
465 | samples[0] = QUANT_TYPE30[idx]; | |
466 | } else { | |
467 | samples[0] = self.noisegen.next(band); | |
468 | } | |
469 | } else { | |
470 | samples[0] = self.noisegen.next(band); | |
471 | } | |
472 | len = 1; | |
473 | }, | |
474 | 34 => { | |
475 | if br.left() >= 7 { | |
476 | if type34_first { | |
477 | type34_first = false; | |
478 | type34_scale = 1.0 / ((1 << br.read(2)) as f32); | |
479 | type34_pred = ((br.read(5) as f32) - 16.0) / 15.0; | |
480 | samples[0] = type34_pred; | |
481 | } else { | |
482 | let idx = br.read_code(&self.cbs.type34_codes_cb).unwrap_or(99) as usize; | |
483 | if idx < TYPE34_DIFF.len() - 1 { | |
484 | samples[0] = type34_pred + TYPE34_DIFF[idx] * type34_scale; | |
485 | type34_pred = samples[0]; | |
486 | } else { | |
487 | samples[0] = self.noisegen.next(band); | |
488 | } | |
489 | } | |
490 | } else { | |
491 | samples[0] = self.noisegen.next(band); | |
492 | } | |
493 | len = 1; | |
494 | }, | |
495 | _ => { | |
496 | len = 1; | |
497 | }, | |
498 | }; | |
499 | let llen = len.min(128 - idx); | |
500 | if !jstereo { | |
501 | for samp in samples.iter().take(llen) { | |
502 | self.sb_samples[ch][idx][band] = self.tone_scale[ch][band][idx / 2] * *samp; | |
503 | idx += 1; | |
504 | } | |
505 | } else { | |
506 | for samp in samples.iter().take(llen) { | |
507 | self.sb_samples[0][idx][band] = self.tone_scale[0][band][idx / 2] * *samp; | |
508 | if self.channels == 2 { | |
509 | let sample = if signs[idx / 8] { -*samp } else { *samp }; | |
510 | self.sb_samples[1][idx][band] = self.tone_scale[1][band][idx / 2] * sample; | |
511 | } | |
512 | idx += 1; | |
513 | } | |
514 | } | |
515 | } | |
516 | Ok(()) | |
517 | } | |
518 | fn read_band_data(&mut self, br: &mut QdmBitReader, start: usize, end: usize) -> DecoderResult<()> { | |
519 | let mut samples = [0.0f32; 10]; | |
520 | let mut signs = [false; 16]; | |
521 | for band in start..end { | |
522 | let jstereo = if self.channels == 1 || band < 12 { | |
523 | false | |
524 | } else if band >= 24 { | |
525 | true | |
526 | } else { | |
527 | br.read_bool() | |
528 | }; | |
529 | if jstereo { | |
530 | if br.left() >= 16 { | |
531 | for el in signs.iter_mut() { | |
532 | *el = br.read_bool(); | |
533 | } | |
534 | } | |
535 | for i in 0..64 { | |
536 | self.quant_weight[0][band][i] = self.quant_weight[0][band][i].max(self.quant_weight[1][band][i]); | |
537 | } | |
538 | if !self.inc_quant_weight(band) { | |
539 | self.fill_noise(band); | |
540 | continue; | |
541 | } | |
542 | } | |
543 | ||
544 | let band_chan = if jstereo { 1 } else { self.channels }; | |
545 | for ch in 0..band_chan { | |
546 | self.read_noise_band(br, ch, band, &mut samples, &signs, jstereo)?; | |
547 | } | |
548 | } | |
549 | Ok(()) | |
550 | } | |
551 | fn fill_noise(&mut self, band: usize) { | |
552 | for ch in 0..self.channels { | |
553 | for i in 0..128 { | |
554 | self.sb_samples[ch][i][band] = self.noisegen.next(band) * self.tone_scale[ch][band][i / 2]; | |
555 | } | |
556 | } | |
557 | } | |
558 | pub fn read_type_11(&mut self, br: &mut QdmBitReader) -> DecoderResult<()> { | |
559 | if br.left() >= 32 { | |
560 | let c = br.read(13); | |
561 | if c > 3 { | |
562 | if self.is_intra { | |
563 | for ch in 0..self.channels { | |
564 | for band in 0..MAX_BANDS { | |
565 | let sb = QUANT_WEIGHT[self.cm_selector][band]; | |
566 | self.quant_weight[ch][band] = [sb; 64]; | |
567 | } | |
568 | } | |
569 | } else { | |
570 | unimplemented!(); | |
571 | } | |
572 | } | |
573 | } | |
574 | self.read_band_data(br, 0, 8)?; | |
575 | Ok(()) | |
576 | } | |
577 | pub fn read_type_12(&mut self, br: &mut QdmBitReader) -> DecoderResult<()> { | |
578 | self.read_band_data(br, 8, self.num_bands)?; | |
579 | Ok(()) | |
580 | } | |
581 | pub fn fill_default(&mut self, id: u8) { | |
582 | match id { | |
583 | 10 => { | |
584 | self.set_tone_scales(false); | |
585 | }, | |
586 | 11 => { | |
587 | for band in 0..8 { | |
588 | self.fill_noise(band); | |
589 | } | |
590 | }, | |
591 | 12 => { | |
592 | for band in 8..self.num_bands { | |
593 | self.fill_noise(band); | |
594 | } | |
595 | }, | |
596 | _ => {}, | |
597 | }; | |
598 | } | |
599 | pub fn new_frame(&mut self) { | |
600 | self.grid_1_quant = [[[[0; 8]; 8]; 3]; 2]; | |
601 | self.grid_3_quant = [[0; MAX_BANDS]; 2]; | |
602 | self.tone_idx_mid = [[[0; 8]; MAX_BANDS]; 2]; | |
603 | self.average_grid_quants(); | |
604 | } | |
605 | pub fn synth(&mut self, dst: &mut [f32], sf: usize, ch: usize) { | |
606 | let mut osamps = [0.0f32; 32 * 8]; | |
607 | let ssamp = 4 >> self.subsampling; | |
608 | for (i, out) in osamps.chunks_mut(32).enumerate() { | |
609 | self.qmf[ch].synth(&self.sb_samples[ch][sf * 8 + i], out); | |
610 | } | |
611 | let scale = 1.0 / ((1 << self.subsampling) as f32); | |
612 | for (src, dst) in osamps.chunks(ssamp).zip(dst.iter_mut()) { | |
613 | *dst += src[0] * scale; | |
614 | } | |
615 | } | |
616 | pub fn flush(&mut self) { | |
617 | for qmf in self.qmf.iter_mut() { | |
618 | qmf.reset(); | |
619 | } | |
620 | } | |
621 | } | |
622 | ||
623 | const LEVEL_CODES: &[u16; 24] = &[ | |
624 | 0x37C, 0x004, 0x03C, 0x04C, 0x03A, 0x02C, 0x01C, 0x01A, | |
625 | 0x024, 0x014, 0x001, 0x002, 0x000, 0x003, 0x007, 0x005, | |
626 | 0x006, 0x008, 0x009, 0x00A, 0x00C, 0x0FC, 0x07C, 0x17C | |
627 | ]; | |
628 | const LEVEL_BITS: &[u8; 24] = &[ | |
629 | 10, 6, 7, 7, 6, 6, 6, 6, 6, 5, 4, 4, 4, 3, 3, 3, | |
630 | 3, 4, 4, 5, 7, 8, 9, 10 | |
631 | ]; | |
632 | ||
633 | const LEVEL_DIFF_CODES: &[u16; 37] = &[ | |
634 | 0x1C57, 0x0004, 0x0000, 0x0001, 0x0003, 0x0002, 0x000F, 0x000E, | |
635 | 0x0007, 0x0016, 0x0037, 0x0027, 0x0026, 0x0066, 0x0006, 0x0097, | |
636 | 0x0046, 0x01C6, 0x0017, 0x0786, 0x0086, 0x0257, 0x00D7, 0x0357, | |
637 | 0x00C6, 0x0386, 0x0186, 0x0000, 0x0157, 0x0C57, 0x0057, 0x0000, | |
638 | 0x0B86, 0x0000, 0x1457, 0x0000, 0x0457 | |
639 | ]; | |
640 | const LEVEL_DIFF_BITS: &[u8; 37] = &[ | |
641 | 13, 3, 3, 2, 3, 3, 4, 4, 6, 5, 6, 6, 7, 7, 8, 8, | |
642 | 8, 9, 8, 11, 9, 10, 8, 10, 9, 12, 10, 0, 10, 13, 11, 0, | |
643 | 12, 0, 13, 0, 13 | |
644 | ]; | |
645 | ||
646 | const RUN_CODES: &[u8; 6] = &[ 0x1F, 0x00, 0x01, 0x03, 0x07, 0x0F ]; | |
647 | const RUN_BITS: &[u8; 6] = &[ 5, 1, 2, 3, 4, 5 ]; | |
648 | ||
649 | const TONE_IDX_HIGH1_CODES: &[u16; 20] = &[ | |
650 | 0x5714, 0x000C, 0x0002, 0x0001, 0x0000, 0x0004, 0x0034, 0x0054, | |
651 | 0x0094, 0x0014, 0x0114, 0x0214, 0x0314, 0x0614, 0x0E14, 0x0F14, | |
652 | 0x2714, 0x0714, 0x1714, 0x3714 | |
653 | ]; | |
654 | const TONE_IDX_HIGH1_BITS: &[u8; 20] = &[ | |
655 | 15, 4, 2, 1, 3, 5, 6, 7, 8, 10, 10, 11, 11, 12, 12, 12, 14, 14, 15, 14 | |
656 | ]; | |
657 | ||
658 | const TONE_IDX_HIGH2_CODES: &[u16; 24] = &[ | |
659 | 0x0664, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0064, 0x00E4, | |
660 | 0x00A4, 0x0068, 0x0004, 0x0008, 0x0014, 0x0018, 0x0000, 0x0001, | |
661 | 0x0002, 0x0003, 0x000C, 0x0028, 0x0024, 0x0164, 0x0000, 0x0264 | |
662 | ]; | |
663 | const TONE_IDX_HIGH2_BITS: &[u8; 24] = &[ | |
664 | 11, 0, 0, 0, 0, 0, 10, 8, 8, 7, 6, 6, 5, 5, 4, 2, 2, 2, 4, 7, 8, 9, 0, 11 | |
665 | ]; | |
666 | ||
667 | const TONE_IDX_MID_CODES: &[u16; 24] = &[ | |
668 | 0x0FEA, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | |
669 | 0x0000, 0x0000, 0x0000, 0x0000, 0x03EA, 0x00EA, 0x002A, 0x001A, | |
670 | 0x0006, 0x0001, 0x0000, 0x0002, 0x000A, 0x006A, 0x01EA, 0x07EA | |
671 | ]; | |
672 | const TONE_IDX_MID_BITS: &[u8; 24] = &[ | |
673 | 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 9, 7, 5, | |
674 | 3, 1, 2, 4, 6, 8, 10, 12 | |
675 | ]; | |
676 | ||
677 | const TYPE30_CODES: &[u8; 9] = &[ 0x3C, 0x06, 0x00, 0x01, 0x03, 0x02, 0x04, 0x0C, 0x1C ]; | |
678 | const TYPE30_BITS: &[u8; 9] = &[ 6, 3, 3, 2, 2, 3, 4, 5, 6 ]; | |
679 | ||
680 | const TYPE34_CODES: &[u8; 10] = &[ 0x18, 0x00, 0x01, 0x04, 0x05, 0x07, 0x03, 0x02, 0x06, 0x08 ]; | |
681 | const TYPE34_BITS: &[u8; 10] = &[ 5, 4, 3, 3, 3, 3, 3, 3, 3, 5 ]; | |
682 | ||
683 | const SB_NOISE_ATTENUATION: [f32; 32] = [ | |
684 | 0.0, 0.0, 0.3, 0.4, 0.5, 0.7, 1.0, 1.0, | |
685 | 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, | |
686 | 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, | |
687 | 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 | |
688 | ]; | |
689 | ||
690 | const QUANT_1BIT: [[f32; 3]; 2] = [[ -0.92, 0.0, 0.92 ], [ -0.89, 0.0, 0.89 ]]; | |
691 | ||
692 | const QUANT_TYPE30: [f32; 8] = [ | |
693 | -1.0, -0.625, -0.291666656732559, 0.0, 0.25, 0.5, 0.75, 1.0 | |
694 | ]; | |
695 | ||
696 | const TYPE34_DIFF: [f32; 10] = [ | |
697 | -1.0, -0.60947573184967, -0.333333343267441, -0.138071194291115, 0.0, | |
698 | 0.138071194291115, 0.333333343267441, 0.60947573184967, 1.0, 0.0 | |
699 | ]; | |
700 | ||
701 | const DEQUANT: [[[u16; 30]; 10]; 3] = [ | |
702 | [ | |
703 | [ 256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
704 | [ 0, 256, 256, 205, 154, 102, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
705 | [ 0, 0, 0, 51, 102, 154, 205, 256, 238, 219, 201, 183, 165, 146, 128, 110, 91, 73, 55, 37, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
706 | [ 0, 0, 0, 0, 0, 0, 0, 0, 18, 37, 55, 73, 91, 110, 128, 146, 165, 183, 201, 219, 238, 256, 228, 199, 171, 142, 114, 85, 57, 28 ], | |
707 | [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
708 | [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
709 | [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
710 | [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
711 | [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
712 | [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] | |
713 | ], [ | |
714 | [ 256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
715 | [ 0, 256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
716 | [ 0, 0, 256, 171, 85, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
717 | [ 0, 0, 0, 85, 171, 256, 171, 85, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
718 | [ 0, 0, 0, 0, 0, 0, 85, 171, 256, 219, 183, 146, 110, 73, 37, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
719 | [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 73, 110, 146, 183, 219, 256, 228, 199, 171, 142, 114, 85, 57, 28, 0, 0, 0, 0, 0, 0 ], | |
720 | [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 57, 85, 114, 142, 171, 199, 228, 256, 213, 171, 128, 85, 43 ], | |
721 | [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
722 | [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
723 | [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] | |
724 | ], [ | |
725 | [ 256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
726 | [ 0, 256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
727 | [ 0, 0, 256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
728 | [ 0, 0, 0, 256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
729 | [ 0, 0, 0, 0, 256, 256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
730 | [ 0, 0, 0, 0, 0, 0, 256, 171, 85, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
731 | [ 0, 0, 0, 0, 0, 0, 0, 85, 171, 256, 192, 128, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
732 | [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 128, 192, 256, 205, 154, 102, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], | |
733 | [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 51, 102, 154, 205, 256, 213, 171, 128, 85, 43, 0, 0, 0, 0, 0, 0 ], | |
734 | [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 43, 85, 128, 171, 213, 256, 213, 171, 128, 85, 43 ] | |
735 | ] | |
736 | ]; | |
737 | ||
738 | /*const TONE_LEVEL_IDX_OFFSET: [[i8; 4]; 30] = [ | |
739 | [ -50, -50, 0, -50 ], | |
740 | [ -50, -50, 0, -50 ], | |
741 | [ -50, -9, 0, -19 ], | |
742 | [ -16, -6, 0, -12 ], | |
743 | [ -11, -4, 0, -8 ], | |
744 | [ -8, -3, 0, -6 ], | |
745 | [ -7, -3, 0, -5 ], | |
746 | [ -6, -2, 0, -4 ], | |
747 | [ -5, -2, 0, -3 ], | |
748 | [ -4, -1, 0, -3 ], | |
749 | [ -4, -1, 0, -2 ], | |
750 | [ -3, -1, 0, -2 ], | |
751 | [ -3, -1, 0, -2 ], | |
752 | [ -3, -1, 0, -2 ], | |
753 | [ -2, -1, 0, -1 ], | |
754 | [ -2, -1, 0, -1 ], | |
755 | [ -2, -1, 0, -1 ], | |
756 | [ -2, 0, 0, -1 ], | |
757 | [ -2, 0, 0, -1 ], | |
758 | [ -1, 0, 0, -1 ], | |
759 | [ -1, 0, 0, -1 ], | |
760 | [ -1, 0, 0, -1 ], | |
761 | [ -1, 0, 0, -1 ], | |
762 | [ -1, 0, 0, -1 ], | |
763 | [ -1, 0, 0, -1 ], | |
764 | [ -1, 0, 0, -1 ], | |
765 | [ -1, 0, 0, 0 ], | |
766 | [ -1, 0, 0, 0 ], | |
767 | [ -1, 0, 0, 0 ], | |
768 | [ -1, 0, 0, 0 ] | |
769 | ];*/ | |
770 | ||
771 | const COEFFS_PER_SB_AVG: [[u8; 4]; 3] = [ | |
772 | [ 2, 3, 3, 3 ], | |
773 | [ 4, 5, 6, 6 ], | |
774 | [ 5, 7, 9, 9 ] | |
775 | ]; | |
776 | ||
777 | /*const COEFFS_PER_SB_AVG: [[u8; 30]; 3] = [ | |
778 | [ 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 ], | |
779 | [ 0, 1, 2, 2, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 ], | |
780 | [ 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9 ] | |
781 | ];*/ | |
782 | ||
783 | const COEFFS_PER_SB_DEQUANT: [[u8; 30]; 3] = [ | |
784 | [ 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3 ], | |
785 | [ 0, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6 ], | |
786 | [ 0, 1, 2, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9 ] | |
787 | ]; | |
788 | ||
789 | const QUANT_WEIGHT: [[u8; 30]; 5] = [ | |
790 | [ | |
791 | 34, 30, 24, 24, 16, 16, 16, 16, 10, 10, 10, 10, 10, 10, 10, | |
792 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 | |
793 | ], [ | |
794 | 34, 30, 24, 24, 16, 16, 16, 16, 10, 10, 10, 10, 10, 10, 10, | |
795 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 | |
796 | ], [ | |
797 | 34, 30, 30, 30, 24, 24, 16, 16, 16, 16, 16, 16, 10, 10, 10, | |
798 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 | |
799 | ], [ | |
800 | 34, 34, 30, 30, 24, 24, 24, 24, 16, 16, 16, 16, 16, 16, 16, | |
801 | 16, 16, 16, 16, 16, 16, 16, 10, 10, 10, 10, 10, 10, 10, 10 | |
802 | ], [ | |
803 | 34, 34, 30, 30, 30, 30, 30, 30, 24, 24, 24, 24, 24, 24, 24, | |
804 | 24, 24, 24, 24, 24, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 | |
805 | ] | |
806 | ]; |