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