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 <string.h>
     21 #include "ixheaacd_sbr_common.h"
     22 #include <ixheaacd_type_def.h>
     23 
     24 #include "ixheaacd_constants.h"
     25 #include <ixheaacd_basic_ops32.h>
     26 #include <ixheaacd_basic_ops16.h>
     27 #include <ixheaacd_basic_ops40.h>
     28 #include "ixheaacd_basic_ops.h"
     29 
     30 #include <ixheaacd_basic_op.h>
     31 #include "ixheaacd_intrinsics.h"
     32 #include "ixheaacd_common_rom.h"
     33 #include "ixheaacd_basic_funcs.h"
     34 #include "ixheaacd_bitbuffer.h"
     35 #include "ixheaacd_sbr_common.h"
     36 #include "ixheaacd_drc_data_struct.h"
     37 #include "ixheaacd_drc_dec.h"
     38 #include "ixheaacd_sbrdecoder.h"
     39 #include "ixheaacd_sbrdecsettings.h"
     40 #include "ixheaacd_sbr_scale.h"
     41 #include "ixheaacd_lpp_tran.h"
     42 #include "ixheaacd_env_extr_part.h"
     43 #include <ixheaacd_sbr_rom.h>
     44 #include "ixheaacd_hybrid.h"
     45 #include "ixheaacd_ps_dec.h"
     46 #include "ixheaacd_env_extr.h"
     47 
     48 #include <math.h>
     49 
     50 #include "ixheaacd_sbr_const.h"
     51 #include "ixheaacd_intrinsics.h"
     52 
     53 #include "ixheaacd_pvc_dec.h"
     54 
     55 #include "ixheaacd_ps_bitdec.h"
     56 
     57 #include "ixheaacd_audioobjtypes.h"
     58 
     59 WORD32 ixheaacd_cnt_leading_ones(WORD32 a) {
     60   WORD32 count = 0;
     61 
     62   while (a) {
     63     if (a & 0x80000000)
     64       count++;
     65     else
     66       break;
     67     a = a << 1;
     68   }
     69   return count;
     70 }
     71 VOID ixheaacd_huffman_decode(WORD32 it_bit_buff, WORD16 *h_index, WORD16 *len,
     72                              const UWORD16 *input_table,
     73                              const UWORD32 *idx_table) {
     74   UWORD32 temp = 0;
     75   UWORD32 temp1 = 0;
     76   WORD32 found = 0;
     77   UWORD32 mask = 0x80000000;
     78 
     79   WORD32 clo;
     80   WORD32 MAX_LEN;
     81   WORD32 ixheaacd_drc_offset = 0;
     82   WORD32 length;
     83   UWORD32 cwrd;
     84   WORD32 len_end;
     85 
     86   MAX_LEN = input_table[0];
     87   mask = mask - (1 << (31 - MAX_LEN));
     88   mask = mask << 1;
     89   temp = (UWORD32)(it_bit_buff & mask);
     90 
     91   len_end = input_table[0];
     92   clo = ixheaacd_cnt_leading_ones(temp);
     93   do {
     94     ixheaacd_drc_offset = (idx_table[clo] >> 20) & 0xff;
     95     length = input_table[ixheaacd_drc_offset + 1] & 0x1f;
     96     cwrd = idx_table[clo] & 0xfffff;
     97     temp1 = temp >> (32 - length);
     98     if (temp1 <= cwrd) {
     99       ixheaacd_drc_offset = ixheaacd_drc_offset - (cwrd - temp1);
    100       found = 1;
    101     } else {
    102       len_end = len_end + ((idx_table[clo] >> 28) & 0xf);
    103       clo = len_end;
    104     }
    105   } while (!found);
    106   *h_index = input_table[ixheaacd_drc_offset + 1] >> 5;
    107   *len = length;
    108 }
    109 
    110 static WORD32 ixheaacd_read_esbr_pvc_envelope(ia_pvc_data_struct *ptr_pvc_data,
    111                                               ia_bit_buf_struct *it_bit_buff,
    112                                               WORD32 indepFlag) {
    113   WORD32 i, j, k;
    114   WORD32 fixed_length = 0, num_grid_info = 0, grid_info;
    115   UWORD8 div_mode, ns_mode;
    116   UWORD16 pvc_id[PVC_NUM_TIME_SLOTS + 1];
    117   UWORD8 num_length;
    118   UWORD8 length;
    119   UWORD8 reuse_pvc_id;
    120   WORD32 sum_length = 0;
    121   WORD32 length_bits = 4;
    122   UWORD8 pvc_id_bits = PVC_ID_BITS;
    123 
    124   div_mode = (UWORD8)ixheaacd_read_bits_buf(it_bit_buff, PVC_DIV_MODE_BITS);
    125   ns_mode = (UWORD8)ixheaacd_read_bits_buf(it_bit_buff, PVC_NS_MODE_BITS);
    126 
    127   if (ptr_pvc_data->pvc_mode == 3) {
    128     pvc_id_bits = 0;
    129   }
    130 
    131   if (div_mode <= 3) {
    132     num_length = div_mode;
    133     if (indepFlag) {
    134       reuse_pvc_id = 0;
    135     } else {
    136       reuse_pvc_id =
    137           (UWORD8)ixheaacd_read_bits_buf(it_bit_buff, PVC_REUSE_PVC_ID_BITS);
    138     }
    139     if (reuse_pvc_id == 1) {
    140       pvc_id[0] = ptr_pvc_data->prev_pvc_id;
    141     } else {
    142       pvc_id[0] = (UWORD16)ixheaacd_read_bits_buf(it_bit_buff, pvc_id_bits);
    143     }
    144 
    145     k = 1;
    146     if (num_length) {
    147       sum_length = 0;
    148       for (i = 0; i < num_length; i++) {
    149         if (sum_length >= 13) {
    150           length_bits = 1;
    151         } else if (sum_length >= 11) {
    152           length_bits = 2;
    153         } else if (sum_length >= 7) {
    154           length_bits = 3;
    155         } else {
    156           length_bits = 4;
    157         }
    158         length = (UWORD8)ixheaacd_read_bits_buf(it_bit_buff, length_bits);
    159         length += 1;
    160         sum_length += length;
    161         if ((k + length - 1) > PVC_NUM_TIME_SLOTS) {
    162           return -1;
    163         }
    164         for (j = 1; j < length; j++, k++) {
    165           pvc_id[k] = pvc_id[k - 1];
    166         }
    167         pvc_id[k++] = (UWORD16)ixheaacd_read_bits_buf(it_bit_buff, pvc_id_bits);
    168       }
    169     }
    170 
    171     for (; k < 16; k++) {
    172       pvc_id[k] = pvc_id[k - 1];
    173     }
    174 
    175   } else {
    176     switch (div_mode) {
    177       case 4:
    178         num_grid_info = 2;
    179         fixed_length = 8;
    180         break;
    181       case 5:
    182         num_grid_info = 4;
    183         fixed_length = 4;
    184         break;
    185       case 6:
    186         num_grid_info = 8;
    187         fixed_length = 2;
    188         break;
    189       case 7:
    190         num_grid_info = 16;
    191         fixed_length = 1;
    192         break;
    193       default:;
    194     }
    195     if (indepFlag) {
    196       grid_info = 1;
    197     } else {
    198       grid_info = ixheaacd_read_bits_buf(it_bit_buff, PVC_GRID_INFO_BITS);
    199     }
    200     if (grid_info) {
    201       pvc_id[0] = (UWORD16)ixheaacd_read_bits_buf(it_bit_buff, pvc_id_bits);
    202     } else {
    203       pvc_id[0] = ptr_pvc_data->prev_pvc_id;
    204     }
    205     for (j = 1, k = 1; j < fixed_length; j++, k++) {
    206       pvc_id[k] = pvc_id[k - 1];
    207     }
    208 
    209     for (i = 1; i < num_grid_info; i++) {
    210       grid_info = ixheaacd_read_bits_buf(it_bit_buff, PVC_GRID_INFO_BITS);
    211       if (grid_info == 1) {
    212         pvc_id[k++] = (UWORD16)ixheaacd_read_bits_buf(it_bit_buff, pvc_id_bits);
    213       } else {
    214         pvc_id[k] = pvc_id[k - 1];
    215         k++;
    216       }
    217       for (j = 1; j < fixed_length; j++, k++) {
    218         pvc_id[k] = pvc_id[k - 1];
    219       }
    220     }
    221   }
    222   ptr_pvc_data->div_mode = div_mode;
    223   ptr_pvc_data->ns_mode = ns_mode;
    224   for (i = 0; i < PVC_NUM_TIME_SLOTS; i++) {
    225     ptr_pvc_data->pvc_id[i] = pvc_id[i];
    226   }
    227   return 0;
    228 }
    229 
    230 static VOID ixheaacd_pvc_env_dtdf_data(
    231     ia_sbr_frame_info_data_struct *ptr_frame_data,
    232     ia_bit_buf_struct *it_bit_buff) {
    233   WORD32 i;
    234   WORD32 usac_independency_flag = ptr_frame_data->usac_independency_flag;
    235   WORD32 bs_num_noise = ptr_frame_data->str_frame_info_details.num_noise_env;
    236 
    237   if (usac_independency_flag) {
    238     ptr_frame_data->del_cod_dir_noise_arr[0] = 0;
    239   } else {
    240     ptr_frame_data->del_cod_dir_noise_arr[0] =
    241         ixheaacd_read_bits_buf(it_bit_buff, ESBR_DOMAIN_BITS);
    242   }
    243 
    244   for (i = 1; i < bs_num_noise; i++) {
    245     ptr_frame_data->del_cod_dir_noise_arr[i] =
    246         ixheaacd_read_bits_buf(it_bit_buff, ESBR_DOMAIN_BITS);
    247   }
    248 }
    249 
    250 static VOID ixheaacd_read_sbr_addi_data(
    251     ia_sbr_frame_info_data_struct *ptr_frame_data,
    252     ia_sbr_header_data_struct *ptr_header_data,
    253     ia_bit_buf_struct *it_bit_buff) {
    254   WORD32 i;
    255 
    256   WORD32 flag = ixheaacd_read_bits_buf(it_bit_buff, 1);
    257 
    258   ptr_frame_data->sin_start_for_cur_top =
    259       ptr_frame_data->sin_start_for_next_top;
    260   ptr_frame_data->sin_len_for_cur_top = ptr_frame_data->sin_len_for_next_top;
    261   ptr_frame_data->sin_start_for_next_top = 0;
    262   ptr_frame_data->sin_len_for_next_top = 0;
    263 
    264   if (flag) {
    265     for (i = 0; i < ptr_header_data->pstr_freq_band_data->num_sf_bands[HIGH];
    266          i++) {
    267       ptr_frame_data->add_harmonics[i] = ixheaacd_read_bits_buf(it_bit_buff, 1);
    268     }
    269     if (ptr_frame_data->pvc_mode != 0) {
    270       ptr_frame_data->sine_position = ESC_SIN_POS;
    271 
    272       ptr_frame_data->bs_sin_pos_present =
    273           ixheaacd_read_bits_buf(it_bit_buff, 1);
    274 
    275       if (ptr_frame_data->bs_sin_pos_present == 1) {
    276         ptr_frame_data->sine_position = ixheaacd_read_bits_buf(it_bit_buff, 5);
    277       }
    278       if (ptr_frame_data->var_len > 0) {
    279         if (ptr_frame_data->sine_position > 16) {
    280           if (ptr_frame_data->sine_position == 31) {
    281             ptr_frame_data->sin_start_for_next_top = 0;
    282             ptr_frame_data->sin_len_for_next_top = ptr_frame_data->var_len;
    283           } else {
    284             if ((ptr_frame_data->var_len + 16) ==
    285                 ptr_frame_data->sine_position) {
    286               ptr_frame_data->sin_start_for_next_top = 0;
    287               ptr_frame_data->sin_len_for_next_top = ptr_frame_data->var_len;
    288             } else {
    289               ptr_frame_data->sin_start_for_next_top =
    290                   ptr_frame_data->sine_position - 16;
    291               ptr_frame_data->sin_len_for_next_top = ptr_frame_data->var_len;
    292             }
    293           }
    294         } else {
    295           ptr_frame_data->sin_start_for_next_top = 0;
    296           ptr_frame_data->sin_len_for_next_top = ptr_frame_data->var_len;
    297         }
    298       } else {
    299         ptr_frame_data->sin_start_for_next_top = 0;
    300         ptr_frame_data->sin_len_for_next_top = 0;
    301       }
    302     }
    303   }
    304   return;
    305 }
    306 
    307 WORD32 ixheaacd_ssc_huff_dec(ia_huffman_data_type t_huff,
    308                              ia_handle_bit_buf_struct it_bit_buff) {
    309   WORD32 index;
    310   WORD32 value, bit;
    311   WORD16 cw;
    312   index = 0;
    313 
    314   while (index >= 0) {
    315     cw = t_huff[index];
    316 
    317     bit = ixheaacd_read_bits_buf(it_bit_buff, 1);
    318 
    319     if (bit) {
    320       WORD sign = (cw & 0x0080);
    321       if (sign) {
    322         index = (cw | 0xffffff80);
    323       } else {
    324         index = (cw & 0x007f);
    325       }
    326     } else {
    327       index = (cw >> 8);
    328     }
    329   }
    330 
    331   value = (index + 64);
    332 
    333   return (value);
    334 }
    335 
    336 WORD32 ixheaacd_sbr_read_header_data(
    337     ia_sbr_header_data_struct *pstr_sbr_header, ia_bit_buf_struct *it_bit_buff,
    338     FLAG stereo_flag, ia_sbr_header_data_struct *pstr_sbr_dflt_header) {
    339   ia_sbr_header_data_struct prev_header_info;
    340   FLAG header_extra_1 = 0, header_extra_2 = 0;
    341   WORD32 tmp;
    342   WORD32 usac_independency_flag = pstr_sbr_header->usac_independency_flag;
    343   WORD32 use_dflt_hdr = 0;
    344   WORD32 header_present = 1;
    345   WORD32 usac_flag = pstr_sbr_header->usac_flag;
    346 
    347   if (!usac_flag) {
    348     memcpy(&prev_header_info, pstr_sbr_header,
    349            sizeof(ia_sbr_header_data_struct));
    350 
    351     tmp = ixheaacd_read_bits_buf(
    352         it_bit_buff, SBR_AMPLITUDE_RESOLUTION_BITS + SBR_BEGIN_SAMP_FREQ_BITS +
    353                          SBR_END_SAMP_FREQ_BITS + SBR_CROSS_OVER_BND_BITS);
    354 
    355     pstr_sbr_header->amp_res = (WORD16)(
    356         (tmp & 0x0800) >> (SBR_BEGIN_SAMP_FREQ_BITS + SBR_END_SAMP_FREQ_BITS +
    357                            SBR_CROSS_OVER_BND_BITS));
    358 
    359     pstr_sbr_header->start_freq = (WORD16)(
    360         (tmp & 0x0780) >> (SBR_END_SAMP_FREQ_BITS + SBR_CROSS_OVER_BND_BITS));
    361 
    362     pstr_sbr_header->stop_freq =
    363         (WORD16)((tmp & 0x078) >> (SBR_CROSS_OVER_BND_BITS));
    364 
    365     pstr_sbr_header->xover_band = (WORD16)((tmp & 0x07));
    366 
    367     tmp = ixheaacd_read_bits_buf(
    368         it_bit_buff,
    369         SBR_HDR_RESERV_BITS + SBR_HDR_EXTR_1_BITS + SBR_HDR_EXTR_2_BITS);
    370     header_extra_1 = (FLAG)((tmp & 0x02) >> (SBR_HDR_EXTR_2_BITS));
    371     header_extra_2 = (FLAG)((tmp & 0x01));
    372     if (stereo_flag) {
    373       pstr_sbr_header->channel_mode = SBR_STEREO;
    374     } else {
    375       pstr_sbr_header->channel_mode = SBR_MONO;
    376     }
    377   } else {
    378     WORD32 info_present = 0;
    379     if (pstr_sbr_header->sync_state == SBR_ACTIVE) {
    380       memcpy(&prev_header_info, pstr_sbr_header,
    381              sizeof(ia_sbr_header_data_struct));
    382     }
    383     if (usac_independency_flag) {
    384       header_present = 1;
    385       info_present = 1;
    386     } else {
    387       info_present = ixheaacd_read_bits_buf(it_bit_buff, 1);
    388       if (info_present) {
    389         header_present = ixheaacd_read_bits_buf(it_bit_buff, 1);
    390       } else {
    391         header_present = 0;
    392       }
    393     }
    394 
    395     if (info_present) {
    396       tmp = ixheaacd_read_bits_buf(it_bit_buff, SBR_AMPLITUDE_RESOLUTION_BITS +
    397                                                     ESBR_CROSS_OVER_BND_BITS +
    398                                                     ESBR_PRE_FLAT_BITS);
    399       pstr_sbr_header->amp_res = (WORD16)(
    400           (tmp & 0x0020) >> (ESBR_CROSS_OVER_BND_BITS + ESBR_PRE_FLAT_BITS));
    401       pstr_sbr_header->xover_band =
    402           (WORD16)((tmp & 0x001E) >> (ESBR_PRE_FLAT_BITS));
    403       pstr_sbr_header->pre_proc_flag = (WORD16)((tmp & 0x001));
    404       if (pstr_sbr_header->pvc_flag) {
    405         pstr_sbr_header->pvc_mode =
    406             ixheaacd_read_bits_buf(it_bit_buff, ESBR_PVC_MODE_BITS);
    407       } else {
    408         pstr_sbr_header->pvc_mode = 0;
    409       }
    410     }
    411 
    412     if (header_present) {
    413       use_dflt_hdr = ixheaacd_read_bits_buf(it_bit_buff, 1);
    414       if (use_dflt_hdr) {
    415         pstr_sbr_header->start_freq = pstr_sbr_dflt_header->start_freq;
    416         pstr_sbr_header->stop_freq = pstr_sbr_dflt_header->stop_freq;
    417         pstr_sbr_header->header_extra_1 = pstr_sbr_dflt_header->header_extra_1;
    418         pstr_sbr_header->header_extra_2 = pstr_sbr_dflt_header->header_extra_2;
    419         pstr_sbr_header->freq_scale = pstr_sbr_dflt_header->freq_scale;
    420         pstr_sbr_header->alter_scale = pstr_sbr_dflt_header->alter_scale;
    421         pstr_sbr_header->noise_bands = pstr_sbr_dflt_header->noise_bands;
    422         pstr_sbr_header->limiter_bands = pstr_sbr_dflt_header->limiter_bands;
    423         pstr_sbr_header->limiter_gains = pstr_sbr_dflt_header->limiter_gains;
    424         pstr_sbr_header->interpol_freq = pstr_sbr_dflt_header->interpol_freq;
    425         pstr_sbr_header->smoothing_mode = pstr_sbr_dflt_header->smoothing_mode;
    426       } else {
    427         tmp = ixheaacd_read_bits_buf(
    428             it_bit_buff, SBR_BEGIN_SAMP_FREQ_BITS + SBR_END_SAMP_FREQ_BITS +
    429                              SBR_HDR_EXTR_1_BITS + SBR_HDR_EXTR_2_BITS);
    430         pstr_sbr_header->start_freq =
    431             (tmp & 0x03C0) >> (SBR_END_SAMP_FREQ_BITS + SBR_HDR_EXTR_1_BITS +
    432                                SBR_HDR_EXTR_2_BITS);
    433         pstr_sbr_header->stop_freq =
    434             (tmp & 0x003C) >> (SBR_HDR_EXTR_1_BITS + SBR_HDR_EXTR_2_BITS);
    435         pstr_sbr_header->header_extra_1 =
    436             (tmp & 0x0002) >> (SBR_HDR_EXTR_2_BITS);
    437         pstr_sbr_header->header_extra_2 = (tmp & 0x0001);
    438         header_extra_1 = pstr_sbr_header->header_extra_1;
    439         header_extra_2 = pstr_sbr_header->header_extra_2;
    440       }
    441     }
    442   }
    443 
    444   if (!use_dflt_hdr && header_present) {
    445     if (header_extra_1) {
    446       tmp = ixheaacd_read_bits_buf(
    447           it_bit_buff,
    448           SBR_SAMP_FREQ_LVL_BITS + SBR_CHANGE_LVL_BITS + SBR_NOISE_BND_BITS);
    449       pstr_sbr_header->freq_scale =
    450           (WORD16)((tmp & 0x018) >> (SBR_CHANGE_LVL_BITS + SBR_NOISE_BND_BITS));
    451       pstr_sbr_header->alter_scale =
    452           (WORD16)((tmp & 0x04) >> (SBR_NOISE_BND_BITS));
    453       pstr_sbr_header->noise_bands = (WORD16)((tmp & 0x03));
    454     } else {
    455       pstr_sbr_header->freq_scale = SBR_SAMP_FEQ_LVL_DEF;
    456       pstr_sbr_header->alter_scale = SBR_CHANGE_LVL_DEF;
    457       pstr_sbr_header->noise_bands = SBR_NOISE_BND_DEF;
    458     }
    459 
    460     if (header_extra_2) {
    461       tmp = ixheaacd_read_bits_buf(
    462           it_bit_buff, SBR_BND_LIMIT_BITS + SBR_GAIN_LIMIT_BITS +
    463                            SBR_INTERPOL_SAMP_FREQ_BITS + SBR_SMOOTH_LEN_BITS);
    464       pstr_sbr_header->limiter_bands = (WORD16)(
    465           (tmp & 0x030) >> (SBR_GAIN_LIMIT_BITS + SBR_INTERPOL_SAMP_FREQ_BITS +
    466                             SBR_SMOOTH_LEN_BITS));
    467       pstr_sbr_header->limiter_gains = (WORD16)(
    468           (tmp & 0x0c) >> (SBR_INTERPOL_SAMP_FREQ_BITS + SBR_SMOOTH_LEN_BITS));
    469       pstr_sbr_header->interpol_freq =
    470           (WORD16)((tmp & 0x02) >> (SBR_SMOOTH_LEN_BITS));
    471       pstr_sbr_header->smoothing_mode = (WORD16)((tmp & 0x01));
    472     } else {
    473       pstr_sbr_header->limiter_bands = SBR_BND_LIMIT_DEF;
    474       pstr_sbr_header->limiter_gains = SBR_GAIN_LIMIT_DEF;
    475       pstr_sbr_header->interpol_freq = SBR_INTERPOL_SAMP_FEQ_DEF;
    476       pstr_sbr_header->smoothing_mode = SBR_SMOOTH_LEN_DEF;
    477     }
    478   }
    479 
    480   if ((pstr_sbr_header->sync_state != SBR_ACTIVE) ||
    481       (prev_header_info.start_freq != pstr_sbr_header->start_freq) ||
    482       (prev_header_info.stop_freq != pstr_sbr_header->stop_freq) ||
    483       (prev_header_info.xover_band != pstr_sbr_header->xover_band) ||
    484       (prev_header_info.freq_scale != pstr_sbr_header->freq_scale) ||
    485       (prev_header_info.alter_scale != pstr_sbr_header->alter_scale) ||
    486       (prev_header_info.noise_bands != pstr_sbr_header->noise_bands)) {
    487     return SBR_RESET;
    488   }
    489 
    490   return 0;
    491 }
    492 
    493 static VOID ixheaacd_sbr_sin_coding_data(
    494     ia_sbr_header_data_struct *ptr_header_data,
    495     ia_sbr_frame_info_data_struct *ptr_frame_data,
    496     ia_bit_buf_struct *it_bit_buff) {
    497   FLAG *p_add_harmonic = ptr_frame_data->add_harmonics;
    498   WORD32 i;
    499 
    500   i = ptr_header_data->pstr_freq_band_data->num_sf_bands[HIGH];
    501   do {
    502     *p_add_harmonic++ =
    503         (FLAG)ixheaacd_read_bits_buf(it_bit_buff, SBR_ADD_SINE_FLAG_BITS);
    504     i--;
    505   } while (i != 0);
    506 
    507   return;
    508 }
    509 
    510 static WORD16 ixheaacd_validate_frame_info(
    511     ia_frame_info_struct *pstr_frame_info, WORD16 num_time_slots,
    512     WORD audio_object_type) {
    513   WORD32 i, j;
    514 
    515   WORD32 start_pos, end_pos, transient_env, start_pos_noise, end_pos_noise,
    516       num_env_sf, num_noise_env;
    517 
    518   num_env_sf = pstr_frame_info->num_env;
    519   num_noise_env = pstr_frame_info->num_noise_env;
    520 
    521   if ((num_env_sf < 1) || (num_env_sf > MAX_ENVELOPES)) return 0;
    522 
    523   if (num_noise_env > MAX_NOISE_ENVELOPES) return 0;
    524 
    525   start_pos = pstr_frame_info->border_vec[0];
    526   end_pos = pstr_frame_info->border_vec[num_env_sf];
    527   transient_env = pstr_frame_info->transient_env;
    528 
    529   if (transient_env > num_env_sf) return 0;
    530 
    531   start_pos_noise = pstr_frame_info->noise_border_vec[0];
    532   end_pos_noise = pstr_frame_info->noise_border_vec[num_noise_env];
    533 
    534   if ((start_pos < 0) || (start_pos >= end_pos)) return 0;
    535 
    536   if (start_pos > SBR_OV_SLOTS) return 0;
    537   if (audio_object_type != AOT_ER_AAC_ELD &&
    538       audio_object_type != AOT_ER_AAC_LD) {
    539     if (end_pos < SBR_TIME_SLOTS) return 0;
    540   } else {
    541     if (end_pos < num_time_slots) return 0;
    542   }
    543 
    544   if (end_pos > add_d(SBR_TIME_SLOTS, SBR_OV_SLOTS)) return 0;
    545 
    546   for (i = 0; i < num_env_sf; i++) {
    547     if (pstr_frame_info->border_vec[i] > pstr_frame_info->border_vec[i + 1])
    548       return 0;
    549   }
    550 
    551   if ((num_env_sf == 1) && (num_noise_env > 1)) return 0;
    552 
    553   if ((start_pos != start_pos_noise) || (end_pos != end_pos_noise)) return 0;
    554 
    555   for (i = 0; i < num_noise_env; i++) {
    556     start_pos_noise = pstr_frame_info->noise_border_vec[i];
    557 
    558     for (j = 0; j < num_env_sf; j++) {
    559       if (pstr_frame_info->border_vec[j] == start_pos_noise) break;
    560     }
    561     if (j == num_env_sf) return 0;
    562   }
    563 
    564   return 1;
    565 }
    566 
    567 static WORD16 ixheaacd_read_extn_data(
    568     ia_sbr_header_data_struct *ptr_header_data, ia_ps_dec_struct *ptr_ps_dec,
    569     ia_bit_buf_struct *it_bit_buff, ia_ps_tables_struct *ps_tables_ptr) {
    570   WORD i;
    571   WORD extended_data;
    572   WORD no_bits_left;
    573 
    574   extended_data = ixheaacd_read_bits_buf(it_bit_buff, SBR_ENLARGED_DATA_BITS);
    575 
    576   if (extended_data) {
    577     WORD cnt;
    578     FLAG ps_read;
    579 
    580     ps_read = 0;
    581 
    582     cnt = ixheaacd_read_bits_buf(it_bit_buff, SBR_CONT_SIZE_BITS);
    583 
    584     if (cnt == ((1 << SBR_CONT_SIZE_BITS) - 1)) {
    585       cnt = (cnt + ixheaacd_read_bits_buf(it_bit_buff, SBR_CONT_ESC_CNT_BITS));
    586     }
    587 
    588     no_bits_left = (cnt << 3);
    589 
    590     while (no_bits_left > 7) {
    591       WORD extension_id = ixheaacd_read_bits_buf(it_bit_buff, SBR_CONT_ID_BITS);
    592 
    593       no_bits_left = (no_bits_left - SBR_CONT_ID_BITS);
    594 
    595       switch (extension_id) {
    596         case EXTENSION_ID_PS_CODING:
    597 
    598           if (ptr_ps_dec == NULL) {
    599             return 0;
    600           }
    601 
    602           if (!(ptr_ps_dec->force_mono || ps_read)) {
    603             no_bits_left =
    604                 (no_bits_left - ixheaacd_read_ps_data(ptr_ps_dec, it_bit_buff,
    605                                                       (WORD16)no_bits_left,
    606                                                       ps_tables_ptr));
    607 
    608             if (no_bits_left < 0) return 0;
    609 
    610             ptr_header_data->channel_mode = PS_STEREO;
    611             ps_read = 1;
    612             break;
    613           }
    614 
    615         default:
    616           cnt = (no_bits_left >> 3);
    617           for (i = cnt - 1; i >= 0; i--) ixheaacd_read_bits_buf(it_bit_buff, 8);
    618           no_bits_left = (no_bits_left - (cnt << 3));
    619           break;
    620       }
    621     }
    622 
    623     if (no_bits_left < 0) return 0;
    624 
    625     ixheaacd_read_bits_buf(it_bit_buff, no_bits_left);
    626   }
    627   return 1;
    628 }
    629 
    630 WORD32 ixheaacd_sbr_read_pvc_sce(ia_sbr_frame_info_data_struct *ptr_frame_data,
    631                                  ia_bit_buf_struct *it_bit_buff,
    632                                  WORD32 hbe_flag,
    633                                  ia_pvc_data_struct *ptr_pvc_data,
    634                                  ia_sbr_tables_struct *ptr_sbr_tables,
    635                                  ia_sbr_header_data_struct *ptr_header_data) {
    636   WORD32 i;
    637   WORD32 err_code = 0;
    638   ia_env_extr_tables_struct *env_extr_tables_ptr =
    639       ptr_sbr_tables->env_extr_tables_ptr;
    640   WORD32 usac_independency_flag = ptr_frame_data->usac_independency_flag;
    641 
    642   if (hbe_flag) {
    643     ptr_frame_data->sbr_patching_mode =
    644         ixheaacd_read_bits_buf(it_bit_buff, ESBR_PATCHING_MODE_BITS);
    645 
    646     if (ptr_frame_data->sbr_patching_mode == 0) {
    647       ptr_frame_data->over_sampling_flag =
    648           ixheaacd_read_bits_buf(it_bit_buff, ESBR_OVERSAMPLING_FLAG_BITS);
    649       if (ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_FLAG_BITS))
    650         ptr_frame_data->pitch_in_bins =
    651             ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_BINS_BITS);
    652       else
    653         ptr_frame_data->pitch_in_bins = 0;
    654     } else {
    655       ptr_frame_data->over_sampling_flag = ptr_frame_data->pitch_in_bins = 0;
    656     }
    657   }
    658 
    659   err_code = ixheaacd_pvc_time_freq_grid_info(it_bit_buff, ptr_frame_data);
    660   if (err_code) return err_code;
    661 
    662   ptr_pvc_data->prev_sbr_mode = PVC_SBR;
    663 
    664   ixheaacd_pvc_env_dtdf_data(ptr_frame_data, it_bit_buff);
    665 
    666   for (i = 0; i < ptr_header_data->pstr_freq_band_data->num_nf_bands; i++) {
    667     ptr_frame_data->sbr_invf_mode_prev[i] = ptr_frame_data->sbr_invf_mode[i];
    668     ptr_frame_data->sbr_invf_mode[i] =
    669         (WORD32)ixheaacd_read_bits_buf(it_bit_buff, ESBR_INVF_MODE_BITS);
    670   }
    671 
    672   ptr_pvc_data->pvc_mode = ptr_header_data->pvc_mode;
    673 
    674   ixheaacd_read_esbr_pvc_envelope(ptr_pvc_data, it_bit_buff,
    675                                   usac_independency_flag);
    676 
    677   ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data,
    678                                      it_bit_buff, env_extr_tables_ptr);
    679 
    680   memset(ptr_frame_data->add_harmonics, 0,
    681          ptr_header_data->pstr_freq_band_data->num_sf_bands[HIGH] *
    682              sizeof(WORD32));
    683   ptr_frame_data->pvc_mode = ptr_header_data->pvc_mode;
    684 
    685   ixheaacd_read_sbr_addi_data(ptr_frame_data, ptr_header_data, it_bit_buff);
    686 
    687   ptr_frame_data->coupling_mode = COUPLING_OFF;
    688 
    689   return 0;
    690 }
    691 
    692 WORD8 ixheaacd_sbr_read_sce(ia_sbr_header_data_struct *ptr_header_data,
    693                             ia_sbr_frame_info_data_struct *ptr_frame_data,
    694                             ia_ps_dec_struct *ptr_ps_dec,
    695                             ia_bit_buf_struct *it_bit_buff,
    696                             ia_sbr_tables_struct *ptr_sbr_tables,
    697                             WORD audio_object_type) {
    698   WORD32 bit;
    699   WORD32 i;
    700   WORD32 hbe_flag = ptr_header_data->hbe_flag;
    701   WORD32 num_if_bands = ptr_header_data->pstr_freq_band_data->num_if_bands;
    702   WORD32 usac_flag = ptr_header_data->usac_flag;
    703   ia_env_extr_tables_struct *env_extr_tables_ptr =
    704       ptr_sbr_tables->env_extr_tables_ptr;
    705 
    706   ptr_frame_data->coupling_mode = COUPLING_OFF;
    707 
    708   if (!usac_flag) {
    709     bit = ixheaacd_read_bits_buf(it_bit_buff, 1);
    710 
    711     if (bit) ixheaacd_read_bits_buf(it_bit_buff, SBR_SCE_RESERV_BITS);
    712     if (audio_object_type == AOT_ER_AAC_ELD ||
    713         audio_object_type == AOT_ER_AAC_LD) {
    714       if (ptr_frame_data->eld_sbr_flag == 1) {
    715         if (!ixheaacd_extract_frame_info_ld(it_bit_buff, ptr_frame_data))
    716           return 0;
    717       }
    718     } else {
    719       if (!ixheaacd_sbr_time_freq_grid_info(it_bit_buff, ptr_frame_data,
    720                                             env_extr_tables_ptr))
    721 
    722         return 0;
    723     }
    724     if (!ixheaacd_validate_frame_info(&ptr_frame_data->str_frame_info_details,
    725                                       ptr_header_data->num_time_slots,
    726                                       audio_object_type))
    727       return 0;
    728 
    729   } else {
    730     if (hbe_flag) {
    731       ptr_frame_data->sbr_patching_mode =
    732           ixheaacd_read_bits_buf(it_bit_buff, ESBR_PATCHING_MODE_BITS);
    733       if (ptr_frame_data->sbr_patching_mode == 0) {
    734         ptr_frame_data->over_sampling_flag =
    735             ixheaacd_read_bits_buf(it_bit_buff, ESBR_OVERSAMPLING_FLAG_BITS);
    736         if (ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_FLAG_BITS))
    737           ptr_frame_data->pitch_in_bins =
    738               ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_BINS_BITS);
    739         else
    740           ptr_frame_data->pitch_in_bins = 0;
    741       } else {
    742         ptr_frame_data->over_sampling_flag = ptr_frame_data->pitch_in_bins = 0;
    743       }
    744     }
    745     ptr_frame_data->num_time_slots = ptr_header_data->num_time_slots;
    746     if (!ixheaacd_sbr_time_freq_grid_info(it_bit_buff, ptr_frame_data,
    747                                           env_extr_tables_ptr))
    748       return 0;
    749 
    750     if (!ixheaacd_validate_frame_info(&ptr_frame_data->str_frame_info_details,
    751                                       ptr_header_data->num_time_slots,
    752                                       audio_object_type))
    753       return 0;
    754 
    755     ptr_frame_data->prev_sbr_mode = ORIG_SBR;
    756   }
    757 
    758   ixheaacd_sbr_env_dtdf_data(ptr_frame_data, it_bit_buff,
    759                              ptr_header_data->usac_flag);
    760 
    761   if (ptr_frame_data->del_cod_dir_arr[0] == DTDF_DIR_FREQ) {
    762     ptr_header_data->err_flag = 0;
    763   }
    764 
    765   for (i = 0; i < num_if_bands; i++) {
    766     ptr_frame_data->sbr_invf_mode_prev[i] = ptr_frame_data->sbr_invf_mode[i];
    767     ptr_frame_data->sbr_invf_mode[i] =
    768         (WORD32)ixheaacd_read_bits_buf(it_bit_buff, SBR_INVERSE_FILT_MODE_BITS);
    769   }
    770 
    771   if (!ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data, it_bit_buff,
    772                                   env_extr_tables_ptr, audio_object_type))
    773     return 0;
    774 
    775   ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data,
    776                                      it_bit_buff, env_extr_tables_ptr);
    777 
    778   if (usac_flag) {
    779     memset(
    780         ptr_frame_data->add_harmonics, 0,
    781         ptr_header_data->pstr_freq_band_data->num_sf_bands[1] * sizeof(WORD32));
    782     ptr_frame_data->coupling_mode = COUPLING_OFF;
    783   }
    784 
    785   bit = (FLAG)ixheaacd_read_bits_buf(it_bit_buff, 1);
    786   if (bit) {
    787     ixheaacd_sbr_sin_coding_data(ptr_header_data, ptr_frame_data, it_bit_buff);
    788   } else {
    789     memset(ptr_frame_data->add_harmonics, 0, sizeof(FLAG) * MAX_FREQ_COEFFS);
    790   }
    791 
    792   if (!usac_flag) {
    793     ixheaacd_read_extn_data(ptr_header_data, ptr_ps_dec, it_bit_buff,
    794                             ptr_sbr_tables->ps_tables_ptr);
    795   }
    796 
    797   return 1;
    798 }
    799 
    800 WORD8 ixheaacd_sbr_read_cpe(ia_sbr_header_data_struct *ptr_header_data,
    801                             ia_sbr_frame_info_data_struct **ptr_frame_data,
    802                             ia_bit_buf_struct *it_bit_buff,
    803                             ia_sbr_tables_struct *ptr_sbr_tables,
    804                             WORD audio_object_type) {
    805   WORD32 i, k, bit, num_ch = 2;
    806   WORD32 num_if_bands = ptr_header_data->pstr_freq_band_data->num_if_bands;
    807   WORD32 hbe_flag = ptr_header_data->hbe_flag;
    808   WORD32 usac_flag = ptr_header_data->usac_flag;
    809 
    810   ia_env_extr_tables_struct *env_extr_tables_ptr =
    811       ptr_sbr_tables->env_extr_tables_ptr;
    812   bit = ixheaacd_read_bits_buf(it_bit_buff, 1);
    813 
    814   if (usac_flag) {
    815     if (bit) {
    816       if (hbe_flag) {
    817         ptr_frame_data[0]->sbr_patching_mode =
    818             ptr_frame_data[1]->sbr_patching_mode =
    819                 ixheaacd_read_bits_buf(it_bit_buff, ESBR_PATCHING_MODE_BITS);
    820         if (ptr_frame_data[0]->sbr_patching_mode == 0) {
    821           ptr_frame_data[0]->over_sampling_flag =
    822               ptr_frame_data[1]->over_sampling_flag = ixheaacd_read_bits_buf(
    823                   it_bit_buff, ESBR_OVERSAMPLING_FLAG_BITS);
    824           if (ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_FLAG_BITS))
    825             ptr_frame_data[0]->pitch_in_bins =
    826                 ptr_frame_data[1]->pitch_in_bins =
    827                     ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_BINS_BITS);
    828           else
    829             ptr_frame_data[0]->pitch_in_bins =
    830                 ptr_frame_data[1]->pitch_in_bins = 0;
    831         } else {
    832           ptr_frame_data[0]->over_sampling_flag = 0;
    833           ptr_frame_data[1]->over_sampling_flag = 0;
    834           ptr_frame_data[0]->pitch_in_bins = 0;
    835           ptr_frame_data[1]->pitch_in_bins = 0;
    836         }
    837       }
    838       ptr_frame_data[0]->coupling_mode = COUPLING_LEVEL;
    839       ptr_frame_data[1]->coupling_mode = COUPLING_BAL;
    840     } else {
    841       if (hbe_flag) {
    842         ptr_frame_data[0]->sbr_patching_mode =
    843             ixheaacd_read_bits_buf(it_bit_buff, ESBR_PATCHING_MODE_BITS);
    844         if (ptr_frame_data[0]->sbr_patching_mode == 0) {
    845           ptr_frame_data[0]->over_sampling_flag =
    846               ixheaacd_read_bits_buf(it_bit_buff, ESBR_OVERSAMPLING_FLAG_BITS);
    847           if (ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_FLAG_BITS))
    848             ptr_frame_data[0]->pitch_in_bins =
    849                 ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_BINS_BITS);
    850           else
    851             ptr_frame_data[0]->pitch_in_bins = 0;
    852         } else {
    853           ptr_frame_data[0]->over_sampling_flag = 0;
    854           ptr_frame_data[0]->pitch_in_bins = 0;
    855         }
    856         ptr_frame_data[1]->sbr_patching_mode =
    857             ixheaacd_read_bits_buf(it_bit_buff, ESBR_PATCHING_MODE_BITS);
    858         if (ptr_frame_data[1]->sbr_patching_mode == 0) {
    859           ptr_frame_data[1]->over_sampling_flag =
    860               ixheaacd_read_bits_buf(it_bit_buff, ESBR_OVERSAMPLING_FLAG_BITS);
    861           if (ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_FLAG_BITS))
    862             ptr_frame_data[1]->pitch_in_bins =
    863                 ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_BINS_BITS);
    864           else
    865             ptr_frame_data[1]->pitch_in_bins = 0;
    866         } else {
    867           ptr_frame_data[1]->over_sampling_flag =
    868               ptr_frame_data[1]->pitch_in_bins = 0;
    869         }
    870       }
    871 
    872       ptr_frame_data[0]->coupling_mode = COUPLING_OFF;
    873       ptr_frame_data[1]->coupling_mode = COUPLING_OFF;
    874     }
    875   } else {
    876     if (bit) {
    877       ixheaacd_read_bits_buf(it_bit_buff,
    878                              SBR_SCE_RESERV_BITS + SBR_SCE_RESERV_BITS);
    879     }
    880     if ((audio_object_type != AOT_ER_AAC_ELD) &&
    881         (ptr_header_data->channel_mode != SBR_STEREO)) {
    882       ptr_header_data->sync_state = UPSAMPLING;
    883       return 0;
    884     }
    885 
    886     bit = ixheaacd_read_bits_buf(it_bit_buff, SBR_COUPLNG_MODE_BITS);
    887 
    888     if (bit) {
    889       ptr_frame_data[0]->coupling_mode = COUPLING_LEVEL;
    890       ptr_frame_data[1]->coupling_mode = COUPLING_BAL;
    891     } else {
    892       ptr_frame_data[0]->coupling_mode = COUPLING_OFF;
    893       ptr_frame_data[1]->coupling_mode = COUPLING_OFF;
    894     }
    895   }
    896 
    897   for (i = 0; i < num_ch; i++) {
    898     ptr_frame_data[i]->num_time_slots = ptr_header_data->num_time_slots;
    899     if (audio_object_type == AOT_ER_AAC_ELD ||
    900         audio_object_type == AOT_ER_AAC_LD) {
    901       if (ptr_frame_data[i]->eld_sbr_flag == 1) {
    902         if (!ixheaacd_extract_frame_info_ld(it_bit_buff, ptr_frame_data[i]))
    903           return 0;
    904       }
    905     } else {
    906       if (!ixheaacd_sbr_time_freq_grid_info(it_bit_buff, ptr_frame_data[i],
    907                                             env_extr_tables_ptr))
    908         return 0;
    909     }
    910 
    911     if (!ixheaacd_validate_frame_info(
    912             &ptr_frame_data[i]->str_frame_info_details,
    913             ptr_header_data->num_time_slots, audio_object_type))
    914       return 0;
    915 
    916     if (ptr_frame_data[0]->coupling_mode) {
    917       memcpy(&ptr_frame_data[1]->str_frame_info_details,
    918              &ptr_frame_data[0]->str_frame_info_details,
    919              sizeof(ia_frame_info_struct));
    920       if (audio_object_type == AOT_ER_AAC_ELD ||
    921           audio_object_type == AOT_ER_AAC_LD) {
    922         ptr_frame_data[1]->amp_res = ptr_frame_data[0]->amp_res;
    923       }
    924       num_ch = 1;
    925     }
    926   }
    927 
    928   if (ptr_frame_data[0]->coupling_mode && usac_flag) {
    929     ixheaacd_sbr_env_dtdf_data(ptr_frame_data[0], it_bit_buff,
    930                                ptr_header_data->usac_flag);
    931     ixheaacd_sbr_env_dtdf_data(ptr_frame_data[1], it_bit_buff,
    932                                ptr_header_data->usac_flag);
    933 
    934     for (i = 0; i < ptr_header_data->pstr_freq_band_data->num_if_bands; i++) {
    935       ptr_frame_data[0]->sbr_invf_mode_prev[i] =
    936           ptr_frame_data[0]->sbr_invf_mode[i];
    937       ptr_frame_data[1]->sbr_invf_mode_prev[i] =
    938           ptr_frame_data[1]->sbr_invf_mode[i];
    939 
    940       ptr_frame_data[0]->sbr_invf_mode[i] =
    941           (WORD32)ixheaacd_read_bits_buf(it_bit_buff, ESBR_INVF_MODE_BITS);
    942       ptr_frame_data[1]->sbr_invf_mode[i] = ptr_frame_data[0]->sbr_invf_mode[i];
    943     }
    944 
    945     ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data[0], it_bit_buff,
    946                                env_extr_tables_ptr, audio_object_type);
    947     ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data[0],
    948                                        it_bit_buff, env_extr_tables_ptr);
    949 
    950     ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data[1], it_bit_buff,
    951                                env_extr_tables_ptr, audio_object_type);
    952     ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data[1],
    953                                        it_bit_buff, env_extr_tables_ptr);
    954 
    955     memset(
    956         ptr_frame_data[0]->add_harmonics, 0,
    957         ptr_header_data->pstr_freq_band_data->num_sf_bands[1] * sizeof(WORD32));
    958     memset(
    959         ptr_frame_data[1]->add_harmonics, 0,
    960         ptr_header_data->pstr_freq_band_data->num_sf_bands[1] * sizeof(WORD32));
    961 
    962   } else {
    963     ixheaacd_sbr_env_dtdf_data(ptr_frame_data[0], it_bit_buff,
    964                                ptr_header_data->usac_flag);
    965     ixheaacd_sbr_env_dtdf_data(ptr_frame_data[1], it_bit_buff,
    966                                ptr_header_data->usac_flag);
    967 
    968     if ((ptr_frame_data[0]->del_cod_dir_arr[0] == DTDF_DIR_FREQ) &&
    969         (ptr_frame_data[1]->del_cod_dir_arr[0] == DTDF_DIR_FREQ)) {
    970       ptr_header_data->err_flag = 0;
    971     }
    972 
    973     for (k = 0; k < num_ch; k++) {
    974       for (i = 0; i < num_if_bands; i++) {
    975         ptr_frame_data[k]->sbr_invf_mode_prev[i] =
    976             ptr_frame_data[k]->sbr_invf_mode[i];
    977         ptr_frame_data[k]->sbr_invf_mode[i] = (WORD32)ixheaacd_read_bits_buf(
    978             it_bit_buff, SBR_INVERSE_FILT_MODE_BITS);
    979       }
    980     }
    981 
    982     if (ptr_frame_data[0]->coupling_mode) {
    983       memcpy(ptr_frame_data[1]->sbr_invf_mode, ptr_frame_data[0]->sbr_invf_mode,
    984              sizeof(WORD32) * num_if_bands);
    985 
    986       if (!ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data[0],
    987                                       it_bit_buff, env_extr_tables_ptr,
    988                                       audio_object_type)) {
    989         return 0;
    990       }
    991 
    992       ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data[0],
    993                                          it_bit_buff, env_extr_tables_ptr);
    994 
    995       if (!ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data[1],
    996                                       it_bit_buff, env_extr_tables_ptr,
    997                                       audio_object_type)) {
    998         return 0;
    999       }
   1000     } else {
   1001       if (!ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data[0],
   1002                                       it_bit_buff, env_extr_tables_ptr,
   1003                                       audio_object_type))
   1004         return 0;
   1005 
   1006       if (!ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data[1],
   1007                                       it_bit_buff, env_extr_tables_ptr,
   1008                                       audio_object_type))
   1009         return 0;
   1010 
   1011       ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data[0],
   1012                                          it_bit_buff, env_extr_tables_ptr);
   1013     }
   1014     ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data[1],
   1015                                        it_bit_buff, env_extr_tables_ptr);
   1016   }
   1017 
   1018   bit = (FLAG)ixheaacd_read_bits_buf(it_bit_buff, 1);
   1019   if (bit) {
   1020     ixheaacd_sbr_sin_coding_data(ptr_header_data, ptr_frame_data[0],
   1021                                  it_bit_buff);
   1022   } else {
   1023     memset(ptr_frame_data[0]->add_harmonics, 0, sizeof(FLAG) * MAX_FREQ_COEFFS);
   1024   }
   1025 
   1026   bit = (FLAG)ixheaacd_read_bits_buf(it_bit_buff, 1);
   1027   if (bit) {
   1028     ixheaacd_sbr_sin_coding_data(ptr_header_data, ptr_frame_data[1],
   1029                                  it_bit_buff);
   1030   } else {
   1031     memset(ptr_frame_data[1]->add_harmonics, 0, sizeof(FLAG) * MAX_FREQ_COEFFS);
   1032   }
   1033 
   1034   if (!usac_flag) {
   1035     ixheaacd_read_extn_data(ptr_header_data, NULL, it_bit_buff,
   1036                             ptr_sbr_tables->ps_tables_ptr);
   1037   }
   1038   return 1;
   1039 }
   1040 
   1041 VOID ixheaacd_sbr_env_dtdf_data(ia_sbr_frame_info_data_struct *ptr_frame_data,
   1042                                 ia_bit_buf_struct *it_bit_buff,
   1043                                 WORD32 usac_flag) {
   1044   WORD32 i;
   1045   WORD32 num_env = ptr_frame_data->str_frame_info_details.num_env;
   1046   WORD32 num_noise_env = ptr_frame_data->str_frame_info_details.num_noise_env;
   1047   WORD16 *p_coding_dir_vec = ptr_frame_data->del_cod_dir_arr;
   1048   WORD16 *p_coding_dir_noise_vec = ptr_frame_data->del_cod_dir_noise_arr;
   1049   WORD32 usac_independency_flag = ptr_frame_data->usac_independency_flag;
   1050 
   1051   if (usac_flag) {
   1052     if (usac_independency_flag) {
   1053       *p_coding_dir_vec = 0;
   1054       p_coding_dir_vec++;
   1055     } else {
   1056       *p_coding_dir_vec =
   1057           (WORD16)ixheaacd_read_bits_buf(it_bit_buff, SBR_DEL_COD_DIR_BITS);
   1058       p_coding_dir_vec++;
   1059     }
   1060     for (i = num_env - 1; i >= 1; i--) {
   1061       *p_coding_dir_vec++ =
   1062           (WORD16)ixheaacd_read_bits_buf(it_bit_buff, SBR_DEL_COD_DIR_BITS);
   1063     }
   1064     if (usac_independency_flag) {
   1065       *p_coding_dir_noise_vec = 0;
   1066       p_coding_dir_noise_vec++;
   1067     } else {
   1068       *p_coding_dir_noise_vec =
   1069           (WORD16)ixheaacd_read_bits_buf(it_bit_buff, SBR_DEL_COD_DIR_BITS);
   1070       p_coding_dir_noise_vec++;
   1071     }
   1072     for (i = num_noise_env - 1; i >= 1; i--) {
   1073       *p_coding_dir_noise_vec++ =
   1074           (WORD16)ixheaacd_read_bits_buf(it_bit_buff, SBR_DEL_COD_DIR_BITS);
   1075     }
   1076   } else {
   1077     for (i = num_env - 1; i >= 0; i--) {
   1078       *p_coding_dir_vec++ =
   1079           (WORD16)ixheaacd_read_bits_buf(it_bit_buff, SBR_DEL_COD_DIR_BITS);
   1080     }
   1081 
   1082     for (i = num_noise_env - 1; i >= 0; i--) {
   1083       *p_coding_dir_noise_vec++ =
   1084           (WORD16)ixheaacd_read_bits_buf(it_bit_buff, SBR_DEL_COD_DIR_BITS);
   1085     }
   1086   }
   1087 }
   1088 
   1089 VOID ixheaacd_read_env_data(ia_sbr_frame_info_data_struct *ptr_frame_data,
   1090                             ia_bit_buf_struct *it_bit_buff,
   1091                             ia_huffman_data_type hcb_t,
   1092                             ia_huffman_data_type hcb_f, WORD32 *idx_t,
   1093                             WORD32 *idx_f, WORD16 *no_band, WORD32 num_env,
   1094                             WORD32 env_data_tbl_comp_factor, WORD32 start_bits,
   1095                             WORD32 start_bits_balance, WORD32 num_noise_env,
   1096                             WORD32 lav, WORD32 usac_flag) {
   1097   WORD32 j, i, ixheaacd_drc_offset = 0,
   1098                coupling_mode = ptr_frame_data->coupling_mode, delta, bits,
   1099                shift;
   1100   WORD16 *p_coding_dir_vec, *p_sbr_sf;
   1101   WORD16 index, length;
   1102   WORD32 readword;
   1103   FLOAT32 *p_sbr_sf_float;
   1104 
   1105   if (num_noise_env) {
   1106     p_coding_dir_vec = ptr_frame_data->del_cod_dir_noise_arr;
   1107     p_sbr_sf = ptr_frame_data->int_noise_floor;
   1108     p_sbr_sf_float = ptr_frame_data->flt_noise_floor;
   1109   } else {
   1110     p_coding_dir_vec = ptr_frame_data->del_cod_dir_arr;
   1111     p_sbr_sf = ptr_frame_data->int_env_sf_arr;
   1112     p_sbr_sf_float = ptr_frame_data->flt_env_sf_arr;
   1113   }
   1114 
   1115   if (coupling_mode == COUPLING_BAL) {
   1116     bits = start_bits_balance;
   1117     shift = env_data_tbl_comp_factor;
   1118 
   1119   } else {
   1120     bits = start_bits;
   1121     shift = 0;
   1122   }
   1123 
   1124   for (j = 0; j < num_env; j++) {
   1125     ia_huffman_data_type h;
   1126     const WORD32 *idx_tab;
   1127     WORD32 dtdf_dir_flag = p_coding_dir_vec[j];
   1128 
   1129     if (dtdf_dir_flag == DTDF_DIR_FREQ) {
   1130       p_sbr_sf[ixheaacd_drc_offset] =
   1131           (WORD16)(ixheaacd_read_bits_buf(it_bit_buff, bits) << shift);
   1132       p_sbr_sf_float[ixheaacd_drc_offset] = p_sbr_sf[ixheaacd_drc_offset];
   1133       h = hcb_f;
   1134       idx_tab = idx_f;
   1135     } else {
   1136       h = hcb_t;
   1137       idx_tab = idx_t;
   1138     }
   1139 
   1140     for (i = (1 - dtdf_dir_flag); i < no_band[j]; i++) {
   1141       readword = ixheaacd_show_bits_buf(it_bit_buff, 20);
   1142       ixheaacd_huffman_decode(readword << 12, &index, &length,
   1143                               (const UWORD16 *)h, (const UWORD32 *)idx_tab);
   1144       delta = index - lav;
   1145       ixheaacd_read_bits_buf(it_bit_buff, length);
   1146       p_sbr_sf[ixheaacd_drc_offset + i] =
   1147           (WORD16)(delta << env_data_tbl_comp_factor);
   1148       p_sbr_sf_float[ixheaacd_drc_offset + i] =
   1149           p_sbr_sf[ixheaacd_drc_offset + i];
   1150     }
   1151     if (usac_flag && (num_noise_env == 0)) {
   1152       ptr_frame_data->inter_temp_shape_mode[j] = 0;
   1153       if (ptr_frame_data->inter_tes_flag) {
   1154         WORD32 flag = (WORD32)ixheaacd_read_bits_buf(it_bit_buff, 1);
   1155         if (flag) {
   1156           ptr_frame_data->inter_temp_shape_mode[j] =
   1157               (WORD32)ixheaacd_read_bits_buf(it_bit_buff, 2);
   1158         }
   1159       }
   1160     }
   1161     ixheaacd_drc_offset += (no_band[j]);
   1162   }
   1163 }
   1164 
   1165 VOID ixheaacd_read_sbr_noise_floor_data(
   1166     ia_sbr_header_data_struct *ptr_header_data,
   1167     ia_sbr_frame_info_data_struct *ptr_frame_data,
   1168     ia_bit_buf_struct *it_bit_buff,
   1169     ia_env_extr_tables_struct *env_extr_tables_ptr) {
   1170   WORD32 i;
   1171   WORD32 coupling_mode;
   1172   WORD16 num_noise_bands[MAX_NOISE_ENVELOPES];
   1173   ia_huffman_data_type hcb_noise_env;
   1174   ia_huffman_data_type hcb_noise;
   1175   WORD32 *idx_noise_env;
   1176   WORD32 *idx_noise;
   1177   WORD32 lav;
   1178   WORD32 env_data_tbl_comp_factor;
   1179 
   1180   WORD32 start_bits;
   1181   WORD32 start_bits_balance;
   1182   WORD32 num_noise_env = ptr_frame_data->str_frame_info_details.num_noise_env;
   1183 
   1184   for (i = 0; i < num_noise_env; i++)
   1185     num_noise_bands[i] = ptr_header_data->pstr_freq_band_data->num_nf_bands;
   1186 
   1187   start_bits = SBR_BEGIN_NOISE_BITS_AMPLITUDE_RESOLUTION_3_0;
   1188   start_bits_balance = SBR_BEGIN_NOISE_BITS_BALNCE_AMPLITUDE_RESOLUTION_3_0;
   1189 
   1190   coupling_mode = ptr_frame_data->coupling_mode;
   1191 
   1192   if (coupling_mode == COUPLING_BAL) {
   1193     lav = 12;
   1194     hcb_noise = (ia_huffman_data_type)&env_extr_tables_ptr
   1195                     ->ixheaacd_t_huffman_noise_bal_3_0db_inp_table;
   1196     idx_noise =
   1197         env_extr_tables_ptr->ixheaacd_t_huffman_noise_bal_3_0db_idx_table;
   1198     hcb_noise_env = (ia_huffman_data_type)&env_extr_tables_ptr
   1199                         ->ixheaacd_f_huffman_env_bal_3_0db_inp_table;
   1200     idx_noise_env =
   1201         env_extr_tables_ptr->ixheaacd_f_huffman_env_bal_3_0db_idx_table;
   1202     env_data_tbl_comp_factor = 1;
   1203   } else {
   1204     lav = 31;
   1205     hcb_noise = (ia_huffman_data_type)&env_extr_tables_ptr
   1206                     ->ixheaacd_t_huffman_noise_3_0db_inp_table;
   1207     idx_noise = env_extr_tables_ptr->ixheaacd_t_huffman_noise_3_0db_idx_table;
   1208     hcb_noise_env = (ia_huffman_data_type)&env_extr_tables_ptr
   1209                         ->ixheaacd_f_huffman_env_3_0db_inp_table;
   1210     idx_noise_env = env_extr_tables_ptr->ixheaacd_f_huffman_env_3_0db_idx_table;
   1211     env_data_tbl_comp_factor = 0;
   1212   }
   1213 
   1214   ixheaacd_read_env_data(ptr_frame_data, it_bit_buff, hcb_noise, hcb_noise_env,
   1215                          idx_noise, idx_noise_env, &num_noise_bands[0],
   1216                          num_noise_env, env_data_tbl_comp_factor, start_bits,
   1217                          start_bits_balance, 1, lav,
   1218                          ptr_header_data->usac_flag);
   1219 }
   1220 
   1221 WORD16 ixheaacd_read_sbr_env_data(
   1222     ia_sbr_header_data_struct *ptr_header_data,
   1223     ia_sbr_frame_info_data_struct *ptr_frame_data,
   1224     ia_bit_buf_struct *it_bit_buff,
   1225     ia_env_extr_tables_struct *env_extr_tables_ptr, WORD audio_object_type) {
   1226   WORD32 coupling_mode = ptr_frame_data->coupling_mode;
   1227   WORD32 *idx_t, *idx_f;
   1228   WORD32 lav;
   1229   WORD32 i;
   1230   WORD16 no_band[MAX_ENVELOPES];
   1231   WORD32 delta;
   1232   WORD32 amp_res, num_env, env_data_tbl_comp_factor, start_bits,
   1233       start_bits_balance;
   1234   WORD16 *p_freq_res = ptr_frame_data->str_frame_info_details.freq_res;
   1235   WORD16 *p_num_sf_bands = ptr_header_data->pstr_freq_band_data->num_sf_bands;
   1236   ia_huffman_data_type hcb_t, hcb_f;
   1237 
   1238   delta = 0;
   1239   amp_res = ptr_header_data->amp_res;
   1240   num_env = ptr_frame_data->str_frame_info_details.num_env;
   1241 
   1242   ptr_frame_data->num_env_sfac = 0;
   1243 
   1244   if ((ptr_frame_data->str_frame_info_details.frame_class == FIXFIX) &&
   1245       (num_env == 1)) {
   1246     if (audio_object_type != AOT_ER_AAC_ELD &&
   1247         audio_object_type != AOT_ER_AAC_LD) {
   1248       amp_res = SBR_AMPLITUDE_RESOLUTION_1_5;
   1249     } else {
   1250       amp_res = ptr_frame_data->amp_res;
   1251     }
   1252   }
   1253   ptr_frame_data->amp_res = amp_res;
   1254 
   1255   if (amp_res == SBR_AMPLITUDE_RESOLUTION_3_0) {
   1256     start_bits = SBR_BEGIN_ENVN_BITS_AMPLITUDE_RESOLUTION_3_0;
   1257     start_bits_balance = SBR_BEGIN_ENVN_BITS_BALNCE_AMPLITUDE_RESOLUTION_3_0;
   1258   } else {
   1259     start_bits = SBR_BEGIN_ENVN_BITS_AMPLITUDE_RESOLUTION_1_5;
   1260     start_bits_balance = SBR_BEGIN_ENVN_BITS_BALNCE_AMPLITUDE_RESOLUTION_1_5;
   1261   }
   1262 
   1263   for (i = 0; i < num_env; i++) {
   1264     no_band[i] = p_num_sf_bands[*p_freq_res++];
   1265     ptr_frame_data->num_env_sfac =
   1266         ixheaacd_add16(ptr_frame_data->num_env_sfac, no_band[i]);
   1267   }
   1268 
   1269   if (ptr_frame_data->num_env_sfac > MAX_NUM_ENVELOPE_VALUES) return 0;
   1270 
   1271   if (coupling_mode == COUPLING_BAL) {
   1272     env_data_tbl_comp_factor = 1;
   1273 
   1274     if (amp_res == SBR_AMPLITUDE_RESOLUTION_1_5) {
   1275       lav = 24;
   1276       hcb_t = (ia_huffman_data_type)&env_extr_tables_ptr
   1277                   ->ixheaacd_t_huffman_env_bal_1_5db_inp_table;
   1278       idx_t = env_extr_tables_ptr->ixheaacd_t_huffman_env_bal_1_5db_idx_table;
   1279       hcb_f = (ia_huffman_data_type)&env_extr_tables_ptr
   1280                   ->ixheaacd_f_huffman_env_bal_1_5db_inp_table;
   1281       idx_f = env_extr_tables_ptr->ixheaacd_f_huffman_env_bal_1_5db_idx_table;
   1282     } else {
   1283       lav = 12;
   1284       hcb_t = (ia_huffman_data_type)&env_extr_tables_ptr
   1285                   ->ixheaacd_t_huffman_env_bal_3_0db_inp_table;
   1286       idx_t = env_extr_tables_ptr->ixheaacd_t_huffman_env_bal_3_0db_idx_table;
   1287       hcb_f = (ia_huffman_data_type)&env_extr_tables_ptr
   1288                   ->ixheaacd_f_huffman_env_bal_3_0db_inp_table;
   1289       idx_f = env_extr_tables_ptr->ixheaacd_f_huffman_env_bal_3_0db_idx_table;
   1290     }
   1291   } else {
   1292     env_data_tbl_comp_factor = 0;
   1293 
   1294     if (amp_res == SBR_AMPLITUDE_RESOLUTION_1_5) {
   1295       lav = 60;
   1296       hcb_t = (ia_huffman_data_type)&env_extr_tables_ptr
   1297                   ->ixheaacd_t_huffman_env_1_5db_inp_table;
   1298       idx_t = env_extr_tables_ptr->ixheaacd_t_huffman_env_1_5db_idx_table;
   1299       hcb_f = (ia_huffman_data_type)&env_extr_tables_ptr
   1300                   ->ixheaacd_f_huffman_env_1_5db_inp_table;
   1301       idx_f = env_extr_tables_ptr->ixheaacd_f_huffman_env_1_5db_idx_table;
   1302     } else {
   1303       lav = 31;
   1304       hcb_t = (ia_huffman_data_type)&env_extr_tables_ptr
   1305                   ->ixheaacd_t_huffman_env_3_0db_inp_table;
   1306       idx_t = env_extr_tables_ptr->ixheaacd_t_huffman_env_3_0db_idx_table;
   1307       hcb_f = (ia_huffman_data_type)&env_extr_tables_ptr
   1308                   ->ixheaacd_f_huffman_env_3_0db_inp_table;
   1309       idx_f = env_extr_tables_ptr->ixheaacd_f_huffman_env_3_0db_idx_table;
   1310     }
   1311   }
   1312 
   1313   ixheaacd_read_env_data(ptr_frame_data, it_bit_buff, hcb_t, hcb_f, idx_t,
   1314                          idx_f, &no_band[0], num_env, env_data_tbl_comp_factor,
   1315                          start_bits, start_bits_balance, 0, lav,
   1316                          ptr_header_data->usac_flag);
   1317 
   1318   return 1;
   1319 }
   1320 
   1321 int ixheaacd_extract_frame_info_ld(
   1322     ia_bit_buf_struct *it_bit_buff,
   1323     ia_sbr_frame_info_data_struct *h_frame_data) {
   1324   int abs_bord_lead = 0, num_rel_lead = 0, num_rel_trail = 0, bs_num_env = 0,
   1325       frame_class, temp, env, k, abs_bord_trail = 0, middle_bord = 0,
   1326       bs_num_noise, transient_env_temp = 0, bs_transient_position = 0;
   1327 
   1328   WORD16 time_border[MAX_ENVELOPES + 1];
   1329   WORD16 time_border_noise[2 + 1];
   1330   WORD16 f[MAX_ENVELOPES + 1];
   1331   int rel_bord_lead[3];
   1332   int rel_bord_trail[3] = {0};
   1333 
   1334   ia_frame_info_struct *v_frame_info = &h_frame_data->str_frame_info_details;
   1335 
   1336   int numTimeSlots = h_frame_data->num_time_slots;
   1337 
   1338   v_frame_info->frame_class = frame_class =
   1339       ixheaacd_read_bits_buf(it_bit_buff, SBRLD_CLA_BITS);
   1340 
   1341   switch (frame_class) {
   1342     case FIXFIX:
   1343       temp = ixheaacd_read_bits_buf(it_bit_buff, SBR_ENV_BITS);
   1344       bs_num_env = 1 << temp;
   1345 
   1346       if (bs_num_env == 1)
   1347         h_frame_data->amp_res =
   1348             ixheaacd_read_bits_buf(it_bit_buff, SBR_AMPLITUDE_RESOLUTION_BITS);
   1349 
   1350       f[0] = ixheaacd_read_bits_buf(it_bit_buff, SBR_RES_BITS);
   1351 
   1352       for (env = 1; env < bs_num_env; env++) f[env] = f[0];
   1353       break;
   1354     case LD_TRAN:
   1355       bs_transient_position =
   1356           ixheaacd_read_bits_buf(it_bit_buff, SBR_TRAN_BITS);
   1357       v_frame_info->frame_class = 0;
   1358       bs_num_env = (numTimeSlots == 16)
   1359                        ? ixheaacd_ld_env_table_512[bs_transient_position]
   1360                                                   [SBR_ENVT_NUMENV]
   1361                        : ixheaacd_ld_env_table_480[bs_transient_position]
   1362                                                   [SBR_ENVT_NUMENV];
   1363       for (env = 0; env < bs_num_env; env++)
   1364         f[env] = ixheaacd_read_bits_buf(it_bit_buff, SBR_RES_BITS);
   1365       break;
   1366   }
   1367 
   1368   switch (frame_class) {
   1369     case FIXFIX:
   1370       abs_bord_lead = 0;
   1371       abs_bord_trail = numTimeSlots;
   1372       num_rel_lead = bs_num_env - 1;
   1373       num_rel_trail = 0;
   1374 
   1375       for (k = 0; k < num_rel_lead; k++) {
   1376         rel_bord_lead[k] = ixheaacd_ld_env_table_time_slot[num_rel_lead - 1];
   1377       }
   1378 
   1379       time_border[0] = abs_bord_lead;
   1380       time_border[bs_num_env] = abs_bord_trail;
   1381       for (env = 1; env <= num_rel_lead; env++) {
   1382         time_border[env] = abs_bord_lead;
   1383         for (k = 0; k <= env - 1; k++) time_border[env] += rel_bord_lead[k];
   1384       }
   1385       for (env = num_rel_lead + 1; env < bs_num_env; env++) {
   1386         time_border[env] = abs_bord_trail;
   1387         for (k = 0; k <= bs_num_env - env - 1; k++)
   1388           time_border[env] -= rel_bord_trail[k];
   1389       }
   1390       break;
   1391 
   1392     case LD_TRAN:
   1393       time_border[0] = 0;
   1394       time_border[bs_num_env] = numTimeSlots;
   1395       for (k = 1; k < bs_num_env; k++)
   1396         time_border[k] =
   1397             (numTimeSlots == 16)
   1398                 ? ixheaacd_ld_env_table_512[bs_transient_position][k]
   1399                 : ixheaacd_ld_env_table_480[bs_transient_position][k];
   1400       break;
   1401 
   1402     default:
   1403       time_border[0] = 0;
   1404 
   1405       break;
   1406   };
   1407 
   1408   switch (frame_class) {
   1409     case FIXFIX:
   1410       middle_bord = bs_num_env / 2;
   1411       break;
   1412     case LD_TRAN:
   1413       middle_bord = 1;
   1414       break;
   1415   };
   1416 
   1417   time_border_noise[0] = time_border[0];
   1418   if (bs_num_env > 1) {
   1419     time_border_noise[1] = time_border[middle_bord];
   1420     time_border_noise[2] = time_border[bs_num_env];
   1421     bs_num_noise = 2;
   1422   } else {
   1423     time_border_noise[1] = time_border[bs_num_env];
   1424     bs_num_noise = 1;
   1425   }
   1426 
   1427   switch (frame_class) {
   1428     case FIXFIX:
   1429       transient_env_temp = -1;
   1430       break;
   1431     case LD_TRAN:
   1432       transient_env_temp =
   1433           (numTimeSlots == 16)
   1434               ? ixheaacd_ld_env_table_512[bs_transient_position]
   1435                                          [SBR_ENVT_TRANIDX]
   1436               : ixheaacd_ld_env_table_480[bs_transient_position]
   1437                                          [SBR_ENVT_TRANIDX];
   1438       break;
   1439   };
   1440 
   1441   v_frame_info->num_env = bs_num_env;
   1442   memcpy(v_frame_info->border_vec, time_border,
   1443          (bs_num_env + 1) * sizeof(WORD16));
   1444   memcpy(v_frame_info->freq_res, f, bs_num_env * sizeof(WORD16));
   1445   v_frame_info->transient_env = transient_env_temp;
   1446   v_frame_info->num_noise_env = bs_num_noise;
   1447   memcpy(v_frame_info->noise_border_vec, time_border_noise,
   1448          (bs_num_noise + 1) * sizeof(WORD16));
   1449 
   1450   return 1;
   1451 }
   1452 
   1453 WORD32 ixheaacd_pvc_time_freq_grid_info(
   1454     ia_bit_buf_struct *it_bit_buff,
   1455     ia_sbr_frame_info_data_struct *ptr_frame_data) {
   1456   WORD32 bs_num_env = 0, bs_num_noise = 0;
   1457   WORD32 time_border[MAX_ENVELOPES + 1];
   1458   WORD32 time_border_noise[2 + 1];
   1459   WORD32 pvc_time_border[MAX_ENVELOPES + 1];
   1460   WORD32 pvc_time_border_noise[2 + 1];
   1461   WORD32 bs_freq_res[MAX_ENVELOPES + 1];
   1462   WORD32 var_len;
   1463   ia_frame_info_struct *p_frame_info = &ptr_frame_data->str_frame_info_details;
   1464   ia_frame_info_struct *pvc_frame_info = &ptr_frame_data->str_pvc_frame_info;
   1465   WORD32 i;
   1466   WORD32 prev_sbr_mode = ptr_frame_data->prev_sbr_mode;
   1467 
   1468   WORD32 tmp;
   1469   WORD32 bs_noise_pos;
   1470   bs_noise_pos = ixheaacd_read_bits_buf(it_bit_buff, 4);
   1471 
   1472   tmp = ixheaacd_read_bits_buf(it_bit_buff, 1);
   1473   if (tmp == 0) {
   1474     ptr_frame_data->var_len = 0;
   1475   } else {
   1476     tmp = ixheaacd_read_bits_buf(it_bit_buff, 2);
   1477     ptr_frame_data->var_len = tmp + 1;
   1478   }
   1479   var_len = ptr_frame_data->var_len;
   1480 
   1481   if (p_frame_info->num_env > 0) {
   1482     time_border[0] = p_frame_info->border_vec[p_frame_info->num_env] - 16;
   1483   } else {
   1484     time_border[0] = 0;
   1485   }
   1486   if (time_border[0] < 0) return -1;
   1487   pvc_time_border[0] = 0;
   1488   bs_freq_res[0] = 0;
   1489 
   1490   if (bs_noise_pos == 0) {
   1491     time_border[1] = 16 + var_len;
   1492     pvc_time_border[1] = 16;
   1493     bs_num_noise = 1;
   1494     bs_num_env = 1;
   1495   } else {
   1496     time_border[1] = bs_noise_pos;
   1497     pvc_time_border[1] = bs_noise_pos;
   1498     time_border[2] = 16 + var_len;
   1499     pvc_time_border[2] = 16;
   1500     bs_freq_res[1] = 0;
   1501     bs_num_noise = 2;
   1502     bs_num_env = 2;
   1503   }
   1504 
   1505   for (i = 0; i < 3; i++) {
   1506     time_border_noise[i] = time_border[i];
   1507     pvc_time_border_noise[i] = pvc_time_border[i];
   1508   }
   1509 
   1510   if (prev_sbr_mode == ORIG_SBR) {
   1511     pvc_time_border[0] = time_border[0];
   1512     pvc_time_border_noise[0] = time_border[0];
   1513   }
   1514 
   1515   pvc_frame_info->num_env = bs_num_env;
   1516   for (i = 0; i < (bs_num_env + 1); i++) {
   1517     pvc_frame_info->border_vec[i] = pvc_time_border[i];
   1518   }
   1519   for (i = 0; i < (bs_num_env); i++) {
   1520     pvc_frame_info->freq_res[i] = bs_freq_res[i];
   1521   }
   1522   pvc_frame_info->transient_env = -1;
   1523   pvc_frame_info->num_noise_env = bs_num_noise;
   1524   for (i = 0; i < (bs_num_noise + 1); i++) {
   1525     pvc_frame_info->noise_border_vec[i] = pvc_time_border_noise[i];
   1526   }
   1527   p_frame_info->num_env = bs_num_env;
   1528   for (i = 0; i < (bs_num_env + 1); i++) {
   1529     p_frame_info->border_vec[i] = time_border[i];
   1530   }
   1531   for (i = 0; i < (bs_num_env); i++) {
   1532     p_frame_info->freq_res[i] = bs_freq_res[i];
   1533   }
   1534   p_frame_info->transient_env = -1;
   1535   p_frame_info->num_noise_env = bs_num_noise;
   1536   for (i = 0; i < (bs_num_noise + 1); i++) {
   1537     p_frame_info->noise_border_vec[i] = time_border_noise[i];
   1538   }
   1539   return 0;
   1540 }
   1541 
   1542 WORD16 ixheaacd_sbr_time_freq_grid_info(
   1543     ia_bit_buf_struct *it_bit_buff,
   1544     ia_sbr_frame_info_data_struct *ptr_frame_data,
   1545     ia_env_extr_tables_struct *env_extr_tables_ptr) {
   1546   WORD32 i, k, bs_num_rel = 0;
   1547   WORD32 bs_pointer_bits = 0, bs_num_env = 0, border, bs_pointer,
   1548          bs_var_bord = 0, temp = 0;
   1549   WORD32 freq_res_0 = 0, frame_class;
   1550   WORD32 abs_bord_lead, abs_bord_trail, num_rel_trail, num_rel_lead;
   1551   WORD32 pointer_bits_array[7] = {1, 2, 2, 3, 3, 3, 3};
   1552   ia_frame_info_struct *p_fixfix_tab;
   1553   ia_frame_info_struct *p_frame_info = &ptr_frame_data->str_frame_info_details;
   1554 
   1555   frame_class = ixheaacd_read_bits_buf(it_bit_buff, SBR_FRAME_CLASS_BITS);
   1556   p_frame_info->frame_class = frame_class;
   1557 
   1558   switch (frame_class) {
   1559     case FIXFIX:
   1560       temp =
   1561           ixheaacd_read_bits_buf(it_bit_buff, SBR_ENV_BITS + SBR_FRQ_RES_BITS);
   1562       bs_num_env = (temp & 0x6) >> SBR_FRQ_RES_BITS;
   1563       p_fixfix_tab = &env_extr_tables_ptr->sbr_frame_info1_2_4_16[bs_num_env];
   1564       memcpy(p_frame_info, p_fixfix_tab, sizeof(ia_frame_info_struct));
   1565       bs_num_env = (1 << bs_num_env);
   1566       freq_res_0 = temp & 0x1;
   1567 
   1568       if (!freq_res_0) {
   1569         memset(&p_frame_info->freq_res[0], 0, sizeof(WORD16) * bs_num_env);
   1570       }
   1571       break;
   1572     case FIXVAR:
   1573       bs_var_bord =
   1574           ixheaacd_read_bits_buf(it_bit_buff, SBR_VAR_BORD_BITS + SBR_NUM_BITS);
   1575       bs_num_rel = bs_var_bord & 3;
   1576       bs_var_bord = bs_var_bord >> SBR_NUM_BITS;
   1577       bs_num_env = bs_num_rel + 1;
   1578       p_frame_info->border_vec[0] = 0;
   1579       border = bs_var_bord + SBR_TIME_SLOTS;
   1580       p_frame_info->border_vec[bs_num_env] = border;
   1581       for (k = bs_num_rel; k > 0; k--) {
   1582         temp = ixheaacd_read_bits_buf(it_bit_buff, SBR_REL_BITS);
   1583         border = border - ((temp << 1) + 2);
   1584         if (border < 0) border = 0;
   1585         p_frame_info->border_vec[k] = border;
   1586       }
   1587 
   1588       bs_pointer_bits = pointer_bits_array[bs_num_rel];
   1589       bs_pointer = ixheaacd_read_bits_buf(it_bit_buff, bs_pointer_bits);
   1590 
   1591       if ((bs_pointer - (bs_num_rel + 1)) > 0) return 0;
   1592 
   1593       for (k = bs_num_rel; k >= 0; k--) {
   1594         p_frame_info->freq_res[k] =
   1595             ixheaacd_read_bits_buf(it_bit_buff, SBR_FRQ_RES_BITS);
   1596       }
   1597       if (bs_pointer) {
   1598         p_frame_info->transient_env = bs_num_env + 1 - bs_pointer;
   1599       } else {
   1600         p_frame_info->transient_env = -1;
   1601       }
   1602       if ((bs_pointer == 0) || (bs_pointer == 1))
   1603         p_frame_info->noise_border_vec[1] =
   1604             p_frame_info->border_vec[bs_num_rel];
   1605       else
   1606         p_frame_info->noise_border_vec[1] =
   1607             p_frame_info->border_vec[p_frame_info->transient_env];
   1608 
   1609       break;
   1610 
   1611     case VARFIX:
   1612       bs_var_bord =
   1613           ixheaacd_read_bits_buf(it_bit_buff, SBR_VAR_BORD_BITS + SBR_NUM_BITS);
   1614       bs_num_rel = bs_var_bord & 3;
   1615       bs_var_bord = bs_var_bord >> SBR_NUM_BITS;
   1616       bs_num_env = bs_num_rel + 1;
   1617 
   1618       border = bs_var_bord;
   1619       p_frame_info->border_vec[0] = border;
   1620       for (k = 1; k <= bs_num_rel; k++) {
   1621         temp = ixheaacd_read_bits_buf(it_bit_buff, SBR_REL_BITS);
   1622         border = border + ((temp << 1) + 2);
   1623         if (border > SBR_TIME_SLOTS) border = SBR_TIME_SLOTS;
   1624         p_frame_info->border_vec[k] = border;
   1625       }
   1626       p_frame_info->border_vec[k] = SBR_TIME_SLOTS;
   1627 
   1628       bs_pointer_bits = pointer_bits_array[bs_num_rel];
   1629 
   1630       bs_pointer = ixheaacd_read_bits_buf(it_bit_buff, bs_pointer_bits);
   1631 
   1632       if ((bs_pointer - (bs_num_rel + 1)) > 0) return 0;
   1633 
   1634       if (bs_pointer == 0 || (bs_pointer - 1) == 0) {
   1635         p_frame_info->transient_env = -1;
   1636       } else {
   1637         p_frame_info->transient_env = bs_pointer - 1;
   1638       }
   1639 
   1640       for (k = 0; k <= bs_num_rel; k++) {
   1641         p_frame_info->freq_res[k] =
   1642             ixheaacd_read_bits_buf(it_bit_buff, SBR_FRQ_RES_BITS);
   1643       }
   1644 
   1645       switch (bs_pointer) {
   1646         case 0:
   1647           p_frame_info->noise_border_vec[1] = p_frame_info->border_vec[1];
   1648           break;
   1649         case 1:
   1650           p_frame_info->noise_border_vec[1] =
   1651               p_frame_info->border_vec[bs_num_rel];
   1652           break;
   1653         default:
   1654           p_frame_info->noise_border_vec[1] =
   1655               p_frame_info->border_vec[(WORD32)p_frame_info->transient_env];
   1656           break;
   1657       }
   1658 
   1659       break;
   1660 
   1661     case VARVAR:
   1662       abs_bord_lead = ixheaacd_read_bits_buf(
   1663           it_bit_buff, 2 * SBR_VAR_BORD_BITS + 2 * SBR_NUM_BITS);
   1664       abs_bord_trail =
   1665           (((abs_bord_lead & 0x30) >> (2 * SBR_NUM_BITS)) + SBR_TIME_SLOTS);
   1666       num_rel_trail = ((abs_bord_lead & 0xc) >> SBR_NUM_BITS);
   1667       num_rel_lead = (abs_bord_lead & 0x3);
   1668       abs_bord_lead = abs_bord_lead >> (SBR_VAR_BORD_BITS + 2 * SBR_NUM_BITS);
   1669       bs_num_env = ((num_rel_trail + num_rel_lead) + 1);
   1670       border = abs_bord_lead;
   1671       p_frame_info->border_vec[0] = border;
   1672 
   1673       for (k = 1; k <= num_rel_trail; k++) {
   1674         temp = ixheaacd_read_bits_buf(it_bit_buff, SBR_REL_BITS);
   1675         border = border + ((temp << 1) + 2);
   1676         p_frame_info->border_vec[k] = border;
   1677       }
   1678 
   1679       border = abs_bord_trail;
   1680       i = bs_num_env;
   1681 
   1682       p_frame_info->border_vec[i] = border;
   1683 
   1684       for (k = 0; k < num_rel_lead; k++) {
   1685         temp = ixheaacd_read_bits_buf(it_bit_buff, SBR_REL_BITS);
   1686         border = border - ((temp << 1) + 2);
   1687         i--;
   1688         p_frame_info->border_vec[i] = border;
   1689       }
   1690       bs_pointer_bits = pointer_bits_array[num_rel_trail + num_rel_lead];
   1691 
   1692       bs_pointer = ixheaacd_read_bits_buf(it_bit_buff, bs_pointer_bits);
   1693       if ((bs_pointer - ((num_rel_trail + num_rel_lead) + 1)) > 0) return 0;
   1694 
   1695       if (bs_pointer) {
   1696         p_frame_info->transient_env = bs_num_env + 1 - bs_pointer;
   1697       } else {
   1698         p_frame_info->transient_env = -1;
   1699       }
   1700 
   1701       for (k = 0; k < bs_num_env; k++) {
   1702         p_frame_info->freq_res[k] =
   1703             ixheaacd_read_bits_buf(it_bit_buff, SBR_FRQ_RES_BITS);
   1704       }
   1705       p_frame_info->noise_border_vec[0] = abs_bord_lead;
   1706       if (bs_num_env == 1) {
   1707         p_frame_info->noise_border_vec[1] = abs_bord_trail;
   1708       } else {
   1709         if (bs_pointer == 0 || (bs_pointer - 1) == 0)
   1710           p_frame_info->noise_border_vec[1] =
   1711               p_frame_info->border_vec[bs_num_env - 1];
   1712         else
   1713           p_frame_info->noise_border_vec[1] =
   1714               p_frame_info->border_vec[(WORD32)p_frame_info->transient_env];
   1715 
   1716         p_frame_info->noise_border_vec[2] = abs_bord_trail;
   1717       }
   1718       break;
   1719   }
   1720   p_frame_info->num_env = bs_num_env;
   1721 
   1722   if (bs_num_env == 1)
   1723     p_frame_info->num_noise_env = 1;
   1724   else
   1725     p_frame_info->num_noise_env = 2;
   1726 
   1727   if (frame_class == VARFIX || frame_class == FIXVAR) {
   1728     p_frame_info->noise_border_vec[0] = p_frame_info->border_vec[0];
   1729     p_frame_info->noise_border_vec[p_frame_info->num_noise_env] =
   1730         p_frame_info->border_vec[bs_num_env];
   1731   }
   1732   return 1;
   1733 }
   1734