LCOV - code coverage report
Current view: top level - lib_dec - ivas_stereo_switching_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main @ fec202a8f89be4a2f278a9fc377bfb58b58fab11 Lines: 662 715 92.6 %
Date: 2025-09-11 08:49:05 Functions: 12 12 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <stdint.h>
      34             : #include "options.h"
      35             : #ifdef DEBUGGING
      36             : #include "debug.h"
      37             : #endif
      38             : #include "cnst.h"
      39             : #include "rom_com.h"
      40             : #include "prot.h"
      41             : #include "ivas_prot.h"
      42             : #include "ivas_rom_com.h"
      43             : #include "assert.h"
      44             : #include "wmc_auto.h"
      45             : #include <math.h>
      46             : 
      47             : /*-------------------------------------------------------------------*
      48             :  * Local constants
      49             :  *-------------------------------------------------------------------*/
      50             : 
      51             : #define DFT2TD_CORR_THRESH 0.9f
      52             : 
      53             : 
      54             : /*-------------------------------------------------------------------*
      55             :  * Function allocate_CoreCoder_TCX()
      56             :  *
      57             :  * Allocate CoreCoder TCX modules
      58             :  *-------------------------------------------------------------------*/
      59             : 
      60       19893 : static ivas_error allocate_CoreCoder_TCX(
      61             :     DEC_CORE_HANDLE st /* i/o: Core decoder state structure     */
      62             : )
      63             : {
      64       19893 :     if ( st->hTcxDec == NULL )
      65             :     {
      66       19893 :         if ( ( st->hTcxDec = (TCX_DEC_HANDLE) malloc( sizeof( TCX_DEC_DATA ) ) ) == NULL )
      67             :         {
      68           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for hTcxDec" ) );
      69             :         }
      70             : 
      71       19893 :         reset_tcx_overl_buf( st->hTcxDec );
      72             : 
      73       19893 :         set_zero( st->hTcxDec->syn_OverlFB, L_FRAME48k / 2 );
      74       19893 :         set_zero( st->hTcxDec->old_synth, OLD_SYNTH_INTERNAL_DEC );
      75       19893 :         set_zero( st->hTcxDec->synth_history, L_PROT48k + L_FRAME48k );
      76             :     }
      77             : 
      78       19893 :     if ( st->hTcxCfg == NULL )
      79             :     {
      80       19893 :         if ( ( st->hTcxCfg = (TCX_CONFIG_HANDLE) malloc( sizeof( TCX_config ) ) ) == NULL )
      81             :         {
      82           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for hTcxCfg" ) );
      83             :         }
      84             :     }
      85             : 
      86             :     /* allocated TCX-LTP structure for second channel */
      87       19893 :     if ( st->hTcxLtpDec == NULL )
      88             :     {
      89       19893 :         if ( ( st->hTcxLtpDec = (TCX_LTP_DEC_HANDLE) malloc( sizeof( TCX_LTP_DEC_DATA ) ) ) == NULL )
      90             :         {
      91           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for TCX-LTP handle\n" ) );
      92             :         }
      93             : 
      94       19893 :         tcxltp_dec_init( st->hTcxLtpDec, 0, st->last_codec_mode, st->element_mode, st->pit_max, st->sr_core );
      95             :     }
      96             : 
      97             :     /* allocate HQ structure */
      98       19893 :     if ( st->hHQ_core == NULL )
      99             :     {
     100       19893 :         if ( ( st->hHQ_core = (HQ_DEC_HANDLE) malloc( sizeof( HQ_DEC_DATA ) ) ) == NULL )
     101             :         {
     102           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for HQ core\n" ) );
     103             :         }
     104             : 
     105       19893 :         HQ_core_dec_init( st->hHQ_core );
     106             :     }
     107             : 
     108       19893 :     if ( st->hIGFDec == NULL )
     109             :     {
     110       19893 :         if ( ( st->hIGFDec = (IGF_DEC_INSTANCE_HANDLE) malloc( sizeof( IGFDEC_INSTANCE ) ) ) == NULL )
     111             :         {
     112           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for IGF\n" ) );
     113             :         }
     114             : 
     115       19893 :         st->igf = 0;
     116       19893 :         init_igf_dec( st->hIGFDec );
     117             :     }
     118             : 
     119       19893 :     if ( st->hTonalMDCTConc == NULL )
     120             :     {
     121       19893 :         if ( ( st->hTonalMDCTConc = (TonalMDCTConcealPtr) malloc( sizeof( TonalMDCTConceal_INSTANCE ) ) ) == NULL )
     122             :         {
     123           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for TonalMDCTConcealment\n" ) );
     124             :         }
     125             :     }
     126             : 
     127       19893 :     st->last_con_tcx = 0;
     128       19893 :     st->hTonalMDCTConc->nSamples = 0;
     129             : 
     130       19893 :     return IVAS_ERR_OK;
     131             : }
     132             : 
     133             : 
     134             : /*-------------------------------------------------------------------*
     135             :  * Function allocate_CoreCoder()
     136             :  *
     137             :  * Allocate CoreCoder modules
     138             :  *-------------------------------------------------------------------*/
     139             : 
     140       20718 : static ivas_error allocate_CoreCoder(
     141             :     DEC_CORE_HANDLE st /* i/o: Core decoder state structure     */
     142             : )
     143             : {
     144             :     ivas_error error;
     145             : 
     146       20718 :     error = IVAS_ERR_OK;
     147             : 
     148       20718 :     if ( st->hGSCDec == NULL )
     149             :     {
     150       20718 :         if ( ( st->hGSCDec = (GSC_DEC_HANDLE) malloc( sizeof( GSC_DEC_DATA ) ) ) == NULL )
     151             :         {
     152           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for GSC\n" ) );
     153             :         }
     154             : 
     155       20718 :         GSC_dec_init( st->hGSCDec );
     156             :     }
     157             : 
     158       20718 :     if ( st->hPFstat == NULL )
     159             :     {
     160       20718 :         if ( ( st->hPFstat = (PFSTAT_HANDLE) malloc( sizeof( PFSTAT ) ) ) == NULL )
     161             :         {
     162           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for NB/formant postflter\n" ) );
     163             :         }
     164             : 
     165       20718 :         Init_post_filter( st->hPFstat );
     166       20718 :         st->psf_lp_noise = 0.0f;
     167             :     }
     168             : 
     169       20718 :     if ( st->hMusicPF == NULL )
     170             :     {
     171       20718 :         if ( ( st->hMusicPF = (MUSIC_POSTFILT_HANDLE) malloc( sizeof( MUSIC_POSTFILT_DATA ) ) ) == NULL )
     172             :         {
     173           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LD music postflter\n" ) );
     174             :         }
     175             : 
     176       20718 :         music_postfilt_init( st->hMusicPF );
     177             :     }
     178             : 
     179       20718 :     if ( st->hBPF == NULL )
     180             :     {
     181       20718 :         if ( ( st->hBPF = (BPF_DEC_HANDLE) malloc( sizeof( BPF_DEC_DATA ) ) ) == NULL )
     182             :         {
     183           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for BPF\n" ) );
     184             :         }
     185             : 
     186       20718 :         bass_psfilter_init( st->hBPF );
     187             :     }
     188             : 
     189       20718 :     if ( st->hBWE_zero == NULL )
     190             :     {
     191       20718 :         if ( ( st->hBWE_zero = (ZERO_BWE_DEC_HANDLE) malloc( sizeof( ZERO_BWE_DEC_DATA ) ) ) == NULL )
     192             :         {
     193           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for zero BWE\n" ) );
     194             :         }
     195             : 
     196       20718 :         hf_synth_init( st->hBWE_zero );
     197             :     }
     198             : 
     199       20718 :     if ( st->cldfbAna == NULL )
     200             :     {
     201             :         /* open analysis for max. sampling rate 48kHz */
     202         684 :         if ( ( error = openCldfb( &st->cldfbAna, CLDFB_ANALYSIS, 48000, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
     203             :         {
     204           0 :             return error;
     205             :         }
     206             :     }
     207             : 
     208       20718 :     if ( st->cldfbBPF == NULL )
     209             :     {
     210             :         /* open analysis BPF for max. internal sampling rate 16kHz */
     211         684 :         if ( ( error = openCldfb( &st->cldfbBPF, CLDFB_ANALYSIS, 16000, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
     212             :         {
     213           0 :             return error;
     214             :         }
     215             :     }
     216             : 
     217       20718 :     return error;
     218             : }
     219             : 
     220             : 
     221             : /*-------------------------------------------------------------------*
     222             :  * Function deallocate_CoreCoder_TCX()
     223             :  *
     224             :  * Deallocate CoreCoder TCX modules
     225             :  *-------------------------------------------------------------------*/
     226             : 
     227       21136 : static void deallocate_CoreCoder_TCX(
     228             :     DEC_CORE_HANDLE st /* i/o: Core decoder state structure     */
     229             : )
     230             : {
     231       21136 :     if ( st->hTcxDec != NULL )
     232             :     {
     233       19865 :         free( st->hTcxDec );
     234       19865 :         st->hTcxDec = NULL;
     235             :     }
     236             : 
     237       21136 :     if ( st->hTcxCfg != NULL )
     238             :     {
     239       19865 :         free( st->hTcxCfg );
     240       19865 :         st->hTcxCfg = NULL;
     241             :     }
     242             : 
     243       21136 :     if ( st->hIGFDec != NULL )
     244             :     {
     245       19865 :         free( st->hIGFDec );
     246       19865 :         st->hIGFDec = NULL;
     247             :     }
     248             : 
     249       21136 :     if ( st->hTonalMDCTConc != NULL )
     250             :     {
     251       19865 :         free( st->hTonalMDCTConc );
     252       19865 :         st->hTonalMDCTConc = NULL;
     253             :     }
     254             : 
     255       21136 :     return;
     256             : }
     257             : 
     258             : 
     259             : /*-------------------------------------------------------------------*
     260             :  * Function deallocate_CoreCoder()
     261             :  *
     262             :  * Deallocate CoreCoder modules
     263             :  *-------------------------------------------------------------------*/
     264             : 
     265       60069 : static void deallocate_CoreCoder(
     266             :     DEC_CORE_HANDLE st /* i/o: Core decoder state structure     */
     267             : )
     268             : {
     269       60069 :     if ( st->hGSCDec != NULL )
     270             :     {
     271       20738 :         free( st->hGSCDec );
     272       20738 :         st->hGSCDec = NULL;
     273             :     }
     274             : 
     275       60069 :     if ( st->hPFstat != NULL )
     276             :     {
     277       20738 :         free( st->hPFstat );
     278       20738 :         st->hPFstat = NULL;
     279             :     }
     280             : 
     281       60069 :     if ( st->hMusicPF != NULL )
     282             :     {
     283       20738 :         free( st->hMusicPF );
     284       20738 :         st->hMusicPF = NULL;
     285             :     }
     286             : 
     287       60069 :     if ( st->hBPF != NULL )
     288             :     {
     289       20738 :         free( st->hBPF );
     290       20738 :         st->hBPF = NULL;
     291             :     }
     292             : 
     293       60069 :     if ( st->hBWE_zero != NULL )
     294             :     {
     295       20738 :         free( st->hBWE_zero );
     296       20738 :         st->hBWE_zero = NULL;
     297             :     }
     298             : 
     299             :     /* CLDFB BPF & resampling tools */
     300       60069 :     if ( st->element_mode != IVAS_CPE_MDCT )
     301             :     {
     302       20283 :         deleteCldfb( &st->cldfbAna ); /* delete analysis at max. sampling rate 48kHz */
     303       20283 :         deleteCldfb( &st->cldfbBPF ); /* delete analysis BPF at max. internal sampling rate 16kHz */
     304             :     }
     305             : 
     306       60069 :     if ( st->element_mode != IVAS_CPE_MDCT )
     307             :     {
     308       20283 :         deallocate_CoreCoder_TCX( st );
     309             :     }
     310             : 
     311       60069 :     return;
     312             : }
     313             : 
     314             : 
     315             : /*-------------------------------------------------------------------*
     316             :  * Function cpy_tcx_ltp_data()
     317             :  *
     318             :  * In case of MDCT<->DFT Stereo switching, copy TCX-LTP data of the right
     319             :  * channel to the correct structure in the new mode
     320             :  *-------------------------------------------------------------------*/
     321             : 
     322       59010 : static void cpy_tcx_ltp_data(
     323             :     TCX_LTP_DEC_HANDLE hTcxLtpDecOld, /* i  : TCX-LTP structure to copy from */
     324             :     TCX_LTP_DEC_HANDLE hTcxLtpDecNew, /* o  : TCX-LTP structure to copy to   */
     325             :     const int32_t output_Fs           /* i  : output sampling rate           */
     326             : )
     327             : {
     328       59010 :     hTcxLtpDecNew->tcxltp_pitch_int_post_prev = hTcxLtpDecOld->tcxltp_pitch_int_post_prev;
     329       59010 :     hTcxLtpDecNew->tcxltp_pitch_fr_post_prev = hTcxLtpDecOld->tcxltp_pitch_fr_post_prev;
     330       59010 :     hTcxLtpDecNew->tcxltp_gain_post_prev = hTcxLtpDecOld->tcxltp_gain_post_prev;
     331       59010 :     hTcxLtpDecNew->tcxltp_filt_idx_prev = hTcxLtpDecOld->tcxltp_filt_idx_prev;
     332             : 
     333       59010 :     mvr2r( hTcxLtpDecOld->tcxltp_mem_in, hTcxLtpDecNew->tcxltp_mem_in, (int16_t) ( ( TCXLTP_MAX_DELAY * output_Fs ) / 48000 ) );
     334       59010 :     mvr2r( hTcxLtpDecOld->tcxltp_mem_out, hTcxLtpDecNew->tcxltp_mem_out, (int16_t) ( ( L_FRAME48k * output_Fs ) / 48000 ) );
     335             : 
     336       59010 :     return;
     337             : }
     338             : 
     339             : 
     340             : /*-------------------------------------------------------------------*
     341             :  * Function stereo_memory_dec()
     342             :  *
     343             :  * Dynamically allocate/deallocate data structures depending on the actual CPE mode
     344             :  *-------------------------------------------------------------------*/
     345             : 
     346    14055321 : ivas_error stereo_memory_dec(
     347             :     const int32_t ivas_total_brate, /* i  : IVAS total bitrate          */
     348             :     CPE_DEC_HANDLE hCPE,            /* i  : CPE decoder structure       */
     349             :     const int16_t nb_bits_metadata, /* i  : number of metadata bits     */
     350             :     const int32_t output_Fs,        /* i  : output sampling rate        */
     351             :     const IVAS_FORMAT ivas_format,  /* i  : IVAS format                 */
     352             :     const MC_MODE mc_mode,          /* i  : MC mode                     */
     353             :     const int16_t nchan_transport   /* i  : number of transport channels*/
     354             : )
     355             : {
     356             :     DEC_CORE_HANDLE st;
     357             :     int16_t i, n, delay_comp_DFT;
     358             :     ivas_error error;
     359             : 
     360    14055321 :     error = IVAS_ERR_OK;
     361             : 
     362    14055321 :     assert( hCPE->last_element_mode >= IVAS_CPE_DFT && "Switching from SCE to CPE is not a valid configuration!" );
     363             : 
     364             : 
     365    14055321 :     hCPE->hCoreCoder[0]->element_mode = hCPE->element_mode;
     366    14055321 :     hCPE->hCoreCoder[1]->element_mode = hCPE->element_mode;
     367             : 
     368             :     /*--------------------------------------------------------------*
     369             :      * stereo switching (using parameters that will be freed)
     370             :      *---------------------------------------------------------------*/
     371             : 
     372             :     /* handling of DFT->TD switching */
     373    14055321 :     if ( hCPE->last_element_mode == IVAS_CPE_DFT && ( hCPE->element_mode == IVAS_CPE_TD || hCPE->element_mode == IVAS_CPE_MDCT ) )
     374             :     {
     375       20319 :         delay_comp_DFT = NS2SA( output_Fs, IVAS_DEC_DELAY_NS - STEREO_DFT32MS_OVL_NS );
     376             : 
     377             :         /* redressing of the DFT OLA part */
     378       60957 :         for ( n = 0; n < CPE_CHANNELS; n++ )
     379             :         {
     380     5259294 :             for ( i = delay_comp_DFT; i < hCPE->hStereoDft->dft32ms_ovl; i++ )
     381             :             {
     382     5218656 :                 hCPE->output_mem[n][i - delay_comp_DFT] /= hCPE->hStereoDft->win32ms[STEREO_DFT32MS_STEP * ( hCPE->hStereoDft->dft32ms_ovl - 1 + delay_comp_DFT - i )];
     383             :             }
     384             :         }
     385             : 
     386       20319 :         if ( hCPE->hCoreCoder[0]->last_core != ACELP_CORE )
     387             :         {
     388        8244 :             mvr2r( hCPE->hStereoDft->buff_LBTCX_mem, hCPE->input_mem_LB[0], NS2SA( hCPE->hCoreCoder[0]->last_L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) );
     389             :         }
     390             :     }
     391             : 
     392    14055321 :     if ( hCPE->last_element_mode == IVAS_CPE_MDCT && hCPE->element_mode == IVAS_CPE_DFT )
     393             :     {
     394       19652 :         v_add( hCPE->hCoreCoder[0]->hHQ_core->old_out, hCPE->hCoreCoder[1]->hHQ_core->old_out, hCPE->hCoreCoder[0]->hHQ_core->old_out, (int16_t) ( output_Fs / FRAMES_PER_SEC ) );
     395       19652 :         v_multc( hCPE->hCoreCoder[0]->hHQ_core->old_out, 0.5f, hCPE->hCoreCoder[0]->hHQ_core->old_out, (int16_t) ( output_Fs / FRAMES_PER_SEC ) );
     396             : 
     397       19652 :         v_add( hCPE->hCoreCoder[0]->hHQ_core->old_outLB, hCPE->hCoreCoder[1]->hHQ_core->old_outLB, hCPE->hCoreCoder[0]->hHQ_core->old_outLB, L_FRAME32k );
     398       19652 :         v_multc( hCPE->hCoreCoder[0]->hHQ_core->old_outLB, 0.5f, hCPE->hCoreCoder[0]->hHQ_core->old_outLB, L_FRAME32k );
     399             :     }
     400             : 
     401             :     /*--------------------------------------------------------------*
     402             :      * allocate/deallocate data structures
     403             :      *---------------------------------------------------------------*/
     404             : 
     405    14055321 :     if ( hCPE->element_mode != hCPE->last_element_mode )
     406             :     {
     407             :         /*--------------------------------------------------------------*
     408             :          * switching CPE mode to DFT stereo
     409             :          *---------------------------------------------------------------*/
     410             : 
     411       41029 :         if ( hCPE->element_mode == IVAS_CPE_DFT )
     412             :         {
     413             :             /* deallocate data structure of the previous CPE mode */
     414       20283 :             if ( hCPE->hStereoTD != NULL )
     415             :             {
     416         631 :                 free( hCPE->hStereoTD );
     417         631 :                 hCPE->hStereoTD = NULL;
     418             :             }
     419             : 
     420       20283 :             if ( hCPE->hStereoMdct != NULL )
     421             :             {
     422       19652 :                 free( hCPE->hStereoMdct );
     423       19652 :                 hCPE->hStereoMdct = NULL;
     424             :             }
     425             : 
     426             :             /* deallocate secondary channel */
     427       20283 :             deallocate_CoreCoder( hCPE->hCoreCoder[1] );
     428             : 
     429             :             /* allocate DFT stereo data structure */
     430       20283 :             if ( ( error = stereo_dft_dec_create( &( hCPE->hStereoDft ), hCPE->element_brate, output_Fs, 0, nchan_transport ) ) != IVAS_ERR_OK )
     431             :             {
     432           0 :                 return error;
     433             :             }
     434             : 
     435       20283 :             if ( hCPE->last_element_mode == IVAS_CPE_MDCT )
     436             :             {
     437       19652 :                 cpy_tcx_ltp_data( hCPE->hCoreCoder[1]->hTcxLtpDec, hCPE->hStereoDft->hTcxLtpDec, output_Fs );
     438       19652 :                 deleteFdCngDec( &hCPE->hCoreCoder[1]->hFdCngDec );
     439             :             }
     440             : 
     441             :             /* memory update - needed in TD stereo, TCX/HQ frame -> DFT stereo, ACELP frame switching */
     442       20283 :             mvr2r( hCPE->input_mem_LB[0], hCPE->hStereoDft->buff_LBTCX_mem, NS2SA( min( hCPE->hCoreCoder[0]->last_L_frame * FRAMES_PER_SEC, 16000 ), STEREO_DFT32MS_OVL_NS ) );
     443             : 
     444             :             /* allocate ICBWE structure */
     445       20283 :             if ( hCPE->hStereoICBWE == NULL )
     446             :             {
     447       19963 :                 if ( ( hCPE->hStereoICBWE = (STEREO_ICBWE_DEC_HANDLE) malloc( sizeof( STEREO_ICBWE_DEC_DATA ) ) ) == NULL )
     448             :                 {
     449           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE \n" ) );
     450             :                 }
     451             : 
     452       19963 :                 stereo_icBWE_init_dec( hCPE->hStereoICBWE );
     453             :             }
     454             : 
     455             :             /* allocate HQ core */
     456       20283 :             st = hCPE->hCoreCoder[0];
     457       20283 :             if ( st->hHQ_core == NULL )
     458             :             {
     459           0 :                 if ( ( st->hHQ_core = (HQ_DEC_HANDLE) malloc( sizeof( HQ_DEC_DATA ) ) ) == NULL )
     460             :                 {
     461           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HQ core\n" ) );
     462             :                 }
     463             : 
     464           0 :                 HQ_core_dec_init( st->hHQ_core );
     465             :             }
     466             : 
     467             :             /* allocate TD CNG handle */
     468       20283 :             if ( st->idchan == 0 && st->hTdCngDec == NULL )
     469             :             {
     470          44 :                 if ( ( st->hTdCngDec = (TD_CNG_DEC_HANDLE) malloc( sizeof( TD_CNG_DEC_DATA ) ) ) == NULL )
     471             :                 {
     472           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DTX/TD CNG\n" ) );
     473             :                 }
     474             : 
     475          44 :                 td_cng_dec_init( st );
     476             :             }
     477             :         }
     478             : 
     479             :         /*--------------------------------------------------------------*
     480             :          * switching CPE mode to TD stereo
     481             :          *---------------------------------------------------------------*/
     482             : 
     483       41029 :         if ( hCPE->element_mode == IVAS_CPE_TD )
     484             :         {
     485             :             /* deallocate data structure of the previous CPE mode */
     486         853 :             if ( hCPE->hStereoDft != NULL )
     487             :             {
     488         640 :                 stereo_dft_dec_destroy( &( hCPE->hStereoDft ) );
     489         640 :                 hCPE->hStereoDft = NULL;
     490             :             }
     491             : 
     492         853 :             if ( hCPE->hStereoMdct != NULL )
     493             :             {
     494         213 :                 free( hCPE->hStereoMdct );
     495         213 :                 hCPE->hStereoMdct = NULL;
     496             :             }
     497             : 
     498             :             /* deallocated TCX/IGF structures for second channel */
     499         853 :             deallocate_CoreCoder_TCX( hCPE->hCoreCoder[1] );
     500             : 
     501         853 :             if ( hCPE->last_element_mode == IVAS_CPE_MDCT )
     502             :             {
     503         213 :                 deleteFdCngDec( &hCPE->hCoreCoder[1]->hFdCngDec );
     504             :             }
     505             : 
     506             :             /* allocate TD stereo data structure */
     507         853 :             if ( hCPE->hStereoTD != NULL )
     508             :             {
     509           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: TD Stereo memory already allocated\n" );
     510             :             }
     511             : 
     512         853 :             if ( ( hCPE->hStereoTD = (STEREO_TD_DEC_DATA_HANDLE) malloc( sizeof( STEREO_TD_DEC_DATA ) ) ) == NULL )
     513             :             {
     514           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD Stereo\n" ) );
     515             :             }
     516             : 
     517         853 :             stereo_td_init_dec( hCPE->hStereoTD, hCPE->last_element_mode );
     518             : 
     519             :             /* allocate CoreCoder secondary channel */
     520         853 :             if ( ( error = allocate_CoreCoder( hCPE->hCoreCoder[1] ) ) != IVAS_ERR_OK )
     521             :             {
     522           0 :                 return error;
     523             :             }
     524             :         }
     525             : 
     526             :         /*--------------------------------------------------------------*
     527             :          * allocate DFT/TD stereo structures after MDCT stereo frame
     528             :          *---------------------------------------------------------------*/
     529             : 
     530       41029 :         if ( hCPE->last_element_mode == IVAS_CPE_MDCT && ( hCPE->element_mode == IVAS_CPE_DFT || hCPE->element_mode == IVAS_CPE_TD ) )
     531             :         {
     532             :             /* deallocated TCX-LTP for second channel */
     533       19865 :             st = hCPE->hCoreCoder[1];
     534       19865 :             if ( st->hTcxLtpDec != 0 )
     535             :             {
     536       19865 :                 free( st->hTcxLtpDec );
     537       19865 :                 st->hTcxLtpDec = NULL;
     538             :             }
     539             : 
     540       19865 :             if ( st->element_mode == IVAS_CPE_TD )
     541             :             {
     542             :                 /* re-use an existing buffer for MDCT->TD stereo switching */
     543             :                 int16_t nZeros, len;
     544         213 :                 nZeros = (int16_t) ( NS2SA( st->output_Fs, N_ZERO_MDCT_NS ) );
     545         213 :                 len = NS2SA( st->output_Fs, 3000000 );
     546         213 :                 mvr2r( st->hHQ_core->old_out + nZeros, hCPE->output_mem[1], len );
     547             :             }
     548             : 
     549             :             /* deallocated HQ-core for second channel */
     550       19865 :             if ( st->hHQ_core != 0 )
     551             :             {
     552       19865 :                 free( st->hHQ_core );
     553       19865 :                 st->hHQ_core = NULL;
     554             :             }
     555             : 
     556             :             /* allocate DFT stereo mono DMX data structure */
     557       19865 :             if ( hCPE->nchan_out == 1 && hCPE->hStereoDftDmx == NULL )
     558             :             {
     559       12041 :                 if ( ( hCPE->hStereoDftDmx = (STEREO_DFT_DMX_DATA_HANDLE) malloc( sizeof( STEREO_DFT_DMX_DATA ) ) ) == NULL )
     560             :                 {
     561           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo DFT mono output\n" ) );
     562             :                 }
     563       12041 :                 stereo_dft_dmx_out_reset( hCPE->hStereoDftDmx );
     564             :             }
     565             : 
     566             :             /* allocate TCA data structure */
     567       19865 :             if ( hCPE->nchan_out != 1 && hCPE->hStereoTCA == NULL )
     568             :             {
     569        7368 :                 if ( ( hCPE->hStereoTCA = (STEREO_TCA_DEC_HANDLE) malloc( sizeof( STEREO_TCA_DEC_DATA ) ) ) == NULL )
     570             :                 {
     571           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
     572             :                 }
     573             : 
     574        7368 :                 stereo_tca_init_dec( hCPE->hStereoTCA );
     575             :             }
     576             : 
     577       19865 :             st = hCPE->hCoreCoder[0];
     578             : 
     579             :             /* allocate primary channel substructures */
     580       19865 :             if ( ( error = allocate_CoreCoder( st ) ) != IVAS_ERR_OK )
     581             :             {
     582           0 :                 return error;
     583             :             }
     584             : 
     585             :             /* allocate BWEs for primary channel */
     586       19865 :             if ( st->hBWE_TD == NULL )
     587             :             {
     588          44 :                 if ( ( st->hBWE_TD = (TD_BWE_DEC_HANDLE) malloc( sizeof( TD_BWE_DEC_DATA ) ) ) == NULL )
     589             :                 {
     590           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD BWE\n" ) );
     591             :                 }
     592             : 
     593          44 :                 td_bwe_dec_init( st->hBWE_TD, -1, st->output_Fs );
     594             : 
     595          44 :                 if ( ( st->hBWE_FD = (FD_BWE_DEC_HANDLE) malloc( sizeof( FD_BWE_DEC_DATA ) ) ) == NULL )
     596             :                 {
     597           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FD BWE\n" ) );
     598             :                 }
     599             : 
     600          44 :                 fd_bwe_dec_init( st->hBWE_FD );
     601             :             }
     602             : 
     603             :             /* Allocated FD_CNG instance for primary channel*/
     604       19865 :             if ( st->hFdCngDec == NULL )
     605             :             {
     606             :                 /* Create FD_CNG instance */
     607           0 :                 if ( ( error = createFdCngDec( &st->hFdCngDec ) ) != IVAS_ERR_OK )
     608             :                 {
     609           0 :                     return error;
     610             :                 }
     611           0 :                 initFdCngDec( st );
     612           0 :                 configureFdCngDec( st->hFdCngDec, st->bwidth, st->total_brate, st->L_frame, st->last_L_frame, st->element_mode );
     613             :             }
     614             : 
     615             :             /* allocate stereo CNG structure */
     616       19865 :             if ( hCPE->hStereoCng == NULL )
     617             :             {
     618       19865 :                 if ( ( hCPE->hStereoCng = (STEREO_CNG_DEC_HANDLE) malloc( sizeof( STEREO_CNG_DEC ) ) ) == NULL )
     619             :                 {
     620           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo CNG\n" ) );
     621             :                 }
     622       19865 :                 stereo_cng_init_dec( hCPE->hStereoCng, &st->hFdCngDec->hFdCngCom->frameSize );
     623             :             }
     624             :         }
     625             : 
     626             :         /*--------------------------------------------------------------*
     627             :          * switching CPE mode to MDCT stereo
     628             :          *---------------------------------------------------------------*/
     629             : 
     630       41029 :         if ( hCPE->element_mode == IVAS_CPE_MDCT )
     631             :         {
     632             :             float tmpF_buff[L_FRAME16k / 2];
     633             :             TCX_LTP_DEC_DATA tcxLtpTmp;
     634             : 
     635       19893 :             if ( hCPE->last_element_mode == IVAS_CPE_TD )
     636             :             {
     637         214 :                 mvr2r( hCPE->hStereoTD->TCX_old_syn_Overl, tmpF_buff, L_FRAME16k / 2 );
     638             :             }
     639             : 
     640       19893 :             if ( hCPE->last_element_mode == IVAS_CPE_DFT )
     641             :             {
     642       19679 :                 cpy_tcx_ltp_data( hCPE->hStereoDft->hTcxLtpDec, &tcxLtpTmp, output_Fs );
     643             :             }
     644             : 
     645             :             /* deallocate data structure of the previous CPE mode */
     646       19893 :             if ( hCPE->hStereoDft != NULL )
     647             :             {
     648       19679 :                 stereo_dft_dec_destroy( &( hCPE->hStereoDft ) );
     649       19679 :                 hCPE->hStereoDft = NULL;
     650             :             }
     651             : 
     652       19893 :             if ( hCPE->hStereoTD != NULL )
     653             :             {
     654         214 :                 free( hCPE->hStereoTD );
     655         214 :                 hCPE->hStereoTD = NULL;
     656             :             }
     657             : 
     658       19893 :             if ( hCPE->hStereoDftDmx != NULL )
     659             :             {
     660       11974 :                 free( hCPE->hStereoDftDmx );
     661       11974 :                 hCPE->hStereoDftDmx = NULL;
     662             :             }
     663             : 
     664       19893 :             if ( hCPE->hStereoICBWE != NULL )
     665             :             {
     666       18776 :                 free( hCPE->hStereoICBWE );
     667       18776 :                 hCPE->hStereoICBWE = NULL;
     668             :             }
     669             : 
     670             :             /* de-allocate stereo CNG structure */
     671       19893 :             if ( hCPE->hStereoCng != NULL )
     672             :             {
     673       19893 :                 free( hCPE->hStereoCng );
     674       19893 :                 hCPE->hStereoCng = NULL;
     675             :             }
     676             : 
     677       59679 :             for ( i = 0; i < CPE_CHANNELS; i++ )
     678             :             {
     679       39786 :                 st = hCPE->hCoreCoder[i];
     680       39786 :                 st->element_mode = hCPE->element_mode;
     681             : 
     682             :                 /* deallocate core-decoder substructures */
     683       39786 :                 deallocate_CoreCoder( st );
     684             : 
     685       39786 :                 st->first_CNG = 0;
     686             :             }
     687             : 
     688             :             /* allocate CLDFB structures for second channel */
     689       19893 :             st = hCPE->hCoreCoder[1];
     690             : 
     691       19893 :             if ( st->cldfbAna == NULL )
     692             :             {
     693       19679 :                 if ( ( error = openCldfb( &st->cldfbAna, CLDFB_ANALYSIS, 48000, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
     694             :                 {
     695           0 :                     return error;
     696             :                 }
     697             :             }
     698             : 
     699       19893 :             if ( st->cldfbBPF == NULL )
     700             :             {
     701             :                 /* open analysis BPF for max. internal sampling rate 16kHz */
     702       19679 :                 if ( ( error = openCldfb( &st->cldfbBPF, CLDFB_ANALYSIS, 16000, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
     703             :                 {
     704           0 :                     return error;
     705             :                 }
     706             :             }
     707             : 
     708             :             /* allocate Fd-Cng structure for second channel */
     709       19893 :             if ( ( error = createFdCngDec( &st->hFdCngDec ) ) != IVAS_ERR_OK )
     710             :             {
     711           0 :                 return error;
     712             :             }
     713             : 
     714             :             /* Init FD-CNG */
     715       19893 :             initFdCngDec( st );
     716             : 
     717       19893 :             if ( hCPE->last_element_mode == IVAS_CPE_DFT )
     718             :             {
     719       19679 :                 st->last_core = ACELP_CORE; /* needed to set-up TCX core in SetTCXModeInfo() */
     720             :             }
     721             : 
     722             :             /*Allocate CoreCoder TCX modules for second channel */
     723       19893 :             if ( ( error = allocate_CoreCoder_TCX( hCPE->hCoreCoder[1] ) ) != IVAS_ERR_OK )
     724             :             {
     725           0 :                 return error;
     726             :             }
     727             : 
     728       19893 :             if ( hCPE->last_element_mode == IVAS_CPE_DFT )
     729             :             {
     730       19679 :                 if ( hCPE->nchan_out == 1 )
     731             :                 {
     732         244 :                     cpy_tcx_ltp_data( hCPE->hCoreCoder[0]->hTcxLtpDec, hCPE->hCoreCoder[1]->hTcxLtpDec, output_Fs );
     733             :                 }
     734             :                 else
     735             :                 {
     736       19435 :                     cpy_tcx_ltp_data( &tcxLtpTmp, hCPE->hCoreCoder[1]->hTcxLtpDec, output_Fs );
     737             :                 }
     738             :             }
     739             : 
     740       19893 :             if ( hCPE->last_element_mode == IVAS_CPE_TD )
     741             :             {
     742         214 :                 mvr2r( tmpF_buff, st->hTcxDec->old_syn_Overl, L_FRAME16k / 2 );
     743             :             }
     744             : 
     745       19893 :             set_f( st->hTcxDec->FBTCXdelayBuf, 0.0f, 111 );
     746       19893 :             st->hTcxDec->old_synthFB = st->hTcxDec->synth_history + NS2SA( st->output_Fs, PH_ECU_MEM_NS );
     747       19893 :             st->hTcxDec->prev_good_synth = st->hTcxDec->old_synthFB + NS2SA( st->output_Fs, PH_ECU_LOOKAHEAD_NS );
     748             : 
     749             :             /* allocate and initialize MDCT stereo structure */
     750       19893 :             if ( ( hCPE->hStereoMdct = (STEREO_MDCT_DEC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_DEC_DATA ) ) ) == NULL )
     751             :             {
     752           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
     753             :             }
     754       19893 :             if ( ivas_format == STEREO_FORMAT && hCPE->element_brate <= MAX_MDCT_ITD_BRATE )
     755             :             {
     756         416 :                 hCPE->hStereoMdct->use_itd = 1;
     757             :             }
     758             :             else
     759             :             {
     760       19477 :                 hCPE->hStereoMdct->use_itd = 0;
     761             :             }
     762       19893 :             hCPE->hStereoMdct->reverse_dmx = 0;
     763       19893 :             hCPE->hStereoMdct->smooth_ratio = 1.f;
     764             :         }
     765             :     }
     766             : 
     767             :     /*--------------------------------------------------------------*
     768             :      * normal TD / LRTD switching
     769             :      *---------------------------------------------------------------*/
     770             : 
     771    14055321 :     if ( hCPE->element_mode == IVAS_CPE_TD )
     772             :     {
     773       39134 :         if ( hCPE->hCoreCoder[0]->bfi == 0 )
     774             :         {
     775       37897 :             st = hCPE->hCoreCoder[1];
     776       37897 :             hCPE->hStereoTD->tdm_LRTD_flag = get_indice_st( hCPE->hCoreCoder[0], hCPE->element_brate + hCPE->brate_surplus, (int16_t) ( ( hCPE->element_brate / FRAMES_PER_SEC ) - nb_bits_metadata + ( hCPE->brate_surplus / FRAMES_PER_SEC ) - TDM_SECONDARY_SIGNALLING - TDM_RATIO_BITS - TDM_LP_REUSE_BITS - TDM_LR_CONTENT_BITS ), TDM_LR_CONTENT_BITS );
     777             : 
     778       37897 :             if ( hCPE->hStereoTD->tdm_LRTD_flag )
     779             :             {
     780             :                 /* deallocate ICBWE structure */
     781       34965 :                 if ( hCPE->hStereoICBWE != NULL )
     782             :                 {
     783         744 :                     free( hCPE->hStereoICBWE );
     784         744 :                     hCPE->hStereoICBWE = NULL;
     785             :                 }
     786             : 
     787             :                 /* allocate BWEs for secondary channel */
     788       34965 :                 if ( st->hBWE_TD == NULL )
     789             :                 {
     790         581 :                     if ( ( st->hBWE_TD = (TD_BWE_DEC_HANDLE) malloc( sizeof( TD_BWE_DEC_DATA ) ) ) == NULL )
     791             :                     {
     792           0 :                         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD BWE\n" ) );
     793             :                     }
     794             : 
     795         581 :                     td_bwe_dec_init( st->hBWE_TD, -1, st->output_Fs );
     796             : 
     797         581 :                     if ( ( st->hBWE_FD = (FD_BWE_DEC_HANDLE) malloc( sizeof( FD_BWE_DEC_DATA ) ) ) == NULL )
     798             :                     {
     799           0 :                         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FD BWE\n" ) );
     800             :                     }
     801             : 
     802         581 :                     fd_bwe_dec_init( st->hBWE_FD );
     803             :                 }
     804             :             }
     805             :             else /* tdm_LRTD_flag  == 0 */
     806             :             {
     807             :                 /* deallocate BWEs for secondary channel */
     808        2932 :                 if ( st->hBWE_TD != NULL )
     809             :                 {
     810         477 :                     if ( st->hBWE_TD != NULL )
     811             :                     {
     812         477 :                         free( st->hBWE_TD );
     813         477 :                         st->hBWE_TD = NULL;
     814             :                     }
     815             : 
     816         477 :                     if ( st->hBWE_FD != NULL )
     817             :                     {
     818         477 :                         free( st->hBWE_FD );
     819         477 :                         st->hBWE_FD = NULL;
     820             :                     }
     821             :                 }
     822             : 
     823             :                 /* allocate ICBWE structure */
     824        2932 :                 if ( hCPE->hStereoICBWE == NULL )
     825             :                 {
     826         477 :                     if ( ( hCPE->hStereoICBWE = (STEREO_ICBWE_DEC_HANDLE) malloc( sizeof( STEREO_ICBWE_DEC_DATA ) ) ) == NULL )
     827             :                     {
     828           0 :                         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE \n" ) );
     829             :                     }
     830             : 
     831         477 :                     stereo_icBWE_init_dec( hCPE->hStereoICBWE );
     832             :                 }
     833             :             }
     834             :         }
     835             :     }
     836             : 
     837             :     /*--------------------------------------------------------------*
     838             :      * MDCT stereo bitrate switching
     839             :      *---------------------------------------------------------------*/
     840             : 
     841    14055321 :     if ( ivas_format == STEREO_FORMAT && hCPE->element_mode == IVAS_CPE_MDCT )
     842             :     {
     843      319524 :         if ( hCPE->element_brate <= MAX_MDCT_ITD_BRATE && ivas_total_brate > IVAS_SID_5k2 )
     844             :         {
     845      185131 :             if ( hCPE->hStereoMdct->use_itd == 0 )
     846             :             {
     847         458 :                 if ( hCPE->hStereoTCA == NULL )
     848             :                 {
     849             :                     /* allocate TCA data structure */
     850         223 :                     if ( ( hCPE->hStereoTCA = (STEREO_TCA_DEC_HANDLE) malloc( sizeof( STEREO_TCA_DEC_DATA ) ) ) == NULL )
     851             :                     {
     852           0 :                         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
     853             :                     }
     854             :                 }
     855         458 :                 stereo_tca_init_dec( hCPE->hStereoTCA );
     856             :             }
     857             : 
     858      185131 :             hCPE->hStereoMdct->use_itd = 1;
     859             :         }
     860             :         else
     861             :         {
     862             :             /* de-allocate TCA data structure */
     863      134393 :             if ( hCPE->hStereoMdct->use_itd == 1 && ivas_total_brate > IVAS_SID_5k2 && hCPE->hStereoTCA != NULL )
     864             :             {
     865         376 :                 free( hCPE->hStereoTCA );
     866         376 :                 hCPE->hStereoTCA = NULL;
     867         376 :                 hCPE->hStereoMdct->use_itd = 0;
     868             :             }
     869      134017 :             else if ( hCPE->hStereoMdct->use_itd == 1 && ivas_total_brate <= IVAS_SID_5k2 )
     870             :             {
     871       17034 :                 hCPE->hStereoMdct->itd = 0.0f;
     872             :             }
     873             :             else
     874             :             {
     875      116983 :                 hCPE->hStereoMdct->use_itd = 0;
     876             :             }
     877             :         }
     878             :     }
     879             : 
     880             :     /*--------------------------------------------------------------*
     881             :      * Bitrate switching in MASA format
     882             :      *---------------------------------------------------------------*/
     883             : 
     884    14055321 :     if ( ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT ) && nchan_transport == 2 )
     885             :     {
     886     1338935 :         if ( hCPE->nchan_out == 1 )
     887             :         {
     888      286427 :             if ( hCPE->hStereoDftDmx == NULL )
     889             :             {
     890        3644 :                 if ( ( hCPE->hStereoDftDmx = (STEREO_DFT_DMX_DATA_HANDLE) malloc( sizeof( STEREO_DFT_DMX_DATA ) ) ) == NULL )
     891             :                 {
     892           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo DFT mono output\n" ) );
     893             :                 }
     894        3644 :                 stereo_dft_dmx_out_reset( hCPE->hStereoDftDmx );
     895             :             }
     896             : 
     897      286427 :             if ( hCPE->prev_synth_chs[1] != NULL )
     898             :             {
     899       15432 :                 free( hCPE->prev_synth_chs[1] );
     900       15432 :                 hCPE->prev_synth_chs[1] = NULL;
     901             :             }
     902             : 
     903      286427 :             if ( hCPE->hStereoTCA != NULL )
     904             :             {
     905        3644 :                 free( hCPE->hStereoTCA );
     906        3644 :                 hCPE->hStereoTCA = NULL;
     907             :             }
     908             :         }
     909             :         else /* nchan_out == 2 */
     910             :         {
     911     1052508 :             if ( hCPE->hStereoDftDmx != NULL )
     912             :             {
     913        3749 :                 free( hCPE->hStereoDftDmx );
     914        3749 :                 hCPE->hStereoDftDmx = NULL;
     915             :             }
     916             : 
     917     1052508 :             if ( hCPE->prev_synth_chs[1] == NULL )
     918             :             {
     919       15470 :                 st = hCPE->hCoreCoder[1];
     920       15470 :                 if ( ( hCPE->prev_synth_chs[1] = (float *) malloc( sizeof( float ) * NS2SA( st->output_Fs, FRAME_SIZE_NS ) ) ) == NULL )
     921             :                 {
     922           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
     923             :                 }
     924       15470 :                 set_zero( hCPE->prev_synth_chs[1], NS2SA( st->output_Fs, FRAME_SIZE_NS ) );
     925             :             }
     926             : 
     927     1052508 :             if ( hCPE->hStereoICBWE == NULL && hCPE->element_mode == IVAS_CPE_DFT )
     928             :             {
     929          20 :                 if ( ( hCPE->hStereoICBWE = (STEREO_ICBWE_DEC_HANDLE) malloc( sizeof( STEREO_ICBWE_DEC_DATA ) ) ) == NULL )
     930             :                 {
     931           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE \n" ) );
     932             :                 }
     933             : 
     934          20 :                 stereo_icBWE_init_dec( hCPE->hStereoICBWE );
     935             :             }
     936             : 
     937     1052508 :             if ( hCPE->hStereoTCA == NULL && ( hCPE->element_mode == IVAS_CPE_DFT || hCPE->element_mode == IVAS_CPE_TD ) )
     938             :             {
     939        3749 :                 if ( ( hCPE->hStereoTCA = (STEREO_TCA_DEC_HANDLE) malloc( sizeof( STEREO_TCA_DEC_DATA ) ) ) == NULL )
     940             :                 {
     941           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
     942             :                 }
     943             : 
     944        3749 :                 stereo_tca_init_dec( hCPE->hStereoTCA );
     945             :             }
     946             : 
     947     1052508 :             if ( hCPE->element_mode == IVAS_CPE_MDCT )
     948             :             {
     949      783845 :                 if ( hCPE->hStereoTCA != NULL )
     950             :                 {
     951             :                     /* note: in MASA, hCPE->hStereoMdct->itd = 0 */
     952        7293 :                     free( hCPE->hStereoTCA );
     953        7293 :                     hCPE->hStereoTCA = NULL;
     954             :                 }
     955             : 
     956      783845 :                 if ( hCPE->hStereoICBWE != NULL )
     957             :                 {
     958           0 :                     free( hCPE->hStereoICBWE );
     959           0 :                     hCPE->hStereoICBWE = NULL;
     960             :                 }
     961             :             }
     962             :         }
     963             :     }
     964             : 
     965             :     /*--------------------------------------------------------------*
     966             :      * Bitrate switching in MASA format
     967             :      *---------------------------------------------------------------*/
     968             : 
     969    14055321 :     if ( ivas_format == MC_FORMAT && hCPE->element_mode == IVAS_CPE_MDCT )
     970             :     {
     971     2781897 :         if ( mc_mode == MC_MODE_MCT || mc_mode == MC_MODE_PARAMUPMIX )
     972             :         {
     973             :             /* deallocate the FdCNG handle */
     974     7339092 :             for ( i = 0; i < CPE_CHANNELS; ++i )
     975             :             {
     976     4892728 :                 deleteFdCngDec( &hCPE->hCoreCoder[i]->hFdCngDec );
     977             :             }
     978             :         }
     979             :         else
     980             :         {
     981             :             /* allocate the FdCNG handle (for noise estimation for TCX PLC fadeout)*/
     982     1006599 :             for ( i = 0; i < CPE_CHANNELS; ++i )
     983             :             {
     984      671066 :                 if ( hCPE->hCoreCoder[i]->cldfbSyn == NULL ) /* could be NULL when we had the MCT LFE channel */
     985             :                 {
     986           0 :                     if ( ( error = openCldfb( &hCPE->hCoreCoder[i]->cldfbSyn, CLDFB_SYNTHESIS, hCPE->hCoreCoder[i]->output_Fs, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
     987             :                     {
     988           0 :                         return error;
     989             :                     }
     990             :                 }
     991             : 
     992      671066 :                 if ( hCPE->hCoreCoder[i]->hFdCngDec == NULL )
     993             :                 {
     994        6084 :                     if ( ( error = createFdCngDec( &hCPE->hCoreCoder[i]->hFdCngDec ) ) != IVAS_ERR_OK )
     995             :                     {
     996           0 :                         return error;
     997             :                     }
     998        6084 :                     initFdCngDec( hCPE->hCoreCoder[i] );
     999             :                 }
    1000             :             }
    1001             :         }
    1002             :     }
    1003             : 
    1004    14055321 :     return error;
    1005             : }
    1006             : 
    1007             : 
    1008             : /*-------------------------------------------------------------------*
    1009             :  * Function synchro_synthesis()
    1010             :  *
    1011             :  * Synchronize upmixed DFT/TD/MDCT stereo synthesis to match the overall delay of 32ms
    1012             :  * Handling of TD stereo <-> DFT stereo transitions
    1013             :  *-------------------------------------------------------------------*/
    1014             : 
    1015    14060960 : void synchro_synthesis(
    1016             :     const int32_t ivas_total_brate,     /* i  : IVAS total bitrate                  */
    1017             :     CPE_DEC_HANDLE hCPE,                /* i/o: CPE decoder structure               */
    1018             :     float *output[CPE_CHANNELS],        /* i/o: output synthesis signal             */
    1019             :     const int16_t output_frame,         /* i  : Number of samples                   */
    1020             :     const int16_t sba_dirac_stereo_flag /* i  : signal stereo output for SBA DirAC  */
    1021             : )
    1022             : {
    1023             :     int16_t n, delay_comp_TD, delay_comp_DFT;
    1024             :     int32_t output_Fs;
    1025             :     Decoder_State **sts;
    1026             :     int16_t i, delay_cldfb, dft32ms_ovl;
    1027             :     float *p_output_mem[CPE_CHANNELS];
    1028             :     float tmp_out[CPE_CHANNELS][NS2SA( 48000, DELAY_CLDFB_NS )];
    1029             :     float tmp_out_TD[CPE_CHANNELS][STEREO_DFT32MS_OVL_MAX];
    1030             :     float tmp_out_TD2[CPE_CHANNELS][STEREO_DFT32MS_OVL_MAX];
    1031             :     int16_t use_cldfb_for_last_dft;
    1032             :     int16_t dft_mono_brate_switch;
    1033             :     int16_t delay_diff;
    1034             :     float tmpF;
    1035             :     int16_t nChannels;
    1036             : 
    1037    14060960 :     sts = hCPE->hCoreCoder;
    1038    14060960 :     output_Fs = sts[0]->output_Fs;
    1039             : 
    1040    14060960 :     use_cldfb_for_last_dft = 0;
    1041    14060960 :     if ( ( hCPE->element_mode != IVAS_CPE_DFT && hCPE->nchan_out == 1 && hCPE->last_element_brate <= IVAS_24k4 ) /* note: this is to mimic the DFT stereo condition "hCPE->hStereoDft->hConfig->res_cod_mode == 0" in last frame */
    1042    14050851 :          || ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->nchan_out == 1 && hCPE->hStereoDft->hConfig->res_cod_mode == STEREO_DFT_RES_COD_OFF ) )
    1043             :     {
    1044      430695 :         use_cldfb_for_last_dft = 1;
    1045             :     }
    1046             : 
    1047    14060960 :     dft_mono_brate_switch = 0;
    1048    14060960 :     if ( hCPE->element_mode == IVAS_CPE_DFT && ( hCPE->last_element_mode == IVAS_CPE_DFT || hCPE->last_element_mode == IVAS_CPE_MDCT ) && hCPE->nchan_out == 1 && hCPE->element_brate != hCPE->last_element_brate )
    1049             :     {
    1050       28490 :         if ( hCPE->last_element_brate >= IVAS_32k && hCPE->hStereoDft->hConfig->res_cod_mode == STEREO_DFT_RES_COD_OFF )
    1051             :         {
    1052       14711 :             dft_mono_brate_switch = -1; /* switch from residual coding mode or MDCT Stereo */
    1053             :         }
    1054       13779 :         else if ( hCPE->last_element_brate <= IVAS_24k4 && hCPE->hStereoDft->hConfig->res_cod_mode > STEREO_DFT_RES_COD_OFF )
    1055             :         {
    1056         411 :             dft_mono_brate_switch = 1; /* switch to residual coding mode*/
    1057             :         }
    1058             :     }
    1059             : 
    1060    14060960 :     if ( use_cldfb_for_last_dft )
    1061             :     {
    1062      430695 :         if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->last_element_mode == IVAS_CPE_TD && ( ivas_total_brate > IVAS_SID_5k2 || hCPE->nchan_out == 2 ) )
    1063             :         {
    1064         101 :             stereo_tca_scale_R_channel( hCPE, output[0], output_frame );
    1065             :         }
    1066             :     }
    1067             : 
    1068             :     /* set delays */
    1069    14060960 :     delay_comp_DFT = NS2SA( output_Fs, IVAS_DEC_DELAY_NS - STEREO_DFT32MS_OVL_NS );
    1070    14060960 :     delay_comp_TD = NS2SA( output_Fs, IVAS_DEC_DELAY_NS - DELAY_CLDFB_NS );
    1071    14060960 :     delay_diff = delay_comp_TD - delay_comp_DFT;
    1072             : 
    1073    14060960 :     dft32ms_ovl = (int16_t) ( ( STEREO_DFT32MS_OVL_MAX * output_Fs ) / 48000 );
    1074    14060960 :     delay_cldfb = NS2SA( output_Fs, DELAY_CLDFB_NS );
    1075             : 
    1076             :     /* initialize pointers */
    1077    14060960 :     if ( hCPE->element_mode >= IVAS_CPE_DFT && hCPE->output_mem[0] != NULL )
    1078             :     {
    1079     8036442 :         for ( n = 0; n < CPE_CHANNELS; n++ )
    1080             :         {
    1081     5357628 :             p_output_mem[n] = hCPE->output_mem[n];
    1082             :         }
    1083             :     }
    1084             : 
    1085             :     /*----------------------------------------------------------------*
    1086             :      * DFT stereo synchro
    1087             :      *----------------------------------------------------------------*/
    1088             : 
    1089    14060960 :     if ( hCPE->element_mode == IVAS_CPE_DFT || sba_dirac_stereo_flag )
    1090             :     {
    1091             :         /* handling of bitrate switching from residual (using DFT) to non-residual mode (using CLDFB) for mono output - as in DFT->TD switching */
    1092     1473979 :         if ( dft_mono_brate_switch == -1 )
    1093             :         {
    1094     1198571 :             for ( i = delay_comp_DFT; i < delay_comp_TD; i++ )
    1095             :             {
    1096     1183860 :                 sts[0]->prev_synth_buffer[i] = p_output_mem[0][i - delay_comp_DFT];
    1097             :             }
    1098             : 
    1099      803951 :             for ( i = delay_comp_TD; i < delay_comp_TD + delay_cldfb; i++ )
    1100             :             {
    1101      789240 :                 tmp_out[0][i - delay_comp_TD] = p_output_mem[0][i - delay_comp_DFT];
    1102             :             }
    1103             :         }
    1104             : 
    1105     1473979 :         if ( hCPE->nchan_out == 1 && hCPE->last_element_mode == IVAS_CPE_MDCT )
    1106             :         {
    1107       12039 :             v_add( sts[0]->prev_synth_buffer, sts[1]->prev_synth_buffer, sts[0]->prev_synth_buffer, delay_comp_DFT );
    1108       12039 :             v_multc( sts[0]->prev_synth_buffer, INV_SQRT_2, sts[0]->prev_synth_buffer, delay_comp_DFT );
    1109             :         }
    1110             : 
    1111     1473979 :         if ( use_cldfb_for_last_dft )
    1112             :         {
    1113             :             /* delay CLDFB-based mono output (<= 24.4 kbps) to be aligned with DFT-based mono output (32 kbps), needed to avoid discontinuities with TCX-LTP. */
    1114      420586 :             mvr2r( sts[0]->prev_synth_buffer + delay_comp_DFT, hCPE->hCoreCoder[0]->hTcxDec->FBTCXdelayBuf, delay_diff );
    1115      420586 :             delay_signal( output[0], output_frame, hCPE->hCoreCoder[0]->hTcxDec->FBTCXdelayBuf, delay_diff );
    1116             :         }
    1117             : 
    1118     1473979 :         if ( hCPE->element_mode != IVAS_CPE_MDCT )
    1119             :         {
    1120     1302373 :             ivas_post_proc( NULL, hCPE, 0, output[0], output, output_frame, sba_dirac_stereo_flag );
    1121             :         }
    1122             : 
    1123             :         /* zero padding in order to synchronize the upmixed DFT stereo synthesis with the TD/MDCT stereo synthesis */
    1124     3916969 :         for ( n = 0; n < hCPE->nchan_out; n++ )
    1125             :         {
    1126     2442990 :             if ( sba_dirac_stereo_flag )
    1127             :             {
    1128      570932 :                 delay_signal( output[n], output_frame, hCPE->prev_synth[n], delay_comp_DFT );
    1129             :             }
    1130             :             else
    1131             :             {
    1132     1872058 :                 delay_signal( output[n], output_frame, sts[n]->prev_synth_buffer, delay_comp_DFT );
    1133             :             }
    1134             :         }
    1135             : 
    1136     1473979 :         if ( use_cldfb_for_last_dft )
    1137             :         {
    1138      420586 :             mvr2r( hCPE->hCoreCoder[0]->hTcxDec->FBTCXdelayBuf, sts[0]->prev_synth_buffer + delay_comp_DFT, delay_diff );
    1139             :         }
    1140             : 
    1141             :         /* handling of TD->DFT switching */
    1142     3916969 :         for ( n = 0; n < hCPE->nchan_out; n++ )
    1143             :         {
    1144     2442990 :             if ( ( !use_cldfb_for_last_dft && hCPE->last_element_mode != IVAS_CPE_DFT && !sba_dirac_stereo_flag && dft_mono_brate_switch != -1 ) || dft_mono_brate_switch == 1 )
    1145       16629 :             {
    1146             :                 float *pPrev_synth;
    1147       16629 :                 float inv_fade_len = 1.f / delay_diff;
    1148             : 
    1149             :                 /* cross-fading between TD synchro memory and the DFT output */
    1150       16629 :                 if ( sba_dirac_stereo_flag )
    1151             :                 {
    1152           0 :                     pPrev_synth = hCPE->prev_synth[n];
    1153             :                 }
    1154             :                 else
    1155             :                 {
    1156       16629 :                     pPrev_synth = sts[n]->prev_synth_buffer;
    1157             :                 }
    1158             : 
    1159       16629 :                 if ( hCPE->last_element_mode != IVAS_CPE_MDCT )
    1160             :                 {
    1161       87392 :                     for ( i = delay_comp_DFT; i < delay_comp_TD; i++ )
    1162             :                     {
    1163       86070 :                         output[n][i] = ( pPrev_synth[i] * ( delay_comp_TD - i ) + output[n][i] * ( i - delay_comp_DFT ) ) * inv_fade_len;
    1164             :                     }
    1165             :                 }
    1166             :             }
    1167     2426361 :             else if ( dft_mono_brate_switch == -1 )
    1168             :             {
    1169       14711 :                 float inv_fade_len_1 = 1.0f / (float) delay_diff;
    1170       14711 :                 float inv_fade_len_2 = 1.0f / (float) delay_cldfb;
    1171             : 
    1172     1198571 :                 for ( i = 0; i < delay_diff; i++ )
    1173             :                 {
    1174     1183860 :                     output[0][i + delay_comp_DFT] = ( output[0][i + delay_comp_DFT] * ( delay_diff - i ) + p_output_mem[0][i] * i ) * inv_fade_len_1;
    1175             :                 }
    1176             : 
    1177      803951 :                 for ( i = 0; i < delay_cldfb; i++ )
    1178             :                 {
    1179      789240 :                     output[0][i + delay_comp_TD] = ( tmp_out[0][i] * ( delay_cldfb - i ) + output[0][i + delay_comp_TD] * i ) * inv_fade_len_2;
    1180             :                 }
    1181             :             }
    1182             :         }
    1183             :     }
    1184             : 
    1185             :     /*----------------------------------------------------------------*
    1186             :      * TD/MDCT stereo synchro
    1187             :      *----------------------------------------------------------------*/
    1188             : 
    1189    14060960 :     if ( sba_dirac_stereo_flag )
    1190             :     {
    1191      285466 :         return;
    1192             :     }
    1193             : 
    1194    13775494 :     if ( hCPE->element_mode == IVAS_CPE_TD || hCPE->element_mode == IVAS_CPE_MDCT )
    1195             :     {
    1196             :         /* handling of DFT->TD switching */
    1197    12586981 :         if ( hCPE->last_element_mode == IVAS_CPE_DFT && !use_cldfb_for_last_dft && hCPE->output_mem[0] != NULL )
    1198             :         {
    1199             :             /* use redressed DFT stereo OLA part to reconstruct the TD stereo synchro memory */
    1200       60145 :             for ( n = 0; n < hCPE->nchan_out; n++ )
    1201             :             {
    1202     3256199 :                 for ( i = delay_comp_DFT; i < delay_comp_TD; i++ )
    1203             :                 {
    1204     3216150 :                     sts[n]->prev_synth_buffer[i] = p_output_mem[n][i - delay_comp_DFT];
    1205             :                 }
    1206             : 
    1207     2184149 :                 for ( i = delay_comp_TD; i < delay_comp_TD + delay_cldfb; i++ )
    1208             :                 {
    1209     2144100 :                     tmp_out[n][i - delay_comp_TD] = p_output_mem[n][i - delay_comp_DFT];
    1210             :                 }
    1211             :             }
    1212             :         }
    1213             : 
    1214             :         /* if previous frame had only one channel copy buffers to other channel */
    1215    12586981 :         if ( hCPE->nchan_out == 1 && hCPE->element_mode == IVAS_CPE_MDCT && hCPE->last_element_mode == IVAS_CPE_DFT )
    1216             :         {
    1217         244 :             mvr2r( sts[0]->prev_synth_buffer, sts[1]->prev_synth_buffer, delay_comp_TD );
    1218         244 :             mvr2r( tmp_out[0], tmp_out[1], delay_cldfb );
    1219         244 :             mvr2r( p_output_mem[0], p_output_mem[1], delay_diff );
    1220             :         }
    1221             : 
    1222             :         /*----------------------------------------------------------------*
    1223             :          * update DFT synthesis overlap memory @output_Fs; needed for TD->DFT stereo switching
    1224             :          *----------------------------------------------------------------*/
    1225             : 
    1226             :         /* resample LB synthesis to output_Fs */
    1227    12586981 :         if ( hCPE->element_mode != IVAS_CPE_MDCT && !use_cldfb_for_last_dft )
    1228             :         {
    1229      103877 :             for ( n = 0; n < hCPE->nchan_out; n++ )
    1230             :             {
    1231       67745 :                 if ( sts[n]->core == ACELP_CORE )
    1232             :                 {
    1233       66214 :                     lerp( hCPE->input_mem_LB[n], tmp_out_TD[n], dft32ms_ovl, NS2SA( sts[n]->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) );
    1234             :                 }
    1235             :                 else /* TCX/HQ core */
    1236             :                 {
    1237        1531 :                     lerp( hCPE->input_mem_LB[n], tmp_out_TD[n], dft32ms_ovl, NS2SA( sts[n]->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) );
    1238             : 
    1239             :                     /* use TCX synchro memory (perfect signal is available) */
    1240       75631 :                     for ( i = delay_diff; i < dft32ms_ovl; i++ )
    1241             :                     {
    1242       74100 :                         tmp_out_TD[n][i] = sts[n]->delay_buf_out[i - delay_diff];
    1243             :                     }
    1244             :                 }
    1245             :             }
    1246             : 
    1247       36132 :             if ( hCPE->nchan_out == CPE_CHANNELS )
    1248             :             {
    1249             :                 /* upmix the resampled LB / the TCX synchro memory */
    1250       31613 :                 tdm_upmix_plain( tmp_out_TD2[0], tmp_out_TD2[1], tmp_out_TD[0], tmp_out_TD[1], tdm_ratio_tabl[hCPE->hStereoTD->tdm_last_ratio_idx], tdm_den_ratio_tabl[hCPE->hStereoTD->tdm_last_ratio_idx], 0, dft32ms_ovl, 1 );
    1251             :             }
    1252             :             else
    1253             :             {
    1254        4519 :                 mvr2r( tmp_out_TD[0], tmp_out_TD2[0], dft32ms_ovl );
    1255             :             }
    1256             : 
    1257      103877 :             for ( n = 0; n < hCPE->nchan_out; n++ )
    1258             :             {
    1259       67745 :                 if ( sts[0]->core == ACELP_CORE ) /* ACELP core in primary channel */
    1260             :                 {
    1261       64966 :                     tmpF = 1.0f / (float) delay_diff;
    1262             : 
    1263             :                     /* cross-fading between regular output synthesis and lerp() resampled synthesis in 3.125 - 1.25 ms OLA part */
    1264     4981066 :                     for ( i = 0; i < delay_diff; i++ )
    1265             :                     {
    1266     4916100 :                         p_output_mem[n][i] = ( output[n][output_frame - dft32ms_ovl + delay_cldfb + i] * ( delay_diff - i ) + tmp_out_TD2[n][i] * i ) * tmpF;
    1267             :                     }
    1268             :                 }
    1269             :                 else /* TCX core */
    1270             :                 {
    1271             :                     /* reconstruct the 3.125 - 1.25 ms OLA part */
    1272      207889 :                     for ( i = 0; i < delay_diff; i++ )
    1273             :                     {
    1274      205110 :                         p_output_mem[n][i] = output[n][output_frame - dft32ms_ovl + delay_cldfb + i];
    1275             :                     }
    1276             :                 }
    1277             : 
    1278             :                 /* reconstruct the last 1.25 ms part of OLA window */
    1279     3481885 :                 for ( i = delay_diff; i < dft32ms_ovl; i++ )
    1280             :                 {
    1281     3414140 :                     p_output_mem[n][i] = tmp_out_TD2[n][i];
    1282             :                 }
    1283             :             }
    1284             :         }
    1285             : 
    1286             :         /*----------------------------------------------------------------*
    1287             :          * zero padding TD/MDCT synthesis in order to synchronize
    1288             :          * the upmixed TD/MDCT stereo synthesis with the DFT stereo synthesis
    1289             :          *----------------------------------------------------------------*/
    1290             : 
    1291    12586981 :         if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->stereo_switching_counter == 0 && hCPE->nchan_out == 1 && hCPE->last_element_brate <= IVAS_24k4 )
    1292             :         {
    1293             :             float step;
    1294          67 :             tmpF = 1.0f;
    1295          67 :             step = 0.5f / delay_comp_TD;
    1296             : 
    1297             :             /* for the first switching frame from DFT to TD, downmix memory too */
    1298        3843 :             for ( n = 0; n < delay_comp_TD; n++ )
    1299             :             {
    1300        3776 :                 sts[0]->prev_synth_buffer[n] = ( sts[0]->prev_synth_buffer[n] ) * tmpF;
    1301        3776 :                 tmpF -= step;
    1302             :             }
    1303             :         }
    1304             : 
    1305    12586981 :         if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->nchan_out == 1 && !is_DTXrate( hCPE->element_brate ) && is_DTXrate( hCPE->last_element_brate ) )
    1306             :         {
    1307         243 :             mvr2r( sts[0]->prev_synth_buffer, sts[1]->prev_synth_buffer, delay_comp_TD );
    1308             :         }
    1309             : 
    1310    12586981 :         nChannels = ( hCPE->element_mode == IVAS_CPE_MDCT ) ? 2 : hCPE->nchan_out;
    1311    37753422 :         for ( n = 0; n < nChannels; n++ )
    1312             :         {
    1313    25166441 :             if ( hCPE->element_mode == IVAS_CPE_MDCT )
    1314             :             {
    1315    25095694 :                 mvr2r( sts[n]->prev_synth_buffer + delay_comp_DFT, hCPE->hCoreCoder[n]->hTcxDec->FBTCXdelayBuf, delay_diff );
    1316    25095694 :                 delay_signal( output[n], output_frame, hCPE->hCoreCoder[n]->hTcxDec->FBTCXdelayBuf, delay_diff );
    1317    25095694 :                 ivas_post_proc( NULL, hCPE, n, output[n], output, output_frame, 0 );
    1318    25095694 :                 delay_signal( output[n], output_frame, sts[n]->prev_synth_buffer, delay_comp_DFT );
    1319    25095694 :                 mvr2r( hCPE->hCoreCoder[n]->hTcxDec->FBTCXdelayBuf, sts[n]->prev_synth_buffer + delay_comp_DFT, delay_diff );
    1320             :             }
    1321             :             else
    1322             :             {
    1323       70747 :                 delay_signal( output[n], output_frame, sts[n]->prev_synth_buffer, delay_comp_TD );
    1324             :             }
    1325             :         }
    1326             : 
    1327             :         /* handling of DFT->TD switching */
    1328    12586981 :         if ( hCPE->last_element_mode == IVAS_CPE_DFT && !use_cldfb_for_last_dft )
    1329             :         {
    1330       20096 :             if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->hStereoCng->prev_sid_nodata )
    1331             :             {
    1332          84 :                 for ( n = 0; n < hCPE->nchan_out; n++ )
    1333             :                 {
    1334          56 :                     tmpF = 1.0f / (float) delay_cldfb;
    1335             : 
    1336        2816 :                     for ( i = 0; i < delay_cldfb; i++ )
    1337             :                     {
    1338        2760 :                         tmp_out[n][i] = tmp_out[n][i] * ( delay_cldfb - i ) * tmpF;
    1339             :                     }
    1340             :                 }
    1341             :             }
    1342             : 
    1343             :             /* cross-fading between DFT OLA memory and TD output */
    1344       60233 :             for ( n = 0; n < nChannels; n++ )
    1345             :             {
    1346       40137 :                 if ( hCPE->element_mode == IVAS_CPE_MDCT )
    1347             :                 {
    1348       39046 :                     tmpF = 1.0f / (float) delay_diff;
    1349             : 
    1350     3187546 :                     for ( i = 0; i < delay_diff; i++ )
    1351             :                     {
    1352     3148500 :                         output[n][i + delay_comp_DFT] = ( output[n][i + delay_comp_DFT] * ( delay_diff - i ) + p_output_mem[n][i] * i ) * tmpF;
    1353             :                     }
    1354             :                 }
    1355             : 
    1356       40137 :                 tmpF = 1.0f / (float) delay_cldfb;
    1357             : 
    1358     2189437 :                 for ( i = 0; i < delay_cldfb; i++ )
    1359             :                 {
    1360     2149300 :                     output[n][i + delay_comp_TD] = ( tmp_out[n][i] * ( delay_cldfb - i ) + output[n][i + delay_comp_TD] * i ) * tmpF;
    1361             :                 }
    1362             :             }
    1363             :         }
    1364             :     }
    1365             : 
    1366    13775494 :     return;
    1367             : }
    1368             : 
    1369             : 
    1370             : /*-------------------------------------------------------------------*
    1371             :  * Function stereo_switching_dec()
    1372             :  *
    1373             :  * Handling of memories in case of CPE modes switching
    1374             :  *-------------------------------------------------------------------*/
    1375             : 
    1376    14055321 : void stereo_switching_dec(
    1377             :     CPE_DEC_HANDLE hCPE,           /* i/o: CPE decoder structure       */
    1378             :     const int32_t ivas_total_brate /* i  : IVAS total bitrate          */
    1379             : )
    1380             : {
    1381             :     int16_t i, n;
    1382             :     int16_t dft32ms_ovl;
    1383             :     Decoder_State **sts;
    1384             :     float tmpF;
    1385             :     int16_t delay_comp_TD;
    1386             : 
    1387    14055321 :     sts = hCPE->hCoreCoder;
    1388             : 
    1389    14055321 :     delay_comp_TD = NS2SA( sts[0]->output_Fs, IVAS_DEC_DELAY_NS - DELAY_CLDFB_NS );
    1390             : 
    1391             :     /* prevent CPE mode switching in the first received frame */
    1392    14055321 :     if ( sts[0]->ini_frame == 0 )
    1393             :     {
    1394      140035 :         hCPE->last_element_mode = hCPE->element_mode;
    1395      140035 :         hCPE->stereo_switching_counter = 10;
    1396      140035 :         hCPE->NbFrameMod = 7;
    1397      140035 :         hCPE->lt_es_em = 0.0f;
    1398             :     }
    1399             : 
    1400    14055321 :     if ( hCPE->element_mode == hCPE->last_element_mode )
    1401             :     {
    1402    14014292 :         hCPE->stereo_switching_counter++;
    1403    14014292 :         hCPE->stereo_switching_counter = min( 10, hCPE->stereo_switching_counter );
    1404             :     }
    1405             :     else
    1406             :     {
    1407       41029 :         hCPE->stereo_switching_counter = 0;
    1408             :     }
    1409             : 
    1410    14055321 :     if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->nchan_out == 1 && hCPE->element_brate >= IVAS_32k && hCPE->last_element_brate <= IVAS_24k4 )
    1411             :     {
    1412         411 :         dft32ms_ovl = (int16_t) ( ( STEREO_DFT32MS_OVL_MAX * sts[0]->output_Fs ) / 48000 );
    1413         411 :         set_zero( hCPE->output_mem[0], dft32ms_ovl );
    1414             :     }
    1415             : 
    1416    14055321 :     if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->last_element_mode != IVAS_CPE_DFT && hCPE->hCoreCoder[0]->ini_frame > 0 )
    1417             :     {
    1418             :         /* windowing the OLA memory */
    1419       20283 :         dft32ms_ovl = (int16_t) ( ( STEREO_DFT32MS_OVL_MAX * sts[0]->output_Fs ) / 48000 );
    1420       60849 :         for ( n = 0; n < CPE_CHANNELS; n++ )
    1421             :         {
    1422       40566 :             if ( hCPE->last_element_mode == IVAS_CPE_MDCT )
    1423             :             {
    1424             :                 /* copy memories from previous MDCT Stereo frame to output_mem */
    1425       39304 :                 mvr2r( hCPE->input_mem[n], hCPE->output_mem[n], dft32ms_ovl );
    1426             :             }
    1427             : 
    1428       40566 :             if ( ivas_total_brate > IVAS_SID_5k2 || n == 0 || hCPE->last_element_mode != IVAS_CPE_TD || hCPE->nchan_out == 1 )
    1429             :             {
    1430     5459267 :                 for ( i = 0; i < dft32ms_ovl; i++ )
    1431             :                 {
    1432     5418750 :                     hCPE->output_mem[n][i] *= hCPE->hStereoDft->win32ms[STEREO_DFT32MS_STEP * ( dft32ms_ovl - 1 - i )];
    1433             :                 }
    1434             :             }
    1435             :             else
    1436             :             {
    1437          49 :                 tmpF = 1.0f / hCPE->hStereoTCA->prevTargetGain;
    1438        5899 :                 for ( i = 0; i < dft32ms_ovl; i++ )
    1439             :                 {
    1440        5850 :                     hCPE->output_mem[n][i] *= tmpF * hCPE->hStereoDft->win32ms[STEREO_DFT32MS_STEP * ( dft32ms_ovl - 1 - i )];
    1441             :                 }
    1442        3793 :                 for ( i = 0; i < delay_comp_TD; i++ )
    1443             :                 {
    1444        3744 :                     hCPE->hCoreCoder[1]->prev_synth_buffer[i] *= tmpF;
    1445             :                 }
    1446       13426 :                 for ( i = 0; i < L_DEC_MEM_LEN_ICA; i++ )
    1447             :                 {
    1448       13377 :                     hCPE->hStereoTCA->memChanR[i] *= tmpF;
    1449             :                 }
    1450             :             }
    1451             :         }
    1452             : 
    1453       20283 :         if ( hCPE->last_element_mode == IVAS_CPE_MDCT )
    1454             :         {
    1455             :             /* create passive downmix of MDCT Stereo memories for DFT input memory */
    1456       19652 :             v_add( hCPE->input_mem_LB[0], hCPE->input_mem_LB[1], hCPE->input_mem_LB[0], STEREO_DFT32MS_OVL_16k );
    1457       19652 :             v_multc( hCPE->input_mem_LB[0], 0.5f, hCPE->input_mem_LB[0], STEREO_DFT32MS_OVL_16k );
    1458             : 
    1459       19652 :             v_add( hCPE->input_mem[0], hCPE->input_mem[1], hCPE->input_mem[0], dft32ms_ovl );
    1460       19652 :             v_multc( hCPE->input_mem[0], 0.5f, hCPE->input_mem[0], dft32ms_ovl );
    1461             : 
    1462       19652 :             if ( hCPE->nchan_out == 1 )
    1463             :             {
    1464       12039 :                 v_add( hCPE->output_mem[0], hCPE->output_mem[1], hCPE->output_mem[0], dft32ms_ovl );
    1465       12039 :                 v_multc( hCPE->output_mem[0], INV_SQRT_2, hCPE->output_mem[0], dft32ms_ovl );
    1466             :             }
    1467             :         }
    1468             : 
    1469             :         /* Update the side_gain[] parameters */
    1470       20283 :         if ( hCPE->last_element_mode != IVAS_CPE_MDCT )
    1471             :         {
    1472         631 :             tmpF = 0;
    1473         631 :             if ( hCPE->hStereoTCA != NULL )
    1474             :             {
    1475         440 :                 tmpF = usdequant( hCPE->hStereoTCA->indx_ica_gD, STEREO_TCA_GDMIN, STEREO_TCA_GDSTEP );
    1476             :             }
    1477             : 
    1478         631 :             set_f( hCPE->hStereoDft->side_gain + STEREO_DFT_NBDIV * STEREO_DFT_BAND_MAX, tmpF, STEREO_DFT_BAND_MAX );
    1479             :         }
    1480             : 
    1481             :         /* reset residual coding / ESF (secondary channel) */
    1482       20283 :         set_zero( hCPE->hStereoDft->res_cod_mem, STEREO_DFT_OVL_8k );
    1483       20283 :         set_zero( hCPE->input_mem[1], NS2SA( sts[0]->output_Fs, STEREO_DFT32MS_OVL_NS ) );
    1484             :     }
    1485             : 
    1486    14055321 :     if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->last_element_mode != IVAS_CPE_TD && hCPE->hCoreCoder[0]->ini_frame > 0 )
    1487             :     {
    1488         853 :         hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_MID_IS_PRIM;
    1489         853 :         hCPE->hStereoTD->tdm_last_SM_flag = 0;
    1490         853 :         hCPE->hStereoTD->tdm_prev_last_SM_flag = 0;
    1491             : 
    1492             :         /* First frame after DFT frame AND the content is uncorrelated or xtalk -> the primary channel is forced to left */
    1493         853 :         if ( hCPE->hStereoTD->tdm_LRTD_flag == 1 )
    1494             :         {
    1495         853 :             hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_LEFT_IS_PRIM;
    1496             :         }
    1497             :     }
    1498             : 
    1499             :     /* no secondary channel in the previous frame -> memory resets */
    1500    14055321 :     if ( hCPE->element_mode > IVAS_CPE_DFT && hCPE->last_element_mode == IVAS_CPE_DFT )
    1501             :     {
    1502       20319 :         if ( hCPE->last_element_brate <= IVAS_SID_5k2 && hCPE->nchan_out == 2 )
    1503             :         {
    1504             :             /* reset CLDFB memories */
    1505          42 :             cldfb_reset_memory( sts[0]->cldfbAna );
    1506          42 :             cldfb_reset_memory( sts[0]->cldfbBPF );
    1507          42 :             cldfb_reset_memory( sts[0]->cldfbSyn );
    1508             : 
    1509          42 :             sts[0]->mem_deemph = 0;
    1510             : 
    1511          42 :             sts[0]->tilt_code = 0.0f;
    1512          42 :             sts[0]->gc_threshold = 0.0f;
    1513             : 
    1514          42 :             set_f( sts[0]->mem_syn1, 0, M );
    1515          42 :             set_f( sts[0]->mem_syn2, 0, M );
    1516          42 :             set_f( sts[0]->mem_syn3, 0, M );
    1517          42 :             set_f( sts[0]->mem_syn_r, 0.0f, L_SYN_MEM );
    1518             : 
    1519          42 :             sts[1]->last_L_frame = sts[0]->last_L_frame;
    1520             : 
    1521             :             /* reset PCh memories */
    1522          42 :             set_f( sts[0]->old_exc, 0, L_EXC_MEM_DEC );
    1523          42 :             set_f( sts[0]->lsf_old, 0, M );
    1524          42 :             set_f( sts[0]->lsp_old, 0, M );
    1525             :         }
    1526       20319 :         sts[1]->last_extl = -1;
    1527             : 
    1528       20319 :         sts[1]->prev_bfi = sts[0]->prev_bfi;
    1529             : 
    1530       20319 :         set_f( sts[1]->old_pitch_buf, (float) L_SUBFR, 2 * NB_SUBFR16k );
    1531       20319 :         sts[1]->old_fpitchFB = 2 * (float) L_SUBFR;
    1532             : 
    1533             :         /* reset CLDFB memories */
    1534       20319 :         cldfb_reset_memory( sts[1]->cldfbAna );
    1535       20319 :         cldfb_reset_memory( sts[1]->cldfbBPF );
    1536       20319 :         cldfb_reset_memory( sts[1]->cldfbSyn );
    1537             : 
    1538       20319 :         sts[1]->mem_deemph = 0;
    1539             : 
    1540       20319 :         sts[1]->tilt_code = 0.0f;
    1541       20319 :         sts[1]->gc_threshold = 0.0f;
    1542             : 
    1543       20319 :         set_f( sts[1]->mem_syn1, 0, M );
    1544       20319 :         set_f( sts[1]->mem_syn2, 0, M );
    1545       20319 :         set_f( sts[1]->mem_syn3, 0, M );
    1546       20319 :         set_f( sts[1]->mem_syn_r, 0.0f, L_SYN_MEM );
    1547             : 
    1548       20319 :         sts[1]->last_L_frame = sts[0]->last_L_frame;
    1549             : 
    1550             :         /* populate PCh memories into the SCh */
    1551       20319 :         mvr2r( sts[0]->old_exc, sts[1]->old_exc, L_EXC_MEM_DEC );
    1552       20319 :         mvr2r( sts[0]->lsf_old, sts[1]->lsf_old, M );
    1553       20319 :         mvr2r( sts[0]->lsp_old, sts[1]->lsp_old, M );
    1554             : 
    1555       20319 :         sts[1]->last_core_brate = sts[0]->last_core_brate;
    1556             : 
    1557       20319 :         if ( hCPE->element_mode == IVAS_CPE_MDCT )
    1558             :         {
    1559       19679 :             sts[1]->last_core = sts[0]->last_core;
    1560       19679 :             sts[1]->last_coder_type = sts[0]->last_coder_type;
    1561             : 
    1562       19679 :             mvr2r( sts[0]->hHQ_core->old_out, sts[1]->hHQ_core->old_out, L_FRAME48k );
    1563       19679 :             mvr2r( sts[0]->delay_buf_out, sts[1]->delay_buf_out, HQ_DELTA_MAX * HQ_DELAY_COMP );
    1564       19679 :             mvr2r( sts[0]->hTcxDec->old_syn_Overl, sts[1]->hTcxDec->old_syn_Overl, 256 );
    1565             :         }
    1566             :     }
    1567    14035002 :     else if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->last_element_mode == IVAS_CPE_MDCT )
    1568             :     {
    1569         213 :         set_f( sts[0]->old_exc, 0.0f, L_EXC_MEM_DEC );
    1570         213 :         set_f( sts[1]->old_exc, 0.0f, L_EXC_MEM_DEC );
    1571             :     }
    1572             : 
    1573             :     /* TD/DFT -> MDCT stereo switching (there is no TCX in the TD stereo secondary channel, or DFT stereo) */
    1574    14055321 :     if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->last_element_mode != IVAS_CPE_MDCT )
    1575             :     {
    1576       19893 :         sts[1]->hTcxCfg->last_aldo = sts[0]->hTcxCfg->last_aldo;
    1577       19893 :         sts[1]->hTcxCfg->tcx_curr_overlap_mode = sts[0]->hTcxCfg->tcx_curr_overlap_mode;
    1578       19893 :         sts[1]->fscale = sts[0]->fscale;
    1579       19893 :         sts[1]->hTcxCfg->tcx_mdct_window_length = sts[0]->hTcxCfg->tcx_mdct_window_length;
    1580       19893 :         sts[1]->pit_res_max = sts[0]->pit_res_max;
    1581       19893 :         sts[1]->pit_res_max_past = sts[0]->pit_res_max_past;
    1582       19893 :         sts[1]->hTcxDec->L_frameTCX = sts[0]->hTcxDec->L_frameTCX;
    1583       19893 :         sts[1]->hTcxDec->conceal_eof_gain = sts[0]->hTcxDec->conceal_eof_gain;
    1584             :     }
    1585             : 
    1586    14055321 :     return;
    1587             : }
    1588             : 
    1589             : 
    1590             : /*-------------------------------------------------------------------*
    1591             :  * Function stereo_td2dft_update()
    1592             :  *
    1593             :  * update OLA buffers - needed for switching from TD stereo to DFT stereo
    1594             :  *-------------------------------------------------------------------*/
    1595             : 
    1596    15074067 : void stereo_td2dft_update(
    1597             :     CPE_DEC_HANDLE hCPE,       /* i/o: CPE decoder structure   */
    1598             :     const int16_t n,           /* i  : channel number          */
    1599             :     float output[],            /* i/o: synthesis @internal Fs  */
    1600             :     float synth[],             /* i/o: synthesis @output Fs    */
    1601             :     float hb_synth[],          /* i/o: hb synthesis            */
    1602             :     const int16_t output_frame /* i  : frame length            */
    1603             : )
    1604             : {
    1605             :     int16_t ovl, ovl_TCX, dft32ms_ovl, hq_delay_comp;
    1606             :     int16_t ns, nsLB;
    1607             :     int16_t old_out_len, old_outLB_len;
    1608             :     Decoder_State **sts;
    1609             : 
    1610    15074067 :     if ( hCPE == NULL )
    1611             :     {
    1612     8564959 :         return;
    1613             :     }
    1614             : 
    1615             :     /* initialization */
    1616     6509108 :     sts = hCPE->hCoreCoder;
    1617     6509108 :     ovl = NS2SA( sts[n]->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS );
    1618     6509108 :     dft32ms_ovl = (int16_t) ( ( STEREO_DFT32MS_OVL_MAX * sts[0]->output_Fs ) / 48000 );
    1619     6509108 :     hq_delay_comp = NS2SA( sts[0]->output_Fs, DELAY_CLDFB_NS );
    1620             : 
    1621     6509108 :     if ( hCPE->element_mode >= IVAS_CPE_DFT && hCPE->element_mode != IVAS_CPE_MDCT )
    1622             :     {
    1623      498854 :         if ( sts[n]->core == ACELP_CORE )
    1624             :         {
    1625      396544 :             if ( n == 0 )
    1626             :             {
    1627             :                 /* update DFT analysis overlap memory @internal_fs: core synthesis */
    1628      357410 :                 mvr2r( output + sts[n]->L_frame - ovl, hCPE->input_mem_LB[n], ovl );
    1629             : 
    1630             :                 /* update DFT analysis overlap memory @internal_fs: BPF */
    1631      357410 :                 if ( sts[n]->p_bpf_noise_buf )
    1632             :                 {
    1633      315586 :                     mvr2r( sts[n]->p_bpf_noise_buf + sts[n]->L_frame - ovl, hCPE->input_mem_BPF[n], ovl );
    1634             :                 }
    1635             : 
    1636             :                 /* update DFT analysis overlap memory @output_Fs: BWE */
    1637      357410 :                 if ( sts[n]->extl != -1 || ( sts[n]->bws_cnt > 0 && sts[n]->core == ACELP_CORE ) )
    1638             :                 {
    1639      332628 :                     mvr2r( hb_synth + output_frame - dft32ms_ovl, hCPE->input_mem[n], dft32ms_ovl );
    1640             :                 }
    1641             :             }
    1642             :             else
    1643             :             {
    1644             :                 /* update DFT analysis overlap memory @internal_fs: core synthesis, secondary channel */
    1645       39134 :                 mvr2r( output + sts[n]->L_frame - ovl, hCPE->input_mem_LB[n], ovl );
    1646             :             }
    1647             :         }
    1648             :         else /* TCX core */
    1649             :         {
    1650             :             /* LB-TCX synthesis */
    1651      102310 :             mvr2r( output + sts[n]->L_frame - ovl, hCPE->input_mem_LB[n], ovl );
    1652             : 
    1653             :             /* BPF */
    1654      102310 :             if ( n == 0 && sts[n]->p_bpf_noise_buf )
    1655             :             {
    1656      102292 :                 mvr2r( sts[n]->p_bpf_noise_buf + sts[n]->L_frame - ovl, hCPE->input_mem_BPF[n], ovl );
    1657             :             }
    1658             : 
    1659             :             /* TCX synthesis (it was already delayed in TD stereo in core_switching_post_dec()) */
    1660      102310 :             if ( sts[n]->hTcxDec != NULL )
    1661             :             {
    1662      102310 :                 ovl_TCX = NS2SA( sts[n]->hTcxDec->L_frameTCX * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS );
    1663      102310 :                 mvr2r( synth + sts[n]->hTcxDec->L_frameTCX + hq_delay_comp - ovl_TCX, hCPE->input_mem[n], ovl_TCX - hq_delay_comp );
    1664      102310 :                 mvr2r( sts[n]->delay_buf_out, hCPE->input_mem[n] + ovl_TCX - hq_delay_comp, hq_delay_comp );
    1665             :             }
    1666             :         }
    1667             :     }
    1668     6010254 :     else if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->input_mem[0] != NULL )
    1669             :     {
    1670             : 
    1671             :         /* update DFT stereo OLA memories */
    1672             :         /*set_zero( hCPE->input_mem_LB[n], STEREO_DFT32MS_OVL_16k );*/
    1673     2394004 :         lerp( output + sts[n]->L_frame - ovl, hCPE->input_mem_LB[n], STEREO_DFT32MS_OVL_16k, ovl );
    1674             : 
    1675             : 
    1676             :         /* TCX synthesis (it was already delayed in TD stereo in core_switching_post_dec()) */
    1677     2394004 :         if ( sts[n]->hTcxDec != NULL )
    1678             :         {
    1679     2394004 :             ovl_TCX = NS2SA( sts[n]->hTcxDec->L_frameTCX * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS );
    1680     2394004 :             mvr2r( synth + sts[n]->hTcxDec->L_frameTCX + hq_delay_comp - ovl_TCX, hCPE->input_mem[n], ovl_TCX - hq_delay_comp );
    1681     2394004 :             mvr2r( sts[n]->delay_buf_out, hCPE->input_mem[n] + ovl_TCX - hq_delay_comp, hq_delay_comp );
    1682             :         }
    1683             : 
    1684     2394004 :         if ( n == 1 )
    1685             :         {
    1686     1197002 :             nsLB = NS2SA( sts[n]->L_frame * FRAMES_PER_SEC, N_ZERO_MDCT_NS );
    1687     1197002 :             ns = NS2SA( sts[n]->output_Fs, N_ZERO_MDCT_NS );
    1688     1197002 :             old_outLB_len = (int16_t) ( ( 3 * STEREO_MDCT2DFT_FADE_LEN_48k * sts[0]->L_frame * FRAMES_PER_SEC ) / 48000 );
    1689     1197002 :             old_out_len = (int16_t) ( ( STEREO_MDCT2DFT_FADE_LEN_48k * sts[0]->output_Fs ) / 48000 );
    1690             : 
    1691             :             /* update buffers used for fading when switching to DFT Stereo */
    1692     1197002 :             v_add( sts[0]->hHQ_core->old_outLB + nsLB, sts[1]->hHQ_core->old_outLB + nsLB, hCPE->old_outLB_mdct, old_outLB_len );
    1693     1197002 :             lerp( hCPE->old_outLB_mdct, hCPE->old_outLB_mdct, STEREO_MDCT2DFT_FADE_LEN_48k, old_outLB_len );
    1694     1197002 :             v_multc( hCPE->old_outLB_mdct, 0.5f, hCPE->old_outLB_mdct, STEREO_MDCT2DFT_FADE_LEN_48k );
    1695             : 
    1696     1197002 :             v_add( sts[0]->hHQ_core->old_out + ns, sts[1]->hHQ_core->old_out + ns, hCPE->old_out_mdct, old_out_len );
    1697     1197002 :             v_multc( hCPE->old_out_mdct, 0.5f, hCPE->old_out_mdct, old_out_len );
    1698             :         }
    1699             : 
    1700     2394004 :         if ( n == 0 )
    1701             :         {
    1702     1197002 :             set_zero( hCPE->input_mem_BPF[n], STEREO_DFT32MS_OVL_16k );
    1703             :         }
    1704             :     }
    1705             : 
    1706             :     /* update ovl buffer for possible switching from TD stereo SCh ACELP frame to MDCT stereo TCX frame */
    1707     6509108 :     if ( hCPE->element_mode == IVAS_CPE_TD && n == 1 && sts[n]->hTcxDec == NULL )
    1708             :     {
    1709       39134 :         mvr2r( output + sts[n]->L_frame / 2, hCPE->hStereoTD->TCX_old_syn_Overl, sts[n]->L_frame / 2 );
    1710             :     }
    1711             : 
    1712     6509108 :     return;
    1713             : }
    1714             : 
    1715             : 
    1716             : /*-------------------------------------------------------------------*
    1717             :  * Function stereo_mdct2dft_update()
    1718             :  *
    1719             :  * update OLA buffers - needed for switching from MDCT stereo to DFT stereo
    1720             :  *-------------------------------------------------------------------*/
    1721             : 
    1722       19652 : void stereo_mdct2dft_update(
    1723             :     CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure        */
    1724             :     float output0[],     /* i/o: synthesis @internal Fs, ch0  */
    1725             :     float synth0[]       /* i/o: synthesis @output Fs, ch0    */
    1726             : )
    1727             : {
    1728             :     int16_t i;
    1729             :     int16_t fade_len, fade_len_LB;
    1730             :     float tmpF;
    1731             :     Decoder_State *st;
    1732             : 
    1733       19652 :     if ( hCPE == NULL )
    1734             :     {
    1735           0 :         return;
    1736             :     }
    1737             : 
    1738       19652 :     st = hCPE->hCoreCoder[0];
    1739             : 
    1740       19652 :     fade_len = (int16_t) ( ( STEREO_MDCT2DFT_FADE_LEN_48k * st->output_Fs ) / 48000 );
    1741       19652 :     fade_len_LB = (int16_t) ( 3 * ( STEREO_MDCT2DFT_FADE_LEN_48k * st->L_frame * FRAMES_PER_SEC ) / 48000 );
    1742             : 
    1743       19652 :     tmpF = 1.f / fade_len;
    1744     2133052 :     for ( i = 0; i < fade_len; i++ )
    1745             :     {
    1746     2113400 :         synth0[i] = ( hCPE->old_out_mdct[i] * ( fade_len - i ) + synth0[i] * i ) * tmpF;
    1747             :     }
    1748             : 
    1749       19652 :     tmpF = 1.f / fade_len_LB;
    1750     2084828 :     for ( i = 0; i < fade_len_LB; i++ )
    1751             :     {
    1752     2065176 :         output0[i] = ( hCPE->old_outLB_mdct[i] * ( fade_len_LB - i ) + output0[i] * i ) * tmpF;
    1753             :     }
    1754             : 
    1755       19652 :     return;
    1756             : }
    1757             : 
    1758             : 
    1759       22978 : static float ncross_corr_self(
    1760             :     float *signal,
    1761             :     const int16_t x,
    1762             :     const int16_t y,
    1763             :     const int16_t corr_len,
    1764             :     const int16_t subsampling )
    1765             : {
    1766             :     float c_c;
    1767             :     float energy_xy, energy_x, energy_y;
    1768             :     uint16_t j;
    1769             :     float *signal_a, *signal_b;
    1770             : 
    1771       22978 :     c_c = 0.0f;
    1772       22978 :     energy_x = 0.0f;
    1773       22978 :     energy_y = 0.0f;
    1774       22978 :     signal_a = &signal[x];
    1775       22978 :     signal_b = &signal[y];
    1776             : 
    1777      267514 :     for ( j = 0; j < corr_len; j += subsampling )
    1778             :     {
    1779      244536 :         c_c += ( signal_a[j] * signal_b[j] );
    1780      244536 :         energy_x += ( signal_a[j] ) * ( signal_a[j] );
    1781      244536 :         energy_y += ( signal_b[j] ) * ( signal_b[j] );
    1782             :     }
    1783             : 
    1784       22978 :     energy_xy = sqrtf( energy_x * energy_y );
    1785             : 
    1786       22978 :     if ( energy_xy < 1.0f )
    1787             :     {
    1788         567 :         energy_xy = 1.0f; /* conceal silent frames */
    1789             :     }
    1790       22978 :     c_c = c_c / energy_xy;
    1791             : 
    1792       22978 :     return c_c;
    1793             : }
    1794             : 
    1795             : 
    1796             : /*-------------------------------------------------------------------*
    1797             :  * Function smooth_dft2td_transition()
    1798             :  *
    1799             :  * apply smoothing to avoid discontinuities and energy variation when
    1800             :  * switching from DFT stereo to TD stereo
    1801             :  *-------------------------------------------------------------------*/
    1802             : 
    1803     4279775 : void smooth_dft2td_transition(
    1804             :     CPE_DEC_HANDLE hCPE,         /* i/o: CPE decoder structure        */
    1805             :     float *output[CPE_CHANNELS], /* i/o: synthesis @external Fs       */
    1806             :     const int16_t output_frame   /* i  : output frame lenght          */
    1807             : )
    1808             : {
    1809             :     Decoder_State **sts;
    1810             :     int16_t ipit, i, ipit_max, ipit_min, idiff, ilen, j, ch, lsearch, corr_len, subsampl;
    1811             :     float flen, ftmp_corr, fmaxcorr, fac_fs;
    1812             :     float tmp_out[L_FRAME48k + L_FRAME48k / 2], tmp_out2[L_FRAME48k], *ptO2, *ptO, *pt1, *ptE;
    1813             : 
    1814     4279775 :     if ( hCPE == NULL )
    1815             :     {
    1816           0 :         return;
    1817             :     }
    1818             : 
    1819             :     /* initialization */
    1820     4279775 :     sts = hCPE->hCoreCoder;
    1821             : 
    1822     4279775 :     if ( ( hCPE->element_mode == IVAS_CPE_TD && ( hCPE->last_element_mode == IVAS_CPE_DFT || hCPE->last_element_mode == IVAS_CPE_MDCT ) && ( sts[0]->clas_dec == VOICED_CLAS && sts[0]->coder_type < TRANSITION && sts[0]->coder_type > UNVOICED && sts[0]->last_coder_type > UNVOICED && sts[1]->coder_type > UNVOICED ) ) )
    1823             :     {
    1824             :         /* length of OVA */
    1825         126 :         ilen = output_frame / 2;
    1826             : 
    1827             :         /* correlation length */
    1828         126 :         corr_len = output_frame / 20;
    1829         126 :         subsampl = 4;
    1830             : 
    1831         352 :         for ( ch = 0; ch < hCPE->nchan_out; ch++ )
    1832             :         {
    1833             :             /* core to external sampling frequency ratio */
    1834         226 :             fac_fs = (float) output_frame / sts[ch]->L_frame;
    1835             : 
    1836             :             /* Find minimum and maximum pitch*/
    1837         226 :             ipit_min = minimum( sts[ch]->old_pitch_buf + 4, 4, &flen ) + 4;
    1838         226 :             ipit_max = maximum( sts[ch]->old_pitch_buf + 4, 4, &flen ) + 4;
    1839         226 :             ipit_min = (short) ( sts[ch]->old_pitch_buf[ipit_min] * fac_fs + 0.5f );
    1840         226 :             ipit_max = (short) ( sts[ch]->old_pitch_buf[ipit_max] * fac_fs + 0.5f );
    1841             : 
    1842         226 :             if ( ( ipit_max + corr_len ) > ilen ) /*ensure the search is performed on the available memory*/
    1843             :             {
    1844             : #ifdef DEBUG_MODE_TD
    1845             :                 /*printf( "**********Pitch too long  = %d, skipping this loop at frame %d\n", ipit_max + corr_len, frame );*/
    1846             : #endif
    1847          55 :                 continue;
    1848             :             }
    1849             : 
    1850         171 :             lsearch = ipit_max - ipit_min + corr_len;
    1851         171 :             lsearch = min( lsearch, output_frame / 4 );
    1852             : 
    1853             :             /* ptr init for search of the best correlation in the past frame */
    1854         171 :             ptE = hCPE->prev_synth_chs[ch] + output_frame - ipit_max - corr_len;
    1855             : 
    1856         171 :             idiff = 0;
    1857         171 :             fmaxcorr = -1.0f;
    1858       11660 :             for ( i = 0; i < lsearch; i++ )
    1859             :             {
    1860       11489 :                 ftmp_corr = ncross_corr_self( ptE, i, ipit_max, corr_len, subsampl );
    1861       11489 :                 if ( ftmp_corr > fmaxcorr )
    1862             :                 {
    1863        1870 :                     idiff = i;
    1864             :                 }
    1865       11489 :                 fmaxcorr = max( fmaxcorr, ftmp_corr );
    1866             :             }
    1867             : 
    1868         171 :             ipit = ipit_max - idiff;
    1869             : 
    1870         171 :             ptO = tmp_out + output_frame;
    1871             : 
    1872             :             /* If the correlation is too low, don't use the prediction */
    1873         171 :             if ( fmaxcorr < DFT2TD_CORR_THRESH )
    1874             :             {
    1875          44 :                 mvr2r( &output[ch][0], ptO, ilen );
    1876             :             }
    1877             :             else
    1878             :             {
    1879         127 :                 mvr2r( hCPE->prev_synth_chs[ch], tmp_out, output_frame );
    1880         127 :                 pt1 = tmp_out + output_frame - ipit;
    1881       49727 :                 for ( i = 0; i < ilen; i++ )
    1882             :                 {
    1883       49600 :                     ptO[i] = pt1[i];
    1884             :                 }
    1885             :             }
    1886             : #ifdef DEBUG_MODE_TD
    1887             :             /*printf( "ch %d, ipit_max %d ipit_min %d, ipit %d :::: lsearch %d\tidiff %d\t%.4f At frame : \t%d\n", ch, ipit_max, ipit_min, ipit, lsearch, idiff, fmaxcorr, frame );*/
    1888             : #endif
    1889             : 
    1890             :             /* Set buffer for the reserved buffer of the current frame */
    1891         171 :             ptO2 = tmp_out2 + output_frame - ilen;
    1892         171 :             set_f( tmp_out2 + output_frame - ilen, 0.0f, ilen );
    1893      132971 :             for ( i = 0; i < output_frame; i++ )
    1894             :             {
    1895      132800 :                 tmp_out2[i] = output[ch][output_frame - 1 - i];
    1896             :             }
    1897             :             /* ptr init for search of the best correlation of the current frame */
    1898         171 :             ptE = ptO2 - ( ipit_max + corr_len );
    1899             : 
    1900         171 :             idiff = 0;
    1901         171 :             fmaxcorr = -1.0f;
    1902       11660 :             for ( i = 0; i < lsearch; i++ )
    1903             :             {
    1904       11489 :                 ftmp_corr = ncross_corr_self( ptE, i, ipit_max, corr_len, subsampl );
    1905       11489 :                 if ( ftmp_corr > fmaxcorr )
    1906             :                 {
    1907        1654 :                     idiff = i;
    1908             :                 }
    1909       11489 :                 fmaxcorr = max( fmaxcorr, ftmp_corr );
    1910             :             }
    1911             : 
    1912         171 :             ipit = ipit_max - idiff;
    1913             : 
    1914             :             /* If the correlation is too low, don't use the prediction */
    1915         171 :             if ( fmaxcorr > DFT2TD_CORR_THRESH )
    1916             :             {
    1917         123 :                 pt1 = tmp_out2 + output_frame - ilen - ipit;
    1918       49083 :                 for ( i = 0; i < ilen; i++ )
    1919             :                 {
    1920       48960 :                     ptO2[i] = pt1[i];
    1921             :                 }
    1922             :             }
    1923             : #ifdef DEBUG_MODE_TD
    1924             :             /*printf( "ch %d, ipit_max %d ipit_min %d, ipit %d :::: lsearch %d\tidiff %d\t%.4f At frame : \t%d\n", ch, ipit_max, ipit_min, ipit, lsearch, idiff, fmaxcorr, frame );*/
    1925             : #endif
    1926             : 
    1927             :             /* perform OVA between predicted signals */
    1928         171 :             flen = 1.0f / ilen;
    1929             : 
    1930         855 :             for ( i = 0; i < 4; i++ )
    1931             :             {
    1932         684 :                 output[ch][i] = ( ( ( 4.0f - i ) * output[ch][i] + i * ptO[i] ) * 0.25f * ( ilen - i ) + tmp_out2[output_frame - 1 - i] * i ) * flen;
    1933             :             }
    1934       65203 :             for ( ; i < ilen - 4; i++ )
    1935             :             {
    1936       65032 :                 output[ch][i] = ( ptO[i] * ( ilen - i ) + tmp_out2[output_frame - 1 - i] * i ) * flen;
    1937             :             }
    1938         171 :             j = 0;
    1939         855 :             for ( ; i < ilen; i++ )
    1940             :             {
    1941         684 :                 output[ch][i] = ( ptO[i] * ( ilen - i ) + ( ( 4.0f - j ) * tmp_out2[output_frame - 1 - i] + j * output[ch][i] ) * i * 0.25f ) * flen;
    1942         684 :                 j++;
    1943             :             }
    1944             :         }
    1945             :     }
    1946     4279649 :     else if ( hCPE->element_mode == IVAS_CPE_DFT )
    1947             :     {
    1948             :         /* Updates */
    1949     3060571 :         for ( ch = 0; ch < hCPE->nchan_out; ch++ )
    1950             :         {
    1951     1872058 :             mvr2r( output[ch], hCPE->prev_synth_chs[ch], output_frame );
    1952             :         }
    1953             :     }
    1954             : 
    1955     4279775 :     return;
    1956             : }

Generated by: LCOV version 1.14