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 31725 : 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 31725 : mem[0] = DIST_ISF_MAX;
75 31725 : move16(); /* Q0 */
76 31725 : mem[1] = GAIN_PIT_MIN;
77 31725 : move16(); /* 1Q14 */
78 31725 : mem[2] = 0;
79 31725 : move16(); /* 8Q7 */ /* old energy of target (dB) */
80 31725 : mem[3] = 0;
81 31725 : move16(); /* Q0 */
82 31725 : mem[4] = 0;
83 31725 : move16(); /* Q14 */
84 31725 : mem[5] = 13107; /*0.8*/
85 31725 : move16(); /* Q14 */
86 :
87 31725 : 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 610289 : 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 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
118 610289 : Flag Overflow = 0;
119 610289 : move32();
120 : #endif
121 :
122 610289 : clip = 0;
123 610289 : move16();
124 610289 : test();
125 610289 : test();
126 610289 : IF( EQ_32( core_brate, ACELP_6k60 ) || EQ_32( core_brate, ACELP_8k85 ) || element_mode > EVS_MONO )
127 : {
128 604008 : 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) */
129 604008 : if ( GT_16( mem[1], thres ) )
130 : {
131 0 : clip = 1;
132 0 : move16();
133 : }
134 : }
135 : ELSE
136 : {
137 6281 : test();
138 6281 : if ( LT_16( mem[0], DIST_ISF_THRES ) && GT_16( mem[1], GAIN_PIT_THRES ) )
139 : {
140 23 : clip = 1;
141 23 : move16();
142 : }
143 : }
144 :
145 610289 : ener = L_mac_o( 1L, xn[0], xn[0], &Overflow );
146 39058496 : FOR( i = 1; i < L_SUBFR; i++ )
147 : {
148 38448207 : ener = L_mac_o( ener, xn[i], xn[i], &Overflow );
149 : }
150 :
151 : /* ener = 10.0f*(float)log10(ener) */
152 610289 : e_ener = norm_l( ener );
153 610289 : f_ener = Log2_norm_lc( L_shl( ener, e_ener ) );
154 610289 : e_ener = sub( 30, e_ener );
155 610289 : IF( element_mode > EVS_MONO )
156 : {
157 604008 : e_ener = sub( e_ener, Q_new * 2 + 1 );
158 : }
159 : ELSE
160 : {
161 6281 : e_ener = sub( e_ener, Q_new );
162 : }
163 610289 : ener = Mpy_32_16( e_ener, f_ener, LG10 ); /* Q14 */
164 610289 : wener = round_fx( L_shl( ener, 10 ) ); /* Q8 */
165 :
166 610289 : test();
167 610289 : if ( LT_16( wener, sub( mem[2], 1536 /* 6.0f in Q8 */ ) ) && GT_16( mem[1], 16384 /* 1 in Q14 */ ) )
168 : {
169 0 : clip = 1;
170 0 : move16();
171 : }
172 :
173 610289 : mem[2] = wener; /* Q8 */
174 610289 : move16();
175 :
176 610289 : L_tmp = L_mult( ALPHA1, mem[4] ); /* Q30 */
177 :
178 610289 : test();
179 610289 : test();
180 610289 : if ( EQ_16( coder_type, GENERIC ) || EQ_16( coder_type, TRANSITION ) || EQ_16( coder_type, INACTIVE ) )
181 : {
182 : /* mem[4] = (1-ALPHA1) + ALPHA1 * mem[4], if branch taken */
183 : /* mem[4] = ALPHA1 * mem[4], otherwise */
184 545877 : L_tmp = L_add( L_tmp, 32768L * ( 32768 - ALPHA1 ) ); /* Q30 */
185 : }
186 610289 : mem[4] = round_fx( L_tmp ); /* Q14 */
187 :
188 610289 : L_tmp = L_mult( ALPHA4, mem[5] );
189 610289 : IF( i_subfr == 0 )
190 : {
191 : /* mem[5] = (1-ALPHA4) * voicing[0] + ALPHA4 * mem[5] */
192 140325 : mem[5] = mac_r( L_tmp, ( 32768 - ALPHA4 ) / 2, voicing[0] );
193 140325 : move16(); /* /2 to put voicing from Q15 to Q14 */
194 : }
195 :
196 610289 : IF( EQ_16( i_subfr, 2 * L_SUBFR ) )
197 : {
198 : /* mem[5] = (1-ALPHA4) * voicing[1] + ALPHA4 * mem[5] */
199 135228 : mem[5] = mac_r( L_tmp, ( 32768 - ALPHA4 ) / 2, voicing[1] );
200 135228 : move16(); /* /2 to put voicing from Q15 to Q14 */
201 : }
202 :
203 610289 : IF( GT_16( mem[3], WINDOW_SIZE ) )
204 : {
205 426594 : test();
206 426594 : if ( GT_16( mem[4], THRESH_TYPE ) && GT_16( mem[5], THRESH_VOICING ) )
207 : {
208 6107 : clip = 1;
209 6107 : move16();
210 : }
211 : }
212 : ELSE
213 : {
214 183695 : mem[3] = add( mem[3], 1 ); /* Q0 */
215 183695 : move16();
216 : }
217 :
218 610289 : return ( clip );
219 : }
220 :
221 : /*-------------------------------------------------------------------*
222 : * gp_clip_test_lsf()
223 : *
224 : * check the minimum distance of LSFs for pitch gain clipping flag
225 : *-------------------------------------------------------------------*/
226 :
227 0 : void gp_clip_test_isf_fx(
228 : const Word16 element_mode, /* i : element mode Q0*/
229 : const Word32 core_brate, /* i : core bitrate Q0*/
230 : const Word16 isf[], /* i : isf values (in frequency domain) Q2.56*/
231 : Word16 mem[], /* i/o: memory of gain of pitch clipping algorithm Q15*/
232 : const Word16 Opt_AMR_WB /* i : flag indicating AMR-WB IO mode Q0*/
233 : )
234 : {
235 : Word16 i, dist, dist_min, m;
236 :
237 0 : dist_min = sub( isf[1], isf[0] ); /* Q2.56 */
238 :
239 0 : m = M;
240 0 : move16();
241 0 : if ( EQ_16( Opt_AMR_WB, 1 ) )
242 : {
243 0 : m = M - 1;
244 0 : move16();
245 : }
246 :
247 0 : move16(); /* ptr init*/
248 0 : FOR( i = 2; i < m; i++ )
249 : {
250 0 : dist = sub( isf[i], isf[i - 1] ); /* Q2.56 */
251 0 : dist_min = s_min( dist, dist_min ); /* Q2.56 */
252 : }
253 :
254 0 : dist = extract_h( L_mac( L_mult( 26214 /* 0.8f in Q15 */, mem[0] ), 6554 /* 0.2f in Q15 */, dist_min ) ); /* Q15 */
255 :
256 0 : test();
257 0 : test();
258 0 : IF( EQ_32( core_brate, ACELP_6k60 ) || EQ_32( core_brate, ACELP_8k85 ) || element_mode > EVS_MONO )
259 : {
260 0 : dist = s_min( dist, DIST_ISF_MAX_IO );
261 : }
262 : ELSE
263 : {
264 0 : dist = s_min( dist, DIST_ISF_MAX );
265 : }
266 0 : mem[0] = dist; /* Q15 */
267 0 : move16();
268 :
269 0 : return;
270 : }
271 :
272 : /*-------------------------------------------------------------------*
273 : * gp_clip_test_gain_pit()
274 : *
275 : * low-pass filtering of the pitch gain for pitch gain clipping flag
276 : *-------------------------------------------------------------------*/
277 :
278 641942 : void gp_clip_test_gain_pit_fx(
279 : const Word16 element_mode, /* i : element mode Q0*/
280 : const Word32 core_brate, /* i : core bitrate Q0*/
281 : const Word16 gain_pit, /* i : gain of quantized pitch Q14*/
282 : Word16 mem[] /* i/o: memory of gain of pitch clipping algorithm 1Q14*/
283 : )
284 : {
285 : Word16 gain;
286 : Word32 L_tmp;
287 :
288 641942 : test();
289 641942 : test();
290 641942 : IF( EQ_32( core_brate, ACELP_6k60 ) || EQ_32( core_brate, ACELP_8k85 ) || element_mode > EVS_MONO )
291 : {
292 632532 : L_tmp = L_mult( 32113 /* 0.98 in Q15 */, mem[1] ); /* long term LTP gain average (>250ms) */
293 632532 : L_tmp = L_mac( L_tmp, 655 /* 0.02 in Q15 */, gain_pit );
294 : }
295 : ELSE
296 : {
297 9410 : L_tmp = L_mult( 29491 /* 0.9 in Q15 */, mem[1] );
298 9410 : L_tmp = L_mac( L_tmp, 3277 /* 0.1 in Q15 */, gain_pit );
299 : }
300 641942 : gain = extract_h( L_tmp );
301 641942 : gain = s_max( gain, GAIN_PIT_MIN );
302 641942 : mem[1] = gain; /* Q14 */
303 641942 : move16();
304 :
305 641942 : return;
306 : }
307 :
308 :
309 : /*-------------------------------------------------------------------*
310 : * Function gp_clip
311 : *
312 : * The gain needs to be limited (gain pitch < 1.0) when one of the
313 : * following cases occurs:
314 : * - a resonance on LPC filter (lp_disp < 60 Hz) AND a good pitch
315 : * prediction (lp_gp > 0.9)
316 : * - target energy drops by 6 dB AND a good pitch prediction (lp_gp>1.0)
317 : *-------------------------------------------------------------------*/
318 11020 : Word16 Mode2_gp_clip_fx(
319 : const Word16 *voicing, /* i : normalized correlations from OL pitch Q15 */
320 : const Word16 i_subfr, /* i : subframe index Q0 */
321 : const Word16 coder_type, /* i : type of coder Q0 */
322 : const Word16 xn[], /* i : target vector Q_xn */
323 : Word16 mem[], /* i/o: memory of gain of pitch clipping algorithm */
324 : /* mem[0]: Q0 */
325 : /* mem[1]: 1Q14 */
326 : /* mem[2]: 8Q7 */
327 : /* mem[3]: Q0 (integer) */
328 : /* mem[4]: Q14 */
329 : /* mem[5]: Q14 */
330 : const Word16 L_subfr, /* Q0 */
331 : const Word16 Q_xn /* i : scaling factor of vector xn[] */
332 : )
333 : {
334 : Word16 clip, tmp, exp_xn;
335 : Word16 i;
336 : Word32 wener, Ltmp;
337 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
338 11020 : Flag Overflow = 0;
339 11020 : move32();
340 : #endif
341 11020 : move16();
342 11020 : clip = 0;
343 :
344 11020 : test();
345 11020 : if ( ( LT_16( mem[0], DIST_ISF_THRES ) ) && ( GT_16( mem[1], GAIN_PIT_THRES ) ) )
346 : {
347 0 : move16();
348 0 : clip = 1;
349 : }
350 :
351 : /*ener_exp = exp_xn * 2 + 1*/
352 11020 : exp_xn = add( shl( sub( 15, Q_xn ), 1 ), 1 );
353 11020 : wener = L_shr( 21474836l /*0.01f Q31*/, s_min( 31, exp_xn ) );
354 11020 : wener = L_max( 1, wener );
355 :
356 716300 : FOR( i = 0; i < L_subfr; i++ )
357 : {
358 705280 : wener = L_mac0_o( wener, xn[i], xn[i], &Overflow );
359 : }
360 :
361 : /*wener = 10.0f*(float)log10(wener);*/
362 11020 : wener = BASOP_Util_Log2( wener );
363 11020 : wener = L_add( wener, L_shl( exp_xn, 31 - LD_DATA_SCALE ) );
364 11020 : wener = Mpy_32_16_1( wener, LG10 ); /* wener in 8Q7 */
365 : #if ( GPCLIP_E != 6 + 2 )
366 : wener = shl( wener, GPCLIP_E - ( 6 + 2 ) );
367 : #endif
368 11020 : tmp = round_fx( wener );
369 : /* exponent of wener = 6+2 */
370 :
371 11020 : test();
372 11967 : if ( LT_16( tmp, sub( mem[2], 768 /*6.0f Q7*/ ) ) &&
373 947 : GT_16( mem[1], 16384 /*1.0f Q14*/ ) )
374 : {
375 0 : move16();
376 0 : clip = 1;
377 : }
378 :
379 11020 : move16();
380 11020 : mem[2] = tmp; /* wener in 8Q7 format */
381 11020 : Ltmp = Mpy_32_16_1( ALPHA1, mem[4] ); /* mem[4] in Q14 format, Ltmp in Q14 */
382 :
383 11020 : if ( s_or( (Word16) EQ_16( coder_type, GENERIC ), (Word16) EQ_16( coder_type, TRANSITION ) ) )
384 : {
385 1535 : Ltmp = L_add( Ltmp, ALPHA1_M1 ); /* Q30 */
386 : }
387 11020 : mem[4] = round_fx( Ltmp ); /* Q14 */
388 :
389 11020 : Ltmp = Mpy_32_16_1( ALPHA4, mem[5] ); /* mem[5] in Q14 format, Ltmp in Q14 */
390 11020 : IF( i_subfr == 0 )
391 : {
392 2604 : move16(); /* voicing: Q15 */
393 2604 : mem[5] = round_fx( L_add( Mpy_32_16_1( ALPHA4_M1, voicing[0] ), Ltmp ) ); /* Q14 */
394 : }
395 8416 : ELSE IF( EQ_16( i_subfr, shl( L_subfr, 1 ) ) )
396 : {
397 2604 : move16();
398 2604 : mem[5] = round_fx( L_add( Mpy_32_16_1( ALPHA4_M1, voicing[1] ), Ltmp ) ); /* Q14 */
399 : }
400 :
401 11020 : IF( GT_16( mem[3], WINDOW_SIZE ) )
402 : {
403 7586 : test();
404 7586 : if ( ( GT_16( mem[4], THRESH_TYPE ) ) && ( GT_16( mem[5], THRESH_VOICING ) ) )
405 : {
406 0 : move16();
407 0 : clip = 1;
408 : }
409 : }
410 : ELSE
411 : {
412 3434 : move16();
413 3434 : mem[3] = add( mem[3], 1 ); /* Q0 */
414 : }
415 :
416 :
417 11020 : return ( clip );
418 : }
419 :
420 : /*-------------------------------------------------------------------*
421 : * gp_clip_test_lsf:
422 : *
423 : * check the minimum distance of LSFs for pitch gain clipping flag
424 : *-------------------------------------------------------------------*/
425 2693 : void gp_clip_test_lsf_fx(
426 : const Word16 element_mode, /* i : element mode Q0*/
427 : const Word16 lsf[], /* i : lsf values (in frequency domain) 14Q1*1.28*/
428 : Word16 mem[], /* i/o: memory of gain of pitch clipping algorithm [2.56x,Q14,Q8,Q0,Q14,Q14]*/
429 : const Word16 m /* i : dimension of lsf Q0*/
430 : )
431 : {
432 : Word16 i;
433 : Word16 dist, dist_min, dist_max;
434 :
435 2693 : dist_max = DIST_ISF_MAX;
436 2693 : move16();
437 2693 : if ( element_mode > EVS_MONO )
438 : {
439 0 : dist_max = DIST_ISF_MAX_IO;
440 0 : move16();
441 : }
442 2693 : dist_min = sub( lsf[1], lsf[0] ); /* 14Q1*1.28 */
443 :
444 19151 : FOR( i = 2; i < m - 1; i++ )
445 : {
446 16458 : dist = sub( lsf[i], lsf[i - 1] ); /* 14Q1*1.28 */
447 16458 : dist_min = s_min( dist, dist_min );
448 : }
449 : /*dist = 0.8f*mem[0] + 0.2f*dist_min;*/
450 2693 : dist = s_min( dist_max, mac_r( L_mult( 26214 /*0.8f Q15*/, mem[0] ), 6554 /*0.2f Q15*/, dist_min ) ); /* x2.56 */
451 :
452 2693 : mem[0] = dist; /* x2.56 */
453 2693 : move16();
454 :
455 :
456 2693 : return;
457 : }
458 :
459 173729 : void gp_clip_test_lsf_ivas_fx(
460 : const Word16 element_mode, /* i : element mode Q0*/
461 : const Word32 core_brate, /* i : core bitrate Q0*/
462 : const Word16 lsf[], /* i : LSF vector 14Q1*1.28*/
463 : Word16 mem[], /* i/o: memory of gain of pitch clipping algorithm [2.56x,Q14,Q8,Q0,Q14,Q14]*/
464 : const Word16 Opt_AMR_WB /* i : flag indicating AMR-WB IO mode Q0*/
465 : )
466 : {
467 : Word16 i;
468 : Word16 m;
469 : Word16 dist, dist_min;
470 :
471 173729 : dist_min = sub( lsf[1], lsf[0] ); /* 14Q1*1.28 */
472 :
473 173729 : IF( Opt_AMR_WB )
474 : {
475 0 : m = M - 1;
476 0 : move16();
477 : }
478 : ELSE
479 : {
480 173729 : m = M;
481 173729 : move16();
482 : }
483 :
484 2605935 : FOR( i = 2; i < m; i++ )
485 : {
486 2432206 : dist = sub( lsf[i], lsf[i - 1] ); /* 14Q1*1.28 */
487 2432206 : dist_min = s_min( dist, dist_min );
488 : }
489 :
490 : // dist = 0.8f * mem[0] + 0.2f * dist_min;
491 173729 : dist = mac_r( L_mult( 26214 /*0.8f Q15*/, mem[0] ), 6554 /*0.2f Q15*/, dist_min ); /* 2.56x */
492 :
493 173729 : test();
494 173729 : test();
495 173729 : IF( EQ_32( core_brate, ACELP_6k60 ) || EQ_32( core_brate, ACELP_8k85 ) || ( element_mode > EVS_MONO ) )
496 : {
497 173729 : if ( GT_16( dist, DIST_ISF_MAX_IO ) )
498 : {
499 74020 : dist = DIST_ISF_MAX_IO;
500 74020 : move16();
501 : }
502 : }
503 0 : ELSE IF( GT_16( dist, DIST_ISF_MAX ) )
504 : {
505 0 : dist = DIST_ISF_MAX;
506 0 : move16();
507 : }
508 :
509 173729 : mem[0] = dist; /* 2.56x */
510 173729 : move16();
511 :
512 173729 : return;
513 : }
|