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