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 <math.h>
     23 
     24 #include <ixheaacd_type_def.h>
     25 #include <ixheaacd_type_def.h>
     26 #include "ixheaacd_memory_standards.h"
     27 #include "ixheaacd_sbrdecsettings.h"
     28 #include "ixheaacd_env_extr_part.h"
     29 #include "ixheaacd_defines.h"
     30 #include <ixheaacd_aac_rom.h>
     31 #include "ixheaacd_common_rom.h"
     32 #include <ixheaacd_sbr_rom.h>
     33 #include "ixheaacd_bitbuffer.h"
     34 #include "ixheaacd_pulsedata.h"
     35 #include "ixheaacd_pns.h"
     36 
     37 #include "ixheaacd_sbr_common.h"
     38 #include "ixheaacd_drc_data_struct.h"
     39 #include "ixheaacd_drc_dec.h"
     40 
     41 #include "ixheaacd_lt_predict.h"
     42 #include "ixheaacd_channelinfo.h"
     43 #include "ixheaacd_channel.h"
     44 #include "ixheaacd_sbrdecoder.h"
     45 #include "ixheaacd_audioobjtypes.h"
     46 #include "ixheaacd_latmdemux.h"
     47 #include "ixheaacd_aacdec.h"
     48 #include "ixheaacd_sbr_common.h"
     49 
     50 #include "ixheaacd_mps_polyphase.h"
     51 #include "ixheaacd_config.h"
     52 #include "ixheaacd_mps_dec.h"
     53 #include "ixheaacd_mps_interface.h"
     54 #include "ixheaacd_struct_def.h"
     55 #include "ixheaacd_config.h"
     56 #include "ixheaacd_bitbuffer.h"
     57 #include "ixheaacd_interface.h"
     58 
     59 #include "ixheaacd_tns_usac.h"
     60 #include "ixheaacd_cnst.h"
     61 #include "ixheaacd_acelp_info.h"
     62 #include "ixheaacd_sbrdecsettings.h"
     63 #include "ixheaacd_info.h"
     64 #include "ixheaacd_struct.h"
     65 
     66 #include "ixheaacd_sbrdecoder.h"
     67 #include "ixheaacd_mps_polyphase.h"
     68 #include "ixheaacd_sbr_const.h"
     69 #include "ixheaacd_main.h"
     70 #include "ixheaacd_arith_dec.h"
     71 #include "ixheaacd_bit_extract.h"
     72 #include "ixheaacd_create.h"
     73 
     74 #include "ixheaacd_func_def.h"
     75 #include "ixheaacd_mps_interface.h"
     76 #include "ixheaacd_mps_polyphase.h"
     77 
     78 #include "ixheaacd_defines.h"
     79 
     80 #include "ixheaacd_sbr_scale.h"
     81 #include "ixheaacd_lpp_tran.h"
     82 #include "ixheaacd_hybrid.h"
     83 #include "ixheaacd_ps_dec.h"
     84 #include "ixheaacd_env_extr.h"
     85 #include "ixheaacd_env_calc.h"
     86 #include "ixheaacd_qmf_dec.h"
     87 
     88 #include "ixheaacd_pvc_dec.h"
     89 #include "ixheaacd_sbr_dec.h"
     90 
     91 #include "ixheaacd_error_codes.h"
     92 
     93 #define MAXNRSBRELEMENTS 6
     94 
     95 VOID ixheaacd_allocate_sbr_scr(ia_sbr_scr_struct *sbr_scratch_struct,
     96                                VOID *base_scratch_ptr, VOID *output_ptr,
     97                                WORD total_elements, WORD ch_fac,
     98                                WORD32 object_type);
     99 
    100 IA_ERRORCODE ixheaacd_applysbr(
    101     ia_handle_sbr_dec_inst_struct self,
    102     ia_aac_dec_sbr_bitstream_struct *p_sbr_bit_stream, WORD16 *core_sample_buf,
    103     WORD16 *codec_num_channels, FLAG frame_status, FLAG down_samp_flag,
    104     FLAG down_mix_flag, ia_sbr_scr_struct *sbr_scratch_struct, WORD32 ps_enable,
    105     WORD32 ch_fac, WORD32 slot_element, ia_bit_buf_struct *it_bit_buff,
    106     ia_drc_dec_struct *pstr_drc_dec, WORD eld_sbr_flag, WORD32 object_type);
    107 
    108 IA_ERRORCODE ixheaacd_esbr_process(ia_usac_data_struct *usac_data,
    109                                    ia_bit_buf_struct *it_bit_buff,
    110                                    WORD32 stereo_config_idx,
    111                                    WORD16 num_channels,
    112                                    WORD32 audio_object_type) {
    113   WORD32 err_code = 0;
    114   ia_aac_dec_sbr_bitstream_struct *esbr_bit_str = &usac_data->esbr_bit_str[0];
    115   ia_handle_sbr_dec_inst_struct self = usac_data->pstr_esbr_dec;
    116 
    117   ia_sbr_scr_struct sbr_scratch_struct;
    118   ixheaacd_allocate_sbr_scr(&sbr_scratch_struct,
    119                             usac_data->sbr_scratch_mem_base, NULL, 2, 1,
    120                             audio_object_type);
    121 
    122   self->usac_independency_flag = usac_data->usac_independency_flg;
    123 
    124   self->time_sample_buf[0] = usac_data->time_sample_vector[0];
    125   self->time_sample_buf[1] = usac_data->time_sample_vector[1];
    126   self->stereo_config_idx = stereo_config_idx;
    127 
    128   self->sbr_mode = usac_data->sbr_mode;
    129   self->aot_usac_flag = usac_data->usac_flag;
    130 
    131   err_code = ixheaacd_applysbr(self, esbr_bit_str, NULL, &num_channels, 1, 0, 0,
    132                                &sbr_scratch_struct, 0, 1, 0, it_bit_buff, NULL,
    133                                0, audio_object_type);
    134 
    135   usac_data->sbr_mode = self->sbr_mode;
    136 
    137   return err_code;
    138 }
    139 
    140 static VOID ixheaacd_sbr_ext_data_read(
    141     ia_bit_buf_struct *it_bit_buff,
    142     ia_aac_dec_sbr_bitstream_struct *esbr_bit_str) {
    143   WORD32 count = 0;
    144   WORD32 read_bits = 0;
    145   WORD32 unaligned_bits = 0;
    146   WORD32 cnt_bits_in;
    147 
    148   cnt_bits_in = it_bit_buff->cnt_bits;
    149   count = (it_bit_buff->cnt_bits) >> 3;
    150   if (count > 0) {
    151     if ((esbr_bit_str->no_elements < MAXNRSBRELEMENTS)) {
    152       esbr_bit_str->str_sbr_ele[esbr_bit_str->no_elements].extension_type =
    153           EXT_SBR_DATA;
    154       esbr_bit_str->str_sbr_ele[esbr_bit_str->no_elements].size_payload = count;
    155 
    156       read_bits = count << 3;
    157 
    158       unaligned_bits = (cnt_bits_in - read_bits);
    159       if (unaligned_bits > 0 && unaligned_bits < 8) {
    160         count++;
    161         esbr_bit_str->str_sbr_ele[esbr_bit_str->no_elements].size_payload =
    162             count;
    163       }
    164       esbr_bit_str->no_elements += 1;
    165     }
    166   }
    167 }
    168 
    169 static WORD32 ixheaacd_read_ext_element(
    170     UWORD32 usac_ext_element_default_length,
    171     UWORD32 usac_ext_element_payload_frag, ia_bit_buf_struct *it_bit_buff,
    172     ia_usac_decoder_config_struct *pstr_usac_dec_config, WORD32 elem_idx) {
    173   UWORD32 usac_ext_element_present;
    174   UWORD32 usac_ext_element_use_dft_length;
    175   UWORD32 pay_load_length, tmp;
    176   UWORD32 i;
    177   usac_ext_element_present = ixheaacd_read_bits_buf(it_bit_buff, 1);
    178 
    179   if (usac_ext_element_present) {
    180     usac_ext_element_use_dft_length = ixheaacd_read_bits_buf(it_bit_buff, 1);
    181 
    182     if (usac_ext_element_use_dft_length) {
    183       pay_load_length = usac_ext_element_default_length;
    184     } else {
    185       pay_load_length = ixheaacd_read_bits_buf(it_bit_buff, 8);
    186 
    187       if (pay_load_length == 255) {
    188         WORD32 val_add = 0;
    189         val_add = ixheaacd_read_bits_buf(it_bit_buff, 16);
    190         pay_load_length = (UWORD32)((WORD32)pay_load_length + val_add - 2);
    191       }
    192     }
    193     if ((it_bit_buff->cnt_bits >> 3) < (WORD32)pay_load_length)
    194       return IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES;
    195     if (pay_load_length > 0) {
    196       if (usac_ext_element_payload_frag)
    197         tmp = ixheaacd_read_bits_buf(it_bit_buff, 2);
    198 
    199       if (pstr_usac_dec_config->usac_ext_ele_payload_present[elem_idx]) {
    200         for (i = 0; i < pay_load_length; i++) {
    201           pstr_usac_dec_config->usac_ext_gain_payload_buf
    202               [i + pstr_usac_dec_config->usac_ext_gain_payload_len] =
    203               ixheaacd_read_bits_buf(it_bit_buff, 8);
    204         }
    205         pstr_usac_dec_config->usac_ext_gain_payload_len += pay_load_length;
    206       } else {
    207         if (it_bit_buff->cnt_bits < (WORD32)(pay_load_length << 3)) {
    208           longjmp(*(it_bit_buff->xaac_jmp_buf),
    209                   IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
    210         }
    211         it_bit_buff->ptr_read_next =
    212             it_bit_buff->ptr_read_next + pay_load_length;
    213         it_bit_buff->cnt_bits =
    214             it_bit_buff->cnt_bits - (WORD32)(pay_load_length << 3);
    215       }
    216     }
    217   }
    218   return 0;
    219 }
    220 
    221 static VOID ixheaacd_sbr_ele_type_set(
    222     ia_aac_dec_sbr_bitstream_struct *esbr_bit_str0,
    223     ia_aac_dec_sbr_bitstream_struct *esbr_bit_str1, WORD32 ele_id,
    224     WORD32 st_config_idx) {
    225   if (ele_id == ID_USAC_SCE) {
    226     esbr_bit_str0->str_sbr_ele[esbr_bit_str0->no_elements].sbr_ele_id =
    227         SBR_ID_SCE;
    228     esbr_bit_str1->str_sbr_ele[esbr_bit_str1->no_elements].sbr_ele_id =
    229         SBR_ID_SCE;
    230   }
    231   if (ele_id == ID_USAC_CPE) {
    232     if ((st_config_idx == 0) || (st_config_idx == 3)) {
    233       esbr_bit_str0->str_sbr_ele[esbr_bit_str0->no_elements].sbr_ele_id =
    234           SBR_ID_CPE;
    235       esbr_bit_str1->str_sbr_ele[esbr_bit_str1->no_elements].sbr_ele_id =
    236           SBR_ID_CPE;
    237     } else {
    238       esbr_bit_str0->str_sbr_ele[esbr_bit_str0->no_elements].sbr_ele_id =
    239           SBR_ID_SCE;
    240       esbr_bit_str1->str_sbr_ele[esbr_bit_str1->no_elements].sbr_ele_id =
    241           SBR_ID_SCE;
    242     }
    243   }
    244   if (ele_id == ID_USAC_LFE) {
    245     esbr_bit_str0->str_sbr_ele[esbr_bit_str0->no_elements].sbr_ele_id =
    246         SBR_ID_SCE;
    247     esbr_bit_str1->str_sbr_ele[esbr_bit_str1->no_elements].sbr_ele_id =
    248         SBR_ID_SCE;
    249 
    250     esbr_bit_str0->no_elements++;
    251     esbr_bit_str0->str_sbr_ele[0].size_payload = 0;
    252   }
    253 }
    254 
    255 VOID ixheaacd_ms_processing(ia_usac_data_struct *pstr_usac_data) {
    256   WORD32 i;
    257 
    258   FLOAT32 tmp, tmp1;
    259   FLOAT32 ms_factor = (FLOAT32)0.7071067812;
    260   for (i = 0; i < pstr_usac_data->ccfl; i++) {
    261     tmp = (FLOAT32)((pstr_usac_data->time_sample_vector[0][i] +
    262                      pstr_usac_data->time_sample_vector[1][i]) *
    263                     ms_factor);
    264     tmp1 = (FLOAT32)((pstr_usac_data->time_sample_vector[0][i] -
    265                       pstr_usac_data->time_sample_vector[1][i]) *
    266                      ms_factor);
    267     pstr_usac_data->time_sample_vector[1][i] = tmp1;
    268     pstr_usac_data->time_sample_vector[0][i] = tmp;
    269   }
    270 }
    271 
    272 WORD32 ixheaacd_usac_process(ia_dec_data_struct *pstr_dec_data,
    273                              WORD32 *num_out_channels, VOID *codec_handle) {
    274   WORD32 ele_id = 0;
    275   WORD32 err_code = 0;
    276 
    277   ia_aac_dec_state_struct *p_state_aac_dec =
    278       (ia_aac_dec_state_struct *)codec_handle;
    279 
    280   ia_usac_data_struct *pstr_usac_data = &(pstr_dec_data->str_usac_data);
    281   ia_bit_buf_struct *it_bit_buff = &pstr_dec_data->dec_bit_buf;
    282 
    283   ia_frame_data_struct *fd = &(pstr_dec_data->str_frame_data);
    284 
    285   ia_usac_config_struct *pstr_usac_config =
    286       &(fd->str_audio_specific_config.str_usac_config);
    287   ia_usac_decoder_config_struct *pstr_usac_dec_config =
    288       &(fd->str_audio_specific_config.str_usac_config.str_usac_dec_config);
    289 
    290   WORD32 err = 0;
    291   WORD16 nr_core_coder_channels = 0;
    292   WORD32 ch_offset = 0;
    293 
    294   WORD32 elem_idx = 0;
    295   WORD32 num_ch_out = 0;
    296   WORD32 num_elements = pstr_usac_dec_config->num_elements;
    297 
    298   pstr_usac_dec_config->usac_ext_gain_payload_len = 0;
    299   pstr_usac_data->usac_independency_flg =
    300       ixheaacd_read_bits_buf(it_bit_buff, 1);
    301 
    302   for (elem_idx = 0; elem_idx < num_elements; elem_idx++) {
    303     WORD32 stereo_config_index =
    304         pstr_usac_config->str_usac_dec_config.str_usac_element_config[elem_idx]
    305             .stereo_config_index;
    306 
    307     pstr_usac_data->esbr_bit_str[0].no_elements = 0;
    308     pstr_usac_data->esbr_bit_str[1].no_elements = 0;
    309 
    310     switch (ele_id = pstr_usac_dec_config->usac_element_type[elem_idx]) {
    311       case ID_USAC_SCE:
    312         nr_core_coder_channels = 1;
    313         num_ch_out += 1;
    314         goto core_data_extracting;
    315 
    316       case ID_USAC_CPE:
    317         nr_core_coder_channels = (stereo_config_index == 1) ? 1 : 2;
    318         if (((stereo_config_index > 1) || (stereo_config_index == 0)) &&
    319             (p_state_aac_dec->num_of_output_ch < 2))
    320           return -1;
    321         num_ch_out += 2;
    322         goto core_data_extracting;
    323       case ID_USAC_LFE:
    324         nr_core_coder_channels = 1;
    325         num_ch_out += 1;
    326 
    327       core_data_extracting:
    328         if (ch_offset >= MAX_NUM_CHANNELS_USAC_LVL2) return -1;
    329         if (num_ch_out > MAX_NUM_CHANNELS_USAC_LVL2) return -1;
    330         err = ixheaacd_core_coder_data(ele_id, pstr_usac_data, elem_idx,
    331                                        ch_offset, it_bit_buff,
    332                                        nr_core_coder_channels);
    333         if (err != 0) return -1;
    334 
    335         ixheaacd_sbr_ele_type_set(&pstr_usac_data->esbr_bit_str[0],
    336                                   &pstr_usac_data->esbr_bit_str[1], ele_id,
    337                                   stereo_config_index);
    338 
    339         if (pstr_usac_data->mps_pseudo_lr[elem_idx])
    340           ixheaacd_ms_processing(pstr_usac_data);
    341 
    342         if (ele_id != ID_USAC_LFE) {
    343           if (pstr_usac_data->sbr_ratio_idx > 0)
    344             ixheaacd_sbr_ext_data_read(it_bit_buff,
    345                                        &pstr_usac_data->esbr_bit_str[0]);
    346         }
    347 
    348         if ((pstr_usac_data->sbr_ratio_idx > 0) &&
    349             (pstr_usac_data->esbr_bit_str[0].no_elements != 0)) {
    350           err_code = ixheaacd_esbr_process(
    351               pstr_usac_data, it_bit_buff, stereo_config_index,
    352               nr_core_coder_channels,
    353               pstr_dec_data->str_usac_data.audio_object_type);
    354           if (err_code < 0) return err_code;
    355         }
    356 
    357         if (stereo_config_index > 0) {
    358           FLOAT32 **ptr_inp[2 * 2];
    359           WORD32 ch;
    360 
    361           *num_out_channels = p_state_aac_dec->mps_dec_handle.out_ch_count;
    362 
    363           ixheaacd_mps_frame_parsing(&p_state_aac_dec->mps_dec_handle,
    364                                      pstr_usac_data->usac_independency_flg,
    365                                      it_bit_buff);
    366 
    367           for (ch = 0; ch < nr_core_coder_channels; ch++) {
    368             ptr_inp[2 * ch] =
    369                 pstr_usac_data->pstr_esbr_dec->pstr_sbr_channel[ch]
    370                     ->str_sbr_dec.pp_qmf_buf_real;
    371             ptr_inp[2 * ch + 1] =
    372                 pstr_usac_data->pstr_esbr_dec->pstr_sbr_channel[ch]
    373                     ->str_sbr_dec.pp_qmf_buf_imag;
    374             p_state_aac_dec->mps_dec_handle.p_sbr_dec[ch] =
    375                 (VOID *)(&pstr_usac_data->pstr_esbr_dec->pstr_sbr_channel[ch]
    376                               ->str_sbr_dec);
    377             p_state_aac_dec->mps_dec_handle.p_sbr_header[ch] =
    378                 (VOID *)(pstr_usac_data->pstr_esbr_dec->pstr_sbr_header[ch]);
    379             p_state_aac_dec->mps_dec_handle.p_sbr_frame[ch] =
    380                 (VOID *)(pstr_usac_data->pstr_esbr_dec->frame_buffer[ch]);
    381           }
    382           if (nr_core_coder_channels == 1) {
    383             if (p_state_aac_dec->mps_dec_handle.res_ch_count != 0) {
    384               ptr_inp[2] = pstr_usac_data->pstr_esbr_dec->pstr_sbr_channel[1]
    385                                ->str_sbr_dec.pp_qmf_buf_real;
    386               ptr_inp[2 + 1] =
    387                   pstr_usac_data->pstr_esbr_dec->pstr_sbr_channel[1]
    388                       ->str_sbr_dec.pp_qmf_buf_imag;
    389             }
    390             p_state_aac_dec->mps_dec_handle.p_sbr_dec[1] =
    391                 (VOID *)(&pstr_usac_data->pstr_esbr_dec->pstr_sbr_channel[1]
    392                               ->str_sbr_dec);
    393             p_state_aac_dec->mps_dec_handle.p_sbr_header[1] =
    394                 (VOID *)(pstr_usac_data->pstr_esbr_dec->pstr_sbr_header[1]);
    395             p_state_aac_dec->mps_dec_handle.p_sbr_frame[1] =
    396                 (VOID *)(pstr_usac_data->pstr_esbr_dec->frame_buffer[1]);
    397           }
    398 
    399           err = ixheaacd_mps_apply(&p_state_aac_dec->mps_dec_handle, ptr_inp,
    400                                    pstr_usac_data->time_sample_vector);
    401           if (err) return err;
    402         }
    403 
    404         ch_offset += nr_core_coder_channels;
    405         break;
    406 
    407       case ID_USAC_EXT: {
    408         ia_usac_dec_element_config_struct *pusac_element_config =
    409             &pstr_usac_dec_config->str_usac_element_config[elem_idx];
    410         err = ixheaacd_read_ext_element(
    411             pusac_element_config->usac_ext_eleme_def_len,
    412             pusac_element_config->usac_ext_elem_pld_frag, it_bit_buff,
    413             pstr_usac_dec_config, elem_idx);
    414         if (err != 0) return err;
    415 
    416         break;
    417       }
    418 
    419       default:
    420 
    421         return -1;
    422 
    423         break;
    424     }
    425   }
    426 
    427   return 0;
    428 }
    429