LCOV - code coverage report
Current view: top level - lib_com - gs_noisefill.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 259 273 94.9 %
Date: 2025-05-23 08:37:30 Functions: 8 8 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 <stdint.h>
      38             : #include "options.h"
      39             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #endif
      42             : #include <math.h>
      43             : #include "cnst.h"
      44             : #include "rom_com.h"
      45             : #include "prot.h"
      46             : #include "wmc_auto.h"
      47             : 
      48             : /*-------------------------------------------------------------------*
      49             :  * gs_noisf()
      50             :  *
      51             :  * Noise fill-in function
      52             :  *-------------------------------------------------------------------*/
      53             : 
      54     1014693 : static void gs_noisf(
      55             :     const int16_t Start_BIN, /* i  : First bin for noise fill */
      56             :     const int16_t NB_Qbins,  /* i  : Number of bin per band   */
      57             :     const float Noise_fac,   /* i  : Noise level              */
      58             :     const float *y_norm,     /* i  : Quantized pulses         */
      59             :     float *exc_diffQ,        /* o  : Quantized pulses with noise added */
      60             :     int16_t *seed_tcx,       /* i  : Random generator seed    */
      61             :     const int16_t coder_type /* i  : coder type               */
      62             : )
      63             : {
      64             :     float ftmp;
      65             :     int16_t i, k;
      66     1014693 :     int16_t NB_zer = NB_Qbins / 2;
      67             : 
      68     1014693 :     if ( coder_type == INACTIVE )
      69             :     {
      70      290378 :         NB_zer = 2;
      71             :     }
      72             : 
      73             :     /*----------------------------------------------*
      74             :      * noise fill-in on unquantized subvector       *
      75             :      * injected only from 1066Hz to 6400Hz.         *
      76             :      *----------------------------------------------*/
      77             : 
      78     4954491 :     for ( k = Start_BIN; k < NB_Qbins + Start_BIN; k += NB_zer )
      79             :     {
      80     3939798 :         ftmp = 0.0;
      81    20513462 :         for ( i = k; i < k + NB_zer; i++ )
      82             :         {
      83    16573664 :             exc_diffQ[i] = (float) y_norm[i];
      84    16573664 :             ftmp += exc_diffQ[i] * exc_diffQ[i];
      85             :         }
      86             : 
      87     3939798 :         if ( ftmp < .5 )
      88             :         {
      89    15247188 :             for ( i = k; i < k + NB_zer; i++ )
      90             :             {
      91    12311020 :                 exc_diffQ[i] += Noise_fac * ( (float) own_random( seed_tcx ) / PCM16_TO_FLT_FAC );
      92             :             }
      93             :         }
      94             :         else
      95             :         {
      96             :             /* This is added only to keep the seed in sync between different compilers */
      97     5266274 :             for ( i = k; i < k + NB_zer; i++ )
      98             :             {
      99     4262644 :                 own_random( seed_tcx );
     100             :             }
     101             :         }
     102             :     }
     103             : 
     104     1014693 :     return;
     105             : }
     106             : 
     107             : /*-------------------------------------------------------------------*
     108             :  * EstimateNoiseLevel_inner()
     109             :  *
     110             :  * Estimate noise level from the power spectrum
     111             :  *-------------------------------------------------------------------*/
     112             : 
     113       48919 : static void EstimateNoiseLevel_inner(
     114             :     float *noisepb,         /* o  : Noise per band */
     115             :     const int32_t bitrate,  /* i  : Bitrate of the codec */
     116             :     const int16_t i_band,   /* i  : First band to compute the noise  */
     117             :     const int16_t Mbands_gn /* i  : number of bands                  */
     118             : )
     119             : {
     120             :     int16_t i;
     121             :     float noise_offset;
     122             : 
     123       48919 :     noise_offset = 0.25f;
     124       48919 :     if ( bitrate > ACELP_24k40 )
     125             :     {
     126         808 :         noise_offset = .2f;
     127             :     }
     128       48111 :     else if ( bitrate >= ACELP_22k60 )
     129             :     {
     130        2661 :         noise_offset = .3f;
     131             :     }
     132       45450 :     else if ( bitrate >= ACELP_9k60 )
     133             :     {
     134       19456 :         noise_offset = 0.35f;
     135             :     }
     136             :     else
     137             :     {
     138       25994 :         noise_offset = .4f;
     139             :     }
     140             : 
     141       48919 :     set_f( noisepb + i_band, noise_offset, Mbands_gn - i_band );
     142             : 
     143      293514 :     for ( i = i_band; i < 5; i++ )
     144             :     {
     145      244595 :         if ( noisepb[i] > 0.2f )
     146             :         {
     147      240555 :             noisepb[i] = 0.2f;
     148             :         }
     149             :     }
     150             : 
     151       48919 :     return;
     152             : }
     153             : 
     154             : 
     155             : /*-------------------------------------------------------------------*
     156             :  * EstimateNoiseLevel()
     157             :  *
     158             :  *
     159             :  *-------------------------------------------------------------------*/
     160             : 
     161       48919 : static void EstimateNoiseLevel(
     162             :     float *noisepb,             /* o  : Noise per band                          */
     163             :     const int32_t bitrate,      /* i  : Bitrate of the codec                    */
     164             :     const int16_t Diff_len,     /* i  : number of bin before cut-off frequency  */
     165             :     const int16_t Mbands_gn,    /* i  : number of bands                         */
     166             :     const int16_t coder_type,   /* i  : coder type                              */
     167             :     const int16_t noise_lev,    /* i  : pulses dynamic                          */
     168             :     const int16_t pit_band_idx, /* i  : bin position of the cut-off frequency   */
     169             :     const int16_t last_bin,     /* i  : the last bin of bit allocation          */
     170             :     const int16_t bwidth,
     171             :     const int16_t L_frame /* i  : frame length                            */
     172             : )
     173             : {
     174             :     int16_t i_band;
     175             : 
     176       48919 :     i_band = 0;
     177             : 
     178       48919 :     if ( Diff_len < L_frame )
     179             :     {
     180       48919 :         EstimateNoiseLevel_inner( noisepb, bitrate, i_band, Mbands_gn );
     181             : 
     182       48919 :         if ( coder_type != INACTIVE )
     183             :         {
     184       32421 :             if ( ( bitrate == ACELP_8k00 && last_bin > 8 ) && bwidth != NB )
     185             :             {
     186           0 :                 while ( Mbands_gn > i_band )
     187             :                 {
     188           0 :                     noisepb[i_band] *= 2.0f;
     189           0 :                     i_band++;
     190             :                 }
     191             :             }
     192             :             else
     193             :             {
     194      309213 :                 while ( pit_band_idx > i_band )
     195             :                 {
     196      276792 :                     noisepb[i_band] /= 2.0f;
     197      276792 :                     i_band++;
     198             :                 }
     199             :             }
     200             :         }
     201             :     }
     202             : 
     203       48919 :     if ( ( coder_type == INACTIVE || noise_lev >= NOISE_LEVEL_SP3 ) && L_frame == L_FRAME )
     204             :     {
     205      199551 :         for ( i_band = 9; i_band < Mbands_gn; i_band++ )
     206             :         {
     207      174589 :             noisepb[i_band] *= 1.15f;
     208             :         }
     209             :     }
     210       23957 :     else if ( L_frame == L_FRAME16k )
     211             :     {
     212       10509 :         if ( bitrate == ACELP_13k20 )
     213             :         {
     214           0 :             set_f( noisepb, .45f, Mbands_gn );
     215             :         }
     216             : 
     217       10509 :         if ( coder_type == INACTIVE )
     218             :         {
     219      199671 :             for ( ; i_band < Mbands_gn; i_band++ )
     220             :             {
     221      189162 :                 noisepb[i_band] = .4f;
     222             :             }
     223             :         }
     224           0 :         else if ( ( noise_lev <= NOISE_LEVEL_SP1 ) && bitrate > ACELP_16k40 )
     225             :         {
     226           0 :             for ( ; i_band < Mbands_gn - 4; i_band++ )
     227             :             {
     228           0 :                 noisepb[i_band] *= .6f;
     229             :             }
     230             :         }
     231           0 :         else if ( ( noise_lev <= NOISE_LEVEL_SP2 ) && bitrate > ACELP_16k40 )
     232             :         {
     233           0 :             for ( ; i_band < Mbands_gn - 4; i_band++ )
     234             :             {
     235           0 :                 noisepb[i_band] *= .8f;
     236             :             }
     237             :         }
     238             :     }
     239             : 
     240       48919 :     return;
     241             : }
     242             : 
     243             : 
     244             : /*-------------------------------------------------------------------*
     245             :  * Apply_NoiseFill()
     246             :  *
     247             :  *
     248             :  *-------------------------------------------------------------------*/
     249             : 
     250       62123 : static void Apply_NoiseFill(
     251             :     float *exc_diffQ,                  /* i/o: Noise per band                          */
     252             :     int16_t *seed_tcx,                 /* i  : Seed for noise                          */
     253             :     const float *noisepb,              /* i  : Noise per band                          */
     254             :     const int16_t Diff_len,            /* i  : number of bin before cut-off frequency  */
     255             :     const int16_t Mbands_gn,           /* i  : number of bands                         */
     256             :     const int16_t coder_type,          /* i  : coder type                              */
     257             :     const int16_t *freq_nsbin_per_band /* i  : bin per bands tables                    */
     258             : )
     259             : {
     260             :     int16_t StartBin, NB_Qbins, i_band;
     261       62123 :     StartBin = 0;
     262       62123 :     NB_Qbins = 0;
     263             : 
     264     1076816 :     for ( i_band = 0; i_band < Mbands_gn; i_band++ )
     265             :     {
     266     1014693 :         StartBin += NB_Qbins;
     267     1014693 :         NB_Qbins = freq_nsbin_per_band[i_band];
     268             : 
     269     1014693 :         if ( Diff_len < L_FRAME )
     270             :         {
     271     1014693 :             gs_noisf( StartBin, NB_Qbins, noisepb[i_band], exc_diffQ, exc_diffQ, seed_tcx, coder_type );
     272             :         }
     273             :     }
     274             : 
     275       62123 :     return;
     276             : }
     277             : 
     278             : 
     279             : /*-------------------------------------------------------------------*
     280             :  * freq_dnw_scaling()
     281             :  *
     282             :  *
     283             :  *-------------------------------------------------------------------*/
     284             : 
     285       60267 : void freq_dnw_scaling(
     286             :     const int16_t cor_strong_limit, /* i  : HF correlation                */
     287             :     const int16_t coder_type,       /* i  : coder type                    */
     288             :     const int16_t noise_lev,        /* i  : Noise level                   */
     289             :     const int32_t core_brate,       /* i  : Core bitrate                  */
     290             :     float fy_norm[],                /* i/o: Frequency quantized parameter */
     291             :     const int16_t L_frame           /* i  : frame length                  */
     292             : )
     293             : {
     294             :     float sc_dyn;
     295             :     int16_t start_sc, i;
     296             : 
     297       60267 :     sc_dyn = 1.0f;
     298       60267 :     start_sc = L_frame;
     299             : 
     300       60267 :     if ( core_brate <= ACELP_8k00 && coder_type == INACTIVE )
     301             :     {
     302         201 :         sc_dyn *= .15f;
     303         201 :         start_sc = 64;
     304             :     }
     305       60066 :     else if ( coder_type == INACTIVE )
     306             :     {
     307       16542 :         sc_dyn *= .25f;
     308       16542 :         start_sc = 80;
     309             :     }
     310             :     else
     311             :     {
     312       43524 :         sc_dyn = (float) ( NOISE_LEVEL_SP3 - noise_lev ) / 10.0f + 0.4f;
     313       43524 :         start_sc = 112 + ( NOISE_LEVEL_SP3 - noise_lev ) * 16;
     314             : 
     315       43524 :         if ( noise_lev == NOISE_LEVEL_SP0 )
     316             :         {
     317       11099 :             start_sc = L_FRAME;
     318             :         }
     319             :     }
     320             : 
     321       60267 :     if ( L_frame == L_FRAME16k && core_brate <= ACELP_24k40 )
     322             :     {
     323        9701 :         sc_dyn += 0.125f;
     324        9701 :         if ( sc_dyn > 1.0f )
     325             :         {
     326           0 :             sc_dyn = 1.0f;
     327             :         }
     328             :     }
     329             : 
     330     7945035 :     for ( i = start_sc; i < L_frame; i++ )
     331             :     {
     332     7884768 :         fy_norm[i] *= sc_dyn;
     333             :     }
     334             : 
     335       60267 :     if ( ( core_brate < ACELP_13k20 && cor_strong_limit == 0 ) || core_brate < ACELP_9k60 )
     336             :     {
     337     4385952 :         for ( i = 160; i < L_frame; i++ )
     338             :         {
     339     4340736 :             if ( fy_norm[i] > 1.0f )
     340             :             {
     341        1946 :                 fy_norm[i] = 1.0f;
     342             :             }
     343             : 
     344     4340736 :             if ( fy_norm[i] < -1.0f )
     345             :             {
     346        1714 :                 fy_norm[i] = -1.0f;
     347             :             }
     348             :         }
     349             :     }
     350       15051 :     else if ( core_brate < ACELP_22k60 )
     351             :     {
     352     1574014 :         for ( i = 160; i < L_frame; i++ )
     353             :         {
     354     1562432 :             if ( fy_norm[i] > 1.5f )
     355             :             {
     356        1049 :                 fy_norm[i] = 1.5f;
     357             :             }
     358             : 
     359     1562432 :             if ( fy_norm[i] < -1.5f )
     360             :             {
     361        1082 :                 fy_norm[i] = -1.5f;
     362             :             }
     363             :         }
     364             :     }
     365             : 
     366       60267 :     return;
     367             : }
     368             : 
     369             : 
     370             : /*-------------------------------------------------------------------*
     371             :  * Decreas_freqPeak()
     372             :  *
     373             :  *
     374             :  *-------------------------------------------------------------------*/
     375             : 
     376         197 : static void Decreas_freqPeak(
     377             :     const float *lsf_new, /* i  : ISFs at the end of the frame                          */
     378             :     float *exc_diffQ,     /* i/o: frequency coefficients of per band                    */
     379             :     float thr_rat         /* i  : threshold of ratio between consecutive lsf_new_diff   */
     380             : )
     381             : {
     382             :     int16_t i, j, k;
     383         197 :     int16_t last_bin = 0;
     384         197 :     int16_t pos = 0;
     385             :     float *src;
     386             :     float avrg, max_val;
     387             :     float lsf_new_diff[M];
     388         197 :     lsf_new_diff[0] = 0; /* prevent unitialized value */
     389        2955 :     for ( j = 1; j < ( M - 1 ); j++ )
     390             :     {
     391        2758 :         lsf_new_diff[j] = lsf_new[j] - lsf_new[j - 1];
     392             :     }
     393             : 
     394         197 :     avrg = 0.0f;
     395             :     /* This is to prevent a possible div by 0 in the '*(src) = (*src > 0) ?...'
     396             :        loop. The value of 'max' is not important because it will be mutiplied
     397             :        by 'avrg' and the result will be close to 0. The 'fabs(*src)/max'
     398             :        div by 0 error will be avoided. */
     399         197 :     max_val = 0.001f;
     400       19109 :     for ( i = 160; i < L_FRAME; i++ )
     401             :     {
     402       18912 :         if ( fabs( exc_diffQ[i] ) > max_val )
     403             :         {
     404        1042 :             max_val = (float) fabs( exc_diffQ[i] );
     405        1042 :             pos = i;
     406             :         }
     407       18912 :         avrg += (float) fabs( exc_diffQ[i] );
     408             :     }
     409         197 :     avrg /= 96;
     410         197 :     last_bin = M - 1; /* When the search is false, should equate the end of the vector, not the beginning */
     411        2253 :     for ( i = 0; i < ( M - 1 ); i++ )
     412             :     {
     413        2253 :         if ( lsf_new[i] > 4000 )
     414             :         {
     415         197 :             last_bin = i;
     416         197 :             break;
     417             :         }
     418             :     }
     419             : 
     420         899 :     for ( i = last_bin; i < 14; i++ )
     421             :     {
     422         702 :         if ( lsf_new_diff[i] < thr_rat * lsf_new_diff[i - 1] )
     423             :         {
     424          41 :             src = &exc_diffQ[( i - 1 ) * 16];
     425         123 :             for ( j = 0; j < 2; j++ )
     426             :             {
     427        1394 :                 for ( k = 0; k < 16; k++ )
     428             :                 {
     429        1312 :                     if ( fabs( *src ) > 2.0f * avrg )
     430             :                     {
     431          69 :                         *( src ) = ( *src > 0 ) ? (float) ( avrg * ( 2.0f - fabs( *src ) / max_val ) ) : (float) ( -avrg * ( 2.0f - fabs( *src ) / max_val ) );
     432             :                     }
     433        1312 :                     src++;
     434             :                 }
     435             :             }
     436             :         }
     437             :     }
     438             : 
     439         197 :     if ( fabs( exc_diffQ[pos] ) == max_val && max_val > 4.0f * avrg )
     440             :     {
     441         312 :         for ( i = pos - 1; i < pos + 2; i++ )
     442             :         {
     443         234 :             exc_diffQ[pos] *= 0.5f;
     444             :         }
     445             :     }
     446             : 
     447         197 :     return;
     448             : }
     449             : 
     450             : 
     451             : /*-------------------------------------------------------------------*
     452             :  * envelop_modify()
     453             :  *
     454             :  *
     455             :  *-------------------------------------------------------------------*/
     456             : 
     457         109 : static void envelop_modify(
     458             :     float *exc_diffQ,     /* i/o: frequency coefficients of per band      */
     459             :     int16_t *seed_tcx,    /* i  : Seed for noise                          */
     460             :     int16_t last_bin,     /* i  : last bin of bit allocation              */
     461             :     float *Ener_per_bd_iQ /* i  : Quantized energy of targeted vector     */
     462             : )
     463             : {
     464             :     int16_t i, j, end_band;
     465             :     float Ener, Ener1, *src;
     466         109 :     float weight = 1.0f;
     467             : 
     468         109 :     end_band = L_FRAME;
     469         109 :     Ener = 0.1f;
     470       10573 :     for ( i = last_bin * 16; i < end_band; i++ )
     471             :     {
     472       10464 :         Ener += exc_diffQ[i] * exc_diffQ[i];
     473             :     }
     474         109 :     Ener = (float) sqrt( ( end_band - last_bin * 16 ) / Ener );
     475             : 
     476         109 :     weight = 0.5f;
     477             : 
     478         109 :     src = &exc_diffQ[16 * last_bin];
     479         545 :     for ( i = last_bin; i < last_bin + 4; i++ )
     480             :     {
     481         436 :         Ener1 = (float) ( 0.4f * pow( 10, Ener_per_bd_iQ[i + 1] ) );
     482        7412 :         for ( j = 0; j < 16; j++ )
     483             :         {
     484        6976 :             *src = Ener1 * ( weight * ( *src ) * Ener + ( 1.0f - weight ) * own_random( seed_tcx ) / PCM16_TO_FLT_FAC );
     485        6976 :             src++;
     486             :         }
     487             :     }
     488             : 
     489         109 :     Ener1 = (float) ( 0.4f * pow( 10, Ener_per_bd_iQ[15] ) );
     490             : 
     491         109 :     src = &exc_diffQ[224];
     492        3597 :     for ( j = 0; j < 32; j++ )
     493             :     {
     494        3488 :         *src = Ener1 * ( weight * ( *src ) * Ener + ( 1.0f - weight ) * own_random( seed_tcx ) / PCM16_TO_FLT_FAC );
     495        3488 :         src++;
     496             :     }
     497             : 
     498         109 :     return;
     499             : }
     500             : 
     501             : 
     502             : /*-------------------------------------------------------------------*
     503             :  * highband_exc_dct_in()
     504             :  *
     505             :  *
     506             :  *-------------------------------------------------------------------*/
     507             : 
     508       92085 : void highband_exc_dct_in(
     509             :     const int32_t core_brate,         /* i  : core bitrate                            */
     510             :     const int16_t *mfreq_bindiv,      /* i  : bin per bands tables                    */
     511             :     int16_t last_bin,                 /* i  : last bin of bit allocation              */
     512             :     int16_t Diff_len,                 /* i  : number of bin before cut-off frequency  */
     513             :     int16_t noise_lev,                /* i  : pulses dynamic                          */
     514             :     int16_t pit_band_idx,             /* i  : bin position of the cut-off frequency   */
     515             :     float *exc_diffQ,                 /* i  : frequency coefficients of per band      */
     516             :     int16_t *seed_tcx,                /* i  : Seed for noise                          */
     517             :     float *Ener_per_bd_iQ,            /* i  : Quantized energy of targeted vector     */
     518             :     int16_t nb_subfr,                 /* i  : Number of subframe considered           */
     519             :     float *exc_dct_in,                /* o  : dct of residual signal                  */
     520             :     int16_t last_coder_type,          /* i  : coding type of last frame               */
     521             :     int16_t *bitallocation_band,      /* i  : bit allocation flag of each band        */
     522             :     const float *lsf_new,             /* i  : LSFs at the end of the frame            */
     523             :     float *last_exc_dct_in,           /* i  : dct of residual signal of last frame    */
     524             :     float *last_ener,                 /* i  : frequency energy  of last frame         */
     525             :     int16_t *last_bitallocation_band, /* i  : bit allocation flag of each band  of last frame   */
     526             :     int16_t *bitallocation_exc,       /* i  : flag of decoded coefficients            */
     527             :     const int16_t bfi,                /* i  : bad frame indicator                     */
     528             :     const int16_t coder_type,         /* i  : coder type                              */
     529             :     const int16_t bwidth,             /* i  : audio bandwidth                         */
     530             :     float *exc_wo_nf,                 /* o  : temporal excitation (in f domain) without noisefill   */
     531             :     const int16_t GSC_noisy_speech,   /* i  : GSC noisy speech flag                   */
     532             :     float *lt_ener_per_band,          /* i/o: Average per band energy                 */
     533             :     const int16_t L_frame,            /* i  : frame length                            */
     534             :     const int16_t element_mode,       /* i  : IVAS element mode                       */
     535             :     const int16_t GSC_IVAS_mode       /* i  : GSC IVAS mode                           */
     536             : )
     537             : {
     538             :     int16_t i, j;
     539       92085 :     int16_t MAX_Bin = 0;
     540             :     int16_t last_bin_tmp;
     541             :     float noisepb[MBANDS_GN16k];
     542             :     float Ener_per_bd_yQ[MBANDS_GN16k];
     543             :     float *src, *dst, *end;
     544       92085 :     float ener = 0.0f;
     545       92085 :     int16_t length_bin, bwe_flag = 0;
     546             : 
     547      644595 :     for ( j = 10; j < MBANDS_GN; j++ )
     548             :     {
     549      552510 :         ener += (float) pow( 10, Ener_per_bd_iQ[j] );
     550             :     }
     551             : 
     552       92085 :     if ( core_brate == ACELP_8k00 && bwidth != NB )
     553             :     {
     554         197 :         if ( last_coder_type != AUDIO )
     555             :         {
     556          88 :             *last_ener = ener;
     557             :         }
     558             : 
     559         197 :         if ( ( last_bin > 8 || Diff_len != 0 ) && last_coder_type == AUDIO )
     560             :         {
     561         109 :             MAX_Bin = 10;
     562         109 :             bwe_flag = 1;
     563             :         }
     564             :         else
     565             :         {
     566          88 :             MAX_Bin = 15;
     567             :         }
     568             : 
     569         197 :         last_bin_tmp = last_bin;
     570         197 :         if ( last_bin < MAX_Bin )
     571             :         {
     572         197 :             last_bin = MAX_Bin;
     573             :         }
     574         197 :         last_bin += 1;
     575             :     }
     576             :     else
     577             :     {
     578       91888 :         if ( L_frame == L_FRAME16k )
     579             :         {
     580       10635 :             last_bin = MBANDS_GN16k;
     581             :         }
     582             :         else
     583             :         {
     584       81253 :             last_bin = MBANDS_GN;
     585             :         }
     586       91888 :         last_bin_tmp = last_bin;
     587             :     }
     588             : 
     589             : 
     590       92085 :     if ( bfi || core_brate < 6000 || ( core_brate < 8600 && coder_type == UNVOICED ) )
     591             :     {
     592       31298 :         set_f( noisepb, 0.4f, MBANDS_GN );
     593             :     }
     594       60787 :     else if ( GSC_IVAS_mode == 3 || ( GSC_IVAS_mode > 0 && GSC_noisy_speech == 1 ) )
     595             :     {
     596       11868 :         set_f( noisepb, 0.4f, MBANDS_GN16k );
     597             :     }
     598             :     else
     599             :     {
     600       48919 :         EstimateNoiseLevel( noisepb, core_brate, Diff_len, last_bin, coder_type, noise_lev, pit_band_idx, last_bin_tmp, bwidth, L_frame );
     601             :     }
     602             : 
     603       92085 :     if ( exc_wo_nf != NULL )
     604             :     {
     605       90525 :         mvr2r( exc_diffQ, exc_wo_nf, L_frame );
     606             :     }
     607             : 
     608       92085 :     if ( GSC_IVAS_mode == 0 && GSC_noisy_speech && !bfi && element_mode <= IVAS_SCE )
     609             :     {
     610       31650 :         set_f( noisepb, 0.1f, MBANDS_GN );
     611             :     }
     612             : 
     613       92085 :     if ( core_brate < 6000 && coder_type <= UNVOICED )
     614             :     {
     615     7700234 :         for ( i = 0; i < L_frame; i++ )
     616             :         {
     617     7670272 :             if ( exc_diffQ[i] == 0.0f )
     618             :             {
     619     7581600 :                 exc_diffQ[i] += 2.0f * noisepb[0] * ( (float) own_random( seed_tcx ) / PCM16_TO_FLT_FAC );
     620             :             }
     621             :         }
     622             :     }
     623             :     else
     624             :     {
     625       62123 :         Apply_NoiseFill( exc_diffQ, seed_tcx, noisepb, Diff_len, last_bin, coder_type, mfreq_bindiv );
     626             :     }
     627             : 
     628             :     /*--------------------------------------------------------------------------------------*
     629             :      * Quantize average gain
     630             :      * Subtract Q averaged gain
     631             :      * VQ of remaining gain per band
     632             :      *--------------------------------------------------------------------------------------*/
     633             : 
     634       92085 :     if ( core_brate == ACELP_8k00 && bwidth != NB )
     635             :     {
     636         197 :         Ener_per_band_comp( exc_diffQ, Ener_per_bd_yQ, last_bin + 1, 0, L_frame );
     637             :     }
     638             :     else
     639             :     {
     640       91888 :         Ener_per_band_comp( exc_diffQ, Ener_per_bd_yQ, MBANDS_GN, 1, L_frame );
     641             : 
     642       91888 :         if ( nb_subfr < 4 && L_frame < L_FRAME16k )
     643             :         {
     644     1115727 :             for ( i = L_FRAME - 16; i < L_FRAME; i++ )
     645             :             {
     646     1050096 :                 exc_diffQ[i] *= ( 0.067f * i - 15.0f );
     647             :             }
     648             :         }
     649             :     }
     650             : 
     651             :     /*--------------------------------------------------------------------------------------*
     652             :      * Check potential energy excitation overshoot
     653             :      *--------------------------------------------------------------------------------------*/
     654             : 
     655       92085 :     if ( bfi )
     656             :     {
     657        1560 :         if ( GSC_noisy_speech == 0 && coder_type > UNVOICED ) /* Here coder_type == last_coder_type because of the bfi */
     658             :         {
     659       16146 :             for ( i = 0; i < last_bin; i++ )
     660             :             {
     661       15192 :                 Ener_per_bd_iQ[i] = min( Ener_per_bd_iQ[i], ( lt_ener_per_band[i] - 0.0376f ) - Ener_per_bd_yQ[i] );
     662       15192 :                 lt_ener_per_band[i] -= 0.0188f;
     663             :             }
     664        1074 :             for ( ; i < MBANDS_GN; i++ )
     665             :             {
     666         120 :                 Ener_per_bd_iQ[i] = min( Ener_per_bd_iQ[i], ( lt_ener_per_band[i] - 0.0376f ) );
     667         120 :                 lt_ener_per_band[i] -= 0.0188f;
     668             :             }
     669             :         }
     670             :         else
     671             :         {
     672       10506 :             for ( i = 0; i < last_bin; i++ )
     673             :             {
     674        9900 :                 Ener_per_bd_iQ[i] = min( Ener_per_bd_iQ[i], ( lt_ener_per_band[i] + 0.3f ) - Ener_per_bd_yQ[i] );
     675        9900 :                 lt_ener_per_band[i] -= 0.0188f;
     676             :             }
     677         606 :             for ( ; i < MBANDS_GN; i++ )
     678             :             {
     679           0 :                 Ener_per_bd_iQ[i] = min( Ener_per_bd_iQ[i], ( lt_ener_per_band[i] + 0.3f ) );
     680           0 :                 lt_ener_per_band[i] -= 0.0188f;
     681             :             }
     682             :         }
     683             :     }
     684             : 
     685             :     /*--------------------------------------------------------------------------------------*
     686             :      * Apply decoded gain onto the difference signal
     687             :      *--------------------------------------------------------------------------------------*/
     688             : 
     689       92085 :     if ( GSC_IVAS_mode >= 1 )
     690             :     {
     691       19627 :         float scale_factLF = 0.9f;
     692       19627 :         float scale_factHF = 0.9f;
     693             : 
     694       19627 :         if ( GSC_IVAS_mode == 1 && GSC_noisy_speech == 0 )
     695             :         {
     696        6755 :             scale_factHF = 0.8f;
     697             :         }
     698       12872 :         else if ( GSC_IVAS_mode == 2 || GSC_noisy_speech == 1 )
     699             :         {
     700        1440 :             scale_factHF = 0.71f;
     701             :         }
     702       11432 :         else if ( GSC_IVAS_mode == 3 )
     703             :         {
     704       11432 :             scale_factHF = 0.9f;
     705             :         }
     706     2739739 :         for ( i = 0; i < pit_band_idx * 16; i++ )
     707             :         {
     708     2720112 :             exc_diffQ[i] *= scale_factLF;
     709             :         }
     710     2324027 :         for ( ; i < L_frame; i++ )
     711             :         {
     712     2304400 :             exc_diffQ[i] *= scale_factHF;
     713             :         }
     714             :     }
     715       72458 :     else if ( GSC_noisy_speech )
     716             :     {
     717       33186 :         float scale_fact = 0.9f;
     718             : 
     719       33186 :         if ( element_mode == IVAS_CPE_TD )
     720             :         {
     721         108 :             if ( coder_type == INACTIVE )
     722             :             {
     723           0 :                 scale_fact = 1.0f;
     724             :             }
     725             :             else
     726             :             {
     727         108 :                 scale_fact = 0.95f;
     728             :             }
     729             :         }
     730       33078 :         else if ( element_mode > IVAS_SCE )
     731             :         {
     732        1116 :             scale_fact = 0.71f;
     733             :         }
     734             : 
     735     8528802 :         for ( i = 0; i < L_frame; i++ )
     736             :         {
     737     8495616 :             exc_diffQ[i] *= scale_fact;
     738             :         }
     739             :     }
     740             : 
     741       92085 :     if ( GSC_noisy_speech && element_mode > IVAS_SCE && core_brate < ACELP_7k20 )
     742             :     {
     743       18408 :         for ( i = 80; i < L_frame; i++ )
     744             :         {
     745       18304 :             exc_diffQ[i] *= ( +0.0024f * (float) i + 1.192f );
     746             :         }
     747             :     }
     748             : 
     749       92085 :     Comp_and_apply_gain( exc_diffQ, Ener_per_bd_iQ, Ener_per_bd_yQ, last_bin, 0 );
     750             : 
     751       92085 :     if ( exc_wo_nf != NULL )
     752             :     {
     753       90525 :         Comp_and_apply_gain( exc_wo_nf, Ener_per_bd_iQ, Ener_per_bd_yQ, last_bin, 1 );
     754             : 
     755       90525 :         v_add( exc_dct_in, exc_wo_nf, exc_wo_nf, L_frame );
     756             :     }
     757             : 
     758             :     /*--------------------------------------------------------------------------------------*
     759             :      * add the correction layer to the LF bins,
     760             :      * and add the quantized pulses or the noise for the higher part of the spectrum
     761             :      * (non valuable temporal content already zeroed)
     762             :      * DC is Zeroed
     763             :      *--------------------------------------------------------------------------------------*/
     764             : 
     765       92085 :     v_add( exc_dct_in, exc_diffQ, exc_dct_in, L_frame );
     766             : 
     767       92085 :     if ( core_brate == ACELP_8k00 && bwidth != NB )
     768             :     {
     769         197 :         if ( bwe_flag == 1 )
     770             :         {
     771         109 :             last_bin -= 1;
     772         109 :             src = &exc_diffQ[L_FRAME - 1];
     773         109 :             dst = &exc_dct_in[MAX_Bin * 16 - 1];
     774         109 :             end = &exc_diffQ[last_bin * 16 - 1];
     775             : 
     776       10573 :             while ( src > end )
     777             :             {
     778       10464 :                 *src-- = *dst--;
     779             :             }
     780             : 
     781         109 :             if ( ( bitallocation_exc[0] != 0 || bitallocation_exc[1] != 0 ) && core_brate == ACELP_8k00 )
     782             :             {
     783          12 :                 exc_diffQ[160] = 0.0f;
     784             :             }
     785             : 
     786         109 :             envelop_modify( exc_diffQ, seed_tcx, last_bin, Ener_per_bd_iQ );
     787             : 
     788         109 :             mvr2r( &exc_diffQ[last_bin * 16], &exc_dct_in[last_bin * 16], L_FRAME - last_bin * 16 );
     789             :         }
     790             : 
     791         197 :         if ( nb_subfr < 4 )
     792             :         {
     793         765 :             for ( i = L_FRAME - 16; i < L_FRAME; i++ )
     794             :             {
     795         720 :                 exc_dct_in[i] *= ( 0.067f * i - 15.f );
     796             :             }
     797             :         }
     798             : 
     799         197 :         if ( ener < 2 * ( *last_ener ) && ener > 0.5f * ( *last_ener ) )
     800             :         {
     801         193 :             length_bin = 6;
     802         193 :             if ( last_coder_type != AUDIO )
     803             :             {
     804          88 :                 set_s( last_bitallocation_band, 0, 6 );
     805          88 :                 mvr2r( &exc_dct_in[( 4 + length_bin ) * 16], &last_exc_dct_in[( 4 + length_bin ) * 16], length_bin * 16 );
     806             :             }
     807             : 
     808        1351 :             for ( i = 4; i < ( 4 + length_bin ); i++ )
     809             :             {
     810        1158 :                 if ( !( bitallocation_band[i] == 0 && last_bitallocation_band[i - 4] == 0 ) )
     811             :                 {
     812         823 :                     src = &exc_dct_in[( i + length_bin ) * 16];
     813         823 :                     dst = &last_exc_dct_in[( i + length_bin ) * 16];
     814       13991 :                     for ( j = 0; j < 16; j++ )
     815             :                     {
     816       13168 :                         if ( fabs( *src ) > 3.0f * fabs( *dst ) )
     817             :                         {
     818        1069 :                             *src = ( *src > 0 ) ? (float) ( 0.5f * ( *src + fabs( *dst ) ) ) : (float) ( 0.5f * ( *src - fabs( *dst ) ) );
     819             :                         }
     820       12099 :                         else if ( fabs( *dst ) > 3.0f * fabs( *src ) )
     821             :                         {
     822        1779 :                             *src = ( *src > 0 ) ? (float) ( 0.7f * ( *src ) + 0.3f * fabs( *dst ) ) : (float) ( 0.7f * ( *src ) - 0.3f * fabs( *dst ) );
     823             :                         }
     824       13168 :                         src++;
     825       13168 :                         dst++;
     826             :                     }
     827             :                 }
     828             :             }
     829             :         }
     830             : 
     831         197 :         if ( bwe_flag == 1 )
     832             :         {
     833         109 :             Decreas_freqPeak( lsf_new, exc_dct_in, 0.3f );
     834             :         }
     835             :         else
     836             :         {
     837          88 :             Decreas_freqPeak( lsf_new, exc_dct_in, 0.5f );
     838             :         }
     839             :     }
     840             : 
     841       92085 :     mvr2r( &exc_dct_in[64], &last_exc_dct_in[64], L_frame - 64 );
     842       92085 :     mvs2s( &bitallocation_band[4], last_bitallocation_band, 6 );
     843       92085 :     *last_ener = ener;
     844             : 
     845       92085 :     return;
     846             : }

Generated by: LCOV version 1.14