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 "rom_enc.h"
47 : #include "wmc_auto.h"
48 : #include "ivas_prot.h"
49 :
50 : /*-----------------------------------------------------------------*
51 : * Local constants
52 : *-----------------------------------------------------------------*/
53 :
54 : #define ENVSHBRES_ACORR_MIN 40 /* minimum lag for calculating autocorrelation function on SHB residual TD envelope */
55 : #define ENVSHBRES_ACORR_MAX 80 /* maximum lag for calculating autocorrelation function on SHB residual TD envelope */
56 :
57 :
58 : /*-----------------------------------------------------------------*
59 : * Local function prototypes
60 : *-----------------------------------------------------------------*/
61 :
62 : static void return_M_Least( const float *inp, const int16_t n_cols, const float *codebook, const int16_t num_grp, const float *weight, const int16_t interNum, int16_t *least );
63 : static void singlevectortest_gain( const float *inp, const int16_t dimen, const int16_t cb_size, int16_t *index, const float *weight, float *recon, const float *codebook );
64 : static void determine_gain_weights( const float *gain, float *weights, const int16_t dims );
65 : static void QuantizeSHBsubgains( Encoder_State *st, float *subgains, const int16_t extl );
66 : static void QuantizeSHBframegain( Encoder_State *st, float *GainFrame, const int16_t extl, const int32_t extl_brate, int16_t *rf_gainFrame_ind, const int16_t flag_conservative );
67 : static int16_t closest_centroid( const float *data, const float *weights, const float *quantizer, const int16_t centroids, const int16_t length );
68 : static void EstimateSHBFrameGain( const int16_t length, const float *oriSHB, const float *synSHB, float *subgain, float *GainFrame, const float *win_shb, const float *subwin );
69 : static void EstimateSHBGainShape( const int16_t length, const float *oriSHB, const float *synSHB, float *subgain, const float *subwin );
70 : static float pow_off_pk( float a[], const int16_t len, const int16_t step );
71 : static float pow_off_pk_corrected( float a[], const int16_t len, const int16_t step );
72 : static void Quant_lower_LSF( const float lsf[], float lsf_q[], int16_t lsf_idx[] );
73 : static int16_t Quant_mirror_point( const float lsf[], const float lsf_q[], float *m );
74 : static int16_t Find_LSF_grid( const float lsf[], float lsf_q[], const float m );
75 : static void gainFrSmooth_En( TD_BWE_ENC_HANDLE hBWE_TD, const int16_t last_extl, float *shb_frame, const float *lpc_shb, const float *lsp_shb, float *MA_lsp_shb_spacing, int16_t *frGainAttenuate, int16_t *frGainSmoothEn );
76 : static void Quant_BWE_LSF( BSTR_ENC_HANDLE hBstr, TD_BWE_ENC_HANDLE hBWE_TD, const int16_t codec_mode, const float lsp_shb[], float Q_lsfs[], const int32_t extl_brate );
77 : static void Quant_shb_ener_sf( Encoder_State *st, float *shb_ener_sf );
78 : static void Quant_shb_res_gshape( Encoder_State *st, float *shb_res_gshape );
79 : static void LVQQuant_BWE_LSF( BSTR_ENC_HANDLE hBstr, const float lsf_shb[], float Q_lsfs[], int16_t nbits );
80 :
81 :
82 : /*-------------------------------------------------------------------*
83 : * InitSWBencBuffer()
84 : *
85 : * Initialize SWB buffers and parameters
86 : *-------------------------------------------------------------------*/
87 :
88 76346 : void InitSWBencBuffer(
89 : TD_BWE_ENC_HANDLE hBWE_TD /* i/o: TD BWE data handle */
90 : )
91 : {
92 : int16_t i;
93 :
94 76346 : set_f( hBWE_TD->old_bwe_exc, 0.0f, ( PIT16k_MAX * 2 ) );
95 76346 : hBWE_TD->bwe_seed[0] = 23;
96 76346 : hBWE_TD->bwe_seed[1] = 59;
97 76346 : set_f( hBWE_TD->old_bwe_exc_extended, 0.0f, NL_BUFF_OFFSET );
98 76346 : hBWE_TD->bwe_non_lin_prev_scale = 0;
99 :
100 76346 : set_f( hBWE_TD->state_ana_filt_shb, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
101 :
102 76346 : set_f( hBWE_TD->elliptic_bpf_2_48k_mem[0], 0.0f, 4 );
103 76346 : set_f( hBWE_TD->elliptic_bpf_2_48k_mem[1], 0.0f, 4 );
104 76346 : set_f( hBWE_TD->elliptic_bpf_2_48k_mem[2], 0.0f, 4 );
105 76346 : set_f( hBWE_TD->elliptic_bpf_2_48k_mem[3], 0.0f, 4 );
106 76346 : hBWE_TD->prev_fb_energy = 0.0f;
107 :
108 76346 : set_f( hBWE_TD->old_speech_shb, 0.0f, L_LOOK_16k + L_SUBFR16k );
109 76346 : set_f( hBWE_TD->old_speech_wb, 0.0f, ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16 );
110 76346 : set_f( hBWE_TD->old_input_fhb, 0.0f, NS2SA( 48000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) - L_FRAME48k / 2 );
111 :
112 76346 : InitSWBencBufferStates( hBWE_TD, NULL );
113 :
114 839806 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
115 : {
116 763460 : hBWE_TD->prev_lsp_shb[i] = i / 20.0f;
117 : }
118 :
119 76346 : hBWE_TD->cldfbHBLT = 1.0f;
120 76346 : hBWE_TD->prev_gainFr_SHB = 0;
121 76346 : set_f( hBWE_TD->lsp_shb_slow_interpl, 0, LPC_SHB_ORDER );
122 76346 : set_f( hBWE_TD->lsp_shb_fast_interpl, 0, LPC_SHB_ORDER );
123 76346 : set_f( hBWE_TD->shb_inv_filt_mem, 0, LPC_SHB_ORDER );
124 76346 : set_f( hBWE_TD->lsp_shb_spacing, 0.1f, 3 );
125 76346 : hBWE_TD->prev_swb_GainShape = 0;
126 76346 : hBWE_TD->prev_frGainAtten = 0;
127 76346 : hBWE_TD->prev_wb_GainShape = 0;
128 76346 : set_f( hBWE_TD->fb_state_lpc_syn, 0, LPC_SHB_ORDER );
129 76346 : hBWE_TD->fb_tbe_demph = 0.0f;
130 76346 : hBWE_TD->tilt_mem = 0.0f;
131 :
132 76346 : hBWE_TD->prev_coder_type = GENERIC;
133 76346 : set_f( hBWE_TD->prev_lsf_diff, 0.5f, LPC_SHB_ORDER - 2 );
134 76346 : hBWE_TD->prev_tilt_para = 0.0f;
135 76346 : set_f( hBWE_TD->cur_sub_Aq, 0.0f, M + 1 );
136 :
137 : /* TD BWE post-processing */
138 76346 : hBWE_TD->ptr_mem_stp_swb = hBWE_TD->mem_stp_swb + LPC_SHB_ORDER - 1;
139 76346 : set_f( hBWE_TD->mem_zero_swb, 0, LPC_SHB_ORDER );
140 :
141 839806 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
142 : {
143 763460 : hBWE_TD->swb_lsp_prev_interp[i] = (float) cos( (float) i * EVS_PI / (float) 10.0f );
144 : }
145 :
146 76346 : set_f( hBWE_TD->dec_2_over_3_mem, 0.0f, L_FILT_2OVER3 );
147 76346 : set_f( hBWE_TD->dec_2_over_3_mem_lp, 0.0f, L_FILT_2OVER3_LP );
148 :
149 76346 : return;
150 : }
151 :
152 :
153 : /*-------------------------------------------------------------------*
154 : * InitSWBencBufferStates()
155 : *
156 : * Initialize SWB buffer states
157 : *-------------------------------------------------------------------*/
158 :
159 124478 : void InitSWBencBufferStates(
160 : TD_BWE_ENC_HANDLE hBWE_TD, /* i/o: TD BWE data handle */
161 : float *shb_speech /* o : SHB target signal (6-14kHz) at 16kHz */
162 : )
163 : {
164 124478 : if ( shb_speech != NULL )
165 : {
166 12763 : set_f( shb_speech, 0.0f, L_FRAME16k );
167 : }
168 :
169 124478 : set_f( hBWE_TD->old_speech_shb, 0.0f, L_LOOK_16k + L_SUBFR16k );
170 124478 : set_f( hBWE_TD->mem_shb_res, 0.0f, MAX_LEN_MA_FILTER );
171 124478 : set_f( hBWE_TD->old_EnvSHBres, 0.0f, L_FRAME4k );
172 124478 : hBWE_TD->old_mean_EnvSHBres = 0.0f;
173 124478 : hBWE_TD->prev_enr_EnvSHBres = 1.0f;
174 124478 : hBWE_TD->prev_shb_env_tilt = 0.0f;
175 124478 : hBWE_TD->prev_pow_exc16kWhtnd = 1.0f;
176 124478 : hBWE_TD->prev_mix_factor = 1.0f;
177 124478 : hBWE_TD->prev_Env_error = 0.0f;
178 :
179 124478 : return;
180 : }
181 :
182 :
183 : /*-------------------------------------------------------------------*
184 : * ResetSHBbuffer_Enc()
185 : *
186 : *-------------------------------------------------------------------*/
187 :
188 76346 : void ResetSHBbuffer_Enc(
189 : TD_BWE_ENC_HANDLE hBWE_TD /* i/o: TD BWE data handle */
190 : )
191 : {
192 : /* states for the filters used in generating SHB excitation from WB excitation*/
193 76346 : set_f( hBWE_TD->mem_genSHBexc_filt_down_shb, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
194 76346 : set_f( hBWE_TD->mem_csfilt, 0, 2 );
195 :
196 : /* states for the filters used in generating SHB signal from SHB excitation*/
197 76346 : set_f( hBWE_TD->state_syn_shbexc, 0, L_SHB_LAHEAD );
198 76346 : set_f( hBWE_TD->state_lpc_syn, 0, LPC_SHB_ORDER );
199 76346 : set_f( hBWE_TD->fb_state_lpc_syn, 0, LPC_SHB_ORDER );
200 76346 : hBWE_TD->fb_tbe_demph = 0;
201 :
202 : /* states for the filters used in generating WB signal from WB excitation*/
203 76346 : set_f( hBWE_TD->decim_state1, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
204 76346 : set_f( hBWE_TD->decim_state2, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
205 76346 : set_f( hBWE_TD->mem_genSHBexc_filt_down_wb2, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
206 76346 : set_f( hBWE_TD->mem_genSHBexc_filt_down_wb3, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
207 :
208 : /* overlap buffer used to Adjust SHB Frame Gain */
209 76346 : set_f( hBWE_TD->mem_stp_swb, 0, LPC_SHB_ORDER );
210 76346 : hBWE_TD->gain_prec_swb = 1.0f;
211 76346 : set_f( hBWE_TD->syn_overlap, 0, L_SHB_LAHEAD );
212 76346 : hBWE_TD->tbe_demph = 0.0f;
213 76346 : hBWE_TD->tbe_premph = 0.0f;
214 :
215 76346 : return;
216 : }
217 :
218 :
219 : /*-------------------------------------------------------------------*
220 : * wb_tbe_enc()
221 : *
222 : * WB TBE encoder, 6 - 8 kHz band encoding module
223 : *-------------------------------------------------------------------*/
224 :
225 75078 : void wb_tbe_enc(
226 : Encoder_State *st, /* i/o: encoder state structure */
227 : const float *hb_speech, /* i : HB target signal (6-8kHz) at 16kHz */
228 : const float *bwe_exc_extended, /* i : bandwidth extended exciatation */
229 : const float voice_factors[], /* i : voicing factors */
230 : const float pitch_buf[] /* i : pitch for each subframe */
231 : )
232 : {
233 : int16_t i, j, k, delay;
234 : TD_BWE_ENC_HANDLE hBWE_TD;
235 : BSTR_ENC_HANDLE hBstr;
236 : float hb_old_speech[( L_LOOK_12k8 + L_SUBFR + L_FRAME ) * 5 / 16];
237 : float shaped_wb_excitation[( L_FRAME16k + L_SHB_LAHEAD ) / 4];
238 : float exc4kWhtnd[L_FRAME16k / 4];
239 75078 : int16_t ana_align_delay = -L_SHB_LAHEAD / 4 - 5;
240 : float GainFrame, GainShape[NUM_SHB_SUBFR];
241 : float lpc_wb[LPC_SHB_ORDER_WB + 1];
242 75078 : float lsp_wb[LPC_SHB_ORDER_WB], weights_lsp[LPC_SHB_ORDER_WB] = { 1.0, 1.0 };
243 : float *hb_new_speech, *hb_frame;
244 : float R[LPC_SHB_ORDER_WB + 2], ervec[LPC_SHB_ORDER_WB + 1];
245 : float prev_pow, curr_pow, scale;
246 : float p2m_in, p2m_out;
247 : int16_t uv_flag;
248 : float pitBufAvg, voicingBufAvg;
249 : float vf_modified[NB_SUBFR16k];
250 : float temp_wb_fac, feedback;
251 : float lsp_spacing;
252 : float ervec_temp[LPC_SHB_ORDER_WB + 1];
253 : float lsp_wb_temp[LPC_SHB_ORDER_WB], lpc_wb_temp[LPC_SHB_ORDER_WB + 1];
254 :
255 75078 : hBWE_TD = st->hBWE_TD;
256 75078 : hBstr = st->hBstr;
257 :
258 : /* delay alignment */
259 75078 : delay = ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16;
260 :
261 75078 : hb_new_speech = hb_old_speech + delay;
262 75078 : hb_frame = hb_old_speech + L_SUBFR * 5 / 16 + ana_align_delay;
263 :
264 75078 : mvr2r( hBWE_TD->old_speech_wb, hb_old_speech, delay );
265 75078 : mvr2r( hb_speech, hb_new_speech, L_FRAME16k / 4 );
266 75078 : mvr2r( hb_old_speech + L_FRAME16k / 4, hBWE_TD->old_speech_wb, delay );
267 :
268 75078 : if ( ( st->last_extl != WB_TBE && st->last_extl != WB_BWE ) &&
269 1792 : ( st->clas == UNVOICED_CLAS || ( st->voicing[0] < 0.5f && st->voicing[1] < 0.5f && st->voicing[2] < 0.5f ) ) &&
270 1100 : ( !st->igf ) )
271 : {
272 : /* In case of unvoiced signals after switching cores, back-propagate the target signal */
273 1025 : mvr2r( hb_speech, hb_old_speech, delay );
274 :
275 21525 : for ( i = ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16, j = k = 0; j < L_SUBFR16k; i--, j += 4, k++ )
276 : {
277 20500 : hb_old_speech[i] *= ola_win_shb_switch_fold[j];
278 20500 : hb_old_speech[i] += hb_speech[k] * ola_win_shb_switch_fold[L_SUBFR16k - 4 - j];
279 : }
280 : }
281 :
282 75078 : autocorr( hb_old_speech, R, LPC_SHB_ORDER_WB + 1, ( NS2SA( INT_FS_12k8, 5000000L ) + L_SUBFR + L_FRAME ) * 5 / 16, win_lpc_hb_wb, 0, 1, 1 );
283 :
284 75078 : if ( st->element_mode > EVS_MONO )
285 : {
286 : /* Ensure R[0] isn't zero when entering Levinson-Durbin */
287 71146 : R[0] = max( R[0], 1.0e-8f );
288 : }
289 :
290 75078 : lev_dur( lpc_wb_temp, R, LPC_SHB_ORDER_WB, ervec_temp );
291 75078 : a2lsp( lsp_wb_temp, lpc_wb_temp, LPC_SHB_ORDER_WB );
292 75078 : lsp_spacing = 0.5f;
293 :
294 525546 : for ( i = 0; i < LPC_SHB_ORDER_WB; i++ )
295 : {
296 450468 : lsp_spacing = min( lsp_spacing, (float) ( i == 0 ? lsp_wb_temp[0] : ( lsp_wb_temp[i] - lsp_wb_temp[i - 1] ) ) );
297 : }
298 :
299 : /* Spectral smoothing of autocorrelation coefficients */
300 600624 : for ( i = 0; i <= LPC_SHB_ORDER_WB; i++ )
301 : {
302 525546 : R[i] = R[i] * wac[i];
303 : }
304 75078 : R[0] = max( R[0], 1.0e-8f );
305 :
306 75078 : if ( st->rf_mode == 1 || st->extl_brate == WB_TBE_0k35 )
307 : {
308 13577 : lev_dur( lpc_wb, R, LPC_SHB_ORDER_LBR_WB, ervec );
309 :
310 : /* Expand bandwidth of the LP coeffs */
311 81462 : for ( i = 0; i <= LPC_SHB_ORDER_LBR_WB; i++ )
312 : {
313 67885 : lpc_wb[i] *= lpc_weights[i];
314 : }
315 :
316 : /* convert into lsps and calculate weights */
317 13577 : a2lsp( lsp_wb, lpc_wb, LPC_SHB_ORDER_LBR_WB );
318 13577 : lsp_weights( lsp_wb, weights_lsp, LPC_SHB_ORDER_LBR_WB );
319 :
320 : /* Quantization of LSFs */
321 13577 : i = closest_centroid( lsp_wb, weights_lsp, lbr_wb_bwe_lsfvq_cbook_2bit, 4, LPC_SHB_ORDER_LBR_WB );
322 13577 : if ( st->codec_mode == MODE2 )
323 : {
324 2138 : hBWE_TD->lsf_WB = i;
325 : }
326 : else
327 : {
328 11439 : push_indice( hBstr, IND_SHB_LSF, i, NUM_BITS_LBR_WB_LSF );
329 : }
330 :
331 13577 : mvr2r( lbr_wb_bwe_lsfvq_cbook_2bit + i * LPC_SHB_ORDER_LBR_WB, lsp_wb, LPC_SHB_ORDER_LBR_WB );
332 :
333 13577 : lsp2a( lpc_wb, lsp_wb, LPC_SHB_ORDER_LBR_WB );
334 13577 : set_f( lpc_wb + LPC_SHB_ORDER_LBR_WB + 1, 0.0f, ( LPC_SHB_ORDER_WB - LPC_SHB_ORDER_LBR_WB ) );
335 : }
336 : else
337 : {
338 61501 : lev_dur( lpc_wb, R, LPC_SHB_ORDER_WB, ervec );
339 :
340 : /* Expand bandwidth of the LP coeffs */
341 492008 : for ( i = 0; i <= LPC_SHB_ORDER_WB; i++ )
342 : {
343 430507 : lpc_wb[i] *= lpc_weights[i];
344 : }
345 :
346 : /* convert into lsps and calculate weights */
347 61501 : a2lsp( lsp_wb, lpc_wb, LPC_SHB_ORDER_WB );
348 61501 : lsp_weights( lsp_wb, weights_lsp, LPC_SHB_ORDER_WB );
349 :
350 : /* Quantization of LSFs */
351 61501 : i = closest_centroid( lsp_wb, weights_lsp, wb_bwe_lsfvq_cbook_8bit, 256, LPC_SHB_ORDER_WB );
352 61501 : push_indice( hBstr, IND_SHB_LSF, i, NUM_BITS_WB_LSF );
353 :
354 61501 : mvr2r( wb_bwe_lsfvq_cbook_8bit + i * LPC_SHB_ORDER_WB, lsp_wb, LPC_SHB_ORDER_WB );
355 :
356 61501 : lsp2a( lpc_wb, lsp_wb, LPC_SHB_ORDER_WB );
357 : }
358 :
359 75078 : uv_flag = 0;
360 75078 : if ( st->extl_brate == WB_TBE_1k05 && st->coder_type_raw == UNVOICED )
361 : {
362 5621 : uv_flag = 1;
363 : }
364 :
365 75078 : mvr2r( voice_factors, vf_modified, NB_SUBFR16k );
366 75078 : if ( st->coder_type == VOICED )
367 : {
368 45212 : for ( i = 1; i < NB_SUBFR; i++ )
369 : {
370 33909 : vf_modified[i] = 0.8f * voice_factors[i] + 0.2f * voice_factors[i - 1];
371 : }
372 :
373 11303 : if ( st->L_frame != L_FRAME )
374 : {
375 0 : vf_modified[4] = 0.8f * voice_factors[4] + 0.2f * voice_factors[3];
376 : }
377 : }
378 :
379 : /* From low band excitation, generate highband excitation */
380 75078 : mvr2r( hBWE_TD->state_syn_shbexc, shaped_wb_excitation, L_SHB_LAHEAD / 4 );
381 75078 : GenShapedWBExcitation( shaped_wb_excitation + L_SHB_LAHEAD / 4, lpc_wb, exc4kWhtnd, hBWE_TD->mem_csfilt, hBWE_TD->mem_genSHBexc_filt_down_shb, hBWE_TD->mem_genSHBexc_filt_down_wb2, hBWE_TD->mem_genSHBexc_filt_down_wb3, hBWE_TD->state_lpc_syn, st->coder_type, bwe_exc_extended, hBWE_TD->bwe_seed, vf_modified, uv_flag, st->igf );
382 :
383 75078 : prev_pow = sum2_f( shaped_wb_excitation, L_SHB_LAHEAD / 4 );
384 75078 : curr_pow = sum2_f( shaped_wb_excitation + L_SHB_LAHEAD / 4, L_SHB_LAHEAD / 4 );
385 :
386 75078 : if ( st->element_mode > EVS_MONO )
387 : {
388 : /* prevent too low values of energy */
389 71146 : prev_pow = max( 0.00001f, prev_pow );
390 71146 : curr_pow = max( 0.00001f, curr_pow );
391 : }
392 :
393 75078 : if ( voice_factors[0] > 0.75f )
394 : {
395 2957 : curr_pow = (float) ( curr_pow * 0.25 );
396 : }
397 :
398 75078 : if ( prev_pow == 0 )
399 : {
400 165 : scale = 0;
401 : }
402 : else
403 : {
404 74913 : scale = (float) sqrt( curr_pow / prev_pow );
405 : }
406 :
407 375390 : for ( i = 0; i < ( L_SHB_LAHEAD / 4 - 1 ); i++ )
408 : {
409 300312 : shaped_wb_excitation[i] *= scale;
410 : }
411 :
412 75078 : scale = (float) sqrt( scale );
413 :
414 75078 : shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] *= scale;
415 :
416 : /* Update WB excitation */
417 75078 : mvr2r( shaped_wb_excitation + L_FRAME16k / 4, hBWE_TD->state_syn_shbexc, L_SHB_LAHEAD / 4 );
418 :
419 : /* estimate the gain shape parameter */
420 75078 : EstimateSHBGainShape( SHB_OVERLAP_LEN / 2, hb_frame, shaped_wb_excitation, GainShape, subwin_wb );
421 :
422 : /* Gain frame adjustment factor */
423 75078 : temp_wb_fac = (float) log( ( GainShape[0] + 0.00001f ) / ( hBWE_TD->prev_wb_GainShape + 0.0001f ) );
424 75078 : feedback = temp_wb_fac * temp_wb_fac;
425 300312 : for ( i = 1; i < NUM_SHB_SUBFR / 4; i++ )
426 : {
427 225234 : temp_wb_fac = (float) log( ( GainShape[i] + 0.00001f ) / ( GainShape[i - 1] + 0.0001f ) );
428 225234 : feedback += ( temp_wb_fac * temp_wb_fac );
429 : }
430 75078 : feedback = 0.4f / ( 1 + 0.5f * feedback );
431 :
432 75078 : temp_wb_fac = hBWE_TD->prev_wb_GainShape;
433 375390 : for ( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
434 : {
435 300312 : GainShape[i] = ( 1 - feedback ) * GainShape[i] + feedback * temp_wb_fac;
436 300312 : temp_wb_fac = GainShape[i];
437 : }
438 75078 : hBWE_TD->prev_wb_GainShape = GainShape[NUM_SHB_SUBFR / 4 - 1];
439 :
440 : /* Compute the power of gains away from the peak gain prior to quantization */
441 75078 : if ( st->element_mode > EVS_MONO )
442 : {
443 71146 : p2m_in = pow_off_pk_corrected( GainShape, NUM_SHB_SUBFR / 4, 1 );
444 : }
445 : else
446 : {
447 3932 : p2m_in = pow_off_pk( GainShape, NUM_SHB_SUBFR / 4, 1 );
448 : }
449 :
450 75078 : if ( st->extl_brate == WB_TBE_0k35 )
451 : {
452 122193 : for ( i = 0; i < 8; i++ )
453 : {
454 108616 : GainShape[i] = RECIP_ROOT_EIGHT;
455 : }
456 : }
457 : else
458 : {
459 61501 : push_indice( hBstr, IND_UV_FLAG, uv_flag, 1 );
460 :
461 : /* Quantization of the subframe gain parameter */
462 61501 : QuantizeSHBsubgains( st, GainShape, st->extl );
463 : }
464 :
465 : /* Compute the power of gains away from the peak gain after quantization */
466 75078 : if ( st->element_mode > EVS_MONO )
467 : {
468 71146 : p2m_out = pow_off_pk_corrected( GainShape, NUM_SHB_SUBFR / 2, 2 );
469 : }
470 : else
471 : {
472 3932 : p2m_out = pow_off_pk( GainShape, NUM_SHB_SUBFR / 2, 2 );
473 : }
474 :
475 : /* Estimate the gain parameter */
476 75078 : EstimateSHBFrameGain( SHB_OVERLAP_LEN / 2, hb_frame, shaped_wb_excitation, GainShape, &GainFrame, window_wb, subwin_wb );
477 :
478 : /* If there's a big difference in the power of gains away from the peak gain */
479 : /* due to poor quantization then suppress energy of the high band. */
480 75078 : if ( p2m_out > 2.0f * p2m_in )
481 : {
482 1463 : float temp = 0;
483 1463 : if ( p2m_in >= 0 && p2m_out > 0 )
484 : {
485 1463 : temp = (float) sqrt( ( 2.0f * p2m_in ) / p2m_out );
486 : }
487 1463 : GainFrame *= temp;
488 : }
489 :
490 75078 : pitBufAvg = 0.0025f * sum_f( pitch_buf, NB_SUBFR );
491 75078 : voicingBufAvg = 0.333f * sum_f( st->voicing, 3 );
492 75078 : if ( voicingBufAvg <= 0.0f && ( pitBufAvg != 0 ) )
493 : {
494 0 : voicingBufAvg = pitBufAvg / 1.001f;
495 : }
496 75078 : else if ( voicingBufAvg <= 0.0f )
497 : {
498 0 : voicingBufAvg = 1.0f;
499 : }
500 :
501 75078 : GainFrame *= max( min( (float) ( pitBufAvg / voicingBufAvg ), 1.0f ), 0.7f );
502 :
503 75078 : if ( lsp_spacing < 0.01f )
504 : {
505 522 : GainFrame *= 0.65f;
506 : }
507 :
508 : /* Quantization of the frame gain parameter */
509 75078 : if ( st->igf && st->coder_type == VOICED )
510 : {
511 788 : GainFrame *= 0.5f;
512 : }
513 74290 : else if ( st->igf && ( 0.25f * sum_f( voice_factors, NB_SUBFR ) > 0.35f ) )
514 : {
515 30 : GainFrame *= 0.75f;
516 : }
517 :
518 75078 : if ( st->rf_mode )
519 : {
520 736 : QuantizeSHBframegain( st, &GainFrame, st->extl, st->extl_brate, &st->hRF->RF_bwe_gainFr_ind, 0 );
521 : }
522 : else
523 : {
524 74342 : QuantizeSHBframegain( st, &GainFrame, st->extl, st->extl_brate, NULL, 0 );
525 : }
526 :
527 : /* Adjust the subframe and frame gain of the synthesized SHB signal */
528 : /* Scale the shaped excitation*/
529 75078 : ScaleShapedSHB( SHB_OVERLAP_LEN / 2, shaped_wb_excitation, hBWE_TD->syn_overlap, GainShape, GainFrame, window_wb, subwin_wb );
530 :
531 75078 : return;
532 : }
533 :
534 :
535 : /*-------------------------------------------------------------------*
536 : * swb_tbe_enc()
537 : *
538 : * SWB TBE encoder, 6 - 14 kHz (or 7.5 - 15.5 kHz) band encoding module
539 : *-------------------------------------------------------------------*/
540 :
541 1063315 : void swb_tbe_enc(
542 : Encoder_State *st, /* i/o: encoder state structure */
543 : STEREO_ICBWE_ENC_HANDLE hStereoICBWE, /* i/o: IC-BWE state structure */
544 : const float *shb_speech, /* i : SHB target signal (6-14kHz) at 16kHz */
545 : const float *bwe_exc_extended, /* i : bandwidth extended exciatation */
546 : const float voice_factors[], /* i : voicing factors */
547 : float *White_exc16k, /* o : shaped white excitation for the FB TBE */
548 : const float pitch_buf[] /* i : pitch for each subframe */
549 : )
550 : {
551 : int16_t i, j, delay;
552 : TD_BWE_ENC_HANDLE hBWE_TD;
553 : BSTR_ENC_HANDLE hBstr;
554 : float shb_old_speech[L_LOOK_16k + L_SUBFR16k + L_FRAME16k];
555 : float shaped_shb_excitation[L_FRAME16k + L_SHB_LAHEAD];
556 : float GainFrame, GainShape[NUM_SHB_SUBFR];
557 : float lpc_shb[LPC_SHB_ORDER + 1];
558 : float lsf_q[LPC_SHB_ORDER];
559 : float weights_lsp[LPC_SHB_ORDER];
560 : float *shb_frame, *shb_new_speech;
561 : float lsf_shb_orig[LPC_SHB_ORDER];
562 : float lsp_shb_1[LPC_SHB_ORDER], lsp_shb_2[LPC_SHB_ORDER], lsp_temp[LPC_SHB_ORDER];
563 : float lpc_shb_sf[4 * ( LPC_SHB_ORDER + 1 )];
564 : const float *ptr_lsp_interp_coef;
565 : int16_t tmp;
566 : float shb_ener_sf;
567 : float lsf_shb[LPC_SHB_ORDER];
568 : float shb_res[L_FRAME16k];
569 : float shb_res_gshape[NB_SUBFR16k], normFac;
570 : int16_t vf_ind;
571 : float sd_uq_q, vf_modified[NB_SUBFR16k];
572 : float pitBufAvg, voicingBufAvg;
573 : float R[LPC_SHB_ORDER + 2], ervec[LPC_SHB_ORDER + 1];
574 : int16_t ana_align_delay;
575 : float prev_pow, curr_pow, scale;
576 : float p2m_in, p2m_out;
577 : int16_t frGainAttenuate, frGainSmoothEn;
578 : float MA_lsp_shb_spacing;
579 : float temp_swb_fac, feedback;
580 : float shaped_shb_excitationTemp[L_FRAME16k];
581 : float lsf_diff[LPC_SHB_ORDER], w[LPC_SHB_ORDER];
582 : float refl[M];
583 : float tilt_para;
584 : float formant_fac;
585 : float temp;
586 1063315 : int16_t stab_check = 1;
587 : int16_t MSFlag;
588 : float *nlExc16k, *mixExc16k;
589 : int16_t k;
590 : float pow0, fact_atten, alpha;
591 : float acorr_EnvSHBres[ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN], *p_acorr, acorr_v2a, shb_env_tilt, Env_error;
592 : float buf_EnvSHBres[2 * L_FRAME4k], *p_buf, EnvSHBres[L_FRAME16k], EnvSHBres_4k[L_FRAME4k], EnvSHBres_4k_norm[L_FRAME4k], env_mean_normf[L_FRAME4k];
593 : float GainShape_Interp[NUM_SHB_SUBGAINS], GainShape_tilt;
594 : float seg_mean[4], den_seg_mean[4], *p_env, step;
595 : int16_t seg_len, seg_len_2;
596 : float den_seg_len, den_seg_len_2;
597 : float sum_gain, normFact;
598 : float Env_error_part[NUM_SHB_SUBGAINS];
599 :
600 1063315 : Env_error = 0.0f;
601 1063315 : pitBufAvg = 0.0f;
602 1063315 : voicingBufAvg = 0.0f;
603 1063315 : set_zero( Env_error_part, NUM_SHB_SUBGAINS );
604 1063315 : shb_env_tilt = 0.0f;
605 :
606 1063315 : hBWE_TD = st->hBWE_TD;
607 1063315 : hBstr = st->hBstr;
608 :
609 1063315 : if ( st->element_mode >= IVAS_CPE_DFT && hStereoICBWE != NULL )
610 : {
611 269937 : nlExc16k = hStereoICBWE->nlExc16k;
612 269937 : mixExc16k = hStereoICBWE->mixExc16k;
613 269937 : MSFlag = hStereoICBWE->MSFlag;
614 : }
615 : else
616 : {
617 793378 : nlExc16k = NULL;
618 793378 : mixExc16k = NULL;
619 793378 : MSFlag = 0;
620 : }
621 :
622 : /* initializations */
623 1063315 : set_f( shaped_shb_excitationTemp, 0.0f, L_FRAME16k );
624 :
625 1063315 : if ( st->element_mode == IVAS_CPE_TD && ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 ) )
626 : {
627 17948 : ana_align_delay = -L_SHB_LAHEAD - ( NL_BUFF_OFFSET / 2 ) + 20; /* 20 corresponds to the 1.25 ms CLDFB delay */
628 : }
629 : else
630 : {
631 1045367 : ana_align_delay = -L_SHB_LAHEAD - ( NL_BUFF_OFFSET / 2 );
632 : }
633 :
634 : /* compensate for the delay in target generation and subframe LA */
635 1063315 : shb_frame = shb_old_speech + L_SUBFR16k + ana_align_delay;
636 :
637 1063315 : set_f( shb_old_speech, 0, L_LOOK_16k + L_SUBFR16k + L_FRAME16k );
638 :
639 : /* set up the speech buffers for TBE processing */
640 1063315 : delay = L_LOOK_16k + L_SUBFR16k;
641 1063315 : if ( st->element_mode == IVAS_CPE_TD )
642 : {
643 43489 : shb_new_speech = shb_old_speech + delay - L_MEM_RECALC_TBE_16K;
644 : }
645 : else
646 : {
647 1019826 : shb_new_speech = shb_old_speech + delay;
648 : }
649 :
650 1063315 : mvr2r( hBWE_TD->old_speech_shb, shb_old_speech, delay );
651 1063315 : mvr2r( shb_speech, shb_new_speech, L_FRAME16k );
652 1063315 : mvr2r( shb_old_speech + L_FRAME16k, hBWE_TD->old_speech_shb, delay );
653 :
654 1063315 : autocorr( shb_old_speech, R, LPC_SHB_ORDER + 1, NS2SA( INT_FS_16k, ACELP_LOOK_NS ) + L_SUBFR16k + L_FRAME16k, win_lpc_shb, 0, 1, 1 );
655 :
656 : /* Spectral smoothing of autocorrelation coefficients */
657 1063315 : if ( st->rf_mode || st->extl_brate == SWB_TBE_0k95 || st->extl_brate == SWB_TBE_1k10 )
658 : {
659 3336888 : for ( i = 0; i <= LPC_SHB_ORDER; i++ )
660 : {
661 3058814 : R[i] = R[i] * wac_swb[i];
662 : }
663 : }
664 :
665 : /* Set the autocorr[0] element to a non-negative value */
666 1063315 : R[0] = max( R[0], 1.0e-8f );
667 :
668 1063315 : lev_dur( lpc_shb, R, LPC_SHB_ORDER, ervec );
669 : {
670 : float enerG, lpc_shb1[M + 1];
671 :
672 : /* extend the lpc_shb to a 16th order gain calc */
673 1063315 : set_f( lpc_shb1, 0, M + 1 );
674 1063315 : mvr2r( lpc_shb, lpc_shb1, LPC_SHB_ORDER + 1 );
675 :
676 : /* estimate the LP gain */
677 1063315 : enerG = enr_1_Az( lpc_shb1, 2 * L_SUBFR );
678 :
679 : /* if the LP gain is greater than a threshold, avoid saturation.
680 : The function 'is_numeric_float' used to check for infinity enerG */
681 1063315 : if ( enerG > 32 || !( is_numeric_float( enerG ) ) )
682 : {
683 4613 : set_f( lpc_shb, 0, LPC_SHB_ORDER + 1 );
684 4613 : lev_dur( lpc_shb, R, 2, ervec );
685 : }
686 : }
687 :
688 : /* Expand bandwidth of the LP coeffs */
689 1063315 : if ( st->rf_mode || st->extl_brate == SWB_TBE_0k95 || st->extl_brate == SWB_TBE_1k10 )
690 : {
691 3336888 : for ( i = 0; i <= LPC_SHB_ORDER; i++ )
692 : {
693 3058814 : lpc_shb[i] *= lpc_weights[i];
694 : }
695 : }
696 :
697 : /* convert to LSFs */
698 1063315 : stab_check = a2lsp( lsf_shb, lpc_shb, LPC_SHB_ORDER );
699 :
700 1063315 : if ( ( st->last_extl != SWB_TBE && st->last_extl != FB_TBE ) || st->ini_frame == 0 )
701 : {
702 1152613 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
703 : {
704 1047830 : hBWE_TD->prev_lsp_shb[i] = i / 20.0f;
705 : }
706 : }
707 :
708 1063315 : if ( stab_check == 0 )
709 : {
710 92 : mvr2r( hBWE_TD->prev_lsp_shb, lsf_shb, LPC_SHB_ORDER );
711 : }
712 :
713 1063315 : mvr2r( lsf_shb, hBWE_TD->prev_lsp_shb, LPC_SHB_ORDER );
714 :
715 1063315 : mvr2r( lsf_shb, lsf_shb_orig, LPC_SHB_ORDER );
716 :
717 1063315 : gainFrSmooth_En( hBWE_TD, st->last_extl, shb_frame, lpc_shb, lsf_shb, &MA_lsp_shb_spacing, &frGainAttenuate, &frGainSmoothEn );
718 :
719 1063315 : if ( st->rf_mode || st->extl_brate == SWB_TBE_0k95 || st->extl_brate == SWB_TBE_1k10 )
720 : {
721 278074 : lsp_weights( lsf_shb, weights_lsp, LPC_SHB_ORDER );
722 :
723 : /* to compensate for the 1.1* weighting done inside the function lsp_weights */
724 278074 : weights_lsp[3] *= 0.909091f;
725 278074 : weights_lsp[4] *= 0.909091f;
726 :
727 : /* 8-bit VQ, 10 dimension */
728 278074 : i = closest_centroid( lsf_shb, weights_lsp, swb_tbe_lsfvq_cbook_8b, 256, LPC_SHB_ORDER );
729 278074 : mvr2r( swb_tbe_lsfvq_cbook_8b + i * LPC_SHB_ORDER, lsf_shb, LPC_SHB_ORDER );
730 :
731 278074 : set_s( hBWE_TD->lsf_idx, 0, NUM_Q_LSF );
732 :
733 278074 : if ( st->codec_mode == MODE1 )
734 : {
735 276743 : push_indice( hBstr, IND_SHB_LSF, i, 8 );
736 : }
737 : else
738 : {
739 1331 : hBWE_TD->lsf_idx[0] = i;
740 : }
741 :
742 278074 : mvr2r( swb_tbe_lsfvq_cbook_8b + i * LPC_SHB_ORDER, lsf_shb, LPC_SHB_ORDER );
743 : }
744 : else
745 : {
746 : /* Quantization of LSFs */
747 785241 : Quant_BWE_LSF( hBstr, st->hBWE_TD, st->codec_mode, lsf_shb, lsf_q, st->extl_brate );
748 :
749 785241 : mvr2r( lsf_q, lsf_shb, LPC_SHB_ORDER );
750 : }
751 :
752 1063315 : space_lsfs( lsf_shb, LPC_SHB_ORDER );
753 :
754 : /* voice factor adjustment and gainframe attenuation factor */
755 1063315 : sd_uq_q = 0;
756 11696465 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
757 : {
758 : /* Estimate the QD in lsps between UQ and Q*/
759 10633150 : sd_uq_q += ( lsf_shb[i] - lsf_shb_orig[i] ) * ( lsf_shb[i] - lsf_shb_orig[i] );
760 : }
761 :
762 1063315 : mvr2r( voice_factors, vf_modified, NB_SUBFR16k );
763 :
764 1063315 : if ( st->coder_type == VOICED || mean( voice_factors, 4 ) > 0.4f )
765 : {
766 1118124 : for ( i = 1; i < NB_SUBFR; i++ )
767 : {
768 838593 : vf_modified[i] = 0.8f * voice_factors[i] + 0.2f * voice_factors[i - 1];
769 : }
770 :
771 279531 : if ( st->L_frame != L_FRAME )
772 : {
773 152147 : vf_modified[4] = 0.8f * voice_factors[4] + 0.2f * voice_factors[3];
774 : }
775 : }
776 :
777 : /* SHB LSF from current frame; and convert to LSP for interpolation */
778 1063315 : lsf2lsp( lsf_shb, lsp_shb_2, LPC_SHB_ORDER, 1 );
779 :
780 1063315 : if ( st->last_extl == SWB_TBE || st->last_extl == FB_TBE )
781 : {
782 : /* SHB LSP values from prev. frame for interpolation */
783 989945 : mvr2r( hBWE_TD->swb_lsp_prev_interp, lsp_shb_1, LPC_SHB_ORDER );
784 : }
785 : else
786 : {
787 : /* Use current frame's LSPs; in effect no interpolation */
788 73370 : mvr2r( lsp_shb_2, lsp_shb_1, LPC_SHB_ORDER );
789 : }
790 :
791 1063315 : lsf_diff[0] = lsf_diff[LPC_SHB_ORDER - 1] = 0.5f;
792 9569835 : for ( i = 1; i < ( LPC_SHB_ORDER - 1 ); i++ )
793 : {
794 8506520 : lsf_diff[i] = lsf_shb[i] - lsf_shb[i - 1];
795 : }
796 :
797 1063315 : a2rc( hBWE_TD->cur_sub_Aq + 1, refl, M );
798 :
799 : /* LSP interpolation for 13.2 kbps and 16.4 kbps */
800 1063315 : tilt_para = 6.6956f * ( 1.0f + refl[0] ) * ( 1.0f + refl[0] ) - 3.8714f * ( 1.0f + refl[0] ) + 1.3041f;
801 1063315 : if ( st->last_extl != SWB_TBE )
802 : {
803 3573072 : for ( i = 1; i < LPC_SHB_ORDER - 1; i++ )
804 : {
805 3176064 : hBWE_TD->prev_lsf_diff[i - 1] = 0.5f * lsf_diff[i];
806 : }
807 : }
808 :
809 1063315 : if ( st->extl_brate <= FB_TBE_1k8 )
810 : {
811 847993 : if ( !( hBWE_TD->prev_tilt_para > 5.0f && ( st->coder_type == TRANSITION || tilt_para < 1.0f ) ) && !( ( hBWE_TD->prev_tilt_para < 3.0f && hBWE_TD->prev_coder_type >= VOICED ) && tilt_para > 5.0f ) )
812 : {
813 7388793 : for ( i = 1; i < ( LPC_SHB_ORDER - 1 ); i++ )
814 : {
815 6567816 : w[i] = ( lsf_diff[i] < hBWE_TD->prev_lsf_diff[i - 1] ) ? min( max( 0.8f * lsf_diff[i] / hBWE_TD->prev_lsf_diff[i - 1], 0.5f ), 1.0f ) : min( max( 0.8f * hBWE_TD->prev_lsf_diff[i - 1] / lsf_diff[i], 0.5f ), 1.0f );
816 : }
817 820977 : w[0] = w[1];
818 820977 : w[LPC_SHB_ORDER - 1] = w[LPC_SHB_ORDER - 2];
819 :
820 9030747 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
821 : {
822 8209770 : lsp_temp[i] = lsp_shb_1[i] * ( 1.0f - w[i] ) + lsp_shb_2[i] * w[i];
823 : }
824 : }
825 : else
826 : {
827 27016 : mvr2r( lsp_shb_2, lsp_temp, LPC_SHB_ORDER );
828 : }
829 : }
830 :
831 1063315 : mvr2r( lsf_diff + 1, hBWE_TD->prev_lsf_diff, LPC_SHB_ORDER - 2 );
832 1063315 : hBWE_TD->prev_tilt_para = tilt_para;
833 :
834 1063315 : if ( st->extl_brate >= SWB_TBE_2k8 )
835 : {
836 : /* SHB LSP interpolation */
837 215322 : ptr_lsp_interp_coef = interpol_frac_shb;
838 1076610 : for ( j = 0; j < 4; j++ )
839 : {
840 9474168 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
841 : {
842 8612880 : lsp_temp[i] = lsp_shb_1[i] * ( *ptr_lsp_interp_coef ) + lsp_shb_2[i] * ( *( ptr_lsp_interp_coef + 1 ) );
843 : }
844 861288 : ptr_lsp_interp_coef += 2;
845 :
846 861288 : tmp = j * ( LPC_SHB_ORDER + 1 );
847 : /* convert from lsp to lsf */
848 861288 : lsp2lsf( lsp_temp, lpc_shb_sf + tmp, LPC_SHB_ORDER, 1 );
849 : /* convert lsf to lpc for SHB synthesis */
850 861288 : lsp2a( lpc_shb_sf + tmp, lpc_shb_sf + tmp, LPC_SHB_ORDER );
851 861288 : lpc_shb_sf[j * ( LPC_SHB_ORDER + 1 )] = 1.0f;
852 : }
853 :
854 : /* -------- Calculate the SHB Energy -------- */
855 215322 : shb_ener_sf = 0.003125f * sum2_f( shb_frame + L_SHB_LAHEAD, L_FRAME16k );
856 215322 : Quant_shb_ener_sf( st, &shb_ener_sf );
857 :
858 : /* -------- calculate the residuals using the FOUR subframe LPCs -------- */
859 215322 : set_f( shb_res, 0, L_FRAME16k );
860 215322 : residu( lpc_shb_sf, LPC_SHB_ORDER, shb_frame + L_SHB_LAHEAD, shb_res, 80 );
861 215322 : residu( lpc_shb_sf + ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, shb_frame + L_SHB_LAHEAD + 80, shb_res + 80, 80 );
862 215322 : residu( lpc_shb_sf + 2 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, shb_frame + L_SHB_LAHEAD + 160, shb_res + 160, 80 );
863 215322 : residu( lpc_shb_sf + 3 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, shb_frame + L_SHB_LAHEAD + 240, shb_res + 240, 80 );
864 :
865 215322 : set_f( shb_res_gshape, 0, NB_SUBFR16k );
866 1291932 : for ( i = 0; i < NB_SUBFR16k; i++ )
867 : {
868 1076610 : shb_res_gshape[i] = sum2_f( shb_res + i * 64, 64 );
869 : }
870 :
871 215322 : maximum( shb_res_gshape, NB_SUBFR16k, &normFac );
872 215322 : normFac = (float) 1.0f / ( 0.0001f + normFac );
873 1291932 : for ( i = 0; i < NB_SUBFR16k; i++ )
874 : {
875 1076610 : shb_res_gshape[i] = (float) sqrt( shb_res_gshape[i] * normFac );
876 : }
877 :
878 215322 : Quant_shb_res_gshape( st, shb_res_gshape );
879 : }
880 847993 : else if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
881 : {
882 89740 : for ( j = 0; j < 4; j++ )
883 : {
884 71792 : tmp = j * ( LPC_SHB_ORDER + 1 );
885 :
886 : /* convert from lsp to lsf */
887 71792 : lsp2lsf( lsp_temp, lpc_shb_sf + tmp, LPC_SHB_ORDER, 1 );
888 :
889 : /* convert lsf to lpc for SHB synthesis */
890 71792 : lsp2a( lpc_shb_sf + tmp, lpc_shb_sf + tmp, LPC_SHB_ORDER );
891 71792 : lpc_shb_sf[j * ( LPC_SHB_ORDER + 1 )] = 1.0f;
892 : }
893 :
894 : /* -------- calculate the residuals using the FOUR subframe LPCs -------- */
895 17948 : set_f( shb_res, 0, L_FRAME16k );
896 17948 : residu( lpc_shb_sf, LPC_SHB_ORDER, shb_frame + L_SHB_LAHEAD, shb_res, 80 );
897 17948 : residu( lpc_shb_sf + ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, shb_frame + L_SHB_LAHEAD + 80, shb_res + 80, 80 );
898 17948 : residu( lpc_shb_sf + 2 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, shb_frame + L_SHB_LAHEAD + 160, shb_res + 160, 80 );
899 17948 : residu( lpc_shb_sf + 3 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, shb_frame + L_SHB_LAHEAD + 240, shb_res + 240, 80 );
900 : }
901 :
902 : /* Save the SWB LSP values from current frame for interpolation */
903 1063315 : mvr2r( lsp_shb_2, hBWE_TD->swb_lsp_prev_interp, LPC_SHB_ORDER );
904 :
905 : /* convert from lsp to lsf */
906 1063315 : lsp2lsf( lsp_temp, lpc_shb, LPC_SHB_ORDER, 1 );
907 :
908 : /* convert lsf to lpc for SHB synthesis */
909 1063315 : lsp2a( lpc_shb, lpc_shb, LPC_SHB_ORDER );
910 1063315 : lpc_shb[0] = 1.0f;
911 :
912 1063315 : mvr2r( hBWE_TD->state_syn_shbexc, shaped_shb_excitation, L_SHB_LAHEAD );
913 :
914 : /* Determine formant PF strength */
915 1063315 : formant_fac = swb_formant_fac( lpc_shb[1], &hBWE_TD->tilt_mem );
916 :
917 : /* calculate SHB auto-correlation function and convert to SHB voicing factor */
918 1063315 : acorr_v2a = 0.0f;
919 1063315 : if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
920 : {
921 : /* calculate the TD envelope of the SHB residual signal */
922 17948 : find_td_envelope( shb_res, L_FRAME16k, 20, hBWE_TD->mem_shb_res, EnvSHBres );
923 :
924 : /* downsample the TD envelope by 4 */
925 1453788 : for ( k = 0; k < L_FRAME4k; k++ )
926 : {
927 1435840 : EnvSHBres_4k[k] = EnvSHBres[4 * k];
928 : }
929 :
930 : /* calculate long-term mean envelope over four consecutive segments using linear interpolation */
931 17948 : seg_len = L_FRAME4k / 4;
932 17948 : den_seg_len = 1.0f / seg_len;
933 17948 : seg_len_2 = L_FRAME4k / 8;
934 17948 : den_seg_len_2 = 1.0f / ( seg_len_2 - 1 );
935 89740 : for ( k = 0; k < 4; k++ )
936 : {
937 71792 : seg_mean[k] = max( 1.0f, mean( &EnvSHBres_4k[k * seg_len], seg_len ) );
938 71792 : den_seg_mean[k] = 1.0f / seg_mean[k];
939 : }
940 :
941 : /* first half of the first segment */
942 17948 : if ( hBWE_TD->old_mean_EnvSHBres == 0 )
943 : {
944 693 : set_f( env_mean_normf, den_seg_mean[0], seg_len_2 );
945 : }
946 : else
947 : {
948 17255 : env_mean_normf[0] = hBWE_TD->old_mean_EnvSHBres;
949 17255 : step = ( den_seg_mean[0] - hBWE_TD->old_mean_EnvSHBres ) * den_seg_len_2;
950 172550 : for ( i = 1; i < seg_len_2; i++ )
951 : {
952 155295 : env_mean_normf[i] = env_mean_normf[i - 1] + step;
953 : }
954 : }
955 :
956 : /* segments 1-4 */
957 17948 : p_env = &env_mean_normf[seg_len_2];
958 71792 : for ( k = 1; k < 4; k++ )
959 : {
960 53844 : step = ( den_seg_mean[k] - den_seg_mean[k - 1] ) * den_seg_len;
961 1130724 : for ( i = 0; i < seg_len; i++ )
962 : {
963 1076880 : *p_env = *( p_env - 1 ) + step;
964 1076880 : p_env++;
965 : }
966 : }
967 :
968 : /* last half of the last segment */
969 17948 : set_f( p_env, den_seg_mean[3], seg_len_2 );
970 17948 : hBWE_TD->old_mean_EnvSHBres = den_seg_mean[3];
971 :
972 : /* normalize residual SHB envelope with its long-term mean envelope */
973 1453788 : for ( k = 0; k < L_FRAME4k; k++ )
974 : {
975 1435840 : EnvSHBres_4k_norm[k] = EnvSHBres_4k[k] * env_mean_normf[k];
976 : }
977 :
978 : /* calculate tilt of the long-term mean envelope */
979 17948 : lls_interp_n( seg_mean, 4, &shb_env_tilt, &temp, 0 );
980 :
981 : /* copy previous residual envelope to the buffer */
982 17948 : mvr2r( hBWE_TD->old_EnvSHBres, buf_EnvSHBres, L_FRAME4k );
983 :
984 : /* subtract mean value from the normalized SHB residual envelope */
985 17948 : p_buf = &buf_EnvSHBres[L_FRAME4k];
986 17948 : temp = mean( EnvSHBres_4k_norm, L_FRAME4k );
987 1453788 : for ( k = 0; k < L_FRAME4k; k++ )
988 : {
989 1435840 : *p_buf++ = EnvSHBres_4k_norm[k] - temp;
990 : }
991 :
992 : /* update memory */
993 17948 : mvr2r( &buf_EnvSHBres[L_FRAME4k], hBWE_TD->old_EnvSHBres, L_FRAME4k );
994 :
995 : /* calculate energy normalization factor for the auto-correlation function */
996 17948 : pow0 = sum2_f( &buf_EnvSHBres[L_FRAME4k], L_FRAME4k ) + 1.0f;
997 17948 : if ( hBWE_TD->prev_enr_EnvSHBres == 1.0f )
998 : {
999 1324 : scale = 1.0f / pow0;
1000 : }
1001 : else
1002 : {
1003 16624 : scale = 1.0f / max( pow0, hBWE_TD->prev_enr_EnvSHBres );
1004 : }
1005 17948 : hBWE_TD->prev_enr_EnvSHBres = pow0;
1006 :
1007 : /* calculate normalized auto-correlation function on the residual normalized SHB envelope */
1008 17948 : p_acorr = acorr_EnvSHBres;
1009 17948 : p_buf = &buf_EnvSHBres[L_FRAME4k - ENVSHBRES_ACORR_MIN];
1010 735868 : for ( k = 0; k < ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN; k++ )
1011 : {
1012 717920 : *p_acorr++ = scale * dotp( &buf_EnvSHBres[L_FRAME4k], p_buf--, L_FRAME4k );
1013 : }
1014 :
1015 : /* calculate variance of the SHB autocorrelation function */
1016 17948 : acorr_v2a = sum2_f( acorr_EnvSHBres, ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN ) / ( ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN );
1017 :
1018 : /* multiply with the maximum of the SHB autocorrelation function */
1019 17948 : maximum( acorr_EnvSHBres, ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN, &temp );
1020 17948 : acorr_v2a *= temp;
1021 :
1022 17948 : alpha = 25.0f;
1023 17948 : acorr_v2a = 2.0f / ( 1.0f + (float) expf( -alpha * acorr_v2a ) ) - 1.0f;
1024 :
1025 : /* limit in the range 0.0 - 1.0 */
1026 17948 : acorr_v2a = min( 1.0f, max( 0.0f, acorr_v2a ) );
1027 :
1028 17948 : hBWE_TD->prev_shb_env_tilt = shb_env_tilt;
1029 : }
1030 :
1031 1063315 : vf_ind = 20;
1032 1063315 : GenShapedSHBExcitation( shaped_shb_excitation + L_SHB_LAHEAD, lpc_shb, White_exc16k, hBWE_TD->mem_csfilt, hBWE_TD->mem_genSHBexc_filt_down_shb, hBWE_TD->state_lpc_syn, st->coder_type, bwe_exc_extended, hBWE_TD->bwe_seed, vf_modified, st->extl, &( hBWE_TD->tbe_demph ), &( hBWE_TD->tbe_premph ), lpc_shb_sf, &shb_ener_sf, shb_res_gshape, shb_res, &vf_ind, formant_fac, hBWE_TD->fb_state_lpc_syn, &( hBWE_TD->fb_tbe_demph ), st->total_brate, 0, st->element_mode, st->flag_ACELP16k, nlExc16k, mixExc16k, st->extl_brate, MSFlag,
1033 : EnvSHBres_4k_norm, &( hBWE_TD->prev_pow_exc16kWhtnd ), &( hBWE_TD->prev_mix_factor ), &Env_error, Env_error_part );
1034 :
1035 : /* fill-in missing SHB excitation */
1036 1063315 : if ( ( st->element_mode == IVAS_CPE_TD || st->element_mode == IVAS_CPE_DFT ) && st->last_core_brate <= SID_2k40 )
1037 : {
1038 881 : mvr2r( shaped_shb_excitation + L_SHB_LAHEAD, shaped_shb_excitation, L_SHB_LAHEAD );
1039 : }
1040 :
1041 1063315 : if ( st->element_mode >= IVAS_CPE_DFT && hStereoICBWE != NULL )
1042 : {
1043 269937 : mvr2r( shaped_shb_excitation + L_SHB_LAHEAD, hStereoICBWE->shbSynthRef, L_FRAME16k );
1044 : }
1045 :
1046 1063315 : if ( st->extl_brate >= SWB_TBE_2k8 || st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
1047 : {
1048 233270 : if ( st->codec_mode == MODE2 )
1049 : {
1050 6300 : hBWE_TD->idx_mixFac = vf_ind;
1051 : }
1052 : else
1053 : {
1054 226970 : push_indice( hBstr, IND_SHB_VF, vf_ind, NUM_BITS_SHB_VF );
1055 : }
1056 : }
1057 :
1058 1063315 : if ( st->extl_brate != SWB_TBE_1k10 && st->extl_brate != SWB_TBE_1k75 )
1059 : {
1060 5226835 : for ( i = 0; i < L_FRAME16k; i += L_SUBFR16k )
1061 : {
1062 : /* TD BWE post-processing */
1063 4181468 : PostShortTerm( &shaped_shb_excitation[L_SHB_LAHEAD + i], lpc_shb, &shaped_shb_excitationTemp[i], hBWE_TD->mem_stp_swb, hBWE_TD->ptr_mem_stp_swb, &( hBWE_TD->gain_prec_swb ), hBWE_TD->mem_zero_swb, formant_fac );
1064 : }
1065 :
1066 1045367 : mvr2r( shaped_shb_excitationTemp, &shaped_shb_excitation[L_SHB_LAHEAD], L_FRAME16k );
1067 :
1068 1045367 : prev_pow = sum2_f( shaped_shb_excitation, L_SHB_LAHEAD + 10 );
1069 1045367 : curr_pow = sum2_f( shaped_shb_excitation + L_SHB_LAHEAD + 10, L_SHB_LAHEAD + 10 );
1070 :
1071 1045367 : if ( st->element_mode > EVS_MONO )
1072 : {
1073 : /* prevent too low values of energy */
1074 1022224 : prev_pow = max( 0.00001f, prev_pow );
1075 1022224 : curr_pow = max( 0.00001f, curr_pow );
1076 : }
1077 :
1078 1045367 : if ( voice_factors[0] > 0.75f )
1079 : {
1080 59692 : curr_pow = (float) ( curr_pow * 0.25 );
1081 : }
1082 :
1083 1045367 : if ( prev_pow == 0 )
1084 : {
1085 0 : scale = 0;
1086 : }
1087 : else
1088 : {
1089 1045367 : scale = (float) sqrt( curr_pow / prev_pow );
1090 : }
1091 :
1092 21952707 : for ( i = 0; i < L_SHB_LAHEAD; i++ )
1093 : {
1094 20907340 : shaped_shb_excitation[i] *= scale;
1095 : }
1096 :
1097 11499037 : for ( ; i < L_SHB_LAHEAD + 10; i++ )
1098 : {
1099 10453670 : temp = ( i - 19 ) / 10.0f;
1100 10453670 : shaped_shb_excitation[i] *= ( temp * 1.0f + ( 1.0f - temp ) * scale );
1101 : }
1102 : }
1103 : else
1104 : {
1105 : /* reset the PF memories if the PF is not running */
1106 17948 : set_f( hBWE_TD->mem_stp_swb, 0, LPC_SHB_ORDER );
1107 17948 : hBWE_TD->gain_prec_swb = 1.0f;
1108 17948 : set_f( hBWE_TD->mem_zero_swb, 0, LPC_SHB_ORDER );
1109 : }
1110 :
1111 : /* Update SHB excitation */
1112 1063315 : mvr2r( shaped_shb_excitation + L_FRAME16k, hBWE_TD->state_syn_shbexc, L_SHB_LAHEAD );
1113 :
1114 : /* Estimate the gain-shape parameter */
1115 1063315 : EstimateSHBGainShape( SHB_OVERLAP_LEN, shb_frame, shaped_shb_excitation, GainShape, subwin_shb );
1116 :
1117 1063315 : if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
1118 : {
1119 : /* Gain shape attenuation in case of big error in envelope modelling */
1120 89740 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
1121 : {
1122 71792 : if ( Env_error_part[i] > 100.0f )
1123 : {
1124 : /* set gain shape to the average of the remaining gains */
1125 89 : GainShape[i] = ( sum_f( GainShape, NUM_SHB_SUBGAINS ) - GainShape[i] ) / ( NUM_SHB_SUBGAINS - 1 );
1126 : }
1127 : }
1128 : }
1129 :
1130 1063315 : if ( st->extl_brate != SWB_TBE_1k10 && st->extl_brate != SWB_TBE_1k75 )
1131 : {
1132 : /* Gain shape BWS/high band low energy fix */
1133 1045367 : if ( hBWE_TD->cldfbHBLT < 1.0f )
1134 : {
1135 : /* There is not much HB past 10kHz; the temporal resolution is quite coarse, so reduce the dynamic range */
1136 414500 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
1137 : {
1138 : /* 0.35f +/- delta variation; delta = 0.1*(GS-0.35)*/
1139 331600 : GainShape[i] = 0.315f + 0.1f * GainShape[i];
1140 : }
1141 : }
1142 : }
1143 :
1144 : /* high-band gain control in case of BWS */
1145 1063315 : if ( st->extl_brate != SWB_TBE_1k10 && st->extl_brate != SWB_TBE_1k75 )
1146 : {
1147 1045367 : if ( st->bwidth_sw_cnt > 0 )
1148 : {
1149 46040 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
1150 : {
1151 36832 : GainShape[i] *= ( (float) ( st->bwidth_sw_cnt ) / (float) BWS_TRAN_PERIOD );
1152 : }
1153 : }
1154 : }
1155 :
1156 : /* Gain shape adjustment factor */
1157 1063315 : temp_swb_fac = (float) log( ( GainShape[0] + 0.00001f ) / ( hBWE_TD->prev_swb_GainShape + 0.0001f ) );
1158 1063315 : feedback = temp_swb_fac * temp_swb_fac;
1159 4253260 : for ( i = 1; i < NUM_SHB_SUBGAINS; i++ )
1160 : {
1161 3189945 : temp_swb_fac = (float) log( ( GainShape[i] + 0.00001f ) / ( GainShape[i - 1] + 0.0001f ) );
1162 3189945 : feedback += ( temp_swb_fac * temp_swb_fac );
1163 : }
1164 :
1165 1063315 : feedback = 0.4f / ( 1 + 0.5f * feedback );
1166 :
1167 : /* calculate tilt in all sub-frame gains */
1168 1063315 : mvr2r( GainShape, GainShape_Interp, NUM_SHB_SUBGAINS );
1169 1063315 : lls_interp_n( GainShape_Interp, NUM_SHB_SUBGAINS, &GainShape_tilt, &temp, 1 );
1170 :
1171 1063315 : if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
1172 : {
1173 17948 : if ( acorr_v2a < 0.4f && vf_ind >= 5 && fabs( GainShape_tilt ) < 0.2f && shb_env_tilt < 500.0f && hBWE_TD->prev_shb_env_tilt < 500.0f )
1174 : {
1175 : /* stronger smoothing in case of unvoiced SHB residual signal with gaussian excitation */
1176 13520 : feedback = lin_interp( acorr_v2a, 0.4f, 0.0f, 0.0f, 0.95f, 0 );
1177 13520 : feedback = min( max( feedback, 0.0f ), 1.0f );
1178 :
1179 67600 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
1180 : {
1181 54080 : GainShape[i] = ( 1 - feedback ) * GainShape[i] + feedback * GainShape_Interp[i];
1182 : }
1183 : }
1184 : }
1185 : else
1186 : {
1187 1045367 : if ( frGainAttenuate == 1 || ( sum_f( st->voicing, 3 ) > 2.4f && sum_f( voice_factors, 4 ) > 0.8f ) )
1188 : {
1189 347912 : temp_swb_fac = hBWE_TD->prev_swb_GainShape;
1190 1739560 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
1191 : {
1192 1391648 : GainShape[i] = ( 1 - feedback ) * GainShape[i] + feedback * temp_swb_fac;
1193 1391648 : temp_swb_fac = GainShape[i];
1194 : }
1195 : }
1196 : }
1197 :
1198 1063315 : if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
1199 : {
1200 : /* Re-normalize gain shape before quantization */
1201 17948 : sum_gain = sum2_f( GainShape, NUM_SHB_SUBGAINS );
1202 17948 : if ( sum_gain == 0 )
1203 : {
1204 0 : normFact = 0;
1205 : }
1206 : else
1207 : {
1208 17948 : normFact = (float) sqrt( 1.0f / sum_gain );
1209 : }
1210 :
1211 17948 : v_multc( GainShape, normFact, GainShape, NUM_SHB_SUBGAINS );
1212 : }
1213 :
1214 1063315 : hBWE_TD->prev_swb_GainShape = GainShape[3];
1215 :
1216 : /* Compute the power of gains away from the peak gain prior to quantization */
1217 1063315 : if ( st->element_mode > EVS_MONO )
1218 : {
1219 1040172 : p2m_in = pow_off_pk_corrected( GainShape, NUM_SHB_SUBGAINS, 1 );
1220 : }
1221 : else
1222 : {
1223 23143 : p2m_in = pow_off_pk( GainShape, NUM_SHB_SUBGAINS, 1 );
1224 : }
1225 :
1226 : /* Quantization of the gain shape parameter */
1227 1063315 : QuantizeSHBsubgains( st, GainShape, st->extl );
1228 :
1229 : /* Compute the power of gains away from the peak gain after quantization */
1230 1063315 : if ( st->element_mode > EVS_MONO )
1231 : {
1232 1040172 : p2m_out = pow_off_pk_corrected( GainShape, NUM_SHB_SUBFR, 4 );
1233 : }
1234 : else
1235 : {
1236 23143 : p2m_out = pow_off_pk( GainShape, NUM_SHB_SUBFR, 4 );
1237 : }
1238 :
1239 : /* Gain shape smoothing after quantization */
1240 1063315 : if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
1241 : {
1242 89740 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
1243 : {
1244 71792 : GainShape_Interp[i] = GainShape[i * NUM_SHB_SUBGAINS];
1245 : }
1246 :
1247 17948 : lls_interp_n( GainShape_Interp, NUM_SHB_SUBGAINS, &GainShape_tilt, &temp, 1 );
1248 :
1249 17948 : if ( vf_ind >= 6 && fabs( GainShape_tilt ) < 0.12f )
1250 : {
1251 10252 : feedback = 0.3f;
1252 51260 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
1253 : {
1254 41008 : GainShape[i] = ( 1 - feedback ) * GainShape[i * NUM_SHB_SUBGAINS] + feedback * GainShape_Interp[i];
1255 : }
1256 :
1257 164032 : for ( i = NUM_SHB_SUBFR - 1; i > 0; i-- )
1258 : {
1259 153780 : GainShape[i] = GainShape[i * NUM_SHB_SUBGAINS / NUM_SHB_SUBFR];
1260 : }
1261 : }
1262 : }
1263 :
1264 : /* Estimate the gain parameter */
1265 1063315 : EstimateSHBFrameGain( SHB_OVERLAP_LEN, shb_frame, shaped_shb_excitation, GainShape, &GainFrame, window_shb, subwin_shb );
1266 :
1267 1063315 : if ( st->tec_tfa == 1 )
1268 : {
1269 8575 : tfaCalcEnv( shb_frame, st->tfa_enr );
1270 : }
1271 :
1272 : /* If there's a big difference in the power of gains away from the peak gain */
1273 : /* due to poor quantization then suppress energy of the high band. */
1274 1063315 : if ( p2m_out > 2.0f * p2m_in )
1275 : {
1276 33936 : temp = 0.f;
1277 33936 : if ( p2m_in >= 0 && p2m_out > 0 )
1278 : {
1279 33936 : temp = (float) sqrt( ( 2.0f * p2m_in ) / p2m_out );
1280 : }
1281 33936 : GainFrame *= temp;
1282 : }
1283 :
1284 1063315 : if ( st->element_mode > EVS_MONO && st->L_frame != st->last_L_frame && hBWE_TD->prev_gainFr_SHB != 0 && ( st->last_extl == SWB_TBE || st->last_extl == FB_TBE ) && st->coder_type == TRANSITION && st->coder_type_raw != VOICED && st->clas == VOICED_CLAS && st->last_clas == VOICED_CLAS && ( 3.0f * voice_factors[0] < voice_factors[( st->L_frame >> 6 ) - 1] ) )
1285 : {
1286 398 : float fac = GainFrame / hBWE_TD->prev_gainFr_SHB;
1287 :
1288 398 : if ( fac > 4.0f )
1289 : {
1290 29 : GainFrame = 4.0f * GainFrame / fac;
1291 : }
1292 : }
1293 :
1294 1063315 : if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
1295 : {
1296 17948 : if ( acorr_v2a > 0.1f && Env_error > 5.0f )
1297 : {
1298 : /* attenuate frame gain in case of voiced SHB residual signal with gaussian excitation */
1299 2544 : fact_atten = lin_interp( Env_error, 5.0f, 1.0f, 25.0f, 0.2f, 1 );
1300 2544 : GainFrame *= fact_atten;
1301 : }
1302 : /* Frame gain attenuation in case of big error in envelope modelling */
1303 15404 : else if ( Env_error > 100.0f || hBWE_TD->prev_Env_error > 100.0f )
1304 : {
1305 153 : if ( Env_error > 100.0f )
1306 : {
1307 77 : fact_atten = lin_interp( Env_error, 100.0f, 1.0f, 300.0f, 0.1f, 1 );
1308 : }
1309 : else
1310 : {
1311 76 : fact_atten = lin_interp( hBWE_TD->prev_Env_error, 100.0f, 1.0f, 300.0f, 0.5f, 1 );
1312 : }
1313 153 : GainFrame *= fact_atten;
1314 : }
1315 : }
1316 : else
1317 : {
1318 1045367 : if ( frGainSmoothEn == 1 && hBWE_TD->prev_gainFr_SHB < GainFrame )
1319 : {
1320 292 : GainFrame = 0.5f * ( hBWE_TD->prev_gainFr_SHB + GainFrame );
1321 : }
1322 : }
1323 :
1324 1063315 : if ( frGainAttenuate == 1 && MA_lsp_shb_spacing <= 0.0024f )
1325 : {
1326 442 : GainFrame = (float) pow( GainFrame, 0.8f );
1327 : }
1328 1062873 : else if ( hBWE_TD->prev_frGainAtten == 1 && GainFrame > 3.0f * hBWE_TD->prev_gainFr_SHB )
1329 : {
1330 633 : GainFrame *= ( 0.8f + 0.5f * feedback );
1331 : }
1332 1063315 : hBWE_TD->prev_frGainAtten = frGainAttenuate;
1333 :
1334 1063315 : hBWE_TD->prev_gainFr_SHB = GainFrame;
1335 :
1336 1063315 : if ( GainFrame > 153.0f && st->element_mode >= IVAS_CPE_DFT && hStereoICBWE != NULL )
1337 : {
1338 606 : hStereoICBWE->MSFlag = 1;
1339 : }
1340 :
1341 : /* Gain attenuation when the SWB LSF quantization error is larger than a threshold */
1342 1063315 : sd_uq_q = ( sd_uq_q / 0.0025f );
1343 1063315 : if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
1344 : {
1345 17948 : sd_uq_q = 1 - 0.7f * ( sd_uq_q * sd_uq_q );
1346 : }
1347 : else
1348 : {
1349 1045367 : if ( st->L_frame == L_FRAME )
1350 : {
1351 493312 : sd_uq_q = 1 - 0.2f * ( sd_uq_q * sd_uq_q );
1352 : }
1353 : else
1354 : {
1355 552055 : sd_uq_q = 1 - 0.1f * ( sd_uq_q * sd_uq_q );
1356 : }
1357 : }
1358 :
1359 1063315 : sd_uq_q = max( min( sd_uq_q, 1.0f ), 0.5f );
1360 :
1361 1063315 : if ( st->extl_brate != SWB_TBE_1k10 && st->extl_brate != SWB_TBE_1k75 )
1362 : {
1363 1045367 : pitBufAvg = 0.0025f * sum_f( pitch_buf, 4 );
1364 1045367 : voicingBufAvg = ( sum_f( voice_factors, 4 ) > 0.6f ) ? 0.333f : 0.1667f;
1365 1045367 : voicingBufAvg = voicingBufAvg * sum_f( st->voicing, 3 );
1366 :
1367 1045367 : if ( voicingBufAvg <= 0.0f && sd_uq_q * pitBufAvg != 0 )
1368 : {
1369 3668 : voicingBufAvg = sd_uq_q * pitBufAvg / 1.001f;
1370 : }
1371 1041699 : else if ( voicingBufAvg <= 0.0f )
1372 : {
1373 0 : voicingBufAvg = 1.0f;
1374 : }
1375 : }
1376 :
1377 : /* high-band gain control in case of BWS */
1378 1063315 : if ( st->extl_brate != SWB_TBE_1k10 && st->extl_brate != SWB_TBE_1k75 )
1379 : {
1380 1045367 : if ( st->bwidth_sw_cnt > 0 )
1381 : {
1382 9208 : GainFrame *= ( (float) ( st->bwidth_sw_cnt ) / (float) BWS_TRAN_PERIOD );
1383 : }
1384 : }
1385 :
1386 : /* Controlled gain evolution in SWB for stronger voiced segments */
1387 1063315 : if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
1388 : {
1389 17948 : GainFrame *= sd_uq_q;
1390 : }
1391 : else
1392 : {
1393 1045367 : GainFrame *= max( min( (float) ( sd_uq_q * pitBufAvg / voicingBufAvg ), 1.0f ), 0.6f );
1394 : }
1395 :
1396 1063315 : if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
1397 : {
1398 17948 : if ( st->rf_mode == 1 )
1399 : {
1400 : /* Compensate for energy increase mismatch due to memory-less synthesis */
1401 0 : GainFrame *= 0.85f;
1402 : }
1403 : }
1404 : else
1405 : {
1406 1045367 : if ( st->L_frame == L_FRAME16k || st->rf_mode == 1 )
1407 : {
1408 : /* Compensate for energy increase mismatch due to memory-less synthesis*/
1409 552727 : GainFrame *= 0.85f;
1410 : }
1411 : }
1412 :
1413 : /* Quantization of the frame gain parameter */
1414 1063315 : if ( st->rf_mode )
1415 : {
1416 672 : QuantizeSHBframegain( st, &GainFrame, st->extl, 0, &st->hRF->RF_bwe_gainFr_ind, 0 );
1417 : }
1418 : else
1419 : {
1420 1062643 : QuantizeSHBframegain( st, &GainFrame, st->extl, 0, NULL, ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 ) ? 1 : 0 );
1421 : }
1422 :
1423 1063315 : if ( hStereoICBWE != NULL )
1424 : {
1425 269937 : push_indice( hBstr, IND_STEREO_ICBWE_MSFLAG, hStereoICBWE->MSFlag, STEREO_ICBWE_MSFLAG_BITS );
1426 : }
1427 :
1428 : /* Backup for the IC BWE parameters */
1429 1063315 : if ( st->element_mode >= IVAS_CPE_DFT && hStereoICBWE != NULL )
1430 : {
1431 269937 : mvr2r( lpc_shb, hStereoICBWE->lpSHBRef, LPC_SHB_ORDER + 1 );
1432 : }
1433 :
1434 1063315 : if ( st->extl == FB_TBE )
1435 : {
1436 111932700 : for ( i = 0; i < L_FRAME16k; i++ )
1437 : {
1438 111584000 : White_exc16k[i] *= GainFrame * GainShape[NUM_SHB_SUBFR * i / L_FRAME16k];
1439 : }
1440 : }
1441 :
1442 1063315 : hBWE_TD->prev_Env_error = Env_error;
1443 :
1444 1063315 : return;
1445 : }
1446 :
1447 :
1448 : /*-------------------------------------------------------------------*
1449 : * EstimateSHBFrameGain()
1450 : *
1451 : * Estimate the overall gain factor needed to scale synthesized highband
1452 : * to original highband signal level.
1453 : *-------------------------------------------------------------------*/
1454 :
1455 1138393 : static void EstimateSHBFrameGain(
1456 : const int16_t length, /* i : SHB overlap length */
1457 : const float *oriSHB, /* i : target original SHB frame */
1458 : const float *synSHB, /* i : shaped SHB excitation */
1459 : float *subgain, /* o : estimate of gain shape */
1460 : float *GainFrame, /* o : estimat of gain frame */
1461 : const float *win_shb, /* i : SHB window */
1462 : const float *subwin /* i : subframe window */
1463 : )
1464 : {
1465 : const int16_t *skip;
1466 : int16_t i, j, k, l_shb_lahead, l_frame;
1467 : int16_t join_length, num_join;
1468 : float sig, mod_syn[L_FRAME16k + L_SHB_LAHEAD];
1469 1138393 : float oriNrg = 0.0f, synNrg = 0.0f, sum_gain;
1470 : float frame_gain;
1471 :
1472 : /* initilaization */
1473 1138393 : l_frame = L_FRAME16k;
1474 1138393 : l_shb_lahead = L_SHB_LAHEAD;
1475 1138393 : skip = skip_bands_SWB_TBE;
1476 :
1477 1138393 : if ( length == SHB_OVERLAP_LEN / 2 )
1478 : {
1479 75078 : skip = skip_bands_WB_TBE;
1480 75078 : l_frame = L_FRAME16k / 4;
1481 75078 : l_shb_lahead = L_SHB_LAHEAD / 4;
1482 : }
1483 :
1484 : /* apply gain for each subframe, and store noise output signal using overlap-add*/
1485 1138393 : set_f( mod_syn, 0, l_frame + l_shb_lahead );
1486 :
1487 1138393 : if ( length == SHB_OVERLAP_LEN / 2 )
1488 : {
1489 75078 : sum_gain = 0;
1490 450468 : for ( k = 0; k < length / 2; k++ )
1491 : {
1492 375390 : sum_gain = subwin[2 * k + 2] * subgain[0];
1493 375390 : mod_syn[skip[0] + k] = synSHB[skip[0] + k] * sum_gain;
1494 375390 : mod_syn[skip[0] + k + length / 2] = synSHB[skip[0] + k + length / 2] * subgain[0];
1495 : }
1496 :
1497 600624 : for ( i = 1; i < NUM_SHB_SUBFR / 2; i++ )
1498 : {
1499 5781006 : for ( k = 0; k < length; k++ )
1500 : {
1501 5255460 : sum_gain = subwin[k + 1] * subgain[i] + subwin[length - k - 1] * subgain[i - 1];
1502 5255460 : mod_syn[skip[i] + k] = synSHB[skip[i] + k] * sum_gain;
1503 : }
1504 : }
1505 :
1506 450468 : for ( k = 0; k < length / 2; k++ )
1507 : {
1508 375390 : sum_gain = subwin[length - 2 * k - 2] * subgain[i - 1];
1509 375390 : mod_syn[skip[i] + k] = synSHB[skip[i] + k] * sum_gain;
1510 : }
1511 : }
1512 : else
1513 : {
1514 1063315 : num_join = NUM_SHB_SUBFR / NUM_SHB_SUBGAINS;
1515 1063315 : join_length = num_join * length;
1516 22329615 : for ( k = 0, j = 0; k < length; k++ )
1517 : {
1518 21266300 : mod_syn[j] = synSHB[j] * subwin[k + 1] * subgain[0];
1519 21266300 : j++;
1520 : }
1521 4253260 : for ( i = 0; i < NUM_SHB_SUBGAINS - 1; i++ )
1522 : {
1523 194586645 : for ( k = 0; k < join_length - length; k++ )
1524 : {
1525 191396700 : mod_syn[j] = synSHB[j] * subgain[i * num_join];
1526 191396700 : j++;
1527 : }
1528 :
1529 66988845 : for ( k = 0; k < length; k++ )
1530 : {
1531 63798900 : mod_syn[j] = synSHB[j] * ( subwin[length - k - 1] * subgain[i * num_join] + subwin[k + 1] * subgain[( i + 1 ) * num_join] );
1532 63798900 : j++;
1533 : }
1534 : }
1535 64862215 : for ( k = 0; k < join_length - length; k++ )
1536 : {
1537 63798900 : mod_syn[j] = synSHB[j] * subgain[( NUM_SHB_SUBGAINS - 1 ) * num_join];
1538 63798900 : j++;
1539 : }
1540 22329615 : for ( k = 0; k < length; k++ )
1541 : {
1542 21266300 : mod_syn[j] = synSHB[j] * subwin[length - k - 1] * subgain[( NUM_SHB_SUBGAINS - 1 ) * num_join];
1543 21266300 : j++;
1544 : }
1545 : }
1546 :
1547 : /* adjust frame energy */
1548 1138393 : oriNrg = (float) 1e-10;
1549 1138393 : synNrg = (float) 1e-10;
1550 :
1551 22780083 : for ( i = 0; i < l_shb_lahead; i++ )
1552 : {
1553 21641690 : sig = oriSHB[i] * win_shb[i];
1554 21641690 : oriNrg += sig * sig;
1555 :
1556 21641690 : sig = mod_syn[i] * win_shb[i];
1557 21641690 : synNrg += sig * sig;
1558 : }
1559 :
1560 325763743 : for ( ; i < l_frame; i++ )
1561 : {
1562 324625350 : oriNrg += oriSHB[i] * oriSHB[i];
1563 324625350 : synNrg += mod_syn[i] * mod_syn[i];
1564 : }
1565 :
1566 22780083 : for ( ; i < l_frame + l_shb_lahead; i++ )
1567 : {
1568 21641690 : sig = oriSHB[i] * win_shb[l_frame + l_shb_lahead - 1 - i];
1569 21641690 : oriNrg += sig * sig;
1570 :
1571 21641690 : sig = mod_syn[i] * win_shb[l_frame + l_shb_lahead - 1 - i];
1572 21641690 : synNrg += sig * sig;
1573 : }
1574 :
1575 1138393 : frame_gain = (float) sqrt( oriNrg / synNrg );
1576 1138393 : if ( ( synNrg ) == 0 )
1577 : {
1578 0 : frame_gain = 0;
1579 : }
1580 :
1581 1138393 : *GainFrame = (float) frame_gain;
1582 :
1583 1138393 : return;
1584 : }
1585 :
1586 :
1587 : /*-------------------------------------------------------------------*
1588 : * pow_off_pk()
1589 : *
1590 : * Sums squares of SWB shape gain parameters away from peak values
1591 : *-------------------------------------------------------------------*/
1592 :
1593 54150 : static float pow_off_pk(
1594 : float a[],
1595 : const int16_t len,
1596 : const int16_t step )
1597 : {
1598 54150 : int16_t i, j = 0;
1599 : float sum;
1600 :
1601 54150 : sum = a[0] * a[0];
1602 243675 : for ( j = 0, i = 1; i < len; i += step )
1603 : {
1604 189525 : sum += a[i] * a[i];
1605 189525 : if ( a[i] > a[j] )
1606 : {
1607 61653 : j = i;
1608 : }
1609 : }
1610 :
1611 54150 : sum -= a[j] * a[j];
1612 :
1613 54150 : return ( sum );
1614 : }
1615 :
1616 : /*-------------------------------------------------------------------*
1617 : * pow_off_pk_corrected()
1618 : *
1619 : * Sums squares of SWB shape gain parameters away from peak values
1620 : *-------------------------------------------------------------------*/
1621 :
1622 2222636 : static float pow_off_pk_corrected(
1623 : float a[],
1624 : const int16_t len,
1625 : const int16_t step )
1626 : {
1627 2222636 : int16_t i, j = 0;
1628 : float sum;
1629 :
1630 2222636 : sum = a[0] * a[0];
1631 8890544 : for ( j = 0, i = step; i < len; i += step )
1632 : {
1633 6667908 : sum += a[i] * a[i];
1634 6667908 : if ( a[i] > a[j] )
1635 : {
1636 2765086 : j = i;
1637 : }
1638 : }
1639 :
1640 2222636 : sum -= a[j] * a[j];
1641 :
1642 2222636 : return ( sum );
1643 : }
1644 :
1645 :
1646 : /*-------------------------------------------------------------------*
1647 : * EstimateSHBGainShape()
1648 : *
1649 : * Estimate temporal gain parameters
1650 : *-------------------------------------------------------------------*/
1651 :
1652 1138393 : static void EstimateSHBGainShape(
1653 : const int16_t length, /* i : SHB overlap length */
1654 : const float *oriSHB, /* i : target original SHB frame */
1655 : const float *synSHB, /* i : shaped SHB excitation */
1656 : float *subgain, /* o : estimate of gain shape */
1657 : const float *subwin /* i : SHB subframe window */
1658 : )
1659 : {
1660 : const int16_t *skip;
1661 : int16_t i, k;
1662 : float sum_gain, oriNrg, synNrg, sig;
1663 : int16_t num_join, num_gains, join_length;
1664 : float normFact;
1665 :
1666 1138393 : num_join = NUM_SHB_SUBFR / NUM_SHB_SUBGAINS;
1667 1138393 : num_gains = NUM_SHB_SUBGAINS;
1668 1138393 : skip = skip_bands_SWB_TBE;
1669 :
1670 1138393 : if ( length == SHB_OVERLAP_LEN / 2 )
1671 : {
1672 75078 : num_gains = NUM_SHB_SUBFR / 4;
1673 75078 : skip = skip_bands_WB_TBE;
1674 : }
1675 :
1676 : /* calculate and normalize the subgain */
1677 1138393 : sum_gain = (float) 1e-10;
1678 1138393 : oriNrg = 0.0f;
1679 1138393 : synNrg = 0.0f;
1680 :
1681 1138393 : if ( length == SHB_OVERLAP_LEN / 2 )
1682 : {
1683 675702 : for ( i = 0; i < NUM_SHB_SUBFR / 2; i++ )
1684 : {
1685 600624 : if ( ( i & 0x1 ) == 0 )
1686 : {
1687 300312 : oriNrg = 1e-10f;
1688 300312 : synNrg = 1e-10f;
1689 : }
1690 :
1691 600624 : if ( i == 0 )
1692 : {
1693 450468 : for ( k = 0; k < length / 2; k++ )
1694 : {
1695 375390 : sig = oriSHB[skip[i] + k] * subwin[2 * k + 2];
1696 375390 : oriNrg += sig * sig;
1697 :
1698 375390 : sig = synSHB[skip[i] + k] * subwin[2 * k + 2];
1699 375390 : synNrg += sig * sig;
1700 : }
1701 :
1702 450468 : for ( k = length / 2; k < length; k++ )
1703 : {
1704 375390 : sig = oriSHB[skip[i] + k];
1705 375390 : oriNrg += sig * sig;
1706 :
1707 375390 : sig = synSHB[skip[i] + k];
1708 375390 : synNrg += sig * sig;
1709 : }
1710 : }
1711 : else
1712 : {
1713 5781006 : for ( k = 0; k < length; k++ )
1714 : {
1715 5255460 : sig = oriSHB[skip[i] + k] * subwin[k + 1];
1716 5255460 : oriNrg += sig * sig;
1717 :
1718 5255460 : sig = synSHB[skip[i] + k] * subwin[k + 1];
1719 5255460 : synNrg += sig * sig;
1720 : }
1721 : }
1722 :
1723 600624 : if ( i == NUM_SHB_SUBFR / 2 - 1 )
1724 : {
1725 450468 : for ( ; k < 2 * length - length / 2; k++ )
1726 : {
1727 375390 : sig = oriSHB[skip[i] + k] * subwin[3 * length - 2 * k - 2];
1728 375390 : oriNrg += sig * sig;
1729 :
1730 375390 : sig = synSHB[skip[i] + k] * subwin[3 * length - 2 * k - 2];
1731 375390 : synNrg += sig * sig;
1732 : }
1733 : }
1734 : else
1735 : {
1736 5781006 : for ( ; k < 2 * length; k++ )
1737 : {
1738 5255460 : sig = oriSHB[skip[i] + k] * subwin[2 * length - k - 1];
1739 5255460 : oriNrg += sig * sig;
1740 :
1741 5255460 : sig = synSHB[skip[i] + k] * subwin[2 * length - k - 1];
1742 5255460 : synNrg += sig * sig;
1743 : }
1744 : }
1745 :
1746 600624 : if ( ( i & 0x1 ) == 1 )
1747 : {
1748 300312 : subgain[i / 2] = (float) sqrt( oriNrg / synNrg );
1749 300312 : if ( synNrg == 0 )
1750 : {
1751 0 : subgain[i / 2] = 0;
1752 : }
1753 :
1754 300312 : sum_gain += subgain[i / 2] * subgain[i / 2];
1755 : }
1756 : }
1757 : }
1758 : else
1759 : {
1760 1063315 : join_length = num_join * length;
1761 :
1762 5316575 : for ( i = 0; i < num_gains; i++ )
1763 : {
1764 4253260 : oriNrg = 1e-10f;
1765 4253260 : synNrg = 1e-10f;
1766 :
1767 89318460 : for ( k = 0; k < length; k++ )
1768 : {
1769 85065200 : sig = oriSHB[join_length * i + k] * subwin[k + 1];
1770 85065200 : oriNrg += sig * sig;
1771 :
1772 85065200 : sig = synSHB[join_length * i + k] * subwin[k + 1];
1773 85065200 : synNrg += sig * sig;
1774 : }
1775 :
1776 259448860 : for ( k = 0; k < ( join_length - length ); k++ )
1777 : {
1778 255195600 : sig = oriSHB[length + join_length * i + k];
1779 255195600 : oriNrg += sig * sig;
1780 :
1781 255195600 : sig = synSHB[length + join_length * i + k];
1782 255195600 : synNrg += sig * sig;
1783 : }
1784 :
1785 89318460 : for ( k = 0; k < length; k++ )
1786 : {
1787 85065200 : sig = oriSHB[join_length * ( i + 1 ) + k] * subwin[length - k - 1];
1788 85065200 : oriNrg += sig * sig;
1789 :
1790 85065200 : sig = synSHB[join_length * ( i + 1 ) + k] * subwin[length - k - 1];
1791 85065200 : synNrg += sig * sig;
1792 : }
1793 :
1794 4253260 : subgain[i] = (float) sqrt( oriNrg / synNrg );
1795 4253260 : if ( synNrg == 0 )
1796 : {
1797 0 : subgain[i] = 0;
1798 : }
1799 :
1800 4253260 : sum_gain += subgain[i] * subgain[i];
1801 : }
1802 : }
1803 :
1804 : /* normalize the subgain */
1805 1138393 : normFact = (float) sqrt( 1.0f / sum_gain );
1806 1138393 : if ( sum_gain == 0 )
1807 : {
1808 0 : normFact = 0;
1809 : }
1810 :
1811 5691965 : for ( i = 0; i < num_gains; i++ )
1812 : {
1813 4553572 : subgain[i] *= normFact;
1814 : }
1815 :
1816 1138393 : return;
1817 : }
1818 :
1819 :
1820 : /*-------------------------------------------------------------------*
1821 : * Quant_lower_LSF()
1822 : *
1823 : * Quantize the lower half of the LSF vector
1824 : *-------------------------------------------------------------------*/
1825 :
1826 769288 : static void Quant_lower_LSF(
1827 : const float lsf[], /* i : Input LSFs */
1828 : float lsf_q[], /* o : Quantized LSFs */
1829 : int16_t lsf_idx[] /* o : Quantized LSFs indices */
1830 : )
1831 : {
1832 : int16_t i;
1833 :
1834 769288 : lsf_idx[0] = (int16_t) squant( lsf[0], &lsf_q[0], lsf_q_cb[0], lsf_q_cb_size[0] );
1835 3846440 : for ( i = 1; i < NUM_Q_LSF; i++ )
1836 : {
1837 3077152 : lsf_idx[i] = (int16_t) squant( lsf[i] - lsf_q[i - 1], &lsf_q[i], lsf_q_cb[i], lsf_q_cb_size[i] );
1838 3077152 : lsf_q[i] += lsf_q[i - 1];
1839 : }
1840 :
1841 769288 : return;
1842 : }
1843 :
1844 : /*-------------------------------------------------------------------*
1845 : * Quant_mirror_point()
1846 : *
1847 : * Quantize the mirror point
1848 : *-------------------------------------------------------------------*/
1849 :
1850 769288 : static int16_t Quant_mirror_point(
1851 : const float lsf[], /* i : Input LSFs */
1852 : const float lsf_q[],
1853 : float *m /* o : Mirror point */
1854 : )
1855 : {
1856 : float m_diff;
1857 : int16_t m_idx;
1858 :
1859 769288 : m_diff = 0.5f * ( lsf[NUM_Q_LSF] - lsf_q[NUM_Q_LSF - 1] );
1860 769288 : m_idx = (int16_t) squant( m_diff, m, mirror_point_q_cb, MIRROR_POINT_Q_CB_SIZE );
1861 :
1862 769288 : *m += lsf_q[NUM_Q_LSF - 1];
1863 :
1864 769288 : return m_idx;
1865 : }
1866 :
1867 : /*-------------------------------------------------------------------*
1868 : * Find_LSF_grid()
1869 : *
1870 : * Find the best grid for the LSFs
1871 : *-------------------------------------------------------------------*/
1872 :
1873 769288 : static int16_t Find_LSF_grid(
1874 : const float lsf[], /* i : Input LSFs */
1875 : float lsf_q[], /* o : Quantized LSFs */
1876 : const float m /* i : Mirror point */
1877 : )
1878 : {
1879 : float lsf_map[NUM_MAP_LSF];
1880 : float grid[NUM_LSF_GRIDS][NUM_MAP_LSF];
1881 : float offset;
1882 : float last_q_lsf;
1883 : float lsf_t[NUM_MAP_LSF];
1884 : float lsf_smooth[NUM_MAP_LSF];
1885 : float D, D_best;
1886 769288 : int16_t I_best = 0;
1887 : int16_t i, j;
1888 : float scale;
1889 :
1890 4615728 : for ( i = 0; i < NUM_MAP_LSF; i++ )
1891 : {
1892 3846440 : lsf_map[i] = 2 * m - lsf_q[NUM_MAP_LSF - 1 - i];
1893 : }
1894 :
1895 769288 : if ( m > MAX_LSF / 2 )
1896 : {
1897 153418 : offset = lsf_map[0];
1898 153418 : scale = ( MAX_LSF - m ) / m;
1899 920508 : for ( i = 0; i < NUM_MAP_LSF; i++ )
1900 : {
1901 767090 : lsf_map[i] = ( lsf_map[i] - offset ) * scale + offset;
1902 : }
1903 : }
1904 :
1905 769288 : last_q_lsf = lsf_q[NUM_Q_LSF - 1];
1906 769288 : scale = MAX_LSF - last_q_lsf;
1907 :
1908 3846440 : for ( i = 0; i < NUM_LSF_GRIDS; i++ )
1909 : {
1910 18462912 : for ( j = 0; j < NUM_MAP_LSF; j++ )
1911 : {
1912 15385760 : grid[i][j] = lsf_grid[i][j] * scale + last_q_lsf;
1913 : }
1914 : }
1915 :
1916 769288 : D_best = QUANT_DIST_INIT;
1917 3846440 : for ( i = 0; i < NUM_LSF_GRIDS; i++ )
1918 : {
1919 3077152 : D = EPSILON;
1920 18462912 : for ( j = 0; j < NUM_MAP_LSF; j++ )
1921 : {
1922 15385760 : lsf_t[j] = ( 1 - grid_smoothing[j] ) * lsf_map[j] + grid_smoothing[j] * grid[i][j];
1923 15385760 : D += ( lsf_t[j] - lsf[NUM_Q_LSF + j] ) * ( lsf_t[j] - lsf[NUM_Q_LSF + j] );
1924 : }
1925 :
1926 3077152 : if ( D < D_best )
1927 : {
1928 1205471 : mvr2r( lsf_t, lsf_smooth, NUM_MAP_LSF );
1929 1205471 : D_best = D;
1930 1205471 : I_best = i;
1931 : }
1932 : }
1933 :
1934 4615728 : for ( i = 0; i < NUM_MAP_LSF; i++ )
1935 : {
1936 3846440 : lsf_q[NUM_Q_LSF + i] = lsf_smooth[i];
1937 : }
1938 :
1939 769288 : return I_best;
1940 : }
1941 :
1942 : /*-------------------------------------------------------------------*
1943 : * gainFrSmooth_En()
1944 : *
1945 : * Gain frame smoothing and attenuation control
1946 : *-------------------------------------------------------------------*/
1947 :
1948 1063315 : static void gainFrSmooth_En(
1949 : TD_BWE_ENC_HANDLE hSWB_TD,
1950 : const int16_t last_extl,
1951 : float *shb_frame,
1952 : const float *lpc_shb,
1953 : const float *lsp_shb,
1954 : float *MA_lsp_shb_spacing,
1955 : int16_t *frGainAttenuate,
1956 : int16_t *frGainSmoothEn )
1957 : {
1958 1063315 : float lsp_slow_evol_rate = 0, lsp_fast_evol_rate = 0, lsp_spacing = 0.5f;
1959 : float temp_shb_frame[L_FRAME16k + L_SHB_LAHEAD];
1960 : int16_t i;
1961 :
1962 11696465 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
1963 : {
1964 10633150 : lsp_spacing = min( lsp_spacing, (float) ( i == 0 ? lsp_shb[0] : ( lsp_shb[i] - lsp_shb[i - 1] ) ) );
1965 :
1966 : /* estimate the mean square error in lsps from current frame to past frames */
1967 10633150 : lsp_slow_evol_rate += ( lsp_shb[i] - hSWB_TD->lsp_shb_slow_interpl[i] ) * ( lsp_shb[i] - hSWB_TD->lsp_shb_slow_interpl[i] );
1968 10633150 : lsp_fast_evol_rate += ( lsp_shb[i] - hSWB_TD->lsp_shb_fast_interpl[i] ) * ( lsp_shb[i] - hSWB_TD->lsp_shb_fast_interpl[i] );
1969 :
1970 : /* update the slow and fast interpolation lsps for next frame */
1971 10633150 : hSWB_TD->lsp_shb_slow_interpl[i] = 0.7f * hSWB_TD->lsp_shb_slow_interpl[i] + 0.3f * lsp_shb[i];
1972 10633150 : hSWB_TD->lsp_shb_fast_interpl[i] = 0.3f * hSWB_TD->lsp_shb_fast_interpl[i] + 0.7f * lsp_shb[i];
1973 : }
1974 :
1975 1063315 : if ( last_extl != SWB_TBE && last_extl != FB_TBE && lsp_spacing < 0.008f )
1976 : {
1977 1016 : hSWB_TD->lsp_shb_spacing[0] = lsp_spacing;
1978 1016 : hSWB_TD->lsp_shb_spacing[1] = lsp_spacing;
1979 1016 : hSWB_TD->lsp_shb_spacing[2] = lsp_spacing;
1980 1016 : hSWB_TD->prev_frGainAtten = 1;
1981 : }
1982 :
1983 1063315 : *MA_lsp_shb_spacing = 0.1f * hSWB_TD->lsp_shb_spacing[0] + 0.2f * hSWB_TD->lsp_shb_spacing[1] + 0.3f * hSWB_TD->lsp_shb_spacing[2] + 0.4f * lsp_spacing;
1984 :
1985 1063315 : hSWB_TD->lsp_shb_spacing[0] = hSWB_TD->lsp_shb_spacing[1];
1986 1063315 : hSWB_TD->lsp_shb_spacing[1] = hSWB_TD->lsp_shb_spacing[2];
1987 1063315 : hSWB_TD->lsp_shb_spacing[2] = lsp_spacing;
1988 :
1989 1063315 : *frGainAttenuate = 0;
1990 1063315 : *frGainSmoothEn = 0;
1991 :
1992 1063315 : if ( ( lsp_spacing < 0.008f && ( *MA_lsp_shb_spacing < 0.005f || hSWB_TD->prev_frGainAtten == 1 ) ) || lsp_spacing <= 0.0032f )
1993 : {
1994 2994 : *frGainAttenuate = 1;
1995 2994 : mvr2r( shb_frame, temp_shb_frame, L_FRAME16k + L_SHB_LAHEAD );
1996 :
1997 2994 : fir( temp_shb_frame, lpc_shb, shb_frame, hSWB_TD->shb_inv_filt_mem, L_FRAME16k + L_SHB_LAHEAD, LPC_SHB_ORDER, 1 );
1998 :
1999 2994 : if ( lsp_slow_evol_rate < 0.001f && lsp_fast_evol_rate < 0.001f )
2000 : {
2001 622 : *frGainSmoothEn = 1;
2002 : }
2003 : }
2004 :
2005 1063315 : return;
2006 : }
2007 :
2008 : /*-------------------------------------------------------------------*
2009 : * Quant_BWE_LSF()
2010 : *
2011 : * Quantize super highband spectral envolope
2012 : *-------------------------------------------------------------------*/
2013 :
2014 15953 : static void LVQQuant_BWE_LSF(
2015 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle*/
2016 : const float lsf_shb[], /* i : unquanitzed LSFs */
2017 : float Q_lsfs[], /* o : quanitzed LSFs */
2018 : int16_t nbits )
2019 : {
2020 : float lsf[LPC_SHB_ORDER];
2021 : float lsf_q[LPC_SHB_ORDER];
2022 15953 : int16_t i, c2, dim1 = 6;
2023 : int16_t levels[1], numbits_VQ;
2024 :
2025 15953 : float w[8] = { 5.0f, 5.0f, 2.0f, 2.0f, 2.0f, 1.0f, 1.0f, 1.0f };
2026 : float cand[LPC_SHB_ORDER], quant[LPC_SHB_ORDER], e[2], ftmp;
2027 : const float *cb_stage;
2028 : int16_t indices_firstVQ[LSFMBEST];
2029 :
2030 : int16_t Idx, idx_lead, idx_scale;
2031 : UWord32 LVQ_index;
2032 : float dd[LATTICE_DIM], cv_out[LATTICE_DIM];
2033 : int16_t bits_lvq, predictor_bits, empty_bits;
2034 :
2035 15953 : empty_bits = 0; /* This is the difference to 21 bits, if we want lower number of bits */
2036 15953 : nbits -= empty_bits; /* THIS IS TO SIMULATE DIFFERENT NUMBER OF input BITS - should be removed */
2037 :
2038 15953 : c2 = NUM_BITS_SHB_MSLVQ - nbits;
2039 :
2040 15953 : numbits_VQ = config_LSF_BWE[c2 * 3];
2041 15953 : levels[0] = config_LSF_BWE[c2 * 3 + 1];
2042 15953 : predictor_bits = config_LSF_BWE[c2 * 3 + 2];
2043 :
2044 15953 : if ( nbits < 19 )
2045 : {
2046 0 : cb_stage = cb_LSF_BWE[1];
2047 : }
2048 : else
2049 : {
2050 15953 : cb_stage = cb_LSF_BWE[0];
2051 : }
2052 :
2053 175483 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
2054 : {
2055 159530 : lsf[i] = 0.5f - lsf_shb[LPC_SHB_ORDER - 1 - i];
2056 : }
2057 :
2058 : /* remove mean */
2059 15953 : v_sub( lsf, SHB_LSF_mean, Q_lsfs, LPC_SHB_ORDER );
2060 :
2061 : /* 6D multi-stage 6bit coding */
2062 15953 : first_VQstages( &cb_stage, Q_lsfs, levels, 1, w, dim1, 3000, indices_firstVQ );
2063 :
2064 :
2065 : /* 8D MSLVQ coding */
2066 15953 : bits_lvq = nbits - numbits_VQ - predictor_bits;
2067 15953 : mvr2r( SHB_LSF_mean, cand, LPC_SHB_ORDER );
2068 15953 : Idx = indices_firstVQ[0];
2069 15953 : v_add( cand, cb_stage + Idx * dim1, cand, dim1 );
2070 :
2071 : /* LVQ quantization */
2072 15953 : mvr2r( cand, quant, LPC_SHB_ORDER );
2073 15953 : v_sub( lsf, cand, dd, LATTICE_DIM );
2074 15953 : mslvq( dd, quant, cv_out, &idx_lead, &idx_scale, w, 0, ( nbits < 19 ), bits_lvq );
2075 :
2076 15953 : if ( idx_scale == -1 )
2077 : {
2078 1 : set_zero( cv_out, LATTICE_DIM );
2079 : }
2080 15953 : v_add( cand, quant, cand, LATTICE_DIM );
2081 :
2082 : /* arrange the LSF candidate vector prior to selection to an ascending order*/
2083 15953 : v_sort( cand, 0, LPC_SHB_ORDER - 1 );
2084 :
2085 15953 : set_zero( Q_lsfs, LPC_SHB_ORDER );
2086 : /* mvr2r( mean_lsf, Q_lsfs, LPC_SHB_ORDER ); */
2087 :
2088 15953 : v_add( Q_lsfs, cb_stage + Idx * dim1, Q_lsfs, dim1 );
2089 :
2090 : /* write VQ indexes */
2091 15953 : push_indice( hBstr, IND_SHB_LSF, Idx, numbits_VQ );
2092 :
2093 : /* write MSLVQ index */
2094 15953 : LVQ_index = index_lvq_SHB( idx_lead, idx_scale, bits_lvq, cv_out, ( nbits < 19 ) );
2095 :
2096 15953 : if ( bits_lvq > 15 )
2097 : {
2098 0 : push_indice( hBstr, IND_SHB_LSF, (int16_t) ( ( LVQ_index ) & ( 0xffff >> 1 ) ), 15 );
2099 0 : push_indice( hBstr, IND_SHB_LSF, (int16_t) ( ( LVQ_index ) >> 15 ) & ( 0xffff >> 1 ), bits_lvq - 15 );
2100 : }
2101 : else
2102 : {
2103 15953 : push_indice( hBstr, IND_SHB_LSF, (int16_t) LVQ_index, bits_lvq );
2104 : }
2105 :
2106 15953 : v_add( Q_lsfs, quant, lsf_q, LATTICE_DIM ); /* quantized mean removed data for first 8 dim*/
2107 :
2108 : /* predict last 2 components */
2109 15953 : if ( predictor_bits == 0 )
2110 : {
2111 0 : lsf_q[LATTICE_DIM] = dotp( lsf_q, LastCoefPred_0bit, LATTICE_DIM );
2112 0 : lsf_q[LATTICE_DIM + 1] = dotp( lsf_q, &LastCoefPred_0bit[LATTICE_DIM + 1], LATTICE_DIM );
2113 : }
2114 : else
2115 : {
2116 15953 : v_sub( &lsf[LATTICE_DIM], &SHB_LSF_mean[LATTICE_DIM], &lsf_q[LATTICE_DIM], 2 );
2117 :
2118 47859 : for ( i = 0; i < 2; i++ )
2119 : {
2120 31906 : ftmp = dotp( lsf_q, &LastCoefPred_1bit[2 * ( LATTICE_DIM + 1 ) * i], LATTICE_DIM ) - lsf_q[LATTICE_DIM];
2121 31906 : e[i] = ( ftmp * ftmp );
2122 31906 : ftmp = dotp( lsf_q, &LastCoefPred_1bit[2 * ( LATTICE_DIM + 1 ) * i + LATTICE_DIM + 1], LATTICE_DIM ) - lsf_q[LATTICE_DIM + 1];
2123 31906 : e[i] += ( ftmp * ftmp );
2124 : }
2125 :
2126 15953 : c2 = minimum( e, 2, &ftmp );
2127 :
2128 15953 : lsf_q[LATTICE_DIM] = dotp( lsf_q, &LastCoefPred_1bit[2 * ( LATTICE_DIM + 1 ) * c2], LATTICE_DIM );
2129 15953 : lsf_q[LATTICE_DIM + 1] = dotp( lsf_q, &LastCoefPred_1bit[2 * ( LATTICE_DIM + 1 ) * c2 + LATTICE_DIM + 1], LATTICE_DIM );
2130 :
2131 15953 : push_indice( hBstr, IND_SHB_LSF, c2, 1 );
2132 : }
2133 :
2134 15953 : if ( empty_bits > 0 )
2135 : {
2136 0 : push_indice( hBstr, IND_SHB_LSF, 0, empty_bits );
2137 : }
2138 :
2139 15953 : v_add( SHB_LSF_mean, lsf_q, lsf_q, LPC_SHB_ORDER );
2140 15953 : v_sort( lsf_q, 0, LPC_SHB_ORDER - 1 );
2141 :
2142 175483 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
2143 : {
2144 159530 : Q_lsfs[i] = 0.5f - lsf_q[LPC_SHB_ORDER - 1 - i];
2145 : }
2146 :
2147 15953 : return;
2148 : }
2149 :
2150 : /*-------------------------------------------------------------------*
2151 : * Quant_BWE_LSF()
2152 : *
2153 : * Quantize super highband spectral envolope
2154 : *-------------------------------------------------------------------*/
2155 :
2156 785241 : static void Quant_BWE_LSF(
2157 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
2158 : TD_BWE_ENC_HANDLE hBWE_TD, /* i/o: TD BWE data handle */
2159 : const int16_t codec_mode, /* i : codec mode */
2160 : const float lsf_shb[], /* i : unquanitzed LSFs */
2161 : float Q_lsfs[], /* o : quanitzed LSFs */
2162 : const int32_t extl_brate /* i : extension layer bit rate */
2163 : )
2164 : {
2165 : float lsf[LPC_SHB_ORDER];
2166 : float lsf_q[LPC_SHB_ORDER];
2167 : int16_t lsf_idx[NUM_Q_LSF];
2168 : int16_t i;
2169 : int16_t m_idx;
2170 : float m;
2171 : int16_t grid_idx;
2172 :
2173 785241 : if ( extl_brate == SWB_TBE_1k75 )
2174 : {
2175 15953 : LVQQuant_BWE_LSF( hBstr, lsf_shb, Q_lsfs, NUM_BITS_SHB_MSLVQ );
2176 : }
2177 : else
2178 : {
2179 8462168 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
2180 : {
2181 7692880 : lsf[i] = 0.5f - lsf_shb[LPC_SHB_ORDER - 1 - i];
2182 : }
2183 :
2184 769288 : Quant_lower_LSF( lsf, lsf_q, lsf_idx );
2185 :
2186 4615728 : for ( i = 0; i < NUM_Q_LSF; i++ )
2187 : {
2188 3846440 : if ( codec_mode == MODE2 )
2189 : {
2190 43415 : hBWE_TD->lsf_idx[i] = lsf_idx[i];
2191 : }
2192 : else
2193 : {
2194 3803025 : push_indice( hBstr, IND_SHB_LSF, lsf_idx[i], lsf_q_num_bits[i] );
2195 : }
2196 : }
2197 :
2198 769288 : m_idx = Quant_mirror_point( lsf, lsf_q, &m );
2199 :
2200 769288 : if ( codec_mode == MODE2 )
2201 : {
2202 8683 : hBWE_TD->m_idx = m_idx;
2203 : }
2204 : else
2205 : {
2206 760605 : push_indice( hBstr, IND_SHB_MIRROR, m_idx, MIRROR_POINT_BITS );
2207 : }
2208 :
2209 769288 : grid_idx = Find_LSF_grid( lsf, lsf_q, m );
2210 :
2211 769288 : if ( codec_mode == MODE2 )
2212 : {
2213 8683 : hBWE_TD->grid_idx = grid_idx;
2214 : }
2215 : else
2216 : {
2217 760605 : push_indice( hBstr, IND_SHB_GRID, grid_idx, NUM_LSF_GRID_BITS );
2218 : }
2219 :
2220 8462168 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
2221 : {
2222 7692880 : Q_lsfs[i] = 0.5f - lsf_q[LPC_SHB_ORDER - 1 - i];
2223 : }
2224 : }
2225 :
2226 785241 : return;
2227 : }
2228 :
2229 : /*-------------------------------------------------------------------*
2230 : * closest_centroid()
2231 : *
2232 : * Determine a set of closest VQ centroids for a given input
2233 : *-------------------------------------------------------------------*/
2234 :
2235 414653 : static int16_t closest_centroid(
2236 : const float *data, /* i : input data */
2237 : const float *weights, /* i : weights */
2238 : const float *quantizer, /* i : quantizer table */
2239 : const int16_t centroids, /* i : number of centroids */
2240 : const int16_t length ) /* i : dimension of quantiser */
2241 : {
2242 : int16_t i, j, index;
2243 : float tmp, werr, best_werr;
2244 :
2245 414653 : index = 0;
2246 414653 : best_werr = 1.0E20f;
2247 :
2248 89368193 : for ( i = 0; i < centroids; i++ )
2249 : {
2250 88953540 : werr = 0.0f;
2251 286698375 : for ( j = 0; j < length; j++ )
2252 : {
2253 283992446 : tmp = (float) *( data + j ) - quantizer[i * length + j];
2254 283992446 : werr += (float) ( *( weights + j ) * tmp * tmp );
2255 283992446 : if ( werr > best_werr )
2256 : {
2257 86247611 : break;
2258 : }
2259 : }
2260 :
2261 88953540 : if ( werr < best_werr )
2262 : {
2263 2705928 : best_werr = werr;
2264 2705928 : index = i;
2265 : }
2266 : }
2267 :
2268 414653 : return index;
2269 : }
2270 :
2271 : /*-------------------------------------------------------------------*
2272 : * QuantizeSHBsubgains()
2273 : *
2274 : * Quantize super highband temporal gains
2275 : *-------------------------------------------------------------------*/
2276 :
2277 1124816 : static void QuantizeSHBsubgains(
2278 : Encoder_State *st, /* i/o: encoder state structure */
2279 : float subgains[], /* i/o: super highband temporal gains */
2280 : const int16_t extl /* i : extension layer */
2281 : )
2282 : {
2283 : int16_t i, idxSubGain;
2284 : float Unit_weights10[NUM_SHB_SUBFR];
2285 1124816 : BSTR_ENC_HANDLE hBstr = st->hBstr;
2286 :
2287 1124816 : if ( extl == WB_TBE )
2288 : {
2289 61501 : set_f( Unit_weights10, 1.0f, (int16_t) NUM_SHB_SUBFR / 4 );
2290 :
2291 307505 : for ( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
2292 : {
2293 246004 : subgains[i + NUM_SHB_SUBFR / 4] = 20.0f * (float) log10( subgains[i] );
2294 : }
2295 :
2296 61501 : idxSubGain = closest_centroid( subgains + NUM_SHB_SUBFR / 4, Unit_weights10, HBCB_SubGain5bit, 1 << NUM_BITS_SHB_SUBGAINS, NUM_SHB_SUBFR / 4 );
2297 :
2298 61501 : mvr2r( HBCB_SubGain5bit + idxSubGain * NUM_SHB_SUBFR / 4, subgains, NUM_SHB_SUBFR / 4 );
2299 :
2300 61501 : push_indice( hBstr, IND_SHB_SUBGAIN, idxSubGain, NUM_BITS_SHB_SUBGAINS );
2301 307505 : for ( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
2302 : {
2303 246004 : subgains[i] = (float) pow( 10.0f, subgains[i] / 20.0f );
2304 : }
2305 :
2306 553509 : for ( i = NUM_SHB_SUBFR / 2 - 1; i >= 0; i-- )
2307 : {
2308 492008 : subgains[i] = subgains[i / 2];
2309 : }
2310 : }
2311 : else
2312 : {
2313 5316575 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
2314 : {
2315 4253260 : subgains[i] = (float) log10( subgains[i] + 0.001f );
2316 : }
2317 :
2318 1063315 : idxSubGain = (int16_t) vquant( subgains, 0, subgains, SHBCB_SubGain5bit, NUM_SHB_SUBGAINS, 1 << NUM_BITS_SHB_SUBGAINS );
2319 :
2320 5316575 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
2321 : {
2322 4253260 : subgains[i] = (float) pow( 10.0, subgains[i] );
2323 : }
2324 :
2325 18076355 : for ( i = NUM_SHB_SUBFR - 1; i >= 0; i-- )
2326 : {
2327 17013040 : subgains[i] = subgains[i * NUM_SHB_SUBGAINS / NUM_SHB_SUBFR];
2328 : }
2329 :
2330 1063315 : if ( st->codec_mode == MODE2 )
2331 : {
2332 10014 : st->hBWE_TD->idxSubGains = idxSubGain;
2333 : }
2334 : else
2335 : {
2336 1053301 : push_indice( hBstr, IND_SHB_SUBGAIN, idxSubGain, NUM_BITS_SHB_SUBGAINS );
2337 : }
2338 : }
2339 :
2340 1124816 : return;
2341 : }
2342 :
2343 : /*-------------------------------------------------------------------*
2344 : * Quant_shb_ener_sf()
2345 : *
2346 : * Quantize SHB subframe energies
2347 : *-------------------------------------------------------------------*/
2348 :
2349 215322 : static void Quant_shb_ener_sf(
2350 : Encoder_State *st, /* i/o: encoder state structure */
2351 : float *shb_ener_sf /* i/o: super highband subframe energies */
2352 : )
2353 : {
2354 : int16_t idxSubEner;
2355 : float temp_shb_ener_sf, sum;
2356 :
2357 215322 : sum = *shb_ener_sf;
2358 215322 : *shb_ener_sf = (float) log10( sum );
2359 :
2360 215322 : temp_shb_ener_sf = 0;
2361 215322 : idxSubEner = usquant( *shb_ener_sf, &temp_shb_ener_sf, 0, 0.042f, 1 << NUM_BITS_SHB_ENER_SF );
2362 215322 : *shb_ener_sf = (float) pow( 10.0, temp_shb_ener_sf );
2363 :
2364 215322 : if ( st->codec_mode == MODE2 )
2365 : {
2366 6300 : st->hBWE_TD->idx_shb_fr_gain = idxSubEner;
2367 : }
2368 : else
2369 : {
2370 209022 : push_indice( st->hBstr, IND_SHB_ENER_SF, idxSubEner, NUM_BITS_SHB_ENER_SF );
2371 : }
2372 :
2373 215322 : return;
2374 : }
2375 :
2376 : /*-------------------------------------------------------------------*
2377 : * Quant_shb_res_gshape()
2378 : *
2379 : * Quantize SHB gain shapes in residual domain
2380 : *-------------------------------------------------------------------*/
2381 :
2382 215322 : static void Quant_shb_res_gshape(
2383 : Encoder_State *st, /* i/o: encoder state structure */
2384 : float shb_res_gshape[] /* i/o: super highband gain shapes */
2385 : )
2386 : {
2387 : int16_t i, idxSubGain[NB_SUBFR16k];
2388 :
2389 1291932 : for ( i = 0; i < NB_SUBFR16k; i++ )
2390 : {
2391 1076610 : idxSubGain[i] = usquant( shb_res_gshape[i], &shb_res_gshape[i], 0.125, 0.125f, 1 << NUM_BITS_SHB_RES_GS );
2392 :
2393 1076610 : if ( st->codec_mode == MODE2 )
2394 : {
2395 31500 : st->hBWE_TD->idx_res_gs[i] = idxSubGain[i];
2396 : }
2397 : else
2398 : {
2399 1045110 : push_indice( st->hBstr, IND_SHB_RES_GS + i, idxSubGain[i], NUM_BITS_SHB_RES_GS );
2400 : }
2401 : }
2402 :
2403 215322 : return;
2404 : }
2405 :
2406 : /*-------------------------------------------------------------------*
2407 : * QuantizeSHBframegains()
2408 : *
2409 : * Quantize super highband frame gain
2410 : *-------------------------------------------------------------------*/
2411 :
2412 1138393 : static void QuantizeSHBframegain(
2413 : Encoder_State *st, /* i/o: encoder state structure */
2414 : float *GainFrame, /* i/o: Gain */
2415 : const int16_t extl, /* i : extension layer */
2416 : const int32_t extl_brate, /* i : extension layer bitrate */
2417 : int16_t *rf_gainFrame_ind,
2418 : const int16_t flag_conservative /* i : flag to indicate more conservative gain quantization */
2419 : )
2420 : {
2421 : int16_t idxFrameGain;
2422 : float Q_GainFrame;
2423 1138393 : float Unit_weights1 = 1.0f;
2424 1138393 : BSTR_ENC_HANDLE hBstr = st->hBstr;
2425 :
2426 : float GainFrameLog;
2427 :
2428 1138393 : if ( extl == WB_TBE )
2429 : {
2430 75078 : determine_gain_weights( GainFrame, &( Unit_weights1 ), 1 );
2431 :
2432 75078 : if ( extl_brate == WB_TBE_0k35 )
2433 : {
2434 13577 : singlevectortest_gain( GainFrame, 1, 1 << NUM_BITS_SHB_FrameGain_LBR_WB, &idxFrameGain, &( Unit_weights1 ), &Q_GainFrame, SHBCB_FrameGain16 );
2435 :
2436 13577 : if ( Q_GainFrame > *GainFrame * 1.06f && idxFrameGain > 0 ) /* 1.06 = +0.5 dB */
2437 : {
2438 734 : idxFrameGain--;
2439 734 : Q_GainFrame = SHBCB_FrameGain16[idxFrameGain];
2440 : }
2441 :
2442 13577 : if ( st->codec_mode == MODE2 )
2443 : {
2444 2138 : st->hBWE_TD->gFrame_WB = idxFrameGain;
2445 : }
2446 : else
2447 : {
2448 11439 : push_indice( hBstr, IND_SHB_FRAMEGAIN, idxFrameGain, NUM_BITS_SHB_FrameGain_LBR_WB );
2449 : }
2450 :
2451 13577 : if ( st->rf_mode )
2452 : {
2453 736 : *rf_gainFrame_ind = idxFrameGain;
2454 : }
2455 : }
2456 : else
2457 : {
2458 61501 : singlevectortest_gain( GainFrame, 1, 1 << NUM_BITS_SHB_FrameGain, &idxFrameGain, &( Unit_weights1 ), &Q_GainFrame, SHBCB_FrameGain64 );
2459 :
2460 61501 : push_indice( hBstr, IND_SHB_FRAMEGAIN, idxFrameGain, NUM_BITS_SHB_FrameGain );
2461 :
2462 61501 : if ( st->rf_mode )
2463 : {
2464 0 : *rf_gainFrame_ind = idxFrameGain;
2465 : }
2466 : }
2467 : }
2468 : else
2469 : {
2470 1063315 : GainFrameLog = (float) log10( *GainFrame + 0.001f );
2471 :
2472 1063315 : if ( st->extl_brate == SWB_TBE_1k75 )
2473 : {
2474 15953 : idxFrameGain = (int16_t) usquant( GainFrameLog, &Q_GainFrame, SHB_GAIN_QLOW_1k75, SHB_GAIN_QDELTA_1k75, 1 << NUM_BITS_SHB_FRAMEGAIN_1k75 );
2475 : }
2476 : else
2477 : {
2478 1047362 : idxFrameGain = (int16_t) usquant( GainFrameLog, &Q_GainFrame, SHB_GAIN_QLOW, SHB_GAIN_QDELTA, 1 << NUM_BITS_SHB_FRAMEGAIN );
2479 : }
2480 :
2481 1068463 : while ( Q_GainFrame > GainFrameLog + 0.495 * SHB_GAIN_QDELTA && idxFrameGain != 0 )
2482 : {
2483 5148 : idxFrameGain = idxFrameGain - 1;
2484 5148 : Q_GainFrame = idxFrameGain * SHB_GAIN_QDELTA + SHB_GAIN_QLOW;
2485 : }
2486 :
2487 1063315 : if ( flag_conservative && ( Q_GainFrame > GainFrameLog + 0.41 * SHB_GAIN_QDELTA ) && idxFrameGain != 0 )
2488 : {
2489 : /* be more conservative in the quantization process -> prevent energy overshoot */
2490 133 : idxFrameGain = idxFrameGain - 1;
2491 133 : Q_GainFrame = idxFrameGain * SHB_GAIN_QDELTA + SHB_GAIN_QLOW;
2492 : }
2493 :
2494 1063315 : Q_GainFrame = (float) pow( 10.0, Q_GainFrame );
2495 1063315 : if ( st->codec_mode == MODE2 )
2496 : {
2497 10014 : st->hBWE_TD->idxFrameGain = idxFrameGain;
2498 : }
2499 : else
2500 : {
2501 1053301 : if ( st->extl_brate == SWB_TBE_1k75 )
2502 : {
2503 15953 : push_indice( hBstr, IND_SHB_FRAMEGAIN, idxFrameGain, NUM_BITS_SHB_FRAMEGAIN_1k75 );
2504 : }
2505 : else
2506 : {
2507 1037348 : push_indice( hBstr, IND_SHB_FRAMEGAIN, idxFrameGain, NUM_BITS_SHB_FRAMEGAIN );
2508 : }
2509 : }
2510 :
2511 1063315 : if ( st->rf_mode )
2512 : {
2513 672 : *rf_gainFrame_ind = idxFrameGain;
2514 : }
2515 : }
2516 :
2517 1138393 : if ( st->rf_mode )
2518 : {
2519 : /*Currently intended for SWB only. Modify for WB is needed later!*/
2520 1408 : if ( st->hRF->rf_frame_type == RF_NELP )
2521 : {
2522 40 : *rf_gainFrame_ind = idxFrameGain; /* NELP Frame uses full 5 bits */
2523 : }
2524 : else /*RF_ALLPRED, RF_GENPRED, RF_NOPRED modes*/
2525 : {
2526 1368 : if ( *GainFrame <= 1.25 ) /* [0 to 1.25] range --> 0.5*/
2527 : {
2528 336 : *rf_gainFrame_ind = 0;
2529 : }
2530 1032 : else if ( *GainFrame <= 3 ) /* (1.25 to 3] --> 2 */
2531 : {
2532 502 : *rf_gainFrame_ind = 1;
2533 : }
2534 530 : else if ( *GainFrame <= 6 ) /* (3 to 6] --> 4 */
2535 : {
2536 242 : *rf_gainFrame_ind = 2;
2537 : }
2538 : else /* (6 to Inf) --> 8 */
2539 : {
2540 288 : *rf_gainFrame_ind = 3;
2541 : }
2542 : }
2543 : }
2544 :
2545 1138393 : *GainFrame = Q_GainFrame;
2546 :
2547 1138393 : return;
2548 : }
2549 :
2550 :
2551 : /*-------------------------------------------------------------------*
2552 : * determine_gain_weights()
2553 : *
2554 : * Determine weights for gain quantization
2555 : *-------------------------------------------------------------------*/
2556 :
2557 75078 : static void determine_gain_weights(
2558 : const float *gain, /* i : Gain parameter */
2559 : float *weights, /* o : gain weights */
2560 : const int16_t dims /* i : number of gains */
2561 : )
2562 : {
2563 : int16_t j;
2564 :
2565 150156 : for ( j = 0; j < dims; j++ )
2566 : {
2567 75078 : if ( gain[j] > 1e-6 )
2568 : {
2569 75078 : weights[j] = (float) ( pow( fabs( gain[j] ), -0.9f ) );
2570 : }
2571 : else
2572 : {
2573 0 : weights[j] = 501.187233628f;
2574 : }
2575 : }
2576 :
2577 75078 : return;
2578 : }
2579 :
2580 : /*-------------------------------------------------------------------*
2581 : * singlevectortest_gain()
2582 : *
2583 : * VQ for coding superhigh band gain
2584 : *-------------------------------------------------------------------*/
2585 :
2586 75078 : static void singlevectortest_gain(
2587 : const float *inp, /* i : input gain vector */
2588 : const int16_t dimen, /* i : dimension of the input vector */
2589 : const int16_t cb_size, /* i : codebook size */
2590 : int16_t *index, /* o : quanitzation index */
2591 : const float *weight, /* i : Weights for the quanitzation */
2592 : float *recon, /* o : Reconstruction */
2593 : const float *codebook /* i : Codebook */
2594 : )
2595 : {
2596 : int16_t k, interNum, flag;
2597 : float meanU, meanQ;
2598 : int16_t least[4];
2599 :
2600 75078 : interNum = 4;
2601 :
2602 75078 : return_M_Least( inp, dimen, codebook, cb_size, weight, interNum, least );
2603 :
2604 75078 : meanU = sum_f( inp, dimen );
2605 75078 : mvr2r( codebook + dimen * least[0], recon, dimen );
2606 :
2607 75078 : index[0] = least[0];
2608 75078 : flag = 0;
2609 375390 : for ( k = 0; k < interNum; k++ )
2610 : {
2611 300312 : if ( flag == 0 )
2612 : {
2613 80035 : meanQ = sum_f( codebook + dimen * least[k], dimen );
2614 :
2615 80035 : if ( meanQ <= 1.1 * meanU )
2616 : {
2617 74696 : flag = 1;
2618 74696 : mvr2r( codebook + dimen * least[k], recon, dimen );
2619 74696 : index[0] = least[k];
2620 : }
2621 : }
2622 : }
2623 :
2624 75078 : return;
2625 : }
2626 :
2627 : /*-------------------------------------------------------------------*
2628 : * return_M_Least()
2629 : *
2630 : *
2631 : *-------------------------------------------------------------------*/
2632 :
2633 75078 : static void return_M_Least(
2634 : const float *inp, /* i : input */
2635 : const int16_t n_cols, /* i : vector size */
2636 : const float *codebook, /* i : codebook */
2637 : const int16_t num_grp, /* i : number of centroids */
2638 : const float *weight, /* i : gain weights */
2639 : const int16_t interNum, /* i : number on short list prior to 2nd stage search*/
2640 : int16_t *least /* o : return value */
2641 : )
2642 : {
2643 : int16_t i, k;
2644 : float distance[1024], mindist, tmp;
2645 :
2646 75078 : mindist = QUANT_DIST_INIT;
2647 4228374 : for ( i = 0; i < num_grp; i++ )
2648 : {
2649 4153296 : distance[i] = 0;
2650 8306592 : for ( k = 0; k < n_cols; k++ )
2651 : {
2652 4153296 : tmp = inp[k] - codebook[n_cols * i + k];
2653 4153296 : distance[i] += weight[k] * tmp * tmp;
2654 : }
2655 :
2656 4153296 : if ( distance[i] < mindist )
2657 : {
2658 1750429 : mindist = distance[i];
2659 1750429 : least[0] = i;
2660 : }
2661 : }
2662 :
2663 75078 : distance[least[0]] = QUANT_DIST_INIT;
2664 :
2665 300312 : for ( k = 1; k < interNum; k++ )
2666 : {
2667 225234 : mindist = QUANT_DIST_INIT;
2668 12685122 : for ( i = 0; i < num_grp; i++ )
2669 : {
2670 12459888 : if ( distance[i] < mindist )
2671 : {
2672 4990254 : mindist = distance[i];
2673 4990254 : least[k] = i;
2674 : }
2675 : }
2676 :
2677 225234 : distance[least[k]] = QUANT_DIST_INIT;
2678 : }
2679 :
2680 75078 : return;
2681 : }
2682 :
2683 : /*-------------------------------------------------------------------*
2684 : * fb_tbe_reset_enc()
2685 : *
2686 : * Reset the extra parameters needed for FB TBE encoding
2687 : *-------------------------------------------------------------------*/
2688 :
2689 1605681 : void fb_tbe_reset_enc(
2690 : float elliptic_bpf_2_48k_mem[][4],
2691 : float *prev_fb_energy )
2692 : {
2693 1605681 : set_f( elliptic_bpf_2_48k_mem[0], 0.0f, 4 );
2694 1605681 : set_f( elliptic_bpf_2_48k_mem[1], 0.0f, 4 );
2695 1605681 : set_f( elliptic_bpf_2_48k_mem[2], 0.0f, 4 );
2696 1605681 : set_f( elliptic_bpf_2_48k_mem[3], 0.0f, 4 );
2697 1605681 : *prev_fb_energy = 0.0f;
2698 :
2699 1605681 : return;
2700 : }
2701 :
2702 : /*-------------------------------------------------------------------*
2703 : * fb_tbe_enc()
2704 : *
2705 : * FB TBE encoder, 14(resp. 15.5) - 20 kHz band encoding module
2706 : *-------------------------------------------------------------------*/
2707 :
2708 348700 : void fb_tbe_enc(
2709 : Encoder_State *st, /* i/o: encoder state structure */
2710 : const float new_input[], /* i : input speech at 48 kHz sample rate */
2711 : const float fb_exc[] /* i : FB excitation from the SWB part */
2712 : )
2713 : {
2714 : float fb_exc_energy, ratio, temp2;
2715 : float tmp_vec[L_FRAME48k];
2716 : int16_t idxGain;
2717 : float input_fhb[L_FRAME48k];
2718 : int16_t Sample_Delay_HP;
2719 :
2720 348700 : TD_BWE_ENC_HANDLE hBWE_TD = st->hBWE_TD;
2721 :
2722 348700 : if ( st->element_mode == IVAS_CPE_DFT )
2723 : {
2724 : /*don't process first samples of redressed lookahead (energy is not relevant for these samples)*/
2725 58059 : elliptic_bpf_48k_generic( new_input - NS2SA( 48000, DELAY_FIR_RESAMPL_NS ), tmp_vec, hBWE_TD->elliptic_bpf_2_48k_mem, full_band_bpf_2 );
2726 : }
2727 : else
2728 : {
2729 290641 : elliptic_bpf_48k_generic( new_input, tmp_vec, hBWE_TD->elliptic_bpf_2_48k_mem, full_band_bpf_2 );
2730 : }
2731 :
2732 348700 : if ( st->element_mode > IVAS_CPE_DFT || st->element_mode == IVAS_SCE )
2733 : {
2734 289501 : if ( st->L_frame == L_FRAME )
2735 : {
2736 61765 : Sample_Delay_HP = NS2SA( 48000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS ) - L_FRAME48k / 2;
2737 : }
2738 : else
2739 : {
2740 227736 : Sample_Delay_HP = NS2SA( 48000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS ) - L_FRAME48k / 2;
2741 : }
2742 : }
2743 : else
2744 : {
2745 59199 : Sample_Delay_HP = NS2SA( 48000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) - L_FRAME48k / 2;
2746 : }
2747 :
2748 348700 : if ( st->last_extl != FB_TBE )
2749 : {
2750 27188 : set_f( hBWE_TD->old_input_fhb, 0.0f, Sample_Delay_HP );
2751 27188 : set_f( tmp_vec, 0.0f, 320 );
2752 : }
2753 :
2754 348700 : mvr2r( hBWE_TD->old_input_fhb, input_fhb, Sample_Delay_HP );
2755 348700 : mvr2r( tmp_vec, input_fhb + Sample_Delay_HP, L_FRAME48k - Sample_Delay_HP );
2756 348700 : mvr2r( tmp_vec + L_FRAME48k - Sample_Delay_HP, hBWE_TD->old_input_fhb, Sample_Delay_HP );
2757 :
2758 : /* Compute the energy of the Fullband component over 4kHz (16kHz to 20kHz) */
2759 348700 : temp2 = sum2_f( input_fhb, L_FRAME48k / 2 ) + hBWE_TD->prev_fb_energy;
2760 348700 : hBWE_TD->prev_fb_energy = sum2_f( input_fhb + L_FRAME48k / 2, L_FRAME48k / 2 );
2761 348700 : fb_exc_energy = sum2_f( fb_exc, L_FRAME16k ) + EPSILON;
2762 348700 : ratio = (float) sqrt( temp2 / fb_exc_energy );
2763 348700 : if ( ratio < 1.0f )
2764 : {
2765 1826 : ratio = 1.0f;
2766 : }
2767 348700 : idxGain = (int16_t) ( log2_f( (float) ratio ) + 0.5f );
2768 348700 : idxGain = max( 0, min( 15, idxGain ) );
2769 348700 : ratio = (float) ( 1 << idxGain );
2770 :
2771 348700 : if ( st->codec_mode == MODE2 )
2772 : {
2773 268 : hBWE_TD->idxGain = idxGain;
2774 : }
2775 : else
2776 : {
2777 348432 : push_indice( st->hBstr, IND_FB_SLOPE, idxGain, NUM_BITS_FB_FRAMEGAIN_TBE );
2778 : }
2779 :
2780 348700 : return;
2781 : }
2782 :
2783 : /*---------------------------------------------------------------------*
2784 : * tbe_write_bitstream()
2785 : *
2786 : * Write TBE bitstream.
2787 : *---------------------------------------------------------------------*/
2788 :
2789 12152 : void tbe_write_bitstream(
2790 : Encoder_State *st )
2791 : {
2792 : int16_t i;
2793 12152 : TD_BWE_ENC_HANDLE hBWE_TD = st->hBWE_TD;
2794 12152 : BSTR_ENC_HANDLE hBstr = st->hBstr;
2795 :
2796 12152 : if ( ( st->rf_mode || st->total_brate == ACELP_9k60 ) && st->bwidth == WB )
2797 : {
2798 : /* WB LSF */
2799 2138 : push_next_indice( hBstr, hBWE_TD->lsf_WB, NUM_BITS_LBR_WB_LSF );
2800 :
2801 : /* WB frame */
2802 2138 : push_next_indice( hBstr, hBWE_TD->gFrame_WB, NUM_BITS_SHB_FrameGain_LBR_WB );
2803 : }
2804 10014 : else if ( st->total_brate >= ACELP_9k60 && st->total_brate <= ACELP_32k && ( st->bwidth == SWB || st->bwidth == FB ) )
2805 : {
2806 : /* LSF coefficients */
2807 :
2808 10014 : if ( st->rf_mode || st->total_brate == ACELP_9k60 )
2809 : {
2810 1331 : push_next_indice( hBstr, hBWE_TD->lsf_idx[0], 8 );
2811 : }
2812 : else
2813 : {
2814 52098 : for ( i = 0; i < NUM_Q_LSF; i++ )
2815 : {
2816 43415 : push_next_indice( hBstr, hBWE_TD->lsf_idx[i], lsf_q_num_bits[i] );
2817 : }
2818 :
2819 : /* LSF mirror points */
2820 8683 : push_next_indice( hBstr, hBWE_TD->m_idx, MIRROR_POINT_BITS );
2821 :
2822 : /* LSF grid points */
2823 8683 : push_next_indice( hBstr, hBWE_TD->grid_idx, NUM_LSF_GRID_BITS );
2824 : }
2825 :
2826 : /* Gain shape */
2827 10014 : push_next_indice( hBstr, hBWE_TD->idxSubGains, NUM_BITS_SHB_SUBGAINS );
2828 :
2829 : /* frame gain */
2830 10014 : push_next_indice( hBstr, hBWE_TD->idxFrameGain, NUM_BITS_SHB_FRAMEGAIN );
2831 :
2832 10014 : if ( st->total_brate >= ACELP_24k40 )
2833 : {
2834 : /* sub frame energy*/
2835 6300 : push_next_indice( hBstr, hBWE_TD->idx_shb_fr_gain, NUM_BITS_SHB_ENER_SF );
2836 :
2837 : /* gain shapes residual */
2838 37800 : for ( i = 0; i < NB_SUBFR16k; i++ )
2839 : {
2840 31500 : push_next_indice( hBstr, hBWE_TD->idx_res_gs[i], NUM_BITS_SHB_RES_GS );
2841 : }
2842 :
2843 : /* voicing factor */
2844 6300 : push_next_indice( hBstr, hBWE_TD->idx_mixFac, NUM_BITS_SHB_VF );
2845 : }
2846 :
2847 10014 : if ( st->tec_tfa == 1 )
2848 : {
2849 8415 : push_next_indice( hBstr, st->tec_flag, BITS_TEC );
2850 8415 : push_next_indice( hBstr, st->tfa_flag, BITS_TFA );
2851 : }
2852 : }
2853 :
2854 12152 : if ( st->bwidth == FB )
2855 : {
2856 268 : push_next_indice( hBstr, hBWE_TD->idxGain, 4 );
2857 : }
2858 :
2859 12152 : return;
2860 : }
2861 :
2862 : /*---------------------------------------------------------------------*
2863 : * TBEreset_enc()
2864 : *
2865 : *
2866 : *---------------------------------------------------------------------*/
2867 :
2868 2790173 : void TBEreset_enc(
2869 : TD_BWE_ENC_HANDLE hBWE_TD, /* i/o: TD BWE data handle */
2870 : const int16_t last_core, /* i : last core */
2871 : const int16_t bwidth /* i : audio bandwidth */
2872 : )
2873 : {
2874 2790173 : if ( last_core != ACELP_CORE )
2875 : {
2876 2776774 : set_f( hBWE_TD->old_bwe_exc, 0.0f, PIT16k_MAX * 2 );
2877 2776774 : hBWE_TD->bwe_non_lin_prev_scale = 0.f;
2878 : }
2879 :
2880 2790173 : if ( bwidth == WB )
2881 : {
2882 294342 : wb_tbe_extras_reset( hBWE_TD->mem_genSHBexc_filt_down_wb2, hBWE_TD->mem_genSHBexc_filt_down_wb3 );
2883 :
2884 294342 : set_f( hBWE_TD->mem_genSHBexc_filt_down_shb, 0, 7 );
2885 294342 : set_f( hBWE_TD->state_lpc_syn, 0, 10 );
2886 294342 : set_f( hBWE_TD->state_syn_shbexc, 0, L_SHB_LAHEAD / 4 );
2887 294342 : set_f( hBWE_TD->syn_overlap, 0, L_SHB_LAHEAD );
2888 294342 : set_f( hBWE_TD->mem_csfilt, 0, 2 );
2889 : }
2890 2495831 : else if ( bwidth == SWB || bwidth == FB )
2891 : {
2892 2495812 : set_f( hBWE_TD->state_ana_filt_shb, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
2893 :
2894 2495812 : swb_tbe_reset( hBWE_TD->mem_csfilt, hBWE_TD->mem_genSHBexc_filt_down_shb, hBWE_TD->state_lpc_syn, hBWE_TD->syn_overlap, hBWE_TD->state_syn_shbexc, &( hBWE_TD->tbe_demph ), &( hBWE_TD->tbe_premph ), hBWE_TD->mem_stp_swb, &( hBWE_TD->gain_prec_swb ) );
2895 :
2896 2495812 : if ( bwidth == FB )
2897 : {
2898 1594851 : set_f( hBWE_TD->fb_state_lpc_syn, 0, LPC_SHB_ORDER );
2899 1594851 : hBWE_TD->fb_tbe_demph = 0;
2900 1594851 : fb_tbe_reset_enc( hBWE_TD->elliptic_bpf_2_48k_mem, &hBWE_TD->prev_fb_energy );
2901 : }
2902 : }
2903 :
2904 2790173 : return;
2905 : }
|