LCOV - code coverage report
Current view: top level - lib_dec - ivas_stereo_cng_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main @ fec202a8f89be4a2f278a9fc377bfb58b58fab11 Lines: 396 410 96.6 %
Date: 2025-09-11 08:49:05 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <stdint.h>
      34             : #include "options.h"
      35             : #include <math.h>
      36             : #include "cnst.h"
      37             : #include "prot.h"
      38             : #include "ivas_prot.h"
      39             : #include "ivas_cnst.h"
      40             : #include "ivas_rom_com.h"
      41             : #ifdef DEBUGGING
      42             : #include "debug.h"
      43             : #endif
      44             : #include "wmc_auto.h"
      45             : 
      46             : /*-------------------------------------------------------------------
      47             :  * Local constants
      48             :  *-------------------------------------------------------------------*/
      49             : 
      50             : #define A_GFILT           0.8f /* LP-filter coefficient for coherence and sidegain */
      51             : #define SKIP_XFADE_FRAMES 2
      52             : 
      53             : /* DTX/CNG */
      54             : #define MAX_K                      50.0f
      55             : #define STEREO_TD_PS_CORR_FILT     0.8f
      56             : #define MAX_XFADE                  50.0f
      57             : #define CM_INIT                    50
      58             : #define CORR_INIT                  8
      59             : #define SID_INIT                   6
      60             : #define STEREO_CNA_LR_CORR_LT_FILT 0.95f /* long-term averaging factor for L/R correlation estimation in stereo CNA */
      61             : #define STEREO_CNA_ILD_LT_FILT     0.9f  /* long-term averaging factor for ILD estimation in stereo CNA */
      62             : #define STEREO_CNA_SOFT_VAD_UP     0.7f  /* long-term averaging factor for upward soft VAD update in stereo CNA */
      63             : #define STEREO_CNA_SOFT_VAD_DN     0.95f /* long-term averaging factor for downward soft VAD update in stereo CNA */
      64             : 
      65             : 
      66             : /*-------------------------------------------------------------------
      67             :  * stereo_dft_dec_sid_coh()
      68             :  *
      69             :  * Decode coherence vector
      70             :  *-------------------------------------------------------------------*/
      71             : 
      72        7946 : void stereo_dft_dec_sid_coh(
      73             :     Decoder_State *st,    /* i/o: decoder state structure     */
      74             :     const int16_t nbands, /* i  : number of DFT stereo bands  */
      75             :     float *coh,           /* i/o: coherence                   */
      76             :     int16_t *nb_bits      /* i/o: number of bits read         */
      77             : )
      78             : {
      79             :     int16_t nr_of_sid_stereo_bits;
      80             :     int16_t i;
      81             :     float alpha;
      82             :     int16_t alpha_index;
      83             :     int16_t alpha_step;
      84             :     float pred;
      85             :     float pred_err;
      86             :     int16_t coh_pred_index;
      87             :     int16_t res_index;
      88             :     float cohBandq[STEREO_DFT_BAND_MAX];
      89             :     const float *pptr;
      90             :     int16_t bits_tmp;
      91             :     int16_t b;
      92             : 
      93        7946 :     nr_of_sid_stereo_bits = ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - SID_FORMAT_NBITS;
      94             : 
      95             :     /* If the coherence is not encoded due to lack of bits set alpha to zero which leads to that the coherence */
      96             :     /* from the previous frame is used. */
      97        7946 :     if ( ( nr_of_sid_stereo_bits - *nb_bits - STEREO_DFT_N_COH_ALPHA_BITS - STEREO_DFT_PRED_NBITS ) > 0 )
      98             :     {
      99             :         /* Read coherence from bitstream  */
     100        7946 :         coh_pred_index = get_next_indice( st, STEREO_DFT_PRED_NBITS ); /* Read predictor index */
     101        7946 :         ( *nb_bits ) += STEREO_DFT_PRED_NBITS;
     102             : 
     103        7946 :         alpha_index = get_next_indice( st, STEREO_DFT_N_COH_ALPHA_BITS ); /* Read alpha index */
     104        7946 :         ( *nb_bits ) += STEREO_DFT_N_COH_ALPHA_BITS;
     105             : 
     106        7946 :         alpha_step = 0;
     107       39730 :         for ( i = 0; i < STEREO_DFT_N_COH_ALPHA_STEPS - 1; i++ )
     108             :         {
     109       31784 :             if ( nr_of_sid_stereo_bits - *nb_bits > dft_cng_coh_alpha_start[i] )
     110             :             {
     111       31784 :                 alpha_step = i + 1;
     112             :             }
     113             :         }
     114        7946 :         alpha = dft_cng_alpha_bits[alpha_step][alpha_index];
     115             :     }
     116             :     else
     117             :     {
     118           0 :         alpha = 0;
     119           0 :         coh_pred_index = 0;
     120           0 :         for ( i = *nb_bits; i < nr_of_sid_stereo_bits; i++ )
     121             :         {
     122           0 :             get_next_indice( st, 1 );
     123           0 :             ( *nb_bits )++;
     124             :         }
     125             :     }
     126             : 
     127        7946 :     pptr = dft_cng_coh_pred[coh_pred_index];
     128        7946 :     pred = 0.4f;
     129             : 
     130       53881 :     for ( b = 0; b < nbands; b++ )
     131             :     {
     132             :         /* Intra-frame prediction */
     133             : 
     134      156420 :         for ( i = 0; i < b; i++ )
     135             :         {
     136      110485 :             pred += ( *pptr++ ) * cohBandq[i];
     137             :         }
     138             :         /* Weighted intra/inter-frame prediction */
     139       45935 :         pred = alpha * pred + ( 1 - alpha ) * coh[b];
     140             : 
     141             :         /* Read residual index from bitstream */
     142       45935 :         if ( *nb_bits < nr_of_sid_stereo_bits ) /* If the bit limit is reached, res_index = 0 is assumed for remaining indices */
     143             :         {
     144       45935 :             bits_tmp = read_GR0( &st->bit_stream[st->next_bit_pos], &res_index, 1 );
     145       45935 :             *nb_bits += bits_tmp;
     146       45935 :             st->next_bit_pos += bits_tmp;
     147             :         }
     148             :         else
     149             :         {
     150           0 :             res_index = 0;
     151             :         }
     152             : 
     153             :         /* Reconstruct */
     154       45935 :         res_index = dft_cng_coh_u2i[res_index];
     155       45935 :         pred_err = usdequant( res_index, -0.4f, 0.1f );
     156       45935 :         cohBandq[b] = pred + pred_err; /* Store for intra-frame prediction */
     157       45935 :         if ( cohBandq[b] > 1 )
     158             :         {
     159           0 :             cohBandq[b] = 1;
     160             :         }
     161       45935 :         else if ( cohBandq[b] < 0 )
     162             :         {
     163         873 :             cohBandq[b] = 0;
     164             :         }
     165       45935 :         coh[b] = cohBandq[b]; /* Update memory for next frame */
     166       45935 :         pred = 0;
     167             :     }
     168             : 
     169             :     /* Remove padding bits */
     170      134290 :     for ( i = *nb_bits; i < nr_of_sid_stereo_bits; i++ )
     171             :     {
     172      126344 :         get_next_indice( st, 1 );
     173      126344 :         ( *nb_bits )++;
     174             :     }
     175             : 
     176             : 
     177        7946 :     return;
     178             : }
     179             : 
     180             : 
     181             : /*-------------------------------------------------------------------
     182             :  * stereo_dft_generate_comfort_noise()
     183             :  *
     184             :  * Generate the comfort noise based on the target noise level for the CLDFB part
     185             :  *-------------------------------------------------------------------*/
     186             : 
     187       63992 : static void stereo_dft_generate_comfort_noise(
     188             :     STEREO_DFT_DEC_DATA_HANDLE hStereoDft,       /* i/o: DFT Stereo decoder handle                  */
     189             :     STEREO_CNG_DEC_HANDLE hStereoCng,            /* i/o: Stereo CNG data structure                  */
     190             :     const int16_t last_element_mode,             /* i  : last element mode                          */
     191             :     Decoder_State *st,                           /* i/o: Core coder decoder state                   */
     192             :     float DFT[CPE_CHANNELS][STEREO_DFT_BUF_MAX], /* i/o: DFT buffers                                */
     193             :     Decoder_State *st1,                          /* i/o: Core coder decoder state secondary channel */
     194             :     const float targetGain,                      /* i  : ICA target gain                            */
     195             :     const int16_t chan,                          /* i  : channel number                             */
     196             :     const int16_t output_frame                   /* i  : output frame size                          */
     197             : )
     198             : {
     199             :     int16_t i, j, k;
     200             :     float *ptr_level, *ptr_shb, *ptr_r, *ptr_i;
     201             :     HANDLE_FD_CNG_COM hFdCngCom;
     202             :     int16_t numSlots;
     203             :     float scale;
     204             :     float lp_noise;
     205             :     float tmp, enr;
     206             :     float shb_shape[L_FRAME16k];
     207             :     float *ptr0, *ptr1, *ptr2;
     208             :     float dmpf[M + 2], Atmp[M + 2];
     209             :     float cngNoiseLevel_upd[L_FRAME16k], cngNoiseLevel_hist[L_FRAME16k - 2];
     210             :     float *ptr_tmp, *ptr_cng;
     211             :     float E0, E1;
     212             :     int16_t b;
     213             :     float *pSideGain;
     214             :     float gamma;
     215             :     float c;
     216             :     float scaleMS;
     217             :     float scaleAvg;
     218             :     float LR_ratio;
     219             :     float factor;
     220             :     float alpha, ftmp;
     221             :     float trigo_dec[STEREO_DFT32MS_N_16k / 2 + 1];
     222             :     const float *pTrigo;
     223             :     int16_t trigo_step;
     224             : 
     225       63992 :     hFdCngCom = st->hFdCngDec->hFdCngCom;
     226             : 
     227       63992 :     push_wmops( "DFT_CNG" );
     228             : 
     229       63992 :     set_f( dmpf, 0.0f, M + 2 );
     230       63992 :     set_f( Atmp, 0.0f, M + 2 );
     231             : 
     232       63992 :     set_zero( DFT[chan], STEREO_DFT_BUF_MAX );
     233             : 
     234       63992 :     enr = 0; /* Eliminates compiler warning. They are always set before they are used */
     235       63992 :     E0 = 0.0f;
     236       63992 :     E1 = 0.0f;
     237       63992 :     lp_noise = 0;
     238             : 
     239       63992 :     if ( chan == 0 )
     240             :     {
     241       31996 :         pSideGain = hStereoDft->side_gain + STEREO_DFT_NBDIV * STEREO_DFT_BAND_MAX;
     242      219382 :         for ( b = 0; b < hStereoDft->nbands; b++ )
     243             :         {
     244      187386 :             if ( hStereoCng->xfade_frame_counter == 0 )
     245             :             {
     246        4586 :                 hStereoDft->g_state[b] = *pSideGain++;
     247             :             }
     248             :             else
     249             :             {
     250      182800 :                 hStereoDft->g_state[b] = ( 1 - A_GFILT ) * *pSideGain++ + A_GFILT * hStereoDft->g_state[b];
     251             :             }
     252             : 
     253      187386 :             if ( hStereoCng->first_SID )
     254             :             {
     255         422 :                 if ( hStereoCng->first_SID_after_TD )
     256             :                 {
     257           0 :                     hStereoCng->cm[b] = hStereoCng->c_LR_LT * hStereoCng->c_LR_LT;
     258             :                 }
     259             :                 else
     260             :                 {
     261         422 :                     hStereoCng->cm[b] = hStereoCng->coh[b];
     262             :                 }
     263             :             }
     264      186964 :             else if ( hStereoCng->nr_dft_frames < CM_INIT && hStereoCng->nr_sid_frames < SID_INIT )
     265             :             {
     266         668 :                 if ( hStereoCng->nr_corr_frames > CORR_INIT )
     267             :                 {
     268           0 :                     hStereoCng->cm[b] = hStereoCng->c_LR_LT * hStereoCng->c_LR_LT;
     269             :                 }
     270             :                 else
     271             :                 {
     272         668 :                     hStereoCng->cm[b] = ( 1 - A_GFILT ) * hStereoCng->coh[b] + A_GFILT * hStereoCng->cm[b];
     273             :                 }
     274             :             }
     275             :             else
     276             :             {
     277      186296 :                 hStereoCng->cm[b] = ( 1 - A_GFILT ) * hStereoCng->coh[b] + A_GFILT * hStereoCng->cm[b];
     278             :             }
     279             :         }
     280             : 
     281       31996 :         if ( hStereoCng->first_SID_after_TD )
     282             :         {
     283          75 :             scaleAvg = 0;
     284         504 :             for ( b = 0; b < hStereoDft->nbands; b++ )
     285             :             {
     286         429 :                 if ( hStereoCng->cm[b] < 0.9f )
     287             :                 {
     288         429 :                     gamma = hStereoCng->cm[b];
     289         429 :                     gamma = gamma / ( 1 - gamma );
     290         429 :                     gamma = sqrtf( gamma + 1 - hStereoDft->g_state[b] * hStereoDft->g_state[b] ) - sqrtf( gamma );
     291             :                 }
     292             :                 else
     293             :                 {
     294           0 :                     gamma = 0;
     295             :                 }
     296             : 
     297         429 :                 LR_ratio = tdm_ratio_tabl[hStereoCng->last_tdm_idx];
     298         429 :                 c = ( ( 1 + hStereoDft->g_state[b] ) * ( 1 + hStereoDft->g_state[b] ) + gamma * gamma ) / ( ( 1 - hStereoDft->g_state[b] ) * ( 1 - hStereoDft->g_state[b] ) + gamma * gamma );
     299         429 :                 scaleMS = ( 1 + c + 2 * sqrtf( c * hStereoCng->cm[b] ) ) / ( 4 * ( c * LR_ratio * LR_ratio + ( 1 - LR_ratio ) * ( 1 - LR_ratio ) * targetGain * targetGain + 2 * LR_ratio * ( 1 - LR_ratio ) * targetGain * sqrtf( c * hStereoCng->cm[b] ) ) );
     300         429 :                 scaleMS = sqrtf( scaleMS );
     301         429 :                 scaleAvg += scaleMS;
     302             :             }
     303          75 :             scaleAvg = scaleAvg / hStereoDft->nbands;
     304             : 
     305          75 :             hStereoDft->scale = scaleAvg;
     306             :         }
     307             :     }
     308             : 
     309       63992 :     if ( st->cng_type == LP_CNG )
     310             :     {
     311       13856 :         set_f( cngNoiseLevel_upd, 0.0f, st->L_frame );
     312             : 
     313             :         /* Deemphasis */
     314       13856 :         dmpf[0] = 1.0f;
     315       13856 :         dmpf[1] = -st->preemph_fac;
     316       13856 :         mvr2r( st->Aq_cng, Atmp, M + 1 );
     317       13856 :         conv( Atmp, dmpf, cngNoiseLevel_upd, M + 2 );
     318             : 
     319       13856 :         if ( st->L_frame == L_FRAME )
     320             :         {
     321        4100 :             pTrigo = hStereoDft->dft_trigo_12k8;
     322        4100 :             trigo_step = STEREO_DFT_TRIGO_SRATE_12k8_STEP * STEREO_DFT_TRIGO_DEC_STEP;
     323             :         }
     324             :         else
     325             :         {
     326        9756 :             pTrigo = hStereoDft->dft_trigo_16k;
     327        9756 :             trigo_step = STEREO_DFT_TRIGO_SRATE_16k_STEP * STEREO_DFT_TRIGO_DEC_STEP;
     328             :         }
     329             : 
     330     1056736 :         for ( i = 0; i < st->L_frame / 4; i++ )
     331             :         {
     332     1042880 :             trigo_dec[i] = pTrigo[i * trigo_step];
     333     1042880 :             trigo_dec[st->L_frame / 2 - i] = pTrigo[i * trigo_step];
     334             :         }
     335       13856 :         trigo_dec[st->L_frame / 4] = pTrigo[st->L_frame / 4 * trigo_step];
     336             : 
     337       13856 :         rfft( cngNoiseLevel_upd, trigo_dec, st->L_frame, -1 );
     338             : 
     339             :         /* Compute 1/|A| */
     340       13856 :         ptr0 = cngNoiseLevel_upd;
     341       13856 :         ptr1 = ptr0 + 2;
     342       13856 :         ptr2 = ptr1 + 1;
     343       13856 :         assert( st->lp_ener > 0.0f );
     344       13856 :         factor = 2.0f * sqrtf( st->lp_ener / st->L_frame * 0.5f ); /* fixed factor  in the loop below */
     345     2085760 :         for ( i = 0; i < st->L_frame / 2 - 1; i++ )
     346             :         {
     347     2071904 :             ftmp = *ptr1 * *ptr1 + *ptr2 * *ptr2;
     348     2071904 :             assert( ftmp > 0.0f );
     349     2071904 :             *ptr0++ = factor * inv_sqrt( ftmp );
     350     2071904 :             ptr1 += 2;
     351     2071904 :             ptr2 += 2;
     352             :         }
     353             : 
     354       13856 :         if ( min( output_frame, L_FRAME32k ) - hFdCngCom->stopFFTbin > 0 )
     355             :         {
     356             :             /* Transform shb LP spectrum */
     357       11920 :             set_f( shb_shape, 0.0f, L_FRAME16k );
     358       11920 :             mvr2r( st->hTdCngDec->shb_lpcCNG, shb_shape, LPC_SHB_ORDER + 1 );
     359             : 
     360       11920 :             if ( st->L_frame != L_FRAME16k )
     361             :             {
     362        4100 :                 pTrigo = hStereoDft->dft_trigo_16k;
     363        4100 :                 trigo_step = STEREO_DFT_TRIGO_SRATE_16k_STEP * STEREO_DFT_TRIGO_DEC_STEP;
     364             : 
     365      332100 :                 for ( i = 0; i < L_FRAME16k / 4; i++ )
     366             :                 {
     367      328000 :                     trigo_dec[i] = pTrigo[i * trigo_step];
     368      328000 :                     trigo_dec[L_FRAME16k / 2 - i] = pTrigo[i * trigo_step];
     369             :                 }
     370        4100 :                 trigo_dec[L_FRAME16k / 4] = pTrigo[L_FRAME16k / 4 * trigo_step];
     371             :             }
     372             : 
     373       11920 :             rfft( shb_shape, trigo_dec, L_FRAME16k, -1 );
     374             : 
     375             :             /* Compute 1/|A| */
     376       11920 :             enr = shb_shape[0] * shb_shape[0] + shb_shape[1] * shb_shape[1];
     377       11920 :             ptr0 = shb_shape;
     378       11920 :             ptr1 = ptr0 + 2;
     379       11920 :             ptr2 = ptr1 + 1;
     380             : 
     381     1907200 :             for ( i = 0; i < L_FRAME16k / 2 - 1; i++ )
     382             :             {
     383     1895280 :                 ftmp = ( *ptr1 * *ptr1 + *ptr2 * *ptr2 );
     384     1895280 :                 assert( ftmp > 0.0f );
     385     1895280 :                 ftmp = 1.0f / ftmp;
     386             :                 /* in float:
     387             :                      both a = "div"=(1/(x^2+y^2) and sqrt(a)  is used and summed up in the same loop.
     388             : 
     389             :                   in BASOP:
     390             :                     sum up  using inv_sqrt( *ptr1 * *ptr1 + *ptr2 * *ptr2 ), in this loop
     391             :                     and then sum up  enr = sum( *ptr0 * *ptr0 ),  in a subsequent MAC loop  */
     392     1895280 :                 enr += ftmp;
     393     1895280 :                 *ptr0++ = sqrtf( ftmp );
     394     1895280 :                 ptr1 += 2;
     395     1895280 :                 ptr2 += 2;
     396             :             }
     397             :         }
     398             : 
     399             :         /* Update CNG noise level from MS noise estimation */
     400       13856 :         mvr2r( st->hFdCngDec->bandNoiseShape, cngNoiseLevel_hist, hFdCngCom->stopFFTbin - hFdCngCom->startBand );
     401       13856 :         ptr_cng = cngNoiseLevel_hist;
     402     2085760 :         for ( i = 0; i < ( st->last_L_frame - hFdCngCom->startBand ) / 2; i++ )
     403             :         {
     404     2071904 :             tmp = *( cngNoiseLevel_hist + i * 2 );
     405     2071904 :             tmp += *( cngNoiseLevel_hist + i * 2 + 1 );
     406     2071904 :             *ptr_cng++ = tmp * 0.5f;
     407             :         }
     408             : 
     409       13856 :         if ( last_element_mode == IVAS_CPE_TD && chan == 0 && hStereoCng->xfade_frame_counter == 0 && !( hFdCngCom->msFrCnt_init_counter < hFdCngCom->msFrCnt_init_thresh ) )
     410             :         {
     411          25 :             ptr_cng = cngNoiseLevel_hist + hFdCngCom->startBand;
     412          25 :             ptr_tmp = cngNoiseLevel_upd + hFdCngCom->startBand;
     413        4000 :             for ( i = 0; i < ( st->last_L_frame - hFdCngCom->startBand ) / 2; i++ )
     414             :             {
     415        3975 :                 E0 += *ptr_cng++;
     416        3975 :                 E1 += ( *ptr_tmp ) * ( *ptr_tmp );
     417        3975 :                 ptr_tmp++;
     418             :             }
     419             : 
     420          25 :             tmp = sqrtf( E0 / E1 );
     421          25 :             if ( tmp < 1 )
     422             :             {
     423          25 :                 hStereoCng->xfade_length = (int16_t) ( -MAX_XFADE * tmp + MAX_XFADE );
     424             :             }
     425             :             else
     426             :             {
     427           0 :                 hStereoCng->xfade_length = (int16_t) ( -MAX_XFADE * ( 1 / tmp ) + MAX_XFADE );
     428             :             }
     429             :         }
     430             : 
     431       13856 :         if ( hStereoCng->xfade_frame_counter < hStereoCng->xfade_length )
     432             :         {
     433         344 :             ptr_cng = cngNoiseLevel_hist + hFdCngCom->startBand;
     434         344 :             ptr_tmp = cngNoiseLevel_upd + hFdCngCom->startBand;
     435       55040 :             for ( i = 0; i < ( st->last_L_frame - hFdCngCom->startBand ) / 2; i++ )
     436             :             {
     437       54696 :                 tmp = hStereoCng->xfade_frame_counter / (float) hStereoCng->xfade_length;
     438       54696 :                 *ptr_tmp = tmp * *ptr_tmp + ( 1 - tmp ) * sqrtf( *ptr_cng++ ) * hStereoDft->scale;
     439       54696 :                 ptr_tmp++;
     440             :             }
     441             :         }
     442             : 
     443       41568 :         for ( k = 0; k < STEREO_DFT_NBDIV; k++ )
     444             :         {
     445             :             /* low band */
     446       27712 :             ptr_level = cngNoiseLevel_upd;
     447       27712 :             ptr_r = DFT[chan] + hFdCngCom->startBand + k * STEREO_DFT32MS_N_MAX;
     448       27712 :             ptr_i = ptr_r + 1;
     449       27712 :             scale = output_frame * 0.5f;
     450             : 
     451     4171520 :             for ( i = 0; i < ( hFdCngCom->stopFFTbin - hFdCngCom->startBand ) / 2; i++ )
     452             :             {
     453             :                 /* Real part in FFT bins */
     454     4143808 :                 rand_gauss( ptr_r, &st->hTdCngDec->cng_seed );
     455     4143808 :                 tmp = scale * *ptr_level;
     456     4143808 :                 ( *ptr_r ) *= tmp;
     457     4143808 :                 ptr_r += 2;
     458             :                 /* Imaginary part in FFT bins */
     459     4143808 :                 rand_gauss( ptr_i, &st->hTdCngDec->cng_seed );
     460     4143808 :                 ( *ptr_i ) *= tmp;
     461     4143808 :                 ptr_i += 2;
     462     4143808 :                 ptr_level++;
     463             :             }
     464             : 
     465       27712 :             if ( min( output_frame, L_FRAME32k ) - hFdCngCom->stopFFTbin > 0 )
     466             :             {
     467             : 
     468             :                 /* high band generation, flipped spectrum */
     469       23840 :                 assert( enr != 0.0f );
     470       23840 :                 scale = sqrtf( powf( 10, 0.1f * st->hTdCngDec->shb_cng_gain ) / enr );
     471       23840 :                 ptr_shb = shb_shape + L_FRAME16k / 2 - 1;
     472             :                 /* Averaging for Nyquist frequency */
     473       23840 :                 *ptr_shb = 0.5f * ( cngNoiseLevel_upd[( hFdCngCom->stopFFTbin - hFdCngCom->startBand ) / 2 - 1] / scale + *( ptr_shb - 1 ) );
     474             : 
     475       23840 :                 ptr_r = DFT[chan] + hFdCngCom->stopFFTbin + k * STEREO_DFT32MS_N_MAX;
     476       23840 :                 ptr_i = ptr_r + 1;
     477             : 
     478     2920640 :                 for ( i = 0; i < ( min( output_frame, hFdCngCom->regularStopBand * 16 ) - hFdCngCom->stopFFTbin ) / 2; i++ )
     479             :                 {
     480             :                     /* Real part in FFT bins */
     481     2896800 :                     rand_gauss( ptr_r, &st->hTdCngDec->cng_seed );
     482     2896800 :                     ( *ptr_r ) *= *ptr_shb;
     483     2896800 :                     ptr_r += 2;
     484             :                     /* Imaginary part in FFT bins */
     485     2896800 :                     rand_gauss( ptr_i, &st->hTdCngDec->cng_seed );
     486     2896800 :                     ( *ptr_i ) *= *ptr_shb;
     487     2896800 :                     ptr_i += 2;
     488     2896800 :                     ptr_shb--;
     489             :                 }
     490             : 
     491             :                 /* rescale */
     492       23840 :                 scale *= output_frame * 0.5f;
     493       23840 :                 ptr_r = DFT[chan] + hFdCngCom->stopFFTbin + k * STEREO_DFT32MS_N_MAX;
     494       23840 :                 ptr_i = ptr_r + 1;
     495     2920640 :                 for ( i = 0; i < ( min( output_frame, hFdCngCom->regularStopBand * 16 ) - hFdCngCom->stopFFTbin ) / 2; i++ )
     496             :                 {
     497     2896800 :                     ( *ptr_r ) *= scale;
     498     2896800 :                     ( *ptr_i ) *= scale;
     499     2896800 :                     ptr_r += 2;
     500     2896800 :                     ptr_i += 2;
     501             :                 }
     502             :             }
     503             :         }
     504             : 
     505             :         /* Expand cngNoiseLevel from 0-159 to 0-318, compute noise level */
     506       13856 :         lp_noise = 0.0f;
     507       13856 :         ptr_level = hFdCngCom->cngNoiseLevel + hFdCngCom->stopFFTbin - hFdCngCom->startBand - 1;
     508       13856 :         ptr_tmp = cngNoiseLevel_upd + ( hFdCngCom->stopFFTbin - hFdCngCom->startBand ) / 2 - 1;
     509     2085760 :         for ( i = 0; i < ( hFdCngCom->stopFFTbin - hFdCngCom->startBand ) / 2; i++ )
     510             :         {
     511     2071904 :             *ptr_level-- = *ptr_tmp * *ptr_tmp;
     512     2071904 :             ptr_tmp--;
     513     2071904 :             *ptr_level = *( ptr_level + 1 );
     514     2071904 :             lp_noise += 2 * *ptr_level--;
     515             :         }
     516             :     }
     517             :     else
     518             :     {
     519             :         /* FD-CNG */
     520       50136 :         if ( !( hFdCngCom->msFrCnt_init_counter < hFdCngCom->msFrCnt_init_thresh ) )
     521             :         {
     522       50136 :             if ( hStereoCng->xfade_frame_counter <= MAX_K && hStereoCng->last_act_element_mode == IVAS_CPE_TD && chan == 0 )
     523             :             {
     524             :                 /* Fade MS -> SID/MS */
     525         226 :                 j = 0;
     526        4520 :                 for ( k = 0; k < ( hFdCngCom->nFFTpart - 2 ); k++ )
     527             :                 {
     528        4294 :                     factor = ( hFdCngCom->sidNoiseEstLp[k] + DELTA ) / ( st->hFdCngDec->partNoiseShape[k] + DELTA );
     529        4294 :                     factor = min( hStereoDft->scale + ( 1 / MAX_K ) * ( factor - hStereoDft->scale ) * hStereoCng->xfade_frame_counter, factor );
     530       51980 :                     for ( ; j <= hFdCngCom->part[k]; j++ )
     531             :                     {
     532       47686 :                         hFdCngCom->cngNoiseLevel[j] = st->hFdCngDec->bandNoiseShape[j] * factor;
     533             :                     }
     534             :                 }
     535             :             }
     536             :         }
     537       50136 :         scale = output_frame * 0.5f;
     538       50136 :         numSlots = hFdCngCom->numSlots / 2;
     539      150408 :         for ( k = 0; k < STEREO_DFT_NBDIV; k++ )
     540             :         {
     541      100272 :             ptr_level = hFdCngCom->cngNoiseLevel;
     542      100272 :             ptr_r = DFT[chan] + hFdCngCom->startBand + k * STEREO_DFT32MS_N_MAX;
     543      100272 :             ptr_i = ptr_r + 1;
     544    14433536 :             for ( i = 0; i < ( hFdCngCom->stopFFTbin - hFdCngCom->startBand ) / 2; i++ )
     545             :             {
     546             :                 /* Real part in FFT bins */
     547    14333264 :                 tmp = *ptr_level++;
     548    14333264 :                 tmp += *ptr_level++;
     549    14333264 :                 tmp = tmp * 0.5f;
     550    14333264 :                 rand_gauss( ptr_r, &st->hTdCngDec->cng_seed );
     551    14333264 :                 tmp = sqrtf( tmp ) * scale;
     552    14333264 :                 ( *ptr_r ) *= tmp;
     553    14333264 :                 ptr_r += 2;
     554             :                 /* Imaginary part in FFT bins */
     555    14333264 :                 rand_gauss( ptr_i, &st->hTdCngDec->cng_seed );
     556    14333264 :                 ( *ptr_i ) *= tmp;
     557    14333264 :                 ptr_i += 2;
     558             :             }
     559      100272 :             ptr_level = hFdCngCom->cngNoiseLevel + hFdCngCom->stopFFTbin - hFdCngCom->startBand;
     560      100272 :             ptr_r = DFT[chan] + hFdCngCom->stopFFTbin + k * STEREO_DFT32MS_N_MAX;
     561      100272 :             ptr_i = ptr_r + 1;
     562     1523700 :             for ( j = hFdCngCom->numCoreBands; j < hFdCngCom->regularStopBand; j++ )
     563             :             {
     564    12810852 :                 for ( i = 0; i < numSlots; i++ )
     565             :                 {
     566             :                     /* Real part in FFT bins */
     567    11387424 :                     rand_gauss( ptr_r, &st->hTdCngDec->cng_seed );
     568    11387424 :                     tmp = sqrtf( *ptr_level ) * scale;
     569    11387424 :                     ( *ptr_r ) *= tmp;
     570    11387424 :                     ptr_r += 2;
     571             :                     /* Imaginary part in FFT bins */
     572    11387424 :                     rand_gauss( ptr_i, &st->hTdCngDec->cng_seed );
     573    11387424 :                     ( *ptr_i ) *= tmp;
     574    11387424 :                     ptr_i += 2;
     575             :                 }
     576     1423428 :                 ptr_level++;
     577             :             }
     578             :         }
     579             : 
     580             :         /* Compute noise level */
     581       50136 :         lp_noise = 0.0f;
     582       50136 :         ptr_level = hFdCngCom->cngNoiseLevel;
     583    14383400 :         for ( i = 0; i < hFdCngCom->stopFFTbin - hFdCngCom->startBand; i++ )
     584             :         {
     585    14333264 :             lp_noise += *ptr_level++;
     586             :         }
     587             :     }
     588             : 
     589       63992 :     if ( hStereoCng->last_act_element_mode == IVAS_CPE_TD && chan > 0 )
     590             :     {
     591         421 :         st1->lp_noise = 0.9f * st1->lp_noise + 0.1f * 10.f * log10f( lp_noise + DELTA );
     592             :     }
     593       63571 :     else if ( chan == 0 )
     594             :     {
     595       31996 :         st->hFdCngDec->lp_noise = 0.9f * st->hFdCngDec->lp_noise + 0.1f * 10.f * log10f( lp_noise + DELTA );
     596       31996 :         st->lp_noise = st->hFdCngDec->lp_noise;
     597       31996 :         st->hFdCngDec->hFdCngCom->flag_noisy_speech = ( st->hFdCngDec->lp_speech - st->hFdCngDec->lp_noise ) < 28.f;
     598       31996 :         st->hFdCngDec->hFdCngCom->likelihood_noisy_speech = 0.99f * st->hFdCngDec->hFdCngCom->likelihood_noisy_speech + 0.01f * st->hFdCngDec->hFdCngCom->flag_noisy_speech;
     599             :     }
     600             : 
     601       63992 :     if ( chan == 0 && st->core_brate <= SID_2k40 )
     602             :     {
     603             :         /* update smoothed periodogram used by stereo CNA in SID and NO_DATA frames from cngNoiseLevel */
     604     9270532 :         for ( i = hFdCngCom->startBand; i < hFdCngCom->stopFFTbin; i++ )
     605             :         {
     606     9238536 :             ftmp = hFdCngCom->cngNoiseLevel[i - hFdCngCom->startBand];
     607             : 
     608     9238536 :             if ( !st->hFdCngDec->first_cna_noise_updated )
     609             :             {
     610             :                 /* very first update */
     611       11188 :                 alpha = 0.0f;
     612             :             }
     613             :             else
     614             :             {
     615     9227348 :                 alpha = 0.95f;
     616     9227348 :                 if ( st->hFdCngDec->smoothed_psd[i] > 0.0f && ftmp > 2.5f * st->hFdCngDec->smoothed_psd[i] )
     617             :                 {
     618             :                     /* prevent abrupt upward update steps */
     619      761197 :                     ftmp = 2.5f * st->hFdCngDec->smoothed_psd[i];
     620             :                 }
     621     8466151 :                 else if ( ftmp < st->hFdCngDec->smoothed_psd[i] )
     622             :                 {
     623             :                     /* faster downward updates */
     624     3004089 :                     alpha = 0.7f;
     625             :                 }
     626             :             }
     627             : 
     628             :             /* smoothing */
     629     9238536 :             st->hFdCngDec->smoothed_psd[i] = alpha * st->hFdCngDec->smoothed_psd[i] + ( 1 - alpha ) * ftmp;
     630             :         }
     631             : 
     632             :         /* update msNoiseEst in SID and NO_DATA frames */
     633       31996 :         bandcombinepow( &st->hFdCngDec->smoothed_psd[hFdCngCom->startBand], hFdCngCom->stopFFTbin - hFdCngCom->startBand, st->hFdCngDec->part_shaping, st->hFdCngDec->nFFTpart_shaping, st->hFdCngDec->psize_inv_shaping, st->hFdCngDec->msNoiseEst );
     634             : 
     635       31996 :         st->hFdCngDec->first_cna_noise_updated = 1;
     636       31996 :         mvr2r( st->hFdCngDec->msNoiseEst, st->hFdCngDec->msPeriodog_ST, st->hFdCngDec->nFFTpart_shaping );
     637       31996 :         st->hFdCngDec->ms_last_inactive_bwidth = st->bwidth;
     638             :     }
     639             : 
     640       63992 :     pop_wmops();
     641       63992 :     return;
     642             : }
     643             : 
     644             : 
     645             : /*-------------------------------------------------------------------
     646             :  * stereo_dtf_cng()
     647             :  *
     648             :  * DFT stereo CNG
     649             :  *-------------------------------------------------------------------*/
     650             : 
     651      767927 : void stereo_dtf_cng(
     652             :     CPE_DEC_HANDLE hCPE,                         /* i/o: CPE decoder structure    */
     653             :     const int32_t ivas_total_brate,              /* i  : IVAS total bitrate       */
     654             :     float DFT[CPE_CHANNELS][STEREO_DFT_BUF_MAX], /* i/o: DFT buffers              */
     655             :     const int16_t output_frame                   /* i  : output frame size        */
     656             : )
     657             : {
     658             :     Decoder_State **sts;
     659             :     int16_t n;
     660             : 
     661      767927 :     sts = hCPE->hCoreCoder;
     662             : 
     663      767927 :     if ( hCPE->hStereoCng != NULL )
     664             :     {
     665      767927 :         if ( hCPE->hStereoCng->nr_dft_frames < CM_INIT )
     666             :         {
     667      193726 :             hCPE->hStereoCng->nr_dft_frames++;
     668             :         }
     669             : 
     670      767927 :         if ( ivas_total_brate <= IVAS_SID_5k2 )
     671             :         {
     672       31996 :             if ( hCPE->hStereoCng->nr_sid_frames < SID_INIT && ivas_total_brate == IVAS_SID_5k2 )
     673             :             {
     674         398 :                 hCPE->hStereoCng->nr_sid_frames++;
     675             :             }
     676             : 
     677       95988 :             for ( n = 0; n < CPE_CHANNELS; n++ )
     678             :             {
     679       63992 :                 stereo_dft_generate_comfort_noise( hCPE->hStereoDft, hCPE->hStereoCng, hCPE->last_element_mode, sts[0], DFT, sts[1], hCPE->hStereoTCA->targetGain, n, output_frame );
     680             :             }
     681             :         }
     682             :     }
     683             : 
     684      767927 :     return;
     685             : }
     686             : 
     687             : 
     688             : /*-------------------------------------------------------------------
     689             :  * stereo_cng_dec_update()
     690             :  *
     691             :  * Update counters used for TD->DFT-CNG cross fade and stereo SID parameters
     692             :  *-------------------------------------------------------------------*/
     693             : 
     694     1227647 : void stereo_cng_dec_update(
     695             :     CPE_DEC_HANDLE hCPE,           /* i/o: CPE decoder structure    */
     696             :     const int32_t ivas_total_brate /* i  : IVAS total bitrate       */
     697             : )
     698             : {
     699     1227647 :     if ( hCPE->hCoreCoder[0]->core_brate > SID_2k40 )
     700             :     {
     701     1168584 :         hCPE->hStereoCng->last_act_element_mode = hCPE->element_mode;
     702     1168584 :         if ( hCPE->hStereoCng->active_frame_counter > SKIP_XFADE_FRAMES )
     703             :         {
     704     1092186 :             hCPE->hStereoCng->xfade_frame_counter = 0;
     705     1092186 :             hCPE->hStereoCng->xfade_length = 0;
     706             :         }
     707     1168584 :         if ( hCPE->hStereoCng->active_frame_counter < MAX_FRAME_COUNTER )
     708             :         {
     709      578186 :             hCPE->hStereoCng->active_frame_counter++;
     710             :         }
     711             :     }
     712             :     else
     713             :     {
     714       59063 :         hCPE->hStereoCng->active_frame_counter = 0;
     715       59063 :         if ( hCPE->hStereoCng->xfade_frame_counter < MAX_FRAME_COUNTER )
     716             :         {
     717       58750 :             hCPE->hStereoCng->xfade_frame_counter++;
     718             :         }
     719             :     }
     720             : 
     721     1227647 :     if ( hCPE->element_mode == IVAS_CPE_DFT )
     722             :     {
     723     1188513 :         if ( ivas_total_brate == IVAS_SID_5k2 || ivas_total_brate == FRAME_NO_DATA )
     724             :         {
     725       59063 :             hCPE->hStereoCng->prev_sid_nodata = 1;
     726             :         }
     727             :         else
     728             :         {
     729     1129450 :             hCPE->hStereoCng->prev_sid_nodata = 0;
     730             :         }
     731             :     }
     732             : 
     733     1227647 :     hCPE->hCoreCoder[0]->hFdCngDec->hFdCngCom->active_frame_counter = hCPE->hStereoCng->active_frame_counter;
     734             : 
     735     1227647 :     return;
     736             : }
     737             : 
     738             : 
     739             : /*-------------------------------------------------------------------*
     740             :  * Function stereo_cng_compute_PScorr()
     741             :  *
     742             :  * CNA for TD stereo, compute PS correlation
     743             :  *-------------------------------------------------------------------*/
     744             : 
     745       39134 : void stereo_cng_compute_PScorr(
     746             :     float *output[CPE_CHANNELS], /* i  : Output signal     */
     747             :     float *c_PS_LT,              /* i/o: Correlation       */
     748             :     const int16_t L_frame_0,     /* i  : L_frame channel 0 */
     749             :     const int16_t L_frame_1      /* i  : L_frame channel 1 */
     750             : )
     751             : {
     752             :     int16_t i;
     753             :     float enrP, enrS, dotPS;
     754             :     float c_PS;
     755             :     float outputResamp[L_FRAME16k];
     756             :     float *outputPptr, *outputSptr;
     757             : 
     758       39134 :     enrP = 0.0f;
     759       39134 :     enrS = 0.0f;
     760       39134 :     dotPS = 0.0f;
     761             : 
     762       39134 :     if ( L_frame_0 > L_frame_1 )
     763             :     {
     764       26272 :         outputPptr = output[0];
     765       26272 :         lerp( output[1], outputResamp, L_frame_0, L_frame_1 );
     766       26272 :         outputSptr = outputResamp;
     767             :     }
     768       12862 :     else if ( L_frame_1 > L_frame_0 )
     769             :     {
     770           0 :         outputSptr = output[1];
     771           0 :         lerp( output[0], outputResamp, L_frame_1, L_frame_0 );
     772           0 :         outputPptr = outputResamp;
     773             :     }
     774             :     else
     775             :     {
     776       12862 :         outputPptr = output[0];
     777       12862 :         outputSptr = output[1];
     778             :     }
     779             : 
     780    11738846 :     for ( i = 0; i < L_frame_0; i++ )
     781             :     {
     782    11699712 :         enrP += *outputPptr * *outputPptr;
     783    11699712 :         enrS += *outputSptr * *outputSptr;
     784    11699712 :         dotPS += *outputPptr++ * *outputSptr++;
     785             :     }
     786             : 
     787       39134 :     c_PS = ( dotPS + EPSILON ) * inv_sqrt( enrP * enrS + EPSILON );
     788             : 
     789       39134 :     *c_PS_LT = STEREO_TD_PS_CORR_FILT * *c_PS_LT + ( 1 - STEREO_TD_PS_CORR_FILT ) * c_PS;
     790             : 
     791       39134 :     return;
     792             : }
     793             : 
     794             : 
     795             : /*-------------------------------------------------------------------*
     796             :  * Function stereo_cng_compute_LRcorr()
     797             :  *
     798             :  * CNA for TD stereo, compute LR correlation
     799             :  *-------------------------------------------------------------------*/
     800             : 
     801       39134 : static void stereo_cng_compute_LRcorr(
     802             :     CPE_DEC_HANDLE hCPE,         /* i/o: CPE handle                           */
     803             :     float *output[CPE_CHANNELS], /* i  : Output signal                        */
     804             :     const int16_t output_frame,  /* i  : Output frame length                  */
     805             :     const int16_t tdm_ratio_idx  /* i  : TDM ratio index                      */
     806             : )
     807             : {
     808             :     int16_t i;
     809             :     float c_LR, c, tmp, sc;
     810             :     float enrL, enrR, dotLR;
     811             :     float beta;
     812             : 
     813       39134 :     beta = tdm_ratio_tabl[tdm_ratio_idx];
     814             : 
     815       39134 :     enrL = 0.0f;
     816       39134 :     enrR = 0.0f;
     817       39134 :     dotLR = 0.0f;
     818    30390814 :     for ( i = 0; i < output_frame; i++ )
     819             :     {
     820    30351680 :         enrL += output[0][i] * output[0][i];
     821    30351680 :         enrR += output[1][i] * output[1][i];
     822    30351680 :         dotLR += output[0][i] * output[1][i];
     823             :     }
     824             : 
     825       39134 :     c_LR = fabsf( dotLR + EPSILON ) * inv_sqrt( enrL * enrR + EPSILON );
     826       39134 :     c = ( enrL + DELTA ) / ( enrR + DELTA );
     827       39134 :     hCPE->hStereoTD->c_LR_LT = STEREO_TD_PS_CORR_FILT * hCPE->hStereoTD->c_LR_LT + ( 1 - STEREO_TD_PS_CORR_FILT ) * c_LR;
     828       39134 :     hCPE->hStereoCng->c_LR_LT = hCPE->hStereoTD->c_LR_LT;
     829             : 
     830       39134 :     tmp = 2 * beta * ( 1 - beta ) * sqrtf( c ) * hCPE->hStereoTD->c_LR_LT;
     831       39134 :     sc = ( ( 1 - beta ) * ( 1 - beta ) * c + beta * beta - tmp + DELTA ) / ( beta * beta * c + ( 1 - beta ) * ( 1 - beta ) + tmp + DELTA );
     832       39134 :     sc = min( sc, 4.0f );
     833       39134 :     hCPE->hStereoTD->SP_ratio_LT = 0.9f * hCPE->hStereoTD->SP_ratio_LT + ( 1 - 0.9f ) * sqrtf( sc );
     834             : 
     835       39134 :     if ( hCPE->hStereoCng->nr_corr_frames < CM_INIT )
     836             :     {
     837       11934 :         hCPE->hStereoCng->nr_corr_frames++;
     838             :     }
     839             : 
     840       39134 :     hCPE->hStereoCng->nr_dft_frames = 0;
     841             : 
     842       39134 :     return;
     843             : }
     844             : 
     845             : 
     846             : /*-------------------------------------------------------------------*
     847             :  * FindEmEs()
     848             :  *
     849             :  * Find the energie ratio between the mono and the side
     850             :  *-------------------------------------------------------------------*/
     851             : 
     852      722679 : static void FindEmEs(
     853             :     const float *ch1,  /* i  : Left channel          */
     854             :     const float *ch2,  /* i  : right channel         */
     855             :     const int16_t len, /* i  : length                */
     856             :     float *lt_es_em    /* i/o: LT energy ratio       */
     857             : )
     858             : {
     859             :     int16_t i;
     860             :     float mono_i, ener, es_em;
     861             :     float side_i, ener_side;
     862             : 
     863      722679 :     ener = 1e-6f;
     864      722679 :     ener_side = 1e-6f;
     865             : 
     866   491681399 :     for ( i = 0; i < len; i++ )
     867             :     {
     868   490958720 :         mono_i = ( ch1[i] + ch2[i] ) / 2.0f;
     869   490958720 :         ener += mono_i * mono_i;
     870   490958720 :         side_i = ( ch1[i] - ch2[i] ) / 2.0f;
     871   490958720 :         ener_side += side_i * side_i;
     872             :     }
     873             : 
     874             :     /**es_em = 10 * ( log10( sqrt( ener_side / len ) ) - log10( sqrt( ener / len ) ) ); */
     875      722679 :     es_em = 5 * ( logf( sqrtf( ener_side / ener ) ) / logf( 10 ) );
     876             : 
     877             :     /* long-term estimate */
     878      722679 :     *lt_es_em = 0.4f * *lt_es_em + 0.6f * es_em;
     879             : 
     880      722679 :     return;
     881             : }
     882             : 
     883             : 
     884             : /*-------------------------------------------------------------------*
     885             :  * Function stereo_cna_update_params()
     886             :  *
     887             :  * compute LR correlation and update long-term parameters for stereo CNA
     888             :  *-------------------------------------------------------------------*/
     889             : 
     890     4279775 : void stereo_cna_update_params(
     891             :     CPE_DEC_HANDLE hCPE,         /* i/o: CPE decoder structure       */
     892             :     float *output[CPE_CHANNELS], /* i  : Output signal               */
     893             :     const int16_t output_frame,  /* i  : Output frame length         */
     894             :     const int16_t tdm_ratio_idx  /* i  : TDM ratio index             */
     895             : )
     896             : {
     897             :     int16_t i;
     898             :     float c, c_LR, c_ILD, enrL, enrR, dotLR;
     899             :     Decoder_State **sts;
     900             :     HANDLE_FD_CNG_DEC hFdCngDec;
     901             : 
     902     4279775 :     sts = hCPE->hCoreCoder;
     903             : 
     904     4279775 :     hFdCngDec = sts[0]->hFdCngDec;
     905             : 
     906     4279775 :     if ( hCPE->element_mode == IVAS_CPE_DFT )
     907             :     {
     908     1188513 :         if ( hCPE->nchan_out > 1 )
     909             :         {
     910      683545 :             FindEmEs( output[0], output[1], output_frame, &hCPE->lt_es_em );
     911             :         }
     912             :         else
     913             :         {
     914      504968 :             hCPE->lt_es_em = 0.0f;
     915             :         }
     916             :     }
     917     3091262 :     else if ( hCPE->element_mode == IVAS_CPE_TD )
     918             :     {
     919       39134 :         FindEmEs( output[0], output[1], output_frame, &hCPE->lt_es_em );
     920             : 
     921       39134 :         hCPE->hStereoCng->first_SID_after_TD = 1;
     922       39134 :         stereo_cng_compute_LRcorr( hCPE, output, output_frame, tdm_ratio_idx );
     923             :     }
     924             :     else
     925             :     {
     926     3052128 :         hFdCngDec->first_cna_noise_updated = 0;
     927             : 
     928     3052128 :         return;
     929             :     }
     930             : 
     931     1227647 :     enrL = 0.0f;
     932     1227647 :     enrR = 0.0f;
     933     1227647 :     dotLR = 0.0f;
     934     1227647 :     if ( hCPE->element_mode == IVAS_CPE_TD || ( hCPE->element_mode == IVAS_CPE_DFT && sts[0]->core_brate > SID_2k40 && sts[0]->VAD == 0 ) )
     935             :     {
     936      106882 :         if ( hCPE->nchan_out == 1 )
     937             :         {
     938       30069 :             c_LR = 1;
     939       30069 :             c_ILD = 0;
     940             :         }
     941             :         else
     942             :         {
     943    59132173 :             for ( i = 0; i < output_frame; i++ )
     944             :             {
     945    59055360 :                 enrL += output[0][i] * output[0][i];
     946    59055360 :                 enrR += output[1][i] * output[1][i];
     947    59055360 :                 dotLR += output[0][i] * output[1][i];
     948             :             }
     949             : 
     950             :             /* estimate L/R correlation factor and ILD in time domain */
     951       76813 :             c_LR = fabsf( dotLR + EPSILON ) * inv_sqrt( enrL * enrR + EPSILON );
     952       76813 :             c = ( enrL + DELTA ) / ( enrR + DELTA );
     953       76813 :             c_ILD = ( c - 1 ) / ( c + 1 );
     954             :         }
     955             : 
     956             :         /* update of long-term ILD and LR correlation factors for stereo CNA */
     957      106882 :         if ( !hFdCngDec->first_cna_noise_updated )
     958             :         {
     959       16646 :             hFdCngDec->cna_LR_LT = c_LR;
     960       16646 :             hFdCngDec->cna_ILD_LT = c_ILD;
     961             :         }
     962             :         else
     963             :         {
     964       90236 :             hFdCngDec->cna_LR_LT = STEREO_CNA_LR_CORR_LT_FILT * hFdCngDec->cna_LR_LT + ( 1 - STEREO_CNA_LR_CORR_LT_FILT ) * c_LR;
     965       90236 :             hFdCngDec->cna_ILD_LT = STEREO_CNA_ILD_LT_FILT * hFdCngDec->cna_ILD_LT + ( 1 - STEREO_CNA_ILD_LT_FILT ) * c_ILD;
     966             :         }
     967             : 
     968      106882 :         set_f( hFdCngDec->cna_g_state, hFdCngDec->cna_ILD_LT, hFdCngDec->cna_nbands );
     969      106882 :         set_f( hFdCngDec->cna_cm, hFdCngDec->cna_LR_LT, hFdCngDec->cna_nbands );
     970             :     }
     971             : 
     972             :     /* Soft VAD for stereo CNA */
     973     1227647 :     if ( hCPE->element_mode == IVAS_CPE_TD || hCPE->element_mode == IVAS_CPE_DFT )
     974             :     {
     975     1227647 :         if ( !hFdCngDec->first_cna_noise_updated )
     976             :         {
     977      475146 :             hFdCngDec->cna_act_fact = 0.0f;
     978             :         }
     979             :         else
     980             :         {
     981      752501 :             if ( hCPE->element_mode == IVAS_CPE_TD )
     982             :             {
     983             :                 /* quickly decrease in TD stereo mode */
     984       28660 :                 hFdCngDec->cna_act_fact *= 0.7f;
     985             :             }
     986      723841 :             else if ( sts[0]->VAD > hFdCngDec->cna_act_fact )
     987             :             {
     988             :                 /* quickly increase during active frames in DFT stereo mode */
     989      603290 :                 hFdCngDec->cna_act_fact = 0.7f * hFdCngDec->cna_act_fact + 0.3f * sts[0]->VAD;
     990             :             }
     991             :             else
     992             :             {
     993             :                 /* slowly decrease during inactive frames in DFT stereo mode */
     994      120551 :                 hFdCngDec->cna_act_fact = 0.95f * hFdCngDec->cna_act_fact + 0.05f * sts[0]->VAD;
     995             :             }
     996             :         }
     997             :     }
     998             : 
     999     1227647 :     return;
    1000             : }
    1001             : 
    1002             : 
    1003             : /*-------------------------------------------------------------------
    1004             :  * stereo_cng_init_dec()
    1005             :  *
    1006             :  * Initialized stereo CNG
    1007             :  *-------------------------------------------------------------------*/
    1008             : 
    1009       26392 : void stereo_cng_init_dec(
    1010             :     STEREO_CNG_DEC_HANDLE hStereoCng, /* i/o: stereo CNG decoder structure                               */
    1011             :     const int16_t *frameSize          /* i  : pointer to frameSize of channel 0 to be used for channel 1 */
    1012             : )
    1013             : {
    1014       26392 :     hStereoCng->prev_sid_nodata = 0;
    1015       26392 :     set_f( hStereoCng->coh, 0.5f, STEREO_DFT_BAND_MAX + 1 );
    1016       26392 :     set_zero( hStereoCng->cm, STEREO_DFT_BAND_MAX );
    1017       26392 :     hStereoCng->first_SID = 1;
    1018       26392 :     hStereoCng->first_SID_after_TD = 0;
    1019       26392 :     hStereoCng->active_frame_counter = 0;
    1020       26392 :     hStereoCng->xfade_frame_counter = 0;
    1021       26392 :     hStereoCng->xfade_length = 0;
    1022       26392 :     hStereoCng->nr_dft_frames = 0;
    1023       26392 :     hStereoCng->nr_corr_frames = 0;
    1024       26392 :     hStereoCng->nr_sid_frames = 0;
    1025       26392 :     set_f( hStereoCng->olapBufferSynth22, 0.0f, FFTLEN );
    1026       26392 :     hStereoCng->flag_cna_fade = 0;
    1027       26392 :     set_zero( hStereoCng->maskingNoiseS, L_FRAME16k );
    1028       26392 :     hStereoCng->enableSecCNA = 0;
    1029       26392 :     hStereoCng->c_PS_LT = 0.5f;
    1030       26392 :     hStereoCng->frameSize = frameSize;
    1031       26392 :     hStereoCng->last_act_element_mode = IVAS_CPE_DFT;
    1032             : 
    1033       26392 :     return;
    1034             : }

Generated by: LCOV version 1.14