forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

pred.rs872 lines, 26.6 KiBHistoryRawDownload
1//! `silk/float/find_pred_coefs_FLP.c` and what it calls, `process_gains_FLP.c`, `silk/gain_quant.c`.
2
3use super::nlsf::{a2nlsf_flp, interpolate, nlsf2a_flp, process_nlsfs_flp};
4use super::sigproc::{energy, inner_product, lpc_analysis_filter};
5use super::*;
6use crate::silk::tables::{
7 LTP_GAIN_VQ_0, LTP_GAIN_VQ_1, LTP_GAIN_VQ_2, LTPSCALES_TABLE_Q14, QUANTIZATION_OFFSETS_Q10,
8};
9use crate::silk_fix::{add_pos_sat32, lin2log, log2lin, smlawb, smulbb, smulwb};
10
11const SILK_MAX_ORDER_LPC: usize = 24;
12const FIND_LPC_COND_FAC: f32 = 1e-5;
13const LTP_CORR_INV_MAX: f32 = 0.03;
14const MAX_SUM_LOG_GAIN_DB: f32 = 250.0;
15const MAX_PREDICTION_POWER_GAIN: f32 = 1e4;
16const MAX_PREDICTION_POWER_GAIN_AFTER_RESET: f32 = 1e2;
17const LAMBDA_OFFSET: f32 = 1.2;
18const LAMBDA_SPEECH_ACT: f32 = -0.2;
19const LAMBDA_DELAYED_DECISIONS: f32 = -0.05;
20const LAMBDA_INPUT_QUALITY: f32 = -0.1;
21const LAMBDA_CODING_QUALITY: f32 = -0.2;
22const LAMBDA_QUANT_OFFSET: f32 = 0.8;
23
24const OFFSET: i32 = (MIN_QGAIN_DB * 128) / 6 + 16 * 128;
25const SCALE_Q16: i32 = (65536 * (N_LEVELS_QGAIN - 1)) / (((MAX_QGAIN_DB - MIN_QGAIN_DB) * 128) / 6);
26const INV_SCALE_Q16: i32 =
27 (65536 * (((MAX_QGAIN_DB - MIN_QGAIN_DB) * 128) / 6)) / (N_LEVELS_QGAIN - 1);
28
29const LTP_GAIN_BITS_Q5_0: [u8; 8] = [15, 131, 138, 138, 155, 155, 173, 173];
30const LTP_GAIN_BITS_Q5_1: [u8; 16] = [
31 69, 93, 115, 118, 131, 138, 141, 138, 150, 150, 155, 150, 155, 160, 166, 160,
32];
33const LTP_GAIN_BITS_Q5_2: [u8; 32] = [
34 131, 128, 134, 141, 141, 141, 145, 145, 145, 150, 155, 155, 155, 155, 160, 160, 160, 160, 166,
35 166, 173, 173, 182, 192, 182, 192, 192, 192, 205, 192, 205, 224,
36];
37const LTP_GAIN_BITS_Q5: [&[u8]; NB_LTP_CBKS] = [
38 &LTP_GAIN_BITS_Q5_0,
39 &LTP_GAIN_BITS_Q5_1,
40 &LTP_GAIN_BITS_Q5_2,
41];
42
43const LTP_VQ_GAIN_Q7_0: [u8; 8] = [46, 2, 90, 87, 93, 91, 82, 98];
44const LTP_VQ_GAIN_Q7_1: [u8; 16] = [
45 109, 120, 118, 12, 113, 115, 117, 119, 99, 59, 87, 111, 63, 111, 112, 80,
46];
47const LTP_VQ_GAIN_Q7_2: [u8; 32] = [
48 126, 124, 125, 124, 129, 121, 126, 23, 132, 127, 127, 127, 126, 127, 122, 133, 130, 134, 101,
49 118, 119, 145, 126, 86, 124, 120, 123, 119, 170, 173, 107, 109,
50];
51const LTP_VQ_GAIN_Q7: [&[u8]; NB_LTP_CBKS] =
52 [&LTP_VQ_GAIN_Q7_0, &LTP_VQ_GAIN_Q7_1, &LTP_VQ_GAIN_Q7_2];
53
54const fn silk_fix_const(c: f64, q: u32) -> i32 {
55 (c * (1i64 << q) as f64 + 0.5) as i32
56}
57
58const LTP_GAIN_SAFETY_Q7: i32 = silk_fix_const(0.4, 7);
59const MAX_SUM_LOG_GAIN_Q7: i32 = silk_fix_const(MAX_SUM_LOG_GAIN_DB as f64 / 6.0, 7);
60const ONE_Q7: i32 = silk_fix_const(7.0, 7);
61const LTP_RATE_PENALTY_OFFSET: i32 = 15 << 7;
62const QUANT_ERR_INIT_Q15: i32 = silk_fix_const(1.001, 15);
63
64/// `silk_float2int`: round to nearest, ties to even.
65fn float2int(x: f32) -> i32 {
66 (x as f64).round_ties_even() as i32
67}
68
69fn limit(a: i32, lo: i32, hi: i32) -> i32 {
70 if a > hi {
71 hi
72 } else if a < lo {
73 lo
74 } else {
75 a
76 }
77}
78
79fn sigmoid(x: f32) -> f32 {
80 (1.0 / (1.0 + (-x as f64).exp())) as f32
81}
82
83/// `silk_burg_modified_FLP`.
84fn burg_modified(
85 a: &mut [f32],
86 x: &[f32],
87 min_inv_gain: f32,
88 subfr_length: usize,
89 nb_subfr: usize,
90 d: usize,
91) -> f32 {
92 let mut c_first_row = [0f64; SILK_MAX_ORDER_LPC];
93 let mut c_last_row;
94 let mut caf = [0f64; SILK_MAX_ORDER_LPC + 1];
95 let mut cab = [0f64; SILK_MAX_ORDER_LPC + 1];
96 let mut af = [0f64; SILK_MAX_ORDER_LPC];
97 let mut c0 = energy(&x[..nb_subfr * subfr_length]);
98 for s in 0..nb_subfr {
99 let xp = s * subfr_length;
100 for n in 1..d + 1 {
101 c_first_row[n - 1] += inner_product(
102 &x[xp..xp + subfr_length - n],
103 &x[xp + n..xp + subfr_length],
104 subfr_length - n,
105 );
106 }
107 }
108 c_last_row = c_first_row;
109
110 caf[0] = c0 + (FIND_LPC_COND_FAC as f64) * c0 + 1e-1;
111 cab[0] = caf[0];
112 let mut inv_gain = 1.0f64;
113 let mut reached_max_gain = false;
114 for n in 0..d {
115 for s in 0..nb_subfr {
116 let xp = s * subfr_length;
117 let mut tmp1 = x[xp + n] as f64;
118 let mut tmp2 = x[xp + subfr_length - n - 1] as f64;
119 for k in 0..n {
120 c_first_row[k] -= (x[xp + n] * x[xp + n - k - 1]) as f64;
121 c_last_row[k] -=
122 (x[xp + subfr_length - n - 1] * x[xp + subfr_length - n + k]) as f64;
123 let atmp = af[k];
124 tmp1 += x[xp + n - k - 1] as f64 * atmp;
125 tmp2 += x[xp + subfr_length - n + k] as f64 * atmp;
126 }
127 for k in 0..=n {
128 caf[k] -= tmp1 * x[xp + n - k] as f64;
129 cab[k] -= tmp2 * x[xp + subfr_length - n + k - 1] as f64;
130 }
131 }
132 let mut tmp1 = c_first_row[n];
133 let mut tmp2 = c_last_row[n];
134 for k in 0..n {
135 let atmp = af[k];
136 tmp1 += c_last_row[n - k - 1] * atmp;
137 tmp2 += c_first_row[n - k - 1] * atmp;
138 }
139 caf[n + 1] = tmp1;
140 cab[n + 1] = tmp2;
141
142 let mut num = cab[n + 1];
143 let mut nrg_b = cab[0];
144 let mut nrg_f = caf[0];
145 for k in 0..n {
146 let atmp = af[k];
147 num += cab[n - k] * atmp;
148 nrg_b += cab[k + 1] * atmp;
149 nrg_f += caf[k + 1] * atmp;
150 }
151
152 let mut rc = -2.0 * num / (nrg_f + nrg_b);
153
154 let tmp1 = inv_gain * (1.0 - rc * rc);
155 if tmp1 <= min_inv_gain as f64 {
156 rc = (1.0 - min_inv_gain as f64 / inv_gain).sqrt();
157 if num > 0.0 {
158 rc = -rc;
159 }
160 inv_gain = min_inv_gain as f64;
161 reached_max_gain = true;
162 } else {
163 inv_gain = tmp1;
164 }
165
166 for k in 0..(n + 1) >> 1 {
167 let t1 = af[k];
168 let t2 = af[n - k - 1];
169 af[k] = t1 + rc * t2;
170 af[n - k - 1] = t2 + rc * t1;
171 }
172 af[n] = rc;
173
174 if reached_max_gain {
175 for v in af.iter_mut().take(d).skip(n + 1) {
176 *v = 0.0;
177 }
178 break;
179 }
180
181 for k in 0..=n + 1 {
182 let t1 = caf[k];
183 caf[k] += rc * cab[n + 1 - k];
184 cab[n + 1 - k] += rc * t1;
185 }
186 }
187
188 let nrg_f = if reached_max_gain {
189 for k in 0..d {
190 a[k] = (-af[k]) as f32;
191 }
192 for s in 0..nb_subfr {
193 c0 -= energy(&x[s * subfr_length..s * subfr_length + d]);
194 }
195 c0 * inv_gain
196 } else {
197 let mut nrg_f = caf[0];
198 let mut tmp1 = 1.0f64;
199 for k in 0..d {
200 let atmp = af[k];
201 nrg_f += caf[k + 1] * atmp;
202 tmp1 += atmp * atmp;
203 a[k] = (-atmp) as f32;
204 }
205 nrg_f - (FIND_LPC_COND_FAC as f64) * c0 * tmp1
206 };
207 nrg_f as f32
208}
209
210/// `silk_corrVector_FLP`.
211fn corr_vector(x: &[f32], t: &[f32], l: usize, order: usize, xt: &mut [f32]) {
212 let p1 = order - 1;
213 for lag in 0..order {
214 xt[lag] = inner_product(&x[p1 - lag..p1 - lag + l], &t[..l], l) as f32;
215 }
216}
217
218/// `silk_corrMatrix_FLP`.
219fn corr_matrix(x: &[f32], l: usize, order: usize, xx: &mut [f32]) {
220 let p1 = order - 1;
221 let mut e = energy(&x[p1..p1 + l]);
222 xx[0] = e as f32;
223 for j in 1..order {
224 e += (x[p1 - j] * x[p1 - j] - x[p1 + l - j] * x[p1 + l - j]) as f64;
225 xx[j * order + j] = e as f32;
226 }
227 for lag in 1..order {
228 let p2 = order - 1 - lag;
229 let mut e2 = inner_product(&x[p1..p1 + l], &x[p2..p2 + l], l);
230 xx[lag * order] = e2 as f32;
231 xx[lag] = e2 as f32;
232 for j in 1..order - lag {
233 e2 += (x[p1 - j] * x[p2 - j] - x[p1 + l - j] * x[p2 + l - j]) as f64;
234 xx[(lag + j) * order + j] = e2 as f32;
235 xx[j * order + lag + j] = e2 as f32;
236 }
237 }
238}
239
240/// `silk_find_LTP_FLP`. `r` is the whole pitch residual buffer, the C
241/// `r_ptr` sits at `r_base`.
242fn find_ltp(
243 xx: &mut [f32],
244 xx_vec: &mut [f32],
245 r: &[f32],
246 r_base: usize,
247 lag: &[i32],
248 subfr_length: usize,
249 nb_subfr: usize,
250) {
251 let mut r_ptr = r_base;
252 for k in 0..nb_subfr {
253 let lag_ptr = r_ptr - (lag[k] as usize + LTP_ORDER / 2);
254 let xx_k = &mut xx[k * LTP_ORDER * LTP_ORDER..(k + 1) * LTP_ORDER * LTP_ORDER];
255 corr_matrix(&r[lag_ptr..], subfr_length, LTP_ORDER, xx_k);
256 corr_vector(
257 &r[lag_ptr..],
258 &r[r_ptr..],
259 subfr_length,
260 LTP_ORDER,
261 &mut xx_vec[k * LTP_ORDER..(k + 1) * LTP_ORDER],
262 );
263 let xx_e = energy(&r[r_ptr..r_ptr + subfr_length + LTP_ORDER]) as f32;
264 let thr = LTP_CORR_INV_MAX * 0.5 * (xx_k[0] + xx_k[24]) + 1.0;
265 let m = if xx_e > thr { xx_e } else { thr };
266 let temp = 1.0 / m;
267 for v in xx_k.iter_mut() {
268 *v *= temp;
269 }
270 for v in xx_vec[k * LTP_ORDER..(k + 1) * LTP_ORDER].iter_mut() {
271 *v *= temp;
272 }
273 r_ptr += subfr_length;
274 }
275}
276
277/// `silk_regularize_correlations_FLP`.
278#[allow(dead_code)]
279fn regularize_correlations(xx: &mut [f32], xx_vec: &mut [f32], noise: f32, d: usize) {
280 for i in 0..d {
281 xx[i * d + i] += noise;
282 }
283 xx_vec[0] += noise;
284}
285
286/// `silk_residual_energy_covar_FLP`.
287#[allow(dead_code)]
288fn residual_energy_covar(
289 c: &[f32],
290 wxx: &mut [f32],
291 wxx_vec: &[f32],
292 wxx_scalar: f32,
293 d: usize,
294) -> f32 {
295 const MAX_ITERATIONS_RESIDUAL_NRG: usize = 10;
296 const REGULARIZATION_FACTOR: f32 = 1e-8;
297 let mut nrg = 0.0f32;
298 let mut regularization = REGULARIZATION_FACTOR * (wxx[0] + wxx[d * d - 1]);
299 let mut k = 0;
300 while k < MAX_ITERATIONS_RESIDUAL_NRG {
301 nrg = wxx_scalar;
302 let mut tmp = 0.0f32;
303 for i in 0..d {
304 tmp += wxx_vec[i] * c[i];
305 }
306 nrg -= 2.0 * tmp;
307 for i in 0..d {
308 let mut tmp = 0.0f32;
309 for j in i + 1..d {
310 tmp += wxx[i + j * d] * c[j];
311 }
312 nrg += c[i] * (2.0 * tmp + wxx[i + i * d] * c[i]);
313 }
314 if nrg > 0.0 {
315 break;
316 }
317 for i in 0..d {
318 wxx[i + i * d] += regularization;
319 }
320 regularization *= 2.0;
321 k += 1;
322 }
323 if k == MAX_ITERATIONS_RESIDUAL_NRG {
324 nrg = 1.0;
325 }
326 nrg
327}
328
329/// `silk_residual_energy_FLP`.
330fn residual_energy(
331 nrgs: &mut [f32; MAX_NB_SUBFR],
332 x: &[f32],
333 a: &[[f32; MAX_LPC_ORDER]; 2],
334 gains: &[f32],
335 subfr_length: usize,
336 nb_subfr: usize,
337 lpc_order: usize,
338) {
339 let mut lpc_res = [0f32; (MAX_FRAME_LENGTH + MAX_NB_SUBFR * MAX_LPC_ORDER) / 2];
340 let shift = lpc_order + subfr_length;
341 lpc_analysis_filter(&mut lpc_res, &a[0], &x[..2 * shift], 2 * shift, lpc_order);
342 nrgs[0] = ((gains[0] * gains[0]) as f64 * energy(&lpc_res[lpc_order..lpc_order + subfr_length]))
343 as f32;
344 nrgs[1] = ((gains[1] * gains[1]) as f64
345 * energy(&lpc_res[lpc_order + shift..lpc_order + shift + subfr_length]))
346 as f32;
347 if nb_subfr == MAX_NB_SUBFR {
348 lpc_analysis_filter(
349 &mut lpc_res,
350 &a[1],
351 &x[2 * shift..4 * shift],
352 2 * shift,
353 lpc_order,
354 );
355 nrgs[2] = ((gains[2] * gains[2]) as f64
356 * energy(&lpc_res[lpc_order..lpc_order + subfr_length])) as f32;
357 nrgs[3] = ((gains[3] * gains[3]) as f64
358 * energy(&lpc_res[lpc_order + shift..lpc_order + shift + subfr_length]))
359 as f32;
360 }
361}
362
363/// `silk_LTP_analysis_filter_FLP`. `x` is the whole signal buffer, the C
364/// `x` pointer sits at `x_base`.
365fn ltp_analysis_filter(
366 ltp_res: &mut [f32],
367 x: &[f32],
368 x_base: usize,
369 b: &[f32],
370 pitch_l: &[i32],
371 inv_gains: &[f32],
372 subfr_length: usize,
373 nb_subfr: usize,
374 pre_length: usize,
375) {
376 let mut x_ptr = x_base;
377 let mut res_ptr = 0;
378 for k in 0..nb_subfr {
379 let lag = pitch_l[k] as usize;
380 let inv_gain = inv_gains[k];
381 let mut btmp = [0f32; LTP_ORDER];
382 btmp.copy_from_slice(&b[k * LTP_ORDER..(k + 1) * LTP_ORDER]);
383 for i in 0..subfr_length + pre_length {
384 let lag_idx = x_ptr + i + LTP_ORDER / 2 - lag;
385 let mut v = x[x_ptr + i];
386 for j in 0..LTP_ORDER {
387 v -= btmp[j] * x[lag_idx - j];
388 }
389 ltp_res[res_ptr + i] = v * inv_gain;
390 }
391 res_ptr += subfr_length + pre_length;
392 x_ptr += subfr_length;
393 }
394}
395
396/// `silk_LTP_scale_ctrl_FLP`.
397fn ltp_scale_ctrl(st: &mut EncState, ctrl: &mut EncCtrl, cond_coding: CondCoding) {
398 if cond_coding == CondCoding::Independently {
399 let mut round_loss = st.packet_loss_perc * st.n_frames_per_packet as i32;
400 if st.lbrr_flag {
401 round_loss = 2 + smulbb(round_loss, round_loss) / 100;
402 }
403 let v = smulbb(ctrl.lt_pred_cod_gain as i32, round_loss);
404 let snr = st.snr_db_q7;
405 st.indices.ltp_scale_index =
406 (v > log2lin(2900 - snr)) as i8 + (v > log2lin(3900 - snr)) as i8;
407 } else {
408 st.indices.ltp_scale_index = 0;
409 }
410 ctrl.ltp_scale = LTPSCALES_TABLE_Q14[st.indices.ltp_scale_index as usize] as f32 / 16384.0;
411}
412
413/// `silk_VQ_WMat_EC_c`. Returns `(ind, res_nrg_Q15, rate_dist_Q8)`; `gain_q7`
414/// is only written when a candidate is accepted, as in C.
415fn vq_wmat_ec(
416 gain_q7: &mut i32,
417 xx: &[i32],
418 xx_vec: &[i32],
419 cb: &[[i8; LTP_ORDER]],
420 cb_gain: &[u8],
421 cl: &[u8],
422 subfr_len: i32,
423 max_gain_q7: i32,
424) -> (i8, i32, i32) {
425 let mut neg = [0i32; LTP_ORDER];
426 for i in 0..LTP_ORDER {
427 neg[i] = (xx_vec[i] << 7).wrapping_neg();
428 }
429 let mut rate_dist = i32::MAX;
430 let mut res_nrg = i32::MAX;
431 let mut ind = 0i8;
432 let mla = |a: i32, b: i32, c: i32| a.wrapping_add(b.wrapping_mul(c));
433 for (k, row) in cb.iter().enumerate() {
434 let c = row.map(|v| v as i32);
435 let gain_tmp = cb_gain[k] as i32;
436 let mut sum1: i32 = QUANT_ERR_INIT_Q15;
437 let penalty = gain_tmp.wrapping_sub(max_gain_q7).max(0) << 11;
438
439 let mut sum2 = mla(neg[0], xx[1], c[1]);
440 sum2 = mla(sum2, xx[2], c[2]);
441 sum2 = mla(sum2, xx[3], c[3]);
442 sum2 = mla(sum2, xx[4], c[4]);
443 sum2 <<= 1;
444 sum2 = mla(sum2, xx[0], c[0]);
445 sum1 = smlawb(sum1, sum2, c[0]);
446
447 let mut sum2 = mla(neg[1], xx[7], c[2]);
448 sum2 = mla(sum2, xx[8], c[3]);
449 sum2 = mla(sum2, xx[9], c[4]);
450 sum2 <<= 1;
451 sum2 = mla(sum2, xx[6], c[1]);
452 sum1 = smlawb(sum1, sum2, c[1]);
453
454 let mut sum2 = mla(neg[2], xx[13], c[3]);
455 sum2 = mla(sum2, xx[14], c[4]);
456 sum2 <<= 1;
457 sum2 = mla(sum2, xx[12], c[2]);
458 sum1 = smlawb(sum1, sum2, c[2]);
459
460 let mut sum2 = mla(neg[3], xx[19], c[4]);
461 sum2 <<= 1;
462 sum2 = mla(sum2, xx[18], c[3]);
463 sum1 = smlawb(sum1, sum2, c[3]);
464
465 let mut sum2 = neg[4] << 1;
466 sum2 = mla(sum2, xx[24], c[4]);
467 sum1 = smlawb(sum1, sum2, c[4]);
468
469 if sum1 >= 0 {
470 let bits_res = smulbb(
471 subfr_len,
472 lin2log(sum1.wrapping_add(penalty)) - LTP_RATE_PENALTY_OFFSET,
473 );
474 let bits_tot = bits_res.wrapping_add((cl[k] as i32) << 2);
475 if bits_tot <= rate_dist {
476 rate_dist = bits_tot;
477 res_nrg = sum1.wrapping_add(penalty);
478 ind = k as i8;
479 *gain_q7 = gain_tmp;
480 }
481 }
482 }
483 (ind, res_nrg, rate_dist)
484}
485
486/// `silk_quant_LTP_gains`.
487fn quant_ltp_gains(
488 b_q14: &mut [i16; MAX_NB_SUBFR * LTP_ORDER],
489 cbk_index: &mut [i8; MAX_NB_SUBFR],
490 periodicity_index: &mut i8,
491 sum_log_gain_q7: &mut i32,
492 pred_gain_db_q7: &mut i32,
493 xx_q17: &[i32; MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER],
494 xx_vec_q17: &[i32; MAX_NB_SUBFR * LTP_ORDER],
495 subfr_len: usize,
496 nb_subfr: usize,
497) {
498 let mut min_rate_dist = i32::MAX;
499 let mut best_sum_log_gain = 0i32;
500 let mut res_nrg_q15 = 0i32;
501 let mut gain_q7 = 0i32;
502 for k in 0..3 {
503 let cl = LTP_GAIN_BITS_Q5[k];
504 let cbk: &[[i8; LTP_ORDER]] = match k {
505 0 => &LTP_GAIN_VQ_0,
506 1 => &LTP_GAIN_VQ_1,
507 _ => &LTP_GAIN_VQ_2,
508 };
509 let cbk_gain = LTP_VQ_GAIN_Q7[k];
510
511 let mut temp_idx = [0i8; MAX_NB_SUBFR];
512 res_nrg_q15 = 0;
513 let mut rate_dist_q7 = 0i32;
514 let mut sum_log_gain_tmp = *sum_log_gain_q7;
515 for j in 0..nb_subfr {
516 let max_gain_q7 =
517 log2lin((MAX_SUM_LOG_GAIN_Q7 - sum_log_gain_tmp) + ONE_Q7) - LTP_GAIN_SAFETY_Q7;
518 let (ind, res_sub, rate_sub) = vq_wmat_ec(
519 &mut gain_q7,
520 &xx_q17[j * LTP_ORDER * LTP_ORDER..(j + 1) * LTP_ORDER * LTP_ORDER],
521 &xx_vec_q17[j * LTP_ORDER..(j + 1) * LTP_ORDER],
522 cbk,
523 cbk_gain,
524 cl,
525 subfr_len as i32,
526 max_gain_q7,
527 );
528 temp_idx[j] = ind;
529 res_nrg_q15 = add_pos_sat32(res_nrg_q15, res_sub);
530 rate_dist_q7 = add_pos_sat32(rate_dist_q7, rate_sub);
531 sum_log_gain_tmp =
532 0.max(sum_log_gain_tmp + lin2log(LTP_GAIN_SAFETY_Q7 + gain_q7) - ONE_Q7);
533 }
534
535 if rate_dist_q7 <= min_rate_dist {
536 min_rate_dist = rate_dist_q7;
537 *periodicity_index = k as i8;
538 cbk_index[..nb_subfr].copy_from_slice(&temp_idx[..nb_subfr]);
539 best_sum_log_gain = sum_log_gain_tmp;
540 }
541 }
542
543 let cbk: &[[i8; LTP_ORDER]] = match *periodicity_index {
544 0 => &LTP_GAIN_VQ_0,
545 1 => &LTP_GAIN_VQ_1,
546 _ => &LTP_GAIN_VQ_2,
547 };
548 for j in 0..nb_subfr {
549 for k in 0..LTP_ORDER {
550 b_q14[j * LTP_ORDER + k] = ((cbk[cbk_index[j] as usize][k] as i32) << 7) as i16;
551 }
552 }
553
554 if nb_subfr == 2 {
555 res_nrg_q15 >>= 1;
556 } else {
557 res_nrg_q15 >>= 2;
558 }
559
560 *sum_log_gain_q7 = best_sum_log_gain;
561 *pred_gain_db_q7 = smulbb(-3, lin2log(res_nrg_q15) - LTP_RATE_PENALTY_OFFSET);
562}
563
564/// `silk_quant_LTP_gains_FLP`.
565fn quant_ltp_gains_flp(
566 b: &mut [f32],
567 cbk_index: &mut [i8; MAX_NB_SUBFR],
568 periodicity_index: &mut i8,
569 sum_log_gain_q7: &mut i32,
570 pred_gain_db: &mut f32,
571 xx: &[f32],
572 xx_vec: &[f32],
573 subfr_len: usize,
574 nb_subfr: usize,
575) {
576 let mut xx_q17 = [0i32; MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER];
577 for i in 0..nb_subfr * LTP_ORDER * LTP_ORDER {
578 xx_q17[i] = float2int(xx[i] * 131072.0);
579 }
580 let mut xx_vec_q17 = [0i32; MAX_NB_SUBFR * LTP_ORDER];
581 for i in 0..nb_subfr * LTP_ORDER {
582 xx_vec_q17[i] = float2int(xx_vec[i] * 131072.0);
583 }
584
585 let mut b_q14 = [0i16; MAX_NB_SUBFR * LTP_ORDER];
586 let mut pred_gain_db_q7 = 0i32;
587 quant_ltp_gains(
588 &mut b_q14,
589 cbk_index,
590 periodicity_index,
591 sum_log_gain_q7,
592 &mut pred_gain_db_q7,
593 &xx_q17,
594 &xx_vec_q17,
595 subfr_len,
596 nb_subfr,
597 );
598
599 for i in 0..nb_subfr * LTP_ORDER {
600 b[i] = b_q14[i] as f32 * (1.0 / 16384.0);
601 }
602 *pred_gain_db = pred_gain_db_q7 as f32 * (1.0 / 128.0);
603}
604
605/// `silk_find_LPC_FLP`.
606fn find_lpc(st: &mut EncState, nlsf_q15: &mut [i16; MAX_LPC_ORDER], x: &[f32], min_inv_gain: f32) {
607 let order = st.predict_lpc_order;
608 let sub = st.subfr_length + order;
609 st.indices.nlsf_interp_coef_q2 = 4;
610
611 let mut a = [0f32; MAX_LPC_ORDER];
612 let mut res_nrg = burg_modified(&mut a, x, min_inv_gain, sub, st.nb_subfr, order);
613
614 if st.use_interpolated_nlsfs && !st.first_frame_after_reset && st.nb_subfr == MAX_NB_SUBFR {
615 let mut a_tmp = [0f32; MAX_LPC_ORDER];
616 res_nrg -= burg_modified(
617 &mut a_tmp,
618 &x[(MAX_NB_SUBFR / 2) * sub..],
619 min_inv_gain,
620 sub,
621 MAX_NB_SUBFR / 2,
622 order,
623 );
624
625 a2nlsf_flp(&mut nlsf_q15[..], &a_tmp[..], order);
626
627 let mut res_nrg_2nd = f32::MAX;
628 for k in (0..4i32).rev() {
629 let mut nlsf0 = [0i16; MAX_LPC_ORDER];
630 interpolate(
631 &mut nlsf0[..],
632 &st.prev_nlsfq_q15[..],
633 &nlsf_q15[..],
634 k,
635 order,
636 );
637 nlsf2a_flp(&mut a_tmp[..], &nlsf0[..], order);
638
639 let mut lpc_res = [0f32; MAX_FRAME_LENGTH + MAX_NB_SUBFR * MAX_LPC_ORDER];
640 lpc_analysis_filter(&mut lpc_res, &a_tmp, x, 2 * sub, order);
641 let res_nrg_interp =
642 (energy(&lpc_res[order..sub]) + energy(&lpc_res[order + sub..2 * sub])) as f32;
643
644 if res_nrg_interp < res_nrg {
645 res_nrg = res_nrg_interp;
646 st.indices.nlsf_interp_coef_q2 = k as i8;
647 } else if res_nrg_interp > res_nrg_2nd {
648 break;
649 }
650 res_nrg_2nd = res_nrg_interp;
651 }
652 }
653
654 if st.indices.nlsf_interp_coef_q2 == 4 {
655 a2nlsf_flp(&mut nlsf_q15[..], &a[..], order);
656 }
657}
658
659/// `silk_find_pred_coefs_FLP`.
660pub(crate) fn find_pred_coefs(
661 st: &mut EncState,
662 ctrl: &mut EncCtrl,
663 res_pitch: &[f32],
664 cond_coding: CondCoding,
665) {
666 let nb = st.nb_subfr;
667 let sub = st.subfr_length;
668 let order = st.predict_lpc_order;
669 let x_buf = st.x_buf;
670 let x_frame = st.ltp_mem_length;
671
672 let mut inv_gains = [0f32; MAX_NB_SUBFR];
673 for i in 0..nb {
674 inv_gains[i] = 1.0 / ctrl.gains[i];
675 }
676
677 let mut lpc_in_pre = [0f32; MAX_NB_SUBFR * MAX_LPC_ORDER + MAX_FRAME_LENGTH];
678 if st.indices.signal_type == TYPE_VOICED {
679 let mut xx = [0f32; MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER];
680 let mut xx_vec = [0f32; MAX_NB_SUBFR * LTP_ORDER];
681 find_ltp(
682 &mut xx,
683 &mut xx_vec,
684 res_pitch,
685 x_frame,
686 &ctrl.pitch_l,
687 sub,
688 nb,
689 );
690
691 quant_ltp_gains_flp(
692 &mut ctrl.ltp_coef,
693 &mut st.indices.ltp_index,
694 &mut st.indices.per_index,
695 &mut st.sum_log_gain_q7,
696 &mut ctrl.lt_pred_cod_gain,
697 &xx,
698 &xx_vec,
699 sub,
700 nb,
701 );
702
703 ltp_scale_ctrl(st, ctrl, cond_coding);
704
705 ltp_analysis_filter(
706 &mut lpc_in_pre,
707 &x_buf,
708 x_frame - order,
709 &ctrl.ltp_coef,
710 &ctrl.pitch_l,
711 &inv_gains,
712 sub,
713 nb,
714 order,
715 );
716 } else {
717 let mut x_ptr = x_frame - order;
718 let mut pre_ptr = 0;
719 for i in 0..nb {
720 for j in 0..sub + order {
721 lpc_in_pre[pre_ptr + j] = inv_gains[i] * x_buf[x_ptr + j];
722 }
723 pre_ptr += sub + order;
724 x_ptr += sub;
725 }
726 ctrl.ltp_coef[..nb * LTP_ORDER].fill(0.0);
727 ctrl.lt_pred_cod_gain = 0.0;
728 st.sum_log_gain_q7 = 0;
729 }
730
731 let min_inv_gain = if st.first_frame_after_reset {
732 1.0 / MAX_PREDICTION_POWER_GAIN_AFTER_RESET
733 } else {
734 let p = (2f64).powf((ctrl.lt_pred_cod_gain / 3.0) as f64) as f32;
735 let mut m = p / MAX_PREDICTION_POWER_GAIN;
736 m /= 0.25 + 0.75 * ctrl.coding_quality;
737 m
738 };
739
740 let mut nlsf_q15 = [0i16; MAX_LPC_ORDER];
741 find_lpc(st, &mut nlsf_q15, &lpc_in_pre, min_inv_gain);
742
743 let prev = st.prev_nlsfq_q15;
744 process_nlsfs_flp(st, &mut ctrl.pred_coef, &mut nlsf_q15, &prev);
745
746 residual_energy(
747 &mut ctrl.res_nrg,
748 &lpc_in_pre,
749 &ctrl.pred_coef,
750 &ctrl.gains,
751 sub,
752 nb,
753 order,
754 );
755
756 st.prev_nlsfq_q15 = nlsf_q15;
757}
758
759/// `silk_process_gains_FLP`.
760pub(crate) fn process_gains(st: &mut EncState, ctrl: &mut EncCtrl, cond_coding: CondCoding) {
761 let nb = st.nb_subfr;
762
763 if st.indices.signal_type == TYPE_VOICED {
764 let s = 1.0f32 - 0.5 * sigmoid(0.25 * (ctrl.lt_pred_cod_gain - 12.0));
765 for k in 0..nb {
766 ctrl.gains[k] *= s;
767 }
768 }
769
770 let snr_term = st.snr_db_q7 as f32 * (1.0 / 128.0);
771 let inv_max_sqr_val =
772 ((2f64).powf((0.33f32 * (21.0 - snr_term)) as f64) / st.subfr_length as f64) as f32;
773
774 for k in 0..nb {
775 let gain = ctrl.gains[k];
776 let gain = ((gain * gain + ctrl.res_nrg[k] * inv_max_sqr_val) as f64).sqrt() as f32;
777 ctrl.gains[k] = if gain < 32767.0 { gain } else { 32767.0 };
778 }
779
780 let mut p_gains_q16 = [0i32; MAX_NB_SUBFR];
781 for k in 0..nb {
782 p_gains_q16[k] = (ctrl.gains[k] * 65536.0) as i32;
783 }
784
785 for k in 0..nb {
786 ctrl.gains_unq_q16[k] = p_gains_q16[k];
787 }
788 ctrl.last_gain_index_prev = st.s_shape.last_gain_index;
789
790 gains_quant(
791 &mut st.indices.gains_indices,
792 &mut p_gains_q16,
793 &mut st.s_shape.last_gain_index,
794 cond_coding == CondCoding::Conditionally,
795 nb,
796 );
797
798 for k in 0..nb {
799 ctrl.gains[k] = p_gains_q16[k] as f32 / 65536.0;
800 }
801
802 if st.indices.signal_type == TYPE_VOICED {
803 st.indices.quant_offset_type =
804 if ctrl.lt_pred_cod_gain + st.input_tilt_q15 as f32 * (1.0 / 32768.0) > 1.0 {
805 0
806 } else {
807 1
808 };
809 }
810
811 let quant_offset = QUANTIZATION_OFFSETS_Q10[(st.indices.signal_type >> 1) as usize]
812 [st.indices.quant_offset_type as usize] as f32
813 / 1024.0;
814 ctrl.lambda = LAMBDA_OFFSET
815 + LAMBDA_DELAYED_DECISIONS * st.n_states_delayed_decision as f32
816 + LAMBDA_SPEECH_ACT * st.speech_activity_q8 as f32 * (1.0 / 256.0)
817 + LAMBDA_INPUT_QUALITY * ctrl.input_quality
818 + LAMBDA_CODING_QUALITY * ctrl.coding_quality
819 + LAMBDA_QUANT_OFFSET * quant_offset;
820}
821
822/// `silk_gains_quant`.
823pub(crate) fn gains_quant(
824 ind: &mut [i8; MAX_NB_SUBFR],
825 gain_q16: &mut [i32; MAX_NB_SUBFR],
826 prev_ind: &mut i8,
827 conditional: bool,
828 nb_subfr: usize,
829) {
830 let mut prev = *prev_ind as i32;
831 for k in 0..nb_subfr {
832 let mut v = smulwb(SCALE_Q16, lin2log(gain_q16[k]) - OFFSET);
833
834 if v < prev {
835 v += 1;
836 }
837 v = limit(v, 0, N_LEVELS_QGAIN - 1);
838
839 if k == 0 && !conditional {
840 v = limit(v, prev + MIN_DELTA_GAIN_QUANT, N_LEVELS_QGAIN - 1);
841 prev = v;
842 } else {
843 v -= prev;
844 let double_step_size_threshold = 2 * MAX_DELTA_GAIN_QUANT - N_LEVELS_QGAIN + prev;
845 if v > double_step_size_threshold {
846 v = double_step_size_threshold + ((v - double_step_size_threshold + 1) >> 1);
847 }
848 v = limit(v, MIN_DELTA_GAIN_QUANT, MAX_DELTA_GAIN_QUANT);
849
850 if v > double_step_size_threshold {
851 prev += (v << 1) - double_step_size_threshold;
852 prev = prev.min(N_LEVELS_QGAIN - 1);
853 } else {
854 prev += v;
855 }
856 v -= MIN_DELTA_GAIN_QUANT;
857 }
858 ind[k] = v as i8;
859
860 gain_q16[k] = log2lin(smulwb(INV_SCALE_Q16, prev).wrapping_add(OFFSET).min(3967));
861 }
862 *prev_ind = prev as i8;
863}
864
865/// `silk_gains_ID`.
866pub(crate) fn gains_id(ind: &[i8; MAX_NB_SUBFR], nb_subfr: usize) -> i32 {
867 let mut id = 0i32;
868 for k in 0..nb_subfr {
869 id = (ind[k] as i32).wrapping_add(id << 8);
870 }
871 id
872}