]> git.nihav.org Git - nihav.git/blame - nihav-qt/src/codecs/qdm2qmf.rs
avimux: do not record palette change chunks in OpenDML index
[nihav.git] / nihav-qt / src / codecs / qdm2qmf.rs
CommitLineData
4c1582cf
KS
1use nihav_core::codecs::*;
2use nihav_core::io::codebook::*;
3use nihav_codec_support::dsp::qmf::QMF;
4use super::qdmcommon::*;
5use super::qdm2::TONE_SCALES;
6
7const MAX_BANDS: usize = 30;
8
9const NOISE_TAB_LEN: usize = 3840;
10
11struct NoiseGen {
12 noise_tab: [f32; NOISE_TAB_LEN],
13 idx: usize,
14}
15
16impl 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
36struct Tables {
37 noise_samples: [f32; 128],
38 mod3: [[u8; 5]; 243],
39 mod5: [[u8; 3]; 125],
40}
41
42impl 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
78struct 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
89fn map_idx(idx: usize) -> u8 { idx as u8 }
90
91macro_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
98impl 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
118pub 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
143impl 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;
310unimplemented!();
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 }
e6aaad5c 362 self.tone_idx[ch][band][i] = q;
4c1582cf
KS
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 }
b7c882c1 373 #[allow(clippy::cognitive_complexity)]
4c1582cf
KS
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 {
237cc1f9 397 let k = self.tables.mod3[idx][i] as usize;
4c1582cf
KS
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 {
27116a4c 424 for (i, dst) in samples[..5].iter_mut().enumerate() {
4c1582cf 425 if idx + i >= 128 { break; }
27116a4c 426 *dst = if br.read_bool() {
4c1582cf
KS
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());
27116a4c
KS
434 for (dst, &k) in samples.iter_mut().zip(self.tables.mod3[idx].iter()) {
435 *dst = QUANT_1BIT[jstereo as usize][k as usize];
4c1582cf
KS
436 }
437 }
438 } else {
439 for el in samples.iter_mut().take(5) {
440 *el = self.noisegen.next(band);
441 }
442 }
443 len = 5;
444 },
445 24 => {
446 if br.left() >= 7 {
447 let idx = br.read(7) as usize;
448 validate!(idx < self.tables.mod5.len());
27116a4c
KS
449 for (dst, &k) in samples.iter_mut().zip(self.tables.mod5[idx].iter()) {
450 *dst = ((k as f32) - 2.0) * 0.5;
4c1582cf
KS
451 }
452 } else {
453 for el in samples.iter_mut().take(3) {
454 *el = self.noisegen.next(band);
455 }
456 }
457 len = 3;
458 },
459 30 => {
460 if br.left() >= 4 {
461 let idx = br.read_code(&self.cbs.type30_codes_cb).unwrap_or(99) as usize;
462 if idx < QUANT_TYPE30.len() - 1 {
463 samples[0] = QUANT_TYPE30[idx];
464 } else {
465 samples[0] = self.noisegen.next(band);
466 }
467 } else {
468 samples[0] = self.noisegen.next(band);
469 }
470 len = 1;
471 },
472 34 => {
473 if br.left() >= 7 {
474 if type34_first {
475 type34_first = false;
476 type34_scale = 1.0 / ((1 << br.read(2)) as f32);
477 type34_pred = ((br.read(5) as f32) - 16.0) / 15.0;
478 samples[0] = type34_pred;
479 } else {
480 let idx = br.read_code(&self.cbs.type34_codes_cb).unwrap_or(99) as usize;
481 if idx < TYPE34_DIFF.len() - 1 {
482 samples[0] = type34_pred + TYPE34_DIFF[idx] * type34_scale;
483 type34_pred = samples[0];
484 } else {
485 samples[0] = self.noisegen.next(band);
486 }
487 }
488 } else {
489 samples[0] = self.noisegen.next(band);
490 }
491 len = 1;
492 },
493 _ => {
494 len = 1;
495 },
496 };
497 let llen = len.min(128 - idx);
498 if !jstereo {
499 for samp in samples.iter().take(llen) {
500 self.sb_samples[ch][idx][band] = self.tone_scale[ch][band][idx / 2] * *samp;
501 idx += 1;
502 }
503 } else {
504 for samp in samples.iter().take(llen) {
505 self.sb_samples[0][idx][band] = self.tone_scale[0][band][idx / 2] * *samp;
506 if self.channels == 2 {
507 let sample = if signs[idx / 8] { -*samp } else { *samp };
508 self.sb_samples[1][idx][band] = self.tone_scale[1][band][idx / 2] * sample;
509 }
510 idx += 1;
511 }
512 }
513 }
514 Ok(())
515 }
516 fn read_band_data(&mut self, br: &mut QdmBitReader, start: usize, end: usize) -> DecoderResult<()> {
517 let mut samples = [0.0f32; 10];
518 let mut signs = [false; 16];
519 for band in start..end {
520 let jstereo = if self.channels == 1 || band < 12 {
521 false
522 } else if band >= 24 {
523 true
524 } else {
525 br.read_bool()
526 };
527 if jstereo {
528 if br.left() >= 16 {
529 for el in signs.iter_mut() {
530 *el = br.read_bool();
531 }
532 }
533 for i in 0..64 {
534 self.quant_weight[0][band][i] = self.quant_weight[0][band][i].max(self.quant_weight[1][band][i]);
535 }
536 if !self.inc_quant_weight(band) {
537 self.fill_noise(band);
538 continue;
539 }
540 }
541
542 let band_chan = if jstereo { 1 } else { self.channels };
543 for ch in 0..band_chan {
544 self.read_noise_band(br, ch, band, &mut samples, &signs, jstereo)?;
545 }
546 }
547 Ok(())
548 }
549 fn fill_noise(&mut self, band: usize) {
550 for ch in 0..self.channels {
551 for i in 0..128 {
552 self.sb_samples[ch][i][band] = self.noisegen.next(band) * self.tone_scale[ch][band][i / 2];
553 }
554 }
555 }
556 pub fn read_type_11(&mut self, br: &mut QdmBitReader) -> DecoderResult<()> {
557 if br.left() >= 32 {
558 let c = br.read(13);
559 if c > 3 {
560 if self.is_intra {
561 for ch in 0..self.channels {
562 for band in 0..MAX_BANDS {
563 let sb = QUANT_WEIGHT[self.cm_selector][band];
564 self.quant_weight[ch][band] = [sb; 64];
565 }
566 }
567 } else {
568unimplemented!();
569 }
570 }
571 }
572 self.read_band_data(br, 0, 8)?;
573 Ok(())
574 }
575 pub fn read_type_12(&mut self, br: &mut QdmBitReader) -> DecoderResult<()> {
576 self.read_band_data(br, 8, self.num_bands)?;
577 Ok(())
578 }
579 pub fn fill_default(&mut self, id: u8) {
580 match id {
581 10 => {
582 self.set_tone_scales(false);
583 },
584 11 => {
585 for band in 0..8 {
586 self.fill_noise(band);
587 }
588 },
589 12 => {
590 for band in 8..self.num_bands {
591 self.fill_noise(band);
592 }
593 },
594 _ => {},
595 };
596 }
597 pub fn new_frame(&mut self) {
598 self.grid_1_quant = [[[[0; 8]; 8]; 3]; 2];
599 self.grid_3_quant = [[0; MAX_BANDS]; 2];
600 self.tone_idx_mid = [[[0; 8]; MAX_BANDS]; 2];
601 self.average_grid_quants();
602 }
603 pub fn synth(&mut self, dst: &mut [f32], sf: usize, ch: usize) {
604 let mut osamps = [0.0f32; 32 * 8];
605 let ssamp = 4 >> self.subsampling;
606 for (i, out) in osamps.chunks_mut(32).enumerate() {
607 self.qmf[ch].synth(&self.sb_samples[ch][sf * 8 + i], out);
608 }
609 let scale = 1.0 / ((1 << self.subsampling) as f32);
610 for (src, dst) in osamps.chunks(ssamp).zip(dst.iter_mut()) {
611 *dst += src[0] * scale;
612 }
613 }
614 pub fn flush(&mut self) {
615 for qmf in self.qmf.iter_mut() {
616 qmf.reset();
617 }
618 }
619}
620
621const LEVEL_CODES: &[u16; 24] = &[
622 0x37C, 0x004, 0x03C, 0x04C, 0x03A, 0x02C, 0x01C, 0x01A,
623 0x024, 0x014, 0x001, 0x002, 0x000, 0x003, 0x007, 0x005,
624 0x006, 0x008, 0x009, 0x00A, 0x00C, 0x0FC, 0x07C, 0x17C
625];
626const LEVEL_BITS: &[u8; 24] = &[
627 10, 6, 7, 7, 6, 6, 6, 6, 6, 5, 4, 4, 4, 3, 3, 3,
628 3, 4, 4, 5, 7, 8, 9, 10
629];
630
631const LEVEL_DIFF_CODES: &[u16; 37] = &[
632 0x1C57, 0x0004, 0x0000, 0x0001, 0x0003, 0x0002, 0x000F, 0x000E,
633 0x0007, 0x0016, 0x0037, 0x0027, 0x0026, 0x0066, 0x0006, 0x0097,
634 0x0046, 0x01C6, 0x0017, 0x0786, 0x0086, 0x0257, 0x00D7, 0x0357,
635 0x00C6, 0x0386, 0x0186, 0x0000, 0x0157, 0x0C57, 0x0057, 0x0000,
636 0x0B86, 0x0000, 0x1457, 0x0000, 0x0457
637];
638const LEVEL_DIFF_BITS: &[u8; 37] = &[
639 13, 3, 3, 2, 3, 3, 4, 4, 6, 5, 6, 6, 7, 7, 8, 8,
640 8, 9, 8, 11, 9, 10, 8, 10, 9, 12, 10, 0, 10, 13, 11, 0,
641 12, 0, 13, 0, 13
642];
643
644const RUN_CODES: &[u8; 6] = &[ 0x1F, 0x00, 0x01, 0x03, 0x07, 0x0F ];
645const RUN_BITS: &[u8; 6] = &[ 5, 1, 2, 3, 4, 5 ];
646
647const TONE_IDX_HIGH1_CODES: &[u16; 20] = &[
648 0x5714, 0x000C, 0x0002, 0x0001, 0x0000, 0x0004, 0x0034, 0x0054,
649 0x0094, 0x0014, 0x0114, 0x0214, 0x0314, 0x0614, 0x0E14, 0x0F14,
650 0x2714, 0x0714, 0x1714, 0x3714
651];
652const TONE_IDX_HIGH1_BITS: &[u8; 20] = &[
653 15, 4, 2, 1, 3, 5, 6, 7, 8, 10, 10, 11, 11, 12, 12, 12, 14, 14, 15, 14
654];
655
656const TONE_IDX_HIGH2_CODES: &[u16; 24] = &[
657 0x0664, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0064, 0x00E4,
658 0x00A4, 0x0068, 0x0004, 0x0008, 0x0014, 0x0018, 0x0000, 0x0001,
659 0x0002, 0x0003, 0x000C, 0x0028, 0x0024, 0x0164, 0x0000, 0x0264
660];
661const TONE_IDX_HIGH2_BITS: &[u8; 24] = &[
662 11, 0, 0, 0, 0, 0, 10, 8, 8, 7, 6, 6, 5, 5, 4, 2, 2, 2, 4, 7, 8, 9, 0, 11
663];
664
665const TONE_IDX_MID_CODES: &[u16; 24] = &[
666 0x0FEA, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
667 0x0000, 0x0000, 0x0000, 0x0000, 0x03EA, 0x00EA, 0x002A, 0x001A,
668 0x0006, 0x0001, 0x0000, 0x0002, 0x000A, 0x006A, 0x01EA, 0x07EA
669];
670const TONE_IDX_MID_BITS: &[u8; 24] = &[
671 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 9, 7, 5,
672 3, 1, 2, 4, 6, 8, 10, 12
673];
674
675const TYPE30_CODES: &[u8; 9] = &[ 0x3C, 0x06, 0x00, 0x01, 0x03, 0x02, 0x04, 0x0C, 0x1C ];
676const TYPE30_BITS: &[u8; 9] = &[ 6, 3, 3, 2, 2, 3, 4, 5, 6 ];
677
678const TYPE34_CODES: &[u8; 10] = &[ 0x18, 0x00, 0x01, 0x04, 0x05, 0x07, 0x03, 0x02, 0x06, 0x08 ];
679const TYPE34_BITS: &[u8; 10] = &[ 5, 4, 3, 3, 3, 3, 3, 3, 3, 5 ];
680
681const SB_NOISE_ATTENUATION: [f32; 32] = [
682 0.0, 0.0, 0.3, 0.4, 0.5, 0.7, 1.0, 1.0,
683 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
684 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
685 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0
686];
687
688const QUANT_1BIT: [[f32; 3]; 2] = [[ -0.92, 0.0, 0.92 ], [ -0.89, 0.0, 0.89 ]];
689
690const QUANT_TYPE30: [f32; 8] = [
691 -1.0, -0.625, -0.291666656732559, 0.0, 0.25, 0.5, 0.75, 1.0
692];
693
694const TYPE34_DIFF: [f32; 10] = [
695 -1.0, -0.60947573184967, -0.333333343267441, -0.138071194291115, 0.0,
696 0.138071194291115, 0.333333343267441, 0.60947573184967, 1.0, 0.0
697];
698
699const DEQUANT: [[[u16; 30]; 10]; 3] = [
700 [
701 [ 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 ],
702 [ 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 ],
703 [ 0, 0, 0, 51, 102, 154, 205, 256, 238, 219, 201, 183, 165, 146, 128, 110, 91, 73, 55, 37, 18, 0, 0,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, 0, 0, 0, 0, 0, 0, 0 ],
727 [ 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 ],
728 [ 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 ],
729 [ 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 ],
730 [ 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 ],
731 [ 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 ],
732 [ 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 ]
733 ]
734];
735
736/*const TONE_LEVEL_IDX_OFFSET: [[i8; 4]; 30] = [
737 [ -50, -50, 0, -50 ],
738 [ -50, -50, 0, -50 ],
739 [ -50, -9, 0, -19 ],
740 [ -16, -6, 0, -12 ],
741 [ -11, -4, 0, -8 ],
742 [ -8, -3, 0, -6 ],
743 [ -7, -3, 0, -5 ],
744 [ -6, -2, 0, -4 ],
745 [ -5, -2, 0, -3 ],
746 [ -4, -1, 0, -3 ],
747 [ -4, -1, 0, -2 ],
748 [ -3, -1, 0, -2 ],
749 [ -3, -1, 0, -2 ],
750 [ -3, -1, 0, -2 ],
751 [ -2, -1, 0, -1 ],
752 [ -2, -1, 0, -1 ],
753 [ -2, -1, 0, -1 ],
754 [ -2, 0, 0, -1 ],
755 [ -2, 0, 0, -1 ],
756 [ -1, 0, 0, -1 ],
757 [ -1, 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, 0 ],
764 [ -1, 0, 0, 0 ],
765 [ -1, 0, 0, 0 ],
766 [ -1, 0, 0, 0 ]
767];*/
768
769const COEFFS_PER_SB_AVG: [[u8; 4]; 3] = [
770 [ 2, 3, 3, 3 ],
771 [ 4, 5, 6, 6 ],
772 [ 5, 7, 9, 9 ]
773];
774
775/*const COEFFS_PER_SB_AVG: [[u8; 30]; 3] = [
776 [ 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 ],
777 [ 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 ],
778 [ 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 ]
779];*/
780
781const COEFFS_PER_SB_DEQUANT: [[u8; 30]; 3] = [
782 [ 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 ],
783 [ 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 ],
784 [ 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 ]
785];
786
787const QUANT_WEIGHT: [[u8; 30]; 5] = [
788 [
789 34, 30, 24, 24, 16, 16, 16, 16, 10, 10, 10, 10, 10, 10, 10,
790 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
791 ], [
792 34, 30, 24, 24, 16, 16, 16, 16, 10, 10, 10, 10, 10, 10, 10,
793 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
794 ], [
795 34, 30, 30, 30, 24, 24, 16, 16, 16, 16, 16, 16, 10, 10, 10,
796 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
797 ], [
798 34, 34, 30, 30, 24, 24, 24, 24, 16, 16, 16, 16, 16, 16, 16,
799 16, 16, 16, 16, 16, 16, 16, 10, 10, 10, 10, 10, 10, 10, 10
800 ], [
801 34, 34, 30, 30, 30, 30, 30, 30, 24, 24, 24, 24, 24, 24, 24,
802 24, 24, 24, 24, 24, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16
803 ]
804];