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 enc/dec/rend @ 574a190e3c6896c6c4ed10d7f23649709a0c4347 Lines: 391 422 92.7 %
Date: 2025-06-27 02:59:36 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_fx.h"
      39             : #include "ivas_rom_com.h"
      40             : #include "assert.h"
      41             : #include "wmc_auto.h"
      42             : #include "prot_fx_enc.h"
      43             : #include "ivas_prot_fx.h"
      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_ivas_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      410255 : 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      410255 :     error = IVAS_ERR_OK;
     232      410255 :     move32();
     233             : 
     234      410255 :     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      410255 :     test();
     240      410255 :     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       19956 :             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       39721 :             hCPE->hStereoTCA->prevRefChanIndx = L_CH_INDX;
     251             :         }
     252             :         ELSE
     253             :         {
     254       19956 :             hCPE->hStereoTCA->prevRefChanIndx = R_CH_INDX;
     255             :         }
     256       59677 :         move16();
     257             :     }
     258             : 
     259             :     /*--------------------------------------------------------------*
     260             :      * allocate/deallocate data structures
     261             :      *---------------------------------------------------------------*/
     262             : 
     263      410255 :     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      410255 :     test();
     617      410255 :     test();
     618      410255 :     test();
     619      410255 :     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      410255 :     return error;
     629             : }
     630             : 
     631             : /*-------------------------------------------------------------------*
     632             :  * Function stereo_switching_enc()
     633             :  *
     634             :  * Handling of memories in case of CPE modes switching
     635             :  *-------------------------------------------------------------------*/
     636             : 
     637      410255 : 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      410255 :     move16();
     646             :     Encoder_State **sts;
     647             : 
     648      410255 :     sts = hCPE->hCoreCoder;
     649      410255 :     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      410255 :     test();
     653      410255 :     test();
     654      410255 :     IF( GT_16( hCPE->element_mode, IVAS_CPE_DFT ) && hCPE->input_mem_fx[0] != NULL && NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
     655             :     {
     656       11433 :         FOR( n = 0; n < CPE_CHANNELS; n++ )
     657             :         {
     658        7622 :             Copy_Scale_sig32_16( sts[n]->input32_fx + input_frame - dft_ovl, hCPE->input_mem_fx[n], dft_ovl, 0 ); /* sts[n]->q_inp32 - 16*/
     659        7622 :             hCPE->q_input_mem[n] = sub( sts[n]->q_inp32, 16 );
     660        7622 :             move16();
     661             :         }
     662             :     }
     663             : 
     664             :     /* save original stereo input (MDCT overlap part) for both channels in unused old input of right channel for possible DFT->MDCT transition */
     665      410255 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
     666             :     {
     667       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 */
     668             : 
     669       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 */
     670             :     }
     671             : 
     672             : 
     673             :     /* TD/MDCT -> DFT stereo switching */
     674      410255 :     test();
     675      410255 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && NE_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
     676             :     {
     677             :         /* window DFT synthesis overlap memory @input_Fs, primary channel */
     678      306680 :         FOR( i = 0; i < dft_ovl; i++ )
     679             :         {
     680      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
     681      305900 :             move32();
     682             :         }
     683             :         /* reset 48kHz BWE overlap memory */
     684         780 :         set32_fx( hCPE->hStereoDft->output_mem_dmx_32k_fx, 0, STEREO_DFT_OVL_32k );
     685             : 
     686         780 :         stereo_dft_enc_reset_fx( hCPE->hStereoDft );
     687             : 
     688             :         /* update ITD parameters */
     689         780 :         test();
     690         780 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_TD ) )
     691             :         {
     692          56 :             set32_fx( hCPE->hStereoDft->hItd->itd_fx, hCPE->hStereoTCA->prevCorrLagStats[2], STEREO_DFT_ENC_DFT_NB );
     693             :         }
     694             : 
     695             :         /* Update the side_gain[] parameters */
     696         780 :         test();
     697         780 :         IF( hCPE->hStereoTCA != NULL && NE_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
     698             :         {
     699          56 :             tmp_fx = usdequant_fx( hCPE->hStereoTCA->indx_ica_gD, STEREO_TCA_GDMIN_FX, STEREO_TCA_GDSTEP_FX );
     700         784 :             FOR( i = 0; i < STEREO_DFT_BAND_MAX; i++ )
     701             :             {
     702         728 :                 hCPE->hStereoDft->side_gain_fx[STEREO_DFT_BAND_MAX + i] = L_deposit_h( tmp_fx ); /* Q31 */
     703         728 :                 move32();
     704             :             }
     705             :         }
     706             : 
     707             :         /* do not allow differential coding of DFT side parameters */
     708         780 :         hCPE->hStereoDft->res_pred_counter = STEREO_DFT_FEC_THRESHOLD;
     709         780 :         move16();
     710             : 
     711             :         /* update DFT synthesis overlap memory @12.8kHz */
     712       88140 :         FOR( i = 0; i < STEREO_DFT_OVL_12k8; i++ )
     713             :         {
     714       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 */
     715       87360 :             move32();
     716             :         }
     717         780 :         Word16 q_dmx = Q15;
     718         780 :         move16();
     719             :         /* update DFT synthesis overlap memory @16kHz, primary channel only */
     720         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 );
     721      109980 :         FOR( i = 0; i < STEREO_DFT_OVL_16k; i++ )
     722             :         {
     723      109200 :             hCPE->hStereoDft->output_mem_dmx_16k_fx[i] = L_shl( hCPE->hStereoDft->output_mem_dmx_16k_fx[i], sub( Q15, q_dmx ) ); // Q15
     724      109200 :             move32();
     725             :         }
     726             : 
     727      328380 :         FOR( i = 0; i < STEREO_DFT_OVL_MAX; i++ )
     728             :         {
     729      327600 :             hCPE->hStereoDft->output_mem_dmx_fx[i] = L_shl( hCPE->hStereoDft->output_mem_dmx_fx[i], sub( Q15, q_dmx ) ); // Q15
     730      327600 :             move32();
     731             :         }
     732             : 
     733             :         /* reset DFT synthesis overlap memory @8kHz, secondary channel */
     734         780 :         set32_fx( hCPE->hStereoDft->output_mem_res_8k_fx, 0, STEREO_DFT_OVL_8k );
     735             : 
     736         780 :         hCPE->hCoreCoder[1]->vad_flag = 0;
     737         780 :         move16();
     738             :     }
     739             : 
     740             :     /* MDCT -> TD stereo switching */
     741      410255 :     test();
     742      410255 :     test();
     743      410255 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
     744             :     {
     745           8 :         hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_LEFT_IS_PRIM;
     746           8 :         move16();
     747           8 :         hCPE->hStereoTD->tdm_last_ratio_idx_SM = LRTD_STEREO_LEFT_IS_PRIM;
     748           8 :         move16();
     749           8 :         hCPE->hStereoTD->tdm_last_SM_flag = 0;
     750           8 :         move16();
     751           8 :         hCPE->hStereoTD->tdm_last_inst_ratio_idx = LRTD_STEREO_MID_IS_PRIM;
     752           8 :         move16();
     753           8 :         hCPE->hStereoTD->tdm_last_ratio_fx = tdm_ratio_tabl_fx[LRTD_STEREO_LEFT_IS_PRIM];
     754           8 :         move32();
     755             :     }
     756             :     /* DFT -> TD stereo switching */
     757      410247 :     ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
     758             :     {
     759          52 :         hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_MID_IS_PRIM;
     760          52 :         move16();
     761          52 :         hCPE->hStereoTD->tdm_last_ratio_idx_SM = LRTD_STEREO_MID_IS_PRIM;
     762          52 :         move16();
     763          52 :         hCPE->hStereoTD->tdm_last_SM_flag = 0;
     764          52 :         move16();
     765          52 :         hCPE->hStereoTD->tdm_last_inst_ratio_idx = LRTD_STEREO_MID_IS_PRIM;
     766          52 :         move16();
     767             : 
     768             :         /* First frame after DFT frame AND the content is uncorrelated or xtalk -> the primary channel is forced to left */
     769          52 :         IF( EQ_16( hCPE->hStereoClassif->lrtd_mode, 1 ) )
     770             :         {
     771          52 :             set_zero_fx( sts[1]->input32_fx - input_frame, input_frame );
     772          52 :             set16_zero_fx( sts[1]->input_fx - input_frame, input_frame );
     773          52 :             sts[1]->q_old_inp = Q15;
     774          52 :             hCPE->hStereoTD->tdm_last_ratio_fx = tdm_ratio_tabl_fx[LRTD_STEREO_LEFT_IS_PRIM]; /* Q31 */
     775          52 :             move16();
     776          52 :             move32();
     777             : 
     778          52 :             hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_LEFT_IS_PRIM;
     779          52 :             move16();
     780             : 
     781          52 :             test();
     782          52 :             test();
     783          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 */
     784             :             {
     785          16 :                 hCPE->hStereoTD->tdm_last_ratio_fx = tdm_ratio_tabl_fx[LRTD_STEREO_RIGHT_IS_PRIM]; /* Q31 */
     786          16 :                 move32();
     787          16 :                 hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_RIGHT_IS_PRIM;
     788          16 :                 move16();
     789             :             }
     790             :         }
     791             :     }
     792             : 
     793             :     /* no secondary channel in the previous frame -> memory resets */
     794      410255 :     test();
     795      410255 :     IF( GT_16( hCPE->element_mode, IVAS_CPE_DFT ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
     796             :     {
     797         798 :         IF( sts[0]->cldfbAnaEnc != NULL )
     798             :         {
     799         113 :             offset = sub( sts[0]->cldfbAnaEnc->p_filter_length, sts[0]->cldfbAnaEnc->no_channels ); /* Q0 */
     800       52853 :             FOR( i = 0; i < offset; i++ )
     801             :             {
     802       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 */
     803       52740 :                 move32();
     804             :             }
     805         113 :             sts[0]->cldfbAnaEnc->Q_cldfb_state = q_inp;
     806         113 :             move16();
     807             :         }
     808             : 
     809         798 :         IF( sts[0]->cldfbSynTd != NULL )
     810             :         {
     811          52 :             cldfb_reset_memory_fx( sts[0]->cldfbSynTd );
     812          52 :             sts[0]->currEnergyLookAhead_fx = 130996; /* Q31 */
     813          52 :             move32();
     814             :         }
     815             : 
     816         798 :         test();
     817         798 :         IF( hCPE->hStereoICBWE == NULL && sts[1]->cldfbAnaEnc != NULL )
     818             :         {
     819         113 :             offset = sub( sts[1]->cldfbAnaEnc->p_filter_length, sts[1]->cldfbAnaEnc->no_channels );
     820             : 
     821         113 :             test();
     822         113 :             IF( hCPE->hStereoTD != NULL && EQ_16( hCPE->hStereoTD->tdm_last_ratio_idx, LRTD_STEREO_LEFT_IS_PRIM ) )
     823             :             {
     824          36 :                 v_multc_fixed( 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 */
     825          36 :                 sts[1]->cldfbAnaEnc->Q_cldfb_state = q_inp;
     826          36 :                 move16();
     827             :             }
     828             :             ELSE
     829             :             {
     830       39317 :                 FOR( i = 0; i < offset; i++ )
     831             :                 {
     832       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 */
     833       39240 :                     move32();
     834             :                 }
     835          77 :                 sts[1]->cldfbAnaEnc->Q_cldfb_state = q_inp;
     836          77 :                 move16();
     837             :             }
     838             : 
     839         113 :             IF( sts[1]->cldfbSynTd != NULL )
     840             :             {
     841          52 :                 cldfb_reset_memory_fx( sts[1]->cldfbSynTd );
     842          52 :                 sts[1]->currEnergyLookAhead_fx = 130996; /* Q31 */
     843          52 :                 move32();
     844             :             }
     845             :         }
     846             : 
     847         798 :         sts[1]->last_extl = -1;
     848         798 :         move16();
     849             : 
     850             :         /* no secondary channel in the previous frame -> memory resets */
     851         798 :         set16_fx( sts[1]->old_inp_12k8_fx, 0, L_INP_MEM );
     852         798 :         set16_fx( sts[1]->mem_decim_fx, 0, 2 * L_FILT_MAX );
     853             : 
     854         798 :         sts[1]->mem_preemph_fx = 0;
     855         798 :         move16();
     856             : 
     857         798 :         set16_fx( sts[1]->buf_speech_enc, 0, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
     858         798 :         sts[1]->exp_buf_speech_enc = 0;
     859         798 :         move16();
     860         798 :         set16_fx( sts[1]->buf_speech_enc_pe, 0, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
     861         798 :         sts[1]->exp_buf_speech_enc_pe = 0;
     862         798 :         move16();
     863         798 :         IF( sts[1]->hTcxEnc != NULL )
     864             :         {
     865         746 :             set16_fx( sts[1]->hTcxEnc->buf_speech_ltp, 0, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
     866             :         }
     867         798 :         set16_fx( sts[1]->buf_wspeech_enc, 0, L_FRAME16k + L_SUBFR + L_FRAME16k + L_NEXT_MAX_16k );
     868         798 :         set16_fx( sts[1]->buf_synth, 0, OLD_SYNTH_SIZE_ENC + L_FRAME32k );
     869         798 :         sts[1]->mem_wsp_fx = 0;
     870         798 :         move16();
     871         798 :         sts[1]->mem_wsp_q = 0;
     872         798 :         move16();
     873         798 :         sts[1]->mem_wsp_enc = 0;
     874         798 :         move16();
     875         798 :         init_gp_clip_fx( sts[1]->clip_var_fx );
     876             : 
     877         798 :         set32_fx( sts[1]->Bin_E_fx, 0, L_FFT );
     878         798 :         sts[1]->q_Bin_E = Q31;
     879         798 :         move16();
     880         798 :         set32_fx( sts[1]->Bin_E_old_fx, 0, L_FFT / 2 );
     881         798 :         sts[1]->q_Bin_E_old = Q31;
     882         798 :         move16();
     883             :         /* sts[1]->hLPDmem reset already done in allocation of handles */
     884             : 
     885         798 :         sts[1]->last_L_frame = sts[0]->last_L_frame;
     886         798 :         move16();
     887         798 :         pitch_ol_init_fx( &sts[1]->old_thres_fx, &sts[1]->old_pitch, &sts[1]->delta_pit, &sts[1]->old_corr_fx );
     888             : 
     889         798 :         set16_fx( sts[1]->old_wsp_fx, 0, L_WSP_MEM );
     890         798 :         set16_fx( sts[1]->old_wsp2_fx, 0, ( L_WSP_MEM - L_INTERPOL ) / OPL_DECIM );
     891         798 :         set16_fx( sts[1]->mem_decim2_fx, 0, 3 );
     892         798 :         Copy( sts[0]->pitch, sts[1]->pitch, 3 );
     893             : 
     894         798 :         sts[1]->Nb_ACELP_frames = 0;
     895         798 :         move16();
     896             : 
     897             :         /* populate PCh memories into the SCh */
     898         798 :         IF( sts[0]->hLPDmem != NULL )
     899             :         {
     900          52 :             Copy( sts[0]->hLPDmem->old_exc, sts[1]->hLPDmem->old_exc, L_EXC_MEM );
     901          52 :             sts[1]->hLPDmem->q_lpd_old_exc = sts[0]->hLPDmem->q_lpd_old_exc;
     902          52 :             move16();
     903             :         }
     904         798 :         Copy( sts[0]->lsf_old_fx, sts[1]->lsf_old_fx, M );   /* Qlog2(2.56) */
     905         798 :         Copy( sts[0]->lsp_old_fx, sts[1]->lsp_old_fx, M );   /* Q15 */
     906         798 :         Copy( sts[0]->lsf_old1_fx, sts[1]->lsf_old1_fx, M ); /* Qlog2(2.56) */
     907         798 :         Copy( sts[0]->lsp_old1_fx, sts[1]->lsp_old1_fx, M ); /* Q15 */
     908             : 
     909         798 :         sts[1]->GSC_noisy_speech = 0;
     910         798 :         move16();
     911         798 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
     912             :         {
     913             :             /* cross-fade overlap region of DFT Stereo downmix and original stereo channels */
     914         746 :             tmp_fx = div_s( 64, shl( sts[0]->encoderLookahead_FB, Q6 ) ); /* Q15 */
     915      298246 :             FOR( i = 0; i < sts[0]->encoderLookahead_FB; i++ )
     916             :             {
     917      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 */
     918      297500 :                 move32();
     919      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 */
     920      297500 :                 move32();
     921             :             }
     922             :             /* restore continuous signal in right channel (part of old_output was used to store original left channel) */
     923         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 */
     924             : 
     925         746 :             sts[1]->last_core = sts[0]->last_core; /* Q0 */
     926         746 :             move16();
     927         746 :             sts[1]->last_coder_type = sts[0]->last_coder_type; /* Q0 */
     928         746 :             move16();
     929         746 :             sts[1]->last_bwidth = sts[0]->last_bwidth; /* Q0 */
     930         746 :             move16();
     931             :         }
     932             :     }
     933      409457 :     ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
     934             :     {
     935           8 :         set16_fx( sts[0]->hLPDmem->old_exc, 0, L_EXC_MEM );
     936           8 :         set16_fx( sts[1]->hLPDmem->old_exc, 0, L_EXC_MEM );
     937             :     }
     938      410255 :     test();
     939             : 
     940             :     /* TD/DFT -> MDCT stereo switching (there is no TCX in the TD stereo secondary channel, or DFT stereo) */
     941      410255 :     test();
     942      410255 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && NE_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
     943             :     {
     944         749 :         sts[1]->hTcxCfg->last_aldo = sts[0]->hTcxCfg->last_aldo; /* Q0 */
     945         749 :         move16();
     946         749 :         sts[1]->hTcxCfg->tcx_curr_overlap_mode = sts[0]->hTcxCfg->tcx_curr_overlap_mode;
     947         749 :         move16();
     948             :     }
     949             : 
     950      410255 :     return;
     951             : }

Generated by: LCOV version 1.14