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 18618 : 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 18618 : 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 18618 : *hFdCngCom = hs;
78 :
79 18618 : return IVAS_ERR_OK;
80 : }
81 :
82 :
83 : /*-------------------------------------------------------------------
84 : * initFdCngCom()
85 : *
86 : *
87 : *-------------------------------------------------------------------*/
88 :
89 18618 : 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 18618 : hFdCngCom->scalingFactor = 1 / ( scale * scale * 8.f );
95 :
96 : /* Initialize the overlap-add */
97 18618 : set_f( hFdCngCom->timeDomainBuffer, 0.0f, L_FRAME16k );
98 18618 : set_f( hFdCngCom->olapBufferAna, 0.0f, FFTLEN );
99 18618 : set_f( hFdCngCom->olapBufferSynth, 0.0f, FFTLEN );
100 18618 : set_f( hFdCngCom->olapBufferSynth2, 0.0f, FFTLEN );
101 :
102 : /* Initialize the comfort noise generation */
103 18618 : set_f( hFdCngCom->fftBuffer, 0.0f, FFTLEN );
104 18618 : set_f( hFdCngCom->cngNoiseLevel, 0.0f, FFTCLDFBLEN );
105 :
106 : /* Initialize quantizer */
107 18618 : set_f( hFdCngCom->sidNoiseEst, 0.0f, NPART );
108 18618 : set_f( hFdCngCom->A_cng, 0.0f, M + 1 );
109 18618 : hFdCngCom->A_cng[0] = 1.f;
110 :
111 : /* Set some counters and flags */
112 18618 : hFdCngCom->inactive_frame_counter = 0; /* Either SID or zero frames */
113 18618 : hFdCngCom->active_frame_counter = 0;
114 18618 : hFdCngCom->frame_type_previous = ACTIVE_FRAME;
115 18618 : hFdCngCom->flag_noisy_speech = 0;
116 18618 : hFdCngCom->likelihood_noisy_speech = 0.f;
117 18618 : hFdCngCom->numCoreBands = 0;
118 18618 : hFdCngCom->stopBand = 0;
119 18618 : hFdCngCom->startBand = 0;
120 18618 : hFdCngCom->stopFFTbin = 0;
121 18618 : hFdCngCom->frameSize = 0;
122 18618 : hFdCngCom->fftlen = 0;
123 18618 : hFdCngCom->seed = 0;
124 18618 : hFdCngCom->seed2 = 1;
125 18618 : hFdCngCom->seed3 = 2;
126 18618 : hFdCngCom->CngBitrate = -1;
127 :
128 : /* Initialize noise estimation algorithm */
129 18618 : set_f( hFdCngCom->periodog, 0.0f, PERIODOGLEN );
130 18618 : mhvals( MSNUMSUBFR * MSSUBFRLEN, &( hFdCngCom->msM_win ) );
131 18618 : mhvals( MSSUBFRLEN, &( hFdCngCom->msM_subwin ) );
132 18618 : set_f( hFdCngCom->msPeriodogSum, 0.0f, 2 );
133 18618 : set_f( hFdCngCom->msPsdSum, 0.0f, 2 );
134 18618 : set_f( hFdCngCom->msSlope, 0.0f, 2 );
135 18618 : set_f( hFdCngCom->msQeqInvAv, 0.0f, 2 );
136 18618 : hFdCngCom->msFrCnt_init_counter = 0;
137 18618 : hFdCngCom->msFrCnt_init_thresh = 1;
138 18618 : hFdCngCom->init_old = 0;
139 18618 : hFdCngCom->offsetflag = 0;
140 18618 : hFdCngCom->msFrCnt = MSSUBFRLEN;
141 18618 : hFdCngCom->msMinBufferPtr = 0;
142 18618 : set_f( hFdCngCom->msAlphaCor, 0.3f, 2 );
143 :
144 18618 : set_f( hFdCngCom->coherence, 0.5f, MDCT_ST_DTX_NUM_COHERENCE_BANDS );
145 :
146 18618 : return;
147 : }
148 :
149 :
150 : /*-------------------------------------------------------------------
151 : * deleteFdCngCom()
152 : *
153 : * Delete an instance of type FD_CNG_COM
154 : *-------------------------------------------------------------------*/
155 :
156 18618 : void deleteFdCngCom(
157 : HANDLE_FD_CNG_COM *hFdCngCom /* i/o: FD_CNG structure containing all buffers and variables */
158 : )
159 : {
160 18618 : HANDLE_FD_CNG_COM hsCom = *hFdCngCom;
161 :
162 18618 : if ( hsCom != NULL )
163 : {
164 18618 : free( hsCom );
165 18618 : *hFdCngCom = NULL;
166 : }
167 :
168 18618 : return;
169 : }
170 :
171 :
172 : /*-------------------------------------------------------------------
173 : * initPartitions()
174 : *
175 : * Initialize the spectral partitioning
176 : *-------------------------------------------------------------------*/
177 :
178 2417683 : 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 2417683 : if ( part_in != NULL )
193 : {
194 2417683 : if ( stopBandFR > startBand )
195 : {
196 1193613 : len_out = stopBandFR - startBand; /*part_out*/
197 46550907 : for ( i = 0; i < len_out; i++ )
198 : {
199 45357294 : part_out[i] = i;
200 : }
201 : }
202 : else
203 : {
204 1224070 : len_out = 0;
205 : } /*npart_in,part_out*/
206 78343525 : for ( j = 0; j < npart_in && part_in[j] < stopBand; j++ )
207 : {
208 75925842 : if ( part_in[j] >= stopBandFR && part_in[j] >= startBand )
209 : {
210 56828034 : 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 2417683 : *npart_out = len_out;
224 2417683 : getmidbands( part_out, len_out, midband, psize, psize_inv );
225 :
226 2417683 : return;
227 : }
228 :
229 :
230 : /*-------------------------------------------------------------------
231 : * compress_range()
232 : *
233 : * Apply some dynamic range compression based on the log
234 : *-------------------------------------------------------------------*/
235 :
236 181014 : void compress_range(
237 : float *in,
238 : float *out,
239 : const int16_t len )
240 : {
241 181014 : float *ptrIn = in;
242 181014 : float *ptrOut = out;
243 : int16_t i;
244 :
245 : /* out = log2( 1 + in ) */
246 7803042 : for ( i = 0; i < len; i++ )
247 : {
248 7622028 : *ptrOut = (float) log10( *ptrIn + 1.f );
249 7622028 : ptrIn++;
250 7622028 : ptrOut++;
251 : }
252 181014 : v_multc( out, 1.f / (float) log10( 2.f ), out, len );
253 :
254 : /* Quantize to simulate a fixed-point representation 6Q9 */
255 181014 : v_multc( out, CNG_LOG_SCALING, out, len );
256 7803042 : for ( ptrOut = out; ptrOut < out + len; ptrOut++ )
257 : {
258 7622028 : *ptrOut = (float) ( (int16_t) ( *ptrOut + 0.5f ) );
259 7622028 : if ( *ptrOut == 0.f )
260 : {
261 1252687 : *ptrOut = 1.f;
262 : }
263 : }
264 181014 : v_multc( out, 1. / CNG_LOG_SCALING, out, len );
265 :
266 181014 : return;
267 : }
268 :
269 :
270 : /*-------------------------------------------------------------------
271 : * expand_range()
272 : *
273 : * Apply some dynamic range expansion to undo the compression
274 : *-------------------------------------------------------------------*/
275 :
276 181014 : void expand_range(
277 : float *in,
278 : float *out,
279 : const int16_t len )
280 : {
281 181014 : float *ptrIn = in;
282 181014 : float *ptrOut = out;
283 : int16_t i;
284 :
285 : /* out = (2^(in) - 1) */
286 7803042 : for ( i = 0; i < len; i++ )
287 : {
288 7622028 : *ptrOut = (float) pow( 2.f, *ptrIn ) - 1.f;
289 7622028 : if ( *ptrOut < 0.0003385080526823181f )
290 : {
291 102914 : *ptrOut = 0.0003385080526823181f;
292 : }
293 7622028 : ptrIn++;
294 7622028 : ptrOut++;
295 : }
296 :
297 181014 : return;
298 : }
299 :
300 :
301 : /*-------------------------------------------------------------------
302 : * minimum_statistics()
303 : *
304 : * Noise estimation using Minimum Statistics (MS)
305 : *-------------------------------------------------------------------*/
306 :
307 181014 : 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 181014 : float msM_win = hFdCngCom->msM_win;
334 181014 : float msM_subwin = hFdCngCom->msM_subwin;
335 181014 : float *msPsdSum = hFdCngCom->msPsdSum;
336 181014 : float *msPeriodogSum = hFdCngCom->msPeriodogSum;
337 : float slope;
338 : float *ptr;
339 181014 : float msAlphaCorAlpha = MSALPHACORALPHA;
340 181014 : 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 181014 : int16_t len2 = MSNUMSUBFR * len;
348 : int16_t current_len;
349 : int16_t start, stop, cnt;
350 : int16_t totsize;
351 181014 : const float inv_buflen = 1.f / MSBUFLEN;
352 :
353 : /* No minimum statistics at initialization */
354 181014 : if ( hFdCngCom->msFrCnt_init_counter < hFdCngCom->msFrCnt_init_thresh )
355 : {
356 16406 : mvr2r( msPeriodog, msPsd, len );
357 16406 : mvr2r( msPeriodog, msNoiseFloor, len );
358 16406 : mvr2r( msPeriodog, msNoiseEst, len );
359 16406 : mvr2r( msPeriodog, msPsdFirstMoment, len );
360 16406 : set_f( msPsdSecondMoment, 0.0f, len );
361 16406 : msPeriodogSum[0] = dotp( msPeriodog, psize, lenFFT );
362 16406 : msPsdSum[0] = msPeriodogSum[0];
363 16406 : if ( lenFFT < len )
364 : {
365 3280 : msPeriodogSum[1] = dotp( msPeriodog + lenFFT, psize + lenFFT, len - lenFFT );
366 3280 : msPsdSum[1] = msPeriodogSum[1];
367 : }
368 :
369 : /* Increment frame counter at initialization */
370 : /* Some frames are sometimes zero at initialization => ignore them */
371 16406 : if ( msPeriodog[0] < hFdCngCom->init_old )
372 : {
373 3009 : set_f( msCurrentMinOut, FLT_MAX, len );
374 3009 : set_f( msCurrentMin, FLT_MAX, len );
375 3009 : set_f( msMinBuf, FLT_MAX, len2 );
376 3009 : set_f( msCurrentMinSubWindow, FLT_MAX, len );
377 3009 : hFdCngCom->msFrCnt_init_counter++;
378 : }
379 16406 : 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 164608 : start = 0;
388 164608 : stop = lenFFT;
389 164608 : totsize = hFdCngCom->stopFFTbin - hFdCngCom->startBand;
390 164608 : cnt = 0; /*msAlphaCor*/
391 418654 : while ( stop > start )
392 : {
393 254046 : current_len = stop - start;
394 :
395 : /* Compute smoothed correction factor for PSD smoothing */
396 254046 : msPeriodogSum[cnt] = dotp( msPeriodog + start, psize + start, current_len );
397 254046 : scalar = msPeriodogSum[cnt] * msPeriodogSum[cnt] + DELTA;
398 254046 : scalar2 = msPsdSum[cnt] - msPeriodogSum[cnt];
399 254046 : scalar = max( scalar / ( scalar + scalar2 * scalar2 ), MSALPHACORMAX );
400 254046 : hFdCngCom->msAlphaCor[cnt] = msAlphaCorAlpha * hFdCngCom->msAlphaCor[cnt] + msAlphaCorAlpha2 * scalar;
401 :
402 : /* Compute SNR */
403 254046 : snr = dotp( msNoiseFloor + start, psize + start, current_len );
404 254046 : snr = ( msPsdSum[cnt] + DELTA ) / ( snr + DELTA );
405 254046 : snr = (float) pow( snr, MSSNREXP );
406 254046 : msAlphaHatMin2 = min( MSALPHAHATMIN, snr );
407 254046 : scalar = MSALPHAMAX * hFdCngCom->msAlphaCor[cnt]; /*msAlpha,msPsd,msPeriodog,msNoiseFloor*/
408 6991038 : for ( j = start; j < stop; j++ )
409 : {
410 : /* Compute optimal smoothing parameter for PSD estimation */
411 6736992 : scalar2 = msNoiseFloor[j] + DELTA;
412 6736992 : scalar2 *= scalar2;
413 6736992 : scalar3 = msPsd[j] - msNoiseFloor[j];
414 6736992 : msAlpha[j] = max( ( scalar * scalar2 ) / ( scalar2 + scalar3 * scalar3 ), msAlphaHatMin2 );
415 :
416 : /* Compute the PSD (smoothed periodogram) in each band */
417 6736992 : msPsd[j] = msAlpha[j] * msPsd[j] + ( 1.f - msAlpha[j] ) * msPeriodog[j];
418 : }
419 254046 : msPsdSum[cnt] = dotp( msPsd + start, psize + start, current_len );
420 254046 : QeqInvAv = 0;
421 254046 : scalar = ( (float) ( MSNUMSUBFR * MSSUBFRLEN ) - 1.f ) * ( 1.f - msM_win );
422 254046 : scalar2 = ( (float) MSSUBFRLEN - 1.f ) * ( 1.f - msM_subwin ); /*msAlpha,msPsd,msPsdFirstMoment,msPsdSecondMoment,msNoiseFloor,msBminSubWin,msBminWin,psize*/
423 6991038 : for ( j = start; j < stop; j++ )
424 : {
425 : /* Compute variance of PSD */
426 6736992 : beta = min( msAlpha[j] * msAlpha[j], MSBETAMAX );
427 6736992 : scalar3 = msPsd[j] - msPsdFirstMoment[j];
428 6736992 : msPsdFirstMoment[j] = beta * msPsdFirstMoment[j] + ( 1.f - beta ) * msPsd[j];
429 6736992 : msPsdSecondMoment[j] = beta * msPsdSecondMoment[j] + ( 1.f - beta ) * scalar3 * scalar3;
430 : /* Compute inverse of amount of degrees of freedom */
431 6736992 : QeqInv = min( ( msPsdSecondMoment[j] + DELTA ) / ( 2.f * msNoiseFloor[j] * msNoiseFloor[j] + DELTA ), MSQEQINVMAX );
432 6736992 : QeqInvAv += QeqInv * psize[j];
433 :
434 : /* Compute bias correction Bmin */
435 6736992 : msBminWin[j] = 1.f + scalar * QeqInv / ( 0.5f - msM_win * QeqInv );
436 6736992 : msBminSubWin[j] = 1.f + scalar2 * QeqInv / ( 0.5f - msM_subwin * QeqInv );
437 : }
438 254046 : QeqInvAv /= totsize;
439 254046 : hFdCngCom->msQeqInvAv[cnt] = QeqInvAv;
440 :
441 : /* New minimum? */
442 254046 : BminCorr = 1.f + MSAV * (float) sqrt( QeqInvAv ); /*msPsd,msBminWin,msNewMinFlag,msCurrentMin,msCurrentMinSubWindow*/
443 6991038 : for ( j = start; j < stop; j++ )
444 : {
445 6736992 : scalar = BminCorr * msPsd[j];
446 6736992 : scalar2 = scalar * msBminWin[j];
447 6736992 : if ( scalar2 < msCurrentMin[j] )
448 : {
449 3173921 : msNewMinFlag[j] = 1;
450 3173921 : msCurrentMin[j] = scalar2;
451 3173921 : msCurrentMinSubWindow[j] = scalar * msBminSubWin[j];
452 : }
453 : else
454 : {
455 3563071 : msNewMinFlag[j] = 0;
456 : }
457 : }
458 :
459 : /* This is used later to identify local minima */
460 254046 : if ( hFdCngCom->msFrCnt >= MSSUBFRLEN )
461 : {
462 22652 : i = 0;
463 53529 : while ( i < 3 )
464 : {
465 45290 : if ( hFdCngCom->msQeqInvAv[cnt] < msQeqInvAv_thresh[i] )
466 : {
467 14413 : break;
468 : }
469 : else
470 : {
471 30877 : i++;
472 : }
473 : }
474 22652 : hFdCngCom->msSlope[cnt] = msNoiseSlopeMax[i];
475 : }
476 :
477 : /* Consider the FFT and CLDFB bands separately */
478 254046 : start = stop;
479 254046 : stop = len;
480 254046 : totsize = hFdCngCom->stopBand - hFdCngCom->stopFFTbin;
481 254046 : cnt++;
482 : } /*while (stop > start)*/
483 :
484 : /* Update minimum between sub windows */
485 164608 : if ( hFdCngCom->msFrCnt > 1 && hFdCngCom->msFrCnt < MSSUBFRLEN )
486 : {
487 : /*msNewMinFlag,msCurrentMinSubWindow,msCurrentMinOut*/
488 5633489 : for ( j = 0; j < len; j++ )
489 : {
490 5498491 : if ( msNewMinFlag[j] > 0 )
491 : {
492 2289015 : msLocalMinFlag[j] = 1;
493 : }
494 5498491 : if ( msCurrentMinSubWindow[j] < msCurrentMinOut[j] )
495 : {
496 1014311 : msCurrentMinOut[j] = msCurrentMinSubWindow[j];
497 : }
498 : }
499 : /* Get the current noise floor */
500 134998 : mvr2r( msCurrentMinOut, msNoiseFloor, len );
501 : }
502 :
503 : /* sub window complete */
504 : else
505 : {
506 29610 : if ( hFdCngCom->msFrCnt >= MSSUBFRLEN )
507 : {
508 : /* Collect buffers */
509 14942 : mvr2r( msCurrentMinSubWindow, msMinBuf + len * hFdCngCom->msMinBufferPtr, len );
510 :
511 : /* Compute minimum among all buffers */
512 14942 : mvr2r( msMinBuf, msCurrentMinOut, len );
513 14942 : ptr = msMinBuf + len;
514 89652 : for ( i = 1; i < MSNUMSUBFR; i++ )
515 : {
516 : /*msCurrentMinOut*/
517 3210200 : for ( j = 0; j < len; j++ )
518 : {
519 3135490 : if ( *ptr < msCurrentMinOut[j] )
520 : {
521 715517 : msCurrentMinOut[j] = *ptr;
522 : }
523 3135490 : ptr++;
524 : }
525 : }
526 :
527 : /* Take over local minima */
528 14942 : slope = hFdCngCom->msSlope[0]; /*msLocalMinFlag,msNewMinFlag,msCurrentMinSubWindow,msCurrentMinOut*/
529 642040 : for ( j = 0; j < len; j++ )
530 : {
531 627098 : if ( j == lenFFT )
532 : {
533 7710 : slope = hFdCngCom->msSlope[1];
534 : }
535 627098 : if ( msLocalMinFlag[j] && !msNewMinFlag[j] &&
536 286345 : msCurrentMinSubWindow[j] < slope * msCurrentMinOut[j] &&
537 174256 : msCurrentMinSubWindow[j] > msCurrentMinOut[j] )
538 : {
539 59240 : msCurrentMinOut[j] = msCurrentMinSubWindow[j];
540 59240 : i = j;
541 414680 : for ( k = 0; k < MSNUMSUBFR; k++ )
542 : {
543 355440 : msMinBuf[i] = msCurrentMinOut[j];
544 355440 : i += len;
545 : }
546 : }
547 : }
548 :
549 : /* Reset */
550 14942 : set_s( msLocalMinFlag, 0, len );
551 14942 : set_f( msCurrentMin, FLT_MAX, len );
552 :
553 : /* Get the current noise floor */
554 14942 : mvr2r( msCurrentMinOut, msNoiseFloor, len );
555 : }
556 : }
557 :
558 : /* Detect sudden offsets based on the FFT bins (core bandwidth) */
559 164608 : if ( msPsdSum[0] > 50.f * msPeriodogSum[0] )
560 : {
561 392 : if ( hFdCngCom->offsetflag > 0 )
562 : {
563 130 : mvr2r( msPeriodog, msPsd, len );
564 130 : mvr2r( msPeriodog, msCurrentMinOut, len );
565 130 : set_f( hFdCngCom->msAlphaCor, 1.0f, cnt );
566 130 : set_f( msAlpha, 0.0f, len );
567 130 : mvr2r( msPeriodog, msPsdFirstMoment, len );
568 130 : set_f( msPsdSecondMoment, 0.0f, len );
569 130 : msPsdSum[0] = dotp( msPeriodog, psize, lenFFT );
570 130 : if ( lenFFT < len )
571 : {
572 15 : msPsdSum[1] = dotp( msPeriodog + lenFFT, psize + lenFFT, len - lenFFT );
573 : }
574 : }
575 392 : hFdCngCom->offsetflag = 1;
576 : }
577 : else
578 : {
579 164216 : hFdCngCom->offsetflag = 0;
580 : }
581 :
582 : /* Increment frame counter */
583 164608 : if ( hFdCngCom->msFrCnt == MSSUBFRLEN )
584 : {
585 14942 : hFdCngCom->msFrCnt = 1;
586 14942 : hFdCngCom->msMinBufferPtr++;
587 14942 : if ( hFdCngCom->msMinBufferPtr == MSNUMSUBFR )
588 : {
589 1912 : hFdCngCom->msMinBufferPtr = 0;
590 : }
591 : }
592 : else
593 : {
594 149666 : ( hFdCngCom->msFrCnt )++;
595 : }
596 :
597 : /* Smooth noise estimate during CNG phases */ /*msNoiseEst,msNoiseFloor*/
598 6901600 : for ( j = 0; j < len; j++ )
599 : {
600 6736992 : msNoiseEst[j] = 0.95f * msNoiseEst[j] + 0.05f * msNoiseFloor[j];
601 : }
602 : }
603 :
604 181014 : 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 181014 : mvr2r( msPeriodog, msPeriodogBuf + len * ( *msPeriodogBufPtr ), len );
611 :
612 181014 : ( *msPeriodogBufPtr )++;
613 181014 : if ( ( *msPeriodogBufPtr ) == MSBUFLEN )
614 : {
615 34590 : ( *msPeriodogBufPtr ) = 0;
616 : }
617 :
618 : /* Upper limit the noise floors with the averaged input energy */ /*msNoiseEst*/
619 7803042 : for ( j = 0; j < len; j++ )
620 : {
621 7622028 : scalar = msPeriodogBuf[j];
622 38110140 : for ( i = j + len; i < MSBUFLEN * len; i += len )
623 : {
624 30488112 : scalar += msPeriodogBuf[i];
625 : } /*division by a constant = multiplication by its (constant) inverse */
626 7622028 : scalar *= inv_buflen;
627 7622028 : if ( msNoiseEst[j] > scalar )
628 : {
629 2355327 : msNoiseEst[j] = scalar;
630 : }
631 7622028 : assert( msNoiseEst[j] >= 0 );
632 : }
633 :
634 181014 : return;
635 : }
636 :
637 :
638 : /*-------------------------------------------------------------------
639 : * apply_scale()
640 : *
641 : * Apply bitrate-dependent scale
642 : *-------------------------------------------------------------------*/
643 :
644 310798 : 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 3411918 : for ( i = 0; i < scaleTableSize; i++ )
655 : {
656 3411918 : if ( ( bwidth == scaleTable[i].bwmode ) &&
657 1436644 : ( brate >= scaleTable[i].bitrateFrom ) &&
658 1436644 : ( brate < scaleTable[i].bitrateTo ) )
659 : {
660 310798 : break;
661 : }
662 : }
663 :
664 310798 : assert( i < scaleTableSize );
665 :
666 310798 : *scale += scaleTable[i].scale;
667 :
668 310798 : return;
669 : }
670 :
671 :
672 : /*-------------------------------------------------------------------
673 : * bandcombinepow()
674 : *
675 : * Compute the power for each partition
676 : *-------------------------------------------------------------------*/
677 :
678 191044 : 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 191044 : if ( nband == npart )
691 : {
692 0 : mvr2r( bandpow, partpow, nband );
693 : }
694 : else
695 : {
696 : /* Compute the power in each partition */
697 191044 : i = 0; /*part,partpow,psize_inv*/
698 6387159 : for ( p = 0; p < npart; p++ )
699 : {
700 : /* Arithmetic averaging of power for all bins in partition */
701 6196115 : temp = 0;
702 37102519 : for ( ; i <= part[p]; i++ )
703 : {
704 30906404 : temp += bandpow[i];
705 : }
706 6196115 : partpow[p] = temp * psize_inv[p];
707 : }
708 : }
709 :
710 191044 : return;
711 : }
712 :
713 :
714 : /*-------------------------------------------------------------------
715 : * scalebands()
716 : *
717 : * Scale partitions (with smoothing)
718 : *-------------------------------------------------------------------*/
719 :
720 196969 : 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 196969 : int16_t i, j = 0, nint, startBand, startPart, stopPart;
731 196969 : float val, delta = 0.f;
732 :
733 : /* Interpolate the bin/band-wise levels from the partition levels */
734 196969 : if ( nband == npart )
735 : {
736 0 : mvr2r( partpow, bandpow, npart );
737 : }
738 : else
739 : {
740 196969 : startBand = 0;
741 196969 : startPart = 0;
742 196969 : stopPart = nFFTpart;
743 398993 : while ( startBand < nband )
744 : {
745 202024 : if ( flag_fft_en || startPart >= nFFTpart )
746 : {
747 :
748 : /* first half partition */
749 200588 : j = startPart;
750 200588 : val = partpow[j];
751 417796 : for ( i = startBand; i <= midband[j]; i++ )
752 : {
753 217208 : bandpow[i] = val;
754 : }
755 200588 : j++;
756 :
757 200588 : delta = 1;
758 : /* inner partitions */
759 11852467 : for ( ; j < stopPart; j++ )
760 : {
761 11651879 : nint = midband[j] - midband[j - 1];
762 : /* log-linear interpolation */ /* Only one new LOG needs to be computed per loop iteration */
763 11651879 : delta = (float) exp( ( log( partpow[j] + DELTA ) - log( partpow[j - 1] + DELTA ) ) * normReciprocal[nint] );
764 11651879 : val = partpow[j - 1];
765 54558045 : for ( ; i < midband[j]; i++ )
766 : {
767 42906166 : val *= delta;
768 42906166 : bandpow[i] = val;
769 : }
770 11651879 : bandpow[i++] = partpow[j];
771 : }
772 200588 : if ( delta > 1.f )
773 : {
774 28843 : delta = 1.f;
775 : }
776 :
777 : /* last half partition */
778 200588 : val = partpow[stopPart - 1];
779 1836141 : for ( ; i <= part[stopPart - 1]; i++ )
780 : {
781 1635553 : val *= delta;
782 1635553 : bandpow[i] = val;
783 : }
784 : }
785 202024 : startBand = part[stopPart - 1] + 1;
786 202024 : startPart = stopPart;
787 202024 : stopPart = npart;
788 : }
789 : }
790 :
791 196969 : return;
792 : }
793 :
794 :
795 : /*-------------------------------------------------------------------
796 : * getmidbands()
797 : *
798 : * Get central band for each partition
799 : *-------------------------------------------------------------------*/
800 :
801 2417683 : 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 2417683 : midband[0] = part[0];
813 2417683 : psize[0] = (float) part[0] + 1.f;
814 2417683 : psize_inv[0] = normReciprocal[part[0] + 1];
815 :
816 : /* inner partitions */ /*part,midband,psize_inv*/
817 102185328 : for ( j = 1; j < npart; j++ )
818 : {
819 99767645 : midband[j] = ( part[j - 1] + 1 + part[j] ) >> 1;
820 99767645 : psize[j] = (float) ( part[j] - part[j - 1] );
821 99767645 : psize_inv[j] = normReciprocal[part[j] - part[j - 1]];
822 : }
823 :
824 2417683 : return;
825 : }
826 :
827 :
828 : /*-------------------------------------------------------------------
829 : * AnalysisSTFT()
830 : *
831 : * STFT analysis filterbank
832 : *-------------------------------------------------------------------*/
833 :
834 185580 : 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 185580 : float *olapBuffer = hFdCngCom->olapBufferAna;
841 185580 : const float *olapWin = hFdCngCom->olapWinAna;
842 :
843 : /* Shift and cascade for overlap-add */
844 185580 : mvr2r( olapBuffer + hFdCngCom->frameSize, olapBuffer, hFdCngCom->fftlen - hFdCngCom->frameSize );
845 185580 : mvr2r( timeDomainInput, olapBuffer + hFdCngCom->fftlen - hFdCngCom->frameSize, hFdCngCom->frameSize );
846 :
847 : /* Window the signal */
848 185580 : v_mult( olapBuffer, olapWin, fftBuffer, hFdCngCom->fftlen );
849 :
850 : /* Perform FFT */
851 185580 : RFFTN( fftBuffer, hFdCngCom->fftSineTab, hFdCngCom->fftlen, -1 );
852 :
853 185580 : return;
854 : }
855 :
856 :
857 : /*-------------------------------------------------------------------
858 : * SynthesisSTFT()
859 : *
860 : * STFT synthesis filterbank
861 : *-------------------------------------------------------------------*/
862 :
863 111436 : 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 111436 : RFFTN( fftBuffer, hFdCngCom->fftSineTab, hFdCngCom->fftlen, 1 );
879 :
880 : /* Handle overlap in P/S domain for stereo */
881 111436 : if ( ( element_mode == IVAS_CPE_TD || element_mode == IVAS_CPE_DFT ) && nchan_out == 2 )
882 : {
883 916 : mvr2r( olapBuffer + 3 * hFdCngCom->frameSize / 4 - ( M + 1 ), buf, hFdCngCom->frameSize + M + 1 );
884 916 : set_f( olapBuffer, 0.0f, hFdCngCom->fftlen );
885 : }
886 : else
887 : {
888 110520 : mvr2r( olapBuffer + hFdCngCom->frameSize, olapBuffer, hFdCngCom->frameSize );
889 110520 : set_f( olapBuffer + hFdCngCom->frameSize, 0.0f, hFdCngCom->frameSize ); /*olapBuffer, fftBuffer, olapWin*/
890 : }
891 :
892 111436 : if ( tcx_transition )
893 : {
894 785182 : for ( i = 0; i < 5 * hFdCngCom->frameSize / 4; i++ )
895 : {
896 782880 : olapBuffer[i] = fftBuffer[i];
897 : }
898 : }
899 : else
900 : {
901 15413806 : for ( i = hFdCngCom->frameSize / 4; i < 3 * hFdCngCom->frameSize / 4; i++ )
902 : {
903 15304672 : olapBuffer[i] += fftBuffer[i] * olapWin[i - hFdCngCom->frameSize / 4];
904 : }
905 15413806 : for ( ; i < 5 * hFdCngCom->frameSize / 4; i++ )
906 : {
907 15304672 : olapBuffer[i] = fftBuffer[i];
908 : }
909 : }
910 15729260 : for ( ; i < 7 * hFdCngCom->frameSize / 4; i++ )
911 : {
912 15617824 : olapBuffer[i] = fftBuffer[i] * olapWin[i - 3 * hFdCngCom->frameSize / 4];
913 : }
914 :
915 7920348 : for ( ; i < hFdCngCom->fftlen; i++ )
916 : {
917 7808912 : olapBuffer[i] = 0;
918 : }
919 :
920 : /* Get time-domain signal */
921 111436 : v_multc( olapBuffer + hFdCngCom->frameSize / 4, (float) ( hFdCngCom->fftlen / 2 ), timeDomainOutput, hFdCngCom->frameSize );
922 :
923 : /* Get excitation */
924 111436 : if ( ( element_mode == IVAS_CPE_TD || element_mode == IVAS_CPE_DFT ) && nchan_out == 2 )
925 : {
926 118164 : for ( i = 0; i < hFdCngCom->frameSize / 2; i++ )
927 : {
928 117248 : buf[i + ( M + 1 )] += olapBuffer[i + hFdCngCom->frameSize / 4];
929 : }
930 916 : v_multc( buf, (float) ( hFdCngCom->fftlen / 2 ), buf, M + 1 + hFdCngCom->frameSize );
931 : }
932 : else
933 : {
934 110520 : v_multc( olapBuffer + hFdCngCom->frameSize / 4 - ( M + 1 ), (float) ( hFdCngCom->fftlen / 2 ), buf, M + 1 + hFdCngCom->frameSize );
935 : }
936 :
937 111436 : tmp = buf[0];
938 111436 : preemph( buf + 1, PREEMPH_FAC, M + hFdCngCom->frameSize, &tmp );
939 111436 : residu( hFdCngCom->A_cng, M, buf + 1 + M, hFdCngCom->exc_cng, hFdCngCom->frameSize );
940 :
941 111436 : return;
942 : }
943 :
944 :
945 : /*-------------------------------------------------------------------
946 : * SynthesisSTFT_dirac()
947 : *
948 : * STFT synthesis filterbank
949 : *-------------------------------------------------------------------*/
950 :
951 13977 : 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 13977 : RFFTN( fftBuffer, hFdCngCom->fftSineTab, hFdCngCom->fftlen, 1 );
965 :
966 : /* Handle overlap in P/S domain for stereo */
967 13977 : mvr2r( olapBuffer + hFdCngCom->frameSize, olapBuffer, hFdCngCom->frameSize );
968 13977 : set_f( olapBuffer + hFdCngCom->frameSize, 0.0f, hFdCngCom->frameSize ); /*olapBuffer, fftBuffer, olapWin*/
969 :
970 2132825 : for ( i = hFdCngCom->frameSize / 4; i < 3 * hFdCngCom->frameSize / 4; i++ )
971 : {
972 2118848 : olapBuffer[i] += fftBuffer[i] * olapWin[i - hFdCngCom->frameSize / 4];
973 : }
974 2132825 : for ( ; i < 5 * hFdCngCom->frameSize / 4; i++ )
975 : {
976 2118848 : olapBuffer[i] = fftBuffer[i];
977 : }
978 :
979 2132825 : for ( ; i < 7 * hFdCngCom->frameSize / 4; i++ )
980 : {
981 2118848 : olapBuffer[i] = fftBuffer[i];
982 : }
983 :
984 1073401 : for ( ; i < hFdCngCom->fftlen; i++ )
985 : {
986 1059424 : olapBuffer[i] = 0;
987 : }
988 :
989 : /* Get time-domain signal */
990 13977 : v_multc( olapBuffer + hFdCngCom->frameSize / 4, (float) ( hFdCngCom->fftlen / 2 ), timeDomainOutput, samples_out );
991 :
992 : /* Get excitation */
993 13977 : v_multc( olapBuffer + hFdCngCom->frameSize / 4 - ( M + 1 ), (float) ( hFdCngCom->fftlen / 2 ), buf, M + 1 + hFdCngCom->frameSize );
994 13977 : tmp = buf[0];
995 13977 : preemph( buf + 1, PREEMPH_FAC, M + hFdCngCom->frameSize, &tmp );
996 13977 : 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 13977 : if ( samples_out < hFdCngCom->frameSize )
1000 : {
1001 0 : mvr2r( olapBuffer + samples_out, olapBuffer + hFdCngCom->frameSize, 3 * hFdCngCom->frameSize / 4 );
1002 : }
1003 2132825 : for ( i = 5 * hFdCngCom->frameSize / 4; i < 7 * hFdCngCom->frameSize / 4; i++ )
1004 : {
1005 2118848 : olapBuffer[i] *= olapWin[i - 3 * hFdCngCom->frameSize / 4];
1006 : }
1007 :
1008 13977 : 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 37236 : static void mhvals(
1019 : const int16_t d,
1020 : float *m )
1021 : {
1022 : int16_t i, j;
1023 : float qi, qj, q;
1024 37236 : int16_t len = SIZE_SCALE_TABLE_CN;
1025 :
1026 37236 : i = 0;
1027 316506 : for ( i = 0; i < len; i++ )
1028 : {
1029 316506 : if ( d <= d_array[i] )
1030 : {
1031 37236 : break;
1032 : }
1033 : }
1034 37236 : if ( i == len )
1035 : {
1036 0 : i = len - 1;
1037 0 : j = i;
1038 : }
1039 : else
1040 : {
1041 37236 : j = i - 1;
1042 : }
1043 37236 : if ( d == d_array[i] )
1044 : {
1045 0 : *m = m_array[i];
1046 : }
1047 : else
1048 : {
1049 37236 : qj = (float) sqrt( (float) d_array[i - 1] ); /*interpolate using sqrt(d)*/
1050 37236 : qi = (float) sqrt( (float) d_array[i] );
1051 37236 : q = (float) sqrt( (float) d );
1052 37236 : *m = m_array[i] + ( qi * qj / q - qj ) * ( m_array[j] - m_array[i] ) / ( qi - qj );
1053 : }
1054 :
1055 37236 : return;
1056 : }
1057 :
1058 : /*-------------------------------------------------------------------
1059 : * rand_gauss()
1060 : *
1061 : * Random generator with Gaussian distribution with mean 0 and std 1
1062 : *-------------------------------------------------------------------*/
1063 :
1064 182897991 : float rand_gauss(
1065 : float *x,
1066 : int16_t *seed )
1067 : {
1068 : float temp;
1069 :
1070 182897991 : temp = (float) own_random( seed );
1071 182897991 : temp += (float) own_random( seed );
1072 182897991 : temp += (float) own_random( seed );
1073 182897991 : temp *= OUTMAX_INV;
1074 :
1075 182897991 : *x = temp;
1076 :
1077 182897991 : return temp;
1078 : }
1079 :
1080 :
1081 : /*-------------------------------------------------------------------
1082 : * lpc_from_spectrum()
1083 : *
1084 : *
1085 : *-------------------------------------------------------------------*/
1086 :
1087 7252 : 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 7252 : float *powspec = hFdCngCom->cngNoiseLevel;
1098 7252 : int16_t fftlen = hFdCngCom->fftlen;
1099 7252 : const float *fftSineTab = hFdCngCom->fftSineTab;
1100 7252 : float *A = hFdCngCom->A_cng;
1101 :
1102 : /* Power Spectrum */
1103 7252 : ptr = fftBuffer;
1104 7252 : pti = fftBuffer + 1;
1105 7252 : nf = 1e-3f;
1106 21756 : for ( i = 0; i < start; i++ )
1107 : {
1108 14504 : *ptr = nf;
1109 14504 : *pti = 0.f;
1110 14504 : ptr += 2;
1111 14504 : pti += 2;
1112 : }
1113 2148396 : for ( ; i < stop; i++ )
1114 : {
1115 2141144 : *ptr = max( nf, powspec[i - start] );
1116 2141144 : *pti = 0.f;
1117 2141144 : ptr += 2;
1118 2141144 : pti += 2;
1119 : }
1120 7444 : for ( ; i < fftlen / 2; i++ )
1121 : {
1122 192 : *ptr = nf;
1123 192 : *pti = 0.f;
1124 192 : ptr += 2;
1125 192 : pti += 2;
1126 : }
1127 7252 : fftBuffer[1] = nf;
1128 :
1129 : /* Pre-emphasis */
1130 7252 : ptr = fftBuffer;
1131 2163092 : for ( i = 0; i < fftlen / 2; i++ )
1132 : {
1133 2155840 : *ptr *= ( 1.f + preemph_fac * preemph_fac - 2.0f * preemph_fac * (float) cos( -2.0f * EVS_PI * (float) i / (float) fftlen ) );
1134 :
1135 2155840 : ptr += 2;
1136 : }
1137 7252 : fftBuffer[1] *= ( 1.f + preemph_fac * preemph_fac + 2.0f * preemph_fac );
1138 :
1139 : /* Autocorrelation */
1140 7252 : RFFTN( fftBuffer, fftSineTab, fftlen, 1 );
1141 130536 : for ( i = 0; i <= M; i++ )
1142 : {
1143 123284 : r[i] = fftBuffer[i] * ( fftlen / 2 ) * ( fftlen / 2 );
1144 : }
1145 7252 : if ( r[0] < 100.f )
1146 : {
1147 616 : r[0] = 100.f;
1148 : }
1149 :
1150 7252 : r[0] *= 1.0005f;
1151 :
1152 : /* LPC */
1153 7252 : lev_dur( A, r, M, NULL );
1154 :
1155 7252 : return;
1156 : }
1157 :
1158 :
1159 : /*-------------------------------------------------------------------
1160 : * FdCng_exc()
1161 : *
1162 : * Generate FD-CNG as LP excitation
1163 : *-------------------------------------------------------------------*/
1164 :
1165 23388 : 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 23388 : *CNG_mode = -1;
1182 :
1183 : /*Get excitation */
1184 129830 : for ( i = 0; i < L_frame / L_SUBFR; i++ )
1185 : {
1186 106442 : mvr2r( hFdCngCom->A_cng, Aq + i * ( M + 1 ), M + 1 );
1187 : }
1188 :
1189 23388 : a2lsp_stab( Aq, lsp_new, lsp_old );
1190 :
1191 23388 : if ( first_CNG == 0 )
1192 : {
1193 10024 : mvr2r( lsp_old, lspCNG, M );
1194 : }
1195 :
1196 397596 : for ( i = 0; i < M; i++ )
1197 : {
1198 : /* AR low-pass filter */
1199 374208 : lspCNG[i] = CNG_ISF_FACT * lspCNG[i] + ( 1 - CNG_ISF_FACT ) * lsp_new[i];
1200 : }
1201 :
1202 23388 : if ( L_frame == L_FRAME16k )
1203 : {
1204 12890 : lsp2lsf( lsp_new, lsf_new, M, INT_FS_16k );
1205 : }
1206 : else
1207 : {
1208 10498 : lsp2lsf( lsp_new, lsf_new, M, INT_FS_12k8 );
1209 : }
1210 :
1211 23388 : mvr2r( hFdCngCom->exc_cng, exc, L_frame );
1212 23388 : mvr2r( hFdCngCom->exc_cng, exc2, L_frame );
1213 :
1214 23388 : if ( bwe_exc )
1215 : {
1216 17287 : if ( L_frame == L_FRAME )
1217 : {
1218 10498 : interp_code_5over2( exc2, bwe_exc, L_frame );
1219 : }
1220 : else
1221 : {
1222 6789 : interp_code_4over2( exc2, bwe_exc, L_frame );
1223 : }
1224 : }
1225 :
1226 23388 : return;
1227 : }
|