LCOV - code coverage report
Current view: top level - lib_com - stab_est.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 50 50 100.0 %
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 "rom_com.h"
      45             : #include "prot.h"
      46             : #include "wmc_auto.h"
      47             : 
      48             : /*-------------------------------------------------------------------*
      49             :  * Local constants
      50             :  *-------------------------------------------------------------------*/
      51             : 
      52             : #define BIN_4000     80 /* The frequency bin corresponding to 4kHz */
      53             : #define MAX_SNR1     45.0f
      54             : #define INV_MAX_SNR  ( 1.0f / ( MAX_SNR1 - 1.0f ) ) /* Max. SNR considered for noise subtraction in voiced segments */
      55             : #define MAX_SNR_SNR1 ( MAX_SNR1 * INV_MAX_SNR )     /* 45 * (1 / (MAX_SNR1-1)) */
      56             : #define MAX_BANDEXC  20
      57             : #define TH_0_MAX     ( 1.5f * 3.125f )
      58             : #define TH_1_MAX     ( 1.5f * 2.8125f )
      59             : #define TH_2_MAX     ( 1.5f * 2.1875f )
      60             : #define TH_3_MAX     ( 1.5f * 1.875f )
      61             : 
      62             : #define TH_UP      0.15625f
      63             : #define TH_DW      0.15625f
      64             : #define NB_TH3_MIN 30
      65             : #define NB_TH1_MIN 30
      66             : 
      67             : #define MAX_THR 0.92f
      68             : 
      69             : /*------------------------------------------------------------------------*
      70             :  * stab_est()
      71             :  *
      72             :  * Signal stability estimation based on energy variation
      73             :  *------------------------------------------------------------------------*/
      74             : 
      75      447627 : int16_t stab_est(
      76             :     float etot,               /* i  : Total energy of the current frame         */
      77             :     float *lt_diff_etot,      /* i/o: Long term total energy variation          */
      78             :     float *mem_etot,          /* i/o: Total energy memory                       */
      79             :     int16_t *nb_thr_3,        /* i/o: Number of consecutives frames of level 3  */
      80             :     int16_t *nb_thr_1,        /* i/o: Number of consecutives frames of level 1  */
      81             :     float *thresh,            /* i/o: Detection thresold                        */
      82             :     int16_t *last_music_flag, /* i/o: Previous music detection ouptut           */
      83             :     const int16_t vad_flag    /* i  : VAD flag                                  */
      84             : )
      85             : {
      86             :     int16_t i, music_flag2;
      87             :     float mean_diff;
      88             :     float ftmp_c, fcorr, dev;
      89             : 
      90             :     /*------------------------------------------------------------------------*
      91             :      * Find mean of the past MAX_LT frames energy variation
      92             :      *------------------------------------------------------------------------*/
      93             : 
      94      447627 :     mean_diff = 0.0f;
      95    17905080 :     for ( i = 1; i < MAX_LT; i++ )
      96             :     {
      97    17457453 :         mean_diff += lt_diff_etot[i - 1] * INV_MAX_LT; /* divide by MAX_LT */
      98    17457453 :         lt_diff_etot[i - 1] = lt_diff_etot[i];
      99             :     }
     100             : 
     101      447627 :     mean_diff += lt_diff_etot[i - 1] * INV_MAX_LT; /* divide by MAX_LT */
     102             : 
     103             :     /*------------------------------------------------------------------------*
     104             :      * Find statistical deviation of the energy variation history
     105             :      * against the last 15 frames
     106             :      *------------------------------------------------------------------------*/
     107             : 
     108      447627 :     fcorr = 0.0f;
     109     7162032 :     for ( i = MAX_LT - 15; i < MAX_LT; i++ )
     110             :     {
     111     6714405 :         ftmp_c = lt_diff_etot[i] - mean_diff;
     112     6714405 :         fcorr += ftmp_c * ftmp_c;
     113             :     }
     114             : 
     115      447627 :     lt_diff_etot[i - 1] = etot - *mem_etot;
     116      447627 :     *mem_etot = etot;
     117             : 
     118             :     /*------------------------------------------------------------------------*
     119             :      * Compute statistical deviation
     120             :      *------------------------------------------------------------------------*/
     121             : 
     122      447627 :     dev = (float) sqrt( fcorr / ( MAX_LT - 15 ) );
     123             : 
     124             :     /*------------------------------------------------------------------------*
     125             :      * State machine to decide level of inter-harmonic noise reduction and
     126             :      * the first bins where this inter-harmonic noise reduction will be applied
     127             :      * (only if this frame is GOOD or if we are already far from NB_BFI_THR)
     128             :      * (if music_flag2 is 0, the spectral modification is deactivated, otherwise, it is activated)
     129             :      *------------------------------------------------------------------------*/
     130             : 
     131      447627 :     music_flag2 = 0;
     132             : 
     133             :     /*--------------------------------------------------------------------*
     134             :      * statistical deviation < Thresh3 and last signal category type >= 3
     135             :      * (last category was "tonal" and the new one is "very tonal")
     136             :      *--------------------------------------------------------------------*/
     137             : 
     138      447627 :     if ( dev < thresh[3] && *last_music_flag >= 3 )
     139             :     {
     140      143896 :         music_flag2 = 4;
     141      143896 :         *nb_thr_3 += 1;
     142      143896 :         *nb_thr_1 = 0;
     143             :     }
     144             : 
     145             :     /*--------------------------------------------------------------------*
     146             :      * statistical deviation < Thresh2 and last signal category type >= 2
     147             :      * (last category was "moderatly tonal" and the new one is a "tonal" )
     148             :      *--------------------------------------------------------------------*/
     149             : 
     150      303731 :     else if ( dev < thresh[2] && *last_music_flag >= 2 )
     151             :     {
     152       16035 :         music_flag2 = 3;
     153       16035 :         *nb_thr_3 += 1;
     154       16035 :         *nb_thr_1 = 0;
     155             :     }
     156             : 
     157             :     /*--------------------------------------------------------------------*
     158             :      * statistical deviation < Thresh1 and last signal category type  >= 1
     159             :      * (last category was "slightly tonal" and the new one is a "moderatly tonal")
     160             :      *--------------------------------------------------------------------*/
     161             : 
     162      287696 :     else if ( dev < thresh[1] && *last_music_flag >= 1 )
     163             :     {
     164       26745 :         music_flag2 = 2;
     165             :     }
     166             : 
     167             :     /*--------------------------------------------------------------------*
     168             :      * statistical deviation < Thresh0
     169             :      * (last category was "not tonal" and the new one is "slightly tonal")
     170             :      *--------------------------------------------------------------------*/
     171             : 
     172      260951 :     else if ( dev < thresh[0] )
     173             :     {
     174       65261 :         music_flag2 = 1;
     175             :     }
     176             : 
     177             :     /*--------------------------------------------------------------------*
     178             :      * statistical deviation > Thresh0
     179             :      * (Statistical deviation is high: the new tonal category is not tonal)
     180             :      *--------------------------------------------------------------------*/
     181             : 
     182             :     else
     183             :     {
     184      195690 :         *nb_thr_1 += 1;
     185      195690 :         *nb_thr_3 = 0;
     186             :     }
     187             : 
     188             :     /*------------------------------------------------------------------------*
     189             :      * Update the thresholds
     190             :      *------------------------------------------------------------------------*/
     191             : 
     192      447627 :     if ( *nb_thr_3 > NB_TH3_MIN )
     193             :     {
     194             :         /* the number of consecutive categories type 3 or 4 (most tonal and tonal) */
     195             :         /* is greater than 30 frames ->increase the deviations thresholds to allow more variation */
     196      130736 :         thresh[0] += TH_UP;
     197      130736 :         thresh[1] += TH_UP;
     198      130736 :         thresh[2] += TH_UP;
     199      130736 :         thresh[3] += TH_UP;
     200             :     }
     201      316891 :     else if ( *nb_thr_1 > NB_TH1_MIN )
     202             :     {
     203             :         /* the number of consecutive categories type 0 (non tonal frames) */
     204             :         /* is greater than 30 frames -> decrease the deviations thresholds to allow less variation */
     205      164625 :         thresh[0] -= TH_DW;
     206      164625 :         thresh[1] -= TH_DW;
     207      164625 :         thresh[2] -= TH_DW;
     208      164625 :         thresh[3] -= TH_DW;
     209             :     }
     210             : 
     211             :     /* limitation of the threshold (this local macro stores the highest of the two and it also counts the # of operations) */
     212      447627 :     set_max( &thresh[0], TH_0_MIN2 );
     213      447627 :     set_max( &thresh[1], TH_1_MIN2 );
     214      447627 :     set_max( &thresh[2], TH_2_MIN2 );
     215             : 
     216      447627 :     set_min( &thresh[0], TH_0_MAX );
     217      447627 :     set_min( &thresh[1], TH_1_MAX );
     218      447627 :     set_min( &thresh[2], TH_2_MAX );
     219             : 
     220      447627 :     set_max( &thresh[3], TH_3_MIN2 );
     221      447627 :     set_min( &thresh[3], TH_3_MAX );
     222             :     /*------------------------------------------------------------------------*
     223             :      * Final updates
     224             :      *------------------------------------------------------------------------*/
     225             : 
     226      447627 :     *last_music_flag = music_flag2;
     227      447627 :     if ( vad_flag == 0 )
     228             :     {
     229             :         /* overwrite decision in unvoiced frames */
     230       61752 :         music_flag2 = 0;
     231             :     }
     232             : 
     233      447627 :     return music_flag2;
     234             : }

Generated by: LCOV version 1.14