xref: /aosp_15_r20/external/libxaac/encoder/iusace_tns_usac.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 <math.h>
22 #include <string.h>
23 #include "ixheaace_mps_common_define.h"
24 #include "ixheaac_constants.h"
25 #include "iusace_cnst.h"
26 #include "ixheaac_type_def.h"
27 #include "iusace_bitbuffer.h"
28 #include "iusace_tns_usac.h"
29 #include "iusace_psy_mod.h"
30 #include "ixheaac_basic_ops32.h"
31 #include "ixheaac_basic_ops40.h"
32 #include "ixheaac_basic_ops.h"
33 #include "ixheaac_error_standards.h"
34 #include "ixheaace_error_codes.h"
35 
36 static const WORD32 iusace_tns_supported_sampling_rates[13] = {
37     96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 0};
38 
39 static const UWORD16 iusace_tns_min_band_number_long[12] = {11, 12, 15, 16, 17, 20,
40                                                             25, 26, 24, 28, 30, 31};
41 
42 static const UWORD16 iusace_tns_min_band_number_short[12] = {2, 2, 2, 3,  3,  4,
43                                                              6, 6, 8, 10, 10, 12};
44 
45 static const WORD32 iusace_tns_max_bands_table[16][2] = {{31, 9},  /**< 96000 */
46                                                          {31, 9},  /**< 88200 */
47                                                          {34, 10}, /**< 64000 */
48                                                          {40, 14}, /**< 48000 */
49                                                          {42, 14}, /**< 44100 */
50                                                          {51, 14}, /**< 32000 */
51                                                          {47, 15}, /**< 24000 */
52                                                          {47, 15}, /**< 22050 */
53                                                          {43, 15}, /**< 16000 */
54                                                          {43, 15}, /**< 12000 */
55                                                          {43, 15}, /**< 11025 */
56                                                          {40, 15}, /**< 8000  */
57                                                          {40, 15}, /**< 7350  */
58                                                          {0, 0},   {0, 0}, {0, 0}};
59 
iusace_freq_to_band_mapping(WORD32 freq,WORD32 sample_rate,WORD32 num_bands,const WORD32 * ptr_band_start_offset)60 static WORD32 iusace_freq_to_band_mapping(WORD32 freq, WORD32 sample_rate, WORD32 num_bands,
61                                           const WORD32 *ptr_band_start_offset) {
62   WORD32 line_num, band;
63 
64   line_num = (freq * ptr_band_start_offset[num_bands] * 4 / sample_rate + 1) / 2;
65 
66   if (line_num >= ptr_band_start_offset[num_bands]) {
67     return num_bands;
68   }
69 
70   for (band = 0; band < num_bands; band++) {
71     if (ptr_band_start_offset[band + 1] > line_num) break;
72   }
73 
74   if (line_num - ptr_band_start_offset[band] > ptr_band_start_offset[band + 1] - line_num) {
75     band++;
76   }
77 
78   return band;
79 };
80 
iusace_calc_gauss_win(FLOAT64 * ptr_win,const WORD32 length,const WORD32 sample_rate,const WORD32 win_seq,const FLOAT32 time_resolution)81 static VOID iusace_calc_gauss_win(FLOAT64 *ptr_win, const WORD32 length, const WORD32 sample_rate,
82                                   const WORD32 win_seq, const FLOAT32 time_resolution) {
83   WORD32 i;
84   FLOAT32 gauss_exp = 3.14159265358979323f * sample_rate * 0.001f * (FLOAT32)time_resolution /
85                       (win_seq != EIGHT_SHORT_SEQUENCE ? 1024.0f : 128.0f);
86 
87   gauss_exp = -0.5f * gauss_exp * gauss_exp;
88 
89   for (i = 0; i < length; i++) {
90     ptr_win[i] = (FLOAT32)exp(gauss_exp * (i + 0.5) * (i + 0.5));
91   }
92   return;
93 }
94 
iusace_tns_init(WORD32 sampling_rate,WORD32 bit_rate,ia_tns_info * tns_info,WORD32 num_channels)95 IA_ERRORCODE iusace_tns_init(WORD32 sampling_rate, WORD32 bit_rate, ia_tns_info *tns_info,
96                              WORD32 num_channels) {
97   IA_ERRORCODE err_code = IA_NO_ERROR;
98   WORD32 fs_index = 0;
99   WORD32 lpc_stop_freq = 16000;
100   WORD32 lpc_start_freq_long = 2500, lpc_start_freq_short = 3750;
101   tns_info->threshold = 1.41f;
102   tns_info->tns_time_res_short = 0.6f;
103   tns_info->tns_time_res_long = 0.6f;
104 
105   if (sampling_rate == 14700) {
106     sampling_rate = 16000;
107   }
108   if (sampling_rate == 29400) {
109     sampling_rate = 32000;
110   }
111 
112   if (bit_rate < 32000) {
113     if (num_channels == 1) {
114       tns_info->threshold = 1.2f;
115       lpc_start_freq_long = 2000;
116     }
117   } else if (bit_rate < 36000) {
118     if (num_channels == 1) {
119       tns_info->tns_time_res_long = 0.8f;
120     } else {
121       tns_info->tns_time_res_long = 0.5f;
122     }
123     tns_info->tns_time_res_short = 0.3f;
124   } else {
125     tns_info->tns_time_res_long = 0.5f;
126     tns_info->tns_time_res_short = 0.3f;
127   }
128 
129   /** Determine if sampling rate is supported
130    */
131   while (sampling_rate != iusace_tns_supported_sampling_rates[fs_index]) {
132     if (!iusace_tns_supported_sampling_rates[fs_index]) {
133       return IA_EXHEAACE_INIT_FATAL_USAC_INVALID_CORE_SAMPLE_RATE;
134     }
135     fs_index++;
136   }
137 
138   tns_info->tns_max_bands_long = iusace_tns_max_bands_table[fs_index][0];
139   tns_info->tns_max_bands_short = iusace_tns_max_bands_table[fs_index][1];
140   tns_info->tns_max_order_long = 15;
141   tns_info->tns_max_order_short = 7;
142 
143   tns_info->tns_min_band_number_long = iusace_tns_min_band_number_long[fs_index];
144   tns_info->tns_min_band_number_short = iusace_tns_min_band_number_short[fs_index];
145 
146   tns_info->lpc_start_band_long =
147       iusace_freq_to_band_mapping(lpc_start_freq_long, sampling_rate, tns_info->max_sfb_long,
148                                   tns_info->sfb_offset_table_long);
149 
150   tns_info->lpc_start_band_short =
151       iusace_freq_to_band_mapping(lpc_start_freq_short, sampling_rate, tns_info->max_sfb_short,
152                                   tns_info->sfb_offset_table_short);
153 
154   tns_info->lpc_stop_band_long = iusace_freq_to_band_mapping(
155       lpc_stop_freq, sampling_rate, tns_info->max_sfb_long, tns_info->sfb_offset_table_long);
156 
157   tns_info->lpc_stop_band_short = iusace_freq_to_band_mapping(
158       lpc_stop_freq, sampling_rate, tns_info->max_sfb_short, tns_info->sfb_offset_table_short);
159 
160   iusace_calc_gauss_win(tns_info->win_long, tns_info->tns_max_order_long + 1, sampling_rate,
161                         ONLY_LONG_SEQUENCE, tns_info->tns_time_res_long);
162 
163   iusace_calc_gauss_win(tns_info->win_short, tns_info->tns_max_order_short + 1, sampling_rate,
164                         EIGHT_SHORT_SEQUENCE, tns_info->tns_time_res_short);
165   return err_code;
166 }
167 
iusace_tns_filter(WORD32 length,FLOAT64 * spec,ia_tns_filter_data * filter,FLOAT64 * scratch_tns_filter)168 VOID iusace_tns_filter(WORD32 length, FLOAT64 *spec, ia_tns_filter_data *filter,
169                        FLOAT64 *scratch_tns_filter) {
170   WORD32 i, j, k = 0;
171   WORD32 order = filter->order;
172   FLOAT64 *a = filter->a_coeffs;
173   FLOAT64 *temp = scratch_tns_filter;
174 
175   /** Determine loop parameters for given direction
176    */
177   if (filter->direction) {
178     /** Startup, initial state is zero
179      */
180     temp[length - 1] = spec[length - 1];
181     for (i = length - 2; i > (length - 1 - order); i--) {
182       temp[i] = spec[i];
183       k++;
184       for (j = 1; j <= k; j++) {
185         spec[i] += temp[i + j] * a[j];
186       }
187     }
188 
189     /** Now filter the rest
190      */
191     for (i = length - 1 - order; i >= 0; i--) {
192       temp[i] = spec[i];
193       for (j = 1; j <= order; j++) {
194         spec[i] += temp[i + j] * a[j];
195       }
196     }
197   } else {
198     /** Startup, initial state is zero
199      */
200     temp[0] = spec[0];
201     for (i = 1; i < order; i++) {
202       temp[i] = spec[i];
203       for (j = 1; j <= i; j++) {
204         spec[i] += temp[i - j] * a[j];
205       }
206     }
207 
208     /** Now filter the rest
209      */
210     for (i = order; i < length; i++) {
211       temp[i] = spec[i];
212       for (j = 1; j <= order; j++) {
213         spec[i] += temp[i - j] * a[j];
214       }
215     }
216   }
217 
218   return;
219 }
220 
iusace_truncate_coeffs(WORD32 f_order,FLOAT64 threshold,FLOAT64 * k_array)221 static WORD32 iusace_truncate_coeffs(WORD32 f_order, FLOAT64 threshold, FLOAT64 *k_array) {
222   WORD32 i;
223   for (i = f_order; i >= 0; i--) {
224     k_array[i] = (fabs(k_array[i]) > threshold) ? k_array[i] : 0.0;
225     if (k_array[i] != 0.0) {
226       return i;
227     }
228   }
229   return 0;
230 }
231 
iusace_quantize_reflection_coeffs(WORD32 f_order,WORD32 coeff_res,FLOAT64 * k_array,WORD32 * index_array)232 VOID iusace_quantize_reflection_coeffs(WORD32 f_order, WORD32 coeff_res, FLOAT64 *k_array,
233                                        WORD32 *index_array) {
234   FLOAT64 iqfac, iqfac_m;
235   WORD32 i;
236 
237   iqfac = (((SIZE_T)1 << (coeff_res - 1)) - 0.5) / (PI / 2);
238   iqfac_m = (((SIZE_T)1 << (coeff_res - 1)) + 0.5) / (PI / 2);
239 
240   /* Quantize and inverse quantize */
241   for (i = 1; i <= f_order; i++) {
242     index_array[i] = (WORD32)(0.5 + (asin(k_array[i]) * ((k_array[i] >= 0) ? iqfac : iqfac_m)));
243     k_array[i] = sin((FLOAT64)index_array[i] / ((index_array[i] >= 0) ? iqfac : iqfac_m));
244   }
245   return;
246 }
247 
iusace_tns_auto_corr(WORD32 max_order,WORD32 data_size,FLOAT64 * data,FLOAT64 * r_array)248 VOID iusace_tns_auto_corr(WORD32 max_order, WORD32 data_size, FLOAT64 *data, FLOAT64 *r_array) {
249   WORD32 i, j;
250   FLOAT64 tmp_var;
251   for (i = 0; i < data_size; i += 2) {
252     const FLOAT64 *input1 = &data[i];
253     FLOAT64 temp1 = *input1;
254     FLOAT64 temp2 = *(input1 + 1);
255     FLOAT64 inp_tmp1 = *input1++;
256     for (j = 0; j <= max_order; j++) {
257       FLOAT64 inp_tmp2;
258       tmp_var = temp1 * inp_tmp1;
259       inp_tmp2 = *input1++;
260       tmp_var += temp2 * inp_tmp2;
261       r_array[j] += tmp_var;
262       j++;
263       tmp_var = temp1 * inp_tmp2;
264       inp_tmp1 = *input1++;
265       tmp_var += temp2 * inp_tmp1;
266       r_array[j] += tmp_var;
267     }
268   }
269   return;
270 }
271 
iusace_levinson_durbin(WORD32 order,WORD32 data_size,FLOAT64 * ptr_data,FLOAT64 * ptr_k,FLOAT64 * ptr_win,FLOAT64 * ptr_scratch)272 static FLOAT64 iusace_levinson_durbin(WORD32 order, WORD32 data_size, FLOAT64 *ptr_data,
273                                       FLOAT64 *ptr_k, FLOAT64 *ptr_win, FLOAT64 *ptr_scratch) {
274   WORD32 i, j;
275   FLOAT64 *ptr_work_buffer_temp;
276   FLOAT64 *ptr_work_buffer = ptr_scratch;
277   FLOAT64 *ptr_input = ptr_scratch + TNS_MAX_ORDER + 1;
278   memset(ptr_input, 0, (TNS_MAX_ORDER + 1) * sizeof(ptr_input[0]));
279   iusace_tns_auto_corr(order, data_size, ptr_data, ptr_input);
280 
281   WORD32 num_of_coeff = order;
282   FLOAT64 *ptr_refl_coeff = ptr_k;
283   ptr_k[0] = 1.0;
284 
285   if (ptr_input[0] == 0) {
286     return 0;
287   }
288 
289   for (i = 0; i < num_of_coeff + 1; i++) {
290     ptr_input[i] = ptr_input[i] * ptr_win[i];
291   }
292 
293   FLOAT64 tmp_var;
294   ptr_work_buffer[0] = ptr_input[0];
295 
296   for (i = 1; i < num_of_coeff; i++) {
297     tmp_var = ptr_input[i];
298     ptr_work_buffer[i] = tmp_var;
299     ptr_work_buffer[i + num_of_coeff - 1] = tmp_var;
300   }
301   ptr_work_buffer[i + num_of_coeff - 1] = ptr_input[i];
302 
303   for (i = 0; i < num_of_coeff; i++) {
304     FLOAT64 refc, tmp;
305     tmp = ptr_work_buffer[num_of_coeff + i];
306     if (tmp < 0) {
307       tmp = -tmp;
308     } else {
309       if (ptr_work_buffer[0] < tmp) {
310         break;
311       }
312     }
313     if (ptr_work_buffer[0] == 0) {
314       refc = 0;
315     } else {
316       refc = tmp / ptr_work_buffer[0];
317     }
318 
319     if (ptr_work_buffer[num_of_coeff + i] > 0) {
320       refc = -refc;
321     }
322     ptr_refl_coeff[i + 1] = refc;
323     ptr_work_buffer_temp = &(ptr_work_buffer[num_of_coeff]);
324 
325     for (j = i; j < num_of_coeff; j++) {
326       FLOAT64 accu1, accu2;
327       accu1 = refc * ptr_work_buffer[j - i];
328       accu1 += ptr_work_buffer_temp[j];
329       accu2 = refc * ptr_work_buffer_temp[j];
330       accu2 += ptr_work_buffer[j - i];
331       ptr_work_buffer_temp[j] = accu1;
332       ptr_work_buffer[j - i] = accu2;
333     }
334   }
335   return (ptr_input[0] / ptr_work_buffer[0]);
336 }
337 
iusace_step_up(WORD32 f_order,FLOAT64 * ptr_k,FLOAT64 * ptr_a,FLOAT64 * ptr_scratch)338 static VOID iusace_step_up(WORD32 f_order, FLOAT64 *ptr_k, FLOAT64 *ptr_a, FLOAT64 *ptr_scratch) {
339   FLOAT64 *ptr_a_temp = ptr_scratch;
340   WORD32 i, order;
341 
342   ptr_a[0] = 1.0;
343   ptr_a_temp[0] = 1.0;
344   for (order = 1; order <= f_order; order++) {
345     ptr_a[order] = 0.0;
346     for (i = 1; i <= order; i++) {
347       ptr_a_temp[i] = ptr_a[i] + ptr_k[order] * ptr_a[order - i];
348     }
349     for (i = 1; i <= order; i++) {
350       ptr_a[i] = ptr_a_temp[i];
351     }
352   }
353   return;
354 }
355 
iusace_calc_weighted_spec(FLOAT64 * ptr_spec,FLOAT64 * ptr_wgt_spec,FLOAT32 * ptr_sfb_en,WORD32 * ptr_sfb_offset,WORD32 lpc_start_band,WORD32 lpc_stop_band,FLOAT64 * ptr_scratch)356 static VOID iusace_calc_weighted_spec(FLOAT64 *ptr_spec, FLOAT64 *ptr_wgt_spec,
357                                       FLOAT32 *ptr_sfb_en, WORD32 *ptr_sfb_offset,
358                                       WORD32 lpc_start_band, WORD32 lpc_stop_band,
359                                       FLOAT64 *ptr_scratch) {
360   WORD32 i, sfb;
361   FLOAT32 temp;
362   FLOAT32 *ptr_tns_sfb_mean = (FLOAT32 *)ptr_scratch;
363   memset(ptr_scratch, 0, MAX_NUM_GROUPED_SFB * sizeof(ptr_tns_sfb_mean[0]));
364   WORD32 lpc_stop_line = ptr_sfb_offset[lpc_stop_band];
365   WORD32 lpc_start_line = ptr_sfb_offset[lpc_start_band];
366 
367   for (sfb = lpc_start_band; sfb < lpc_stop_band; sfb++) {
368     ptr_tns_sfb_mean[sfb] = (FLOAT32)(1.0 / sqrt(ptr_sfb_en[sfb] + 1e-30f));
369   }
370 
371   sfb = lpc_start_band;
372   temp = ptr_tns_sfb_mean[sfb];
373 
374   for (i = lpc_start_line; i < lpc_stop_line; i++) {
375     if (ptr_sfb_offset[sfb + 1] == i) {
376       sfb++;
377 
378       if (sfb + 1 < lpc_stop_band) {
379         temp = ptr_tns_sfb_mean[sfb];
380       }
381     }
382     ptr_wgt_spec[i] = temp;
383   }
384 
385   for (i = lpc_stop_line - 2; i >= lpc_start_line; i--) {
386     ptr_wgt_spec[i] = (ptr_wgt_spec[i] + ptr_wgt_spec[i + 1]) * 0.5f;
387   }
388 
389   for (i = lpc_start_line + 1; i < lpc_stop_line; i++) {
390     ptr_wgt_spec[i] = (ptr_wgt_spec[i] + ptr_wgt_spec[i - 1]) * 0.5f;
391   }
392 
393   for (i = lpc_start_line; i < lpc_stop_line; i++) {
394     ptr_wgt_spec[i] = ptr_wgt_spec[i] * ptr_spec[i];
395   }
396   return;
397 }
398 
iusace_tns_data_sync(ia_tns_info * ptr_tns_dest,ia_tns_info * ptr_tns_src,const WORD32 w,WORD32 order)399 VOID iusace_tns_data_sync(ia_tns_info *ptr_tns_dest, ia_tns_info *ptr_tns_src, const WORD32 w,
400                           WORD32 order) {
401   ia_tns_window_data *win_data_src = &ptr_tns_src->window_data[w];
402   ia_tns_window_data *win_data_dest = &ptr_tns_dest->window_data[w];
403   WORD32 i;
404   if (fabs(win_data_dest->tns_pred_gain - win_data_src->tns_pred_gain) <
405       ((FLOAT32)0.03f * win_data_dest->tns_pred_gain)) {
406     win_data_dest->tns_active = win_data_src->tns_active;
407 
408     for (i = 0; i < order; i++) {
409       win_data_dest->tns_filter->k_coeffs[i] = win_data_src->tns_filter->k_coeffs[i];
410     }
411   }
412   return;
413 }
414 
iusace_tns_encode(ia_tns_info * pstr_tns_info_ch2,ia_tns_info * pstr_tns_info,FLOAT32 * ptr_sfb_energy,WORD32 w,WORD32 i_ch,WORD32 low_pass_line,FLOAT64 * ptr_scratch_tns_filter,WORD32 core_mode,FLOAT64 * ptr_tns_scratch)415 VOID iusace_tns_encode(ia_tns_info *pstr_tns_info_ch2, ia_tns_info *pstr_tns_info,
416                        FLOAT32 *ptr_sfb_energy, WORD32 w, WORD32 i_ch, WORD32 low_pass_line,
417                        FLOAT64 *ptr_scratch_tns_filter, WORD32 core_mode,
418                        FLOAT64 *ptr_tns_scratch) {
419   WORD32 number_of_bands = pstr_tns_info->number_of_bands;
420   WORD32 block_type = pstr_tns_info->block_type;
421   FLOAT64 *ptr_spec = pstr_tns_info->spec;
422   WORD32 start_band, stop_band, order; /**< bands over which to apply TNS */
423   WORD32 length_in_bands;              /**< Length to filter, in bands */
424   WORD32 start_index, length;
425   WORD32 nbands;
426   WORD32 coeff_res;
427   FLOAT64 *ptr_weighted_spec = ptr_tns_scratch;
428   memset(ptr_weighted_spec, 0, 4096 * sizeof(ptr_weighted_spec[0]));
429   FLOAT64 *ptr_scratch = ptr_tns_scratch + 4096;
430   FLOAT64 *ptr_window = NULL;
431   WORD32 lpc_start_band, lpc_stop_band;
432   WORD32 *ptr_sfb_offset_table;
433 
434   switch (block_type) {
435     case EIGHT_SHORT_SEQUENCE:
436       start_band = pstr_tns_info->tns_min_band_number_short;
437       stop_band = number_of_bands;
438       length_in_bands = stop_band - start_band;
439       order = pstr_tns_info->tns_max_order_short;
440       start_band = MIN(start_band, pstr_tns_info->tns_max_bands_short);
441       stop_band = MIN(stop_band, pstr_tns_info->tns_max_bands_short);
442       coeff_res = 3;
443       ptr_window = pstr_tns_info->win_short;
444       nbands = pstr_tns_info->max_sfb_short;
445       lpc_start_band = pstr_tns_info->lpc_start_band_short;
446       lpc_stop_band = pstr_tns_info->lpc_stop_band_short;
447       if (core_mode == CORE_MODE_FD) {
448         ptr_sfb_offset_table = pstr_tns_info->sfb_offset_table_short;
449       } else {
450         ptr_sfb_offset_table = pstr_tns_info->sfb_offset_table_short_tcx;
451       }
452       break;
453 
454     default:
455       start_band = pstr_tns_info->tns_min_band_number_long;
456       stop_band = number_of_bands;
457       length_in_bands = stop_band - start_band;
458       order = pstr_tns_info->tns_max_order_long;
459       start_band = MIN(start_band, pstr_tns_info->tns_max_bands_long);
460       stop_band = MIN(stop_band, pstr_tns_info->tns_max_bands_long);
461       coeff_res = 4;
462       ptr_window = pstr_tns_info->win_long;
463       nbands = pstr_tns_info->max_sfb_long;
464       lpc_start_band = pstr_tns_info->lpc_start_band_long;
465       lpc_stop_band = pstr_tns_info->lpc_stop_band_long;
466       ptr_sfb_offset_table = pstr_tns_info->sfb_offset_table_long;
467       break;
468   }
469 
470   /** Make sure that start and stop bands < max_sfb
471    * Make sure that start and stop bands >= 0
472    */
473   start_band = MIN(start_band, nbands);
474   stop_band = MIN(stop_band, nbands);
475   start_band = MAX(start_band, 0);
476   stop_band = MAX(stop_band, 0);
477 
478   pstr_tns_info->tns_data_present = 0; /**< default TNS not used */
479 
480   /** Perform analysis and filtering for each window
481    */
482   {
483     ia_tns_window_data *window_data = &pstr_tns_info->window_data[w];
484     ia_tns_filter_data *tns_filter = window_data->tns_filter;
485     FLOAT64 *k = tns_filter->k_coeffs; /**< reflection coeffs */
486     FLOAT64 *a = tns_filter->a_coeffs; /**< prediction coeffs */
487 
488     iusace_calc_weighted_spec(ptr_spec, ptr_weighted_spec, ptr_sfb_energy, ptr_sfb_offset_table,
489                               lpc_start_band, lpc_stop_band, ptr_scratch);
490 
491     window_data->n_filt = 0;
492     window_data->coef_res = coeff_res;
493 
494     start_index = ptr_sfb_offset_table[lpc_start_band];
495     length =
496         ptr_sfb_offset_table[lpc_stop_band] -
497         ptr_sfb_offset_table[lpc_start_band]; /**< The length of the spectral data to be
498                                                                                          processed
499                                                */
500 
501     window_data->tns_pred_gain = iusace_levinson_durbin(
502         order, length, &ptr_weighted_spec[start_index], k, ptr_window, ptr_scratch);
503 
504     window_data->tns_active = 0;
505     if (window_data->tns_pred_gain > DEF_TNS_GAIN_THRESH) {
506       window_data->tns_active = 1;
507     }
508 
509     if (i_ch == 1) {
510       iusace_tns_data_sync(pstr_tns_info, pstr_tns_info_ch2, w, order);
511     }
512 
513     if (window_data->tns_pred_gain > DEF_TNS_GAIN_THRESH) {
514       /** Use TNS
515        */
516       WORD32 truncated_order;
517       window_data->n_filt++;
518       pstr_tns_info->tns_data_present = 1;
519       tns_filter->direction = 0;
520       tns_filter->coef_compress = 0;
521       tns_filter->length = length_in_bands;
522       iusace_quantize_reflection_coeffs(order, coeff_res, k, tns_filter->index);
523       truncated_order = iusace_truncate_coeffs(order, DEF_TNS_COEFF_THRESH, k);
524       tns_filter->order = truncated_order;
525       iusace_step_up(truncated_order, k, a, ptr_scratch); /**< Compute prediction coefficients */
526       start_index = ptr_sfb_offset_table[start_band];
527       length = MIN(ptr_sfb_offset_table[stop_band], low_pass_line) - start_index;
528       if (block_type == EIGHT_SHORT_SEQUENCE) {
529         length = ptr_sfb_offset_table[stop_band] - start_index;
530       }
531       iusace_tns_filter(length, &ptr_spec[start_index], tns_filter,
532                         ptr_scratch_tns_filter); /**< filter */
533     }
534   }
535   return;
536 }
537