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 <assert.h>
38 : #include <stdint.h>
39 : #include "options.h"
40 : #ifdef DEBUGGING
41 : #include "debug.h"
42 : #endif
43 : #include <math.h>
44 : #include "prot.h"
45 : #include "ivas_prot.h"
46 : #include "rom_com.h"
47 : #include "wmc_auto.h"
48 : #include "ivas_prot.h"
49 : #include "ivas_rom_dec.h"
50 :
51 : /*-------------------------------------------------------------------
52 : * Local constants
53 : *-------------------------------------------------------------------*/
54 :
55 : #define DELTA_MASKING_NOISE 1e-20f
56 : #define CNA_ACT_DN_LARGE_PARTITION 50 /* index of the first larger partition */
57 : #define ST_PERIODOG_FACT 0.9 /* short-term filter factor for periodogram */
58 : #define CNA_ACT_DN_FACT 0.7 /* downward updating factor for CNA during active frames */
59 : #define FIRST_CNA_NOISE_UPD_FRAMES 5 /* minimum number of CN initialization frames */
60 :
61 :
62 : /*-------------------------------------------------------------------
63 : * Local fucntions declarations
64 : *-------------------------------------------------------------------*/
65 :
66 : static void perform_noise_estimation_dec( const float *timeDomainInput, float *power_spectrum, HANDLE_FD_CNG_DEC hFdCngDec, const int16_t element_mode, const int16_t bwidth, const int16_t L_frame, const int16_t last_L_frame, const int32_t last_core_brate, const int16_t VAD );
67 :
68 :
69 : /*-------------------------------------------------------------------
70 : * createFdCngDec()
71 : *
72 : * Create an instance of type FD_CNG
73 : *-------------------------------------------------------------------*/
74 :
75 368768 : ivas_error createFdCngDec(
76 : HANDLE_FD_CNG_DEC *hFdCngDec )
77 : {
78 : HANDLE_FD_CNG_DEC hs;
79 : ivas_error error;
80 368768 : error = IVAS_ERR_OK;
81 :
82 : /* Set output to NULL in case of errors and early return */
83 368768 : *hFdCngDec = NULL;
84 :
85 : /* Allocate memory */
86 368768 : if ( ( hs = (HANDLE_FD_CNG_DEC) malloc( sizeof( FD_CNG_DEC ) ) ) == NULL )
87 : {
88 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for FD CNG DEC structure" );
89 : }
90 :
91 368768 : if ( ( error = createFdCngCom( &( hs->hFdCngCom ) ) ) != IVAS_ERR_OK )
92 : {
93 0 : return error;
94 : }
95 :
96 368768 : *hFdCngDec = hs;
97 :
98 368768 : return error;
99 : }
100 :
101 :
102 : /*-------------------------------------------------------------------
103 : * initFdCngDec()
104 : *
105 : * Initialize an instance of type FD_CNG
106 : *-------------------------------------------------------------------*/
107 :
108 368768 : void initFdCngDec(
109 : DEC_CORE_HANDLE st /* i/o: decoder state structure */
110 : )
111 : {
112 : HANDLE_FD_CNG_DEC hFdCngDec;
113 :
114 368768 : hFdCngDec = st->hFdCngDec;
115 :
116 : /* Initialize common */
117 368768 : initFdCngCom( hFdCngDec->hFdCngCom, st->cldfbSyn->scale );
118 :
119 : /* Set some counters and flags */
120 368768 : hFdCngDec->flag_dtx_mode = 0;
121 368768 : hFdCngDec->lp_noise = -20.f;
122 368768 : hFdCngDec->lp_speech = 25.f;
123 :
124 : /* Initialize noise estimation algorithm */
125 368768 : set_f( hFdCngDec->bandNoiseShape, 0.0f, FFTLEN2 );
126 368768 : set_f( hFdCngDec->partNoiseShape, 0.0f, NPART );
127 368768 : set_f( hFdCngDec->msPeriodog, 0.0f, NPART_SHAPING );
128 368768 : set_f( hFdCngDec->msAlpha, 0.0f, NPART_SHAPING );
129 368768 : set_f( hFdCngDec->msBminWin, 0.0f, NPART_SHAPING );
130 368768 : set_f( hFdCngDec->msBminSubWin, 0.0f, NPART_SHAPING );
131 368768 : set_f( hFdCngDec->msPsd, 0.0f, NPART_SHAPING );
132 368768 : set_f( hFdCngDec->msNoiseFloor, 0.0f, NPART_SHAPING );
133 368768 : set_f( hFdCngDec->msNoiseEst, 0.0f, NPART_SHAPING );
134 368768 : set_f( hFdCngDec->msMinBuf, FLT_MAX, MSNUMSUBFR * NPART_SHAPING );
135 368768 : set_f( hFdCngDec->msCurrentMin, FLT_MAX, NPART_SHAPING );
136 368768 : set_f( hFdCngDec->msCurrentMinOut, FLT_MAX, NPART_SHAPING );
137 368768 : set_f( hFdCngDec->msCurrentMinSubWindow, FLT_MAX, NPART_SHAPING );
138 368768 : set_s( hFdCngDec->msLocalMinFlag, 0, NPART_SHAPING );
139 368768 : set_s( hFdCngDec->msNewMinFlag, 0, NPART_SHAPING );
140 368768 : set_f( hFdCngDec->msPsdFirstMoment, 0.0f, NPART_SHAPING );
141 368768 : set_f( hFdCngDec->msPsdSecondMoment, 0.0f, NPART_SHAPING );
142 368768 : hFdCngDec->msPeriodogBufPtr = 0;
143 368768 : set_f( hFdCngDec->msPeriodogBuf, 0.0f, MSBUFLEN * NPART_SHAPING );
144 368768 : set_f( hFdCngDec->msLogPeriodog, 0.0f, NPART_SHAPING );
145 368768 : set_f( hFdCngDec->msLogNoiseEst, 0.0f, NPART_SHAPING );
146 368768 : set_f( hFdCngDec->psize_shaping, 0.0f, NPART_SHAPING );
147 368768 : hFdCngDec->nFFTpart_shaping = 0;
148 :
149 368768 : set_f( hFdCngDec->hFdCngCom->sidNoiseEstLp, 0.0f, NPART );
150 :
151 368768 : set_f( hFdCngDec->smoothed_psd, 0.0f, L_FRAME16k );
152 368768 : set_f( hFdCngDec->msPeriodog_ST, 0.0f, NPART_SHAPING );
153 :
154 368768 : hFdCngDec->ms_last_inactive_bwidth = NB;
155 368768 : hFdCngDec->ms_cnt_bw_up = 0;
156 :
157 368768 : hFdCngDec->cna_LR_LT = 0.5f;
158 368768 : hFdCngDec->cna_ILD_LT = 0.0f;
159 368768 : hFdCngDec->first_cna_noise_updated = 0;
160 368768 : hFdCngDec->first_cna_noise_update_cnt = 0;
161 368768 : hFdCngDec->cna_nbands = CNA_INIT_NBANDS;
162 368768 : mvs2s( cna_init_bands, hFdCngDec->cna_band_limits, CNA_INIT_NBANDS + 1 );
163 368768 : hFdCngDec->cna_act_fact = 1.0f;
164 368768 : hFdCngDec->cna_rescale_fact = 0.0f;
165 368768 : hFdCngDec->cna_seed = 5687;
166 368768 : set_zero( hFdCngDec->cna_cm, STEREO_DFT_BAND_MAX );
167 368768 : set_zero( hFdCngDec->cna_g_state, STEREO_DFT_BAND_MAX );
168 :
169 368768 : st->CNG_mode = -1;
170 368768 : mvr2r( st->lsp_old, st->lspCNG, M );
171 :
172 368768 : return;
173 : }
174 :
175 :
176 : /*-------------------------------------------------------------------
177 : * configureFdCngDec()
178 : *
179 : * Configure an instance of type FD_CNG
180 : *-------------------------------------------------------------------*/
181 :
182 17070999 : void configureFdCngDec(
183 : HANDLE_FD_CNG_DEC hFdCngDec, /* i/o: Contains the variables related to the FD-based CNG process */
184 : const int16_t bwidth,
185 : const int32_t total_brate,
186 : const int16_t L_frame,
187 : const int16_t last_L_frame,
188 : const int16_t element_mode )
189 : {
190 : int16_t j, stopBandFR;
191 17070999 : HANDLE_FD_CNG_COM hsCom = hFdCngDec->hFdCngCom;
192 :
193 17070999 : hsCom->CngBandwidth = bwidth;
194 17070999 : if ( hsCom->CngBandwidth == FB )
195 : {
196 9855028 : hsCom->CngBandwidth = SWB;
197 : }
198 17070999 : if ( total_brate != FRAME_NO_DATA && total_brate != SID_2k40 )
199 : {
200 17068438 : hsCom->CngBitrate = total_brate;
201 : }
202 2561 : else if ( hsCom->CngBitrate == -1 )
203 : {
204 : /* set minimum active CBR bitrate if CngBitrate is uninitialized */
205 0 : if ( element_mode > EVS_MONO )
206 : {
207 0 : hsCom->CngBitrate = IVAS_13k2;
208 : }
209 : else
210 : {
211 0 : hsCom->CngBitrate = ACELP_7k20;
212 : }
213 : }
214 :
215 : /* FD-CNG config for MDCT-Stereo is always the same (since for > 48 kbps only) */
216 : /* This may need adjustment in the future if 2TC DTX for some mode uses MDCT-Stereo DTX for lower bitrates too */
217 17070999 : if ( element_mode == IVAS_CPE_MDCT )
218 : {
219 14965836 : hsCom->CngBitrate = IVAS_48k;
220 : }
221 17070999 : hsCom->numSlots = 16;
222 :
223 : /* NB configuration */
224 17070999 : if ( bwidth == NB )
225 : {
226 169 : hsCom->FdCngSetup = FdCngSetup_nb;
227 169 : hsCom->numCoreBands = 16;
228 169 : hsCom->regularStopBand = 16;
229 : }
230 :
231 : /* WB configuration */
232 17070830 : else if ( bwidth == WB )
233 : {
234 : /* FFT 6.4kHz, no CLDFB */
235 110840 : if ( hsCom->CngBitrate <= ACELP_8k00 && L_frame == L_FRAME )
236 : {
237 7458 : hsCom->FdCngSetup = FdCngSetup_wb1;
238 7458 : hsCom->numCoreBands = 16;
239 7458 : hsCom->regularStopBand = 16;
240 : }
241 : /* FFT 6.4kHz, CLDFB 8.0kHz */
242 103382 : else if ( hsCom->CngBitrate <= ACELP_13k20 || L_frame == L_FRAME )
243 : {
244 17220 : hsCom->FdCngSetup = FdCngSetup_wb2;
245 17220 : hsCom->numCoreBands = 16;
246 17220 : hsCom->regularStopBand = 20;
247 17220 : if ( L_frame == L_FRAME16k )
248 : {
249 71 : hsCom->FdCngSetup = FdCngSetup_wb2;
250 71 : hsCom->numCoreBands = 20;
251 71 : hsCom->regularStopBand = 20;
252 71 : hsCom->FdCngSetup.fftlen = 640;
253 71 : hsCom->FdCngSetup.stopFFTbin = 256;
254 : }
255 : }
256 : /* FFT 8.0kHz, no CLDFB */
257 : else
258 : {
259 86162 : hsCom->FdCngSetup = FdCngSetup_wb3;
260 86162 : hsCom->numCoreBands = 20;
261 86162 : hsCom->regularStopBand = 20;
262 : }
263 : }
264 :
265 : /* SWB/FB configuration */
266 : else
267 : {
268 : /* FFT 6.4kHz, CLDFB 14kHz */
269 16959990 : if ( L_frame == L_FRAME )
270 : {
271 84200 : hsCom->FdCngSetup = FdCngSetup_swb1;
272 84200 : hsCom->numCoreBands = 16;
273 84200 : hsCom->regularStopBand = 35;
274 : }
275 : /* FFT 8.0kHz, CLDFB 16kHz */
276 : else
277 : {
278 16875790 : hsCom->FdCngSetup = FdCngSetup_swb2;
279 16875790 : hsCom->numCoreBands = 20;
280 16875790 : hsCom->regularStopBand = 40;
281 16875790 : if ( last_L_frame == L_FRAME && element_mode == IVAS_CPE_DFT )
282 : {
283 4229 : hsCom->regularStopBand = 35;
284 : }
285 : }
286 : }
287 :
288 :
289 17070999 : hsCom->fftlen = hsCom->FdCngSetup.fftlen;
290 17070999 : hsCom->stopFFTbin = hsCom->FdCngSetup.stopFFTbin;
291 :
292 : /* Configure the SID quantizer and the Comfort Noise Generator */
293 :
294 17070999 : hsCom->startBand = 2;
295 17070999 : hsCom->stopBand = hsCom->FdCngSetup.sidPartitions[hsCom->FdCngSetup.numPartitions - 1] + 1;
296 17070999 : initPartitions( hsCom->FdCngSetup.sidPartitions, hsCom->FdCngSetup.numPartitions, hsCom->startBand, hsCom->stopBand, hsCom->part, &hsCom->npart, hsCom->midband, hsCom->psize, hsCom->psize_inv, 0 );
297 17070999 : if ( hsCom->stopFFTbin == 160 )
298 : {
299 169 : hsCom->nFFTpart = 17;
300 : }
301 17070830 : else if ( hsCom->stopFFTbin == 256 )
302 : {
303 108878 : hsCom->nFFTpart = 20;
304 : }
305 : else
306 : {
307 16961952 : hsCom->nFFTpart = 21;
308 : }
309 17070999 : hsCom->nCLDFBpart = hsCom->npart - hsCom->nFFTpart;
310 68052389 : for ( j = 0; j < hsCom->nCLDFBpart; j++ )
311 : {
312 50981390 : hsCom->CLDFBpart[j] = hsCom->part[j + hsCom->nFFTpart] - ( hsCom->stopFFTbin - hsCom->startBand );
313 50981390 : hsCom->CLDFBpsize_inv[j] = hsCom->psize_inv[j + hsCom->nFFTpart];
314 : }
315 :
316 17070999 : stopBandFR = (int16_t) floor( 1000.f /*Hz*/ / 25.f /*Hz/Bin*/ );
317 17070999 : if ( stopBandFR > hsCom->stopFFTbin )
318 : {
319 0 : stopBandFR = hsCom->stopFFTbin;
320 : }
321 17070999 : initPartitions( hsCom->FdCngSetup.shapingPartitions, hsCom->FdCngSetup.numShapingPartitions, hsCom->startBand, hsCom->stopFFTbin, hFdCngDec->part_shaping, &hFdCngDec->npart_shaping, hFdCngDec->midband_shaping, hFdCngDec->psize_shaping, hFdCngDec->psize_inv_shaping, stopBandFR );
322 :
323 17070999 : hFdCngDec->nFFTpart_shaping = hFdCngDec->npart_shaping;
324 :
325 17070999 : switch ( hsCom->fftlen )
326 : {
327 108976 : case 512:
328 108976 : hsCom->fftSineTab = NULL;
329 108976 : hsCom->olapWinAna = olapWinAna512;
330 108976 : hsCom->olapWinSyn = olapWinSyn256;
331 108976 : break;
332 16962023 : case 640:
333 16962023 : hsCom->fftSineTab = fftSineTab640;
334 16962023 : hsCom->olapWinAna = olapWinAna640;
335 16962023 : hsCom->olapWinSyn = olapWinSyn320;
336 16962023 : break;
337 0 : default:
338 0 : assert( !"Unsupported FFT length for FD-based CNG" );
339 : break;
340 : }
341 17070999 : hsCom->frameSize = hsCom->fftlen >> 1;
342 :
343 17070999 : return;
344 : }
345 :
346 :
347 : /*-------------------------------------------------------------------
348 : * deleteFdCngDec()
349 : *
350 : * Delete the instance of type FD_CNG
351 : *-------------------------------------------------------------------*/
352 :
353 5331445 : void deleteFdCngDec(
354 : HANDLE_FD_CNG_DEC *hFdCngDec )
355 : {
356 5331445 : HANDLE_FD_CNG_DEC hsDec = *hFdCngDec;
357 :
358 5331445 : if ( hsDec != NULL )
359 : {
360 368768 : deleteFdCngCom( &( hsDec->hFdCngCom ) );
361 368768 : free( hsDec );
362 368768 : *hFdCngDec = NULL;
363 : }
364 :
365 5331445 : return;
366 : }
367 :
368 :
369 : /*-------------------------------------------------------------------
370 : * ApplyFdCng()
371 : *
372 : * Apply the CLDFB-based CNG at the decoder
373 : *-------------------------------------------------------------------*/
374 :
375 7978322 : void ApplyFdCng(
376 : float *timeDomainInput,
377 : float *powerSpectrum,
378 : float **realBuffer, /* i/o: Real part of the buffer */
379 : float **imagBuffer, /* i/o: Imaginary part of the buffer */
380 : Decoder_State *st,
381 : const int16_t concealWholeFrame,
382 : const int16_t is_music )
383 : {
384 7978322 : HANDLE_FD_CNG_DEC hFdCngDec = st->hFdCngDec;
385 7978322 : HANDLE_FD_CNG_COM hFdCngCom = hFdCngDec->hFdCngCom;
386 7978322 : float *cngNoiseLevel = hFdCngCom->cngNoiseLevel;
387 7978322 : float *sidNoiseEst = hFdCngCom->sidNoiseEst;
388 : int16_t j, k;
389 : float factor;
390 : float lsp_cng[M];
391 : int16_t L_frame, last_L_frame;
392 : int32_t sr_core;
393 :
394 7978322 : push_wmops( "ApplyFdCng" );
395 :
396 : /* limit L_frame and core Fs values for MDCT-Stereo modes which can have higher core sampling than 16kHz, but use a downsampled buffer */
397 7978322 : L_frame = min( st->L_frame, L_FRAME16k );
398 7978322 : last_L_frame = min( st->last_L_frame, L_FRAME16k );
399 7978322 : sr_core = min( st->sr_core, INT_FS_16k );
400 :
401 7978322 : if ( hFdCngCom->frame_type_previous == ACTIVE_FRAME )
402 : {
403 7878662 : hFdCngCom->inactive_frame_counter = 0;
404 : }
405 :
406 7978322 : if ( st->element_mode == IVAS_CPE_TD )
407 : {
408 39134 : hFdCngDec->flag_dtx_mode = hFdCngDec->flag_dtx_mode || st->first_CNG;
409 : }
410 :
411 7978322 : switch ( st->m_frame_type )
412 : {
413 :
414 7886192 : case ACTIVE_FRAME:
415 : /**************************
416 : * ACTIVE_FRAME at DECODER *
417 : **************************/
418 :
419 7886192 : hFdCngCom->inactive_frame_counter = 0;
420 7886192 : hFdCngCom->sid_frame_counter = 0;
421 : /* set noise estimation inactive during concealment, as no update with noise generated by concealment should be performed. */
422 : /* set noise estimation inactive when we have bit errors, as no update with noise generated by corrupt frame (biterror) should be performed. */
423 7886192 : if ( concealWholeFrame == 0 &&
424 6631352 : ( timeDomainInput == NULL ||
425 6631352 : ( *timeDomainInput<FLT_MAX && * timeDomainInput>( -FLT_MAX ) &&
426 6631352 : *( timeDomainInput + hFdCngCom->frameSize - 1 ) < FLT_MAX &&
427 6631352 : *( timeDomainInput + hFdCngCom->frameSize - 1 ) > ( -FLT_MAX ) ) ) &&
428 6995900 : ( ( ( ( st->element_mode != IVAS_CPE_TD && st->element_mode != IVAS_CPE_DFT && hFdCngDec->flag_dtx_mode ) || !st->VAD || ( st->ini_frame < 100 && st->is_ism_format ) ) &&
429 2968639 : !( st->cng_type == LP_CNG && hFdCngDec->flag_dtx_mode ) && ( is_music == 0 ) ) ||
430 4072831 : ( st->element_mode == IVAS_CPE_TD ) ) &&
431 2960116 : ( !st->BER_detect ) )
432 : {
433 : /* Perform noise estimation at the decoder */
434 2960116 : perform_noise_estimation_dec( timeDomainInput, powerSpectrum, hFdCngDec, st->element_mode, st->bwidth, L_frame, last_L_frame, st->last_core_brate, st->VAD );
435 :
436 2960116 : if ( st->element_mode != IVAS_CPE_TD && st->element_mode != IVAS_CPE_DFT )
437 : {
438 : /* Update the shaping parameters */
439 2860130 : scalebands( hFdCngDec->msNoiseEst, hFdCngDec->part_shaping, hFdCngDec->nFFTpart_shaping, hFdCngDec->midband_shaping, hFdCngDec->nFFTpart_shaping, hFdCngCom->stopFFTbin - hFdCngCom->startBand, hFdCngDec->bandNoiseShape, 1 );
440 : }
441 :
442 : /* Update CNG levels */
443 2960116 : if ( hFdCngDec->flag_dtx_mode && st->cng_type == FD_CNG )
444 : {
445 430050 : bandcombinepow( hFdCngDec->bandNoiseShape, hFdCngCom->stopFFTbin - hFdCngCom->startBand, hFdCngCom->part, hFdCngCom->nFFTpart, hFdCngCom->psize_inv, hFdCngDec->partNoiseShape ); /* This needs to be done only once per inactive phase */
446 :
447 430050 : j = 0;
448 9442839 : for ( k = 0; k < hFdCngCom->nFFTpart; k++ )
449 : {
450 9012789 : factor = ( hFdCngCom->sidNoiseEst[k] + DELTA ) / ( hFdCngDec->partNoiseShape[k] + DELTA );
451 144683793 : for ( ; j <= hFdCngCom->part[k]; j++ )
452 : {
453 135671004 : cngNoiseLevel[j] = hFdCngDec->bandNoiseShape[j] * factor;
454 : }
455 : }
456 : }
457 : else
458 : {
459 : /* This sets the new CNG levels until a SID update overwrites it */
460 2530066 : if ( !( st->element_mode == IVAS_CPE_TD ) || ( st->element_mode == IVAS_CPE_TD && !hFdCngDec->flag_dtx_mode && !st->VAD ) )
461 : {
462 2498895 : mvr2r( hFdCngDec->bandNoiseShape, cngNoiseLevel, hFdCngCom->stopFFTbin - hFdCngCom->startBand ); /* This sets the new CNG levels until a SID update overwrites it */
463 : }
464 : }
465 :
466 2960116 : if ( st->element_mode == IVAS_CPE_MDCT && timeDomainInput == NULL )
467 : {
468 364039 : st->hTcxDec->CngLevelBackgroundTrace_bfi = sqrtf( sum_f( cngNoiseLevel, hFdCngCom->stopFFTbin - hFdCngCom->startBand ) / NORM_MDCT_FACTOR );
469 : }
470 : else
471 : {
472 2596077 : st->hTcxDec->CngLevelBackgroundTrace_bfi = (float) sqrt( ( sum_f( cngNoiseLevel, hFdCngCom->stopFFTbin - hFdCngCom->startBand ) / 2 * hFdCngCom->fftlen ) / L_frame );
473 : }
474 2960116 : st->cngTDLevel = (float) sqrt( ( sum_f( cngNoiseLevel, hFdCngCom->stopFFTbin - hFdCngCom->startBand ) / 2 * hFdCngCom->fftlen ) / st->L_frame );
475 : }
476 4926076 : else if ( st->element_mode == IVAS_CPE_TD || st->element_mode == IVAS_CPE_DFT )
477 : {
478 1037557 : if ( hFdCngCom->active_frame_counter > 0 )
479 : {
480 : /* Perform noise estimation in active frames in the decoder for downward updates */
481 1029368 : perform_noise_estimation_dec( timeDomainInput, powerSpectrum, hFdCngDec, st->element_mode, st->bwidth, L_frame, last_L_frame, st->last_core_brate, st->VAD );
482 : }
483 : }
484 :
485 7886192 : if ( ( concealWholeFrame == 1 ) && ( st->nbLostCmpt == 1 ) )
486 : {
487 : /* update lsf cng estimate for concealment. Do that during concealment, in order to avoid addition clean channel complexity*/
488 :
489 : /* always set psychParameters for MDCT-Stereo ... */
490 365926 : if ( st->element_mode == IVAS_CPE_MDCT && st->hTonalMDCTConc != NULL )
491 : {
492 216822 : st->hTonalMDCTConc->psychParams = ( st->core == TCX_20_CORE ) ? &st->hTonalMDCTConc->psychParamsTCX20 : &st->hTonalMDCTConc->psychParamsTCX10;
493 : }
494 :
495 : /* ... but do actual computations only if sufficient energy in noise shape */
496 365926 : if ( sum_f( cngNoiseLevel + hFdCngCom->startBand, hFdCngCom->stopFFTbin - hFdCngCom->startBand ) > 0.01f )
497 : {
498 212011 : if ( st->element_mode == IVAS_CPE_MDCT && st->core != ACELP_CORE )
499 : {
500 61232 : TonalMdctConceal_whiten_noise_shape( st, L_frame, ON_FIRST_LOST_FRAME );
501 : }
502 150779 : else if ( st->element_mode != IVAS_CPE_MDCT || st->core == ACELP_CORE )
503 : {
504 150779 : lpc_from_spectrum( hFdCngCom, hFdCngCom->startBand, hFdCngCom->stopFFTbin, 0.f );
505 150779 : a2lsp_stab( hFdCngCom->A_cng, lsp_cng, st->lspold_cng );
506 150779 : mvr2r( lsp_cng, st->lspold_cng, M );
507 150779 : lsp2lsf( lsp_cng, st->lsf_cng, M, sr_core );
508 : }
509 212011 : st->plcBackgroundNoiseUpdated = 1;
510 : }
511 : }
512 7886192 : break;
513 :
514 12625 : case SID_FRAME:
515 12625 : hFdCngDec->flag_dtx_mode = 1;
516 : /* FALLTHRU */
517 :
518 92130 : case ZERO_FRAME:
519 :
520 92130 : if ( st != NULL && st->cng_type == LP_CNG )
521 : {
522 : /* Perform noise estimation on inactive phase at the decoder */
523 8774 : perform_noise_estimation_dec( timeDomainInput, powerSpectrum, hFdCngDec, st->element_mode, st->bwidth, L_frame, last_L_frame, st->last_core_brate, st->VAD );
524 :
525 8774 : if ( st->element_mode != IVAS_CPE_TD && st->element_mode != IVAS_CPE_DFT )
526 : {
527 : /* Update the shaping parameters */
528 2604 : scalebands( hFdCngDec->msNoiseEst, hFdCngDec->part_shaping, hFdCngDec->nFFTpart_shaping, hFdCngDec->midband_shaping, hFdCngDec->nFFTpart_shaping, hFdCngCom->stopFFTbin - hFdCngCom->startBand, hFdCngDec->bandNoiseShape, 1 );
529 : }
530 :
531 : /* This sets the new CNG levels until a SID update overwrites it */
532 8774 : mvr2r( hFdCngDec->bandNoiseShape, cngNoiseLevel, hFdCngCom->stopFFTbin - hFdCngCom->startBand ); /* This sets the new CNG levels until a SID update overwrites it */
533 :
534 8774 : st->cngTDLevel = (float) sqrt( ( sum_f( cngNoiseLevel, hFdCngCom->stopFFTbin - hFdCngCom->startBand ) / 2 * hFdCngCom->fftlen ) / L_frame );
535 8774 : break;
536 : }
537 :
538 83356 : hFdCngCom->inactive_frame_counter++;
539 :
540 : /*************************************
541 : * SID_FRAME or ZERO_FRAME at DECODER *
542 : *************************************/
543 :
544 : /* Detect first non-active frame */
545 83356 : if ( hFdCngCom->inactive_frame_counter == 1 )
546 : {
547 : /* Compute the fine spectral structure of the comfort noise shape using the decoder-side noise estimates */
548 2846 : bandcombinepow( hFdCngDec->bandNoiseShape, hFdCngCom->stopFFTbin - hFdCngCom->startBand, hFdCngCom->part, hFdCngCom->nFFTpart, hFdCngCom->psize_inv, hFdCngDec->partNoiseShape );
549 :
550 2846 : if ( st->element_mode == IVAS_CPE_DFT )
551 : {
552 1790 : mvr2r( st->hFdCngDec->hFdCngCom->sidNoiseEst, st->hFdCngDec->hFdCngCom->sidNoiseEstLp, NPART );
553 : }
554 : }
555 :
556 83356 : if ( st->m_frame_type == SID_FRAME )
557 : {
558 11161 : if ( hFdCngCom->msFrCnt_init_counter < hFdCngCom->msFrCnt_init_thresh )
559 : {
560 : /* At initialization, interpolate the bin/band-wise levels from the partition levels */
561 131 : scalebands( sidNoiseEst, hFdCngCom->part, hFdCngCom->npart, hFdCngCom->midband, hFdCngCom->nFFTpart, hFdCngCom->stopBand - hFdCngCom->startBand, cngNoiseLevel, 1 );
562 : }
563 : else
564 : {
565 11030 : if ( st->element_mode == IVAS_CPE_DFT )
566 : {
567 8628 : sidNoiseEst = hFdCngCom->sidNoiseEstLp;
568 : }
569 :
570 : /* Interpolate the CLDFB band levels from the SID (partition) levels */
571 11030 : if ( hFdCngCom->regularStopBand > hFdCngCom->numCoreBands )
572 : {
573 9080 : scalebands( sidNoiseEst, hFdCngCom->part, hFdCngCom->npart, hFdCngCom->midband, hFdCngCom->nFFTpart, hFdCngCom->stopBand - hFdCngCom->startBand, cngNoiseLevel, 0 );
574 : }
575 :
576 :
577 : /* Shape the SID noise levels in each FFT bin */
578 11030 : j = 0;
579 238123 : for ( k = 0; k < hFdCngCom->nFFTpart; k++ )
580 : {
581 227093 : factor = ( sidNoiseEst[k] + DELTA ) / ( hFdCngDec->partNoiseShape[k] + DELTA );
582 3455497 : for ( ; j <= hFdCngCom->part[k]; j++ )
583 : {
584 3228404 : cngNoiseLevel[j] = hFdCngDec->bandNoiseShape[j] * factor;
585 : }
586 : }
587 : }
588 : }
589 72195 : else if ( st->element_mode == IVAS_CPE_DFT )
590 : {
591 58234 : if ( !( hFdCngCom->msFrCnt_init_counter < hFdCngCom->msFrCnt_init_thresh ) )
592 : {
593 58234 : sidNoiseEst = hFdCngCom->sidNoiseEstLp;
594 58234 : j = 0;
595 1257911 : for ( k = 0; k < hFdCngCom->nFFTpart; k++ )
596 : {
597 1199677 : factor = ( sidNoiseEst[k] + DELTA ) / ( hFdCngDec->partNoiseShape[k] + DELTA );
598 18230921 : for ( ; j <= hFdCngCom->part[k]; j++ )
599 : {
600 17031244 : cngNoiseLevel[j] = hFdCngDec->bandNoiseShape[j] * factor;
601 : }
602 : }
603 : }
604 : }
605 83356 : if ( st->codec_mode == MODE2 )
606 : {
607 : /* Generate comfort noise during SID or zero frames */
608 626 : generate_comfort_noise_dec( realBuffer, imagBuffer, st, -1 );
609 : }
610 :
611 83356 : break;
612 :
613 0 : default:
614 0 : break;
615 : }
616 :
617 7978322 : pop_wmops();
618 :
619 7978322 : return;
620 : }
621 :
622 :
623 : /*-------------------------------------------------------------------
624 : * perform_noise_estimation_dec()
625 : *
626 : * Perform noise estimation at the decoder
627 : *-------------------------------------------------------------------*/
628 :
629 3998258 : static void perform_noise_estimation_dec(
630 : const float *timeDomainInput,
631 : float *power_spectrum,
632 : HANDLE_FD_CNG_DEC hFdCngDec, /* i/o: FD_CNG structure containing all buffers and variables */
633 : const int16_t element_mode, /* i : element mode */
634 : const int16_t bwidth, /* i : audio bandwidth */
635 : const int16_t L_frame, /* i : frame length at internal Fs */
636 : const int16_t last_L_frame, /* i : frame length of the last frame at internal Fs */
637 : const int32_t last_core_brate, /* i : previous frame core bitrate */
638 : const int16_t VAD /* i : VAD flag in the decoder */
639 : )
640 : {
641 : float *ptr_r;
642 : float *ptr_i;
643 3998258 : int16_t startBand = hFdCngDec->hFdCngCom->startBand;
644 3998258 : int16_t stopFFTbin = hFdCngDec->hFdCngCom->stopFFTbin;
645 3998258 : float *fftBuffer = hFdCngDec->hFdCngCom->fftBuffer;
646 3998258 : float *periodog = hFdCngDec->hFdCngCom->periodog;
647 3998258 : float *ptr_per = periodog;
648 3998258 : float *msPeriodog = hFdCngDec->msPeriodog;
649 3998258 : float *msNoiseEst = hFdCngDec->msNoiseEst;
650 :
651 3998258 : int16_t *part = hFdCngDec->part_shaping;
652 3998258 : int16_t npart = hFdCngDec->npart_shaping;
653 3998258 : int16_t nFFTpart = hFdCngDec->nFFTpart_shaping;
654 3998258 : float *psize_inv = hFdCngDec->psize_inv_shaping;
655 3998258 : float *psize = hFdCngDec->psize_shaping;
656 3998258 : float *msLogPeriodog = hFdCngDec->msLogPeriodog;
657 3998258 : float *msLogNoiseEst = hFdCngDec->msLogNoiseEst;
658 : int16_t i;
659 : float enr, enr_tot, enr_tot0, enr_ratio, alpha;
660 : int16_t p;
661 : float temp, ftemp, delta;
662 : float wght;
663 :
664 3998258 : if ( !( element_mode == IVAS_CPE_MDCT && power_spectrum != NULL ) )
665 : {
666 : /* Perform STFT analysis */
667 3634219 : AnalysisSTFT( timeDomainInput, fftBuffer, hFdCngDec->hFdCngCom );
668 : }
669 :
670 3998258 : if ( element_mode == IVAS_CPE_TD || element_mode == IVAS_CPE_DFT )
671 : {
672 : /* Calculate periodogram (squared magnitude in each FFT bin) */
673 1135524 : if ( startBand == 0 )
674 : {
675 0 : ( *ptr_per ) = fftBuffer[0] * fftBuffer[0];
676 0 : ptr_per++;
677 0 : ptr_r = fftBuffer + 2;
678 : }
679 : else
680 : {
681 1135524 : ptr_r = fftBuffer + 2 * startBand;
682 : }
683 :
684 1135524 : ptr_i = ptr_r + 1;
685 :
686 327096988 : for ( ; ptr_per < periodog + stopFFTbin - startBand; ptr_per++ )
687 : {
688 325961464 : ( *ptr_per ) = ( *ptr_r ) * ( *ptr_r ) + ( *ptr_i ) * ( *ptr_i );
689 325961464 : ptr_r += 2;
690 325961464 : ptr_i += 2;
691 : }
692 :
693 : /* Rescale to get energy/sample: it should be 2*(1/N)*(2/N), parseval relation with 1/N,*2 for nrg computed till Nyquist only, 2/N as windowed samples correspond to half a frame*/
694 1135524 : v_multc( periodog, 4.f / (float) ( hFdCngDec->hFdCngCom->fftlen * hFdCngDec->hFdCngCom->fftlen ), periodog, stopFFTbin - startBand );
695 :
696 : /* Combine bins of power spectrum into partitions */
697 1135524 : i = 0;
698 70951475 : for ( p = 0; p < npart; p++ )
699 : {
700 :
701 : /* calculate mean over all bins in power partition */
702 69815951 : temp = 0;
703 395777415 : for ( ; i <= part[p]; i++ )
704 : {
705 325961464 : temp += periodog[i];
706 : }
707 69815951 : msPeriodog[p] = temp * psize_inv[p];
708 : }
709 :
710 : /* compensate for the loss of variance - don't do when first noise update is not completed yet due to risk of msPeriodog[p] < 0 */
711 1135524 : if ( hFdCngDec->first_cna_noise_updated )
712 : {
713 678795 : i = 0;
714 42383060 : for ( p = 0; p < npart; p++ )
715 : {
716 : /* calculate variance over all bins in power partition */
717 41704265 : temp = 0;
718 238503795 : for ( ; i <= part[p]; i++ )
719 : {
720 196799530 : delta = periodog[i] - msPeriodog[p];
721 196799530 : temp += delta * delta;
722 : }
723 41704265 : temp *= psize_inv[p];
724 :
725 : /* compensate for the loss of variance */
726 41704265 : msPeriodog[p] = (float) ( msPeriodog[p] + sqrt( temp ) * rand_gauss( &ftemp, &hFdCngDec->cna_seed ) );
727 :
728 41704265 : if ( msPeriodog[p] < 1e-5f )
729 : {
730 2393590 : msPeriodog[p] = 1e-5f;
731 : }
732 : }
733 : }
734 :
735 : /* calculate total energy (short-term and long-term) */
736 1135524 : enr_tot = sum_f( msPeriodog, npart ) + EPSILON;
737 1135524 : enr_tot0 = sum_f( msNoiseEst, npart ) + EPSILON;
738 :
739 : /* update short-term periodogram on larger partitions */
740 14175275 : for ( p = CNA_ACT_DN_LARGE_PARTITION; p < npart; p++ )
741 : {
742 13039751 : if ( L_frame != last_L_frame || last_core_brate <= SID_2k40 )
743 : {
744 : /* core Fs has changed or last frame was SID/NO_DATA -> re-initialize short-term periodogram */
745 309428 : hFdCngDec->msPeriodog_ST[p] = msPeriodog[p];
746 : }
747 : else
748 : {
749 12730323 : hFdCngDec->msPeriodog_ST[p] = (float) ( ST_PERIODOG_FACT * hFdCngDec->msPeriodog_ST[p] + ( 1 - ST_PERIODOG_FACT ) * msPeriodog[p] );
750 : }
751 : }
752 :
753 : /* core Fs has changed -> partitions have changed -> re-calculate long-term periodogram */
754 : /* part L_FRAME16k L_FRAME */
755 : /* ... */
756 : /* [55] 146 146 */
757 : /* [56] 174 160 */
758 : /* [57] 210 174 */
759 : /* [58] 254 190 */
760 : /* [59] 306 210 */
761 : /* [60] 317 230 */
762 : /* [61] 253 */
763 :
764 1135524 : if ( last_L_frame == L_FRAME16k && L_frame == L_FRAME )
765 : {
766 16507 : msNoiseEst[61] = msNoiseEst[58];
767 16507 : msNoiseEst[60] = min( msNoiseEst[58], msNoiseEst[57] );
768 16507 : msNoiseEst[59] = msNoiseEst[57];
769 16507 : msNoiseEst[58] = msNoiseEst[56];
770 16507 : msNoiseEst[57] = msNoiseEst[56];
771 16507 : msNoiseEst[56] = min( msNoiseEst[56], msNoiseEst[55] );
772 : }
773 1119017 : else if ( last_L_frame == L_FRAME && L_frame == L_FRAME16k )
774 : {
775 3987 : msNoiseEst[56] = min( msNoiseEst[56], msNoiseEst[57] );
776 3987 : msNoiseEst[57] = min( msNoiseEst[58], msNoiseEst[59] );
777 3987 : msNoiseEst[58] = min( msNoiseEst[60], msNoiseEst[61] );
778 3987 : msNoiseEst[59] = 0.0f;
779 3987 : msNoiseEst[60] = 0.0f;
780 3987 : msNoiseEst[61] = 0.0f;
781 :
782 3987 : hFdCngDec->ms_cnt_bw_up = FIRST_CNA_NOISE_UPD_FRAMES;
783 : }
784 :
785 : /* Smooth with IIR filter */
786 1135524 : if ( !hFdCngDec->first_cna_noise_updated )
787 : {
788 456729 : if ( !VAD )
789 : {
790 : /* background noise update with moving average */
791 7003 : alpha = 1.0f / ( hFdCngDec->first_cna_noise_update_cnt + 1 );
792 437512 : for ( p = 0; p < npart; p++ )
793 : {
794 430509 : msNoiseEst[p] = ( 1 - alpha ) * msNoiseEst[p] + alpha * msPeriodog[p];
795 : }
796 :
797 : /* check, if we reached the required number of first CNA noise update frames */
798 7003 : if ( hFdCngDec->first_cna_noise_update_cnt < FIRST_CNA_NOISE_UPD_FRAMES - 1 )
799 : {
800 6086 : hFdCngDec->first_cna_noise_update_cnt++;
801 : }
802 : else
803 : {
804 917 : hFdCngDec->first_cna_noise_updated = 1;
805 917 : if ( hFdCngDec->hFdCngCom->msFrCnt_init_counter == 0 )
806 : {
807 228 : hFdCngDec->hFdCngCom->msFrCnt_init_counter = 1;
808 : }
809 : }
810 : }
811 : else
812 : {
813 449726 : hFdCngDec->first_cna_noise_update_cnt = 0;
814 : }
815 : }
816 : else
817 : {
818 678795 : hFdCngDec->hFdCngCom->msFrCnt_init_counter = 1;
819 678795 : if ( VAD )
820 : {
821 : /* no updates during active frames except for significant energy drops */
822 610197 : enr_ratio = enr_tot / enr_tot0;
823 610197 : if ( enr_ratio < 0.5f )
824 : {
825 : /* total energy significantly decreases during active frames -> downward update */
826 5693 : wght = lin_interp( enr_ratio, 0.0f, 0.8f, 0.5f, 0.95f, 1 );
827 355608 : for ( p = 0; p < npart; p++ )
828 : {
829 349915 : if ( msPeriodog[p] < msNoiseEst[p] )
830 : {
831 257184 : msNoiseEst[p] = wght * msNoiseEst[p] + ( 1 - wght ) * msPeriodog[p];
832 : }
833 : }
834 : }
835 : else
836 : {
837 : /* energy significantly decreases in one of the larger partitions during active frames -> downward update */
838 7522527 : for ( p = CNA_ACT_DN_LARGE_PARTITION; p < npart; p++ )
839 : {
840 6918023 : if ( hFdCngDec->msPeriodog_ST[p] < msNoiseEst[p] )
841 : {
842 97349 : msNoiseEst[p] = (float) ( CNA_ACT_DN_FACT * msNoiseEst[p] + ( 1 - CNA_ACT_DN_FACT ) * hFdCngDec->msPeriodog_ST[p] );
843 : }
844 : }
845 : }
846 : }
847 : else
848 : {
849 :
850 68598 : if ( bwidth >= WB && hFdCngDec->ms_last_inactive_bwidth == NB )
851 : {
852 : /* bandwidth increased -> set counter for fast initilization */
853 254 : hFdCngDec->ms_cnt_bw_up = FIRST_CNA_NOISE_UPD_FRAMES;
854 : }
855 68598 : hFdCngDec->ms_last_inactive_bwidth = bwidth;
856 : /* update background noise during inactive frames */
857 68598 : ptr_per = msNoiseEst;
858 4279725 : for ( p = 0; p < npart; p++ )
859 : {
860 4211127 : enr = msPeriodog[p];
861 4211127 : alpha = 0.95f;
862 : /* bandwidth increased -> do fast re-initilization */
863 4211127 : if ( hFdCngDec->ms_cnt_bw_up > 0 && p > 55 )
864 : {
865 13672 : alpha = 1.0f / ( hFdCngDec->ms_cnt_bw_up + 1 );
866 : }
867 4197455 : else if ( enr < *ptr_per && part[p] == 1 )
868 : {
869 : /* faster downward update for single-bin partitions */
870 19699 : alpha = 0.8f;
871 : }
872 4177756 : else if ( enr > 2.0f * ( *ptr_per ) )
873 : {
874 : /* prevent abrupt upward updates */
875 1555490 : enr = 2.0f * ( *ptr_per );
876 : }
877 :
878 : /* IIR smoothing */
879 4211127 : *ptr_per *= alpha;
880 4211127 : *ptr_per += ( 1 - alpha ) * enr;
881 4211127 : ptr_per++;
882 : }
883 :
884 68598 : if ( hFdCngDec->ms_cnt_bw_up > 0 )
885 : {
886 2555 : hFdCngDec->ms_cnt_bw_up--;
887 : }
888 : }
889 : }
890 :
891 1135524 : mvr2r( msNoiseEst, hFdCngDec->msPsd, npart );
892 :
893 : /* Expand partitions into bins of power spectrum */
894 1135524 : scalebands( msNoiseEst, part, nFFTpart, hFdCngDec->midband_shaping, nFFTpart, stopFFTbin - startBand, hFdCngDec->bandNoiseShape, 1 );
895 :
896 1135524 : mvr2r( hFdCngDec->bandNoiseShape, &hFdCngDec->smoothed_psd[startBand], stopFFTbin - startBand );
897 1135524 : set_zero( &hFdCngDec->smoothed_psd[stopFFTbin], L_FRAME16k - stopFFTbin );
898 : }
899 : else
900 : {
901 2862734 : if ( element_mode == IVAS_CPE_MDCT && power_spectrum != NULL )
902 : {
903 : /* use power spectrum calculated in the MDCT-domain instead of calculating new power spectrum */
904 364039 : periodog = power_spectrum;
905 : }
906 : else
907 : {
908 : /* Compute the squared magnitude in each FFT bin */
909 2498695 : if ( startBand == 0 )
910 : {
911 0 : ( *ptr_per ) = fftBuffer[0] * fftBuffer[0]; /* DC component */
912 0 : ptr_per++;
913 0 : ptr_r = fftBuffer + 2;
914 : }
915 : else
916 : {
917 2498695 : ptr_r = fftBuffer + 2 * startBand;
918 : }
919 :
920 2498695 : ptr_i = ptr_r + 1;
921 :
922 732702969 : for ( ; ptr_per < periodog + stopFFTbin - startBand; ptr_per++ )
923 : {
924 730204274 : ( *ptr_per ) = ( *ptr_r ) * ( *ptr_r ) + ( *ptr_i ) * ( *ptr_i );
925 730204274 : ptr_r += 2;
926 730204274 : ptr_i += 2;
927 : }
928 : /* Nyquist frequency is discarded */
929 :
930 : /* Rescale to get energy/sample: it should be 2*(1/N)*(2/N), parseval relation with 1/N,*2 for nrg computed till Nyquist only, 2/N as windowed samples correspond to half a frame*/
931 2498695 : v_multc( periodog, 4.f / (float) ( hFdCngDec->hFdCngCom->fftlen * hFdCngDec->hFdCngCom->fftlen ), periodog, stopFFTbin - startBand );
932 : }
933 :
934 : /* Adjust to the desired frequency resolution by averaging over spectral partitions for SID transmission */
935 2862734 : bandcombinepow( periodog, stopFFTbin - startBand, part, npart, psize_inv, msPeriodog );
936 :
937 : /* Compress MS inputs */
938 2862734 : compress_range( msPeriodog, msLogPeriodog, npart );
939 :
940 : /* Call the minimum statistics routine for noise estimation */
941 2862734 : minimum_statistics( npart, nFFTpart, psize, msLogPeriodog, hFdCngDec->msNoiseFloor, msLogNoiseEst, hFdCngDec->msAlpha, hFdCngDec->msPsd, hFdCngDec->msPsdFirstMoment, hFdCngDec->msPsdSecondMoment, hFdCngDec->msMinBuf, hFdCngDec->msBminWin, hFdCngDec->msBminSubWin, hFdCngDec->msCurrentMin, hFdCngDec->msCurrentMinOut, hFdCngDec->msCurrentMinSubWindow, hFdCngDec->msLocalMinFlag, hFdCngDec->msNewMinFlag, hFdCngDec->msPeriodogBuf, &( hFdCngDec->msPeriodogBufPtr ), hFdCngDec->hFdCngCom,
942 : DEC, element_mode );
943 :
944 : /* Expand MS outputs */
945 2862734 : expand_range( msLogNoiseEst, msNoiseEst, npart );
946 : }
947 :
948 3998258 : return;
949 : }
950 :
951 :
952 : /*-------------------------------------------------------------------
953 : * FdCng_decodeSID()
954 : *
955 : * Decode the FD-CNG bitstream
956 : *-------------------------------------------------------------------*/
957 :
958 12547 : void FdCng_decodeSID(
959 : Decoder_State *st /* i/o: decoder state structure */
960 : )
961 : {
962 : int16_t N;
963 : float *sidNoiseEst;
964 : float gain;
965 : int16_t i, index;
966 : float v[32];
967 : int16_t indices[32];
968 : HANDLE_FD_CNG_COM hFdCngCom;
969 : float *invTrfMatrix;
970 : float tmpRAM[FDCNG_VQ_MAX_LEN][FDCNG_VQ_DCT_MAXTRUNC];
971 :
972 12547 : const float gain_q_offset = ( st->element_mode == EVS_MONO ) ? GAIN_Q_OFFSET_EVS : GAIN_Q_OFFSET_IVAS;
973 :
974 12547 : invTrfMatrix = (float *) tmpRAM;
975 :
976 12547 : hFdCngCom = ( st->hFdCngDec )->hFdCngCom;
977 :
978 12547 : sidNoiseEst = hFdCngCom->sidNoiseEst;
979 :
980 12547 : N = hFdCngCom->npart;
981 12547 : gain = 0.0f;
982 12547 : hFdCngCom->sid_frame_counter++;
983 :
984 : /* Read bitstream */
985 87829 : for ( i = 0; i < FD_CNG_stages_37bits; i++ )
986 : {
987 75282 : indices[i] = get_next_indice( st, bits_37bits[i] );
988 : }
989 :
990 12547 : index = get_next_indice( st, 7 );
991 :
992 : /* MSVQ decoder */
993 :
994 12547 : if ( st->element_mode != EVS_MONO )
995 : {
996 12334 : create_IDCT_N_Matrix( invTrfMatrix, FDCNG_VQ_MAX_LEN, FDCNG_VQ_DCT_MAXTRUNC, sizeof( tmpRAM ) / ( sizeof( float ) ) );
997 12334 : msvq_dec( cdk_37bits_ivas, NULL, NULL, FD_CNG_stages_37bits, N, FD_CNG_maxN_37bits, indices, 1, invTrfMatrix, v, NULL );
998 : }
999 : else
1000 : { /* Legacy EVS_MONO MSVQ tables */
1001 213 : msvq_dec( cdk_37bits, NULL, NULL, FD_CNG_stages_37bits, N, FD_CNG_maxN_37bits, indices, 0, NULL, v, NULL );
1002 : }
1003 :
1004 :
1005 : /* Decode gain */
1006 12547 : gain = ( (float) index - gain_q_offset ) / 1.5f;
1007 :
1008 : /* Apply gain and undo log */
1009 304975 : for ( i = 0; i < N; i++ )
1010 : {
1011 292428 : sidNoiseEst[i] = (float) pow( 10.f, ( v[i] + gain ) / 10.f );
1012 : }
1013 :
1014 : /* NB last band energy compensation */
1015 :
1016 12547 : if ( hFdCngCom->CngBandwidth == NB )
1017 : {
1018 117 : sidNoiseEst[N - 1] *= NB_LAST_BAND_SCALE;
1019 : }
1020 :
1021 12547 : if ( hFdCngCom->CngBandwidth == SWB && hFdCngCom->CngBitrate <= ACELP_13k20 )
1022 : {
1023 2954 : sidNoiseEst[N - 1] *= SWB_13k2_LAST_BAND_SCALE;
1024 : }
1025 :
1026 12547 : scalebands( sidNoiseEst, hFdCngCom->part, hFdCngCom->npart, hFdCngCom->midband, hFdCngCom->nFFTpart, hFdCngCom->stopBand - hFdCngCom->startBand, hFdCngCom->cngNoiseLevel, 1 );
1027 :
1028 12547 : lpc_from_spectrum( hFdCngCom, hFdCngCom->startBand, hFdCngCom->stopFFTbin, st->preemph_fac );
1029 :
1030 12547 : return;
1031 : }
1032 :
1033 :
1034 : /*-------------------------------------------------------------------
1035 : * noisy_speech_detection()
1036 : *
1037 : *
1038 : *-------------------------------------------------------------------*/
1039 :
1040 12821531 : void noisy_speech_detection(
1041 : HANDLE_FD_CNG_DEC hFdCngDec, /* i/o: FD_CNG structure */
1042 : const int16_t vad, /* i : VAD flag */
1043 : const float syn[] /* i : input time-domain frame */
1044 : )
1045 : {
1046 : float tmp;
1047 :
1048 12821531 : if ( vad == 0 )
1049 : {
1050 1063182 : tmp = dotp( hFdCngDec->msNoiseEst, hFdCngDec->psize_shaping, hFdCngDec->nFFTpart_shaping );
1051 1063182 : hFdCngDec->lp_noise = 0.995f * hFdCngDec->lp_noise + 0.005f * 10.f * (float) log10( tmp + DELTA );
1052 : }
1053 : else
1054 : {
1055 11758349 : tmp = dotp( syn, syn, hFdCngDec->hFdCngCom->frameSize ) * 2.f / hFdCngDec->hFdCngCom->frameSize;
1056 11758349 : hFdCngDec->lp_speech = 0.995f * hFdCngDec->lp_speech + 0.005f * 10.f * (float) log10( tmp + DELTA );
1057 : }
1058 :
1059 12821531 : tmp = hFdCngDec->lp_speech - 45.f;
1060 12821531 : if ( hFdCngDec->lp_noise < tmp )
1061 : {
1062 6409792 : hFdCngDec->lp_noise = tmp;
1063 : }
1064 :
1065 12821531 : hFdCngDec->hFdCngCom->flag_noisy_speech = ( hFdCngDec->lp_speech - hFdCngDec->lp_noise ) < 28.f;
1066 :
1067 12821531 : return;
1068 : }
1069 :
1070 :
1071 : /*-------------------------------------------------------------------
1072 : * generate_comfort_noise_dec()
1073 : *
1074 : * Generate the comfort noise based on the target noise level
1075 : *-------------------------------------------------------------------*/
1076 :
1077 117021 : void generate_comfort_noise_dec(
1078 : float **bufferReal, /* o : Real part of input bands */
1079 : float **bufferImag, /* o : Imaginary part of input bands */
1080 : Decoder_State *st, /* i/o: decoder state structure */
1081 : const int16_t nchan_out /* i : number of output channels */
1082 : )
1083 : {
1084 : int16_t i, j;
1085 : float *ptr_r, *ptr_i;
1086 117021 : HANDLE_FD_CNG_DEC hFdCngDec = st->hFdCngDec;
1087 117021 : HANDLE_FD_CNG_COM hFdCngCom = hFdCngDec->hFdCngCom;
1088 117021 : float *cngNoiseLevel = hFdCngCom->cngNoiseLevel;
1089 117021 : float *ptr_level = cngNoiseLevel;
1090 117021 : int16_t *seed = &( hFdCngCom->seed );
1091 : int16_t *seed2;
1092 : float c1, c2;
1093 : float tmp1, tmp2;
1094 : float scale, scaleCldfb;
1095 117021 : float *fftBuffer = hFdCngCom->fftBuffer;
1096 117021 : float *timeDomainOutput = hFdCngCom->timeDomainBuffer;
1097 : int16_t tcx_transition;
1098 : float enr, att;
1099 :
1100 117021 : scale = 1.f;
1101 117021 : scaleCldfb = CLDFB_SCALING / hFdCngCom->scalingFactor;
1102 :
1103 117021 : c1 = (float) sqrt( hFdCngCom->coherence );
1104 117021 : c2 = (float) sqrt( 1 - hFdCngCom->coherence );
1105 :
1106 117021 : seed2 = &( hFdCngCom->seed2 );
1107 117021 : if ( st->element_mode == IVAS_CPE_MDCT && st->idchan == 1 )
1108 : {
1109 17390 : seed2 = &( hFdCngCom->seed3 );
1110 : }
1111 :
1112 : /* Generate Gaussian random noise in real and imaginary parts of the FFT bins
1113 : Amplitudes are adjusted to the estimated noise level cngNoiseLevel in each bin */
1114 :
1115 117021 : if ( hFdCngCom->startBand == 0 )
1116 : {
1117 0 : if ( st->element_mode == IVAS_CPE_MDCT || ( st->element_mode == IVAS_SCE && st->cng_ism_flag ) )
1118 : {
1119 0 : rand_gauss( &tmp1, seed );
1120 0 : rand_gauss( &tmp2, seed2 );
1121 0 : fftBuffer[0] = tmp1 * c1 + tmp2 * c2;
1122 : }
1123 : else
1124 : {
1125 0 : rand_gauss( &fftBuffer[0], seed );
1126 : }
1127 0 : fftBuffer[0] *= (float) sqrt( scale * *ptr_level ); /* DC component in FFT */
1128 0 : ptr_level++;
1129 0 : ptr_r = fftBuffer + 2;
1130 : }
1131 : else
1132 : {
1133 117021 : fftBuffer[0] = 0.f;
1134 117021 : set_f( fftBuffer + 2, 0.0f, 2 * ( hFdCngCom->startBand - 1 ) );
1135 117021 : ptr_r = fftBuffer + 2 * hFdCngCom->startBand;
1136 : }
1137 :
1138 117021 : ptr_i = ptr_r + 1;
1139 34593635 : for ( ; ptr_level < cngNoiseLevel + hFdCngCom->stopFFTbin - hFdCngCom->startBand; ptr_level++ )
1140 : {
1141 : /* Real part in FFT bins */
1142 34476614 : if ( st->element_mode == IVAS_CPE_MDCT || ( st->element_mode == IVAS_SCE && st->cng_ism_flag ) )
1143 : {
1144 23486884 : rand_gauss( &tmp1, seed );
1145 23486884 : rand_gauss( &tmp2, seed2 );
1146 23486884 : *ptr_r = tmp1 * c1 + tmp2 * c2;
1147 : }
1148 : else
1149 : {
1150 10989730 : rand_gauss( ptr_r, seed );
1151 : }
1152 34476614 : ( *ptr_r ) *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
1153 34476614 : ptr_r += 2;
1154 :
1155 : /* Imaginary part in FFT bins */
1156 34476614 : if ( st->element_mode == IVAS_CPE_MDCT || ( st->element_mode == IVAS_SCE && st->cng_ism_flag ) )
1157 : {
1158 23486884 : rand_gauss( &tmp1, seed );
1159 23486884 : rand_gauss( &tmp2, seed2 );
1160 23486884 : *ptr_i = tmp1 * c1 + tmp2 * c2;
1161 : }
1162 : else
1163 : {
1164 10989730 : rand_gauss( ptr_i, seed );
1165 : }
1166 34476614 : ( *ptr_i ) *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
1167 34476614 : ptr_i += 2;
1168 : }
1169 :
1170 : /* Remaining FFT bins are set to zero */
1171 117021 : set_f( fftBuffer + 2 * hFdCngCom->stopFFTbin, 0.0f, hFdCngCom->fftlen - 2 * hFdCngCom->stopFFTbin );
1172 :
1173 : /* Nyquist frequency is discarded */
1174 117021 : fftBuffer[1] = 0.f;
1175 :
1176 : /* If previous frame is active, reset the overlap-add buffer */
1177 117021 : tcx_transition = 0;
1178 117021 : if ( hFdCngCom->frame_type_previous == ACTIVE_FRAME )
1179 : {
1180 11349 : set_f( hFdCngCom->olapBufferSynth, 0.0f, hFdCngCom->fftlen );
1181 11349 : if ( ( st->core > ACELP_CORE && st->codec_mode == MODE2 ) || st->codec_mode == MODE1 )
1182 : {
1183 11329 : tcx_transition = 1;
1184 : }
1185 : }
1186 :
1187 : /* Perform STFT synthesis */
1188 117021 : SynthesisSTFT( fftBuffer, timeDomainOutput, hFdCngCom->olapBufferSynth, hFdCngCom->olapWinSyn, tcx_transition, hFdCngCom, st->element_mode, nchan_out );
1189 :
1190 : /* update CNG excitation energy for LP_CNG */
1191 :
1192 : /* calculate the residual signal energy */
1193 117021 : enr = dotp( hFdCngCom->exc_cng, hFdCngCom->exc_cng, hFdCngCom->frameSize ) / hFdCngCom->frameSize;
1194 :
1195 : /* convert log2 of residual signal energy */
1196 117021 : enr = (float) log10( enr + 0.1f ) / (float) log10( 2.0f );
1197 :
1198 : /* decrease the energy in case of WB input */
1199 117021 : if ( st->bwidth != NB )
1200 : {
1201 116077 : if ( st->bwidth == WB )
1202 : {
1203 26928 : if ( st->CNG_mode >= 0 )
1204 : {
1205 : /* Bitrate adapted attenuation */
1206 9 : att = ENR_ATT[st->CNG_mode];
1207 : }
1208 : else
1209 : {
1210 : /* Use least attenuation for higher bitrates */
1211 26919 : att = ENR_ATT[4];
1212 : }
1213 : }
1214 : else
1215 : {
1216 89149 : att = 1.5f;
1217 : }
1218 :
1219 116077 : enr -= att;
1220 : }
1221 :
1222 117021 : st->lp_ener = (float) ( 0.8f * st->lp_ener + 0.2f * pow( 2.0f, enr ) );
1223 :
1224 : /* Generate Gaussian random noise in real and imaginary parts of the CLDFB bands
1225 : Amplitudes are adjusted to the estimated noise level cngNoiseLevel in each band */
1226 :
1227 117021 : if ( bufferReal != NULL && hFdCngCom->numCoreBands < hFdCngCom->regularStopBand )
1228 : {
1229 0 : for ( j = hFdCngCom->numCoreBands; j < hFdCngCom->regularStopBand; j++ )
1230 : {
1231 0 : for ( i = 0; i < hFdCngCom->numSlots; i++ )
1232 : {
1233 : /* Real part in CLDFB band */
1234 0 : if ( st->element_mode == IVAS_CPE_MDCT || ( st->element_mode == IVAS_SCE && st->cng_ism_flag ) )
1235 : {
1236 0 : rand_gauss( &tmp1, seed );
1237 0 : rand_gauss( &tmp2, seed2 );
1238 0 : bufferReal[i][j] = tmp1 * c1 + tmp2 * c2;
1239 : }
1240 : else
1241 : {
1242 0 : rand_gauss( &bufferReal[i][j], seed );
1243 : }
1244 0 : bufferReal[i][j] *= (float) sqrt( ( scaleCldfb * *ptr_level ) * 0.5f );
1245 :
1246 : /* Imaginary part in CLDFB band */
1247 0 : if ( st->element_mode == IVAS_CPE_MDCT || ( st->element_mode == IVAS_SCE && st->cng_ism_flag ) )
1248 : {
1249 0 : rand_gauss( &tmp1, seed );
1250 0 : rand_gauss( &tmp2, seed2 );
1251 0 : bufferImag[i][j] = tmp1 * c1 + tmp2 * c2;
1252 : }
1253 : else
1254 : {
1255 0 : rand_gauss( &bufferImag[i][j], seed );
1256 : }
1257 0 : bufferImag[i][j] *= (float) sqrt( ( scaleCldfb * *ptr_level ) * 0.5f );
1258 : }
1259 0 : ptr_level++;
1260 : }
1261 : }
1262 :
1263 : /* Overlap-add when previous frame is active */
1264 117021 : if ( hFdCngCom->frame_type_previous == ACTIVE_FRAME && st->codec_mode == MODE2 )
1265 : {
1266 22 : float noise[2048], old_exc_ener = 0.f, gain = 0.f, tmp;
1267 22 : int16_t N = hFdCngCom->frameSize;
1268 22 : int16_t seed_loc = hFdCngCom->seed;
1269 : float *old_exc, old_Aq[M + 1], *old_syn_pe, old_syn;
1270 :
1271 22 : if ( st->core > ACELP_CORE )
1272 : {
1273 4 : tcx_windowing_synthesis_current_frame( timeDomainOutput, st->hTcxCfg->tcx_mdct_window, /*Keep sine windows for limiting Time modulation*/
1274 6 : st->hTcxCfg->tcx_mdct_window_half, st->hTcxCfg->tcx_mdct_window_minimum, st->hTcxCfg->tcx_mdct_window_length, st->hTcxCfg->tcx_mdct_window_half_length, st->hTcxCfg->tcx_mdct_window_min_length, 0, st->hTcxCfg->tcx_last_overlap_mode == ALDO_WINDOW ? FULL_OVERLAP : st->hTcxCfg->tcx_last_overlap_mode, NULL, NULL, NULL, NULL, NULL, N / 2, st->hTcxCfg->tcx_offset < 0 ? -st->hTcxCfg->tcx_offset : 0, 1, 0, 0 );
1275 :
1276 2 : if ( st->hTcxCfg->last_aldo )
1277 : {
1278 370 : for ( i = 0; i < ( hFdCngCom->frameSize - NS2SA( st->sr_core, N_ZERO_MDCT_NS ) ); i++ )
1279 : {
1280 368 : timeDomainOutput[i] += st->hHQ_core->old_outLB[i + NS2SA( st->sr_core, N_ZERO_MDCT_NS )];
1281 : }
1282 : }
1283 : else
1284 : {
1285 0 : tcx_windowing_synthesis_past_frame( st->hTcxDec->syn_Overl, st->hTcxCfg->tcx_mdct_window, st->hTcxCfg->tcx_mdct_window_half, st->hTcxCfg->tcx_mdct_window_minimum, st->hTcxCfg->tcx_mdct_window_length, st->hTcxCfg->tcx_mdct_window_half_length, st->hTcxCfg->tcx_mdct_window_min_length, st->hTcxCfg->tcx_last_overlap_mode );
1286 :
1287 0 : for ( i = 0; i < st->hTcxCfg->tcx_mdct_window_length; i++ )
1288 : {
1289 0 : timeDomainOutput[i] += st->hTcxDec->syn_Overl[i];
1290 : }
1291 : }
1292 : }
1293 : else
1294 : {
1295 20 : mvr2r( st->old_Aq_12_8, old_Aq, M + 1 );
1296 20 : old_exc = st->old_exc + L_EXC_MEM_DEC - ( N / 2 );
1297 20 : old_syn_pe = st->mem_syn2;
1298 20 : old_syn = st->syn[M];
1299 :
1300 2580 : for ( i = 0; i < N / 2; i++ )
1301 : {
1302 2560 : old_exc_ener += old_exc[i] * old_exc[i];
1303 : }
1304 :
1305 20 : old_exc_ener = (float) sqrt( old_exc_ener / (float) ( N / 2 ) );
1306 :
1307 5140 : for ( i = 0; i < N; i++ )
1308 : {
1309 5120 : rand_gauss( &( noise[i] ), &( seed_loc ) );
1310 5120 : gain += noise[i] * noise[i];
1311 : }
1312 :
1313 20 : gain = old_exc_ener / (float) sqrt( gain / (float) N );
1314 :
1315 5140 : for ( i = 0; i < N; i++ )
1316 : {
1317 5120 : noise[i] *= gain;
1318 : }
1319 :
1320 20 : syn_filt( old_Aq, M, noise, noise, N, old_syn_pe, 0 );
1321 :
1322 20 : tmp = old_syn;
1323 :
1324 20 : deemph( noise, st->preemph_fac, N, &tmp );
1325 :
1326 2580 : for ( i = 0; i < N / 2; i++ )
1327 : {
1328 2560 : timeDomainOutput[i] += noise[i] * hFdCngCom->olapWinSyn[N / 2 + i];
1329 : }
1330 : }
1331 : }
1332 :
1333 117021 : return;
1334 : }
1335 :
1336 :
1337 : /*-------------------------------------------------------------------
1338 : * generate_comfort_noise_dec_hf()
1339 : *
1340 : * Generate the comfort noise based on the target noise level for the CLDFB part
1341 : *-------------------------------------------------------------------*/
1342 :
1343 94957 : void generate_comfort_noise_dec_hf(
1344 : float **bufferReal, /* o : Real part of input bands */
1345 : float **bufferImag, /* o : Imaginary part of input bands */
1346 : HANDLE_FD_CNG_COM hFdCngCom, /* i/o: FD_CNG structure containing all buffers and variables */
1347 : const int16_t cng_coh_flag /* i : CNG Flag for coherence handling */
1348 : )
1349 : {
1350 : int16_t i, j;
1351 : float *ptr_level;
1352 :
1353 94957 : int16_t *seed = &( hFdCngCom->seed );
1354 94957 : float scale = CLDFB_SCALING / hFdCngCom->scalingFactor;
1355 :
1356 94957 : int16_t *seed2 = &( hFdCngCom->seed );
1357 :
1358 94957 : float tmp1, tmp2, c1 = 0.f, c2 = 0.f;
1359 :
1360 94957 : if ( cng_coh_flag )
1361 : {
1362 38139 : seed2 = &( hFdCngCom->seed2 );
1363 :
1364 38139 : c1 = (float) sqrt( hFdCngCom->coherence );
1365 38139 : c2 = (float) sqrt( 1 - hFdCngCom->coherence );
1366 : }
1367 :
1368 94957 : ptr_level = hFdCngCom->cngNoiseLevel + hFdCngCom->stopFFTbin - hFdCngCom->startBand;
1369 : /*
1370 : Generate Gaussian random noise in real and imaginary parts of the CLDFB bands
1371 : Amplitudes are adjusted to the estimated noise level cngNoiseLevel in each band
1372 : */
1373 94957 : if ( hFdCngCom->numCoreBands < hFdCngCom->regularStopBand )
1374 : {
1375 1708529 : for ( j = hFdCngCom->numCoreBands; j < hFdCngCom->regularStopBand; j++ )
1376 : {
1377 27599058 : for ( i = 0; i < hFdCngCom->numSlots; i++ )
1378 : {
1379 25975584 : if ( cng_coh_flag )
1380 : {
1381 : /* Real part in CLDFB band */
1382 10720080 : rand_gauss( &tmp1, seed );
1383 10720080 : rand_gauss( &tmp2, seed2 );
1384 10720080 : bufferReal[i][j] = tmp1 * c1 + tmp2 * c2;
1385 10720080 : bufferReal[i][j] *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
1386 :
1387 : /* Imaginary part in CLDFB band */
1388 10720080 : rand_gauss( &tmp1, seed );
1389 10720080 : rand_gauss( &tmp2, seed2 );
1390 10720080 : bufferImag[i][j] = tmp1 * c1 + tmp2 * c2;
1391 10720080 : bufferImag[i][j] *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
1392 : }
1393 : else
1394 : {
1395 : /* Real part in CLDFB band */
1396 15255504 : rand_gauss( &bufferReal[i][j], seed );
1397 15255504 : bufferReal[i][j] *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
1398 : /* Imaginary part in CLDFB band */
1399 15255504 : rand_gauss( &bufferImag[i][j], seed );
1400 15255504 : bufferImag[i][j] *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
1401 : }
1402 : }
1403 1623474 : ptr_level++;
1404 : }
1405 : }
1406 :
1407 94957 : return;
1408 : }
1409 :
1410 :
1411 : /*-------------------------------------------------------------------
1412 : * generate_masking_noise()
1413 : *
1414 : * Generate additional comfort noise (kind of noise filling)
1415 : *-------------------------------------------------------------------*/
1416 :
1417 2532779 : void generate_masking_noise(
1418 : float *timeDomainBuffer, /* i/o: time-domain signal */
1419 : HANDLE_FD_CNG_COM hFdCngCom, /* i/o: FD_CNG structure containing all buffers and variables */
1420 : const int16_t length, /* i : frame size */
1421 : const int16_t core, /* i : core */
1422 : const int16_t return_noise, /* i : noise is returned instead of added */
1423 : const int16_t secondary, /* i : flag to indicate secondary noise generation */
1424 : const int16_t element_mode, /* i : element mode */
1425 : STEREO_CNG_DEC_HANDLE hStereoCng, /* i : stereo CNG handle */
1426 : const int16_t nchan_out /* i : number of output channels */
1427 : )
1428 : {
1429 2532779 : float *cngNoiseLevel = hFdCngCom->cngNoiseLevel;
1430 2532779 : float *ptr_level = cngNoiseLevel;
1431 2532779 : float *fftBuffer = hFdCngCom->fftBuffer;
1432 : int16_t i;
1433 : float maskingNoise[L_FRAME16k];
1434 : float *ptr_r;
1435 : float *ptr_i;
1436 2532779 : int16_t startBand = hFdCngCom->startBand;
1437 2532779 : int16_t *seed = &( hFdCngCom->seed );
1438 2532779 : float scale = 1.f;
1439 :
1440 : /* skip noise generating if level is very low, to avoid problems with possibly running into denormals */
1441 2532779 : if ( hFdCngCom->likelihood_noisy_speech > DELTA_MASKING_NOISE )
1442 : {
1443 894160 : if ( core != AMR_WB_CORE )
1444 : {
1445 : /* Compute additional CN level */
1446 12178440 : for ( i = 0; i < SIZE_SCALE_TABLE_CN; i++ )
1447 : {
1448 12178440 : if ( ( hFdCngCom->CngBandwidth == scaleTable_cn_only[i].bwmode ) &&
1449 4226176 : ( hFdCngCom->CngBitrate >= scaleTable_cn_only[i].bitrateFrom ) &&
1450 4226176 : ( hFdCngCom->CngBitrate < scaleTable_cn_only[i].bitrateTo ) )
1451 : {
1452 894160 : break;
1453 : }
1454 : }
1455 :
1456 894160 : scale *= (float) pow( 10.f, -scaleTable_cn_only[i].scale / 10.f ) - 1.f;
1457 : }
1458 : else
1459 : {
1460 : /* Compute additional CN level */
1461 0 : for ( i = 0; i < SIZE_SCALE_TABLE_CN_AMRWB; i++ )
1462 : {
1463 0 : if ( hFdCngCom->CngBitrate >= scaleTable_cn_only_amrwbio[i][0] )
1464 : {
1465 0 : break;
1466 : }
1467 : }
1468 :
1469 0 : if ( i < SIZE_SCALE_TABLE_CN_AMRWB )
1470 : {
1471 0 : scale *= (float) pow( 10.f, -scaleTable_cn_only_amrwbio[i][1] / 10.f ) - 1.f;
1472 : }
1473 : else
1474 : {
1475 0 : scale = 0.f;
1476 : }
1477 : }
1478 :
1479 : /* Exclude clean speech */
1480 894160 : scale *= hFdCngCom->likelihood_noisy_speech;
1481 :
1482 : /* Generate Gaussian random noise in real and imaginary parts of the FFT bins
1483 : Amplitudes are adjusted to the estimated noise level cngNoiseLevel in each bin */
1484 894160 : if ( startBand == 0 )
1485 : {
1486 0 : rand_gauss( &fftBuffer[0], seed );
1487 0 : ptr_r = fftBuffer + 2;
1488 0 : fftBuffer[0] *= (float) sqrt( scale * *ptr_level ); /* DC component in FFT */
1489 0 : ptr_level++;
1490 : }
1491 : else
1492 : {
1493 894160 : fftBuffer[0] = 0.f;
1494 894160 : set_f( fftBuffer + 2, 0.0f, 2 * ( startBand - 1 ) );
1495 894160 : ptr_r = fftBuffer + 2 * startBand;
1496 : }
1497 894160 : ptr_i = ptr_r + 1;
1498 277345136 : for ( ; ptr_level < cngNoiseLevel + hFdCngCom->stopFFTbin - startBand; ptr_level++ )
1499 : {
1500 : /* Real part in FFT bins */
1501 276450976 : rand_gauss( ptr_r, seed );
1502 276450976 : ( *ptr_r ) *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
1503 276450976 : ptr_r += 2;
1504 : /* Imaginary part in FFT bins */
1505 276450976 : rand_gauss( ptr_i, seed );
1506 276450976 : ( *ptr_i ) *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
1507 276450976 : ptr_i += 2;
1508 : }
1509 :
1510 : /* Remaining FFT bins are set to zero */
1511 894160 : set_f( fftBuffer + 2 * hFdCngCom->stopFFTbin, 0.0f, hFdCngCom->fftlen - 2 * hFdCngCom->stopFFTbin );
1512 : /* Nyquist frequency is discarded */
1513 894160 : fftBuffer[1] = 0.f;
1514 : }
1515 : else
1516 : {
1517 : /* very low level case - update random seeds and reset FFT buffer; don't fully skip SynthesisSTFT(), because of the buffer updates done there... */
1518 1638619 : generate_masking_noise_update_seed( hFdCngCom );
1519 :
1520 1638619 : set_f( fftBuffer, 0.f, hFdCngCom->fftlen );
1521 : }
1522 :
1523 : /* Perform STFT synthesis */
1524 2532779 : if ( secondary )
1525 : {
1526 1908 : SynthesisSTFT( fftBuffer, maskingNoise, hStereoCng->olapBufferSynth22, hFdCngCom->olapWinSyn, 0, hFdCngCom, element_mode, nchan_out );
1527 : }
1528 : else
1529 : {
1530 2530871 : SynthesisSTFT( fftBuffer, maskingNoise, hFdCngCom->olapBufferSynth2, hFdCngCom->olapWinSyn, 0, hFdCngCom, element_mode, nchan_out );
1531 : }
1532 :
1533 : /* Add some comfort noise on top of decoded signal */
1534 2532779 : if ( return_noise )
1535 : {
1536 3819 : mvr2r( maskingNoise, timeDomainBuffer, min( hFdCngCom->frameSize, length ) );
1537 : }
1538 : else
1539 : {
1540 2528960 : v_add( maskingNoise, timeDomainBuffer, timeDomainBuffer, min( hFdCngCom->frameSize, length ) );
1541 : }
1542 :
1543 2532779 : return;
1544 : }
1545 :
1546 :
1547 : /*-------------------------------------------------------------------
1548 : * generate_masking_noise_update_seed()
1549 : *
1550 : * Update seed for scenarios where generate_masking_noise() is
1551 : * not called based on signal statistics
1552 : *-------------------------------------------------------------------*/
1553 :
1554 1748298 : void generate_masking_noise_update_seed(
1555 : HANDLE_FD_CNG_COM hFdCngCom /* i/o: FD_CNG structure containing all buffers and variables */
1556 : )
1557 : {
1558 1748298 : float *cngNoiseLevel = hFdCngCom->cngNoiseLevel;
1559 1748298 : float *ptr_level = cngNoiseLevel;
1560 1748298 : int16_t startBand = hFdCngCom->startBand;
1561 1748298 : int16_t *seed = &( hFdCngCom->seed );
1562 1748298 : float tmp = 0;
1563 :
1564 : /*
1565 : Generate Gaussian random noise in real and imaginary parts of the FFT bins
1566 : Amplitudes are adjusted to the estimated noise level cngNoiseLevel in each bin
1567 : */
1568 1748298 : if ( startBand == 0 )
1569 : {
1570 0 : rand_gauss( &tmp, seed );
1571 0 : ptr_level++;
1572 : }
1573 :
1574 501695030 : for ( ; ptr_level < cngNoiseLevel + hFdCngCom->stopFFTbin - startBand; ptr_level++ )
1575 : {
1576 : /* Real part in FFT bins */
1577 499946732 : rand_gauss( &tmp, seed );
1578 499946732 : rand_gauss( &tmp, seed );
1579 : }
1580 :
1581 1748298 : return;
1582 : }
1583 :
1584 :
1585 : /*-------------------------------------------------------------------
1586 : * generate_masking_noise_mdct()
1587 : *
1588 : * Generate additional comfort noise (kind of noise filling)
1589 : *-------------------------------------------------------------------*/
1590 :
1591 1168280 : void generate_masking_noise_mdct(
1592 : float *mdctBuffer, /* i/o: time-domain signal */
1593 : HANDLE_FD_CNG_COM hFdCngCom /* i/o: FD_CNG structure containing all buffers and variables */
1594 : )
1595 : {
1596 1168280 : float *cngNoiseLevel = hFdCngCom->cngNoiseLevel;
1597 : int16_t i;
1598 : float maskingNoise[2 * L_FRAME16k];
1599 : float *ptr_r;
1600 1168280 : float *ptr_level = cngNoiseLevel;
1601 1168280 : int16_t startBand = hFdCngCom->startBand;
1602 1168280 : int16_t *seed = &( hFdCngCom->seed );
1603 1168280 : float scale = 1.f;
1604 :
1605 : /* skip noise generating if level is very low, to avoid problems with possibly running into denormals */
1606 1168280 : if ( hFdCngCom->likelihood_noisy_speech > DELTA_MASKING_NOISE )
1607 : {
1608 5606976 : for ( i = 0; i < SIZE_SCALE_TABLE_CN; i++ )
1609 : {
1610 5606976 : if ( ( hFdCngCom->CngBandwidth == scaleTable_cn_only[i].bwmode ) &&
1611 1977850 : ( hFdCngCom->CngBitrate >= scaleTable_cn_only[i].bitrateFrom ) &&
1612 1977850 : ( hFdCngCom->CngBitrate < scaleTable_cn_only[i].bitrateTo ) )
1613 : {
1614 407868 : break;
1615 : }
1616 : }
1617 :
1618 407868 : scale *= (float) pow( 10.f, -scaleTable_cn_only[i].scale / 10.f ) - 1.f;
1619 :
1620 : /* Exclude clean speech */
1621 407868 : scale *= hFdCngCom->likelihood_noisy_speech;
1622 :
1623 : /*
1624 : Generate Gaussian random noise in real and imaginary parts of the FFT bins
1625 : Amplitudes are adjusted to the estimated noise level cngNoiseLevel in each bin
1626 : */
1627 407868 : if ( startBand == 0 )
1628 : {
1629 0 : rand_gauss( &maskingNoise[0], seed );
1630 0 : maskingNoise[0] *= (float) sqrt( scale * *ptr_level * 0.5f ); /* DC component in FFT */
1631 0 : ptr_level++;
1632 0 : ptr_r = maskingNoise + 1;
1633 : }
1634 : else
1635 : {
1636 407868 : maskingNoise[0] = 0.f;
1637 407868 : set_f( maskingNoise + 1, 0.0f, ( startBand - 1 ) );
1638 407868 : ptr_r = maskingNoise + startBand;
1639 : }
1640 :
1641 127684996 : for ( ; ptr_level < cngNoiseLevel + hFdCngCom->stopFFTbin - startBand; ptr_level++ )
1642 : {
1643 : /* MDCT bins */
1644 127277128 : rand_gauss( ptr_r, seed );
1645 127277128 : ( *ptr_r ) *= (float) sqrt( scale * *ptr_level * 0.5f );
1646 127277128 : ptr_r += 1;
1647 : }
1648 :
1649 : /*re-normalization of energy level: M/sqrt(2)*/
1650 407868 : v_multc( maskingNoise, (float) sqrt( NORM_MDCT_FACTOR ), maskingNoise, hFdCngCom->stopFFTbin );
1651 :
1652 : /* Add some comfort noise on top of decoded signal */
1653 407868 : v_add( maskingNoise, mdctBuffer, mdctBuffer, hFdCngCom->stopFFTbin );
1654 : }
1655 : else
1656 : {
1657 : /* very low level case - just update random seeds */
1658 760412 : if ( startBand == 0 )
1659 : {
1660 0 : rand_gauss( &maskingNoise[0], seed );
1661 0 : ptr_level++;
1662 : }
1663 :
1664 226400676 : for ( ; ptr_level < cngNoiseLevel + hFdCngCom->stopFFTbin - startBand; ptr_level++ )
1665 : {
1666 225640264 : rand_gauss( &maskingNoise[0], seed );
1667 : }
1668 : }
1669 :
1670 1168280 : return;
1671 : }
1672 :
1673 : /*-------------------------------------------------------------------
1674 : * generate_stereo_masking_noise()
1675 : *
1676 : * Generate additional comfort noise (kind of noise filling)
1677 : *-------------------------------------------------------------------*/
1678 :
1679 3855 : void generate_stereo_masking_noise(
1680 : float *syn, /* i/o: time-domain signal */
1681 : Decoder_State *st, /* i/o: decoder state structure */
1682 : STEREO_TD_DEC_DATA_HANDLE hStereoTD, /* i : TD stereo structure */
1683 : const int16_t flag_sec_CNA, /* i : CNA flag for secondary channel */
1684 : const int16_t fadeOut, /* i : only fade out of previous state */
1685 : STEREO_CNG_DEC_HANDLE hStereoCng, /* i : Stereo CNG handle */
1686 : const int16_t nchan_out /* i : number of output channels */
1687 : )
1688 : {
1689 : HANDLE_FD_CNG_COM hFdCngCom;
1690 : float gamma, scale, SP_ratio;
1691 : float Np[L_FRAME16k];
1692 : float Ns[L_FRAME16k];
1693 : float N1[L_FRAME16k];
1694 : float N2[L_FRAME16k];
1695 : int16_t i;
1696 :
1697 3855 : if ( st->idchan == 0 )
1698 : {
1699 1911 : hFdCngCom = st->hFdCngDec->hFdCngCom;
1700 1911 : mvr2r( hFdCngCom->olapBufferSynth2, Np, hFdCngCom->frameSize / 2 );
1701 1911 : mvr2r( hStereoCng->olapBufferSynth22, Ns, hFdCngCom->frameSize / 2 );
1702 1911 : set_f( &Np[hFdCngCom->frameSize / 2], 0.0f, hFdCngCom->frameSize / 2 );
1703 1911 : set_f( &Ns[hFdCngCom->frameSize / 2], 0.0f, hFdCngCom->frameSize / 2 );
1704 :
1705 1911 : if ( !fadeOut )
1706 : {
1707 1911 : generate_masking_noise( N1, hFdCngCom, hFdCngCom->frameSize, 0, 1, 0, st->element_mode, hStereoCng, nchan_out );
1708 : /* Generate masking noise for secondary channel */
1709 1911 : if ( flag_sec_CNA )
1710 : {
1711 1908 : generate_masking_noise( N2, hFdCngCom, hFdCngCom->frameSize, 0, 1, 1, st->element_mode, hStereoCng, nchan_out );
1712 1908 : gamma = hStereoCng->c_PS_LT * hStereoCng->c_PS_LT;
1713 1908 : scale = 1.0f;
1714 1908 : if ( gamma < 0.9f )
1715 : {
1716 1908 : gamma = gamma / ( 1 - gamma );
1717 1908 : gamma = (float) sqrt( gamma + 1 ) - (float) sqrt( gamma );
1718 1908 : scale = 1.0f / (float) sqrt( 1 + gamma * gamma );
1719 : }
1720 : else
1721 : {
1722 0 : gamma = 0.0f;
1723 : }
1724 :
1725 246132 : for ( i = 0; i < 2 * hFdCngCom->frameSize / 4; i++ )
1726 : {
1727 244224 : Np[i] += scale * ( N1[i] + gamma * N2[i] );
1728 244224 : Ns[i] += scale * sign( hStereoCng->c_PS_LT ) * ( N1[i] - gamma * N2[i] );
1729 : }
1730 246132 : for ( ; i < hFdCngCom->frameSize; i++ )
1731 : {
1732 244224 : Np[i] = scale * ( N1[i] + gamma * N2[i] );
1733 244224 : Ns[i] = scale * sign( hStereoCng->c_PS_LT ) * ( N1[i] - gamma * N2[i] );
1734 : }
1735 1908 : scale *= (float) ( hFdCngCom->fftlen / 2 );
1736 246132 : for ( i = 0; i < hFdCngCom->frameSize / 2; i++ )
1737 : {
1738 244224 : hFdCngCom->olapBufferSynth2[i] = scale * ( hFdCngCom->olapBufferSynth2[i + 5 * hFdCngCom->frameSize / 4] + gamma * hStereoCng->olapBufferSynth22[i + 5 * hFdCngCom->frameSize / 4] );
1739 244224 : hStereoCng->olapBufferSynth22[i] = sign( hStereoCng->c_PS_LT ) * scale * ( hFdCngCom->olapBufferSynth2[i + 5 * hFdCngCom->frameSize / 4] - gamma * hStereoCng->olapBufferSynth22[i + 5 * hFdCngCom->frameSize / 4] );
1740 : }
1741 : }
1742 : else
1743 : {
1744 387 : for ( i = 0; i < hFdCngCom->frameSize / 2; i++ )
1745 : {
1746 384 : Np[i] += N1[i];
1747 : }
1748 3 : mvr2r( &N1[hFdCngCom->frameSize / 2], &Np[hFdCngCom->frameSize / 2], hFdCngCom->frameSize / 2 );
1749 3 : scale = (float) ( hFdCngCom->fftlen / 2 );
1750 771 : for ( i = 0; i < hFdCngCom->frameSize; i++ )
1751 : {
1752 768 : hFdCngCom->olapBufferSynth2[i] = scale * hFdCngCom->olapBufferSynth2[i + 5 * hFdCngCom->frameSize / 4];
1753 : }
1754 : }
1755 : }
1756 : else
1757 : {
1758 0 : set_f( hFdCngCom->olapBufferSynth2, 0.0f, hFdCngCom->frameSize / 2 );
1759 0 : set_f( hStereoCng->olapBufferSynth22, 0.0f, hFdCngCom->frameSize / 2 );
1760 : }
1761 1911 : if ( flag_sec_CNA )
1762 : {
1763 1908 : mvr2r( Ns, hStereoCng->maskingNoiseS, hFdCngCom->frameSize );
1764 1908 : hStereoCng->enableSecCNA = 1;
1765 : }
1766 : else
1767 : {
1768 3 : set_f( hStereoCng->olapBufferSynth22, 0.0f, hFdCngCom->frameSize );
1769 : }
1770 :
1771 : /* add masking noise */
1772 1911 : v_add( Np, syn, syn, hFdCngCom->frameSize );
1773 : }
1774 1944 : else if ( hStereoCng->enableSecCNA )
1775 : {
1776 1908 : SP_ratio = hStereoTD->SP_ratio_LT; /* Use long-term SP ratio based on L/R synthesis */
1777 : /* scale and add masking noise */
1778 124020 : for ( i = 0; i < *hStereoCng->frameSize / 4; i++ )
1779 : {
1780 122112 : scale = ( ( hStereoTD->prevSP_ratio * ( *hStereoCng->frameSize / 4 - (float) i ) + SP_ratio * (float) i ) / ( *hStereoCng->frameSize / 4 ) );
1781 122112 : syn[i] += scale * hStereoCng->maskingNoiseS[i];
1782 : }
1783 124020 : for ( ; i < *hStereoCng->frameSize / 2; i++ )
1784 : {
1785 122112 : syn[i] += SP_ratio * hStereoCng->maskingNoiseS[i];
1786 : }
1787 246132 : for ( ; i < *hStereoCng->frameSize; i++ )
1788 : {
1789 244224 : syn[i] += SP_ratio * hStereoCng->maskingNoiseS[i];
1790 : }
1791 1908 : hStereoTD->prevSP_ratio = SP_ratio;
1792 : }
1793 :
1794 3855 : return;
1795 : }
1796 :
1797 :
1798 : /*-------------------------------------------------------------------
1799 : * generate_masking_noise_hf_cldfb()
1800 : *
1801 : * Generate additional comfort noise (kind of noise filling)
1802 : *-------------------------------------------------------------------*/
1803 :
1804 493140 : void generate_masking_noise_lb_dirac(
1805 : HANDLE_FD_CNG_COM hFdCngCom, /* i/o: FD_CNG structure containing all buffers and variables */
1806 : float *tdBuffer, /* i/o: time-domain signal, if NULL no LB-CNA */
1807 : const int16_t nCldfbTs, /* i : number of CLDFB slots that will be rendered */
1808 : SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom, /* i : common spatial rendering parameters handle */
1809 : const int16_t cna_flag /* i : CNA flag for LB and HB */
1810 : )
1811 : {
1812 : int16_t i;
1813 493140 : float *cngNoiseLevel = hFdCngCom->cngNoiseLevel;
1814 493140 : float *fftBuffer = hFdCngCom->fftBuffer;
1815 : float *ptr_r;
1816 : float *ptr_i;
1817 : float *ptr_level;
1818 493140 : int16_t *seed = &( hFdCngCom->seed );
1819 : float scale;
1820 : int16_t n_samples_out, n_samples_start, n_samples_out_loop;
1821 :
1822 493140 : push_wmops( "fd_cng_dirac" );
1823 :
1824 : /* Init */
1825 493140 : scale = 0.f;
1826 493140 : n_samples_out = hFdCngCom->frameSize / DEFAULT_JBM_CLDFB_TIMESLOTS * nCldfbTs;
1827 493140 : n_samples_start = 0;
1828 :
1829 : /*LB CLDFB - CNA from STFT*/
1830 : #ifdef DEBUG_MODE_DIRAC
1831 : {
1832 : int16_t tmp_s;
1833 : tmp_s = (int16_t) ( 32768.f * 0.5f * hFdCngCom->likelihood_noisy_speech * cna_flag + 0.5f );
1834 : dbgwrite( &tmp_s, sizeof( int16_t ), 1, hFdCngCom->frameSize / 16, "./res/ivas_dirac_likelihood_noisy.pcm" );
1835 : }
1836 : #endif
1837 493140 : if ( cna_flag )
1838 : {
1839 : /* skip noise generating if level is very low, to avoid problems with possibly running into denormals */
1840 125408 : if ( hFdCngCom->likelihood_noisy_speech > DELTA_MASKING_NOISE )
1841 : {
1842 : /* Compute additional CN level */
1843 209908 : for ( i = 0; i < 15; i++ )
1844 : {
1845 209908 : if ( ( hFdCngCom->CngBandwidth == scaleTable_cn_dirac[i].bwmode ) &&
1846 43268 : ( hFdCngCom->CngBitrate >= scaleTable_cn_dirac[i].bitrateFrom ) &&
1847 43268 : ( hFdCngCom->CngBitrate < scaleTable_cn_dirac[i].bitrateTo ) )
1848 : {
1849 16834 : break;
1850 : }
1851 : }
1852 :
1853 16834 : scale = (float) pow( 10.f, -scaleTable_cn_dirac[i].scale / 10.f ) - 1.f;
1854 16834 : scale *= hFdCngCom->likelihood_noisy_speech;
1855 : }
1856 : }
1857 :
1858 : /* LB CLDFB - CNA from STFT: CNA applied only in channel 0*/
1859 493140 : if ( cna_flag && tdBuffer != NULL )
1860 : {
1861 : int16_t cur_subframe;
1862 : int16_t cur_subframe_start_outfs;
1863 : int16_t cur_subframe_start_cngfs;
1864 : int16_t slot_size_cng;
1865 :
1866 250817 : while ( n_samples_out > 0 )
1867 : {
1868 125409 : n_samples_out_loop = min( hFdCngCom->frameSize, n_samples_out );
1869 125409 : if ( scale != 0 )
1870 : {
1871 : /*Generate LF comfort noise only at first slot, for the whole frame*/
1872 16834 : ptr_level = cngNoiseLevel;
1873 :
1874 : /* Generate Gaussian random noise in real and imaginary parts of the FFT bins
1875 : Amplitudes are adjusted to the estimated noise level cngNoiseLevel in each bin */
1876 16834 : if ( hFdCngCom->startBand == 0 )
1877 : {
1878 0 : rand_gauss( &fftBuffer[0], seed );
1879 0 : ptr_r = fftBuffer + 2;
1880 0 : fftBuffer[0] *= (float) sqrt( scale * *ptr_level ); /* DC component in FFT */
1881 0 : ptr_level++;
1882 : }
1883 : else
1884 : {
1885 16834 : fftBuffer[0] = 0.f;
1886 16834 : set_f( fftBuffer + 2, 0.0f, 2 * ( hFdCngCom->startBand - 1 ) );
1887 16834 : ptr_r = fftBuffer + 2 * hFdCngCom->startBand;
1888 : }
1889 16834 : ptr_i = ptr_r + 1;
1890 :
1891 4966974 : for ( ; ptr_level < cngNoiseLevel + hFdCngCom->stopFFTbin - hFdCngCom->startBand; ptr_level++ )
1892 : {
1893 : /* Real part in FFT bins */
1894 4950140 : rand_gauss( ptr_r, seed );
1895 4950140 : ( *ptr_r ) *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
1896 4950140 : ptr_r += 2;
1897 : /* Imaginary part in FFT bins */
1898 4950140 : rand_gauss( ptr_i, seed );
1899 4950140 : ( *ptr_i ) *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
1900 4950140 : ptr_i += 2;
1901 : }
1902 :
1903 : /* Remaining FFT bins are set to zero */
1904 16834 : set_f( fftBuffer + 2 * hFdCngCom->stopFFTbin, 0.0f, hFdCngCom->fftlen - 2 * hFdCngCom->stopFFTbin );
1905 : /* Nyquist frequency is discarded */
1906 16834 : fftBuffer[1] = 0.f;
1907 :
1908 : /* Perform STFT synthesis */
1909 16834 : SynthesisSTFT_dirac( fftBuffer, tdBuffer + n_samples_start, hFdCngCom->olapBufferSynth2, hFdCngCom->olapWinSyn, n_samples_out_loop, hFdCngCom );
1910 :
1911 : #ifdef DEBUG_MODE_DIRAC
1912 : {
1913 : int16_t tmp[1000];
1914 :
1915 : for ( i = 0; i < hFdCngCom->frameSize; i++ )
1916 : {
1917 : tmp[i] = (int16_t) ( tdBuffer[i] + 0.5f );
1918 : }
1919 : dbgwrite( tmp, sizeof( int16_t ), hFdCngCom->frameSize, 1, "./res/ivas_dirac_cna_fft.pcm" );
1920 : }
1921 : #endif
1922 : }
1923 :
1924 : else
1925 : {
1926 : /* very low level case - update random seeds */
1927 108575 : generate_masking_noise_update_seed( hFdCngCom );
1928 :
1929 108575 : set_f( fftBuffer, 0.f, hFdCngCom->fftlen );
1930 :
1931 : /* Perform STFT synthesis */
1932 108575 : SynthesisSTFT_dirac( fftBuffer, tdBuffer + n_samples_start, hFdCngCom->olapBufferSynth2, hFdCngCom->olapWinSyn, n_samples_out_loop, hFdCngCom );
1933 :
1934 : #ifdef DEBUG_MODE_DIRAC
1935 : {
1936 : int16_t tmp[1000];
1937 :
1938 : for ( i = 0; i < hFdCngCom->frameSize; i++ )
1939 : {
1940 : tmp[i] = (int16_t) ( tdBuffer[i] + 0.5f );
1941 : }
1942 : dbgwrite( tmp, sizeof( int16_t ), hFdCngCom->frameSize, 1, "./res/ivas_dirac_cna_fft.pcm" );
1943 : }
1944 : #endif
1945 : }
1946 125409 : n_samples_out -= hFdCngCom->frameSize;
1947 125409 : n_samples_start += hFdCngCom->frameSize;
1948 : }
1949 :
1950 : /* move generated noise to the 5ms subframe starts in the tc buffer according to the output sampling frequency to avoid
1951 : overwriting it with the synthesis in case of shared tc and synth channel memory, i.e. non-TSM mode */
1952 125408 : slot_size_cng = hFdCngCom->frameSize / DEFAULT_JBM_CLDFB_TIMESLOTS;
1953 : /* move start indices forward to the end of the last subframe */
1954 125408 : cur_subframe_start_outfs = nCldfbTs * hSpatParamRendCom->slot_size;
1955 125408 : cur_subframe_start_cngfs = nCldfbTs * slot_size_cng;
1956 :
1957 : /* go from the last subframe back and move the LB noise */
1958 630281 : for ( cur_subframe = hSpatParamRendCom->nb_subframes - 1; cur_subframe >= 0; cur_subframe-- )
1959 : {
1960 : int16_t move_size, subframe_size_outfs;
1961 504873 : move_size = slot_size_cng * hSpatParamRendCom->subframe_nbslots[cur_subframe];
1962 504873 : subframe_size_outfs = hSpatParamRendCom->subframe_nbslots[cur_subframe] * hSpatParamRendCom->slot_size;
1963 504873 : cur_subframe_start_outfs -= hSpatParamRendCom->subframe_nbslots[cur_subframe] * hSpatParamRendCom->slot_size;
1964 504873 : cur_subframe_start_cngfs -= hSpatParamRendCom->subframe_nbslots[cur_subframe] * slot_size_cng;
1965 : /* move cna */
1966 504873 : mvr2r( tdBuffer + cur_subframe_start_cngfs, tdBuffer + cur_subframe_start_outfs, move_size );
1967 : /* set everything else to zero */
1968 504873 : set_zero( tdBuffer + cur_subframe_start_outfs + move_size, subframe_size_outfs - move_size );
1969 : }
1970 : }
1971 :
1972 :
1973 493140 : pop_wmops();
1974 :
1975 493140 : return;
1976 : }
1977 :
1978 :
1979 : /*-------------------------------------------------------------------
1980 : * generate_masking_noise_hf_cldfb()
1981 : *
1982 : * Generate additional comfort noise (kind of noise filling)
1983 : *-------------------------------------------------------------------*/
1984 :
1985 15043281 : void generate_masking_noise_dirac(
1986 : HANDLE_FD_CNG_COM hFdCngCom, /* i/o: FD_CNG structure containing all buffers and variables */
1987 : HANDLE_CLDFB_FILTER_BANK h_cldfb, /* i : filterbank state */
1988 : float *tdBuffer, /* i/o: time-domain signal, if NULL no LB-CNA */
1989 : float *Cldfb_RealBuffer, /* o : CLDFD real buffer */
1990 : float *Cldfb_ImagBuffer, /* o : CLDFD imaginary buffer */
1991 : const int16_t slot_index, /* i : CLDFB slot index */
1992 : const int16_t cna_flag, /* i : CNA flag for LB and HB */
1993 : const int16_t fd_cng_flag /* i : FD-CNG flag for HB */
1994 : )
1995 : {
1996 : int16_t i;
1997 : float *ptr_level;
1998 15043281 : int16_t *seed = &( hFdCngCom->seed );
1999 : float scale;
2000 :
2001 15043281 : push_wmops( "fd_cng_dirac" );
2002 :
2003 : /* Init */
2004 15043281 : scale = 0.f;
2005 :
2006 : /* Resample CLDFB memories if necessary*/
2007 15043281 : if ( ( h_cldfb->no_channels * h_cldfb->no_col ) != hFdCngCom->frameSize )
2008 : {
2009 10841 : resampleCldfb( h_cldfb, hFdCngCom->frameSize * FRAMES_PER_SEC );
2010 : }
2011 :
2012 15043281 : set_zero( Cldfb_RealBuffer, CLDFB_NO_CHANNELS_MAX );
2013 15043281 : set_zero( Cldfb_ImagBuffer, CLDFB_NO_CHANNELS_MAX );
2014 :
2015 : /*LB CLDFB - CNA from STFT*/
2016 : #ifdef DEBUG_MODE_DIRAC
2017 : {
2018 : int16_t tmp_s;
2019 : tmp_s = (int16_t) ( 32768.f * 0.5f * hFdCngCom->likelihood_noisy_speech * cna_flag + 0.5f );
2020 : dbgwrite( &tmp_s, sizeof( int16_t ), 1, hFdCngCom->frameSize / 16, "./res/ivas_dirac_likelihood_noisy.pcm" );
2021 : }
2022 : #endif
2023 15043281 : if ( cna_flag )
2024 : {
2025 : /* skip noise generating if level is very low, to avoid problems with possibly running into denormals */
2026 3693430 : if ( hFdCngCom->likelihood_noisy_speech > DELTA_MASKING_NOISE )
2027 : {
2028 : /* Compute additional CN level */
2029 5766816 : for ( i = 0; i < 15; i++ )
2030 : {
2031 5766816 : if ( ( hFdCngCom->CngBandwidth == scaleTable_cn_dirac[i].bwmode ) &&
2032 1213216 : ( hFdCngCom->CngBitrate >= scaleTable_cn_dirac[i].bitrateFrom ) &&
2033 1213216 : ( hFdCngCom->CngBitrate < scaleTable_cn_dirac[i].bitrateTo ) )
2034 : {
2035 460800 : break;
2036 : }
2037 : }
2038 :
2039 460800 : scale = (float) pow( 10.f, -scaleTable_cn_dirac[i].scale / 10.f ) - 1.f;
2040 460800 : scale *= hFdCngCom->likelihood_noisy_speech;
2041 : }
2042 : }
2043 : /* LB CLDFB - CNA from STFT: CNA applied only in channel 0*/
2044 15043281 : if ( cna_flag && tdBuffer != NULL )
2045 : {
2046 2006466 : if ( scale != 0 )
2047 : {
2048 : /* LF CLDFB*/
2049 269344 : cldfbAnalysis_ts( &( tdBuffer[hFdCngCom->numCoreBands * slot_index] ), Cldfb_RealBuffer, Cldfb_ImagBuffer, hFdCngCom->numCoreBands, h_cldfb );
2050 : }
2051 : else
2052 : {
2053 : /* LB ana CLDFB*/
2054 1737122 : cldfbAnalysis_ts( &( tdBuffer[hFdCngCom->numCoreBands * slot_index] ), Cldfb_RealBuffer, Cldfb_ImagBuffer, hFdCngCom->numCoreBands, h_cldfb );
2055 : }
2056 : }
2057 :
2058 : /*HF CLDFB - CNA and/or FD-CNG*/
2059 15043281 : if ( fd_cng_flag )
2060 : {
2061 51424 : scale += 1.f;
2062 : }
2063 15043281 : if ( scale != 0 )
2064 : {
2065 475296 : scale *= CLDFB_SCALING * ( h_cldfb->scale * h_cldfb->scale * 8.f );
2066 475296 : ptr_level = hFdCngCom->cngNoiseLevel + hFdCngCom->stopFFTbin - hFdCngCom->startBand;
2067 :
2068 9679104 : for ( i = hFdCngCom->numCoreBands; i < hFdCngCom->regularStopBand; i++ )
2069 : {
2070 : /* Real part in CLDFB band */
2071 9203808 : rand_gauss( &Cldfb_RealBuffer[i], seed );
2072 9203808 : Cldfb_RealBuffer[i] *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
2073 : /* Imaginary part in CLDFB band */
2074 9203808 : rand_gauss( &Cldfb_ImagBuffer[i], seed );
2075 9203808 : Cldfb_ImagBuffer[i] *= (float) sqrt( ( scale * *ptr_level ) * 0.5f );
2076 :
2077 9203808 : ptr_level++;
2078 : }
2079 : }
2080 :
2081 15043281 : pop_wmops();
2082 :
2083 15043281 : return;
2084 : }
2085 :
2086 :
2087 : /*-------------------------------------------------------------------
2088 : * FdCngDecodeMDCTStereoSID()
2089 : *
2090 : * Decode FD-Cng parameters for CNG in MDCT-Stereo mode from the bitstream
2091 : *
2092 : *-------------------------------------------------------------------*/
2093 :
2094 2386 : void FdCngDecodeMDCTStereoSID(
2095 : CPE_DEC_HANDLE hCPE /* i/o: CPE decoder state structure */
2096 : )
2097 : {
2098 : DEC_CORE_HANDLE sts[CPE_CHANNELS];
2099 : HANDLE_FD_CNG_COM hFdCngCom;
2100 : float *ms_ptr[CPE_CHANNELS];
2101 : float *lr_ptr[CPE_CHANNELS];
2102 : float logNoiseEst[CPE_CHANNELS][NPART];
2103 : float gain[CPE_CHANNELS];
2104 : int16_t indices[FD_CNG_stages_37bits];
2105 : int16_t N, i, ch, p, stages;
2106 : int16_t is_out_ms;
2107 : float *invTrfMatrix;
2108 : float tmpRAM[FDCNG_VQ_MAX_LEN][FDCNG_VQ_DCT_MAXTRUNC];
2109 :
2110 :
2111 2386 : invTrfMatrix = (float *) tmpRAM;
2112 2386 : create_IDCT_N_Matrix( invTrfMatrix, FDCNG_VQ_MAX_LEN, FDCNG_VQ_DCT_MAXTRUNC, sizeof( tmpRAM ) / ( sizeof( float ) ) );
2113 :
2114 2386 : is_out_ms = 0;
2115 2386 : if ( hCPE->hCoreCoder[0]->cng_sba_flag )
2116 : {
2117 0 : is_out_ms = 1;
2118 : }
2119 :
2120 2386 : N = 0; /* to avoid compilation warning */
2121 :
2122 7158 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
2123 : {
2124 4772 : sts[ch] = hCPE->hCoreCoder[ch];
2125 4772 : ms_ptr[ch] = &logNoiseEst[ch][0];
2126 4772 : lr_ptr[ch] = &sts[ch]->hFdCngDec->hFdCngCom->sidNoiseEst[0];
2127 : }
2128 :
2129 : /* decode noise shapes and gains */
2130 7158 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
2131 : {
2132 4772 : sts[ch] = hCPE->hCoreCoder[ch];
2133 4772 : hFdCngCom = ( sts[ch]->hFdCngDec )->hFdCngCom;
2134 4772 : N = hFdCngCom->npart;
2135 4772 : hFdCngCom->sid_frame_counter++;
2136 :
2137 4772 : if ( ch )
2138 : {
2139 2386 : stages = FD_CNG_JOINT_stages_25bits;
2140 : }
2141 : else
2142 : {
2143 2386 : stages = FD_CNG_stages_37bits;
2144 : }
2145 :
2146 : /* read bitstream */
2147 28632 : for ( i = 0; i < stages; i++ )
2148 : {
2149 23860 : indices[i] = get_next_indice( sts[ch], bits_37bits[i] );
2150 : }
2151 : {
2152 4772 : gain[ch] = ( (float) get_next_indice( sts[ch], 7 ) - GAIN_Q_OFFSET_IVAS ) / 1.5f;
2153 : }
2154 :
2155 : /* MSVQ decoder */
2156 4772 : msvq_dec( cdk_37bits_ivas, NULL, NULL, stages, N, FD_CNG_maxN_37bits, indices, 1, invTrfMatrix, ms_ptr[ch], NULL );
2157 : }
2158 :
2159 2386 : dtx_read_padding_bits( sts[1], ( IVAS_SID_5k2 - 4400 ) / FRAMES_PER_SEC );
2160 :
2161 2386 : if ( sts[0]->hFdCngDec->hFdCngCom->no_side_flag )
2162 : {
2163 75 : set_zero( ms_ptr[1], NPART );
2164 : }
2165 :
2166 2386 : if ( is_out_ms == 0 )
2167 : {
2168 2386 : inverseMS( N, ms_ptr[0], ms_ptr[1], 1.f );
2169 : }
2170 :
2171 7158 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
2172 : {
2173 4772 : hFdCngCom = sts[ch]->hFdCngDec->hFdCngCom;
2174 113840 : for ( p = 0; p < N; p++ )
2175 : {
2176 109068 : lr_ptr[ch][p] = powf( 10.f, ( ms_ptr[ch][p] + gain[ch] ) / 10.f );
2177 : }
2178 :
2179 4772 : scalebands( hFdCngCom->sidNoiseEst, hFdCngCom->part, hFdCngCom->npart, hFdCngCom->midband, hFdCngCom->nFFTpart, hFdCngCom->stopBand - hFdCngCom->startBand, hFdCngCom->cngNoiseLevel, 1 );
2180 :
2181 4772 : lpc_from_spectrum( hFdCngCom, hFdCngCom->startBand, hFdCngCom->stopFFTbin, sts[ch]->preemph_fac );
2182 : }
2183 :
2184 2386 : if ( hCPE->nchan_out == 1 && hCPE->last_element_brate <= IVAS_SID_5k2 )
2185 : {
2186 : /* create proper M noise shape in channel zero after gains have been applied */
2187 16730 : for ( p = 0; p < N; p++ )
2188 : {
2189 16038 : sts[0]->hFdCngDec->hFdCngCom->sidNoiseEst[p] = 0.5f * ( sts[0]->hFdCngDec->hFdCngCom->sidNoiseEst[p] + sts[1]->hFdCngDec->hFdCngCom->sidNoiseEst[p] );
2190 : }
2191 : }
2192 :
2193 2386 : return;
2194 : }
2195 :
2196 :
2197 : /*-------------------------------------------------------------------
2198 : * FdCngDecodeDiracMDCTStereoSID()
2199 : *
2200 : * Decode FD-CNG parameters for CNG in 2TC DirAC mode from the bitstream
2201 : *-------------------------------------------------------------------*/
2202 :
2203 1084 : void FdCngDecodeDiracMDCTStereoSID(
2204 : CPE_DEC_HANDLE hCPE /* i/o: CPE decoder state structure */
2205 : )
2206 : {
2207 : DEC_CORE_HANDLE sts[CPE_CHANNELS];
2208 : HANDLE_FD_CNG_COM hFdCngCom;
2209 : float *ms_ptr[CPE_CHANNELS];
2210 : float *lr_ptr[CPE_CHANNELS];
2211 : float logNoiseEst[CPE_CHANNELS][NPART];
2212 : float gain[CPE_CHANNELS];
2213 : int16_t indices[FD_CNG_stages_37bits];
2214 : int16_t N, i, ch, p;
2215 : float *invTrfMatrix;
2216 : float tmpRAM[FDCNG_VQ_MAX_LEN][FDCNG_VQ_DCT_MAXTRUNC];
2217 :
2218 :
2219 1084 : invTrfMatrix = (float *) tmpRAM; /* dynamically filled */
2220 1084 : create_IDCT_N_Matrix( invTrfMatrix, FDCNG_VQ_MAX_LEN, FDCNG_VQ_DCT_MAXTRUNC, sizeof( tmpRAM ) / ( sizeof( float ) ) );
2221 :
2222 3252 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
2223 : {
2224 2168 : sts[ch] = hCPE->hCoreCoder[ch];
2225 2168 : ms_ptr[ch] = &logNoiseEst[ch][0];
2226 2168 : lr_ptr[ch] = &sts[ch]->hFdCngDec->hFdCngCom->sidNoiseEst[0];
2227 2168 : ( sts[ch]->hFdCngDec )->hFdCngCom->sid_frame_counter++;
2228 : }
2229 :
2230 : /* decode noise shapes and gains */
2231 1084 : hFdCngCom = ( sts[0]->hFdCngDec )->hFdCngCom;
2232 1084 : N = hFdCngCom->npart;
2233 :
2234 : /* read bitstream */
2235 7588 : for ( i = 0; i < FD_CNG_stages_37bits; i++ )
2236 : {
2237 6504 : indices[i] = get_next_indice( sts[0], bits_37bits[i] );
2238 : }
2239 1084 : gain[0] = ( (float) get_next_indice( sts[0], 7 ) - GAIN_Q_OFFSET_IVAS ) / 1.5f;
2240 1084 : gain[1] = gain[0];
2241 :
2242 : /* MSVQ decoder */
2243 1084 : msvq_dec( cdk_37bits_ivas, NULL, NULL, FD_CNG_stages_37bits, N, FD_CNG_maxN_37bits, indices, 1, invTrfMatrix, ms_ptr[0], NULL );
2244 1084 : mvr2r( ms_ptr[0], ms_ptr[1], N );
2245 :
2246 : /*inverseMS( N, ms_ptr[0], ms_ptr[1], 1.f );*/
2247 :
2248 3252 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
2249 : {
2250 2168 : hFdCngCom = sts[ch]->hFdCngDec->hFdCngCom;
2251 53402 : for ( p = 0; p < N; p++ )
2252 : {
2253 51234 : lr_ptr[ch][p] = powf( 10.f, ( ms_ptr[ch][p] + gain[ch] ) / 10.f );
2254 : }
2255 :
2256 : /* NB last band energy compensation */
2257 2168 : if ( hFdCngCom->CngBandwidth == NB )
2258 : {
2259 0 : lr_ptr[ch][N - 1] *= NB_LAST_BAND_SCALE;
2260 : }
2261 2168 : else if ( hFdCngCom->CngBandwidth == SWB && hFdCngCom->CngBitrate <= ACELP_13k20 )
2262 : {
2263 0 : lr_ptr[ch][N - 1] *= SWB_13k2_LAST_BAND_SCALE;
2264 : }
2265 :
2266 2168 : scalebands( hFdCngCom->sidNoiseEst, hFdCngCom->part, hFdCngCom->npart, hFdCngCom->midband, hFdCngCom->nFFTpart, hFdCngCom->stopBand - hFdCngCom->startBand, hFdCngCom->cngNoiseLevel, 1 );
2267 :
2268 2168 : lpc_from_spectrum( hFdCngCom, hFdCngCom->startBand, hFdCngCom->stopFFTbin, sts[ch]->preemph_fac );
2269 : }
2270 1084 : sts[0]->hFdCngDec->hFdCngCom->coherence = 0.0f;
2271 1084 : sts[1]->hFdCngDec->hFdCngCom->coherence = 0.0f;
2272 :
2273 1084 : if ( hCPE->nchan_out == 1 )
2274 : {
2275 : /* create proper M noise shape in channel zero after gains have been applied */
2276 0 : for ( p = 0; p < N; p++ )
2277 : {
2278 0 : sts[0]->hFdCngDec->hFdCngCom->sidNoiseEst[p] = 0.5f * ( sts[0]->hFdCngDec->hFdCngCom->sidNoiseEst[p] + sts[1]->hFdCngDec->hFdCngCom->sidNoiseEst[p] );
2279 : }
2280 : }
2281 :
2282 1084 : return;
2283 : }
|