edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·370.3 MiB·9b67700·Tahinli·3 days ago·4021 commits
| 1 | //! Port of `pvamrwbdecoder.cpp`: the AMR-WB frame decoder (state + `pvDecoder_AmrWb`). |
| 2 | |
| 3 | use super::bits::{Prms, Serial_parm, Serial_parm_1bit}; |
| 4 | use super::dtx::*; |
| 5 | use super::exc::*; |
| 6 | use super::isf::*; |
| 7 | use super::math::extract_h; |
| 8 | use super::ops::*; |
| 9 | use super::synth::{SynthState, synthesis_amr_wb}; |
| 10 | |
| 11 | const INTERPOL_FRAC: [i16; NB_SUBFR] = [14746, 26214, 31457, 32767]; |
| 12 | |
| 13 | const ISP_INIT: [i16; M] = [ |
| 14 | 32138, 30274, 27246, 23170, 18205, 12540, 6393, 0, -6393, -12540, -18205, -23170, -27246, |
| 15 | -30274, -32138, 1475, |
| 16 | ]; |
| 17 | const ISF_INIT: [i16; M] = [ |
| 18 | 1024, 2048, 3072, 4096, 5120, 6144, 7168, 8192, 9216, 10240, 11264, 12288, 13312, 14336, 15360, |
| 19 | 3840, |
| 20 | ]; |
| 21 | |
| 22 | const OLD_EXC_LEN: usize = PIT_MAX as usize + L_INTERPOL; |
| 23 | |
| 24 | /// Decoder state (`Decoder_State`). |
| 25 | pub struct DecoderState { |
| 26 | pub old_exc: [i16; OLD_EXC_LEN], |
| 27 | pub ispold: [i16; M], |
| 28 | pub isfold: [i16; M], |
| 29 | pub isf_buf: [i16; L_MEANBUF * M], |
| 30 | pub past_isfq: [i16; M], |
| 31 | pub tilt_code: i16, |
| 32 | pub q_old: i16, |
| 33 | pub qsubfr: [i16; 4], |
| 34 | pub l_gc_thres: i32, |
| 35 | pub synth: SynthState, |
| 36 | pub seed: i16, |
| 37 | pub old_t0: i16, |
| 38 | pub old_t0_frac: i16, |
| 39 | pub lag_hist: [i16; 5], |
| 40 | pub dec_gain: [i16; 23], |
| 41 | pub seed3: i16, |
| 42 | pub disp_mem: [i16; 8], |
| 43 | pub prev_bfi: i16, |
| 44 | pub state: i16, |
| 45 | pub first_frame: i16, |
| 46 | pub dtx: DtxDecState, |
| 47 | } |
| 48 | |
| 49 | impl DecoderState { |
| 50 | /// `pvDecoder_AmrWb_Init`. |
| 51 | pub fn new() -> Box<DecoderState> { |
| 52 | let mut s = Box::new(DecoderState { |
| 53 | old_exc: [0; OLD_EXC_LEN], |
| 54 | ispold: [0; M], |
| 55 | isfold: [0; M], |
| 56 | isf_buf: [0; L_MEANBUF * M], |
| 57 | past_isfq: [0; M], |
| 58 | tilt_code: 0, |
| 59 | q_old: Q_MAX, |
| 60 | qsubfr: [Q_MAX; 4], |
| 61 | l_gc_thres: 0, |
| 62 | synth: SynthState::new(), |
| 63 | seed: RANDOM_INITSEED, |
| 64 | old_t0: 64, |
| 65 | old_t0_frac: 0, |
| 66 | lag_hist: [0; 5], |
| 67 | dec_gain: [0; 23], |
| 68 | seed3: RANDOM_INITSEED, |
| 69 | disp_mem: [0; 8], |
| 70 | prev_bfi: 0, |
| 71 | state: 0, |
| 72 | first_frame: 1, |
| 73 | dtx: DtxDecState::default(), |
| 74 | }); |
| 75 | dtx_dec_amr_wb_reset(&mut s.dtx, &ISF_INIT); |
| 76 | s.reset(true); |
| 77 | s |
| 78 | } |
| 79 | |
| 80 | /// `pvDecoder_AmrWb_Reset`. |
| 81 | pub fn reset(&mut self, reset_all: bool) { |
| 82 | self.old_exc = [0; OLD_EXC_LEN]; |
| 83 | self.past_isfq = [0; M]; |
| 84 | self.old_t0_frac = 0; |
| 85 | self.old_t0 = 64; |
| 86 | self.first_frame = 1; |
| 87 | self.l_gc_thres = 0; |
| 88 | self.tilt_code = 0; |
| 89 | self.disp_mem = [0; 8]; |
| 90 | self.q_old = Q_MAX; |
| 91 | self.qsubfr = [Q_MAX; 4]; |
| 92 | if reset_all { |
| 93 | dec_gain2_amr_wb_init(&mut self.dec_gain); |
| 94 | self.synth.reset(true); |
| 95 | init_lagconc(&mut self.lag_hist); |
| 96 | self.ispold = ISP_INIT; |
| 97 | self.isfold = ISF_INIT; |
| 98 | for i in 0..L_MEANBUF { |
| 99 | self.isf_buf[i * M..(i + 1) * M].copy_from_slice(&ISF_INIT); |
| 100 | } |
| 101 | self.synth.mem_deemph = 0; |
| 102 | self.seed = RANDOM_INITSEED; |
| 103 | self.synth.seed2 = RANDOM_INITSEED; |
| 104 | self.seed3 = RANDOM_INITSEED; |
| 105 | self.state = 0; |
| 106 | self.prev_bfi = 0; |
| 107 | self.synth.mem_syn_hf = [0; M16K]; |
| 108 | self.synth.mem_syn_hi = [0; M]; |
| 109 | self.synth.mem_syn_lo = [0; M]; |
| 110 | dtx_dec_amr_wb_reset(&mut self.dtx, &ISF_INIT); |
| 111 | self.synth.vad_hist = 0; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /// `pvDecoder_AmrWb`: decode one frame of parameters (`prms`, one bit per i16 in |
| 117 | /// the decoder's serial order) into 320 samples. |
| 118 | pub fn decode_frame( |
| 119 | mode: i16, |
| 120 | prms_buf: &[i16], |
| 121 | synth16k: &mut [i16], |
| 122 | st: &mut DecoderState, |
| 123 | frame_type: i16, |
| 124 | ) { |
| 125 | let mut prms = Prms::new(prms_buf); |
| 126 | let nb_bits = AMR_WB_COMPRESSED[(mode & 15).min(9) as usize]; |
| 127 | |
| 128 | let mut old_exc = [0i16; (L_FRAME + 1) + PIT_MAX as usize + L_INTERPOL]; |
| 129 | let mut aq = [0i16; NB_SUBFR * (M + 1)]; |
| 130 | let mut ispnew = [0i16; M]; |
| 131 | let mut isf = [0i16; M]; |
| 132 | let mut isf_tmp = [0i16; M]; |
| 133 | let mut code = [0i16; L_SUBFR]; |
| 134 | let mut excp = [0i16; L_SUBFR]; |
| 135 | let mut exc2 = [0i16; L_FRAME]; |
| 136 | let mut hf_isf = [0i16; M16K]; |
| 137 | let mut scratch = [0i16; 256]; |
| 138 | let eo = OLD_EXC_LEN; // index of exc[0] inside old_exc |
| 139 | |
| 140 | let new_dtx_state = rx_amr_wb_dtx_handler(&mut st.dtx, frame_type); |
| 141 | if new_dtx_state != SPEECH { |
| 142 | dtx_dec_amr_wb(&mut st.dtx, &mut exc2, new_dtx_state, &mut isf, &mut prms); |
| 143 | } |
| 144 | |
| 145 | let (bfi, unusable_frame): (i16, i16); |
| 146 | if frame_type == RX_SPEECH_BAD || frame_type == RX_SPEECH_PROBABLY_DEGRADED { |
| 147 | bfi = 1; |
| 148 | unusable_frame = 0; |
| 149 | } else if frame_type == RX_NO_DATA || frame_type == RX_SPEECH_LOST { |
| 150 | bfi = 1; |
| 151 | unusable_frame = 1; |
| 152 | } else { |
| 153 | bfi = 0; |
| 154 | unusable_frame = 0; |
| 155 | } |
| 156 | if bfi != 0 { |
| 157 | st.state += 1; |
| 158 | if st.state > 6 { |
| 159 | st.state = 6; |
| 160 | } |
| 161 | } else { |
| 162 | st.state >>= 1; |
| 163 | } |
| 164 | |
| 165 | if st.dtx.dtxGlobalState == DTX { |
| 166 | st.state = 5; |
| 167 | st.prev_bfi = 0; |
| 168 | } else if st.dtx.dtxGlobalState == DTX_MUTE { |
| 169 | st.state = 5; |
| 170 | st.prev_bfi = 1; |
| 171 | } |
| 172 | |
| 173 | if new_dtx_state == SPEECH { |
| 174 | let vad_flag = Serial_parm_1bit(&mut prms); |
| 175 | if bfi == 0 { |
| 176 | if vad_flag == 0 { |
| 177 | st.synth.vad_hist = add_int16(st.synth.vad_hist, 1); |
| 178 | } else { |
| 179 | st.synth.vad_hist = 0; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // DTX-CNG |
| 185 | if new_dtx_state != SPEECH { |
| 186 | isf_isp(&isf, &mut ispnew, M as i16); |
| 187 | isp_az(&ispnew, &mut aq, M as i16, 1); |
| 188 | isf_tmp.copy_from_slice(&st.isfold); |
| 189 | let mut i_subfr = 0; |
| 190 | while i_subfr < L_FRAME { |
| 191 | let j = i_subfr >> 6; |
| 192 | for i in 0..M { |
| 193 | let mut l_tmp = mul_16by16_to_int32(isf_tmp[i], sub_int16(32767, INTERPOL_FRAC[j])); |
| 194 | l_tmp = mac_16by16_to_int32(l_tmp, isf[i], INTERPOL_FRAC[j]); |
| 195 | hf_isf[i] = amr_wb_round(l_tmp); |
| 196 | } |
| 197 | let out = i_subfr * 5 / 4; |
| 198 | synthesis_amr_wb( |
| 199 | &aq, |
| 200 | &mut exc2[i_subfr..i_subfr + L_SUBFR], |
| 201 | 0, |
| 202 | &mut synth16k[out..out + L_SUBFR16K], |
| 203 | 1, |
| 204 | &mut hf_isf, |
| 205 | nb_bits, |
| 206 | new_dtx_state, |
| 207 | &mut st.synth, |
| 208 | bfi, |
| 209 | ); |
| 210 | i_subfr += L_SUBFR; |
| 211 | } |
| 212 | st.reset(false); |
| 213 | st.isfold.copy_from_slice(&isf); |
| 214 | st.prev_bfi = bfi; |
| 215 | st.dtx.dtxGlobalState = new_dtx_state; |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | // ACELP |
| 220 | old_exc[..OLD_EXC_LEN].copy_from_slice(&st.old_exc); |
| 221 | let mut ind = [0i16; 8]; |
| 222 | |
| 223 | if nb_bits > NBBITS_7K { |
| 224 | ind[0] = Serial_parm(8, &mut prms); |
| 225 | ind[1] = Serial_parm(8, &mut prms); |
| 226 | ind[2] = Serial_parm(6, &mut prms); |
| 227 | ind[3] = Serial_parm(7, &mut prms); |
| 228 | ind[4] = Serial_parm(7, &mut prms); |
| 229 | ind[5] = Serial_parm(5, &mut prms); |
| 230 | ind[6] = Serial_parm(5, &mut prms); |
| 231 | dpisf_2s_46b( |
| 232 | &ind, |
| 233 | &mut isf, |
| 234 | &mut st.past_isfq, |
| 235 | &st.isfold, |
| 236 | &mut st.isf_buf, |
| 237 | bfi, |
| 238 | 1, |
| 239 | ); |
| 240 | } else { |
| 241 | ind[0] = Serial_parm(8, &mut prms); |
| 242 | ind[1] = Serial_parm(8, &mut prms); |
| 243 | ind[2] = Serial_parm(14, &mut prms); |
| 244 | ind[3] = ind[2] & 0x007F; |
| 245 | ind[2] >>= 7; |
| 246 | ind[4] = Serial_parm(6, &mut prms); |
| 247 | dpisf_2s_36b( |
| 248 | &ind, |
| 249 | &mut isf, |
| 250 | &mut st.past_isfq, |
| 251 | &st.isfold, |
| 252 | &mut st.isf_buf, |
| 253 | bfi, |
| 254 | 1, |
| 255 | ); |
| 256 | } |
| 257 | |
| 258 | isf_isp(&isf, &mut ispnew, M as i16); |
| 259 | if st.first_frame != 0 { |
| 260 | st.first_frame = 0; |
| 261 | st.ispold.copy_from_slice(&ispnew); |
| 262 | } |
| 263 | interpolate_isp(&st.ispold, &ispnew, &INTERPOL_FRAC, &mut aq); |
| 264 | st.ispold.copy_from_slice(&ispnew); |
| 265 | |
| 266 | // stability factor |
| 267 | let mut l_tmp: i32 = 0; |
| 268 | for i in 0..M - 1 { |
| 269 | let tmp = sub_int16(isf[i], st.isfold[i]); |
| 270 | l_tmp = mac_16by16_to_int32(l_tmp, tmp, tmp); |
| 271 | } |
| 272 | let mut tmp = extract_h(shl_int32(l_tmp, 8)); |
| 273 | tmp = mult_int16(tmp, 26214); |
| 274 | tmp = (20480i32 - tmp as i32) as i16; |
| 275 | let mut stab_fac = shl_int16(tmp, 1); |
| 276 | if stab_fac < 0 { |
| 277 | stab_fac = 0; |
| 278 | } |
| 279 | isf_tmp.copy_from_slice(&st.isfold); |
| 280 | st.isfold.copy_from_slice(&isf); |
| 281 | |
| 282 | let mut t0: i16 = 0; |
| 283 | let mut t0_frac: i16 = 0; |
| 284 | let mut t0_min: i16 = 0; |
| 285 | let mut t0_max: i16; |
| 286 | let mut q_new: i16 = 0; |
| 287 | let mut gain_pit: i16 = 0; |
| 288 | let mut l_gain_code: i32 = 0; |
| 289 | let mut p_aq = 0usize; |
| 290 | |
| 291 | let mut i_subfr = 0usize; |
| 292 | while i_subfr < L_FRAME { |
| 293 | let mut pit_flag = i_subfr as i16; |
| 294 | if i_subfr == 2 * L_SUBFR && nb_bits > NBBITS_7K { |
| 295 | pit_flag = 0; |
| 296 | } |
| 297 | let index: i16; |
| 298 | if pit_flag == 0 { |
| 299 | if nb_bits <= NBBITS_9K { |
| 300 | index = Serial_parm(8, &mut prms); |
| 301 | if index < (PIT_FR1_8B - PIT_MIN) * 2 { |
| 302 | t0 = PIT_MIN + (index >> 1); |
| 303 | t0_frac = sub_int16(index, shl_int16(sub_int16(t0, PIT_MIN), 1)); |
| 304 | t0_frac = shl_int16(t0_frac, 1); |
| 305 | } else { |
| 306 | t0 = add_int16(index, PIT_FR1_8B - ((PIT_FR1_8B - PIT_MIN) * 2)); |
| 307 | t0_frac = 0; |
| 308 | } |
| 309 | } else { |
| 310 | index = Serial_parm(9, &mut prms); |
| 311 | if index < (PIT_FR2 - PIT_MIN) * 4 { |
| 312 | t0 = PIT_MIN + (index >> 2); |
| 313 | t0_frac = sub_int16(index, shl_int16(sub_int16(t0, PIT_MIN), 2)); |
| 314 | } else if index < (((PIT_FR2 - PIT_MIN) << 2) + ((PIT_FR1_9B - PIT_FR2) << 1)) { |
| 315 | let idx = index - ((PIT_FR2 - PIT_MIN) << 2); |
| 316 | t0 = PIT_FR2 + (idx >> 1); |
| 317 | t0_frac = sub_int16(idx, shl_int16(sub_int16(t0, PIT_FR2), 1)); |
| 318 | t0_frac = shl_int16(t0_frac, 1); |
| 319 | } else { |
| 320 | t0 = add_int16( |
| 321 | index, |
| 322 | PIT_FR1_9B - ((PIT_FR2 - PIT_MIN) * 4) - ((PIT_FR1_9B - PIT_FR2) * 2), |
| 323 | ); |
| 324 | t0_frac = 0; |
| 325 | } |
| 326 | } |
| 327 | t0_min = t0 - 8; |
| 328 | if t0_min < PIT_MIN { |
| 329 | t0_min = PIT_MIN; |
| 330 | } |
| 331 | t0_max = t0_min + 15; |
| 332 | if t0_max > PIT_MAX { |
| 333 | t0_min = PIT_MAX - 15; |
| 334 | } |
| 335 | } else if nb_bits <= NBBITS_9K { |
| 336 | index = Serial_parm(5, &mut prms); |
| 337 | t0 = t0_min + (index >> 1); |
| 338 | t0_frac = sub_int16(index, shl_int16(t0 - t0_min, 1)); |
| 339 | t0_frac = shl_int16(t0_frac, 1); |
| 340 | } else { |
| 341 | index = Serial_parm(6, &mut prms); |
| 342 | t0 = t0_min + (index >> 2); |
| 343 | t0_frac = sub_int16(index, shl_int16(t0 - t0_min, 2)); |
| 344 | } |
| 345 | |
| 346 | if bfi != 0 { |
| 347 | let old_t0 = st.old_t0; |
| 348 | lagconceal( |
| 349 | &st.dec_gain[17..], |
| 350 | &st.lag_hist, |
| 351 | &mut t0, |
| 352 | &old_t0, |
| 353 | &mut st.seed3, |
| 354 | unusable_frame, |
| 355 | ); |
| 356 | t0_frac = 0; |
| 357 | } |
| 358 | |
| 359 | pred_lt4( |
| 360 | &mut old_exc, |
| 361 | eo + i_subfr, |
| 362 | t0, |
| 363 | t0_frac, |
| 364 | (L_SUBFR + 1) as i16, |
| 365 | ); |
| 366 | |
| 367 | let select: i16 = if unusable_frame != 0 { |
| 368 | 1 |
| 369 | } else if nb_bits <= NBBITS_9K { |
| 370 | 0 |
| 371 | } else { |
| 372 | Serial_parm_1bit(&mut prms) |
| 373 | }; |
| 374 | |
| 375 | if select == 0 { |
| 376 | for i in 0..L_SUBFR { |
| 377 | let base = eo + i_subfr + i; |
| 378 | let mut l: i32 = old_exc[base - 1] as i32 + old_exc[base + 1] as i32; |
| 379 | l = l.wrapping_mul(5898); |
| 380 | l = l.wrapping_add((old_exc[base] as i32).wrapping_mul(20972)); |
| 381 | code[i] = amr_wb_round(l.wrapping_shl(1)); |
| 382 | } |
| 383 | old_exc[eo + i_subfr..eo + i_subfr + L_SUBFR].copy_from_slice(&code); |
| 384 | } |
| 385 | |
| 386 | // innovative codebook |
| 387 | if unusable_frame != 0 { |
| 388 | for c in code.iter_mut() { |
| 389 | *c = noise_gen_amrwb(&mut st.seed) >> 3; |
| 390 | } |
| 391 | } else if nb_bits <= NBBITS_7K { |
| 392 | ind[0] = Serial_parm(12, &mut prms); |
| 393 | dec_acelp_2p_in_64(ind[0], &mut code); |
| 394 | } else if nb_bits <= NBBITS_9K { |
| 395 | for i in 0..4 { |
| 396 | ind[i] = Serial_parm(5, &mut prms); |
| 397 | } |
| 398 | dec_acelp_4p_in_64(&ind, 20, &mut code); |
| 399 | } else if nb_bits <= NBBITS_12K { |
| 400 | for i in 0..4 { |
| 401 | ind[i] = Serial_parm(9, &mut prms); |
| 402 | } |
| 403 | dec_acelp_4p_in_64(&ind, 36, &mut code); |
| 404 | } else if nb_bits <= NBBITS_14K { |
| 405 | ind[0] = Serial_parm(13, &mut prms); |
| 406 | ind[1] = Serial_parm(13, &mut prms); |
| 407 | ind[2] = Serial_parm(9, &mut prms); |
| 408 | ind[3] = Serial_parm(9, &mut prms); |
| 409 | dec_acelp_4p_in_64(&ind, 44, &mut code); |
| 410 | } else if nb_bits <= NBBITS_16K { |
| 411 | for i in 0..4 { |
| 412 | ind[i] = Serial_parm(13, &mut prms); |
| 413 | } |
| 414 | dec_acelp_4p_in_64(&ind, 52, &mut code); |
| 415 | } else if nb_bits <= NBBITS_18K { |
| 416 | for i in 0..4 { |
| 417 | ind[i] = Serial_parm(2, &mut prms); |
| 418 | } |
| 419 | for i in 4..8 { |
| 420 | ind[i] = Serial_parm(14, &mut prms); |
| 421 | } |
| 422 | dec_acelp_4p_in_64(&ind, 64, &mut code); |
| 423 | } else if nb_bits <= NBBITS_20K { |
| 424 | ind[0] = Serial_parm(10, &mut prms); |
| 425 | ind[1] = Serial_parm(10, &mut prms); |
| 426 | ind[2] = Serial_parm(2, &mut prms); |
| 427 | ind[3] = Serial_parm(2, &mut prms); |
| 428 | ind[4] = Serial_parm(10, &mut prms); |
| 429 | ind[5] = Serial_parm(10, &mut prms); |
| 430 | ind[6] = Serial_parm(14, &mut prms); |
| 431 | ind[7] = Serial_parm(14, &mut prms); |
| 432 | dec_acelp_4p_in_64(&ind, 72, &mut code); |
| 433 | } else { |
| 434 | for i in 0..8 { |
| 435 | ind[i] = Serial_parm(11, &mut prms); |
| 436 | } |
| 437 | dec_acelp_4p_in_64(&ind, 88, &mut code); |
| 438 | } |
| 439 | |
| 440 | preemph_amrwb_dec(&mut code, st.tilt_code, L_SUBFR as i16); |
| 441 | |
| 442 | let mut tmp = t0; |
| 443 | if t0_frac > 2 { |
| 444 | tmp += 1; |
| 445 | } |
| 446 | pit_shrp(&mut code, tmp, PIT_SHARP, L_SUBFR as i16); |
| 447 | |
| 448 | // gains |
| 449 | let (gidx, gbits) = if nb_bits <= NBBITS_9K { |
| 450 | (Serial_parm(6, &mut prms), 6) |
| 451 | } else { |
| 452 | (Serial_parm(7, &mut prms), 7) |
| 453 | }; |
| 454 | dec_gain2_amr_wb( |
| 455 | gidx, |
| 456 | gbits, |
| 457 | &code, |
| 458 | L_SUBFR as i16, |
| 459 | &mut gain_pit, |
| 460 | &mut l_gain_code, |
| 461 | bfi, |
| 462 | st.prev_bfi, |
| 463 | st.state, |
| 464 | unusable_frame, |
| 465 | st.synth.vad_hist, |
| 466 | &mut st.dec_gain, |
| 467 | ); |
| 468 | // scaling of the excitation |
| 469 | let mut tmp = st.qsubfr[0]; |
| 470 | for i in 1..4 { |
| 471 | if st.qsubfr[i] < tmp { |
| 472 | tmp = st.qsubfr[i]; |
| 473 | } |
| 474 | } |
| 475 | if tmp > Q_MAX { |
| 476 | tmp = Q_MAX; |
| 477 | } |
| 478 | q_new = 0; |
| 479 | let mut l_tmp = l_gain_code; |
| 480 | while l_tmp < 0x0800_0000 && q_new < tmp { |
| 481 | l_tmp = l_tmp.wrapping_shl(1); |
| 482 | q_new += 1; |
| 483 | } |
| 484 | let mut gain_code = amr_wb_round(l_tmp); |
| 485 | scale_signal( |
| 486 | &mut old_exc[i_subfr..], |
| 487 | (OLD_EXC_LEN + L_SUBFR) as i16, |
| 488 | q_new - st.q_old, |
| 489 | ); |
| 490 | st.q_old = q_new; |
| 491 | |
| 492 | if bfi == 0 { |
| 493 | for i in (1..=4).rev() { |
| 494 | st.lag_hist[i] = st.lag_hist[i - 1]; |
| 495 | } |
| 496 | st.lag_hist[0] = t0; |
| 497 | st.old_t0 = t0; |
| 498 | st.old_t0_frac = 0; |
| 499 | } |
| 500 | |
| 501 | // voice factor |
| 502 | for i in (0..L_SUBFR).rev() { |
| 503 | let e = old_exc[eo + i_subfr + i]; |
| 504 | exc2[i] = ((e as i32 + 4 * (e != MAX_16) as i32) >> 3) as i16; |
| 505 | } |
| 506 | |
| 507 | let mut pit_sharp: i16; |
| 508 | if nb_bits <= NBBITS_9K { |
| 509 | pit_sharp = shl_int16(gain_pit, 1); |
| 510 | if pit_sharp > 16384 { |
| 511 | for i in 0..L_SUBFR { |
| 512 | let t = mult_int16(exc2[i], pit_sharp); |
| 513 | let mut l = mul_16by16_to_int32(t, gain_pit); |
| 514 | l >>= 1; |
| 515 | excp[i] = amr_wb_round(l); |
| 516 | } |
| 517 | } |
| 518 | } else { |
| 519 | pit_sharp = 0; |
| 520 | } |
| 521 | |
| 522 | let voice_fac = voice_factor(&exc2, -3, gain_pit, &code, gain_code, L_SUBFR as i16); |
| 523 | st.tilt_code = (voice_fac >> 2) + 8192; |
| 524 | |
| 525 | // total excitation |
| 526 | exc2[..L_SUBFR].copy_from_slice(&old_exc[eo + i_subfr..eo + i_subfr + L_SUBFR]); |
| 527 | let mut max: i32 = 1; |
| 528 | for i in 0..L_SUBFR { |
| 529 | let mut l = mul_16by16_to_int32(code[i], gain_code); |
| 530 | l = shl_int32(l, 5); |
| 531 | l = mac_16by16_to_int32(l, old_exc[eo + i_subfr + i], gain_pit); |
| 532 | l = shl_int32(l, 1); |
| 533 | let mut t = amr_wb_round(l); |
| 534 | old_exc[eo + i_subfr + i] = t; |
| 535 | t = (t as i32 - (t < 0) as i32) as i16; |
| 536 | max |= (t ^ (t >> 15)) as i32; |
| 537 | } |
| 538 | let t = ((add_int16(normalize_amr_wb(max) - 16, q_new)) as i32 - 1) as i16; |
| 539 | st.qsubfr[3] = st.qsubfr[2]; |
| 540 | st.qsubfr[2] = st.qsubfr[1]; |
| 541 | st.qsubfr[1] = st.qsubfr[0]; |
| 542 | st.qsubfr[0] = t; |
| 543 | |
| 544 | // phase dispersion |
| 545 | let j: i16 = if nb_bits <= NBBITS_7K { |
| 546 | 0 |
| 547 | } else if nb_bits <= NBBITS_9K { |
| 548 | 1 |
| 549 | } else { |
| 550 | 2 |
| 551 | }; |
| 552 | phase_dispersion( |
| 553 | (l_gain_code >> 16) as i16, |
| 554 | gain_pit, |
| 555 | &mut code, |
| 556 | j, |
| 557 | &mut st.disp_mem, |
| 558 | &mut scratch, |
| 559 | ); |
| 560 | |
| 561 | // noise enhancer |
| 562 | let t = 16384 - (voice_fac >> 1); |
| 563 | let fac = mult_int16(stab_fac, t); |
| 564 | let mut l_tmp = l_gain_code; |
| 565 | if l_tmp < st.l_gc_thres { |
| 566 | l_tmp = l_tmp.wrapping_add(fxp_mul32_by_16b(l_gain_code, 6226).wrapping_shl(1)); |
| 567 | if l_tmp > st.l_gc_thres { |
| 568 | l_tmp = st.l_gc_thres; |
| 569 | } |
| 570 | } else { |
| 571 | l_tmp = fxp_mul32_by_16b(l_gain_code, 27536).wrapping_shl(1); |
| 572 | if l_tmp < st.l_gc_thres { |
| 573 | l_tmp = st.l_gc_thres; |
| 574 | } |
| 575 | } |
| 576 | st.l_gc_thres = l_tmp; |
| 577 | l_gain_code = fxp_mul32_by_16b(l_gain_code, 32767 - fac as i32).wrapping_shl(1); |
| 578 | l_gain_code = add_int32( |
| 579 | l_gain_code, |
| 580 | fxp_mul32_by_16b(l_tmp, fac as i32).wrapping_shl(1), |
| 581 | ); |
| 582 | |
| 583 | // pitch enhancer |
| 584 | let tmp = (voice_fac >> 3) + 4096; |
| 585 | gain_code = amr_wb_round(shl_int32(l_gain_code, q_new)); |
| 586 | |
| 587 | let enh = |code: &[i16], i: usize, nxt: i16, prv: i16, e2: i16, gp: i16, gc: i16| -> i16 { |
| 588 | let _ = i; |
| 589 | let mut l = (code[i] as i32).wrapping_shl(16); |
| 590 | l = msu_16by16_from_int32(l, nxt.wrapping_add(prv), tmp); |
| 591 | l = mul_16by16_to_int32(amr_wb_round(l), gc); |
| 592 | l = shl_int32(l, 5); |
| 593 | l = mac_16by16_to_int32(l, e2, gp); |
| 594 | l = shl_int32(l, 1); |
| 595 | amr_wb_round(l) |
| 596 | }; |
| 597 | exc2[0] = enh(&code, 0, code[1], 0, exc2[0], gain_pit, gain_code); |
| 598 | for i in 1..L_SUBFR - 1 { |
| 599 | exc2[i] = enh( |
| 600 | &code, |
| 601 | i, |
| 602 | code[i + 1], |
| 603 | code[i - 1], |
| 604 | exc2[i], |
| 605 | gain_pit, |
| 606 | gain_code, |
| 607 | ); |
| 608 | } |
| 609 | exc2[L_SUBFR - 1] = enh( |
| 610 | &code, |
| 611 | L_SUBFR - 1, |
| 612 | 0, |
| 613 | code[L_SUBFR - 2], |
| 614 | exc2[L_SUBFR - 1], |
| 615 | gain_pit, |
| 616 | gain_code, |
| 617 | ); |
| 618 | |
| 619 | if nb_bits <= NBBITS_9K && pit_sharp > 16384 { |
| 620 | for i in 0..L_SUBFR { |
| 621 | excp[i] = add_int16(excp[i], exc2[i]); |
| 622 | } |
| 623 | agc2_amr_wb(&exc2[..L_SUBFR], &mut excp, L_SUBFR as i16); |
| 624 | exc2[..L_SUBFR].copy_from_slice(&excp); |
| 625 | } |
| 626 | let _ = &mut pit_sharp; |
| 627 | |
| 628 | if nb_bits <= NBBITS_7K { |
| 629 | let j = i_subfr >> 6; |
| 630 | for i in 0..M { |
| 631 | let mut l = mul_16by16_to_int32(isf_tmp[i], sub_int16(32767, INTERPOL_FRAC[j])); |
| 632 | l = mac_16by16_to_int32(l, isf[i], INTERPOL_FRAC[j]); |
| 633 | hf_isf[i] = amr_wb_round(l); |
| 634 | } |
| 635 | } else { |
| 636 | for v in st.synth.mem_syn_hf[..M16K - M].iter_mut() { |
| 637 | *v = 0; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | let corr_gain = if nb_bits >= NBBITS_24K { |
| 642 | Serial_parm(4, &mut prms) |
| 643 | } else { |
| 644 | 0 |
| 645 | }; |
| 646 | |
| 647 | let out = i_subfr + (i_subfr >> 2); |
| 648 | synthesis_amr_wb( |
| 649 | &aq[p_aq..p_aq + M + 1], |
| 650 | &mut exc2[..L_SUBFR], |
| 651 | q_new, |
| 652 | &mut synth16k[out..out + L_SUBFR16K], |
| 653 | corr_gain, |
| 654 | &mut hf_isf, |
| 655 | nb_bits, |
| 656 | new_dtx_state, |
| 657 | &mut st.synth, |
| 658 | bfi, |
| 659 | ); |
| 660 | p_aq += M + 1; |
| 661 | i_subfr += L_SUBFR; |
| 662 | } |
| 663 | |
| 664 | st.old_exc |
| 665 | .copy_from_slice(&old_exc[L_FRAME..L_FRAME + OLD_EXC_LEN]); |
| 666 | scale_signal(&mut old_exc[eo..], L_FRAME as i16, -q_new); |
| 667 | dtx_dec_amr_wb_activity_update(&mut st.dtx, &isf, &old_exc[eo..eo + L_FRAME]); |
| 668 | st.dtx.dtxGlobalState = new_dtx_state; |
| 669 | st.prev_bfi = bfi; |
| 670 | } |