| 1 | |
| 2 | |
| 3 | |
| 4 | #![allow(dead_code)] |
| 5 | |
| 6 | use super::ops::*; |
| 7 | |
| 8 | const L_FIR_BP: usize = 30; |
| 9 | const L_FIR_LP: usize = 30; |
| 10 | const L_FILT16K_: usize = 15; |
| 11 | const SPEECH: i16 = 0; |
| 12 | |
| 13 | pub static HP_GAIN: [i16; 16] = [ |
| 14 | 3624, 4673, 5597, 6479, 7425, 8378, 9324, 10264, 11210, 12206, 13391, 14844, 16770, 19655, |
| 15 | 24289, 32728, |
| 16 | ]; |
| 17 | pub static FIR_UP: [[i16; 24]; 4] = [ |
| 18 | [ |
| 19 | -1, 12, -33, 68, -119, 191, -291, 430, -634, 963, -1616, 3792, 15317, -2496, 1288, -809, |
| 20 | 542, -369, 247, -160, 96, -52, 23, -6, |
| 21 | ], |
| 22 | [ |
| 23 | -4, 24, -62, 124, -213, 338, -510, 752, -1111, 1708, -2974, 8219, 12368, -3432, 1881, |
| 24 | -1204, 812, -552, 368, -235, 139, -73, 30, -7, |
| 25 | ], |
| 26 | [ |
| 27 | -7, 30, -73, 139, -235, 368, -552, 812, -1204, 1881, -3432, 12368, 8219, -2974, 1708, |
| 28 | -1111, 752, -510, 338, -213, 124, -62, 24, -4, |
| 29 | ], |
| 30 | [ |
| 31 | -6, 23, -52, 96, -160, 247, -369, 542, -809, 1288, -2496, 15317, 3792, -1616, 963, -634, |
| 32 | 430, -291, 191, -119, 68, -33, 12, -1, |
| 33 | ], |
| 34 | ]; |
| 35 | pub static FIR_6K_7K: [i16; 30] = [ |
| 36 | -32, 47, 32, -27, -369, 1122, -1421, 0, 3798, -8880, 12349, -10984, 3548, 7766, -18001, 22118, |
| 37 | -18001, 7766, 3548, -10984, 12349, -8880, 3798, 0, -1421, 1122, -369, -27, 32, 47, |
| 38 | ]; |
| 39 | pub static FIR_7K: [i16; 31] = [ |
| 40 | -21, 47, -89, 146, -203, 229, -177, 0, 335, -839, 1485, -2211, 2931, -3542, 3953, 28682, 3953, |
| 41 | -3542, 2931, -2211, 1485, -839, 335, 0, -177, 229, -203, 146, -89, 47, -21, |
| 42 | ]; |
| 43 | |
| 44 | |
| 45 | #[derive(Clone, Debug)] |
| 46 | pub struct SynthState { |
| 47 | pub mem_syn_hi: [i16; M], |
| 48 | pub mem_syn_lo: [i16; M], |
| 49 | pub mem_deemph: i16, |
| 50 | pub mem_sig_out: [i16; 6], |
| 51 | pub mem_oversamp: [i16; 2 * L_FILT], |
| 52 | pub mem_syn_hf: [i16; M16K], |
| 53 | pub mem_hf: [i16; 2 * L_FILT16K], |
| 54 | pub mem_hf3: [i16; 2 * L_FILT16K], |
| 55 | pub mem_hp400: [i16; 6], |
| 56 | pub seed2: i16, |
| 57 | pub vad_hist: i16, |
| 58 | } |
| 59 | |
| 60 | impl SynthState { |
| 61 | |
| 62 | pub fn new() -> Self { |
| 63 | SynthState { |
| 64 | mem_syn_hi: [0; M], |
| 65 | mem_syn_lo: [0; M], |
| 66 | mem_deemph: 0, |
| 67 | mem_sig_out: [0; 6], |
| 68 | mem_oversamp: [0; 2 * L_FILT], |
| 69 | mem_syn_hf: [0; M16K], |
| 70 | mem_hf: [0; 2 * L_FILT16K], |
| 71 | mem_hf3: [0; 2 * L_FILT16K], |
| 72 | mem_hp400: [0; 6], |
| 73 | seed2: 21845, |
| 74 | vad_hist: 0, |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | |
| 79 | pub fn reset(&mut self, reset_all: bool) { |
| 80 | if reset_all { |
| 81 | self.mem_oversamp = [0; 2 * L_FILT]; |
| 82 | self.mem_hf = [0; 2 * L_FILT16K]; |
| 83 | self.mem_hf3 = [0; 2 * L_FILT16K]; |
| 84 | self.mem_sig_out = [0; 6]; |
| 85 | self.mem_hp400 = [0; 6]; |
| 86 | self.mem_deemph = 0; |
| 87 | self.seed2 = 21845; |
| 88 | self.mem_syn_hf = [0; M16K]; |
| 89 | self.mem_syn_hi = [0; M]; |
| 90 | self.mem_syn_lo = [0; M]; |
| 91 | self.vad_hist = 0; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | impl Default for SynthState { |
| 97 | fn default() -> Self { |
| 98 | Self::new() |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | |
| 103 | |
| 104 | pub fn wb_syn_filt( |
| 105 | a: &[i16], |
| 106 | m: i16, |
| 107 | x: &[i16], |
| 108 | y: &mut [i16], |
| 109 | lg: i16, |
| 110 | mem: &mut [i16], |
| 111 | update: i16, |
| 112 | y_buf: &mut [i16], |
| 113 | ) { |
| 114 | let m = m as usize; |
| 115 | let lg = lg as usize; |
| 116 | y_buf[..m].copy_from_slice(&mem[..m]); |
| 117 | for n in 0..(lg >> 2) << 2 { |
| 118 | let mut l: i32 = -((x[n] as i32) << 11); |
| 119 | for t in 1..=m { |
| 120 | l = fxp_mac_16by16(y_buf[m + n - t], a[t], l); |
| 121 | } |
| 122 | let l = shl_int32(l, 4); |
| 123 | let v = amr_wb_round(l.wrapping_neg()); |
| 124 | y[n] = v; |
| 125 | y_buf[m + n] = v; |
| 126 | } |
| 127 | if update != 0 { |
| 128 | mem[..m].copy_from_slice(&y[lg - m..lg]); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | |
| 133 | |
| 134 | pub fn syn_filt_32( |
| 135 | a: &[i16], |
| 136 | m: i16, |
| 137 | exc: &[i16], |
| 138 | q_new: i16, |
| 139 | sig_hi: &mut [i16], |
| 140 | sig_lo: &mut [i16], |
| 141 | lg: i16, |
| 142 | ) { |
| 143 | let m = m as usize; |
| 144 | let a0 = (9i16.wrapping_sub(q_new)) as u32; |
| 145 | for n in 0..lg as usize { |
| 146 | let mut lo_s: i32 = 0; |
| 147 | let mut hi_s: i32 = 0; |
| 148 | for k in 1..=m { |
| 149 | lo_s = fxp_mac_16by16(sig_lo[m + n - k], a[k], lo_s); |
| 150 | hi_s = fxp_mac_16by16(sig_hi[m + n - k], a[k], hi_s); |
| 151 | } |
| 152 | let mut t: i32 = lo_s.wrapping_neg() >> 11; |
| 153 | t = t.wrapping_add((exc[n] as i32).wrapping_shl(a0)); |
| 154 | t = t.wrapping_sub(hi_s.wrapping_shl(1)); |
| 155 | t = shl_int32(t, 3); |
| 156 | sig_hi[m + n] = (t >> 16) as i16; |
| 157 | sig_lo[m + n] = ((t >> 4).wrapping_sub((t >> 16).wrapping_shl(12))) as i16; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | |
| 162 | pub fn deemphasis_32(x_hi: &[i16], x_lo: &[i16], y: &mut [i16], mu: i16, l: i16, mem: &mut i16) { |
| 163 | let l = l as usize; |
| 164 | let step = |hi: i16, lo: i16, prev: i16| -> i16 { |
| 165 | let mut t: i32 = (hi as i32).wrapping_shl(16); |
| 166 | t = t.wrapping_add((lo as i32).wrapping_shl(4)); |
| 167 | t = shl_int32(t, 3); |
| 168 | t = fxp_mac_16by16(prev, mu, t); |
| 169 | t = shl_int32(t, 1); |
| 170 | amr_wb_round(t) |
| 171 | }; |
| 172 | y[0] = step(x_hi[0], x_lo[0], *mem); |
| 173 | for i in 1..l { |
| 174 | y[i] = step(x_hi[i], x_lo[i], y[i - 1]); |
| 175 | } |
| 176 | *mem = y[l - 1]; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | pub fn highpass_50hz_at_12k8(signal: &mut [i16], lg: i16, mem: &mut [i16]) { |
| 181 | let (mut y2_hi, mut y2_lo, mut y1_hi, mut y1_lo, mut x0, mut x1) = |
| 182 | (mem[0], mem[1], mem[2], mem[3], mem[4], mem[5]); |
| 183 | for i in 0..lg as usize { |
| 184 | let mut l1 = fxp_mac_16by16(y1_lo, 16211, 8192); |
| 185 | l1 = fxp_mac_16by16(y2_lo, -8021, l1); |
| 186 | let mut l2 = fxp_mul_16by16(y1_hi, 32422); |
| 187 | l2 = fxp_mac_16by16(y2_hi, -16042, l2); |
| 188 | let x2 = x1; |
| 189 | x1 = x0; |
| 190 | x0 = signal[i]; |
| 191 | l2 = fxp_mac_16by16(x2, 8106, l2); |
| 192 | l2 = fxp_mac_16by16(x1, -16212, l2); |
| 193 | l2 = fxp_mac_16by16(x0, 8106, l2); |
| 194 | l1 = ((l1 >> 14).wrapping_add(l2)).wrapping_shl(2); |
| 195 | y2_hi = y1_hi; |
| 196 | y2_lo = y1_lo; |
| 197 | y1_hi = (l1 >> 16) as i16; |
| 198 | y1_lo = (l1.wrapping_sub((y1_hi as i32).wrapping_shl(16)) >> 1) as i16; |
| 199 | signal[i] = amr_wb_shl1_round(l1); |
| 200 | } |
| 201 | mem[0] = y2_hi; |
| 202 | mem[1] = y2_lo; |
| 203 | mem[2] = y1_hi; |
| 204 | mem[3] = y1_lo; |
| 205 | mem[4] = x0; |
| 206 | mem[5] = x1; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | pub fn highpass_400hz_at_12k8(signal: &mut [i16], lg: i16, mem: &mut [i16]) { |
| 211 | let (mut y2_hi, mut y2_lo, mut y1_hi, mut y1_lo, mut x0, mut x1) = |
| 212 | (mem[0], mem[1], mem[2], mem[3], mem[4], mem[5]); |
| 213 | for i in 0..lg as usize { |
| 214 | let mut l1 = fxp_mac_16by16(y1_lo, 29280, 8192); |
| 215 | let mut l2 = fxp_mul_16by16(y1_hi, 29280); |
| 216 | l1 = fxp_mac_16by16(y2_lo, -14160, l1); |
| 217 | l2 = fxp_mac_16by16(y2_hi, -14160, l2); |
| 218 | let x2 = x1; |
| 219 | x1 = x0; |
| 220 | x0 = signal[i]; |
| 221 | l2 = fxp_mac_16by16(x2, 915, l2); |
| 222 | l2 = fxp_mac_16by16(x1, -1830, l2); |
| 223 | l2 = fxp_mac_16by16(x0, 915, l2); |
| 224 | l1 = (l1 >> 13).wrapping_add(l2.wrapping_shl(2)); |
| 225 | y2_hi = y1_hi; |
| 226 | y2_lo = y1_lo; |
| 227 | signal[i] = (l1.wrapping_add(0x8000) >> 16) as i16; |
| 228 | y1_hi = (l1 >> 16) as i16; |
| 229 | y1_lo = (l1.wrapping_sub((y1_hi as i32).wrapping_shl(16)) >> 1) as i16; |
| 230 | } |
| 231 | mem[0] = y2_hi; |
| 232 | mem[1] = y2_lo; |
| 233 | mem[2] = y1_hi; |
| 234 | mem[3] = y1_lo; |
| 235 | mem[4] = x0; |
| 236 | mem[5] = x1; |
| 237 | } |
| 238 | |
| 239 | |
| 240 | fn amr_wb_interpol(x: &[i16], base: usize, fir: &[i16; 24]) -> i16 { |
| 241 | let start = base - 11; |
| 242 | let mut l: i32 = 0x2000; |
| 243 | for t in 0..24 { |
| 244 | l = fxp_mac_16by16(x[start + t], fir[t], l); |
| 245 | } |
| 246 | l = shl_int32(l, 2); |
| 247 | (l >> 16) as i16 |
| 248 | } |
| 249 | |
| 250 | |
| 251 | fn amr_wb_up_samp(sig_d: &[i16], base: usize, sig_u: &mut [i16], l_frame: i16) { |
| 252 | let mut frac: i16 = 1; |
| 253 | for j in 0..l_frame as i32 { |
| 254 | let i = ((j * 6554) >> 13) as usize; |
| 255 | frac -= 1; |
| 256 | if frac != 0 { |
| 257 | sig_u[j as usize] = amr_wb_interpol(sig_d, base + i, &FIR_UP[(5 - 1 - frac) as usize]); |
| 258 | } else { |
| 259 | sig_u[j as usize] = sig_d[base + i]; |
| 260 | frac = 5; |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | |
| 266 | pub fn oversamp_12k8_to_16k(sig12k8: &[i16], lg: i16, sig16k: &mut [i16], mem: &mut [i16]) { |
| 267 | let lgu = lg as usize; |
| 268 | let mut signal = vec![0i16; 24 + lgu]; |
| 269 | signal[..24].copy_from_slice(&mem[..24]); |
| 270 | signal[24..24 + lgu].copy_from_slice(&sig12k8[..lgu]); |
| 271 | let lg_up = lg + (lg >> 2); |
| 272 | amr_wb_up_samp(&signal, 12, sig16k, lg_up); |
| 273 | mem[..24].copy_from_slice(&signal[lgu..lgu + 24]); |
| 274 | } |
| 275 | |
| 276 | |
| 277 | pub fn band_pass_6k_7k(signal: &mut [i16], lg: i16, mem: &mut [i16]) { |
| 278 | let lg = lg as usize; |
| 279 | let mut x = vec![0i16; L_FIR_BP + lg]; |
| 280 | x[..L_FIR_BP].copy_from_slice(&mem[..L_FIR_BP]); |
| 281 | for n in 0..lg { |
| 282 | x[L_FIR_BP + n] = signal[n] >> 2; |
| 283 | } |
| 284 | for n in 0..lg { |
| 285 | let mut l: i32 = 0x4000; |
| 286 | l = l.wrapping_sub((x[n + L_FIR_BP] as i32).wrapping_shl(5)); |
| 287 | l = l.wrapping_sub((x[n] as i32).wrapping_shl(5)); |
| 288 | for j in 1..L_FIR_BP { |
| 289 | l = fxp_mac_16by16(x[n + j], FIR_6K_7K[j], l); |
| 290 | } |
| 291 | signal[n] = (l >> 15) as i16; |
| 292 | } |
| 293 | mem[..L_FIR_BP].copy_from_slice(&x[lg..lg + L_FIR_BP]); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | pub fn low_pass_filt_7k(signal: &mut [i16], lg: i16, mem: &mut [i16]) { |
| 298 | let lg = lg as usize; |
| 299 | let mut x = vec![0i16; L_FIR_LP + lg]; |
| 300 | x[..L_FIR_LP].copy_from_slice(&mem[..L_FIR_LP]); |
| 301 | for n in 0..lg { |
| 302 | x[L_FIR_LP + n] = signal[n]; |
| 303 | } |
| 304 | for n in 0..lg { |
| 305 | let s = (x[n] as i32 + signal[n] as i32) as i16; |
| 306 | let mut l = fxp_mac_16by16(s, FIR_7K[0], 0x4000); |
| 307 | for j in 1..L_FIR_LP { |
| 308 | l = fxp_mac_16by16(x[n + j], FIR_7K[j], l); |
| 309 | } |
| 310 | signal[n] = (l >> 15) as i16; |
| 311 | } |
| 312 | mem[..L_FIR_LP].copy_from_slice(&x[lg..lg + L_FIR_LP]); |
| 313 | } |
| 314 | |
| 315 | #[inline] |
| 316 | fn shl_trunc(v: i16, s: i16) -> i16 { |
| 317 | ((v as i32).wrapping_shl((s as u32) & 31)) as i16 |
| 318 | } |
| 319 | |
| 320 | |
| 321 | pub fn isf_extrapolation(hf_isf: &mut [i16]) { |
| 322 | let mut isf_diff = [0i16; M - 2]; |
| 323 | let mut isf_corr = [0i32; 3]; |
| 324 | hf_isf[M16K - 1] = hf_isf[M - 1]; |
| 325 | for i in 1..(M - 1) { |
| 326 | isf_diff[i - 1] = sub_int16(hf_isf[i], hf_isf[i - 1]); |
| 327 | } |
| 328 | let mut l_tmp: i32 = 0; |
| 329 | for i in 3..(M - 1) { |
| 330 | l_tmp = mac_16by16_to_int32(l_tmp, isf_diff[i - 1], 2731); |
| 331 | } |
| 332 | let mut mean = amr_wb_round(l_tmp); |
| 333 | let mut tmp: i16 = 0; |
| 334 | for i in 0..(M - 2) { |
| 335 | if isf_diff[i] > tmp { |
| 336 | tmp = isf_diff[i]; |
| 337 | } |
| 338 | } |
| 339 | let exp = norm_s(tmp); |
| 340 | for i in 0..(M - 2) { |
| 341 | isf_diff[i] = shl_int16(isf_diff[i], exp); |
| 342 | } |
| 343 | mean = shl_int16(mean, exp); |
| 344 | for (c, lagv) in [(0usize, 2usize), (1, 3), (2, 4)] { |
| 345 | isf_corr[c] = 0; |
| 346 | for i in 7..(M - 2) { |
| 347 | let tmp2 = sub_int16(isf_diff[i], mean); |
| 348 | let tmp3 = sub_int16(isf_diff[i - lagv], mean); |
| 349 | let l = mul_16by16_to_int32(tmp2, tmp3); |
| 350 | let (hi, lo) = super::math::int32_to_dpf(l); |
| 351 | let l = super::math::mpy_dpf_32(hi, lo, hi, lo); |
| 352 | isf_corr[c] = add_int32(isf_corr[c], l); |
| 353 | } |
| 354 | } |
| 355 | let mut max_corr: usize = if isf_corr[0] > isf_corr[1] { 0 } else { 1 }; |
| 356 | if isf_corr[2] > isf_corr[max_corr] { |
| 357 | max_corr = 2; |
| 358 | } |
| 359 | max_corr += 1; |
| 360 | for i in (M - 1)..(M16K - 1) { |
| 361 | let t = sub_int16(hf_isf[i - 1 - max_corr], hf_isf[i - 2 - max_corr]); |
| 362 | hf_isf[i] = add_int16(hf_isf[i - 1], t); |
| 363 | } |
| 364 | let mut tmp = add_int16(hf_isf[4], hf_isf[3]); |
| 365 | tmp = sub_int16(hf_isf[2], tmp); |
| 366 | tmp = mult_int16(tmp, 5461); |
| 367 | tmp = tmp.wrapping_add(20390); |
| 368 | if tmp > 19456 { |
| 369 | tmp = 19456; |
| 370 | } |
| 371 | tmp = sub_int16(tmp, hf_isf[M - 2]); |
| 372 | let mut tmp2 = sub_int16(hf_isf[M16K - 2], hf_isf[M - 2]); |
| 373 | let exp2 = norm_s(tmp2); |
| 374 | let mut exp = norm_s(tmp); |
| 375 | exp = exp.wrapping_sub(1); |
| 376 | tmp = shl_trunc(tmp, exp); |
| 377 | tmp2 = shl_trunc(tmp2, exp2); |
| 378 | let coeff = super::math::div_16by16(tmp, tmp2); |
| 379 | let exp = exp2.wrapping_sub(exp); |
| 380 | for i in (M - 1)..(M16K - 1) { |
| 381 | let t = mult_int16(sub_int16(hf_isf[i], hf_isf[i - 1]), coeff); |
| 382 | isf_diff[i - (M - 1)] = shl_int16(t, exp); |
| 383 | } |
| 384 | for i in M..(M16K - 1) { |
| 385 | let t = (isf_diff[i - (M - 1)] as i32 + isf_diff[i - M] as i32 - 1280) as i16; |
| 386 | if t < 0 { |
| 387 | if isf_diff[i - (M - 1)] > isf_diff[i - M] { |
| 388 | isf_diff[i - M] = (1280 - isf_diff[i - (M - 1)] as i32) as i16; |
| 389 | } else { |
| 390 | isf_diff[i - (M - 1)] = (1280 - isf_diff[i - M] as i32) as i16; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | for i in (M - 1)..(M16K - 1) { |
| 395 | hf_isf[i] = add_int16(hf_isf[i - 1], isf_diff[i - (M - 1)]); |
| 396 | } |
| 397 | for i in 0..(M16K - 1) { |
| 398 | hf_isf[i] = mult_int16(hf_isf[i], 26214); |
| 399 | } |
| 400 | let src: Vec<i16> = hf_isf[..M16K].to_vec(); |
| 401 | super::isf::isf_isp(&src, hf_isf, M16K as i16); |
| 402 | } |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | pub fn synthesis_amr_wb( |
| 409 | a: &[i16], |
| 410 | exc: &mut [i16], |
| 411 | q_new: i16, |
| 412 | synth16k: &mut [i16], |
| 413 | prms: i16, |
| 414 | hf_isf: &mut [i16], |
| 415 | nb_bits: i16, |
| 416 | new_dtx_state: i16, |
| 417 | st: &mut SynthState, |
| 418 | bfi: i16, |
| 419 | ) { |
| 420 | let mut q_new = q_new; |
| 421 | let mut synth_hi = vec![0i16; M + L_SUBFR]; |
| 422 | let mut synth_lo = vec![0i16; M + L_SUBFR]; |
| 423 | let mut synth = vec![0i16; L_SUBFR]; |
| 424 | let mut hf = vec![0i16; L_SUBFR16K]; |
| 425 | let mut ap = vec![0i16; M16K + 1]; |
| 426 | let mut hf_a = vec![0i16; M16K + 1]; |
| 427 | let mut scratch = vec![0i16; 2 * M16K + L_SUBFR16K + 64]; |
| 428 | |
| 429 | synth_hi[..M].copy_from_slice(&st.mem_syn_hi); |
| 430 | synth_lo[..M].copy_from_slice(&st.mem_syn_lo); |
| 431 | |
| 432 | syn_filt_32( |
| 433 | a, |
| 434 | M as i16, |
| 435 | exc, |
| 436 | q_new, |
| 437 | &mut synth_hi, |
| 438 | &mut synth_lo, |
| 439 | L_SUBFR as i16, |
| 440 | ); |
| 441 | |
| 442 | st.mem_syn_hi |
| 443 | .copy_from_slice(&synth_hi[L_SUBFR..L_SUBFR + M]); |
| 444 | st.mem_syn_lo |
| 445 | .copy_from_slice(&synth_lo[L_SUBFR..L_SUBFR + M]); |
| 446 | |
| 447 | deemphasis_32( |
| 448 | &synth_hi[M..], |
| 449 | &synth_lo[M..], |
| 450 | &mut synth, |
| 451 | PREEMPH_FAC, |
| 452 | L_SUBFR as i16, |
| 453 | &mut st.mem_deemph, |
| 454 | ); |
| 455 | |
| 456 | highpass_50hz_at_12k8(&mut synth, L_SUBFR as i16, &mut st.mem_sig_out); |
| 457 | |
| 458 | oversamp_12k8_to_16k(&synth, L_SUBFR as i16, synth16k, &mut st.mem_oversamp); |
| 459 | |
| 460 | for v in hf.iter_mut() { |
| 461 | *v = super::exc::noise_gen_amrwb(&mut st.seed2) >> 3; |
| 462 | } |
| 463 | |
| 464 | for v in exc[..L_SUBFR].iter_mut() { |
| 465 | *v = add_int16(*v, 0x0004) >> 3; |
| 466 | } |
| 467 | |
| 468 | q_new = q_new.wrapping_sub(3); |
| 469 | let mut exp_ener: i16 = 0; |
| 470 | let mut exp: i16 = 0; |
| 471 | let mut ener = super::math::extract_h(super::math::dot_product12( |
| 472 | exc, |
| 473 | exc, |
| 474 | L_SUBFR as i16, |
| 475 | &mut exp_ener, |
| 476 | )); |
| 477 | exp_ener = exp_ener.wrapping_sub(q_new.wrapping_shl(1)); |
| 478 | |
| 479 | let mut tmp = super::math::extract_h(super::math::dot_product12( |
| 480 | &hf, |
| 481 | &hf, |
| 482 | L_SUBFR16K as i16, |
| 483 | &mut exp, |
| 484 | )); |
| 485 | if tmp > ener { |
| 486 | tmp >>= 1; |
| 487 | exp = exp.wrapping_add(1); |
| 488 | } |
| 489 | let mut l_tmp = super::math::l_deposit_h(super::math::div_16by16(tmp, ener)); |
| 490 | exp = exp.wrapping_sub(exp_ener); |
| 491 | super::math::one_ov_sqrt_norm(&mut l_tmp, &mut exp); |
| 492 | l_tmp = shl_int32(l_tmp, exp.wrapping_add(1)); |
| 493 | tmp = (l_tmp >> 16) as i16; |
| 494 | |
| 495 | for v in hf.iter_mut() { |
| 496 | *v = fxp_mul_16by16(*v, tmp).wrapping_shr(15) as i16; |
| 497 | } |
| 498 | |
| 499 | highpass_400hz_at_12k8(&mut synth, L_SUBFR as i16, &mut st.mem_hp400); |
| 500 | |
| 501 | let mut l_tmp: i32 = 1; |
| 502 | let mut l_tmp2: i32 = 1; |
| 503 | l_tmp = mac_16by16_to_int32(l_tmp, synth[0], synth[0]); |
| 504 | for i in 1..L_SUBFR { |
| 505 | l_tmp = mac_16by16_to_int32(l_tmp, synth[i], synth[i]); |
| 506 | l_tmp2 = mac_16by16_to_int32(l_tmp2, synth[i], synth[i - 1]); |
| 507 | } |
| 508 | exp = normalize_amr_wb(l_tmp); |
| 509 | ener = (l_tmp.wrapping_shl(exp as u32 & 31) >> 16) as i16; |
| 510 | tmp = (l_tmp2.wrapping_shl(exp as u32 & 31) >> 16) as i16; |
| 511 | let fac = if tmp > 0 { |
| 512 | super::math::div_16by16(tmp, ener) |
| 513 | } else { |
| 514 | 0 |
| 515 | }; |
| 516 | |
| 517 | let gain1: i16 = 32767i16.wrapping_sub(fac); |
| 518 | let mut gain2 = mult_int16(gain1, 20480); |
| 519 | gain2 = shl_int16(gain2, 1); |
| 520 | tmp = if st.vad_hist > 0 { |
| 521 | gain2.wrapping_sub(1) |
| 522 | } else { |
| 523 | gain1.wrapping_sub(1) |
| 524 | }; |
| 525 | if tmp != 0 { |
| 526 | tmp = tmp.wrapping_add(1); |
| 527 | } |
| 528 | if tmp < 3277 { |
| 529 | tmp = 3277; |
| 530 | } |
| 531 | |
| 532 | if nb_bits >= NBBITS_24K && bfi == 0 { |
| 533 | let hf_corr_gain = HP_GAIN[(prms as usize) & 15]; |
| 534 | for v in hf.iter_mut() { |
| 535 | *v = mult_int16(*v, hf_corr_gain).wrapping_shl(1); |
| 536 | } |
| 537 | } else { |
| 538 | for v in hf.iter_mut() { |
| 539 | *v = mult_int16(*v, tmp); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | let hf_in = hf.clone(); |
| 544 | if nb_bits <= NBBITS_7K && new_dtx_state == SPEECH { |
| 545 | isf_extrapolation(hf_isf); |
| 546 | super::isf::isp_az(hf_isf, &mut hf_a, M16K as i16, 0); |
| 547 | super::isf::weight_amrwb_lpc(&hf_a, &mut ap, 29491, M16K as i16); |
| 548 | wb_syn_filt( |
| 549 | &ap, |
| 550 | M16K as i16, |
| 551 | &hf_in, |
| 552 | &mut hf, |
| 553 | L_SUBFR16K as i16, |
| 554 | &mut st.mem_syn_hf, |
| 555 | 1, |
| 556 | &mut scratch, |
| 557 | ); |
| 558 | } else { |
| 559 | super::isf::weight_amrwb_lpc(a, &mut ap, 19661, M as i16); |
| 560 | wb_syn_filt( |
| 561 | &ap, |
| 562 | M as i16, |
| 563 | &hf_in, |
| 564 | &mut hf, |
| 565 | L_SUBFR16K as i16, |
| 566 | &mut st.mem_syn_hf[M16K - M..], |
| 567 | 1, |
| 568 | &mut scratch, |
| 569 | ); |
| 570 | } |
| 571 | |
| 572 | band_pass_6k_7k(&mut hf, L_SUBFR16K as i16, &mut st.mem_hf); |
| 573 | |
| 574 | if nb_bits >= NBBITS_24K { |
| 575 | low_pass_filt_7k(&mut hf, L_SUBFR16K as i16, &mut st.mem_hf3); |
| 576 | } |
| 577 | |
| 578 | for i in 0..L_SUBFR16K { |
| 579 | synth16k[i] = add_int16(synth16k[i], hf[i]); |
| 580 | } |
| 581 | } |