xref: /aosp_15_r20/external/libxaac/decoder/ixheaacd_aacdecoder.c (revision 15dc779a375ca8b5125643b829a8aa4b70d7f451)
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 <stdio.h>
21 #include <string.h>
22 #include "ixheaacd_sbr_common.h"
23 #include "ixheaac_type_def.h"
24 
25 #include "ixheaac_constants.h"
26 #include "ixheaac_basic_ops32.h"
27 #include "ixheaac_basic_ops16.h"
28 #include "ixheaac_basic_ops40.h"
29 #include "ixheaac_basic_ops.h"
30 #include "ixheaacd_bitbuffer.h"
31 
32 #include "ixheaacd_defines.h"
33 
34 #include "ixheaacd_aac_rom.h"
35 
36 #include "ixheaacd_error_codes.h"
37 
38 #include "ixheaacd_pulsedata.h"
39 
40 #include "ixheaacd_pns.h"
41 #include "ixheaacd_drc_data_struct.h"
42 
43 #include "ixheaacd_interface.h"
44 #include "ixheaacd_info.h"
45 #include "ixheaacd_cnst.h"
46 #include "ixheaacd_sbrdecsettings.h"
47 #include "ixheaacd_sbr_scale.h"
48 #include "ixheaacd_common_rom.h"
49 #include "ixheaacd_env_extr_part.h"
50 #include "ixheaacd_sbr_rom.h"
51 #include "ixheaacd_hybrid.h"
52 #include "ixheaacd_ps_dec.h"
53 #include "ixheaacd_qmf_dec.h"
54 #include "ixheaacd_mps_polyphase.h"
55 #include "ixheaacd_config.h"
56 #include "ixheaacd_mps_struct_def.h"
57 #include "ixheaacd_mps_res_rom.h"
58 #include "ixheaacd_mps_aac_struct.h"
59 #include "ixheaacd_mps_dec.h"
60 #include "ixheaac_error_standards.h"
61 #include "ixheaacd_sbrdecoder.h"
62 #include "ixheaacd_acelp_info.h"
63 #include "ixheaacd_tns_usac.h"
64 #include "ixheaacd_ec_defines.h"
65 #include "ixheaacd_ec_rom.h"
66 #include "ixheaacd_ec_struct_def.h"
67 #include "ixheaacd_main.h"
68 #include "ixheaacd_channelinfo.h"
69 #include "ixheaacd_ec.h"
70 #include "ixheaacd_drc_dec.h"
71 
72 #include "ixheaacd_block.h"
73 #include "ixheaacd_channel.h"
74 #include "ixheaacd_sbr_payload.h"
75 #include "ixheaacd_stereo.h"
76 #include "ixheaacd_adts.h"
77 #include "ixheaacd_audioobjtypes.h"
78 #include "ixheaacd_memory_standards.h"
79 #include "ixheaacd_latmdemux.h"
80 #include "ixheaacd_aacdec.h"
81 
82 #include "ixheaacd_struct_def.h"
83 #include "ixheaacd_headerdecode.h"
84 #include "ixheaacd_multichannel.h"
85 #include "ixheaacd_adts_crc_check.h"
86 #include "ixheaacd_ld_mps_dec.h"
87 
88 #include "ixheaacd_hcr.h"
89 #include "ixheaacd_struct.h"
90 
91 #define SIZEOF_INT(x) ((sizeof(x) + sizeof(WORD32) - 1) / sizeof(WORD32))
92 
93 #define EXT_FILL_DATA 1
94 #define EXT_FIL 0
95 #define EXT_DATA_LENGTH 3
96 #define EXT_LDSAC_DATA 9
97 
98 extern const ia_usac_samp_rate_info ixheaacd_samp_rate_info[];
99 
ixheaacd_aacdec_decodeframe(ia_exhaacplus_dec_api_struct * p_obj_exhaacplus_dec,ia_aac_dec_scratch_struct * aac_scratch_ptrs,VOID * time_data_tmp,FLAG frame_status,WORD * type,WORD * ch_idx,WORD init_flag,WORD channel,WORD * element_index_order,WORD skip_full_decode,WORD ch_fac,WORD slot_element,WORD max_channels,WORD32 total_channels,WORD32 frame_length,WORD32 frame_size,ia_drc_dec_struct * pstr_drc_dec,WORD32 object_type,WORD32 ch_config,ia_eld_specific_config_struct eld_specific_config,WORD16 adtsheader,ia_drc_dec_struct * drc_dummy,WORD32 ldmps_present,UWORD8 * slot_pos,UWORD8 * mps_buffer,WORD32 * mps_header,WORD32 * mps_bytes,WORD32 is_init,WORD32 first_frame)100 WORD32 ixheaacd_aacdec_decodeframe(
101     ia_exhaacplus_dec_api_struct *p_obj_exhaacplus_dec,
102     ia_aac_dec_scratch_struct *aac_scratch_ptrs, VOID *time_data_tmp,
103     FLAG frame_status, WORD *type, WORD *ch_idx, WORD init_flag, WORD channel,
104     WORD *element_index_order, WORD skip_full_decode, WORD ch_fac,
105     WORD slot_element, WORD max_channels, WORD32 total_channels,
106     WORD32 frame_length, WORD32 frame_size, ia_drc_dec_struct *pstr_drc_dec,
107     WORD32 object_type, WORD32 ch_config,
108     ia_eld_specific_config_struct eld_specific_config, WORD16 adtsheader,
109     ia_drc_dec_struct *drc_dummy, WORD32 ldmps_present, UWORD8 *slot_pos, UWORD8 *mps_buffer,
110     WORD32 *mps_header, WORD32 *mps_bytes, WORD32 is_init, WORD32 first_frame) {
111   WORD ch, ele_type;
112   ia_aac_dec_state_struct *p_state_enhaacplus_dec;
113   ia_aac_decoder_struct *aac_dec_handle;
114   ia_bit_buf_struct *it_bit_buff;
115   ixheaacd_latm_struct *latm_element;
116 
117   WORD error_code = (WORD)frame_status;
118   WORD previous_element;
119   WORD prev_data_ele_present = 0;
120   WORD new_element;
121   WORD32 num_ch = 0;
122 
123   WORD32 crc_reg = 0;
124   ia_adts_crc_info_struct *ptr_adts_crc_info;
125 
126   WORD32 cnt_bits = 0;
127 
128   WORD32 eld_sbr_flag = eld_specific_config.ld_sbr_flag_present;
129   WORD32 ld_sbr_crc_flag = eld_specific_config.ld_sbr_crc_flag;
130   WORD32 aac_spect_data_resil_flag =
131       eld_specific_config.aac_spect_data_resil_flag;
132 
133   WORD32 ele_ch = 0;
134 
135   ia_aac_sfb_code_book_struct *ptr_aac_sfb_code_book_data[CHANNELS];
136   ia_pns_stereo_data_struct *ptr_pns_stereo_data;
137 
138   WORD32 *work_buffer_core = aac_scratch_ptrs->base_scr_8k;
139   WORD32 *work_buffer_1 = aac_scratch_ptrs->extra_scr_4k[0];
140   WORD32 *work_buffer_2 = aac_scratch_ptrs->extra_scr_4k[2];
141   p_state_enhaacplus_dec = p_obj_exhaacplus_dec->p_state_aac;
142 
143   WORD32 *time_data = (WORD32 *)time_data_tmp;
144 
145   aac_dec_handle = p_state_enhaacplus_dec->pstr_aac_dec_info[*ch_idx];
146   it_bit_buff = p_state_enhaacplus_dec->ptr_bit_stream;
147 
148   ptr_adts_crc_info = it_bit_buff->pstr_adts_crc_info;
149 
150   latm_element = &p_state_enhaacplus_dec->latm_struct_element;
151 
152   ptr_pns_stereo_data =
153       (ia_pns_stereo_data_struct
154            *)&work_buffer_1[2 * SIZEOF_INT(ia_aac_dec_channel_info_struct) +
155                             2 * SIZEOF_INT(ia_aac_sfb_code_book_struct)];
156 
157   aac_dec_handle->frame_status = 1;
158 
159   for (ch = 0; ch < channel; ch++) {
160     const ia_aac_dec_imdct_tables_struct *pstr_imdct_tables;
161     aac_dec_handle->pstr_aac_dec_ch_info[ch] =
162         (ia_aac_dec_channel_info_struct
163              *)&work_buffer_1[ch * SIZEOF_INT(ia_aac_dec_channel_info_struct)];
164     ptr_aac_sfb_code_book_data[ch] =
165         (ia_aac_sfb_code_book_struct
166              *)&work_buffer_1[2 * SIZEOF_INT(ia_aac_dec_channel_info_struct) +
167                               (ch * SIZEOF_INT(ia_aac_sfb_code_book_struct))];
168 
169     aac_dec_handle->pstr_aac_dec_ch_info[ch]->ptr_scale_factor =
170         ptr_aac_sfb_code_book_data[ch]->scale_factor;
171     aac_dec_handle->pstr_aac_dec_ch_info[ch]->ptr_code_book =
172         ptr_aac_sfb_code_book_data[ch]->code_book;
173 
174     aac_dec_handle->pstr_aac_dec_ch_info[ch]->ptr_spec_coeff =
175         &work_buffer_core[ch * MAX_BINS_LONG];
176 
177     if (object_type == AOT_ER_AAC_ELD) {
178       aac_dec_handle->pstr_aac_dec_ch_info[ch]->ptr_spec_coeff =
179           &work_buffer_core[2 * ch * MAX_BINS_LONG];
180     }
181 
182     aac_dec_handle->pstr_aac_dec_ch_info[ch]->pstr_stereo_info =
183         &ptr_pns_stereo_data->str_stereo_info;
184     aac_dec_handle->pstr_aac_dec_ch_info[ch]->pstr_pns_corr_info =
185         &ptr_pns_stereo_data->str_pns_corr_info;
186     aac_dec_handle->pstr_aac_dec_ch_info[ch]->pstr_pns_rand_vec_data =
187         aac_dec_handle->pstr_pns_rand_vec_data;
188 
189     pstr_imdct_tables = aac_dec_handle->pstr_aac_tables->pstr_imdct_tables;
190 
191     if (960 != frame_length) {
192       aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[0] =
193           pstr_imdct_tables->only_long_window_sine;
194       aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_short_window[0] =
195           pstr_imdct_tables->only_short_window_sine;
196       aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[1] =
197           pstr_imdct_tables->only_long_window_kbd;
198       aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_short_window[1] =
199           pstr_imdct_tables->only_short_window_kbd;
200 
201       aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ptr_long_window[0] =
202           pstr_imdct_tables->only_long_window_sine;
203       aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ptr_short_window[0] =
204           pstr_imdct_tables->only_short_window_sine;
205       aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ptr_long_window[1] =
206           pstr_imdct_tables->only_long_window_kbd;
207       aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ptr_short_window[1] =
208           pstr_imdct_tables->only_short_window_kbd;
209     } else {
210       aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[0] =
211           pstr_imdct_tables->only_long_window_sine_960;
212       aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_short_window[0] =
213           pstr_imdct_tables->only_short_window_sine_120;
214       aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[1] =
215           pstr_imdct_tables->only_long_window_kbd_960;
216       aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_short_window[1] =
217           pstr_imdct_tables->only_short_window_kbd_120;
218 
219       aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ptr_long_window[0] =
220           pstr_imdct_tables->only_long_window_sine_960;
221       aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ptr_short_window[0] =
222           pstr_imdct_tables->only_short_window_sine_120;
223       aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ptr_long_window[1] =
224           pstr_imdct_tables->only_long_window_kbd_960;
225       aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ptr_short_window[1] =
226           pstr_imdct_tables->only_short_window_kbd_120;
227     }
228 
229     if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && frame_status == 0)
230     {
231       memset(&aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info, 0,
232              sizeof(ia_ics_info_struct));
233     }
234     aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info.frame_length = frame_length;
235     if (object_type == AOT_ER_AAC_ELD || object_type == AOT_ER_AAC_LD ||
236         object_type == AOT_AAC_LTP) {
237       if (512 == aac_dec_handle->samples_per_frame) {
238         if (object_type != AOT_ER_AAC_ELD) {
239           aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[1] =
240               (WORD16 *)pstr_imdct_tables->low_overlap_win;
241           aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[0] =
242               (WORD16 *)pstr_imdct_tables->window_sine_512;
243 
244           aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
245               ->ptr_long_window[1] =
246               (WORD16 *)pstr_imdct_tables->low_overlap_win;
247           aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
248               ->ptr_long_window[0] =
249               (WORD16 *)pstr_imdct_tables->window_sine_512;
250         } else {
251           aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[1] =
252               (WORD16 *)pstr_imdct_tables->window_sine_512_eld;
253           aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[0] =
254               (WORD16 *)pstr_imdct_tables->window_sine_512_eld;
255         }
256       } else if (480 == aac_dec_handle->samples_per_frame) {
257         if (object_type != AOT_ER_AAC_ELD) {
258           aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[1] =
259               (WORD16 *)pstr_imdct_tables->low_overlap_win_480;
260           aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[0] =
261               (WORD16 *)pstr_imdct_tables->window_sine_480;
262 
263           aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
264               ->ptr_long_window[1] =
265               (WORD16 *)pstr_imdct_tables->low_overlap_win_480;
266           aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
267               ->ptr_long_window[0] =
268               (WORD16 *)pstr_imdct_tables->window_sine_480;
269 
270         } else {
271           aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[1] =
272               (WORD16 *)pstr_imdct_tables->window_sine_480_eld;
273           aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[0] =
274               (WORD16 *)pstr_imdct_tables->window_sine_480_eld;
275         }
276       }
277     }
278     if ((object_type == AOT_ER_AAC_LD) || (object_type == AOT_AAC_LTP)) {
279       if (aac_dec_handle->samples_per_frame <= 512) {
280         aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info.ltp2.lag =
281             aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ltp_lag_1;
282         aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info.ltp.lag =
283             aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ltp_lag_2;
284       }
285       aac_dec_handle->pstr_aac_dec_ch_info[ch]->ltp_buf =
286           aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ltp_buf;
287       aac_dec_handle->pstr_aac_dec_ch_info[ch]->ltp_lag =
288           aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ltp_lag_1;
289     }
290 
291     aac_dec_handle->pstr_aac_dec_ch_info[ch]->scratch_buf_ptr = work_buffer_2;
292     if (object_type == AOT_ER_AAC_ELD) {
293       aac_dec_handle->pstr_aac_dec_ch_info[ch]->pulse_scratch =
294           aac_scratch_ptrs->extra_scr_4k[3];
295     }
296   }
297 
298   if (channel == 2) {
299     if (aac_dec_handle->pstr_aac_dec_ch_info[1]->ptr_spec_coeff ==
300         aac_scratch_ptrs->extra_scr_4k[0]) {
301       aac_dec_handle->pstr_aac_dec_ch_info[1]->ptr_spec_coeff =
302           aac_dec_handle->pstr_aac_dec_ch_info[0]->ptr_spec_coeff;
303     }
304   }
305 
306   for (ch = 0; ch < channel; ch++) {
307     ia_pns_info_struct *ptr_pns_info =
308         &aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_pns_info;
309     memset(ptr_pns_info, 0, sizeof(ia_pns_info_struct));
310   }
311 
312   if (channel > 0) {
313     ia_pns_correlation_info_struct *ptr_corr_info =
314         aac_dec_handle->pstr_aac_dec_ch_info[0]->pstr_pns_corr_info;
315     memset(ptr_corr_info->correlated, 0, sizeof(UWORD8) * PNS_BAND_FLAGS_SIZE);
316   }
317 
318   for (ch = 0; ch < channel; ch++) {
319     memset(&aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_hcr_info, 0,
320            sizeof(ia_hcr_info_struct));
321     ixheaacd_huff_code_reorder_tbl_init(
322         &aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_hcr_info);
323   }
324 
325   for (ch = 0; ch < channel; ch++) {
326     aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info.ltp.data_present = 0;
327     aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info.ltp2.data_present =
328         0;
329   }
330 
331   for (ch = 0; ch < channel; ch++) {
332     if (object_type == AOT_ER_AAC_ELD || object_type == AOT_ER_AAC_LD)
333       aac_dec_handle->pstr_aac_dec_ch_info[ch]->granule_len =
334           aac_dec_handle->samples_per_frame;
335     if (object_type == AOT_ER_AAC_LC)
336       aac_dec_handle->pstr_aac_dec_ch_info[ch]->granule_len =
337           aac_dec_handle->samples_per_frame / 8;
338   }
339   previous_element = ID_END;
340 
341   aac_dec_handle->pstr_sbr_bitstream->no_elements = 0;
342   new_element = 0;
343   ele_type = *type;
344 
345   cnt_bits = it_bit_buff->cnt_bits;
346 
347   WORD32 err = 0;
348   jmp_buf local;
349 
350   if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
351     err = setjmp(local);
352   }
353 
354   if (!err && frame_status) {
355     if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
356       it_bit_buff->xaac_jmp_buf = &local;
357     }
358 
359     if (((object_type != AOT_ER_AAC_ELD) && (object_type != AOT_ER_AAC_LD) &&
360          (object_type != AOT_ER_AAC_LC)) ||
361         (object_type < ER_OBJECT_START)) {
362       while (ele_type != ID_END && aac_dec_handle->frame_status) {
363         ele_type = (WORD)ixheaacd_read_bits_buf(it_bit_buff, 3);
364         ixheaacd_read_bidirection(it_bit_buff, -3);
365 
366         if (it_bit_buff->cnt_bits < 3) {
367           it_bit_buff->cnt_bits = -1;
368           error_code = (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
369           break;
370         }
371 
372         if ((ele_type == ID_FIL) || (ele_type == ID_DSE) || (new_element == 0)) {
373           ele_type = (WORD)ixheaacd_read_bits_buf(it_bit_buff, 3);
374           new_element = 1;
375         } else if ((ele_type != ID_END)) {
376           ele_type = -1;
377           break;
378         } else {
379           ele_type = (WORD)ixheaacd_read_bits_buf(it_bit_buff, 3);
380         }
381 
382         if (it_bit_buff->cnt_bits < 0) {
383           aac_dec_handle->frame_status = 0;
384         }
385 
386         switch (ele_type) {
387           case ID_SCE:
388           case ID_CPE:
389           case ID_LFE:
390 
391             if (aac_dec_handle->frame_status) {
392               ia_aac_dec_channel_info_struct *pstr_aac_dec_ch_info =
393                   aac_dec_handle->pstr_aac_dec_ch_info[LEFT];
394               ia_ics_info_struct *ptr_ics_info = &pstr_aac_dec_ch_info->str_ics_info;
395               ele_ch = 1;
396               if (ele_type == ID_CPE) {
397                 ele_ch = 2;
398               } else {
399                 ele_ch = 1;
400               }
401 
402               prev_data_ele_present = 1;
403 
404               if (ptr_adts_crc_info->crc_active == 1 && ptr_adts_crc_info->no_reg < 7) {
405                 crc_reg = ixheaacd_adts_crc_start_reg(ptr_adts_crc_info, it_bit_buff,
406                                                       CRC_ADTS_RAW_DATA_BLK_LEN);
407               }
408 
409               pstr_aac_dec_ch_info->element_instance_tag =
410                   (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 4);
411 
412               element_index_order[*ch_idx] = pstr_aac_dec_ch_info->element_instance_tag;
413               pstr_aac_dec_ch_info->common_window = 0;
414 
415               ptr_ics_info->num_swb_window = 0;
416               ptr_ics_info->sampling_rate_index = aac_dec_handle->sampling_rate_index;
417               if ((object_type == AOT_ER_AAC_LD) || (object_type == AOT_AAC_LTP)) {
418                 ptr_ics_info->ltp.data_present = 0;
419                 ptr_ics_info->ltp2.data_present = 0;
420                 ptr_ics_info->predictor_data_present = 0;
421               }
422 
423               if (ele_ch > 1) {
424                 aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]->str_ics_info.num_swb_window = 0;
425                 aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]->str_ics_info.sampling_rate_index =
426                     aac_dec_handle->sampling_rate_index;
427 
428                 pstr_aac_dec_ch_info->common_window =
429                     (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 1);
430 
431                 if (pstr_aac_dec_ch_info->common_window) {
432                   error_code = ixheaacd_ics_read(
433                       it_bit_buff, ptr_ics_info, aac_dec_handle->num_swb_window, object_type,
434                       pstr_aac_dec_ch_info->common_window, aac_dec_handle->samples_per_frame);
435                   if (error_code) {
436                     aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]->str_ics_info =
437                         aac_dec_handle->pstr_aac_dec_ch_info[LEFT]->str_ics_info;
438                     if (it_bit_buff->cnt_bits < 0) {
439                       error_code =
440                           (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
441                     }
442 
443                     goto _ia_handle_error;
444                   }
445 
446                   aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]->str_ics_info =
447                       pstr_aac_dec_ch_info->str_ics_info;
448 
449                   ixheaacd_read_ms_data(it_bit_buff, pstr_aac_dec_ch_info);
450                 }
451               }
452 
453               error_code = ixheaacd_individual_ch_stream(
454                   it_bit_buff, aac_dec_handle, ele_ch, frame_length, total_channels, object_type,
455                   eld_specific_config, ele_type);
456 
457               if (error_code) {
458                 if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && !is_init) {
459                   aac_dec_handle->frame_status = 0;
460                 } else {
461                   return error_code;
462                 }
463               }
464 
465               if (ptr_adts_crc_info->crc_active == 1) {
466                 ixheaacd_adts_crc_end_reg(ptr_adts_crc_info, it_bit_buff, crc_reg);
467               }
468 
469               if (it_bit_buff->cnt_bits < 0) {
470                 error_code =
471                     (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
472               }
473 
474               if (error_code) {
475                 goto _ia_handle_error;
476               }
477 
478             _ia_handle_error:
479               if (error_code) {
480                 aac_dec_handle->frame_status = 0;
481                 if ((ele_type >= ID_SCE) && (ele_type <= ID_LFE)) num_ch = num_ch + ele_ch;
482                 break;
483               } else {
484                 error_code = ixheaacd_channel_pair_process(
485                     aac_dec_handle->pstr_aac_dec_ch_info, ele_ch, aac_dec_handle->pstr_aac_tables,
486                     total_channels, object_type, aac_spect_data_resil_flag,
487                     eld_specific_config.aac_sf_data_resil_flag, aac_scratch_ptrs->in_data,
488                     aac_scratch_ptrs->out_data, (VOID *)aac_dec_handle);
489                 if (error_code) {
490                   if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && !is_init) {
491                     aac_dec_handle->frame_status = 0;
492                   } else {
493                     return error_code;
494                   }
495                 }
496                 num_ch = num_ch + ele_ch;
497               }
498             }
499 
500             break;
501           case ID_CCE:
502             if (max_channels > 2) {
503               prev_data_ele_present = 1;
504               error_code = ixheaacd_dec_coupling_channel_element(
505                   it_bit_buff, aac_dec_handle, aac_dec_handle->sampling_rate_index,
506                   aac_dec_handle->pstr_aac_tables, aac_dec_handle->pstr_common_tables,
507                   &element_index_order[*ch_idx],
508                   (ia_enhaacplus_dec_ind_cc *)aac_dec_handle->p_ind_channel_info, total_channels,
509                   frame_length, object_type, eld_specific_config, ele_type);
510 
511               num_ch = num_ch + 1;
512 
513               if (error_code) {
514                 aac_dec_handle->frame_status = 0;
515                 if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && !is_init) {
516                   aac_dec_handle->frame_status = 0;
517                 } else {
518                   return error_code;
519                 }
520               } else {
521                 error_code = ixheaacd_channel_pair_process(
522                     aac_dec_handle->pstr_aac_dec_ch_info, 1, aac_dec_handle->pstr_aac_tables,
523                     total_channels, object_type, aac_spect_data_resil_flag,
524                     eld_specific_config.aac_sf_data_resil_flag, aac_scratch_ptrs->in_data,
525                     aac_scratch_ptrs->out_data, (VOID *)aac_dec_handle);
526                 if (error_code) {
527                   if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && !is_init) {
528                     aac_dec_handle->frame_status = 0;
529                   } else {
530                     return error_code;
531                   }
532                 }
533               }
534             } else {
535               error_code = (WORD32)((WORD32)IA_XHEAAC_DEC_EXE_FATAL_UNIMPLEMENTED_CCE);
536             }
537             if (it_bit_buff->cnt_bits < 0) {
538               error_code = (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
539               goto _ia_handle_error;
540             }
541             break;
542 
543           case ID_DSE:
544           case ID_PCE:
545           case ID_FIL:
546 
547           {
548             WORD32 flag = 1;
549 
550             if ((ele_type != ID_FIL) && (ptr_adts_crc_info->crc_active == 1) &&
551                 (ptr_adts_crc_info->no_reg < 7)) {
552               crc_reg = ixheaacd_adts_crc_start_reg(ptr_adts_crc_info, it_bit_buff, 0);
553             }
554             if (ele_type == ID_DSE) {
555               ixheaacd_read_data_stream_element(it_bit_buff, &aac_dec_handle->byte_align_bits,
556                                                 p_obj_exhaacplus_dec->p_state_aac->pstr_drc_dec);
557             }
558 
559             else if (ele_type == ID_PCE) {
560               error_code = ixheaacd_decode_pce(
561                   it_bit_buff, &p_obj_exhaacplus_dec->aac_config.ui_pce_found_in_hdr,
562                   &p_obj_exhaacplus_dec->aac_config.str_prog_config);
563               if (error_code != 0) {
564                 if (it_bit_buff->cnt_bits < 0) {
565                   error_code =
566                       (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
567                   goto _ia_handle_error;
568                 }
569                 aac_dec_handle->frame_status = 0;
570                 if (error_code > 0) {
571                   if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && !is_init) {
572                     aac_dec_handle->frame_status = 0;
573                   } else {
574                     return IA_XHEAAC_DEC_EXE_NONFATAL_DECODE_FRAME_ERROR;
575                   }
576                 } else {
577                   if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && !is_init) {
578                     aac_dec_handle->frame_status = 0;
579                   } else {
580                     return error_code;
581                   }
582                 }
583               }
584             }
585 
586             else if (ele_type == ID_FIL) {
587               WORD32 bits_decoded = 0;
588               if (object_type == AOT_ER_AAC_ELD) {
589                 bits_decoded = (it_bit_buff->size - it_bit_buff->cnt_bits);
590                 cnt_bits = (frame_size * 8 - bits_decoded);
591                 if (adtsheader == 1) {
592                   if (cnt_bits > it_bit_buff->cnt_bits) {
593                     if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && !is_init) {
594                       aac_dec_handle->frame_status = 0;
595                     } else {
596                       return IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES;
597                     }
598                   }
599                 }
600               }
601 
602               if (ixheaacd_check_for_sbr_payload(
603                       it_bit_buff, aac_dec_handle->pstr_sbr_bitstream, (WORD16)previous_element,
604                       pstr_drc_dec, object_type, adtsheader, cnt_bits, ld_sbr_crc_flag, drc_dummy,
605                       mps_buffer, mps_header, mps_bytes, is_init, &aac_dec_handle->is_first,
606                       p_obj_exhaacplus_dec->aac_config.ui_err_conceal)) {
607                 flag = 0;
608               }
609             }
610 
611             if (it_bit_buff->cnt_bits < 0) {
612               error_code = (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
613               goto _ia_handle_error;
614             }
615 
616             if (flag) {
617               if (prev_data_ele_present == 0) {
618                 new_element = 0;
619               }
620             }
621             if ((ele_type != ID_FIL) && (ptr_adts_crc_info->crc_active == 1)) {
622               ixheaacd_adts_crc_end_reg(ptr_adts_crc_info, it_bit_buff, crc_reg);
623             }
624 
625             if (ele_type == ID_PCE) {
626               if (ptr_adts_crc_info->str_crc_reg_data[crc_reg].bit_buf_cnt) {
627                 ptr_adts_crc_info->str_crc_reg_data[crc_reg].max_bits =
628                     ptr_adts_crc_info->str_crc_reg_data[crc_reg].bit_buf_cnt;
629               }
630             }
631           }
632 
633           break;
634 
635           case ID_END:
636             error_code = 0;
637             break;
638         }
639 
640         previous_element = ele_type;
641 
642         if (init_flag) {
643           if ((ele_type >= ID_SCE) && (ele_type <= ID_LFE)) {
644             p_obj_exhaacplus_dec->aac_config.element_type[*ch_idx] = ele_type;
645           }
646         }
647       }
648     } else {
649       {
650         switch (ch_config) {
651           default:
652             if (aac_dec_handle->frame_status) {
653               ia_aac_dec_channel_info_struct *pstr_aac_dec_ch_info =
654                   aac_dec_handle->pstr_aac_dec_ch_info[LEFT];
655               ia_ics_info_struct *ptr_ics_info = &pstr_aac_dec_ch_info->str_ics_info;
656 
657               if (ch_config == 2)
658                 ele_ch = 2, ele_type = 1;
659               else
660                 ele_ch = 1, ele_type = 0;
661 
662               prev_data_ele_present = 1;
663 
664               if ((ptr_adts_crc_info->crc_active == 1) && (ptr_adts_crc_info->no_reg < 7)) {
665                 crc_reg = ixheaacd_adts_crc_start_reg(ptr_adts_crc_info, it_bit_buff,
666                                                       CRC_ADTS_RAW_DATA_BLK_LEN);
667               }
668 
669               if (object_type != AOT_ER_AAC_ELD)
670                 pstr_aac_dec_ch_info->element_instance_tag =
671                     (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 4);
672 
673               element_index_order[*ch_idx] = pstr_aac_dec_ch_info->element_instance_tag;
674               pstr_aac_dec_ch_info->common_window = 0;
675 
676               ptr_ics_info->num_swb_window = 0;
677               ptr_ics_info->sampling_rate_index = aac_dec_handle->sampling_rate_index;
678 
679               if (object_type == AOT_ER_AAC_LD) {
680                 ptr_ics_info->ltp.data_present = 0;
681                 ptr_ics_info->ltp2.data_present = 0;
682                 ptr_ics_info->predictor_data_present = 0;
683               }
684               if (ele_ch > 1) {
685                 aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]->str_ics_info.num_swb_window = 0;
686                 aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]->str_ics_info.sampling_rate_index =
687                     aac_dec_handle->sampling_rate_index;
688 
689                 if (object_type != 39)
690                   pstr_aac_dec_ch_info->common_window =
691                       (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 1);
692                 else
693                   pstr_aac_dec_ch_info->common_window = 1;
694 
695                 if (pstr_aac_dec_ch_info->common_window) {
696                   error_code = ixheaacd_ics_read(
697                       it_bit_buff, ptr_ics_info, aac_dec_handle->num_swb_window, object_type,
698                       pstr_aac_dec_ch_info->common_window, aac_dec_handle->samples_per_frame);
699                   if (error_code) {
700                     aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]->str_ics_info =
701                         aac_dec_handle->pstr_aac_dec_ch_info[LEFT]->str_ics_info;
702                     if (it_bit_buff->cnt_bits < 0) {
703                       error_code =
704                           (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
705                     }
706 
707                     goto _ia_handle_error1;
708                   }
709 
710                   aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]->str_ics_info =
711                       pstr_aac_dec_ch_info->str_ics_info;
712 
713                   ixheaacd_read_ms_data(it_bit_buff, pstr_aac_dec_ch_info);
714 
715                   {
716                     if (object_type == AOT_ER_AAC_LD) {
717                       IA_ERRORCODE temp =
718                           ixheaacd_ltp_decode(it_bit_buff, ptr_ics_info, object_type,
719                                               aac_dec_handle->samples_per_frame, LEFT);
720 
721                       if (temp != 0) {
722                         if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && !is_init) {
723                           p_obj_exhaacplus_dec->aac_config.frame_status = 0;
724                         } else {
725                           return temp;
726                         }
727                       }
728                     }
729                   }
730                 }
731               }
732 
733               error_code = ixheaacd_individual_ch_stream(
734                   it_bit_buff, aac_dec_handle, ele_ch, frame_length, total_channels, object_type,
735                   eld_specific_config, ele_type);
736               if (error_code) {
737                 if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && !is_init) {
738                   aac_dec_handle->frame_status = 0;
739                 } else {
740                   return error_code;
741                 }
742               }
743 
744               if (ptr_adts_crc_info->crc_active == 1) {
745                 ixheaacd_adts_crc_end_reg(ptr_adts_crc_info, it_bit_buff, crc_reg);
746               }
747 
748               if (it_bit_buff->cnt_bits < 0) {
749                 error_code =
750                     (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
751               }
752 
753               if (error_code) {
754                 goto _ia_handle_error1;
755               }
756 
757             _ia_handle_error1:
758               if (error_code) {
759                 aac_dec_handle->frame_status = 0;
760                 if ((ele_type >= ID_SCE) && (ele_type <= ID_LFE)) num_ch = num_ch + ele_ch;
761                 break;
762               } else {
763                 error_code = ixheaacd_channel_pair_process(
764                     aac_dec_handle->pstr_aac_dec_ch_info, ele_ch, aac_dec_handle->pstr_aac_tables,
765                     total_channels, object_type, aac_spect_data_resil_flag,
766                     eld_specific_config.aac_sf_data_resil_flag, aac_scratch_ptrs->in_data,
767                     aac_scratch_ptrs->out_data, (VOID *)aac_dec_handle);
768                 if (error_code) {
769                   if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && !is_init) {
770                     aac_dec_handle->frame_status = 0;
771                   } else {
772                     return error_code;
773                   }
774                 }
775                 num_ch = num_ch + ele_ch;
776               }
777             }
778 
779             p_obj_exhaacplus_dec->aac_config.element_type[*ch_idx] = ele_ch - 1;
780             break;
781         }
782 
783         if ((object_type == AOT_ER_AAC_LC) || (!eld_sbr_flag)) {
784           WORD32 cnt_bits;
785           cnt_bits = it_bit_buff->cnt_bits;
786           p_obj_exhaacplus_dec->p_state_aac->mps_dec_handle.ldmps_config.no_ldsbr_present = 1;
787 
788           if (cnt_bits >= 8) {
789             error_code = ixheaacd_extension_payload(
790                 it_bit_buff, &cnt_bits, &p_obj_exhaacplus_dec->p_state_aac->mps_dec_handle);
791             if (error_code) {
792               if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && !is_init) {
793                 aac_dec_handle->frame_status = 0;
794               } else {
795                 return error_code;
796               }
797             }
798           }
799 
800           if (it_bit_buff->cnt_bits) {
801             WORD32 alignment = it_bit_buff->bit_pos & 0x07;
802             it_bit_buff->cnt_bits = (it_bit_buff->cnt_bits + alignment) & 7;
803             it_bit_buff->bit_pos = 7;
804             it_bit_buff->ptr_read_next++;
805           }
806         }
807 
808         else if ((object_type != AOT_ER_AAC_ELD) || (!eld_sbr_flag)) {
809           WORD32 bits_decoded, cnt_bits;
810           bits_decoded = (it_bit_buff->size - it_bit_buff->cnt_bits);
811 
812           cnt_bits = (frame_size * 8 - bits_decoded);
813 
814           if (object_type == AOT_ER_AAC_LC) cnt_bits = it_bit_buff->cnt_bits;
815 
816           p_obj_exhaacplus_dec->p_state_aac->mps_dec_handle.ldmps_config.no_ldsbr_present = 1;
817 
818           if (cnt_bits >= 8) {
819             error_code = ixheaacd_extension_payload(
820                 it_bit_buff, &cnt_bits, &p_obj_exhaacplus_dec->p_state_aac->mps_dec_handle);
821             if (error_code) {
822               if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && !is_init) {
823                 aac_dec_handle->frame_status = 0;
824               } else {
825                 return error_code;
826               }
827             }
828           }
829 
830           if (((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD)) &&
831               (p_obj_exhaacplus_dec->aac_config.ld_decoder != 1)) {
832             if (it_bit_buff->cnt_bits) {
833               WORD32 alignment = it_bit_buff->bit_pos & 0x07;
834               it_bit_buff->cnt_bits = (it_bit_buff->cnt_bits + alignment) & 7;
835               it_bit_buff->bit_pos = 7;
836               it_bit_buff->ptr_read_next++;
837             }
838           } else {
839             if (it_bit_buff->bit_pos != 7) {
840               WORD32 alignment = it_bit_buff->bit_pos & 0x07;
841               it_bit_buff->cnt_bits -= alignment + 1;
842               it_bit_buff->bit_pos += 7 - alignment;
843               it_bit_buff->ptr_read_next++;
844             }
845           }
846         } else {
847           WORD32 bits_decoded, cnt_bits;
848           bits_decoded = (it_bit_buff->size - it_bit_buff->cnt_bits);
849           cnt_bits = (frame_size * 8 - bits_decoded);
850           if (adtsheader == 1) {
851             if (cnt_bits > it_bit_buff->cnt_bits) {
852               if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && !is_init) {
853                 aac_dec_handle->frame_status = 0;
854               } else {
855                 return IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES;
856               }
857             }
858           }
859           ixheaacd_check_for_sbr_payload(
860               it_bit_buff, aac_dec_handle->pstr_sbr_bitstream, (WORD16)(ch_config - 1),
861               pstr_drc_dec, object_type, adtsheader, cnt_bits, ld_sbr_crc_flag, drc_dummy,
862               mps_buffer, mps_header, mps_bytes, is_init, &aac_dec_handle->is_first,
863               p_obj_exhaacplus_dec->aac_config.ui_err_conceal);
864         }
865       }
866     }
867   }
868   if ((err || (aac_dec_handle->frame_status == 0) || (frame_status == 0)) && (!is_init)) {
869     if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
870       aac_dec_handle->frame_status = 0;
871       error_code = 0;
872       num_ch = channel;
873       ele_type = ID_END;
874       p_obj_exhaacplus_dec->aac_config.frame_status = 0;
875       it_bit_buff->cnt_bits = 0;
876       aac_dec_handle->byte_align_bits = 0;
877     } else {
878       return err;
879     }
880   }
881   if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && aac_dec_handle->conceal_count == 0) {
882     for (ch = 0; ch < channel; ch++) {
883       ixheaacd_aac_ec_init(&aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->str_ec_state);
884     }
885   }
886 
887   if (ele_type == ID_END && p_state_enhaacplus_dec->bs_format == LOAS_BSFORMAT) {
888     WORD32 tmp;
889     tmp = ((WORD32)latm_element->layer_info[0][0].frame_len_bits) -
890           (it_bit_buff->initial_cnt_bits - it_bit_buff->cnt_bits);
891 
892     if (tmp > 0) ixheaacd_read_bidirection(it_bit_buff, tmp);
893 
894     if (latm_element->other_data_present) {
895       WORD32 count_bits = (WORD32)latm_element->other_data_length;
896       ixheaacd_read_bidirection(it_bit_buff, count_bits);
897     }
898   }
899 
900   if (object_type == AOT_ER_AAC_LD) {
901     for (ch = 0; ch < channel; ch++) {
902       aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ltp_lag_1 =
903           aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info.ltp2.lag;
904       aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ltp_lag_2 =
905           aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info.ltp.lag;
906     }
907   }
908   aac_dec_handle->frame_status = aac_dec_handle->frame_status && frame_status;
909 
910   aac_dec_handle->channels = num_ch;
911 
912   if (error_code == 0)
913     if ((skip_full_decode == 0) || ((skip_full_decode == 1) && error_code)) {
914       ia_ics_info_struct str_ics_info[2];
915       WORD32 *spec_coef[2];
916       WORD32 *scratch[2];
917 
918       for (ch = 0; ch < channel; ch++) {
919         str_ics_info[ch] = aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info;
920         spec_coef[ch] = aac_dec_handle->pstr_aac_dec_ch_info[ch]->ptr_spec_coeff;
921       }
922 
923       scratch[0] = (WORD32 *)aac_scratch_ptrs->extra_scr_4k[2];
924       scratch[1] = (WORD32 *)aac_scratch_ptrs->extra_scr_4k[1];
925 
926       error_code = ixheaacd_drc_map_channels(
927           pstr_drc_dec, aac_dec_handle->channels,
928           aac_dec_handle->pstr_aac_dec_ch_info[0]->str_ics_info.frame_length);
929       if (error_code) {
930         if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && !is_init) {
931           error_code = 0;
932           aac_dec_handle->frame_status = 0;
933         } else {
934           return error_code;
935         }
936       }
937 
938       for (ch = 0; ch < aac_dec_handle->channels; ch++) {
939         WORD32 *overlap1 =
940             aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->overlap_add_data.ptr_overlap_buf;
941         const WORD16 *ptr_long_window_next =
942             aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
943                 ->ptr_long_window[(int)str_ics_info[ch].window_shape];
944         const WORD16 *ptr_short_window_next =
945             aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
946                 ->ptr_short_window[(int)str_ics_info[ch].window_shape];
947         if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
948           ia_aac_dec_channel_info_struct *pstr_aac_dec_channel_info =
949               aac_dec_handle->pstr_aac_dec_ch_info[ch];
950           ia_aac_dec_channel_info *pstr_aac_dec_static_channel_info =
951               aac_dec_handle->ptr_aac_dec_static_channel_info[ch];
952 
953           ia_aac_dec_channel_info_struct **ppstr_aac_dec_channel_info =
954               &pstr_aac_dec_channel_info;
955           ia_aac_dec_channel_info **ppstr_aac_dec_static_channel_info =
956               &pstr_aac_dec_static_channel_info;
957           ia_audio_specific_config_struct *pstr_audio_specific_config;
958           pstr_audio_specific_config = p_state_enhaacplus_dec->ia_audio_specific_config;
959 
960           if (str_ics_info[ch].max_sfb > str_ics_info[ch].num_swb_window) {
961             aac_dec_handle->frame_status = 0;
962           }
963 
964           ixheaacd_aac_apply_ec(
965               &(*ppstr_aac_dec_static_channel_info)->str_ec_state, *ppstr_aac_dec_channel_info,
966               &ixheaacd_samp_rate_info[pstr_audio_specific_config->samp_frequency_index],
967               aac_dec_handle->samples_per_frame, &str_ics_info[ch], aac_dec_handle->frame_status);
968 
969           aac_dec_handle->conceal_count = aac_dec_handle->conceal_count + 1;
970           if (aac_dec_handle->frame_status) {
971             aac_dec_handle->sbr_num_elements = aac_dec_handle->pstr_sbr_bitstream->no_elements;
972           } else {
973             aac_dec_handle->pstr_sbr_bitstream->no_elements = aac_dec_handle->sbr_num_elements;
974           }
975           if (first_frame == 1)
976             skip_full_decode = 1;
977           else
978             skip_full_decode = 0;
979         }
980         if (pstr_drc_dec->drc_on) {
981           ixheaacd_drc_apply(pstr_drc_dec, spec_coef[ch],
982                              str_ics_info[ch].window_sequence, ch,
983                              str_ics_info[ch].frame_length,
984                              p_obj_exhaacplus_dec->aac_config.ui_enh_sbr, object_type);
985         }
986         if (skip_full_decode == 0) {
987           ixheaacd_imdct_process(aac_dec_handle->pstr_aac_dec_overlap_info[ch],
988                                  spec_coef[ch], &str_ics_info[ch],
989                                  time_data + slot_element, ch_fac, scratch[ch],
990                                  aac_dec_handle->pstr_aac_tables, object_type,
991                                  ldmps_present, slot_element);
992 
993           if (slot_pos != NULL) *slot_pos = slot_element;
994           if (p_obj_exhaacplus_dec->p_state_aac->qshift_cnt > 15) {
995             return IA_FATAL_ERROR;
996           }
997 
998           p_obj_exhaacplus_dec->p_state_aac
999               ->qshift_adj[p_obj_exhaacplus_dec->p_state_aac->qshift_cnt++] =
1000               str_ics_info[ch].qshift_adj;
1001 
1002           aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
1003               ->overlap_add_data.win_shape = str_ics_info[ch].window_shape;
1004           aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
1005               ->overlap_add_data.win_seq = str_ics_info[ch].window_sequence;
1006           if ((object_type == AOT_ER_AAC_LD) || (object_type == AOT_AAC_LTP)) {
1007             {
1008               if ((str_ics_info[ch].window_sequence == ONLY_LONG_SEQUENCE) ||
1009                   (str_ics_info[ch].window_sequence == LONG_STOP_SEQUENCE)) {
1010                 ixheaacd_lt_update_state(
1011                     aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
1012                         ->ltp_buf,
1013                     time_data + slot_element, overlap1,
1014                     aac_dec_handle->samples_per_frame, object_type,
1015                     (WORD16)ch_fac, str_ics_info[ch].window_sequence,
1016                     (WORD16 *)ptr_long_window_next, slot_element);
1017               } else {
1018                 ixheaacd_lt_update_state(
1019                     aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
1020                         ->ltp_buf,
1021                     time_data + slot_element, overlap1,
1022                     aac_dec_handle->samples_per_frame, object_type,
1023                     (WORD16)ch_fac, str_ics_info[ch].window_sequence,
1024                     (WORD16 *)ptr_short_window_next, slot_element);
1025               }
1026             }
1027           }
1028           if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
1029             ia_aac_dec_channel_info *pstr_aac_dec_static_channel_info =
1030                 aac_dec_handle->ptr_aac_dec_static_channel_info[ch];
1031             ia_ec_state_str *pstr_ec_state = &pstr_aac_dec_static_channel_info->str_ec_state;
1032             WORD32 k;
1033 
1034             if (pstr_ec_state->fade_idx < MAX_FADE_FRAMES) {
1035               WORD32 fade_fac = ia_ec_fade_factors_fix[pstr_ec_state->fade_idx];
1036               for (k = 0; k < str_ics_info[ch].frame_length; k++) {
1037                 time_data[k] = ixheaac_mul32_sh(time_data[k], fade_fac, 30);
1038               }
1039             } else {
1040               memset(time_data, 0, str_ics_info[ch].frame_length * sizeof(time_data[0]));
1041             }
1042           }
1043           slot_element++;
1044         }
1045       }
1046     }
1047 
1048   if (ele_type == ID_END) {
1049     if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal) {
1050       if (err && !is_init) {
1051         aac_dec_handle->frame_status = 0;
1052       } else {
1053         ixheaacd_byte_align(it_bit_buff, &aac_dec_handle->byte_align_bits);
1054         if (p_state_enhaacplus_dec->bs_format == LOAS_BSFORMAT) {
1055           ixheaacd_byte_align(it_bit_buff, &it_bit_buff->audio_mux_align);
1056         }
1057       }
1058     } else {
1059       ixheaacd_byte_align(it_bit_buff, &aac_dec_handle->byte_align_bits);
1060       if (p_state_enhaacplus_dec->bs_format == LOAS_BSFORMAT) {
1061         ixheaacd_byte_align(it_bit_buff, &it_bit_buff->audio_mux_align);
1062       }
1063     }
1064   }
1065   *type = ele_type;
1066 
1067   aac_dec_handle->block_number =
1068       ixheaac_add32(aac_dec_handle->block_number, 1);
1069   if (p_obj_exhaacplus_dec->aac_config.ui_err_conceal && !is_init) {
1070     p_obj_exhaacplus_dec->aac_config.frame_status = aac_dec_handle->frame_status;
1071     return IA_NO_ERROR;
1072   } else {
1073     return error_code;
1074   }
1075 }
1076 
ixheaacd_extension_payload(ia_bit_buf_struct * it_bit_buff,WORD32 * cnt,ia_mps_dec_state_struct * self)1077 WORD32 ixheaacd_extension_payload(ia_bit_buf_struct *it_bit_buff, WORD32 *cnt,
1078                                   ia_mps_dec_state_struct *self) {
1079   WORD16 extension_type;
1080   WORD32 len, add_len;
1081   WORD32 i;
1082   WORD32 fill_nibble;
1083 
1084   WORD32 err = 0;
1085   extension_type = (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 4);
1086   switch (extension_type) {
1087     case EXT_FILL_DATA:
1088 
1089       fill_nibble = ixheaacd_read_bits_buf(it_bit_buff, 4);
1090 
1091       if (fill_nibble == 0) {
1092         for (i = 0; i < (*cnt >> 3) - 1; i++) {
1093           if (it_bit_buff->cnt_bits >= 8)
1094             ixheaacd_read_bits_buf(it_bit_buff, 8);
1095           else
1096             ixheaacd_read_bits_buf(it_bit_buff, it_bit_buff->cnt_bits);
1097         }
1098 
1099       } else
1100           err = -1;
1101       *cnt = it_bit_buff->cnt_bits;
1102       break;
1103 
1104     case EXT_DATA_LENGTH:
1105 
1106       len = ixheaacd_read_bits_buf(it_bit_buff, 4);
1107 
1108       if (len == 15) {
1109         add_len = ixheaacd_read_bits_buf(it_bit_buff, 8);
1110         len += add_len;
1111 
1112         if (add_len == 255) {
1113           len += ixheaacd_read_bits_buf(it_bit_buff, 16);
1114         }
1115       }
1116       len <<= 3;
1117 
1118       ixheaacd_extension_payload(it_bit_buff, cnt, self);
1119       break;
1120 
1121     case EXT_LDSAC_DATA:
1122 
1123       self->parse_nxt_frame = 1;
1124       ixheaacd_read_bits_buf(it_bit_buff, 2);/*anc_type*/
1125       ixheaacd_read_bits_buf(it_bit_buff, 2);/*anc_start_stop*/
1126 
1127       if (self->ldmps_config.ldmps_present_flag == 1) {
1128         err = ixheaacd_ld_mps_frame_parsing(self, it_bit_buff);
1129         if (err) return err;
1130       }
1131 
1132       *cnt = it_bit_buff->cnt_bits;
1133       break;
1134     case EXT_FIL:
1135     default:
1136 
1137       for (i = 0; i < (*cnt) - 4; i++) {
1138          ixheaacd_skip_bits_buf(it_bit_buff, 1);/*discard*/
1139       }
1140 
1141       *cnt = it_bit_buff->cnt_bits;
1142       break;
1143   }
1144 
1145   return err;
1146 }
1147