• 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 <stdio.h>
22 #include <stdlib.h>
23 #include "ixheaacd_sbr_common.h"
24 #include "ixheaacd_type_def.h"
25 #include "ixheaacd_memory_standards.h"
26 #include "ixheaacd_mps_struct_def.h"
27 #include "ixheaacd_mps_res_rom.h"
28 #include "ixheaacd_mps_aac_struct.h"
29 #include "ixheaacd_constants.h"
30 #include "ixheaacd_basic_ops32.h"
31 #include "ixheaacd_basic_ops16.h"
32 #include "ixheaacd_basic_ops40.h"
33 #include "ixheaacd_basic_ops.h"
34 
35 #include "ixheaacd_error_standards.h"
36 #include "ixheaacd_apicmd_standards.h"
37 #include "ixheaacd_aac_config.h"
38 #include "ixheaacd_api_defs.h"
39 
40 #include "ixheaacd_definitions.h"
41 #include "ixheaacd_error_codes.h"
42 #include "ixheaacd_bitbuffer.h"
43 
44 #include "ixheaacd_audioobjtypes.h"
45 #include "ixheaacd_sbrdecsettings.h"
46 #include "ixheaacd_memory_standards.h"
47 
48 #include "ixheaacd_bitbuffer.h"
49 #include "ixheaacd_adts.h"
50 #include "ixheaacd_defines.h"
51 #include "ixheaacd_cnst.h"
52 #include "ixheaacd_aac_rom.h"
53 #include "ixheaacd_ec_defines.h"
54 #include "ixheaacd_ec_struct_def.h"
55 
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 
61 #include "ixheaacd_hybrid.h"
62 #include "ixheaacd_ps_dec.h"
63 #include "ixheaacd_ps_bitdec.h"
64 
65 #include "ixheaacd_pulsedata.h"
66 
67 #include "ixheaacd_pns.h"
68 #include "ixheaacd_drc_data_struct.h"
69 
70 #include "ixheaacd_lt_predict.h"
71 
72 #include "ixheaacd_channelinfo.h"
73 #include "ixheaacd_drc_dec.h"
74 #include "ixheaacd_sbr_const.h"
75 #include "ixheaacd_sbrdecoder.h"
76 #include "ixheaacd_env_extr.h"
77 #include "ixheaacd_common_rom.h"
78 #include "ixheaacd_freq_sca.h"
79 #include "ixheaacd_qmf_dec.h"
80 #include "ixheaacd_env_calc.h"
81 
82 #include "ixheaacd_pvc_dec.h"
83 #include "ixheaacd_sbr_dec.h"
84 #include "ixheaacd_block.h"
85 #include "ixheaacd_channel.h"
86 
87 #include "ixheaacd_audioobjtypes.h"
88 #include "ixheaacd_latmdemux.h"
89 
90 #include "ixheaacd_aacdec.h"
91 #include "ixheaacd_mps_polyphase.h"
92 #include "ixheaacd_config.h"
93 #include "ixheaacd_mps_dec.h"
94 #include "ixheaacd_struct_def.h"
95 #include "ixheaacd_headerdecode.h"
96 #include "ixheaacd_adts_crc_check.h"
97 
98 #include "ixheaacd_multichannel.h"
99 #include "ixheaacd_ver_number.h"
100 
101 #include "ixheaacd_interface.h"
102 #include "ixheaacd_info.h"
103 
104 #include "ixheaacd_config.h"
105 
106 #include "ixheaacd_struct.h"
107 #include "ixheaacd_mps_polyphase.h"
108 #include "ixheaacd_tns_usac.h"
109 #include "ixheaacd_acelp_info.h"
110 
111 #include "ixheaacd_main.h"
112 #include "ixheaacd_arith_dec.h"
113 #include "ixheaacd_create.h"
114 #include "ixheaacd_function_selector.h"
115 #include "ixheaacd_ld_mps_dec.h"
116 #include "ixheaacd_mps_tables.h"
117 #define MAX_TRACKS_PER_LAYER 50
118 
119 #define ALIGN_SIZE64(x) ((((x) + 7) >> 3) << 3)
120 
121 #define IA_ENHAACPDEC_NUM_MEMTABS (4)
122 
123 #define NUM_AAC_TABLES 8
124 
125 #define IXHEAACD_CCE_DEC_INFO_MEM_SIZE (610)
126 #define IXHEAACD_CCE_DEC_INFO_MEM_SIZE_8 (IXHEAACD_CCE_DEC_INFO_MEM_SIZE + 8)
127 
128 #define LD_OBJ -2
129 
130 #define MIN(x, y) ((x) > (y) ? (y) : (x))
131 
ixheaacd_dec_mem_api(ia_exhaacplus_dec_api_struct * p_obj_exhaacplus_dec,WORD32 i_cmd,WORD32 i_idx,VOID * pv_value)132 IA_ERRORCODE ixheaacd_dec_mem_api(ia_exhaacplus_dec_api_struct *p_obj_exhaacplus_dec,
133                                   WORD32 i_cmd, WORD32 i_idx, VOID *pv_value) {
134   pUWORD32 pui_value = pv_value;
135 
136   if (i_idx < 0 || i_idx >= IA_ENHAACPDEC_NUM_MEMTABS) {
137     return IA_XHEAAC_DEC_API_FATAL_INVALID_MEMTAB_INDEX;
138   }
139 
140   if (i_cmd == IA_API_CMD_SET_MEM_PTR) {
141     if (pv_value == 0) {
142       return (IA_XHEAAC_DEC_API_FATAL_MEM_ALLOC);
143     }
144     if (((SIZE_T)pv_value %
145          p_obj_exhaacplus_dec->p_mem_info_aac[i_idx].ui_alignment) != 0) {
146       return (IA_XHEAAC_DEC_API_FATAL_MEM_ALIGN);
147     }
148     p_obj_exhaacplus_dec->pp_mem_aac[i_idx] = pv_value;
149     memset(p_obj_exhaacplus_dec->pp_mem_aac[i_idx], 0,
150            p_obj_exhaacplus_dec->p_mem_info_aac[i_idx].ui_size);
151 
152     if (i_idx == IA_ENHAACPLUS_DEC_PERSIST_IDX) {
153       pUWORD8 p_temp = pv_value;
154       UWORD32 *meminfo =
155           (UWORD32 *)p_obj_exhaacplus_dec->p_mem_info_aac + i_idx;
156       UWORD32 pers_size = meminfo[0];
157       p_temp = p_temp + pers_size -
158                (sizeof(ia_dec_data_struct) +
159                 sizeof(ia_audio_specific_config_struct) + (8300));
160       p_obj_exhaacplus_dec->p_state_aac = pv_value;
161       memset(p_obj_exhaacplus_dec->p_state_aac, 0,
162              sizeof(ia_aac_dec_state_struct));
163       p_obj_exhaacplus_dec->p_state_aac->pstr_dec_data = p_temp;
164       p_obj_exhaacplus_dec->p_state_aac->ia_audio_specific_config =
165           p_temp + sizeof(ia_dec_data_struct);
166       p_obj_exhaacplus_dec->p_state_aac->header_ptr =
167           p_temp + sizeof(ia_dec_data_struct) +
168           sizeof(ia_audio_specific_config_struct);
169     }
170 
171   } else {
172     UWORD32 *meminfo =
173         (UWORD32 *)(p_obj_exhaacplus_dec->p_mem_info_aac + i_idx);
174     *pui_value = *(meminfo + (i_cmd - IA_API_CMD_GET_MEM_INFO_SIZE));
175   }
176 
177   return IA_NO_ERROR;
178 }
179 
180 static PLATFORM_INLINE VOID
ixheaacd_init_sbr_tables(ia_sbr_tables_struct * ptr_sbr_tables)181 ixheaacd_init_sbr_tables(ia_sbr_tables_struct *ptr_sbr_tables) {
182   ptr_sbr_tables->env_calc_tables_ptr =
183       (ia_env_calc_tables_struct *)&ixheaacd_aac_dec_env_calc_tables;
184   ptr_sbr_tables->qmf_dec_tables_ptr =
185       (ia_qmf_dec_tables_struct *)&ixheaacd_aac_qmf_dec_tables;
186   ptr_sbr_tables->env_extr_tables_ptr =
187       (ia_env_extr_tables_struct *)&ixheaacd_aac_dec_env_extr_tables;
188   ptr_sbr_tables->ps_tables_ptr =
189       (ia_ps_tables_struct *)&ixheaacd_aac_dec_ps_tables;
190 }
191 
ixheaacd_updatebytesconsumed(ia_aac_dec_state_struct * p_state_enhaacplus_dec,struct ia_bit_buf_struct * it_bit_buff)192 VOID ixheaacd_updatebytesconsumed(
193     ia_aac_dec_state_struct *p_state_enhaacplus_dec,
194     struct ia_bit_buf_struct *it_bit_buff) {
195   p_state_enhaacplus_dec->i_bytes_consumed =
196       (WORD32)(it_bit_buff->ptr_read_next - it_bit_buff->ptr_bit_buf_base);
197   if ((p_state_enhaacplus_dec->i_bytes_consumed == 0) &&
198       (it_bit_buff->cnt_bits == 0)) {
199     p_state_enhaacplus_dec->i_bytes_consumed =
200         p_state_enhaacplus_dec->ui_in_bytes;
201   }
202   if (it_bit_buff->cnt_bits < 0) {
203     p_state_enhaacplus_dec->i_bytes_consumed = 0;
204     p_state_enhaacplus_dec->ui_out_bytes = 0;
205     p_state_enhaacplus_dec->ui_mps_out_bytes = 0;
206     p_state_enhaacplus_dec->b_n_raw_data_blk = 0;
207   }
208 }
209 
copy_qmf_to_ldmps(ia_mps_dec_state_struct * mps_dec_handle,VOID * sbr_persistent_mem_v)210 static VOID copy_qmf_to_ldmps(ia_mps_dec_state_struct *mps_dec_handle,
211                               VOID *sbr_persistent_mem_v) {
212   ia_sbr_pers_struct *sbr_persistent_mem =
213       (ia_sbr_pers_struct *)sbr_persistent_mem_v;
214   memcpy(&mps_dec_handle->str_mps_qmf_bank,
215          &sbr_persistent_mem->str_sbr_dec_inst.pstr_sbr_channel[0]
216               ->str_sbr_dec.str_codec_qmf_bank,
217          sizeof(ia_sbr_qmf_filter_bank_struct));
218 
219   mps_dec_handle->sbr_tables_ptr =
220       sbr_persistent_mem->str_sbr_dec_inst.pstr_sbr_tables;
221   mps_dec_handle->qmf_dec_tables_ptr =
222       sbr_persistent_mem->str_sbr_dec_inst.pstr_sbr_tables->qmf_dec_tables_ptr;
223   mps_dec_handle->str_sbr_scale_fact =
224       &sbr_persistent_mem->str_sbr_dec_inst.pstr_sbr_channel[0]
225            ->str_sbr_dec.str_sbr_scale_fact;
226 }
227 
ixheaacd_readifadts(ia_aac_dec_state_struct * p_state_enhaacplus_dec,struct ia_bit_buf_struct * it_bit_buff,ia_adts_header_struct * adts)228 WORD32 ixheaacd_readifadts(ia_aac_dec_state_struct *p_state_enhaacplus_dec,
229                            struct ia_bit_buf_struct *it_bit_buff,
230                            ia_adts_header_struct *adts) {
231   WORD error;
232 
233   if ((error = ixheaacd_find_syncword(adts, it_bit_buff)) != 0) {
234     ixheaacd_updatebytesconsumed(p_state_enhaacplus_dec, it_bit_buff);
235     return IA_XHEAAC_DEC_EXE_NONFATAL_ADTS_SYNC_LOST;
236   }
237   if ((error = ixheaacd_check_if_adts(
238            adts, it_bit_buff,
239            p_state_enhaacplus_dec->p_config->ui_max_channels)) != 0) {
240     p_state_enhaacplus_dec->i_bytes_consumed = 1;
241 
242     if (it_bit_buff->cnt_bits < 0) {
243       p_state_enhaacplus_dec->i_bytes_consumed = 0;
244       p_state_enhaacplus_dec->ui_out_bytes = 0;
245       p_state_enhaacplus_dec->ui_mps_out_bytes = 0;
246       error = IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES;
247       return error;
248     }
249     return IA_XHEAAC_DEC_EXE_NONFATAL_ADTS_SYNC_LOST;
250   }
251   p_state_enhaacplus_dec->b_n_raw_data_blk =
252       (WORD8)(adts->no_raw_data_blocks + 1);
253   return 0;
254 }
255 
ixheaacd_allocate_aac_scr(ia_aac_dec_scratch_struct * aac_scratch_struct,VOID * base_scratch_ptr,VOID * output_ptr,WORD channel,WORD max_channel,WORD32 audio_object_type)256 static VOID ixheaacd_allocate_aac_scr(
257     ia_aac_dec_scratch_struct *aac_scratch_struct, VOID *base_scratch_ptr,
258     VOID *output_ptr, WORD channel, WORD max_channel,
259     WORD32 audio_object_type) {
260   aac_scratch_struct->base_scr_8k = base_scratch_ptr;
261   aac_scratch_struct->extra_scr_4k[1] = (WORD8 *)base_scratch_ptr;
262   if (channel == 1) {
263     aac_scratch_struct->extra_scr_4k[0] =
264         (WORD8 *)base_scratch_ptr + (IXHEAACD_CCE_DEC_INFO_MEM_SIZE_8 * 1024) +
265         (4 * 1024);
266   } else {
267     aac_scratch_struct->extra_scr_4k[0] = output_ptr;
268 
269     if (max_channel > 2) {
270       aac_scratch_struct->extra_scr_4k[0] =
271           (WORD8 *)base_scratch_ptr +
272           (IXHEAACD_CCE_DEC_INFO_MEM_SIZE_8 * 1024) + (8 * 1024);
273     }
274   }
275 
276   aac_scratch_struct->extra_scr_4k[2] =
277       (WORD8 *)base_scratch_ptr + (IXHEAACD_CCE_DEC_INFO_MEM_SIZE_8 * 1024) +
278       (46 * 1024);
279 
280   if (audio_object_type == AOT_ER_AAC_ELD ||
281       audio_object_type == AOT_ER_AAC_LD) {
282     aac_scratch_struct->extra_scr_4k[0] =
283         (WORD8 *)base_scratch_ptr + (IXHEAACD_CCE_DEC_INFO_MEM_SIZE_8 * 1024) +
284         (4 * 1024);
285 
286     aac_scratch_struct->extra_scr_4k[2] =
287         (WORD8 *)base_scratch_ptr + (IXHEAACD_CCE_DEC_INFO_MEM_SIZE_8 * 1024) +
288         (46 * 1024);
289 
290     aac_scratch_struct->extra_scr_4k[3] =
291         (WORD8 *)base_scratch_ptr + (IXHEAACD_CCE_DEC_INFO_MEM_SIZE_8 * 1024) +
292         (54 * 1024);
293   }
294   if ((audio_object_type == AOT_ER_AAC_LD) ||
295       (audio_object_type == AOT_AAC_LTP)) {
296     aac_scratch_struct->in_data =
297         (WORD32 *)((WORD8 *)base_scratch_ptr +
298                    (IXHEAACD_CCE_DEC_INFO_MEM_SIZE_8 * 1024) + (62 * 1024) +
299                    (4 * 16));
300     aac_scratch_struct->out_data =
301         (WORD32 *)((WORD8 *)base_scratch_ptr +
302                    (IXHEAACD_CCE_DEC_INFO_MEM_SIZE_8 * 1024) + (56 * 1024) +
303                    (4 * 16));
304   }
305 }
306 
ixheaacd_allocate_sbr_scr(ia_sbr_scr_struct * sbr_scratch_struct,VOID * base_scratch_ptr,VOID * output_ptr,WORD total_elements,WORD ch_fac,WORD32 audio_object_type,WORD32 total_channels,WORD8 * p_qshift_arr,UWORD8 slot_pos,UWORD8 num_ch)307 VOID ixheaacd_allocate_sbr_scr(ia_sbr_scr_struct *sbr_scratch_struct,
308                                VOID *base_scratch_ptr, VOID *output_ptr,
309                                WORD total_elements, WORD ch_fac,
310                                WORD32 audio_object_type, WORD32 total_channels,
311                                WORD8 *p_qshift_arr, UWORD8 slot_pos,
312                                UWORD8 num_ch) {
313   WORD32 temp = 0;
314   WORD32 j, i;
315   sbr_scratch_struct->ptr_work_buf_core = base_scratch_ptr;
316   sbr_scratch_struct->ptr_work_buf = (WORD8 *)base_scratch_ptr + (18 * 1024);
317 
318   if (p_qshift_arr != NULL && *p_qshift_arr != LD_OBJ) {
319     WORD32 *tmp_buf = (WORD32 *)output_ptr;
320 
321     for (j = 1; j < num_ch; j++) {
322       if ((*p_qshift_arr + j) == 0)
323         *(p_qshift_arr + j) = *(p_qshift_arr + j - 1);
324     }
325 
326     if (total_channels > 2) {
327       for (j = 0; j < num_ch; j++) {
328         for (i = 0; i < 1024; i++) {
329           *((WORD16 *)tmp_buf + slot_pos + total_channels * i + j) =
330               ixheaacd_round16(ixheaacd_shl32_sat(
331                   *(tmp_buf + slot_pos + total_channels * i + j),
332                   *(p_qshift_arr + j)));
333         }
334       }
335     } else {
336       for (j = 0; j < num_ch; j++) {
337         for (i = 0; i < 1024; i++) {
338           *((WORD16 *)tmp_buf + total_channels * i + j) =
339               ixheaacd_round16(ixheaacd_shl32_sat(
340                   *(tmp_buf + total_channels * i + j), *(p_qshift_arr + j)));
341         }
342       }
343     }
344   }
345   if (total_elements > 1) {
346     sbr_scratch_struct->extra_scr_1k[0] =
347         (WORD8 *)base_scratch_ptr + (18 * 1024);
348 
349     sbr_scratch_struct->extra_scr_1k[1] =
350         (WORD8 *)base_scratch_ptr + (19 * 1024);
351   }
352 
353   else {
354     if (ch_fac == 1) {
355       temp = 2;
356     } else {
357       temp = 4;
358     }
359 
360     if (audio_object_type != AOT_ER_AAC_ELD) {
361       sbr_scratch_struct->extra_scr_1k[0] = (WORD8 *)output_ptr + (temp * 1024);
362 
363       sbr_scratch_struct->extra_scr_1k[1] =
364           (WORD8 *)base_scratch_ptr + (18 * 1024);
365     } else {
366       sbr_scratch_struct->extra_scr_1k[0] =
367           (WORD8 *)base_scratch_ptr + (18 * 1024);
368 
369       sbr_scratch_struct->extra_scr_1k[1] =
370           (WORD8 *)base_scratch_ptr + (19 * 1024);
371     }
372   }
373 }
374 
ixheaacd_get_lib_id_strings(pVOID pv_output)375 VOID ixheaacd_get_lib_id_strings(pVOID pv_output) {
376   ia_lib_info_struct *pstr_lib_info = (ia_lib_info_struct *)pv_output;
377 
378   pstr_lib_info->p_lib_name = (WORD8 *)LIBNAME;
379   pstr_lib_info->p_version_num = (WORD8 *)xHE_AAC_DEC_ITTIAM_VER;
380 
381   return;
382 }
383 
ixheaacd_dec_api(pVOID p_ia_xheaac_dec_obj,WORD32 i_cmd,WORD32 i_idx,pVOID pv_value)384 IA_ERRORCODE ixheaacd_dec_api(pVOID p_ia_xheaac_dec_obj, WORD32 i_cmd,
385                               WORD32 i_idx, pVOID pv_value) {
386   ia_exhaacplus_dec_api_struct *p_obj_exhaacplus_dec = p_ia_xheaac_dec_obj;
387   pUWORD32 pui_value = pv_value;
388   pWORD32 pui_value_signed = pv_value;
389   pWORD8 pb_value = pv_value;
390   pVOID *pp_value = (pVOID *)pv_value;
391   float *pf_value = pv_value;
392 
393   if ((i_cmd != IA_API_CMD_GET_API_SIZE) &&
394       (i_cmd != IA_API_CMD_GET_LIB_ID_STRINGS)) {
395     if (p_ia_xheaac_dec_obj == 0) {
396       return (IA_XHEAAC_DEC_API_FATAL_MEM_ALLOC);
397     }
398     if (((SIZE_T)p_ia_xheaac_dec_obj & 3) != 0) {
399       return (IA_XHEAAC_DEC_API_FATAL_MEM_ALIGN);
400     }
401   }
402 
403   switch (i_cmd) {
404     case IA_API_CMD_GET_MEM_INFO_SIZE:
405     case IA_API_CMD_GET_MEM_INFO_ALIGNMENT:
406     case IA_API_CMD_GET_MEM_INFO_TYPE:
407     case IA_API_CMD_SET_MEM_PTR: {
408       return ixheaacd_dec_mem_api(p_ia_xheaac_dec_obj, i_cmd, i_idx,
409                                   pv_value);
410     }
411 
412     case IA_API_CMD_GET_TABLE_INFO_SIZE:
413     case IA_API_CMD_GET_TABLE_INFO_ALIGNMENT:
414     case IA_API_CMD_SET_TABLE_PTR:
415     case IA_API_CMD_GET_TABLE_PTR: {
416       return ixheaacd_dec_table_api(p_ia_xheaac_dec_obj, i_cmd, i_idx,
417                                     pv_value);
418     }
419   };
420 
421   switch (i_cmd) {
422     case IA_API_CMD_GET_LIB_ID_STRINGS: {
423       WORD8 *i1_ver;
424       WORD8 ver_char;
425 
426       if (i_idx == IA_CMD_TYPE_LIB_NAME)
427         i1_ver = (WORD8 *)LIBNAME;
428       else if (i_idx == IA_CMD_TYPE_LIB_VERSION)
429         i1_ver = (WORD8 *)xHE_AAC_DEC_ITTIAM_VER;
430       else
431         return IA_XHEAAC_DEC_API_FATAL_INVALID_LIB_ID_STRINGS_IDX;
432 
433       ver_char = *i1_ver++;
434 
435       for (; ver_char != '\0';) {
436         if (ver_char != '$') {
437           *pb_value++ = ver_char;
438         }
439         ver_char = *i1_ver++;
440       }
441       *pb_value = ver_char;
442 
443       break;
444     }
445     case IA_API_CMD_GET_API_SIZE: {
446       *pui_value = sizeof(ia_exhaacplus_dec_api_struct);
447       break;
448     }
449     case IA_API_CMD_INIT: {
450       switch (i_idx) {
451         case IA_CMD_TYPE_INIT_API_PRE_CONFIG_PARAMS: {
452           memset(p_obj_exhaacplus_dec, 0, sizeof(*p_obj_exhaacplus_dec));
453           p_obj_exhaacplus_dec->aac_config.ui_pcm_wdsz = 16;
454           p_obj_exhaacplus_dec->aac_config.flag_downmix = 0;
455           p_obj_exhaacplus_dec->aac_config.flag_08khz_out = 0;
456           p_obj_exhaacplus_dec->aac_config.flag_16khz_out = 0;
457           p_obj_exhaacplus_dec->aac_config.flag_to_stereo = 0;
458           p_obj_exhaacplus_dec->aac_config.down_sample_flag = 0;
459           p_obj_exhaacplus_dec->aac_config.header_dec_done = 0;
460           p_obj_exhaacplus_dec->aac_config.frame_status = 1;
461           p_obj_exhaacplus_dec->aac_config.ui_mp4_flag = 0;
462           p_obj_exhaacplus_dec->aac_config.ui_disable_sync = 0;
463           p_obj_exhaacplus_dec->aac_config.ui_auto_sbr_upsample = 1;
464           p_obj_exhaacplus_dec->aac_config.ui_samp_freq = 0;
465           p_obj_exhaacplus_dec->aac_config.ui_frame_size = 0;
466 
467           p_obj_exhaacplus_dec->aac_config.ui_n_channels = 2;
468           p_obj_exhaacplus_dec->aac_config.i_channel_mask = 3;
469           p_obj_exhaacplus_dec->aac_config.ui_channel_mode = 3;
470           p_obj_exhaacplus_dec->aac_config.ui_sbr_mode = 0;
471           p_obj_exhaacplus_dec->aac_config.ui_effect_type = 0;
472           p_obj_exhaacplus_dec->aac_config.ui_target_loudness = -24;
473           p_obj_exhaacplus_dec->aac_config.ui_loud_norm_flag = 0;
474           p_obj_exhaacplus_dec->aac_config.ui_hq_esbr = 0;
475           p_obj_exhaacplus_dec->aac_config.ui_enh_sbr = 1;
476           p_obj_exhaacplus_dec->aac_config.ui_enh_sbr_ps = 0;
477           p_obj_exhaacplus_dec->aac_config.ui_pce_found_in_hdr = 0;
478           p_obj_exhaacplus_dec->aac_config.loas_present = 0;
479           p_obj_exhaacplus_dec->aac_config.ld_decoder = 0;
480           p_obj_exhaacplus_dec->aac_config.ui_drc_boost = 0;
481           p_obj_exhaacplus_dec->aac_config.ui_drc_cut = 0;
482           p_obj_exhaacplus_dec->aac_config.ui_drc_target_level = 108;
483           p_obj_exhaacplus_dec->aac_config.ui_drc_set = 0;
484           p_obj_exhaacplus_dec->aac_config.ui_flush_cmd = 0;
485           p_obj_exhaacplus_dec->aac_config.output_level = -1;
486 
487           p_obj_exhaacplus_dec->aac_config.ui_max_channels = 6;
488 
489           p_obj_exhaacplus_dec->aac_config.ui_coupling_channel = 0;
490           p_obj_exhaacplus_dec->aac_config.downmix = 0;
491 
492           p_obj_exhaacplus_dec->aac_config.ui_err_conceal = 0;
493 
494           {
495             ia_aac_dec_tables_struct *pstr_aac_tables =
496                 &p_obj_exhaacplus_dec->aac_tables;
497             pstr_aac_tables->pstr_huffmann_tables =
498                 (ia_aac_dec_huffman_tables_struct
499                      *)&ixheaacd_aac_huffmann_tables;
500             pstr_aac_tables->pstr_block_tables =
501                 (ia_aac_dec_block_tables_struct *)&ixheaacd_aac_block_tables;
502             pstr_aac_tables->pstr_imdct_tables =
503                 (ia_aac_dec_imdct_tables_struct *)&ixheaacd_imdct_tables;
504 
505             ixheaacd_huff_tables_create(pstr_aac_tables);
506           }
507           ixheaacd_init_sbr_tables(&p_obj_exhaacplus_dec->str_sbr_tables);
508           p_obj_exhaacplus_dec->common_tables =
509               (ixheaacd_misc_tables *)&ixheaacd_str_fft_n_transcendent_tables;
510           p_obj_exhaacplus_dec->aac_config.ui_qmf_bands = 64;
511 
512           break;
513         }
514         case IA_CMD_TYPE_INIT_API_POST_CONFIG_PARAMS: {
515           ixheaacd_fill_aac_mem_tables(p_obj_exhaacplus_dec);
516           break;
517         }
518         case IA_CMD_TYPE_INIT_PROCESS: {
519           WORD32 err_code = 0;
520           if (p_obj_exhaacplus_dec->p_state_aac->fatal_err_present) {
521             err_code = IA_FATAL_ERROR;
522           } else {
523             err_code = ixheaacd_dec_init(p_obj_exhaacplus_dec);
524             if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && err_code) {
525               if (err_code & IA_FATAL_ERROR) {
526                 err_code = IA_XHEAAC_DEC_INIT_FATAL_EC_INIT_FAIL;
527               } else {
528                 err_code = IA_XHEAAC_DEC_INIT_NONFATAL_EC_INIT_FAIL;
529               }
530             }
531           }
532           if (err_code != 0) {
533             if (err_code < 0)
534               p_obj_exhaacplus_dec->p_state_aac->fatal_err_present = 1;
535             p_obj_exhaacplus_dec->p_state_aac->i_bytes_consumed =
536                 p_obj_exhaacplus_dec->p_state_aac->ui_in_bytes;
537           }
538           return err_code;
539           break;
540         }
541         case IA_CMD_TYPE_INIT_DONE_QUERY: {
542           if (p_obj_exhaacplus_dec->p_state_aac->ui_init_done == 1) {
543             *pui_value = 1;
544           } else {
545             *pui_value = 0;
546           }
547           break;
548         }
549 
550         case IA_CMD_TYPE_GA_HDR: {
551           return ixheaacd_decoder_2_ga_hdr(p_obj_exhaacplus_dec);
552           break;
553         }
554 
555         case IA_CMD_TYPE_FLUSH_MEM: {
556           return ixheaacd_decoder_flush_api(p_obj_exhaacplus_dec);
557           break;
558         }
559 
560         default: {
561           return IA_XHEAAC_DEC_API_NONFATAL_CMD_TYPE_NOT_SUPPORTED;
562         }
563       };
564       break;
565     }
566     case IA_API_CMD_SET_CONFIG_PARAM: {
567       switch (i_idx) {
568         case IA_XHEAAC_DEC_CONFIG_PARAM_SAMP_FREQ: {
569           if ((*pui_value < 8000) || (*pui_value > 96000)) {
570             return (IA_XHEAAC_DEC_CONFIG_FATAL_INVALID_SAMPLE_RATE);
571           }
572           p_obj_exhaacplus_dec->aac_config.ui_samp_freq = *pui_value;
573           break;
574         }
575         case IA_XHEAAC_DEC_CONFIG_PARAM_FRAMELENGTH_FLAG: {
576           if ((*pui_value != 1) && (*pui_value != 0)) {
577             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_FRAMELENGTHFLAG);
578           }
579           p_obj_exhaacplus_dec->aac_config.ui_frame_size = *pui_value;
580           break;
581         }
582         case IA_XHEAAC_DEC_CONFIG_PARAM_PCM_WDSZ: {
583           if ((*pui_value != 16) && (*pui_value != 24)) {
584             p_obj_exhaacplus_dec->aac_config.ui_pcm_wdsz = 16;
585             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_PCM_WDSZ);
586           }
587           p_obj_exhaacplus_dec->aac_config.ui_pcm_wdsz = *pui_value;
588           break;
589         }
590         case IA_XHEAAC_DEC_CONFIG_PARAM_DOWNMIX: {
591           if ((*pui_value != 1) && (*pui_value != 0)) {
592             p_obj_exhaacplus_dec->aac_config.flag_downmix = 0;
593             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_DOWNMIX);
594           }
595           p_obj_exhaacplus_dec->aac_config.flag_downmix = *pui_value;
596           p_obj_exhaacplus_dec->aac_config.downmix = *pui_value;
597           break;
598         }
599 
600         case IA_XHEAAC_DEC_CONFIG_PARAM_TOSTEREO: {
601           if ((*pui_value != 1) && (*pui_value != 0)) {
602             p_obj_exhaacplus_dec->aac_config.flag_to_stereo = 1;
603             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_TOSTEREO);
604           }
605           p_obj_exhaacplus_dec->aac_config.flag_to_stereo = *pui_value;
606           break;
607         }
608         case IA_XHEAAC_DEC_CONFIG_PARAM_DSAMPLE: {
609           if ((*pui_value != 1) && (*pui_value != 0)) {
610             p_obj_exhaacplus_dec->aac_config.down_sample_flag = 0;
611             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_DSAMPLE);
612           }
613           p_obj_exhaacplus_dec->aac_config.down_sample_flag = *pui_value;
614           break;
615         }
616 
617         case IA_XHEAAC_DEC_CONFIG_PARAM_MP4FLAG: {
618           if ((*pui_value != 1) && (*pui_value != 0)) {
619             p_obj_exhaacplus_dec->aac_config.ui_mp4_flag = 0;
620             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_MP4FLAG);
621           }
622           p_obj_exhaacplus_dec->aac_config.ui_mp4_flag = *pui_value;
623           break;
624         }
625         case IA_XHEAAC_DEC_CONFIG_PARAM_DRC_CUT: {
626           p_obj_exhaacplus_dec->aac_config.ui_drc_set = 1;
627           if (*pf_value > 1) {
628             p_obj_exhaacplus_dec->aac_config.ui_drc_cut = 0;
629             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_DRC_CUT);
630           }
631           p_obj_exhaacplus_dec->aac_config.ui_drc_cut =
632               (WORD32)((*pf_value) * 100);
633           break;
634         }
635 
636         case IA_XHEAAC_DEC_CONFIG_PARAM_DRC_BOOST: {
637           p_obj_exhaacplus_dec->aac_config.ui_drc_set = 1;
638           if (*pf_value > 1) {
639             p_obj_exhaacplus_dec->aac_config.ui_drc_boost = 0;
640             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_DRC_BOOST);
641           }
642           p_obj_exhaacplus_dec->aac_config.ui_drc_boost =
643               (WORD32)((*pf_value) * 100);
644           break;
645         }
646 
647         case IA_XHEAAC_DEC_CONFIG_PARAM_DRC_TARGET_LEVEL: {
648           p_obj_exhaacplus_dec->aac_config.ui_drc_set = 1;
649           p_obj_exhaacplus_dec->aac_config.i_loud_ref_level = *pui_value_signed;
650           if (*pui_value > 127) {
651             p_obj_exhaacplus_dec->aac_config.ui_drc_target_level = 108;
652             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_DRC_TARGET);
653           }
654           p_obj_exhaacplus_dec->aac_config.ui_drc_target_level = *pui_value;
655           break;
656         }
657         case IA_XHEAAC_DEC_CONFIG_PARAM_DRC_HEAVY_COMP: {
658           p_obj_exhaacplus_dec->aac_config.ui_drc_set = 1;
659           if ((*pui_value != 1) && (*pui_value != 0)) {
660             p_obj_exhaacplus_dec->aac_config.ui_drc_heavy_comp = 0;
661             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_DRCFLAG);
662           }
663           p_obj_exhaacplus_dec->aac_config.ui_drc_heavy_comp = *pui_value;
664           break;
665         }
666         case IA_XHEAAC_DEC_CONFIG_DISABLE_SYNC: {
667           if ((*pui_value != 1) && (*pui_value != 0)) {
668             p_obj_exhaacplus_dec->aac_config.ui_disable_sync = 0;
669             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_SYNCFLAG);
670           }
671           p_obj_exhaacplus_dec->aac_config.ui_disable_sync = *pui_value;
672           break;
673         }
674         case IA_XHEAAC_DEC_CONFIG_PARAM_AUTO_SBR_UPSAMPLE: {
675           if ((*pui_value != 1) && (*pui_value != 0)) {
676             p_obj_exhaacplus_dec->aac_config.ui_auto_sbr_upsample = 0;
677             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_SBRUPFLAG);
678           }
679           p_obj_exhaacplus_dec->aac_config.ui_auto_sbr_upsample = *pui_value;
680           break;
681         }
682         case IA_XHEAAC_DEC_CONFIG_PARAM_MAX_CHANNEL: {
683           if (*pui_value > 8) {
684             p_obj_exhaacplus_dec->aac_config.ui_max_channels = 8;
685             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_MAX_CHANNEL);
686           }
687           if (*pui_value < 2) {
688             p_obj_exhaacplus_dec->aac_config.ui_max_channels = 2;
689             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_MAX_CHANNEL);
690           }
691           p_obj_exhaacplus_dec->aac_config.ui_max_channels = *pui_value;
692           break;
693         }
694         case IA_XHEAAC_DEC_CONFIG_PARAM_FRAMESIZE: {
695           if (*pui_value == 1) {
696             p_obj_exhaacplus_dec->aac_config.framesize_480 = 1;
697           } else if (*pui_value == 0) {
698             p_obj_exhaacplus_dec->aac_config.framesize_480 = 0;
699           } else {
700             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_FRAMSZ);
701           }
702           break;
703         }
704 
705         case IA_XHEAAC_DEC_CONFIG_PARAM_LD_TESTING: {
706           if (*pui_value == 1) {
707             p_obj_exhaacplus_dec->aac_config.ld_decoder = 1;
708           } else if (*pui_value == 0) {
709             p_obj_exhaacplus_dec->aac_config.ld_decoder = 0;
710           } else {
711             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_LD_CONFIG);
712           }
713           break;
714         }
715 
716         case IA_XHEAAC_DEC_CONFIG_PARAM_COUP_CHANNEL: {
717           if (*pui_value > 16) {
718             p_obj_exhaacplus_dec->aac_config.ui_coupling_channel = 1;
719             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_COUP_CHANNEL);
720           }
721           p_obj_exhaacplus_dec->aac_config.ui_coupling_channel = *pui_value;
722           break;
723         }
724 
725         case IA_XHEAAC_DEC_CONFIG_PARAM_DOWNMIX_STEREO: {
726           if ((*pui_value != 1) && (*pui_value != 0)) {
727             p_obj_exhaacplus_dec->aac_config.downmix = 0;
728             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_DOWNMIX_STEREO);
729           }
730           p_obj_exhaacplus_dec->aac_config.downmix = *pui_value;
731           break;
732         }
733 
734         case IA_XHEAAC_DEC_CONFIG_PARAM_DRC_EFFECT_TYPE: {
735           if (((*pui_value_signed) > 8) || ((*pui_value_signed) < -1)) {
736             p_obj_exhaacplus_dec->aac_config.ui_effect_type = -1;
737             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_EFFECT_TYPE);
738           }
739           p_obj_exhaacplus_dec->aac_config.ui_effect_type = *pui_value_signed;
740           break;
741         }
742         case IA_XHEAAC_DEC_CONFIG_PARAM_DRC_TARGET_LOUDNESS: {
743           if (*pui_value_signed >= 0) {
744             p_obj_exhaacplus_dec->aac_config.ui_loud_norm_flag = 1;
745           }
746           *pui_value_signed = -(*pui_value_signed >> 2);
747           if (((*pui_value_signed) > 0) || ((*pui_value_signed) < -63)) {
748             p_obj_exhaacplus_dec->aac_config.ui_target_loudness = 0;
749             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_TARGET_LOUDNESS);
750           }
751           p_obj_exhaacplus_dec->aac_config.ui_target_loudness =
752               *pui_value_signed;
753           break;
754         }
755         case IA_XHEAAC_DEC_CONFIG_PARAM_HQ_ESBR: {
756           if (((*pui_value_signed) != 0) && ((*pui_value_signed) != 1)) {
757             p_obj_exhaacplus_dec->aac_config.ui_hq_esbr = 0;
758             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_ESBR_HQ_FLAG);
759           }
760           p_obj_exhaacplus_dec->aac_config.ui_hq_esbr = *pui_value_signed;
761           break;
762         }
763         case IA_XHEAAC_DEC_CONFIG_PARAM_PS_ENABLE: {
764           if (((*pui_value_signed) != 0) && ((*pui_value_signed) != 1)) {
765             p_obj_exhaacplus_dec->aac_config.ui_enh_sbr_ps = 0;
766             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_ESBR_PS_FLAG);
767           }
768           p_obj_exhaacplus_dec->aac_config.ui_enh_sbr_ps = *pui_value_signed;
769           break;
770         }
771         case IA_XHEAAC_DEC_CONFIG_PARAM_PEAK_LIMITER: {
772           if (((*pui_value_signed) != 0) && ((*pui_value_signed) != 1)) {
773             p_obj_exhaacplus_dec->aac_config.peak_limiter_off = 0;
774             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_PEAK_LIM_FLAG_TYPE);
775           }
776           p_obj_exhaacplus_dec->aac_config.peak_limiter_off = *pui_value_signed;
777           break;
778         }
779         case IA_XHEAAC_DEC_CONFIG_ERROR_CONCEALMENT: {
780           if (((*pui_value_signed) != 0) && ((*pui_value_signed) != 1)) {
781             p_obj_exhaacplus_dec->aac_config.ui_err_conceal = 0;
782             return (IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_ERR_CONCEAL_FLAG_TYPE);
783           }
784           p_obj_exhaacplus_dec->aac_config.ui_err_conceal = *pui_value_signed;
785           break;
786         }
787         default: { return IA_XHEAAC_DEC_API_FATAL_INVALID_CONFIG_PARAM; }
788       }
789       break;
790     }
791 
792     case IA_API_CMD_GET_CONFIG_PARAM: {
793       UWORD32 i;
794       WORD32 *pvalue =
795           (WORD32 *)(&p_obj_exhaacplus_dec->aac_config.ui_pcm_wdsz);
796       if (IA_XHEAAC_DEC_CONFIG_PARAM_NUM_CHANNELS == i_idx) {
797         if (p_obj_exhaacplus_dec->p_state_aac != NULL &&
798             p_obj_exhaacplus_dec->p_state_aac->heaac_mps_handle
799                     .heaac_mps_present == 1) {
800           *pui_value = p_obj_exhaacplus_dec->p_state_aac->heaac_mps_handle
801                            .num_output_channels_at;
802         } else {
803           *pui_value = pvalue[i_idx];
804         }
805       } else if (IA_XHEAAC_DEC_CONFIG_PARAM_CHANNEL_MASK == i_idx) {
806         if (p_obj_exhaacplus_dec->p_state_aac != NULL &&
807             p_obj_exhaacplus_dec->p_state_aac->heaac_mps_handle
808                     .heaac_mps_present == 1) {
809           *pui_value = p_obj_exhaacplus_dec->p_state_aac->heaac_mps_handle
810                            .bs_config.ui_channel_mask;
811         } else {
812           *pui_value = pvalue[i_idx];
813         }
814       } else
815       if (i_idx >= 0 && i_idx <= 8) {
816         *pui_value = pvalue[i_idx];
817       } else if (IA_ENHAACPLUS_DEC_CONFIG_GET_NUM_PRE_ROLL_FRAMES == i_idx) {
818         WORD32 *ptri_value = (WORD32 *)pv_value;
819         ia_audio_specific_config_struct *ptr_audio_specific_config =
820             ((ia_audio_specific_config_struct *)
821                  p_obj_exhaacplus_dec->p_state_aac->ia_audio_specific_config);
822 
823         for (i = 0; i < MAX_AUDIO_PREROLLS + 1; i++) {
824           if (ptr_audio_specific_config->str_usac_config.str_usac_dec_config
825                   .preroll_bytes[i] == 0) {
826             break;
827           }
828         }
829 
830         *ptri_value = i;
831       } else if (IA_ENHAACPLUS_DEC_CONFIG_EXT_ELE_PTR == i_idx) {
832         ia_audio_specific_config_struct *ptr_audio_specific_config =
833             ((ia_audio_specific_config_struct *)
834                 p_obj_exhaacplus_dec->p_state_aac->ia_audio_specific_config);
835 
836         for (i = 0; i < ptr_audio_specific_config->str_usac_config
837                             .str_usac_dec_config.num_config_extensions;
838              i++) {
839           pp_value[i] = ptr_audio_specific_config->str_usac_config
840                             .str_usac_dec_config.usac_cfg_ext_info_buf[i];
841         }
842 
843         for (i = 0; i < ptr_audio_specific_config->str_usac_config
844                             .str_usac_dec_config.num_elements;
845              i++) {
846           if (ptr_audio_specific_config->str_usac_config.str_usac_dec_config
847                   .usac_ext_ele_payload_present[i]) {
848             pp_value[i + 16] =
849                 ptr_audio_specific_config->str_usac_config.str_usac_dec_config
850                     .usac_ext_ele_payload_buf[i];
851           }
852         }
853       } else if (IA_ENHAACPLUS_DEC_CONFIG_EXT_ELE_BUF_SIZES == i_idx) {
854         WORD32 *ptri_value = (WORD32 *)pv_value;
855         ia_audio_specific_config_struct *ptr_audio_specific_config =
856             ((ia_audio_specific_config_struct *)
857                  p_obj_exhaacplus_dec->p_state_aac->ia_audio_specific_config);
858         for (i = 0; i < ptr_audio_specific_config->str_usac_config
859                             .str_usac_dec_config.num_config_extensions;
860              i++) {
861           ptri_value[i] = ptr_audio_specific_config->str_usac_config
862                               .str_usac_dec_config.usac_cfg_ext_info_len[i];
863         }
864         for (i = 0; i < ptr_audio_specific_config->str_usac_config
865                             .str_usac_dec_config.num_elements;
866              i++) {
867           ptri_value[i + 16] =
868               ptr_audio_specific_config->str_usac_config.str_usac_dec_config
869                   .usac_ext_ele_payload_len[i];
870         }
871 
872       } else if (IA_ENHAACPLUS_DEC_DRC_IS_CONFIG_CHANGED == i_idx) {
873         *pui_value = p_obj_exhaacplus_dec->p_state_aac->drc_config_changed;
874       } else if (IA_ENHAACPLUS_DEC_DRC_APPLY_CROSSFADE == i_idx) {
875         *pui_value = p_obj_exhaacplus_dec->p_state_aac->apply_crossfade;
876       } else if (IA_ENHAACPLUS_DEC_CONFIG_NUM_ELE == i_idx) {
877         UWORD32 *ptri_value = (UWORD32 *)pv_value;
878         ia_audio_specific_config_struct *ptr_audio_specific_config =
879             ((ia_audio_specific_config_struct *)
880                  p_obj_exhaacplus_dec->p_state_aac->ia_audio_specific_config);
881         *ptri_value = ptr_audio_specific_config->str_usac_config
882                           .str_usac_dec_config.num_elements;
883 
884       } else if (IA_ENHAACPLUS_DEC_CONFIG_NUM_CONFIG_EXT == i_idx) {
885         UWORD32 *ptri_value = (UWORD32 *)pv_value;
886         ia_audio_specific_config_struct *ptr_audio_specific_config =
887             ((ia_audio_specific_config_struct *)
888                  p_obj_exhaacplus_dec->p_state_aac->ia_audio_specific_config);
889         *ptri_value = ptr_audio_specific_config->str_usac_config
890                           .str_usac_dec_config.num_config_extensions;
891       } else if (IA_ENHAACPLUS_DEC_CONFIG_GAIN_PAYLOAD_LEN == i_idx) {
892         UWORD32 *ptri_value = (UWORD32 *)pv_value;
893         ia_audio_specific_config_struct *ptr_audio_specific_config =
894             ((ia_audio_specific_config_struct *)
895                  p_obj_exhaacplus_dec->p_state_aac->ia_audio_specific_config);
896         WORD32 preroll_counter = ptr_audio_specific_config->str_usac_config
897                                      .str_usac_dec_config.preroll_counter;
898         *ptri_value =
899             ptr_audio_specific_config->str_usac_config.str_usac_dec_config
900                 .usac_ext_gain_payload_len[preroll_counter];
901       } else if (IA_ENHAACPLUS_DEC_CONFIG_GAIN_PAYLOAD_BUF == i_idx) {
902         WORD32 payload_buffer_offeset = 0;
903         ia_audio_specific_config_struct *ptr_audio_specific_config =
904             ((ia_audio_specific_config_struct *)
905                  p_obj_exhaacplus_dec->p_state_aac->ia_audio_specific_config);
906         WORD32 preroll_counter = ptr_audio_specific_config->str_usac_config
907                                      .str_usac_dec_config.preroll_counter;
908         for (i = 0; i < (UWORD32)preroll_counter; i++)
909           payload_buffer_offeset +=
910               ptr_audio_specific_config->str_usac_config.str_usac_dec_config
911                   .usac_ext_gain_payload_len[i] *
912               sizeof(WORD8);
913         *pp_value = ptr_audio_specific_config->str_usac_config
914                         .str_usac_dec_config.usac_ext_gain_payload_buf +
915                     payload_buffer_offeset;
916       } else if (IA_XHEAAC_DEC_CONFIG_PARAM_AOT == i_idx) {
917         if (p_obj_exhaacplus_dec->p_state_aac != NULL) {
918           if (p_obj_exhaacplus_dec->aac_config.ui_mp4_flag == 1) {
919             ia_audio_specific_config_struct *ptr_audio_specific_config =
920                 ((ia_audio_specific_config_struct *)
921                     p_obj_exhaacplus_dec->p_state_aac->ia_audio_specific_config);
922             *pui_value = ptr_audio_specific_config->audio_object_type;
923           } else {
924             *pui_value = p_obj_exhaacplus_dec->p_state_aac->audio_object_type;
925           }
926         } else {
927           *pui_value = AOT_AAC_LC;
928         }
929       } else {
930         return IA_XHEAAC_DEC_API_FATAL_INVALID_CONFIG_PARAM;
931       }
932       break;
933     }
934 
935     case IA_API_CMD_GET_MEMTABS_SIZE: {
936       *pui_value = (sizeof(ia_mem_info_struct) + sizeof(pVOID *)) *
937                    (IA_ENHAACPDEC_NUM_MEMTABS);
938       break;
939     }
940     case IA_API_CMD_GET_LOUDNESS_VAL: {
941       *pui_value = p_obj_exhaacplus_dec->aac_config.output_level;
942       break;
943     }
944     case IA_API_CMD_SET_MEMTABS_PTR: {
945       if (pv_value == NULL) return IA_XHEAAC_DEC_API_FATAL_MEM_ALLOC;
946       memset(pv_value, 0, (sizeof(ia_mem_info_struct) + sizeof(pVOID *)) *
947                               (IA_ENHAACPDEC_NUM_MEMTABS));
948 
949       p_obj_exhaacplus_dec->p_mem_info_aac = pv_value;
950       p_obj_exhaacplus_dec->pp_mem_aac =
951           (pVOID *)((WORD8 *)pv_value +
952                     sizeof(ia_mem_info_struct) * IA_ENHAACPDEC_NUM_MEMTABS);
953 
954       break;
955     }
956     case IA_API_CMD_GET_N_MEMTABS: {
957       *pui_value = IA_ENHAACPDEC_NUM_MEMTABS;
958       break;
959     }
960 
961     case IA_API_CMD_GET_N_TABLES: {
962       *pui_value = NUM_AAC_TABLES + NUM_MPS_TABLES;
963       break;
964     }
965     case IA_API_CMD_EXECUTE: {
966       switch (i_idx) {
967         case IA_CMD_TYPE_DO_EXECUTE: {
968           WORD32 err_code = 0;
969           if (!p_obj_exhaacplus_dec->p_state_aac->ui_init_done ||
970               p_obj_exhaacplus_dec->p_state_aac->fatal_err_present) {
971             err_code = IA_FATAL_ERROR;
972           } else {
973             memset(p_obj_exhaacplus_dec->p_state_aac->qshift_adj, 0,
974                    sizeof(p_obj_exhaacplus_dec->p_state_aac->qshift_adj));
975             p_obj_exhaacplus_dec->p_state_aac->qshift_cnt = 0;
976             err_code = ixheaacd_dec_execute(p_obj_exhaacplus_dec);
977           }
978           if (err_code != IA_NO_ERROR) {
979             if (err_code < 0) {
980               p_obj_exhaacplus_dec->p_state_aac->fatal_err_present = 1;
981             }
982             p_obj_exhaacplus_dec->p_state_aac->i_bytes_consumed =
983                 p_obj_exhaacplus_dec->p_state_aac->ui_in_bytes;
984           }
985           return err_code;
986           break;
987         }
988         case IA_CMD_TYPE_DONE_QUERY: {
989           if (p_obj_exhaacplus_dec->p_state_aac->ui_input_over == 1) {
990             *pui_value = 1;
991           } else {
992             *pui_value = 0;
993           }
994 
995           break;
996         }
997         default: { return IA_XHEAAC_DEC_API_FATAL_INVALID_EXECUTE_TYPE; }
998       };
999       break;
1000     }
1001     case IA_API_CMD_GET_CURIDX_INPUT_BUF: {
1002       *pui_value = p_obj_exhaacplus_dec->p_state_aac->i_bytes_consumed;
1003       break;
1004     }
1005     case IA_API_CMD_SET_INPUT_BYTES: {
1006       p_obj_exhaacplus_dec->p_state_aac->ui_in_bytes = *pui_value;
1007       break;
1008     }
1009     case IA_API_CMD_GET_OUTPUT_BYTES: {
1010       if (1 == i_idx) {
1011         *pui_value = p_obj_exhaacplus_dec->p_state_aac->ui_mps_out_bytes;
1012       } else {
1013         *pui_value = p_obj_exhaacplus_dec->p_state_aac->ui_out_bytes;
1014       }
1015       if (p_obj_exhaacplus_dec->p_state_aac->audio_object_type == AOT_USAC) {
1016         ia_audio_specific_config_struct *ptr_audio_specific_config =
1017             ((ia_audio_specific_config_struct *)
1018                  p_obj_exhaacplus_dec->p_state_aac->ia_audio_specific_config);
1019 
1020         WORD32 preroll_counter = ptr_audio_specific_config->str_usac_config
1021                                      .str_usac_dec_config.preroll_counter;
1022 
1023         *pui_value = ptr_audio_specific_config->str_usac_config
1024                          .str_usac_dec_config.preroll_bytes[preroll_counter];
1025 
1026         preroll_counter++;
1027         if (preroll_counter > (MAX_AUDIO_PREROLLS + 1)) return IA_FATAL_ERROR;
1028         ptr_audio_specific_config->str_usac_config.str_usac_dec_config
1029             .preroll_counter = preroll_counter;
1030       }
1031       break;
1032     }
1033     case IA_API_CMD_INPUT_OVER: {
1034       p_obj_exhaacplus_dec->p_state_aac->ui_input_over = 1;
1035       break;
1036     }
1037     default: { return IA_XHEAAC_DEC_API_FATAL_INVALID_CMD; }
1038   };
1039   return IA_NO_ERROR;
1040 }
1041 
ixheaacd_decoder_2_ga_hdr(ia_exhaacplus_dec_api_struct * p_obj_exhaacplus_dec)1042 IA_ERRORCODE ixheaacd_decoder_2_ga_hdr(ia_exhaacplus_dec_api_struct *p_obj_exhaacplus_dec) {
1043   IA_ERRORCODE err_code = IA_NO_ERROR;
1044   if (p_obj_exhaacplus_dec->aac_config.ui_flush_cmd == 0) {
1045     p_obj_exhaacplus_dec->aac_config.ui_pcm_wdsz = 16;
1046     p_obj_exhaacplus_dec->aac_config.flag_downmix = 0;
1047     p_obj_exhaacplus_dec->aac_config.flag_08khz_out = 0;
1048     p_obj_exhaacplus_dec->aac_config.flag_16khz_out = 0;
1049     p_obj_exhaacplus_dec->aac_config.flag_to_stereo = 0;
1050     p_obj_exhaacplus_dec->aac_config.down_sample_flag = 0;
1051     p_obj_exhaacplus_dec->aac_config.header_dec_done = 0;
1052     p_obj_exhaacplus_dec->aac_config.frame_status = 1;
1053     p_obj_exhaacplus_dec->aac_config.ui_mp4_flag = 1;
1054     p_obj_exhaacplus_dec->aac_config.ui_disable_sync = 0;
1055     p_obj_exhaacplus_dec->aac_config.ui_auto_sbr_upsample = 1;
1056     p_obj_exhaacplus_dec->aac_config.ui_samp_freq = 0;
1057     p_obj_exhaacplus_dec->aac_config.ui_channel_mode = 3;
1058     p_obj_exhaacplus_dec->aac_config.ui_sbr_mode = 2;
1059     p_obj_exhaacplus_dec->aac_config.ui_pce_found_in_hdr = 0;
1060     p_obj_exhaacplus_dec->aac_config.loas_present = 0;
1061     p_obj_exhaacplus_dec->aac_config.ui_drc_boost = 0;
1062     p_obj_exhaacplus_dec->aac_config.ui_drc_cut = 0;
1063     p_obj_exhaacplus_dec->aac_config.ui_drc_target_level = 108;
1064     p_obj_exhaacplus_dec->aac_config.ui_drc_set = 0;
1065     p_obj_exhaacplus_dec->aac_config.ui_flush_cmd = 1;
1066 
1067     p_obj_exhaacplus_dec->aac_config.ui_max_channels = 6;
1068 
1069     p_obj_exhaacplus_dec->aac_config.ui_coupling_channel = 0;
1070     p_obj_exhaacplus_dec->aac_config.downmix = 0;
1071 
1072     {
1073       ia_aac_dec_tables_struct *pstr_aac_tables =
1074           &p_obj_exhaacplus_dec->aac_tables;
1075       pstr_aac_tables->pstr_huffmann_tables =
1076           (ia_aac_dec_huffman_tables_struct *)&ixheaacd_aac_huffmann_tables;
1077       pstr_aac_tables->pstr_block_tables =
1078           (ia_aac_dec_block_tables_struct *)&ixheaacd_aac_block_tables;
1079       pstr_aac_tables->pstr_imdct_tables =
1080           (ia_aac_dec_imdct_tables_struct *)&ixheaacd_imdct_tables;
1081 
1082       ixheaacd_huff_tables_create(pstr_aac_tables);
1083     }
1084     ixheaacd_init_sbr_tables(&p_obj_exhaacplus_dec->str_sbr_tables);
1085     p_obj_exhaacplus_dec->common_tables =
1086         (ixheaacd_misc_tables *)&ixheaacd_str_fft_n_transcendent_tables;
1087     p_obj_exhaacplus_dec->aac_config.ui_qmf_bands = 64;
1088     p_obj_exhaacplus_dec->p_state_aac->ui_init_done = 0;
1089 
1090     err_code = ixheaacd_dec_init(p_obj_exhaacplus_dec);
1091   } else {
1092     p_obj_exhaacplus_dec->aac_config.ui_flush_cmd = 0;
1093     err_code = ixheaacd_dec_init(p_obj_exhaacplus_dec);
1094   }
1095   if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && err_code) {
1096     if (err_code & IA_FATAL_ERROR) {
1097       return IA_XHEAAC_DEC_INIT_FATAL_EC_INIT_FAIL;
1098     } else {
1099       return IA_XHEAAC_DEC_INIT_NONFATAL_EC_INIT_FAIL;
1100     }
1101   } else {
1102     return err_code;
1103   }
1104 }
1105 
ixheaacd_decoder_flush_api(ia_exhaacplus_dec_api_struct * p_obj_exhaacplus_dec)1106 IA_ERRORCODE ixheaacd_decoder_flush_api(ia_exhaacplus_dec_api_struct *p_obj_exhaacplus_dec) {
1107   IA_ERRORCODE err_code = IA_NO_ERROR;
1108   UWORD8 *header_temp_ptr;
1109   WORD32 header_length;
1110   if (p_obj_exhaacplus_dec->aac_config.ui_flush_cmd == 0) {
1111     header_temp_ptr = p_obj_exhaacplus_dec->p_state_aac->header_ptr;
1112     header_length = p_obj_exhaacplus_dec->p_state_aac->header_length;
1113     memset(p_obj_exhaacplus_dec->p_state_aac, 0,
1114            sizeof(ia_aac_dec_state_struct));
1115     {
1116       pUWORD8 p_temp = (pUWORD8)p_obj_exhaacplus_dec->p_state_aac;
1117       UWORD32 *meminfo = (UWORD32 *)p_obj_exhaacplus_dec->p_mem_info_aac;
1118       UWORD32 pers_size = meminfo[0];
1119       p_temp = p_temp + pers_size -
1120                (sizeof(ia_dec_data_struct) +
1121                 sizeof(ia_audio_specific_config_struct) + (8300));
1122 
1123       p_obj_exhaacplus_dec->p_state_aac->pstr_dec_data = p_temp;
1124       p_obj_exhaacplus_dec->p_state_aac->ia_audio_specific_config =
1125           p_temp + sizeof(ia_dec_data_struct);
1126       p_obj_exhaacplus_dec->p_state_aac->header_ptr =
1127           p_temp + sizeof(ia_dec_data_struct) +
1128           sizeof(ia_audio_specific_config_struct);
1129     }
1130     memset(&(p_obj_exhaacplus_dec->aac_config), 0,
1131            sizeof(ia_aac_dec_config_struct));
1132 
1133     p_obj_exhaacplus_dec->aac_config.ui_pcm_wdsz = 16;
1134     p_obj_exhaacplus_dec->aac_config.flag_downmix = 0;
1135     p_obj_exhaacplus_dec->aac_config.flag_08khz_out = 0;
1136     p_obj_exhaacplus_dec->aac_config.flag_16khz_out = 0;
1137     p_obj_exhaacplus_dec->aac_config.flag_to_stereo = 0;
1138     p_obj_exhaacplus_dec->aac_config.down_sample_flag = 0;
1139     p_obj_exhaacplus_dec->aac_config.header_dec_done = 0;
1140     p_obj_exhaacplus_dec->aac_config.frame_status = 1;
1141     p_obj_exhaacplus_dec->aac_config.ui_mp4_flag = 1;
1142     p_obj_exhaacplus_dec->aac_config.ui_disable_sync = 0;
1143     p_obj_exhaacplus_dec->aac_config.ui_auto_sbr_upsample = 1;
1144     p_obj_exhaacplus_dec->aac_config.ui_samp_freq = 0;
1145     p_obj_exhaacplus_dec->aac_config.ui_channel_mode = 3;
1146     p_obj_exhaacplus_dec->aac_config.ui_sbr_mode = 2;
1147     p_obj_exhaacplus_dec->aac_config.ui_pce_found_in_hdr = 0;
1148     p_obj_exhaacplus_dec->aac_config.loas_present = 0;
1149     p_obj_exhaacplus_dec->aac_config.ui_drc_boost = 0;
1150     p_obj_exhaacplus_dec->aac_config.ui_drc_cut = 0;
1151     p_obj_exhaacplus_dec->aac_config.ui_drc_target_level = 108;
1152     p_obj_exhaacplus_dec->aac_config.ui_drc_set = 0;
1153     p_obj_exhaacplus_dec->aac_config.ui_flush_cmd = 1;
1154 
1155     p_obj_exhaacplus_dec->aac_config.ui_max_channels = 6;
1156 
1157     p_obj_exhaacplus_dec->aac_config.ui_coupling_channel = 0;
1158     p_obj_exhaacplus_dec->aac_config.downmix = 0;
1159     p_obj_exhaacplus_dec->p_state_aac->peak_lim_init = 0;
1160 
1161     {
1162       ia_aac_dec_tables_struct *pstr_aac_tables =
1163           &p_obj_exhaacplus_dec->aac_tables;
1164       pstr_aac_tables->pstr_huffmann_tables =
1165           (ia_aac_dec_huffman_tables_struct *)&ixheaacd_aac_huffmann_tables;
1166       pstr_aac_tables->pstr_block_tables =
1167           (ia_aac_dec_block_tables_struct *)&ixheaacd_aac_block_tables;
1168       pstr_aac_tables->pstr_imdct_tables =
1169           (ia_aac_dec_imdct_tables_struct *)&ixheaacd_imdct_tables;
1170 
1171       ixheaacd_huff_tables_create(pstr_aac_tables);
1172     }
1173     ixheaacd_init_sbr_tables(&p_obj_exhaacplus_dec->str_sbr_tables);
1174     p_obj_exhaacplus_dec->common_tables =
1175         (ixheaacd_misc_tables *)&ixheaacd_str_fft_n_transcendent_tables;
1176     p_obj_exhaacplus_dec->aac_config.ui_qmf_bands = 64;
1177     p_obj_exhaacplus_dec->p_state_aac->header_ptr = header_temp_ptr;
1178     p_obj_exhaacplus_dec->p_state_aac->ui_in_bytes = header_length;
1179     p_obj_exhaacplus_dec->p_state_aac->header_length = header_length;
1180 
1181     err_code = ixheaacd_dec_init(p_obj_exhaacplus_dec);
1182   } else {
1183     p_obj_exhaacplus_dec->aac_config.ui_flush_cmd = 0;
1184     err_code = ixheaacd_dec_init(p_obj_exhaacplus_dec);
1185   }
1186   if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && err_code) {
1187     if (err_code & IA_FATAL_ERROR) {
1188       return IA_XHEAAC_DEC_INIT_FATAL_EC_INIT_FAIL;
1189     } else {
1190       return IA_XHEAAC_DEC_INIT_NONFATAL_EC_INIT_FAIL;
1191     }
1192   } else {
1193     return err_code;
1194   }
1195 }
1196 
1197 static PLATFORM_INLINE WORD32
ixheaacd_persistent_buffer_sizes(WORD32 num_channel)1198 ixheaacd_persistent_buffer_sizes(WORD32 num_channel) {
1199   WORD32 size_buffers = 0;
1200 
1201   WORD32 temp;
1202   WORD32 max_channels;
1203 
1204   size_buffers += 4 * 512 * num_channel * sizeof(WORD32);
1205 
1206   size_buffers += (ltp_buffer_size * num_channel * sizeof(WORD16));
1207 
1208   size_buffers += num_channel * sizeof(ia_aac_dec_channel_info);
1209 
1210   if (num_channel > 2) {
1211     max_channels = MAX_BS_ELEMENT;
1212   } else {
1213     max_channels = 2;
1214   }
1215   size_buffers +=
1216       (max_channels) * 2 * ALIGN_SIZE64(sizeof(ia_aac_dec_sbr_bitstream_struct));
1217 
1218   size_buffers += ALIGN_SIZE64(MAXSBRBYTES) * num_channel * sizeof(WORD8);
1219 
1220   size_buffers += ALIGN_SIZE64(MAXSBRBYTES) * num_channel * sizeof(WORD8);
1221 
1222   size_buffers += 2 * num_channel *
1223                   (QMF_FILTER_STATE_ANA_SIZE + 2 * NO_ANALYSIS_CHANNELS) *
1224                   sizeof(WORD16);
1225 
1226   size_buffers += 2 * num_channel *
1227                   (QMF_FILTER_STATE_ANA_SIZE + 2 * NO_ANALYSIS_CHANNELS) *
1228                   sizeof(WORD32);
1229 
1230   size_buffers += 2 * num_channel *
1231                   (QMF_FILTER_STATE_SYN_SIZE + 2 * NO_SYNTHESIS_CHANNELS) *
1232                   sizeof(WORD16);
1233 
1234   size_buffers += 2 * num_channel *
1235                   (QMF_FILTER_STATE_SYN_SIZE + 2 * NO_SYNTHESIS_CHANNELS) *
1236                   sizeof(WORD32);
1237 
1238   size_buffers +=
1239       num_channel * 2 * MAX_OV_COLS * NO_SYNTHESIS_CHANNELS * sizeof(WORD32);
1240 
1241   size_buffers +=
1242       2 * LPC_ORDER * num_channel * NO_ANALYSIS_CHANNELS * sizeof(WORD32);
1243 
1244   size_buffers += LPC_ORDER * 2 * sizeof(WORD32 *);
1245 
1246   size_buffers +=
1247       2 * LPC_ORDER * num_channel * NO_ANALYSIS_CHANNELS * sizeof(WORD32);
1248 
1249   size_buffers += num_channel * 3 * 2 * MAX_FREQ_COEFFS * sizeof(WORD16);
1250 
1251   temp = sizeof(ia_freq_band_data_struct) +
1252          sizeof(ia_sbr_prev_frame_data_struct) + sizeof(ia_sbr_channel_struct) +
1253          sizeof(ia_sbr_header_data_struct);
1254   size_buffers += 2 * num_channel * ALIGN_SIZE64(temp);
1255 
1256   size_buffers += MAX_BS_ELEMENT * sizeof(ixheaac_drc_bs_data_struct *);
1257 
1258   size_buffers += num_channel * sizeof(ia_ps_dec_struct);
1259 
1260   {
1261     WORD32 temp_size = 0;
1262     size_buffers +=
1263     (num_channel * (sizeof(ia_sbr_frame_info_data_struct) +
1264                             MAX_FREQ_COEFFS * sizeof(WORD32) * 2 + 8) * 2);
1265     temp_size += sizeof(ia_pvc_data_struct);
1266     temp_size += sizeof(ia_esbr_hbe_txposer_struct) * 2;
1267     temp_size += (MAX_HBE_PERSISTENT_SIZE * 2);
1268     temp_size += (MAX_QMF_BUF_LEN * 2 * 2 * sizeof(FLOAT32 *));
1269     temp_size += (MAX_QMF_BUF_LEN * MAX_QMF_BUF_LEN * 2 * 2 * sizeof(FLOAT32));
1270     size_buffers += temp_size * num_channel;
1271   }
1272 
1273   return (size_buffers);
1274 }
1275 
ixheaacd_fill_aac_mem_tables(ia_exhaacplus_dec_api_struct * p_obj_exhaacplus_dec)1276 VOID ixheaacd_fill_aac_mem_tables(
1277     ia_exhaacplus_dec_api_struct *p_obj_exhaacplus_dec) {
1278   ia_mem_info_struct *p_mem_info_aac;
1279 
1280   WORD32 num_channels;
1281   WORD32 channels;
1282   WORD32 buffer_size;
1283 
1284   if (p_obj_exhaacplus_dec->aac_config.ui_max_channels > 2) {
1285     num_channels = (p_obj_exhaacplus_dec->aac_config.ui_max_channels + 1);
1286   } else
1287 
1288   {
1289     num_channels = p_obj_exhaacplus_dec->aac_config.ui_max_channels;
1290   }
1291 
1292   channels = num_channels;
1293   buffer_size = ixheaacd_persistent_buffer_sizes(num_channels);
1294 
1295   {
1296     p_mem_info_aac =
1297         &p_obj_exhaacplus_dec->p_mem_info_aac[IA_ENHAACPLUS_DEC_PERSIST_IDX];
1298     p_mem_info_aac->ui_size =
1299         sizeof(ia_aac_dec_state_struct) +
1300         channels * sizeof(struct ia_aac_persistent_struct) +
1301         buffer_size + channels * ixheaacd_getsize_sbr_persistent() +
1302         channels * 16 + ixheaacd_mps_persistent_buffer_sizes();
1303     p_mem_info_aac->ui_size += sizeof(ia_dec_data_struct);
1304     p_mem_info_aac->ui_size += sizeof(ia_audio_specific_config_struct);
1305     p_mem_info_aac->ui_size += 8300;
1306 
1307     p_mem_info_aac->ui_alignment = 8;
1308     p_mem_info_aac->ui_type = IA_MEMTYPE_PERSIST;
1309   }
1310 
1311   {
1312     p_mem_info_aac =
1313         &p_obj_exhaacplus_dec->p_mem_info_aac[IA_ENHAACPLUS_DEC_SCRATCH_IDX];
1314 
1315     {
1316       if (num_channels > 2) {
1317         WORD32 other_scr1;
1318         WORD32 other_scr2 = 0;
1319 
1320         p_mem_info_aac->ui_size =
1321             2 * sizeof(WORD32) * IA_ENHAACPLUS_DEC_SAMPLES_PER_FRAME;
1322 
1323         other_scr2 = 2 * sizeof(WORD32) * IA_ENHAACPLUS_DEC_SAMPLES_PER_FRAME;
1324 
1325         other_scr1 = (4 * 1024);
1326 
1327         if (MAX_CC_CHANNEL_NUM > 0) {
1328           other_scr1 +=
1329               sizeof(WORD16) * IA_ENHAACPLUS_DEC_SAMPLES_PER_FRAME * 2;
1330           other_scr1 += (4 * 1024);
1331 
1332           other_scr1 += 4 * 12;
1333         }
1334 
1335         p_mem_info_aac->ui_size += max(other_scr1, other_scr2);
1336 
1337       } else {
1338         p_mem_info_aac->ui_size =
1339             2 * sizeof(WORD32) * IA_ENHAACPLUS_DEC_SAMPLES_PER_FRAME;
1340 
1341         p_mem_info_aac->ui_size +=
1342             2 * sizeof(WORD32) * IA_ENHAACPLUS_DEC_SAMPLES_PER_FRAME;
1343 
1344         p_mem_info_aac->ui_size +=
1345             sizeof(WORD32) * IA_ENHAACPLUS_DEC_SAMPLES_PER_FRAME;
1346         p_mem_info_aac->ui_size += 4 * 12;
1347 
1348         p_mem_info_aac->ui_size +=
1349             ((IA_ENHAACPLUS_DEC_SAMPLES_PER_FRAME << 1) * sizeof(WORD32));
1350         p_mem_info_aac->ui_size +=
1351             ((IA_ENHAACPLUS_DEC_SAMPLES_PER_FRAME << 1) * sizeof(WORD32));
1352         p_mem_info_aac->ui_size +=
1353             2 * (sizeof(ia_sbr_frame_info_data_struct) + 232);
1354       }
1355     }
1356 
1357     p_mem_info_aac->ui_size += 2200000 + 2048;
1358     p_mem_info_aac->ui_alignment = 8;
1359     p_mem_info_aac->ui_type = IA_MEMTYPE_SCRATCH;
1360   }
1361   {
1362     p_mem_info_aac =
1363         &p_obj_exhaacplus_dec->p_mem_info_aac[IA_ENHAACPLUS_DEC_INPUT_IDX];
1364 
1365     p_mem_info_aac->ui_size = IA_MAX_INP_BUFFER_SIZE;
1366 
1367     p_mem_info_aac->ui_alignment = 8;
1368     p_mem_info_aac->ui_type = IA_MEMTYPE_INPUT;
1369   }
1370   {
1371     p_mem_info_aac =
1372         &p_obj_exhaacplus_dec->p_mem_info_aac[IA_ENHAACPLUS_DEC_OUTPUT_IDX];
1373     p_mem_info_aac->ui_size = IA_ENHAACPLUS_DEC_OUT_BUF_SIZE;
1374     p_mem_info_aac->ui_alignment = 8;
1375     p_mem_info_aac->ui_type = IA_MEMTYPE_OUTPUT;
1376   }
1377   return;
1378 }
1379 
ixheaacd_dec_table_api(ia_exhaacplus_dec_api_struct * p_obj_exhaacplus_dec,WORD32 i_cmd,WORD32 i_idx,pVOID pv_value)1380 IA_ERRORCODE ixheaacd_dec_table_api(
1381     ia_exhaacplus_dec_api_struct *p_obj_exhaacplus_dec, WORD32 i_cmd,
1382     WORD32 i_idx, pVOID pv_value) {
1383   ia_heaac_mps_state_struct *pstr_mps_state =
1384       &p_obj_exhaacplus_dec->p_state_aac->heaac_mps_handle;
1385   pUWORD32 pui_value = pv_value;
1386   pUWORD32 *p_pui_value = pv_value;
1387   SIZE_T ui_get_vals[5];
1388 
1389   pVOID *table_ptrs[8 + NUM_MPS_TABLES];
1390   UWORD32 table_sizes[8 + NUM_MPS_TABLES] = {
1391       sizeof(ixheaacd_aac_huffmann_tables),
1392       sizeof(ixheaacd_aac_block_tables),
1393       sizeof(ixheaacd_imdct_tables),
1394       sizeof(ixheaacd_str_fft_n_transcendent_tables),
1395       sizeof(ixheaacd_aac_dec_env_calc_tables),
1396       sizeof(ixheaacd_aac_qmf_dec_tables),
1397       sizeof(ixheaacd_aac_dec_env_extr_tables),
1398       sizeof(ixheaacd_aac_dec_ps_tables),
1399       sizeof(ixheaacd_mps_dec_qmf_tables),
1400       sizeof(ixheaacd_mps_dec_common_tables),
1401       sizeof(ixheaacd_mps_dec_hybrid_tables),
1402       sizeof(ixheaacd_mps_dec_m1_m2_tables),
1403       sizeof(ixheaacd_mps_dec_decorr_tables),
1404       sizeof(ixheaacd_mps_dec_tp_process_tables),
1405       sizeof(ixheaacd_mps_dec_mdct2qmf_table),
1406       sizeof(ixheaacd_mps_dec_tonality_tables),
1407       sizeof(ixheaacd_mps_dec_bitdec_tables),
1408       sizeof(ixheaacd_mps_dec_blind_tables),
1409       sizeof(ixheaacd_mps_dec_mdct2qmf_tables),
1410       sizeof(ia_mps_dec_mdct2qmf_cos_table_struct),
1411       sizeof(ia_mps_dec_residual_aac_tables_struct)};
1412   table_ptrs[0] =
1413       (pVOID *)&(p_obj_exhaacplus_dec->aac_tables.pstr_huffmann_tables);
1414   table_ptrs[1] =
1415       (pVOID *)&(p_obj_exhaacplus_dec->aac_tables.pstr_block_tables);
1416   table_ptrs[2] =
1417       (pVOID *)&(p_obj_exhaacplus_dec->aac_tables.pstr_imdct_tables);
1418   table_ptrs[3] = (pVOID *)&(p_obj_exhaacplus_dec->common_tables);
1419   table_ptrs[4] =
1420       (pVOID *)&p_obj_exhaacplus_dec->str_sbr_tables.env_calc_tables_ptr;
1421   table_ptrs[5] =
1422       (pVOID *)&p_obj_exhaacplus_dec->str_sbr_tables.qmf_dec_tables_ptr;
1423   table_ptrs[6] =
1424       (pVOID *)&p_obj_exhaacplus_dec->str_sbr_tables.env_extr_tables_ptr;
1425   table_ptrs[7] = (pVOID *)&p_obj_exhaacplus_dec->str_sbr_tables.ps_tables_ptr;
1426   table_ptrs[8] = (pVOID *)&(pstr_mps_state->ia_mps_dec_mps_table.qmf_table_ptr);
1427   table_ptrs[9] = (pVOID *)&(pstr_mps_state->ia_mps_dec_mps_table.common_table_ptr);
1428   table_ptrs[10] = (pVOID *)&(pstr_mps_state->ia_mps_dec_mps_table.hybrid_table_ptr);
1429   table_ptrs[11] = (pVOID *)&(pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr);
1430   table_ptrs[12] = (pVOID *)&(pstr_mps_state->ia_mps_dec_mps_table.decor_table_ptr);
1431   table_ptrs[13] = (pVOID *)&(pstr_mps_state->ia_mps_dec_mps_table.tp_process_table_ptr);
1432   table_ptrs[14] = (pVOID *)&(pstr_mps_state->ia_mps_dec_mps_table.mdct2qmf_table_ptr);
1433   table_ptrs[15] = (pVOID *)&(pstr_mps_state->ia_mps_dec_mps_table.tonality_table_ptr);
1434   table_ptrs[16] = (pVOID *)&(pstr_mps_state->ia_mps_dec_mps_table.bitdec_table_ptr);
1435   table_ptrs[17] = (pVOID *)&(pstr_mps_state->ia_mps_dec_mps_table.blind_table_ptr);
1436   table_ptrs[18] = (pVOID *)&(pstr_mps_state->ia_mps_dec_mps_table.mdct2qmfcos_table_ptr);
1437   table_ptrs[19] = (pVOID *)&(pstr_mps_state->ia_mps_dec_mps_table.mdct2qmfcos_tab_ptr);
1438   table_ptrs[20] = (pVOID *)&(pstr_mps_state->ia_mps_dec_mps_table.aac_tab);
1439 
1440   if (i_idx < 0 || i_idx >= (NUM_AAC_TABLES + NUM_MPS_TABLES)) {
1441     return IA_XHEAAC_DEC_API_FATAL_INVALID_MEMTAB_INDEX;
1442   }
1443 
1444   ui_get_vals[0] = table_sizes[i_idx];
1445   ui_get_vals[1] = 4;
1446   ui_get_vals[4] = (SIZE_T)(*table_ptrs[i_idx]);
1447 
1448   if (i_cmd == IA_API_CMD_SET_TABLE_PTR) {
1449     if (pv_value == 0) {
1450       return (IA_XHEAAC_DEC_API_FATAL_MEM_ALLOC);
1451     }
1452     if (((SIZE_T)pv_value) & 3) {
1453       return IA_XHEAAC_DEC_API_FATAL_MEM_ALIGN;
1454     }
1455 
1456     *table_ptrs[i_idx] = pv_value;
1457 
1458     if (i_idx == 0) {
1459       ixheaacd_huff_tables_create(&p_obj_exhaacplus_dec->aac_tables);
1460     }
1461 
1462   }
1463 
1464   else if (i_cmd == IA_API_CMD_GET_TABLE_PTR) {
1465     *p_pui_value = (UWORD32 *)((SIZE_T)(
1466         ui_get_vals[i_cmd - IA_API_CMD_GET_TABLE_INFO_SIZE]));
1467   } else {
1468     *pui_value = (WORD32)(ui_get_vals[i_cmd - IA_API_CMD_GET_TABLE_INFO_SIZE]);
1469   }
1470 
1471   return IA_NO_ERROR;
1472 }
1473 
ixheaacd_mps_payload(ia_handle_sbr_dec_inst_struct self,ia_exhaacplus_dec_api_struct * p_obj_exhaacplus_dec)1474 VOID ixheaacd_mps_payload(ia_handle_sbr_dec_inst_struct self,
1475                           ia_exhaacplus_dec_api_struct *p_obj_exhaacplus_dec) {
1476   struct ia_bit_buf_struct local_bit_buff;
1477   struct ia_bit_buf_struct *it_bit_buff;
1478   if (self->ptr_mps_data != NULL) {
1479     ixheaacd_create_init_bit_buf(&local_bit_buff, (UWORD8 *)self->ptr_mps_data,
1480                                  (self->left_mps_bits >> 3) + 1);
1481   }
1482 
1483   local_bit_buff.xaac_jmp_buf =
1484       &p_obj_exhaacplus_dec->p_state_aac->xaac_jmp_buf;
1485 
1486   it_bit_buff = &local_bit_buff;
1487 
1488   it_bit_buff->bit_pos = self->mps_bits_pos;
1489   it_bit_buff->cnt_bits = self->left_mps_bits;
1490 
1491   while (self->left_mps_bits >= 8) {
1492     ixheaacd_extension_payload(
1493         it_bit_buff, &self->left_mps_bits,
1494         &p_obj_exhaacplus_dec->p_state_aac->mps_dec_handle);
1495   }
1496 }
1497 
ixheaacd_dec_init(ia_exhaacplus_dec_api_struct * p_obj_exhaacplus_dec)1498 IA_ERRORCODE ixheaacd_dec_init(
1499     ia_exhaacplus_dec_api_struct *p_obj_exhaacplus_dec) {
1500   FLAG frame_status = 1;
1501   WORD32 frame_size_1;
1502   WORD32 sample_rate_1;
1503   WORD16 num_channels_1;
1504   WORD32 ps_detected = 0;
1505   UWORD8 *in_buffer;
1506   WORD16 *time_data;
1507   WORD ch_idx;
1508   WORD sbr_present_flag = 0;
1509   UWORD8 *mps_buffer;
1510   ia_aac_dec_state_struct *p_state_enhaacplus_dec;
1511 
1512   WORD32 error_code = IA_NO_ERROR;
1513   WORD32 persistent_used = 0;
1514   IA_ERRORCODE err_code = IA_NO_ERROR;
1515   struct ia_aac_persistent_struct *aac_persistent_mem;
1516   struct ia_sbr_pers_struct *sbr_persistent_mem;
1517   WORD32 ret_val;
1518 
1519   p_obj_exhaacplus_dec->p_state_aac =
1520       p_obj_exhaacplus_dec->pp_mem_aac[IA_ENHAACPLUS_DEC_PERSIST_IDX];
1521 
1522   if (p_obj_exhaacplus_dec->p_state_aac->ui_init_done)
1523   {
1524     return IA_NO_ERROR;
1525   }
1526 
1527   p_obj_exhaacplus_dec->p_state_aac->preroll_config_present = 0;
1528 
1529   if (p_obj_exhaacplus_dec->p_state_aac != NULL) {
1530     ret_val = setjmp(p_obj_exhaacplus_dec->p_state_aac->xaac_jmp_buf);
1531     if (ret_val != 0) {
1532       p_obj_exhaacplus_dec->p_state_aac->i_bytes_consumed =
1533           p_obj_exhaacplus_dec->p_state_aac->ui_in_bytes;
1534       p_obj_exhaacplus_dec->p_state_aac->ui_out_bytes = 0;
1535       return IA_NO_ERROR;
1536     }
1537   }
1538 
1539   time_data = (WORD16 *)(p_obj_exhaacplus_dec
1540                              ->pp_mem_aac[IA_ENHAACPLUS_DEC_OUTPUT_IDX]);
1541 
1542   if (p_obj_exhaacplus_dec->aac_config.ui_flush_cmd == 0) {
1543     in_buffer = p_obj_exhaacplus_dec->pp_mem_aac[IA_ENHAACPLUS_DEC_INPUT_IDX];
1544   } else {
1545     in_buffer = p_obj_exhaacplus_dec->p_state_aac->header_ptr;
1546   }
1547 
1548   p_state_enhaacplus_dec = p_obj_exhaacplus_dec->p_state_aac;
1549 
1550   p_state_enhaacplus_dec->aac_scratch_mem_v =
1551       p_obj_exhaacplus_dec->pp_mem_aac[IA_ENHAACPLUS_DEC_SCRATCH_IDX];
1552   p_obj_exhaacplus_dec->p_state_aac->huffman_code_book_scl =
1553       p_obj_exhaacplus_dec->aac_tables.pstr_huffmann_tables
1554           ->huffman_code_book_scl;
1555   mps_buffer = p_obj_exhaacplus_dec->p_state_aac->mps_buffer;
1556   p_state_enhaacplus_dec->mps_header = -1;
1557   p_obj_exhaacplus_dec->p_state_aac->huffman_code_book_scl_index =
1558       p_obj_exhaacplus_dec->aac_tables.pstr_huffmann_tables
1559           ->huffman_code_book_scl_index;
1560 
1561   p_state_enhaacplus_dec->pstr_aac_tables = &p_obj_exhaacplus_dec->aac_tables;
1562   if (p_obj_exhaacplus_dec->p_state_aac->header_dec_done == 0)
1563   {
1564     p_obj_exhaacplus_dec->aac_config.header_dec_done = 0;
1565     p_state_enhaacplus_dec->mps_dec_handle.ldmps_config.ldmps_present_flag = 0;
1566   }
1567   if (p_obj_exhaacplus_dec->aac_config.header_dec_done == 0) {
1568     WORD32 channels;
1569 
1570     p_obj_exhaacplus_dec->p_state_aac->p_config =
1571         &p_obj_exhaacplus_dec->aac_config;
1572 
1573     p_obj_exhaacplus_dec->p_state_aac->pstr_stream_sbr =
1574         (pVOID)((SIZE_T)((pWORD8)p_obj_exhaacplus_dec->p_state_aac +
1575                          sizeof(ia_aac_dec_state_struct) + sizeof(SIZE_T) - 1) &
1576                 (SIZE_T)(~(sizeof(SIZE_T) - 1)));
1577     if (p_obj_exhaacplus_dec->aac_config.ui_max_channels > 2) {
1578       p_state_enhaacplus_dec->aac_persistent_mem_v =
1579           (pVOID)((pWORD8)p_obj_exhaacplus_dec->p_state_aac->pstr_stream_sbr +
1580                   (MAX_BS_ELEMENT)*2 *
1581                       ALIGN_SIZE64(sizeof(ia_aac_dec_sbr_bitstream_struct)));
1582 
1583       memset(p_obj_exhaacplus_dec->p_state_aac->pstr_stream_sbr, 0,
1584              (MAX_BS_ELEMENT)*2 *
1585                  ALIGN_SIZE64(sizeof(ia_aac_dec_sbr_bitstream_struct)));
1586     } else {
1587       p_state_enhaacplus_dec->aac_persistent_mem_v = (pVOID)(
1588           (pWORD8)p_obj_exhaacplus_dec->p_state_aac->pstr_stream_sbr +
1589           (2) * 2 * ALIGN_SIZE64(sizeof(ia_aac_dec_sbr_bitstream_struct)));
1590 
1591       memset(p_obj_exhaacplus_dec->p_state_aac->pstr_stream_sbr, 0,
1592              (2) * 2 * ALIGN_SIZE64(sizeof(ia_aac_dec_sbr_bitstream_struct)));
1593     }
1594     if (1 == p_obj_exhaacplus_dec->aac_config.ui_max_channels)
1595       channels = 1;
1596     else
1597       channels = 2;
1598 
1599     persistent_used = ixheaacd_set_aac_persistent_buffers(
1600         p_state_enhaacplus_dec->aac_persistent_mem_v, channels);
1601 
1602     p_state_enhaacplus_dec->sbr_persistent_mem_v =
1603         (pVOID)((SIZE_T)((pWORD8)p_state_enhaacplus_dec->aac_persistent_mem_v +
1604                          persistent_used + sizeof(SIZE_T) - 1) &
1605                 (SIZE_T)(~(sizeof(SIZE_T) - 1)));
1606 
1607     persistent_used = ixheaacd_getsize_sbr_persistent();
1608     ixheaacd_set_sbr_persistent_buffers(
1609         p_state_enhaacplus_dec->sbr_persistent_mem_v, &persistent_used,
1610         channels, 1);
1611 
1612     p_state_enhaacplus_dec->heaac_mps_handle.mps_persistent_mem_v =
1613         (pVOID)((SIZE_T)((pWORD8)p_state_enhaacplus_dec->sbr_persistent_mem_v +
1614                          persistent_used + sizeof(SIZE_T) - 1) &
1615                 (SIZE_T)(~(sizeof(SIZE_T) - 1)));
1616 
1617     persistent_used = ixheaacd_getsize_mps_persistent();
1618 
1619     ixheaacd_set_mps_persistent_buffers(
1620         &p_state_enhaacplus_dec->heaac_mps_handle, &persistent_used, channels,
1621         p_state_enhaacplus_dec->heaac_mps_handle.mps_persistent_mem_v);
1622 
1623     aac_persistent_mem = (struct ia_aac_persistent_struct *)
1624                              p_state_enhaacplus_dec->aac_persistent_mem_v;
1625     if (p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_LD ||
1626         p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_ELD)
1627       p_state_enhaacplus_dec->frame_len_flag =
1628           p_obj_exhaacplus_dec->aac_config.framesize_480;
1629 
1630     p_state_enhaacplus_dec->ptr_overlap_buf =
1631         aac_persistent_mem->overlap_buffer;
1632 
1633     p_state_enhaacplus_dec->bit_count = 0;
1634     p_state_enhaacplus_dec->ec_enable = p_obj_exhaacplus_dec->aac_config.ui_err_conceal;
1635     p_state_enhaacplus_dec->sync_status = 0;
1636     p_state_enhaacplus_dec->bs_format = ADTS_BSFORMAT;
1637     p_state_enhaacplus_dec->latm_initialized = 0;
1638     p_state_enhaacplus_dec->frame_size = 0;
1639     memset(&p_state_enhaacplus_dec->latm_struct_element, 0,
1640            sizeof(ixheaacd_latm_struct));
1641     memset(&p_state_enhaacplus_dec->b_n_raw_data_blk, 0,
1642            sizeof(WORD32) * (9 + MAX_BS_ELEMENT));
1643 
1644     p_state_enhaacplus_dec->sbr_present_flag = 0;
1645 
1646     for (ch_idx = 0; ch_idx < MAX_BS_ELEMENT; ch_idx++) {
1647       p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx] = 0;
1648     }
1649 
1650     memset(&p_state_enhaacplus_dec->ind_cc_info, 0,
1651            sizeof(ia_enhaacplus_dec_ind_cc));
1652 
1653     p_state_enhaacplus_dec->last_frame_ok = 1;
1654     p_obj_exhaacplus_dec->aac_config.header_dec_done = 1;
1655 
1656     aac_persistent_mem->str_aac_decoder.pstr_aac_tables =
1657         &p_obj_exhaacplus_dec->aac_tables;
1658     aac_persistent_mem->str_aac_decoder.pstr_common_tables =
1659         p_obj_exhaacplus_dec->common_tables;
1660 
1661     p_obj_exhaacplus_dec->p_state_aac->sbr_persistent_mem_u =
1662         p_obj_exhaacplus_dec->p_state_aac->sbr_persistent_mem_v;
1663 
1664     p_obj_exhaacplus_dec->p_state_aac->sbr_scratch_mem_u =
1665         p_obj_exhaacplus_dec->p_state_aac->aac_scratch_mem_v;
1666 
1667     ixheaacd_set_sbr_persistent_table_pointer(
1668         p_obj_exhaacplus_dec->p_state_aac->sbr_persistent_mem_v,
1669         &p_obj_exhaacplus_dec->str_sbr_tables,
1670         p_obj_exhaacplus_dec->common_tables);
1671     ixheaacd_set_scratch_buffers(
1672         &p_obj_exhaacplus_dec->p_state_aac->heaac_mps_handle,
1673         p_state_enhaacplus_dec->aac_scratch_mem_v);
1674   }
1675 
1676   if (p_obj_exhaacplus_dec->p_state_aac->ui_input_over == 1) {
1677     return IA_XHEAAC_DEC_INIT_FATAL_EO_INPUT_REACHED;
1678   }
1679 
1680   if (p_obj_exhaacplus_dec->p_state_aac->header_dec_done == 0) {
1681     if (p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_LD ||
1682         p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_ELD)
1683       p_state_enhaacplus_dec->frame_len_flag =
1684           p_obj_exhaacplus_dec->aac_config.framesize_480;
1685 
1686     aac_persistent_mem = (struct ia_aac_persistent_struct *)
1687                              p_state_enhaacplus_dec->aac_persistent_mem_v;
1688     sbr_persistent_mem = (struct ia_sbr_pers_struct *)
1689                              p_state_enhaacplus_dec->sbr_persistent_mem_v;
1690     if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
1691       p_obj_exhaacplus_dec->p_state_aac->first_frame = 1;
1692     }
1693 
1694     if (p_obj_exhaacplus_dec->aac_config.ui_samp_freq == 0) {
1695       WORD32 header_bytes_consumed, return_val;
1696 
1697       if (p_state_enhaacplus_dec->ui_in_bytes == 0) {
1698         p_state_enhaacplus_dec->i_bytes_consumed = 0;
1699         return IA_NO_ERROR;
1700       }
1701 
1702       if (1 == p_obj_exhaacplus_dec->aac_config.ui_frame_size) {
1703         p_state_enhaacplus_dec->frame_len_flag = 1;
1704         p_state_enhaacplus_dec->frame_length = 960;
1705       } else {
1706         p_state_enhaacplus_dec->frame_len_flag = 0;
1707         p_state_enhaacplus_dec->frame_length = 1024;
1708       }
1709 
1710       p_state_enhaacplus_dec->ui_init_done = 0;
1711       memset(&(p_state_enhaacplus_dec->eld_specific_config), 0,
1712              sizeof(ia_eld_specific_config_struct));
1713       return_val = ixheaacd_aac_headerdecode(
1714           p_obj_exhaacplus_dec, (UWORD8 *)in_buffer, &header_bytes_consumed,
1715           aac_persistent_mem->str_aac_decoder.pstr_aac_tables
1716               ->pstr_huffmann_tables);
1717       if (p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_LD ||
1718           p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_ELD ||
1719           p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_LC) {
1720         *sbr_persistent_mem->str_sbr_dec_inst.pstr_sbr_header[0] =
1721             p_obj_exhaacplus_dec->p_state_aac->str_sbr_config;
1722         *sbr_persistent_mem->str_sbr_dec_inst.pstr_sbr_header[1] =
1723             p_obj_exhaacplus_dec->p_state_aac->str_sbr_config;
1724       } else {
1725         memset(&(p_state_enhaacplus_dec->eld_specific_config), 0,
1726                sizeof(ia_eld_specific_config_struct));
1727       }
1728 
1729       if (return_val < 0) {
1730         if (return_val ==
1731             (WORD32)IA_XHEAAC_DEC_INIT_FATAL_STREAM_CHAN_GT_MAX) {
1732           p_state_enhaacplus_dec->i_bytes_consumed = header_bytes_consumed;
1733           return return_val;
1734         }
1735         p_state_enhaacplus_dec->i_bytes_consumed = 1;
1736 
1737         return return_val;
1738       }
1739 
1740       if (return_val ==
1741           IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES) {
1742         p_state_enhaacplus_dec->i_bytes_consumed = header_bytes_consumed;
1743         return return_val;
1744       }
1745 
1746       p_state_enhaacplus_dec->i_bytes_consumed = header_bytes_consumed;
1747 
1748       if ((return_val == 0) &&
1749           (p_obj_exhaacplus_dec->p_state_aac->audio_object_type == AOT_USAC)) {
1750         {
1751           WORD32 pcm_size = p_obj_exhaacplus_dec->aac_config.ui_pcm_wdsz;
1752           WORD8 *inbuffer =
1753               p_obj_exhaacplus_dec->pp_mem_aac[IA_ENHAACPLUS_DEC_INPUT_IDX];
1754           WORD8 *outbuffer =
1755               p_obj_exhaacplus_dec->pp_mem_aac[IA_ENHAACPLUS_DEC_OUTPUT_IDX];
1756           WORD32 out_bytes = 0;
1757           WORD32 frames_done = p_obj_exhaacplus_dec->p_state_aac->frame_counter;
1758           p_obj_exhaacplus_dec->p_state_aac->decode_create_done = 0;
1759 
1760           if (p_obj_exhaacplus_dec->p_state_aac->ui_input_over == 0) {
1761             error_code = ixheaacd_dec_main(
1762                 p_obj_exhaacplus_dec, inbuffer, outbuffer, &out_bytes,
1763                 frames_done, pcm_size,
1764                 &p_obj_exhaacplus_dec->p_state_aac->num_of_output_ch);
1765             if (error_code) return error_code;
1766             p_obj_exhaacplus_dec->p_state_aac->frame_counter++;
1767           } else {
1768             out_bytes = 0;
1769           }
1770 
1771           p_obj_exhaacplus_dec->aac_config.ui_n_channels =
1772               p_obj_exhaacplus_dec->p_state_aac->num_of_output_ch;
1773         }
1774         if (return_val == 0)
1775           p_obj_exhaacplus_dec->p_state_aac->ui_init_done = 1;
1776         return return_val;
1777       }
1778 
1779       if (return_val == 0) {
1780         p_obj_exhaacplus_dec->p_state_aac->header_dec_done = 1;
1781         if (p_obj_exhaacplus_dec->aac_config.ui_flush_cmd == 0) {
1782           memcpy(p_state_enhaacplus_dec->header_ptr, in_buffer,
1783                  header_bytes_consumed * sizeof(UWORD8));
1784           p_state_enhaacplus_dec->header_length = header_bytes_consumed;
1785         }
1786       }
1787 
1788       if (p_obj_exhaacplus_dec->p_state_aac->header_dec_done != 1)
1789         return IA_XHEAAC_DEC_INIT_NONFATAL_HEADER_NOT_AT_START;
1790 
1791       if (p_state_enhaacplus_dec->dwnsmp_signal == 1 &&
1792           p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_ELD)
1793         p_obj_exhaacplus_dec->aac_config.down_sample_flag = 1;
1794 
1795       if (p_state_enhaacplus_dec->sampling_rate ==
1796           p_state_enhaacplus_dec->extension_samp_rate) {
1797         p_obj_exhaacplus_dec->aac_config.down_sample_flag = 1;
1798       }
1799 
1800     } else {
1801       p_obj_exhaacplus_dec->p_state_aac->header_dec_done = 1;
1802       p_state_enhaacplus_dec->i_bytes_consumed = 0;
1803 
1804       p_state_enhaacplus_dec->sampling_rate =
1805           p_obj_exhaacplus_dec->aac_config.ui_samp_freq;
1806 
1807       if (1 == p_obj_exhaacplus_dec->aac_config.ui_frame_size) {
1808         p_state_enhaacplus_dec->frame_len_flag = 1;
1809         p_state_enhaacplus_dec->frame_length = 960;
1810       } else {
1811         p_state_enhaacplus_dec->frame_len_flag = 0;
1812         p_state_enhaacplus_dec->frame_length = 1024;
1813       }
1814     }
1815 
1816     p_state_enhaacplus_dec->pstr_bit_buf = ixheaacd_create_bit_buf(
1817         &p_state_enhaacplus_dec->str_bit_buf, (UWORD8 *)in_buffer,
1818         p_obj_exhaacplus_dec->p_mem_info_aac[IA_ENHAACPLUS_DEC_INPUT_IDX]
1819             .ui_size);
1820     p_state_enhaacplus_dec->pstr_bit_buf->xaac_jmp_buf =
1821         &(p_state_enhaacplus_dec->xaac_jmp_buf);
1822 
1823     p_state_enhaacplus_dec->ptr_bit_stream =
1824         p_state_enhaacplus_dec->pstr_bit_buf;
1825 
1826     if (p_state_enhaacplus_dec->s_adts_hdr_present) {
1827       if (p_obj_exhaacplus_dec->aac_config.ld_decoder == 1)
1828         p_state_enhaacplus_dec->audio_object_type = 23;
1829     }
1830 
1831     if ((p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_ELD) ||
1832         (p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_LD))
1833       if (p_state_enhaacplus_dec->s_adts_hdr_present) {
1834         if (p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_ELD) {
1835           p_state_enhaacplus_dec->eld_specific_config.ld_sbr_samp_rate = 1;
1836           p_state_enhaacplus_dec->eld_specific_config.ld_sbr_crc_flag = 0;
1837           p_state_enhaacplus_dec->eld_specific_config.ld_sbr_flag_present = 0;
1838 
1839           if (p_obj_exhaacplus_dec->aac_config.eld_sbr_present == 1) {
1840             p_state_enhaacplus_dec->eld_specific_config.ld_sbr_flag_present = 1;
1841           }
1842         }
1843         if (p_obj_exhaacplus_dec->aac_config.framesize_480)
1844           p_state_enhaacplus_dec->frame_length = 480;
1845         else
1846           p_state_enhaacplus_dec->frame_length = 512;
1847       }
1848 
1849     {
1850       for (ch_idx = 0; ch_idx < MAX_BS_ELEMENT; ch_idx++) {
1851         WORD32 channels;
1852         channels = 2;
1853 
1854         p_state_enhaacplus_dec->pstr_aac_dec_info[ch_idx] =
1855             ixheaacd_aac_decoder_init(
1856                 p_state_enhaacplus_dec,
1857 
1858                 p_state_enhaacplus_dec->pstr_stream_sbr[0], channels,
1859                 p_state_enhaacplus_dec->aac_persistent_mem_v,
1860                 p_state_enhaacplus_dec->frame_length);
1861 
1862         if (!p_state_enhaacplus_dec->pstr_aac_dec_info[ch_idx]) {
1863           p_state_enhaacplus_dec->i_bytes_consumed = 1;
1864           return IA_XHEAAC_DEC_INIT_FATAL_DEC_INIT_FAIL;
1865         }
1866       }
1867 
1868       {
1869         p_state_enhaacplus_dec->pstr_drc_dec =
1870             &p_state_enhaacplus_dec->str_drc_dec_info;
1871         ixheaacd_drc_dec_create(p_state_enhaacplus_dec->pstr_drc_dec, 127, 127);
1872       }
1873       p_state_enhaacplus_dec->pstr_drc_dec->cut_factor =
1874           p_obj_exhaacplus_dec->aac_config.ui_drc_cut;
1875       p_state_enhaacplus_dec->pstr_drc_dec->boost_factor =
1876           p_obj_exhaacplus_dec->aac_config.ui_drc_boost;
1877       p_state_enhaacplus_dec->pstr_drc_dec->target_ref_level =
1878           p_obj_exhaacplus_dec->aac_config.ui_drc_target_level;
1879       p_state_enhaacplus_dec->pstr_drc_dec->prog_ref_level =
1880           p_obj_exhaacplus_dec->aac_config.ui_drc_target_level;
1881 
1882       if (1 == p_obj_exhaacplus_dec->aac_config.ui_drc_set) {
1883         if (p_obj_exhaacplus_dec->aac_config.ui_drc_heavy_comp == 1) {
1884           p_state_enhaacplus_dec->pstr_drc_dec->drc_on = 1;
1885           p_state_enhaacplus_dec->pstr_drc_dec->heavy_mode = 1;
1886         } else {
1887           p_state_enhaacplus_dec->pstr_drc_dec->heavy_mode = 0;
1888           if (p_state_enhaacplus_dec->pstr_drc_dec->target_ref_level > 127)
1889             p_state_enhaacplus_dec->pstr_drc_dec->target_ref_level = 127;
1890           if (p_state_enhaacplus_dec->pstr_drc_dec->target_ref_level < 0) {
1891             if (p_state_enhaacplus_dec->pstr_drc_dec->cut_factor > 0 ||
1892                 p_state_enhaacplus_dec->pstr_drc_dec->boost_factor > 0)
1893               p_state_enhaacplus_dec->pstr_drc_dec->drc_on = 1;
1894             else
1895               p_state_enhaacplus_dec->pstr_drc_dec->drc_on = 0;
1896             p_state_enhaacplus_dec->pstr_drc_dec->drc_dig_norm = 0;
1897             p_state_enhaacplus_dec->pstr_drc_dec->target_ref_level = 108;
1898           } else {
1899             p_state_enhaacplus_dec->pstr_drc_dec->drc_on = 1;
1900             p_state_enhaacplus_dec->pstr_drc_dec->drc_dig_norm = 1;
1901           }
1902         }
1903       }
1904     }
1905   } else {
1906     struct ia_bit_buf_struct temp_bit_buff = {0};
1907     ia_adts_header_struct adts;
1908     struct ia_bit_buf_struct *it_bit_buff;
1909 
1910     WORD16 frame_size_2 = 0;
1911     WORD32 sample_rate_2 = 0;
1912     WORD32 sample_rate = 0;
1913     WORD type, i;
1914     WORD elements_number;
1915 
1916     memset(&adts, 0, sizeof(ia_adts_header_struct));
1917 
1918     for (i = 0; i < MAX_BS_ELEMENT + 1; i++) {
1919       p_obj_exhaacplus_dec->aac_config.element_type[i] = -1;
1920     }
1921 
1922     it_bit_buff = p_state_enhaacplus_dec->pstr_bit_buf;
1923 
1924     p_obj_exhaacplus_dec->aac_config.ui_sbr_mode = 0;
1925     p_obj_exhaacplus_dec->p_state_aac->ui_out_bytes = 0;
1926     p_state_enhaacplus_dec->ui_mps_out_bytes = 0;
1927     if (p_state_enhaacplus_dec->ui_in_bytes == 0) {
1928       p_state_enhaacplus_dec->i_bytes_consumed = 0;
1929       return IA_NO_ERROR;
1930     }
1931 
1932     if (p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_LD ||
1933         p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_ELD) {
1934       if (p_obj_exhaacplus_dec->aac_config.ui_mp4_flag)
1935         p_state_enhaacplus_dec->frame_size =
1936             p_state_enhaacplus_dec->ui_in_bytes;
1937     }
1938 
1939     ixheaacd_create_init_bit_buf(it_bit_buff, in_buffer,
1940                                  p_state_enhaacplus_dec->ui_in_bytes);
1941     p_state_enhaacplus_dec->pstr_bit_buf->xaac_jmp_buf =
1942         &(p_state_enhaacplus_dec->xaac_jmp_buf);
1943 
1944     it_bit_buff->adts_header_present =
1945         p_state_enhaacplus_dec->s_adts_hdr_present;
1946     it_bit_buff->no_raw_data_blocks =
1947         (WORD8)p_state_enhaacplus_dec->b_n_raw_data_blk;
1948     it_bit_buff->protection_absent = p_state_enhaacplus_dec->protection_absent;
1949 
1950     memcpy(&temp_bit_buff, it_bit_buff, sizeof(struct ia_bit_buf_struct));
1951 
1952     if (p_obj_exhaacplus_dec->aac_config.ui_max_channels > 2)
1953       elements_number = MAX_BS_ELEMENT;
1954     else
1955       elements_number = 2;
1956 
1957     for (i = 0; i < elements_number; i++)
1958       p_state_enhaacplus_dec->pstr_stream_sbr[i][0].no_elements = 0;
1959 
1960     { it_bit_buff->initial_cnt_bits = it_bit_buff->cnt_bits; }
1961 
1962     ixheaacd_byte_align(
1963         p_state_enhaacplus_dec->ptr_bit_stream,
1964         &p_state_enhaacplus_dec->pstr_aac_dec_info[0]->byte_align_bits);
1965 
1966     if (p_state_enhaacplus_dec->s_adts_hdr_present) {
1967       WORD32 error;
1968 
1969       if (p_state_enhaacplus_dec->b_n_raw_data_blk == 0) {
1970         error = ixheaacd_readifadts(p_state_enhaacplus_dec, it_bit_buff, &adts);
1971 
1972         if (error) return error;
1973 
1974         p_state_enhaacplus_dec->protection_absent = adts.protection_absent;
1975 
1976         if (p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_LD ||
1977             p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_ELD) {
1978           p_state_enhaacplus_dec->frame_size = adts.aac_frame_length;
1979           if (p_obj_exhaacplus_dec->aac_config.framesize_480)
1980             p_state_enhaacplus_dec->frame_length = 480;
1981           else
1982             p_state_enhaacplus_dec->frame_length = 512;
1983         }
1984       }
1985     }
1986 
1987     if (p_state_enhaacplus_dec->bs_format == LOAS_BSFORMAT) {
1988       WORD32 result;
1989       WORD32 sync;
1990       WORD32 cnt_bits;
1991 
1992       sync = ixheaacd_read_bits_buf(it_bit_buff, 11);
1993       cnt_bits = it_bit_buff->cnt_bits;
1994       if (it_bit_buff->cnt_bits <= 24) {
1995         return IA_XHEAAC_DEC_INIT_NONFATAL_INSUFFICIENT_INPUT_BYTES;
1996       }
1997 
1998       while (sync != 0x2b7) {
1999         sync = ((sync & 0x3ff) << 1) | ixheaacd_read_bits_buf(it_bit_buff, 1);
2000         if (it_bit_buff->cnt_bits < 11) {
2001           ixheaacd_read_bidirection(it_bit_buff, -11);
2002           p_state_enhaacplus_dec->i_bytes_consumed =
2003               (cnt_bits - it_bit_buff->cnt_bits) / 8;
2004           return (IA_XHEAAC_DEC_INIT_NONFATAL_HEADER_NOT_AT_START);
2005         }
2006       }
2007 
2008       it_bit_buff->audio_mux_align = it_bit_buff->cnt_bits - 13;
2009 
2010       if (sync == 0x2b7) {
2011         result = ixheaacd_latm_audio_mux_element(
2012             it_bit_buff, &p_state_enhaacplus_dec->latm_struct_element,
2013             p_state_enhaacplus_dec,
2014             (ia_sampling_rate_info_struct *)&p_obj_exhaacplus_dec->aac_tables
2015                 .pstr_huffmann_tables->str_sample_rate_info[0]);
2016         if (result < 0) {
2017           return result;
2018         }
2019       }
2020     }
2021 
2022     p_state_enhaacplus_dec->pstr_aac_dec_info[0]->byte_align_bits =
2023         it_bit_buff->cnt_bits;
2024 
2025     type = -1;
2026     ch_idx = 0;
2027 
2028     while ((type != 7)) {
2029       ia_aac_dec_scratch_struct aac_scratch_struct;
2030 
2031       if (ch_idx >= elements_number) {
2032         p_state_enhaacplus_dec->i_bytes_consumed = 1;
2033 
2034         return IA_XHEAAC_DEC_INIT_FATAL_STREAM_CHAN_GT_MAX;
2035       }
2036 
2037       ixheaacd_allocate_aac_scr(
2038           &aac_scratch_struct, p_state_enhaacplus_dec->aac_scratch_mem_v,
2039           time_data, 1, p_obj_exhaacplus_dec->aac_config.ui_max_channels,
2040           p_state_enhaacplus_dec->audio_object_type);
2041 
2042       p_state_enhaacplus_dec->pstr_aac_dec_info[ch_idx]->p_ind_channel_info =
2043           &p_state_enhaacplus_dec->ind_cc_info;
2044       if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
2045         p_obj_exhaacplus_dec->aac_config.first_frame = 1;
2046       }
2047 
2048       error_code = ixheaacd_aacdec_decodeframe(
2049           p_obj_exhaacplus_dec, &aac_scratch_struct, time_data, frame_status, &type, &ch_idx, 1,
2050           2, p_obj_exhaacplus_dec->aac_config.element_instance_order, 0, 1, 0,
2051           p_obj_exhaacplus_dec->aac_config.ui_max_channels, 2,
2052           p_obj_exhaacplus_dec->p_state_aac->frame_length,
2053           p_obj_exhaacplus_dec->p_state_aac->frame_size, p_state_enhaacplus_dec->pstr_drc_dec,
2054           p_state_enhaacplus_dec->audio_object_type, p_state_enhaacplus_dec->ch_config,
2055           p_state_enhaacplus_dec->eld_specific_config, p_state_enhaacplus_dec->s_adts_hdr_present,
2056           &p_state_enhaacplus_dec->drc_dummy, p_state_enhaacplus_dec->ldmps_present,
2057           &p_state_enhaacplus_dec->slot_pos, mps_buffer, &p_state_enhaacplus_dec->mps_header,
2058           &p_state_enhaacplus_dec->ui_mps_out_bytes, 1,
2059           p_obj_exhaacplus_dec->aac_config.first_frame);
2060 
2061       if (p_state_enhaacplus_dec->pstr_drc_dec->drc_element_found == 1) {
2062         if (p_obj_exhaacplus_dec->aac_config.i_loud_ref_level < 0) {
2063           p_obj_exhaacplus_dec->aac_config.output_level =
2064               p_state_enhaacplus_dec->pstr_drc_dec->prog_ref_level;
2065         } else {
2066           p_obj_exhaacplus_dec->aac_config.output_level =
2067               p_obj_exhaacplus_dec->aac_config.i_loud_ref_level;
2068         }
2069       }
2070 
2071       memset(&(p_obj_exhaacplus_dec->p_state_aac->pstr_aac_dec_info[ch_idx]
2072                    ->pstr_aac_dec_ch_info[0]
2073                    ->str_ics_info.ltp),
2074              0, sizeof(ltp_info));
2075       memset(&(p_obj_exhaacplus_dec->p_state_aac->pstr_aac_dec_info[ch_idx]
2076                    ->pstr_aac_dec_ch_info[0]
2077                    ->str_ics_info.ltp2),
2078              0, sizeof(ltp_info));
2079       memset(&(p_obj_exhaacplus_dec->p_state_aac->pstr_aac_dec_info[ch_idx]
2080                    ->pstr_aac_dec_ch_info[1]
2081                    ->str_ics_info.ltp),
2082              0, sizeof(ltp_info));
2083       memset(&(p_obj_exhaacplus_dec->p_state_aac->pstr_aac_dec_info[ch_idx]
2084                    ->pstr_aac_dec_ch_info[1]
2085                    ->str_ics_info.ltp2),
2086              0, sizeof(ltp_info));
2087 
2088       {
2089 
2090         frame_size_1 = p_state_enhaacplus_dec->frame_length;
2091         sample_rate_1 =
2092             p_state_enhaacplus_dec->pstr_aac_dec_info[ch_idx]->sampling_rate;
2093         num_channels_1 =
2094             p_state_enhaacplus_dec->pstr_aac_dec_info[ch_idx]->channels;
2095       }
2096 
2097       if ((p_obj_exhaacplus_dec->aac_config.ui_max_channels <= 2) &&
2098           (p_obj_exhaacplus_dec->aac_config.element_type[ch_idx] == 2)) {
2099         p_state_enhaacplus_dec->i_bytes_consumed = 1;
2100         return IA_XHEAAC_DEC_INIT_FATAL_UNIMPLEMENTED_CCE;
2101       }
2102 
2103       if (p_state_enhaacplus_dec->pstr_stream_sbr[0][0].no_elements) {
2104         sbr_present_flag = 1;
2105         p_obj_exhaacplus_dec->aac_config.ui_sbr_mode = 1;
2106       }
2107 
2108       if (error_code) {
2109         if (p_state_enhaacplus_dec->ui_input_over) {
2110           return IA_XHEAAC_DEC_INIT_FATAL_EO_INPUT_REACHED;
2111         }
2112 
2113         ixheaacd_updatebytesconsumed(p_state_enhaacplus_dec, it_bit_buff);
2114         return error_code;
2115       }
2116 
2117       if (p_state_enhaacplus_dec->s_adts_hdr_present) {
2118         if (adts.no_raw_data_blocks != 0) {
2119           if (adts.protection_absent == 0 && it_bit_buff->cnt_bits >= 16) {
2120             adts.crc_check = ixheaacd_read_bits_buf(it_bit_buff, 16);
2121           }
2122         }
2123         p_state_enhaacplus_dec->b_n_raw_data_blk--;
2124       }
2125 
2126       sample_rate_2 = sample_rate_1;
2127       frame_size_2 = frame_size_1;
2128 
2129       if (!p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx] &&
2130           p_state_enhaacplus_dec->pstr_stream_sbr[0][0].no_elements) {
2131         WORD32 harmonic_sbr_flag = 0;
2132         if ((p_obj_exhaacplus_dec->aac_config.flag_16khz_out == 1) &&
2133             (sample_rate_1 == 8000)) {
2134           p_obj_exhaacplus_dec->aac_config.flag_16khz_out = 0;
2135         }
2136 
2137         p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx] = ixheaacd_init_sbr(
2138             sample_rate_1, frame_size_1,
2139             (FLAG *)&p_obj_exhaacplus_dec->aac_config.down_sample_flag,
2140             p_state_enhaacplus_dec->sbr_persistent_mem_v,
2141             p_state_enhaacplus_dec->ptr_overlap_buf, MAXNRSBRCHANNELS, (WORD)1,
2142             1, frame_size_1 * 2, &harmonic_sbr_flag, NULL,
2143             p_state_enhaacplus_dec->str_sbr_config,
2144             p_state_enhaacplus_dec->audio_object_type,
2145             p_state_enhaacplus_dec->mps_dec_handle.ldmps_config
2146                 .ldmps_present_flag,
2147             p_state_enhaacplus_dec->mps_dec_handle.ldmps_config
2148                 .no_ldsbr_present);
2149         if (p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]) {
2150           p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->xaac_jmp_buf =
2151               &(p_state_enhaacplus_dec->xaac_jmp_buf);
2152         }
2153       } else {
2154       }
2155 
2156       if (p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx] &&
2157           p_state_enhaacplus_dec->pstr_stream_sbr[0][0].no_elements) {
2158         ia_sbr_scr_struct sbr_scratch_struct;
2159         WORD16 num_channels_1_t = num_channels_1;
2160         ixheaacd_allocate_sbr_scr(
2161             &sbr_scratch_struct, p_state_enhaacplus_dec->aac_scratch_mem_v,
2162             time_data, 1, 1, p_state_enhaacplus_dec->audio_object_type, 0, NULL,
2163             0, 0);
2164         {
2165           WORD32 audio_object_type = p_state_enhaacplus_dec->audio_object_type;
2166 
2167           if (audio_object_type != AOT_ER_AAC_ELD && audio_object_type != AOT_ER_AAC_LD) {
2168             WORD32 i = 0;
2169             ia_dec_data_struct *pstr_dec_data =
2170                 (ia_dec_data_struct *)p_state_enhaacplus_dec->pstr_dec_data;
2171             if (num_channels_1 == 1) {
2172               for (; i < 1024; i++) {
2173                 pstr_dec_data->str_usac_data.time_sample_vector[0][i] =
2174                     ((FLOAT32)time_data[i]);
2175               }
2176               p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->time_sample_buf[0] =
2177                   &pstr_dec_data->str_usac_data.time_sample_vector[0][0];
2178 
2179               p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->time_sample_buf[1] =
2180                   &pstr_dec_data->str_usac_data.time_sample_vector[1][0];
2181             } else if (num_channels_1 == 2) {
2182               for (; i < 1024; i++) {
2183                 pstr_dec_data->str_usac_data.time_sample_vector[0][i] =
2184                     ((FLOAT32)time_data[2 * i + 0]);
2185                 pstr_dec_data->str_usac_data.time_sample_vector[1][i] =
2186                     ((FLOAT32)time_data[2 * i + 1]);
2187               }
2188               p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->time_sample_buf[0] =
2189                   &pstr_dec_data->str_usac_data.time_sample_vector[0][0];
2190 
2191               p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->time_sample_buf[1] =
2192                   &pstr_dec_data->str_usac_data.time_sample_vector[1][0];
2193             }
2194           }
2195         }
2196         p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->ec_flag =
2197             p_obj_exhaacplus_dec->aac_config.ui_err_conceal;
2198         p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->esbr_hq =
2199             p_obj_exhaacplus_dec->aac_config.ui_hq_esbr;
2200         p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->enh_sbr =
2201             p_obj_exhaacplus_dec->aac_config.ui_enh_sbr;
2202         p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->enh_sbr_ps =
2203             p_obj_exhaacplus_dec->aac_config.ui_enh_sbr_ps;
2204 
2205         if (p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]) {
2206           p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->xaac_jmp_buf =
2207               &(p_state_enhaacplus_dec->xaac_jmp_buf);
2208         }
2209 
2210         if (ixheaacd_applysbr(p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx],
2211                               &p_state_enhaacplus_dec->pstr_stream_sbr[0][0], time_data,
2212                               &num_channels_1, frame_status,
2213                               p_obj_exhaacplus_dec->aac_config.down_sample_flag, 0,
2214                               &sbr_scratch_struct, 1, 1, 0, NULL, NULL,
2215                               p_state_enhaacplus_dec->eld_specific_config.ld_sbr_flag_present,
2216                               p_state_enhaacplus_dec->audio_object_type, 1,
2217                               p_state_enhaacplus_dec->ldmps_present, frame_size_1,
2218                               p_state_enhaacplus_dec->heaac_mps_handle.heaac_mps_present,
2219                               p_obj_exhaacplus_dec->aac_config.ui_err_conceal,
2220                               p_obj_exhaacplus_dec->aac_config.first_frame) != SBRDEC_OK) {
2221           p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx] = 0;
2222           return -1;
2223         } else {
2224           if (!p_obj_exhaacplus_dec->aac_config.down_sample_flag) {
2225             sample_rate_1 *= 2;
2226           }
2227           if (p_state_enhaacplus_dec->eld_specific_config.ld_sbr_flag_present == 1) {
2228             p_obj_exhaacplus_dec->p_state_aac->mps_dec_handle.pre_mix_req = 1;
2229             ixheaacd_mps_payload(
2230               p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx],
2231               p_obj_exhaacplus_dec);
2232           }
2233         }
2234         {
2235           WORD32 audio_object_type = p_state_enhaacplus_dec->audio_object_type;
2236 
2237           if (audio_object_type != AOT_ER_AAC_ELD && audio_object_type != AOT_ER_AAC_LD) {
2238             WORD32 out_bytes = 0;
2239             ia_dec_data_struct *pstr_dec_data =
2240                 (ia_dec_data_struct *)p_state_enhaacplus_dec->pstr_dec_data;
2241             ixheaacd_samples_sat((WORD8 *)time_data, 2048, 16,
2242                                  pstr_dec_data->str_usac_data.time_sample_vector, &out_bytes, 1);
2243           }
2244         }
2245         if (p_obj_exhaacplus_dec->aac_config.flag_downmix) {
2246           num_channels_1 = 1;
2247         }
2248         if (num_channels_1_t == 1 && num_channels_1 == 2) ps_detected = 1;
2249       } else {
2250         p_state_enhaacplus_dec->mps_dec_handle.ldmps_config.no_ldsbr_present = 1;
2251       }
2252 
2253       p_state_enhaacplus_dec->i_bytes_consumed = 0;
2254       p_state_enhaacplus_dec->pstr_bit_buf = it_bit_buff;
2255 
2256       {
2257         p_state_enhaacplus_dec->pstr_aac_dec_info[ch_idx] =
2258             ixheaacd_aac_decoder_init(
2259                 p_state_enhaacplus_dec,
2260                 p_state_enhaacplus_dec->pstr_stream_sbr[0], 2,
2261                 p_state_enhaacplus_dec->aac_persistent_mem_v,
2262                 p_state_enhaacplus_dec->frame_length);
2263 
2264         if (!p_state_enhaacplus_dec->pstr_aac_dec_info[ch_idx]) {
2265           p_state_enhaacplus_dec->i_bytes_consumed = 1;
2266           return IA_XHEAAC_DEC_INIT_FATAL_DEC_INIT_FAIL;
2267         }
2268 
2269         if (p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]) {
2270           WORD32 harmonic_sbr_flag = 0;
2271           p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx] = ixheaacd_init_sbr(
2272               sample_rate_2, frame_size_2,
2273               (FLAG *)&p_obj_exhaacplus_dec->aac_config.down_sample_flag,
2274               p_state_enhaacplus_dec->sbr_persistent_mem_v,
2275               p_state_enhaacplus_dec->ptr_overlap_buf, MAXNRSBRCHANNELS, 1, 1,
2276               frame_size_2 * 2, &harmonic_sbr_flag, NULL,
2277               p_state_enhaacplus_dec->str_sbr_config,
2278               p_state_enhaacplus_dec->audio_object_type,
2279               p_state_enhaacplus_dec->mps_dec_handle.ldmps_config
2280                 .ldmps_present_flag,
2281               p_state_enhaacplus_dec->mps_dec_handle.ldmps_config
2282                 .no_ldsbr_present);
2283         }
2284         if (p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]) {
2285           p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->xaac_jmp_buf =
2286               &(p_state_enhaacplus_dec->xaac_jmp_buf);
2287         }
2288       }
2289 
2290       if (sample_rate < sample_rate_1) sample_rate = sample_rate_1;
2291 
2292       ch_idx++;
2293 
2294       if (p_state_enhaacplus_dec->audio_object_type >= ER_OBJECT_START &&
2295           (p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_ELD ||
2296           p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_LD ||
2297           p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_LC))
2298         break;
2299     }
2300 
2301     {
2302       ia_adts_crc_info_struct *ptr_adts_crc_info =
2303           p_state_enhaacplus_dec->ptr_bit_stream->pstr_adts_crc_info;
2304       if (ptr_adts_crc_info->crc_active == 1) {
2305         if ((error_code = ixheaacd_adts_crc_check_crc(ptr_adts_crc_info))) {
2306           return error_code;
2307         }
2308       }
2309     }
2310 
2311     {
2312       VOID *temp;
2313       WORD prev_persistent_used_t;
2314       WORD prev_sbrpersistent_used_t;
2315       WORD ps_enable;
2316       WORD ch_idx_err = 0;
2317       WORD persistent_used_t = 0;
2318       WORD channel_check = 0;
2319       WORD cc_channel_check = 0;
2320       WORD max_ch_num = p_obj_exhaacplus_dec->aac_config.ui_max_channels;
2321       WORD32 harmonic_sbr_flag = 0;
2322       i = 0;
2323 
2324       p_obj_exhaacplus_dec->aac_config.ui_n_channels = ch_idx;
2325       while (p_obj_exhaacplus_dec->aac_config.element_type[ch_idx_err] <= 3 &&
2326              p_obj_exhaacplus_dec->aac_config.element_type[ch_idx_err] >= 0) {
2327         ch_idx_err++;
2328       }
2329 
2330       if (ch_idx_err == 0) {
2331         p_obj_exhaacplus_dec->p_state_aac->header_dec_done = 0;
2332         p_state_enhaacplus_dec->i_bytes_consumed = (WORD32)(
2333             it_bit_buff->ptr_read_next - it_bit_buff->ptr_bit_buf_base);
2334         return IA_XHEAAC_DEC_INIT_NONFATAL_DECODE_FRAME_ERROR;
2335       }
2336 
2337       if (ch_idx_err == 1)
2338         ps_enable = 1;
2339       else
2340         ps_enable = 0;
2341 
2342       while (p_obj_exhaacplus_dec->aac_config.element_type[i] >= 0 &&
2343              p_obj_exhaacplus_dec->aac_config.element_type[i] <= 3) {
2344         WORD32 channel = 0;
2345         switch (p_obj_exhaacplus_dec->aac_config.element_type[i]) {
2346           case 0:
2347           case 3:
2348             channel = 1;
2349             break;
2350           case 1:
2351             channel = 2;
2352             break;
2353           case 2:
2354             if (max_ch_num > 2) {
2355               if (p_obj_exhaacplus_dec->aac_config.element_instance_order[i] !=
2356                   p_obj_exhaacplus_dec->aac_config.ui_coupling_channel) {
2357                 i++;
2358                 continue;
2359               }
2360               channel = 1;
2361             } else {
2362               i++;
2363               continue;
2364             }
2365             cc_channel_check++;
2366             break;
2367           default:
2368             return -1;
2369         }
2370 
2371         if (cc_channel_check > MAX_CC_CHANNEL_NUM)
2372           return IA_XHEAAC_DEC_EXE_FATAL_UNIMPLEMENTED_CCE;
2373         if (ps_enable == 1) {
2374           channel = 2;
2375         }
2376 
2377         if (p_obj_exhaacplus_dec->aac_config.element_type[i] != 2) {
2378           channel_check += channel;
2379         }
2380 
2381         if (channel_check > max_ch_num) {
2382           p_state_enhaacplus_dec->i_bytes_consumed = 1;
2383           return IA_XHEAAC_DEC_INIT_FATAL_STREAM_CHAN_GT_MAX;
2384         }
2385 
2386         temp = p_state_enhaacplus_dec->aac_persistent_mem_v;
2387 
2388         prev_persistent_used_t = persistent_used_t;
2389 
2390         ixheaacd_allocate_mem_persistent(
2391             p_obj_exhaacplus_dec, p_state_enhaacplus_dec, channel,
2392             &persistent_used_t, &prev_sbrpersistent_used_t, ps_enable);
2393 
2394         p_state_enhaacplus_dec->aac_persistent_mem_v = temp;
2395         p_state_enhaacplus_dec->last_frame_ok = 1;
2396 
2397         p_state_enhaacplus_dec->num_channel_last = 0;
2398         p_state_enhaacplus_dec->ui_init_done = 0;
2399         p_state_enhaacplus_dec->ui_input_over = 0;
2400         p_state_enhaacplus_dec->ptr_bit_stream =
2401             p_state_enhaacplus_dec->pstr_bit_buf;
2402 
2403         p_state_enhaacplus_dec->pstr_aac_dec_info[i] = 0;
2404 
2405         p_state_enhaacplus_dec->pstr_aac_dec_info[i] =
2406             ixheaacd_aac_decoder_init(
2407                 p_state_enhaacplus_dec,
2408                 p_state_enhaacplus_dec->pstr_stream_sbr[i], channel,
2409                 (WORD8 *)p_state_enhaacplus_dec->aac_persistent_mem_v +
2410                     prev_persistent_used_t,
2411                 p_state_enhaacplus_dec->frame_length);
2412 
2413         if (!p_state_enhaacplus_dec->pstr_aac_dec_info[i]) {
2414           p_state_enhaacplus_dec->i_bytes_consumed = 1;
2415           return IA_XHEAAC_DEC_INIT_FATAL_DEC_INIT_FAIL;
2416         }
2417 
2418         p_state_enhaacplus_dec->str_sbr_dec_info[i] = ixheaacd_init_sbr(
2419             sample_rate_2, frame_size_2,
2420             (FLAG *)&p_obj_exhaacplus_dec->aac_config.down_sample_flag,
2421             p_state_enhaacplus_dec->sbr_persistent_mem_v,
2422             p_state_enhaacplus_dec->ptr_overlap_buf, channel, ps_enable, 1,
2423             frame_size_2 * 2, &harmonic_sbr_flag, NULL,
2424             p_state_enhaacplus_dec->str_sbr_config,
2425             p_state_enhaacplus_dec->audio_object_type,
2426             p_state_enhaacplus_dec->mps_dec_handle.ldmps_config
2427                 .ldmps_present_flag,
2428             p_state_enhaacplus_dec->mps_dec_handle.ldmps_config
2429                 .no_ldsbr_present);
2430         if (p_state_enhaacplus_dec->str_sbr_dec_info[i]) {
2431           p_state_enhaacplus_dec->str_sbr_dec_info[i]->xaac_jmp_buf =
2432               &(p_state_enhaacplus_dec->xaac_jmp_buf);
2433         }
2434         if ((sbr_present_flag &&
2435             ((p_obj_exhaacplus_dec->p_state_aac->audio_object_type ==
2436               AOT_AAC_LC) ||
2437              (p_obj_exhaacplus_dec->p_state_aac->audio_object_type ==
2438               AOT_SBR) ||
2439              (p_obj_exhaacplus_dec->p_state_aac->audio_object_type ==
2440                AOT_PS))) ||
2441             ((p_state_enhaacplus_dec->mps_dec_handle.ldmps_config
2442                   .ldmps_present_flag == 1) &&
2443              (p_obj_exhaacplus_dec->p_state_aac->audio_object_type ==
2444               AOT_ER_AAC_ELD)))
2445           p_obj_exhaacplus_dec->aac_config.flag_to_stereo = 1;
2446         if (p_state_enhaacplus_dec->mps_dec_handle.ldmps_config
2447                 .ldmps_present_flag == 1) {
2448           copy_qmf_to_ldmps(&p_obj_exhaacplus_dec->p_state_aac->mps_dec_handle,
2449                             p_state_enhaacplus_dec->sbr_persistent_mem_v);
2450         }
2451         if (p_state_enhaacplus_dec->audio_object_type == AOT_AAC_LC &&
2452             p_state_enhaacplus_dec->ui_mps_out_bytes != 0) {
2453           p_state_enhaacplus_dec->heaac_mps_handle.heaac_mps_present = 1;
2454           if (p_state_enhaacplus_dec->pstr_stream_sbr[0][0].no_elements) {
2455             p_obj_exhaacplus_dec->p_state_aac->heaac_mps_handle.mps_with_sbr = 1;
2456           }
2457           error_code =
2458               ixheaacd_aac_mps_init(p_obj_exhaacplus_dec, mps_buffer,
2459                                     p_state_enhaacplus_dec->ui_mps_out_bytes, sample_rate_1);
2460           if (error_code) return error_code;
2461           p_obj_exhaacplus_dec->aac_config.ui_n_channels =
2462               p_state_enhaacplus_dec->heaac_mps_handle.num_output_channels_at;
2463           if (p_obj_exhaacplus_dec->p_state_aac->heaac_mps_handle.mps_with_sbr == 1) {
2464             p_obj_exhaacplus_dec->aac_config.ui_sbr_mode = 1;
2465           }
2466           if (p_obj_exhaacplus_dec->aac_config.element_type[i + 1] >= 0 &&
2467               p_obj_exhaacplus_dec->aac_config.element_type[i + 1] <= 3) {
2468             return IA_FATAL_ERROR;
2469           }
2470           break;
2471         }
2472         i++;
2473       }
2474       p_state_enhaacplus_dec->pers_mem_ptr =
2475           (WORD8 *)p_state_enhaacplus_dec->aac_persistent_mem_v +
2476           persistent_used_t;
2477 
2478       p_obj_exhaacplus_dec->aac_config.i_channel_mask =
2479           ixheaacd_get_channel_mask(p_obj_exhaacplus_dec);
2480 
2481       {
2482         num_channels_1 = 0;
2483         ch_idx = 0;
2484         while (p_obj_exhaacplus_dec->aac_config.element_type[ch_idx] >= 0 &&
2485                p_obj_exhaacplus_dec->aac_config.element_type[ch_idx] <= 3) {
2486           if (p_obj_exhaacplus_dec->aac_config.element_type[ch_idx] == 0 ||
2487               p_obj_exhaacplus_dec->aac_config.element_type[ch_idx] == 3)
2488             num_channels_1 += 1;
2489           if (p_obj_exhaacplus_dec->aac_config.element_type[ch_idx] == 1)
2490             num_channels_1 += 2;
2491           ch_idx++;
2492         }
2493 
2494         if (ch_idx == 2 && num_channels_1 == 2) {
2495           p_obj_exhaacplus_dec->aac_config.ui_channel_mode = 2;
2496         }
2497         if (ch_idx == 1) {
2498           if (num_channels_1 == 1)
2499             p_obj_exhaacplus_dec->aac_config.ui_channel_mode = 0;
2500           if (num_channels_1 == 2)
2501             p_obj_exhaacplus_dec->aac_config.ui_channel_mode = 1;
2502         }
2503 
2504         if (ps_detected == 1 && num_channels_1 == 1) num_channels_1 = 2;
2505       }
2506     }
2507     if (1 == p_obj_exhaacplus_dec->aac_config.downmix) num_channels_1 = 2;
2508 
2509     if (p_obj_exhaacplus_dec->aac_config.flag_downmix == 1) {
2510       num_channels_1 = 1;
2511     }
2512 
2513     if ((p_obj_exhaacplus_dec->aac_config.flag_to_stereo == 1) &&
2514         (ch_idx == 1 || num_channels_1 <= 2)) {
2515       num_channels_1 = 2;
2516     }
2517 
2518     p_obj_exhaacplus_dec->aac_config.ui_n_channels = num_channels_1;
2519     p_obj_exhaacplus_dec->aac_config.ui_samp_freq = sample_rate;
2520     p_state_enhaacplus_dec->ui_init_done = 1;
2521 
2522     memcpy(it_bit_buff, &temp_bit_buff, sizeof(struct ia_bit_buf_struct));
2523 
2524     p_state_enhaacplus_dec->b_n_raw_data_blk = 0;
2525 
2526     if (p_obj_exhaacplus_dec->p_state_aac->header_dec_done == 1) {
2527       p_obj_exhaacplus_dec->p_state_aac->header_dec_done = 0;
2528     }
2529   }
2530   return err_code;
2531 }
2532 
ixheaacd_fill_slot_order(ia_aac_dec_state_struct * p_state_enhaacplus_dec,WORD32 ch,WORD8 * ptr_is_cpe,WORD8 * ptr_tag_select,WORD32 * ptr_idx_no)2533 VOID ixheaacd_fill_slot_order(ia_aac_dec_state_struct *p_state_enhaacplus_dec,
2534                               WORD32 ch, WORD8 *ptr_is_cpe,
2535                               WORD8 *ptr_tag_select, WORD32 *ptr_idx_no) {
2536   WORD32 i;
2537   WORD32 idx_no = *ptr_idx_no;
2538   WORD *p_slot_element = p_state_enhaacplus_dec->p_config->slot_element;
2539   WORD *p_element_type = p_state_enhaacplus_dec->p_config->element_type;
2540   WORD *p_element_instance_order =
2541       p_state_enhaacplus_dec->p_config->element_instance_order;
2542 
2543   for (i = 0; i < ch; i++) {
2544     if (ptr_is_cpe[i] == 0) {
2545       *p_slot_element++ = idx_no++;
2546       *p_element_type++ = 0;
2547       *p_element_instance_order++ = ptr_tag_select[i];
2548     }
2549   }
2550   *ptr_idx_no = idx_no;
2551 }
2552 
ixheaacd_fill_prog_config_slots(ia_aac_dec_state_struct * p_state_enhaacplus_dec)2553 VOID ixheaacd_fill_prog_config_slots(
2554     ia_aac_dec_state_struct *p_state_enhaacplus_dec) {
2555   WORD32 idx_no = 0;
2556 
2557   ixheaacd_fill_slot_order(
2558       p_state_enhaacplus_dec, p_state_enhaacplus_dec->p_config->str_prog_config
2559                                   .num_front_channel_elements,
2560       p_state_enhaacplus_dec->p_config->str_prog_config.front_element_is_cpe,
2561       p_state_enhaacplus_dec->p_config->str_prog_config
2562           .front_element_tag_select,
2563       &idx_no);
2564 
2565   ixheaacd_fill_slot_order(
2566       p_state_enhaacplus_dec, p_state_enhaacplus_dec->p_config->str_prog_config
2567                                   .num_side_channel_elements,
2568       p_state_enhaacplus_dec->p_config->str_prog_config.side_element_is_cpe,
2569       p_state_enhaacplus_dec->p_config->str_prog_config.side_element_tag_select,
2570       &idx_no);
2571 
2572   ixheaacd_fill_slot_order(
2573       p_state_enhaacplus_dec, p_state_enhaacplus_dec->p_config->str_prog_config
2574                                   .num_back_channel_elements,
2575       p_state_enhaacplus_dec->p_config->str_prog_config.back_element_is_cpe,
2576       p_state_enhaacplus_dec->p_config->str_prog_config.back_element_tag_select,
2577       &idx_no);
2578 }
2579 
ixheaacd_dec_execute(ia_exhaacplus_dec_api_struct * p_obj_exhaacplus_dec)2580 IA_ERRORCODE ixheaacd_dec_execute(
2581     ia_exhaacplus_dec_api_struct *p_obj_exhaacplus_dec) {
2582   ia_adts_header_struct adts = {0};
2583   ia_aac_dec_state_struct *p_state_enhaacplus_dec;
2584 
2585   UWORD8 *in_buffer;
2586   WORD16 *time_data;
2587   WORD16 num_of_out_samples = 0;
2588   WORD16 frame_size = 0;
2589   WORD32 sample_rate_dec = 0;
2590   WORD32 sample_rate = 0;
2591   WORD16 num_ch = 0;
2592   struct ia_bit_buf_struct *it_bit_buff;
2593   UWORD8 *mps_buffer;
2594   WORD32 error_code = IA_NO_ERROR;
2595   WORD ch_idx1;
2596   WORD type;
2597   WORD total_channels = 0;
2598   WORD total_elements = 0;
2599   WORD16 *actual_out_buffer = NULL;
2600   WORD ps_enable;
2601   WORD esbr_mono_downmix = 0;
2602   WORD8 element_used[MAX_BS_ELEMENT];
2603   WORD32 channel_coupling_flag = 0;
2604 
2605   SIZE_T bytes_for_sync;
2606   WORD32 audio_mux_length_bytes_last = 0;
2607   WORD32 ret_val;
2608   WORD32 mps_out_samples;
2609 
2610   p_obj_exhaacplus_dec->aac_config.ui_sbr_mode = 0;
2611   p_obj_exhaacplus_dec->aac_config.frame_status = 1;
2612 
2613   if (p_obj_exhaacplus_dec->p_state_aac != NULL) {
2614     ret_val = setjmp(p_obj_exhaacplus_dec->p_state_aac->xaac_jmp_buf);
2615     if (ret_val != 0) {
2616       p_obj_exhaacplus_dec->p_state_aac->i_bytes_consumed =
2617           p_obj_exhaacplus_dec->p_state_aac->ui_in_bytes;
2618       p_obj_exhaacplus_dec->p_state_aac->ui_out_bytes = 0;
2619       p_obj_exhaacplus_dec->aac_config.frame_status = 0;
2620       return IA_NO_ERROR;
2621     }
2622   }
2623 
2624   time_data = (WORD16 *)(p_obj_exhaacplus_dec
2625                              ->pp_mem_aac[IA_ENHAACPLUS_DEC_OUTPUT_IDX]);
2626   in_buffer = p_obj_exhaacplus_dec->pp_mem_aac[IA_ENHAACPLUS_DEC_INPUT_IDX];
2627   p_state_enhaacplus_dec = p_obj_exhaacplus_dec->p_state_aac;
2628   p_state_enhaacplus_dec->aac_scratch_mem_v =
2629       p_obj_exhaacplus_dec->pp_mem_aac[IA_ENHAACPLUS_DEC_SCRATCH_IDX];
2630   p_state_enhaacplus_dec->mps_header = -1;
2631   mps_buffer = p_state_enhaacplus_dec->mps_buffer;
2632   it_bit_buff = p_state_enhaacplus_dec->pstr_bit_buf;
2633 
2634   ch_idx1 = 0;
2635   p_state_enhaacplus_dec->i_bytes_consumed = 0;
2636 
2637   if (p_state_enhaacplus_dec->audio_object_type == AOT_USAC) {
2638     WORD32 pcm_size = p_obj_exhaacplus_dec->aac_config.ui_pcm_wdsz;
2639     WORD8 *inbuffer = (WORD8 *)(p_obj_exhaacplus_dec
2640                                     ->pp_mem_aac[IA_ENHAACPLUS_DEC_INPUT_IDX]);
2641     WORD8 *outbuffer =
2642         (WORD8 *)(p_obj_exhaacplus_dec
2643                       ->pp_mem_aac[IA_ENHAACPLUS_DEC_OUTPUT_IDX]);
2644     WORD32 out_bytes = 0;
2645 
2646     WORD32 frames_done = p_obj_exhaacplus_dec->p_state_aac->frame_counter;
2647 
2648     ia_dec_data_struct *pstr_dec_data =
2649         (ia_dec_data_struct *)(p_obj_exhaacplus_dec->p_state_aac
2650                                    ->pstr_dec_data);
2651 
2652     if (pstr_dec_data->str_usac_data.down_samp_sbr != 0) return IA_FATAL_ERROR;
2653 
2654     if (p_obj_exhaacplus_dec->p_state_aac->ui_input_over == 0) {
2655       ia_audio_specific_config_struct *ptr_audio_specific_config =
2656           ((ia_audio_specific_config_struct *)
2657                p_obj_exhaacplus_dec->p_state_aac->ia_audio_specific_config);
2658 
2659       ptr_audio_specific_config->str_usac_config.str_usac_dec_config
2660           .preroll_counter = 0;
2661       {
2662         WORD32 iii = 0;
2663         for (iii = 0; iii < (MAX_AUDIO_PREROLLS + 1); iii++) {
2664           ((ia_dec_data_struct *)(p_obj_exhaacplus_dec->p_state_aac
2665                                       ->pstr_dec_data))
2666               ->str_frame_data.str_audio_specific_config.str_usac_config
2667               .str_usac_dec_config.usac_ext_gain_payload_len[iii] = 0;
2668           ptr_audio_specific_config->str_usac_config.str_usac_dec_config
2669               .usac_ext_gain_payload_len[iii] =
2670               0;  // reinitilize the payload len buff
2671           ptr_audio_specific_config->str_usac_config.str_usac_dec_config
2672               .preroll_bytes[iii] = 0;
2673         }
2674       }
2675 
2676       ((ia_dec_data_struct *)(p_obj_exhaacplus_dec->p_state_aac->pstr_dec_data))
2677           ->str_frame_data.str_audio_specific_config.str_usac_config
2678           .str_usac_dec_config.preroll_counter = 0;
2679       error_code = ixheaacd_dec_main(
2680           p_obj_exhaacplus_dec, inbuffer, outbuffer, &out_bytes, frames_done,
2681           pcm_size, &p_obj_exhaacplus_dec->p_state_aac->num_of_output_ch);
2682       if (error_code) {
2683         if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
2684           p_obj_exhaacplus_dec->aac_config.frame_status = 0;
2685         } else {
2686           return error_code;
2687         }
2688       }
2689       p_obj_exhaacplus_dec->p_state_aac->frame_counter++;
2690     } else {
2691       out_bytes = 0;
2692     }
2693 
2694     if (pstr_dec_data->str_usac_data.ec_flag == 0) {
2695       if (p_state_enhaacplus_dec->bs_format != LOAS_BSFORMAT) {
2696         p_obj_exhaacplus_dec->p_state_aac->i_bytes_consumed =
2697             p_obj_exhaacplus_dec->p_state_aac->ui_in_bytes;
2698       }
2699     } else {
2700       if (p_state_enhaacplus_dec->bs_format != LOAS_BSFORMAT) {
2701         if (pstr_dec_data->str_usac_data.frame_ok == 0) {
2702           p_obj_exhaacplus_dec->p_state_aac->i_bytes_consumed =
2703               p_obj_exhaacplus_dec->p_state_aac->ui_in_bytes;
2704           pstr_dec_data->dec_bit_buf.cnt_bits = 0;
2705         } else {
2706           ia_dec_data_struct *pstr_dec_data =
2707               (ia_dec_data_struct *)p_obj_exhaacplus_dec->p_state_aac->pstr_dec_data;
2708 
2709           if (pstr_dec_data->dec_bit_buf.cnt_bits & 7) {
2710             pstr_dec_data->dec_bit_buf.cnt_bits -= (pstr_dec_data->dec_bit_buf.cnt_bits & 7);
2711           }
2712           if (pstr_dec_data->dec_bit_buf.cnt_bits == 0) {
2713             p_obj_exhaacplus_dec->p_state_aac->i_bytes_consumed =
2714                 p_obj_exhaacplus_dec->p_state_aac->ui_in_bytes;
2715           } else {
2716             p_obj_exhaacplus_dec->p_state_aac->i_bytes_consumed =
2717                 p_obj_exhaacplus_dec->p_state_aac->ui_in_bytes -
2718                 (pstr_dec_data->dec_bit_buf.cnt_bits >> 3);
2719           }
2720         }
2721       }
2722     }
2723 
2724     p_obj_exhaacplus_dec->p_state_aac->ui_out_bytes = out_bytes;
2725     p_obj_exhaacplus_dec->aac_config.ui_n_channels =
2726         p_obj_exhaacplus_dec->p_state_aac->num_of_output_ch;
2727     pstr_dec_data->str_usac_data.sbr_parse_err_flag = 0;
2728 
2729     return 0;
2730   }
2731 
2732   while (p_obj_exhaacplus_dec->aac_config.element_type[ch_idx1] <= 3 &&
2733          p_obj_exhaacplus_dec->aac_config.element_type[ch_idx1] >= 0) {
2734     if (p_obj_exhaacplus_dec->aac_config.element_type[ch_idx1] == 0 ||
2735         p_obj_exhaacplus_dec->aac_config.element_type[ch_idx1] == 3) {
2736       total_channels += 1;
2737       total_elements += 1;
2738     }
2739     if (p_obj_exhaacplus_dec->aac_config.element_type[ch_idx1] == 1) {
2740       total_elements += 1;
2741       total_channels += 2;
2742     }
2743     if (p_obj_exhaacplus_dec->aac_config.element_type[ch_idx1] == 2) {
2744       total_elements += 1;
2745     }
2746 
2747     ch_idx1++;
2748     if (ch_idx1 > MAX_BS_ELEMENT) {
2749       if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal)
2750         break;
2751       else
2752         return IA_FATAL_ERROR;
2753     }
2754   }
2755 
2756   if (ch_idx1 != 1) {
2757     ps_enable = 0;
2758     if (p_obj_exhaacplus_dec->aac_config.ui_max_channels > 2) {
2759       WORD32 scratch_pointer;
2760 
2761       scratch_pointer = 12 * 1024;
2762 
2763       p_state_enhaacplus_dec->coup_ch_output =
2764           (WORD32 *)((WORD8 *)
2765                          p_obj_exhaacplus_dec->p_state_aac->aac_scratch_mem_v +
2766                      scratch_pointer);
2767     }
2768 
2769   }
2770 
2771   else {
2772     if (total_channels < (WORD)p_obj_exhaacplus_dec->aac_config.ui_n_channels)
2773       total_channels = p_obj_exhaacplus_dec->aac_config.ui_n_channels;
2774     ps_enable = 1;
2775   }
2776 
2777   p_obj_exhaacplus_dec->p_state_aac->ui_out_bytes = 0;
2778   p_obj_exhaacplus_dec->p_state_aac->ui_mps_out_bytes = 0;
2779   if (p_state_enhaacplus_dec->ui_in_bytes == 0) {
2780     UWORD32 i;
2781     WORD32 j;
2782     if (p_state_enhaacplus_dec->peak_lim_init == 1) {
2783       p_obj_exhaacplus_dec->p_state_aac->ui_out_bytes =
2784           (p_state_enhaacplus_dec->peak_limiter.attack_time_samples) *
2785           total_channels * sizeof(WORD16);
2786 
2787       for (j = 0; j < total_channels; j++) {
2788         for (i = 0;
2789              i < (p_state_enhaacplus_dec->peak_limiter.attack_time_samples -
2790                   p_state_enhaacplus_dec->peak_limiter.delayed_input_index);
2791              i++) {
2792           *(time_data + total_channels * i + j) = ixheaacd_round16(
2793               (WORD32)*(p_state_enhaacplus_dec->peak_limiter.delayed_input +
2794                 (p_state_enhaacplus_dec->peak_limiter.delayed_input_index) *
2795                     total_channels +
2796                 total_channels * i + j));
2797         }
2798       }
2799 
2800       for (j = 0; j < total_channels; j++) {
2801         for (i = 0;
2802              i < p_state_enhaacplus_dec->peak_limiter.delayed_input_index;
2803              i++) {
2804           *(time_data +
2805             (p_state_enhaacplus_dec->peak_limiter.attack_time_samples -
2806              p_state_enhaacplus_dec->peak_limiter.delayed_input_index) *
2807                 total_channels +
2808             total_channels * i + j) =
2809               ixheaacd_round16(
2810                   (WORD32)*(p_state_enhaacplus_dec->peak_limiter.delayed_input +
2811                    total_channels * i + j));
2812         }
2813       }
2814 
2815       if (p_obj_exhaacplus_dec->aac_config.dup_stereo_flag) {
2816         for (i = 0;
2817              i < (p_state_enhaacplus_dec->peak_limiter.attack_time_samples);
2818              i++) {
2819           time_data[2 * i + 1] = time_data[2 * i + 0];
2820         }
2821       }
2822     } else {
2823       p_obj_exhaacplus_dec->p_state_aac->ui_out_bytes = 0;
2824     }
2825 
2826     p_state_enhaacplus_dec->i_bytes_consumed = 0;
2827     return IA_NO_ERROR;
2828   }
2829 
2830   if (ch_idx1 == 0) {
2831     p_state_enhaacplus_dec->i_bytes_consumed = 1;
2832     if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
2833       p_obj_exhaacplus_dec->aac_config.frame_status = 0;
2834     } else {
2835       return IA_XHEAAC_DEC_EXE_NONFATAL_DECODE_FRAME_ERROR;
2836     }
2837   }
2838   if (total_channels > (WORD)p_obj_exhaacplus_dec->aac_config.ui_max_channels) {
2839     p_state_enhaacplus_dec->i_bytes_consumed = 1;
2840     if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
2841       p_obj_exhaacplus_dec->aac_config.frame_status = 0;
2842     } else {
2843       return IA_XHEAAC_DEC_CONFIG_NONFATAL_INVALID_MAX_CHANNEL;
2844     }
2845   }
2846 
2847   if (p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_LD ||
2848       p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_ELD) {
2849     if (p_obj_exhaacplus_dec->aac_config.ui_mp4_flag)
2850       p_state_enhaacplus_dec->frame_size = p_state_enhaacplus_dec->ui_in_bytes;
2851   }
2852 
2853   if (p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_LC) {
2854     if (p_obj_exhaacplus_dec->aac_config.ui_mp4_flag)
2855       p_state_enhaacplus_dec->frame_size = 1024;
2856   }
2857 
2858   {
2859     ixheaacd_create_init_bit_buf(it_bit_buff, in_buffer,
2860                                  p_state_enhaacplus_dec->ui_in_bytes);
2861     it_bit_buff->xaac_jmp_buf = &(p_state_enhaacplus_dec->xaac_jmp_buf);
2862 
2863     it_bit_buff->adts_header_present =
2864         p_state_enhaacplus_dec->s_adts_hdr_present;
2865     it_bit_buff->no_raw_data_blocks =
2866         (WORD8)p_state_enhaacplus_dec->b_n_raw_data_blk;
2867     it_bit_buff->protection_absent = p_state_enhaacplus_dec->protection_absent;
2868 
2869     if (p_state_enhaacplus_dec->s_adts_hdr_present) {
2870       if (p_state_enhaacplus_dec->b_n_raw_data_blk == 0) {
2871         WORD32 error;
2872 
2873         error = ixheaacd_readifadts(p_state_enhaacplus_dec, it_bit_buff, &adts);
2874         if (error) {
2875           if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
2876             p_obj_exhaacplus_dec->aac_config.frame_status = 0;
2877             if (adts.samp_freq_index > 11) {
2878               adts.samp_freq_index = 11;
2879             }
2880           } else {
2881             return error;
2882           }
2883         }
2884 
2885         if ((WORD32)p_state_enhaacplus_dec->sampling_rate !=
2886             (WORD32)((p_obj_exhaacplus_dec->aac_tables.pstr_huffmann_tables
2887                           ->str_sample_rate_info[adts.samp_freq_index]
2888                           .sampling_frequency))) {
2889           p_state_enhaacplus_dec->i_bytes_consumed = 0;
2890           if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
2891             p_obj_exhaacplus_dec->aac_config.frame_status = 0;
2892           } else {
2893             return IA_XHEAAC_DEC_EXE_NONFATAL_CHANGED_ADTS_SF;
2894           }
2895         }
2896       }
2897     }
2898 
2899     bytes_for_sync = (SIZE_T)it_bit_buff->ptr_read_next;
2900 
2901     if (p_state_enhaacplus_dec->bs_format == LOAS_BSFORMAT) {
2902       WORD32 result, audio_mux_len_bytes_last;
2903       WORD32 cnt_bits = it_bit_buff->cnt_bits;
2904       WORD32 sync = ixheaacd_read_bits_buf(it_bit_buff, 11);
2905       UWORD32 curr_samp_rate = 0;
2906 
2907       if (p_state_enhaacplus_dec->latm_initialized)
2908         curr_samp_rate =
2909             p_state_enhaacplus_dec->latm_struct_element.layer_info[0][0]
2910                 .asc.sampling_freq;
2911 
2912       while (sync != 0x2b7) {
2913         sync = ((sync & 0x3ff) << 1) | ixheaacd_read_bits_buf(it_bit_buff, 1);
2914         if (it_bit_buff->cnt_bits < 13) {
2915           ixheaacd_read_bidirection(it_bit_buff, -11);
2916           p_state_enhaacplus_dec->i_bytes_consumed =
2917               (cnt_bits - it_bit_buff->cnt_bits) / 8;
2918 
2919           if (p_state_enhaacplus_dec->i_bytes_consumed == 0)
2920             p_state_enhaacplus_dec->i_bytes_consumed = 1;
2921           if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
2922             p_obj_exhaacplus_dec->aac_config.frame_status = 0;
2923           } else {
2924             return (IA_XHEAAC_DEC_INIT_NONFATAL_HEADER_NOT_AT_START);
2925           }
2926         }
2927       }
2928 
2929       it_bit_buff->audio_mux_align = it_bit_buff->cnt_bits - 13;
2930 
2931       audio_mux_len_bytes_last = ixheaacd_read_bits_buf(it_bit_buff, 13);
2932 
2933       audio_mux_length_bytes_last = audio_mux_len_bytes_last;
2934 
2935       bytes_for_sync = (SIZE_T)it_bit_buff->ptr_read_next - bytes_for_sync;
2936 
2937       if (it_bit_buff->cnt_bits < (audio_mux_len_bytes_last << 3)) {
2938         ixheaacd_read_bidirection(it_bit_buff, -(13 + 11));
2939         p_state_enhaacplus_dec->i_bytes_consumed = (cnt_bits - it_bit_buff->cnt_bits) / 8;
2940         if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
2941           p_obj_exhaacplus_dec->aac_config.frame_status = 0;
2942         } else {
2943           return IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES;
2944         }
2945       } else {
2946         ixheaacd_read_bidirection(it_bit_buff, -(13));
2947       }
2948 
2949       if (sync == 0x2b7) {
2950         result = ixheaacd_latm_audio_mux_element(
2951             it_bit_buff, &p_state_enhaacplus_dec->latm_struct_element,
2952             p_state_enhaacplus_dec,
2953             (ia_sampling_rate_info_struct *)&p_obj_exhaacplus_dec->aac_tables
2954                 .pstr_huffmann_tables->str_sample_rate_info[0]);
2955         if (result < 0) {
2956           if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
2957             p_obj_exhaacplus_dec->aac_config.frame_status = 0;
2958           } else {
2959             return result;
2960           }
2961         }
2962         if (!p_state_enhaacplus_dec->latm_initialized) {
2963           p_state_enhaacplus_dec->sampling_rate =
2964               p_state_enhaacplus_dec->latm_struct_element.layer_info[0][0].asc.sampling_freq;
2965           p_state_enhaacplus_dec->latm_initialized = 1;
2966         } else {
2967           if (p_state_enhaacplus_dec->sampling_rate != curr_samp_rate) {
2968             p_state_enhaacplus_dec->i_bytes_consumed = 0;
2969             if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
2970               p_obj_exhaacplus_dec->aac_config.frame_status = 0;
2971             } else {
2972               return IA_XHEAAC_DEC_EXE_NONFATAL_CHANGED_ADTS_SF;
2973             }
2974           }
2975         }
2976       }
2977     }
2978   }
2979 
2980   if (total_elements == 2 && total_channels == 2 &&
2981       (p_state_enhaacplus_dec->p_config->ui_pce_found_in_hdr == 1 ||
2982        p_state_enhaacplus_dec->p_config->ui_pce_found_in_hdr == 3)) {
2983     ixheaacd_fill_prog_config_slots(p_state_enhaacplus_dec);
2984   }
2985 
2986   memset(element_used, 0, sizeof(WORD8) * MAX_BS_ELEMENT);
2987 
2988   if (it_bit_buff->cnt_bits <= 0) {
2989     it_bit_buff->cnt_bits = -1;
2990     ixheaacd_updatebytesconsumed(p_state_enhaacplus_dec, it_bit_buff);
2991     if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
2992       p_obj_exhaacplus_dec->aac_config.frame_status = 0;
2993     } else {
2994       return (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
2995     }
2996   }
2997 
2998   { it_bit_buff->initial_cnt_bits = it_bit_buff->cnt_bits; }
2999 
3000   if (p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_LD) {
3001     if (p_state_enhaacplus_dec->s_adts_hdr_present)
3002       p_state_enhaacplus_dec->frame_size = adts.aac_frame_length;
3003   }
3004 
3005   if (p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_LC) {
3006     if (p_state_enhaacplus_dec->s_adts_hdr_present)
3007       p_state_enhaacplus_dec->frame_size = 1024;
3008   }
3009 
3010   if (p_state_enhaacplus_dec->pstr_drc_dec) {
3011     p_state_enhaacplus_dec->pstr_drc_dec->num_drc_elements = 0;
3012 
3013     p_state_enhaacplus_dec->pstr_drc_dec->state = 1;
3014   }
3015   if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
3016     if (total_elements > MAX_BS_ELEMENT) {
3017       total_elements = MAX_BS_ELEMENT;
3018     }
3019   }
3020 
3021   WORD16 *intermediate_scr =
3022       (WORD16 *)(WORD8 *)p_state_enhaacplus_dec->aac_scratch_mem_v +
3023       (128 * 1024);
3024 
3025   for (ch_idx1 = 0; ch_idx1 < total_elements; ch_idx1++) {
3026     WORD32 skip_full_decode = 0;
3027     WORD32 ch_idx = ch_idx1;
3028     WORD32 channel = 0;
3029     WORD ch_fac, slot_ele;
3030 
3031     if (p_state_enhaacplus_dec->audio_object_type < ER_OBJECT_START ||
3032         (p_state_enhaacplus_dec->audio_object_type != AOT_ER_AAC_LD &&
3033          p_state_enhaacplus_dec->audio_object_type != AOT_ER_AAC_ELD &&
3034          p_state_enhaacplus_dec->audio_object_type != AOT_ER_AAC_LC)) {
3035       jmp_buf local;
3036       ret_val = setjmp(local);
3037       if (ret_val == 0) {
3038         p_obj_exhaacplus_dec->p_state_aac->ptr_bit_stream->xaac_jmp_buf = &local;
3039         error_code = ixheaacd_get_element_index_tag(
3040             p_obj_exhaacplus_dec, ch_idx1, &ch_idx, &channel,
3041             p_obj_exhaacplus_dec->aac_config.element_instance_order, total_elements, element_used,
3042             total_channels, p_state_enhaacplus_dec->pstr_drc_dec,
3043             &p_state_enhaacplus_dec->drc_dummy, mps_buffer, &p_state_enhaacplus_dec->mps_header,
3044             &p_state_enhaacplus_dec->ui_mps_out_bytes);
3045       }
3046 
3047       if (error_code || ret_val) {
3048         ixheaacd_updatebytesconsumed(p_state_enhaacplus_dec, it_bit_buff);
3049         if (it_bit_buff->cnt_bits < 0) {
3050           p_state_enhaacplus_dec->ui_out_bytes = 0;
3051           p_state_enhaacplus_dec->ui_mps_out_bytes = 0;
3052           p_state_enhaacplus_dec->b_n_raw_data_blk = 0;
3053         }
3054         p_state_enhaacplus_dec->i_bytes_consumed = 1;
3055         p_state_enhaacplus_dec->b_n_raw_data_blk = 0;
3056         if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
3057           p_obj_exhaacplus_dec->aac_config.frame_status = 0;
3058           if (p_obj_exhaacplus_dec->aac_config.element_type[0] == ID_SCE)
3059             channel = 1;
3060           else
3061             channel = 2;
3062         } else {
3063           return error_code;
3064         }
3065       }
3066     } else {
3067       if (p_obj_exhaacplus_dec->aac_config.element_type[0] == ID_SCE)
3068         channel = 1;
3069       else
3070         channel = 2;
3071     }
3072 
3073     if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && (error_code || ret_val)) {
3074       if (p_obj_exhaacplus_dec->aac_config.element_type[ch_idx1] == 0 ||
3075           p_obj_exhaacplus_dec->aac_config.element_type[ch_idx1] == 3) {
3076         if (channel > 1) {
3077           channel = 1;
3078         }
3079       }
3080       if (p_obj_exhaacplus_dec->aac_config.element_type[ch_idx1] == 1) {
3081         if (channel > 2) {
3082           channel = 2;
3083         }
3084       }
3085       if (p_obj_exhaacplus_dec->aac_config.element_type[ch_idx1] == 2) {
3086         if (p_obj_exhaacplus_dec->aac_config.ui_max_channels > 2) {
3087           if (!(p_obj_exhaacplus_dec->aac_config.element_instance_order[ch_idx1] !=
3088                 p_obj_exhaacplus_dec->aac_config.ui_coupling_channel)) {
3089             if (channel > 1) {
3090               channel = 1;
3091             }
3092           }
3093         }
3094       }
3095       if (ps_enable == 1) {
3096         if (channel > 2) {
3097           channel = 2;
3098         }
3099       }
3100     }
3101 
3102     ch_fac = total_channels;
3103     slot_ele = p_obj_exhaacplus_dec->aac_config.slot_element[ch_idx];
3104     actual_out_buffer = time_data;
3105     if (p_obj_exhaacplus_dec->aac_config.element_type[ch_idx] == 2) {
3106       p_state_enhaacplus_dec->pstr_aac_dec_info[ch_idx]->p_ind_channel_info =
3107           &p_state_enhaacplus_dec->ind_cc_info;
3108       if (p_obj_exhaacplus_dec->aac_config.element_instance_order[ch_idx] !=
3109           p_obj_exhaacplus_dec->aac_config.ui_coupling_channel) {
3110         WORD32 pers_used = 0;
3111         skip_full_decode = 1;
3112         pers_used = ixheaacd_set_aac_persistent_buffers(
3113             p_state_enhaacplus_dec->pers_mem_ptr, channel);
3114 
3115         {
3116           struct ia_aac_persistent_struct *aac_persistent_mem =
3117               (struct ia_aac_persistent_struct *)
3118                   p_state_enhaacplus_dec->pers_mem_ptr;
3119           aac_persistent_mem->str_aac_decoder.pstr_aac_tables =
3120               &p_obj_exhaacplus_dec->aac_tables;
3121           aac_persistent_mem->str_aac_decoder.pstr_common_tables =
3122               p_obj_exhaacplus_dec->common_tables;
3123         }
3124 
3125         p_state_enhaacplus_dec->pstr_aac_dec_info[ch_idx] = 0;
3126 
3127         p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx] = 0;
3128 
3129         p_state_enhaacplus_dec->pstr_aac_dec_info[ch_idx] =
3130             ixheaacd_aac_decoder_init(
3131                 p_state_enhaacplus_dec,
3132 
3133                 p_state_enhaacplus_dec->pstr_stream_sbr[ch_idx],
3134 
3135                 channel, p_state_enhaacplus_dec->pers_mem_ptr,
3136                 p_state_enhaacplus_dec->frame_length
3137 
3138                 );
3139         if (!p_state_enhaacplus_dec->pstr_aac_dec_info[ch_idx]) {
3140           p_state_enhaacplus_dec->i_bytes_consumed = 1;
3141           if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
3142             p_obj_exhaacplus_dec->aac_config.frame_status = 0;
3143           } else {
3144             return IA_XHEAAC_DEC_INIT_FATAL_DEC_INIT_FAIL;
3145           }
3146         }
3147         p_state_enhaacplus_dec->pstr_aac_dec_info[ch_idx]->p_ind_channel_info =
3148             (WORD8 *)p_state_enhaacplus_dec->aac_scratch_mem_v + (8 * 1024) +
3149             pers_used;
3150       }
3151       if (p_obj_exhaacplus_dec->aac_config.element_type[1] < 3 &&
3152           p_obj_exhaacplus_dec->aac_config.element_type[1] > 0 &&
3153           p_obj_exhaacplus_dec->aac_config.ui_max_channels > 2) {
3154         actual_out_buffer =
3155             (WORD16 *)(VOID *)p_state_enhaacplus_dec->coup_ch_output;
3156       }
3157       ch_fac = 1;
3158       slot_ele = 0;
3159     }
3160 
3161     type = -1;
3162     p_state_enhaacplus_dec->pstr_stream_sbr[ch_idx][0].no_elements = 0;
3163 
3164     {
3165       WORD element_index_order1[MAX_BS_ELEMENT];
3166       ia_aac_dec_scratch_struct aac_scratch_struct;
3167       ixheaacd_allocate_aac_scr(
3168           &aac_scratch_struct, p_state_enhaacplus_dec->aac_scratch_mem_v,
3169           time_data, channel, p_obj_exhaacplus_dec->aac_config.ui_max_channels,
3170           p_state_enhaacplus_dec->audio_object_type);
3171 
3172       if (p_state_enhaacplus_dec->ch_config == 2 && channel == 1) {
3173         if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
3174           p_obj_exhaacplus_dec->aac_config.frame_status = 0;
3175         } else {
3176           return IA_XHEAAC_DEC_EXE_NONFATAL_DECODE_FRAME_ERROR;
3177         }
3178       }
3179 
3180       error_code = ixheaacd_aacdec_decodeframe(
3181           p_obj_exhaacplus_dec, &aac_scratch_struct, actual_out_buffer,
3182           p_obj_exhaacplus_dec->aac_config.frame_status, &type, &ch_idx, 0, channel,
3183           element_index_order1, skip_full_decode, ch_fac, slot_ele,
3184           p_obj_exhaacplus_dec->aac_config.ui_max_channels, total_channels,
3185           p_obj_exhaacplus_dec->p_state_aac->frame_length,
3186           p_obj_exhaacplus_dec->p_state_aac->frame_size, p_state_enhaacplus_dec->pstr_drc_dec,
3187           p_state_enhaacplus_dec->audio_object_type, p_state_enhaacplus_dec->ch_config,
3188           p_state_enhaacplus_dec->eld_specific_config, p_state_enhaacplus_dec->s_adts_hdr_present,
3189           &p_state_enhaacplus_dec->drc_dummy, p_state_enhaacplus_dec->ldmps_present,
3190           &p_state_enhaacplus_dec->slot_pos, mps_buffer, &p_state_enhaacplus_dec->mps_header,
3191           &p_state_enhaacplus_dec->ui_mps_out_bytes, 0,
3192           p_obj_exhaacplus_dec->aac_config.first_frame);
3193 
3194       p_state_enhaacplus_dec->slot_pos -= (channel - 1);
3195       p_state_enhaacplus_dec->sbr_present = 0;
3196 
3197       if (p_obj_exhaacplus_dec->p_state_aac->qshift_adj[0] != LD_OBJ &&
3198           p_state_enhaacplus_dec->frame_counter == 0) {
3199         ixheaacd_peak_limiter_init(
3200             &p_state_enhaacplus_dec->peak_limiter, total_channels,
3201             p_obj_exhaacplus_dec->p_state_aac->p_config->ui_samp_freq,
3202             &p_state_enhaacplus_dec->peak_limiter.buffer[0],
3203             &p_obj_exhaacplus_dec->p_state_aac->delay_in_samples);
3204         p_obj_exhaacplus_dec->p_state_aac->peak_lim_init = 1;
3205       }
3206 
3207       if (p_state_enhaacplus_dec->audio_object_type < ER_OBJECT_START ||
3208           (p_state_enhaacplus_dec->audio_object_type != AOT_ER_AAC_LD &&
3209            p_state_enhaacplus_dec->audio_object_type != AOT_ER_AAC_ELD &&
3210            p_state_enhaacplus_dec->audio_object_type != AOT_ER_AAC_LC)) {
3211         if ((error_code == 0) && ((ch_idx1 + 1) == total_elements) &&
3212             (type != ID_END)) {
3213           {
3214             p_state_enhaacplus_dec->i_bytes_consumed = (WORD32)(
3215                 it_bit_buff->ptr_read_next - it_bit_buff->ptr_bit_buf_base);
3216             p_state_enhaacplus_dec->b_n_raw_data_blk = 0;
3217             if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
3218               p_obj_exhaacplus_dec->aac_config.frame_status = 0;
3219             } else {
3220             return IA_XHEAAC_DEC_EXE_NONFATAL_ELE_INSTANCE_TAG_NOT_FOUND;
3221           }
3222           }
3223         }
3224       }
3225 
3226       num_ch = p_state_enhaacplus_dec->pstr_aac_dec_info[ch_idx]->channels;
3227       if (skip_full_decode == 0) {
3228         if (p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_ELD ||
3229             p_state_enhaacplus_dec->audio_object_type == AOT_ER_AAC_LD)
3230           frame_size = p_state_enhaacplus_dec->frame_length;
3231         else {
3232           frame_size = p_state_enhaacplus_dec->frame_length;
3233         }
3234 
3235         sample_rate_dec =
3236             p_state_enhaacplus_dec->pstr_aac_dec_info[ch_idx]->sampling_rate;
3237       }
3238     }
3239 
3240     if (skip_full_decode == 1) {
3241       p_state_enhaacplus_dec->pstr_stream_sbr[ch_idx][0].no_elements = 0;
3242     }
3243 
3244     if (p_state_enhaacplus_dec->pstr_stream_sbr[ch_idx][0].no_elements != 0) {
3245       p_obj_exhaacplus_dec->aac_config.ui_sbr_mode = 1;
3246     }
3247 
3248     if (error_code) {
3249       if (p_state_enhaacplus_dec->ui_input_over) {
3250         if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
3251           p_obj_exhaacplus_dec->aac_config.frame_status = 0;
3252         } else {
3253           return IA_XHEAAC_DEC_INIT_FATAL_EO_INPUT_REACHED;
3254         }
3255       }
3256       ixheaacd_updatebytesconsumed(p_state_enhaacplus_dec, it_bit_buff);
3257       p_obj_exhaacplus_dec->p_state_aac->ui_out_bytes +=
3258           p_state_enhaacplus_dec->num_of_out_samples * num_ch * sizeof(WORD16);
3259       if (error_code) {
3260         if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
3261           p_obj_exhaacplus_dec->aac_config.frame_status = 0;
3262         } else {
3263           return error_code;
3264         }
3265       }
3266     }
3267 
3268     error_code = IA_NO_ERROR;
3269 
3270     if (p_obj_exhaacplus_dec->aac_config.ui_auto_sbr_upsample == 0) {
3271       if (p_state_enhaacplus_dec->pstr_stream_sbr[ch_idx][0].no_elements == 0 &&
3272           p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]) {
3273         p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx] = 0;
3274         error_code = IA_XHEAAC_DEC_EXE_NONFATAL_SBR_TURNED_OFF;
3275       }
3276     }
3277     if ((!p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]) &&
3278         p_state_enhaacplus_dec->pstr_stream_sbr[ch_idx][0].no_elements) {
3279       WORD32 harmonic_sbr_flag = 0;
3280       error_code = IA_XHEAAC_DEC_EXE_NONFATAL_SBR_TURNED_ON;
3281       p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx] = ixheaacd_init_sbr(
3282           sample_rate_dec, frame_size,
3283           (FLAG *)&p_obj_exhaacplus_dec->aac_config.down_sample_flag,
3284           p_state_enhaacplus_dec->sbr_persistent_mem_v,
3285           p_state_enhaacplus_dec->ptr_overlap_buf, ps_enable ? 2 : channel,
3286           ps_enable, 1, frame_size * 2, &harmonic_sbr_flag, NULL,
3287           p_state_enhaacplus_dec->str_sbr_config,
3288           p_state_enhaacplus_dec->audio_object_type,
3289           p_state_enhaacplus_dec->mps_dec_handle.ldmps_config
3290               .ldmps_present_flag,
3291           p_state_enhaacplus_dec->mps_dec_handle.ldmps_config.no_ldsbr_present);
3292       if (p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]) {
3293         p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->xaac_jmp_buf =
3294             &(p_state_enhaacplus_dec->xaac_jmp_buf);
3295       }
3296     }
3297 
3298     {
3299       if (p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx] &&
3300           p_state_enhaacplus_dec->pstr_stream_sbr[0][0].no_elements) {
3301         ia_sbr_scr_struct sbr_scratch_struct;
3302         ixheaacd_allocate_sbr_scr(
3303             &sbr_scratch_struct, p_state_enhaacplus_dec->aac_scratch_mem_v,
3304             time_data, total_elements, ch_fac,
3305             p_state_enhaacplus_dec->audio_object_type, total_channels,
3306             p_obj_exhaacplus_dec->p_state_aac->qshift_adj,
3307             p_state_enhaacplus_dec->slot_pos, channel);
3308 
3309         p_state_enhaacplus_dec->sbr_present = 1;
3310         p_state_enhaacplus_dec->peak_lim_init = 0;
3311         {
3312           WORD32 audio_object_type = p_state_enhaacplus_dec->audio_object_type;
3313 
3314           if (audio_object_type != AOT_ER_AAC_ELD && audio_object_type != AOT_ER_AAC_LD) {
3315             WORD32 i = 0;
3316             ia_dec_data_struct* pstr_dec_data =
3317                 (ia_dec_data_struct*)p_state_enhaacplus_dec->pstr_dec_data;
3318             if (ch_fac == 1) {
3319               for (; i < 1024; i++) {
3320                 pstr_dec_data->str_usac_data.time_sample_vector[0][i] =
3321                     (FLOAT32)time_data[i];
3322               }
3323               p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->time_sample_buf[0] =
3324                   &pstr_dec_data->str_usac_data.time_sample_vector[0][0];
3325 
3326               p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->time_sample_buf[1] =
3327                   &pstr_dec_data->str_usac_data.time_sample_vector[1][0];
3328             } else if (ch_fac == 2) {
3329               for (; i < 1024; i++) {
3330                 pstr_dec_data->str_usac_data.time_sample_vector[0][i] =
3331                     (FLOAT32)time_data[2 * i + 0];
3332                 pstr_dec_data->str_usac_data.time_sample_vector[1][i] =
3333                     (FLOAT32)time_data[2 * i + 1];
3334               }
3335               p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->time_sample_buf[0] =
3336                   &pstr_dec_data->str_usac_data.time_sample_vector[0][0];
3337 
3338               p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->time_sample_buf[1] =
3339                   &pstr_dec_data->str_usac_data.time_sample_vector[1][0];
3340             } else if (ch_fac > 2) {
3341               if (channel == 1) {
3342                 for (; i < 1024; i++) {
3343                   pstr_dec_data->str_usac_data.time_sample_vector[0][i] =
3344                       (FLOAT32)(time_data + slot_ele)[i* ch_fac];
3345                 }
3346                 p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->time_sample_buf[0] =
3347                     &pstr_dec_data->str_usac_data.time_sample_vector[0][0];
3348               } else if (channel == 2) {
3349                 for (; i < 1024; i++) {
3350                   pstr_dec_data->str_usac_data.time_sample_vector[0][i] =
3351                       (FLOAT32)(time_data + slot_ele)[ch_fac * i + 0];
3352                   pstr_dec_data->str_usac_data.time_sample_vector[1][i] =
3353                       (FLOAT32)(time_data + slot_ele)[ch_fac * i + 1];
3354                 }
3355                 p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->time_sample_buf[0] =
3356                     &pstr_dec_data->str_usac_data.time_sample_vector[0][0];
3357 
3358                 p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->time_sample_buf[1] =
3359                     &pstr_dec_data->str_usac_data.time_sample_vector[1][0];
3360               }
3361             }
3362           }
3363         }
3364         p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->esbr_hq =
3365             p_obj_exhaacplus_dec->aac_config.ui_hq_esbr;
3366         p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->enh_sbr =
3367             p_obj_exhaacplus_dec->aac_config.ui_enh_sbr;
3368         p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->enh_sbr_ps =
3369             p_obj_exhaacplus_dec->aac_config.ui_enh_sbr_ps;
3370 
3371         if (p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]) {
3372           p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]->xaac_jmp_buf =
3373               &(p_state_enhaacplus_dec->xaac_jmp_buf);
3374         }
3375 
3376         if (ixheaacd_applysbr(p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx],
3377                               &p_state_enhaacplus_dec->pstr_stream_sbr[ch_idx][0],
3378                               actual_out_buffer, &num_ch,
3379                               p_obj_exhaacplus_dec->aac_config.frame_status,
3380                               p_obj_exhaacplus_dec->aac_config.down_sample_flag,
3381                               esbr_mono_downmix, &sbr_scratch_struct, ps_enable, ch_fac, slot_ele,
3382                               NULL, &p_state_enhaacplus_dec->str_drc_dec_info,
3383                               p_state_enhaacplus_dec->eld_specific_config.ld_sbr_flag_present,
3384                               p_state_enhaacplus_dec->audio_object_type, 0,
3385                               p_state_enhaacplus_dec->ldmps_present, frame_size,
3386                               p_state_enhaacplus_dec->heaac_mps_handle.heaac_mps_present,
3387                               p_obj_exhaacplus_dec->aac_config.ui_err_conceal,
3388                               p_obj_exhaacplus_dec->aac_config.first_frame) != SBRDEC_OK) {
3389           p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx] = 0;
3390           if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
3391             p_obj_exhaacplus_dec->aac_config.frame_status = 0;
3392           } else {
3393             return -1;
3394           }
3395         } else {
3396           if (!p_obj_exhaacplus_dec->aac_config.down_sample_flag) {
3397             frame_size = (WORD16)(frame_size * 2);
3398             sample_rate_dec *= 2;
3399           }
3400           if (p_state_enhaacplus_dec->mps_dec_handle.ldmps_config
3401                   .ldmps_present_flag == 1) {
3402             ixheaacd_mps_payload(
3403                 p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx],
3404                 p_obj_exhaacplus_dec);
3405           }
3406         }
3407         {
3408           WORD32 audio_object_type = p_state_enhaacplus_dec->audio_object_type;
3409 
3410           if (audio_object_type != AOT_ER_AAC_ELD && audio_object_type != AOT_ER_AAC_LD) {
3411             WORD32 out_bytes = 0;
3412             ia_dec_data_struct *pstr_dec_data =
3413                 (ia_dec_data_struct *)p_state_enhaacplus_dec->pstr_dec_data;
3414             if (ch_fac <= 2) {
3415               ixheaacd_samples_sat((WORD8*)time_data, 2048, 16,
3416                                    pstr_dec_data->str_usac_data.time_sample_vector,
3417                                    &out_bytes, ch_fac);
3418             } else {
3419               ixheaacd_samples_sat_mc((WORD8*)(time_data + slot_ele), 2048,
3420                                       pstr_dec_data->str_usac_data.time_sample_vector,
3421                                       &out_bytes, channel, ch_fac);
3422             }
3423           }
3424         }
3425         p_state_enhaacplus_dec->mps_dec_handle.ldmps_config.no_ldsbr_present =
3426             0;
3427         if (p_state_enhaacplus_dec->ui_mps_out_bytes > 0) {
3428           p_state_enhaacplus_dec->heaac_mps_handle.heaac_mps_present = 1;
3429         }
3430       } else {
3431         p_state_enhaacplus_dec->mps_dec_handle.ldmps_config.no_ldsbr_present = 1;
3432       }
3433     }
3434 
3435     if (p_state_enhaacplus_dec->mps_dec_handle.ldmps_config
3436                 .ldmps_present_flag == 1 &&
3437          p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx] &&
3438          p_state_enhaacplus_dec->mps_dec_handle.mps_init_done == 1) {
3439       if (p_state_enhaacplus_dec->ec_enable) {
3440         if (!p_obj_exhaacplus_dec->aac_config.first_frame) {
3441           error_code = ixheaacd_ld_mps_apply(p_obj_exhaacplus_dec, actual_out_buffer);
3442           if (error_code) p_obj_exhaacplus_dec->aac_config.frame_status = 0;
3443         }
3444       } else {
3445         error_code = ixheaacd_ld_mps_apply(p_obj_exhaacplus_dec, actual_out_buffer);
3446 
3447         if (error_code)
3448           return error_code;
3449       }
3450     }
3451     if (sample_rate < sample_rate_dec) {
3452       sample_rate = sample_rate_dec;
3453     }
3454 
3455     if (p_state_enhaacplus_dec->sbr_present ||
3456         p_obj_exhaacplus_dec->p_state_aac->qshift_adj[0] == LD_OBJ) {
3457       num_of_out_samples = frame_size;
3458 
3459     } else {
3460       num_of_out_samples =
3461           frame_size -
3462           MIN((WORD16)p_obj_exhaacplus_dec->p_state_aac->delay_in_samples, frame_size);
3463     }
3464 
3465     if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal &&
3466         p_obj_exhaacplus_dec->aac_config.first_frame &&
3467         (p_obj_exhaacplus_dec->p_state_aac->audio_object_type == AOT_ER_AAC_ELD ||
3468          p_obj_exhaacplus_dec->p_state_aac->audio_object_type == AOT_ER_AAC_LD)) {
3469       num_of_out_samples = frame_size;
3470     }
3471 
3472     p_obj_exhaacplus_dec->aac_config.ui_samp_freq = sample_rate;
3473 
3474     p_state_enhaacplus_dec->num_channel_last = num_ch;
3475     p_state_enhaacplus_dec->num_of_out_samples = num_of_out_samples;
3476 
3477     if (p_state_enhaacplus_dec->mps_dec_handle.ldmps_config
3478             .ldmps_present_flag == 1 &&
3479         p_state_enhaacplus_dec->mps_dec_handle.mps_init_done == 1 &&
3480         p_state_enhaacplus_dec->str_sbr_dec_info[ch_idx]) {
3481       if (p_obj_exhaacplus_dec->p_state_aac->mps_dec_handle.output_buffer) {
3482         ixheaacd_samples_sat((WORD8 *)actual_out_buffer, num_of_out_samples,
3483                              p_obj_exhaacplus_dec->aac_config.ui_pcm_wdsz,
3484                              p_obj_exhaacplus_dec->p_state_aac->mps_dec_handle.output_buffer,
3485                              &mps_out_samples, 2);
3486         p_obj_exhaacplus_dec->p_state_aac->ui_out_bytes = mps_out_samples;
3487       }
3488       num_ch = p_obj_exhaacplus_dec->p_state_aac->mps_dec_handle.out_ch_count;
3489       if (p_state_enhaacplus_dec->ec_enable) {
3490         if (p_obj_exhaacplus_dec->aac_config.first_frame) {
3491           p_obj_exhaacplus_dec->p_state_aac->ui_out_bytes =
3492               p_state_enhaacplus_dec->num_of_out_samples * num_ch * sizeof(WORD16);
3493         }
3494       }
3495     } else {
3496       if (p_obj_exhaacplus_dec->aac_config.element_type[ch_idx] != 2) {
3497         if (p_obj_exhaacplus_dec->aac_config.flag_to_stereo == 1 &&
3498           channel == 1 && total_elements == 1 && num_ch == 1) {
3499         num_ch = 2;
3500         p_obj_exhaacplus_dec->aac_config.dup_stereo_flag = 1;
3501 
3502       } else {
3503         p_obj_exhaacplus_dec->aac_config.dup_stereo_flag = 0;
3504       }
3505 
3506       p_obj_exhaacplus_dec->aac_config.ui_n_channels = 2;
3507 
3508       p_obj_exhaacplus_dec->p_state_aac->ui_out_bytes +=
3509           p_state_enhaacplus_dec->num_of_out_samples * num_ch * sizeof(WORD16);
3510 
3511     } else {
3512       channel_coupling_flag = 1;
3513     }
3514   }
3515 
3516     if (p_state_enhaacplus_dec->sbr_present && total_channels > 2) {
3517       for (WORD32 j = 0; j < channel; j++) {
3518         for (WORD32 i = 0; i < frame_size; i++) {
3519           intermediate_scr[total_channels * i + j +
3520                            p_state_enhaacplus_dec->slot_pos] =
3521               actual_out_buffer[total_channels * i + j +
3522                                 p_state_enhaacplus_dec->slot_pos];
3523         }
3524       }
3525     }
3526   }
3527 
3528   if (p_state_enhaacplus_dec->sbr_present && total_channels > 2) {
3529     memcpy(time_data, intermediate_scr,
3530            sizeof(WORD16) * frame_size * total_channels);
3531   }
3532 
3533   {
3534     ia_adts_crc_info_struct *ptr_adts_crc_info =
3535         p_state_enhaacplus_dec->ptr_bit_stream->pstr_adts_crc_info;
3536     if (ptr_adts_crc_info->crc_active == 1) {
3537       if ((error_code = ixheaacd_adts_crc_check_crc(ptr_adts_crc_info))) {
3538         if (error_code) {
3539           if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
3540             p_obj_exhaacplus_dec->aac_config.frame_status = 0;
3541           } else {
3542             return error_code;
3543           }
3544         }
3545       }
3546     }
3547   }
3548 
3549   p_obj_exhaacplus_dec->aac_config.ui_n_channels = total_channels;
3550 
3551   p_state_enhaacplus_dec->frame_counter++;
3552 
3553   WORD32 i, j;
3554 
3555   if (channel_coupling_flag) {
3556     ixheaacd_dec_ind_coupling(p_obj_exhaacplus_dec,
3557                               p_state_enhaacplus_dec->coup_ch_output,
3558                               num_of_out_samples, total_channels, time_data);
3559   }
3560 
3561   for (i = 0; i < total_channels; i++) {
3562     if (p_obj_exhaacplus_dec->p_state_aac->qshift_adj[i + 1] == 0)
3563       p_obj_exhaacplus_dec->p_state_aac->qshift_adj[i + 1] =
3564           p_obj_exhaacplus_dec->p_state_aac->qshift_adj[0];
3565   }
3566 
3567   if (p_obj_exhaacplus_dec->aac_config.flag_to_stereo == 1 &&
3568       total_elements == 1 && num_ch == 2 &&
3569       p_obj_exhaacplus_dec->aac_config.dup_stereo_flag == 1) {
3570     WORD i;
3571 
3572     if (!p_state_enhaacplus_dec->sbr_present &&
3573         p_obj_exhaacplus_dec->p_state_aac->qshift_adj[0] != LD_OBJ) {
3574       for (i = 0; i < frame_size; i++) {
3575         *((WORD32 *)actual_out_buffer + 2 * i + 1) =
3576             *((WORD32 *)actual_out_buffer + 2 * i);
3577       }
3578     } else {
3579       for (i = 0; i < frame_size; i++) {
3580         *(actual_out_buffer + 2 * i + 1) = *(actual_out_buffer + 2 * i);
3581       }
3582     }
3583   }
3584 
3585   if (!p_state_enhaacplus_dec->sbr_present &&
3586       p_obj_exhaacplus_dec->p_state_aac->peak_lim_init == 1 &&
3587       p_obj_exhaacplus_dec->p_state_aac->qshift_adj[0] != LD_OBJ) {
3588     if (!p_obj_exhaacplus_dec->aac_config.peak_limiter_off) {
3589       ixheaacd_peak_limiter_process(
3590           &p_state_enhaacplus_dec->peak_limiter, time_data, frame_size,
3591           p_obj_exhaacplus_dec->p_state_aac->qshift_adj);
3592     } else {
3593       ixheaacd_scale_adjust(time_data, frame_size,
3594                             p_obj_exhaacplus_dec->p_state_aac->qshift_adj,
3595                             total_channels);
3596     }
3597 
3598     for (i = 0; i < frame_size * 2; i++) {
3599       for (j = 0; j < total_channels; j++) {
3600         *((WORD16 *)time_data + total_channels * i + j) =
3601             ixheaacd_round16(*((WORD32 *)time_data + total_channels * i + j));
3602       }
3603     }
3604 
3605     memmove(
3606         time_data,
3607         (time_data +
3608          total_channels * p_obj_exhaacplus_dec->p_state_aac->delay_in_samples),
3609         sizeof(WORD16) * num_of_out_samples * total_channels);
3610 
3611     p_obj_exhaacplus_dec->p_state_aac->delay_in_samples =
3612         p_obj_exhaacplus_dec->p_state_aac->delay_in_samples -
3613         MIN(p_obj_exhaacplus_dec->p_state_aac->delay_in_samples, (UWORD16)frame_size);
3614   }
3615   if (p_state_enhaacplus_dec->heaac_mps_handle.heaac_mps_present == 1) {
3616     ia_heaac_mps_state_struct *pstr_mps_state =
3617         &p_state_enhaacplus_dec->heaac_mps_handle;
3618     if (p_state_enhaacplus_dec->sbr_present == 0) {
3619       p_state_enhaacplus_dec->heaac_mps_handle.mps_decode = 1;
3620     } else {
3621       p_state_enhaacplus_dec->heaac_mps_handle.mps_with_sbr = 1;
3622     }
3623     if (p_state_enhaacplus_dec->heaac_mps_handle.mps_init_done == 1) {
3624       p_obj_exhaacplus_dec->p_state_aac->heaac_mps_handle.frame_ok =
3625           p_obj_exhaacplus_dec->aac_config.frame_status;
3626       p_obj_exhaacplus_dec->p_state_aac->heaac_mps_handle.ec_flag =
3627           p_obj_exhaacplus_dec->aac_config.ui_err_conceal;
3628 
3629       ixheaacd_heaac_mps_apply(p_obj_exhaacplus_dec, actual_out_buffer,
3630                                mps_buffer,
3631                                p_state_enhaacplus_dec->ui_mps_out_bytes);
3632 
3633       p_state_enhaacplus_dec->heaac_mps_handle.mps_decode = 1;
3634       p_obj_exhaacplus_dec->p_state_aac->ui_out_bytes =
3635           (pstr_mps_state->num_output_channels_at *
3636            pstr_mps_state->frame_length *
3637            (p_obj_exhaacplus_dec->aac_config.ui_pcm_wdsz >> 3));
3638       p_obj_exhaacplus_dec->p_state_aac->heaac_mps_handle.first_frame = 0;
3639     }
3640   }
3641   if ((total_channels > 2) && (1 == p_obj_exhaacplus_dec->aac_config.downmix)) {
3642     ixheaacd_dec_downmix_to_stereo(p_obj_exhaacplus_dec, num_of_out_samples,
3643                                    total_elements, time_data, total_channels);
3644 
3645     total_channels = 2;
3646     p_obj_exhaacplus_dec->p_state_aac->ui_out_bytes =
3647         p_state_enhaacplus_dec->num_of_out_samples * 2 * sizeof(WORD16);
3648   }
3649 
3650   if (p_obj_exhaacplus_dec->aac_config.flag_downmix && total_channels == 2) {
3651     WORD32 out_ch = 1;
3652     WORD i;
3653     if (p_obj_exhaacplus_dec->aac_config.flag_to_stereo == 1) {
3654       out_ch = 2;
3655     }
3656 
3657     p_obj_exhaacplus_dec->aac_config.ui_n_channels = out_ch;
3658     p_obj_exhaacplus_dec->p_state_aac->ui_out_bytes =
3659         p_state_enhaacplus_dec->num_of_out_samples * out_ch * sizeof(WORD16);
3660 
3661     for (i = 0; i < num_of_out_samples; i++) {
3662       WORD16 temp;
3663 
3664       temp = (time_data[2 * i + 0] >> 1) + (time_data[2 * i + 1] >> 1);
3665 
3666       if (out_ch == 2) {
3667         time_data[2 * i + 0] = temp;
3668         time_data[2 * i + 1] = time_data[2 * i + 0];
3669       } else {
3670         time_data[i] = temp;
3671       }
3672     }
3673   }
3674 
3675   if (p_state_enhaacplus_dec->s_adts_hdr_present) {
3676     if (adts.no_raw_data_blocks != 0) {
3677       if (adts.protection_absent == 0 && it_bit_buff->cnt_bits >= 16) {
3678         adts.crc_check = ixheaacd_read_bits_buf(it_bit_buff, 16);
3679       }
3680     }
3681     p_state_enhaacplus_dec->b_n_raw_data_blk--;
3682   }
3683 
3684   ixheaacd_updatebytesconsumed(p_state_enhaacplus_dec, it_bit_buff);
3685 
3686   if (p_state_enhaacplus_dec->bs_format == LOAS_BSFORMAT)
3687     p_state_enhaacplus_dec->i_bytes_consumed =
3688         (WORD32)(audio_mux_length_bytes_last + bytes_for_sync);
3689 
3690   if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal &&
3691       p_obj_exhaacplus_dec->aac_config.first_frame) {
3692     p_obj_exhaacplus_dec->aac_config.first_frame = 0;
3693   }
3694 
3695   if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
3696     if (p_obj_exhaacplus_dec->aac_config.frame_status != 1) {
3697       p_state_enhaacplus_dec->i_bytes_consumed = p_state_enhaacplus_dec->ui_in_bytes;
3698     }
3699     return IA_NO_ERROR;
3700   } else {
3701     return error_code;
3702   }
3703 }
3704