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 3100 : 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 3100 : int16_t music_background_frame = 0; 57 3100 : float *sp_center = hVAD_CLDFB->sp_center; 58 3100 : float *ltd_stable_rate = hVAD_CLDFB->ltd_stable_rate; 59 3100 : float *sSFM = hVAD_CLDFB->sfm; 60 3100 : float *f_tonality_rate = hVAD_CLDFB->f_tonality_rate; 61 : 62 3100 : if ( ( f_tonality_rate[1] > 0.60 ) || ( f_tonality_rate[0] > 0.86 ) ) 63 : { 64 1851 : 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 7 : music_background_frame = 1; 67 : } 68 : } 69 : 70 3100 : 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 7 : hVAD_CLDFB->music_background_rate = hVAD_CLDFB->music_background_rate * 0.975f + 0.025f; 73 : } 74 3093 : else if ( music_background_frame ) 75 : { 76 0 : hVAD_CLDFB->music_background_rate = hVAD_CLDFB->music_background_rate * 0.998f + 0.002f; 77 : } 78 : else 79 : { 80 3093 : hVAD_CLDFB->music_background_rate = hVAD_CLDFB->music_background_rate * 0.997f; 81 : } 82 : 83 3100 : if ( hVAD_CLDFB->music_background_rate > 0.5 ) 84 : { 85 0 : *music_backgound_f = 1; 86 : } 87 : else 88 : { 89 3100 : *music_backgound_f = 0; 90 : } 91 : 92 3100 : return; 93 : } 94 : 95 : 96 : /*-------------------------------------------------------------------* 97 : * update_decision() 98 : * 99 : * 100 : *-------------------------------------------------------------------*/ 101 : 102 3100 : 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 3100 : float g_high_eng_sacle = 0.0f; 113 : float sp_center3_diff; 114 3100 : int16_t update_flag = 1; 115 3100 : int16_t tonality_flag = 0; 116 3100 : int16_t frameloop = hVAD_CLDFB->frameloop; 117 3100 : int16_t bw = hVAD_CLDFB->bw_index; 118 3100 : float *sp_center = hVAD_CLDFB->sp_center; 119 3100 : float *ltd_stable_rate = hVAD_CLDFB->ltd_stable_rate; 120 3100 : float *sSFM = hVAD_CLDFB->sfm; 121 3100 : float *f_tonality_rate = hVAD_CLDFB->f_tonality_rate; 122 3100 : float tmpout = frame_energy - 4 * hVAD_CLDFB->frame_energy_smooth; 123 : 124 3100 : g_high_eng_sacle = high_eng / ( hVAD_CLDFB->lt_bg_highf_eng + FLT_MIN ); 125 3100 : sp_center3_diff = sp_center[3] - hVAD_CLDFB->lt_noise_sp_center3; 126 : 127 3100 : if ( frameloop > 50 ) 128 : { 129 2947 : if ( ltd_stable_rate[0] > 0.12 ) 130 : { 131 2801 : update_flag = 0; 132 : } 133 : 134 2947 : if ( ( bw == CLDFBVAD_WB_ID || bw == CLDFBVAD_SWB_ID ) && hVAD_CLDFB->frame_energy_smooth < 4 * frame_energy ) 135 : { 136 1654 : if ( g_high_eng_sacle > 3.0f && ( sp_center3_diff > 0.4 ) ) 137 : { 138 434 : update_flag = 0; 139 : } 140 1654 : if ( ( sp_center[3] > 2.8f ) && ( ltd_stable_rate[0] > 0.02f ) ) 141 : { 142 375 : update_flag = 0; 143 : } 144 : } 145 : } 146 : 147 3100 : if ( ( f_tonality_rate[1] > 0.50 ) && ( ltd_stable_rate[0] > 0.1 ) ) 148 : { 149 2301 : update_flag = 0; 150 : } 151 3100 : if ( sSFM[1] < 0.92 && sSFM[0] < 0.92 && sSFM[2] < 0.92 ) 152 : { 153 1655 : update_flag = 0; 154 : } 155 3100 : if ( sSFM[0] < 0.80 || sSFM[1] < 0.78 || sSFM[2] < 0.80 ) 156 : { 157 2214 : update_flag = 0; 158 : } 159 : 160 3100 : if ( frame_energy > 32 * hVAD_CLDFB->frame_energy_smooth ) 161 : { 162 8 : update_flag = 0; 163 : } 164 3100 : 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 1588 : update_flag = 0; 167 : } 168 3100 : if ( ( f_tonality_rate[1] > 0.60 ) || ( f_tonality_rate[0] > 0.86 ) ) 169 : { 170 1851 : update_flag = 0; 171 1851 : tonality_flag = 1; 172 : } 173 : 174 3100 : if ( tonality_flag ) 175 : { 176 1851 : hVAD_CLDFB->tonality_rate3 = hVAD_CLDFB->tonality_rate3 * 0.983f + 0.017f; 177 : } 178 : else 179 : { 180 1249 : hVAD_CLDFB->tonality_rate3 = hVAD_CLDFB->tonality_rate3 * 0.983f; 181 : } 182 : 183 3100 : if ( hVAD_CLDFB->tonality_rate3 > 0.5 ) 184 : { 185 1722 : update_flag = 0; 186 : } 187 : 188 3100 : if ( ( sp_center[0] > 4.0f ) && ltd_stable_rate[0] > 0.04 ) 189 : { 190 168 : update_flag = 0; 191 : } 192 3100 : if ( ( f_tonality_rate[1] > 0.46 ) && ( ( sSFM[1] > 0.93 ) || ( ltd_stable_rate[0] > 0.09 ) ) ) 193 : { 194 2664 : update_flag = 0; 195 : } 196 3100 : if ( ( sSFM[1] < 0.93 && sSFM[0] < 0.92 && sSFM[2] < 0.97 ) && ( f_tonality_rate[1] > 0.5 ) ) 197 : { 198 1900 : update_flag = 0; 199 : } 200 3100 : 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 3100 : if ( update_flag ) 206 : { 207 18 : if ( hVAD_CLDFB->update_count < 1000 ) 208 : { 209 18 : hVAD_CLDFB->update_count = hVAD_CLDFB->update_count + 1; 210 : } 211 : } 212 : 213 3100 : if ( update_flag ) 214 : { 215 18 : hVAD_CLDFB->lt_noise_sp_center3 = 0.9f * hVAD_CLDFB->lt_noise_sp_center3 + 0.1f * sp_center[3]; 216 : } 217 3100 : if ( ( tmpout > 0 ) && ( frameloop < 100 ) && ( f_tonality_rate[1] < 0.56 ) && ( ( sp_center[0] < 1.36 ) || ltd_stable_rate[0] < 0.03 ) ) 218 : { 219 0 : update_flag = 1; 220 : } 221 3100 : 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 18 : update_flag = 1; 225 : } 226 3100 : if ( vad_flag && ( snr > 1.0 ) && bw == CLDFBVAD_SWB_ID && tmpout > 0 ) 227 : { 228 299 : update_flag = 0; 229 : } 230 : 231 3100 : if ( vad_flag && ( snr > 1.5 ) && bw != CLDFBVAD_SWB_ID && tmpout > 0 ) 232 : { 233 0 : update_flag = 0; 234 : } 235 : 236 3100 : if ( update_flag == 0 ) 237 : { 238 3070 : hVAD_CLDFB->update_num_with_snr = 0; 239 : } 240 : else 241 : { 242 30 : if ( vad_flag && ( snr > 3.0 ) && hVAD_CLDFB->update_num_with_snr < 10 ) 243 : { 244 0 : update_flag = 0; 245 0 : hVAD_CLDFB->update_num_with_snr++; 246 : } 247 : } 248 : 249 3100 : if ( vad_flag == 0 || update_flag == 1 ) 250 : { 251 88 : float tmpp = (float) fabs( hVAD_CLDFB->sp_center[2] - hVAD_CLDFB->lt_noise_sp_center0 ); 252 : 253 88 : if ( tmpp > 2.5 ) 254 : { 255 0 : tmpp = 2.5f; 256 : } 257 : 258 88 : hVAD_CLDFB->lt_noise_sp_center_diff_sum += tmpp; 259 88 : hVAD_CLDFB->lt_noise_sp_center_diff_counter++; 260 : 261 88 : if ( hVAD_CLDFB->lt_noise_sp_center_diff_counter == 128 ) 262 : { 263 0 : hVAD_CLDFB->lt_noise_sp_center_diff_sum = hVAD_CLDFB->lt_noise_sp_center_diff_sum * 0.75f; 264 0 : hVAD_CLDFB->lt_noise_sp_center_diff_counter = 96; 265 : } 266 : 267 88 : if ( fabs( sp_center[0] - hVAD_CLDFB->lt_noise_sp_center0 ) > 2.4 ) 268 : { 269 0 : hVAD_CLDFB->lt_noise_sp_center0 = 0.996f * hVAD_CLDFB->lt_noise_sp_center0 + 0.004f * sp_center[0]; 270 : } 271 88 : else if ( fabs( sp_center[0] - hVAD_CLDFB->lt_noise_sp_center0 ) > 1.0 ) 272 : { 273 0 : hVAD_CLDFB->lt_noise_sp_center0 = 0.99f * hVAD_CLDFB->lt_noise_sp_center0 + 0.01f * sp_center[0]; 274 : } 275 : else 276 : { 277 88 : hVAD_CLDFB->lt_noise_sp_center0 = 0.96f * hVAD_CLDFB->lt_noise_sp_center0 + 0.04f * sp_center[0]; 278 : } 279 : } 280 : 281 3100 : 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 128 : update_flag = 0; 284 : } 285 : 286 3100 : return update_flag; 287 : }