• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *                                                                            *
3  * Copyright (C) 2018 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 #include <string.h>
21 #include "ixheaacd_sbr_common.h"
22 #include "ixheaacd_type_def.h"
23 
24 #include "ixheaacd_constants.h"
25 #include "ixheaacd_basic_ops32.h"
26 #include "ixheaacd_basic_ops16.h"
27 #include "ixheaacd_basic_ops40.h"
28 #include "ixheaacd_basic_ops.h"
29 
30 #include "ixheaacd_basic_op.h"
31 #include "ixheaacd_intrinsics.h"
32 #include "ixheaacd_common_rom.h"
33 #include "ixheaacd_basic_funcs.h"
34 #include "ixheaacd_bitbuffer.h"
35 #include "ixheaacd_defines.h"
36 
37 #include "ixheaacd_pns.h"
38 
39 #include "ixheaacd_aac_rom.h"
40 #include "ixheaacd_pulsedata.h"
41 
42 #include "ixheaacd_drc_data_struct.h"
43 #include "ixheaacd_lt_predict.h"
44 #include "ixheaacd_channelinfo.h"
45 #include "ixheaacd_drc_dec.h"
46 #include "ixheaacd_sbrdecoder.h"
47 
48 #include "ixheaacd_defines.h"
49 #include "ixheaacd_sbrdecoder.h"
50 #include "ixheaacd_definitions.h"
51 #include "ixheaacd_error_codes.h"
52 
53 #include "ixheaacd_pulsedata.h"
54 
55 #include "ixheaacd_sbrdecsettings.h"
56 #include "ixheaacd_sbr_scale.h"
57 #include "ixheaacd_lpp_tran.h"
58 #include "ixheaacd_env_extr_part.h"
59 #include "ixheaacd_sbr_rom.h"
60 #include "ixheaacd_hybrid.h"
61 #include "ixheaacd_ps_dec.h"
62 #include "ixheaacd_env_extr.h"
63 
64 #include "ixheaacd_qmf_dec.h"
65 
66 #include "ixheaacd_env_calc.h"
67 #include "ixheaacd_sbr_const.h"
68 
69 #include "ixheaacd_pvc_dec.h"
70 #include "ixheaacd_sbr_dec.h"
71 #include "ixheaacd_env_extr.h"
72 #include "ixheaacd_env_calc.h"
73 #include "ixheaacd_ps_dec.h"
74 #include "ixheaacd_function_selector.h"
75 
76 #include "ixheaacd_audioobjtypes.h"
77 
ixheaacd_qmf_enrg_calc(ia_sbr_dec_struct * ptr_sbr_dec,WORD32 upsample_ratio_idx,WORD32 low_pow_flag)78 VOID ixheaacd_qmf_enrg_calc(ia_sbr_dec_struct *ptr_sbr_dec,
79                             WORD32 upsample_ratio_idx, WORD32 low_pow_flag) {
80   WORD32 i, j;
81   if (upsample_ratio_idx == SBR_UPSAMPLE_IDX_4_1) {
82     for (i = 0; i < ptr_sbr_dec->str_codec_qmf_bank.num_time_slots; i++) {
83       for (j = 0; j < 16; j++) {
84         ptr_sbr_dec->qmf_energy_buf[i][j] =
85             ptr_sbr_dec->qmf_buf_real[2 + i][j] *
86             ptr_sbr_dec->qmf_buf_real[2 + i][j];
87         if (!low_pow_flag)
88           ptr_sbr_dec->qmf_energy_buf[i][j] +=
89               (ptr_sbr_dec->qmf_buf_imag[2 + i][j] *
90                ptr_sbr_dec->qmf_buf_imag[2 + i][j]);
91       }
92     }
93 
94     for (i = 0; i < 16; i++) {
95       for (j = 0; j < 16; j++) {
96         ptr_sbr_dec->pvc_qmf_enrg_arr[32 * i + j] =
97             (ptr_sbr_dec->qmf_energy_buf[4 * i + 0][j] +
98              ptr_sbr_dec->qmf_energy_buf[4 * i + 1][j] +
99              ptr_sbr_dec->qmf_energy_buf[4 * i + 2][j] +
100              ptr_sbr_dec->qmf_energy_buf[4 * i + 3][j]) *
101             0.25f;
102       }
103     }
104   } else {
105     for (i = 0; i < ptr_sbr_dec->str_codec_qmf_bank.num_time_slots; i++) {
106       for (j = 0; j < 32; j++) {
107         ptr_sbr_dec->qmf_energy_buf[i][j] =
108             ptr_sbr_dec->qmf_buf_real[2 + i][j] *
109             ptr_sbr_dec->qmf_buf_real[2 + i][j];
110         if (!low_pow_flag)
111           ptr_sbr_dec->qmf_energy_buf[i][j] +=
112               (ptr_sbr_dec->qmf_buf_imag[2 + i][j] *
113                ptr_sbr_dec->qmf_buf_imag[2 + i][j]);
114       }
115     }
116 
117     for (i = 0; i < 16; i++) {
118       for (j = 0; j < 32; j++) {
119         ptr_sbr_dec->pvc_qmf_enrg_arr[32 * i + j] =
120             (ptr_sbr_dec->qmf_energy_buf[2 * i + 0][j] +
121              ptr_sbr_dec->qmf_energy_buf[2 * i + 1][j]) *
122             0.5f;
123       }
124     }
125   }
126 }
127 
ixheaacd_hbe_repl_spec(WORD32 x_over_qmf[MAX_NUM_PATCHES],FLOAT32 qmf_buf_real[][64],FLOAT32 qmf_buf_imag[][64],WORD32 no_bins,WORD32 max_stretch)128 VOID ixheaacd_hbe_repl_spec(WORD32 x_over_qmf[MAX_NUM_PATCHES],
129                             FLOAT32 qmf_buf_real[][64],
130                             FLOAT32 qmf_buf_imag[][64], WORD32 no_bins,
131                             WORD32 max_stretch) {
132   WORD32 patch_bands;
133   WORD32 patch, band, col, target, source_bands, i;
134   WORD32 num_patches = 0;
135 
136   for (i = 1; i < MAX_NUM_PATCHES; i++) {
137     if (x_over_qmf[i] != 0) {
138       num_patches++;
139     }
140   }
141 
142   for (patch = (max_stretch - 1); patch < num_patches; patch++) {
143     patch_bands = x_over_qmf[patch + 1] - x_over_qmf[patch];
144     target = x_over_qmf[patch];
145     source_bands = x_over_qmf[max_stretch - 1] - x_over_qmf[max_stretch - 2];
146     while (patch_bands > 0) {
147       WORD32 ixheaacd_num_bands = source_bands;
148       WORD32 start_band = x_over_qmf[max_stretch - 1] - 1;
149       if (target + ixheaacd_num_bands >= x_over_qmf[patch + 1]) {
150         ixheaacd_num_bands = x_over_qmf[patch + 1] - target;
151       }
152       if ((((target + ixheaacd_num_bands - 1) & 1) +
153            ((x_over_qmf[max_stretch - 1] - 1) & 1)) &
154           1) {
155         if (ixheaacd_num_bands == source_bands) {
156           ixheaacd_num_bands--;
157         } else {
158           start_band--;
159         }
160       }
161       if (!ixheaacd_num_bands) break;
162       for (col = 0; col < no_bins; col++) {
163         WORD32 i = 0;
164         band = target + ixheaacd_num_bands - 1;
165         if (64 <= band) {
166           band = 63;
167         }
168         if (x_over_qmf[patch + 1] <= band) {
169           band = x_over_qmf[patch + 1] - 1;
170         }
171         for (i = 0; i < ixheaacd_num_bands; i++, band--) {
172           qmf_buf_real[col][band] = qmf_buf_real[col][start_band - i];
173           qmf_buf_imag[col][band] = qmf_buf_imag[col][start_band - i];
174         }
175       }
176       target += ixheaacd_num_bands;
177       patch_bands -= ixheaacd_num_bands;
178     }
179   }
180 }
181 
ixheaacd_esbr_analysis_filt_block(ia_sbr_dec_struct * ptr_sbr_dec,ia_sbr_tables_struct * sbr_tables_ptr,WORD32 op_delay)182 VOID ixheaacd_esbr_analysis_filt_block(ia_sbr_dec_struct *ptr_sbr_dec,
183                                        ia_sbr_tables_struct *sbr_tables_ptr,
184                                        WORD32 op_delay) {
185   FLOAT32 *core_coder_samples;
186   WORD32 *ptr_filt_states;
187   WORD32 *ptr_filt_states_1;
188   WORD32 *ptr_filt_states_2;
189   WORD32 *ptr_temp;
190   WORD32 *ptr_win_coeffs_1;
191   WORD32 *ptr_win_coeffs_2;
192   WORD32 *ptr_win_coeffs;
193   WORD32 *ploc_qmf_buf_real;
194   WORD32 *ploc_qmf_buf_imag;
195   WORD32 local_qmf_buffer[128] = {0};
196   WORD32 anal_buf[2 * 32];
197   WORD32 idx, z;
198   WORD32 core_syn_ch_index;
199   FLOAT32 gain;
200   WORD32 filt_offset;
201   WORD32 num_columns;
202   ia_qmf_dec_tables_struct *qmf_dec_tables_ptr =
203       sbr_tables_ptr->qmf_dec_tables_ptr;
204   ia_sbr_qmf_filter_bank_struct *pstr_qmf_anal_bank =
205       &ptr_sbr_dec->str_codec_qmf_bank;
206   core_coder_samples = ptr_sbr_dec->time_sample_buf;
207   ptr_filt_states = pstr_qmf_anal_bank->state_new_samples_pos_low_32;
208   ptr_win_coeffs_1 = pstr_qmf_anal_bank->filter_pos_32;
209   num_columns = pstr_qmf_anal_bank->no_channels;
210 
211   switch (num_columns) {
212     case 16:
213       ptr_win_coeffs_2 = ptr_win_coeffs_1 + 64;
214       gain = 128.0f;
215       filt_offset = 64;
216       break;
217     case 24:
218       ptr_win_coeffs_2 = ptr_win_coeffs_1 + 24;
219       gain = 12.0f;
220       filt_offset = 24;
221       break;
222     case 32:
223       ptr_win_coeffs_2 = ptr_win_coeffs_1 + 64;
224       gain = 256.0f;
225       filt_offset = 64;
226       break;
227     default:
228       ptr_win_coeffs_2 = ptr_win_coeffs_1 + 64;
229       gain = 256.0f;
230       filt_offset = 64;
231       break;
232   }
233   gain = 1.0f / gain;
234 
235   pstr_qmf_anal_bank->usb = num_columns;
236 
237   ploc_qmf_buf_real = &local_qmf_buffer[0];
238   ploc_qmf_buf_imag = &local_qmf_buffer[64];
239 
240   ptr_filt_states_1 = pstr_qmf_anal_bank->anal_filter_states_32;
241   ptr_filt_states_2 = pstr_qmf_anal_bank->anal_filter_states_32 + num_columns;
242 
243   for (idx = 0; idx < ptr_sbr_dec->str_codec_qmf_bank.num_time_slots; idx++) {
244     for (z = 0; z < num_columns; z++) {
245       ptr_filt_states[num_columns - 1 - z] =
246           (WORD32)(core_coder_samples[z] * (1 << 15));
247     }
248     ixheaacd_esbr_qmfanal32_winadd(ptr_filt_states_1, ptr_filt_states_2,
249                                    ptr_win_coeffs_1, ptr_win_coeffs_2, anal_buf,
250                                    num_columns);
251 
252     core_coder_samples += num_columns;
253 
254     ptr_filt_states -= num_columns;
255     if (ptr_filt_states < pstr_qmf_anal_bank->anal_filter_states_32) {
256       ptr_filt_states = pstr_qmf_anal_bank->anal_filter_states_32 +
257                         10 * num_columns - num_columns;
258     }
259 
260     ptr_temp = ptr_filt_states_1;
261     ptr_filt_states_1 = ptr_filt_states_2;
262     ptr_filt_states_2 = ptr_temp;
263 
264     ptr_win_coeffs_1 += filt_offset;
265     ptr_win_coeffs_2 += filt_offset;
266 
267     ptr_win_coeffs = ptr_win_coeffs_1;
268     ptr_win_coeffs_1 = ptr_win_coeffs_2;
269     ptr_win_coeffs_2 = ptr_win_coeffs;
270 
271     if (ptr_win_coeffs_2 >
272         (pstr_qmf_anal_bank->analy_win_coeff_32 + filt_offset * 10)) {
273       ptr_win_coeffs_1 = pstr_qmf_anal_bank->analy_win_coeff_32;
274       ptr_win_coeffs_2 = pstr_qmf_anal_bank->analy_win_coeff_32 + filt_offset;
275     }
276 
277     ixheaacd_esbr_fwd_modulation(anal_buf, &ploc_qmf_buf_real[0],
278                                  &ploc_qmf_buf_imag[0], pstr_qmf_anal_bank,
279                                  qmf_dec_tables_ptr);
280     core_syn_ch_index = num_columns;
281 
282     for (z = 0; z < core_syn_ch_index; z++) {
283       ptr_sbr_dec->qmf_buf_real[op_delay + idx][z] =
284           ((FLOAT32)ploc_qmf_buf_real[z] * gain);
285       ptr_sbr_dec->qmf_buf_imag[op_delay + idx][z] =
286           ((FLOAT32)ploc_qmf_buf_imag[z] * gain);
287     }
288     for (z = core_syn_ch_index; z < num_columns; z++) {
289       ptr_sbr_dec->qmf_buf_real[op_delay + idx][z] = (FLOAT32)0.0f;
290       ptr_sbr_dec->qmf_buf_imag[op_delay + idx][z] = (FLOAT32)0.0f;
291     }
292   }
293 
294   pstr_qmf_anal_bank->filter_pos_32 = ptr_win_coeffs_1;
295   pstr_qmf_anal_bank->state_new_samples_pos_low_32 = ptr_filt_states;
296 }
297 
ixheaacd_esbr_synthesis_regrp(FLOAT32 * qmf_buf_real,FLOAT32 * qmf_buf_imag,ia_sbr_dec_struct * ptr_sbr_dec,ia_sbr_frame_info_data_struct * ptr_frame_data,ia_sbr_header_data_struct * ptr_header_data,WORD32 stereo_config_idx,WORD32 apply_processing)298 VOID ixheaacd_esbr_synthesis_regrp(
299     FLOAT32 *qmf_buf_real, FLOAT32 *qmf_buf_imag,
300     ia_sbr_dec_struct *ptr_sbr_dec,
301     ia_sbr_frame_info_data_struct *ptr_frame_data,
302     ia_sbr_header_data_struct *ptr_header_data, WORD32 stereo_config_idx,
303     WORD32 apply_processing) {
304   WORD32 i, k;
305   WORD32 stop_border = 0;
306   WORD32 num_anal_bands = ptr_sbr_dec->str_codec_qmf_bank.no_channels;
307   WORD32 x_over_band = num_anal_bands;
308 
309   if (apply_processing) {
310     if (ptr_header_data->sbr_ratio_idx == SBR_UPSAMPLE_IDX_4_1) {
311       stop_border = 4 * ptr_frame_data->str_frame_info_details.border_vec[0];
312     } else {
313       stop_border = 2 * ptr_frame_data->str_frame_info_details.border_vec[0];
314     }
315     x_over_band = ptr_header_data->pstr_freq_band_data->qmf_sb_prev;
316   }
317 
318   if (stereo_config_idx > 0) {
319     for (i = 0; i < stop_border; i++) {
320       for (k = 0; k < 3; k++) {
321         *qmf_buf_real++ =
322             ptr_sbr_dec->qmf_buf_real[(2) + i + HYBRID_FILTER_DELAY][k];
323         *qmf_buf_imag++ =
324             ptr_sbr_dec->qmf_buf_imag[(2) + i + HYBRID_FILTER_DELAY][k];
325       }
326 
327       for (; k < x_over_band; k++) {
328         *qmf_buf_real++ = ptr_sbr_dec->qmf_buf_real[(2) + i][k];
329         *qmf_buf_imag++ = ptr_sbr_dec->qmf_buf_imag[(2) + i][k];
330       }
331 
332       for (; k < 64; k++) {
333         *qmf_buf_real++ = ptr_sbr_dec->sbr_qmf_out_real[(2) + i][k];
334         *qmf_buf_imag++ = ptr_sbr_dec->sbr_qmf_out_imag[(2) + i][k];
335       }
336 
337       qmf_buf_real += 14;
338       qmf_buf_imag += 14;
339     }
340 
341     x_over_band = ptr_header_data->pstr_freq_band_data->sub_band_start;
342 
343     for (; i < ptr_sbr_dec->str_codec_qmf_bank.num_time_slots; i++) {
344       for (k = 0; k < 3; k++) {
345         *qmf_buf_real++ =
346             ptr_sbr_dec->qmf_buf_real[(2) + i + HYBRID_FILTER_DELAY][k];
347         *qmf_buf_imag++ =
348             ptr_sbr_dec->qmf_buf_imag[(2) + i + HYBRID_FILTER_DELAY][k];
349       }
350 
351       for (; k < x_over_band; k++) {
352         *qmf_buf_real++ = ptr_sbr_dec->qmf_buf_real[(2) + i][k];
353         *qmf_buf_imag++ = ptr_sbr_dec->qmf_buf_imag[(2) + i][k];
354       }
355 
356       for (; k < 64; k++) {
357         *qmf_buf_real++ = ptr_sbr_dec->sbr_qmf_out_real[(2) + i][k];
358         *qmf_buf_imag++ = ptr_sbr_dec->sbr_qmf_out_imag[(2) + i][k];
359       }
360 
361       qmf_buf_real += 14;
362       qmf_buf_imag += 14;
363     }
364 
365   } else {
366     for (i = 0; i < stop_border; i++) {
367       for (k = 0; k < x_over_band; k++) {
368         *qmf_buf_real++ = ptr_sbr_dec->qmf_buf_real[(2) + i][k];
369         *qmf_buf_imag++ = ptr_sbr_dec->qmf_buf_imag[(2) + i][k];
370       }
371 
372       for (; k < 64; k++) {
373         *qmf_buf_real++ = ptr_sbr_dec->sbr_qmf_out_real[(2) + i][k];
374         *qmf_buf_imag++ = ptr_sbr_dec->sbr_qmf_out_imag[(2) + i][k];
375       }
376 
377       qmf_buf_real += 14;
378       qmf_buf_imag += 14;
379     }
380 
381     x_over_band = ptr_header_data->pstr_freq_band_data->sub_band_start;
382 
383     for (; i < ptr_sbr_dec->str_codec_qmf_bank.num_time_slots; i++) {
384       for (k = 0; k < x_over_band; k++) {
385         *qmf_buf_real++ = ptr_sbr_dec->qmf_buf_real[(2) + i][k];
386         *qmf_buf_imag++ = ptr_sbr_dec->qmf_buf_imag[(2) + i][k];
387       }
388 
389       for (; k < 64; k++) {
390         *qmf_buf_real++ = ptr_sbr_dec->sbr_qmf_out_real[(2) + i][k];
391         *qmf_buf_imag++ = ptr_sbr_dec->sbr_qmf_out_imag[(2) + i][k];
392       }
393 
394       qmf_buf_real += 14;
395       qmf_buf_imag += 14;
396     }
397   }
398 }
399 
ixheaacd_mps_esbr_synthesis_regrp(FLOAT32 * qmf_buf_real,FLOAT32 * qmf_buf_imag,ia_sbr_dec_struct * ptr_sbr_dec,WORD32 stereo_config_idx)400 VOID ixheaacd_mps_esbr_synthesis_regrp(FLOAT32 *qmf_buf_real,
401                                        FLOAT32 *qmf_buf_imag,
402                                        ia_sbr_dec_struct *ptr_sbr_dec,
403                                        WORD32 stereo_config_idx) {
404   WORD32 i, k;
405   WORD32 num_anal_bands = ptr_sbr_dec->str_codec_qmf_bank.no_channels;
406   WORD32 x_over_band = num_anal_bands;
407 
408   if (stereo_config_idx > 0) {
409     for (i = 0; i < ptr_sbr_dec->str_codec_qmf_bank.num_time_slots; i++) {
410       for (k = 0; k < 3; k++) {
411         *qmf_buf_real++ =
412             ptr_sbr_dec->qmf_buf_real[(2) + i + HYBRID_FILTER_DELAY][k];
413         *qmf_buf_imag++ =
414             ptr_sbr_dec->qmf_buf_imag[(2) + i + HYBRID_FILTER_DELAY][k];
415       }
416 
417       for (; k < x_over_band; k++) {
418         *qmf_buf_real++ = ptr_sbr_dec->qmf_buf_real[(2) + i][k];
419         *qmf_buf_imag++ = ptr_sbr_dec->qmf_buf_imag[(2) + i][k];
420       }
421 
422       for (; k < 64; k++) {
423         *qmf_buf_real++ = 0;
424         *qmf_buf_imag++ = 0;
425       }
426 
427       qmf_buf_real += 14;
428       qmf_buf_imag += 14;
429     }
430   } else {
431     for (i = 0; i < ptr_sbr_dec->str_codec_qmf_bank.num_time_slots; i++) {
432       for (k = 0; k < x_over_band; k++) {
433         *qmf_buf_real++ = ptr_sbr_dec->qmf_buf_real[(2) + i][k];
434         *qmf_buf_imag++ = ptr_sbr_dec->qmf_buf_imag[(2) + i][k];
435       }
436 
437       for (; k < 64; k++) {
438         *qmf_buf_real++ = 0.0f;
439         *qmf_buf_imag++ = 0.0f;
440       }
441 
442       qmf_buf_real += 14;
443       qmf_buf_imag += 14;
444     }
445   }
446 }
447 
ixheaacd_esbr_synthesis_filt_block(ia_sbr_dec_struct * ptr_sbr_dec,ia_sbr_header_data_struct * ptr_header_data,ia_sbr_frame_info_data_struct * ptr_frame_data,WORD32 apply_processing,FLOAT32 ** qmf_buf_real,FLOAT32 ** qmf_buf_imag,WORD32 stereo_config_idx,ia_sbr_tables_struct * sbr_tables_ptr,WORD32 mps_sbr_flag,WORD32 ch_fac)448 VOID ixheaacd_esbr_synthesis_filt_block(
449     ia_sbr_dec_struct *ptr_sbr_dec, ia_sbr_header_data_struct *ptr_header_data,
450     ia_sbr_frame_info_data_struct *ptr_frame_data, WORD32 apply_processing,
451     FLOAT32 **qmf_buf_real, FLOAT32 **qmf_buf_imag, WORD32 stereo_config_idx,
452     ia_sbr_tables_struct *sbr_tables_ptr, WORD32 mps_sbr_flag, WORD32 ch_fac) {
453   if (!mps_sbr_flag) {
454     ixheaacd_esbr_synthesis_regrp(&qmf_buf_real[0][0], &qmf_buf_imag[0][0],
455                                   ptr_sbr_dec, ptr_frame_data, ptr_header_data,
456                                   stereo_config_idx, apply_processing);
457   } else {
458     ixheaacd_mps_esbr_synthesis_regrp(&qmf_buf_real[0][0], &qmf_buf_imag[0][0],
459                                       ptr_sbr_dec, stereo_config_idx);
460   }
461 
462   if (stereo_config_idx <= 0) {
463     WORD32 i, k, p1;
464     WORD32 *ptr_filt_states;
465     WORD32 *ptr_filt_states_1;
466     WORD32 *ptr_filt_states_2;
467     WORD32 *filter_l;
468     WORD32 *ploc_qmf_buf_real;
469     WORD32 *ploc_qmf_buf_imag;
470     WORD32 out_scalefactor;
471     WORD32 sixty4;
472     WORD32 no_synthesis_channels;
473     WORD32 ixheaacd_drc_offset;
474     FLOAT32 *syn_buffer;
475     WORD32 *local_qmf_buffer = ptr_sbr_dec->sbr_scratch_local;
476     WORD32 *time_out = &(ptr_sbr_dec->sbr_scratch_local[128]);
477     ia_sbr_qmf_filter_bank_struct *qmf_bank =
478         &ptr_sbr_dec->str_synthesis_qmf_bank;
479     ia_qmf_dec_tables_struct *qmf_dec_tables_ptr =
480         sbr_tables_ptr->qmf_dec_tables_ptr;
481     out_scalefactor = 5;
482     qmf_bank->no_channels = 64;
483     qmf_bank->esbr_cos_twiddle =
484         (WORD32 *)qmf_dec_tables_ptr->esbr_sin_cos_twiddle_l64;
485     qmf_bank->esbr_alt_sin_twiddle =
486         (WORD32 *)qmf_dec_tables_ptr->esbr_alt_sin_twiddle_l64;
487 
488     qmf_bank->filter_pos_syn_32 +=
489         qmf_dec_tables_ptr->esbr_qmf_c - qmf_bank->p_filter_32;
490     qmf_bank->p_filter_32 = qmf_dec_tables_ptr->esbr_qmf_c;
491 
492     sixty4 = NO_SYNTHESIS_CHANNELS;
493 
494     ptr_filt_states = qmf_bank->filter_states_32;
495 
496     no_synthesis_channels = qmf_bank->no_channels;
497     ptr_filt_states_1 = &ptr_filt_states[0];
498     ptr_filt_states_2 = ptr_filt_states_1 + no_synthesis_channels;
499 
500     filter_l = qmf_bank->filter_pos_syn_32;
501 
502     p1 = 0;
503 
504     ixheaacd_drc_offset = qmf_bank->ixheaacd_drc_offset;
505 
506     for (i = 0; i < ptr_sbr_dec->str_codec_qmf_bank.num_time_slots; i++) {
507       for (k = 0; k < 64; k++) {
508         local_qmf_buffer[k + 0] = (WORD32)(qmf_buf_real[i][k] * 64);
509         local_qmf_buffer[k + 64] = (WORD32)(qmf_buf_imag[i][k] * 64);
510       }
511       ploc_qmf_buf_real = local_qmf_buffer;
512       ploc_qmf_buf_imag = local_qmf_buffer + 64;
513 
514       ixheaacd_esbr_inv_modulation(ploc_qmf_buf_real,
515                                    &ptr_sbr_dec->str_synthesis_qmf_bank,
516                                    sbr_tables_ptr->qmf_dec_tables_ptr);
517 
518       ixheaacd_shiftrountine_with_rnd_hq(ploc_qmf_buf_real, ploc_qmf_buf_imag,
519                                          &ptr_filt_states[ixheaacd_drc_offset],
520                                          no_synthesis_channels,
521                                          out_scalefactor + 1);
522 
523       ixheaacd_esbr_qmfsyn64_winadd(ptr_filt_states_1, ptr_filt_states_2,
524                                     filter_l, &time_out[0], ch_fac);
525 
526       syn_buffer = ptr_sbr_dec->time_sample_buf + i * 64;
527       for (k = 0; k < 64; k++) {
528         syn_buffer[k] = (FLOAT32)time_out[k] / (1 << 16);
529       }
530 
531       ptr_filt_states_1 += sixty4;
532       ptr_filt_states_2 -= sixty4;
533       sixty4 = -sixty4;
534       ixheaacd_drc_offset -= 128;
535 
536       if (ixheaacd_drc_offset < 0) ixheaacd_drc_offset += 1280;
537 
538       filter_l += 64;
539 
540       if (filter_l == qmf_bank->p_filter_32 + 640)
541         filter_l = (WORD32 *)qmf_bank->p_filter_32;
542 
543       p1 += no_synthesis_channels;
544     }
545 
546     qmf_bank->filter_pos_syn_32 = filter_l;
547     qmf_bank->ixheaacd_drc_offset = ixheaacd_drc_offset;
548   }
549 
550   if (!mps_sbr_flag) ptr_frame_data->reset_flag = 0;
551 
552   if (apply_processing && !mps_sbr_flag) {
553     ptr_header_data->pstr_freq_band_data->qmf_sb_prev =
554         ptr_header_data->pstr_freq_band_data->sub_band_start;
555   }
556 }
557 
ixheaacd_sbr_dec(ia_sbr_dec_struct * ptr_sbr_dec,WORD16 * ptr_time_data,ia_sbr_header_data_struct * ptr_header_data,ia_sbr_frame_info_data_struct * ptr_frame_data,ia_sbr_prev_frame_data_struct * ptr_frame_data_prev,ia_ps_dec_struct * ptr_ps_dec,ia_sbr_qmf_filter_bank_struct * ptr_qmf_synth_bank_r,ia_sbr_scale_fact_struct * ptr_sbr_sf_r,FLAG apply_processing,FLAG low_pow_flag,WORD32 * ptr_work_buf_core,ia_sbr_tables_struct * sbr_tables_ptr,ixheaacd_misc_tables * pstr_common_tables,WORD ch_fac,ia_pvc_data_struct * ptr_pvc_data,FLAG drc_on,WORD32 drc_sbr_factors[][64],WORD32 audio_object_type)558 WORD32 ixheaacd_sbr_dec(ia_sbr_dec_struct *ptr_sbr_dec, WORD16 *ptr_time_data,
559                         ia_sbr_header_data_struct *ptr_header_data,
560                         ia_sbr_frame_info_data_struct *ptr_frame_data,
561                         ia_sbr_prev_frame_data_struct *ptr_frame_data_prev,
562                         ia_ps_dec_struct *ptr_ps_dec,
563                         ia_sbr_qmf_filter_bank_struct *ptr_qmf_synth_bank_r,
564                         ia_sbr_scale_fact_struct *ptr_sbr_sf_r,
565                         FLAG apply_processing, FLAG low_pow_flag,
566                         WORD32 *ptr_work_buf_core,
567                         ia_sbr_tables_struct *sbr_tables_ptr,
568                         ixheaacd_misc_tables *pstr_common_tables, WORD ch_fac,
569                         ia_pvc_data_struct *ptr_pvc_data, FLAG drc_on,
570                         WORD32 drc_sbr_factors[][64],
571                         WORD32 audio_object_type) {
572   WORD i;
573   WORD slot, reserve;
574   WORD save_lb_scale;
575   WORD op_delay;
576 
577   WORD32 *p_arr_qmf_buf_real[MAX_ENV_COLS] = {0};
578   WORD32 *p_arr_qmf_buf_imag[MAX_ENV_COLS] = {0};
579   WORD32 *ptr;
580   WORD hbe_flag = ptr_header_data->hbe_flag;
581 
582   FLOAT32 **pp_qmf_buf_real = NULL;
583   FLOAT32 **pp_qmf_buf_imag = NULL;
584   FLOAT32 pvc_dec_out_buf[16 * 64];
585 
586   WORD upsample_ratio_idx = ptr_header_data->sbr_ratio_idx;
587   WORD no_bins;
588   WORD mps_sbr_flag = ptr_frame_data->mps_sbr_flag;
589   WORD stereo_config_idx = ptr_frame_data->stereo_config_idx;
590   WORD sbr_mode = ptr_frame_data->sbr_mode;
591   WORD usac_flag = ptr_header_data->usac_flag;
592 
593   FLOAT32 *pvc_qmf_enrg_arr = (FLOAT32 *)ptr_sbr_dec->pvc_qmf_enrg_arr;
594 
595   memset(pvc_dec_out_buf, 0, 1024 * sizeof(FLOAT32));
596   memset(pvc_qmf_enrg_arr, 0, 512 * sizeof(FLOAT32));
597   if (audio_object_type == AOT_ER_AAC_ELD) {
598     op_delay = 0;
599   } else {
600     op_delay = 6;
601   }
602   if (usac_flag) {
603     pp_qmf_buf_real = ptr_sbr_dec->pp_qmf_buf_real;
604     pp_qmf_buf_imag = ptr_sbr_dec->pp_qmf_buf_imag;
605     if (upsample_ratio_idx == SBR_UPSAMPLE_IDX_4_1) {
606       op_delay = 2 * 6;
607     }
608   }
609 
610   no_bins = (ptr_header_data->num_time_slots * ptr_header_data->time_step);
611 
612   if (!usac_flag) {
613     WORD32 num = op_delay;
614     WORD32 *ptr_pers_qmf_real = ptr_sbr_dec->ptr_sbr_overlap_buf;
615     WORD32 *p_scr_qmf_real = ptr_work_buf_core + (2 << (6 + !low_pow_flag));
616 
617     if ((no_bins < LPC_ORDER) || ((no_bins + op_delay) > MAX_ENV_COLS))
618       return -1;
619 
620     if (!low_pow_flag) {
621       num = num << 1;
622     }
623     if (audio_object_type != AOT_ER_AAC_ELD) {
624       memcpy(p_scr_qmf_real, ptr_pers_qmf_real,
625              sizeof(WORD32) * NO_SYNTHESIS_CHANNELS * num);
626     }
627     ptr = p_scr_qmf_real;
628 
629     for (slot = 0; slot < op_delay + no_bins; slot++) {
630       p_arr_qmf_buf_real[slot] = ptr;
631       ptr += NO_SYNTHESIS_CHANNELS;
632 
633       if (!low_pow_flag) {
634         p_arr_qmf_buf_imag[slot] = ptr;
635         ptr += NO_SYNTHESIS_CHANNELS;
636       }
637     }
638 
639     ptr_sbr_dec->str_sbr_scale_fact.lb_scale = 0;
640 
641     if (apply_processing) {
642       ixheaacd_rescale_x_overlap(ptr_sbr_dec, ptr_header_data, ptr_frame_data,
643                                  ptr_frame_data_prev, p_arr_qmf_buf_real,
644                                  p_arr_qmf_buf_imag, low_pow_flag);
645     }
646   }
647 
648   if (usac_flag) {
649     WORD32 num_anal_bands = ptr_sbr_dec->str_codec_qmf_bank.no_channels;
650     WORD32 codec_x_delay = 0;
651     WORD32 core_syn_ch_index = 0;
652     WORD32 frame_move = 288;
653     WORD32 core_frame_size = ptr_header_data->core_frame_size;
654 
655     if (hbe_flag) {
656       codec_x_delay = ESBR_HBE_DELAY_OFFSET;
657     }
658     if (upsample_ratio_idx == SBR_UPSAMPLE_IDX_4_1) {
659       codec_x_delay = 2 * codec_x_delay;
660     }
661     /* fixed decoder delay for bitstreams with SBR 4:1 and stereoConfigIndex 3
662      */
663     if (mps_sbr_flag) op_delay = MPS_SBR_DELAY;
664 
665     if (hbe_flag || mps_sbr_flag) {
666       core_syn_ch_index = num_anal_bands;
667     } else {
668       core_syn_ch_index = ptr_header_data->pstr_freq_band_data->sub_band_start;
669     }
670 
671     frame_move = 9 * num_anal_bands;
672 
673     memmove(ptr_sbr_dec->core_sample_buf,
674             ptr_sbr_dec->core_sample_buf + core_frame_size,
675             frame_move * sizeof(FLOAT32));
676     memcpy(&ptr_sbr_dec->core_sample_buf[frame_move],
677            &ptr_sbr_dec->time_sample_buf[0], core_frame_size * sizeof(FLOAT32));
678 
679     memmove(
680         &ptr_sbr_dec->qmf_buf_real[0][0],
681         &ptr_sbr_dec
682              ->qmf_buf_real[ptr_sbr_dec->str_codec_qmf_bank.num_time_slots][0],
683         (op_delay + SBR_HF_ADJ_OFFSET + codec_x_delay) * sizeof(FLOAT32) * 64);
684 
685     memmove(
686         &ptr_sbr_dec->qmf_buf_imag[0][0],
687         &ptr_sbr_dec
688              ->qmf_buf_imag[ptr_sbr_dec->str_codec_qmf_bank.num_time_slots][0],
689         (op_delay + SBR_HF_ADJ_OFFSET + codec_x_delay) * sizeof(FLOAT32) * 64);
690 
691     memmove(&ptr_sbr_dec->sbr_qmf_out_real[0][0],
692             &ptr_sbr_dec->sbr_qmf_out_real[ptr_sbr_dec->str_codec_qmf_bank
693                                                .num_time_slots][0],
694             (op_delay + SBR_HF_ADJ_OFFSET) * sizeof(FLOAT32) * 64);
695 
696     memmove(&ptr_sbr_dec->sbr_qmf_out_imag[0][0],
697             &ptr_sbr_dec->sbr_qmf_out_imag[ptr_sbr_dec->str_codec_qmf_bank
698                                                .num_time_slots][0],
699             (op_delay + SBR_HF_ADJ_OFFSET) * sizeof(FLOAT32) * 64);
700 
701     if (hbe_flag) {
702       memmove(&ptr_sbr_dec->ph_vocod_qmf_real[0][0],
703               &ptr_sbr_dec->ph_vocod_qmf_real[ptr_sbr_dec->str_codec_qmf_bank
704                                                   .num_time_slots][0],
705               64 * sizeof(FLOAT32) * (op_delay + SBR_HF_ADJ_OFFSET));
706 
707       memmove(ptr_sbr_dec->ph_vocod_qmf_imag,
708               ptr_sbr_dec->ph_vocod_qmf_imag +
709                   ptr_sbr_dec->str_codec_qmf_bank.num_time_slots,
710               64 * sizeof(FLOAT32) * (op_delay + SBR_HF_ADJ_OFFSET));
711     }
712     ixheaacd_esbr_analysis_filt_block(
713         ptr_sbr_dec, sbr_tables_ptr,
714         op_delay + codec_x_delay + SBR_HF_ADJ_OFFSET);
715 
716     if (hbe_flag) {
717       WORD32 err_code = ixheaacd_qmf_hbe_apply(
718           ptr_sbr_dec->p_hbe_txposer,
719           ptr_sbr_dec->qmf_buf_real + (op_delay + SBR_HF_ADJ_OFFSET) +
720               ESBR_HBE_DELAY_OFFSET,
721           ptr_sbr_dec->qmf_buf_imag + (op_delay + SBR_HF_ADJ_OFFSET) +
722               ESBR_HBE_DELAY_OFFSET,
723           ptr_sbr_dec->str_codec_qmf_bank.num_time_slots,
724           ptr_sbr_dec->ph_vocod_qmf_real + (op_delay + SBR_HF_ADJ_OFFSET),
725           ptr_sbr_dec->ph_vocod_qmf_imag + (op_delay + SBR_HF_ADJ_OFFSET),
726           ptr_frame_data->pitch_in_bins);
727       if (err_code) return err_code;
728 
729       if (upsample_ratio_idx == SBR_UPSAMPLE_IDX_4_1) {
730         ixheaacd_hbe_repl_spec(
731             &ptr_sbr_dec->p_hbe_txposer->x_over_qmf[0],
732             ptr_sbr_dec->ph_vocod_qmf_real + (op_delay + SBR_HF_ADJ_OFFSET),
733             ptr_sbr_dec->ph_vocod_qmf_imag + (op_delay + SBR_HF_ADJ_OFFSET),
734             ptr_sbr_dec->str_codec_qmf_bank.num_time_slots,
735             ptr_sbr_dec->p_hbe_txposer->max_stretch);
736       }
737     }
738     ixheaacd_qmf_enrg_calc(ptr_sbr_dec, upsample_ratio_idx, low_pow_flag);
739 
740     if (!mps_sbr_flag && apply_processing) {
741       WORD32 err_code = 0;
742       err_code = ixheaacd_generate_hf(
743           ptr_sbr_dec->qmf_buf_real + (SBR_HF_ADJ_OFFSET),
744           ptr_sbr_dec->qmf_buf_imag + (SBR_HF_ADJ_OFFSET),
745           ptr_sbr_dec->ph_vocod_qmf_real + (SBR_HF_ADJ_OFFSET),
746           ptr_sbr_dec->ph_vocod_qmf_imag + (SBR_HF_ADJ_OFFSET),
747           ptr_sbr_dec->sbr_qmf_out_real + (SBR_HF_ADJ_OFFSET),
748           ptr_sbr_dec->sbr_qmf_out_imag + (SBR_HF_ADJ_OFFSET), ptr_frame_data,
749           ptr_header_data);
750       if (err_code) return err_code;
751 
752       ptr_pvc_data->pvc_rate = ptr_header_data->upsamp_fac;
753 
754       if (sbr_mode == PVC_SBR) {
755         err_code = ixheaacd_pvc_process(
756             ptr_pvc_data, ptr_header_data->pstr_freq_band_data->sub_band_start,
757             ptr_frame_data->str_pvc_frame_info.border_vec[0],
758             &pvc_qmf_enrg_arr[0], &pvc_dec_out_buf[0]);
759 
760         if (err_code) return err_code;
761 
762         ptr_pvc_data->prev_pvc_flg = 1;
763       } else {
764         memset(pvc_dec_out_buf, 0, 1024 * sizeof(FLOAT32));
765         ptr_pvc_data->prev_pvc_flg = 0;
766       }
767 
768       ptr_pvc_data->prev_first_bnd_idx =
769           ptr_header_data->pstr_freq_band_data->sub_band_start;
770       ptr_pvc_data->prev_pvc_rate = ptr_pvc_data->pvc_rate;
771 
772       ptr_frame_data->pstr_sbr_header = ptr_header_data;
773       err_code = ixheaacd_sbr_env_calc(
774           ptr_frame_data, ptr_sbr_dec->sbr_qmf_out_real + (SBR_HF_ADJ_OFFSET),
775           ptr_sbr_dec->sbr_qmf_out_imag + (SBR_HF_ADJ_OFFSET),
776           ptr_sbr_dec->qmf_buf_real + (SBR_HF_ADJ_OFFSET),
777           ptr_sbr_dec->qmf_buf_imag + (SBR_HF_ADJ_OFFSET),
778           (ptr_header_data->hbe_flag == 0)
779               ? NULL
780               : ptr_sbr_dec->p_hbe_txposer->x_over_qmf,
781           ptr_sbr_dec->scratch_buff, pvc_dec_out_buf);
782       if (err_code) return err_code;
783 
784     } else {
785       for (i = 0; i < 64; i++) {
786         memset(ptr_sbr_dec->sbr_qmf_out_real[i], 0, 64 * sizeof(FLOAT32));
787         memset(ptr_sbr_dec->sbr_qmf_out_imag[i], 0, 64 * sizeof(FLOAT32));
788       }
789     }
790 
791     ixheaacd_esbr_synthesis_filt_block(
792         ptr_sbr_dec, ptr_header_data, ptr_frame_data, apply_processing,
793         pp_qmf_buf_real, pp_qmf_buf_imag, stereo_config_idx, sbr_tables_ptr,
794         mps_sbr_flag, ch_fac);
795 
796     ptr_frame_data->prev_sbr_mode = sbr_mode;
797 
798     return 0;
799   }
800 
801   ixheaacd_cplx_anal_qmffilt(
802       ptr_time_data, &ptr_sbr_dec->str_sbr_scale_fact,
803       &p_arr_qmf_buf_real[op_delay], &p_arr_qmf_buf_imag[op_delay],
804       &ptr_sbr_dec->str_codec_qmf_bank, sbr_tables_ptr->qmf_dec_tables_ptr,
805       ch_fac, low_pow_flag, audio_object_type);
806 
807   {
808     WORD shift1, shift2;
809     WORD min_shift;
810     WORD shift_over;
811     WORD reserve_ov1, reserve_ov2;
812     WORD reservea[2];
813     WORD i = 0;
814     WORD usb = ptr_sbr_dec->str_codec_qmf_bank.usb;
815     WORD iter_val = 1;
816     if (audio_object_type == AOT_ER_AAC_ELD ||
817         audio_object_type == AOT_ER_AAC_LD) {
818       iter_val = 0;
819     }
820     do {
821       WORD t1 = op_delay;
822       WORD t2 = no_bins + op_delay;
823       if (i) {
824         t1 = 0;
825         t2 = op_delay;
826       }
827       reservea[i] = (*ixheaacd_ixheaacd_expsubbandsamples)(
828           p_arr_qmf_buf_real, p_arr_qmf_buf_imag, 0, usb, t1, t2, low_pow_flag);
829       i++;
830     } while (i <= iter_val);
831     ;
832 
833     reserve = reservea[0];
834     if (audio_object_type != AOT_ER_AAC_ELD &&
835         audio_object_type != AOT_ER_AAC_LD)
836       reserve_ov1 = reservea[1];
837     else
838       reserve_ov1 = reserve;
839     ptr_sbr_dec->max_samp_val = ixheaacd_min32(reserve, reserve_ov1);
840 
841     reserve_ov2 = (*ixheaacd_ixheaacd_expsubbandsamples)(
842         ptr_sbr_dec->str_hf_generator.lpc_filt_states_real,
843         ptr_sbr_dec->str_hf_generator.lpc_filt_states_imag, 0, usb, 0,
844         LPC_ORDER, low_pow_flag);
845 
846     reserve_ov1 = ixheaacd_min32(reserve_ov1, reserve_ov2);
847 
848     shift1 = ptr_sbr_dec->str_sbr_scale_fact.lb_scale + reserve;
849 
850     shift2 = ptr_sbr_dec->str_sbr_scale_fact.ov_lb_scale + reserve_ov1;
851     min_shift = ixheaacd_min32(shift1, shift2);
852     shift_over = (shift2 - min_shift);
853     reserve -= (shift1 - min_shift);
854 
855     ptr_sbr_dec->str_sbr_scale_fact.ov_lb_scale += (reserve_ov1 - shift_over);
856 
857     (*ixheaacd_adjust_scale)(p_arr_qmf_buf_real, p_arr_qmf_buf_imag, 0, usb, 0,
858                              op_delay, reserve_ov1 - shift_over, low_pow_flag);
859 
860     (*ixheaacd_adjust_scale)(p_arr_qmf_buf_real, p_arr_qmf_buf_imag, 0, usb,
861                              op_delay, (no_bins + op_delay), reserve,
862                              low_pow_flag);
863 
864     (*ixheaacd_adjust_scale)(ptr_sbr_dec->str_hf_generator.lpc_filt_states_real,
865                              ptr_sbr_dec->str_hf_generator.lpc_filt_states_imag,
866                              0, usb, 0, LPC_ORDER, reserve_ov1 - shift_over,
867                              low_pow_flag);
868 
869     ptr_sbr_dec->str_sbr_scale_fact.lb_scale += reserve;
870 
871     save_lb_scale = ptr_sbr_dec->str_sbr_scale_fact.lb_scale;
872   }
873 
874   {
875     WORD32 num = no_bins;
876     WORD32 *p_loc_qmf_real =
877         &p_arr_qmf_buf_real[op_delay][NO_ANALYSIS_CHANNELS];
878 
879     if (!low_pow_flag) {
880       num = num << 1;
881     }
882 
883     ixheaacd_clr_subsamples(p_loc_qmf_real, num - 1, (NO_SYN_ANA_CHANNELS));
884   }
885 
886   if (apply_processing) {
887     WORD16 degree_alias[NO_SYNTHESIS_CHANNELS];
888     WORD16 *border_vec = ptr_frame_data->str_frame_info_details.border_vec;
889     IA_ERRORCODE error_code = IA_NO_ERROR;
890 
891     if (low_pow_flag) {
892       memset(degree_alias, 0, NO_SYNTHESIS_CHANNELS * sizeof(WORD16));
893     }
894 
895     if (low_pow_flag) {
896       WORD32 com_low_band_scale;
897       ixheaacd_low_pow_hf_generator(
898           &ptr_sbr_dec->str_hf_generator, p_arr_qmf_buf_real, degree_alias,
899           border_vec[0] * ptr_header_data->time_step,
900           ptr_header_data->time_step *
901               ixheaacd_sub16_sat(
902                   border_vec[ptr_frame_data->str_frame_info_details.num_env],
903                   ptr_header_data->num_time_slots),
904           ptr_header_data->pstr_freq_band_data->num_if_bands,
905           ptr_frame_data->max_qmf_subband_aac, ptr_frame_data->sbr_invf_mode,
906           ptr_frame_data_prev->sbr_invf_mode, ptr_sbr_dec->max_samp_val,
907           ptr_work_buf_core);
908 
909       com_low_band_scale =
910           ixheaacd_min32(ptr_sbr_dec->str_sbr_scale_fact.ov_lb_scale,
911                          ptr_sbr_dec->str_sbr_scale_fact.lb_scale);
912 
913       ptr_sbr_dec->str_sbr_scale_fact.hb_scale =
914           (WORD16)(com_low_band_scale - 2);
915     } else {
916       ixheaacd_hf_generator(
917           &ptr_sbr_dec->str_hf_generator, &ptr_sbr_dec->str_sbr_scale_fact,
918           p_arr_qmf_buf_real, p_arr_qmf_buf_imag, ptr_header_data->time_step,
919           border_vec[0],
920           ixheaacd_sub16_sat(
921               border_vec[ptr_frame_data->str_frame_info_details.num_env],
922               ptr_header_data->num_time_slots),
923           ptr_header_data->pstr_freq_band_data->num_if_bands,
924           ptr_frame_data->max_qmf_subband_aac, ptr_frame_data->sbr_invf_mode,
925           ptr_frame_data_prev->sbr_invf_mode, ptr_work_buf_core,
926           audio_object_type);
927     }
928 
929     error_code = ixheaacd_calc_sbrenvelope(
930         &ptr_sbr_dec->str_sbr_scale_fact, &ptr_sbr_dec->str_sbr_calc_env,
931         ptr_header_data, ptr_frame_data, ptr_frame_data_prev,
932         p_arr_qmf_buf_real, p_arr_qmf_buf_imag, degree_alias, low_pow_flag,
933         sbr_tables_ptr, pstr_common_tables,
934         ptr_work_buf_core + (LPC_ORDER << (6 + !low_pow_flag)),
935         audio_object_type);
936     if (error_code) return error_code;
937 
938     memcpy(ptr_frame_data_prev->sbr_invf_mode, ptr_frame_data->sbr_invf_mode,
939            ptr_header_data->pstr_freq_band_data->num_if_bands * sizeof(WORD32));
940 
941     ptr_frame_data_prev->coupling_mode = ptr_frame_data->coupling_mode;
942     ptr_frame_data_prev->max_qmf_subband_aac =
943         ptr_frame_data->max_qmf_subband_aac;
944     ptr_frame_data_prev->end_position =
945         border_vec[ptr_frame_data->str_frame_info_details.num_env];
946     ptr_frame_data_prev->amp_res = ptr_frame_data->amp_res;
947   } else {
948     ptr_sbr_dec->str_sbr_scale_fact.hb_scale = save_lb_scale;
949   }
950 
951   if (!low_pow_flag) {
952     for (i = 0; i < LPC_ORDER; i++) {
953       WORD32 *p_loc_qmf_real = &p_arr_qmf_buf_real[no_bins - LPC_ORDER + i][0];
954       WORD32 *p_loc_qmf_imag = &p_arr_qmf_buf_imag[no_bins - LPC_ORDER + i][0];
955       WORD32 *plpc_filt_states_real =
956           &ptr_sbr_dec->str_hf_generator.lpc_filt_states_real[i][0];
957       WORD32 *plpc_filt_states_imag =
958           &ptr_sbr_dec->str_hf_generator.lpc_filt_states_imag[i][0];
959 
960       memcpy(plpc_filt_states_real, p_loc_qmf_real,
961              sizeof(WORD32) * (ptr_sbr_dec->str_codec_qmf_bank.usb));
962       memcpy(plpc_filt_states_imag, p_loc_qmf_imag,
963              sizeof(WORD32) * (ptr_sbr_dec->str_codec_qmf_bank.usb));
964     }
965   } else {
966     for (i = 0; i < LPC_ORDER; i++) {
967       WORD32 *p_loc_qmf_real = &p_arr_qmf_buf_real[no_bins - LPC_ORDER + i][0];
968       WORD32 *plpc_filt_states_real =
969           &ptr_sbr_dec->str_hf_generator.lpc_filt_states_real[i][0];
970       memcpy(plpc_filt_states_real, p_loc_qmf_real,
971              sizeof(WORD32) * (ptr_sbr_dec->str_codec_qmf_bank.usb));
972     }
973   }
974 
975   if (apply_processing && ptr_header_data->channel_mode == PS_STEREO &&
976       ((audio_object_type != AOT_ER_AAC_ELD) &&
977        (audio_object_type != AOT_ER_AAC_LD))) {
978     WORD32 ps_scale;
979 
980     ixheaacd_init_ps_scale(ptr_ps_dec, &ptr_sbr_dec->str_sbr_scale_fact);
981 
982     ixheaacd_cplx_synt_qmffilt(p_arr_qmf_buf_real, p_arr_qmf_buf_imag, op_delay,
983                                &ptr_sbr_dec->str_sbr_scale_fact, ptr_time_data,
984                                &ptr_sbr_dec->str_synthesis_qmf_bank, ptr_ps_dec,
985                                1, 0, sbr_tables_ptr, pstr_common_tables, ch_fac,
986                                drc_on, drc_sbr_factors, audio_object_type);
987 
988     ps_scale = ptr_sbr_dec->str_sbr_scale_fact.ps_scale;
989     ptr_sbr_sf_r->ov_lb_scale = ps_scale;
990     ptr_sbr_sf_r->lb_scale = ps_scale;
991     ptr_sbr_sf_r->hb_scale = ps_scale;
992 
993     ixheaacd_cplx_synt_qmffilt(p_arr_qmf_buf_real, p_arr_qmf_buf_imag, op_delay,
994                                ptr_sbr_sf_r, ptr_time_data + 1,
995                                ptr_qmf_synth_bank_r, ptr_ps_dec, 0, 0,
996                                sbr_tables_ptr, pstr_common_tables, ch_fac,
997                                drc_on, drc_sbr_factors, audio_object_type);
998   } else {
999     ixheaacd_cplx_synt_qmffilt(p_arr_qmf_buf_real, p_arr_qmf_buf_imag, op_delay,
1000                                &ptr_sbr_dec->str_sbr_scale_fact, ptr_time_data,
1001                                &ptr_sbr_dec->str_synthesis_qmf_bank, ptr_ps_dec,
1002                                0, low_pow_flag, sbr_tables_ptr,
1003                                pstr_common_tables, ch_fac, drc_on,
1004                                drc_sbr_factors, audio_object_type);
1005   }
1006 
1007   {
1008     WORD32 num = op_delay;
1009     WORD32 *p_loc_qmf_real = ptr_sbr_dec->ptr_sbr_overlap_buf;
1010     WORD32 *p_loc_qmf_real_1 = &p_arr_qmf_buf_real[no_bins][0];
1011 
1012     if (!low_pow_flag) {
1013       num = num << 1;
1014     }
1015 
1016     memcpy(p_loc_qmf_real, p_loc_qmf_real_1,
1017            sizeof(WORD32) * NO_SYNTHESIS_CHANNELS * num);
1018   }
1019 
1020   ptr_sbr_dec->str_sbr_scale_fact.ov_lb_scale = save_lb_scale;
1021   return 0;
1022 }
1023 
ixheaacd_esbr_dec(ia_sbr_dec_struct * ptr_sbr_dec,ia_sbr_header_data_struct * ptr_header_data,ia_sbr_frame_info_data_struct * ptr_frame_data,FLAG apply_processing,FLAG low_pow_flag,ia_sbr_tables_struct * ptr_sbr_tables,WORD ch_fac)1024 WORD32 ixheaacd_esbr_dec(ia_sbr_dec_struct *ptr_sbr_dec,
1025                          ia_sbr_header_data_struct *ptr_header_data,
1026                          ia_sbr_frame_info_data_struct *ptr_frame_data,
1027                          FLAG apply_processing, FLAG low_pow_flag,
1028                          ia_sbr_tables_struct *ptr_sbr_tables, WORD ch_fac) {
1029   WORD32 i;
1030   WORD32 op_delay;
1031 
1032   WORD32 codec_x_delay = 0;
1033 
1034   FLOAT32 **pp_qmf_buf_real = ptr_sbr_dec->pp_qmf_buf_real;
1035   FLOAT32 **pp_qmf_buf_imag = ptr_sbr_dec->pp_qmf_buf_imag;
1036 
1037   WORD32 upsample_ratio_idx = ptr_header_data->sbr_ratio_idx;
1038   WORD32 core_frame_size = ptr_header_data->core_frame_size;
1039 
1040   WORD32 no_bins;
1041   WORD32 mps_sbr_flag = ptr_frame_data->mps_sbr_flag;
1042   WORD32 stereo_config_idx = ptr_frame_data->stereo_config_idx;
1043   WORD32 hbe_flag = ptr_header_data->hbe_flag;
1044   WORD32 sbr_mode = ptr_frame_data->sbr_mode;
1045 
1046   op_delay = 6;
1047   if (upsample_ratio_idx == SBR_UPSAMPLE_IDX_4_1) {
1048     op_delay = 2 * 6;
1049   }
1050 
1051   no_bins = ptr_sbr_dec->str_codec_qmf_bank.num_time_slots;
1052 
1053   ptr_sbr_dec->str_sbr_scale_fact.lb_scale = 0;
1054   {
1055     WORD32 num_anal_bands = ptr_sbr_dec->str_codec_qmf_bank.no_channels;
1056     WORD32 frame_move = 288;
1057     if (hbe_flag) {
1058       codec_x_delay = 32;
1059     }
1060     if (upsample_ratio_idx == SBR_UPSAMPLE_IDX_4_1) {
1061       codec_x_delay = 2 * codec_x_delay;
1062     }
1063 
1064     frame_move = 9 * num_anal_bands;
1065 
1066     memmove(ptr_sbr_dec->core_sample_buf,
1067             ptr_sbr_dec->core_sample_buf + core_frame_size,
1068             frame_move * sizeof(FLOAT32));
1069 
1070     memcpy(&ptr_sbr_dec->core_sample_buf[frame_move],
1071            &ptr_sbr_dec->time_sample_buf[0], core_frame_size * sizeof(FLOAT32));
1072 
1073     memmove(
1074         &ptr_sbr_dec->qmf_buf_real[0][0],
1075         &ptr_sbr_dec
1076              ->qmf_buf_real[ptr_sbr_dec->str_codec_qmf_bank.num_time_slots][0],
1077         (op_delay + SBR_HF_ADJ_OFFSET + codec_x_delay) * sizeof(FLOAT32) * 64);
1078     memmove(
1079         &ptr_sbr_dec->qmf_buf_imag[0][0],
1080         &ptr_sbr_dec
1081              ->qmf_buf_imag[ptr_sbr_dec->str_codec_qmf_bank.num_time_slots][0],
1082         (op_delay + SBR_HF_ADJ_OFFSET + codec_x_delay) * sizeof(FLOAT32) * 64);
1083 
1084     memmove(&ptr_sbr_dec->sbr_qmf_out_real[0][0],
1085             &ptr_sbr_dec->sbr_qmf_out_real[ptr_sbr_dec->str_codec_qmf_bank
1086                                                .num_time_slots][0],
1087             (op_delay + SBR_HF_ADJ_OFFSET) * sizeof(FLOAT32) * 64);
1088     memmove(&ptr_sbr_dec->sbr_qmf_out_imag[0][0],
1089             &ptr_sbr_dec->sbr_qmf_out_imag[ptr_sbr_dec->str_codec_qmf_bank
1090                                                .num_time_slots][0],
1091             (op_delay + SBR_HF_ADJ_OFFSET) * sizeof(FLOAT32) * 64);
1092 
1093     if (hbe_flag) {
1094       memmove(&ptr_sbr_dec->ph_vocod_qmf_real[0][0],
1095               &ptr_sbr_dec->ph_vocod_qmf_real[ptr_sbr_dec->str_codec_qmf_bank
1096                                                   .num_time_slots][0],
1097               64 * sizeof(FLOAT32) * (op_delay + SBR_HF_ADJ_OFFSET));
1098       memmove(ptr_sbr_dec->ph_vocod_qmf_imag,
1099               ptr_sbr_dec->ph_vocod_qmf_imag +
1100                   ptr_sbr_dec->str_codec_qmf_bank.num_time_slots,
1101               64 * sizeof(FLOAT32) * (op_delay + SBR_HF_ADJ_OFFSET));
1102     }
1103   }
1104 
1105   ixheaacd_esbr_analysis_filt_block(
1106       ptr_sbr_dec, ptr_sbr_tables,
1107       op_delay + codec_x_delay + SBR_HF_ADJ_OFFSET);
1108 
1109   if (hbe_flag) {
1110     WORD32 err = ixheaacd_qmf_hbe_apply(
1111         ptr_sbr_dec->p_hbe_txposer,
1112         ptr_sbr_dec->qmf_buf_real + (op_delay + SBR_HF_ADJ_OFFSET) +
1113             ESBR_HBE_DELAY_OFFSET,
1114         ptr_sbr_dec->qmf_buf_imag + (op_delay + SBR_HF_ADJ_OFFSET) +
1115             ESBR_HBE_DELAY_OFFSET,
1116         ptr_sbr_dec->str_codec_qmf_bank.num_time_slots,
1117         ptr_sbr_dec->ph_vocod_qmf_real + (op_delay + SBR_HF_ADJ_OFFSET),
1118         ptr_sbr_dec->ph_vocod_qmf_imag + (op_delay + SBR_HF_ADJ_OFFSET),
1119         ptr_frame_data->pitch_in_bins);
1120     if (err) return err;
1121 
1122     if (upsample_ratio_idx == SBR_UPSAMPLE_IDX_4_1) {
1123       ixheaacd_hbe_repl_spec(
1124           &ptr_sbr_dec->p_hbe_txposer->x_over_qmf[0],
1125           ptr_sbr_dec->ph_vocod_qmf_real + (op_delay + SBR_HF_ADJ_OFFSET),
1126           ptr_sbr_dec->ph_vocod_qmf_imag + (op_delay + SBR_HF_ADJ_OFFSET),
1127           ptr_sbr_dec->str_codec_qmf_bank.num_time_slots,
1128           ptr_sbr_dec->p_hbe_txposer->max_stretch);
1129     }
1130   }
1131   ixheaacd_qmf_enrg_calc(ptr_sbr_dec, upsample_ratio_idx, low_pow_flag);
1132 
1133   for (i = 0; i < 64; i++) {
1134     memset(ptr_sbr_dec->sbr_qmf_out_real[i], 0, 64 * sizeof(FLOAT32));
1135     memset(ptr_sbr_dec->sbr_qmf_out_imag[i], 0, 64 * sizeof(FLOAT32));
1136   }
1137 
1138   ixheaacd_esbr_synthesis_filt_block(
1139       ptr_sbr_dec, ptr_header_data, ptr_frame_data, apply_processing,
1140       pp_qmf_buf_real, pp_qmf_buf_imag, stereo_config_idx, ptr_sbr_tables,
1141       mps_sbr_flag, ch_fac);
1142 
1143   ptr_frame_data->prev_sbr_mode = sbr_mode;
1144   return 0;
1145 }
1146 
ixheaacd_sbr_dec_from_mps(FLOAT32 * p_mps_qmf_output,VOID * p_sbr_dec,VOID * p_sbr_frame,VOID * p_sbr_header)1147 WORD32 ixheaacd_sbr_dec_from_mps(FLOAT32 *p_mps_qmf_output, VOID *p_sbr_dec,
1148                                  VOID *p_sbr_frame, VOID *p_sbr_header) {
1149   WORD32 i, k;
1150   ia_sbr_frame_info_data_struct *ptr_frame_data =
1151       (ia_sbr_frame_info_data_struct *)p_sbr_frame;
1152   ia_sbr_header_data_struct *ptr_header_data =
1153       (ia_sbr_header_data_struct *)p_sbr_header;
1154   ia_sbr_dec_struct *ptr_sbr_dec = (ia_sbr_dec_struct *)p_sbr_dec;
1155   ia_frame_info_struct *p_frame_info = &ptr_frame_data->str_frame_info_details;
1156   WORD32 no_bins;
1157   WORD32 upsample_ratio_idx = ptr_header_data->sbr_ratio_idx;
1158   WORD32 op_delay = 6 + SBR_HF_ADJ_OFFSET;
1159   WORD32 num_anal_bands = 40;
1160   WORD32 mps_sbr_flag = ptr_frame_data->mps_sbr_flag;
1161   WORD32 err = 0;
1162 
1163   if (ptr_header_data->is_usf_4) {
1164     op_delay += 6;
1165   }
1166 
1167   num_anal_bands = num_anal_bands - (upsample_ratio_idx << 3);
1168 
1169   if (!mps_sbr_flag) {
1170     return 0;
1171   }
1172 
1173   no_bins = ptr_header_data->output_framesize / 64;
1174 
1175   for (i = 0; i < no_bins; i++) {
1176     FLOAT32 *p_loc_mps_qmf_output =
1177         p_mps_qmf_output + i * (MAX_NUM_QMF_BANDS_ESBR * 2);
1178     for (k = 0; k < ptr_header_data->pstr_freq_band_data->sub_band_start; k++) {
1179       ptr_sbr_dec->mps_qmf_buf_real[op_delay + i][k] = *p_loc_mps_qmf_output++;
1180       ptr_sbr_dec->mps_qmf_buf_imag[op_delay + i][k] = *p_loc_mps_qmf_output++;
1181 
1182       ptr_sbr_dec->mps_sbr_qmf_buf_real[SBR_HF_ADJ_OFFSET + i][k] =
1183           ptr_sbr_dec->qmf_buf_real[SBR_HF_ADJ_OFFSET + i][k];
1184       ptr_sbr_dec->mps_sbr_qmf_buf_imag[SBR_HF_ADJ_OFFSET + i][k] =
1185           ptr_sbr_dec->qmf_buf_real[SBR_HF_ADJ_OFFSET + i][k];
1186     }
1187   }
1188 
1189   if (ptr_frame_data->reset_flag) {
1190     WORD32 l;
1191     WORD32 start_band = ptr_header_data->pstr_freq_band_data->qmf_sb_prev;
1192     WORD32 end_band = num_anal_bands;
1193     WORD32 start_slot =
1194         SBR_HF_ADJ_OFFSET + ptr_frame_data->rate * p_frame_info->border_vec[0];
1195 
1196     for (l = start_slot; l < op_delay; l++) {
1197       for (k = start_band; k < end_band; k++) {
1198         ptr_sbr_dec->mps_qmf_buf_real[l][k] = 0.0;
1199         ptr_sbr_dec->mps_qmf_buf_imag[l][k] = 0.0;
1200       }
1201     }
1202 
1203     for (l = 0; l < SBR_HF_ADJ_OFFSET; l++) {
1204       for (k = start_band; k < end_band; k++) {
1205         ptr_sbr_dec->mps_qmf_buf_real[l][k] = 0.0;
1206         ptr_sbr_dec->mps_qmf_buf_imag[l][k] = 0.0;
1207       }
1208     }
1209   }
1210   ptr_header_data->pstr_freq_band_data->qmf_sb_prev =
1211       ptr_header_data->pstr_freq_band_data->sub_band_start;
1212 
1213   err = ixheaacd_generate_hf(
1214       ptr_sbr_dec->mps_qmf_buf_real + SBR_HF_ADJ_OFFSET,
1215       ptr_sbr_dec->mps_qmf_buf_imag + SBR_HF_ADJ_OFFSET, NULL, NULL,
1216       ptr_sbr_dec->mps_sbr_qmf_buf_real + SBR_HF_ADJ_OFFSET,
1217       ptr_sbr_dec->mps_sbr_qmf_buf_imag + SBR_HF_ADJ_OFFSET, ptr_frame_data,
1218       ptr_header_data);
1219   if (err) return err;
1220 
1221   ptr_frame_data->pstr_sbr_header = ptr_header_data;
1222   ptr_frame_data->sbr_mode = ORIG_SBR;
1223   ptr_frame_data->prev_sbr_mode = ORIG_SBR;
1224   err = ixheaacd_sbr_env_calc(
1225       ptr_frame_data, ptr_sbr_dec->mps_sbr_qmf_buf_real + SBR_HF_ADJ_OFFSET,
1226       ptr_sbr_dec->mps_sbr_qmf_buf_imag + SBR_HF_ADJ_OFFSET,
1227       ptr_sbr_dec->mps_qmf_buf_real + SBR_HF_ADJ_OFFSET,
1228       ptr_sbr_dec->mps_qmf_buf_imag + SBR_HF_ADJ_OFFSET,
1229       (ptr_header_data->hbe_flag == 0) ? NULL
1230                                        : ptr_sbr_dec->p_hbe_txposer->x_over_qmf,
1231       ptr_sbr_dec->scratch_buff, NULL);
1232 
1233   if (err) return err;
1234   for (i = 0; i < no_bins; i++) {
1235     FLOAT32 *p_loc_mps_qmf_output =
1236         p_mps_qmf_output + i * (MAX_NUM_QMF_BANDS_ESBR * 2);
1237     for (k = 0; k < ptr_header_data->pstr_freq_band_data->sub_band_start; k++) {
1238       *p_loc_mps_qmf_output++ =
1239           ptr_sbr_dec->mps_qmf_buf_real[SBR_HF_ADJ_OFFSET + i][k];
1240       *p_loc_mps_qmf_output++ =
1241           ptr_sbr_dec->mps_qmf_buf_imag[SBR_HF_ADJ_OFFSET + i][k];
1242     }
1243     for (k = ptr_header_data->pstr_freq_band_data->sub_band_start; k < 64;
1244          k++) {
1245       *p_loc_mps_qmf_output++ =
1246           ptr_sbr_dec->mps_sbr_qmf_buf_real[SBR_HF_ADJ_OFFSET + i][k];
1247       *p_loc_mps_qmf_output++ =
1248           ptr_sbr_dec->mps_sbr_qmf_buf_imag[SBR_HF_ADJ_OFFSET + i][k];
1249     }
1250   }
1251 
1252   for (i = 0; i < op_delay; i++) {
1253     memmove(ptr_sbr_dec->mps_qmf_buf_real[i],
1254             ptr_sbr_dec->mps_qmf_buf_real[no_bins + i], 64 * sizeof(FLOAT32));
1255 
1256     memmove(ptr_sbr_dec->mps_qmf_buf_imag[i],
1257             ptr_sbr_dec->mps_qmf_buf_imag[no_bins + i], 64 * sizeof(FLOAT32));
1258 
1259     memmove(ptr_sbr_dec->mps_sbr_qmf_buf_real[i],
1260             ptr_sbr_dec->mps_sbr_qmf_buf_real[no_bins + i],
1261             64 * sizeof(FLOAT32));
1262 
1263     memmove(ptr_sbr_dec->mps_sbr_qmf_buf_imag[i],
1264             ptr_sbr_dec->mps_sbr_qmf_buf_imag[no_bins + i],
1265             64 * sizeof(FLOAT32));
1266   }
1267 
1268   ptr_frame_data->reset_flag = 0;
1269   return err;
1270 }
1271