1 use nihav_core::codecs::{DecoderResult, DecoderError};
2 use nihav_core::io::bitreader::*;
8 pub use bs::{SBRCodebooks, sbr_read_sce, sbr_read_cpe};
10 pub use synth::SBRChannel;
12 pub const NUM_ENVELOPES: usize = 8;
13 pub const NUM_PATCHES: usize = 5;
14 pub const SBR_BANDS: usize = 64;
15 pub const QMF_DELAY: usize = 8;
16 pub const HF_ADJ: usize = 2;
17 pub const MAX_SLOTS: usize = 16;
19 #[derive(Clone, Copy, Debug)]
20 pub struct SBRHeader {
35 pub fn new() -> Self {
36 let mut obj: Self = unsafe { std::mem::zeroed() };
42 self.alter_scale = true;
44 self.limiter_bands = 2;
45 self.limiter_gains = 2;
46 self.interpol_freq = true;
47 self.smoothing_mode = true;
50 pub fn read(br: &mut BitReader) -> DecoderResult<Self> {
51 let mut sbrh = Self::new();
53 sbrh.amp_res = br.read_bool()?;
54 sbrh.start_freq = br.read(4)? as usize;
55 sbrh.stop_freq = br.read(4)? as usize;
56 sbrh.xover_band = br.read(3)? as usize;
58 let header_extra_1 = br.read_bool()?;
59 let header_extra_2 = br.read_bool()?;
61 sbrh.freq_scale = br.read(2)? as u8;
62 sbrh.alter_scale = br.read_bool()?;
63 sbrh.noise_bands = br.read(2)? as u8;
66 sbrh.limiter_bands = br.read(2)? as u8;
67 sbrh.limiter_gains = br.read(2)? as u8;
68 sbrh.interpol_freq = br.read_bool()?;
69 if !sbrh.interpol_freq {
70 return Err(DecoderError::NotImplemented);
72 sbrh.smoothing_mode = br.read_bool()?;
77 pub fn differs_from(&self, rval: &Self) -> bool {
78 self.start_freq != rval.start_freq ||
79 self.stop_freq != rval.stop_freq ||
80 self.xover_band != rval.xover_band ||
81 self.freq_scale != rval.freq_scale ||
82 self.alter_scale != rval.alter_scale ||
83 self.noise_bands != rval.noise_bands
89 num_env_bands: [usize; 2],
91 num_noise_bands: usize,
94 low_to_high_res: [usize; SBR_BANDS],
95 high_to_low_res: [usize; SBR_BANDS],
96 f: [usize; SBR_BANDS],
97 f_low: [usize; SBR_BANDS],
98 f_noise: [usize; SBR_BANDS],
99 f_lim: [usize; SBR_BANDS],
100 patch_num_subbands: [usize; SBR_BANDS],
101 patch_start_subband: [usize; SBR_BANDS],
106 pub fn new() -> Self {
108 num_env_bands: [0; 2],
113 low_to_high_res: [0; SBR_BANDS],
114 high_to_low_res: [0; SBR_BANDS],
116 f_low: [0; SBR_BANDS],
117 f_noise: [0; SBR_BANDS],
118 f_lim: [0; SBR_BANDS],
119 patch_num_subbands: [0; SBR_BANDS],
120 patch_start_subband: [0; SBR_BANDS],
124 pub fn init(&mut self, hdr: &SBRHeader, srate: u32) -> DecoderResult<()> {
125 let offset_tab = match srate {
126 0..=16000 => &SBR_OFFSETS[0],
127 16001..=22050 => &SBR_OFFSETS[1],
128 22051..=24000 => &SBR_OFFSETS[2],
129 24001..=32000 => &SBR_OFFSETS[3],
130 32001..=64000 => &SBR_OFFSETS[4],
131 _ => &SBR_OFFSETS[5],
133 let smult = match srate {
135 32000..=63999 => 4000,
138 let start_min = (128 * smult + srate / 2) / srate;
139 let stop_min = (128 * smult * 2 + srate / 2) / srate;
141 let k0 = ((start_min as i32) + i32::from(offset_tab[hdr.start_freq])).max(0) as usize;
142 let k2 = (match hdr.stop_freq {
146 let mut stop_dk = [0; 14];
147 generate_vdk(&mut stop_dk, stop_min as usize, SBR_BANDS, 13);
148 let dk_sum: usize = stop_dk[..hdr.stop_freq].iter().sum();
149 (stop_min as usize) + dk_sum
153 let max_bands = match srate {
158 validate!(k2 - k0 <= max_bands);
160 self.num_master = calculate_master_frequencies(&mut self.f, k0, k2, hdr);
161 let num_high = self.num_master - hdr.xover_band;
162 let num_low = (num_high + 1) / 2; // INT(num_high / 2) + (num_high - 2 * INT(num_high / 2))
164 self.num_env_bands = [num_low, num_high];
166 let f_high = &self.f[hdr.xover_band..];
167 let m = f_high[num_high] - f_high[0];
170 self.f_low = [0; SBR_BANDS];
171 if (num_high & 1) == 0 {
172 for k in 0..=num_low {
173 self.f_low[k] = f_high[k * 2];
176 self.f_low[0] = f_high[0];
177 for k in 1..=num_low {
178 self.f_low[k] = f_high[k * 2 - 1];
182 let high_src = &f_high[..=num_high];
183 let low_src = &self.f_low[..=num_low];
184 for (dst, low) in self.high_to_low_res.iter_mut().zip(low_src.iter()) {
185 if let Ok(idx) = high_src.binary_search(low) {
188 return Err(DecoderError::Bug);
191 for (dst, high) in self.low_to_high_res.iter_mut().zip(high_src.iter()) {
192 *dst = match low_src.binary_search(high) {
198 let num_q = (((hdr.noise_bands as f32) * ((k2 as f32) / (k_x as f32)).log2()).round() as usize).max(1);
199 self.num_noise_bands = num_q;
200 self.f_noise = [0; SBR_BANDS];
202 self.f_noise[0] = self.f_low[0];
204 let idx = prev + ((num_low - prev) / (num_q + 1 - k));
205 self.f_noise[k] = self.f_low[idx];
209 let mut num_patches = 0;
210 self.patch_num_subbands = [0; SBR_BANDS];
211 self.patch_start_subband = [0; SBR_BANDS];
214 let goal_sb = ((2048000 + srate / 2) / srate) as usize;
215 let last_band = k_x + m;
216 let mut k = if goal_sb < last_band {
218 for i in 0..self.num_master {
219 if self.f[i] >= goal_sb {
234 odd = (sb - 2 + k0) & 1;
235 if sb <= k0 + msb - 1 - odd {
241 self.patch_num_subbands[num_patches] = sb.saturating_sub(usb);
242 self.patch_start_subband[num_patches] = k0 - odd - self.patch_num_subbands[num_patches];
244 if self.patch_num_subbands[num_patches] > 0 {
252 if self.f[k] < sb + 3 {
260 if (num_patches > 1) && (self.patch_num_subbands[num_patches - 1] < 3) {
263 validate!(num_patches <= NUM_PATCHES);
264 self.num_patches = num_patches;
266 self.f_lim = [0; SBR_BANDS];
267 let num_l = if hdr.limiter_bands == 0 {
268 self.f_lim[0] = self.f_low[0];
269 self.f_lim[1] = self.f_low[num_low];
272 let lim_bands = match hdr.limiter_bands {
277 let mut patch_borders = [0; NUM_PATCHES + 1];
278 patch_borders[0] = k_x;
279 for k in 0..num_patches {
280 patch_borders[k + 1] = patch_borders[k] + self.patch_num_subbands[k];
282 self.f_lim = self.f_low;
283 let f_lim_ptr = &mut self.f_lim[..num_low + num_patches];
284 for &pborder in patch_borders[1..num_patches].iter() {
286 for &el in f_lim_ptr.iter() {
292 for j in (i..num_low + num_patches - 1).rev() {
293 f_lim_ptr[j + 1] = f_lim_ptr[j];
295 f_lim_ptr[i] = pborder;
297 let mut nr_lim = num_low + num_patches - 1;
299 let pbord = &patch_borders[..=num_patches];
301 let n_octaves = ((self.f_lim[k] as f32) / (self.f_lim[k - 1] as f32)).log2();
302 if (n_octaves * lim_bands) < 0.49 {
303 if self.f_lim[k] == self.f_lim[k - 1] || !pbord.contains(&self.f_lim[k]) {
305 self.f_lim[l] = self.f_lim[l + 1];
308 } else if !pbord.contains(&self.f_lim[k - 1]) {
309 for l in (k - 1)..nr_lim {
310 self.f_lim[l] = self.f_lim[l + 1];
323 self.num_lim = num_l;
329 fn generate_vdk(v_dk: &mut [usize], k0: usize, k1: usize, num_bands: usize) {
332 let factor = (k1 as f64) / k0;
333 for k in 0..num_bands {
334 let next = (k0 * factor.powf((k + 1) as f64 / (num_bands as f64))).round() as usize;
335 let newval = next - last;
338 for (j, &el) in v_dk[..k].iter().enumerate() {
344 for j in (idx..k).rev() {
345 v_dk[j + 1] = v_dk[j];
351 fn calculate_master_frequencies(f: &mut [usize; SBR_BANDS], k0: usize, k2: usize, hdr: &SBRHeader) -> usize {
352 if hdr.freq_scale == 0 {
353 let (dk, num_bands) = if !hdr.alter_scale {
354 (1, 2 * ((k2 - k0) / 2))
356 (2, 2 * ((k2 - k0 + 2) / (2 * 2)))
358 let k2_achieved = k0 + num_bands * dk;
359 let mut k2_diff = (k2 as isize) - (k2_achieved as isize);
360 let mut v_dk = [dk; SBR_BANDS];
368 } else if k2_diff > 0 {
369 let mut k = num_bands - 1;
377 for i in 0..num_bands {
378 f[i + 1] = f[i] + v_dk[i];
383 let bands = 14 - hdr.freq_scale * 2;
384 let warp = if !hdr.alter_scale { 1.0f32 } else { 1.3f32 };
385 let two_regions = (k2 as f32) / (k0 as f32) > 2.2449;
386 let k1 = if two_regions { 2 * k0 } else { k2 };
387 let num_bands0 = 2 * (((bands as f32) * ((k1 as f32) / (k0 as f32)).log2() / 2.0).round() as usize);
388 let mut v_dk0 = [0; SBR_BANDS];
389 generate_vdk(&mut v_dk0, k0, k1, num_bands0);
390 let mut v_k0 = [0; SBR_BANDS];
392 for i in 0..num_bands0 {
393 v_k0[i + 1] = v_k0[i] + v_dk0[i];
397 let num_bands1 = 2 * (((bands as f32) * ((k2 as f32) / (k1 as f32)).log2() / (2.0 * warp)).round() as usize);
398 let mut v_dk1 = [0; SBR_BANDS];
399 generate_vdk(&mut v_dk1, k1, k2, num_bands1);
400 let max_vdk0 = v_dk0[num_bands0 - 1];
401 if v_dk1[0] < max_vdk0 {
402 let change = (max_vdk0 - v_dk1[0]).min((v_dk1[num_bands1 - 1] - v_dk1[0]) / 2);
404 v_dk1[num_bands1 - 1] -= change;
406 let mut v_k1 = [0; SBR_BANDS];
408 for i in 0..num_bands1 {
409 v_k1[i + 1] = v_k1[i] + v_dk1[i];
411 f[..=num_bands0].copy_from_slice(&v_k0[..=num_bands0]);
412 f[num_bands0 + 1..][..=num_bands1].copy_from_slice(&v_k1[1..][..=num_bands1]);
413 num_bands0 + num_bands1
415 f[..=num_bands0].copy_from_slice(&v_k0[..=num_bands0]);
421 const SBR_OFFSETS: [[i8; 16]; 6] = [
422 [ -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7 ], // 16kHz
423 [ -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13 ], // 22kHz
424 [ -5, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16 ], // 24kHz
425 [ -6, -4, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16 ], // 32kHz
426 [ -4, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20 ], // 44kHz
427 [ -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20, 24 ] // 64kHz