LCOV - code coverage report
Current view: top level - lib_enc - ivas_stereo_switching_enc_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ cede165d26d1b794bfc5f5f6f9ec19d4d64a9a3b Lines: 394 425 92.7 %
Date: 2025-11-01 03:16:20 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <stdint.h>
      34             : #include "options.h"
      35             : #include "cnst.h"
      36             : #include "rom_com.h"
      37             : #include "prot_fx.h"
      38             : #include "ivas_rom_com.h"
      39             : #include "assert.h"
      40             : #include "wmc_auto.h"
      41             : #include "prot_fx_enc.h"
      42             : #include "ivas_prot_fx.h"
      43             : 
      44             : 
      45             : /*-------------------------------------------------------------------*
      46             :  * Function allocate_CoreCoder_enc()
      47             :  *
      48             :  * Allocate CoreCoder modules
      49             :  *-------------------------------------------------------------------*/
      50             : 
      51        1538 : static ivas_error allocate_CoreCoder_enc_fx(
      52             :     ENC_CORE_HANDLE st /* i/o: Core encoder state structure     */
      53             : )
      54             : {
      55        1538 :     IF( st->hLPDmem == NULL && NE_16( st->element_mode, IVAS_CPE_MDCT ) )
      56             :     {
      57         792 :         IF( ( st->hLPDmem = (LPD_state_HANDLE) malloc( sizeof( LPD_state ) ) ) == NULL )
      58             :         {
      59           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LPDmem\n" ) );
      60             :         }
      61         792 :         LPDmem_enc_init_ivas_fx( st->hLPDmem );
      62             :     }
      63             : 
      64        1538 :     IF( st->hGSCEnc == NULL && NE_16( st->element_mode, IVAS_CPE_MDCT ) )
      65             :     {
      66         792 :         IF( ( st->hGSCEnc = (GSC_ENC_HANDLE) malloc( sizeof( GSC_ENC_DATA ) ) ) == NULL )
      67             :         {
      68           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for GSC\n" ) );
      69             :         }
      70         792 :         GSC_enc_init_fx( st->hGSCEnc );
      71             :     }
      72             : 
      73        1538 :     IF( st->hNoiseEst == NULL )
      74             :     {
      75         798 :         IF( ( st->hNoiseEst = (NOISE_EST_HANDLE) malloc( sizeof( NOISE_EST_DATA ) ) ) == NULL )
      76             :         {
      77           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Noise estimation\n" ) );
      78             :         }
      79         798 :         noise_est_init_ivas_fx( st->hNoiseEst );
      80             :     }
      81             : 
      82        1538 :     IF( st->hVAD == NULL )
      83             :     {
      84         798 :         IF( ( st->hVAD = (VAD_HANDLE) malloc( sizeof( VAD_DATA ) ) ) == NULL )
      85             :         {
      86           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for VAD\n" ) );
      87             :         }
      88         798 :         wb_vad_init_fx( st->hVAD );
      89             :     }
      90             : 
      91        1538 :     IF( st->hSpMusClas == NULL )
      92             :     {
      93         798 :         IF( ( st->hSpMusClas = (SP_MUS_CLAS_HANDLE) malloc( sizeof( SP_MUS_CLAS_DATA ) ) ) == NULL )
      94             :         {
      95           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Speech/music classifier\n" ) );
      96             :         }
      97         798 :         speech_music_clas_init_ivas_fx( st->hSpMusClas );
      98             :     }
      99             : 
     100        1538 :     return IVAS_ERR_OK;
     101             : }
     102             : 
     103             : /*-------------------------------------------------------------------*
     104             :  * Function deallocate_CoreCoder_TCX_enc()
     105             :  *
     106             :  * Deallocate CoreCoder TCX modules
     107             :  *-------------------------------------------------------------------*/
     108             : 
     109         840 : static void deallocate_CoreCoder_TCX_enc_fx(
     110             :     ENC_CORE_HANDLE st /* i/o: Core encoder state structure     */
     111             : )
     112             : {
     113         840 :     IF( st->hTcxEnc != NULL )
     114             :     {
     115         732 :         free( st->hTcxEnc );
     116         732 :         st->hTcxEnc = NULL;
     117             :     }
     118             : 
     119         840 :     IF( st->hTcxCfg != NULL )
     120             :     {
     121         732 :         free( st->hTcxCfg );
     122         732 :         st->hTcxCfg = NULL;
     123             :     }
     124             : 
     125         840 :     IF( st->hIGFEnc != NULL )
     126             :     {
     127         533 :         free( st->hIGFEnc );
     128         533 :         st->hIGFEnc = NULL;
     129             :     }
     130             : 
     131         840 :     IF( st->hHQ_core != NULL )
     132             :     {
     133           0 :         free( st->hHQ_core );
     134           0 :         st->hHQ_core = NULL;
     135             :     }
     136             : 
     137         840 :     return;
     138             : }
     139             : 
     140             : 
     141             : /*-------------------------------------------------------------------*
     142             :  * Function deallocate_CoreCoder_enc()
     143             :  *
     144             :  * Deallocate CoreCoder modules
     145             :  *-------------------------------------------------------------------*/
     146             : 
     147        2278 : static void deallocate_CoreCoder_enc_fx(
     148             :     ENC_CORE_HANDLE st /* i/o: Core encoder state structure     */
     149             : )
     150             : {
     151        2278 :     IF( st->hLPDmem != NULL )
     152             :     {
     153         808 :         free( st->hLPDmem );
     154         808 :         st->hLPDmem = NULL;
     155             :     }
     156             : 
     157        2278 :     IF( st->hGSCEnc != NULL )
     158             :     {
     159         808 :         free( st->hGSCEnc );
     160         808 :         st->hGSCEnc = NULL;
     161             :     }
     162             : 
     163        2278 :     test();
     164        2278 :     IF( st->hNoiseEst != NULL && NE_16( st->element_mode, IVAS_CPE_MDCT ) )
     165             :     {
     166         780 :         free( st->hNoiseEst );
     167         780 :         st->hNoiseEst = NULL;
     168             :     }
     169             : 
     170        2278 :     test();
     171        2278 :     IF( st->hVAD != NULL && NE_16( st->element_mode, IVAS_CPE_MDCT ) )
     172             :     {
     173         780 :         free( st->hVAD );
     174         780 :         st->hVAD = NULL;
     175             :     }
     176             : 
     177        2278 :     test();
     178        2278 :     IF( st->hSpMusClas != NULL && NE_16( st->element_mode, IVAS_CPE_MDCT ) )
     179             :     {
     180         780 :         free( st->hSpMusClas );
     181         780 :         st->hSpMusClas = NULL;
     182             :     }
     183             : 
     184        2278 :     IF( st->cldfbAnaEnc != NULL )
     185             :     {
     186         834 :         deleteCldfb_ivas_fx( &st->cldfbAnaEnc );
     187             :     }
     188             : 
     189        2278 :     IF( st->hBWE_TD != NULL )
     190             :     {
     191         776 :         free( st->hBWE_TD );
     192         776 :         st->hBWE_TD = NULL;
     193             :     }
     194             : 
     195        2278 :     IF( st->cldfbSynTd != NULL )
     196             :     {
     197         776 :         deleteCldfb_ivas_fx( &st->cldfbSynTd );
     198             :     }
     199             : 
     200        2278 :     IF( st->hBWE_FD != NULL )
     201             :     {
     202         776 :         free( st->hBWE_FD );
     203         776 :         st->hBWE_FD = NULL;
     204             :     }
     205             : 
     206        2278 :     IF( st->element_mode != IVAS_CPE_MDCT )
     207             :     {
     208         780 :         deallocate_CoreCoder_TCX_enc_fx( st );
     209             :     }
     210             : 
     211        2278 :     return;
     212             : }
     213             : 
     214             : /*-------------------------------------------------------------------*
     215             :  * Function stereo_memory_enc()
     216             :  *
     217             :  * Dynamically allocate/deallocate data structures depending on the actual CPE mode
     218             :  *-------------------------------------------------------------------*/
     219             : 
     220      422855 : ivas_error stereo_memory_enc_fx(
     221             :     CPE_ENC_HANDLE hCPE,           /* i  : CPE encoder structure   */
     222             :     const Word32 input_Fs,         /* i  : input sampling rate     Q0*/
     223             :     const Word16 max_bwidth,       /* i  : maximum audio bandwidth Q0*/
     224             :     const IVAS_FORMAT ivas_format, /* i  : ivas format             */
     225             :     const Word16 nchan_transport   /* i  : number transport chans  Q0*/
     226             : )
     227             : {
     228             :     Encoder_State *st;
     229             :     ivas_error error;
     230             : 
     231      422855 :     error = IVAS_ERR_OK;
     232      422855 :     move32();
     233             : 
     234      422855 :     assert( hCPE->last_element_mode >= IVAS_CPE_DFT && "Switching from SCE to CPE is not a valid configuration!" );
     235             : 
     236             :     /*--------------------------------------------------------------*
     237             :      * save parameters from structures that will be freed
     238             :      *---------------------------------------------------------------*/
     239      422855 :     test();
     240      422855 :     IF( hCPE->hStereoTCA != NULL && EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
     241             :     {
     242       59677 :         Word16 tmp = extract_h( L_abs( hCPE->hStereoDft->hItd->itd_fx[1] ) );
     243       59677 :         if ( hCPE->hStereoDft->hItd->itd_fx[1] < 0 )
     244             :         {
     245       20001 :             tmp = negate( tmp );
     246             :         }
     247       59677 :         set16_fx( hCPE->hStereoTCA->prevCorrLagStats, tmp, 3 );
     248       59677 :         IF( hCPE->hStereoDft->hItd->itd_fx[1] >= 0 )
     249             :         {
     250       39676 :             hCPE->hStereoTCA->prevRefChanIndx = L_CH_INDX;
     251             :         }
     252             :         ELSE
     253             :         {
     254       20001 :             hCPE->hStereoTCA->prevRefChanIndx = R_CH_INDX;
     255             :         }
     256       59677 :         move16();
     257             :     }
     258             : 
     259             :     /*--------------------------------------------------------------*
     260             :      * allocate/deallocate data structures
     261             :      *---------------------------------------------------------------*/
     262             : 
     263      422855 :     IF( NE_16( hCPE->element_mode, hCPE->last_element_mode ) )
     264             :     {
     265             :         /*--------------------------------------------------------------*
     266             :          * switching CPE mode to DFT stereo
     267             :          *---------------------------------------------------------------*/
     268             : 
     269        1589 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
     270             :         {
     271             :             /* deallocate data structure of the previous CPE mode */
     272         780 :             IF( hCPE->hStereoTD != NULL )
     273             :             {
     274          56 :                 free( hCPE->hStereoTD );
     275          56 :                 hCPE->hStereoTD = NULL;
     276             :             }
     277             : 
     278         780 :             IF( hCPE->hStereoMdct != NULL )
     279             :             {
     280         724 :                 stereo_mdct_enc_destroy_fx( &( hCPE->hStereoMdct ) );
     281         724 :                 hCPE->hStereoMdct = NULL;
     282             :             }
     283             : 
     284             :             /* deallocate CoreCoder secondary channel */
     285         780 :             deallocate_CoreCoder_enc_fx( hCPE->hCoreCoder[1] );
     286             : 
     287             :             /* allocate DFT stereo data structure */
     288         780 :             IF( NE_32( ( error = stereo_dft_enc_create_fx( &( hCPE->hStereoDft ), input_Fs, max_bwidth ) ), IVAS_ERR_OK ) )
     289             :             {
     290           0 :                 return error;
     291             :             }
     292             : 
     293             :             /* allocate ICBWE structure */
     294         780 :             IF( hCPE->hStereoICBWE == NULL )
     295             :             {
     296         748 :                 IF( ( hCPE->hStereoICBWE = (STEREO_ICBWE_ENC_HANDLE) malloc( sizeof( STEREO_ICBWE_ENC_DATA ) ) ) == NULL )
     297             :                 {
     298           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE \n" ) );
     299             :                 }
     300         748 :                 stereo_icBWE_init_enc_fx( hCPE->hStereoICBWE );
     301             :             }
     302             : 
     303             :             /* allocate HQ core in M channel */
     304         780 :             st = hCPE->hCoreCoder[0];
     305         780 :             IF( st->hHQ_core == NULL )
     306             :             {
     307           3 :                 IF( ( st->hHQ_core = (HQ_ENC_HANDLE) malloc( sizeof( HQ_ENC_DATA ) ) ) == NULL )
     308             :                 {
     309           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HQ core\n" ) );
     310             :                 }
     311             : 
     312           3 :                 HQ_core_enc_init_fx( st->hHQ_core );
     313             :             }
     314             :         }
     315             : 
     316             :         /*--------------------------------------------------------------*
     317             :          * switching CPE mode to TD stereo
     318             :          *---------------------------------------------------------------*/
     319             : 
     320        1589 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
     321             :         {
     322             :             /* deallocate data structure of the previous CPE mode */
     323          60 :             IF( hCPE->hStereoDft != NULL )
     324             :             {
     325          52 :                 stereo_dft_enc_destroy_fx( &( hCPE->hStereoDft ) );
     326          52 :                 hCPE->hStereoDft = NULL;
     327             :             }
     328             : 
     329          60 :             IF( hCPE->hStereoMdct != NULL )
     330             :             {
     331           8 :                 stereo_mdct_enc_destroy_fx( &( hCPE->hStereoMdct ) );
     332           8 :                 hCPE->hStereoMdct = NULL;
     333             :             }
     334             : 
     335             :             /* deallocated TCX/IGF structures for second channel */
     336          60 :             deallocate_CoreCoder_TCX_enc_fx( hCPE->hCoreCoder[1] );
     337             : 
     338             :             /* allocate TD stereo data structure */
     339          60 :             IF( hCPE->hStereoTD != NULL )
     340             :             {
     341           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: TD Stereo memory already allocated\n" );
     342             :             }
     343             : 
     344          60 :             IF( ( hCPE->hStereoTD = (STEREO_TD_ENC_DATA_HANDLE) malloc( sizeof( STEREO_TD_ENC_DATA ) ) ) == NULL )
     345             :             {
     346           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD Stereo\n" ) );
     347             :             }
     348             : 
     349          60 :             stereo_td_init_enc_fx( hCPE->hStereoTD, hCPE->last_element_mode );
     350             :             /* allocate secondary channel */
     351          60 :             IF( NE_32( ( error = allocate_CoreCoder_enc_fx( hCPE->hCoreCoder[1] ) ), IVAS_ERR_OK ) )
     352             :             {
     353           0 :                 return error;
     354             :             }
     355             :         }
     356             : 
     357             :         /*--------------------------------------------------------------*
     358             :          * allocate DFT/TD stereo structures after MDCT stereo frame
     359             :          *---------------------------------------------------------------*/
     360        1589 :         test();
     361        1589 :         test();
     362        1589 :         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 ) ) )
     363             :         {
     364             :             /* Deallocate MDCT CNG structures */
     365         732 :             deleteCldfb_ivas_fx( &hCPE->hCoreCoder[0]->cldfbAnaEnc );
     366         732 :             deleteCldfb_ivas_fx( &hCPE->hCoreCoder[1]->cldfbAnaEnc );
     367             : 
     368         732 :             IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
     369             :             {
     370         724 :                 IF( hCPE->hCoreCoder[1]->hDtxEnc != NULL )
     371             :                 {
     372          58 :                     free( hCPE->hCoreCoder[1]->hDtxEnc );
     373          58 :                     hCPE->hCoreCoder[1]->hDtxEnc = NULL;
     374             :                 }
     375             : 
     376         724 :                 IF( hCPE->hCoreCoder[1]->hFdCngEnc != NULL )
     377             :                 {
     378          58 :                     deleteFdCngEnc_fx( &hCPE->hCoreCoder[1]->hFdCngEnc );
     379             :                 }
     380             :             }
     381             : 
     382         732 :             IF( hCPE->hCoreCoder[0]->Opt_DTX_ON && hCPE->hCoreCoder[0]->hTdCngEnc == NULL )
     383             :             {
     384          60 :                 IF( ( hCPE->hCoreCoder[0]->hTdCngEnc = (TD_CNG_ENC_HANDLE) malloc( sizeof( TD_CNG_ENC_DATA ) ) ) == NULL )
     385             :                 {
     386           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DTX/TD CNG\n" ) );
     387             :                 }
     388             : 
     389          60 :                 td_cng_enc_init_ivas_fx( hCPE->hCoreCoder[0]->hTdCngEnc, hCPE->hCoreCoder[0]->Opt_DTX_ON, hCPE->hCoreCoder[0]->max_bwidth );
     390             :             }
     391             : 
     392             :             /* allocate TCA data structure */
     393         732 :             IF( hCPE->hStereoTCA != NULL )
     394             :             {
     395           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: TCA Stereo memory already allocated\n" );
     396             :             }
     397             : 
     398         732 :             IF( ( hCPE->hStereoTCA = (STEREO_TCA_ENC_HANDLE) malloc( sizeof( STEREO_TCA_ENC_DATA ) ) ) == NULL )
     399             :             {
     400           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
     401             :             }
     402             : 
     403         732 :             stereo_tca_init_enc_fx( hCPE->hStereoTCA, input_Fs );
     404             : 
     405         732 :             st = hCPE->hCoreCoder[0];
     406             : 
     407             :             /* allocate primary channel substructures */
     408         732 :             IF( NE_32( ( error = allocate_CoreCoder_enc_fx( st ) ), IVAS_ERR_OK ) )
     409             :             {
     410           0 :                 return error;
     411             :             }
     412             : 
     413             :             /* allocate CLDFB for primary channel */
     414         732 :             IF( st->cldfbAnaEnc == NULL )
     415             :             {
     416         732 :                 IF( NE_32( ( error = openCldfb_ivas_fx( &st->cldfbAnaEnc, CLDFB_ANALYSIS, input_Fs, CLDFB_PROTOTYPE_1_25MS, ENC ) ), IVAS_ERR_OK ) )
     417             :                 {
     418           0 :                     return error;
     419             :                 }
     420             :             }
     421             : 
     422             :             /* allocate BWEs for primary channel */
     423         732 :             IF( st->hBWE_TD == NULL )
     424             :             {
     425         732 :                 IF( ( st->hBWE_TD = (TD_BWE_ENC_HANDLE) malloc( sizeof( TD_BWE_ENC_DATA ) ) ) == NULL )
     426             :                 {
     427           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD BWE\n" ) );
     428             :                 }
     429             : 
     430         732 :                 IF( st->cldfbSynTd == NULL )
     431             :                 {
     432         732 :                     IF( NE_32( ( error = openCldfb_ivas_fx( &st->cldfbSynTd, CLDFB_SYNTHESIS, 16000, CLDFB_PROTOTYPE_1_25MS, ENC ) ), IVAS_ERR_OK ) )
     433             :                     {
     434           0 :                         return error;
     435             :                     }
     436             :                 }
     437             : 
     438         732 :                 InitSWBencBuffer_ivas_fx( st );
     439         732 :                 ResetSHBbuffer_Enc_fx( st );
     440             : 
     441         732 :                 IF( ( st->hBWE_FD = (FD_BWE_ENC_HANDLE) malloc( sizeof( FD_BWE_ENC_DATA ) ) ) == NULL )
     442             :                 {
     443           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FD BWE\n" ) );
     444             :                 }
     445             : 
     446         732 :                 fd_bwe_enc_init_fx( st->hBWE_FD );
     447         732 :                 st->Q_old_wtda = 0;
     448         732 :                 move16();
     449             :             }
     450             : 
     451             :             /* allocate stereo CNG structure */
     452         732 :             IF( hCPE->hStereoCng == NULL )
     453             :             {
     454          26 :                 IF( ( hCPE->hStereoCng = (STEREO_CNG_ENC_HANDLE) malloc( sizeof( STEREO_CNG_ENC ) ) ) == NULL )
     455             :                 {
     456           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo Cng for Unified/TD\n" ) );
     457             :                 }
     458             : 
     459          26 :                 stereo_enc_cng_init_fx( hCPE->hStereoCng );
     460             :             }
     461             :         }
     462             : 
     463             :         /*--------------------------------------------------------------*
     464             :          * switching CPE mode to MDCT stereo
     465             :          *---------------------------------------------------------------*/
     466             : 
     467        1589 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
     468             :         {
     469             :             Word16 i;
     470             : 
     471             :             /* deallocate data structure of the previous CPE mode */
     472         749 :             IF( hCPE->hStereoDft != NULL )
     473             :             {
     474         746 :                 stereo_dft_enc_destroy_fx( &( hCPE->hStereoDft ) );
     475         746 :                 hCPE->hStereoDft = NULL;
     476             :             }
     477             : 
     478         749 :             IF( hCPE->hStereoTD != NULL )
     479             :             {
     480           3 :                 free( hCPE->hStereoTD );
     481           3 :                 hCPE->hStereoTD = NULL;
     482             :             }
     483             : 
     484         749 :             IF( hCPE->hStereoTCA != NULL )
     485             :             {
     486         749 :                 free( hCPE->hStereoTCA );
     487         749 :                 hCPE->hStereoTCA = NULL;
     488             :             }
     489             : 
     490         749 :             IF( hCPE->hStereoICBWE != NULL )
     491             :             {
     492         746 :                 free( hCPE->hStereoICBWE );
     493         746 :                 hCPE->hStereoICBWE = NULL;
     494             :             }
     495             : 
     496        2247 :             FOR( i = 0; i < CPE_CHANNELS; i++ )
     497             :             {
     498             :                 /* deallocate core-coder substructures */
     499        1498 :                 deallocate_CoreCoder_enc_fx( hCPE->hCoreCoder[i] );
     500             :             }
     501             : 
     502         749 :             IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
     503             :             {
     504             :                 /* allocate secondary channel */
     505         746 :                 IF( NE_32( ( error = allocate_CoreCoder_enc_fx( hCPE->hCoreCoder[1] ) ), IVAS_ERR_OK ) )
     506             :                 {
     507           0 :                     return error;
     508             :                 }
     509             :             }
     510             : 
     511             :             /* allocate TCX/IGF structures for second channel */
     512         749 :             st = hCPE->hCoreCoder[1];
     513             : 
     514         749 :             IF( ( st->hTcxEnc = (TCX_ENC_HANDLE) malloc( sizeof( TCX_ENC_DATA ) ) ) == NULL )
     515             :             {
     516           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for hTcxEnc\n" ) );
     517             :             }
     518             : 
     519             :             // st->hTcxEnc->spectrum[0] = st->hTcxEnc->spectrum_long;
     520             :             // st->hTcxEnc->spectrum[1] = st->hTcxEnc->spectrum_long + N_TCX10_MAX;
     521         749 :             st->hTcxEnc->spectrum_fx[0] = st->hTcxEnc->spectrum_long_fx;               /* Q31-st->hTcxEnc->spectrum_e[0] */
     522         749 :             st->hTcxEnc->spectrum_fx[1] = st->hTcxEnc->spectrum_long_fx + N_TCX10_MAX; /* Q31-st->hTcxEnc->spectrum_e[1] */
     523         749 :             st->hTcxEnc->spectrum_e[0] = st->hTcxEnc->spectrum_e[1] = 0;
     524         749 :             move16();
     525         749 :             move16();
     526             :             // set_f( st->hTcxEnc->old_out, 0, L_FRAME32k );
     527             :             // set_f( st->hTcxEnc->spectrum_long, 0, N_MAX );
     528         749 :             set32_fx( st->hTcxEnc->spectrum_long_fx, 0, N_MAX );
     529         749 :             st->hTcxEnc->spectrum_long_e = 0;
     530         749 :             st->hTcxEnc->tfm_mem_fx = 1610612736; /* 0.75 in Q31 */
     531         749 :             move16();
     532         749 :             move32();
     533             : 
     534         749 :             IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
     535             :             {
     536         746 :                 st->last_core = ACELP_CORE; /* needed to set-up TCX core in SetTCXModeInfo() */
     537         746 :                 move16();
     538             :             }
     539             : 
     540         749 :             IF( ( st->hTcxCfg = (TCX_CONFIG_HANDLE) malloc( sizeof( TCX_config ) ) ) == NULL )
     541             :             {
     542           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for hTcxCfg\n" ) );
     543             :             }
     544             : 
     545         749 :             IF( ( st->hIGFEnc = (IGF_ENC_INSTANCE_HANDLE) malloc( sizeof( IGF_ENC_INSTANCE ) ) ) == NULL )
     546             :             {
     547           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for hIGFEnc\n" ) );
     548             :             }
     549         749 :             st->igf = getIgfPresent_fx( st->element_mode, st->total_brate, st->bwidth, st->rf_mode );
     550         749 :             move16();
     551             : 
     552             :             /* allocate and initialize MDCT stereo structure */
     553         749 :             IF( ( hCPE->hStereoMdct = (STEREO_MDCT_ENC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_ENC_DATA ) ) ) == NULL )
     554             :             {
     555           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
     556             :             }
     557             : 
     558             : #ifdef DEBUGGING
     559             :             hCPE->hStereoMdct->mdct_stereo_mode_cmdl = SMDCT_MS_DECISION;
     560             : #endif
     561             : 
     562         749 :             initMdctStereoEncData_fx( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, hCPE->hCoreCoder[0]->max_bwidth, 0, NULL, 1 );
     563             : 
     564         749 :             test();
     565         749 :             hCPE->hStereoMdct->isSBAStereoMode = ( EQ_32( ivas_format, SBA_FORMAT ) && EQ_16( nchan_transport, 2 ) );
     566         749 :             move16();
     567             : 
     568         749 :             test();
     569         749 :             test();
     570         749 :             IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && LE_32( hCPE->element_brate, MAX_MDCT_ITD_BRATE ) && EQ_16( ivas_format, STEREO_FORMAT ) )
     571             :             {
     572          24 :                 IF( NE_32( ( error = initMdctItdHandling_fx( hCPE->hStereoMdct, input_Fs ) ), IVAS_ERR_OK ) )
     573             :                 {
     574           0 :                     return error;
     575             :                 }
     576             :             }
     577             : 
     578             :             /* allocate/deallocate and initialize DTX/CNG structures */
     579         749 :             IF( hCPE->hCoreCoder[0]->Opt_DTX_ON )
     580             :             {
     581         189 :                 FOR( i = 0; i < CPE_CHANNELS; i++ )
     582             :                 {
     583         126 :                     st = hCPE->hCoreCoder[i];
     584         126 :                     IF( NE_32( ( error = openCldfb_ivas_fx( &st->cldfbAnaEnc, CLDFB_ANALYSIS, st->input_Fs, CLDFB_PROTOTYPE_1_25MS, ENC ) ), IVAS_ERR_OK ) )
     585             :                     {
     586           0 :                         return error;
     587             :                     }
     588             : 
     589         126 :                     IF( st->hDtxEnc == NULL )
     590             :                     {
     591          61 :                         IF( ( st->hDtxEnc = (DTX_ENC_HANDLE) malloc( sizeof( DTX_ENC_DATA ) ) ) == NULL )
     592             :                         {
     593           0 :                             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DTX variables\n" ) );
     594             :                         }
     595             :                     }
     596         126 :                     dtx_enc_init_fx( st, 0, FIXED_SID_RATE );
     597             : 
     598         126 :                     IF( st->hTdCngEnc != NULL )
     599             :                     {
     600          63 :                         free( st->hTdCngEnc );
     601          63 :                         st->hTdCngEnc = NULL;
     602             :                     }
     603             : 
     604         126 :                     IF( st->hFdCngEnc == NULL )
     605             :                     {
     606          61 :                         createFdCngEnc_fx( &st->hFdCngEnc );
     607             : 
     608          61 :                         initFdCngEnc_fx( st->hFdCngEnc, st->input_Fs, st->cldfbAnaEnc->scale );
     609          61 :                         configureFdCngEnc_ivas_fx( st->hFdCngEnc, st->bwidth, st->rf_mode && st->total_brate == ACELP_13k20 ? ACELP_9k60 : st->total_brate );
     610             :                     }
     611             :                 }
     612             :             }
     613             :         }
     614             :     }
     615             : 
     616      422855 :     test();
     617      422855 :     test();
     618      422855 :     test();
     619      422855 :     IF( EQ_16( ivas_format, STEREO_FORMAT ) && EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && LE_32( hCPE->element_brate, MAX_MDCT_ITD_BRATE ) && GT_32( hCPE->last_element_brate, MAX_MDCT_ITD_BRATE ) )
     620             :     {
     621             :         /* allocate MDCT stereo ITD handling structure */
     622          30 :         IF( NE_32( ( error = initMdctItdHandling_fx( hCPE->hStereoMdct, input_Fs ) ), IVAS_ERR_OK ) )
     623             :         {
     624           0 :             return error;
     625             :         }
     626             :     }
     627             : 
     628      422855 :     return error;
     629             : }
     630             : 
     631             : /*-------------------------------------------------------------------*
     632             :  * Function stereo_switching_enc()
     633             :  *
     634             :  * Handling of memories in case of CPE modes switching
     635             :  *-------------------------------------------------------------------*/
     636             : 
     637      422855 : void stereo_switching_enc_fx(
     638             :     CPE_ENC_HANDLE hCPE,           /* i  : CPE encoder structure               */
     639             :     Word32 old_input_signal_pri[], /* i  : old input signal of primary channel q_inp*/
     640             :     const Word16 input_frame,      /* i  : input frame length                  Q0*/
     641             :     const Word16 q_inp )
     642             : {
     643             :     Word16 i, n, dft_ovl, offset;
     644             :     Word16 tmp_fx;
     645      422855 :     move16();
     646             :     Encoder_State **sts;
     647             : 
     648      422855 :     sts = hCPE->hCoreCoder;
     649      422855 :     dft_ovl = extract_l( Mpy_32_32( imult3216( input_frame, STEREO_DFT_OVL_MAX ), 2236963 ) ); // 1/L_FRAME48k = 2236963 (Q31)
     650             : 
     651             :     /* update DFT analysis overlap memory */
     652             :     /* note: in MDCT stereo, the update is done in stereo_td_itd_mdct_stereo() */
     653      422855 :     test();
     654      422855 :     test();
     655      422855 :     IF( GT_16( hCPE->element_mode, IVAS_CPE_DFT ) && hCPE->input_mem_fx[0] != NULL && NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
     656             :     {
     657       11433 :         FOR( n = 0; n < CPE_CHANNELS; n++ )
     658             :         {
     659        7622 :             Word16 sf = L_norm_arr( sts[n]->input32_fx + input_frame - dft_ovl, dft_ovl );
     660        7622 :             Copy_Scale_sig32_16( sts[n]->input32_fx + input_frame - dft_ovl, hCPE->input_mem_fx[n], dft_ovl, sf ); /* sts[n]->q_inp32 - 16 */
     661        7622 :             hCPE->q_input_mem[n] = sub( add( sts[n]->q_inp32, sf ), 16 );
     662        7622 :             move16();
     663             :         }
     664             :     }
     665             : 
     666             :     /* save original stereo input (MDCT overlap part) for both channels in unused old input of right channel for possible DFT->MDCT transition */
     667      422855 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
     668             :     {
     669       59659 :         Copy32( sts[0]->input32_fx + sub( extract_l( Mpy_32_32( sts[0]->input_Fs, 42949673 /* 1/50 in Q31*/ ) ), sts[0]->encoderLookahead_FB ), sts[1]->input32_fx - shl( sts[0]->encoderLookahead_FB, 1 ), sts[0]->encoderLookahead_FB ); /* sts[0]->q_inp32 */
     670             : 
     671       59659 :         Copy32( sts[1]->input32_fx + sub( extract_l( Mpy_32_32( sts[1]->input_Fs, 42949673 /* 1/50 in Q31*/ ) ), sts[1]->encoderLookahead_FB ), sts[1]->input32_fx - sts[1]->encoderLookahead_FB, sts[1]->encoderLookahead_FB ); /* sts[1]->q_inp32 */
     672             :     }
     673             : 
     674             : 
     675             :     /* TD/MDCT -> DFT stereo switching */
     676      422855 :     test();
     677      422855 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && NE_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
     678             :     {
     679             :         /* window DFT synthesis overlap memory @input_Fs, primary channel */
     680      306680 :         FOR( i = 0; i < dft_ovl; i++ )
     681             :         {
     682      305900 :             hCPE->hStereoDft->output_mem_dmx_fx[i] = L_shl( Mpy_32_32_r( hCPE->hStereoDft->win_fx[dft_ovl - 1 - i], old_input_signal_pri[input_frame - dft_ovl + i] ), sub( 15, q_inp ) ); // Q15
     683      305900 :             move32();
     684             :         }
     685             :         /* reset 48kHz BWE overlap memory */
     686         780 :         set32_fx( hCPE->hStereoDft->output_mem_dmx_32k_fx, 0, STEREO_DFT_OVL_32k );
     687             : 
     688         780 :         stereo_dft_enc_reset_fx( hCPE->hStereoDft );
     689             : 
     690             :         /* update ITD parameters */
     691         780 :         test();
     692         780 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_TD ) )
     693             :         {
     694          56 :             set32_fx( hCPE->hStereoDft->hItd->itd_fx, hCPE->hStereoTCA->prevCorrLagStats[2], STEREO_DFT_ENC_DFT_NB );
     695             :         }
     696             : 
     697             :         /* Update the side_gain[] parameters */
     698         780 :         test();
     699         780 :         IF( hCPE->hStereoTCA != NULL && NE_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
     700             :         {
     701          56 :             tmp_fx = usdequant_fx( hCPE->hStereoTCA->indx_ica_gD, STEREO_TCA_GDMIN_FX, STEREO_TCA_GDSTEP_FX );
     702         784 :             FOR( i = 0; i < STEREO_DFT_BAND_MAX; i++ )
     703             :             {
     704         728 :                 hCPE->hStereoDft->side_gain_fx[STEREO_DFT_BAND_MAX + i] = L_deposit_h( tmp_fx ); /* Q31 */
     705         728 :                 move32();
     706             :             }
     707             :         }
     708             : 
     709             :         /* do not allow differential coding of DFT side parameters */
     710         780 :         hCPE->hStereoDft->res_pred_counter = STEREO_DFT_FEC_THRESHOLD;
     711         780 :         move16();
     712             : 
     713             :         /* update DFT synthesis overlap memory @12.8kHz */
     714       88140 :         FOR( i = 0; i < STEREO_DFT_OVL_12k8; i++ )
     715             :         {
     716       87360 :             hCPE->hStereoDft->output_mem_dmx_12k8_fx[i] = L_shr( Mpy_32_16_r( hCPE->hStereoDft->win_12k8_fx[STEREO_DFT_OVL_12k8 - 1 - i], sts[0]->buf_speech_enc[L_FRAME32k + L_FRAME - STEREO_DFT_OVL_12k8 + i] ), sub( 16, sts[0]->exp_buf_speech_enc ) ); /* Q15 */
     717       87360 :             move32();
     718             :         }
     719         780 :         Word16 q_dmx = Q15;
     720         780 :         move16();
     721             :         /* update DFT synthesis overlap memory @16kHz, primary channel only */
     722         780 :         L_lerp_fx( hCPE->hStereoDft->output_mem_dmx_fx, hCPE->hStereoDft->output_mem_dmx_16k_fx, STEREO_DFT_OVL_16k, dft_ovl, &q_dmx );
     723      109980 :         FOR( i = 0; i < STEREO_DFT_OVL_16k; i++ )
     724             :         {
     725      109200 :             hCPE->hStereoDft->output_mem_dmx_16k_fx[i] = L_shl( hCPE->hStereoDft->output_mem_dmx_16k_fx[i], sub( Q15, q_dmx ) ); // Q15
     726      109200 :             move32();
     727             :         }
     728             : 
     729      328380 :         FOR( i = 0; i < STEREO_DFT_OVL_MAX; i++ )
     730             :         {
     731      327600 :             hCPE->hStereoDft->output_mem_dmx_fx[i] = L_shl( hCPE->hStereoDft->output_mem_dmx_fx[i], sub( Q15, q_dmx ) ); // Q15
     732      327600 :             move32();
     733             :         }
     734             : 
     735             :         /* reset DFT synthesis overlap memory @8kHz, secondary channel */
     736         780 :         set32_fx( hCPE->hStereoDft->output_mem_res_8k_fx, 0, STEREO_DFT_OVL_8k );
     737             : 
     738         780 :         hCPE->hCoreCoder[1]->vad_flag = 0;
     739         780 :         move16();
     740             :     }
     741             : 
     742             :     /* MDCT -> TD stereo switching */
     743      422855 :     test();
     744      422855 :     test();
     745      422855 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
     746             :     {
     747           8 :         hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_LEFT_IS_PRIM;
     748           8 :         move16();
     749           8 :         hCPE->hStereoTD->tdm_last_ratio_idx_SM = LRTD_STEREO_LEFT_IS_PRIM;
     750           8 :         move16();
     751           8 :         hCPE->hStereoTD->tdm_last_SM_flag = 0;
     752           8 :         move16();
     753           8 :         hCPE->hStereoTD->tdm_last_inst_ratio_idx = LRTD_STEREO_MID_IS_PRIM;
     754           8 :         move16();
     755           8 :         hCPE->hStereoTD->tdm_last_ratio_fx = tdm_ratio_tabl_fx[LRTD_STEREO_LEFT_IS_PRIM];
     756           8 :         move32();
     757             :     }
     758             :     /* DFT -> TD stereo switching */
     759      422847 :     ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
     760             :     {
     761          52 :         hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_MID_IS_PRIM;
     762          52 :         move16();
     763          52 :         hCPE->hStereoTD->tdm_last_ratio_idx_SM = LRTD_STEREO_MID_IS_PRIM;
     764          52 :         move16();
     765          52 :         hCPE->hStereoTD->tdm_last_SM_flag = 0;
     766          52 :         move16();
     767          52 :         hCPE->hStereoTD->tdm_last_inst_ratio_idx = LRTD_STEREO_MID_IS_PRIM;
     768          52 :         move16();
     769             : 
     770             :         /* First frame after DFT frame AND the content is uncorrelated or xtalk -> the primary channel is forced to left */
     771          52 :         IF( EQ_16( hCPE->hStereoClassif->lrtd_mode, 1 ) )
     772             :         {
     773          52 :             set_zero_fx( sts[1]->input32_fx - input_frame, input_frame );
     774          52 :             set16_zero_fx( sts[1]->input_fx - input_frame, input_frame );
     775          52 :             sts[1]->q_old_inp = Q15;
     776          52 :             hCPE->hStereoTD->tdm_last_ratio_fx = tdm_ratio_tabl_fx[LRTD_STEREO_LEFT_IS_PRIM]; /* Q31 */
     777          52 :             move16();
     778          52 :             move32();
     779             : 
     780          52 :             hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_LEFT_IS_PRIM;
     781          52 :             move16();
     782             : 
     783          52 :             test();
     784          52 :             test();
     785          52 :             IF( LT_32( hCPE->hStereoTCA->instTargetGain_fx, 26843546 /*Q29*/ ) && ( hCPE->hCoreCoder[0]->vad_flag || hCPE->hCoreCoder[1]->vad_flag ) ) /* but if there is no content in the L channel -> the primary channel is forced to right */
     786             :             {
     787          16 :                 hCPE->hStereoTD->tdm_last_ratio_fx = tdm_ratio_tabl_fx[LRTD_STEREO_RIGHT_IS_PRIM]; /* Q31 */
     788          16 :                 move32();
     789          16 :                 hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_RIGHT_IS_PRIM;
     790          16 :                 move16();
     791             :             }
     792             :         }
     793             :     }
     794             : 
     795             :     /* no secondary channel in the previous frame -> memory resets */
     796      422855 :     test();
     797      422855 :     IF( GT_16( hCPE->element_mode, IVAS_CPE_DFT ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
     798             :     {
     799         798 :         IF( sts[0]->cldfbAnaEnc != NULL )
     800             :         {
     801         113 :             offset = sub( sts[0]->cldfbAnaEnc->p_filter_length, sts[0]->cldfbAnaEnc->no_channels ); /* Q0 */
     802       52853 :             FOR( i = 0; i < offset; i++ )
     803             :             {
     804       52740 :                 sts[0]->cldfbAnaEnc->cldfb_state_fx[i] = old_input_signal_pri[input_frame - offset - NS2SA_FX2( L_mult0( input_frame, FRAMES_PER_SEC ), L_MEM_RECALC_TBE_NS ) + i]; /* q_inp */
     805       52740 :                 move32();
     806             :             }
     807         113 :             sts[0]->cldfbAnaEnc->Q_cldfb_state = q_inp;
     808         113 :             move16();
     809             :         }
     810             : 
     811         798 :         IF( sts[0]->cldfbSynTd != NULL )
     812             :         {
     813          52 :             cldfb_reset_memory_fx( sts[0]->cldfbSynTd );
     814          52 :             sts[0]->currEnergyLookAhead_fx = 130996; /* Q31 */
     815          52 :             move32();
     816             :         }
     817             : 
     818         798 :         test();
     819         798 :         IF( hCPE->hStereoICBWE == NULL && sts[1]->cldfbAnaEnc != NULL )
     820             :         {
     821         113 :             offset = sub( sts[1]->cldfbAnaEnc->p_filter_length, sts[1]->cldfbAnaEnc->no_channels );
     822             : 
     823         113 :             test();
     824         113 :             IF( hCPE->hStereoTD != NULL && EQ_16( hCPE->hStereoTD->tdm_last_ratio_idx, LRTD_STEREO_LEFT_IS_PRIM ) )
     825             :             {
     826          36 :                 v_multc_fx( hCPE->hCoreCoder[1]->old_input_signal32_fx + sub( input_frame, add( offset, NS2SA_FX2( L_mult0( input_frame, FRAMES_PER_SEC ), L_MEM_RECALC_TBE_NS ) ) ), -MAX_32, sts[1]->cldfbAnaEnc->cldfb_state_fx, offset ); /* Q16+q_inp */
     827          36 :                 sts[1]->cldfbAnaEnc->Q_cldfb_state = q_inp;
     828          36 :                 move16();
     829             :             }
     830             :             ELSE
     831             :             {
     832       39317 :                 FOR( i = 0; i < offset; i++ )
     833             :                 {
     834       39240 :                     sts[1]->cldfbAnaEnc->cldfb_state_fx[i] = ( hCPE->hCoreCoder[1]->old_input_signal32_fx[input_frame - offset - NS2SA_FX2( L_mult0( input_frame, FRAMES_PER_SEC ), L_MEM_RECALC_TBE_NS ) + i] ); /* q_inp */
     835       39240 :                     move32();
     836             :                 }
     837          77 :                 sts[1]->cldfbAnaEnc->Q_cldfb_state = q_inp;
     838          77 :                 move16();
     839             :             }
     840             : 
     841         113 :             IF( sts[1]->cldfbSynTd != NULL )
     842             :             {
     843          52 :                 cldfb_reset_memory_fx( sts[1]->cldfbSynTd );
     844          52 :                 sts[1]->currEnergyLookAhead_fx = 130996; /* Q31 */
     845          52 :                 move32();
     846             :             }
     847             :         }
     848             : 
     849         798 :         sts[1]->last_extl = -1;
     850         798 :         move16();
     851             : 
     852             :         /* no secondary channel in the previous frame -> memory resets */
     853         798 :         set16_fx( sts[1]->old_inp_12k8_fx, 0, L_INP_MEM );
     854         798 :         set16_fx( sts[1]->mem_decim_fx, 0, 2 * L_FILT_MAX );
     855             : 
     856         798 :         sts[1]->mem_preemph_fx = 0;
     857         798 :         move16();
     858             : 
     859         798 :         set16_fx( sts[1]->buf_speech_enc, 0, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
     860         798 :         sts[1]->exp_buf_speech_enc = 0;
     861         798 :         move16();
     862         798 :         set16_fx( sts[1]->buf_speech_enc_pe, 0, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
     863         798 :         sts[1]->exp_buf_speech_enc_pe = 0;
     864         798 :         move16();
     865         798 :         IF( sts[1]->hTcxEnc != NULL )
     866             :         {
     867         746 :             set16_fx( sts[1]->hTcxEnc->buf_speech_ltp, 0, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
     868             :         }
     869         798 :         set16_fx( sts[1]->buf_wspeech_enc, 0, L_FRAME16k + L_SUBFR + L_FRAME16k + L_NEXT_MAX_16k );
     870         798 :         set16_fx( sts[1]->buf_synth, 0, OLD_SYNTH_SIZE_ENC + L_FRAME32k );
     871         798 :         sts[1]->mem_wsp_fx = 0;
     872         798 :         move16();
     873         798 :         sts[1]->mem_wsp_q = 0;
     874         798 :         move16();
     875         798 :         sts[1]->mem_wsp_enc = 0;
     876         798 :         move16();
     877         798 :         init_gp_clip_fx( sts[1]->clip_var_fx );
     878             : 
     879         798 :         set32_fx( sts[1]->Bin_E_fx, 0, L_FFT );
     880         798 :         sts[1]->q_Bin_E = Q31;
     881         798 :         move16();
     882         798 :         set32_fx( sts[1]->Bin_E_old_fx, 0, L_FFT / 2 );
     883         798 :         sts[1]->q_Bin_E_old = Q31;
     884         798 :         move16();
     885             :         /* sts[1]->hLPDmem reset already done in allocation of handles */
     886             : 
     887         798 :         sts[1]->last_L_frame = sts[0]->last_L_frame;
     888         798 :         move16();
     889         798 :         pitch_ol_init_fx( &sts[1]->old_thres_fx, &sts[1]->old_pitch, &sts[1]->delta_pit, &sts[1]->old_corr_fx );
     890             : 
     891         798 :         set16_fx( sts[1]->old_wsp_fx, 0, L_WSP_MEM );
     892         798 :         set16_fx( sts[1]->old_wsp2_fx, 0, ( L_WSP_MEM - L_INTERPOL ) / OPL_DECIM );
     893         798 :         set16_fx( sts[1]->mem_decim2_fx, 0, 3 );
     894         798 :         Copy( sts[0]->pitch, sts[1]->pitch, 3 );
     895             : 
     896         798 :         sts[1]->Nb_ACELP_frames = 0;
     897         798 :         move16();
     898             : 
     899             :         /* populate PCh memories into the SCh */
     900         798 :         IF( sts[0]->hLPDmem != NULL )
     901             :         {
     902          52 :             Copy( sts[0]->hLPDmem->old_exc, sts[1]->hLPDmem->old_exc, L_EXC_MEM );
     903          52 :             sts[1]->hLPDmem->q_lpd_old_exc = sts[0]->hLPDmem->q_lpd_old_exc;
     904          52 :             move16();
     905             :         }
     906         798 :         Copy( sts[0]->lsf_old_fx, sts[1]->lsf_old_fx, M );   /* Qlog2(2.56) */
     907         798 :         Copy( sts[0]->lsp_old_fx, sts[1]->lsp_old_fx, M );   /* Q15 */
     908         798 :         Copy( sts[0]->lsf_old1_fx, sts[1]->lsf_old1_fx, M ); /* Qlog2(2.56) */
     909         798 :         Copy( sts[0]->lsp_old1_fx, sts[1]->lsp_old1_fx, M ); /* Q15 */
     910             : 
     911         798 :         sts[1]->last_core_brate = sts[0]->last_core_brate;
     912         798 :         move16();
     913             : 
     914         798 :         sts[1]->GSC_noisy_speech = 0;
     915         798 :         move16();
     916             : 
     917         798 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
     918             :         {
     919             :             /* cross-fade overlap region of DFT Stereo downmix and original stereo channels */
     920         746 :             tmp_fx = div_s( 64, shl( sts[0]->encoderLookahead_FB, Q6 ) ); /* Q15 */
     921      298246 :             FOR( i = 0; i < sts[0]->encoderLookahead_FB; i++ )
     922             :             {
     923      297500 :                 sts[1]->input32_fx[-sts[0]->encoderLookahead_FB + i] = ( Mpy_32_16_1( L_add( imult3216( sts[0]->input32_fx[-sts[0]->encoderLookahead_FB + i], sts[0]->encoderLookahead_FB - i ), imult3216( sts[1]->input32_fx[-sts[0]->encoderLookahead_FB + i], i ) ), tmp_fx ) ); /* sts[0]->q_inp32 */
     924      297500 :                 move32();
     925      297500 :                 sts[0]->input32_fx[-sts[0]->encoderLookahead_FB + i] = ( Mpy_32_16_1( L_add( imult3216( sts[0]->input32_fx[-sts[0]->encoderLookahead_FB + i], sts[0]->encoderLookahead_FB - i ), imult3216( sts[1]->input32_fx[-2 * sts[0]->encoderLookahead_FB + i], i ) ), tmp_fx ) ); /* sts[0]->q_inp32 */
     926      297500 :                 move32();
     927             :             }
     928             :             /* restore continuous signal in right channel (part of old_output was used to store original left channel) */
     929         746 :             Copy32( sts[0]->input32_fx - sts[0]->hTcxEnc->L_frameTCX, sts[1]->input32_fx - sts[0]->hTcxEnc->L_frameTCX, sub( sts[0]->hTcxEnc->L_frameTCX, sts[0]->encoderLookahead_FB ) ); /* sts[0]->q_inp32 */
     930             : 
     931         746 :             sts[1]->last_core = sts[0]->last_core; /* Q0 */
     932         746 :             move16();
     933         746 :             sts[1]->last_coder_type = sts[0]->last_coder_type; /* Q0 */
     934         746 :             move16();
     935         746 :             sts[1]->last_bwidth = sts[0]->last_bwidth; /* Q0 */
     936         746 :             move16();
     937             :         }
     938             :     }
     939      422057 :     ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
     940             :     {
     941           8 :         set16_fx( sts[0]->hLPDmem->old_exc, 0, L_EXC_MEM );
     942           8 :         set16_fx( sts[1]->hLPDmem->old_exc, 0, L_EXC_MEM );
     943             :     }
     944      422855 :     test();
     945             : 
     946             :     /* TD/DFT -> MDCT stereo switching (there is no TCX in the TD stereo secondary channel, or DFT stereo) */
     947      422855 :     test();
     948      422855 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && NE_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
     949             :     {
     950         749 :         sts[1]->hTcxCfg->last_aldo = sts[0]->hTcxCfg->last_aldo; /* Q0 */
     951         749 :         move16();
     952         749 :         sts[1]->hTcxCfg->tcx_curr_overlap_mode = sts[0]->hTcxCfg->tcx_curr_overlap_mode;
     953         749 :         move16();
     954             :     }
     955             : 
     956      422855 :     return;
     957             : }

Generated by: LCOV version 1.14