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 <stdlib.h>
     21 #include <string.h>
     22 #include <ixheaacd_type_def.h>
     23 #include "ixheaacd_error_standards.h"
     24 #include "ixheaacd_memory_standards.h"
     25 #include "ixheaacd_sbrdecsettings.h"
     26 #include "ixheaacd_env_extr_part.h"
     27 #include "ixheaacd_defines.h"
     28 #include <ixheaacd_aac_rom.h>
     29 #include "ixheaacd_common_rom.h"
     30 #include <ixheaacd_sbr_rom.h>
     31 #include "ixheaacd_bitbuffer.h"
     32 #include "ixheaacd_pulsedata.h"
     33 #include "ixheaacd_pns.h"
     34 
     35 #include "ixheaacd_lt_predict.h"
     36 
     37 #include "ixheaacd_channelinfo.h"
     38 #include "ixheaacd_sbr_common.h"
     39 #include "ixheaacd_drc_data_struct.h"
     40 #include "ixheaacd_drc_dec.h"
     41 #include "ixheaacd_channel.h"
     42 #include "ixheaacd_channelinfo.h"
     43 #include "ixheaacd_sbrdecoder.h"
     44 #include "ixheaacd_audioobjtypes.h"
     45 #include "ixheaacd_latmdemux.h"
     46 #include "ixheaacd_aacdec.h"
     47 #include "ixheaacd_sbr_common.h"
     48 
     49 #include "ixheaacd_mps_polyphase.h"
     50 #include "ixheaacd_config.h"
     51 #include "ixheaacd_mps_dec.h"
     52 
     53 #include "ixheaacd_struct_def.h"
     54 
     55 #include <ixheaacd_type_def.h>
     56 #include "ixheaacd_bitbuffer.h"
     57 #include "ixheaacd_interface.h"
     58 
     59 #include "ixheaacd_tns_usac.h"
     60 #include "ixheaacd_cnst.h"
     61 
     62 #include "ixheaacd_acelp_info.h"
     63 
     64 #include "ixheaacd_sbrdecsettings.h"
     65 #include "ixheaacd_info.h"
     66 #include "ixheaacd_sbrdecoder.h"
     67 #include "ixheaacd_mps_polyphase.h"
     68 #include "ixheaacd_sbr_const.h"
     69 #include "ixheaacd_main.h"
     70 
     71 #include "ixheaacd_arith_dec.h"
     72 
     73 #include "ixheaacd_config.h"
     74 #include "ixheaacd_struct.h"
     75 
     76 #include "ixheaacd_create.h"
     77 
     78 #include "ixheaacd_dec_main.h"
     79 
     80 VOID ixheaacd_samples_sat(WORD8 *outbuffer, WORD32 num_samples_out,
     81                           WORD32 pcmsize, FLOAT32 (*out_samples)[4096],
     82                           WORD32 *out_bytes, WORD32 num_channel_out) {
     83   WORD32 num;
     84   WORD32 i;
     85   WORD64 write_local;
     86 
     87   WORD16 *out_buf = (WORD16 *)outbuffer;
     88 
     89   num = num_channel_out * num_samples_out;
     90 
     91   if (pcmsize == 16) {
     92     for (i = 0; i < num; i++) {
     93       write_local =
     94           ((WORD64)(out_samples[i % num_channel_out][i / num_channel_out]));
     95 
     96       if (write_local > 32767) {
     97         write_local = 32767;
     98       }
     99       if (write_local < -32768) {
    100         write_local = -32768;
    101       }
    102       out_buf[i] = (WORD16)write_local;
    103     }
    104 
    105     *out_bytes = num * sizeof(WORD16);
    106   } else {
    107     WORD8 *out_24bit = (WORD8 *)out_buf;
    108     for (i = 0; i < num; i++) {
    109       write_local = ((WORD64)(
    110           out_samples[i % num_channel_out][i / num_channel_out] * 256));
    111 
    112       if (write_local > 8388607) {
    113         write_local = 8388607;
    114       }
    115       if (write_local < -8388608) {
    116         write_local = -8388608;
    117       }
    118       *out_24bit++ = (WORD32)write_local & 0xff;
    119       *out_24bit++ = ((WORD32)write_local >> 8) & 0xff;
    120       *out_24bit++ = ((WORD32)write_local >> 16) & 0xff;
    121     }
    122 
    123     *out_bytes = num * 3 * sizeof(WORD8);
    124   }
    125 }
    126 
    127 /* audio pre roll frame parsing*/
    128 static WORD32 ixheaacd_audio_preroll_parsing(ia_dec_data_struct *pstr_dec_data,
    129                                              UWORD8 *conf_buf,
    130                                              WORD32 *preroll_units,
    131                                              WORD32 *preroll_frame_offset) {
    132   ia_bit_buf_struct *temp_buff =
    133       (ia_bit_buf_struct *)&(pstr_dec_data->dec_bit_buf);
    134   WORD32 independency_flag = 0;
    135   WORD32 ext_ele_present = 0;
    136   WORD32 ext_ele_use_dflt_len = 0;
    137   WORD32 ext_ele_payload_len = 0;
    138 
    139   WORD32 apply_crossfade = 0;
    140   WORD32 un_used_val = 0;
    141   WORD32 num_pre_roll_frames = 0;
    142 
    143   WORD32 frame_idx = 0;
    144   WORD32 frame_len[18] = {
    145       0};  // max of escapedValue(2, 4, 0) i.e. 2^2 -1 + 2^4 -1;
    146   WORD32 temp = 0;
    147 
    148   WORD32 config_len = 0;
    149   WORD32 loop;
    150 
    151   if (pstr_dec_data->str_frame_data.str_audio_specific_config.str_usac_config
    152           .str_usac_dec_config.usac_element_type[0] == ID_USAC_EXT) {
    153     temp = ixheaacd_show_bits_buf(temp_buff, 3);
    154     independency_flag = (temp >> 2) & 0x1;
    155     ext_ele_present = (temp >> 1) & 0x1;
    156 
    157     if (ext_ele_present) {
    158       ext_ele_use_dflt_len = temp & 0x1;  // ixheaacd_read_bit(&temp_buff, 1);
    159       if (ext_ele_use_dflt_len != 0) return 0;
    160 
    161       un_used_val = ixheaacd_read_bits_buf(temp_buff, 3);
    162 
    163       ext_ele_payload_len = ixheaacd_read_bits_buf(temp_buff, 8);
    164 
    165       if (ext_ele_payload_len == 255) {
    166         WORD32 val_add = 0;
    167         val_add = ixheaacd_read_bits_buf(temp_buff, 16);
    168         ext_ele_payload_len =
    169             (UWORD32)((WORD32)ext_ele_payload_len + val_add - 2);
    170       }
    171 
    172       // escapedValue(4, 4, 8);
    173       config_len = ixheaacd_read_bits_buf(temp_buff, 4);
    174       if (config_len == 15) {
    175         WORD32 val_add = 0;
    176         val_add = ixheaacd_read_bits_buf(temp_buff, 4);
    177         config_len += val_add;
    178         if (val_add == 15) {
    179           WORD32 val_add1 = 0;
    180           val_add1 = ixheaacd_read_bits_buf(temp_buff, 8);
    181           config_len += val_add1;
    182         }
    183       }
    184 
    185       for (loop = 0; loop < config_len; loop++)
    186         conf_buf[loop] = ixheaacd_read_bits_buf(temp_buff, 8);
    187 
    188       apply_crossfade = ixheaacd_read_bits_buf(temp_buff, 1);
    189       un_used_val = ixheaacd_read_bits_buf(temp_buff, 1);  // reserverd
    190 
    191       // escapedValue(2, 4, 0);
    192       num_pre_roll_frames = ixheaacd_read_bits_buf(temp_buff, 2);
    193       if (num_pre_roll_frames == 3) {
    194         WORD32 val_add = 0;
    195         val_add = ixheaacd_read_bits_buf(temp_buff, 4);
    196         num_pre_roll_frames += val_add;
    197       }
    198 
    199       for (frame_idx = 0; frame_idx < num_pre_roll_frames; frame_idx++) {
    200         WORD32 au_len = 0;  // escapedValued(16,16,0)
    201         au_len = ixheaacd_read_bits_buf(temp_buff, 16);
    202         if (au_len == 65535) {
    203           WORD32 val_add = ixheaacd_read_bits_buf(temp_buff, 16);
    204           au_len += val_add;
    205         }
    206         preroll_frame_offset[frame_idx] = temp_buff->size - temp_buff->cnt_bits;
    207         frame_len[frame_idx] =
    208             (8 * au_len) + (temp_buff->size - temp_buff->cnt_bits);
    209         temp_buff->ptr_read_next += au_len;
    210         temp_buff->cnt_bits -= au_len * 8;
    211       }
    212     }
    213   }
    214   *preroll_units = num_pre_roll_frames;
    215   return config_len;
    216 }
    217 
    218 WORD32 ixheaacd_dec_main(VOID *temp_handle, WORD8 *inbuffer, WORD8 *outbuffer,
    219                          WORD32 *out_bytes, WORD32 frames_done, WORD32 pcmsize,
    220                          WORD32 *num_channel_out) {
    221   WORD32 err = 0;
    222   ia_exhaacplus_dec_api_struct *handle =
    223       (ia_exhaacplus_dec_api_struct *)temp_handle;
    224   ia_aac_dec_state_struct *aac_dec_handle = handle->p_state_aac;
    225 
    226   WORD32 tmp;
    227   ia_audio_specific_config_struct *pstr_audio_specific_config =
    228       (ia_audio_specific_config_struct *)
    229           aac_dec_handle->ia_audio_specific_config;
    230   WORD32 suitable_tracks = 1;
    231   WORD32 num_samples_out;
    232   ia_dec_data_struct *pstr_dec_data;
    233   UWORD8 config[285];  // max of escapedValue(4, 4, 8) i.e. 2^4 -1 + 2^4 -1 +
    234                        // 2^8 -1;
    235   WORD32 config_len;
    236   WORD32 delay;
    237   WORD preroll_frame_offset[4] = {0};
    238   WORD preroll_units = -1;
    239   WORD32 access_units = 0;
    240 
    241   if (frames_done == 0) {
    242     if ((pstr_audio_specific_config->channel_configuration > 2) ||
    243         (pstr_audio_specific_config->channel_configuration == 0)) {
    244       return -1;
    245     }
    246 
    247     pstr_dec_data = (ia_dec_data_struct *)aac_dec_handle->pstr_dec_data;
    248 
    249     tmp = pstr_audio_specific_config->channel_configuration;
    250 
    251     suitable_tracks =
    252         ixheaacd_frm_data_init(pstr_audio_specific_config, pstr_dec_data);
    253 
    254     pstr_audio_specific_config->channel_configuration = tmp;
    255 
    256     if (suitable_tracks <= 0) {
    257       return -1;
    258     }
    259   }
    260 
    261   {
    262     pstr_dec_data = (ia_dec_data_struct *)aac_dec_handle->pstr_dec_data;
    263 
    264     if (frames_done == 0) {
    265       WORD32 delay;
    266       if (aac_dec_handle->decode_create_done == 0) {
    267         delay = ixheaacd_decode_create(
    268             handle, pstr_dec_data,
    269             pstr_dec_data->str_frame_data.scal_out_select + 1);
    270         if (delay == -1) return -1;
    271       }
    272       pstr_dec_data->dec_bit_buf.max_size =
    273           handle->p_mem_info_aac[IA_MEMTYPE_INPUT].ui_size;
    274       *num_channel_out = pstr_dec_data->str_frame_data.scal_out_num_channels;
    275       return 0;
    276     }
    277 
    278     pstr_dec_data->dec_bit_buf.ptr_bit_buf_base = (UWORD8 *)inbuffer;
    279     pstr_dec_data->dec_bit_buf.size = aac_dec_handle->ui_in_bytes << 3;
    280     pstr_dec_data->dec_bit_buf.ptr_bit_buf_end =
    281         (UWORD8 *)inbuffer + aac_dec_handle->ui_in_bytes - 1;
    282     pstr_dec_data->dec_bit_buf.ptr_read_next = (UWORD8 *)inbuffer;
    283     pstr_dec_data->dec_bit_buf.bit_pos = 7;
    284     pstr_dec_data->dec_bit_buf.cnt_bits = pstr_dec_data->dec_bit_buf.size;
    285     pstr_dec_data->dec_bit_buf.xaac_jmp_buf = &(aac_dec_handle->xaac_jmp_buf);
    286 
    287     pstr_dec_data->str_usac_data.usac_flag = aac_dec_handle->usac_flag;
    288     if (pstr_dec_data->dec_bit_buf.size > pstr_dec_data->dec_bit_buf.max_size)
    289       pstr_dec_data->dec_bit_buf.max_size = pstr_dec_data->dec_bit_buf.size;
    290     /* audio pre roll frame parsing*/
    291 
    292     do {
    293       config_len = 0;
    294       if (access_units == 0 &&
    295           pstr_audio_specific_config->str_usac_config.str_usac_dec_config
    296               .preroll_flag) {
    297         config_len = ixheaacd_audio_preroll_parsing(pstr_dec_data, &config[0],
    298                                                     &preroll_units,
    299                                                     &preroll_frame_offset[0]);
    300       }
    301 
    302       if (config_len != 0) {
    303         /* updating the config parameters*/
    304         ia_bit_buf_struct config_bit_buf = {0};
    305 
    306         config_bit_buf.ptr_bit_buf_base = config;
    307         config_bit_buf.size = config_len << 3;
    308         config_bit_buf.ptr_read_next = config_bit_buf.ptr_bit_buf_base;
    309         config_bit_buf.ptr_bit_buf_end = (UWORD8 *)config + config_len;
    310         config_bit_buf.bit_pos = 7;
    311         config_bit_buf.cnt_bits = config_bit_buf.size;
    312         config_bit_buf.xaac_jmp_buf = &(aac_dec_handle->xaac_jmp_buf);
    313 
    314         suitable_tracks =
    315             ixheaacd_frm_data_init(pstr_audio_specific_config, pstr_dec_data);
    316 
    317         if (suitable_tracks <= 0) return -1;
    318 
    319         /* call codec re-configure*/
    320         aac_dec_handle->decode_create_done = 0;
    321         err = ixheaacd_config(
    322             &config_bit_buf, &(pstr_dec_data->str_frame_data
    323                                    .str_audio_specific_config.str_usac_config),
    324             &(pstr_audio_specific_config
    325                   ->channel_configuration) /*&pstr_audio_specific_config->str_usac_config*/);
    326         if (err != 0) return -1;
    327 
    328         pstr_dec_data->str_frame_data.str_audio_specific_config
    329             .sampling_frequency =
    330             pstr_dec_data->str_frame_data.str_audio_specific_config
    331                 .str_usac_config.usac_sampling_frequency;
    332         delay = ixheaacd_decode_create(
    333             handle, pstr_dec_data,
    334             pstr_dec_data->str_frame_data.scal_out_select + 1);
    335         if (delay == -1) return -1;
    336         *num_channel_out = pstr_dec_data->str_frame_data.scal_out_num_channels;
    337       }
    338 
    339       pstr_dec_data->dec_bit_buf.ptr_bit_buf_base = (UWORD8 *)inbuffer;
    340       pstr_dec_data->dec_bit_buf.size = aac_dec_handle->ui_in_bytes << 3;
    341       pstr_dec_data->dec_bit_buf.ptr_bit_buf_end =
    342           (UWORD8 *)inbuffer + aac_dec_handle->ui_in_bytes - 1;
    343       pstr_dec_data->dec_bit_buf.ptr_read_next = (UWORD8 *)inbuffer;
    344       pstr_dec_data->dec_bit_buf.bit_pos = 7;
    345       pstr_dec_data->dec_bit_buf.cnt_bits = pstr_dec_data->dec_bit_buf.size;
    346       pstr_dec_data->dec_bit_buf.xaac_jmp_buf = &(aac_dec_handle->xaac_jmp_buf);
    347 
    348       pstr_dec_data->str_usac_data.usac_flag = aac_dec_handle->usac_flag;
    349 
    350       if (preroll_frame_offset[access_units]) {
    351         pstr_dec_data->dec_bit_buf.cnt_bits =
    352             pstr_dec_data->dec_bit_buf.size -
    353             preroll_frame_offset[access_units];
    354         pstr_dec_data->dec_bit_buf.bit_pos =
    355             7 - preroll_frame_offset[access_units] % 8;
    356         pstr_dec_data->dec_bit_buf.ptr_read_next =
    357             pstr_dec_data->dec_bit_buf.ptr_read_next +
    358             (preroll_frame_offset[access_units] / 8);
    359       }
    360 
    361       // temp_read=ixheaacd_show_bits_buf(pstr_dec_data->dec_bit_buf,preroll_frame_offset[access_unit]);
    362       if (!aac_dec_handle->decode_create_done) return IA_FATAL_ERROR;
    363 
    364       err =
    365           ixheaacd_usac_process(pstr_dec_data, num_channel_out, aac_dec_handle);
    366 
    367       switch (pstr_dec_data->str_usac_data.sbr_ratio_idx) {
    368         case 0:
    369           handle->aac_config.ui_sbr_mode = 0;
    370           break;
    371         case 1:
    372           handle->aac_config.ui_sbr_mode = 1;
    373           break;
    374         case 2:
    375           handle->aac_config.ui_sbr_mode = 1;
    376           break;
    377         case 3:
    378           handle->aac_config.ui_sbr_mode = 3;
    379           break;
    380 
    381         default:
    382           handle->aac_config.ui_sbr_mode = 0;
    383       }
    384 
    385       if (err == -1) return err;
    386 
    387       num_samples_out = pstr_dec_data->str_usac_data.output_samples;
    388 
    389       ixheaacd_samples_sat(outbuffer, num_samples_out, pcmsize,
    390                            pstr_dec_data->str_usac_data.time_sample_vector,
    391                            out_bytes, *num_channel_out);
    392       pstr_audio_specific_config->str_usac_config.str_usac_dec_config
    393           .usac_ext_gain_payload_len =
    394           pstr_dec_data->str_frame_data.str_audio_specific_config
    395               .str_usac_config.str_usac_dec_config.usac_ext_gain_payload_len;
    396       memcpy(pstr_audio_specific_config->str_usac_config.str_usac_dec_config
    397                  .usac_ext_gain_payload_buf,
    398              pstr_dec_data->str_frame_data.str_audio_specific_config
    399                  .str_usac_config.str_usac_dec_config.usac_ext_gain_payload_buf,
    400              pstr_dec_data->str_frame_data.str_audio_specific_config
    401                      .str_usac_config.str_usac_dec_config
    402                      .usac_ext_gain_payload_len *
    403                  sizeof(WORD8));
    404 
    405       access_units++;
    406       preroll_units--;
    407     } while (preroll_units >= 0);
    408   }
    409 
    410   return err;
    411 }
    412