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.452 Aug 12, 2021. Version 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h" /* Compilation switches */
39 : #include "cnst.h" /* Common constants */
40 : #include "prot_fx.h" /* Function prototypes */
41 : #include "prot_fx_enc.h" /* Function prototypes */
42 : #include "basop_util.h"
43 :
44 : /*-------------------------------------------------------------------*
45 : * Local constants
46 : *-------------------------------------------------------------------*/
47 :
48 : #define DIST_ISF_MAX_IO 384 /* 150 Hz (6400Hz=16384) */
49 : #define DIST_ISF_MAX 307 /* 120 Hz (6400Hz=16384) */
50 : #define DIST_ISF_THRES 154 /* 60 (6400Hz=16384) */
51 : #define GAIN_PIT_THRES 14746 /* 0.9 in Q14 */
52 : #define GAIN_PIT_MIN 9830 /* 0.6 in Q14 */
53 :
54 : #define ALPHA1 32113 /* 0.98f */
55 : #define ALPHA4 32440 /* 0.99f */
56 : #define WINDOW_SIZE 50
57 : #define THRESH_TYPE 13926 /* 0.85f in Q14 */
58 : #define THRESH_VOICING 14090 /* 0.86f in Q14 */
59 :
60 : #define GPCLIP_E ( 6 + 2 )
61 :
62 : #define ALPHA1_M1 21474836l /*1.0f-0.98 Q30*/
63 : #define ALPHA4_M1 10737408l /*1.0f-0.99f Q30*/
64 :
65 : /*-------------------------------------------------------------------*
66 : * init_gp_clip
67 : *
68 : * Pitch Gain clipping initializations
69 : *-------------------------------------------------------------------*/
70 32357 : void init_gp_clip_fx(
71 : Word16 mem[] /* o : memory of gain of pitch clipping algorithm [2.56x,Q14,Q8,Q0,Q14,Q14]*/
72 : )
73 : {
74 32357 : mem[0] = DIST_ISF_MAX;
75 32357 : move16(); /* Q0 */
76 32357 : mem[1] = GAIN_PIT_MIN;
77 32357 : move16(); /* 1Q14 */
78 32357 : mem[2] = 0;
79 32357 : move16(); /* 8Q7 */ /* old energy of target (dB) */
80 32357 : mem[3] = 0;
81 32357 : move16(); /* Q0 */
82 32357 : mem[4] = 0;
83 32357 : move16(); /* Q14 */
84 32357 : mem[5] = 13107; /*0.8*/
85 32357 : move16(); /* Q14 */
86 :
87 32357 : return;
88 : }
89 :
90 : /*-------------------------------------------------------------------*
91 : * Function gp_clip
92 : *
93 : * The gain needs to be limited (gain pitch < 1.0) when one of the
94 : * following cases occurs:
95 : * - a resonance on LPC filter (lp_disp < 60 Hz) AND a good pitch
96 : * prediction (lp_gp > 0.9)
97 : * - target energy drops by 6 dB AND a good pitch prediction (lp_gp>1.0)
98 : *-------------------------------------------------------------------*/
99 :
100 613261 : Word16 gp_clip_fx(
101 : const Word16 element_mode, /* i : element mode Q0*/
102 : const Word32 core_brate, /* i : core bitrate Q0*/
103 : const Word16 *voicing, /* i : normalized correlations (from OL pitch) Q15*/
104 : const Word16 i_subfr, /* i : subframe index Q0*/
105 : const Word16 coder_type, /* i : type of coder Q0*/
106 : const Word16 xn[], /* i : target vector Q_new*/
107 : Word16 mem[], /* i/o: memory of gain of pitch clipping algorithm [2.56x,Q14,Q8,Q0,Q14,Q14]*/
108 : const Word16 Q_new /* i : scaling factor */
109 : )
110 : {
111 : Word16 clip;
112 : Word16 i, wener;
113 : Word16 e_ener, f_ener;
114 : Word32 ener;
115 : Word32 L_tmp;
116 : Word16 thres;
117 :
118 613261 : clip = 0;
119 613261 : move16();
120 613261 : test();
121 613261 : test();
122 613261 : IF( EQ_32( core_brate, ACELP_6k60 ) || EQ_32( core_brate, ACELP_8k85 ) || element_mode > EVS_MONO )
123 : {
124 606983 : thres = add( 14746 /* 0.9 in Q14 */, mult( 1638 /* 0.1 in Q14 */, extract_l( L_mult( mem[0], (Word16) ( 16384 / DIST_ISF_MAX_IO ) ) ) ) ); /* clipping is activated when filtered pitch gain > threshold (0.94 to 1 in Q14) */
125 606983 : if ( GT_16( mem[1], thres ) )
126 : {
127 0 : clip = 1;
128 0 : move16();
129 : }
130 : }
131 : ELSE
132 : {
133 6278 : test();
134 6278 : if ( LT_16( mem[0], DIST_ISF_THRES ) && GT_16( mem[1], GAIN_PIT_THRES ) )
135 : {
136 16 : clip = 1;
137 16 : move16();
138 : }
139 : }
140 :
141 613261 : ener = L_mac_sat( 1L, xn[0], xn[0] );
142 39248704 : FOR( i = 1; i < L_SUBFR; i++ )
143 : {
144 38635443 : ener = L_mac_sat( ener, xn[i], xn[i] );
145 : }
146 :
147 : /* ener = 10.0f*(float)log10(ener) */
148 613261 : e_ener = norm_l( ener );
149 613261 : f_ener = Log2_norm_lc( L_shl( ener, e_ener ) );
150 613261 : e_ener = sub( 30, e_ener );
151 613261 : IF( element_mode > EVS_MONO )
152 : {
153 606983 : e_ener = sub( e_ener, Q_new * 2 + 1 );
154 : }
155 : ELSE
156 : {
157 6278 : e_ener = sub( e_ener, Q_new );
158 : }
159 613261 : ener = Mpy_32_16( e_ener, f_ener, LG10 ); /* Q14 */
160 613261 : wener = round_fx( L_shl( ener, 10 ) ); /* Q8 */
161 :
162 613261 : test();
163 613261 : if ( LT_16( wener, sub( mem[2], 1536 /* 6.0f in Q8 */ ) ) && GT_16( mem[1], 16384 /* 1 in Q14 */ ) )
164 : {
165 0 : clip = 1;
166 0 : move16();
167 : }
168 :
169 613261 : mem[2] = wener; /* Q8 */
170 613261 : move16();
171 :
172 613261 : L_tmp = L_mult( ALPHA1, mem[4] ); /* Q30 */
173 :
174 613261 : test();
175 613261 : test();
176 613261 : if ( EQ_16( coder_type, GENERIC ) || EQ_16( coder_type, TRANSITION ) || EQ_16( coder_type, INACTIVE ) )
177 : {
178 : /* mem[4] = (1-ALPHA1) + ALPHA1 * mem[4], if branch taken */
179 : /* mem[4] = ALPHA1 * mem[4], otherwise */
180 548665 : L_tmp = L_add( L_tmp, 32768L * ( 32768 - ALPHA1 ) ); /* Q30 */
181 : }
182 613261 : mem[4] = round_fx( L_tmp ); /* Q14 */
183 :
184 613261 : L_tmp = L_mult( ALPHA4, mem[5] );
185 613261 : IF( i_subfr == 0 )
186 : {
187 : /* mem[5] = (1-ALPHA4) * voicing[0] + ALPHA4 * mem[5] */
188 141007 : mem[5] = mac_r( L_tmp, ( 32768 - ALPHA4 ) / 2, voicing[0] );
189 141007 : move16(); /* /2 to put voicing from Q15 to Q14 */
190 : }
191 :
192 613261 : IF( EQ_16( i_subfr, 2 * L_SUBFR ) )
193 : {
194 : /* mem[5] = (1-ALPHA4) * voicing[1] + ALPHA4 * mem[5] */
195 135876 : mem[5] = mac_r( L_tmp, ( 32768 - ALPHA4 ) / 2, voicing[1] );
196 135876 : move16(); /* /2 to put voicing from Q15 to Q14 */
197 : }
198 :
199 613261 : IF( GT_16( mem[3], WINDOW_SIZE ) )
200 : {
201 427860 : test();
202 427860 : if ( GT_16( mem[4], THRESH_TYPE ) && GT_16( mem[5], THRESH_VOICING ) )
203 : {
204 6109 : clip = 1;
205 6109 : move16();
206 : }
207 : }
208 : ELSE
209 : {
210 185401 : mem[3] = add( mem[3], 1 ); /* Q0 */
211 185401 : move16();
212 : }
213 :
214 613261 : return ( clip );
215 : }
216 :
217 : /*-------------------------------------------------------------------*
218 : * gp_clip_test_lsf()
219 : *
220 : * check the minimum distance of LSFs for pitch gain clipping flag
221 : *-------------------------------------------------------------------*/
222 :
223 0 : void gp_clip_test_isf_fx(
224 : const Word16 element_mode, /* i : element mode Q0*/
225 : const Word32 core_brate, /* i : core bitrate Q0*/
226 : const Word16 isf[], /* i : isf values (in frequency domain) Q2.56*/
227 : Word16 mem[], /* i/o: memory of gain of pitch clipping algorithm Q15*/
228 : const Word16 Opt_AMR_WB /* i : flag indicating AMR-WB IO mode Q0*/
229 : )
230 : {
231 : Word16 i, dist, dist_min, m;
232 :
233 0 : dist_min = sub( isf[1], isf[0] ); /* Q2.56 */
234 :
235 0 : m = M;
236 0 : move16();
237 0 : if ( EQ_16( Opt_AMR_WB, 1 ) )
238 : {
239 0 : m = M - 1;
240 0 : move16();
241 : }
242 :
243 0 : move16(); /* ptr init*/
244 0 : FOR( i = 2; i < m; i++ )
245 : {
246 0 : dist = sub( isf[i], isf[i - 1] ); /* Q2.56 */
247 0 : dist_min = s_min( dist, dist_min ); /* Q2.56 */
248 : }
249 :
250 0 : dist = extract_h( L_mac( L_mult( 26214 /* 0.8f in Q15 */, mem[0] ), 6554 /* 0.2f in Q15 */, dist_min ) ); /* Q15 */
251 :
252 0 : test();
253 0 : test();
254 0 : IF( EQ_32( core_brate, ACELP_6k60 ) || EQ_32( core_brate, ACELP_8k85 ) || element_mode > EVS_MONO )
255 : {
256 0 : dist = s_min( dist, DIST_ISF_MAX_IO );
257 : }
258 : ELSE
259 : {
260 0 : dist = s_min( dist, DIST_ISF_MAX );
261 : }
262 0 : mem[0] = dist; /* Q15 */
263 0 : move16();
264 :
265 0 : return;
266 : }
267 :
268 : /*-------------------------------------------------------------------*
269 : * gp_clip_test_gain_pit()
270 : *
271 : * low-pass filtering of the pitch gain for pitch gain clipping flag
272 : *-------------------------------------------------------------------*/
273 :
274 645221 : void gp_clip_test_gain_pit_fx(
275 : const Word16 element_mode, /* i : element mode Q0*/
276 : const Word32 core_brate, /* i : core bitrate Q0*/
277 : const Word16 gain_pit, /* i : gain of quantized pitch Q14*/
278 : Word16 mem[] /* i/o: memory of gain of pitch clipping algorithm 1Q14*/
279 : )
280 : {
281 : Word16 gain;
282 : Word32 L_tmp;
283 :
284 645221 : test();
285 645221 : test();
286 645221 : IF( EQ_32( core_brate, ACELP_6k60 ) || EQ_32( core_brate, ACELP_8k85 ) || element_mode > EVS_MONO )
287 : {
288 635786 : L_tmp = L_mult( 32113 /* 0.98 in Q15 */, mem[1] ); /* long term LTP gain average (>250ms) */
289 635786 : L_tmp = L_mac( L_tmp, 655 /* 0.02 in Q15 */, gain_pit );
290 : }
291 : ELSE
292 : {
293 9435 : L_tmp = L_mult( 29491 /* 0.9 in Q15 */, mem[1] );
294 9435 : L_tmp = L_mac( L_tmp, 3277 /* 0.1 in Q15 */, gain_pit );
295 : }
296 645221 : gain = extract_h( L_tmp );
297 645221 : gain = s_max( gain, GAIN_PIT_MIN );
298 645221 : mem[1] = gain; /* Q14 */
299 645221 : move16();
300 :
301 645221 : return;
302 : }
303 :
304 :
305 : /*-------------------------------------------------------------------*
306 : * Function gp_clip
307 : *
308 : * The gain needs to be limited (gain pitch < 1.0) when one of the
309 : * following cases occurs:
310 : * - a resonance on LPC filter (lp_disp < 60 Hz) AND a good pitch
311 : * prediction (lp_gp > 0.9)
312 : * - target energy drops by 6 dB AND a good pitch prediction (lp_gp>1.0)
313 : *-------------------------------------------------------------------*/
314 11120 : Word16 Mode2_gp_clip_fx(
315 : const Word16 *voicing, /* i : normalized correlations from OL pitch Q15 */
316 : const Word16 i_subfr, /* i : subframe index Q0 */
317 : const Word16 coder_type, /* i : type of coder Q0 */
318 : const Word16 xn[], /* i : target vector Q_xn */
319 : Word16 mem[], /* i/o: memory of gain of pitch clipping algorithm */
320 : /* mem[0]: Q0 */
321 : /* mem[1]: 1Q14 */
322 : /* mem[2]: 8Q7 */
323 : /* mem[3]: Q0 (integer) */
324 : /* mem[4]: Q14 */
325 : /* mem[5]: Q14 */
326 : const Word16 L_subfr, /* Q0 */
327 : const Word16 Q_xn /* i : scaling factor of vector xn[] */
328 : )
329 : {
330 : Word16 clip, tmp, exp_xn;
331 : Word16 i;
332 : Word32 wener, Ltmp;
333 :
334 11120 : move16();
335 11120 : clip = 0;
336 :
337 11120 : test();
338 11120 : if ( ( LT_16( mem[0], DIST_ISF_THRES ) ) && ( GT_16( mem[1], GAIN_PIT_THRES ) ) )
339 : {
340 0 : move16();
341 0 : clip = 1;
342 : }
343 :
344 : /*ener_exp = exp_xn * 2 + 1*/
345 11120 : exp_xn = add( shl( sub( 15, Q_xn ), 1 ), 1 );
346 11120 : wener = L_shr( 21474836l /*0.01f Q31*/, s_min( 31, exp_xn ) );
347 11120 : wener = L_max( 1, wener );
348 :
349 722800 : FOR( i = 0; i < L_subfr; i++ )
350 : {
351 711680 : wener = L_mac0_sat( wener, xn[i], xn[i] );
352 : }
353 :
354 : /*wener = 10.0f*(float)log10(wener);*/
355 11120 : wener = BASOP_Util_Log2( wener );
356 11120 : wener = L_add( wener, L_shl( exp_xn, 31 - LD_DATA_SCALE ) );
357 11120 : wener = Mpy_32_16_1( wener, LG10 ); /* wener in 8Q7 */
358 : #if ( GPCLIP_E != 6 + 2 )
359 : wener = shl( wener, GPCLIP_E - ( 6 + 2 ) );
360 : #endif
361 11120 : tmp = round_fx( wener );
362 : /* exponent of wener = 6+2 */
363 :
364 11120 : test();
365 12067 : if ( LT_16( tmp, sub( mem[2], 768 /*6.0f Q7*/ ) ) &&
366 947 : GT_16( mem[1], 16384 /*1.0f Q14*/ ) )
367 : {
368 0 : move16();
369 0 : clip = 1;
370 : }
371 :
372 11120 : move16();
373 11120 : mem[2] = tmp; /* wener in 8Q7 format */
374 11120 : Ltmp = Mpy_32_16_1( ALPHA1, mem[4] ); /* mem[4] in Q14 format, Ltmp in Q14 */
375 :
376 11120 : if ( s_or( (Word16) EQ_16( coder_type, GENERIC ), (Word16) EQ_16( coder_type, TRANSITION ) ) )
377 : {
378 1575 : Ltmp = L_add( Ltmp, ALPHA1_M1 ); /* Q30 */
379 : }
380 11120 : mem[4] = round_fx( Ltmp ); /* Q14 */
381 :
382 11120 : Ltmp = Mpy_32_16_1( ALPHA4, mem[5] ); /* mem[5] in Q14 format, Ltmp in Q14 */
383 11120 : IF( i_subfr == 0 )
384 : {
385 2627 : move16(); /* voicing: Q15 */
386 2627 : mem[5] = round_fx( L_add( Mpy_32_16_1( ALPHA4_M1, voicing[0] ), Ltmp ) ); /* Q14 */
387 : }
388 8493 : ELSE IF( EQ_16( i_subfr, shl( L_subfr, 1 ) ) )
389 : {
390 2627 : move16();
391 2627 : mem[5] = round_fx( L_add( Mpy_32_16_1( ALPHA4_M1, voicing[1] ), Ltmp ) ); /* Q14 */
392 : }
393 :
394 11120 : IF( GT_16( mem[3], WINDOW_SIZE ) )
395 : {
396 7691 : test();
397 7691 : if ( ( GT_16( mem[4], THRESH_TYPE ) ) && ( GT_16( mem[5], THRESH_VOICING ) ) )
398 : {
399 0 : move16();
400 0 : clip = 1;
401 : }
402 : }
403 : ELSE
404 : {
405 3429 : move16();
406 3429 : mem[3] = add( mem[3], 1 ); /* Q0 */
407 : }
408 :
409 :
410 11120 : return ( clip );
411 : }
412 :
413 : /*-------------------------------------------------------------------*
414 : * gp_clip_test_lsf:
415 : *
416 : * check the minimum distance of LSFs for pitch gain clipping flag
417 : *-------------------------------------------------------------------*/
418 2669 : void gp_clip_test_lsf_fx(
419 : const Word16 element_mode, /* i : element mode Q0*/
420 : const Word16 lsf[], /* i : lsf values (in frequency domain) 14Q1*1.28*/
421 : Word16 mem[], /* i/o: memory of gain of pitch clipping algorithm [2.56x,Q14,Q8,Q0,Q14,Q14]*/
422 : const Word16 m /* i : dimension of lsf Q0*/
423 : )
424 : {
425 : Word16 i;
426 : Word16 dist, dist_min, dist_max;
427 :
428 2669 : dist_max = DIST_ISF_MAX;
429 2669 : move16();
430 2669 : if ( element_mode > EVS_MONO )
431 : {
432 0 : dist_max = DIST_ISF_MAX_IO;
433 0 : move16();
434 : }
435 2669 : dist_min = sub( lsf[1], lsf[0] ); /* 14Q1*1.28 */
436 :
437 18867 : FOR( i = 2; i < m - 1; i++ )
438 : {
439 16198 : dist = sub( lsf[i], lsf[i - 1] ); /* 14Q1*1.28 */
440 16198 : dist_min = s_min( dist, dist_min );
441 : }
442 : /*dist = 0.8f*mem[0] + 0.2f*dist_min;*/
443 2669 : dist = s_min( dist_max, mac_r( L_mult( 26214 /*0.8f Q15*/, mem[0] ), 6554 /*0.2f Q15*/, dist_min ) ); /* x2.56 */
444 :
445 2669 : mem[0] = dist; /* x2.56 */
446 2669 : move16();
447 :
448 :
449 2669 : return;
450 : }
451 :
452 174460 : void gp_clip_test_lsf_ivas_fx(
453 : const Word16 element_mode, /* i : element mode Q0*/
454 : const Word32 core_brate, /* i : core bitrate Q0*/
455 : const Word16 lsf[], /* i : LSF vector 14Q1*1.28*/
456 : Word16 mem[], /* i/o: memory of gain of pitch clipping algorithm [2.56x,Q14,Q8,Q0,Q14,Q14]*/
457 : const Word16 Opt_AMR_WB /* i : flag indicating AMR-WB IO mode Q0*/
458 : )
459 : {
460 : Word16 i;
461 : Word16 m;
462 : Word16 dist, dist_min;
463 :
464 174460 : dist_min = sub( lsf[1], lsf[0] ); /* 14Q1*1.28 */
465 :
466 174460 : IF( Opt_AMR_WB )
467 : {
468 0 : m = M - 1;
469 0 : move16();
470 : }
471 : ELSE
472 : {
473 174460 : m = M;
474 174460 : move16();
475 : }
476 :
477 2616900 : FOR( i = 2; i < m; i++ )
478 : {
479 2442440 : dist = sub( lsf[i], lsf[i - 1] ); /* 14Q1*1.28 */
480 2442440 : dist_min = s_min( dist, dist_min );
481 : }
482 :
483 : // dist = 0.8f * mem[0] + 0.2f * dist_min;
484 174460 : dist = mac_r( L_mult( 26214 /*0.8f Q15*/, mem[0] ), 6554 /*0.2f Q15*/, dist_min ); /* 2.56x */
485 :
486 174460 : test();
487 174460 : test();
488 174460 : IF( EQ_32( core_brate, ACELP_6k60 ) || EQ_32( core_brate, ACELP_8k85 ) || ( element_mode > EVS_MONO ) )
489 : {
490 174460 : if ( GT_16( dist, DIST_ISF_MAX_IO ) )
491 : {
492 74088 : dist = DIST_ISF_MAX_IO;
493 74088 : move16();
494 : }
495 : }
496 0 : ELSE IF( GT_16( dist, DIST_ISF_MAX ) )
497 : {
498 0 : dist = DIST_ISF_MAX;
499 0 : move16();
500 : }
501 :
502 174460 : mem[0] = dist; /* 2.56x */
503 174460 : move16();
504 :
505 174460 : return;
506 : }
|