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 : * Local constants
50 : *-------------------------------------------------------------------*/
51 :
52 : #define MAX_SNR1 45.0f
53 : #define INV_MAX_SNR ( 1.f / ( MAX_SNR1 - 1.0f ) ) /* max. SNR considered for noise subtraction in voiced segments */
54 : #define MAX_SNR_SNR1 ( MAX_SNR1 * INV_MAX_SNR ) /* 45 * (1 / (MAX_SNR1-1)) */
55 :
56 :
57 : #define BIN_1KHZ ( int16_t )( 1000.f / BIN_16kdct )
58 : #define BIN_2KHZ ( int16_t )( 2000.f / BIN_16kdct )
59 : #define BIN_4KHZ ( int16_t )( 4000.f / BIN_16kdct )
60 :
61 : #define MAX_GN_R 0.2f
62 : #define ALPH 1.00f
63 : #define BET ( 1.925f - ALPH )
64 : #define MAXX 5
65 :
66 : /*-------------------------------------------------------------------*
67 : * Local function prototypes
68 : *-------------------------------------------------------------------*/
69 :
70 : static void spectrum_mod_dct( float data[], const float lf_E[], float lf_EO[], const float noiseE[], const float minGain, float lp_gbin[], const int16_t music_flag, int16_t min_band, const float MAX_GN, const int16_t max_band );
71 :
72 : static void analy_sp_dct( const float dct_in[], float dct_buf[], float *fr_bands, float *lf_E, float *etot );
73 :
74 : static void find_enr_dct( const float data[], float band[], float *ptE, float *Etot, const int16_t min_band, const int16_t max_band, float *Bin_E, const float bin_freq );
75 :
76 :
77 : /*------------------------------------------------------------------------*
78 : * LD_music_post_filter()
79 : *
80 : * Music post-filter
81 : *------------------------------------------------------------------------*/
82 :
83 164098 : void LD_music_post_filter(
84 : MUSIC_POSTFILT_HANDLE hMusicPF, /* i/o: LD music postfilter handle */
85 : const float dtc_in[], /* i : input synthesis */
86 : float dtc_out[], /* o : output synthesis */
87 : const int32_t core_brate, /* i : core bitrate */
88 : const int16_t coder_type, /* i : Coder type : -1 in case of IO */
89 : const int16_t Last_coder_type /* i : last Coder type */
90 : )
91 : {
92 : float DCT_buf[DCT_L_POST];
93 : float fr_bands[MBANDS_GN_LD];
94 : float lf_E[VOIC_BINS_HR];
95 : float etot, ftmp;
96 : int16_t i, j, k;
97 164098 : int16_t min_band = 0;
98 164098 : float local_min_gain = hMusicPF->LDm_enh_min_ns_gain;
99 164098 : int16_t music_flag2 = 0;
100 : float max_val;
101 : float max_ovf_2k, max_ovf_4k, max_ovf_6k;
102 : float min_g_2k, min_g_4k, min_g_6k;
103 : float m_ave, old_ftmp;
104 : float old_ftmp_1;
105 : float tmp_lfE[DCT_L_POST];
106 164098 : float MAX_GN = MAX_GN_R;
107 164098 : int16_t MAX_band = MBANDS_GN_LD;
108 :
109 : /*------------------------------------------------------------------------*
110 : * Frequency analysis
111 : *------------------------------------------------------------------------*/
112 :
113 164098 : analy_sp_dct( dtc_in, DCT_buf, fr_bands, lf_E, &etot );
114 :
115 : /*------------------------------------------------------------------------*
116 : * Find signal classification
117 : *------------------------------------------------------------------------*/
118 :
119 164098 : music_flag2 = stab_est( etot, hMusicPF->LDm_lt_diff_etot, &hMusicPF->LDm_mem_etot, &hMusicPF->LDm_nb_thr_3, &hMusicPF->LDm_nb_thr_1, hMusicPF->LDm_thres, &hMusicPF->LDm_last_music_flag, 1 );
120 :
121 164098 : if ( core_brate < ACELP_6k60 || Last_coder_type != AUDIO )
122 : {
123 : /* do not perform music improvement on SID frames */
124 9335 : music_flag2 = 0;
125 : }
126 :
127 164098 : if ( music_flag2 < 4 )
128 : {
129 121578 : hMusicPF->last_nonfull_music = 0;
130 : }
131 : else
132 : {
133 42520 : ( hMusicPF->last_nonfull_music )++;
134 : }
135 :
136 164098 : hMusicPF->last_nonfull_music = min( 51, hMusicPF->last_nonfull_music );
137 :
138 : /*------------------------------------------------------------------------*
139 : * Remapping of bands
140 : * Section to "remap" the minimal band and the minimum gain for our needs
141 : *------------------------------------------------------------------------*/
142 :
143 164098 : if ( music_flag2 > 3 )
144 : {
145 42520 : min_band = 2;
146 42520 : local_min_gain = 0.25119f;
147 : }
148 121578 : else if ( music_flag2 == 3 )
149 : {
150 7385 : min_band = 3;
151 7385 : local_min_gain = 0.25119f;
152 : }
153 114193 : else if ( music_flag2 == 2 )
154 : {
155 9599 : min_band = 4;
156 9599 : local_min_gain = 0.35481f;
157 : }
158 104594 : else if ( music_flag2 == 1 )
159 : {
160 24811 : min_band = 4;
161 24811 : local_min_gain = 0.50119f;
162 : }
163 :
164 164098 : min_band += 4;
165 :
166 164098 : MAX_GN = 0.1f;
167 164098 : if ( core_brate > ACELP_9k60 )
168 : {
169 : /* overshoot not allowed, since GSC already matches the energy */
170 66405 : MAX_GN = 0.0f;
171 : }
172 :
173 164098 : if ( coder_type == AUDIO )
174 : {
175 : /* with GSC we know for sure that we are in music */
176 159840 : min_band = min( min_band, 3 );
177 : }
178 :
179 : /*------------------------------------------------------------------------*
180 : * Approximation of the inter-harmonic noise level
181 : * - sort the bin energy
182 : * - compupte the average energy per band excluding the maximum energy bin
183 : *------------------------------------------------------------------------*/
184 :
185 164098 : j = 0;
186 3446058 : for ( i = 0; i < MBANDS_GN_LD; i++ )
187 : {
188 3281960 : m_ave = 0.0f;
189 3281960 : max_val = 0.0f;
190 :
191 108304680 : for ( k = j; k < mfreq_bindiv_LD[i] + j; k++ )
192 : {
193 105022720 : m_ave += lf_E[k];
194 105022720 : max_val = max( max_val, lf_E[k] );
195 : }
196 :
197 3281960 : m_ave -= max_val;
198 3281960 : m_ave /= mfreq_bindiv_LD[i] - 1;
199 :
200 3281960 : hMusicPF->LDm_bckr_noise[i] = m_ave * sc_qnoise[i];
201 3281960 : j += mfreq_bindiv_LD[i];
202 : }
203 :
204 164098 : i = maximum( lf_E, DCT_L_POST, &m_ave );
205 :
206 : /*------------------------------------------------------------------------*
207 : * - Normalisation of the energy vector between [0.72, 5], with the form of pow(x,4)
208 : * - Simple LP filtering along the frequency domain
209 : * - LT averaging with the past and in function of the stability factor
210 : *------------------------------------------------------------------------*/
211 :
212 164098 : m_ave = ALPH / lf_E[i];
213 :
214 164098 : ftmp = lf_E[0] * m_ave + BET;
215 164098 : ftmp *= ftmp;
216 164098 : ftmp *= ftmp;
217 164098 : ftmp *= ftmp;
218 164098 : ftmp = min( ftmp, MAXX );
219 164098 : old_ftmp = ftmp;
220 :
221 164098 : ftmp = lf_E[1] * m_ave + BET;
222 164098 : ftmp *= ftmp;
223 164098 : ftmp *= ftmp;
224 164098 : ftmp *= ftmp;
225 164098 : ftmp = min( ftmp, MAXX );
226 164098 : old_ftmp_1 = ftmp;
227 164098 : tmp_lfE[0] = 0.5f * old_ftmp + 0.5f * ftmp;
228 :
229 104858622 : for ( i = 1; i < DCT_L_POST - 1; i++ )
230 : {
231 104694524 : tmp_lfE[i] = 0.333f * old_ftmp + 0.333f * old_ftmp_1;
232 104694524 : old_ftmp = old_ftmp_1;
233 104694524 : ftmp = lf_E[i + 1] * m_ave + BET;
234 104694524 : ftmp *= ftmp;
235 104694524 : ftmp *= ftmp;
236 104694524 : ftmp *= ftmp;
237 104694524 : old_ftmp_1 = min( ftmp, MAXX );
238 104694524 : tmp_lfE[i] += 0.333f * old_ftmp_1;
239 : }
240 :
241 164098 : ftmp = lf_E[i] * m_ave + BET;
242 164098 : ftmp *= ftmp;
243 164098 : ftmp *= ftmp;
244 164098 : ftmp *= ftmp;
245 164098 : ftmp = min( ftmp, MAXX );
246 164098 : tmp_lfE[i] = 0.5f * old_ftmp + 0.5f * ftmp;
247 :
248 65803298 : for ( i = 0; i < BIN_4KHZ; i++ )
249 : {
250 65639200 : hMusicPF->filt_lfE[i] = tmp_lfE[i] * 0.05f + 0.95f * hMusicPF->filt_lfE[i];
251 : }
252 :
253 39547618 : for ( ; i < DCT_L_POST; i++ )
254 : {
255 39383520 : hMusicPF->filt_lfE[i] = tmp_lfE[i] * 0.15f + 0.85f * hMusicPF->filt_lfE[i];
256 : }
257 :
258 : /*------------------------------------------------------------------------*
259 : * Reduce inter-harmonic noise with SNR based method
260 : * Second stage of spectral shaping modification based on the pow(x,4) energy spectrum
261 : *------------------------------------------------------------------------*/
262 :
263 164098 : if ( coder_type == AUDIO )
264 : {
265 159840 : MAX_band = 16;
266 : }
267 :
268 164098 : spectrum_mod_dct( DCT_buf, lf_E, hMusicPF->LDm_enh_lf_EO, hMusicPF->LDm_bckr_noise, local_min_gain, hMusicPF->LDm_enh_lp_gbin, music_flag2, min_band, MAX_GN, MAX_band );
269 :
270 164098 : i = 0;
271 164098 : if ( music_flag2 >= 1 )
272 : {
273 8515815 : for ( i = 0; i < BIN_1KHZ; i++ )
274 : {
275 8431500 : ftmp = min( 1.0f, hMusicPF->filt_lfE[i] );
276 8431500 : DCT_buf[i] *= ftmp;
277 : }
278 : }
279 :
280 164098 : if ( hMusicPF->last_nonfull_music > 40 )
281 : {
282 12263 : max_ovf_2k = 1.25f;
283 12263 : max_ovf_4k = 1.5f;
284 12263 : max_ovf_6k = 1.5f;
285 :
286 12263 : min_g_2k = 0.0f;
287 12263 : min_g_4k = 0.0f;
288 12263 : min_g_6k = 0.0f;
289 :
290 12263 : if ( coder_type == AUDIO )
291 : {
292 12263 : max_ovf_2k = 1.0f;
293 12263 : max_ovf_4k = 1.1f;
294 12263 : max_ovf_6k = 1.25f;
295 :
296 12263 : min_g_2k = 0.75f;
297 12263 : min_g_4k = 0.5f;
298 12263 : min_g_6k = 0.5f;
299 :
300 12263 : if ( core_brate > ACELP_9k60 )
301 : {
302 3733 : max_ovf_4k = 1.0f;
303 3733 : max_ovf_6k = 1.15f;
304 :
305 3733 : min_g_2k = 0.925f;
306 3733 : min_g_4k = 0.825f;
307 3733 : min_g_6k = 0.75f;
308 : }
309 : }
310 0 : else if ( core_brate >= ACELP_12k65 )
311 : {
312 0 : max_ovf_2k = 1.0f;
313 0 : max_ovf_4k = 1.25f;
314 :
315 0 : if ( core_brate > ACELP_15k85 )
316 : {
317 0 : max_ovf_4k = 1.0f;
318 0 : max_ovf_6k = 1.25f;
319 :
320 0 : min_g_2k = 0.75f;
321 0 : min_g_4k = 0.5f;
322 0 : min_g_6k = 0.5f;
323 : }
324 : }
325 :
326 1238563 : for ( ; i < BIN_2KHZ; i++ )
327 : {
328 1226300 : ftmp = min( max_ovf_2k, hMusicPF->filt_lfE[i] );
329 1226300 : ftmp = max( min_g_2k, ftmp );
330 1226300 : DCT_buf[i] *= ftmp;
331 : }
332 :
333 2464863 : for ( ; i < BIN_4KHZ; i++ )
334 : {
335 2452600 : ftmp = min( max_ovf_4k, hMusicPF->filt_lfE[i] );
336 2452600 : ftmp = max( min_g_4k, ftmp );
337 2452600 : DCT_buf[i] *= ftmp;
338 : }
339 :
340 12263 : if ( coder_type != AUDIO || core_brate > ACELP_8k85 )
341 : {
342 : /* Do not modify HF when coded with GSC at LR, because the spectrum is just noise */
343 1143063 : for ( ; i < DCT_L_POST; i++ )
344 : {
345 1138320 : ftmp = min( max_ovf_6k, hMusicPF->filt_lfE[i] );
346 1138320 : ftmp = max( min_g_6k, ftmp );
347 1138320 : DCT_buf[i] *= ftmp;
348 : }
349 : }
350 : }
351 151835 : else if ( hMusicPF->last_nonfull_music > 25 )
352 : {
353 : /* When unsure on content type only slight clean-up allowed, no overshoot allowed */
354 1695494 : for ( ; i < DCT_L_POST; i++ )
355 : {
356 1692360 : ftmp = min( 1.f, hMusicPF->filt_lfE[i] );
357 1692360 : DCT_buf[i] *= ftmp;
358 : }
359 : }
360 :
361 : /* reconstruction of the enhanced synthesis */
362 164098 : mvr2r( DCT_buf, dtc_out, DCT_L_POST );
363 164098 : }
364 :
365 : /*---------------------------------------------------------------------------*
366 : * spectrum_mod_dct()
367 : *
368 : * spectrum enhancement according to the output of signal_type_clas()
369 : *---------------------------------------------------------------------------*/
370 :
371 164098 : static void spectrum_mod_dct(
372 : float data[], /* i/o: DCT spectrum */
373 : const float lf_E[], /* i : per bin E for first 46 bins (without DC) */
374 : float lf_EO[], /* i/o: old per bin E for previous half frame */
375 : const float noiseE[], /* i : per band background noise energy estimate */
376 : const float minGain, /* i : minimum suppression gain */
377 : float lp_gbin[], /* i/o: Smoothed suppression gain, per FFT bin */
378 : const int16_t music_flag, /* i : music ? 1:0 */
379 : int16_t min_band,
380 : const float MAX_GN,
381 : const int16_t MAX_band )
382 : {
383 : float maxNoise;
384 : float binE[VOIC_BINS_HR];
385 : float gain, minE;
386 : float freq;
387 : float slope;
388 : float inv_noise[MBANDS_GN_LD];
389 : float *pt_gbin, alpha, tmpN;
390 : int16_t i;
391 : float *pt;
392 : float tmp, shift;
393 : const float *pt2;
394 :
395 164098 : gain = 0.0f;
396 :
397 : /*-----------------------------------------------------------------*
398 : * Compute the inverse of noise
399 : *-----------------------------------------------------------------*/
400 :
401 3446058 : for ( i = 0; i <= MBANDS_GN_LD - 1; i++ )
402 : {
403 3281960 : inv_noise[i] = 1.0f / noiseE[i];
404 : }
405 :
406 : /*----------------------------------------------------------------------*
407 : * Perform noise reduction for 1 frames
408 : *----------------------------------------------------------------------*/
409 :
410 105186818 : for ( i = 0; i < VOIC_BINS_HR; i++ )
411 : {
412 105022720 : binE[i] = 0.3f * lf_EO[i] + 0.7f * lf_E[i];
413 : }
414 164098 : mvr2r( lf_E, lf_EO, VOIC_BINS_HR ); /* update */
415 :
416 : /*----------------------------------------------------------------------*
417 : * Find the maximum noise in a critical band
418 : *----------------------------------------------------------------------*/
419 :
420 164098 : maxNoise = 0.0f;
421 3446058 : for ( i = 0; i <= MBANDS_GN_LD - 1; i++ )
422 : {
423 3281960 : set_max( &maxNoise, noiseE[i] );
424 : }
425 :
426 : /* pointer initialization */
427 164098 : pt = &data[0];
428 :
429 : /*-----------------------------------------------------------------*
430 : * Initialization for active speech frames or VAD hangover frames,
431 : * (exclude Clean speech)
432 : *-----------------------------------------------------------------*/
433 :
434 164098 : if ( music_flag != 0 ) /* prevent subtraction on clean speech */
435 : {
436 84315 : if ( maxNoise <= 10.0f )
437 : {
438 66068 : minE = .5625f;
439 : }
440 : else
441 : {
442 18247 : minE = minGain * minGain;
443 : }
444 :
445 84315 : pt2 = binE;
446 84315 : freq = 0;
447 :
448 84315 : pt_gbin = lp_gbin;
449 84315 : tmp = INV_MAX_SNR;
450 84315 : slope = tmp - tmp * minE;
451 84315 : shift = MAX_SNR_SNR1 * minE - tmp;
452 342203 : for ( i = 0; i < min_band; i++ )
453 : {
454 2932605 : for ( ; freq <= mfreq_loc_LD[i]; freq += BIN_16kdct )
455 : {
456 2674717 : pt2++;
457 2674717 : pt++;
458 2674717 : *pt_gbin++ = 1.0f;
459 : }
460 : }
461 :
462 : /*-----------------------------------------------------------------*
463 : * Per Frequency MODE1_BIN processing
464 : * For highly voiced and highly pitched speech, use per bin
465 : * subtraction in low frequencies (maximum up to 3700 Hz,
466 : * first 17 critical bands)
467 : *-----------------------------------------------------------------*/
468 :
469 1179539 : for ( ; i < MAX_band /*MBANDS_GN_LD*/; i++ )
470 : {
471 :
472 1095224 : tmp = INV_MAX_SNR_tab[i];
473 1095224 : slope = tmp - tmp * minE;
474 1095224 : shift = MAX_SNR_SNR1_tab[i] * minE - tmp;
475 :
476 1095224 : tmpN = slope * inv_noise[i];
477 1095224 : tmp = 0.0f;
478 25393879 : while ( freq <= mfreq_loc_LD[i] )
479 : {
480 24298655 : gain = 1.0f;
481 :
482 24298655 : if ( noiseE[i] >= 0.5f ) /* Do not alter if noise E very low */
483 : {
484 12572954 : gain = tmpN * *pt2 + shift; /* limits: [x,y] = {[1, minE], [MAX_SNR1, 1]}, */
485 : }
486 :
487 24298655 : pt2++;
488 :
489 24298655 : if ( gain < minE )
490 : {
491 2182141 : gain = minE;
492 : }
493 :
494 24298655 : if ( gain > 1.0f + MAX_GN )
495 : {
496 1103238 : gain = 1.0f + MAX_GN;
497 : }
498 :
499 : /* the gain smoothing control: stronger lp filtering for lower gains */
500 24298655 : alpha = 1.0f - (float) sqrt( gain );
501 :
502 24298655 : *pt_gbin = gain + alpha * *pt_gbin;
503 24298655 : *pt++ *= *pt_gbin;
504 24298655 : freq += BIN_16kdct;
505 24298655 : pt_gbin++;
506 : }
507 : }
508 : }
509 : else
510 : {
511 79783 : freq = BIN_16kdct;
512 79783 : pt_gbin = lp_gbin;
513 1675443 : for ( i = 0; i < MBANDS_GN_LD; i++ )
514 : {
515 52576997 : for ( ; freq <= mfreq_loc_LD[i]; freq += BIN_16kdct )
516 : {
517 50981337 : *pt_gbin = 0.9f * *pt_gbin + 0.1f;
518 50981337 : pt_gbin++;
519 : }
520 : }
521 : }
522 :
523 164098 : return;
524 : }
525 :
526 : /*----------------------------------------------------------------------------------*
527 : * analy_sp_dct()
528 : *
529 : * Spectral analysis of the current synthesized frame
530 : *----------------------------------------------------------------------------------*/
531 :
532 164098 : static void analy_sp_dct(
533 : const float dct_in[], /* i : input DCT spectrum */
534 : float dct_buf[], /* i : output DCT spectrum */
535 : float *fr_bands, /* o : energy in critical frequency bands */
536 : float *lf_E, /* o : per bin E for first... */
537 : float *etot /* o : total input energy */
538 : )
539 : {
540 : float Bin_E[DCT_L_POST];
541 :
542 164098 : *etot = 0.0f;
543 164098 : mvr2r( dct_in, dct_buf, DCT_L_POST );
544 :
545 : /*-----------------------------------------------------------------*
546 : * Windowing
547 : * Compute spectrum
548 : * find energy per critical frequency band and total energy in dB
549 : *-----------------------------------------------------------------*/
550 :
551 164098 : find_enr_dct( dct_buf, fr_bands, lf_E, etot, 0, MBANDS_GN_LD, Bin_E, BIN_16kdct );
552 :
553 : /* find average log total energy over both half-frames */
554 164098 : *etot = 10.0f * (float) log10( *etot ) - 3.0103f;
555 :
556 164098 : return;
557 : }
558 :
559 : /*------------------------------------------------------------------------*
560 : * find_enr_dct)
561 : *
562 : * find input signal energy for each critical band and first 74 LF bins
563 : * The energy is normalized by the number of frequency bins in a channel
564 : *------------------------------------------------------------------------*/
565 :
566 164098 : void find_enr_dct(
567 : const float data[], /* i : fft result, for the format see fft_rel.c */
568 : float band[], /* o : per band energy */
569 : float *ptE, /* o : per bin energy for low frequencies */
570 : float *Etot, /* o : total energy */
571 : const int16_t min_band, /* i : minimum critical band */
572 : const int16_t max_band, /* i : maximum critical band */
573 : float *Bin_E, /* o : Per bin energy */
574 : const float bin_freq /* i : Number of frequency bins */
575 : )
576 : {
577 : int16_t i, cnt;
578 : float freq, tmp;
579 : const float *ptR;
580 :
581 164098 : ptR = &data[0]; /* pointer to first real coefficient */
582 164098 : freq = 0;
583 3446058 : for ( i = 0; i < max_band; i++ )
584 : {
585 3281960 : band[i] = 0.0f;
586 3281960 : cnt = 0;
587 108304680 : while ( freq <= mfreq_loc_LD[i] )
588 : {
589 : /* energy */
590 105022720 : *ptE = *ptR * *ptR;
591 :
592 : /* normalization - corresponds to FFT normalization by 2/L_FFT */
593 105022720 : *ptE *= 1.0f / ( DCT_L_POST );
594 :
595 105022720 : *Bin_E = *ptE;
596 105022720 : Bin_E++;
597 105022720 : band[i] += *ptE++;
598 105022720 : ptR++;
599 :
600 105022720 : freq += bin_freq;
601 105022720 : cnt++;
602 : }
603 :
604 : /* normalization per frequency bin */
605 3281960 : band[i] /= cnt;
606 :
607 3281960 : if ( band[i] < E_MIN )
608 : {
609 36129 : band[i] = E_MIN;
610 : }
611 : }
612 :
613 : /*-----------------------------------------------------------------*
614 : * Find the total energy over the input bandwidth
615 : *-----------------------------------------------------------------*/
616 :
617 164098 : tmp = *Etot;
618 2953764 : for ( i = min_band; i <= NB_LIMIT_BAND; i++ )
619 : {
620 : /* total channel energy */
621 2789666 : tmp += band[i];
622 : }
623 :
624 164098 : *Etot = tmp;
625 :
626 164098 : return;
627 : }
628 :
629 : /*------------------------------------------------------------------------*
630 : * Prep_music_postP()
631 : *
632 : * Performs the steps needed to do the music post processing
633 : *------------------------------------------------------------------------*/
634 :
635 164098 : void Prep_music_postP(
636 : float exc_buffer_in[], /* i/o: excitation buffer */
637 : float dct_buffer_out[], /* o : DCT output buffer */
638 : float filt_lfE[], /* i/o: long term spectrum energy */
639 : const int16_t last_core, /* i : last core */
640 : const float *pitch_buf, /* i : current frame pitch information */
641 : float *LDm_enh_lp_gbin /* o : smoothed suppression gain, per bin FFT */
642 : )
643 : {
644 : int16_t i;
645 : float *pt1;
646 : const float *pt2;
647 : int16_t s_pit, fr_pit;
648 : int16_t offset2;
649 :
650 164098 : offset2 = OFFSET2;
651 :
652 164098 : s_pit = (int16_t) ( pitch_buf[3] );
653 164098 : fr_pit = (int16_t) ( ( pitch_buf[3] - s_pit ) * 4.0f );
654 :
655 : /*------------------------------------------------------------*
656 : * Extrapolation of the last future part and windowing
657 : *------------------------------------------------------------*/
658 :
659 164098 : if ( last_core == HQ_CORE || last_core == TCX_20_CORE || last_core == TCX_10_CORE )
660 : {
661 13592 : set_f( filt_lfE, 1.0f, DCT_L_POST );
662 13592 : set_f( LDm_enh_lp_gbin, 1.0f, VOIC_BINS_HR );
663 13592 : pt1 = exc_buffer_in + offset2 - 1;
664 13592 : pt2 = pt1 + (int16_t) ( pitch_buf[0] + 0.5f );
665 2623256 : for ( i = 0; i < offset2; i++ )
666 : {
667 2609664 : *pt1 = *pt2;
668 2609664 : pt1--;
669 2609664 : pt2--;
670 : }
671 : }
672 :
673 164098 : pt1 = exc_buffer_in + DCT_L_POST - offset2;
674 164098 : pred_lt4( pt1, pt1, s_pit, fr_pit, offset2, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
675 :
676 164098 : pt2 = post_dct_wind;
677 31670914 : for ( i = 0; i < offset2; i++ )
678 : {
679 31506816 : *pt1 *= *pt2;
680 31506816 : pt1++;
681 31506816 : pt2++;
682 : }
683 :
684 164098 : pt1 = exc_buffer_in;
685 164098 : pt2--;
686 31670914 : for ( i = 0; i < offset2; i++ )
687 : {
688 31506816 : *pt1 *= *pt2;
689 31506816 : pt1++;
690 31506816 : pt2--;
691 : }
692 :
693 164098 : edct( exc_buffer_in, dct_buffer_out, DCT_L_POST, EVS_MONO );
694 :
695 164098 : return;
696 : }
697 :
698 : /*------------------------------------------------------------------------*
699 : * Post_music_postP()
700 : *
701 : * Going back from frequency to time domain from the enhanced spectrum
702 : * to retreive the aligned excitation and redo the synthesis
703 : *------------------------------------------------------------------------*/
704 :
705 164098 : void Post_music_postP(
706 : float dct_buffer_in[], /* i/o: excitation buffer */
707 : float exc_buffer_out[], /* o : DCT output buffer */
708 : float *exc2, /* i/o: Current excitation to be overwriten */
709 : const float *mem_tmp, /* i : previous frame synthesis memory */
710 : float *st_mem_syn2, /* i/o: current frame synthesis memory */
711 : const float *Aq, /* i : LPC filter coefficients */
712 : float *syn /* i/o: 12k8 synthesis */
713 :
714 : )
715 : {
716 164098 : edct( dct_buffer_in, exc_buffer_out, DCT_L_POST, EVS_MONO );
717 :
718 164098 : mvr2r( exc_buffer_out + OFFSET2, exc2, L_FRAME );
719 164098 : mvr2r( mem_tmp, st_mem_syn2, M );
720 :
721 164098 : syn_12k8( L_FRAME, Aq, exc2, syn, st_mem_syn2, 1 );
722 :
723 164098 : return;
724 : }
725 :
726 :
727 : /*-------------------------------------------------------------------*
728 : * music_postfilt_init()
729 : *
730 : * Initialize LD music postfilter state structure
731 : *-------------------------------------------------------------------*/
732 :
733 166027 : void music_postfilt_init(
734 : MUSIC_POSTFILT_HANDLE hMusicPF /* i/o: LD music postfilter handle */
735 : )
736 : {
737 : int16_t i;
738 :
739 166027 : set_f( hMusicPF->dct_post_old_exc, 0, DCT_L_POST - OFFSET2 );
740 :
741 166027 : hMusicPF->LDm_enh_min_ns_gain = (float) pow( 10.0f, -12 / 20.0f );
742 166027 : hMusicPF->LDm_last_music_flag = 0;
743 166027 : set_f( hMusicPF->LDm_lt_diff_etot, 0, MAX_LT );
744 166027 : hMusicPF->LDm_thres[0] = TH_0_MIN;
745 166027 : hMusicPF->LDm_thres[1] = TH_1_MIN;
746 166027 : hMusicPF->LDm_thres[2] = TH_2_MIN;
747 166027 : hMusicPF->LDm_thres[3] = TH_3_MIN;
748 166027 : hMusicPF->LDm_nb_thr_1 = 0;
749 166027 : hMusicPF->LDm_nb_thr_3 = 0;
750 166027 : hMusicPF->LDm_mem_etot = 0.0f;
751 :
752 106423307 : for ( i = 0; i < VOIC_BINS_HR; i++ )
753 : {
754 106257280 : hMusicPF->LDm_enh_lp_gbin[i] = 1.0f;
755 106257280 : hMusicPF->LDm_enh_lf_EO[i] = 0.01f;
756 : }
757 :
758 3486567 : for ( i = 0; i < MBANDS_GN_LD; i++ )
759 : {
760 3320540 : hMusicPF->LDm_bckr_noise[i] = E_MIN;
761 : }
762 :
763 166027 : set_f( hMusicPF->filt_lfE, 1.0f, DCT_L_POST );
764 166027 : hMusicPF->last_nonfull_music = 0;
765 :
766 166027 : return;
767 : }
|