LCOV - code coverage report
Current view: top level - lib_dec - ivas_stereo_switching_dec_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main -- dec/rend @ 633e3f2e309758d10805ef21e0436356fe719b7a Lines: 1132 1241 91.2 %
Date: 2025-08-23 01:22:27 Functions: 12 12 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 "cnst.h"
      36             : #include "rom_com.h"
      37             : #include "prot_fx.h"
      38             : #include "ivas_prot_fx.h"
      39             : #include "ivas_rom_com.h"
      40             : #include "assert.h"
      41             : #include "wmc_auto.h"
      42             : #include <math.h>
      43             : #include "ivas_prot_fx.h"
      44             : #include "ivas_rom_com_fx.h"
      45             : 
      46             : 
      47             : /*-------------------------------------------------------------------*
      48             :  * Local constants
      49             :  *-------------------------------------------------------------------*/
      50             : 
      51             : #define DFT2TD_CORR_THRESH_FX 1932735283
      52             : 
      53             : 
      54             : /*-------------------------------------------------------------------*
      55             :  * Function allocate_CoreCoder_TCX()
      56             :  *
      57             :  * Allocate CoreCoder TCX modules
      58             :  *-------------------------------------------------------------------*/
      59             : 
      60         725 : static ivas_error allocate_CoreCoder_TCX_fx(
      61             :     DEC_CORE_HANDLE st /* i/o: Core decoder state structure     */
      62             : )
      63             : {
      64         725 :     IF( st->hTcxDec == NULL )
      65             :     {
      66         725 :         IF( ( st->hTcxDec = (TCX_DEC_HANDLE) malloc( sizeof( TCX_DEC_DATA ) ) ) == NULL )
      67             :         {
      68           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for hTcxDec" ) );
      69             :         }
      70             : 
      71         725 :         reset_tcx_overl_buf_fx( st->hTcxDec );
      72             :         // st->hTcxDec->CngLevelBackgroundTrace_bfi = 0; //initializing to avoid garbage overflow;
      73         725 :         set16_fx( st->hTcxDec->syn_OverlFB, 0, L_FRAME48k / 2 );
      74         725 :         st->hTcxDec->Q_syn_OverlFB = 0;
      75         725 :         move16();
      76         725 :         set16_fx( st->hTcxDec->old_synth, 0, OLD_SYNTH_INTERNAL_DEC );
      77         725 :         st->hTcxDec->q_old_synth = 0;
      78         725 :         move16();
      79         725 :         set16_fx( st->hTcxDec->synth_history_fx, 0, L_PROT48k + L_FRAME48k );
      80             :     }
      81             : 
      82         725 :     IF( st->hTcxCfg == NULL )
      83             :     {
      84         725 :         IF( ( st->hTcxCfg = (TCX_CONFIG_HANDLE) malloc( sizeof( TCX_config ) ) ) == NULL )
      85             :         {
      86           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for hTcxCfg" ) );
      87             :         }
      88             :     }
      89             : 
      90             :     /* allocated TCX-LTP structure for second channel */
      91         725 :     IF( st->hTcxLtpDec == NULL )
      92             :     {
      93         725 :         IF( ( st->hTcxLtpDec = (TCX_LTP_DEC_HANDLE) malloc( sizeof( TCX_LTP_DEC_DATA ) ) ) == NULL )
      94             :         {
      95           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for TCX-LTP handle\n" ) );
      96             :         }
      97             : 
      98         725 :         tcxltp_dec_init_fx( st->hTcxLtpDec, 0, st->last_codec_mode, st->element_mode, st->pit_max, st->sr_core );
      99             :     }
     100             : 
     101             :     /* allocate HQ structure */
     102         725 :     IF( st->hHQ_core == NULL )
     103             :     {
     104         725 :         IF( ( st->hHQ_core = (HQ_DEC_HANDLE) malloc( sizeof( HQ_DEC_DATA ) ) ) == NULL )
     105             :         {
     106           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for HQ core\n" ) );
     107             :         }
     108             : 
     109         725 :         HQ_core_dec_init_fx( st->hHQ_core );
     110             :     }
     111             : 
     112         725 :     IF( st->hIGFDec == NULL )
     113             :     {
     114         725 :         IF( ( st->hIGFDec = (IGF_DEC_INSTANCE_HANDLE) malloc( sizeof( IGFDEC_INSTANCE ) ) ) == NULL )
     115             :         {
     116           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for IGF\n" ) );
     117             :         }
     118             : 
     119         725 :         st->igf = 0;
     120         725 :         move16();
     121         725 :         init_igf_dec( st->hIGFDec );
     122             :     }
     123             : 
     124         725 :     IF( st->hTonalMDCTConc == NULL )
     125             :     {
     126         725 :         IF( ( st->hTonalMDCTConc = (TonalMDCTConcealPtr) malloc( sizeof( TonalMDCTConceal_INSTANCE ) ) ) == NULL )
     127             :         {
     128           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for TonalMDCTConcealment\n" ) );
     129             :         }
     130             :     }
     131             : 
     132         725 :     st->last_con_tcx = 0;
     133         725 :     move16();
     134         725 :     st->hTonalMDCTConc->nSamples = 0;
     135         725 :     move16();
     136             : 
     137         725 :     return IVAS_ERR_OK;
     138             : }
     139             : 
     140             : 
     141             : /*-------------------------------------------------------------------*
     142             :  * Function allocate_CoreCoder()
     143             :  *
     144             :  * Allocate CoreCoder modules
     145             :  *-------------------------------------------------------------------*/
     146             : 
     147         764 : static ivas_error allocate_CoreCoder_fx(
     148             :     DEC_CORE_HANDLE st /* i/o: Core decoder state structure     */
     149             : )
     150             : {
     151             :     ivas_error error;
     152             : 
     153         764 :     error = IVAS_ERR_OK;
     154             : 
     155         764 :     IF( st->hGSCDec == NULL )
     156             :     {
     157         764 :         IF( ( st->hGSCDec = (GSC_DEC_HANDLE) malloc( sizeof( GSC_DEC_DATA ) ) ) == NULL )
     158             :         {
     159           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for GSC\n" ) );
     160             :         }
     161             : 
     162         764 :         GSC_dec_init_ivas_fx( st->hGSCDec );
     163             :     }
     164             : 
     165         764 :     IF( st->hPFstat == NULL )
     166             :     {
     167         764 :         IF( ( st->hPFstat = (PFSTAT_HANDLE) malloc( sizeof( PFSTAT ) ) ) == NULL )
     168             :         {
     169           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for NB/formant postflter\n" ) );
     170             :         }
     171             : 
     172         764 :         Init_post_filter_fx( st->hPFstat );
     173         764 :         st->psf_lp_noise_fx = 0;
     174         764 :         move16();
     175             :     }
     176             : 
     177         764 :     IF( st->hMusicPF == NULL )
     178             :     {
     179         764 :         IF( ( st->hMusicPF = (MUSIC_POSTFILT_HANDLE) malloc( sizeof( MUSIC_POSTFILT_DATA ) ) ) == NULL )
     180             :         {
     181           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LD music postflter\n" ) );
     182             :         }
     183             : 
     184         764 :         music_postfilt_init( st->hMusicPF );
     185             :     }
     186             : 
     187         764 :     IF( st->hBPF == NULL )
     188             :     {
     189         764 :         IF( ( st->hBPF = (BPF_DEC_HANDLE) malloc( sizeof( BPF_DEC_DATA ) ) ) == NULL )
     190             :         {
     191           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for BPF\n" ) );
     192             :         }
     193             : 
     194         764 :         bass_psfilter_init_fx( st->hBPF );
     195             :     }
     196             : 
     197         764 :     IF( st->hBWE_zero == NULL )
     198             :     {
     199         764 :         IF( ( st->hBWE_zero = (ZERO_BWE_DEC_HANDLE) malloc( sizeof( ZERO_BWE_DEC_DATA ) ) ) == NULL )
     200             :         {
     201           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for zero BWE\n" ) );
     202             :         }
     203             : 
     204         764 :         hf_synth_init_fx( st->hBWE_zero );
     205         764 :         set16_fx( st->hBWE_zero->mem_hp400_fx, 0, 6 );
     206             :     }
     207             : 
     208         764 :     IF( st->cldfbAna == NULL )
     209             :     {
     210             :         /* open analysis for max. sampling rate 48kHz */
     211          51 :         IF( NE_32( ( error = openCldfb_ivas_fx( &st->cldfbAna, CLDFB_ANALYSIS, 48000, CLDFB_PROTOTYPE_1_25MS, DEC ) ), IVAS_ERR_OK ) )
     212             :         {
     213           0 :             return error;
     214             :         }
     215             :     }
     216             : 
     217         764 :     IF( st->cldfbBPF == NULL )
     218             :     {
     219             :         /* open analysis BPF for max. internal sampling rate 16kHz */
     220          51 :         IF( NE_32( ( error = openCldfb_ivas_fx( &st->cldfbBPF, CLDFB_ANALYSIS, 16000, CLDFB_PROTOTYPE_1_25MS, DEC ) ), IVAS_ERR_OK ) )
     221             :         {
     222           0 :             return error;
     223             :         }
     224             :     }
     225             : 
     226         764 :     return error;
     227             : }
     228             : 
     229             : 
     230             : /*-------------------------------------------------------------------*
     231             :  * Function deallocate_CoreCoder_TCX()
     232             :  *
     233             :  * Deallocate CoreCoder TCX modules
     234             :  *-------------------------------------------------------------------*/
     235             : 
     236         810 : static void deallocate_CoreCoder_TCX(
     237             :     DEC_CORE_HANDLE st /* i/o: Core decoder state structure     */
     238             : )
     239             : {
     240         810 :     IF( st->hTcxDec != NULL )
     241             :     {
     242         708 :         free( st->hTcxDec );
     243         708 :         st->hTcxDec = NULL;
     244             :     }
     245             : 
     246         810 :     IF( st->hTcxCfg != NULL )
     247             :     {
     248         708 :         free( st->hTcxCfg );
     249         708 :         st->hTcxCfg = NULL;
     250             :     }
     251             : 
     252         810 :     IF( st->hIGFDec != NULL )
     253             :     {
     254         708 :         free( st->hIGFDec );
     255         708 :         st->hIGFDec = NULL;
     256             :     }
     257             : 
     258         810 :     IF( st->hTonalMDCTConc != NULL )
     259             :     {
     260         708 :         free( st->hTonalMDCTConc );
     261         708 :         st->hTonalMDCTConc = NULL;
     262             :     }
     263             : 
     264         810 :     return;
     265             : }
     266             : 
     267             : 
     268             : /*-------------------------------------------------------------------*
     269             :  * Function deallocate_CoreCoder()
     270             :  *
     271             :  * Deallocate CoreCoder modules
     272             :  *-------------------------------------------------------------------*/
     273             : 
     274        2204 : static void deallocate_CoreCoder_fx(
     275             :     DEC_CORE_HANDLE st /* i/o: Core decoder state structure     */
     276             : )
     277             : {
     278        2204 :     IF( st->hGSCDec != NULL )
     279             :     {
     280         781 :         free( st->hGSCDec );
     281         781 :         st->hGSCDec = NULL;
     282             :     }
     283             : 
     284        2204 :     IF( st->hPFstat != NULL )
     285             :     {
     286         781 :         free( st->hPFstat );
     287         781 :         st->hPFstat = NULL;
     288             :     }
     289             : 
     290        2204 :     IF( st->hMusicPF != NULL )
     291             :     {
     292         781 :         free( st->hMusicPF );
     293         781 :         st->hMusicPF = NULL;
     294             :     }
     295             : 
     296        2204 :     IF( st->hBPF != NULL )
     297             :     {
     298         781 :         free( st->hBPF );
     299         781 :         st->hBPF = NULL;
     300             :     }
     301             : 
     302        2204 :     IF( st->hBWE_zero != NULL )
     303             :     {
     304         781 :         free( st->hBWE_zero );
     305         781 :         st->hBWE_zero = NULL;
     306             :     }
     307             : 
     308             :     /* CLDFB BPF & resampling tools */
     309        2204 :     IF( NE_16( st->element_mode, IVAS_CPE_MDCT ) )
     310             :     {
     311         754 :         deleteCldfb_ivas_fx( &st->cldfbAna ); /* delete analysis at max. sampling rate 48kHz */
     312         754 :         deleteCldfb_ivas_fx( &st->cldfbBPF ); /* delete analysis BPF at max. internal sampling rate 16kHz */
     313             :     }
     314             : 
     315        2204 :     IF( NE_16( st->element_mode, IVAS_CPE_MDCT ) )
     316             :     {
     317         754 :         deallocate_CoreCoder_TCX( st );
     318             :     }
     319             : 
     320        2204 :     return;
     321             : }
     322             : 
     323             : /*-------------------------------------------------------------------*
     324             :  * Function cpy_tcx_ltp_data()
     325             :  *
     326             :  * In case of MDCT<->DFT Stereo switching, copy TCX-LTP data of the right
     327             :  * channel to the correct structure in the new mode
     328             :  *-------------------------------------------------------------------*/
     329             : 
     330        2146 : static void cpy_tcx_ltp_data_fx(
     331             :     TCX_LTP_DEC_HANDLE hTcxLtpDecOld, /* i  : TCX-LTP structure to copy from   */
     332             :     TCX_LTP_DEC_HANDLE hTcxLtpDecNew, /* o  : TCX-LTP structure to copy to     */
     333             :     const Word32 output_Fs            /* i  : output sampling rate           Q0*/
     334             : )
     335             : {
     336             :     Word16 sz;
     337        2146 :     hTcxLtpDecNew->tcxltp_pitch_int_post_prev = hTcxLtpDecOld->tcxltp_pitch_int_post_prev; /* Q0 */
     338        2146 :     move16();
     339        2146 :     hTcxLtpDecNew->tcxltp_pitch_fr_post_prev = hTcxLtpDecOld->tcxltp_pitch_fr_post_prev; /* Q0 */
     340        2146 :     move16();
     341        2146 :     hTcxLtpDecNew->tcxltp_gain_post_prev = hTcxLtpDecOld->tcxltp_gain_post_prev; /* Qx */
     342        2146 :     move16();
     343        2146 :     hTcxLtpDecNew->tcxltp_filt_idx_prev = hTcxLtpDecOld->tcxltp_filt_idx_prev; /* Q0 */
     344        2146 :     move16();
     345             :     /* (int16_t) ( ( TCXLTP_MAX_DELAY * output_Fs ) / 48000 ) */
     346        2146 :     sz = extract_l( Mpy_32_32_r( TCXLTP_MAX_DELAY * output_Fs, 44739 /* 1 / 48000 in Q31 */ ) ); /* Q0 */
     347        2146 :     Copy( hTcxLtpDecOld->tcxltp_mem_in, hTcxLtpDecNew->tcxltp_mem_in, sz );                      // TODO: One of these will be removed later      /* exp(exp_tcxltp_mem_in) */
     348        2146 :     Copy32( hTcxLtpDecOld->tcxltp_mem_in_32, hTcxLtpDecNew->tcxltp_mem_in_32, sz );              /* exp(exp_tcxltp_mem_in) */
     349        2146 :     hTcxLtpDecNew->exp_tcxltp_mem_in = hTcxLtpDecOld->exp_tcxltp_mem_in;                         /* exp(exp_tcxltp_mem_in) */
     350        2146 :     move16();
     351             :     /* (int16_t) ( ( L_FRAME48k * output_Fs ) / 48000 ) */
     352        2146 :     sz = extract_l( Mpy_32_32_r( L_FRAME48k * output_Fs, 44739 /* 1 / 48000 in Q31 */ ) ); /* Q0 */
     353        2146 :     Copy( hTcxLtpDecOld->tcxltp_mem_out, hTcxLtpDecNew->tcxltp_mem_out, sz );              // TODO: One of these will be removed later    /* exp(exp_tcxltp_mem_in) */
     354        2146 :     Copy32( hTcxLtpDecOld->tcxltp_mem_out_32, hTcxLtpDecNew->tcxltp_mem_out_32, sz );      /* exp(exp_tcxltp_mem_in) */
     355        2146 :     hTcxLtpDecNew->exp_tcxltp_mem_out = hTcxLtpDecOld->exp_tcxltp_mem_out;                 /* exp(exp_tcxltp_mem_in) */
     356        2146 :     move16();
     357             : 
     358        2146 :     return;
     359             : }
     360             : 
     361             : 
     362             : /*-------------------------------------------------------------------*
     363             :  * Function stereo_memory_dec()
     364             :  *
     365             :  * Dynamically allocate/deallocate data structures depending on the actual CPE mode
     366             :  *-------------------------------------------------------------------*/
     367             : 
     368      410722 : ivas_error stereo_memory_dec_fx(
     369             :     const Word32 ivas_total_brate, /* i  : IVAS total bitrate                           Q0*/
     370             :     CPE_DEC_HANDLE hCPE,           /* i  : CPE decoder structure                          */
     371             :     const Word16 nb_bits_metadata, /* i  : number of metadata bits                      Q0*/
     372             :     const Word32 output_Fs,        /* i  : output sampling rate                         Q0*/
     373             :     const IVAS_FORMAT ivas_format, /* i  : IVAS format                                            */
     374             :     const MC_MODE mc_mode,         /* i  : MC mode                                                        */
     375             :     const Word16 nchan_transport   /* i  : number of transport channels         Q0*/
     376             : )
     377             : {
     378             :     DEC_CORE_HANDLE st;
     379             :     Word16 i, n, delay_comp_DFT;
     380             :     ivas_error error;
     381             :     Word16 div_m, div_e, tmp16;
     382             : 
     383      410722 :     error = IVAS_ERR_OK;
     384      410722 :     move32();
     385             : 
     386      410722 :     assert( GE_16( hCPE->last_element_mode, IVAS_CPE_DFT ) && "Switching from SCE to CPE is not a valid configuration!" );
     387             : 
     388             : 
     389      410722 :     hCPE->hCoreCoder[0]->element_mode = hCPE->element_mode; /* Q0 */
     390      410722 :     move16();
     391      410722 :     hCPE->hCoreCoder[1]->element_mode = hCPE->element_mode; /* Q0 */
     392      410722 :     move16();
     393             : 
     394             :     /*--------------------------------------------------------------*
     395             :      * stereo switching (using parameters that will be freed)
     396             :      *---------------------------------------------------------------*/
     397             : 
     398             :     /* handling of DFT->TD switching */
     399      410722 :     test();
     400      410722 :     test();
     401      410722 :     IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) && ( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) || EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) ) )
     402             :     {
     403         771 :         delay_comp_DFT = NS2SA_FX2( output_Fs, IVAS_DEC_DELAY_NS - STEREO_DFT32MS_OVL_NS ); /* Q0 */
     404         771 :         move16();
     405             : 
     406             :         /* redressing of the DFT OLA part */
     407        2313 :         FOR( n = 0; n < CPE_CHANNELS; n++ )
     408             :         {
     409      195462 :             FOR( i = delay_comp_DFT; i < hCPE->hStereoDft->dft32ms_ovl; i++ )
     410             :             {
     411      193920 :                 div_m = BASOP_Util_Divide3216_Scale( hCPE->output_mem_fx[n][i - delay_comp_DFT], hCPE->hStereoDft->win32ms_fx[STEREO_DFT32MS_STEP * ( hCPE->hStereoDft->dft32ms_ovl - 1 + delay_comp_DFT - i )], &div_e );
     412      193920 :                 hCPE->output_mem_fx[n][i - delay_comp_DFT] = L_shl( div_m, add( div_e, 16 ) ); /* div_e + Q16 */
     413      193920 :                 move32();
     414             :                 // hCPE->output_mem_fx[n][sub(i, delay_comp_DFT)] = hCPE->hStereoDft->win32ms_fx[i_mult(STEREO_DFT32MS_STEP, ( sub(add(sub(hCPE->hStereoDft->dft32ms_ovl, 1), delay_comp_DFT), i) ))];
     415             :             }
     416             :         }
     417             : 
     418         771 :         IF( hCPE->hCoreCoder[0]->last_core != ACELP_CORE )
     419             :         {
     420         201 :             Copy32( hCPE->hStereoDft->buff_LBTCX_mem_fx, hCPE->input_mem_LB_fx[0], NS2SA_FX2( i_mult( hCPE->hCoreCoder[0]->last_L_frame, FRAMES_PER_SEC ), STEREO_DFT32MS_OVL_NS ) ); /* Q11 */
     421             :         }
     422             :     }
     423             : 
     424      410722 :     test();
     425      410722 :     IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) && EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
     426             :     {
     427         700 :         assert( hCPE->hCoreCoder[0]->hHQ_core->Q_old_out_fx32 == hCPE->hCoreCoder[1]->hHQ_core->Q_old_out_fx32 );
     428         700 :         v_add_32( hCPE->hCoreCoder[0]->hHQ_core->old_out_fx32, hCPE->hCoreCoder[1]->hHQ_core->old_out_fx32, hCPE->hCoreCoder[0]->hHQ_core->old_out_fx32, extract_l( Mpy_32_16_1( output_Fs, INV_FRAME_PER_SEC_Q15 ) ) ); /* exp(exp_old_out) */
     429         700 :         v_multc_fixed_16( hCPE->hCoreCoder[0]->hHQ_core->old_out_fx32, 16384 /* 0.5 in Q15 */, hCPE->hCoreCoder[0]->hHQ_core->old_out_fx32, extract_l( Mpy_32_16_1( output_Fs, INV_FRAME_PER_SEC_Q15 ) ) );              /* exp(exp_old_out) */
     430             : 
     431         700 :         v_add_32( hCPE->hCoreCoder[0]->hHQ_core->old_out_LB_fx32, hCPE->hCoreCoder[1]->hHQ_core->old_out_LB_fx32, hCPE->hCoreCoder[0]->hHQ_core->old_out_LB_fx32, L_FRAME32k ); /* q_old_outLB_fx */
     432         700 :         v_multc_fixed_16( hCPE->hCoreCoder[0]->hHQ_core->old_out_LB_fx32, 16384 /* 0.5 in Q15 */, hCPE->hCoreCoder[0]->hHQ_core->old_out_LB_fx32, L_FRAME32k );                 /* exp(exp_old_out) */
     433             :     }
     434             : 
     435             :     /*--------------------------------------------------------------*
     436             :      * allocate/deallocate data structures
     437             :      *---------------------------------------------------------------*/
     438             : 
     439      410722 :     IF( NE_16( hCPE->element_mode, hCPE->last_element_mode ) )
     440             :     {
     441             :         /*--------------------------------------------------------------*
     442             :          * switching CPE mode to DFT stereo
     443             :          *---------------------------------------------------------------*/
     444             : 
     445        1535 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
     446             :         {
     447             :             /* deallocate data structure of the previous CPE mode */
     448         754 :             IF( hCPE->hStereoTD != NULL )
     449             :             {
     450          54 :                 free( hCPE->hStereoTD );
     451          54 :                 hCPE->hStereoTD = NULL;
     452             :             }
     453             : 
     454         754 :             IF( hCPE->hStereoMdct != NULL )
     455             :             {
     456         700 :                 free( hCPE->hStereoMdct );
     457         700 :                 hCPE->hStereoMdct = NULL;
     458             :             }
     459             : 
     460             :             /* deallocate secondary channel */
     461         754 :             deallocate_CoreCoder_fx( hCPE->hCoreCoder[1] );
     462             : 
     463             :             /* allocate DFT stereo data structure */
     464         754 :             IF( NE_32( ( error = stereo_dft_dec_create_fx( &( hCPE->hStereoDft ), hCPE->element_brate, output_Fs, 0, nchan_transport ) ), IVAS_ERR_OK ) )
     465             :             {
     466           0 :                 return error;
     467             :             }
     468             : 
     469         754 :             IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
     470             :             {
     471         700 :                 cpy_tcx_ltp_data_fx( hCPE->hCoreCoder[1]->hTcxLtpDec, hCPE->hStereoDft->hTcxLtpDec, output_Fs );
     472         700 :                 deleteFdCngDec_fx( &hCPE->hCoreCoder[1]->hFdCngDec );
     473             :             }
     474             : 
     475             :             /* memory update - needed in TD stereo, TCX/HQ frame -> DFT stereo, ACELP frame switching */
     476         754 :             Copy32( hCPE->input_mem_LB_fx[0], hCPE->hStereoDft->buff_LBTCX_mem_fx, NS2SA_FX2( s_min( i_mult( hCPE->hCoreCoder[0]->last_L_frame, FRAMES_PER_SEC ), 16000 ), STEREO_DFT32MS_OVL_NS ) ); /* Q11 */
     477             : 
     478             :             /* allocate ICBWE structure */
     479         754 :             IF( hCPE->hStereoICBWE == NULL )
     480             :             {
     481         722 :                 IF( ( hCPE->hStereoICBWE = (STEREO_ICBWE_DEC_HANDLE) malloc( sizeof( STEREO_ICBWE_DEC_DATA ) ) ) == NULL )
     482             :                 {
     483           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE \n" ) );
     484             :                 }
     485             : 
     486         722 :                 stereo_icBWE_init_dec_fx( hCPE->hStereoICBWE );
     487             :             }
     488             : 
     489             :             /* allocate HQ core */
     490         754 :             st = hCPE->hCoreCoder[0];
     491         754 :             IF( st->hHQ_core == NULL )
     492             :             {
     493           0 :                 IF( ( st->hHQ_core = (HQ_DEC_HANDLE) malloc( sizeof( HQ_DEC_DATA ) ) ) == NULL )
     494             :                 {
     495           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HQ core\n" ) );
     496             :                 }
     497             : 
     498           0 :                 HQ_core_dec_init_fx( st->hHQ_core );
     499             :             }
     500             : 
     501             :             /* allocate TD CNG handle */
     502         754 :             IF( EQ_16( st->idchan, 0 ) && st->hTdCngDec == NULL )
     503             :             {
     504           3 :                 IF( ( st->hTdCngDec = (TD_CNG_DEC_HANDLE) malloc( sizeof( TD_CNG_DEC_DATA ) ) ) == NULL )
     505             :                 {
     506           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DTX/TD CNG\n" ) );
     507             :                 }
     508             : 
     509           3 :                 td_cng_dec_init_fx( st );
     510             :             }
     511             :         }
     512             : 
     513             :         /*--------------------------------------------------------------*
     514             :          * switching CPE mode to TD stereo
     515             :          *---------------------------------------------------------------*/
     516             : 
     517        1535 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
     518             :         {
     519             :             /* deallocate data structure of the previous CPE mode */
     520          56 :             IF( hCPE->hStereoDft != NULL )
     521             :             {
     522          48 :                 stereo_dft_dec_destroy_fx( &( hCPE->hStereoDft ) );
     523          48 :                 hCPE->hStereoDft = NULL;
     524             :             }
     525             : 
     526          56 :             IF( hCPE->hStereoMdct != NULL )
     527             :             {
     528           8 :                 free( hCPE->hStereoMdct );
     529           8 :                 hCPE->hStereoMdct = NULL;
     530             :             }
     531             : 
     532             :             /* deallocated TCX/IGF structures for second channel */
     533          56 :             deallocate_CoreCoder_TCX( hCPE->hCoreCoder[1] );
     534             : 
     535          56 :             IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
     536             :             {
     537           8 :                 deleteFdCngDec_fx( &hCPE->hCoreCoder[1]->hFdCngDec );
     538             :             }
     539             : 
     540             :             /* allocate TD stereo data structure */
     541          56 :             IF( hCPE->hStereoTD != NULL )
     542             :             {
     543           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: TD Stereo memory already allocated\n" );
     544             :             }
     545             : 
     546          56 :             IF( ( hCPE->hStereoTD = (STEREO_TD_DEC_DATA_HANDLE) malloc( sizeof( STEREO_TD_DEC_DATA ) ) ) == NULL )
     547             :             {
     548           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD Stereo\n" ) );
     549             :             }
     550             : 
     551          56 :             stereo_td_init_dec_fx( hCPE->hStereoTD, hCPE->last_element_mode );
     552             : 
     553             :             /* allocate CoreCoder secondary channel */
     554          56 :             IF( NE_32( ( error = allocate_CoreCoder_fx( hCPE->hCoreCoder[1] ) ), IVAS_ERR_OK ) )
     555             :             {
     556           0 :                 return error;
     557             :             }
     558             :         }
     559             : 
     560             :         /*--------------------------------------------------------------*
     561             :          * allocate DFT/TD stereo structures after MDCT stereo frame
     562             :          *---------------------------------------------------------------*/
     563             : 
     564        1535 :         test();
     565        1535 :         test();
     566        1535 :         IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) && ( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) || EQ_16( hCPE->element_mode, IVAS_CPE_TD ) ) )
     567             :         {
     568             :             /* deallocated TCX-LTP for second channel */
     569         708 :             st = hCPE->hCoreCoder[1];
     570         708 :             IF( st->hTcxLtpDec != NULL )
     571             :             {
     572         708 :                 free( st->hTcxLtpDec );
     573         708 :                 st->hTcxLtpDec = NULL;
     574             :             }
     575             : 
     576         708 :             IF( EQ_16( st->element_mode, IVAS_CPE_TD ) )
     577             :             {
     578             :                 /* re-use an existing buffer for MDCT->TD stereo switching */
     579             :                 Word16 nZeros, len;
     580           8 :                 nZeros = extract_l( NS2SA_FX2( st->output_Fs, N_ZERO_MDCT_NS ) ); /* Q0 */
     581           8 :                 move16();
     582           8 :                 len = NS2SA_FX2( st->output_Fs, 3000000 ); /* Q0 */
     583           8 :                 move16();
     584           8 :                 Copy32( st->hHQ_core->old_out_fx32 + nZeros, hCPE->output_mem_fx[1], len ); /* exp(exp_old_out) */
     585             :             }
     586             : 
     587             :             /* deallocated HQ-core for second channel */
     588         708 :             IF( st->hHQ_core != NULL )
     589             :             {
     590         708 :                 free( st->hHQ_core );
     591         708 :                 st->hHQ_core = NULL;
     592             :             }
     593             : 
     594             :             /* allocate DFT stereo mono DMX data structure */
     595         708 :             test();
     596         708 :             IF( EQ_16( hCPE->nchan_out, 1 ) && hCPE->hStereoDftDmx == NULL )
     597             :             {
     598         411 :                 IF( ( hCPE->hStereoDftDmx = (STEREO_DFT_DMX_DATA_HANDLE) malloc( sizeof( STEREO_DFT_DMX_DATA ) ) ) == NULL )
     599             :                 {
     600           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo DFT mono output\n" ) );
     601             :                 }
     602             : 
     603         411 :                 stereo_dft_dmx_out_reset_fx( hCPE->hStereoDftDmx );
     604             :             }
     605             : 
     606             :             /* allocate TCA data structure */
     607         708 :             test();
     608         708 :             IF( NE_16( hCPE->nchan_out, 1 ) && hCPE->hStereoTCA == NULL )
     609             :             {
     610         275 :                 IF( ( hCPE->hStereoTCA = (STEREO_TCA_DEC_HANDLE) malloc( sizeof( STEREO_TCA_DEC_DATA ) ) ) == NULL )
     611             :                 {
     612           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
     613             :                 }
     614             : 
     615         275 :                 stereo_tca_init_dec_fx( hCPE->hStereoTCA );
     616             :             }
     617             : 
     618         708 :             st = hCPE->hCoreCoder[0];
     619             : 
     620             :             /* allocate primary channel substructures */
     621         708 :             IF( NE_32( ( error = allocate_CoreCoder_fx( st ) ), IVAS_ERR_OK ) )
     622             :             {
     623           0 :                 return error;
     624             :             }
     625             : 
     626             :             /* allocate BWEs for primary channel */
     627         708 :             IF( st->hBWE_TD == NULL )
     628             :             {
     629           3 :                 IF( ( st->hBWE_TD = (TD_BWE_DEC_HANDLE) malloc( sizeof( TD_BWE_DEC_DATA ) ) ) == NULL )
     630             :                 {
     631           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD BWE\n" ) );
     632             :                 }
     633             : 
     634           3 :                 td_bwe_dec_init_fx( st->hBWE_TD, -1, st->output_Fs );
     635             : 
     636           3 :                 st->prev_Q_bwe_exc = 31;
     637           3 :                 move16();
     638             : 
     639           3 :                 IF( ( st->hBWE_FD = (FD_BWE_DEC_HANDLE) malloc( sizeof( FD_BWE_DEC_DATA ) ) ) == NULL )
     640             :                 {
     641           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FD BWE\n" ) );
     642             :                 }
     643             : 
     644           3 :                 fd_bwe_dec_init_fx( st->hBWE_FD );
     645             :             }
     646             : 
     647             :             /* Allocated FD_CNG instance for primary channel*/
     648         708 :             IF( st->hFdCngDec == NULL )
     649             :             {
     650             :                 /* Create FD_CNG instance */
     651           0 :                 IF( NE_32( ( error = createFdCngDec_fx( &st->hFdCngDec ) ), IVAS_ERR_OK ) )
     652             :                 {
     653           0 :                     return error;
     654             :                 }
     655             : 
     656           0 :                 initFdCngDec_ivas_fx( st, st->cldfbSyn->scale );
     657             : 
     658           0 :                 configureFdCngDec_ivas_fx( st->hFdCngDec, st->bwidth, st->total_brate, st->L_frame, st->last_L_frame, st->element_mode );
     659             :             }
     660             : 
     661             :             /* allocate stereo CNG structure */
     662         708 :             IF( hCPE->hStereoCng == NULL )
     663             :             {
     664         708 :                 IF( ( hCPE->hStereoCng = (STEREO_CNG_DEC_HANDLE) malloc( sizeof( STEREO_CNG_DEC ) ) ) == NULL )
     665             :                 {
     666           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo CNG\n" ) );
     667             :                 }
     668         708 :                 stereo_cng_init_dec_fx( hCPE->hStereoCng, &st->hFdCngDec->hFdCngCom->frameSize );
     669             :             }
     670             :         }
     671             : 
     672             :         /*--------------------------------------------------------------*
     673             :          * switching CPE mode to MDCT stereo
     674             :          *---------------------------------------------------------------*/
     675             : 
     676        1535 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
     677             :         {
     678             :             Word32 tmpF_buff[L_FRAME16k / 2];
     679             :             TCX_LTP_DEC_DATA tcxLtpTmp;
     680             : 
     681         725 :             IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_TD ) )
     682             :             {
     683           2 :                 Copy32( hCPE->hStereoTD->TCX_old_syn_Overl_fx, tmpF_buff, L_FRAME16k / 2 ); /* Q11 */
     684             :             }
     685             : 
     686         725 :             IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
     687             :             {
     688         723 :                 cpy_tcx_ltp_data_fx( hCPE->hStereoDft->hTcxLtpDec, &tcxLtpTmp, output_Fs );
     689             :             }
     690             : 
     691             :             /* deallocate data structure of the previous CPE mode */
     692         725 :             IF( hCPE->hStereoDft != NULL )
     693             :             {
     694         723 :                 stereo_dft_dec_destroy_fx( &( hCPE->hStereoDft ) );
     695         723 :                 hCPE->hStereoDft = NULL;
     696             :             }
     697             : 
     698         725 :             IF( hCPE->hStereoTD != NULL )
     699             :             {
     700           2 :                 free( hCPE->hStereoTD );
     701           2 :                 hCPE->hStereoTD = NULL;
     702             :             }
     703             : 
     704         725 :             IF( hCPE->hStereoDftDmx != NULL )
     705             :             {
     706         434 :                 free( hCPE->hStereoDftDmx );
     707         434 :                 hCPE->hStereoDftDmx = NULL;
     708             :             }
     709             : 
     710         725 :             IF( hCPE->hStereoICBWE != NULL )
     711             :             {
     712         710 :                 free( hCPE->hStereoICBWE );
     713         710 :                 hCPE->hStereoICBWE = NULL;
     714             :             }
     715             : 
     716             :             /* de-allocate stereo CNG structure */
     717         725 :             IF( hCPE->hStereoCng != NULL )
     718             :             {
     719         725 :                 free( hCPE->hStereoCng );
     720         725 :                 hCPE->hStereoCng = NULL;
     721             :             }
     722             : 
     723        2175 :             FOR( i = 0; i < CPE_CHANNELS; i++ )
     724             :             {
     725        1450 :                 st = hCPE->hCoreCoder[i];
     726        1450 :                 st->element_mode = hCPE->element_mode; /* Q0 */
     727        1450 :                 move16();
     728             : 
     729             :                 /* deallocate core-decoder substructures */
     730        1450 :                 deallocate_CoreCoder_fx( st );
     731             : 
     732        1450 :                 st->first_CNG = 0;
     733        1450 :                 move16();
     734             :             }
     735             : 
     736             :             /* allocate CLDFB structures for second channel */
     737         725 :             st = hCPE->hCoreCoder[1];
     738             : 
     739         725 :             IF( st->cldfbAna == NULL )
     740             :             {
     741         723 :                 IF( NE_32( ( error = openCldfb_ivas_fx( &st->cldfbAna, CLDFB_ANALYSIS, 48000, CLDFB_PROTOTYPE_1_25MS, DEC ) ), IVAS_ERR_OK ) )
     742             :                 {
     743           0 :                     return error;
     744             :                 }
     745             :             }
     746             : 
     747         725 :             IF( st->cldfbBPF == NULL )
     748             :             {
     749             :                 /* open analysis BPF for max. internal sampling rate 16kHz */
     750         723 :                 IF( NE_32( ( error = openCldfb_ivas_fx( &st->cldfbBPF, CLDFB_ANALYSIS, 16000, CLDFB_PROTOTYPE_1_25MS, DEC ) ), IVAS_ERR_OK ) )
     751             :                 {
     752           0 :                     return error;
     753             :                 }
     754             :             }
     755             : 
     756             :             /* allocate Fd-Cng structure for second channel */
     757         725 :             IF( NE_32( ( error = createFdCngDec_fx( &st->hFdCngDec ) ), IVAS_ERR_OK ) )
     758             :             {
     759           0 :                 return error;
     760             :             }
     761             : 
     762             :             /* Init FD-CNG */
     763         725 :             initFdCngDec_ivas_fx( st, st->cldfbSyn->scale );
     764             : 
     765         725 :             if ( EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
     766             :             {
     767         723 :                 st->last_core = ACELP_CORE; /* needed to set-up TCX core in SetTCXModeInfo() Q0*/
     768         723 :                 move16();
     769             :             }
     770             : 
     771             :             /*Allocate CoreCoder TCX modules for second channel */
     772         725 :             IF( NE_32( ( error = allocate_CoreCoder_TCX_fx( hCPE->hCoreCoder[1] ) ), IVAS_ERR_OK ) )
     773             :             {
     774           0 :                 return error;
     775             :             }
     776             : 
     777         725 :             IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
     778             :             {
     779         723 :                 IF( EQ_16( hCPE->nchan_out, 1 ) )
     780             :                 {
     781          18 :                     cpy_tcx_ltp_data_fx( hCPE->hCoreCoder[0]->hTcxLtpDec, hCPE->hCoreCoder[1]->hTcxLtpDec, output_Fs );
     782             :                 }
     783             :                 ELSE
     784             :                 {
     785         705 :                     cpy_tcx_ltp_data_fx( &tcxLtpTmp, hCPE->hCoreCoder[1]->hTcxLtpDec, output_Fs );
     786             :                 }
     787             :             }
     788             : 
     789         725 :             IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_TD ) )
     790             :             {
     791           2 :                 Copy32( tmpF_buff, st->hTcxDec->old_syn_Overl_32, L_FRAME16k / 2 );                                                    /* Q11 */
     792           2 :                 Copy_Scale_sig32_16( st->hTcxDec->old_syn_Overl_32, st->hTcxDec->old_syn_Overl, L_FRAME16k / 2, add( st->Q_syn, 5 ) ); //(st->Qsyn - (11 - 16))
     793           2 :                 st->hTcxDec->Q_old_syn_Overl = st->Q_syn;
     794             :             }
     795         725 :             st->hTcxDec->q_old_synth = st->Q_syn;
     796         725 :             move16();
     797         725 :             set16_fx( st->hTcxDec->FBTCXdelayBuf, 0, 111 );
     798         725 :             set32_fx( st->hTcxDec->FBTCXdelayBuf_32, 0, 111 );
     799         725 :             st->hTcxDec->old_synthFB_fx = st->hTcxDec->synth_history_fx + NS2SA_FX2( st->output_Fs, PH_ECU_MEM_NS );         /* q_synth_history_fx */
     800         725 :             st->hTcxDec->prev_good_synth_fx = st->hTcxDec->old_synthFB_fx + NS2SA_FX2( st->output_Fs, PH_ECU_LOOKAHEAD_NS ); /* q_synth_history_fx */
     801             : 
     802             :             /* allocate and initialize MDCT stereo structure */
     803         725 :             IF( ( hCPE->hStereoMdct = (STEREO_MDCT_DEC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_DEC_DATA ) ) ) == NULL )
     804             :             {
     805           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
     806             :             }
     807         725 :             IF( EQ_16( ivas_format, STEREO_FORMAT ) && hCPE->element_brate <= MAX_MDCT_ITD_BRATE )
     808             :             {
     809          24 :                 hCPE->hStereoMdct->use_itd = 1; /* Q0 */
     810          24 :                 move16();
     811             :             }
     812             :             ELSE
     813             :             {
     814         701 :                 hCPE->hStereoMdct->use_itd = 0;
     815         701 :                 move16();
     816             :             }
     817         725 :             hCPE->hStereoMdct->reverse_dmx = 0;
     818         725 :             move16();
     819         725 :             hCPE->hStereoMdct->smooth_ratio_fx = ONE_IN_Q26; /* Q26 */
     820         725 :             move32();
     821             :         }
     822             :     }
     823             : 
     824             :     /*--------------------------------------------------------------*
     825             :      * normal TD / LRTD switching
     826             :      *---------------------------------------------------------------*/
     827             : 
     828      410722 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
     829             :     {
     830        3686 :         IF( hCPE->hCoreCoder[0]->bfi == 0 )
     831             :         {
     832        3630 :             st = hCPE->hCoreCoder[1];
     833        3630 :             IF( hCPE->brate_surplus < 0 )
     834             :             {
     835           0 :                 tmp16 = extract_l( L_negate( Mpy_32_32( L_abs( hCPE->brate_surplus ), ONE_BY_FRAMES_PER_SEC_Q31 ) ) ); /* Q0 */
     836             :             }
     837             :             ELSE
     838             :             {
     839        3630 :                 tmp16 = extract_l( Mpy_32_32( hCPE->brate_surplus, ONE_BY_FRAMES_PER_SEC_Q31 ) ); /* Q0 */
     840             :             }
     841             : 
     842        3630 :             hCPE->hStereoTD->tdm_LRTD_flag = get_indice_st( hCPE->hCoreCoder[0], L_add( hCPE->element_brate, hCPE->brate_surplus ), ( sub( add( sub( extract_l( Mpy_32_16_1( hCPE->element_brate, INV_FRAME_PER_SEC_Q15 ) ), nb_bits_metadata ), tmp16 ), ( TDM_SECONDARY_SIGNALLING + TDM_RATIO_BITS + TDM_LP_REUSE_BITS + TDM_LR_CONTENT_BITS ) ) ), TDM_LR_CONTENT_BITS ); /* Q0 */
     843        3630 :             move16();
     844             : 
     845        3630 :             IF( hCPE->hStereoTD->tdm_LRTD_flag != 0 )
     846             :             {
     847             :                 /* deallocate ICBWE structure */
     848        3549 :                 IF( hCPE->hStereoICBWE != NULL )
     849             :                 {
     850          36 :                     free( hCPE->hStereoICBWE );
     851          36 :                     hCPE->hStereoICBWE = NULL;
     852             :                 }
     853             : 
     854             :                 /* allocate BWEs for secondary channel */
     855        3549 :                 IF( st->hBWE_TD == NULL )
     856             :                 {
     857          41 :                     IF( ( st->hBWE_TD = (TD_BWE_DEC_HANDLE) malloc( sizeof( TD_BWE_DEC_DATA ) ) ) == NULL )
     858             :                     {
     859           0 :                         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD BWE\n" ) );
     860             :                     }
     861             : 
     862          41 :                     td_bwe_dec_init_fx( st->hBWE_TD, -1, st->output_Fs );
     863             : 
     864          41 :                     st->prev_Q_bwe_exc = 31;
     865          41 :                     move16();
     866             : 
     867          41 :                     IF( ( st->hBWE_FD = (FD_BWE_DEC_HANDLE) malloc( sizeof( FD_BWE_DEC_DATA ) ) ) == NULL )
     868             :                     {
     869           0 :                         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FD BWE\n" ) );
     870             :                     }
     871             : 
     872          41 :                     fd_bwe_dec_init_fx( st->hBWE_FD );
     873             :                 }
     874             :             }
     875             :             ELSE /* tdm_LRTD_flag  == 0 */
     876             :             {
     877             :                 /* deallocate BWEs for secondary channel */
     878          81 :                 IF( st->hBWE_TD != NULL )
     879             :                 {
     880          32 :                     IF( st->hBWE_TD != NULL )
     881             :                     {
     882          32 :                         free( st->hBWE_TD );
     883          32 :                         st->hBWE_TD = NULL;
     884             :                     }
     885             : 
     886          32 :                     IF( st->hBWE_FD != NULL )
     887             :                     {
     888          32 :                         free( st->hBWE_FD );
     889          32 :                         st->hBWE_FD = NULL;
     890             :                     }
     891             :                 }
     892             : 
     893             :                 /* allocate ICBWE structure */
     894          81 :                 IF( hCPE->hStereoICBWE == NULL )
     895             :                 {
     896          32 :                     IF( ( hCPE->hStereoICBWE = (STEREO_ICBWE_DEC_HANDLE) malloc( sizeof( STEREO_ICBWE_DEC_DATA ) ) ) == NULL )
     897             :                     {
     898           0 :                         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE \n" ) );
     899             :                     }
     900             : 
     901          32 :                     stereo_icBWE_init_dec_fx( hCPE->hStereoICBWE );
     902             :                 }
     903             :             }
     904             :         }
     905             :     }
     906             : 
     907             :     /*--------------------------------------------------------------*
     908             :      * MDCT stereo bitrate switching
     909             :      *---------------------------------------------------------------*/
     910             : 
     911      410722 :     test();
     912      410722 :     IF( EQ_16( ivas_format, STEREO_FORMAT ) && EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
     913             :     {
     914       21001 :         test();
     915       21001 :         IF( LE_32( hCPE->element_brate, MAX_MDCT_ITD_BRATE ) && GT_32( ivas_total_brate, IVAS_SID_5k2 ) )
     916             :         {
     917       11761 :             IF( hCPE->hStereoMdct->use_itd == 0 )
     918             :             {
     919          29 :                 IF( hCPE->hStereoTCA == NULL )
     920             :                 {
     921             :                     /* allocate TCA data structure */
     922          12 :                     IF( ( hCPE->hStereoTCA = (STEREO_TCA_DEC_HANDLE) malloc( sizeof( STEREO_TCA_DEC_DATA ) ) ) == NULL )
     923             :                     {
     924           0 :                         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
     925             :                     }
     926             :                 }
     927             : 
     928          29 :                 stereo_tca_init_dec_fx( hCPE->hStereoTCA );
     929             :             }
     930             : 
     931       11761 :             hCPE->hStereoMdct->use_itd = 1;
     932       11761 :             move16();
     933             :         }
     934             :         ELSE
     935             :         {
     936             :             /* de-allocate TCA data structure */
     937        9240 :             test();
     938        9240 :             test();
     939        9240 :             test();
     940        9240 :             IF( EQ_16( hCPE->hStereoMdct->use_itd, 1 ) && GT_32( ivas_total_brate, IVAS_SID_5k2 ) && hCPE->hStereoTCA != NULL )
     941             :             {
     942          24 :                 free( hCPE->hStereoTCA );
     943          24 :                 hCPE->hStereoTCA = NULL;
     944          24 :                 hCPE->hStereoMdct->use_itd = 0;
     945          24 :                 move16();
     946             :             }
     947        9216 :             ELSE IF( EQ_16( hCPE->hStereoMdct->use_itd, 1 ) && LE_32( ivas_total_brate, IVAS_SID_5k2 ) )
     948             :             {
     949        2920 :                 hCPE->hStereoMdct->itd_fx = 0;
     950        2920 :                 move32();
     951             :             }
     952             :             ELSE
     953             :             {
     954        6296 :                 hCPE->hStereoMdct->use_itd = 0;
     955        6296 :                 move16();
     956             :             }
     957             :         }
     958             :     }
     959             : 
     960             :     /*--------------------------------------------------------------*
     961             :      * Bitrate switching in MASA format
     962             :      *---------------------------------------------------------------*/
     963             : 
     964      410722 :     test();
     965      410722 :     test();
     966      410722 :     IF( ( EQ_16( ivas_format, MASA_FORMAT ) || EQ_16( ivas_format, MASA_ISM_FORMAT ) ) && EQ_16( nchan_transport, 2 ) )
     967             :     {
     968       24976 :         IF( EQ_16( hCPE->nchan_out, 1 ) )
     969             :         {
     970        5837 :             IF( hCPE->hStereoDftDmx == NULL )
     971             :             {
     972         127 :                 IF( ( hCPE->hStereoDftDmx = (STEREO_DFT_DMX_DATA_HANDLE) malloc( sizeof( STEREO_DFT_DMX_DATA ) ) ) == NULL )
     973             :                 {
     974           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo DFT mono output\n" ) );
     975             :                 }
     976             : 
     977         127 :                 stereo_dft_dmx_out_reset_fx( hCPE->hStereoDftDmx );
     978             :             }
     979             : 
     980        5837 :             IF( hCPE->prev_synth_chs_fx[1] != NULL )
     981             :             {
     982         521 :                 free( hCPE->prev_synth_chs_fx[1] );
     983         521 :                 hCPE->prev_synth_chs_fx[1] = NULL;
     984             :             }
     985             : 
     986        5837 :             IF( hCPE->hStereoTCA != NULL )
     987             :             {
     988         127 :                 free( hCPE->hStereoTCA );
     989         127 :                 hCPE->hStereoTCA = NULL;
     990             :             }
     991             :         }
     992             :         ELSE /* nchan_out == 2 */
     993             :         {
     994       19139 :             IF( hCPE->hStereoDftDmx != NULL )
     995             :             {
     996         118 :                 free( hCPE->hStereoDftDmx );
     997         118 :                 hCPE->hStereoDftDmx = NULL;
     998             :             }
     999             : 
    1000       19139 :             IF( hCPE->prev_synth_chs_fx[1] == NULL )
    1001             :             {
    1002         534 :                 st = hCPE->hCoreCoder[1];
    1003         534 :                 IF( ( hCPE->prev_synth_chs_fx[1] = (Word32 *) malloc( sizeof( Word32 ) * NS2SA_FX2( st->output_Fs, FRAME_SIZE_NS ) ) ) == NULL )
    1004             :                 {
    1005           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
    1006             :                 }
    1007         534 :                 set32_fx( hCPE->prev_synth_chs_fx[1], 0, NS2SA_FX2( st->output_Fs, FRAME_SIZE_NS ) );
    1008             :             }
    1009             : 
    1010       19139 :             IF( hCPE->hStereoICBWE == NULL && EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
    1011             :             {
    1012           5 :                 IF( ( hCPE->hStereoICBWE = (STEREO_ICBWE_DEC_HANDLE) malloc( sizeof( STEREO_ICBWE_DEC_DATA ) ) ) == NULL )
    1013             :                 {
    1014           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE \n" ) );
    1015             :                 }
    1016             : 
    1017           5 :                 stereo_icBWE_init_dec_fx( hCPE->hStereoICBWE );
    1018             :             }
    1019             : 
    1020       19139 :             IF( hCPE->hStereoTCA == NULL && ( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) || EQ_16( hCPE->element_mode, IVAS_CPE_TD ) ) )
    1021             :             {
    1022         118 :                 IF( ( hCPE->hStereoTCA = (STEREO_TCA_DEC_HANDLE) malloc( sizeof( STEREO_TCA_DEC_DATA ) ) ) == NULL )
    1023             :                 {
    1024           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
    1025             :                 }
    1026             : 
    1027         118 :                 stereo_tca_init_dec_fx( hCPE->hStereoTCA );
    1028             :             }
    1029             : 
    1030       19139 :             IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
    1031             :             {
    1032       13079 :                 IF( hCPE->hStereoTCA != NULL )
    1033             :                 {
    1034             :                     /* note: in MASA, hCPE->hStereoMdct->itd = 0 */
    1035         255 :                     free( hCPE->hStereoTCA );
    1036         255 :                     hCPE->hStereoTCA = NULL;
    1037             :                 }
    1038             : 
    1039       13079 :                 IF( hCPE->hStereoICBWE != NULL )
    1040             :                 {
    1041           0 :                     free( hCPE->hStereoICBWE );
    1042           0 :                     hCPE->hStereoICBWE = NULL;
    1043             :                 }
    1044             :             }
    1045             :         }
    1046             :     }
    1047             : 
    1048             :     /*--------------------------------------------------------------*
    1049             :      * Bitrate switching in MASA format
    1050             :      *---------------------------------------------------------------*/
    1051             : 
    1052      410722 :     test();
    1053      410722 :     IF( EQ_16( ivas_format, MC_FORMAT ) && EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
    1054             :     {
    1055       99112 :         test();
    1056       99112 :         IF( EQ_16( mc_mode, MC_MODE_MCT ) || EQ_16( mc_mode, MC_MODE_PARAMUPMIX ) )
    1057             :         {
    1058             :             /* deallocate the FdCNG handle */
    1059      263244 :             FOR( i = 0; i < CPE_CHANNELS; ++i )
    1060             :             {
    1061      175496 :                 deleteFdCngDec_fx( &hCPE->hCoreCoder[i]->hFdCngDec );
    1062             :             }
    1063             :         }
    1064             :         ELSE
    1065             :         {
    1066             :             /* allocate the FdCNG handle (for noise estimation for TCX PLC fadeout)*/
    1067       34092 :             FOR( i = 0; i < CPE_CHANNELS; ++i )
    1068             :             {
    1069       22728 :                 IF( hCPE->hCoreCoder[i]->cldfbSyn == NULL ) /* could be NULL when we had the MCT LFE channel */
    1070             :                 {
    1071           0 :                     IF( NE_32( ( error = openCldfb_ivas_fx( &hCPE->hCoreCoder[i]->cldfbSyn, CLDFB_SYNTHESIS, hCPE->hCoreCoder[i]->output_Fs, CLDFB_PROTOTYPE_1_25MS, DEC ) ), IVAS_ERR_OK ) )
    1072             :                     {
    1073           0 :                         return error;
    1074             :                     }
    1075             :                 }
    1076             : 
    1077       22728 :                 IF( hCPE->hCoreCoder[i]->hFdCngDec == NULL )
    1078             :                 {
    1079         360 :                     IF( NE_32( ( error = createFdCngDec_fx( &hCPE->hCoreCoder[i]->hFdCngDec ) ), IVAS_ERR_OK ) )
    1080             :                     {
    1081           0 :                         return error;
    1082             :                     }
    1083         360 :                     initFdCngDec_ivas_fx( hCPE->hCoreCoder[i], hCPE->hCoreCoder[i]->cldfbSyn->scale );
    1084             :                 }
    1085             :             }
    1086             :         }
    1087             :     }
    1088             : 
    1089      410722 :     return error;
    1090             : }
    1091             : 
    1092             : 
    1093             : /*-------------------------------------------------------------------*
    1094             :  * Function synchro_synthesis()
    1095             :  *
    1096             :  * Synchronize upmixed DFT/TD/MDCT stereo synthesis to match the overall delay of 32ms
    1097             :  * Handling of TD stereo <-> DFT stereo transitions
    1098             :  *-------------------------------------------------------------------*/
    1099             : 
    1100      410292 : void synchro_synthesis_fx(
    1101             :     const Word32 ivas_total_brate,      /* i  : IVAS total bitrate                  Q0*/
    1102             :     CPE_DEC_HANDLE hCPE,                /* i/o: CPE decoder structure                 */
    1103             :     Word32 *output_fx[CPE_CHANNELS],    /* i/o: output synthesis signal             Q11*/
    1104             :     const Word16 output_frame,          /* i  : Number of samples                   Q0*/
    1105             :     const Word16 sba_dirac_stereo_flag, /* i  : signal stereo output FOR SBA DirAC  Q0*/
    1106             :     Word16 output_fx_q )
    1107             : {
    1108             :     Word16 n, delay_comp_TD, delay_comp_DFT;
    1109             :     Word32 output_Fs;
    1110             :     Decoder_State **sts;
    1111             :     Word16 i, delay_cldfb, dft32ms_ovl;
    1112             : 
    1113             :     Word32 *p_output_mem_fx[CPE_CHANNELS];
    1114             :     Word32 tmp_out_fx[CPE_CHANNELS][NS2SA( 48000, DELAY_CLDFB_NS )];
    1115             :     Word32 tmp_out_TD_fx[CPE_CHANNELS][STEREO_DFT32MS_OVL_MAX];
    1116             :     Word32 tmp_out_TD2_fx[CPE_CHANNELS][STEREO_DFT32MS_OVL_MAX];
    1117             : 
    1118             :     Word16 use_cldfb_for_last_dft;
    1119             :     Word16 dft_mono_brate_switch;
    1120             :     Word16 delay_diff;
    1121      410292 :     Word32 tmpF_fx = 0;
    1122      410292 :     move32();
    1123             :     Word16 nChannels;
    1124             : 
    1125      410292 :     sts = hCPE->hCoreCoder;
    1126      410292 :     output_Fs = sts[0]->output_Fs; /* Q0 */
    1127             : 
    1128      410292 :     use_cldfb_for_last_dft = 0;
    1129      410292 :     move16();
    1130             : 
    1131      410292 :     test();
    1132      410292 :     test();
    1133      410292 :     test();
    1134      410292 :     test();
    1135      410292 :     test();
    1136      410292 :     if ( ( NE_16( hCPE->element_mode, IVAS_CPE_DFT ) && EQ_16( hCPE->nchan_out, 1 ) && LE_32( hCPE->last_element_brate, IVAS_24k4 ) ) /* note: this is to mimic the DFT stereo condition "hCPE->hStereoDft->hConfig->res_cod_mode == 0" in last frame */
    1137      408617 :          || ( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && EQ_16( hCPE->nchan_out, 1 ) && EQ_16( hCPE->hStereoDft->hConfig->res_cod_mode, STEREO_DFT_RES_COD_OFF ) ) )
    1138             :     {
    1139       18725 :         use_cldfb_for_last_dft = 1; /* Q0 */
    1140       18725 :         move16();
    1141             :     }
    1142             : 
    1143      410292 :     dft_mono_brate_switch = 0;
    1144      410292 :     move16();
    1145      410292 :     test();
    1146      410292 :     test();
    1147      410292 :     test();
    1148      410292 :     test();
    1149      410292 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && ( EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) || EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) ) && EQ_16( hCPE->nchan_out, 1 ) && NE_32( hCPE->element_brate, hCPE->last_element_brate ) )
    1150             :     {
    1151        2167 :         test();
    1152        2167 :         test();
    1153        2167 :         IF( GE_32( hCPE->last_element_brate, IVAS_32k ) && EQ_16( hCPE->hStereoDft->hConfig->res_cod_mode, STEREO_DFT_RES_COD_OFF ) )
    1154             :         {
    1155         546 :             dft_mono_brate_switch = -1; /* switch from residual coding mode or MDCT Stereo      Q0*/
    1156         546 :             move16();
    1157             :         }
    1158        1621 :         ELSE IF( LE_32( hCPE->last_element_brate, IVAS_24k4 ) && GT_16( hCPE->hStereoDft->hConfig->res_cod_mode, STEREO_DFT_RES_COD_OFF ) )
    1159             :         {
    1160          59 :             dft_mono_brate_switch = 1; /* switch to residual coding mode        Q0*/
    1161          59 :             move16();
    1162             :         }
    1163             :     }
    1164             : 
    1165      410292 :     IF( use_cldfb_for_last_dft )
    1166             :     {
    1167       18725 :         test();
    1168       18725 :         test();
    1169       18725 :         test();
    1170       18725 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_TD ) && ( GT_32( ivas_total_brate, IVAS_SID_5k2 ) || EQ_16( hCPE->nchan_out, 2 ) ) )
    1171             :         {
    1172           7 :             stereo_tca_scale_R_channel_fx( hCPE, output_fx[0], output_frame );
    1173             :         }
    1174             :     }
    1175             : 
    1176             :     /* set delays */
    1177      410292 :     delay_comp_DFT = NS2SA_FX2( output_Fs, IVAS_DEC_DELAY_NS - STEREO_DFT32MS_OVL_NS ); /* Q0 */
    1178      410292 :     move16();
    1179      410292 :     delay_comp_TD = NS2SA_FX2( output_Fs, IVAS_DEC_DELAY_NS - DELAY_CLDFB_NS ); /* Q0 */
    1180      410292 :     move16();
    1181      410292 :     delay_diff = sub( delay_comp_TD, delay_comp_DFT ); /* Q0 */
    1182             : 
    1183      410292 :     dft32ms_ovl = extract_l( Mpy_32_32( imult3216( output_Fs, STEREO_DFT32MS_OVL_MAX ), 44740 ) ); /* Q0 */
    1184      410292 :     delay_cldfb = NS2SA_FX2( output_Fs, DELAY_CLDFB_NS );                                          /* Q0 */
    1185      410292 :     move16();
    1186             : 
    1187             :     /* initialize pointers */
    1188      410292 :     test();
    1189      410292 :     IF( GE_16( hCPE->element_mode, IVAS_CPE_DFT ) && hCPE->output_mem_fx[0] != NULL )
    1190             :     {
    1191      324159 :         FOR( n = 0; n < CPE_CHANNELS; n++ )
    1192             :         {
    1193      216106 :             p_output_mem_fx[n] = hCPE->output_mem_fx[n]; /* Q11 */
    1194             :         }
    1195             :     }
    1196             : 
    1197             :     /*----------------------------------------------------------------*
    1198             :      * DFT stereo synchro
    1199             :      *----------------------------------------------------------------*/
    1200             : 
    1201      410292 :     test();
    1202      410292 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) || sba_dirac_stereo_flag )
    1203             :     {
    1204             :         /* handling of bitrate switching from residual (using DFT) to non-residual mode (using CLDFB) FOR mono output - as in DFT->TD switching */
    1205       70482 :         IF( EQ_16( dft_mono_brate_switch, -1 ) )
    1206             :         {
    1207       42936 :             FOR( i = delay_comp_DFT; i < delay_comp_TD; i++ )
    1208             :             {
    1209       42390 :                 sts[0]->prev_synth_buffer32_fx[i] = p_output_mem_fx[0][i - delay_comp_DFT]; /* Q11 */
    1210       42390 :                 move32();
    1211             :             }
    1212             : 
    1213       28806 :             FOR( i = delay_comp_TD; i < add( delay_comp_TD, delay_cldfb ); i++ )
    1214             :             {
    1215       28260 :                 tmp_out_fx[0][i - delay_comp_TD] = p_output_mem_fx[0][i - delay_comp_DFT]; /* Q11 */
    1216       28260 :                 move32();
    1217             :             }
    1218             :         }
    1219             : 
    1220       70482 :         test();
    1221       70482 :         IF( EQ_16( hCPE->nchan_out, 1 ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
    1222             :         {
    1223        2617 :             FOR( Word32 ind = 0; ind < delay_comp_DFT; ind++ )
    1224             :             {
    1225        2206 :                 Word32 temp = L_add( sts[0]->prev_synth_buffer32_fx[ind], sts[1]->prev_synth_buffer32_fx[ind] ); /* Q11 */
    1226        2206 :                 sts[0]->prev_synth_buffer32_fx[ind] = Mpy_32_32( temp, INV_SQRT2_FX );                           /* Q11 */
    1227        2206 :                 move32();
    1228             :             }
    1229             :         }
    1230             : 
    1231       70482 :         IF( use_cldfb_for_last_dft )
    1232             :         {
    1233             :             /* delay CLDFB-based mono output (<= 24.4 kbps) to be aligned with DFT-based mono output (32 kbps), needed to avoid discontinuities with TCX-LTP. */
    1234       17050 :             Copy32( sts[0]->prev_synth_buffer32_fx + delay_comp_DFT, hCPE->hCoreCoder[0]->hTcxDec->FBTCXdelayBuf_32, delay_diff ); /* Q11 */
    1235       17050 :             delay_signal32_fx( output_fx[0], output_frame, hCPE->hCoreCoder[0]->hTcxDec->FBTCXdelayBuf_32, delay_diff );
    1236             :         }
    1237             : 
    1238       70482 :         IF( NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
    1239             :         {
    1240       63502 :             ivas_post_proc_fx( NULL, hCPE, 0, output_fx[0], output_fx, output_frame, sba_dirac_stereo_flag, output_fx_q );
    1241             :         }
    1242             : 
    1243             :         /* zero padding in order to synchronize the upmixed DFT stereo synthesis with the TD/MDCT stereo synthesis */
    1244      189041 :         FOR( n = 0; n < hCPE->nchan_out; n++ )
    1245             :         {
    1246      118559 :             IF( sba_dirac_stereo_flag )
    1247             :             {
    1248       21580 :                 delay_signal_q_adj_fx( output_fx[n], output_frame, hCPE->prev_synth_fx[n], delay_comp_DFT, hCPE->hStereoDft->q_dft, hCPE->q_prev_synth_fx );
    1249             :             }
    1250             :             ELSE
    1251             :             {
    1252       96979 :                 delay_signal32_fx( output_fx[n], output_frame, sts[n]->prev_synth_buffer32_fx, delay_comp_DFT );
    1253             :             }
    1254             :         }
    1255       70482 :         if ( sba_dirac_stereo_flag )
    1256             :         {
    1257       10790 :             hCPE->q_prev_synth_fx = hCPE->hStereoDft->q_dft;
    1258       10790 :             move16();
    1259             :         }
    1260       70482 :         IF( use_cldfb_for_last_dft )
    1261             :         {
    1262       17050 :             Copy32( hCPE->hCoreCoder[0]->hTcxDec->FBTCXdelayBuf_32, sts[0]->prev_synth_buffer32_fx + delay_comp_DFT, delay_diff ); /* Q11 */
    1263             :         }
    1264             : 
    1265             :         /* handling of TD->DFT switching */
    1266      189041 :         FOR( n = 0; n < hCPE->nchan_out; n++ )
    1267             :         {
    1268      118559 :             test();
    1269      118559 :             test();
    1270      118559 :             test();
    1271             : #ifdef NONBE_1289_STEREO_SW_TO_MONO
    1272      118559 :             test();
    1273      118559 :             IF( ( !use_cldfb_for_last_dft && NE_16( hCPE->last_element_mode, IVAS_CPE_DFT ) && !sba_dirac_stereo_flag && NE_16( dft_mono_brate_switch, -1 ) ) || EQ_16( dft_mono_brate_switch, 1 ) )
    1274             : #else
    1275             :             IF( ( NE_16( hCPE->last_element_mode, IVAS_CPE_DFT ) && !sba_dirac_stereo_flag && NE_16( dft_mono_brate_switch, -1 ) ) || EQ_16( dft_mono_brate_switch, 1 ) )
    1276             : #endif
    1277         698 :             {
    1278             :                 Word32 *pPrev_synth_fx;
    1279         698 :                 Word32 inv_fade_len_fx = 0;
    1280         698 :                 move32();
    1281         698 :                 SWITCH( output_Fs )
    1282             :                 {
    1283          78 :                     case 16000:
    1284          78 :                         inv_fade_len_fx = 71582792; /* Q31 */
    1285          78 :                         move32();
    1286          78 :                         BREAK;
    1287         169 :                     case 32000:
    1288         169 :                         inv_fade_len_fx = 35791396; /* Q31 */
    1289         169 :                         move32();
    1290         169 :                         BREAK;
    1291         451 :                     case 48000:
    1292         451 :                         inv_fade_len_fx = 23860930; /* Q31 */
    1293         451 :                         move32();
    1294         451 :                         BREAK;
    1295           0 :                     default:
    1296           0 :                         assert( 0 );
    1297             :                 }
    1298             : 
    1299             :                 /* cross-fading between TD synchro memory and the DFT output */
    1300         698 :                 IF( sba_dirac_stereo_flag )
    1301             :                 {
    1302           0 :                     pPrev_synth_fx = hCPE->prev_synth_fx[n]; /* Q11 */
    1303           0 :                     move32();
    1304             :                 }
    1305             :                 ELSE
    1306             :                 {
    1307         698 :                     pPrev_synth_fx = sts[n]->prev_synth_buffer32_fx; /* Q11 */
    1308         698 :                     move32();
    1309             :                 }
    1310             : 
    1311         698 :                 IF( NE_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
    1312             :                 {
    1313        7015 :                     FOR( i = delay_comp_DFT; i < delay_comp_TD; i++ )
    1314             :                     {
    1315        6900 :                         Word64 res1 = W_mult0_32_32( pPrev_synth_fx[i], ( sub( delay_comp_TD, i ) ) ); /* Q11 */
    1316        6900 :                         Word64 res2 = W_mult0_32_32( output_fx[n][i], ( sub( i, delay_comp_DFT ) ) );  /* Q11 */
    1317        6900 :                         Word64 res = W_add( W_shr( res1, 6 ), W_shr( res2, 6 ) );                      /* Q11 - 6 */
    1318        6900 :                         res = res * inv_fade_len_fx;                                                   /* Q11 + 31 - 6 */
    1319        6900 :                         move64();
    1320        6900 :                         output_fx[n][i] = W_extract_l( W_shr( res, 31 - 6 ) ); /* Q11 */
    1321        6900 :                         move32();
    1322             :                         // change 6 to less number to get better precision
    1323             :                     }
    1324             :                 }
    1325             :             }
    1326      117861 :             ELSE IF( EQ_16( dft_mono_brate_switch, -1 ) )
    1327             :             {
    1328         546 :                 Word32 inv_fade_len_1_fx = 0;
    1329         546 :                 move32();
    1330         546 :                 Word32 inv_fade_len_2_fx = 0;
    1331         546 :                 move32();
    1332             : 
    1333         546 :                 SWITCH( output_Fs )
    1334             :                 {
    1335          63 :                     case 16000:
    1336          63 :                         inv_fade_len_1_fx = 71582792; /* Q31 */
    1337          63 :                         move32();
    1338          63 :                         inv_fade_len_2_fx = 107374184; /* Q31 */
    1339          63 :                         move32();
    1340          63 :                         BREAK;
    1341          99 :                     case 32000:
    1342          99 :                         inv_fade_len_1_fx = 35791396; /* Q31 */
    1343          99 :                         move32();
    1344          99 :                         inv_fade_len_2_fx = 53687092; /* Q31 */
    1345          99 :                         move32();
    1346          99 :                         BREAK;
    1347         384 :                     case 48000:
    1348         384 :                         inv_fade_len_1_fx = 23860930; /* Q31 */
    1349         384 :                         move32();
    1350         384 :                         inv_fade_len_2_fx = 35791396; /* Q31 */
    1351         384 :                         move32();
    1352         384 :                         BREAK;
    1353           0 :                     default:
    1354           0 :                         assert( 0 );
    1355             :                 }
    1356       42936 :                 FOR( i = 0; i < delay_diff; i++ )
    1357             :                 {
    1358       42390 :                     Word64 res1 = W_mult0_32_32( output_fx[0][i + delay_comp_DFT], ( sub( delay_diff, i ) ) ); /* Q11 */
    1359       42390 :                     Word64 res2 = W_mult0_32_32( p_output_mem_fx[0][i], i );                                   /* Q11 */
    1360       42390 :                     Word64 res = W_add( W_shr( res1, 6 ), W_shr( res2, 6 ) );                                  /* Q11 - 6 */
    1361       42390 :                     res = res * inv_fade_len_1_fx;                                                             /* Q11 + 31 - 6 */
    1362       42390 :                     move64();
    1363       42390 :                     output_fx[0][i + delay_comp_DFT] = W_extract_l( W_shr( res, 31 - 6 ) ); /* Q11 */
    1364       42390 :                     move32();
    1365             :                 }
    1366             : 
    1367       28806 :                 FOR( i = 0; i < delay_cldfb; i++ )
    1368             :                 {
    1369       28260 :                     Word64 res1 = W_mult0_32_32( tmp_out_fx[0][i], ( sub( delay_cldfb, i ) ) ); /* Q11 */
    1370       28260 :                     Word64 res2 = W_mult0_32_32( output_fx[0][i + delay_comp_TD], i );          /* Q11 */
    1371       28260 :                     Word64 res = W_add( W_shr( res1, 6 ), W_shr( res2, 6 ) );                   /* Q11 - 6 */
    1372       28260 :                     res = res * inv_fade_len_2_fx;                                              /* Q11 + 31 - 6 */
    1373       28260 :                     move64();
    1374       28260 :                     output_fx[0][i + delay_comp_TD] = W_extract_l( W_shr( res, 31 - 6 ) ); /* Q11 */
    1375       28260 :                     move32();
    1376             :                 }
    1377             :             }
    1378             :         }
    1379             :     }
    1380             : 
    1381             :     /*----------------------------------------------------------------*
    1382             :      * TD/MDCT stereo synchro
    1383             :      *----------------------------------------------------------------*/
    1384             : 
    1385      410292 :     IF( sba_dirac_stereo_flag )
    1386             :     {
    1387       10790 :         return;
    1388             :     }
    1389             : 
    1390      399502 :     test();
    1391      399502 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) || EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
    1392             :     {
    1393             :         /* handling of DFT->TD switching */
    1394      339810 :         test();
    1395      339810 :         test();
    1396      339810 :         IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) && !use_cldfb_for_last_dft && hCPE->output_mem_fx[0] != NULL )
    1397             :         {
    1398             :             /* use redressed DFT stereo OLA part to reconstruct the TD stereo synchro memory */
    1399        2222 :             FOR( n = 0; n < hCPE->nchan_out; n++ )
    1400             :             {
    1401      118147 :                 FOR( i = delay_comp_DFT; i < delay_comp_TD; i++ )
    1402             :                 {
    1403      116670 :                     sts[n]->prev_synth_buffer32_fx[i] = p_output_mem_fx[n][i - delay_comp_DFT]; /* Q11 */
    1404      116670 :                     move32();
    1405             :                 }
    1406             : 
    1407       79257 :                 FOR( i = delay_comp_TD; i < add( delay_comp_TD, delay_cldfb ); i++ )
    1408             :                 {
    1409       77780 :                     tmp_out_fx[n][i - delay_comp_TD] = p_output_mem_fx[n][i - delay_comp_DFT]; /* Q11 */
    1410       77780 :                     move32();
    1411             :                 }
    1412             :             }
    1413             :         }
    1414             : 
    1415             :         /* IF previous frame had only one channel copy buffers to other channel */
    1416      339810 :         test();
    1417      339810 :         test();
    1418      339810 :         IF( EQ_16( hCPE->nchan_out, 1 ) && EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
    1419             :         {
    1420          18 :             Copy32( sts[0]->prev_synth_buffer32_fx, sts[1]->prev_synth_buffer32_fx, delay_comp_TD ); /* Q11 */
    1421          18 :             Copy32( tmp_out_fx[0], tmp_out_fx[1], delay_cldfb );                                     /* Q11 */
    1422          18 :             Copy32( p_output_mem_fx[0], p_output_mem_fx[1], delay_diff );                            /* Q11 */
    1423             :         }
    1424             : 
    1425             :         /*----------------------------------------------------------------*
    1426             :          * update DFT synthesis overlap memory @output_Fs; needed FOR TD->DFT stereo switching
    1427             :          *----------------------------------------------------------------*/
    1428             : 
    1429             :         /* resample LB synthesis to output_Fs */
    1430      339810 :         test();
    1431      339810 :         IF( NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) && !use_cldfb_for_last_dft )
    1432             :         {
    1433        8644 :             FOR( n = 0; n < hCPE->nchan_out; n++ )
    1434             :             {
    1435        5499 :                 IF( EQ_16( sts[n]->core, ACELP_CORE ) )
    1436             :                 {
    1437        5388 :                     L_lerp_fx_q11( hCPE->input_mem_LB_fx[n], tmp_out_TD_fx[n], dft32ms_ovl, NS2SA_FX2( sts[n]->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) );
    1438             :                 }
    1439             :                 ELSE /* TCX/HQ core */
    1440             :                 {
    1441         111 :                     L_lerp_fx_q11( hCPE->input_mem_LB_fx[n], tmp_out_TD_fx[n], dft32ms_ovl, NS2SA_FX2( sts[n]->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) );
    1442             : 
    1443             :                     /* use TCX synchro memory (perfect signal is available) */
    1444        4871 :                     FOR( i = delay_diff; i < dft32ms_ovl; i++ )
    1445             :                     {
    1446        4760 :                         tmp_out_TD_fx[n][i] = sts[n]->delay_buf_out32_fx[i - delay_diff]; /* Q11 */
    1447        4760 :                         move32();
    1448             :                     }
    1449             :                 }
    1450             :             }
    1451             : 
    1452        3145 :             IF( EQ_16( hCPE->nchan_out, CPE_CHANNELS ) )
    1453             :             {
    1454             :                 /* upmix the resampled LB / the TCX synchro memory */
    1455        2354 :                 tdm_upmix_plain_fx( tmp_out_TD2_fx[0], tmp_out_TD2_fx[1], tmp_out_TD_fx[0], tmp_out_TD_fx[1], tdm_ratio_tabl_fx[hCPE->hStereoTD->tdm_last_ratio_idx], tdm_den_ratio_tabl_fx[hCPE->hStereoTD->tdm_last_ratio_idx], 0, dft32ms_ovl, 1 );
    1456             :             }
    1457             :             ELSE
    1458             :             {
    1459         791 :                 Copy32( tmp_out_TD_fx[0], tmp_out_TD2_fx[0], dft32ms_ovl ); /* Q11 */
    1460             :             }
    1461             : 
    1462        8644 :             FOR( n = 0; n < hCPE->nchan_out; n++ )
    1463             :             {
    1464        5499 :                 IF( sts[0]->core == ACELP_CORE ) /* ACELP core in primary channel */
    1465             :                 {
    1466        5323 :                     SWITCH( output_Fs )
    1467             :                     {
    1468        1264 :                         case 16000:
    1469        1264 :                             tmpF_fx = 71582792; /* 0.033 in Q31 */
    1470        1264 :                             move32();
    1471        1264 :                             BREAK;
    1472        2291 :                         case 32000:
    1473        2291 :                             tmpF_fx = 35791396; /* 0.016 in Q31 */
    1474        2291 :                             move32();
    1475        2291 :                             BREAK;
    1476        1768 :                         case 48000:
    1477        1768 :                             tmpF_fx = 23860930; /* 0.011 in Q31 */
    1478        1768 :                             move32();
    1479        1768 :                             BREAK;
    1480           0 :                         default:
    1481           0 :                             assert( 0 );
    1482             :                     }
    1483             :                     /* cross-fading between regular output synthesis and lerp_flt() resampled synthesis in 3.125 - 1.25 ms OLA part */
    1484      339823 :                     FOR( i = 0; i < delay_diff; i++ )
    1485             :                     {
    1486             : 
    1487      334500 :                         Word64 res1 = W_mult0_32_32( output_fx[n][output_frame - dft32ms_ovl + delay_cldfb + i], ( sub( delay_diff, i ) ) ); /* Q11 */
    1488      334500 :                         Word64 res2 = W_mult0_32_32( tmp_out_TD2_fx[n][i], i );                                                              /* Q11 */
    1489      334500 :                         Word64 res = W_add( W_shr( res1, 6 ), W_shr( res2, 6 ) );                                                            /* Q11 - Q6 */
    1490      334500 :                         res = res * tmpF_fx;                                                                                                 /* Q11 + Q31 - Q6 */
    1491      334500 :                         move64();
    1492      334500 :                         p_output_mem_fx[n][i] = W_extract_l( W_shr( res, 31 - 6 ) ); /* Q11 */
    1493             :                     }
    1494             :                 }
    1495             :                 ELSE /* TCX core */
    1496             :                 {
    1497             :                     /* reconstruct the 3.125 - 1.25 ms OLA part */
    1498       11696 :                     FOR( i = 0; i < delay_diff; i++ )
    1499             :                     {
    1500       11520 :                         p_output_mem_fx[n][i] = output_fx[n][output_frame - dft32ms_ovl + delay_cldfb + i]; /* Q11 */
    1501       11520 :                         move32();
    1502             :                     }
    1503             :                 }
    1504             : 
    1505             :                 /* reconstruct the last 1.25 ms part of OLA window */
    1506      236179 :                 FOR( i = delay_diff; i < dft32ms_ovl; i++ )
    1507             :                 {
    1508      230680 :                     p_output_mem_fx[n][i] = tmp_out_TD2_fx[n][i]; /* Q11 */
    1509      230680 :                     move32();
    1510             :                 }
    1511             :             }
    1512             :         }
    1513             : 
    1514             :         /*----------------------------------------------------------------*
    1515             :          * zero padding TD/MDCT synthesis in order to synchronize
    1516             :          * the upmixed TD/MDCT stereo synthesis with the DFT stereo synthesis
    1517             :          *----------------------------------------------------------------*/
    1518             : 
    1519      339810 :         test();
    1520      339810 :         test();
    1521      339810 :         test();
    1522      339810 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && hCPE->stereo_switching_counter == 0 && EQ_16( hCPE->nchan_out, 1 ) && LE_32( hCPE->last_element_brate, IVAS_24k4 ) )
    1523             :         {
    1524          14 :             Word32 step_fx = 0;
    1525          14 :             move32();
    1526          14 :             tmpF_fx = ONE_IN_Q31; /* Q31 */
    1527          14 :             move32();
    1528          14 :             SWITCH( output_Fs )
    1529             :             {
    1530           7 :                 case 16000:
    1531           7 :                     step_fx = 33554432; /* 0.0156 in Q31 */
    1532           7 :                     move32();
    1533           7 :                     BREAK;
    1534           5 :                 case 32000:
    1535           5 :                     step_fx = 16777216; /* 0.0078 in Q31 */
    1536           5 :                     move32();
    1537           5 :                     BREAK;
    1538           2 :                 case 48000:
    1539           2 :                     step_fx = 11184811; /* 0.0052 in Q31 */
    1540           2 :                     move32();
    1541           2 :                     BREAK;
    1542           0 :                 default:
    1543           0 :                     assert( 0 );
    1544             :             }
    1545             :             /* FOR the first switching frame from DFT to TD, downmix memory too */
    1546         750 :             FOR( n = 0; n < delay_comp_TD; n++ )
    1547             :             {
    1548         736 :                 sts[0]->prev_synth_buffer32_fx[n] = Mpy_32_32( sts[0]->prev_synth_buffer32_fx[n], tmpF_fx ); /* Q11 */
    1549         736 :                 move32();
    1550         736 :                 tmpF_fx = L_sub_sat( tmpF_fx, step_fx ); /* Q31 */
    1551             :             }
    1552             :         }
    1553             : 
    1554      339810 :         test();
    1555      339810 :         test();
    1556      339810 :         test();
    1557      339810 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && EQ_16( hCPE->nchan_out, 1 ) && !is_DTXrate( hCPE->element_brate ) && is_DTXrate( hCPE->last_element_brate ) )
    1558             :         {
    1559          34 :             Copy32( sts[0]->prev_synth_buffer32_fx, sts[1]->prev_synth_buffer32_fx, delay_comp_TD ); /* Q11 */
    1560             :         }
    1561             : 
    1562             :         // nChannels = ( hCPE->element_mode == IVAS_CPE_MDCT ) ? 2 : hCPE->nchan_out;
    1563      339810 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
    1564             :         {
    1565      336124 :             nChannels = 2; /* Q0 */
    1566      336124 :             move16();
    1567             :         }
    1568             :         ELSE
    1569             :         {
    1570        3686 :             nChannels = hCPE->nchan_out; /* Q0 */
    1571        3686 :             move16();
    1572             :         }
    1573     1018098 :         FOR( n = 0; n < nChannels; n++ )
    1574             :         {
    1575      678288 :             IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
    1576             :             {
    1577      672248 :                 Copy32( sts[n]->prev_synth_buffer32_fx + delay_comp_DFT, hCPE->hCoreCoder[n]->hTcxDec->FBTCXdelayBuf_32, delay_diff ); /* Q11 */
    1578      672248 :                 delay_signal32_fx( output_fx[n], output_frame, hCPE->hCoreCoder[n]->hTcxDec->FBTCXdelayBuf_32, delay_diff );           /* Q11 */
    1579      672248 :                 ivas_post_proc_fx( NULL, hCPE, n, output_fx[n], output_fx, output_frame, 0, output_fx_q );
    1580             : 
    1581      672248 :                 delay_signal32_fx( output_fx[n], output_frame, sts[n]->prev_synth_buffer32_fx, delay_comp_DFT );
    1582      672248 :                 Copy32( hCPE->hCoreCoder[n]->hTcxDec->FBTCXdelayBuf_32, sts[n]->prev_synth_buffer32_fx + delay_comp_DFT, delay_diff ); /* Q11 */
    1583             :             }
    1584             :             ELSE
    1585             :             {
    1586        6040 :                 delay_signal32_fx( output_fx[n], output_frame, sts[n]->prev_synth_buffer32_fx, delay_comp_TD );
    1587             :             }
    1588             :         }
    1589             : 
    1590             :         /* handling of DFT->TD switching */
    1591      339810 :         test();
    1592      339810 :         IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) && !use_cldfb_for_last_dft )
    1593             :         {
    1594         745 :             test();
    1595         745 :             IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && hCPE->hStereoCng->prev_sid_nodata )
    1596             :             {
    1597          21 :                 FOR( n = 0; n < hCPE->nchan_out; n++ )
    1598             :                 {
    1599          14 :                     SWITCH( output_Fs )
    1600             :                     {
    1601           2 :                         case 16000:
    1602           2 :                             tmpF_fx = 107374184; /* 0.05 in Q31 */
    1603           2 :                             move32();
    1604           2 :                             BREAK;
    1605           6 :                         case 32000:
    1606           6 :                             tmpF_fx = 53687092; /* 0.025 in Q31 */
    1607           6 :                             move32();
    1608           6 :                             BREAK;
    1609           6 :                         case 48000:
    1610           6 :                             tmpF_fx = 35791396; /* 0.016 in Q31 */
    1611           6 :                             move32();
    1612           6 :                             BREAK;
    1613           0 :                         default:
    1614           0 :                             assert( 0 );
    1615             :                     }
    1616         654 :                     FOR( i = 0; i < delay_cldfb; i++ )
    1617             :                     {
    1618         640 :                         tmp_out_fx[n][i] = W_extract_l( W_mult0_32_32( Mpy_32_32( tmp_out_fx[n][i], tmpF_fx ), ( sub( delay_cldfb, i ) ) ) ); /* Q11 */
    1619         640 :                         move32();
    1620             :                     }
    1621             :                 }
    1622             :             }
    1623             : 
    1624             :             /* cross-fading between DFT OLA memory and TD output */
    1625        2228 :             FOR( n = 0; n < nChannels; n++ )
    1626             :             {
    1627        1483 :                 IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
    1628             :                 {
    1629        1422 :                     SWITCH( output_Fs )
    1630             :                     {
    1631         108 :                         case 16000:
    1632         108 :                             tmpF_fx = 71582792; /* Q31 */
    1633         108 :                             move32();
    1634         108 :                             BREAK;
    1635         272 :                         case 32000:
    1636         272 :                             tmpF_fx = 35791396; /* Q31 */
    1637         272 :                             move32();
    1638         272 :                             BREAK;
    1639        1042 :                         case 48000:
    1640        1042 :                             tmpF_fx = 23860930; /* Q31 */
    1641        1042 :                             move32();
    1642        1042 :                             BREAK;
    1643           0 :                         default:
    1644           0 :                             assert( 0 );
    1645             :                     }
    1646      114762 :                     FOR( i = 0; i < delay_diff; i++ )
    1647             :                     {
    1648             : 
    1649      113340 :                         Word64 res1 = W_mult0_32_32( output_fx[n][i + delay_comp_DFT], ( sub( delay_diff, i ) ) ); /* Q11 */
    1650      113340 :                         Word64 res2 = W_mult0_32_32( p_output_mem_fx[n][i], i );                                   /* Q11 */
    1651      113340 :                         Word64 res = W_add( W_shr( res1, 6 ), W_shr( res2, 6 ) );                                  /* Q11 - Q6 */
    1652      113340 :                         res = res * tmpF_fx;                                                                       /* Q11 + Q31 - Q6 */
    1653      113340 :                         move64();
    1654      113340 :                         output_fx[n][i + delay_comp_DFT] = W_extract_l( W_shr( res, 31 - 6 ) ); /* Q11 */
    1655      113340 :                         move32();
    1656             :                     }
    1657             :                 }
    1658        1483 :                 SWITCH( output_Fs )
    1659             :                 {
    1660         122 :                     case 16000:
    1661         122 :                         tmpF_fx = 107374184; /* Q31 */
    1662         122 :                         move32();
    1663         122 :                         BREAK;
    1664         298 :                     case 32000:
    1665         298 :                         tmpF_fx = 53687092; /* Q31 */
    1666         298 :                         move32();
    1667         298 :                         BREAK;
    1668        1063 :                     case 48000:
    1669        1063 :                         tmpF_fx = 35791396; /* Q31 */
    1670        1063 :                         move32();
    1671        1063 :                         BREAK;
    1672           0 :                     default:
    1673           0 :                         assert( 0 );
    1674             :                 }
    1675       79623 :                 FOR( i = 0; i < delay_cldfb; i++ )
    1676             :                 {
    1677       78140 :                     Word64 res1 = W_mult0_32_32( tmp_out_fx[n][i], ( sub( delay_cldfb, i ) ) ); /* Q11 */
    1678       78140 :                     Word64 res2 = W_mult0_32_32( output_fx[n][i + delay_comp_TD], i );          /* Q11 */
    1679       78140 :                     Word64 res = W_add( W_shr( res1, 6 ), W_shr( res2, 6 ) );                   /* Q11 - Q6 */
    1680       78140 :                     res = res * tmpF_fx;                                                        /* Q11 + Q31 - Q6 */
    1681       78140 :                     move64();
    1682       78140 :                     output_fx[n][i + delay_comp_TD] = W_extract_l( W_shr( res, 31 - 6 ) ); /* Q11 */
    1683       78140 :                     move32();
    1684             :                 }
    1685             :             }
    1686             :         }
    1687             :     }
    1688             : 
    1689      399502 :     return;
    1690             : }
    1691             : 
    1692             : 
    1693             : /*-------------------------------------------------------------------*
    1694             :  * Function stereo_switching_dec()
    1695             :  *
    1696             :  * Handling of memories in case of CPE modes switching
    1697             :  *-------------------------------------------------------------------*/
    1698             : 
    1699             : static Word32 side_gain_table[32 + 1] = {
    1700             :     -ONE_IN_Q31, -2040109440, -1932735232, -1825361152, -1717986944,
    1701             :     -1610612736, -1503238528, -1395864320, -1288490240, -1181115904, -1073741824,
    1702             :     -966367616, -858993408, -751619200, -644245120, -536870912, -429496704,
    1703             :     -322122496, -214748288, -107374208, 0, 107374336, 214748416,
    1704             :     322122496, 429496832, 536870912, 644245248, 751619328, 858993408,
    1705             :     966367744, 1073741824, 1181116160, 1288490240
    1706             : };
    1707             : 
    1708      410722 : void stereo_switching_dec(
    1709             :     CPE_DEC_HANDLE hCPE,          /* i/o: CPE decoder structure         */
    1710             :     const Word32 ivas_total_brate /* i  : IVAS total bitrate          Q0*/
    1711             : )
    1712             : {
    1713             :     Word16 i, n;
    1714             :     Word16 dft32ms_ovl;
    1715             :     Decoder_State **sts;
    1716             :     Word32 tmpF_fx;
    1717             :     Word16 delay_comp_TD;
    1718             : 
    1719      410722 :     sts = hCPE->hCoreCoder;
    1720             : 
    1721      410722 :     delay_comp_TD = NS2SA_FX2( sts[0]->output_Fs, IVAS_DEC_DELAY_NS - DELAY_CLDFB_NS );
    1722      410722 :     move16();
    1723             : 
    1724             :     /* prevent CPE mode switching in the first received frame */
    1725      410722 :     IF( sts[0]->ini_frame == 0 )
    1726             :     {
    1727        2789 :         hCPE->last_element_mode = hCPE->element_mode; /* Q0 */
    1728        2789 :         move16();
    1729        2789 :         hCPE->stereo_switching_counter = 10; /* Q0 */
    1730        2789 :         move16();
    1731        2789 :         hCPE->NbFrameMod = 7; /* Q0 */
    1732        2789 :         move16();
    1733        2789 :         hCPE->lt_es_em_fx = 0;
    1734        2789 :         move16();
    1735             :     }
    1736             : 
    1737      410722 :     IF( EQ_16( hCPE->element_mode, hCPE->last_element_mode ) )
    1738             :     {
    1739      409187 :         hCPE->stereo_switching_counter = add( hCPE->stereo_switching_counter, 1 );    /* Q0 */
    1740      409187 :         hCPE->stereo_switching_counter = s_min( 10, hCPE->stereo_switching_counter ); /* Q0 */
    1741             :     }
    1742             :     ELSE
    1743             :     {
    1744        1535 :         hCPE->stereo_switching_counter = 0;
    1745        1535 :         move16();
    1746             :     }
    1747             : 
    1748      410722 :     test();
    1749      410722 :     test();
    1750      410722 :     test();
    1751      410722 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && EQ_16( hCPE->nchan_out, 1 ) && GE_32( hCPE->element_brate, IVAS_32k ) && LE_32( hCPE->last_element_brate, IVAS_24k4 ) )
    1752             :     {
    1753          59 :         dft32ms_ovl = extract_l( Mpy_32_32( imult3216( sts[0]->output_Fs, STEREO_DFT32MS_OVL_MAX ), 44740 ) ); // 1/48000 = 44740 (Q31)              /* Q0 */
    1754          59 :         set32_fx( hCPE->output_mem_fx[0], 0, dft32ms_ovl );
    1755             :     }
    1756      410722 :     test();
    1757      410722 :     test();
    1758      410722 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && NE_16( hCPE->last_element_mode, IVAS_CPE_DFT ) && hCPE->hCoreCoder[0]->ini_frame > 0 )
    1759             :     {
    1760             :         /* windowing the OLA memory */
    1761         754 :         dft32ms_ovl = extract_l( Mpy_32_32( imult3216( sts[0]->output_Fs, STEREO_DFT32MS_OVL_MAX ), 44740 ) ); // 1/48000 = 44740 (Q31)              /* Q0 */
    1762        2262 :         FOR( n = 0; n < CPE_CHANNELS; n++ )
    1763             :         {
    1764        1508 :             IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
    1765             :             {
    1766             :                 /* copy memories from previous MDCT Stereo frame to output_mem */
    1767        1400 :                 Copy32( hCPE->input_mem_fx[n], hCPE->output_mem_fx[n], dft32ms_ovl ); /* Q11 */
    1768             :             }
    1769             : 
    1770        1508 :             test();
    1771        1508 :             test();
    1772        1508 :             test();
    1773        1508 :             IF( GT_32( ivas_total_brate, IVAS_SID_5k2 ) || n == 0 || NE_16( hCPE->last_element_mode, IVAS_CPE_TD ) || EQ_16( hCPE->nchan_out, 1 ) )
    1774             :             {
    1775      197394 :                 FOR( i = 0; i < dft32ms_ovl; i++ )
    1776             :                 {
    1777      195900 :                     hCPE->output_mem_fx[n][i] = Mpy_32_16_1( hCPE->output_mem_fx[n][i], hCPE->hStereoDft->win32ms_fx[STEREO_DFT32MS_STEP * ( dft32ms_ovl - 1 - i )] ); /* q_output_mem_fx */
    1778      195900 :                     move32();
    1779             :                 }
    1780             :             }
    1781             :             ELSE
    1782             :             {
    1783          14 :                 IF( EQ_32( hCPE->hStereoTCA->prevTargetGain_fx, ONE_IN_Q29 ) )
    1784             :                 {
    1785          14 :                     tmpF_fx = ONE_IN_Q27; /* Q27 */
    1786          14 :                     move32();
    1787             :                 }
    1788             :                 ELSE
    1789             :                 {
    1790           0 :                     Word16 temp_b = extract_l( L_shr( hCPE->hStereoTCA->prevTargetGain_fx, 16 ) ); /* Q13 */
    1791           0 :                     Word16 temp_b_q = 2;
    1792           0 :                     move16();
    1793           0 :                     tmpF_fx = Inv16( temp_b, &temp_b_q );
    1794           0 :                     tmpF_fx = L_shl( tmpF_fx, sub( sub( 31, 4 ), sub( 15, temp_b_q ) ) ); /* Q27 */
    1795             :                 }
    1796        1614 :                 FOR( i = 0; i < dft32ms_ovl; i++ )
    1797             :                 {
    1798        1600 :                     Word32 temp_result = Mpy_32_16_1( tmpF_fx, hCPE->hStereoDft->win32ms_fx[STEREO_DFT32MS_STEP * ( dft32ms_ovl - 1 - i )] ); /* Q27 */
    1799        1600 :                     hCPE->output_mem_fx[n][i] = L_shl_sat( Mpy_32_32( hCPE->output_mem_fx[n][i], temp_result ), 4 );                          /* q_output_mem_fx */
    1800             :                 }
    1801        1038 :                 FOR( i = 0; i < delay_comp_TD; i++ )
    1802             :                 {
    1803             :                     Word64 temp_result;
    1804        1024 :                     temp_result = W_mult0_32_32( hCPE->hCoreCoder[1]->prev_synth_buffer32_fx[i], tmpF_fx ); /* q_prev_synth + Q27 */
    1805        1024 :                     hCPE->hCoreCoder[1]->prev_synth_buffer32_fx[i] = W_sat_l( W_shr( temp_result, 27 ) );   /* q_prev_synth */
    1806        1024 :                     move32();
    1807             :                 }
    1808        3836 :                 FOR( i = 0; i < L_DEC_MEM_LEN_ICA; i++ )
    1809             :                 {
    1810        3822 :                     hCPE->hStereoTCA->memChanR_fx[i] = L_shl_sat( Mpy_32_32( hCPE->hStereoTCA->memChanR_fx[i], tmpF_fx ), 4 ); /* q_mem_chanR */
    1811        3822 :                     move32();
    1812             :                 }
    1813             :             }
    1814             :         }
    1815             : 
    1816         754 :         IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
    1817             :         {
    1818             :             /* create passive downmix of MDCT Stereo memories FOR DFT input memory */
    1819       35700 :             FOR( Word32 ind = 0; ind < STEREO_DFT32MS_OVL_16k; ind++ )
    1820             :             {
    1821       35000 :                 Word32 result_int = L_add_sat( hCPE->input_mem_LB_fx[0][ind], hCPE->input_mem_LB_fx[1][ind] ); /* Q11 */
    1822       35000 :                 hCPE->input_mem_LB_fx[0][ind] = L_shr( result_int, 1 );                                        /* Q11 */
    1823       35000 :                 move32();
    1824             :             }
    1825             : 
    1826       93950 :             FOR( Word32 ind = 0; ind < dft32ms_ovl; ind++ )
    1827             :             {
    1828       93250 :                 Word32 result_int = L_add_sat( hCPE->input_mem_fx[0][ind], hCPE->input_mem_fx[1][ind] ); /* Q11 */
    1829       93250 :                 hCPE->input_mem_fx[0][ind] = L_shr( result_int, 1 );                                     /* Q11 */
    1830       93250 :                 move32();
    1831             :             }
    1832             : 
    1833         700 :             IF( EQ_16( hCPE->nchan_out, 1 ) )
    1834             :             {
    1835       55561 :                 FOR( Word32 ind = 0; ind < dft32ms_ovl; ind++ )
    1836             :                 {
    1837       55150 :                     Word32 result_int = L_add_sat( hCPE->output_mem_fx[0][ind], hCPE->output_mem_fx[1][ind] ); /* q_output_mem_fx */
    1838       55150 :                     hCPE->output_mem_fx[0][ind] = Mpy_32_32( result_int, INV_SQRT_2_Q31 );                     /* q_output_mem_fx */
    1839       55150 :                     move32();
    1840             :                 }
    1841             :             }
    1842             :         }
    1843             : 
    1844             :         /* Update the side_gain[] parameters */
    1845         754 :         IF( NE_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
    1846             :         {
    1847          54 :             tmpF_fx = 0;
    1848          54 :             move32();
    1849          54 :             if ( hCPE->hStereoTCA != NULL )
    1850             :             {
    1851          28 :                 tmpF_fx = side_gain_table[hCPE->hStereoTCA->indx_ica_gD]; /* Q31 */
    1852          28 :                 move32();
    1853             :             }
    1854          54 :             set32_fx( hCPE->hStereoDft->side_gain_fx + STEREO_DFT_NBDIV * STEREO_DFT_BAND_MAX, tmpF_fx, STEREO_DFT_BAND_MAX );
    1855             :         }
    1856             : 
    1857             :         /* reset residual coding / ESF (secondary channel) */
    1858         754 :         set32_fx( hCPE->hStereoDft->res_cod_mem_fx, 0, STEREO_DFT_OVL_8k );
    1859         754 :         hCPE->hStereoDft->q_res_cod_mem_fx = Q15;
    1860         754 :         move16();
    1861             : 
    1862         754 :         set32_fx( hCPE->input_mem_fx[1], 0, NS2SA_FX2( sts[0]->output_Fs, STEREO_DFT32MS_OVL_NS ) );
    1863             :     }
    1864             : 
    1865      410722 :     test();
    1866      410722 :     test();
    1867      410722 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && NE_16( hCPE->last_element_mode, IVAS_CPE_TD ) && hCPE->hCoreCoder[0]->ini_frame > 0 )
    1868             :     {
    1869          56 :         hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_MID_IS_PRIM; /* Q0 */
    1870          56 :         move16();
    1871          56 :         hCPE->hStereoTD->tdm_last_SM_flag = 0;
    1872          56 :         move16();
    1873          56 :         hCPE->hStereoTD->tdm_prev_last_SM_flag = 0;
    1874          56 :         move16();
    1875             : 
    1876             :         /* First frame after DFT frame AND the content is uncorrelated or xtalk -> the primary channel is forced to left */
    1877          56 :         if ( EQ_16( hCPE->hStereoTD->tdm_LRTD_flag, 1 ) )
    1878             :         {
    1879          56 :             hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_LEFT_IS_PRIM; /* Q0 */
    1880          56 :             move16();
    1881             :         }
    1882             :     }
    1883             : 
    1884             :     /* no secondary channel in the previous frame -> memory resets */
    1885      410722 :     test();
    1886      410722 :     test();
    1887      410722 :     IF( GT_16( hCPE->element_mode, IVAS_CPE_DFT ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
    1888             :     {
    1889         771 :         test();
    1890         771 :         IF( LE_32( hCPE->last_element_brate, IVAS_SID_5k2 ) && EQ_16( hCPE->nchan_out, 2 ) )
    1891             :         {
    1892             :             /* reset CLDFB memories */
    1893             : 
    1894           7 :             cldfb_reset_memory_fx( sts[0]->cldfbAna );
    1895           7 :             cldfb_reset_memory_fx( sts[0]->cldfbBPF );
    1896           7 :             cldfb_reset_memory_fx( sts[0]->cldfbSyn );
    1897             : 
    1898           7 :             sts[0]->mem_deemph_fx = 0;
    1899           7 :             move16();
    1900             : 
    1901             : 
    1902           7 :             sts[0]->tilt_code_fx = 0;
    1903           7 :             move16();
    1904           7 :             sts[0]->gc_threshold_fx = 0;
    1905           7 :             move16();
    1906             : 
    1907             : 
    1908           7 :             set16_fx( sts[0]->mem_syn1_fx, 0, M );
    1909           7 :             set16_fx( sts[0]->mem_syn2_fx, 0, M );
    1910           7 :             set16_fx( sts[0]->mem_syn3_fx, 0, M );
    1911           7 :             set16_fx( sts[0]->mem_syn_r, 0, L_SYN_MEM );
    1912             : 
    1913           7 :             sts[1]->last_L_frame = sts[0]->last_L_frame; /* Q0 */
    1914           7 :             move16();
    1915             : 
    1916             : 
    1917           7 :             set16_fx( sts[0]->old_exc_fx, 0, L_EXC_MEM_DEC );
    1918           7 :             set16_fx( sts[0]->lsf_old_fx, 0, M );
    1919           7 :             set16_fx( sts[0]->lsp_old_fx, 0, M );
    1920             :         }
    1921         771 :         sts[1]->last_extl = -1;
    1922         771 :         move16();
    1923             : 
    1924         771 :         sts[1]->prev_bfi = sts[0]->prev_bfi; /* Q0 */
    1925         771 :         move16();
    1926             : 
    1927             : 
    1928             :         // fix need to do correctly
    1929         771 :         set32_fx( sts[1]->old_pitch_buf_fx, L_deposit_h( L_SUBFR ), 2 * NB_SUBFR16k );
    1930         771 :         sts[1]->old_fpitchFB = L_deposit_h( 2 * L_SUBFR ); /* 15Q16 */
    1931         771 :         move32();
    1932             : 
    1933             :         /* reset CLDFB memories */
    1934             : 
    1935         771 :         cldfb_reset_memory_fx( sts[1]->cldfbAna );
    1936         771 :         cldfb_reset_memory_fx( sts[1]->cldfbBPF );
    1937         771 :         cldfb_reset_memory_fx( sts[1]->cldfbSyn );
    1938             : 
    1939         771 :         sts[1]->mem_deemph_fx = 0;
    1940         771 :         move16();
    1941             : 
    1942         771 :         sts[1]->tilt_code_fx = 0;
    1943         771 :         move16();
    1944         771 :         sts[1]->gc_threshold_fx = 0;
    1945         771 :         move16();
    1946             : 
    1947             : 
    1948         771 :         set16_fx( sts[1]->mem_syn1_fx, 0, M );
    1949         771 :         set16_fx( sts[1]->mem_syn2_fx, 0, M );
    1950         771 :         set16_fx( sts[1]->mem_syn3_fx, 0, M );
    1951         771 :         set16_fx( sts[1]->mem_syn_r, 0, L_SYN_MEM );
    1952             : 
    1953         771 :         sts[1]->last_L_frame = sts[0]->last_L_frame; /* Q0 */
    1954         771 :         move16();
    1955             :         /* Should not force sts[1]->Q_exc on the data of st[0], instead it better to just reuse the current scaling of st[0]*/
    1956         771 :         Copy( sts[0]->old_exc_fx, sts[1]->old_exc_fx, L_EXC_MEM_DEC ); /* Q_exc */
    1957         771 :         sts[1]->Q_exc = sts[0]->Q_exc;
    1958         771 :         move16();
    1959         771 :         sts[1]->Q_exc_cng = sts[0]->Q_exc_cng;
    1960         771 :         move16();
    1961         771 :         sts[1]->prev_Q_exc = sts[0]->prev_Q_exc;
    1962         771 :         move16();
    1963         771 :         Copy( sts[0]->Q_subfr, sts[1]->Q_subfr, L_Q_MEM );
    1964             : 
    1965         771 :         test();
    1966         771 :         IF( sts[0]->hBWE_TD != NULL && sts[1]->hBWE_TD != NULL )
    1967             :         {
    1968         146 :             sts[1]->hBWE_TD->prev_Q_bwe_syn = sts[0]->hBWE_TD->prev_Q_bwe_syn;
    1969         146 :             move16();
    1970         146 :             sts[1]->hBWE_TD->prev_Q_bwe_syn2 = sts[0]->hBWE_TD->prev_Q_bwe_syn2;
    1971         146 :             move16();
    1972             : 
    1973         146 :             sts[1]->hBWE_TD->prev_Q_bwe_exc_fb = sts[0]->hBWE_TD->prev_Q_bwe_exc_fb;
    1974         146 :             move16();
    1975         146 :             sts[1]->hBWE_TD->prev_Qx = sts[0]->hBWE_TD->prev_Qx;
    1976         146 :             move16();
    1977             :         }
    1978             : 
    1979         771 :         test();
    1980         771 :         IF( sts[0]->hBWE_FD != NULL && sts[1]->hBWE_FD != NULL )
    1981             :         {
    1982         146 :             sts[1]->hBWE_FD->prev_Q_synth = sts[0]->hBWE_FD->prev_Q_synth;
    1983         146 :             move16();
    1984             :         }
    1985             : 
    1986         771 :         sts[1]->prev_Q_bwe_exc = sts[0]->prev_Q_bwe_exc;
    1987         771 :         move16();
    1988         771 :         sts[1]->Q_syn = sts[0]->Q_syn;
    1989         771 :         move16();
    1990         771 :         sts[1]->Q_syn2 = sts[0]->Q_syn2;
    1991         771 :         move16();
    1992         771 :         sts[1]->Q_syn_cng = sts[0]->Q_syn_cng;
    1993         771 :         move16();
    1994         771 :         sts[1]->prev_Q_syn = sts[0]->prev_Q_syn;
    1995         771 :         move16();
    1996             : 
    1997         771 :         Copy( sts[0]->lsf_old_fx, sts[1]->lsf_old_fx, M ); /* Q2.56 */
    1998         771 :         Copy( sts[0]->lsp_old_fx, sts[1]->lsp_old_fx, M ); /* Q15 */
    1999             : 
    2000             : #ifdef NONBE_FIX_1110_STEREO_DTX_BRATE_SWITCHING
    2001         771 :         sts[1]->last_core_brate = sts[0]->last_core_brate;
    2002         771 :         move16();
    2003             : #endif
    2004             : 
    2005         771 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
    2006             :         {
    2007         723 :             sts[1]->last_core = sts[0]->last_core; /* Q0 */
    2008         723 :             move16();
    2009         723 :             sts[1]->last_coder_type = sts[0]->last_coder_type; /* Q0 */
    2010         723 :             move16();
    2011             : 
    2012             :             // 32bit buffer
    2013         723 :             Copy32( sts[0]->hHQ_core->old_out_fx32, sts[1]->hHQ_core->old_out_fx32, L_FRAME48k );           /* exp(exp_old_out) */
    2014         723 :             Copy32( sts[0]->delay_buf_out32_fx, sts[1]->delay_buf_out32_fx, HQ_DELTA_MAX * HQ_DELAY_COMP ); /* Q11 */
    2015         723 :             Copy32( sts[0]->hTcxDec->old_syn_Overl_32, sts[1]->hTcxDec->old_syn_Overl_32, 256 );            /* Q_old_syn_Overl*/
    2016             :             // 16 bit buffer
    2017         723 :             Copy( sts[0]->hHQ_core->old_out_fx, sts[1]->hHQ_core->old_out_fx, L_FRAME48k ); /* exp(exp_old_out) */
    2018             : 
    2019         723 :             sts[1]->hHQ_core->Q_old_wtda = sts[0]->hHQ_core->Q_old_wtda;
    2020         723 :             move16();
    2021         723 :             Copy( sts[0]->delay_buf_out_fx, sts[1]->delay_buf_out_fx, HQ_DELTA_MAX * HQ_DELAY_COMP ); /* Q0 */
    2022         723 :             Copy( sts[0]->hTcxDec->old_syn_Overl, sts[1]->hTcxDec->old_syn_Overl, 256 );              /* Q_old_syn_Overl*/
    2023         723 :             sts[1]->hTcxDec->Q_old_syn_Overl = sts[0]->hTcxDec->Q_old_syn_Overl;
    2024             :         }
    2025             :     }
    2026      409951 :     ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
    2027             :     {
    2028           8 :         set16_fx( sts[0]->old_exc_fx, 0, L_EXC_MEM_DEC );
    2029           8 :         set16_fx( sts[1]->old_exc_fx, 0, L_EXC_MEM_DEC );
    2030             :     }
    2031             : 
    2032             :     /* TD/DFT -> MDCT stereo switching (there is no TCX in the TD stereo secondary channel, or DFT stereo) */
    2033      410722 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && NE_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
    2034             :     {
    2035         725 :         sts[1]->hTcxCfg->last_aldo = sts[0]->hTcxCfg->last_aldo; /* Q0 */
    2036         725 :         move16();
    2037         725 :         sts[1]->hTcxCfg->tcx_curr_overlap_mode = sts[0]->hTcxCfg->tcx_curr_overlap_mode; /* Q0 */
    2038         725 :         move16();
    2039         725 :         sts[1]->fscale = sts[0]->fscale; /* Q0 */
    2040         725 :         move16();
    2041         725 :         sts[1]->hTcxCfg->tcx_mdct_window_length = sts[0]->hTcxCfg->tcx_mdct_window_length; /* Q0 */
    2042         725 :         move16();
    2043         725 :         sts[1]->pit_res_max = sts[0]->pit_res_max; /* Q0 */
    2044         725 :         move16();
    2045         725 :         sts[1]->pit_res_max_past = sts[0]->pit_res_max_past; /* Q0 */
    2046         725 :         move16();
    2047         725 :         sts[1]->hTcxDec->L_frameTCX = sts[0]->hTcxDec->L_frameTCX; /* Q0 */
    2048         725 :         move16();
    2049         725 :         sts[1]->hTcxDec->conceal_eof_gain = sts[0]->hTcxDec->conceal_eof_gain; /* Q14 */
    2050         725 :         move16();
    2051         725 :         sts[1]->hTcxDec->conceal_eof_gain32 = sts[0]->hTcxDec->conceal_eof_gain32; /* sts[0]->hTcxDec->conceal_eof_gain_e */
    2052         725 :         move32();
    2053         725 :         sts[1]->hTcxDec->conceal_eof_gain_e = sts[0]->hTcxDec->conceal_eof_gain_e;
    2054         725 :         move16();
    2055             :     }
    2056             : 
    2057      410722 :     return;
    2058             : }
    2059             : 
    2060             : 
    2061             : /*-------------------------------------------------------------------*
    2062             :  * Function stereo_td2dft_update()
    2063             :  *
    2064             :  * update OLA buffers - needed for switching from TD stereo to DFT stereo
    2065             :  *-------------------------------------------------------------------*/
    2066             : 
    2067      193546 : void stereo_td2dft_update_fx(
    2068             :     CPE_DEC_HANDLE hCPE,      /* i/o: CPE decoder structure     */
    2069             :     const Word16 n,           /* i  : channel number          Q0*/
    2070             :     Word32 output_fx[],       /* i/o: synthesis @internal Fs  Q11*/
    2071             :     Word32 synth_fx[],        /* i/o: synthesis @output Fs    Q11*/
    2072             :     Word32 hb_synth_fx[],     /* i/o: hb synthesis            Q11*/
    2073             :     const Word16 output_frame /* i  : frame length            Q0*/
    2074             : )
    2075             : {
    2076             :     Word16 ovl, ovl_TCX, dft32ms_ovl, hq_delay_comp;
    2077             :     Word16 ns, nsLB, i;
    2078             :     Word16 old_out_len, old_outLB_len;
    2079             :     Decoder_State **sts;
    2080             : 
    2081      193546 :     IF( hCPE == NULL )
    2082             :     {
    2083           0 :         return;
    2084             :     }
    2085             : 
    2086             :     /* initialization */
    2087      193546 :     sts = hCPE->hCoreCoder;
    2088      193546 :     ovl = NS2SA_FX2( i_mult( sts[n]->L_frame, FRAMES_PER_SEC ), STEREO_DFT32MS_OVL_NS ); /* Q0 */
    2089      193546 :     move16();
    2090      193546 :     dft32ms_ovl = extract_l( Mpy_32_32( imult3216( sts[0]->output_Fs, STEREO_DFT32MS_OVL_MAX ), 44740 ) ); // 1/48000 = 44740 (Q31)          /* Q0 */
    2091      193546 :     move16();
    2092      193546 :     hq_delay_comp = NS2SA_FX2( sts[0]->output_Fs, DELAY_CLDFB_NS ); /* Q0 */
    2093      193546 :     move16();
    2094             : 
    2095      193546 :     test();
    2096      193546 :     test();
    2097      193546 :     IF( GE_16( hCPE->element_mode, IVAS_CPE_DFT ) && NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
    2098             :     {
    2099       24422 :         IF( sts[n]->core == ACELP_CORE )
    2100             :         {
    2101       22036 :             IF( n == 0 )
    2102             :             {
    2103             :                 /* update DFT analysis overlap memory @internal_fs: core synthesis */
    2104       18350 :                 Copy32( output_fx + sts[n]->L_frame - ovl, hCPE->input_mem_LB_fx[n], ovl ); /* Q11 */
    2105             : 
    2106             :                 /* update DFT analysis overlap memory @internal_fs: BPF */
    2107       18350 :                 IF( sts[n]->p_bpf_noise_buf_32 )
    2108             :                 {
    2109       16185 :                     Copy32( sts[n]->p_bpf_noise_buf_32 + sub( sts[n]->L_frame, ovl ), hCPE->input_mem_BPF_fx[n], ovl ); /* Q11 */
    2110             :                 }
    2111             : 
    2112             : 
    2113             :                 /* update DFT analysis overlap memory @output_Fs: BWE */
    2114       18350 :                 test();
    2115       18350 :                 test();
    2116       18350 :                 IF( NE_16( sts[n]->extl, -1 ) || ( sts[n]->bws_cnt > 0 && sts[n]->core == ACELP_CORE ) )
    2117             :                 {
    2118       15083 :                     Copy32( hb_synth_fx + sub( output_frame, dft32ms_ovl ), hCPE->input_mem_fx[n], dft32ms_ovl ); /* Q11 */
    2119             :                 }
    2120             :             }
    2121             :             ELSE
    2122             :             {
    2123             :                 /* update DFT analysis overlap memory @internal_fs: core synthesis, secondary channel */
    2124        3686 :                 Copy32( output_fx + sub( sts[n]->L_frame, ovl ), hCPE->input_mem_LB_fx[n], ovl ); /* Q11 */
    2125             :             }
    2126             :         }
    2127             :         ELSE /* TCX core */
    2128             :         {
    2129             :             /* LB-TCX synthesis */
    2130        2386 :             Copy32( output_fx + sub( sts[n]->L_frame, ovl ), hCPE->input_mem_LB_fx[n], ovl ); /* Q11 */
    2131             : 
    2132             :             /* BPF */
    2133        2386 :             IF( n == 0 && sts[n]->p_bpf_noise_buf_32 )
    2134             :             {
    2135        2386 :                 Copy32( sts[n]->p_bpf_noise_buf_32 + sub( sts[n]->L_frame, ovl ), hCPE->input_mem_BPF_fx[n], ovl ); /* Q11 */
    2136             :             }
    2137             : 
    2138             :             /* TCX synthesis (it was already delayed in TD stereo in core_switching_post_dec()) */
    2139        2386 :             IF( sts[n]->hTcxDec != NULL )
    2140             :             {
    2141        2386 :                 ovl_TCX = NS2SA_FX2( L_mult0( sts[n]->hTcxDec->L_frameTCX, FRAMES_PER_SEC ), STEREO_DFT32MS_OVL_NS ); /* Q0 */
    2142        2386 :                 move16();
    2143        2386 :                 Copy32( synth_fx + sts[n]->hTcxDec->L_frameTCX + hq_delay_comp - ovl_TCX, hCPE->input_mem_fx[n], sub( ovl_TCX, hq_delay_comp ) ); /* Q11 */
    2144        2386 :                 Copy32( sts[n]->delay_buf_out32_fx, hCPE->input_mem_fx[n] + ovl_TCX - hq_delay_comp, hq_delay_comp );                             /* Q11 */
    2145             :             }
    2146             :         }
    2147             :     }
    2148      169124 :     ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && hCPE->input_mem_fx[0] != NULL )
    2149             :     {
    2150             : 
    2151             :         /* update DFT stereo OLA memories */
    2152             :         /*set_zero( hCPE->input_mem_LB[n], STEREO_DFT32MS_OVL_16k );*/
    2153       70170 :         L_lerp_fx_q11( output_fx + sub( sts[n]->L_frame, ovl ), hCPE->input_mem_LB_fx[n], STEREO_DFT32MS_OVL_16k, ovl );
    2154             : 
    2155             : 
    2156             :         /* TCX synthesis (it was already delayed in TD stereo in core_switching_post_dec()) */
    2157       70170 :         IF( sts[n]->hTcxDec != NULL )
    2158             :         {
    2159       70170 :             ovl_TCX = NS2SA_FX2( L_mult0( sts[n]->hTcxDec->L_frameTCX, FRAMES_PER_SEC ), STEREO_DFT32MS_OVL_NS ); /* Q0 */
    2160       70170 :             move16();
    2161       70170 :             Copy32( synth_fx + add( sts[n]->hTcxDec->L_frameTCX, sub( hq_delay_comp, ovl_TCX ) ), hCPE->input_mem_fx[n], sub( ovl_TCX, hq_delay_comp ) ); /* Q11 */
    2162       70170 :             Copy32( sts[n]->delay_buf_out32_fx, hCPE->input_mem_fx[n] + sub( ovl_TCX, hq_delay_comp ), hq_delay_comp );                                   /* Q11 */
    2163             :         }
    2164             : 
    2165       70170 :         IF( EQ_16( n, 1 ) )
    2166             :         {
    2167       35085 :             nsLB = NS2SA_FX2( L_mult0( sts[n]->L_frame, FRAMES_PER_SEC ), N_ZERO_MDCT_NS ); /* Q0 */
    2168       35085 :             move16();
    2169       35085 :             ns = NS2SA_FX2( sts[n]->output_Fs, N_ZERO_MDCT_NS ); /* Q0 */
    2170       35085 :             move16();
    2171       35085 :             old_outLB_len = extract_l( Mpy_32_32( ( L_mult0( STEREO_MDCT2DFT_FADE_LEN_48k * FRAMES_PER_SEC, i_mult( 3, sts[0]->L_frame ) ) ), 44740 ) ); /* Q0 */
    2172       35085 :             move16();
    2173       35085 :             old_out_len = extract_l( Mpy_32_32( imult3216( sts[0]->output_Fs, STEREO_MDCT2DFT_FADE_LEN_48k ), 44740 ) ); /* Q0 */
    2174       35085 :             move16();
    2175             : 
    2176             :             /* update buffers used for fading when switching to DFT Stereo */
    2177       35085 :             assert( sts[0]->hHQ_core->Q_old_out_fx32 == sts[1]->hHQ_core->Q_old_out_fx32 );
    2178             : 
    2179       35085 :             v_add_fx( sts[0]->hHQ_core->old_out_LB_fx32 + nsLB, sts[1]->hHQ_core->old_out_LB_fx32 + nsLB, hCPE->old_outLB_mdct_fx, old_outLB_len );
    2180       35085 :             L_lerp_fx_q11( hCPE->old_outLB_mdct_fx, hCPE->old_outLB_mdct_fx, STEREO_MDCT2DFT_FADE_LEN_48k, old_outLB_len );
    2181     6463029 :             FOR( i = 0; i < old_outLB_len; i++ )
    2182             :             {
    2183     6427944 :                 hCPE->old_outLB_mdct_fx[i] = L_shr( hCPE->old_outLB_mdct_fx[i], 1 ); /* Q11 */
    2184     6427944 :                 move32();
    2185             :             }
    2186       35085 :             v_add_fx( sts[0]->hHQ_core->old_out_fx32 + ns, sts[1]->hHQ_core->old_out_fx32 + ns, hCPE->old_out_mdct_fx, old_out_len ); /* exp(exp_old_out) */
    2187     3154845 :             FOR( i = 0; i < old_out_len; i++ )
    2188             :             {
    2189     3119760 :                 hCPE->old_out_mdct_fx[i] = L_shr( hCPE->old_out_mdct_fx[i], 1 ); /* q_old_out_mdct */
    2190     3119760 :                 move32();
    2191             :             }
    2192             :         }
    2193             : 
    2194       70170 :         IF( n == 0 )
    2195             :         {
    2196       35085 :             set32_fx( hCPE->input_mem_BPF_fx[n], 0, STEREO_DFT32MS_OVL_16k );
    2197             :         }
    2198             :     }
    2199             : 
    2200             :     /* update ovl buffer for possible switching from TD stereo SCh ACELP frame to MDCT stereo TCX frame */
    2201      193546 :     test();
    2202      193546 :     test();
    2203      193546 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && EQ_16( n, 1 ) && sts[n]->hTcxDec == NULL )
    2204             :     {
    2205        3686 :         Copy32( output_fx + shr( sts[n]->L_frame, 1 ), hCPE->hStereoTD->TCX_old_syn_Overl_fx, shr( sts[n]->L_frame, 1 ) ); /* Q11*/
    2206             :     }
    2207             : 
    2208      193546 :     return;
    2209             : }
    2210             : 
    2211             : 
    2212             : /*-------------------------------------------------------------------*
    2213             :  * Function stereo_mdct2dft_update()
    2214             :  *
    2215             :  * update OLA buffers - needed for switching from MDCT stereo to DFT stereo
    2216             :  *-------------------------------------------------------------------*/
    2217             : 
    2218         700 : void stereo_mdct2dft_update_fx(
    2219             :     CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure           */
    2220             :     Word32 output0_fx[], /* i/o: synthesis @internal Fs, ch0  Q11*/
    2221             :     Word32 synth0_fx[]   /* i/o: synthesis @output Fs, ch0    Q11*/
    2222             : )
    2223             : {
    2224             :     Word16 i;
    2225             :     Word16 fade_len, fade_len_LB;
    2226         700 :     Word32 tmpF_fx = 0;
    2227             :     Decoder_State *st;
    2228             : 
    2229         700 :     IF( hCPE == NULL )
    2230             :     {
    2231           0 :         return;
    2232             :     }
    2233             : 
    2234         700 :     st = hCPE->hCoreCoder[0];
    2235             : 
    2236         700 :     fade_len = extract_l( Mpy_32_32( imult3216( st->output_Fs, STEREO_MDCT2DFT_FADE_LEN_48k ), 44740 ) );                       // 1/48000 = 44740 (Q31)     /* Q0 */
    2237         700 :     fade_len_LB = extract_l( Mpy_32_32( imult3216( 3 * STEREO_MDCT2DFT_FADE_LEN_48k * FRAMES_PER_SEC, st->L_frame ), 44740 ) ); // 1/48000 = 44740 (Q31)     /* Q0 */
    2238             : 
    2239         700 :     SWITCH( st->output_Fs )
    2240             :     {
    2241          51 :         case 16000:
    2242          51 :             tmpF_fx = 53687092; /* Q31 */
    2243          51 :             move32();
    2244          51 :             BREAK;
    2245         133 :         case 32000:
    2246         133 :             tmpF_fx = 26843546; /* Q31 */
    2247         133 :             move32();
    2248         133 :             BREAK;
    2249         516 :         case 48000:
    2250         516 :             tmpF_fx = 17895698; /* Q31 */
    2251         516 :             move32();
    2252         516 :             BREAK;
    2253             :     }
    2254             : 
    2255       75300 :     FOR( i = 0; i < fade_len; i++ )
    2256             :     {
    2257             :         Word32 descen_gain;
    2258       74600 :         IF( EQ_16( i, 0 ) )
    2259             :         {
    2260         700 :             descen_gain = ONE_IN_Q31; /* Q31 */
    2261         700 :             move32();
    2262             :         }
    2263             :         ELSE
    2264             :         {
    2265       73900 :             descen_gain = W_extract_l( W_mult0_32_32( ( sub( fade_len, i ) ), tmpF_fx ) ); /* Q31 */
    2266             :         }
    2267             : 
    2268       74600 :         Word32 temp_a = Mpy_32_32( hCPE->old_out_mdct_fx[i], descen_gain ); /* Q11 */
    2269       74600 :         Word32 ascend_gain = W_extract_l( W_mult0_32_32( i, tmpF_fx ) );    /* Q31 */
    2270       74600 :         Word32 temp_b = Mpy_32_32( synth0_fx[i], ascend_gain );             /* Q11 */
    2271       74600 :         synth0_fx[i] = L_add_sat( temp_a, temp_b );                         /* Q11 */
    2272       74600 :         move32();
    2273             :     }
    2274             : 
    2275         700 :     SWITCH( st->L_frame )
    2276             :     {
    2277           0 :         case 80:
    2278           0 :             tmpF_fx = 71582792; /* Q31 */
    2279           0 :             move32();
    2280           0 :             BREAK;
    2281           0 :         case 160:
    2282           0 :             tmpF_fx = 35791396; /* Q31 */
    2283           0 :             move32();
    2284           0 :             BREAK;
    2285         422 :         case 256:
    2286         422 :             tmpF_fx = 22369622; /* Q31 */
    2287         422 :             move32();
    2288         422 :             BREAK;
    2289         278 :         case 320:
    2290         278 :             tmpF_fx = 17895698; /* Q31 */
    2291         278 :             move32();
    2292         278 :             BREAK;
    2293           0 :         case 512:
    2294           0 :             tmpF_fx = 11184811; /* Q31 */
    2295           0 :             move32();
    2296           0 :             BREAK;
    2297           0 :         case 640:
    2298           0 :             tmpF_fx = 8947849; /* Q31 */
    2299           0 :             move32();
    2300           0 :             BREAK;
    2301           0 :         case 960:
    2302           0 :             tmpF_fx = 5965232; /* Q31 */
    2303           0 :             move32();
    2304           0 :             BREAK;
    2305           0 :         default:
    2306           0 :             assert( 0 );
    2307             :     }
    2308       74572 :     FOR( i = 0; i < fade_len_LB; i++ )
    2309             :     {
    2310             :         Word32 descen_gain;
    2311       73872 :         IF( i == 0 )
    2312             :         {
    2313         700 :             descen_gain = ONE_IN_Q31; /* Q31 */
    2314         700 :             move32();
    2315             :         }
    2316             :         ELSE
    2317             :         {
    2318       73172 :             descen_gain = W_extract_l( W_mult0_32_32( sub( fade_len_LB, i ), tmpF_fx ) ); /* Q31 */
    2319             :         }
    2320       73872 :         Word32 temp_a = Mpy_32_32( hCPE->old_outLB_mdct_fx[i], descen_gain ); /* Q11 */
    2321       73872 :         Word32 ascend_gain = W_extract_l( W_mult0_32_32( i, tmpF_fx ) );      /* Q31 */
    2322       73872 :         Word32 temp_b = Mpy_32_32( output0_fx[i], ascend_gain );              /* Q11 */
    2323       73872 :         output0_fx[i] = L_add_sat( temp_a, temp_b );                          /* Q11 */
    2324             :     }
    2325             : 
    2326         700 :     return;
    2327             : }
    2328             : 
    2329             : 
    2330         320 : static Word32 ncross_corr_self_fx(
    2331             :     Word32 *signal_fx,       /* Q11*/
    2332             :     const Word16 x,          /* Q0 */
    2333             :     const Word16 y,          /* Q0 */
    2334             :     const Word16 corr_len,   /* Q0 */
    2335             :     const Word16 subsampling /* Q0 */
    2336             : )
    2337             : {
    2338             :     Word64 c_c_fx;
    2339             :     Word32 c_c_fx_return;
    2340             :     Word64 energy_x_fx, energy_y_fx;
    2341             :     UWord16 j;
    2342             :     Word32 *signal_a_fx, *signal_b_fx;
    2343             :     Word32 temp_x, temp_y;
    2344             :     Word16 headroom_left_x, headroom_left_y;
    2345         320 :     c_c_fx = 0;
    2346         320 :     move64();
    2347         320 :     energy_x_fx = 0;
    2348         320 :     move64();
    2349         320 :     energy_y_fx = 0;
    2350         320 :     move64();
    2351         320 :     signal_a_fx = &signal_fx[x]; /* Q11 */
    2352         320 :     signal_b_fx = &signal_fx[y]; /* Q11 */
    2353        2880 :     FOR( j = 0; j < corr_len; j += subsampling )
    2354             :     {
    2355        2560 :         c_c_fx = W_mac_32_32( c_c_fx, signal_a_fx[j], signal_b_fx[j] );           /* 2 * Q11 + 1*/
    2356        2560 :         energy_x_fx = W_mac_32_32( energy_x_fx, signal_a_fx[j], signal_a_fx[j] ); /* 2 * Q11+ 1 */
    2357        2560 :         energy_y_fx = W_mac_32_32( energy_y_fx, signal_b_fx[j], signal_b_fx[j] ); /* 2 * Q11+ 1 */
    2358             :     }
    2359             : 
    2360         320 :     headroom_left_x = W_norm( energy_x_fx );
    2361         320 :     headroom_left_y = W_norm( energy_y_fx );
    2362         320 :     temp_x = W_extract_h( W_shl( energy_x_fx, headroom_left_x ) ); // Q23 + headroom_left_x -32
    2363         320 :     temp_y = W_extract_h( W_shl( energy_y_fx, headroom_left_y ) ); // Q23 + headroom_left_y -32
    2364         320 :     Word64 prod = W_mult0_32_32( temp_x, temp_y );                 // Q(headroom_left_x + headroom_left_y - 18)
    2365         320 :     Word16 q_prod = W_norm( prod );
    2366         320 :     Word32 energy = W_extract_h( W_shl( prod, q_prod ) ); // Q(headroom_left_x + headroom_left_y + q_prod - 18) - 32
    2367         320 :     q_prod = sub( 81, add( add( headroom_left_x, headroom_left_y ), q_prod ) );
    2368         320 :     energy = Sqrt32( energy, &q_prod );
    2369             : 
    2370         320 :     IF( LE_32( energy, L_shl_sat( 1, sub( 31, q_prod ) ) ) )
    2371             :     {
    2372           0 :         c_c_fx_return = W_shl_sat_l( c_c_fx, 31 - ( 2 * OUTPUT_Q + 1 ) ); // Q31
    2373             :     }
    2374             :     ELSE
    2375             :     {
    2376             :         // Maximize c_c_fx
    2377         320 :         Word16 q_cc = W_norm( c_c_fx );
    2378         320 :         Word32 num = W_extract_h( W_shl( c_c_fx, q_cc ) ); // Q(23 + q_cc - 32) -> e(40 - q_cc)
    2379             :         Word16 quo_e;
    2380         320 :         num = BASOP_Util_Divide3232_Scale_newton( num, energy, &quo_e );
    2381         320 :         quo_e = add( sub( sub( 40, q_cc ), q_prod ), quo_e );
    2382         320 :         c_c_fx_return = L_shl_sat( num, quo_e ); // Q31
    2383             :     }
    2384             : 
    2385         320 :     return c_c_fx_return;
    2386             : }
    2387             : 
    2388             : 
    2389             : /*-------------------------------------------------------------------*
    2390             :  * Function smooth_dft2td_transition()
    2391             :  *
    2392             :  * apply smoothing to avoid discontinuities and energy variation when
    2393             :  * switching from DFT stereo to TD stereo
    2394             :  *-------------------------------------------------------------------*/
    2395             : 
    2396      149278 : void smooth_dft2td_transition_fx(
    2397             :     CPE_DEC_HANDLE hCPE,             /* i/o: CPE decoder structure                       */
    2398             :     Word32 *output_fx[CPE_CHANNELS], /* i/o: synthesis @external Fs       Q11*/
    2399             :     const Word16 output_frame        /* i  : output frame lenght           Q0*/
    2400             : )
    2401             : {
    2402             : 
    2403             :     Decoder_State **sts;
    2404             :     Word16 ipit, i, ipit_max, ipit_min, idiff, ilen, j, ch, lsearch, corr_len, subsampl;
    2405             :     Word32 flen_fx, ftmp_corr_fx, fmaxcorr_fx, fac_fs_fx;
    2406             :     Word32 tmp_out_fx[L_FRAME48k + L_FRAME48k / 2], tmp_out2_fx[L_FRAME48k], *ptO2_fx, *ptO_fx, *pt1_fx, *ptE_fx;
    2407             : 
    2408      149278 :     IF( hCPE == NULL )
    2409             :     {
    2410           0 :         return;
    2411             :     }
    2412             : 
    2413             :     /* initialization */
    2414      149278 :     sts = hCPE->hCoreCoder;
    2415      149278 :     test();
    2416      149278 :     test();
    2417      149278 :     test();
    2418      149278 :     test();
    2419      149278 :     test();
    2420      149278 :     test();
    2421      149278 :     test();
    2422      149278 :     IF( ( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && ( EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) || EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) ) && ( EQ_16( sts[0]->clas_dec, VOICED_CLAS ) && LT_16( sts[0]->coder_type, TRANSITION ) && GT_16( sts[0]->coder_type, UNVOICED ) && GT_16( sts[0]->last_coder_type, UNVOICED ) && GT_16( sts[1]->coder_type, UNVOICED ) ) ) )
    2423             :     {
    2424             :         /* length of OVA */
    2425           1 :         ilen = shr( output_frame, 1 ); /* Q0 */
    2426             : 
    2427             :         /* correlation length */
    2428           1 :         corr_len = idiv1616( output_frame, 20 ); /* Q0 */
    2429           1 :         subsampl = 4;
    2430             : 
    2431           3 :         FOR( ch = 0; ch < hCPE->nchan_out; ch++ )
    2432             :         {
    2433             :             /* core to external sampling frequency ratio */
    2434           2 :             Word16 q = norm_l( output_frame );
    2435           2 :             Word32 check = BASOP_Util_Divide3232_Scale( output_frame, sts[ch]->L_frame, &q ); /* q */
    2436           2 :             fac_fs_fx = imult3216( check, shl( 1, add( 26 - 15, q ) ) );                      /* Q11 */
    2437             :             /* Find minimum and maximum pitch*/
    2438           2 :             ipit_min = add( minimum_32_fx( sts[ch]->old_pitch_buf_fx + 4, 4, &flen_fx ), 4 );                                  /* Q0 */
    2439           2 :             ipit_max = add( maximum_32_fx( sts[ch]->old_pitch_buf_fx + 4, 4, &flen_fx ), 4 );                                  /* Q0 */
    2440           2 :             ipit_min = (Word16) L_shr( L_add( Mpy_32_32( sts[ch]->old_pitch_buf_fx[ipit_min], fac_fs_fx ), ONE_IN_Q10 ), 11 ); /* Q0 */
    2441           2 :             ipit_max = (Word16) L_shr( L_add( Mpy_32_32( sts[ch]->old_pitch_buf_fx[ipit_max], fac_fs_fx ), ONE_IN_Q10 ), 11 ); /* Q0 */
    2442             : 
    2443           2 :             IF( GT_16( add( ipit_max, corr_len ), ilen ) ) /*ensure the search is performed on the available memory*/
    2444             :             {
    2445           1 :                 CONTINUE;
    2446             :             }
    2447             : 
    2448           1 :             lsearch = add( sub( ipit_max, ipit_min ), corr_len ); /* Q0 */
    2449           1 :             lsearch = s_min( lsearch, shr( output_frame, 2 ) );   /* Q0 */
    2450             : 
    2451             :             /* ptr init for search of the best correlation in the past frame */
    2452           1 :             ptE_fx = hCPE->prev_synth_chs_fx[ch] + sub( output_frame, add( ipit_max, corr_len ) ); /* Q11 */
    2453             : 
    2454           1 :             idiff = 0;
    2455           1 :             move16();
    2456           1 :             fmaxcorr_fx = -ONE_IN_Q31; /* Q31 */
    2457           1 :             move32();
    2458         161 :             FOR( i = 0; i < lsearch; i++ )
    2459             :             {
    2460         160 :                 ftmp_corr_fx = ncross_corr_self_fx( ptE_fx, i, ipit_max, corr_len, subsampl ); /* Q31 */
    2461         160 :                 IF( GT_32( ftmp_corr_fx, fmaxcorr_fx ) )
    2462             :                 {
    2463          14 :                     idiff = i; /* Q0 */
    2464          14 :                     move16();
    2465             :                 }
    2466         160 :                 fmaxcorr_fx = L_max( fmaxcorr_fx, ftmp_corr_fx ); /* Q11 */
    2467             :             }
    2468             : 
    2469           1 :             ipit = sub( ipit_max, idiff ); /* Q0 */
    2470             : 
    2471           1 :             ptO_fx = tmp_out_fx + output_frame; /* Q11 */
    2472             : 
    2473             :             /* If the correlation is too low, don't use the prediction */
    2474           1 :             IF( LT_32( fmaxcorr_fx, DFT2TD_CORR_THRESH_FX ) )
    2475             :             {
    2476           0 :                 Copy32( &output_fx[ch][0], ptO_fx, ilen ); /* Q11 */
    2477             :             }
    2478             :             ELSE
    2479             :             {
    2480           1 :                 Copy32( hCPE->prev_synth_chs_fx[ch], tmp_out_fx, output_frame ); /* Q11 */
    2481           1 :                 pt1_fx = tmp_out_fx + output_frame - ipit;                       /* Q11 */
    2482         321 :                 FOR( i = 0; i < ilen; i++ )
    2483             :                 {
    2484         320 :                     ptO_fx[i] = pt1_fx[i]; /* Q11 */
    2485         320 :                     move32();
    2486             :                 }
    2487             :             }
    2488             : 
    2489             :             /* Set buffer for the reserved buffer of the current frame */
    2490           1 :             ptO2_fx = tmp_out2_fx + sub( output_frame, ilen ); /* Q11 */
    2491           1 :             set32_fx( tmp_out2_fx + sub( output_frame, ilen ), 0, ilen );
    2492         641 :             FOR( i = 0; i < output_frame; i++ )
    2493             :             {
    2494         640 :                 tmp_out2_fx[i] = output_fx[ch][output_frame - 1 - i]; /* Q11 */
    2495         640 :                 move32();
    2496             :             }
    2497             :             /* ptr init for search of the best correlation of the current frame */
    2498           1 :             ptE_fx = ptO2_fx - ( ipit_max + corr_len ); /* Q11 */
    2499             : 
    2500           1 :             idiff = 0;
    2501           1 :             move16();
    2502           1 :             fmaxcorr_fx = -ONE_IN_Q31; /* Q31 */
    2503           1 :             move32();
    2504         161 :             FOR( i = 0; i < lsearch; i++ )
    2505             :             {
    2506         160 :                 ftmp_corr_fx = ncross_corr_self_fx( ptE_fx, i, ipit_max, corr_len, subsampl ); /* Q31 */
    2507         160 :                 IF( GT_32( ftmp_corr_fx, fmaxcorr_fx ) )
    2508             :                 {
    2509           9 :                     idiff = i; /* Q0 */
    2510           9 :                     move16();
    2511             :                 }
    2512         160 :                 fmaxcorr_fx = L_max( fmaxcorr_fx, ftmp_corr_fx ); /* Q31 */
    2513             :             }
    2514             : 
    2515             : 
    2516           1 :             ipit = sub( ipit_max, idiff ); /* Q0 */
    2517             : 
    2518             :             /* If the correlation is too low, don't use the prediction */
    2519           1 :             IF( GT_32( fmaxcorr_fx, DFT2TD_CORR_THRESH_FX ) )
    2520             :             {
    2521           1 :                 pt1_fx = tmp_out2_fx + sub( output_frame, add( ilen, ipit ) ); /* Q11 */
    2522         321 :                 FOR( i = 0; i < ilen; i++ )
    2523             :                 {
    2524         320 :                     ptO2_fx[i] = pt1_fx[i]; /* Q11 */
    2525         320 :                     move32();
    2526             :                 }
    2527             :             }
    2528             : 
    2529             :             /* perform OVA between predicted signals */
    2530           1 :             SWITCH( ilen )
    2531             :             {
    2532           0 :                 case 160:
    2533           0 :                     flen_fx = 13421772; /* Q31 */
    2534           0 :                     move32();
    2535           0 :                     BREAK;
    2536           1 :                 case 320:
    2537           1 :                     flen_fx = 6710886; /* Q31 */
    2538           1 :                     move32();
    2539           1 :                     BREAK;
    2540           0 :                 case 480:
    2541           0 :                     flen_fx = 4473924; /* Q31 */
    2542           0 :                     move32();
    2543           0 :                     BREAK;
    2544           0 :                 default:
    2545           0 :                     assert( 0 );
    2546             :                     BREAK;
    2547             :             }
    2548           5 :             FOR( i = 0; i < 4; i++ )
    2549             :             {
    2550           4 :                 Word64 temp_a = W_shr( W_add( W_mult0_32_32( sub( 4, i ), output_fx[ch][i] ), W_mult0_32_32( i, ptO_fx[i] ) ), 2 ) * ( ilen - i ); /* Q11 */
    2551           4 :                 move64();
    2552           4 :                 Word64 temp_b = W_mult0_32_32( tmp_out2_fx[output_frame - 1 - i], i );                /* Q11 */
    2553           4 :                 output_fx[ch][i] = W_extract_l( W_shr( ( W_add( temp_a, temp_b ) * flen_fx ), 31 ) ); /* Q11 */
    2554           4 :                 move32();
    2555             :             }
    2556         313 :             FOR( ; i < ilen - 4; i++ )
    2557             :             {
    2558         312 :                 Word64 temp_a = W_mult0_32_32( ptO_fx[i], sub( ilen, i ) );                           /* Q11 */
    2559         312 :                 Word64 temp_b = W_mult0_32_32( tmp_out2_fx[output_frame - 1 - i], i );                /* Q11 */
    2560         312 :                 output_fx[ch][i] = W_extract_l( W_shr( ( W_add( temp_a, temp_b ) * flen_fx ), 31 ) ); /* Q11 */
    2561         312 :                 move32();
    2562             :             }
    2563           1 :             j = 0;
    2564           1 :             move16();
    2565           5 :             FOR( ; i < ilen; i++ )
    2566             :             {
    2567           4 :                 Word64 temp_a = W_mult0_32_32( ptO_fx[i], sub( ilen, i ) );                                                                                         /* Q11 */
    2568           4 :                 Word64 temp_b = W_shr( ( W_add( W_mult0_32_32( sub( 4, j ), tmp_out2_fx[output_frame - 1 - i] ), W_mult0_32_32( j, output_fx[ch][i] ) ) * i ), 2 ); /* Q11 */
    2569           4 :                 output_fx[ch][i] = W_extract_l( W_shr( ( W_add( temp_a, temp_b ) * flen_fx ), 31 ) );                                                               /* Q11 */
    2570           4 :                 move32();
    2571           4 :                 j = add( j, 1 );
    2572             :             }
    2573             :         }
    2574             :     }
    2575      149277 :     ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
    2576             :     {
    2577             :         /* Updates */
    2578      156671 :         FOR( ch = 0; ch < hCPE->nchan_out; ch++ )
    2579             :         {
    2580       96979 :             Copy32( output_fx[ch], hCPE->prev_synth_chs_fx[ch], output_frame ); /* Q11 */
    2581             :         }
    2582             :     }
    2583             : 
    2584      149278 :     return;
    2585             : }

Generated by: LCOV version 1.14