LCOV - code coverage report
Current view: top level - lib_dec - fd_cng_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 867217ee32c8e8cd2cf5aae69e60c58e00160b49 Lines: 777 887 87.6 %
Date: 2025-12-15 05:59:49 Functions: 18 18 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             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include "ivas_cnst.h"
      38             : #include "ivas_rom_com.h"
      39             : #include <assert.h>
      40             : #include <stdint.h>
      41             : #include "options.h"
      42             : #ifdef DEBUGGING
      43             : #include "debug.h"
      44             : #endif
      45             : #include <math.h>
      46             : #include "prot.h"
      47             : #include "ivas_prot.h"
      48             : #include "rom_com.h"
      49             : #include "wmc_auto.h"
      50             : #include "ivas_prot.h"
      51             : #include "ivas_rom_dec.h"
      52             : 
      53             : /*-------------------------------------------------------------------
      54             :  * Local constants
      55             :  *-------------------------------------------------------------------*/
      56             : 
      57             : #define DELTA_MASKING_NOISE        1e-20f
      58             : #define CNA_ACT_DN_LARGE_PARTITION 50  /* index of the first larger partition */
      59             : #define ST_PERIODOG_FACT           0.9 /* short-term filter factor for periodogram */
      60             : #define CNA_ACT_DN_FACT            0.7 /* downward updating factor for CNA during active frames */
      61             : #define FIRST_CNA_NOISE_UPD_FRAMES 5   /* minimum number of CN initialization frames */
      62             : 
      63             : 
      64             : /*-------------------------------------------------------------------
      65             :  * Local fucntions declarations
      66             :  *-------------------------------------------------------------------*/
      67             : 
      68             : static void perform_noise_estimation_dec( const float *timeDomainInput, float *power_spectrum, HANDLE_FD_CNG_DEC hFdCngDec, const int16_t element_mode, const int16_t bwidth, const int16_t L_frame, const int16_t last_L_frame, const int32_t last_core_brate, const int16_t VAD );
      69             : 
      70             : 
      71             : /*-------------------------------------------------------------------
      72             :  * createFdCngDec()
      73             :  *
      74             :  * Create an instance of type FD_CNG
      75             :  *-------------------------------------------------------------------*/
      76             : 
      77      295664 : ivas_error createFdCngDec(
      78             :     HANDLE_FD_CNG_DEC *hFdCngDec )
      79             : {
      80             :     HANDLE_FD_CNG_DEC hs;
      81             :     ivas_error error;
      82      295664 :     error = IVAS_ERR_OK;
      83             : 
      84             :     /* Set output to NULL in case of errors and early return */
      85      295664 :     *hFdCngDec = NULL;
      86             : 
      87             :     /* Allocate memory */
      88      295664 :     if ( ( hs = (HANDLE_FD_CNG_DEC) malloc( sizeof( FD_CNG_DEC ) ) ) == NULL )
      89             :     {
      90           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for FD CNG DEC structure" );
      91             :     }
      92             : 
      93      295664 :     if ( ( error = createFdCngCom( &( hs->hFdCngCom ) ) ) != IVAS_ERR_OK )
      94             :     {
      95           0 :         return error;
      96             :     }
      97             : 
      98      295664 :     *hFdCngDec = hs;
      99             : 
     100      295664 :     return error;
     101             : }
     102             : 
     103             : 
     104             : /*-------------------------------------------------------------------
     105             :  * initFdCngDec()
     106             :  *
     107             :  * Initialize an instance of type FD_CNG
     108             :  *-------------------------------------------------------------------*/
     109             : 
     110      295664 : void initFdCngDec(
     111             :     DEC_CORE_HANDLE st /* i/o: decoder state structure     */
     112             : )
     113             : {
     114             :     HANDLE_FD_CNG_DEC hFdCngDec;
     115             : 
     116      295664 :     hFdCngDec = st->hFdCngDec;
     117             : 
     118             :     /* Initialize common */
     119      295664 :     initFdCngCom( hFdCngDec->hFdCngCom, st->cldfbSyn->scale );
     120             : 
     121             :     /* Set some counters and flags */
     122      295664 :     hFdCngDec->flag_dtx_mode = 0;
     123      295664 :     hFdCngDec->lp_noise = -20.f;
     124      295664 :     hFdCngDec->lp_speech = 25.f;
     125             : 
     126             :     /* Initialize noise estimation algorithm */
     127      295664 :     set_f( hFdCngDec->bandNoiseShape, 0.0f, FFTLEN2 );
     128      295664 :     set_f( hFdCngDec->partNoiseShape, 0.0f, NPART );
     129      295664 :     set_f( hFdCngDec->msPeriodog, 0.0f, NPART_SHAPING );
     130      295664 :     set_f( hFdCngDec->msAlpha, 0.0f, NPART_SHAPING );
     131      295664 :     set_f( hFdCngDec->msBminWin, 0.0f, NPART_SHAPING );
     132      295664 :     set_f( hFdCngDec->msBminSubWin, 0.0f, NPART_SHAPING );
     133      295664 :     set_f( hFdCngDec->msPsd, 0.0f, NPART_SHAPING );
     134      295664 :     set_f( hFdCngDec->msNoiseFloor, 0.0f, NPART_SHAPING );
     135      295664 :     set_f( hFdCngDec->msNoiseEst, 0.0f, NPART_SHAPING );
     136      295664 :     set_f( hFdCngDec->msMinBuf, FLT_MAX, MSNUMSUBFR * NPART_SHAPING );
     137      295664 :     set_f( hFdCngDec->msCurrentMin, FLT_MAX, NPART_SHAPING );
     138      295664 :     set_f( hFdCngDec->msCurrentMinOut, FLT_MAX, NPART_SHAPING );
     139      295664 :     set_f( hFdCngDec->msCurrentMinSubWindow, FLT_MAX, NPART_SHAPING );
     140      295664 :     set_s( hFdCngDec->msLocalMinFlag, 0, NPART_SHAPING );
     141      295664 :     set_s( hFdCngDec->msNewMinFlag, 0, NPART_SHAPING );
     142      295664 :     set_f( hFdCngDec->msPsdFirstMoment, 0.0f, NPART_SHAPING );
     143      295664 :     set_f( hFdCngDec->msPsdSecondMoment, 0.0f, NPART_SHAPING );
     144      295664 :     hFdCngDec->msPeriodogBufPtr = 0;
     145      295664 :     set_f( hFdCngDec->msPeriodogBuf, 0.0f, MSBUFLEN * NPART_SHAPING );
     146      295664 :     set_f( hFdCngDec->msLogPeriodog, 0.0f, NPART_SHAPING );
     147      295664 :     set_f( hFdCngDec->msLogNoiseEst, 0.0f, NPART_SHAPING );
     148      295664 :     set_f( hFdCngDec->psize_shaping, 0.0f, NPART_SHAPING );
     149      295664 :     hFdCngDec->nFFTpart_shaping = 0;
     150             : 
     151      295664 :     set_f( hFdCngDec->hFdCngCom->sidNoiseEstLp, 0.0f, NPART );
     152             : 
     153      295664 :     set_f( hFdCngDec->smoothed_psd, 0.0f, L_FRAME16k );
     154      295664 :     set_f( hFdCngDec->msPeriodog_ST, 0.0f, NPART_SHAPING );
     155             : 
     156      295664 :     hFdCngDec->ms_last_inactive_bwidth = NB;
     157      295664 :     hFdCngDec->ms_cnt_bw_up = 0;
     158             : 
     159      295664 :     hFdCngDec->cna_LR_LT = 0.5f;
     160      295664 :     hFdCngDec->cna_ILD_LT = 0.0f;
     161      295664 :     hFdCngDec->first_cna_noise_updated = 0;
     162      295664 :     hFdCngDec->first_cna_noise_update_cnt = 0;
     163      295664 :     hFdCngDec->cna_nbands = CNA_INIT_NBANDS;
     164      295664 :     mvs2s( cna_init_bands, hFdCngDec->cna_band_limits, CNA_INIT_NBANDS + 1 );
     165      295664 :     hFdCngDec->cna_act_fact = 1.0f;
     166      295664 :     hFdCngDec->cna_rescale_fact = 0.0f;
     167      295664 :     hFdCngDec->cna_seed = 5687;
     168      295664 :     set_zero( hFdCngDec->cna_cm, STEREO_DFT_BAND_MAX );
     169      295664 :     set_zero( hFdCngDec->cna_g_state, STEREO_DFT_BAND_MAX );
     170             : 
     171      295664 :     st->CNG_mode = -1;
     172      295664 :     mvr2r( st->lsp_old, st->lspCNG, M );
     173             : 
     174      295664 :     return;
     175             : }
     176             : 
     177             : 
     178             : /*-------------------------------------------------------------------
     179             :  * configureFdCngDec()
     180             :  *
     181             :  * Configure an instance of type FD_CNG
     182             :  *-------------------------------------------------------------------*/
     183             : 
     184     8842636 : void configureFdCngDec(
     185             :     HANDLE_FD_CNG_DEC hFdCngDec, /* i/o: Contains the variables related to the FD-based CNG process */
     186             :     const int16_t bwidth,
     187             :     const int32_t total_brate,
     188             :     const int16_t L_frame,
     189             :     const int16_t last_L_frame,
     190             :     const int16_t element_mode )
     191             : {
     192             :     int16_t j, stopBandFR;
     193     8842636 :     HANDLE_FD_CNG_COM hsCom = hFdCngDec->hFdCngCom;
     194             : 
     195     8842636 :     hsCom->CngBandwidth = bwidth;
     196     8842636 :     if ( hsCom->CngBandwidth == FB )
     197             :     {
     198     5235713 :         hsCom->CngBandwidth = SWB;
     199             :     }
     200     8842636 :     if ( total_brate != FRAME_NO_DATA && total_brate != SID_2k40 )
     201             :     {
     202     8841701 :         hsCom->CngBitrate = total_brate;
     203             :     }
     204         935 :     else if ( hsCom->CngBitrate == -1 )
     205             :     {
     206             :         /* set minimum active CBR bitrate if CngBitrate is uninitialized */
     207           0 :         if ( element_mode > EVS_MONO )
     208             :         {
     209           0 :             hsCom->CngBitrate = IVAS_13k2;
     210             :         }
     211             :         else
     212             :         {
     213           0 :             hsCom->CngBitrate = ACELP_7k20;
     214             :         }
     215             :     }
     216             : 
     217             :     /* FD-CNG config for MDCT-Stereo is always the same (since for > 48 kbps only) */
     218             :     /* This may need adjustment in the future if 2TC DTX for some mode uses MDCT-Stereo DTX for lower bitrates too */
     219     8842636 :     if ( element_mode == IVAS_CPE_MDCT )
     220             :     {
     221     7429907 :         hsCom->CngBitrate = IVAS_48k;
     222             :     }
     223     8842636 :     hsCom->numSlots = 16;
     224             : 
     225             :     /* NB configuration */
     226     8842636 :     if ( bwidth == NB )
     227             :     {
     228          38 :         hsCom->FdCngSetup = FdCngSetup_nb;
     229          38 :         hsCom->numCoreBands = 16;
     230          38 :         hsCom->regularStopBand = 16;
     231             :     }
     232             : 
     233             :     /* WB configuration */
     234     8842598 :     else if ( bwidth == WB )
     235             :     {
     236             :         /* FFT 6.4kHz, no CLDFB */
     237       94357 :         if ( hsCom->CngBitrate <= ACELP_8k00 && L_frame == L_FRAME )
     238             :         {
     239        2710 :             hsCom->FdCngSetup = FdCngSetup_wb1;
     240        2710 :             hsCom->numCoreBands = 16;
     241        2710 :             hsCom->regularStopBand = 16;
     242             :         }
     243             :         /* FFT 6.4kHz, CLDFB 8.0kHz */
     244       91647 :         else if ( hsCom->CngBitrate <= ACELP_13k20 || L_frame == L_FRAME )
     245             :         {
     246       14259 :             hsCom->FdCngSetup = FdCngSetup_wb2;
     247       14259 :             hsCom->numCoreBands = 16;
     248       14259 :             hsCom->regularStopBand = 20;
     249       14259 :             if ( L_frame == L_FRAME16k )
     250             :             {
     251          24 :                 hsCom->FdCngSetup = FdCngSetup_wb2;
     252          24 :                 hsCom->numCoreBands = 20;
     253          24 :                 hsCom->regularStopBand = 20;
     254          24 :                 hsCom->FdCngSetup.fftlen = 640;
     255          24 :                 hsCom->FdCngSetup.stopFFTbin = 256;
     256             :             }
     257             :         }
     258             :         /* FFT 8.0kHz, no CLDFB */
     259             :         else
     260             :         {
     261       77388 :             hsCom->FdCngSetup = FdCngSetup_wb3;
     262       77388 :             hsCom->numCoreBands = 20;
     263       77388 :             hsCom->regularStopBand = 20;
     264             :         }
     265             :     }
     266             : 
     267             :     /* SWB/FB configuration */
     268             :     else
     269             :     {
     270             :         /* FFT 6.4kHz, CLDFB 14kHz */
     271     8748241 :         if ( L_frame == L_FRAME )
     272             :         {
     273       48783 :             hsCom->FdCngSetup = FdCngSetup_swb1;
     274       48783 :             hsCom->numCoreBands = 16;
     275       48783 :             hsCom->regularStopBand = 35;
     276             :         }
     277             :         /* FFT 8.0kHz, CLDFB 16kHz */
     278             :         else
     279             :         {
     280     8699458 :             hsCom->FdCngSetup = FdCngSetup_swb2;
     281     8699458 :             hsCom->numCoreBands = 20;
     282     8699458 :             hsCom->regularStopBand = 40;
     283     8699458 :             if ( last_L_frame == L_FRAME && element_mode == IVAS_CPE_DFT )
     284             :             {
     285        1825 :                 hsCom->regularStopBand = 35;
     286             :             }
     287             :         }
     288             :     }
     289             : 
     290             : 
     291     8842636 :     hsCom->fftlen = hsCom->FdCngSetup.fftlen;
     292     8842636 :     hsCom->stopFFTbin = hsCom->FdCngSetup.stopFFTbin;
     293             : 
     294             :     /* Configure the SID quantizer and the Comfort Noise Generator */
     295             : 
     296     8842636 :     hsCom->startBand = 2;
     297     8842636 :     hsCom->stopBand = hsCom->FdCngSetup.sidPartitions[hsCom->FdCngSetup.numPartitions - 1] + 1;
     298     8842636 :     initPartitions( hsCom->FdCngSetup.sidPartitions, hsCom->FdCngSetup.numPartitions, hsCom->startBand, hsCom->stopBand, hsCom->part, &hsCom->npart, hsCom->midband, hsCom->psize, hsCom->psize_inv, 0 );
     299     8842636 :     if ( hsCom->stopFFTbin == 160 )
     300             :     {
     301          38 :         hsCom->nFFTpart = 17;
     302             :     }
     303     8842598 :     else if ( hsCom->stopFFTbin == 256 )
     304             :     {
     305       65752 :         hsCom->nFFTpart = 20;
     306             :     }
     307             :     else
     308             :     {
     309     8776846 :         hsCom->nFFTpart = 21;
     310             :     }
     311     8842636 :     hsCom->nCLDFBpart = hsCom->npart - hsCom->nFFTpart;
     312    35150401 :     for ( j = 0; j < hsCom->nCLDFBpart; j++ )
     313             :     {
     314    26307765 :         hsCom->CLDFBpart[j] = hsCom->part[j + hsCom->nFFTpart] - ( hsCom->stopFFTbin - hsCom->startBand );
     315    26307765 :         hsCom->CLDFBpsize_inv[j] = hsCom->psize_inv[j + hsCom->nFFTpart];
     316             :     }
     317             : 
     318     8842636 :     stopBandFR = (int16_t) floor( 1000.f /*Hz*/ / 25.f /*Hz/Bin*/ );
     319     8842636 :     if ( stopBandFR > hsCom->stopFFTbin )
     320             :     {
     321           0 :         stopBandFR = hsCom->stopFFTbin;
     322             :     }
     323     8842636 :     initPartitions( hsCom->FdCngSetup.shapingPartitions, hsCom->FdCngSetup.numShapingPartitions, hsCom->startBand, hsCom->stopFFTbin, hFdCngDec->part_shaping, &hFdCngDec->npart_shaping, hFdCngDec->midband_shaping, hFdCngDec->psize_shaping, hFdCngDec->psize_inv_shaping, stopBandFR );
     324             : 
     325     8842636 :     hFdCngDec->nFFTpart_shaping = hFdCngDec->npart_shaping;
     326             : 
     327     8842636 :     switch ( hsCom->fftlen )
     328             :     {
     329       65766 :         case 512:
     330       65766 :             hsCom->fftSineTab = NULL;
     331       65766 :             hsCom->olapWinAna = olapWinAna512;
     332       65766 :             hsCom->olapWinSyn = olapWinSyn256;
     333       65766 :             break;
     334     8776870 :         case 640:
     335     8776870 :             hsCom->fftSineTab = fftSineTab640;
     336     8776870 :             hsCom->olapWinAna = olapWinAna640;
     337     8776870 :             hsCom->olapWinSyn = olapWinSyn320;
     338     8776870 :             break;
     339           0 :         default:
     340           0 :             assert( !"Unsupported FFT length for FD-based CNG" );
     341             :             break;
     342             :     }
     343     8842636 :     hsCom->frameSize = hsCom->fftlen >> 1;
     344             : 
     345     8842636 :     return;
     346             : }
     347             : 
     348             : 
     349             : /*-------------------------------------------------------------------
     350             :  * deleteFdCngDec()
     351             :  *
     352             :  * Delete the instance of type FD_CNG
     353             :  *-------------------------------------------------------------------*/
     354             : 
     355     2644581 : void deleteFdCngDec(
     356             :     HANDLE_FD_CNG_DEC *hFdCngDec )
     357             : {
     358     2644581 :     HANDLE_FD_CNG_DEC hsDec = *hFdCngDec;
     359             : 
     360     2644581 :     if ( hsDec != NULL )
     361             :     {
     362      295664 :         deleteFdCngCom( &( hsDec->hFdCngCom ) );
     363      295664 :         free( hsDec );
     364      295664 :         *hFdCngDec = NULL;
     365             :     }
     366             : 
     367     2644581 :     return;
     368             : }
     369             : 
     370             : 
     371             : /*-------------------------------------------------------------------
     372             :  * ApplyFdCng()
     373             :  *
     374             :  * Apply the CLDFB-based CNG at the decoder
     375             :  *-------------------------------------------------------------------*/
     376             : 
     377     3982028 : void ApplyFdCng(
     378             :     float *timeDomainInput,
     379             :     float *powerSpectrum,
     380             :     float **realBuffer, /* i/o: Real part of the buffer */
     381             :     float **imagBuffer, /* i/o: Imaginary part of the buffer */
     382             :     Decoder_State *st,
     383             :     const int16_t concealWholeFrame,
     384             :     const int16_t is_music )
     385             : {
     386     3982028 :     HANDLE_FD_CNG_DEC hFdCngDec = st->hFdCngDec;
     387     3982028 :     HANDLE_FD_CNG_COM hFdCngCom = hFdCngDec->hFdCngCom;
     388     3982028 :     float *cngNoiseLevel = hFdCngCom->cngNoiseLevel;
     389     3982028 :     float *sidNoiseEst = hFdCngCom->sidNoiseEst;
     390             :     int16_t j, k;
     391             :     float factor;
     392             :     float lsp_cng[M];
     393             :     int16_t L_frame, last_L_frame;
     394             :     int32_t sr_core;
     395             : 
     396     3982028 :     push_wmops( "ApplyFdCng" );
     397             : 
     398             :     /* limit L_frame and core Fs values for MDCT-Stereo modes which can have higher core sampling than 16kHz, but use a downsampled buffer */
     399     3982028 :     L_frame = min( st->L_frame, L_FRAME16k );
     400     3982028 :     last_L_frame = min( st->last_L_frame, L_FRAME16k );
     401     3982028 :     sr_core = min( st->sr_core, INT_FS_16k );
     402             : 
     403     3982028 :     if ( hFdCngCom->frame_type_previous == ACTIVE_FRAME )
     404             :     {
     405     3932425 :         hFdCngCom->inactive_frame_counter = 0;
     406             :     }
     407             : 
     408     3982028 :     if ( st->element_mode == IVAS_CPE_TD )
     409             :     {
     410       16026 :         hFdCngDec->flag_dtx_mode = hFdCngDec->flag_dtx_mode || st->first_CNG;
     411             :     }
     412             : 
     413     3982028 :     switch ( st->m_frame_type )
     414             :     {
     415             : 
     416     3937348 :         case ACTIVE_FRAME:
     417             :             /**************************
     418             :              * ACTIVE_FRAME at DECODER *
     419             :              **************************/
     420             : 
     421     3937348 :             hFdCngCom->inactive_frame_counter = 0;
     422     3937348 :             hFdCngCom->sid_frame_counter = 0;
     423             :             /* set noise estimation inactive during concealment, as no update with noise generated by concealment should be performed. */
     424             :             /* set noise estimation inactive when we have bit errors, as no update with noise generated by corrupt frame (biterror) should be performed. */
     425     3937348 :             if ( concealWholeFrame == 0 &&
     426     3501849 :                  ( timeDomainInput == NULL ||
     427     3501849 :                    ( *timeDomainInput<FLT_MAX && * timeDomainInput>( -FLT_MAX ) &&
     428     3501849 :                      *( timeDomainInput + hFdCngCom->frameSize - 1 ) < FLT_MAX &&
     429     3501849 :                      *( timeDomainInput + hFdCngCom->frameSize - 1 ) > ( -FLT_MAX ) ) ) &&
     430     3530303 :                  ( ( ( ( st->element_mode != IVAS_CPE_TD && st->element_mode != IVAS_CPE_DFT && hFdCngDec->flag_dtx_mode ) || !st->VAD || ( st->ini_frame < 100 && st->is_ism_format ) ) &&
     431     1644760 :                      !( st->cng_type == LP_CNG && hFdCngDec->flag_dtx_mode ) && ( is_music == 0 ) ) ||
     432     1916425 :                    ( st->element_mode == IVAS_CPE_TD ) ) &&
     433     1629529 :                  ( !st->BER_detect ) )
     434             :             {
     435             :                 /* Perform noise estimation at the decoder */
     436     1629529 :                 perform_noise_estimation_dec( timeDomainInput, powerSpectrum, hFdCngDec, st->element_mode, st->bwidth, L_frame, last_L_frame, st->last_core_brate, st->VAD );
     437             : 
     438     1629529 :                 if ( st->element_mode != IVAS_CPE_TD && st->element_mode != IVAS_CPE_DFT )
     439             :                 {
     440             :                     /* Update the shaping parameters */
     441     1604040 :                     scalebands( hFdCngDec->msNoiseEst, hFdCngDec->part_shaping, hFdCngDec->nFFTpart_shaping, hFdCngDec->midband_shaping, hFdCngDec->nFFTpart_shaping, hFdCngCom->stopFFTbin - hFdCngCom->startBand, hFdCngDec->bandNoiseShape, 1 );
     442             :                 }
     443             : 
     444             :                 /* Update CNG levels */
     445     1629529 :                 if ( hFdCngDec->flag_dtx_mode && st->cng_type == FD_CNG )
     446             :                 {
     447       13025 :                     bandcombinepow( hFdCngDec->bandNoiseShape, hFdCngCom->stopFFTbin - hFdCngCom->startBand, hFdCngCom->part, hFdCngCom->nFFTpart, hFdCngCom->psize_inv, hFdCngDec->partNoiseShape ); /* This needs to be done only once per inactive phase */
     448             : 
     449       13025 :                     j = 0;
     450      280775 :                     for ( k = 0; k < hFdCngCom->nFFTpart; k++ )
     451             :                     {
     452      267750 :                         factor = ( hFdCngCom->sidNoiseEst[k] + DELTA ) / ( hFdCngDec->partNoiseShape[k] + DELTA );
     453     4040100 :                         for ( ; j <= hFdCngCom->part[k]; j++ )
     454             :                         {
     455     3772350 :                             cngNoiseLevel[j] = hFdCngDec->bandNoiseShape[j] * factor;
     456             :                         }
     457             :                     }
     458             :                 }
     459             :                 else
     460             :                 {
     461             :                     /* This sets the new CNG levels until a SID update overwrites it */
     462     1616504 :                     if ( !( st->element_mode == IVAS_CPE_TD ) || ( st->element_mode == IVAS_CPE_TD && !hFdCngDec->flag_dtx_mode && !st->VAD ) )
     463             :                     {
     464     1605713 :                         mvr2r( hFdCngDec->bandNoiseShape, cngNoiseLevel, hFdCngCom->stopFFTbin - hFdCngCom->startBand ); /* This sets the new CNG levels until a SID update overwrites it */
     465             :                     }
     466             :                 }
     467             : 
     468     1629529 :                 if ( st->element_mode == IVAS_CPE_MDCT && timeDomainInput == NULL )
     469             :                 {
     470       28454 :                     st->hTcxDec->CngLevelBackgroundTrace_bfi = sqrtf( sum_f( cngNoiseLevel, hFdCngCom->stopFFTbin - hFdCngCom->startBand ) / NORM_MDCT_FACTOR );
     471             :                 }
     472             :                 else
     473             :                 {
     474     1601075 :                     st->hTcxDec->CngLevelBackgroundTrace_bfi = (float) sqrt( ( sum_f( cngNoiseLevel, hFdCngCom->stopFFTbin - hFdCngCom->startBand ) / 2 * hFdCngCom->fftlen ) / L_frame );
     475             :                 }
     476     1629529 :                 st->cngTDLevel = (float) sqrt( ( sum_f( cngNoiseLevel, hFdCngCom->stopFFTbin - hFdCngCom->startBand ) / 2 * hFdCngCom->fftlen ) / st->L_frame );
     477             :             }
     478     2307819 :             else if ( st->element_mode == IVAS_CPE_TD || st->element_mode == IVAS_CPE_DFT )
     479             :             {
     480      516886 :                 if ( hFdCngCom->active_frame_counter > 0 )
     481             :                 {
     482             :                     /* Perform noise estimation in active frames in the decoder for downward updates */
     483      509543 :                     perform_noise_estimation_dec( timeDomainInput, powerSpectrum, hFdCngDec, st->element_mode, st->bwidth, L_frame, last_L_frame, st->last_core_brate, st->VAD );
     484             :                 }
     485             :             }
     486             : 
     487     3937348 :             if ( ( concealWholeFrame == 1 ) && ( st->nbLostCmpt == 1 ) )
     488             :             {
     489             :                 /* update lsf cng estimate for concealment. Do that during concealment, in order to avoid addition clean channel complexity*/
     490             : 
     491             :                 /* always set psychParameters for MDCT-Stereo ... */
     492      157426 :                 if ( st->element_mode == IVAS_CPE_MDCT && st->hTonalMDCTConc != NULL )
     493             :                 {
     494       88168 :                     st->hTonalMDCTConc->psychParams = ( st->core == TCX_20_CORE ) ? &st->hTonalMDCTConc->psychParamsTCX20 : &st->hTonalMDCTConc->psychParamsTCX10;
     495             :                 }
     496             : 
     497             :                 /* ... but do actual computations only if sufficient energy in noise shape */
     498      157426 :                 if ( sum_f( cngNoiseLevel + hFdCngCom->startBand, hFdCngCom->stopFFTbin - hFdCngCom->startBand ) > 0.01f )
     499             :                 {
     500       51841 :                     if ( st->element_mode == IVAS_CPE_MDCT && st->core != ACELP_CORE )
     501             :                     {
     502        3231 :                         TonalMdctConceal_whiten_noise_shape( st, L_frame, ON_FIRST_LOST_FRAME );
     503             :                     }
     504       48610 :                     else if ( st->element_mode != IVAS_CPE_MDCT || st->core == ACELP_CORE )
     505             :                     {
     506       48610 :                         lpc_from_spectrum( hFdCngCom, hFdCngCom->startBand, hFdCngCom->stopFFTbin, 0.f );
     507       48610 :                         a2lsp_stab( hFdCngCom->A_cng, lsp_cng, st->lspold_cng );
     508       48610 :                         mvr2r( lsp_cng, st->lspold_cng, M );
     509       48610 :                         lsp2lsf( lsp_cng, st->lsf_cng, M, sr_core );
     510             :                     }
     511       51841 :                     st->plcBackgroundNoiseUpdated = 1;
     512             :                 }
     513             :             }
     514     3937348 :             break;
     515             : 
     516        5924 :         case SID_FRAME:
     517        5924 :             hFdCngDec->flag_dtx_mode = 1;
     518             :             /* FALLTHRU */
     519             : 
     520       44680 :         case ZERO_FRAME:
     521             : 
     522       44680 :             if ( st != NULL && st->cng_type == LP_CNG )
     523             :             {
     524             :                 /* Perform noise estimation on inactive phase at the decoder */
     525        3450 :                 perform_noise_estimation_dec( timeDomainInput, powerSpectrum, hFdCngDec, st->element_mode, st->bwidth, L_frame, last_L_frame, st->last_core_brate, st->VAD );
     526             : 
     527        3450 :                 if ( st->element_mode != IVAS_CPE_TD && st->element_mode != IVAS_CPE_DFT )
     528             :                 {
     529             :                     /* Update the shaping parameters */
     530           0 :                     scalebands( hFdCngDec->msNoiseEst, hFdCngDec->part_shaping, hFdCngDec->nFFTpart_shaping, hFdCngDec->midband_shaping, hFdCngDec->nFFTpart_shaping, hFdCngCom->stopFFTbin - hFdCngCom->startBand, hFdCngDec->bandNoiseShape, 1 );
     531             :                 }
     532             : 
     533             :                 /* This sets the new CNG levels until a SID update overwrites it */
     534        3450 :                 mvr2r( hFdCngDec->bandNoiseShape, cngNoiseLevel, hFdCngCom->stopFFTbin - hFdCngCom->startBand ); /* This sets the new CNG levels until a SID update overwrites it */
     535             : 
     536        3450 :                 st->cngTDLevel = (float) sqrt( ( sum_f( cngNoiseLevel, hFdCngCom->stopFFTbin - hFdCngCom->startBand ) / 2 * hFdCngCom->fftlen ) / L_frame );
     537        3450 :                 break;
     538             :             }
     539             : 
     540       41230 :             hFdCngCom->inactive_frame_counter++;
     541             : 
     542             :             /*************************************
     543             :              * SID_FRAME or ZERO_FRAME at DECODER *
     544             :              *************************************/
     545             : 
     546             :             /* Detect first non-active frame */
     547       41230 :             if ( hFdCngCom->inactive_frame_counter == 1 )
     548             :             {
     549             :                 /* Compute the fine spectral structure of the comfort noise shape using the decoder-side noise estimates */
     550        1104 :                 bandcombinepow( hFdCngDec->bandNoiseShape, hFdCngCom->stopFFTbin - hFdCngCom->startBand, hFdCngCom->part, hFdCngCom->nFFTpart, hFdCngCom->psize_inv, hFdCngDec->partNoiseShape );
     551             : 
     552        1104 :                 if ( st->element_mode == IVAS_CPE_DFT )
     553             :                 {
     554         882 :                     mvr2r( st->hFdCngDec->hFdCngCom->sidNoiseEst, st->hFdCngDec->hFdCngCom->sidNoiseEstLp, NPART );
     555             :                 }
     556             :             }
     557             : 
     558       41230 :             if ( st->m_frame_type == SID_FRAME )
     559             :             {
     560        5279 :                 if ( hFdCngCom->msFrCnt_init_counter < hFdCngCom->msFrCnt_init_thresh )
     561             :                 {
     562             :                     /* At initialization, interpolate the bin/band-wise levels from the partition levels */
     563          95 :                     scalebands( sidNoiseEst, hFdCngCom->part, hFdCngCom->npart, hFdCngCom->midband, hFdCngCom->nFFTpart, hFdCngCom->stopBand - hFdCngCom->startBand, cngNoiseLevel, 1 );
     564             :                 }
     565             :                 else
     566             :                 {
     567        5184 :                     if ( st->element_mode == IVAS_CPE_DFT )
     568             :                     {
     569        4521 :                         sidNoiseEst = hFdCngCom->sidNoiseEstLp;
     570             :                     }
     571             : 
     572             :                     /* Interpolate the CLDFB band levels from the SID (partition) levels */
     573        5184 :                     if ( hFdCngCom->regularStopBand > hFdCngCom->numCoreBands )
     574             :                     {
     575        4500 :                         scalebands( sidNoiseEst, hFdCngCom->part, hFdCngCom->npart, hFdCngCom->midband, hFdCngCom->nFFTpart, hFdCngCom->stopBand - hFdCngCom->startBand, cngNoiseLevel, 0 );
     576             :                     }
     577             : 
     578             : 
     579             :                     /* Shape the SID noise levels in each FFT bin */
     580        5184 :                     j = 0;
     581      111942 :                     for ( k = 0; k < hFdCngCom->nFFTpart; k++ )
     582             :                     {
     583      106758 :                         factor = ( sidNoiseEst[k] + DELTA ) / ( hFdCngDec->partNoiseShape[k] + DELTA );
     584     1620486 :                         for ( ; j <= hFdCngCom->part[k]; j++ )
     585             :                         {
     586     1513728 :                             cngNoiseLevel[j] = hFdCngDec->bandNoiseShape[j] * factor;
     587             :                         }
     588             :                     }
     589             :                 }
     590             :             }
     591       35951 :             else if ( st->element_mode == IVAS_CPE_DFT )
     592             :             {
     593       31390 :                 if ( !( hFdCngCom->msFrCnt_init_counter < hFdCngCom->msFrCnt_init_thresh ) )
     594             :                 {
     595       31390 :                     sidNoiseEst = hFdCngCom->sidNoiseEstLp;
     596       31390 :                     j = 0;
     597      677973 :                     for ( k = 0; k < hFdCngCom->nFFTpart; k++ )
     598             :                     {
     599      646583 :                         factor = ( sidNoiseEst[k] + DELTA ) / ( hFdCngDec->partNoiseShape[k] + DELTA );
     600     9821755 :                         for ( ; j <= hFdCngCom->part[k]; j++ )
     601             :                         {
     602     9175172 :                             cngNoiseLevel[j] = hFdCngDec->bandNoiseShape[j] * factor;
     603             :                         }
     604             :                     }
     605             :                 }
     606             :             }
     607       41230 :             if ( st->codec_mode == MODE2 )
     608             :             {
     609             :                 /* Generate comfort noise during SID or zero frames */
     610           0 :                 generate_comfort_noise_dec( realBuffer, imagBuffer, st, -1 );
     611             :             }
     612             : 
     613       41230 :             break;
     614             : 
     615           0 :         default:
     616           0 :             break;
     617             :     }
     618             : 
     619     3982028 :     pop_wmops();
     620             : 
     621     3982028 :     return;
     622             : }
     623             : 
     624             : 
     625             : /*-------------------------------------------------------------------
     626             :  * perform_noise_estimation_dec()
     627             :  *
     628             :  * Perform noise estimation at the decoder
     629             :  *-------------------------------------------------------------------*/
     630             : 
     631     2142522 : static void perform_noise_estimation_dec(
     632             :     const float *timeDomainInput,
     633             :     float *power_spectrum,
     634             :     HANDLE_FD_CNG_DEC hFdCngDec,   /* i/o: FD_CNG structure containing all buffers and variables */
     635             :     const int16_t element_mode,    /* i  : element mode                                          */
     636             :     const int16_t bwidth,          /* i  : audio bandwidth                                       */
     637             :     const int16_t L_frame,         /* i  : frame length at internal Fs                           */
     638             :     const int16_t last_L_frame,    /* i  : frame length of the last frame at internal Fs         */
     639             :     const int32_t last_core_brate, /* i  : previous frame core bitrate                           */
     640             :     const int16_t VAD              /* i  : VAD flag in the decoder                               */
     641             : )
     642             : {
     643             :     float *ptr_r;
     644             :     float *ptr_i;
     645     2142522 :     int16_t startBand = hFdCngDec->hFdCngCom->startBand;
     646     2142522 :     int16_t stopFFTbin = hFdCngDec->hFdCngCom->stopFFTbin;
     647     2142522 :     float *fftBuffer = hFdCngDec->hFdCngCom->fftBuffer;
     648     2142522 :     float *periodog = hFdCngDec->hFdCngCom->periodog;
     649     2142522 :     float *ptr_per = periodog;
     650     2142522 :     float *msPeriodog = hFdCngDec->msPeriodog;
     651     2142522 :     float *msNoiseEst = hFdCngDec->msNoiseEst;
     652             : 
     653     2142522 :     int16_t *part = hFdCngDec->part_shaping;
     654     2142522 :     int16_t npart = hFdCngDec->npart_shaping;
     655     2142522 :     int16_t nFFTpart = hFdCngDec->nFFTpart_shaping;
     656     2142522 :     float *psize_inv = hFdCngDec->psize_inv_shaping;
     657     2142522 :     float *psize = hFdCngDec->psize_shaping;
     658     2142522 :     float *msLogPeriodog = hFdCngDec->msLogPeriodog;
     659     2142522 :     float *msLogNoiseEst = hFdCngDec->msLogNoiseEst;
     660             :     int16_t i;
     661             :     float enr, enr_tot, enr_tot0, enr_ratio, alpha;
     662             :     int16_t p;
     663             :     float temp, ftemp, delta;
     664             :     float wght;
     665             : 
     666     2142522 :     if ( !( element_mode == IVAS_CPE_MDCT && power_spectrum != NULL ) )
     667             :     {
     668             :         /* Perform STFT analysis */
     669     2114068 :         AnalysisSTFT( timeDomainInput, fftBuffer, hFdCngDec->hFdCngCom );
     670             :     }
     671             : 
     672     2142522 :     if ( element_mode == IVAS_CPE_TD || element_mode == IVAS_CPE_DFT )
     673             :     {
     674             :         /* Calculate periodogram (squared magnitude in each FFT bin) */
     675      538482 :         if ( startBand == 0 )
     676             :         {
     677           0 :             ( *ptr_per ) = fftBuffer[0] * fftBuffer[0];
     678           0 :             ptr_per++;
     679           0 :             ptr_r = fftBuffer + 2;
     680             :         }
     681             :         else
     682             :         {
     683      538482 :             ptr_r = fftBuffer + 2 * startBand;
     684             :         }
     685             : 
     686      538482 :         ptr_i = ptr_r + 1;
     687             : 
     688   152919118 :         for ( ; ptr_per < periodog + stopFFTbin - startBand; ptr_per++ )
     689             :         {
     690   152380636 :             ( *ptr_per ) = ( *ptr_r ) * ( *ptr_r ) + ( *ptr_i ) * ( *ptr_i );
     691   152380636 :             ptr_r += 2;
     692   152380636 :             ptr_i += 2;
     693             :         }
     694             : 
     695             :         /* Rescale to get energy/sample: it should be 2*(1/N)*(2/N), parseval relation with 1/N,*2 for nrg computed till Nyquist only, 2/N as windowed samples correspond to half a frame*/
     696      538482 :         v_multc( periodog, 4.f / (float) ( hFdCngDec->hFdCngCom->fftlen * hFdCngDec->hFdCngCom->fftlen ), periodog, stopFFTbin - startBand );
     697             : 
     698             :         /* Combine bins of power spectrum into partitions */
     699      538482 :         i = 0;
     700    33680519 :         for ( p = 0; p < npart; p++ )
     701             :         {
     702             : 
     703             :             /* calculate mean over all bins in power partition */
     704    33142037 :             temp = 0;
     705   185522673 :             for ( ; i <= part[p]; i++ )
     706             :             {
     707   152380636 :                 temp += periodog[i];
     708             :             }
     709    33142037 :             msPeriodog[p] = temp * psize_inv[p];
     710             :         }
     711             : 
     712             :         /* compensate for the loss of variance - don't do when first noise update is not completed yet due to risk of msPeriodog[p] < 0 */
     713      538482 :         if ( hFdCngDec->first_cna_noise_updated )
     714             :         {
     715      105511 :             i = 0;
     716     6579904 :             for ( p = 0; p < npart; p++ )
     717             :             {
     718             :                 /* calculate variance over all bins in power partition */
     719     6474393 :                 temp = 0;
     720    37580683 :                 for ( ; i <= part[p]; i++ )
     721             :                 {
     722    31106290 :                     delta = periodog[i] - msPeriodog[p];
     723    31106290 :                     temp += delta * delta;
     724             :                 }
     725     6474393 :                 temp *= psize_inv[p];
     726             : 
     727             :                 /* compensate for the loss of variance */
     728     6474393 :                 msPeriodog[p] = (float) ( msPeriodog[p] + sqrt( temp ) * rand_gauss( &ftemp, &hFdCngDec->cna_seed ) );
     729             : 
     730     6474393 :                 if ( msPeriodog[p] < 1e-5f )
     731             :                 {
     732      331026 :                     msPeriodog[p] = 1e-5f;
     733             :                 }
     734             :             }
     735             :         }
     736             : 
     737             :         /* calculate total energy (short-term and long-term) */
     738      538482 :         enr_tot = sum_f( msPeriodog, npart ) + EPSILON;
     739      538482 :         enr_tot0 = sum_f( msNoiseEst, npart ) + EPSILON;
     740             : 
     741             :         /* update short-term periodogram on larger partitions */
     742     6756419 :         for ( p = CNA_ACT_DN_LARGE_PARTITION; p < npart; p++ )
     743             :         {
     744     6217937 :             if ( L_frame != last_L_frame || last_core_brate <= SID_2k40 )
     745             :             {
     746             :                 /* core Fs has changed or last frame was SID/NO_DATA -> re-initialize short-term periodogram */
     747      153759 :                 hFdCngDec->msPeriodog_ST[p] = msPeriodog[p];
     748             :             }
     749             :             else
     750             :             {
     751     6064178 :                 hFdCngDec->msPeriodog_ST[p] = (float) ( ST_PERIODOG_FACT * hFdCngDec->msPeriodog_ST[p] + ( 1 - ST_PERIODOG_FACT ) * msPeriodog[p] );
     752             :             }
     753             :         }
     754             : 
     755             :         /* core Fs has changed -> partitions have changed -> re-calculate long-term periodogram */
     756             :         /* part L_FRAME16k L_FRAME */
     757             :         /* ...                     */
     758             :         /* [55] 146        146     */
     759             :         /* [56] 174        160     */
     760             :         /* [57] 210        174     */
     761             :         /* [58] 254        190     */
     762             :         /* [59] 306        210     */
     763             :         /* [60] 317        230     */
     764             :         /* [61]            253     */
     765             : 
     766      538482 :         if ( last_L_frame == L_FRAME16k && L_frame == L_FRAME )
     767             :         {
     768        7927 :             msNoiseEst[61] = msNoiseEst[58];
     769        7927 :             msNoiseEst[60] = min( msNoiseEst[58], msNoiseEst[57] );
     770        7927 :             msNoiseEst[59] = msNoiseEst[57];
     771        7927 :             msNoiseEst[58] = msNoiseEst[56];
     772        7927 :             msNoiseEst[57] = msNoiseEst[56];
     773        7927 :             msNoiseEst[56] = min( msNoiseEst[56], msNoiseEst[55] );
     774             :         }
     775      530555 :         else if ( last_L_frame == L_FRAME && L_frame == L_FRAME16k )
     776             :         {
     777        1836 :             msNoiseEst[56] = min( msNoiseEst[56], msNoiseEst[57] );
     778        1836 :             msNoiseEst[57] = min( msNoiseEst[58], msNoiseEst[59] );
     779        1836 :             msNoiseEst[58] = min( msNoiseEst[60], msNoiseEst[61] );
     780        1836 :             msNoiseEst[59] = 0.0f;
     781        1836 :             msNoiseEst[60] = 0.0f;
     782        1836 :             msNoiseEst[61] = 0.0f;
     783             : 
     784        1836 :             hFdCngDec->ms_cnt_bw_up = FIRST_CNA_NOISE_UPD_FRAMES;
     785             :         }
     786             : 
     787             :         /* Smooth with IIR filter */
     788      538482 :         if ( !hFdCngDec->first_cna_noise_updated )
     789             :         {
     790      432971 :             if ( !VAD )
     791             :             {
     792             :                 /* background noise update with moving average */
     793        2609 :                 alpha = 1.0f / ( hFdCngDec->first_cna_noise_update_cnt + 1 );
     794      162797 :                 for ( p = 0; p < npart; p++ )
     795             :                 {
     796      160188 :                     msNoiseEst[p] = ( 1 - alpha ) * msNoiseEst[p] + alpha * msPeriodog[p];
     797             :                 }
     798             : 
     799             :                 /* check, if we reached the required number of first CNA noise update frames */
     800        2609 :                 if ( hFdCngDec->first_cna_noise_update_cnt < FIRST_CNA_NOISE_UPD_FRAMES - 1 )
     801             :                 {
     802        2356 :                     hFdCngDec->first_cna_noise_update_cnt++;
     803             :                 }
     804             :                 else
     805             :                 {
     806         253 :                     hFdCngDec->first_cna_noise_updated = 1;
     807         253 :                     if ( hFdCngDec->hFdCngCom->msFrCnt_init_counter == 0 )
     808             :                     {
     809         181 :                         hFdCngDec->hFdCngCom->msFrCnt_init_counter = 1;
     810             :                     }
     811             :                 }
     812             :             }
     813             :             else
     814             :             {
     815      430362 :                 hFdCngDec->first_cna_noise_update_cnt = 0;
     816             :             }
     817             :         }
     818             :         else
     819             :         {
     820      105511 :             hFdCngDec->hFdCngCom->msFrCnt_init_counter = 1;
     821      105511 :             if ( VAD )
     822             :             {
     823             :                 /* no updates during active frames except for significant energy drops */
     824       93842 :                 enr_ratio = enr_tot / enr_tot0;
     825       93842 :                 if ( enr_ratio < 0.5f )
     826             :                 {
     827             :                     /* total energy significantly decreases during active frames -> downward update */
     828        2701 :                     wght = lin_interp( enr_ratio, 0.0f, 0.8f, 0.5f, 0.95f, 1 );
     829      168716 :                     for ( p = 0; p < npart; p++ )
     830             :                     {
     831      166015 :                         if ( msPeriodog[p] < msNoiseEst[p] )
     832             :                         {
     833      122051 :                             msNoiseEst[p] = wght * msNoiseEst[p] + ( 1 - wght ) * msPeriodog[p];
     834             :                         }
     835             :                     }
     836             :                 }
     837             :                 else
     838             :                 {
     839             :                     /* energy significantly decreases in one of the larger partitions during active frames -> downward update */
     840     1125796 :                     for ( p = CNA_ACT_DN_LARGE_PARTITION; p < npart; p++ )
     841             :                     {
     842     1034655 :                         if ( hFdCngDec->msPeriodog_ST[p] < msNoiseEst[p] )
     843             :                         {
     844       44032 :                             msNoiseEst[p] = (float) ( CNA_ACT_DN_FACT * msNoiseEst[p] + ( 1 - CNA_ACT_DN_FACT ) * hFdCngDec->msPeriodog_ST[p] );
     845             :                         }
     846             :                     }
     847             :                 }
     848             :             }
     849             :             else
     850             :             {
     851             : 
     852       11669 :                 if ( bwidth >= WB && hFdCngDec->ms_last_inactive_bwidth == NB )
     853             :                 {
     854             :                     /* bandwidth increased -> set counter for fast initilization  */
     855         175 :                     hFdCngDec->ms_cnt_bw_up = FIRST_CNA_NOISE_UPD_FRAMES;
     856             :                 }
     857       11669 :                 hFdCngDec->ms_last_inactive_bwidth = bwidth;
     858             :                 /* update background noise during inactive frames */
     859       11669 :                 ptr_per = msNoiseEst;
     860      728342 :                 for ( p = 0; p < npart; p++ )
     861             :                 {
     862      716673 :                     enr = msPeriodog[p];
     863      716673 :                     alpha = 0.95f;
     864             :                     /* bandwidth increased -> do fast re-initilization  */
     865      716673 :                     if ( hFdCngDec->ms_cnt_bw_up > 0 && p > 55 )
     866             :                     {
     867        5912 :                         alpha = 1.0f / ( hFdCngDec->ms_cnt_bw_up + 1 );
     868             :                     }
     869      710761 :                     else if ( enr < *ptr_per && part[p] == 1 )
     870             :                     {
     871             :                         /* faster downward update for single-bin partitions */
     872        4001 :                         alpha = 0.8f;
     873             :                     }
     874      706760 :                     else if ( enr > 2.0f * ( *ptr_per ) )
     875             :                     {
     876             :                         /* prevent abrupt upward updates */
     877      216700 :                         enr = 2.0f * ( *ptr_per );
     878             :                     }
     879             : 
     880             :                     /* IIR smoothing */
     881      716673 :                     *ptr_per *= alpha;
     882      716673 :                     *ptr_per += ( 1 - alpha ) * enr;
     883      716673 :                     ptr_per++;
     884             :                 }
     885             : 
     886       11669 :                 if ( hFdCngDec->ms_cnt_bw_up > 0 )
     887             :                 {
     888        1112 :                     hFdCngDec->ms_cnt_bw_up--;
     889             :                 }
     890             :             }
     891             :         }
     892             : 
     893      538482 :         mvr2r( msNoiseEst, hFdCngDec->msPsd, npart );
     894             : 
     895             :         /* Expand partitions into bins of power spectrum */
     896      538482 :         scalebands( msNoiseEst, part, nFFTpart, hFdCngDec->midband_shaping, nFFTpart, stopFFTbin - startBand, hFdCngDec->bandNoiseShape, 1 );
     897             : 
     898      538482 :         mvr2r( hFdCngDec->bandNoiseShape, &hFdCngDec->smoothed_psd[startBand], stopFFTbin - startBand );
     899      538482 :         set_zero( &hFdCngDec->smoothed_psd[stopFFTbin], L_FRAME16k - stopFFTbin );
     900             :     }
     901             :     else
     902             :     {
     903     1604040 :         if ( element_mode == IVAS_CPE_MDCT && power_spectrum != NULL )
     904             :         {
     905             :             /* use power spectrum calculated in the MDCT-domain instead of calculating new power spectrum */
     906       28454 :             periodog = power_spectrum;
     907             :         }
     908             :         else
     909             :         {
     910             :             /* Compute the squared magnitude in each FFT bin */
     911     1575586 :             if ( startBand == 0 )
     912             :             {
     913           0 :                 ( *ptr_per ) = fftBuffer[0] * fftBuffer[0]; /* DC component */
     914           0 :                 ptr_per++;
     915           0 :                 ptr_r = fftBuffer + 2;
     916             :             }
     917             :             else
     918             :             {
     919     1575586 :                 ptr_r = fftBuffer + 2 * startBand;
     920             :             }
     921             : 
     922     1575586 :             ptr_i = ptr_r + 1;
     923             : 
     924   461646750 :             for ( ; ptr_per < periodog + stopFFTbin - startBand; ptr_per++ )
     925             :             {
     926   460071164 :                 ( *ptr_per ) = ( *ptr_r ) * ( *ptr_r ) + ( *ptr_i ) * ( *ptr_i );
     927   460071164 :                 ptr_r += 2;
     928   460071164 :                 ptr_i += 2;
     929             :             }
     930             :             /* Nyquist frequency is discarded */
     931             : 
     932             :             /* Rescale to get energy/sample: it should be 2*(1/N)*(2/N), parseval relation with 1/N,*2 for nrg computed till Nyquist only, 2/N as windowed samples correspond to half a frame*/
     933     1575586 :             v_multc( periodog, 4.f / (float) ( hFdCngDec->hFdCngCom->fftlen * hFdCngDec->hFdCngCom->fftlen ), periodog, stopFFTbin - startBand );
     934             :         }
     935             : 
     936             :         /* Adjust to the desired frequency resolution by averaging over spectral partitions for SID transmission */
     937     1604040 :         bandcombinepow( periodog, stopFFTbin - startBand, part, npart, psize_inv, msPeriodog );
     938             : 
     939             :         /* Compress MS inputs */
     940     1604040 :         compress_range( msPeriodog, msLogPeriodog, npart );
     941             : 
     942             :         /* Call the minimum statistics routine for noise estimation */
     943     1604040 :         minimum_statistics( npart, nFFTpart, psize, msLogPeriodog, hFdCngDec->msNoiseFloor, msLogNoiseEst, hFdCngDec->msAlpha, hFdCngDec->msPsd, hFdCngDec->msPsdFirstMoment, hFdCngDec->msPsdSecondMoment, hFdCngDec->msMinBuf, hFdCngDec->msBminWin, hFdCngDec->msBminSubWin, hFdCngDec->msCurrentMin, hFdCngDec->msCurrentMinOut, hFdCngDec->msCurrentMinSubWindow, hFdCngDec->msLocalMinFlag, hFdCngDec->msNewMinFlag, hFdCngDec->msPeriodogBuf, &( hFdCngDec->msPeriodogBufPtr ), hFdCngDec->hFdCngCom,
     944             :                             DEC, element_mode );
     945             : 
     946             :         /* Expand MS outputs */
     947     1604040 :         expand_range( msLogNoiseEst, msNoiseEst, npart );
     948             :     }
     949             : 
     950     2142522 :     return;
     951             : }
     952             : 
     953             : 
     954             : /*-------------------------------------------------------------------
     955             :  * FdCng_decodeSID()
     956             :  *
     957             :  * Decode the FD-CNG bitstream
     958             :  *-------------------------------------------------------------------*/
     959             : 
     960        5898 : void FdCng_decodeSID(
     961             :     Decoder_State *st /* i/o: decoder state structure */
     962             : )
     963             : {
     964             :     int16_t N;
     965             :     float *sidNoiseEst;
     966             :     float gain;
     967             :     int16_t i, index;
     968             :     float v[32];
     969             :     int16_t indices[32];
     970             :     HANDLE_FD_CNG_COM hFdCngCom;
     971             :     float *invTrfMatrix;
     972             :     float tmpRAM[FDCNG_VQ_MAX_LEN][FDCNG_VQ_DCT_MAXTRUNC];
     973             : 
     974        5898 :     const float gain_q_offset = ( st->element_mode == EVS_MONO ) ? GAIN_Q_OFFSET_EVS : GAIN_Q_OFFSET_IVAS;
     975             : 
     976        5898 :     invTrfMatrix = (float *) tmpRAM;
     977             : 
     978        5898 :     hFdCngCom = ( st->hFdCngDec )->hFdCngCom;
     979             : 
     980        5898 :     sidNoiseEst = hFdCngCom->sidNoiseEst;
     981             : 
     982        5898 :     N = hFdCngCom->npart;
     983        5898 :     gain = 0.0f;
     984        5898 :     hFdCngCom->sid_frame_counter++;
     985             : 
     986             :     /* Read bitstream */
     987       41286 :     for ( i = 0; i < FD_CNG_stages_37bits; i++ )
     988             :     {
     989       35388 :         indices[i] = get_next_indice( st, bits_37bits[i] );
     990             :     }
     991             : 
     992        5898 :     index = get_next_indice( st, 7 );
     993             : 
     994             :     /* MSVQ decoder */
     995             : 
     996        5898 :     if ( st->element_mode != EVS_MONO )
     997             :     {
     998        5898 :         create_IDCT_N_Matrix( invTrfMatrix, FDCNG_VQ_MAX_LEN, FDCNG_VQ_DCT_MAXTRUNC, sizeof( tmpRAM ) / ( sizeof( float ) ) );
     999        5898 :         msvq_dec( cdk_37bits_ivas, NULL, NULL, FD_CNG_stages_37bits, N, FD_CNG_maxN_37bits, indices, 1, invTrfMatrix, v, NULL );
    1000             :     }
    1001             :     else
    1002             :     { /* Legacy EVS_MONO MSVQ tables */
    1003           0 :         msvq_dec( cdk_37bits, NULL, NULL, FD_CNG_stages_37bits, N, FD_CNG_maxN_37bits, indices, 0, NULL, v, NULL );
    1004             :     }
    1005             : 
    1006             : 
    1007             :     /* Decode gain */
    1008        5898 :     gain = ( (float) index - gain_q_offset ) / 1.5f;
    1009             : 
    1010             :     /* Apply gain and undo log */
    1011      144342 :     for ( i = 0; i < N; i++ )
    1012             :     {
    1013      138444 :         sidNoiseEst[i] = (float) pow( 10.f, ( v[i] + gain ) / 10.f );
    1014             :     }
    1015             : 
    1016             :     /* NB last band energy compensation */
    1017             : 
    1018        5898 :     if ( hFdCngCom->CngBandwidth == NB )
    1019             :     {
    1020           0 :         sidNoiseEst[N - 1] *= NB_LAST_BAND_SCALE;
    1021             :     }
    1022             : 
    1023        5898 :     if ( hFdCngCom->CngBandwidth == SWB && hFdCngCom->CngBitrate <= ACELP_13k20 )
    1024             :     {
    1025        1729 :         sidNoiseEst[N - 1] *= SWB_13k2_LAST_BAND_SCALE;
    1026             :     }
    1027             : 
    1028        5898 :     scalebands( sidNoiseEst, hFdCngCom->part, hFdCngCom->npart, hFdCngCom->midband, hFdCngCom->nFFTpart, hFdCngCom->stopBand - hFdCngCom->startBand, hFdCngCom->cngNoiseLevel, 1 );
    1029             : 
    1030        5898 :     lpc_from_spectrum( hFdCngCom, hFdCngCom->startBand, hFdCngCom->stopFFTbin, st->preemph_fac );
    1031             : 
    1032        5898 :     return;
    1033             : }
    1034             : 
    1035             : 
    1036             : /*-------------------------------------------------------------------
    1037             :  * noisy_speech_detection()
    1038             :  *
    1039             :  *
    1040             :  *-------------------------------------------------------------------*/
    1041             : 
    1042     6440180 : void noisy_speech_detection(
    1043             :     HANDLE_FD_CNG_DEC hFdCngDec, /* i/o: FD_CNG structure            */
    1044             :     const int16_t vad,           /* i  : VAD flag                    */
    1045             :     const float syn[]            /* i  : input time-domain frame     */
    1046             : )
    1047             : {
    1048             :     float tmp;
    1049             : 
    1050     6440180 :     if ( vad == 0 )
    1051             :     {
    1052      234162 :         tmp = dotp( hFdCngDec->msNoiseEst, hFdCngDec->psize_shaping, hFdCngDec->nFFTpart_shaping );
    1053      234162 :         hFdCngDec->lp_noise = 0.995f * hFdCngDec->lp_noise + 0.005f * 10.f * (float) log10( tmp + DELTA );
    1054             :     }
    1055             :     else
    1056             :     {
    1057     6206018 :         tmp = dotp( syn, syn, hFdCngDec->hFdCngCom->frameSize ) * 2.f / hFdCngDec->hFdCngCom->frameSize;
    1058     6206018 :         hFdCngDec->lp_speech = 0.995f * hFdCngDec->lp_speech + 0.005f * 10.f * (float) log10( tmp + DELTA );
    1059             :     }
    1060             : 
    1061     6440180 :     tmp = hFdCngDec->lp_speech - 45.f;
    1062     6440180 :     if ( hFdCngDec->lp_noise < tmp )
    1063             :     {
    1064     5334375 :         hFdCngDec->lp_noise = tmp;
    1065             :     }
    1066             : 
    1067     6440180 :     hFdCngDec->hFdCngCom->flag_noisy_speech = ( hFdCngDec->lp_speech - hFdCngDec->lp_noise ) < 28.f;
    1068             : 
    1069     6440180 :     return;
    1070             : }
    1071             : 
    1072             : 
    1073             : /*-------------------------------------------------------------------
    1074             :  * generate_comfort_noise_dec()
    1075             :  *
    1076             :  * Generate the comfort noise based on the target noise level
    1077             :  *-------------------------------------------------------------------*/
    1078             : 
    1079       54030 : void generate_comfort_noise_dec(
    1080             :     float **bufferReal,     /* o  : Real part of input bands      */
    1081             :     float **bufferImag,     /* o  : Imaginary part of input bands */
    1082             :     Decoder_State *st,      /* i/o: decoder state structure       */
    1083             :     const int16_t nchan_out /* i  : number of output channels     */
    1084             : )
    1085             : {
    1086             :     int16_t i, j;
    1087             :     float *ptr_r, *ptr_i;
    1088       54030 :     HANDLE_FD_CNG_DEC hFdCngDec = st->hFdCngDec;
    1089       54030 :     HANDLE_FD_CNG_COM hFdCngCom = hFdCngDec->hFdCngCom;
    1090       54030 :     float *cngNoiseLevel = hFdCngCom->cngNoiseLevel;
    1091       54030 :     float *ptr_level = cngNoiseLevel;
    1092       54030 :     int16_t *seed = &( hFdCngCom->seed );
    1093             :     int16_t *seed2;
    1094             :     float c1, c2;
    1095             :     float tmp1, tmp2;
    1096             :     float scale, scaleCldfb;
    1097       54030 :     float *fftBuffer = hFdCngCom->fftBuffer;
    1098       54030 :     float *timeDomainOutput = hFdCngCom->timeDomainBuffer;
    1099             :     int16_t tcx_transition;
    1100             :     float enr, att;
    1101             : 
    1102       54030 :     scale = 1.f;
    1103       54030 :     scaleCldfb = CLDFB_SCALING / hFdCngCom->scalingFactor;
    1104             : 
    1105       54030 :     c1 = (float) sqrt( hFdCngCom->coherence[0] );
    1106       54030 :     c2 = (float) sqrt( 1 - hFdCngCom->coherence[0] );
    1107             : 
    1108       54030 :     seed2 = &( hFdCngCom->seed2 );
    1109       54030 :     if ( st->element_mode == IVAS_CPE_MDCT && st->idchan == 1 )
    1110             :     {
    1111        7974 :         seed2 = &( hFdCngCom->seed3 );
    1112             :     }
    1113             : 
    1114             :     /* Generate Gaussian random noise in real and imaginary parts of the FFT bins
    1115             :       Amplitudes are adjusted to the estimated noise level cngNoiseLevel in each bin */
    1116             : 
    1117       54030 :     if ( hFdCngCom->startBand == 0 )
    1118             :     {
    1119           0 :         if ( ( st->element_mode == IVAS_CPE_MDCT && nchan_out != 1 ) || ( st->element_mode == IVAS_SCE && st->cng_ism_flag ) )
    1120             :         {
    1121           0 :             rand_gauss( &tmp1, seed );
    1122           0 :             rand_gauss( &tmp2, seed2 );
    1123           0 :             fftBuffer[0] = tmp1 * c1 + tmp2 * c2;
    1124             :         }
    1125             :         else
    1126             :         {
    1127           0 :             rand_gauss( &fftBuffer[0], seed );
    1128             :         }
    1129           0 :         fftBuffer[0] *= (float) sqrt( scale * *ptr_level ); /* DC component in FFT */
    1130           0 :         ptr_level++;
    1131           0 :         ptr_r = fftBuffer + 2;
    1132             :     }
    1133             :     else
    1134             :     {
    1135       54030 :         fftBuffer[0] = 0.f;
    1136       54030 :         set_f( fftBuffer + 2, 0.0f, 2 * ( hFdCngCom->startBand - 1 ) );
    1137       54030 :         ptr_r = fftBuffer + 2 * hFdCngCom->startBand;
    1138             :     }
    1139             : 
    1140       54030 :     ptr_i = ptr_r + 1;
    1141       54030 :     if ( st->element_mode == IVAS_CPE_MDCT && nchan_out != 1 )
    1142       15662 :     {
    1143             :         int16_t band_len_accu;
    1144             : 
    1145       15662 :         band_len_accu = 0;
    1146       15662 :         i = 0;
    1147       93972 :         for ( int16_t b = 0; b < MDCT_ST_DTX_NUM_COHERENCE_BANDS; b++ )
    1148             :         {
    1149       78310 :             band_len_accu += mdct_stereo_dtx_coherence_bandlengths[b];
    1150             : 
    1151             :             /* First band needs to be shortened. The offset from encoder-side estimation is already in, so add it back here */
    1152       78310 :             if ( b == 0 )
    1153             :             {
    1154       15662 :                 band_len_accu += MDCT_ST_DTX_FIRST_BAND_OFFSET - hFdCngCom->startBand;
    1155             :             }
    1156             : 
    1157             :             /*
    1158             :              * for last band, we need to keep going until the end of the fft section - if there is still any
    1159             :              * this way, the coherence value of the last band is used for eveyrthing above as well
    1160             :              */
    1161       78310 :             if ( b == MDCT_ST_DTX_NUM_COHERENCE_BANDS - 1 )
    1162             :             {
    1163       15662 :                 band_len_accu = max( band_len_accu, hFdCngCom->stopFFTbin - hFdCngCom->startBand );
    1164             :             }
    1165             : 
    1166             :             /* mixing values for coherence is now frequency-dependent */
    1167       78310 :             c1 = (float) sqrt( hFdCngCom->coherence[b] );
    1168       78310 :             c2 = (float) sqrt( 1 - hFdCngCom->coherence[b] );
    1169             : 
    1170     5058826 :             for ( ; i < band_len_accu; i++ )
    1171             :             {
    1172             :                 float val_level;
    1173     4980516 :                 val_level = (float) sqrt( ( scale * *ptr_level ) * 0.5f );
    1174             : 
    1175             :                 /* Real part in FFT bins */
    1176     4980516 :                 rand_gauss( &tmp1, seed );
    1177     4980516 :                 rand_gauss( &tmp2, seed2 );
    1178     4980516 :                 *ptr_r = tmp1 * c1 + tmp2 * c2;
    1179     4980516 :                 ( *ptr_r ) *= val_level;
    1180             : 
    1181             :                 /* Imaginary part in FFT bins */
    1182     4980516 :                 rand_gauss( &tmp1, seed );
    1183     4980516 :                 rand_gauss( &tmp2, seed2 );
    1184     4980516 :                 *ptr_i = tmp1 * c1 + tmp2 * c2;
    1185     4980516 :                 ( *ptr_i ) *= val_level;
    1186             : 
    1187             :                 /* advance all pointers together here */
    1188     4980516 :                 ptr_r += 2;
    1189     4980516 :                 ptr_i += 2;
    1190     4980516 :                 ptr_level++;
    1191             :             }
    1192             :         }
    1193             :     }
    1194             :     else
    1195             :     {
    1196    10817952 :         for ( ; ptr_level < cngNoiseLevel + hFdCngCom->stopFFTbin - hFdCngCom->startBand; ptr_level++ )
    1197             :         {
    1198             :             /* Real part in FFT bins */
    1199    10779584 :             if ( st->element_mode == IVAS_SCE && st->cng_ism_flag )
    1200             :             {
    1201     4604348 :                 rand_gauss( &tmp1, seed );
    1202     4604348 :                 rand_gauss( &tmp2, seed2 );
    1203     4604348 :                 *ptr_r = tmp1 * c1 + tmp2 * c2;
    1204             :             }
    1205             :             else
    1206             :             {
    1207     6175236 :                 rand_gauss( ptr_r, seed );
    1208             :             }
    1209    10779584 :             ( *ptr_r ) *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
    1210    10779584 :             ptr_r += 2;
    1211             : 
    1212             :             /* Imaginary part in FFT bins */
    1213    10779584 :             if ( st->element_mode == IVAS_SCE && st->cng_ism_flag )
    1214             :             {
    1215     4604348 :                 rand_gauss( &tmp1, seed );
    1216     4604348 :                 rand_gauss( &tmp2, seed2 );
    1217     4604348 :                 *ptr_i = tmp1 * c1 + tmp2 * c2;
    1218             :             }
    1219             :             else
    1220             :             {
    1221     6175236 :                 rand_gauss( ptr_i, seed );
    1222             :             }
    1223    10779584 :             ( *ptr_i ) *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
    1224    10779584 :             ptr_i += 2;
    1225             :         }
    1226             :     }
    1227             : 
    1228             :     /* Remaining FFT bins are set to zero */
    1229       54030 :     set_f( fftBuffer + 2 * hFdCngCom->stopFFTbin, 0.0f, hFdCngCom->fftlen - 2 * hFdCngCom->stopFFTbin );
    1230             : 
    1231             :     /* Nyquist frequency is discarded */
    1232       54030 :     fftBuffer[1] = 0.f;
    1233             : 
    1234             :     /* If previous frame is active, reset the overlap-add buffer */
    1235       54030 :     tcx_transition = 0;
    1236       54030 :     if ( hFdCngCom->frame_type_previous == ACTIVE_FRAME )
    1237             :     {
    1238        6136 :         set_f( hFdCngCom->olapBufferSynth, 0.0f, hFdCngCom->fftlen );
    1239        6136 :         if ( ( st->core > ACELP_CORE && st->codec_mode == MODE2 ) || st->codec_mode == MODE1 )
    1240             :         {
    1241        6136 :             tcx_transition = 1;
    1242             :         }
    1243             :     }
    1244             : 
    1245             :     /* Perform STFT synthesis */
    1246       54030 :     SynthesisSTFT( fftBuffer, timeDomainOutput, hFdCngCom->olapBufferSynth, hFdCngCom->olapWinSyn, tcx_transition, hFdCngCom, st->element_mode, nchan_out );
    1247             : 
    1248             :     /* update CNG excitation energy for LP_CNG */
    1249             : 
    1250             :     /* calculate the residual signal energy */
    1251       54030 :     enr = dotp( hFdCngCom->exc_cng, hFdCngCom->exc_cng, hFdCngCom->frameSize ) / hFdCngCom->frameSize;
    1252             : 
    1253             :     /* convert log2 of residual signal energy */
    1254       54030 :     enr = (float) log10( enr + 0.1f ) / (float) log10( 2.0f );
    1255             : 
    1256             :     /* decrease the energy in case of WB input */
    1257       54030 :     if ( st->bwidth != NB )
    1258             :     {
    1259       54030 :         if ( st->bwidth == WB )
    1260             :         {
    1261       11439 :             if ( st->CNG_mode >= 0 )
    1262             :             {
    1263             :                 /* Bitrate adapted attenuation */
    1264           3 :                 att = ENR_ATT[st->CNG_mode];
    1265             :             }
    1266             :             else
    1267             :             {
    1268             :                 /* Use least attenuation for higher bitrates */
    1269       11436 :                 att = ENR_ATT[4];
    1270             :             }
    1271             :         }
    1272             :         else
    1273             :         {
    1274       42591 :             att = 1.5f;
    1275             :         }
    1276             : 
    1277       54030 :         enr -= att;
    1278             :     }
    1279             : 
    1280       54030 :     st->lp_ener = (float) ( 0.8f * st->lp_ener + 0.2f * pow( 2.0f, enr ) );
    1281             : 
    1282             :     /* Generate Gaussian random noise in real and imaginary parts of the CLDFB bands
    1283             :       Amplitudes are adjusted to the estimated noise level cngNoiseLevel in each band */
    1284             : 
    1285             :     /*
    1286             :      * Note: for the stereo DTX noise mixing, c1 and c2 at this point are set to the value calculated for the last band
    1287             :      *       as all the coherence bands are in the FFT region, we do not need the special handling here
    1288             :      */
    1289             : 
    1290       54030 :     if ( bufferReal != NULL && hFdCngCom->numCoreBands < hFdCngCom->regularStopBand )
    1291             :     {
    1292           0 :         for ( j = hFdCngCom->numCoreBands; j < hFdCngCom->regularStopBand; j++ )
    1293             :         {
    1294           0 :             for ( i = 0; i < hFdCngCom->numSlots; i++ )
    1295             :             {
    1296             :                 /* Real part in CLDFB band */
    1297           0 :                 if ( ( st->element_mode == IVAS_CPE_MDCT && nchan_out != 1 ) || ( st->element_mode == IVAS_SCE && st->cng_ism_flag ) )
    1298             :                 {
    1299           0 :                     rand_gauss( &tmp1, seed );
    1300           0 :                     rand_gauss( &tmp2, seed2 );
    1301           0 :                     bufferReal[i][j] = tmp1 * c1 + tmp2 * c2;
    1302             :                 }
    1303             :                 else
    1304             :                 {
    1305           0 :                     rand_gauss( &bufferReal[i][j], seed );
    1306             :                 }
    1307           0 :                 bufferReal[i][j] *= (float) sqrt( ( scaleCldfb * *ptr_level ) * 0.5f );
    1308             : 
    1309             :                 /* Imaginary part in CLDFB band */
    1310           0 :                 if ( ( st->element_mode == IVAS_CPE_MDCT && nchan_out != 1 ) || ( st->element_mode == IVAS_SCE && st->cng_ism_flag ) )
    1311             :                 {
    1312           0 :                     rand_gauss( &tmp1, seed );
    1313           0 :                     rand_gauss( &tmp2, seed2 );
    1314           0 :                     bufferImag[i][j] = tmp1 * c1 + tmp2 * c2;
    1315             :                 }
    1316             :                 else
    1317             :                 {
    1318           0 :                     rand_gauss( &bufferImag[i][j], seed );
    1319             :                 }
    1320           0 :                 bufferImag[i][j] *= (float) sqrt( ( scaleCldfb * *ptr_level ) * 0.5f );
    1321             :             }
    1322           0 :             ptr_level++;
    1323             :         }
    1324             :     }
    1325             : 
    1326             :     /* Overlap-add when previous frame is active */
    1327       54030 :     if ( hFdCngCom->frame_type_previous == ACTIVE_FRAME && st->codec_mode == MODE2 )
    1328             :     {
    1329           0 :         float noise[2048], old_exc_ener = 0.f, gain = 0.f, tmp;
    1330           0 :         int16_t N = hFdCngCom->frameSize;
    1331           0 :         int16_t seed_loc = hFdCngCom->seed;
    1332             :         float *old_exc, old_Aq[M + 1], *old_syn_pe, old_syn;
    1333             : 
    1334           0 :         if ( st->core > ACELP_CORE )
    1335             :         {
    1336           0 :             tcx_windowing_synthesis_current_frame( timeDomainOutput, st->hTcxCfg->tcx_mdct_window, /*Keep sine windows for limiting Time modulation*/
    1337           0 :                                                    st->hTcxCfg->tcx_mdct_window_half, st->hTcxCfg->tcx_mdct_window_minimum, st->hTcxCfg->tcx_mdct_window_length, st->hTcxCfg->tcx_mdct_window_half_length, st->hTcxCfg->tcx_mdct_window_min_length, 0, st->hTcxCfg->tcx_last_overlap_mode == ALDO_WINDOW ? FULL_OVERLAP : st->hTcxCfg->tcx_last_overlap_mode, NULL, NULL, NULL, NULL, NULL, N / 2, st->hTcxCfg->tcx_offset < 0 ? -st->hTcxCfg->tcx_offset : 0, 1, 0, 0 );
    1338             : 
    1339           0 :             if ( st->hTcxCfg->last_aldo )
    1340             :             {
    1341           0 :                 for ( i = 0; i < ( hFdCngCom->frameSize - NS2SA( st->sr_core, N_ZERO_MDCT_NS ) ); i++ )
    1342             :                 {
    1343           0 :                     timeDomainOutput[i] += st->hHQ_core->old_outLB[i + NS2SA( st->sr_core, N_ZERO_MDCT_NS )];
    1344             :                 }
    1345             :             }
    1346             :             else
    1347             :             {
    1348           0 :                 tcx_windowing_synthesis_past_frame( st->hTcxDec->syn_Overl, st->hTcxCfg->tcx_mdct_window, st->hTcxCfg->tcx_mdct_window_half, st->hTcxCfg->tcx_mdct_window_minimum, st->hTcxCfg->tcx_mdct_window_length, st->hTcxCfg->tcx_mdct_window_half_length, st->hTcxCfg->tcx_mdct_window_min_length, st->hTcxCfg->tcx_last_overlap_mode );
    1349             : 
    1350           0 :                 for ( i = 0; i < st->hTcxCfg->tcx_mdct_window_length; i++ )
    1351             :                 {
    1352           0 :                     timeDomainOutput[i] += st->hTcxDec->syn_Overl[i];
    1353             :                 }
    1354             :             }
    1355             :         }
    1356             :         else
    1357             :         {
    1358           0 :             mvr2r( st->old_Aq_12_8, old_Aq, M + 1 );
    1359           0 :             old_exc = st->old_exc + L_EXC_MEM_DEC - ( N / 2 );
    1360           0 :             old_syn_pe = st->mem_syn2;
    1361           0 :             old_syn = st->syn[M];
    1362             : 
    1363           0 :             for ( i = 0; i < N / 2; i++ )
    1364             :             {
    1365           0 :                 old_exc_ener += old_exc[i] * old_exc[i];
    1366             :             }
    1367             : 
    1368           0 :             old_exc_ener = (float) sqrt( old_exc_ener / (float) ( N / 2 ) );
    1369             : 
    1370           0 :             for ( i = 0; i < N; i++ )
    1371             :             {
    1372           0 :                 rand_gauss( &( noise[i] ), &( seed_loc ) );
    1373           0 :                 gain += noise[i] * noise[i];
    1374             :             }
    1375             : 
    1376           0 :             gain = old_exc_ener / (float) sqrt( gain / (float) N );
    1377             : 
    1378           0 :             for ( i = 0; i < N; i++ )
    1379             :             {
    1380           0 :                 noise[i] *= gain;
    1381             :             }
    1382             : 
    1383           0 :             syn_filt( old_Aq, M, noise, noise, N, old_syn_pe, 0 );
    1384             : 
    1385           0 :             tmp = old_syn;
    1386             : 
    1387           0 :             deemph( noise, st->preemph_fac, N, &tmp );
    1388             : 
    1389           0 :             for ( i = 0; i < N / 2; i++ )
    1390             :             {
    1391           0 :                 timeDomainOutput[i] += noise[i] * hFdCngCom->olapWinSyn[N / 2 + i];
    1392             :             }
    1393             :         }
    1394             :     }
    1395             : 
    1396       54030 :     return;
    1397             : }
    1398             : 
    1399             : 
    1400             : /*-------------------------------------------------------------------
    1401             :  * generate_comfort_noise_dec_hf()
    1402             :  *
    1403             :  * Generate the comfort noise based on the target noise level for the CLDFB part
    1404             :  *-------------------------------------------------------------------*/
    1405             : 
    1406       44112 : void generate_comfort_noise_dec_hf(
    1407             :     float **bufferReal,          /* o  : Real part of input bands                               */
    1408             :     float **bufferImag,          /* o  : Imaginary part of input bands                          */
    1409             :     HANDLE_FD_CNG_COM hFdCngCom, /* i/o: FD_CNG structure containing all buffers and variables  */
    1410             :     const int16_t cng_coh_flag   /* i  : CNG Flag for coherence handling                        */
    1411             : )
    1412             : {
    1413             :     int16_t i, j;
    1414             :     float *ptr_level;
    1415             : 
    1416       44112 :     int16_t *seed = &( hFdCngCom->seed );
    1417       44112 :     float scale = CLDFB_SCALING / hFdCngCom->scalingFactor;
    1418             : 
    1419       44112 :     int16_t *seed2 = &( hFdCngCom->seed );
    1420             : 
    1421       44112 :     float tmp1, tmp2, c1 = 0.f, c2 = 0.f;
    1422             : 
    1423       44112 :     if ( cng_coh_flag )
    1424             :     {
    1425       17762 :         seed2 = &( hFdCngCom->seed2 );
    1426             : 
    1427             :         /* alwas use the value for the last band - frequency-wise we are here always above */
    1428       17762 :         c1 = (float) sqrt( hFdCngCom->coherence[MDCT_ST_DTX_NUM_COHERENCE_BANDS - 1] );
    1429       17762 :         c2 = (float) sqrt( 1 - hFdCngCom->coherence[MDCT_ST_DTX_NUM_COHERENCE_BANDS - 1] );
    1430             :     }
    1431             : 
    1432       44112 :     ptr_level = hFdCngCom->cngNoiseLevel + hFdCngCom->stopFFTbin - hFdCngCom->startBand;
    1433             :     /*
    1434             :       Generate Gaussian random noise in real and imaginary parts of the CLDFB bands
    1435             :       Amplitudes are adjusted to the estimated noise level cngNoiseLevel in each band
    1436             :     */
    1437       44112 :     if ( hFdCngCom->numCoreBands < hFdCngCom->regularStopBand )
    1438             :     {
    1439      802739 :         for ( j = hFdCngCom->numCoreBands; j < hFdCngCom->regularStopBand; j++ )
    1440             :         {
    1441    12965968 :             for ( i = 0; i < hFdCngCom->numSlots; i++ )
    1442             :             {
    1443    12203264 :                 if ( cng_coh_flag )
    1444             :                 {
    1445             :                     /* Real part in CLDFB band */
    1446     5208528 :                     rand_gauss( &tmp1, seed );
    1447     5208528 :                     rand_gauss( &tmp2, seed2 );
    1448     5208528 :                     bufferReal[i][j] = tmp1 * c1 + tmp2 * c2;
    1449     5208528 :                     bufferReal[i][j] *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
    1450             : 
    1451             :                     /* Imaginary part in CLDFB band */
    1452     5208528 :                     rand_gauss( &tmp1, seed );
    1453     5208528 :                     rand_gauss( &tmp2, seed2 );
    1454     5208528 :                     bufferImag[i][j] = tmp1 * c1 + tmp2 * c2;
    1455     5208528 :                     bufferImag[i][j] *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
    1456             :                 }
    1457             :                 else
    1458             :                 {
    1459             :                     /* Real part in CLDFB band */
    1460     6994736 :                     rand_gauss( &bufferReal[i][j], seed );
    1461     6994736 :                     bufferReal[i][j] *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
    1462             :                     /* Imaginary part in CLDFB band */
    1463     6994736 :                     rand_gauss( &bufferImag[i][j], seed );
    1464     6994736 :                     bufferImag[i][j] *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
    1465             :                 }
    1466             :             }
    1467      762704 :             ptr_level++;
    1468             :         }
    1469             :     }
    1470             : 
    1471       44112 :     return;
    1472             : }
    1473             : 
    1474             : 
    1475             : /*-------------------------------------------------------------------
    1476             :  * generate_masking_noise()
    1477             :  *
    1478             :  * Generate additional comfort noise (kind of noise filling)
    1479             :  *-------------------------------------------------------------------*/
    1480             : 
    1481      784137 : void generate_masking_noise(
    1482             :     float *timeDomainBuffer,          /* i/o: time-domain signal */
    1483             :     HANDLE_FD_CNG_COM hFdCngCom,      /* i/o: FD_CNG structure containing all buffers and variables */
    1484             :     const int16_t length,             /* i  : frame size                                       */
    1485             :     const int16_t core,               /* i  : core                                             */
    1486             :     const int16_t return_noise,       /* i  : noise is returned instead of added */
    1487             :     const int16_t secondary,          /* i  : flag to indicate secondary noise generation */
    1488             :     const int16_t element_mode,       /* i  : element mode */
    1489             :     STEREO_CNG_DEC_HANDLE hStereoCng, /* i  : stereo CNG handle */
    1490             :     const int16_t nchan_out           /* i  : number of output channels */
    1491             : )
    1492             : {
    1493      784137 :     float *cngNoiseLevel = hFdCngCom->cngNoiseLevel;
    1494      784137 :     float *ptr_level = cngNoiseLevel;
    1495      784137 :     float *fftBuffer = hFdCngCom->fftBuffer;
    1496             :     int16_t i;
    1497             :     float maskingNoise[L_FRAME16k];
    1498             :     float *ptr_r;
    1499             :     float *ptr_i;
    1500      784137 :     int16_t startBand = hFdCngCom->startBand;
    1501      784137 :     int16_t *seed = &( hFdCngCom->seed );
    1502      784137 :     float scale = 1.f;
    1503             : 
    1504             :     /* skip noise generating if level is very low, to avoid problems with possibly running into denormals */
    1505      784137 :     if ( hFdCngCom->likelihood_noisy_speech > DELTA_MASKING_NOISE )
    1506             :     {
    1507       15089 :         if ( core != AMR_WB_CORE )
    1508             :         {
    1509             :             /* Compute additional CN level */
    1510      203531 :             for ( i = 0; i < SIZE_SCALE_TABLE_CN; i++ )
    1511             :             {
    1512      203531 :                 if ( ( hFdCngCom->CngBandwidth == scaleTable_cn_only[i].bwmode ) &&
    1513       58432 :                      ( hFdCngCom->CngBitrate >= scaleTable_cn_only[i].bitrateFrom ) &&
    1514       58432 :                      ( hFdCngCom->CngBitrate < scaleTable_cn_only[i].bitrateTo ) )
    1515             :                 {
    1516       15089 :                     break;
    1517             :                 }
    1518             :             }
    1519             : 
    1520       15089 :             scale *= (float) pow( 10.f, -scaleTable_cn_only[i].scale / 10.f ) - 1.f;
    1521             :         }
    1522             :         else
    1523             :         {
    1524             :             /* Compute additional CN level */
    1525           0 :             for ( i = 0; i < SIZE_SCALE_TABLE_CN_AMRWB; i++ )
    1526             :             {
    1527           0 :                 if ( hFdCngCom->CngBitrate >= scaleTable_cn_only_amrwbio[i][0] )
    1528             :                 {
    1529           0 :                     break;
    1530             :                 }
    1531             :             }
    1532             : 
    1533           0 :             if ( i < SIZE_SCALE_TABLE_CN_AMRWB )
    1534             :             {
    1535           0 :                 scale *= (float) pow( 10.f, -scaleTable_cn_only_amrwbio[i][1] / 10.f ) - 1.f;
    1536             :             }
    1537             :             else
    1538             :             {
    1539           0 :                 scale = 0.f;
    1540             :             }
    1541             :         }
    1542             : 
    1543             :         /* Exclude clean speech */
    1544       15089 :         scale *= hFdCngCom->likelihood_noisy_speech;
    1545             : 
    1546             :         /* Generate Gaussian random noise in real and imaginary parts of the FFT bins
    1547             :           Amplitudes are adjusted to the estimated noise level cngNoiseLevel in each bin */
    1548       15089 :         if ( startBand == 0 )
    1549             :         {
    1550           0 :             rand_gauss( &fftBuffer[0], seed );
    1551           0 :             ptr_r = fftBuffer + 2;
    1552           0 :             fftBuffer[0] *= (float) sqrt( scale * *ptr_level ); /* DC component in FFT */
    1553           0 :             ptr_level++;
    1554             :         }
    1555             :         else
    1556             :         {
    1557       15089 :             fftBuffer[0] = 0.f;
    1558       15089 :             set_f( fftBuffer + 2, 0.0f, 2 * ( startBand - 1 ) );
    1559       15089 :             ptr_r = fftBuffer + 2 * startBand;
    1560             :         }
    1561       15089 :         ptr_i = ptr_r + 1;
    1562     4292751 :         for ( ; ptr_level < cngNoiseLevel + hFdCngCom->stopFFTbin - startBand; ptr_level++ )
    1563             :         {
    1564             :             /* Real part in FFT bins */
    1565     4277662 :             rand_gauss( ptr_r, seed );
    1566     4277662 :             ( *ptr_r ) *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
    1567     4277662 :             ptr_r += 2;
    1568             :             /* Imaginary part in FFT bins */
    1569     4277662 :             rand_gauss( ptr_i, seed );
    1570     4277662 :             ( *ptr_i ) *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
    1571     4277662 :             ptr_i += 2;
    1572             :         }
    1573             : 
    1574             :         /* Remaining FFT bins are set to zero */
    1575       15089 :         set_f( fftBuffer + 2 * hFdCngCom->stopFFTbin, 0.0f, hFdCngCom->fftlen - 2 * hFdCngCom->stopFFTbin );
    1576             :         /* Nyquist frequency is discarded */
    1577       15089 :         fftBuffer[1] = 0.f;
    1578             :     }
    1579             :     else
    1580             :     {
    1581             :         /* very low level case - update random seeds and reset FFT buffer; don't fully skip SynthesisSTFT(), because of the buffer updates done there... */
    1582      769048 :         generate_masking_noise_update_seed( hFdCngCom );
    1583             : 
    1584      769048 :         set_f( fftBuffer, 0.f, hFdCngCom->fftlen );
    1585             :     }
    1586             : 
    1587             :     /* Perform STFT synthesis */
    1588      784137 :     if ( secondary )
    1589             :     {
    1590        1374 :         SynthesisSTFT( fftBuffer, maskingNoise, hStereoCng->olapBufferSynth22, hFdCngCom->olapWinSyn, 0, hFdCngCom, element_mode, nchan_out );
    1591             :     }
    1592             :     else
    1593             :     {
    1594      782763 :         SynthesisSTFT( fftBuffer, maskingNoise, hFdCngCom->olapBufferSynth2, hFdCngCom->olapWinSyn, 0, hFdCngCom, element_mode, nchan_out );
    1595             :     }
    1596             : 
    1597             :     /* Add some comfort noise on top of decoded signal */
    1598      784137 :     if ( return_noise )
    1599             :     {
    1600        2748 :         mvr2r( maskingNoise, timeDomainBuffer, min( hFdCngCom->frameSize, length ) );
    1601             :     }
    1602             :     else
    1603             :     {
    1604      781389 :         v_add( maskingNoise, timeDomainBuffer, timeDomainBuffer, min( hFdCngCom->frameSize, length ) );
    1605             :     }
    1606             : 
    1607      784137 :     return;
    1608             : }
    1609             : 
    1610             : 
    1611             : /*-------------------------------------------------------------------
    1612             :  * generate_masking_noise_update_seed()
    1613             :  *
    1614             :  * Update seed for scenarios where generate_masking_noise() is
    1615             :  * not called based on signal statistics
    1616             :  *-------------------------------------------------------------------*/
    1617             : 
    1618      861803 : void generate_masking_noise_update_seed(
    1619             :     HANDLE_FD_CNG_COM hFdCngCom /* i/o: FD_CNG structure containing all buffers and variables */
    1620             : )
    1621             : {
    1622      861803 :     float *cngNoiseLevel = hFdCngCom->cngNoiseLevel;
    1623      861803 :     float *ptr_level = cngNoiseLevel;
    1624      861803 :     int16_t startBand = hFdCngCom->startBand;
    1625      861803 :     int16_t *seed = &( hFdCngCom->seed );
    1626      861803 :     float tmp = 0;
    1627             : 
    1628             :     /*
    1629             :       Generate Gaussian random noise in real and imaginary parts of the FFT bins
    1630             :       Amplitudes are adjusted to the estimated noise level cngNoiseLevel in each bin
    1631             :     */
    1632      861803 :     if ( startBand == 0 )
    1633             :     {
    1634           0 :         rand_gauss( &tmp, seed );
    1635           0 :         ptr_level++;
    1636             :     }
    1637             : 
    1638   233339477 :     for ( ; ptr_level < cngNoiseLevel + hFdCngCom->stopFFTbin - startBand; ptr_level++ )
    1639             :     {
    1640             :         /* Real part in FFT bins */
    1641   232477674 :         rand_gauss( &tmp, seed );
    1642   232477674 :         rand_gauss( &tmp, seed );
    1643             :     }
    1644             : 
    1645      861803 :     return;
    1646             : }
    1647             : 
    1648             : 
    1649             : /*-------------------------------------------------------------------
    1650             :  * generate_masking_noise_mdct()
    1651             :  *
    1652             :  * Generate additional comfort noise (kind of noise filling)
    1653             :  *-------------------------------------------------------------------*/
    1654             : 
    1655      207783 : void generate_masking_noise_mdct(
    1656             :     float *mdctBuffer,          /* i/o: time-domain signal */
    1657             :     HANDLE_FD_CNG_COM hFdCngCom /* i/o: FD_CNG structure containing all buffers and variables */
    1658             : )
    1659             : {
    1660      207783 :     float *cngNoiseLevel = hFdCngCom->cngNoiseLevel;
    1661             :     int16_t i;
    1662             :     float maskingNoise[2 * L_FRAME16k];
    1663             :     float *ptr_r;
    1664      207783 :     float *ptr_level = cngNoiseLevel;
    1665      207783 :     int16_t startBand = hFdCngCom->startBand;
    1666      207783 :     int16_t *seed = &( hFdCngCom->seed );
    1667      207783 :     float scale = 1.f;
    1668             : 
    1669             :     /* skip noise generating if level is very low, to avoid problems with possibly running into denormals */
    1670      207783 :     if ( hFdCngCom->likelihood_noisy_speech > DELTA_MASKING_NOISE )
    1671             :     {
    1672       46733 :         for ( i = 0; i < SIZE_SCALE_TABLE_CN; i++ )
    1673             :         {
    1674       46733 :             if ( ( hFdCngCom->CngBandwidth == scaleTable_cn_only[i].bwmode ) &&
    1675       14029 :                  ( hFdCngCom->CngBitrate >= scaleTable_cn_only[i].bitrateFrom ) &&
    1676       14029 :                  ( hFdCngCom->CngBitrate < scaleTable_cn_only[i].bitrateTo ) )
    1677             :             {
    1678        3482 :                 break;
    1679             :             }
    1680             :         }
    1681             : 
    1682        3482 :         scale *= (float) pow( 10.f, -scaleTable_cn_only[i].scale / 10.f ) - 1.f;
    1683             : 
    1684             :         /* Exclude clean speech */
    1685        3482 :         scale *= hFdCngCom->likelihood_noisy_speech;
    1686             : 
    1687             :         /*
    1688             :           Generate Gaussian random noise in real and imaginary parts of the FFT bins
    1689             :           Amplitudes are adjusted to the estimated noise level cngNoiseLevel in each bin
    1690             :         */
    1691        3482 :         if ( startBand == 0 )
    1692             :         {
    1693           0 :             rand_gauss( &maskingNoise[0], seed );
    1694           0 :             maskingNoise[0] *= (float) sqrt( scale * *ptr_level * 0.5f ); /* DC component in FFT */
    1695           0 :             ptr_level++;
    1696           0 :             ptr_r = maskingNoise + 1;
    1697             :         }
    1698             :         else
    1699             :         {
    1700        3482 :             maskingNoise[0] = 0.f;
    1701        3482 :             set_f( maskingNoise + 1, 0.0f, ( startBand - 1 ) );
    1702        3482 :             ptr_r = maskingNoise + startBand;
    1703             :         }
    1704             : 
    1705      994470 :         for ( ; ptr_level < cngNoiseLevel + hFdCngCom->stopFFTbin - startBand; ptr_level++ )
    1706             :         {
    1707             :             /* MDCT bins */
    1708      990988 :             rand_gauss( ptr_r, seed );
    1709      990988 :             ( *ptr_r ) *= (float) sqrt( scale * *ptr_level * 0.5f );
    1710      990988 :             ptr_r += 1;
    1711             :         }
    1712             : 
    1713             :         /*re-normalization of energy level: M/sqrt(2)*/
    1714        3482 :         v_multc( maskingNoise, (float) sqrt( NORM_MDCT_FACTOR ), maskingNoise, hFdCngCom->stopFFTbin );
    1715             : 
    1716             :         /* Add some comfort noise on top of decoded signal */
    1717        3482 :         v_add( maskingNoise, mdctBuffer, mdctBuffer, hFdCngCom->stopFFTbin );
    1718             :     }
    1719             :     else
    1720             :     {
    1721             :         /* very low level case - just update random seeds */
    1722      204301 :         if ( startBand == 0 )
    1723             :         {
    1724           0 :             rand_gauss( &maskingNoise[0], seed );
    1725           0 :             ptr_level++;
    1726             :         }
    1727             : 
    1728    54660147 :         for ( ; ptr_level < cngNoiseLevel + hFdCngCom->stopFFTbin - startBand; ptr_level++ )
    1729             :         {
    1730    54455846 :             rand_gauss( &maskingNoise[0], seed );
    1731             :         }
    1732             :     }
    1733             : 
    1734      207783 :     return;
    1735             : }
    1736             : 
    1737             : /*-------------------------------------------------------------------
    1738             :  * generate_stereo_masking_noise()
    1739             :  *
    1740             :  * Generate additional comfort noise (kind of noise filling)
    1741             :  *-------------------------------------------------------------------*/
    1742             : 
    1743        2784 : void generate_stereo_masking_noise(
    1744             :     float *syn,                          /* i/o: time-domain signal              */
    1745             :     Decoder_State *st,                   /* i/o: decoder state structure         */
    1746             :     STEREO_TD_DEC_DATA_HANDLE hStereoTD, /* i  : TD stereo structure             */
    1747             :     const int16_t flag_sec_CNA,          /* i  : CNA flag for secondary channel  */
    1748             :     const int16_t fadeOut,               /* i  : only fade out of previous state */
    1749             :     STEREO_CNG_DEC_HANDLE hStereoCng,    /* i  : Stereo CNG handle               */
    1750             :     const int16_t nchan_out              /* i  : number of output channels       */
    1751             : )
    1752             : {
    1753             :     HANDLE_FD_CNG_COM hFdCngCom;
    1754             :     float gamma, scale, SP_ratio;
    1755             :     float Np[L_FRAME16k];
    1756             :     float Ns[L_FRAME16k];
    1757             :     float N1[L_FRAME16k];
    1758             :     float N2[L_FRAME16k];
    1759             :     int16_t i;
    1760             : 
    1761        2784 :     if ( st->idchan == 0 )
    1762             :     {
    1763        1374 :         hFdCngCom = st->hFdCngDec->hFdCngCom;
    1764        1374 :         mvr2r( hFdCngCom->olapBufferSynth2, Np, hFdCngCom->frameSize / 2 );
    1765        1374 :         mvr2r( hStereoCng->olapBufferSynth22, Ns, hFdCngCom->frameSize / 2 );
    1766        1374 :         set_f( &Np[hFdCngCom->frameSize / 2], 0.0f, hFdCngCom->frameSize / 2 );
    1767        1374 :         set_f( &Ns[hFdCngCom->frameSize / 2], 0.0f, hFdCngCom->frameSize / 2 );
    1768             : 
    1769        1374 :         if ( !fadeOut )
    1770             :         {
    1771        1374 :             generate_masking_noise( N1, hFdCngCom, hFdCngCom->frameSize, 0, 1, 0, st->element_mode, hStereoCng, nchan_out );
    1772             :             /* Generate masking noise for secondary channel */
    1773        1374 :             if ( flag_sec_CNA )
    1774             :             {
    1775        1374 :                 generate_masking_noise( N2, hFdCngCom, hFdCngCom->frameSize, 0, 1, 1, st->element_mode, hStereoCng, nchan_out );
    1776        1374 :                 gamma = hStereoCng->c_PS_LT * hStereoCng->c_PS_LT;
    1777        1374 :                 scale = 1.0f;
    1778        1374 :                 if ( gamma < 0.9f )
    1779             :                 {
    1780        1374 :                     gamma = gamma / ( 1 - gamma );
    1781        1374 :                     gamma = (float) sqrt( gamma + 1 ) - (float) sqrt( gamma );
    1782        1374 :                     scale = 1.0f / (float) sqrt( 1 + gamma * gamma );
    1783             :                 }
    1784             :                 else
    1785             :                 {
    1786           0 :                     gamma = 0.0f;
    1787             :                 }
    1788             : 
    1789      177246 :                 for ( i = 0; i < 2 * hFdCngCom->frameSize / 4; i++ )
    1790             :                 {
    1791      175872 :                     Np[i] += scale * ( N1[i] + gamma * N2[i] );
    1792      175872 :                     Ns[i] += scale * sign( hStereoCng->c_PS_LT ) * ( N1[i] - gamma * N2[i] );
    1793             :                 }
    1794      177246 :                 for ( ; i < hFdCngCom->frameSize; i++ )
    1795             :                 {
    1796      175872 :                     Np[i] = scale * ( N1[i] + gamma * N2[i] );
    1797      175872 :                     Ns[i] = scale * sign( hStereoCng->c_PS_LT ) * ( N1[i] - gamma * N2[i] );
    1798             :                 }
    1799        1374 :                 scale *= (float) ( hFdCngCom->fftlen / 2 );
    1800      177246 :                 for ( i = 0; i < hFdCngCom->frameSize / 2; i++ )
    1801             :                 {
    1802      175872 :                     hFdCngCom->olapBufferSynth2[i] = scale * ( hFdCngCom->olapBufferSynth2[i + 5 * hFdCngCom->frameSize / 4] + gamma * hStereoCng->olapBufferSynth22[i + 5 * hFdCngCom->frameSize / 4] );
    1803      175872 :                     hStereoCng->olapBufferSynth22[i] = sign( hStereoCng->c_PS_LT ) * scale * ( hFdCngCom->olapBufferSynth2[i + 5 * hFdCngCom->frameSize / 4] - gamma * hStereoCng->olapBufferSynth22[i + 5 * hFdCngCom->frameSize / 4] );
    1804             :                 }
    1805             :             }
    1806             :             else
    1807             :             {
    1808           0 :                 for ( i = 0; i < hFdCngCom->frameSize / 2; i++ )
    1809             :                 {
    1810           0 :                     Np[i] += N1[i];
    1811             :                 }
    1812           0 :                 mvr2r( &N1[hFdCngCom->frameSize / 2], &Np[hFdCngCom->frameSize / 2], hFdCngCom->frameSize / 2 );
    1813           0 :                 scale = (float) ( hFdCngCom->fftlen / 2 );
    1814           0 :                 for ( i = 0; i < hFdCngCom->frameSize; i++ )
    1815             :                 {
    1816           0 :                     hFdCngCom->olapBufferSynth2[i] = scale * hFdCngCom->olapBufferSynth2[i + 5 * hFdCngCom->frameSize / 4];
    1817             :                 }
    1818             :             }
    1819             :         }
    1820             :         else
    1821             :         {
    1822           0 :             set_f( hFdCngCom->olapBufferSynth2, 0.0f, hFdCngCom->frameSize / 2 );
    1823           0 :             set_f( hStereoCng->olapBufferSynth22, 0.0f, hFdCngCom->frameSize / 2 );
    1824             :         }
    1825        1374 :         if ( flag_sec_CNA )
    1826             :         {
    1827        1374 :             mvr2r( Ns, hStereoCng->maskingNoiseS, hFdCngCom->frameSize );
    1828        1374 :             hStereoCng->enableSecCNA = 1;
    1829             :         }
    1830             :         else
    1831             :         {
    1832           0 :             set_f( hStereoCng->olapBufferSynth22, 0.0f, hFdCngCom->frameSize );
    1833             :         }
    1834             : 
    1835             :         /* add masking noise */
    1836        1374 :         v_add( Np, syn, syn, hFdCngCom->frameSize );
    1837             :     }
    1838        1410 :     else if ( hStereoCng->enableSecCNA )
    1839             :     {
    1840        1374 :         SP_ratio = hStereoTD->SP_ratio_LT; /* Use long-term SP ratio based on L/R synthesis */
    1841             :         /* scale and add masking noise */
    1842       89310 :         for ( i = 0; i < *hStereoCng->frameSize / 4; i++ )
    1843             :         {
    1844       87936 :             scale = ( ( hStereoTD->prevSP_ratio * ( *hStereoCng->frameSize / 4 - (float) i ) + SP_ratio * (float) i ) / ( *hStereoCng->frameSize / 4 ) );
    1845       87936 :             syn[i] += scale * hStereoCng->maskingNoiseS[i];
    1846             :         }
    1847       89310 :         for ( ; i < *hStereoCng->frameSize / 2; i++ )
    1848             :         {
    1849       87936 :             syn[i] += SP_ratio * hStereoCng->maskingNoiseS[i];
    1850             :         }
    1851      177246 :         for ( ; i < *hStereoCng->frameSize; i++ )
    1852             :         {
    1853      175872 :             syn[i] += SP_ratio * hStereoCng->maskingNoiseS[i];
    1854             :         }
    1855        1374 :         hStereoTD->prevSP_ratio = SP_ratio;
    1856             :     }
    1857             : 
    1858        2784 :     return;
    1859             : }
    1860             : 
    1861             : 
    1862             : /*-------------------------------------------------------------------
    1863             :  * generate_masking_noise_hf_cldfb()
    1864             :  *
    1865             :  * Generate additional comfort noise (kind of noise filling)
    1866             :  *-------------------------------------------------------------------*/
    1867             : 
    1868      338649 : void generate_masking_noise_lb_dirac(
    1869             :     HANDLE_FD_CNG_COM hFdCngCom,                          /* i/o: FD_CNG structure containing all buffers and variables */
    1870             :     float *tdBuffer,                                      /* i/o: time-domain signal, if NULL no LB-CNA                 */
    1871             :     const int16_t nCldfbTs,                               /* i  : number of CLDFB slots that will be rendered           */
    1872             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom, /* i  : common spatial rendering parameters handle            */
    1873             :     const int16_t cna_flag                                /* i  : CNA flag for LB and HB                                */
    1874             : )
    1875             : {
    1876             :     int16_t i;
    1877      338649 :     float *cngNoiseLevel = hFdCngCom->cngNoiseLevel;
    1878      338649 :     float *fftBuffer = hFdCngCom->fftBuffer;
    1879             :     float *ptr_r;
    1880             :     float *ptr_i;
    1881             :     float *ptr_level;
    1882      338649 :     int16_t *seed = &( hFdCngCom->seed );
    1883             :     float scale;
    1884             :     int16_t n_samples_out, n_samples_start, n_samples_out_loop;
    1885             : 
    1886      338649 :     push_wmops( "fd_cng_dirac" );
    1887             : 
    1888             :     /* Init */
    1889      338649 :     scale = 0.f;
    1890      338649 :     n_samples_out = hFdCngCom->frameSize / DEFAULT_JBM_CLDFB_TIMESLOTS * nCldfbTs;
    1891      338649 :     n_samples_start = 0;
    1892             : 
    1893             :     /*LB CLDFB - CNA from STFT*/
    1894             : #ifdef DEBUG_MODE_DIRAC
    1895             :     {
    1896             :         int16_t tmp_s;
    1897             :         tmp_s = (int16_t) ( 32768.f * 0.5f * hFdCngCom->likelihood_noisy_speech * cna_flag + 0.5f );
    1898             :         dbgwrite( &tmp_s, sizeof( int16_t ), 1, hFdCngCom->frameSize / 16, "./res/ivas_dirac_likelihood_noisy.pcm" );
    1899             :     }
    1900             : #endif
    1901      338649 :     if ( cna_flag )
    1902             :     {
    1903             :         /* skip noise generating if level is very low, to avoid problems with possibly running into denormals */
    1904       93578 :         if ( hFdCngCom->likelihood_noisy_speech > DELTA_MASKING_NOISE )
    1905             :         {
    1906             :             /* Compute additional CN level */
    1907       22971 :             for ( i = 0; i < 15; i++ )
    1908             :             {
    1909       22971 :                 if ( ( hFdCngCom->CngBandwidth == scaleTable_cn_dirac[i].bwmode ) &&
    1910        5301 :                      ( hFdCngCom->CngBitrate >= scaleTable_cn_dirac[i].bitrateFrom ) &&
    1911        5301 :                      ( hFdCngCom->CngBitrate < scaleTable_cn_dirac[i].bitrateTo ) )
    1912             :                 {
    1913        1767 :                     break;
    1914             :                 }
    1915             :             }
    1916             : 
    1917        1767 :             scale = (float) pow( 10.f, -scaleTable_cn_dirac[i].scale / 10.f ) - 1.f;
    1918        1767 :             scale *= hFdCngCom->likelihood_noisy_speech;
    1919             :         }
    1920             :     }
    1921             : 
    1922             :     /* LB CLDFB - CNA from STFT: CNA applied only in channel 0*/
    1923      338649 :     if ( cna_flag && tdBuffer != NULL )
    1924             :     {
    1925             :         int16_t cur_subframe;
    1926             :         int16_t cur_subframe_start_outfs;
    1927             :         int16_t cur_subframe_start_cngfs;
    1928             :         int16_t slot_size_cng;
    1929             : 
    1930      187156 :         while ( n_samples_out > 0 )
    1931             :         {
    1932       93578 :             n_samples_out_loop = min( hFdCngCom->frameSize, n_samples_out );
    1933       93578 :             if ( scale != 0 )
    1934             :             {
    1935             :                 /*Generate LF comfort noise only at first slot, for the whole frame*/
    1936        1767 :                 ptr_level = cngNoiseLevel;
    1937             : 
    1938             :                 /* Generate Gaussian random noise in real and imaginary parts of the FFT bins
    1939             :               Amplitudes are adjusted to the estimated noise level cngNoiseLevel in each bin */
    1940        1767 :                 if ( hFdCngCom->startBand == 0 )
    1941             :                 {
    1942           0 :                     rand_gauss( &fftBuffer[0], seed );
    1943           0 :                     ptr_r = fftBuffer + 2;
    1944           0 :                     fftBuffer[0] *= (float) sqrt( scale * *ptr_level ); /* DC component in FFT */
    1945           0 :                     ptr_level++;
    1946             :                 }
    1947             :                 else
    1948             :                 {
    1949        1767 :                     fftBuffer[0] = 0.f;
    1950        1767 :                     set_f( fftBuffer + 2, 0.0f, 2 * ( hFdCngCom->startBand - 1 ) );
    1951        1767 :                     ptr_r = fftBuffer + 2 * hFdCngCom->startBand;
    1952             :                 }
    1953        1767 :                 ptr_i = ptr_r + 1;
    1954             : 
    1955      563673 :                 for ( ; ptr_level < cngNoiseLevel + hFdCngCom->stopFFTbin - hFdCngCom->startBand; ptr_level++ )
    1956             :                 {
    1957             :                     /* Real part in FFT bins */
    1958      561906 :                     rand_gauss( ptr_r, seed );
    1959      561906 :                     ( *ptr_r ) *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
    1960      561906 :                     ptr_r += 2;
    1961             :                     /* Imaginary part in FFT bins */
    1962      561906 :                     rand_gauss( ptr_i, seed );
    1963      561906 :                     ( *ptr_i ) *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
    1964      561906 :                     ptr_i += 2;
    1965             :                 }
    1966             : 
    1967             :                 /* Remaining FFT bins are set to zero */
    1968        1767 :                 set_f( fftBuffer + 2 * hFdCngCom->stopFFTbin, 0.0f, hFdCngCom->fftlen - 2 * hFdCngCom->stopFFTbin );
    1969             :                 /* Nyquist frequency is discarded */
    1970        1767 :                 fftBuffer[1] = 0.f;
    1971             : 
    1972             :                 /* Perform STFT synthesis */
    1973        1767 :                 SynthesisSTFT_dirac( fftBuffer, tdBuffer + n_samples_start, hFdCngCom->olapBufferSynth2, hFdCngCom->olapWinSyn, n_samples_out_loop, hFdCngCom );
    1974             : 
    1975             : #ifdef DEBUG_MODE_DIRAC
    1976             :                 {
    1977             :                     int16_t tmp[1000];
    1978             : 
    1979             :                     for ( i = 0; i < hFdCngCom->frameSize; i++ )
    1980             :                     {
    1981             :                         tmp[i] = (int16_t) ( tdBuffer[i] + 0.5f );
    1982             :                     }
    1983             :                     dbgwrite( tmp, sizeof( int16_t ), hFdCngCom->frameSize, 1, "./res/ivas_dirac_cna_fft.pcm" );
    1984             :                 }
    1985             : #endif
    1986             :             }
    1987             : 
    1988             :             else
    1989             :             {
    1990             :                 /* very low level case - update random seeds */
    1991       91811 :                 generate_masking_noise_update_seed( hFdCngCom );
    1992             : 
    1993       91811 :                 set_f( fftBuffer, 0.f, hFdCngCom->fftlen );
    1994             : 
    1995             :                 /* Perform STFT synthesis */
    1996       91811 :                 SynthesisSTFT_dirac( fftBuffer, tdBuffer + n_samples_start, hFdCngCom->olapBufferSynth2, hFdCngCom->olapWinSyn, n_samples_out_loop, hFdCngCom );
    1997             : 
    1998             : #ifdef DEBUG_MODE_DIRAC
    1999             :                 {
    2000             :                     int16_t tmp[1000];
    2001             : 
    2002             :                     for ( i = 0; i < hFdCngCom->frameSize; i++ )
    2003             :                     {
    2004             :                         tmp[i] = (int16_t) ( tdBuffer[i] + 0.5f );
    2005             :                     }
    2006             :                     dbgwrite( tmp, sizeof( int16_t ), hFdCngCom->frameSize, 1, "./res/ivas_dirac_cna_fft.pcm" );
    2007             :                 }
    2008             : #endif
    2009             :             }
    2010       93578 :             n_samples_out -= hFdCngCom->frameSize;
    2011       93578 :             n_samples_start += hFdCngCom->frameSize;
    2012             :         }
    2013             : 
    2014             :         /* move generated noise to the 5ms subframe starts in the tc buffer according to the output sampling frequency to avoid
    2015             :            overwriting it with the synthesis in case of shared tc and synth channel memory, i.e. non-TSM mode */
    2016       93578 :         slot_size_cng = hFdCngCom->frameSize / DEFAULT_JBM_CLDFB_TIMESLOTS;
    2017             :         /* move start indices forward to the end of the last subframe */
    2018       93578 :         cur_subframe_start_outfs = nCldfbTs * hSpatParamRendCom->slot_size;
    2019       93578 :         cur_subframe_start_cngfs = nCldfbTs * slot_size_cng;
    2020             : 
    2021             :         /* go from the last subframe back and move the LB noise */
    2022      467890 :         for ( cur_subframe = hSpatParamRendCom->nb_subframes - 1; cur_subframe >= 0; cur_subframe-- )
    2023             :         {
    2024             :             int16_t move_size, subframe_size_outfs;
    2025      374312 :             move_size = slot_size_cng * hSpatParamRendCom->subframe_nbslots[cur_subframe];
    2026      374312 :             subframe_size_outfs = hSpatParamRendCom->subframe_nbslots[cur_subframe] * hSpatParamRendCom->slot_size;
    2027      374312 :             cur_subframe_start_outfs -= hSpatParamRendCom->subframe_nbslots[cur_subframe] * hSpatParamRendCom->slot_size;
    2028      374312 :             cur_subframe_start_cngfs -= hSpatParamRendCom->subframe_nbslots[cur_subframe] * slot_size_cng;
    2029             :             /* move cna */
    2030      374312 :             mvr2r( tdBuffer + cur_subframe_start_cngfs, tdBuffer + cur_subframe_start_outfs, move_size );
    2031             :             /* set everything else to zero */
    2032      374312 :             set_zero( tdBuffer + cur_subframe_start_outfs + move_size, subframe_size_outfs - move_size );
    2033             :         }
    2034             :     }
    2035             : 
    2036             : 
    2037      338649 :     pop_wmops();
    2038             : 
    2039      338649 :     return;
    2040             : }
    2041             : 
    2042             : 
    2043             : /*-------------------------------------------------------------------
    2044             :  * generate_masking_noise_hf_cldfb()
    2045             :  *
    2046             :  * Generate additional comfort noise (kind of noise filling)
    2047             :  *-------------------------------------------------------------------*/
    2048             : 
    2049    10336368 : void generate_masking_noise_dirac(
    2050             :     HANDLE_FD_CNG_COM hFdCngCom,      /* i/o: FD_CNG structure containing all buffers and variables */
    2051             :     HANDLE_CLDFB_FILTER_BANK h_cldfb, /* i  : filterbank state                                      */
    2052             :     float *tdBuffer,                  /* i/o: time-domain signal, if NULL no LB-CNA                 */
    2053             :     float *Cldfb_RealBuffer,          /* o  : CLDFD real buffer                                     */
    2054             :     float *Cldfb_ImagBuffer,          /* o  : CLDFD imaginary buffer                                */
    2055             :     const int16_t slot_index,         /* i  : CLDFB slot index                                      */
    2056             :     const int16_t cna_flag,           /* i  : CNA flag for LB and HB                                */
    2057             :     const int16_t fd_cng_flag         /* i  : FD-CNG flag for HB                                    */
    2058             : )
    2059             : {
    2060             :     int16_t i;
    2061             :     float *ptr_level;
    2062    10336368 :     int16_t *seed = &( hFdCngCom->seed );
    2063             :     float scale;
    2064             : 
    2065    10336368 :     push_wmops( "fd_cng_dirac" );
    2066             : 
    2067             :     /* Init */
    2068    10336368 :     scale = 0.f;
    2069             : 
    2070             :     /* Resample CLDFB memories if necessary*/
    2071    10336368 :     if ( ( h_cldfb->no_channels * h_cldfb->no_col ) != hFdCngCom->frameSize )
    2072             :     {
    2073        7189 :         resampleCldfb( h_cldfb, hFdCngCom->frameSize * FRAMES_PER_SEC );
    2074             :     }
    2075             : 
    2076    10336368 :     set_zero( Cldfb_RealBuffer, CLDFB_NO_CHANNELS_MAX );
    2077    10336368 :     set_zero( Cldfb_ImagBuffer, CLDFB_NO_CHANNELS_MAX );
    2078             : 
    2079             :     /*LB CLDFB - CNA from STFT*/
    2080             : #ifdef DEBUG_MODE_DIRAC
    2081             :     {
    2082             :         int16_t tmp_s;
    2083             :         tmp_s = (int16_t) ( 32768.f * 0.5f * hFdCngCom->likelihood_noisy_speech * cna_flag + 0.5f );
    2084             :         dbgwrite( &tmp_s, sizeof( int16_t ), 1, hFdCngCom->frameSize / 16, "./res/ivas_dirac_likelihood_noisy.pcm" );
    2085             :     }
    2086             : #endif
    2087    10336368 :     if ( cna_flag )
    2088             :     {
    2089             :         /* skip noise generating if level is very low, to avoid problems with possibly running into denormals */
    2090     2811280 :         if ( hFdCngCom->likelihood_noisy_speech > DELTA_MASKING_NOISE )
    2091             :         {
    2092             :             /* Compute additional CN level */
    2093      735072 :             for ( i = 0; i < 15; i++ )
    2094             :             {
    2095      735072 :                 if ( ( hFdCngCom->CngBandwidth == scaleTable_cn_dirac[i].bwmode ) &&
    2096      169632 :                      ( hFdCngCom->CngBitrate >= scaleTable_cn_dirac[i].bitrateFrom ) &&
    2097      169632 :                      ( hFdCngCom->CngBitrate < scaleTable_cn_dirac[i].bitrateTo ) )
    2098             :                 {
    2099       56544 :                     break;
    2100             :                 }
    2101             :             }
    2102             : 
    2103       56544 :             scale = (float) pow( 10.f, -scaleTable_cn_dirac[i].scale / 10.f ) - 1.f;
    2104       56544 :             scale *= hFdCngCom->likelihood_noisy_speech;
    2105             :         }
    2106             :     }
    2107             :     /* LB CLDFB - CNA from STFT: CNA applied only in channel 0*/
    2108    10336368 :     if ( cna_flag && tdBuffer != NULL )
    2109             :     {
    2110     1497248 :         if ( scale != 0 )
    2111             :         {
    2112             :             /* LF CLDFB*/
    2113       28272 :             cldfbAnalysis_ts( &( tdBuffer[hFdCngCom->numCoreBands * slot_index] ), Cldfb_RealBuffer, Cldfb_ImagBuffer, hFdCngCom->numCoreBands, h_cldfb );
    2114             :         }
    2115             :         else
    2116             :         {
    2117             :             /* LB ana CLDFB*/
    2118     1468976 :             cldfbAnalysis_ts( &( tdBuffer[hFdCngCom->numCoreBands * slot_index] ), Cldfb_RealBuffer, Cldfb_ImagBuffer, hFdCngCom->numCoreBands, h_cldfb );
    2119             :         }
    2120             :     }
    2121             : 
    2122             :     /*HF CLDFB - CNA and/or FD-CNG*/
    2123    10336368 :     if ( fd_cng_flag )
    2124             :     {
    2125       29088 :         scale += 1.f;
    2126             :     }
    2127    10336368 :     if ( scale != 0 )
    2128             :     {
    2129       65664 :         scale *= CLDFB_SCALING * ( h_cldfb->scale * h_cldfb->scale * 8.f );
    2130       65664 :         ptr_level = hFdCngCom->cngNoiseLevel + hFdCngCom->stopFFTbin - hFdCngCom->startBand;
    2131             : 
    2132     1378944 :         for ( i = hFdCngCom->numCoreBands; i < hFdCngCom->regularStopBand; i++ )
    2133             :         {
    2134             :             /* Real part in CLDFB band */
    2135     1313280 :             rand_gauss( &Cldfb_RealBuffer[i], seed );
    2136     1313280 :             Cldfb_RealBuffer[i] *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
    2137             :             /* Imaginary part in CLDFB band */
    2138     1313280 :             rand_gauss( &Cldfb_ImagBuffer[i], seed );
    2139     1313280 :             Cldfb_ImagBuffer[i] *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
    2140             : 
    2141     1313280 :             ptr_level++;
    2142             :         }
    2143             :     }
    2144             : 
    2145    10336368 :     pop_wmops();
    2146             : 
    2147    10336368 :     return;
    2148             : }
    2149             : 
    2150             : 
    2151             : /*-------------------------------------------------------------------
    2152             :  * FdCngDecodeMDCTStereoSID()
    2153             :  *
    2154             :  * Decode FD-Cng parameters for CNG in MDCT-Stereo mode from the bitstream
    2155             :  *
    2156             :  *-------------------------------------------------------------------*/
    2157             : 
    2158        1313 : void FdCngDecodeMDCTStereoSID(
    2159             :     CPE_DEC_HANDLE hCPE /* i/o: CPE decoder state structure     */
    2160             : )
    2161             : {
    2162             :     DEC_CORE_HANDLE sts[CPE_CHANNELS];
    2163             :     HANDLE_FD_CNG_COM hFdCngCom;
    2164             :     float *ms_ptr[CPE_CHANNELS];
    2165             :     float *lr_ptr[CPE_CHANNELS];
    2166             :     float logNoiseEst[CPE_CHANNELS][NPART];
    2167             :     float gain[CPE_CHANNELS];
    2168             :     int16_t indices[FD_CNG_stages_37bits];
    2169             :     int16_t N, i, ch, p, stages;
    2170             :     int16_t is_out_ms;
    2171             :     float *invTrfMatrix;
    2172             :     float tmpRAM[FDCNG_VQ_MAX_LEN][FDCNG_VQ_DCT_MAXTRUNC];
    2173             :     int32_t tmp;
    2174             : 
    2175        1313 :     invTrfMatrix = (float *) tmpRAM;
    2176        1313 :     create_IDCT_N_Matrix( invTrfMatrix, FDCNG_VQ_MAX_LEN, FDCNG_VQ_DCT_MAXTRUNC, sizeof( tmpRAM ) / ( sizeof( float ) ) );
    2177             : 
    2178        1313 :     is_out_ms = 0;
    2179        1313 :     if ( hCPE->hCoreCoder[0]->cng_sba_flag )
    2180             :     {
    2181           0 :         is_out_ms = 1;
    2182             :     }
    2183             : 
    2184        1313 :     N = 0; /* to avoid compilation warning */
    2185             : 
    2186        3939 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    2187             :     {
    2188        2626 :         sts[ch] = hCPE->hCoreCoder[ch];
    2189        2626 :         ms_ptr[ch] = &logNoiseEst[ch][0];
    2190        2626 :         lr_ptr[ch] = &sts[ch]->hFdCngDec->hFdCngCom->sidNoiseEst[0];
    2191             :     }
    2192             : 
    2193             :     /* decode noise shapes and gains */
    2194        3939 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    2195             :     {
    2196        2626 :         sts[ch] = hCPE->hCoreCoder[ch];
    2197        2626 :         hFdCngCom = ( sts[ch]->hFdCngDec )->hFdCngCom;
    2198        2626 :         N = hFdCngCom->npart;
    2199        2626 :         hFdCngCom->sid_frame_counter++;
    2200             : 
    2201        2626 :         if ( ch )
    2202             :         {
    2203        1313 :             stages = FD_CNG_JOINT_stages_25bits;
    2204             :         }
    2205             :         else
    2206             :         {
    2207        1313 :             stages = FD_CNG_stages_37bits;
    2208             :         }
    2209             : 
    2210             :         /* read bitstream */
    2211       15756 :         for ( i = 0; i < stages; i++ )
    2212             :         {
    2213       13130 :             indices[i] = get_next_indice( sts[ch], bits_37bits[i] );
    2214             :         }
    2215             :         {
    2216        2626 :             gain[ch] = ( (float) get_next_indice( sts[ch], 7 ) - GAIN_Q_OFFSET_IVAS ) / 1.5f;
    2217             :         }
    2218             : 
    2219             :         /* MSVQ decoder */
    2220        2626 :         msvq_dec( cdk_37bits_ivas, NULL, NULL, stages, N, FD_CNG_maxN_37bits, indices, 1, invTrfMatrix, ms_ptr[ch], NULL );
    2221             :     }
    2222             : 
    2223        1313 :     tmp = sts[1]->total_brate;
    2224        1313 :     sts[1]->total_brate = sts[1]->total_brate + 16 * FRAMES_PER_SEC;
    2225             :     /* read the four additional coherence values */
    2226        6565 :     for ( int16_t b = 1; b < MDCT_ST_DTX_NUM_COHERENCE_BANDS; b++ )
    2227             :     {
    2228             :         uint16_t tmp_bit;
    2229             : 
    2230        5252 :         tmp_bit = get_next_indice( sts[1], 4 );
    2231        5252 :         sts[0]->hFdCngDec->hFdCngCom->coherence[b] = (float) tmp_bit / 15.f;
    2232        5252 :         sts[1]->hFdCngDec->hFdCngCom->coherence[b] = sts[0]->hFdCngDec->hFdCngCom->coherence[b];
    2233             :     }
    2234        1313 :     sts[1]->total_brate = tmp;
    2235             : 
    2236        1313 :     if ( sts[0]->hFdCngDec->hFdCngCom->no_side_flag )
    2237             :     {
    2238          41 :         set_zero( ms_ptr[1], NPART );
    2239             :     }
    2240             : 
    2241        1313 :     if ( is_out_ms == 0 )
    2242             :     {
    2243        1313 :         inverseMS( N, ms_ptr[0], ms_ptr[1], 1.f );
    2244             :     }
    2245             : 
    2246        3939 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    2247             :     {
    2248        2626 :         hFdCngCom = sts[ch]->hFdCngDec->hFdCngCom;
    2249       62554 :         for ( p = 0; p < N; p++ )
    2250             :         {
    2251       59928 :             lr_ptr[ch][p] = powf( 10.f, ( ms_ptr[ch][p] + gain[ch] ) / 10.f );
    2252             :         }
    2253             : 
    2254        2626 :         scalebands( hFdCngCom->sidNoiseEst, hFdCngCom->part, hFdCngCom->npart, hFdCngCom->midband, hFdCngCom->nFFTpart, hFdCngCom->stopBand - hFdCngCom->startBand, hFdCngCom->cngNoiseLevel, 1 );
    2255             : 
    2256        2626 :         lpc_from_spectrum( hFdCngCom, hFdCngCom->startBand, hFdCngCom->stopFFTbin, sts[ch]->preemph_fac );
    2257             :     }
    2258             : 
    2259        1313 :     if ( hCPE->nchan_out == 1 && hCPE->last_element_brate <= IVAS_SID_5k2 )
    2260             :     {
    2261             :         /* create proper M noise shape in channel zero after gains have been applied */
    2262        9944 :         for ( p = 0; p < N; p++ )
    2263             :         {
    2264        9534 :             sts[0]->hFdCngDec->hFdCngCom->sidNoiseEst[p] = 0.5f * ( sts[0]->hFdCngDec->hFdCngCom->sidNoiseEst[p] + sts[1]->hFdCngDec->hFdCngCom->sidNoiseEst[p] );
    2265             :         }
    2266             :     }
    2267             : 
    2268        1313 :     return;
    2269             : }
    2270             : 
    2271             : 
    2272             : /*-------------------------------------------------------------------
    2273             :  * FdCngDecodeDiracMDCTStereoSID()
    2274             :  *
    2275             :  * Decode FD-CNG parameters for CNG in 2TC DirAC mode from the bitstream
    2276             :  *-------------------------------------------------------------------*/
    2277             : 
    2278         351 : void FdCngDecodeDiracMDCTStereoSID(
    2279             :     CPE_DEC_HANDLE hCPE /* i/o: CPE decoder state structure     */
    2280             : )
    2281             : {
    2282             :     DEC_CORE_HANDLE sts[CPE_CHANNELS];
    2283             :     HANDLE_FD_CNG_COM hFdCngCom;
    2284             :     float *ms_ptr[CPE_CHANNELS];
    2285             :     float *lr_ptr[CPE_CHANNELS];
    2286             :     float logNoiseEst[CPE_CHANNELS][NPART];
    2287             :     float gain[CPE_CHANNELS];
    2288             :     int16_t indices[FD_CNG_stages_37bits];
    2289             :     int16_t N, i, ch, p;
    2290             :     float *invTrfMatrix;
    2291             :     float tmpRAM[FDCNG_VQ_MAX_LEN][FDCNG_VQ_DCT_MAXTRUNC];
    2292             : 
    2293             : 
    2294         351 :     invTrfMatrix = (float *) tmpRAM; /* dynamically filled  */
    2295         351 :     create_IDCT_N_Matrix( invTrfMatrix, FDCNG_VQ_MAX_LEN, FDCNG_VQ_DCT_MAXTRUNC, sizeof( tmpRAM ) / ( sizeof( float ) ) );
    2296             : 
    2297        1053 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    2298             :     {
    2299         702 :         sts[ch] = hCPE->hCoreCoder[ch];
    2300         702 :         ms_ptr[ch] = &logNoiseEst[ch][0];
    2301         702 :         lr_ptr[ch] = &sts[ch]->hFdCngDec->hFdCngCom->sidNoiseEst[0];
    2302         702 :         ( sts[ch]->hFdCngDec )->hFdCngCom->sid_frame_counter++;
    2303             :     }
    2304             : 
    2305             :     /* decode noise shapes and gains */
    2306         351 :     hFdCngCom = ( sts[0]->hFdCngDec )->hFdCngCom;
    2307         351 :     N = hFdCngCom->npart;
    2308             : 
    2309             :     /* read bitstream */
    2310        2457 :     for ( i = 0; i < FD_CNG_stages_37bits; i++ )
    2311             :     {
    2312        2106 :         indices[i] = get_next_indice( sts[0], bits_37bits[i] );
    2313             :     }
    2314         351 :     gain[0] = ( (float) get_next_indice( sts[0], 7 ) - GAIN_Q_OFFSET_IVAS ) / 1.5f;
    2315         351 :     gain[1] = gain[0];
    2316             : 
    2317             :     /* MSVQ decoder */
    2318         351 :     msvq_dec( cdk_37bits_ivas, NULL, NULL, FD_CNG_stages_37bits, N, FD_CNG_maxN_37bits, indices, 1, invTrfMatrix, ms_ptr[0], NULL );
    2319         351 :     mvr2r( ms_ptr[0], ms_ptr[1], N );
    2320             : 
    2321             :     /*inverseMS( N, ms_ptr[0], ms_ptr[1], 1.f );*/
    2322             : 
    2323        1053 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    2324             :     {
    2325         702 :         hFdCngCom = sts[ch]->hFdCngDec->hFdCngCom;
    2326       17550 :         for ( p = 0; p < N; p++ )
    2327             :         {
    2328       16848 :             lr_ptr[ch][p] = powf( 10.f, ( ms_ptr[ch][p] + gain[ch] ) / 10.f );
    2329             :         }
    2330             : 
    2331             :         /* NB last band energy compensation */
    2332         702 :         if ( hFdCngCom->CngBandwidth == NB )
    2333             :         {
    2334           0 :             lr_ptr[ch][N - 1] *= NB_LAST_BAND_SCALE;
    2335             :         }
    2336         702 :         else if ( hFdCngCom->CngBandwidth == SWB && hFdCngCom->CngBitrate <= ACELP_13k20 )
    2337             :         {
    2338           0 :             lr_ptr[ch][N - 1] *= SWB_13k2_LAST_BAND_SCALE;
    2339             :         }
    2340             : 
    2341         702 :         scalebands( hFdCngCom->sidNoiseEst, hFdCngCom->part, hFdCngCom->npart, hFdCngCom->midband, hFdCngCom->nFFTpart, hFdCngCom->stopBand - hFdCngCom->startBand, hFdCngCom->cngNoiseLevel, 1 );
    2342             : 
    2343         702 :         lpc_from_spectrum( hFdCngCom, hFdCngCom->startBand, hFdCngCom->stopFFTbin, sts[ch]->preemph_fac );
    2344             :     }
    2345             : 
    2346        2106 :     for ( i = 0; i < MDCT_ST_DTX_NUM_COHERENCE_BANDS; i++ )
    2347             :     {
    2348        1755 :         sts[0]->hFdCngDec->hFdCngCom->coherence[i] = 0.0f;
    2349        1755 :         sts[1]->hFdCngDec->hFdCngCom->coherence[i] = 0.0f;
    2350             :     }
    2351             : 
    2352         351 :     if ( hCPE->nchan_out == 1 )
    2353             :     {
    2354             :         /* create proper M noise shape in channel zero after gains have been applied */
    2355           0 :         for ( p = 0; p < N; p++ )
    2356             :         {
    2357           0 :             sts[0]->hFdCngDec->hFdCngCom->sidNoiseEst[p] = 0.5f * ( sts[0]->hFdCngDec->hFdCngCom->sidNoiseEst[p] + sts[1]->hFdCngDec->hFdCngCom->sidNoiseEst[p] );
    2358             :         }
    2359             :     }
    2360             : 
    2361         351 :     return;
    2362             : }

Generated by: LCOV version 1.14