Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #include <math.h>
40 : #include "prot.h"
41 : #include "ivas_prot.h"
42 : #include "cnst.h"
43 : #include "stat_dec.h"
44 : #include "rom_com.h"
45 : #ifdef DEBUGGING
46 : #include "debug.h"
47 : #endif
48 : #include <assert.h>
49 : #include "wmc_auto.h"
50 :
51 : /*---------------------------------------------------------------------*
52 : * Local constants
53 : *---------------------------------------------------------------------*/
54 :
55 : #define NBPSF_L_EXTRA 120
56 : #define BPF_STOP_STOPBAND_16 16
57 :
58 : #define K_PC_DEC -0.0357f /* <45, 17> */
59 : #define C_PC_DEC 1.6071f
60 :
61 : /*---------------------------------------------------------------------*
62 : * Local function prototypes
63 : *---------------------------------------------------------------------*/
64 :
65 : static int16_t Pit_track( const float syn[], int16_t T );
66 :
67 : /*---------------------------------------------------------------------*
68 : * bass_psfilter_init()
69 : *
70 : * Initialisation of postfiltering variables
71 : *---------------------------------------------------------------------*/
72 :
73 197119 : void bass_psfilter_init(
74 : BPF_DEC_HANDLE hBPF /* o : BPF data handle */
75 : )
76 : {
77 : /* post-filter memories */
78 197119 : hBPF->pst_mem_deemp_err = 0.0f;
79 197119 : hBPF->pst_lp_ener = 0.0f;
80 197119 : set_f( hBPF->pst_old_syn, 0, NBPSF_PIT_MAX );
81 :
82 197119 : set_s( hBPF->Track_on_hist, 0, L_TRACK_HIST );
83 197119 : set_s( hBPF->vibrato_hist, 0, L_TRACK_HIST );
84 197119 : set_f( hBPF->mem_mean_pit, 80, L_TRACK_HIST );
85 197119 : hBPF->psf_att = 1.0f;
86 :
87 197119 : return;
88 : }
89 :
90 : /*---------------------------------------------------------------------*
91 : * bass_psfilter()
92 : *
93 : * Perform low-frequency postfiltering
94 : *---------------------------------------------------------------------*/
95 :
96 3755954 : void bass_psfilter(
97 : BPF_DEC_HANDLE hBPF, /* o : BPF data handle */
98 : const int16_t Opt_AMR_WB, /* i : AMR-WB IO flag */
99 : const float synth_in[], /* i : input synthesis (at 16kHz) */
100 : const int16_t L_frame, /* i : length of the last frame */
101 : const float pitch_buf[], /* i : pitch for every subfr [0,1,2,3] */
102 : const int16_t bpf_off, /* i : do not use BPF when set to 1 */
103 : float v_stab, /* i : stability factor */
104 : float *v_stab_smooth, /* i/o: smoothed stability factor */
105 : const int16_t coder_type, /* i : coder_type */
106 : float bpf_noise_buf[] /* o : BPF error signal (at int_fs) */
107 : )
108 : {
109 : int16_t i, i_subfr, T;
110 : float gain, alpha, corr, ener;
111 : float syn_buf[NBPSF_PIT_MAX + L_FRAME16k + NBPSF_PIT_MAX], *syn;
112 : float syn2_buf[L_FRAME16k], *syn2;
113 : float err[L_HALFR16k];
114 : int16_t T_sf[NB_SUBFR16k];
115 : int16_t nb_subfr, T_update;
116 : float delta_v_stab;
117 : int16_t dist_pit_diff, idx_pit_min, idx_pit_max, vibrato, Track_on;
118 : float loc_pit_max, loc_pit_min, diff_pit;
119 : float TrackOnR, vibratR, alp_tmp;
120 : int16_t subfr_len;
121 : int16_t nbpsf_pit_max;
122 :
123 3755954 : subfr_len = ( L_frame == L_FRAME8k ) ? STEREO_DFT_L_SUBFR_8k : L_SUBFR;
124 3755954 : nbpsf_pit_max = ( L_frame == L_FRAME8k ) ? STEREO_DFT_NBPSF_PIT_MAX_8k : NBPSF_PIT_MAX;
125 :
126 3755954 : Track_on = 0;
127 3755954 : T_update = 01;
128 3755954 : vibrato = 0;
129 3755954 : nb_subfr = L_frame / subfr_len;
130 :
131 : /*-------------------------------------------------------
132 : * Initialize pointers to various synthesis buffers
133 : *
134 : * |--------------------syn_buf--------------------------------|
135 : * |-----old_syn-----|-------synth_in--------|----extrapol---- |
136 : * |--NBPSF_PIT_MAX--| sf1 | sf2 | sf3 | sf4 |--NBPSF_PIT_MAX--|
137 : * |------syn2_buf---------|
138 : * |------L_frame----------|
139 : * |----bpf_noise_buf------|
140 : *
141 : *-------------------------------------------------------*/
142 :
143 3755954 : mvr2r( hBPF->pst_old_syn, syn_buf, nbpsf_pit_max );
144 3755954 : mvr2r( synth_in, syn_buf + nbpsf_pit_max, L_frame );
145 :
146 3755954 : if ( !( pitch_buf == NULL || bpf_off ) )
147 : {
148 32541640 : for ( i = L_TRACK_HIST - 1; i > 0; i-- )
149 : {
150 29287476 : hBPF->mem_mean_pit[i] = hBPF->mem_mean_pit[i - 1];
151 : }
152 :
153 3254164 : hBPF->mem_mean_pit[i] = mean( pitch_buf, nb_subfr );
154 : }
155 :
156 3755954 : idx_pit_min = minimum( hBPF->mem_mean_pit, L_TRACK_HIST, &loc_pit_min );
157 3755954 : idx_pit_max = maximum( hBPF->mem_mean_pit, L_TRACK_HIST, &loc_pit_max );
158 :
159 3755954 : dist_pit_diff = (int16_t) abs( idx_pit_max - idx_pit_min );
160 3755954 : diff_pit = loc_pit_max - loc_pit_min;
161 :
162 3755954 : if ( L_frame == L_FRAME16k || L_frame == L_FRAME8k )
163 : {
164 1882946 : diff_pit *= 0.8f;
165 : }
166 :
167 3755954 : if ( coder_type != INACTIVE && diff_pit >= 2.0f && diff_pit < 10 && dist_pit_diff >= 3 )
168 : {
169 213806 : vibrato = 1;
170 : }
171 :
172 3755954 : TrackOnR = (float) sum_s( hBPF->Track_on_hist, L_TRACK_HIST ) / L_TRACK_HIST;
173 3755954 : vibratR = (float) sum_s( hBPF->vibrato_hist, L_TRACK_HIST );
174 3755954 : alp_tmp = 1.0f - TrackOnR;
175 :
176 3755954 : if ( vibrato )
177 : {
178 213806 : vibratR = vibratR * vibratR * -0.009f + 1.0f;
179 213806 : alp_tmp *= vibratR;
180 : }
181 :
182 3755954 : alp_tmp = max( 0.1f, alp_tmp );
183 :
184 3755954 : if ( alp_tmp > hBPF->psf_att )
185 : {
186 3352303 : hBPF->psf_att = 0.05f * alp_tmp + 0.95f * hBPF->psf_att;
187 : }
188 : else
189 : {
190 403651 : hBPF->psf_att = 0.4f * alp_tmp + 0.6f * hBPF->psf_att;
191 : }
192 :
193 3755954 : if ( pitch_buf == NULL || bpf_off )
194 : {
195 : /* do not use BPF for HQ core */
196 501790 : T_update = L_frame == L_FRAME8k ? 40 : 80;
197 501790 : set_s( T_sf, 0, 5 );
198 501790 : syn = &syn_buf[nbpsf_pit_max + L_frame];
199 144468955 : for ( i = 0; i < nbpsf_pit_max; i++ )
200 : {
201 143967165 : syn[i] = syn[i - T_update];
202 : }
203 : }
204 : else
205 : {
206 : /* extrapolation of synth_in */
207 17837651 : for ( i = 0; i < nb_subfr; i++ )
208 : {
209 : /* copy subframe pitch values [1,2,3,4] of the current frame */
210 14583487 : T_sf[i] = ( L_frame == L_FRAME8k ) ? (int16_t) ( pitch_buf[i] / 2 + 0.5f ) : (int16_t) ( pitch_buf[i] + 0.5f );
211 :
212 14583487 : if ( L_frame == L_FRAME16k || L_frame == L_FRAME8k )
213 : {
214 : /* Safety check, should be very rare */
215 7834155 : if ( T_sf[i] > nbpsf_pit_max )
216 : {
217 0 : T_sf[i] = nbpsf_pit_max;
218 : }
219 : }
220 : else
221 : {
222 6749332 : if ( T_sf[i] > PIT_MAX )
223 : {
224 2708 : T_sf[i] = PIT_MAX;
225 : }
226 : }
227 : }
228 :
229 3254164 : T = T_sf[nb_subfr - 1];
230 3254164 : syn = &syn_buf[nbpsf_pit_max + L_frame];
231 927377009 : for ( i = 0; i < nbpsf_pit_max; i++ )
232 : {
233 924122845 : syn[i] = syn[i - T];
234 : }
235 : }
236 :
237 20661946 : for ( i_subfr = 0; i_subfr < L_frame; i_subfr += subfr_len )
238 : {
239 16905992 : T = T_sf[i_subfr / subfr_len];
240 :
241 : /* Pitch limitation already done in the previous loop */
242 16905992 : syn = &syn_buf[nbpsf_pit_max + i_subfr];
243 16905992 : syn2 = &syn2_buf[i_subfr];
244 :
245 16905992 : if ( T != 0 )
246 : {
247 14583487 : if ( T >= PIT_MIN && Opt_AMR_WB )
248 : {
249 25568 : T = Pit_track( syn, T );
250 :
251 25568 : if ( T != T_sf[i_subfr / L_SUBFR] )
252 : {
253 194 : Track_on = 1;
254 : }
255 : }
256 :
257 : /* symetric pitch prediction : phase is opposite between harmonic */
258 926315935 : for ( i = 0; i < subfr_len; i++ )
259 : {
260 911732448 : syn2[i] = 0.5f * ( syn[i - T] + syn[i + T] );
261 : }
262 :
263 : /* gain of prediction */
264 14583487 : corr = 0.0001f;
265 926315935 : for ( i = 0; i < subfr_len; i++ )
266 : {
267 911732448 : corr += syn[i] * syn2[i];
268 : }
269 :
270 14583487 : ener = 0.0001f;
271 926315935 : for ( i = 0; i < subfr_len; i++ )
272 : {
273 911732448 : ener += syn2[i] * syn2[i];
274 : }
275 14583487 : gain = corr / ener;
276 :
277 : /* error of prediction for noise estimator */
278 926315935 : for ( i = 0; i < subfr_len; i++ )
279 : {
280 911732448 : err[i] = syn[i] - ( gain * syn2[i] );
281 : }
282 :
283 : /* alpha = post-filtering factor (0=OFF, 1=100%) */
284 14583487 : ener = (float) ( ener + pow( 10.0, 0.1 * hBPF->pst_lp_ener ) ); /* limit alpha (post-filtering) when noise is high */
285 14583487 : alpha = corr / ener;
286 14583487 : if ( alpha > 0.5f )
287 : {
288 6650154 : alpha = 0.5f;
289 : }
290 :
291 14583487 : alpha *= ( hBPF->psf_att );
292 14583487 : if ( alpha > 0.3f && Track_on )
293 : {
294 83 : alpha = 0.3f;
295 : }
296 14583404 : else if ( alpha > 0.4f && vibrato )
297 : {
298 75379 : alpha = 0.4f;
299 : }
300 :
301 14583487 : if ( alpha < 0.0f )
302 : {
303 730667 : alpha = 0.0f;
304 : }
305 :
306 14583487 : *v_stab_smooth = 0.8f * v_stab + 0.2f * ( *v_stab_smooth );
307 14583487 : delta_v_stab = (float) fabs( *v_stab_smooth - v_stab );
308 14583487 : v_stab = (float) pow( v_stab, 0.5f );
309 14583487 : alpha = ( 1.0f + 0.15f * v_stab - 2.0f * delta_v_stab ) * alpha;
310 :
311 926315935 : for ( i = 0; i < subfr_len; i++ )
312 : {
313 911732448 : syn2[i] = alpha * ( syn[i] - syn2[i] );
314 : }
315 : }
316 :
317 : else
318 : {
319 : /* symetric pitch prediction : phase is opposite between harmonic */
320 149250345 : for ( i = 0; i < subfr_len; i++ )
321 : {
322 146927840 : syn2[i] = 0.5f * ( syn[i - T_update] + syn[i + T_update] );
323 : }
324 :
325 2322505 : if ( coder_type == AUDIO ) /* GSC mode without temporal component */
326 : {
327 33728 : Track_on = 1;
328 : }
329 : else
330 : {
331 2288777 : Track_on = 0;
332 : }
333 :
334 : /* gain of prediction */
335 2322505 : corr = 0.0001f;
336 149250345 : for ( i = 0; i < subfr_len; i++ )
337 : {
338 146927840 : corr += syn[i] * syn2[i];
339 : }
340 2322505 : ener = 0.0001f;
341 149250345 : for ( i = 0; i < subfr_len; i++ )
342 : {
343 146927840 : ener += syn2[i] * syn2[i];
344 : }
345 :
346 2322505 : gain = corr / ener;
347 :
348 : /* error of prediction for noise estimator */
349 149250345 : for ( i = 0; i < subfr_len; i++ )
350 : {
351 146927840 : err[i] = syn[i] - ( gain * syn2[i] );
352 : }
353 :
354 : /* alpha = post-filtering factor (0=OFF, 1=100%) */
355 2322505 : alpha = 0.0f;
356 :
357 2322505 : *v_stab_smooth = 0.8f * v_stab + 0.2f * ( *v_stab_smooth );
358 :
359 2322505 : set_f( syn2, 0.0f, subfr_len );
360 : }
361 :
362 : /* low-frequency noise estimator (lp_ener is average in dB) */
363 16905992 : deemph( err, 0.9f, subfr_len, &( hBPF->pst_mem_deemp_err ) ); /* +20dB at 50Hz */
364 :
365 16905992 : ener = 0.0001f;
366 1075566280 : for ( i = 0; i < subfr_len; i++ )
367 : {
368 1058660288 : ener += err[i] * err[i];
369 : }
370 16905992 : ener = (float) ( 10.0 * log10( ener ) );
371 16905992 : hBPF->pst_lp_ener = (float) ( 0.99f * hBPF->pst_lp_ener + 0.01f * ener );
372 :
373 : /* just write out the error signal */
374 16905992 : mvr2r( syn2, bpf_noise_buf + i_subfr, subfr_len );
375 : }
376 :
377 : /*-------------------------------------------------------*
378 : * update memory for next frame
379 : *-------------------------------------------------------*/
380 :
381 37559540 : for ( i = L_TRACK_HIST - 1; i > 0; i-- )
382 : {
383 33803586 : hBPF->Track_on_hist[i] = hBPF->Track_on_hist[i - 1];
384 33803586 : hBPF->vibrato_hist[i] = hBPF->vibrato_hist[i - 1];
385 : }
386 :
387 3755954 : hBPF->Track_on_hist[i] = Track_on;
388 3755954 : hBPF->vibrato_hist[i] = vibrato;
389 :
390 3755954 : mvr2r( syn_buf + L_frame, hBPF->pst_old_syn, nbpsf_pit_max );
391 :
392 3755954 : return;
393 : }
394 :
395 :
396 : /*---------------------------------------------------------------------*
397 : * Pit_track()
398 : *
399 : * Perform pitch tracking and test pitch/2 to avoid continuous pitch doubling
400 : *---------------------------------------------------------------------*/
401 :
402 : /*! r: Pitch */
403 25568 : static int16_t Pit_track(
404 : const float syn[], /* i : synthesis [-NBPSF_PIT_MAX..L_HALFR16k] */
405 : int16_t T /* i : pitch period (>= PIT_MIN) */
406 : )
407 : {
408 : int16_t i, T2;
409 : float tmp, corr, ener, cn;
410 : const float *v1, *v2;
411 :
412 : /*----------------------------------------------------------------*
413 : * Test pitch/2 to avoid continuous pitch doubling
414 : * (short pitch is limited to PIT_MIN (34 = 376Hz) by the encoder
415 : *----------------------------------------------------------------*/
416 :
417 25568 : T2 = T >> 1;
418 :
419 25568 : v1 = &syn[-NBPSF_L_EXTRA];
420 25568 : v2 = &syn[-T2 - NBPSF_L_EXTRA];
421 :
422 25568 : ener = 0.01f;
423 7184608 : for ( i = 0; i < L_HALFR16k + NBPSF_L_EXTRA; i++ )
424 : {
425 7159040 : ener += v1[i] * v1[i];
426 : }
427 25568 : corr = 0.01f;
428 7184608 : for ( i = 0; i < L_HALFR16k + NBPSF_L_EXTRA; i++ )
429 : {
430 7159040 : corr += v1[i] * v2[i];
431 : }
432 25568 : tmp = 0.01f;
433 7184608 : for ( i = 0; i < L_HALFR16k + NBPSF_L_EXTRA; i++ )
434 : {
435 7159040 : tmp += v2[i] * v2[i];
436 : }
437 :
438 : /* cn = normalized correlation of pitch/2 */
439 25568 : cn = corr / (float) sqrt( ener * tmp );
440 25568 : if ( cn > 0.95f )
441 : {
442 194 : T = T2;
443 : }
444 :
445 25568 : return ( T );
446 : }
447 :
448 :
449 : /*---------------------------------------------------------------------*
450 : * addBassPostFilter()
451 : *
452 : * Add BPF component in cldfb domain
453 : *---------------------------------------------------------------------*/
454 :
455 3632813 : void addBassPostFilter(
456 : const float *harm_timeIn,
457 : const int16_t samplesToProcess,
458 : float **rAnalysis,
459 : float **iAnalysis,
460 : HANDLE_CLDFB_FILTER_BANK cldfb )
461 : {
462 : float *tmp_R[CLDFB_NO_COL_MAX];
463 : float *tmp_I[CLDFB_NO_COL_MAX];
464 : float cldfbBufferReal[CLDFB_NO_COL_MAX][20];
465 : float cldfbBufferImag[CLDFB_NO_COL_MAX][20];
466 : int16_t i, b;
467 : int16_t maxBand;
468 : const float *weights;
469 3632813 : int16_t nCol = cldfb->no_col;
470 3632813 : int16_t nColToProcess = nCol;
471 3632813 : int16_t nChan = cldfb->no_channels;
472 :
473 3632813 : if ( samplesToProcess > -1 )
474 : {
475 805846 : nColToProcess = ( ( samplesToProcess + cldfb->no_channels - 1 ) / cldfb->no_channels );
476 : }
477 :
478 3632813 : assert( nCol == 16 );
479 :
480 3632813 : weights = bpf_weights_16;
481 :
482 3632813 : if ( nChan > BPF_STOP_STOPBAND_16 )
483 : {
484 1754736 : maxBand = BPF_STOP_STOPBAND_16;
485 : }
486 : else
487 : {
488 1878077 : maxBand = nChan;
489 : }
490 :
491 54580143 : for ( i = 0; i < nColToProcess; i++ )
492 : {
493 50947330 : tmp_R[i] = cldfbBufferReal[i];
494 50947330 : tmp_I[i] = cldfbBufferImag[i];
495 : }
496 :
497 : /* do the analysis of filtered signal */
498 3632813 : cldfbAnalysis( harm_timeIn, tmp_R, tmp_I, samplesToProcess, cldfb );
499 :
500 : /* now do the subtraction */
501 54580143 : for ( i = 0; i < nColToProcess; i++ )
502 : {
503 : /* loop over low frequency bands */
504 866104610 : for ( b = 0; b < maxBand; b++ )
505 : {
506 815157280 : rAnalysis[i][b] -= weights[b] * tmp_R[i][b];
507 815157280 : iAnalysis[i][b] -= weights[b] * tmp_I[i][b];
508 : }
509 : }
510 :
511 3632813 : return;
512 : }
513 :
514 : /*---------------------------------------------------------------------*
515 : * res_bpf_adapt()
516 : *
517 : * Analyze BPF output and decide if it should be applied on DFT stereo
518 : * residual signal
519 : *---------------------------------------------------------------------*/
520 :
521 : /*! r: Decision to enable or disable BPF on DFT stereo residual */
522 145770 : int16_t res_bpf_adapt(
523 : STEREO_DFT_DEC_DATA_HANDLE hStereoDft, /* i/o: DFT stereo decoder handle */
524 : const float *bpf_error_signal_8k, /* i : BPF modification signal */
525 : float res_buf[STEREO_DFT_N_8k] /* i : residual buffer */
526 : )
527 : {
528 : float error_nrg;
529 : float tmp;
530 : float res_hb_nrg;
531 : float bpf_error_ratio;
532 : int16_t res_bpf_flag;
533 : int16_t i;
534 : int16_t i_start;
535 : int16_t i_end;
536 : float bw_inv;
537 :
538 145770 : if ( hStereoDft->res_cod_band_max == 6 )
539 : {
540 0 : i_start = 39;
541 0 : i_end = 64;
542 0 : bw_inv = 0.04f; /* 1/(64 - 39) */
543 : }
544 : else
545 : {
546 145770 : i_start = 28;
547 145770 : i_end = 40;
548 145770 : bw_inv = 0.083f; /* 1/(40 - 28) */
549 : }
550 :
551 : /* Measure energy of high frequency band in MDCT domain */
552 145770 : res_hb_nrg = EPSILON;
553 1895010 : for ( i = i_start; i < i_end; i++ )
554 : {
555 1749240 : res_hb_nrg += res_buf[i] * res_buf[i];
556 : }
557 145770 : res_hb_nrg *= bw_inv;
558 145770 : res_hb_nrg = STEREO_DFT_BPF_ADAPT_ALPHA * res_hb_nrg + ( 1 - STEREO_DFT_BPF_ADAPT_ALPHA ) * hStereoDft->res_hb_nrg_mem;
559 145770 : hStereoDft->res_hb_nrg_mem = res_hb_nrg;
560 :
561 : /* Measure energy of discontinuities at subframe boundaries */
562 145770 : error_nrg = 0;
563 874620 : for ( i = 0; i < L_FRAME8k; i += STEREO_DFT_L_SUBFR_8k )
564 : {
565 728850 : tmp = bpf_error_signal_8k[i] - hStereoDft->bpf_error_signal_last;
566 728850 : error_nrg += tmp * tmp;
567 728850 : hStereoDft->bpf_error_signal_last = bpf_error_signal_8k[i + STEREO_DFT_L_SUBFR_8k - 1];
568 : }
569 145770 : error_nrg *= 0.2f; /* Division by 5 for average value */
570 145770 : bpf_error_ratio = min( 2, error_nrg / res_hb_nrg ); /* Form decision variable and apply limit */
571 145770 : bpf_error_ratio = STEREO_DFT_BPF_ADAPT_BETA * bpf_error_ratio + ( 1 - STEREO_DFT_BPF_ADAPT_BETA ) * hStereoDft->bpf_error_ratio_mem;
572 145770 : hStereoDft->bpf_error_ratio_mem = bpf_error_ratio;
573 :
574 145770 : res_bpf_flag = bpf_error_ratio < 1;
575 :
576 145770 : return res_bpf_flag;
577 : }
578 :
579 : /*---------------------------------------------------------------------*
580 : * bpf_pitch_coherence()
581 : *
582 : * Analyse pitch coherence
583 : *---------------------------------------------------------------------*/
584 3412069 : void bpf_pitch_coherence(
585 : Decoder_State *st, /* i/o: decoder state structure */
586 : const float pitch_buf[] /* i : pitch for every subfr [0,1,2,3] */
587 : )
588 : {
589 : int16_t nb_subfr;
590 : float pc, pcn1, pcn2, pcn3;
591 :
592 3412069 : nb_subfr = st->L_frame / L_SUBFR;
593 :
594 3412069 : if ( st->clas_dec > UNVOICED_CLAS && st->element_mode != EVS_MONO )
595 : {
596 2278712 : pc = (float) fabs( st->old_pitch_buf[nb_subfr + 3] + st->old_pitch_buf[nb_subfr + 2] - st->old_pitch_buf[nb_subfr] - st->old_pitch_buf[nb_subfr + 1] ) * 256.0f / (float) st->L_frame;
597 2278712 : pcn1 = K_PC_DEC * pc + C_PC_DEC;
598 2278712 : pcn1 = max( min( pcn1, 1.0f ), 0.0f );
599 :
600 2278712 : pc = (float) fabs( pitch_buf[nb_subfr - 1] + pitch_buf[nb_subfr - 2] - pitch_buf[1] - pitch_buf[0] ) * 256.0f / (float) st->L_frame;
601 2278712 : pcn2 = K_PC_DEC * pc + C_PC_DEC;
602 2278712 : pcn2 = max( min( pcn2, 1.0f ), 0.0f );
603 :
604 2278712 : pc = (float) fabs( st->old_pitch_buf[nb_subfr + 3] + st->old_pitch_buf[nb_subfr + 2] - pitch_buf[1] - pitch_buf[0] ) * 256.0f / (float) st->L_frame;
605 2278712 : pcn3 = K_PC_DEC * pc + C_PC_DEC;
606 2278712 : pcn3 = max( min( pcn3, 1.0f ), 0.0f );
607 :
608 2278712 : if ( pcn1 + pcn2 + pcn3 < 2.5f )
609 : {
610 566015 : st->hBPF->psf_att = 0.4f;
611 566015 : set_s( &st->hBPF->Track_on_hist[L_TRACK_HIST - nb_subfr], 1, nb_subfr );
612 : }
613 : }
614 :
615 3412069 : return;
616 : }
|