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 526776 : 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 526776 : int16_t NB_zer = NB_Qbins / 2;
67 :
68 526776 : if ( coder_type == INACTIVE )
69 : {
70 138064 : NB_zer = 2;
71 : }
72 :
73 : /*----------------------------------------------*
74 : * noise fill-in on unquantized subvector *
75 : * injected only from 1066Hz to 6400Hz. *
76 : *----------------------------------------------*/
77 :
78 2491656 : for ( k = Start_BIN; k < NB_Qbins + Start_BIN; k += NB_zer )
79 : {
80 1964880 : ftmp = 0.0;
81 10561872 : for ( i = k; i < k + NB_zer; i++ )
82 : {
83 8596992 : exc_diffQ[i] = (float) y_norm[i];
84 8596992 : ftmp += exc_diffQ[i] * exc_diffQ[i];
85 : }
86 :
87 1964880 : if ( ftmp < .5 )
88 : {
89 7711843 : for ( i = k; i < k + NB_zer; i++ )
90 : {
91 6267542 : 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 2850029 : for ( i = k; i < k + NB_zer; i++ )
98 : {
99 2329450 : own_random( seed_tcx );
100 : }
101 : }
102 : }
103 :
104 526776 : return;
105 : }
106 :
107 : /*-------------------------------------------------------------------*
108 : * EstimateNoiseLevel_inner()
109 : *
110 : * Estimate noise level from the power spectrum
111 : *-------------------------------------------------------------------*/
112 :
113 24470 : 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 24470 : noise_offset = 0.25f;
124 24470 : if ( bitrate > ACELP_24k40 )
125 : {
126 380 : noise_offset = .2f;
127 : }
128 24090 : else if ( bitrate >= ACELP_22k60 )
129 : {
130 1136 : noise_offset = .3f;
131 : }
132 22954 : else if ( bitrate >= ACELP_9k60 )
133 : {
134 11643 : noise_offset = 0.35f;
135 : }
136 : else
137 : {
138 11311 : noise_offset = .4f;
139 : }
140 :
141 24470 : set_f( noisepb + i_band, noise_offset, Mbands_gn - i_band );
142 :
143 146820 : for ( i = i_band; i < 5; i++ )
144 : {
145 122350 : if ( noisepb[i] > 0.2f )
146 : {
147 120450 : noisepb[i] = 0.2f;
148 : }
149 : }
150 :
151 24470 : return;
152 : }
153 :
154 :
155 : /*-------------------------------------------------------------------*
156 : * EstimateNoiseLevel()
157 : *
158 : *
159 : *-------------------------------------------------------------------*/
160 :
161 24470 : 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 24470 : i_band = 0;
177 :
178 24470 : if ( Diff_len < L_frame )
179 : {
180 24470 : EstimateNoiseLevel_inner( noisepb, bitrate, i_band, Mbands_gn );
181 :
182 24470 : if ( coder_type != INACTIVE )
183 : {
184 16613 : if ( ( bitrate == ACELP_8k00 && last_bin > 8 ) && bwidth != NB )
185 : {
186 0 : while ( Mbands_gn > i_band )
187 : {
188 0 : noisepb[i_band] *= 2.0f;
189 0 : i_band++;
190 : }
191 : }
192 : else
193 : {
194 155246 : while ( pit_band_idx > i_band )
195 : {
196 138633 : noisepb[i_band] /= 2.0f;
197 138633 : i_band++;
198 : }
199 : }
200 : }
201 : }
202 :
203 24470 : if ( ( coder_type == INACTIVE || noise_lev >= NOISE_LEVEL_SP3 ) && L_frame == L_FRAME )
204 : {
205 114411 : for ( i_band = 9; i_band < Mbands_gn; i_band++ )
206 : {
207 100099 : noisepb[i_band] *= 1.15f;
208 : }
209 : }
210 10158 : else if ( L_frame == L_FRAME16k )
211 : {
212 5184 : if ( bitrate == ACELP_13k20 )
213 : {
214 0 : set_f( noisepb, .45f, Mbands_gn );
215 : }
216 :
217 5184 : if ( coder_type == INACTIVE )
218 : {
219 98496 : for ( ; i_band < Mbands_gn; i_band++ )
220 : {
221 93312 : 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 24470 : return;
241 : }
242 :
243 :
244 : /*-------------------------------------------------------------------*
245 : * Apply_NoiseFill()
246 : *
247 : *
248 : *-------------------------------------------------------------------*/
249 :
250 32278 : 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 32278 : StartBin = 0;
262 32278 : NB_Qbins = 0;
263 :
264 559054 : for ( i_band = 0; i_band < Mbands_gn; i_band++ )
265 : {
266 526776 : StartBin += NB_Qbins;
267 526776 : NB_Qbins = freq_nsbin_per_band[i_band];
268 :
269 526776 : if ( Diff_len < L_FRAME )
270 : {
271 526776 : gs_noisf( StartBin, NB_Qbins, noisepb[i_band], exc_diffQ, exc_diffQ, seed_tcx, coder_type );
272 : }
273 : }
274 :
275 32278 : return;
276 : }
277 :
278 :
279 : /*-------------------------------------------------------------------*
280 : * freq_dnw_scaling()
281 : *
282 : *
283 : *-------------------------------------------------------------------*/
284 :
285 31117 : 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 31117 : sc_dyn = 1.0f;
298 31117 : start_sc = L_frame;
299 :
300 31117 : if ( core_brate <= ACELP_8k00 && coder_type == INACTIVE )
301 : {
302 108 : sc_dyn *= .15f;
303 108 : start_sc = 64;
304 : }
305 31009 : else if ( coder_type == INACTIVE )
306 : {
307 7834 : sc_dyn *= .25f;
308 7834 : start_sc = 80;
309 : }
310 : else
311 : {
312 23175 : sc_dyn = (float) ( NOISE_LEVEL_SP3 - noise_lev ) / 10.0f + 0.4f;
313 23175 : start_sc = 112 + ( NOISE_LEVEL_SP3 - noise_lev ) * 16;
314 :
315 23175 : if ( noise_lev == NOISE_LEVEL_SP0 )
316 : {
317 6560 : start_sc = L_FRAME;
318 : }
319 : }
320 :
321 31117 : if ( L_frame == L_FRAME16k && core_brate <= ACELP_24k40 )
322 : {
323 4804 : sc_dyn += 0.125f;
324 4804 : if ( sc_dyn > 1.0f )
325 : {
326 0 : sc_dyn = 1.0f;
327 : }
328 : }
329 :
330 4004653 : for ( i = start_sc; i < L_frame; i++ )
331 : {
332 3973536 : fy_norm[i] *= sc_dyn;
333 : }
334 :
335 31117 : if ( ( core_brate < ACELP_13k20 && cor_strong_limit == 0 ) || core_brate < ACELP_9k60 )
336 : {
337 2229157 : for ( i = 160; i < L_frame; i++ )
338 : {
339 2206176 : if ( fy_norm[i] > 1.0f )
340 : {
341 1203 : fy_norm[i] = 1.0f;
342 : }
343 :
344 2206176 : if ( fy_norm[i] < -1.0f )
345 : {
346 1119 : fy_norm[i] = -1.0f;
347 : }
348 : }
349 : }
350 8136 : else if ( core_brate < ACELP_22k60 )
351 : {
352 876892 : for ( i = 160; i < L_frame; i++ )
353 : {
354 870272 : if ( fy_norm[i] > 1.5f )
355 : {
356 1326 : fy_norm[i] = 1.5f;
357 : }
358 :
359 870272 : if ( fy_norm[i] < -1.5f )
360 : {
361 1287 : fy_norm[i] = -1.5f;
362 : }
363 : }
364 : }
365 :
366 31117 : return;
367 : }
368 :
369 :
370 : /*-------------------------------------------------------------------*
371 : * Decreas_freqPeak()
372 : *
373 : *
374 : *-------------------------------------------------------------------*/
375 :
376 93 : 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 93 : int16_t last_bin = 0;
384 93 : int16_t pos = 0;
385 : float *src;
386 : float avrg, max_val;
387 : float lsf_new_diff[M];
388 93 : lsf_new_diff[0] = 0; /* prevent unitialized value */
389 1395 : for ( j = 1; j < ( M - 1 ); j++ )
390 : {
391 1302 : lsf_new_diff[j] = lsf_new[j] - lsf_new[j - 1];
392 : }
393 :
394 93 : 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 93 : max_val = 0.001f;
400 9021 : for ( i = 160; i < L_FRAME; i++ )
401 : {
402 8928 : if ( fabs( exc_diffQ[i] ) > max_val )
403 : {
404 510 : max_val = (float) fabs( exc_diffQ[i] );
405 510 : pos = i;
406 : }
407 8928 : avrg += (float) fabs( exc_diffQ[i] );
408 : }
409 93 : avrg /= 96;
410 93 : last_bin = M - 1; /* When the search is false, should equate the end of the vector, not the beginning */
411 1070 : for ( i = 0; i < ( M - 1 ); i++ )
412 : {
413 1070 : if ( lsf_new[i] > 4000 )
414 : {
415 93 : last_bin = i;
416 93 : break;
417 : }
418 : }
419 :
420 418 : for ( i = last_bin; i < 14; i++ )
421 : {
422 325 : if ( lsf_new_diff[i] < thr_rat * lsf_new_diff[i - 1] )
423 : {
424 20 : src = &exc_diffQ[( i - 1 ) * 16];
425 60 : for ( j = 0; j < 2; j++ )
426 : {
427 680 : for ( k = 0; k < 16; k++ )
428 : {
429 640 : if ( fabs( *src ) > 2.0f * avrg )
430 : {
431 35 : *( src ) = ( *src > 0 ) ? (float) ( avrg * ( 2.0f - fabs( *src ) / max_val ) ) : (float) ( -avrg * ( 2.0f - fabs( *src ) / max_val ) );
432 : }
433 640 : src++;
434 : }
435 : }
436 : }
437 : }
438 :
439 93 : if ( fabs( exc_diffQ[pos] ) == max_val && max_val > 4.0f * avrg )
440 : {
441 140 : for ( i = pos - 1; i < pos + 2; i++ )
442 : {
443 105 : exc_diffQ[pos] *= 0.5f;
444 : }
445 : }
446 :
447 93 : return;
448 : }
449 :
450 :
451 : /*-------------------------------------------------------------------*
452 : * envelop_modify()
453 : *
454 : *
455 : *-------------------------------------------------------------------*/
456 :
457 52 : 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 52 : float weight = 1.0f;
467 :
468 52 : end_band = L_FRAME;
469 52 : Ener = 0.1f;
470 5044 : for ( i = last_bin * 16; i < end_band; i++ )
471 : {
472 4992 : Ener += exc_diffQ[i] * exc_diffQ[i];
473 : }
474 52 : Ener = (float) sqrt( ( end_band - last_bin * 16 ) / Ener );
475 :
476 52 : weight = 0.5f;
477 :
478 52 : src = &exc_diffQ[16 * last_bin];
479 260 : for ( i = last_bin; i < last_bin + 4; i++ )
480 : {
481 208 : Ener1 = (float) ( 0.4f * pow( 10, Ener_per_bd_iQ[i + 1] ) );
482 3536 : for ( j = 0; j < 16; j++ )
483 : {
484 3328 : *src = Ener1 * ( weight * ( *src ) * Ener + ( 1.0f - weight ) * own_random( seed_tcx ) / PCM16_TO_FLT_FAC );
485 3328 : src++;
486 : }
487 : }
488 :
489 52 : Ener1 = (float) ( 0.4f * pow( 10, Ener_per_bd_iQ[15] ) );
490 :
491 52 : src = &exc_diffQ[224];
492 1716 : for ( j = 0; j < 32; j++ )
493 : {
494 1664 : *src = Ener1 * ( weight * ( *src ) * Ener + ( 1.0f - weight ) * own_random( seed_tcx ) / PCM16_TO_FLT_FAC );
495 1664 : src++;
496 : }
497 :
498 52 : return;
499 : }
500 :
501 :
502 : /*-------------------------------------------------------------------*
503 : * highband_exc_dct_in()
504 : *
505 : *
506 : *-------------------------------------------------------------------*/
507 :
508 46605 : 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 46605 : 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 46605 : float ener = 0.0f;
545 46605 : int16_t length_bin, bwe_flag = 0;
546 :
547 326235 : for ( j = 10; j < MBANDS_GN; j++ )
548 : {
549 279630 : ener += (float) pow( 10, Ener_per_bd_iQ[j] );
550 : }
551 :
552 46605 : if ( core_brate == ACELP_8k00 && bwidth != NB )
553 : {
554 93 : if ( last_coder_type != AUDIO )
555 : {
556 41 : *last_ener = ener;
557 : }
558 :
559 93 : if ( ( last_bin > 8 || Diff_len != 0 ) && last_coder_type == AUDIO )
560 : {
561 52 : MAX_Bin = 10;
562 52 : bwe_flag = 1;
563 : }
564 : else
565 : {
566 41 : MAX_Bin = 15;
567 : }
568 :
569 93 : last_bin_tmp = last_bin;
570 93 : if ( last_bin < MAX_Bin )
571 : {
572 93 : last_bin = MAX_Bin;
573 : }
574 93 : last_bin += 1;
575 : }
576 : else
577 : {
578 46512 : if ( L_frame == L_FRAME16k )
579 : {
580 5294 : last_bin = MBANDS_GN16k;
581 : }
582 : else
583 : {
584 41218 : last_bin = MBANDS_GN;
585 : }
586 46512 : last_bin_tmp = last_bin;
587 : }
588 :
589 :
590 46605 : if ( bfi || core_brate < 6000 || ( core_brate < 8600 && coder_type == UNVOICED ) )
591 : {
592 15216 : set_f( noisepb, 0.4f, MBANDS_GN );
593 : }
594 31389 : else if ( GSC_IVAS_mode == 3 || ( GSC_IVAS_mode > 0 && GSC_noisy_speech == 1 ) )
595 : {
596 6919 : set_f( noisepb, 0.4f, MBANDS_GN16k );
597 : }
598 : else
599 : {
600 24470 : EstimateNoiseLevel( noisepb, core_brate, Diff_len, last_bin, coder_type, noise_lev, pit_band_idx, last_bin_tmp, bwidth, L_frame );
601 : }
602 :
603 46605 : if ( exc_wo_nf != NULL )
604 : {
605 45643 : mvr2r( exc_diffQ, exc_wo_nf, L_frame );
606 : }
607 :
608 46605 : if ( GSC_IVAS_mode == 0 && GSC_noisy_speech && !bfi && element_mode <= IVAS_SCE )
609 : {
610 15137 : set_f( noisepb, 0.1f, MBANDS_GN );
611 : }
612 :
613 46605 : if ( core_brate < 6000 && coder_type <= UNVOICED )
614 : {
615 3682039 : for ( i = 0; i < L_frame; i++ )
616 : {
617 3667712 : if ( exc_diffQ[i] == 0.0f )
618 : {
619 3636174 : exc_diffQ[i] += 2.0f * noisepb[0] * ( (float) own_random( seed_tcx ) / PCM16_TO_FLT_FAC );
620 : }
621 : }
622 : }
623 : else
624 : {
625 32278 : 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 46605 : if ( core_brate == ACELP_8k00 && bwidth != NB )
635 : {
636 93 : Ener_per_band_comp( exc_diffQ, Ener_per_bd_yQ, last_bin + 1, 0, L_frame );
637 : }
638 : else
639 : {
640 46512 : Ener_per_band_comp( exc_diffQ, Ener_per_bd_yQ, MBANDS_GN, 1, L_frame );
641 :
642 46512 : if ( nb_subfr < 4 && L_frame < L_FRAME16k )
643 : {
644 547961 : for ( i = L_FRAME - 16; i < L_FRAME; i++ )
645 : {
646 515728 : exc_diffQ[i] *= ( 0.067f * i - 15.0f );
647 : }
648 : }
649 : }
650 :
651 : /*--------------------------------------------------------------------------------------*
652 : * Check potential energy excitation overshoot
653 : *--------------------------------------------------------------------------------------*/
654 :
655 46605 : if ( bfi )
656 : {
657 962 : if ( GSC_noisy_speech == 0 && coder_type > UNVOICED ) /* Here coder_type == last_coder_type because of the bfi */
658 : {
659 11740 : for ( i = 0; i < last_bin; i++ )
660 : {
661 11048 : Ener_per_bd_iQ[i] = min( Ener_per_bd_iQ[i], ( lt_ener_per_band[i] - 0.0376f ) - Ener_per_bd_yQ[i] );
662 11048 : lt_ener_per_band[i] -= 0.0188f;
663 : }
664 732 : for ( ; i < MBANDS_GN; i++ )
665 : {
666 40 : Ener_per_bd_iQ[i] = min( Ener_per_bd_iQ[i], ( lt_ener_per_band[i] - 0.0376f ) );
667 40 : lt_ener_per_band[i] -= 0.0188f;
668 : }
669 : }
670 : else
671 : {
672 4794 : for ( i = 0; i < last_bin; i++ )
673 : {
674 4524 : Ener_per_bd_iQ[i] = min( Ener_per_bd_iQ[i], ( lt_ener_per_band[i] + 0.3f ) - Ener_per_bd_yQ[i] );
675 4524 : lt_ener_per_band[i] -= 0.0188f;
676 : }
677 270 : 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 46605 : if ( GSC_IVAS_mode >= 1 )
690 : {
691 14142 : float scale_factLF = 0.9f;
692 14142 : float scale_factHF = 0.9f;
693 :
694 14142 : if ( GSC_IVAS_mode == 1 && GSC_noisy_speech == 0 )
695 : {
696 6038 : scale_factHF = 0.8f;
697 : }
698 8104 : else if ( GSC_IVAS_mode == 2 || GSC_noisy_speech == 1 )
699 : {
700 1422 : scale_factHF = 0.71f;
701 : }
702 6682 : else if ( GSC_IVAS_mode == 3 )
703 : {
704 6682 : scale_factHF = 0.9f;
705 : }
706 1915150 : for ( i = 0; i < pit_band_idx * 16; i++ )
707 : {
708 1901008 : exc_diffQ[i] *= scale_factLF;
709 : }
710 1733486 : for ( ; i < L_frame; i++ )
711 : {
712 1719344 : exc_diffQ[i] *= scale_factHF;
713 : }
714 : }
715 32463 : else if ( GSC_noisy_speech )
716 : {
717 15703 : float scale_fact = 0.9f;
718 :
719 15703 : if ( element_mode == IVAS_CPE_TD )
720 : {
721 38 : if ( coder_type == INACTIVE )
722 : {
723 0 : scale_fact = 1.0f;
724 : }
725 : else
726 : {
727 38 : scale_fact = 0.95f;
728 : }
729 : }
730 15665 : else if ( element_mode > IVAS_SCE )
731 : {
732 424 : scale_fact = 0.71f;
733 : }
734 :
735 4035671 : for ( i = 0; i < L_frame; i++ )
736 : {
737 4019968 : exc_diffQ[i] *= scale_fact;
738 : }
739 : }
740 :
741 46605 : if ( GSC_noisy_speech && element_mode > IVAS_SCE && core_brate < ACELP_7k20 )
742 : {
743 6372 : for ( i = 80; i < L_frame; i++ )
744 : {
745 6336 : exc_diffQ[i] *= ( +0.0024f * (float) i + 1.192f );
746 : }
747 : }
748 :
749 46605 : Comp_and_apply_gain( exc_diffQ, Ener_per_bd_iQ, Ener_per_bd_yQ, last_bin, 0 );
750 :
751 46605 : if ( exc_wo_nf != NULL )
752 : {
753 45643 : Comp_and_apply_gain( exc_wo_nf, Ener_per_bd_iQ, Ener_per_bd_yQ, last_bin, 1 );
754 :
755 45643 : 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 46605 : v_add( exc_dct_in, exc_diffQ, exc_dct_in, L_frame );
766 :
767 46605 : if ( core_brate == ACELP_8k00 && bwidth != NB )
768 : {
769 93 : if ( bwe_flag == 1 )
770 : {
771 52 : last_bin -= 1;
772 52 : src = &exc_diffQ[L_FRAME - 1];
773 52 : dst = &exc_dct_in[MAX_Bin * 16 - 1];
774 52 : end = &exc_diffQ[last_bin * 16 - 1];
775 :
776 5044 : while ( src > end )
777 : {
778 4992 : *src-- = *dst--;
779 : }
780 :
781 52 : if ( ( bitallocation_exc[0] != 0 || bitallocation_exc[1] != 0 ) && core_brate == ACELP_8k00 )
782 : {
783 6 : exc_diffQ[160] = 0.0f;
784 : }
785 :
786 52 : envelop_modify( exc_diffQ, seed_tcx, last_bin, Ener_per_bd_iQ );
787 :
788 52 : mvr2r( &exc_diffQ[last_bin * 16], &exc_dct_in[last_bin * 16], L_FRAME - last_bin * 16 );
789 : }
790 :
791 93 : if ( nb_subfr < 4 )
792 : {
793 391 : for ( i = L_FRAME - 16; i < L_FRAME; i++ )
794 : {
795 368 : exc_dct_in[i] *= ( 0.067f * i - 15.f );
796 : }
797 : }
798 :
799 93 : if ( ener < 2 * ( *last_ener ) && ener > 0.5f * ( *last_ener ) )
800 : {
801 91 : length_bin = 6;
802 91 : if ( last_coder_type != AUDIO )
803 : {
804 41 : set_s( last_bitallocation_band, 0, 6 );
805 41 : mvr2r( &exc_dct_in[( 4 + length_bin ) * 16], &last_exc_dct_in[( 4 + length_bin ) * 16], length_bin * 16 );
806 : }
807 :
808 637 : for ( i = 4; i < ( 4 + length_bin ); i++ )
809 : {
810 546 : if ( !( bitallocation_band[i] == 0 && last_bitallocation_band[i - 4] == 0 ) )
811 : {
812 399 : src = &exc_dct_in[( i + length_bin ) * 16];
813 399 : dst = &last_exc_dct_in[( i + length_bin ) * 16];
814 6783 : for ( j = 0; j < 16; j++ )
815 : {
816 6384 : if ( fabs( *src ) > 3.0f * fabs( *dst ) )
817 : {
818 528 : *src = ( *src > 0 ) ? (float) ( 0.5f * ( *src + fabs( *dst ) ) ) : (float) ( 0.5f * ( *src - fabs( *dst ) ) );
819 : }
820 5856 : else if ( fabs( *dst ) > 3.0f * fabs( *src ) )
821 : {
822 879 : *src = ( *src > 0 ) ? (float) ( 0.7f * ( *src ) + 0.3f * fabs( *dst ) ) : (float) ( 0.7f * ( *src ) - 0.3f * fabs( *dst ) );
823 : }
824 6384 : src++;
825 6384 : dst++;
826 : }
827 : }
828 : }
829 : }
830 :
831 93 : if ( bwe_flag == 1 )
832 : {
833 52 : Decreas_freqPeak( lsf_new, exc_dct_in, 0.3f );
834 : }
835 : else
836 : {
837 41 : Decreas_freqPeak( lsf_new, exc_dct_in, 0.5f );
838 : }
839 : }
840 :
841 46605 : mvr2r( &exc_dct_in[64], &last_exc_dct_in[64], L_frame - 64 );
842 46605 : mvs2s( &bitallocation_band[4], last_bitallocation_band, 6 );
843 46605 : *last_ener = ener;
844 :
845 46605 : return;
846 : }
|