Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include <math.h>
43 : #include "cnst.h"
44 : #include "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[] );
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 8104808 : 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 8104808 : set_f( mem_csfilt, 0, 2 );
80 8104808 : set_f( mem_genSHBexc_filt_down_shb, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
81 8104808 : set_f( state_lpc_syn, 0.0f, LPC_SHB_ORDER );
82 8104808 : set_f( syn_overlap, 0.0f, L_SHB_LAHEAD );
83 8104808 : set_f( state_syn_shbexc, 0.0f, L_SHB_LAHEAD );
84 8104808 : *tbe_demph = 0.0f;
85 8104808 : *tbe_premph = 0.0f;
86 8104808 : set_f( mem_stp_swb, 0, LPC_SHB_ORDER );
87 8104808 : *gain_prec_swb = 1.0f;
88 :
89 8104808 : 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 5573629 : void swb_tbe_reset_synth(
100 : float genSHBsynth_Hilbert_Mem[],
101 : float genSHBsynth_state_lsyn_filt_shb_local[] )
102 : {
103 5573629 : set_f( genSHBsynth_Hilbert_Mem, 0.0f, HILBERT_MEM_SIZE );
104 5573629 : set_f( genSHBsynth_state_lsyn_filt_shb_local, 0.0f, 2 * ALLPASSSECTIONS_STEEP );
105 :
106 5573629 : 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 6496635 : 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 6496635 : 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 6496635 : 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 975974 : 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 975974 : period = 17; /* == (int16_t) (32000.0f / 1850.0f + 0.5f); */
159 :
160 975974 : recip_period = 256.0f / (float) period;
161 17567532 : for ( i = 0; i < period; i++ )
162 : {
163 16591558 : k = (int16_t) ( i * recip_period + 0.5f );
164 16591558 : if ( k <= 64 )
165 : {
166 4879870 : local_negsin_table[i] = -sincos_t[k];
167 4879870 : local_cos_table[i] = sincos_t[64 - k];
168 : }
169 11711688 : else if ( k <= 128 )
170 : {
171 3903896 : local_negsin_table[i] = -sincos_t[128 - k];
172 3903896 : local_cos_table[i] = -sincos_t[k - 64];
173 : }
174 7807792 : else if ( k <= 192 )
175 : {
176 3903896 : local_negsin_table[i] = sincos_t[k - 128];
177 3903896 : local_cos_table[i] = -sincos_t[192 - k];
178 : }
179 : else
180 : {
181 3903896 : local_negsin_table[i] = sincos_t[256 - k];
182 3903896 : local_cos_table[i] = sincos_t[k - 192];
183 : }
184 : }
185 :
186 300214854 : for ( i = 0; i < length; i = i + 2 )
187 : {
188 299238880 : input[i] = -input[i];
189 : }
190 :
191 975974 : mvr2r( input, tmp + HILBERT_ORDER1, length );
192 :
193 975974 : mvr2r( mem1_ext, tmp, HILBERT_ORDER1 );
194 :
195 : /* Hilber transform stage - 0 */
196 975974 : Hilbert_transform( tmp, tmp, tmpi_R, tmpi_I, length, 0 );
197 :
198 975974 : mvr2r( mem2_ext, tmpi2_R, HILBERT_ORDER2 );
199 975974 : mvr2r( mem3_ext, tmpi2_I, HILBERT_ORDER2 );
200 :
201 : /* Hilber transform stage - 1 */
202 975974 : Hilbert_transform( tmpi_R, tmpi_I, tmpi2_R, tmpi2_I, length, 1 );
203 :
204 975974 : mvr2r( tmp + length, mem1_ext, HILBERT_ORDER1 );
205 975974 : mvr2r( mem2_ext + HILBERT_ORDER2, tmp_R, HILBERT_ORDER2 );
206 975974 : mvr2r( mem3_ext + HILBERT_ORDER2, tmp_I, HILBERT_ORDER2 );
207 :
208 : /* Hilber transform stage - 2 */
209 975974 : Hilbert_transform( tmpi2_R, tmpi2_I, tmpi_R, tmpi_I, length, 2 );
210 :
211 975974 : mvr2r( tmpi2_R + length, mem2_ext, HILBERT_ORDER2 );
212 975974 : mvr2r( tmpi2_I + length, mem3_ext, HILBERT_ORDER2 );
213 :
214 : /* Hilber transform stage - 3 */
215 975974 : Hilbert_transform( tmpi_R, tmpi_I, tmp_R, tmp_I, length, 3 );
216 :
217 975974 : mvr2r( tmp_R + length, mem2_ext + HILBERT_ORDER2, HILBERT_ORDER2 );
218 975974 : mvr2r( tmp_I + length, mem3_ext + HILBERT_ORDER2, HILBERT_ORDER2 );
219 :
220 975974 : if ( *phase_state >= period )
221 : {
222 0 : *phase_state = 0;
223 : }
224 :
225 37080989 : for ( i = 0, j = *phase_state; i < length; )
226 : {
227 634582775 : for ( ; ( j < period ) && ( i < length ); j++, i++ )
228 : {
229 598477760 : output[i] = tmp_R[i + HILBERT_ORDER2] * local_cos_table[j] + tmp_I[i + HILBERT_ORDER2] * local_negsin_table[j];
230 : }
231 :
232 36105015 : if ( j >= period )
233 : {
234 35183497 : j = 0;
235 : }
236 : }
237 :
238 975974 : *phase_state = j;
239 :
240 975974 : return;
241 : }
242 :
243 : /*----------------------------------------------
244 : * Hilbert_transform()
245 : *
246 : * Hilbert transform
247 : *------------------------------------------------*/
248 :
249 3903896 : 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 3903896 : hb_filter_stage = 2 * HB_stage_id;
261 3903896 : offset = ( HB_stage_id == 0 ) ? 1 : 0;
262 :
263 3903896 : if ( HB_stage_id == 0 || HB_stage_id == 2 )
264 : {
265 1198907468 : for ( i = 0; i < length; i++ )
266 : {
267 1196955520 : 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 1196955520 : 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 1951948 : else if ( HB_stage_id == 1 || HB_stage_id == 3 )
273 : {
274 1198907468 : for ( i = 0; i < length; i++ )
275 : {
276 1196955520 : 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 1196955520 : 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 3903896 : return;
283 : }
284 :
285 : /*-------------------------------------------------------------------*
286 : * flip_spectrum()
287 : *
288 : *
289 : *-------------------------------------------------------------------*/
290 :
291 1279670 : 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 206026870 : for ( i = 0; i < length; i = i + 2 )
300 : {
301 204747200 : output[i] = -input[i];
302 204747200 : output[i + 1] = input[i + 1];
303 : }
304 :
305 1279670 : return;
306 : }
307 :
308 : /*-------------------------------------------------------------------*
309 : * flip_spectrum_and_decimby4()
310 : *
311 : *
312 : *-------------------------------------------------------------------*/
313 :
314 1208268 : 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 1208268 : if ( ramp_flag )
328 : {
329 8331 : factor = 4.0f / length;
330 341571 : for ( i = 0; i < length / 4; i = i + 2 )
331 : {
332 333240 : input_change[i] = -input[i] * ( i * factor );
333 333240 : input_change[i + 1] = input[i + 1] * ( ( i + 1.0f ) * factor );
334 : }
335 : }
336 : else
337 : {
338 1199937 : i = 0;
339 : }
340 :
341 140111258 : for ( ; i < length; i = i + 2 )
342 : {
343 138902990 : input_change[i] = -input[i];
344 138902990 : input_change[i + 1] = input[i + 1];
345 : }
346 :
347 1208268 : Decimate_allpass_steep( input_change, mem1, length, tmp );
348 1208268 : Decimate_allpass_steep( tmp, mem2, length / 2, output );
349 :
350 1208268 : return;
351 : }
352 :
353 : /*-------------------------------------------------------------------*
354 : * GenShapedWBExcitation()
355 : *
356 : * Synthesize spectrally shaped highband excitation signal for the wideband
357 : *-------------------------------------------------------------------*/
358 :
359 405536 : 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 float *bwe_exc_extended, /* i : bandwidth extended exciatation */
370 : int16_t bwe_seed[], /* i/o: random number generator seed */
371 : const float voice_factors[], /* i : voicing factor */
372 : const int16_t uv_flag, /* i : unvoiced flag */
373 : const int16_t igf_flag )
374 : {
375 : int16_t i, j, k;
376 : float wht_fil_mem[LPC_WHTN_ORDER_WB];
377 : float lpc_whtn[LPC_WHTN_ORDER_WB + 1];
378 : float R[LPC_WHTN_ORDER_WB + 2];
379 : float excTmp[L_FRAME16k];
380 : float excTmp2[L_FRAME16k / 4];
381 : float exc4k[L_FRAME16k / 4];
382 : float pow1, pow22, scale;
383 : float excNoisyEnv[L_FRAME16k / 4];
384 405536 : float csfilt_num2[1] = { 0.05f };
385 405536 : float csfilt_den2[2] = { 1.0f, -0.96f };
386 : float temp1, temp2;
387 : float ervec[LPC_WHTN_ORDER_WB + 2];
388 : float tmp_vfac;
389 405536 : float avg_voice_fac = 0.25f * sum_f( voice_factors, NB_SUBFR );
390 :
391 405536 : if ( igf_flag && ( coder_type == VOICED || avg_voice_fac > 0.35f ) )
392 : {
393 2314 : csfilt_num2[0] = 0.2f;
394 2314 : csfilt_den2[1] = -0.8f;
395 : }
396 403222 : else if ( igf_flag && ( coder_type == UNVOICED || avg_voice_fac < 0.2f ) )
397 : {
398 2229 : csfilt_num2[0] = 0.01f;
399 2229 : csfilt_den2[1] = -0.99f;
400 : }
401 405536 : set_f( wht_fil_mem, 0, LPC_WHTN_ORDER_WB );
402 :
403 405536 : Decimate_allpass_steep( bwe_exc_extended, mem_genSHBexc_filt_down1, L_FRAME32k, excTmp );
404 :
405 405536 : flip_spectrum_and_decimby4( excTmp, exc4k, L_FRAME16k, mem_genSHBexc_filt_down2, mem_genSHBexc_filt_down3, 0 );
406 :
407 405536 : if ( uv_flag )
408 : {
409 : /* unvoiced signal */
410 15448 : create_random_vector( exc4kWhtnd, L_FRAME16k / 4, bwe_seed );
411 : }
412 : else
413 : {
414 390088 : autocorr( exc4k, R, LPC_WHTN_ORDER_WB + 1, L_FRAME16k / 4, win_flatten_4k, 0, 1, 1 );
415 :
416 : /* Ensure R[0] isn't zero when entering Levinson Durbin */
417 390088 : R[0] = max( R[0], 1.0e-8f );
418 1560352 : for ( i = 0; i <= LPC_WHTN_ORDER_WB; i++ )
419 : {
420 1170264 : R[i] = R[i] * wac[i];
421 : }
422 390088 : lev_dur( lpc_whtn, R, LPC_WHTN_ORDER_WB, ervec );
423 :
424 390088 : fir( exc4k, lpc_whtn, exc4kWhtnd, wht_fil_mem, L_FRAME16k / 4, LPC_WHTN_ORDER_WB, 0 );
425 :
426 : /* Ensure pow1 is greater than zero when computing normalization */
427 31597128 : for ( i = 0, pow1 = 0.00001f; i < L_FRAME16k / 4; i++ )
428 : {
429 31207040 : excTmp2[i] = (float) ( fabs( exc4kWhtnd[i] ) );
430 31207040 : pow1 += exc4kWhtnd[i] * exc4kWhtnd[i];
431 : }
432 :
433 31597128 : for ( i = 0; i < L_FRAME16k / 4; i++ )
434 : {
435 31207040 : excNoisyEnv[i] = *mem_csfilt + csfilt_num2[0] * excTmp2[i];
436 31207040 : *mem_csfilt = -csfilt_den2[1] * excNoisyEnv[i];
437 : }
438 :
439 390088 : create_random_vector( exc4k, L_FRAME16k / 4, bwe_seed );
440 :
441 : /* Ensure pow22 is greater than zero when computing normalization */
442 31597128 : for ( i = 0, pow22 = 0.00001f; i < L_FRAME16k / 4; i++ )
443 : {
444 31207040 : exc4k[i] *= excNoisyEnv[i];
445 31207040 : pow22 += exc4k[i] * exc4k[i];
446 : }
447 :
448 390088 : if ( coder_type == UNVOICED || ( igf_flag && avg_voice_fac < 0.2f ) )
449 : {
450 9066 : scale = (float) sqrt( pow1 / pow22 );
451 9066 : if ( pow22 == 0.f )
452 : {
453 0 : scale = 0;
454 : }
455 :
456 734346 : for ( i = 0; i < L_FRAME16k / 4; i++ )
457 : {
458 725280 : exc4kWhtnd[i] = exc4k[i] * scale;
459 : }
460 : }
461 : else
462 : {
463 1905110 : for ( i = 0, k = 0; i < 4; i++ )
464 : {
465 :
466 1524088 : if ( igf_flag && coder_type == VOICED )
467 : {
468 8032 : tmp_vfac = 2 * voice_factors[i];
469 8032 : tmp_vfac = min( 1, tmp_vfac );
470 : }
471 : else
472 : {
473 1516056 : tmp_vfac = voice_factors[i];
474 : }
475 :
476 1524088 : temp1 = root_a( tmp_vfac );
477 1524088 : temp2 = root_a_over_b( pow1 * ( 1.0f - tmp_vfac ), pow22 );
478 :
479 :
480 32005848 : for ( j = 0; j < L_FRAME16k / 16; j++, k++ )
481 : {
482 30481760 : exc4kWhtnd[k] = temp1 * exc4kWhtnd[k] + temp2 * exc4k[k];
483 : }
484 : }
485 : }
486 : }
487 :
488 405536 : syn_filt( lpc_shb, LPC_SHB_ORDER_WB, exc4kWhtnd, excSHB, L_FRAME16k / 4, state_lpc_syn, 1 );
489 :
490 405536 : return;
491 : }
492 :
493 : /*-------------------------------------------------------------------*
494 : * GenWBSynth()
495 : *
496 : * Generate 16 KHz sampled highband component from synthesized highband
497 : *-------------------------------------------------------------------*/
498 :
499 330458 : void GenWBSynth(
500 : const float *input_synspeech, /* i : input synthesized speech */
501 : float *shb_syn_speech_16k, /* o : output highband compnent */
502 : float *state_lsyn_filt_shb1, /* i/o: memory */
503 : float *state_lsyn_filt_shb2 /* i/o: memory */
504 : )
505 : {
506 : float speech_buf_16k1[L_FRAME16k], speech_buf_16k2[L_FRAME16k];
507 :
508 330458 : Interpolate_allpass_steep( input_synspeech, state_lsyn_filt_shb1, L_FRAME16k / 4, speech_buf_16k1 );
509 :
510 330458 : Interpolate_allpass_steep( speech_buf_16k1, state_lsyn_filt_shb2, L_FRAME16k / 2, speech_buf_16k2 );
511 :
512 330458 : flip_spectrum( speech_buf_16k2, shb_syn_speech_16k, L_FRAME16k );
513 :
514 330458 : return;
515 : }
516 :
517 : /*-------------------------------------------------------------------*
518 : * PostShortTerm()
519 : *
520 : * Short term processing
521 : *-------------------------------------------------------------------*/
522 :
523 11969588 : void PostShortTerm(
524 : float *sig_in, /* i : input signal (pointer to current subframe */
525 : float *lpccoeff, /* i : LPC coefficients for current subframe */
526 : float *sig_out, /* o : postfiltered output */
527 : float *mem_stp, /* i/o: postfilter memory*/
528 : float *ptr_mem_stp, /* i/o: pointer to postfilter memory*/
529 : float *ptr_gain_prec, /* i/o: for gain adjustment*/
530 : float *mem_zero, /* i/o: null memory to compute h_st*/
531 : const float formant_fac /* i : Strength of post-filter [0,1] */
532 : )
533 : {
534 : float apond1[LPC_SHB_ORDER + 1]; /* denominator coeff.*/
535 : float apond2[LONG_H_ST]; /* numerator coeff. */
536 : float sig_ltp[L_SUBFR16k + 1]; /* residual signal */
537 : float parcor0;
538 : float g1, g2;
539 :
540 11969588 : set_f( apond1, 0, LPC_SHB_ORDER + 1 );
541 11969588 : set_f( apond2, 0, LONG_H_ST );
542 11969588 : set_f( sig_ltp, 0, L_SUBFR16k + 1 );
543 :
544 : /* Obtain post-filter weights */
545 11969588 : g1 = GAMMA0 + GAMMA_SHARP * formant_fac;
546 11969588 : g2 = GAMMA0 - GAMMA_SHARP * formant_fac;
547 :
548 : /* Compute weighted LPC coefficients */
549 11969588 : weight_a( lpccoeff, apond1, g1, LPC_SHB_ORDER );
550 11969588 : weight_a( lpccoeff, apond2, g2, LPC_SHB_ORDER );
551 :
552 : /* Compute A(gamma2) residual */
553 11969588 : residu( apond2, LPC_SHB_ORDER, sig_in, sig_ltp + 1, L_SUBFR16k );
554 :
555 : /* Save last output of 1/A(gamma1) */
556 11969588 : sig_ltp[0] = *ptr_mem_stp;
557 :
558 : /* Control short term pst filter gain and compute parcor0 */
559 11969588 : calc_st_filt( apond2, apond1, &parcor0, sig_ltp + 1, mem_zero, L_SUBFR16k, SWB_TBE );
560 :
561 : /* 1/A(gamma1) filtering, mem_stp is updated */
562 11969588 : syn_filt( apond1, LPC_SHB_ORDER, sig_ltp + 1, sig_ltp + 1, L_SUBFR16k, mem_stp, 1 );
563 :
564 : /* (1 + mu z-1) tilt filtering */
565 11969588 : filt_mu( sig_ltp, sig_out, parcor0, L_SUBFR16k, SWB_TBE );
566 :
567 : /* gain control */
568 11969588 : scale_st( sig_in, sig_out, ptr_gain_prec, L_SUBFR16k, SWB_TBE );
569 :
570 11969588 : return;
571 : }
572 :
573 : /*-------------------------------------------------------------------*
574 : * swb_formant_fac()
575 : *
576 : * Find strength of adaptive formant postfilter using tilt of the high
577 : * band. The 2nd lpc coefficient is used as a tilt approximation.
578 : *-------------------------------------------------------------------*/
579 :
580 : /*! r: Formant filter strength [0,1] */
581 3038855 : float swb_formant_fac(
582 : const float lpc_shb2, /* i : 2nd HB LPC coefficient */
583 : float *tilt_mem /* i/o: Tilt smoothing memory */
584 : )
585 : {
586 : float formant_fac;
587 : float tmp;
588 :
589 : /* Smoothen tilt value */
590 3038855 : tmp = 0.5f * (float) fabs( lpc_shb2 ) + 0.5f * *tilt_mem;
591 3038855 : *tilt_mem = tmp;
592 :
593 : /* Map to PF strength */
594 3038855 : formant_fac = ( tmp - SWB_TILT_LOW ) * SWB_TILT_DELTA;
595 3038855 : if ( formant_fac > 1.0f )
596 : {
597 3480 : formant_fac = 1.0f;
598 : }
599 3035375 : else if ( formant_fac < 0.0f )
600 : {
601 2769095 : formant_fac = 0.0f;
602 : }
603 :
604 3038855 : formant_fac = 1.0f - 0.5f * formant_fac;
605 :
606 3038855 : return formant_fac;
607 : }
608 :
609 53844 : void find_td_envelope(
610 : const float inp[], /* i : input signal */
611 : const int16_t len, /* i : length of the input signal */
612 : const int16_t len_h, /* i : length of the MA filter */
613 : float mem_h[], /* i/o: memory of the MA filter, length len_h/2 */
614 : float out[] /* o : td envelope of the input signal */
615 : )
616 : {
617 : int16_t k, K;
618 : float buf_in[L_FRAME16k + MAX_LEN_MA_FILTER], *p_in, *p_out, *p_prev, w;
619 :
620 53844 : assert( len > 0 && len <= L_FRAME16k );
621 :
622 53844 : K = (int16_t) ( len_h / 2 ); /* length of FIR filter memory = half of the total filter length */
623 53844 : w = 1.0f / len_h; /* MA filtering coefficient */
624 :
625 : /* copy filter memory to the input buffer */
626 53844 : if ( mem_h != NULL )
627 : {
628 17948 : mvr2r( mem_h, buf_in, K );
629 : }
630 : else
631 : {
632 : /* no memory available, use the first len_h/2 samples as memory */
633 35896 : p_in = buf_in;
634 394856 : for ( k = 0; k < K; k++ )
635 : {
636 358960 : *p_in++ = (float) fabs( inp[k] ) * w;
637 : }
638 : }
639 :
640 : /* take the absolute value of the input signal and copy it to the input buffer */
641 : /* multiply each value by 1 / filter length */
642 53844 : p_in = &buf_in[K];
643 17283924 : for ( k = 0; k < len; k++ )
644 : {
645 17230080 : *p_in++ = (float) fabs( inp[k] ) * w;
646 : }
647 :
648 : /* update filter memory from the end of the input buffer */
649 53844 : if ( mem_h != NULL )
650 : {
651 17948 : mvr2r( &buf_in[len], mem_h, K );
652 : }
653 :
654 : /* do MA filtering */
655 53844 : out[0] = sum_f( buf_in, len_h );
656 53844 : p_out = &buf_in[0]; /* pointer to leaving sample */
657 53844 : p_in = &buf_in[len_h]; /* pointer to entering sample*/
658 16691640 : for ( k = 1; k < len - K; k++ )
659 : {
660 16637796 : out[k] = out[k - 1] - *p_out++ + *p_in++;
661 : }
662 :
663 : /* use IIR filtering to extrapolate the last K samples */
664 53844 : p_in = &buf_in[len - K];
665 53844 : p_out = &out[len - K];
666 53844 : p_prev = p_out - 1;
667 592284 : for ( k = 0; k < K; k++ )
668 : {
669 538440 : *p_out++ = 0.05f * ( *p_in++ ) + 0.95f * ( *p_prev++ );
670 : }
671 :
672 53844 : return;
673 : }
674 :
675 : /*-------------------------------------------------------------------*
676 : * GenShapedSHBExcitation()
677 : *
678 : * Synthesize spectrally shaped highband excitation signal
679 : *-------------------------------------------------------------------*/
680 :
681 3038855 : void GenShapedSHBExcitation(
682 : float *excSHB, /* o : synthesized shaped shb excitation */
683 : const float *lpc_shb, /* i : lpc coefficients */
684 : float *White_exc16k_FB, /* o : white excitation for the Fullband extension */
685 : float *mem_csfilt, /* i/o: memory */
686 : float *mem_genSHBexc_filt_down_shb, /* i/o: memory */
687 : float *state_lpc_syn, /* i/o: memory */
688 : const int16_t coder_type, /* i : coding type */
689 : const float *bwe_exc_extended, /* i : bandwidth extended excitation */
690 : int16_t bwe_seed[], /* i/o: random number generator seed */
691 : float voice_factors[], /* i : voicing factor */
692 : const int16_t extl, /* i : extension layer */
693 : float *tbe_demph, /* i/o: de-emphasis memory */
694 : float *tbe_premph, /* i/o: pre-emphasis memory */
695 : float *lpc_shb_sf, /* i : LP coefficients */
696 : float *shb_ener_sf,
697 : float *shb_res_gshape,
698 : float *shb_res,
699 : int16_t *vf_ind,
700 : const float formant_fac, /* i : Formant sharpening factor [0..1] */
701 : float fb_state_lpc_syn[], /* i/o: memory */
702 : float *fb_tbe_demph, /* i/o: fb de-emphasis memory */
703 : const int32_t total_brate, /* i : bitrate */
704 : const int16_t prev_bfi, /* i : previous frame was concealed */
705 : const int16_t element_mode, /* i : element mode */
706 : const int16_t flag_ACELP16k, /* i : ACELP@16kHz flag */
707 : float *nlExc16k, /* i/o: NL exc for IC-BWE */
708 : float *mixExc16k, /* i/o: exc spreading for IC-BWE */
709 : const int32_t extl_brate, /* i : extension layer bitarte */
710 : const int16_t MSFlag, /* i : Multi Source flag */
711 : float EnvSHBres_4k[], /* i/o: TD envelope of the SHB residual signal */
712 : float *prev_pow_exc16kWhtnd, /* i/o: power of the LB excitation signal in the previous frame */
713 : float *prev_mix_factor, /* i/o: mixing factor in the previous frame */
714 : float *Env_error, /* o : error in SHB residual envelope modelling*/
715 : float Env_error_part[] /* o : per-segment error in SHB residual envelope modelling */
716 : )
717 : {
718 : int16_t i, j, k;
719 : float wht_fil_mem[LPC_WHTN_ORDER];
720 : float lpc_whtn[LPC_WHTN_ORDER + 1];
721 : float R[LPC_WHTN_ORDER + 2];
722 : float exc32k[L_FRAME32k], exc16k[L_FRAME16k];
723 : float pow1, pow22, scale, temp1, temp2;
724 : float excTmp2[L_FRAME16k];
725 : int16_t nbSubFr;
726 : float excNoisyEnv[L_FRAME16k];
727 3038855 : float csfilt_num2[1] = { 0.2f };
728 3038855 : float csfilt_den2[2] = { 1.0f, -0.8f };
729 : float varEnvShape;
730 : float ervec[LPC_WHTN_ORDER + 2];
731 : float exc16kWhtnd[L_FRAME16k];
732 3038855 : float temp = 0.0f;
733 : float *White_exc16k;
734 : float voiceFacEst[NB_SUBFR16k];
735 : float syn_shb_ener_sf[4], tempSHB[80];
736 : float zero_mem[LPC_SHB_ORDER];
737 : float vf_tmp;
738 : float White_exc16k_FB_temp[L_FRAME16k];
739 3038855 : float fb_deemph_fac = 0.48f;
740 : double tempD;
741 : float alpha, step, mem_csfilt_left, mem_csfilt_right, excNoisyEnvLeft[L_FRAME16k], excNoisyEnvRight[L_FRAME16k];
742 : int16_t cbsize;
743 : float mix_factor, old_fact, new_fact, fact, old_scale, new_scale, step_scale;
744 : float c0, c1, c2, c3, c4, c5, g1, g2, g, den;
745 : float EnvWhiteExc16k[L_FRAME16k], EnvExc16kWhtnd[L_FRAME16k];
746 : float EnvWhiteExc16k_4k[L_FRAME4k], EnvExc16kWhtnd_4k[L_FRAME4k];
747 : int16_t flag_plosive;
748 : float delta;
749 : 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];
750 :
751 3038855 : mix_factor = 0.0f;
752 :
753 3038855 : set_f( zero_mem, 0, LPC_SHB_ORDER );
754 3038855 : set_f( wht_fil_mem, 0, LPC_WHTN_ORDER );
755 3038855 : set_f( EnvWhiteExc16k_4k, 0, L_FRAME4k );
756 3038855 : set_f( EnvExc16kWhtnd_4k, 0, L_FRAME4k );
757 :
758 : /* Mirror the spectrum */
759 1947906055 : for ( i = 0; i < L_FRAME32k; i++ )
760 : {
761 1944867200 : exc32k[i] = ( ( i % 2 ) == 0 ) ? ( -bwe_exc_extended[i] ) : ( bwe_exc_extended[i] );
762 : }
763 :
764 : /* Decimate by 2 */
765 3038855 : Decimate_allpass_steep( exc32k, mem_genSHBexc_filt_down_shb, 2 * L_FRAME16k, exc16k );
766 :
767 3038855 : autocorr( exc16k, R, LPC_WHTN_ORDER + 1, L_FRAME16k, win_flatten, 0, 1, 1 );
768 :
769 : /* Ensure R[0] isn't zero when entering Levinson-Durbin */
770 3038855 : R[0] = max( R[0], 1.0e-8f );
771 18233130 : for ( i = 0; i <= LPC_WHTN_ORDER; i++ )
772 : {
773 15194275 : R[i] = R[i] * wac[i];
774 : }
775 :
776 : /* Ensure R[0] isn't zero when entering Levinson-Durbin */
777 3038855 : R[0] += 1.0e-8f;
778 :
779 3038855 : lev_dur( lpc_whtn, R, LPC_WHTN_ORDER, ervec );
780 :
781 3038855 : fir( exc16k, lpc_whtn, exc16kWhtnd, wht_fil_mem, L_FRAME16k, LPC_WHTN_ORDER, 0 );
782 :
783 3038855 : if ( extl_brate >= SWB_TBE_2k8 )
784 : {
785 212914485 : for ( i = 0; i < L_FRAME16k; i++ )
786 : {
787 212251200 : exc16kWhtnd[i] *= shb_res_gshape[(int16_t) ( i / 80 )];
788 : }
789 : }
790 :
791 975472455 : for ( k = 0, pow1 = 0.00001f; k < L_FRAME16k; k++ )
792 : {
793 972433600 : excTmp2[k] = (float) ( fabs( exc16kWhtnd[k] ) );
794 972433600 : pow1 += exc16kWhtnd[k] * exc16kWhtnd[k];
795 : }
796 :
797 3038855 : if ( !flag_ACELP16k )
798 : {
799 1434997 : varEnvShape = mean( voice_factors, NB_SUBFR );
800 : }
801 : else
802 : {
803 1603858 : varEnvShape = mean( voice_factors, NB_SUBFR16k );
804 : }
805 :
806 3038855 : if ( extl == FB_TBE )
807 : {
808 949212 : fb_deemph_fac = max( ( 0.68f - (float) pow( varEnvShape, 3 ) ), 0.48f );
809 : }
810 :
811 3038855 : varEnvShape = 1.09875f - 0.49875f * varEnvShape;
812 3038855 : varEnvShape = min( max( varEnvShape, 0.6f ), 0.999f );
813 3038855 : csfilt_num2[0] = 1.0f - varEnvShape;
814 3038855 : csfilt_den2[1] = -varEnvShape;
815 :
816 3038855 : if ( element_mode == EVS_MONO && *mem_csfilt == 0 && ( total_brate == ACELP_9k60 || total_brate == ACELP_16k40 || total_brate == ACELP_24k40 ) )
817 : {
818 : /* pre-init smoothing avoid energy drop outs */
819 1126 : float tmp_scale = 0;
820 23646 : for ( i = 0; i < L_SUBFR16k / 4; i++ )
821 : {
822 22520 : tmp_scale += excTmp2[i];
823 : }
824 :
825 : /* 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 */
826 : /* rather stick to the more conservative approach, to avoid potential clippings */
827 1126 : if ( !( prev_bfi && extl == FB_TBE ) )
828 : {
829 : /* use weak smoothing for 1st frame after switching to make filter recover more quickly */
830 1116 : varEnvShape = 0.8f;
831 1116 : csfilt_num2[0] = 1.0f - varEnvShape;
832 1116 : csfilt_den2[1] = -varEnvShape;
833 : }
834 1126 : *mem_csfilt = varEnvShape * ( tmp_scale / ( L_SUBFR16k / 4 ) );
835 : }
836 :
837 3038855 : if ( MSFlag > 0 )
838 : {
839 147393 : varEnvShape = 0.995f;
840 147393 : csfilt_num2[0] = 1.0f - varEnvShape;
841 147393 : csfilt_den2[1] = -varEnvShape;
842 : }
843 :
844 3038855 : White_exc16k = exc16k;
845 :
846 : /* Track the low band envelope */
847 3038855 : if ( element_mode == IVAS_CPE_TD || element_mode == IVAS_CPE_DFT )
848 : {
849 761046 : if ( extl_brate != SWB_TBE_1k10 && extl_brate != SWB_TBE_1k75 )
850 : {
851 714588 : mem_csfilt_left = 0.0f;
852 714588 : mem_csfilt_right = 0.0f;
853 229382748 : for ( k = 0; k < L_FRAME16k; k++ )
854 : {
855 228668160 : excNoisyEnvLeft[k] = mem_csfilt_left + csfilt_num2[0] * excTmp2[k];
856 228668160 : mem_csfilt_left = -csfilt_den2[1] * excNoisyEnvLeft[k];
857 228668160 : excNoisyEnvRight[L_FRAME16k - k - 1] = mem_csfilt_right + csfilt_num2[0] * excTmp2[L_FRAME16k - k - 1];
858 228668160 : mem_csfilt_right = -csfilt_den2[1] * excNoisyEnvRight[L_FRAME16k - k - 1];
859 : }
860 :
861 714588 : alpha = 0.0f;
862 714588 : step = 1.0f / L_FRAME16k;
863 229382748 : for ( k = 0; k < L_FRAME16k; k++ )
864 : {
865 228668160 : excNoisyEnv[k] = alpha * excNoisyEnvLeft[k] + ( 1 - alpha ) * excNoisyEnvRight[k];
866 228668160 : alpha += step;
867 : }
868 : }
869 : }
870 : else
871 : {
872 731176689 : for ( k = 0; k < L_FRAME16k; k++ )
873 : {
874 728898880 : excNoisyEnv[k] = *mem_csfilt + csfilt_num2[0] * excTmp2[k];
875 728898880 : *mem_csfilt = -csfilt_den2[1] * excNoisyEnv[k];
876 : }
877 : }
878 :
879 3038855 : if ( extl_brate == SWB_TBE_1k10 || extl_brate == SWB_TBE_1k75 )
880 : {
881 : /* generate gaussian (white) excitation */
882 14913018 : for ( k = 0; k < L_FRAME16k; k++ )
883 : {
884 14866560 : White_exc16k[k] = (float) own_random( &bwe_seed[0] );
885 : }
886 :
887 : /* normalize the amplitude of the gaussian excitation to that of the LB exc. */
888 46458 : pow22 = POW_EXC16k_WHTND;
889 46458 : v_multc( White_exc16k, (float) sqrt( pow1 / pow22 ), White_exc16k, L_FRAME16k );
890 : }
891 : else
892 : {
893 2992397 : create_random_vector( White_exc16k, L_FRAME, bwe_seed );
894 2992397 : create_random_vector( White_exc16k + L_FRAME, L_FRAME16k - L_FRAME, bwe_seed );
895 :
896 960559437 : for ( k = 0, pow22 = 0.00001f; k < L_FRAME16k; k++ )
897 : {
898 957567040 : White_exc16k[k] *= excNoisyEnv[k];
899 957567040 : pow22 += White_exc16k[k] * White_exc16k[k];
900 : }
901 : }
902 :
903 3038855 : flag_plosive = 0;
904 :
905 3038855 : if ( extl_brate >= SWB_TBE_2k8 || extl_brate == SWB_TBE_1k10 || extl_brate == SWB_TBE_1k75 )
906 : {
907 709743 : if ( *vf_ind == 20 ) /* encoder side */
908 : {
909 233266 : if ( extl_brate == SWB_TBE_1k10 || extl_brate == SWB_TBE_1k75 )
910 : {
911 : /* calculate TD envelopes of exc16kWhtnd and White_exc16k */
912 17948 : find_td_envelope( White_exc16k, L_FRAME16k, 20, NULL, EnvWhiteExc16k );
913 17948 : find_td_envelope( exc16kWhtnd, L_FRAME16k, 20, NULL, EnvExc16kWhtnd );
914 :
915 1453788 : for ( k = 0; k < L_FRAME4k; k++ )
916 : {
917 1435840 : EnvWhiteExc16k_4k[k] = EnvWhiteExc16k[4 * k];
918 1435840 : EnvExc16kWhtnd_4k[k] = EnvExc16kWhtnd[4 * k];
919 : }
920 :
921 : /* calculate the optimal mix factor */
922 17948 : c0 = c1 = c2 = c3 = c4 = c5 = 0.0f;
923 89740 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
924 : {
925 71792 : c0_part[i] = sum2_f( &EnvExc16kWhtnd_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], L_FRAME4k / NUM_SHB_SUBGAINS );
926 71792 : 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 );
927 71792 : c2_part[i] = sum2_f( &EnvWhiteExc16k_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], L_FRAME4k / NUM_SHB_SUBGAINS );
928 71792 : 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 );
929 71792 : 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 );
930 71792 : c5_part[i] = sum2_f( &EnvSHBres_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], L_FRAME4k / NUM_SHB_SUBGAINS );
931 :
932 71792 : c0 += c0_part[i];
933 71792 : c1 += c1_part[i];
934 71792 : c2 += c2_part[i];
935 71792 : c3 += c3_part[i];
936 71792 : c4 += c4_part[i];
937 71792 : c5 += c5_part[i];
938 : }
939 :
940 17948 : den = 4.0f * c0 * c2 - c4 * c4;
941 :
942 17948 : if ( den == 0.0f )
943 : {
944 0 : den = 1e-7f;
945 : }
946 :
947 17948 : g1 = ( c3 * c4 - 2 * c1 * c2 ) / den;
948 17948 : g2 = ( c1 * c4 - 2 * c0 * c3 ) / den;
949 :
950 17948 : *Env_error = 0.0f;
951 17948 : flag_plosive = 0;
952 89740 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
953 : {
954 71792 : 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];
955 71792 : *Env_error += Env_error_part[i];
956 :
957 71792 : if ( Env_error_part[i] > THR_ENV_ERROR_PLOSIVE )
958 : {
959 : /* envelope error is too high -> likely a plosive */
960 54 : flag_plosive = 1;
961 : }
962 : }
963 :
964 17948 : if ( flag_plosive )
965 : {
966 : /* plosive detected -> set the mixing factor to 0 */
967 35 : *vf_ind = 0;
968 35 : mix_factor = 0.0f;
969 : }
970 : else
971 : {
972 : /* normalize gain */
973 17913 : temp = 0.0f;
974 17913 : if ( g1 + g2 == 0.0f )
975 : {
976 650 : temp = 1e-7f;
977 : }
978 :
979 17913 : g = g2 / ( g1 + g2 + temp );
980 :
981 : /* quantization of the mixing factor */
982 17913 : cbsize = 1 << NUM_BITS_SHB_VF;
983 17913 : delta = 1.0f / ( cbsize - 1 );
984 17913 : if ( g > 1.0f )
985 : {
986 3165 : g = 1.0f;
987 : }
988 14748 : else if ( g < delta )
989 : {
990 : /* prevent low gains to be quantized to 0 as this is reserved for plosives */
991 737 : g = delta;
992 : }
993 :
994 17913 : *vf_ind = usquant( g, &mix_factor, 0.0f, 1.0f / ( cbsize - 1 ), cbsize );
995 : }
996 : }
997 : else
998 : {
999 215318 : Estimate_mix_factors( shb_res, exc16kWhtnd, White_exc16k, pow1, pow22, voiceFacEst, vf_ind );
1000 215318 : temp = ( voiceFacEst[0] > 0.7f ) ? 1.0f : 0.8f;
1001 : }
1002 : }
1003 : else /* decoder side */
1004 : {
1005 476477 : if ( extl_brate == SWB_TBE_1k10 || extl_brate == SWB_TBE_1k75 )
1006 : {
1007 28510 : if ( *vf_ind == 0 )
1008 : {
1009 95 : mix_factor = 0.0f;
1010 95 : flag_plosive = 1;
1011 : }
1012 : else
1013 : {
1014 28415 : mix_factor = usdequant( *vf_ind, 0.0f, 1.0f / ( ( 1 << NUM_BITS_SHB_VF ) - 1 ) );
1015 : }
1016 : }
1017 : else
1018 : {
1019 447967 : temp = ( ( *vf_ind * 0.125f ) > 0.7f ) ? 1.0f : 0.8f;
1020 : }
1021 : }
1022 :
1023 709743 : if ( extl_brate != SWB_TBE_1k10 && extl_brate != SWB_TBE_1k75 )
1024 : {
1025 3979710 : for ( i = 0; i < NB_SUBFR16k; i++ )
1026 : {
1027 3316425 : voice_factors[i] *= temp;
1028 : }
1029 : }
1030 : }
1031 :
1032 3038855 : if ( element_mode >= IVAS_CPE_DFT && nlExc16k != NULL )
1033 : {
1034 : /* save buffers for IC-BWE */
1035 533162 : mvr2r( exc16kWhtnd, nlExc16k, L_FRAME16k );
1036 533162 : v_multc( White_exc16k, (float) sqrt( pow1 / pow22 ), mixExc16k, L_FRAME16k );
1037 : }
1038 :
1039 3038855 : mvr2r( White_exc16k, White_exc16k_FB, L_FRAME16k );
1040 3038855 : deemph( White_exc16k, PREEMPH_FAC, L_FRAME16k, tbe_demph );
1041 :
1042 3038855 : if ( extl_brate == SWB_TBE_1k10 || extl_brate == SWB_TBE_1k75 )
1043 : {
1044 46458 : if ( !flag_plosive ) /* use only LB excitation in case of plosives */
1045 : {
1046 : /* re-scale gaussian excitation at the beginning to gradually move from old energy to new energy */
1047 46328 : old_scale = (float) sqrt( *prev_pow_exc16kWhtnd / pow1 );
1048 46328 : new_scale = 1.0f;
1049 46328 : step_scale = ( new_scale - old_scale ) / ( L_FRAME16k / 2 );
1050 46328 : scale = old_scale;
1051 :
1052 : /* interpolate between the old and the new value of the mixing factor */
1053 46328 : old_fact = *prev_mix_factor;
1054 46328 : new_fact = mix_factor;
1055 46328 : step = ( new_fact - old_fact ) / ( L_FRAME16k / 2 );
1056 46328 : fact = old_fact;
1057 :
1058 : /* mixing of LB and gaussian excitation in the first half of the frame */
1059 7458808 : for ( k = 0; k < L_FRAME16k / 2; k++ )
1060 : {
1061 7412480 : exc16kWhtnd[k] = (float) fact * ( White_exc16k[k] * scale ) + (float) ( 1 - fact ) * exc16kWhtnd[k];
1062 7412480 : fact += step;
1063 7412480 : scale += step_scale;
1064 : }
1065 :
1066 : /* mixing of LB and gaussian excitation in the second half of the frame */
1067 7458808 : for ( ; k < L_FRAME16k; k++ )
1068 : {
1069 7412480 : exc16kWhtnd[k] = (float) new_fact * White_exc16k[k] + (float) ( 1 - new_fact ) * exc16kWhtnd[k];
1070 : }
1071 : }
1072 46458 : preemph( exc16kWhtnd, PREEMPH_FAC, L_FRAME16k, tbe_premph );
1073 : }
1074 : else
1075 : {
1076 2992397 : if ( coder_type == UNVOICED || MSFlag == 1 )
1077 : {
1078 197284 : scale = (float) sqrt( pow1 / pow22 );
1079 197284 : if ( pow22 == 0.f )
1080 : {
1081 0 : scale = 0;
1082 : }
1083 63328164 : for ( k = 0; k < L_FRAME16k; k++ )
1084 : {
1085 63130880 : exc16kWhtnd[k] = White_exc16k[k] * scale;
1086 : }
1087 :
1088 197284 : preemph( exc16kWhtnd, PREEMPH_FAC, L_FRAME16k, tbe_premph );
1089 : }
1090 : else
1091 : {
1092 2795113 : nbSubFr = ( extl_brate < SWB_TBE_2k8 ) ? NB_SUBFR : NB_SUBFR16k; /* note: this condition is designed based on TBE bitrate rather than internal sampling rate */
1093 :
1094 14636133 : for ( i = 0, k = 0; i < nbSubFr; i++ )
1095 : {
1096 11841020 : if ( coder_type == VOICED && extl_brate < SWB_TBE_2k8 )
1097 : {
1098 663636 : temp = (float) sqrt( voice_factors[i] );
1099 663636 : temp1 = (float) sqrt( temp );
1100 663636 : temp2 = (float) sqrt( ( pow1 * ( 1.0f - temp ) ) / pow22 );
1101 663636 : if ( pow22 == 0.f )
1102 : {
1103 0 : temp2 = 0;
1104 : }
1105 : }
1106 : else
1107 : {
1108 : /* Adjust noise mixing for formant sharpening filter */
1109 11177384 : vf_tmp = SWB_NOISE_MIX_FAC * formant_fac;
1110 11177384 : vf_tmp = voice_factors[i] * ( 1.0f - vf_tmp );
1111 :
1112 11177384 : temp1 = (float) sqrt( vf_tmp );
1113 11177384 : temp2 = (float) sqrt( ( pow1 * ( 1.0f - vf_tmp ) ) / pow22 );
1114 11177384 : if ( pow22 == 0.f )
1115 : {
1116 0 : temp2 = 0;
1117 : }
1118 : }
1119 :
1120 906277180 : for ( j = 0; j < L_FRAME16k / nbSubFr; j++, k++ )
1121 : {
1122 894436160 : exc16kWhtnd[k] = temp1 * exc16kWhtnd[k] + temp2 * White_exc16k[k];
1123 : }
1124 :
1125 11841020 : temp = (float) sqrt( 1.0f - voice_factors[i] );
1126 11841020 : temp = PREEMPH_FAC * temp / ( temp1 + temp );
1127 :
1128 11841020 : preemph( &exc16kWhtnd[i * L_FRAME16k / nbSubFr], temp, L_FRAME16k / nbSubFr, tbe_premph );
1129 : }
1130 : }
1131 : }
1132 :
1133 3038855 : if ( extl_brate < SWB_TBE_2k8 )
1134 : {
1135 2375570 : syn_filt( lpc_shb, LPC_SHB_ORDER, exc16kWhtnd, excSHB, L_FRAME16k, state_lpc_syn, 1 );
1136 : }
1137 : else
1138 : {
1139 663285 : set_f( zero_mem, 0, LPC_SHB_ORDER );
1140 663285 : syn_filt( lpc_shb_sf, LPC_SHB_ORDER, exc16kWhtnd, tempSHB, 80, zero_mem, 1 );
1141 663285 : syn_shb_ener_sf[0] = 0.125f * sum2_f( tempSHB, 80 );
1142 663285 : syn_filt( lpc_shb_sf + ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 80, tempSHB, 80, zero_mem, 1 );
1143 663285 : syn_shb_ener_sf[1] = 0.125f * sum2_f( tempSHB, 80 );
1144 663285 : syn_filt( lpc_shb_sf + 2 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 160, tempSHB, 80, zero_mem, 1 );
1145 663285 : syn_shb_ener_sf[2] = 0.125f * sum2_f( tempSHB, 80 );
1146 663285 : syn_filt( lpc_shb_sf + 3 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 240, tempSHB, 80, zero_mem, 1 );
1147 663285 : syn_shb_ener_sf[3] = 0.125f * sum2_f( tempSHB, 80 );
1148 :
1149 663285 : if ( total_brate <= MAX_ACELP_BRATE )
1150 : {
1151 663285 : 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] );
1152 663285 : tempD = sqrt( tempSHB[0] );
1153 :
1154 212914485 : for ( i = 0; i < L_FRAME16k; i++ )
1155 : {
1156 212251200 : exc16kWhtnd[i] = (float) ( exc16kWhtnd[i] * tempD );
1157 : }
1158 : }
1159 :
1160 663285 : syn_filt( lpc_shb_sf, LPC_SHB_ORDER, exc16kWhtnd, excSHB, 80, state_lpc_syn, 1 );
1161 663285 : syn_filt( lpc_shb_sf + ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 80, excSHB + 80, 80, state_lpc_syn, 1 );
1162 663285 : syn_filt( lpc_shb_sf + 2 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 160, excSHB + 160, 80, state_lpc_syn, 1 );
1163 663285 : syn_filt( lpc_shb_sf + 3 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 240, excSHB + 240, 80, state_lpc_syn, 1 );
1164 : }
1165 :
1166 3038855 : if ( extl == FB_TBE )
1167 : {
1168 949212 : syn_filt( lpc_shb, LPC_SHB_ORDER, White_exc16k_FB, White_exc16k_FB_temp, L_FRAME16k, fb_state_lpc_syn, 1 );
1169 :
1170 304697052 : for ( i = 0; i < L_FRAME16k; i++ )
1171 : {
1172 303747840 : White_exc16k_FB_temp[i] *= cos_fb_exc[i % 32];
1173 : }
1174 :
1175 949212 : flip_spectrum( White_exc16k_FB_temp, White_exc16k_FB, L_FRAME16k );
1176 :
1177 949212 : deemph( White_exc16k_FB, fb_deemph_fac, L_FRAME16k, fb_tbe_demph );
1178 : }
1179 : else
1180 : {
1181 670775403 : for ( i = 0; i < L_FRAME16k; i++ )
1182 : {
1183 668685760 : White_exc16k_FB[i] = 0.0f;
1184 : }
1185 : }
1186 :
1187 3038855 : *prev_pow_exc16kWhtnd = pow1;
1188 3038855 : *prev_mix_factor = mix_factor;
1189 :
1190 3038855 : return;
1191 : }
1192 :
1193 : /*-------------------------------------------------------------------*
1194 : * GenSHBSynth()
1195 : *
1196 : * Generate 32 KHz sampled highband component from synthesized highband
1197 : *-------------------------------------------------------------------*/
1198 :
1199 2118165 : void GenSHBSynth(
1200 : const float *input_synspeech, /* i : input synthesized speech */
1201 : float *shb_syn_speech_32k, /* o : output highband component */
1202 : float Hilbert_Mem[], /* i/o: memory */
1203 : float state_lsyn_filt_shb_local[], /* i/o: memory */
1204 : const int16_t L_frame, /* i : ACELP frame length */
1205 : int16_t *syn_dm_phase )
1206 : {
1207 : float speech_buf_32k[L_FRAME32k];
1208 : int16_t i;
1209 :
1210 2118165 : Interpolate_allpass_steep( input_synspeech, state_lsyn_filt_shb_local, L_FRAME16k, speech_buf_32k );
1211 :
1212 2118165 : if ( L_frame == L_FRAME )
1213 : {
1214 932398 : 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 );
1215 : }
1216 : else
1217 : {
1218 760076647 : for ( i = 0; i < L_FRAME32k; i++ )
1219 : {
1220 758890880 : shb_syn_speech_32k[i] = ( ( i % 2 ) == 0 ) ? ( -speech_buf_32k[i] ) : ( speech_buf_32k[i] );
1221 : }
1222 : }
1223 :
1224 2118165 : return;
1225 : }
1226 :
1227 : /*-------------------------------------------------------------------*
1228 : * ScaleShapedSHB()
1229 : *
1230 : *
1231 : *-------------------------------------------------------------------*/
1232 :
1233 2512821 : void ScaleShapedSHB(
1234 : const int16_t length, /* i : SHB overlap length */
1235 : float *synSHB, /* i/o: synthesized shb signal */
1236 : float *overlap, /* i/o: buffer for overlap-add */
1237 : const float *subgain, /* i : subframe gain */
1238 : const float frame_gain, /* i : frame gain */
1239 : const float *win, /* i : window */
1240 : const float *subwin /* i : subframes window */
1241 : )
1242 : {
1243 : const int16_t *skip;
1244 : int16_t i, j, k, l_shb_lahead, l_frame;
1245 : int16_t join_length, num_join;
1246 : float mod_syn[L_FRAME16k + L_SHB_LAHEAD], sum_gain;
1247 :
1248 : /* initilaization */
1249 2512821 : l_frame = L_FRAME16k;
1250 2512821 : l_shb_lahead = L_SHB_LAHEAD;
1251 2512821 : skip = skip_bands_SWB_TBE;
1252 :
1253 2512821 : if ( length == SHB_OVERLAP_LEN / 2 )
1254 : {
1255 405536 : skip = skip_bands_WB_TBE;
1256 405536 : l_frame = L_FRAME16k / 4;
1257 405536 : l_shb_lahead = L_SHB_LAHEAD / 4;
1258 : }
1259 :
1260 : /* apply gain for each subframe, and store noise output signal using overlap-add */
1261 2512821 : set_f( mod_syn, 0, l_frame + l_shb_lahead );
1262 :
1263 2512821 : if ( length == SHB_OVERLAP_LEN / 2 )
1264 : {
1265 405536 : sum_gain = 0;
1266 2433216 : for ( k = 0; k < length / 2; k++ )
1267 : {
1268 2027680 : sum_gain = subwin[2 * k + 2] * subgain[0];
1269 2027680 : mod_syn[skip[0] + k] = synSHB[skip[0] + k] * sum_gain;
1270 2027680 : mod_syn[skip[0] + k + length / 2] = synSHB[skip[0] + k + length / 2] * subgain[0];
1271 : }
1272 3244288 : for ( i = 1; i < NUM_SHB_SUBFR / 2; i++ )
1273 : {
1274 31226272 : for ( k = 0; k < length; k++ )
1275 : {
1276 28387520 : sum_gain = subwin[k + 1] * subgain[i] + subwin[length - k - 1] * subgain[i - 1];
1277 28387520 : mod_syn[skip[i] + k] = synSHB[skip[i] + k] * sum_gain;
1278 : }
1279 : }
1280 2433216 : for ( k = 0; k < length / 2; k++ )
1281 : {
1282 2027680 : sum_gain = subwin[length - 2 * k - 2] * subgain[i - 1];
1283 2027680 : mod_syn[skip[i] + k] = synSHB[skip[i] + k] * sum_gain;
1284 : }
1285 : }
1286 : else
1287 : {
1288 2107285 : num_join = NUM_SHB_SUBFR / NUM_SHB_SUBGAINS;
1289 2107285 : join_length = num_join * length;
1290 44252985 : for ( k = 0, j = 0; k < length; k++ )
1291 : {
1292 42145700 : mod_syn[j] = synSHB[j] * subwin[k + 1] * subgain[0];
1293 42145700 : j++;
1294 : }
1295 8429140 : for ( i = 0; i < NUM_SHB_SUBGAINS - 1; i++ )
1296 : {
1297 385633155 : for ( k = 0; k < join_length - length; k++ )
1298 : {
1299 379311300 : mod_syn[j] = synSHB[j] * subgain[i * num_join];
1300 379311300 : j++;
1301 : }
1302 :
1303 132758955 : for ( k = 0; k < length; k++ )
1304 : {
1305 126437100 : mod_syn[j] = synSHB[j] * ( subwin[length - k - 1] * subgain[i * num_join] + subwin[k + 1] * subgain[( i + 1 ) * num_join] );
1306 126437100 : j++;
1307 : }
1308 : }
1309 128544385 : for ( k = 0; k < join_length - length; k++ )
1310 : {
1311 126437100 : mod_syn[j] = synSHB[j] * subgain[( NUM_SHB_SUBGAINS - 1 ) * num_join];
1312 126437100 : j++;
1313 : }
1314 44252985 : for ( k = 0; k < length; k++ )
1315 : {
1316 42145700 : mod_syn[j] = synSHB[j] * subwin[length - k - 1] * subgain[( NUM_SHB_SUBGAINS - 1 ) * num_join];
1317 42145700 : j++;
1318 : }
1319 : }
1320 :
1321 46686201 : for ( i = 0; i < l_shb_lahead; i++ )
1322 : {
1323 44173380 : synSHB[i] = mod_syn[i] * win[i] * frame_gain;
1324 44173380 : synSHB[i] += overlap[i];
1325 44173380 : synSHB[i + l_shb_lahead] = mod_syn[i] * frame_gain;
1326 : }
1327 :
1328 665113521 : for ( ; i < l_frame; i++ )
1329 : {
1330 662600700 : synSHB[i] = mod_syn[i] * frame_gain;
1331 : }
1332 :
1333 46686201 : for ( ; i < l_frame + l_shb_lahead; i++ )
1334 : {
1335 44173380 : overlap[i - l_frame] = mod_syn[i] * win[l_frame + l_shb_lahead - 1 - i] * frame_gain;
1336 : }
1337 :
1338 2512821 : return;
1339 : }
1340 :
1341 : /*-------------------------------------------------------------------*
1342 : * non_linearity()
1343 : *
1344 : * Apply a non linearity to the SHB excitation
1345 : * -------------------------------------------------------------------*/
1346 :
1347 5113903 : void non_linearity(
1348 : const float input[], /* i : input signal */
1349 : float output[], /* o : output signal */
1350 : float old_bwe_exc_extended[], /* i/o: memory bugffer */
1351 : const int16_t length, /* i : input length */
1352 : float *prev_scale, /* i/o: memory */
1353 : const int16_t coder_type, /* i : Coder Type */
1354 : const float *voice_factors, /* i : Voice Factors */
1355 : const int16_t L_frame /* i : ACELP frame length */
1356 : #ifdef NONBE_1328_FIX_NON_LINEARITY
1357 : ,
1358 : const int16_t element_mode /* i : element_mode to differentiate EVS and IVAS */
1359 : #endif
1360 : )
1361 : {
1362 : int16_t i, j;
1363 :
1364 5113903 : float max_val = 0.0;
1365 : float scale, temp;
1366 : float scale_step;
1367 : float *p_out;
1368 :
1369 5113903 : int16_t en_abs = 0;
1370 5113903 : float v_fac = 0, ths;
1371 : int16_t nframes;
1372 : #ifdef NONBE_1328_FIX_NON_LINEARITY
1373 : float sc_factor;
1374 : #endif
1375 5113903 : if ( L_frame == L_FRAME16k )
1376 : {
1377 2337155 : nframes = 5;
1378 2337155 : ths = 0.87f;
1379 : }
1380 : else
1381 : {
1382 2776748 : nframes = 4;
1383 2776748 : ths = 0.94f;
1384 : }
1385 :
1386 27906670 : for ( i = 0; i < nframes; i++ )
1387 : {
1388 22792767 : v_fac += voice_factors[i];
1389 : }
1390 5113903 : v_fac /= nframes;
1391 :
1392 5113903 : if ( coder_type == VOICED && v_fac > ths )
1393 : {
1394 253 : en_abs = 1;
1395 : }
1396 :
1397 5113903 : p_out = output + NL_BUFF_OFFSET; /* NL_BUFF_OFFSET = 12 */
1398 : /* update buffer memory */
1399 5113903 : mvr2r( old_bwe_exc_extended, output, NL_BUFF_OFFSET );
1400 :
1401 1641562863 : for ( i = j = 0; i < length / 2; i++ )
1402 : {
1403 1636448960 : if ( ( temp = (float) fabs( input[i] ) ) > max_val )
1404 : {
1405 34227741 : max_val = temp;
1406 34227741 : j = i;
1407 : }
1408 : }
1409 :
1410 5113903 : if ( max_val > 1.0f )
1411 : {
1412 4424164 : scale = 0.67f / max_val;
1413 : }
1414 : else
1415 : {
1416 689739 : scale = 0.67f;
1417 : }
1418 :
1419 : #ifdef NONBE_1328_FIX_NON_LINEARITY
1420 5113903 : sc_factor = 1024.0f;
1421 5113903 : if ( element_mode > EVS_MONO )
1422 : {
1423 4999695 : sc_factor = (float) ( 1 << max( 13 - norm_s( j + 1 ), 0 ) ); /* Adapt the scaling factor allowed depending of max position */
1424 4999695 : sc_factor = max( sc_factor, 2.0f );
1425 : }
1426 5113903 : if ( *prev_scale <= 0.0 || *prev_scale > sc_factor * scale )
1427 : #else
1428 : if ( *prev_scale <= 0.0 || *prev_scale > 1024.0f * scale )
1429 : #endif
1430 : {
1431 711882 : scale_step = 1.0;
1432 711882 : *prev_scale = scale;
1433 : }
1434 : else
1435 : {
1436 4402021 : scale_step = 1.0f;
1437 4402021 : if ( j != 0 )
1438 : {
1439 3807291 : scale_step = (float) exp( 1.0f / (float) j * (float) log( scale / *prev_scale ) );
1440 : }
1441 : }
1442 :
1443 1641562863 : for ( i = 0; i < length / 2; i++ )
1444 : {
1445 1636448960 : if ( input[i] >= 0.0 )
1446 : {
1447 910123808 : *p_out++ = ( input[i] * input[i] ) * *prev_scale;
1448 : }
1449 : else
1450 : {
1451 726325152 : if ( en_abs )
1452 : {
1453 38401 : *p_out++ = 1.0f * ( input[i] * input[i] ) * *prev_scale;
1454 : }
1455 : else
1456 : {
1457 726286751 : *p_out++ = -1.0f * ( input[i] * input[i] ) * *prev_scale;
1458 : }
1459 : }
1460 :
1461 1636448960 : if ( i < j )
1462 : {
1463 721345658 : *prev_scale *= scale_step;
1464 : }
1465 : }
1466 :
1467 5113903 : max_val = 0.0f;
1468 1641562863 : for ( i = j = length / 2; i < length; i++ )
1469 : {
1470 1636448960 : if ( ( temp = (float) fabs( input[i] ) ) > max_val )
1471 : {
1472 34018176 : max_val = temp;
1473 34018176 : j = i;
1474 : }
1475 : }
1476 :
1477 5113903 : if ( max_val > 1.0f )
1478 : {
1479 4417230 : scale = 0.67f / max_val;
1480 : }
1481 : else
1482 : {
1483 696673 : scale = 0.67f;
1484 : }
1485 :
1486 : #ifdef NONBE_1328_FIX_NON_LINEARITY
1487 5113903 : sc_factor = 1024.0f;
1488 5113903 : if ( element_mode > EVS_MONO )
1489 : {
1490 4999695 : sc_factor = (float) ( 1 << max( 12 - norm_s( j - length / 2 + 1 ), 0 ) ); /* allowed intra frame jump is smaller */
1491 4999695 : sc_factor = max( sc_factor, 2.0f );
1492 : }
1493 5113903 : if ( *prev_scale <= 0.0 || *prev_scale > sc_factor * scale )
1494 : #else
1495 : if ( *prev_scale <= 0.0 || *prev_scale > 1024.0f * scale )
1496 : #endif
1497 : {
1498 9794 : scale_step = 1.0;
1499 9794 : *prev_scale = scale;
1500 : }
1501 : else
1502 : {
1503 5104109 : scale_step = 1.0f;
1504 5104109 : if ( j != length / 2 )
1505 : {
1506 4476457 : scale_step = (float) exp( 1.0f / (float) ( j - length / 2 ) * (float) log( scale / *prev_scale ) );
1507 : }
1508 : }
1509 :
1510 1641562863 : for ( i = length / 2; i < length; i++ )
1511 : {
1512 1636448960 : if ( input[i] >= 0.0 )
1513 : {
1514 905486756 : *p_out++ = ( input[i] * input[i] ) * *prev_scale;
1515 : }
1516 : else
1517 : {
1518 730962204 : if ( en_abs )
1519 : {
1520 39102 : *p_out++ = 1.0f * ( input[i] * input[i] ) * *prev_scale;
1521 : }
1522 : else
1523 : {
1524 730923102 : *p_out++ = -1.0f * ( input[i] * input[i] ) * *prev_scale;
1525 : }
1526 : }
1527 :
1528 1636448960 : if ( i < j )
1529 : {
1530 675710708 : *prev_scale *= scale_step;
1531 : }
1532 : }
1533 :
1534 : /* update buffer memory */
1535 5113903 : mvr2r( output + L_FRAME32k, old_bwe_exc_extended, NL_BUFF_OFFSET );
1536 :
1537 5113903 : return;
1538 : }
1539 :
1540 :
1541 : /*-------------------------------------------------------------------*
1542 : * create_random_vector()
1543 : *
1544 : * creates random number vector
1545 : * -------------------------------------------------------------------*/
1546 :
1547 6390330 : void create_random_vector(
1548 : float output[], /* o : output random vector */
1549 : const int16_t length, /* i : length of random vector */
1550 : int16_t seed[] /* i/o: start seed */
1551 : )
1552 : {
1553 : int16_t i, j, k;
1554 : float scale1, scale2;
1555 :
1556 6390330 : j = (int16_t) ( own_random( &seed[0] ) * 0.0078f );
1557 6390330 : j = abs( j ) & 0xff;
1558 6390330 : k = (int16_t) ( own_random( &seed[1] ) * 0.0078f );
1559 6390330 : k = abs( k ) & 0xff;
1560 :
1561 6418107 : while ( k == j )
1562 : {
1563 27777 : k = (int16_t) ( own_random( &seed[1] ) * 0.0078f );
1564 27777 : k = abs( k ) & 0xff;
1565 : }
1566 :
1567 6390330 : if ( own_random( &seed[0] ) < 0 )
1568 : {
1569 3163659 : scale1 = -563.154f; /* -200.00f * 0.35f/0.1243f; */
1570 : }
1571 : else
1572 : {
1573 3226671 : scale1 = 563.154f; /* 200.00f * 0.35f/0.1243f; */
1574 : }
1575 :
1576 6390330 : if ( own_random( &seed[1] ) < 0 )
1577 : {
1578 3481107 : scale2 = -225.261f; /* -80.00f * 0.35f/0.1243f; */
1579 : }
1580 : else
1581 : {
1582 2909223 : scale2 = 225.261f; /* 80.00f * 0.35f/0.1243f; */
1583 : }
1584 :
1585 996400250 : for ( i = 0; i < length; i++, j++, k++ )
1586 : {
1587 990009920 : j &= 0xff;
1588 990009920 : k &= 0xff;
1589 990009920 : output[i] = scale1 * gaus_dico_swb[j] + scale2 * gaus_dico_swb[k];
1590 : }
1591 :
1592 6390330 : return;
1593 : }
1594 :
1595 :
1596 : /*-------------------------------------------------------------------*
1597 : * interp_code_5over2()
1598 : *
1599 : * Used to interpolate the excitation from the core sample rate
1600 : * of 12.8 kHz to 32 kHz.
1601 : * Simple linear interpolator - No need for precision here.
1602 : *-------------------------------------------------------------------*/
1603 :
1604 7897004 : void interp_code_5over2(
1605 : const float inp_code[], /* i : input vector */
1606 : float interp_code[], /* o : output vector */
1607 : const int16_t inp_length /* i : length of input vector */
1608 : )
1609 : {
1610 : int16_t i, kk, kkp1;
1611 : const float factor_i[5] = { 0.2f, 0.6f, 1.0f, 0.6f, 0.2f };
1612 : const float factor_j[5] = { 0.8f, 0.4f, 0.0f, 0.4f, 0.8f };
1613 :
1614 7897004 : interp_code[0] = inp_code[0];
1615 7897004 : interp_code[1] = inp_code[0] * factor_i[3] + inp_code[1] * factor_j[3];
1616 7897004 : interp_code[2] = inp_code[0] * factor_i[4] + inp_code[1] * factor_j[4];
1617 :
1618 302828608 : for ( i = 3, kk = 1, kkp1 = 2; i < ( inp_length - 2 ) * HIBND_ACB_L_FAC; i += 5, kk++, kkp1++ )
1619 : {
1620 294931604 : interp_code[i] = inp_code[kk] * factor_j[0] + inp_code[kkp1] * factor_i[0];
1621 294931604 : interp_code[i + 1] = inp_code[kk] * factor_j[1] + inp_code[kkp1] * factor_i[1];
1622 294931604 : interp_code[i + 2] = inp_code[kkp1] * factor_i[2];
1623 294931604 : kk++;
1624 294931604 : kkp1++;
1625 294931604 : interp_code[i + 3] = inp_code[kk] * factor_i[3] + inp_code[kkp1] * factor_j[3];
1626 294931604 : interp_code[i + 4] = inp_code[kk] * factor_i[4] + inp_code[kkp1] * factor_j[4];
1627 : }
1628 :
1629 7897004 : interp_code[i] = inp_code[kk] * factor_j[0];
1630 7897004 : interp_code[i + 1] = inp_code[kk] * factor_j[1];
1631 :
1632 7897004 : return;
1633 : }
1634 :
1635 : /*-------------------------------------------------------------------*
1636 : * interp_code_4over2()
1637 : *
1638 : * Used to interpolate the excitation from the core sample rate
1639 : * of 16 kHz to 32 kHz.
1640 : * Simple linear interpolator - No need for precision here.
1641 : *-------------------------------------------------------------------*/
1642 :
1643 10985247 : void interp_code_4over2(
1644 : const float inp_code[], /* i : input vector */
1645 : float interp_code[], /* o : output vector */
1646 : const int16_t inp_length /* i : length of input vector */
1647 : )
1648 : {
1649 : int16_t i, j;
1650 813164736 : for ( i = j = 0; i < inp_length - 1; i++, j += 2 )
1651 : {
1652 802179489 : interp_code[j] = inp_code[i];
1653 802179489 : interp_code[j + 1] = inp_code[i] * 0.5f + inp_code[i + 1] * 0.5f;
1654 : }
1655 :
1656 10985247 : interp_code[j] = inp_code[i];
1657 10985247 : interp_code[j + 1] = inp_code[i] * 0.5f;
1658 :
1659 10985247 : return;
1660 : }
1661 :
1662 : /*-------------------------------------------------------------------*
1663 : * fb_tbe_reset_synth()
1664 : *
1665 : * Reset the extra parameters needed for synthesis of the FB TBE output
1666 : *-------------------------------------------------------------------*/
1667 :
1668 3122373 : void fb_tbe_reset_synth(
1669 : float fbbwe_hpf_mem[][4],
1670 : float *prev_fbbwe_ratio )
1671 : {
1672 3122373 : set_f( fbbwe_hpf_mem[0], 0, 4 );
1673 3122373 : set_f( fbbwe_hpf_mem[1], 0, 4 );
1674 3122373 : set_f( fbbwe_hpf_mem[2], 0, 4 );
1675 3122373 : set_f( fbbwe_hpf_mem[3], 0, 4 );
1676 3122373 : *prev_fbbwe_ratio = 1.0f;
1677 :
1678 3122373 : return;
1679 : }
1680 :
1681 : /*-------------------------------------------------------------------*
1682 : * wb_tbe_extras_reset()
1683 : *
1684 : * Reset the extra parameters only required for WB TBE encoding
1685 : *-------------------------------------------------------------------*/
1686 :
1687 1683940 : void wb_tbe_extras_reset(
1688 : float mem_genSHBexc_filt_down_wb2[],
1689 : float mem_genSHBexc_filt_down_wb3[] )
1690 : {
1691 1683940 : set_f( mem_genSHBexc_filt_down_wb2, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
1692 1683940 : set_f( mem_genSHBexc_filt_down_wb3, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
1693 :
1694 1683940 : return;
1695 : }
1696 :
1697 : /*-------------------------------------------------------------------*
1698 : * wb_tbe_extras_reset_synth()
1699 : *
1700 : * Reset the extra parameters only required for WB TBE synthesis
1701 : *-------------------------------------------------------------------*/
1702 :
1703 1387041 : void wb_tbe_extras_reset_synth(
1704 : float state_lsyn_filt_shb[],
1705 : float state_lsyn_filt_dwn_shb[],
1706 : float mem_resamp_HB[] )
1707 : {
1708 1387041 : set_f( state_lsyn_filt_shb, 0.0f, 2 * ALLPASSSECTIONS_STEEP );
1709 1387041 : set_f( state_lsyn_filt_dwn_shb, 0.0f, 2 * ALLPASSSECTIONS_STEEP );
1710 1387041 : set_f( mem_resamp_HB, 0.0f, INTERP_3_1_MEM_LEN );
1711 :
1712 1387041 : return;
1713 : }
1714 :
1715 : /*-------------------------------------------------------------------*
1716 : * elliptic_bpf_48k_generic()
1717 : *
1718 : * 18th-order elliptic bandpass filter at 14.0 to 20 kHz sampled at 48 kHz
1719 : * Implemented as 3 fourth order sections cascaded.
1720 : *-------------------------------------------------------------------*/
1721 :
1722 906904 : void elliptic_bpf_48k_generic(
1723 : const float input[], /* i : input signal */
1724 : float output[], /* o : output signal */
1725 : float memory[][4], /* i/o: 4 arrays of 4 for memory */
1726 : const float full_band_bpf[][5] /* i : filter coefficients b0,b1,b2,a0,a1,a2 */
1727 : )
1728 : {
1729 : int16_t i;
1730 : float tmp[L_FRAME48k], tmp2[L_FRAME48k];
1731 :
1732 906904 : 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];
1733 906904 : 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];
1734 906904 : 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];
1735 906904 : 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];
1736 :
1737 867907128 : for ( i = 4; i < L_FRAME48k; i++ )
1738 : {
1739 867000224 : 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];
1740 : }
1741 :
1742 906904 : memory[0][0] = input[L_FRAME48k - 4];
1743 906904 : memory[0][1] = input[L_FRAME48k - 3];
1744 906904 : memory[0][2] = input[L_FRAME48k - 2];
1745 906904 : memory[0][3] = input[L_FRAME48k - 1];
1746 :
1747 906904 : 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];
1748 906904 : 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];
1749 906904 : 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];
1750 906904 : 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];
1751 :
1752 867907128 : for ( i = 4; i < L_FRAME48k; i++ )
1753 : {
1754 867000224 : 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];
1755 : }
1756 :
1757 906904 : memory[1][0] = tmp[L_FRAME48k - 4];
1758 906904 : memory[1][1] = tmp[L_FRAME48k - 3];
1759 906904 : memory[1][2] = tmp[L_FRAME48k - 2];
1760 906904 : memory[1][3] = tmp[L_FRAME48k - 1];
1761 :
1762 906904 : 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];
1763 906904 : 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];
1764 906904 : 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];
1765 906904 : 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];
1766 :
1767 867907128 : for ( i = 4; i < L_FRAME48k; i++ )
1768 : {
1769 867000224 : 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];
1770 : }
1771 :
1772 906904 : memory[2][0] = tmp2[L_FRAME48k - 4];
1773 906904 : memory[2][1] = tmp2[L_FRAME48k - 3];
1774 906904 : memory[2][2] = tmp2[L_FRAME48k - 2];
1775 906904 : memory[2][3] = tmp2[L_FRAME48k - 1];
1776 :
1777 906904 : memory[3][0] = output[L_FRAME48k - 4];
1778 906904 : memory[3][1] = output[L_FRAME48k - 3];
1779 906904 : memory[3][2] = output[L_FRAME48k - 2];
1780 906904 : memory[3][3] = output[L_FRAME48k - 1];
1781 :
1782 906904 : return;
1783 : }
1784 :
1785 :
1786 : /*-------------------------------------------------------------------*
1787 : * synthesise_fb_high_band()
1788 : *
1789 : * Creates the highband output for full band - 14.0 to 20 kHz
1790 : * Using the energy shaped white excitation signal from the SWB BWE.
1791 : * The excitation signal input is sampled at 16kHz and so is upsampled
1792 : * to 48 kHz first.
1793 : * Uses a complementary split filter to code the two regions from
1794 : * 14kHz to 16kHz and 16 kHz to 20 kHz.
1795 : * One of 16 tilt filters is also applied afterwards to further
1796 : * refine the spectral shape of the fullband signal.
1797 : * The tilt is specified in dB per kHz. N.B. Only negative values are
1798 : * accomodated.
1799 : *-------------------------------------------------------------------*/
1800 :
1801 558202 : void synthesise_fb_high_band(
1802 : const float excitation_in[], /* i : full band excitation */
1803 : float output[], /* o : high band speech - 14.0 to 20 kHz */
1804 : const float fb_exc_energy, /* i : full band excitation energy */
1805 : const float ratio, /* i : energy ratio */
1806 : const int16_t L_frame, /* i : ACELP frame length */
1807 : const int16_t bfi, /* i : BFI flag */
1808 : float *prev_fbbwe_ratio, /* o : previous frame energy for FEC */
1809 : float bpf_memory[][4] /* i/o: memory for elliptic bpf 48k */
1810 : )
1811 : {
1812 : int16_t i, j;
1813 : float excitation_in_interp3[L_FRAME48k];
1814 : float tmp[L_FRAME48k];
1815 : float temp1, ratio2;
1816 :
1817 : /* Interpolate the white energy shaped gaussian excitation from 16 kHz to 48 kHz with zeros */
1818 : /* white excitation from DC to 8 kHz resampled to produce DC to 24 kHz excitation. */
1819 179182842 : for ( i = 0, j = 0; i < L_FRAME48k; i += 3, j++ )
1820 : {
1821 178624640 : excitation_in_interp3[i] = 3.0f * excitation_in[j];
1822 178624640 : excitation_in_interp3[i + 1] = 0.0f;
1823 178624640 : excitation_in_interp3[i + 2] = 0.0f;
1824 : }
1825 :
1826 558202 : if ( L_frame == L_FRAME16k )
1827 : {
1828 : /* for 16kHz ACELP core */
1829 478893 : elliptic_bpf_48k_generic( excitation_in_interp3, tmp, bpf_memory, full_band_bpf_3 );
1830 : }
1831 : else
1832 : {
1833 : /* for 12.8kHz ACELP core */
1834 79309 : elliptic_bpf_48k_generic( excitation_in_interp3, tmp, bpf_memory, full_band_bpf_1 );
1835 : }
1836 558202 : temp1 = sum2_f( tmp, L_FRAME48k ) + 0.001f;
1837 558202 : ratio2 = (float) ( ratio * sqrt( fb_exc_energy / temp1 ) );
1838 :
1839 558202 : if ( !bfi )
1840 : {
1841 511735 : *prev_fbbwe_ratio = ratio;
1842 : }
1843 : else
1844 : {
1845 46467 : *prev_fbbwe_ratio = ratio * 0.5f;
1846 : }
1847 536432122 : for ( i = 0; i < L_FRAME48k; i++ )
1848 : {
1849 535873920 : output[i] = tmp[i] * ratio2;
1850 : }
1851 :
1852 558202 : return;
1853 : }
1854 :
1855 : /*-------------------------------------------------------------------*
1856 : * Estimate_mix_factors() *
1857 : * *
1858 : * Estimate mix factors for SHB excitation generation *
1859 : *-------------------------------------------------------------------*/
1860 :
1861 215318 : static void Estimate_mix_factors(
1862 : const float *shb_res, /* i : SHB LP residual */
1863 : const float *exc16kWhtnd, /* i : SHB transformed low band excitation */
1864 : const float *White_exc16k, /* i : Modulated envelope shaped white noise */
1865 : const float pow1, /* i : SHB exc. power for normalization */
1866 : const float pow22, /* i : White noise excitation for normalization*/
1867 : float *vf_modified, /* o : Estimated voice factors */
1868 : int16_t *vf_ind /* o : voice factors VQ index */
1869 : )
1870 : {
1871 : float shb_res_local[L_FRAME16k], WN_exc_local[L_FRAME16k];
1872 : float pow3, temp_p1_p2, temp_p1_p3;
1873 : float temp_numer1[L_FRAME16k], temp_numer2[L_FRAME16k];
1874 : int16_t i, length;
1875 :
1876 215318 : mvr2r( shb_res, shb_res_local, L_FRAME16k );
1877 215318 : mvr2r( White_exc16k, WN_exc_local, L_FRAME16k );
1878 :
1879 215318 : pow3 = dotp( shb_res_local, shb_res_local, L_FRAME16k );
1880 :
1881 215318 : pow3 += 0.00001f;
1882 215318 : temp_p1_p2 = (float) sqrt( pow1 / pow22 );
1883 215318 : temp_p1_p3 = (float) sqrt( pow1 / pow3 );
1884 :
1885 :
1886 69117078 : for ( i = 0; i < L_FRAME16k; i++ )
1887 : {
1888 68901760 : WN_exc_local[i] *= temp_p1_p2;
1889 68901760 : shb_res_local[i] *= temp_p1_p3;
1890 : }
1891 69117078 : for ( i = 0; i < L_FRAME16k; i++ )
1892 : {
1893 68901760 : temp_numer1[i] = shb_res_local[i] - WN_exc_local[i];
1894 68901760 : temp_numer2[i] = exc16kWhtnd[i] - WN_exc_local[i];
1895 : }
1896 :
1897 215318 : length = L_FRAME16k;
1898 430636 : for ( i = 0; i < 1; i++ )
1899 : {
1900 215318 : temp_p1_p2 = dotp( temp_numer1 + i * length, temp_numer2 + i * length, length );
1901 215318 : temp_p1_p3 = dotp( temp_numer2 + i * length, temp_numer2 + i * length, length );
1902 215318 : vf_modified[i] = min( max( ( temp_p1_p2 / temp_p1_p3 ), 0.1f ), 0.99f );
1903 : }
1904 :
1905 215318 : *vf_ind = usquant( vf_modified[0], &temp_p1_p2, 0.125, 0.125, 1 << NUM_BITS_SHB_VF );
1906 215318 : set_f( vf_modified, temp_p1_p2, NB_SUBFR16k );
1907 :
1908 215318 : return;
1909 : }
1910 :
1911 : /*-------------------------------------------------------------------*
1912 : * tbe_celp_exc() *
1913 : * *
1914 : * Prepare adaptive part of TBE excitation *
1915 : *-------------------------------------------------------------------*/
1916 :
1917 15264963 : void tbe_celp_exc(
1918 : const int16_t element_mode, /* i : element mode */
1919 : const int16_t idchan, /* i : channel ID */
1920 : float *bwe_exc, /* i/o: BWE excitation */
1921 : const int16_t L_frame, /* i : frame length */
1922 : const int16_t L_subfr, /* i : subframe length */
1923 : const int16_t i_subfr, /* i : subframe index */
1924 : const int16_t T0, /* i : integer pitch lag */
1925 : const int16_t T0_frac, /* i : fraction of lag */
1926 : float *error, /* i/o: error */
1927 : const int16_t tdm_LRTD_flag /* i : LRTD stereo mode flag */
1928 : )
1929 : {
1930 : int16_t i, offset;
1931 :
1932 15264963 : if ( element_mode == IVAS_CPE_TD && idchan == 1 && !tdm_LRTD_flag )
1933 : {
1934 8892 : return;
1935 : }
1936 :
1937 15256071 : assert( bwe_exc != NULL && "BWE excitation is NULL" );
1938 :
1939 15256071 : if ( L_frame == L_FRAME )
1940 : {
1941 6092596 : offset = tbe_celp_exc_offset( T0, T0_frac );
1942 :
1943 980907956 : for ( i = 0; i < L_subfr * HIBND_ACB_L_FAC; i++ )
1944 : {
1945 974815360 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset + (int16_t) *error];
1946 : }
1947 6092596 : *error += (float) offset - (float) T0 * HIBND_ACB_L_FAC - 0.25f * HIBND_ACB_L_FAC * (float) T0_frac;
1948 : }
1949 : else
1950 : {
1951 9163475 : offset = T0 * 2 + (int16_t) ( (float) T0_frac * 0.5f + 4 + 0.5f ) - 4;
1952 1182088275 : for ( i = 0; i < L_subfr * 2; i++ )
1953 : {
1954 1172924800 : bwe_exc[i + i_subfr * 2] = bwe_exc[i + i_subfr * 2 - offset + (int16_t) *error];
1955 : }
1956 9163475 : *error += (float) offset - (float) T0 * 2 - 0.5f * (float) T0_frac;
1957 : }
1958 :
1959 15256071 : return;
1960 : }
1961 :
1962 : /*-------------------------------------------------------------------*
1963 : * prep_tbe_exc() *
1964 : * *
1965 : * Prepare TBE excitation *
1966 : *-------------------------------------------------------------------*/
1967 :
1968 17286374 : void prep_tbe_exc(
1969 : const int16_t L_frame, /* i : length of the frame */
1970 : const int16_t L_subfr, /* i : subframe length */
1971 : const int16_t i_subfr, /* i : subframe index */
1972 : const float gain_pit, /* i : Pitch gain */
1973 : const float gain_code, /* i : algebraic codebook gain */
1974 : const float code[], /* i : algebraic excitation */
1975 : const float voice_fac, /* i : voicing factor */
1976 : float *voice_factors, /* o : TBE voicing factor */
1977 : float bwe_exc[], /* i/o: excitation for TBE */
1978 : const float gain_preQ, /* i : prequantizer excitation gain*/
1979 : const float code_preQ[], /* i : prequantizer excitation */
1980 : const int16_t T0, /* i : integer pitch variables */
1981 : const int16_t coder_type, /* i : coding type */
1982 : const int32_t core_brate, /* i : core bitrate */
1983 : const int16_t element_mode, /* i : element mode */
1984 : const int16_t idchan, /* i : channel ID */
1985 : const int16_t flag_TD_BWE, /* i : flag indicating whether hTD_BWE exists */
1986 : const int16_t tdm_LRTD_flag /* i : LRTD stereo mode flag */
1987 : )
1988 : {
1989 : int16_t i;
1990 : float tmp_code[2 * L_SUBFR * HIBND_ACB_L_FAC];
1991 : float tmp_code_preInt[L_SUBFR];
1992 17286374 : float tmp = 1.0f;
1993 :
1994 17286374 : *voice_factors = VF_0th_PARAM + VF_1st_PARAM * voice_fac + VF_2nd_PARAM * voice_fac * voice_fac;
1995 :
1996 17286374 : if ( ( coder_type == VOICED || T0 > 115.5f ) && core_brate > ACELP_8k00 )
1997 : {
1998 5318896 : tmp = 1.0f;
1999 5318896 : *voice_factors *= tmp;
2000 : }
2001 :
2002 17286374 : *voice_factors = min( max( 0.000001f, *voice_factors ), 0.999999f );
2003 :
2004 17286374 : if ( element_mode == IVAS_CPE_TD && idchan == 1 && !tdm_LRTD_flag )
2005 : {
2006 8892 : if ( flag_TD_BWE && i_subfr == 0 )
2007 : {
2008 0 : set_f( bwe_exc, 0, L_FRAME32k );
2009 : }
2010 :
2011 8892 : return;
2012 : }
2013 :
2014 17277482 : if ( L_frame == L_FRAME )
2015 : {
2016 6952612 : interp_code_5over2( code, tmp_code, L_subfr );
2017 :
2018 1119370532 : for ( i = 0; i < L_subfr * HIBND_ACB_L_FAC; i++ )
2019 : {
2020 1112417920 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = gain_pit * bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] +
2021 1112417920 : gain_code * tmp_code[i];
2022 : }
2023 : }
2024 : else
2025 : {
2026 671116550 : for ( i = 0; i < L_subfr; i++ )
2027 : {
2028 660791680 : tmp_code_preInt[i] = gain_code * code[i] + 2 * gain_preQ * code_preQ[i];
2029 : }
2030 :
2031 10324870 : interp_code_4over2( tmp_code_preInt, tmp_code, L_subfr );
2032 :
2033 1331908230 : for ( i = 0; i < L_subfr * 2; i++ )
2034 : {
2035 1321583360 : bwe_exc[i + i_subfr * 2] = gain_pit * bwe_exc[i + i_subfr * 2] + tmp_code[i];
2036 : }
2037 : }
2038 :
2039 17277482 : return;
2040 : }
2041 :
2042 :
2043 : /*-------------------------------------------------------------------*
2044 : * get_tbe_bits() *
2045 : * *
2046 : * Determine TBE bit consumption per frame from bitrate *
2047 : *-------------------------------------------------------------------*/
2048 :
2049 58427 : int16_t get_tbe_bits(
2050 : const int32_t total_brate,
2051 : const int16_t bwidth,
2052 : const int16_t rf_mode )
2053 : {
2054 58427 : int16_t i, bits = 0;
2055 :
2056 58427 : if ( rf_mode )
2057 : {
2058 : /* TBE bits for core, primary frame */
2059 8056 : if ( bwidth == WB && total_brate == ACELP_13k20 )
2060 : {
2061 : /* Gain frame: 4, Gain shapes: 0, and LSFs: 2 */
2062 4216 : bits = NUM_BITS_SHB_FrameGain_LBR_WB + NUM_BITS_LBR_WB_LSF;
2063 : }
2064 3840 : else if ( bwidth == SWB && total_brate == ACELP_13k20 )
2065 : {
2066 : /* Gain frame: 5, Gain shapes: 5, and lowrate LSFs: 8 */
2067 3840 : bits = NUM_BITS_SHB_FRAMEGAIN + NUM_BITS_SHB_SUBGAINS + 8;
2068 : }
2069 : }
2070 : else
2071 : {
2072 50371 : if ( bwidth == WB && total_brate == ACELP_9k60 )
2073 : {
2074 5988 : bits = NUM_BITS_LBR_WB_LSF + NUM_BITS_SHB_FrameGain_LBR_WB;
2075 : }
2076 44383 : else if ( bwidth == SWB || bwidth == FB )
2077 : {
2078 40690 : if ( total_brate == ACELP_9k60 )
2079 : {
2080 2884 : bits = NUM_BITS_SHB_FRAMEGAIN + NUM_BITS_SHB_SUBGAINS + 8;
2081 : }
2082 37806 : else if ( total_brate >= ACELP_13k20 && total_brate <= ACELP_32k )
2083 : {
2084 37806 : bits = NUM_BITS_SHB_SUBGAINS + NUM_BITS_SHB_FRAMEGAIN + NUM_LSF_GRID_BITS + MIRROR_POINT_BITS;
2085 :
2086 226836 : for ( i = 0; i < NUM_Q_LSF; i++ )
2087 : {
2088 189030 : bits += lsf_q_num_bits[i];
2089 : }
2090 : }
2091 :
2092 40690 : if ( total_brate >= ACELP_24k40 )
2093 : {
2094 27968 : bits += NUM_BITS_SHB_ENER_SF + NUM_BITS_SHB_VF + NUM_BITS_SHB_RES_GS * NB_SUBFR16k;
2095 : }
2096 :
2097 40690 : if ( bwidth == SWB && ( total_brate == ACELP_16k40 || total_brate == ACELP_24k40 ) )
2098 : {
2099 36366 : bits += BITS_TEC + BITS_TFA;
2100 : }
2101 :
2102 40690 : if ( bwidth == FB )
2103 : {
2104 : /* fullband slope */
2105 1440 : bits += 4;
2106 : }
2107 : }
2108 : }
2109 :
2110 58427 : return bits;
2111 : }
|