xref: /aosp_15_r20/external/libxaac/encoder/iusace_lpd_utils.c (revision 15dc779a375ca8b5125643b829a8aa4b70d7f451)
1 /******************************************************************************
2  *                                                                            *
3  * Copyright (C) 2023 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19  */
20 
21 #include <string.h>
22 #include <math.h>
23 #include "ixheaac_type_def.h"
24 #include "ixheaace_adjust_threshold_data.h"
25 #include "iusace_bitbuffer.h"
26 
27 /* DRC */
28 #include "impd_drc_common_enc.h"
29 #include "impd_drc_uni_drc.h"
30 #include "impd_drc_tables.h"
31 #include "impd_drc_api.h"
32 #include "impd_drc_uni_drc_eq.h"
33 #include "impd_drc_uni_drc_filter_bank.h"
34 #include "impd_drc_gain_enc.h"
35 #include "impd_drc_struct_def.h"
36 
37 #include "iusace_cnst.h"
38 #include "iusace_tns_usac.h"
39 #include "iusace_psy_mod.h"
40 #include "iusace_fd_qc_util.h"
41 #include "iusace_tns_usac.h"
42 #include "iusace_config.h"
43 #include "iusace_arith_enc.h"
44 #include "iusace_fd_quant.h"
45 #include "iusace_block_switch_const.h"
46 #include "iusace_block_switch_struct_def.h"
47 #include "iusace_ms.h"
48 #include "iusace_signal_classifier.h"
49 #include "ixheaace_sbr_header.h"
50 #include "ixheaace_config.h"
51 #include "ixheaace_asc_write.h"
52 #include "iusace_main.h"
53 #include "iusace_func_prototypes.h"
54 #include "iusace_lpd_rom.h"
55 #include "ixheaace_common_utils.h"
56 
ia_get_sample_rate(WORD32 sample_rate)57 WORD32 ia_get_sample_rate(WORD32 sample_rate) {
58   if (92017 <= sample_rate) {
59     return 11;
60   }
61   if (75132 <= sample_rate) {
62     return 10;
63   }
64   if (55426 <= sample_rate) {
65     return 9;
66   }
67   if (46009 <= sample_rate) {
68     return 8;
69   }
70   if (37566 <= sample_rate) {
71     return 7;
72   }
73   if (27713 <= sample_rate) {
74     return 6;
75   }
76   if (23004 <= sample_rate) {
77     return 5;
78   }
79   if (18783 <= sample_rate) {
80     return 4;
81   }
82   if (13856 <= sample_rate) {
83     return 3;
84   }
85   if (11502 <= sample_rate) {
86     return 2;
87   }
88   if (9391 <= sample_rate) {
89     return 1;
90   }
91   return 0;
92 }
93 
iusace_write_bits2buf(WORD32 value,WORD32 no_of_bits,WORD16 * bitstream)94 VOID iusace_write_bits2buf(WORD32 value, WORD32 no_of_bits, WORD16 *bitstream) {
95   WORD16 *pt_bitstream;
96   WORD32 i;
97   pt_bitstream = bitstream + no_of_bits;
98   for (i = 0; i < no_of_bits; i++) {
99     *--pt_bitstream = (WORD16)(value & MASK);
100     value >>= 1;
101   }
102   return;
103 }
104 
iusace_get_num_params(WORD32 * qn)105 WORD32 iusace_get_num_params(WORD32 *qn) {
106   return 2 + ((qn[0] > 0) ? 9 : 0) + ((qn[1] > 0) ? 9 : 0);
107 }
108 
iusace_cal_segsnr(FLOAT32 * sig1,FLOAT32 * sig2,WORD16 len,WORD16 nseg)109 FLOAT32 iusace_cal_segsnr(FLOAT32 *sig1, FLOAT32 *sig2, WORD16 len, WORD16 nseg) {
110   FLOAT32 snr = 0.0f;
111   FLOAT32 signal, noise, error, fac;
112   WORD16 i, j;
113   for (i = 0; i < len; i += nseg) {
114     signal = 1e-6f;
115     noise = 1e-6f;
116     for (j = 0; j < nseg; j++) {
117       signal += (*sig1) * (*sig1);
118       error = *sig1++ - *sig2++;
119       noise += error * error;
120     }
121     snr += (FLOAT32)log10((FLOAT64)(signal / noise));
122   }
123   fac = ((FLOAT32)(10 * nseg)) / (FLOAT32)len;
124   snr = fac * snr;
125   if (snr < -99.0f) {
126     snr = -99.0f;
127   }
128   return (snr);
129 }
130 
iusace_highpass_50hz_12k8(FLOAT32 * signal,WORD32 lg,FLOAT32 * mem,WORD32 fscale)131 VOID iusace_highpass_50hz_12k8(FLOAT32 *signal, WORD32 lg, FLOAT32 *mem, WORD32 fscale) {
132   WORD32 i;
133   WORD32 sr_idx = 0;
134   FLOAT32 x0, x1, x2, y0, y1, y2;
135   const FLOAT32 *a = NULL, *b = NULL;
136 
137   y1 = mem[0];
138   y2 = mem[1];
139   x0 = mem[2];
140   x1 = mem[3];
141   sr_idx = ia_get_sample_rate(fscale);
142   a = &iusace_hp20_filter_coeffs[sr_idx][0];
143   b = &iusace_hp20_filter_coeffs[sr_idx][2];
144 
145   for (i = 0; i < lg; i++) {
146     x2 = x1;
147     x1 = x0;
148     x0 = signal[i];
149     y0 = (y1 * a[0]) + (y2 * a[1]) + (x0 * b[1]) + (x1 * b[0]) + (x2 * b[1]);
150     signal[i] = y0;
151     y2 = y1;
152     y1 = y0;
153   }
154 
155   mem[0] = ((y1 > 1e-10) | (y1 < -1e-10)) ? y1 : 0;
156   mem[1] = ((y2 > 1e-10) | (y2 < -1e-10)) ? y2 : 0;
157   mem[2] = ((x0 > 1e-10) | (x0 < -1e-10)) ? x0 : 0;
158   mem[3] = ((x1 > 1e-10) | (x1 < -1e-10)) ? x1 : 0;
159 }
160 
iusace_apply_preemph(FLOAT32 * signal,FLOAT32 factor,WORD32 length,FLOAT32 * mem)161 VOID iusace_apply_preemph(FLOAT32 *signal, FLOAT32 factor, WORD32 length, FLOAT32 *mem) {
162   WORD32 i;
163   FLOAT32 temp;
164   temp = signal[length - 1];
165   for (i = length - 1; i > 0; i--) {
166     signal[i] = signal[i] - factor * signal[i - 1];
167   }
168   signal[0] -= factor * (*mem);
169   *mem = temp;
170 }
171 
iusace_apply_deemph(FLOAT32 * signal,FLOAT32 factor,WORD32 length,FLOAT32 * mem)172 VOID iusace_apply_deemph(FLOAT32 *signal, FLOAT32 factor, WORD32 length, FLOAT32 *mem) {
173   WORD32 i;
174   signal[0] = signal[0] + factor * (*mem);
175   for (i = 1; i < length; i++) {
176     signal[i] = signal[i] + factor * signal[i - 1];
177   }
178   *mem = signal[length - 1];
179   if ((*mem < 1e-10) & (*mem > -1e-10)) {
180     *mem = 0;
181   }
182 }
183 
iusace_synthesis_tool_float(FLOAT32 * a,FLOAT32 * x,FLOAT32 * y,WORD32 l,FLOAT32 * mem,FLOAT32 * scratch_synth_tool)184 VOID iusace_synthesis_tool_float(FLOAT32 *a, FLOAT32 *x, FLOAT32 *y, WORD32 l, FLOAT32 *mem,
185                                  FLOAT32 *scratch_synth_tool) {
186   FLOAT32 s;
187   FLOAT32 *yy;
188   WORD32 i, j;
189   memcpy(scratch_synth_tool, mem, ORDER * sizeof(FLOAT32));
190   yy = &scratch_synth_tool[ORDER];
191   for (i = 0; i < l; i++) {
192     s = x[i];
193     for (j = 1; j <= ORDER; j += 4) {
194       s -= a[j] * yy[i - j];
195       s -= a[j + 1] * yy[i - (j + 1)];
196       s -= a[j + 2] * yy[i - (j + 2)];
197       s -= a[j + 3] * yy[i - (j + 3)];
198     }
199     yy[i] = s;
200     y[i] = s;
201   }
202 }
203 
iusace_compute_lp_residual(FLOAT32 * a,FLOAT32 * x,FLOAT32 * y,WORD32 l)204 VOID iusace_compute_lp_residual(FLOAT32 *a, FLOAT32 *x, FLOAT32 *y, WORD32 l) {
205   FLOAT32 s;
206   WORD32 i;
207   for (i = 0; i < l; i++) {
208     s = x[i];
209     s += a[1] * x[i - 1];
210     s += a[2] * x[i - 2];
211     s += a[3] * x[i - 3];
212     s += a[4] * x[i - 4];
213     s += a[5] * x[i - 5];
214     s += a[6] * x[i - 6];
215     s += a[7] * x[i - 7];
216     s += a[8] * x[i - 8];
217     s += a[9] * x[i - 9];
218     s += a[10] * x[i - 10];
219     s += a[11] * x[i - 11];
220     s += a[12] * x[i - 12];
221     s += a[13] * x[i - 13];
222     s += a[14] * x[i - 14];
223     s += a[15] * x[i - 15];
224     s += a[16] * x[i - 16];
225     y[i] = s;
226   }
227 }
228 
iusace_convolve(FLOAT32 * signal,FLOAT32 * wsynth_filter_ir,FLOAT32 * conv_out)229 VOID iusace_convolve(FLOAT32 *signal, FLOAT32 *wsynth_filter_ir, FLOAT32 *conv_out) {
230   FLOAT32 temp;
231   WORD32 i, n;
232   for (n = 0; n < LEN_SUBFR; n += 2) {
233     temp = 0.0f;
234     for (i = 0; i <= n; i++) {
235       temp += signal[i] * wsynth_filter_ir[n - i];
236     }
237     conv_out[n] = temp;
238     temp = 0.0f;
239     for (i = 0; i <= (n + 1); i += 2) {
240       temp += signal[i] * wsynth_filter_ir[(n + 1) - i];
241       temp += signal[i + 1] * wsynth_filter_ir[n - i];
242     }
243     conv_out[n + 1] = temp;
244   }
245 }
246 
iusace_autocorr_plus(FLOAT32 * speech,FLOAT32 * auto_corr_vector,WORD32 window_len,const FLOAT32 * lp_analysis_win,FLOAT32 * temp_aut_corr)247 VOID iusace_autocorr_plus(FLOAT32 *speech, FLOAT32 *auto_corr_vector, WORD32 window_len,
248                           const FLOAT32 *lp_analysis_win, FLOAT32 *temp_aut_corr) {
249   FLOAT32 val;
250   WORD16 i, j;
251   for (i = 0; i < window_len; i++) {
252     temp_aut_corr[i] = speech[i] * lp_analysis_win[i];
253   }
254   for (i = 0; i <= ORDER; i++) {
255     val = 0.0f;
256     for (j = 0; j < window_len - i; j++) {
257       val += temp_aut_corr[j] * temp_aut_corr[j + i];
258     }
259     auto_corr_vector[i] = val;
260   }
261   if (auto_corr_vector[0] < 1.0) {
262     auto_corr_vector[0] = 1.0;
263   }
264 }
265 
iusace_get_norm_correlation(FLOAT32 * exc,FLOAT32 * xn,FLOAT32 * wsyn_filt_ir,WORD32 min_interval,WORD32 max_interval,FLOAT32 * norm_corr)266 static VOID iusace_get_norm_correlation(FLOAT32 *exc, FLOAT32 *xn, FLOAT32 *wsyn_filt_ir,
267                                         WORD32 min_interval, WORD32 max_interval,
268                                         FLOAT32 *norm_corr) {
269   WORD32 i, j, k;
270   FLOAT32 filt_prev_exc[LEN_SUBFR];
271   FLOAT32 energy_filt_exc, corr, norm;
272   k = -min_interval;
273 
274   iusace_convolve(&exc[k], wsyn_filt_ir, filt_prev_exc);
275 
276   for (i = min_interval; i <= max_interval; i++) {
277     corr = 0.0F;
278     energy_filt_exc = 0.01F;
279     for (j = 0; j < LEN_SUBFR; j++) {
280       corr += xn[j] * filt_prev_exc[j];
281       energy_filt_exc += filt_prev_exc[j] * filt_prev_exc[j];
282     }
283 
284     norm = (FLOAT32)(1.0f / sqrt(energy_filt_exc));
285     norm_corr[i - min_interval] = corr * norm;
286 
287     if (i != max_interval) {
288       k--;
289       for (j = LEN_SUBFR - 1; j > 0; j--) {
290         filt_prev_exc[j] = filt_prev_exc[j - 1] + exc[k] * wsyn_filt_ir[j];
291       }
292       filt_prev_exc[0] = exc[k];
293     }
294   }
295 }
296 
iusace_corr_interpolate(FLOAT32 * x,WORD32 fraction)297 static FLOAT32 iusace_corr_interpolate(FLOAT32 *x, WORD32 fraction) {
298   FLOAT32 interpol_value, *x1, *x2;
299   const FLOAT32 *p1_interp4_1_table, *p2_interp4_1_table;
300   if (fraction < 0) {
301     fraction += 4;
302     x--;
303   }
304   x1 = &x[0];
305   x2 = &x[1];
306   p1_interp4_1_table = &iusace_interp4_1[fraction];
307   p2_interp4_1_table = &iusace_interp4_1[4 - fraction];
308   interpol_value = x1[0] * p1_interp4_1_table[0] + x2[0] * p2_interp4_1_table[0];
309   interpol_value += x1[-1] * p1_interp4_1_table[4] + x2[1] * p2_interp4_1_table[4];
310   interpol_value += x1[-2] * p1_interp4_1_table[8] + x2[2] * p2_interp4_1_table[8];
311   interpol_value += x1[-3] * p1_interp4_1_table[12] + x2[3] * p2_interp4_1_table[12];
312 
313   return interpol_value;
314 }
315 
iusace_open_loop_search(FLOAT32 * wsp,WORD32 min_pitch_lag,WORD32 max_pitch_lag,WORD32 num_frame,WORD32 * ol_pitch_lag,ia_usac_td_encoder_struct * st)316 VOID iusace_open_loop_search(FLOAT32 *wsp, WORD32 min_pitch_lag, WORD32 max_pitch_lag,
317                              WORD32 num_frame, WORD32 *ol_pitch_lag,
318                              ia_usac_td_encoder_struct *st) {
319   WORD32 i, j, k;
320   FLOAT32 r, corr, energy1, energy2, corr_max = -1.0e23f;
321   const FLOAT32 *p1_ol_cw_table, *p2_ol_cw_table;
322   FLOAT32 *data_a, *data_b, *hp_wsp, *p, *p1;
323 
324   p1_ol_cw_table = &iusace_ol_corr_weight[453];
325   p2_ol_cw_table = &iusace_ol_corr_weight[259 + max_pitch_lag - st->prev_pitch_med];
326   *ol_pitch_lag = 0;
327   for (i = max_pitch_lag; i > min_pitch_lag; i--) {
328     p = &wsp[0];
329     p1 = &wsp[-i];
330     corr = 0.0;
331     for (j = 0; j < num_frame; j += 2) {
332       corr += p[j] * p1[j];
333       corr += p[j + 1] * p1[j + 1];
334     }
335     corr *= *p1_ol_cw_table--;
336     if ((st->prev_pitch_med > 0) && (st->ol_wght_flg == 1)) {
337       corr *= *p2_ol_cw_table--;
338     }
339     if (corr >= corr_max) {
340       corr_max = corr;
341       *ol_pitch_lag = i;
342     }
343   }
344   data_a = st->hp_ol_ltp_mem;
345   data_b = st->hp_ol_ltp_mem + HP_ORDER;
346   hp_wsp = st->prev_hp_wsp + max_pitch_lag;
347   for (k = 0; k < num_frame; k++) {
348     data_b[0] = data_b[1];
349     data_b[1] = data_b[2];
350     data_b[2] = data_b[3];
351     data_b[HP_ORDER] = wsp[k];
352     r = data_b[0] * 0.83787057505665F;
353     r += data_b[1] * -2.50975570071058F;
354     r += data_b[2] * 2.50975570071058F;
355     r += data_b[3] * -0.83787057505665F;
356     r -= data_a[0] * -2.64436711600664F;
357     r -= data_a[1] * 2.35087386625360F;
358     r -= data_a[2] * -0.70001156927424F;
359     data_a[2] = data_a[1];
360     data_a[1] = data_a[0];
361     data_a[0] = r;
362     hp_wsp[k] = r;
363   }
364   p = &hp_wsp[0];
365   p1 = &hp_wsp[-(*ol_pitch_lag)];
366   corr = 0.0F;
367   energy1 = 0.0F;
368   energy2 = 0.0F;
369   for (j = 0; j < num_frame; j++) {
370     energy1 += p1[j] * p1[j];
371     energy2 += p[j] * p[j];
372     corr += p[j] * p1[j];
373   }
374   st->ol_gain = (FLOAT32)(corr / (sqrt(energy1 * energy2) + 1e-5));
375   memmove(st->prev_hp_wsp, &st->prev_hp_wsp[num_frame], max_pitch_lag * sizeof(FLOAT32));
376 }
377 
iusace_get_ol_lag_median(WORD32 prev_ol_lag,WORD32 * prev_ol_lags)378 WORD32 iusace_get_ol_lag_median(WORD32 prev_ol_lag, WORD32 *prev_ol_lags) {
379   WORD32 sorted_ol_lags_out[NUM_OPEN_LOOP_LAGS + 1] = {0};
380   WORD32 i, j, idx, val;
381   WORD32 num_lags = NUM_OPEN_LOOP_LAGS;
382   for (i = NUM_OPEN_LOOP_LAGS - 1; i > 0; i--) {
383     prev_ol_lags[i] = prev_ol_lags[i - 1];
384   }
385   prev_ol_lags[0] = prev_ol_lag;
386   for (i = 0; i < NUM_OPEN_LOOP_LAGS; i++) {
387     sorted_ol_lags_out[i + 1] = prev_ol_lags[i];
388   }
389 
390   idx = (NUM_OPEN_LOOP_LAGS >> 1) + 1;
391   for (;;) {
392     if (idx > 1) {
393       val = sorted_ol_lags_out[--idx];
394     } else {
395       val = sorted_ol_lags_out[num_lags];
396       sorted_ol_lags_out[num_lags] = sorted_ol_lags_out[1];
397       if (--num_lags == 1) {
398         sorted_ol_lags_out[1] = val;
399         break;
400       }
401     }
402     i = idx;
403     j = idx << 1;
404     while (j <= num_lags) {
405       if (j < num_lags && sorted_ol_lags_out[j] < sorted_ol_lags_out[j + 1]) {
406         ++j;
407       }
408       if (val < sorted_ol_lags_out[j]) {
409         sorted_ol_lags_out[i] = sorted_ol_lags_out[j];
410         i = j;
411         j *= 2;
412       } else {
413         j = num_lags + 1;
414       }
415     }
416     sorted_ol_lags_out[i] = val;
417   }
418 
419   return sorted_ol_lags_out[OPEN_LOOP_LAG_MEDIAN];
420 }
421 
iusace_closed_loop_search(FLOAT32 * exc,FLOAT32 * xn,FLOAT32 * wsyn_filt_ir,WORD32 search_range_min,WORD32 search_range_max,WORD32 * pit_frac,WORD32 is_first_subfrm,WORD32 min_pitch_lag_res1_2,WORD32 min_pitch_lag_res_1,WORD32 * pitch_lag_out)422 VOID iusace_closed_loop_search(FLOAT32 *exc, FLOAT32 *xn, FLOAT32 *wsyn_filt_ir,
423                                WORD32 search_range_min, WORD32 search_range_max, WORD32 *pit_frac,
424                                WORD32 is_first_subfrm, WORD32 min_pitch_lag_res1_2,
425                                WORD32 min_pitch_lag_res_1, WORD32 *pitch_lag_out) {
426   WORD32 i, fraction, step;
427   FLOAT32 corr_vector[15 + 2 * LEN_INTERPOL1 + 1] = {0};
428   FLOAT32 corr_max, temp;
429   FLOAT32 *p_norm_corr_vector;
430   WORD32 min_interval, max_interval;
431   min_interval = search_range_min - LEN_INTERPOL1;
432   max_interval = search_range_max + LEN_INTERPOL1;
433   p_norm_corr_vector = &corr_vector[0];
434   iusace_get_norm_correlation(exc, xn, wsyn_filt_ir, min_interval, max_interval,
435                               p_norm_corr_vector);
436 
437   corr_max = p_norm_corr_vector[LEN_INTERPOL1];
438   *pitch_lag_out = search_range_min;
439   for (i = search_range_min + 1; i <= search_range_max; i++) {
440     if (p_norm_corr_vector[i - search_range_min + LEN_INTERPOL1] > corr_max) {
441       corr_max = p_norm_corr_vector[i - search_range_min + LEN_INTERPOL1];
442       *pitch_lag_out = i;
443     }
444   }
445   if ((is_first_subfrm == 0) && (*pitch_lag_out >= min_pitch_lag_res_1)) {
446     *pit_frac = 0;
447   } else {
448     step = 1;
449     fraction = -3;
450     if (((is_first_subfrm == 0) && (*pitch_lag_out >= min_pitch_lag_res1_2)) ||
451         (min_pitch_lag_res1_2 == TMIN)) {
452       step = 2;
453       fraction = -2;
454     }
455     if (*pitch_lag_out == search_range_min) {
456       fraction = 0;
457     }
458     corr_max = iusace_corr_interpolate(
459         &p_norm_corr_vector[(*pitch_lag_out) - search_range_min + LEN_INTERPOL1], fraction);
460     for (i = (fraction + step); i <= 3; i += step) {
461       temp = iusace_corr_interpolate(
462           &p_norm_corr_vector[(*pitch_lag_out) - search_range_min + LEN_INTERPOL1], i);
463       if (temp > corr_max) {
464         corr_max = temp;
465         fraction = i;
466       }
467     }
468     if (fraction < 0) {
469       fraction += 4;
470       (*pitch_lag_out) -= 1;
471     }
472     *pit_frac = fraction;
473   }
474 }
475 
iusace_decim2_fir_filter(FLOAT32 * signal,WORD32 length,FLOAT32 * mem,FLOAT32 * scratch_fir_sig_buf)476 VOID iusace_decim2_fir_filter(FLOAT32 *signal, WORD32 length, FLOAT32 *mem,
477                               FLOAT32 *scratch_fir_sig_buf) {
478   FLOAT32 *sig_buf = scratch_fir_sig_buf;
479   FLOAT32 temp;
480   WORD32 i, j;
481   memcpy(sig_buf, mem, DECIM2_FIR_FILT_MEM_SIZE * sizeof(FLOAT32));
482   memcpy(sig_buf + DECIM2_FIR_FILT_MEM_SIZE, signal, length * sizeof(FLOAT32));
483   for (i = 0; i < DECIM2_FIR_FILT_MEM_SIZE; i++) {
484     mem[i] = ((signal[length - DECIM2_FIR_FILT_MEM_SIZE + i] > 1e-10) ||
485               (signal[length - DECIM2_FIR_FILT_MEM_SIZE + i] < -1e-10))
486                  ? signal[length - DECIM2_FIR_FILT_MEM_SIZE + i]
487                  : 0;
488   }
489   for (i = 0, j = 0; i < length; i += 2, j++) {
490     temp = sig_buf[i] * 0.13F;
491     temp += sig_buf[i + 1] * 0.23F;
492     temp += sig_buf[i + 2] * 0.28F;
493 #ifdef _WIN32
494 #pragma warning(suppress : 6385)
495 #endif
496     temp += sig_buf[i + 3] * 0.23F;
497     temp += sig_buf[i + 4] * 0.13F;
498     signal[j] = temp;
499   }
500 }
501 
iusace_calc_sq_gain(FLOAT32 * x,WORD32 num_bits,WORD32 length,FLOAT32 * scratch_sq_gain_en)502 FLOAT32 iusace_calc_sq_gain(FLOAT32 *x, WORD32 num_bits, WORD32 length,
503                             FLOAT32 *scratch_sq_gain_en) {
504   WORD32 i, j, k;
505   FLOAT32 gain, ener, temp, target, factor, offset;
506   FLOAT32 *en = scratch_sq_gain_en;
507 
508   for (i = 0; i < length; i += 4) {
509     ener = 0.01f;
510     for (j = i; j < i + 4; j++) {
511       ener += x[j] * x[j];
512     }
513 
514     temp = (FLOAT32)log10(ener);
515     en[i / 4] = 9.0f + 10.0f * temp;
516   }
517 
518   target = (6.0f / 4.0f) * (FLOAT32)(num_bits - (length / 16));
519 
520   factor = 128.0f;
521   offset = factor;
522 
523   for (k = 0; k < 10; k++) {
524     factor *= 0.5f;
525     offset -= factor;
526     ener = 0.0f;
527     for (i = 0; i < length / 4; i++) {
528       temp = en[i] - offset;
529 
530       if (temp > 3.0f) {
531         ener += temp;
532       }
533     }
534     if (ener > target) {
535       offset += factor;
536     }
537   }
538 
539   gain = (FLOAT32)pow(10.0f, offset / 20.0f);
540 
541   return (gain);
542 }
543 
iusace_lpc_coef_gen(FLOAT32 * lsf_old,FLOAT32 * lsf_new,FLOAT32 * a,WORD32 nb_subfr,WORD32 m)544 VOID iusace_lpc_coef_gen(FLOAT32 *lsf_old, FLOAT32 *lsf_new, FLOAT32 *a, WORD32 nb_subfr,
545                          WORD32 m) {
546   FLOAT32 lsf[ORDER] = {0}, *ptr_a;
547   FLOAT32 inc, fnew, fold;
548   WORD32 i = 0;
549 
550   ptr_a = a;
551 
552   inc = 1.0f / (FLOAT32)nb_subfr;
553   fnew = 0.5f - (0.5f * inc);
554   fold = 1.0f - fnew;
555   for (i = 0; i < m; i++) {
556     lsf[i] = (lsf_old[i] * fold) + (lsf_new[i] * fnew);
557   }
558   iusace_lsp_to_lp_conversion(lsf, ptr_a);
559   ptr_a += (m + 1);
560   iusace_lsp_to_lp_conversion(lsf_old, ptr_a);
561   ptr_a += (m + 1);
562   iusace_lsp_to_lp_conversion(lsf_new, ptr_a);
563 
564   return;
565 }
566 
iusace_interpolation_lsp_params(FLOAT32 * lsp_old,FLOAT32 * lsp_new,FLOAT32 * lp_flt_coff_a,WORD32 nb_subfr)567 VOID iusace_interpolation_lsp_params(FLOAT32 *lsp_old, FLOAT32 *lsp_new, FLOAT32 *lp_flt_coff_a,
568                                      WORD32 nb_subfr) {
569   FLOAT32 lsp[ORDER];
570   FLOAT32 factor;
571   WORD32 i, k;
572   FLOAT32 x_plus_y, x_minus_y;
573 
574   factor = 1.0f / (FLOAT32)nb_subfr;
575 
576   x_plus_y = 0.5f * factor;
577 
578   for (k = 0; k < nb_subfr; k++) {
579     x_minus_y = 1.0f - x_plus_y;
580     for (i = 0; i < ORDER; i++) {
581       lsp[i] = (lsp_old[i] * x_minus_y) + (lsp_new[i] * x_plus_y);
582     }
583     x_plus_y += factor;
584 
585     iusace_lsp_to_lp_conversion(lsp, lp_flt_coff_a);
586 
587     lp_flt_coff_a += (ORDER + 1);
588   }
589 
590   iusace_lsp_to_lp_conversion(lsp_new, lp_flt_coff_a);
591 
592   return;
593 }
594