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