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 "rom_com.h"
45 : #include "prot.h"
46 : #include "wmc_auto.h"
47 :
48 : /*-------------------------------------------------------------------*
49 : * gs_noisf()
50 : *
51 : * Noise fill-in function
52 : *-------------------------------------------------------------------*/
53 :
54 6132119 : static void gs_noisf(
55 : const int16_t Start_BIN, /* i : First bin for noise fill */
56 : const int16_t NB_Qbins, /* i : Number of bin per band */
57 : const float Noise_fac, /* i : Noise level */
58 : const float *y_norm, /* i : Quantized pulses */
59 : float *exc_diffQ, /* o : Quantized pulses with noise added */
60 : int16_t *seed_tcx, /* i : Random generator seed */
61 : const int16_t coder_type /* i : coder type */
62 : )
63 : {
64 : float ftmp;
65 : int16_t i, k;
66 6132119 : int16_t NB_zer = NB_Qbins / 2;
67 :
68 6132119 : if ( coder_type == INACTIVE )
69 : {
70 2020896 : NB_zer = 2;
71 : }
72 :
73 : /*----------------------------------------------*
74 : * noise fill-in on unquantized subvector *
75 : * injected only from 1066Hz to 6400Hz. *
76 : *----------------------------------------------*/
77 :
78 31250821 : for ( k = Start_BIN; k < NB_Qbins + Start_BIN; k += NB_zer )
79 : {
80 25118702 : ftmp = 0.0;
81 124679438 : for ( i = k; i < k + NB_zer; i++ )
82 : {
83 99560736 : exc_diffQ[i] = (float) y_norm[i];
84 99560736 : ftmp += exc_diffQ[i] * exc_diffQ[i];
85 : }
86 :
87 25118702 : if ( ftmp < .5 )
88 : {
89 92486795 : for ( i = k; i < k + NB_zer; i++ )
90 : {
91 73273604 : exc_diffQ[i] += Noise_fac * ( (float) own_random( seed_tcx ) / PCM16_TO_FLT_FAC );
92 : }
93 : }
94 : else
95 : {
96 : /* This is added only to keep the seed in sync between different compilers */
97 32192643 : for ( i = k; i < k + NB_zer; i++ )
98 : {
99 26287132 : own_random( seed_tcx );
100 : }
101 : }
102 : }
103 :
104 6132119 : return;
105 : }
106 :
107 : /*-------------------------------------------------------------------*
108 : * EstimateNoiseLevel_inner()
109 : *
110 : * Estimate noise level from the power spectrum
111 : *-------------------------------------------------------------------*/
112 :
113 234376 : static void EstimateNoiseLevel_inner(
114 : float *noisepb, /* o : Noise per band */
115 : const int32_t bitrate, /* i : Bitrate of the codec */
116 : const int16_t i_band, /* i : First band to compute the noise */
117 : const int16_t Mbands_gn /* i : number of bands */
118 : )
119 : {
120 : int16_t i;
121 : float noise_offset;
122 :
123 234376 : noise_offset = 0.25f;
124 234376 : if ( bitrate > ACELP_24k40 )
125 : {
126 3607 : noise_offset = .2f;
127 : }
128 230769 : else if ( bitrate >= ACELP_22k60 )
129 : {
130 7080 : noise_offset = .3f;
131 : }
132 223689 : else if ( bitrate >= ACELP_9k60 )
133 : {
134 103005 : noise_offset = 0.35f;
135 : }
136 : else
137 : {
138 120684 : noise_offset = .4f;
139 : }
140 :
141 234376 : set_f( noisepb + i_band, noise_offset, Mbands_gn - i_band );
142 :
143 1406256 : for ( i = i_band; i < 5; i++ )
144 : {
145 1171880 : if ( noisepb[i] > 0.2f )
146 : {
147 1153845 : noisepb[i] = 0.2f;
148 : }
149 : }
150 :
151 234376 : return;
152 : }
153 :
154 :
155 : /*-------------------------------------------------------------------*
156 : * EstimateNoiseLevel()
157 : *
158 : *
159 : *-------------------------------------------------------------------*/
160 :
161 234376 : static void EstimateNoiseLevel(
162 : float *noisepb, /* o : Noise per band */
163 : const int32_t bitrate, /* i : Bitrate of the codec */
164 : const int16_t Diff_len, /* i : number of bin before cut-off frequency */
165 : const int16_t Mbands_gn, /* i : number of bands */
166 : const int16_t coder_type, /* i : coder type */
167 : const int16_t noise_lev, /* i : pulses dynamic */
168 : const int16_t pit_band_idx, /* i : bin position of the cut-off frequency */
169 : const int16_t last_bin, /* i : the last bin of bit allocation */
170 : const int16_t bwidth,
171 : const int16_t L_frame /* i : frame length */
172 : )
173 : {
174 : int16_t i_band;
175 :
176 234376 : i_band = 0;
177 :
178 234376 : if ( Diff_len < L_frame )
179 : {
180 234376 : EstimateNoiseLevel_inner( noisepb, bitrate, i_band, Mbands_gn );
181 :
182 234376 : if ( coder_type != INACTIVE )
183 : {
184 114340 : if ( ( bitrate == ACELP_8k00 && last_bin > 8 ) && bwidth != NB )
185 : {
186 7252 : while ( Mbands_gn > i_band )
187 : {
188 6656 : noisepb[i_band] *= 2.0f;
189 6656 : i_band++;
190 : }
191 : }
192 : else
193 : {
194 1124763 : while ( pit_band_idx > i_band )
195 : {
196 1011019 : noisepb[i_band] /= 2.0f;
197 1011019 : i_band++;
198 : }
199 : }
200 : }
201 : }
202 :
203 234376 : if ( ( coder_type == INACTIVE || noise_lev >= NOISE_LEVEL_SP3 ) && L_frame == L_FRAME )
204 : {
205 1209365 : for ( i_band = 9; i_band < Mbands_gn; i_band++ )
206 : {
207 1057690 : noisepb[i_band] *= 1.15f;
208 : }
209 : }
210 82701 : else if ( L_frame == L_FRAME16k )
211 : {
212 45568 : if ( bitrate == ACELP_13k20 )
213 : {
214 0 : set_f( noisepb, .45f, Mbands_gn );
215 : }
216 :
217 45568 : if ( coder_type == INACTIVE )
218 : {
219 865792 : for ( ; i_band < Mbands_gn; i_band++ )
220 : {
221 820224 : noisepb[i_band] = .4f;
222 : }
223 : }
224 0 : else if ( ( noise_lev <= NOISE_LEVEL_SP1 ) && bitrate > ACELP_16k40 )
225 : {
226 0 : for ( ; i_band < Mbands_gn - 4; i_band++ )
227 : {
228 0 : noisepb[i_band] *= .6f;
229 : }
230 : }
231 0 : else if ( ( noise_lev <= NOISE_LEVEL_SP2 ) && bitrate > ACELP_16k40 )
232 : {
233 0 : for ( ; i_band < Mbands_gn - 4; i_band++ )
234 : {
235 0 : noisepb[i_band] *= .8f;
236 : }
237 : }
238 : }
239 :
240 234376 : return;
241 : }
242 :
243 :
244 : /*-------------------------------------------------------------------*
245 : * Apply_NoiseFill()
246 : *
247 : *
248 : *-------------------------------------------------------------------*/
249 :
250 378030 : static void Apply_NoiseFill(
251 : float *exc_diffQ, /* i/o: Noise per band */
252 : int16_t *seed_tcx, /* i : Seed for noise */
253 : const float *noisepb, /* i : Noise per band */
254 : const int16_t Diff_len, /* i : number of bin before cut-off frequency */
255 : const int16_t Mbands_gn, /* i : number of bands */
256 : const int16_t coder_type, /* i : coder type */
257 : const int16_t *freq_nsbin_per_band /* i : bin per bands tables */
258 : )
259 : {
260 : int16_t StartBin, NB_Qbins, i_band;
261 378030 : StartBin = 0;
262 378030 : NB_Qbins = 0;
263 :
264 6510149 : for ( i_band = 0; i_band < Mbands_gn; i_band++ )
265 : {
266 6132119 : StartBin += NB_Qbins;
267 6132119 : NB_Qbins = freq_nsbin_per_band[i_band];
268 :
269 6132119 : if ( Diff_len < L_FRAME )
270 : {
271 6132119 : gs_noisf( StartBin, NB_Qbins, noisepb[i_band], exc_diffQ, exc_diffQ, seed_tcx, coder_type );
272 : }
273 : }
274 :
275 378030 : return;
276 : }
277 :
278 :
279 : /*-------------------------------------------------------------------*
280 : * freq_dnw_scaling()
281 : *
282 : *
283 : *-------------------------------------------------------------------*/
284 :
285 332631 : void freq_dnw_scaling(
286 : const int16_t cor_strong_limit, /* i : HF correlation */
287 : const int16_t coder_type, /* i : coder type */
288 : const int16_t noise_lev, /* i : Noise level */
289 : const int32_t core_brate, /* i : Core bitrate */
290 : float fy_norm[], /* i/o: Frequency quantized parameter */
291 : const int16_t L_frame /* i : frame length */
292 : )
293 : {
294 : float sc_dyn;
295 : int16_t start_sc, i;
296 :
297 332631 : sc_dyn = 1.0f;
298 332631 : start_sc = L_frame;
299 :
300 332631 : if ( core_brate <= ACELP_8k00 && coder_type == INACTIVE )
301 : {
302 8331 : sc_dyn *= .15f;
303 8331 : start_sc = 64;
304 : }
305 324300 : else if ( coder_type == INACTIVE )
306 : {
307 92208 : sc_dyn *= .25f;
308 92208 : start_sc = 80;
309 : }
310 : else
311 : {
312 232092 : sc_dyn = (float) ( NOISE_LEVEL_SP3 - noise_lev ) / 10.0f + 0.4f;
313 232092 : start_sc = 112 + ( NOISE_LEVEL_SP3 - noise_lev ) * 16;
314 :
315 232092 : if ( noise_lev == NOISE_LEVEL_SP0 )
316 : {
317 117828 : start_sc = L_FRAME;
318 : }
319 : }
320 :
321 332631 : if ( L_frame == L_FRAME16k && core_brate <= ACELP_24k40 )
322 : {
323 41961 : sc_dyn += 0.125f;
324 41961 : if ( sc_dyn > 1.0f )
325 : {
326 0 : sc_dyn = 1.0f;
327 : }
328 : }
329 :
330 36436983 : for ( i = start_sc; i < L_frame; i++ )
331 : {
332 36104352 : fy_norm[i] *= sc_dyn;
333 : }
334 :
335 332631 : if ( ( core_brate < ACELP_13k20 && cor_strong_limit == 0 ) || core_brate < ACELP_9k60 )
336 : {
337 24330219 : for ( i = 160; i < L_frame; i++ )
338 : {
339 24079392 : if ( fy_norm[i] > 1.0f )
340 : {
341 17889 : fy_norm[i] = 1.0f;
342 : }
343 :
344 24079392 : if ( fy_norm[i] < -1.0f )
345 : {
346 19874 : fy_norm[i] = -1.0f;
347 : }
348 : }
349 : }
350 81804 : else if ( core_brate < ACELP_22k60 )
351 : {
352 9081341 : for ( i = 160; i < L_frame; i++ )
353 : {
354 9011168 : if ( fy_norm[i] > 1.5f )
355 : {
356 24954 : fy_norm[i] = 1.5f;
357 : }
358 :
359 9011168 : if ( fy_norm[i] < -1.5f )
360 : {
361 25528 : fy_norm[i] = -1.5f;
362 : }
363 : }
364 : }
365 :
366 332631 : return;
367 : }
368 :
369 :
370 : /*-------------------------------------------------------------------*
371 : * Decreas_freqPeak()
372 : *
373 : *
374 : *-------------------------------------------------------------------*/
375 :
376 2337 : static void Decreas_freqPeak(
377 : const float *lsf_new, /* i : ISFs at the end of the frame */
378 : float *exc_diffQ, /* i/o: frequency coefficients of per band */
379 : float thr_rat /* i : threshold of ratio between consecutive lsf_new_diff */
380 : )
381 : {
382 : int16_t i, j, k;
383 2337 : int16_t last_bin = 0;
384 2337 : int16_t pos = 0;
385 : float *src;
386 : float avrg, max_val;
387 : float lsf_new_diff[M];
388 2337 : lsf_new_diff[0] = 0; /* prevent unitialized value */
389 35055 : for ( j = 1; j < ( M - 1 ); j++ )
390 : {
391 32718 : lsf_new_diff[j] = lsf_new[j] - lsf_new[j - 1];
392 : }
393 :
394 2337 : avrg = 0.0f;
395 : /* This is to prevent a possible div by 0 in the '*(src) = (*src > 0) ?...'
396 : loop. The value of 'max' is not important because it will be mutiplied
397 : by 'avrg' and the result will be close to 0. The 'fabs(*src)/max'
398 : div by 0 error will be avoided. */
399 2337 : max_val = 0.001f;
400 226689 : for ( i = 160; i < L_FRAME; i++ )
401 : {
402 224352 : if ( fabs( exc_diffQ[i] ) > max_val )
403 : {
404 14012 : max_val = (float) fabs( exc_diffQ[i] );
405 14012 : pos = i;
406 : }
407 224352 : avrg += (float) fabs( exc_diffQ[i] );
408 : }
409 2337 : avrg /= 96;
410 2337 : last_bin = M - 1; /* When the search is false, should equate the end of the vector, not the beginning */
411 26609 : for ( i = 0; i < ( M - 1 ); i++ )
412 : {
413 26609 : if ( lsf_new[i] > 4000 )
414 : {
415 2337 : last_bin = i;
416 2337 : break;
417 : }
418 : }
419 :
420 10783 : for ( i = last_bin; i < 14; i++ )
421 : {
422 8446 : if ( lsf_new_diff[i] < thr_rat * lsf_new_diff[i - 1] )
423 : {
424 290 : src = &exc_diffQ[( i - 1 ) * 16];
425 870 : for ( j = 0; j < 2; j++ )
426 : {
427 9860 : for ( k = 0; k < 16; k++ )
428 : {
429 9280 : if ( fabs( *src ) > 2.0f * avrg )
430 : {
431 702 : *( src ) = ( *src > 0 ) ? (float) ( avrg * ( 2.0f - fabs( *src ) / max_val ) ) : (float) ( -avrg * ( 2.0f - fabs( *src ) / max_val ) );
432 : }
433 9280 : src++;
434 : }
435 : }
436 : }
437 : }
438 :
439 2337 : if ( fabs( exc_diffQ[pos] ) == max_val && max_val > 4.0f * avrg )
440 : {
441 4800 : for ( i = pos - 1; i < pos + 2; i++ )
442 : {
443 3600 : exc_diffQ[pos] *= 0.5f;
444 : }
445 : }
446 :
447 2337 : return;
448 : }
449 :
450 :
451 : /*-------------------------------------------------------------------*
452 : * envelop_modify()
453 : *
454 : *
455 : *-------------------------------------------------------------------*/
456 :
457 1697 : static void envelop_modify(
458 : float *exc_diffQ, /* i/o: frequency coefficients of per band */
459 : int16_t *seed_tcx, /* i : Seed for noise */
460 : int16_t last_bin, /* i : last bin of bit allocation */
461 : float *Ener_per_bd_iQ /* i : Quantized energy of targeted vector */
462 : )
463 : {
464 : int16_t i, j, end_band;
465 : float Ener, Ener1, *src;
466 1697 : float weight = 1.0f;
467 :
468 1697 : end_band = L_FRAME;
469 1697 : Ener = 0.1f;
470 164609 : for ( i = last_bin * 16; i < end_band; i++ )
471 : {
472 162912 : Ener += exc_diffQ[i] * exc_diffQ[i];
473 : }
474 1697 : Ener = (float) sqrt( ( end_band - last_bin * 16 ) / Ener );
475 :
476 1697 : weight = 0.5f;
477 :
478 1697 : src = &exc_diffQ[16 * last_bin];
479 8485 : for ( i = last_bin; i < last_bin + 4; i++ )
480 : {
481 6788 : Ener1 = (float) ( 0.4f * pow( 10, Ener_per_bd_iQ[i + 1] ) );
482 115396 : for ( j = 0; j < 16; j++ )
483 : {
484 108608 : *src = Ener1 * ( weight * ( *src ) * Ener + ( 1.0f - weight ) * own_random( seed_tcx ) / PCM16_TO_FLT_FAC );
485 108608 : src++;
486 : }
487 : }
488 :
489 1697 : Ener1 = (float) ( 0.4f * pow( 10, Ener_per_bd_iQ[15] ) );
490 :
491 1697 : src = &exc_diffQ[224];
492 56001 : for ( j = 0; j < 32; j++ )
493 : {
494 54304 : *src = Ener1 * ( weight * ( *src ) * Ener + ( 1.0f - weight ) * own_random( seed_tcx ) / PCM16_TO_FLT_FAC );
495 54304 : src++;
496 : }
497 :
498 1697 : return;
499 : }
500 :
501 :
502 : /*-------------------------------------------------------------------*
503 : * highband_exc_dct_in()
504 : *
505 : *
506 : *-------------------------------------------------------------------*/
507 :
508 909389 : void highband_exc_dct_in(
509 : const int32_t core_brate, /* i : core bitrate */
510 : const int16_t *mfreq_bindiv, /* i : bin per bands tables */
511 : int16_t last_bin, /* i : last bin of bit allocation */
512 : int16_t Diff_len, /* i : number of bin before cut-off frequency */
513 : int16_t noise_lev, /* i : pulses dynamic */
514 : int16_t pit_band_idx, /* i : bin position of the cut-off frequency */
515 : float *exc_diffQ, /* i : frequency coefficients of per band */
516 : int16_t *seed_tcx, /* i : Seed for noise */
517 : float *Ener_per_bd_iQ, /* i : Quantized energy of targeted vector */
518 : int16_t nb_subfr, /* i : Number of subframe considered */
519 : float *exc_dct_in, /* o : dct of residual signal */
520 : int16_t last_coder_type, /* i : coding type of last frame */
521 : int16_t *bitallocation_band, /* i : bit allocation flag of each band */
522 : const float *lsf_new, /* i : LSFs at the end of the frame */
523 : float *last_exc_dct_in, /* i : dct of residual signal of last frame */
524 : float *last_ener, /* i : frequency energy of last frame */
525 : int16_t *last_bitallocation_band, /* i : bit allocation flag of each band of last frame */
526 : int16_t *bitallocation_exc, /* i : flag of decoded coefficients */
527 : const int16_t bfi, /* i : bad frame indicator */
528 : const int16_t coder_type, /* i : coder type */
529 : const int16_t bwidth, /* i : audio bandwidth */
530 : float *exc_wo_nf, /* o : temporal excitation (in f domain) without noisefill */
531 : const int16_t GSC_noisy_speech, /* i : GSC noisy speech flag */
532 : float *lt_ener_per_band, /* i/o: Average per band energy */
533 : const int16_t L_frame, /* i : frame length */
534 : const int16_t element_mode, /* i : IVAS element mode */
535 : const int16_t GSC_IVAS_mode /* i : GSC IVAS mode */
536 : )
537 : {
538 : int16_t i, j;
539 909389 : int16_t MAX_Bin = 0;
540 : int16_t last_bin_tmp;
541 : float noisepb[MBANDS_GN16k];
542 : float Ener_per_bd_yQ[MBANDS_GN16k];
543 : float *src, *dst, *end;
544 909389 : float ener = 0.0f;
545 909389 : int16_t length_bin, bwe_flag = 0;
546 :
547 6365723 : for ( j = 10; j < MBANDS_GN; j++ )
548 : {
549 5456334 : ener += (float) pow( 10, Ener_per_bd_iQ[j] );
550 : }
551 :
552 909389 : if ( core_brate == ACELP_8k00 && bwidth != NB )
553 : {
554 2337 : if ( last_coder_type != AUDIO )
555 : {
556 640 : *last_ener = ener;
557 : }
558 :
559 2337 : if ( ( last_bin > 8 || Diff_len != 0 ) && last_coder_type == AUDIO )
560 : {
561 1697 : MAX_Bin = 10;
562 1697 : bwe_flag = 1;
563 : }
564 : else
565 : {
566 640 : MAX_Bin = 15;
567 : }
568 :
569 2337 : last_bin_tmp = last_bin;
570 2337 : if ( last_bin < MAX_Bin )
571 : {
572 1761 : last_bin = MAX_Bin;
573 : }
574 2337 : last_bin += 1;
575 : }
576 : else
577 : {
578 907052 : if ( L_frame == L_FRAME16k )
579 : {
580 46062 : last_bin = MBANDS_GN16k;
581 : }
582 : else
583 : {
584 860990 : last_bin = MBANDS_GN;
585 : }
586 907052 : last_bin_tmp = last_bin;
587 : }
588 :
589 :
590 909389 : if ( bfi || core_brate < 6000 || ( core_brate < 8600 && coder_type == UNVOICED ) )
591 : {
592 549741 : set_f( noisepb, 0.4f, MBANDS_GN );
593 : }
594 359648 : else if ( GSC_IVAS_mode == 3 || ( GSC_IVAS_mode > 0 && GSC_noisy_speech == 1 ) )
595 : {
596 125272 : set_f( noisepb, 0.4f, MBANDS_GN16k );
597 : }
598 : else
599 : {
600 234376 : EstimateNoiseLevel( noisepb, core_brate, Diff_len, last_bin, coder_type, noise_lev, pit_band_idx, last_bin_tmp, bwidth, L_frame );
601 : }
602 :
603 909389 : if ( exc_wo_nf != NULL )
604 : {
605 885475 : mvr2r( exc_diffQ, exc_wo_nf, L_frame );
606 : }
607 :
608 909389 : if ( GSC_IVAS_mode == 0 && GSC_noisy_speech && !bfi && element_mode <= IVAS_SCE )
609 : {
610 549738 : set_f( noisepb, 0.1f, MBANDS_GN );
611 : }
612 :
613 909389 : if ( core_brate < 6000 && coder_type <= UNVOICED )
614 : {
615 136559263 : for ( i = 0; i < L_frame; i++ )
616 : {
617 136027904 : if ( exc_diffQ[i] == 0.0f )
618 : {
619 134447402 : exc_diffQ[i] += 2.0f * noisepb[0] * ( (float) own_random( seed_tcx ) / PCM16_TO_FLT_FAC );
620 : }
621 : }
622 : }
623 : else
624 : {
625 378030 : Apply_NoiseFill( exc_diffQ, seed_tcx, noisepb, Diff_len, last_bin, coder_type, mfreq_bindiv );
626 : }
627 :
628 : /*--------------------------------------------------------------------------------------*
629 : * Quantize average gain
630 : * Subtract Q averaged gain
631 : * VQ of remaining gain per band
632 : *--------------------------------------------------------------------------------------*/
633 :
634 909389 : if ( core_brate == ACELP_8k00 && bwidth != NB )
635 : {
636 2337 : Ener_per_band_comp( exc_diffQ, Ener_per_bd_yQ, last_bin + 1, 0, L_frame );
637 : }
638 : else
639 : {
640 907052 : Ener_per_band_comp( exc_diffQ, Ener_per_bd_yQ, MBANDS_GN, 1, L_frame );
641 :
642 907052 : if ( nb_subfr < 4 && L_frame < L_FRAME16k )
643 : {
644 12109253 : for ( i = L_FRAME - 16; i < L_FRAME; i++ )
645 : {
646 11396944 : exc_diffQ[i] *= ( 0.067f * i - 15.0f );
647 : }
648 : }
649 : }
650 :
651 : /*--------------------------------------------------------------------------------------*
652 : * Check potential energy excitation overshoot
653 : *--------------------------------------------------------------------------------------*/
654 :
655 909389 : if ( bfi )
656 : {
657 23914 : if ( GSC_noisy_speech == 0 && coder_type > UNVOICED ) /* Here coder_type == last_coder_type because of the bfi */
658 : {
659 283984 : for ( i = 0; i < last_bin; i++ )
660 : {
661 267280 : Ener_per_bd_iQ[i] = min( Ener_per_bd_iQ[i], ( lt_ener_per_band[i] - 0.0376f ) - Ener_per_bd_yQ[i] );
662 267280 : lt_ener_per_band[i] -= 0.0188f;
663 : }
664 16864 : for ( ; i < MBANDS_GN; i++ )
665 : {
666 160 : Ener_per_bd_iQ[i] = min( Ener_per_bd_iQ[i], ( lt_ener_per_band[i] - 0.0376f ) );
667 160 : lt_ener_per_band[i] -= 0.0188f;
668 : }
669 : }
670 : else
671 : {
672 123382 : for ( i = 0; i < last_bin; i++ )
673 : {
674 116172 : Ener_per_bd_iQ[i] = min( Ener_per_bd_iQ[i], ( lt_ener_per_band[i] + 0.3f ) - Ener_per_bd_yQ[i] );
675 116172 : lt_ener_per_band[i] -= 0.0188f;
676 : }
677 7210 : for ( ; i < MBANDS_GN; i++ )
678 : {
679 0 : Ener_per_bd_iQ[i] = min( Ener_per_bd_iQ[i], ( lt_ener_per_band[i] + 0.3f ) );
680 0 : lt_ener_per_band[i] -= 0.0188f;
681 : }
682 : }
683 : }
684 :
685 : /*--------------------------------------------------------------------------------------*
686 : * Apply decoded gain onto the difference signal
687 : *--------------------------------------------------------------------------------------*/
688 :
689 909389 : if ( GSC_IVAS_mode >= 1 )
690 : {
691 162595 : float scale_factLF = 0.9f;
692 162595 : float scale_factHF = 0.9f;
693 :
694 162595 : if ( GSC_IVAS_mode == 1 && GSC_noisy_speech == 0 )
695 : {
696 31004 : scale_factHF = 0.8f;
697 : }
698 131591 : else if ( GSC_IVAS_mode == 2 || GSC_noisy_speech == 1 )
699 : {
700 13273 : scale_factHF = 0.71f;
701 : }
702 118318 : else if ( GSC_IVAS_mode == 3 )
703 : {
704 118318 : scale_factHF = 0.9f;
705 : }
706 23411507 : for ( i = 0; i < pit_band_idx * 16; i++ )
707 : {
708 23248912 : exc_diffQ[i] *= scale_factLF;
709 : }
710 18538003 : for ( ; i < L_frame; i++ )
711 : {
712 18375408 : exc_diffQ[i] *= scale_factHF;
713 : }
714 : }
715 746794 : else if ( GSC_noisy_speech )
716 : {
717 570588 : float scale_fact = 0.9f;
718 :
719 570588 : if ( element_mode == IVAS_CPE_TD )
720 : {
721 12366 : if ( coder_type == INACTIVE )
722 : {
723 12142 : scale_fact = 1.0f;
724 : }
725 : else
726 : {
727 224 : scale_fact = 0.95f;
728 : }
729 : }
730 558222 : else if ( element_mode > IVAS_SCE )
731 : {
732 2676 : scale_fact = 0.71f;
733 : }
734 :
735 146641116 : for ( i = 0; i < L_frame; i++ )
736 : {
737 146070528 : exc_diffQ[i] *= scale_fact;
738 : }
739 : }
740 :
741 909389 : if ( GSC_noisy_speech && element_mode > IVAS_SCE && core_brate < ACELP_7k20 )
742 : {
743 1766814 : for ( i = 80; i < L_frame; i++ )
744 : {
745 1756832 : exc_diffQ[i] *= ( +0.0024f * (float) i + 1.192f );
746 : }
747 : }
748 :
749 909389 : Comp_and_apply_gain( exc_diffQ, Ener_per_bd_iQ, Ener_per_bd_yQ, last_bin, 0 );
750 :
751 909389 : if ( exc_wo_nf != NULL )
752 : {
753 885475 : Comp_and_apply_gain( exc_wo_nf, Ener_per_bd_iQ, Ener_per_bd_yQ, last_bin, 1 );
754 :
755 885475 : v_add( exc_dct_in, exc_wo_nf, exc_wo_nf, L_frame );
756 : }
757 :
758 : /*--------------------------------------------------------------------------------------*
759 : * add the correction layer to the LF bins,
760 : * and add the quantized pulses or the noise for the higher part of the spectrum
761 : * (non valuable temporal content already zeroed)
762 : * DC is Zeroed
763 : *--------------------------------------------------------------------------------------*/
764 :
765 909389 : v_add( exc_dct_in, exc_diffQ, exc_dct_in, L_frame );
766 :
767 909389 : if ( core_brate == ACELP_8k00 && bwidth != NB )
768 : {
769 2337 : if ( bwe_flag == 1 )
770 : {
771 1697 : last_bin -= 1;
772 1697 : src = &exc_diffQ[L_FRAME - 1];
773 1697 : dst = &exc_dct_in[MAX_Bin * 16 - 1];
774 1697 : end = &exc_diffQ[last_bin * 16 - 1];
775 :
776 164609 : while ( src > end )
777 : {
778 162912 : *src-- = *dst--;
779 : }
780 :
781 1697 : if ( ( bitallocation_exc[0] != 0 || bitallocation_exc[1] != 0 ) && core_brate == ACELP_8k00 )
782 : {
783 194 : exc_diffQ[160] = 0.0f;
784 : }
785 :
786 1697 : envelop_modify( exc_diffQ, seed_tcx, last_bin, Ener_per_bd_iQ );
787 :
788 1697 : mvr2r( &exc_diffQ[last_bin * 16], &exc_dct_in[last_bin * 16], L_FRAME - last_bin * 16 );
789 : }
790 :
791 2337 : if ( nb_subfr < 4 )
792 : {
793 31382 : for ( i = L_FRAME - 16; i < L_FRAME; i++ )
794 : {
795 29536 : exc_dct_in[i] *= ( 0.067f * i - 15.f );
796 : }
797 : }
798 :
799 2337 : if ( ener < 2 * ( *last_ener ) && ener > 0.5f * ( *last_ener ) )
800 : {
801 2161 : length_bin = 6;
802 2161 : if ( last_coder_type != AUDIO )
803 : {
804 640 : set_s( last_bitallocation_band, 0, 6 );
805 640 : mvr2r( &exc_dct_in[( 4 + length_bin ) * 16], &last_exc_dct_in[( 4 + length_bin ) * 16], length_bin * 16 );
806 : }
807 :
808 15127 : for ( i = 4; i < ( 4 + length_bin ); i++ )
809 : {
810 12966 : if ( !( bitallocation_band[i] == 0 && last_bitallocation_band[i - 4] == 0 ) )
811 : {
812 8609 : src = &exc_dct_in[( i + length_bin ) * 16];
813 8609 : dst = &last_exc_dct_in[( i + length_bin ) * 16];
814 146353 : for ( j = 0; j < 16; j++ )
815 : {
816 137744 : if ( fabs( *src ) > 3.0f * fabs( *dst ) )
817 : {
818 13866 : *src = ( *src > 0 ) ? (float) ( 0.5f * ( *src + fabs( *dst ) ) ) : (float) ( 0.5f * ( *src - fabs( *dst ) ) );
819 : }
820 123878 : else if ( fabs( *dst ) > 3.0f * fabs( *src ) )
821 : {
822 26119 : *src = ( *src > 0 ) ? (float) ( 0.7f * ( *src ) + 0.3f * fabs( *dst ) ) : (float) ( 0.7f * ( *src ) - 0.3f * fabs( *dst ) );
823 : }
824 137744 : src++;
825 137744 : dst++;
826 : }
827 : }
828 : }
829 : }
830 :
831 2337 : if ( bwe_flag == 1 )
832 : {
833 1697 : Decreas_freqPeak( lsf_new, exc_dct_in, 0.3f );
834 : }
835 : else
836 : {
837 640 : Decreas_freqPeak( lsf_new, exc_dct_in, 0.5f );
838 : }
839 : }
840 :
841 909389 : mvr2r( &exc_dct_in[64], &last_exc_dct_in[64], L_frame - 64 );
842 909389 : mvs2s( &bitallocation_band[4], last_bitallocation_band, 6 );
843 909389 : *last_ener = ener;
844 :
845 909389 : return;
846 : }
|