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 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include <math.h>
43 : #include "cnst.h"
44 : #include "rom_dec.h"
45 : #include "rom_com.h"
46 : #include "prot.h"
47 : #include "wmc_auto.h"
48 :
49 : /*---------------------------------------------------------------------*
50 : * Local prototypes
51 : *---------------------------------------------------------------------*/
52 :
53 : static int16_t FEC_phase_matching( HQ_NBFEC_HANDLE st, float *ImdctOut, float *auOut, float *OldauOut, float OldauOut_pha[2][N_LEAD_NB] );
54 :
55 : static void FEC_phase_matching_nextgood( const float *ImdctOut, float *auOut, float *OldauOut, float OldauOut_pha[2][N_LEAD_NB], float mean_en_high );
56 :
57 : static void FEC_phase_matching_burst( const float *ImdctOut, float *auOut, float *OldauOut, float OldauOut_pha[2][N_LEAD_NB], float *prev_oldauOut );
58 :
59 : static void Repetition_smoothing_nextgood( const float *ImdctOut, float *auOut, float *OldImdctOut, float *OldauOut, int16_t cur_data_use_flag, int16_t overlap_time );
60 :
61 : static int16_t Repetition_smoothing( const float *ImdctOut, float *auOut, float *OldImdctOut, float *OldauOut, const int16_t L, float *prev_oldauOut, int16_t overlap_time );
62 :
63 : static void Windowing_1st_NB( float *ImdctOutWin, const float *ImdctOut, const float *win, const float *smoothingWin, const int16_t smoothing_flag );
64 :
65 : static void Windowing_2nd_NB( float *ImdctOutWin, const float *ImdctOut, const float *win );
66 :
67 : static void common_overlapping( float *auOut, float *ImdctOutWin, float *OldauOut, const int16_t end1, const int16_t offset1, const int16_t start2, const int16_t end2, const int16_t offset_i2, const int16_t offset2 );
68 :
69 : static void Smoothing_vector_NB( const float OldauOutnoWin[], const float ImdctOutWin[], const float SmoothingWin[], float auOut[], const int16_t ol_size );
70 :
71 : static void Smoothing_vector_scaledown_NB( const float OldauOutnoWin[], const float ImdctOutWin[], const float SmoothingWin[], float auOut[], const int16_t ol_size );
72 :
73 :
74 : /*--------------------------------------------------------------------------*
75 : * Regression_Anal()
76 : *
77 : *
78 : *--------------------------------------------------------------------------*/
79 :
80 0 : static void Regression_Anal(
81 : const float *values, /* i : Previous values */
82 : float *r_p, /* o : Output r[a b] array : y=ax+b */
83 : const int16_t num_pgf /* i : Number of previous good frame */
84 : )
85 : {
86 : int16_t i;
87 : float aindex[MAX_PGF + 1], b_p[MAX_PGF + 1];
88 :
89 : /* Initialize */
90 0 : for ( i = 0; i < num_pgf + 1; i++ )
91 : {
92 0 : aindex[i] = 0.f;
93 0 : b_p[i] = 0.f;
94 : }
95 :
96 : /* [aindex[0] aindex[1]][r[0]]=[b[0]]*/
97 : /* [aindex[1] aindex[2]][r[1]] [b[1]]*/
98 : /* r[0] is the y-intercept(initial value). r[1] is slope*/
99 :
100 : /* r[0] = (b[0]a[2]-a[1]b[1])/(a[0]a[2]-a[1]a[1])
101 : r[1] = (b[0]a[1]-a[0]b[1])/(a[1]a[1]-a[0]a[2]) */
102 :
103 0 : aindex[0] = num_pgf;
104 0 : for ( i = 1; i < num_pgf + 1; i++ )
105 : {
106 0 : aindex[1] += (float) i;
107 0 : aindex[2] += ( (float) i * (float) i );
108 : }
109 : /* Calculate b[] */
110 0 : for ( i = 0; i < num_pgf; i++ )
111 : {
112 0 : b_p[0] += ( values[i] );
113 0 : b_p[1] += ( (float) ( num_pgf - i ) * values[i] );
114 : }
115 0 : r_p[0] = ( b_p[0] * aindex[2] - aindex[1] * b_p[1] ) / ( aindex[0] * aindex[2] - aindex[1] * aindex[1] );
116 0 : r_p[1] = ( b_p[0] * aindex[1] - aindex[0] * b_p[1] ) / ( aindex[1] * aindex[1] - aindex[0] * aindex[2] );
117 :
118 0 : return;
119 : }
120 :
121 :
122 : /*--------------------------------------------------------------------------*
123 : * FEC_scaling()
124 : *
125 : *
126 : *--------------------------------------------------------------------------*/
127 0 : static void FEC_scaling(
128 : float *old_coeffs, /* i/o: Pointer to old MDCT coeffs. */
129 : float *t_audio_q, /* o : MDCT coeffs. (for synthesis) */
130 : float *Norm_gain, /* i : Gain for Norm of each band */
131 : int16_t *HQ_FEC_seed, /* i/o: Seed for Ransom number Generator */
132 : int16_t nb_sfm, /* i : Number of sub-band */
133 : const int16_t *start_band,
134 : const int16_t *end_band )
135 : {
136 : int16_t i, j;
137 :
138 0 : for ( i = 0; i < RANDOM_START; i++ )
139 : {
140 0 : for ( j = start_band[i]; j < end_band[i]; j++ )
141 : {
142 0 : t_audio_q[j] = Norm_gain[i] * old_coeffs[j];
143 : }
144 : }
145 :
146 0 : for ( i = RANDOM_START; i < nb_sfm; i++ )
147 : {
148 0 : for ( j = start_band[i]; j < end_band[i]; j++ )
149 : {
150 0 : if ( (float) own_random( HQ_FEC_seed ) < 0.f )
151 : {
152 0 : t_audio_q[j] = Norm_gain[i] * ( -old_coeffs[j] );
153 : }
154 : else
155 : {
156 0 : t_audio_q[j] = Norm_gain[i] * old_coeffs[j];
157 : }
158 : }
159 : }
160 0 : return;
161 : }
162 :
163 :
164 : /*--------------------------------------------------------------------------*
165 : * HQ_FEC_processing()
166 : *
167 : *
168 : *--------------------------------------------------------------------------*/
169 :
170 0 : void HQ_FEC_processing(
171 : Decoder_State *st, /* i/o: decoder state structure */
172 : float *t_audio_q, /* o : MDCT coeffs. (for synthesis) */
173 : int16_t is_transient, /* i : Old flag for transient */
174 : float ynrm_values[][MAX_PGF], /* i : Old average Norm values for each group of bands */
175 : float r_p_values[][MAX_ROW], /* i : Computed y-intercept and slope by Regression */
176 : int16_t num_Sb, /* i : Number of sub-band group */
177 : int16_t nb_sfm, /* i : Number of sub-band */
178 : int16_t *Num_bands_p, /* i : Number of coeffs. for each sub-band */
179 : int16_t output_frame, /* i : Frame size */
180 : const int16_t *sfm_start, /* i : Start of bands */
181 : const int16_t *sfm_end /* i : End of bands */
182 : )
183 : {
184 : int16_t i, j, k;
185 : float energy_diff;
186 : int16_t mute_start, num_pgf;
187 : int16_t Num_sb_bwe;
188 : float tmp, norm_p[MAX_SB_NB];
189 : float *norm_values;
190 : float *r_p;
191 : int16_t sfm;
192 :
193 0 : HQ_DEC_HANDLE hHQ_core = st->hHQ_core;
194 0 : HQ_NBFEC_HANDLE hHQ_nbfec = st->hHQ_nbfec;
195 :
196 :
197 : /* Make sure 'energy_MA_Curr[0]' is not zero to prevent a 'div by 0' error. */
198 0 : if ( hHQ_nbfec->energy_MA_Curr[0] < 1.0f )
199 : {
200 0 : hHQ_nbfec->energy_MA_Curr[0] = 1.0f; /*It seems to be in Q0 the FxP*/
201 : }
202 : /* Decide the start frame number for adaptive muting */
203 : /* Normalized energy difference between the current frame and the moving average */
204 0 : energy_diff = (float) fabs( ( hHQ_nbfec->energy_MA_Curr[1] - hHQ_nbfec->energy_MA_Curr[0] ) / hHQ_nbfec->energy_MA_Curr[0] );
205 :
206 0 : if ( ( energy_diff < ED_THRES ) && ( is_transient == 0 ) ) /* First erasure frame */
207 : {
208 0 : mute_start = 5;
209 : }
210 : else
211 : {
212 0 : mute_start = 2;
213 : }
214 :
215 0 : if ( st->prev_old_bfi == 1 && st->nbLostCmpt == 1 && output_frame == L_FRAME8k )
216 : {
217 0 : st->nbLostCmpt++;
218 : }
219 :
220 : /* Frequency-domain FEC */
221 0 : if ( st->nbLostCmpt == 1 ) /* First erasure frame */
222 : {
223 0 : if ( is_transient == 0 )
224 : {
225 0 : if ( energy_diff < ED_THRES )
226 : {
227 0 : for ( i = 0; i < output_frame; i++ )
228 : {
229 0 : t_audio_q[i] = hHQ_nbfec->old_coeffs[i];
230 : }
231 : }
232 : else
233 : {
234 0 : for ( i = 0; i < output_frame; i++ )
235 : {
236 0 : hHQ_nbfec->old_coeffs[i] *= SCALE_DOWN_3dB;
237 0 : t_audio_q[i] = hHQ_nbfec->old_coeffs[i];
238 : }
239 : }
240 :
241 : /* Sign prediction in 4-dim bands up to 1.6 kHz*/
242 0 : if ( hHQ_core->old_is_transient[1] == 0 )
243 : {
244 0 : if ( hHQ_core->old_is_transient[2] == 0 )
245 : {
246 0 : for ( sfm = 0; sfm < HQ_FEC_SIGN_SFM; sfm++ )
247 : {
248 0 : if ( hHQ_nbfec->prev_sign_switch[sfm] >= HQ_FEC_SIGN_THRES )
249 : {
250 0 : for ( i = 0; i < HQ_FEC_BAND_SIZE; i++ )
251 : {
252 0 : t_audio_q[i + sfm * HQ_FEC_BAND_SIZE] *= -1.0f;
253 : }
254 : }
255 : }
256 : }
257 : else
258 : {
259 0 : for ( sfm = 0; sfm < HQ_FEC_SIGN_SFM; sfm++ )
260 : {
261 0 : if ( hHQ_nbfec->prev_sign_switch[sfm] >= HQ_FEC_SIGN_THRES_TRANS )
262 : {
263 0 : for ( i = 0; i < HQ_FEC_BAND_SIZE; i++ )
264 : {
265 0 : t_audio_q[i + sfm * HQ_FEC_BAND_SIZE] *= -1.0f;
266 : }
267 : }
268 : }
269 : }
270 : }
271 : else
272 : {
273 0 : for ( i = RANDOM_START * 8; i < output_frame; i++ )
274 : {
275 0 : if ( (float) own_random( &hHQ_nbfec->HQ_FEC_seed ) < 0.0f )
276 : {
277 0 : t_audio_q[i] *= -1.0f;
278 : }
279 : }
280 : }
281 : }
282 : else
283 : {
284 0 : if ( hHQ_core->old_is_transient[1] ) /* hangover */
285 : {
286 0 : for ( i = 0; i < output_frame; i++ )
287 : {
288 0 : hHQ_nbfec->old_coeffs[i] *= SCALE_DOWN_3dB;
289 0 : t_audio_q[i] = hHQ_nbfec->old_coeffs[i];
290 : }
291 : }
292 : else
293 : {
294 0 : for ( i = 0; i < RANDOM_START * 8; i++ )
295 : {
296 0 : hHQ_nbfec->old_coeffs[i] *= SCALE_DOWN_3dB;
297 0 : t_audio_q[i] = hHQ_nbfec->old_coeffs[i];
298 : }
299 :
300 0 : for ( i = RANDOM_START * 8; i < output_frame; i++ )
301 : {
302 0 : hHQ_nbfec->old_coeffs[i] *= SCALE_DOWN_3dB;
303 0 : if ( (float) own_random( &hHQ_nbfec->HQ_FEC_seed ) < 0.0f )
304 : {
305 0 : t_audio_q[i] = ( -hHQ_nbfec->old_coeffs[i] );
306 : }
307 : else
308 : {
309 0 : t_audio_q[i] = hHQ_nbfec->old_coeffs[i];
310 : }
311 : }
312 : }
313 : }
314 : }
315 : else /* st->nbLostCmpt > 1 */
316 : {
317 0 : if ( energy_diff < ED_THRES && is_transient == 0 )
318 : {
319 0 : num_pgf = 4;
320 : }
321 : else
322 : {
323 0 : num_pgf = 2;
324 : }
325 :
326 0 : Num_sb_bwe = num_Sb;
327 0 : if ( st->nbLostCmpt == 2 )
328 : {
329 0 : for ( i = 0; i < Num_sb_bwe; i++ )
330 : {
331 0 : norm_values = &ynrm_values[i][0];
332 0 : r_p = &r_p_values[i][0];
333 0 : Regression_Anal( norm_values, r_p, num_pgf );
334 : }
335 : }
336 :
337 : /* Fade-out Norm by the result of Regression */
338 0 : if ( st->nbLostCmpt >= mute_start )
339 : {
340 : /* Scaling */
341 0 : for ( i = 0; i < output_frame; i++ )
342 : {
343 0 : hHQ_nbfec->old_coeffs[i] *= SCALE_DOWN_3dB;
344 : }
345 : }
346 :
347 0 : k = 0;
348 0 : for ( i = 0; i < Num_sb_bwe; i++ )
349 : {
350 0 : norm_values = &ynrm_values[i][0];
351 0 : r_p = &r_p_values[i][0];
352 :
353 : /* Predict the average energy of each sub-band using Regression */
354 : /* Linear Regression */
355 0 : if ( r_p[1] > MAX_TILT )
356 : {
357 0 : r_p[1] = MAX_TILT;
358 0 : norm_p[i] = (float) ( norm_values[0] + r_p[1] * (float) ( st->nbLostCmpt - 1 ) );
359 : }
360 : else
361 : {
362 0 : norm_p[i] = (float) ( r_p[0] + r_p[1] * (float) ( st->nbLostCmpt - 1 + num_pgf ) );
363 : }
364 :
365 0 : if ( norm_values[0] != 0.0f && norm_p[i] > 0.0f ) /* Avoid negative value of the predicted energy */
366 : {
367 0 : tmp = norm_p[i] / norm_values[0]; /* Pred_new / Old */
368 :
369 0 : if ( tmp > 1.0f )
370 : {
371 0 : tmp = 1.f;
372 : }
373 :
374 0 : for ( j = 0; j < Num_bands_p[i]; j++ )
375 : {
376 0 : hHQ_nbfec->Norm_gain[k++] = tmp;
377 : }
378 : }
379 : else
380 : {
381 : /* Scale down the last gain with the fixed gain(-3dB) */
382 0 : for ( j = 0; j < Num_bands_p[i]; j++ )
383 : {
384 0 : hHQ_nbfec->Norm_gain[k++] *= SCALE_DOWN_3dB;
385 : }
386 : }
387 : }
388 :
389 : /* Scaling for core band */
390 0 : FEC_scaling( hHQ_nbfec->old_coeffs, t_audio_q, hHQ_nbfec->Norm_gain, &hHQ_nbfec->HQ_FEC_seed, nb_sfm, sfm_start, sfm_end );
391 : }
392 :
393 0 : return;
394 : }
395 :
396 :
397 : /*--------------------------------------------------------------------------*
398 : * HQ_FEC_Mem_update()
399 : *
400 : *
401 : *--------------------------------------------------------------------------*/
402 :
403 24807 : void HQ_FEC_Mem_update(
404 : Decoder_State *st, /* i/o: decoder state structure */
405 : const float *t_audio_q,
406 : float *normq,
407 : int16_t *ynrm,
408 : const int16_t *Num_bands_p,
409 : const int16_t is_transient,
410 : const int16_t hqswb_clas,
411 : const int16_t c_switching_flag,
412 : const int16_t nb_sfm,
413 : const int16_t num_Sb,
414 : float *mean_en_high,
415 : const int16_t hq_core_type, /* i : normal or low-rate MDCT(HQ) core */
416 : const int16_t output_frame /* i : Frame size */
417 : )
418 : {
419 : int16_t i, j, k;
420 : float tmp;
421 24807 : float tmp_energy = 0;
422 : float *norm_values;
423 : int16_t offset;
424 : int16_t Min_ind;
425 : int16_t Min_value;
426 : float Max_coeff;
427 : int16_t Max_ind;
428 : int16_t stat_mode_curr;
429 : float en_high[MAX_SB_NB];
430 :
431 24807 : HQ_DEC_HANDLE hHQ_core = st->hHQ_core;
432 24807 : HQ_NBFEC_HANDLE hHQ_nbfec = st->hHQ_nbfec;
433 :
434 24807 : if ( output_frame == L_FRAME8k )
435 : {
436 0 : if ( is_transient )
437 : {
438 0 : set_s( hHQ_nbfec->prev_sign_switch_2, 0, HQ_FEC_SIGN_SFM );
439 0 : set_s( hHQ_nbfec->prev_sign_switch, 0, HQ_FEC_SIGN_SFM );
440 : }
441 : else
442 : {
443 0 : for ( j = 0; j < HQ_FEC_SIGN_SFM; j++ )
444 : {
445 0 : hHQ_nbfec->prev_sign_switch[j] = hHQ_nbfec->prev_sign_switch_2[j];
446 0 : hHQ_nbfec->prev_sign_switch_2[j] = 0;
447 :
448 0 : for ( i = 0; i < HQ_FEC_BAND_SIZE; i++ )
449 : {
450 0 : tmp = hHQ_nbfec->old_coeffs[i + j * HQ_FEC_BAND_SIZE] * t_audio_q[i + j * HQ_FEC_BAND_SIZE];
451 0 : if ( tmp < 0 )
452 : {
453 0 : hHQ_nbfec->prev_sign_switch[j]++;
454 0 : hHQ_nbfec->prev_sign_switch_2[j]++;
455 : }
456 : }
457 : }
458 : }
459 :
460 : /* if LR MDCT core is used, recalculate norms from decoded MDCT spectrum (using code from hq_hr_enc()) */
461 0 : if ( ( hqswb_clas == HQ_HVQ ) || ( hq_core_type == LOW_RATE_HQ_CORE ) )
462 : {
463 : /* First group */
464 0 : logqnorm( t_audio_q, ynrm, 32, WID_G1, thren_HQ );
465 0 : j = ynrm[0];
466 0 : offset = WID_G1;
467 :
468 0 : for ( i = 1; i < SFM_G1; i++ )
469 : {
470 0 : logqnorm( &t_audio_q[offset], &ynrm[i], 40, WID_G1, thren_HQ );
471 0 : offset += WID_G1;
472 : }
473 :
474 : /* Second group */
475 0 : for ( i = SFM_G1; i < SFM_G1 + 2; i++ )
476 : {
477 0 : logqnorm( &t_audio_q[offset], &ynrm[i], 40, WID_G2, thren_HQ );
478 0 : offset += WID_G2;
479 : }
480 : }
481 :
482 : /* Memory update for the LGF log2 Norm */
483 0 : for ( i = 0; i < nb_sfm; i++ )
484 : {
485 0 : normq[i] = dicn[ynrm[i]];
486 : }
487 :
488 0 : k = 0;
489 0 : for ( i = 0; i < num_Sb; i++ )
490 : {
491 0 : norm_values = &hHQ_nbfec->ynrm_values[i][0];
492 0 : mvr2r( norm_values, &norm_values[1], MAX_PGF - 1 );
493 :
494 0 : tmp = 0.f;
495 0 : for ( j = 0; j < Num_bands_p[i]; j++ )
496 : {
497 0 : tmp += (float) normq[k++];
498 : }
499 0 : norm_values[0] = tmp / (float) Num_bands_p[i];
500 0 : tmp_energy += tmp;
501 : }
502 :
503 0 : if ( ( c_switching_flag ) || ( ( st->last_core == ACELP_CORE ) && ( st->core == HQ_CORE ) ) )
504 : {
505 0 : for ( i = 0; i < MAX_SB_NB; i++ )
506 : {
507 0 : for ( j = 1; j < MAX_PGF; j++ )
508 : {
509 0 : hHQ_nbfec->ynrm_values[i][j] = hHQ_nbfec->ynrm_values[i][0];
510 : }
511 : }
512 : }
513 0 : set_f( hHQ_nbfec->Norm_gain, 1.f, SFM_N_NB );
514 :
515 : /* hHQ_nbfec->energy_MA_Curr[1]=Energy of the current frame */
516 0 : hHQ_nbfec->energy_MA_Curr[1] = tmp_energy / (float) nb_sfm;
517 :
518 : /* Moving Average */
519 0 : hHQ_nbfec->energy_MA_Curr[0] = 0.8f * hHQ_nbfec->energy_MA_Curr[0] + 0.2f * hHQ_nbfec->energy_MA_Curr[1];
520 :
521 0 : hHQ_nbfec->diff_energy = (float) fabs( ( hHQ_nbfec->energy_MA_Curr[1] - hHQ_nbfec->energy_MA_Curr[0] ) / hHQ_nbfec->energy_MA_Curr[0] );
522 : /* Classify the stationary mode : 12% */
523 0 : if ( ( hHQ_nbfec->diff_energy < ED_THRES_12P ) )
524 : {
525 0 : stat_mode_curr = 1;
526 : }
527 : else
528 : {
529 0 : stat_mode_curr = 0;
530 : }
531 :
532 : /* Apply Hysteresis to prevent frequent mode changing */
533 0 : if ( hHQ_nbfec->stat_mode_old == stat_mode_curr )
534 : {
535 0 : hHQ_nbfec->stat_mode_out = stat_mode_curr;
536 : }
537 :
538 0 : hHQ_nbfec->stat_mode_old = stat_mode_curr;
539 :
540 :
541 : /* Find max. band index (Minimum value means maximum energy) */
542 0 : Min_ind = 0;
543 0 : Min_value = 100;
544 0 : for ( i = 0; i < num_Sb; i++ )
545 : {
546 0 : if ( Min_value > ynrm[i] )
547 : {
548 0 : Min_value = ynrm[i];
549 0 : Min_ind = i;
550 : }
551 : }
552 :
553 : /* Find max. coeff in band 0 */
554 0 : Max_ind = 0;
555 0 : if ( Min_ind == 0 )
556 : {
557 0 : Max_coeff = 0.f;
558 0 : for ( i = 0; i < 8; i++ )
559 : {
560 0 : tmp = (float) fabs( t_audio_q[i] );
561 0 : if ( Max_coeff < tmp )
562 : {
563 0 : Max_coeff = tmp;
564 0 : Max_ind = i;
565 : }
566 : }
567 : }
568 :
569 : /* Find energy difference from band 16 */
570 0 : k = 1;
571 :
572 0 : for ( i = k; i < num_Sb; i++ )
573 : {
574 0 : en_high[i] = 0.f;
575 0 : for ( j = 0; j < 2; j++ )
576 : {
577 0 : en_high[i] += 0.5f * hHQ_nbfec->ynrm_values[i][j + 1];
578 : }
579 : }
580 :
581 0 : *mean_en_high = 0.f;
582 0 : for ( i = k; i < num_Sb; i++ )
583 : {
584 0 : *mean_en_high += (float) ( en_high[i] / hHQ_nbfec->ynrm_values[i][0] );
585 : }
586 0 : *mean_en_high /= (float) ( num_Sb - k );
587 :
588 0 : if ( ( Min_ind < 5 ) && ( abs( Min_ind - hHQ_nbfec->old_Min_ind ) < 2 ) && ( hHQ_nbfec->diff_energy < ED_THRES_90P ) && ( !st->bfi ) && ( !st->prev_bfi ) && ( !st->prev_old_bfi ) && ( !is_transient ) && ( !hHQ_core->old_is_transient[1] ) && ( hHQ_nbfec->prev_last_core == HQ_CORE ) && ( st->last_core == HQ_CORE ) )
589 : {
590 0 : if ( ( Min_ind == 0 ) && ( Max_ind < 3 ) )
591 : {
592 0 : hHQ_nbfec->phase_mat_flag = 0;
593 : }
594 : else
595 : {
596 0 : hHQ_nbfec->phase_mat_flag = 1;
597 : }
598 : }
599 : else
600 : {
601 0 : hHQ_nbfec->phase_mat_flag = 0;
602 : }
603 :
604 0 : hHQ_nbfec->old_Min_ind = Min_ind;
605 :
606 0 : for ( i = 0; i < L_FRAME8k; i++ )
607 : {
608 0 : hHQ_nbfec->old_coeffs[i] = t_audio_q[i];
609 : }
610 : }
611 :
612 24807 : hHQ_core->old_is_transient[2] = hHQ_core->old_is_transient[1];
613 24807 : hHQ_core->old_is_transient[1] = hHQ_core->old_is_transient[0];
614 24807 : hHQ_core->old_is_transient[0] = is_transient;
615 :
616 24807 : return;
617 : }
618 :
619 : /*--------------------------------------------------------------------------*
620 : * find_best_delay()
621 : *
622 : *
623 : *--------------------------------------------------------------------------*/
624 :
625 0 : static int16_t find_best_delay(
626 : float *mu_o,
627 : float *in,
628 : int16_t mind1,
629 : int16_t maxd1,
630 : int16_t lin,
631 : int16_t delta,
632 : int16_t *false_flag
633 :
634 : )
635 : {
636 : int16_t i, d1, k;
637 0 : int16_t d1m = 0;
638 : float min_sq_cross, min_corr;
639 : float accA, accB;
640 : float Rxy[MAXDELAY_FEC], Ryy[MAXDELAY_FEC];
641 :
642 0 : for ( k = 0, d1 = mind1; k < ( maxd1 - mind1 ) / delta; d1 += delta, k++ )
643 : {
644 0 : accA = accB = 0;
645 0 : for ( i = 0; i < lin; i += delta )
646 : {
647 0 : accA += mu_o[d1 + i] * mu_o[d1 + i];
648 0 : accB += mu_o[d1 + i] * in[i];
649 : }
650 :
651 0 : Rxy[k] = accB;
652 0 : Ryy[k] = accA;
653 : }
654 :
655 : /* Obtain the best delay values */
656 0 : min_sq_cross = -FLT_MAX;
657 0 : min_corr = 0;
658 :
659 0 : for ( d1 = 0; d1 < ( maxd1 - mind1 ) / delta; d1++ )
660 : {
661 0 : if ( Rxy[d1] * min_corr >= min_sq_cross * Ryy[d1] )
662 : {
663 0 : d1m = d1;
664 0 : min_corr = Ryy[d1];
665 0 : min_sq_cross = Rxy[d1];
666 : }
667 : }
668 0 : d1m *= delta;
669 :
670 0 : if ( min_sq_cross <= 0.f || min_corr <= 0.f )
671 : {
672 0 : accA = 0.f;
673 : }
674 : else
675 : {
676 0 : accA = min_sq_cross / min_corr;
677 : }
678 0 : if ( accA < 0.5 || accA > 1.5 )
679 : {
680 0 : *false_flag = 1;
681 : }
682 : else
683 : {
684 0 : *false_flag = 0;
685 : }
686 :
687 0 : return d1m;
688 : }
689 :
690 : /*--------------------------------------------------------------------------*
691 : * Search_Max_Corr()
692 : *
693 : *
694 : *--------------------------------------------------------------------------*/
695 :
696 0 : static int16_t Search_Max_Corr(
697 : float *mu_o, /* i : *old_auOut_2fr */
698 : int16_t old_Min_ind, /* i : * Old Minimum index */
699 : const int16_t L /* i : L/2 */
700 : )
701 : {
702 : int16_t pos;
703 : int16_t pos2, delta2;
704 : int16_t lin, delta;
705 : int16_t mind1, maxd1;
706 : float *in;
707 : int16_t false_flag;
708 : int16_t min_d1, max_d1;
709 :
710 0 : if ( old_Min_ind == 0 )
711 : {
712 0 : lin = 8 * L / 20; /* Basic size of the block for phase matching */
713 :
714 0 : mind1 = 0; /* min value of delay d1 to search for */
715 0 : maxd1 = 12 * L / 20; /* max value of delay d1 to search for */
716 :
717 0 : in = mu_o + 2 * L - lin;
718 :
719 : /* generate correlation */
720 0 : delta = 2;
721 0 : delta2 = 1;
722 :
723 0 : pos = find_best_delay( mu_o, in, mind1, maxd1, lin, delta, &false_flag );
724 :
725 0 : if ( false_flag )
726 : {
727 0 : return 0;
728 : }
729 0 : min_d1 = max( mind1, mind1 + pos - delta + 1 );
730 0 : max_d1 = min( maxd1, mind1 + pos + delta );
731 0 : pos2 = find_best_delay( mu_o, in, min_d1, max_d1, lin, delta2, &false_flag );
732 :
733 0 : if ( mind1 > ( mind1 + pos - delta + 1 ) )
734 : {
735 0 : pos = pos2;
736 : }
737 : else
738 : {
739 0 : pos = pos + pos2 - delta + 1;
740 : }
741 0 : pos = pos + lin + mind1;
742 : }
743 : else
744 : {
745 0 : lin = 6 * L / 20;
746 :
747 0 : mind1 = 9 * L / 20; /* min value of delay d1 to search for */
748 0 : maxd1 = 14 * L / 20; /* max value of delay d1 to search for */
749 :
750 0 : in = mu_o + 2 * L - lin;
751 :
752 : /* generate correlation */
753 0 : delta = 2;
754 0 : delta2 = 1;
755 :
756 0 : pos = find_best_delay( mu_o, in, mind1, maxd1, lin, delta, &false_flag );
757 :
758 0 : if ( false_flag )
759 : {
760 0 : return 0;
761 : }
762 :
763 0 : min_d1 = max( mind1, mind1 + pos - delta + 1 );
764 0 : max_d1 = min( maxd1, mind1 + pos + delta );
765 0 : pos2 = find_best_delay( mu_o, in, min_d1, max_d1, lin, delta2, &false_flag );
766 :
767 0 : if ( mind1 > ( mind1 + pos - delta + 1 ) )
768 : {
769 0 : pos = pos2;
770 : }
771 : else
772 : {
773 0 : pos = pos + pos2 - delta + 1;
774 : }
775 :
776 0 : pos = pos + lin + mind1;
777 : }
778 :
779 0 : return pos;
780 : }
781 :
782 : /*--------------------------------------------------------------------------*
783 : * FEC_phase_matching()
784 : *
785 : *
786 : *--------------------------------------------------------------------------*/
787 :
788 0 : static int16_t FEC_phase_matching(
789 : HQ_NBFEC_HANDLE st, /* i/o: HQ NB FEC handle */
790 : float *ImdctOut, /* i : input */
791 : float *auOut, /* o : output audio */
792 : float *OldauOut,
793 : float OldauOut_pha[2][N_LEAD_NB] )
794 : {
795 : int16_t i;
796 : float ImdctOutWin[2 * L_FRAME8k];
797 : int16_t pos, remain;
798 : int16_t ol_size;
799 : float OldauOutnoWin[L_FRAME8k];
800 : int16_t L_overlap, L;
801 : float OldauOut2[L_FRAME8k];
802 0 : float pow1 = 0, pow22 = 0;
803 : float win_NB[L_FRAME8k + 25];
804 : float SmoothingWin_NB3[24];
805 :
806 0 : L = L_FRAME8k;
807 :
808 0 : for ( i = 0; i < 3 * L / 20; i++ )
809 : {
810 0 : SmoothingWin_NB3[i] = SmoothingWin_NB875[i * 3];
811 : }
812 :
813 0 : for ( i = 0; i < L + 25; i++ )
814 : {
815 0 : win_NB[i] = window_48kHz[i * 6 + 3];
816 : }
817 :
818 0 : set_f( ImdctOutWin, 0.0, 2 * L );
819 :
820 : /* OLA */
821 0 : ol_size = 2 * L / 20;
822 0 : pos = Search_Max_Corr( st->old_auOut_2fr, st->old_Min_ind, L );
823 :
824 0 : if ( pos == 0 )
825 : {
826 0 : return 1;
827 : }
828 :
829 : /* Repetition */
830 0 : remain = L + N_Z_L_NB - ( ( 2 * L ) - pos );
831 0 : mvr2r( &st->old_auOut_2fr[pos], &ImdctOutWin[N_ZERO_NB], ( 2 * L ) - pos );
832 :
833 : /* OldauOut without windowing */
834 0 : for ( i = N_ZERO_NB; i < L / 2; i++ )
835 : {
836 0 : OldauOutnoWin[i - N_ZERO_NB] = -st->oldIMDCTout[L / 2 - 1 - i];
837 : }
838 0 : for ( i = 0; i < L / 2; i++ )
839 : {
840 0 : OldauOutnoWin[i + N_ZERO_O_NB] = -st->oldIMDCTout[i];
841 : }
842 :
843 0 : mvr2r( OldauOutnoWin, &ImdctOutWin[N_ZERO_NB + ( 2 * L ) - pos], remain );
844 :
845 0 : for ( i = 0; i < L; i++ )
846 : {
847 0 : pow1 += (float) fabs( st->old_auOut_2fr[L + i] );
848 0 : pow22 += (float) fabs( ImdctOutWin[N_ZERO_NB + i] );
849 : }
850 0 : if ( pow22 != 0 )
851 : {
852 0 : pow1 /= pow22;
853 0 : for ( i = N_ZERO_NB; i < 2 * L; i++ )
854 : {
855 0 : ImdctOutWin[i] *= pow1;
856 : }
857 : }
858 0 : Smoothing_vector_NB( OldauOutnoWin, &ImdctOutWin[N_ZERO_NB], SmoothingWin_NB2, auOut, ol_size );
859 :
860 0 : for ( i = 0; i < L / 2; i++ )
861 : {
862 0 : ImdctOutWin[3 * L / 2 + i] *= win_NB[L / 2 - i - 1];
863 : }
864 :
865 0 : for ( i = N_ZERO_NB; i < L / 2; i++ )
866 : {
867 0 : ImdctOutWin[L + i] *= win_NB[( L - 1 - i )];
868 : }
869 0 : mvr2r( &ImdctOutWin[N_Z_L_O_NB], &OldauOut_pha[0][0], N_LEAD_NB );
870 0 : mvr2r( &ImdctOutWin[ol_size + N_ZERO_NB], &auOut[ol_size], N_Z_L_NB - ol_size );
871 0 : mvr2r( &ImdctOutWin[L], &auOut[N_Z_L_NB], N_ZERO_NB );
872 0 : mvr2r( &ImdctOutWin[L], OldauOut, L );
873 :
874 0 : for ( i = 0; i < L / 2; i++ )
875 : {
876 0 : OldauOut2[i] = -ImdctOut[L / 2 - 1 - i];
877 0 : OldauOut2[L / 2 + i] = -ImdctOut[i];
878 : }
879 :
880 0 : L_overlap = 3 * L / 20;
881 0 : Smoothing_vector_NB( &ImdctOutWin[N_Z_L_O_NB], &OldauOut2[N_ZERO_NB], SmoothingWin_NB3, &OldauOut_pha[1][0], L_overlap );
882 :
883 0 : for ( i = L_overlap; i < N_LEAD_NB; i++ )
884 : {
885 0 : OldauOut_pha[1][i] = OldauOut2[i + N_ZERO_NB];
886 : }
887 :
888 0 : return 0;
889 : }
890 :
891 : /*--------------------------------------------------------------------------*
892 : * FEC_phase_matching_nextgood()
893 : *
894 : *
895 : *--------------------------------------------------------------------------*/
896 :
897 0 : void FEC_phase_matching_nextgood(
898 : const float *ImdctOut, /* i : input */
899 : float *auOut, /* o : output audio */
900 : float *OldauOut, /* i/o: audio from previous frame */
901 : float OldauOut_pha[2][N_LEAD_NB],
902 : float mean_en_high )
903 : {
904 : int16_t i;
905 : float ImdctOutWin[2 * L_FRAME8k];
906 : int16_t L_overlap, L;
907 : int16_t oldout_pha_idx;
908 : float *OldOut_pha;
909 : float win_NB[L_FRAME8k + 25];
910 :
911 0 : L = L_FRAME8k;
912 0 : for ( i = 0; i < L + 25; i++ )
913 : {
914 0 : win_NB[i] = window_48kHz[i * 6 + 3];
915 : }
916 :
917 0 : if ( ( mean_en_high > 2.f ) || ( mean_en_high < 0.5f ) )
918 : {
919 0 : oldout_pha_idx = 1;
920 : }
921 : else
922 : {
923 0 : oldout_pha_idx = 0;
924 : }
925 :
926 : /* Overlapping with next good frame : Overlapping to remove the discontinuity */
927 0 : L_overlap = N_LEAD_NB;
928 0 : OldOut_pha = OldauOut_pha[oldout_pha_idx];
929 0 : for ( i = 0; i < N_LEAD_NB; i++ )
930 : {
931 0 : OldOut_pha[i] *= SmoothingWin_NB875[L_overlap - i - 1];
932 : }
933 :
934 0 : if ( oldout_pha_idx == 1 )
935 : {
936 : /* Use phase matching and overlapping with the Oldauout*/
937 : /* Windowing */
938 0 : Windowing_1st_NB( ImdctOutWin, ImdctOut, win_NB, NULL, 0 );
939 0 : Windowing_2nd_NB( ImdctOutWin, ImdctOut, win_NB );
940 : }
941 : else
942 : {
943 : /* Only use phase matching */
944 : /* Windowing */
945 0 : Windowing_1st_NB( ImdctOutWin, ImdctOut, win_NB, SmoothingWin_NB875, 1 );
946 0 : Windowing_2nd_NB( ImdctOutWin, ImdctOut, win_NB );
947 : }
948 :
949 0 : common_overlapping( auOut, ImdctOutWin, OldOut_pha, N_LEAD_NB, 0, N_LEAD_NB, L, N_ZERO_NB, 0 );
950 0 : mvr2r( &ImdctOutWin[L], OldauOut, L );
951 :
952 0 : return;
953 : }
954 :
955 : /*--------------------------------------------------------------------------*
956 : * FEC_phase_matching_burst()
957 : *
958 : *
959 : *--------------------------------------------------------------------------*/
960 :
961 0 : static void FEC_phase_matching_burst(
962 : const float *ImdctOut, /* i : input */
963 : float *auOut, /* o : output audio */
964 : float *OldauOut, /* i/o: audio from previous frame */
965 : float OldauOut_pha[2][N_LEAD_NB],
966 : float *prev_oldauOut /* i : OldauOut from previous frame */
967 : )
968 : {
969 : int16_t i;
970 : int16_t L_overlap;
971 : float OldauOut2[L_FRAME8k];
972 : float ImdctOutWin[2 * L_FRAME8k];
973 : int16_t L;
974 : float win_NB[L_FRAME8k + 25];
975 : float SmoothingWin_NB3[24];
976 :
977 0 : L = L_FRAME8k;
978 :
979 0 : for ( i = 0; i < 3 * L / 20; i++ )
980 : {
981 0 : SmoothingWin_NB3[i] = SmoothingWin_NB875[i * 3];
982 : }
983 :
984 0 : for ( i = 0; i < L + 25; i++ )
985 : {
986 0 : win_NB[i] = window_48kHz[i * 6 + 3];
987 : }
988 :
989 : /* Windowing */
990 0 : Windowing_1st_NB( ImdctOutWin, ImdctOut, win_NB, NULL, 0 );
991 0 : Windowing_2nd_NB( ImdctOutWin, ImdctOut, win_NB );
992 :
993 : /* Repetition with old frame to reserve energy */
994 0 : common_overlapping( auOut, ImdctOutWin, prev_oldauOut, N_Z_L_NB, 0, N_Z_L_NB, L, N_ZERO_NB, 0 );
995 :
996 : /* data transition from OldauOut to auOut using smoothing win*/
997 0 : Smoothing_vector_NB( OldauOut_pha[0], auOut, SmoothingWin_NB875, auOut, N_LEAD_NB );
998 :
999 : /* Update the OldauOut array for next overlapping */
1000 0 : mvr2r( &ImdctOutWin[N_Z_L_O_NB], &OldauOut_pha[0][0], N_LEAD_NB );
1001 0 : mvr2r( &ImdctOutWin[L], OldauOut, L );
1002 0 : v_multc( prev_oldauOut, SCALE_DOWN_3dB, prev_oldauOut, L );
1003 :
1004 0 : for ( i = 0; i < L / 2; i++ )
1005 : {
1006 0 : OldauOut2[i] = -ImdctOut[L / 2 - 1 - i];
1007 0 : OldauOut2[L / 2 + i] = -ImdctOut[i];
1008 : }
1009 :
1010 0 : L_overlap = 3 * L / 20;
1011 0 : Smoothing_vector_NB( &ImdctOutWin[N_Z_L_O_NB], &OldauOut2[N_ZERO_NB], SmoothingWin_NB3, &OldauOut_pha[1][0], L_overlap );
1012 :
1013 0 : for ( i = L_overlap; i < N_LEAD_NB; i++ )
1014 : {
1015 0 : OldauOut_pha[1][i] = OldauOut2[i + N_ZERO_NB];
1016 : }
1017 :
1018 0 : return;
1019 : }
1020 :
1021 :
1022 : /*--------------------------------------------------------------------------*
1023 : * Repetition_smoothing_nextgood()
1024 : *
1025 : *
1026 : *--------------------------------------------------------------------------*/
1027 :
1028 0 : static void Repetition_smoothing_nextgood(
1029 : const float *ImdctOut, /* i : input */
1030 : float *auOut, /* o : output audio */
1031 : float *OldImdctOut, /* i : input */
1032 : float *OldauOut, /* i/o: audio from previous frame */
1033 : int16_t cur_data_use_flag, /* i : current imdct data use flag */
1034 : int16_t overlap_time )
1035 : {
1036 : int16_t i;
1037 : float ImdctOutWin[2 * L_FRAME8k];
1038 : float win_NB[L_FRAME8k + 25];
1039 : int16_t L_overlap;
1040 : int16_t ol_size;
1041 : int16_t L;
1042 :
1043 0 : L = L_FRAME8k;
1044 :
1045 0 : for ( i = 0; i < L_FRAME8k + 25; i++ )
1046 : {
1047 0 : win_NB[i] = window_48kHz[i * 6 + 3];
1048 : }
1049 :
1050 0 : for ( i = N_ZERO_NB; i < L / 2; i++ )
1051 : {
1052 0 : OldauOut[i - N_ZERO_NB] = -OldImdctOut[L / 2 - 1 - i];
1053 : }
1054 0 : for ( i = 0; i < L / 2; i++ )
1055 : {
1056 0 : OldauOut[i + N_ZERO_O_NB] = -OldImdctOut[i];
1057 : }
1058 :
1059 : /* Overlapping with next good frame : Overlapping to remove the discontinuity */
1060 0 : if ( cur_data_use_flag )
1061 : {
1062 0 : ol_size = N_LEAD_NB;
1063 :
1064 0 : for ( i = N_ZERO_NB; i < L / 2; i++ )
1065 : {
1066 0 : ImdctOutWin[i + L] = -ImdctOut[L / 2 - 1 - i];
1067 : }
1068 0 : for ( i = 0; i < L / 2; i++ )
1069 : {
1070 0 : ImdctOutWin[i + 3 * L / 2] = -ImdctOut[i];
1071 : }
1072 :
1073 : /*a = (float)(1./(float)(ol_size)); y = ax */
1074 0 : Smoothing_vector_scaledown_NB( OldauOut, &ImdctOutWin[N_Z_L_O_NB], SmoothingWin_NB875, OldauOut, ol_size );
1075 :
1076 : /* Scale down the overlapped signal */
1077 0 : v_multc( &ImdctOutWin[ol_size + N_Z_L_O_NB], SCALE_DOWN_3dB, &OldauOut[ol_size], N_Z_L_NB - ol_size );
1078 : }
1079 :
1080 0 : L_overlap = overlap_time;
1081 0 : for ( i = 0; i < L_overlap; i++ )
1082 : {
1083 0 : OldauOut[i] *= SmoothingWin_NB875[L_overlap - i - 1];
1084 : }
1085 0 : for ( i = L_overlap; i < L; i++ )
1086 : {
1087 0 : OldauOut[i] = 0.f;
1088 : }
1089 :
1090 : /* Windowing */
1091 0 : Windowing_1st_NB( ImdctOutWin, ImdctOut, win_NB, SmoothingWin_NB875, 1 );
1092 0 : Windowing_2nd_NB( ImdctOutWin, ImdctOut, win_NB );
1093 :
1094 0 : v_add( &ImdctOutWin[N_ZERO_NB], OldauOut, auOut, L );
1095 0 : mvr2r( &ImdctOutWin[L], OldauOut, L );
1096 :
1097 0 : return;
1098 : }
1099 :
1100 : /*--------------------------------------------------------------------------*
1101 : * Repetition_smoothing()
1102 : *
1103 : *
1104 : *--------------------------------------------------------------------------*/
1105 :
1106 0 : static int16_t Repetition_smoothing(
1107 : const float *ImdctOut, /* i : input */
1108 : float *auOut, /* o : output audio */
1109 : float *OldImdctOut, /* i : input */
1110 : float *OldauOut, /* i/o: audio from previous frame */
1111 : const int16_t L, /* i : length */
1112 : float *prev_oldauOut, /* i : OldauOut from previous frame */
1113 : int16_t overlap_time /* i : overlap time */
1114 : )
1115 : {
1116 : int16_t i;
1117 : float ImdctOutWin[2 * L_FRAME8k];
1118 0 : float pow1 = 0.f;
1119 0 : float pow22 = 0.f;
1120 : float OldauOutnoWin[L_FRAME8k];
1121 : float win_NB[L_FRAME8k + 25];
1122 :
1123 0 : for ( i = 0; i < L_FRAME8k + 25; i++ )
1124 : {
1125 0 : win_NB[i] = window_48kHz[i * 6 + 3];
1126 : }
1127 :
1128 : /* Windowing */
1129 0 : Windowing_1st_NB( ImdctOutWin, ImdctOut, win_NB, NULL, 0 );
1130 0 : Windowing_2nd_NB( ImdctOutWin, ImdctOut, win_NB );
1131 :
1132 : /* Repetition with old frame to reserve energy */
1133 0 : common_overlapping( auOut, ImdctOutWin, prev_oldauOut, N_Z_L_NB, 0, N_Z_L_NB, L, N_ZERO_NB, 0 );
1134 :
1135 : /* OldauOut without windowing */
1136 0 : for ( i = N_ZERO_NB; i < L / 2; i++ )
1137 : {
1138 0 : OldauOutnoWin[i - N_ZERO_NB] = -OldImdctOut[L / 2 - 1 - i];
1139 : }
1140 0 : for ( i = 0; i < L / 2; i++ )
1141 : {
1142 0 : OldauOutnoWin[i + N_ZERO_O_NB] = -OldImdctOut[i];
1143 : }
1144 :
1145 : /* data transition from OldauOut to auOut using smoothing win*/
1146 0 : Smoothing_vector_NB( OldauOutnoWin, auOut, SmoothingWin_NB875, auOut, overlap_time );
1147 :
1148 0 : pow1 = sum2_f( &auOut[1 * L / 20], 4 * L / 20 );
1149 0 : pow22 = sum2_f( &auOut[N_LEAD_NB], 4 * L / 20 );
1150 :
1151 :
1152 0 : if ( pow22 > pow1 * 3 )
1153 : {
1154 0 : return 1;
1155 : }
1156 :
1157 : /* Update the OldauOut array for next overlapping */
1158 0 : mvr2r( &ImdctOutWin[L], OldauOut, L );
1159 0 : v_multc( prev_oldauOut, SCALE_DOWN_3dB, prev_oldauOut, L );
1160 :
1161 0 : return 0;
1162 : }
1163 :
1164 :
1165 : /*--------------------------------------------------------------------------*
1166 : * Windowing_1st()
1167 : *
1168 : *
1169 : *--------------------------------------------------------------------------*/
1170 :
1171 0 : static void Windowing_1st_NB(
1172 : float *ImdctOutWin, /* o : Output */
1173 : const float *ImdctOut, /* i : Input */
1174 : const float *win, /* i : Window */
1175 : const float *smoothingWin, /* i : Smoothing Window */
1176 : const int16_t smoothing_flag /* i : 1=Smoothing window, 0=Original window */
1177 : )
1178 : {
1179 : int16_t i;
1180 : int16_t L;
1181 :
1182 0 : L = L_FRAME8k;
1183 0 : if ( smoothing_flag == 0 )
1184 : {
1185 0 : for ( i = N_ZERO_NB; i < L / 2; i++ )
1186 : {
1187 0 : ImdctOutWin[i] = ImdctOut[L / 2 + i] * win[( 2 * L - 1 - i ) - N_LEAD_O_NB];
1188 : }
1189 :
1190 0 : for ( i = 0; i < N_ZERO_O_NB; i++ )
1191 : {
1192 0 : ImdctOutWin[L / 2 + i] = -ImdctOut[L - 1 - i] * win[( 3 * L / 2 - 1 - i ) - N_LEAD_O_NB];
1193 0 : ImdctOutWin[3 * L / 2 + i] = -ImdctOut[i] * win[( L / 2 - i - 1 )];
1194 : }
1195 : }
1196 : else
1197 : {
1198 0 : for ( i = N_ZERO_NB; i < L / 2; i++ )
1199 : {
1200 0 : ImdctOutWin[i] = ImdctOut[L / 2 + i] * smoothingWin[( i - N_ZERO_NB )]; /*win[(2*L-i)*decimate-1-decay-14*L_FRAME48k/20];*/
1201 : }
1202 :
1203 0 : for ( i = 0; i < N_ZERO_O_NB; i++ )
1204 : {
1205 0 : ImdctOutWin[L / 2 + i] = -ImdctOut[L - 1 - i] * smoothingWin[( i + N_ZERO_O_NB )]; /*win[(3*L/2-1-i)*decimate+decay-L_FRAME48k*14/20];*/
1206 0 : ImdctOutWin[3 * L / 2 + i] = -ImdctOut[i] * win[( L / 2 - i - 1 )];
1207 : }
1208 : }
1209 :
1210 0 : return;
1211 : }
1212 :
1213 : /*--------------------------------------------------------------------------*
1214 : * Windowing_2nd()
1215 : *
1216 : *
1217 : *--------------------------------------------------------------------------*/
1218 :
1219 0 : static void Windowing_2nd_NB(
1220 : float *ImdctOutWin, /* o : Output */
1221 : const float *ImdctOut, /* i : Input */
1222 : const float *win /* i : Window */
1223 : )
1224 : {
1225 : int16_t i;
1226 : int16_t L;
1227 :
1228 0 : L = L_FRAME8k;
1229 0 : for ( i = N_ZERO_O_NB; i < L / 2; i++ )
1230 : {
1231 0 : ImdctOutWin[L / 2 + i] = -ImdctOut[L - 1 - i];
1232 0 : ImdctOutWin[3 * L / 2 + i] = -ImdctOut[i] * win[L / 2 - i - 1];
1233 : }
1234 :
1235 0 : for ( i = 0; i < N_ZERO_NB; i++ )
1236 : {
1237 0 : ImdctOutWin[L + i] = -ImdctOut[L / 2 - 1 - i];
1238 : }
1239 :
1240 0 : for ( i = N_ZERO_NB; i < L / 2; i++ )
1241 : {
1242 0 : ImdctOutWin[L + i] = -ImdctOut[L / 2 - 1 - i] * win[L - 1 - i];
1243 : }
1244 :
1245 0 : return;
1246 : }
1247 :
1248 : /*--------------------------------------------------------------------------*
1249 : * common_overlapping()
1250 : *
1251 : *
1252 : *--------------------------------------------------------------------------*/
1253 :
1254 0 : static void common_overlapping(
1255 : float *auOut, /* i : Input */
1256 : float *ImdctOutWin, /* o : Output */
1257 : float *OldauOut, /* i : Window */
1258 : const int16_t end1, /* i : Decay */
1259 : const int16_t offset1,
1260 : const int16_t start2,
1261 : const int16_t end2,
1262 : const int16_t offset_i2,
1263 : const int16_t offset2 )
1264 : {
1265 : int16_t i;
1266 :
1267 : /* Common Overlapping */
1268 0 : for ( i = 0; i < end1; i++ )
1269 : {
1270 0 : auOut[i] = ImdctOutWin[i + N_ZERO_NB] + OldauOut[i + offset1];
1271 : }
1272 0 : for ( i = start2; i < end2; i++ )
1273 : {
1274 0 : auOut[i + offset2] = ImdctOutWin[i + offset_i2];
1275 : }
1276 :
1277 0 : return;
1278 : }
1279 :
1280 :
1281 : /*--------------------------------------------------------------------------*
1282 : * Smoothing_vector()
1283 : *
1284 : *
1285 : *--------------------------------------------------------------------------*/
1286 :
1287 0 : static void Smoothing_vector_NB(
1288 : const float OldauOutnoWin[], /* i : Input vector 1 */
1289 : const float ImdctOutWin[], /* i : Input vector 2 */
1290 : const float SmoothingWin[], /* i : Smoothing window */
1291 : float auOut[], /* o : Output vector that contains vector 1 .* vector 2 */
1292 : const int16_t ol_size /* i : Overlap size */
1293 : )
1294 : {
1295 : int16_t i;
1296 : float weight;
1297 :
1298 0 : for ( i = 0; i < ol_size; i++ )
1299 : {
1300 0 : weight = SmoothingWin[i];
1301 0 : auOut[i] = ( OldauOutnoWin[i] * ( 1.f - weight ) ) + ( ImdctOutWin[i] * weight );
1302 : }
1303 :
1304 0 : return;
1305 : }
1306 :
1307 : /*--------------------------------------------------------------------------*
1308 : * Smoothing_vector_scaledown()
1309 : *
1310 : *
1311 : *--------------------------------------------------------------------------*/
1312 :
1313 0 : static void Smoothing_vector_scaledown_NB(
1314 : const float OldauOutnoWin[], /* i : Input vector 1 */
1315 : const float ImdctOutWin[], /* i : Input vector 2 */
1316 : const float SmoothingWin[], /* i : Smoothing window */
1317 : float auOut[], /* o : Output vector that contains vector 1 .* vector 2 */
1318 : const int16_t ol_size /* i : Overlap size */
1319 : )
1320 : {
1321 : int16_t i;
1322 : float weight;
1323 :
1324 0 : for ( i = 0; i < ol_size; i++ )
1325 : {
1326 0 : weight = SmoothingWin[i];
1327 0 : auOut[i] = ( OldauOutnoWin[i] * ( 1.f - weight ) ) + ( ImdctOutWin[i] * SCALE_DOWN_3dB * weight );
1328 : }
1329 :
1330 0 : return;
1331 : }
1332 :
1333 :
1334 : /*--------------------------------------------------------------------------*
1335 : * time_domain_FEC_HQ()
1336 : *
1337 : *
1338 : *--------------------------------------------------------------------------*/
1339 :
1340 0 : void time_domain_FEC_HQ(
1341 : Decoder_State *st, /* i : Decoder State */
1342 : float *wtda_audio, /* i : input */
1343 : float *out, /* o : output audio */
1344 : const float mean_en_high, /* i : transient flag */
1345 : const int16_t output_frame /* i : Frame size */
1346 : )
1347 : {
1348 0 : HQ_DEC_HANDLE hHQ_core = st->hHQ_core;
1349 0 : HQ_NBFEC_HANDLE hHQ_nbfec = st->hHQ_nbfec;
1350 :
1351 0 : if ( ( st->nbLostCmpt == 1 ) && ( hHQ_nbfec->phase_mat_flag == 1 ) && ( hHQ_nbfec->phase_mat_next == 0 ) )
1352 : {
1353 0 : if ( FEC_phase_matching( hHQ_nbfec, wtda_audio, out, hHQ_core->old_out, hHQ_nbfec->old_out_pha ) )
1354 : {
1355 0 : window_ola( wtda_audio, out, hHQ_core->old_out, output_frame, st->hTcxCfg->tcx_last_overlap_mode, st->hTcxCfg->tcx_curr_overlap_mode, st->prev_bfi, hHQ_core->oldHqVoicing, hHQ_core->oldgapsynth );
1356 0 : hHQ_nbfec->phase_mat_next = 0;
1357 : }
1358 : else
1359 : {
1360 0 : hHQ_nbfec->phase_mat_next = 1;
1361 : }
1362 : }
1363 0 : else if ( ( st->prev_bfi == 1 ) && ( st->bfi == 0 ) && ( hHQ_nbfec->phase_mat_next == 1 ) )
1364 : {
1365 0 : FEC_phase_matching_nextgood( wtda_audio, out, hHQ_core->old_out, hHQ_nbfec->old_out_pha, mean_en_high );
1366 0 : hHQ_nbfec->phase_mat_next = 0;
1367 : }
1368 0 : else if ( ( st->prev_bfi == 1 ) && ( st->bfi == 1 ) && ( hHQ_nbfec->phase_mat_next == 1 ) )
1369 : {
1370 0 : FEC_phase_matching_burst( wtda_audio, out, hHQ_core->old_out, hHQ_nbfec->old_out_pha, hHQ_nbfec->prev_oldauOut );
1371 0 : hHQ_nbfec->phase_mat_next = 1;
1372 : }
1373 : else
1374 : {
1375 0 : if ( st->bfi == 0 && st->prev_bfi == 1 )
1376 : {
1377 0 : if ( ( hHQ_nbfec->stat_mode_out == 1 ) || ( hHQ_nbfec->diff_energy < ED_THRES_50P ) ) /* hHQ_nbfec->diff_energy<ED_THRES_L1)*/
1378 : {
1379 0 : Repetition_smoothing_nextgood( wtda_audio, out, hHQ_nbfec->oldIMDCTout, hHQ_core->old_out, st->old_bfi_cnt > 1 ? 1 : 0, N_LEAD_NB );
1380 : }
1381 0 : else if ( st->old_bfi_cnt > 1 )
1382 : {
1383 0 : Next_good_after_burst_erasures( wtda_audio, out, hHQ_core->old_out, N_LEAD_NB );
1384 : }
1385 : else
1386 : {
1387 0 : window_ola( wtda_audio, out, hHQ_core->old_out, output_frame, st->hTcxCfg->tcx_last_overlap_mode, st->hTcxCfg->tcx_curr_overlap_mode, st->prev_bfi, hHQ_core->oldHqVoicing, hHQ_core->oldgapsynth );
1388 : }
1389 : }
1390 : else /* if(st->bfi == 1) */
1391 : {
1392 0 : if ( ( hHQ_nbfec->stat_mode_out == 1 ) || ( hHQ_nbfec->diff_energy < ED_THRES_50P ) )
1393 : {
1394 0 : if ( Repetition_smoothing( wtda_audio, out, hHQ_nbfec->oldIMDCTout, hHQ_core->old_out, output_frame, hHQ_nbfec->prev_oldauOut, N_LEAD_NB ) )
1395 : {
1396 0 : window_ola( wtda_audio, out, hHQ_core->old_out, output_frame, st->hTcxCfg->tcx_last_overlap_mode, st->hTcxCfg->tcx_curr_overlap_mode, st->prev_bfi, hHQ_core->oldHqVoicing, hHQ_core->oldgapsynth );
1397 : }
1398 : }
1399 : else
1400 : {
1401 0 : window_ola( wtda_audio, out, hHQ_core->old_out, output_frame, st->hTcxCfg->tcx_last_overlap_mode, st->hTcxCfg->tcx_curr_overlap_mode, st->prev_bfi, hHQ_core->oldHqVoicing, hHQ_core->oldgapsynth );
1402 : }
1403 : }
1404 :
1405 0 : hHQ_nbfec->phase_mat_next = 0;
1406 : }
1407 :
1408 0 : return;
1409 : }
1410 :
1411 :
1412 : /*--------------------------------------------------------------------------*
1413 : * Next_good_after_burst_erasures()
1414 : *
1415 : * Windowing, Overlap and Add
1416 : *--------------------------------------------------------------------------*/
1417 :
1418 0 : void Next_good_after_burst_erasures(
1419 : const float *ImdctOut, /* i : input */
1420 : float *auOut, /* o : output audio */
1421 : float *OldauOut, /* i/o: audio from previous frame */
1422 : const int16_t ol_size /* i : overlap size */
1423 : )
1424 : {
1425 : float ImdctOutWin[2 * L_FRAME8k];
1426 : int16_t i, L;
1427 : float win_NB[L_FRAME8k + 25];
1428 :
1429 0 : L = L_FRAME8k;
1430 0 : for ( i = 0; i < L + 25; i++ )
1431 : {
1432 0 : win_NB[i] = window_48kHz[i * 6 + 3];
1433 : }
1434 :
1435 : /* Windowing */
1436 0 : Windowing_1st_NB( ImdctOutWin, ImdctOut, win_NB, NULL, 0 );
1437 0 : Windowing_2nd_NB( ImdctOutWin, ImdctOut, win_NB );
1438 :
1439 : /* Overlapping with next good frame : Overlapping to remove the discontinuity */
1440 0 : Smoothing_vector_scaledown_NB( &OldauOut[N_ZERO_NB], &ImdctOutWin[N_Z_L_O_NB], SmoothingWin_NB875, &OldauOut[N_ZERO_NB], ol_size );
1441 :
1442 : /* Scale down the overlapped signal */
1443 0 : v_multc( &ImdctOutWin[ol_size + N_Z_L_O_NB], SCALE_DOWN_3dB, &OldauOut[ol_size + N_ZERO_NB], N_Z_L_NB - ol_size );
1444 :
1445 : /* Common Overlapping */
1446 0 : common_overlapping( auOut, ImdctOutWin, OldauOut, N_Z_L_NB, N_ZERO_NB, 0, N_ZERO_NB, L, N_Z_L_NB );
1447 0 : mvr2r( &ImdctOutWin[L], OldauOut, L );
1448 :
1449 0 : return;
1450 : }
1451 :
1452 : /*--------------------------------------------------------------------------
1453 : * save_synthesis_hq_fec()
1454 : *
1455 : * Save synthesis for HQ FEC
1456 : *-------------------------------------------------------------------------*/
1457 :
1458 3386124 : void save_synthesis_hq_fec(
1459 : Decoder_State *st, /* i/o: decoder state structure */
1460 : const float *output, /* i : decoded synthesis */
1461 : const int16_t output_frame, /* i : decoded synthesis */
1462 : CPE_DEC_HANDLE hCPE /* i : CPE decoder structure */
1463 : )
1464 : {
1465 : int16_t post_hq_delay;
1466 :
1467 3386124 : switch ( st->element_mode )
1468 : {
1469 9300 : case EVS_MONO:
1470 9300 : post_hq_delay = NS2SA( st->output_Fs, POST_HQ_DELAY_NS );
1471 9300 : break;
1472 1033566 : case IVAS_SCE:
1473 1033566 : post_hq_delay = NS2SA( st->output_Fs, DELAY_CLDFB_NS );
1474 1033566 : break;
1475 179058 : case IVAS_CPE_DFT:
1476 179058 : if ( hCPE->nchan_out == 1 && hCPE->hStereoDft->hConfig->res_cod_mode == STEREO_DFT_RES_COD_OFF )
1477 : {
1478 51339 : post_hq_delay = NS2SA( st->output_Fs, DELAY_CLDFB_NS );
1479 : }
1480 : else
1481 : {
1482 127719 : post_hq_delay = 0;
1483 : }
1484 179058 : break;
1485 2164200 : default:
1486 2164200 : post_hq_delay = 0;
1487 2164200 : break;
1488 : }
1489 :
1490 :
1491 3386124 : if ( ( st->codec_mode == MODE1 && st->hTcxDec != NULL ) && ( ( st->core == ACELP_CORE && !( st->bfi == 1 && st->con_tcx == 1 ) ) || st->core == HQ_CORE ) )
1492 : {
1493 557331 : mvr2r( st->hTcxDec->synth_history + output_frame, st->hTcxDec->synth_history, output_frame - post_hq_delay + NS2SA( st->output_Fs, PH_ECU_MEM_NS ) );
1494 557331 : mvr2r( output, st->hTcxDec->old_synthFB + output_frame - post_hq_delay, output_frame );
1495 :
1496 557331 : if ( st->element_mode == EVS_MONO )
1497 : {
1498 : /* reset the remaining buffer, which is read in TCX concealment the necessary samples to fill
1499 : this buffer are not available for all cases, the impact on the output is limited */
1500 5550 : set_f( st->hTcxDec->old_synthFB + 2 * output_frame - post_hq_delay, 0.f, post_hq_delay );
1501 5550 : if ( output_frame >= L_FRAME16k )
1502 : {
1503 5550 : mvr2r( st->prev_synth_buffer, st->hTcxDec->old_synthFB + 2 * output_frame - NS2SA( st->output_Fs, DELAY_BWE_TOTAL_NS ), NS2SA( st->output_Fs, DELAY_BWE_TOTAL_NS - DELAY_CLDFB_NS ) );
1504 : }
1505 : else
1506 : {
1507 0 : mvr2r( st->hHQ_core->old_out + NS2SA( st->output_Fs, N_ZERO_MDCT_NS ), st->hTcxDec->old_synthFB + 2 * output_frame, NS2SA( st->output_Fs, PH_ECU_LOOKAHEAD_NS ) );
1508 : }
1509 :
1510 5550 : if ( st->core != ACELP_CORE )
1511 : {
1512 1320 : if ( output_frame >= L_FRAME16k )
1513 : {
1514 1320 : mvr2r( st->delay_buf_out, st->hTcxDec->old_synthFB + 2 * output_frame - NS2SA( st->output_Fs, DELAY_CLDFB_NS ), NS2SA( st->output_Fs, DELAY_CLDFB_NS ) );
1515 1320 : mvr2r( st->hHQ_core->old_out + NS2SA( st->output_Fs, N_ZERO_MDCT_NS ), st->hTcxDec->old_synthFB + 2 * output_frame, NS2SA( st->output_Fs, PH_ECU_LOOKAHEAD_NS ) );
1516 : }
1517 : else
1518 : {
1519 0 : mvr2r( st->delay_buf_out, st->hTcxDec->old_synthFB + 2 * output_frame - NS2SA( st->output_Fs, DELAY_BWE_TOTAL_NS ), NS2SA( st->output_Fs, DELAY_CLDFB_NS ) );
1520 0 : mvr2r( st->hHQ_core->old_out + NS2SA( st->output_Fs, N_ZERO_MDCT_NS ), st->hTcxDec->old_synthFB + 2 * output_frame - NS2SA( st->output_Fs, DELAY_BWE_TOTAL_NS - DELAY_CLDFB_NS ), NS2SA( st->output_Fs, PH_ECU_LOOKAHEAD_NS ) );
1521 : }
1522 : }
1523 : }
1524 : else
1525 : {
1526 551781 : if ( st->core != ACELP_CORE )
1527 : {
1528 23880 : mvr2r( st->delay_buf_out, st->hTcxDec->old_synthFB + 2 * output_frame - post_hq_delay, post_hq_delay );
1529 23880 : mvr2r( st->hHQ_core->old_out + NS2SA( st->output_Fs, N_ZERO_MDCT_NS ), st->hTcxDec->old_synthFB + 2 * output_frame, NS2SA( st->output_Fs, PH_ECU_LOOKAHEAD_NS ) );
1530 : }
1531 : }
1532 : }
1533 :
1534 3386124 : return;
1535 : }
|