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 3676103 : 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 3676103 : set_f( mem_csfilt, 0, 2 );
80 3676103 : set_f( mem_genSHBexc_filt_down_shb, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
81 3676103 : set_f( state_lpc_syn, 0.0f, LPC_SHB_ORDER );
82 3676103 : set_f( syn_overlap, 0.0f, L_SHB_LAHEAD );
83 3676103 : set_f( state_syn_shbexc, 0.0f, L_SHB_LAHEAD );
84 3676103 : *tbe_demph = 0.0f;
85 3676103 : *tbe_premph = 0.0f;
86 3676103 : set_f( mem_stp_swb, 0, LPC_SHB_ORDER );
87 3676103 : *gain_prec_swb = 1.0f;
88 :
89 3676103 : 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 3171545 : void swb_tbe_reset_synth(
100 : float genSHBsynth_Hilbert_Mem[],
101 : float genSHBsynth_state_lsyn_filt_shb_local[] )
102 : {
103 3171545 : set_f( genSHBsynth_Hilbert_Mem, 0.0f, HILBERT_MEM_SIZE );
104 3171545 : set_f( genSHBsynth_state_lsyn_filt_shb_local, 0.0f, 2 * ALLPASSSECTIONS_STEEP );
105 :
106 3171545 : 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 3478376 : 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 3478376 : 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 3478376 : 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 624941 : 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 624941 : period = 17; /* == (int16_t) (32000.0f / 1850.0f + 0.5f); */
159 :
160 624941 : recip_period = 256.0f / (float) period;
161 11248938 : for ( i = 0; i < period; i++ )
162 : {
163 10623997 : k = (int16_t) ( i * recip_period + 0.5f );
164 10623997 : if ( k <= 64 )
165 : {
166 3124705 : local_negsin_table[i] = -sincos_t[k];
167 3124705 : local_cos_table[i] = sincos_t[64 - k];
168 : }
169 7499292 : else if ( k <= 128 )
170 : {
171 2499764 : local_negsin_table[i] = -sincos_t[128 - k];
172 2499764 : local_cos_table[i] = -sincos_t[k - 64];
173 : }
174 4999528 : else if ( k <= 192 )
175 : {
176 2499764 : local_negsin_table[i] = sincos_t[k - 128];
177 2499764 : local_cos_table[i] = -sincos_t[192 - k];
178 : }
179 : else
180 : {
181 2499764 : local_negsin_table[i] = sincos_t[256 - k];
182 2499764 : local_cos_table[i] = sincos_t[k - 192];
183 : }
184 : }
185 :
186 192080061 : for ( i = 0; i < length; i = i + 2 )
187 : {
188 191455120 : input[i] = -input[i];
189 : }
190 :
191 624941 : mvr2r( input, tmp + HILBERT_ORDER1, length );
192 :
193 624941 : mvr2r( mem1_ext, tmp, HILBERT_ORDER1 );
194 :
195 : /* Hilber transform stage - 0 */
196 624941 : Hilbert_transform( tmp, tmp, tmpi_R, tmpi_I, length, 0 );
197 :
198 624941 : mvr2r( mem2_ext, tmpi2_R, HILBERT_ORDER2 );
199 624941 : mvr2r( mem3_ext, tmpi2_I, HILBERT_ORDER2 );
200 :
201 : /* Hilber transform stage - 1 */
202 624941 : Hilbert_transform( tmpi_R, tmpi_I, tmpi2_R, tmpi2_I, length, 1 );
203 :
204 624941 : mvr2r( tmp + length, mem1_ext, HILBERT_ORDER1 );
205 624941 : mvr2r( mem2_ext + HILBERT_ORDER2, tmp_R, HILBERT_ORDER2 );
206 624941 : mvr2r( mem3_ext + HILBERT_ORDER2, tmp_I, HILBERT_ORDER2 );
207 :
208 : /* Hilber transform stage - 2 */
209 624941 : Hilbert_transform( tmpi2_R, tmpi2_I, tmpi_R, tmpi_I, length, 2 );
210 :
211 624941 : mvr2r( tmpi2_R + length, mem2_ext, HILBERT_ORDER2 );
212 624941 : mvr2r( tmpi2_I + length, mem3_ext, HILBERT_ORDER2 );
213 :
214 : /* Hilber transform stage - 3 */
215 624941 : Hilbert_transform( tmpi_R, tmpi_I, tmp_R, tmp_I, length, 3 );
216 :
217 624941 : mvr2r( tmp_R + length, mem2_ext + HILBERT_ORDER2, HILBERT_ORDER2 );
218 624941 : mvr2r( tmp_I + length, mem3_ext + HILBERT_ORDER2, HILBERT_ORDER2 );
219 :
220 624941 : if ( *phase_state >= period )
221 : {
222 0 : *phase_state = 0;
223 : }
224 :
225 23726044 : for ( i = 0, j = *phase_state; i < length; )
226 : {
227 406011343 : for ( ; ( j < period ) && ( i < length ); j++, i++ )
228 : {
229 382910240 : output[i] = tmp_R[i + HILBERT_ORDER2] * local_cos_table[j] + tmp_I[i + HILBERT_ORDER2] * local_negsin_table[j];
230 : }
231 :
232 23101103 : if ( j >= period )
233 : {
234 22509724 : j = 0;
235 : }
236 : }
237 :
238 624941 : *phase_state = j;
239 :
240 624941 : return;
241 : }
242 :
243 : /*----------------------------------------------
244 : * Hilbert_transform()
245 : *
246 : * Hilbert transform
247 : *------------------------------------------------*/
248 :
249 2499764 : 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 2499764 : hb_filter_stage = 2 * HB_stage_id;
261 2499764 : offset = ( HB_stage_id == 0 ) ? 1 : 0;
262 :
263 2499764 : if ( HB_stage_id == 0 || HB_stage_id == 2 )
264 : {
265 767070362 : for ( i = 0; i < length; i++ )
266 : {
267 765820480 : 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 765820480 : 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 1249882 : else if ( HB_stage_id == 1 || HB_stage_id == 3 )
273 : {
274 767070362 : for ( i = 0; i < length; i++ )
275 : {
276 765820480 : 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 765820480 : 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 2499764 : return;
283 : }
284 :
285 : /*-------------------------------------------------------------------*
286 : * flip_spectrum()
287 : *
288 : *
289 : *-------------------------------------------------------------------*/
290 :
291 654606 : 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 105391566 : for ( i = 0; i < length; i = i + 2 )
300 : {
301 104736960 : output[i] = -input[i];
302 104736960 : output[i + 1] = input[i + 1];
303 : }
304 :
305 654606 : return;
306 : }
307 :
308 : /*-------------------------------------------------------------------*
309 : * flip_spectrum_and_decimby4()
310 : *
311 : *
312 : *-------------------------------------------------------------------*/
313 :
314 353698 : 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 353698 : if ( ramp_flag )
328 : {
329 1583 : factor = 4.0f / length;
330 64903 : for ( i = 0; i < length / 4; i = i + 2 )
331 : {
332 63320 : input_change[i] = -input[i] * ( i * factor );
333 63320 : input_change[i + 1] = input[i + 1] * ( ( i + 1.0f ) * factor );
334 : }
335 : }
336 : else
337 : {
338 352115 : i = 0;
339 : }
340 :
341 53371476 : for ( ; i < length; i = i + 2 )
342 : {
343 53017778 : input_change[i] = -input[i];
344 53017778 : input_change[i + 1] = input[i + 1];
345 : }
346 :
347 353698 : Decimate_allpass_steep( input_change, mem1, length, tmp );
348 353698 : Decimate_allpass_steep( tmp, mem2, length / 2, output );
349 :
350 353698 : return;
351 : }
352 :
353 : /*-------------------------------------------------------------------*
354 : * GenShapedWBExcitation()
355 : *
356 : * Synthesize spectrally shaped highband excitation signal for the wideband
357 : *-------------------------------------------------------------------*/
358 :
359 290111 : 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 290111 : float csfilt_num2[1] = { 0.05f };
385 290111 : 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 290111 : float avg_voice_fac = 0.25f * sum_f( voice_factors, NB_SUBFR );
390 :
391 290111 : if ( igf_flag && ( coder_type == VOICED || avg_voice_fac > 0.35f ) )
392 : {
393 1916 : csfilt_num2[0] = 0.2f;
394 1916 : csfilt_den2[1] = -0.8f;
395 : }
396 288195 : else if ( igf_flag && ( coder_type == UNVOICED || avg_voice_fac < 0.2f ) )
397 : {
398 817 : csfilt_num2[0] = 0.01f;
399 817 : csfilt_den2[1] = -0.99f;
400 : }
401 290111 : set_f( wht_fil_mem, 0, LPC_WHTN_ORDER_WB );
402 :
403 290111 : Decimate_allpass_steep( bwe_exc_extended, mem_genSHBexc_filt_down1, L_FRAME32k, excTmp );
404 :
405 290111 : flip_spectrum_and_decimby4( excTmp, exc4k, L_FRAME16k, mem_genSHBexc_filt_down2, mem_genSHBexc_filt_down3, 0 );
406 :
407 290111 : if ( uv_flag )
408 : {
409 : /* unvoiced signal */
410 5412 : create_random_vector( exc4kWhtnd, L_FRAME16k / 4, bwe_seed );
411 : }
412 : else
413 : {
414 284699 : 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 284699 : R[0] = max( R[0], 1.0e-8f );
418 1138796 : for ( i = 0; i <= LPC_WHTN_ORDER_WB; i++ )
419 : {
420 854097 : R[i] = R[i] * wac[i];
421 : }
422 284699 : lev_dur( lpc_whtn, R, LPC_WHTN_ORDER_WB, ervec );
423 :
424 284699 : 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 23060619 : for ( i = 0, pow1 = 0.00001f; i < L_FRAME16k / 4; i++ )
428 : {
429 22775920 : excTmp2[i] = (float) ( fabs( exc4kWhtnd[i] ) );
430 22775920 : pow1 += exc4kWhtnd[i] * exc4kWhtnd[i];
431 : }
432 :
433 23060619 : for ( i = 0; i < L_FRAME16k / 4; i++ )
434 : {
435 22775920 : excNoisyEnv[i] = *mem_csfilt + csfilt_num2[0] * excTmp2[i];
436 22775920 : *mem_csfilt = -csfilt_den2[1] * excNoisyEnv[i];
437 : }
438 :
439 284699 : create_random_vector( exc4k, L_FRAME16k / 4, bwe_seed );
440 :
441 : /* Ensure pow22 is greater than zero when computing normalization */
442 23060619 : for ( i = 0, pow22 = 0.00001f; i < L_FRAME16k / 4; i++ )
443 : {
444 22775920 : exc4k[i] *= excNoisyEnv[i];
445 22775920 : pow22 += exc4k[i] * exc4k[i];
446 : }
447 :
448 284699 : if ( coder_type == UNVOICED || ( igf_flag && avg_voice_fac < 0.2f ) )
449 : {
450 6704 : scale = (float) sqrt( pow1 / pow22 );
451 6704 : if ( pow22 == 0.f )
452 : {
453 0 : scale = 0;
454 : }
455 :
456 543024 : for ( i = 0; i < L_FRAME16k / 4; i++ )
457 : {
458 536320 : exc4kWhtnd[i] = exc4k[i] * scale;
459 : }
460 : }
461 : else
462 : {
463 1389975 : for ( i = 0, k = 0; i < 4; i++ )
464 : {
465 :
466 1111980 : if ( igf_flag && coder_type == VOICED )
467 : {
468 6644 : tmp_vfac = 2 * voice_factors[i];
469 6644 : tmp_vfac = min( 1, tmp_vfac );
470 : }
471 : else
472 : {
473 1105336 : tmp_vfac = voice_factors[i];
474 : }
475 :
476 1111980 : temp1 = root_a( tmp_vfac );
477 1111980 : temp2 = root_a_over_b( pow1 * ( 1.0f - tmp_vfac ), pow22 );
478 :
479 :
480 23351580 : for ( j = 0; j < L_FRAME16k / 16; j++, k++ )
481 : {
482 22239600 : exc4kWhtnd[k] = temp1 * exc4kWhtnd[k] + temp2 * exc4k[k];
483 : }
484 : }
485 : }
486 : }
487 :
488 290111 : syn_filt( lpc_shb, LPC_SHB_ORDER_WB, exc4kWhtnd, excSHB, L_FRAME16k / 4, state_lpc_syn, 1 );
489 :
490 290111 : return;
491 : }
492 :
493 : /*-------------------------------------------------------------------*
494 : * GenWBSynth()
495 : *
496 : * Generate 16 KHz sampled highband component from synthesized highband
497 : *-------------------------------------------------------------------*/
498 :
499 273073 : 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 273073 : Interpolate_allpass_steep( input_synspeech, state_lsyn_filt_shb1, L_FRAME16k / 4, speech_buf_16k1 );
509 :
510 273073 : Interpolate_allpass_steep( speech_buf_16k1, state_lsyn_filt_shb2, L_FRAME16k / 2, speech_buf_16k2 );
511 :
512 273073 : flip_spectrum( speech_buf_16k2, shb_syn_speech_16k, L_FRAME16k );
513 :
514 273073 : return;
515 : }
516 :
517 : /*-------------------------------------------------------------------*
518 : * PostShortTerm()
519 : *
520 : * Short term processing
521 : *-------------------------------------------------------------------*/
522 :
523 5150820 : 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 5150820 : set_f( apond1, 0, LPC_SHB_ORDER + 1 );
541 5150820 : set_f( apond2, 0, LONG_H_ST );
542 5150820 : set_f( sig_ltp, 0, L_SUBFR16k + 1 );
543 :
544 : /* Obtain post-filter weights */
545 5150820 : g1 = GAMMA0 + GAMMA_SHARP * formant_fac;
546 5150820 : g2 = GAMMA0 - GAMMA_SHARP * formant_fac;
547 :
548 : /* Compute weighted LPC coefficients */
549 5150820 : weight_a( lpccoeff, apond1, g1, LPC_SHB_ORDER );
550 5150820 : weight_a( lpccoeff, apond2, g2, LPC_SHB_ORDER );
551 :
552 : /* Compute A(gamma2) residual */
553 5150820 : residu( apond2, LPC_SHB_ORDER, sig_in, sig_ltp + 1, L_SUBFR16k );
554 :
555 : /* Save last output of 1/A(gamma1) */
556 5150820 : sig_ltp[0] = *ptr_mem_stp;
557 :
558 : /* Control short term pst filter gain and compute parcor0 */
559 5150820 : 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 5150820 : 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 5150820 : filt_mu( sig_ltp, sig_out, parcor0, L_SUBFR16k, SWB_TBE );
566 :
567 : /* gain control */
568 5150820 : scale_st( sig_in, sig_out, ptr_gain_prec, L_SUBFR16k, SWB_TBE );
569 :
570 5150820 : 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 1307608 : 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 1307608 : tmp = 0.5f * (float) fabs( lpc_shb2 ) + 0.5f * *tilt_mem;
591 1307608 : *tilt_mem = tmp;
592 :
593 : /* Map to PF strength */
594 1307608 : formant_fac = ( tmp - SWB_TILT_LOW ) * SWB_TILT_DELTA;
595 1307608 : if ( formant_fac > 1.0f )
596 : {
597 1131 : formant_fac = 1.0f;
598 : }
599 1306477 : else if ( formant_fac < 0.0f )
600 : {
601 1212297 : formant_fac = 0.0f;
602 : }
603 :
604 1307608 : formant_fac = 1.0f - 0.5f * formant_fac;
605 :
606 1307608 : return formant_fac;
607 : }
608 :
609 13260 : 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 13260 : assert( len > 0 && len <= L_FRAME16k );
621 :
622 13260 : K = (int16_t) ( len_h / 2 ); /* length of FIR filter memory = half of the total filter length */
623 13260 : w = 1.0f / len_h; /* MA filtering coefficient */
624 :
625 : /* copy filter memory to the input buffer */
626 13260 : if ( mem_h != NULL )
627 : {
628 4420 : mvr2r( mem_h, buf_in, K );
629 : }
630 : else
631 : {
632 : /* no memory available, use the first len_h/2 samples as memory */
633 8840 : p_in = buf_in;
634 97240 : for ( k = 0; k < K; k++ )
635 : {
636 88400 : *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 13260 : p_in = &buf_in[K];
643 4256460 : for ( k = 0; k < len; k++ )
644 : {
645 4243200 : *p_in++ = (float) fabs( inp[k] ) * w;
646 : }
647 :
648 : /* update filter memory from the end of the input buffer */
649 13260 : if ( mem_h != NULL )
650 : {
651 4420 : mvr2r( &buf_in[len], mem_h, K );
652 : }
653 :
654 : /* do MA filtering */
655 13260 : out[0] = sum_f( buf_in, len_h );
656 13260 : p_out = &buf_in[0]; /* pointer to leaving sample */
657 13260 : p_in = &buf_in[len_h]; /* pointer to entering sample*/
658 4110600 : for ( k = 1; k < len - K; k++ )
659 : {
660 4097340 : out[k] = out[k - 1] - *p_out++ + *p_in++;
661 : }
662 :
663 : /* use IIR filtering to extrapolate the last K samples */
664 13260 : p_in = &buf_in[len - K];
665 13260 : p_out = &out[len - K];
666 13260 : p_prev = p_out - 1;
667 145860 : for ( k = 0; k < K; k++ )
668 : {
669 132600 : *p_out++ = 0.05f * ( *p_in++ ) + 0.95f * ( *p_prev++ );
670 : }
671 :
672 13260 : return;
673 : }
674 :
675 : /*-------------------------------------------------------------------*
676 : * GenShapedSHBExcitation()
677 : *
678 : * Synthesize spectrally shaped highband excitation signal
679 : *-------------------------------------------------------------------*/
680 :
681 1307608 : 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 1307608 : float csfilt_num2[1] = { 0.2f };
728 1307608 : float csfilt_den2[2] = { 1.0f, -0.8f };
729 : float varEnvShape;
730 : float ervec[LPC_WHTN_ORDER + 2];
731 : float exc16kWhtnd[L_FRAME16k];
732 1307608 : 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 1307608 : 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 1307608 : mix_factor = 0.0f;
752 :
753 1307608 : set_f( zero_mem, 0, LPC_SHB_ORDER );
754 1307608 : set_f( wht_fil_mem, 0, LPC_WHTN_ORDER );
755 1307608 : set_f( EnvWhiteExc16k_4k, 0, L_FRAME4k );
756 1307608 : set_f( EnvExc16kWhtnd_4k, 0, L_FRAME4k );
757 :
758 : /* Mirror the spectrum */
759 838176728 : for ( i = 0; i < L_FRAME32k; i++ )
760 : {
761 836869120 : exc32k[i] = ( ( i % 2 ) == 0 ) ? ( -bwe_exc_extended[i] ) : ( bwe_exc_extended[i] );
762 : }
763 :
764 : /* Decimate by 2 */
765 1307608 : Decimate_allpass_steep( exc32k, mem_genSHBexc_filt_down_shb, 2 * L_FRAME16k, exc16k );
766 :
767 1307608 : 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 1307608 : R[0] = max( R[0], 1.0e-8f );
771 7845648 : for ( i = 0; i <= LPC_WHTN_ORDER; i++ )
772 : {
773 6538040 : R[i] = R[i] * wac[i];
774 : }
775 :
776 : /* Ensure R[0] isn't zero when entering Levinson-Durbin */
777 1307608 : R[0] += 1.0e-8f;
778 :
779 1307608 : lev_dur( lpc_whtn, R, LPC_WHTN_ORDER, ervec );
780 :
781 1307608 : fir( exc16k, lpc_whtn, exc16kWhtnd, wht_fil_mem, L_FRAME16k, LPC_WHTN_ORDER, 0 );
782 :
783 1307608 : if ( extl_brate >= SWB_TBE_2k8 )
784 : {
785 95595084 : for ( i = 0; i < L_FRAME16k; i++ )
786 : {
787 95297280 : exc16kWhtnd[i] *= shb_res_gshape[(int16_t) ( i / 80 )];
788 : }
789 : }
790 :
791 419742168 : for ( k = 0, pow1 = 0.00001f; k < L_FRAME16k; k++ )
792 : {
793 418434560 : excTmp2[k] = (float) ( fabs( exc16kWhtnd[k] ) );
794 418434560 : pow1 += exc16kWhtnd[k] * exc16kWhtnd[k];
795 : }
796 :
797 1307608 : if ( !flag_ACELP16k )
798 : {
799 713605 : varEnvShape = mean( voice_factors, NB_SUBFR );
800 : }
801 : else
802 : {
803 594003 : varEnvShape = mean( voice_factors, NB_SUBFR16k );
804 : }
805 :
806 1307608 : if ( extl == FB_TBE )
807 : {
808 381533 : fb_deemph_fac = max( ( 0.68f - (float) pow( varEnvShape, 3 ) ), 0.48f );
809 : }
810 :
811 1307608 : varEnvShape = 1.09875f - 0.49875f * varEnvShape;
812 1307608 : varEnvShape = min( max( varEnvShape, 0.6f ), 0.999f );
813 1307608 : csfilt_num2[0] = 1.0f - varEnvShape;
814 1307608 : csfilt_den2[1] = -varEnvShape;
815 :
816 1307608 : 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 474 : float tmp_scale = 0;
820 9954 : for ( i = 0; i < L_SUBFR16k / 4; i++ )
821 : {
822 9480 : 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 474 : if ( !( prev_bfi && extl == FB_TBE ) )
828 : {
829 : /* use weak smoothing for 1st frame after switching to make filter recover more quickly */
830 464 : varEnvShape = 0.8f;
831 464 : csfilt_num2[0] = 1.0f - varEnvShape;
832 464 : csfilt_den2[1] = -varEnvShape;
833 : }
834 474 : *mem_csfilt = varEnvShape * ( tmp_scale / ( L_SUBFR16k / 4 ) );
835 : }
836 :
837 1307608 : if ( MSFlag > 0 )
838 : {
839 50403 : varEnvShape = 0.995f;
840 50403 : csfilt_num2[0] = 1.0f - varEnvShape;
841 50403 : csfilt_den2[1] = -varEnvShape;
842 : }
843 :
844 1307608 : White_exc16k = exc16k;
845 :
846 : /* Track the low band envelope */
847 1307608 : if ( element_mode == IVAS_CPE_TD || element_mode == IVAS_CPE_DFT )
848 : {
849 294908 : if ( extl_brate != SWB_TBE_1k10 && extl_brate != SWB_TBE_1k75 )
850 : {
851 275005 : mem_csfilt_left = 0.0f;
852 275005 : mem_csfilt_right = 0.0f;
853 88276605 : for ( k = 0; k < L_FRAME16k; k++ )
854 : {
855 88001600 : excNoisyEnvLeft[k] = mem_csfilt_left + csfilt_num2[0] * excTmp2[k];
856 88001600 : mem_csfilt_left = -csfilt_den2[1] * excNoisyEnvLeft[k];
857 88001600 : excNoisyEnvRight[L_FRAME16k - k - 1] = mem_csfilt_right + csfilt_num2[0] * excTmp2[L_FRAME16k - k - 1];
858 88001600 : mem_csfilt_right = -csfilt_den2[1] * excNoisyEnvRight[L_FRAME16k - k - 1];
859 : }
860 :
861 275005 : alpha = 0.0f;
862 275005 : step = 1.0f / L_FRAME16k;
863 88276605 : for ( k = 0; k < L_FRAME16k; k++ )
864 : {
865 88001600 : excNoisyEnv[k] = alpha * excNoisyEnvLeft[k] + ( 1 - alpha ) * excNoisyEnvRight[k];
866 88001600 : alpha += step;
867 : }
868 : }
869 : }
870 : else
871 : {
872 325076700 : for ( k = 0; k < L_FRAME16k; k++ )
873 : {
874 324064000 : excNoisyEnv[k] = *mem_csfilt + csfilt_num2[0] * excTmp2[k];
875 324064000 : *mem_csfilt = -csfilt_den2[1] * excNoisyEnv[k];
876 : }
877 : }
878 :
879 1307608 : if ( extl_brate == SWB_TBE_1k10 || extl_brate == SWB_TBE_1k75 )
880 : {
881 : /* generate gaussian (white) excitation */
882 6388863 : for ( k = 0; k < L_FRAME16k; k++ )
883 : {
884 6368960 : 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 19903 : pow22 = POW_EXC16k_WHTND;
889 19903 : v_multc( White_exc16k, (float) sqrt( pow1 / pow22 ), White_exc16k, L_FRAME16k );
890 : }
891 : else
892 : {
893 1287705 : create_random_vector( White_exc16k, L_FRAME, bwe_seed );
894 1287705 : create_random_vector( White_exc16k + L_FRAME, L_FRAME16k - L_FRAME, bwe_seed );
895 :
896 413353305 : for ( k = 0, pow22 = 0.00001f; k < L_FRAME16k; k++ )
897 : {
898 412065600 : White_exc16k[k] *= excNoisyEnv[k];
899 412065600 : pow22 += White_exc16k[k] * White_exc16k[k];
900 : }
901 : }
902 :
903 1307608 : flag_plosive = 0;
904 :
905 1307608 : if ( extl_brate >= SWB_TBE_2k8 || extl_brate == SWB_TBE_1k10 || extl_brate == SWB_TBE_1k75 )
906 : {
907 317707 : if ( *vf_ind == 20 ) /* encoder side */
908 : {
909 71239 : if ( extl_brate == SWB_TBE_1k10 || extl_brate == SWB_TBE_1k75 )
910 : {
911 : /* calculate TD envelopes of exc16kWhtnd and White_exc16k */
912 4420 : find_td_envelope( White_exc16k, L_FRAME16k, 20, NULL, EnvWhiteExc16k );
913 4420 : find_td_envelope( exc16kWhtnd, L_FRAME16k, 20, NULL, EnvExc16kWhtnd );
914 :
915 358020 : for ( k = 0; k < L_FRAME4k; k++ )
916 : {
917 353600 : EnvWhiteExc16k_4k[k] = EnvWhiteExc16k[4 * k];
918 353600 : EnvExc16kWhtnd_4k[k] = EnvExc16kWhtnd[4 * k];
919 : }
920 :
921 : /* calculate the optimal mix factor */
922 4420 : c0 = c1 = c2 = c3 = c4 = c5 = 0.0f;
923 22100 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
924 : {
925 17680 : c0_part[i] = sum2_f( &EnvExc16kWhtnd_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], L_FRAME4k / NUM_SHB_SUBGAINS );
926 17680 : c1_part[i] = -2.0f * dotp( &EnvSHBres_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], &EnvExc16kWhtnd_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], L_FRAME4k / NUM_SHB_SUBGAINS );
927 17680 : c2_part[i] = sum2_f( &EnvWhiteExc16k_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], L_FRAME4k / NUM_SHB_SUBGAINS );
928 17680 : c3_part[i] = -2.0f * dotp( &EnvSHBres_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], &EnvWhiteExc16k_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], L_FRAME4k / NUM_SHB_SUBGAINS );
929 17680 : c4_part[i] = 2.0f * dotp( &EnvExc16kWhtnd_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], &EnvWhiteExc16k_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], L_FRAME4k / NUM_SHB_SUBGAINS );
930 17680 : c5_part[i] = sum2_f( &EnvSHBres_4k[i * L_FRAME4k / NUM_SHB_SUBGAINS], L_FRAME4k / NUM_SHB_SUBGAINS );
931 :
932 17680 : c0 += c0_part[i];
933 17680 : c1 += c1_part[i];
934 17680 : c2 += c2_part[i];
935 17680 : c3 += c3_part[i];
936 17680 : c4 += c4_part[i];
937 17680 : c5 += c5_part[i];
938 : }
939 :
940 4420 : den = 4.0f * c0 * c2 - c4 * c4;
941 :
942 4420 : if ( den == 0.0f )
943 : {
944 0 : den = 1e-7f;
945 : }
946 :
947 4420 : g1 = ( c3 * c4 - 2 * c1 * c2 ) / den;
948 4420 : g2 = ( c1 * c4 - 2 * c0 * c3 ) / den;
949 :
950 4420 : *Env_error = 0.0f;
951 4420 : flag_plosive = 0;
952 22100 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
953 : {
954 17680 : Env_error_part[i] = c5_part[i] + g1 * g1 * c0_part[i] + g1 * c1_part[i] + g2 * g2 * c2_part[i] + g2 * c3_part[i] + g1 * g2 * c4_part[i];
955 17680 : *Env_error += Env_error_part[i];
956 :
957 17680 : if ( Env_error_part[i] > THR_ENV_ERROR_PLOSIVE )
958 : {
959 : /* envelope error is too high -> likely a plosive */
960 40 : flag_plosive = 1;
961 : }
962 : }
963 :
964 4420 : if ( flag_plosive )
965 : {
966 : /* plosive detected -> set the mixing factor to 0 */
967 22 : *vf_ind = 0;
968 22 : mix_factor = 0.0f;
969 : }
970 : else
971 : {
972 : /* normalize gain */
973 4398 : temp = 0.0f;
974 4398 : if ( g1 + g2 == 0.0f )
975 : {
976 0 : temp = 1e-7f;
977 : }
978 :
979 4398 : g = g2 / ( g1 + g2 + temp );
980 :
981 : /* quantization of the mixing factor */
982 4398 : cbsize = 1 << NUM_BITS_SHB_VF;
983 4398 : delta = 1.0f / ( cbsize - 1 );
984 4398 : if ( g > 1.0f )
985 : {
986 834 : g = 1.0f;
987 : }
988 3564 : 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 4398 : *vf_ind = usquant( g, &mix_factor, 0.0f, 1.0f / ( cbsize - 1 ), cbsize );
995 : }
996 : }
997 : else
998 : {
999 66819 : Estimate_mix_factors( shb_res, exc16kWhtnd, White_exc16k, pow1, pow22, voiceFacEst, vf_ind );
1000 66819 : temp = ( voiceFacEst[0] > 0.7f ) ? 1.0f : 0.8f;
1001 : }
1002 : }
1003 : else /* decoder side */
1004 : {
1005 246468 : if ( extl_brate == SWB_TBE_1k10 || extl_brate == SWB_TBE_1k75 )
1006 : {
1007 15483 : if ( *vf_ind == 0 )
1008 : {
1009 84 : mix_factor = 0.0f;
1010 84 : flag_plosive = 1;
1011 : }
1012 : else
1013 : {
1014 15399 : mix_factor = usdequant( *vf_ind, 0.0f, 1.0f / ( ( 1 << NUM_BITS_SHB_VF ) - 1 ) );
1015 : }
1016 : }
1017 : else
1018 : {
1019 230985 : temp = ( ( *vf_ind * 0.125f ) > 0.7f ) ? 1.0f : 0.8f;
1020 : }
1021 : }
1022 :
1023 317707 : if ( extl_brate != SWB_TBE_1k10 && extl_brate != SWB_TBE_1k75 )
1024 : {
1025 1786824 : for ( i = 0; i < NB_SUBFR16k; i++ )
1026 : {
1027 1489020 : voice_factors[i] *= temp;
1028 : }
1029 : }
1030 : }
1031 :
1032 1307608 : if ( element_mode >= IVAS_CPE_DFT && nlExc16k != NULL )
1033 : {
1034 : /* save buffers for IC-BWE */
1035 160789 : mvr2r( exc16kWhtnd, nlExc16k, L_FRAME16k );
1036 160789 : v_multc( White_exc16k, (float) sqrt( pow1 / pow22 ), mixExc16k, L_FRAME16k );
1037 : }
1038 :
1039 1307608 : mvr2r( White_exc16k, White_exc16k_FB, L_FRAME16k );
1040 1307608 : deemph( White_exc16k, PREEMPH_FAC, L_FRAME16k, tbe_demph );
1041 :
1042 1307608 : if ( extl_brate == SWB_TBE_1k10 || extl_brate == SWB_TBE_1k75 )
1043 : {
1044 19903 : 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 19797 : old_scale = (float) sqrt( *prev_pow_exc16kWhtnd / pow1 );
1048 19797 : new_scale = 1.0f;
1049 19797 : step_scale = ( new_scale - old_scale ) / ( L_FRAME16k / 2 );
1050 19797 : scale = old_scale;
1051 :
1052 : /* interpolate between the old and the new value of the mixing factor */
1053 19797 : old_fact = *prev_mix_factor;
1054 19797 : new_fact = mix_factor;
1055 19797 : step = ( new_fact - old_fact ) / ( L_FRAME16k / 2 );
1056 19797 : fact = old_fact;
1057 :
1058 : /* mixing of LB and gaussian excitation in the first half of the frame */
1059 3187317 : for ( k = 0; k < L_FRAME16k / 2; k++ )
1060 : {
1061 3167520 : exc16kWhtnd[k] = (float) fact * ( White_exc16k[k] * scale ) + (float) ( 1 - fact ) * exc16kWhtnd[k];
1062 3167520 : fact += step;
1063 3167520 : scale += step_scale;
1064 : }
1065 :
1066 : /* mixing of LB and gaussian excitation in the second half of the frame */
1067 3187317 : for ( ; k < L_FRAME16k; k++ )
1068 : {
1069 3167520 : exc16kWhtnd[k] = (float) new_fact * White_exc16k[k] + (float) ( 1 - new_fact ) * exc16kWhtnd[k];
1070 : }
1071 : }
1072 19903 : preemph( exc16kWhtnd, PREEMPH_FAC, L_FRAME16k, tbe_premph );
1073 : }
1074 : else
1075 : {
1076 1287705 : if ( coder_type == UNVOICED || MSFlag == 1 )
1077 : {
1078 69487 : scale = (float) sqrt( pow1 / pow22 );
1079 69487 : if ( pow22 == 0.f )
1080 : {
1081 0 : scale = 0;
1082 : }
1083 22305327 : for ( k = 0; k < L_FRAME16k; k++ )
1084 : {
1085 22235840 : exc16kWhtnd[k] = White_exc16k[k] * scale;
1086 : }
1087 :
1088 69487 : preemph( exc16kWhtnd, PREEMPH_FAC, L_FRAME16k, tbe_premph );
1089 : }
1090 : else
1091 : {
1092 1218218 : 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 6388206 : for ( i = 0, k = 0; i < nbSubFr; i++ )
1095 : {
1096 5169988 : if ( coder_type == VOICED && extl_brate < SWB_TBE_2k8 )
1097 : {
1098 282388 : temp = (float) sqrt( voice_factors[i] );
1099 282388 : temp1 = (float) sqrt( temp );
1100 282388 : temp2 = (float) sqrt( ( pow1 * ( 1.0f - temp ) ) / pow22 );
1101 282388 : if ( pow22 == 0.f )
1102 : {
1103 0 : temp2 = 0;
1104 : }
1105 : }
1106 : else
1107 : {
1108 : /* Adjust noise mixing for formant sharpening filter */
1109 4887600 : vf_tmp = SWB_NOISE_MIX_FAC * formant_fac;
1110 4887600 : vf_tmp = voice_factors[i] * ( 1.0f - vf_tmp );
1111 :
1112 4887600 : temp1 = (float) sqrt( vf_tmp );
1113 4887600 : temp2 = (float) sqrt( ( pow1 * ( 1.0f - vf_tmp ) ) / pow22 );
1114 4887600 : if ( pow22 == 0.f )
1115 : {
1116 0 : temp2 = 0;
1117 : }
1118 : }
1119 :
1120 394999748 : for ( j = 0; j < L_FRAME16k / nbSubFr; j++, k++ )
1121 : {
1122 389829760 : exc16kWhtnd[k] = temp1 * exc16kWhtnd[k] + temp2 * White_exc16k[k];
1123 : }
1124 :
1125 5169988 : temp = (float) sqrt( 1.0f - voice_factors[i] );
1126 5169988 : temp = PREEMPH_FAC * temp / ( temp1 + temp );
1127 :
1128 5169988 : preemph( &exc16kWhtnd[i * L_FRAME16k / nbSubFr], temp, L_FRAME16k / nbSubFr, tbe_premph );
1129 : }
1130 : }
1131 : }
1132 :
1133 1307608 : if ( extl_brate < SWB_TBE_2k8 )
1134 : {
1135 1009804 : syn_filt( lpc_shb, LPC_SHB_ORDER, exc16kWhtnd, excSHB, L_FRAME16k, state_lpc_syn, 1 );
1136 : }
1137 : else
1138 : {
1139 297804 : set_f( zero_mem, 0, LPC_SHB_ORDER );
1140 297804 : syn_filt( lpc_shb_sf, LPC_SHB_ORDER, exc16kWhtnd, tempSHB, 80, zero_mem, 1 );
1141 297804 : syn_shb_ener_sf[0] = 0.125f * sum2_f( tempSHB, 80 );
1142 297804 : syn_filt( lpc_shb_sf + ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 80, tempSHB, 80, zero_mem, 1 );
1143 297804 : syn_shb_ener_sf[1] = 0.125f * sum2_f( tempSHB, 80 );
1144 297804 : syn_filt( lpc_shb_sf + 2 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 160, tempSHB, 80, zero_mem, 1 );
1145 297804 : syn_shb_ener_sf[2] = 0.125f * sum2_f( tempSHB, 80 );
1146 297804 : syn_filt( lpc_shb_sf + 3 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 240, tempSHB, 80, zero_mem, 1 );
1147 297804 : syn_shb_ener_sf[3] = 0.125f * sum2_f( tempSHB, 80 );
1148 :
1149 297804 : if ( total_brate <= MAX_ACELP_BRATE )
1150 : {
1151 297804 : 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 297804 : tempD = sqrt( tempSHB[0] );
1153 :
1154 95595084 : for ( i = 0; i < L_FRAME16k; i++ )
1155 : {
1156 95297280 : exc16kWhtnd[i] = (float) ( exc16kWhtnd[i] * tempD );
1157 : }
1158 : }
1159 :
1160 297804 : syn_filt( lpc_shb_sf, LPC_SHB_ORDER, exc16kWhtnd, excSHB, 80, state_lpc_syn, 1 );
1161 297804 : syn_filt( lpc_shb_sf + ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 80, excSHB + 80, 80, state_lpc_syn, 1 );
1162 297804 : syn_filt( lpc_shb_sf + 2 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, exc16kWhtnd + 160, excSHB + 160, 80, state_lpc_syn, 1 );
1163 297804 : 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 1307608 : if ( extl == FB_TBE )
1167 : {
1168 381533 : syn_filt( lpc_shb, LPC_SHB_ORDER, White_exc16k_FB, White_exc16k_FB_temp, L_FRAME16k, fb_state_lpc_syn, 1 );
1169 :
1170 122472093 : for ( i = 0; i < L_FRAME16k; i++ )
1171 : {
1172 122090560 : White_exc16k_FB_temp[i] *= cos_fb_exc[i % 32];
1173 : }
1174 :
1175 381533 : flip_spectrum( White_exc16k_FB_temp, White_exc16k_FB, L_FRAME16k );
1176 :
1177 381533 : deemph( White_exc16k_FB, fb_deemph_fac, L_FRAME16k, fb_tbe_demph );
1178 : }
1179 : else
1180 : {
1181 297270075 : for ( i = 0; i < L_FRAME16k; i++ )
1182 : {
1183 296344000 : White_exc16k_FB[i] = 0.0f;
1184 : }
1185 : }
1186 :
1187 1307608 : *prev_pow_exc16kWhtnd = pow1;
1188 1307608 : *prev_mix_factor = mix_factor;
1189 :
1190 1307608 : return;
1191 : }
1192 :
1193 : /*-------------------------------------------------------------------*
1194 : * GenSHBSynth()
1195 : *
1196 : * Generate 32 KHz sampled highband component from synthesized highband
1197 : *-------------------------------------------------------------------*/
1198 :
1199 1130626 : 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 1130626 : Interpolate_allpass_steep( input_synspeech, state_lsyn_filt_shb_local, L_FRAME16k, speech_buf_32k );
1211 :
1212 1130626 : if ( L_frame == L_FRAME )
1213 : {
1214 596521 : 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 342361305 : for ( i = 0; i < L_FRAME32k; i++ )
1219 : {
1220 341827200 : shb_syn_speech_32k[i] = ( ( i % 2 ) == 0 ) ? ( -speech_buf_32k[i] ) : ( speech_buf_32k[i] );
1221 : }
1222 : }
1223 :
1224 1130626 : return;
1225 : }
1226 :
1227 : /*-------------------------------------------------------------------*
1228 : * ScaleShapedSHB()
1229 : *
1230 : *
1231 : *-------------------------------------------------------------------*/
1232 :
1233 1415040 : 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 1415040 : l_frame = L_FRAME16k;
1250 1415040 : l_shb_lahead = L_SHB_LAHEAD;
1251 1415040 : skip = skip_bands_SWB_TBE;
1252 :
1253 1415040 : if ( length == SHB_OVERLAP_LEN / 2 )
1254 : {
1255 290111 : skip = skip_bands_WB_TBE;
1256 290111 : l_frame = L_FRAME16k / 4;
1257 290111 : l_shb_lahead = L_SHB_LAHEAD / 4;
1258 : }
1259 :
1260 : /* apply gain for each subframe, and store noise output signal using overlap-add */
1261 1415040 : set_f( mod_syn, 0, l_frame + l_shb_lahead );
1262 :
1263 1415040 : if ( length == SHB_OVERLAP_LEN / 2 )
1264 : {
1265 290111 : sum_gain = 0;
1266 1740666 : for ( k = 0; k < length / 2; k++ )
1267 : {
1268 1450555 : sum_gain = subwin[2 * k + 2] * subgain[0];
1269 1450555 : mod_syn[skip[0] + k] = synSHB[skip[0] + k] * sum_gain;
1270 1450555 : mod_syn[skip[0] + k + length / 2] = synSHB[skip[0] + k + length / 2] * subgain[0];
1271 : }
1272 2320888 : for ( i = 1; i < NUM_SHB_SUBFR / 2; i++ )
1273 : {
1274 22338547 : for ( k = 0; k < length; k++ )
1275 : {
1276 20307770 : sum_gain = subwin[k + 1] * subgain[i] + subwin[length - k - 1] * subgain[i - 1];
1277 20307770 : mod_syn[skip[i] + k] = synSHB[skip[i] + k] * sum_gain;
1278 : }
1279 : }
1280 1740666 : for ( k = 0; k < length / 2; k++ )
1281 : {
1282 1450555 : sum_gain = subwin[length - 2 * k - 2] * subgain[i - 1];
1283 1450555 : mod_syn[skip[i] + k] = synSHB[skip[i] + k] * sum_gain;
1284 : }
1285 : }
1286 : else
1287 : {
1288 1124929 : num_join = NUM_SHB_SUBFR / NUM_SHB_SUBGAINS;
1289 1124929 : join_length = num_join * length;
1290 23623509 : for ( k = 0, j = 0; k < length; k++ )
1291 : {
1292 22498580 : mod_syn[j] = synSHB[j] * subwin[k + 1] * subgain[0];
1293 22498580 : j++;
1294 : }
1295 4499716 : for ( i = 0; i < NUM_SHB_SUBGAINS - 1; i++ )
1296 : {
1297 205862007 : for ( k = 0; k < join_length - length; k++ )
1298 : {
1299 202487220 : mod_syn[j] = synSHB[j] * subgain[i * num_join];
1300 202487220 : j++;
1301 : }
1302 :
1303 70870527 : for ( k = 0; k < length; k++ )
1304 : {
1305 67495740 : mod_syn[j] = synSHB[j] * ( subwin[length - k - 1] * subgain[i * num_join] + subwin[k + 1] * subgain[( i + 1 ) * num_join] );
1306 67495740 : j++;
1307 : }
1308 : }
1309 68620669 : for ( k = 0; k < join_length - length; k++ )
1310 : {
1311 67495740 : mod_syn[j] = synSHB[j] * subgain[( NUM_SHB_SUBGAINS - 1 ) * num_join];
1312 67495740 : j++;
1313 : }
1314 23623509 : for ( k = 0; k < length; k++ )
1315 : {
1316 22498580 : mod_syn[j] = synSHB[j] * subwin[length - k - 1] * subgain[( NUM_SHB_SUBGAINS - 1 ) * num_join];
1317 22498580 : j++;
1318 : }
1319 : }
1320 :
1321 25364175 : for ( i = 0; i < l_shb_lahead; i++ )
1322 : {
1323 23949135 : synSHB[i] = mod_syn[i] * win[i] * frame_gain;
1324 23949135 : synSHB[i] += overlap[i];
1325 23949135 : synSHB[i + l_shb_lahead] = mod_syn[i] * frame_gain;
1326 : }
1327 :
1328 360652065 : for ( ; i < l_frame; i++ )
1329 : {
1330 359237025 : synSHB[i] = mod_syn[i] * frame_gain;
1331 : }
1332 :
1333 25364175 : for ( ; i < l_frame + l_shb_lahead; i++ )
1334 : {
1335 23949135 : overlap[i - l_frame] = mod_syn[i] * win[l_frame + l_shb_lahead - 1 - i] * frame_gain;
1336 : }
1337 :
1338 1415040 : return;
1339 : }
1340 :
1341 : /*-------------------------------------------------------------------*
1342 : * non_linearity()
1343 : *
1344 : * Apply a non linearity to the SHB excitation
1345 : * -------------------------------------------------------------------*/
1346 :
1347 2118855 : 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 2118855 : float max_val = 0.0;
1362 : float scale, temp;
1363 : float scale_step;
1364 : float *p_out;
1365 :
1366 2118855 : int16_t en_abs = 0;
1367 2118855 : float v_fac = 0, ths;
1368 : int16_t nframes;
1369 : float sc_factor;
1370 :
1371 2118855 : if ( L_frame == L_FRAME16k )
1372 : {
1373 883037 : nframes = NB_SUBFR16k;
1374 883037 : ths = 0.87f;
1375 : }
1376 : else
1377 : {
1378 1235818 : nframes = NB_SUBFR;
1379 1235818 : ths = 0.94f;
1380 : }
1381 :
1382 11477312 : for ( i = 0; i < nframes; i++ )
1383 : {
1384 9358457 : v_fac += voice_factors[i];
1385 : }
1386 2118855 : v_fac /= nframes;
1387 :
1388 2118855 : if ( coder_type == VOICED && v_fac > ths )
1389 : {
1390 142 : en_abs = 1;
1391 : }
1392 :
1393 2118855 : p_out = output + NL_BUFF_OFFSET; /* NL_BUFF_OFFSET = 12 */
1394 : /* update buffer memory */
1395 2118855 : mvr2r( old_bwe_exc_extended, output, NL_BUFF_OFFSET );
1396 :
1397 680152455 : for ( i = j = 0; i < length / 2; i++ )
1398 : {
1399 678033600 : if ( ( temp = (float) fabs( input[i] ) ) > max_val )
1400 : {
1401 15734463 : max_val = temp;
1402 15734463 : j = i;
1403 : }
1404 : }
1405 :
1406 2118855 : if ( max_val > 1.0f )
1407 : {
1408 2056183 : scale = 0.67f / max_val;
1409 : }
1410 : else
1411 : {
1412 62672 : scale = 0.67f;
1413 : }
1414 :
1415 2118855 : sc_factor = 1024.0f;
1416 2118855 : if ( element_mode > EVS_MONO )
1417 : {
1418 2088122 : sc_factor = (float) ( 1 << max( 13 - norm_s( j + 1 ), 0 ) ); /* Adapt the scaling factor allowed depending of max position */
1419 2088122 : sc_factor = max( sc_factor, 2.0f );
1420 : }
1421 :
1422 2118855 : if ( *prev_scale <= 0.0 || *prev_scale > sc_factor * scale )
1423 : {
1424 335331 : scale_step = 1.0;
1425 335331 : *prev_scale = scale;
1426 : }
1427 : else
1428 : {
1429 1783524 : scale_step = 1.0f;
1430 1783524 : if ( j != 0 )
1431 : {
1432 1724010 : scale_step = (float) exp( 1.0f / (float) j * (float) log( scale / *prev_scale ) );
1433 : }
1434 : }
1435 :
1436 680152455 : for ( i = 0; i < length / 2; i++ )
1437 : {
1438 678033600 : if ( input[i] >= 0.0 )
1439 : {
1440 347593617 : *p_out++ = ( input[i] * input[i] ) * *prev_scale;
1441 : }
1442 : else
1443 : {
1444 330439983 : if ( en_abs )
1445 : {
1446 20902 : *p_out++ = 1.0f * ( input[i] * input[i] ) * *prev_scale;
1447 : }
1448 : else
1449 : {
1450 330419081 : *p_out++ = -1.0f * ( input[i] * input[i] ) * *prev_scale;
1451 : }
1452 : }
1453 :
1454 678033600 : if ( i < j )
1455 : {
1456 331528333 : *prev_scale *= scale_step;
1457 : }
1458 : }
1459 :
1460 2118855 : max_val = 0.0f;
1461 680152455 : for ( i = j = length / 2; i < length; i++ )
1462 : {
1463 678033600 : if ( ( temp = (float) fabs( input[i] ) ) > max_val )
1464 : {
1465 15524797 : max_val = temp;
1466 15524797 : j = i;
1467 : }
1468 : }
1469 :
1470 2118855 : if ( max_val > 1.0f )
1471 : {
1472 2055975 : scale = 0.67f / max_val;
1473 : }
1474 : else
1475 : {
1476 62880 : scale = 0.67f;
1477 : }
1478 :
1479 2118855 : sc_factor = 1024.0f;
1480 2118855 : if ( element_mode > EVS_MONO )
1481 : {
1482 2088122 : sc_factor = (float) ( 1 << max( 12 - norm_s( j - length / 2 + 1 ), 0 ) ); /* allowed intra frame jump is smaller */
1483 2088122 : sc_factor = max( sc_factor, 2.0f );
1484 : }
1485 :
1486 2118855 : if ( *prev_scale <= 0.0 || *prev_scale > sc_factor * scale )
1487 : {
1488 5902 : scale_step = 1.0;
1489 5902 : *prev_scale = scale;
1490 : }
1491 : else
1492 : {
1493 2112953 : scale_step = 1.0f;
1494 2112953 : if ( j != length / 2 )
1495 : {
1496 2036217 : scale_step = (float) exp( 1.0f / (float) ( j - length / 2 ) * (float) log( scale / *prev_scale ) );
1497 : }
1498 : }
1499 :
1500 680152455 : for ( i = length / 2; i < length; i++ )
1501 : {
1502 678033600 : if ( input[i] >= 0.0 )
1503 : {
1504 344895281 : *p_out++ = ( input[i] * input[i] ) * *prev_scale;
1505 : }
1506 : else
1507 : {
1508 333138319 : if ( en_abs )
1509 : {
1510 21362 : *p_out++ = 1.0f * ( input[i] * input[i] ) * *prev_scale;
1511 : }
1512 : else
1513 : {
1514 333116957 : *p_out++ = -1.0f * ( input[i] * input[i] ) * *prev_scale;
1515 : }
1516 : }
1517 :
1518 678033600 : if ( i < j )
1519 : {
1520 307039053 : *prev_scale *= scale_step;
1521 : }
1522 : }
1523 :
1524 : /* update buffer memory */
1525 2118855 : mvr2r( output + L_FRAME32k, old_bwe_exc_extended, NL_BUFF_OFFSET );
1526 :
1527 2118855 : return;
1528 : }
1529 :
1530 :
1531 : /*-------------------------------------------------------------------*
1532 : * create_random_vector()
1533 : *
1534 : * creates random number vector
1535 : * -------------------------------------------------------------------*/
1536 :
1537 2865521 : 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 2865521 : j = (int16_t) ( own_random( &seed[0] ) * 0.0078f );
1547 2865521 : j = abs( j ) & 0xff;
1548 2865521 : k = (int16_t) ( own_random( &seed[1] ) * 0.0078f );
1549 2865521 : k = abs( k ) & 0xff;
1550 :
1551 2877044 : while ( k == j )
1552 : {
1553 11523 : k = (int16_t) ( own_random( &seed[1] ) * 0.0078f );
1554 11523 : k = abs( k ) & 0xff;
1555 : }
1556 :
1557 2865521 : if ( own_random( &seed[0] ) < 0 )
1558 : {
1559 1392474 : scale1 = -563.154f; /* -200.00f * 0.35f/0.1243f; */
1560 : }
1561 : else
1562 : {
1563 1473047 : scale1 = 563.154f; /* 200.00f * 0.35f/0.1243f; */
1564 : }
1565 :
1566 2865521 : if ( own_random( &seed[1] ) < 0 )
1567 : {
1568 1613680 : scale2 = -225.261f; /* -80.00f * 0.35f/0.1243f; */
1569 : }
1570 : else
1571 : {
1572 1251841 : scale2 = 225.261f; /* 80.00f * 0.35f/0.1243f; */
1573 : }
1574 :
1575 438140001 : for ( i = 0; i < length; i++, j++, k++ )
1576 : {
1577 435274480 : j &= 0xff;
1578 435274480 : k &= 0xff;
1579 435274480 : output[i] = scale1 * gaus_dico_swb[j] + scale2 * gaus_dico_swb[k];
1580 : }
1581 :
1582 2865521 : 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 4221173 : 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 4221173 : interp_code[0] = inp_code[0];
1605 4221173 : interp_code[1] = inp_code[0] * factor_i[3] + inp_code[1] * factor_j[3];
1606 4221173 : interp_code[2] = inp_code[0] * factor_i[4] + inp_code[1] * factor_j[4];
1607 :
1608 165369568 : for ( i = 3, kk = 1, kkp1 = 2; i < ( inp_length - 2 ) * HIBND_ACB_L_FAC; i += 5, kk++, kkp1++ )
1609 : {
1610 161148395 : interp_code[i] = inp_code[kk] * factor_j[0] + inp_code[kkp1] * factor_i[0];
1611 161148395 : interp_code[i + 1] = inp_code[kk] * factor_j[1] + inp_code[kkp1] * factor_i[1];
1612 161148395 : interp_code[i + 2] = inp_code[kkp1] * factor_i[2];
1613 161148395 : kk++;
1614 161148395 : kkp1++;
1615 161148395 : interp_code[i + 3] = inp_code[kk] * factor_i[3] + inp_code[kkp1] * factor_j[3];
1616 161148395 : interp_code[i + 4] = inp_code[kk] * factor_i[4] + inp_code[kkp1] * factor_j[4];
1617 : }
1618 :
1619 4221173 : interp_code[i] = inp_code[kk] * factor_j[0];
1620 4221173 : interp_code[i + 1] = inp_code[kk] * factor_j[1];
1621 :
1622 4221173 : 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 4253186 : 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 301884544 : for ( i = j = 0; i < inp_length - 1; i++, j += 2 )
1641 : {
1642 297631358 : interp_code[j] = inp_code[i];
1643 297631358 : interp_code[j + 1] = inp_code[i] * 0.5f + inp_code[i + 1] * 0.5f;
1644 : }
1645 :
1646 4253186 : interp_code[j] = inp_code[i];
1647 4253186 : interp_code[j + 1] = inp_code[i] * 0.5f;
1648 :
1649 4253186 : 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 1663450 : void fb_tbe_reset_synth(
1659 : float fbbwe_hpf_mem[][4],
1660 : float *prev_fbbwe_ratio )
1661 : {
1662 1663450 : set_f( fbbwe_hpf_mem[0], 0, 4 );
1663 1663450 : set_f( fbbwe_hpf_mem[1], 0, 4 );
1664 1663450 : set_f( fbbwe_hpf_mem[2], 0, 4 );
1665 1663450 : set_f( fbbwe_hpf_mem[3], 0, 4 );
1666 1663450 : *prev_fbbwe_ratio = 1.0f;
1667 :
1668 1663450 : return;
1669 : }
1670 :
1671 : /*-------------------------------------------------------------------*
1672 : * wb_tbe_extras_reset()
1673 : *
1674 : * Reset the extra parameters only required for WB TBE encoding
1675 : *-------------------------------------------------------------------*/
1676 :
1677 970788 : void wb_tbe_extras_reset(
1678 : float mem_genSHBexc_filt_down_wb2[],
1679 : float mem_genSHBexc_filt_down_wb3[] )
1680 : {
1681 970788 : set_f( mem_genSHBexc_filt_down_wb2, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
1682 970788 : set_f( mem_genSHBexc_filt_down_wb3, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
1683 :
1684 970788 : 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 930154 : 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 930154 : set_f( state_lsyn_filt_shb, 0.0f, 2 * ALLPASSSECTIONS_STEEP );
1699 930154 : set_f( state_lsyn_filt_dwn_shb, 0.0f, 2 * ALLPASSSECTIONS_STEEP );
1700 930154 : set_f( mem_resamp_HB, 0.0f, INTERP_3_1_MEM_LEN );
1701 :
1702 930154 : 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 362825 : 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 362825 : 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 362825 : 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 362825 : 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 362825 : 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 347223525 : for ( i = 4; i < L_FRAME48k; i++ )
1728 : {
1729 346860700 : 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 362825 : memory[0][0] = input[L_FRAME48k - 4];
1733 362825 : memory[0][1] = input[L_FRAME48k - 3];
1734 362825 : memory[0][2] = input[L_FRAME48k - 2];
1735 362825 : memory[0][3] = input[L_FRAME48k - 1];
1736 :
1737 362825 : 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 362825 : 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 362825 : 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 362825 : 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 347223525 : for ( i = 4; i < L_FRAME48k; i++ )
1743 : {
1744 346860700 : 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 362825 : memory[1][0] = tmp[L_FRAME48k - 4];
1748 362825 : memory[1][1] = tmp[L_FRAME48k - 3];
1749 362825 : memory[1][2] = tmp[L_FRAME48k - 2];
1750 362825 : memory[1][3] = tmp[L_FRAME48k - 1];
1751 :
1752 362825 : 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 362825 : 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 362825 : 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 362825 : 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 347223525 : for ( i = 4; i < L_FRAME48k; i++ )
1758 : {
1759 346860700 : 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 362825 : memory[2][0] = tmp2[L_FRAME48k - 4];
1763 362825 : memory[2][1] = tmp2[L_FRAME48k - 3];
1764 362825 : memory[2][2] = tmp2[L_FRAME48k - 2];
1765 362825 : memory[2][3] = tmp2[L_FRAME48k - 1];
1766 :
1767 362825 : memory[3][0] = output[L_FRAME48k - 4];
1768 362825 : memory[3][1] = output[L_FRAME48k - 3];
1769 362825 : memory[3][2] = output[L_FRAME48k - 2];
1770 362825 : memory[3][3] = output[L_FRAME48k - 1];
1771 :
1772 362825 : 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 269928 : 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 86646888 : for ( i = 0, j = 0; i < L_FRAME48k; i += 3, j++ )
1810 : {
1811 86376960 : excitation_in_interp3[i] = 3.0f * excitation_in[j];
1812 86376960 : excitation_in_interp3[i + 1] = 0.0f;
1813 86376960 : excitation_in_interp3[i + 2] = 0.0f;
1814 : }
1815 :
1816 269928 : if ( L_frame == L_FRAME16k )
1817 : {
1818 : /* for 16kHz ACELP core */
1819 225897 : 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 44031 : elliptic_bpf_48k_generic( excitation_in_interp3, tmp, bpf_memory, full_band_bpf_1 );
1825 : }
1826 269928 : temp1 = sum2_f( tmp, L_FRAME48k ) + 0.001f;
1827 269928 : ratio2 = (float) ( ratio * sqrt( fb_exc_energy / temp1 ) );
1828 :
1829 269928 : if ( !bfi )
1830 : {
1831 263217 : *prev_fbbwe_ratio = ratio;
1832 : }
1833 : else
1834 : {
1835 6711 : *prev_fbbwe_ratio = ratio * 0.5f;
1836 : }
1837 259400808 : for ( i = 0; i < L_FRAME48k; i++ )
1838 : {
1839 259130880 : output[i] = tmp[i] * ratio2;
1840 : }
1841 :
1842 269928 : return;
1843 : }
1844 :
1845 : /*-------------------------------------------------------------------*
1846 : * Estimate_mix_factors() *
1847 : * *
1848 : * Estimate mix factors for SHB excitation generation *
1849 : *-------------------------------------------------------------------*/
1850 :
1851 66819 : 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 66819 : mvr2r( shb_res, shb_res_local, L_FRAME16k );
1867 66819 : mvr2r( White_exc16k, WN_exc_local, L_FRAME16k );
1868 :
1869 66819 : pow3 = dotp( shb_res_local, shb_res_local, L_FRAME16k );
1870 :
1871 66819 : pow3 += 0.00001f;
1872 66819 : temp_p1_p2 = (float) sqrt( pow1 / pow22 );
1873 66819 : temp_p1_p3 = (float) sqrt( pow1 / pow3 );
1874 :
1875 :
1876 21448899 : for ( i = 0; i < L_FRAME16k; i++ )
1877 : {
1878 21382080 : WN_exc_local[i] *= temp_p1_p2;
1879 21382080 : shb_res_local[i] *= temp_p1_p3;
1880 : }
1881 21448899 : for ( i = 0; i < L_FRAME16k; i++ )
1882 : {
1883 21382080 : temp_numer1[i] = shb_res_local[i] - WN_exc_local[i];
1884 21382080 : temp_numer2[i] = exc16kWhtnd[i] - WN_exc_local[i];
1885 : }
1886 :
1887 66819 : length = L_FRAME16k;
1888 133638 : for ( i = 0; i < 1; i++ )
1889 : {
1890 66819 : temp_p1_p2 = dotp( temp_numer1 + i * length, temp_numer2 + i * length, length );
1891 66819 : temp_p1_p3 = dotp( temp_numer2 + i * length, temp_numer2 + i * length, length );
1892 66819 : vf_modified[i] = min( max( ( temp_p1_p2 / temp_p1_p3 ), 0.1f ), 0.99f );
1893 : }
1894 :
1895 66819 : *vf_ind = usquant( vf_modified[0], &temp_p1_p2, 0.125, 0.125, 1 << NUM_BITS_SHB_VF );
1896 66819 : set_f( vf_modified, temp_p1_p2, NB_SUBFR16k );
1897 :
1898 66819 : return;
1899 : }
1900 :
1901 : /*-------------------------------------------------------------------*
1902 : * tbe_celp_exc() *
1903 : * *
1904 : * Prepare adaptive part of TBE excitation *
1905 : *-------------------------------------------------------------------*/
1906 :
1907 6808347 : 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 6808347 : if ( element_mode == IVAS_CPE_TD && idchan == 1 && !tdm_LRTD_flag )
1923 : {
1924 3380 : return;
1925 : }
1926 :
1927 6804967 : assert( bwe_exc != NULL && "BWE excitation is NULL" );
1928 :
1929 6804967 : if ( L_frame == L_FRAME )
1930 : {
1931 3261472 : offset = tbe_celp_exc_offset( T0, T0_frac );
1932 :
1933 525096992 : for ( i = 0; i < L_subfr * HIBND_ACB_L_FAC; i++ )
1934 : {
1935 521835520 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset + (int16_t) *error];
1936 : }
1937 3261472 : *error += (float) offset - (float) T0 * HIBND_ACB_L_FAC - 0.25f * HIBND_ACB_L_FAC * (float) T0_frac;
1938 : }
1939 : else
1940 : {
1941 3543495 : offset = T0 * 2 + (int16_t) ( (float) T0_frac * 0.5f + 4 + 0.5f ) - 4;
1942 457110855 : for ( i = 0; i < L_subfr * 2; i++ )
1943 : {
1944 453567360 : bwe_exc[i + i_subfr * 2] = bwe_exc[i + i_subfr * 2 - offset + (int16_t) *error];
1945 : }
1946 3543495 : *error += (float) offset - (float) T0 * 2 - 0.5f * (float) T0_frac;
1947 : }
1948 :
1949 6804967 : return;
1950 : }
1951 :
1952 : /*-------------------------------------------------------------------*
1953 : * prep_tbe_exc() *
1954 : * *
1955 : * Prepare TBE excitation *
1956 : *-------------------------------------------------------------------*/
1957 :
1958 7740513 : 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 7740513 : float tmp = 1.0f;
1983 :
1984 7740513 : *voice_factors = VF_0th_PARAM + VF_1st_PARAM * voice_fac + VF_2nd_PARAM * voice_fac * voice_fac;
1985 :
1986 7740513 : if ( ( coder_type == VOICED || T0 > 115.5f ) && core_brate > ACELP_8k00 )
1987 : {
1988 2015587 : tmp = 1.0f;
1989 2015587 : *voice_factors *= tmp;
1990 : }
1991 :
1992 7740513 : *voice_factors = min( max( 0.000001f, *voice_factors ), 0.999999f );
1993 :
1994 7740513 : if ( element_mode == IVAS_CPE_TD && idchan == 1 && !tdm_LRTD_flag )
1995 : {
1996 3380 : if ( flag_TD_BWE && i_subfr == 0 )
1997 : {
1998 0 : set_f( bwe_exc, 0, L_FRAME32k );
1999 : }
2000 :
2001 3380 : return;
2002 : }
2003 :
2004 7737133 : if ( L_frame == L_FRAME )
2005 : {
2006 3698488 : interp_code_5over2( code, tmp_code, L_subfr );
2007 :
2008 595456568 : for ( i = 0; i < L_subfr * HIBND_ACB_L_FAC; i++ )
2009 : {
2010 591758080 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = gain_pit * bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] +
2011 591758080 : gain_code * tmp_code[i];
2012 : }
2013 : }
2014 : else
2015 : {
2016 262511925 : for ( i = 0; i < L_subfr; i++ )
2017 : {
2018 258473280 : tmp_code_preInt[i] = gain_code * code[i] + 2 * gain_preQ * code_preQ[i];
2019 : }
2020 :
2021 4038645 : interp_code_4over2( tmp_code_preInt, tmp_code, L_subfr );
2022 :
2023 520985205 : for ( i = 0; i < L_subfr * 2; i++ )
2024 : {
2025 516946560 : bwe_exc[i + i_subfr * 2] = gain_pit * bwe_exc[i + i_subfr * 2] + tmp_code[i];
2026 : }
2027 : }
2028 :
2029 7737133 : return;
2030 : }
2031 :
2032 :
2033 : /*-------------------------------------------------------------------*
2034 : * get_tbe_bits() *
2035 : * *
2036 : * Determine TBE bit consumption per frame from bitrate *
2037 : *-------------------------------------------------------------------*/
2038 :
2039 24519 : int16_t get_tbe_bits(
2040 : const int32_t total_brate,
2041 : const int16_t bwidth,
2042 : const int16_t rf_mode )
2043 : {
2044 24519 : int16_t i, bits = 0;
2045 :
2046 24519 : if ( rf_mode )
2047 : {
2048 : /* TBE bits for core, primary frame */
2049 8056 : if ( bwidth == WB && total_brate == ACELP_13k20 )
2050 : {
2051 : /* Gain frame: 4, Gain shapes: 0, and LSFs: 2 */
2052 4216 : bits = NUM_BITS_SHB_FrameGain_LBR_WB + NUM_BITS_LBR_WB_LSF;
2053 : }
2054 3840 : else if ( bwidth == SWB && total_brate == ACELP_13k20 )
2055 : {
2056 : /* Gain frame: 5, Gain shapes: 5, and lowrate LSFs: 8 */
2057 3840 : bits = NUM_BITS_SHB_FRAMEGAIN + NUM_BITS_SHB_SUBGAINS + 8;
2058 : }
2059 : }
2060 : else
2061 : {
2062 16463 : if ( bwidth == WB && total_brate == ACELP_9k60 )
2063 : {
2064 1488 : bits = NUM_BITS_LBR_WB_LSF + NUM_BITS_SHB_FrameGain_LBR_WB;
2065 : }
2066 14975 : else if ( bwidth == SWB || bwidth == FB )
2067 : {
2068 13700 : if ( total_brate == ACELP_9k60 )
2069 : {
2070 920 : bits = NUM_BITS_SHB_FRAMEGAIN + NUM_BITS_SHB_SUBGAINS + 8;
2071 : }
2072 12780 : else if ( total_brate >= ACELP_13k20 && total_brate <= ACELP_32k )
2073 : {
2074 12780 : bits = NUM_BITS_SHB_SUBGAINS + NUM_BITS_SHB_FRAMEGAIN + NUM_LSF_GRID_BITS + MIRROR_POINT_BITS;
2075 :
2076 76680 : for ( i = 0; i < NUM_Q_LSF; i++ )
2077 : {
2078 63900 : bits += lsf_q_num_bits[i];
2079 : }
2080 : }
2081 :
2082 13700 : if ( total_brate >= ACELP_24k40 )
2083 : {
2084 11244 : bits += NUM_BITS_SHB_ENER_SF + NUM_BITS_SHB_VF + NUM_BITS_SHB_RES_GS * NB_SUBFR16k;
2085 : }
2086 :
2087 13700 : if ( bwidth == SWB && ( total_brate == ACELP_16k40 || total_brate == ACELP_24k40 ) )
2088 : {
2089 11340 : bits += BITS_TEC + BITS_TFA;
2090 : }
2091 :
2092 13700 : if ( bwidth == FB )
2093 : {
2094 : /* fullband slope */
2095 1440 : bits += 4;
2096 : }
2097 : }
2098 : }
2099 :
2100 24519 : return bits;
2101 : }
|