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 96918 : 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 96918 : float *sb_bg_energy = hVAD_CLDFB->sb_bg_energy;
59 96918 : float *frame_sb_energy = hVAD_CLDFB->frame_sb_energy;
60 96918 : float *f_tonality_rate = hVAD_CLDFB->f_tonality_rate;
61 96918 : float *ltd_stable_rate = hVAD_CLDFB->ltd_stable_rate;
62 96918 : int16_t frameloop = hVAD_CLDFB->frameloop;
63 96918 : float t_bg_energy = hVAD_CLDFB->t_bg_energy;
64 :
65 96918 : SNR_sb_num = ENERGY_BAND_NUM[hVAD_CLDFB->bw_index - CLDFBVAD_NB_ID];
66 :
67 96918 : frame_energy = frame_energy + 0.0001f;
68 :
69 96918 : if ( ( frameloop < 60 ) && ( frameloop > 5 ) && ( f_tonality_rate[0] < 0.56 ) &&
70 5209 : ( f_tonality_rate[1] < 0.5 ) && ltd_stable_rate[1] < 0.06 && snr < 2.5f )
71 : {
72 978 : if ( frameloop < 50 )
73 : {
74 843 : hVAD_CLDFB->t_bg_energy_sum += frame_energy / 10;
75 843 : hVAD_CLDFB->tbg_energy_count++;
76 : }
77 : else
78 : {
79 135 : hVAD_CLDFB->t_bg_energy_sum += frame_energy;
80 135 : hVAD_CLDFB->tbg_energy_count++;
81 : }
82 :
83 11246 : for ( i = 0; i < SNR_sb_num; i++ )
84 : {
85 10268 : sb_bg_energy[i] = sb_bg_energy[i] * 0.9f + frame_sb_energy[i] * 0.01f;
86 : }
87 : }
88 :
89 96918 : if ( update_flag == 1 && frameloop > 2 && music_backgound_f == 0 )
90 : {
91 5434 : if ( hVAD_CLDFB->bg_update_count < 16 )
92 : {
93 582 : if ( frameloop < 50 )
94 : {
95 582 : hVAD_CLDFB->t_bg_energy_sum += frame_energy / 10;
96 582 : 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 6846 : for ( i = 0; i < SNR_sb_num; i++ )
104 : {
105 6264 : sb_bg_energy[i] = sb_bg_energy[i] * 0.96f + frame_sb_energy[i] * 0.04f;
106 : }
107 582 : hVAD_CLDFB->bg_update_count++;
108 : }
109 : else
110 : {
111 4852 : float a = 0.94f;
112 :
113 4852 : 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 4852 : else if ( 12 * t_bg_energy < frame_energy )
121 : {
122 153 : if ( frameloop < 50 )
123 : {
124 104 : hVAD_CLDFB->t_bg_energy_sum += frame_energy / 10;
125 104 : hVAD_CLDFB->tbg_energy_count++;
126 : }
127 : else
128 : {
129 49 : hVAD_CLDFB->t_bg_energy_sum += frame_energy;
130 49 : hVAD_CLDFB->tbg_energy_count++;
131 : }
132 :
133 1783 : for ( i = 0; i < SNR_sb_num; i++ )
134 : {
135 1630 : sb_bg_energy[i] = sb_bg_energy[i] * 0.96f + frame_sb_energy[i] * 0.04f;
136 : }
137 : }
138 : else
139 : {
140 4699 : if ( t_bg_energy > frame_energy )
141 : {
142 1851 : a = 0.95f;
143 1851 : 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 1851 : hVAD_CLDFB->t_bg_energy_sum += frame_energy;
151 1851 : hVAD_CLDFB->tbg_energy_count++;
152 : }
153 :
154 22375 : for ( i = 0; i < SNR_sb_num; i++ )
155 : {
156 20524 : sb_bg_energy[i] = sb_bg_energy[i] * a + frame_sb_energy[i] * ( 1 - a );
157 : }
158 : }
159 : else
160 : {
161 2848 : a = 0.96f;
162 2848 : if ( frameloop < 50 )
163 : {
164 293 : hVAD_CLDFB->t_bg_energy_sum += frame_energy / 10;
165 293 : hVAD_CLDFB->tbg_energy_count++;
166 : }
167 : else
168 : {
169 2555 : hVAD_CLDFB->t_bg_energy_sum += frame_energy;
170 2555 : hVAD_CLDFB->tbg_energy_count++;
171 : }
172 :
173 32792 : for ( i = 0; i < SNR_sb_num; i++ )
174 : {
175 29944 : sb_bg_energy[i] = sb_bg_energy[i] * a + frame_sb_energy[i] * ( 1 - a );
176 : }
177 : }
178 : }
179 : }
180 : }
181 : else
182 : {
183 91484 : if ( ( t_bg_energy > 500 * frame_energy ) && ( sb_bg_energy[0] > 10 * frame_sb_energy[0] ) )
184 : {
185 11401 : for ( i = 0; i < SNR_sb_num; i++ )
186 : {
187 10460 : sb_bg_energy[i] = sb_bg_energy[i] * 0.9f + frame_sb_energy[i] * 0.1f;
188 : }
189 : }
190 90543 : else if ( t_bg_energy > 10 * frame_energy )
191 : {
192 34635 : for ( i = 0; i < SNR_sb_num; i++ )
193 : {
194 31960 : sb_bg_energy[i] = sb_bg_energy[i] * 0.999f + frame_sb_energy[i] * 0.001f;
195 : }
196 : }
197 : }
198 :
199 96918 : 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 96918 : if ( music_backgound_f == 1 && hVAD_CLDFB->lt_snr_org < 3.2 && t_bg_energy > 1 && update_flag == 0 )
205 : {
206 5226 : for ( i = 0; i < SNR_sb_num; i++ )
207 : {
208 4824 : sb_bg_energy[i] = sb_bg_energy[i] * 0.98f + 0.000001f;
209 : }
210 : }
211 96918 : if ( music_backgound_f == 1 && frame_energy < 5000 * t_bg_energy )
212 : {
213 31954 : for ( i = 0; i < SNR_sb_num; i++ )
214 : {
215 29496 : sb_bg_energy[i] = sb_bg_energy[i] * 0.98f + 0.000001f;
216 : }
217 : }
218 :
219 96918 : if ( hVAD_CLDFB->tbg_energy_count == 64 )
220 : {
221 107 : hVAD_CLDFB->tbg_energy_count = 48;
222 107 : hVAD_CLDFB->t_bg_energy_sum = hVAD_CLDFB->t_bg_energy_sum * 0.75f;
223 : }
224 :
225 96918 : hVAD_CLDFB->t_bg_energy = hVAD_CLDFB->t_bg_energy_sum / hVAD_CLDFB->tbg_energy_count;
226 :
227 96918 : return;
228 : }
229 :
230 :
231 : /*-------------------------------------------------------------------*
232 : * est_energy()
233 : *
234 : *
235 : *-------------------------------------------------------------------*/
236 :
237 96918 : 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 96918 : int16_t band_num = BAND_NUM_TAB[bw];
249 96918 : const float sb_power_scale[5] = { 0.0f, 0.16f, 0.24f, 0.28f, 0.28f };
250 : const int16_t *Nregion_index;
251 :
252 96918 : frame_energy2 = 0.0f;
253 96918 : high_energy = 0.0f;
254 3306538 : for ( i = 0; i < band_num; i++ )
255 : {
256 3209620 : if ( i > 0 && ( i != band_num - 1 ) )
257 : {
258 3015784 : frame_energy2 += sb_power[i];
259 : }
260 3209620 : if ( i > 5 )
261 : {
262 2628112 : high_energy += sb_power[i];
263 : }
264 : }
265 :
266 96918 : high_energy /= ( PCM16_TO_FLT_FAC * PCM16_TO_FLT_FAC );
267 96918 : frame_energy2 /= ( PCM16_TO_FLT_FAC * PCM16_TO_FLT_FAC );
268 :
269 96918 : Nregion_index = REGION_INDEX[bw - CLDFBVAD_NB_ID];
270 96918 : SNR_sb_num = ENERGY_BAND_NUM[bw - CLDFBVAD_NB_ID];
271 :
272 1185514 : for ( i = 0; i < SNR_sb_num; i++ )
273 : {
274 1088596 : frame_sb_energy[i] = 0;
275 3933674 : for ( j = Nregion_index[i]; j < Nregion_index[i + 1]; j++ )
276 : {
277 2845078 : frame_sb_energy[i] += sb_power[j];
278 : }
279 :
280 1088596 : frame_sb_energy[i] /= ( PCM16_TO_FLT_FAC * PCM16_TO_FLT_FAC );
281 : }
282 96918 : *p_high_energy = high_energy;
283 96918 : *p_frame_energy2 = frame_energy2;
284 96918 : *p_frame_energy = frame_energy2 + ( sb_power_scale[bw] * sb_power[0] / ( PCM16_TO_FLT_FAC * PCM16_TO_FLT_FAC ) );
285 :
286 96918 : return;
287 : }
|