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                 ptr_adts_crc_info->no_reg < 7) {
    352               crc_reg = ixheaacd_adts_crc_start_reg(
    353                   ptr_adts_crc_info, it_bit_buff, CRC_ADTS_RAW_DATA_BLK_LEN);
    354             }
    355 
    356             pstr_aac_dec_ch_info->element_instance_tag =
    357                 (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 4);
    358 
    359             element_index_order[*ch_idx] =
    360                 pstr_aac_dec_ch_info->element_instance_tag;
    361             pstr_aac_dec_ch_info->common_window = 0;
    362 
    363             ptr_ics_info->num_swb_window = 0;
    364             ptr_ics_info->sampling_rate_index =
    365                 aac_dec_handle->sampling_rate_index;
    366             if ((object_type == AOT_ER_AAC_LD) ||
    367                 (object_type == AOT_AAC_LTP)) {
    368               ptr_ics_info->ltp.data_present = 0;
    369               ptr_ics_info->ltp2.data_present = 0;
    370               ptr_ics_info->predictor_data_present = 0;
    371             }
    372 
    373             if (ele_ch > 1) {
    374               aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]
    375                   ->str_ics_info.num_swb_window = 0;
    376               aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]
    377                   ->str_ics_info.sampling_rate_index =
    378                   aac_dec_handle->sampling_rate_index;
    379 
    380               pstr_aac_dec_ch_info->common_window =
    381                   (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 1);
    382 
    383               if (pstr_aac_dec_ch_info->common_window) {
    384                 error_code = ixheaacd_ics_read(
    385                     it_bit_buff, ptr_ics_info, aac_dec_handle->num_swb_window,
    386                     object_type, pstr_aac_dec_ch_info->common_window,
    387                     aac_dec_handle->samples_per_frame);
    388                 if (error_code) {
    389                   if (it_bit_buff->cnt_bits < 0) {
    390                     error_code = (WORD16)(
    391                         (WORD32)
    392                             IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
    393                   }
    394 
    395                   goto _ia_handle_error;
    396                 }
    397 
    398                 aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]->str_ics_info =
    399                     pstr_aac_dec_ch_info->str_ics_info;
    400 
    401                 ixheaacd_read_ms_data(it_bit_buff, pstr_aac_dec_ch_info);
    402               }
    403             }
    404 
    405             error_code = ixheaacd_individual_ch_stream(
    406                 it_bit_buff, aac_dec_handle, ele_ch, frame_length,
    407                 total_channels, object_type, eld_specific_config, ele_type);
    408 
    409             if (ptr_adts_crc_info->crc_active == 1) {
    410               ixheaacd_adts_crc_end_reg(ptr_adts_crc_info, it_bit_buff,
    411                                         crc_reg);
    412             }
    413 
    414             if (it_bit_buff->cnt_bits < 0) {
    415               error_code = (WORD16)(
    416                   (WORD32)
    417                       IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
    418             }
    419 
    420             if (error_code) {
    421               goto _ia_handle_error;
    422             }
    423 
    424           _ia_handle_error:
    425             if (error_code) {
    426               aac_dec_handle->frame_status = 0;
    427               if ((ele_type >= ID_SCE) && (ele_type <= ID_LFE))
    428                 num_ch = num_ch + ele_ch;
    429               break;
    430             } else {
    431               ixheaacd_channel_pair_process(
    432                   aac_dec_handle->pstr_aac_dec_ch_info, ele_ch,
    433                   aac_dec_handle->pstr_aac_tables, total_channels, object_type,
    434                   aac_spect_data_resil_flag,
    435                   eld_specific_config.aac_sf_data_resil_flag,
    436                   aac_scratch_ptrs->in_data, aac_scratch_ptrs->out_data,
    437                   (void *)aac_dec_handle);
    438               num_ch = num_ch + ele_ch;
    439             }
    440           }
    441 
    442           break;
    443         case ID_CCE:
    444           if (max_channels > 2) {
    445             prev_data_ele_present = 1;
    446             error_code = ixheaacd_dec_coupling_channel_element(
    447                 it_bit_buff, aac_dec_handle,
    448                 aac_dec_handle->sampling_rate_index,
    449                 aac_dec_handle->pstr_aac_tables,
    450                 aac_dec_handle->pstr_common_tables,
    451                 &element_index_order[*ch_idx],
    452                 (ia_enhaacplus_dec_ind_cc *)aac_dec_handle->p_ind_channel_info,
    453                 total_channels, frame_length, object_type, eld_specific_config,
    454                 ele_type);
    455 
    456             num_ch = num_ch + 1;
    457 
    458             if (error_code) {
    459               aac_dec_handle->frame_status = 0;
    460 
    461               break;
    462             } else {
    463               ixheaacd_channel_pair_process(
    464                   aac_dec_handle->pstr_aac_dec_ch_info, 1,
    465                   aac_dec_handle->pstr_aac_tables, total_channels, object_type,
    466                   aac_spect_data_resil_flag,
    467                   eld_specific_config.aac_sf_data_resil_flag,
    468                   aac_scratch_ptrs->in_data, aac_scratch_ptrs->out_data,
    469                   (void *)aac_dec_handle);
    470             }
    471           } else {
    472             error_code =
    473                 (WORD32)((WORD32)IA_ENHAACPLUS_DEC_EXE_FATAL_UNIMPLEMENTED_CCE);
    474           }
    475           if (it_bit_buff->cnt_bits < 0) {
    476             error_code = (WORD16)((
    477                 WORD32)IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
    478             goto _ia_handle_error;
    479           }
    480           break;
    481 
    482         case ID_DSE:
    483         case ID_PCE:
    484         case ID_FIL:
    485 
    486         {
    487           WORD32 flag = 1;
    488 
    489           if ((ele_type != ID_FIL) && (ptr_adts_crc_info->crc_active == 1) &&
    490               (ptr_adts_crc_info->no_reg < 7)) {
    491             crc_reg =
    492                 ixheaacd_adts_crc_start_reg(ptr_adts_crc_info, it_bit_buff, 0);
    493           }
    494           if (ele_type == ID_DSE) {
    495             ixheaacd_read_data_stream_element(
    496                 it_bit_buff, &aac_dec_handle->byte_align_bits,
    497                 p_obj_exhaacplus_dec->p_state_aac->pstr_drc_dec);
    498           }
    499 
    500           else if (ele_type == ID_PCE) {
    501             error_code = ixheaacd_decode_pce(
    502                 it_bit_buff,
    503                 &p_obj_exhaacplus_dec->aac_config.ui_pce_found_in_hdr,
    504                 &p_obj_exhaacplus_dec->aac_config.str_prog_config);
    505             if (error_code != 0) {
    506               if (it_bit_buff->cnt_bits < 0) {
    507                 error_code = (WORD16)(
    508                     (WORD32)
    509                         IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
    510                 goto _ia_handle_error;
    511               }
    512               aac_dec_handle->frame_status = 0;
    513               if (error_code > 0) {
    514                 error_code = IA_ENHAACPLUS_DEC_EXE_NONFATAL_DECODE_FRAME_ERROR;
    515                 return error_code;
    516               } else {
    517                 return error_code;
    518               }
    519             }
    520           }
    521 
    522           else if (ele_type == ID_FIL) {
    523             WORD32 bits_decoded = 0;
    524             if (object_type == AOT_ER_AAC_ELD) {
    525               bits_decoded = (it_bit_buff->size - it_bit_buff->cnt_bits);
    526               cnt_bits = (frame_size * 8 - bits_decoded);
    527               if (adtsheader == 1) {
    528                 if (cnt_bits > it_bit_buff->cnt_bits)
    529                   return IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES;
    530               }
    531             }
    532 
    533             if (ixheaacd_check_for_sbr_payload(
    534                     it_bit_buff, aac_dec_handle->pstr_sbr_bitstream,
    535                     (WORD16)previous_element, pstr_drc_dec, object_type,
    536                     adtsheader, cnt_bits, ld_sbr_crc_flag, drc_dummy)) {
    537               flag = 0;
    538             }
    539           }
    540 
    541           if (it_bit_buff->cnt_bits < 0) {
    542             error_code = (WORD16)((
    543                 WORD32)IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
    544             goto _ia_handle_error;
    545           }
    546 
    547           if (flag) {
    548             if (prev_data_ele_present == 0) {
    549               new_element = 0;
    550             }
    551           }
    552           if ((ele_type != ID_FIL) && (ptr_adts_crc_info->crc_active == 1)) {
    553             ixheaacd_adts_crc_end_reg(ptr_adts_crc_info, it_bit_buff, crc_reg);
    554           }
    555 
    556           if (ele_type == ID_PCE) {
    557             if (ptr_adts_crc_info->str_crc_reg_data[crc_reg].bit_buf_cnt) {
    558               ptr_adts_crc_info->str_crc_reg_data[crc_reg].max_bits =
    559                   ptr_adts_crc_info->str_crc_reg_data[crc_reg].bit_buf_cnt;
    560             }
    561           }
    562         }
    563 
    564         break;
    565 
    566         case ID_END:
    567           error_code = 0;
    568           break;
    569       }
    570 
    571       previous_element = ele_type;
    572 
    573       if (init_flag) {
    574         if ((ele_type >= ID_SCE) && (ele_type <= ID_LFE)) {
    575           p_obj_exhaacplus_dec->aac_config.element_type[*ch_idx] = ele_type;
    576         }
    577       }
    578     }
    579   } else {
    580     {
    581       switch (ch_config) {
    582         default:
    583           if (aac_dec_handle->frame_status) {
    584             ia_aac_dec_channel_info_struct *pstr_aac_dec_ch_info =
    585                 aac_dec_handle->pstr_aac_dec_ch_info[LEFT];
    586             ia_ics_info_struct *ptr_ics_info =
    587                 &pstr_aac_dec_ch_info->str_ics_info;
    588 
    589             if (ch_config == 2)
    590               ele_ch = 2, ele_type = 1;
    591             else
    592               ele_ch = 1, ele_type = 0;
    593 
    594             prev_data_ele_present = 1;
    595 
    596             if ((ptr_adts_crc_info->crc_active == 1) &&
    597                 (ptr_adts_crc_info->no_reg < 7)) {
    598               crc_reg = ixheaacd_adts_crc_start_reg(
    599                   ptr_adts_crc_info, it_bit_buff, CRC_ADTS_RAW_DATA_BLK_LEN);
    600             }
    601 
    602             if (object_type != AOT_ER_AAC_ELD)
    603               pstr_aac_dec_ch_info->element_instance_tag =
    604                   (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 4);
    605 
    606             element_index_order[*ch_idx] =
    607                 pstr_aac_dec_ch_info->element_instance_tag;
    608             pstr_aac_dec_ch_info->common_window = 0;
    609 
    610             ptr_ics_info->num_swb_window = 0;
    611             ptr_ics_info->sampling_rate_index =
    612                 aac_dec_handle->sampling_rate_index;
    613 
    614             if (object_type == AOT_ER_AAC_LD) {
    615               ptr_ics_info->ltp.data_present = 0;
    616               ptr_ics_info->ltp2.data_present = 0;
    617               ptr_ics_info->predictor_data_present = 0;
    618             }
    619             if (ele_ch > 1) {
    620               aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]
    621                   ->str_ics_info.num_swb_window = 0;
    622               aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]
    623                   ->str_ics_info.sampling_rate_index =
    624                   aac_dec_handle->sampling_rate_index;
    625 
    626               if (object_type != 39)
    627                 pstr_aac_dec_ch_info->common_window =
    628                     (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 1);
    629               else
    630                 pstr_aac_dec_ch_info->common_window = 1;
    631 
    632               if (pstr_aac_dec_ch_info->common_window) {
    633                 error_code = ixheaacd_ics_read(
    634                     it_bit_buff, ptr_ics_info, aac_dec_handle->num_swb_window,
    635                     object_type, pstr_aac_dec_ch_info->common_window,
    636                     aac_dec_handle->samples_per_frame);
    637                 if (error_code) {
    638                   if (it_bit_buff->cnt_bits < 0) {
    639                     error_code = (WORD16)(
    640                         (WORD32)
    641                             IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
    642                   }
    643 
    644                   goto _ia_handle_error1;
    645                 }
    646 
    647                 aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]->str_ics_info =
    648                     pstr_aac_dec_ch_info->str_ics_info;
    649 
    650                 ixheaacd_read_ms_data(it_bit_buff, pstr_aac_dec_ch_info);
    651 
    652                 {
    653                   if (object_type == AOT_ER_AAC_LD) {
    654                     WORD16 temp = ixheaacd_ltp_decode(
    655                         it_bit_buff, ptr_ics_info, object_type,
    656                         aac_dec_handle->samples_per_frame, LEFT);
    657 
    658                     if (temp != 0) {
    659                       return temp;
    660                     }
    661                   }
    662                 }
    663               }
    664             }
    665 
    666             error_code = ixheaacd_individual_ch_stream(
    667                 it_bit_buff, aac_dec_handle, ele_ch, frame_length,
    668                 total_channels, object_type, eld_specific_config, ele_type);
    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               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               num_ch = num_ch + ele_ch;
    700             }
    701           }
    702 
    703           p_obj_exhaacplus_dec->aac_config.element_type[*ch_idx] = ele_ch - 1;
    704           break;
    705       }
    706 
    707       if ((object_type != AOT_ER_AAC_ELD) || (!eld_sbr_flag)) {
    708         WORD32 bits_decoded, cnt_bits;
    709         bits_decoded = (it_bit_buff->size - it_bit_buff->cnt_bits);
    710 
    711         cnt_bits = (frame_size * 8 - bits_decoded);
    712         if (cnt_bits >= 8) {
    713           ixheaacd_extension_payload(it_bit_buff, cnt_bits);
    714         }
    715 
    716         if (((object_type == AOT_ER_AAC_ELD) ||
    717              (object_type == AOT_ER_AAC_LD)) &&
    718             (p_obj_exhaacplus_dec->aac_config.ld_decoder != 1)) {
    719           if (it_bit_buff->cnt_bits) {
    720             WORD32 alignment = it_bit_buff->bit_pos & 0x07;
    721             it_bit_buff->cnt_bits = (it_bit_buff->cnt_bits + alignment) & 7;
    722             it_bit_buff->bit_pos = 7;
    723             it_bit_buff->ptr_read_next++;
    724           }
    725         } else {
    726           if (it_bit_buff->bit_pos != 7) {
    727             WORD32 alignment = it_bit_buff->bit_pos & 0x07;
    728             it_bit_buff->cnt_bits -= alignment + 1;
    729             it_bit_buff->bit_pos += 7 - alignment;
    730             it_bit_buff->ptr_read_next++;
    731           }
    732         }
    733       } else {
    734         WORD32 bits_decoded, cnt_bits;
    735         bits_decoded = (it_bit_buff->size - it_bit_buff->cnt_bits);
    736         cnt_bits = (frame_size * 8 - bits_decoded);
    737         if (adtsheader == 1) {
    738           if (cnt_bits > it_bit_buff->cnt_bits)
    739             return IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES;
    740         }
    741         ixheaacd_check_for_sbr_payload(
    742             it_bit_buff, aac_dec_handle->pstr_sbr_bitstream,
    743             (WORD16)(ch_config - 1), pstr_drc_dec, object_type, adtsheader,
    744             cnt_bits, ld_sbr_crc_flag, drc_dummy);
    745       }
    746     }
    747   }
    748 
    749   if (ele_type == ID_END &&
    750       p_state_enhaacplus_dec->bs_format == LOAS_BSFORMAT) {
    751     WORD32 tmp;
    752     tmp = ((WORD32)latm_element->layer_info[0][0].frame_len_bits) -
    753           (it_bit_buff->initial_cnt_bits - it_bit_buff->cnt_bits);
    754 
    755     if (tmp > 0) ixheaacd_read_bidirection(it_bit_buff, tmp);
    756 
    757     if (latm_element->other_data_present) {
    758       WORD32 count_bits = (WORD32)latm_element->other_data_length;
    759       ixheaacd_read_bidirection(it_bit_buff, count_bits);
    760     }
    761   }
    762 
    763   if (object_type == AOT_ER_AAC_LD) {
    764     for (ch = 0; ch < channel; ch++) {
    765       aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ltp_lag =
    766           aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info.ltp.lag;
    767     }
    768   }
    769   aac_dec_handle->frame_status = aac_dec_handle->frame_status && frame_status;
    770 
    771   aac_dec_handle->channels = num_ch;
    772 
    773   if (error_code == 0)
    774     if ((skip_full_decode == 0) || ((skip_full_decode == 1) && error_code)) {
    775       ia_ics_info_struct str_ics_info[2];
    776       WORD32 *spec_coef[2];
    777       WORD32 *scratch[2];
    778 
    779       for (ch = 0; ch < channel; ch++) {
    780         str_ics_info[ch] =
    781             aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info;
    782         spec_coef[ch] =
    783             aac_dec_handle->pstr_aac_dec_ch_info[ch]->ptr_spec_coeff;
    784       }
    785 
    786       scratch[0] = (WORD32 *)aac_scratch_ptrs->extra_scr_4k[2];
    787       scratch[1] = (WORD32 *)aac_scratch_ptrs->extra_scr_4k[1];
    788 
    789       ixheaacd_drc_map_channels(
    790           pstr_drc_dec, aac_dec_handle->channels,
    791           aac_dec_handle->pstr_aac_dec_ch_info[0]->str_ics_info.frame_length);
    792 
    793       for (ch = 0; ch < aac_dec_handle->channels; ch++) {
    794         WORD32 *overlap1 = aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
    795                                ->overlap_add_data.ptr_overlap_buf;
    796         const WORD16 *ptr_long_window_next =
    797             aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
    798                 ->ptr_long_window[(int)str_ics_info[ch].window_shape];
    799         const WORD16 *ptr_short_window_next =
    800             aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
    801                 ->ptr_short_window[(int)str_ics_info[ch].window_shape];
    802         if (pstr_drc_dec->drc_on) {
    803           ixheaacd_drc_apply(pstr_drc_dec, spec_coef[ch],
    804                              str_ics_info[ch].window_sequence, ch,
    805                              str_ics_info[ch].frame_length);
    806         }
    807         if (skip_full_decode == 0) {
    808           ixheaacd_imdct_process(
    809               aac_dec_handle->pstr_aac_dec_overlap_info[ch], spec_coef[ch],
    810               &str_ics_info[ch], time_data + slot_element, (WORD16)ch_fac,
    811               scratch[ch], aac_dec_handle->pstr_aac_tables, object_type);
    812 
    813           aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
    814               ->overlap_add_data.win_shape = str_ics_info[ch].window_shape;
    815           aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
    816               ->overlap_add_data.win_seq = str_ics_info[ch].window_sequence;
    817           if ((object_type == AOT_ER_AAC_LD) || (object_type == AOT_AAC_LTP)) {
    818             {
    819               if ((str_ics_info[ch].window_sequence == ONLY_LONG_SEQUENCE) ||
    820                   (str_ics_info[ch].window_sequence == LONG_STOP_SEQUENCE)) {
    821                 ixheaacd_lt_update_state(
    822                     aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
    823                         ->ltp_buf,
    824                     time_data + slot_element, overlap1,
    825                     aac_dec_handle->samples_per_frame, object_type,
    826                     (WORD16)ch_fac, str_ics_info[ch].window_sequence,
    827                     (WORD16 *)ptr_long_window_next);
    828               } else {
    829                 ixheaacd_lt_update_state(
    830                     aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
    831                         ->ltp_buf,
    832                     time_data + slot_element, overlap1,
    833                     aac_dec_handle->samples_per_frame, object_type,
    834                     (WORD16)ch_fac, str_ics_info[ch].window_sequence,
    835                     (WORD16 *)ptr_short_window_next);
    836               }
    837             }
    838           }
    839           slot_element++;
    840         }
    841       }
    842     }
    843 
    844   if (ele_type == ID_END) {
    845     ixheaacd_byte_align(it_bit_buff, &aac_dec_handle->byte_align_bits);
    846     if (p_state_enhaacplus_dec->bs_format == LOAS_BSFORMAT) {
    847       ixheaacd_byte_align(it_bit_buff, &it_bit_buff->audio_mux_align);
    848     }
    849   }
    850   *type = ele_type;
    851 
    852   aac_dec_handle->block_number =
    853       ixheaacd_add32(aac_dec_handle->block_number, 1);
    854   return error_code;
    855 }
    856 
    857 WORD32 ixheaacd_extension_payload(ia_bit_buf_struct *it_bit_buff, WORD32 cnt) {
    858   WORD16 extension_type, discard;
    859   WORD32 i;
    860   WORD32 fill_nibble;
    861 
    862   WORD32 err = 0;
    863   extension_type = (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 4);
    864   switch (extension_type) {
    865     case EXT_FILL_DATA:
    866 
    867       fill_nibble = ixheaacd_read_bits_buf(it_bit_buff, 4);
    868 
    869       if (fill_nibble == 0) {
    870         for (i = 0; i < (cnt >> 3) - 1; i++) {
    871           ixheaacd_read_bits_buf(it_bit_buff, 8);
    872         }
    873 
    874       } else
    875         err = -1;
    876       break;
    877     case EXT_FIL:
    878     default:
    879       for (i = 0; i < ((cnt)-8) + 4; i++) {
    880         discard = (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 1);
    881       }
    882   }
    883 
    884   return err;
    885 }
    886