• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <limits.h>
24 
25 #include "ixheaac_type_def.h"
26 #include "ixheaac_constants.h"
27 #include "ixheaace_aac_constants.h"
28 #include "ixheaac_error_standards.h"
29 #include "ixheaace_error_codes.h"
30 #include "ixheaac_basic_ops32.h"
31 #include "ixheaac_basic_ops16.h"
32 #include "ixheaac_basic_ops40.h"
33 #include "ixheaac_basic_ops.h"
34 
35 #include "ixheaace_sbr_header.h"
36 #include "ixheaace_sbr_def.h"
37 #include "ixheaace_resampler.h"
38 
39 #include "ixheaace_sbr_rom.h"
40 #include "ixheaace_common_rom.h"
41 #include "ixheaace_sbr_hbe.h"
42 #include "ixheaace_sbr_qmf_enc.h"
43 #include "ixheaace_sbr_tran_det.h"
44 #include "ixheaace_sbr_frame_info_gen.h"
45 #include "ixheaace_sbr_env_est.h"
46 #include "ixheaace_sbr_code_envelope.h"
47 #include "ixheaace_sbr_main.h"
48 #include "ixheaace_sbr_missing_harmonics_det.h"
49 #include "ixheaace_sbr_inv_filtering_estimation.h"
50 #include "ixheaace_sbr_noise_floor_est.h"
51 
52 #include "ixheaace_common_rom.h"
53 #include "ixheaace_sbr_ton_corr.h"
54 #include "iusace_esbr_pvc.h"
55 #include "iusace_esbr_inter_tes.h"
56 #include "ixheaace_sbr.h"
57 
58 #include "ixheaace_bitbuffer.h"
59 
60 #include "ixheaace_sbr_cmondata.h"
61 #include "ixheaace_sbr_write_bitstream.h"
62 
63 #include "ixheaace_sbr_hybrid.h"
64 #include "ixheaace_sbr_ps_enc.h"
65 
66 #include "ixheaace_common_utils.h"
67 
68 #include "ixheaace_sbr_header.h"
69 #include "ixheaace_sbr_def.h"
70 #include "ixheaace_resampler.h"
71 #include "ixheaace_sbr_rom.h"
72 #include "ixheaace_common_rom.h"
73 #include "ixheaace_sbr_hbe.h"
74 #include "ixheaace_sbr_qmf_enc.h"
75 #include "ixheaace_sbr_tran_det.h"
76 #include "ixheaace_sbr_frame_info_gen.h"
77 #include "ixheaace_sbr_env_est.h"
78 #include "ixheaace_sbr_code_envelope.h"
79 #include "ixheaace_psy_const.h"
80 #include "ixheaace_tns.h"
81 #include "ixheaace_tns_params.h"
82 #include "ixheaace_rom.h"
83 #include "ixheaace_common_rom.h"
84 #include "ixheaace_bitbuffer.h"
85 
86 #include "ixheaace_sbr_main.h"
87 #include "ixheaace_common_rom.h"
88 #include "ixheaace_sbr_missing_harmonics_det.h"
89 #include "ixheaace_sbr_inv_filtering_estimation.h"
90 #include "ixheaace_sbr_noise_floor_est.h"
91 #include "ixheaace_sbr_ton_corr.h"
92 #include "iusace_esbr_pvc.h"
93 #include "ixheaace_sbr.h"
94 
95 #include "ixheaace_sbr_freq_scaling.h"
96 
97 #include "ixheaace_bitbuffer.h"
98 
99 #include "ixheaace_sbr_hybrid.h"
100 #include "ixheaace_sbr_ps_enc.h"
101 
102 #include "ixheaace_sbr_crc.h"
103 #include "ixheaace_sbr_cmondata.h"
104 #include "ixheaace_sbr_enc_struct.h"
105 #include "ixheaace_sbr_write_bitstream.h"
106 
107 #include "ixheaace_common_utils.h"
108 
ixheaace_map_panorama(WORD32 nrg_val,WORD32 amp_res,WORD32 * ptr_quant_error)109 static WORD32 ixheaace_map_panorama(WORD32 nrg_val, WORD32 amp_res, WORD32 *ptr_quant_error) {
110   WORD32 i = 0;
111   ;
112   WORD32 min_val, val;
113   WORD32 pan_tab[2][10] = {{0, 2, 4, 6, 8, 12, 16, 20, 24}, {0, 2, 4, 8, 12}};
114   WORD32 max_index[2] = {9, 5};
115 
116   WORD32 pan_index;
117   WORD32 sign;
118 
119   sign = nrg_val > 0 ? 1 : -1;
120 
121   nrg_val = sign * nrg_val;
122 
123   min_val = INT_MAX;
124   pan_index = 0;
125 
126   while (i < max_index[amp_res]) {
127     val = ixheaac_abs32(nrg_val - pan_tab[amp_res][i]);
128     if (val < min_val) {
129       min_val = val;
130       pan_index = i;
131     }
132     i++;
133   }
134 
135   *ptr_quant_error = min_val;
136 
137   return pan_tab[amp_res][max_index[amp_res] - 1] + sign * pan_tab[amp_res][pan_index];
138 }
139 
ixheaace_sbr_noise_floor_levels_quantisation(WORD32 * ptr_noise_levels,FLOAT32 * ptr_flt_noise_levels,WORD32 coupling)140 static VOID ixheaace_sbr_noise_floor_levels_quantisation(WORD32 *ptr_noise_levels,
141                                                          FLOAT32 *ptr_flt_noise_levels,
142                                                          WORD32 coupling) {
143   WORD32 i = 0;
144   WORD32 dummy;
145 
146   while (i < MAXIMUM_NUM_NOISE_VALUES) {
147     WORD32 tmp;
148 
149     tmp = ptr_flt_noise_levels[i] > 30.0f ? 30 : (WORD32)(ptr_flt_noise_levels[i] + 0.5f);
150 
151     if (coupling) {
152       tmp = tmp < -30 ? -30 : tmp;
153       tmp = ixheaace_map_panorama(tmp, 1, &dummy);
154     }
155     ptr_noise_levels[i] = tmp;
156 
157     i++;
158   }
159 }
160 
ixheaace_couple_noise_floor(FLOAT32 * ptr_noise_lvl_left,FLOAT32 * ptr_noise_lvl_right)161 static VOID ixheaace_couple_noise_floor(FLOAT32 *ptr_noise_lvl_left,
162                                         FLOAT32 *ptr_noise_lvl_right) {
163   WORD32 i = 0;
164 
165   while (i < MAXIMUM_NUM_NOISE_VALUES) {
166     FLOAT32 pow_left, pow_right;
167 
168     pow_left = (FLOAT32)pow(2.0f, (SBR_NOISE_FLOOR_OFFSET - ptr_noise_lvl_left[i]));
169     pow_right = (FLOAT32)pow(2.0f, (SBR_NOISE_FLOOR_OFFSET - ptr_noise_lvl_right[i]));
170 
171     ptr_noise_lvl_right[i] -= ptr_noise_lvl_left[i];
172     ptr_noise_lvl_left[i] =
173         (FLOAT32)(SBR_NOISE_FLOOR_OFFSET - log((pow_left * pow_right) / 2) * SBR_INV_LOG_2);
174     i++;
175   }
176 }
177 
ixheaace_calculate_sbr_envelope(FLOAT32 ** ptr_y_buf_left,FLOAT32 ** ptr_y_buf_right,const ixheaace_str_frame_info_sbr * pstr_const_frame_info,WORD32 * ptr_sfb_ene_l,WORD32 * ptr_sfb_ene_r,ixheaace_pstr_sbr_config_data pstr_sbr_cfg,ixheaace_pstr_enc_channel pstr_sbr,ixheaace_sbr_stereo_mode stereo_mode,WORD32 * ptr_max_quant_err)178 static IA_ERRORCODE ixheaace_calculate_sbr_envelope(
179     FLOAT32 **ptr_y_buf_left, FLOAT32 **ptr_y_buf_right,
180     const ixheaace_str_frame_info_sbr *pstr_const_frame_info, WORD32 *ptr_sfb_ene_l,
181     WORD32 *ptr_sfb_ene_r, ixheaace_pstr_sbr_config_data pstr_sbr_cfg,
182     ixheaace_pstr_enc_channel pstr_sbr, ixheaace_sbr_stereo_mode stereo_mode,
183     WORD32 *ptr_max_quant_err) {
184   WORD32 i, j, k, l, count, m = 0;
185   WORD32 num_bands, start_pos, stop_pos, li, ui;
186   ixheaace_freq_res freq_res;
187 
188   WORD32 ca = 2 - pstr_sbr->enc_env_data.init_sbr_amp_res;
189   WORD32 n_envelopes = pstr_const_frame_info->n_envelopes;
190   WORD32 short_env = pstr_const_frame_info->short_env - 1;
191   WORD32 time_step = pstr_sbr->str_sbr_extract_env.time_step;
192   WORD32 missing_harmonic = 0;
193 
194   if ((ca != 1) && (ca != 2)) {
195     return IA_EXHEAACE_EXE_FATAL_SBR_INVALID_AMP_RES;
196   }
197 
198   if (stereo_mode == SBR_COUPLING) {
199     *ptr_max_quant_err = 0;
200   }
201 
202   i = 0;
203   while (i < n_envelopes) {
204     start_pos = pstr_const_frame_info->borders[i];
205     stop_pos = pstr_const_frame_info->borders[i + 1];
206     freq_res = pstr_const_frame_info->freq_res[i];
207     num_bands = pstr_sbr_cfg->num_scf[freq_res];
208 
209     if (i == short_env) {
210       if (pstr_sbr_cfg->is_ld_sbr) {
211         WORD32 temp = 2;
212         if (temp < time_step) {
213           temp = time_step;
214         }
215         if (stop_pos - start_pos > temp) {
216           stop_pos = stop_pos - temp;
217         }
218       } else {
219         stop_pos = stop_pos - 1;
220       }
221     }
222     for (j = 0; j < num_bands; j++) {
223       FLOAT32 energy_left = 0, energy_right = 0, tmp_ene_l = 0, tmp_ene_r = 0;
224       li = pstr_sbr_cfg->ptr_freq_band_tab[freq_res][j];
225       ui = pstr_sbr_cfg->ptr_freq_band_tab[freq_res][j + 1];
226 
227       if ((freq_res == FREQ_RES_HIGH) && (j == 0 && ui - li > 1)) {
228         li++;
229       } else {
230         if (j == 0 && ui - li > 2) {
231           li++;
232         }
233       }
234 
235       missing_harmonic = 0;
236 
237       if (pstr_sbr->enc_env_data.add_harmonic_flag) {
238         if (freq_res == FREQ_RES_HIGH) {
239           if (pstr_sbr->enc_env_data.add_harmonic[j]) {
240             missing_harmonic = 1;
241           }
242         } else {
243           WORD32 band;
244           WORD32 start_band_high = 0;
245           WORD32 stop_band_high = 0;
246 
247           while (pstr_sbr_cfg->ptr_freq_band_tab[FREQ_RES_HIGH][start_band_high] <
248                  pstr_sbr_cfg->ptr_freq_band_tab[FREQ_RES_LOW][j]) {
249             start_band_high++;
250           }
251 
252           while (pstr_sbr_cfg->ptr_freq_band_tab[FREQ_RES_HIGH][stop_band_high] <
253                  pstr_sbr_cfg->ptr_freq_band_tab[FREQ_RES_LOW][j + 1]) {
254             stop_band_high++;
255           }
256 
257           for (band = start_band_high; band < stop_band_high; band++) {
258             if (pstr_sbr->enc_env_data.add_harmonic[band]) {
259               missing_harmonic = 1;
260             }
261           }
262         }
263       }
264 
265       if (missing_harmonic) {
266         count = stop_pos - start_pos;
267         for (l = start_pos; l < stop_pos; l++) {
268           if (pstr_sbr_cfg->is_ld_sbr) {
269             energy_left += ptr_y_buf_left[l >> 1][li];
270           } else {
271             energy_left += ptr_y_buf_left[l][li];
272           }
273         }
274 
275         k = li + 1;
276         while (k < ui) {
277           tmp_ene_l = 0.0f;
278           for (l = start_pos; l < stop_pos; l++) {
279             if (pstr_sbr_cfg->is_ld_sbr) {
280               tmp_ene_l += ptr_y_buf_left[l >> 1][k];
281             } else {
282               tmp_ene_l += ptr_y_buf_left[l][k];
283             }
284           }
285 
286           if (tmp_ene_l > energy_left) {
287             energy_left = tmp_ene_l;
288           }
289           k++;
290         }
291 
292         if (ui - li > 2) {
293           energy_left = energy_left * 0.398107267f;
294         } else {
295           if (ui - li > 1) {
296             energy_left = energy_left * 0.5f;
297           }
298         }
299 
300         if (stereo_mode == SBR_COUPLING) {
301           for (l = start_pos; l < stop_pos; l++) {
302             if (pstr_sbr_cfg->is_ld_sbr) {
303               energy_right += ptr_y_buf_right[l >> 1][li];
304             } else {
305               energy_right += ptr_y_buf_right[l][li];
306             }
307           }
308 
309           k = li + 1;
310           while (k < ui) {
311             tmp_ene_r = 0.0f;
312             for (l = start_pos; l < stop_pos; l++) {
313               if (pstr_sbr_cfg->is_ld_sbr) {
314                 tmp_ene_r += ptr_y_buf_right[l >> 1][k];
315               } else {
316                 tmp_ene_r += ptr_y_buf_right[l][k];
317               }
318             }
319 
320             if (tmp_ene_r > energy_right) {
321               energy_right = tmp_ene_r;
322             }
323             k++;
324           }
325 
326           if (ui - li > 2) {
327             energy_right = energy_right * 0.398107267f;
328           } else {
329             if (ui - li > 1) {
330               energy_right = energy_right * 0.5f;
331             }
332           }
333           tmp_ene_l = energy_left;
334           energy_left = (energy_left + energy_right) * 0.5f;
335           energy_right = ((tmp_ene_l * time_step) + 1) / ((energy_right * time_step) + 1);
336         }
337       } else {
338         count = (stop_pos - start_pos) * (ui - li);
339 
340         k = li;
341         while (k < ui) {
342           for (l = start_pos; l < stop_pos; l++) {
343             energy_left += ptr_y_buf_left[l][k];
344           }
345           k++;
346         }
347 
348         if (stereo_mode == SBR_COUPLING) {
349           k = li;
350           while (k < ui) {
351             for (l = start_pos; l < stop_pos; l++) {
352               if (pstr_sbr_cfg->is_ld_sbr) {
353                 energy_right += ptr_y_buf_right[l >> 1][k];
354               } else {
355                 energy_right += ptr_y_buf_right[l][k];
356               }
357             }
358             k++;
359           }
360           tmp_ene_l = energy_left;
361           energy_left = (energy_left + energy_right) * 0.5f;
362           energy_right = ((tmp_ene_l * time_step) + 1) / ((energy_right * time_step) + 1);
363         }
364       }
365 
366       energy_left = (FLOAT32)(log((energy_left / (count * 64)) + EPS) * SBR_INV_LOG_2);
367 
368       if (energy_left < 0.0f) {
369         energy_left = 0.0f;
370       }
371 
372       ptr_sfb_ene_l[m] = (WORD32)(ca * energy_left + 0.5);
373 
374       if (stereo_mode == SBR_COUPLING) {
375         WORD32 quant_err;
376         energy_right = (FLOAT32)(log(energy_right) * SBR_INV_LOG_2);
377         ptr_sfb_ene_r[m] =
378             ixheaace_map_panorama((WORD32)((FLOAT32)ca * energy_right),
379                                   pstr_sbr->enc_env_data.init_sbr_amp_res, &quant_err);
380         if (quant_err > *ptr_max_quant_err) {
381           *ptr_max_quant_err = quant_err;
382         }
383       }
384       m++;
385     }
386 
387     if (pstr_sbr_cfg->use_parametric_coding) {
388       m -= num_bands;
389 
390       for (j = 0; j < num_bands; j++) {
391         if (freq_res == FREQ_RES_HIGH && pstr_sbr->str_sbr_extract_env.envelope_compensation[j]) {
392           ptr_sfb_ene_l[m] -=
393               (WORD32)(ca *
394                        ixheaac_abs32(pstr_sbr->str_sbr_extract_env.envelope_compensation[j]));
395         }
396 
397         if (ptr_sfb_ene_l[m] < 0) {
398           ptr_sfb_ene_l[m] = 0;
399         }
400         m++;
401       }
402     }
403     i++;
404   }
405   return IA_NO_ERROR;
406 }
ixheaace_get_pitch_bin_deint(FLOAT32 * ptr_fft_data_real,FLOAT32 * ptr_fft_data_im,const WORD32 * ptr_sfb_tab,WORD32 is_4_1)407 static WORD32 ixheaace_get_pitch_bin_deint(FLOAT32 *ptr_fft_data_real, FLOAT32 *ptr_fft_data_im,
408                                            const WORD32 *ptr_sfb_tab, WORD32 is_4_1) {
409   WORD32 i, j = 0, k = 0;
410   WORD32 bin = -1;
411   FLOAT32 tmp, prev_val = 0.0f;
412 
413   while (ptr_sfb_tab[j] != -1) {
414     WORD32 size = ptr_sfb_tab[j];
415     tmp = 0;
416 
417     for (i = 0; i < size; i++) {
418       tmp += ptr_fft_data_real[k / 2] * ptr_fft_data_real[k / 2];
419       tmp += ptr_fft_data_im[k / 2] * ptr_fft_data_im[k / 2];
420       k += 2;
421     }
422 
423     tmp = (FLOAT32)log(max(MIN_FLT_VAL, (tmp / (FLOAT32)size)));
424     if (j != 0) {
425       if (fabs(tmp - prev_val) >= 3.0f) {
426         if (1 == is_4_1) {
427           bin = ((k - (ptr_sfb_tab[j] * 2)) * 3) / 8;
428         } else {
429           bin = ((k - (ptr_sfb_tab[j] * 2)) * 3) / 4;
430         }
431         if (bin > 127) {
432           bin = -1;
433         }
434         break;
435       }
436     }
437     prev_val = tmp;
438     j++;
439   }
440 
441   return bin;
442 }
ixheaace_get_pitch_bin(FLOAT32 * fft_data,const WORD32 * ptr_sfb_tab,WORD32 is_4_1)443 static WORD32 ixheaace_get_pitch_bin(FLOAT32 *fft_data, const WORD32 *ptr_sfb_tab,
444                                      WORD32 is_4_1) {
445   WORD32 i, j = 0, k = 0;
446   WORD32 bin = -1;
447   FLOAT32 tmp, prev_val = 0;
448 
449   while (ptr_sfb_tab[j] != -1) {
450     WORD32 size = ptr_sfb_tab[j];
451     tmp = 0;
452 
453     for (i = 0; i < size; i++) {
454       tmp += fft_data[k] * fft_data[k];
455       tmp += fft_data[k + 1] * fft_data[k + 1];
456       k += 2;
457     }
458 
459     tmp = (FLOAT32)log(MAX(MIN_FLT_VAL, (tmp / (FLOAT32)size)));
460     if (j != 0) {
461       if (fabs(tmp - prev_val) >= 3.0f) {
462         if (1 == is_4_1) {
463           bin = ((k - (ptr_sfb_tab[j] * 2)) * 3) / 8;
464         } else {
465           bin = ((k - (ptr_sfb_tab[j] * 2)) * 3) / 4;
466         }
467         if (bin > 127) {
468           bin = -1;
469         }
470         break;
471       }
472     }
473     prev_val = tmp;
474     j++;
475   }
476 
477   return bin;
478 }
ixheaace_hbe_get_pitch_bins(FLOAT32 * ptr_time_in,ixheaace_pstr_sbr_config_data pstr_sbr_cfg,FLOAT32 * ptr_esbr_scr,ixheaace_str_sbr_tabs * ptr_sbr_tab,WORD32 time_sn_stride,WORD32 num_samples,WORD32 * bin1,WORD32 * bin2)479 static IA_ERRORCODE ixheaace_hbe_get_pitch_bins(FLOAT32 *ptr_time_in,
480                                                 ixheaace_pstr_sbr_config_data pstr_sbr_cfg,
481                                                 FLOAT32 *ptr_esbr_scr,
482                                                 ixheaace_str_sbr_tabs *ptr_sbr_tab,
483                                                 WORD32 time_sn_stride, WORD32 num_samples,
484                                                 WORD32 *bin1, WORD32 *bin2) {
485   const WORD32 *ptr_sbr_table = NULL;
486   FLOAT32 *ptr_esbr_inp = ptr_esbr_scr;
487   ptr_esbr_scr += num_samples * 2;
488   FLOAT32 *ptr_esbr_inp_i = ptr_esbr_inp + num_samples;
489   FLOAT32 *ptr_scratch_fft = ptr_esbr_scr;
490   WORD32 idx, sf, is_4_1 = 0;
491 
492   sf = pstr_sbr_cfg->sample_freq;
493   if (IXHEAACE_MAX_NUM_SAMPLES == num_samples) {
494     sf = sf >> 1;
495     is_4_1 = 1;
496   }
497 
498   switch (sf) {
499     case 16000:
500       ptr_sbr_table = ptr_sbr_tab->ptr_esbr_sfb_tab->sfb_bins_8k;
501       break;
502     case 22050:
503       ptr_sbr_table = ptr_sbr_tab->ptr_esbr_sfb_tab->sfb_bins_11k;
504       break;
505     case 24000:
506       ptr_sbr_table = ptr_sbr_tab->ptr_esbr_sfb_tab->sfb_bins_12k;
507       break;
508     case 32000:
509       ptr_sbr_table = ptr_sbr_tab->ptr_esbr_sfb_tab->sfb_bins_16k;
510       break;
511     case 44100:
512       ptr_sbr_table = ptr_sbr_tab->ptr_esbr_sfb_tab->sfb_bins_22k;
513       break;
514     case 48000:
515       ptr_sbr_table = ptr_sbr_tab->ptr_esbr_sfb_tab->sfb_bins_24k;
516       break;
517     default:
518       return IA_EXHEAACE_EXE_FATAL_SBR_INVALID_SAMP_FREQ;
519   }
520   if (1 == pstr_sbr_cfg->num_ch) {
521     if (num_samples == 2048) {
522       for (idx = 0; idx < num_samples; idx += 2) {
523         ptr_esbr_inp[idx] = ptr_time_in[time_sn_stride * idx];
524         ptr_esbr_inp[idx + 1] = 0;
525         ptr_esbr_inp[num_samples + idx] = ptr_time_in[time_sn_stride * (idx + 1)];
526         ptr_esbr_inp[num_samples + idx + 1] = 0;
527       }
528       iusace_complex_fft_2048(ptr_esbr_inp, ptr_scratch_fft);
529       *bin1 = ixheaace_get_pitch_bin(ptr_esbr_inp, ptr_sbr_table, is_4_1);
530     } else if (num_samples == IXHEAACE_MAX_NUM_SAMPLES) {
531       memset(ptr_esbr_inp_i, 0, num_samples * sizeof(ptr_esbr_inp_i[0]));
532       for (idx = 0; idx < num_samples; idx += 2) {
533         ptr_esbr_inp[idx / 2] = ptr_time_in[time_sn_stride * idx];
534         ptr_esbr_inp[(num_samples + idx) / 2] = ptr_time_in[time_sn_stride * (idx + 1)];
535       }
536       iusace_complex_fft_4096(ptr_esbr_inp, ptr_esbr_inp_i, ptr_scratch_fft);
537       *bin1 = ixheaace_get_pitch_bin_deint(ptr_esbr_inp, ptr_esbr_inp_i, ptr_sbr_table, is_4_1);
538     }
539   } else {
540     if (num_samples == 2048) {
541       for (idx = 0; idx < num_samples; idx += 2) {
542         ptr_esbr_inp[idx] = ptr_time_in[2 * idx];
543         ptr_esbr_inp[idx + 1] = 0;
544         ptr_esbr_inp[num_samples + idx] = ptr_time_in[2 * idx + 2];
545         ptr_esbr_inp[num_samples + idx + 1] = 0;
546       }
547       iusace_complex_fft_2048(ptr_esbr_inp, ptr_scratch_fft);
548       *bin1 = ixheaace_get_pitch_bin(ptr_esbr_inp, ptr_sbr_table, is_4_1);
549 
550       for (idx = 0; idx < num_samples; idx += 2) {
551         ptr_esbr_inp[idx] = ptr_time_in[2 * idx + 1];
552         ptr_esbr_inp[idx + 1] = 0;
553         ptr_esbr_inp[num_samples + idx] = ptr_time_in[2 * idx + 3];
554         ptr_esbr_inp[num_samples + idx + 1] = 0;
555       }
556       iusace_complex_fft_2048(ptr_esbr_inp, ptr_scratch_fft);
557       *bin2 = ixheaace_get_pitch_bin(ptr_esbr_inp, ptr_sbr_table, is_4_1);
558     } else if (num_samples == IXHEAACE_MAX_NUM_SAMPLES) {
559       memset(ptr_esbr_inp_i, 0, num_samples * sizeof(ptr_esbr_inp_i[0]));
560       for (idx = 0; idx < num_samples; idx += 2) {
561         ptr_esbr_inp[idx / 2] = ptr_time_in[2 * idx];
562         ptr_esbr_inp[(num_samples + idx) / 2] = ptr_time_in[2 * idx + 2];
563       }
564       iusace_complex_fft_4096(ptr_esbr_inp, ptr_esbr_inp_i, ptr_scratch_fft);
565       *bin1 = ixheaace_get_pitch_bin_deint(ptr_esbr_inp, ptr_esbr_inp_i, ptr_sbr_table, is_4_1);
566 
567       memset(ptr_esbr_inp_i, 0, num_samples * sizeof(ptr_esbr_inp_i[0]));
568       for (idx = 0; idx < num_samples; idx += 2) {
569         ptr_esbr_inp[idx / 2] = ptr_time_in[2 * idx + 1];
570         ptr_esbr_inp[(num_samples + idx) / 2] = ptr_time_in[2 * idx + 3];
571       }
572       iusace_complex_fft_4096(ptr_esbr_inp, ptr_esbr_inp_i, ptr_scratch_fft);
573       *bin2 = ixheaace_get_pitch_bin_deint(ptr_esbr_inp, ptr_esbr_inp_i, ptr_sbr_table, is_4_1);
574     }
575   }
576   return IA_NO_ERROR;
577 }
ixheaace_update_esbr_ext_data(FLOAT32 * ptr_time_in,ixheaace_pstr_sbr_config_data pstr_sbr_cfg,FLOAT32 * ptr_esbr_scr,ixheaace_str_esbr_bs_data * pstr_esbr,WORD32 transient_info[][3],ixheaace_str_sbr_tabs * ptr_sbr_tab,WORD32 coupling,WORD32 time_sn_stride,WORD32 num_samples)578 static IA_ERRORCODE ixheaace_update_esbr_ext_data(
579     FLOAT32 *ptr_time_in, ixheaace_pstr_sbr_config_data pstr_sbr_cfg, FLOAT32 *ptr_esbr_scr,
580     ixheaace_str_esbr_bs_data *pstr_esbr, WORD32 transient_info[][3],
581     ixheaace_str_sbr_tabs *ptr_sbr_tab, WORD32 coupling, WORD32 time_sn_stride,
582     WORD32 num_samples) {
583   WORD32 bin, bin1;
584   const WORD32 *ptr_sbr_table = NULL;
585   FLOAT32 *ptr_esbr_inp = ptr_esbr_scr;
586   ptr_esbr_scr += num_samples * 2;
587   FLOAT32 *ptr_scratch_fft = ptr_esbr_scr;
588   WORD32 idx;
589   switch (pstr_sbr_cfg->sample_freq) {
590     case 16000:
591       ptr_sbr_table = ptr_sbr_tab->ptr_esbr_sfb_tab->sfb_bins_8k;
592       break;
593     case 22050:
594       ptr_sbr_table = ptr_sbr_tab->ptr_esbr_sfb_tab->sfb_bins_11k;
595       break;
596     case 24000:
597       ptr_sbr_table = ptr_sbr_tab->ptr_esbr_sfb_tab->sfb_bins_12k;
598       break;
599     case 32000:
600       ptr_sbr_table = ptr_sbr_tab->ptr_esbr_sfb_tab->sfb_bins_16k;
601       break;
602     case 44100:
603       ptr_sbr_table = ptr_sbr_tab->ptr_esbr_sfb_tab->sfb_bins_22k;
604       break;
605     case 48000:
606       ptr_sbr_table = ptr_sbr_tab->ptr_esbr_sfb_tab->sfb_bins_24k;
607       break;
608     default:
609       return IA_EXHEAACE_EXE_FATAL_SBR_INVALID_SAMP_FREQ;
610   }
611   if (1 == pstr_sbr_cfg->num_ch) {
612     for (idx = 0; idx < num_samples; idx += 2) {
613       ptr_esbr_inp[idx] = ptr_time_in[time_sn_stride * idx];
614       ptr_esbr_inp[idx + 1] = 0;
615       ptr_esbr_inp[num_samples + idx] = ptr_time_in[time_sn_stride * (idx + 1)];
616       ptr_esbr_inp[num_samples + idx + 1] = 0;
617     }
618     iusace_complex_fft_2048(ptr_esbr_inp, ptr_scratch_fft);
619     bin = ixheaace_get_pitch_bin(ptr_esbr_inp, ptr_sbr_table, 0);
620     pstr_esbr->sbr_num_chan = 1;
621     if (transient_info[0][1] != 0) {
622       pstr_esbr->sbr_preprocessing = 1;
623     } else {
624       pstr_esbr->sbr_preprocessing = 0;
625     }
626 
627     if (transient_info[0][1] != 0 && bin != -1) {
628       pstr_esbr->sbr_oversampling_flag[0] = 1;
629       pstr_esbr->sbr_patching_mode[0] = 0;
630       pstr_esbr->sbr_pitchin_flags[0] = 1;
631       pstr_esbr->sbr_pitchin_bins[0] = (UWORD8)MIN(bin, 127);
632     } else if (bin != -1) {
633       pstr_esbr->sbr_oversampling_flag[0] = 0;
634       pstr_esbr->sbr_patching_mode[0] = 0;
635       pstr_esbr->sbr_pitchin_flags[0] = 1;
636       pstr_esbr->sbr_pitchin_bins[0] = (UWORD8)MIN(bin, 127);
637     } else if (transient_info[0][1] != 0) {
638       pstr_esbr->sbr_oversampling_flag[0] = 1;
639       pstr_esbr->sbr_patching_mode[0] = 0;
640       pstr_esbr->sbr_pitchin_flags[0] = 0;
641     } else {
642       pstr_esbr->sbr_patching_mode[0] = 1;
643     }
644   } else {
645     pstr_esbr->sbr_num_chan = 2;
646     pstr_esbr->sbr_coupling = coupling;
647     for (idx = 0; idx < num_samples; idx += 2) {
648       ptr_esbr_inp[idx] = ptr_time_in[2 * idx];
649       ptr_esbr_inp[idx + 1] = 0;
650       ptr_esbr_inp[num_samples + idx] = ptr_time_in[2 * idx + 2];
651       ptr_esbr_inp[num_samples + idx + 1] = 0;
652     }
653     iusace_complex_fft_2048(ptr_esbr_inp, ptr_scratch_fft);
654     bin = ixheaace_get_pitch_bin(ptr_esbr_inp, ptr_sbr_table, 0);
655     for (idx = 0; idx < num_samples; idx += 2) {
656       ptr_esbr_inp[idx] = ptr_time_in[2 * idx + 1];
657       ptr_esbr_inp[idx + 1] = 0;
658       ptr_esbr_inp[num_samples + idx] = ptr_time_in[2 * idx + 3];
659       ptr_esbr_inp[num_samples + idx + 1] = 0;
660     }
661     iusace_complex_fft_2048(ptr_esbr_inp, ptr_scratch_fft);
662     bin1 = ixheaace_get_pitch_bin(ptr_esbr_inp, ptr_sbr_table, 0);
663 
664     if (coupling) {
665       if ((transient_info[0][1] != 0 || transient_info[1][1] != 0)) {
666         pstr_esbr->sbr_preprocessing = 1;
667       } else {
668         pstr_esbr->sbr_preprocessing = 0;
669       }
670       if ((transient_info[0][1] != 0 || transient_info[1][1] != 0) && bin != -1) {
671         pstr_esbr->sbr_oversampling_flag[0] = 1;
672         pstr_esbr->sbr_patching_mode[0] = 0;
673         pstr_esbr->sbr_pitchin_flags[0] = 1;
674         bin = MIN(bin, bin1);
675         pstr_esbr->sbr_pitchin_bins[0] = (UWORD8)MIN(bin, 127);
676       } else if (bin != -1) {
677         pstr_esbr->sbr_oversampling_flag[0] = 0;
678         pstr_esbr->sbr_patching_mode[0] = 0;
679         pstr_esbr->sbr_pitchin_flags[0] = 1;
680         bin = MIN(bin, bin1);
681         pstr_esbr->sbr_pitchin_bins[0] = (UWORD8)MIN(bin, 127);
682       } else if ((transient_info[0][1] != 0 || transient_info[1][1] != 0)) {
683         pstr_esbr->sbr_oversampling_flag[0] = 1;
684         pstr_esbr->sbr_patching_mode[0] = 0;
685         pstr_esbr->sbr_pitchin_flags[0] = 0;
686       } else {
687         pstr_esbr->sbr_patching_mode[0] = 1;
688       }
689     } else {
690       pstr_esbr->sbr_preprocessing = 0;
691       if ((transient_info[0][1] != 0 || transient_info[1][1] != 0)) {
692         pstr_esbr->sbr_preprocessing = 1;
693       }
694 
695       if (transient_info[0][1] != 0 && bin != -1) {
696         pstr_esbr->sbr_oversampling_flag[0] = 1;
697         pstr_esbr->sbr_patching_mode[0] = 0;
698         pstr_esbr->sbr_pitchin_flags[0] = 1;
699         bin = MIN(bin, bin1);
700         pstr_esbr->sbr_pitchin_bins[0] = (UWORD8)MIN(bin, 127);
701       } else if (bin != -1) {
702         pstr_esbr->sbr_oversampling_flag[0] = 0;
703         pstr_esbr->sbr_patching_mode[0] = 0;
704         pstr_esbr->sbr_pitchin_flags[0] = 1;
705         pstr_esbr->sbr_pitchin_bins[0] = (UWORD8)MIN(bin, 127);
706       } else if (transient_info[0][1] != 0) {
707         pstr_esbr->sbr_oversampling_flag[0] = 1;
708         pstr_esbr->sbr_patching_mode[0] = 0;
709         pstr_esbr->sbr_pitchin_flags[0] = 0;
710       } else {
711         pstr_esbr->sbr_patching_mode[0] = 1;
712       }
713 
714       if (transient_info[1][1] != 0 && bin1 != -1) {
715         pstr_esbr->sbr_oversampling_flag[1] = 1;
716         pstr_esbr->sbr_patching_mode[1] = 0;
717         pstr_esbr->sbr_pitchin_flags[1] = 1;
718         pstr_esbr->sbr_pitchin_bins[1] = (UWORD8)MIN(bin1, 127);
719       } else if (bin1 != -1) {
720         pstr_esbr->sbr_oversampling_flag[1] = 0;
721         pstr_esbr->sbr_patching_mode[1] = 0;
722         pstr_esbr->sbr_pitchin_flags[1] = 1;
723         pstr_esbr->sbr_pitchin_bins[1] = (UWORD8)MIN(bin1, 127);
724       } else if (transient_info[1][1] != 0) {
725         pstr_esbr->sbr_oversampling_flag[1] = 1;
726         pstr_esbr->sbr_patching_mode[1] = 0;
727         pstr_esbr->sbr_pitchin_flags[1] = 0;
728       } else {
729         pstr_esbr->sbr_patching_mode[1] = 1;
730       }
731     }
732   }
733   return IA_NO_ERROR;
734 }
735 
ixheaace_update_harmonic_sbr_data(WORD32 transient_info[][3],WORD32 coupling,ixheaace_pstr_enc_channel * pstr_enc_ch,WORD32 num_channels)736 static VOID ixheaace_update_harmonic_sbr_data(
737     WORD32 transient_info[][3], WORD32 coupling,
738     ixheaace_pstr_enc_channel *pstr_enc_ch, WORD32 num_channels) {
739   WORD32 bin, bin1;
740   struct ixheaace_str_sbr_env_data *pstr_sbr_env_left = NULL;
741   struct ixheaace_str_sbr_env_data *pstr_sbr_env_right = NULL;
742   if (1 == num_channels) {
743     pstr_sbr_env_left = &pstr_enc_ch[0]->enc_env_data;
744     bin = pstr_sbr_env_left->sbr_pitchin_bins;
745     if (transient_info[0][1] != 0) {
746       pstr_sbr_env_left->sbr_preprocessing = 1;
747     } else {
748       pstr_sbr_env_left->sbr_preprocessing = 0;
749     }
750 
751     if (transient_info[0][1] != 0 && bin != -1) {
752       pstr_sbr_env_left->sbr_oversampling_flag = 1;
753       pstr_sbr_env_left->sbr_patching_mode = 0;
754       pstr_sbr_env_left->sbr_pitchin_bins_flag = 1;
755       pstr_sbr_env_left->sbr_pitchin_bins = min(bin, 127);
756     } else if (bin != -1) {
757       pstr_sbr_env_left->sbr_oversampling_flag = 0;
758       pstr_sbr_env_left->sbr_patching_mode = 0;
759       pstr_sbr_env_left->sbr_pitchin_bins_flag = 1;
760       pstr_sbr_env_left->sbr_pitchin_bins = min(bin, 127);
761     } else if (transient_info[0][1] != 0) {
762       pstr_sbr_env_left->sbr_oversampling_flag = 1;
763       pstr_sbr_env_left->sbr_patching_mode = 0;
764       pstr_sbr_env_left->sbr_pitchin_bins = 0;
765     } else {
766       pstr_sbr_env_left->sbr_patching_mode = 1;
767     }
768   } else {
769     pstr_sbr_env_left = &pstr_enc_ch[0]->enc_env_data;
770     pstr_sbr_env_right = &pstr_enc_ch[1]->enc_env_data;
771     pstr_sbr_env_left->sbr_coupling = coupling;
772     pstr_sbr_env_right->sbr_coupling = coupling;
773     bin = pstr_sbr_env_left->sbr_pitchin_bins;
774 
775     bin1 = pstr_sbr_env_right->sbr_pitchin_bins;
776 
777     if (coupling) {
778       pstr_sbr_env_right->sbr_preprocessing = 1;
779       if ((transient_info[0][1] != 0 || transient_info[1][1] != 0)) {
780         pstr_sbr_env_left->sbr_preprocessing = 1;
781       } else {
782         pstr_sbr_env_left->sbr_preprocessing = 0;
783       }
784       if ((transient_info[0][1] != 0 || transient_info[1][1] != 0) && bin != -1) {
785         pstr_sbr_env_left->sbr_oversampling_flag = 1;
786         pstr_sbr_env_left->sbr_patching_mode = 0;
787         pstr_sbr_env_left->sbr_pitchin_bins_flag = 1;
788         bin = min(bin, bin1);
789         pstr_sbr_env_left->sbr_pitchin_bins = min(bin, 127);
790       } else if (bin != -1) {
791         pstr_sbr_env_left->sbr_oversampling_flag = 0;
792         pstr_sbr_env_left->sbr_patching_mode = 0;
793         pstr_sbr_env_left->sbr_pitchin_bins_flag = 1;
794         bin = min(bin, bin1);
795         pstr_sbr_env_left->sbr_pitchin_bins = min(bin, 127);
796       } else if ((transient_info[0][1] != 0 || transient_info[1][1] != 0)) {
797         pstr_sbr_env_left->sbr_oversampling_flag = 1;
798         pstr_sbr_env_left->sbr_patching_mode = 0;
799         pstr_sbr_env_left->sbr_pitchin_bins_flag = 0;
800       } else {
801         pstr_sbr_env_left->sbr_patching_mode = 1;
802       }
803     } else {
804       pstr_sbr_env_left->sbr_preprocessing = 0;
805       pstr_sbr_env_right->sbr_preprocessing = 0;
806       if ((transient_info[0][1] != 0 || transient_info[1][1] != 0)) {
807         pstr_sbr_env_left->sbr_preprocessing = 1;
808         pstr_sbr_env_right->sbr_preprocessing = 1;
809       }
810 
811       if (transient_info[0][1] != 0 && bin != -1) {
812         pstr_sbr_env_left->sbr_oversampling_flag = 1;
813         pstr_sbr_env_left->sbr_patching_mode = 0;
814         pstr_sbr_env_left->sbr_pitchin_bins_flag = 1;
815         bin = min(bin, bin1);
816         pstr_sbr_env_left->sbr_pitchin_bins = min(bin, 127);
817       } else if (bin != -1) {
818         pstr_sbr_env_left->sbr_oversampling_flag = 0;
819         pstr_sbr_env_left->sbr_patching_mode = 0;
820         pstr_sbr_env_left->sbr_pitchin_bins_flag = 1;
821         pstr_sbr_env_left->sbr_pitchin_bins = min(bin, 127);
822       } else if (transient_info[0][1] != 0) {
823         pstr_sbr_env_left->sbr_oversampling_flag = 1;
824         pstr_sbr_env_left->sbr_patching_mode = 0;
825         pstr_sbr_env_left->sbr_pitchin_bins_flag = 0;
826       } else {
827         pstr_sbr_env_left->sbr_patching_mode = 1;
828       }
829 
830       if (transient_info[1][1] != 0 && bin1 != -1) {
831         pstr_sbr_env_right->sbr_oversampling_flag = 1;
832         pstr_sbr_env_right->sbr_patching_mode = 0;
833         pstr_sbr_env_right->sbr_pitchin_bins_flag = 1;
834         pstr_sbr_env_right->sbr_pitchin_bins = bin1 < 127 ? bin1 : 127;
835       } else if (bin1 != -1) {
836         pstr_sbr_env_right->sbr_oversampling_flag = 0;
837         pstr_sbr_env_right->sbr_patching_mode = 0;
838         pstr_sbr_env_right->sbr_pitchin_bins_flag = 1;
839         pstr_sbr_env_right->sbr_pitchin_bins = bin1 < 127 ? bin1 : 127;
840       } else if (transient_info[1][1] != 0) {
841         pstr_sbr_env_right->sbr_oversampling_flag = 1;
842         pstr_sbr_env_right->sbr_patching_mode = 0;
843         pstr_sbr_env_right->sbr_pitchin_bins_flag = 0;
844       } else {
845         pstr_sbr_env_right->sbr_patching_mode = 1;
846       }
847     }
848   }
849 }
850 
ixheaace_esbr_qmf_init(ia_sbr_qmf_filter_bank_struct * pstr_codec_qmf_bank,WORD32 sbr_ratio_idx,WORD32 output_frame_size)851 VOID ixheaace_esbr_qmf_init(ia_sbr_qmf_filter_bank_struct *pstr_codec_qmf_bank,
852                             WORD32 sbr_ratio_idx, WORD32 output_frame_size) {
853   pstr_codec_qmf_bank->pstr_qmf_dec_tabs =
854       (ixheaace_str_qmf_dec_tabs_struct *)&ixheaace_str_aac_qmf_tabs;
855   memset(
856       pstr_codec_qmf_bank->anal_filter_states_32, 0,
857       sizeof(pstr_codec_qmf_bank->anal_filter_states_32[0]) * IXHEAACE_QMF_FILTER_STATE_ANA_SIZE);
858   pstr_codec_qmf_bank->num_time_slots = (WORD16)(output_frame_size / 64);
859   pstr_codec_qmf_bank->ptr_filter_pos_32 = ixheaace_str_aac_qmf_tabs.esbr_qmf_c;
860   pstr_codec_qmf_bank->ptr_state_new_samples_pos_low_32 =
861       pstr_codec_qmf_bank->anal_filter_states_32;
862   pstr_codec_qmf_bank->ptr_ana_win_coeff_32 = ixheaace_str_aac_qmf_tabs.esbr_qmf_c;
863 
864   switch (sbr_ratio_idx) {
865     case USAC_SBR_RATIO_INDEX_2_1:
866       pstr_codec_qmf_bank->no_channels = 32;
867       pstr_codec_qmf_bank->ptr_esbr_cos_twiddle =
868           ixheaace_str_aac_qmf_tabs.esbr_sin_cos_twiddle_l32;
869       pstr_codec_qmf_bank->ptr_esbr_alt_sin_twiddle =
870           ixheaace_str_aac_qmf_tabs.esbr_alt_sin_twiddle_l32;
871       pstr_codec_qmf_bank->ptr_esbr_t_cos = ixheaace_str_aac_qmf_tabs.esbr_t_cos_sin_l32;
872       break;
873     case USAC_SBR_RATIO_INDEX_8_3:
874       pstr_codec_qmf_bank->no_channels = 24;
875       pstr_codec_qmf_bank->ptr_filter_pos_32 = ixheaace_str_aac_qmf_tabs.esbr_qmf_c_24;
876       pstr_codec_qmf_bank->ptr_ana_win_coeff_32 = ixheaace_str_aac_qmf_tabs.esbr_qmf_c_24;
877       pstr_codec_qmf_bank->ptr_esbr_cos_twiddle =
878           ixheaace_str_aac_qmf_tabs.esbr_sin_cos_twiddle_l24;
879       pstr_codec_qmf_bank->ptr_esbr_alt_sin_twiddle =
880           ixheaace_str_aac_qmf_tabs.esbr_alt_sin_twiddle_l24;
881       pstr_codec_qmf_bank->ptr_esbr_t_cos = ixheaace_str_aac_qmf_tabs.esbr_t_cos_sin_l24;
882       break;
883     case USAC_SBR_RATIO_INDEX_4_1:
884       pstr_codec_qmf_bank->no_channels = 16;
885       pstr_codec_qmf_bank->ptr_esbr_cos_twiddle =
886           ixheaace_str_aac_qmf_tabs.esbr_sin_cos_twiddle_l16;
887       pstr_codec_qmf_bank->ptr_esbr_alt_sin_twiddle =
888           ixheaace_str_aac_qmf_tabs.esbr_alt_sin_twiddle_l16;
889       pstr_codec_qmf_bank->ptr_esbr_t_cos = ixheaace_str_aac_qmf_tabs.esbr_t_cos_sin_l16;
890       break;
891     default:
892       pstr_codec_qmf_bank->no_channels = 32;
893       pstr_codec_qmf_bank->ptr_esbr_cos_twiddle =
894           ixheaace_str_aac_qmf_tabs.esbr_sin_cos_twiddle_l32;
895       pstr_codec_qmf_bank->ptr_esbr_alt_sin_twiddle =
896           ixheaace_str_aac_qmf_tabs.esbr_alt_sin_twiddle_l32;
897       pstr_codec_qmf_bank->ptr_esbr_t_cos = ixheaace_str_aac_qmf_tabs.esbr_t_cos_sin_l32;
898       break;
899   }
900 }
ixheaace_esbr_qmfanal32_winadd(FLOAT32 * ptr_inp1,FLOAT32 * ptr_inp2,FLOAT32 * ptr_qmf1,FLOAT32 * ptr_qmf2,FLOAT32 * ptr_out,WORD32 num_band_anal_qmf)901 VOID ixheaace_esbr_qmfanal32_winadd(FLOAT32 *ptr_inp1, FLOAT32 *ptr_inp2, FLOAT32 *ptr_qmf1,
902                                     FLOAT32 *ptr_qmf2, FLOAT32 *ptr_out,
903                                     WORD32 num_band_anal_qmf) {
904   WORD32 n;
905   FLOAT32 accu;
906 
907   switch (num_band_anal_qmf) {
908     case 32: {
909       n = 0;
910       while (n < num_band_anal_qmf) {
911         accu = (ptr_inp1[n + 0] * ptr_qmf1[2 * (n + 0)]);
912         accu += (ptr_inp1[n + 2 * num_band_anal_qmf] * ptr_qmf1[2 * (n + 2 * num_band_anal_qmf)]);
913         accu += (ptr_inp1[n + 4 * num_band_anal_qmf] * ptr_qmf1[2 * (n + 4 * num_band_anal_qmf)]);
914         accu += (ptr_inp1[n + 6 * num_band_anal_qmf] * ptr_qmf1[2 * (n + 6 * num_band_anal_qmf)]);
915         accu += (ptr_inp1[n + 8 * num_band_anal_qmf] * ptr_qmf1[2 * (n + 8 * num_band_anal_qmf)]);
916         ptr_out[n] = 2 * accu;
917 
918         accu = (ptr_inp1[n + 1 + 0] * ptr_qmf1[2 * (n + 1 + 0)]);
919         accu += (ptr_inp1[n + 1 + 2 * num_band_anal_qmf] *
920                  ptr_qmf1[2 * (n + 1 + 2 * num_band_anal_qmf)]);
921         accu += (ptr_inp1[n + 1 + 4 * num_band_anal_qmf] *
922                  ptr_qmf1[2 * (n + 1 + 4 * num_band_anal_qmf)]);
923         accu += (ptr_inp1[n + 1 + 6 * num_band_anal_qmf] *
924                  ptr_qmf1[2 * (n + 1 + 6 * num_band_anal_qmf)]);
925         accu += (ptr_inp1[n + 1 + 8 * num_band_anal_qmf] *
926                  ptr_qmf1[2 * (n + 1 + 8 * num_band_anal_qmf)]);
927         ptr_out[n + 1] = 2 * accu;
928 
929         accu = (ptr_inp2[n + 0] * ptr_qmf2[2 * (n + 0)]);
930         accu += (ptr_inp2[n + 2 * num_band_anal_qmf] * ptr_qmf2[2 * (n + 2 * num_band_anal_qmf)]);
931         accu += (ptr_inp2[n + 4 * num_band_anal_qmf] * ptr_qmf2[2 * (n + 4 * num_band_anal_qmf)]);
932         accu += (ptr_inp2[n + 6 * num_band_anal_qmf] * ptr_qmf2[2 * (n + 6 * num_band_anal_qmf)]);
933         accu += (ptr_inp2[n + 8 * num_band_anal_qmf] * ptr_qmf2[2 * (n + 8 * num_band_anal_qmf)]);
934         ptr_out[n + num_band_anal_qmf] = 2 * accu;
935 
936         accu = (ptr_inp2[n + 1 + 0] * ptr_qmf2[2 * (n + 1 + 0)]);
937         accu += (ptr_inp2[n + 1 + 2 * num_band_anal_qmf] *
938                  ptr_qmf2[2 * (n + 1 + 2 * num_band_anal_qmf)]);
939         accu += (ptr_inp2[n + 1 + 4 * num_band_anal_qmf] *
940                  ptr_qmf2[2 * (n + 1 + 4 * num_band_anal_qmf)]);
941         accu += (ptr_inp2[n + 1 + 6 * num_band_anal_qmf] *
942                  ptr_qmf2[2 * (n + 1 + 6 * num_band_anal_qmf)]);
943         accu += (ptr_inp2[n + 1 + 8 * num_band_anal_qmf] *
944                  ptr_qmf2[2 * (n + 1 + 8 * num_band_anal_qmf)]);
945         ptr_out[n + 1 + num_band_anal_qmf] = 2 * accu;
946 
947         n += 2;
948       }
949       break;
950     }
951     case 24: {
952       n = 0;
953       while (n < num_band_anal_qmf) {
954         accu = (ptr_inp1[n + 0] * ptr_qmf1[(n + 0)]);
955         accu += (ptr_inp1[n + 2 * num_band_anal_qmf] * ptr_qmf1[(n + 2 * num_band_anal_qmf)]);
956         accu += (ptr_inp1[n + 4 * num_band_anal_qmf] * ptr_qmf1[(n + 4 * num_band_anal_qmf)]);
957         accu += (ptr_inp1[n + 6 * num_band_anal_qmf] * ptr_qmf1[(n + 6 * num_band_anal_qmf)]);
958         accu += (ptr_inp1[n + 8 * num_band_anal_qmf] * ptr_qmf1[(n + 8 * num_band_anal_qmf)]);
959         ptr_out[n] = 2 * accu;
960 
961         accu = (ptr_inp1[n + 1 + 0] * ptr_qmf1[(n + 1 + 0)]);
962         accu +=
963             (ptr_inp1[n + 1 + 2 * num_band_anal_qmf] * ptr_qmf1[(n + 1 + 2 * num_band_anal_qmf)]);
964         accu +=
965             (ptr_inp1[n + 1 + 4 * num_band_anal_qmf] * ptr_qmf1[(n + 1 + 4 * num_band_anal_qmf)]);
966         accu +=
967             (ptr_inp1[n + 1 + 6 * num_band_anal_qmf] * ptr_qmf1[(n + 1 + 6 * num_band_anal_qmf)]);
968         accu +=
969             (ptr_inp1[n + 1 + 8 * num_band_anal_qmf] * ptr_qmf1[(n + 1 + 8 * num_band_anal_qmf)]);
970         ptr_out[n + 1] = 2 * accu;
971 
972         accu = (ptr_inp2[n + 0] * ptr_qmf2[(n + 0)]);
973         accu += (ptr_inp2[n + 2 * num_band_anal_qmf] * ptr_qmf2[(n + 2 * num_band_anal_qmf)]);
974         accu += (ptr_inp2[n + 4 * num_band_anal_qmf] * ptr_qmf2[(n + 4 * num_band_anal_qmf)]);
975         accu += (ptr_inp2[n + 6 * num_band_anal_qmf] * ptr_qmf2[(n + 6 * num_band_anal_qmf)]);
976         accu += (ptr_inp2[n + 8 * num_band_anal_qmf] * ptr_qmf2[(n + 8 * num_band_anal_qmf)]);
977         ptr_out[n + num_band_anal_qmf] = 2 * accu;
978 
979         accu = (ptr_inp2[n + 1 + 0] * ptr_qmf2[(n + 1 + 0)]);
980         accu +=
981             (ptr_inp2[n + 1 + 2 * num_band_anal_qmf] * ptr_qmf2[(n + 1 + 2 * num_band_anal_qmf)]);
982         accu +=
983             (ptr_inp2[n + 1 + 4 * num_band_anal_qmf] * ptr_qmf2[(n + 1 + 4 * num_band_anal_qmf)]);
984         accu +=
985             (ptr_inp2[n + 1 + 6 * num_band_anal_qmf] * ptr_qmf2[(n + 1 + 6 * num_band_anal_qmf)]);
986         accu +=
987             (ptr_inp2[n + 1 + 8 * num_band_anal_qmf] * ptr_qmf2[(n + 1 + 8 * num_band_anal_qmf)]);
988         ptr_out[n + 1 + num_band_anal_qmf] = 2 * accu;
989         n += 2;
990       }
991       break;
992     }
993     default: {
994       n = 0;
995       while (n < num_band_anal_qmf) {
996         accu = (ptr_inp1[n + 0] * ptr_qmf1[4 * (n + 0)]);
997         accu += (ptr_inp1[n + 2 * num_band_anal_qmf] * ptr_qmf1[4 * (n + 2 * num_band_anal_qmf)]);
998         accu += (ptr_inp1[n + 4 * num_band_anal_qmf] * ptr_qmf1[4 * (n + 4 * num_band_anal_qmf)]);
999         accu += (ptr_inp1[n + 6 * num_band_anal_qmf] * ptr_qmf1[4 * (n + 6 * num_band_anal_qmf)]);
1000         accu += (ptr_inp1[n + 8 * num_band_anal_qmf] * ptr_qmf1[4 * (n + 8 * num_band_anal_qmf)]);
1001         ptr_out[n] = 2 * accu;
1002 
1003         accu = (ptr_inp1[n + 1 + 0] * ptr_qmf1[4 * (n + 1 + 0)]);
1004         accu += (ptr_inp1[n + 1 + 2 * num_band_anal_qmf] *
1005                  ptr_qmf1[4 * (n + 1 + 2 * num_band_anal_qmf)]);
1006         accu += (ptr_inp1[n + 1 + 4 * num_band_anal_qmf] *
1007                  ptr_qmf1[4 * (n + 1 + 4 * num_band_anal_qmf)]);
1008         accu += (ptr_inp1[n + 1 + 6 * num_band_anal_qmf] *
1009                  ptr_qmf1[4 * (n + 1 + 6 * num_band_anal_qmf)]);
1010         accu += (ptr_inp1[n + 1 + 8 * num_band_anal_qmf] *
1011                  ptr_qmf1[4 * (n + 1 + 8 * num_band_anal_qmf)]);
1012         ptr_out[n + 1] = 2 * accu;
1013 
1014         accu = (ptr_inp2[n + 0] * ptr_qmf2[4 * (n + 0)]);
1015         accu += (ptr_inp2[n + 2 * num_band_anal_qmf] * ptr_qmf2[4 * (n + 2 * num_band_anal_qmf)]);
1016         accu += (ptr_inp2[n + 4 * num_band_anal_qmf] * ptr_qmf2[4 * (n + 4 * num_band_anal_qmf)]);
1017         accu += (ptr_inp2[n + 6 * num_band_anal_qmf] * ptr_qmf2[4 * (n + 6 * num_band_anal_qmf)]);
1018         accu += (ptr_inp2[n + 8 * num_band_anal_qmf] * ptr_qmf2[4 * (n + 8 * num_band_anal_qmf)]);
1019         ptr_out[n + num_band_anal_qmf] = 2 * accu;
1020 
1021         accu = (ptr_inp2[n + 1 + 0] * ptr_qmf2[4 * (n + 1 + 0)]);
1022         accu += (ptr_inp2[n + 1 + 2 * num_band_anal_qmf] *
1023                  ptr_qmf2[4 * (n + 1 + 2 * num_band_anal_qmf)]);
1024         accu += (ptr_inp2[n + 1 + 4 * num_band_anal_qmf] *
1025                  ptr_qmf2[4 * (n + 1 + 4 * num_band_anal_qmf)]);
1026         accu += (ptr_inp2[n + 1 + 6 * num_band_anal_qmf] *
1027                  ptr_qmf2[4 * (n + 1 + 6 * num_band_anal_qmf)]);
1028         accu += (ptr_inp2[n + 1 + 8 * num_band_anal_qmf] *
1029                  ptr_qmf2[4 * (n + 1 + 8 * num_band_anal_qmf)]);
1030         ptr_out[n + 1 + num_band_anal_qmf] = 2 * accu;
1031         n += 2;
1032       }
1033       break;
1034     }
1035   }
1036 }
1037 
ixheaace_esbr_radix4bfly(const FLOAT32 * ptr_w_in,FLOAT32 * ptr_x,WORD32 index1,WORD32 index)1038 VOID ixheaace_esbr_radix4bfly(const FLOAT32 *ptr_w_in, FLOAT32 *ptr_x, WORD32 index1,
1039                               WORD32 index) {
1040   int i;
1041   WORD32 l1, l2, h2, fft_jmp;
1042   FLOAT32 xt0_0, yt0_0, xt1_0, yt1_0, xt2_0, yt2_0;
1043   FLOAT32 xh0_0, xh1_0, xh20_0, xh21_0, xl0_0, xl1_0, xl20_0, xl21_0;
1044   FLOAT32 x_0, x_1, x_l1_0, x_l1_1, x_l2_0, x_l2_1;
1045   FLOAT32 x_h2_0, x_h2_1;
1046   FLOAT32 si10, si20, si30, co10, co20, co30;
1047 
1048   FLOAT32 mul_1, mul_2, mul_3, mul_4, mul_5, mul_6;
1049   FLOAT32 mul_7, mul_8, mul_9, mul_10, mul_11, mul_12;
1050 
1051   const FLOAT32 *ptr_w = ptr_w_in;
1052   WORD32 i1;
1053 
1054   h2 = index << 1;
1055   l1 = index << 2;
1056   l2 = (index << 2) + (index << 1);
1057 
1058   fft_jmp = 6 * (index);
1059 
1060   i1 = 0;
1061   while (i1 < index1) {
1062     for (i = 0; i < index; i++) {
1063       si10 = (*ptr_w++);
1064       co10 = (*ptr_w++);
1065       si20 = (*ptr_w++);
1066       co20 = (*ptr_w++);
1067       si30 = (*ptr_w++);
1068       co30 = (*ptr_w++);
1069 
1070       x_0 = ptr_x[0];
1071       x_h2_0 = ptr_x[h2];
1072       x_l1_0 = ptr_x[l1];
1073       x_l2_0 = ptr_x[l2];
1074 
1075       xh0_0 = (x_0 + x_l1_0);
1076       xl0_0 = (x_0 - x_l1_0);
1077 
1078       xh20_0 = (x_h2_0 + x_l2_0);
1079       xl20_0 = (x_h2_0 - x_l2_0);
1080 
1081       ptr_x[0] = (xh0_0 + xh20_0);
1082       xt0_0 = (xh0_0 - xh20_0);
1083 
1084       x_1 = ptr_x[1];
1085       x_h2_1 = ptr_x[h2 + 1];
1086       x_l1_1 = ptr_x[l1 + 1];
1087       x_l2_1 = ptr_x[l2 + 1];
1088 
1089       xh1_0 = (x_1 + x_l1_1);
1090       xl1_0 = (x_1 - x_l1_1);
1091 
1092       xh21_0 = (x_h2_1 + x_l2_1);
1093       xl21_0 = (x_h2_1 - x_l2_1);
1094 
1095       ptr_x[1] = (xh1_0 + xh21_0);
1096       yt0_0 = (xh1_0 - xh21_0);
1097 
1098       xt1_0 = (xl0_0 + xl21_0);
1099       xt2_0 = (xl0_0 - xl21_0);
1100 
1101       yt2_0 = (xl1_0 + xl20_0);
1102       yt1_0 = (xl1_0 - xl20_0);
1103 
1104       mul_11 = (xt2_0 * co30);
1105       mul_3 = (yt2_0 * si30);
1106       ptr_x[l2] = 2 * (mul_3 + mul_11);
1107 
1108       mul_5 = (xt2_0 * si30);
1109       mul_9 = (yt2_0 * co30);
1110       ptr_x[l2 + 1] = 2 * (mul_9 - mul_5);
1111 
1112       mul_12 = (xt0_0 * co20);
1113       mul_2 = (yt0_0 * si20);
1114       ptr_x[l1] = 2 * (mul_2 + mul_12);
1115 
1116       mul_6 = (xt0_0 * si20);
1117       mul_8 = (yt0_0 * co20);
1118       ptr_x[l1 + 1] = 2 * (mul_8 - mul_6);
1119 
1120       mul_4 = (xt1_0 * co10);
1121       mul_1 = (yt1_0 * si10);
1122       ptr_x[h2] = 2 * (mul_1 + mul_4);
1123 
1124       mul_10 = (xt1_0 * si10);
1125       mul_7 = (yt1_0 * co10);
1126       ptr_x[h2 + 1] = 2 * (mul_7 - mul_10);
1127 
1128       ptr_x += 2;
1129     }
1130     ptr_x += fft_jmp;
1131     ptr_w = ptr_w - fft_jmp;
1132     i1++;
1133   }
1134 }
1135 
ixheaace_esbr_postradixcompute2(FLOAT32 * ptr_y,FLOAT32 * ptr_x,const FLOAT32 * ptr_dig_rev_tbl,WORD32 npoints)1136 VOID ixheaace_esbr_postradixcompute2(FLOAT32 *ptr_y, FLOAT32 *ptr_x,
1137                                      const FLOAT32 *ptr_dig_rev_tbl, WORD32 npoints) {
1138   WORD32 i, k;
1139   WORD32 h2;
1140   FLOAT32 x_0, x_1, x_2, x_3;
1141   FLOAT32 x_4, x_5, x_6, x_7;
1142   FLOAT32 x_8, x_9, x_a, x_b, x_c, x_d, x_e, x_f;
1143   FLOAT32 n00, n10, n20, n30, n01, n11, n21, n31;
1144   FLOAT32 n02, n12, n22, n32, n03, n13, n23, n33;
1145 
1146   FLOAT32 *ptr_x2, *ptr_x0;
1147   FLOAT32 *ptr_y0, *ptr_y1, *ptr_y2, *ptr_y3;
1148 
1149   ptr_y0 = ptr_y;
1150   ptr_y2 = ptr_y + (WORD32)npoints;
1151   ptr_x0 = ptr_x;
1152   ptr_x2 = ptr_x + (WORD32)(npoints >> 1);
1153 
1154   ptr_y1 = ptr_y0 + (WORD32)(npoints >> 2);
1155   ptr_y3 = ptr_y2 + (WORD32)(npoints >> 2);
1156 
1157   for (k = 0; k < 2; k++) {
1158     for (i = 0; i<npoints>> 1; i += 8) {
1159       h2 = (WORD32)*ptr_dig_rev_tbl++ / 4;
1160 
1161       x_0 = *ptr_x0++;
1162       x_1 = *ptr_x0++;
1163       x_2 = *ptr_x0++;
1164       x_3 = *ptr_x0++;
1165       x_4 = *ptr_x0++;
1166       x_5 = *ptr_x0++;
1167       x_6 = *ptr_x0++;
1168       x_7 = *ptr_x0++;
1169 
1170       n00 = (x_0 + x_2);
1171       n01 = (x_1 + x_3);
1172 
1173       n20 = (x_0 - x_2);
1174       n21 = (x_1 - x_3);
1175 
1176       n10 = (x_4 + x_6);
1177       n11 = (x_5 + x_7);
1178 
1179       n30 = (x_4 - x_6);
1180       n31 = (x_5 - x_7);
1181 
1182       ptr_y0[h2] = n00;
1183       ptr_y0[h2 + 1] = n01;
1184       ptr_y1[h2] = n10;
1185       ptr_y1[h2 + 1] = n11;
1186       ptr_y2[h2] = n20;
1187       ptr_y2[h2 + 1] = n21;
1188       ptr_y3[h2] = n30;
1189       ptr_y3[h2 + 1] = n31;
1190 
1191       x_8 = *ptr_x2++;
1192       x_9 = *ptr_x2++;
1193       x_a = *ptr_x2++;
1194       x_b = *ptr_x2++;
1195       x_c = *ptr_x2++;
1196       x_d = *ptr_x2++;
1197       x_e = *ptr_x2++;
1198       x_f = *ptr_x2++;
1199 
1200       n02 = (x_8 + x_a);
1201       n03 = (x_9 + x_b);
1202 
1203       n22 = (x_8 - x_a);
1204       n23 = (x_9 - x_b);
1205 
1206       n12 = (x_c + x_e);
1207       n13 = (x_d + x_f);
1208 
1209       n32 = (x_c - x_e);
1210       n33 = (x_d - x_f);
1211 
1212       ptr_y0[h2 + 2] = n02;
1213       ptr_y0[h2 + 3] = n03;
1214       ptr_y1[h2 + 2] = n12;
1215       ptr_y1[h2 + 3] = n13;
1216       ptr_y2[h2 + 2] = n22;
1217       ptr_y2[h2 + 3] = n23;
1218       ptr_y3[h2 + 2] = n32;
1219       ptr_y3[h2 + 3] = n33;
1220     }
1221     ptr_x0 += (WORD32)npoints >> 1;
1222     ptr_x2 += (WORD32)npoints >> 1;
1223   }
1224 }
1225 
ixheaace_esbr_postradixcompute4(FLOAT32 * ptr_y,FLOAT32 * ptr_x,const FLOAT32 * ptr_dig_rev_tbl,WORD32 npoints)1226 VOID ixheaace_esbr_postradixcompute4(FLOAT32 *ptr_y, FLOAT32 *ptr_x,
1227                                      const FLOAT32 *ptr_dig_rev_tbl, WORD32 npoints) {
1228   WORD32 i, k;
1229   WORD32 h2;
1230   FLOAT32 xh0_0, xh1_0, xl0_0, xl1_0;
1231   FLOAT32 xh0_1, xh1_1, xl0_1, xl1_1;
1232   FLOAT32 x_0, x_1, x_2, x_3;
1233   FLOAT32 xh0_2, xh1_2, xl0_2, xl1_2, xh0_3, xh1_3, xl0_3, xl1_3;
1234   FLOAT32 x_4, x_5, x_6, x_7;
1235   FLOAT32 x_8, x_9, x_a, x_b, x_c, x_d, x_e, x_f;
1236   FLOAT32 n00, n10, n20, n30, n01, n11, n21, n31;
1237   FLOAT32 n02, n12, n22, n32, n03, n13, n23, n33;
1238 
1239   FLOAT32 *ptr_x2, *ptr_x0;
1240   FLOAT32 *ptr_y0, *ptr_y1, *ptr_y2, *ptr_y3;
1241 
1242   ptr_y0 = ptr_y;
1243   ptr_y2 = ptr_y + npoints;
1244   ptr_x0 = ptr_x;
1245   ptr_x2 = ptr_x + (WORD32)(npoints >> 1);
1246 
1247   ptr_y1 = ptr_y0 + (WORD32)(npoints >> 1);
1248   ptr_y3 = ptr_y2 + (WORD32)(npoints >> 1);
1249 
1250   for (k = 0; k < 2; k++) {
1251     for (i = 0; i<npoints>> 1; i += 8) {
1252       h2 = (WORD32)*ptr_dig_rev_tbl++ / 4;
1253       x_0 = *ptr_x0++;
1254       x_1 = *ptr_x0++;
1255       x_2 = *ptr_x0++;
1256       x_3 = *ptr_x0++;
1257       x_4 = *ptr_x0++;
1258       x_5 = *ptr_x0++;
1259       x_6 = *ptr_x0++;
1260       x_7 = *ptr_x0++;
1261 
1262       xh0_0 = (x_0 + x_4);
1263       xh1_0 = (x_1 + x_5);
1264 
1265       xl0_0 = (x_0 - x_4);
1266       xl1_0 = (x_1 - x_5);
1267 
1268       xh0_1 = (x_2 + x_6);
1269       xh1_1 = (x_3 + x_7);
1270 
1271       xl0_1 = (x_2 - x_6);
1272       xl1_1 = (x_3 - x_7);
1273 
1274       n00 = (xh0_0 + xh0_1);
1275       n01 = (xh1_0 + xh1_1);
1276       n10 = (xl0_0 + xl1_1);
1277 
1278       n11 = (xl1_0 - xl0_1);
1279       n20 = (xh0_0 - xh0_1);
1280       n21 = (xh1_0 - xh1_1);
1281       n30 = (xl0_0 - xl1_1);
1282 
1283       n31 = (xl1_0 + xl0_1);
1284 
1285       ptr_y0[h2] = n00;
1286       ptr_y0[h2 + 1] = n01;
1287       ptr_y1[h2] = n10;
1288       ptr_y1[h2 + 1] = n11;
1289       ptr_y2[h2] = n20;
1290       ptr_y2[h2 + 1] = n21;
1291       ptr_y3[h2] = n30;
1292       ptr_y3[h2 + 1] = n31;
1293 
1294       x_8 = *ptr_x2++;
1295       x_9 = *ptr_x2++;
1296       x_a = *ptr_x2++;
1297       x_b = *ptr_x2++;
1298       x_c = *ptr_x2++;
1299       x_d = *ptr_x2++;
1300       x_e = *ptr_x2++;
1301       x_f = *ptr_x2++;
1302 
1303       xh0_2 = (x_8 + x_c);
1304       xh1_2 = (x_9 + x_d);
1305 
1306       xl0_2 = (x_8 - x_c);
1307       xl1_2 = (x_9 - x_d);
1308 
1309       xh0_3 = (x_a + x_e);
1310       xh1_3 = (x_b + x_f);
1311 
1312       xl0_3 = (x_a - x_e);
1313       xl1_3 = (x_b - x_f);
1314 
1315       n02 = (xh0_2 + xh0_3);
1316       n03 = (xh1_2 + xh1_3);
1317       n12 = (xl0_2 + xl1_3);
1318 
1319       n13 = (xl1_2 - xl0_3);
1320       n22 = (xh0_2 - xh0_3);
1321       n23 = (xh1_2 - xh1_3);
1322       n32 = (xl0_2 - xl1_3);
1323 
1324       n33 = (xl1_2 + xl0_3);
1325 
1326       ptr_y0[h2 + 2] = n02;
1327       ptr_y0[h2 + 3] = n03;
1328       ptr_y1[h2 + 2] = n12;
1329       ptr_y1[h2 + 3] = n13;
1330       ptr_y2[h2 + 2] = n22;
1331       ptr_y2[h2 + 3] = n23;
1332       ptr_y3[h2 + 2] = n32;
1333       ptr_y3[h2 + 3] = n33;
1334     }
1335     ptr_x0 += (WORD32)npoints >> 1;
1336     ptr_x2 += (WORD32)npoints >> 1;
1337   }
1338 }
1339 
ixheaace_esbr_cos_sin_mod(FLOAT32 * subband,ia_sbr_qmf_filter_bank_struct * pstr_qmf_bank,FLOAT32 * ptr_twiddle,FLOAT32 * ptr_dig_rev_tbl)1340 VOID ixheaace_esbr_cos_sin_mod(FLOAT32 *subband, ia_sbr_qmf_filter_bank_struct *pstr_qmf_bank,
1341                                FLOAT32 *ptr_twiddle, FLOAT32 *ptr_dig_rev_tbl) {
1342   WORD32 z;
1343   FLOAT32 temp[128] = {0};
1344 
1345   FLOAT32 re2, re3;
1346   FLOAT32 wim, wre;
1347 
1348   WORD32 i, M_2;
1349   WORD32 M = pstr_qmf_bank->no_channels / 2;
1350 
1351   const FLOAT32 *ptr_sin;
1352   const FLOAT32 *ptr_sin_cos;
1353 
1354   FLOAT32 subband_tmp[128] = {0};
1355   FLOAT32 re;
1356   FLOAT32 im;
1357   FLOAT32 *ptr_subband, *ptr_subband1;
1358   FLOAT32 *ptr_subband_t, *ptr_subband1_t;
1359   FLOAT32 *ptr_subband2, *ptr_subband12;
1360   FLOAT32 *ptr_subband_t2, *ptr_subband1_t2;
1361 
1362   M_2 = M / 2;
1363 
1364   ptr_sin_cos = pstr_qmf_bank->ptr_esbr_cos_twiddle;
1365 
1366   ptr_subband = &subband[0];
1367   ptr_subband1 = &subband[2 * M - 1];
1368   ptr_subband_t = subband_tmp;
1369   ptr_subband1_t = &subband_tmp[2 * M - 1];
1370 
1371   ptr_subband2 = &subband[64];
1372   ptr_subband12 = &subband[2 * M - 1 + 64];
1373   ptr_subband_t2 = &subband_tmp[64];
1374   ptr_subband1_t2 = &subband_tmp[2 * M - 1 + 64];
1375 
1376   i = (M_2 >> 1) - 1;
1377   while (i >= 0) {
1378     re = *ptr_subband++;
1379     im = *ptr_subband1--;
1380 
1381     wim = *ptr_sin_cos++;
1382     wre = *ptr_sin_cos++;
1383 
1384     *ptr_subband_t++ = (re * wre) + (im * wim);
1385     *ptr_subband_t++ = (im * wre) - (re * wim);
1386 
1387     re = *ptr_subband2++;
1388     im = *ptr_subband12--;
1389 
1390     *ptr_subband_t2++ = (im * wim) - (re * wre);
1391     *ptr_subband_t2++ = (re * wim) + (im * wre);
1392 
1393     re = *ptr_subband1--;
1394     im = *ptr_subband++;
1395 
1396     wim = *ptr_sin_cos++;
1397     wre = *ptr_sin_cos++;
1398 
1399     *ptr_subband1_t-- = (im * wre) - (re * wim);
1400     *ptr_subband1_t-- = (re * wre) + (im * wim);
1401 
1402     re = *ptr_subband12--;
1403     im = *ptr_subband2++;
1404 
1405     *ptr_subband1_t2-- = (re * wim) + (im * wre);
1406     *ptr_subband1_t2-- = (im * wim) - (re * wre);
1407 
1408     re = *ptr_subband++;
1409     im = *ptr_subband1--;
1410 
1411     wim = *ptr_sin_cos++;
1412     wre = *ptr_sin_cos++;
1413 
1414     *ptr_subband_t++ = (re * wre) + (im * wim);
1415     *ptr_subband_t++ = (im * wre) - (re * wim);
1416 
1417     re = *ptr_subband2++;
1418     im = *ptr_subband12--;
1419 
1420     *ptr_subband_t2++ = (im * wim) - (re * wre);
1421     *ptr_subband_t2++ = (re * wim) + (im * wre);
1422 
1423     re = *ptr_subband1--;
1424     im = *ptr_subband++;
1425 
1426     wim = *ptr_sin_cos++;
1427     wre = *ptr_sin_cos++;
1428 
1429     *ptr_subband1_t-- = (im * wre) - (re * wim);
1430     *ptr_subband1_t-- = (re * wre) + (im * wim);
1431 
1432     re = *ptr_subband12--;
1433     im = *ptr_subband2++;
1434 
1435     *ptr_subband1_t2-- = (re * wim) + (im * wre);
1436     *ptr_subband1_t2-- = (im * wim) - (re * wre);
1437 
1438     i--;
1439   }
1440 
1441   switch (M) {
1442     case M_32:
1443       ixheaace_esbr_radix4bfly(ptr_twiddle, subband_tmp, 1, 8);
1444       ixheaace_esbr_radix4bfly(ptr_twiddle + 48, subband_tmp, 4, 2);
1445       ixheaace_esbr_postradixcompute2(subband, subband_tmp, ptr_dig_rev_tbl, 32);
1446 
1447       ixheaace_esbr_radix4bfly(ptr_twiddle, &subband_tmp[64], 1, 8);
1448       ixheaace_esbr_radix4bfly(ptr_twiddle + 48, &subband_tmp[64], 4, 2);
1449       ixheaace_esbr_postradixcompute2(&subband[64], &subband_tmp[64], ptr_dig_rev_tbl, 32);
1450       break;
1451 
1452     case M_16:
1453       ixheaace_esbr_radix4bfly(ptr_twiddle, subband_tmp, 1, 4);
1454       ixheaace_esbr_postradixcompute4(subband, subband_tmp, ptr_dig_rev_tbl, 16);
1455 
1456       ixheaace_esbr_radix4bfly(ptr_twiddle, &subband_tmp[64], 1, 4);
1457       ixheaace_esbr_postradixcompute4(&subband[64], &subband_tmp[64], ptr_dig_rev_tbl, 16);
1458       break;
1459 
1460     case M_12:
1461 
1462       for (z = 0; z < (pstr_qmf_bank->no_channels >> 1); z++) {
1463         temp[z] = subband_tmp[2 * z];
1464         temp[12 + z] = subband_tmp[2 * z + 1];
1465       }
1466 
1467       // convert re and im data to interleave
1468       FLOAT32 intermediate[24];
1469       WORD32 cnt = 0;
1470       while (cnt < M_12) {
1471         intermediate[2 * cnt] = temp[cnt];
1472         intermediate[2 * cnt + 1] = temp[12 + cnt];
1473         cnt++;
1474       }
1475 
1476       iusace_complex_fft_p3_no_scratch(intermediate, 12);
1477       // de-interleave
1478       for (cnt = 0; cnt < 12; cnt++) {
1479         temp[cnt] = intermediate[2 * cnt];
1480         temp[12 + cnt] = intermediate[2 * cnt + 1];
1481       }
1482 
1483       z = 0;
1484       while (z < (pstr_qmf_bank->no_channels >> 1)) {
1485         subband[2 * z] = temp[z];
1486         subband[2 * z + 1] = temp[z + 12];
1487         z++;
1488       }
1489 
1490       z = 0;
1491       while (z < (pstr_qmf_bank->no_channels >> 1)) {
1492         temp[z] = subband_tmp[64 + 2 * z];
1493         temp[12 + z] = subband_tmp[64 + 2 * z + 1];
1494         z++;
1495       }
1496 
1497       // convert re and im data to interleave
1498       cnt = 0;
1499       while (cnt < 12) {
1500         intermediate[2 * cnt] = temp[cnt];
1501         intermediate[2 * cnt + 1] = temp[12 + cnt];
1502         cnt++;
1503       }
1504       iusace_complex_fft_p3_no_scratch(intermediate, 12);
1505       // de-interleave
1506 
1507       cnt = 0;
1508       while (cnt < 12) {
1509         temp[cnt] = intermediate[2 * cnt];
1510         temp[12 + cnt] = intermediate[2 * cnt + 1];
1511         cnt++;
1512       }
1513 
1514       z = 0;
1515       while (z < (pstr_qmf_bank->no_channels >> 1)) {
1516         subband[64 + 2 * z] = temp[z];
1517         subband[64 + 2 * z + 1] = temp[z + 12];
1518         z++;
1519       }
1520       break;
1521 
1522     default:
1523       z = 0;
1524       while (z < (pstr_qmf_bank->no_channels >> 1)) {
1525         temp[z] = subband_tmp[2 * z];
1526         temp[8 + z] = subband_tmp[2 * z + 1];
1527         z++;
1528       }
1529 
1530       FLOAT32 scratch[1024];
1531       cnt = 0;
1532       while (cnt < 8) {
1533         intermediate[2 * cnt] = temp[cnt];
1534         intermediate[2 * cnt + 1] = temp[8 + cnt];
1535         cnt++;
1536       }
1537 
1538       iusace_complex_fft_p2(intermediate, 8, scratch);
1539       // de-interleave
1540       cnt = 0;
1541       while (cnt < 8) {
1542         temp[cnt] = intermediate[2 * cnt];
1543         temp[8 + cnt] = intermediate[2 * cnt + 1];
1544         cnt++;
1545       }
1546 
1547       z = 0;
1548       while (z < (pstr_qmf_bank->no_channels >> 1)) {
1549         subband[2 * z] = temp[z];
1550         subband[2 * z + 1] = temp[z + 8];
1551         z++;
1552       }
1553       z = 0;
1554       while (z < (pstr_qmf_bank->no_channels >> 1)) {
1555         temp[z] = subband_tmp[64 + 2 * z];
1556         temp[8 + z] = subband_tmp[64 + 2 * z + 1];
1557         z++;
1558       }
1559 
1560       // convert re and im data to interleave
1561       cnt = 0;
1562       while (cnt < 8) {
1563         intermediate[2 * cnt] = temp[cnt];
1564         intermediate[2 * cnt + 1] = temp[8 + cnt];
1565         cnt++;
1566       }
1567 
1568       iusace_complex_fft_p2(intermediate, 8, scratch);
1569 
1570       // de-interleave
1571       cnt = 0;
1572       while (cnt < 8) {
1573         temp[cnt] = intermediate[2 * cnt];
1574         temp[8 + cnt] = intermediate[2 * cnt + 1];
1575         cnt++;
1576       }
1577 
1578       z = 0;
1579       while (z < (pstr_qmf_bank->no_channels >> 1)) {
1580         subband[64 + 2 * z] = temp[z];
1581         subband[64 + 2 * z + 1] = temp[8 + z];
1582         z++;
1583       }
1584       break;
1585   }
1586 
1587   ptr_subband = &subband[0];
1588   ptr_subband1 = &subband[2 * M - 1];
1589 
1590   re = *ptr_subband1;
1591 
1592   *ptr_subband = *ptr_subband / 2;
1593   ptr_subband++;
1594   *ptr_subband1 = -(*ptr_subband / 2);
1595   ptr_subband1--;
1596 
1597   ptr_sin = pstr_qmf_bank->ptr_esbr_alt_sin_twiddle;
1598 
1599   wim = *ptr_sin++;
1600   wre = *ptr_sin++;
1601 
1602   im = *ptr_subband1;
1603 
1604   *ptr_subband1-- = (re * wre) + (im * wim);
1605   *ptr_subband++ = (im * wre) - (re * wim);
1606 
1607   ptr_subband2 = &subband[64];
1608   ptr_subband12 = &subband[2 * M - 1 + 64];
1609 
1610   re = *ptr_subband12;
1611 
1612   *ptr_subband12-- = -(*ptr_subband2 / 2);
1613 
1614   *ptr_subband2 = ptr_subband2[1] / 2;
1615 
1616   ptr_subband2++;
1617 
1618   im = *ptr_subband12;
1619 
1620   *ptr_subband2++ = -((re * wre) + (im * wim));
1621   *ptr_subband12-- = (re * wim) - (im * wre);
1622 
1623   i = (M_2 - 2);
1624   while (i >= 0) {
1625     im = ptr_subband[0];
1626 
1627     re = ptr_subband[1];
1628 
1629     re2 = *ptr_subband1;
1630 
1631     *ptr_subband++ = (re * wim) + (im * wre);
1632     *ptr_subband1-- = (im * wim) - (re * wre);
1633 
1634     im = ptr_subband2[0];
1635 
1636     re = ptr_subband2[1];
1637 
1638     re3 = *ptr_subband12;
1639 
1640     *ptr_subband12-- = -((re * wim) + (im * wre));
1641     *ptr_subband2++ = (re * wre) - (im * wim);
1642 
1643     wim = *ptr_sin++;
1644     wre = *ptr_sin++;
1645     im = ptr_subband1[0];
1646 
1647     *ptr_subband1-- = (re2 * wre) + (im * wim);
1648     *ptr_subband++ = (im * wre) - (re2 * wim);
1649 
1650     im = ptr_subband12[0];
1651 
1652     *ptr_subband2++ = -((re3 * wre) + (im * wim));
1653     *ptr_subband12-- = (re3 * wim) - (im * wre);
1654     i--;
1655   }
1656 }
1657 
ixheaace_esbr_fwd_modulation(const FLOAT32 * ptr_time_sample_buf,FLOAT32 * ptr_in_real_subband,FLOAT32 * ptr_in_imag_subband,ia_sbr_qmf_filter_bank_struct * pstr_qmf_bank,ixheaace_str_qmf_dec_tabs_struct * pstr_qmf_dec_tabs)1658 static VOID ixheaace_esbr_fwd_modulation(const FLOAT32 *ptr_time_sample_buf,
1659                                          FLOAT32 *ptr_in_real_subband,
1660                                          FLOAT32 *ptr_in_imag_subband,
1661                                          ia_sbr_qmf_filter_bank_struct *pstr_qmf_bank,
1662                                          ixheaace_str_qmf_dec_tabs_struct *pstr_qmf_dec_tabs) {
1663   WORD32 i;
1664   const FLOAT32 *ptr_time_sample_buf1 = &ptr_time_sample_buf[2 * pstr_qmf_bank->no_channels - 1];
1665   FLOAT32 temp1, temp2;
1666   FLOAT32 *ptr_real_subband = ptr_in_real_subband;
1667   FLOAT32 *ptr_imag_subband = ptr_in_imag_subband;
1668   const FLOAT32 *ptr_cos;
1669 
1670   for (i = pstr_qmf_bank->no_channels - 1; i >= 0; i--) {
1671     temp1 = *ptr_time_sample_buf++ / 16.0f;
1672     temp2 = *ptr_time_sample_buf1-- / 16.0f;
1673     *ptr_real_subband++ = (temp1 - temp2);
1674     *ptr_imag_subband++ = (temp1 + temp2);
1675   }
1676 
1677   ixheaace_esbr_cos_sin_mod(ptr_in_real_subband, pstr_qmf_bank, pstr_qmf_dec_tabs->esbr_w_16,
1678                             pstr_qmf_dec_tabs->dig_rev_tab_4_16);
1679 
1680   ptr_cos = pstr_qmf_bank->ptr_esbr_t_cos;
1681 
1682   i = (pstr_qmf_bank->usb - pstr_qmf_bank->lsb - 1);
1683   while (i >= 0) {
1684     FLOAT32 cosh, sinh;
1685     FLOAT32 re, im;
1686 
1687     re = *ptr_in_real_subband;
1688     im = *ptr_in_imag_subband;
1689     cosh = *ptr_cos++;
1690     sinh = *ptr_cos++;
1691     *ptr_in_real_subband++ = 2 * ((re * cosh) + (im * sinh));
1692     *ptr_in_imag_subband++ = 2 * ((im * cosh) - (re * sinh));
1693     i--;
1694   }
1695 }
1696 
ixheaace_esbr_analysis_filt_block(ia_sbr_qmf_filter_bank_struct * pstr_codec_qmf_bank,ixheaace_str_qmf_dec_tabs_struct * pstr_qmf_dec_tabs,FLOAT32 * ptr_core_coder_samples,FLOAT32 qmf_buf_real[IXHEAACE_TIMESLOT_BUFFER_SIZE+2* 32][IXHEAACE_NUM_QMF_SYNTH_CHANNELS],FLOAT32 qmf_buf_imag[IXHEAACE_TIMESLOT_BUFFER_SIZE+2* 32][IXHEAACE_NUM_QMF_SYNTH_CHANNELS],WORD32 op_delay)1697 VOID ixheaace_esbr_analysis_filt_block(
1698     ia_sbr_qmf_filter_bank_struct *pstr_codec_qmf_bank,
1699     ixheaace_str_qmf_dec_tabs_struct *pstr_qmf_dec_tabs, FLOAT32 *ptr_core_coder_samples,
1700     FLOAT32 qmf_buf_real[IXHEAACE_TIMESLOT_BUFFER_SIZE + 2 * 32][IXHEAACE_NUM_QMF_SYNTH_CHANNELS],
1701     FLOAT32 qmf_buf_imag[IXHEAACE_TIMESLOT_BUFFER_SIZE + 2 * 32][IXHEAACE_NUM_QMF_SYNTH_CHANNELS],
1702     WORD32 op_delay) {
1703   FLOAT32 *ptr_filt_states;
1704   FLOAT32 *ptr_filt_states_1;
1705   FLOAT32 *ptr_filt_states_2;
1706   FLOAT32 *ptr_temp;
1707   FLOAT32 *ptr_win_coeffs_1;
1708   FLOAT32 *ptr_win_coeffs_2;
1709   FLOAT32 *ptr_win_coeffs;
1710   FLOAT32 *ptr_loc_qmf_buf_real;
1711   FLOAT32 *ptr_loc_qmf_buf_imag;
1712   FLOAT32 local_qmf_buffer[128] = {0};
1713   FLOAT32 anal_buf[2 * 32] = {0};
1714   WORD32 idx, z;
1715   WORD32 core_syn_ch_index;
1716   FLOAT32 gain;
1717   WORD32 filt_offset;
1718   WORD32 num_columns;
1719 
1720   ia_sbr_qmf_filter_bank_struct *pstr_qmf_anal_bank = pstr_codec_qmf_bank;
1721   ptr_filt_states = pstr_qmf_anal_bank->ptr_state_new_samples_pos_low_32;
1722   ptr_win_coeffs_1 = (FLOAT32 *)pstr_qmf_anal_bank->ptr_filter_pos_32;
1723   num_columns = pstr_qmf_anal_bank->no_channels;
1724 
1725   switch (num_columns) {
1726     case 16:
1727       ptr_win_coeffs_2 = ptr_win_coeffs_1 + 64;
1728       gain = 128.0f;
1729       filt_offset = 64;
1730       break;
1731     case 24:
1732       ptr_win_coeffs_2 = ptr_win_coeffs_1 + 24;
1733       gain = 12.0f;
1734       filt_offset = 24;
1735       break;
1736     case 32:
1737       ptr_win_coeffs_2 = ptr_win_coeffs_1 + 64;
1738       gain = 256.0f;
1739       filt_offset = 64;
1740       break;
1741     default:
1742       ptr_win_coeffs_2 = ptr_win_coeffs_1 + 64;
1743       gain = 256.0f;
1744       filt_offset = 64;
1745       break;
1746   }
1747   gain = 1.0f / gain;
1748 
1749   pstr_qmf_anal_bank->usb = (WORD16)num_columns;
1750 
1751   ptr_loc_qmf_buf_real = &local_qmf_buffer[0];
1752   ptr_loc_qmf_buf_imag = &local_qmf_buffer[64];
1753 
1754   ptr_filt_states_1 = pstr_qmf_anal_bank->anal_filter_states_32;
1755   ptr_filt_states_2 = pstr_qmf_anal_bank->anal_filter_states_32 + num_columns;
1756 
1757   idx = 0;
1758   while (idx < pstr_codec_qmf_bank->num_time_slots) {
1759     for (z = 0; z < num_columns; z++) {
1760       ptr_filt_states[num_columns - 1 - z] = ptr_core_coder_samples[z];
1761     }
1762 
1763     ixheaace_esbr_qmfanal32_winadd(ptr_filt_states_1, ptr_filt_states_2, ptr_win_coeffs_1,
1764                                    ptr_win_coeffs_2, anal_buf, num_columns);
1765 
1766     ptr_core_coder_samples += num_columns;
1767 
1768     ptr_filt_states -= num_columns;
1769     if (ptr_filt_states < pstr_qmf_anal_bank->anal_filter_states_32) {
1770       ptr_filt_states =
1771           pstr_qmf_anal_bank->anal_filter_states_32 + 10 * num_columns - num_columns;
1772     }
1773 
1774     ptr_temp = ptr_filt_states_1;
1775     ptr_filt_states_1 = ptr_filt_states_2;
1776     ptr_filt_states_2 = ptr_temp;
1777 
1778     ptr_win_coeffs_1 += filt_offset;
1779     ptr_win_coeffs_2 += filt_offset;
1780 
1781     ptr_win_coeffs = ptr_win_coeffs_1;
1782     ptr_win_coeffs_1 = ptr_win_coeffs_2;
1783     ptr_win_coeffs_2 = ptr_win_coeffs;
1784 
1785     if (ptr_win_coeffs_2 > (pstr_qmf_anal_bank->ptr_ana_win_coeff_32 + filt_offset * 10)) {
1786       ptr_win_coeffs_1 = (FLOAT32 *)pstr_qmf_anal_bank->ptr_ana_win_coeff_32;
1787       ptr_win_coeffs_2 = (FLOAT32 *)pstr_qmf_anal_bank->ptr_ana_win_coeff_32 + filt_offset;
1788     }
1789 
1790     ixheaace_esbr_fwd_modulation(anal_buf, &ptr_loc_qmf_buf_real[0], &ptr_loc_qmf_buf_imag[0],
1791                                  pstr_qmf_anal_bank, pstr_qmf_dec_tabs);
1792 
1793     core_syn_ch_index = num_columns;
1794 
1795     for (z = 0; z < core_syn_ch_index; z++) {
1796       qmf_buf_real[op_delay + idx][z] = ((FLOAT32)ptr_loc_qmf_buf_real[z] * gain);
1797       qmf_buf_imag[op_delay + idx][z] = ((FLOAT32)ptr_loc_qmf_buf_imag[z] * gain);
1798     }
1799 
1800     idx++;
1801   }
1802 
1803   pstr_qmf_anal_bank->ptr_filter_pos_32 = ptr_win_coeffs_1;
1804   pstr_qmf_anal_bank->ptr_state_new_samples_pos_low_32 = ptr_filt_states;
1805 }
ixheaace_extract_sbr_envelope(FLOAT32 * ptr_in_time,FLOAT32 * ptr_core_buf,UWORD32 time_sn_stride,ixheaace_pstr_sbr_enc pstr_env_enc,ixheaace_str_sbr_tabs * ptr_sbr_tab,ixheaace_comm_tables * pstr_com_tab,WORD32 flag_framelength_small)1806 IA_ERRORCODE ixheaace_extract_sbr_envelope(FLOAT32 *ptr_in_time, FLOAT32 *ptr_core_buf,
1807                                            UWORD32 time_sn_stride,
1808                                            ixheaace_pstr_sbr_enc pstr_env_enc,
1809                                            ixheaace_str_sbr_tabs *ptr_sbr_tab,
1810                                            ixheaace_comm_tables *pstr_com_tab,
1811                                            WORD32 flag_framelength_small) {
1812   IA_ERRORCODE err_code = IA_NO_ERROR;
1813   WORD32 ch, i, j, c;
1814   WORD32 n_envelopes[IXHEAACE_MAX_CH_IN_BS_ELE];
1815   WORD32 transient_info[IXHEAACE_MAX_CH_IN_BS_ELE][3];
1816   const ixheaace_str_frame_info_sbr *pstr_const_frame_info[IXHEAACE_MAX_CH_IN_BS_ELE];
1817   ixheaace_str_frame_info_sbr *pstr_frame_info = NULL;
1818 
1819   ixheaace_pstr_sbr_config_data pstr_sbr_cfg = &pstr_env_enc->str_sbr_cfg;
1820   ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr = &pstr_env_enc->str_sbr_hdr;
1821   ixheaace_pstr_sbr_bitstream_data pstr_sbr_bs = &pstr_env_enc->str_sbr_bs;
1822   struct ixheaace_ps_enc *pstr_ps_enc = pstr_env_enc->pstr_ps_enc;
1823   ixheaace_pstr_sbr_qmf_filter_bank pstr_synthesis_qmf_bank =
1824       pstr_env_enc->pstr_synthesis_qmf_bank;
1825   ixheaace_pstr_common_data pstr_com_data = &pstr_env_enc->str_cmon_data;
1826   WORD8 *ptr_sbr_scratch = pstr_env_enc->ptr_sbr_enc_scr->sbr_scratch;
1827   ixheaace_pstr_enc_channel pstr_env_ch[IXHEAACE_MAX_CH_IN_BS_ELE];
1828   pstr_env_ch[0] = pstr_env_enc->pstr_env_channel[0];
1829   pstr_env_ch[1] = pstr_env_enc->pstr_env_channel[1];
1830 
1831   WORD32 num_channels = pstr_sbr_cfg->num_ch;
1832   WORD32 n_in_channels = (pstr_ps_enc) ? 2 : num_channels;
1833 
1834   ixheaace_sbr_stereo_mode stereo_mode = pstr_sbr_cfg->stereo_mode;
1835   struct ixheaace_str_sbr_env_data *pstr_env_0 = &(pstr_env_ch[0]->enc_env_data);
1836   struct ixheaace_str_sbr_env_data *pstr_env_1 = NULL;
1837 
1838   if (num_channels > 1) {
1839     pstr_env_1 = &(pstr_env_ch[1]->enc_env_data);
1840   }
1841   ixheaace_freq_res res[MAXIMUM_NUM_NOISE_VALUES];
1842   WORD32 *ptr_v_tuning;
1843   WORD32 v_tuning_lc_sbr[6] = {0, 2, 4, 0, 0, 0};
1844   WORD32 v_tuning_ld_sbr[6] = {0, 2, 3, 0, 0, 0};
1845   if (pstr_sbr_cfg->is_ld_sbr) {
1846     ptr_v_tuning = v_tuning_ld_sbr;
1847   } else {
1848     ptr_v_tuning = v_tuning_lc_sbr;
1849   }
1850   FLOAT32 *ptr_noise_floor[IXHEAACE_MAX_CH_IN_BS_ELE] = {NULL};
1851   WORD32 *ptr_scale_factor_band_nrg[IXHEAACE_MAX_CH_IN_BS_ELE] = {NULL};
1852   WORD32 *ptr_noise_level[IXHEAACE_MAX_CH_IN_BS_ELE] = {NULL};
1853 
1854   WORD32 *ptr_sfb_nrg_coupling[IXHEAACE_MAX_CH_IN_BS_ELE];
1855   WORD32 *ptr_noise_lvl_coupling[IXHEAACE_MAX_CH_IN_BS_ELE];
1856   WORD32 *ptr_frame_splitter_scratch =
1857       (WORD32 *)pstr_env_ch[0]->str_sbr_extract_env.ptr_r_buffer[0];
1858 
1859   WORD32 max_quant_error;
1860   ixheaace_str_esbr_bs_data str_esbr = {0};
1861   WORD32 samp_ratio_fac = DOWNSAMPLE_FAC_2_1;
1862   if ((pstr_env_enc->str_sbr_cfg.sbr_codec == USAC_SBR) &&
1863       (pstr_env_enc->str_sbr_cfg.sbr_ratio_idx == USAC_SBR_RATIO_INDEX_4_1)) {
1864     samp_ratio_fac = DOWNSAMPLE_FAC_4_1;
1865   }
1866   if ((n_in_channels > IXHEAACE_MAX_CH_IN_BS_ELE) || (n_in_channels < num_channels) ||
1867       (n_in_channels <= 0) || (num_channels <= 0)) {
1868     return IA_EXHEAACE_EXE_FATAL_SBR_INVALID_IN_CHANNELS;
1869   }
1870   ch = 0;
1871   while (ch < n_in_channels) {
1872     ptr_sfb_nrg_coupling[ch] = (WORD32 *)pstr_env_ch[ch]->str_sbr_extract_env.ptr_r_buffer[0];
1873     ptr_noise_lvl_coupling[ch] = (WORD32 *)pstr_env_ch[ch]->str_sbr_extract_env.ptr_i_buffer[0];
1874     ptr_scale_factor_band_nrg[ch] =
1875         (WORD32 *)pstr_env_ch[ch]->str_sbr_extract_env.ptr_r_buffer[0] +
1876         IXHEAACE_MAX_CH_IN_BS_ELE * MAXIMUM_NUM_ENVELOPE_VALUES;
1877     ptr_noise_level[ch] = (WORD32 *)pstr_env_ch[ch]->str_sbr_extract_env.ptr_i_buffer[0] +
1878                           IXHEAACE_MAX_CH_IN_BS_ELE * MAXIMUM_NUM_ENVELOPE_VALUES;
1879     ptr_noise_floor[ch] = pstr_env_ch[ch]->str_sbr_extract_env.ptr_i_buffer[0] +
1880                           IXHEAACE_MAX_CH_IN_BS_ELE * MAXIMUM_NUM_ENVELOPE_VALUES * 2;
1881     ch++;
1882   }
1883   if ((pstr_sbr_cfg->sbr_codec == USAC_SBR) && (pstr_sbr_hdr->sbr_harmonic)) {
1884     WORD32 num_sbr_samples = 2048;
1885     if (pstr_sbr_cfg->sbr_ratio_idx == USAC_SBR_RATIO_INDEX_4_1) {
1886       num_sbr_samples = IXHEAACE_MAX_NUM_SAMPLES;
1887     }
1888     err_code = ixheaace_hbe_get_pitch_bins(
1889         ptr_in_time, pstr_sbr_cfg, pstr_env_ch[0]->str_sbr_extract_env.ptr_r_buffer[0],
1890         ptr_sbr_tab, time_sn_stride, num_sbr_samples, &pstr_env_0->sbr_pitchin_bins,
1891         n_in_channels == 1 ? NULL : &pstr_env_1->sbr_pitchin_bins);
1892     if (err_code) return err_code;
1893 
1894     WORD32 op_delay, codec_x_delay, num_time_slots;
1895     op_delay = IXHEAACE_OP_DELAY_OFFSET;
1896     codec_x_delay = IXHEAACE_ESBR_HBE_DELAY_OFFSET;
1897     if (pstr_sbr_cfg->sbr_ratio_idx == USAC_SBR_RATIO_INDEX_4_1) {
1898       op_delay = 2 * op_delay;
1899       codec_x_delay = 2 * codec_x_delay;
1900     }
1901 
1902     WORD32 eff_offset = op_delay + IXHEAACE_SBR_HF_ADJ_OFFSET;
1903     WORD32 memmove_sz1 = (eff_offset + codec_x_delay) *
1904                          sizeof(pstr_env_ch[0]->pstr_hbe_enc->qmf_buf_real[0][0]) *
1905                          MAX_QMF_TIME_SLOTS;
1906     WORD32 memmove_sz2 = eff_offset *
1907                          sizeof(pstr_env_ch[0]->pstr_hbe_enc->ph_vocod_qmf_real[0][0]) *
1908                          MAX_QMF_TIME_SLOTS;
1909 
1910     for (ch = 0; ch < n_in_channels; ch++) {
1911       ixheaace_str_hbe_enc *pstr_hbe_enc = pstr_env_ch[ch]->pstr_hbe_enc;
1912       num_time_slots =
1913           pstr_env_ch[ch]->str_sbr_qmf.num_time_slots * pstr_env_ch[ch]->str_sbr_qmf.rate;
1914 
1915       memmove(pstr_hbe_enc->qmf_buf_real[0], pstr_hbe_enc->qmf_buf_real[num_time_slots],
1916               memmove_sz1);
1917       memmove(pstr_hbe_enc->qmf_buf_imag[0], pstr_hbe_enc->qmf_buf_imag[num_time_slots],
1918               memmove_sz1);
1919       memmove(pstr_hbe_enc->ph_vocod_qmf_real[0], pstr_hbe_enc->ph_vocod_qmf_real[num_time_slots],
1920               memmove_sz2);
1921       memmove(pstr_hbe_enc->ph_vocod_qmf_imag, pstr_hbe_enc->ph_vocod_qmf_imag + num_time_slots,
1922               memmove_sz2);
1923     }
1924   }
1925   i = 0;
1926   while (i < MAXIMUM_NUM_NOISE_VALUES) {
1927     res[i] = FREQ_RES_HIGH;
1928     i++;
1929   }
1930 
1931   memset(transient_info, 0, sizeof(transient_info));
1932 
1933   ch = 0;
1934   while (ch < n_in_channels) {
1935     ixheaace_str_sbr_extr_env *pstr_sbr_extract_env = &(pstr_env_ch[ch]->str_sbr_extract_env);
1936 
1937     ixheaace_sbr_analysis_filtering(
1938         ptr_in_time ? ptr_in_time + ch : NULL, time_sn_stride,
1939         pstr_sbr_extract_env->ptr_r_buffer, pstr_sbr_extract_env->ptr_i_buffer,
1940         &pstr_env_ch[ch]->str_sbr_qmf, ptr_sbr_tab->ptr_qmf_tab,
1941         pstr_env_ch[ch]->str_sbr_qmf.num_time_slots * pstr_env_ch[ch]->str_sbr_qmf.rate,
1942         pstr_sbr_cfg->is_ld_sbr, (FLOAT32 *)ptr_sbr_scratch,
1943         (pstr_ps_enc != NULL && flag_framelength_small));
1944 
1945     if ((1 == n_in_channels) && (USAC_SBR == pstr_sbr_cfg->sbr_codec) &&
1946         (pstr_sbr_hdr->sbr_pvc_active)) {
1947       ixheaace_pvc_scratch *pstr_pvc_scr = (ixheaace_pvc_scratch *)ptr_sbr_scratch;
1948       WORD32 ts, bd;
1949       FLOAT32 nrg_0, nrg_1;
1950       FLOAT32 *ptr_r_0, *ptr_r_1, *ptr_i_0, *ptr_i_1;
1951       FLOAT32 *ptr_r_2, *ptr_r_3, *ptr_i_2, *ptr_i_3, nrg_2, nrg_3;
1952       WORD32 pvc_rate = pstr_env_enc->pstr_pvc_enc->pvc_param.pvc_rate;
1953 
1954       // update header_active to send SBR header when previous PVC mode is different from
1955       // current frame's
1956       if (pstr_env_enc->str_sbr_hdr.sbr_pvc_mode !=
1957           pstr_env_enc->pstr_pvc_enc->pvc_param.pvc_mode) {
1958         pstr_sbr_bs->header_active = 1;
1959       }
1960 
1961       switch (pvc_rate) {
1962         case 2: {
1963           for (ts = 0; ts < IXHEAACE_ESBR_PVC_NUM_TS; ts++) {
1964             ptr_r_0 = pstr_sbr_extract_env->ptr_r_buffer[pvc_rate * ts];
1965             ptr_r_1 = pstr_sbr_extract_env->ptr_r_buffer[pvc_rate * ts + 1];
1966             ptr_i_0 = pstr_sbr_extract_env->ptr_i_buffer[pvc_rate * ts];
1967             ptr_i_1 = pstr_sbr_extract_env->ptr_i_buffer[pvc_rate * ts + 1];
1968 
1969             for (bd = 0; bd < MAX_QMF_TIME_SLOTS; bd++) {
1970               nrg_0 = ptr_r_0[bd] * ptr_r_0[bd] + ptr_i_0[bd] * ptr_i_0[bd];
1971               nrg_1 = ptr_r_1[bd] * ptr_r_1[bd] + ptr_i_1[bd] * ptr_i_1[bd];
1972               pstr_pvc_scr->pvc_qmf_high[ts * IXHEAACE_ESBR_PVC_NUM_QMF_BANDS + bd] =
1973                   (nrg_0 + nrg_1) / 2.0f;
1974             }
1975             WORD32 num_low_bands = MAX_QMF_TIME_SLOTS >> 1;
1976             for (bd = 0; bd < num_low_bands; bd++) {
1977               pstr_pvc_scr->pvc_qmf_low[ts * num_low_bands + bd] =
1978                   pstr_pvc_scr->pvc_qmf_high[ts * IXHEAACE_ESBR_PVC_NUM_QMF_BANDS + bd];
1979             }
1980           }
1981           break;
1982         }
1983         case 4: {
1984           for (ts = 0; ts < IXHEAACE_ESBR_PVC_NUM_TS; ts++) {
1985             ptr_r_0 = pstr_sbr_extract_env->ptr_r_buffer[pvc_rate * ts];
1986             ptr_r_1 = pstr_sbr_extract_env->ptr_r_buffer[pvc_rate * ts + 1];
1987             ptr_r_2 = pstr_sbr_extract_env->ptr_r_buffer[pvc_rate * ts + 2];
1988             ptr_r_3 = pstr_sbr_extract_env->ptr_r_buffer[pvc_rate * ts + 3];
1989             ptr_i_0 = pstr_sbr_extract_env->ptr_i_buffer[pvc_rate * ts];
1990             ptr_i_1 = pstr_sbr_extract_env->ptr_i_buffer[pvc_rate * ts + 1];
1991             ptr_i_2 = pstr_sbr_extract_env->ptr_i_buffer[pvc_rate * ts + 2];
1992             ptr_i_3 = pstr_sbr_extract_env->ptr_i_buffer[pvc_rate * ts + 3];
1993 
1994             for (bd = 0; bd < MAX_QMF_TIME_SLOTS; bd++) {
1995               nrg_0 = ptr_r_0[bd] * ptr_r_0[bd] + ptr_i_0[bd] * ptr_i_0[bd];
1996               nrg_1 = ptr_r_1[bd] * ptr_r_1[bd] + ptr_i_1[bd] * ptr_i_1[bd];
1997               nrg_2 = ptr_r_2[bd] * ptr_r_2[bd] + ptr_i_2[bd] * ptr_i_2[bd];
1998               nrg_3 = ptr_r_3[bd] * ptr_r_3[bd] + ptr_i_3[bd] * ptr_i_3[bd];
1999               pstr_pvc_scr->pvc_qmf_high[ts * IXHEAACE_ESBR_PVC_NUM_QMF_BANDS + bd] =
2000                   (nrg_0 + nrg_1 + nrg_2 + nrg_3) / 4.0f;
2001             }
2002             WORD32 num_low_bands = (MAX_QMF_TIME_SLOTS >> 2);
2003             for (bd = 0; bd < num_low_bands; bd++) {
2004               pstr_pvc_scr->pvc_qmf_low[ts * num_low_bands + bd] =
2005                   pstr_pvc_scr->pvc_qmf_high[ts * IXHEAACE_ESBR_PVC_NUM_QMF_BANDS + bd];
2006             }
2007           }
2008           break;
2009         }
2010       }
2011       pstr_env_enc->pstr_pvc_enc->pvc_param.usac_indep_flag = pstr_sbr_bs->usac_indep_flag;
2012       err_code = ixheaace_pvc_encode_frame(
2013           pstr_env_enc->pstr_pvc_enc, (UWORD8)pstr_env_enc->str_sbr_hdr.sbr_pvc_mode,
2014           pstr_pvc_scr->pvc_qmf_low, pstr_pvc_scr->pvc_qmf_high,
2015           pstr_sbr_cfg->ptr_v_k_master[0],
2016           pstr_sbr_cfg->ptr_v_k_master[pstr_sbr_cfg->num_master] - 1);
2017       if (err_code) {
2018         return err_code;
2019       }
2020 
2021       memcpy(&pstr_env_ch[ch]->enc_env_data.pvc_info, &pstr_env_enc->pstr_pvc_enc->pvc_bs_info,
2022              sizeof(ixheaace_pvc_bs_info));
2023     }
2024 
2025     // COPY generated spectrum for inter-TES encoder
2026     if ((USAC_SBR == pstr_sbr_cfg->sbr_codec) && (1 == pstr_sbr_hdr->sbr_inter_tes_active)) {
2027       WORD32 ts, num_ts, delay;
2028       num_ts = pstr_env_ch[ch]->str_sbr_qmf.num_time_slots;
2029 
2030       ixheaace_str_inter_tes_params *pstr_tes_enc = &pstr_env_ch[ch]->str_inter_tes_enc;
2031       delay = pstr_tes_enc->op_delay + pstr_tes_enc->codec_delay + IXHEAACE_SBR_HF_ADJ_OFFSET;
2032       ts = 0;
2033       while (ts < num_ts) {
2034         memcpy(pstr_tes_enc->qmf_buf_real[delay + ts], pstr_sbr_extract_env->ptr_r_buffer[ts],
2035                IXHEAACE_QMF_CHANNELS * sizeof(pstr_tes_enc->qmf_buf_real[0][0]));
2036         memcpy(pstr_tes_enc->qmf_buf_imag[delay + ts], pstr_sbr_extract_env->ptr_i_buffer[ts],
2037                IXHEAACE_QMF_CHANNELS * sizeof(pstr_tes_enc->qmf_buf_imag[0][0]));
2038         ts++;
2039       }
2040     }
2041 
2042     ch++;
2043   }
2044   if ((pstr_sbr_cfg->sbr_codec == USAC_SBR) && (pstr_sbr_hdr->sbr_harmonic)) {
2045     WORD32 dft_hbe_flag = 0;
2046     WORD32 op_delay, codec_x_delay, num_time_slots;
2047     WORD32 esbr_hbe_delay_offsets = IXHEAACE_ESBR_HBE_DELAY_OFFSET;
2048     WORD32 oversampling_flag = 0;
2049     op_delay = IXHEAACE_OP_DELAY_OFFSET;
2050     codec_x_delay = IXHEAACE_ESBR_HBE_DELAY_OFFSET;
2051     if (pstr_sbr_cfg->sbr_ratio_idx == USAC_SBR_RATIO_INDEX_4_1) {
2052       op_delay = 2 * IXHEAACE_OP_DELAY_OFFSET;
2053       codec_x_delay = 2 * codec_x_delay;
2054       oversampling_flag = 1;
2055     }
2056     WORD32 eff_offset = op_delay + IXHEAACE_SBR_HF_ADJ_OFFSET;
2057     dft_hbe_flag = pstr_sbr_hdr->hq_esbr;
2058     ch = 0;
2059     while (ch < n_in_channels) {
2060       ixheaace_str_hbe_enc *pstr_hbe_enc = pstr_env_ch[ch]->pstr_hbe_enc;
2061       pstr_hbe_enc->pstr_hbe_txposer->oversampling_flag = oversampling_flag;
2062       num_time_slots =
2063           pstr_env_ch[ch]->str_sbr_qmf.num_time_slots * pstr_env_ch[ch]->str_sbr_qmf.rate;
2064 
2065       if (dft_hbe_flag == 1) {
2066         err_code = ixheaace_dft_hbe_apply(
2067             pstr_hbe_enc->pstr_hbe_txposer,
2068             pstr_hbe_enc->qmf_buf_real + eff_offset + esbr_hbe_delay_offsets,
2069             pstr_hbe_enc->qmf_buf_imag + eff_offset + esbr_hbe_delay_offsets, num_time_slots,
2070             pstr_hbe_enc->ph_vocod_qmf_real + eff_offset,
2071             pstr_hbe_enc->ph_vocod_qmf_imag + eff_offset,
2072             pstr_env_ch[ch]->enc_env_data.sbr_pitchin_bins, (FLOAT32 *)ptr_sbr_scratch);
2073         if (err_code) {
2074           return err_code;
2075         }
2076       } else {
2077         // size 4096 samples
2078         FLOAT32 *ptr_time_data = (FLOAT32 *)ptr_sbr_scratch;
2079         int cnt = 0;
2080         if (0 == ch) {
2081           while (cnt < IXHEAACE_MAX_NUM_SAMPLES) {
2082             ptr_time_data[cnt] = pstr_env_enc->ptr_hbe_resample_buf[2 * cnt];
2083             cnt++;
2084           }
2085         } else {
2086           while (cnt < IXHEAACE_MAX_NUM_SAMPLES) {
2087             ptr_time_data[cnt] = pstr_env_enc->ptr_hbe_resample_buf[2 * cnt + 1];
2088             cnt++;
2089           }
2090         }
2091 
2092         ixheaace_esbr_analysis_filt_block(&(pstr_hbe_enc->str_codec_qmf_bank),
2093                                           pstr_hbe_enc->str_codec_qmf_bank.pstr_qmf_dec_tabs,
2094                                           ptr_time_data, pstr_hbe_enc->qmf_buf_real,
2095                                           pstr_hbe_enc->qmf_buf_imag,
2096                                           op_delay + codec_x_delay + IXHEAACE_SBR_HF_ADJ_OFFSET);
2097 
2098         err_code = ixheaace_qmf_hbe_apply(
2099             pstr_hbe_enc->pstr_hbe_txposer,
2100             pstr_hbe_enc->qmf_buf_real + eff_offset + esbr_hbe_delay_offsets,
2101             pstr_hbe_enc->qmf_buf_imag + eff_offset + esbr_hbe_delay_offsets, num_time_slots,
2102             pstr_hbe_enc->ph_vocod_qmf_real + eff_offset,
2103             pstr_hbe_enc->ph_vocod_qmf_imag + eff_offset,
2104             pstr_env_ch[ch]->enc_env_data.sbr_pitchin_bins);
2105         if (err_code) {
2106           return err_code;
2107         }
2108 
2109         if (pstr_sbr_cfg->sbr_ratio_idx == USAC_SBR_RATIO_INDEX_4_1) {
2110           ixheaace_hbe_repl_spec(&pstr_hbe_enc->pstr_hbe_txposer->x_over_qmf[0],
2111                                  pstr_hbe_enc->ph_vocod_qmf_real + eff_offset,
2112                                  pstr_hbe_enc->ph_vocod_qmf_imag + eff_offset, num_time_slots,
2113                                  pstr_hbe_enc->pstr_hbe_txposer->max_stretch);
2114         }
2115       }
2116       ch++;
2117     }
2118   }
2119   if (pstr_ps_enc && pstr_synthesis_qmf_bank) {
2120     err_code = ixheaace_encode_ps_frame(
2121         pstr_ps_enc, pstr_env_ch[0]->str_sbr_extract_env.ptr_i_buffer,
2122         pstr_env_ch[0]->str_sbr_extract_env.ptr_r_buffer,
2123         pstr_env_ch[1]->str_sbr_extract_env.ptr_i_buffer,
2124         pstr_env_ch[1]->str_sbr_extract_env.ptr_r_buffer, ptr_sbr_tab->ptr_ps_tab, pstr_com_tab);
2125     if (err_code) {
2126       return err_code;
2127     }
2128     ixheaace_enc_synthesis_qmf_filtering(
2129         pstr_env_ch[0]->str_sbr_extract_env.ptr_r_buffer,
2130         pstr_env_ch[0]->str_sbr_extract_env.ptr_i_buffer, ptr_core_buf,
2131         (ixheaace_pstr_sbr_qmf_filter_bank)pstr_synthesis_qmf_bank);
2132   }
2133 
2134   ch = 0;
2135   while (ch < num_channels) {
2136     ixheaace_str_hbe_enc *pstr_hbe_enc = pstr_env_ch[ch]->pstr_hbe_enc;
2137     ixheaace_str_sbr_extr_env *pstr_sbr_extract_env = &(pstr_env_ch[ch]->str_sbr_extract_env);
2138 
2139     ixheaace_get_energy_from_cplx_qmf(
2140         pstr_sbr_extract_env->ptr_y_buffer + pstr_sbr_extract_env->y_buffer_write_offset,
2141         pstr_sbr_extract_env->ptr_r_buffer, pstr_sbr_extract_env->ptr_i_buffer,
2142         pstr_sbr_cfg->is_ld_sbr, pstr_env_ch[ch]->str_sbr_qmf.num_time_slots, samp_ratio_fac,
2143         pstr_hbe_enc,
2144         (IXHEAACE_OP_DELAY_OFFSET + IXHEAACE_ESBR_HBE_DELAY_OFFSET + IXHEAACE_SBR_HF_ADJ_OFFSET),
2145         pstr_sbr_hdr->sbr_harmonic);
2146 
2147     ixheaace_calculate_tonality_quotas(
2148         &pstr_env_ch[ch]->str_ton_corr, pstr_sbr_extract_env->ptr_r_buffer,
2149         pstr_sbr_extract_env->ptr_i_buffer,
2150         pstr_sbr_cfg->ptr_freq_band_tab[HI][pstr_sbr_cfg->num_scf[HI]],
2151         pstr_env_ch[ch]->str_sbr_qmf.num_time_slots, pstr_sbr_extract_env->time_step);
2152     if (pstr_sbr_cfg->is_ld_sbr) {
2153       ixheaace_detect_transient_eld(pstr_sbr_extract_env->ptr_y_buffer,
2154                                     &pstr_env_ch[ch]->str_sbr_trans_detector, transient_info[ch]);
2155     } else if (pstr_sbr_extract_env->time_step == 4) {
2156       ixheaace_detect_transient_4_1(pstr_sbr_extract_env->ptr_y_buffer,
2157                                     &pstr_env_ch[ch]->str_sbr_trans_detector, transient_info[ch],
2158                                     pstr_sbr_extract_env->time_step, pstr_sbr_cfg->sbr_codec);
2159     } else {
2160       ixheaace_detect_transient(pstr_sbr_extract_env->ptr_y_buffer,
2161                                 &pstr_env_ch[ch]->str_sbr_trans_detector, transient_info[ch],
2162                                 pstr_sbr_extract_env->time_step, pstr_sbr_cfg->sbr_codec);
2163     }
2164     if (transient_info[ch][1] == 0) {
2165       if (pstr_sbr_cfg->is_ld_sbr) {
2166         err_code = ixheaace_frame_splitter(
2167             pstr_sbr_extract_env->ptr_y_buffer, &pstr_env_ch[ch]->str_sbr_trans_detector,
2168             pstr_sbr_cfg->ptr_freq_band_tab[1], pstr_sbr_cfg->num_scf[1],
2169             pstr_sbr_extract_env->time_step, pstr_sbr_extract_env->time_slots, transient_info[ch],
2170             (FLOAT32 *)ptr_frame_splitter_scratch, pstr_sbr_cfg->is_ld_sbr);
2171       } else {
2172         err_code = ixheaace_frame_splitter(
2173             pstr_sbr_extract_env->ptr_y_buffer, &pstr_env_ch[ch]->str_sbr_trans_detector,
2174             pstr_sbr_cfg->ptr_freq_band_tab[1], pstr_sbr_cfg->num_scf[1],
2175             pstr_sbr_extract_env->time_step, pstr_sbr_extract_env->no_cols, transient_info[ch],
2176             (FLOAT32 *)ptr_frame_splitter_scratch, pstr_sbr_cfg->is_ld_sbr);
2177       }
2178       if (err_code) {
2179         return err_code;
2180       }
2181     }
2182     ch++;
2183   }
2184 
2185   if (stereo_mode == SBR_COUPLING) {
2186     if (transient_info[0][1] && transient_info[1][1]) {
2187       transient_info[0][0] = ixheaac_min32(transient_info[1][0], transient_info[0][0]);
2188 
2189       transient_info[1][0] = transient_info[0][0];
2190     } else if (transient_info[0][1] && !transient_info[1][1]) {
2191       transient_info[1][0] = transient_info[0][0];
2192     } else if (!transient_info[0][1] && transient_info[1][1]) {
2193       transient_info[0][0] = transient_info[1][0];
2194     } else {
2195       transient_info[0][0] = ixheaac_max32(transient_info[1][0], transient_info[0][0]);
2196 
2197       transient_info[1][0] = transient_info[0][0];
2198     }
2199   }
2200 
2201   err_code = ixheaace_frame_info_generator(
2202       &pstr_env_ch[0]->str_sbr_env_frame, pstr_env_ch[0]->str_sbr_extract_env.pre_transient_info,
2203       transient_info[0], ptr_v_tuning, ptr_sbr_tab->ptr_qmf_tab,
2204       pstr_env_ch[0]->str_sbr_qmf.num_time_slots, pstr_sbr_cfg->is_ld_sbr, &pstr_frame_info,
2205       flag_framelength_small);
2206   if (pstr_sbr_cfg->is_ld_sbr && transient_info[0][2]) {
2207     pstr_frame_info->short_env = pstr_frame_info->n_envelopes;
2208   }
2209   pstr_const_frame_info[0] = pstr_frame_info;
2210   if (err_code) {
2211     return err_code;
2212   }
2213 
2214   pstr_env_0->pstr_sbr_bs_grid = &pstr_env_ch[0]->str_sbr_env_frame.sbr_grid;
2215 
2216   for (ch = 0; ch < num_channels; ch++) {
2217     memset(
2218         ptr_noise_floor[ch], 0,
2219         IXHEAACE_MAX_CH_IN_BS_ELE * MAXIMUM_NUM_ENVELOPE_VALUES * sizeof(ptr_noise_floor[0][0]));
2220   }
2221 
2222   switch (stereo_mode) {
2223     case IXHEAACE_SBR_MODE_LEFT_RIGHT:
2224     case IXHEAACE_SBR_MODE_SWITCH_LRC:
2225 
2226       err_code = ixheaace_frame_info_generator(
2227           &pstr_env_ch[1]->str_sbr_env_frame,
2228           pstr_env_ch[1]->str_sbr_extract_env.pre_transient_info, transient_info[1], ptr_v_tuning,
2229           ptr_sbr_tab->ptr_qmf_tab, pstr_env_ch[1]->str_sbr_qmf.num_time_slots,
2230           pstr_sbr_cfg->is_ld_sbr, &pstr_frame_info, flag_framelength_small);
2231 
2232       if (pstr_sbr_cfg->is_ld_sbr && transient_info[1][2]) {
2233         pstr_frame_info->short_env = pstr_frame_info->n_envelopes;
2234       }
2235       pstr_const_frame_info[1] = pstr_frame_info;
2236       if (err_code) {
2237         return err_code;
2238       }
2239 
2240       pstr_env_1->pstr_sbr_bs_grid = &pstr_env_ch[1]->str_sbr_env_frame.sbr_grid;
2241 
2242       if (pstr_const_frame_info[0]->n_envelopes != pstr_const_frame_info[1]->n_envelopes) {
2243         stereo_mode = IXHEAACE_SBR_MODE_LEFT_RIGHT;
2244       } else {
2245         for (i = 0; i < pstr_const_frame_info[0]->n_envelopes + 1; i++) {
2246           if (pstr_const_frame_info[0]->borders[i] != pstr_const_frame_info[1]->borders[i]) {
2247             stereo_mode = IXHEAACE_SBR_MODE_LEFT_RIGHT;
2248             break;
2249           }
2250         }
2251 
2252         for (i = 0; i < pstr_const_frame_info[0]->n_envelopes; i++) {
2253           if (pstr_const_frame_info[0]->freq_res[i] != pstr_const_frame_info[1]->freq_res[i]) {
2254             stereo_mode = IXHEAACE_SBR_MODE_LEFT_RIGHT;
2255             break;
2256           }
2257         }
2258 
2259         if (pstr_const_frame_info[0]->short_env != pstr_const_frame_info[1]->short_env) {
2260           stereo_mode = IXHEAACE_SBR_MODE_LEFT_RIGHT;
2261         }
2262       }
2263       break;
2264     case SBR_COUPLING:
2265 
2266       pstr_const_frame_info[1] = pstr_const_frame_info[0];
2267 
2268       pstr_env_1->pstr_sbr_bs_grid = &pstr_env_ch[0]->str_sbr_env_frame.sbr_grid;
2269       break;
2270     case IXHEAACE_SBR_MODE_MONO:
2271       break;
2272   }
2273 
2274   for (ch = 0; ch < num_channels; ch++) {
2275     ixheaace_str_sbr_extr_env *pstr_sbr_extract_env = &(pstr_env_ch[ch]->str_sbr_extract_env);
2276 
2277     pstr_sbr_extract_env->pre_transient_info[0] = transient_info[ch][0];
2278     pstr_sbr_extract_env->pre_transient_info[1] = transient_info[ch][1];
2279     pstr_env_ch[ch]->enc_env_data.no_of_envelopes = n_envelopes[ch] =
2280         pstr_const_frame_info[ch]->n_envelopes;
2281 
2282     for (i = 0; i < n_envelopes[ch]; i++) {
2283       pstr_env_ch[ch]->enc_env_data.no_scf_bands[i] =
2284           (pstr_const_frame_info[ch]->freq_res[i] == FREQ_RES_HIGH
2285                ? pstr_sbr_cfg->num_scf[FREQ_RES_HIGH]
2286                : pstr_sbr_cfg->num_scf[FREQ_RES_LOW]);
2287     }
2288 
2289     if ((pstr_env_ch[ch]->enc_env_data.pstr_sbr_bs_grid->frame_type == IXHEAACE_FIXFIX) &&
2290         (n_envelopes[ch] == 1)) {
2291       if (pstr_sbr_cfg->is_ld_sbr) {
2292         pstr_env_ch[ch]->enc_env_data.curr_sbr_amp_res = IXHEAACE_SBR_AMP_RES_3_0;
2293       } else {
2294         pstr_env_ch[ch]->enc_env_data.curr_sbr_amp_res = IXHEAACE_SBR_AMP_RES_1_5;
2295       }
2296       if (pstr_env_ch[ch]->enc_env_data.init_sbr_amp_res !=
2297           pstr_env_ch[ch]->enc_env_data.curr_sbr_amp_res) {
2298         err_code = ixheaace_init_sbr_huffman_tabs(
2299             &pstr_env_ch[ch]->enc_env_data, &pstr_env_ch[ch]->str_sbr_code_env,
2300             &pstr_env_ch[ch]->str_sbr_code_noise_floor, IXHEAACE_SBR_AMP_RES_1_5,
2301             ptr_sbr_tab->ptr_sbr_huff_tab);
2302         if (err_code) {
2303           return err_code;
2304         }
2305         pstr_env_ch[ch]->sbr_amp_res_init = IXHEAACE_SBR_AMP_RES_1_5;
2306       }
2307     } else {
2308       if (pstr_sbr_hdr->sbr_amp_res != pstr_env_ch[ch]->enc_env_data.init_sbr_amp_res) {
2309         err_code = ixheaace_init_sbr_huffman_tabs(
2310             &pstr_env_ch[ch]->enc_env_data, &pstr_env_ch[ch]->str_sbr_code_env,
2311             &pstr_env_ch[ch]->str_sbr_code_noise_floor, pstr_sbr_hdr->sbr_amp_res,
2312             ptr_sbr_tab->ptr_sbr_huff_tab);
2313         if (err_code) {
2314           return err_code;
2315         }
2316         pstr_env_ch[ch]->sbr_amp_res_init = pstr_sbr_hdr->sbr_amp_res;
2317       }
2318     }
2319 
2320     ixheaace_ton_corr_param_extr(
2321         &pstr_env_ch[ch]->str_ton_corr, pstr_env_ch[ch]->enc_env_data.sbr_invf_mode_vec,
2322         ptr_noise_floor[ch], &pstr_env_ch[ch]->enc_env_data.add_harmonic_flag,
2323         pstr_env_ch[ch]->enc_env_data.add_harmonic, pstr_sbr_extract_env->envelope_compensation,
2324         pstr_const_frame_info[ch], transient_info[ch], pstr_sbr_cfg->ptr_freq_band_tab[HI],
2325         pstr_sbr_cfg->num_scf[HI], pstr_env_ch[ch]->enc_env_data.sbr_xpos_mode, ptr_sbr_scratch,
2326         pstr_sbr_cfg->is_ld_sbr);
2327 
2328     pstr_env_ch[ch]->enc_env_data.sbr_invf_mode =
2329         pstr_env_ch[ch]->enc_env_data.sbr_invf_mode_vec[0];
2330     pstr_env_ch[ch]->enc_env_data.noise_band_count =
2331         pstr_env_ch[ch]->str_ton_corr.sbr_noise_floor_est.num_of_noise_bands;
2332   }
2333 
2334   switch (stereo_mode) {
2335     case IXHEAACE_SBR_MODE_MONO:
2336       err_code = ixheaace_calculate_sbr_envelope(pstr_env_ch[0]->str_sbr_extract_env.ptr_y_buffer,
2337                                                  NULL, pstr_const_frame_info[0],
2338                                                  ptr_scale_factor_band_nrg[0], NULL, pstr_sbr_cfg,
2339                                                  pstr_env_ch[0], IXHEAACE_SBR_MODE_MONO, NULL);
2340 
2341       if (err_code) {
2342         return err_code;
2343       }
2344       break;
2345 
2346     case IXHEAACE_SBR_MODE_LEFT_RIGHT:
2347 
2348       err_code = ixheaace_calculate_sbr_envelope(pstr_env_ch[0]->str_sbr_extract_env.ptr_y_buffer,
2349                                                  NULL, pstr_const_frame_info[0],
2350                                                  ptr_scale_factor_band_nrg[0], NULL, pstr_sbr_cfg,
2351                                                  pstr_env_ch[0], IXHEAACE_SBR_MODE_MONO, NULL);
2352       if (err_code) {
2353         return err_code;
2354       }
2355 
2356       err_code = ixheaace_calculate_sbr_envelope(pstr_env_ch[1]->str_sbr_extract_env.ptr_y_buffer,
2357                                                  NULL, pstr_const_frame_info[1],
2358                                                  ptr_scale_factor_band_nrg[1], NULL, pstr_sbr_cfg,
2359                                                  pstr_env_ch[1], IXHEAACE_SBR_MODE_MONO, NULL);
2360 
2361       if (err_code) {
2362         return err_code;
2363       }
2364 
2365       break;
2366 
2367     case SBR_COUPLING:
2368 
2369       err_code = ixheaace_calculate_sbr_envelope(
2370           pstr_env_ch[0]->str_sbr_extract_env.ptr_y_buffer,
2371           pstr_env_ch[1]->str_sbr_extract_env.ptr_y_buffer, pstr_const_frame_info[0],
2372           ptr_scale_factor_band_nrg[0], ptr_scale_factor_band_nrg[1], pstr_sbr_cfg,
2373           pstr_env_ch[0], SBR_COUPLING, &max_quant_error);
2374       if (err_code) {
2375         return err_code;
2376       }
2377       break;
2378 
2379     case IXHEAACE_SBR_MODE_SWITCH_LRC:
2380       err_code = ixheaace_calculate_sbr_envelope(pstr_env_ch[0]->str_sbr_extract_env.ptr_y_buffer,
2381                                                  NULL, pstr_const_frame_info[0],
2382                                                  ptr_scale_factor_band_nrg[0], NULL, pstr_sbr_cfg,
2383                                                  pstr_env_ch[0], IXHEAACE_SBR_MODE_MONO, NULL);
2384       if (err_code) {
2385         return err_code;
2386       }
2387 
2388       err_code = ixheaace_calculate_sbr_envelope(pstr_env_ch[1]->str_sbr_extract_env.ptr_y_buffer,
2389                                                  NULL, pstr_const_frame_info[1],
2390                                                  ptr_scale_factor_band_nrg[1], NULL, pstr_sbr_cfg,
2391                                                  pstr_env_ch[1], IXHEAACE_SBR_MODE_MONO, NULL);
2392       if (err_code) {
2393         return err_code;
2394       }
2395 
2396       err_code = ixheaace_calculate_sbr_envelope(
2397           pstr_env_ch[0]->str_sbr_extract_env.ptr_y_buffer,
2398           pstr_env_ch[1]->str_sbr_extract_env.ptr_y_buffer, pstr_const_frame_info[0],
2399           ptr_sfb_nrg_coupling[0], ptr_sfb_nrg_coupling[1], pstr_sbr_cfg, pstr_env_ch[0],
2400           SBR_COUPLING, &max_quant_error);
2401       if (err_code) {
2402         return err_code;
2403       }
2404       break;
2405   }
2406 
2407   switch (stereo_mode) {
2408     case IXHEAACE_SBR_MODE_MONO:
2409 
2410       ixheaace_sbr_noise_floor_levels_quantisation(ptr_noise_level[0], ptr_noise_floor[0], 0);
2411 
2412       err_code = ixheaace_code_envelope(
2413           ptr_noise_level[0], res, &pstr_env_ch[0]->str_sbr_code_noise_floor,
2414           pstr_env_0->domain_vec_noise, 0, (pstr_const_frame_info[0]->n_envelopes > 1 ? 2 : 1), 0,
2415           pstr_sbr_bs->header_active, pstr_sbr_bs->usac_indep_flag, pstr_sbr_cfg->is_ld_sbr);
2416       if (err_code) {
2417         return err_code;
2418       }
2419 
2420       break;
2421 
2422     case IXHEAACE_SBR_MODE_LEFT_RIGHT:
2423       // We have a error checks for Number of channels to ensure memory is assigned to
2424       // ptr_noise_floor[]. However, MSVS static analysis is marking this as a potential error.
2425       // So, suppressed this in source
2426       ixheaace_sbr_noise_floor_levels_quantisation(ptr_noise_level[0], ptr_noise_floor[0], 0);
2427 
2428       err_code = ixheaace_code_envelope(
2429           ptr_noise_level[0], res, &pstr_env_ch[0]->str_sbr_code_noise_floor,
2430           pstr_env_0->domain_vec_noise, 0, (pstr_const_frame_info[0]->n_envelopes > 1 ? 2 : 1), 0,
2431           pstr_sbr_bs->header_active, pstr_sbr_bs->usac_indep_flag, pstr_sbr_cfg->is_ld_sbr);
2432       if (err_code) {
2433         return err_code;
2434       }
2435 
2436       ixheaace_sbr_noise_floor_levels_quantisation(ptr_noise_level[1], ptr_noise_floor[1], 0);
2437 
2438       err_code = ixheaace_code_envelope(
2439           ptr_noise_level[1], res, &pstr_env_ch[1]->str_sbr_code_noise_floor,
2440           pstr_env_1->domain_vec_noise, 0, (pstr_const_frame_info[1]->n_envelopes > 1 ? 2 : 1), 0,
2441           pstr_sbr_bs->header_active, pstr_sbr_bs->usac_indep_flag, pstr_sbr_cfg->is_ld_sbr);
2442       if (err_code) {
2443         return err_code;
2444       }
2445 
2446       break;
2447 
2448     case SBR_COUPLING:
2449       ixheaace_couple_noise_floor(ptr_noise_floor[0], ptr_noise_floor[1]);
2450 
2451       ixheaace_sbr_noise_floor_levels_quantisation(ptr_noise_level[0], ptr_noise_floor[0], 0);
2452 
2453       err_code = ixheaace_code_envelope(
2454           ptr_noise_level[0], res, &pstr_env_ch[0]->str_sbr_code_noise_floor,
2455           pstr_env_0->domain_vec_noise, 1, (pstr_const_frame_info[0]->n_envelopes > 1 ? 2 : 1), 0,
2456           pstr_sbr_bs->header_active, pstr_sbr_bs->usac_indep_flag, pstr_sbr_cfg->is_ld_sbr);
2457       if (err_code) {
2458         return err_code;
2459       }
2460 
2461       ixheaace_sbr_noise_floor_levels_quantisation(ptr_noise_level[1], ptr_noise_floor[1], 1);
2462 
2463       err_code = ixheaace_code_envelope(
2464           ptr_noise_level[1], res, &pstr_env_ch[1]->str_sbr_code_noise_floor,
2465           pstr_env_1->domain_vec_noise, 1, (pstr_const_frame_info[1]->n_envelopes > 1 ? 2 : 1), 1,
2466           pstr_sbr_bs->header_active, pstr_sbr_bs->usac_indep_flag, pstr_sbr_cfg->is_ld_sbr);
2467       if (err_code) {
2468         return err_code;
2469       }
2470 
2471       break;
2472 
2473     case IXHEAACE_SBR_MODE_SWITCH_LRC:
2474 
2475       ixheaace_sbr_noise_floor_levels_quantisation(ptr_noise_level[0], ptr_noise_floor[0], 0);
2476 
2477       ixheaace_sbr_noise_floor_levels_quantisation(ptr_noise_level[1], ptr_noise_floor[1], 0);
2478 
2479       ixheaace_couple_noise_floor(ptr_noise_floor[0], ptr_noise_floor[1]);
2480 
2481       ixheaace_sbr_noise_floor_levels_quantisation(ptr_noise_lvl_coupling[0], ptr_noise_floor[0],
2482                                                    0);
2483 
2484       ixheaace_sbr_noise_floor_levels_quantisation(ptr_noise_lvl_coupling[1], ptr_noise_floor[1],
2485                                                    1);
2486       break;
2487   }
2488 
2489   switch (stereo_mode) {
2490     case IXHEAACE_SBR_MODE_MONO:
2491 
2492       pstr_sbr_hdr->coupling = 0;
2493       pstr_env_0->balance = 0;
2494 
2495       err_code = ixheaace_code_envelope(
2496           ptr_scale_factor_band_nrg[0], pstr_const_frame_info[0]->freq_res,
2497           &pstr_env_ch[0]->str_sbr_code_env, pstr_env_0->domain_vec, pstr_sbr_hdr->coupling,
2498           pstr_const_frame_info[0]->n_envelopes, 0, pstr_sbr_bs->header_active,
2499           pstr_sbr_bs->usac_indep_flag, pstr_sbr_cfg->is_ld_sbr);
2500       if (err_code) {
2501         return err_code;
2502       }
2503       break;
2504 
2505     case IXHEAACE_SBR_MODE_LEFT_RIGHT:
2506 
2507       pstr_sbr_hdr->coupling = 0;
2508 
2509       pstr_env_0->balance = 0;
2510       pstr_env_1->balance = 0;
2511 
2512       err_code = ixheaace_code_envelope(
2513           ptr_scale_factor_band_nrg[0], pstr_const_frame_info[0]->freq_res,
2514           &pstr_env_ch[0]->str_sbr_code_env, pstr_env_0->domain_vec, pstr_sbr_hdr->coupling,
2515           pstr_const_frame_info[0]->n_envelopes, 0, pstr_sbr_bs->header_active,
2516           pstr_sbr_bs->usac_indep_flag, pstr_sbr_cfg->is_ld_sbr);
2517       if (err_code) {
2518         return err_code;
2519       }
2520 
2521       err_code = ixheaace_code_envelope(
2522           ptr_scale_factor_band_nrg[1], pstr_const_frame_info[1]->freq_res,
2523           &pstr_env_ch[1]->str_sbr_code_env, pstr_env_1->domain_vec, pstr_sbr_hdr->coupling,
2524           pstr_const_frame_info[1]->n_envelopes, 0, pstr_sbr_bs->header_active,
2525           pstr_sbr_bs->usac_indep_flag, pstr_sbr_cfg->is_ld_sbr);
2526       if (err_code) {
2527         return err_code;
2528       }
2529       break;
2530 
2531     case SBR_COUPLING:
2532 
2533       pstr_sbr_hdr->coupling = 1;
2534       pstr_env_0->balance = 0;
2535       pstr_env_1->balance = 1;
2536 
2537       err_code = ixheaace_code_envelope(
2538           ptr_scale_factor_band_nrg[0], pstr_const_frame_info[0]->freq_res,
2539           &pstr_env_ch[0]->str_sbr_code_env, pstr_env_0->domain_vec, pstr_sbr_hdr->coupling,
2540           pstr_const_frame_info[0]->n_envelopes, 0, pstr_sbr_bs->header_active,
2541           pstr_sbr_bs->usac_indep_flag, pstr_sbr_cfg->is_ld_sbr);
2542       if (err_code) {
2543         return err_code;
2544       }
2545 
2546       err_code = ixheaace_code_envelope(
2547           ptr_scale_factor_band_nrg[1], pstr_const_frame_info[1]->freq_res,
2548           &pstr_env_ch[1]->str_sbr_code_env, pstr_env_1->domain_vec, pstr_sbr_hdr->coupling,
2549           pstr_const_frame_info[1]->n_envelopes, 1, pstr_sbr_bs->header_active,
2550           pstr_sbr_bs->usac_indep_flag, pstr_sbr_cfg->is_ld_sbr);
2551       if (err_code) {
2552         return err_code;
2553       }
2554       break;
2555 
2556     case IXHEAACE_SBR_MODE_SWITCH_LRC: {
2557       WORD32 payloadbits_lr;
2558       WORD32 payloadbits_coupling;
2559 
2560       WORD32 scale_factor_band_nrg_prev_temp[IXHEAACE_MAX_CH_IN_BS_ELE][MAXIMUM_FREQ_COEFFS];
2561       WORD32 noise_prev_temp[IXHEAACE_MAX_CH_IN_BS_ELE][MAXIMUM_NUM_NOISE_COEFFS];
2562       WORD32 up_date_nrg_temp[IXHEAACE_MAX_CH_IN_BS_ELE];
2563       WORD32 up_date_noise_temp[IXHEAACE_MAX_CH_IN_BS_ELE];
2564       WORD32 domain_vec_temp[IXHEAACE_MAX_CH_IN_BS_ELE][IXHEAACE_MAX_ENV];
2565       WORD32 domain_vec_noise_temp[IXHEAACE_MAX_CH_IN_BS_ELE][IXHEAACE_MAX_ENV];
2566 
2567       WORD32 temp_flag_right = 0;
2568       WORD32 temp_flag_left = 0;
2569 
2570       ch = 0;
2571       while (ch < num_channels) {
2572         memcpy(scale_factor_band_nrg_prev_temp[ch],
2573                pstr_env_ch[ch]->str_sbr_code_env.sfb_nrg_prev,
2574                MAXIMUM_FREQ_COEFFS * sizeof(scale_factor_band_nrg_prev_temp[0][0]));
2575 
2576         memcpy(noise_prev_temp[ch], pstr_env_ch[ch]->str_sbr_code_noise_floor.sfb_nrg_prev,
2577                MAXIMUM_NUM_NOISE_COEFFS * sizeof(noise_prev_temp[0][0]));
2578 
2579         up_date_nrg_temp[ch] = pstr_env_ch[ch]->str_sbr_code_env.update;
2580         up_date_noise_temp[ch] = pstr_env_ch[ch]->str_sbr_code_noise_floor.update;
2581 
2582         if (pstr_sbr_hdr->prev_coupling) {
2583           pstr_env_ch[ch]->str_sbr_code_env.update = 0;
2584           pstr_env_ch[ch]->str_sbr_code_noise_floor.update = 0;
2585         }
2586         ch++;
2587       }
2588 
2589       err_code = ixheaace_code_envelope(
2590           ptr_scale_factor_band_nrg[0], pstr_const_frame_info[0]->freq_res,
2591           &pstr_env_ch[0]->str_sbr_code_env, pstr_env_0->domain_vec, 0,
2592           pstr_const_frame_info[0]->n_envelopes, 0, pstr_sbr_bs->header_active,
2593           pstr_sbr_bs->usac_indep_flag, pstr_sbr_cfg->is_ld_sbr);
2594       if (err_code) {
2595         return err_code;
2596       }
2597 
2598       err_code = ixheaace_code_envelope(
2599           ptr_scale_factor_band_nrg[1], pstr_const_frame_info[1]->freq_res,
2600           &pstr_env_ch[1]->str_sbr_code_env, pstr_env_1->domain_vec, 0,
2601           pstr_const_frame_info[1]->n_envelopes, 0, pstr_sbr_bs->header_active,
2602           pstr_sbr_bs->usac_indep_flag, pstr_sbr_cfg->is_ld_sbr);
2603       if (err_code) {
2604         return err_code;
2605       }
2606 
2607       c = 0;
2608       i = 0;
2609       while (i < n_envelopes[0]) {
2610         for (j = 0; j < pstr_env_0->no_scf_bands[i]; j++) {
2611           pstr_env_0->ienvelope[i][j] = ptr_scale_factor_band_nrg[0][c];
2612           pstr_env_1->ienvelope[i][j] = ptr_scale_factor_band_nrg[1][c];
2613 
2614           c++;
2615         }
2616         i++;
2617       }
2618 
2619       err_code = ixheaace_code_envelope(
2620           ptr_noise_level[0], res, &pstr_env_ch[0]->str_sbr_code_noise_floor,
2621           pstr_env_0->domain_vec_noise, 0, (pstr_const_frame_info[0]->n_envelopes > 1 ? 2 : 1), 0,
2622           pstr_sbr_bs->header_active, pstr_sbr_bs->usac_indep_flag, pstr_sbr_cfg->is_ld_sbr);
2623       if (err_code) {
2624         return err_code;
2625       }
2626 
2627       i = 0;
2628       while (i < MAXIMUM_NUM_NOISE_VALUES) {
2629         pstr_env_0->noise_level[i] = ptr_noise_level[0][i];
2630         i++;
2631       }
2632 
2633       err_code = ixheaace_code_envelope(
2634           ptr_noise_level[1], res, &pstr_env_ch[1]->str_sbr_code_noise_floor,
2635           pstr_env_1->domain_vec_noise, 0, (pstr_const_frame_info[1]->n_envelopes > 1 ? 2 : 1), 0,
2636           pstr_sbr_bs->header_active, pstr_sbr_bs->usac_indep_flag, pstr_sbr_cfg->is_ld_sbr);
2637       if (err_code) {
2638         return err_code;
2639       }
2640 
2641       i = 0;
2642       while (i < MAXIMUM_NUM_NOISE_VALUES) {
2643         pstr_env_1->noise_level[i] = ptr_noise_level[1][i];
2644         i++;
2645       }
2646 
2647       pstr_sbr_hdr->coupling = 0;
2648       pstr_env_0->balance = 0;
2649       pstr_env_1->balance = 0;
2650 
2651       err_code = ixheaace_count_sbr_channel_pair_element(
2652           pstr_sbr_hdr, pstr_sbr_bs, &pstr_env_ch[0]->enc_env_data, &pstr_env_ch[1]->enc_env_data,
2653           pstr_com_data, ptr_sbr_tab, pstr_sbr_cfg->sbr_codec, pstr_sbr_cfg->is_esbr, &str_esbr,
2654           &payloadbits_lr);
2655       if (err_code) {
2656         return err_code;
2657       }
2658 
2659       for (ch = 0; ch < num_channels; ch++) {
2660         WORD32 itmp;
2661 
2662         for (i = 0; i < MAXIMUM_FREQ_COEFFS; i++) {
2663           itmp = pstr_env_ch[ch]->str_sbr_code_env.sfb_nrg_prev[i];
2664           pstr_env_ch[ch]->str_sbr_code_env.sfb_nrg_prev[i] =
2665               scale_factor_band_nrg_prev_temp[ch][i];
2666           scale_factor_band_nrg_prev_temp[ch][i] = itmp;
2667         }
2668 
2669         for (i = 0; i < MAXIMUM_NUM_NOISE_COEFFS; i++) {
2670           itmp = pstr_env_ch[ch]->str_sbr_code_noise_floor.sfb_nrg_prev[i];
2671           pstr_env_ch[ch]->str_sbr_code_noise_floor.sfb_nrg_prev[i] = noise_prev_temp[ch][i];
2672           noise_prev_temp[ch][i] = itmp;
2673         }
2674 
2675         itmp = pstr_env_ch[ch]->str_sbr_code_env.update;
2676         pstr_env_ch[ch]->str_sbr_code_env.update = up_date_nrg_temp[ch];
2677         up_date_nrg_temp[ch] = itmp;
2678 
2679         itmp = pstr_env_ch[ch]->str_sbr_code_noise_floor.update;
2680         pstr_env_ch[ch]->str_sbr_code_noise_floor.update = up_date_noise_temp[ch];
2681         up_date_noise_temp[ch] = itmp;
2682 
2683         memcpy(domain_vec_temp[ch], pstr_env_ch[ch]->enc_env_data.domain_vec,
2684                sizeof(domain_vec_temp[0][0]) * IXHEAACE_MAX_ENV);
2685 
2686         memcpy(domain_vec_noise_temp[ch], pstr_env_ch[ch]->enc_env_data.domain_vec_noise,
2687                sizeof(domain_vec_noise_temp[0][0]) * IXHEAACE_MAX_ENV);
2688 
2689         if (!pstr_sbr_hdr->prev_coupling) {
2690           pstr_env_ch[ch]->str_sbr_code_env.update = 0;
2691           pstr_env_ch[ch]->str_sbr_code_noise_floor.update = 0;
2692         }
2693       }
2694 
2695       err_code = ixheaace_code_envelope(
2696           ptr_sfb_nrg_coupling[0], pstr_const_frame_info[0]->freq_res,
2697           &pstr_env_ch[0]->str_sbr_code_env, pstr_env_0->domain_vec, 1,
2698           pstr_const_frame_info[0]->n_envelopes, 0, pstr_sbr_bs->header_active,
2699           pstr_sbr_bs->usac_indep_flag, pstr_sbr_cfg->is_ld_sbr);
2700       if (err_code) {
2701         return err_code;
2702       }
2703 
2704       err_code = ixheaace_code_envelope(
2705           ptr_sfb_nrg_coupling[1], pstr_const_frame_info[1]->freq_res,
2706           &pstr_env_ch[1]->str_sbr_code_env, pstr_env_1->domain_vec, 1,
2707           pstr_const_frame_info[1]->n_envelopes, 1, pstr_sbr_bs->header_active,
2708           pstr_sbr_bs->usac_indep_flag, pstr_sbr_cfg->is_ld_sbr);
2709       if (err_code) {
2710         return err_code;
2711       }
2712 
2713       c = 0;
2714       i = 0;
2715       while (i < n_envelopes[0]) {
2716         for (j = 0; j < pstr_env_0->no_scf_bands[i]; j++) {
2717           pstr_env_0->ienvelope[i][j] = ptr_sfb_nrg_coupling[0][c];
2718           pstr_env_1->ienvelope[i][j] = ptr_sfb_nrg_coupling[1][c];
2719           c++;
2720         }
2721         i++;
2722       }
2723 
2724       err_code = ixheaace_code_envelope(
2725           ptr_noise_lvl_coupling[0], res, &pstr_env_ch[0]->str_sbr_code_noise_floor,
2726           pstr_env_0->domain_vec_noise, 1, (pstr_const_frame_info[0]->n_envelopes > 1 ? 2 : 1), 0,
2727           pstr_sbr_bs->header_active, pstr_sbr_bs->usac_indep_flag, pstr_sbr_cfg->is_ld_sbr);
2728       if (err_code) {
2729         return err_code;
2730       }
2731 
2732       for (i = 0; i < MAXIMUM_NUM_NOISE_VALUES; i++) {
2733         pstr_env_0->noise_level[i] = ptr_noise_lvl_coupling[0][i];
2734       }
2735 
2736       err_code = ixheaace_code_envelope(
2737           ptr_noise_lvl_coupling[1], res, &pstr_env_ch[1]->str_sbr_code_noise_floor,
2738           pstr_env_1->domain_vec_noise, 1, ((pstr_const_frame_info[1]->n_envelopes > 1) ? 2 : 1),
2739           1, pstr_sbr_bs->header_active, pstr_sbr_bs->usac_indep_flag, pstr_sbr_cfg->is_ld_sbr);
2740       if (err_code) {
2741         return err_code;
2742       }
2743 
2744       for (i = 0; i < MAXIMUM_NUM_NOISE_VALUES; i++) {
2745         pstr_env_1->noise_level[i] = ptr_noise_lvl_coupling[1][i];
2746       }
2747 
2748       pstr_sbr_hdr->coupling = 1;
2749 
2750       pstr_env_0->balance = 0;
2751       pstr_env_1->balance = 1;
2752 
2753       temp_flag_left = pstr_env_0->add_harmonic_flag;
2754       temp_flag_right = pstr_env_1->add_harmonic_flag;
2755 
2756       err_code = ixheaace_count_sbr_channel_pair_element(
2757           pstr_sbr_hdr, pstr_sbr_bs, &pstr_env_ch[0]->enc_env_data, &pstr_env_ch[1]->enc_env_data,
2758           pstr_com_data, ptr_sbr_tab, pstr_sbr_cfg->sbr_codec, pstr_sbr_cfg->is_esbr, &str_esbr,
2759           &payloadbits_coupling);
2760       if (err_code) {
2761         return err_code;
2762       }
2763 
2764       pstr_env_0->add_harmonic_flag = temp_flag_left;
2765       pstr_env_1->add_harmonic_flag = temp_flag_right;
2766 
2767       if (payloadbits_coupling < payloadbits_lr) {
2768         ch = 0;
2769         while (ch < num_channels) {
2770           memcpy(ptr_scale_factor_band_nrg[ch], ptr_sfb_nrg_coupling[ch],
2771                  MAXIMUM_NUM_ENVELOPE_VALUES * sizeof(ptr_scale_factor_band_nrg[0][0]));
2772 
2773           memcpy(ptr_noise_level[ch], ptr_noise_lvl_coupling[ch],
2774                  MAXIMUM_NUM_NOISE_VALUES * sizeof(ptr_noise_level[0][0]));
2775           ch++;
2776         }
2777 
2778         pstr_sbr_hdr->coupling = 1;
2779         pstr_env_0->balance = 0;
2780         pstr_env_1->balance = 1;
2781       } else {
2782         ch = 0;
2783         while (ch < num_channels) {
2784           memcpy(pstr_env_ch[ch]->str_sbr_code_env.sfb_nrg_prev,
2785                  scale_factor_band_nrg_prev_temp[ch],
2786                  MAXIMUM_FREQ_COEFFS * sizeof(scale_factor_band_nrg_prev_temp[0][0]));
2787 
2788           pstr_env_ch[ch]->str_sbr_code_env.update = up_date_nrg_temp[ch];
2789 
2790           memcpy(pstr_env_ch[ch]->str_sbr_code_noise_floor.sfb_nrg_prev, noise_prev_temp[ch],
2791                  MAXIMUM_NUM_NOISE_COEFFS * sizeof(noise_prev_temp[0][0]));
2792 
2793           memcpy(pstr_env_ch[ch]->enc_env_data.domain_vec, domain_vec_temp[ch],
2794                  sizeof(domain_vec_temp[0][0]) * IXHEAACE_MAX_ENV);
2795 
2796           memcpy(pstr_env_ch[ch]->enc_env_data.domain_vec_noise, domain_vec_noise_temp[ch],
2797                  sizeof(domain_vec_noise_temp[0][0]) * IXHEAACE_MAX_ENV);
2798 
2799           pstr_env_ch[ch]->str_sbr_code_noise_floor.update = up_date_noise_temp[ch];
2800           ch++;
2801         }
2802 
2803         pstr_sbr_hdr->coupling = 0;
2804         pstr_env_0->balance = 0;
2805         pstr_env_1->balance = 0;
2806       }
2807     } break;
2808   }
2809 
2810   if (num_channels == 1) {
2811     if (pstr_env_0->domain_vec[0] == TIME) {
2812       pstr_env_ch[0]->str_sbr_code_env.df_edge_incr_fac++;
2813     } else {
2814       pstr_env_ch[0]->str_sbr_code_env.df_edge_incr_fac = 0;
2815     }
2816   } else {
2817     if (pstr_env_0->domain_vec[0] == TIME || pstr_env_1->domain_vec[0] == TIME) {
2818       pstr_env_ch[0]->str_sbr_code_env.df_edge_incr_fac++;
2819       pstr_env_ch[1]->str_sbr_code_env.df_edge_incr_fac++;
2820     } else {
2821       pstr_env_ch[0]->str_sbr_code_env.df_edge_incr_fac = 0;
2822       pstr_env_ch[1]->str_sbr_code_env.df_edge_incr_fac = 0;
2823     }
2824   }
2825 
2826   for (ch = 0; ch < num_channels; ch++) {
2827     c = 0;
2828     i = 0;
2829 
2830     while (i < n_envelopes[ch]) {
2831       for (j = 0; j < pstr_env_ch[ch]->enc_env_data.no_scf_bands[i]; j++) {
2832         pstr_env_ch[ch]->enc_env_data.ienvelope[i][j] = ptr_scale_factor_band_nrg[ch][c];
2833         c++;
2834       }
2835       i++;
2836     }
2837 
2838     i = 0;
2839     while (i < MAXIMUM_NUM_NOISE_VALUES) {
2840       pstr_env_ch[ch]->enc_env_data.noise_level[i] = ptr_noise_level[ch][i];
2841       i++;
2842     }
2843   }
2844   if ((USAC_SBR == pstr_sbr_cfg->sbr_codec) && (1 == pstr_sbr_hdr->sbr_inter_tes_active)) {
2845     // inter-TES encoder
2846     WORD32 idx;
2847     for (ch = 0; ch < num_channels; ch++) {
2848       ixheaace_str_inter_tes_params *pstr_tes_enc = &pstr_env_ch[ch]->str_inter_tes_enc;
2849 
2850       pstr_tes_enc->num_if_bands = pstr_env_ch[ch]->enc_env_data.noise_band_count;
2851       pstr_tes_enc->sub_band_start = pstr_sbr_cfg->ptr_freq_band_tab[LO][0];
2852       pstr_tes_enc->sub_band_end = pstr_sbr_cfg->ptr_freq_band_tab[LO][pstr_sbr_cfg->num_scf[LO]];
2853       pstr_tes_enc->num_mf_bands = pstr_sbr_cfg->num_master;
2854       pstr_tes_enc->out_fs = pstr_sbr_cfg->sample_freq;
2855       pstr_tes_enc->num_env = pstr_env_ch[ch]->str_sbr_env_frame.sbr_grid.bs_num_env;
2856 
2857       for (idx = 0; idx < (IXHEAACE_MAX_ENV + 1); idx++) {
2858         pstr_tes_enc->border_vec[idx] = (WORD16)pstr_const_frame_info[ch]->borders[idx];
2859       }
2860 
2861       for (idx = 0; idx < (MAXIMUM_FREQ_COEFFS + 1); idx++) {
2862         pstr_tes_enc->f_master_tbl[idx] = (WORD16)((UWORD16)pstr_sbr_cfg->ptr_v_k_master[idx]);
2863       }
2864 
2865       for (idx = 0; idx < MAXIMUM_NUM_NOISE_VALUES; idx++) {
2866         pstr_tes_enc->inv_filt_mode[idx] =
2867             (WORD32)pstr_env_ch[ch]->enc_env_data.sbr_invf_mode_vec[idx];
2868         pstr_tes_enc->invf_band_tbl[idx] =
2869             (WORD16)pstr_env_ch[ch]->str_ton_corr.sbr_noise_floor_est.s_freq_qmf_band_tbl[idx];
2870       }
2871       pstr_tes_enc->invf_band_tbl[MAXIMUM_NUM_NOISE_VALUES] =
2872           (WORD16)pstr_env_ch[ch]
2873               ->str_ton_corr.sbr_noise_floor_est.s_freq_qmf_band_tbl[MAXIMUM_NUM_NOISE_VALUES];
2874 
2875       err_code = ixheaace_process_inter_tes(pstr_tes_enc, ptr_sbr_scratch);
2876       if (err_code) {
2877         return err_code;
2878       }
2879 
2880       WORD32 ts, num_ts, delay;
2881       num_ts = pstr_env_ch[ch]->str_sbr_qmf.num_time_slots;
2882 
2883       delay = pstr_tes_enc->op_delay + pstr_tes_enc->codec_delay + IXHEAACE_SBR_HF_ADJ_OFFSET;
2884 
2885       for (ts = 0; ts < delay; ts++) {
2886         memcpy(pstr_tes_enc->qmf_buf_real[ts], pstr_tes_enc->qmf_buf_real[num_ts + ts],
2887                IXHEAACE_QMF_CHANNELS * sizeof(pstr_tes_enc->qmf_buf_real[0][0]));
2888         memcpy(pstr_tes_enc->qmf_buf_imag[ts], pstr_tes_enc->qmf_buf_imag[num_ts + ts],
2889                IXHEAACE_QMF_CHANNELS * sizeof(pstr_tes_enc->qmf_buf_imag[0][0]));
2890       }
2891     }
2892   }
2893   // Pitch detection, pre-processing detection and oversampling decision making
2894   if ((1 == pstr_sbr_cfg->is_esbr) && (pstr_sbr_cfg->sbr_codec == HEAAC_SBR)) {
2895     err_code = ixheaace_update_esbr_ext_data(
2896         ptr_in_time, pstr_sbr_cfg, pstr_env_ch[0]->str_sbr_extract_env.ptr_r_buffer[0], &str_esbr,
2897         transient_info, ptr_sbr_tab, pstr_sbr_hdr->coupling, time_sn_stride, 2048);
2898     if (err_code) return err_code;
2899   }
2900 
2901   if ((pstr_sbr_cfg->sbr_codec == USAC_SBR) && (pstr_sbr_hdr->sbr_harmonic)) {
2902     ixheaace_update_harmonic_sbr_data(transient_info, pstr_sbr_hdr->coupling,
2903                                       &pstr_env_ch[0], num_channels);
2904   }
2905   if (num_channels == 2) {
2906     WORD32 num_bits;
2907     pstr_env_0->usac_indep_flag = pstr_sbr_bs->usac_indep_flag;
2908     pstr_env_1->usac_indep_flag = pstr_sbr_bs->usac_indep_flag;
2909     err_code = ixheaace_write_env_channel_pair_element(
2910         pstr_sbr_hdr, pstr_sbr_bs, &pstr_env_ch[0]->enc_env_data, &pstr_env_ch[1]->enc_env_data,
2911         pstr_com_data, ptr_sbr_tab, pstr_sbr_cfg->sbr_codec, pstr_sbr_cfg->is_esbr, &str_esbr,
2912         &num_bits);
2913     if (err_code) {
2914       return err_code;
2915     }
2916   } else {
2917     WORD32 num_bits;
2918     pstr_env_0->sbr_pvc_mode = pstr_sbr_hdr->sbr_pvc_mode;
2919     pstr_env_0->sbr_sinusoidal_pos_flag = 0;
2920     pstr_env_0->usac_indep_flag = pstr_sbr_bs->usac_indep_flag;
2921 
2922     err_code = ixheaace_write_env_single_channel_element(
2923         pstr_sbr_hdr, pstr_sbr_bs, &pstr_env_ch[0]->enc_env_data, pstr_ps_enc, pstr_com_data,
2924         ptr_sbr_tab, pstr_sbr_cfg->sbr_codec, pstr_sbr_cfg->is_esbr, &str_esbr, &num_bits);
2925     if (err_code) {
2926       return err_code;
2927     }
2928   }
2929 
2930   ch = 0;
2931   while (ch < num_channels) {
2932     ixheaace_str_sbr_extr_env *pstr_sbr_extract_env = &(pstr_env_ch[ch]->str_sbr_extract_env);
2933     for (i = 0; i < pstr_sbr_extract_env->y_buffer_write_offset; i++) {
2934       FLOAT32 *ptr_tmp;
2935       ptr_tmp = pstr_sbr_extract_env->ptr_y_buffer[i];
2936       pstr_sbr_extract_env->ptr_y_buffer[i] =
2937           pstr_sbr_extract_env->ptr_y_buffer[i + pstr_sbr_extract_env->time_slots];
2938       pstr_sbr_extract_env->ptr_y_buffer[i + pstr_sbr_extract_env->time_slots] = ptr_tmp;
2939     }
2940 
2941     pstr_sbr_extract_env->buffer_flag ^= 1;
2942     ch++;
2943   }
2944 
2945   pstr_sbr_hdr->prev_coupling = pstr_sbr_hdr->coupling;
2946 
2947   return err_code;
2948 }
2949