• 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 <stdio.h>
21 #include <string.h>
22 #include "ixheaacd_sbr_common.h"
23 #include "ixheaacd_type_def.h"
24 
25 #include "ixheaacd_constants.h"
26 #include "ixheaacd_basic_ops32.h"
27 #include "ixheaacd_basic_ops16.h"
28 #include "ixheaacd_basic_ops40.h"
29 #include "ixheaacd_basic_ops.h"
30 #include "ixheaacd_bitbuffer.h"
31 
32 #include "ixheaacd_basic_op.h"
33 #include "ixheaacd_intrinsics.h"
34 
35 #include "ixheaacd_defines.h"
36 
37 #include "ixheaacd_aac_rom.h"
38 
39 #include "ixheaacd_definitions.h"
40 
41 #include "ixheaacd_error_codes.h"
42 
43 #include "ixheaacd_pulsedata.h"
44 
45 #include "ixheaacd_pns.h"
46 #include "ixheaacd_drc_data_struct.h"
47 
48 #include "ixheaacd_lt_predict.h"
49 
50 #include "ixheaacd_channelinfo.h"
51 #include "ixheaacd_cnst.h"
52 #include "ixheaacd_drc_dec.h"
53 #include "ixheaacd_sbrdecoder.h"
54 #include "ixheaacd_block.h"
55 #include "ixheaacd_channel.h"
56 
57 #include "ixheaacd_sbr_payload.h"
58 #include "ixheaacd_common_rom.h"
59 #include "ixheaacd_sbrdecsettings.h"
60 #include "ixheaacd_sbr_scale.h"
61 #include "ixheaacd_env_extr_part.h"
62 #include "ixheaacd_sbr_rom.h"
63 #include "ixheaacd_stereo.h"
64 #include "ixheaacd_lpp_tran.h"
65 #include "ixheaacd_hybrid.h"
66 #include "ixheaacd_ps_dec.h"
67 #include "ixheaacd_env_extr.h"
68 #include "ixheaacd_adts.h"
69 #include "ixheaacd_audioobjtypes.h"
70 #include "ixheaacd_memory_standards.h"
71 #include "ixheaacd_latmdemux.h"
72 #include "ixheaacd_aacdec.h"
73 #include "ixheaacd_mps_polyphase.h"
74 #include "ixheaacd_config.h"
75 #include "ixheaacd_mps_dec.h"
76 #include "ixheaacd_struct_def.h"
77 #include "ixheaacd_headerdecode.h"
78 #include "ixheaacd_multichannel.h"
79 #include "ixheaacd_adts_crc_check.h"
80 
81 #include "ixheaacd_hcr.h"
82 
83 #define SIZEOF_INT(x) ((sizeof(x) + sizeof(WORD32) - 1) / sizeof(WORD32))
84 
85 #define EXT_FILL_DATA 1
86 #define EXT_FIL 0
87 
ixheaacd_aacdec_decodeframe(ia_exhaacplus_dec_api_struct * p_obj_exhaacplus_dec,ia_aac_dec_scratch_struct * aac_scratch_ptrs,WORD16 * time_data,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)88 WORD32 ixheaacd_aacdec_decodeframe(
89     ia_exhaacplus_dec_api_struct *p_obj_exhaacplus_dec,
90     ia_aac_dec_scratch_struct *aac_scratch_ptrs, WORD16 *time_data,
91     FLAG frame_status, WORD *type, WORD *ch_idx, WORD init_flag, WORD channel,
92     WORD *element_index_order, WORD skip_full_decode, WORD ch_fac,
93     WORD slot_element, WORD max_channels, WORD32 total_channels,
94     WORD32 frame_length, WORD32 frame_size, ia_drc_dec_struct *pstr_drc_dec,
95     WORD32 object_type, WORD32 ch_config,
96     ia_eld_specific_config_struct eld_specific_config, WORD16 adtsheader,
97     ia_drc_dec_struct *drc_dummy)
98 
99 {
100   WORD ch, ele_type;
101   ia_aac_dec_state_struct *p_state_enhaacplus_dec;
102   ia_aac_decoder_struct *aac_dec_handle;
103   ia_bit_buf_struct *it_bit_buff;
104   ixheaacd_latm_struct *latm_element;
105 
106   WORD error_code = (WORD)frame_status;
107   WORD previous_element;
108   WORD prev_data_ele_present = 0;
109   WORD new_element;
110   WORD32 num_ch = 0;
111 
112   WORD32 crc_reg = 0;
113   ia_adts_crc_info_struct *ptr_adts_crc_info;
114 
115   WORD32 cnt_bits = 0;
116 
117   WORD32 eld_sbr_flag = eld_specific_config.ld_sbr_flag_present;
118   WORD32 ld_sbr_crc_flag = eld_specific_config.ld_sbr_crc_flag;
119   WORD32 aac_spect_data_resil_flag =
120       eld_specific_config.aac_spect_data_resil_flag;
121 
122   WORD32 ele_ch = 0;
123 
124   ia_aac_sfb_code_book_struct *ptr_aac_sfb_code_book_data[CHANNELS];
125   ia_pns_stereo_data_struct *ptr_pns_stereo_data;
126 
127   WORD32 *work_buffer_core = aac_scratch_ptrs->base_scr_8k;
128   WORD32 *work_buffer_1 = aac_scratch_ptrs->extra_scr_4k[0];
129   WORD32 *work_buffer_2 = aac_scratch_ptrs->extra_scr_4k[2];
130   p_state_enhaacplus_dec = p_obj_exhaacplus_dec->p_state_aac;
131 
132   aac_dec_handle = p_state_enhaacplus_dec->pstr_aac_dec_info[*ch_idx];
133   it_bit_buff = p_state_enhaacplus_dec->ptr_bit_stream;
134 
135   ptr_adts_crc_info = it_bit_buff->pstr_adts_crc_info;
136 
137   latm_element = &p_state_enhaacplus_dec->latm_struct_element;
138 
139   ptr_pns_stereo_data =
140       (ia_pns_stereo_data_struct
141            *)&work_buffer_1[2 * SIZEOF_INT(ia_aac_dec_channel_info_struct) +
142                             2 * SIZEOF_INT(ia_aac_sfb_code_book_struct)];
143 
144   aac_dec_handle->frame_status = 1;
145 
146   for (ch = 0; ch < channel; ch++) {
147     const ia_aac_dec_imdct_tables_struct *pstr_imdct_tables;
148     aac_dec_handle->pstr_aac_dec_ch_info[ch] =
149         (ia_aac_dec_channel_info_struct
150              *)&work_buffer_1[ch * SIZEOF_INT(ia_aac_dec_channel_info_struct)];
151     ptr_aac_sfb_code_book_data[ch] =
152         (ia_aac_sfb_code_book_struct
153              *)&work_buffer_1[2 * SIZEOF_INT(ia_aac_dec_channel_info_struct) +
154                               (ch * SIZEOF_INT(ia_aac_sfb_code_book_struct))];
155 
156     aac_dec_handle->pstr_aac_dec_ch_info[ch]->ptr_scale_factor =
157         ptr_aac_sfb_code_book_data[ch]->scale_factor;
158     aac_dec_handle->pstr_aac_dec_ch_info[ch]->ptr_code_book =
159         ptr_aac_sfb_code_book_data[ch]->code_book;
160 
161     aac_dec_handle->pstr_aac_dec_ch_info[ch]->ptr_spec_coeff =
162         &work_buffer_core[ch * MAX_BINS_LONG];
163 
164     if (object_type == AOT_ER_AAC_ELD) {
165       aac_dec_handle->pstr_aac_dec_ch_info[ch]->ptr_spec_coeff =
166           &work_buffer_core[2 * ch * MAX_BINS_LONG];
167     }
168 
169     aac_dec_handle->pstr_aac_dec_ch_info[ch]->pstr_stereo_info =
170         &ptr_pns_stereo_data->str_stereo_info;
171     aac_dec_handle->pstr_aac_dec_ch_info[ch]->pstr_pns_corr_info =
172         &ptr_pns_stereo_data->str_pns_corr_info;
173     aac_dec_handle->pstr_aac_dec_ch_info[ch]->pstr_pns_rand_vec_data =
174         aac_dec_handle->pstr_pns_rand_vec_data;
175 
176     pstr_imdct_tables = aac_dec_handle->pstr_aac_tables->pstr_imdct_tables;
177 
178     aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[0] =
179         pstr_imdct_tables->only_long_window_sine;
180     aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_short_window[0] =
181         pstr_imdct_tables->only_short_window_sine;
182     aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[1] =
183         pstr_imdct_tables->only_long_window_kbd;
184     aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_short_window[1] =
185         pstr_imdct_tables->only_short_window_kbd;
186 
187     aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ptr_long_window[0] =
188         pstr_imdct_tables->only_long_window_sine;
189     aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ptr_short_window[0] =
190         pstr_imdct_tables->only_short_window_sine;
191     aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ptr_long_window[1] =
192         pstr_imdct_tables->only_long_window_kbd;
193     aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ptr_short_window[1] =
194         pstr_imdct_tables->only_short_window_kbd;
195 
196     if (object_type == AOT_ER_AAC_ELD || object_type == AOT_ER_AAC_LD ||
197         object_type == AOT_AAC_LTP) {
198       aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info.frame_length =
199           frame_length;
200 
201       if (512 == aac_dec_handle->samples_per_frame) {
202         if (object_type != AOT_ER_AAC_ELD) {
203           aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[1] =
204               (WORD16 *)pstr_imdct_tables->low_overlap_win;
205           aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[0] =
206               (WORD16 *)pstr_imdct_tables->window_sine_512;
207 
208           aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
209               ->ptr_long_window[1] =
210               (WORD16 *)pstr_imdct_tables->low_overlap_win;
211           aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
212               ->ptr_long_window[0] =
213               (WORD16 *)pstr_imdct_tables->window_sine_512;
214         } else {
215           aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[1] =
216               (WORD16 *)pstr_imdct_tables->window_sine_512_eld;
217           aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[0] =
218               (WORD16 *)pstr_imdct_tables->window_sine_512_eld;
219         }
220       } else if (480 == aac_dec_handle->samples_per_frame) {
221         if (object_type != AOT_ER_AAC_ELD) {
222           aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[1] =
223               (WORD16 *)pstr_imdct_tables->low_overlap_win_480;
224           aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[0] =
225               (WORD16 *)pstr_imdct_tables->window_sine_480;
226 
227           aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
228               ->ptr_long_window[1] =
229               (WORD16 *)pstr_imdct_tables->low_overlap_win_480;
230           aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
231               ->ptr_long_window[0] =
232               (WORD16 *)pstr_imdct_tables->window_sine_480;
233 
234         } else {
235           aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[1] =
236               (WORD16 *)pstr_imdct_tables->window_sine_480_eld;
237           aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[0] =
238               (WORD16 *)pstr_imdct_tables->window_sine_480_eld;
239         }
240       }
241     }
242     if ((object_type == AOT_ER_AAC_LD) || (object_type == AOT_AAC_LTP)) {
243       if (aac_dec_handle->samples_per_frame <= 512) {
244         aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info.ltp.lag =
245             aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ltp_lag;
246       }
247       aac_dec_handle->pstr_aac_dec_ch_info[ch]->ltp_buf =
248           aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ltp_buf;
249       aac_dec_handle->pstr_aac_dec_ch_info[ch]->ltp_lag =
250           aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ltp_lag;
251     }
252 
253     aac_dec_handle->pstr_aac_dec_ch_info[ch]->scratch_buf_ptr = work_buffer_2;
254     if (object_type == AOT_ER_AAC_ELD) {
255       aac_dec_handle->pstr_aac_dec_ch_info[ch]->pulse_scratch =
256           aac_scratch_ptrs->extra_scr_4k[3];
257     }
258   }
259 
260   if (channel == 2) {
261     if (aac_dec_handle->pstr_aac_dec_ch_info[1]->ptr_spec_coeff ==
262         aac_scratch_ptrs->extra_scr_4k[0]) {
263       aac_dec_handle->pstr_aac_dec_ch_info[1]->ptr_spec_coeff =
264           aac_dec_handle->pstr_aac_dec_ch_info[0]->ptr_spec_coeff;
265     }
266   }
267 
268   for (ch = 0; ch < channel; ch++) {
269     ia_pns_info_struct *ptr_pns_info =
270         &aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_pns_info;
271     memset(ptr_pns_info, 0, sizeof(ia_pns_info_struct));
272   }
273 
274   {
275     ia_pns_correlation_info_struct *ptr_corr_info =
276         aac_dec_handle->pstr_aac_dec_ch_info[0]->pstr_pns_corr_info;
277     memset(ptr_corr_info->correlated, 0, sizeof(UWORD8) * PNS_BAND_FLAGS_SIZE);
278   }
279 
280   for (ch = 0; ch < channel; ch++) {
281     memset(&aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_hcr_info, 0,
282            sizeof(ia_hcr_info_struct));
283     ixheaacd_huff_code_reorder_tbl_init(
284         &aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_hcr_info);
285   }
286 
287   for (ch = 0; ch < channel; ch++) {
288     aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info.ltp.data_present = 0;
289     aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info.ltp2.data_present =
290         0;
291   }
292 
293   previous_element = ID_END;
294 
295   aac_dec_handle->pstr_sbr_bitstream->no_elements = 0;
296   new_element = 0;
297   ele_type = *type;
298 
299   cnt_bits = it_bit_buff->cnt_bits;
300 
301   if (((object_type != AOT_ER_AAC_ELD) && (object_type != AOT_ER_AAC_LD)) ||
302       (object_type < ER_OBJECT_START)) {
303     while (ele_type != ID_END && aac_dec_handle->frame_status) {
304       ele_type = (WORD)ixheaacd_read_bits_buf(it_bit_buff, 3);
305       ixheaacd_read_bidirection(it_bit_buff, -3);
306 
307       if (it_bit_buff->cnt_bits < 3) {
308         it_bit_buff->cnt_bits = -1;
309         error_code = (WORD16)(
310             (WORD32)IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
311         break;
312       }
313 
314       if ((ele_type == ID_FIL) || (ele_type == ID_DSE) || (new_element == 0)) {
315         ele_type = (WORD)ixheaacd_read_bits_buf(it_bit_buff, 3);
316         new_element = 1;
317       } else if ((ele_type != ID_END)) {
318         ele_type = -1;
319         break;
320       } else {
321         ele_type = (WORD)ixheaacd_read_bits_buf(it_bit_buff, 3);
322       }
323 
324       if (it_bit_buff->cnt_bits < 0) {
325         aac_dec_handle->frame_status = 0;
326       }
327 
328       switch (ele_type) {
329         case ID_SCE:
330         case ID_CPE:
331         case ID_LFE:
332 
333           if (aac_dec_handle->frame_status) {
334             ia_aac_dec_channel_info_struct *pstr_aac_dec_ch_info =
335                 aac_dec_handle->pstr_aac_dec_ch_info[LEFT];
336             ia_ics_info_struct *ptr_ics_info =
337                 &pstr_aac_dec_ch_info->str_ics_info;
338             ele_ch = 1;
339             if (ele_type == ID_CPE) {
340               ele_ch = 2;
341             } else {
342               ele_ch = 1;
343             }
344 
345             prev_data_ele_present = 1;
346 
347             if (ptr_adts_crc_info->crc_active == 1 &&
348                 ptr_adts_crc_info->no_reg < 7) {
349               crc_reg = ixheaacd_adts_crc_start_reg(
350                   ptr_adts_crc_info, it_bit_buff, CRC_ADTS_RAW_DATA_BLK_LEN);
351             }
352 
353             pstr_aac_dec_ch_info->element_instance_tag =
354                 (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 4);
355 
356             element_index_order[*ch_idx] =
357                 pstr_aac_dec_ch_info->element_instance_tag;
358             pstr_aac_dec_ch_info->common_window = 0;
359 
360             ptr_ics_info->num_swb_window = 0;
361             ptr_ics_info->sampling_rate_index =
362                 aac_dec_handle->sampling_rate_index;
363             if ((object_type == AOT_ER_AAC_LD) ||
364                 (object_type == AOT_AAC_LTP)) {
365               ptr_ics_info->ltp.data_present = 0;
366               ptr_ics_info->ltp2.data_present = 0;
367               ptr_ics_info->predictor_data_present = 0;
368             }
369 
370             if (ele_ch > 1) {
371               aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]
372                   ->str_ics_info.num_swb_window = 0;
373               aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]
374                   ->str_ics_info.sampling_rate_index =
375                   aac_dec_handle->sampling_rate_index;
376 
377               pstr_aac_dec_ch_info->common_window =
378                   (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 1);
379 
380               if (pstr_aac_dec_ch_info->common_window) {
381                 error_code = ixheaacd_ics_read(
382                     it_bit_buff, ptr_ics_info, aac_dec_handle->num_swb_window,
383                     object_type, pstr_aac_dec_ch_info->common_window,
384                     aac_dec_handle->samples_per_frame);
385                 if (error_code) {
386                   if (it_bit_buff->cnt_bits < 0) {
387                     error_code = (WORD16)(
388                         (WORD32)
389                             IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
390                   }
391 
392                   goto _ia_handle_error;
393                 }
394 
395                 aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]->str_ics_info =
396                     pstr_aac_dec_ch_info->str_ics_info;
397 
398                 ixheaacd_read_ms_data(it_bit_buff, pstr_aac_dec_ch_info);
399               }
400             }
401 
402             error_code = ixheaacd_individual_ch_stream(
403                 it_bit_buff, aac_dec_handle, ele_ch, frame_length,
404                 total_channels, object_type, eld_specific_config, ele_type);
405             if (error_code) return error_code;
406 
407             if (ptr_adts_crc_info->crc_active == 1) {
408               ixheaacd_adts_crc_end_reg(ptr_adts_crc_info, it_bit_buff,
409                                         crc_reg);
410             }
411 
412             if (it_bit_buff->cnt_bits < 0) {
413               error_code = (WORD16)(
414                   (WORD32)
415                       IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
416             }
417 
418             if (error_code) {
419               goto _ia_handle_error;
420             }
421 
422           _ia_handle_error:
423             if (error_code) {
424               aac_dec_handle->frame_status = 0;
425               if ((ele_type >= ID_SCE) && (ele_type <= ID_LFE))
426                 num_ch = num_ch + ele_ch;
427               break;
428             } else {
429               error_code = ixheaacd_channel_pair_process(
430                   aac_dec_handle->pstr_aac_dec_ch_info, ele_ch,
431                   aac_dec_handle->pstr_aac_tables, total_channels, object_type,
432                   aac_spect_data_resil_flag,
433                   eld_specific_config.aac_sf_data_resil_flag,
434                   aac_scratch_ptrs->in_data, aac_scratch_ptrs->out_data,
435                   (void *)aac_dec_handle);
436               if (error_code) return error_code;
437               num_ch = num_ch + ele_ch;
438             }
439           }
440 
441           break;
442         case ID_CCE:
443           if (max_channels > 2) {
444             prev_data_ele_present = 1;
445             error_code = ixheaacd_dec_coupling_channel_element(
446                 it_bit_buff, aac_dec_handle,
447                 aac_dec_handle->sampling_rate_index,
448                 aac_dec_handle->pstr_aac_tables,
449                 aac_dec_handle->pstr_common_tables,
450                 &element_index_order[*ch_idx],
451                 (ia_enhaacplus_dec_ind_cc *)aac_dec_handle->p_ind_channel_info,
452                 total_channels, frame_length, object_type, eld_specific_config,
453                 ele_type);
454 
455             num_ch = num_ch + 1;
456 
457             if (error_code) {
458               aac_dec_handle->frame_status = 0;
459               return error_code;
460             } else {
461               error_code = ixheaacd_channel_pair_process(
462                   aac_dec_handle->pstr_aac_dec_ch_info, 1,
463                   aac_dec_handle->pstr_aac_tables, total_channels, object_type,
464                   aac_spect_data_resil_flag,
465                   eld_specific_config.aac_sf_data_resil_flag,
466                   aac_scratch_ptrs->in_data, aac_scratch_ptrs->out_data,
467                   (void *)aac_dec_handle);
468               if (error_code) return error_code;
469             }
470           } else {
471             error_code =
472                 (WORD32)((WORD32)IA_ENHAACPLUS_DEC_EXE_FATAL_UNIMPLEMENTED_CCE);
473           }
474           if (it_bit_buff->cnt_bits < 0) {
475             error_code = (WORD16)((
476                 WORD32)IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
477             goto _ia_handle_error;
478           }
479           break;
480 
481         case ID_DSE:
482         case ID_PCE:
483         case ID_FIL:
484 
485         {
486           WORD32 flag = 1;
487 
488           if ((ele_type != ID_FIL) && (ptr_adts_crc_info->crc_active == 1) &&
489               (ptr_adts_crc_info->no_reg < 7)) {
490             crc_reg =
491                 ixheaacd_adts_crc_start_reg(ptr_adts_crc_info, it_bit_buff, 0);
492           }
493           if (ele_type == ID_DSE) {
494             ixheaacd_read_data_stream_element(
495                 it_bit_buff, &aac_dec_handle->byte_align_bits,
496                 p_obj_exhaacplus_dec->p_state_aac->pstr_drc_dec);
497           }
498 
499           else if (ele_type == ID_PCE) {
500             error_code = ixheaacd_decode_pce(
501                 it_bit_buff,
502                 &p_obj_exhaacplus_dec->aac_config.ui_pce_found_in_hdr,
503                 &p_obj_exhaacplus_dec->aac_config.str_prog_config);
504             if (error_code != 0) {
505               if (it_bit_buff->cnt_bits < 0) {
506                 error_code = (WORD16)(
507                     (WORD32)
508                         IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
509                 goto _ia_handle_error;
510               }
511               aac_dec_handle->frame_status = 0;
512               if (error_code > 0) {
513                 error_code = IA_ENHAACPLUS_DEC_EXE_NONFATAL_DECODE_FRAME_ERROR;
514                 return error_code;
515               } else {
516                 return error_code;
517               }
518             }
519           }
520 
521           else if (ele_type == ID_FIL) {
522             WORD32 bits_decoded = 0;
523             if (object_type == AOT_ER_AAC_ELD) {
524               bits_decoded = (it_bit_buff->size - it_bit_buff->cnt_bits);
525               cnt_bits = (frame_size * 8 - bits_decoded);
526               if (adtsheader == 1) {
527                 if (cnt_bits > it_bit_buff->cnt_bits)
528                   return IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES;
529               }
530             }
531 
532             if (ixheaacd_check_for_sbr_payload(
533                     it_bit_buff, aac_dec_handle->pstr_sbr_bitstream,
534                     (WORD16)previous_element, pstr_drc_dec, object_type,
535                     adtsheader, cnt_bits, ld_sbr_crc_flag, drc_dummy)) {
536               flag = 0;
537             }
538           }
539 
540           if (it_bit_buff->cnt_bits < 0) {
541             error_code = (WORD16)((
542                 WORD32)IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
543             goto _ia_handle_error;
544           }
545 
546           if (flag) {
547             if (prev_data_ele_present == 0) {
548               new_element = 0;
549             }
550           }
551           if ((ele_type != ID_FIL) && (ptr_adts_crc_info->crc_active == 1)) {
552             ixheaacd_adts_crc_end_reg(ptr_adts_crc_info, it_bit_buff, crc_reg);
553           }
554 
555           if (ele_type == ID_PCE) {
556             if (ptr_adts_crc_info->str_crc_reg_data[crc_reg].bit_buf_cnt) {
557               ptr_adts_crc_info->str_crc_reg_data[crc_reg].max_bits =
558                   ptr_adts_crc_info->str_crc_reg_data[crc_reg].bit_buf_cnt;
559             }
560           }
561         }
562 
563         break;
564 
565         case ID_END:
566           error_code = 0;
567           break;
568       }
569 
570       previous_element = ele_type;
571 
572       if (init_flag) {
573         if ((ele_type >= ID_SCE) && (ele_type <= ID_LFE)) {
574           p_obj_exhaacplus_dec->aac_config.element_type[*ch_idx] = ele_type;
575         }
576       }
577     }
578   } else {
579     {
580       switch (ch_config) {
581         default:
582           if (aac_dec_handle->frame_status) {
583             ia_aac_dec_channel_info_struct *pstr_aac_dec_ch_info =
584                 aac_dec_handle->pstr_aac_dec_ch_info[LEFT];
585             ia_ics_info_struct *ptr_ics_info =
586                 &pstr_aac_dec_ch_info->str_ics_info;
587 
588             if (ch_config == 2)
589               ele_ch = 2, ele_type = 1;
590             else
591               ele_ch = 1, ele_type = 0;
592 
593             prev_data_ele_present = 1;
594 
595             if ((ptr_adts_crc_info->crc_active == 1) &&
596                 (ptr_adts_crc_info->no_reg < 7)) {
597               crc_reg = ixheaacd_adts_crc_start_reg(
598                   ptr_adts_crc_info, it_bit_buff, CRC_ADTS_RAW_DATA_BLK_LEN);
599             }
600 
601             if (object_type != AOT_ER_AAC_ELD)
602               pstr_aac_dec_ch_info->element_instance_tag =
603                   (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 4);
604 
605             element_index_order[*ch_idx] =
606                 pstr_aac_dec_ch_info->element_instance_tag;
607             pstr_aac_dec_ch_info->common_window = 0;
608 
609             ptr_ics_info->num_swb_window = 0;
610             ptr_ics_info->sampling_rate_index =
611                 aac_dec_handle->sampling_rate_index;
612 
613             if (object_type == AOT_ER_AAC_LD) {
614               ptr_ics_info->ltp.data_present = 0;
615               ptr_ics_info->ltp2.data_present = 0;
616               ptr_ics_info->predictor_data_present = 0;
617             }
618             if (ele_ch > 1) {
619               aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]
620                   ->str_ics_info.num_swb_window = 0;
621               aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]
622                   ->str_ics_info.sampling_rate_index =
623                   aac_dec_handle->sampling_rate_index;
624 
625               if (object_type != 39)
626                 pstr_aac_dec_ch_info->common_window =
627                     (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 1);
628               else
629                 pstr_aac_dec_ch_info->common_window = 1;
630 
631               if (pstr_aac_dec_ch_info->common_window) {
632                 error_code = ixheaacd_ics_read(
633                     it_bit_buff, ptr_ics_info, aac_dec_handle->num_swb_window,
634                     object_type, pstr_aac_dec_ch_info->common_window,
635                     aac_dec_handle->samples_per_frame);
636                 if (error_code) {
637                   if (it_bit_buff->cnt_bits < 0) {
638                     error_code = (WORD16)(
639                         (WORD32)
640                             IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
641                   }
642 
643                   goto _ia_handle_error1;
644                 }
645 
646                 aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]->str_ics_info =
647                     pstr_aac_dec_ch_info->str_ics_info;
648 
649                 ixheaacd_read_ms_data(it_bit_buff, pstr_aac_dec_ch_info);
650 
651                 {
652                   if (object_type == AOT_ER_AAC_LD) {
653                     IA_ERRORCODE temp = ixheaacd_ltp_decode(
654                         it_bit_buff, ptr_ics_info, object_type,
655                         aac_dec_handle->samples_per_frame, LEFT);
656 
657                     if (temp != 0) {
658                       return temp;
659                     }
660                   }
661                 }
662               }
663             }
664 
665             error_code = ixheaacd_individual_ch_stream(
666                 it_bit_buff, aac_dec_handle, ele_ch, frame_length,
667                 total_channels, object_type, eld_specific_config, ele_type);
668             if (error_code) return error_code;
669 
670             if (ptr_adts_crc_info->crc_active == 1) {
671               ixheaacd_adts_crc_end_reg(ptr_adts_crc_info, it_bit_buff,
672                                         crc_reg);
673             }
674 
675             if (it_bit_buff->cnt_bits < 0) {
676               error_code = (WORD16)(
677                   (WORD32)
678                       IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
679             }
680 
681             if (error_code) {
682               goto _ia_handle_error1;
683             }
684 
685           _ia_handle_error1:
686             if (error_code) {
687               aac_dec_handle->frame_status = 0;
688               if ((ele_type >= ID_SCE) && (ele_type <= ID_LFE))
689                 num_ch = num_ch + ele_ch;
690               break;
691             } else {
692               error_code = ixheaacd_channel_pair_process(
693                   aac_dec_handle->pstr_aac_dec_ch_info, ele_ch,
694                   aac_dec_handle->pstr_aac_tables, total_channels, object_type,
695                   aac_spect_data_resil_flag,
696                   eld_specific_config.aac_sf_data_resil_flag,
697                   aac_scratch_ptrs->in_data, aac_scratch_ptrs->out_data,
698                   (void *)aac_dec_handle);
699               if (error_code) return error_code;
700               num_ch = num_ch + ele_ch;
701             }
702           }
703 
704           p_obj_exhaacplus_dec->aac_config.element_type[*ch_idx] = ele_ch - 1;
705           break;
706       }
707 
708       if ((object_type != AOT_ER_AAC_ELD) || (!eld_sbr_flag)) {
709         WORD32 bits_decoded, cnt_bits;
710         bits_decoded = (it_bit_buff->size - it_bit_buff->cnt_bits);
711 
712         cnt_bits = (frame_size * 8 - bits_decoded);
713         if (cnt_bits >= 8) {
714           error_code = ixheaacd_extension_payload(it_bit_buff, cnt_bits);
715           if (error_code) return error_code;
716         }
717 
718         if (((object_type == AOT_ER_AAC_ELD) ||
719              (object_type == AOT_ER_AAC_LD)) &&
720             (p_obj_exhaacplus_dec->aac_config.ld_decoder != 1)) {
721           if (it_bit_buff->cnt_bits) {
722             WORD32 alignment = it_bit_buff->bit_pos & 0x07;
723             it_bit_buff->cnt_bits = (it_bit_buff->cnt_bits + alignment) & 7;
724             it_bit_buff->bit_pos = 7;
725             it_bit_buff->ptr_read_next++;
726           }
727         } else {
728           if (it_bit_buff->bit_pos != 7) {
729             WORD32 alignment = it_bit_buff->bit_pos & 0x07;
730             it_bit_buff->cnt_bits -= alignment + 1;
731             it_bit_buff->bit_pos += 7 - alignment;
732             it_bit_buff->ptr_read_next++;
733           }
734         }
735       } else {
736         WORD32 bits_decoded, cnt_bits;
737         bits_decoded = (it_bit_buff->size - it_bit_buff->cnt_bits);
738         cnt_bits = (frame_size * 8 - bits_decoded);
739         if (adtsheader == 1) {
740           if (cnt_bits > it_bit_buff->cnt_bits)
741             return IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES;
742         }
743         ixheaacd_check_for_sbr_payload(
744             it_bit_buff, aac_dec_handle->pstr_sbr_bitstream,
745             (WORD16)(ch_config - 1), pstr_drc_dec, object_type, adtsheader,
746             cnt_bits, ld_sbr_crc_flag, drc_dummy);
747       }
748     }
749   }
750 
751   if (ele_type == ID_END &&
752       p_state_enhaacplus_dec->bs_format == LOAS_BSFORMAT) {
753     WORD32 tmp;
754     tmp = ((WORD32)latm_element->layer_info[0][0].frame_len_bits) -
755           (it_bit_buff->initial_cnt_bits - it_bit_buff->cnt_bits);
756 
757     if (tmp > 0) ixheaacd_read_bidirection(it_bit_buff, tmp);
758 
759     if (latm_element->other_data_present) {
760       WORD32 count_bits = (WORD32)latm_element->other_data_length;
761       ixheaacd_read_bidirection(it_bit_buff, count_bits);
762     }
763   }
764 
765   if (object_type == AOT_ER_AAC_LD) {
766     for (ch = 0; ch < channel; ch++) {
767       aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ltp_lag =
768           aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info.ltp.lag;
769     }
770   }
771   aac_dec_handle->frame_status = aac_dec_handle->frame_status && frame_status;
772 
773   aac_dec_handle->channels = num_ch;
774 
775   if (error_code == 0)
776     if ((skip_full_decode == 0) || ((skip_full_decode == 1) && error_code)) {
777       ia_ics_info_struct str_ics_info[2];
778       WORD32 *spec_coef[2];
779       WORD32 *scratch[2];
780 
781       for (ch = 0; ch < channel; ch++) {
782         str_ics_info[ch] =
783             aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info;
784         spec_coef[ch] =
785             aac_dec_handle->pstr_aac_dec_ch_info[ch]->ptr_spec_coeff;
786       }
787 
788       scratch[0] = (WORD32 *)aac_scratch_ptrs->extra_scr_4k[2];
789       scratch[1] = (WORD32 *)aac_scratch_ptrs->extra_scr_4k[1];
790 
791       error_code = ixheaacd_drc_map_channels(
792           pstr_drc_dec, aac_dec_handle->channels,
793           aac_dec_handle->pstr_aac_dec_ch_info[0]->str_ics_info.frame_length);
794       if (error_code) return error_code;
795 
796       for (ch = 0; ch < aac_dec_handle->channels; ch++) {
797         WORD32 *overlap1 = aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
798                                ->overlap_add_data.ptr_overlap_buf;
799         const WORD16 *ptr_long_window_next =
800             aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
801                 ->ptr_long_window[(int)str_ics_info[ch].window_shape];
802         const WORD16 *ptr_short_window_next =
803             aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
804                 ->ptr_short_window[(int)str_ics_info[ch].window_shape];
805         if (pstr_drc_dec->drc_on) {
806           ixheaacd_drc_apply(pstr_drc_dec, spec_coef[ch],
807                              str_ics_info[ch].window_sequence, ch,
808                              str_ics_info[ch].frame_length);
809         }
810         if (skip_full_decode == 0) {
811           ixheaacd_imdct_process(
812               aac_dec_handle->pstr_aac_dec_overlap_info[ch], spec_coef[ch],
813               &str_ics_info[ch], time_data + slot_element, (WORD16)ch_fac,
814               scratch[ch], aac_dec_handle->pstr_aac_tables, object_type);
815 
816           aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
817               ->overlap_add_data.win_shape = str_ics_info[ch].window_shape;
818           aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
819               ->overlap_add_data.win_seq = str_ics_info[ch].window_sequence;
820           if ((object_type == AOT_ER_AAC_LD) || (object_type == AOT_AAC_LTP)) {
821             {
822               if ((str_ics_info[ch].window_sequence == ONLY_LONG_SEQUENCE) ||
823                   (str_ics_info[ch].window_sequence == LONG_STOP_SEQUENCE)) {
824                 ixheaacd_lt_update_state(
825                     aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
826                         ->ltp_buf,
827                     time_data + slot_element, overlap1,
828                     aac_dec_handle->samples_per_frame, object_type,
829                     (WORD16)ch_fac, str_ics_info[ch].window_sequence,
830                     (WORD16 *)ptr_long_window_next);
831               } else {
832                 ixheaacd_lt_update_state(
833                     aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
834                         ->ltp_buf,
835                     time_data + slot_element, overlap1,
836                     aac_dec_handle->samples_per_frame, object_type,
837                     (WORD16)ch_fac, str_ics_info[ch].window_sequence,
838                     (WORD16 *)ptr_short_window_next);
839               }
840             }
841           }
842           slot_element++;
843         }
844       }
845     }
846 
847   if (ele_type == ID_END) {
848     ixheaacd_byte_align(it_bit_buff, &aac_dec_handle->byte_align_bits);
849     if (p_state_enhaacplus_dec->bs_format == LOAS_BSFORMAT) {
850       ixheaacd_byte_align(it_bit_buff, &it_bit_buff->audio_mux_align);
851     }
852   }
853   *type = ele_type;
854 
855   aac_dec_handle->block_number =
856       ixheaacd_add32(aac_dec_handle->block_number, 1);
857   return error_code;
858 }
859 
ixheaacd_extension_payload(ia_bit_buf_struct * it_bit_buff,WORD32 cnt)860 WORD32 ixheaacd_extension_payload(ia_bit_buf_struct *it_bit_buff, WORD32 cnt) {
861   WORD16 extension_type, discard;
862   WORD32 i;
863   WORD32 fill_nibble;
864 
865   WORD32 err = 0;
866   extension_type = (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 4);
867   switch (extension_type) {
868     case EXT_FILL_DATA:
869 
870       fill_nibble = ixheaacd_read_bits_buf(it_bit_buff, 4);
871 
872       if (fill_nibble == 0) {
873         for (i = 0; i < (cnt >> 3) - 1; i++) {
874           ixheaacd_read_bits_buf(it_bit_buff, 8);
875         }
876 
877       } else
878         err = -1;
879       break;
880     case EXT_FIL:
881     default:
882       for (i = 0; i < ((cnt)-8) + 4; i++) {
883         discard = (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 1);
884       }
885   }
886 
887   return err;
888 }
889