LCOV - code coverage report
Current view: top level - lib_enc - update_decision.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 99 100 99.0 %
Date: 2025-05-28 04:28:20 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             : #include <math.h>
      40             : #include "prot.h"
      41             : #include "wmc_auto.h"
      42             : 
      43             : 
      44             : /*-------------------------------------------------------------------*
      45             :  * bg_music_decision()
      46             :  *
      47             :  *
      48             :  *-------------------------------------------------------------------*/
      49             : 
      50       96918 : void bg_music_decision(
      51             :     VAD_CLDFB_HANDLE hVAD_CLDFB, /* i/o: CLDFB VAD state        */
      52             :     int16_t *music_backgound_f,  /* i  : background music flag  */
      53             :     const float frame_energy     /* i  : current frame energy 1 */
      54             : )
      55             : {
      56       96918 :     int16_t music_background_frame = 0;
      57       96918 :     float *sp_center = hVAD_CLDFB->sp_center;
      58       96918 :     float *ltd_stable_rate = hVAD_CLDFB->ltd_stable_rate;
      59       96918 :     float *sSFM = hVAD_CLDFB->sfm;
      60       96918 :     float *f_tonality_rate = hVAD_CLDFB->f_tonality_rate;
      61             : 
      62       96918 :     if ( ( f_tonality_rate[1] > 0.60 ) || ( f_tonality_rate[0] > 0.86 ) )
      63             :     {
      64       45392 :         if ( ltd_stable_rate[0] < 0.072 && sp_center[0] > 1.2 && ( sSFM[0] < 0.76 || sSFM[1] < 0.88 || sSFM[2] < 0.96 ) )
      65             :         {
      66        2751 :             music_background_frame = 1;
      67             :         }
      68             :     }
      69             : 
      70       96918 :     if ( music_background_frame && ( 4.6 * hVAD_CLDFB->fg_energy_count * frame_energy > hVAD_CLDFB->fg_energy ) && ( hVAD_CLDFB->fg_energy_est_start == 1 ) )
      71             :     {
      72        1990 :         hVAD_CLDFB->music_background_rate = hVAD_CLDFB->music_background_rate * 0.975f + 0.025f;
      73             :     }
      74       94928 :     else if ( music_background_frame )
      75             :     {
      76         761 :         hVAD_CLDFB->music_background_rate = hVAD_CLDFB->music_background_rate * 0.998f + 0.002f;
      77             :     }
      78             :     else
      79             :     {
      80       94167 :         hVAD_CLDFB->music_background_rate = hVAD_CLDFB->music_background_rate * 0.997f;
      81             :     }
      82             : 
      83       96918 :     if ( hVAD_CLDFB->music_background_rate > 0.5 )
      84             :     {
      85        2841 :         *music_backgound_f = 1;
      86             :     }
      87             :     else
      88             :     {
      89       94077 :         *music_backgound_f = 0;
      90             :     }
      91             : 
      92       96918 :     return;
      93             : }
      94             : 
      95             : 
      96             : /*-------------------------------------------------------------------*
      97             :  * update_decision()
      98             :  *
      99             :  *
     100             :  *-------------------------------------------------------------------*/
     101             : 
     102       96918 : int16_t update_decision(
     103             :     VAD_CLDFB_HANDLE hVAD_CLDFB,    /* i/o: CLDFB VAD state                     */
     104             :     const float snr,                /* i  : frequency domain SNR                */
     105             :     const float tsnr,               /* i  : time domain SNR                     */
     106             :     const float frame_energy,       /* i  : current frame energy                */
     107             :     const float high_eng,           /* i  : current frame high frequency energy */
     108             :     const int16_t vad_flag,         /* i  : VAD flag                            */
     109             :     const int16_t music_backgound_f /* i  : background music flag               */
     110             : )
     111             : {
     112       96918 :     float g_high_eng_sacle = 0.0f;
     113             :     float sp_center3_diff;
     114       96918 :     int16_t update_flag = 1;
     115       96918 :     int16_t tonality_flag = 0;
     116       96918 :     int16_t frameloop = hVAD_CLDFB->frameloop;
     117       96918 :     int16_t bw = hVAD_CLDFB->bw_index;
     118       96918 :     float *sp_center = hVAD_CLDFB->sp_center;
     119       96918 :     float *ltd_stable_rate = hVAD_CLDFB->ltd_stable_rate;
     120       96918 :     float *sSFM = hVAD_CLDFB->sfm;
     121       96918 :     float *f_tonality_rate = hVAD_CLDFB->f_tonality_rate;
     122       96918 :     float tmpout = frame_energy - 4 * hVAD_CLDFB->frame_energy_smooth;
     123             : 
     124       96918 :     g_high_eng_sacle = high_eng / ( hVAD_CLDFB->lt_bg_highf_eng + FLT_MIN );
     125       96918 :     sp_center3_diff = sp_center[3] - hVAD_CLDFB->lt_noise_sp_center3;
     126             : 
     127       96918 :     if ( frameloop > 50 )
     128             :     {
     129       84000 :         if ( ltd_stable_rate[0] > 0.12 )
     130             :         {
     131       65707 :             update_flag = 0;
     132             :         }
     133             : 
     134       84000 :         if ( ( bw == CLDFBVAD_WB_ID || bw == CLDFBVAD_SWB_ID ) && hVAD_CLDFB->frame_energy_smooth < 4 * frame_energy )
     135             :         {
     136       51679 :             if ( g_high_eng_sacle > 3.0f && ( sp_center3_diff > 0.4 ) )
     137             :             {
     138       14477 :                 update_flag = 0;
     139             :             }
     140       51679 :             if ( ( sp_center[3] > 2.8f ) && ( ltd_stable_rate[0] > 0.02f ) )
     141             :             {
     142        9792 :                 update_flag = 0;
     143             :             }
     144             :         }
     145             :     }
     146             : 
     147       96918 :     if ( ( f_tonality_rate[1] > 0.50 ) && ( ltd_stable_rate[0] > 0.1 ) )
     148             :     {
     149       56162 :         update_flag = 0;
     150             :     }
     151       96918 :     if ( sSFM[1] < 0.92 && sSFM[0] < 0.92 && sSFM[2] < 0.92 )
     152             :     {
     153       40449 :         update_flag = 0;
     154             :     }
     155       96918 :     if ( sSFM[0] < 0.80 || sSFM[1] < 0.78 || sSFM[2] < 0.80 )
     156             :     {
     157       61113 :         update_flag = 0;
     158             :     }
     159             : 
     160       96918 :     if ( frame_energy > 32 * hVAD_CLDFB->frame_energy_smooth )
     161             :     {
     162         787 :         update_flag = 0;
     163             :     }
     164       96918 :     if ( ( 4.6 * hVAD_CLDFB->fg_energy_count * frame_energy > hVAD_CLDFB->fg_energy ) && ( hVAD_CLDFB->fg_energy_est_start == 1 ) && ( frame_energy > 3 ) )
     165             :     {
     166       45785 :         update_flag = 0;
     167             :     }
     168       96918 :     if ( ( f_tonality_rate[1] > 0.60 ) || ( f_tonality_rate[0] > 0.86 ) )
     169             :     {
     170       45392 :         update_flag = 0;
     171       45392 :         tonality_flag = 1;
     172             :     }
     173             : 
     174       96918 :     if ( tonality_flag )
     175             :     {
     176       45392 :         hVAD_CLDFB->tonality_rate3 = hVAD_CLDFB->tonality_rate3 * 0.983f + 0.017f;
     177             :     }
     178             :     else
     179             :     {
     180       51526 :         hVAD_CLDFB->tonality_rate3 = hVAD_CLDFB->tonality_rate3 * 0.983f;
     181             :     }
     182             : 
     183       96918 :     if ( hVAD_CLDFB->tonality_rate3 > 0.5 )
     184             :     {
     185       36638 :         update_flag = 0;
     186             :     }
     187             : 
     188       96918 :     if ( ( sp_center[0] > 4.0f ) && ltd_stable_rate[0] > 0.04 )
     189             :     {
     190        3662 :         update_flag = 0;
     191             :     }
     192       96918 :     if ( ( f_tonality_rate[1] > 0.46 ) && ( ( sSFM[1] > 0.93 ) || ( ltd_stable_rate[0] > 0.09 ) ) )
     193             :     {
     194       68304 :         update_flag = 0;
     195             :     }
     196       96918 :     if ( ( sSFM[1] < 0.93 && sSFM[0] < 0.92 && sSFM[2] < 0.97 ) && ( f_tonality_rate[1] > 0.5 ) )
     197             :     {
     198       47675 :         update_flag = 0;
     199             :     }
     200       96918 :     if ( ( f_tonality_rate[1] > 0.43 ) && ( sSFM[0] < 0.95 ) && ( sp_center[1] > 1.94f ) )
     201             :     {
     202           0 :         update_flag = 0;
     203             :     }
     204             : 
     205       96918 :     if ( update_flag )
     206             :     {
     207        5490 :         if ( hVAD_CLDFB->update_count < 1000 )
     208             :         {
     209        5490 :             hVAD_CLDFB->update_count = hVAD_CLDFB->update_count + 1;
     210             :         }
     211             :     }
     212             : 
     213       96918 :     if ( update_flag )
     214             :     {
     215        5490 :         hVAD_CLDFB->lt_noise_sp_center3 = 0.9f * hVAD_CLDFB->lt_noise_sp_center3 + 0.1f * sp_center[3];
     216             :     }
     217       96918 :     if ( ( tmpout > 0 ) && ( frameloop < 100 ) && ( f_tonality_rate[1] < 0.56 ) && ( ( sp_center[0] < 1.36 ) || ltd_stable_rate[0] < 0.03 ) )
     218             :     {
     219        1103 :         update_flag = 1;
     220             :     }
     221       96918 :     if ( snr < 0.3 && tmpout < 0 && tsnr < 1.2 && vad_flag == 0 && f_tonality_rate[1] < 0.5 && ( music_backgound_f == 0 ) && ltd_stable_rate[3] < 0.1 )
     222             : 
     223             :     {
     224        2598 :         update_flag = 1;
     225             :     }
     226       96918 :     if ( vad_flag && ( snr > 1.0 ) && bw == CLDFBVAD_SWB_ID && tmpout > 0 )
     227             :     {
     228        6737 :         update_flag = 0;
     229             :     }
     230             : 
     231       96918 :     if ( vad_flag && ( snr > 1.5 ) && bw != CLDFBVAD_SWB_ID && tmpout > 0 )
     232             :     {
     233        2710 :         update_flag = 0;
     234             :     }
     235             : 
     236       96918 :     if ( update_flag == 0 )
     237             :     {
     238       91055 :         hVAD_CLDFB->update_num_with_snr = 0;
     239             :     }
     240             :     else
     241             :     {
     242        5863 :         if ( vad_flag && ( snr > 3.0 ) && hVAD_CLDFB->update_num_with_snr < 10 )
     243             :         {
     244         323 :             update_flag = 0;
     245         323 :             hVAD_CLDFB->update_num_with_snr++;
     246             :         }
     247             :     }
     248             : 
     249       96918 :     if ( vad_flag == 0 || update_flag == 1 )
     250             :     {
     251        9880 :         float tmpp = (float) fabs( hVAD_CLDFB->sp_center[2] - hVAD_CLDFB->lt_noise_sp_center0 );
     252             : 
     253        9880 :         if ( tmpp > 2.5 )
     254             :         {
     255         261 :             tmpp = 2.5f;
     256             :         }
     257             : 
     258        9880 :         hVAD_CLDFB->lt_noise_sp_center_diff_sum += tmpp;
     259        9880 :         hVAD_CLDFB->lt_noise_sp_center_diff_counter++;
     260             : 
     261        9880 :         if ( hVAD_CLDFB->lt_noise_sp_center_diff_counter == 128 )
     262             :         {
     263         243 :             hVAD_CLDFB->lt_noise_sp_center_diff_sum = hVAD_CLDFB->lt_noise_sp_center_diff_sum * 0.75f;
     264         243 :             hVAD_CLDFB->lt_noise_sp_center_diff_counter = 96;
     265             :         }
     266             : 
     267        9880 :         if ( fabs( sp_center[0] - hVAD_CLDFB->lt_noise_sp_center0 ) > 2.4 )
     268             :         {
     269           4 :             hVAD_CLDFB->lt_noise_sp_center0 = 0.996f * hVAD_CLDFB->lt_noise_sp_center0 + 0.004f * sp_center[0];
     270             :         }
     271        9876 :         else if ( fabs( sp_center[0] - hVAD_CLDFB->lt_noise_sp_center0 ) > 1.0 )
     272             :         {
     273         245 :             hVAD_CLDFB->lt_noise_sp_center0 = 0.99f * hVAD_CLDFB->lt_noise_sp_center0 + 0.01f * sp_center[0];
     274             :         }
     275             :         else
     276             :         {
     277        9631 :             hVAD_CLDFB->lt_noise_sp_center0 = 0.96f * hVAD_CLDFB->lt_noise_sp_center0 + 0.04f * sp_center[0];
     278             :         }
     279             :     }
     280             : 
     281       96918 :     if ( ( fabs( sp_center[2] - hVAD_CLDFB->lt_noise_sp_center0 ) > ( 6 * ( hVAD_CLDFB->lt_noise_sp_center_diff_sum / hVAD_CLDFB->lt_noise_sp_center_diff_counter ) + 0.3 ) ) && hVAD_CLDFB->frameloop > 200 )
     282             :     {
     283       14189 :         update_flag = 0;
     284             :     }
     285             : 
     286       96918 :     return update_flag;
     287             : }

Generated by: LCOV version 1.14