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 <stdlib.h>
     22 #include <string.h>
     23 #include <assert.h>
     24 
     25 #include <ixheaacd_type_def.h>
     26 
     27 #include "ixheaacd_cnst.h"
     28 
     29 #include "ixheaacd_bitbuffer.h"
     30 #include "ixheaacd_config.h"
     31 #include "ixheaacd_interface.h"
     32 #include "ixheaacd_acelp_info.h"
     33 
     34 #include "ixheaacd_tns_usac.h"
     35 #include "ixheaacd_sbrdecsettings.h"
     36 #include "ixheaacd_info.h"
     37 #include "ixheaacd_struct.h"
     38 #include "ixheaacd_sbr_common.h"
     39 #include "ixheaacd_drc_data_struct.h"
     40 #include "ixheaacd_drc_dec.h"
     41 
     42 #include "ixheaacd_sbrdecoder.h"
     43 #include "ixheaacd_mps_polyphase.h"
     44 #include "ixheaacd_sbr_const.h"
     45 
     46 #include "ixheaacd_main.h"
     47 
     48 #include "ixheaacd_arith_dec.h"
     49 
     50 #include <ixheaacd_type_def.h>
     51 #include "ixheaacd_memory_standards.h"
     52 #include "ixheaacd_sbrdecsettings.h"
     53 #include "ixheaacd_env_extr_part.h"
     54 #include "ixheaacd_defines.h"
     55 #include <ixheaacd_aac_rom.h>
     56 #include "ixheaacd_common_rom.h"
     57 #include <ixheaacd_sbr_rom.h>
     58 #include "ixheaacd_bitbuffer.h"
     59 #include "ixheaacd_pulsedata.h"
     60 #include "ixheaacd_pns.h"
     61 
     62 #include "ixheaacd_lt_predict.h"
     63 
     64 #include "ixheaacd_channelinfo.h"
     65 #include "ixheaacd_channel.h"
     66 #include "ixheaacd_channelinfo.h"
     67 #include "ixheaacd_sbrdecoder.h"
     68 #include "ixheaacd_audioobjtypes.h"
     69 #include "ixheaacd_latmdemux.h"
     70 #include "ixheaacd_aacdec.h"
     71 #include "ixheaacd_sbr_common.h"
     72 
     73 #include "ixheaacd_mps_polyphase.h"
     74 #include "ixheaacd_config.h"
     75 #include "ixheaacd_mps_dec.h"
     76 #include "ixheaacd_struct_def.h"
     77 
     78 #include "ixheaacd_create.h"
     79 
     80 #include "ixheaacd_process.h"
     81 
     82 #include "ixheaacd_sbrdecoder.h"
     83 
     84 #include "ixheaacd_mps_interface.h"
     85 
     86 #include "ixheaacd_bit_extract.h"
     87 #include "ixheaacd_func_def.h"
     88 #include "ixheaacd_interface.h"
     89 
     90 extern ia_huff_code_word_struct ixheaacd_huff_book_scl[];
     91 
     92 extern WORD32 ixheaacd_book_scl_index[];
     93 extern WORD16 ixheaacd_book_scl_code_book[];
     94 
     95 extern ia_usac_samp_rate_info ixheaacd_samp_rate_info[];
     96 extern const WORD32 ixheaacd_sampling_boundaries[(1 << LEN_SAMP_IDX)];
     97 
     98 const WORD32 ixheaacd_sampl_freq_idx_table[17] = {
     99     96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000,
    100     12000, 11025, 8000,  7350,  -1,    -1,    -1,    -1};
    101 
    102 static VOID ixheaacd_info_init(ia_usac_samp_rate_info *ptr_samp_info,
    103                                WORD32 block_size_samples,
    104                                ia_sfb_info_struct *pstr_sfb_info_long,
    105                                ia_sfb_info_struct *pstr_sfb_info_short,
    106                                WORD16 *sfb_width_short,
    107                                WORD16 *sfb_width_long) {
    108   WORD32 i, j, k, n, ws;
    109   const WORD16 *sfbands;
    110   ia_sfb_info_struct *pstr_sfb_info_ip;
    111 
    112   pstr_sfb_info_long->islong = 1;
    113   pstr_sfb_info_long->max_win_len = 1;
    114   pstr_sfb_info_long->samp_per_bk = block_size_samples;
    115 
    116   switch (block_size_samples) {
    117     case 480:
    118       pstr_sfb_info_long->ptr_sfb_tbl = ptr_samp_info->ptr_sfb_480;
    119       pstr_sfb_info_long->sfb_per_sbk = ptr_samp_info->num_sfb_480;
    120       break;
    121     case 512:
    122       pstr_sfb_info_long->ptr_sfb_tbl = ptr_samp_info->ptr_sfb_512;
    123       pstr_sfb_info_long->sfb_per_sbk = ptr_samp_info->num_sfb_512;
    124       break;
    125     case 768:
    126       pstr_sfb_info_long->sfb_per_sbk = ptr_samp_info->num_sfb_768;
    127       pstr_sfb_info_long->ptr_sfb_tbl = ptr_samp_info->ptr_sfb_768;
    128       break;
    129     case 960:
    130       pstr_sfb_info_long->ptr_sfb_tbl = ptr_samp_info->ptr_sfb_960;
    131       pstr_sfb_info_long->sfb_per_sbk = ptr_samp_info->num_sfb_960;
    132       break;
    133     case 1024:
    134       pstr_sfb_info_long->sfb_per_sbk = ptr_samp_info->num_sfb_1024;
    135       pstr_sfb_info_long->ptr_sfb_tbl = ptr_samp_info->ptr_sfb_1024;
    136       break;
    137     default:
    138       assert(0);
    139       break;
    140   }
    141 
    142   pstr_sfb_info_long->sfb_width = sfb_width_long;
    143   pstr_sfb_info_long->num_groups = 1;
    144   pstr_sfb_info_long->group_len[0] = 1;
    145 
    146   for (i = 0, j = 0, n = pstr_sfb_info_long->sfb_per_sbk; i < n; i++) {
    147     k = pstr_sfb_info_long->ptr_sfb_tbl[i];
    148     pstr_sfb_info_long->sfb_width[i] = k - j;
    149     j = k;
    150   }
    151 
    152   pstr_sfb_info_short->islong = 0;
    153   pstr_sfb_info_short->max_win_len = NSHORT;
    154   pstr_sfb_info_short->samp_per_bk = block_size_samples;
    155 
    156   for (i = 0; i < pstr_sfb_info_short->max_win_len; i++) {
    157     switch (block_size_samples) {
    158       case 768:
    159         pstr_sfb_info_short->ptr_sfb_tbl = ptr_samp_info->ptr_sfb_96;
    160         pstr_sfb_info_short->sfb_per_sbk = ptr_samp_info->num_sfb_96;
    161         break;
    162       case 960:
    163         pstr_sfb_info_short->ptr_sfb_tbl = ptr_samp_info->ptr_sfb_120;
    164         pstr_sfb_info_short->sfb_per_sbk = ptr_samp_info->num_sfb_120;
    165         break;
    166       case 1024:
    167         pstr_sfb_info_short->ptr_sfb_tbl = ptr_samp_info->ptr_sfb_128;
    168         pstr_sfb_info_short->sfb_per_sbk = ptr_samp_info->num_sfb_128;
    169         break;
    170       default:
    171         assert(0);
    172         break;
    173     }
    174   }
    175 
    176   pstr_sfb_info_short->sfb_width = sfb_width_short;
    177   for (i = 0, j = 0, n = pstr_sfb_info_short->sfb_per_sbk; i < n; i++) {
    178     k = pstr_sfb_info_short->ptr_sfb_tbl[i];
    179     pstr_sfb_info_short->sfb_width[i] = k - j;
    180     j = k;
    181   }
    182 
    183   pstr_sfb_info_ip = pstr_sfb_info_long;
    184   for (ws = 0; ws < 2; ws++) {
    185     pstr_sfb_info_ip->sfb_per_bk = 0;
    186     k = 0;
    187     n = 0;
    188     for (i = 0; i < pstr_sfb_info_ip->max_win_len; i++) {
    189       pstr_sfb_info_ip->bins_per_sbk =
    190           pstr_sfb_info_ip->samp_per_bk / pstr_sfb_info_ip->max_win_len;
    191 
    192       pstr_sfb_info_ip->sfb_per_bk += pstr_sfb_info_ip->sfb_per_sbk;
    193 
    194       sfbands = pstr_sfb_info_ip->ptr_sfb_tbl;
    195       for (j = 0; j < pstr_sfb_info_ip->sfb_per_sbk; j++)
    196         pstr_sfb_info_ip->sfb_idx_tbl[j + k] = sfbands[j] + n;
    197 
    198       n += pstr_sfb_info_ip->bins_per_sbk;
    199       k += pstr_sfb_info_ip->sfb_per_sbk;
    200     }
    201     pstr_sfb_info_ip = pstr_sfb_info_short;
    202   }
    203 }
    204 
    205 WORD32 ixheaacd_decode_init(
    206     VOID *handle, WORD32 sample_rate, ia_usac_data_struct *usac_data,
    207     ia_audio_specific_config_struct *pstr_stream_config) {
    208   WORD32 i;
    209   ia_exhaacplus_dec_api_struct *codec_handle =
    210       (ia_exhaacplus_dec_api_struct *)handle;
    211   ia_aac_dec_state_struct *aac_dec_handle = codec_handle->p_state_aac;
    212   WORD32 fscale;
    213 
    214   WORD32 ele_id = 0;
    215 
    216   ia_usac_config_struct *ptr_usac_config =
    217       &(pstr_stream_config->str_usac_config);
    218   ia_usac_decoder_config_struct *ptr_usac_dec_config =
    219       &(pstr_stream_config->str_usac_config.str_usac_dec_config);
    220   WORD32 num_elements = ptr_usac_dec_config->num_elements;
    221   WORD32 chan = 0;
    222 
    223   usac_data->huffman_code_book_scl = aac_dec_handle->huffman_code_book_scl;
    224   usac_data->huffman_code_book_scl_index =
    225       aac_dec_handle->huffman_code_book_scl_index;
    226 
    227   usac_data->tns_coeff3_32 =
    228       aac_dec_handle->pstr_aac_tables->pstr_block_tables->tns_coeff3_32;
    229   usac_data->tns_coeff4_32 =
    230       aac_dec_handle->pstr_aac_tables->pstr_block_tables->tns_coeff4_32;
    231   usac_data->tns_max_bands_tbl_usac =
    232       &aac_dec_handle->pstr_aac_tables->pstr_block_tables
    233            ->tns_max_bands_tbl_usac;
    234 
    235   for (i = 0; i < 11; i++) {
    236     if (ixheaacd_sampling_boundaries[i] <= sample_rate) break;
    237   }
    238 
    239   if (i == (1 << LEN_SAMP_IDX)) return -1;
    240   usac_data->sampling_rate_idx = i;
    241 
    242   fscale = (WORD32)((double)sample_rate * (double)FSCALE_DENOM / 12800.0f);
    243 
    244   for (i = 0; i < MAX_NUM_CHANNELS; i++) {
    245     usac_data->window_shape_prev[i] = 0;
    246     usac_data->window_shape[i] = 0;
    247   }
    248 
    249   ixheaacd_hufftab(&ixheaacd_book, ixheaacd_huff_book_scl,
    250                    ixheaacd_book_scl_code_book, ixheaacd_book_scl_index, 1, 60,
    251                    60, 1, 19);
    252 
    253   usac_data->pstr_usac_winmap[0] = &usac_data->str_only_long_info;
    254   usac_data->pstr_usac_winmap[1] = &usac_data->str_only_long_info;
    255   usac_data->pstr_usac_winmap[2] = &usac_data->str_eight_short_info;
    256   usac_data->pstr_usac_winmap[3] = &usac_data->str_only_long_info;
    257   usac_data->pstr_usac_winmap[4] = &usac_data->str_only_long_info;
    258 
    259   if ((usac_data->ccfl != 480) && (usac_data->ccfl != 512) &&
    260       (usac_data->ccfl != 768) && (usac_data->ccfl != 960) &&
    261       (usac_data->ccfl != 1024))
    262     return -1;
    263   ixheaacd_info_init(&ixheaacd_samp_rate_info[usac_data->sampling_rate_idx],
    264                      usac_data->ccfl, usac_data->pstr_usac_winmap[0],
    265                      usac_data->pstr_usac_winmap[2], usac_data->sfb_width_short,
    266                      usac_data->sfb_width_long);
    267 
    268   for (i = 0; i < MAX_NUM_CHANNELS; i++) {
    269     usac_data->str_tddec[i] = &usac_data->arr_str_tddec[i];
    270     usac_data->str_tddec[i]->fscale =
    271         ((fscale)*usac_data->ccfl) / LEN_SUPERFRAME;
    272     usac_data->len_subfrm = usac_data->ccfl / 4;
    273     usac_data->num_subfrm = (MAX_NUM_SUBFR * usac_data->ccfl) / LEN_SUPERFRAME;
    274 
    275     ixheaacd_init_acelp_data(usac_data, usac_data->str_tddec[i]);
    276 
    277     usac_data->str_tddec[i]->fd_synth =
    278         &usac_data->str_tddec[i]->fd_synth_buf[LEN_FRAME];
    279   }
    280 
    281   for (ele_id = 0; ele_id < num_elements; ele_id++) {
    282     UWORD32 ele_type;
    283     WORD32 stereo_config_index;
    284 
    285     ia_usac_dec_element_config_struct *ptr_usac_ele_config =
    286         &ptr_usac_config->str_usac_dec_config.str_usac_element_config[ele_id];
    287 
    288     if (ptr_usac_ele_config) {
    289       if (usac_data->tw_mdct[ele_id]) {
    290         return -1;
    291       }
    292 
    293       usac_data->noise_filling_config[ele_id] =
    294           ptr_usac_ele_config->noise_filling;
    295     }
    296 
    297     ele_type = ptr_usac_config->str_usac_dec_config.usac_element_type[ele_id];
    298 
    299     stereo_config_index = ptr_usac_ele_config->stereo_config_index;
    300 
    301     switch (ele_type) {
    302       case ID_USAC_SCE:
    303       case ID_USAC_LFE:
    304 
    305         usac_data->seed_value[chan] = 0x3039;
    306 
    307         break;
    308 
    309       case ID_USAC_CPE: {
    310         WORD32 frame_len_tbl[] = {-1, -1, 32, 32, 64};
    311 
    312         usac_data->seed_value[chan] = 0x3039;
    313         chan++;
    314 
    315         usac_data->seed_value[chan] = 0x10932;
    316 
    317         if (stereo_config_index > 0) {
    318           WORD32 bs_frame_length =
    319               frame_len_tbl[ptr_usac_config->core_sbr_framelength_index] - 1;
    320           WORD32 bs_residual_coding = (stereo_config_index > 1) ? 1 : 0;
    321 
    322           ia_usac_dec_mps_config_struct *ptr_usac_mps212_config =
    323               &(ptr_usac_config->str_usac_dec_config
    324                     .str_usac_element_config[ele_id]
    325                     .str_usac_mps212_config);
    326 
    327           ixheaacd_mps_create(&aac_dec_handle->mps_dec_handle, bs_frame_length,
    328                               bs_residual_coding, ptr_usac_mps212_config);
    329         }
    330         break;
    331       }
    332 
    333       break;
    334       case ID_USAC_EXT:
    335         break;
    336       default:
    337         return -1;
    338         break;
    339     }
    340   }
    341 
    342   return 0;
    343 }
    344 
    345 WORD32 ixheaacd_dec_data_init(VOID *handle,
    346                               ia_frame_data_struct *pstr_frame_data,
    347                               ia_usac_data_struct *usac_data) {
    348   ia_audio_specific_config_struct *pstr_stream_config, *layer_config;
    349   WORD32 err_code = 0;
    350 
    351   WORD32 num_out_chan = 0;
    352 
    353   WORD32 i_ch, i, ele_id;
    354   WORD32 num_elements;
    355 
    356   WORD32 out_frame_len, sbr_ratio_idx;
    357 
    358   ia_usac_config_struct *ptr_usac_config =
    359       &(pstr_frame_data->str_audio_specific_config.str_usac_config);
    360 
    361   usac_data->window_shape_prev[0] = WIN_SEL_0;
    362   usac_data->window_shape_prev[1] = WIN_SEL_0;
    363 
    364   pstr_frame_data->str_layer.bit_rate =
    365       pstr_frame_data->str_audio_specific_config.avg_bit_rate;
    366   pstr_stream_config = &pstr_frame_data->str_audio_specific_config;
    367   layer_config = &pstr_frame_data->str_audio_specific_config;
    368 
    369   sbr_ratio_idx = ixheaacd_sbr_params(
    370       ptr_usac_config->core_sbr_framelength_index, &out_frame_len,
    371       &usac_data->ccfl, &usac_data->output_samples,
    372       &pstr_frame_data->str_layer.sample_rate_layer,
    373       &layer_config->samp_frequency_index);
    374 
    375   pstr_stream_config->sampling_frequency =
    376       pstr_frame_data->str_layer.sample_rate_layer;
    377   pstr_stream_config->samp_frequency_index = layer_config->samp_frequency_index;
    378 
    379   num_elements = ptr_usac_config->str_usac_dec_config.num_elements;
    380 
    381   for (ele_id = 0; ele_id < num_elements; ele_id++) {
    382     ia_usac_dec_element_config_struct *ptr_usac_ele_config =
    383         &(ptr_usac_config->str_usac_dec_config.str_usac_element_config[ele_id]);
    384 
    385     if (ptr_usac_ele_config) {
    386       usac_data->tw_mdct[ele_id] = ptr_usac_ele_config->tw_mdct;
    387     }
    388 
    389     {
    390       ia_usac_dec_mps_config_struct *ptr_usac_mps212_config =
    391           &ptr_usac_ele_config->str_usac_mps212_config;
    392       WORD32 stereo_config_index = ptr_usac_ele_config->stereo_config_index;
    393 
    394       usac_data->mps_pseudo_lr[ele_id] =
    395           (stereo_config_index > 1) ? ptr_usac_mps212_config->bs_pseudo_lr : 0;
    396     }
    397   }
    398 
    399   usac_data->sbr_ratio_idx = sbr_ratio_idx;
    400   usac_data->esbr_bit_str[0].no_elements = 0;
    401   usac_data->esbr_bit_str[1].no_elements = 0;
    402 
    403   num_out_chan = ptr_usac_config->num_out_channels;
    404 
    405   if (usac_data->ccfl == 768)
    406     pstr_frame_data->str_layer.sample_rate_layer =
    407         4 * pstr_frame_data->str_layer.sample_rate_layer / 3;
    408 
    409   for (i = 0; i < MAX_NUM_CHANNELS; i++) {
    410     usac_data->coef_fix[i] = &usac_data->arr_coef_fix[i][0];
    411     usac_data->coef[i] = &usac_data->arr_coef[i][0];
    412     usac_data->coef_save[i] = &usac_data->arr_coef_save[i][0];
    413     usac_data->factors[i] = &usac_data->arr_factors[i][0];
    414     usac_data->group_dis[i] = &usac_data->arr_group_dis[i][0];
    415     usac_data->pstr_tns[i] = &usac_data->arr_str_tns[i];
    416     usac_data->tw_ratio[i] = &usac_data->arr_tw_ratio[i][0];
    417     usac_data->ms_used[i] = &usac_data->arr_ms_used[i][0];
    418     usac_data->window_shape_prev[i] = WIN_SEL_0;
    419 
    420     usac_data->seed_value[i] = 0x0;
    421 
    422     usac_data->fac_data_present[i] = 0;
    423   }
    424 
    425   err_code =
    426       ixheaacd_decode_init(handle, pstr_frame_data->str_layer.sample_rate_layer,
    427                            usac_data, pstr_stream_config);
    428   if (err_code == -1) return -1;
    429 
    430   for (i_ch = 0; i_ch < MAX_NUM_CHANNELS; i_ch++) {
    431     if (usac_data->tw_mdct[0] == 1) {
    432       WORD32 i;
    433       for (i = 0; i < 2 * usac_data->ccfl; i++) {
    434         usac_data->warp_cont_mem[i_ch][i] = 1.0;
    435       }
    436       usac_data->warp_sum[i_ch][0] = usac_data->warp_sum[i_ch][1] =
    437           (FLOAT32)usac_data->ccfl;
    438     }
    439   }
    440   return err_code;
    441 }
    442 
    443 static VOID ixheaacd_count_tracks_per_layer(int *max_layer, int *stream_count,
    444                                             int *tracks_in_layer) {
    445   WORD32 stream;
    446   WORD32 num_layer;
    447   WORD32 num_streams;
    448   WORD32 layer = 0;
    449 
    450   if (stream_count == NULL)
    451     num_streams = 0;
    452   else
    453     num_streams = *stream_count;
    454   if (max_layer == NULL)
    455     num_layer = num_streams;
    456   else
    457     num_layer = *max_layer;
    458   if (num_layer < 0) num_layer = num_streams;
    459 
    460   for (stream = 0; (layer <= num_layer) && (stream < num_streams);) {
    461     *tracks_in_layer = 1;
    462     stream += 1;
    463     layer++;
    464     if (layer <= num_layer) *tracks_in_layer = 0;
    465   }
    466 
    467   if (max_layer) *max_layer = (layer - 1);
    468   if (stream_count) *stream_count = stream;
    469 }
    470 
    471 WORD32 ixheaacd_frm_data_init(ia_audio_specific_config_struct *pstr_audio_conf,
    472                               ia_dec_data_struct *pstr_dec_data)
    473 
    474 {
    475   WORD32 layer;
    476   WORD32 track;
    477   WORD32 num_dec_streams;
    478   ia_frame_data_struct *pstr_frame_data;
    479 
    480   WORD32 stream_count = 1;
    481   WORD32 max_layer = -1;
    482 
    483   memset(pstr_dec_data, 0, sizeof(ia_dec_data_struct));
    484   memset(&(pstr_dec_data->str_frame_data), 0,
    485          sizeof(pstr_dec_data->str_frame_data));
    486 
    487   pstr_frame_data = &(pstr_dec_data->str_frame_data);
    488 
    489   if (max_layer < 0) max_layer = stream_count - 1;
    490 
    491   ixheaacd_count_tracks_per_layer(&max_layer, &stream_count,
    492                                   &pstr_frame_data->tracks_in_layer);
    493 
    494   pstr_frame_data->scal_out_select = max_layer;
    495 
    496   pstr_frame_data->stream_count = 0;
    497 
    498   num_dec_streams = track = 0;
    499   for (layer = 0; layer < (signed)pstr_frame_data->scal_out_select + 1;
    500        layer++) {
    501     WORD32 j;
    502     for (j = 0; j < 1; j++, num_dec_streams++) {
    503       pstr_frame_data->str_audio_specific_config = *pstr_audio_conf;
    504       pstr_frame_data->str_layer.sample_rate_layer =
    505           pstr_frame_data->str_audio_specific_config.sampling_frequency;
    506       pstr_frame_data->str_layer.bit_rate =
    507           pstr_frame_data->str_audio_specific_config.avg_bit_rate;
    508     }
    509 
    510     track += pstr_frame_data->tracks_in_layer;
    511   }
    512 
    513   pstr_frame_data->stream_count = num_dec_streams;
    514 
    515   return num_dec_streams;
    516 }
    517 
    518 WORD32 ixheaacd_decode_create(ia_exhaacplus_dec_api_struct *handle,
    519                               ia_dec_data_struct *pstr_dec_data,
    520                               WORD32 tracks_for_decoder) {
    521   WORD32 stream;
    522 
    523   WORD32 num_delay_samp = 0;
    524   WORD32 err = 0;
    525   ia_frame_data_struct *pstr_frame_data;
    526   WORD32 stream_count;
    527   ia_aac_dec_state_struct *aac_dec_handle = handle->p_state_aac;
    528   pstr_frame_data = &(pstr_dec_data->str_frame_data);
    529   stream_count = pstr_frame_data->stream_count;
    530   pstr_frame_data->stream_count = tracks_for_decoder;
    531 
    532   for (stream = 0; stream < stream_count; stream++) {
    533     UWORD32 aot = pstr_frame_data->str_audio_specific_config.audio_object_type;
    534 
    535     switch (aot) {
    536       case AOT_USAC:
    537 
    538         err = ixheaacd_dec_data_init(handle, pstr_frame_data,
    539                                      &(pstr_dec_data->str_usac_data));
    540 
    541         switch (pstr_dec_data->str_usac_data.sbr_ratio_idx) {
    542           case 0:
    543             handle->aac_config.ui_sbr_mode = 0;
    544             break;
    545           case 1:
    546             handle->aac_config.ui_sbr_mode = 1;
    547             break;
    548           case 2:
    549             handle->aac_config.ui_sbr_mode = 1;
    550             break;
    551           case 3:
    552             handle->aac_config.ui_sbr_mode = 3;
    553             break;
    554 
    555           default:
    556             handle->aac_config.ui_sbr_mode = 0;
    557         }
    558 
    559         if (err == -1) return -1;
    560 
    561         break;
    562 
    563       default:
    564 
    565         break;
    566     }
    567   }
    568 
    569   pstr_frame_data->scal_out_object_type =
    570       pstr_frame_data->str_audio_specific_config.audio_object_type;
    571   pstr_frame_data->scal_out_num_channels =
    572       pstr_frame_data->str_audio_specific_config.channel_configuration;
    573   pstr_frame_data->scal_out_sampling_frequency =
    574       pstr_frame_data->str_audio_specific_config.sampling_frequency;
    575 
    576   if (&(pstr_dec_data->str_usac_data) != NULL) {
    577     ia_sbr_header_data_struct usac_def_header;
    578     ia_audio_specific_config_struct *pstr_aud_spec_config =
    579         &pstr_frame_data->str_audio_specific_config;
    580     ia_usac_config_struct *ptr_usac_config =
    581         &(pstr_frame_data->str_audio_specific_config.str_usac_config);
    582 
    583     WORD32 inter_tes[MAX_NUM_ELEMENTS] = {0};
    584     WORD32 bs_pvc[MAX_NUM_ELEMENTS] = {0};
    585     WORD32 harmonic_sbr[MAX_NUM_ELEMENTS] = {0};
    586     WORD32 inter_test_flag = 0;
    587     WORD32 bs_pvc_flag = 0;
    588     WORD32 harmonic_Sbr_flag = 0;
    589 
    590     ia_usac_decoder_config_struct *ptr_usac_dec_config =
    591         &ptr_usac_config->str_usac_dec_config;
    592     WORD32 const num_ele = ptr_usac_dec_config->num_elements;
    593     WORD32 elem_idx = 0;
    594 
    595     memset(&usac_def_header, 0, sizeof(ia_sbr_header_data_struct));
    596 
    597     for (elem_idx = 0; elem_idx < num_ele; elem_idx++) {
    598       UWORD32 usac_ele_type =
    599           ptr_usac_config->str_usac_dec_config.usac_element_type[elem_idx];
    600       ia_usac_dec_element_config_struct *ptr_usac_ele_config =
    601           &ptr_usac_config->str_usac_dec_config
    602                .str_usac_element_config[elem_idx];
    603 
    604       ia_usac_dec_sbr_config_struct *ptr_usac_sbr_config =
    605           &(ptr_usac_dec_config->str_usac_element_config[elem_idx]
    606                 .str_usac_sbr_config);
    607       inter_tes[elem_idx] =
    608           (ptr_usac_sbr_config != NULL) ? ptr_usac_sbr_config->bs_inter_tes : 0;
    609       bs_pvc[elem_idx] =
    610           (ptr_usac_sbr_config != NULL) ? ptr_usac_sbr_config->bs_pvc : 0;
    611       harmonic_sbr[elem_idx] =
    612           (ptr_usac_sbr_config != NULL) ? ptr_usac_sbr_config->harmonic_sbr : 0;
    613 
    614       if (ptr_usac_sbr_config->bs_inter_tes) inter_test_flag = 1;
    615       if (ptr_usac_sbr_config->bs_pvc) bs_pvc_flag = 1;
    616       if (ptr_usac_sbr_config->harmonic_sbr) harmonic_Sbr_flag = 1;
    617 
    618       if ((usac_ele_type != ID_USAC_LFE) && (usac_ele_type != ID_USAC_EXT)) {
    619         ia_usac_dec_sbr_config_struct *ptr_usac_sbr_config =
    620             &(ptr_usac_ele_config->str_usac_sbr_config);
    621 
    622         usac_def_header.start_freq = ptr_usac_sbr_config->dflt_start_freq;
    623         usac_def_header.stop_freq = ptr_usac_sbr_config->dflt_stop_freq;
    624         usac_def_header.header_extra_1 =
    625             ptr_usac_sbr_config->dflt_header_extra1;
    626         usac_def_header.header_extra_2 =
    627             ptr_usac_sbr_config->dflt_header_extra2;
    628         usac_def_header.freq_scale = ptr_usac_sbr_config->dflt_freq_scale;
    629         usac_def_header.alter_scale = ptr_usac_sbr_config->dflt_alter_scale;
    630         usac_def_header.noise_bands = ptr_usac_sbr_config->dflt_noise_bands;
    631         usac_def_header.limiter_bands = ptr_usac_sbr_config->dflt_limiter_bands;
    632         usac_def_header.limiter_gains = ptr_usac_sbr_config->dflt_limiter_gains;
    633         usac_def_header.interpol_freq = ptr_usac_sbr_config->dflt_interpol_freq;
    634         usac_def_header.smoothing_mode =
    635             ptr_usac_sbr_config->dflt_smoothing_mode;
    636       }
    637     }
    638 
    639     pstr_dec_data->str_usac_data.down_samp_sbr = 0;
    640 
    641     if (pstr_dec_data->str_usac_data.sbr_ratio_idx > 0) {
    642       if (pstr_aud_spec_config->ext_sampling_frequency ==
    643           pstr_aud_spec_config->sampling_frequency) {
    644         pstr_dec_data->str_usac_data.down_samp_sbr = 1;
    645       }
    646       if (pstr_dec_data->str_usac_data.down_samp_sbr == 0) {
    647         if (pstr_dec_data->str_usac_data.sbr_ratio_idx == 3) {
    648           pstr_frame_data->scal_out_sampling_frequency =
    649               4 * pstr_frame_data->scal_out_sampling_frequency;
    650         } else {
    651           pstr_frame_data->scal_out_sampling_frequency =
    652               2 * pstr_frame_data->scal_out_sampling_frequency;
    653         }
    654       }
    655 
    656       {
    657         void *sbr_persistent_mem_v = aac_dec_handle->sbr_persistent_mem_u;
    658 
    659         pstr_dec_data->str_usac_data.pstr_esbr_dec = ixheaacd_init_sbr(
    660             pstr_frame_data->str_layer.sample_rate_layer,
    661             pstr_dec_data->str_usac_data.ccfl,
    662             &pstr_dec_data->str_usac_data.down_samp_sbr, sbr_persistent_mem_v,
    663             NULL, pstr_frame_data->scal_out_num_channels, 0,
    664             pstr_dec_data->str_usac_data.sbr_ratio_idx,
    665             pstr_dec_data->str_usac_data.output_samples, &harmonic_Sbr_flag,
    666             (void *)&usac_def_header, aac_dec_handle->str_sbr_config,
    667             pstr_dec_data->str_usac_data.audio_object_type);
    668         pstr_dec_data->str_usac_data.sbr_scratch_mem_base =
    669             aac_dec_handle->sbr_scratch_mem_u;
    670         if (num_ele)
    671           ixheaacd_setesbr_flags(sbr_persistent_mem_v, bs_pvc_flag,
    672                                  harmonic_Sbr_flag, inter_test_flag);
    673       }
    674 
    675       if (pstr_dec_data->str_usac_data.pstr_esbr_dec == NULL) {
    676         return -1;
    677       }
    678     }
    679   }
    680 
    681   return (num_delay_samp);
    682 }
    683