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 "prot.h"
40 : #include "cnst.h"
41 : #include "rom_enc.h"
42 : #include "wmc_auto.h"
43 :
44 : /*-------------------------------------------------------------------*
45 : * background_update()
46 : *
47 : *
48 : *-------------------------------------------------------------------*/
49 :
50 3100 : void background_update(
51 : VAD_CLDFB_HANDLE hVAD_CLDFB, /* i/o: CLDFB VAD state */
52 : float frame_energy, /* i : current frame energy */
53 : const int16_t update_flag, /* i : current frame update flag */
54 : const int16_t music_backgound_f, /* i : background music flag */
55 : const float snr )
56 : {
57 : int16_t i, SNR_sb_num;
58 3100 : float *sb_bg_energy = hVAD_CLDFB->sb_bg_energy;
59 3100 : float *frame_sb_energy = hVAD_CLDFB->frame_sb_energy;
60 3100 : float *f_tonality_rate = hVAD_CLDFB->f_tonality_rate;
61 3100 : float *ltd_stable_rate = hVAD_CLDFB->ltd_stable_rate;
62 3100 : int16_t frameloop = hVAD_CLDFB->frameloop;
63 3100 : float t_bg_energy = hVAD_CLDFB->t_bg_energy;
64 :
65 3100 : SNR_sb_num = ENERGY_BAND_NUM[hVAD_CLDFB->bw_index - CLDFBVAD_NB_ID];
66 :
67 3100 : frame_energy = frame_energy + 0.0001f;
68 :
69 3100 : if ( ( frameloop < 60 ) && ( frameloop > 5 ) && ( f_tonality_rate[0] < 0.56 ) &&
70 70 : ( f_tonality_rate[1] < 0.5 ) && ltd_stable_rate[1] < 0.06 && snr < 2.5f )
71 : {
72 14 : if ( frameloop < 50 )
73 : {
74 14 : hVAD_CLDFB->t_bg_energy_sum += frame_energy / 10;
75 14 : hVAD_CLDFB->tbg_energy_count++;
76 : }
77 : else
78 : {
79 0 : hVAD_CLDFB->t_bg_energy_sum += frame_energy;
80 0 : hVAD_CLDFB->tbg_energy_count++;
81 : }
82 :
83 182 : for ( i = 0; i < SNR_sb_num; i++ )
84 : {
85 168 : sb_bg_energy[i] = sb_bg_energy[i] * 0.9f + frame_sb_energy[i] * 0.01f;
86 : }
87 : }
88 :
89 3100 : if ( update_flag == 1 && frameloop > 2 && music_backgound_f == 0 )
90 : {
91 24 : if ( hVAD_CLDFB->bg_update_count < 16 )
92 : {
93 24 : if ( frameloop < 50 )
94 : {
95 24 : hVAD_CLDFB->t_bg_energy_sum += frame_energy / 10;
96 24 : hVAD_CLDFB->tbg_energy_count++;
97 : }
98 : else
99 : {
100 0 : hVAD_CLDFB->t_bg_energy_sum += frame_energy;
101 0 : hVAD_CLDFB->tbg_energy_count++;
102 : }
103 312 : for ( i = 0; i < SNR_sb_num; i++ )
104 : {
105 288 : sb_bg_energy[i] = sb_bg_energy[i] * 0.96f + frame_sb_energy[i] * 0.04f;
106 : }
107 24 : hVAD_CLDFB->bg_update_count++;
108 : }
109 : else
110 : {
111 0 : float a = 0.94f;
112 :
113 0 : if ( ( t_bg_energy < frame_energy ) && 24 * hVAD_CLDFB->frame_energy_smooth < frame_energy )
114 : {
115 0 : for ( i = 0; i < SNR_sb_num; i++ )
116 : {
117 0 : sb_bg_energy[i] = (float) ( sb_bg_energy[i] * 0.999f + frame_sb_energy[i] * 0.001 );
118 : }
119 : }
120 0 : else if ( 12 * t_bg_energy < frame_energy )
121 : {
122 0 : if ( frameloop < 50 )
123 : {
124 0 : hVAD_CLDFB->t_bg_energy_sum += frame_energy / 10;
125 0 : hVAD_CLDFB->tbg_energy_count++;
126 : }
127 : else
128 : {
129 0 : hVAD_CLDFB->t_bg_energy_sum += frame_energy;
130 0 : hVAD_CLDFB->tbg_energy_count++;
131 : }
132 :
133 0 : for ( i = 0; i < SNR_sb_num; i++ )
134 : {
135 0 : sb_bg_energy[i] = sb_bg_energy[i] * 0.96f + frame_sb_energy[i] * 0.04f;
136 : }
137 : }
138 : else
139 : {
140 0 : if ( t_bg_energy > frame_energy )
141 : {
142 0 : a = 0.95f;
143 0 : if ( frameloop < 50 )
144 : {
145 0 : hVAD_CLDFB->t_bg_energy_sum += frame_energy / 10;
146 0 : hVAD_CLDFB->tbg_energy_count++;
147 : }
148 : else
149 : {
150 0 : hVAD_CLDFB->t_bg_energy_sum += frame_energy;
151 0 : hVAD_CLDFB->tbg_energy_count++;
152 : }
153 :
154 0 : for ( i = 0; i < SNR_sb_num; i++ )
155 : {
156 0 : sb_bg_energy[i] = sb_bg_energy[i] * a + frame_sb_energy[i] * ( 1 - a );
157 : }
158 : }
159 : else
160 : {
161 0 : a = 0.96f;
162 0 : if ( frameloop < 50 )
163 : {
164 0 : hVAD_CLDFB->t_bg_energy_sum += frame_energy / 10;
165 0 : hVAD_CLDFB->tbg_energy_count++;
166 : }
167 : else
168 : {
169 0 : hVAD_CLDFB->t_bg_energy_sum += frame_energy;
170 0 : hVAD_CLDFB->tbg_energy_count++;
171 : }
172 :
173 0 : for ( i = 0; i < SNR_sb_num; i++ )
174 : {
175 0 : sb_bg_energy[i] = sb_bg_energy[i] * a + frame_sb_energy[i] * ( 1 - a );
176 : }
177 : }
178 : }
179 : }
180 : }
181 : else
182 : {
183 3076 : if ( ( t_bg_energy > 500 * frame_energy ) && ( sb_bg_energy[0] > 10 * frame_sb_energy[0] ) )
184 : {
185 0 : for ( i = 0; i < SNR_sb_num; i++ )
186 : {
187 0 : sb_bg_energy[i] = sb_bg_energy[i] * 0.9f + frame_sb_energy[i] * 0.1f;
188 : }
189 : }
190 3076 : else if ( t_bg_energy > 10 * frame_energy )
191 : {
192 156 : for ( i = 0; i < SNR_sb_num; i++ )
193 : {
194 144 : sb_bg_energy[i] = sb_bg_energy[i] * 0.999f + frame_sb_energy[i] * 0.001f;
195 : }
196 : }
197 : }
198 :
199 3100 : if ( hVAD_CLDFB->t_bg_energy_sum > 160 * hVAD_CLDFB->tbg_energy_count )
200 : {
201 0 : hVAD_CLDFB->t_bg_energy_sum = 160.0f * hVAD_CLDFB->tbg_energy_count;
202 : }
203 :
204 3100 : if ( music_backgound_f == 1 && hVAD_CLDFB->lt_snr_org < 3.2 && t_bg_energy > 1 && update_flag == 0 )
205 : {
206 0 : for ( i = 0; i < SNR_sb_num; i++ )
207 : {
208 0 : sb_bg_energy[i] = sb_bg_energy[i] * 0.98f + 0.000001f;
209 : }
210 : }
211 3100 : if ( music_backgound_f == 1 && frame_energy < 5000 * t_bg_energy )
212 : {
213 0 : for ( i = 0; i < SNR_sb_num; i++ )
214 : {
215 0 : sb_bg_energy[i] = sb_bg_energy[i] * 0.98f + 0.000001f;
216 : }
217 : }
218 :
219 3100 : if ( hVAD_CLDFB->tbg_energy_count == 64 )
220 : {
221 0 : hVAD_CLDFB->tbg_energy_count = 48;
222 0 : hVAD_CLDFB->t_bg_energy_sum = hVAD_CLDFB->t_bg_energy_sum * 0.75f;
223 : }
224 :
225 3100 : hVAD_CLDFB->t_bg_energy = hVAD_CLDFB->t_bg_energy_sum / hVAD_CLDFB->tbg_energy_count;
226 :
227 3100 : return;
228 : }
229 :
230 :
231 : /*-------------------------------------------------------------------*
232 : * est_energy()
233 : *
234 : *
235 : *-------------------------------------------------------------------*/
236 :
237 3100 : void est_energy(
238 : float sb_power[], /* o : energy of sub-band divided uniformly */
239 : float frame_sb_energy[], /* o : energy of sub-band divided non-uniformly */
240 : float *p_frame_energy, /* o : frame energy 1 */
241 : float *p_frame_energy2, /* o : frame energy 2 */
242 : float *p_high_energy, /* o : high frequency energy */
243 : const int16_t bw /* i : bandwidth */
244 : )
245 : {
246 : int16_t i, j, SNR_sb_num;
247 : float frame_energy2, high_energy;
248 3100 : int16_t band_num = BAND_NUM_TAB[bw];
249 3100 : const float sb_power_scale[5] = { 0.0f, 0.16f, 0.24f, 0.28f, 0.28f };
250 : const int16_t *Nregion_index;
251 :
252 3100 : frame_energy2 = 0.0f;
253 3100 : high_energy = 0.0f;
254 127100 : for ( i = 0; i < band_num; i++ )
255 : {
256 124000 : if ( i > 0 && ( i != band_num - 1 ) )
257 : {
258 117800 : frame_energy2 += sb_power[i];
259 : }
260 124000 : if ( i > 5 )
261 : {
262 105400 : high_energy += sb_power[i];
263 : }
264 : }
265 :
266 3100 : high_energy /= ( PCM16_TO_FLT_FAC * PCM16_TO_FLT_FAC );
267 3100 : frame_energy2 /= ( PCM16_TO_FLT_FAC * PCM16_TO_FLT_FAC );
268 :
269 3100 : Nregion_index = REGION_INDEX[bw - CLDFBVAD_NB_ID];
270 3100 : SNR_sb_num = ENERGY_BAND_NUM[bw - CLDFBVAD_NB_ID];
271 :
272 40300 : for ( i = 0; i < SNR_sb_num; i++ )
273 : {
274 37200 : frame_sb_energy[i] = 0;
275 148800 : for ( j = Nregion_index[i]; j < Nregion_index[i + 1]; j++ )
276 : {
277 111600 : frame_sb_energy[i] += sb_power[j];
278 : }
279 :
280 37200 : frame_sb_energy[i] /= ( PCM16_TO_FLT_FAC * PCM16_TO_FLT_FAC );
281 : }
282 3100 : *p_high_energy = high_energy;
283 3100 : *p_frame_energy2 = frame_energy2;
284 3100 : *p_frame_energy = frame_energy2 + ( sb_power_scale[bw] * sb_power[0] / ( PCM16_TO_FLT_FAC * PCM16_TO_FLT_FAC ) );
285 :
286 3100 : return;
287 : }
|