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