LCOV - code coverage report
Current view: top level - lib_com - stat_noise_uv_mod.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 54 57 94.7 %
Date: 2025-05-23 08:37:30 Functions: 2 2 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 "prot.h"
      44             : #include "wmc_auto.h"
      45             : 
      46             : /*---------------------------------------------------------------------*
      47             :  * Local constants
      48             :  *---------------------------------------------------------------------*/
      49             : 
      50             : #define TILT_COMP_LIM 0.75f
      51             : 
      52             : /*---------------------------------------------------------*
      53             :  * Local function prototypes
      54             :  *---------------------------------------------------------*/
      55             : 
      56             : static float calc_tilt( const float *x, const int16_t len );
      57             : 
      58             : /*--------------------------------------------------------------------*
      59             :  * stat_noise_uv_mod()
      60             :  *
      61             :  * Modifies excitation signal in stationary noise segments
      62             :  *--------------------------------------------------------------------*/
      63             : 
      64      585133 : void stat_noise_uv_mod(
      65             :     const int16_t coder_type,   /* i  : coder type                           */
      66             :     float noisiness,            /* i  : noisiness parameter                  */
      67             :     const float *const lsp_old, /* i  : old LSP vector at 4th sfr            */
      68             :     const float *const lsp_new, /* i  : LSP vector at 4th sfr                */
      69             :     const float *const lsp_mid, /* i  : LSP vector at 2nd sfr                */
      70             :     float *Aq,                  /* o  : A(z) quantized for the 4 subframes   */
      71             :     float *exc2,                /* o  : excitation buffer                    */
      72             :     const int16_t bfi,          /* i  : bad frame indicator                  */
      73             :     float *ge_sm,               /* i/o: smoothed excitation gain             */
      74             :     int16_t *uv_count,          /* i/o: unvoiced counter                     */
      75             :     int16_t *act_count,         /* i/o: activation counter                   */
      76             :     float lspold_s[],           /* i/o: old LSP                              */
      77             :     int16_t *noimix_seed,       /* i/o: mixture seed                         */
      78             :     float *st_min_alpha,        /* i/o: minimum alpha                        */
      79             :     float *exc_pe,              /* i/o: memory of the preemphasis filter     */
      80             :     const int32_t bitrate,      /* i  : core bitrate                         */
      81             :     const int16_t bwidth        /* i  : audio bandwidth                      */
      82             : )
      83             : {
      84             :     int16_t i, k;
      85             :     int16_t i_subfr;
      86             :     float exctilt;
      87             :     float vare, ge, randval;
      88             :     float alpha, min_alpha;
      89             :     float lspnew_s[M], oldlsp_mix[M], midlsp_mix[M], newlsp_mix[M];
      90             :     float beta;
      91             :     float noimix_fac;
      92             : 
      93      585133 :     alpha = 1.0f;
      94      585133 :     min_alpha = 0.5f;
      95             : 
      96             :     /*---------------------------------------------------------*
      97             :      * Update minimum mixing factor alpha
      98             :      *---------------------------------------------------------*/
      99             : 
     100             :     /* Activate modifications for WB/SWB <= 9.6kbps for NB only at 9.6kbps */
     101      585133 :     if ( coder_type == INACTIVE && ( bitrate == ACELP_9k60 || ( bitrate < ACELP_9k60 && bwidth > NB ) ) )
     102             :     {
     103        1388 :         if ( !bfi )
     104             :         {
     105        1388 :             min_alpha = max( noisiness / 31.0f * 0.5f + 0.5f, *st_min_alpha - 0.05f );
     106        1388 :             *st_min_alpha = min_alpha;
     107             :         }
     108             :         else
     109             :         {
     110           0 :             min_alpha = *st_min_alpha;
     111             :         }
     112             :     }
     113             : 
     114             :     /*---------------------------------------------------------*
     115             :      * Mix excitation signal with random noise
     116             :      *---------------------------------------------------------*/
     117             : 
     118             :     /* Activate modifications for WB/SWB <= 9.6kbps for NB only at 9.6kbps */
     119      585133 :     if ( coder_type == INACTIVE && ( bitrate == ACELP_9k60 || ( bitrate < ACELP_9k60 && bwidth > NB ) ) )
     120             :     {
     121             :         /* preemphasize the excitation signal with its tilt */
     122        1388 :         if ( min_alpha < TILT_COMP_LIM )
     123             :         {
     124        2645 :             for ( i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR )
     125             :             {
     126        2116 :                 exctilt = calc_tilt( &exc2[i_subfr], L_SUBFR );
     127        2116 :                 exctilt = ( TILT_COMP_LIM - min_alpha ) / ( TILT_COMP_LIM - 0.5f ) * exctilt;
     128             : 
     129        2116 :                 preemph( &exc2[i_subfr], exctilt, L_SUBFR, exc_pe );
     130             :             }
     131             :         }
     132             : 
     133             :         /* set the mixing factor alpha */
     134        1388 :         ( *uv_count )++;
     135        1388 :         if ( *uv_count <= START_NG )
     136             :         {
     137        1080 :             *act_count = 3;
     138        1080 :             alpha = 1;
     139        1080 :             mvr2r( lsp_new, lspold_s, M );
     140             :         }
     141             :         else
     142             :         {
     143         308 :             *act_count = 0;
     144         308 :             if ( *uv_count > FULL_NG )
     145             :             {
     146           0 :                 *uv_count = FULL_NG;
     147             :             }
     148             : 
     149         308 :             alpha = 1 + ( (float) *uv_count - START_NG ) / ( (float) FULL_NG - START_NG ) * ( min_alpha - 1.0f );
     150             :         }
     151             : 
     152             :         /* calculate lowpass-filtered excitation gain */
     153        1388 :         vare = 0.01f;
     154      356716 :         for ( i = 0; i < L_FRAME; i++ )
     155             :         {
     156      355328 :             vare += exc2[i] * exc2[i];
     157             :         }
     158             : 
     159        1388 :         ge = (float) sqrt( vare / (float) L_FRAME );
     160        1388 :         if ( *uv_count == 1 )
     161             :         {
     162         496 :             *ge_sm = ge;
     163             :         }
     164             :         else
     165             :         {
     166         892 :             *ge_sm = ISP_SMOOTHING_QUANT_A1 * *ge_sm + ( 1 - ISP_SMOOTHING_QUANT_A1 ) * ge;
     167             :         }
     168             : 
     169             :         /* generate mixture of excitation and noise */
     170        1388 :         beta = 2 * ( alpha - 0.5f );
     171        1388 :         noimix_fac = ( beta + *ge_sm / ge * ( 1 - beta ) ) / (float) sqrt( alpha * alpha + ( 1 - alpha ) * ( 1 - alpha ) );
     172             : 
     173      356716 :         for ( i = 0; i < L_FRAME; i++ )
     174             :         {
     175      355328 :             randval = ge * (float) sqrt( 12.0f ) * ( (float) own_random( noimix_seed ) / 65536.0f );
     176      355328 :             exc2[i] = noimix_fac * ( exc2[i] * alpha + randval * ( 1 - alpha ) );
     177             :         }
     178             : 
     179             :         /* generate low-pass filtered version of LSP coefficients */
     180       23596 :         for ( k = 0; k < M; k++ )
     181             :         {
     182       22208 :             lspnew_s[k] = (float) ISP_SMOOTHING_QUANT_A1 * lspold_s[k] + (float) ( 1 - ISP_SMOOTHING_QUANT_A1 ) * lsp_new[k];
     183             :         }
     184             : 
     185       23596 :         for ( i = 0; i < M; i++ )
     186             :         {
     187       22208 :             oldlsp_mix[i] = beta * lsp_old[i] + ( 1 - beta ) * lspold_s[i];
     188       22208 :             midlsp_mix[i] = beta * lsp_mid[i] + ( 1 - beta ) * 0.5f * ( lspold_s[i] + lspnew_s[i] );
     189       22208 :             newlsp_mix[i] = beta * lsp_new[i] + ( 1 - beta ) * lspnew_s[i];
     190             :         }
     191             : 
     192             :         /* redo the interpolation of LSP coefficients and recalculte A(z) */
     193        1388 :         int_lsp4( L_FRAME, oldlsp_mix, midlsp_mix, newlsp_mix, Aq, M, 0 );
     194             : 
     195        1388 :         mvr2r( lspnew_s, lspold_s, M );
     196             :     }
     197             :     else
     198             :     {
     199             :         /* active signal - reset counters */
     200      583745 :         ( *act_count )++;
     201      583745 :         if ( *act_count > 3 )
     202             :         {
     203      583686 :             *act_count = 3;
     204      583686 :             *uv_count = 0;
     205             :         }
     206             :     }
     207             : 
     208      585133 :     return;
     209             : }
     210             : 
     211             : /*---------------------------------------------------------------------------*
     212             :  * calc_tilt()
     213             :  *
     214             :  * Calculate spectral tilt by means of 1st-order LP analysis
     215             :  *---------------------------------------------------------------------------*/
     216             : 
     217             : /*! r: estimated tilt */
     218        2116 : static float calc_tilt(
     219             :     const float *x,   /* i  : Signal input   */
     220             :     const int16_t len /* i  : length         */
     221             : )
     222             : {
     223             :     int16_t i;
     224             :     float r0, r1;
     225             : 
     226        2116 :     r0 = 0;
     227        2116 :     r1 = 0;
     228             : 
     229      135424 :     for ( i = 0; i < len - 1; i++ )
     230             :     {
     231      133308 :         r0 += x[i] * x[i];
     232      133308 :         r1 += x[i] * x[i + 1];
     233             :     }
     234             : 
     235        2116 :     if ( r0 == 0 )
     236             :     {
     237           0 :         r0 = 0.005f;
     238             :     }
     239             : 
     240        2116 :     return r1 / r0;
     241             : }

Generated by: LCOV version 1.14