LCOV - code coverage report
Current view: top level - lib_dec - ivas_stereo_td_dec_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 3b2f07138c61dcf997bbf4165d0882f794b2995f Lines: 267 318 84.0 %
Date: 2025-05-03 01:55:50 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <stdint.h>
      34             : #include "options.h"
      35             : #include <math.h>
      36             : #include "cnst.h"
      37             : #include "rom_com.h"
      38             : #include "prot_fx.h"
      39             : #include "ivas_rom_com.h"
      40             : #include "ivas_cnst.h"
      41             : #include "wmc_auto.h"
      42             : #include "ivas_rom_com_fx.h"
      43             : #include "ivas_prot_fx.h"
      44             : 
      45             : 
      46          58 : void stereo_td_init_dec_fx(
      47             :     STEREO_TD_DEC_DATA_HANDLE hStereoTD, /* i/o: TD stereo decoder handle      */
      48             :     const Word16 last_element_mode       /* i  : last element mode           Q0*/
      49             : )
      50             : {
      51          58 :     hStereoTD->tdm_SM_flag = 0;
      52          58 :     move16();
      53          58 :     hStereoTD->tdm_last_SM_flag = 0;
      54          58 :     move16();
      55          58 :     hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_MID_IS_PRIM; /* Q0 */
      56          58 :     move16();
      57          58 :     hStereoTD->tdm_prev_last_SM_flag = 0;
      58          58 :     move16();
      59          58 :     hStereoTD->tdm_LRTD_flag = 0;
      60          58 :     move16();
      61             :     // hStereoTD->prevSP_ratio = 0.5f;
      62          58 :     hStereoTD->prevSP_ratio_fx = ONE_IN_Q14; //.5    /* Q15 */
      63          58 :     move16();
      64             :     // hStereoTD->SP_ratio_LT = 0.0f;
      65          58 :     hStereoTD->SP_ratio_LT_fx = 0;
      66          58 :     move32();
      67             :     // hStereoTD->c_LR_LT = 0.5f;
      68          58 :     hStereoTD->c_LR_LT_fx = ONE_IN_Q30; //.5 /* Q31 */
      69          58 :     move32();
      70             : 
      71          58 :     hStereoTD->flag_skip_DMX = 0;
      72          58 :     move16();
      73             : 
      74          58 :     IF( EQ_16( last_element_mode, IVAS_CPE_MDCT ) )
      75             :     {
      76           7 :         hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_LEFT_IS_PRIM; /* Q0 */
      77           7 :         move16();
      78           7 :         hStereoTD->tdm_LRTD_flag = 1; /* Q0 */
      79           7 :         move16();
      80             :     }
      81             : 
      82          58 :     set32_fx( hStereoTD->TCX_old_syn_Overl_fx, 0, L_FRAME16k / 2 );
      83             : 
      84          58 :     return;
      85             : }
      86             : 
      87             : /*-------------------------------------------------------------------*
      88             :  * tdm_configure_dec()
      89             :  *
      90             :  * Configure TD stereo decoder
      91             :  *-------------------------------------------------------------------*/
      92             : Word32 power_table[32 + 1] = {
      93             :     53687092, 60237908, 67588048, 75835024, 85088304, 95470648, 107119832, 120190432,
      94             :     134855872, 151310800, 169773488, 190488992, 213732176, 239811440, 269072832, 301904704,
      95             :     338742656, 380075520, 426451744, 478486688, 536870912, 602379200, 675880448, 758350272,
      96             :     850883136, 954706496, 1071198464, 1201904384, 1348558720, 1513107968, 1697734912, 1904890240,
      97             :     2137321728
      98             : }; // Q29
      99        3695 : void tdm_configure_dec_fx(
     100             :     const Word16 ivas_format,     /* i  : IVAS format                     Q0*/
     101             :     const Word16 ism_mode,        /* i  : ISM mode in combined format     Q0*/
     102             :     CPE_DEC_HANDLE hCPE,          /* i/o: CPE decoder structure             */
     103             :     Word16 *tdm_ratio_idx,        /* o  : ratio index                     Q0*/
     104             :     const Word16 nb_bits_metadata /* i  : number of metadata bits         Q0*/
     105             : )
     106             : {
     107             :     STEREO_TD_DEC_DATA_HANDLE hStereoTD;
     108             :     Decoder_State **sts;
     109             :     Word16 tdm_tmp_SM_LRTD_flag;
     110             :     Word16 mod_ct, core, bits_offset;
     111             :     Word16 idx_LRTD_pri_side, tdm_inst_ratio_idx;
     112             :     Word32 element_brate_adapt;
     113             :     Word16 bstr_last_pos, temp;
     114             : 
     115        3695 :     hStereoTD = hCPE->hStereoTD;
     116        3695 :     sts = hCPE->hCoreCoder;
     117             : 
     118        3695 :     element_brate_adapt = L_add( hCPE->element_brate, hCPE->brate_surplus ); /* Q0 */
     119             : 
     120        3695 :     IF( hCPE->brate_surplus < 0 )
     121             :     {
     122           0 :         temp = extract_l( L_negate( Mpy_32_32( L_abs( hCPE->brate_surplus ), ONE_BY_FRAMES_PER_SEC_Q31 ) ) ); /* Q0 */
     123             :     }
     124             :     ELSE
     125             :     {
     126        3695 :         temp = extract_l( Mpy_32_32( hCPE->brate_surplus, ONE_BY_FRAMES_PER_SEC_Q31 ) ); /* Q0 */
     127             :     }
     128        3695 :     bstr_last_pos = add( sub( div_l( hCPE->element_brate, FRAMES_PER_SEC / 2 ), nb_bits_metadata ), temp ); /* Q0 */
     129             : 
     130             :     /*----------------------------------------------------------------*
     131             :      * Decode CoreCoder signaling
     132             :      *----------------------------------------------------------------*/
     133             : 
     134             :     /* temporarily decode PCh signaling */
     135        3695 :     bits_offset = sts[0]->next_bit_pos; /* Q0 */
     136        3695 :     move16();
     137        3695 :     core = get_indice_st( sts[0], hCPE->element_brate, bits_offset, 1 ); /* Q0 */
     138        3695 :     bits_offset = add( bits_offset, 1 );                                 /* Q0 */
     139             : 
     140        3695 :     IF( core == ACELP_CORE && LT_32( hCPE->element_brate, IVAS_24k4 ) )
     141             :     {
     142         555 :         mod_ct = get_indice_st( sts[0], hCPE->element_brate, bits_offset, 3 ); /* Q0 */
     143             :         /* Only transition mode is important to decoder, otherwise mod_ct is set to AUDIO only to easy debugging IF needed */
     144         555 :         if ( NE_16( mod_ct, TRANSITION ) )
     145             :         {
     146         533 :             mod_ct = AUDIO; /* Q0 */
     147         533 :             move16();
     148             :         }
     149             :     }
     150             :     ELSE /* core != ACELP_CORE */
     151             :     {
     152        3140 :         mod_ct = AUDIO; /* coder_type == VOICED || coder_type == GENERIC */ /* Q0 */
     153        3140 :         move16();
     154             :     }
     155             : 
     156             :     /* Get few parameters needed to decode the bitrate allocated to each channel */
     157             :     /* Get the coder_type of the secondary channel (last parameter on 2 bits) */
     158        3695 :     sts[1]->coder_type = get_indice_st( sts[0], element_brate_adapt, sub( bstr_last_pos, TDM_SECONDARY_SIGNALLING ), TDM_SECONDARY_SIGNALLING ); /* Q0 */
     159        3695 :     move16();
     160             : 
     161             :     /* Get the LRTD config flag:  1 = LRTD configuration, favor closer bitrate per channel;
     162             :                                   0 = Pri/Sec configuration, bitrates linked wrt. the mono */
     163        3695 :     tdm_tmp_SM_LRTD_flag = s_and( sts[1]->coder_type, 0x1 ); /* Q0 */
     164        3695 :     sts[1]->coder_type = shr( sts[1]->coder_type, 1 );       /* Q0 */
     165        3695 :     move16();
     166        3695 :     hStereoTD->tdm_Pitch_reuse_flag = 0;
     167        3695 :     move16();
     168             : 
     169        3695 :     IF( EQ_16( sts[1]->coder_type, 2 ) )
     170             :     {
     171        3567 :         sts[1]->coder_type = GENERIC; /* Q0 */
     172        3567 :         move16();
     173             :     }
     174         128 :     ELSE IF( EQ_16( sts[1]->coder_type, 3 ) )
     175             :     {
     176          33 :         sts[1]->coder_type = AUDIO; /* Q0 */
     177          33 :         move16();
     178             : 
     179          33 :         IF( LE_32( hCPE->element_brate, IVAS_24k4 ) )
     180             :         {
     181          33 :             hStereoTD->tdm_Pitch_reuse_flag = 1; /* Q0 */
     182          33 :             move16();
     183          33 :             sts[1]->coder_type = GENERIC; /* Q0 */
     184          33 :             move16();
     185             :         }
     186             :     }
     187             : 
     188             :     /*----------------------------------------------------------------*
     189             :      * Decode TDM parameters
     190             :      *----------------------------------------------------------------*/
     191             : 
     192             :     /* Get the correlation ratio */
     193        3695 :     *tdm_ratio_idx = get_indice_st( sts[0], element_brate_adapt, ( sub( bstr_last_pos, ( TDM_SECONDARY_SIGNALLING + TDM_RATIO_BITS ) ) ), TDM_RATIO_BITS ); /* Q0 */
     194        3695 :     move16();
     195             : 
     196        3695 :     hStereoTD->tdm_use_IAWB_Ave_lpc = 0;
     197        3695 :     move16();
     198        3695 :     IF( sts[1]->coder_type == INACTIVE )
     199             :     {
     200             :         /* Get the flag on the LPC reusage type (primary channel of ave LPC */
     201           0 :         hStereoTD->tdm_use_IAWB_Ave_lpc = get_indice_st( sts[0], element_brate_adapt, sub( bstr_last_pos, TDM_SECONDARY_SIGNALLING + TDM_RATIO_BITS + TDM_LP_REUSE_BITS ), TDM_LP_REUSE_BITS ); /* Q0 */
     202           0 :         move16();
     203           0 :         hStereoTD->tdm_lp_reuse_flag = 1; /* Q0 */
     204           0 :         move16();
     205             :     }
     206             :     ELSE
     207             :     {
     208             :         /* Get the flag on the LPC reusage */
     209        3695 :         hStereoTD->tdm_lp_reuse_flag = get_indice_st( sts[0], element_brate_adapt, sub( bstr_last_pos, TDM_SECONDARY_SIGNALLING + TDM_RATIO_BITS + TDM_LP_REUSE_BITS ), TDM_LP_REUSE_BITS ); /* Q0 */
     210        3695 :         move16();
     211             :     }
     212             : 
     213        3695 :     sts[0]->tdm_LRTD_flag = hStereoTD->tdm_LRTD_flag; /* the flag was already read in function stereo_memory_dec()        Q0*/
     214        3695 :     move16();
     215        3695 :     sts[1]->tdm_LRTD_flag = hStereoTD->tdm_LRTD_flag; /* Q0 */
     216        3695 :     move16();
     217             : 
     218        3695 :     tdm_inst_ratio_idx = *tdm_ratio_idx; /* Q0 */
     219        3695 :     move16();
     220             : 
     221             :     /* update past tdm_SM_flag */
     222        3695 :     hStereoTD->tdm_prev_last_SM_flag = hStereoTD->tdm_last_SM_flag; /* Q0 */
     223        3695 :     move16();
     224        3695 :     hStereoTD->tdm_last_SM_flag = hStereoTD->tdm_SM_flag; /* Q0 */
     225        3695 :     move16();
     226             : 
     227        3695 :     idx_LRTD_pri_side = -1; /* Q0 */
     228        3695 :     move16();
     229        3695 :     IF( EQ_16( hStereoTD->tdm_LRTD_flag, 1 ) )
     230             :     {
     231        3569 :         idx_LRTD_pri_side = tdm_tmp_SM_LRTD_flag; /* Q0 */
     232        3569 :         move16();
     233        3569 :         hStereoTD->tdm_SM_flag = 0;
     234        3569 :         move16();
     235        3569 :         IF( tdm_inst_ratio_idx == TDM_NQ )
     236             :         {
     237          80 :             hStereoTD->flag_skip_DMX = 1; /* Q0 */
     238          80 :             move16();
     239             :         }
     240             :         ELSE
     241             :         {
     242        3489 :             hStereoTD->flag_skip_DMX = 0;
     243        3489 :             move16();
     244             :         }
     245             :         /* Set primary channel */
     246        3569 :         *tdm_ratio_idx = LRTD_STEREO_RIGHT_IS_PRIM; /* Q0 */
     247        3569 :         move16();
     248        3569 :         if ( EQ_16( idx_LRTD_pri_side, 1 ) )
     249             :         {
     250        3301 :             *tdm_ratio_idx = LRTD_STEREO_LEFT_IS_PRIM; /* Q0 */
     251        3301 :             move16();
     252             :         }
     253             :     }
     254             :     ELSE
     255             :     {
     256         126 :         hStereoTD->tdm_SM_flag = tdm_tmp_SM_LRTD_flag; /* Q0 */
     257         126 :         move16();
     258         126 :         if ( EQ_16( hCPE->nchan_out, 1 ) )
     259             :         {
     260             :             /* in case of mono output, use exclusively the YX upmixing scheme in order to deal with NOOP signals */
     261          28 :             hStereoTD->tdm_SM_flag = 0;
     262          28 :             move16();
     263             :         }
     264             :     }
     265             : 
     266        3695 :     test();
     267        3695 :     test();
     268        3695 :     IF( sts[1]->coder_type == INACTIVE && ( GE_16( *tdm_ratio_idx, 29 ) || LE_16( *tdm_ratio_idx, 1 ) ) )
     269             :     {
     270           0 :         hStereoTD->tdm_lp_reuse_flag = hStereoTD->tdm_use_IAWB_Ave_lpc; /* Q0 */
     271           0 :         move16();
     272           0 :         hStereoTD->tdm_use_IAWB_Ave_lpc = 0;
     273           0 :         move16();
     274             :     }
     275             : 
     276             :     /*sts[1]->tdm_inst_ratio_idx = sts[0]->tdm_inst_ratio_idx;*/
     277             : 
     278        3695 :     test();
     279        3695 :     IF( EQ_16( hCPE->nchan_out, 1 ) && hCPE->hStereoDftDmx != NULL )
     280        1315 :     {
     281             :         /* in mono DMX, only targetGain is needed */
     282        1315 :         Word16 tmpS = 20;
     283        1315 :         move16();
     284        1315 :         IF( hStereoTD->tdm_LRTD_flag == 0 )
     285             :         {
     286          28 :             tmpS = get_indice_st( sts[0], element_brate_adapt, add( sub( bstr_last_pos, TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS ), STEREO_BITS_TCA_CHAN + STEREO_BITS_TCA_CORRSTATS ), STEREO_BITS_TCA_GD );
     287             :         }
     288        1315 :         hCPE->hStereoDftDmx->targetGain_fx = power_table[tmpS]; /* Q29 */
     289        1315 :         move32();
     290             :     }
     291             :     ELSE
     292             :     {
     293        2380 :         IF( hStereoTD->tdm_LRTD_flag == 0 )
     294             :         {
     295          98 :             hCPE->hStereoTCA->refChanIndx = get_indice_st( sts[0], element_brate_adapt, sub( bstr_last_pos, TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS ), STEREO_BITS_TCA_CHAN ); /* Q0 */
     296          98 :             move16();
     297          98 :             hCPE->hStereoTCA->indx_ica_NCShift = get_indice_st( sts[0], element_brate_adapt, add( sub( bstr_last_pos, TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS ), STEREO_BITS_TCA_CHAN ), STEREO_BITS_TCA_CORRSTATS ); /* Q0 */
     298          98 :             move16();
     299          98 :             hCPE->hStereoTCA->indx_ica_gD = get_indice_st( sts[0], element_brate_adapt, add( sub( bstr_last_pos, TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS ), STEREO_BITS_TCA_CHAN + STEREO_BITS_TCA_CORRSTATS ), STEREO_BITS_TCA_GD ); /* Q0 */
     300          98 :             move16();
     301             :         }
     302             :         ELSE
     303             :         {
     304        2282 :             hCPE->hStereoTCA->refChanIndx = L_CH_INDX; /* Q0 */
     305        2282 :             move16();
     306        2282 :             hCPE->hStereoTCA->indx_ica_NCShift = 0; /* Q0 */
     307        2282 :             move16();
     308        2282 :             hCPE->hStereoTCA->indx_ica_gD = 20; /* Q0 */
     309        2282 :             move16();
     310             :         }
     311        2380 :         hCPE->hStereoTCA->targetGain_fx = power_table[hCPE->hStereoTCA->indx_ica_gD]; /* Q29 */
     312        2380 :         move32();
     313             :     }
     314             :     /* set the BW of the secondary channel */
     315        3695 :     IF( hStereoTD->tdm_LRTD_flag && GE_16( sts[1]->bits_frame_channel, IVAS_16k4 / FRAMES_PER_SEC ) )
     316             :     {
     317             :         /* set BW of the secondary channel in LRTD stereo mode as the BW of the primary channel at higher bitrates */
     318        3569 :         sts[1]->bwidth = sts[0]->bwidth; /* Q0 */
     319        3569 :         move16();
     320             :     }
     321             :     ELSE
     322             :     {
     323             :         /* limit BW of the secondary channel in LRTD mode to WB for low bitrates */
     324         126 :         sts[1]->bwidth = WB; /* Q0 */
     325         126 :         move16();
     326             :     }
     327             : 
     328             :     /*----------------------------------------------------------------*
     329             :      * bitbudget distribution between channels (taking into account also metadata bitbudget)
     330             :      *----------------------------------------------------------------*/
     331             : 
     332        3695 :     tdm_bit_alloc( ivas_format, ism_mode, L_add( L_sub( hCPE->element_brate, L_mult0( nb_bits_metadata, FRAMES_PER_SEC ) ), hCPE->brate_surplus ),
     333        3695 :                    hStereoTD->tdm_lp_reuse_flag, &( sts[0]->total_brate ), &( sts[1]->total_brate ),
     334        3695 :                    &hStereoTD->tdm_low_rate_mode, sts[1]->coder_type, *tdm_ratio_idx, hStereoTD->tdm_Pitch_reuse_flag,
     335        3695 :                    sts[0]->bwidth, sts[1]->bwidth, sts[0]->flag_ACELP16k, hStereoTD->tdm_LRTD_flag, mod_ct, tdm_inst_ratio_idx );
     336             : 
     337        3695 :     return;
     338             : }
     339             : 
     340       16836 : void tdm_upmix_plain_fx(
     341             :     Word32 Left_fx[],                 /* o  : left channel            Qx*/
     342             :     Word32 Right_fx[],                /* o  : right channel           Qx*/
     343             :     const Word32 PCh_2_L_fx[],        /* i  : primary channel         Qx*/
     344             :     const Word32 SCh_2_R_fx[],        /* i  : secondary channel       Qx*/
     345             :     const Word32 LR_ratio_fx,         /* i  : mixing ratio            Q31*/
     346             :     const Word32 inv_den_LR_ratio_fx, /* i  : inverse mixing ration   Q30*/
     347             :     const Word16 start_index,         /* i  : start index             Q0*/
     348             :     const Word16 end_index,           /* i  : end index               Q0*/
     349             :     const Word16 plus_minus_flag      /* i  : plus/minus flag         Q0*/
     350             : )
     351             : {
     352             :     Word16 i;
     353             : 
     354       16836 :     IF( EQ_16( plus_minus_flag, 1 ) )
     355             :     {
     356     4844693 :         FOR( i = start_index; i < end_index; i++ )
     357             :         {
     358             : #ifdef OPT_STEREO_32KBPS_V1
     359     4827880 :             Word32 temp_left = Madd_32_32( SCh_2_R_fx[i], L_sub( PCh_2_L_fx[i], SCh_2_R_fx[i] ), LR_ratio_fx ); /* Qx */
     360     4827880 :             Left_fx[i] = W_shl_sat_l( W_mult0_32_32( temp_left, inv_den_LR_ratio_fx ), -30 );                   /* Qx */
     361     4827880 :             move32();
     362     4827880 :             Word32 temp_right = Msub_32_32( PCh_2_L_fx[i], L_add( PCh_2_L_fx[i], SCh_2_R_fx[i] ), LR_ratio_fx ); /* Qx */
     363     4827880 :             Right_fx[i] = W_shl_sat_l( W_mult0_32_32( temp_right, inv_den_LR_ratio_fx ), -30 );                  /* Qx */
     364     4827880 :             move32();
     365             : #else  /* OPT_STEREO_32KBPS_V1 */
     366             :             Word32 temp_left = L_add( Mpy_32_32( L_sub( PCh_2_L_fx[i], SCh_2_R_fx[i] ), LR_ratio_fx ), SCh_2_R_fx[i] ); /* Qx */
     367             :             Left_fx[i] = L_shl_sat( Mpy_32_32( temp_left, inv_den_LR_ratio_fx ), 1 );                                   /* Qx + 1 */
     368             :             move32();
     369             :             Word32 temp_right = L_add( Mpy_32_32( L_add( PCh_2_L_fx[i], SCh_2_R_fx[i] ), L_negate( LR_ratio_fx ) ), PCh_2_L_fx[i] ); /* Qx */
     370             :             Right_fx[i] = L_shl_sat( Mpy_32_32( temp_right, inv_den_LR_ratio_fx ), 1 );                                              /* Qx + 1 */
     371             :             move32();
     372             : #endif /* OPT_STEREO_32KBPS_V1 */
     373             :         }
     374             :     }
     375             :     ELSE
     376             :     {
     377             : #ifdef OPT_STEREO_32KBPS_V1
     378          23 :         Word32 inv_den_LR_ratio_fx_neg = L_negate( inv_den_LR_ratio_fx );
     379        7063 :         FOR( i = start_index; i < end_index; i++ )
     380             :         {
     381        7040 :             Word32 temp_left = Msub_32_32( SCh_2_R_fx[i], L_add( PCh_2_L_fx[i], SCh_2_R_fx[i] ), LR_ratio_fx ); /* Qx */
     382        7040 :             Left_fx[i] = W_shl_sat_l( W_mult0_32_32( temp_left, inv_den_LR_ratio_fx_neg ), -30 );               /* Qx */
     383        7040 :             move32();
     384        7040 :             Word32 temp_right = Msub_32_32( PCh_2_L_fx[i], L_sub( PCh_2_L_fx[i], SCh_2_R_fx[i] ), LR_ratio_fx ); /* Qx */
     385        7040 :             Right_fx[i] = W_shl_sat_l( W_mult0_32_32( temp_right, inv_den_LR_ratio_fx_neg ), -30 );              /* Qx */
     386        7040 :             move32();
     387             :         }
     388             : #else  /* OPT_STEREO_32KBPS_V1 */
     389             :         FOR( i = start_index; i < end_index; i++ )
     390             :         {
     391             :             Word32 temp_left = L_sub( Mpy_32_32( L_add( PCh_2_L_fx[i], SCh_2_R_fx[i] ), LR_ratio_fx ), SCh_2_R_fx[i] ); /* Qx */
     392             :             Left_fx[i] = L_shl_sat( Mpy_32_32( temp_left, inv_den_LR_ratio_fx ), 1 );                                   /* Qx + 1 */
     393             :             move32();
     394             :             Word32 temp_right = L_sub( Mpy_32_32( L_sub( PCh_2_L_fx[i], SCh_2_R_fx[i] ), LR_ratio_fx ), PCh_2_L_fx[i] ); /* Qx */
     395             :             Right_fx[i] = L_shl_sat( Mpy_32_32( temp_right, inv_den_LR_ratio_fx ), 1 );                                  /* Qx + 1 */
     396             :             move32();
     397             :         }
     398             : #endif /* OPT_STEREO_32KBPS_V1 */
     399             :     }
     400             : 
     401       16836 :     return;
     402             : }
     403             : 
     404             : /*-------------------------------------------------------------------*
     405             :  * Function tdm_downmix_fade()
     406             :  *
     407             :  * downmix Left+Right to Primary+Secondary channel with fade in/out
     408             :  *-------------------------------------------------------------------*/
     409             : 
     410             : 
     411             : // Q31
     412             : Word32 inv_time[960 + 1] = {
     413             :     0, 2147483647, 1073741824, 715827904, 536870912, 429496736, 357913952, 306783392,
     414             :     268435456, 238609296, 214748368, 195225792, 178956976, 165191056, 153391696, 143165584,
     415             :     134217728, 126322568, 119304648, 113025456, 107374184, 102261128, 97612896, 93368856,
     416             :     89478488, 85899344, 82595528, 79536432, 76695848, 74051160, 71582792, 69273664,
     417             :     67108864, 65075264, 63161284, 61356676, 59652324, 58040100, 56512728, 55063684,
     418             :     53687092, 52377648, 51130564, 49941480, 48806448, 47721860, 46684428, 45691140,
     419             :     44739244, 43826196, 42949672, 42107524, 41297764, 40518560, 39768216, 39045156,
     420             :     38347924, 37675152, 37025580, 36398028, 35791396, 35204648, 34636832, 34087044,
     421             :     33554432, 33038210, 32537632, 32051994, 31580642, 31122952, 30678338, 30246248,
     422             :     29826162, 29417584, 29020050, 28633116, 28256364, 27889398, 27531842, 27183338,
     423             :     26843546, 26512144, 26188824, 25873296, 25565282, 25264514, 24970740, 24683720,
     424             :     24403224, 24129030, 23860930, 23598722, 23342214, 23091222, 22845570, 22605092,
     425             :     22369622, 22139006, 21913098, 21691754, 21474836, 21262214, 21053762, 20849356,
     426             :     20648882, 20452226, 20259280, 20069940, 19884108, 19701684, 19522578, 19346700,
     427             :     19173962, 19004280, 18837576, 18673770, 18512790, 18354562, 18199014, 18046082,
     428             :     17895698, 17747798, 17602324, 17459216, 17318416, 17179870, 17043522, 16909320,
     429             :     16777216, 16647160, 16519105, 16393005, 16268816, 16146494, 16025997, 15907286,
     430             :     15790321, 15675063, 15561476, 15449523, 15339169, 15230380, 15123124, 15017368,
     431             :     14913081, 14810232, 14708792, 14608732, 14510025, 14412642, 14316558, 14221746,
     432             :     14128182, 14035841, 13944699, 13854733, 13765921, 13678240, 13591669, 13506186,
     433             :     13421773, 13338408, 13256072, 13174746, 13094412, 13015052, 12936648, 12859184,
     434             :     12782641, 12707004, 12632257, 12558384, 12485370, 12413200, 12341860, 12271335,
     435             :     12201612, 12132676, 12064515, 11997115, 11930465, 11864551, 11799361, 11734883,
     436             :     11671107, 11608020, 11545611, 11483870, 11422785, 11362347, 11302546, 11243370,
     437             :     11184811, 11126858, 11069503, 11012737, 10956549, 10900932, 10845877, 10791375,
     438             :     10737418, 10683998, 10631107, 10578737, 10526881, 10475530, 10424678, 10374317,
     439             :     10324441, 10275041, 10226113, 10177648, 10129640, 10082083, 10034970, 9988296,
     440             :     9942054, 9896238, 9850842, 9805861, 9761289, 9717121, 9673350, 9629972,
     441             :     9586981, 9544372, 9502140, 9460280, 9418788, 9377658, 9336885, 9296466,
     442             :     9256395, 9216668, 9177281, 9138228, 9099507, 9061112, 9023041, 8985287,
     443             :     8947849, 8910721, 8873899, 8837381, 8801162, 8765239, 8729608, 8694266,
     444             :     8659208, 8624432, 8589935, 8555712, 8521761, 8488078, 8454660, 8421505,
     445             :     8388608, 8355967, 8323580, 8291442, 8259552, 8227906, 8196502, 8165337,
     446             :     8134408, 8103712, 8073247, 8043010, 8012998, 7983210, 7953643, 7924294,
     447             :     7895160, 7866240, 7837531, 7809031, 7780738, 7752648, 7724761, 7697074,
     448             :     7669584, 7642290, 7615190, 7588281, 7561562, 7535030, 7508684, 7482521,
     449             :     7456540, 7430739, 7405116, 7379669, 7354396, 7329295, 7304366, 7279605,
     450             :     7255012, 7230584, 7206321, 7182219, 7158279, 7134497, 7110873, 7087405,
     451             :     7064091, 7040930, 7017920, 6995060, 6972349, 6949785, 6927366, 6905092,
     452             :     6882960, 6860970, 6839120, 6817408, 6795834, 6774396, 6753093, 6731923,
     453             :     6710886, 6689980, 6669204, 6648556, 6628036, 6607642, 6587373, 6567228,
     454             :     6547206, 6527306, 6507526, 6487866, 6468324, 6448900, 6429592, 6410399,
     455             :     6391320, 6372355, 6353502, 6334760, 6316128, 6297606, 6279192, 6260885,
     456             :     6242685, 6224590, 6206600, 6188713, 6170930, 6153248, 6135667, 6118187,
     457             :     6100806, 6083523, 6066338, 6049249, 6032257, 6015360, 5998557, 5981848,
     458             :     5965232, 5948708, 5932275, 5915933, 5899680, 5883517, 5867441, 5851454,
     459             :     5835553, 5819739, 5804010, 5788365, 5772805, 5757329, 5741935, 5726623,
     460             :     5711392, 5696243, 5681173, 5666184, 5651273, 5636440, 5621685, 5607007,
     461             :     5592405, 5577879, 5563429, 5549053, 5534751, 5520523, 5506368, 5492285,
     462             :     5478274, 5464335, 5450466, 5436667, 5422938, 5409278, 5395687, 5382164,
     463             :     5368709, 5355321, 5341999, 5328743, 5315553, 5302429, 5289368, 5276372,
     464             :     5263440, 5250571, 5237765, 5225021, 5212339, 5199718, 5187158, 5174659,
     465             :     5162220, 5149841, 5137520, 5125259, 5113056, 5100911, 5088824, 5076793,
     466             :     5064820, 5052902, 5041041, 5029235, 5017485, 5005789, 4994148, 4982560,
     467             :     4971027, 4959546, 4948119, 4936744, 4925421, 4914150, 4902930, 4891762,
     468             :     4880644, 4869577, 4858560, 4847593, 4836675, 4825806, 4814986, 4804214,
     469             :     4793490, 4782814, 4772186, 4761604, 4751070, 4740582, 4730140, 4719744,
     470             :     4709394, 4699089, 4688829, 4678613, 4668442, 4658316, 4648233, 4638193,
     471             :     4628197, 4618244, 4608334, 4598466, 4588640, 4578856, 4569114, 4559413,
     472             :     4549753, 4540134, 4530556, 4521018, 4511520, 4502062, 4492643, 4483264,
     473             :     4473924, 4464623, 4455360, 4446136, 4436949, 4427801, 4418690, 4409617,
     474             :     4400581, 4391582, 4382619, 4373694, 4364804, 4355950, 4347133, 4338351,
     475             :     4329604, 4320892, 4312216, 4303574, 4294967, 4286394, 4277856, 4269351,
     476             :     4260880, 4252443, 4244039, 4235668, 4227330, 4219025, 4210752, 4202512,
     477             :     4194304, 4186128, 4177983, 4169871, 4161790, 4153740, 4145721, 4137733,
     478             :     4129776, 4121849, 4113953, 4106087, 4098251, 4090445, 4082668, 4074921,
     479             :     4067204, 4059515, 4051856, 4044225, 4036623, 4029050, 4021505, 4013988,
     480             :     4006499, 3999038, 3991605, 3984199, 3976821, 3969470, 3962147, 3954850,
     481             :     3947580, 3940337, 3933120, 3925930, 3918765, 3911627, 3904515, 3897429,
     482             :     3890369, 3883334, 3876324, 3869340, 3862380, 3855446, 3848537, 3841652,
     483             :     3834792, 3827956, 3821145, 3814358, 3807595, 3800856, 3794140, 3787449,
     484             :     3780781, 3774136, 3767515, 3760917, 3754342, 3747790, 3741260, 3734754,
     485             :     3728270, 3721808, 3715369, 3708952, 3702558, 3696185, 3689834, 3683505,
     486             :     3677198, 3670912, 3664647, 3658404, 3652183, 3645982, 3639802, 3633644,
     487             :     3627506, 3621389, 3615292, 3609216, 3603160, 3597125, 3591109, 3585114,
     488             :     3579139, 3573184, 3567248, 3561332, 3555436, 3549559, 3543702, 3537864,
     489             :     3532045, 3526245, 3520465, 3514703, 3508960, 3503236, 3497530, 3491843,
     490             :     3486174, 3480524, 3474892, 3469279, 3463683, 3458105, 3452546, 3447004,
     491             :     3441480, 3435973, 3430485, 3425013, 3419560, 3414123, 3408704, 3403302,
     492             :     3397917, 3392549, 3387198, 3381864, 3376546, 3371246, 3365961, 3360694,
     493             :     3355443, 3350208, 3344990, 3339788, 3334602, 3329432, 3324278, 3319140,
     494             :     3314018, 3308911, 3303821, 3298746, 3293686, 3288642, 3283614, 3278601,
     495             :     3273603, 3268620, 3263653, 3258700, 3253763, 3248840, 3243933, 3239040,
     496             :     3234162, 3229298, 3224450, 3219615, 3214796, 3209990, 3205199, 3200422,
     497             :     3195660, 3190911, 3186177, 3181457, 3176751, 3172058, 3167380, 3162715,
     498             :     3158064, 3153426, 3148803, 3144192, 3139596, 3135012, 3130442, 3125886,
     499             :     3121342, 3116812, 3112295, 3107791, 3103300, 3098822, 3094356, 3089904,
     500             :     3085465, 3081038, 3076624, 3072222, 3067833, 3063457, 3059093, 3054742,
     501             :     3050403, 3046076, 3041761, 3037459, 3033169, 3028891, 3024624, 3020370,
     502             :     3016128, 3011898, 3007680, 3003473, 2999278, 2995095, 2990924, 2986764,
     503             :     2982616, 2978479, 2974354, 2970240, 2966137, 2962046, 2957966, 2953897,
     504             :     2949840, 2945793, 2941758, 2937734, 2933720, 2929718, 2925727, 2921746,
     505             :     2917776, 2913817, 2909869, 2905931, 2902005, 2898088, 2894182, 2890287,
     506             :     2886402, 2882528, 2878664, 2874810, 2870967, 2867134, 2863311, 2859498,
     507             :     2855696, 2851904, 2848121, 2844349, 2840586, 2836834, 2833092, 2829359,
     508             :     2825636, 2821923, 2818220, 2814526, 2810842, 2807168, 2803503, 2799848,
     509             :     2796202, 2792566, 2788939, 2785322, 2781714, 2778116, 2774526, 2770946,
     510             :     2767375, 2763814, 2760261, 2756718, 2753184, 2749659, 2746142, 2742635,
     511             :     2739137, 2735648, 2732167, 2728695, 2725233, 2721779, 2718333, 2714897,
     512             :     2711469, 2708050, 2704639, 2701237, 2697843, 2694458, 2691082, 2687714,
     513             :     2684354, 2681003, 2677660, 2674325, 2670999, 2667681, 2664371, 2661070,
     514             :     2657776, 2654491, 2651214, 2647945, 2644684, 2641431, 2638186, 2634949,
     515             :     2631720, 2628499, 2625285, 2622080, 2618882, 2615692, 2612510, 2609336,
     516             :     2606169, 2603010, 2599859, 2596715, 2593579, 2590450, 2587329, 2584216,
     517             :     2581110, 2578011, 2574920, 2571836, 2568760, 2565691, 2562629, 2559575,
     518             :     2556528, 2553488, 2550455, 2547430, 2544412, 2541400, 2538396, 2535399,
     519             :     2532410, 2529427, 2526451, 2523482, 2520520, 2517565, 2514617, 2511676,
     520             :     2508742, 2505815, 2502894, 2499981, 2497074, 2494173, 2491280, 2488393,
     521             :     2485513, 2482640, 2479773, 2476913, 2474059, 2471212, 2468372, 2465538,
     522             :     2462710, 2459889, 2457075, 2454267, 2451465, 2448670, 2445881, 2443098,
     523             :     2440322, 2437552, 2434788, 2432031, 2429280, 2426535, 2423796, 2421063,
     524             :     2418337, 2415617, 2412903, 2410195, 2407493, 2404797, 2402107, 2399423,
     525             :     2396745, 2394073, 2391407, 2388747, 2386093, 2383444, 2380802, 2378165,
     526             :     2375535, 2372910, 2370291, 2367677, 2365070, 2362468, 2359872, 2357281,
     527             :     2354697, 2352118, 2349544, 2346976, 2344414, 2341857, 2339306, 2336761,
     528             :     2334221, 2331687, 2329158, 2326634, 2324116, 2321604, 2319096, 2316595,
     529             :     2314098, 2311607, 2309122, 2306642, 2304167, 2301697, 2299233, 2296774,
     530             :     2294320, 2291871, 2289428, 2286990, 2284557, 2282129, 2279706, 2277289,
     531             :     2274876, 2272469, 2270067, 2267670, 2265278, 2262891, 2260509, 2258132,
     532             :     2255760, 2253393, 2251031, 2248674, 2246321, 2243974, 2241632, 2239294,
     533             :     2236962
     534             : };
     535         122 : static void tdm_upmix_fade_fx(
     536             :     Word32 Left_fx[],                     /* o  : left channel                Qx*/
     537             :     Word32 Right_fx[],                    /* o  : right channel                           Qx*/
     538             :     const Word32 PCh_2_L_fx[],            /* i  : primary channel             Qx*/
     539             :     const Word32 SCh_2_R_fx[],            /* i  : secondary channel           Qx*/
     540             :     const Word32 LR_ratio_mem_fx,         /* i  : last mixing ratio           Q31*/
     541             :     const Word32 inv_den_LR_ratio_mem_fx, /* i  : last inverse mixing ration  Q31*/
     542             :     const Word32 LR_ratio_fx,             /* i  : mixing ratio                Q31*/
     543             :     const Word32 inv_den_LR_ratio_fx,     /* i  : inverse mixing ration       Q31*/
     544             :     const Word16 start_index,             /* i  : start index                 Q0*/
     545             :     const Word16 end_index,               /* i  : end index                   Q0*/
     546             :     const Word16 fading_type              /* i  : fading type                 Q0*/
     547             : )
     548             : {
     549             :     Word16 i;
     550             :     Word32 step_fx, step2_fx, fade_in_fx, fade_out_fx;
     551             : 
     552         122 :     step_fx = inv_time[end_index - start_index];
     553         122 :     fade_out_fx = ONE_IN_Q31; /* Q31 */
     554         122 :     move32();
     555         122 :     fade_in_fx = 0;
     556         122 :     move32();
     557             : 
     558         122 :     fade_out_fx = Mpy_32_32( fade_out_fx, inv_den_LR_ratio_mem_fx ); /* Q31 */
     559         122 :     fade_in_fx = Mpy_32_32( fade_in_fx, inv_den_LR_ratio_fx );       /* Q31 */
     560         122 :     step2_fx = Mpy_32_32( step_fx, inv_den_LR_ratio_fx );            /* Q31 */
     561         122 :     step_fx = Mpy_32_32( step_fx, inv_den_LR_ratio_mem_fx );         /* Q31 */
     562             : 
     563         122 :     IF( fading_type == 0 ) /* Switching from YX scheme to SM scheme */
     564             :     {
     565         161 :         FOR( i = start_index; i < end_index; i++ )
     566             :         {
     567         160 :             Word32 temp_left1 = Mpy_32_32( L_add( Mpy_32_32( L_sub( PCh_2_L_fx[i], SCh_2_R_fx[i] ), LR_ratio_mem_fx ), SCh_2_R_fx[i] ), fade_out_fx ); /* Qx */
     568         160 :             Word32 temp_left2 = Mpy_32_32( L_sub( Mpy_32_32( L_add( PCh_2_L_fx[i], SCh_2_R_fx[i] ), LR_ratio_fx ), SCh_2_R_fx[i] ), fade_in_fx );      /* Qx */
     569         160 :             Left_fx[i] = L_shl_sat( L_add( temp_left1, temp_left2 ), 1 );                                                                              /* Qx + 1 */
     570         160 :             move32();
     571         160 :             Word32 temp_right1 = Mpy_32_32( L_add( Mpy_32_32( L_add( PCh_2_L_fx[i], SCh_2_R_fx[i] ), L_negate( LR_ratio_mem_fx ) ), PCh_2_L_fx[i] ), fade_out_fx ); /* Qx */
     572         160 :             Word32 temp_right2 = Mpy_32_32( L_sub( Mpy_32_32( L_sub( PCh_2_L_fx[i], SCh_2_R_fx[i] ), LR_ratio_fx ), PCh_2_L_fx[i] ), fade_in_fx );                  /* Qx */
     573         160 :             Right_fx[i] = L_shl_sat( L_add( temp_right1, temp_right2 ), 1 );                                                                                        /* Qx + 1 */
     574         160 :             move32();
     575             : 
     576         160 :             fade_in_fx = L_add( fade_in_fx, step2_fx );  /* Q31 */
     577         160 :             fade_out_fx = L_sub( fade_out_fx, step_fx ); /* Q31 */
     578             :         }
     579             :     }
     580         121 :     ELSE IF( EQ_16( fading_type, 1 ) ) /* SM scheme */
     581             :     {
     582         161 :         FOR( i = start_index; i < end_index; i++ )
     583             :         {
     584         160 :             Word32 temp_left1 = Mpy_32_32( L_sub( Mpy_32_32( L_add( PCh_2_L_fx[i], SCh_2_R_fx[i] ), LR_ratio_mem_fx ), SCh_2_R_fx[i] ), fade_out_fx ); /* Qx */
     585         160 :             Word32 temp_left2 = Mpy_32_32( L_sub( Mpy_32_32( L_add( PCh_2_L_fx[i], SCh_2_R_fx[i] ), LR_ratio_fx ), SCh_2_R_fx[i] ), fade_in_fx );      /* Qx */
     586         160 :             Left_fx[i] = L_shl_sat( L_add( temp_left1, temp_left2 ), 1 );                                                                              /* Qx + 1 */
     587         160 :             move32();
     588             : 
     589         160 :             Word32 temp_right1 = Mpy_32_32( L_sub( Mpy_32_32( L_sub( PCh_2_L_fx[i], SCh_2_R_fx[i] ), LR_ratio_mem_fx ), PCh_2_L_fx[i] ), fade_out_fx ); /* Qx */
     590         160 :             Word32 temp_right2 = Mpy_32_32( L_sub( Mpy_32_32( L_sub( PCh_2_L_fx[i], SCh_2_R_fx[i] ), LR_ratio_fx ), PCh_2_L_fx[i] ), fade_in_fx );      /* Qx */
     591         160 :             Right_fx[i] = L_shl_sat( L_add( temp_right1, temp_right2 ), 1 );                                                                            /* Qx + 1 */
     592         160 :             move32();
     593             : 
     594         160 :             fade_in_fx = L_add( fade_in_fx, step2_fx );  /* Q31 */
     595         160 :             fade_out_fx = L_sub( fade_out_fx, step_fx ); /* Q31 */
     596             :         }
     597             :     }
     598         120 :     ELSE IF( EQ_16( fading_type, 2 ) ) /* Switching from SM scheme to YX scheme */
     599             :     {
     600           0 :         FOR( i = start_index; i < end_index; i++ )
     601             :         {
     602           0 :             Word32 temp_left1 = Mpy_32_32( L_sub( Mpy_32_32( L_add( PCh_2_L_fx[i], SCh_2_R_fx[i] ), LR_ratio_mem_fx ), SCh_2_R_fx[i] ), fade_out_fx ); /* Qx */
     603           0 :             Word32 temp_left2 = Mpy_32_32( L_add( Mpy_32_32( L_sub( PCh_2_L_fx[i], SCh_2_R_fx[i] ), LR_ratio_fx ), SCh_2_R_fx[i] ), fade_in_fx );      /* Qx */
     604           0 :             Left_fx[i] = L_shl_sat( L_add( temp_left1, temp_left2 ), 1 );                                                                              /* Qx + 1 */
     605           0 :             move32();
     606             : 
     607           0 :             Word32 temp_right1 = Mpy_32_32( L_sub( Mpy_32_32( L_sub( PCh_2_L_fx[i], SCh_2_R_fx[i] ), LR_ratio_mem_fx ), PCh_2_L_fx[i] ), fade_out_fx );        /* Qx */
     608           0 :             Word32 temp_right2 = Mpy_32_32( L_add( Mpy_32_32( L_add( PCh_2_L_fx[i], SCh_2_R_fx[i] ), L_negate( LR_ratio_fx ) ), PCh_2_L_fx[i] ), fade_in_fx ); /* Qx */
     609           0 :             Right_fx[i] = L_shl_sat( L_add( temp_right1, temp_right2 ), 1 );                                                                                   /* Qx + 1 */
     610           0 :             move32();
     611             : 
     612           0 :             fade_in_fx = L_add( fade_in_fx, step2_fx );  /* Q31 */
     613           0 :             fade_out_fx = L_sub( fade_out_fx, step_fx ); /* Q31 */
     614             :         }
     615             :     }
     616         120 :     ELSE IF( EQ_16( fading_type, 3 ) ) /* YX scheme */
     617             :     {
     618       19080 :         FOR( i = start_index; i < end_index; i++ )
     619             :         {
     620       18960 :             Word32 temp_left1 = Mpy_32_32( L_add( Mpy_32_32( L_sub( PCh_2_L_fx[i], SCh_2_R_fx[i] ), LR_ratio_mem_fx ), SCh_2_R_fx[i] ), fade_out_fx ); /* Qx */
     621       18960 :             Word32 temp_left2 = Mpy_32_32( L_add( Mpy_32_32( L_sub( PCh_2_L_fx[i], SCh_2_R_fx[i] ), LR_ratio_fx ), SCh_2_R_fx[i] ), fade_in_fx );      /* Qx */
     622       18960 :             Left_fx[i] = L_shl_sat( L_add( temp_left1, temp_left2 ), 1 );                                                                              /* Qx + 1 */
     623       18960 :             move32();
     624             : 
     625       18960 :             Word32 temp_right1 = Mpy_32_32( L_add( Mpy_32_32( L_add( PCh_2_L_fx[i], SCh_2_R_fx[i] ), L_negate( LR_ratio_mem_fx ) ), PCh_2_L_fx[i] ), fade_out_fx ); /* Qx */
     626       18960 :             Word32 temp_right2 = Mpy_32_32( L_add( Mpy_32_32( L_add( PCh_2_L_fx[i], SCh_2_R_fx[i] ), L_negate( LR_ratio_fx ) ), PCh_2_L_fx[i] ), fade_in_fx );      /* Qx */
     627       18960 :             Right_fx[i] = L_shl_sat( L_add( temp_right1, temp_right2 ), 1 );                                                                                        /* Qx + 1 */
     628       18960 :             move32();
     629             : 
     630       18960 :             fade_in_fx = L_add( fade_in_fx, step2_fx );  /* Q31 */
     631       18960 :             fade_out_fx = L_sub( fade_out_fx, step_fx ); /* Q31 */
     632             :         }
     633             :     }
     634             : 
     635         122 :     return;
     636             : }
     637             : /*-------------------------------------------------------------------*
     638             :  * stereo_tdm_combine()
     639             :  *
     640             :  * Combine Primary and Secondary channels into L and R channels
     641             :  *-------------------------------------------------------------------*/
     642             : 
     643        7376 : void stereo_tdm_combine_fx(
     644             :     CPE_DEC_HANDLE hCPE,       /* i/o: CPE decoder structure                       */
     645             :     Word32 *PCh_2_L_fx,        /* i/o: Primary channel -> output as left channel                                     Qx*/
     646             :     Word32 *SCh_2_R_fx,        /* i/o: Secondary channel -> output as right channel                                  Qx*/
     647             :     const Word16 output_frame, /* i  : Number of samples                                                                                        Q0*/
     648             :     const Word16 flag_HB,      /* i  : flag to distinguish between core (0) and HB (1) synthesis        Q0*/
     649             :     const Word16 tdm_ratio_idx /* i  : TDM ratio index                                                                                          Q0*/
     650             : )
     651             : {
     652             :     Word16 i;
     653             :     Word32 output_Fs;
     654             :     Word32 LR_ratio_fx, LR_ratio_mem_fx;
     655             :     Word32 Left_fx[L_FRAME48k], Right_fx[L_FRAME48k];
     656             :     Word16 upmixing_delay;
     657             :     Word16 stereo_tdm_coder_type;
     658             :     Word16 tdm_n_OVA;
     659        7376 :     Word16 tdm_last_ratio_idx = hCPE->hStereoTD->tdm_last_ratio_idx;
     660        7376 :     move16();
     661             : 
     662        7376 :     output_Fs = hCPE->hCoreCoder[0]->output_Fs; /* Q0 */
     663        7376 :     move32();
     664        7376 :     tdm_n_OVA = NS2SA_FX2( output_Fs, TDM_L_NOVA_NS ); /* Q0 */
     665        7376 :     move16();
     666             : 
     667        7376 :     IF( flag_HB )
     668             :     {
     669           0 :         upmixing_delay = NS2SA_FX2( output_Fs, ACELP_LOOK_NS + DELAY_BWE_TOTAL_NS ); /* Q0 */
     670           0 :         move16();
     671             :     }
     672             :     ELSE
     673             :     {
     674        7376 :         upmixing_delay = NS2SA_FX2( output_Fs, ACELP_LOOK_NS + DELAY_CLDFB_NS ); /* Q0 */
     675        7376 :         move16();
     676             :     }
     677             : 
     678        7376 :     LR_ratio_fx = tdm_ratio_tabl_fx[tdm_ratio_idx]; /* Q31 */
     679        7376 :     move32();
     680        7376 :     LR_ratio_mem_fx = tdm_ratio_tabl_fx[tdm_last_ratio_idx]; /* Q31 */
     681        7376 :     move32();
     682             : 
     683        7376 :     IF( hCPE->hStereoTD->flag_skip_DMX )
     684             :     {
     685         160 :         stereo_tdm_coder_type = 10; /* no DMX */
     686         160 :         move16();
     687         160 :         Copy32( PCh_2_L_fx, Left_fx, output_frame );  /* Qx */
     688         160 :         Copy32( SCh_2_R_fx, Right_fx, output_frame ); /* Qx */
     689             :     }
     690        7216 :     ELSE IF( EQ_16( hCPE->hStereoTD->tdm_last_SM_flag, 1 ) )
     691             :     {
     692          12 :         IF( hCPE->hStereoTD->tdm_prev_last_SM_flag == 0 )
     693             :         {
     694           1 :             stereo_tdm_coder_type = 0; /* mode 1 : Switching from YX scheme to SM scheme*/
     695           1 :             move16();
     696             :         }
     697             :         ELSE
     698             :         {
     699          11 :             stereo_tdm_coder_type = 1; /* mode 2 : SM scheme    Q0*/
     700          11 :             move16();
     701             :         }
     702             :     }
     703             :     ELSE
     704             :     {
     705        7204 :         IF( EQ_16( hCPE->hStereoTD->tdm_prev_last_SM_flag, 1 ) )
     706             :         {
     707           0 :             stereo_tdm_coder_type = 2; /* mode 3 : Switching from SM scheme to YX scheme*/
     708           0 :             move16();
     709             :         }
     710             :         ELSE
     711             :         {
     712        7204 :             stereo_tdm_coder_type = 3; /* mode 4 : YX scheme*/
     713        7204 :             move16();
     714             :         }
     715             :     }
     716             : 
     717        7376 :     SWITCH( stereo_tdm_coder_type )
     718             :     {
     719           1 :         case ( 0 ):
     720             :         {
     721             :             /* Do the upmixing of the first upmixing_delay samples with the old coefficient and formular for YX scheme */
     722           1 :             tdm_upmix_plain_fx( Left_fx, Right_fx, PCh_2_L_fx, SCh_2_R_fx, LR_ratio_mem_fx, tdm_den_ratio_tabl_fx[tdm_last_ratio_idx], 0, upmixing_delay, 1 );
     723             : 
     724             :             /* Switching from YX scheme to SM scheme */
     725           1 :             tdm_upmix_fade_fx( Left_fx, Right_fx, PCh_2_L_fx, SCh_2_R_fx, LR_ratio_mem_fx, tdm_den_ratio_tabl_fx[tdm_last_ratio_idx], LR_ratio_fx, tdm_den_ratio_tabl_fx[tdm_ratio_idx], upmixing_delay, add( upmixing_delay, tdm_n_OVA ), 0 );
     726             : 
     727             :             /* Do the upmixing of the other samples with the new coefficient and formular for SM scheme */
     728           1 :             tdm_upmix_plain_fx( Left_fx, Right_fx, PCh_2_L_fx, SCh_2_R_fx, LR_ratio_fx, tdm_den_ratio_tabl_fx[tdm_ratio_idx], add( upmixing_delay, tdm_n_OVA ), output_frame, 0 );
     729             :         }
     730           1 :             BREAK;
     731          11 :         case ( 1 ):
     732             :         {
     733             :             /* Do the upmixing of the first upmixing_delay samples with the old coefficient and formular for SM scheme */
     734          11 :             tdm_upmix_plain_fx( Left_fx, Right_fx, PCh_2_L_fx, SCh_2_R_fx, LR_ratio_mem_fx, tdm_den_ratio_tabl_fx[tdm_last_ratio_idx], 0, upmixing_delay, 0 );
     735             : 
     736             :             /* Do the upmixing of the other samples with the new coefficient and formular for SM scheme */
     737          11 :             IF( EQ_32( LR_ratio_fx, LR_ratio_mem_fx ) )
     738             :             {
     739          10 :                 tdm_upmix_plain_fx( Left_fx, Right_fx, PCh_2_L_fx, SCh_2_R_fx, LR_ratio_mem_fx, tdm_den_ratio_tabl_fx[tdm_last_ratio_idx], upmixing_delay, output_frame, 0 );
     740             :             }
     741             :             ELSE
     742             :             {
     743           1 :                 tdm_upmix_fade_fx( Left_fx, Right_fx, PCh_2_L_fx, SCh_2_R_fx, LR_ratio_mem_fx, tdm_den_ratio_tabl_fx[tdm_last_ratio_idx], LR_ratio_fx, tdm_den_ratio_tabl_fx[tdm_ratio_idx], upmixing_delay, add( upmixing_delay, tdm_n_OVA ), 1 );
     744           1 :                 tdm_upmix_plain_fx( Left_fx, Right_fx, PCh_2_L_fx, SCh_2_R_fx, LR_ratio_fx, tdm_den_ratio_tabl_fx[tdm_ratio_idx], upmixing_delay + tdm_n_OVA, output_frame, 0 );
     745             :             }
     746             :         }
     747          11 :             BREAK;
     748           0 :         case ( 2 ):
     749             :         {
     750             :             /* Do the upmixing of the first upmixing_delay samples with the old coefficient and formular for SM scheme */
     751           0 :             tdm_upmix_plain_fx( Left_fx, Right_fx, PCh_2_L_fx, SCh_2_R_fx, LR_ratio_mem_fx, tdm_den_ratio_tabl_fx[tdm_last_ratio_idx], 0, upmixing_delay, 0 );
     752             : 
     753             :             /* Switching from SM scheme to YX scheme */
     754           0 :             tdm_upmix_fade_fx( Left_fx, Right_fx, PCh_2_L_fx, SCh_2_R_fx, LR_ratio_mem_fx, tdm_den_ratio_tabl_fx[tdm_last_ratio_idx], LR_ratio_fx, tdm_den_ratio_tabl_fx[tdm_ratio_idx], upmixing_delay, add( upmixing_delay, tdm_n_OVA ), 2 );
     755             : 
     756             :             /* Do the upmixing of the other samples with the new coefficient and formular for YX scheme */
     757           0 :             tdm_upmix_plain_fx( Left_fx, Right_fx, PCh_2_L_fx, SCh_2_R_fx, LR_ratio_fx, tdm_den_ratio_tabl_fx[tdm_ratio_idx], upmixing_delay + tdm_n_OVA, output_frame, 1 );
     758             :         }
     759           0 :             BREAK;
     760        7204 :         case ( 3 ):
     761             :         {
     762             :             /* Do the upmixing of the first upmixing_delay samples with the old coefficient and formular for YX scheme */
     763        7204 :             tdm_upmix_plain_fx( Left_fx, Right_fx, PCh_2_L_fx, SCh_2_R_fx, LR_ratio_mem_fx, tdm_den_ratio_tabl_fx[tdm_last_ratio_idx], 0, upmixing_delay, 1 );
     764             : 
     765             :             /* Do the upmixing of the other samples with the new coefficient and formular for YX scheme */
     766        7204 :             IF( EQ_32( LR_ratio_fx, LR_ratio_mem_fx ) )
     767             :             {
     768        7084 :                 tdm_upmix_plain_fx( Left_fx, Right_fx, PCh_2_L_fx, SCh_2_R_fx, LR_ratio_mem_fx, tdm_den_ratio_tabl_fx[tdm_last_ratio_idx], upmixing_delay, output_frame, 1 );
     769             :             }
     770             :             ELSE
     771             :             {
     772         120 :                 tdm_upmix_fade_fx( Left_fx, Right_fx, PCh_2_L_fx, SCh_2_R_fx, LR_ratio_mem_fx, tdm_den_ratio_tabl_fx[tdm_last_ratio_idx], LR_ratio_fx, tdm_den_ratio_tabl_fx[tdm_ratio_idx], upmixing_delay, add( upmixing_delay, tdm_n_OVA ), 3 );
     773         120 :                 tdm_upmix_plain_fx( Left_fx, Right_fx, PCh_2_L_fx, SCh_2_R_fx, LR_ratio_fx, tdm_den_ratio_tabl_fx[tdm_ratio_idx], upmixing_delay + tdm_n_OVA, output_frame, 1 );
     774             :             }
     775             :         }
     776        7204 :             BREAK;
     777         160 :         default:
     778         160 :             BREAK;
     779             :     }
     780     4733776 :     FOR( i = 0; i < output_frame; i++ )
     781             :     {
     782     4726400 :         PCh_2_L_fx[i] = Left_fx[i]; /* Qx */
     783     4726400 :         move32();
     784     4726400 :         SCh_2_R_fx[i] = Right_fx[i]; /* Qx */
     785     4726400 :         move32();
     786             :     }
     787             : 
     788             :     /* DFT -> TD stereo switching: equalize signal energies */
     789        7376 :     IF( NE_16( hCPE->last_element_mode, IVAS_CPE_TD ) )
     790             :     {
     791             :         Word32 incr_fx, fac_fx;
     792             : 
     793         116 :         IF( hCPE->hStereoDftDmx != NULL )
     794             :         {
     795          44 :             fac_fx = ONE_IN_Q29; /* Q29 */
     796          44 :             move32();
     797             :         }
     798             :         ELSE
     799             :         {
     800          72 :             fac_fx = hCPE->hStereoTCA->targetGain_fx; /* Q29 */
     801          72 :             move32();
     802             :         }
     803             : 
     804             : 
     805         116 :         IF( LT_32( fac_fx, ONE_IN_Q29 ) )
     806             :         {
     807           0 :             Word32 step = 0;
     808           0 :             move32();
     809           0 :             SWITCH( output_Fs )
     810             :             {
     811           0 :                 case ( 16000 ):
     812           0 :                     step = 15339168; /* Q31 */
     813           0 :                     move32();
     814           0 :                     BREAK;
     815           0 :                 case ( 32000 ):
     816           0 :                     step = 7669584; /* Q31 */
     817           0 :                     move32();
     818           0 :                     BREAK;
     819           0 :                 case ( 48000 ):
     820           0 :                     step = 5113056; /* Q31 */
     821           0 :                     move32();
     822           0 :                     BREAK;
     823           0 :                 default:
     824           0 :                     assert( 0 );
     825             :                     BREAK;
     826             :             }
     827           0 :             incr_fx = Mpy_32_32( L_sub( ONE_IN_Q29, fac_fx ), step ); /* Q29 */
     828             : 
     829           0 :             FOR( i = 0; i < NS2SA_FX2( output_Fs, ACELP_LOOK_NS ); i++ )
     830             :             {
     831           0 :                 PCh_2_L_fx[i] = L_shl_sat( Mpy_32_32( PCh_2_L_fx[i], fac_fx ), 2 ); /* Qx */
     832           0 :                 move32();
     833             : 
     834           0 :                 fac_fx = L_add( incr_fx, fac_fx ); /* Q29 */
     835             :             }
     836             :         }
     837             :     }
     838        7376 :     return;
     839             : }

Generated by: LCOV version 1.14