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 <stdlib.h>
     23 
     24 #include <ixheaacd_type_def.h>
     25 
     26 #include "ixheaacd_cnst.h"
     27 
     28 #include "ixheaacd_bitbuffer.h"
     29 
     30 #include "ixheaacd_interface.h"
     31 #include "ixheaacd_acelp_info.h"
     32 
     33 #include "ixheaacd_tns_usac.h"
     34 #include "ixheaacd_acelp_info.h"
     35 
     36 #include "ixheaacd_sbrdecsettings.h"
     37 #include "ixheaacd_info.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 #include "ixheaacd_arith_dec.h"
     48 #include "ixheaacd_tns_usac.h"
     49 
     50 #include "ixheaacd_bit_extract.h"
     51 
     52 #include "ixheaacd_constants.h"
     53 #include <ixheaacd_type_def.h>
     54 #include <ixheaacd_basic_ops32.h>
     55 #include <ixheaacd_basic_ops40.h>
     56 
     57 #include "ixheaacd_func_def.h"
     58 
     59 #include "ixheaacd_defines.h"
     60 #include "ixheaacd_windows.h"
     61 
     62 #include "ixheaacd_vec_baisc_ops.h"
     63 #include "ixheaacd_config.h"
     64 
     65 const WORD16 ixheaacd_mdst_fcoeff_long_sin[] = {0, 0, -16384, 0, 16384, 0, 0};
     66 const WORD16 ixheaacd_mdst_fcoeff_long_kbd[] = {-2998, 0, -19052, 0,
     67                                                 19052, 0, 2998};
     68 const WORD16 ixheaacd_mdst_fcoeff_long_sin_kbd[] = {-1499, -1876, -17718, 0,
     69                                                     17718, 1876,  1499};
     70 const WORD16 ixheaacd_mdst_fcoeff_long_kbd_sin[] = {-1499, 1876,  -17718, 0,
     71                                                     17718, -1876, 1499};
     72 
     73 const WORD16 *ixheaacd_mdst_fcoeff_longshort_curr[2][2] = {
     74     {ixheaacd_mdst_fcoeff_long_sin, ixheaacd_mdst_fcoeff_long_sin_kbd},
     75     {ixheaacd_mdst_fcoeff_long_kbd_sin, ixheaacd_mdst_fcoeff_long_kbd}};
     76 
     77 const WORD16 ixheaacd_mdst_fcoeff_start_sin[] = {-3364, -3401, -18584, 0,
     78                                                  18584, 3401,  3364};
     79 const WORD16 ixheaacd_mdst_fcoeff_start_kbd[] = {-4932, -1572, -19942, 0,
     80                                                  19942, 1572,  4932};
     81 const WORD16 ixheaacd_mdst_fcoeff_start_sin_kbd[] = {-3433, -3447, -18608, 0,
     82                                                      18608, 3447,  3433};
     83 const WORD16 ixheaacd_mdst_fcoeff_start_kbd_sin[] = {-4863, -1525, -19918, 0,
     84                                                      19918, 1525,  4863};
     85 
     86 const WORD16 *ixheaacd_mdst_fcoeff_start_curr[2][2] = {
     87     {ixheaacd_mdst_fcoeff_start_sin, ixheaacd_mdst_fcoeff_start_sin_kbd},
     88     {ixheaacd_mdst_fcoeff_start_kbd_sin, ixheaacd_mdst_fcoeff_start_kbd}};
     89 
     90 const WORD16 ixheaacd_mdst_fcoeff_stop_sin[] = {-3364, 3401,  -18584, 0,
     91                                                 18584, -3401, 3364};
     92 const WORD16 ixheaacd_mdst_fcoeff_stop_kbd[] = {-4932, 1572,  -19942, 0,
     93                                                 19942, -1572, 4932};
     94 const WORD16 ixheaacd_mdst_fcoeff_stop_sin_kbd[] = {-4863, 1525,  -19918, 0,
     95                                                     19918, -1525, 4863};
     96 const WORD16 ixheaacd_mdst_fcoeff_stop_kbd_sin[] = {-3433, 3447,  -18608, 0,
     97                                                     18608, -3447, 3433};
     98 
     99 const WORD16 *ixheaacd_mdst_fcoeff_stop_cur[2][2] = {
    100     {ixheaacd_mdst_fcoeff_stop_sin, ixheaacd_mdst_fcoeff_stop_sin_kbd},
    101     {ixheaacd_mdst_fcoeff_stop_kbd_sin, ixheaacd_mdst_fcoeff_stop_kbd}};
    102 
    103 const WORD16 ixheaacd_mdst_fcoeff_stopstart_sin[] = {-6728, 0, -20785, 0,
    104                                                      20785, 0, 6728};
    105 const WORD16 ixheaacd_mdst_fcoeff_stopstart_kbd[] = {-6866, -0, -20831, 0,
    106                                                      20831, 0,  6866};
    107 const WORD16 ixheaacd_mdst_fcoeff_stopstart_sin_kbd[] = {-6797, -46, -20808, 0,
    108                                                          20808, 46,  6797};
    109 const WORD16 ixheaacd_mdst_fcoeff_stopstart_kbd_sin[] = {-6797, 46, -20808, 0,
    110                                                          20808, 46, 6797};
    111 
    112 const WORD16 *ixheaacd_mdst_fcoeff_stopstart_cur[2][2] = {
    113     {ixheaacd_mdst_fcoeff_stopstart_sin,
    114      ixheaacd_mdst_fcoeff_stopstart_sin_kbd},
    115     {ixheaacd_mdst_fcoeff_stopstart_kbd_sin,
    116      ixheaacd_mdst_fcoeff_stopstart_kbd}};
    117 
    118 const WORD16 ixheaacd_mdst_fcoeff_l_s_start_left_sin[] = {
    119     -0, 3477, 8192, 10430, 8192, 3477, -0};
    120 const WORD16 ixheaacd_mdst_fcoeff_l_s_start_left_kbd[] = {
    121     1950, 4054, 6114, 6982, 6114, 4054, 1950};
    122 
    123 const WORD16 ixheaacd_mdst_fcoeff_stop_stopstart_left_sin[] = {
    124     1262, 1285, 1299, 1304, 1299, 1285, 1262};
    125 const WORD16 ixheaacd_mdst_fcoeff_stop_stopstart_left_kbd[] = {
    126     857, 866, 871, 873, 871, 866, 857};
    127 
    128 const WORD16 *ixheaacd_mdst_fcoeff_l_s_start_left_prev[2] = {
    129     ixheaacd_mdst_fcoeff_l_s_start_left_sin,
    130     ixheaacd_mdst_fcoeff_l_s_start_left_kbd};
    131 const WORD16 *ixheaacd_mdst_fcoeff_stop_stopstart_left_prev[2] = {
    132     ixheaacd_mdst_fcoeff_stop_stopstart_left_sin,
    133     ixheaacd_mdst_fcoeff_stop_stopstart_left_kbd};
    134 
    135 #define ONE_BY_TWO_POW_14 0.00006103515625
    136 #define ONE_BY_TWO_POW_15 0.000030517578125
    137 
    138 void ixheaacd_usac_cplx_save_prev(ia_sfb_info_struct *info, WORD32 *l_spec,
    139                                   WORD32 *r_spec, WORD32 *l_spec_prev,
    140                                   WORD32 *r_spec_prev) {
    141   WORD32 ixheaacd_drc_offset;
    142 
    143   ixheaacd_drc_offset = info->samp_per_bk - info->bins_per_sbk;
    144 
    145   memcpy(l_spec_prev + ixheaacd_drc_offset, l_spec + ixheaacd_drc_offset,
    146          sizeof(WORD32) * info->bins_per_sbk);
    147   memcpy(r_spec_prev + ixheaacd_drc_offset, r_spec + ixheaacd_drc_offset,
    148          sizeof(WORD32) * info->bins_per_sbk);
    149 }
    150 
    151 static WORD32 ixheaacd_cplx_pred_data(
    152     ia_usac_data_struct *usac_data,
    153     ia_usac_tmp_core_coder_struct *pstr_core_coder, WORD32 num_window_groups,
    154     ia_bit_buf_struct *it_bit_buff) {
    155   ia_huff_code_book_struct *ptr_huff_code_book = &ixheaacd_book;
    156   ia_huff_code_word_struct *ptr_huff_code_word =
    157       ptr_huff_code_book->pstr_huff_code_word;
    158   WORD32 cplx_pred_all;
    159   WORD32 delta_code_time;
    160   WORD32 g, sfb;
    161   WORD32 dpcm_alpha, last_alpha_q_re, last_alpha_q_im;
    162   UWORD8 max_sfb_ste = pstr_core_coder->max_sfb_ste;
    163 
    164   WORD32(*alpha_q_re)[SFB_NUM_MAX] = usac_data->alpha_q_re;
    165   WORD32(*alpha_q_im)[SFB_NUM_MAX] = usac_data->alpha_q_im;
    166   WORD32 *alpha_q_re_prev = usac_data->alpha_q_re_prev;
    167   WORD32 *alpha_q_im_prev = usac_data->alpha_q_im_prev;
    168   UWORD8(*cplx_pred_used)[SFB_NUM_MAX] = usac_data->cplx_pred_used;
    169 
    170   cplx_pred_all = ixheaacd_read_bits_buf(it_bit_buff, 1);
    171 
    172   if (cplx_pred_all == 0) {
    173     for (g = 0; g < num_window_groups; g++) {
    174       for (sfb = 0; sfb < max_sfb_ste; sfb += SFB_PER_PRED_BAND) {
    175         cplx_pred_used[g][sfb] = ixheaacd_read_bits_buf(it_bit_buff, 1);
    176 
    177         if (sfb + 1 < max_sfb_ste)
    178           cplx_pred_used[g][sfb + 1] = cplx_pred_used[g][sfb];
    179       }
    180       for (sfb = max_sfb_ste; sfb < SFB_NUM_MAX; sfb++)
    181         cplx_pred_used[g][sfb] = 0;
    182     }
    183   } else {
    184     for (g = 0; g < num_window_groups; g++) {
    185       for (sfb = 0; sfb < max_sfb_ste; sfb++) cplx_pred_used[g][sfb] = 1;
    186 
    187       for (sfb = max_sfb_ste; sfb < SFB_NUM_MAX; sfb++)
    188         cplx_pred_used[g][sfb] = 0;
    189     }
    190   }
    191 
    192   pstr_core_coder->pred_dir = ixheaacd_read_bits_buf(it_bit_buff, 1);
    193 
    194   pstr_core_coder->complex_coef = ixheaacd_read_bits_buf(it_bit_buff, 1);
    195 
    196   if (pstr_core_coder->complex_coef) {
    197     if (usac_data->usac_independency_flg)
    198       pstr_core_coder->use_prev_frame = 0;
    199     else
    200       pstr_core_coder->use_prev_frame = ixheaacd_read_bits_buf(it_bit_buff, 1);
    201   }
    202 
    203   if (usac_data->usac_independency_flg)
    204     delta_code_time = 0;
    205   else
    206     delta_code_time = ixheaacd_read_bits_buf(it_bit_buff, 1);
    207 
    208   for (g = 0; g < num_window_groups; g++) {
    209     for (sfb = 0; sfb < max_sfb_ste; sfb += SFB_PER_PRED_BAND) {
    210       if (delta_code_time == 1) {
    211         last_alpha_q_re = alpha_q_re_prev[sfb];
    212         last_alpha_q_im = alpha_q_im_prev[sfb];
    213       } else {
    214         if (sfb > 0) {
    215           last_alpha_q_re = alpha_q_re[g][sfb - 1];
    216           last_alpha_q_im = alpha_q_im[g][sfb - 1];
    217         } else {
    218           last_alpha_q_re = last_alpha_q_im = 0;
    219         }
    220       }
    221 
    222       if (cplx_pred_used[g][sfb] == 1) {
    223         dpcm_alpha =
    224             -ixheaacd_huff_codeword(ptr_huff_code_word, 0, it_bit_buff) + 60;
    225         alpha_q_re[g][sfb] = dpcm_alpha + last_alpha_q_re;
    226 
    227         if (pstr_core_coder->complex_coef) {
    228           dpcm_alpha =
    229               -ixheaacd_huff_codeword(ptr_huff_code_word, 0, it_bit_buff) + 60;
    230           alpha_q_im[g][sfb] = dpcm_alpha + last_alpha_q_im;
    231         } else {
    232           alpha_q_im[g][sfb] = 0;
    233         }
    234       } else {
    235         alpha_q_re[g][sfb] = 0;
    236         alpha_q_im[g][sfb] = 0;
    237       }
    238 
    239       if ((sfb + 1) < max_sfb_ste) {
    240         alpha_q_re[g][sfb + 1] = alpha_q_re[g][sfb];
    241         alpha_q_im[g][sfb + 1] = alpha_q_im[g][sfb];
    242       }
    243 
    244       alpha_q_re_prev[sfb] = alpha_q_re[g][sfb];
    245       alpha_q_im_prev[sfb] = alpha_q_im[g][sfb];
    246     }
    247     for (sfb = max_sfb_ste; sfb < SFB_NUM_MAX; sfb++) {
    248       alpha_q_re[g][sfb] = 0;
    249       alpha_q_im[g][sfb] = 0;
    250       alpha_q_re_prev[sfb] = 0;
    251       alpha_q_im_prev[sfb] = 0;
    252     }
    253   }
    254 
    255   return 1;
    256 }
    257 
    258 static WORD32 ixheaacd_read_ms_mask(
    259     ia_usac_data_struct *usac_data,
    260     ia_usac_tmp_core_coder_struct *pstr_core_coder,
    261     ia_bit_buf_struct *it_bit_buff, WORD32 chn) {
    262   WORD32 g, sfb;
    263   WORD32 ms_mask_present;
    264 
    265   UWORD8 *sfb_group = usac_data->group_dis[chn];
    266   UWORD8 max_sfb = pstr_core_coder->max_sfb_ste;
    267   UWORD8 *ms_used = usac_data->ms_used[chn];
    268   ia_sfb_info_struct *info = usac_data->pstr_sfb_info[chn];
    269 
    270   ms_mask_present = ixheaacd_read_bits_buf(it_bit_buff, 2);
    271 
    272   switch (ms_mask_present) {
    273     case 0:
    274 
    275       break;
    276 
    277     case 1:
    278       for (g = 0; g < info->max_win_len; g = *sfb_group++) {
    279         for (sfb = 0; sfb < max_sfb; sfb++) {
    280           *ms_used = ixheaacd_read_bits_buf(it_bit_buff, 1);
    281           ms_used++;
    282         }
    283         for (; sfb < info->sfb_per_sbk; sfb++) {
    284           *ms_used = 0;
    285           ms_used++;
    286         }
    287       }
    288 
    289       break;
    290     case 2:
    291       for (g = 0; g < info->max_win_len; g = *sfb_group++)
    292         for (sfb = 0; sfb < info->sfb_per_sbk; sfb++) *ms_used++ = 1;
    293       break;
    294 
    295     case 3:
    296 
    297       ixheaacd_cplx_pred_data(usac_data, pstr_core_coder, info->num_groups,
    298                               it_bit_buff);
    299       return 3;
    300   }
    301 
    302   for (sfb = 0; sfb < SFB_NUM_MAX; sfb++) {
    303     usac_data->alpha_q_re_prev[sfb] = 0;
    304     usac_data->alpha_q_im_prev[sfb] = 0;
    305   }
    306   return ms_mask_present;
    307 }
    308 
    309 VOID ixheaacd_ms_stereo(ia_usac_data_struct *usac_data, WORD32 *r_spec,
    310                         WORD32 *l_spec, WORD32 chn, WORD32 nband) {
    311   WORD32 temp_r, temp_l;
    312   WORD32 sfb, k, grp, grp_len;
    313   ia_sfb_info_struct *ptr_sfb_info = usac_data->pstr_sfb_info[chn];
    314   UWORD8 *ms_used = usac_data->ms_used[chn];
    315   WORD32 ixheaacd_drc_offset = 0;
    316 
    317   for (grp = 0; grp < ptr_sfb_info->num_groups; grp++) {
    318     for (grp_len = 0; grp_len < ptr_sfb_info->group_len[grp]; grp_len++) {
    319       ixheaacd_drc_offset = 0;
    320       for (sfb = 0; sfb < nband; sfb++) {
    321         ixheaacd_drc_offset += ptr_sfb_info->sfb_width[sfb];
    322         if (ms_used[sfb]) {
    323           for (k = 0; k < ptr_sfb_info->sfb_width[sfb]; k++) {
    324             temp_r = *r_spec;
    325             temp_l = *l_spec;
    326             *l_spec = ixheaacd_add32_sat(temp_r, temp_l);
    327             *r_spec = ixheaacd_sub32_sat(temp_l, temp_r);
    328             r_spec++;
    329             l_spec++;
    330           }
    331         } else {
    332           r_spec += ptr_sfb_info->sfb_width[sfb];
    333           l_spec += ptr_sfb_info->sfb_width[sfb];
    334         }
    335       }
    336 
    337       l_spec = l_spec + ptr_sfb_info->bins_per_sbk - ixheaacd_drc_offset;
    338       r_spec = r_spec + ptr_sfb_info->bins_per_sbk - ixheaacd_drc_offset;
    339     }
    340 
    341     ms_used += ptr_sfb_info->sfb_per_sbk;
    342   }
    343 }
    344 
    345 static VOID ixheaacd_filter_and_add(const WORD32 *in, const WORD32 length,
    346                                     const WORD16 *filter, WORD32 *out,
    347                                     const WORD32 factor_even,
    348                                     const WORD32 factor_odd) {
    349   WORD32 i;
    350   WORD64 sum;
    351 
    352   sum = ixheaacd_mult32x32in64(in[2], filter[0]);
    353   sum = ixheaacd_mac32x32in64(sum, in[1], filter[1]);
    354   sum = ixheaacd_mac32x32in64(sum, in[0], filter[2]);
    355   sum = ixheaacd_mac32x32in64_n(sum, &in[0], &filter[3], 4);
    356   *out = ixheaacd_add32_sat(
    357       *out, ixheaacd_sat64_32((((WORD64)sum * (WORD64)factor_even) >> 15)));
    358   out++;
    359 
    360   sum = ixheaacd_mult32x32in64(in[1], filter[0]);
    361   sum = ixheaacd_mac32x32in64(sum, in[0], filter[1]);
    362   sum = ixheaacd_mac32x32in64_n(sum, &in[0], &filter[2], 5);
    363   *out = ixheaacd_add32_sat(
    364       *out, ixheaacd_sat64_32((((WORD64)sum * (WORD64)factor_odd) >> 15)));
    365   out++;
    366 
    367   sum = ixheaacd_mult32x32in64(in[0], filter[0]);
    368   sum = ixheaacd_mac32x32in64_n(sum, &in[0], &filter[1], 6);
    369   *out = ixheaacd_add32_sat(
    370       *out, ixheaacd_sat64_32((((WORD64)sum * (WORD64)factor_even) >> 15)));
    371   out++;
    372 
    373   for (i = 3; i < length - 4; i += 2) {
    374     sum = 0;
    375     sum = ixheaacd_mac32x32in64_7(sum, &in[i - 3], filter);
    376     *out = ixheaacd_add32_sat(
    377         *out, ixheaacd_sat64_32((((WORD64)sum * (WORD64)factor_odd) >> 15)));
    378     out++;
    379 
    380     sum = 0;
    381     sum = ixheaacd_mac32x32in64_7(sum, &in[i - 2], filter);
    382     *out = ixheaacd_add32_sat(
    383         *out, ixheaacd_sat64_32((((WORD64)sum * (WORD64)factor_even) >> 15)));
    384     out++;
    385   }
    386   i = length - 3;
    387   sum = 0;
    388   sum = ixheaacd_mac32x32in64_n(sum, &in[i - 3], filter, 6);
    389   sum = ixheaacd_mac32x32in64(sum, in[i + 2], filter[6]);
    390   *out = ixheaacd_add32_sat(
    391       *out, ixheaacd_sat64_32((((WORD64)sum * (WORD64)factor_odd) >> 15)));
    392 
    393   out++;
    394   i = length - 2;
    395   sum = 0;
    396   sum = ixheaacd_mac32x32in64_n(sum, &in[i - 3], filter, 5);
    397   sum = ixheaacd_mac32x32in64(sum, in[i + 1], filter[5]);
    398   sum = ixheaacd_mac32x32in64(sum, in[i], filter[6]);
    399 
    400   *out = ixheaacd_add32_sat(
    401       *out, ixheaacd_sat64_32((((WORD64)sum * (WORD64)factor_even) >> 15)));
    402   out++;
    403 
    404   i = length - 1;
    405   sum = 0;
    406   sum = ixheaacd_mac32x32in64_n(sum, &in[i - 3], filter, 4);
    407   sum = ixheaacd_mac32x32in64(sum, in[i], filter[4]);
    408   sum = ixheaacd_mac32x32in64(sum, in[i - 1], filter[5]);
    409   sum = ixheaacd_mac32x32in64(sum, in[i - 2], filter[6]);
    410 
    411   *out = ixheaacd_add32_sat(
    412       *out, ixheaacd_sat64_32((((WORD64)sum * (WORD64)factor_odd) >> 15)));
    413 }
    414 
    415 static WORD32 ixheaacd_estimate_dmx_im(const WORD32 *dmx_re,
    416                                        const WORD32 *dmx_re_prev,
    417                                        WORD32 *dmx_im,
    418                                        ia_sfb_info_struct *pstr_sfb_info,
    419                                        WORD32 window, const WORD32 w_shape,
    420                                        const WORD32 prev_w_shape) {
    421   WORD32 i;
    422   const WORD16 *mdst_fcoeff_curr, *mdst_fcoeff_prev;
    423   WORD32 err = 0;
    424 
    425   switch (window) {
    426     case ONLY_LONG_SEQUENCE:
    427     case EIGHT_SHORT_SEQUENCE:
    428       mdst_fcoeff_curr =
    429           ixheaacd_mdst_fcoeff_longshort_curr[prev_w_shape][w_shape];
    430       mdst_fcoeff_prev = ixheaacd_mdst_fcoeff_l_s_start_left_prev[prev_w_shape];
    431       break;
    432     case LONG_START_SEQUENCE:
    433       mdst_fcoeff_curr = ixheaacd_mdst_fcoeff_start_curr[prev_w_shape][w_shape];
    434       mdst_fcoeff_prev = ixheaacd_mdst_fcoeff_l_s_start_left_prev[prev_w_shape];
    435       break;
    436     case LONG_STOP_SEQUENCE:
    437       mdst_fcoeff_curr = ixheaacd_mdst_fcoeff_stop_cur[prev_w_shape][w_shape];
    438       mdst_fcoeff_prev =
    439           ixheaacd_mdst_fcoeff_stop_stopstart_left_prev[prev_w_shape];
    440       break;
    441     case STOP_START_SEQUENCE:
    442       mdst_fcoeff_curr =
    443           ixheaacd_mdst_fcoeff_stopstart_cur[prev_w_shape][w_shape];
    444       mdst_fcoeff_prev =
    445           ixheaacd_mdst_fcoeff_stop_stopstart_left_prev[prev_w_shape];
    446       break;
    447     default:
    448       mdst_fcoeff_curr =
    449           ixheaacd_mdst_fcoeff_stopstart_cur[prev_w_shape][w_shape];
    450       mdst_fcoeff_prev =
    451           ixheaacd_mdst_fcoeff_stop_stopstart_left_prev[prev_w_shape];
    452       break;
    453   }
    454 
    455   for (i = 0; i < pstr_sfb_info->max_win_len; i++) {
    456     ixheaacd_filter_and_add(dmx_re, pstr_sfb_info->bins_per_sbk,
    457                             mdst_fcoeff_curr, dmx_im, 1, 1);
    458 
    459     if (dmx_re_prev)
    460       ixheaacd_filter_and_add(dmx_re_prev, pstr_sfb_info->bins_per_sbk,
    461                               mdst_fcoeff_prev, dmx_im, -1, 1);
    462 
    463     dmx_re_prev = dmx_re;
    464     dmx_re += pstr_sfb_info->bins_per_sbk;
    465     dmx_im += pstr_sfb_info->bins_per_sbk;
    466   }
    467   return err;
    468 }
    469 
    470 static WORD32 ixheaacd_cplx_pred_upmixing(
    471     ia_usac_data_struct *usac_data, WORD32 *l_spec, WORD32 *r_spec,
    472     ia_usac_tmp_core_coder_struct *pstr_core_coder, WORD32 chn) {
    473   ia_sfb_info_struct *pstr_sfb_info = usac_data->pstr_sfb_info[chn];
    474   WORD32 *dmx_re = &usac_data->scratch_buffer[0];
    475   WORD32 *dmx_im = &usac_data->x_ac_dec[0];
    476 
    477   WORD32 grp, sfb, grp_len, i = 0, k;
    478   WORD32 *dmx_re_prev = usac_data->dmx_re_prev;
    479   const WORD32(*alpha_q_re)[SFB_NUM_MAX] = usac_data->alpha_q_re;
    480   const WORD32(*alpha_q_im)[SFB_NUM_MAX] = usac_data->alpha_q_im;
    481   WORD32 err = 0;
    482 
    483   UWORD8(*cplx_pred_used)[SFB_NUM_MAX] = usac_data->cplx_pred_used;
    484 
    485   WORD32 alpha_q_re_temp;
    486   WORD32 alpha_q_im_temp;
    487   WORD32 factor = 1;
    488 
    489   if (pstr_core_coder->pred_dir) factor = -1;
    490 
    491   for (grp = 0; grp < pstr_sfb_info->num_groups; grp++) {
    492     for (grp_len = 0; grp_len < pstr_sfb_info->group_len[grp]; grp_len++) {
    493       for (sfb = 0; sfb < pstr_sfb_info->sfb_per_sbk; sfb++) {
    494         if (cplx_pred_used[grp][sfb] == 1) {
    495           memcpy(&dmx_re[i], &l_spec[i],
    496                  pstr_sfb_info->sfb_width[sfb] * sizeof(WORD32));
    497           i += pstr_sfb_info->sfb_width[sfb];
    498         }
    499 
    500         else {
    501           for (k = 0; k < pstr_sfb_info->sfb_width[sfb]; k++, i++) {
    502             dmx_re[i] = (WORD32)(
    503                 ((WORD64)l_spec[i] + ((WORD64)factor * (WORD64)r_spec[i])) >>
    504                 1);
    505           }
    506         }
    507       }
    508     }
    509   }
    510 
    511   memset(dmx_im, 0, sizeof(WORD32) * BLOCK_LEN_LONG);
    512 
    513   if (pstr_core_coder->complex_coef) {
    514     WORD32 *p_dmx_re_prev =
    515         pstr_core_coder->use_prev_frame ? dmx_re_prev : NULL;
    516     err = ixheaacd_estimate_dmx_im(dmx_re, p_dmx_re_prev, dmx_im, pstr_sfb_info,
    517                                    usac_data->window_sequence[chn],
    518                                    usac_data->window_shape[chn],
    519                                    usac_data->window_shape_prev[chn]);
    520     if (err == -1) return err;
    521 
    522     for (grp = 0, i = 0; grp < pstr_sfb_info->num_groups; grp++) {
    523       for (grp_len = 0; grp_len < pstr_sfb_info->group_len[grp]; grp_len++) {
    524         for (sfb = 0; sfb < pstr_sfb_info->sfb_per_sbk; sfb++) {
    525           alpha_q_re_temp = alpha_q_re[grp][sfb] * 1677722;
    526           alpha_q_im_temp = alpha_q_im[grp][sfb] * 1677722;
    527           if (cplx_pred_used[grp][sfb]) {
    528             for (k = 0; k < pstr_sfb_info->sfb_width[sfb]; k++, i++) {
    529               WORD32 mid_side = ixheaacd_sub32_sat(
    530                   ixheaacd_sub32_sat(r_spec[i],
    531                                      (WORD32)((WORD64)ixheaacd_mult32x32in64(
    532                                                   alpha_q_re_temp, l_spec[i]) >>
    533                                               24)),
    534                   (WORD32)((WORD64)ixheaacd_mult32x32in64(alpha_q_im_temp,
    535                                                           dmx_im[i]) >>
    536                            24));
    537               r_spec[i] = ixheaacd_sat64_32((WORD64)factor) *
    538                           (WORD64)(ixheaacd_sub32_sat(l_spec[i], mid_side));
    539               l_spec[i] = ixheaacd_add32_sat(l_spec[i], mid_side);
    540             }
    541 
    542           } else {
    543             i += pstr_sfb_info->sfb_width[sfb];
    544           }
    545         }
    546       }
    547     }
    548   } else {
    549     for (grp = 0, i = 0; grp < pstr_sfb_info->num_groups; grp++) {
    550       for (grp_len = 0; grp_len < pstr_sfb_info->group_len[grp]; grp_len++) {
    551         for (sfb = 0; sfb < pstr_sfb_info->sfb_per_sbk; sfb++) {
    552           alpha_q_re_temp = alpha_q_re[grp][sfb] * 1677722;
    553           if (cplx_pred_used[grp][sfb]) {
    554             for (k = 0; k < pstr_sfb_info->sfb_width[sfb]; k++, i++) {
    555               WORD32 mid_side = ixheaacd_sub32_sat(
    556                   r_spec[i], (WORD32)((WORD64)ixheaacd_mult32x32in64(
    557                                           alpha_q_re_temp, l_spec[i]) >>
    558                                       24));
    559 
    560               r_spec[i] = ixheaacd_sat64_32((WORD64)factor) *
    561                           (WORD64)(ixheaacd_sub32_sat(l_spec[i], mid_side));
    562               l_spec[i] = ixheaacd_add32_sat(l_spec[i], mid_side);
    563             }
    564 
    565           } else {
    566             i += pstr_sfb_info->sfb_width[sfb];
    567           }
    568         }
    569       }
    570     }
    571   }
    572 
    573   return err;
    574 }
    575 
    576 static VOID ixheaacd_cplx_prev_mdct_dmx(ia_sfb_info_struct *pstr_sfb_info,
    577                                         WORD32 *l_spec, WORD32 *r_spec,
    578                                         WORD32 *dmx_re_prev, WORD32 pred_dir) {
    579   WORD32 offs, i;
    580   WORD32 factor = 1;
    581   if (pred_dir) factor = -1;
    582 
    583   offs = pstr_sfb_info->samp_per_bk - pstr_sfb_info->bins_per_sbk;
    584 
    585   for (i = 0; i < pstr_sfb_info->bins_per_sbk; i++)
    586     dmx_re_prev[i] = (WORD32)(((WORD64)l_spec[i + offs] +
    587                                ((WORD64)factor * (WORD64)r_spec[i + offs])) >>
    588                               1);
    589 }
    590 
    591 WORD32 ixheaacd_ics_info(ia_usac_data_struct *usac_data, WORD32 chn,
    592                          UWORD8 *max_sfb, ia_bit_buf_struct *it_bit_buff,
    593                          WORD32 window_sequence_last
    594 
    595                          )
    596 
    597 {
    598   WORD32 win;
    599   WORD32 mask = 0x40;
    600 
    601   UWORD8 *scf_group_ptr = usac_data->group_dis[chn];
    602 
    603   win = ixheaacd_read_bits_buf(it_bit_buff, 2);
    604 
    605   win = usac_data->window_sequence[chn] =
    606       ixheaacd_win_seq_select(win, window_sequence_last);
    607   if (win == -1) return -1;
    608 
    609   usac_data->pstr_sfb_info[chn] =
    610       usac_data->pstr_usac_winmap[usac_data->window_sequence[chn]];
    611 
    612   usac_data->window_shape[chn] = (WORD32)ixheaacd_read_bits_buf(it_bit_buff, 1);
    613 
    614   if (usac_data->pstr_usac_winmap[win]->islong) {
    615     *max_sfb = ixheaacd_read_bits_buf(it_bit_buff, 6);
    616     *scf_group_ptr = 1;
    617 
    618   } else {
    619     WORD32 i, scale_factor_grouping;
    620 
    621     *max_sfb = ixheaacd_read_bits_buf(it_bit_buff, 4);
    622 
    623     scale_factor_grouping = ixheaacd_read_bits_buf(it_bit_buff, 7);
    624 
    625     for (i = 1; i < 8; i++) {
    626       if (!(scale_factor_grouping & mask)) *scf_group_ptr++ = i;
    627 
    628       mask = mask >> 1;
    629     }
    630     *scf_group_ptr++ = i;
    631 
    632     ixheaacd_calc_grp_offset(usac_data->pstr_usac_winmap[win],
    633                              &usac_data->group_dis[chn][0]);
    634   }
    635 
    636   if (*max_sfb > usac_data->pstr_sfb_info[chn]->sfb_per_sbk) {
    637     *max_sfb = usac_data->pstr_sfb_info[chn]->sfb_per_sbk;
    638     return 0;
    639   }
    640 
    641   return 0;
    642 }
    643 
    644 WORD32 ixheaacd_core_coder_data(WORD32 id, ia_usac_data_struct *usac_data,
    645                                 WORD32 elem_idx, WORD32 chan_offset,
    646                                 ia_bit_buf_struct *it_bit_buff,
    647                                 WORD32 nr_core_coder_channels) {
    648   WORD32 err_code = 0;
    649   WORD32 k = 0, ch = 0, chn, left = 0, right = 0;
    650 
    651   ia_usac_tmp_core_coder_struct str_tmp_core_coder = {0};
    652   ia_usac_tmp_core_coder_struct *pstr_core_coder = &str_tmp_core_coder;
    653   ia_td_frame_data_struct td_frame;
    654 
    655   memset(&td_frame, 0, sizeof(td_frame));
    656   pstr_core_coder->tns_on_lr = 0;
    657   pstr_core_coder->pred_dir = 0;
    658   if (id != ID_USAC_LFE) {
    659     for (ch = 0; ch < nr_core_coder_channels; ch++)
    660       pstr_core_coder->core_mode[ch] = ixheaacd_read_bits_buf(it_bit_buff, 1);
    661   } else {
    662     for (ch = 0; ch < nr_core_coder_channels; ch++)
    663       pstr_core_coder->core_mode[ch] = 0;
    664   }
    665 
    666   if (nr_core_coder_channels == 2 && pstr_core_coder->core_mode[0] == 0 &&
    667       pstr_core_coder->core_mode[1] == 0) {
    668     pstr_core_coder->tns_active = ixheaacd_read_bits_buf(it_bit_buff, 1);
    669     pstr_core_coder->common_window = ixheaacd_read_bits_buf(it_bit_buff, 1);
    670 
    671     if (pstr_core_coder->common_window) {
    672       left = chan_offset;
    673       right = chan_offset + 1;
    674 
    675       err_code =
    676           ixheaacd_ics_info(usac_data, left, &pstr_core_coder->max_sfb[left],
    677                             it_bit_buff, usac_data->window_sequence_last[left]);
    678 
    679       if (err_code == -1) return err_code;
    680 
    681       pstr_core_coder->common_max_sfb = ixheaacd_read_bits_buf(it_bit_buff, 1);
    682 
    683       if (pstr_core_coder->common_max_sfb == 0) {
    684         if (usac_data->window_sequence[left] == EIGHT_SHORT_SEQUENCE)
    685           pstr_core_coder->max_sfb[right] =
    686               ixheaacd_read_bits_buf(it_bit_buff, 4);
    687         else
    688           pstr_core_coder->max_sfb[right] =
    689               ixheaacd_read_bits_buf(it_bit_buff, 6);
    690       } else {
    691         pstr_core_coder->max_sfb[right] = pstr_core_coder->max_sfb[left];
    692       }
    693 
    694       pstr_core_coder->max_sfb_ste =
    695           max(pstr_core_coder->max_sfb[left], pstr_core_coder->max_sfb[right]);
    696 
    697       usac_data->window_sequence[right] = usac_data->window_sequence[left];
    698       usac_data->window_shape[right] = usac_data->window_shape[left];
    699       memcpy(&usac_data->group_dis[right][0], &usac_data->group_dis[left][0],
    700              8);
    701       usac_data->pstr_sfb_info[right] = usac_data->pstr_sfb_info[left];
    702       if (pstr_core_coder->max_sfb[right] >
    703           usac_data->pstr_sfb_info[right]->sfb_per_sbk)
    704         pstr_core_coder->max_sfb[right] =
    705             usac_data->pstr_sfb_info[right]->sfb_per_sbk;
    706 
    707       pstr_core_coder->ms_mask_present[0] =
    708           ixheaacd_read_ms_mask(usac_data, pstr_core_coder, it_bit_buff, left);
    709     } else {
    710       left = chan_offset;
    711       right = chan_offset + 1;
    712 
    713       pstr_core_coder->ms_mask_present[0] = 0;
    714       pstr_core_coder->ms_mask_present[1] = 0;
    715 
    716       for (k = 0; k < SFB_NUM_MAX; k++) {
    717         usac_data->alpha_q_re_prev[k] = 0;
    718         usac_data->alpha_q_im_prev[k] = 0;
    719       }
    720     }
    721 
    722     if (usac_data->tw_mdct[elem_idx] == 1) {
    723       pstr_core_coder->common_tw = ixheaacd_read_bits_buf(it_bit_buff, 1);
    724 
    725       if (pstr_core_coder->common_tw == 1) {
    726         usac_data->tw_data_present[left] =
    727             ixheaacd_read_bits_buf(it_bit_buff, 1);
    728         usac_data->tw_data_present[right] = usac_data->tw_data_present[left];
    729         if (usac_data->tw_data_present[left]) {
    730           for (k = 0; k < NUM_TW_NODES; k++) {
    731             usac_data->tw_ratio[left][k] =
    732                 ixheaacd_read_bits_buf(it_bit_buff, 3);
    733             usac_data->tw_ratio[right][k] = usac_data->tw_ratio[left][k];
    734           }
    735         }
    736       }
    737     }
    738 
    739     if (pstr_core_coder->tns_active) {
    740       if (pstr_core_coder->common_window) {
    741         pstr_core_coder->common_tns = ixheaacd_read_bits_buf(it_bit_buff, 1);
    742 
    743       } else {
    744         pstr_core_coder->common_tns = 0;
    745       }
    746 
    747       pstr_core_coder->tns_on_lr = ixheaacd_read_bits_buf(it_bit_buff, 1);
    748 
    749       if (pstr_core_coder->common_tns) {
    750         ixheaacd_read_tns_u(usac_data->pstr_sfb_info[0],
    751                             &usac_data->pstr_tns[left][0], it_bit_buff);
    752         memcpy(&usac_data->pstr_tns[right][0], &usac_data->pstr_tns[left][0],
    753                sizeof(ia_tns_frame_info_struct));
    754 
    755         pstr_core_coder->tns_data_present[0] = 2;
    756         pstr_core_coder->tns_data_present[1] = 2;
    757       } else {
    758         pstr_core_coder->tns_present_both =
    759             ixheaacd_read_bits_buf(it_bit_buff, 1);
    760 
    761         if (pstr_core_coder->tns_present_both) {
    762           pstr_core_coder->tns_data_present[0] = 1;
    763           pstr_core_coder->tns_data_present[1] = 1;
    764         } else {
    765           pstr_core_coder->tns_data_present[1] =
    766               ixheaacd_read_bits_buf(it_bit_buff, 1);
    767           pstr_core_coder->tns_data_present[0] =
    768               1 - pstr_core_coder->tns_data_present[1];
    769         }
    770       }
    771     } else {
    772       pstr_core_coder->common_tns = 0;
    773       pstr_core_coder->tns_data_present[0] = 0;
    774       pstr_core_coder->tns_data_present[1] = 0;
    775     }
    776 
    777   } else {
    778     pstr_core_coder->common_window = 0;
    779     pstr_core_coder->common_tw = 0;
    780     left = chan_offset;
    781     right = chan_offset;
    782     if (nr_core_coder_channels == 2) right = chan_offset + 1;
    783   }
    784 
    785   for (ch = 0, chn = chan_offset; ch < nr_core_coder_channels; ch++, chn++) {
    786     if (pstr_core_coder->core_mode[ch] == 1) {
    787       err_code =
    788           ixheaacd_tw_buff_update(usac_data, chn, usac_data->str_tddec[chn]);
    789       if (err_code == -1) return err_code;
    790 
    791       if (!usac_data->td_frame_prev[chn]) {
    792         ixheaacd_fix2flt_data(usac_data, usac_data->str_tddec[chn], chn);
    793       }
    794 
    795       for (k = 0; k < usac_data->ccfl; k++) {
    796         usac_data->time_sample_vector[chn][k] =
    797             (FLOAT32)((FLOAT32)usac_data->output_data_ptr[chn][k] *
    798                       (FLOAT32)(ONE_BY_TWO_POW_15));
    799       }
    800       usac_data->present_chan = chn;
    801       err_code =
    802           ixheaacd_lpd_channel_stream(usac_data, &td_frame, it_bit_buff,
    803                                       usac_data->time_sample_vector[chn]);
    804       if (err_code == -1) return err_code;
    805 
    806       for (k = 0; k < usac_data->ccfl; k++) {
    807         usac_data->output_data_ptr[chn][k] = (WORD32)(
    808             usac_data->time_sample_vector[chn][k] * (FLOAT32)((WORD64)1 << 15));
    809       }
    810 
    811       usac_data->window_shape[chn] = WIN_SEL_0;
    812 
    813       ixheaacd_td_frm_dec(usac_data, chn, td_frame.mod[0]);
    814 
    815       for (k = 0; k < usac_data->ccfl; k++) {
    816         usac_data->time_sample_vector[chn][k] =
    817             (FLOAT32)((FLOAT32)usac_data->output_data_ptr[chn][k] *
    818                       (FLOAT32)(ONE_BY_TWO_POW_15));
    819       }
    820 
    821       usac_data->window_shape_prev[chn] = usac_data->window_shape[chn];
    822       usac_data->window_sequence_last[chn] = EIGHT_SHORT_SEQUENCE;
    823 
    824     } else {
    825       memset(usac_data->coef_fix[chn], 0,
    826              LN2 * sizeof(*usac_data->coef_fix[0]));
    827 
    828       if (usac_data->str_tddec[chn] && usac_data->td_frame_prev[chn]) {
    829         ixheaacd_lpd_dec_update(usac_data->str_tddec[chn], usac_data, chn);
    830       }
    831 
    832       if (id != ID_USAC_LFE) {
    833         if ((nr_core_coder_channels == 1) ||
    834             (pstr_core_coder->core_mode[0] != pstr_core_coder->core_mode[1]))
    835           pstr_core_coder->tns_data_present[ch] =
    836               ixheaacd_read_bits_buf(it_bit_buff, 1);
    837       }
    838 
    839       err_code = ixheaacd_fd_channel_stream(
    840           usac_data, pstr_core_coder, &pstr_core_coder->max_sfb[ch],
    841           usac_data->window_sequence_last[chn], chn,
    842           usac_data->noise_filling_config[elem_idx], ch, it_bit_buff);
    843       if (err_code == -1) return err_code;
    844     }
    845   }
    846 
    847   if (pstr_core_coder->core_mode[0] == CORE_MODE_FD &&
    848       pstr_core_coder->core_mode[1] == CORE_MODE_FD &&
    849       nr_core_coder_channels == 2) {
    850     ixheaacd_cplx_prev_mdct_dmx(
    851         usac_data->pstr_sfb_info[left], usac_data->coef_save[left],
    852         usac_data->coef_save[right], usac_data->dmx_re_prev,
    853         pstr_core_coder->pred_dir);
    854   }
    855 
    856   if (pstr_core_coder->tns_on_lr == 0 && (id != ID_USAC_LFE)) {
    857     for (ch = 0, chn = left; chn <= right; ch++, chn++) {
    858       if (pstr_core_coder->core_mode[ch] == CORE_MODE_FD) {
    859         err_code = ixheaacd_tns_apply(
    860             usac_data, usac_data->coef_fix[chn], pstr_core_coder->max_sfb[ch],
    861             usac_data->pstr_sfb_info[chn], usac_data->pstr_tns[chn]);
    862         if (err_code) return err_code;
    863       }
    864     }
    865   }
    866 
    867   if (nr_core_coder_channels == 2 && pstr_core_coder->core_mode[0] == 0 &&
    868       pstr_core_coder->core_mode[1] == 0) {
    869     if (pstr_core_coder->ms_mask_present[0] == 3) {
    870       err_code = ixheaacd_cplx_pred_upmixing(
    871           usac_data, usac_data->coef_fix[left], usac_data->coef_fix[right],
    872           pstr_core_coder, left);
    873       if (err_code == -1) return err_code;
    874 
    875     } else if (pstr_core_coder->ms_mask_present[0] > 0) {
    876       ixheaacd_ms_stereo(
    877           usac_data, usac_data->coef_fix[right], usac_data->coef_fix[left],
    878           left, pstr_core_coder->max_sfb[right] > pstr_core_coder->max_sfb[left]
    879                     ? pstr_core_coder->max_sfb[right]
    880                     : pstr_core_coder->max_sfb[left]);
    881     }
    882 
    883     if (pstr_core_coder->tns_on_lr) {
    884       for (ch = 0, chn = left; chn <= right; ch++, chn++) {
    885         if (pstr_core_coder->core_mode[ch] == CORE_MODE_FD) {
    886           err_code = ixheaacd_tns_apply(
    887               usac_data, usac_data->coef_fix[chn], pstr_core_coder->max_sfb[ch],
    888               usac_data->pstr_sfb_info[chn], usac_data->pstr_tns[chn]);
    889           if (err_code) return err_code;
    890         }
    891       }
    892     }
    893 
    894     ixheaacd_usac_cplx_save_prev(
    895         usac_data->pstr_sfb_info[left], usac_data->coef_fix[left],
    896         usac_data->coef_fix[right], usac_data->coef_save[left],
    897         usac_data->coef_save[right]);
    898   }
    899 
    900   for (ch = left; ch <= right; ch++) {
    901     if (pstr_core_coder->core_mode[ch] == CORE_MODE_FD) {
    902       if (usac_data->tw_mdct[elem_idx]) {
    903         err_code = -1;
    904         return err_code;
    905 
    906       } else {
    907         err_code = ixheaacd_fd_frm_dec(usac_data, ch);
    908         if (err_code == -1) return err_code;
    909 
    910         for (k = 0; k < usac_data->ccfl; k++) {
    911           usac_data->time_sample_vector[ch][k] =
    912               (FLOAT32)((FLOAT32)usac_data->output_data_ptr[ch][k] *
    913                         (FLOAT32)(ONE_BY_TWO_POW_15));
    914         }
    915       }
    916 
    917       usac_data->window_shape_prev[ch] = usac_data->window_shape[ch];
    918       usac_data->window_sequence_last[ch] = usac_data->window_sequence[ch];
    919     }
    920   }
    921 
    922   for (ch = 0, chn = left; chn <= right; chn++, ch++)
    923     usac_data->td_frame_prev[chn] = pstr_core_coder->core_mode[ch];
    924 
    925   return 0;
    926 }
    927