LCOV - code coverage report
Current view: top level - lib_dec - ivas_stereo_switching_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- conformance test test_26252.py @ a21f94bc6bac334fe001a5bad2f7b32b79038097 Lines: 660 715 92.3 %
Date: 2025-11-01 05:07:43 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         788 : static ivas_error allocate_CoreCoder_TCX(
      61             :     DEC_CORE_HANDLE st /* i/o: Core decoder state structure     */
      62             : )
      63             : {
      64         788 :     if ( st->hTcxDec == NULL )
      65             :     {
      66         788 :         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         788 :         reset_tcx_overl_buf( st->hTcxDec );
      72             : 
      73         788 :         set_zero( st->hTcxDec->syn_OverlFB, L_FRAME48k / 2 );
      74         788 :         set_zero( st->hTcxDec->old_synth, OLD_SYNTH_INTERNAL_DEC );
      75         788 :         set_zero( st->hTcxDec->synth_history, L_PROT48k + L_FRAME48k );
      76             :     }
      77             : 
      78         788 :     if ( st->hTcxCfg == NULL )
      79             :     {
      80         788 :         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         788 :     if ( st->hTcxLtpDec == NULL )
      88             :     {
      89         788 :         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         788 :         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         788 :     if ( st->hHQ_core == NULL )
      99             :     {
     100         788 :         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         788 :         HQ_core_dec_init( st->hHQ_core );
     106             :     }
     107             : 
     108         788 :     if ( st->hIGFDec == NULL )
     109             :     {
     110         788 :         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         788 :         st->igf = 0;
     116         788 :         init_igf_dec( st->hIGFDec );
     117             :     }
     118             : 
     119         788 :     if ( st->hTonalMDCTConc == NULL )
     120             :     {
     121         788 :         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         788 :     st->last_con_tcx = 0;
     128         788 :     st->hTonalMDCTConc->nSamples = 0;
     129             : 
     130         788 :     return IVAS_ERR_OK;
     131             : }
     132             : 
     133             : 
     134             : /*-------------------------------------------------------------------*
     135             :  * Function allocate_CoreCoder()
     136             :  *
     137             :  * Allocate CoreCoder modules
     138             :  *-------------------------------------------------------------------*/
     139             : 
     140         828 : static ivas_error allocate_CoreCoder(
     141             :     DEC_CORE_HANDLE st /* i/o: Core decoder state structure     */
     142             : )
     143             : {
     144             :     ivas_error error;
     145             : 
     146         828 :     error = IVAS_ERR_OK;
     147             : 
     148         828 :     if ( st->hGSCDec == NULL )
     149             :     {
     150         828 :         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         828 :         GSC_dec_init( st->hGSCDec );
     156             :     }
     157             : 
     158         828 :     if ( st->hPFstat == NULL )
     159             :     {
     160         828 :         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         828 :         Init_post_filter( st->hPFstat );
     166         828 :         st->psf_lp_noise = 0.0f;
     167             :     }
     168             : 
     169         828 :     if ( st->hMusicPF == NULL )
     170             :     {
     171         828 :         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         828 :         music_postfilt_init( st->hMusicPF );
     177             :     }
     178             : 
     179         828 :     if ( st->hBPF == NULL )
     180             :     {
     181         828 :         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         828 :         bass_psfilter_init( st->hBPF );
     187             :     }
     188             : 
     189         828 :     if ( st->hBWE_zero == NULL )
     190             :     {
     191         828 :         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         828 :         hf_synth_init( st->hBWE_zero );
     197             :     }
     198             : 
     199         828 :     if ( st->cldfbAna == NULL )
     200             :     {
     201             :         /* open analysis for max. sampling rate 48kHz */
     202          54 :         if ( ( error = openCldfb( &st->cldfbAna, CLDFB_ANALYSIS, 48000, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
     203             :         {
     204           0 :             return error;
     205             :         }
     206             :     }
     207             : 
     208         828 :     if ( st->cldfbBPF == NULL )
     209             :     {
     210             :         /* open analysis BPF for max. internal sampling rate 16kHz */
     211          54 :         if ( ( error = openCldfb( &st->cldfbBPF, CLDFB_ANALYSIS, 16000, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
     212             :         {
     213           0 :             return error;
     214             :         }
     215             :     }
     216             : 
     217         828 :     return error;
     218             : }
     219             : 
     220             : 
     221             : /*-------------------------------------------------------------------*
     222             :  * Function deallocate_CoreCoder_TCX()
     223             :  *
     224             :  * Deallocate CoreCoder TCX modules
     225             :  *-------------------------------------------------------------------*/
     226             : 
     227         874 : static void deallocate_CoreCoder_TCX(
     228             :     DEC_CORE_HANDLE st /* i/o: Core decoder state structure     */
     229             : )
     230             : {
     231         874 :     if ( st->hTcxDec != NULL )
     232             :     {
     233         768 :         free( st->hTcxDec );
     234         768 :         st->hTcxDec = NULL;
     235             :     }
     236             : 
     237         874 :     if ( st->hTcxCfg != NULL )
     238             :     {
     239         768 :         free( st->hTcxCfg );
     240         768 :         st->hTcxCfg = NULL;
     241             :     }
     242             : 
     243         874 :     if ( st->hIGFDec != NULL )
     244             :     {
     245         768 :         free( st->hIGFDec );
     246         768 :         st->hIGFDec = NULL;
     247             :     }
     248             : 
     249         874 :     if ( st->hTonalMDCTConc != NULL )
     250             :     {
     251         768 :         free( st->hTonalMDCTConc );
     252         768 :         st->hTonalMDCTConc = NULL;
     253             :     }
     254             : 
     255         874 :     return;
     256             : }
     257             : 
     258             : 
     259             : /*-------------------------------------------------------------------*
     260             :  * Function deallocate_CoreCoder()
     261             :  *
     262             :  * Deallocate CoreCoder modules
     263             :  *-------------------------------------------------------------------*/
     264             : 
     265        2390 : static void deallocate_CoreCoder(
     266             :     DEC_CORE_HANDLE st /* i/o: Core decoder state structure     */
     267             : )
     268             : {
     269        2390 :     if ( st->hGSCDec != NULL )
     270             :     {
     271         847 :         free( st->hGSCDec );
     272         847 :         st->hGSCDec = NULL;
     273             :     }
     274             : 
     275        2390 :     if ( st->hPFstat != NULL )
     276             :     {
     277         847 :         free( st->hPFstat );
     278         847 :         st->hPFstat = NULL;
     279             :     }
     280             : 
     281        2390 :     if ( st->hMusicPF != NULL )
     282             :     {
     283         847 :         free( st->hMusicPF );
     284         847 :         st->hMusicPF = NULL;
     285             :     }
     286             : 
     287        2390 :     if ( st->hBPF != NULL )
     288             :     {
     289         847 :         free( st->hBPF );
     290         847 :         st->hBPF = NULL;
     291             :     }
     292             : 
     293        2390 :     if ( st->hBWE_zero != NULL )
     294             :     {
     295         847 :         free( st->hBWE_zero );
     296         847 :         st->hBWE_zero = NULL;
     297             :     }
     298             : 
     299             :     /* CLDFB BPF & resampling tools */
     300        2390 :     if ( st->element_mode != IVAS_CPE_MDCT )
     301             :     {
     302         814 :         deleteCldfb( &st->cldfbAna ); /* delete analysis at max. sampling rate 48kHz */
     303         814 :         deleteCldfb( &st->cldfbBPF ); /* delete analysis BPF at max. internal sampling rate 16kHz */
     304             :     }
     305             : 
     306        2390 :     if ( st->element_mode != IVAS_CPE_MDCT )
     307             :     {
     308         814 :         deallocate_CoreCoder_TCX( st );
     309             :     }
     310             : 
     311        2390 :     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        2327 : 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        2327 :     hTcxLtpDecNew->tcxltp_pitch_int_post_prev = hTcxLtpDecOld->tcxltp_pitch_int_post_prev;
     329        2327 :     hTcxLtpDecNew->tcxltp_pitch_fr_post_prev = hTcxLtpDecOld->tcxltp_pitch_fr_post_prev;
     330        2327 :     hTcxLtpDecNew->tcxltp_gain_post_prev = hTcxLtpDecOld->tcxltp_gain_post_prev;
     331        2327 :     hTcxLtpDecNew->tcxltp_filt_idx_prev = hTcxLtpDecOld->tcxltp_filt_idx_prev;
     332             : 
     333        2327 :     mvr2r( hTcxLtpDecOld->tcxltp_mem_in, hTcxLtpDecNew->tcxltp_mem_in, (int16_t) ( ( TCXLTP_MAX_DELAY * output_Fs ) / 48000 ) );
     334        2327 :     mvr2r( hTcxLtpDecOld->tcxltp_mem_out, hTcxLtpDecNew->tcxltp_mem_out, (int16_t) ( ( L_FRAME48k * output_Fs ) / 48000 ) );
     335             : 
     336        2327 :     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      857207 : 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      857207 :     error = IVAS_ERR_OK;
     361             : 
     362      857207 :     assert( hCPE->last_element_mode >= IVAS_CPE_DFT && "Switching from SCE to CPE is not a valid configuration!" );
     363             : 
     364             : 
     365      857207 :     hCPE->hCoreCoder[0]->element_mode = hCPE->element_mode;
     366      857207 :     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      857207 :     if ( hCPE->last_element_mode == IVAS_CPE_DFT && ( hCPE->element_mode == IVAS_CPE_TD || hCPE->element_mode == IVAS_CPE_MDCT ) )
     374             :     {
     375         835 :         delay_comp_DFT = NS2SA( output_Fs, IVAS_DEC_DELAY_NS - STEREO_DFT32MS_OVL_NS );
     376             : 
     377             :         /* redressing of the DFT OLA part */
     378        2505 :         for ( n = 0; n < CPE_CHANNELS; n++ )
     379             :         {
     380      213926 :             for ( i = delay_comp_DFT; i < hCPE->hStereoDft->dft32ms_ovl; i++ )
     381             :             {
     382      212256 :                 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         835 :         if ( hCPE->hCoreCoder[0]->last_core != ACELP_CORE )
     387             :         {
     388         217 :             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      857207 :     if ( hCPE->last_element_mode == IVAS_CPE_MDCT && hCPE->element_mode == IVAS_CPE_DFT )
     393             :     {
     394         759 :         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         759 :         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         759 :         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         759 :         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      857207 :     if ( hCPE->element_mode != hCPE->last_element_mode )
     406             :     {
     407             :         /*--------------------------------------------------------------*
     408             :          * switching CPE mode to DFT stereo
     409             :          *---------------------------------------------------------------*/
     410             : 
     411        1662 :         if ( hCPE->element_mode == IVAS_CPE_DFT )
     412             :         {
     413             :             /* deallocate data structure of the previous CPE mode */
     414         814 :             if ( hCPE->hStereoTD != NULL )
     415             :             {
     416          55 :                 free( hCPE->hStereoTD );
     417          55 :                 hCPE->hStereoTD = NULL;
     418             :             }
     419             : 
     420         814 :             if ( hCPE->hStereoMdct != NULL )
     421             :             {
     422         759 :                 free( hCPE->hStereoMdct );
     423         759 :                 hCPE->hStereoMdct = NULL;
     424             :             }
     425             : 
     426             :             /* deallocate secondary channel */
     427         814 :             deallocate_CoreCoder( hCPE->hCoreCoder[1] );
     428             : 
     429             :             /* allocate DFT stereo data structure */
     430         814 :             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         814 :             if ( hCPE->last_element_mode == IVAS_CPE_MDCT )
     436             :             {
     437         759 :                 cpy_tcx_ltp_data( hCPE->hCoreCoder[1]->hTcxLtpDec, hCPE->hStereoDft->hTcxLtpDec, output_Fs );
     438         759 :                 deleteFdCngDec( &hCPE->hCoreCoder[1]->hFdCngDec );
     439             :             }
     440             : 
     441             :             /* memory update - needed in TD stereo, TCX/HQ frame -> DFT stereo, ACELP frame switching */
     442         814 :             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         814 :             if ( hCPE->hStereoICBWE == NULL )
     446             :             {
     447         782 :                 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         782 :                 stereo_icBWE_init_dec( hCPE->hStereoICBWE );
     453             :             }
     454             : 
     455             :             /* allocate HQ core */
     456         814 :             st = hCPE->hCoreCoder[0];
     457         814 :             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         814 :             if ( st->idchan == 0 && st->hTdCngDec == NULL )
     469             :             {
     470           3 :                 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           3 :                 td_cng_dec_init( st );
     476             :             }
     477             :         }
     478             : 
     479             :         /*--------------------------------------------------------------*
     480             :          * switching CPE mode to TD stereo
     481             :          *---------------------------------------------------------------*/
     482             : 
     483        1662 :         if ( hCPE->element_mode == IVAS_CPE_TD )
     484             :         {
     485             :             /* deallocate data structure of the previous CPE mode */
     486          60 :             if ( hCPE->hStereoDft != NULL )
     487             :             {
     488          51 :                 stereo_dft_dec_destroy( &( hCPE->hStereoDft ) );
     489          51 :                 hCPE->hStereoDft = NULL;
     490             :             }
     491             : 
     492          60 :             if ( hCPE->hStereoMdct != NULL )
     493             :             {
     494           9 :                 free( hCPE->hStereoMdct );
     495           9 :                 hCPE->hStereoMdct = NULL;
     496             :             }
     497             : 
     498             :             /* deallocated TCX/IGF structures for second channel */
     499          60 :             deallocate_CoreCoder_TCX( hCPE->hCoreCoder[1] );
     500             : 
     501          60 :             if ( hCPE->last_element_mode == IVAS_CPE_MDCT )
     502             :             {
     503           9 :                 deleteFdCngDec( &hCPE->hCoreCoder[1]->hFdCngDec );
     504             :             }
     505             : 
     506             :             /* allocate TD stereo data structure */
     507          60 :             if ( hCPE->hStereoTD != NULL )
     508             :             {
     509           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: TD Stereo memory already allocated\n" );
     510             :             }
     511             : 
     512          60 :             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          60 :             stereo_td_init_dec( hCPE->hStereoTD, hCPE->last_element_mode );
     518             : 
     519             :             /* allocate CoreCoder secondary channel */
     520          60 :             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        1662 :         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         768 :             st = hCPE->hCoreCoder[1];
     534         768 :             if ( st->hTcxLtpDec != 0 )
     535             :             {
     536         768 :                 free( st->hTcxLtpDec );
     537         768 :                 st->hTcxLtpDec = NULL;
     538             :             }
     539             : 
     540         768 :             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           9 :                 nZeros = (int16_t) ( NS2SA( st->output_Fs, N_ZERO_MDCT_NS ) );
     545           9 :                 len = NS2SA( st->output_Fs, 3000000 );
     546           9 :                 mvr2r( st->hHQ_core->old_out + nZeros, hCPE->output_mem[1], len );
     547             :             }
     548             : 
     549             :             /* deallocated HQ-core for second channel */
     550         768 :             if ( st->hHQ_core != 0 )
     551             :             {
     552         768 :                 free( st->hHQ_core );
     553         768 :                 st->hHQ_core = NULL;
     554             :             }
     555             : 
     556             :             /* allocate DFT stereo mono DMX data structure */
     557         768 :             if ( hCPE->nchan_out == 1 && hCPE->hStereoDftDmx == NULL )
     558             :             {
     559         440 :                 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         440 :                 stereo_dft_dmx_out_reset( hCPE->hStereoDftDmx );
     564             :             }
     565             : 
     566             :             /* allocate TCA data structure */
     567         768 :             if ( hCPE->nchan_out != 1 && hCPE->hStereoTCA == NULL )
     568             :             {
     569         295 :                 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         295 :                 stereo_tca_init_dec( hCPE->hStereoTCA );
     575             :             }
     576             : 
     577         768 :             st = hCPE->hCoreCoder[0];
     578             : 
     579             :             /* allocate primary channel substructures */
     580         768 :             if ( ( error = allocate_CoreCoder( st ) ) != IVAS_ERR_OK )
     581             :             {
     582           0 :                 return error;
     583             :             }
     584             : 
     585             :             /* allocate BWEs for primary channel */
     586         768 :             if ( st->hBWE_TD == NULL )
     587             :             {
     588           3 :                 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           3 :                 td_bwe_dec_init( st->hBWE_TD, -1, st->output_Fs );
     594             : 
     595           3 :                 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           3 :                 fd_bwe_dec_init( st->hBWE_FD );
     601             :             }
     602             : 
     603             :             /* Allocated FD_CNG instance for primary channel*/
     604         768 :             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         768 :             if ( hCPE->hStereoCng == NULL )
     617             :             {
     618         768 :                 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         768 :                 stereo_cng_init_dec( hCPE->hStereoCng, &st->hFdCngDec->hFdCngCom->frameSize );
     623             :             }
     624             :         }
     625             : 
     626             :         /*--------------------------------------------------------------*
     627             :          * switching CPE mode to MDCT stereo
     628             :          *---------------------------------------------------------------*/
     629             : 
     630        1662 :         if ( hCPE->element_mode == IVAS_CPE_MDCT )
     631             :         {
     632             :             float tmpF_buff[L_FRAME16k / 2];
     633             :             TCX_LTP_DEC_DATA tcxLtpTmp;
     634             : 
     635         788 :             if ( hCPE->last_element_mode == IVAS_CPE_TD )
     636             :             {
     637           4 :                 mvr2r( hCPE->hStereoTD->TCX_old_syn_Overl, tmpF_buff, L_FRAME16k / 2 );
     638             :             }
     639             : 
     640         788 :             if ( hCPE->last_element_mode == IVAS_CPE_DFT )
     641             :             {
     642         784 :                 cpy_tcx_ltp_data( hCPE->hStereoDft->hTcxLtpDec, &tcxLtpTmp, output_Fs );
     643             :             }
     644             : 
     645             :             /* deallocate data structure of the previous CPE mode */
     646         788 :             if ( hCPE->hStereoDft != NULL )
     647             :             {
     648         784 :                 stereo_dft_dec_destroy( &( hCPE->hStereoDft ) );
     649         784 :                 hCPE->hStereoDft = NULL;
     650             :             }
     651             : 
     652         788 :             if ( hCPE->hStereoTD != NULL )
     653             :             {
     654           4 :                 free( hCPE->hStereoTD );
     655           4 :                 hCPE->hStereoTD = NULL;
     656             :             }
     657             : 
     658         788 :             if ( hCPE->hStereoDftDmx != NULL )
     659             :             {
     660         463 :                 free( hCPE->hStereoDftDmx );
     661         463 :                 hCPE->hStereoDftDmx = NULL;
     662             :             }
     663             : 
     664         788 :             if ( hCPE->hStereoICBWE != NULL )
     665             :             {
     666         770 :                 free( hCPE->hStereoICBWE );
     667         770 :                 hCPE->hStereoICBWE = NULL;
     668             :             }
     669             : 
     670             :             /* de-allocate stereo CNG structure */
     671         788 :             if ( hCPE->hStereoCng != NULL )
     672             :             {
     673         788 :                 free( hCPE->hStereoCng );
     674         788 :                 hCPE->hStereoCng = NULL;
     675             :             }
     676             : 
     677        2364 :             for ( i = 0; i < CPE_CHANNELS; i++ )
     678             :             {
     679        1576 :                 st = hCPE->hCoreCoder[i];
     680        1576 :                 st->element_mode = hCPE->element_mode;
     681             : 
     682             :                 /* deallocate core-decoder substructures */
     683        1576 :                 deallocate_CoreCoder( st );
     684             : 
     685        1576 :                 st->first_CNG = 0;
     686             :             }
     687             : 
     688             :             /* allocate CLDFB structures for second channel */
     689         788 :             st = hCPE->hCoreCoder[1];
     690             : 
     691         788 :             if ( st->cldfbAna == NULL )
     692             :             {
     693         784 :                 if ( ( error = openCldfb( &st->cldfbAna, CLDFB_ANALYSIS, 48000, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
     694             :                 {
     695           0 :                     return error;
     696             :                 }
     697             :             }
     698             : 
     699         788 :             if ( st->cldfbBPF == NULL )
     700             :             {
     701             :                 /* open analysis BPF for max. internal sampling rate 16kHz */
     702         784 :                 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         788 :             if ( ( error = createFdCngDec( &st->hFdCngDec ) ) != IVAS_ERR_OK )
     710             :             {
     711           0 :                 return error;
     712             :             }
     713             : 
     714             :             /* Init FD-CNG */
     715         788 :             initFdCngDec( st );
     716             : 
     717         788 :             if ( hCPE->last_element_mode == IVAS_CPE_DFT )
     718             :             {
     719         784 :                 st->last_core = ACELP_CORE; /* needed to set-up TCX core in SetTCXModeInfo() */
     720             :             }
     721             : 
     722             :             /*Allocate CoreCoder TCX modules for second channel */
     723         788 :             if ( ( error = allocate_CoreCoder_TCX( hCPE->hCoreCoder[1] ) ) != IVAS_ERR_OK )
     724             :             {
     725           0 :                 return error;
     726             :             }
     727             : 
     728         788 :             if ( hCPE->last_element_mode == IVAS_CPE_DFT )
     729             :             {
     730         784 :                 if ( hCPE->nchan_out == 1 )
     731             :                 {
     732          17 :                     cpy_tcx_ltp_data( hCPE->hCoreCoder[0]->hTcxLtpDec, hCPE->hCoreCoder[1]->hTcxLtpDec, output_Fs );
     733             :                 }
     734             :                 else
     735             :                 {
     736         767 :                     cpy_tcx_ltp_data( &tcxLtpTmp, hCPE->hCoreCoder[1]->hTcxLtpDec, output_Fs );
     737             :                 }
     738             :             }
     739             : 
     740         788 :             if ( hCPE->last_element_mode == IVAS_CPE_TD )
     741             :             {
     742           4 :                 mvr2r( tmpF_buff, st->hTcxDec->old_syn_Overl, L_FRAME16k / 2 );
     743             :             }
     744             : 
     745         788 :             set_f( st->hTcxDec->FBTCXdelayBuf, 0.0f, 111 );
     746         788 :             st->hTcxDec->old_synthFB = st->hTcxDec->synth_history + NS2SA( st->output_Fs, PH_ECU_MEM_NS );
     747         788 :             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         788 :             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         788 :             if ( ivas_format == STEREO_FORMAT && hCPE->element_brate <= MAX_MDCT_ITD_BRATE )
     755             :             {
     756          32 :                 hCPE->hStereoMdct->use_itd = 1;
     757             :             }
     758             :             else
     759             :             {
     760         756 :                 hCPE->hStereoMdct->use_itd = 0;
     761             :             }
     762         788 :             hCPE->hStereoMdct->reverse_dmx = 0;
     763         788 :             hCPE->hStereoMdct->smooth_ratio = 1.f;
     764             :         }
     765             :     }
     766             : 
     767             :     /*--------------------------------------------------------------*
     768             :      * normal TD / LRTD switching
     769             :      *---------------------------------------------------------------*/
     770             : 
     771      857207 :     if ( hCPE->element_mode == IVAS_CPE_TD )
     772             :     {
     773        3800 :         if ( hCPE->hCoreCoder[0]->bfi == 0 )
     774             :         {
     775        3672 :             st = hCPE->hCoreCoder[1];
     776        3672 :             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        3672 :             if ( hCPE->hStereoTD->tdm_LRTD_flag )
     779             :             {
     780             :                 /* deallocate ICBWE structure */
     781        3554 :                 if ( hCPE->hStereoICBWE != NULL )
     782             :                 {
     783          39 :                     free( hCPE->hStereoICBWE );
     784          39 :                     hCPE->hStereoICBWE = NULL;
     785             :                 }
     786             : 
     787             :                 /* allocate BWEs for secondary channel */
     788        3554 :                 if ( st->hBWE_TD == NULL )
     789             :                 {
     790          43 :                     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          43 :                     td_bwe_dec_init( st->hBWE_TD, -1, st->output_Fs );
     796             : 
     797          43 :                     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          43 :                     fd_bwe_dec_init( st->hBWE_FD );
     803             :                 }
     804             :             }
     805             :             else /* tdm_LRTD_flag  == 0 */
     806             :             {
     807             :                 /* deallocate BWEs for secondary channel */
     808         118 :                 if ( st->hBWE_TD != NULL )
     809             :                 {
     810          33 :                     if ( st->hBWE_TD != NULL )
     811             :                     {
     812          33 :                         free( st->hBWE_TD );
     813          33 :                         st->hBWE_TD = NULL;
     814             :                     }
     815             : 
     816          33 :                     if ( st->hBWE_FD != NULL )
     817             :                     {
     818          33 :                         free( st->hBWE_FD );
     819          33 :                         st->hBWE_FD = NULL;
     820             :                     }
     821             :                 }
     822             : 
     823             :                 /* allocate ICBWE structure */
     824         118 :                 if ( hCPE->hStereoICBWE == NULL )
     825             :                 {
     826          33 :                     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          33 :                     stereo_icBWE_init_dec( hCPE->hStereoICBWE );
     832             :                 }
     833             :             }
     834             :         }
     835             :     }
     836             : 
     837             :     /*--------------------------------------------------------------*
     838             :      * MDCT stereo bitrate switching
     839             :      *---------------------------------------------------------------*/
     840             : 
     841      857207 :     if ( ivas_format == STEREO_FORMAT && hCPE->element_mode == IVAS_CPE_MDCT )
     842             :     {
     843       21547 :         if ( hCPE->element_brate <= MAX_MDCT_ITD_BRATE && ivas_total_brate > IVAS_SID_5k2 )
     844             :         {
     845       11977 :             if ( hCPE->hStereoMdct->use_itd == 0 )
     846             :             {
     847          39 :                 if ( hCPE->hStereoTCA == NULL )
     848             :                 {
     849             :                     /* allocate TCA data structure */
     850          14 :                     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          39 :                 stereo_tca_init_dec( hCPE->hStereoTCA );
     856             :             }
     857             : 
     858       11977 :             hCPE->hStereoMdct->use_itd = 1;
     859             :         }
     860             :         else
     861             :         {
     862             :             /* de-allocate TCA data structure */
     863        9570 :             if ( hCPE->hStereoMdct->use_itd == 1 && ivas_total_brate > IVAS_SID_5k2 && hCPE->hStereoTCA != NULL )
     864             :             {
     865          32 :                 free( hCPE->hStereoTCA );
     866          32 :                 hCPE->hStereoTCA = NULL;
     867          32 :                 hCPE->hStereoMdct->use_itd = 0;
     868             :             }
     869        9538 :             else if ( hCPE->hStereoMdct->use_itd == 1 && ivas_total_brate <= IVAS_SID_5k2 )
     870             :             {
     871        2911 :                 hCPE->hStereoMdct->itd = 0.0f;
     872             :             }
     873             :             else
     874             :             {
     875        6627 :                 hCPE->hStereoMdct->use_itd = 0;
     876             :             }
     877             :         }
     878             :     }
     879             : 
     880             :     /*--------------------------------------------------------------*
     881             :      * Bitrate switching in MASA format
     882             :      *---------------------------------------------------------------*/
     883             : 
     884      857207 :     if ( ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT ) && nchan_transport == 2 )
     885             :     {
     886       77833 :         if ( hCPE->nchan_out == 1 )
     887             :         {
     888       26541 :             if ( hCPE->hStereoDftDmx == NULL )
     889             :             {
     890         144 :                 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         144 :                 stereo_dft_dmx_out_reset( hCPE->hStereoDftDmx );
     895             :             }
     896             : 
     897       26541 :             if ( hCPE->prev_synth_chs[1] != NULL )
     898             :             {
     899         567 :                 free( hCPE->prev_synth_chs[1] );
     900         567 :                 hCPE->prev_synth_chs[1] = NULL;
     901             :             }
     902             : 
     903       26541 :             if ( hCPE->hStereoTCA != NULL )
     904             :             {
     905         144 :                 free( hCPE->hStereoTCA );
     906         144 :                 hCPE->hStereoTCA = NULL;
     907             :             }
     908             :         }
     909             :         else /* nchan_out == 2 */
     910             :         {
     911       51292 :             if ( hCPE->hStereoDftDmx != NULL )
     912             :             {
     913         135 :                 free( hCPE->hStereoDftDmx );
     914         135 :                 hCPE->hStereoDftDmx = NULL;
     915             :             }
     916             : 
     917       51292 :             if ( hCPE->prev_synth_chs[1] == NULL )
     918             :             {
     919         580 :                 st = hCPE->hCoreCoder[1];
     920         580 :                 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         580 :                 set_zero( hCPE->prev_synth_chs[1], NS2SA( st->output_Fs, FRAME_SIZE_NS ) );
     925             :             }
     926             : 
     927       51292 :             if ( hCPE->hStereoICBWE == NULL && hCPE->element_mode == IVAS_CPE_DFT )
     928             :             {
     929           5 :                 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           5 :                 stereo_icBWE_init_dec( hCPE->hStereoICBWE );
     935             :             }
     936             : 
     937       51292 :             if ( hCPE->hStereoTCA == NULL && ( hCPE->element_mode == IVAS_CPE_DFT || hCPE->element_mode == IVAS_CPE_TD ) )
     938             :             {
     939         135 :                 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         135 :                 stereo_tca_init_dec( hCPE->hStereoTCA );
     945             :             }
     946             : 
     947       51292 :             if ( hCPE->element_mode == IVAS_CPE_MDCT )
     948             :             {
     949       40012 :                 if ( hCPE->hStereoTCA != NULL )
     950             :                 {
     951             :                     /* note: in MASA, hCPE->hStereoMdct->itd = 0 */
     952         271 :                     free( hCPE->hStereoTCA );
     953         271 :                     hCPE->hStereoTCA = NULL;
     954             :                 }
     955             : 
     956       40012 :                 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      857207 :     if ( ivas_format == MC_FORMAT && hCPE->element_mode == IVAS_CPE_MDCT )
     970             :     {
     971      280604 :         if ( mc_mode == MC_MODE_MCT || mc_mode == MC_MODE_PARAMUPMIX )
     972             :         {
     973             :             /* deallocate the FdCNG handle */
     974      775770 :             for ( i = 0; i < CPE_CHANNELS; ++i )
     975             :             {
     976      517180 :                 deleteFdCngDec( &hCPE->hCoreCoder[i]->hFdCngDec );
     977             :             }
     978             :         }
     979             :         else
     980             :         {
     981             :             /* allocate the FdCNG handle (for noise estimation for TCX PLC fadeout)*/
     982       66042 :             for ( i = 0; i < CPE_CHANNELS; ++i )
     983             :             {
     984       44028 :                 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       44028 :                 if ( hCPE->hCoreCoder[i]->hFdCngDec == NULL )
     993             :                 {
     994         434 :                     if ( ( error = createFdCngDec( &hCPE->hCoreCoder[i]->hFdCngDec ) ) != IVAS_ERR_OK )
     995             :                     {
     996           0 :                         return error;
     997             :                     }
     998         434 :                     initFdCngDec( hCPE->hCoreCoder[i] );
     999             :                 }
    1000             :             }
    1001             :         }
    1002             :     }
    1003             : 
    1004      857207 :     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      856780 : 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      856780 :     sts = hCPE->hCoreCoder;
    1038      856780 :     output_Fs = sts[0]->output_Fs;
    1039             : 
    1040      856780 :     use_cldfb_for_last_dft = 0;
    1041      856780 :     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      855106 :          || ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->nchan_out == 1 && hCPE->hStereoDft->hConfig->res_cod_mode == STEREO_DFT_RES_COD_OFF ) )
    1043             :     {
    1044       39407 :         use_cldfb_for_last_dft = 1;
    1045             :     }
    1046             : 
    1047      856780 :     dft_mono_brate_switch = 0;
    1048      856780 :     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        2157 :         if ( hCPE->last_element_brate >= IVAS_32k && hCPE->hStereoDft->hConfig->res_cod_mode == STEREO_DFT_RES_COD_OFF )
    1051             :         {
    1052         582 :             dft_mono_brate_switch = -1; /* switch from residual coding mode or MDCT Stereo */
    1053             :         }
    1054        1575 :         else if ( hCPE->last_element_brate <= IVAS_24k4 && hCPE->hStereoDft->hConfig->res_cod_mode > STEREO_DFT_RES_COD_OFF )
    1055             :         {
    1056          58 :             dft_mono_brate_switch = 1; /* switch to residual coding mode*/
    1057             :         }
    1058             :     }
    1059             : 
    1060      856780 :     if ( use_cldfb_for_last_dft )
    1061             :     {
    1062       39407 :         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           7 :             stereo_tca_scale_R_channel( hCPE, output[0], output_frame );
    1065             :         }
    1066             :     }
    1067             : 
    1068             :     /* set delays */
    1069      856780 :     delay_comp_DFT = NS2SA( output_Fs, IVAS_DEC_DELAY_NS - STEREO_DFT32MS_OVL_NS );
    1070      856780 :     delay_comp_TD = NS2SA( output_Fs, IVAS_DEC_DELAY_NS - DELAY_CLDFB_NS );
    1071      856780 :     delay_diff = delay_comp_TD - delay_comp_DFT;
    1072             : 
    1073      856780 :     dft32ms_ovl = (int16_t) ( ( STEREO_DFT32MS_OVL_MAX * output_Fs ) / 48000 );
    1074      856780 :     delay_cldfb = NS2SA( output_Fs, DELAY_CLDFB_NS );
    1075             : 
    1076             :     /* initialize pointers */
    1077      856780 :     if ( hCPE->element_mode >= IVAS_CPE_DFT && hCPE->output_mem[0] != NULL )
    1078             :     {
    1079      485904 :         for ( n = 0; n < CPE_CHANNELS; n++ )
    1080             :         {
    1081      323936 :             p_output_mem[n] = hCPE->output_mem[n];
    1082             :         }
    1083             :     }
    1084             : 
    1085             :     /*----------------------------------------------------------------*
    1086             :      * DFT stereo synchro
    1087             :      *----------------------------------------------------------------*/
    1088             : 
    1089      856780 :     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       96764 :         if ( dft_mono_brate_switch == -1 )
    1093             :         {
    1094       46212 :             for ( i = delay_comp_DFT; i < delay_comp_TD; i++ )
    1095             :             {
    1096       45630 :                 sts[0]->prev_synth_buffer[i] = p_output_mem[0][i - delay_comp_DFT];
    1097             :             }
    1098             : 
    1099       31002 :             for ( i = delay_comp_TD; i < delay_comp_TD + delay_cldfb; i++ )
    1100             :             {
    1101       30420 :                 tmp_out[0][i - delay_comp_TD] = p_output_mem[0][i - delay_comp_DFT];
    1102             :             }
    1103             :         }
    1104             : 
    1105       96764 :         if ( hCPE->nchan_out == 1 && hCPE->last_element_mode == IVAS_CPE_MDCT )
    1106             :         {
    1107         440 :             v_add( sts[0]->prev_synth_buffer, sts[1]->prev_synth_buffer, sts[0]->prev_synth_buffer, delay_comp_DFT );
    1108         440 :             v_multc( sts[0]->prev_synth_buffer, INV_SQRT_2, sts[0]->prev_synth_buffer, delay_comp_DFT );
    1109             :         }
    1110             : 
    1111       96764 :         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       37733 :             mvr2r( sts[0]->prev_synth_buffer + delay_comp_DFT, hCPE->hCoreCoder[0]->hTcxDec->FBTCXdelayBuf, delay_diff );
    1115       37733 :             delay_signal( output[0], output_frame, hCPE->hCoreCoder[0]->hTcxDec->FBTCXdelayBuf, delay_diff );
    1116             :         }
    1117             : 
    1118       96764 :         if ( hCPE->element_mode != IVAS_CPE_MDCT )
    1119             :         {
    1120       89784 :             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      247217 :         for ( n = 0; n < hCPE->nchan_out; n++ )
    1125             :         {
    1126      150453 :             if ( sba_dirac_stereo_flag )
    1127             :             {
    1128       21576 :                 delay_signal( output[n], output_frame, hCPE->prev_synth[n], delay_comp_DFT );
    1129             :             }
    1130             :             else
    1131             :             {
    1132      128877 :                 delay_signal( output[n], output_frame, sts[n]->prev_synth_buffer, delay_comp_DFT );
    1133             :             }
    1134             :         }
    1135             : 
    1136       96764 :         if ( use_cldfb_for_last_dft )
    1137             :         {
    1138       37733 :             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      247217 :         for ( n = 0; n < hCPE->nchan_out; n++ )
    1143             :         {
    1144      150453 :             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         759 :             {
    1146             :                 float *pPrev_synth;
    1147         759 :                 float inv_fade_len = 1.f / delay_diff;
    1148             : 
    1149             :                 /* cross-fading between TD synchro memory and the DFT output */
    1150         759 :                 if ( sba_dirac_stereo_flag )
    1151             :                 {
    1152           0 :                     pPrev_synth = hCPE->prev_synth[n];
    1153             :                 }
    1154             :                 else
    1155             :                 {
    1156         759 :                     pPrev_synth = sts[n]->prev_synth_buffer;
    1157             :                 }
    1158             : 
    1159         759 :                 if ( hCPE->last_element_mode != IVAS_CPE_MDCT )
    1160             :                 {
    1161        7046 :                     for ( i = delay_comp_DFT; i < delay_comp_TD; i++ )
    1162             :                     {
    1163        6930 :                         output[n][i] = ( pPrev_synth[i] * ( delay_comp_TD - i ) + output[n][i] * ( i - delay_comp_DFT ) ) * inv_fade_len;
    1164             :                     }
    1165             :                 }
    1166             :             }
    1167      149694 :             else if ( dft_mono_brate_switch == -1 )
    1168             :             {
    1169         582 :                 float inv_fade_len_1 = 1.0f / (float) delay_diff;
    1170         582 :                 float inv_fade_len_2 = 1.0f / (float) delay_cldfb;
    1171             : 
    1172       46212 :                 for ( i = 0; i < delay_diff; i++ )
    1173             :                 {
    1174       45630 :                     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       31002 :                 for ( i = 0; i < delay_cldfb; i++ )
    1178             :                 {
    1179       30420 :                     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      856780 :     if ( sba_dirac_stereo_flag )
    1190             :     {
    1191       10788 :         return;
    1192             :     }
    1193             : 
    1194      845992 :     if ( hCPE->element_mode == IVAS_CPE_TD || hCPE->element_mode == IVAS_CPE_MDCT )
    1195             :     {
    1196             :         /* handling of DFT->TD switching */
    1197      760016 :         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        2412 :             for ( n = 0; n < hCPE->nchan_out; n++ )
    1201             :             {
    1202      129644 :                 for ( i = delay_comp_DFT; i < delay_comp_TD; i++ )
    1203             :                 {
    1204      128040 :                     sts[n]->prev_synth_buffer[i] = p_output_mem[n][i - delay_comp_DFT];
    1205             :                 }
    1206             : 
    1207       86964 :                 for ( i = delay_comp_TD; i < delay_comp_TD + delay_cldfb; i++ )
    1208             :                 {
    1209       85360 :                     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      760016 :         if ( hCPE->nchan_out == 1 && hCPE->element_mode == IVAS_CPE_MDCT && hCPE->last_element_mode == IVAS_CPE_DFT )
    1216             :         {
    1217          17 :             mvr2r( sts[0]->prev_synth_buffer, sts[1]->prev_synth_buffer, delay_comp_TD );
    1218          17 :             mvr2r( tmp_out[0], tmp_out[1], delay_cldfb );
    1219          17 :             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      760016 :         if ( hCPE->element_mode != IVAS_CPE_MDCT && !use_cldfb_for_last_dft )
    1228             :         {
    1229        8949 :             for ( n = 0; n < hCPE->nchan_out; n++ )
    1230             :             {
    1231        5698 :                 if ( sts[n]->core == ACELP_CORE )
    1232             :                 {
    1233        5538 :                     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         160 :                     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        6980 :                     for ( i = delay_diff; i < dft32ms_ovl; i++ )
    1241             :                     {
    1242        6820 :                         tmp_out_TD[n][i] = sts[n]->delay_buf_out[i - delay_diff];
    1243             :                     }
    1244             :                 }
    1245             :             }
    1246             : 
    1247        3251 :             if ( hCPE->nchan_out == CPE_CHANNELS )
    1248             :             {
    1249             :                 /* upmix the resampled LB / the TCX synchro memory */
    1250        2447 :                 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         804 :                 mvr2r( tmp_out_TD[0], tmp_out_TD2[0], dft32ms_ovl );
    1255             :             }
    1256             : 
    1257        8949 :             for ( n = 0; n < hCPE->nchan_out; n++ )
    1258             :             {
    1259        5698 :                 if ( sts[0]->core == ACELP_CORE ) /* ACELP core in primary channel */
    1260             :                 {
    1261        5424 :                     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      347184 :                     for ( i = 0; i < delay_diff; i++ )
    1265             :                     {
    1266      341760 :                         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       17974 :                     for ( i = 0; i < delay_diff; i++ )
    1273             :                     {
    1274       17700 :                         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      245338 :                 for ( i = delay_diff; i < dft32ms_ovl; i++ )
    1280             :                 {
    1281      239640 :                     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      760016 :         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          15 :             tmpF = 1.0f;
    1295          15 :             step = 0.5f / delay_comp_TD;
    1296             : 
    1297             :             /* for the first switching frame from DFT to TD, downmix memory too */
    1298         847 :             for ( n = 0; n < delay_comp_TD; n++ )
    1299             :             {
    1300         832 :                 sts[0]->prev_synth_buffer[n] = ( sts[0]->prev_synth_buffer[n] ) * tmpF;
    1301         832 :                 tmpF -= step;
    1302             :             }
    1303             :         }
    1304             : 
    1305      760016 :         if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->nchan_out == 1 && !is_DTXrate( hCPE->element_brate ) && is_DTXrate( hCPE->last_element_brate ) )
    1306             :         {
    1307          35 :             mvr2r( sts[0]->prev_synth_buffer, sts[1]->prev_synth_buffer, delay_comp_TD );
    1308             :         }
    1309             : 
    1310      760016 :         nChannels = ( hCPE->element_mode == IVAS_CPE_MDCT ) ? 2 : hCPE->nchan_out;
    1311     2278695 :         for ( n = 0; n < nChannels; n++ )
    1312             :         {
    1313     1518679 :             if ( hCPE->element_mode == IVAS_CPE_MDCT )
    1314             :             {
    1315     1512432 :                 mvr2r( sts[n]->prev_synth_buffer + delay_comp_DFT, hCPE->hCoreCoder[n]->hTcxDec->FBTCXdelayBuf, delay_diff );
    1316     1512432 :                 delay_signal( output[n], output_frame, hCPE->hCoreCoder[n]->hTcxDec->FBTCXdelayBuf, delay_diff );
    1317     1512432 :                 ivas_post_proc( NULL, hCPE, n, output[n], output, output_frame, 0 );
    1318     1512432 :                 delay_signal( output[n], output_frame, sts[n]->prev_synth_buffer, delay_comp_DFT );
    1319     1512432 :                 mvr2r( hCPE->hCoreCoder[n]->hTcxDec->FBTCXdelayBuf, sts[n]->prev_synth_buffer + delay_comp_DFT, delay_diff );
    1320             :             }
    1321             :             else
    1322             :             {
    1323        6247 :                 delay_signal( output[n], output_frame, sts[n]->prev_synth_buffer, delay_comp_TD );
    1324             :             }
    1325             :         }
    1326             : 
    1327             :         /* handling of DFT->TD switching */
    1328      760016 :         if ( hCPE->last_element_mode == IVAS_CPE_DFT && !use_cldfb_for_last_dft )
    1329             :         {
    1330         808 :             if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->hStereoCng->prev_sid_nodata )
    1331             :             {
    1332          21 :                 for ( n = 0; n < hCPE->nchan_out; n++ )
    1333             :                 {
    1334          14 :                     tmpF = 1.0f / (float) delay_cldfb;
    1335             : 
    1336         654 :                     for ( i = 0; i < delay_cldfb; i++ )
    1337             :                     {
    1338         640 :                         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        2417 :             for ( n = 0; n < nChannels; n++ )
    1345             :             {
    1346        1609 :                 if ( hCPE->element_mode == IVAS_CPE_MDCT )
    1347             :                 {
    1348        1544 :                     tmpF = 1.0f / (float) delay_diff;
    1349             : 
    1350      125924 :                     for ( i = 0; i < delay_diff; i++ )
    1351             :                     {
    1352      124380 :                         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        1609 :                 tmpF = 1.0f / (float) delay_cldfb;
    1357             : 
    1358       87269 :                 for ( i = 0; i < delay_cldfb; i++ )
    1359             :                 {
    1360       85660 :                     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      845992 :     return;
    1367             : }
    1368             : 
    1369             : 
    1370             : /*-------------------------------------------------------------------*
    1371             :  * Function stereo_switching_dec()
    1372             :  *
    1373             :  * Handling of memories in case of CPE modes switching
    1374             :  *-------------------------------------------------------------------*/
    1375             : 
    1376      857207 : 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      857207 :     sts = hCPE->hCoreCoder;
    1388             : 
    1389      857207 :     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      857207 :     if ( sts[0]->ini_frame == 0 )
    1393             :     {
    1394        5237 :         hCPE->last_element_mode = hCPE->element_mode;
    1395        5237 :         hCPE->stereo_switching_counter = 10;
    1396        5237 :         hCPE->NbFrameMod = 7;
    1397        5237 :         hCPE->lt_es_em = 0.0f;
    1398             :     }
    1399             : 
    1400      857207 :     if ( hCPE->element_mode == hCPE->last_element_mode )
    1401             :     {
    1402      855545 :         hCPE->stereo_switching_counter++;
    1403      855545 :         hCPE->stereo_switching_counter = min( 10, hCPE->stereo_switching_counter );
    1404             :     }
    1405             :     else
    1406             :     {
    1407        1662 :         hCPE->stereo_switching_counter = 0;
    1408             :     }
    1409             : 
    1410      857207 :     if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->nchan_out == 1 && hCPE->element_brate >= IVAS_32k && hCPE->last_element_brate <= IVAS_24k4 )
    1411             :     {
    1412          58 :         dft32ms_ovl = (int16_t) ( ( STEREO_DFT32MS_OVL_MAX * sts[0]->output_Fs ) / 48000 );
    1413          58 :         set_zero( hCPE->output_mem[0], dft32ms_ovl );
    1414             :     }
    1415             : 
    1416      857207 :     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         814 :         dft32ms_ovl = (int16_t) ( ( STEREO_DFT32MS_OVL_MAX * sts[0]->output_Fs ) / 48000 );
    1420        2442 :         for ( n = 0; n < CPE_CHANNELS; n++ )
    1421             :         {
    1422        1628 :             if ( hCPE->last_element_mode == IVAS_CPE_MDCT )
    1423             :             {
    1424             :                 /* copy memories from previous MDCT Stereo frame to output_mem */
    1425        1518 :                 mvr2r( hCPE->input_mem[n], hCPE->output_mem[n], dft32ms_ovl );
    1426             :             }
    1427             : 
    1428        1628 :             if ( ivas_total_brate > IVAS_SID_5k2 || n == 0 || hCPE->last_element_mode != IVAS_CPE_TD || hCPE->nchan_out == 1 )
    1429             :             {
    1430      215514 :                 for ( i = 0; i < dft32ms_ovl; i++ )
    1431             :                 {
    1432      213900 :                     hCPE->output_mem[n][i] *= hCPE->hStereoDft->win32ms[STEREO_DFT32MS_STEP * ( dft32ms_ovl - 1 - i )];
    1433             :                 }
    1434             :             }
    1435             :             else
    1436             :             {
    1437          14 :                 tmpF = 1.0f / hCPE->hStereoTCA->prevTargetGain;
    1438        1614 :                 for ( i = 0; i < dft32ms_ovl; i++ )
    1439             :                 {
    1440        1600 :                     hCPE->output_mem[n][i] *= tmpF * hCPE->hStereoDft->win32ms[STEREO_DFT32MS_STEP * ( dft32ms_ovl - 1 - i )];
    1441             :                 }
    1442        1038 :                 for ( i = 0; i < delay_comp_TD; i++ )
    1443             :                 {
    1444        1024 :                     hCPE->hCoreCoder[1]->prev_synth_buffer[i] *= tmpF;
    1445             :                 }
    1446        3836 :                 for ( i = 0; i < L_DEC_MEM_LEN_ICA; i++ )
    1447             :                 {
    1448        3822 :                     hCPE->hStereoTCA->memChanR[i] *= tmpF;
    1449             :                 }
    1450             :             }
    1451             :         }
    1452             : 
    1453         814 :         if ( hCPE->last_element_mode == IVAS_CPE_MDCT )
    1454             :         {
    1455             :             /* create passive downmix of MDCT Stereo memories for DFT input memory */
    1456         759 :             v_add( hCPE->input_mem_LB[0], hCPE->input_mem_LB[1], hCPE->input_mem_LB[0], STEREO_DFT32MS_OVL_16k );
    1457         759 :             v_multc( hCPE->input_mem_LB[0], 0.5f, hCPE->input_mem_LB[0], STEREO_DFT32MS_OVL_16k );
    1458             : 
    1459         759 :             v_add( hCPE->input_mem[0], hCPE->input_mem[1], hCPE->input_mem[0], dft32ms_ovl );
    1460         759 :             v_multc( hCPE->input_mem[0], 0.5f, hCPE->input_mem[0], dft32ms_ovl );
    1461             : 
    1462         759 :             if ( hCPE->nchan_out == 1 )
    1463             :             {
    1464         440 :                 v_add( hCPE->output_mem[0], hCPE->output_mem[1], hCPE->output_mem[0], dft32ms_ovl );
    1465         440 :                 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         814 :         if ( hCPE->last_element_mode != IVAS_CPE_MDCT )
    1471             :         {
    1472          55 :             tmpF = 0;
    1473          55 :             if ( hCPE->hStereoTCA != NULL )
    1474             :             {
    1475          29 :                 tmpF = usdequant( hCPE->hStereoTCA->indx_ica_gD, STEREO_TCA_GDMIN, STEREO_TCA_GDSTEP );
    1476             :             }
    1477             : 
    1478          55 :             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         814 :         set_zero( hCPE->hStereoDft->res_cod_mem, STEREO_DFT_OVL_8k );
    1483         814 :         set_zero( hCPE->input_mem[1], NS2SA( sts[0]->output_Fs, STEREO_DFT32MS_OVL_NS ) );
    1484             :     }
    1485             : 
    1486      857207 :     if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->last_element_mode != IVAS_CPE_TD && hCPE->hCoreCoder[0]->ini_frame > 0 )
    1487             :     {
    1488          60 :         hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_MID_IS_PRIM;
    1489          60 :         hCPE->hStereoTD->tdm_last_SM_flag = 0;
    1490          60 :         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          60 :         if ( hCPE->hStereoTD->tdm_LRTD_flag == 1 )
    1494             :         {
    1495          60 :             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      857207 :     if ( hCPE->element_mode > IVAS_CPE_DFT && hCPE->last_element_mode == IVAS_CPE_DFT )
    1501             :     {
    1502         835 :         if ( hCPE->last_element_brate <= IVAS_SID_5k2 && hCPE->nchan_out == 2 )
    1503             :         {
    1504             :             /* reset CLDFB memories */
    1505           7 :             cldfb_reset_memory( sts[0]->cldfbAna );
    1506           7 :             cldfb_reset_memory( sts[0]->cldfbBPF );
    1507           7 :             cldfb_reset_memory( sts[0]->cldfbSyn );
    1508             : 
    1509           7 :             sts[0]->mem_deemph = 0;
    1510             : 
    1511           7 :             sts[0]->tilt_code = 0.0f;
    1512           7 :             sts[0]->gc_threshold = 0.0f;
    1513             : 
    1514           7 :             set_f( sts[0]->mem_syn1, 0, M );
    1515           7 :             set_f( sts[0]->mem_syn2, 0, M );
    1516           7 :             set_f( sts[0]->mem_syn3, 0, M );
    1517           7 :             set_f( sts[0]->mem_syn_r, 0.0f, L_SYN_MEM );
    1518             : 
    1519           7 :             sts[1]->last_L_frame = sts[0]->last_L_frame;
    1520             : 
    1521             :             /* reset PCh memories */
    1522           7 :             set_f( sts[0]->old_exc, 0, L_EXC_MEM_DEC );
    1523           7 :             set_f( sts[0]->lsf_old, 0, M );
    1524           7 :             set_f( sts[0]->lsp_old, 0, M );
    1525             :         }
    1526         835 :         sts[1]->last_extl = -1;
    1527             : 
    1528         835 :         sts[1]->prev_bfi = sts[0]->prev_bfi;
    1529             : 
    1530         835 :         set_f( sts[1]->old_pitch_buf, (float) L_SUBFR, 2 * NB_SUBFR16k );
    1531         835 :         sts[1]->old_fpitchFB = 2 * (float) L_SUBFR;
    1532             : 
    1533             :         /* reset CLDFB memories */
    1534         835 :         cldfb_reset_memory( sts[1]->cldfbAna );
    1535         835 :         cldfb_reset_memory( sts[1]->cldfbBPF );
    1536         835 :         cldfb_reset_memory( sts[1]->cldfbSyn );
    1537             : 
    1538         835 :         sts[1]->mem_deemph = 0;
    1539             : 
    1540         835 :         sts[1]->tilt_code = 0.0f;
    1541         835 :         sts[1]->gc_threshold = 0.0f;
    1542             : 
    1543         835 :         set_f( sts[1]->mem_syn1, 0, M );
    1544         835 :         set_f( sts[1]->mem_syn2, 0, M );
    1545         835 :         set_f( sts[1]->mem_syn3, 0, M );
    1546         835 :         set_f( sts[1]->mem_syn_r, 0.0f, L_SYN_MEM );
    1547             : 
    1548         835 :         sts[1]->last_L_frame = sts[0]->last_L_frame;
    1549             : 
    1550             :         /* populate PCh memories into the SCh */
    1551         835 :         mvr2r( sts[0]->old_exc, sts[1]->old_exc, L_EXC_MEM_DEC );
    1552         835 :         mvr2r( sts[0]->lsf_old, sts[1]->lsf_old, M );
    1553         835 :         mvr2r( sts[0]->lsp_old, sts[1]->lsp_old, M );
    1554             : 
    1555         835 :         sts[1]->last_core_brate = sts[0]->last_core_brate;
    1556             : 
    1557         835 :         if ( hCPE->element_mode == IVAS_CPE_MDCT )
    1558             :         {
    1559         784 :             sts[1]->last_core = sts[0]->last_core;
    1560         784 :             sts[1]->last_coder_type = sts[0]->last_coder_type;
    1561             : 
    1562         784 :             mvr2r( sts[0]->hHQ_core->old_out, sts[1]->hHQ_core->old_out, L_FRAME48k );
    1563         784 :             mvr2r( sts[0]->delay_buf_out, sts[1]->delay_buf_out, HQ_DELTA_MAX * HQ_DELAY_COMP );
    1564         784 :             mvr2r( sts[0]->hTcxDec->old_syn_Overl, sts[1]->hTcxDec->old_syn_Overl, 256 );
    1565             :         }
    1566             :     }
    1567      856372 :     else if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->last_element_mode == IVAS_CPE_MDCT )
    1568             :     {
    1569           9 :         set_f( sts[0]->old_exc, 0.0f, L_EXC_MEM_DEC );
    1570           9 :         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      857207 :     if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->last_element_mode != IVAS_CPE_MDCT )
    1575             :     {
    1576         788 :         sts[1]->hTcxCfg->last_aldo = sts[0]->hTcxCfg->last_aldo;
    1577         788 :         sts[1]->hTcxCfg->tcx_curr_overlap_mode = sts[0]->hTcxCfg->tcx_curr_overlap_mode;
    1578         788 :         sts[1]->fscale = sts[0]->fscale;
    1579         788 :         sts[1]->hTcxCfg->tcx_mdct_window_length = sts[0]->hTcxCfg->tcx_mdct_window_length;
    1580         788 :         sts[1]->pit_res_max = sts[0]->pit_res_max;
    1581         788 :         sts[1]->pit_res_max_past = sts[0]->pit_res_max_past;
    1582         788 :         sts[1]->hTcxDec->L_frameTCX = sts[0]->hTcxDec->L_frameTCX;
    1583         788 :         sts[1]->hTcxDec->conceal_eof_gain = sts[0]->hTcxDec->conceal_eof_gain;
    1584             :     }
    1585             : 
    1586      857207 :     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      750287 : 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      750287 :     if ( hCPE == NULL )
    1611             :     {
    1612      444962 :         return;
    1613             :     }
    1614             : 
    1615             :     /* initialization */
    1616      305325 :     sts = hCPE->hCoreCoder;
    1617      305325 :     ovl = NS2SA( sts[n]->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS );
    1618      305325 :     dft32ms_ovl = (int16_t) ( ( STEREO_DFT32MS_OVL_MAX * sts[0]->output_Fs ) / 48000 );
    1619      305325 :     hq_delay_comp = NS2SA( sts[0]->output_Fs, DELAY_CLDFB_NS );
    1620             : 
    1621      305325 :     if ( hCPE->element_mode >= IVAS_CPE_DFT && hCPE->element_mode != IVAS_CPE_MDCT )
    1622             :     {
    1623       45333 :         if ( sts[n]->core == ACELP_CORE )
    1624             :         {
    1625       42760 :             if ( n == 0 )
    1626             :             {
    1627             :                 /* update DFT analysis overlap memory @internal_fs: core synthesis */
    1628       38960 :                 mvr2r( output + sts[n]->L_frame - ovl, hCPE->input_mem_LB[n], ovl );
    1629             : 
    1630             :                 /* update DFT analysis overlap memory @internal_fs: BPF */
    1631       38960 :                 if ( sts[n]->p_bpf_noise_buf )
    1632             :                 {
    1633       36929 :                     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       38960 :                 if ( sts[n]->extl != -1 || ( sts[n]->bws_cnt > 0 && sts[n]->core == ACELP_CORE ) )
    1638             :                 {
    1639       35706 :                     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        3800 :                 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        2573 :             mvr2r( output + sts[n]->L_frame - ovl, hCPE->input_mem_LB[n], ovl );
    1652             : 
    1653             :             /* BPF */
    1654        2573 :             if ( n == 0 && sts[n]->p_bpf_noise_buf )
    1655             :             {
    1656        2573 :                 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        2573 :             if ( sts[n]->hTcxDec != NULL )
    1661             :             {
    1662        2573 :                 ovl_TCX = NS2SA( sts[n]->hTcxDec->L_frameTCX * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS );
    1663        2573 :                 mvr2r( synth + sts[n]->hTcxDec->L_frameTCX + hq_delay_comp - ovl_TCX, hCPE->input_mem[n], ovl_TCX - hq_delay_comp );
    1664        2573 :                 mvr2r( sts[n]->delay_buf_out, hCPE->input_mem[n] + ovl_TCX - hq_delay_comp, hq_delay_comp );
    1665             :             }
    1666             :         }
    1667             :     }
    1668      259992 :     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      125204 :         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      125204 :         if ( sts[n]->hTcxDec != NULL )
    1678             :         {
    1679      125204 :             ovl_TCX = NS2SA( sts[n]->hTcxDec->L_frameTCX * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS );
    1680      125204 :             mvr2r( synth + sts[n]->hTcxDec->L_frameTCX + hq_delay_comp - ovl_TCX, hCPE->input_mem[n], ovl_TCX - hq_delay_comp );
    1681      125204 :             mvr2r( sts[n]->delay_buf_out, hCPE->input_mem[n] + ovl_TCX - hq_delay_comp, hq_delay_comp );
    1682             :         }
    1683             : 
    1684      125204 :         if ( n == 1 )
    1685             :         {
    1686       62602 :             nsLB = NS2SA( sts[n]->L_frame * FRAMES_PER_SEC, N_ZERO_MDCT_NS );
    1687       62602 :             ns = NS2SA( sts[n]->output_Fs, N_ZERO_MDCT_NS );
    1688       62602 :             old_outLB_len = (int16_t) ( ( 3 * STEREO_MDCT2DFT_FADE_LEN_48k * sts[0]->L_frame * FRAMES_PER_SEC ) / 48000 );
    1689       62602 :             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       62602 :             v_add( sts[0]->hHQ_core->old_outLB + nsLB, sts[1]->hHQ_core->old_outLB + nsLB, hCPE->old_outLB_mdct, old_outLB_len );
    1693       62602 :             lerp( hCPE->old_outLB_mdct, hCPE->old_outLB_mdct, STEREO_MDCT2DFT_FADE_LEN_48k, old_outLB_len );
    1694       62602 :             v_multc( hCPE->old_outLB_mdct, 0.5f, hCPE->old_outLB_mdct, STEREO_MDCT2DFT_FADE_LEN_48k );
    1695             : 
    1696       62602 :             v_add( sts[0]->hHQ_core->old_out + ns, sts[1]->hHQ_core->old_out + ns, hCPE->old_out_mdct, old_out_len );
    1697       62602 :             v_multc( hCPE->old_out_mdct, 0.5f, hCPE->old_out_mdct, old_out_len );
    1698             :         }
    1699             : 
    1700      125204 :         if ( n == 0 )
    1701             :         {
    1702       62602 :             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      305325 :     if ( hCPE->element_mode == IVAS_CPE_TD && n == 1 && sts[n]->hTcxDec == NULL )
    1708             :     {
    1709        3800 :         mvr2r( output + sts[n]->L_frame / 2, hCPE->hStereoTD->TCX_old_syn_Overl, sts[n]->L_frame / 2 );
    1710             :     }
    1711             : 
    1712      305325 :     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         759 : 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         759 :     if ( hCPE == NULL )
    1734             :     {
    1735           0 :         return;
    1736             :     }
    1737             : 
    1738         759 :     st = hCPE->hCoreCoder[0];
    1739             : 
    1740         759 :     fade_len = (int16_t) ( ( STEREO_MDCT2DFT_FADE_LEN_48k * st->output_Fs ) / 48000 );
    1741         759 :     fade_len_LB = (int16_t) ( 3 * ( STEREO_MDCT2DFT_FADE_LEN_48k * st->L_frame * FRAMES_PER_SEC ) / 48000 );
    1742             : 
    1743         759 :     tmpF = 1.f / fade_len;
    1744       82479 :     for ( i = 0; i < fade_len; i++ )
    1745             :     {
    1746       81720 :         synth0[i] = ( hCPE->old_out_mdct[i] * ( fade_len - i ) + synth0[i] * i ) * tmpF;
    1747             :     }
    1748             : 
    1749         759 :     tmpF = 1.f / fade_len_LB;
    1750       80799 :     for ( i = 0; i < fade_len_LB; i++ )
    1751             :     {
    1752       80040 :         output0[i] = ( hCPE->old_outLB_mdct[i] * ( fade_len_LB - i ) + output0[i] * i ) * tmpF;
    1753             :     }
    1754             : 
    1755         759 :     return;
    1756             : }
    1757             : 
    1758             : 
    1759         320 : 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         320 :     c_c = 0.0f;
    1772         320 :     energy_x = 0.0f;
    1773         320 :     energy_y = 0.0f;
    1774         320 :     signal_a = &signal[x];
    1775         320 :     signal_b = &signal[y];
    1776             : 
    1777        2880 :     for ( j = 0; j < corr_len; j += subsampling )
    1778             :     {
    1779        2560 :         c_c += ( signal_a[j] * signal_b[j] );
    1780        2560 :         energy_x += ( signal_a[j] ) * ( signal_a[j] );
    1781        2560 :         energy_y += ( signal_b[j] ) * ( signal_b[j] );
    1782             :     }
    1783             : 
    1784         320 :     energy_xy = sqrtf( energy_x * energy_y );
    1785             : 
    1786         320 :     if ( energy_xy < 1.0f )
    1787             :     {
    1788           0 :         energy_xy = 1.0f; /* conceal silent frames */
    1789             :     }
    1790         320 :     c_c = c_c / energy_xy;
    1791             : 
    1792         320 :     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      221109 : 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      221109 :     if ( hCPE == NULL )
    1815             :     {
    1816           0 :         return;
    1817             :     }
    1818             : 
    1819             :     /* initialization */
    1820      221109 :     sts = hCPE->hCoreCoder;
    1821             : 
    1822      221109 :     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           1 :         ilen = output_frame / 2;
    1826             : 
    1827             :         /* correlation length */
    1828           1 :         corr_len = output_frame / 20;
    1829           1 :         subsampl = 4;
    1830             : 
    1831           3 :         for ( ch = 0; ch < hCPE->nchan_out; ch++ )
    1832             :         {
    1833             :             /* core to external sampling frequency ratio */
    1834           2 :             fac_fs = (float) output_frame / sts[ch]->L_frame;
    1835             : 
    1836             :             /* Find minimum and maximum pitch*/
    1837           2 :             ipit_min = minimum( sts[ch]->old_pitch_buf + 4, 4, &flen ) + 4;
    1838           2 :             ipit_max = maximum( sts[ch]->old_pitch_buf + 4, 4, &flen ) + 4;
    1839           2 :             ipit_min = (short) ( sts[ch]->old_pitch_buf[ipit_min] * fac_fs + 0.5f );
    1840           2 :             ipit_max = (short) ( sts[ch]->old_pitch_buf[ipit_max] * fac_fs + 0.5f );
    1841             : 
    1842           2 :             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           1 :                 continue;
    1848             :             }
    1849             : 
    1850           1 :             lsearch = ipit_max - ipit_min + corr_len;
    1851           1 :             lsearch = min( lsearch, output_frame / 4 );
    1852             : 
    1853             :             /* ptr init for search of the best correlation in the past frame */
    1854           1 :             ptE = hCPE->prev_synth_chs[ch] + output_frame - ipit_max - corr_len;
    1855             : 
    1856           1 :             idiff = 0;
    1857           1 :             fmaxcorr = -1.0f;
    1858         161 :             for ( i = 0; i < lsearch; i++ )
    1859             :             {
    1860         160 :                 ftmp_corr = ncross_corr_self( ptE, i, ipit_max, corr_len, subsampl );
    1861         160 :                 if ( ftmp_corr > fmaxcorr )
    1862             :                 {
    1863          14 :                     idiff = i;
    1864             :                 }
    1865         160 :                 fmaxcorr = max( fmaxcorr, ftmp_corr );
    1866             :             }
    1867             : 
    1868           1 :             ipit = ipit_max - idiff;
    1869             : 
    1870           1 :             ptO = tmp_out + output_frame;
    1871             : 
    1872             :             /* If the correlation is too low, don't use the prediction */
    1873           1 :             if ( fmaxcorr < DFT2TD_CORR_THRESH )
    1874             :             {
    1875           0 :                 mvr2r( &output[ch][0], ptO, ilen );
    1876             :             }
    1877             :             else
    1878             :             {
    1879           1 :                 mvr2r( hCPE->prev_synth_chs[ch], tmp_out, output_frame );
    1880           1 :                 pt1 = tmp_out + output_frame - ipit;
    1881         321 :                 for ( i = 0; i < ilen; i++ )
    1882             :                 {
    1883         320 :                     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           1 :             ptO2 = tmp_out2 + output_frame - ilen;
    1892           1 :             set_f( tmp_out2 + output_frame - ilen, 0.0f, ilen );
    1893         641 :             for ( i = 0; i < output_frame; i++ )
    1894             :             {
    1895         640 :                 tmp_out2[i] = output[ch][output_frame - 1 - i];
    1896             :             }
    1897             :             /* ptr init for search of the best correlation of the current frame */
    1898           1 :             ptE = ptO2 - ( ipit_max + corr_len );
    1899             : 
    1900           1 :             idiff = 0;
    1901           1 :             fmaxcorr = -1.0f;
    1902         161 :             for ( i = 0; i < lsearch; i++ )
    1903             :             {
    1904         160 :                 ftmp_corr = ncross_corr_self( ptE, i, ipit_max, corr_len, subsampl );
    1905         160 :                 if ( ftmp_corr > fmaxcorr )
    1906             :                 {
    1907           9 :                     idiff = i;
    1908             :                 }
    1909         160 :                 fmaxcorr = max( fmaxcorr, ftmp_corr );
    1910             :             }
    1911             : 
    1912           1 :             ipit = ipit_max - idiff;
    1913             : 
    1914             :             /* If the correlation is too low, don't use the prediction */
    1915           1 :             if ( fmaxcorr > DFT2TD_CORR_THRESH )
    1916             :             {
    1917           1 :                 pt1 = tmp_out2 + output_frame - ilen - ipit;
    1918         321 :                 for ( i = 0; i < ilen; i++ )
    1919             :                 {
    1920         320 :                     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           1 :             flen = 1.0f / ilen;
    1929             : 
    1930           5 :             for ( i = 0; i < 4; i++ )
    1931             :             {
    1932           4 :                 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         313 :             for ( ; i < ilen - 4; i++ )
    1935             :             {
    1936         312 :                 output[ch][i] = ( ptO[i] * ( ilen - i ) + tmp_out2[output_frame - 1 - i] * i ) * flen;
    1937             :             }
    1938           1 :             j = 0;
    1939           5 :             for ( ; i < ilen; i++ )
    1940             :             {
    1941           4 :                 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           4 :                 j++;
    1943             :             }
    1944             :         }
    1945             :     }
    1946      221108 :     else if ( hCPE->element_mode == IVAS_CPE_DFT )
    1947             :     {
    1948             :         /* Updates */
    1949      214853 :         for ( ch = 0; ch < hCPE->nchan_out; ch++ )
    1950             :         {
    1951      128877 :             mvr2r( output[ch], hCPE->prev_synth_chs[ch], output_frame );
    1952             :         }
    1953             :     }
    1954             : 
    1955      221109 :     return;
    1956             : }

Generated by: LCOV version 1.14