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 "ixheaacd_sbr_common.h"
     21 #include <ixheaacd_type_def.h>
     22 #include "ixheaacd_constants.h"
     23 #include <ixheaacd_basic_ops32.h>
     24 #include <ixheaacd_basic_ops16.h>
     25 #include <ixheaacd_basic_ops40.h>
     26 #include "ixheaacd_basic_ops.h"
     27 
     28 #include "ixheaacd_defines.h"
     29 #include <ixheaacd_aac_rom.h>
     30 #include "ixheaacd_aac_imdct.h"
     31 #include "ixheaacd_bitbuffer.h"
     32 #include <ixheaacd_basic_op.h>
     33 #include "ixheaacd_intrinsics.h"
     34 
     35 #include "ixheaacd_pulsedata.h"
     36 
     37 #include "ixheaacd_pns.h"
     38 #include "ixheaacd_common_rom.h"
     39 #include "ixheaacd_drc_data_struct.h"
     40 
     41 #include "ixheaacd_lt_predict.h"
     42 
     43 #include "ixheaacd_channelinfo.h"
     44 #include "ixheaacd_cnst.h"
     45 #include "ixheaacd_drc_dec.h"
     46 
     47 #include "ixheaacd_block.h"
     48 #include "ixheaacd_channel.h"
     49 
     50 #include "ixheaacd_pulsedata.h"
     51 #include "ixheaacd_pns.h"
     52 #include "ixheaacd_sbrdecoder.h"
     53 #include "ixheaacd_error_codes.h"
     54 #include "ixheaacd_audioobjtypes.h"
     55 #include "ixheaacd_latmdemux.h"
     56 #include "ixheaacd_aacdec.h"
     57 
     58 #define LONG_BLOCK_SECT_LEN 5
     59 #define SHORT_BLOCK_SECT_LEN 3
     60 
     61 WORD16 ixheaacd_read_section_data(
     62     ia_bit_buf_struct *it_bit_buff,
     63     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info,
     64     WORD32 aac_spect_data_resil_flag, WORD32 aac_sect_data_resil_flag,
     65     ia_aac_dec_tables_struct *ptr_aac_tables) {
     66   WORD sfb;
     67   WORD sect_cb;
     68   WORD sect_len;
     69   WORD sect_len_incr;
     70   WORD sect_esc_val;
     71   ia_ics_info_struct *ptr_ics_info = &ptr_aac_dec_channel_info->str_ics_info;
     72   WORD max_sfb = ptr_ics_info->max_sfb;
     73   WORD num_win_group;
     74 
     75   WORD8 *ptr_code_book = ptr_aac_dec_channel_info->ptr_code_book;
     76   WORD8 *ptr_code_book_temp = ptr_code_book;
     77   WORD32 sect_bitlen = LONG_BLOCK_SECT_LEN;
     78   int num_lines_sec_idx = 0, top;
     79   short *ptr_num_sect_lines =
     80       ptr_aac_dec_channel_info->num_line_in_sec4_hcr_arr;
     81   UWORD8 *ptr_hcr_code_book = ptr_aac_dec_channel_info->cb4_hcr_arr;
     82   const short *band_offsets = (WORD16 *)ixheaacd_getscalefactorbandoffsets(
     83       &(ptr_aac_dec_channel_info->str_ics_info), ptr_aac_tables);
     84   ptr_aac_dec_channel_info->number_sect = 0;
     85 
     86   if (ptr_aac_dec_channel_info->str_ics_info.window_sequence ==
     87       EIGHT_SHORT_SEQUENCE) {
     88     sect_bitlen = SHORT_BLOCK_SECT_LEN;
     89   }
     90 
     91   sect_esc_val = (1 << sect_bitlen) - 1;
     92 
     93   for (num_win_group = 0; num_win_group < ptr_ics_info->num_window_groups;
     94        num_win_group++) {
     95     sfb = 0;
     96 
     97     while (sfb < max_sfb) {
     98       sect_len = 0;
     99       if (aac_sect_data_resil_flag) {
    100         sect_cb = ixheaacd_read_bits_buf(it_bit_buff, 5);
    101       } else {
    102         sect_cb = ixheaacd_read_bits_buf(it_bit_buff, 4);
    103       }
    104 
    105       if ((aac_sect_data_resil_flag == 0) ||
    106           ((sect_cb < 11) || ((sect_cb > 11) && (sect_cb < 16)))) {
    107         sect_len_incr = ixheaacd_read_bits_buf(it_bit_buff, sect_bitlen);
    108         while (sect_len_incr == sect_esc_val) {
    109           sect_len = (sect_len + sect_esc_val);
    110           sect_len_incr = ixheaacd_read_bits_buf(it_bit_buff, sect_bitlen);
    111         }
    112       } else
    113         sect_len_incr = 1;
    114 
    115       sect_len = (sect_len + sect_len_incr);
    116       top = (sfb + sect_len);
    117 
    118       if (aac_spect_data_resil_flag) {
    119         if (num_lines_sec_idx >= MAX_SFB_HCR) {
    120           return -1;
    121         }
    122         ptr_num_sect_lines[num_lines_sec_idx] =
    123             band_offsets[top] - band_offsets[sfb];
    124         num_lines_sec_idx++;
    125         if (sect_cb == (ESC_HCB + 1)) {
    126           return IA_ENHAACPLUS_DEC_EXE_NONFATAL_INVALID_CODE_BOOK;
    127         } else {
    128           *ptr_hcr_code_book++ = sect_cb;
    129         }
    130         ptr_aac_dec_channel_info->number_sect++;
    131       }
    132 
    133       sfb = (sfb + sect_len);
    134       if (sfb > max_sfb) {
    135         return (WORD16)(
    136             (WORD32)IA_ENHAACPLUS_DEC_EXE_NONFATAL_EXCEEDS_SFB_TRANSMITTED);
    137       }
    138       if (sect_cb == (ESC_HCB + 1)) {
    139         return (WORD16)(
    140             (WORD32)IA_ENHAACPLUS_DEC_EXE_NONFATAL_INVALID_CODE_BOOK);
    141       }
    142 
    143       while (sect_len--) {
    144         *ptr_code_book_temp++ = sect_cb;
    145       }
    146     }
    147     ptr_code_book += MAX_SCALE_FACTOR_BANDS_SHORT;
    148     ptr_code_book_temp = ptr_code_book;
    149   }
    150 
    151   return AAC_DEC_OK;
    152 }
    153 
    154 VOID ixheaacd_read_scale_factor_data(
    155     ia_bit_buf_struct *it_bit_buff,
    156     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info,
    157     ia_aac_dec_tables_struct *ptr_aac_tables, WORD32 object_type) {
    158   WORD sfb;
    159   WORD16 position = 0;
    160   WORD num_win_group;
    161   WORD16 factor = ptr_aac_dec_channel_info->global_gain;
    162   WORD8 *ptr_code_book, *ptr_code_book_short;
    163   WORD16 *ptr_scale_fact, *ptr_scale_fact_short;
    164   WORD16 norm_value;
    165   WORD16 index, length;
    166   const UWORD16 *hcod_sf =
    167       ptr_aac_tables->pstr_huffmann_tables->huffman_code_book_scl;
    168   const UWORD32 *table_idx =
    169       ptr_aac_tables->pstr_huffmann_tables->huffman_code_book_scl_index;
    170 
    171   WORD start_bit_pos = it_bit_buff->bit_pos;
    172   UWORD8 *start_read_pos = it_bit_buff->ptr_read_next;
    173   UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
    174   WORD32 bit_pos = 7 - it_bit_buff->bit_pos;
    175   WORD32 read_word;
    176   WORD32 diffbytes;
    177 
    178   diffbytes = it_bit_buff->ptr_bit_buf_end - ptr_read_next;
    179   diffbytes++;
    180   if (diffbytes >= 4) {
    181     read_word = ixheaacd_aac_showbits_32(ptr_read_next);
    182     diffbytes = 4;
    183     ptr_read_next = it_bit_buff->ptr_read_next + 4;
    184   } else {
    185     WORD32 ii;
    186     read_word = 0;
    187     for (ii = 0; ii < diffbytes; ii++) {
    188       read_word = (read_word << 8) | (*ptr_read_next);
    189       ptr_read_next++;
    190     }
    191     read_word <<= ((4 - diffbytes) << 3);
    192   }
    193 
    194   ptr_code_book = ptr_aac_dec_channel_info->ptr_code_book;
    195 
    196   ptr_scale_fact = ptr_aac_dec_channel_info->ptr_scale_factor;
    197 
    198   for (num_win_group = 0;
    199        num_win_group < ptr_aac_dec_channel_info->str_ics_info.num_window_groups;
    200        num_win_group++) {
    201     ptr_code_book_short =
    202         &ptr_code_book[num_win_group * MAX_SCALE_FACTOR_BANDS_SHORT];
    203     ptr_scale_fact_short =
    204         &ptr_scale_fact[num_win_group * MAX_SCALE_FACTOR_BANDS_SHORT];
    205     for (sfb = ptr_aac_dec_channel_info->str_ics_info.max_sfb - 1; sfb >= 0;
    206          sfb--) {
    207       WORD32 sfb_cb = *ptr_code_book_short++;
    208       if (sfb_cb == ZERO_HCB)
    209         *ptr_scale_fact_short++ = 0;
    210       else {
    211         {
    212           WORD32 pns_present = 0;
    213           WORD pns_band;
    214 
    215           ia_pns_info_struct *ptr_pns_info =
    216               &ptr_aac_dec_channel_info->str_pns_info;
    217 
    218           if (sfb_cb == NOISE_HCB && (ptr_pns_info->pns_active != 1)) {
    219             pns_present = 1;
    220           }
    221 
    222           if (!pns_present) {
    223             UWORD32 read_word1;
    224 
    225             read_word1 = read_word << bit_pos;
    226 
    227             ixheaacd_huffman_decode(read_word1, &index, &length, hcod_sf,
    228                                     table_idx);
    229 
    230             bit_pos += length;
    231             ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
    232                                         it_bit_buff->ptr_bit_buf_end);
    233             while (bit_pos > 8)
    234               ixheaacd_aac_read_byte(&ptr_read_next, &bit_pos, &read_word);
    235 
    236             norm_value = index - 60;
    237           }
    238 
    239           else {
    240             WORD32 noise_start_value;
    241             UWORD32 temp;
    242 
    243             temp = (read_word << bit_pos);
    244             temp = ((UWORD32)temp >> (32 - 9));
    245             noise_start_value = temp;
    246             bit_pos += 9;
    247 
    248             ixheaacd_aac_read_2bytes(&ptr_read_next, &bit_pos, &read_word);
    249 
    250             norm_value = noise_start_value - 256;
    251             ptr_pns_info->pns_active = 1;
    252 
    253             ptr_pns_info->noise_energy =
    254                 ptr_aac_dec_channel_info->global_gain - NOISE_OFFSET;
    255           }
    256 
    257           if ((object_type != AOT_ER_AAC_ELD) &&
    258               (object_type != AOT_ER_AAC_LD)) {
    259             if (sfb_cb > NOISE_HCB) {
    260               position = position + norm_value;
    261               *ptr_scale_fact_short++ = -position;
    262             } else if (sfb_cb < NOISE_HCB) {
    263               factor = factor + norm_value;
    264               *ptr_scale_fact_short++ = factor;
    265             } else {
    266               ptr_pns_info->noise_energy =
    267                   ixheaacd_add16_sat(ptr_pns_info->noise_energy, norm_value);
    268 
    269               pns_band = (num_win_group << 4) +
    270                          ptr_aac_dec_channel_info->str_ics_info.max_sfb - sfb -
    271                          1;
    272               ptr_aac_dec_channel_info->ptr_scale_factor[pns_band] =
    273                   ptr_pns_info->noise_energy;
    274 
    275               ptr_pns_info->pns_used[pns_band] = 1;
    276               ptr_scale_fact_short++;
    277             }
    278           } else {
    279             if ((sfb_cb == INTENSITY_HCB) || (sfb_cb == INTENSITY_HCB2)) {
    280               position = position + norm_value;
    281               *ptr_scale_fact_short++ = -position;
    282             } else if (sfb_cb == NOISE_HCB) {
    283               ptr_pns_info->noise_energy =
    284                   ixheaacd_add16_sat(ptr_pns_info->noise_energy, norm_value);
    285 
    286               pns_band = (num_win_group << 4) +
    287                          ptr_aac_dec_channel_info->str_ics_info.max_sfb - sfb -
    288                          1;
    289               ptr_aac_dec_channel_info->ptr_scale_factor[pns_band] =
    290                   ptr_pns_info->noise_energy;
    291 
    292               ptr_pns_info->pns_used[pns_band] = 1;
    293               ptr_scale_fact_short++;
    294 
    295             } else {
    296               factor = factor + norm_value;
    297               *ptr_scale_fact_short++ = factor;
    298             }
    299           }
    300         }
    301       }
    302     }
    303   }
    304 
    305   it_bit_buff->ptr_read_next = ptr_read_next - diffbytes;
    306 
    307   it_bit_buff->bit_pos = 7 - bit_pos;
    308   {
    309     WORD bits_consumed;
    310     bits_consumed = ((it_bit_buff->ptr_read_next - start_read_pos) << 3) +
    311                     (start_bit_pos - it_bit_buff->bit_pos);
    312     it_bit_buff->cnt_bits -= bits_consumed;
    313   }
    314 }
    315