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;
7 const MAX_BANDS: usize = 30;
9 const NOISE_TAB_LEN: usize = 3840;
12 noise_tab: [f32; NOISE_TAB_LEN],
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;
24 Self { noise_tab, idx: 0 }
26 fn next(&mut self, band: usize) -> f32 {
27 let ret = self.noise_tab[self.idx];
29 if self.idx >= NOISE_TAB_LEN {
30 self.idx -= NOISE_TAB_LEN;
32 ret * SB_NOISE_ATTENUATION[band]
37 noise_samples: [f32; 128],
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();
50 let mut mod3 = [[0u8; 5]; 243];
51 for (i, row) in mod3.iter_mut().enumerate() {
53 let mut low = i as u8;
54 for el in row.iter_mut() {
61 let mut mod5 = [[0u8; 3]; 125];
62 for (i, row) in mod5.iter_mut().enumerate() {
64 let mut low = i as u8;
65 for el in row.iter_mut() {
73 noise_samples, mod3, mod5,
79 level_cb: Codebook<u8>,
80 level_diff_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>,
89 fn map_idx(idx: usize) -> u8 { idx as u8 }
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()
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);
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);
108 let type30_codes_cb = create_codebook!(TYPE30_CODES, TYPE30_BITS);
109 let type34_codes_cb = create_codebook!(TYPE34_CODES, TYPE34_BITS);
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,
129 coef_per_sb_sel: usize,
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],
144 pub fn new() -> Self {
146 qmf: [QMF::new(), QMF::new()],
147 cbs: Codebooks::new(),
148 tables: Tables::new(),
149 noisegen: NoiseGen::new(),
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],
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);
174 let br = match self.subsampling * 2 + (channels as u8) - 1 {
183 self.cm_selector = if br * 1000 < full_bitrate {
185 } else if br * 1440 < full_bitrate {
187 } else if br * 1760 < full_bitrate {
189 } else if br * 2240 < full_bitrate {
194 self.coef_per_sb_sel = if full_bitrate <= 8000 { 0 } else if full_bitrate < 16000 { 1 } else { 2 };
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 {
201 for el in self.grid_2_quant[ch][i].iter() {
202 sum += i16::from(*el);
208 self.grid_2_quant[ch][i] = [sum as i8; 8];
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;
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);
224 dst[idx] = (last + (i as i32) * diff / (len as i32)) as i8;
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;
234 for ch in 0..self.channels {
235 Self::read_array(br, &mut self.grid_2_quant[ch][i], &self.cbs)?;
238 for ch in 0..self.channels {
239 self.grid_2_quant[ch][0] = [0; 8];
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);
247 self.grid_2_quant[ch][0] = [0; 8];
252 let n = (self.subsampling as usize) + 1;
254 for ch in 0..self.channels {
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;
261 self.grid_1_quant[ch][band][i] = [0; 8];
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;
271 self.grid_3_quant[ch][band] -= 16;
273 self.tone_idx_mid[ch][band] = [-16; 8];
277 for band in 0..self.num_bands - 5 {
278 for ch in 0..self.channels {
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;
286 self.set_tone_scales(true);
289 fn inc_quant_weight(&mut self, band: usize) -> bool {
290 let rlen = 128 / self.channels;
291 for ch in 0..self.channels {
294 if self.quant_weight[ch][band][idx] < 8 {
297 let (val, run) = match self.quant_weight[ch][band][idx] {
305 let len = run.min(rlen - idx);
306 let ref_val = self.quant_weight[ch][band][idx];
308 if self.quant_weight[ch][band + idx / 64][idx % 64] > ref_val {
309 self.quant_weight[ch][band][idx] = val;
318 fn set_tone_scales(&mut self, has_data: bool) {
319 const LAST_COEFF: [usize; 3] = [ 4, 7, 10 ];
321 let csel = self.coef_per_sb_sel;
322 for ch in 0..self.channels {
323 for band in 0..MAX_BANDS {
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]);
333 self.tone_idx_base[ch][band][i] = (q / 256) as i8;
337 if !self.is_intra && !has_data {
338 for band in 0..self.num_bands {
339 for ch in 0..self.channels {
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];
345 self.tone_scale[ch][band][i] = 0.0;
351 for band in 0..self.num_bands {
352 for ch in 0..self.channels {
354 let mut q = self.tone_idx_base[ch][band][i / 8];
356 q = q.wrapping_sub(self.grid_1_quant[ch][(band / 8).min(2)][i / 8][i % 8]);
358 q = q.wrapping_sub(self.tone_idx_mid[ch][band - 4][i / 8]);
360 q = q.wrapping_sub(self.grid_3_quant[ch][band - 4]);
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];
366 self.tone_scale[ch][band][i] = 0.0;
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();
382 match self.quant_weight[ch][band][idx / 2] {
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]
394 let idx = br.read(8) as usize;
395 validate!(idx < self.tables.mod3.len());
397 let k = self.tables.mod3[idx][i] as usize;
398 samples[i * 2] = QUANT_1BIT[jstereo as usize][k];
401 for el in samples.chunks_mut(2) {
402 el[1] = self.noisegen.next(band);
405 for el in samples.iter_mut() {
406 *el = self.noisegen.next(band);
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;
417 samples[0] = self.noisegen.next(band);
424 for (i, dst) in samples[..5].iter_mut().enumerate() {
425 if idx + i >= 128 { break; }
426 *dst = if br.read_bool() {
427 let ix = (br.read(1) as usize) * 2;
428 QUANT_1BIT[jstereo as usize][ix]
432 let idx = br.read(8) as usize;
433 validate!(idx < self.tables.mod3.len());
434 for (dst, &k) in samples.iter_mut().zip(self.tables.mod3[idx].iter()) {
435 *dst = QUANT_1BIT[jstereo as usize][k as usize];
439 for el in samples.iter_mut().take(5) {
440 *el = self.noisegen.next(band);
447 let idx = br.read(7) as usize;
448 validate!(idx < self.tables.mod5.len());
449 for (dst, &k) in samples.iter_mut().zip(self.tables.mod5[idx].iter()) {
450 *dst = ((k as f32) - 2.0) * 0.5;
453 for el in samples.iter_mut().take(3) {
454 *el = self.noisegen.next(band);
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];
465 samples[0] = self.noisegen.next(band);
468 samples[0] = self.noisegen.next(band);
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;
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];
485 samples[0] = self.noisegen.next(band);
489 samples[0] = self.noisegen.next(band);
497 let llen = len.min(128 - idx);
499 for samp in samples.iter().take(llen) {
500 self.sb_samples[ch][idx][band] = self.tone_scale[ch][band][idx / 2] * *samp;
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;
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 {
522 } else if band >= 24 {
529 for el in signs.iter_mut() {
530 *el = br.read_bool();
534 self.quant_weight[0][band][i] = self.quant_weight[0][band][i].max(self.quant_weight[1][band][i]);
536 if !self.inc_quant_weight(band) {
537 self.fill_noise(band);
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)?;
549 fn fill_noise(&mut self, band: usize) {
550 for ch in 0..self.channels {
552 self.sb_samples[ch][i][band] = self.noisegen.next(band) * self.tone_scale[ch][band][i / 2];
556 pub fn read_type_11(&mut self, br: &mut QdmBitReader) -> DecoderResult<()> {
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];
572 self.read_band_data(br, 0, 8)?;
575 pub fn read_type_12(&mut self, br: &mut QdmBitReader) -> DecoderResult<()> {
576 self.read_band_data(br, 8, self.num_bands)?;
579 pub fn fill_default(&mut self, id: u8) {
582 self.set_tone_scales(false);
586 self.fill_noise(band);
590 for band in 8..self.num_bands {
591 self.fill_noise(band);
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();
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);
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;
614 pub fn flush(&mut self) {
615 for qmf in self.qmf.iter_mut() {
621 const 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
626 const 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
631 const 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
638 const 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,
644 const RUN_CODES: &[u8; 6] = &[ 0x1F, 0x00, 0x01, 0x03, 0x07, 0x0F ];
645 const RUN_BITS: &[u8; 6] = &[ 5, 1, 2, 3, 4, 5 ];
647 const 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
652 const 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
656 const 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
661 const 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
665 const 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
670 const 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
675 const TYPE30_CODES: &[u8; 9] = &[ 0x3C, 0x06, 0x00, 0x01, 0x03, 0x02, 0x04, 0x0C, 0x1C ];
676 const TYPE30_BITS: &[u8; 9] = &[ 6, 3, 3, 2, 2, 3, 4, 5, 6 ];
678 const TYPE34_CODES: &[u8; 10] = &[ 0x18, 0x00, 0x01, 0x04, 0x05, 0x07, 0x03, 0x02, 0x06, 0x08 ];
679 const TYPE34_BITS: &[u8; 10] = &[ 5, 4, 3, 3, 3, 3, 3, 3, 3, 5 ];
681 const 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
688 const QUANT_1BIT: [[f32; 3]; 2] = [[ -0.92, 0.0, 0.92 ], [ -0.89, 0.0, 0.89 ]];
690 const QUANT_TYPE30: [f32; 8] = [
691 -1.0, -0.625, -0.291666656732559, 0.0, 0.25, 0.5, 0.75, 1.0
694 const 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
699 const DEQUANT: [[[u16; 30]; 10]; 3] = [
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, 0, 0, 0, 0, 0, 0, 0 ],
704 [ 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 ],
705 [ 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 ],
706 [ 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 ],
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,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, 0, 0, 0, 0, 0, 0, 0, 43, 85, 128, 171, 213, 256, 213, 171, 128, 85, 43 ]
736 /*const TONE_LEVEL_IDX_OFFSET: [[i8; 4]; 30] = [
737 [ -50, -50, 0, -50 ],
738 [ -50, -50, 0, -50 ],
769 const COEFFS_PER_SB_AVG: [[u8; 4]; 3] = [
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 ]
781 const 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 ]
787 const QUANT_WEIGHT: [[u8; 30]; 5] = [
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
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
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
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
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