LCOV - code coverage report
Current view: top level - lib_enc - find_tilt.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 55 59 93.2 %
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 "cnst.h"
      44             : #include "prot.h"
      45             : #include "wmc_auto.h"
      46             : 
      47             : /*---------------------------------------------------------------------*
      48             :  * Local constants
      49             :  *---------------------------------------------------------------------*/
      50             : 
      51             : #define FACT   3.0f                              /* background noise energy estimation adjusting factor - to maintain the ADR about the same */
      52             : #define TH_COR 0.6f                              /* Minimum correlation for per bin processing                    */
      53             : #define TH_D   50.0f                             /* Difference limit between nearest harmonic and a frequency bin */
      54             : #define TH_PIT ( INT_FS_12k8 / ( 2.0f * TH_D ) ) /* Maximum pitch for per bin processing                          */
      55             : 
      56             : /*-------------------------------------------------------------------*
      57             :  * find_tilt()
      58             :  *
      59             :  * Find LF/HF energy ratio
      60             :  *-------------------------------------------------------------------*/
      61             : 
      62     1136044 : void find_tilt(
      63             :     const float fr_bands[],   /* i  : energy in frequency bands                  */
      64             :     const float bckr[],       /* i  : per band background noise energy estimate  */
      65             :     float ee[2],              /* o  : lf/hf E ration for present frame           */
      66             :     const int16_t pitch[3],   /* i  : open loop pitch values for 3 half-frames   */
      67             :     const float voicing[3],   /* i  : normalized correlation for 3 half-frames   */
      68             :     const float *lf_E,        /* i  : per bin energy  for low frequencies        */
      69             :     const float corr_shift,   /* i  : normalized correlation correction          */
      70             :     const int16_t bwidth,     /* i  : input signal bandwidth                     */
      71             :     const int16_t max_band,   /* i  : maximum critical band                      */
      72             :     float hp_E[],             /* o  : energy in HF                               */
      73             :     const int16_t codec_mode, /* i  : Mode 1 or 2                                */
      74             :     float *bckr_tilt_lt,      /* i/o: lf/hf E ratio of background noise          */
      75             :     int16_t Opt_vbr_mode )
      76             : {
      77             :     float lp_bckr, hp_bckr, lp_E, freq, f0, f1, f2, mean_voi, bin;
      78             :     const float *pt_bands, *pt_bckr, *tmp_E, *hf_bands, *pt_E;
      79             :     int16_t cnt, i, nb_bands;
      80             :     float th_pit;
      81             : 
      82             :     /*-----------------------------------------------------------------*
      83             :      * Initializations
      84             :      *-----------------------------------------------------------------*/
      85             : 
      86     1136044 :     th_pit = TH_PIT;
      87             : 
      88     1136044 :     if ( bwidth != NB )
      89             :     {
      90             :         /* WB processing */
      91     1132033 :         bin = BIN; /* First useful frequency bin ~ 50 Hz     */
      92     1132033 :         pt_bands = fr_bands;
      93     1132033 :         tmp_E = lf_E;
      94     1132033 :         pt_bckr = bckr;
      95     1132033 :         nb_bands = 10;
      96             :     }
      97             :     else
      98             :     {
      99             :         /* NB processing */
     100        4011 :         bin = 3.0f * BIN;        /* first useful frequency bin ~ 150 Hz    */
     101        4011 :         pt_bands = fr_bands + 1; /* exlcude 1st critical band              */
     102        4011 :         tmp_E = lf_E + 2;        /* start at the 3rd bin (150 Hz)          */
     103        4011 :         pt_bckr = bckr + 1;      /* exclude 1st critical band              */
     104        4011 :         nb_bands = 9;            /* nb. of "low" frequency bands taken into account in NB processing      */
     105             :     }
     106             : 
     107             :     /*-----------------------------------------------------------------*
     108             :      * Find spectrum tilt
     109             :      *-----------------------------------------------------------------*/
     110             : 
     111     1136044 :     pt_E = tmp_E; /* pointer at the 1st useful element of the per-bin energy vector  */
     112     1136044 :     hf_bands = fr_bands;
     113             : 
     114             :     /* bckr + voicing */
     115     1136044 :     lp_bckr = mean( pt_bckr, nb_bands );                      /* estimated noise E in first critical bands, up to 1270 Hz */
     116     1136044 :     hp_bckr = 0.5f * ( bckr[max_band - 1] + bckr[max_band] ); /* estimated noise E in last 2 critical bands */
     117     1136044 :     *bckr_tilt_lt = 0.9f * *bckr_tilt_lt + 0.1f * lp_bckr / hp_bckr;
     118             : 
     119     1136044 :     if ( codec_mode == MODE2 || Opt_vbr_mode )
     120             :     {
     121        1050 :         lp_bckr *= FACT;
     122        1050 :         hp_bckr *= FACT;
     123             :     }
     124             : 
     125     1136044 :     mean_voi = 0.5f * ( voicing[1] + voicing[2] ) + corr_shift;
     126     1136044 :     f0 = ( (float) INT_FS_12k8 ) / pitch[2];
     127             : 
     128     3408132 :     for ( i = 0; i < 2; i++ )
     129             :     {
     130     2272088 :         hp_E[i] = 0.5f * ( hf_bands[max_band - 1] + hf_bands[max_band] ) - hp_bckr; /* average E in last 2 critical bands */
     131             : 
     132     2272088 :         if ( !Opt_vbr_mode )
     133             :         {
     134     2272088 :             if ( hp_E[i] < E_MIN )
     135             :             {
     136             :                 /* to avoid division by 0 */
     137      356622 :                 hp_E[i] = E_MIN;
     138             :             }
     139             :         }
     140             :         else
     141             :         {
     142           0 :             if ( hp_E[i] < 1.0f )
     143             :             {
     144             :                 /* to avoid division by 0 */
     145           0 :                 hp_E[i] = 1.0f;
     146             :             }
     147             :         }
     148             : 
     149             : 
     150     2272088 :         if ( ( mean_voi > TH_COR ) && ( pitch[2] < th_pit ) )
     151             :         {
     152             :             /* high-pitched voiced frames */
     153     1241464 :             freq = bin; /* 1st useful frequency bin */
     154     1241464 :             cnt = 0;
     155     1241464 :             lp_E = 0.0f;
     156     1241464 :             f1 = 1.5f * f0; /* middle between 2 harmonics */
     157     1241464 :             f2 = f0;
     158             : 
     159     8880352 :             while ( freq <= 1270.0f ) /* end frequency of 10th critical band */
     160             :             {
     161             :                 /*pt_E*/
     162    41082392 :                 while ( freq <= f1 )
     163             :                 {
     164    33443504 :                     if ( fabs( freq - f2 ) < TH_D ) /* include only bins sufficiently close to harmonics */
     165             :                     {
     166    14956930 :                         lp_E += *pt_E;
     167    14956930 :                         cnt++;
     168             :                     }
     169    33443504 :                     freq += BIN;
     170    33443504 :                     pt_E++;
     171             :                 }
     172     7638888 :                 f1 += f0;
     173     7638888 :                 f2 += f0; /* next harmonic */
     174             :             }
     175             : 
     176     1241464 :             lp_E = lp_E / (float) cnt - lp_bckr;
     177     1241464 :             pt_E = tmp_E + VOIC_BINS; /* update for next half-frame */
     178             :         }
     179             :         else
     180             :         {
     181             :             /* other than high-pitched voiced frames */
     182     1030624 :             lp_E = mean( pt_bands, nb_bands ) - lp_bckr; /* average E in first critical bands, up to 1270 Hz */
     183             :         }
     184             : 
     185     2272088 :         if ( !Opt_vbr_mode )
     186             :         {
     187     2272088 :             if ( lp_E < E_MIN )
     188             :             {
     189             :                 /* avoid negative E due to noise subtraction */
     190      296469 :                 lp_E = E_MIN;
     191             :             }
     192             :         }
     193             :         else
     194             :         {
     195             : 
     196           0 :             if ( lp_E < 0.0f )
     197             :             {
     198             :                 /* avoid negative E due to noise subtraction */
     199           0 :                 lp_E = 0.0f;
     200             :             }
     201             :         }
     202             : 
     203             :         /* calculate the tilt (LF/HF ratio) */
     204     2272088 :         ee[i] = lp_E / hp_E[i];
     205             : 
     206     2272088 :         if ( bwidth == NB ) /* for NB input, compensate for the missing bands */
     207             :         {
     208        8022 :             ee[i] *= 6.0f;
     209             :         }
     210             : 
     211     2272088 :         pt_bands += NB_BANDS; /* update pointers for the next half-frame */
     212     2272088 :         hf_bands += NB_BANDS;
     213             :     }
     214             : 
     215     1136044 :     return;
     216             : }

Generated by: LCOV version 1.14