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 995079 : 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 995079 : set_f( mem_csfilt, 0, 2 );
80 995079 : set_f( mem_genSHBexc_filt_down_shb, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
81 995079 : set_f( state_lpc_syn, 0.0f, LPC_SHB_ORDER );
82 995079 : set_f( syn_overlap, 0.0f, L_SHB_LAHEAD );
83 995079 : set_f( state_syn_shbexc, 0.0f, L_SHB_LAHEAD );
84 995079 : *tbe_demph = 0.0f;
85 995079 : *tbe_premph = 0.0f;
86 995079 : set_f( mem_stp_swb, 0, LPC_SHB_ORDER );
87 995079 : *gain_prec_swb = 1.0f;
88 :
89 995079 : 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 760671 : void swb_tbe_reset_synth(
100 : float genSHBsynth_Hilbert_Mem[],
101 : float genSHBsynth_state_lsyn_filt_shb_local[] )
102 : {
103 760671 : set_f( genSHBsynth_Hilbert_Mem, 0.0f, HILBERT_MEM_SIZE );
104 760671 : set_f( genSHBsynth_state_lsyn_filt_shb_local, 0.0f, 2 * ALLPASSSECTIONS_STEEP );
105 :
106 760671 : 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 898847 : 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 898847 : 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 898847 : 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 178371 : 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 178371 : period = 17; /* == (int16_t) (32000.0f / 1850.0f + 0.5f); */
159 :
160 178371 : recip_period = 256.0f / (float) period;
161 3210678 : for ( i = 0; i < period; i++ )
162 : {
163 3032307 : k = (int16_t) ( i * recip_period + 0.5f );
164 3032307 : if ( k <= 64 )
165 : {
166 891855 : local_negsin_table[i] = -sincos_t[k];
167 891855 : local_cos_table[i] = sincos_t[64 - k];
168 : }
169 2140452 : else if ( k <= 128 )
170 : {
171 713484 : local_negsin_table[i] = -sincos_t[128 - k];
172 713484 : local_cos_table[i] = -sincos_t[k - 64];
173 : }
174 1426968 : else if ( k <= 192 )
175 : {
176 713484 : local_negsin_table[i] = sincos_t[k - 128];
177 713484 : local_cos_table[i] = -sincos_t[192 - k];
178 : }
179 : else
180 : {
181 713484 : local_negsin_table[i] = sincos_t[256 - k];
182 713484 : local_cos_table[i] = sincos_t[k - 192];
183 : }
184 : }
185 :
186 55161891 : for ( i = 0; i < length; i = i + 2 )
187 : {
188 54983520 : input[i] = -input[i];
189 : }
190 :
191 178371 : mvr2r( input, tmp + HILBERT_ORDER1, length );
192 :
193 178371 : mvr2r( mem1_ext, tmp, HILBERT_ORDER1 );
194 :
195 : /* Hilber transform stage - 0 */
196 178371 : Hilbert_transform( tmp, tmp, tmpi_R, tmpi_I, length, 0 );
197 :
198 178371 : mvr2r( mem2_ext, tmpi2_R, HILBERT_ORDER2 );
199 178371 : mvr2r( mem3_ext, tmpi2_I, HILBERT_ORDER2 );
200 :
201 : /* Hilber transform stage - 1 */
202 178371 : Hilbert_transform( tmpi_R, tmpi_I, tmpi2_R, tmpi2_I, length, 1 );
203 :
204 178371 : mvr2r( tmp + length, mem1_ext, HILBERT_ORDER1 );
205 178371 : mvr2r( mem2_ext + HILBERT_ORDER2, tmp_R, HILBERT_ORDER2 );
206 178371 : mvr2r( mem3_ext + HILBERT_ORDER2, tmp_I, HILBERT_ORDER2 );
207 :
208 : /* Hilber transform stage - 2 */
209 178371 : Hilbert_transform( tmpi2_R, tmpi2_I, tmpi_R, tmpi_I, length, 2 );
210 :
211 178371 : mvr2r( tmpi2_R + length, mem2_ext, HILBERT_ORDER2 );
212 178371 : mvr2r( tmpi2_I + length, mem3_ext, HILBERT_ORDER2 );
213 :
214 : /* Hilber transform stage - 3 */
215 178371 : Hilbert_transform( tmpi_R, tmpi_I, tmp_R, tmp_I, length, 3 );
216 :
217 178371 : mvr2r( tmp_R + length, mem2_ext + HILBERT_ORDER2, HILBERT_ORDER2 );
218 178371 : mvr2r( tmp_I + length, mem3_ext + HILBERT_ORDER2, HILBERT_ORDER2 );
219 :
220 178371 : if ( *phase_state >= period )
221 : {
222 0 : *phase_state = 0;
223 : }
224 :
225 6812769 : for ( i = 0, j = *phase_state; i < length; )
226 : {
227 116601438 : for ( ; ( j < period ) && ( i < length ); j++, i++ )
228 : {
229 109967040 : output[i] = tmp_R[i + HILBERT_ORDER2] * local_cos_table[j] + tmp_I[i + HILBERT_ORDER2] * local_negsin_table[j];
230 : }
231 :
232 6634398 : if ( j >= period )
233 : {
234 6466284 : j = 0;
235 : }
236 : }
237 :
238 178371 : *phase_state = j;
239 :
240 178371 : return;
241 : }
242 :
243 : /*----------------------------------------------
244 : * Hilbert_transform()
245 : *
246 : * Hilbert transform
247 : *------------------------------------------------*/
248 :
249 713484 : 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 713484 : hb_filter_stage = 2 * HB_stage_id;
261 713484 : offset = ( HB_stage_id == 0 ) ? 1 : 0;
262 :
263 713484 : if ( HB_stage_id == 0 || HB_stage_id == 2 )
264 : {
265 220290822 : for ( i = 0; i < length; i++ )
266 : {
267 219934080 : 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 219934080 : 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 356742 : else if ( HB_stage_id == 1 || HB_stage_id == 3 )
273 : {
274 220290822 : for ( i = 0; i < length; i++ )
275 : {
276 219934080 : 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 219934080 : 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 713484 : return;
283 : }
284 :
285 : /*-------------------------------------------------------------------*
286 : * flip_spectrum()
287 : *
288 : *
289 : *-------------------------------------------------------------------*/
290 :
291 194396 : 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 31297756 : for ( i = 0; i < length; i = i + 2 )
300 : {
301 31103360 : output[i] = -input[i];
302 31103360 : output[i + 1] = input[i + 1];
303 : }
304 :
305 194396 : return;
306 : }
307 :
308 : /*-------------------------------------------------------------------*
309 : * flip_spectrum_and_decimby4()
310 : *
311 : *
312 : *-------------------------------------------------------------------*/
313 :
314 69928 : 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 69928 : if ( ramp_flag )
328 : {
329 613 : factor = 4.0f / length;
330 25133 : for ( i = 0; i < length / 4; i = i + 2 )
331 : {
332 24520 : input_change[i] = -input[i] * ( i * factor );
333 24520 : input_change[i + 1] = input[i + 1] * ( ( i + 1.0f ) * factor );
334 : }
335 : }
336 : else
337 : {
338 69315 : i = 0;
339 : }
340 :
341 8428914 : for ( ; i < length; i = i + 2 )
342 : {
343 8358986 : input_change[i] = -input[i];
344 8358986 : input_change[i + 1] = input[i + 1];
345 : }
346 :
347 69928 : Decimate_allpass_steep( input_change, mem1, length, tmp );
348 69928 : Decimate_allpass_steep( tmp, mem2, length / 2, output );
349 :
350 69928 : return;
351 : }
352 :
353 : /*-------------------------------------------------------------------*
354 : * GenShapedWBExcitation()
355 : *
356 : * Synthesize spectrally shaped highband excitation signal for the wideband
357 : *-------------------------------------------------------------------*/
358 :
359 27968 : 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 27968 : float csfilt_num2[1] = { 0.05f };
385 27968 : 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 27968 : float avg_voice_fac = 0.25f * sum_f( voice_factors, NB_SUBFR );
390 :
391 27968 : if ( igf_flag && ( coder_type == VOICED || avg_voice_fac > 0.35f ) )
392 : {
393 0 : csfilt_num2[0] = 0.2f;
394 0 : csfilt_den2[1] = -0.8f;
395 : }
396 27968 : else if ( igf_flag && ( coder_type == UNVOICED || avg_voice_fac < 0.2f ) )
397 : {
398 0 : csfilt_num2[0] = 0.01f;
399 0 : csfilt_den2[1] = -0.99f;
400 : }
401 27968 : set_f( wht_fil_mem, 0, LPC_WHTN_ORDER_WB );
402 :
403 27968 : Decimate_allpass_steep( bwe_exc_extended, mem_genSHBexc_filt_down1, L_FRAME32k, excTmp );
404 :
405 27968 : flip_spectrum_and_decimby4( excTmp, exc4k, L_FRAME16k, mem_genSHBexc_filt_down2, mem_genSHBexc_filt_down3, 0 );
406 :
407 27968 : if ( uv_flag )
408 : {
409 : /* unvoiced signal */
410 1700 : create_random_vector( exc4kWhtnd, L_FRAME16k / 4, bwe_seed );
411 : }
412 : else
413 : {
414 26268 : 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 26268 : R[0] = max( R[0], 1.0e-8f );
418 105072 : for ( i = 0; i <= LPC_WHTN_ORDER_WB; i++ )
419 : {
420 78804 : R[i] = R[i] * wac[i];
421 : }
422 26268 : lev_dur( lpc_whtn, R, LPC_WHTN_ORDER_WB, ervec );
423 :
424 26268 : 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 2127708 : for ( i = 0, pow1 = 0.00001f; i < L_FRAME16k / 4; i++ )
428 : {
429 2101440 : excTmp2[i] = (float) ( fabs( exc4kWhtnd[i] ) );
430 2101440 : pow1 += exc4kWhtnd[i] * exc4kWhtnd[i];
431 : }
432 :
433 2127708 : for ( i = 0; i < L_FRAME16k / 4; i++ )
434 : {
435 2101440 : excNoisyEnv[i] = *mem_csfilt + csfilt_num2[0] * excTmp2[i];
436 2101440 : *mem_csfilt = -csfilt_den2[1] * excNoisyEnv[i];
437 : }
438 :
439 26268 : create_random_vector( exc4k, L_FRAME16k / 4, bwe_seed );
440 :
441 : /* Ensure pow22 is greater than zero when computing normalization */
442 2127708 : for ( i = 0, pow22 = 0.00001f; i < L_FRAME16k / 4; i++ )
443 : {
444 2101440 : exc4k[i] *= excNoisyEnv[i];
445 2101440 : pow22 += exc4k[i] * exc4k[i];
446 : }
447 :
448 26268 : if ( coder_type == UNVOICED || ( igf_flag && avg_voice_fac < 0.2f ) )
449 : {
450 786 : scale = (float) sqrt( pow1 / pow22 );
451 786 : if ( pow22 == 0.f )
452 : {
453 0 : scale = 0;
454 : }
455 :
456 63666 : for ( i = 0; i < L_FRAME16k / 4; i++ )
457 : {
458 62880 : exc4kWhtnd[i] = exc4k[i] * scale;
459 : }
460 : }
461 : else
462 : {
463 127410 : for ( i = 0, k = 0; i < 4; i++ )
464 : {
465 :
466 101928 : if ( igf_flag && coder_type == VOICED )
467 : {
468 0 : tmp_vfac = 2 * voice_factors[i];
469 0 : tmp_vfac = min( 1, tmp_vfac );
470 : }
471 : else
472 : {
473 101928 : tmp_vfac = voice_factors[i];
474 : }
475 :
476 101928 : temp1 = root_a( tmp_vfac );
477 101928 : temp2 = root_a_over_b( pow1 * ( 1.0f - tmp_vfac ), pow22 );
478 :
479 :
480 2140488 : for ( j = 0; j < L_FRAME16k / 16; j++, k++ )
481 : {
482 2038560 : exc4kWhtnd[k] = temp1 * exc4kWhtnd[k] + temp2 * exc4k[k];
483 : }
484 : }
485 : }
486 : }
487 :
488 27968 : syn_filt( lpc_shb, LPC_SHB_ORDER_WB, exc4kWhtnd, excSHB, L_FRAME16k / 4, state_lpc_syn, 1 );
489 :
490 27968 : return;
491 : }
492 :
493 : /*-------------------------------------------------------------------*
494 : * GenWBSynth()
495 : *
496 : * Generate 16 KHz sampled highband component from synthesized highband
497 : *-------------------------------------------------------------------*/
498 :
499 20727 : 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 20727 : Interpolate_allpass_steep( input_synspeech, state_lsyn_filt_shb1, L_FRAME16k / 4, speech_buf_16k1 );
509 :
510 20727 : Interpolate_allpass_steep( speech_buf_16k1, state_lsyn_filt_shb2, L_FRAME16k / 2, speech_buf_16k2 );
511 :
512 20727 : flip_spectrum( speech_buf_16k2, shb_syn_speech_16k, L_FRAME16k );
513 :
514 20727 : return;
515 : }
516 :
517 : /*-------------------------------------------------------------------*
518 : * PostShortTerm()
519 : *
520 : * Short term processing
521 : *-------------------------------------------------------------------*/
522 :
523 1783092 : 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 1783092 : set_f( apond1, 0, LPC_SHB_ORDER + 1 );
541 1783092 : set_f( apond2, 0, LONG_H_ST );
542 1783092 : set_f( sig_ltp, 0, L_SUBFR16k + 1 );
543 :
544 : /* Obtain post-filter weights */
545 1783092 : g1 = GAMMA0 + GAMMA_SHARP * formant_fac;
546 1783092 : g2 = GAMMA0 - GAMMA_SHARP * formant_fac;
547 :
548 : /* Compute weighted LPC coefficients */
549 1783092 : weight_a( lpccoeff, apond1, g1, LPC_SHB_ORDER );
550 1783092 : weight_a( lpccoeff, apond2, g2, LPC_SHB_ORDER );
551 :
552 : /* Compute A(gamma2) residual */
553 1783092 : residu( apond2, LPC_SHB_ORDER, sig_in, sig_ltp + 1, L_SUBFR16k );
554 :
555 : /* Save last output of 1/A(gamma1) */
556 1783092 : sig_ltp[0] = *ptr_mem_stp;
557 :
558 : /* Control short term pst filter gain and compute parcor0 */
559 1783092 : 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 1783092 : 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 1783092 : filt_mu( sig_ltp, sig_out, parcor0, L_SUBFR16k, SWB_TBE );
566 :
567 : /* gain control */
568 1783092 : scale_st( sig_in, sig_out, ptr_gain_prec, L_SUBFR16k, SWB_TBE );
569 :
570 1783092 : 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 462426 : 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 462426 : tmp = 0.5f * (float) fabs( lpc_shb2 ) + 0.5f * *tilt_mem;
591 462426 : *tilt_mem = tmp;
592 :
593 : /* Map to PF strength */
594 462426 : formant_fac = ( tmp - SWB_TILT_LOW ) * SWB_TILT_DELTA;
595 462426 : if ( formant_fac > 1.0f )
596 : {
597 479 : formant_fac = 1.0f;
598 : }
599 461947 : else if ( formant_fac < 0.0f )
600 : {
601 407460 : formant_fac = 0.0f;
602 : }
603 :
604 462426 : formant_fac = 1.0f - 0.5f * formant_fac;
605 :
606 462426 : return formant_fac;
607 : }
608 :
609 12708 : 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 12708 : assert( len > 0 && len <= L_FRAME16k );
621 :
622 12708 : K = (int16_t) ( len_h / 2 ); /* length of FIR filter memory = half of the total filter length */
623 12708 : w = 1.0f / len_h; /* MA filtering coefficient */
624 :
625 : /* copy filter memory to the input buffer */
626 12708 : if ( mem_h != NULL )
627 : {
628 4236 : mvr2r( mem_h, buf_in, K );
629 : }
630 : else
631 : {
632 : /* no memory available, use the first len_h/2 samples as memory */
633 8472 : p_in = buf_in;
634 93192 : for ( k = 0; k < K; k++ )
635 : {
636 84720 : *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 12708 : p_in = &buf_in[K];
643 4079268 : for ( k = 0; k < len; k++ )
644 : {
645 4066560 : *p_in++ = (float) fabs( inp[k] ) * w;
646 : }
647 :
648 : /* update filter memory from the end of the input buffer */
649 12708 : if ( mem_h != NULL )
650 : {
651 4236 : mvr2r( &buf_in[len], mem_h, K );
652 : }
653 :
654 : /* do MA filtering */
655 12708 : out[0] = sum_f( buf_in, len_h );
656 12708 : p_out = &buf_in[0]; /* pointer to leaving sample */
657 12708 : p_in = &buf_in[len_h]; /* pointer to entering sample*/
658 3939480 : for ( k = 1; k < len - K; k++ )
659 : {
660 3926772 : out[k] = out[k - 1] - *p_out++ + *p_in++;
661 : }
662 :
663 : /* use IIR filtering to extrapolate the last K samples */
664 12708 : p_in = &buf_in[len - K];
665 12708 : p_out = &out[len - K];
666 12708 : p_prev = p_out - 1;
667 139788 : for ( k = 0; k < K; k++ )
668 : {
669 127080 : *p_out++ = 0.05f * ( *p_in++ ) + 0.95f * ( *p_prev++ );
670 : }
671 :
672 12708 : return;
673 : }
674 :
675 : /*-------------------------------------------------------------------*
676 : * GenShapedSHBExcitation()
677 : *
678 : * Synthesize spectrally shaped highband excitation signal
679 : *-------------------------------------------------------------------*/
680 :
681 462426 : 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 462426 : float csfilt_num2[1] = { 0.2f };
728 462426 : float csfilt_den2[2] = { 1.0f, -0.8f };
729 : float varEnvShape;
730 : float ervec[LPC_WHTN_ORDER + 2];
731 : float exc16kWhtnd[L_FRAME16k];
732 462426 : 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 462426 : 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 462426 : mix_factor = 0.0f;
752 :
753 462426 : set_f( zero_mem, 0, LPC_SHB_ORDER );
754 462426 : set_f( wht_fil_mem, 0, LPC_WHTN_ORDER );
755 462426 : set_f( EnvWhiteExc16k_4k, 0, L_FRAME4k );
756 462426 : set_f( EnvExc16kWhtnd_4k, 0, L_FRAME4k );
757 :
758 : /* Mirror the spectrum */
759 296415066 : for ( i = 0; i < L_FRAME32k; i++ )
760 : {
761 295952640 : exc32k[i] = ( ( i % 2 ) == 0 ) ? ( -bwe_exc_extended[i] ) : ( bwe_exc_extended[i] );
762 : }
763 :
764 : /* Decimate by 2 */
765 462426 : Decimate_allpass_steep( exc32k, mem_genSHBexc_filt_down_shb, 2 * L_FRAME16k, exc16k );
766 :
767 462426 : 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 462426 : R[0] = max( R[0], 1.0e-8f );
771 2774556 : for ( i = 0; i <= LPC_WHTN_ORDER; i++ )
772 : {
773 2312130 : R[i] = R[i] * wac[i];
774 : }
775 :
776 : /* Ensure R[0] isn't zero when entering Levinson-Durbin */
777 462426 : R[0] += 1.0e-8f;
778 :
779 462426 : lev_dur( lpc_whtn, R, LPC_WHTN_ORDER, ervec );
780 :
781 462426 : fir( exc16k, lpc_whtn, exc16kWhtnd, wht_fil_mem, L_FRAME16k, LPC_WHTN_ORDER, 0 );
782 :
783 462426 : if ( extl_brate >= SWB_TBE_2k8 )
784 : {
785 33397482 : for ( i = 0; i < L_FRAME16k; i++ )
786 : {
787 33293440 : exc16kWhtnd[i] *= shb_res_gshape[(int16_t) ( i / 80 )];
788 : }
789 : }
790 :
791 148438746 : for ( k = 0, pow1 = 0.00001f; k < L_FRAME16k; k++ )
792 : {
793 147976320 : excTmp2[k] = (float) ( fabs( exc16kWhtnd[k] ) );
794 147976320 : pow1 += exc16kWhtnd[k] * exc16kWhtnd[k];
795 : }
796 :
797 462426 : if ( !flag_ACELP16k )
798 : {
799 225316 : varEnvShape = mean( voice_factors, NB_SUBFR );
800 : }
801 : else
802 : {
803 237110 : varEnvShape = mean( voice_factors, NB_SUBFR16k );
804 : }
805 :
806 462426 : if ( extl == FB_TBE )
807 : {
808 173669 : fb_deemph_fac = max( ( 0.68f - (float) pow( varEnvShape, 3 ) ), 0.48f );
809 : }
810 :
811 462426 : varEnvShape = 1.09875f - 0.49875f * varEnvShape;
812 462426 : varEnvShape = min( max( varEnvShape, 0.6f ), 0.999f );
813 462426 : csfilt_num2[0] = 1.0f - varEnvShape;
814 462426 : csfilt_den2[1] = -varEnvShape;
815 :
816 462426 : 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 120 : float tmp_scale = 0;
820 2520 : for ( i = 0; i < L_SUBFR16k / 4; i++ )
821 : {
822 2400 : 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 120 : if ( !( prev_bfi && extl == FB_TBE ) )
828 : {
829 : /* use weak smoothing for 1st frame after switching to make filter recover more quickly */
830 120 : varEnvShape = 0.8f;
831 120 : csfilt_num2[0] = 1.0f - varEnvShape;
832 120 : csfilt_den2[1] = -varEnvShape;
833 : }
834 120 : *mem_csfilt = varEnvShape * ( tmp_scale / ( L_SUBFR16k / 4 ) );
835 : }
836 :
837 462426 : if ( MSFlag > 0 )
838 : {
839 20624 : varEnvShape = 0.995f;
840 20624 : csfilt_num2[0] = 1.0f - varEnvShape;
841 20624 : csfilt_den2[1] = -varEnvShape;
842 : }
843 :
844 462426 : White_exc16k = exc16k;
845 :
846 : /* Track the low band envelope */
847 462426 : if ( element_mode == IVAS_CPE_TD || element_mode == IVAS_CPE_DFT )
848 : {
849 114715 : if ( extl_brate != SWB_TBE_1k10 && extl_brate != SWB_TBE_1k75 )
850 : {
851 98062 : mem_csfilt_left = 0.0f;
852 98062 : mem_csfilt_right = 0.0f;
853 31477902 : for ( k = 0; k < L_FRAME16k; k++ )
854 : {
855 31379840 : excNoisyEnvLeft[k] = mem_csfilt_left + csfilt_num2[0] * excTmp2[k];
856 31379840 : mem_csfilt_left = -csfilt_den2[1] * excNoisyEnvLeft[k];
857 31379840 : excNoisyEnvRight[L_FRAME16k - k - 1] = mem_csfilt_right + csfilt_num2[0] * excTmp2[L_FRAME16k - k - 1];
858 31379840 : mem_csfilt_right = -csfilt_den2[1] * excNoisyEnvRight[L_FRAME16k - k - 1];
859 : }
860 :
861 98062 : alpha = 0.0f;
862 98062 : step = 1.0f / L_FRAME16k;
863 31477902 : for ( k = 0; k < L_FRAME16k; k++ )
864 : {
865 31379840 : excNoisyEnv[k] = alpha * excNoisyEnvLeft[k] + ( 1 - alpha ) * excNoisyEnvRight[k];
866 31379840 : alpha += step;
867 : }
868 : }
869 : }
870 : else
871 : {
872 111615231 : for ( k = 0; k < L_FRAME16k; k++ )
873 : {
874 111267520 : excNoisyEnv[k] = *mem_csfilt + csfilt_num2[0] * excTmp2[k];
875 111267520 : *mem_csfilt = -csfilt_den2[1] * excNoisyEnv[k];
876 : }
877 : }
878 :
879 462426 : if ( extl_brate == SWB_TBE_1k10 || extl_brate == SWB_TBE_1k75 )
880 : {
881 : /* generate gaussian (white) excitation */
882 5345613 : for ( k = 0; k < L_FRAME16k; k++ )
883 : {
884 5328960 : 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 16653 : pow22 = POW_EXC16k_WHTND;
889 16653 : v_multc( White_exc16k, (float) sqrt( pow1 / pow22 ), White_exc16k, L_FRAME16k );
890 : }
891 : else
892 : {
893 445773 : create_random_vector( White_exc16k, L_FRAME, bwe_seed );
894 445773 : create_random_vector( White_exc16k + L_FRAME, L_FRAME16k - L_FRAME, bwe_seed );
895 :
896 143093133 : for ( k = 0, pow22 = 0.00001f; k < L_FRAME16k; k++ )
897 : {
898 142647360 : White_exc16k[k] *= excNoisyEnv[k];
899 142647360 : pow22 += White_exc16k[k] * White_exc16k[k];
900 : }
901 : }
902 :
903 462426 : flag_plosive = 0;
904 :
905 462426 : if ( extl_brate >= SWB_TBE_2k8 || extl_brate == SWB_TBE_1k10 || extl_brate == SWB_TBE_1k75 )
906 : {
907 120695 : if ( *vf_ind == 20 ) /* encoder side */
908 : {
909 30074 : if ( extl_brate == SWB_TBE_1k10 || extl_brate == SWB_TBE_1k75 )
910 : {
911 : /* calculate TD envelopes of exc16kWhtnd and White_exc16k */
912 4236 : find_td_envelope( White_exc16k, L_FRAME16k, 20, NULL, EnvWhiteExc16k );
913 4236 : find_td_envelope( exc16kWhtnd, L_FRAME16k, 20, NULL, EnvExc16kWhtnd );
914 :
915 343116 : for ( k = 0; k < L_FRAME4k; k++ )
916 : {
917 338880 : EnvWhiteExc16k_4k[k] = EnvWhiteExc16k[4 * k];
918 338880 : EnvExc16kWhtnd_4k[k] = EnvExc16kWhtnd[4 * k];
919 : }
920 :
921 : /* calculate the optimal mix factor */
922 4236 : c0 = c1 = c2 = c3 = c4 = c5 = 0.0f;
923 21180 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
924 : {
925 16944 : c0_part[i] = sum2_f( &EnvExc16kWhtnd_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], L_FRAME4k / NUM_SHB_SUBGAINS );
926 16944 : 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 16944 : c2_part[i] = sum2_f( &EnvWhiteExc16k_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], L_FRAME4k / NUM_SHB_SUBGAINS );
928 16944 : 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 16944 : 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 16944 : c5_part[i] = sum2_f( &EnvSHBres_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], L_FRAME4k / NUM_SHB_SUBGAINS );
931 :
932 16944 : c0 += c0_part[i];
933 16944 : c1 += c1_part[i];
934 16944 : c2 += c2_part[i];
935 16944 : c3 += c3_part[i];
936 16944 : c4 += c4_part[i];
937 16944 : c5 += c5_part[i];
938 : }
939 :
940 4236 : den = 4.0f * c0 * c2 - c4 * c4;
941 :
942 4236 : if ( den == 0.0f )
943 : {
944 0 : den = 1e-7f;
945 : }
946 :
947 4236 : g1 = ( c3 * c4 - 2 * c1 * c2 ) / den;
948 4236 : g2 = ( c1 * c4 - 2 * c0 * c3 ) / den;
949 :
950 4236 : *Env_error = 0.0f;
951 4236 : flag_plosive = 0;
952 21180 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
953 : {
954 16944 : 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 16944 : *Env_error += Env_error_part[i];
956 :
957 16944 : if ( Env_error_part[i] > THR_ENV_ERROR_PLOSIVE )
958 : {
959 : /* envelope error is too high -> likely a plosive */
960 34 : flag_plosive = 1;
961 : }
962 : }
963 :
964 4236 : if ( flag_plosive )
965 : {
966 : /* plosive detected -> set the mixing factor to 0 */
967 21 : *vf_ind = 0;
968 21 : mix_factor = 0.0f;
969 : }
970 : else
971 : {
972 : /* normalize gain */
973 4215 : temp = 0.0f;
974 4215 : if ( g1 + g2 == 0.0f )
975 : {
976 0 : temp = 1e-7f;
977 : }
978 :
979 4215 : g = g2 / ( g1 + g2 + temp );
980 :
981 : /* quantization of the mixing factor */
982 4215 : cbsize = 1 << NUM_BITS_SHB_VF;
983 4215 : delta = 1.0f / ( cbsize - 1 );
984 4215 : if ( g > 1.0f )
985 : {
986 750 : g = 1.0f;
987 : }
988 3465 : else if ( g < delta )
989 : {
990 : /* prevent low gains to be quantized to 0 as this is reserved for plosives */
991 12 : g = delta;
992 : }
993 :
994 4215 : *vf_ind = usquant( g, &mix_factor, 0.0f, 1.0f / ( cbsize - 1 ), cbsize );
995 : }
996 : }
997 : else
998 : {
999 25838 : Estimate_mix_factors( shb_res, exc16kWhtnd, White_exc16k, pow1, pow22, voiceFacEst, vf_ind );
1000 25838 : temp = ( voiceFacEst[0] > 0.7f ) ? 1.0f : 0.8f;
1001 : }
1002 : }
1003 : else /* decoder side */
1004 : {
1005 90621 : if ( extl_brate == SWB_TBE_1k10 || extl_brate == SWB_TBE_1k75 )
1006 : {
1007 12417 : if ( *vf_ind == 0 )
1008 : {
1009 81 : mix_factor = 0.0f;
1010 81 : flag_plosive = 1;
1011 : }
1012 : else
1013 : {
1014 12336 : mix_factor = usdequant( *vf_ind, 0.0f, 1.0f / ( ( 1 << NUM_BITS_SHB_VF ) - 1 ) );
1015 : }
1016 : }
1017 : else
1018 : {
1019 78204 : temp = ( ( *vf_ind * 0.125f ) > 0.7f ) ? 1.0f : 0.8f;
1020 : }
1021 : }
1022 :
1023 120695 : if ( extl_brate != SWB_TBE_1k10 && extl_brate != SWB_TBE_1k75 )
1024 : {
1025 624252 : for ( i = 0; i < NB_SUBFR16k; i++ )
1026 : {
1027 520210 : voice_factors[i] *= temp;
1028 : }
1029 : }
1030 : }
1031 :
1032 462426 : if ( element_mode >= IVAS_CPE_DFT && nlExc16k != NULL )
1033 : {
1034 : /* save buffers for IC-BWE */
1035 73395 : mvr2r( exc16kWhtnd, nlExc16k, L_FRAME16k );
1036 73395 : v_multc( White_exc16k, (float) sqrt( pow1 / pow22 ), mixExc16k, L_FRAME16k );
1037 : }
1038 :
1039 462426 : mvr2r( White_exc16k, White_exc16k_FB, L_FRAME16k );
1040 462426 : deemph( White_exc16k, PREEMPH_FAC, L_FRAME16k, tbe_demph );
1041 :
1042 462426 : if ( extl_brate == SWB_TBE_1k10 || extl_brate == SWB_TBE_1k75 )
1043 : {
1044 16653 : 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 16551 : old_scale = (float) sqrt( *prev_pow_exc16kWhtnd / pow1 );
1048 16551 : new_scale = 1.0f;
1049 16551 : step_scale = ( new_scale - old_scale ) / ( L_FRAME16k / 2 );
1050 16551 : scale = old_scale;
1051 :
1052 : /* interpolate between the old and the new value of the mixing factor */
1053 16551 : old_fact = *prev_mix_factor;
1054 16551 : new_fact = mix_factor;
1055 16551 : step = ( new_fact - old_fact ) / ( L_FRAME16k / 2 );
1056 16551 : fact = old_fact;
1057 :
1058 : /* mixing of LB and gaussian excitation in the first half of the frame */
1059 2664711 : for ( k = 0; k < L_FRAME16k / 2; k++ )
1060 : {
1061 2648160 : exc16kWhtnd[k] = (float) fact * ( White_exc16k[k] * scale ) + (float) ( 1 - fact ) * exc16kWhtnd[k];
1062 2648160 : fact += step;
1063 2648160 : scale += step_scale;
1064 : }
1065 :
1066 : /* mixing of LB and gaussian excitation in the second half of the frame */
1067 2664711 : for ( ; k < L_FRAME16k; k++ )
1068 : {
1069 2648160 : exc16kWhtnd[k] = (float) new_fact * White_exc16k[k] + (float) ( 1 - new_fact ) * exc16kWhtnd[k];
1070 : }
1071 : }
1072 16653 : preemph( exc16kWhtnd, PREEMPH_FAC, L_FRAME16k, tbe_premph );
1073 : }
1074 : else
1075 : {
1076 445773 : if ( coder_type == UNVOICED || MSFlag == 1 )
1077 : {
1078 27288 : scale = (float) sqrt( pow1 / pow22 );
1079 27288 : if ( pow22 == 0.f )
1080 : {
1081 0 : scale = 0;
1082 : }
1083 8759448 : for ( k = 0; k < L_FRAME16k; k++ )
1084 : {
1085 8732160 : exc16kWhtnd[k] = White_exc16k[k] * scale;
1086 : }
1087 :
1088 27288 : preemph( exc16kWhtnd, PREEMPH_FAC, L_FRAME16k, tbe_premph );
1089 : }
1090 : else
1091 : {
1092 418485 : 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 2196327 : for ( i = 0, k = 0; i < nbSubFr; i++ )
1095 : {
1096 1777842 : if ( coder_type == VOICED && extl_brate < SWB_TBE_2k8 )
1097 : {
1098 115896 : temp = (float) sqrt( voice_factors[i] );
1099 115896 : temp1 = (float) sqrt( temp );
1100 115896 : temp2 = (float) sqrt( ( pow1 * ( 1.0f - temp ) ) / pow22 );
1101 115896 : if ( pow22 == 0.f )
1102 : {
1103 0 : temp2 = 0;
1104 : }
1105 : }
1106 : else
1107 : {
1108 : /* Adjust noise mixing for formant sharpening filter */
1109 1661946 : vf_tmp = SWB_NOISE_MIX_FAC * formant_fac;
1110 1661946 : vf_tmp = voice_factors[i] * ( 1.0f - vf_tmp );
1111 :
1112 1661946 : temp1 = (float) sqrt( vf_tmp );
1113 1661946 : temp2 = (float) sqrt( ( pow1 * ( 1.0f - vf_tmp ) ) / pow22 );
1114 1661946 : if ( pow22 == 0.f )
1115 : {
1116 0 : temp2 = 0;
1117 : }
1118 : }
1119 :
1120 135693042 : for ( j = 0; j < L_FRAME16k / nbSubFr; j++, k++ )
1121 : {
1122 133915200 : exc16kWhtnd[k] = temp1 * exc16kWhtnd[k] + temp2 * White_exc16k[k];
1123 : }
1124 :
1125 1777842 : temp = (float) sqrt( 1.0f - voice_factors[i] );
1126 1777842 : temp = PREEMPH_FAC * temp / ( temp1 + temp );
1127 :
1128 1777842 : preemph( &exc16kWhtnd[i * L_FRAME16k / nbSubFr], temp, L_FRAME16k / nbSubFr, tbe_premph );
1129 : }
1130 : }
1131 : }
1132 :
1133 462426 : if ( extl_brate < SWB_TBE_2k8 )
1134 : {
1135 358384 : syn_filt( lpc_shb, LPC_SHB_ORDER, exc16kWhtnd, excSHB, L_FRAME16k, state_lpc_syn, 1 );
1136 : }
1137 : else
1138 : {
1139 104042 : set_f( zero_mem, 0, LPC_SHB_ORDER );
1140 104042 : syn_filt( lpc_shb_sf, LPC_SHB_ORDER, exc16kWhtnd, tempSHB, 80, zero_mem, 1 );
1141 104042 : syn_shb_ener_sf[0] = 0.125f * sum2_f( tempSHB, 80 );
1142 104042 : syn_filt( lpc_shb_sf + ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 80, tempSHB, 80, zero_mem, 1 );
1143 104042 : syn_shb_ener_sf[1] = 0.125f * sum2_f( tempSHB, 80 );
1144 104042 : syn_filt( lpc_shb_sf + 2 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 160, tempSHB, 80, zero_mem, 1 );
1145 104042 : syn_shb_ener_sf[2] = 0.125f * sum2_f( tempSHB, 80 );
1146 104042 : syn_filt( lpc_shb_sf + 3 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 240, tempSHB, 80, zero_mem, 1 );
1147 104042 : syn_shb_ener_sf[3] = 0.125f * sum2_f( tempSHB, 80 );
1148 :
1149 104042 : if ( total_brate <= MAX_ACELP_BRATE )
1150 : {
1151 104042 : 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 104042 : tempD = sqrt( tempSHB[0] );
1153 :
1154 33397482 : for ( i = 0; i < L_FRAME16k; i++ )
1155 : {
1156 33293440 : exc16kWhtnd[i] = (float) ( exc16kWhtnd[i] * tempD );
1157 : }
1158 : }
1159 :
1160 104042 : syn_filt( lpc_shb_sf, LPC_SHB_ORDER, exc16kWhtnd, excSHB, 80, state_lpc_syn, 1 );
1161 104042 : syn_filt( lpc_shb_sf + ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 80, excSHB + 80, 80, state_lpc_syn, 1 );
1162 104042 : syn_filt( lpc_shb_sf + 2 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 160, excSHB + 160, 80, state_lpc_syn, 1 );
1163 104042 : 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 462426 : if ( extl == FB_TBE )
1167 : {
1168 173669 : syn_filt( lpc_shb, LPC_SHB_ORDER, White_exc16k_FB, White_exc16k_FB_temp, L_FRAME16k, fb_state_lpc_syn, 1 );
1169 :
1170 55747749 : for ( i = 0; i < L_FRAME16k; i++ )
1171 : {
1172 55574080 : White_exc16k_FB_temp[i] *= cos_fb_exc[i % 32];
1173 : }
1174 :
1175 173669 : flip_spectrum( White_exc16k_FB_temp, White_exc16k_FB, L_FRAME16k );
1176 :
1177 173669 : deemph( White_exc16k_FB, fb_deemph_fac, L_FRAME16k, fb_tbe_demph );
1178 : }
1179 : else
1180 : {
1181 92690997 : for ( i = 0; i < L_FRAME16k; i++ )
1182 : {
1183 92402240 : White_exc16k_FB[i] = 0.0f;
1184 : }
1185 : }
1186 :
1187 462426 : *prev_pow_exc16kWhtnd = pow1;
1188 462426 : *prev_mix_factor = mix_factor;
1189 :
1190 462426 : return;
1191 : }
1192 :
1193 : /*-------------------------------------------------------------------*
1194 : * GenSHBSynth()
1195 : *
1196 : * Generate 32 KHz sampled highband component from synthesized highband
1197 : *-------------------------------------------------------------------*/
1198 :
1199 376860 : 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 376860 : Interpolate_allpass_steep( input_synspeech, state_lsyn_filt_shb_local, L_FRAME16k, speech_buf_32k );
1211 :
1212 376860 : if ( L_frame == L_FRAME )
1213 : {
1214 171387 : 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 131708193 : for ( i = 0; i < L_FRAME32k; i++ )
1219 : {
1220 131502720 : shb_syn_speech_32k[i] = ( ( i % 2 ) == 0 ) ? ( -speech_buf_32k[i] ) : ( speech_buf_32k[i] );
1221 : }
1222 : }
1223 :
1224 376860 : return;
1225 : }
1226 :
1227 : /*-------------------------------------------------------------------*
1228 : * ScaleShapedSHB()
1229 : *
1230 : *
1231 : *-------------------------------------------------------------------*/
1232 :
1233 399437 : 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 399437 : l_frame = L_FRAME16k;
1250 399437 : l_shb_lahead = L_SHB_LAHEAD;
1251 399437 : skip = skip_bands_SWB_TBE;
1252 :
1253 399437 : if ( length == SHB_OVERLAP_LEN / 2 )
1254 : {
1255 27968 : skip = skip_bands_WB_TBE;
1256 27968 : l_frame = L_FRAME16k / 4;
1257 27968 : l_shb_lahead = L_SHB_LAHEAD / 4;
1258 : }
1259 :
1260 : /* apply gain for each subframe, and store noise output signal using overlap-add */
1261 399437 : set_f( mod_syn, 0, l_frame + l_shb_lahead );
1262 :
1263 399437 : if ( length == SHB_OVERLAP_LEN / 2 )
1264 : {
1265 27968 : sum_gain = 0;
1266 167808 : for ( k = 0; k < length / 2; k++ )
1267 : {
1268 139840 : sum_gain = subwin[2 * k + 2] * subgain[0];
1269 139840 : mod_syn[skip[0] + k] = synSHB[skip[0] + k] * sum_gain;
1270 139840 : mod_syn[skip[0] + k + length / 2] = synSHB[skip[0] + k + length / 2] * subgain[0];
1271 : }
1272 223744 : for ( i = 1; i < NUM_SHB_SUBFR / 2; i++ )
1273 : {
1274 2153536 : for ( k = 0; k < length; k++ )
1275 : {
1276 1957760 : sum_gain = subwin[k + 1] * subgain[i] + subwin[length - k - 1] * subgain[i - 1];
1277 1957760 : mod_syn[skip[i] + k] = synSHB[skip[i] + k] * sum_gain;
1278 : }
1279 : }
1280 167808 : for ( k = 0; k < length / 2; k++ )
1281 : {
1282 139840 : sum_gain = subwin[length - 2 * k - 2] * subgain[i - 1];
1283 139840 : mod_syn[skip[i] + k] = synSHB[skip[i] + k] * sum_gain;
1284 : }
1285 : }
1286 : else
1287 : {
1288 371469 : num_join = NUM_SHB_SUBFR / NUM_SHB_SUBGAINS;
1289 371469 : join_length = num_join * length;
1290 7800849 : for ( k = 0, j = 0; k < length; k++ )
1291 : {
1292 7429380 : mod_syn[j] = synSHB[j] * subwin[k + 1] * subgain[0];
1293 7429380 : j++;
1294 : }
1295 1485876 : for ( i = 0; i < NUM_SHB_SUBGAINS - 1; i++ )
1296 : {
1297 67978827 : for ( k = 0; k < join_length - length; k++ )
1298 : {
1299 66864420 : mod_syn[j] = synSHB[j] * subgain[i * num_join];
1300 66864420 : j++;
1301 : }
1302 :
1303 23402547 : for ( k = 0; k < length; k++ )
1304 : {
1305 22288140 : mod_syn[j] = synSHB[j] * ( subwin[length - k - 1] * subgain[i * num_join] + subwin[k + 1] * subgain[( i + 1 ) * num_join] );
1306 22288140 : j++;
1307 : }
1308 : }
1309 22659609 : for ( k = 0; k < join_length - length; k++ )
1310 : {
1311 22288140 : mod_syn[j] = synSHB[j] * subgain[( NUM_SHB_SUBGAINS - 1 ) * num_join];
1312 22288140 : j++;
1313 : }
1314 7800849 : for ( k = 0; k < length; k++ )
1315 : {
1316 7429380 : mod_syn[j] = synSHB[j] * subwin[length - k - 1] * subgain[( NUM_SHB_SUBGAINS - 1 ) * num_join];
1317 7429380 : j++;
1318 : }
1319 : }
1320 :
1321 7968657 : for ( i = 0; i < l_shb_lahead; i++ )
1322 : {
1323 7569220 : synSHB[i] = mod_syn[i] * win[i] * frame_gain;
1324 7569220 : synSHB[i] += overlap[i];
1325 7569220 : synSHB[i + l_shb_lahead] = mod_syn[i] * frame_gain;
1326 : }
1327 :
1328 113937737 : for ( ; i < l_frame; i++ )
1329 : {
1330 113538300 : synSHB[i] = mod_syn[i] * frame_gain;
1331 : }
1332 :
1333 7968657 : for ( ; i < l_frame + l_shb_lahead; i++ )
1334 : {
1335 7569220 : overlap[i - l_frame] = mod_syn[i] * win[l_frame + l_shb_lahead - 1 - i] * frame_gain;
1336 : }
1337 :
1338 399437 : return;
1339 : }
1340 :
1341 : /*-------------------------------------------------------------------*
1342 : * non_linearity()
1343 : *
1344 : * Apply a non linearity to the SHB excitation
1345 : * -------------------------------------------------------------------*/
1346 :
1347 683802 : 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 : )
1357 : {
1358 : int16_t i, j;
1359 :
1360 683802 : float max_val = 0.0;
1361 : float scale, temp;
1362 : float scale_step;
1363 : float *p_out;
1364 :
1365 683802 : int16_t en_abs = 0;
1366 683802 : float v_fac = 0, ths;
1367 : int16_t nframes;
1368 :
1369 683802 : if ( L_frame == L_FRAME16k )
1370 : {
1371 322019 : nframes = 5;
1372 322019 : ths = 0.87f;
1373 : }
1374 : else
1375 : {
1376 361783 : nframes = 4;
1377 361783 : ths = 0.94f;
1378 : }
1379 :
1380 3741029 : for ( i = 0; i < nframes; i++ )
1381 : {
1382 3057227 : v_fac += voice_factors[i];
1383 : }
1384 683802 : v_fac /= nframes;
1385 :
1386 683802 : if ( coder_type == VOICED && v_fac > ths )
1387 : {
1388 16 : en_abs = 1;
1389 : }
1390 :
1391 683802 : p_out = output + NL_BUFF_OFFSET; /* NL_BUFF_OFFSET = 12 */
1392 : /* update buffer memory */
1393 683802 : mvr2r( old_bwe_exc_extended, output, NL_BUFF_OFFSET );
1394 :
1395 219500442 : for ( i = j = 0; i < length / 2; i++ )
1396 : {
1397 218816640 : if ( ( temp = (float) fabs( input[i] ) ) > max_val )
1398 : {
1399 4878969 : max_val = temp;
1400 4878969 : j = i;
1401 : }
1402 : }
1403 :
1404 683802 : if ( max_val > 1.0f )
1405 : {
1406 641150 : scale = 0.67f / max_val;
1407 : }
1408 : else
1409 : {
1410 42652 : scale = 0.67f;
1411 : }
1412 :
1413 :
1414 683802 : if ( *prev_scale <= 0.0 || *prev_scale > 1024.0f * scale )
1415 : {
1416 106446 : scale_step = 1.0;
1417 106446 : *prev_scale = scale;
1418 : }
1419 : else
1420 : {
1421 577356 : scale_step = 1.0f;
1422 577356 : if ( j != 0 )
1423 : {
1424 540951 : scale_step = (float) exp( 1.0f / (float) j * (float) log( scale / *prev_scale ) );
1425 : }
1426 : }
1427 :
1428 219500442 : for ( i = 0; i < length / 2; i++ )
1429 : {
1430 218816640 : if ( input[i] >= 0.0 )
1431 : {
1432 114979179 : *p_out++ = ( input[i] * input[i] ) * *prev_scale;
1433 : }
1434 : else
1435 : {
1436 103837461 : if ( en_abs )
1437 : {
1438 2208 : *p_out++ = 1.0f * ( input[i] * input[i] ) * *prev_scale;
1439 : }
1440 : else
1441 : {
1442 103835253 : *p_out++ = -1.0f * ( input[i] * input[i] ) * *prev_scale;
1443 : }
1444 : }
1445 :
1446 218816640 : if ( i < j )
1447 : {
1448 103431497 : *prev_scale *= scale_step;
1449 : }
1450 : }
1451 :
1452 683802 : max_val = 0.0f;
1453 219500442 : for ( i = j = length / 2; i < length; i++ )
1454 : {
1455 218816640 : if ( ( temp = (float) fabs( input[i] ) ) > max_val )
1456 : {
1457 4885367 : max_val = temp;
1458 4885367 : j = i;
1459 : }
1460 : }
1461 :
1462 683802 : if ( max_val > 1.0f )
1463 : {
1464 640527 : scale = 0.67f / max_val;
1465 : }
1466 : else
1467 : {
1468 43275 : scale = 0.67f;
1469 : }
1470 :
1471 :
1472 683802 : if ( *prev_scale <= 0.0 || *prev_scale > 1024.0f * scale )
1473 : {
1474 0 : scale_step = 1.0;
1475 0 : *prev_scale = scale;
1476 : }
1477 : else
1478 : {
1479 683802 : scale_step = 1.0f;
1480 683802 : if ( j != length / 2 )
1481 : {
1482 644848 : scale_step = (float) exp( 1.0f / (float) ( j - length / 2 ) * (float) log( scale / *prev_scale ) );
1483 : }
1484 : }
1485 :
1486 219500442 : for ( i = length / 2; i < length; i++ )
1487 : {
1488 218816640 : if ( input[i] >= 0.0 )
1489 : {
1490 114395793 : *p_out++ = ( input[i] * input[i] ) * *prev_scale;
1491 : }
1492 : else
1493 : {
1494 104420847 : if ( en_abs )
1495 : {
1496 2400 : *p_out++ = 1.0f * ( input[i] * input[i] ) * *prev_scale;
1497 : }
1498 : else
1499 : {
1500 104418447 : *p_out++ = -1.0f * ( input[i] * input[i] ) * *prev_scale;
1501 : }
1502 : }
1503 :
1504 218816640 : if ( i < j )
1505 : {
1506 97966790 : *prev_scale *= scale_step;
1507 : }
1508 : }
1509 :
1510 : /* update buffer memory */
1511 683802 : mvr2r( output + L_FRAME32k, old_bwe_exc_extended, NL_BUFF_OFFSET );
1512 :
1513 683802 : return;
1514 : }
1515 :
1516 :
1517 : /*-------------------------------------------------------------------*
1518 : * create_random_vector()
1519 : *
1520 : * creates random number vector
1521 : * -------------------------------------------------------------------*/
1522 :
1523 919514 : void create_random_vector(
1524 : float output[], /* o : output random vector */
1525 : const int16_t length, /* i : length of random vector */
1526 : int16_t seed[] /* i/o: start seed */
1527 : )
1528 : {
1529 : int16_t i, j, k;
1530 : float scale1, scale2;
1531 :
1532 919514 : j = (int16_t) ( own_random( &seed[0] ) * 0.0078f );
1533 919514 : j = abs( j ) & 0xff;
1534 919514 : k = (int16_t) ( own_random( &seed[1] ) * 0.0078f );
1535 919514 : k = abs( k ) & 0xff;
1536 :
1537 922653 : while ( k == j )
1538 : {
1539 3139 : k = (int16_t) ( own_random( &seed[1] ) * 0.0078f );
1540 3139 : k = abs( k ) & 0xff;
1541 : }
1542 :
1543 919514 : if ( own_random( &seed[0] ) < 0 )
1544 : {
1545 457769 : scale1 = -563.154f; /* -200.00f * 0.35f/0.1243f; */
1546 : }
1547 : else
1548 : {
1549 461745 : scale1 = 563.154f; /* 200.00f * 0.35f/0.1243f; */
1550 : }
1551 :
1552 919514 : if ( own_random( &seed[1] ) < 0 )
1553 : {
1554 477303 : scale2 = -225.261f; /* -80.00f * 0.35f/0.1243f; */
1555 : }
1556 : else
1557 : {
1558 442211 : scale2 = 225.261f; /* 80.00f * 0.35f/0.1243f; */
1559 : }
1560 :
1561 145804314 : for ( i = 0; i < length; i++, j++, k++ )
1562 : {
1563 144884800 : j &= 0xff;
1564 144884800 : k &= 0xff;
1565 144884800 : output[i] = scale1 * gaus_dico_swb[j] + scale2 * gaus_dico_swb[k];
1566 : }
1567 :
1568 919514 : return;
1569 : }
1570 :
1571 :
1572 : /*-------------------------------------------------------------------*
1573 : * interp_code_5over2()
1574 : *
1575 : * Used to interpolate the excitation from the core sample rate
1576 : * of 12.8 kHz to 32 kHz.
1577 : * Simple linear interpolator - No need for precision here.
1578 : *-------------------------------------------------------------------*/
1579 :
1580 1109978 : void interp_code_5over2(
1581 : const float inp_code[], /* i : input vector */
1582 : float interp_code[], /* o : output vector */
1583 : const int16_t inp_length /* i : length of input vector */
1584 : )
1585 : {
1586 : int16_t i, kk, kkp1;
1587 : const float factor_i[5] = { 0.2f, 0.6f, 1.0f, 0.6f, 0.2f };
1588 : const float factor_j[5] = { 0.8f, 0.4f, 0.0f, 0.4f, 0.8f };
1589 :
1590 1109978 : interp_code[0] = inp_code[0];
1591 1109978 : interp_code[1] = inp_code[0] * factor_i[3] + inp_code[1] * factor_j[3];
1592 1109978 : interp_code[2] = inp_code[0] * factor_i[4] + inp_code[1] * factor_j[4];
1593 :
1594 43894528 : for ( i = 3, kk = 1, kkp1 = 2; i < ( inp_length - 2 ) * HIBND_ACB_L_FAC; i += 5, kk++, kkp1++ )
1595 : {
1596 42784550 : interp_code[i] = inp_code[kk] * factor_j[0] + inp_code[kkp1] * factor_i[0];
1597 42784550 : interp_code[i + 1] = inp_code[kk] * factor_j[1] + inp_code[kkp1] * factor_i[1];
1598 42784550 : interp_code[i + 2] = inp_code[kkp1] * factor_i[2];
1599 42784550 : kk++;
1600 42784550 : kkp1++;
1601 42784550 : interp_code[i + 3] = inp_code[kk] * factor_i[3] + inp_code[kkp1] * factor_j[3];
1602 42784550 : interp_code[i + 4] = inp_code[kk] * factor_i[4] + inp_code[kkp1] * factor_j[4];
1603 : }
1604 :
1605 1109978 : interp_code[i] = inp_code[kk] * factor_j[0];
1606 1109978 : interp_code[i + 1] = inp_code[kk] * factor_j[1];
1607 :
1608 1109978 : return;
1609 : }
1610 :
1611 : /*-------------------------------------------------------------------*
1612 : * interp_code_4over2()
1613 : *
1614 : * Used to interpolate the excitation from the core sample rate
1615 : * of 16 kHz to 32 kHz.
1616 : * Simple linear interpolator - No need for precision here.
1617 : *-------------------------------------------------------------------*/
1618 :
1619 1480396 : void interp_code_4over2(
1620 : const float inp_code[], /* i : input vector */
1621 : float interp_code[], /* o : output vector */
1622 : const int16_t inp_length /* i : length of input vector */
1623 : )
1624 : {
1625 : int16_t i, j;
1626 107309056 : for ( i = j = 0; i < inp_length - 1; i++, j += 2 )
1627 : {
1628 105828660 : interp_code[j] = inp_code[i];
1629 105828660 : interp_code[j + 1] = inp_code[i] * 0.5f + inp_code[i + 1] * 0.5f;
1630 : }
1631 :
1632 1480396 : interp_code[j] = inp_code[i];
1633 1480396 : interp_code[j + 1] = inp_code[i] * 0.5f;
1634 :
1635 1480396 : return;
1636 : }
1637 :
1638 : /*-------------------------------------------------------------------*
1639 : * fb_tbe_reset_synth()
1640 : *
1641 : * Reset the extra parameters needed for synthesis of the FB TBE output
1642 : *-------------------------------------------------------------------*/
1643 :
1644 499110 : void fb_tbe_reset_synth(
1645 : float fbbwe_hpf_mem[][4],
1646 : float *prev_fbbwe_ratio )
1647 : {
1648 499110 : set_f( fbbwe_hpf_mem[0], 0, 4 );
1649 499110 : set_f( fbbwe_hpf_mem[1], 0, 4 );
1650 499110 : set_f( fbbwe_hpf_mem[2], 0, 4 );
1651 499110 : set_f( fbbwe_hpf_mem[3], 0, 4 );
1652 499110 : *prev_fbbwe_ratio = 1.0f;
1653 :
1654 499110 : return;
1655 : }
1656 :
1657 : /*-------------------------------------------------------------------*
1658 : * wb_tbe_extras_reset()
1659 : *
1660 : * Reset the extra parameters only required for WB TBE encoding
1661 : *-------------------------------------------------------------------*/
1662 :
1663 40373 : void wb_tbe_extras_reset(
1664 : float mem_genSHBexc_filt_down_wb2[],
1665 : float mem_genSHBexc_filt_down_wb3[] )
1666 : {
1667 40373 : set_f( mem_genSHBexc_filt_down_wb2, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
1668 40373 : set_f( mem_genSHBexc_filt_down_wb3, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
1669 :
1670 40373 : return;
1671 : }
1672 :
1673 : /*-------------------------------------------------------------------*
1674 : * wb_tbe_extras_reset_synth()
1675 : *
1676 : * Reset the extra parameters only required for WB TBE synthesis
1677 : *-------------------------------------------------------------------*/
1678 :
1679 29805 : void wb_tbe_extras_reset_synth(
1680 : float state_lsyn_filt_shb[],
1681 : float state_lsyn_filt_dwn_shb[],
1682 : float mem_resamp_HB[] )
1683 : {
1684 29805 : set_f( state_lsyn_filt_shb, 0.0f, 2 * ALLPASSSECTIONS_STEEP );
1685 29805 : set_f( state_lsyn_filt_dwn_shb, 0.0f, 2 * ALLPASSSECTIONS_STEEP );
1686 29805 : set_f( mem_resamp_HB, 0.0f, INTERP_3_1_MEM_LEN );
1687 :
1688 29805 : return;
1689 : }
1690 :
1691 : /*-------------------------------------------------------------------*
1692 : * elliptic_bpf_48k_generic()
1693 : *
1694 : * 18th-order elliptic bandpass filter at 14.0 to 20 kHz sampled at 48 kHz
1695 : * Implemented as 3 fourth order sections cascaded.
1696 : *-------------------------------------------------------------------*/
1697 :
1698 157274 : void elliptic_bpf_48k_generic(
1699 : const float input[], /* i : input signal */
1700 : float output[], /* o : output signal */
1701 : float memory[][4], /* i/o: 4 arrays of 4 for memory */
1702 : const float full_band_bpf[][5] /* i : filter coefficients b0,b1,b2,a0,a1,a2 */
1703 : )
1704 : {
1705 : int16_t i;
1706 : float tmp[L_FRAME48k], tmp2[L_FRAME48k];
1707 :
1708 157274 : 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];
1709 157274 : 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];
1710 157274 : 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];
1711 157274 : 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];
1712 :
1713 150511218 : for ( i = 4; i < L_FRAME48k; i++ )
1714 : {
1715 150353944 : 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];
1716 : }
1717 :
1718 157274 : memory[0][0] = input[L_FRAME48k - 4];
1719 157274 : memory[0][1] = input[L_FRAME48k - 3];
1720 157274 : memory[0][2] = input[L_FRAME48k - 2];
1721 157274 : memory[0][3] = input[L_FRAME48k - 1];
1722 :
1723 157274 : 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];
1724 157274 : 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];
1725 157274 : 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];
1726 157274 : 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];
1727 :
1728 150511218 : for ( i = 4; i < L_FRAME48k; i++ )
1729 : {
1730 150353944 : 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];
1731 : }
1732 :
1733 157274 : memory[1][0] = tmp[L_FRAME48k - 4];
1734 157274 : memory[1][1] = tmp[L_FRAME48k - 3];
1735 157274 : memory[1][2] = tmp[L_FRAME48k - 2];
1736 157274 : memory[1][3] = tmp[L_FRAME48k - 1];
1737 :
1738 157274 : 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];
1739 157274 : 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];
1740 157274 : 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];
1741 157274 : 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];
1742 :
1743 150511218 : for ( i = 4; i < L_FRAME48k; i++ )
1744 : {
1745 150353944 : 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];
1746 : }
1747 :
1748 157274 : memory[2][0] = tmp2[L_FRAME48k - 4];
1749 157274 : memory[2][1] = tmp2[L_FRAME48k - 3];
1750 157274 : memory[2][2] = tmp2[L_FRAME48k - 2];
1751 157274 : memory[2][3] = tmp2[L_FRAME48k - 1];
1752 :
1753 157274 : memory[3][0] = output[L_FRAME48k - 4];
1754 157274 : memory[3][1] = output[L_FRAME48k - 3];
1755 157274 : memory[3][2] = output[L_FRAME48k - 2];
1756 157274 : memory[3][3] = output[L_FRAME48k - 1];
1757 :
1758 157274 : return;
1759 : }
1760 :
1761 :
1762 : /*-------------------------------------------------------------------*
1763 : * synthesise_fb_high_band()
1764 : *
1765 : * Creates the highband output for full band - 14.0 to 20 kHz
1766 : * Using the energy shaped white excitation signal from the SWB BWE.
1767 : * The excitation signal input is sampled at 16kHz and so is upsampled
1768 : * to 48 kHz first.
1769 : * Uses a complementary split filter to code the two regions from
1770 : * 14kHz to 16kHz and 16 kHz to 20 kHz.
1771 : * One of 16 tilt filters is also applied afterwards to further
1772 : * refine the spectral shape of the fullband signal.
1773 : * The tilt is specified in dB per kHz. N.B. Only negative values are
1774 : * accomodated.
1775 : *-------------------------------------------------------------------*/
1776 :
1777 113397 : void synthesise_fb_high_band(
1778 : const float excitation_in[], /* i : full band excitation */
1779 : float output[], /* o : high band speech - 14.0 to 20 kHz */
1780 : const float fb_exc_energy, /* i : full band excitation energy */
1781 : const float ratio, /* i : energy ratio */
1782 : const int16_t L_frame, /* i : ACELP frame length */
1783 : const int16_t bfi, /* i : BFI flag */
1784 : float *prev_fbbwe_ratio, /* o : previous frame energy for FEC */
1785 : float bpf_memory[][4] /* i/o: memory for elliptic bpf 48k */
1786 : )
1787 : {
1788 : int16_t i, j;
1789 : float excitation_in_interp3[L_FRAME48k];
1790 : float tmp[L_FRAME48k];
1791 : float temp1, ratio2;
1792 :
1793 : /* Interpolate the white energy shaped gaussian excitation from 16 kHz to 48 kHz with zeros */
1794 : /* white excitation from DC to 8 kHz resampled to produce DC to 24 kHz excitation. */
1795 36400437 : for ( i = 0, j = 0; i < L_FRAME48k; i += 3, j++ )
1796 : {
1797 36287040 : excitation_in_interp3[i] = 3.0f * excitation_in[j];
1798 36287040 : excitation_in_interp3[i + 1] = 0.0f;
1799 36287040 : excitation_in_interp3[i + 2] = 0.0f;
1800 : }
1801 :
1802 113397 : if ( L_frame == L_FRAME16k )
1803 : {
1804 : /* for 16kHz ACELP core */
1805 90429 : elliptic_bpf_48k_generic( excitation_in_interp3, tmp, bpf_memory, full_band_bpf_3 );
1806 : }
1807 : else
1808 : {
1809 : /* for 12.8kHz ACELP core */
1810 22968 : elliptic_bpf_48k_generic( excitation_in_interp3, tmp, bpf_memory, full_band_bpf_1 );
1811 : }
1812 113397 : temp1 = sum2_f( tmp, L_FRAME48k ) + 0.001f;
1813 113397 : ratio2 = (float) ( ratio * sqrt( fb_exc_energy / temp1 ) );
1814 :
1815 113397 : if ( !bfi )
1816 : {
1817 110682 : *prev_fbbwe_ratio = ratio;
1818 : }
1819 : else
1820 : {
1821 2715 : *prev_fbbwe_ratio = ratio * 0.5f;
1822 : }
1823 108974517 : for ( i = 0; i < L_FRAME48k; i++ )
1824 : {
1825 108861120 : output[i] = tmp[i] * ratio2;
1826 : }
1827 :
1828 113397 : return;
1829 : }
1830 :
1831 : /*-------------------------------------------------------------------*
1832 : * Estimate_mix_factors() *
1833 : * *
1834 : * Estimate mix factors for SHB excitation generation *
1835 : *-------------------------------------------------------------------*/
1836 :
1837 25838 : static void Estimate_mix_factors(
1838 : const float *shb_res, /* i : SHB LP residual */
1839 : const float *exc16kWhtnd, /* i : SHB transformed low band excitation */
1840 : const float *White_exc16k, /* i : Modulated envelope shaped white noise */
1841 : const float pow1, /* i : SHB exc. power for normalization */
1842 : const float pow22, /* i : White noise excitation for normalization*/
1843 : float *vf_modified, /* o : Estimated voice factors */
1844 : int16_t *vf_ind /* o : voice factors VQ index */
1845 : )
1846 : {
1847 : float shb_res_local[L_FRAME16k], WN_exc_local[L_FRAME16k];
1848 : float pow3, temp_p1_p2, temp_p1_p3;
1849 : float temp_numer1[L_FRAME16k], temp_numer2[L_FRAME16k];
1850 : int16_t i, length;
1851 :
1852 25838 : mvr2r( shb_res, shb_res_local, L_FRAME16k );
1853 25838 : mvr2r( White_exc16k, WN_exc_local, L_FRAME16k );
1854 :
1855 25838 : pow3 = dotp( shb_res_local, shb_res_local, L_FRAME16k );
1856 :
1857 25838 : pow3 += 0.00001f;
1858 25838 : temp_p1_p2 = (float) sqrt( pow1 / pow22 );
1859 25838 : temp_p1_p3 = (float) sqrt( pow1 / pow3 );
1860 :
1861 :
1862 8293998 : for ( i = 0; i < L_FRAME16k; i++ )
1863 : {
1864 8268160 : WN_exc_local[i] *= temp_p1_p2;
1865 8268160 : shb_res_local[i] *= temp_p1_p3;
1866 : }
1867 8293998 : for ( i = 0; i < L_FRAME16k; i++ )
1868 : {
1869 8268160 : temp_numer1[i] = shb_res_local[i] - WN_exc_local[i];
1870 8268160 : temp_numer2[i] = exc16kWhtnd[i] - WN_exc_local[i];
1871 : }
1872 :
1873 25838 : length = L_FRAME16k;
1874 51676 : for ( i = 0; i < 1; i++ )
1875 : {
1876 25838 : temp_p1_p2 = dotp( temp_numer1 + i * length, temp_numer2 + i * length, length );
1877 25838 : temp_p1_p3 = dotp( temp_numer2 + i * length, temp_numer2 + i * length, length );
1878 25838 : vf_modified[i] = min( max( ( temp_p1_p2 / temp_p1_p3 ), 0.1f ), 0.99f );
1879 : }
1880 :
1881 25838 : *vf_ind = usquant( vf_modified[0], &temp_p1_p2, 0.125, 0.125, 1 << NUM_BITS_SHB_VF );
1882 25838 : set_f( vf_modified, temp_p1_p2, NB_SUBFR16k );
1883 :
1884 25838 : return;
1885 : }
1886 :
1887 : /*-------------------------------------------------------------------*
1888 : * tbe_celp_exc() *
1889 : * *
1890 : * Prepare adaptive part of TBE excitation *
1891 : *-------------------------------------------------------------------*/
1892 :
1893 2116007 : void tbe_celp_exc(
1894 : const int16_t element_mode, /* i : element mode */
1895 : const int16_t idchan, /* i : channel ID */
1896 : float *bwe_exc, /* i/o: BWE excitation */
1897 : const int16_t L_frame, /* i : frame length */
1898 : const int16_t L_subfr, /* i : subframe length */
1899 : const int16_t i_subfr, /* i : subframe index */
1900 : const int16_t T0, /* i : integer pitch lag */
1901 : const int16_t T0_frac, /* i : fraction of lag */
1902 : float *error, /* i/o: error */
1903 : const int16_t tdm_LRTD_flag /* i : LRTD stereo mode flag */
1904 : )
1905 : {
1906 : int16_t i, offset;
1907 :
1908 2116007 : if ( element_mode == IVAS_CPE_TD && idchan == 1 && !tdm_LRTD_flag )
1909 : {
1910 1664 : return;
1911 : }
1912 :
1913 2114343 : assert( bwe_exc != NULL && "BWE excitation is NULL" );
1914 :
1915 2114343 : if ( L_frame == L_FRAME )
1916 : {
1917 842468 : offset = tbe_celp_exc_offset( T0, T0_frac );
1918 :
1919 135637348 : for ( i = 0; i < L_subfr * HIBND_ACB_L_FAC; i++ )
1920 : {
1921 134794880 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset + (int16_t) *error];
1922 : }
1923 842468 : *error += (float) offset - (float) T0 * HIBND_ACB_L_FAC - 0.25f * HIBND_ACB_L_FAC * (float) T0_frac;
1924 : }
1925 : else
1926 : {
1927 1271875 : offset = T0 * 2 + (int16_t) ( (float) T0_frac * 0.5f + 4 + 0.5f ) - 4;
1928 164071875 : for ( i = 0; i < L_subfr * 2; i++ )
1929 : {
1930 162800000 : bwe_exc[i + i_subfr * 2] = bwe_exc[i + i_subfr * 2 - offset + (int16_t) *error];
1931 : }
1932 1271875 : *error += (float) offset - (float) T0 * 2 - 0.5f * (float) T0_frac;
1933 : }
1934 :
1935 2114343 : return;
1936 : }
1937 :
1938 : /*-------------------------------------------------------------------*
1939 : * prep_tbe_exc() *
1940 : * *
1941 : * Prepare TBE excitation *
1942 : *-------------------------------------------------------------------*/
1943 :
1944 2367373 : void prep_tbe_exc(
1945 : const int16_t L_frame, /* i : length of the frame */
1946 : const int16_t L_subfr, /* i : subframe length */
1947 : const int16_t i_subfr, /* i : subframe index */
1948 : const float gain_pit, /* i : Pitch gain */
1949 : const float gain_code, /* i : algebraic codebook gain */
1950 : const float code[], /* i : algebraic excitation */
1951 : const float voice_fac, /* i : voicing factor */
1952 : float *voice_factors, /* o : TBE voicing factor */
1953 : float bwe_exc[], /* i/o: excitation for TBE */
1954 : const float gain_preQ, /* i : prequantizer excitation gain*/
1955 : const float code_preQ[], /* i : prequantizer excitation */
1956 : const int16_t T0, /* i : integer pitch variables */
1957 : const int16_t coder_type, /* i : coding type */
1958 : const int32_t core_brate, /* i : core bitrate */
1959 : const int16_t element_mode, /* i : element mode */
1960 : const int16_t idchan, /* i : channel ID */
1961 : const int16_t flag_TD_BWE, /* i : flag indicating whether hTD_BWE exists */
1962 : const int16_t tdm_LRTD_flag /* i : LRTD stereo mode flag */
1963 : )
1964 : {
1965 : int16_t i;
1966 : float tmp_code[2 * L_SUBFR * HIBND_ACB_L_FAC];
1967 : float tmp_code_preInt[L_SUBFR];
1968 2367373 : float tmp = 1.0f;
1969 :
1970 2367373 : *voice_factors = VF_0th_PARAM + VF_1st_PARAM * voice_fac + VF_2nd_PARAM * voice_fac * voice_fac;
1971 :
1972 2367373 : if ( ( coder_type == VOICED || T0 > 115.5f ) && core_brate > ACELP_8k00 )
1973 : {
1974 723842 : tmp = 1.0f;
1975 723842 : *voice_factors *= tmp;
1976 : }
1977 :
1978 2367373 : *voice_factors = min( max( 0.000001f, *voice_factors ), 0.999999f );
1979 :
1980 2367373 : if ( element_mode == IVAS_CPE_TD && idchan == 1 && !tdm_LRTD_flag )
1981 : {
1982 1664 : if ( flag_TD_BWE && i_subfr == 0 )
1983 : {
1984 0 : set_f( bwe_exc, 0, L_FRAME32k );
1985 : }
1986 :
1987 1664 : return;
1988 : }
1989 :
1990 2365709 : if ( L_frame == L_FRAME )
1991 : {
1992 960964 : interp_code_5over2( code, tmp_code, L_subfr );
1993 :
1994 154715204 : for ( i = 0; i < L_subfr * HIBND_ACB_L_FAC; i++ )
1995 : {
1996 153754240 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = gain_pit * bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] +
1997 153754240 : gain_code * tmp_code[i];
1998 : }
1999 : }
2000 : else
2001 : {
2002 91308425 : for ( i = 0; i < L_subfr; i++ )
2003 : {
2004 89903680 : tmp_code_preInt[i] = gain_code * code[i] + 2 * gain_preQ * code_preQ[i];
2005 : }
2006 :
2007 1404745 : interp_code_4over2( tmp_code_preInt, tmp_code, L_subfr );
2008 :
2009 181212105 : for ( i = 0; i < L_subfr * 2; i++ )
2010 : {
2011 179807360 : bwe_exc[i + i_subfr * 2] = gain_pit * bwe_exc[i + i_subfr * 2] + tmp_code[i];
2012 : }
2013 : }
2014 :
2015 2365709 : return;
2016 : }
2017 :
2018 :
2019 : /*-------------------------------------------------------------------*
2020 : * get_tbe_bits() *
2021 : * *
2022 : * Determine TBE bit consumption per frame from bitrate *
2023 : *-------------------------------------------------------------------*/
2024 :
2025 4832 : int16_t get_tbe_bits(
2026 : const int32_t total_brate,
2027 : const int16_t bwidth,
2028 : const int16_t rf_mode )
2029 : {
2030 4832 : int16_t i, bits = 0;
2031 :
2032 4832 : if ( rf_mode )
2033 : {
2034 : /* TBE bits for core, primary frame */
2035 0 : if ( bwidth == WB && total_brate == ACELP_13k20 )
2036 : {
2037 : /* Gain frame: 4, Gain shapes: 0, and LSFs: 2 */
2038 0 : bits = NUM_BITS_SHB_FrameGain_LBR_WB + NUM_BITS_LBR_WB_LSF;
2039 : }
2040 0 : else if ( bwidth == SWB && total_brate == ACELP_13k20 )
2041 : {
2042 : /* Gain frame: 5, Gain shapes: 5, and lowrate LSFs: 8 */
2043 0 : bits = NUM_BITS_SHB_FRAMEGAIN + NUM_BITS_SHB_SUBGAINS + 8;
2044 : }
2045 : }
2046 : else
2047 : {
2048 4832 : if ( bwidth == WB && total_brate == ACELP_9k60 )
2049 : {
2050 0 : bits = NUM_BITS_LBR_WB_LSF + NUM_BITS_SHB_FrameGain_LBR_WB;
2051 : }
2052 4832 : else if ( bwidth == SWB || bwidth == FB )
2053 : {
2054 4832 : if ( total_brate == ACELP_9k60 )
2055 : {
2056 0 : bits = NUM_BITS_SHB_FRAMEGAIN + NUM_BITS_SHB_SUBGAINS + 8;
2057 : }
2058 4832 : else if ( total_brate >= ACELP_13k20 && total_brate <= ACELP_32k )
2059 : {
2060 4832 : bits = NUM_BITS_SHB_SUBGAINS + NUM_BITS_SHB_FRAMEGAIN + NUM_LSF_GRID_BITS + MIRROR_POINT_BITS;
2061 :
2062 28992 : for ( i = 0; i < NUM_Q_LSF; i++ )
2063 : {
2064 24160 : bits += lsf_q_num_bits[i];
2065 : }
2066 : }
2067 :
2068 4832 : if ( total_brate >= ACELP_24k40 )
2069 : {
2070 4832 : bits += NUM_BITS_SHB_ENER_SF + NUM_BITS_SHB_VF + NUM_BITS_SHB_RES_GS * NB_SUBFR16k;
2071 : }
2072 :
2073 4832 : if ( bwidth == SWB && ( total_brate == ACELP_16k40 || total_brate == ACELP_24k40 ) )
2074 : {
2075 4832 : bits += BITS_TEC + BITS_TFA;
2076 : }
2077 :
2078 4832 : if ( bwidth == FB )
2079 : {
2080 : /* fullband slope */
2081 0 : bits += 4;
2082 : }
2083 : }
2084 : }
2085 :
2086 4832 : return bits;
2087 : }
|