Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2026 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 "prot.h"
45 : #include "rom_com.h"
46 : #include "wmc_auto.h"
47 : #include "ivas_prot.h"
48 : #include <assert.h>
49 :
50 : #define POW_EXC16k_WHTND 1.14e11f /* power of random excitation, length 320 samples, uniform distribution */
51 : #define THR_ENV_ERROR_PLOSIVE 200.0f /* threshold for envelope error used in plosive detection */
52 :
53 : /*-----------------------------------------------------------------*
54 : * Local function prototypes
55 : *-----------------------------------------------------------------*/
56 :
57 : #ifdef FIX_1465_SWB_TBE_RANDOM_VECTOR_CREATION
58 : static void create_random_vector( float output[], const int16_t length, int16_t seed[], const int16_t element_mode );
59 : #else
60 : static void create_random_vector( float output[], const int16_t length, int16_t seed[] );
61 : #endif
62 : static void flip_spectrum( const float input[], float output[], const int16_t length );
63 : static void Hilbert_transform( float tmp_R[], float tmp_I[], float *tmpi_R, float *tmpi_I, const int16_t length, const int16_t HB_stage_id );
64 : static void Estimate_mix_factors( const float *shb_res, const float *exc16kWhtnd, const float *White_exc16k, const float pow1, const float pow22, float *vf_modified, int16_t *vf_ind );
65 :
66 : /*-------------------------------------------------------------------*
67 : * swb_tbe_reset()
68 : *
69 : * Reset the SWB TBE encoder
70 : *-------------------------------------------------------------------*/
71 :
72 3676931 : void swb_tbe_reset(
73 : float mem_csfilt[],
74 : float mem_genSHBexc_filt_down_shb[],
75 : float state_lpc_syn[],
76 : float syn_overlap[],
77 : float state_syn_shbexc[],
78 : float *tbe_demph,
79 : float *tbe_premph,
80 : float mem_stp_swb[],
81 : float *gain_prec_swb )
82 : {
83 3676931 : set_f( mem_csfilt, 0, 2 );
84 3676931 : set_f( mem_genSHBexc_filt_down_shb, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
85 3676931 : set_f( state_lpc_syn, 0.0f, LPC_SHB_ORDER );
86 3676931 : set_f( syn_overlap, 0.0f, L_SHB_LAHEAD );
87 3676931 : set_f( state_syn_shbexc, 0.0f, L_SHB_LAHEAD );
88 3676931 : *tbe_demph = 0.0f;
89 3676931 : *tbe_premph = 0.0f;
90 3676931 : set_f( mem_stp_swb, 0, LPC_SHB_ORDER );
91 3676931 : *gain_prec_swb = 1.0f;
92 :
93 3676931 : return;
94 : }
95 :
96 :
97 : /*-------------------------------------------------------------------*
98 : * swb_tbe_reset_synth()
99 : *
100 : * Reset the extra parameters needed for synthesis of the SWB TBE output
101 : *-------------------------------------------------------------------*/
102 :
103 3172331 : void swb_tbe_reset_synth(
104 : float genSHBsynth_Hilbert_Mem[],
105 : float genSHBsynth_state_lsyn_filt_shb_local[] )
106 : {
107 3172331 : set_f( genSHBsynth_Hilbert_Mem, 0.0f, HILBERT_MEM_SIZE );
108 3172331 : set_f( genSHBsynth_state_lsyn_filt_shb_local, 0.0f, 2 * ALLPASSSECTIONS_STEEP );
109 :
110 3172331 : return;
111 : }
112 :
113 :
114 : /*-------------------------------------------------------------------*
115 : * tbe_celp_exc_offset()
116 : *
117 : * Compute tbe bwe celp excitation offset
118 : *-------------------------------------------------------------------*/
119 :
120 : /*! r: offset value */
121 3509427 : int16_t tbe_celp_exc_offset(
122 : const int16_t T0, /* i : Integer pitch */
123 : const int16_t T0_frac /* i : Fractional part of the pitch */
124 : )
125 : {
126 : int16_t offset;
127 3509427 : offset = T0 * HIBND_ACB_L_FAC + (int16_t) ( (float) T0_frac * 0.25f * HIBND_ACB_L_FAC + 2 * HIBND_ACB_L_FAC + 0.5f ) - 2 * HIBND_ACB_L_FAC;
128 :
129 3509427 : return offset;
130 : }
131 :
132 :
133 : /*-------------------------------------------------------------------*
134 : * flip_and_downmix_generic()
135 : *
136 : * flips the spectrum and downmixes the signals, lpf if needed
137 : *-------------------------------------------------------------------*/
138 :
139 628264 : void flip_and_downmix_generic(
140 : float input[], /* i : input spectrum */
141 : float output[], /* o : output spectrum */
142 : const int16_t length, /* i : length of spectra */
143 : float mem1_ext[HILBERT_ORDER1], /* i/o: Hilbert filter memory */
144 : float mem2_ext[2 * HILBERT_ORDER2], /* i/o: memory */
145 : float mem3_ext[2 * HILBERT_ORDER2], /* i/o: memory */
146 : int16_t *phase_state /* i/o: Phase state in case frequency isn't multiple of 50 Hz */
147 : )
148 : {
149 : int16_t i, j;
150 : float tmp[L_FRAME32k + HILBERT_ORDER1];
151 : float tmpi_R[L_FRAME32k];
152 : float tmpi_I[L_FRAME32k];
153 : float tmpi2_R[L_FRAME32k + HILBERT_ORDER2];
154 : float tmpi2_I[L_FRAME32k + HILBERT_ORDER2];
155 : float tmp_R[L_FRAME32k + HILBERT_ORDER2];
156 : float tmp_I[L_FRAME32k + HILBERT_ORDER2];
157 : int16_t k, period;
158 : float recip_period;
159 : float local_negsin_table[L_FRAME16k];
160 : float local_cos_table[L_FRAME16k];
161 :
162 628264 : period = 17; /* == (int16_t) (32000.0f / 1850.0f + 0.5f); */
163 :
164 628264 : recip_period = 256.0f / (float) period;
165 11308752 : for ( i = 0; i < period; i++ )
166 : {
167 10680488 : k = (int16_t) ( i * recip_period + 0.5f );
168 10680488 : if ( k <= 64 )
169 : {
170 3141320 : local_negsin_table[i] = -sincos_t[k];
171 3141320 : local_cos_table[i] = sincos_t[64 - k];
172 : }
173 7539168 : else if ( k <= 128 )
174 : {
175 2513056 : local_negsin_table[i] = -sincos_t[128 - k];
176 2513056 : local_cos_table[i] = -sincos_t[k - 64];
177 : }
178 5026112 : else if ( k <= 192 )
179 : {
180 2513056 : local_negsin_table[i] = sincos_t[k - 128];
181 2513056 : local_cos_table[i] = -sincos_t[192 - k];
182 : }
183 : else
184 : {
185 2513056 : local_negsin_table[i] = sincos_t[256 - k];
186 2513056 : local_cos_table[i] = sincos_t[k - 192];
187 : }
188 : }
189 :
190 193117344 : for ( i = 0; i < length; i = i + 2 )
191 : {
192 192489080 : input[i] = -input[i];
193 : }
194 :
195 628264 : mvr2r( input, tmp + HILBERT_ORDER1, length );
196 :
197 628264 : mvr2r( mem1_ext, tmp, HILBERT_ORDER1 );
198 :
199 : /* Hilber transform stage - 0 */
200 628264 : Hilbert_transform( tmp, tmp, tmpi_R, tmpi_I, length, 0 );
201 :
202 628264 : mvr2r( mem2_ext, tmpi2_R, HILBERT_ORDER2 );
203 628264 : mvr2r( mem3_ext, tmpi2_I, HILBERT_ORDER2 );
204 :
205 : /* Hilber transform stage - 1 */
206 628264 : Hilbert_transform( tmpi_R, tmpi_I, tmpi2_R, tmpi2_I, length, 1 );
207 :
208 628264 : mvr2r( tmp + length, mem1_ext, HILBERT_ORDER1 );
209 628264 : mvr2r( mem2_ext + HILBERT_ORDER2, tmp_R, HILBERT_ORDER2 );
210 628264 : mvr2r( mem3_ext + HILBERT_ORDER2, tmp_I, HILBERT_ORDER2 );
211 :
212 : /* Hilber transform stage - 2 */
213 628264 : Hilbert_transform( tmpi2_R, tmpi2_I, tmpi_R, tmpi_I, length, 2 );
214 :
215 628264 : mvr2r( tmpi2_R + length, mem2_ext, HILBERT_ORDER2 );
216 628264 : mvr2r( tmpi2_I + length, mem3_ext, HILBERT_ORDER2 );
217 :
218 : /* Hilber transform stage - 3 */
219 628264 : Hilbert_transform( tmpi_R, tmpi_I, tmp_R, tmp_I, length, 3 );
220 :
221 628264 : mvr2r( tmp_R + length, mem2_ext + HILBERT_ORDER2, HILBERT_ORDER2 );
222 628264 : mvr2r( tmp_I + length, mem3_ext + HILBERT_ORDER2, HILBERT_ORDER2 );
223 :
224 628264 : if ( *phase_state >= period )
225 : {
226 0 : *phase_state = 0;
227 : }
228 :
229 23854144 : for ( i = 0, j = *phase_state; i < length; )
230 : {
231 408204040 : for ( ; ( j < period ) && ( i < length ); j++, i++ )
232 : {
233 384978160 : output[i] = tmp_R[i + HILBERT_ORDER2] * local_cos_table[j] + tmp_I[i + HILBERT_ORDER2] * local_negsin_table[j];
234 : }
235 :
236 23225880 : if ( j >= period )
237 : {
238 22631328 : j = 0;
239 : }
240 : }
241 :
242 628264 : *phase_state = j;
243 :
244 628264 : return;
245 : }
246 :
247 : /*----------------------------------------------
248 : * Hilbert_transform()
249 : *
250 : * Hilbert transform
251 : *------------------------------------------------*/
252 :
253 2513056 : static void Hilbert_transform(
254 : float tmp_R[], /* i : Real component of HB */
255 : float tmp_I[], /* i : Real component of HB */
256 : float tmpi_R[], /* o : Real component of HB */
257 : float tmpi_I[], /* o : Imag. component of HB */
258 : const int16_t length, /* i : input length */
259 : const int16_t HB_stage_id /* i : HB transform stage */
260 : )
261 : {
262 : int16_t i, hb_filter_stage, offset;
263 :
264 2513056 : hb_filter_stage = 2 * HB_stage_id;
265 2513056 : offset = ( HB_stage_id == 0 ) ? 1 : 0;
266 :
267 2513056 : if ( HB_stage_id == 0 || HB_stage_id == 2 )
268 : {
269 771212848 : for ( i = 0; i < length; i++ )
270 : {
271 769956320 : tmpi_R[i] = tmp_R[i + 4] * Hilbert_coeffs[hb_filter_stage][0 + offset] + tmp_R[i + 2] * Hilbert_coeffs[hb_filter_stage][2 + offset] + tmp_R[i] * Hilbert_coeffs[hb_filter_stage][4 + offset];
272 :
273 769956320 : tmpi_I[i] = tmp_I[i + 4 + offset] * Hilbert_coeffs[hb_filter_stage + 1][0] + tmp_I[i + 2 + offset] * Hilbert_coeffs[hb_filter_stage + 1][2] + tmp_I[i + offset] * Hilbert_coeffs[hb_filter_stage + 1][4];
274 : }
275 : }
276 1256528 : else if ( HB_stage_id == 1 || HB_stage_id == 3 )
277 : {
278 771212848 : for ( i = 0; i < length; i++ )
279 : {
280 769956320 : tmpi_R[i + 4] = tmp_R[i] - tmpi_R[i + 2] * Hilbert_coeffs[hb_filter_stage][2] - tmpi_R[i] * Hilbert_coeffs[hb_filter_stage][4];
281 :
282 769956320 : tmpi_I[i + 4] = tmp_I[i] - tmpi_I[i + 2] * Hilbert_coeffs[hb_filter_stage + 1][2] - tmpi_I[i] * Hilbert_coeffs[hb_filter_stage + 1][4];
283 : }
284 : }
285 :
286 2513056 : return;
287 : }
288 :
289 : /*-------------------------------------------------------------------*
290 : * flip_spectrum()
291 : *
292 : *
293 : *-------------------------------------------------------------------*/
294 :
295 661629 : void flip_spectrum(
296 : const float input[], /* i : input spectrum */
297 : float output[], /* o : output spectrum */
298 : const int16_t length /* i : vector length */
299 : )
300 : {
301 : int16_t i;
302 :
303 106522269 : for ( i = 0; i < length; i = i + 2 )
304 : {
305 105860640 : output[i] = -input[i];
306 105860640 : output[i + 1] = input[i + 1];
307 : }
308 :
309 661629 : return;
310 : }
311 :
312 : /*-------------------------------------------------------------------*
313 : * flip_spectrum_and_decimby4()
314 : *
315 : *
316 : *-------------------------------------------------------------------*/
317 :
318 357424 : void flip_spectrum_and_decimby4(
319 : const float input[], /* i : input spectrum */
320 : float output[], /* o : output spectrum */
321 : const int16_t length, /* i : vector length */
322 : float mem1[], /* i/o: memory */
323 : float mem2[], /* i/o: memory */
324 : const int16_t ramp_flag /* i : flag to trigger slow ramp-up of output following change of core (HQ to ACELP or 12k8 to 16k ACELP) */
325 : )
326 : {
327 : int16_t i;
328 : float factor, tmp[L_FRAME16k / 2];
329 : float input_change[L_FRAME16k];
330 :
331 357424 : if ( ramp_flag )
332 : {
333 1583 : factor = 4.0f / length;
334 64903 : for ( i = 0; i < length / 4; i = i + 2 )
335 : {
336 63320 : input_change[i] = -input[i] * ( i * factor );
337 63320 : input_change[i + 1] = input[i + 1] * ( ( i + 1.0f ) * factor );
338 : }
339 : }
340 : else
341 : {
342 355841 : i = 0;
343 : }
344 :
345 53971362 : for ( ; i < length; i = i + 2 )
346 : {
347 53613938 : input_change[i] = -input[i];
348 53613938 : input_change[i + 1] = input[i + 1];
349 : }
350 :
351 357424 : Decimate_allpass_steep( input_change, mem1, length, tmp );
352 357424 : Decimate_allpass_steep( tmp, mem2, length / 2, output );
353 :
354 357424 : return;
355 : }
356 :
357 : /*-------------------------------------------------------------------*
358 : * GenShapedWBExcitation()
359 : *
360 : * Synthesize spectrally shaped highband excitation signal for the wideband
361 : *-------------------------------------------------------------------*/
362 :
363 293637 : void GenShapedWBExcitation(
364 : float *excSHB, /* o : synthesized shaped shb exctiation */
365 : const float *lpc_shb, /* i : lpc coefficients */
366 : float *exc4kWhtnd, /* o : whitened synthesized shb excitation */
367 : float *mem_csfilt, /* i/o: memory */
368 : float *mem_genSHBexc_filt_down1, /* i/o: memory */
369 : float *mem_genSHBexc_filt_down2, /* i/o: memory */
370 : float *mem_genSHBexc_filt_down3, /* i/o: memory */
371 : float *state_lpc_syn, /* i/o: memory */
372 : const int16_t coder_type, /* i : coding type */
373 : #ifdef FIX_1465_SWB_TBE_RANDOM_VECTOR_CREATION
374 : const int16_t element_mode, /* i : element mode */
375 : #endif
376 : const float *bwe_exc_extended, /* i : bandwidth extended exciatation */
377 : int16_t bwe_seed[], /* i/o: random number generator seed */
378 : const float voice_factors[], /* i : voicing factor */
379 : const int16_t uv_flag, /* i : unvoiced flag */
380 : const int16_t igf_flag )
381 : {
382 : int16_t i, j, k;
383 : float wht_fil_mem[LPC_WHTN_ORDER_WB];
384 : float lpc_whtn[LPC_WHTN_ORDER_WB + 1];
385 : float R[LPC_WHTN_ORDER_WB + 2];
386 : float excTmp[L_FRAME16k];
387 : float excTmp2[L_FRAME16k / 4];
388 : float exc4k[L_FRAME16k / 4];
389 : float pow1, pow22, scale;
390 : float excNoisyEnv[L_FRAME16k / 4];
391 293637 : float csfilt_num2[1] = { 0.05f };
392 293637 : float csfilt_den2[2] = { 1.0f, -0.96f };
393 : float temp1, temp2;
394 : float ervec[LPC_WHTN_ORDER_WB + 2];
395 : float tmp_vfac;
396 293637 : float avg_voice_fac = 0.25f * sum_f( voice_factors, NB_SUBFR );
397 :
398 293637 : if ( igf_flag && ( coder_type == VOICED || avg_voice_fac > 0.35f ) )
399 : {
400 2725 : csfilt_num2[0] = 0.2f;
401 2725 : csfilt_den2[1] = -0.8f;
402 : }
403 290912 : else if ( igf_flag && ( coder_type == UNVOICED || avg_voice_fac < 0.2f ) )
404 : {
405 1021 : csfilt_num2[0] = 0.01f;
406 1021 : csfilt_den2[1] = -0.99f;
407 : }
408 293637 : set_f( wht_fil_mem, 0, LPC_WHTN_ORDER_WB );
409 :
410 293637 : Decimate_allpass_steep( bwe_exc_extended, mem_genSHBexc_filt_down1, L_FRAME32k, excTmp );
411 :
412 293637 : flip_spectrum_and_decimby4( excTmp, exc4k, L_FRAME16k, mem_genSHBexc_filt_down2, mem_genSHBexc_filt_down3, 0 );
413 :
414 293637 : if ( uv_flag )
415 : {
416 : /* unvoiced signal */
417 : #ifdef FIX_1465_SWB_TBE_RANDOM_VECTOR_CREATION
418 5514 : create_random_vector( exc4kWhtnd, L_FRAME16k / 4, bwe_seed, element_mode );
419 : #else
420 : create_random_vector( exc4kWhtnd, L_FRAME16k / 4, bwe_seed );
421 : #endif
422 : }
423 : else
424 : {
425 288123 : autocorr( exc4k, R, LPC_WHTN_ORDER_WB + 1, L_FRAME16k / 4, win_flatten_4k, 0, 1, 1 );
426 :
427 : /* Ensure R[0] isn't zero when entering Levinson Durbin */
428 288123 : R[0] = max( R[0], 1.0e-8f );
429 1152492 : for ( i = 0; i <= LPC_WHTN_ORDER_WB; i++ )
430 : {
431 864369 : R[i] = R[i] * wac[i];
432 : }
433 288123 : lev_dur( lpc_whtn, R, LPC_WHTN_ORDER_WB, ervec );
434 :
435 288123 : fir( exc4k, lpc_whtn, exc4kWhtnd, wht_fil_mem, L_FRAME16k / 4, LPC_WHTN_ORDER_WB, 0 );
436 :
437 : /* Ensure pow1 is greater than zero when computing normalization */
438 23337963 : for ( i = 0, pow1 = 0.00001f; i < L_FRAME16k / 4; i++ )
439 : {
440 23049840 : excTmp2[i] = (float) ( fabs( exc4kWhtnd[i] ) );
441 23049840 : pow1 += exc4kWhtnd[i] * exc4kWhtnd[i];
442 : }
443 :
444 23337963 : for ( i = 0; i < L_FRAME16k / 4; i++ )
445 : {
446 23049840 : excNoisyEnv[i] = *mem_csfilt + csfilt_num2[0] * excTmp2[i];
447 23049840 : *mem_csfilt = -csfilt_den2[1] * excNoisyEnv[i];
448 : }
449 :
450 : #ifdef FIX_1465_SWB_TBE_RANDOM_VECTOR_CREATION
451 288123 : create_random_vector( exc4k, L_FRAME16k / 4, bwe_seed, element_mode );
452 : #else
453 : create_random_vector( exc4k, L_FRAME16k / 4, bwe_seed );
454 : #endif
455 :
456 : /* Ensure pow22 is greater than zero when computing normalization */
457 23337963 : for ( i = 0, pow22 = 0.00001f; i < L_FRAME16k / 4; i++ )
458 : {
459 23049840 : exc4k[i] *= excNoisyEnv[i];
460 23049840 : pow22 += exc4k[i] * exc4k[i];
461 : }
462 :
463 288123 : if ( coder_type == UNVOICED || ( igf_flag && avg_voice_fac < 0.2f ) )
464 : {
465 6935 : scale = (float) sqrt( pow1 / pow22 );
466 6935 : if ( pow22 == 0.f )
467 : {
468 0 : scale = 0;
469 : }
470 :
471 561735 : for ( i = 0; i < L_FRAME16k / 4; i++ )
472 : {
473 554800 : exc4kWhtnd[i] = exc4k[i] * scale;
474 : }
475 : }
476 : else
477 : {
478 1405940 : for ( i = 0, k = 0; i < 4; i++ )
479 : {
480 :
481 1124752 : if ( igf_flag && coder_type == VOICED )
482 : {
483 9248 : tmp_vfac = 2 * voice_factors[i];
484 9248 : tmp_vfac = min( 1, tmp_vfac );
485 : }
486 : else
487 : {
488 1115504 : tmp_vfac = voice_factors[i];
489 : }
490 :
491 1124752 : temp1 = root_a( tmp_vfac );
492 1124752 : temp2 = root_a_over_b( pow1 * ( 1.0f - tmp_vfac ), pow22 );
493 :
494 :
495 23619792 : for ( j = 0; j < L_FRAME16k / 16; j++, k++ )
496 : {
497 22495040 : exc4kWhtnd[k] = temp1 * exc4kWhtnd[k] + temp2 * exc4k[k];
498 : }
499 : }
500 : }
501 : }
502 :
503 293637 : syn_filt( lpc_shb, LPC_SHB_ORDER_WB, exc4kWhtnd, excSHB, L_FRAME16k / 4, state_lpc_syn, 1 );
504 :
505 293637 : return;
506 : }
507 :
508 : /*-------------------------------------------------------------------*
509 : * GenWBSynth()
510 : *
511 : * Generate 16 KHz sampled highband component from synthesized highband
512 : *-------------------------------------------------------------------*/
513 :
514 276507 : void GenWBSynth(
515 : const float *input_synspeech, /* i : input synthesized speech */
516 : float *shb_syn_speech_16k, /* o : output highband compnent */
517 : float *state_lsyn_filt_shb1, /* i/o: memory */
518 : float *state_lsyn_filt_shb2 /* i/o: memory */
519 : )
520 : {
521 : float speech_buf_16k1[L_FRAME16k], speech_buf_16k2[L_FRAME16k];
522 :
523 276507 : Interpolate_allpass_steep( input_synspeech, state_lsyn_filt_shb1, L_FRAME16k / 4, speech_buf_16k1 );
524 :
525 276507 : Interpolate_allpass_steep( speech_buf_16k1, state_lsyn_filt_shb2, L_FRAME16k / 2, speech_buf_16k2 );
526 :
527 276507 : flip_spectrum( speech_buf_16k2, shb_syn_speech_16k, L_FRAME16k );
528 :
529 276507 : return;
530 : }
531 :
532 : /*-------------------------------------------------------------------*
533 : * PostShortTerm()
534 : *
535 : * Short term processing
536 : *-------------------------------------------------------------------*/
537 :
538 5195240 : void PostShortTerm(
539 : float *sig_in, /* i : input signal (pointer to current subframe */
540 : float *lpccoeff, /* i : LPC coefficients for current subframe */
541 : float *sig_out, /* o : postfiltered output */
542 : float *mem_stp, /* i/o: postfilter memory*/
543 : float *ptr_mem_stp, /* i/o: pointer to postfilter memory*/
544 : float *ptr_gain_prec, /* i/o: for gain adjustment*/
545 : float *mem_zero, /* i/o: null memory to compute h_st*/
546 : const float formant_fac /* i : Strength of post-filter [0,1] */
547 : )
548 : {
549 : float apond1[LPC_SHB_ORDER + 1]; /* denominator coeff.*/
550 : float apond2[LONG_H_ST]; /* numerator coeff. */
551 : float sig_ltp[L_SUBFR16k + 1]; /* residual signal */
552 : float parcor0;
553 : float g1, g2;
554 :
555 5195240 : set_f( apond1, 0, LPC_SHB_ORDER + 1 );
556 5195240 : set_f( apond2, 0, LONG_H_ST );
557 5195240 : set_f( sig_ltp, 0, L_SUBFR16k + 1 );
558 :
559 : /* Obtain post-filter weights */
560 5195240 : g1 = GAMMA0 + GAMMA_SHARP * formant_fac;
561 5195240 : g2 = GAMMA0 - GAMMA_SHARP * formant_fac;
562 :
563 : /* Compute weighted LPC coefficients */
564 5195240 : weight_a( lpccoeff, apond1, g1, LPC_SHB_ORDER );
565 5195240 : weight_a( lpccoeff, apond2, g2, LPC_SHB_ORDER );
566 :
567 : /* Compute A(gamma2) residual */
568 5195240 : residu( apond2, LPC_SHB_ORDER, sig_in, sig_ltp + 1, L_SUBFR16k );
569 :
570 : /* Save last output of 1/A(gamma1) */
571 5195240 : sig_ltp[0] = *ptr_mem_stp;
572 :
573 : /* Control short term pst filter gain and compute parcor0 */
574 5195240 : calc_st_filt( apond2, apond1, &parcor0, sig_ltp + 1, mem_zero, L_SUBFR16k, SWB_TBE );
575 :
576 : /* 1/A(gamma1) filtering, mem_stp is updated */
577 5195240 : syn_filt( apond1, LPC_SHB_ORDER, sig_ltp + 1, sig_ltp + 1, L_SUBFR16k, mem_stp, 1 );
578 :
579 : /* (1 + mu z-1) tilt filtering */
580 5195240 : filt_mu( sig_ltp, sig_out, parcor0, L_SUBFR16k, SWB_TBE );
581 :
582 : /* gain control */
583 5195240 : scale_st( sig_in, sig_out, ptr_gain_prec, L_SUBFR16k, SWB_TBE );
584 :
585 5195240 : return;
586 : }
587 :
588 : /*-------------------------------------------------------------------*
589 : * swb_formant_fac()
590 : *
591 : * Find strength of adaptive formant postfilter using tilt of the high
592 : * band. The 2nd lpc coefficient is used as a tilt approximation.
593 : *-------------------------------------------------------------------*/
594 :
595 : /*! r: Formant filter strength [0,1] */
596 1318713 : float swb_formant_fac(
597 : const float lpc_shb2, /* i : 2nd HB LPC coefficient */
598 : float *tilt_mem /* i/o: Tilt smoothing memory */
599 : )
600 : {
601 : float formant_fac;
602 : float tmp;
603 :
604 : /* Smoothen tilt value */
605 1318713 : tmp = 0.5f * (float) fabs( lpc_shb2 ) + 0.5f * *tilt_mem;
606 1318713 : *tilt_mem = tmp;
607 :
608 : /* Map to PF strength */
609 1318713 : formant_fac = ( tmp - SWB_TILT_LOW ) * SWB_TILT_DELTA;
610 1318713 : if ( formant_fac > 1.0f )
611 : {
612 1131 : formant_fac = 1.0f;
613 : }
614 1317582 : else if ( formant_fac < 0.0f )
615 : {
616 1217827 : formant_fac = 0.0f;
617 : }
618 :
619 1318713 : formant_fac = 1.0f - 0.5f * formant_fac;
620 :
621 1318713 : return formant_fac;
622 : }
623 :
624 13260 : void find_td_envelope(
625 : const float inp[], /* i : input signal */
626 : const int16_t len, /* i : length of the input signal */
627 : const int16_t len_h, /* i : length of the MA filter */
628 : float mem_h[], /* i/o: memory of the MA filter, length len_h/2 */
629 : float out[] /* o : td envelope of the input signal */
630 : )
631 : {
632 : int16_t k, K;
633 : float buf_in[L_FRAME16k + MAX_LEN_MA_FILTER], *p_in, *p_out, *p_prev, w;
634 :
635 13260 : assert( len > 0 && len <= L_FRAME16k );
636 :
637 13260 : K = (int16_t) ( len_h / 2 ); /* length of FIR filter memory = half of the total filter length */
638 13260 : w = 1.0f / len_h; /* MA filtering coefficient */
639 :
640 : /* copy filter memory to the input buffer */
641 13260 : if ( mem_h != NULL )
642 : {
643 4420 : mvr2r( mem_h, buf_in, K );
644 : }
645 : else
646 : {
647 : /* no memory available, use the first len_h/2 samples as memory */
648 8840 : p_in = buf_in;
649 97240 : for ( k = 0; k < K; k++ )
650 : {
651 88400 : *p_in++ = (float) fabs( inp[k] ) * w;
652 : }
653 : }
654 :
655 : /* take the absolute value of the input signal and copy it to the input buffer */
656 : /* multiply each value by 1 / filter length */
657 13260 : p_in = &buf_in[K];
658 4256460 : for ( k = 0; k < len; k++ )
659 : {
660 4243200 : *p_in++ = (float) fabs( inp[k] ) * w;
661 : }
662 :
663 : /* update filter memory from the end of the input buffer */
664 13260 : if ( mem_h != NULL )
665 : {
666 4420 : mvr2r( &buf_in[len], mem_h, K );
667 : }
668 :
669 : /* do MA filtering */
670 13260 : out[0] = sum_f( buf_in, len_h );
671 13260 : p_out = &buf_in[0]; /* pointer to leaving sample */
672 13260 : p_in = &buf_in[len_h]; /* pointer to entering sample*/
673 4110600 : for ( k = 1; k < len - K; k++ )
674 : {
675 4097340 : out[k] = out[k - 1] - *p_out++ + *p_in++;
676 : }
677 :
678 : /* use IIR filtering to extrapolate the last K samples */
679 13260 : p_in = &buf_in[len - K];
680 13260 : p_out = &out[len - K];
681 13260 : p_prev = p_out - 1;
682 145860 : for ( k = 0; k < K; k++ )
683 : {
684 132600 : *p_out++ = 0.05f * ( *p_in++ ) + 0.95f * ( *p_prev++ );
685 : }
686 :
687 13260 : return;
688 : }
689 :
690 : /*-------------------------------------------------------------------*
691 : * GenShapedSHBExcitation()
692 : *
693 : * Synthesize spectrally shaped highband excitation signal
694 : *-------------------------------------------------------------------*/
695 :
696 1318713 : void GenShapedSHBExcitation(
697 : float *excSHB, /* o : synthesized shaped shb excitation */
698 : const float *lpc_shb, /* i : lpc coefficients */
699 : float *White_exc16k_FB, /* o : white excitation for the Fullband extension */
700 : float *mem_csfilt, /* i/o: memory */
701 : float *mem_genSHBexc_filt_down_shb, /* i/o: memory */
702 : float *state_lpc_syn, /* i/o: memory */
703 : const int16_t coder_type, /* i : coding type */
704 : const float *bwe_exc_extended, /* i : bandwidth extended excitation */
705 : int16_t bwe_seed[], /* i/o: random number generator seed */
706 : float voice_factors[], /* i : voicing factor */
707 : const int16_t extl, /* i : extension layer */
708 : float *tbe_demph, /* i/o: de-emphasis memory */
709 : float *tbe_premph, /* i/o: pre-emphasis memory */
710 : float *lpc_shb_sf, /* i : LP coefficients */
711 : float *shb_ener_sf,
712 : float *shb_res_gshape,
713 : float *shb_res,
714 : int16_t *vf_ind,
715 : const float formant_fac, /* i : Formant sharpening factor [0..1] */
716 : float fb_state_lpc_syn[], /* i/o: memory */
717 : float *fb_tbe_demph, /* i/o: fb de-emphasis memory */
718 : const int32_t total_brate, /* i : bitrate */
719 : const int16_t prev_bfi, /* i : previous frame was concealed */
720 : const int16_t element_mode, /* i : element mode */
721 : const int16_t flag_ACELP16k, /* i : ACELP@16kHz flag */
722 : float *nlExc16k, /* i/o: NL exc for IC-BWE */
723 : float *mixExc16k, /* i/o: exc spreading for IC-BWE */
724 : const int32_t extl_brate, /* i : extension layer bitarte */
725 : const int16_t MSFlag, /* i : Multi Source flag */
726 : float EnvSHBres_4k[], /* i/o: TD envelope of the SHB residual signal */
727 : float *prev_pow_exc16kWhtnd, /* i/o: power of the LB excitation signal in the previous frame */
728 : float *prev_mix_factor, /* i/o: mixing factor in the previous frame */
729 : float *Env_error, /* o : error in SHB residual envelope modelling*/
730 : float Env_error_part[] /* o : per-segment error in SHB residual envelope modelling */
731 : )
732 : {
733 : int16_t i, j, k;
734 : float wht_fil_mem[LPC_WHTN_ORDER];
735 : float lpc_whtn[LPC_WHTN_ORDER + 1];
736 : float R[LPC_WHTN_ORDER + 2];
737 : float exc32k[L_FRAME32k], exc16k[L_FRAME16k];
738 : float pow1, pow22, scale, temp1, temp2;
739 : float excTmp2[L_FRAME16k];
740 : int16_t nbSubFr;
741 : float excNoisyEnv[L_FRAME16k];
742 1318713 : float csfilt_num2[1] = { 0.2f };
743 1318713 : float csfilt_den2[2] = { 1.0f, -0.8f };
744 : float varEnvShape;
745 : float ervec[LPC_WHTN_ORDER + 2];
746 : float exc16kWhtnd[L_FRAME16k];
747 1318713 : float temp = 0.0f;
748 : float *White_exc16k;
749 : float voiceFacEst[NB_SUBFR16k];
750 : float syn_shb_ener_sf[4], tempSHB[80];
751 : float zero_mem[LPC_SHB_ORDER];
752 : float vf_tmp;
753 : float White_exc16k_FB_temp[L_FRAME16k];
754 1318713 : float fb_deemph_fac = 0.48f;
755 : double tempD;
756 : float alpha, step, mem_csfilt_left, mem_csfilt_right, excNoisyEnvLeft[L_FRAME16k], excNoisyEnvRight[L_FRAME16k];
757 : int16_t cbsize;
758 : float mix_factor, old_fact, new_fact, fact, old_scale, new_scale, step_scale;
759 : float c0, c1, c2, c3, c4, c5, g1, g2, g, den;
760 : float EnvWhiteExc16k[L_FRAME16k], EnvExc16kWhtnd[L_FRAME16k];
761 : float EnvWhiteExc16k_4k[L_FRAME4k], EnvExc16kWhtnd_4k[L_FRAME4k];
762 : int16_t flag_plosive;
763 : float delta;
764 : float c0_part[NUM_SHB_SUBGAINS], c1_part[NUM_SHB_SUBGAINS], c2_part[NUM_SHB_SUBGAINS], c3_part[NUM_SHB_SUBGAINS], c4_part[NUM_SHB_SUBGAINS], c5_part[NUM_SHB_SUBGAINS];
765 :
766 1318713 : mix_factor = 0.0f;
767 :
768 1318713 : set_f( zero_mem, 0, LPC_SHB_ORDER );
769 1318713 : set_f( wht_fil_mem, 0, LPC_WHTN_ORDER );
770 1318713 : set_f( EnvWhiteExc16k_4k, 0, L_FRAME4k );
771 1318713 : set_f( EnvExc16kWhtnd_4k, 0, L_FRAME4k );
772 :
773 : /* Mirror the spectrum */
774 845295033 : for ( i = 0; i < L_FRAME32k; i++ )
775 : {
776 843976320 : exc32k[i] = ( ( i % 2 ) == 0 ) ? ( -bwe_exc_extended[i] ) : ( bwe_exc_extended[i] );
777 : }
778 :
779 : /* Decimate by 2 */
780 1318713 : Decimate_allpass_steep( exc32k, mem_genSHBexc_filt_down_shb, 2 * L_FRAME16k, exc16k );
781 :
782 1318713 : autocorr( exc16k, R, LPC_WHTN_ORDER + 1, L_FRAME16k, win_flatten, 0, 1, 1 );
783 :
784 : /* Ensure R[0] isn't zero when entering Levinson-Durbin */
785 1318713 : R[0] = max( R[0], 1.0e-8f );
786 7912278 : for ( i = 0; i <= LPC_WHTN_ORDER; i++ )
787 : {
788 6593565 : R[i] = R[i] * wac[i];
789 : }
790 :
791 : /* Ensure R[0] isn't zero when entering Levinson-Durbin */
792 1318713 : R[0] += 1.0e-8f;
793 :
794 1318713 : lev_dur( lpc_whtn, R, LPC_WHTN_ORDER, ervec );
795 :
796 1318713 : fir( exc16k, lpc_whtn, exc16kWhtnd, wht_fil_mem, L_FRAME16k, LPC_WHTN_ORDER, 0 );
797 :
798 1318713 : if ( extl_brate >= SWB_TBE_2k8 )
799 : {
800 97015509 : for ( i = 0; i < L_FRAME16k; i++ )
801 : {
802 96713280 : exc16kWhtnd[i] *= shb_res_gshape[(int16_t) ( i / 80 )];
803 : }
804 : }
805 :
806 423306873 : for ( k = 0, pow1 = 0.00001f; k < L_FRAME16k; k++ )
807 : {
808 421988160 : excTmp2[k] = (float) ( fabs( exc16kWhtnd[k] ) );
809 421988160 : pow1 += exc16kWhtnd[k] * exc16kWhtnd[k];
810 : }
811 :
812 1318713 : if ( !flag_ACELP16k )
813 : {
814 716920 : varEnvShape = mean( voice_factors, NB_SUBFR );
815 : }
816 : else
817 : {
818 601793 : varEnvShape = mean( voice_factors, NB_SUBFR16k );
819 : }
820 :
821 1318713 : if ( extl == FB_TBE )
822 : {
823 385122 : fb_deemph_fac = max( ( 0.68f - (float) pow( varEnvShape, 3 ) ), 0.48f );
824 : }
825 :
826 1318713 : varEnvShape = 1.09875f - 0.49875f * varEnvShape;
827 1318713 : varEnvShape = min( max( varEnvShape, 0.6f ), 0.999f );
828 1318713 : csfilt_num2[0] = 1.0f - varEnvShape;
829 1318713 : csfilt_den2[1] = -varEnvShape;
830 :
831 1318713 : if ( element_mode == EVS_MONO && *mem_csfilt == 0 && ( total_brate == ACELP_9k60 || total_brate == ACELP_16k40 || total_brate == ACELP_24k40 ) )
832 : {
833 : /* pre-init smoothing avoid energy drop outs */
834 960 : float tmp_scale = 0;
835 20160 : for ( i = 0; i < L_SUBFR16k / 4; i++ )
836 : {
837 19200 : tmp_scale += excTmp2[i];
838 : }
839 :
840 : /* don't apply for FB in case the FB start-frame was potentially lost - White_exc16k is very sensitive to enery mismatch between enc - dec */
841 : /* rather stick to the more conservative approach, to avoid potential clippings */
842 960 : if ( !( prev_bfi && extl == FB_TBE ) )
843 : {
844 : /* use weak smoothing for 1st frame after switching to make filter recover more quickly */
845 924 : varEnvShape = 0.8f;
846 924 : csfilt_num2[0] = 1.0f - varEnvShape;
847 924 : csfilt_den2[1] = -varEnvShape;
848 : }
849 960 : *mem_csfilt = varEnvShape * ( tmp_scale / ( L_SUBFR16k / 4 ) );
850 : }
851 :
852 1318713 : if ( MSFlag > 0 )
853 : {
854 50895 : varEnvShape = 0.995f;
855 50895 : csfilt_num2[0] = 1.0f - varEnvShape;
856 50895 : csfilt_den2[1] = -varEnvShape;
857 : }
858 :
859 1318713 : White_exc16k = exc16k;
860 :
861 : /* Track the low band envelope */
862 1318713 : if ( element_mode == IVAS_CPE_TD || element_mode == IVAS_CPE_DFT )
863 : {
864 297584 : if ( extl_brate != SWB_TBE_1k10 && extl_brate != SWB_TBE_1k75 )
865 : {
866 277681 : mem_csfilt_left = 0.0f;
867 277681 : mem_csfilt_right = 0.0f;
868 89135601 : for ( k = 0; k < L_FRAME16k; k++ )
869 : {
870 88857920 : excNoisyEnvLeft[k] = mem_csfilt_left + csfilt_num2[0] * excTmp2[k];
871 88857920 : mem_csfilt_left = -csfilt_den2[1] * excNoisyEnvLeft[k];
872 88857920 : excNoisyEnvRight[L_FRAME16k - k - 1] = mem_csfilt_right + csfilt_num2[0] * excTmp2[L_FRAME16k - k - 1];
873 88857920 : mem_csfilt_right = -csfilt_den2[1] * excNoisyEnvRight[L_FRAME16k - k - 1];
874 : }
875 :
876 277681 : alpha = 0.0f;
877 277681 : step = 1.0f / L_FRAME16k;
878 89135601 : for ( k = 0; k < L_FRAME16k; k++ )
879 : {
880 88857920 : excNoisyEnv[k] = alpha * excNoisyEnvLeft[k] + ( 1 - alpha ) * excNoisyEnvRight[k];
881 88857920 : alpha += step;
882 : }
883 : }
884 : }
885 : else
886 : {
887 327782409 : for ( k = 0; k < L_FRAME16k; k++ )
888 : {
889 326761280 : excNoisyEnv[k] = *mem_csfilt + csfilt_num2[0] * excTmp2[k];
890 326761280 : *mem_csfilt = -csfilt_den2[1] * excNoisyEnv[k];
891 : }
892 : }
893 :
894 1318713 : if ( extl_brate == SWB_TBE_1k10 || extl_brate == SWB_TBE_1k75 )
895 : {
896 : /* generate gaussian (white) excitation */
897 6388863 : for ( k = 0; k < L_FRAME16k; k++ )
898 : {
899 6368960 : White_exc16k[k] = (float) own_random( &bwe_seed[0] );
900 : }
901 :
902 : /* normalize the amplitude of the gaussian excitation to that of the LB exc. */
903 19903 : pow22 = POW_EXC16k_WHTND;
904 19903 : v_multc( White_exc16k, (float) sqrt( pow1 / pow22 ), White_exc16k, L_FRAME16k );
905 : }
906 : else
907 : {
908 : #ifdef FIX_1465_SWB_TBE_RANDOM_VECTOR_CREATION
909 1298810 : create_random_vector( White_exc16k, L_FRAME, bwe_seed, element_mode );
910 1298810 : create_random_vector( White_exc16k + L_FRAME, L_FRAME16k - L_FRAME, bwe_seed, element_mode );
911 : #else
912 : create_random_vector( White_exc16k, L_FRAME, bwe_seed );
913 : create_random_vector( White_exc16k + L_FRAME, L_FRAME16k - L_FRAME, bwe_seed );
914 : #endif
915 :
916 416918010 : for ( k = 0, pow22 = 0.00001f; k < L_FRAME16k; k++ )
917 : {
918 415619200 : White_exc16k[k] *= excNoisyEnv[k];
919 415619200 : pow22 += White_exc16k[k] * White_exc16k[k];
920 : }
921 : }
922 :
923 1318713 : flag_plosive = 0;
924 :
925 1318713 : if ( extl_brate >= SWB_TBE_2k8 || extl_brate == SWB_TBE_1k10 || extl_brate == SWB_TBE_1k75 )
926 : {
927 322132 : if ( *vf_ind == 20 ) /* encoder side */
928 : {
929 71404 : if ( extl_brate == SWB_TBE_1k10 || extl_brate == SWB_TBE_1k75 )
930 : {
931 : /* calculate TD envelopes of exc16kWhtnd and White_exc16k */
932 4420 : find_td_envelope( White_exc16k, L_FRAME16k, 20, NULL, EnvWhiteExc16k );
933 4420 : find_td_envelope( exc16kWhtnd, L_FRAME16k, 20, NULL, EnvExc16kWhtnd );
934 :
935 358020 : for ( k = 0; k < L_FRAME4k; k++ )
936 : {
937 353600 : EnvWhiteExc16k_4k[k] = EnvWhiteExc16k[4 * k];
938 353600 : EnvExc16kWhtnd_4k[k] = EnvExc16kWhtnd[4 * k];
939 : }
940 :
941 : /* calculate the optimal mix factor */
942 4420 : c0 = c1 = c2 = c3 = c4 = c5 = 0.0f;
943 22100 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
944 : {
945 17680 : c0_part[i] = sum2_f( &EnvExc16kWhtnd_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], L_FRAME4k / NUM_SHB_SUBGAINS );
946 17680 : c1_part[i] = -2.0f * dotp( &EnvSHBres_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], &EnvExc16kWhtnd_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], L_FRAME4k / NUM_SHB_SUBGAINS );
947 17680 : c2_part[i] = sum2_f( &EnvWhiteExc16k_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], L_FRAME4k / NUM_SHB_SUBGAINS );
948 17680 : c3_part[i] = -2.0f * dotp( &EnvSHBres_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], &EnvWhiteExc16k_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], L_FRAME4k / NUM_SHB_SUBGAINS );
949 17680 : c4_part[i] = 2.0f * dotp( &EnvExc16kWhtnd_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], &EnvWhiteExc16k_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], L_FRAME4k / NUM_SHB_SUBGAINS );
950 17680 : c5_part[i] = sum2_f( &EnvSHBres_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], L_FRAME4k / NUM_SHB_SUBGAINS );
951 :
952 17680 : c0 += c0_part[i];
953 17680 : c1 += c1_part[i];
954 17680 : c2 += c2_part[i];
955 17680 : c3 += c3_part[i];
956 17680 : c4 += c4_part[i];
957 17680 : c5 += c5_part[i];
958 : }
959 :
960 4420 : den = 4.0f * c0 * c2 - c4 * c4;
961 :
962 4420 : if ( den == 0.0f )
963 : {
964 0 : den = 1e-7f;
965 : }
966 :
967 4420 : g1 = ( c3 * c4 - 2 * c1 * c2 ) / den;
968 4420 : g2 = ( c1 * c4 - 2 * c0 * c3 ) / den;
969 :
970 4420 : *Env_error = 0.0f;
971 4420 : flag_plosive = 0;
972 22100 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
973 : {
974 17680 : Env_error_part[i] = c5_part[i] + g1 * g1 * c0_part[i] + g1 * c1_part[i] + g2 * g2 * c2_part[i] + g2 * c3_part[i] + g1 * g2 * c4_part[i];
975 17680 : *Env_error += Env_error_part[i];
976 :
977 17680 : if ( Env_error_part[i] > THR_ENV_ERROR_PLOSIVE )
978 : {
979 : /* envelope error is too high -> likely a plosive */
980 40 : flag_plosive = 1;
981 : }
982 : }
983 :
984 4420 : if ( flag_plosive )
985 : {
986 : /* plosive detected -> set the mixing factor to 0 */
987 22 : *vf_ind = 0;
988 22 : mix_factor = 0.0f;
989 : }
990 : else
991 : {
992 : /* normalize gain */
993 4398 : temp = 0.0f;
994 4398 : if ( g1 + g2 == 0.0f )
995 : {
996 0 : temp = 1e-7f;
997 : }
998 :
999 4398 : g = g2 / ( g1 + g2 + temp );
1000 :
1001 : /* quantization of the mixing factor */
1002 4398 : cbsize = 1 << NUM_BITS_SHB_VF;
1003 4398 : delta = 1.0f / ( cbsize - 1 );
1004 4398 : if ( g > 1.0f )
1005 : {
1006 834 : g = 1.0f;
1007 : }
1008 3564 : else if ( g < delta )
1009 : {
1010 : /* prevent low gains to be quantized to 0 as this is reserved for plosives */
1011 12 : g = delta;
1012 : }
1013 :
1014 4398 : *vf_ind = usquant( g, &mix_factor, 0.0f, 1.0f / ( cbsize - 1 ), cbsize );
1015 : }
1016 : }
1017 : else
1018 : {
1019 66984 : Estimate_mix_factors( shb_res, exc16kWhtnd, White_exc16k, pow1, pow22, voiceFacEst, vf_ind );
1020 66984 : temp = ( voiceFacEst[0] > 0.7f ) ? 1.0f : 0.8f;
1021 : }
1022 : }
1023 : else /* decoder side */
1024 : {
1025 250728 : if ( extl_brate == SWB_TBE_1k10 || extl_brate == SWB_TBE_1k75 )
1026 : {
1027 15483 : if ( *vf_ind == 0 )
1028 : {
1029 84 : mix_factor = 0.0f;
1030 84 : flag_plosive = 1;
1031 : }
1032 : else
1033 : {
1034 15399 : mix_factor = usdequant( *vf_ind, 0.0f, 1.0f / ( ( 1 << NUM_BITS_SHB_VF ) - 1 ) );
1035 : }
1036 : }
1037 : else
1038 : {
1039 235245 : temp = ( ( *vf_ind * 0.125f ) > 0.7f ) ? 1.0f : 0.8f;
1040 : }
1041 : }
1042 :
1043 322132 : if ( extl_brate != SWB_TBE_1k10 && extl_brate != SWB_TBE_1k75 )
1044 : {
1045 1813374 : for ( i = 0; i < NB_SUBFR16k; i++ )
1046 : {
1047 1511145 : voice_factors[i] *= temp;
1048 : }
1049 : }
1050 : }
1051 :
1052 1318713 : if ( element_mode >= IVAS_CPE_DFT && nlExc16k != NULL )
1053 : {
1054 : /* save buffers for IC-BWE */
1055 163471 : mvr2r( exc16kWhtnd, nlExc16k, L_FRAME16k );
1056 163471 : v_multc( White_exc16k, (float) sqrt( pow1 / pow22 ), mixExc16k, L_FRAME16k );
1057 : }
1058 :
1059 1318713 : mvr2r( White_exc16k, White_exc16k_FB, L_FRAME16k );
1060 1318713 : deemph( White_exc16k, PREEMPH_FAC, L_FRAME16k, tbe_demph );
1061 :
1062 1318713 : if ( extl_brate == SWB_TBE_1k10 || extl_brate == SWB_TBE_1k75 )
1063 : {
1064 19903 : if ( !flag_plosive ) /* use only LB excitation in case of plosives */
1065 : {
1066 : /* re-scale gaussian excitation at the beginning to gradually move from old energy to new energy */
1067 19797 : old_scale = (float) sqrt( *prev_pow_exc16kWhtnd / pow1 );
1068 19797 : new_scale = 1.0f;
1069 19797 : step_scale = ( new_scale - old_scale ) / ( L_FRAME16k / 2 );
1070 19797 : scale = old_scale;
1071 :
1072 : /* interpolate between the old and the new value of the mixing factor */
1073 19797 : old_fact = *prev_mix_factor;
1074 19797 : new_fact = mix_factor;
1075 19797 : step = ( new_fact - old_fact ) / ( L_FRAME16k / 2 );
1076 19797 : fact = old_fact;
1077 :
1078 : /* mixing of LB and gaussian excitation in the first half of the frame */
1079 3187317 : for ( k = 0; k < L_FRAME16k / 2; k++ )
1080 : {
1081 3167520 : exc16kWhtnd[k] = (float) fact * ( White_exc16k[k] * scale ) + (float) ( 1 - fact ) * exc16kWhtnd[k];
1082 3167520 : fact += step;
1083 3167520 : scale += step_scale;
1084 : }
1085 :
1086 : /* mixing of LB and gaussian excitation in the second half of the frame */
1087 3187317 : for ( ; k < L_FRAME16k; k++ )
1088 : {
1089 3167520 : exc16kWhtnd[k] = (float) new_fact * White_exc16k[k] + (float) ( 1 - new_fact ) * exc16kWhtnd[k];
1090 : }
1091 : }
1092 19903 : preemph( exc16kWhtnd, PREEMPH_FAC, L_FRAME16k, tbe_premph );
1093 : }
1094 : else
1095 : {
1096 1298810 : if ( coder_type == UNVOICED || MSFlag == 1 )
1097 : {
1098 69979 : scale = (float) sqrt( pow1 / pow22 );
1099 69979 : if ( pow22 == 0.f )
1100 : {
1101 0 : scale = 0;
1102 : }
1103 22463259 : for ( k = 0; k < L_FRAME16k; k++ )
1104 : {
1105 22393280 : exc16kWhtnd[k] = White_exc16k[k] * scale;
1106 : }
1107 :
1108 69979 : preemph( exc16kWhtnd, PREEMPH_FAC, L_FRAME16k, tbe_premph );
1109 : }
1110 : else
1111 : {
1112 1228831 : nbSubFr = ( extl_brate < SWB_TBE_2k8 ) ? NB_SUBFR : NB_SUBFR16k; /* note: this condition is designed based on TBE bitrate rather than internal sampling rate */
1113 :
1114 6445696 : for ( i = 0, k = 0; i < nbSubFr; i++ )
1115 : {
1116 5216865 : if ( coder_type == VOICED && extl_brate < SWB_TBE_2k8 )
1117 : {
1118 292068 : temp = (float) sqrt( voice_factors[i] );
1119 292068 : temp1 = (float) sqrt( temp );
1120 292068 : temp2 = (float) sqrt( ( pow1 * ( 1.0f - temp ) ) / pow22 );
1121 292068 : if ( pow22 == 0.f )
1122 : {
1123 0 : temp2 = 0;
1124 : }
1125 : }
1126 : else
1127 : {
1128 : /* Adjust noise mixing for formant sharpening filter */
1129 4924797 : vf_tmp = SWB_NOISE_MIX_FAC * formant_fac;
1130 4924797 : vf_tmp = voice_factors[i] * ( 1.0f - vf_tmp );
1131 :
1132 4924797 : temp1 = (float) sqrt( vf_tmp );
1133 4924797 : temp2 = (float) sqrt( ( pow1 * ( 1.0f - vf_tmp ) ) / pow22 );
1134 4924797 : if ( pow22 == 0.f )
1135 : {
1136 0 : temp2 = 0;
1137 : }
1138 : }
1139 :
1140 398442785 : for ( j = 0; j < L_FRAME16k / nbSubFr; j++, k++ )
1141 : {
1142 393225920 : exc16kWhtnd[k] = temp1 * exc16kWhtnd[k] + temp2 * White_exc16k[k];
1143 : }
1144 :
1145 5216865 : temp = (float) sqrt( 1.0f - voice_factors[i] );
1146 5216865 : temp = PREEMPH_FAC * temp / ( temp1 + temp );
1147 :
1148 5216865 : preemph( &exc16kWhtnd[i * L_FRAME16k / nbSubFr], temp, L_FRAME16k / nbSubFr, tbe_premph );
1149 : }
1150 : }
1151 : }
1152 :
1153 1318713 : if ( extl_brate < SWB_TBE_2k8 )
1154 : {
1155 1016484 : syn_filt( lpc_shb, LPC_SHB_ORDER, exc16kWhtnd, excSHB, L_FRAME16k, state_lpc_syn, 1 );
1156 : }
1157 : else
1158 : {
1159 302229 : set_f( zero_mem, 0, LPC_SHB_ORDER );
1160 302229 : syn_filt( lpc_shb_sf, LPC_SHB_ORDER, exc16kWhtnd, tempSHB, 80, zero_mem, 1 );
1161 302229 : syn_shb_ener_sf[0] = 0.125f * sum2_f( tempSHB, 80 );
1162 302229 : syn_filt( lpc_shb_sf + ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 80, tempSHB, 80, zero_mem, 1 );
1163 302229 : syn_shb_ener_sf[1] = 0.125f * sum2_f( tempSHB, 80 );
1164 302229 : syn_filt( lpc_shb_sf + 2 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 160, tempSHB, 80, zero_mem, 1 );
1165 302229 : syn_shb_ener_sf[2] = 0.125f * sum2_f( tempSHB, 80 );
1166 302229 : syn_filt( lpc_shb_sf + 3 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 240, tempSHB, 80, zero_mem, 1 );
1167 302229 : syn_shb_ener_sf[3] = 0.125f * sum2_f( tempSHB, 80 );
1168 :
1169 302229 : if ( total_brate <= MAX_ACELP_BRATE )
1170 : {
1171 302229 : tempSHB[0] = (float) ( shb_ener_sf[0] ) / ( syn_shb_ener_sf[0] + syn_shb_ener_sf[1] + syn_shb_ener_sf[2] + syn_shb_ener_sf[3] );
1172 302229 : tempD = sqrt( tempSHB[0] );
1173 :
1174 97015509 : for ( i = 0; i < L_FRAME16k; i++ )
1175 : {
1176 96713280 : exc16kWhtnd[i] = (float) ( exc16kWhtnd[i] * tempD );
1177 : }
1178 : }
1179 :
1180 302229 : syn_filt( lpc_shb_sf, LPC_SHB_ORDER, exc16kWhtnd, excSHB, 80, state_lpc_syn, 1 );
1181 302229 : syn_filt( lpc_shb_sf + ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 80, excSHB + 80, 80, state_lpc_syn, 1 );
1182 302229 : syn_filt( lpc_shb_sf + 2 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 160, excSHB + 160, 80, state_lpc_syn, 1 );
1183 302229 : syn_filt( lpc_shb_sf + 3 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 240, excSHB + 240, 80, state_lpc_syn, 1 );
1184 : }
1185 :
1186 1318713 : if ( extl == FB_TBE )
1187 : {
1188 385122 : syn_filt( lpc_shb, LPC_SHB_ORDER, White_exc16k_FB, White_exc16k_FB_temp, L_FRAME16k, fb_state_lpc_syn, 1 );
1189 :
1190 123624162 : for ( i = 0; i < L_FRAME16k; i++ )
1191 : {
1192 123239040 : White_exc16k_FB_temp[i] *= cos_fb_exc[i % 32];
1193 : }
1194 :
1195 385122 : flip_spectrum( White_exc16k_FB_temp, White_exc16k_FB, L_FRAME16k );
1196 :
1197 385122 : deemph( White_exc16k_FB, fb_deemph_fac, L_FRAME16k, fb_tbe_demph );
1198 : }
1199 : else
1200 : {
1201 299682711 : for ( i = 0; i < L_FRAME16k; i++ )
1202 : {
1203 298749120 : White_exc16k_FB[i] = 0.0f;
1204 : }
1205 : }
1206 :
1207 1318713 : *prev_pow_exc16kWhtnd = pow1;
1208 1318713 : *prev_mix_factor = mix_factor;
1209 :
1210 1318713 : return;
1211 : }
1212 :
1213 : /*-------------------------------------------------------------------*
1214 : * GenSHBSynth()
1215 : *
1216 : * Generate 32 KHz sampled highband component from synthesized highband
1217 : *-------------------------------------------------------------------*/
1218 :
1219 1143025 : void GenSHBSynth(
1220 : const float *input_synspeech, /* i : input synthesized speech */
1221 : float *shb_syn_speech_32k, /* o : output highband component */
1222 : float Hilbert_Mem[], /* i/o: memory */
1223 : float state_lsyn_filt_shb_local[], /* i/o: memory */
1224 : const int16_t L_frame, /* i : ACELP frame length */
1225 : int16_t *syn_dm_phase )
1226 : {
1227 : float speech_buf_32k[L_FRAME32k];
1228 : int16_t i;
1229 :
1230 1143025 : Interpolate_allpass_steep( input_synspeech, state_lsyn_filt_shb_local, L_FRAME16k, speech_buf_32k );
1231 :
1232 1143025 : if ( L_frame == L_FRAME )
1233 : {
1234 599746 : flip_and_downmix_generic( speech_buf_32k, shb_syn_speech_32k, L_FRAME32k, Hilbert_Mem, Hilbert_Mem + HILBERT_ORDER1, Hilbert_Mem + ( HILBERT_ORDER1 + 2 * HILBERT_ORDER2 ), syn_dm_phase );
1235 : }
1236 : else
1237 : {
1238 348241839 : for ( i = 0; i < L_FRAME32k; i++ )
1239 : {
1240 347698560 : shb_syn_speech_32k[i] = ( ( i % 2 ) == 0 ) ? ( -speech_buf_32k[i] ) : ( speech_buf_32k[i] );
1241 : }
1242 : }
1243 :
1244 1143025 : return;
1245 : }
1246 :
1247 : /*-------------------------------------------------------------------*
1248 : * ScaleShapedSHB()
1249 : *
1250 : *
1251 : *-------------------------------------------------------------------*/
1252 :
1253 1430965 : void ScaleShapedSHB(
1254 : const int16_t length, /* i : SHB overlap length */
1255 : float *synSHB, /* i/o: synthesized shb signal */
1256 : float *overlap, /* i/o: buffer for overlap-add */
1257 : const float *subgain, /* i : subframe gain */
1258 : const float frame_gain, /* i : frame gain */
1259 : const float *win, /* i : window */
1260 : const float *subwin /* i : subframes window */
1261 : )
1262 : {
1263 : const int16_t *skip;
1264 : int16_t i, j, k, l_shb_lahead, l_frame;
1265 : int16_t join_length, num_join;
1266 : float mod_syn[L_FRAME16k + L_SHB_LAHEAD], sum_gain;
1267 :
1268 : /* initilaization */
1269 1430965 : l_frame = L_FRAME16k;
1270 1430965 : l_shb_lahead = L_SHB_LAHEAD;
1271 1430965 : skip = skip_bands_SWB_TBE;
1272 :
1273 1430965 : if ( length == SHB_OVERLAP_LEN / 2 )
1274 : {
1275 293637 : skip = skip_bands_WB_TBE;
1276 293637 : l_frame = L_FRAME16k / 4;
1277 293637 : l_shb_lahead = L_SHB_LAHEAD / 4;
1278 : }
1279 :
1280 : /* apply gain for each subframe, and store noise output signal using overlap-add */
1281 1430965 : set_f( mod_syn, 0, l_frame + l_shb_lahead );
1282 :
1283 1430965 : if ( length == SHB_OVERLAP_LEN / 2 )
1284 : {
1285 293637 : sum_gain = 0;
1286 1761822 : for ( k = 0; k < length / 2; k++ )
1287 : {
1288 1468185 : sum_gain = subwin[2 * k + 2] * subgain[0];
1289 1468185 : mod_syn[skip[0] + k] = synSHB[skip[0] + k] * sum_gain;
1290 1468185 : mod_syn[skip[0] + k + length / 2] = synSHB[skip[0] + k + length / 2] * subgain[0];
1291 : }
1292 2349096 : for ( i = 1; i < NUM_SHB_SUBFR / 2; i++ )
1293 : {
1294 22610049 : for ( k = 0; k < length; k++ )
1295 : {
1296 20554590 : sum_gain = subwin[k + 1] * subgain[i] + subwin[length - k - 1] * subgain[i - 1];
1297 20554590 : mod_syn[skip[i] + k] = synSHB[skip[i] + k] * sum_gain;
1298 : }
1299 : }
1300 1761822 : for ( k = 0; k < length / 2; k++ )
1301 : {
1302 1468185 : sum_gain = subwin[length - 2 * k - 2] * subgain[i - 1];
1303 1468185 : mod_syn[skip[i] + k] = synSHB[skip[i] + k] * sum_gain;
1304 : }
1305 : }
1306 : else
1307 : {
1308 1137328 : num_join = NUM_SHB_SUBFR / NUM_SHB_SUBGAINS;
1309 1137328 : join_length = num_join * length;
1310 23883888 : for ( k = 0, j = 0; k < length; k++ )
1311 : {
1312 22746560 : mod_syn[j] = synSHB[j] * subwin[k + 1] * subgain[0];
1313 22746560 : j++;
1314 : }
1315 4549312 : for ( i = 0; i < NUM_SHB_SUBGAINS - 1; i++ )
1316 : {
1317 208131024 : for ( k = 0; k < join_length - length; k++ )
1318 : {
1319 204719040 : mod_syn[j] = synSHB[j] * subgain[i * num_join];
1320 204719040 : j++;
1321 : }
1322 :
1323 71651664 : for ( k = 0; k < length; k++ )
1324 : {
1325 68239680 : mod_syn[j] = synSHB[j] * ( subwin[length - k - 1] * subgain[i * num_join] + subwin[k + 1] * subgain[( i + 1 ) * num_join] );
1326 68239680 : j++;
1327 : }
1328 : }
1329 69377008 : for ( k = 0; k < join_length - length; k++ )
1330 : {
1331 68239680 : mod_syn[j] = synSHB[j] * subgain[( NUM_SHB_SUBGAINS - 1 ) * num_join];
1332 68239680 : j++;
1333 : }
1334 23883888 : for ( k = 0; k < length; k++ )
1335 : {
1336 22746560 : mod_syn[j] = synSHB[j] * subwin[length - k - 1] * subgain[( NUM_SHB_SUBGAINS - 1 ) * num_join];
1337 22746560 : j++;
1338 : }
1339 : }
1340 :
1341 25645710 : for ( i = 0; i < l_shb_lahead; i++ )
1342 : {
1343 24214745 : synSHB[i] = mod_syn[i] * win[i] * frame_gain;
1344 24214745 : synSHB[i] += overlap[i];
1345 24214745 : synSHB[i + l_shb_lahead] = mod_syn[i] * frame_gain;
1346 : }
1347 :
1348 364652140 : for ( ; i < l_frame; i++ )
1349 : {
1350 363221175 : synSHB[i] = mod_syn[i] * frame_gain;
1351 : }
1352 :
1353 25645710 : for ( ; i < l_frame + l_shb_lahead; i++ )
1354 : {
1355 24214745 : overlap[i - l_frame] = mod_syn[i] * win[l_frame + l_shb_lahead - 1 - i] * frame_gain;
1356 : }
1357 :
1358 1430965 : return;
1359 : }
1360 :
1361 : /*-------------------------------------------------------------------*
1362 : * non_linearity()
1363 : *
1364 : * Apply a non linearity to the SHB excitation
1365 : * -------------------------------------------------------------------*/
1366 :
1367 2142768 : void non_linearity(
1368 : const float input[], /* i : input signal */
1369 : float output[], /* o : output signal */
1370 : float old_bwe_exc_extended[], /* i/o: memory bugffer */
1371 : const int16_t length, /* i : input length */
1372 : float *prev_scale, /* i/o: memory */
1373 : const int16_t coder_type, /* i : Coder Type */
1374 : const float *voice_factors, /* i : Voice Factors */
1375 : const int16_t L_frame, /* i : ACELP frame length */
1376 : const int16_t element_mode /* i : element_mode to differentiate EVS and IVAS*/
1377 : )
1378 : {
1379 : int16_t i, j;
1380 :
1381 2142768 : float max_val = 0.0;
1382 : float scale, temp;
1383 : float scale_step;
1384 : float *p_out;
1385 :
1386 2142768 : int16_t en_abs = 0;
1387 2142768 : float v_fac = 0, ths;
1388 : int16_t nframes;
1389 : float sc_factor;
1390 :
1391 2142768 : if ( L_frame == L_FRAME16k )
1392 : {
1393 897243 : nframes = NB_SUBFR16k;
1394 897243 : ths = 0.87f;
1395 : }
1396 : else
1397 : {
1398 1245525 : nframes = NB_SUBFR;
1399 1245525 : ths = 0.94f;
1400 : }
1401 :
1402 11611083 : for ( i = 0; i < nframes; i++ )
1403 : {
1404 9468315 : v_fac += voice_factors[i];
1405 : }
1406 2142768 : v_fac /= nframes;
1407 :
1408 2142768 : if ( coder_type == VOICED && v_fac > ths )
1409 : {
1410 142 : en_abs = 1;
1411 : }
1412 :
1413 2142768 : p_out = output + NL_BUFF_OFFSET; /* NL_BUFF_OFFSET = 12 */
1414 : /* update buffer memory */
1415 2142768 : mvr2r( old_bwe_exc_extended, output, NL_BUFF_OFFSET );
1416 :
1417 687828528 : for ( i = j = 0; i < length / 2; i++ )
1418 : {
1419 685685760 : if ( ( temp = (float) fabs( input[i] ) ) > max_val )
1420 : {
1421 15909367 : max_val = temp;
1422 15909367 : j = i;
1423 : }
1424 : }
1425 :
1426 2142768 : if ( max_val > 1.0f )
1427 : {
1428 2080096 : scale = 0.67f / max_val;
1429 : }
1430 : else
1431 : {
1432 62672 : scale = 0.67f;
1433 : }
1434 :
1435 2142768 : sc_factor = 1024.0f;
1436 2142768 : if ( element_mode > EVS_MONO )
1437 : {
1438 2093138 : sc_factor = (float) ( 1 << max( 13 - norm_s( j + 1 ), 0 ) ); /* Adapt the scaling factor allowed depending of max position */
1439 2093138 : sc_factor = max( sc_factor, 2.0f );
1440 : }
1441 :
1442 2142768 : if ( *prev_scale <= 0.0 || *prev_scale > sc_factor * scale )
1443 : {
1444 337363 : scale_step = 1.0;
1445 337363 : *prev_scale = scale;
1446 : }
1447 : else
1448 : {
1449 1805405 : scale_step = 1.0f;
1450 1805405 : if ( j != 0 )
1451 : {
1452 1745575 : scale_step = (float) exp( 1.0f / (float) j * (float) log( scale / *prev_scale ) );
1453 : }
1454 : }
1455 :
1456 687828528 : for ( i = 0; i < length / 2; i++ )
1457 : {
1458 685685760 : if ( input[i] >= 0.0 )
1459 : {
1460 351473794 : *p_out++ = ( input[i] * input[i] ) * *prev_scale;
1461 : }
1462 : else
1463 : {
1464 334211966 : if ( en_abs )
1465 : {
1466 20902 : *p_out++ = 1.0f * ( input[i] * input[i] ) * *prev_scale;
1467 : }
1468 : else
1469 : {
1470 334191064 : *p_out++ = -1.0f * ( input[i] * input[i] ) * *prev_scale;
1471 : }
1472 : }
1473 :
1474 685685760 : if ( i < j )
1475 : {
1476 335294992 : *prev_scale *= scale_step;
1477 : }
1478 : }
1479 :
1480 2142768 : max_val = 0.0f;
1481 687828528 : for ( i = j = length / 2; i < length; i++ )
1482 : {
1483 685685760 : if ( ( temp = (float) fabs( input[i] ) ) > max_val )
1484 : {
1485 15697009 : max_val = temp;
1486 15697009 : j = i;
1487 : }
1488 : }
1489 :
1490 2142768 : if ( max_val > 1.0f )
1491 : {
1492 2079894 : scale = 0.67f / max_val;
1493 : }
1494 : else
1495 : {
1496 62874 : scale = 0.67f;
1497 : }
1498 :
1499 2142768 : sc_factor = 1024.0f;
1500 2142768 : if ( element_mode > EVS_MONO )
1501 : {
1502 2093138 : sc_factor = (float) ( 1 << max( 12 - norm_s( j - length / 2 + 1 ), 0 ) ); /* allowed intra frame jump is smaller */
1503 2093138 : sc_factor = max( sc_factor, 2.0f );
1504 : }
1505 :
1506 2142768 : if ( *prev_scale <= 0.0 || *prev_scale > sc_factor * scale )
1507 : {
1508 5911 : scale_step = 1.0;
1509 5911 : *prev_scale = scale;
1510 : }
1511 : else
1512 : {
1513 2136857 : scale_step = 1.0f;
1514 2136857 : if ( j != length / 2 )
1515 : {
1516 2059838 : scale_step = (float) exp( 1.0f / (float) ( j - length / 2 ) * (float) log( scale / *prev_scale ) );
1517 : }
1518 : }
1519 :
1520 687828528 : for ( i = length / 2; i < length; i++ )
1521 : {
1522 685685760 : if ( input[i] >= 0.0 )
1523 : {
1524 348735887 : *p_out++ = ( input[i] * input[i] ) * *prev_scale;
1525 : }
1526 : else
1527 : {
1528 336949873 : if ( en_abs )
1529 : {
1530 21362 : *p_out++ = 1.0f * ( input[i] * input[i] ) * *prev_scale;
1531 : }
1532 : else
1533 : {
1534 336928511 : *p_out++ = -1.0f * ( input[i] * input[i] ) * *prev_scale;
1535 : }
1536 : }
1537 :
1538 685685760 : if ( i < j )
1539 : {
1540 310471656 : *prev_scale *= scale_step;
1541 : }
1542 : }
1543 :
1544 : /* update buffer memory */
1545 2142768 : mvr2r( output + L_FRAME32k, old_bwe_exc_extended, NL_BUFF_OFFSET );
1546 :
1547 2142768 : return;
1548 : }
1549 :
1550 :
1551 : /*-------------------------------------------------------------------*
1552 : * create_random_vector()
1553 : *
1554 : * creates random number vector
1555 : * -------------------------------------------------------------------*/
1556 :
1557 2891257 : void create_random_vector(
1558 : #ifdef FIX_1465_SWB_TBE_RANDOM_VECTOR_CREATION
1559 : float output[], /* o : output random vector */
1560 : const int16_t length, /* i : length of random vector */
1561 : int16_t seed[], /* i/o: start seed */
1562 : const int16_t element_mode /* i : element mode */
1563 : #else
1564 : float output[], /* o : output random vector */
1565 : const int16_t length, /* i : length of random vector */
1566 : int16_t seed[] /* i/o: start seed */
1567 : #endif
1568 : )
1569 : {
1570 : int16_t i, j, k;
1571 : float scale1, scale2;
1572 :
1573 : #ifdef FIX_1465_SWB_TBE_RANDOM_VECTOR_CREATION
1574 2891257 : if ( element_mode != EVS_MONO )
1575 : {
1576 2833335 : j = (int16_t) ( own_random( &seed[0] ) * 0.0078125f );
1577 2833335 : j = (int16_t) min( abs( j ), 255 );
1578 2833335 : k = (int16_t) ( own_random( &seed[1] ) * 0.0078125f );
1579 2833335 : k = (int16_t) min( abs( k ), 255 );
1580 : }
1581 : else
1582 : {
1583 57922 : j = (int16_t) ( own_random( &seed[0] ) * 0.0078f );
1584 57922 : j = abs( j ) & 0xff;
1585 57922 : k = (int16_t) ( own_random( &seed[1] ) * 0.0078f );
1586 57922 : k = abs( k ) & 0xff;
1587 : }
1588 : #else
1589 : j = (int16_t) ( own_random( &seed[0] ) * 0.0078f );
1590 : j = abs( j ) & 0xff;
1591 : k = (int16_t) ( own_random( &seed[1] ) * 0.0078f );
1592 : k = abs( k ) & 0xff;
1593 : #endif
1594 :
1595 2894845 : while ( k == j )
1596 : {
1597 : #ifdef FIX_1465_SWB_TBE_RANDOM_VECTOR_CREATION
1598 3588 : if ( element_mode != EVS_MONO )
1599 : {
1600 3349 : k = (int16_t) ( own_random( &seed[1] ) * 0.0078125f );
1601 3349 : k = (int16_t) min( abs( k ), 255 );
1602 : }
1603 : else
1604 : {
1605 239 : k = (int16_t) ( own_random( &seed[1] ) * 0.0078f );
1606 239 : k = abs( k ) & 0xff;
1607 : }
1608 : #else
1609 : k = (int16_t) ( own_random( &seed[1] ) * 0.0078f );
1610 : k = abs( k ) & 0xff;
1611 : #endif
1612 : }
1613 :
1614 2891257 : if ( own_random( &seed[0] ) < 0 )
1615 : {
1616 1403649 : scale1 = -563.154f; /* -200.00f * 0.35f/0.1243f; */
1617 : }
1618 : else
1619 : {
1620 1487608 : scale1 = 563.154f; /* 200.00f * 0.35f/0.1243f; */
1621 : }
1622 :
1623 2891257 : if ( own_random( &seed[1] ) < 0 )
1624 : {
1625 1682173 : scale2 = -225.261f; /* -80.00f * 0.35f/0.1243f; */
1626 : }
1627 : else
1628 : {
1629 1209084 : scale2 = 225.261f; /* 80.00f * 0.35f/0.1243f; */
1630 : }
1631 :
1632 442001417 : for ( i = 0; i < length; i++, j++, k++ )
1633 : {
1634 439110160 : j &= 0xff;
1635 439110160 : k &= 0xff;
1636 439110160 : output[i] = scale1 * gaus_dico_swb[j] + scale2 * gaus_dico_swb[k];
1637 : }
1638 :
1639 2891257 : return;
1640 : }
1641 :
1642 :
1643 : /*-------------------------------------------------------------------*
1644 : * interp_code_5over2()
1645 : *
1646 : * Used to interpolate the excitation from the core sample rate
1647 : * of 12.8 kHz to 32 kHz.
1648 : * Simple linear interpolator - No need for precision here.
1649 : *-------------------------------------------------------------------*/
1650 :
1651 4258037 : void interp_code_5over2(
1652 : const float inp_code[], /* i : input vector */
1653 : float interp_code[], /* o : output vector */
1654 : const int16_t inp_length /* i : length of input vector */
1655 : )
1656 : {
1657 : int16_t i, kk, kkp1;
1658 : const float factor_i[5] = { 0.2f, 0.6f, 1.0f, 0.6f, 0.2f };
1659 : const float factor_j[5] = { 0.8f, 0.4f, 0.0f, 0.4f, 0.8f };
1660 :
1661 4258037 : interp_code[0] = inp_code[0];
1662 4258037 : interp_code[1] = inp_code[0] * factor_i[3] + inp_code[1] * factor_j[3];
1663 4258037 : interp_code[2] = inp_code[0] * factor_i[4] + inp_code[1] * factor_j[4];
1664 :
1665 166751872 : for ( i = 3, kk = 1, kkp1 = 2; i < ( inp_length - 2 ) * HIBND_ACB_L_FAC; i += 5, kk++, kkp1++ )
1666 : {
1667 162493835 : interp_code[i] = inp_code[kk] * factor_j[0] + inp_code[kkp1] * factor_i[0];
1668 162493835 : interp_code[i + 1] = inp_code[kk] * factor_j[1] + inp_code[kkp1] * factor_i[1];
1669 162493835 : interp_code[i + 2] = inp_code[kkp1] * factor_i[2];
1670 162493835 : kk++;
1671 162493835 : kkp1++;
1672 162493835 : interp_code[i + 3] = inp_code[kk] * factor_i[3] + inp_code[kkp1] * factor_j[3];
1673 162493835 : interp_code[i + 4] = inp_code[kk] * factor_i[4] + inp_code[kkp1] * factor_j[4];
1674 : }
1675 :
1676 4258037 : interp_code[i] = inp_code[kk] * factor_j[0];
1677 4258037 : interp_code[i + 1] = inp_code[kk] * factor_j[1];
1678 :
1679 4258037 : return;
1680 : }
1681 :
1682 : /*-------------------------------------------------------------------*
1683 : * interp_code_4over2()
1684 : *
1685 : * Used to interpolate the excitation from the core sample rate
1686 : * of 16 kHz to 32 kHz.
1687 : * Simple linear interpolator - No need for precision here.
1688 : *-------------------------------------------------------------------*/
1689 :
1690 4320798 : void interp_code_4over2(
1691 : const float inp_code[], /* i : input vector */
1692 : float interp_code[], /* o : output vector */
1693 : const int16_t inp_length /* i : length of input vector */
1694 : )
1695 : {
1696 : int16_t i, j;
1697 306979712 : for ( i = j = 0; i < inp_length - 1; i++, j += 2 )
1698 : {
1699 302658914 : interp_code[j] = inp_code[i];
1700 302658914 : interp_code[j + 1] = inp_code[i] * 0.5f + inp_code[i + 1] * 0.5f;
1701 : }
1702 :
1703 4320798 : interp_code[j] = inp_code[i];
1704 4320798 : interp_code[j + 1] = inp_code[i] * 0.5f;
1705 :
1706 4320798 : return;
1707 : }
1708 :
1709 : /*-------------------------------------------------------------------*
1710 : * fb_tbe_reset_synth()
1711 : *
1712 : * Reset the extra parameters needed for synthesis of the FB TBE output
1713 : *-------------------------------------------------------------------*/
1714 :
1715 1663762 : void fb_tbe_reset_synth(
1716 : float fbbwe_hpf_mem[][4],
1717 : float *prev_fbbwe_ratio )
1718 : {
1719 1663762 : set_f( fbbwe_hpf_mem[0], 0, 4 );
1720 1663762 : set_f( fbbwe_hpf_mem[1], 0, 4 );
1721 1663762 : set_f( fbbwe_hpf_mem[2], 0, 4 );
1722 1663762 : set_f( fbbwe_hpf_mem[3], 0, 4 );
1723 1663762 : *prev_fbbwe_ratio = 1.0f;
1724 :
1725 1663762 : return;
1726 : }
1727 :
1728 : /*-------------------------------------------------------------------*
1729 : * wb_tbe_extras_reset()
1730 : *
1731 : * Reset the extra parameters only required for WB TBE encoding
1732 : *-------------------------------------------------------------------*/
1733 :
1734 970997 : void wb_tbe_extras_reset(
1735 : float mem_genSHBexc_filt_down_wb2[],
1736 : float mem_genSHBexc_filt_down_wb3[] )
1737 : {
1738 970997 : set_f( mem_genSHBexc_filt_down_wb2, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
1739 970997 : set_f( mem_genSHBexc_filt_down_wb3, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
1740 :
1741 970997 : return;
1742 : }
1743 :
1744 : /*-------------------------------------------------------------------*
1745 : * wb_tbe_extras_reset_synth()
1746 : *
1747 : * Reset the extra parameters only required for WB TBE synthesis
1748 : *-------------------------------------------------------------------*/
1749 :
1750 930352 : void wb_tbe_extras_reset_synth(
1751 : float state_lsyn_filt_shb[],
1752 : float state_lsyn_filt_dwn_shb[],
1753 : float mem_resamp_HB[] )
1754 : {
1755 930352 : set_f( state_lsyn_filt_shb, 0.0f, 2 * ALLPASSSECTIONS_STEEP );
1756 930352 : set_f( state_lsyn_filt_dwn_shb, 0.0f, 2 * ALLPASSSECTIONS_STEEP );
1757 930352 : set_f( mem_resamp_HB, 0.0f, INTERP_3_1_MEM_LEN );
1758 :
1759 930352 : return;
1760 : }
1761 :
1762 : /*-------------------------------------------------------------------*
1763 : * elliptic_bpf_48k_generic()
1764 : *
1765 : * 18th-order elliptic bandpass filter at 14.0 to 20 kHz sampled at 48 kHz
1766 : * Implemented as 3 fourth order sections cascaded.
1767 : *-------------------------------------------------------------------*/
1768 :
1769 366414 : void elliptic_bpf_48k_generic(
1770 : const float input[], /* i : input signal */
1771 : float output[], /* o : output signal */
1772 : float memory[][4], /* i/o: 4 arrays of 4 for memory */
1773 : const float full_band_bpf[][5] /* i : filter coefficients b0,b1,b2,a0,a1,a2 */
1774 : )
1775 : {
1776 : int16_t i;
1777 : float tmp[L_FRAME48k], tmp2[L_FRAME48k];
1778 :
1779 366414 : tmp[0] = memory[0][0] * full_band_bpf[0][4] + memory[0][1] * full_band_bpf[0][3] + memory[0][2] * full_band_bpf[0][2] + memory[0][3] * full_band_bpf[0][1] + input[0] * full_band_bpf[0][0] - full_band_bpf[3][1] * memory[1][3] - full_band_bpf[3][2] * memory[1][2] - full_band_bpf[3][3] * memory[1][1] - full_band_bpf[3][4] * memory[1][0];
1780 366414 : tmp[1] = memory[0][1] * full_band_bpf[0][4] + memory[0][2] * full_band_bpf[0][3] + memory[0][3] * full_band_bpf[0][2] + input[0] * full_band_bpf[0][1] + input[1] * full_band_bpf[0][0] - full_band_bpf[3][1] * tmp[0] - full_band_bpf[3][2] * memory[1][3] - full_band_bpf[3][3] * memory[1][2] - full_band_bpf[3][4] * memory[1][1];
1781 366414 : tmp[2] = memory[0][2] * full_band_bpf[0][4] + memory[0][3] * full_band_bpf[0][3] + input[0] * full_band_bpf[0][2] + input[1] * full_band_bpf[0][1] + input[2] * full_band_bpf[0][0] - full_band_bpf[3][1] * tmp[1] - full_band_bpf[3][2] * tmp[0] - full_band_bpf[3][3] * memory[1][3] - full_band_bpf[3][4] * memory[1][2];
1782 366414 : tmp[3] = memory[0][3] * full_band_bpf[0][4] + input[0] * full_band_bpf[0][3] + input[1] * full_band_bpf[0][2] + input[2] * full_band_bpf[0][1] + input[3] * full_band_bpf[0][0] - full_band_bpf[3][1] * tmp[2] - full_band_bpf[3][2] * tmp[1] - full_band_bpf[3][3] * tmp[0] - full_band_bpf[3][4] * memory[1][3];
1783 :
1784 350658198 : for ( i = 4; i < L_FRAME48k; i++ )
1785 : {
1786 350291784 : tmp[i] = input[i - 4] * full_band_bpf[0][4] + input[i - 3] * full_band_bpf[0][3] + input[i - 2] * full_band_bpf[0][2] + input[i - 1] * full_band_bpf[0][1] + input[i] * full_band_bpf[0][0] - full_band_bpf[3][1] * tmp[i - 1] - full_band_bpf[3][2] * tmp[i - 2] - full_band_bpf[3][3] * tmp[i - 3] - full_band_bpf[3][4] * tmp[i - 4];
1787 : }
1788 :
1789 366414 : memory[0][0] = input[L_FRAME48k - 4];
1790 366414 : memory[0][1] = input[L_FRAME48k - 3];
1791 366414 : memory[0][2] = input[L_FRAME48k - 2];
1792 366414 : memory[0][3] = input[L_FRAME48k - 1];
1793 :
1794 366414 : tmp2[0] = memory[1][0] * full_band_bpf[1][4] + memory[1][1] * full_band_bpf[1][3] + memory[1][2] * full_band_bpf[1][2] + memory[1][3] * full_band_bpf[1][1] + tmp[0] * full_band_bpf[1][0] - full_band_bpf[4][1] * memory[2][3] - full_band_bpf[4][2] * memory[2][2] - full_band_bpf[4][3] * memory[2][1] - full_band_bpf[4][4] * memory[2][0];
1795 366414 : tmp2[1] = memory[1][1] * full_band_bpf[1][4] + memory[1][2] * full_band_bpf[1][3] + memory[1][3] * full_band_bpf[1][2] + tmp[0] * full_band_bpf[1][1] + tmp[1] * full_band_bpf[1][0] - full_band_bpf[4][1] * tmp2[0] - full_band_bpf[4][2] * memory[2][3] - full_band_bpf[4][3] * memory[2][2] - full_band_bpf[4][4] * memory[2][1];
1796 366414 : tmp2[2] = memory[1][2] * full_band_bpf[1][4] + memory[1][3] * full_band_bpf[1][3] + tmp[0] * full_band_bpf[1][2] + tmp[1] * full_band_bpf[1][1] + tmp[2] * full_band_bpf[1][0] - full_band_bpf[4][1] * tmp2[1] - full_band_bpf[4][2] * tmp2[0] - full_band_bpf[4][3] * memory[2][3] - full_band_bpf[4][4] * memory[2][2];
1797 366414 : tmp2[3] = memory[1][3] * full_band_bpf[1][4] + tmp[0] * full_band_bpf[1][3] + tmp[1] * full_band_bpf[1][2] + tmp[2] * full_band_bpf[1][1] + tmp[3] * full_band_bpf[1][0] - full_band_bpf[4][1] * tmp2[2] - full_band_bpf[4][2] * tmp2[1] - full_band_bpf[4][3] * tmp2[0] - full_band_bpf[4][4] * memory[2][3];
1798 :
1799 350658198 : for ( i = 4; i < L_FRAME48k; i++ )
1800 : {
1801 350291784 : tmp2[i] = tmp[i - 4] * full_band_bpf[1][4] + tmp[i - 3] * full_band_bpf[1][3] + tmp[i - 2] * full_band_bpf[1][2] + tmp[i - 1] * full_band_bpf[1][1] + tmp[i] * full_band_bpf[1][0] - full_band_bpf[4][1] * tmp2[i - 1] - full_band_bpf[4][2] * tmp2[i - 2] - full_band_bpf[4][3] * tmp2[i - 3] - full_band_bpf[4][4] * tmp2[i - 4];
1802 : }
1803 :
1804 366414 : memory[1][0] = tmp[L_FRAME48k - 4];
1805 366414 : memory[1][1] = tmp[L_FRAME48k - 3];
1806 366414 : memory[1][2] = tmp[L_FRAME48k - 2];
1807 366414 : memory[1][3] = tmp[L_FRAME48k - 1];
1808 :
1809 366414 : output[0] = memory[2][0] * full_band_bpf[2][4] + memory[2][1] * full_band_bpf[2][3] + memory[2][2] * full_band_bpf[2][2] + memory[2][3] * full_band_bpf[2][1] + tmp2[0] * full_band_bpf[2][0] - full_band_bpf[5][1] * memory[3][3] - full_band_bpf[5][2] * memory[3][2] - full_band_bpf[5][3] * memory[3][1] - full_band_bpf[5][4] * memory[3][0];
1810 366414 : output[1] = memory[2][1] * full_band_bpf[2][4] + memory[2][2] * full_band_bpf[2][3] + memory[2][3] * full_band_bpf[2][2] + tmp2[0] * full_band_bpf[2][1] + tmp2[1] * full_band_bpf[2][0] - full_band_bpf[5][1] * output[0] - full_band_bpf[5][2] * memory[3][3] - full_band_bpf[5][3] * memory[3][2] - full_band_bpf[5][4] * memory[3][1];
1811 366414 : output[2] = memory[2][2] * full_band_bpf[2][4] + memory[2][3] * full_band_bpf[2][3] + tmp2[0] * full_band_bpf[2][2] + tmp2[1] * full_band_bpf[2][1] + tmp2[2] * full_band_bpf[2][0] - full_band_bpf[5][1] * output[1] - full_band_bpf[5][2] * output[0] - full_band_bpf[5][3] * memory[3][3] - full_band_bpf[5][4] * memory[3][2];
1812 366414 : output[3] = memory[2][3] * full_band_bpf[2][4] + tmp2[0] * full_band_bpf[2][3] + tmp2[1] * full_band_bpf[2][2] + tmp2[2] * full_band_bpf[2][1] + tmp2[3] * full_band_bpf[2][0] - full_band_bpf[5][1] * output[2] - full_band_bpf[5][2] * output[1] - full_band_bpf[5][3] * output[0] - full_band_bpf[5][4] * memory[3][3];
1813 :
1814 350658198 : for ( i = 4; i < L_FRAME48k; i++ )
1815 : {
1816 350291784 : output[i] = tmp2[i - 4] * full_band_bpf[2][4] + tmp2[i - 3] * full_band_bpf[2][3] + tmp2[i - 2] * full_band_bpf[2][2] + tmp2[i - 1] * full_band_bpf[2][1] + tmp2[i] * full_band_bpf[2][0] - full_band_bpf[5][1] * output[i - 1] - full_band_bpf[5][2] * output[i - 2] - full_band_bpf[5][3] * output[i - 3] - full_band_bpf[5][4] * output[i - 4];
1817 : }
1818 :
1819 366414 : memory[2][0] = tmp2[L_FRAME48k - 4];
1820 366414 : memory[2][1] = tmp2[L_FRAME48k - 3];
1821 366414 : memory[2][2] = tmp2[L_FRAME48k - 2];
1822 366414 : memory[2][3] = tmp2[L_FRAME48k - 1];
1823 :
1824 366414 : memory[3][0] = output[L_FRAME48k - 4];
1825 366414 : memory[3][1] = output[L_FRAME48k - 3];
1826 366414 : memory[3][2] = output[L_FRAME48k - 2];
1827 366414 : memory[3][3] = output[L_FRAME48k - 1];
1828 :
1829 366414 : return;
1830 : }
1831 :
1832 :
1833 : /*-------------------------------------------------------------------*
1834 : * synthesise_fb_high_band()
1835 : *
1836 : * Creates the highband output for full band - 14.0 to 20 kHz
1837 : * Using the energy shaped white excitation signal from the SWB BWE.
1838 : * The excitation signal input is sampled at 16kHz and so is upsampled
1839 : * to 48 kHz first.
1840 : * Uses a complementary split filter to code the two regions from
1841 : * 14kHz to 16kHz and 16 kHz to 20 kHz.
1842 : * One of 16 tilt filters is also applied afterwards to further
1843 : * refine the spectral shape of the fullband signal.
1844 : * The tilt is specified in dB per kHz. N.B. Only negative values are
1845 : * accomodated.
1846 : *-------------------------------------------------------------------*/
1847 :
1848 273405 : void synthesise_fb_high_band(
1849 : const float excitation_in[], /* i : full band excitation */
1850 : float output[], /* o : high band speech - 14.0 to 20 kHz */
1851 : const float fb_exc_energy, /* i : full band excitation energy */
1852 : const float ratio, /* i : energy ratio */
1853 : const int16_t L_frame, /* i : ACELP frame length */
1854 : const int16_t bfi, /* i : BFI flag */
1855 : float *prev_fbbwe_ratio, /* o : previous frame energy for FEC */
1856 : float bpf_memory[][4] /* i/o: memory for elliptic bpf 48k */
1857 : )
1858 : {
1859 : int16_t i, j;
1860 : float excitation_in_interp3[L_FRAME48k];
1861 : float tmp[L_FRAME48k];
1862 : float temp1, ratio2;
1863 :
1864 : /* Interpolate the white energy shaped gaussian excitation from 16 kHz to 48 kHz with zeros */
1865 : /* white excitation from DC to 8 kHz resampled to produce DC to 24 kHz excitation. */
1866 87763005 : for ( i = 0, j = 0; i < L_FRAME48k; i += 3, j++ )
1867 : {
1868 87489600 : excitation_in_interp3[i] = 3.0f * excitation_in[j];
1869 87489600 : excitation_in_interp3[i + 1] = 0.0f;
1870 87489600 : excitation_in_interp3[i + 2] = 0.0f;
1871 : }
1872 :
1873 273405 : if ( L_frame == L_FRAME16k )
1874 : {
1875 : /* for 16kHz ACELP core */
1876 229374 : elliptic_bpf_48k_generic( excitation_in_interp3, tmp, bpf_memory, full_band_bpf_3 );
1877 : }
1878 : else
1879 : {
1880 : /* for 12.8kHz ACELP core */
1881 44031 : elliptic_bpf_48k_generic( excitation_in_interp3, tmp, bpf_memory, full_band_bpf_1 );
1882 : }
1883 273405 : temp1 = sum2_f( tmp, L_FRAME48k ) + 0.001f;
1884 273405 : ratio2 = (float) ( ratio * sqrt( fb_exc_energy / temp1 ) );
1885 :
1886 273405 : if ( !bfi )
1887 : {
1888 266277 : *prev_fbbwe_ratio = ratio;
1889 : }
1890 : else
1891 : {
1892 7128 : *prev_fbbwe_ratio = ratio * 0.5f;
1893 : }
1894 262742205 : for ( i = 0; i < L_FRAME48k; i++ )
1895 : {
1896 262468800 : output[i] = tmp[i] * ratio2;
1897 : }
1898 :
1899 273405 : return;
1900 : }
1901 :
1902 : /*-------------------------------------------------------------------*
1903 : * Estimate_mix_factors() *
1904 : * *
1905 : * Estimate mix factors for SHB excitation generation *
1906 : *-------------------------------------------------------------------*/
1907 :
1908 66984 : static void Estimate_mix_factors(
1909 : const float *shb_res, /* i : SHB LP residual */
1910 : const float *exc16kWhtnd, /* i : SHB transformed low band excitation */
1911 : const float *White_exc16k, /* i : Modulated envelope shaped white noise */
1912 : const float pow1, /* i : SHB exc. power for normalization */
1913 : const float pow22, /* i : White noise excitation for normalization*/
1914 : float *vf_modified, /* o : Estimated voice factors */
1915 : int16_t *vf_ind /* o : voice factors VQ index */
1916 : )
1917 : {
1918 : float shb_res_local[L_FRAME16k], WN_exc_local[L_FRAME16k];
1919 : float pow3, temp_p1_p2, temp_p1_p3;
1920 : float temp_numer1[L_FRAME16k], temp_numer2[L_FRAME16k];
1921 : int16_t i, length;
1922 :
1923 66984 : mvr2r( shb_res, shb_res_local, L_FRAME16k );
1924 66984 : mvr2r( White_exc16k, WN_exc_local, L_FRAME16k );
1925 :
1926 66984 : pow3 = dotp( shb_res_local, shb_res_local, L_FRAME16k );
1927 :
1928 66984 : pow3 += 0.00001f;
1929 66984 : temp_p1_p2 = (float) sqrt( pow1 / pow22 );
1930 66984 : temp_p1_p3 = (float) sqrt( pow1 / pow3 );
1931 :
1932 :
1933 21501864 : for ( i = 0; i < L_FRAME16k; i++ )
1934 : {
1935 21434880 : WN_exc_local[i] *= temp_p1_p2;
1936 21434880 : shb_res_local[i] *= temp_p1_p3;
1937 : }
1938 21501864 : for ( i = 0; i < L_FRAME16k; i++ )
1939 : {
1940 21434880 : temp_numer1[i] = shb_res_local[i] - WN_exc_local[i];
1941 21434880 : temp_numer2[i] = exc16kWhtnd[i] - WN_exc_local[i];
1942 : }
1943 :
1944 66984 : length = L_FRAME16k;
1945 133968 : for ( i = 0; i < 1; i++ )
1946 : {
1947 66984 : temp_p1_p2 = dotp( temp_numer1 + i * length, temp_numer2 + i * length, length );
1948 66984 : temp_p1_p3 = dotp( temp_numer2 + i * length, temp_numer2 + i * length, length );
1949 66984 : vf_modified[i] = min( max( ( temp_p1_p2 / temp_p1_p3 ), 0.1f ), 0.99f );
1950 : }
1951 :
1952 66984 : *vf_ind = usquant( vf_modified[0], &temp_p1_p2, 0.125, 0.125, 1 << NUM_BITS_SHB_VF );
1953 66984 : set_f( vf_modified, temp_p1_p2, NB_SUBFR16k );
1954 :
1955 66984 : return;
1956 : }
1957 :
1958 : /*-------------------------------------------------------------------*
1959 : * tbe_celp_exc() *
1960 : * *
1961 : * Prepare adaptive part of TBE excitation *
1962 : *-------------------------------------------------------------------*/
1963 :
1964 6894343 : void tbe_celp_exc(
1965 : const int16_t element_mode, /* i : element mode */
1966 : const int16_t idchan, /* i : channel ID */
1967 : float *bwe_exc, /* i/o: BWE excitation */
1968 : const int16_t L_frame, /* i : frame length */
1969 : const int16_t L_subfr, /* i : subframe length */
1970 : const int16_t i_subfr, /* i : subframe index */
1971 : const int16_t T0, /* i : integer pitch lag */
1972 : const int16_t T0_frac, /* i : fraction of lag */
1973 : float *error, /* i/o: error */
1974 : const int16_t tdm_LRTD_flag /* i : LRTD stereo mode flag */
1975 : )
1976 : {
1977 : int16_t i, offset;
1978 :
1979 6894343 : if ( element_mode == IVAS_CPE_TD && idchan == 1 && !tdm_LRTD_flag )
1980 : {
1981 3380 : return;
1982 : }
1983 :
1984 6890963 : assert( bwe_exc != NULL && "BWE excitation is NULL" );
1985 :
1986 6890963 : if ( L_frame == L_FRAME )
1987 : {
1988 3290668 : offset = tbe_celp_exc_offset( T0, T0_frac );
1989 :
1990 529797548 : for ( i = 0; i < L_subfr * HIBND_ACB_L_FAC; i++ )
1991 : {
1992 526506880 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset + (int16_t) *error];
1993 : }
1994 3290668 : *error += (float) offset - (float) T0 * HIBND_ACB_L_FAC - 0.25f * HIBND_ACB_L_FAC * (float) T0_frac;
1995 : }
1996 : else
1997 : {
1998 3600295 : offset = T0 * 2 + (int16_t) ( (float) T0_frac * 0.5f + 4 + 0.5f ) - 4;
1999 464438055 : for ( i = 0; i < L_subfr * 2; i++ )
2000 : {
2001 460837760 : bwe_exc[i + i_subfr * 2] = bwe_exc[i + i_subfr * 2 - offset + (int16_t) *error];
2002 : }
2003 3600295 : *error += (float) offset - (float) T0 * 2 - 0.5f * (float) T0_frac;
2004 : }
2005 :
2006 6890963 : return;
2007 : }
2008 :
2009 : /*-------------------------------------------------------------------*
2010 : * prep_tbe_exc() *
2011 : * *
2012 : * Prepare TBE excitation *
2013 : *-------------------------------------------------------------------*/
2014 :
2015 7837179 : void prep_tbe_exc(
2016 : const int16_t L_frame, /* i : length of the frame */
2017 : const int16_t L_subfr, /* i : subframe length */
2018 : const int16_t i_subfr, /* i : subframe index */
2019 : const float gain_pit, /* i : Pitch gain */
2020 : const float gain_code, /* i : algebraic codebook gain */
2021 : const float code[], /* i : algebraic excitation */
2022 : const float voice_fac, /* i : voicing factor */
2023 : float *voice_factors, /* o : TBE voicing factor */
2024 : float bwe_exc[], /* i/o: excitation for TBE */
2025 : const float gain_preQ, /* i : prequantizer excitation gain*/
2026 : const float code_preQ[], /* i : prequantizer excitation */
2027 : const int16_t T0, /* i : integer pitch variables */
2028 : const int16_t coder_type, /* i : coding type */
2029 : const int32_t core_brate, /* i : core bitrate */
2030 : const int16_t element_mode, /* i : element mode */
2031 : const int16_t idchan, /* i : channel ID */
2032 : const int16_t flag_TD_BWE, /* i : flag indicating whether hTD_BWE exists */
2033 : const int16_t tdm_LRTD_flag /* i : LRTD stereo mode flag */
2034 : )
2035 : {
2036 : int16_t i;
2037 : float tmp_code[2 * L_SUBFR * HIBND_ACB_L_FAC];
2038 : float tmp_code_preInt[L_SUBFR];
2039 7837179 : float tmp = 1.0f;
2040 :
2041 7837179 : *voice_factors = VF_0th_PARAM + VF_1st_PARAM * voice_fac + VF_2nd_PARAM * voice_fac * voice_fac;
2042 :
2043 7837179 : if ( ( coder_type == VOICED || T0 > 115.5f ) && core_brate > ACELP_8k00 )
2044 : {
2045 2041556 : tmp = 1.0f;
2046 2041556 : *voice_factors *= tmp;
2047 : }
2048 :
2049 7837179 : *voice_factors = min( max( 0.000001f, *voice_factors ), 0.999999f );
2050 :
2051 7837179 : if ( element_mode == IVAS_CPE_TD && idchan == 1 && !tdm_LRTD_flag )
2052 : {
2053 3380 : if ( flag_TD_BWE && i_subfr == 0 )
2054 : {
2055 0 : set_f( bwe_exc, 0, L_FRAME32k );
2056 : }
2057 :
2058 3380 : return;
2059 : }
2060 :
2061 7833799 : if ( L_frame == L_FRAME )
2062 : {
2063 3731784 : interp_code_5over2( code, tmp_code, L_subfr );
2064 :
2065 600817224 : for ( i = 0; i < L_subfr * HIBND_ACB_L_FAC; i++ )
2066 : {
2067 597085440 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = gain_pit * bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] +
2068 597085440 : gain_code * tmp_code[i];
2069 : }
2070 : }
2071 : else
2072 : {
2073 266630975 : for ( i = 0; i < L_subfr; i++ )
2074 : {
2075 262528960 : tmp_code_preInt[i] = gain_code * code[i] + 2 * gain_preQ * code_preQ[i];
2076 : }
2077 :
2078 4102015 : interp_code_4over2( tmp_code_preInt, tmp_code, L_subfr );
2079 :
2080 529159935 : for ( i = 0; i < L_subfr * 2; i++ )
2081 : {
2082 525057920 : bwe_exc[i + i_subfr * 2] = gain_pit * bwe_exc[i + i_subfr * 2] + tmp_code[i];
2083 : }
2084 : }
2085 :
2086 7833799 : return;
2087 : }
2088 :
2089 :
2090 : /*-------------------------------------------------------------------*
2091 : * get_tbe_bits() *
2092 : * *
2093 : * Determine TBE bit consumption per frame from bitrate *
2094 : *-------------------------------------------------------------------*/
2095 :
2096 35859 : int16_t get_tbe_bits(
2097 : const int32_t total_brate,
2098 : const int16_t bwidth,
2099 : const int16_t rf_mode )
2100 : {
2101 35859 : int16_t i, bits = 0;
2102 :
2103 35859 : if ( rf_mode )
2104 : {
2105 : /* TBE bits for core, primary frame */
2106 8056 : if ( bwidth == WB && total_brate == ACELP_13k20 )
2107 : {
2108 : /* Gain frame: 4, Gain shapes: 0, and LSFs: 2 */
2109 4216 : bits = NUM_BITS_SHB_FrameGain_LBR_WB + NUM_BITS_LBR_WB_LSF;
2110 : }
2111 3840 : else if ( bwidth == SWB && total_brate == ACELP_13k20 )
2112 : {
2113 : /* Gain frame: 5, Gain shapes: 5, and lowrate LSFs: 8 */
2114 3840 : bits = NUM_BITS_SHB_FRAMEGAIN + NUM_BITS_SHB_SUBGAINS + 8;
2115 : }
2116 : }
2117 : else
2118 : {
2119 27803 : if ( bwidth == WB && total_brate == ACELP_9k60 )
2120 : {
2121 3444 : bits = NUM_BITS_LBR_WB_LSF + NUM_BITS_SHB_FrameGain_LBR_WB;
2122 : }
2123 24359 : else if ( bwidth == SWB || bwidth == FB )
2124 : {
2125 21472 : if ( total_brate == ACELP_9k60 )
2126 : {
2127 2496 : bits = NUM_BITS_SHB_FRAMEGAIN + NUM_BITS_SHB_SUBGAINS + 8;
2128 : }
2129 18976 : else if ( total_brate >= ACELP_13k20 && total_brate <= ACELP_32k )
2130 : {
2131 18976 : bits = NUM_BITS_SHB_SUBGAINS + NUM_BITS_SHB_FRAMEGAIN + NUM_LSF_GRID_BITS + MIRROR_POINT_BITS;
2132 :
2133 113856 : for ( i = 0; i < NUM_Q_LSF; i++ )
2134 : {
2135 94880 : bits += lsf_q_num_bits[i];
2136 : }
2137 : }
2138 :
2139 21472 : if ( total_brate >= ACELP_24k40 )
2140 : {
2141 14342 : bits += NUM_BITS_SHB_ENER_SF + NUM_BITS_SHB_VF + NUM_BITS_SHB_RES_GS * NB_SUBFR16k;
2142 : }
2143 :
2144 21472 : if ( bwidth == SWB && ( total_brate == ACELP_16k40 || total_brate == ACELP_24k40 ) )
2145 : {
2146 14492 : bits += BITS_TEC + BITS_TFA;
2147 : }
2148 :
2149 21472 : if ( bwidth == FB )
2150 : {
2151 : /* fullband slope */
2152 4484 : bits += 4;
2153 : }
2154 : }
2155 : }
2156 :
2157 35859 : return bits;
2158 : }
|