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 "ivas_cnst.h"
38 : #include <assert.h>
39 : #include <stdint.h>
40 : #include "options.h"
41 : #ifdef DEBUGGING
42 : #include "debug.h"
43 : #endif
44 : #include <math.h>
45 : #include "prot.h"
46 : #include "rom_com.h"
47 : #include "wmc_auto.h"
48 :
49 :
50 : /*-------------------------------------------------------------------
51 : * Local function prototypes
52 : *-------------------------------------------------------------------*/
53 :
54 : static void mhvals( const int16_t d, float *m );
55 :
56 : static void getmidbands( int16_t *part, const int16_t npart, int16_t *midband, float *psize, float *psize_inv );
57 :
58 :
59 : /*-------------------------------------------------------------------
60 : * createFdCngCom()
61 : *
62 : * Create an instance of type FD_CNG_COM
63 : *-------------------------------------------------------------------*/
64 :
65 307094 : ivas_error createFdCngCom(
66 : HANDLE_FD_CNG_COM *hFdCngCom /* i/o: FD_CNG structure containing all buffers and variables */
67 : )
68 : {
69 : HANDLE_FD_CNG_COM hs;
70 :
71 : /* Allocate memory */
72 307094 : if ( ( hs = (HANDLE_FD_CNG_COM) malloc( sizeof( FD_CNG_COM ) ) ) == NULL )
73 : {
74 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FD CNG COM" );
75 : }
76 :
77 307094 : *hFdCngCom = hs;
78 :
79 307094 : return IVAS_ERR_OK;
80 : }
81 :
82 :
83 : /*-------------------------------------------------------------------
84 : * initFdCngCom()
85 : *
86 : *
87 : *-------------------------------------------------------------------*/
88 :
89 307094 : void initFdCngCom(
90 : HANDLE_FD_CNG_COM hFdCngCom, /* i/o: FD_CNG structure containing all buffers and variables */
91 : const float scale )
92 : {
93 : /* Calculate FFT scaling factor */
94 307094 : hFdCngCom->scalingFactor = 1 / ( scale * scale * 8.f );
95 :
96 : /* Initialize the overlap-add */
97 307094 : set_f( hFdCngCom->timeDomainBuffer, 0.0f, L_FRAME16k );
98 307094 : set_f( hFdCngCom->olapBufferAna, 0.0f, FFTLEN );
99 307094 : set_f( hFdCngCom->olapBufferSynth, 0.0f, FFTLEN );
100 307094 : set_f( hFdCngCom->olapBufferSynth2, 0.0f, FFTLEN );
101 :
102 : /* Initialize the comfort noise generation */
103 307094 : set_f( hFdCngCom->fftBuffer, 0.0f, FFTLEN );
104 307094 : set_f( hFdCngCom->cngNoiseLevel, 0.0f, FFTCLDFBLEN );
105 :
106 : /* Initialize quantizer */
107 307094 : set_f( hFdCngCom->sidNoiseEst, 0.0f, NPART );
108 307094 : set_f( hFdCngCom->A_cng, 0.0f, M + 1 );
109 307094 : hFdCngCom->A_cng[0] = 1.f;
110 :
111 : /* Set some counters and flags */
112 307094 : hFdCngCom->inactive_frame_counter = 0; /* Either SID or zero frames */
113 307094 : hFdCngCom->active_frame_counter = 0;
114 307094 : hFdCngCom->frame_type_previous = ACTIVE_FRAME;
115 307094 : hFdCngCom->flag_noisy_speech = 0;
116 307094 : hFdCngCom->likelihood_noisy_speech = 0.f;
117 307094 : hFdCngCom->numCoreBands = 0;
118 307094 : hFdCngCom->stopBand = 0;
119 307094 : hFdCngCom->startBand = 0;
120 307094 : hFdCngCom->stopFFTbin = 0;
121 307094 : hFdCngCom->frameSize = 0;
122 307094 : hFdCngCom->fftlen = 0;
123 307094 : hFdCngCom->seed = 0;
124 307094 : hFdCngCom->seed2 = 1;
125 307094 : hFdCngCom->seed3 = 2;
126 307094 : hFdCngCom->CngBitrate = -1;
127 :
128 : /* Initialize noise estimation algorithm */
129 307094 : set_f( hFdCngCom->periodog, 0.0f, PERIODOGLEN );
130 307094 : mhvals( MSNUMSUBFR * MSSUBFRLEN, &( hFdCngCom->msM_win ) );
131 307094 : mhvals( MSSUBFRLEN, &( hFdCngCom->msM_subwin ) );
132 307094 : set_f( hFdCngCom->msPeriodogSum, 0.0f, 2 );
133 307094 : set_f( hFdCngCom->msPsdSum, 0.0f, 2 );
134 307094 : set_f( hFdCngCom->msSlope, 0.0f, 2 );
135 307094 : set_f( hFdCngCom->msQeqInvAv, 0.0f, 2 );
136 307094 : hFdCngCom->msFrCnt_init_counter = 0;
137 307094 : hFdCngCom->msFrCnt_init_thresh = 1;
138 307094 : hFdCngCom->init_old = 0;
139 307094 : hFdCngCom->offsetflag = 0;
140 307094 : hFdCngCom->msFrCnt = MSSUBFRLEN;
141 307094 : hFdCngCom->msMinBufferPtr = 0;
142 307094 : set_f( hFdCngCom->msAlphaCor, 0.3f, 2 );
143 :
144 307094 : set_f( hFdCngCom->coherence, 0.5f, MDCT_ST_DTX_NUM_COHERENCE_BANDS );
145 :
146 307094 : return;
147 : }
148 :
149 :
150 : /*-------------------------------------------------------------------
151 : * deleteFdCngCom()
152 : *
153 : * Delete an instance of type FD_CNG_COM
154 : *-------------------------------------------------------------------*/
155 :
156 307094 : void deleteFdCngCom(
157 : HANDLE_FD_CNG_COM *hFdCngCom /* i/o: FD_CNG structure containing all buffers and variables */
158 : )
159 : {
160 307094 : HANDLE_FD_CNG_COM hsCom = *hFdCngCom;
161 :
162 307094 : if ( hsCom != NULL )
163 : {
164 307094 : free( hsCom );
165 307094 : *hFdCngCom = NULL;
166 : }
167 :
168 307094 : return;
169 : }
170 :
171 :
172 : /*-------------------------------------------------------------------
173 : * initPartitions()
174 : *
175 : * Initialize the spectral partitioning
176 : *-------------------------------------------------------------------*/
177 :
178 38937886 : void initPartitions(
179 : const int16_t *part_in,
180 : const int16_t npart_in,
181 : const int16_t startBand,
182 : const int16_t stopBand,
183 : int16_t *part_out,
184 : int16_t *npart_out,
185 : int16_t *midband,
186 : float *psize,
187 : float *psize_inv,
188 : const int16_t stopBandFR )
189 : {
190 : int16_t i, j, len_out;
191 :
192 38937886 : if ( part_in != NULL )
193 : {
194 38937886 : if ( stopBandFR > startBand )
195 : {
196 19253948 : len_out = stopBandFR - startBand; /*part_out*/
197 750903972 : for ( i = 0; i < len_out; i++ )
198 : {
199 731650024 : part_out[i] = i;
200 : }
201 : }
202 : else
203 : {
204 19683938 : len_out = 0;
205 : } /*npart_in,part_out*/
206 1262205018 : for ( j = 0; j < npart_in && part_in[j] < stopBand; j++ )
207 : {
208 1223267132 : if ( part_in[j] >= stopBandFR && part_in[j] >= startBand )
209 : {
210 915203964 : part_out[len_out++] = part_in[j] - startBand;
211 : }
212 : }
213 : }
214 : else
215 : {
216 0 : len_out = stopBand - startBand; /*part_out*/
217 0 : for ( i = 0; i < len_out; i++ )
218 : {
219 0 : part_out[i] = i;
220 : }
221 : }
222 :
223 38937886 : *npart_out = len_out;
224 38937886 : getmidbands( part_out, len_out, midband, psize, psize_inv );
225 :
226 38937886 : return;
227 : }
228 :
229 :
230 : /*-------------------------------------------------------------------
231 : * compress_range()
232 : *
233 : * Apply some dynamic range compression based on the log
234 : *-------------------------------------------------------------------*/
235 :
236 4868431 : void compress_range(
237 : float *in,
238 : float *out,
239 : const int16_t len )
240 : {
241 4868431 : float *ptrIn = in;
242 4868431 : float *ptrOut = out;
243 : int16_t i;
244 :
245 : /* out = log2( 1 + in ) */
246 238649075 : for ( i = 0; i < len; i++ )
247 : {
248 233780644 : *ptrOut = (float) log10( *ptrIn + 1.f );
249 233780644 : ptrIn++;
250 233780644 : ptrOut++;
251 : }
252 4868431 : v_multc( out, 1.f / (float) log10( 2.f ), out, len );
253 :
254 : /* Quantize to simulate a fixed-point representation 6Q9 */
255 4868431 : v_multc( out, CNG_LOG_SCALING, out, len );
256 238649075 : for ( ptrOut = out; ptrOut < out + len; ptrOut++ )
257 : {
258 233780644 : *ptrOut = (float) ( (int16_t) ( *ptrOut + 0.5f ) );
259 233780644 : if ( *ptrOut == 0.f )
260 : {
261 109063299 : *ptrOut = 1.f;
262 : }
263 : }
264 4868431 : v_multc( out, 1. / CNG_LOG_SCALING, out, len );
265 :
266 4868431 : return;
267 : }
268 :
269 :
270 : /*-------------------------------------------------------------------
271 : * expand_range()
272 : *
273 : * Apply some dynamic range expansion to undo the compression
274 : *-------------------------------------------------------------------*/
275 :
276 4868440 : void expand_range(
277 : float *in,
278 : float *out,
279 : const int16_t len )
280 : {
281 4868440 : float *ptrIn = in;
282 4868440 : float *ptrOut = out;
283 : int16_t i;
284 :
285 : /* out = (2^(in) - 1) */
286 238649642 : for ( i = 0; i < len; i++ )
287 : {
288 233781202 : *ptrOut = (float) pow( 2.f, *ptrIn ) - 1.f;
289 233781202 : if ( *ptrOut < 0.0003385080526823181f )
290 : {
291 2760974 : *ptrOut = 0.0003385080526823181f;
292 : }
293 233781202 : ptrIn++;
294 233781202 : ptrOut++;
295 : }
296 :
297 4868440 : return;
298 : }
299 :
300 :
301 : /*-------------------------------------------------------------------
302 : * minimum_statistics()
303 : *
304 : * Noise estimation using Minimum Statistics (MS)
305 : *-------------------------------------------------------------------*/
306 :
307 4868431 : void minimum_statistics(
308 : const int16_t len, /* i : Vector length */
309 : const int16_t lenFFT, /* i : Length of the FFT part of the vectors */
310 : float *psize,
311 : float *msPeriodog, /* i : Periodograms */
312 : float *msNoiseFloor,
313 : float *msNoiseEst, /* o : Noise estimates */
314 : float *msAlpha,
315 : float *msPsd,
316 : float *msPsdFirstMoment,
317 : float *msPsdSecondMoment,
318 : float *msMinBuf,
319 : float *msBminWin,
320 : float *msBminSubWin,
321 : float *msCurrentMin,
322 : float *msCurrentMinOut,
323 : float *msCurrentMinSubWindow,
324 : int16_t *msLocalMinFlag,
325 : int16_t *msNewMinFlag,
326 : float *msPeriodogBuf,
327 : int16_t *msPeriodogBufPtr,
328 : HANDLE_FD_CNG_COM hFdCngCom, /* i/o: FD_CNG structure containing all buffers and variables */
329 : const int16_t enc_dec, /* i : encoder/decoder indicator */
330 : const int16_t element_mode /* i : IVAS element mode type */
331 : )
332 : {
333 4868431 : float msM_win = hFdCngCom->msM_win;
334 4868431 : float msM_subwin = hFdCngCom->msM_subwin;
335 4868431 : float *msPsdSum = hFdCngCom->msPsdSum;
336 4868431 : float *msPeriodogSum = hFdCngCom->msPeriodogSum;
337 : float slope;
338 : float *ptr;
339 4868431 : float msAlphaCorAlpha = MSALPHACORALPHA;
340 4868431 : float msAlphaCorAlpha2 = 1.f - MSALPHACORALPHA;
341 :
342 : int16_t i, j, k;
343 : float scalar, scalar2, scalar3;
344 : float snr, BminCorr, QeqInv, QeqInvAv;
345 : float beta;
346 : float msAlphaHatMin2;
347 4868431 : int16_t len2 = MSNUMSUBFR * len;
348 : int16_t current_len;
349 : int16_t start, stop, cnt;
350 : int16_t totsize;
351 4868431 : const float inv_buflen = 1.f / MSBUFLEN;
352 :
353 : /* No minimum statistics at initialization */
354 4868431 : if ( hFdCngCom->msFrCnt_init_counter < hFdCngCom->msFrCnt_init_thresh )
355 : {
356 391490 : mvr2r( msPeriodog, msPsd, len );
357 391490 : mvr2r( msPeriodog, msNoiseFloor, len );
358 391490 : mvr2r( msPeriodog, msNoiseEst, len );
359 391490 : mvr2r( msPeriodog, msPsdFirstMoment, len );
360 391490 : set_f( msPsdSecondMoment, 0.0f, len );
361 391490 : msPeriodogSum[0] = dotp( msPeriodog, psize, lenFFT );
362 391490 : msPsdSum[0] = msPeriodogSum[0];
363 391490 : if ( lenFFT < len )
364 : {
365 50083 : msPeriodogSum[1] = dotp( msPeriodog + lenFFT, psize + lenFFT, len - lenFFT );
366 50083 : msPsdSum[1] = msPeriodogSum[1];
367 : }
368 :
369 : /* Increment frame counter at initialization */
370 : /* Some frames are sometimes zero at initialization => ignore them */
371 391490 : if ( msPeriodog[0] < hFdCngCom->init_old )
372 : {
373 34305 : set_f( msCurrentMinOut, FLT_MAX, len );
374 34305 : set_f( msCurrentMin, FLT_MAX, len );
375 34305 : set_f( msMinBuf, FLT_MAX, len2 );
376 34305 : set_f( msCurrentMinSubWindow, FLT_MAX, len );
377 34305 : hFdCngCom->msFrCnt_init_counter++;
378 : }
379 391490 : hFdCngCom->init_old = msPeriodog[0];
380 : }
381 : else
382 : {
383 :
384 : /* Consider the FFT and CLDFB bands separately
385 : - first iteration for FFT bins,
386 : - second one for CLDFB bands in SWB mode */
387 4476941 : start = 0;
388 4476941 : stop = lenFFT;
389 4476941 : totsize = hFdCngCom->stopFFTbin - hFdCngCom->startBand;
390 4476941 : cnt = 0; /*msAlphaCor*/
391 10612291 : while ( stop > start )
392 : {
393 6135350 : current_len = stop - start;
394 :
395 : /* Compute smoothed correction factor for PSD smoothing */
396 6135350 : msPeriodogSum[cnt] = dotp( msPeriodog + start, psize + start, current_len );
397 6135350 : scalar = msPeriodogSum[cnt] * msPeriodogSum[cnt] + DELTA;
398 6135350 : scalar2 = msPsdSum[cnt] - msPeriodogSum[cnt];
399 6135350 : scalar = max( scalar / ( scalar + scalar2 * scalar2 ), MSALPHACORMAX );
400 6135350 : hFdCngCom->msAlphaCor[cnt] = msAlphaCorAlpha * hFdCngCom->msAlphaCor[cnt] + msAlphaCorAlpha2 * scalar;
401 :
402 : /* Compute SNR */
403 6135350 : snr = dotp( msNoiseFloor + start, psize + start, current_len );
404 6135350 : snr = ( msPsdSum[cnt] + DELTA ) / ( snr + DELTA );
405 6135350 : snr = (float) pow( snr, MSSNREXP );
406 6135350 : msAlphaHatMin2 = min( MSALPHAHATMIN, snr );
407 6135350 : scalar = MSALPHAMAX * hFdCngCom->msAlphaCor[cnt]; /*msAlpha,msPsd,msPeriodog,msNoiseFloor*/
408 217775240 : for ( j = start; j < stop; j++ )
409 : {
410 : /* Compute optimal smoothing parameter for PSD estimation */
411 211639890 : scalar2 = msNoiseFloor[j] + DELTA;
412 211639890 : scalar2 *= scalar2;
413 211639890 : scalar3 = msPsd[j] - msNoiseFloor[j];
414 211639890 : msAlpha[j] = max( ( scalar * scalar2 ) / ( scalar2 + scalar3 * scalar3 ), msAlphaHatMin2 );
415 :
416 : /* Compute the PSD (smoothed periodogram) in each band */
417 211639890 : msPsd[j] = msAlpha[j] * msPsd[j] + ( 1.f - msAlpha[j] ) * msPeriodog[j];
418 : }
419 6135350 : msPsdSum[cnt] = dotp( msPsd + start, psize + start, current_len );
420 6135350 : QeqInvAv = 0;
421 6135350 : scalar = ( (float) ( MSNUMSUBFR * MSSUBFRLEN ) - 1.f ) * ( 1.f - msM_win );
422 6135350 : scalar2 = ( (float) MSSUBFRLEN - 1.f ) * ( 1.f - msM_subwin ); /*msAlpha,msPsd,msPsdFirstMoment,msPsdSecondMoment,msNoiseFloor,msBminSubWin,msBminWin,psize*/
423 217775240 : for ( j = start; j < stop; j++ )
424 : {
425 : /* Compute variance of PSD */
426 211639890 : beta = min( msAlpha[j] * msAlpha[j], MSBETAMAX );
427 211639890 : scalar3 = msPsd[j] - msPsdFirstMoment[j];
428 211639890 : msPsdFirstMoment[j] = beta * msPsdFirstMoment[j] + ( 1.f - beta ) * msPsd[j];
429 211639890 : msPsdSecondMoment[j] = beta * msPsdSecondMoment[j] + ( 1.f - beta ) * scalar3 * scalar3;
430 : /* Compute inverse of amount of degrees of freedom */
431 211639890 : QeqInv = min( ( msPsdSecondMoment[j] + DELTA ) / ( 2.f * msNoiseFloor[j] * msNoiseFloor[j] + DELTA ), MSQEQINVMAX );
432 211639890 : QeqInvAv += QeqInv * psize[j];
433 :
434 : /* Compute bias correction Bmin */
435 211639890 : msBminWin[j] = 1.f + scalar * QeqInv / ( 0.5f - msM_win * QeqInv );
436 211639890 : msBminSubWin[j] = 1.f + scalar2 * QeqInv / ( 0.5f - msM_subwin * QeqInv );
437 : }
438 6135350 : QeqInvAv /= totsize;
439 6135350 : hFdCngCom->msQeqInvAv[cnt] = QeqInvAv;
440 :
441 : /* New minimum? */
442 6135350 : BminCorr = 1.f + MSAV * (float) sqrt( QeqInvAv ); /*msPsd,msBminWin,msNewMinFlag,msCurrentMin,msCurrentMinSubWindow*/
443 217775240 : for ( j = start; j < stop; j++ )
444 : {
445 211639890 : scalar = BminCorr * msPsd[j];
446 211639890 : scalar2 = scalar * msBminWin[j];
447 211639890 : if ( scalar2 < msCurrentMin[j] )
448 : {
449 113266692 : msNewMinFlag[j] = 1;
450 113266692 : msCurrentMin[j] = scalar2;
451 113266692 : msCurrentMinSubWindow[j] = scalar * msBminSubWin[j];
452 : }
453 : else
454 : {
455 98373198 : msNewMinFlag[j] = 0;
456 : }
457 : }
458 :
459 : /* This is used later to identify local minima */
460 6135350 : if ( hFdCngCom->msFrCnt >= MSSUBFRLEN )
461 : {
462 527334 : i = 0;
463 1225447 : while ( i < 3 )
464 : {
465 1034106 : if ( hFdCngCom->msQeqInvAv[cnt] < msQeqInvAv_thresh[i] )
466 : {
467 335993 : break;
468 : }
469 : else
470 : {
471 698113 : i++;
472 : }
473 : }
474 527334 : hFdCngCom->msSlope[cnt] = msNoiseSlopeMax[i];
475 : }
476 :
477 : /* Consider the FFT and CLDFB bands separately */
478 6135350 : start = stop;
479 6135350 : stop = len;
480 6135350 : totsize = hFdCngCom->stopBand - hFdCngCom->stopFFTbin;
481 6135350 : cnt++;
482 : } /*while (stop > start)*/
483 :
484 : /* Update minimum between sub windows */
485 4476941 : if ( hFdCngCom->msFrCnt > 1 && hFdCngCom->msFrCnt < MSSUBFRLEN )
486 : {
487 : /*msNewMinFlag,msCurrentMinSubWindow,msCurrentMinOut*/
488 178590949 : for ( j = 0; j < len; j++ )
489 : {
490 174885851 : if ( msNewMinFlag[j] > 0 )
491 : {
492 86543258 : msLocalMinFlag[j] = 1;
493 : }
494 174885851 : if ( msCurrentMinSubWindow[j] < msCurrentMinOut[j] )
495 : {
496 42936901 : msCurrentMinOut[j] = msCurrentMinSubWindow[j];
497 : }
498 : }
499 : /* Get the current noise floor */
500 3705098 : mvr2r( msCurrentMinOut, msNoiseFloor, len );
501 : }
502 :
503 : /* sub window complete */
504 : else
505 : {
506 771843 : if ( hFdCngCom->msFrCnt >= MSSUBFRLEN )
507 : {
508 : /* Collect buffers */
509 387730 : mvr2r( msCurrentMinSubWindow, msMinBuf + len * hFdCngCom->msMinBufferPtr, len );
510 :
511 : /* Compute minimum among all buffers */
512 387730 : mvr2r( msMinBuf, msCurrentMinOut, len );
513 387730 : ptr = msMinBuf + len;
514 2326380 : for ( i = 1; i < MSNUMSUBFR; i++ )
515 : {
516 : /*msCurrentMinOut*/
517 94365895 : for ( j = 0; j < len; j++ )
518 : {
519 92427245 : if ( *ptr < msCurrentMinOut[j] )
520 : {
521 26311947 : msCurrentMinOut[j] = *ptr;
522 : }
523 92427245 : ptr++;
524 : }
525 : }
526 :
527 : /* Take over local minima */
528 387730 : slope = hFdCngCom->msSlope[0]; /*msLocalMinFlag,msNewMinFlag,msCurrentMinSubWindow,msCurrentMinOut*/
529 18873179 : for ( j = 0; j < len; j++ )
530 : {
531 18485449 : if ( j == lenFFT )
532 : {
533 139604 : slope = hFdCngCom->msSlope[1];
534 : }
535 18485449 : if ( msLocalMinFlag[j] && !msNewMinFlag[j] &&
536 7241894 : msCurrentMinSubWindow[j] < slope * msCurrentMinOut[j] &&
537 4354556 : msCurrentMinSubWindow[j] > msCurrentMinOut[j] )
538 : {
539 1617890 : msCurrentMinOut[j] = msCurrentMinSubWindow[j];
540 1617890 : i = j;
541 11325230 : for ( k = 0; k < MSNUMSUBFR; k++ )
542 : {
543 9707340 : msMinBuf[i] = msCurrentMinOut[j];
544 9707340 : i += len;
545 : }
546 : }
547 : }
548 :
549 : /* Reset */
550 387730 : set_s( msLocalMinFlag, 0, len );
551 387730 : set_f( msCurrentMin, FLT_MAX, len );
552 :
553 : /* Get the current noise floor */
554 387730 : mvr2r( msCurrentMinOut, msNoiseFloor, len );
555 : }
556 : }
557 :
558 : /* Detect sudden offsets based on the FFT bins (core bandwidth) */
559 4476941 : if ( msPsdSum[0] > 50.f * msPeriodogSum[0] )
560 : {
561 8212 : if ( hFdCngCom->offsetflag > 0 )
562 : {
563 2543 : mvr2r( msPeriodog, msPsd, len );
564 2543 : mvr2r( msPeriodog, msCurrentMinOut, len );
565 2543 : set_f( hFdCngCom->msAlphaCor, 1.0f, cnt );
566 2543 : set_f( msAlpha, 0.0f, len );
567 2543 : mvr2r( msPeriodog, msPsdFirstMoment, len );
568 2543 : set_f( msPsdSecondMoment, 0.0f, len );
569 2543 : msPsdSum[0] = dotp( msPeriodog, psize, lenFFT );
570 2543 : if ( lenFFT < len )
571 : {
572 1004 : msPsdSum[1] = dotp( msPeriodog + lenFFT, psize + lenFFT, len - lenFFT );
573 : }
574 : }
575 8212 : hFdCngCom->offsetflag = 1;
576 : }
577 : else
578 : {
579 4468729 : hFdCngCom->offsetflag = 0;
580 : }
581 :
582 : /* Increment frame counter */
583 4476941 : if ( hFdCngCom->msFrCnt == MSSUBFRLEN )
584 : {
585 387730 : hFdCngCom->msFrCnt = 1;
586 387730 : hFdCngCom->msMinBufferPtr++;
587 387730 : if ( hFdCngCom->msMinBufferPtr == MSNUMSUBFR )
588 : {
589 58642 : hFdCngCom->msMinBufferPtr = 0;
590 : }
591 : }
592 : else
593 : {
594 4089211 : ( hFdCngCom->msFrCnt )++;
595 : }
596 :
597 : /* Smooth noise estimate during CNG phases */ /*msNoiseEst,msNoiseFloor*/
598 216116831 : for ( j = 0; j < len; j++ )
599 : {
600 211639890 : msNoiseEst[j] = 0.95f * msNoiseEst[j] + 0.05f * msNoiseFloor[j];
601 : }
602 : }
603 :
604 4868431 : if ( enc_dec == DEC && element_mode == IVAS_CPE_TD )
605 : {
606 0 : v_multc( msNoiseEst, 1.4125f, msNoiseEst, NPART_SHAPING );
607 : }
608 :
609 : /* Collect buffers */
610 4868431 : mvr2r( msPeriodog, msPeriodogBuf + len * ( *msPeriodogBufPtr ), len );
611 :
612 4868431 : ( *msPeriodogBufPtr )++;
613 4868431 : if ( ( *msPeriodogBufPtr ) == MSBUFLEN )
614 : {
615 945325 : ( *msPeriodogBufPtr ) = 0;
616 : }
617 :
618 : /* Upper limit the noise floors with the averaged input energy */ /*msNoiseEst*/
619 238649075 : for ( j = 0; j < len; j++ )
620 : {
621 233780644 : scalar = msPeriodogBuf[j];
622 1168903220 : for ( i = j + len; i < MSBUFLEN * len; i += len )
623 : {
624 935122576 : scalar += msPeriodogBuf[i];
625 : } /*division by a constant = multiplication by its (constant) inverse */
626 233780644 : scalar *= inv_buflen;
627 233780644 : if ( msNoiseEst[j] > scalar )
628 : {
629 107905366 : msNoiseEst[j] = scalar;
630 : }
631 233780644 : assert( msNoiseEst[j] >= 0 );
632 : }
633 :
634 4868431 : return;
635 : }
636 :
637 :
638 : /*-------------------------------------------------------------------
639 : * apply_scale()
640 : *
641 : * Apply bitrate-dependent scale
642 : *-------------------------------------------------------------------*/
643 :
644 7131246 : void apply_scale(
645 : float *scale, /* o : scalefactor */
646 : const int16_t bwidth, /* i : audio bandwidth */
647 : const int32_t brate, /* i : Bit rate */
648 : const SCALE_SETUP *scaleTable, /* i : Scale table */
649 : const int16_t scaleTableSize /* i : Size of scale table */
650 : )
651 : {
652 : int16_t i;
653 :
654 73036464 : for ( i = 0; i < scaleTableSize; i++ )
655 : {
656 73036464 : if ( ( bwidth == scaleTable[i].bwmode ) &&
657 32990971 : ( brate >= scaleTable[i].bitrateFrom ) &&
658 32990971 : ( brate < scaleTable[i].bitrateTo ) )
659 : {
660 7131246 : break;
661 : }
662 : }
663 :
664 7131246 : assert( i < scaleTableSize );
665 :
666 7131246 : *scale += scaleTable[i].scale;
667 :
668 7131246 : return;
669 : }
670 :
671 :
672 : /*-------------------------------------------------------------------
673 : * bandcombinepow()
674 : *
675 : * Compute the power for each partition
676 : *-------------------------------------------------------------------*/
677 :
678 5396374 : void bandcombinepow(
679 : const float *bandpow, /* i : Power for each band */
680 : const int16_t nband, /* i : Number of bands */
681 : int16_t *part, /* i : Partition upper boundaries (band indices starting from 0) */
682 : const int16_t npart, /* i : Number of partitions */
683 : const float *psize_inv, /* i : Inverse partition sizes */
684 : float *partpow /* o : Power for each partition */
685 : )
686 : {
687 : int16_t i, p;
688 : float temp;
689 :
690 5396374 : if ( nband == npart )
691 : {
692 0 : mvr2r( bandpow, partpow, nband );
693 : }
694 : else
695 : {
696 : /* Compute the power in each partition */
697 5396374 : i = 0; /*part,partpow,psize_inv*/
698 217809006 : for ( p = 0; p < npart; p++ )
699 : {
700 : /* Arithmetic averaging of power for all bins in partition */
701 212412632 : temp = 0;
702 1343766204 : for ( ; i <= part[p]; i++ )
703 : {
704 1131353572 : temp += bandpow[i];
705 : }
706 212412632 : partpow[p] = temp * psize_inv[p];
707 : }
708 : }
709 :
710 5396374 : return;
711 : }
712 :
713 :
714 : /*-------------------------------------------------------------------
715 : * scalebands()
716 : *
717 : * Scale partitions (with smoothing)
718 : *-------------------------------------------------------------------*/
719 :
720 5317806 : void scalebands(
721 : const float *partpow, /* i : Power for each partition */
722 : int16_t *part, /* i : Partition upper boundaries (band indices starting from 0) */
723 : const int16_t npart, /* i : Number of partitions */
724 : int16_t *midband, /* i : Central band of each partition */
725 : const int16_t nFFTpart, /* i : Number of FFT partitions */
726 : const int16_t nband, /* i : Number of bands */
727 : float *bandpow, /* o : Power for each band */
728 : const int16_t flag_fft_en )
729 : {
730 5317806 : int16_t i, j = 0, nint, startBand, startPart, stopPart;
731 5317806 : float val, delta = 0.f;
732 :
733 : /* Interpolate the bin/band-wise levels from the partition levels */
734 5317806 : if ( nband == npart )
735 : {
736 0 : mvr2r( partpow, bandpow, npart );
737 : }
738 : else
739 : {
740 5317806 : startBand = 0;
741 5317806 : startPart = 0;
742 5317806 : stopPart = nFFTpart;
743 10676376 : while ( startBand < nband )
744 : {
745 5358570 : if ( flag_fft_en || startPart >= nFFTpart )
746 : {
747 :
748 : /* first half partition */
749 5345852 : j = startPart;
750 5345852 : val = partpow[j];
751 10825901 : for ( i = startBand; i <= midband[j]; i++ )
752 : {
753 5480049 : bandpow[i] = val;
754 : }
755 5345852 : j++;
756 :
757 5345852 : delta = 1;
758 : /* inner partitions */
759 324441670 : for ( ; j < stopPart; j++ )
760 : {
761 319095818 : nint = midband[j] - midband[j - 1];
762 : /* log-linear interpolation */ /* Only one new LOG needs to be computed per loop iteration */
763 319095818 : delta = (float) exp( ( log( partpow[j] + DELTA ) - log( partpow[j - 1] + DELTA ) ) * normReciprocal[nint] );
764 319095818 : val = partpow[j - 1];
765 1506613355 : for ( ; i < midband[j]; i++ )
766 : {
767 1187517537 : val *= delta;
768 1187517537 : bandpow[i] = val;
769 : }
770 319095818 : bandpow[i++] = partpow[j];
771 : }
772 5345852 : if ( delta > 1.f )
773 : {
774 1004513 : delta = 1.f;
775 : }
776 :
777 : /* last half partition */
778 5345852 : val = partpow[stopPart - 1];
779 45459826 : for ( ; i <= part[stopPart - 1]; i++ )
780 : {
781 40113974 : val *= delta;
782 40113974 : bandpow[i] = val;
783 : }
784 : }
785 5358570 : startBand = part[stopPart - 1] + 1;
786 5358570 : startPart = stopPart;
787 5358570 : stopPart = npart;
788 : }
789 : }
790 :
791 5317806 : return;
792 : }
793 :
794 :
795 : /*-------------------------------------------------------------------
796 : * getmidbands()
797 : *
798 : * Get central band for each partition
799 : *-------------------------------------------------------------------*/
800 :
801 38937886 : static void getmidbands(
802 : int16_t *part, /* i : Partition upper boundaries (band indices starting from 0) */
803 : const int16_t npart, /* i : Number of partitions */
804 : int16_t *midband, /* o : Central band of each partition */
805 : float *psize, /* o : Partition sizes */
806 : float *psize_inv /* o : Inverse of partition sizes */
807 : )
808 : {
809 : int16_t j;
810 :
811 : /* first half partition */
812 38937886 : midband[0] = part[0];
813 38937886 : psize[0] = (float) part[0] + 1.f;
814 38937886 : psize_inv[0] = normReciprocal[part[0] + 1];
815 :
816 : /* inner partitions */ /*part,midband,psize_inv*/
817 1646853988 : for ( j = 1; j < npart; j++ )
818 : {
819 1607916102 : midband[j] = ( part[j - 1] + 1 + part[j] ) >> 1;
820 1607916102 : psize[j] = (float) ( part[j] - part[j - 1] );
821 1607916102 : psize_inv[j] = normReciprocal[part[j] - part[j - 1]];
822 : }
823 :
824 38937886 : return;
825 : }
826 :
827 :
828 : /*-------------------------------------------------------------------
829 : * AnalysisSTFT()
830 : *
831 : * STFT analysis filterbank
832 : *-------------------------------------------------------------------*/
833 :
834 4382564 : void AnalysisSTFT(
835 : const float *timeDomainInput,
836 : float *fftBuffer, /* o : FFT bins */
837 : HANDLE_FD_CNG_COM hFdCngCom /* i/o: FD_CNG structure containing all buffers and variables */
838 : )
839 : {
840 4382564 : float *olapBuffer = hFdCngCom->olapBufferAna;
841 4382564 : const float *olapWin = hFdCngCom->olapWinAna;
842 :
843 : /* Shift and cascade for overlap-add */
844 4382564 : mvr2r( olapBuffer + hFdCngCom->frameSize, olapBuffer, hFdCngCom->fftlen - hFdCngCom->frameSize );
845 4382564 : mvr2r( timeDomainInput, olapBuffer + hFdCngCom->fftlen - hFdCngCom->frameSize, hFdCngCom->frameSize );
846 :
847 : /* Window the signal */
848 4382564 : v_mult( olapBuffer, olapWin, fftBuffer, hFdCngCom->fftlen );
849 :
850 : /* Perform FFT */
851 4382564 : RFFTN( fftBuffer, hFdCngCom->fftSineTab, hFdCngCom->fftlen, -1 );
852 :
853 4382564 : return;
854 : }
855 :
856 :
857 : /*-------------------------------------------------------------------
858 : * SynthesisSTFT()
859 : *
860 : * STFT synthesis filterbank
861 : *-------------------------------------------------------------------*/
862 :
863 2974466 : void SynthesisSTFT(
864 : float *fftBuffer, /* i : FFT bins */
865 : float *timeDomainOutput,
866 : float *olapBuffer,
867 : const float *olapWin,
868 : const int16_t tcx_transition,
869 : HANDLE_FD_CNG_COM hFdCngCom, /* i/o: FD_CNG structure containing all buffers and variables */
870 : const int16_t element_mode, /* i : element mode */
871 : const int16_t nchan_out /* i : number of output channels */
872 : )
873 : {
874 : int16_t i;
875 : float buf[M + 1 + 320], tmp;
876 :
877 : /* Perform IFFT */
878 2974466 : RFFTN( fftBuffer, hFdCngCom->fftSineTab, hFdCngCom->fftlen, 1 );
879 :
880 : /* Handle overlap in P/S domain for stereo */
881 2974466 : if ( ( element_mode == IVAS_CPE_TD || element_mode == IVAS_CPE_DFT ) && nchan_out == 2 )
882 : {
883 3393 : mvr2r( olapBuffer + 3 * hFdCngCom->frameSize / 4 - ( M + 1 ), buf, hFdCngCom->frameSize + M + 1 );
884 3393 : set_f( olapBuffer, 0.0f, hFdCngCom->fftlen );
885 : }
886 : else
887 : {
888 2971073 : mvr2r( olapBuffer + hFdCngCom->frameSize, olapBuffer, hFdCngCom->frameSize );
889 2971073 : set_f( olapBuffer + hFdCngCom->frameSize, 0.0f, hFdCngCom->frameSize ); /*olapBuffer, fftBuffer, olapWin*/
890 : }
891 :
892 2974466 : if ( tcx_transition )
893 : {
894 5518146 : for ( i = 0; i < 5 * hFdCngCom->frameSize / 4; i++ )
895 : {
896 5502800 : olapBuffer[i] = fftBuffer[i];
897 : }
898 : }
899 : else
900 : {
901 441085648 : for ( i = hFdCngCom->frameSize / 4; i < 3 * hFdCngCom->frameSize / 4; i++ )
902 : {
903 438126528 : olapBuffer[i] += fftBuffer[i] * olapWin[i - hFdCngCom->frameSize / 4];
904 : }
905 441085648 : for ( ; i < 5 * hFdCngCom->frameSize / 4; i++ )
906 : {
907 438126528 : olapBuffer[i] = fftBuffer[i];
908 : }
909 : }
910 443302114 : for ( ; i < 7 * hFdCngCom->frameSize / 4; i++ )
911 : {
912 440327648 : olapBuffer[i] = fftBuffer[i] * olapWin[i - 3 * hFdCngCom->frameSize / 4];
913 : }
914 :
915 223138290 : for ( ; i < hFdCngCom->fftlen; i++ )
916 : {
917 220163824 : olapBuffer[i] = 0;
918 : }
919 :
920 : /* Get time-domain signal */
921 2974466 : v_multc( olapBuffer + hFdCngCom->frameSize / 4, (float) ( hFdCngCom->fftlen / 2 ), timeDomainOutput, hFdCngCom->frameSize );
922 :
923 : /* Get excitation */
924 2974466 : if ( ( element_mode == IVAS_CPE_TD || element_mode == IVAS_CPE_DFT ) && nchan_out == 2 )
925 : {
926 437697 : for ( i = 0; i < hFdCngCom->frameSize / 2; i++ )
927 : {
928 434304 : buf[i + ( M + 1 )] += olapBuffer[i + hFdCngCom->frameSize / 4];
929 : }
930 3393 : v_multc( buf, (float) ( hFdCngCom->fftlen / 2 ), buf, M + 1 + hFdCngCom->frameSize );
931 : }
932 : else
933 : {
934 2971073 : v_multc( olapBuffer + hFdCngCom->frameSize / 4 - ( M + 1 ), (float) ( hFdCngCom->fftlen / 2 ), buf, M + 1 + hFdCngCom->frameSize );
935 : }
936 :
937 2974466 : tmp = buf[0];
938 2974466 : preemph( buf + 1, PREEMPH_FAC, M + hFdCngCom->frameSize, &tmp );
939 2974466 : residu( hFdCngCom->A_cng, M, buf + 1 + M, hFdCngCom->exc_cng, hFdCngCom->frameSize );
940 :
941 2974466 : return;
942 : }
943 :
944 :
945 : /*-------------------------------------------------------------------
946 : * SynthesisSTFT_dirac()
947 : *
948 : * STFT synthesis filterbank
949 : *-------------------------------------------------------------------*/
950 :
951 156096 : void SynthesisSTFT_dirac(
952 : float *fftBuffer, /* i : FFT bins */
953 : float *timeDomainOutput,
954 : float *olapBuffer,
955 : const float *olapWin,
956 : const int16_t samples_out,
957 : HANDLE_FD_CNG_COM hFdCngCom /* i/o: FD_CNG structure containing all buffers and variables */
958 : )
959 : {
960 : int16_t i;
961 : float buf[M + 1 + 320], tmp;
962 :
963 : /* Perform IFFT */
964 156096 : RFFTN( fftBuffer, hFdCngCom->fftSineTab, hFdCngCom->fftlen, 1 );
965 :
966 : /* Handle overlap in P/S domain for stereo */
967 156096 : mvr2r( olapBuffer + hFdCngCom->frameSize, olapBuffer, hFdCngCom->frameSize );
968 156096 : set_f( olapBuffer + hFdCngCom->frameSize, 0.0f, hFdCngCom->frameSize ); /*olapBuffer, fftBuffer, olapWin*/
969 :
970 23069856 : for ( i = hFdCngCom->frameSize / 4; i < 3 * hFdCngCom->frameSize / 4; i++ )
971 : {
972 22913760 : olapBuffer[i] += fftBuffer[i] * olapWin[i - hFdCngCom->frameSize / 4];
973 : }
974 23069856 : for ( ; i < 5 * hFdCngCom->frameSize / 4; i++ )
975 : {
976 22913760 : olapBuffer[i] = fftBuffer[i];
977 : }
978 :
979 23069856 : for ( ; i < 7 * hFdCngCom->frameSize / 4; i++ )
980 : {
981 22913760 : olapBuffer[i] = fftBuffer[i];
982 : }
983 :
984 11612976 : for ( ; i < hFdCngCom->fftlen; i++ )
985 : {
986 11456880 : olapBuffer[i] = 0;
987 : }
988 :
989 : /* Get time-domain signal */
990 156096 : v_multc( olapBuffer + hFdCngCom->frameSize / 4, (float) ( hFdCngCom->fftlen / 2 ), timeDomainOutput, samples_out );
991 :
992 : /* Get excitation */
993 156096 : v_multc( olapBuffer + hFdCngCom->frameSize / 4 - ( M + 1 ), (float) ( hFdCngCom->fftlen / 2 ), buf, M + 1 + hFdCngCom->frameSize );
994 156096 : tmp = buf[0];
995 156096 : preemph( buf + 1, PREEMPH_FAC, M + hFdCngCom->frameSize, &tmp );
996 156096 : residu( hFdCngCom->A_cng, M, buf + 1 + M, hFdCngCom->exc_cng, hFdCngCom->frameSize );
997 :
998 : /* update and window olapBuf if we have a output frame that is shorter than the default frame size...*/
999 156096 : if ( samples_out < hFdCngCom->frameSize )
1000 : {
1001 45 : mvr2r( olapBuffer + samples_out, olapBuffer + hFdCngCom->frameSize, 3 * hFdCngCom->frameSize / 4 );
1002 : }
1003 23069856 : for ( i = 5 * hFdCngCom->frameSize / 4; i < 7 * hFdCngCom->frameSize / 4; i++ )
1004 : {
1005 22913760 : olapBuffer[i] *= olapWin[i - 3 * hFdCngCom->frameSize / 4];
1006 : }
1007 :
1008 156096 : return;
1009 : }
1010 :
1011 :
1012 : /*-------------------------------------------------------------------
1013 : * mhvals()
1014 : *
1015 : * Compute some values used in the bias correction of the minimum statistics algorithm
1016 : *-------------------------------------------------------------------*/
1017 :
1018 614188 : static void mhvals(
1019 : const int16_t d,
1020 : float *m )
1021 : {
1022 : int16_t i, j;
1023 : float qi, qj, q;
1024 614188 : int16_t len = SIZE_SCALE_TABLE_CN;
1025 :
1026 614188 : i = 0;
1027 5220598 : for ( i = 0; i < len; i++ )
1028 : {
1029 5220598 : if ( d <= d_array[i] )
1030 : {
1031 614188 : break;
1032 : }
1033 : }
1034 614188 : if ( i == len )
1035 : {
1036 0 : i = len - 1;
1037 0 : j = i;
1038 : }
1039 : else
1040 : {
1041 614188 : j = i - 1;
1042 : }
1043 614188 : if ( d == d_array[i] )
1044 : {
1045 0 : *m = m_array[i];
1046 : }
1047 : else
1048 : {
1049 614188 : qj = (float) sqrt( (float) d_array[i - 1] ); /*interpolate using sqrt(d)*/
1050 614188 : qi = (float) sqrt( (float) d_array[i] );
1051 614188 : q = (float) sqrt( (float) d );
1052 614188 : *m = m_array[i] + ( qi * qj / q - qj ) * ( m_array[j] - m_array[i] ) / ( qi - qj );
1053 : }
1054 :
1055 614188 : return;
1056 : }
1057 :
1058 : /*-------------------------------------------------------------------
1059 : * rand_gauss()
1060 : *
1061 : * Random generator with Gaussian distribution with mean 0 and std 1
1062 : *-------------------------------------------------------------------*/
1063 :
1064 4429409492 : float rand_gauss(
1065 : float *x,
1066 : int16_t *seed )
1067 : {
1068 : float temp;
1069 :
1070 4429409492 : temp = (float) own_random( seed );
1071 4429409492 : temp += (float) own_random( seed );
1072 4429409492 : temp += (float) own_random( seed );
1073 4429409492 : temp *= OUTMAX_INV;
1074 :
1075 4429409492 : *x = temp;
1076 :
1077 4429409492 : return temp;
1078 : }
1079 :
1080 :
1081 : /*-------------------------------------------------------------------
1082 : * lpc_from_spectrum()
1083 : *
1084 : *
1085 : *-------------------------------------------------------------------*/
1086 :
1087 178672 : void lpc_from_spectrum(
1088 : HANDLE_FD_CNG_COM hFdCngCom,
1089 : const int16_t start,
1090 : const int16_t stop,
1091 : const float preemph_fac )
1092 : {
1093 : int16_t i;
1094 : float r[32], nf;
1095 : float fftBuffer[FFTLEN], *ptr, *pti;
1096 :
1097 178672 : float *powspec = hFdCngCom->cngNoiseLevel;
1098 178672 : int16_t fftlen = hFdCngCom->fftlen;
1099 178672 : const float *fftSineTab = hFdCngCom->fftSineTab;
1100 178672 : float *A = hFdCngCom->A_cng;
1101 :
1102 : /* Power Spectrum */
1103 178672 : ptr = fftBuffer;
1104 178672 : pti = fftBuffer + 1;
1105 178672 : nf = 1e-3f;
1106 536016 : for ( i = 0; i < start; i++ )
1107 : {
1108 357344 : *ptr = nf;
1109 357344 : *pti = 0.f;
1110 357344 : ptr += 2;
1111 357344 : pti += 2;
1112 : }
1113 55068816 : for ( ; i < stop; i++ )
1114 : {
1115 54890144 : *ptr = max( nf, powspec[i - start] );
1116 54890144 : *pti = 0.f;
1117 54890144 : ptr += 2;
1118 54890144 : pti += 2;
1119 : }
1120 208816 : for ( ; i < fftlen / 2; i++ )
1121 : {
1122 30144 : *ptr = nf;
1123 30144 : *pti = 0.f;
1124 30144 : ptr += 2;
1125 30144 : pti += 2;
1126 : }
1127 178672 : fftBuffer[1] = nf;
1128 :
1129 : /* Pre-emphasis */
1130 178672 : ptr = fftBuffer;
1131 55456304 : for ( i = 0; i < fftlen / 2; i++ )
1132 : {
1133 55277632 : *ptr *= ( 1.f + preemph_fac * preemph_fac - 2.0f * preemph_fac * (float) cos( -2.0f * EVS_PI * (float) i / (float) fftlen ) );
1134 :
1135 55277632 : ptr += 2;
1136 : }
1137 178672 : fftBuffer[1] *= ( 1.f + preemph_fac * preemph_fac + 2.0f * preemph_fac );
1138 :
1139 : /* Autocorrelation */
1140 178672 : RFFTN( fftBuffer, fftSineTab, fftlen, 1 );
1141 3216096 : for ( i = 0; i <= M; i++ )
1142 : {
1143 3037424 : r[i] = fftBuffer[i] * ( fftlen / 2 ) * ( fftlen / 2 );
1144 : }
1145 178672 : if ( r[0] < 100.f )
1146 : {
1147 14710 : r[0] = 100.f;
1148 : }
1149 :
1150 178672 : r[0] *= 1.0005f;
1151 :
1152 : /* LPC */
1153 178672 : lev_dur( A, r, M, NULL );
1154 :
1155 178672 : return;
1156 : }
1157 :
1158 :
1159 : /*-------------------------------------------------------------------
1160 : * FdCng_exc()
1161 : *
1162 : * Generate FD-CNG as LP excitation
1163 : *-------------------------------------------------------------------*/
1164 :
1165 192255 : void FdCng_exc(
1166 : HANDLE_FD_CNG_COM hFdCngCom,
1167 : int16_t *CNG_mode,
1168 : const int16_t L_frame,
1169 : const float *lsp_old,
1170 : const int16_t first_CNG,
1171 : float *lspCNG,
1172 : float *Aq, /* o : LPC coeffs */
1173 : float *lsp_new, /* o : lsp */
1174 : float *lsf_new, /* o : lsf */
1175 : float *exc, /* o : LP excitation */
1176 : float *exc2, /* o : LP excitation */
1177 : float *bwe_exc /* o : LP excitation for BWE */
1178 : )
1179 : {
1180 : int16_t i;
1181 192255 : *CNG_mode = -1;
1182 :
1183 : /*Get excitation */
1184 1083049 : for ( i = 0; i < L_frame / L_SUBFR; i++ )
1185 : {
1186 890794 : mvr2r( hFdCngCom->A_cng, Aq + i * ( M + 1 ), M + 1 );
1187 : }
1188 :
1189 192255 : a2lsp_stab( Aq, lsp_new, lsp_old );
1190 :
1191 192255 : if ( first_CNG == 0 )
1192 : {
1193 86484 : mvr2r( lsp_old, lspCNG, M );
1194 : }
1195 :
1196 3268335 : for ( i = 0; i < M; i++ )
1197 : {
1198 : /* AR low-pass filter */
1199 3076080 : lspCNG[i] = CNG_ISF_FACT * lspCNG[i] + ( 1 - CNG_ISF_FACT ) * lsp_new[i];
1200 : }
1201 :
1202 192255 : if ( L_frame == L_FRAME16k )
1203 : {
1204 121774 : lsp2lsf( lsp_new, lsf_new, M, INT_FS_16k );
1205 : }
1206 : else
1207 : {
1208 70481 : lsp2lsf( lsp_new, lsf_new, M, INT_FS_12k8 );
1209 : }
1210 :
1211 192255 : mvr2r( hFdCngCom->exc_cng, exc, L_frame );
1212 192255 : mvr2r( hFdCngCom->exc_cng, exc2, L_frame );
1213 :
1214 192255 : if ( bwe_exc )
1215 : {
1216 146445 : if ( L_frame == L_FRAME )
1217 : {
1218 70481 : interp_code_5over2( exc2, bwe_exc, L_frame );
1219 : }
1220 : else
1221 : {
1222 75964 : interp_code_4over2( exc2, bwe_exc, L_frame );
1223 : }
1224 : }
1225 :
1226 192255 : return;
1227 : }
|