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 "prot.h"
45 : #include "rom_com.h"
46 : #include "wmc_auto.h"
47 :
48 : /*-------------------------------------------------------------------*
49 : * quantize_uvg()
50 : *
51 : * Quantize unvoiced gains
52 : *--------------------------------------------------------------------*/
53 :
54 0 : static void quantize_uvg(
55 : float *G, /* i : unvoiced gain */
56 : int16_t *iG1, /* i : gain 1 index */
57 : int16_t *iG2, /* i : gain 2 index */
58 : float *quantG, /* o : quantized gain */
59 : const int16_t bwidth )
60 : {
61 : float G1[2], G2[10];
62 : int16_t i, j, k;
63 : float mse, mmse;
64 0 : const float( *UVG1CB )[2] = NULL;
65 0 : const float( *UVG2CB1 )[5] = NULL;
66 0 : const float( *UVG2CB2 )[5] = NULL;
67 :
68 0 : if ( bwidth == NB )
69 : {
70 0 : UVG1CB = UVG1CB_NB;
71 0 : UVG2CB1 = UVG2CB1_NB;
72 0 : UVG2CB2 = UVG2CB2_NB;
73 : }
74 0 : else if ( bwidth == WB || bwidth == SWB )
75 : {
76 0 : UVG1CB = UVG1CB_WB;
77 0 : UVG2CB1 = UVG2CB1_WB;
78 0 : UVG2CB2 = UVG2CB2_WB;
79 : }
80 :
81 0 : for ( i = 0; i < 2; i++ )
82 : {
83 0 : G1[i] = 0;
84 0 : for ( j = 0; j < 5; j++ )
85 : {
86 0 : G1[i] += SQR( G[i * 5 + j] );
87 : }
88 0 : G1[i] = (float) log10( sqrt( G1[i] / 5 ) );
89 : }
90 :
91 0 : mmse = (float) 1e30;
92 0 : *iG1 = 0;
93 :
94 0 : for ( i = 0; i < UVG1_CBSIZE; i++ )
95 : {
96 0 : mse = SQR( G1[0] - ( UVG1CB[i][0] ) ) + SQR( G1[1] - ( UVG1CB[i][1] ) );
97 0 : if ( mse < mmse )
98 : {
99 0 : *iG1 = i;
100 0 : mmse = mse;
101 : }
102 : }
103 :
104 0 : G1[0] = (float) pow( 10.0, ( UVG1CB[*iG1][0] ) );
105 0 : G1[1] = (float) pow( 10.0, ( UVG1CB[*iG1][1] ) );
106 :
107 0 : for ( i = 0; i < 2; i++ )
108 : {
109 0 : for ( j = 0; j < 5; j++ )
110 : {
111 0 : G2[i * 5 + j] = G[i * 5 + j] / G1[i];
112 : }
113 : }
114 :
115 0 : for ( i = 0; i < 2; i++ )
116 : {
117 0 : mmse = (float) 1e30;
118 0 : iG2[i] = 0;
119 0 : for ( j = 0; j < UVG2_CBSIZE; j++ )
120 : {
121 0 : mse = 0;
122 0 : for ( k = 0; k < 5; k++ )
123 : {
124 0 : if ( i == 0 )
125 : {
126 0 : mse += SQR( G2[i * 5 + k] - UVG2CB1[j][k] );
127 : }
128 0 : else if ( i == 1 )
129 : {
130 0 : mse += SQR( G2[i * 5 + k] - UVG2CB2[j][k] );
131 : }
132 : }
133 :
134 0 : if ( mse < mmse )
135 : {
136 0 : mmse = mse;
137 0 : iG2[i] = j;
138 : }
139 : }
140 : }
141 :
142 0 : mvr2r( G, G2, 10 );
143 :
144 0 : dequantize_uvg( *iG1, iG2, quantG, bwidth );
145 :
146 0 : return;
147 : }
148 :
149 :
150 : /*-------------------------------------------------------------------*
151 : * nelp_encoder()
152 : *
153 : * NELP encoder
154 : *--------------------------------------------------------------------*/
155 :
156 0 : void nelp_encoder(
157 : Encoder_State *st, /* i/o: encoder state */
158 : float *in, /* i : residual signal */
159 : float *exc, /* o : NELP quantized excitation signal */
160 : const int16_t reduce_gains )
161 : {
162 : int16_t i, j;
163 0 : float *ptr = exc;
164 0 : int16_t lag = 25; /* to cover 25*9 + 31 */
165 : float Gains[10], gain_fac;
166 : int16_t iG1, iG2[2], fid;
167 : float fdbck, var_dB, tmp;
168 0 : float E1 = 0, E2, E3, R, EL1 = 0, EH1 = 0, EL2, EH2, RL, RH;
169 : float filtRes[L_FRAME];
170 : float ptr_tmp[L_FRAME];
171 : int16_t rf_flag;
172 :
173 0 : SC_VBR_ENC_HANDLE hSC_VBR = st->hSC_VBR;
174 0 : BSTR_ENC_HANDLE hBstr = st->hBstr;
175 :
176 0 : rf_flag = st->rf_mode;
177 :
178 0 : if ( ( hSC_VBR->last_nelp_mode == 1 ) && ( st->bwidth != st->last_bwidth ) )
179 : {
180 0 : hSC_VBR->last_nelp_mode = 0;
181 : }
182 :
183 0 : if ( st->bwidth == NB )
184 : {
185 0 : if ( hSC_VBR->last_nelp_mode != 1 )
186 : {
187 0 : set_f( hSC_VBR->bp1_filt_mem_nb, 0, 7 * 2 );
188 : }
189 : }
190 0 : else if ( st->bwidth == WB || st->bwidth == SWB )
191 : {
192 0 : if ( hSC_VBR->last_nelp_mode != 1 )
193 : {
194 0 : set_f( hSC_VBR->bp1_filt_mem_wb, 0, 4 * 2 );
195 : }
196 : }
197 :
198 0 : if ( hSC_VBR->last_nelp_mode != 1 )
199 : {
200 0 : if ( st->bwidth == WB || st->bwidth == SWB )
201 : {
202 0 : set_f( hSC_VBR->shape1_filt_mem, 0, 20 );
203 0 : set_f( hSC_VBR->shape2_filt_mem, 0, 20 );
204 0 : set_f( hSC_VBR->shape3_filt_mem, 0, 20 );
205 0 : set_f( hSC_VBR->txlpf1_filt1_mem, 0, 20 );
206 0 : set_f( hSC_VBR->txlpf1_filt2_mem, 0, 20 );
207 0 : set_f( hSC_VBR->txhpf1_filt1_mem, 0, 20 );
208 0 : set_f( hSC_VBR->txhpf1_filt2_mem, 0, 20 );
209 : }
210 : }
211 :
212 : /* Start Unvoiced/NELP Processing */
213 0 : if ( st->bwidth == WB || st->bwidth == SWB )
214 : {
215 0 : for ( i = 0, E1 = 0.001f; i < L_FRAME; i++ )
216 : {
217 0 : E1 += SQR( in[i] );
218 : }
219 :
220 0 : polezero_filter( in, filtRes, L_FRAME, txlpf1_num_coef, txlpf1_den_coef, 10, hSC_VBR->txlpf1_filt1_mem );
221 :
222 0 : for ( i = 0, EL1 = 0.001f; i < L_FRAME; i++ )
223 : {
224 0 : EL1 += SQR( filtRes[i] );
225 : }
226 :
227 0 : polezero_filter( in, filtRes, L_FRAME, txhpf1_num_coef, txhpf1_den_coef, 10, hSC_VBR->txhpf1_filt1_mem );
228 :
229 0 : for ( i = 0, EH1 = 0.001f; i < L_FRAME; i++ )
230 : {
231 0 : EH1 += SQR( filtRes[i] );
232 : }
233 : }
234 :
235 0 : for ( i = 0; i < 9; i++ )
236 : {
237 0 : for ( j = i * lag, Gains[i] = 0.001f; j < ( i + 1 ) * lag; j++ )
238 : {
239 0 : Gains[i] += SQR( in[j] );
240 : }
241 :
242 0 : Gains[i] = (float) sqrt( Gains[i] / lag );
243 : }
244 :
245 0 : for ( j = i * lag, Gains[i] = 0.001f; j < L_FRAME; j++ )
246 : {
247 0 : Gains[i] += SQR( in[j] );
248 : }
249 :
250 0 : Gains[i] = (float) sqrt( Gains[i] / ( L_FRAME - ( lag * i ) ) );
251 :
252 0 : if ( reduce_gains == 1 )
253 : {
254 0 : for ( i = 0; i < 10; i++ )
255 : {
256 0 : Gains[i] = ( Gains[i] * 0.6f );
257 : }
258 : }
259 :
260 0 : if ( hSC_VBR->last_nelp_mode != 1 ) /* if prev frame was not NELP then init mem*/
261 : {
262 0 : hSC_VBR->nelp_gain_mem = Gains[0];
263 : }
264 :
265 0 : tmp = (float) ( 20.0 * ( log10( Gains[0] ) - log10( hSC_VBR->nelp_gain_mem ) ) );
266 0 : var_dB = tmp * tmp;
267 0 : for ( i = 1; i < 10; i++ )
268 : {
269 0 : tmp = (float) ( 20.0 * ( log10( Gains[i] ) - log10( Gains[i - 1] ) ) );
270 0 : var_dB += tmp * tmp;
271 : }
272 :
273 0 : if ( hSC_VBR->last_nelp_mode != 1 )
274 : {
275 0 : var_dB *= 0.111f;
276 : }
277 : else
278 : {
279 0 : var_dB *= 0.1f;
280 : }
281 :
282 0 : fdbck = (float) ( 0.82f / ( 1.0f + exp( 0.25f * ( var_dB - 20.0f ) ) ) );
283 :
284 0 : for ( i = 0; i < 10; i++ )
285 : {
286 0 : Gains[i] = (float) ( ( 1.0f - fdbck ) * Gains[i] + fdbck * hSC_VBR->nelp_gain_mem );
287 0 : hSC_VBR->nelp_gain_mem = Gains[i];
288 : }
289 :
290 0 : quantize_uvg( Gains, &iG1, iG2, Gains, (int16_t) st->bwidth );
291 :
292 0 : if ( rf_flag )
293 : {
294 0 : st->hRF->rf_indx_nelp_iG1[0] = (int16_t) iG1;
295 0 : st->hRF->rf_indx_nelp_iG2[0][0] = (int16_t) iG2[0];
296 0 : st->hRF->rf_indx_nelp_iG2[0][1] = (int16_t) iG2[1];
297 : }
298 : else
299 : {
300 0 : push_indice( hBstr, IND_IG1, iG1, 5 );
301 0 : push_indice( hBstr, IND_IG2A, iG2[0], 6 );
302 0 : push_indice( hBstr, IND_IG2B, iG2[1], 6 );
303 : }
304 :
305 0 : if ( st->bwidth == WB || st->bwidth == SWB )
306 : {
307 0 : gain_fac = 1.16f;
308 : }
309 : else
310 : {
311 0 : gain_fac = 1.37f;
312 : }
313 :
314 0 : generate_nelp_excitation( &( hSC_VBR->nelp_enc_seed ), Gains, ptr, gain_fac );
315 :
316 0 : if ( st->bwidth == WB || st->bwidth == SWB )
317 : {
318 0 : polezero_filter( ptr, ptr_tmp, L_FRAME, bp1_num_coef_wb, bp1_den_coef_wb, 4, hSC_VBR->bp1_filt_mem_wb );
319 0 : mvr2r( ptr_tmp, ptr, L_FRAME );
320 : }
321 0 : else if ( st->bwidth == NB )
322 : {
323 0 : polezero_filter( ptr, ptr_tmp, L_FRAME, bp1_num_coef_nb_fx_order7, bp1_den_coef_nb_fx_order7, 7, hSC_VBR->bp1_filt_mem_nb );
324 0 : mvr2r( ptr_tmp, ptr, L_FRAME );
325 : }
326 :
327 0 : for ( i = 0, E3 = 0.001f; i < L_FRAME; i++ )
328 : {
329 0 : E3 += SQR( ptr[i] );
330 : }
331 :
332 0 : if ( st->bwidth == WB || st->bwidth == SWB )
333 : {
334 0 : polezero_filter( ptr, ptr_tmp, L_FRAME, shape1_num_coef, shape1_den_coef, 10, hSC_VBR->shape1_filt_mem );
335 0 : mvr2r( ptr_tmp, ptr, L_FRAME );
336 :
337 0 : for ( i = 0, E2 = 0.001f; i < L_FRAME; i++ )
338 : {
339 0 : E2 += SQR( ptr[i] );
340 : }
341 :
342 0 : R = (float) sqrt( E1 / E2 );
343 :
344 0 : for ( i = 0; i < L_FRAME; i++ )
345 : {
346 0 : filtRes[i] = R * ptr[i];
347 : }
348 :
349 0 : polezero_filter( filtRes, ptr_tmp, L_FRAME, txlpf1_num_coef, txlpf1_den_coef, 10, hSC_VBR->txlpf1_filt2_mem );
350 0 : mvr2r( ptr_tmp, filtRes, L_FRAME );
351 :
352 0 : for ( i = 0, EL2 = 0.001f; i < L_FRAME; i++ )
353 : {
354 0 : EL2 += SQR( filtRes[i] );
355 : }
356 :
357 0 : for ( i = 0; i < L_FRAME; i++ )
358 : {
359 0 : filtRes[i] = R * ptr[i];
360 : }
361 :
362 0 : polezero_filter( filtRes, ptr_tmp, L_FRAME, txhpf1_num_coef, txhpf1_den_coef, 10, hSC_VBR->txhpf1_filt2_mem );
363 0 : mvr2r( ptr_tmp, filtRes, L_FRAME );
364 :
365 0 : for ( i = 0, EH2 = 0.001f; i < L_FRAME; i++ )
366 : {
367 0 : EH2 += SQR( filtRes[i] );
368 : }
369 :
370 0 : RL = (float) 10.0f * (float) log10( EL1 / EL2 );
371 0 : RH = (float) 10.0f * (float) log10( EH1 / EH2 );
372 :
373 0 : fid = 0;
374 0 : if ( RL < -3 )
375 : {
376 0 : fid = 1;
377 : }
378 0 : else if ( RH < -3 )
379 : {
380 0 : fid = 2;
381 : }
382 :
383 0 : if ( rf_flag == 0 )
384 : {
385 :
386 0 : switch ( fid )
387 : {
388 0 : case 1:
389 : /* Update other filter memory */
390 0 : polezero_filter( ptr, filtRes, L_FRAME, shape3_num_coef, shape3_den_coef, 10, hSC_VBR->shape3_filt_mem );
391 :
392 : /* filter the residual to desired shape */
393 0 : polezero_filter( ptr, ptr_tmp, L_FRAME, shape2_num_coef, shape2_den_coef, 10, hSC_VBR->shape2_filt_mem );
394 :
395 0 : mvr2r( ptr_tmp, ptr, L_FRAME );
396 :
397 0 : break;
398 0 : case 2:
399 : /* Update other filter memory */
400 0 : polezero_filter( ptr, filtRes, L_FRAME, shape2_num_coef, shape2_den_coef, 10, hSC_VBR->shape2_filt_mem );
401 :
402 : /* filter the residual to desired shape */
403 0 : polezero_filter( ptr, ptr_tmp, L_FRAME, shape3_num_coef, shape3_den_coef, 10, hSC_VBR->shape3_filt_mem );
404 :
405 0 : mvr2r( ptr_tmp, ptr, L_FRAME );
406 :
407 0 : break;
408 0 : default:
409 : /* Update other filter memory */
410 0 : polezero_filter( ptr, filtRes, L_FRAME, shape2_num_coef, shape2_den_coef, 10, hSC_VBR->shape2_filt_mem );
411 0 : polezero_filter( ptr, filtRes, L_FRAME, shape3_num_coef, shape3_den_coef, 10, hSC_VBR->shape3_filt_mem );
412 :
413 0 : break;
414 : }
415 :
416 0 : for ( i = 0, E2 = 0.001f; i < L_FRAME; i++ )
417 : {
418 0 : E2 += SQR( ptr[i] );
419 : }
420 :
421 0 : R = (float) sqrt( E3 / E2 );
422 0 : for ( i = 0; i < L_FRAME; i++ )
423 : {
424 0 : ptr[i] *= R;
425 : }
426 : }
427 :
428 0 : if ( rf_flag )
429 : {
430 0 : st->hRF->rf_indx_nelp_fid[0] = (int16_t) fid;
431 : }
432 : else
433 : {
434 0 : push_indice( hBstr, IND_NELP_FID, fid, 2 );
435 : }
436 : }
437 :
438 0 : if ( rf_flag == 0 )
439 : {
440 0 : for ( i = 0; i < L_FRAME; i++ )
441 : {
442 0 : exc[i] = ptr[i];
443 : }
444 : }
445 :
446 0 : return;
447 : }
|