• 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 #include "ixheaacd_defines.h"
30 
31 #include "ixheaacd_intrinsics.h"
32 #include "ixheaacd_sbr_const.h"
33 #include "ixheaacd_basic_op.h"
34 #include "ixheaacd_defines.h"
35 #include "ixheaacd_bitbuffer.h"
36 #include "ixheaacd_pns.h"
37 
38 #include "ixheaacd_aac_rom.h"
39 #include "ixheaacd_pulsedata.h"
40 
41 #include "ixheaacd_drc_data_struct.h"
42 #include "ixheaacd_lt_predict.h"
43 #include "ixheaacd_channelinfo.h"
44 #include "ixheaacd_drc_dec.h"
45 
46 #include "ixheaacd_sbrdecoder.h"
47 
48 #include "ixheaacd_sbrdecsettings.h"
49 #include "ixheaacd_sbr_scale.h"
50 #include "ixheaacd_lpp_tran.h"
51 #include "ixheaacd_env_extr_part.h"
52 #include "ixheaacd_sbr_rom.h"
53 #include "ixheaacd_hybrid.h"
54 #include "ixheaacd_ps_dec.h"
55 #include "ixheaacd_ps_bitdec.h"
56 #include "ixheaacd_env_extr.h"
57 #include "ixheaacd_common_rom.h"
58 #include "ixheaacd_freq_sca.h"
59 
60 #include "ixheaacd_qmf_dec.h"
61 
62 #include "ixheaacd_env_calc.h"
63 
64 #include "ixheaacd_pvc_dec.h"
65 #include "ixheaacd_sbr_dec.h"
66 #include "ixheaacd_env_dec.h"
67 #include "ixheaacd_basic_funcs.h"
68 #include "ixheaacd_sbr_crc.h"
69 #include "ixheaacd_error_standards.h"
70 #include "ixheaacd_sbrqmftrans.h"
71 
72 #include "ixheaacd_audioobjtypes.h"
73 
74 #define ALIGN_SIZE64(x) ((((x) + 7) >> 3) << 3)
75 
ixheaacd_getsize_sbr_persistent()76 WORD32 ixheaacd_getsize_sbr_persistent() {
77   return (ALIGN_SIZE64(sizeof(ia_sbr_pers_struct)));
78 }
79 
ixheaacd_esbr_hbe_data_init(ia_esbr_hbe_txposer_struct * pstr_esbr_hbe_txposer,const WORD32 num_aac_samples,WORD32 samp_fac_4_flag,const WORD32 num_out_samples,VOID * persistent_hbe_mem)80 VOID ixheaacd_esbr_hbe_data_init(
81     ia_esbr_hbe_txposer_struct *pstr_esbr_hbe_txposer,
82     const WORD32 num_aac_samples, WORD32 samp_fac_4_flag,
83     const WORD32 num_out_samples, VOID *persistent_hbe_mem) {
84   WORD32 i;
85   WORD32 used_persistent = 0;
86 
87   if (pstr_esbr_hbe_txposer) {
88     memset(pstr_esbr_hbe_txposer, 0, sizeof(ia_esbr_hbe_txposer_struct));
89 
90     pstr_esbr_hbe_txposer->core_frame_length = num_aac_samples;
91 
92     pstr_esbr_hbe_txposer->no_bins = num_out_samples / NO_QMF_SYNTH_CHANNELS;
93 
94     pstr_esbr_hbe_txposer->hbe_qmf_in_len =
95         pstr_esbr_hbe_txposer->no_bins / 2 + HBE_OPER_WIN_LEN - 1;
96     pstr_esbr_hbe_txposer->hbe_qmf_out_len =
97         2 * pstr_esbr_hbe_txposer->hbe_qmf_in_len;
98 
99     pstr_esbr_hbe_txposer->ptr_input_buf =
100         (FLOAT32 *)((WORD8 *)persistent_hbe_mem);
101     used_persistent +=
102         (num_aac_samples + NO_QMF_SYNTH_CHANNELS) * sizeof(FLOAT32);
103 
104     pstr_esbr_hbe_txposer->qmf_in_buf =
105         (FLOAT32 **)((WORD8 *)persistent_hbe_mem + used_persistent);
106     used_persistent +=
107         pstr_esbr_hbe_txposer->hbe_qmf_in_len * sizeof(FLOAT32 *);
108 
109     for (i = 0; i < pstr_esbr_hbe_txposer->hbe_qmf_in_len; i++) {
110       pstr_esbr_hbe_txposer->qmf_in_buf[i] =
111           (FLOAT32 *)((WORD8 *)persistent_hbe_mem + used_persistent);
112 
113       used_persistent += (TWICE_QMF_SYNTH_CHANNELS_NUM * sizeof(FLOAT32));
114     }
115 
116     pstr_esbr_hbe_txposer->qmf_out_buf =
117         (FLOAT32 **)((WORD8 *)persistent_hbe_mem + used_persistent);
118     used_persistent +=
119         (pstr_esbr_hbe_txposer->hbe_qmf_out_len * sizeof(FLOAT32 *));
120 
121     for (i = 0; i < pstr_esbr_hbe_txposer->hbe_qmf_out_len; i++) {
122       pstr_esbr_hbe_txposer->qmf_out_buf[i] =
123           (FLOAT32 *)((WORD8 *)persistent_hbe_mem + used_persistent);
124       used_persistent += (TWICE_QMF_SYNTH_CHANNELS_NUM * sizeof(FLOAT32));
125     }
126     pstr_esbr_hbe_txposer->upsamp_4_flag = samp_fac_4_flag;
127   }
128 
129   return;
130 }
131 
ixheaacd_set_sbr_persistent_table_pointer(VOID * sbr_persistent_mem_v,ia_sbr_tables_struct * sbr_tables_ptr,ixheaacd_misc_tables * pstr_common_tables)132 VOID ixheaacd_set_sbr_persistent_table_pointer(
133     VOID *sbr_persistent_mem_v, ia_sbr_tables_struct *sbr_tables_ptr,
134     ixheaacd_misc_tables *pstr_common_tables) {
135   ia_sbr_pers_struct *sbr_persistent_mem =
136       (ia_sbr_pers_struct *)sbr_persistent_mem_v;
137   sbr_persistent_mem->str_sbr_dec_inst.pstr_sbr_tables = sbr_tables_ptr;
138   sbr_persistent_mem->str_sbr_dec_inst.pstr_common_tables = pstr_common_tables;
139 }
140 
ixheaacd_set_sbr_persistent_buffers(VOID * sbr_persistent_mem_v,WORD32 * persistent_used,WORD32 num_channel,WORD ps_enable)141 VOID ixheaacd_set_sbr_persistent_buffers(VOID *sbr_persistent_mem_v,
142                                          WORD32 *persistent_used,
143                                          WORD32 num_channel, WORD ps_enable) {
144   WORD32 i = 0;
145   WORD32 used_persistent = *persistent_used;
146   WORD32 temp, temp1, temp2, temp3;
147   struct ia_sbr_pers_struct *sbr_persistent_mem =
148       (struct ia_sbr_pers_struct *)sbr_persistent_mem_v;
149 
150   struct ia_sbr_dec_inst_struct *p_str_sbr_dec_inst =
151       &sbr_persistent_mem->str_sbr_dec_inst;
152 
153   memset(sbr_persistent_mem, 0, sizeof(struct ia_sbr_pers_struct));
154 
155   sbr_persistent_mem->sbr_qmf_analy_states =
156       (WORD16 *)((WORD8 *)sbr_persistent_mem_v + used_persistent);
157   temp = num_channel * ((QMF_FILTER_STATE_ANA_SIZE + 2 * NO_ANALYSIS_CHANNELS) *
158                         sizeof(WORD16));
159   used_persistent += temp;
160 
161   sbr_persistent_mem->sbr_qmf_analy_states_32 =
162       (WORD32 *)((WORD8 *)sbr_persistent_mem_v + used_persistent);
163   temp1 =
164       num_channel *
165       ((QMF_FILTER_STATE_ANA_SIZE + 2 * NO_ANALYSIS_CHANNELS) * sizeof(WORD32));
166   used_persistent += temp1;
167 
168   sbr_persistent_mem->sbr_qmf_synth_states =
169       (WORD16 *)((WORD8 *)sbr_persistent_mem_v + used_persistent);
170 
171   temp2 =
172       (num_channel * ((QMF_FILTER_STATE_SYN_SIZE + 2 * NO_SYNTHESIS_CHANNELS) *
173                       sizeof(WORD16)));
174   used_persistent += temp2;
175 
176   sbr_persistent_mem->sbr_qmf_synth_states_32 =
177       (WORD32 *)((WORD8 *)sbr_persistent_mem_v + used_persistent);
178 
179   temp3 =
180       (num_channel * ((QMF_FILTER_STATE_SYN_SIZE + 2 * NO_SYNTHESIS_CHANNELS) *
181                       sizeof(WORD32)));
182   used_persistent += temp3;
183 
184   memset(sbr_persistent_mem->sbr_qmf_analy_states, 0,
185          (temp + temp1 + temp2 + temp3));
186 
187   for (i = 0; i < num_channel; i++) {
188     sbr_persistent_mem->ptr_sbr_overlap_buf[i] =
189         (WORD32 *)((WORD8 *)sbr_persistent_mem_v + used_persistent);
190 
191     if (ps_enable) {
192       memset(sbr_persistent_mem->ptr_sbr_overlap_buf[i], 0,
193              2 * MAX_OV_COLS * NO_SYNTHESIS_CHANNELS * sizeof(WORD32));
194       used_persistent +=
195           2 * MAX_OV_COLS * NO_SYNTHESIS_CHANNELS * sizeof(WORD32);
196     } else {
197       memset(sbr_persistent_mem->ptr_sbr_overlap_buf[i], 0,
198              MAX_OV_COLS * NO_SYNTHESIS_CHANNELS * sizeof(WORD32));
199       used_persistent += MAX_OV_COLS * NO_SYNTHESIS_CHANNELS * sizeof(WORD32);
200     }
201   }
202 
203   for (i = 0; i < num_channel; i++) {
204     WORD32 j;
205     sbr_persistent_mem->sbr_lpc_filter_states_real[i] =
206         (WORD32 **)((WORD8 *)sbr_persistent_mem_v + used_persistent);
207     used_persistent += LPC_ORDER * sizeof(WORD32 *);
208     for (j = 0; j < LPC_ORDER; j++) {
209       sbr_persistent_mem->sbr_lpc_filter_states_real[i][j] =
210           (WORD32 *)((WORD8 *)sbr_persistent_mem_v + used_persistent);
211 
212       used_persistent += NO_ANALYSIS_CHANNELS * sizeof(WORD32);
213 
214       memset(sbr_persistent_mem->sbr_lpc_filter_states_real[i][j], 0,
215              NO_ANALYSIS_CHANNELS * sizeof(WORD32));
216     }
217   }
218 
219   if (ps_enable) {
220     for (i = 0; i < num_channel; i++) {
221       WORD32 j;
222 
223       sbr_persistent_mem->sbr_lpc_filter_states_imag[i] =
224           (WORD32 **)((WORD8 *)sbr_persistent_mem_v + used_persistent);
225       used_persistent += LPC_ORDER * sizeof(WORD32 *);
226       for (j = 0; j < LPC_ORDER; j++) {
227         sbr_persistent_mem->sbr_lpc_filter_states_imag[i][j] =
228             (WORD32 *)((WORD8 *)sbr_persistent_mem_v + used_persistent);
229 
230         used_persistent += NO_ANALYSIS_CHANNELS * sizeof(WORD32);
231 
232         memset(sbr_persistent_mem->sbr_lpc_filter_states_imag[i][j], 0,
233                NO_ANALYSIS_CHANNELS * sizeof(WORD32));
234       }
235     }
236   }
237   for (i = 0; i < num_channel; i++) {
238     WORD32 initial_used = used_persistent;
239     WORD32 temp_used = used_persistent;
240 
241     sbr_persistent_mem->sbr_smooth_gain_buf[i] =
242         (WORD16 *)((WORD8 *)sbr_persistent_mem_v + temp_used);
243     temp_used += 2 * MAX_FREQ_COEFFS * sizeof(WORD16);
244 
245     sbr_persistent_mem->sbr_smooth_noise_buf[i] =
246         (WORD16 *)((WORD8 *)sbr_persistent_mem_v + temp_used);
247 
248     temp_used += MAX_FREQ_COEFFS * sizeof(WORD16);
249 
250     p_str_sbr_dec_inst->pstr_freq_band_data[i] =
251         (VOID *)((WORD8 *)sbr_persistent_mem_v + temp_used);
252 
253     temp_used += ALIGN_SIZE64(sizeof(ia_freq_band_data_struct));
254 
255     sbr_persistent_mem->pstr_prev_frame_data[i] =
256         (VOID *)((WORD8 *)sbr_persistent_mem_v + temp_used);
257 
258     temp_used += ALIGN_SIZE64(sizeof(ia_sbr_prev_frame_data_struct));
259 
260     p_str_sbr_dec_inst->pstr_sbr_channel[i] =
261         (VOID *)((WORD8 *)sbr_persistent_mem_v + temp_used);
262 
263     temp_used += ALIGN_SIZE64(sizeof(ia_sbr_channel_struct));
264 
265     p_str_sbr_dec_inst->pstr_sbr_header[i] =
266         (VOID *)((WORD8 *)sbr_persistent_mem_v + temp_used);
267 
268     temp_used += ALIGN_SIZE64(sizeof(ia_sbr_header_data_struct));
269 
270     memset(sbr_persistent_mem->sbr_smooth_gain_buf[i], 0,
271            temp_used - initial_used);
272 
273     used_persistent = temp_used;
274   }
275 
276   if (ps_enable) {
277     p_str_sbr_dec_inst->pstr_ps_stereo_dec =
278         (ia_ps_dec_struct *)((WORD8 *)sbr_persistent_mem_v + used_persistent);
279 
280     memset(p_str_sbr_dec_inst->pstr_ps_stereo_dec, 0, sizeof(ia_ps_dec_struct));
281 
282     used_persistent += sizeof(ia_ps_dec_struct);
283   }
284 
285   p_str_sbr_dec_inst->frame_buffer[0] =
286       (VOID *)((WORD8 *)sbr_persistent_mem_v + used_persistent);
287   memset(p_str_sbr_dec_inst->frame_buffer[0], 0,
288          (sizeof(ia_sbr_frame_info_data_struct) +
289           MAX_FREQ_COEFFS * sizeof(WORD32) * 2 + 8));
290   used_persistent = used_persistent + (sizeof(ia_sbr_frame_info_data_struct) +
291                                        MAX_FREQ_COEFFS * sizeof(WORD32) + 8);
292 
293   p_str_sbr_dec_inst->frame_buffer[1] =
294       (VOID *)((WORD8 *)sbr_persistent_mem_v + used_persistent);
295   memset(p_str_sbr_dec_inst->frame_buffer[1], 0,
296          (sizeof(ia_sbr_frame_info_data_struct) +
297           MAX_FREQ_COEFFS * sizeof(WORD32) * 2 + 8));
298   used_persistent = used_persistent + (sizeof(ia_sbr_frame_info_data_struct) +
299                                        MAX_FREQ_COEFFS * sizeof(WORD32) + 8);
300 
301   {
302     WORD32 index = 0;
303     p_str_sbr_dec_inst->ptr_pvc_data_str =
304         (ia_pvc_data_struct *)((WORD8 *)sbr_persistent_mem_v + used_persistent);
305     memset(p_str_sbr_dec_inst->ptr_pvc_data_str, 0, sizeof(ia_pvc_data_struct));
306     used_persistent += sizeof(ia_pvc_data_struct);
307 
308     p_str_sbr_dec_inst->pstr_sbr_channel[0]->str_sbr_dec.p_hbe_txposer =
309         (ia_esbr_hbe_txposer_struct *)((WORD8 *)sbr_persistent_mem_v +
310                                        used_persistent);
311     memset(p_str_sbr_dec_inst->pstr_sbr_channel[0]->str_sbr_dec.p_hbe_txposer,
312            0, sizeof(ia_esbr_hbe_txposer_struct));
313     used_persistent += sizeof(ia_esbr_hbe_txposer_struct);
314 
315     if (num_channel == 2) {
316       p_str_sbr_dec_inst->pstr_sbr_channel[1]->str_sbr_dec.p_hbe_txposer =
317           (ia_esbr_hbe_txposer_struct *)((WORD8 *)sbr_persistent_mem_v +
318                                          used_persistent);
319       memset(p_str_sbr_dec_inst->pstr_sbr_channel[1]->str_sbr_dec.p_hbe_txposer,
320              0, sizeof(ia_esbr_hbe_txposer_struct));
321       used_persistent += sizeof(ia_esbr_hbe_txposer_struct);
322     }
323 
324     p_str_sbr_dec_inst->hbe_txposer_buffers =
325         (VOID *)((WORD8 *)sbr_persistent_mem_v + used_persistent);
326     memset(p_str_sbr_dec_inst->hbe_txposer_buffers, 0,
327            num_channel * MAX_HBE_PERSISTENT_SIZE);
328     used_persistent += num_channel * MAX_HBE_PERSISTENT_SIZE;
329 
330     p_str_sbr_dec_inst->pstr_sbr_channel[0]->str_sbr_dec.pp_qmf_buf_real =
331         (FLOAT32 **)((WORD8 *)sbr_persistent_mem_v + used_persistent);
332     memset(p_str_sbr_dec_inst->pstr_sbr_channel[0]->str_sbr_dec.pp_qmf_buf_real,
333            0, MAX_QMF_BUF_LEN * sizeof(FLOAT32 *));
334     used_persistent += MAX_QMF_BUF_LEN * sizeof(FLOAT32 *);
335 
336     p_str_sbr_dec_inst->pstr_sbr_channel[0]->str_sbr_dec.pp_qmf_buf_imag =
337         (FLOAT32 **)((WORD8 *)sbr_persistent_mem_v + used_persistent);
338     memset(p_str_sbr_dec_inst->pstr_sbr_channel[0]->str_sbr_dec.pp_qmf_buf_imag,
339            0, MAX_QMF_BUF_LEN);
340     used_persistent += MAX_QMF_BUF_LEN * sizeof(FLOAT32 *);
341 
342     if (num_channel == 2) {
343       p_str_sbr_dec_inst->pstr_sbr_channel[1]->str_sbr_dec.pp_qmf_buf_real =
344           (FLOAT32 **)((WORD8 *)sbr_persistent_mem_v + used_persistent);
345       memset(
346           p_str_sbr_dec_inst->pstr_sbr_channel[1]->str_sbr_dec.pp_qmf_buf_real,
347           0, MAX_QMF_BUF_LEN * sizeof(FLOAT32 *));
348       used_persistent += MAX_QMF_BUF_LEN * sizeof(FLOAT32 *);
349 
350       p_str_sbr_dec_inst->pstr_sbr_channel[1]->str_sbr_dec.pp_qmf_buf_imag =
351           (FLOAT32 **)((WORD8 *)sbr_persistent_mem_v + used_persistent);
352       memset(
353           p_str_sbr_dec_inst->pstr_sbr_channel[1]->str_sbr_dec.pp_qmf_buf_imag,
354           0, MAX_QMF_BUF_LEN * sizeof(FLOAT32 *));
355       used_persistent += MAX_QMF_BUF_LEN * sizeof(FLOAT32 *);
356     }
357 
358     for (index = 0; index < MAX_QMF_BUF_LEN; index++) {
359       p_str_sbr_dec_inst->pstr_sbr_channel[0]
360           ->str_sbr_dec.pp_qmf_buf_real[index] =
361           (FLOAT32 *)((WORD8 *)sbr_persistent_mem_v + used_persistent);
362       used_persistent += MAX_QMF_BUF_LEN * sizeof(FLOAT32);
363     }
364 
365     for (index = 0; index < MAX_QMF_BUF_LEN; index++) {
366       p_str_sbr_dec_inst->pstr_sbr_channel[0]
367           ->str_sbr_dec.pp_qmf_buf_imag[index] =
368           (FLOAT32 *)((WORD8 *)sbr_persistent_mem_v + used_persistent);
369       used_persistent += MAX_QMF_BUF_LEN * sizeof(FLOAT32);
370     }
371 
372     if (num_channel == 2) {
373       for (index = 0; index < MAX_QMF_BUF_LEN; index++) {
374         p_str_sbr_dec_inst->pstr_sbr_channel[1]
375             ->str_sbr_dec.pp_qmf_buf_real[index] =
376             (FLOAT32 *)((WORD8 *)sbr_persistent_mem_v + used_persistent);
377         used_persistent += MAX_QMF_BUF_LEN * sizeof(FLOAT32);
378       }
379 
380       for (index = 0; index < MAX_QMF_BUF_LEN; index++) {
381         p_str_sbr_dec_inst->pstr_sbr_channel[1]
382             ->str_sbr_dec.pp_qmf_buf_imag[index] =
383             (FLOAT32 *)((WORD8 *)sbr_persistent_mem_v + used_persistent);
384         used_persistent += MAX_QMF_BUF_LEN * sizeof(FLOAT32);
385       }
386     }
387   }
388 
389   *persistent_used = used_persistent;
390 }
391 
ia_enhaacplus_dec_get_sbr_buffers_size(WORD32 channels)392 WORD32 ia_enhaacplus_dec_get_sbr_buffers_size(WORD32 channels) {
393   WORD32 sbr_buffers_size = 0;
394   WORD32 temp, temp2;
395   WORD32 num_channel = channels;
396   WORD32 i;
397 
398   temp = num_channel * ((QMF_FILTER_STATE_ANA_SIZE + 2 * NO_ANALYSIS_CHANNELS) *
399                         sizeof(WORD16));
400   sbr_buffers_size += temp;
401 
402   temp2 =
403       (num_channel * ((QMF_FILTER_STATE_SYN_SIZE + 2 * NO_SYNTHESIS_CHANNELS) *
404                       sizeof(WORD16)));
405   sbr_buffers_size += temp2;
406 
407   for (i = 0; i < num_channel; i++) {
408     sbr_buffers_size +=
409         2 * MAX_OV_COLS * NO_SYNTHESIS_CHANNELS * sizeof(WORD32);
410   }
411 
412   for (i = 0; i < num_channel; i++) {
413     int j;
414     sbr_buffers_size += LPC_ORDER * sizeof(VOID *);
415 
416     for (j = 0; j < LPC_ORDER; j++) {
417       sbr_buffers_size += NO_ANALYSIS_CHANNELS * sizeof(WORD32);
418     }
419   }
420 
421   for (i = 0; i < num_channel; i++) {
422     int j;
423     sbr_buffers_size += LPC_ORDER * sizeof(WORD32);
424     for (j = 0; j < LPC_ORDER; j++) {
425       sbr_buffers_size += NO_ANALYSIS_CHANNELS * sizeof(WORD32);
426     }
427   }
428 
429   for (i = 0; i < num_channel; i++) {
430     WORD32 temp_used = sbr_buffers_size;
431 
432     temp_used += 2 * MAX_FREQ_COEFFS * sizeof(WORD16);
433     temp_used += MAX_FREQ_COEFFS * sizeof(WORD16);
434     temp_used += ALIGN_SIZE64(sizeof(ia_freq_band_data_struct));
435     temp_used += ALIGN_SIZE64(sizeof(ia_sbr_prev_frame_data_struct));
436     temp_used += ALIGN_SIZE64(sizeof(ia_sbr_channel_struct));
437     temp_used += ALIGN_SIZE64(sizeof(ia_sbr_header_data_struct));
438 
439     sbr_buffers_size = temp_used;
440   }
441 
442   sbr_buffers_size += sizeof(ia_ps_dec_struct);
443 
444   return sbr_buffers_size;
445 }
446 
ixheaacd_init_headerdata(ia_sbr_header_data_struct * ptr_header_data,WORD32 sample_rate_dec,WORD32 samp_per_frame,ia_freq_band_data_struct * freq_band_data,ia_sbr_tables_struct * sbr_tables,WORD audio_obj_type)447 static PLATFORM_INLINE VOID ixheaacd_init_headerdata(
448     ia_sbr_header_data_struct *ptr_header_data, WORD32 sample_rate_dec,
449     WORD32 samp_per_frame, ia_freq_band_data_struct *freq_band_data,
450     ia_sbr_tables_struct *sbr_tables, WORD audio_obj_type) {
451   ia_freq_band_data_struct *pstr_freq_band_data = freq_band_data;
452   WORD32 tmp;
453 
454   if (audio_obj_type != AOT_ER_AAC_ELD) {
455     memcpy(ptr_header_data,
456            &sbr_tables->env_extr_tables_ptr->str_sbr_default_header,
457            sizeof(ia_sbr_header_data_struct));
458   }
459 
460   if (audio_obj_type == AOT_ER_AAC_ELD) ptr_header_data->time_step -= 1;
461 
462   pstr_freq_band_data->freq_band_table[LOW] =
463       pstr_freq_band_data->freq_band_tbl_lo;
464   pstr_freq_band_data->freq_band_table[HIGH] =
465       pstr_freq_band_data->freq_band_tbl_hi;
466   ptr_header_data->pstr_freq_band_data = pstr_freq_band_data;
467 
468   ptr_header_data->core_frame_size = samp_per_frame;
469   ptr_header_data->out_sampling_freq = sample_rate_dec << 1;
470 
471   if (audio_obj_type != AOT_ER_AAC_ELD) {
472     tmp = ptr_header_data->time_step + 4;
473 
474     if (tmp < 0)
475       ptr_header_data->num_time_slots =
476           ixheaacd_extract16l(samp_per_frame << (-tmp));
477     else
478       ptr_header_data->num_time_slots =
479           ixheaacd_extract16l(samp_per_frame >> tmp);
480   } else {
481     ptr_header_data->time_step = 1;
482 
483     ptr_header_data->num_time_slots =
484         (samp_per_frame / 32 >> (ptr_header_data->time_step - 1));
485   }
486 }
487 
ixheaacd_setesbr_flags(VOID * sbr_persistent_mem_v,FLAG pvc_flag,FLAG hbe_flag,FLAG inter_tes_flag)488 VOID ixheaacd_setesbr_flags(VOID *sbr_persistent_mem_v, FLAG pvc_flag,
489                             FLAG hbe_flag, FLAG inter_tes_flag) {
490   ia_sbr_pers_struct *sbr_persistent_mem;
491   sbr_persistent_mem = (ia_sbr_pers_struct *)sbr_persistent_mem_v;
492   sbr_persistent_mem->str_sbr_dec_inst.hbe_flag = hbe_flag;
493   sbr_persistent_mem->str_sbr_dec_inst.pvc_flag = pvc_flag;
494   sbr_persistent_mem->str_sbr_dec_inst.inter_tes_flag = inter_tes_flag;
495   return;
496 }
497 
ixheaacd_init_sbr(WORD32 sample_rate_dec,WORD32 samp_per_frame,FLAG * down_sample_flag,VOID * sbr_persistent_mem_v,WORD32 * ptr_overlap_buf,WORD32 channel,WORD32 ps_enable,WORD32 sbr_ratio_idx,WORD32 output_frame_size,WORD32 * use_hbe,VOID * p_usac_dflt_header,ia_sbr_header_data_struct str_sbr_config,WORD32 audio_object_type)498 ia_handle_sbr_dec_inst_struct ixheaacd_init_sbr(
499     WORD32 sample_rate_dec, WORD32 samp_per_frame, FLAG *down_sample_flag,
500     VOID *sbr_persistent_mem_v, WORD32 *ptr_overlap_buf, WORD32 channel,
501     WORD32 ps_enable, WORD32 sbr_ratio_idx, WORD32 output_frame_size,
502     WORD32 *use_hbe, VOID *p_usac_dflt_header,
503     ia_sbr_header_data_struct str_sbr_config, WORD32 audio_object_type) {
504   WORD16 i;
505   WORD16 err;
506   ia_sbr_header_data_struct *ptr_header_data[MAXNRSBRCHANNELS];
507   ia_sbr_dec_struct *ptr_sbr_dec[2];
508   ia_qmf_dec_tables_struct *qmf_dec_tables_ptr;
509   ia_sbr_pers_struct *sbr_persistent_mem;
510 
511   sbr_persistent_mem = (ia_sbr_pers_struct *)sbr_persistent_mem_v;
512   ptr_sbr_dec[0] =
513       &sbr_persistent_mem->str_sbr_dec_inst.pstr_sbr_channel[0]->str_sbr_dec;
514   ptr_sbr_dec[1] =
515       &sbr_persistent_mem->str_sbr_dec_inst.pstr_sbr_channel[1]->str_sbr_dec;
516 
517   qmf_dec_tables_ptr =
518       sbr_persistent_mem->str_sbr_dec_inst.pstr_sbr_tables->qmf_dec_tables_ptr;
519 
520   if (sample_rate_dec > 48000) {
521     *down_sample_flag = 1;
522   }
523 
524   for (i = 0; i < channel; i++) {
525     if (audio_object_type == AOT_ER_AAC_ELD) {
526       memcpy(sbr_persistent_mem->str_sbr_dec_inst.pstr_sbr_header[i],
527              &str_sbr_config, sizeof(ia_sbr_header_data_struct));
528     }
529     ptr_header_data[i] =
530         sbr_persistent_mem->str_sbr_dec_inst.pstr_sbr_header[i];
531 
532     ixheaacd_init_headerdata(
533         ptr_header_data[i], sample_rate_dec, samp_per_frame,
534         sbr_persistent_mem->str_sbr_dec_inst.pstr_freq_band_data[i],
535         sbr_persistent_mem->str_sbr_dec_inst.pstr_sbr_tables,
536         audio_object_type);
537 
538     err = ixheaacd_create_sbrdec(
539 
540         sbr_persistent_mem->str_sbr_dec_inst.pstr_common_tables,
541         sbr_persistent_mem->str_sbr_dec_inst.pstr_sbr_channel[i],
542         ptr_header_data[i], i, *down_sample_flag, sbr_persistent_mem, ps_enable,
543         audio_object_type);
544 
545     ptr_header_data[i]->status = 1;
546 
547     if (err) {
548       return NULL;
549     }
550   }
551 
552   if (channel != 1) {
553     if (ps_enable) {
554       if (audio_object_type == AOT_ER_AAC_ELD)
555         ixheaacd_create_psdec(
556             sbr_persistent_mem->str_sbr_dec_inst.pstr_ps_stereo_dec,
557             sbr_persistent_mem, &ptr_overlap_buf[512 * 4]);
558       else
559         ixheaacd_create_psdec(
560             sbr_persistent_mem->str_sbr_dec_inst.pstr_ps_stereo_dec,
561             sbr_persistent_mem, ptr_overlap_buf);
562     }
563   }
564 
565   if (use_hbe != NULL) {
566     ia_sbr_header_data_struct *ptr_sbr_dflt_header =
567         &sbr_persistent_mem->str_sbr_dec_inst.str_sbr_dflt_header;
568     ia_sbr_header_data_struct *ptr_usac_dflt_header =
569         (ia_sbr_header_data_struct *)p_usac_dflt_header;
570     struct ia_sbr_dec_inst_struct *p_str_sbr_dec_inst =
571         &sbr_persistent_mem->str_sbr_dec_inst;
572     VOID *hbe_txposer_buffers = p_str_sbr_dec_inst->hbe_txposer_buffers;
573 
574     ptr_header_data[0] = p_str_sbr_dec_inst->pstr_sbr_header[0];
575     ptr_header_data[1] = p_str_sbr_dec_inst->pstr_sbr_header[1];
576 
577     ptr_header_data[0]->sbr_ratio_idx = sbr_ratio_idx;
578     ptr_header_data[0]->output_framesize = output_frame_size;
579     ptr_header_data[0]->pstr_freq_band_data->sub_band_start = 64;
580     ptr_header_data[0]->esbr_start_up = 1;
581     ptr_header_data[0]->esbr_start_up_pvc = 1;
582 
583     if (channel > 1) {
584       ptr_header_data[1]->sbr_ratio_idx = sbr_ratio_idx;
585       ptr_header_data[1]->output_framesize = output_frame_size;
586       ptr_header_data[1]->pstr_freq_band_data->sub_band_start = 64;
587       ptr_header_data[1]->esbr_start_up = 1;
588       ptr_header_data[1]->esbr_start_up_pvc = 1;
589     }
590     if (hbe_txposer_buffers != NULL && (use_hbe[0] == 1)) {
591       ixheaacd_esbr_hbe_data_init(ptr_sbr_dec[0]->p_hbe_txposer, samp_per_frame,
592                                   sbr_ratio_idx == SBR_UPSAMPLE_IDX_4_1 ? 1 : 0,
593                                   output_frame_size, hbe_txposer_buffers);
594 
595       hbe_txposer_buffers =
596           (WORD8 *)hbe_txposer_buffers + MAX_HBE_PERSISTENT_SIZE;
597 
598       ixheaacd_esbr_hbe_data_init(ptr_sbr_dec[1]->p_hbe_txposer, samp_per_frame,
599                                   sbr_ratio_idx == SBR_UPSAMPLE_IDX_4_1 ? 1 : 0,
600                                   output_frame_size, hbe_txposer_buffers);
601     }
602 
603     p_str_sbr_dec_inst->ptr_pvc_data_str->prev_first_bnd_idx = -1;
604     p_str_sbr_dec_inst->ptr_pvc_data_str->prev_pvc_rate = -1;
605     p_str_sbr_dec_inst->ptr_pvc_data_str->prev_sbr_mode = UNKNOWN_SBR;
606 
607     p_str_sbr_dec_inst->pstr_sbr_channel[0]
608         ->str_sbr_dec.str_codec_qmf_bank.num_time_slots =
609         output_frame_size / 64;
610     p_str_sbr_dec_inst->pstr_sbr_channel[1]
611         ->str_sbr_dec.str_codec_qmf_bank.num_time_slots =
612         output_frame_size / 64;
613 
614     ptr_header_data[0]->core_frame_size = samp_per_frame;
615     ptr_header_data[1]->core_frame_size = samp_per_frame;
616 
617     switch (sbr_ratio_idx) {
618       case SBR_UPSAMPLE_IDX_0_0:
619         ptr_sbr_dec[0]->str_codec_qmf_bank.no_channels = 32;
620         ptr_sbr_dec[0]->str_codec_qmf_bank.esbr_cos_twiddle =
621             (WORD32 *)qmf_dec_tables_ptr->esbr_sin_cos_twiddle_l32;
622         ptr_sbr_dec[0]->str_codec_qmf_bank.esbr_alt_sin_twiddle =
623             (WORD32 *)qmf_dec_tables_ptr->esbr_alt_sin_twiddle_l32;
624         ptr_sbr_dec[0]->str_codec_qmf_bank.esbr_t_cos =
625             (WORD32 *)qmf_dec_tables_ptr->esbr_t_cos_sin_l32;
626         ptr_header_data[0]->is_usf_4 = 0;
627         ptr_header_data[0]->upsamp_fac = 1;
628 
629         ptr_sbr_dec[1]->str_codec_qmf_bank.no_channels = 32;
630         ptr_sbr_dec[0]->str_codec_qmf_bank.esbr_cos_twiddle =
631             (WORD32 *)qmf_dec_tables_ptr->esbr_sin_cos_twiddle_l32;
632         ptr_sbr_dec[0]->str_codec_qmf_bank.esbr_alt_sin_twiddle =
633             (WORD32 *)qmf_dec_tables_ptr->esbr_alt_sin_twiddle_l32;
634         ptr_sbr_dec[0]->str_codec_qmf_bank.esbr_t_cos =
635             (WORD32 *)qmf_dec_tables_ptr->esbr_t_cos_sin_l32;
636         ptr_header_data[1]->is_usf_4 = 0;
637         ptr_header_data[1]->upsamp_fac = 1;
638         break;
639       case SBR_UPSAMPLE_IDX_2_1:
640         ptr_sbr_dec[0]->str_codec_qmf_bank.no_channels = 32;
641         ptr_sbr_dec[0]->str_codec_qmf_bank.esbr_cos_twiddle =
642             (WORD32 *)qmf_dec_tables_ptr->esbr_sin_cos_twiddle_l32;
643         ptr_sbr_dec[0]->str_codec_qmf_bank.esbr_alt_sin_twiddle =
644             (WORD32 *)qmf_dec_tables_ptr->esbr_alt_sin_twiddle_l32;
645         ptr_sbr_dec[0]->str_codec_qmf_bank.esbr_t_cos =
646             (WORD32 *)qmf_dec_tables_ptr->esbr_t_cos_sin_l32;
647         ptr_header_data[0]->is_usf_4 = 0;
648         ptr_header_data[0]->upsamp_fac = 2;
649 
650         ptr_sbr_dec[1]->str_codec_qmf_bank.no_channels = 32;
651         ptr_sbr_dec[1]->str_codec_qmf_bank.esbr_cos_twiddle =
652             (WORD32 *)qmf_dec_tables_ptr->esbr_sin_cos_twiddle_l32;
653         ptr_sbr_dec[1]->str_codec_qmf_bank.esbr_alt_sin_twiddle =
654             (WORD32 *)qmf_dec_tables_ptr->esbr_alt_sin_twiddle_l32;
655         ptr_sbr_dec[1]->str_codec_qmf_bank.esbr_t_cos =
656             (WORD32 *)qmf_dec_tables_ptr->esbr_t_cos_sin_l32;
657         ptr_header_data[1]->is_usf_4 = 0;
658         ptr_header_data[1]->upsamp_fac = 2;
659         break;
660       case SBR_UPSAMPLE_IDX_8_3:
661         ptr_sbr_dec[0]->str_codec_qmf_bank.no_channels = 24;
662         ptr_sbr_dec[0]->str_codec_qmf_bank.filter_pos_32 =
663             qmf_dec_tables_ptr->esbr_qmf_c_24;
664         ptr_sbr_dec[0]->str_codec_qmf_bank.analy_win_coeff_32 =
665             qmf_dec_tables_ptr->esbr_qmf_c_24;
666         ptr_sbr_dec[0]->str_codec_qmf_bank.esbr_cos_twiddle =
667             (WORD32 *)qmf_dec_tables_ptr->esbr_sin_cos_twiddle_l24;
668         ptr_sbr_dec[0]->str_codec_qmf_bank.esbr_alt_sin_twiddle =
669             (WORD32 *)qmf_dec_tables_ptr->esbr_alt_sin_twiddle_l24;
670         ptr_sbr_dec[0]->str_codec_qmf_bank.esbr_t_cos =
671             (WORD32 *)qmf_dec_tables_ptr->esbr_t_cos_sin_l24;
672 
673         ptr_header_data[0]->is_usf_4 = 0;
674         ptr_header_data[0]->upsamp_fac = 2;
675 
676         ptr_sbr_dec[1]->str_codec_qmf_bank.no_channels = 24;
677         ptr_sbr_dec[1]->str_codec_qmf_bank.filter_pos_32 =
678             qmf_dec_tables_ptr->esbr_qmf_c_24;
679         ptr_sbr_dec[1]->str_codec_qmf_bank.analy_win_coeff_32 =
680             qmf_dec_tables_ptr->esbr_qmf_c_24;
681         ptr_sbr_dec[1]->str_codec_qmf_bank.esbr_cos_twiddle =
682             (WORD32 *)qmf_dec_tables_ptr->esbr_sin_cos_twiddle_l24;
683         ptr_sbr_dec[1]->str_codec_qmf_bank.esbr_alt_sin_twiddle =
684             (WORD32 *)qmf_dec_tables_ptr->esbr_alt_sin_twiddle_l24;
685         ptr_sbr_dec[1]->str_codec_qmf_bank.esbr_t_cos =
686             (WORD32 *)qmf_dec_tables_ptr->esbr_t_cos_sin_l24;
687 
688         ptr_header_data[1]->is_usf_4 = 0;
689         ptr_header_data[1]->upsamp_fac = 2;
690         break;
691       case SBR_UPSAMPLE_IDX_4_1:
692         ptr_sbr_dec[0]->str_codec_qmf_bank.no_channels = 16;
693         ptr_sbr_dec[0]->str_codec_qmf_bank.esbr_cos_twiddle =
694             (WORD32 *)qmf_dec_tables_ptr->esbr_sin_cos_twiddle_l16;
695         ptr_sbr_dec[0]->str_codec_qmf_bank.esbr_alt_sin_twiddle =
696             (WORD32 *)qmf_dec_tables_ptr->esbr_alt_sin_twiddle_l16;
697         ptr_sbr_dec[0]->str_codec_qmf_bank.esbr_t_cos =
698             (WORD32 *)qmf_dec_tables_ptr->esbr_t_cos_sin_l16;
699         ptr_header_data[0]->is_usf_4 = 1;
700         ptr_header_data[0]->upsamp_fac = 4;
701         ptr_header_data[0]->out_sampling_freq =
702             ptr_header_data[0]->out_sampling_freq * 2;
703 
704         ptr_sbr_dec[1]->str_codec_qmf_bank.no_channels = 16;
705         ptr_sbr_dec[1]->str_codec_qmf_bank.esbr_cos_twiddle =
706             (WORD32 *)qmf_dec_tables_ptr->esbr_sin_cos_twiddle_l16;
707         ptr_sbr_dec[1]->str_codec_qmf_bank.esbr_alt_sin_twiddle =
708             (WORD32 *)qmf_dec_tables_ptr->esbr_alt_sin_twiddle_l16;
709         ptr_sbr_dec[1]->str_codec_qmf_bank.esbr_t_cos =
710             (WORD32 *)qmf_dec_tables_ptr->esbr_t_cos_sin_l16;
711         ptr_header_data[1]->is_usf_4 = 1;
712         ptr_header_data[1]->upsamp_fac = 4;
713         ptr_header_data[1]->out_sampling_freq =
714             ptr_header_data[1]->out_sampling_freq * 2;
715         break;
716     }
717 
718     ptr_sbr_dflt_header->start_freq = ptr_usac_dflt_header->start_freq;
719     ptr_sbr_dflt_header->stop_freq = ptr_usac_dflt_header->stop_freq;
720 
721     if (ptr_usac_dflt_header->header_extra_1) {
722       ptr_sbr_dflt_header->freq_scale = ptr_usac_dflt_header->freq_scale;
723       ptr_sbr_dflt_header->alter_scale = ptr_usac_dflt_header->alter_scale;
724       ptr_sbr_dflt_header->noise_bands = ptr_usac_dflt_header->noise_bands;
725     } else {
726       ptr_sbr_dflt_header->freq_scale = SBR_FREQ_SCALE_DEFAULT;
727       ptr_sbr_dflt_header->alter_scale = SBR_ALTER_SCALE_DEFAULT;
728       ptr_sbr_dflt_header->noise_bands = SBR_NOISE_BANDS_DEFAULT;
729     }
730 
731     if (ptr_usac_dflt_header->header_extra_2) {
732       ptr_sbr_dflt_header->limiter_bands = ptr_usac_dflt_header->limiter_bands;
733       ptr_sbr_dflt_header->limiter_gains = ptr_usac_dflt_header->limiter_gains;
734       ptr_sbr_dflt_header->interpol_freq = ptr_usac_dflt_header->interpol_freq;
735       ptr_sbr_dflt_header->smoothing_mode =
736           ptr_usac_dflt_header->smoothing_mode;
737     } else {
738       ptr_sbr_dflt_header->limiter_bands = SBR_LIMITER_BANDS_DEFAULT;
739       ptr_sbr_dflt_header->limiter_gains = SBR_LIMITER_GAINS_DEFAULT;
740       ptr_sbr_dflt_header->interpol_freq = SBR_INTERPOL_FREQ_DEFAULT;
741       ptr_sbr_dflt_header->smoothing_mode = SBR_SMOOTHING_LENGTH_DEFAULT;
742     }
743   }
744   return &(sbr_persistent_mem->str_sbr_dec_inst);
745 }
746 
ixheaacd_create_sbr_env_calc(ixheaacd_misc_tables * pstr_common_table,ia_sbr_calc_env_struct * hs,WORD16 chan,VOID * sbr_persistent_mem_v,ia_sbr_header_data_struct * ptr_header_data,WORD audio_object_type)747 static PLATFORM_INLINE WORD16 ixheaacd_create_sbr_env_calc(
748 
749     ixheaacd_misc_tables *pstr_common_table, ia_sbr_calc_env_struct *hs,
750     WORD16 chan, VOID *sbr_persistent_mem_v,
751     ia_sbr_header_data_struct *ptr_header_data, WORD audio_object_type) {
752   WORD16 err;
753   ia_sbr_pers_struct *sbr_persistent_mem =
754       (ia_sbr_pers_struct *)sbr_persistent_mem_v;
755 
756   err = 0;
757   memset(&hs->harm_flags_prev[0], 0, sizeof(WORD8) * MAX_FREQ_COEFFS);
758 
759   hs->harm_index = 0;
760 
761   hs->filt_buf_me = sbr_persistent_mem->sbr_smooth_gain_buf[chan];
762   hs->filt_buf_noise_m = sbr_persistent_mem->sbr_smooth_noise_buf[chan];
763   hs->tansient_env_prev = -1;
764 
765   ixheaacd_reset_sbrenvelope_calc(hs);
766 
767   if ((chan == 0) && (audio_object_type == AOT_ER_AAC_ELD)) {
768     err = ixheaacd_calc_frq_bnd_tbls(ptr_header_data, pstr_common_table);
769   }
770 
771   return err;
772 }
773 
ixheaacd_init_sbr_prev_framedata(ia_sbr_prev_frame_data_struct * ptr_prev_data,WORD16 time_slots)774 static PLATFORM_INLINE VOID ixheaacd_init_sbr_prev_framedata(
775     ia_sbr_prev_frame_data_struct *ptr_prev_data, WORD16 time_slots) {
776   WORD16 *psfb_nrg_prev = ptr_prev_data->sfb_nrg_prev;
777   WORD16 *psfb_noise_level = ptr_prev_data->prev_noise_level;
778   WORD32 *ppsbr_invf_mode = ptr_prev_data->sbr_invf_mode;
779 
780   memset(psfb_nrg_prev, 0, sizeof(WORD16) * (MAX_FREQ_COEFFS));
781   memset(psfb_noise_level, 0, sizeof(WORD16) * (MAX_NOISE_COEFFS));
782 
783   memset(ppsbr_invf_mode, 0, sizeof(WORD32) * MAX_INVF_BANDS);
784 
785   ptr_prev_data->end_position = time_slots;
786   ptr_prev_data->coupling_mode = COUPLING_OFF;
787   ptr_prev_data->amp_res = 0;
788   ptr_prev_data->max_qmf_subband_aac = 0;
789 }
790 
791 static PLATFORM_INLINE VOID
ixheaacd_create_hyb_filterbank(ia_hybrid_struct * ptr_hybrid,WORD32 ** p_ptr,ia_sbr_tables_struct * sbr_tables_ptr)792 ixheaacd_create_hyb_filterbank(ia_hybrid_struct *ptr_hybrid, WORD32 **p_ptr,
793                                ia_sbr_tables_struct *sbr_tables_ptr) {
794   WORD16 i, ptr_step;
795   WORD32 *ptr = (WORD32 *)*p_ptr;
796 
797   ptr_hybrid->ptr_resol = sbr_tables_ptr->ps_tables_ptr->hyb_resol;
798   ptr_hybrid->ptr_qmf_buf = HYBRID_FILTER_LENGTH - 1;
799 
800   ptr_hybrid->ptr_temp_re = ptr;
801   ptr += NO_HYBRID_CHANNELS_HIGH;
802   ptr_hybrid->ptr_temp_im = ptr;
803   ptr += NO_HYBRID_CHANNELS_HIGH;
804 
805   memset(ptr_hybrid->ptr_temp_re, 0,
806          2 * NO_HYBRID_CHANNELS_HIGH * sizeof(WORD32));
807 
808   ptr_step = ixheaacd_add16(1, ptr_hybrid->ptr_qmf_buf);
809   ptr_hybrid->ptr_work_re = ptr;
810   ptr += 16;
811   ptr_hybrid->ptr_work_im = ptr;
812   ptr += 16;
813 
814   for (i = 0; i < NO_QMF_CHANNELS_IN_HYBRID; i++) {
815     ptr_hybrid->ptr_qmf_buf_re[i] = ptr;
816     ptr += ptr_hybrid->ptr_qmf_buf;
817 
818     ptr_hybrid->ptr_qmf_buf_im[i] = ptr;
819     ptr += ptr_hybrid->ptr_qmf_buf;
820 
821     memset(ptr_hybrid->ptr_qmf_buf_re[i], 0,
822            2 * ptr_hybrid->ptr_qmf_buf * sizeof(WORD32));
823   }
824 
825   *p_ptr = ptr;
826 
827   return;
828 }
829 
ixheaacd_create_hf_generator(ia_sbr_hf_generator_struct * ptr_hf_gen_str,WORD16 num_columns,WORD16 chan,VOID * sbr_persistent_mem_v,WORD32 ps_enable)830 static PLATFORM_INLINE VOID ixheaacd_create_hf_generator(
831     ia_sbr_hf_generator_struct *ptr_hf_gen_str, WORD16 num_columns, WORD16 chan,
832     VOID *sbr_persistent_mem_v, WORD32 ps_enable) {
833   WORD16 i;
834   ia_sbr_pers_struct *sbr_persistent_mem =
835       (ia_sbr_pers_struct *)sbr_persistent_mem_v;
836 
837   ptr_hf_gen_str->pstr_settings = &sbr_persistent_mem->str_sbr_tran_settings;
838 
839   ptr_hf_gen_str->lpc_filt_states_real[0] =
840       sbr_persistent_mem->sbr_lpc_filter_states_real[chan][0];
841   ptr_hf_gen_str->lpc_filt_states_real[1] =
842       sbr_persistent_mem->sbr_lpc_filter_states_real[chan][1];
843 
844   if (ps_enable) {
845     ptr_hf_gen_str->lpc_filt_states_imag[0] =
846         sbr_persistent_mem->sbr_lpc_filter_states_imag[chan][0];
847     ptr_hf_gen_str->lpc_filt_states_imag[1] =
848         sbr_persistent_mem->sbr_lpc_filter_states_imag[chan][1];
849   }
850 
851   for (i = 0; i < LPC_ORDER; i++) {
852     memset(ptr_hf_gen_str->lpc_filt_states_real[i], 0,
853            NO_ANALYSIS_CHANNELS * sizeof(WORD32));
854 
855     if (ps_enable)
856       memset(ptr_hf_gen_str->lpc_filt_states_imag[i], 0,
857              NO_ANALYSIS_CHANNELS * sizeof(WORD32));
858   }
859 
860   if (chan == 0) {
861     ptr_hf_gen_str->pstr_settings->num_columns = num_columns;
862   }
863   return;
864 }
865 
ixheaacd_create_psdec(ia_ps_dec_struct * ptr_ps_dec,VOID * sbr_persistent_mem_v,WORD32 * ptr_overlap_buf)866 VOID ixheaacd_create_psdec(ia_ps_dec_struct *ptr_ps_dec,
867                            VOID *sbr_persistent_mem_v,
868                            WORD32 *ptr_overlap_buf) {
869   ia_sbr_pers_struct *sbr_persistent_mem =
870       (ia_sbr_pers_struct *)sbr_persistent_mem_v;
871 
872   WORD16 *ptr1 = (WORD16 *)&(
873       sbr_persistent_mem->ptr_sbr_overlap_buf[MAXNRSBRCHANNELS - 1][0]);
874   WORD32 *ptr2 = (WORD32 *)&ptr_overlap_buf[512];
875   WORD16 *initial_ptr;
876   WORD16 delay;
877   WORD32 temp;
878 
879   ia_sbr_tables_struct *sbr_tables_ptr =
880       sbr_persistent_mem->str_sbr_dec_inst.pstr_sbr_tables;
881 
882   memset(ptr_ps_dec, 0, sizeof(ia_ps_dec_struct));
883 
884   ptr_ps_dec->ps_data_present = 0;
885   ptr_ps_dec->enable_iid = 0;
886   ptr_ps_dec->enable_icc = 0;
887   ptr_ps_dec->enable_ext = 0;
888   ptr_ps_dec->iid_mode = 0;
889   ptr_ps_dec->icc_mode = 0;
890 
891   ptr_ps_dec->ptr_hyb_left_re = ptr2;
892   ptr2 += 16;
893   ptr_ps_dec->ptr_hyb_left_im = ptr2;
894   ptr2 += 16;
895   ptr_ps_dec->ptr_hyb_right_re = ptr2;
896   ptr2 += 16;
897   ptr_ps_dec->ptr_hyb_right_im = ptr2;
898   ptr2 += 16;
899 
900   memset(ptr_ps_dec->ptr_hyb_left_re, 0, sizeof(WORD32) * 16 * 4);
901 
902   ixheaacd_create_hyb_filterbank(&ptr_ps_dec->str_hybrid, &ptr2,
903                                  sbr_tables_ptr);
904 
905   ptr_ps_dec->peak_decay_diff = ptr2;
906   ptr2 += NUM_OF_BINS;
907   ptr_ps_dec->energy_prev = ptr2;
908   ptr2 += NUM_OF_BINS;
909   ptr_ps_dec->peak_decay_diff_prev = ptr2;
910   ptr2 += NUM_OF_BINS;
911 
912   memset(ptr_ps_dec->peak_decay_diff, 0, 3 * sizeof(WORD32) * NUM_OF_BINS);
913 
914   ptr_ps_dec->delay_buf_idx = 0;
915   ptr_ps_dec->delay_buf_idx_long = 0;
916 
917   memset(ptr_ps_dec->delay_buf_qmf_sub_re_im, 0,
918          2 * 16 * DEL_ALL_PASS * sizeof(WORD16));
919   memset(ptr_ps_dec->delay_buf_qmf_sub_ser_re_im, 0,
920          2 * 16 * NUM_SER_AP_LINKS * 5 * sizeof(WORD16));
921 
922   initial_ptr = ptr1;
923   ptr_ps_dec->delay_buf_qmf_ser_re_im = (VOID *)ptr1;
924   ptr1 += 2 * NUM_SER_AP_LINKS * 32 * 5;
925 
926   delay = 2;
927   ptr_ps_dec->delay_buf_qmf_ap_re_im = (VOID *)ptr1;
928   ptr1 += 2 * delay * 32;
929 
930   delay = HIGH_DEL;
931   ptr_ps_dec->delay_buf_qmf_ld_re_im = (VOID *)ptr1;
932   ptr1 += 2 * delay * SMALL_DEL_STRT;
933 
934   delay = SMALL_DEL;
935   ptr_ps_dec->delay_buf_qmf_sd_re_im = (VOID *)ptr1;
936   ptr1 +=
937       2 * delay * (NUM_OF_QUAD_MIRROR_FILTER_ICC_CHNLS -
938                    (NUM_OF_QUAD_MIRROR_FILTER_ALL_PASS_CHNLS + SMALL_DEL_STRT));
939 
940   temp = ptr1 - initial_ptr;
941   memset(ptr_ps_dec->delay_buf_qmf_ser_re_im, 0, temp * sizeof(WORD16));
942 
943   memset(ptr_ps_dec->delay_buf_idx_ser, 0, NUM_SER_AP_LINKS * sizeof(WORD16));
944   memcpy(ptr_ps_dec->delay_sample_ser,
945          sbr_tables_ptr->ps_tables_ptr->rev_link_delay_ser,
946          NUM_SER_AP_LINKS * sizeof(WORD16));
947 
948   memset(ptr_ps_dec->h11_h12_vec, 0xff,
949          (NO_IID_GROUPS + 2) * 2 * sizeof(WORD16));
950   memset(ptr_ps_dec->h21_h22_vec, 0, sizeof(ptr_ps_dec->h21_h22_vec));
951 
952   return;
953 }
954 
ixheaacd_create_cplx_anal_qmfbank(ia_sbr_qmf_filter_bank_struct * ptr_sbr_qmf,ia_sbr_scale_fact_struct * sbr_scale_factor,WORD16 no_bins,WORD16 usb,WORD16 chan,WORD16 * sbr_qmf_analy_states,WORD32 * sbr_qmf_analy_states_32,ia_qmf_dec_tables_struct * qmf_dec_tables_ptr,WORD32 audio_object_type)955 static PLATFORM_INLINE VOID ixheaacd_create_cplx_anal_qmfbank(
956     ia_sbr_qmf_filter_bank_struct *ptr_sbr_qmf,
957     ia_sbr_scale_fact_struct *sbr_scale_factor, WORD16 no_bins, WORD16 usb,
958     WORD16 chan, WORD16 *sbr_qmf_analy_states, WORD32 *sbr_qmf_analy_states_32,
959     ia_qmf_dec_tables_struct *qmf_dec_tables_ptr, WORD32 audio_object_type) {
960   memset(ptr_sbr_qmf, 0, sizeof(ia_sbr_qmf_filter_bank_struct));
961 
962   if (audio_object_type != AOT_ER_AAC_ELD &&
963       audio_object_type != AOT_ER_AAC_LD) {
964     ptr_sbr_qmf->analy_win_coeff = qmf_dec_tables_ptr->qmf_c;
965   } else {
966     ptr_sbr_qmf->analy_win_coeff = qmf_dec_tables_ptr->qmf_c_eld3;
967   }
968 
969   ptr_sbr_qmf->analy_win_coeff_32 = qmf_dec_tables_ptr->esbr_qmf_c;
970 
971   ptr_sbr_qmf->no_channels = NO_ANALYSIS_CHANNELS;
972   ptr_sbr_qmf->num_time_slots = no_bins;
973 
974   ptr_sbr_qmf->lsb = 0;
975   ptr_sbr_qmf->usb = usb;
976 
977   ptr_sbr_qmf->anal_filter_states =
978       &(sbr_qmf_analy_states[chan ? QMF_FILTER_STATE_ANA_SIZE : 0]);
979 
980   memset(ptr_sbr_qmf->anal_filter_states, 0,
981          sizeof(WORD16) * QMF_FILTER_STATE_ANA_SIZE);
982 
983   ptr_sbr_qmf->anal_filter_states_32 =
984       &(sbr_qmf_analy_states_32[chan ? QMF_FILTER_STATE_ANA_SIZE : 0]);
985 
986   memset(ptr_sbr_qmf->anal_filter_states_32, 0,
987          sizeof(WORD32) * QMF_FILTER_STATE_ANA_SIZE);
988 
989   ptr_sbr_qmf->core_samples_buffer = ptr_sbr_qmf->anal_filter_states;
990 
991   ptr_sbr_qmf->state_new_samples_pos_low_32 =
992       ptr_sbr_qmf->anal_filter_states_32;
993   if (audio_object_type != AOT_ER_AAC_ELD &&
994       audio_object_type != AOT_ER_AAC_LD) {
995     ptr_sbr_qmf->filter_pos = (WORD16 *)qmf_dec_tables_ptr->qmf_c;
996   } else {
997     ptr_sbr_qmf->filter_pos = (WORD16 *)qmf_dec_tables_ptr->qmf_c_eld3;
998   }
999 
1000   ptr_sbr_qmf->filter_pos_32 = (WORD32 *)qmf_dec_tables_ptr->esbr_qmf_c;
1001 
1002   sbr_scale_factor->st_lb_scale = 0;
1003 
1004   sbr_scale_factor->st_syn_scale = -6;
1005 
1006   if (audio_object_type == AOT_ER_AAC_ELD ||
1007       audio_object_type == AOT_ER_AAC_LD) {
1008     ptr_sbr_qmf->filter_2 = ptr_sbr_qmf->filter_pos + 32;
1009     ptr_sbr_qmf->fp1_anal = ptr_sbr_qmf->anal_filter_states;
1010     ptr_sbr_qmf->fp2_anal =
1011         ptr_sbr_qmf->anal_filter_states + NO_ANALYSIS_CHANNELS;
1012   }
1013 
1014   return;
1015 }
1016 
ixheaacd_create_cplx_synt_qmfbank(ia_sbr_qmf_filter_bank_struct * ptr_sbr_qmf,WORD16 no_bins,WORD16 lsb,WORD16 usb,WORD16 chan,FLAG down_sample_flag,WORD16 * sbr_qmf_synth_states,WORD32 * sbr_qmf_synth_states_32,ia_qmf_dec_tables_struct * qmf_dec_tables_ptr,WORD32 audio_object_type)1017 static PLATFORM_INLINE VOID ixheaacd_create_cplx_synt_qmfbank(
1018     ia_sbr_qmf_filter_bank_struct *ptr_sbr_qmf, WORD16 no_bins, WORD16 lsb,
1019     WORD16 usb, WORD16 chan, FLAG down_sample_flag,
1020     WORD16 *sbr_qmf_synth_states, WORD32 *sbr_qmf_synth_states_32,
1021     ia_qmf_dec_tables_struct *qmf_dec_tables_ptr, WORD32 audio_object_type) {
1022   WORD32 L;
1023 
1024   WORD32 qmf_filter_state_size;
1025 
1026   memset(ptr_sbr_qmf, 0, sizeof(ia_sbr_qmf_filter_bank_struct));
1027   qmf_filter_state_size = QMF_FILTER_STATE_SYN_SIZE;
1028 
1029   if (down_sample_flag) {
1030     L = NO_SYNTHESIS_CHANNELS_DOWN_SAMPLED;
1031     ptr_sbr_qmf->usb = NO_SYNTHESIS_CHANNELS_DOWN_SAMPLED;
1032   } else {
1033     L = NO_SYNTHESIS_CHANNELS;
1034     ptr_sbr_qmf->usb = usb;
1035   }
1036 
1037   ptr_sbr_qmf->ixheaacd_drc_offset = 0;
1038   if (audio_object_type != AOT_ER_AAC_ELD &&
1039       audio_object_type != AOT_ER_AAC_LD) {
1040     ptr_sbr_qmf->filter_pos_syn = (WORD16 *)qmf_dec_tables_ptr->qmf_c;
1041     ptr_sbr_qmf->p_filter = qmf_dec_tables_ptr->qmf_c;
1042   } else {
1043     ptr_sbr_qmf->filter_pos_syn = (WORD16 *)qmf_dec_tables_ptr->qmf_c_eld;
1044     ptr_sbr_qmf->p_filter = qmf_dec_tables_ptr->qmf_c_eld;
1045   }
1046 
1047   ptr_sbr_qmf->filter_pos_syn_32 = (WORD32 *)qmf_dec_tables_ptr->esbr_qmf_c;
1048   ptr_sbr_qmf->p_filter_32 = qmf_dec_tables_ptr->esbr_qmf_c;
1049 
1050   ptr_sbr_qmf->no_channels = L;
1051   ptr_sbr_qmf->qmf_filter_state_size = qmf_filter_state_size;
1052   ptr_sbr_qmf->num_time_slots = no_bins;
1053   ptr_sbr_qmf->lsb = lsb;
1054 
1055   ptr_sbr_qmf->filter_states =
1056       &sbr_qmf_synth_states[chan ? qmf_filter_state_size : 0];
1057 
1058   memset(ptr_sbr_qmf->filter_states, 0, sizeof(WORD16) * qmf_filter_state_size);
1059 
1060   ptr_sbr_qmf->filter_states_32 =
1061       &sbr_qmf_synth_states_32[chan ? qmf_filter_state_size : 0];
1062 
1063   memset(ptr_sbr_qmf->filter_states_32, 0,
1064          sizeof(WORD32) * qmf_filter_state_size);
1065 
1066   if (audio_object_type == AOT_ER_AAC_ELD ||
1067       audio_object_type == AOT_ER_AAC_LD) {
1068     ptr_sbr_qmf->fp1_syn = ptr_sbr_qmf->filter_states;
1069     ptr_sbr_qmf->fp2_syn =
1070         ptr_sbr_qmf->filter_states + ptr_sbr_qmf->no_channels;
1071     ptr_sbr_qmf->sixty4 = NO_SYNTHESIS_CHANNELS;
1072   }
1073 
1074   return;
1075 }
1076 
ixheaacd_create_sbrdec(ixheaacd_misc_tables * pstr_common_table,ia_sbr_channel_struct * ptr_sbr_channel,ia_sbr_header_data_struct * ptr_header_data,WORD16 chan,FLAG down_sample_flag,VOID * sbr_persistent_mem_v,WORD ps_enable,WORD audio_object_type)1077 WORD16 ixheaacd_create_sbrdec(ixheaacd_misc_tables *pstr_common_table,
1078                               ia_sbr_channel_struct *ptr_sbr_channel,
1079                               ia_sbr_header_data_struct *ptr_header_data,
1080                               WORD16 chan, FLAG down_sample_flag,
1081                               VOID *sbr_persistent_mem_v, WORD ps_enable,
1082                               WORD audio_object_type)
1083 
1084 {
1085   WORD16 err;
1086   WORD16 time_slots;
1087   WORD16 no_bins;
1088   ia_sbr_pers_struct *sbr_persistent_mem =
1089       (ia_sbr_pers_struct *)sbr_persistent_mem_v;
1090   ia_sbr_dec_struct *hs = &(ptr_sbr_channel->str_sbr_dec);
1091 
1092   time_slots = ptr_header_data->num_time_slots;
1093 
1094   no_bins = (WORD16)(time_slots * ptr_header_data->time_step);
1095 
1096   hs->str_sbr_scale_fact.ov_lb_scale = INT_BITS - 1;
1097   hs->str_sbr_scale_fact.hb_scale = INT_BITS - 1;
1098   hs->str_sbr_scale_fact.ov_hb_scale = INT_BITS - 1;
1099   hs->str_sbr_scale_fact.st_syn_scale = INT_BITS - 1;
1100 
1101   ptr_sbr_channel->pstr_prev_frame_data =
1102       sbr_persistent_mem->pstr_prev_frame_data[chan];
1103 
1104   err = ixheaacd_create_sbr_env_calc(pstr_common_table, &hs->str_sbr_calc_env,
1105                                      chan, sbr_persistent_mem, ptr_header_data,
1106                                      audio_object_type);
1107 
1108   if (err) {
1109     return (-1);
1110   }
1111 
1112   ixheaacd_create_cplx_anal_qmfbank(
1113       &hs->str_codec_qmf_bank, &hs->str_sbr_scale_fact, no_bins,
1114       ptr_header_data->pstr_freq_band_data->sub_band_start, chan,
1115       sbr_persistent_mem->sbr_qmf_analy_states,
1116       sbr_persistent_mem->sbr_qmf_analy_states_32,
1117       sbr_persistent_mem->str_sbr_dec_inst.pstr_sbr_tables->qmf_dec_tables_ptr,
1118       audio_object_type);
1119 
1120   ixheaacd_create_cplx_synt_qmfbank(
1121       &hs->str_synthesis_qmf_bank, no_bins,
1122       ptr_header_data->pstr_freq_band_data->sub_band_start,
1123       ptr_header_data->pstr_freq_band_data->sub_band_end, chan,
1124       down_sample_flag, sbr_persistent_mem->sbr_qmf_synth_states,
1125       sbr_persistent_mem->sbr_qmf_synth_states_32,
1126       sbr_persistent_mem->str_sbr_dec_inst.pstr_sbr_tables->qmf_dec_tables_ptr,
1127       audio_object_type);
1128 
1129   ixheaacd_init_sbr_prev_framedata(ptr_sbr_channel->pstr_prev_frame_data,
1130                                    time_slots);
1131 
1132   ixheaacd_create_hf_generator(&hs->str_hf_generator,
1133                                hs->str_codec_qmf_bank.num_time_slots, chan,
1134                                sbr_persistent_mem, ps_enable);
1135 
1136   hs->ptr_sbr_overlap_buf = sbr_persistent_mem->ptr_sbr_overlap_buf[chan];
1137 
1138   return 0;
1139 }
1140