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 3897 : void InitSWBencBuffer(
89 : TD_BWE_ENC_HANDLE hBWE_TD /* i/o: TD BWE data handle */
90 : )
91 : {
92 : int16_t i;
93 :
94 3897 : set_f( hBWE_TD->old_bwe_exc, 0.0f, ( PIT16k_MAX * 2 ) );
95 3897 : hBWE_TD->bwe_seed[0] = 23;
96 3897 : hBWE_TD->bwe_seed[1] = 59;
97 3897 : set_f( hBWE_TD->old_bwe_exc_extended, 0.0f, NL_BUFF_OFFSET );
98 3897 : hBWE_TD->bwe_non_lin_prev_scale = 0;
99 :
100 3897 : set_f( hBWE_TD->state_ana_filt_shb, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
101 :
102 3897 : set_f( hBWE_TD->elliptic_bpf_2_48k_mem[0], 0.0f, 4 );
103 3897 : set_f( hBWE_TD->elliptic_bpf_2_48k_mem[1], 0.0f, 4 );
104 3897 : set_f( hBWE_TD->elliptic_bpf_2_48k_mem[2], 0.0f, 4 );
105 3897 : set_f( hBWE_TD->elliptic_bpf_2_48k_mem[3], 0.0f, 4 );
106 3897 : hBWE_TD->prev_fb_energy = 0.0f;
107 :
108 3897 : set_f( hBWE_TD->old_speech_shb, 0.0f, L_LOOK_16k + L_SUBFR16k );
109 3897 : set_f( hBWE_TD->old_speech_wb, 0.0f, ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16 );
110 3897 : 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 3897 : InitSWBencBufferStates( hBWE_TD, NULL );
113 :
114 42867 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
115 : {
116 38970 : hBWE_TD->prev_lsp_shb[i] = i / 20.0f;
117 : }
118 :
119 3897 : hBWE_TD->cldfbHBLT = 1.0f;
120 3897 : hBWE_TD->prev_gainFr_SHB = 0;
121 3897 : set_f( hBWE_TD->lsp_shb_slow_interpl, 0, LPC_SHB_ORDER );
122 3897 : set_f( hBWE_TD->lsp_shb_fast_interpl, 0, LPC_SHB_ORDER );
123 3897 : set_f( hBWE_TD->shb_inv_filt_mem, 0, LPC_SHB_ORDER );
124 3897 : set_f( hBWE_TD->lsp_shb_spacing, 0.1f, 3 );
125 3897 : hBWE_TD->prev_swb_GainShape = 0;
126 3897 : hBWE_TD->prev_frGainAtten = 0;
127 3897 : hBWE_TD->prev_wb_GainShape = 0;
128 3897 : set_f( hBWE_TD->fb_state_lpc_syn, 0, LPC_SHB_ORDER );
129 3897 : hBWE_TD->fb_tbe_demph = 0.0f;
130 3897 : hBWE_TD->tilt_mem = 0.0f;
131 :
132 3897 : hBWE_TD->prev_coder_type = GENERIC;
133 3897 : set_f( hBWE_TD->prev_lsf_diff, 0.5f, LPC_SHB_ORDER - 2 );
134 3897 : hBWE_TD->prev_tilt_para = 0.0f;
135 3897 : set_f( hBWE_TD->cur_sub_Aq, 0.0f, M + 1 );
136 :
137 : /* TD BWE post-processing */
138 3897 : hBWE_TD->ptr_mem_stp_swb = hBWE_TD->mem_stp_swb + LPC_SHB_ORDER - 1;
139 3897 : set_f( hBWE_TD->mem_zero_swb, 0, LPC_SHB_ORDER );
140 :
141 42867 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
142 : {
143 38970 : hBWE_TD->swb_lsp_prev_interp[i] = (float) cos( (float) i * EVS_PI / (float) 10.0f );
144 : }
145 :
146 3897 : set_f( hBWE_TD->dec_2_over_3_mem, 0.0f, L_FILT_2OVER3 );
147 3897 : set_f( hBWE_TD->dec_2_over_3_mem_lp, 0.0f, L_FILT_2OVER3_LP );
148 :
149 3897 : return;
150 : }
151 :
152 :
153 : /*-------------------------------------------------------------------*
154 : * InitSWBencBufferStates()
155 : *
156 : * Initialize SWB buffer states
157 : *-------------------------------------------------------------------*/
158 :
159 7822 : 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 7822 : if ( shb_speech != NULL )
165 : {
166 669 : set_f( shb_speech, 0.0f, L_FRAME16k );
167 : }
168 :
169 7822 : set_f( hBWE_TD->old_speech_shb, 0.0f, L_LOOK_16k + L_SUBFR16k );
170 7822 : set_f( hBWE_TD->mem_shb_res, 0.0f, MAX_LEN_MA_FILTER );
171 7822 : set_f( hBWE_TD->old_EnvSHBres, 0.0f, L_FRAME4k );
172 7822 : hBWE_TD->old_mean_EnvSHBres = 0.0f;
173 7822 : hBWE_TD->prev_enr_EnvSHBres = 1.0f;
174 7822 : hBWE_TD->prev_shb_env_tilt = 0.0f;
175 7822 : hBWE_TD->prev_pow_exc16kWhtnd = 1.0f;
176 7822 : hBWE_TD->prev_mix_factor = 1.0f;
177 7822 : hBWE_TD->prev_Env_error = 0.0f;
178 :
179 7822 : return;
180 : }
181 :
182 :
183 : /*-------------------------------------------------------------------*
184 : * ResetSHBbuffer_Enc()
185 : *
186 : *-------------------------------------------------------------------*/
187 :
188 3897 : 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 3897 : set_f( hBWE_TD->mem_genSHBexc_filt_down_shb, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
194 3897 : set_f( hBWE_TD->mem_csfilt, 0, 2 );
195 :
196 : /* states for the filters used in generating SHB signal from SHB excitation*/
197 3897 : set_f( hBWE_TD->state_syn_shbexc, 0, L_SHB_LAHEAD );
198 3897 : set_f( hBWE_TD->state_lpc_syn, 0, LPC_SHB_ORDER );
199 3897 : set_f( hBWE_TD->fb_state_lpc_syn, 0, LPC_SHB_ORDER );
200 3897 : hBWE_TD->fb_tbe_demph = 0;
201 :
202 : /* states for the filters used in generating WB signal from WB excitation*/
203 3897 : set_f( hBWE_TD->decim_state1, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
204 3897 : set_f( hBWE_TD->decim_state2, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
205 3897 : set_f( hBWE_TD->mem_genSHBexc_filt_down_wb2, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
206 3897 : 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 3897 : set_f( hBWE_TD->mem_stp_swb, 0, LPC_SHB_ORDER );
210 3897 : hBWE_TD->gain_prec_swb = 1.0f;
211 3897 : set_f( hBWE_TD->syn_overlap, 0, L_SHB_LAHEAD );
212 3897 : hBWE_TD->tbe_demph = 0.0f;
213 3897 : hBWE_TD->tbe_premph = 0.0f;
214 :
215 3897 : return;
216 : }
217 :
218 :
219 : /*-------------------------------------------------------------------*
220 : * wb_tbe_enc()
221 : *
222 : * WB TBE encoder, 6 - 8 kHz band encoding module
223 : *-------------------------------------------------------------------*/
224 :
225 7241 : 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 7241 : 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 7241 : 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 7241 : hBWE_TD = st->hBWE_TD;
256 7241 : hBstr = st->hBstr;
257 :
258 : /* delay alignment */
259 7241 : delay = ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16;
260 :
261 7241 : hb_new_speech = hb_old_speech + delay;
262 7241 : hb_frame = hb_old_speech + L_SUBFR * 5 / 16 + ana_align_delay;
263 :
264 7241 : mvr2r( hBWE_TD->old_speech_wb, hb_old_speech, delay );
265 7241 : mvr2r( hb_speech, hb_new_speech, L_FRAME16k / 4 );
266 7241 : mvr2r( hb_old_speech + L_FRAME16k / 4, hBWE_TD->old_speech_wb, delay );
267 :
268 7241 : if ( ( st->last_extl != WB_TBE && st->last_extl != WB_BWE ) &&
269 193 : ( st->clas == UNVOICED_CLAS || ( st->voicing[0] < 0.5f && st->voicing[1] < 0.5f && st->voicing[2] < 0.5f ) ) &&
270 130 : ( !st->igf ) )
271 : {
272 : /* In case of unvoiced signals after switching cores, back-propagate the target signal */
273 130 : mvr2r( hb_speech, hb_old_speech, delay );
274 :
275 2730 : for ( i = ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16, j = k = 0; j < L_SUBFR16k; i--, j += 4, k++ )
276 : {
277 2600 : hb_old_speech[i] *= ola_win_shb_switch_fold[j];
278 2600 : hb_old_speech[i] += hb_speech[k] * ola_win_shb_switch_fold[L_SUBFR16k - 4 - j];
279 : }
280 : }
281 :
282 7241 : 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 7241 : if ( st->element_mode > EVS_MONO )
285 : {
286 : /* Ensure R[0] isn't zero when entering Levinson-Durbin */
287 7241 : R[0] = max( R[0], 1.0e-8f );
288 : }
289 :
290 7241 : lev_dur( lpc_wb_temp, R, LPC_SHB_ORDER_WB, ervec_temp );
291 7241 : a2lsp( lsp_wb_temp, lpc_wb_temp, LPC_SHB_ORDER_WB );
292 7241 : lsp_spacing = 0.5f;
293 :
294 50687 : for ( i = 0; i < LPC_SHB_ORDER_WB; i++ )
295 : {
296 43446 : 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 57928 : for ( i = 0; i <= LPC_SHB_ORDER_WB; i++ )
301 : {
302 50687 : R[i] = R[i] * wac[i];
303 : }
304 7241 : R[0] = max( R[0], 1.0e-8f );
305 :
306 7241 : if ( st->rf_mode == 1 || st->extl_brate == WB_TBE_0k35 )
307 : {
308 2457 : lev_dur( lpc_wb, R, LPC_SHB_ORDER_LBR_WB, ervec );
309 :
310 : /* Expand bandwidth of the LP coeffs */
311 14742 : for ( i = 0; i <= LPC_SHB_ORDER_LBR_WB; i++ )
312 : {
313 12285 : lpc_wb[i] *= lpc_weights[i];
314 : }
315 :
316 : /* convert into lsps and calculate weights */
317 2457 : a2lsp( lsp_wb, lpc_wb, LPC_SHB_ORDER_LBR_WB );
318 2457 : lsp_weights( lsp_wb, weights_lsp, LPC_SHB_ORDER_LBR_WB );
319 :
320 : /* Quantization of LSFs */
321 2457 : i = closest_centroid( lsp_wb, weights_lsp, lbr_wb_bwe_lsfvq_cbook_2bit, 4, LPC_SHB_ORDER_LBR_WB );
322 2457 : if ( st->codec_mode == MODE2 )
323 : {
324 0 : hBWE_TD->lsf_WB = i;
325 : }
326 : else
327 : {
328 2457 : push_indice( hBstr, IND_SHB_LSF, i, NUM_BITS_LBR_WB_LSF );
329 : }
330 :
331 2457 : mvr2r( lbr_wb_bwe_lsfvq_cbook_2bit + i * LPC_SHB_ORDER_LBR_WB, lsp_wb, LPC_SHB_ORDER_LBR_WB );
332 :
333 2457 : lsp2a( lpc_wb, lsp_wb, LPC_SHB_ORDER_LBR_WB );
334 2457 : 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 4784 : lev_dur( lpc_wb, R, LPC_SHB_ORDER_WB, ervec );
339 :
340 : /* Expand bandwidth of the LP coeffs */
341 38272 : for ( i = 0; i <= LPC_SHB_ORDER_WB; i++ )
342 : {
343 33488 : lpc_wb[i] *= lpc_weights[i];
344 : }
345 :
346 : /* convert into lsps and calculate weights */
347 4784 : a2lsp( lsp_wb, lpc_wb, LPC_SHB_ORDER_WB );
348 4784 : lsp_weights( lsp_wb, weights_lsp, LPC_SHB_ORDER_WB );
349 :
350 : /* Quantization of LSFs */
351 4784 : i = closest_centroid( lsp_wb, weights_lsp, wb_bwe_lsfvq_cbook_8bit, 256, LPC_SHB_ORDER_WB );
352 4784 : push_indice( hBstr, IND_SHB_LSF, i, NUM_BITS_WB_LSF );
353 :
354 4784 : mvr2r( wb_bwe_lsfvq_cbook_8bit + i * LPC_SHB_ORDER_WB, lsp_wb, LPC_SHB_ORDER_WB );
355 :
356 4784 : lsp2a( lpc_wb, lsp_wb, LPC_SHB_ORDER_WB );
357 : }
358 :
359 7241 : uv_flag = 0;
360 7241 : if ( st->extl_brate == WB_TBE_1k05 && st->coder_type_raw == UNVOICED )
361 : {
362 440 : uv_flag = 1;
363 : }
364 :
365 7241 : mvr2r( voice_factors, vf_modified, NB_SUBFR16k );
366 7241 : if ( st->coder_type == VOICED )
367 : {
368 4920 : for ( i = 1; i < NB_SUBFR; i++ )
369 : {
370 3690 : vf_modified[i] = 0.8f * voice_factors[i] + 0.2f * voice_factors[i - 1];
371 : }
372 :
373 1230 : 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 7241 : mvr2r( hBWE_TD->state_syn_shbexc, shaped_wb_excitation, L_SHB_LAHEAD / 4 );
381 7241 : 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 7241 : prev_pow = sum2_f( shaped_wb_excitation, L_SHB_LAHEAD / 4 );
384 7241 : curr_pow = sum2_f( shaped_wb_excitation + L_SHB_LAHEAD / 4, L_SHB_LAHEAD / 4 );
385 :
386 7241 : if ( st->element_mode > EVS_MONO )
387 : {
388 : /* prevent too low values of energy */
389 7241 : prev_pow = max( 0.00001f, prev_pow );
390 7241 : curr_pow = max( 0.00001f, curr_pow );
391 : }
392 :
393 7241 : if ( voice_factors[0] > 0.75f )
394 : {
395 255 : curr_pow = (float) ( curr_pow * 0.25 );
396 : }
397 :
398 7241 : if ( prev_pow == 0 )
399 : {
400 0 : scale = 0;
401 : }
402 : else
403 : {
404 7241 : scale = (float) sqrt( curr_pow / prev_pow );
405 : }
406 :
407 36205 : for ( i = 0; i < ( L_SHB_LAHEAD / 4 - 1 ); i++ )
408 : {
409 28964 : shaped_wb_excitation[i] *= scale;
410 : }
411 :
412 7241 : scale = (float) sqrt( scale );
413 :
414 7241 : shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] *= scale;
415 :
416 : /* Update WB excitation */
417 7241 : mvr2r( shaped_wb_excitation + L_FRAME16k / 4, hBWE_TD->state_syn_shbexc, L_SHB_LAHEAD / 4 );
418 :
419 : /* estimate the gain shape parameter */
420 7241 : EstimateSHBGainShape( SHB_OVERLAP_LEN / 2, hb_frame, shaped_wb_excitation, GainShape, subwin_wb );
421 :
422 : /* Gain frame adjustment factor */
423 7241 : temp_wb_fac = (float) log( ( GainShape[0] + 0.00001f ) / ( hBWE_TD->prev_wb_GainShape + 0.0001f ) );
424 7241 : feedback = temp_wb_fac * temp_wb_fac;
425 28964 : for ( i = 1; i < NUM_SHB_SUBFR / 4; i++ )
426 : {
427 21723 : temp_wb_fac = (float) log( ( GainShape[i] + 0.00001f ) / ( GainShape[i - 1] + 0.0001f ) );
428 21723 : feedback += ( temp_wb_fac * temp_wb_fac );
429 : }
430 7241 : feedback = 0.4f / ( 1 + 0.5f * feedback );
431 :
432 7241 : temp_wb_fac = hBWE_TD->prev_wb_GainShape;
433 36205 : for ( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
434 : {
435 28964 : GainShape[i] = ( 1 - feedback ) * GainShape[i] + feedback * temp_wb_fac;
436 28964 : temp_wb_fac = GainShape[i];
437 : }
438 7241 : 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 7241 : if ( st->element_mode > EVS_MONO )
442 : {
443 7241 : p2m_in = pow_off_pk_corrected( GainShape, NUM_SHB_SUBFR / 4, 1 );
444 : }
445 : else
446 : {
447 0 : p2m_in = pow_off_pk( GainShape, NUM_SHB_SUBFR / 4, 1 );
448 : }
449 :
450 7241 : if ( st->extl_brate == WB_TBE_0k35 )
451 : {
452 22113 : for ( i = 0; i < 8; i++ )
453 : {
454 19656 : GainShape[i] = RECIP_ROOT_EIGHT;
455 : }
456 : }
457 : else
458 : {
459 4784 : push_indice( hBstr, IND_UV_FLAG, uv_flag, 1 );
460 :
461 : /* Quantization of the subframe gain parameter */
462 4784 : QuantizeSHBsubgains( st, GainShape, st->extl );
463 : }
464 :
465 : /* Compute the power of gains away from the peak gain after quantization */
466 7241 : if ( st->element_mode > EVS_MONO )
467 : {
468 7241 : p2m_out = pow_off_pk_corrected( GainShape, NUM_SHB_SUBFR / 2, 2 );
469 : }
470 : else
471 : {
472 0 : p2m_out = pow_off_pk( GainShape, NUM_SHB_SUBFR / 2, 2 );
473 : }
474 :
475 : /* Estimate the gain parameter */
476 7241 : 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 7241 : if ( p2m_out > 2.0f * p2m_in )
481 : {
482 106 : float temp = 0;
483 106 : if ( p2m_in >= 0 && p2m_out > 0 )
484 : {
485 106 : temp = (float) sqrt( ( 2.0f * p2m_in ) / p2m_out );
486 : }
487 106 : GainFrame *= temp;
488 : }
489 :
490 7241 : pitBufAvg = 0.0025f * sum_f( pitch_buf, NB_SUBFR );
491 7241 : voicingBufAvg = 0.333f * sum_f( st->voicing, 3 );
492 7241 : if ( voicingBufAvg <= 0.0f && ( pitBufAvg != 0 ) )
493 : {
494 0 : voicingBufAvg = pitBufAvg / 1.001f;
495 : }
496 7241 : else if ( voicingBufAvg <= 0.0f )
497 : {
498 0 : voicingBufAvg = 1.0f;
499 : }
500 :
501 7241 : GainFrame *= max( min( (float) ( pitBufAvg / voicingBufAvg ), 1.0f ), 0.7f );
502 :
503 7241 : if ( lsp_spacing < 0.01f )
504 : {
505 4 : GainFrame *= 0.65f;
506 : }
507 :
508 : /* Quantization of the frame gain parameter */
509 7241 : if ( st->igf && st->coder_type == VOICED )
510 : {
511 0 : GainFrame *= 0.5f;
512 : }
513 7241 : else if ( st->igf && ( 0.25f * sum_f( voice_factors, NB_SUBFR ) > 0.35f ) )
514 : {
515 0 : GainFrame *= 0.75f;
516 : }
517 :
518 7241 : if ( st->rf_mode )
519 : {
520 0 : QuantizeSHBframegain( st, &GainFrame, st->extl, st->extl_brate, &st->hRF->RF_bwe_gainFr_ind, 0 );
521 : }
522 : else
523 : {
524 7241 : 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 7241 : ScaleShapedSHB( SHB_OVERLAP_LEN / 2, shaped_wb_excitation, hBWE_TD->syn_overlap, GainShape, GainFrame, window_wb, subwin_wb );
530 :
531 7241 : 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 116997 : 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 116997 : 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 116997 : Env_error = 0.0f;
601 116997 : pitBufAvg = 0.0f;
602 116997 : voicingBufAvg = 0.0f;
603 116997 : set_zero( Env_error_part, NUM_SHB_SUBGAINS );
604 116997 : shb_env_tilt = 0.0f;
605 :
606 116997 : hBWE_TD = st->hBWE_TD;
607 116997 : hBstr = st->hBstr;
608 :
609 116997 : if ( st->element_mode >= IVAS_CPE_DFT && hStereoICBWE != NULL )
610 : {
611 24111 : nlExc16k = hStereoICBWE->nlExc16k;
612 24111 : mixExc16k = hStereoICBWE->mixExc16k;
613 24111 : MSFlag = hStereoICBWE->MSFlag;
614 : }
615 : else
616 : {
617 92886 : nlExc16k = NULL;
618 92886 : mixExc16k = NULL;
619 92886 : MSFlag = 0;
620 : }
621 :
622 : /* initializations */
623 116997 : set_f( shaped_shb_excitationTemp, 0.0f, L_FRAME16k );
624 :
625 116997 : if ( st->element_mode == IVAS_CPE_TD && ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 ) )
626 : {
627 4236 : 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 112761 : ana_align_delay = -L_SHB_LAHEAD - ( NL_BUFF_OFFSET / 2 );
632 : }
633 :
634 : /* compensate for the delay in target generation and subframe LA */
635 116997 : shb_frame = shb_old_speech + L_SUBFR16k + ana_align_delay;
636 :
637 116997 : set_f( shb_old_speech, 0, L_LOOK_16k + L_SUBFR16k + L_FRAME16k );
638 :
639 : /* set up the speech buffers for TBE processing */
640 116997 : delay = L_LOOK_16k + L_SUBFR16k;
641 116997 : if ( st->element_mode == IVAS_CPE_TD )
642 : {
643 5708 : shb_new_speech = shb_old_speech + delay - L_MEM_RECALC_TBE_16K;
644 : }
645 : else
646 : {
647 111289 : shb_new_speech = shb_old_speech + delay;
648 : }
649 :
650 116997 : mvr2r( hBWE_TD->old_speech_shb, shb_old_speech, delay );
651 116997 : mvr2r( shb_speech, shb_new_speech, L_FRAME16k );
652 116997 : mvr2r( shb_old_speech + L_FRAME16k, hBWE_TD->old_speech_shb, delay );
653 :
654 116997 : 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 116997 : if ( st->rf_mode || st->extl_brate == SWB_TBE_0k95 || st->extl_brate == SWB_TBE_1k10 )
658 : {
659 395784 : for ( i = 0; i <= LPC_SHB_ORDER; i++ )
660 : {
661 362802 : R[i] = R[i] * wac_swb[i];
662 : }
663 : }
664 :
665 : /* Set the autocorr[0] element to a non-negative value */
666 116997 : R[0] = max( R[0], 1.0e-8f );
667 :
668 116997 : 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 116997 : set_f( lpc_shb1, 0, M + 1 );
674 116997 : mvr2r( lpc_shb, lpc_shb1, LPC_SHB_ORDER + 1 );
675 :
676 : /* estimate the LP gain */
677 116997 : 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 116997 : if ( enerG > 32 || !( is_numeric_float( enerG ) ) )
682 : {
683 63 : set_f( lpc_shb, 0, LPC_SHB_ORDER + 1 );
684 63 : lev_dur( lpc_shb, R, 2, ervec );
685 : }
686 : }
687 :
688 : /* Expand bandwidth of the LP coeffs */
689 116997 : if ( st->rf_mode || st->extl_brate == SWB_TBE_0k95 || st->extl_brate == SWB_TBE_1k10 )
690 : {
691 395784 : for ( i = 0; i <= LPC_SHB_ORDER; i++ )
692 : {
693 362802 : lpc_shb[i] *= lpc_weights[i];
694 : }
695 : }
696 :
697 : /* convert to LSFs */
698 116997 : stab_check = a2lsp( lsf_shb, lpc_shb, LPC_SHB_ORDER );
699 :
700 116997 : if ( ( st->last_extl != SWB_TBE && st->last_extl != FB_TBE ) || st->ini_frame == 0 )
701 : {
702 103862 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
703 : {
704 94420 : hBWE_TD->prev_lsp_shb[i] = i / 20.0f;
705 : }
706 : }
707 :
708 116997 : if ( stab_check == 0 )
709 : {
710 0 : mvr2r( hBWE_TD->prev_lsp_shb, lsf_shb, LPC_SHB_ORDER );
711 : }
712 :
713 116997 : mvr2r( lsf_shb, hBWE_TD->prev_lsp_shb, LPC_SHB_ORDER );
714 :
715 116997 : mvr2r( lsf_shb, lsf_shb_orig, LPC_SHB_ORDER );
716 :
717 116997 : gainFrSmooth_En( hBWE_TD, st->last_extl, shb_frame, lpc_shb, lsf_shb, &MA_lsp_shb_spacing, &frGainAttenuate, &frGainSmoothEn );
718 :
719 116997 : if ( st->rf_mode || st->extl_brate == SWB_TBE_0k95 || st->extl_brate == SWB_TBE_1k10 )
720 : {
721 32982 : 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 32982 : weights_lsp[3] *= 0.909091f;
725 32982 : weights_lsp[4] *= 0.909091f;
726 :
727 : /* 8-bit VQ, 10 dimension */
728 32982 : i = closest_centroid( lsf_shb, weights_lsp, swb_tbe_lsfvq_cbook_8b, 256, LPC_SHB_ORDER );
729 32982 : mvr2r( swb_tbe_lsfvq_cbook_8b + i * LPC_SHB_ORDER, lsf_shb, LPC_SHB_ORDER );
730 :
731 32982 : set_s( hBWE_TD->lsf_idx, 0, NUM_Q_LSF );
732 :
733 32982 : if ( st->codec_mode == MODE1 )
734 : {
735 32982 : push_indice( hBstr, IND_SHB_LSF, i, 8 );
736 : }
737 : else
738 : {
739 0 : hBWE_TD->lsf_idx[0] = i;
740 : }
741 :
742 32982 : mvr2r( swb_tbe_lsfvq_cbook_8b + i * LPC_SHB_ORDER, lsf_shb, LPC_SHB_ORDER );
743 : }
744 : else
745 : {
746 : /* Quantization of LSFs */
747 84015 : Quant_BWE_LSF( hBstr, st->hBWE_TD, st->codec_mode, lsf_shb, lsf_q, st->extl_brate );
748 :
749 84015 : mvr2r( lsf_q, lsf_shb, LPC_SHB_ORDER );
750 : }
751 :
752 116997 : space_lsfs( lsf_shb, LPC_SHB_ORDER );
753 :
754 : /* voice factor adjustment and gainframe attenuation factor */
755 116997 : sd_uq_q = 0;
756 1286967 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
757 : {
758 : /* Estimate the QD in lsps between UQ and Q*/
759 1169970 : sd_uq_q += ( lsf_shb[i] - lsf_shb_orig[i] ) * ( lsf_shb[i] - lsf_shb_orig[i] );
760 : }
761 :
762 116997 : mvr2r( voice_factors, vf_modified, NB_SUBFR16k );
763 :
764 116997 : if ( st->coder_type == VOICED || mean( voice_factors, 4 ) > 0.4f )
765 : {
766 103836 : for ( i = 1; i < NB_SUBFR; i++ )
767 : {
768 77877 : vf_modified[i] = 0.8f * voice_factors[i] + 0.2f * voice_factors[i - 1];
769 : }
770 :
771 25959 : if ( st->L_frame != L_FRAME )
772 : {
773 12998 : 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 116997 : lsf2lsp( lsf_shb, lsp_shb_2, LPC_SHB_ORDER, 1 );
779 :
780 116997 : if ( st->last_extl == SWB_TBE || st->last_extl == FB_TBE )
781 : {
782 : /* SHB LSP values from prev. frame for interpolation */
783 110006 : 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 6991 : mvr2r( lsp_shb_2, lsp_shb_1, LPC_SHB_ORDER );
789 : }
790 :
791 116997 : lsf_diff[0] = lsf_diff[LPC_SHB_ORDER - 1] = 0.5f;
792 1052973 : for ( i = 1; i < ( LPC_SHB_ORDER - 1 ); i++ )
793 : {
794 935976 : lsf_diff[i] = lsf_shb[i] - lsf_shb[i - 1];
795 : }
796 :
797 116997 : a2rc( hBWE_TD->cur_sub_Aq + 1, refl, M );
798 :
799 : /* LSP interpolation for 13.2 kbps and 16.4 kbps */
800 116997 : tilt_para = 6.6956f * ( 1.0f + refl[0] ) * ( 1.0f + refl[0] ) - 3.8714f * ( 1.0f + refl[0] ) + 1.3041f;
801 116997 : if ( st->last_extl != SWB_TBE )
802 : {
803 434934 : for ( i = 1; i < LPC_SHB_ORDER - 1; i++ )
804 : {
805 386608 : hBWE_TD->prev_lsf_diff[i - 1] = 0.5f * lsf_diff[i];
806 : }
807 : }
808 :
809 116997 : if ( st->extl_brate <= FB_TBE_1k8 )
810 : {
811 91159 : 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 799308 : for ( i = 1; i < ( LPC_SHB_ORDER - 1 ); i++ )
814 : {
815 710496 : 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 88812 : w[0] = w[1];
818 88812 : w[LPC_SHB_ORDER - 1] = w[LPC_SHB_ORDER - 2];
819 :
820 976932 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
821 : {
822 888120 : lsp_temp[i] = lsp_shb_1[i] * ( 1.0f - w[i] ) + lsp_shb_2[i] * w[i];
823 : }
824 : }
825 : else
826 : {
827 2347 : mvr2r( lsp_shb_2, lsp_temp, LPC_SHB_ORDER );
828 : }
829 : }
830 :
831 116997 : mvr2r( lsf_diff + 1, hBWE_TD->prev_lsf_diff, LPC_SHB_ORDER - 2 );
832 116997 : hBWE_TD->prev_tilt_para = tilt_para;
833 :
834 116997 : if ( st->extl_brate >= SWB_TBE_2k8 )
835 : {
836 : /* SHB LSP interpolation */
837 25838 : ptr_lsp_interp_coef = interpol_frac_shb;
838 129190 : for ( j = 0; j < 4; j++ )
839 : {
840 1136872 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
841 : {
842 1033520 : lsp_temp[i] = lsp_shb_1[i] * ( *ptr_lsp_interp_coef ) + lsp_shb_2[i] * ( *( ptr_lsp_interp_coef + 1 ) );
843 : }
844 103352 : ptr_lsp_interp_coef += 2;
845 :
846 103352 : tmp = j * ( LPC_SHB_ORDER + 1 );
847 : /* convert from lsp to lsf */
848 103352 : lsp2lsf( lsp_temp, lpc_shb_sf + tmp, LPC_SHB_ORDER, 1 );
849 : /* convert lsf to lpc for SHB synthesis */
850 103352 : lsp2a( lpc_shb_sf + tmp, lpc_shb_sf + tmp, LPC_SHB_ORDER );
851 103352 : lpc_shb_sf[j * ( LPC_SHB_ORDER + 1 )] = 1.0f;
852 : }
853 :
854 : /* -------- Calculate the SHB Energy -------- */
855 25838 : shb_ener_sf = 0.003125f * sum2_f( shb_frame + L_SHB_LAHEAD, L_FRAME16k );
856 25838 : Quant_shb_ener_sf( st, &shb_ener_sf );
857 :
858 : /* -------- calculate the residuals using the FOUR subframe LPCs -------- */
859 25838 : set_f( shb_res, 0, L_FRAME16k );
860 25838 : residu( lpc_shb_sf, LPC_SHB_ORDER, shb_frame + L_SHB_LAHEAD, shb_res, 80 );
861 25838 : residu( lpc_shb_sf + ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, shb_frame + L_SHB_LAHEAD + 80, shb_res + 80, 80 );
862 25838 : residu( lpc_shb_sf + 2 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, shb_frame + L_SHB_LAHEAD + 160, shb_res + 160, 80 );
863 25838 : residu( lpc_shb_sf + 3 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, shb_frame + L_SHB_LAHEAD + 240, shb_res + 240, 80 );
864 :
865 25838 : set_f( shb_res_gshape, 0, NB_SUBFR16k );
866 155028 : for ( i = 0; i < NB_SUBFR16k; i++ )
867 : {
868 129190 : shb_res_gshape[i] = sum2_f( shb_res + i * 64, 64 );
869 : }
870 :
871 25838 : maximum( shb_res_gshape, NB_SUBFR16k, &normFac );
872 25838 : normFac = (float) 1.0f / ( 0.0001f + normFac );
873 155028 : for ( i = 0; i < NB_SUBFR16k; i++ )
874 : {
875 129190 : shb_res_gshape[i] = (float) sqrt( shb_res_gshape[i] * normFac );
876 : }
877 :
878 25838 : Quant_shb_res_gshape( st, shb_res_gshape );
879 : }
880 91159 : else if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
881 : {
882 21180 : for ( j = 0; j < 4; j++ )
883 : {
884 16944 : tmp = j * ( LPC_SHB_ORDER + 1 );
885 :
886 : /* convert from lsp to lsf */
887 16944 : lsp2lsf( lsp_temp, lpc_shb_sf + tmp, LPC_SHB_ORDER, 1 );
888 :
889 : /* convert lsf to lpc for SHB synthesis */
890 16944 : lsp2a( lpc_shb_sf + tmp, lpc_shb_sf + tmp, LPC_SHB_ORDER );
891 16944 : lpc_shb_sf[j * ( LPC_SHB_ORDER + 1 )] = 1.0f;
892 : }
893 :
894 : /* -------- calculate the residuals using the FOUR subframe LPCs -------- */
895 4236 : set_f( shb_res, 0, L_FRAME16k );
896 4236 : residu( lpc_shb_sf, LPC_SHB_ORDER, shb_frame + L_SHB_LAHEAD, shb_res, 80 );
897 4236 : residu( lpc_shb_sf + ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, shb_frame + L_SHB_LAHEAD + 80, shb_res + 80, 80 );
898 4236 : residu( lpc_shb_sf + 2 * ( LPC_SHB_ORDER + 1 ), LPC_SHB_ORDER, shb_frame + L_SHB_LAHEAD + 160, shb_res + 160, 80 );
899 4236 : 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 116997 : mvr2r( lsp_shb_2, hBWE_TD->swb_lsp_prev_interp, LPC_SHB_ORDER );
904 :
905 : /* convert from lsp to lsf */
906 116997 : lsp2lsf( lsp_temp, lpc_shb, LPC_SHB_ORDER, 1 );
907 :
908 : /* convert lsf to lpc for SHB synthesis */
909 116997 : lsp2a( lpc_shb, lpc_shb, LPC_SHB_ORDER );
910 116997 : lpc_shb[0] = 1.0f;
911 :
912 116997 : mvr2r( hBWE_TD->state_syn_shbexc, shaped_shb_excitation, L_SHB_LAHEAD );
913 :
914 : /* Determine formant PF strength */
915 116997 : 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 116997 : acorr_v2a = 0.0f;
919 116997 : 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 4236 : find_td_envelope( shb_res, L_FRAME16k, 20, hBWE_TD->mem_shb_res, EnvSHBres );
923 :
924 : /* downsample the TD envelope by 4 */
925 343116 : for ( k = 0; k < L_FRAME4k; k++ )
926 : {
927 338880 : EnvSHBres_4k[k] = EnvSHBres[4 * k];
928 : }
929 :
930 : /* calculate long-term mean envelope over four consecutive segments using linear interpolation */
931 4236 : seg_len = L_FRAME4k / 4;
932 4236 : den_seg_len = 1.0f / seg_len;
933 4236 : seg_len_2 = L_FRAME4k / 8;
934 4236 : den_seg_len_2 = 1.0f / ( seg_len_2 - 1 );
935 21180 : for ( k = 0; k < 4; k++ )
936 : {
937 16944 : seg_mean[k] = max( 1.0f, mean( &EnvSHBres_4k[k * seg_len], seg_len ) );
938 16944 : den_seg_mean[k] = 1.0f / seg_mean[k];
939 : }
940 :
941 : /* first half of the first segment */
942 4236 : if ( hBWE_TD->old_mean_EnvSHBres == 0 )
943 : {
944 81 : set_f( env_mean_normf, den_seg_mean[0], seg_len_2 );
945 : }
946 : else
947 : {
948 4155 : env_mean_normf[0] = hBWE_TD->old_mean_EnvSHBres;
949 4155 : step = ( den_seg_mean[0] - hBWE_TD->old_mean_EnvSHBres ) * den_seg_len_2;
950 41550 : for ( i = 1; i < seg_len_2; i++ )
951 : {
952 37395 : env_mean_normf[i] = env_mean_normf[i - 1] + step;
953 : }
954 : }
955 :
956 : /* segments 1-4 */
957 4236 : p_env = &env_mean_normf[seg_len_2];
958 16944 : for ( k = 1; k < 4; k++ )
959 : {
960 12708 : step = ( den_seg_mean[k] - den_seg_mean[k - 1] ) * den_seg_len;
961 266868 : for ( i = 0; i < seg_len; i++ )
962 : {
963 254160 : *p_env = *( p_env - 1 ) + step;
964 254160 : p_env++;
965 : }
966 : }
967 :
968 : /* last half of the last segment */
969 4236 : set_f( p_env, den_seg_mean[3], seg_len_2 );
970 4236 : hBWE_TD->old_mean_EnvSHBres = den_seg_mean[3];
971 :
972 : /* normalize residual SHB envelope with its long-term mean envelope */
973 343116 : for ( k = 0; k < L_FRAME4k; k++ )
974 : {
975 338880 : EnvSHBres_4k_norm[k] = EnvSHBres_4k[k] * env_mean_normf[k];
976 : }
977 :
978 : /* calculate tilt of the long-term mean envelope */
979 4236 : lls_interp_n( seg_mean, 4, &shb_env_tilt, &temp, 0 );
980 :
981 : /* copy previous residual envelope to the buffer */
982 4236 : mvr2r( hBWE_TD->old_EnvSHBres, buf_EnvSHBres, L_FRAME4k );
983 :
984 : /* subtract mean value from the normalized SHB residual envelope */
985 4236 : p_buf = &buf_EnvSHBres[L_FRAME4k];
986 4236 : temp = mean( EnvSHBres_4k_norm, L_FRAME4k );
987 343116 : for ( k = 0; k < L_FRAME4k; k++ )
988 : {
989 338880 : *p_buf++ = EnvSHBres_4k_norm[k] - temp;
990 : }
991 :
992 : /* update memory */
993 4236 : mvr2r( &buf_EnvSHBres[L_FRAME4k], hBWE_TD->old_EnvSHBres, L_FRAME4k );
994 :
995 : /* calculate energy normalization factor for the auto-correlation function */
996 4236 : pow0 = sum2_f( &buf_EnvSHBres[L_FRAME4k], L_FRAME4k ) + 1.0f;
997 4236 : if ( hBWE_TD->prev_enr_EnvSHBres == 1.0f )
998 : {
999 81 : scale = 1.0f / pow0;
1000 : }
1001 : else
1002 : {
1003 4155 : scale = 1.0f / max( pow0, hBWE_TD->prev_enr_EnvSHBres );
1004 : }
1005 4236 : hBWE_TD->prev_enr_EnvSHBres = pow0;
1006 :
1007 : /* calculate normalized auto-correlation function on the residual normalized SHB envelope */
1008 4236 : p_acorr = acorr_EnvSHBres;
1009 4236 : p_buf = &buf_EnvSHBres[L_FRAME4k - ENVSHBRES_ACORR_MIN];
1010 173676 : for ( k = 0; k < ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN; k++ )
1011 : {
1012 169440 : *p_acorr++ = scale * dotp( &buf_EnvSHBres[L_FRAME4k], p_buf--, L_FRAME4k );
1013 : }
1014 :
1015 : /* calculate variance of the SHB autocorrelation function */
1016 4236 : 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 4236 : maximum( acorr_EnvSHBres, ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN, &temp );
1020 4236 : acorr_v2a *= temp;
1021 :
1022 4236 : alpha = 25.0f;
1023 4236 : acorr_v2a = 2.0f / ( 1.0f + (float) expf( -alpha * acorr_v2a ) ) - 1.0f;
1024 :
1025 : /* limit in the range 0.0 - 1.0 */
1026 4236 : acorr_v2a = min( 1.0f, max( 0.0f, acorr_v2a ) );
1027 :
1028 4236 : hBWE_TD->prev_shb_env_tilt = shb_env_tilt;
1029 : }
1030 :
1031 116997 : vf_ind = 20;
1032 116997 : 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 116997 : if ( ( st->element_mode == IVAS_CPE_TD || st->element_mode == IVAS_CPE_DFT ) && st->last_core_brate <= SID_2k40 )
1037 : {
1038 199 : mvr2r( shaped_shb_excitation + L_SHB_LAHEAD, shaped_shb_excitation, L_SHB_LAHEAD );
1039 : }
1040 :
1041 116997 : if ( st->element_mode >= IVAS_CPE_DFT && hStereoICBWE != NULL )
1042 : {
1043 24111 : mvr2r( shaped_shb_excitation + L_SHB_LAHEAD, hStereoICBWE->shbSynthRef, L_FRAME16k );
1044 : }
1045 :
1046 116997 : if ( st->extl_brate >= SWB_TBE_2k8 || st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
1047 : {
1048 30074 : if ( st->codec_mode == MODE2 )
1049 : {
1050 604 : hBWE_TD->idx_mixFac = vf_ind;
1051 : }
1052 : else
1053 : {
1054 29470 : push_indice( hBstr, IND_SHB_VF, vf_ind, NUM_BITS_SHB_VF );
1055 : }
1056 : }
1057 :
1058 116997 : if ( st->extl_brate != SWB_TBE_1k10 && st->extl_brate != SWB_TBE_1k75 )
1059 : {
1060 563805 : for ( i = 0; i < L_FRAME16k; i += L_SUBFR16k )
1061 : {
1062 : /* TD BWE post-processing */
1063 451044 : 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 112761 : mvr2r( shaped_shb_excitationTemp, &shaped_shb_excitation[L_SHB_LAHEAD], L_FRAME16k );
1067 :
1068 112761 : prev_pow = sum2_f( shaped_shb_excitation, L_SHB_LAHEAD + 10 );
1069 112761 : curr_pow = sum2_f( shaped_shb_excitation + L_SHB_LAHEAD + 10, L_SHB_LAHEAD + 10 );
1070 :
1071 112761 : if ( st->element_mode > EVS_MONO )
1072 : {
1073 : /* prevent too low values of energy */
1074 111422 : prev_pow = max( 0.00001f, prev_pow );
1075 111422 : curr_pow = max( 0.00001f, curr_pow );
1076 : }
1077 :
1078 112761 : if ( voice_factors[0] > 0.75f )
1079 : {
1080 3345 : curr_pow = (float) ( curr_pow * 0.25 );
1081 : }
1082 :
1083 112761 : if ( prev_pow == 0 )
1084 : {
1085 0 : scale = 0;
1086 : }
1087 : else
1088 : {
1089 112761 : scale = (float) sqrt( curr_pow / prev_pow );
1090 : }
1091 :
1092 2367981 : for ( i = 0; i < L_SHB_LAHEAD; i++ )
1093 : {
1094 2255220 : shaped_shb_excitation[i] *= scale;
1095 : }
1096 :
1097 1240371 : for ( ; i < L_SHB_LAHEAD + 10; i++ )
1098 : {
1099 1127610 : temp = ( i - 19 ) / 10.0f;
1100 1127610 : 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 4236 : set_f( hBWE_TD->mem_stp_swb, 0, LPC_SHB_ORDER );
1107 4236 : hBWE_TD->gain_prec_swb = 1.0f;
1108 4236 : set_f( hBWE_TD->mem_zero_swb, 0, LPC_SHB_ORDER );
1109 : }
1110 :
1111 : /* Update SHB excitation */
1112 116997 : mvr2r( shaped_shb_excitation + L_FRAME16k, hBWE_TD->state_syn_shbexc, L_SHB_LAHEAD );
1113 :
1114 : /* Estimate the gain-shape parameter */
1115 116997 : EstimateSHBGainShape( SHB_OVERLAP_LEN, shb_frame, shaped_shb_excitation, GainShape, subwin_shb );
1116 :
1117 116997 : 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 21180 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
1121 : {
1122 16944 : if ( Env_error_part[i] > 100.0f )
1123 : {
1124 : /* set gain shape to the average of the remaining gains */
1125 57 : GainShape[i] = ( sum_f( GainShape, NUM_SHB_SUBGAINS ) - GainShape[i] ) / ( NUM_SHB_SUBGAINS - 1 );
1126 : }
1127 : }
1128 : }
1129 :
1130 116997 : if ( st->extl_brate != SWB_TBE_1k10 && st->extl_brate != SWB_TBE_1k75 )
1131 : {
1132 : /* Gain shape BWS/high band low energy fix */
1133 112761 : 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 22705 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
1137 : {
1138 : /* 0.35f +/- delta variation; delta = 0.1*(GS-0.35)*/
1139 18164 : GainShape[i] = 0.315f + 0.1f * GainShape[i];
1140 : }
1141 : }
1142 : }
1143 :
1144 : /* high-band gain control in case of BWS */
1145 116997 : if ( st->extl_brate != SWB_TBE_1k10 && st->extl_brate != SWB_TBE_1k75 )
1146 : {
1147 112761 : if ( st->bwidth_sw_cnt > 0 )
1148 : {
1149 3445 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
1150 : {
1151 2756 : GainShape[i] *= ( (float) ( st->bwidth_sw_cnt ) / (float) BWS_TRAN_PERIOD );
1152 : }
1153 : }
1154 : }
1155 :
1156 : /* Gain shape adjustment factor */
1157 116997 : temp_swb_fac = (float) log( ( GainShape[0] + 0.00001f ) / ( hBWE_TD->prev_swb_GainShape + 0.0001f ) );
1158 116997 : feedback = temp_swb_fac * temp_swb_fac;
1159 467988 : for ( i = 1; i < NUM_SHB_SUBGAINS; i++ )
1160 : {
1161 350991 : temp_swb_fac = (float) log( ( GainShape[i] + 0.00001f ) / ( GainShape[i - 1] + 0.0001f ) );
1162 350991 : feedback += ( temp_swb_fac * temp_swb_fac );
1163 : }
1164 :
1165 116997 : feedback = 0.4f / ( 1 + 0.5f * feedback );
1166 :
1167 : /* calculate tilt in all sub-frame gains */
1168 116997 : mvr2r( GainShape, GainShape_Interp, NUM_SHB_SUBGAINS );
1169 116997 : lls_interp_n( GainShape_Interp, NUM_SHB_SUBGAINS, &GainShape_tilt, &temp, 1 );
1170 :
1171 116997 : if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
1172 : {
1173 4236 : 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 3392 : feedback = lin_interp( acorr_v2a, 0.4f, 0.0f, 0.0f, 0.95f, 0 );
1177 3392 : feedback = min( max( feedback, 0.0f ), 1.0f );
1178 :
1179 16960 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
1180 : {
1181 13568 : GainShape[i] = ( 1 - feedback ) * GainShape[i] + feedback * GainShape_Interp[i];
1182 : }
1183 : }
1184 : }
1185 : else
1186 : {
1187 112761 : if ( frGainAttenuate == 1 || ( sum_f( st->voicing, 3 ) > 2.4f && sum_f( voice_factors, 4 ) > 0.8f ) )
1188 : {
1189 31878 : temp_swb_fac = hBWE_TD->prev_swb_GainShape;
1190 159390 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
1191 : {
1192 127512 : GainShape[i] = ( 1 - feedback ) * GainShape[i] + feedback * temp_swb_fac;
1193 127512 : temp_swb_fac = GainShape[i];
1194 : }
1195 : }
1196 : }
1197 :
1198 116997 : if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
1199 : {
1200 : /* Re-normalize gain shape before quantization */
1201 4236 : sum_gain = sum2_f( GainShape, NUM_SHB_SUBGAINS );
1202 4236 : if ( sum_gain == 0 )
1203 : {
1204 0 : normFact = 0;
1205 : }
1206 : else
1207 : {
1208 4236 : normFact = (float) sqrt( 1.0f / sum_gain );
1209 : }
1210 :
1211 4236 : v_multc( GainShape, normFact, GainShape, NUM_SHB_SUBGAINS );
1212 : }
1213 :
1214 116997 : hBWE_TD->prev_swb_GainShape = GainShape[3];
1215 :
1216 : /* Compute the power of gains away from the peak gain prior to quantization */
1217 116997 : if ( st->element_mode > EVS_MONO )
1218 : {
1219 115658 : p2m_in = pow_off_pk_corrected( GainShape, NUM_SHB_SUBGAINS, 1 );
1220 : }
1221 : else
1222 : {
1223 1339 : p2m_in = pow_off_pk( GainShape, NUM_SHB_SUBGAINS, 1 );
1224 : }
1225 :
1226 : /* Quantization of the gain shape parameter */
1227 116997 : QuantizeSHBsubgains( st, GainShape, st->extl );
1228 :
1229 : /* Compute the power of gains away from the peak gain after quantization */
1230 116997 : if ( st->element_mode > EVS_MONO )
1231 : {
1232 115658 : p2m_out = pow_off_pk_corrected( GainShape, NUM_SHB_SUBFR, 4 );
1233 : }
1234 : else
1235 : {
1236 1339 : p2m_out = pow_off_pk( GainShape, NUM_SHB_SUBFR, 4 );
1237 : }
1238 :
1239 : /* Gain shape smoothing after quantization */
1240 116997 : if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
1241 : {
1242 21180 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
1243 : {
1244 16944 : GainShape_Interp[i] = GainShape[i * NUM_SHB_SUBGAINS];
1245 : }
1246 :
1247 4236 : lls_interp_n( GainShape_Interp, NUM_SHB_SUBGAINS, &GainShape_tilt, &temp, 1 );
1248 :
1249 4236 : if ( vf_ind >= 6 && fabs( GainShape_tilt ) < 0.12f )
1250 : {
1251 2523 : feedback = 0.3f;
1252 12615 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
1253 : {
1254 10092 : GainShape[i] = ( 1 - feedback ) * GainShape[i * NUM_SHB_SUBGAINS] + feedback * GainShape_Interp[i];
1255 : }
1256 :
1257 40368 : for ( i = NUM_SHB_SUBFR - 1; i > 0; i-- )
1258 : {
1259 37845 : GainShape[i] = GainShape[i * NUM_SHB_SUBGAINS / NUM_SHB_SUBFR];
1260 : }
1261 : }
1262 : }
1263 :
1264 : /* Estimate the gain parameter */
1265 116997 : EstimateSHBFrameGain( SHB_OVERLAP_LEN, shb_frame, shaped_shb_excitation, GainShape, &GainFrame, window_shb, subwin_shb );
1266 :
1267 116997 : if ( st->tec_tfa == 1 )
1268 : {
1269 604 : 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 116997 : if ( p2m_out > 2.0f * p2m_in )
1275 : {
1276 2461 : temp = 0.f;
1277 2461 : if ( p2m_in >= 0 && p2m_out > 0 )
1278 : {
1279 2461 : temp = (float) sqrt( ( 2.0f * p2m_in ) / p2m_out );
1280 : }
1281 2461 : GainFrame *= temp;
1282 : }
1283 :
1284 116997 : 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 53 : float fac = GainFrame / hBWE_TD->prev_gainFr_SHB;
1287 :
1288 53 : if ( fac > 4.0f )
1289 : {
1290 3 : GainFrame = 4.0f * GainFrame / fac;
1291 : }
1292 : }
1293 :
1294 116997 : if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
1295 : {
1296 4236 : 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 542 : fact_atten = lin_interp( Env_error, 5.0f, 1.0f, 25.0f, 0.2f, 1 );
1300 542 : GainFrame *= fact_atten;
1301 : }
1302 : /* Frame gain attenuation in case of big error in envelope modelling */
1303 3694 : else if ( Env_error > 100.0f || hBWE_TD->prev_Env_error > 100.0f )
1304 : {
1305 79 : if ( Env_error > 100.0f )
1306 : {
1307 40 : fact_atten = lin_interp( Env_error, 100.0f, 1.0f, 300.0f, 0.1f, 1 );
1308 : }
1309 : else
1310 : {
1311 39 : fact_atten = lin_interp( hBWE_TD->prev_Env_error, 100.0f, 1.0f, 300.0f, 0.5f, 1 );
1312 : }
1313 79 : GainFrame *= fact_atten;
1314 : }
1315 : }
1316 : else
1317 : {
1318 112761 : if ( frGainSmoothEn == 1 && hBWE_TD->prev_gainFr_SHB < GainFrame )
1319 : {
1320 0 : GainFrame = 0.5f * ( hBWE_TD->prev_gainFr_SHB + GainFrame );
1321 : }
1322 : }
1323 :
1324 116997 : if ( frGainAttenuate == 1 && MA_lsp_shb_spacing <= 0.0024f )
1325 : {
1326 35 : GainFrame = (float) pow( GainFrame, 0.8f );
1327 : }
1328 116962 : else if ( hBWE_TD->prev_frGainAtten == 1 && GainFrame > 3.0f * hBWE_TD->prev_gainFr_SHB )
1329 : {
1330 19 : GainFrame *= ( 0.8f + 0.5f * feedback );
1331 : }
1332 116997 : hBWE_TD->prev_frGainAtten = frGainAttenuate;
1333 :
1334 116997 : hBWE_TD->prev_gainFr_SHB = GainFrame;
1335 :
1336 116997 : if ( GainFrame > 153.0f && st->element_mode >= IVAS_CPE_DFT && hStereoICBWE != NULL )
1337 : {
1338 77 : hStereoICBWE->MSFlag = 1;
1339 : }
1340 :
1341 : /* Gain attenuation when the SWB LSF quantization error is larger than a threshold */
1342 116997 : sd_uq_q = ( sd_uq_q / 0.0025f );
1343 116997 : if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
1344 : {
1345 4236 : sd_uq_q = 1 - 0.7f * ( sd_uq_q * sd_uq_q );
1346 : }
1347 : else
1348 : {
1349 112761 : if ( st->L_frame == L_FRAME )
1350 : {
1351 53102 : sd_uq_q = 1 - 0.2f * ( sd_uq_q * sd_uq_q );
1352 : }
1353 : else
1354 : {
1355 59659 : sd_uq_q = 1 - 0.1f * ( sd_uq_q * sd_uq_q );
1356 : }
1357 : }
1358 :
1359 116997 : sd_uq_q = max( min( sd_uq_q, 1.0f ), 0.5f );
1360 :
1361 116997 : if ( st->extl_brate != SWB_TBE_1k10 && st->extl_brate != SWB_TBE_1k75 )
1362 : {
1363 112761 : pitBufAvg = 0.0025f * sum_f( pitch_buf, 4 );
1364 112761 : voicingBufAvg = ( sum_f( voice_factors, 4 ) > 0.6f ) ? 0.333f : 0.1667f;
1365 112761 : voicingBufAvg = voicingBufAvg * sum_f( st->voicing, 3 );
1366 :
1367 112761 : if ( voicingBufAvg <= 0.0f && sd_uq_q * pitBufAvg != 0 )
1368 : {
1369 9 : voicingBufAvg = sd_uq_q * pitBufAvg / 1.001f;
1370 : }
1371 112752 : else if ( voicingBufAvg <= 0.0f )
1372 : {
1373 0 : voicingBufAvg = 1.0f;
1374 : }
1375 : }
1376 :
1377 : /* high-band gain control in case of BWS */
1378 116997 : if ( st->extl_brate != SWB_TBE_1k10 && st->extl_brate != SWB_TBE_1k75 )
1379 : {
1380 112761 : if ( st->bwidth_sw_cnt > 0 )
1381 : {
1382 689 : GainFrame *= ( (float) ( st->bwidth_sw_cnt ) / (float) BWS_TRAN_PERIOD );
1383 : }
1384 : }
1385 :
1386 : /* Controlled gain evolution in SWB for stronger voiced segments */
1387 116997 : if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
1388 : {
1389 4236 : GainFrame *= sd_uq_q;
1390 : }
1391 : else
1392 : {
1393 112761 : GainFrame *= max( min( (float) ( sd_uq_q * pitBufAvg / voicingBufAvg ), 1.0f ), 0.6f );
1394 : }
1395 :
1396 116997 : if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
1397 : {
1398 4236 : 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 112761 : if ( st->L_frame == L_FRAME16k || st->rf_mode == 1 )
1407 : {
1408 : /* Compensate for energy increase mismatch due to memory-less synthesis*/
1409 59659 : GainFrame *= 0.85f;
1410 : }
1411 : }
1412 :
1413 : /* Quantization of the frame gain parameter */
1414 116997 : if ( st->rf_mode )
1415 : {
1416 0 : QuantizeSHBframegain( st, &GainFrame, st->extl, 0, &st->hRF->RF_bwe_gainFr_ind, 0 );
1417 : }
1418 : else
1419 : {
1420 116997 : QuantizeSHBframegain( st, &GainFrame, st->extl, 0, NULL, ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 ) ? 1 : 0 );
1421 : }
1422 :
1423 116997 : if ( hStereoICBWE != NULL )
1424 : {
1425 24111 : push_indice( hBstr, IND_STEREO_ICBWE_MSFLAG, hStereoICBWE->MSFlag, STEREO_ICBWE_MSFLAG_BITS );
1426 : }
1427 :
1428 : /* Backup for the IC BWE parameters */
1429 116997 : if ( st->element_mode >= IVAS_CPE_DFT && hStereoICBWE != NULL )
1430 : {
1431 24111 : mvr2r( lpc_shb, hStereoICBWE->lpSHBRef, LPC_SHB_ORDER + 1 );
1432 : }
1433 :
1434 116997 : if ( st->extl == FB_TBE )
1435 : {
1436 14084517 : for ( i = 0; i < L_FRAME16k; i++ )
1437 : {
1438 14040640 : White_exc16k[i] *= GainFrame * GainShape[NUM_SHB_SUBFR * i / L_FRAME16k];
1439 : }
1440 : }
1441 :
1442 116997 : hBWE_TD->prev_Env_error = Env_error;
1443 :
1444 116997 : 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 124238 : 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 124238 : float oriNrg = 0.0f, synNrg = 0.0f, sum_gain;
1470 : float frame_gain;
1471 :
1472 : /* initilaization */
1473 124238 : l_frame = L_FRAME16k;
1474 124238 : l_shb_lahead = L_SHB_LAHEAD;
1475 124238 : skip = skip_bands_SWB_TBE;
1476 :
1477 124238 : if ( length == SHB_OVERLAP_LEN / 2 )
1478 : {
1479 7241 : skip = skip_bands_WB_TBE;
1480 7241 : l_frame = L_FRAME16k / 4;
1481 7241 : l_shb_lahead = L_SHB_LAHEAD / 4;
1482 : }
1483 :
1484 : /* apply gain for each subframe, and store noise output signal using overlap-add*/
1485 124238 : set_f( mod_syn, 0, l_frame + l_shb_lahead );
1486 :
1487 124238 : if ( length == SHB_OVERLAP_LEN / 2 )
1488 : {
1489 7241 : sum_gain = 0;
1490 43446 : for ( k = 0; k < length / 2; k++ )
1491 : {
1492 36205 : sum_gain = subwin[2 * k + 2] * subgain[0];
1493 36205 : mod_syn[skip[0] + k] = synSHB[skip[0] + k] * sum_gain;
1494 36205 : mod_syn[skip[0] + k + length / 2] = synSHB[skip[0] + k + length / 2] * subgain[0];
1495 : }
1496 :
1497 57928 : for ( i = 1; i < NUM_SHB_SUBFR / 2; i++ )
1498 : {
1499 557557 : for ( k = 0; k < length; k++ )
1500 : {
1501 506870 : sum_gain = subwin[k + 1] * subgain[i] + subwin[length - k - 1] * subgain[i - 1];
1502 506870 : mod_syn[skip[i] + k] = synSHB[skip[i] + k] * sum_gain;
1503 : }
1504 : }
1505 :
1506 43446 : for ( k = 0; k < length / 2; k++ )
1507 : {
1508 36205 : sum_gain = subwin[length - 2 * k - 2] * subgain[i - 1];
1509 36205 : mod_syn[skip[i] + k] = synSHB[skip[i] + k] * sum_gain;
1510 : }
1511 : }
1512 : else
1513 : {
1514 116997 : num_join = NUM_SHB_SUBFR / NUM_SHB_SUBGAINS;
1515 116997 : join_length = num_join * length;
1516 2456937 : for ( k = 0, j = 0; k < length; k++ )
1517 : {
1518 2339940 : mod_syn[j] = synSHB[j] * subwin[k + 1] * subgain[0];
1519 2339940 : j++;
1520 : }
1521 467988 : for ( i = 0; i < NUM_SHB_SUBGAINS - 1; i++ )
1522 : {
1523 21410451 : for ( k = 0; k < join_length - length; k++ )
1524 : {
1525 21059460 : mod_syn[j] = synSHB[j] * subgain[i * num_join];
1526 21059460 : j++;
1527 : }
1528 :
1529 7370811 : for ( k = 0; k < length; k++ )
1530 : {
1531 7019820 : mod_syn[j] = synSHB[j] * ( subwin[length - k - 1] * subgain[i * num_join] + subwin[k + 1] * subgain[( i + 1 ) * num_join] );
1532 7019820 : j++;
1533 : }
1534 : }
1535 7136817 : for ( k = 0; k < join_length - length; k++ )
1536 : {
1537 7019820 : mod_syn[j] = synSHB[j] * subgain[( NUM_SHB_SUBGAINS - 1 ) * num_join];
1538 7019820 : j++;
1539 : }
1540 2456937 : for ( k = 0; k < length; k++ )
1541 : {
1542 2339940 : mod_syn[j] = synSHB[j] * subwin[length - k - 1] * subgain[( NUM_SHB_SUBGAINS - 1 ) * num_join];
1543 2339940 : j++;
1544 : }
1545 : }
1546 :
1547 : /* adjust frame energy */
1548 124238 : oriNrg = (float) 1e-10;
1549 124238 : synNrg = (float) 1e-10;
1550 :
1551 2500383 : for ( i = 0; i < l_shb_lahead; i++ )
1552 : {
1553 2376145 : sig = oriSHB[i] * win_shb[i];
1554 2376145 : oriNrg += sig * sig;
1555 :
1556 2376145 : sig = mod_syn[i] * win_shb[i];
1557 2376145 : synNrg += sig * sig;
1558 : }
1559 :
1560 35766413 : for ( ; i < l_frame; i++ )
1561 : {
1562 35642175 : oriNrg += oriSHB[i] * oriSHB[i];
1563 35642175 : synNrg += mod_syn[i] * mod_syn[i];
1564 : }
1565 :
1566 2500383 : for ( ; i < l_frame + l_shb_lahead; i++ )
1567 : {
1568 2376145 : sig = oriSHB[i] * win_shb[l_frame + l_shb_lahead - 1 - i];
1569 2376145 : oriNrg += sig * sig;
1570 :
1571 2376145 : sig = mod_syn[i] * win_shb[l_frame + l_shb_lahead - 1 - i];
1572 2376145 : synNrg += sig * sig;
1573 : }
1574 :
1575 124238 : frame_gain = (float) sqrt( oriNrg / synNrg );
1576 124238 : if ( ( synNrg ) == 0 )
1577 : {
1578 0 : frame_gain = 0;
1579 : }
1580 :
1581 124238 : *GainFrame = (float) frame_gain;
1582 :
1583 124238 : 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 2678 : static float pow_off_pk(
1594 : float a[],
1595 : const int16_t len,
1596 : const int16_t step )
1597 : {
1598 2678 : int16_t i, j = 0;
1599 : float sum;
1600 :
1601 2678 : sum = a[0] * a[0];
1602 12051 : for ( j = 0, i = 1; i < len; i += step )
1603 : {
1604 9373 : sum += a[i] * a[i];
1605 9373 : if ( a[i] > a[j] )
1606 : {
1607 3158 : j = i;
1608 : }
1609 : }
1610 :
1611 2678 : sum -= a[j] * a[j];
1612 :
1613 2678 : 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 245798 : static float pow_off_pk_corrected(
1623 : float a[],
1624 : const int16_t len,
1625 : const int16_t step )
1626 : {
1627 245798 : int16_t i, j = 0;
1628 : float sum;
1629 :
1630 245798 : sum = a[0] * a[0];
1631 983192 : for ( j = 0, i = step; i < len; i += step )
1632 : {
1633 737394 : sum += a[i] * a[i];
1634 737394 : if ( a[i] > a[j] )
1635 : {
1636 298797 : j = i;
1637 : }
1638 : }
1639 :
1640 245798 : sum -= a[j] * a[j];
1641 :
1642 245798 : return ( sum );
1643 : }
1644 :
1645 :
1646 : /*-------------------------------------------------------------------*
1647 : * EstimateSHBGainShape()
1648 : *
1649 : * Estimate temporal gain parameters
1650 : *-------------------------------------------------------------------*/
1651 :
1652 124238 : 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 124238 : num_join = NUM_SHB_SUBFR / NUM_SHB_SUBGAINS;
1667 124238 : num_gains = NUM_SHB_SUBGAINS;
1668 124238 : skip = skip_bands_SWB_TBE;
1669 :
1670 124238 : if ( length == SHB_OVERLAP_LEN / 2 )
1671 : {
1672 7241 : num_gains = NUM_SHB_SUBFR / 4;
1673 7241 : skip = skip_bands_WB_TBE;
1674 : }
1675 :
1676 : /* calculate and normalize the subgain */
1677 124238 : sum_gain = (float) 1e-10;
1678 124238 : oriNrg = 0.0f;
1679 124238 : synNrg = 0.0f;
1680 :
1681 124238 : if ( length == SHB_OVERLAP_LEN / 2 )
1682 : {
1683 65169 : for ( i = 0; i < NUM_SHB_SUBFR / 2; i++ )
1684 : {
1685 57928 : if ( ( i & 0x1 ) == 0 )
1686 : {
1687 28964 : oriNrg = 1e-10f;
1688 28964 : synNrg = 1e-10f;
1689 : }
1690 :
1691 57928 : if ( i == 0 )
1692 : {
1693 43446 : for ( k = 0; k < length / 2; k++ )
1694 : {
1695 36205 : sig = oriSHB[skip[i] + k] * subwin[2 * k + 2];
1696 36205 : oriNrg += sig * sig;
1697 :
1698 36205 : sig = synSHB[skip[i] + k] * subwin[2 * k + 2];
1699 36205 : synNrg += sig * sig;
1700 : }
1701 :
1702 43446 : for ( k = length / 2; k < length; k++ )
1703 : {
1704 36205 : sig = oriSHB[skip[i] + k];
1705 36205 : oriNrg += sig * sig;
1706 :
1707 36205 : sig = synSHB[skip[i] + k];
1708 36205 : synNrg += sig * sig;
1709 : }
1710 : }
1711 : else
1712 : {
1713 557557 : for ( k = 0; k < length; k++ )
1714 : {
1715 506870 : sig = oriSHB[skip[i] + k] * subwin[k + 1];
1716 506870 : oriNrg += sig * sig;
1717 :
1718 506870 : sig = synSHB[skip[i] + k] * subwin[k + 1];
1719 506870 : synNrg += sig * sig;
1720 : }
1721 : }
1722 :
1723 57928 : if ( i == NUM_SHB_SUBFR / 2 - 1 )
1724 : {
1725 43446 : for ( ; k < 2 * length - length / 2; k++ )
1726 : {
1727 36205 : sig = oriSHB[skip[i] + k] * subwin[3 * length - 2 * k - 2];
1728 36205 : oriNrg += sig * sig;
1729 :
1730 36205 : sig = synSHB[skip[i] + k] * subwin[3 * length - 2 * k - 2];
1731 36205 : synNrg += sig * sig;
1732 : }
1733 : }
1734 : else
1735 : {
1736 557557 : for ( ; k < 2 * length; k++ )
1737 : {
1738 506870 : sig = oriSHB[skip[i] + k] * subwin[2 * length - k - 1];
1739 506870 : oriNrg += sig * sig;
1740 :
1741 506870 : sig = synSHB[skip[i] + k] * subwin[2 * length - k - 1];
1742 506870 : synNrg += sig * sig;
1743 : }
1744 : }
1745 :
1746 57928 : if ( ( i & 0x1 ) == 1 )
1747 : {
1748 28964 : subgain[i / 2] = (float) sqrt( oriNrg / synNrg );
1749 28964 : if ( synNrg == 0 )
1750 : {
1751 0 : subgain[i / 2] = 0;
1752 : }
1753 :
1754 28964 : sum_gain += subgain[i / 2] * subgain[i / 2];
1755 : }
1756 : }
1757 : }
1758 : else
1759 : {
1760 116997 : join_length = num_join * length;
1761 :
1762 584985 : for ( i = 0; i < num_gains; i++ )
1763 : {
1764 467988 : oriNrg = 1e-10f;
1765 467988 : synNrg = 1e-10f;
1766 :
1767 9827748 : for ( k = 0; k < length; k++ )
1768 : {
1769 9359760 : sig = oriSHB[join_length * i + k] * subwin[k + 1];
1770 9359760 : oriNrg += sig * sig;
1771 :
1772 9359760 : sig = synSHB[join_length * i + k] * subwin[k + 1];
1773 9359760 : synNrg += sig * sig;
1774 : }
1775 :
1776 28547268 : for ( k = 0; k < ( join_length - length ); k++ )
1777 : {
1778 28079280 : sig = oriSHB[length + join_length * i + k];
1779 28079280 : oriNrg += sig * sig;
1780 :
1781 28079280 : sig = synSHB[length + join_length * i + k];
1782 28079280 : synNrg += sig * sig;
1783 : }
1784 :
1785 9827748 : for ( k = 0; k < length; k++ )
1786 : {
1787 9359760 : sig = oriSHB[join_length * ( i + 1 ) + k] * subwin[length - k - 1];
1788 9359760 : oriNrg += sig * sig;
1789 :
1790 9359760 : sig = synSHB[join_length * ( i + 1 ) + k] * subwin[length - k - 1];
1791 9359760 : synNrg += sig * sig;
1792 : }
1793 :
1794 467988 : subgain[i] = (float) sqrt( oriNrg / synNrg );
1795 467988 : if ( synNrg == 0 )
1796 : {
1797 0 : subgain[i] = 0;
1798 : }
1799 :
1800 467988 : sum_gain += subgain[i] * subgain[i];
1801 : }
1802 : }
1803 :
1804 : /* normalize the subgain */
1805 124238 : normFact = (float) sqrt( 1.0f / sum_gain );
1806 124238 : if ( sum_gain == 0 )
1807 : {
1808 0 : normFact = 0;
1809 : }
1810 :
1811 621190 : for ( i = 0; i < num_gains; i++ )
1812 : {
1813 496952 : subgain[i] *= normFact;
1814 : }
1815 :
1816 124238 : return;
1817 : }
1818 :
1819 :
1820 : /*-------------------------------------------------------------------*
1821 : * Quant_lower_LSF()
1822 : *
1823 : * Quantize the lower half of the LSF vector
1824 : *-------------------------------------------------------------------*/
1825 :
1826 80677 : 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 80677 : lsf_idx[0] = (int16_t) squant( lsf[0], &lsf_q[0], lsf_q_cb[0], lsf_q_cb_size[0] );
1835 403385 : for ( i = 1; i < NUM_Q_LSF; i++ )
1836 : {
1837 322708 : 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 322708 : lsf_q[i] += lsf_q[i - 1];
1839 : }
1840 :
1841 80677 : return;
1842 : }
1843 :
1844 : /*-------------------------------------------------------------------*
1845 : * Quant_mirror_point()
1846 : *
1847 : * Quantize the mirror point
1848 : *-------------------------------------------------------------------*/
1849 :
1850 80677 : 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 80677 : m_diff = 0.5f * ( lsf[NUM_Q_LSF] - lsf_q[NUM_Q_LSF - 1] );
1860 80677 : m_idx = (int16_t) squant( m_diff, m, mirror_point_q_cb, MIRROR_POINT_Q_CB_SIZE );
1861 :
1862 80677 : *m += lsf_q[NUM_Q_LSF - 1];
1863 :
1864 80677 : return m_idx;
1865 : }
1866 :
1867 : /*-------------------------------------------------------------------*
1868 : * Find_LSF_grid()
1869 : *
1870 : * Find the best grid for the LSFs
1871 : *-------------------------------------------------------------------*/
1872 :
1873 80677 : 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 80677 : int16_t I_best = 0;
1887 : int16_t i, j;
1888 : float scale;
1889 :
1890 484062 : for ( i = 0; i < NUM_MAP_LSF; i++ )
1891 : {
1892 403385 : lsf_map[i] = 2 * m - lsf_q[NUM_MAP_LSF - 1 - i];
1893 : }
1894 :
1895 80677 : if ( m > MAX_LSF / 2 )
1896 : {
1897 16134 : offset = lsf_map[0];
1898 16134 : scale = ( MAX_LSF - m ) / m;
1899 96804 : for ( i = 0; i < NUM_MAP_LSF; i++ )
1900 : {
1901 80670 : lsf_map[i] = ( lsf_map[i] - offset ) * scale + offset;
1902 : }
1903 : }
1904 :
1905 80677 : last_q_lsf = lsf_q[NUM_Q_LSF - 1];
1906 80677 : scale = MAX_LSF - last_q_lsf;
1907 :
1908 403385 : for ( i = 0; i < NUM_LSF_GRIDS; i++ )
1909 : {
1910 1936248 : for ( j = 0; j < NUM_MAP_LSF; j++ )
1911 : {
1912 1613540 : grid[i][j] = lsf_grid[i][j] * scale + last_q_lsf;
1913 : }
1914 : }
1915 :
1916 80677 : D_best = QUANT_DIST_INIT;
1917 403385 : for ( i = 0; i < NUM_LSF_GRIDS; i++ )
1918 : {
1919 322708 : D = EPSILON;
1920 1936248 : for ( j = 0; j < NUM_MAP_LSF; j++ )
1921 : {
1922 1613540 : lsf_t[j] = ( 1 - grid_smoothing[j] ) * lsf_map[j] + grid_smoothing[j] * grid[i][j];
1923 1613540 : D += ( lsf_t[j] - lsf[NUM_Q_LSF + j] ) * ( lsf_t[j] - lsf[NUM_Q_LSF + j] );
1924 : }
1925 :
1926 322708 : if ( D < D_best )
1927 : {
1928 123676 : mvr2r( lsf_t, lsf_smooth, NUM_MAP_LSF );
1929 123676 : D_best = D;
1930 123676 : I_best = i;
1931 : }
1932 : }
1933 :
1934 484062 : for ( i = 0; i < NUM_MAP_LSF; i++ )
1935 : {
1936 403385 : lsf_q[NUM_Q_LSF + i] = lsf_smooth[i];
1937 : }
1938 :
1939 80677 : return I_best;
1940 : }
1941 :
1942 : /*-------------------------------------------------------------------*
1943 : * gainFrSmooth_En()
1944 : *
1945 : * Gain frame smoothing and attenuation control
1946 : *-------------------------------------------------------------------*/
1947 :
1948 116997 : 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 116997 : 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 1286967 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
1963 : {
1964 1169970 : 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 1169970 : 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 1169970 : 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 1169970 : hSWB_TD->lsp_shb_slow_interpl[i] = 0.7f * hSWB_TD->lsp_shb_slow_interpl[i] + 0.3f * lsp_shb[i];
1972 1169970 : hSWB_TD->lsp_shb_fast_interpl[i] = 0.3f * hSWB_TD->lsp_shb_fast_interpl[i] + 0.7f * lsp_shb[i];
1973 : }
1974 :
1975 116997 : if ( last_extl != SWB_TBE && last_extl != FB_TBE && lsp_spacing < 0.008f )
1976 : {
1977 62 : hSWB_TD->lsp_shb_spacing[0] = lsp_spacing;
1978 62 : hSWB_TD->lsp_shb_spacing[1] = lsp_spacing;
1979 62 : hSWB_TD->lsp_shb_spacing[2] = lsp_spacing;
1980 62 : hSWB_TD->prev_frGainAtten = 1;
1981 : }
1982 :
1983 116997 : *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 116997 : hSWB_TD->lsp_shb_spacing[0] = hSWB_TD->lsp_shb_spacing[1];
1986 116997 : hSWB_TD->lsp_shb_spacing[1] = hSWB_TD->lsp_shb_spacing[2];
1987 116997 : hSWB_TD->lsp_shb_spacing[2] = lsp_spacing;
1988 :
1989 116997 : *frGainAttenuate = 0;
1990 116997 : *frGainSmoothEn = 0;
1991 :
1992 116997 : if ( ( lsp_spacing < 0.008f && ( *MA_lsp_shb_spacing < 0.005f || hSWB_TD->prev_frGainAtten == 1 ) ) || lsp_spacing <= 0.0032f )
1993 : {
1994 72 : *frGainAttenuate = 1;
1995 72 : mvr2r( shb_frame, temp_shb_frame, L_FRAME16k + L_SHB_LAHEAD );
1996 :
1997 72 : 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 72 : if ( lsp_slow_evol_rate < 0.001f && lsp_fast_evol_rate < 0.001f )
2000 : {
2001 0 : *frGainSmoothEn = 1;
2002 : }
2003 : }
2004 :
2005 116997 : return;
2006 : }
2007 :
2008 : /*-------------------------------------------------------------------*
2009 : * Quant_BWE_LSF()
2010 : *
2011 : * Quantize super highband spectral envolope
2012 : *-------------------------------------------------------------------*/
2013 :
2014 3338 : 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 3338 : int16_t i, c2, dim1 = 6;
2023 : int16_t levels[1], numbits_VQ;
2024 :
2025 3338 : 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 3338 : empty_bits = 0; /* This is the difference to 21 bits, if we want lower number of bits */
2036 3338 : nbits -= empty_bits; /* THIS IS TO SIMULATE DIFFERENT NUMBER OF input BITS - should be removed */
2037 :
2038 3338 : c2 = NUM_BITS_SHB_MSLVQ - nbits;
2039 :
2040 3338 : numbits_VQ = config_LSF_BWE[c2 * 3];
2041 3338 : levels[0] = config_LSF_BWE[c2 * 3 + 1];
2042 3338 : predictor_bits = config_LSF_BWE[c2 * 3 + 2];
2043 :
2044 3338 : if ( nbits < 19 )
2045 : {
2046 0 : cb_stage = cb_LSF_BWE[1];
2047 : }
2048 : else
2049 : {
2050 3338 : cb_stage = cb_LSF_BWE[0];
2051 : }
2052 :
2053 36718 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
2054 : {
2055 33380 : lsf[i] = 0.5f - lsf_shb[LPC_SHB_ORDER - 1 - i];
2056 : }
2057 :
2058 : /* remove mean */
2059 3338 : v_sub( lsf, SHB_LSF_mean, Q_lsfs, LPC_SHB_ORDER );
2060 :
2061 : /* 6D multi-stage 6bit coding */
2062 3338 : first_VQstages( &cb_stage, Q_lsfs, levels, 1, w, dim1, 3000, indices_firstVQ );
2063 :
2064 :
2065 : /* 8D MSLVQ coding */
2066 3338 : bits_lvq = nbits - numbits_VQ - predictor_bits;
2067 3338 : mvr2r( SHB_LSF_mean, cand, LPC_SHB_ORDER );
2068 3338 : Idx = indices_firstVQ[0];
2069 3338 : v_add( cand, cb_stage + Idx * dim1, cand, dim1 );
2070 :
2071 : /* LVQ quantization */
2072 3338 : mvr2r( cand, quant, LPC_SHB_ORDER );
2073 3338 : v_sub( lsf, cand, dd, LATTICE_DIM );
2074 3338 : mslvq( dd, quant, cv_out, &idx_lead, &idx_scale, w, 0, ( nbits < 19 ), bits_lvq );
2075 :
2076 3338 : if ( idx_scale == -1 )
2077 : {
2078 0 : set_zero( cv_out, LATTICE_DIM );
2079 : }
2080 3338 : v_add( cand, quant, cand, LATTICE_DIM );
2081 :
2082 : /* arrange the LSF candidate vector prior to selection to an ascending order*/
2083 3338 : v_sort( cand, 0, LPC_SHB_ORDER - 1 );
2084 :
2085 3338 : set_zero( Q_lsfs, LPC_SHB_ORDER );
2086 : /* mvr2r( mean_lsf, Q_lsfs, LPC_SHB_ORDER ); */
2087 :
2088 3338 : v_add( Q_lsfs, cb_stage + Idx * dim1, Q_lsfs, dim1 );
2089 :
2090 : /* write VQ indexes */
2091 3338 : push_indice( hBstr, IND_SHB_LSF, Idx, numbits_VQ );
2092 :
2093 : /* write MSLVQ index */
2094 3338 : LVQ_index = index_lvq_SHB( idx_lead, idx_scale, bits_lvq, cv_out, ( nbits < 19 ) );
2095 :
2096 3338 : 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 3338 : push_indice( hBstr, IND_SHB_LSF, (int16_t) LVQ_index, bits_lvq );
2104 : }
2105 :
2106 3338 : v_add( Q_lsfs, quant, lsf_q, LATTICE_DIM ); /* quantized mean removed data for first 8 dim*/
2107 :
2108 : /* predict last 2 components */
2109 3338 : 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 3338 : v_sub( &lsf[LATTICE_DIM], &SHB_LSF_mean[LATTICE_DIM], &lsf_q[LATTICE_DIM], 2 );
2117 :
2118 10014 : for ( i = 0; i < 2; i++ )
2119 : {
2120 6676 : ftmp = dotp( lsf_q, &LastCoefPred_1bit[2 * ( LATTICE_DIM + 1 ) * i], LATTICE_DIM ) - lsf_q[LATTICE_DIM];
2121 6676 : e[i] = ( ftmp * ftmp );
2122 6676 : ftmp = dotp( lsf_q, &LastCoefPred_1bit[2 * ( LATTICE_DIM + 1 ) * i + LATTICE_DIM + 1], LATTICE_DIM ) - lsf_q[LATTICE_DIM + 1];
2123 6676 : e[i] += ( ftmp * ftmp );
2124 : }
2125 :
2126 3338 : c2 = minimum( e, 2, &ftmp );
2127 :
2128 3338 : lsf_q[LATTICE_DIM] = dotp( lsf_q, &LastCoefPred_1bit[2 * ( LATTICE_DIM + 1 ) * c2], LATTICE_DIM );
2129 3338 : lsf_q[LATTICE_DIM + 1] = dotp( lsf_q, &LastCoefPred_1bit[2 * ( LATTICE_DIM + 1 ) * c2 + LATTICE_DIM + 1], LATTICE_DIM );
2130 :
2131 3338 : push_indice( hBstr, IND_SHB_LSF, c2, 1 );
2132 : }
2133 :
2134 3338 : if ( empty_bits > 0 )
2135 : {
2136 0 : push_indice( hBstr, IND_SHB_LSF, 0, empty_bits );
2137 : }
2138 :
2139 3338 : v_add( SHB_LSF_mean, lsf_q, lsf_q, LPC_SHB_ORDER );
2140 3338 : v_sort( lsf_q, 0, LPC_SHB_ORDER - 1 );
2141 :
2142 36718 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
2143 : {
2144 33380 : Q_lsfs[i] = 0.5f - lsf_q[LPC_SHB_ORDER - 1 - i];
2145 : }
2146 :
2147 3338 : return;
2148 : }
2149 :
2150 : /*-------------------------------------------------------------------*
2151 : * Quant_BWE_LSF()
2152 : *
2153 : * Quantize super highband spectral envolope
2154 : *-------------------------------------------------------------------*/
2155 :
2156 84015 : 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 84015 : if ( extl_brate == SWB_TBE_1k75 )
2174 : {
2175 3338 : LVQQuant_BWE_LSF( hBstr, lsf_shb, Q_lsfs, NUM_BITS_SHB_MSLVQ );
2176 : }
2177 : else
2178 : {
2179 887447 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
2180 : {
2181 806770 : lsf[i] = 0.5f - lsf_shb[LPC_SHB_ORDER - 1 - i];
2182 : }
2183 :
2184 80677 : Quant_lower_LSF( lsf, lsf_q, lsf_idx );
2185 :
2186 484062 : for ( i = 0; i < NUM_Q_LSF; i++ )
2187 : {
2188 403385 : if ( codec_mode == MODE2 )
2189 : {
2190 3020 : hBWE_TD->lsf_idx[i] = lsf_idx[i];
2191 : }
2192 : else
2193 : {
2194 400365 : push_indice( hBstr, IND_SHB_LSF, lsf_idx[i], lsf_q_num_bits[i] );
2195 : }
2196 : }
2197 :
2198 80677 : m_idx = Quant_mirror_point( lsf, lsf_q, &m );
2199 :
2200 80677 : if ( codec_mode == MODE2 )
2201 : {
2202 604 : hBWE_TD->m_idx = m_idx;
2203 : }
2204 : else
2205 : {
2206 80073 : push_indice( hBstr, IND_SHB_MIRROR, m_idx, MIRROR_POINT_BITS );
2207 : }
2208 :
2209 80677 : grid_idx = Find_LSF_grid( lsf, lsf_q, m );
2210 :
2211 80677 : if ( codec_mode == MODE2 )
2212 : {
2213 604 : hBWE_TD->grid_idx = grid_idx;
2214 : }
2215 : else
2216 : {
2217 80073 : push_indice( hBstr, IND_SHB_GRID, grid_idx, NUM_LSF_GRID_BITS );
2218 : }
2219 :
2220 887447 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
2221 : {
2222 806770 : Q_lsfs[i] = 0.5f - lsf_q[LPC_SHB_ORDER - 1 - i];
2223 : }
2224 : }
2225 :
2226 84015 : return;
2227 : }
2228 :
2229 : /*-------------------------------------------------------------------*
2230 : * closest_centroid()
2231 : *
2232 : * Determine a set of closest VQ centroids for a given input
2233 : *-------------------------------------------------------------------*/
2234 :
2235 45007 : 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 45007 : index = 0;
2246 45007 : best_werr = 1.0E20f;
2247 :
2248 9876019 : for ( i = 0; i < centroids; i++ )
2249 : {
2250 9831012 : werr = 0.0f;
2251 32538466 : for ( j = 0; j < length; j++ )
2252 : {
2253 32250365 : tmp = (float) *( data + j ) - quantizer[i * length + j];
2254 32250365 : werr += (float) ( *( weights + j ) * tmp * tmp );
2255 32250365 : if ( werr > best_werr )
2256 : {
2257 9542911 : break;
2258 : }
2259 : }
2260 :
2261 9831012 : if ( werr < best_werr )
2262 : {
2263 288101 : best_werr = werr;
2264 288101 : index = i;
2265 : }
2266 : }
2267 :
2268 45007 : return index;
2269 : }
2270 :
2271 : /*-------------------------------------------------------------------*
2272 : * QuantizeSHBsubgains()
2273 : *
2274 : * Quantize super highband temporal gains
2275 : *-------------------------------------------------------------------*/
2276 :
2277 121781 : 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 121781 : BSTR_ENC_HANDLE hBstr = st->hBstr;
2286 :
2287 121781 : if ( extl == WB_TBE )
2288 : {
2289 4784 : set_f( Unit_weights10, 1.0f, (int16_t) NUM_SHB_SUBFR / 4 );
2290 :
2291 23920 : for ( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
2292 : {
2293 19136 : subgains[i + NUM_SHB_SUBFR / 4] = 20.0f * (float) log10( subgains[i] );
2294 : }
2295 :
2296 4784 : idxSubGain = closest_centroid( subgains + NUM_SHB_SUBFR / 4, Unit_weights10, HBCB_SubGain5bit, 1 << NUM_BITS_SHB_SUBGAINS, NUM_SHB_SUBFR / 4 );
2297 :
2298 4784 : mvr2r( HBCB_SubGain5bit + idxSubGain * NUM_SHB_SUBFR / 4, subgains, NUM_SHB_SUBFR / 4 );
2299 :
2300 4784 : push_indice( hBstr, IND_SHB_SUBGAIN, idxSubGain, NUM_BITS_SHB_SUBGAINS );
2301 23920 : for ( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
2302 : {
2303 19136 : subgains[i] = (float) pow( 10.0f, subgains[i] / 20.0f );
2304 : }
2305 :
2306 43056 : for ( i = NUM_SHB_SUBFR / 2 - 1; i >= 0; i-- )
2307 : {
2308 38272 : subgains[i] = subgains[i / 2];
2309 : }
2310 : }
2311 : else
2312 : {
2313 584985 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
2314 : {
2315 467988 : subgains[i] = (float) log10( subgains[i] + 0.001f );
2316 : }
2317 :
2318 116997 : idxSubGain = (int16_t) vquant( subgains, 0, subgains, SHBCB_SubGain5bit, NUM_SHB_SUBGAINS, 1 << NUM_BITS_SHB_SUBGAINS );
2319 :
2320 584985 : for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
2321 : {
2322 467988 : subgains[i] = (float) pow( 10.0, subgains[i] );
2323 : }
2324 :
2325 1988949 : for ( i = NUM_SHB_SUBFR - 1; i >= 0; i-- )
2326 : {
2327 1871952 : subgains[i] = subgains[i * NUM_SHB_SUBGAINS / NUM_SHB_SUBFR];
2328 : }
2329 :
2330 116997 : if ( st->codec_mode == MODE2 )
2331 : {
2332 604 : st->hBWE_TD->idxSubGains = idxSubGain;
2333 : }
2334 : else
2335 : {
2336 116393 : push_indice( hBstr, IND_SHB_SUBGAIN, idxSubGain, NUM_BITS_SHB_SUBGAINS );
2337 : }
2338 : }
2339 :
2340 121781 : return;
2341 : }
2342 :
2343 : /*-------------------------------------------------------------------*
2344 : * Quant_shb_ener_sf()
2345 : *
2346 : * Quantize SHB subframe energies
2347 : *-------------------------------------------------------------------*/
2348 :
2349 25838 : 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 25838 : sum = *shb_ener_sf;
2358 25838 : *shb_ener_sf = (float) log10( sum );
2359 :
2360 25838 : temp_shb_ener_sf = 0;
2361 25838 : idxSubEner = usquant( *shb_ener_sf, &temp_shb_ener_sf, 0, 0.042f, 1 << NUM_BITS_SHB_ENER_SF );
2362 25838 : *shb_ener_sf = (float) pow( 10.0, temp_shb_ener_sf );
2363 :
2364 25838 : if ( st->codec_mode == MODE2 )
2365 : {
2366 604 : st->hBWE_TD->idx_shb_fr_gain = idxSubEner;
2367 : }
2368 : else
2369 : {
2370 25234 : push_indice( st->hBstr, IND_SHB_ENER_SF, idxSubEner, NUM_BITS_SHB_ENER_SF );
2371 : }
2372 :
2373 25838 : return;
2374 : }
2375 :
2376 : /*-------------------------------------------------------------------*
2377 : * Quant_shb_res_gshape()
2378 : *
2379 : * Quantize SHB gain shapes in residual domain
2380 : *-------------------------------------------------------------------*/
2381 :
2382 25838 : 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 155028 : for ( i = 0; i < NB_SUBFR16k; i++ )
2390 : {
2391 129190 : idxSubGain[i] = usquant( shb_res_gshape[i], &shb_res_gshape[i], 0.125, 0.125f, 1 << NUM_BITS_SHB_RES_GS );
2392 :
2393 129190 : if ( st->codec_mode == MODE2 )
2394 : {
2395 3020 : st->hBWE_TD->idx_res_gs[i] = idxSubGain[i];
2396 : }
2397 : else
2398 : {
2399 126170 : push_indice( st->hBstr, IND_SHB_RES_GS + i, idxSubGain[i], NUM_BITS_SHB_RES_GS );
2400 : }
2401 : }
2402 :
2403 25838 : return;
2404 : }
2405 :
2406 : /*-------------------------------------------------------------------*
2407 : * QuantizeSHBframegains()
2408 : *
2409 : * Quantize super highband frame gain
2410 : *-------------------------------------------------------------------*/
2411 :
2412 124238 : 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 124238 : float Unit_weights1 = 1.0f;
2424 124238 : BSTR_ENC_HANDLE hBstr = st->hBstr;
2425 :
2426 : float GainFrameLog;
2427 :
2428 124238 : if ( extl == WB_TBE )
2429 : {
2430 7241 : determine_gain_weights( GainFrame, &( Unit_weights1 ), 1 );
2431 :
2432 7241 : if ( extl_brate == WB_TBE_0k35 )
2433 : {
2434 2457 : singlevectortest_gain( GainFrame, 1, 1 << NUM_BITS_SHB_FrameGain_LBR_WB, &idxFrameGain, &( Unit_weights1 ), &Q_GainFrame, SHBCB_FrameGain16 );
2435 :
2436 2457 : if ( Q_GainFrame > *GainFrame * 1.06f && idxFrameGain > 0 ) /* 1.06 = +0.5 dB */
2437 : {
2438 174 : idxFrameGain--;
2439 174 : Q_GainFrame = SHBCB_FrameGain16[idxFrameGain];
2440 : }
2441 :
2442 2457 : if ( st->codec_mode == MODE2 )
2443 : {
2444 0 : st->hBWE_TD->gFrame_WB = idxFrameGain;
2445 : }
2446 : else
2447 : {
2448 2457 : push_indice( hBstr, IND_SHB_FRAMEGAIN, idxFrameGain, NUM_BITS_SHB_FrameGain_LBR_WB );
2449 : }
2450 :
2451 2457 : if ( st->rf_mode )
2452 : {
2453 0 : *rf_gainFrame_ind = idxFrameGain;
2454 : }
2455 : }
2456 : else
2457 : {
2458 4784 : singlevectortest_gain( GainFrame, 1, 1 << NUM_BITS_SHB_FrameGain, &idxFrameGain, &( Unit_weights1 ), &Q_GainFrame, SHBCB_FrameGain64 );
2459 :
2460 4784 : push_indice( hBstr, IND_SHB_FRAMEGAIN, idxFrameGain, NUM_BITS_SHB_FrameGain );
2461 :
2462 4784 : if ( st->rf_mode )
2463 : {
2464 0 : *rf_gainFrame_ind = idxFrameGain;
2465 : }
2466 : }
2467 : }
2468 : else
2469 : {
2470 116997 : GainFrameLog = (float) log10( *GainFrame + 0.001f );
2471 :
2472 116997 : if ( st->extl_brate == SWB_TBE_1k75 )
2473 : {
2474 3338 : 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 113659 : idxFrameGain = (int16_t) usquant( GainFrameLog, &Q_GainFrame, SHB_GAIN_QLOW, SHB_GAIN_QDELTA, 1 << NUM_BITS_SHB_FRAMEGAIN );
2479 : }
2480 :
2481 117552 : while ( Q_GainFrame > GainFrameLog + 0.495 * SHB_GAIN_QDELTA && idxFrameGain != 0 )
2482 : {
2483 555 : idxFrameGain = idxFrameGain - 1;
2484 555 : Q_GainFrame = idxFrameGain * SHB_GAIN_QDELTA + SHB_GAIN_QLOW;
2485 : }
2486 :
2487 116997 : 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 68 : idxFrameGain = idxFrameGain - 1;
2491 68 : Q_GainFrame = idxFrameGain * SHB_GAIN_QDELTA + SHB_GAIN_QLOW;
2492 : }
2493 :
2494 116997 : Q_GainFrame = (float) pow( 10.0, Q_GainFrame );
2495 116997 : if ( st->codec_mode == MODE2 )
2496 : {
2497 604 : st->hBWE_TD->idxFrameGain = idxFrameGain;
2498 : }
2499 : else
2500 : {
2501 116393 : if ( st->extl_brate == SWB_TBE_1k75 )
2502 : {
2503 3338 : push_indice( hBstr, IND_SHB_FRAMEGAIN, idxFrameGain, NUM_BITS_SHB_FRAMEGAIN_1k75 );
2504 : }
2505 : else
2506 : {
2507 113055 : push_indice( hBstr, IND_SHB_FRAMEGAIN, idxFrameGain, NUM_BITS_SHB_FRAMEGAIN );
2508 : }
2509 : }
2510 :
2511 116997 : if ( st->rf_mode )
2512 : {
2513 0 : *rf_gainFrame_ind = idxFrameGain;
2514 : }
2515 : }
2516 :
2517 124238 : if ( st->rf_mode )
2518 : {
2519 : /*Currently intended for SWB only. Modify for WB is needed later!*/
2520 0 : if ( st->hRF->rf_frame_type == RF_NELP )
2521 : {
2522 0 : *rf_gainFrame_ind = idxFrameGain; /* NELP Frame uses full 5 bits */
2523 : }
2524 : else /*RF_ALLPRED, RF_GENPRED, RF_NOPRED modes*/
2525 : {
2526 0 : if ( *GainFrame <= 1.25 ) /* [0 to 1.25] range --> 0.5*/
2527 : {
2528 0 : *rf_gainFrame_ind = 0;
2529 : }
2530 0 : else if ( *GainFrame <= 3 ) /* (1.25 to 3] --> 2 */
2531 : {
2532 0 : *rf_gainFrame_ind = 1;
2533 : }
2534 0 : else if ( *GainFrame <= 6 ) /* (3 to 6] --> 4 */
2535 : {
2536 0 : *rf_gainFrame_ind = 2;
2537 : }
2538 : else /* (6 to Inf) --> 8 */
2539 : {
2540 0 : *rf_gainFrame_ind = 3;
2541 : }
2542 : }
2543 : }
2544 :
2545 124238 : *GainFrame = Q_GainFrame;
2546 :
2547 124238 : return;
2548 : }
2549 :
2550 :
2551 : /*-------------------------------------------------------------------*
2552 : * determine_gain_weights()
2553 : *
2554 : * Determine weights for gain quantization
2555 : *-------------------------------------------------------------------*/
2556 :
2557 7241 : 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 14482 : for ( j = 0; j < dims; j++ )
2566 : {
2567 7241 : if ( gain[j] > 1e-6 )
2568 : {
2569 7241 : weights[j] = (float) ( pow( fabs( gain[j] ), -0.9f ) );
2570 : }
2571 : else
2572 : {
2573 0 : weights[j] = 501.187233628f;
2574 : }
2575 : }
2576 :
2577 7241 : return;
2578 : }
2579 :
2580 : /*-------------------------------------------------------------------*
2581 : * singlevectortest_gain()
2582 : *
2583 : * VQ for coding superhigh band gain
2584 : *-------------------------------------------------------------------*/
2585 :
2586 7241 : 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 7241 : interNum = 4;
2601 :
2602 7241 : return_M_Least( inp, dimen, codebook, cb_size, weight, interNum, least );
2603 :
2604 7241 : meanU = sum_f( inp, dimen );
2605 7241 : mvr2r( codebook + dimen * least[0], recon, dimen );
2606 :
2607 7241 : index[0] = least[0];
2608 7241 : flag = 0;
2609 36205 : for ( k = 0; k < interNum; k++ )
2610 : {
2611 28964 : if ( flag == 0 )
2612 : {
2613 7972 : meanQ = sum_f( codebook + dimen * least[k], dimen );
2614 :
2615 7972 : if ( meanQ <= 1.1 * meanU )
2616 : {
2617 7217 : flag = 1;
2618 7217 : mvr2r( codebook + dimen * least[k], recon, dimen );
2619 7217 : index[0] = least[k];
2620 : }
2621 : }
2622 : }
2623 :
2624 7241 : return;
2625 : }
2626 :
2627 : /*-------------------------------------------------------------------*
2628 : * return_M_Least()
2629 : *
2630 : *
2631 : *-------------------------------------------------------------------*/
2632 :
2633 7241 : 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 7241 : mindist = QUANT_DIST_INIT;
2647 352729 : for ( i = 0; i < num_grp; i++ )
2648 : {
2649 345488 : distance[i] = 0;
2650 690976 : for ( k = 0; k < n_cols; k++ )
2651 : {
2652 345488 : tmp = inp[k] - codebook[n_cols * i + k];
2653 345488 : distance[i] += weight[k] * tmp * tmp;
2654 : }
2655 :
2656 345488 : if ( distance[i] < mindist )
2657 : {
2658 149075 : mindist = distance[i];
2659 149075 : least[0] = i;
2660 : }
2661 : }
2662 :
2663 7241 : distance[least[0]] = QUANT_DIST_INIT;
2664 :
2665 28964 : for ( k = 1; k < interNum; k++ )
2666 : {
2667 21723 : mindist = QUANT_DIST_INIT;
2668 1058187 : for ( i = 0; i < num_grp; i++ )
2669 : {
2670 1036464 : if ( distance[i] < mindist )
2671 : {
2672 420286 : mindist = distance[i];
2673 420286 : least[k] = i;
2674 : }
2675 : }
2676 :
2677 21723 : distance[least[k]] = QUANT_DIST_INIT;
2678 : }
2679 :
2680 7241 : return;
2681 : }
2682 :
2683 : /*-------------------------------------------------------------------*
2684 : * fb_tbe_reset_enc()
2685 : *
2686 : * Reset the extra parameters needed for FB TBE encoding
2687 : *-------------------------------------------------------------------*/
2688 :
2689 161073 : void fb_tbe_reset_enc(
2690 : float elliptic_bpf_2_48k_mem[][4],
2691 : float *prev_fb_energy )
2692 : {
2693 161073 : set_f( elliptic_bpf_2_48k_mem[0], 0.0f, 4 );
2694 161073 : set_f( elliptic_bpf_2_48k_mem[1], 0.0f, 4 );
2695 161073 : set_f( elliptic_bpf_2_48k_mem[2], 0.0f, 4 );
2696 161073 : set_f( elliptic_bpf_2_48k_mem[3], 0.0f, 4 );
2697 161073 : *prev_fb_energy = 0.0f;
2698 :
2699 161073 : 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 43877 : 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 43877 : TD_BWE_ENC_HANDLE hBWE_TD = st->hBWE_TD;
2721 :
2722 43877 : 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 4976 : 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 38901 : elliptic_bpf_48k_generic( new_input, tmp_vec, hBWE_TD->elliptic_bpf_2_48k_mem, full_band_bpf_2 );
2730 : }
2731 :
2732 43877 : if ( st->element_mode > IVAS_CPE_DFT || st->element_mode == IVAS_SCE )
2733 : {
2734 38901 : if ( st->L_frame == L_FRAME )
2735 : {
2736 8745 : Sample_Delay_HP = NS2SA( 48000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS ) - L_FRAME48k / 2;
2737 : }
2738 : else
2739 : {
2740 30156 : Sample_Delay_HP = NS2SA( 48000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS ) - L_FRAME48k / 2;
2741 : }
2742 : }
2743 : else
2744 : {
2745 4976 : Sample_Delay_HP = NS2SA( 48000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) - L_FRAME48k / 2;
2746 : }
2747 :
2748 43877 : if ( st->last_extl != FB_TBE )
2749 : {
2750 2732 : set_f( hBWE_TD->old_input_fhb, 0.0f, Sample_Delay_HP );
2751 2732 : set_f( tmp_vec, 0.0f, 320 );
2752 : }
2753 :
2754 43877 : mvr2r( hBWE_TD->old_input_fhb, input_fhb, Sample_Delay_HP );
2755 43877 : mvr2r( tmp_vec, input_fhb + Sample_Delay_HP, L_FRAME48k - Sample_Delay_HP );
2756 43877 : 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 43877 : temp2 = sum2_f( input_fhb, L_FRAME48k / 2 ) + hBWE_TD->prev_fb_energy;
2760 43877 : hBWE_TD->prev_fb_energy = sum2_f( input_fhb + L_FRAME48k / 2, L_FRAME48k / 2 );
2761 43877 : fb_exc_energy = sum2_f( fb_exc, L_FRAME16k ) + EPSILON;
2762 43877 : ratio = (float) sqrt( temp2 / fb_exc_energy );
2763 43877 : if ( ratio < 1.0f )
2764 : {
2765 1 : ratio = 1.0f;
2766 : }
2767 43877 : idxGain = (int16_t) ( log2_f( (float) ratio ) + 0.5f );
2768 43877 : idxGain = max( 0, min( 15, idxGain ) );
2769 43877 : ratio = (float) ( 1 << idxGain );
2770 :
2771 43877 : if ( st->codec_mode == MODE2 )
2772 : {
2773 0 : hBWE_TD->idxGain = idxGain;
2774 : }
2775 : else
2776 : {
2777 43877 : push_indice( st->hBstr, IND_FB_SLOPE, idxGain, NUM_BITS_FB_FRAMEGAIN_TBE );
2778 : }
2779 :
2780 43877 : return;
2781 : }
2782 :
2783 : /*---------------------------------------------------------------------*
2784 : * tbe_write_bitstream()
2785 : *
2786 : * Write TBE bitstream.
2787 : *---------------------------------------------------------------------*/
2788 :
2789 604 : void tbe_write_bitstream(
2790 : Encoder_State *st )
2791 : {
2792 : int16_t i;
2793 604 : TD_BWE_ENC_HANDLE hBWE_TD = st->hBWE_TD;
2794 604 : BSTR_ENC_HANDLE hBstr = st->hBstr;
2795 :
2796 604 : if ( ( st->rf_mode || st->total_brate == ACELP_9k60 ) && st->bwidth == WB )
2797 : {
2798 : /* WB LSF */
2799 0 : push_next_indice( hBstr, hBWE_TD->lsf_WB, NUM_BITS_LBR_WB_LSF );
2800 :
2801 : /* WB frame */
2802 0 : push_next_indice( hBstr, hBWE_TD->gFrame_WB, NUM_BITS_SHB_FrameGain_LBR_WB );
2803 : }
2804 604 : else if ( st->total_brate >= ACELP_9k60 && st->total_brate <= ACELP_32k && ( st->bwidth == SWB || st->bwidth == FB ) )
2805 : {
2806 : /* LSF coefficients */
2807 :
2808 604 : if ( st->rf_mode || st->total_brate == ACELP_9k60 )
2809 : {
2810 0 : push_next_indice( hBstr, hBWE_TD->lsf_idx[0], 8 );
2811 : }
2812 : else
2813 : {
2814 3624 : for ( i = 0; i < NUM_Q_LSF; i++ )
2815 : {
2816 3020 : push_next_indice( hBstr, hBWE_TD->lsf_idx[i], lsf_q_num_bits[i] );
2817 : }
2818 :
2819 : /* LSF mirror points */
2820 604 : push_next_indice( hBstr, hBWE_TD->m_idx, MIRROR_POINT_BITS );
2821 :
2822 : /* LSF grid points */
2823 604 : push_next_indice( hBstr, hBWE_TD->grid_idx, NUM_LSF_GRID_BITS );
2824 : }
2825 :
2826 : /* Gain shape */
2827 604 : push_next_indice( hBstr, hBWE_TD->idxSubGains, NUM_BITS_SHB_SUBGAINS );
2828 :
2829 : /* frame gain */
2830 604 : push_next_indice( hBstr, hBWE_TD->idxFrameGain, NUM_BITS_SHB_FRAMEGAIN );
2831 :
2832 604 : if ( st->total_brate >= ACELP_24k40 )
2833 : {
2834 : /* sub frame energy*/
2835 604 : push_next_indice( hBstr, hBWE_TD->idx_shb_fr_gain, NUM_BITS_SHB_ENER_SF );
2836 :
2837 : /* gain shapes residual */
2838 3624 : for ( i = 0; i < NB_SUBFR16k; i++ )
2839 : {
2840 3020 : push_next_indice( hBstr, hBWE_TD->idx_res_gs[i], NUM_BITS_SHB_RES_GS );
2841 : }
2842 :
2843 : /* voicing factor */
2844 604 : push_next_indice( hBstr, hBWE_TD->idx_mixFac, NUM_BITS_SHB_VF );
2845 : }
2846 :
2847 604 : if ( st->tec_tfa == 1 )
2848 : {
2849 604 : push_next_indice( hBstr, st->tec_flag, BITS_TEC );
2850 604 : push_next_indice( hBstr, st->tfa_flag, BITS_TFA );
2851 : }
2852 : }
2853 :
2854 604 : if ( st->bwidth == FB )
2855 : {
2856 0 : push_next_indice( hBstr, hBWE_TD->idxGain, 4 );
2857 : }
2858 :
2859 604 : return;
2860 : }
2861 :
2862 : /*---------------------------------------------------------------------*
2863 : * TBEreset_enc()
2864 : *
2865 : *
2866 : *---------------------------------------------------------------------*/
2867 :
2868 241443 : 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 241443 : if ( last_core != ACELP_CORE )
2875 : {
2876 239926 : set_f( hBWE_TD->old_bwe_exc, 0.0f, PIT16k_MAX * 2 );
2877 239926 : hBWE_TD->bwe_non_lin_prev_scale = 0.f;
2878 : }
2879 :
2880 241443 : if ( bwidth == WB )
2881 : {
2882 10291 : wb_tbe_extras_reset( hBWE_TD->mem_genSHBexc_filt_down_wb2, hBWE_TD->mem_genSHBexc_filt_down_wb3 );
2883 :
2884 10291 : set_f( hBWE_TD->mem_genSHBexc_filt_down_shb, 0, 7 );
2885 10291 : set_f( hBWE_TD->state_lpc_syn, 0, 10 );
2886 10291 : set_f( hBWE_TD->state_syn_shbexc, 0, L_SHB_LAHEAD / 4 );
2887 10291 : set_f( hBWE_TD->syn_overlap, 0, L_SHB_LAHEAD );
2888 10291 : set_f( hBWE_TD->mem_csfilt, 0, 2 );
2889 : }
2890 231152 : else if ( bwidth == SWB || bwidth == FB )
2891 : {
2892 231152 : set_f( hBWE_TD->state_ana_filt_shb, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
2893 :
2894 231152 : 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 231152 : if ( bwidth == FB )
2897 : {
2898 160064 : set_f( hBWE_TD->fb_state_lpc_syn, 0, LPC_SHB_ORDER );
2899 160064 : hBWE_TD->fb_tbe_demph = 0;
2900 160064 : fb_tbe_reset_enc( hBWE_TD->elliptic_bpf_2_48k_mem, &hBWE_TD->prev_fb_energy );
2901 : }
2902 : }
2903 :
2904 241443 : return;
2905 : }
|