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

Generated by: LCOV version 1.14