LCOV - code coverage report
Current view: top level - lib_com - hq2_noise_inject.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 60 84 71.4 %
Date: 2025-05-23 08:37:30 Functions: 1 1 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             :  * hq2_noise_inject()
      48             :  *
      49             :  * HQ2 noise injection for WB signals
      50             :  *--------------------------------------------------------------------------*/
      51             : 
      52         136 : void hq2_noise_inject(
      53             :     float y2hat[],
      54             :     const int16_t band_start[],
      55             :     const int16_t band_end[],
      56             :     const int16_t band_width[],
      57             :     float Ep[],
      58             :     float Rk[],
      59             :     const int16_t npulses[],
      60             :     int16_t ni_seed,
      61             :     const int16_t bands,
      62             :     const int16_t ni_start_band,
      63             :     const int16_t bw_low,
      64             :     const int16_t bw_high,
      65             :     const float enerL,
      66             :     const float enerH,
      67             :     float last_ni_gain[],
      68             :     float last_env[],
      69             :     int16_t *last_max_pos_pulse,
      70             :     int16_t *p2a_flags,
      71             :     int16_t p2a_bands,
      72             :     const int16_t hqswb_clas,
      73             :     const int16_t bwidth,
      74             :     const int32_t bwe_br )
      75             : {
      76         136 :     int16_t i, j, k, ni_end_band, satur = 0, count[BANDS_MAX], max_pos_pulse, pos;
      77             :     float ni_gain[BANDS_MAX], pd[BANDS_MAX], rand, peak[BANDS_MAX], fac, env[BANDS_MAX];
      78         136 :     int16_t sb = bands;
      79             : 
      80         136 :     if ( ( hqswb_clas == HQ_HARMONIC || hqswb_clas == HQ_NORMAL ) && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) && bwidth == SWB )
      81             :     {
      82         124 :         sb = ( bwe_br == HQ_16k40 ) ? 19 : 17;
      83             :     }
      84             : 
      85             :     /* calculate the envelopes/ the decoded peak coeff./number of the decoded coeff./ the last subbands of the bit-allocated/saturation of bit-allocation */
      86         136 :     ni_end_band = bands;
      87         136 :     max_pos_pulse = bands;
      88        3248 :     for ( k = ni_start_band; k < ni_end_band; k++ )
      89             :     {
      90        3112 :         pd[k] = (float) Rk[k] / band_width[k];
      91        3112 :         env[k] = (float) sqrt( Ep[k] / band_width[k] );
      92             : 
      93        3112 :         peak[k] = 0.0f;
      94        3112 :         count[k] = 0;
      95        3112 :         if ( npulses[k] != 0 )
      96             :         {
      97       30044 :             for ( i = band_start[k]; i <= band_end[k]; i++ )
      98             :             {
      99       27960 :                 Ep[k] -= y2hat[i] * y2hat[i];
     100       27960 :                 if ( fabs( y2hat[i] ) > peak[k] )
     101             :                 {
     102        2456 :                     peak[k] = (float) fabs( y2hat[i] );
     103             :                 }
     104             : 
     105       27960 :                 if ( y2hat[i] != 0 )
     106             :                 {
     107        3972 :                     count[k]++;
     108             :                 }
     109             :             }
     110             : 
     111        2084 :             max_pos_pulse = k;
     112        2084 :             Ep[k] = Ep[k] > 0 ? (float) sqrt( Ep[k] / band_width[k] ) : 0.0f;
     113             :         }
     114             :         else
     115             :         {
     116        1028 :             Ep[k] = env[k];
     117             :         }
     118             :     }
     119             : 
     120        3248 :     for ( k = ni_start_band; k < ni_end_band; k++ )
     121             :     {
     122             :         /* calculate the noise gain */
     123        3112 :         satur = ( pd[k] >= 0.8f ) ? 1 : 0;
     124        3112 :         if ( satur == 0 && Ep[k] > 0 )
     125             :         {
     126        2132 :             if ( npulses[k] != 0 )
     127             :             {
     128        1108 :                 if ( bwidth == SWB )
     129             :                 {
     130        1108 :                     if ( hqswb_clas != HQ_TRANSIENT )
     131             :                     {
     132        1024 :                         if ( hqswb_clas == HQ_HARMONIC )
     133             :                         {
     134           0 :                             fac = ( k <= sb ) ? 6.0f * ( 1.5f - pd[k] ) * env[k] * Ep[k] / ( peak[k] * peak[k] ) : 1.5f * Ep[k] / peak[k];
     135             :                         }
     136             :                         else
     137             :                         {
     138        1024 :                             fac = ( k <= sb ) ? 5.0f * ( 1.5f - pd[k] ) * Ep[k] / ( peak[k] ) : 4.0f * Ep[k] / peak[k];
     139             :                         }
     140             :                     }
     141             :                     else
     142             :                     {
     143          84 :                         fac = 1.1f;
     144             :                     }
     145             :                 }
     146             :                 else
     147             :                 {
     148           0 :                     fac = 20.0f * min( 1.0f, ( 1.5f - pd[k] ) ) * env[k] * Ep[k] / ( peak[k] * peak[k] );
     149           0 :                     if ( k > 1 && k < ni_end_band - 1 )
     150             :                     {
     151           0 :                         if ( count[k + 1] == 0 && env[k] > 0.5f * env[k + 1] && env[k] < 2.0f * env[k - 1] )
     152             :                         {
     153           0 :                             fac = 1.5f * env[k + 1] / env[k];
     154             :                         }
     155           0 :                         else if ( count[k - 1] == 0 && peak[k] > 2.0f * env[k] )
     156             :                         {
     157           0 :                             fac = env[k - 1] / env[k];
     158             :                         }
     159             :                     }
     160             : 
     161           0 :                     if ( k >= ni_end_band - p2a_bands && bwidth == WB )
     162             :                     {
     163           0 :                         if ( bw_low * enerH > bw_high * enerL && peak[k] < 2.0f * env[k] )
     164             :                         {
     165           0 :                             fac *= ( 2.0f - Ep[k] / peak[k] );
     166             :                         }
     167             : 
     168           0 :                         if ( p2a_flags[k] == 0 && fac > 0 )
     169             :                         {
     170           0 :                             fac *= min( 1.25f * env[k] / ( Ep[k] * fac ), 1.5f );
     171             :                         }
     172             :                     }
     173             :                 }
     174             :             }
     175             :             else
     176             :             {
     177        1024 :                 fac = ( hqswb_clas == HQ_HARMONIC && bwidth == SWB ) ? 0.8f : 1.1f;
     178             :             }
     179             : 
     180        2132 :             ni_gain[k] = fac * Ep[k];
     181             :         }
     182             :         else
     183             :         {
     184         980 :             ni_gain[k] = 0.0;
     185             :         }
     186             : 
     187             :         /* smooth the noise gain between the current frame and the previous frame */
     188        3112 :         pos = bwidth == SWB ? ni_end_band - 1 : max( max_pos_pulse, *last_max_pos_pulse );
     189             : 
     190        3112 :         if ( k <= pos )
     191             :         {
     192        3112 :             if ( k > 0 && k < ni_end_band - 1 )
     193             :             {
     194        2840 :                 if ( ( env[k] > 0.5f * last_env[k] && env[k] < 2.0f * last_env[k] ) ||
     195        1556 :                      ( ( ( env[k] + env[k - 1] + env[k + 1] ) > 0.5f * ( last_env[k] + last_env[k - 1] + last_env[k + 1] ) ) &&
     196        1372 :                        ( ( env[k] + env[k - 1] + env[k + 1] ) < 2.0f * ( last_env[k] + last_env[k - 1] + last_env[k + 1] ) ) ) )
     197             :                 {
     198        1472 :                     if ( ni_gain[k] > last_ni_gain[k] )
     199             :                     {
     200         368 :                         ni_gain[k] = 0.2f * ni_gain[k] + 0.8f * last_ni_gain[k];
     201             :                     }
     202             :                     else
     203             :                     {
     204        1104 :                         ni_gain[k] = 0.6f * ni_gain[k] + 0.4f * last_ni_gain[k];
     205             :                     }
     206             :                 }
     207             :             }
     208         272 :             else if ( k == ni_end_band - 1 )
     209             :             {
     210         136 :                 if ( ( env[k] > 0.5f * last_env[k] && env[k] < 2.0f * last_env[k] ) ||
     211          88 :                      ( ( env[k] + env[k - 1] ) > 0.5f * ( last_env[k] + last_env[k - 1] ) &&
     212          68 :                        ( env[k] + env[k - 1] ) < 2.0f * ( last_env[k] + last_env[k - 1] ) ) )
     213             :                 {
     214          48 :                     if ( ni_gain[k] > last_ni_gain[k] )
     215             :                     {
     216          16 :                         ni_gain[k] = 0.2f * ni_gain[k] + 0.8f * last_ni_gain[k];
     217             :                     }
     218             :                     else
     219             :                     {
     220          32 :                         ni_gain[k] = 0.6f * ni_gain[k] + 0.4f * last_ni_gain[k];
     221             :                     }
     222             :                 }
     223             :             }
     224             :         }
     225             : 
     226             :         /* inject noise into the non-decoded coeffs */
     227        3112 :         if ( k >= ni_end_band - p2a_bands && p2a_flags[k] == 0 && bwidth != SWB )
     228             :         {
     229           0 :             for ( i = band_start[k]; i <= band_end[k]; i++ )
     230             :             {
     231           0 :                 if ( y2hat[i] != 0 )
     232             :                 {
     233           0 :                     y2hat[i] *= 0.8f;
     234             :                 }
     235             :             }
     236             :         }
     237             : 
     238        3112 :         if ( k == max_pos_pulse && k < bands - p2a_bands && satur != 1 && bwidth != SWB )
     239             :         {
     240           0 :             j = 0;
     241           0 :             if ( ni_gain[k] < 0.01f )
     242             :             {
     243           0 :                 fac = 0.0f;
     244             :             }
     245             :             else
     246             :             {
     247           0 :                 fac = max( Ep[k] / ni_gain[k], 1.0f );
     248             :             }
     249           0 :             for ( i = band_start[k]; i <= band_end[k]; i++ )
     250             :             {
     251           0 :                 if ( y2hat[i] == 0 )
     252             :                 {
     253           0 :                     rand = own_random( &ni_seed ) / PCM16_TO_FLT_FAC;
     254           0 :                     y2hat[i] += ( fac - ( ( fac - 1.0f ) * j ) / band_width[k] ) * ni_gain[k] * rand;
     255             :                 }
     256             : 
     257           0 :                 j++;
     258             :             }
     259             :         }
     260             :         else
     261             :         {
     262       80360 :             for ( i = band_start[k]; i <= band_end[k]; i++ )
     263             :             {
     264       77248 :                 if ( y2hat[i] == 0 )
     265             :                 {
     266       73276 :                     rand = own_random( &ni_seed ) / PCM16_TO_FLT_FAC;
     267       73276 :                     y2hat[i] += ni_gain[k] * rand;
     268             :                 }
     269             :             }
     270             :         }
     271             :     }
     272             : 
     273         136 :     mvr2r( env, last_env, ni_end_band );
     274         136 :     mvr2r( ni_gain, last_ni_gain, ni_end_band );
     275         136 :     *last_max_pos_pulse = max_pos_pulse;
     276             : 
     277         136 :     return;
     278             : }

Generated by: LCOV version 1.14