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 @ 3b2f07138c61dcf997bbf4165d0882f794b2995f Lines: 394 425 92.7 %
Date: 2025-05-03 01:55:50 Functions: 6 6 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        1540 : static ivas_error allocate_CoreCoder_enc_fx(
      52             :     ENC_CORE_HANDLE st /* i/o: Core encoder state structure     */
      53             : )
      54             : {
      55        1540 :     IF( st->hLPDmem == NULL && NE_16( st->element_mode, IVAS_CPE_MDCT ) )
      56             :     {
      57         794 :         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         794 :         LPDmem_enc_init_ivas_fx( st->hLPDmem );
      62             :     }
      63             : 
      64        1540 :     IF( st->hGSCEnc == NULL && NE_16( st->element_mode, IVAS_CPE_MDCT ) )
      65             :     {
      66         794 :         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         794 :         GSC_enc_init_fx( st->hGSCEnc );
      71             :     }
      72             : 
      73        1540 :     IF( st->hNoiseEst == NULL )
      74             :     {
      75         800 :         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         800 :         noise_est_init_ivas_fx( st->hNoiseEst );
      80             :     }
      81             : 
      82        1540 :     IF( st->hVAD == NULL )
      83             :     {
      84         800 :         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         800 :         wb_vad_init_ivas_fx( st->hVAD );
      89             :     }
      90             : 
      91        1540 :     IF( st->hSpMusClas == NULL )
      92             :     {
      93         800 :         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         800 :         speech_music_clas_init_ivas_fx( st->hSpMusClas );
      98             :     }
      99             : 
     100        1540 :     return IVAS_ERR_OK;
     101             : }
     102             : 
     103             : /*-------------------------------------------------------------------*
     104             :  * Function deallocate_CoreCoder_TCX_enc()
     105             :  *
     106             :  * Deallocate CoreCoder TCX modules
     107             :  *-------------------------------------------------------------------*/
     108             : 
     109         844 : static void deallocate_CoreCoder_TCX_enc_fx(
     110             :     ENC_CORE_HANDLE st /* i/o: Core encoder state structure     */
     111             : )
     112             : {
     113         844 :     IF( st->hTcxEnc != NULL )
     114             :     {
     115         732 :         free( st->hTcxEnc );
     116         732 :         st->hTcxEnc = NULL;
     117             :     }
     118             : 
     119         844 :     IF( st->hTcxCfg != NULL )
     120             :     {
     121         732 :         free( st->hTcxCfg );
     122         732 :         st->hTcxCfg = NULL;
     123             :     }
     124             : 
     125         844 :     IF( st->hIGFEnc != NULL )
     126             :     {
     127         533 :         free( st->hIGFEnc );
     128         533 :         st->hIGFEnc = NULL;
     129             :     }
     130             : 
     131         844 :     IF( st->hHQ_core != NULL )
     132             :     {
     133           0 :         free( st->hHQ_core );
     134           0 :         st->hHQ_core = NULL;
     135             :     }
     136             : 
     137         844 :     return;
     138             : }
     139             : 
     140             : 
     141             : /*-------------------------------------------------------------------*
     142             :  * Function deallocate_CoreCoder_enc()
     143             :  *
     144             :  * Deallocate CoreCoder modules
     145             :  *-------------------------------------------------------------------*/
     146             : 
     147        2280 : static void deallocate_CoreCoder_enc_fx(
     148             :     ENC_CORE_HANDLE st /* i/o: Core encoder state structure     */
     149             : )
     150             : {
     151        2280 :     IF( st->hLPDmem != NULL )
     152             :     {
     153         810 :         free( st->hLPDmem );
     154         810 :         st->hLPDmem = NULL;
     155             :     }
     156             : 
     157        2280 :     IF( st->hGSCEnc != NULL )
     158             :     {
     159         810 :         free( st->hGSCEnc );
     160         810 :         st->hGSCEnc = NULL;
     161             :     }
     162             : 
     163        2280 :     test();
     164        2280 :     IF( st->hNoiseEst != NULL && NE_16( st->element_mode, IVAS_CPE_MDCT ) )
     165             :     {
     166         782 :         free( st->hNoiseEst );
     167         782 :         st->hNoiseEst = NULL;
     168             :     }
     169             : 
     170        2280 :     test();
     171        2280 :     IF( st->hVAD != NULL && NE_16( st->element_mode, IVAS_CPE_MDCT ) )
     172             :     {
     173         782 :         free( st->hVAD );
     174         782 :         st->hVAD = NULL;
     175             :     }
     176             : 
     177        2280 :     test();
     178        2280 :     IF( st->hSpMusClas != NULL && NE_16( st->element_mode, IVAS_CPE_MDCT ) )
     179             :     {
     180         782 :         free( st->hSpMusClas );
     181         782 :         st->hSpMusClas = NULL;
     182             :     }
     183             : 
     184        2280 :     IF( st->cldfbAnaEnc != NULL )
     185             :     {
     186         834 :         deleteCldfb_ivas_fx( &st->cldfbAnaEnc );
     187             :     }
     188             : 
     189        2280 :     IF( st->hBWE_TD != NULL )
     190             :     {
     191         776 :         free( st->hBWE_TD );
     192         776 :         st->hBWE_TD = NULL;
     193             :     }
     194             : 
     195        2280 :     IF( st->cldfbSynTd != NULL )
     196             :     {
     197         776 :         deleteCldfb_ivas_fx( &st->cldfbSynTd );
     198             :     }
     199             : 
     200        2280 :     IF( st->hBWE_FD != NULL )
     201             :     {
     202         776 :         free( st->hBWE_FD );
     203         776 :         st->hBWE_FD = NULL;
     204             :     }
     205             : 
     206        2280 :     IF( st->element_mode != IVAS_CPE_MDCT )
     207             :     {
     208         782 :         deallocate_CoreCoder_TCX_enc_fx( st );
     209             :     }
     210             : 
     211        2280 :     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       59661 :         Word16 tmp = extract_h( L_abs( hCPE->hStereoDft->hItd->itd_fx[1] ) );
     243       59661 :         if ( hCPE->hStereoDft->hItd->itd_fx[1] < 0 )
     244             :         {
     245       19959 :             tmp = negate( tmp );
     246             :         }
     247       59661 :         set16_fx( hCPE->hStereoTCA->prevCorrLagStats, tmp, 3 );
     248       59661 :         IF( hCPE->hStereoDft->hItd->itd_fx[1] >= 0 )
     249             :         {
     250       39702 :             hCPE->hStereoTCA->prevRefChanIndx = L_CH_INDX;
     251             :         }
     252             :         ELSE
     253             :         {
     254       19959 :             hCPE->hStereoTCA->prevRefChanIndx = R_CH_INDX;
     255             :         }
     256       59661 :         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        1593 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
     270             :         {
     271             :             /* deallocate data structure of the previous CPE mode */
     272         782 :             IF( hCPE->hStereoTD != NULL )
     273             :             {
     274          58 :                 free( hCPE->hStereoTD );
     275          58 :                 hCPE->hStereoTD = NULL;
     276             :             }
     277             : 
     278         782 :             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         782 :             deallocate_CoreCoder_enc_fx( hCPE->hCoreCoder[1] );
     286             : 
     287             :             /* allocate DFT stereo data structure */
     288         782 :             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         782 :             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         782 :             st = hCPE->hCoreCoder[0];
     305         782 :             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        1593 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
     321             :         {
     322             :             /* deallocate data structure of the previous CPE mode */
     323          62 :             IF( hCPE->hStereoDft != NULL )
     324             :             {
     325          54 :                 stereo_dft_enc_destroy_fx( &( hCPE->hStereoDft ) );
     326          54 :                 hCPE->hStereoDft = NULL;
     327             :             }
     328             : 
     329          62 :             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          62 :             deallocate_CoreCoder_TCX_enc_fx( hCPE->hCoreCoder[1] );
     337             : 
     338             :             /* allocate TD stereo data structure */
     339          62 :             IF( hCPE->hStereoTD != NULL )
     340             :             {
     341           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: TD Stereo memory already allocated\n" );
     342             :             }
     343             : 
     344          62 :             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          62 :             stereo_td_init_enc_fx( hCPE->hStereoTD, hCPE->last_element_mode );
     350             :             /* allocate secondary channel */
     351          62 :             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        1593 :         test();
     361        1593 :         test();
     362        1593 :         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        1593 :         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          38 : static void v_multc_fixed_32_16(
     632             :     const Word16 x[], /* i  : Input vector                                     Qx*/
     633             :     const Word32 c,   /* i  : Constant                                         Q31*/
     634             :     Word32 y[],       /* o  : Output vector that contains c*x                  Qx*/
     635             :     const Word16 N    /* i  : Vector length                                    Q0*/
     636             : )
     637             : {
     638             :     Word16 i;
     639             : 
     640       13898 :     FOR( i = 0; i < N; i++ )
     641             :     {
     642       13860 :         y[i] = Mpy_32_16_1( c, x[i] ); /* Q16+Qx */
     643       13860 :         move32();
     644             :     }
     645             : 
     646          38 :     return;
     647             : }
     648             : /*-------------------------------------------------------------------*
     649             :  * Function stereo_switching_enc()
     650             :  *
     651             :  * Handling of memories in case of CPE modes switching
     652             :  *-------------------------------------------------------------------*/
     653             : 
     654      410255 : void stereo_switching_enc_fx(
     655             :     CPE_ENC_HANDLE hCPE,           /* i  : CPE encoder structure               */
     656             :     Word16 old_input_signal_pri[], /* i  : old input signal of primary channel q_inp*/
     657             :     const Word16 input_frame,      /* i  : input frame length                  Q0*/
     658             :     const Word16 q_inp )
     659             : {
     660             :     Word16 i, n, dft_ovl, offset;
     661             :     Word16 tmp_fx;
     662      410255 :     move16();
     663             :     Encoder_State **sts;
     664             : 
     665      410255 :     sts = hCPE->hCoreCoder;
     666      410255 :     dft_ovl = extract_l( Mpy_32_32( imult3216( input_frame, STEREO_DFT_OVL_MAX ), 2236963 ) ); // 1/L_FRAME48k = 2236963 (Q31)
     667             : 
     668             :     /* update DFT analysis overlap memory */
     669      410255 :     test();
     670      410255 :     test();
     671      410255 :     IF( GT_16( hCPE->element_mode, IVAS_CPE_DFT ) && hCPE->input_mem_fx[0] != NULL && NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
     672             :     {
     673       11481 :         FOR( n = 0; n < CPE_CHANNELS; n++ )
     674             :         {
     675        7654 :             Copy( sts[n]->input_fx + input_frame - dft_ovl, hCPE->input_mem_fx[n], dft_ovl ); /* sts[n]->q_inp */
     676        7654 :             hCPE->q_input_mem[n] = sts[n]->q_inp;
     677        7654 :             move16();
     678             :         }
     679             :     }
     680             : 
     681             :     /* save original stereo input (MDCT overlap part) for both channels in unused old input of right channel for possible DFT->MDCT transition */
     682      410255 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
     683             :     {
     684       59643 :         Copy( sts[0]->input_fx + sub( (Word16) Mpy_32_32( sts[0]->input_Fs, 42949673 /* 1/50 in Q31*/ ), sts[0]->encoderLookahead_FB ), sts[1]->input_fx - shl( sts[0]->encoderLookahead_FB, 1 ), sts[0]->encoderLookahead_FB ); /* sts[0]->q_inp */
     685             : 
     686       59643 :         Copy( sts[1]->input_fx + sub( (Word16) Mpy_32_32( sts[1]->input_Fs, 42949673 /* 1/50 in Q31*/ ), sts[1]->encoderLookahead_FB ), sts[1]->input_fx - sts[1]->encoderLookahead_FB, sts[1]->encoderLookahead_FB ); /* sts[1]->q_inp */
     687             :     }
     688             : 
     689             : 
     690             :     /* TD/MDCT -> DFT stereo switching */
     691      410255 :     test();
     692      410255 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && NE_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
     693             :     {
     694             :         /* window DFT synthesis overlap memory @input_Fs, primary channel */
     695      306962 :         FOR( i = 0; i < dft_ovl; i++ )
     696             :         {
     697      306180 :             hCPE->hStereoDft->output_mem_dmx_fx[i] = L_shr( Mpy_32_16_r( hCPE->hStereoDft->win_fx[dft_ovl - 1 - i], old_input_signal_pri[input_frame - dft_ovl + i] ), add( q_inp, 1 ) ); // Q31 + q_inp - Q15 - (q_inp + 1) -> Q15
     698      306180 :             move32();
     699             :         }
     700             :         /* reset 48kHz BWE overlap memory */
     701         782 :         set32_fx( hCPE->hStereoDft->output_mem_dmx_32k_fx, 0, STEREO_DFT_OVL_32k );
     702             : 
     703         782 :         stereo_dft_enc_reset_fx( hCPE->hStereoDft );
     704             : 
     705             :         /* update ITD parameters */
     706         782 :         test();
     707         782 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_TD ) )
     708             :         {
     709          58 :             set32_fx( hCPE->hStereoDft->hItd->itd_fx, hCPE->hStereoTCA->prevCorrLagStats[2], STEREO_DFT_ENC_DFT_NB );
     710             :         }
     711             : 
     712             :         /* Update the side_gain[] parameters */
     713         782 :         test();
     714         782 :         IF( hCPE->hStereoTCA != NULL && NE_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
     715             :         {
     716          58 :             tmp_fx = usdequant_fx( hCPE->hStereoTCA->indx_ica_gD, STEREO_TCA_GDMIN_FX, STEREO_TCA_GDSTEP_FX );
     717         812 :             FOR( i = 0; i < STEREO_DFT_BAND_MAX; i++ )
     718             :             {
     719         754 :                 hCPE->hStereoDft->side_gain_fx[STEREO_DFT_BAND_MAX + i] = L_deposit_h( tmp_fx ); /* Q31 */
     720         754 :                 move32();
     721             :             }
     722             :         }
     723             : 
     724             :         /* do not allow differential coding of DFT side parameters */
     725         782 :         hCPE->hStereoDft->res_pred_counter = STEREO_DFT_FEC_THRESHOLD;
     726         782 :         move16();
     727             : 
     728             :         /* update DFT synthesis overlap memory @12.8kHz */
     729       88366 :         FOR( i = 0; i < STEREO_DFT_OVL_12k8; i++ )
     730             :         {
     731       87584 :             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] ), q_inp + 1 ); /* Q15 */
     732       87584 :             move32();
     733             :         }
     734         782 :         Word16 q_dmx = Q15;
     735         782 :         move16();
     736             :         /* update DFT synthesis overlap memory @16kHz, primary channel only */
     737         782 :         L_lerp_fx( hCPE->hStereoDft->output_mem_dmx_fx, hCPE->hStereoDft->output_mem_dmx_16k_fx, STEREO_DFT_OVL_16k, dft_ovl, &q_dmx );
     738      110262 :         FOR( i = 0; i < STEREO_DFT_OVL_16k; i++ )
     739             :         {
     740      109480 :             hCPE->hStereoDft->output_mem_dmx_16k_fx[i] = L_shl( hCPE->hStereoDft->output_mem_dmx_16k_fx[i], sub( Q15, q_dmx ) ); // Q15
     741      109480 :             move32();
     742             :         }
     743             : 
     744      329222 :         FOR( i = 0; i < STEREO_DFT_OVL_MAX; i++ )
     745             :         {
     746      328440 :             hCPE->hStereoDft->output_mem_dmx_fx[i] = L_shl( hCPE->hStereoDft->output_mem_dmx_fx[i], sub( Q15, q_dmx ) ); // Q15
     747      328440 :             move32();
     748             :         }
     749             : 
     750             :         /* reset DFT synthesis overlap memory @8kHz, secondary channel */
     751         782 :         set32_fx( hCPE->hStereoDft->output_mem_res_8k_fx, 0, STEREO_DFT_OVL_8k );
     752             : 
     753         782 :         hCPE->hCoreCoder[1]->vad_flag = 0;
     754         782 :         move16();
     755             :     }
     756             : 
     757             :     /* MDCT -> TD stereo switching */
     758      410255 :     test();
     759      410255 :     test();
     760      410255 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
     761             :     {
     762           8 :         hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_LEFT_IS_PRIM;
     763           8 :         move16();
     764           8 :         hCPE->hStereoTD->tdm_last_ratio_idx_SM = LRTD_STEREO_LEFT_IS_PRIM;
     765           8 :         move16();
     766           8 :         hCPE->hStereoTD->tdm_last_SM_flag = 0;
     767           8 :         move16();
     768           8 :         hCPE->hStereoTD->tdm_last_inst_ratio_idx = LRTD_STEREO_MID_IS_PRIM;
     769           8 :         move16();
     770           8 :         hCPE->hStereoTD->tdm_last_ratio_fx = tdm_ratio_tabl_fx[LRTD_STEREO_LEFT_IS_PRIM];
     771           8 :         move32();
     772             :     }
     773             :     /* DFT -> TD stereo switching */
     774      410247 :     ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
     775             :     {
     776          54 :         hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_MID_IS_PRIM;
     777          54 :         move16();
     778          54 :         hCPE->hStereoTD->tdm_last_ratio_idx_SM = LRTD_STEREO_MID_IS_PRIM;
     779          54 :         move16();
     780          54 :         hCPE->hStereoTD->tdm_last_SM_flag = 0;
     781          54 :         move16();
     782          54 :         hCPE->hStereoTD->tdm_last_inst_ratio_idx = LRTD_STEREO_MID_IS_PRIM;
     783          54 :         move16();
     784             : 
     785             :         /* First frame after DFT frame AND the content is uncorrelated or xtalk -> the primary channel is forced to left */
     786          54 :         IF( EQ_16( hCPE->hStereoClassif->lrtd_mode, 1 ) )
     787             :         {
     788          54 :             set_zero_fx( sts[1]->input32_fx - input_frame, input_frame );
     789          54 :             set16_zero_fx( sts[1]->input_fx - input_frame, input_frame );
     790          54 :             sts[1]->q_old_inp = Q15;
     791          54 :             hCPE->hStereoTD->tdm_last_ratio_fx = tdm_ratio_tabl_fx[LRTD_STEREO_LEFT_IS_PRIM]; /* Q31 */
     792          54 :             move16();
     793          54 :             move32();
     794             : 
     795          54 :             hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_LEFT_IS_PRIM;
     796          54 :             move16();
     797             : 
     798          54 :             test();
     799          54 :             test();
     800          54 :             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 */
     801             :             {
     802          16 :                 hCPE->hStereoTD->tdm_last_ratio_fx = tdm_ratio_tabl_fx[LRTD_STEREO_RIGHT_IS_PRIM]; /* Q31 */
     803          16 :                 move32();
     804          16 :                 hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_RIGHT_IS_PRIM;
     805          16 :                 move16();
     806             :             }
     807             :         }
     808             :     }
     809             : 
     810             :     /* no secondary channel in the previous frame -> memory resets */
     811      410255 :     test();
     812      410255 :     IF( GT_16( hCPE->element_mode, IVAS_CPE_DFT ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
     813             :     {
     814         800 :         IF( sts[0]->cldfbAnaEnc != NULL )
     815             :         {
     816         115 :             offset = sub( sts[0]->cldfbAnaEnc->p_filter_length, sts[0]->cldfbAnaEnc->no_channels ); /* Q0 */
     817       53215 :             FOR( i = 0; i < offset; i++ )
     818             :             {
     819       53100 :                 sts[0]->cldfbAnaEnc->cldfb_state_fx[i] = L_deposit_h( old_input_signal_pri[input_frame - offset - NS2SA_FX2( L_mult0( input_frame, FRAMES_PER_SEC ), L_MEM_RECALC_TBE_NS ) + i] ); /* Q16+q_inp */
     820       53100 :                 move32();
     821             :             }
     822         115 :             sts[0]->cldfbAnaEnc->Q_cldfb_state = add( Q16, q_inp );
     823         115 :             move16();
     824             :         }
     825             : 
     826         800 :         IF( sts[0]->cldfbSynTd != NULL )
     827             :         {
     828          54 :             cldfb_reset_memory_fx( sts[0]->cldfbSynTd );
     829          54 :             sts[0]->currEnergyLookAhead_fx = 130996; /* Q31 */
     830          54 :             move32();
     831             :         }
     832             : 
     833         800 :         test();
     834         800 :         IF( hCPE->hStereoICBWE == NULL && sts[1]->cldfbAnaEnc != NULL )
     835             :         {
     836         115 :             offset = sub( sts[1]->cldfbAnaEnc->p_filter_length, sts[1]->cldfbAnaEnc->no_channels );
     837             : 
     838         115 :             test();
     839         115 :             IF( hCPE->hStereoTD != NULL && EQ_16( hCPE->hStereoTD->tdm_last_ratio_idx, LRTD_STEREO_LEFT_IS_PRIM ) )
     840             :             {
     841          38 :                 v_multc_fixed_32_16( hCPE->hCoreCoder[1]->old_input_signal_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 */
     842          38 :                 sts[1]->cldfbAnaEnc->Q_cldfb_state = add( Q16, q_inp );
     843          38 :                 move16();
     844             :             }
     845             :             ELSE
     846             :             {
     847       39317 :                 FOR( i = 0; i < offset; i++ )
     848             :                 {
     849       39240 :                     sts[1]->cldfbAnaEnc->cldfb_state_fx[i] = L_shr( L_deposit_h( hCPE->hCoreCoder[1]->old_input_signal_fx[input_frame - offset - NS2SA_FX2( L_mult0( input_frame, FRAMES_PER_SEC ), L_MEM_RECALC_TBE_NS ) + i] ), 5 ); /* Q11+q_inp */
     850       39240 :                     move32();
     851             :                 }
     852          77 :                 sts[1]->cldfbAnaEnc->Q_cldfb_state = add( Q16 - 5, q_inp );
     853          77 :                 move16();
     854             :             }
     855             : 
     856         115 :             IF( sts[1]->cldfbSynTd != NULL )
     857             :             {
     858          54 :                 cldfb_reset_memory_fx( sts[1]->cldfbSynTd );
     859          54 :                 sts[1]->currEnergyLookAhead_fx = 130996; /* Q31 */
     860          54 :                 move32();
     861             :             }
     862             :         }
     863             : 
     864         800 :         sts[1]->last_extl = -1;
     865         800 :         move16();
     866             : 
     867             :         /* no secondary channel in the previous frame -> memory resets */
     868         800 :         set16_fx( sts[1]->old_inp_12k8_fx, 0, L_INP_MEM );
     869         800 :         set16_fx( sts[1]->mem_decim_fx, 0, 2 * L_FILT_MAX );
     870             : 
     871         800 :         sts[1]->mem_preemph_fx = 0;
     872         800 :         move16();
     873             : 
     874         800 :         set16_fx( sts[1]->buf_speech_enc, 0, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
     875         800 :         sts[1]->exp_buf_speech_enc = 0;
     876         800 :         move16();
     877         800 :         set16_fx( sts[1]->buf_speech_enc_pe, 0, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
     878         800 :         sts[1]->exp_buf_speech_enc_pe = 0;
     879         800 :         move16();
     880         800 :         IF( sts[1]->hTcxEnc != NULL )
     881             :         {
     882         746 :             set16_fx( sts[1]->hTcxEnc->buf_speech_ltp, 0, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
     883             :         }
     884         800 :         set16_fx( sts[1]->buf_wspeech_enc, 0, L_FRAME16k + L_SUBFR + L_FRAME16k + L_NEXT_MAX_16k );
     885         800 :         set16_fx( sts[1]->buf_synth, 0, OLD_SYNTH_SIZE_ENC + L_FRAME32k );
     886         800 :         sts[1]->mem_wsp_fx = 0;
     887         800 :         move16();
     888         800 :         sts[1]->mem_wsp_q = 0;
     889         800 :         move16();
     890         800 :         sts[1]->mem_wsp_enc = 0;
     891         800 :         move16();
     892         800 :         init_gp_clip_fx( sts[1]->clip_var_fx );
     893             : 
     894         800 :         set32_fx( sts[1]->Bin_E_fx, 0, L_FFT );
     895         800 :         sts[1]->q_Bin_E = Q31;
     896         800 :         move16();
     897         800 :         set32_fx( sts[1]->Bin_E_old_fx, 0, L_FFT / 2 );
     898         800 :         sts[1]->q_Bin_E_old = Q31;
     899         800 :         move16();
     900             :         /* sts[1]->hLPDmem reset already done in allocation of handles */
     901             : 
     902         800 :         sts[1]->last_L_frame = sts[0]->last_L_frame;
     903         800 :         move16();
     904         800 :         pitch_ol_init_fx( &sts[1]->old_thres_fx, &sts[1]->old_pitch, &sts[1]->delta_pit, &sts[1]->old_corr_fx );
     905             : 
     906         800 :         set16_fx( sts[1]->old_wsp_fx, 0, L_WSP_MEM );
     907         800 :         set16_fx( sts[1]->old_wsp2_fx, 0, ( L_WSP_MEM - L_INTERPOL ) / OPL_DECIM );
     908         800 :         set16_fx( sts[1]->mem_decim2_fx, 0, 3 );
     909         800 :         Copy( sts[0]->pitch, sts[1]->pitch, 3 );
     910             : 
     911         800 :         sts[1]->Nb_ACELP_frames = 0;
     912         800 :         move16();
     913             : 
     914             :         /* populate PCh memories into the SCh */
     915         800 :         IF( sts[0]->hLPDmem != NULL )
     916             :         {
     917          54 :             Copy( sts[0]->hLPDmem->old_exc, sts[1]->hLPDmem->old_exc, L_EXC_MEM );
     918             :         }
     919         800 :         Copy( sts[0]->lsf_old_fx, sts[1]->lsf_old_fx, M );   /* Qlog2(2.56) */
     920         800 :         Copy( sts[0]->lsp_old_fx, sts[1]->lsp_old_fx, M );   /* Q15 */
     921         800 :         Copy( sts[0]->lsf_old1_fx, sts[1]->lsf_old1_fx, M ); /* Qlog2(2.56) */
     922         800 :         Copy( sts[0]->lsp_old1_fx, sts[1]->lsp_old1_fx, M ); /* Q15 */
     923             : 
     924         800 :         sts[1]->GSC_noisy_speech = 0;
     925         800 :         move16();
     926         800 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
     927             :         {
     928             :             /* cross-fade overlap region of DFT Stereo downmix and original stereo channels */
     929         746 :             tmp_fx = div_s( 64, shl( sts[0]->encoderLookahead_FB, Q6 ) ); /* Q15 */
     930      298246 :             FOR( i = 0; i < sts[0]->encoderLookahead_FB; i++ )
     931             :             {
     932      297500 :                 sts[1]->input32_fx[-sts[0]->encoderLookahead_FB + i] = L_shr( Mpy_32_16_1( L_add( Mpy_32_16_1( sts[0]->input32_fx[-sts[0]->encoderLookahead_FB + i], sts[0]->encoderLookahead_FB - i ), Mpy_32_16_1( sts[1]->input32_fx[-sts[0]->encoderLookahead_FB + i], i ) ), tmp_fx ), 13 ); /* q_inp32+0-15+15-15-13 */
     933      297500 :                 move32();
     934      297500 :                 sts[0]->input32_fx[-sts[0]->encoderLookahead_FB + i] = L_shr( Mpy_32_16_1( L_add( Mpy_32_16_1( sts[0]->input32_fx[-sts[0]->encoderLookahead_FB + i], sts[0]->encoderLookahead_FB - i ), Mpy_32_16_1( sts[1]->input32_fx[-2 * sts[0]->encoderLookahead_FB + i], i ) ), tmp_fx ), 13 ); /* q_inp32+0-15+15-15-13 */
     935      297500 :                 move32();
     936             :             }
     937             :             /* restore continuous signal in right channel (part of old_output was used to store original left channel) */
     938         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 */
     939             : 
     940         746 :             sts[1]->last_core = sts[0]->last_core; /* Q0 */
     941         746 :             move16();
     942         746 :             sts[1]->last_coder_type = sts[0]->last_coder_type; /* Q0 */
     943         746 :             move16();
     944         746 :             sts[1]->last_bwidth = sts[0]->last_bwidth; /* Q0 */
     945         746 :             move16();
     946             :         }
     947             :     }
     948      409455 :     ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
     949             :     {
     950           8 :         set16_fx( sts[0]->hLPDmem->old_exc, 0, L_EXC_MEM );
     951           8 :         set16_fx( sts[1]->hLPDmem->old_exc, 0, L_EXC_MEM );
     952             :     }
     953      410255 :     test();
     954             : 
     955             :     /* TD/DFT -> MDCT stereo switching (there is no TCX in the TD stereo secondary channel, or DFT stereo) */
     956      410255 :     test();
     957      410255 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && NE_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
     958             :     {
     959         749 :         sts[1]->hTcxCfg->last_aldo = sts[0]->hTcxCfg->last_aldo; /* Q0 */
     960         749 :         move16();
     961         749 :         sts[1]->hTcxCfg->tcx_curr_overlap_mode = sts[0]->hTcxCfg->tcx_curr_overlap_mode;
     962         749 :         move16();
     963             :     }
     964             : 
     965      410255 :     return;
     966             : }

Generated by: LCOV version 1.14