//! `silk/float/find_pred_coefs_FLP.c` and what it calls, `process_gains_FLP.c`, `silk/gain_quant.c`. use super::nlsf::{a2nlsf_flp, interpolate, nlsf2a_flp, process_nlsfs_flp}; use super::sigproc::{energy, inner_product, lpc_analysis_filter}; use super::*; use crate::silk_tables::{ LTP_GAIN_VQ_0, LTP_GAIN_VQ_1, LTP_GAIN_VQ_2, LTPSCALES_TABLE_Q14, QUANTIZATION_OFFSETS_Q10, }; use crate::silk_fix::{add_pos_sat32, lin2log, log2lin, smlawb, smulbb, smulwb}; const SILK_MAX_ORDER_LPC: usize = 24; const FIND_LPC_COND_FAC: f32 = 1e-5; const LTP_CORR_INV_MAX: f32 = 0.03; const MAX_SUM_LOG_GAIN_DB: f32 = 250.0; const MAX_PREDICTION_POWER_GAIN: f32 = 1e4; const MAX_PREDICTION_POWER_GAIN_AFTER_RESET: f32 = 1e2; const LAMBDA_OFFSET: f32 = 1.2; const LAMBDA_SPEECH_ACT: f32 = -0.2; const LAMBDA_DELAYED_DECISIONS: f32 = -0.05; const LAMBDA_INPUT_QUALITY: f32 = -0.1; const LAMBDA_CODING_QUALITY: f32 = -0.2; const LAMBDA_QUANT_OFFSET: f32 = 0.8; const OFFSET: i32 = (MIN_QGAIN_DB * 128) / 6 + 16 * 128; const SCALE_Q16: i32 = (65536 * (N_LEVELS_QGAIN - 1)) / (((MAX_QGAIN_DB - MIN_QGAIN_DB) * 128) / 6); const INV_SCALE_Q16: i32 = (65536 * (((MAX_QGAIN_DB - MIN_QGAIN_DB) * 128) / 6)) / (N_LEVELS_QGAIN - 1); const LTP_GAIN_BITS_Q5_0: [u8; 8] = [15, 131, 138, 138, 155, 155, 173, 173]; const LTP_GAIN_BITS_Q5_1: [u8; 16] = [ 69, 93, 115, 118, 131, 138, 141, 138, 150, 150, 155, 150, 155, 160, 166, 160, ]; const LTP_GAIN_BITS_Q5_2: [u8; 32] = [ 131, 128, 134, 141, 141, 141, 145, 145, 145, 150, 155, 155, 155, 155, 160, 160, 160, 160, 166, 166, 173, 173, 182, 192, 182, 192, 192, 192, 205, 192, 205, 224, ]; const LTP_GAIN_BITS_Q5: [&[u8]; NB_LTP_CBKS] = [ <P_GAIN_BITS_Q5_0, <P_GAIN_BITS_Q5_1, <P_GAIN_BITS_Q5_2, ]; const LTP_VQ_GAIN_Q7_0: [u8; 8] = [46, 2, 90, 87, 93, 91, 82, 98]; const LTP_VQ_GAIN_Q7_1: [u8; 16] = [ 109, 120, 118, 12, 113, 115, 117, 119, 99, 59, 87, 111, 63, 111, 112, 80, ]; const LTP_VQ_GAIN_Q7_2: [u8; 32] = [ 126, 124, 125, 124, 129, 121, 126, 23, 132, 127, 127, 127, 126, 127, 122, 133, 130, 134, 101, 118, 119, 145, 126, 86, 124, 120, 123, 119, 170, 173, 107, 109, ]; const LTP_VQ_GAIN_Q7: [&[u8]; NB_LTP_CBKS] = [<P_VQ_GAIN_Q7_0, <P_VQ_GAIN_Q7_1, <P_VQ_GAIN_Q7_2]; const fn silk_fix_const(c: f64, q: u32) -> i32 { (c * (1i64 << q) as f64 + 0.5) as i32 } const LTP_GAIN_SAFETY_Q7: i32 = silk_fix_const(0.4, 7); const MAX_SUM_LOG_GAIN_Q7: i32 = silk_fix_const(MAX_SUM_LOG_GAIN_DB as f64 / 6.0, 7); const ONE_Q7: i32 = silk_fix_const(7.0, 7); const LTP_RATE_PENALTY_OFFSET: i32 = 15 << 7; const QUANT_ERR_INIT_Q15: i32 = silk_fix_const(1.001, 15); /// `silk_float2int`: round to nearest, ties to even. fn float2int(x: f32) -> i32 { (x as f64).round_ties_even() as i32 } fn limit(a: i32, lo: i32, hi: i32) -> i32 { if a > hi { hi } else if a < lo { lo } else { a } } fn sigmoid(x: f32) -> f32 { (1.0 / (1.0 + (-x as f64).exp())) as f32 } /// `silk_burg_modified_FLP`. fn burg_modified( a: &mut [f32], x: &[f32], min_inv_gain: f32, subfr_length: usize, nb_subfr: usize, d: usize, ) -> f32 { let mut c_first_row = [0f64; SILK_MAX_ORDER_LPC]; let mut c_last_row; let mut caf = [0f64; SILK_MAX_ORDER_LPC + 1]; let mut cab = [0f64; SILK_MAX_ORDER_LPC + 1]; let mut af = [0f64; SILK_MAX_ORDER_LPC]; let mut c0 = energy(&x[..nb_subfr * subfr_length]); for s in 0..nb_subfr { let xp = s * subfr_length; for n in 1..d + 1 { c_first_row[n - 1] += inner_product( &x[xp..xp + subfr_length - n], &x[xp + n..xp + subfr_length], subfr_length - n, ); } } c_last_row = c_first_row; caf[0] = c0 + (FIND_LPC_COND_FAC as f64) * c0 + 1e-1; cab[0] = caf[0]; let mut inv_gain = 1.0f64; let mut reached_max_gain = false; for n in 0..d { for s in 0..nb_subfr { let xp = s * subfr_length; let mut tmp1 = x[xp + n] as f64; let mut tmp2 = x[xp + subfr_length - n - 1] as f64; for k in 0..n { c_first_row[k] -= (x[xp + n] * x[xp + n - k - 1]) as f64; c_last_row[k] -= (x[xp + subfr_length - n - 1] * x[xp + subfr_length - n + k]) as f64; let atmp = af[k]; tmp1 += x[xp + n - k - 1] as f64 * atmp; tmp2 += x[xp + subfr_length - n + k] as f64 * atmp; } for k in 0..=n { caf[k] -= tmp1 * x[xp + n - k] as f64; cab[k] -= tmp2 * x[xp + subfr_length - n + k - 1] as f64; } } let mut tmp1 = c_first_row[n]; let mut tmp2 = c_last_row[n]; for k in 0..n { let atmp = af[k]; tmp1 += c_last_row[n - k - 1] * atmp; tmp2 += c_first_row[n - k - 1] * atmp; } caf[n + 1] = tmp1; cab[n + 1] = tmp2; let mut num = cab[n + 1]; let mut nrg_b = cab[0]; let mut nrg_f = caf[0]; for k in 0..n { let atmp = af[k]; num += cab[n - k] * atmp; nrg_b += cab[k + 1] * atmp; nrg_f += caf[k + 1] * atmp; } let mut rc = -2.0 * num / (nrg_f + nrg_b); let tmp1 = inv_gain * (1.0 - rc * rc); if tmp1 <= min_inv_gain as f64 { rc = (1.0 - min_inv_gain as f64 / inv_gain).sqrt(); if num > 0.0 { rc = -rc; } inv_gain = min_inv_gain as f64; reached_max_gain = true; } else { inv_gain = tmp1; } for k in 0..(n + 1) >> 1 { let t1 = af[k]; let t2 = af[n - k - 1]; af[k] = t1 + rc * t2; af[n - k - 1] = t2 + rc * t1; } af[n] = rc; if reached_max_gain { for v in af.iter_mut().take(d).skip(n + 1) { *v = 0.0; } break; } for k in 0..=n + 1 { let t1 = caf[k]; caf[k] += rc * cab[n + 1 - k]; cab[n + 1 - k] += rc * t1; } } let nrg_f = if reached_max_gain { for k in 0..d { a[k] = (-af[k]) as f32; } for s in 0..nb_subfr { c0 -= energy(&x[s * subfr_length..s * subfr_length + d]); } c0 * inv_gain } else { let mut nrg_f = caf[0]; let mut tmp1 = 1.0f64; for k in 0..d { let atmp = af[k]; nrg_f += caf[k + 1] * atmp; tmp1 += atmp * atmp; a[k] = (-atmp) as f32; } nrg_f - (FIND_LPC_COND_FAC as f64) * c0 * tmp1 }; nrg_f as f32 } /// `silk_corrVector_FLP`. fn corr_vector(x: &[f32], t: &[f32], l: usize, order: usize, xt: &mut [f32]) { let p1 = order - 1; for lag in 0..order { xt[lag] = inner_product(&x[p1 - lag..p1 - lag + l], &t[..l], l) as f32; } } /// `silk_corrMatrix_FLP`. fn corr_matrix(x: &[f32], l: usize, order: usize, xx: &mut [f32]) { let p1 = order - 1; let mut e = energy(&x[p1..p1 + l]); xx[0] = e as f32; for j in 1..order { e += (x[p1 - j] * x[p1 - j] - x[p1 + l - j] * x[p1 + l - j]) as f64; xx[j * order + j] = e as f32; } for lag in 1..order { let p2 = order - 1 - lag; let mut e2 = inner_product(&x[p1..p1 + l], &x[p2..p2 + l], l); xx[lag * order] = e2 as f32; xx[lag] = e2 as f32; for j in 1..order - lag { e2 += (x[p1 - j] * x[p2 - j] - x[p1 + l - j] * x[p2 + l - j]) as f64; xx[(lag + j) * order + j] = e2 as f32; xx[j * order + lag + j] = e2 as f32; } } } /// `silk_find_LTP_FLP`. `r` is the whole pitch residual buffer, the C /// `r_ptr` sits at `r_base`. fn find_ltp( xx: &mut [f32], xx_vec: &mut [f32], r: &[f32], r_base: usize, lag: &[i32], subfr_length: usize, nb_subfr: usize, ) { let mut r_ptr = r_base; for k in 0..nb_subfr { let lag_ptr = r_ptr - (lag[k] as usize + LTP_ORDER / 2); let xx_k = &mut xx[k * LTP_ORDER * LTP_ORDER..(k + 1) * LTP_ORDER * LTP_ORDER]; corr_matrix(&r[lag_ptr..], subfr_length, LTP_ORDER, xx_k); corr_vector( &r[lag_ptr..], &r[r_ptr..], subfr_length, LTP_ORDER, &mut xx_vec[k * LTP_ORDER..(k + 1) * LTP_ORDER], ); let xx_e = energy(&r[r_ptr..r_ptr + subfr_length + LTP_ORDER]) as f32; let thr = LTP_CORR_INV_MAX * 0.5 * (xx_k[0] + xx_k[24]) + 1.0; let m = if xx_e > thr { xx_e } else { thr }; let temp = 1.0 / m; for v in xx_k.iter_mut() { *v *= temp; } for v in xx_vec[k * LTP_ORDER..(k + 1) * LTP_ORDER].iter_mut() { *v *= temp; } r_ptr += subfr_length; } } /// `silk_regularize_correlations_FLP`. #[allow(dead_code)] fn regularize_correlations(xx: &mut [f32], xx_vec: &mut [f32], noise: f32, d: usize) { for i in 0..d { xx[i * d + i] += noise; } xx_vec[0] += noise; } /// `silk_residual_energy_covar_FLP`. #[allow(dead_code)] fn residual_energy_covar( c: &[f32], wxx: &mut [f32], wxx_vec: &[f32], wxx_scalar: f32, d: usize, ) -> f32 { const MAX_ITERATIONS_RESIDUAL_NRG: usize = 10; const REGULARIZATION_FACTOR: f32 = 1e-8; let mut nrg = 0.0f32; let mut regularization = REGULARIZATION_FACTOR * (wxx[0] + wxx[d * d - 1]); let mut k = 0; while k < MAX_ITERATIONS_RESIDUAL_NRG { nrg = wxx_scalar; let mut tmp = 0.0f32; for i in 0..d { tmp += wxx_vec[i] * c[i]; } nrg -= 2.0 * tmp; for i in 0..d { let mut tmp = 0.0f32; for j in i + 1..d { tmp += wxx[i + j * d] * c[j]; } nrg += c[i] * (2.0 * tmp + wxx[i + i * d] * c[i]); } if nrg > 0.0 { break; } for i in 0..d { wxx[i + i * d] += regularization; } regularization *= 2.0; k += 1; } if k == MAX_ITERATIONS_RESIDUAL_NRG { nrg = 1.0; } nrg } /// `silk_residual_energy_FLP`. fn residual_energy( nrgs: &mut [f32; MAX_NB_SUBFR], x: &[f32], a: &[[f32; MAX_LPC_ORDER]; 2], gains: &[f32], subfr_length: usize, nb_subfr: usize, lpc_order: usize, ) { let mut lpc_res = [0f32; (MAX_FRAME_LENGTH + MAX_NB_SUBFR * MAX_LPC_ORDER) / 2]; let shift = lpc_order + subfr_length; lpc_analysis_filter(&mut lpc_res, &a[0], &x[..2 * shift], 2 * shift, lpc_order); nrgs[0] = ((gains[0] * gains[0]) as f64 * energy(&lpc_res[lpc_order..lpc_order + subfr_length])) as f32; nrgs[1] = ((gains[1] * gains[1]) as f64 * energy(&lpc_res[lpc_order + shift..lpc_order + shift + subfr_length])) as f32; if nb_subfr == MAX_NB_SUBFR { lpc_analysis_filter( &mut lpc_res, &a[1], &x[2 * shift..4 * shift], 2 * shift, lpc_order, ); nrgs[2] = ((gains[2] * gains[2]) as f64 * energy(&lpc_res[lpc_order..lpc_order + subfr_length])) as f32; nrgs[3] = ((gains[3] * gains[3]) as f64 * energy(&lpc_res[lpc_order + shift..lpc_order + shift + subfr_length])) as f32; } } /// `silk_LTP_analysis_filter_FLP`. `x` is the whole signal buffer, the C /// `x` pointer sits at `x_base`. fn ltp_analysis_filter( ltp_res: &mut [f32], x: &[f32], x_base: usize, b: &[f32], pitch_l: &[i32], inv_gains: &[f32], subfr_length: usize, nb_subfr: usize, pre_length: usize, ) { let mut x_ptr = x_base; let mut res_ptr = 0; for k in 0..nb_subfr { let lag = pitch_l[k] as usize; let inv_gain = inv_gains[k]; let mut btmp = [0f32; LTP_ORDER]; btmp.copy_from_slice(&b[k * LTP_ORDER..(k + 1) * LTP_ORDER]); for i in 0..subfr_length + pre_length { let lag_idx = x_ptr + i + LTP_ORDER / 2 - lag; let mut v = x[x_ptr + i]; for j in 0..LTP_ORDER { v -= btmp[j] * x[lag_idx - j]; } ltp_res[res_ptr + i] = v * inv_gain; } res_ptr += subfr_length + pre_length; x_ptr += subfr_length; } } /// `silk_LTP_scale_ctrl_FLP`. fn ltp_scale_ctrl(st: &mut EncState, ctrl: &mut EncCtrl, cond_coding: CondCoding) { if cond_coding == CondCoding::Independently { let mut round_loss = st.packet_loss_perc * st.n_frames_per_packet as i32; if st.lbrr_flag { round_loss = 2 + smulbb(round_loss, round_loss) / 100; } let v = smulbb(ctrl.lt_pred_cod_gain as i32, round_loss); let snr = st.snr_db_q7; st.indices.ltp_scale_index = (v > log2lin(2900 - snr)) as i8 + (v > log2lin(3900 - snr)) as i8; } else { st.indices.ltp_scale_index = 0; } ctrl.ltp_scale = LTPSCALES_TABLE_Q14[st.indices.ltp_scale_index as usize] as f32 / 16384.0; } /// `silk_VQ_WMat_EC_c`. Returns `(ind, res_nrg_Q15, rate_dist_Q8)`; `gain_q7` /// is only written when a candidate is accepted, as in C. fn vq_wmat_ec( gain_q7: &mut i32, xx: &[i32], xx_vec: &[i32], cb: &[[i8; LTP_ORDER]], cb_gain: &[u8], cl: &[u8], subfr_len: i32, max_gain_q7: i32, ) -> (i8, i32, i32) { let mut neg = [0i32; LTP_ORDER]; for i in 0..LTP_ORDER { neg[i] = (xx_vec[i] << 7).wrapping_neg(); } let mut rate_dist = i32::MAX; let mut res_nrg = i32::MAX; let mut ind = 0i8; let mla = |a: i32, b: i32, c: i32| a.wrapping_add(b.wrapping_mul(c)); for (k, row) in cb.iter().enumerate() { let c = row.map(|v| v as i32); let gain_tmp = cb_gain[k] as i32; let mut sum1: i32 = QUANT_ERR_INIT_Q15; let penalty = gain_tmp.wrapping_sub(max_gain_q7).max(0) << 11; let mut sum2 = mla(neg[0], xx[1], c[1]); sum2 = mla(sum2, xx[2], c[2]); sum2 = mla(sum2, xx[3], c[3]); sum2 = mla(sum2, xx[4], c[4]); sum2 <<= 1; sum2 = mla(sum2, xx[0], c[0]); sum1 = smlawb(sum1, sum2, c[0]); let mut sum2 = mla(neg[1], xx[7], c[2]); sum2 = mla(sum2, xx[8], c[3]); sum2 = mla(sum2, xx[9], c[4]); sum2 <<= 1; sum2 = mla(sum2, xx[6], c[1]); sum1 = smlawb(sum1, sum2, c[1]); let mut sum2 = mla(neg[2], xx[13], c[3]); sum2 = mla(sum2, xx[14], c[4]); sum2 <<= 1; sum2 = mla(sum2, xx[12], c[2]); sum1 = smlawb(sum1, sum2, c[2]); let mut sum2 = mla(neg[3], xx[19], c[4]); sum2 <<= 1; sum2 = mla(sum2, xx[18], c[3]); sum1 = smlawb(sum1, sum2, c[3]); let mut sum2 = neg[4] << 1; sum2 = mla(sum2, xx[24], c[4]); sum1 = smlawb(sum1, sum2, c[4]); if sum1 >= 0 { let bits_res = smulbb( subfr_len, lin2log(sum1.wrapping_add(penalty)) - LTP_RATE_PENALTY_OFFSET, ); let bits_tot = bits_res.wrapping_add((cl[k] as i32) << 2); if bits_tot <= rate_dist { rate_dist = bits_tot; res_nrg = sum1.wrapping_add(penalty); ind = k as i8; *gain_q7 = gain_tmp; } } } (ind, res_nrg, rate_dist) } /// `silk_quant_LTP_gains`. fn quant_ltp_gains( b_q14: &mut [i16; MAX_NB_SUBFR * LTP_ORDER], cbk_index: &mut [i8; MAX_NB_SUBFR], periodicity_index: &mut i8, sum_log_gain_q7: &mut i32, pred_gain_db_q7: &mut i32, xx_q17: &[i32; MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER], xx_vec_q17: &[i32; MAX_NB_SUBFR * LTP_ORDER], subfr_len: usize, nb_subfr: usize, ) { let mut min_rate_dist = i32::MAX; let mut best_sum_log_gain = 0i32; let mut res_nrg_q15 = 0i32; let mut gain_q7 = 0i32; for k in 0..3 { let cl = LTP_GAIN_BITS_Q5[k]; let cbk: &[[i8; LTP_ORDER]] = match k { 0 => <P_GAIN_VQ_0, 1 => <P_GAIN_VQ_1, _ => <P_GAIN_VQ_2, }; let cbk_gain = LTP_VQ_GAIN_Q7[k]; let mut temp_idx = [0i8; MAX_NB_SUBFR]; res_nrg_q15 = 0; let mut rate_dist_q7 = 0i32; let mut sum_log_gain_tmp = *sum_log_gain_q7; for j in 0..nb_subfr { let max_gain_q7 = log2lin((MAX_SUM_LOG_GAIN_Q7 - sum_log_gain_tmp) + ONE_Q7) - LTP_GAIN_SAFETY_Q7; let (ind, res_sub, rate_sub) = vq_wmat_ec( &mut gain_q7, &xx_q17[j * LTP_ORDER * LTP_ORDER..(j + 1) * LTP_ORDER * LTP_ORDER], &xx_vec_q17[j * LTP_ORDER..(j + 1) * LTP_ORDER], cbk, cbk_gain, cl, subfr_len as i32, max_gain_q7, ); temp_idx[j] = ind; res_nrg_q15 = add_pos_sat32(res_nrg_q15, res_sub); rate_dist_q7 = add_pos_sat32(rate_dist_q7, rate_sub); sum_log_gain_tmp = 0.max(sum_log_gain_tmp + lin2log(LTP_GAIN_SAFETY_Q7 + gain_q7) - ONE_Q7); } if rate_dist_q7 <= min_rate_dist { min_rate_dist = rate_dist_q7; *periodicity_index = k as i8; cbk_index[..nb_subfr].copy_from_slice(&temp_idx[..nb_subfr]); best_sum_log_gain = sum_log_gain_tmp; } } let cbk: &[[i8; LTP_ORDER]] = match *periodicity_index { 0 => <P_GAIN_VQ_0, 1 => <P_GAIN_VQ_1, _ => <P_GAIN_VQ_2, }; for j in 0..nb_subfr { for k in 0..LTP_ORDER { b_q14[j * LTP_ORDER + k] = ((cbk[cbk_index[j] as usize][k] as i32) << 7) as i16; } } if nb_subfr == 2 { res_nrg_q15 >>= 1; } else { res_nrg_q15 >>= 2; } *sum_log_gain_q7 = best_sum_log_gain; *pred_gain_db_q7 = smulbb(-3, lin2log(res_nrg_q15) - LTP_RATE_PENALTY_OFFSET); } /// `silk_quant_LTP_gains_FLP`. fn quant_ltp_gains_flp( b: &mut [f32], cbk_index: &mut [i8; MAX_NB_SUBFR], periodicity_index: &mut i8, sum_log_gain_q7: &mut i32, pred_gain_db: &mut f32, xx: &[f32], xx_vec: &[f32], subfr_len: usize, nb_subfr: usize, ) { let mut xx_q17 = [0i32; MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER]; for i in 0..nb_subfr * LTP_ORDER * LTP_ORDER { xx_q17[i] = float2int(xx[i] * 131072.0); } let mut xx_vec_q17 = [0i32; MAX_NB_SUBFR * LTP_ORDER]; for i in 0..nb_subfr * LTP_ORDER { xx_vec_q17[i] = float2int(xx_vec[i] * 131072.0); } let mut b_q14 = [0i16; MAX_NB_SUBFR * LTP_ORDER]; let mut pred_gain_db_q7 = 0i32; quant_ltp_gains( &mut b_q14, cbk_index, periodicity_index, sum_log_gain_q7, &mut pred_gain_db_q7, &xx_q17, &xx_vec_q17, subfr_len, nb_subfr, ); for i in 0..nb_subfr * LTP_ORDER { b[i] = b_q14[i] as f32 * (1.0 / 16384.0); } *pred_gain_db = pred_gain_db_q7 as f32 * (1.0 / 128.0); } /// `silk_find_LPC_FLP`. fn find_lpc(st: &mut EncState, nlsf_q15: &mut [i16; MAX_LPC_ORDER], x: &[f32], min_inv_gain: f32) { let order = st.predict_lpc_order; let sub = st.subfr_length + order; st.indices.nlsf_interp_coef_q2 = 4; let mut a = [0f32; MAX_LPC_ORDER]; let mut res_nrg = burg_modified(&mut a, x, min_inv_gain, sub, st.nb_subfr, order); if st.use_interpolated_nlsfs && !st.first_frame_after_reset && st.nb_subfr == MAX_NB_SUBFR { let mut a_tmp = [0f32; MAX_LPC_ORDER]; res_nrg -= burg_modified( &mut a_tmp, &x[(MAX_NB_SUBFR / 2) * sub..], min_inv_gain, sub, MAX_NB_SUBFR / 2, order, ); a2nlsf_flp(&mut nlsf_q15[..], &a_tmp[..], order); let mut res_nrg_2nd = f32::MAX; for k in (0..4i32).rev() { let mut nlsf0 = [0i16; MAX_LPC_ORDER]; interpolate( &mut nlsf0[..], &st.prev_nlsfq_q15[..], &nlsf_q15[..], k, order, ); nlsf2a_flp(&mut a_tmp[..], &nlsf0[..], order); let mut lpc_res = [0f32; MAX_FRAME_LENGTH + MAX_NB_SUBFR * MAX_LPC_ORDER]; lpc_analysis_filter(&mut lpc_res, &a_tmp, x, 2 * sub, order); let res_nrg_interp = (energy(&lpc_res[order..sub]) + energy(&lpc_res[order + sub..2 * sub])) as f32; if res_nrg_interp < res_nrg { res_nrg = res_nrg_interp; st.indices.nlsf_interp_coef_q2 = k as i8; } else if res_nrg_interp > res_nrg_2nd { break; } res_nrg_2nd = res_nrg_interp; } } if st.indices.nlsf_interp_coef_q2 == 4 { a2nlsf_flp(&mut nlsf_q15[..], &a[..], order); } } /// `silk_find_pred_coefs_FLP`. pub(crate) fn find_pred_coefs( st: &mut EncState, ctrl: &mut EncCtrl, res_pitch: &[f32], cond_coding: CondCoding, ) { let nb = st.nb_subfr; let sub = st.subfr_length; let order = st.predict_lpc_order; let x_buf = st.x_buf; let x_frame = st.ltp_mem_length; let mut inv_gains = [0f32; MAX_NB_SUBFR]; for i in 0..nb { inv_gains[i] = 1.0 / ctrl.gains[i]; } let mut lpc_in_pre = [0f32; MAX_NB_SUBFR * MAX_LPC_ORDER + MAX_FRAME_LENGTH]; if st.indices.signal_type == TYPE_VOICED { let mut xx = [0f32; MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER]; let mut xx_vec = [0f32; MAX_NB_SUBFR * LTP_ORDER]; find_ltp( &mut xx, &mut xx_vec, res_pitch, x_frame, &ctrl.pitch_l, sub, nb, ); quant_ltp_gains_flp( &mut ctrl.ltp_coef, &mut st.indices.ltp_index, &mut st.indices.per_index, &mut st.sum_log_gain_q7, &mut ctrl.lt_pred_cod_gain, &xx, &xx_vec, sub, nb, ); ltp_scale_ctrl(st, ctrl, cond_coding); ltp_analysis_filter( &mut lpc_in_pre, &x_buf, x_frame - order, &ctrl.ltp_coef, &ctrl.pitch_l, &inv_gains, sub, nb, order, ); } else { let mut x_ptr = x_frame - order; let mut pre_ptr = 0; for i in 0..nb { for j in 0..sub + order { lpc_in_pre[pre_ptr + j] = inv_gains[i] * x_buf[x_ptr + j]; } pre_ptr += sub + order; x_ptr += sub; } ctrl.ltp_coef[..nb * LTP_ORDER].fill(0.0); ctrl.lt_pred_cod_gain = 0.0; st.sum_log_gain_q7 = 0; } let min_inv_gain = if st.first_frame_after_reset { 1.0 / MAX_PREDICTION_POWER_GAIN_AFTER_RESET } else { let p = (2f64).powf((ctrl.lt_pred_cod_gain / 3.0) as f64) as f32; let mut m = p / MAX_PREDICTION_POWER_GAIN; m /= 0.25 + 0.75 * ctrl.coding_quality; m }; let mut nlsf_q15 = [0i16; MAX_LPC_ORDER]; find_lpc(st, &mut nlsf_q15, &lpc_in_pre, min_inv_gain); let prev = st.prev_nlsfq_q15; process_nlsfs_flp(st, &mut ctrl.pred_coef, &mut nlsf_q15, &prev); residual_energy( &mut ctrl.res_nrg, &lpc_in_pre, &ctrl.pred_coef, &ctrl.gains, sub, nb, order, ); st.prev_nlsfq_q15 = nlsf_q15; } /// `silk_process_gains_FLP`. pub(crate) fn process_gains(st: &mut EncState, ctrl: &mut EncCtrl, cond_coding: CondCoding) { let nb = st.nb_subfr; if st.indices.signal_type == TYPE_VOICED { let s = 1.0f32 - 0.5 * sigmoid(0.25 * (ctrl.lt_pred_cod_gain - 12.0)); for k in 0..nb { ctrl.gains[k] *= s; } } let snr_term = st.snr_db_q7 as f32 * (1.0 / 128.0); let inv_max_sqr_val = ((2f64).powf((0.33f32 * (21.0 - snr_term)) as f64) / st.subfr_length as f64) as f32; for k in 0..nb { let gain = ctrl.gains[k]; let gain = ((gain * gain + ctrl.res_nrg[k] * inv_max_sqr_val) as f64).sqrt() as f32; ctrl.gains[k] = if gain < 32767.0 { gain } else { 32767.0 }; } let mut p_gains_q16 = [0i32; MAX_NB_SUBFR]; for k in 0..nb { p_gains_q16[k] = (ctrl.gains[k] * 65536.0) as i32; } for k in 0..nb { ctrl.gains_unq_q16[k] = p_gains_q16[k]; } ctrl.last_gain_index_prev = st.s_shape.last_gain_index; gains_quant( &mut st.indices.gains_indices, &mut p_gains_q16, &mut st.s_shape.last_gain_index, cond_coding == CondCoding::Conditionally, nb, ); for k in 0..nb { ctrl.gains[k] = p_gains_q16[k] as f32 / 65536.0; } if st.indices.signal_type == TYPE_VOICED { st.indices.quant_offset_type = if ctrl.lt_pred_cod_gain + st.input_tilt_q15 as f32 * (1.0 / 32768.0) > 1.0 { 0 } else { 1 }; } let quant_offset = QUANTIZATION_OFFSETS_Q10[(st.indices.signal_type >> 1) as usize] [st.indices.quant_offset_type as usize] as f32 / 1024.0; ctrl.lambda = LAMBDA_OFFSET + LAMBDA_DELAYED_DECISIONS * st.n_states_delayed_decision as f32 + LAMBDA_SPEECH_ACT * st.speech_activity_q8 as f32 * (1.0 / 256.0) + LAMBDA_INPUT_QUALITY * ctrl.input_quality + LAMBDA_CODING_QUALITY * ctrl.coding_quality + LAMBDA_QUANT_OFFSET * quant_offset; } /// `silk_gains_quant`. pub(crate) fn gains_quant( ind: &mut [i8; MAX_NB_SUBFR], gain_q16: &mut [i32; MAX_NB_SUBFR], prev_ind: &mut i8, conditional: bool, nb_subfr: usize, ) { let mut prev = *prev_ind as i32; for k in 0..nb_subfr { let mut v = smulwb(SCALE_Q16, lin2log(gain_q16[k]) - OFFSET); if v < prev { v += 1; } v = limit(v, 0, N_LEVELS_QGAIN - 1); if k == 0 && !conditional { v = limit(v, prev + MIN_DELTA_GAIN_QUANT, N_LEVELS_QGAIN - 1); prev = v; } else { v -= prev; let double_step_size_threshold = 2 * MAX_DELTA_GAIN_QUANT - N_LEVELS_QGAIN + prev; if v > double_step_size_threshold { v = double_step_size_threshold + ((v - double_step_size_threshold + 1) >> 1); } v = limit(v, MIN_DELTA_GAIN_QUANT, MAX_DELTA_GAIN_QUANT); if v > double_step_size_threshold { prev += (v << 1) - double_step_size_threshold; prev = prev.min(N_LEVELS_QGAIN - 1); } else { prev += v; } v -= MIN_DELTA_GAIN_QUANT; } ind[k] = v as i8; gain_q16[k] = log2lin(smulwb(INV_SCALE_Q16, prev).wrapping_add(OFFSET).min(3967)); } *prev_ind = prev as i8; } /// `silk_gains_ID`. pub(crate) fn gains_id(ind: &[i8; MAX_NB_SUBFR], nb_subfr: usize) -> i32 { let mut id = 0i32; for k in 0..nb_subfr { id = (ind[k] as i32).wrapping_add(id << 8); } id }