Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 :
6 : #include <stdlib.h>
7 : #include "options.h"
8 : #include "cnst.h"
9 : #include "rom_com_fx.h"
10 : #include "rom_com.h"
11 : #include "prot_fx.h" /* Function prototypes */
12 : #include "prot_fx_enc.h" /* Function prototypes */
13 : #include "stl.h"
14 : #include "ivas_prot_fx.h"
15 :
16 :
17 : /*-----------------------------------------------------------------*
18 : * Local constants
19 : *-----------------------------------------------------------------*/
20 :
21 : #define ENVSHBRES_ACORR_MIN 40 /* minimum lag for calculating autocorrelation function on SHB residual TD envelope */
22 : #define ENVSHBRES_ACORR_MAX 80 /* maximum lag for calculating autocorrelation function on SHB residual TD envelope */
23 :
24 : /*-----------------------------------------------------------------*
25 : * Local functions
26 : *-----------------------------------------------------------------*/
27 : static void return_M_Least_fx_GainFrame( const Word32 *inp,
28 : const Word32 *codebook,
29 : const Word16 num_grp,
30 : const Word16 interNum,
31 : Word16 *least );
32 :
33 :
34 : static void singlevectortest_gain_fx( const Word32 *inp, const Word16 dimen, const Word16 cb_size, Word16 *index, Word32 *recon, const Word32 *codebook );
35 :
36 :
37 : static void determine_gain_weights_fx( const Word32 *gain, Word16 *weights, const Word16 dims );
38 :
39 : static void QuantizeSHBsubgains_fx( Encoder_State *st_fx, Word16 *subgains, const Word16 extl );
40 :
41 : static void QuantizeSHBsubgains_ivas_fx( Encoder_State *st_fx, Word16 subgains[], const Word16 extl );
42 :
43 : static void QuantizeSHBframegain_fx( Encoder_State *st_fx, Word32 *GainFrame, const Word16 extl, Word32 extl_brate, Word16 *rf_gainFrame_ind );
44 :
45 : static void QuantizeSHBframegain_ivas_fx( Encoder_State *st_fx, Word32 *GainFrame, const Word16 extl, Word32 extl_brate, Word16 *rf_gainFrame_ind, const Word16 flag_conservative );
46 :
47 : static Word16 closest_centroid_fx( const Word16 *data, const Word16 *weights, const Word16 *quantizer, const Word16 centroids, const Word16 length );
48 :
49 : static Word16 closest_centroid_lc_fx( const Word16 *data, const Word16 *quantizer, const Word16 centroids );
50 :
51 : static void EstimateSHBFrameGain_fx( const Word16 length,
52 : const Word16 *oriSHB,
53 : const Word16 Q_oriSHB,
54 : const Word16 *synSHB,
55 : const Word16 Q_synSHB,
56 : Word16 *subgain,
57 : Word32 *GainFrame,
58 : const Word16 *win_shb,
59 : const Word16 *subwin_shb,
60 : const Word16 n_subfr_saturation );
61 :
62 : static void EstimateSHBFrameGain_ivas_fx( const Word16 length,
63 : const Word16 *oriSHB,
64 : const Word16 Q_oriSHB,
65 : const Word16 *synSHB,
66 : const Word16 Q_synSHB,
67 : Word16 *subgain,
68 : Word32 *GainFrame,
69 : const Word16 *win_shb,
70 : const Word16 *subwin_shb,
71 : const Word16 n_subfr_saturation );
72 :
73 : static void EstimateSHBGainShape_fx( const Word16 length,
74 : const Word16 *oriSHB,
75 : const Word16 Q_oriSHB,
76 : const Word16 *synSHB,
77 : const Word16 Q_synSHB,
78 : Word16 *subgain,
79 : const Word16 *subwin,
80 : Word16 *n_subfr_saturation );
81 :
82 :
83 : static Word32 pow_off_pk_fx( Word16 a[], Word16 len, Word16 step );
84 :
85 : static Word32 pow_off_pk_corrected_fx( Word16 a[], Word16 len, Word16 step );
86 :
87 : static void find_max_mem_enc( Encoder_State *st_fx, Word16 *n_mem, Word16 *n_mem2 );
88 : static void rescale_genSHB_mem_enc( Encoder_State *st_fx, Word16 sf );
89 : static void find_max_mem_wb_enc( Encoder_State *st_fx, Word16 *n_mem );
90 : static void rescale_genWB_mem_enc( Encoder_State *st_fx, Word16 sf );
91 :
92 : static void Quant_lower_LSF_fx( const Word16 lsf[],
93 : Word16 lsf_q[],
94 : Word16 lsf_idx[] );
95 :
96 : static Word16 Quant_mirror_point_fx( const Word16 lsf[], const Word16 lsf_q[], Word16 *m );
97 : static Word16 Find_LSF_grid_fx( const Word16 lsf[], Word16 lsf_q[], const Word16 m );
98 :
99 : static void Quant_BWE_LSF_fx( Encoder_State *st_fx, const Word16 lsp_shb[], Word16 Q_lsfs[] );
100 :
101 : static void Quant_BWE_LSF_ivas_fx( BSTR_ENC_HANDLE hBstr, TD_BWE_ENC_HANDLE hBWE_TD, const Word16 codec_mode, const Word16 lsf_shb_fx[], Word16 Q_lsfs_fx[], const Word32 extl_brate );
102 :
103 : static void Quant_shb_ener_sf_fx( Encoder_State *st_fx, Word32 *shb_ener_sf_fx_32, Word16 Q_shb );
104 :
105 : static void Quant_shb_ener_sf_ivas_fx( Encoder_State *st_fx, Word32 *shb_ener_sf_Q31, Word16 Q_ener );
106 :
107 : static void Quant_shb_res_gshape_fx( Encoder_State *st_fx, Word16 *shb_res_gshape_fx );
108 :
109 : static void Quant_shb_res_gshape_ivas_fx( Encoder_State *st, Word16 shb_res_gshape_fx[] );
110 :
111 : static void gainFrSmooth_En_fx( Encoder_State *st_fx,
112 : Word16 *shb_frame_fx,
113 : const Word16 *lpc_shb_fx,
114 : const Word16 *lsp_shb_fx,
115 : Word16 *MA_lsp_shb_spacing,
116 : Word16 *frGainAttenuate,
117 : Word16 *frGainSmoothEn );
118 :
119 : static void gainFrSmooth_En_ivas_fx( Encoder_State *st_fx,
120 : Word16 *shb_frame_fx /* Q_in */,
121 : const Word16 *lpc_shb_fx /* Q12 */,
122 : const Word16 *lsp_shb_fx /* Q15 */,
123 : Word16 *MA_lsp_shb_spacing /* Q15 */,
124 : Word16 *frGainAttenuate /* Q0 */,
125 : Word16 *frGainSmoothEn /* Q0 */ );
126 :
127 :
128 : /*-------------------------------------------------------------------*
129 : * find_max_mem_enc()
130 : *
131 : * Find norm and max in TBE memories and past buffers
132 : *-------------------------------------------------------------------*/
133 116459 : void find_max_mem_enc(
134 : Encoder_State *st_fx,
135 : Word16 *n_mem,
136 : Word16 *n_mem2 )
137 : {
138 : Word16 i;
139 : Word16 n_mem_32;
140 116459 : Word16 max = 0;
141 116459 : move16();
142 116459 : Word32 Lmax = 0;
143 116459 : move32();
144 116459 : Word16 tempQ15, max2 = 0;
145 116459 : move16();
146 116459 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
147 :
148 : /* old BWE exc max */
149 1513967 : FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
150 : {
151 1397508 : tempQ15 = abs_s( hBWE_TD->old_bwe_exc_extended_fx[i] );
152 1397508 : max = s_max( max, tempQ15 );
153 : }
154 :
155 : /* decimate all-pass steep memory */
156 931672 : FOR( i = 0; i < ( 2 * ALLPASSSECTIONS_STEEP + 1 ); i++ )
157 : {
158 815213 : tempQ15 = abs_s( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] );
159 815213 : max = s_max( max, tempQ15 );
160 : }
161 :
162 : /* -- keep norm of state_lpc_syn_fx, state_syn_shbexc_fx,
163 : and mem_stp_swb_fx separately for 24.4 and 32kbps ----*/
164 : /* findMaxMem2() inside tbe com */
165 1281049 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
166 : {
167 1164590 : tempQ15 = abs_s( hBWE_TD->state_lpc_syn_fx[i] );
168 1164590 : max2 = s_max( max2, tempQ15 );
169 : }
170 :
171 : /* findMaxMem2() inside tbe com */
172 2445639 : FOR( i = 0; i < L_SHB_LAHEAD; i++ )
173 : {
174 2329180 : tempQ15 = abs_s( hBWE_TD->state_syn_shbexc_fx[i] );
175 2329180 : max2 = s_max( max2, tempQ15 );
176 : }
177 :
178 : /* findMaxMem2() inside tbe com */
179 1281049 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
180 : {
181 1164590 : tempQ15 = abs_s( hBWE_TD->mem_stp_swb_fx[i] );
182 1164590 : max2 = s_max( max2, tempQ15 );
183 : }
184 :
185 : /* for total_brate > 16.4kbps, use n_mem2; else account for the max2 for n_mem calculation */
186 116459 : *n_mem2 = norm_s( max2 );
187 116459 : move16();
188 116459 : if ( max2 == 0 )
189 : {
190 95595 : *n_mem2 = 15;
191 95595 : move16();
192 : }
193 116459 : IF( LT_32( st_fx->total_brate, ACELP_24k40 ) )
194 : {
195 90371 : max = s_max( max, max2 );
196 : }
197 :
198 : /* de-emph and pre-emph memory */
199 116459 : tempQ15 = abs_s( hBWE_TD->tbe_demph_fx );
200 116459 : max = s_max( max, tempQ15 );
201 :
202 116459 : tempQ15 = abs_s( hBWE_TD->tbe_premph_fx );
203 116459 : max = s_max( max, tempQ15 );
204 :
205 116459 : IF( EQ_16( st_fx->extl, FB_TBE ) )
206 : {
207 480172 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
208 : {
209 436520 : tempQ15 = abs_s( hBWE_TD->fb_state_lpc_syn_fx[i] );
210 436520 : max = s_max( max, tempQ15 );
211 : }
212 : /* FB de-emph memory */
213 43652 : tempQ15 = abs_s( hBWE_TD->fb_tbe_demph_fx );
214 43652 : max = s_max( max, tempQ15 );
215 : }
216 : /* estimate the norm for 16-bit memories */
217 116459 : *n_mem = norm_s( max );
218 116459 : move16();
219 116459 : if ( max == 0 )
220 : {
221 552 : *n_mem = 15;
222 552 : move16();
223 : }
224 :
225 : /* estimate the norm for 32-bit memories */
226 116459 : Lmax = L_abs( hBWE_TD->mem_csfilt_fx[0] ); /* only element [0] is used in env. shaping */
227 :
228 116459 : n_mem_32 = norm_l( Lmax );
229 116459 : if ( Lmax == 0 )
230 : {
231 37586 : n_mem_32 = 31;
232 37586 : move16();
233 : }
234 :
235 116459 : tempQ15 = sub( s_min( *n_mem, n_mem_32 ), 1 );
236 116459 : *n_mem = s_max( tempQ15, 0 );
237 116459 : move16();
238 116459 : }
239 :
240 :
241 : /*-------------------------------------------------------------------*
242 : * rescale_genSHB_mem_enc()
243 : *
244 : * Rescale genSHB memories
245 : *-------------------------------------------------------------------*/
246 36619 : void rescale_genSHB_mem_enc( Encoder_State *st_fx, Word16 sf )
247 : {
248 : Word16 i;
249 36619 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
250 :
251 476047 : FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
252 : {
253 439428 : hBWE_TD->old_bwe_exc_extended_fx[i] = shl( hBWE_TD->old_bwe_exc_extended_fx[i], sf );
254 439428 : move16();
255 : }
256 :
257 292952 : FOR( i = 0; i < 7; i++ )
258 : {
259 256333 : hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i], sf );
260 256333 : move16();
261 : }
262 :
263 : /* -- Apply memory scaling for 13.2 and 16.4k bps using sf ----*/
264 36619 : IF( LT_32( st_fx->total_brate, ACELP_24k40 ) )
265 : {
266 325523 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
267 : {
268 295930 : hBWE_TD->state_lpc_syn_fx[i] = shl( hBWE_TD->state_lpc_syn_fx[i], sf );
269 295930 : move16();
270 : }
271 :
272 621453 : FOR( i = 0; i < L_SHB_LAHEAD; i++ )
273 : {
274 591860 : hBWE_TD->state_syn_shbexc_fx[i] = shl( hBWE_TD->state_syn_shbexc_fx[i], sf );
275 591860 : move16();
276 : }
277 : }
278 :
279 36619 : hBWE_TD->mem_csfilt_fx[0] = L_shl( hBWE_TD->mem_csfilt_fx[0], sf );
280 36619 : move32();
281 :
282 36619 : hBWE_TD->tbe_demph_fx = shl_r( hBWE_TD->tbe_demph_fx, sf );
283 36619 : move16();
284 36619 : hBWE_TD->tbe_premph_fx = shl_r( hBWE_TD->tbe_premph_fx, sf );
285 36619 : move16();
286 36619 : }
287 :
288 7343 : void find_max_mem_wb_enc( Encoder_State *st_fx, Word16 *n_mem )
289 : {
290 : Word16 i;
291 : Word16 n_mem_32;
292 7343 : Word16 max_16 = 0;
293 7343 : Word32 Lmax = 0;
294 7343 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
295 7343 : move16();
296 7343 : move32();
297 :
298 95459 : FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
299 88116 : max_16 = s_max( max_16, abs_s( hBWE_TD->old_bwe_exc_extended_fx[i] ) );
300 :
301 58744 : FOR( i = 0; i < 7; i++ )
302 : {
303 51401 : IF( GT_16( abs_s( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] ), max_16 ) )
304 5533 : max_16 = abs_s( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] );
305 : }
306 :
307 58744 : FOR( i = 0; i < 7; i++ )
308 : {
309 51401 : IF( GT_16( abs_s( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx[i] ), max_16 ) )
310 4473 : max_16 = abs_s( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx[i] );
311 : }
312 :
313 58744 : FOR( i = 0; i < 7; i++ )
314 : {
315 51401 : IF( GT_16( abs_s( hBWE_TD->mem_genSHBexc_filt_down_wb3_fx[i] ), max_16 ) )
316 1347 : max_16 = abs_s( hBWE_TD->mem_genSHBexc_filt_down_wb3_fx[i] );
317 : }
318 :
319 80773 : FOR( i = 0; i < 10; i++ )
320 : {
321 73430 : IF( GT_16( abs_s( hBWE_TD->state_lpc_syn_fx[i] ), max_16 ) )
322 2057 : max_16 = abs_s( hBWE_TD->state_lpc_syn_fx[i] );
323 : }
324 :
325 44058 : FOR( i = 0; i < 5; i++ )
326 : {
327 36715 : IF( GT_16( abs_s( hBWE_TD->state_syn_shbexc_fx[i] ), max_16 ) )
328 0 : max_16 = abs_s( hBWE_TD->state_syn_shbexc_fx[i] );
329 : }
330 :
331 7343 : IF( max_16 == 0 )
332 : {
333 12 : *n_mem = 15;
334 12 : move16();
335 : }
336 : ELSE
337 : {
338 7331 : *n_mem = norm_s( max_16 );
339 7331 : move16();
340 : }
341 :
342 22029 : FOR( i = 0; i < 2; i++ )
343 : {
344 14686 : IF( GT_32( L_abs( hBWE_TD->mem_csfilt_fx[i] ), Lmax ) )
345 7011 : Lmax = L_abs( hBWE_TD->mem_csfilt_fx[i] );
346 : }
347 :
348 :
349 7343 : IF( Lmax == 0 )
350 : {
351 332 : n_mem_32 = 31;
352 332 : move16();
353 : }
354 : ELSE
355 : {
356 7011 : n_mem_32 = norm_l( Lmax );
357 : }
358 :
359 7343 : *n_mem = sub( s_min( *n_mem, n_mem_32 ), 1 );
360 7343 : move16();
361 7343 : *n_mem = s_max( *n_mem, 0 );
362 7343 : move16();
363 7343 : }
364 :
365 7343 : void rescale_genWB_mem_enc( Encoder_State *st_fx, Word16 sf )
366 : {
367 : Word16 i;
368 7343 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
369 :
370 95459 : FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
371 : {
372 88116 : hBWE_TD->old_bwe_exc_extended_fx[i] = shl( hBWE_TD->old_bwe_exc_extended_fx[i], sf );
373 88116 : move16();
374 : }
375 :
376 80773 : FOR( i = 0; i < 10; i++ )
377 : {
378 73430 : hBWE_TD->state_lpc_syn_fx[i] = shl( hBWE_TD->state_lpc_syn_fx[i], sf );
379 73430 : move16();
380 : }
381 :
382 44058 : FOR( i = 0; i < 5; i++ )
383 : {
384 36715 : hBWE_TD->state_syn_shbexc_fx[i] = shl( hBWE_TD->state_syn_shbexc_fx[i], sf );
385 36715 : move16();
386 : }
387 :
388 58744 : FOR( i = 0; i < 7; i++ )
389 : {
390 51401 : hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i], sf );
391 51401 : move16();
392 : }
393 :
394 58744 : FOR( i = 0; i < 7; i++ )
395 : {
396 51401 : hBWE_TD->mem_genSHBexc_filt_down_wb2_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx[i], sf );
397 51401 : move16();
398 : }
399 :
400 58744 : FOR( i = 0; i < 7; i++ )
401 : {
402 51401 : hBWE_TD->mem_genSHBexc_filt_down_wb3_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_wb3_fx[i], sf );
403 51401 : move16();
404 : }
405 :
406 22029 : FOR( i = 0; i < 2; i++ )
407 : {
408 14686 : hBWE_TD->mem_csfilt_fx[i] = L_shl( hBWE_TD->mem_csfilt_fx[i], sf );
409 14686 : move32();
410 : }
411 7343 : }
412 :
413 :
414 : /*-------------------------------------------------------------------*
415 : * InitSWBencBuffer()
416 : *
417 : * Initialize SWB buffers
418 : *-------------------------------------------------------------------*/
419 3 : void InitSWBencBuffer_fx(
420 : Encoder_State *st_fx /* i/o: SHB encoder structure */
421 : )
422 : {
423 : Word16 i;
424 3 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
425 :
426 3 : set16_fx( hBWE_TD->old_bwe_exc_fx, 0, ( PIT16k_MAX * 2 ) );
427 3 : hBWE_TD->bwe_seed[0] = 23;
428 3 : move16();
429 3 : hBWE_TD->bwe_seed[1] = 59;
430 3 : move16();
431 3 : set16_fx( hBWE_TD->old_bwe_exc_extended_fx, 0, NL_BUFF_OFFSET );
432 3 : hBWE_TD->bwe_non_lin_prev_scale_fx = L_deposit_l( 0 );
433 3 : move32();
434 :
435 3 : set16_fx( hBWE_TD->state_ana_filt_shb_fx, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
436 3 : set32_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx[0], 0, 4 );
437 3 : set32_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx[1], 0, 4 );
438 3 : set32_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx[2], 0, 4 );
439 3 : set32_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx[3], 0, 4 );
440 3 : hBWE_TD->prev_fb_energy_fx = 0;
441 3 : move16();
442 :
443 3 : set16_fx( hBWE_TD->old_speech_shb_fx, 0, L_LOOK_16k + L_SUBFR16k );
444 3 : set16_fx( hBWE_TD->old_speech_wb_fx, 0, ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16 );
445 3 : set16_fx( hBWE_TD->old_input_fhb_fx, 0, NS2SA( 48000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) - L_FRAME48k / 2 );
446 :
447 : // IVAS_CODE
448 : // set_f(hBWE_TD->mem_shb_res, 0.0f, MAX_LEN_MA_FILTER);
449 : // set_f(hBWE_TD->old_EnvSHBres, 0.0f, L_FRAME4k);
450 : // hBWE_TD->old_mean_EnvSHBres = 0.0f;
451 : // hBWE_TD->prev_enr_EnvSHBres = 1.0f;
452 : // hBWE_TD->prev_shb_env_tilt = 0.0f;
453 : // hBWE_TD->prev_pow_exc16kWhtnd = 1.0f;
454 : // hBWE_TD->prev_mix_factor = 1.0f;
455 : // hBWE_TD->prev_Env_error = 0.0f;
456 3 : set16_fx( hBWE_TD->prev_lsp_shb_fx, 0, 10 );
457 :
458 3 : hBWE_TD->cldfbHBLT = 8192 /*1.0f Q13*/;
459 3 : move16();
460 3 : hBWE_TD->prev_gainFr_SHB_fx = 0;
461 3 : move32();
462 3 : set16_fx( hBWE_TD->lsp_shb_slow_interpl_fx, 0, LPC_SHB_ORDER );
463 3 : set16_fx( hBWE_TD->lsp_shb_fast_interpl_fx, 0, LPC_SHB_ORDER );
464 3 : set16_fx( hBWE_TD->shb_inv_filt_mem_fx, 0, LPC_SHB_ORDER );
465 3 : set16_fx( hBWE_TD->lsp_shb_spacing_fx, 3277, 3 );
466 3 : set16_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx_Q, 0, 4 );
467 :
468 3 : hBWE_TD->prev_swb_GainShape_fx = 0;
469 3 : move16();
470 3 : hBWE_TD->prev_frGainAtten = 0;
471 3 : move16();
472 3 : hBWE_TD->prev_wb_GainShape = 0;
473 3 : move16();
474 3 : hBWE_TD->prev_Q_bwe_exc_fb = 51;
475 3 : move16();
476 3 : set16_fx( hBWE_TD->fb_state_lpc_syn_fx, 0, LPC_SHB_ORDER );
477 3 : hBWE_TD->fb_tbe_demph_fx = 0;
478 3 : move16();
479 3 : hBWE_TD->tilt_mem_fx = 0;
480 3 : move16();
481 :
482 3 : hBWE_TD->prev_coder_type = GENERIC;
483 3 : move16();
484 3 : set16_fx( hBWE_TD->prev_lsf_diff_fx, 16384, LPC_SHB_ORDER - 2 );
485 3 : hBWE_TD->prev_tilt_para_fx = 0;
486 3 : move16();
487 3 : set16_fx( hBWE_TD->cur_sub_Aq_fx, 0, M + 1 );
488 :
489 : /* TD BWE post-processing */
490 3 : hBWE_TD->ptr_mem_stp_swb_fx = hBWE_TD->mem_stp_swb_fx + LPC_SHB_ORDER - 1;
491 3 : set16_fx( hBWE_TD->mem_zero_swb_fx, 0, LPC_SHB_ORDER );
492 :
493 33 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
494 : {
495 30 : hBWE_TD->swb_lsp_prev_interp_fx[i] = swb_lsp_prev_interp_init[i];
496 30 : move16();
497 : }
498 :
499 3 : set16_fx( hBWE_TD->dec_2_over_3_mem_fx, 0, 12 );
500 3 : set16_fx( hBWE_TD->dec_2_over_3_mem_lp_fx, 0, 6 );
501 :
502 :
503 3 : hBWE_TD->prev_fb_energy_fx_Q = 0;
504 3 : move16();
505 :
506 :
507 : // TV2TV IVAS_CODE -> To be verify if it has to be moved to hBWE_TD
508 3 : st_fx->prev_Q_bwe_exc = 31;
509 3 : move16();
510 3 : st_fx->prev_Q_bwe_syn = 31;
511 3 : move16();
512 3 : set16_fx( st_fx->prev_lsp_wb_fx, 0, 6 );
513 3 : set16_fx( st_fx->prev_lsp_wb_temp_fx, 0, 6 );
514 3 : set16_fx( st_fx->prev_lpc_wb_fx, 0, LPC_SHB_ORDER_WB );
515 :
516 3 : return;
517 : }
518 :
519 3854 : void InitSWBencBuffer_ivas_fx(
520 : Encoder_State *st_fx /* i/o: SHB encoder structure */
521 : )
522 : {
523 : Word16 i;
524 3854 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
525 :
526 3854 : set16_fx( hBWE_TD->old_bwe_exc_fx, 0, ( PIT16k_MAX * 2 ) );
527 3854 : hBWE_TD->bwe_seed[0] = 23;
528 3854 : move16();
529 3854 : hBWE_TD->bwe_seed[1] = 59;
530 3854 : move16();
531 3854 : set16_fx( hBWE_TD->old_bwe_exc_extended_fx, 0, NL_BUFF_OFFSET );
532 3854 : hBWE_TD->bwe_non_lin_prev_scale_fx = L_deposit_l( 0 );
533 3854 : move32();
534 :
535 3854 : set16_fx( hBWE_TD->state_ana_filt_shb_fx, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
536 3854 : set32_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx[0], 0, 4 );
537 3854 : set32_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx[1], 0, 4 );
538 3854 : set32_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx[2], 0, 4 );
539 3854 : set32_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx[3], 0, 4 );
540 3854 : hBWE_TD->prev_fb_energy_fx = 0;
541 3854 : move16();
542 :
543 3854 : set16_fx( hBWE_TD->old_speech_shb_fx, 0, L_LOOK_16k + L_SUBFR16k );
544 3854 : set16_fx( hBWE_TD->old_speech_wb_fx, 0, ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16 );
545 3854 : set16_fx( hBWE_TD->old_input_fhb_fx, 0, NS2SA( 48000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) - L_FRAME48k / 2 );
546 3854 : hBWE_TD->old_input_fhb_fx_Q = 0;
547 3854 : move16();
548 :
549 3854 : InitSWBencBufferStates_fx( hBWE_TD, NULL );
550 :
551 42394 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
552 : {
553 38540 : hBWE_TD->prev_lsp_shb_fx[i] = lsp_shb_prev_tbl_swb_tbe_enc_fx[i];
554 38540 : move16();
555 : }
556 :
557 3854 : hBWE_TD->cldfbHBLT = 8192; /* 1.0f Q13 */
558 3854 : move16();
559 3854 : hBWE_TD->prev_gainFr_SHB_fx = 0;
560 3854 : move32();
561 3854 : set16_fx( hBWE_TD->lsp_shb_slow_interpl_fx, 0, LPC_SHB_ORDER );
562 3854 : set16_fx( hBWE_TD->lsp_shb_fast_interpl_fx, 0, LPC_SHB_ORDER );
563 3854 : set16_fx( hBWE_TD->shb_inv_filt_mem_fx, 0, LPC_SHB_ORDER );
564 3854 : set16_fx( hBWE_TD->lsp_shb_spacing_fx, 3277 /* 0.1f in Q15 */, 3 );
565 3854 : set16_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx_Q, 0, 4 );
566 :
567 3854 : hBWE_TD->prev_swb_GainShape_fx = 0;
568 3854 : move16();
569 3854 : hBWE_TD->prev_frGainAtten = 0;
570 3854 : move16();
571 3854 : hBWE_TD->prev_wb_GainShape = 0;
572 3854 : move16();
573 3854 : hBWE_TD->prev_Q_bwe_exc_fb = 51;
574 3854 : move16();
575 3854 : set16_fx( hBWE_TD->fb_state_lpc_syn_fx, 0, LPC_SHB_ORDER );
576 3854 : hBWE_TD->fb_tbe_demph_fx = 0;
577 3854 : move16();
578 3854 : hBWE_TD->tilt_mem_fx = 0;
579 3854 : move16();
580 :
581 3854 : hBWE_TD->prev_coder_type = GENERIC;
582 3854 : move16();
583 3854 : set16_fx( hBWE_TD->prev_lsf_diff_fx, 16384 /* 0.5f in Q15 */, LPC_SHB_ORDER - 2 );
584 3854 : hBWE_TD->prev_tilt_para_fx = 0;
585 3854 : move16();
586 3854 : set16_fx( hBWE_TD->cur_sub_Aq_fx, 0, M + 1 );
587 :
588 : /* TD BWE post-processing */
589 3854 : hBWE_TD->ptr_mem_stp_swb_fx = hBWE_TD->mem_stp_swb_fx + LPC_SHB_ORDER - 1;
590 3854 : set16_fx( hBWE_TD->mem_zero_swb_fx, 0, LPC_SHB_ORDER );
591 :
592 42394 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
593 : {
594 38540 : hBWE_TD->swb_lsp_prev_interp_fx[i] = swb_lsp_prev_interp_init[i];
595 38540 : move16();
596 : }
597 :
598 3854 : set16_fx( hBWE_TD->dec_2_over_3_mem_fx, 0, 12 );
599 3854 : set16_fx( hBWE_TD->dec_2_over_3_mem_lp_fx, 0, 6 );
600 :
601 :
602 3854 : hBWE_TD->prev_fb_energy_fx_Q = 0;
603 3854 : move16();
604 :
605 :
606 : // TV2TV IVAS_CODE -> To be verify if it has to be moved to hBWE_TD
607 3854 : st_fx->prev_Q_bwe_exc = 15;
608 3854 : move16();
609 3854 : st_fx->prev_Q_bwe_syn = 15;
610 3854 : move16();
611 3854 : set16_fx( st_fx->prev_lsp_wb_fx, 0, 6 );
612 3854 : set16_fx( st_fx->prev_lsp_wb_temp_fx, 0, 6 );
613 3854 : set16_fx( st_fx->prev_lpc_wb_fx, 0, LPC_SHB_ORDER_WB );
614 :
615 3854 : return;
616 : }
617 :
618 : /*-------------------------------------------------------------------*
619 : * ResetSHBbuffer_Enc()
620 : *
621 : *-------------------------------------------------------------------*/
622 3857 : void ResetSHBbuffer_Enc_fx(
623 : Encoder_State *st_fx /* i/o: SHB encoder structure */
624 : )
625 : {
626 3857 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
627 :
628 : /* states for the filters used in generating SHB excitation from WB excitation*/
629 3857 : set16_fx( hBWE_TD->mem_genSHBexc_filt_down_shb_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
630 3857 : set32_fx( hBWE_TD->mem_csfilt_fx, 0, 2 );
631 :
632 : /* states for the filters used in generating SHB signal from SHB excitation*/
633 3857 : set16_fx( hBWE_TD->state_syn_shbexc_fx, 0, L_SHB_LAHEAD );
634 3857 : set16_fx( hBWE_TD->state_lpc_syn_fx, 0, LPC_SHB_ORDER );
635 :
636 : // IF( EQ_16(st_fx->extl, FB_TBE)) _DIFF_FLOAT_FIX_
637 : {
638 3857 : set16_fx( hBWE_TD->fb_state_lpc_syn_fx, 0, LPC_SHB_ORDER );
639 3857 : hBWE_TD->fb_tbe_demph_fx = 0;
640 3857 : move16();
641 : }
642 : /* states for the filters used in generating WB signal from WB excitation*/
643 3857 : set16_fx( hBWE_TD->decim_state1_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
644 3857 : set16_fx( hBWE_TD->decim_state2_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
645 3857 : set16_fx( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
646 3857 : set16_fx( hBWE_TD->mem_genSHBexc_filt_down_wb3_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
647 :
648 : /* overlap buffer used to Adjust SHB Frame Gain */
649 3857 : set16_fx( hBWE_TD->mem_stp_swb_fx, 0, LPC_SHB_ORDER );
650 3857 : hBWE_TD->gain_prec_swb_fx = 16384; /*Q14=1 */
651 3857 : move16();
652 3857 : set16_fx( hBWE_TD->syn_overlap_fx, 0, L_SHB_LAHEAD );
653 3857 : hBWE_TD->tbe_demph_fx = 0;
654 3857 : move16();
655 3857 : hBWE_TD->tbe_premph_fx = 0;
656 3857 : move16();
657 :
658 :
659 3857 : return;
660 : }
661 :
662 :
663 : /*==========================================================================*/
664 : /* FUNCTION : void wb_tbe_enc_fx() */
665 : /*--------------------------------------------------------------------------*/
666 : /* PURPOSE : WB TBE encoder, 6 - 8 kHz band encoding module */
667 : /*--------------------------------------------------------------------------*/
668 : /* INPUT ARGUMENTS : */
669 : /* Word16 coder_type i : coding type */
670 : /* Word16 *new_speech i : original input signal Q0 */
671 : /* Word32 *bwe_exc_extended i : bandwidth extended exciatation 2*Q_new*/
672 : /* Word16 voice_factors[] i : voicing factors Q15 */
673 : /* Word16 pitch_buf[] i : pitch for each subframe Q6 */
674 : /*--------------------------------------------------------------------------*/
675 : /* OUTPUT ARGUMENTS : */
676 : /* Word16 *synth o : WB SHB final synthesis */
677 : /*--------------------------------------------------------------------------*/
678 : /* INPUT/OUTPUT ARGUMENTS : */
679 : /* Encoder_State *st_fx i/o: encoder state structure */
680 : /*--------------------------------------------------------------------------*/
681 : /* RETURN ARGUMENTS : */
682 : /* _ None */
683 : /*--------------------------------------------------------------------------*/
684 : /* */
685 : /*==========================================================================*/
686 :
687 : #define WBTBE_LOOK_LSUFBR_5_OVER_16 ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16
688 : #define WBTBE_LSUBFR_5_OVER_16 L_SUBFR * 5 / 16
689 : #define WBTBE_ANA_ALIGNDELAY -( L_SHB_LAHEAD / 4 + 5 )
690 : #define LFRAME16K_OVER_4 L_FRAME16k / 4
691 : #define WBTBE_LPCWIN_LENGTH ( L_LOOK_12k8 + L_SUBFR + L_FRAME ) * 5 / 16 - 1
692 :
693 0 : void wb_tbe_enc_fx(
694 : Encoder_State *st_fx, /* i/o: encoder state structure */
695 : const Word16 coder_type, /* i : coding type */
696 : const Word16 *hb_speech, /* i : HB target signal (6-8kHz) at 16kHz at Q-1 */
697 : const Word32 *bwe_exc_extended, /* i : bandwidth extended exciatation */
698 : const Word16 Q_new, /* i : input HB speech Q factor */
699 : const Word16 voice_factors[], /* i : voicing factors */
700 : const Word16 pitch_buf[], /* i : pitch for each subframe */
701 : const Word16 voicing_fx[] /* i : OL maximum normalized correlation */
702 : )
703 : {
704 : Word16 i, j;
705 : Word16 hb_old_speech[( L_LOOK_12k8 + L_SUBFR + L_FRAME ) * 5 / 16];
706 : Word16 bwe_exc_extended_16[L_FRAME32k + NL_BUFF_OFFSET];
707 :
708 : Word16 shaped_wb_excitation[( L_FRAME16k + L_SHB_LAHEAD ) / 4];
709 : Word16 exc4kWhtnd[L_FRAME16k / 4];
710 : /*Word16 ana_align_delay = WBTBE_ANA_ALIGNDELAY; */ /* -L_SHB_LAHEAD/4 - 5 */
711 : Word32 GainFrame;
712 : Word16 GainShape[NUM_SHB_SUBFR];
713 : Word16 lpc_wb[LPC_SHB_ORDER_WB + 1];
714 : Word32 lpc_wb_32_fx[LPC_SHB_ORDER_WB + 1];
715 0 : Word16 lsp_wb[LPC_SHB_ORDER_WB], weights_lsp[LPC_SHB_ORDER_WB] = { 32767, 32767 };
716 0 : move16();
717 0 : move16();
718 : Word16 *hb_new_speech, *hb_frame /*, hb_speech[ L_FRAME16k ]*/;
719 : Word16 R_h[LPC_SHB_ORDER_WB + 2], R_l[LPC_SHB_ORDER_WB + 2];
720 : Word16 Q_R;
721 : Word32 LepsP[LPC_SHB_ORDER_WB + 1];
722 :
723 : Word32 prev_pow, curr_pow, Lscale;
724 : /* Word16 scale; */
725 : /*Word16 ramp_flag;*/
726 : Word32 p2m_in, p2m_out;
727 : /*Word16 cnt, max =0;*/
728 : Word16 n_mem, Q_bwe_exc, Q_bwe_exc_ext, exp_out; /* Q_hb_frame; */
729 : Word32 L_tmp, Lmax;
730 : Word16 tmp, exp, Q_out, sc;
731 0 : Word16 Q_ns = -1;
732 0 : move16();
733 :
734 : Word16 pitBufAvg_fx, voicingBufAvg_fx;
735 : Word16 vf_modified_fx[NB_SUBFR16k];
736 : Word16 temp_wb_fac_fx, feedback_fx;
737 : Word16 lsp_spacing_fx;
738 : Word16 lsp_wb_temp_fx[LPC_SHB_ORDER_WB], lpc_wb_temp_fx[LPC_SHB_ORDER_WB + 1];
739 : Word32 L_feedback;
740 : Word16 frac, exp1;
741 : Word16 uv_flag;
742 0 : Word16 dummy = 0;
743 0 : move16();
744 : Word16 avg_voice_fac;
745 0 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
746 0 : RF_ENC_HANDLE hRF = st_fx->hRF;
747 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
748 0 : Flag Overflow = 0;
749 0 : move16();
750 : #endif
751 :
752 : /*Word16 att = 32767;*/
753 :
754 0 : hb_new_speech = hb_old_speech + WBTBE_LOOK_LSUFBR_5_OVER_16;
755 0 : hb_frame = hb_old_speech + WBTBE_LSUBFR_5_OVER_16 + WBTBE_ANA_ALIGNDELAY;
756 :
757 0 : Copy( hBWE_TD->old_speech_wb_fx, hb_old_speech, WBTBE_LOOK_LSUFBR_5_OVER_16 );
758 0 : Copy( hb_speech, hb_new_speech, LFRAME16K_OVER_4 );
759 0 : Copy( hb_old_speech + LFRAME16K_OVER_4, hBWE_TD->old_speech_wb_fx, WBTBE_LOOK_LSUFBR_5_OVER_16 );
760 :
761 0 : test();
762 0 : test();
763 0 : test();
764 0 : test();
765 0 : test();
766 0 : test();
767 0 : IF( ( NE_16( st_fx->last_extl, WB_TBE ) && NE_16( st_fx->last_extl, WB_BWE ) ) && ( st_fx->clas == UNVOICED_CLAS || ( LT_16( voicing_fx[0], 16384 ) && LT_16( voicing_fx[1], 16384 ) && LT_16( voicing_fx[2], 16384 ) ) ) && st_fx->igf == 0 )
768 : {
769 : /* In case of unvoiced signals after switching cores, back-propagate the target signal */
770 0 : Copy( hb_speech, hb_old_speech, WBTBE_LOOK_LSUFBR_5_OVER_16 );
771 :
772 0 : i = WBTBE_LOOK_LSUFBR_5_OVER_16;
773 0 : move16();
774 :
775 0 : FOR( j = 0; j < L_SUBFR16k; j = j + 4 )
776 : {
777 0 : L_tmp = L_mult( hb_old_speech[i], ola_win_shb_switch_fold_fx[j] );
778 0 : hb_old_speech[i] = mac_r( L_tmp, hb_speech[j], ola_win_shb_switch_fold_fx[L_SUBFR16k - 4 - j] );
779 0 : move16();
780 0 : i--;
781 : }
782 : }
783 :
784 0 : autocorr_fx( hb_old_speech, LPC_SHB_ORDER_WB + 1, R_h, R_l, &Q_R, WBTBE_LPCWIN_LENGTH, win_lpc_hb_wb_fx, 0, 1 );
785 :
786 0 : E_LPC_lev_dur( R_h, R_l, lpc_wb_temp_fx, LepsP, LPC_SHB_ORDER_WB, NULL );
787 :
788 0 : Copy_Scale_sig( lpc_wb_temp_fx, lpc_wb_temp_fx, LPC_SHB_ORDER_WB + 1, sub( norm_s( lpc_wb_temp_fx[0] ), 2 ) );
789 :
790 : /* convert into lsps and calculate weights */
791 0 : FOR( i = 0; i <= LPC_SHB_ORDER_WB; i++ )
792 : {
793 0 : lpc_wb_32_fx[i] = L_negate( L_shr( L_deposit_h( lpc_wb_temp_fx[i] ), 1 ) );
794 0 : move32();
795 : }
796 :
797 0 : lpc2lsp_fx( &lpc_wb_32_fx[1], lsp_wb_temp_fx, st_fx->prev_lsp_wb_temp_fx, LPC_SHB_ORDER_WB );
798 :
799 0 : FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
800 : {
801 0 : st_fx->prev_lsp_wb_temp_fx[i] = lsp_wb_temp_fx[i];
802 0 : move16();
803 : }
804 :
805 : /* lsp_spacing_fx = 16384; move16(); */
806 0 : lsp_spacing_fx = lsp_wb_temp_fx[0];
807 0 : move16();
808 0 : FOR( i = 1; i < LPC_SHB_ORDER_WB; i++ )
809 : {
810 : /*if ( i == 0 )
811 : {
812 : tmp = lsp_wb_temp_fx[0]; move16();
813 : }
814 : else
815 : {*/
816 0 : tmp = sub( lsp_wb_temp_fx[i], lsp_wb_temp_fx[i - 1] );
817 : /*} */
818 :
819 0 : lsp_spacing_fx = s_min( lsp_spacing_fx, tmp );
820 : }
821 :
822 : /* Spectral smoothing of autocorrelation coefficients */
823 0 : FOR( i = 1; i <= LPC_SHB_ORDER_WB; i++ )
824 : {
825 0 : L_tmp = Mpy_32( R_h[i], R_l[i], wac_h[i - 1], wac_l[i - 1] );
826 0 : L_Extract( L_tmp, &R_h[i], &R_l[i] );
827 : }
828 0 : R_l[0] = s_max( R_l[0], 1 );
829 0 : move16();
830 :
831 0 : test();
832 0 : IF( EQ_16( st_fx->rf_mode, 1 ) || EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
833 : {
834 0 : E_LPC_lev_dur( R_h, R_l, lpc_wb, LepsP, LPC_SHB_ORDER_LBR_WB, NULL );
835 0 : Copy_Scale_sig( lpc_wb, lpc_wb, LPC_SHB_ORDER_LBR_WB + 1, sub( norm_s( lpc_wb[0] ), 2 ) );
836 :
837 : /* Expand bandwidth of the LP coeffs */
838 0 : FOR( i = 0; i <= LPC_SHB_ORDER_LBR_WB; i++ )
839 : {
840 0 : lpc_wb[i] = mult_r( lpc_wb[i], lpc_weights_fx[i] );
841 0 : move16();
842 : }
843 :
844 : /* convert into lsps and calculate weights */
845 0 : FOR( i = 0; i <= LPC_SHB_ORDER_LBR_WB; i++ )
846 : {
847 0 : lpc_wb_32_fx[i] = L_negate( L_shr( L_deposit_h( lpc_wb[i] ), 1 ) );
848 0 : move32(); /*Q27 */
849 : }
850 0 : lpc2lsp_fx( &lpc_wb_32_fx[1], lsp_wb, st_fx->prev_lsp_wb_fx, LPC_SHB_ORDER_LBR_WB );
851 :
852 0 : FOR( i = 0; i < LPC_SHB_ORDER_LBR_WB; i++ )
853 : {
854 0 : st_fx->prev_lsp_wb_fx[i] = lsp_wb[i];
855 0 : move16();
856 : }
857 :
858 0 : lsp_weights_fx( lsp_wb, weights_lsp, LPC_SHB_ORDER_LBR_WB, &Q_out );
859 :
860 : /* Quantization of LSFs */
861 0 : i = closest_centroid_fx( lsp_wb, weights_lsp, lbr_wb_bwe_lsfvq_cbook_2bit_fx, 4, LPC_SHB_ORDER_LBR_WB );
862 0 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
863 : {
864 0 : hBWE_TD->lsf_WB = i;
865 0 : move16();
866 : }
867 : ELSE
868 : {
869 0 : push_indice( st_fx->hBstr, IND_SHB_LSF, i, NUM_BITS_LBR_WB_LSF );
870 : }
871 :
872 0 : Copy( lbr_wb_bwe_lsfvq_cbook_2bit_fx + i * LPC_SHB_ORDER_LBR_WB, lsp_wb, LPC_SHB_ORDER_LBR_WB );
873 :
874 0 : lsp2lpc_fx( &lpc_wb[1], lsp_wb, st_fx->prev_lpc_wb_fx, LPC_SHB_ORDER_LBR_WB );
875 :
876 0 : set16_fx( lpc_wb + LPC_SHB_ORDER_LBR_WB + 1, 0, ( LPC_SHB_ORDER_WB - LPC_SHB_ORDER_LBR_WB ) );
877 0 : FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
878 : {
879 0 : st_fx->prev_lpc_wb_fx[i] = lpc_wb[i + 1];
880 0 : move16();
881 : }
882 :
883 0 : FOR( i = 1; i < LPC_SHB_ORDER_LBR_WB + 1; i++ )
884 : {
885 0 : lpc_wb[i] = negate( lpc_wb[i] );
886 0 : move16();
887 : }
888 0 : lpc_wb[0] = 4096;
889 0 : move16();
890 : }
891 : ELSE /* 13.2kbps */
892 : {
893 0 : E_LPC_lev_dur( R_h, R_l, lpc_wb, LepsP, LPC_SHB_ORDER_WB, NULL );
894 0 : Copy_Scale_sig( lpc_wb, lpc_wb, LPC_SHB_ORDER_WB + 1, sub( norm_s( lpc_wb[0] ), 2 ) );
895 :
896 : /* Expand bandwidth of the LP coeffs */
897 0 : FOR( i = 0; i <= LPC_SHB_ORDER_WB; i++ )
898 : {
899 0 : lpc_wb[i] = mult_r( lpc_wb[i], lpc_weights_fx[i] );
900 0 : move16();
901 : }
902 :
903 : /* convert into lsps and calculate weights */
904 0 : FOR( i = 0; i <= LPC_SHB_ORDER_WB; i++ )
905 : {
906 0 : lpc_wb_32_fx[i] = L_negate( L_shr( L_deposit_h( lpc_wb[i] ), 1 ) );
907 0 : move32(); /*Q27 */
908 : }
909 :
910 0 : lpc2lsp_fx( &lpc_wb_32_fx[1], lsp_wb, st_fx->prev_lsp_wb_fx, LPC_SHB_ORDER_WB );
911 :
912 :
913 0 : FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
914 : {
915 0 : st_fx->prev_lsp_wb_fx[i] = lsp_wb[i];
916 0 : move16();
917 : }
918 :
919 0 : lsp_weights_fx( lsp_wb, weights_lsp, LPC_SHB_ORDER_WB, &Q_out );
920 :
921 : /* Quantization of LSFs */
922 0 : i = closest_centroid_fx( lsp_wb, weights_lsp, wb_bwe_lsfvq_cbook_8bit_fx, 256, LPC_SHB_ORDER_WB ); /*move16(); */
923 :
924 0 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
925 : {
926 0 : hBWE_TD->lsf_WB = i;
927 0 : move16();
928 : }
929 : ELSE
930 : {
931 0 : push_indice( st_fx->hBstr, IND_SHB_LSF, i, NUM_BITS_WB_LSF );
932 : }
933 0 : Copy( wb_bwe_lsfvq_cbook_8bit_fx + i * LPC_SHB_ORDER_WB, lsp_wb, LPC_SHB_ORDER_WB );
934 :
935 0 : lsp2lpc_fx( &lpc_wb[1], lsp_wb, st_fx->prev_lpc_wb_fx, LPC_SHB_ORDER_WB );
936 :
937 :
938 0 : FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
939 : {
940 0 : st_fx->prev_lpc_wb_fx[i] = lpc_wb[i + 1];
941 0 : move16();
942 : }
943 0 : FOR( i = 1; i < LPC_SHB_ORDER_WB + 1; i++ )
944 : {
945 0 : lpc_wb[i] = negate( lpc_wb[i] );
946 0 : move16();
947 : }
948 0 : lpc_wb[0] = 4096;
949 0 : move16();
950 : }
951 :
952 0 : uv_flag = 0;
953 0 : move16();
954 0 : test();
955 0 : if ( EQ_32( st_fx->extl_brate, WB_TBE_1k05 ) && EQ_16( st_fx->coder_type_raw, UNVOICED ) )
956 : {
957 0 : uv_flag = 1;
958 0 : move16();
959 : }
960 :
961 0 : Copy( voice_factors, vf_modified_fx, NB_SUBFR16k );
962 0 : IF( EQ_16( coder_type, VOICED ) )
963 : {
964 0 : FOR( i = 1; i < NB_SUBFR; i++ )
965 : {
966 : /*vf_modified[i] = 0.8f * voice_factors[i] + 0.2f * voice_factors[i-1];*/
967 0 : vf_modified_fx[i] = add( mult_r( 26214, voice_factors[i] ), mult_r( 6553, voice_factors[i - 1] ) );
968 : }
969 0 : IF( NE_16( st_fx->L_frame, L_FRAME ) )
970 : {
971 0 : vf_modified_fx[4] = add( mult_r( 26214, voice_factors[4] ), mult_r( 6553, voice_factors[3] ) );
972 : }
973 : }
974 :
975 : /* From low band excitation, generate highband excitation */
976 0 : Lmax = L_deposit_l( 0 );
977 0 : FOR( i = 0; i < L_FRAME32k; i++ )
978 : {
979 0 : Lmax = L_max( Lmax, L_abs( bwe_exc_extended[i] ) );
980 : }
981 :
982 0 : Q_bwe_exc = 31;
983 0 : move16();
984 0 : if ( Lmax != 0 )
985 : {
986 0 : Q_bwe_exc = norm_l( Lmax );
987 : }
988 0 : Q_bwe_exc = sub( Q_bwe_exc, 3 );
989 0 : Q_bwe_exc = add( Q_bwe_exc, add( Q_new, Q_new ) );
990 :
991 0 : find_max_mem_wb_enc( st_fx, &n_mem );
992 :
993 0 : IF( GT_16( sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc ), n_mem ) )
994 : {
995 0 : Q_bwe_exc = add( st_fx->prev_Q_bwe_exc, n_mem );
996 : }
997 :
998 0 : IF( uv_flag )
999 : {
1000 0 : if ( GT_16( Q_bwe_exc, 20 ) )
1001 : {
1002 0 : Q_bwe_exc = 20;
1003 0 : move16(); /* restrict this to 20 due to the Q factor requireemnt of the random number generator (keep 1 bit headroom) */
1004 : }
1005 : }
1006 :
1007 0 : prev_pow = 0;
1008 0 : move32();
1009 0 : IF( st_fx->element_mode > EVS_MONO )
1010 : {
1011 0 : tmp = sub( shl( sub( st_fx->prev_Q_bwe_exc, 16 ), 1 ), 31 + 16 );
1012 0 : prev_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /*Q(2*(st_fx->prev_Q_bwe_exc-16))*/
1013 : }
1014 0 : FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
1015 : {
1016 0 : prev_pow = L_mac0( prev_pow, hBWE_TD->state_syn_shbexc_fx[i], hBWE_TD->state_syn_shbexc_fx[i] ); /* Q(2*st_fx->prev_Q_bwe_exc) */
1017 : }
1018 :
1019 0 : rescale_genWB_mem_enc( st_fx, sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc ) );
1020 :
1021 0 : Copy( hBWE_TD->old_bwe_exc_extended_fx, bwe_exc_extended_16, NL_BUFF_OFFSET );
1022 0 : sc = sub( Q_bwe_exc, add( Q_new, Q_new ) );
1023 0 : FOR( i = 0; i < L_FRAME32k; i++ )
1024 : {
1025 0 : bwe_exc_extended_16[i + NL_BUFF_OFFSET] = round_fx( L_shl( bwe_exc_extended[i], sc ) );
1026 0 : move16();
1027 : }
1028 0 : Copy( bwe_exc_extended_16 + L_FRAME32k, hBWE_TD->old_bwe_exc_extended_fx, NL_BUFF_OFFSET );
1029 :
1030 :
1031 0 : Copy( hBWE_TD->state_syn_shbexc_fx, shaped_wb_excitation, L_SHB_LAHEAD / 4 );
1032 0 : Q_bwe_exc_ext = sub( Q_bwe_exc, 16 );
1033 :
1034 :
1035 0 : GenShapedWBExcitation_fx( shaped_wb_excitation + L_SHB_LAHEAD / 4, lpc_wb, exc4kWhtnd, hBWE_TD->mem_csfilt_fx,
1036 0 : hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->mem_genSHBexc_filt_down_wb2_fx, hBWE_TD->mem_genSHBexc_filt_down_wb3_fx,
1037 0 : hBWE_TD->state_lpc_syn_fx, coder_type, bwe_exc_extended_16, Q_bwe_exc_ext, hBWE_TD->bwe_seed,
1038 0 : vf_modified_fx, uv_flag, st_fx->igf );
1039 :
1040 0 : curr_pow = 0;
1041 0 : move16();
1042 0 : IF( st_fx->element_mode > EVS_MONO )
1043 : {
1044 0 : tmp = sub( shl( Q_bwe_exc_ext, 1 ), 31 + 16 );
1045 0 : curr_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /*Q(2*(Q_bwe_exc_ext))*/
1046 : }
1047 0 : FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
1048 : {
1049 0 : curr_pow = L_mac0( curr_pow, shaped_wb_excitation[i + L_SHB_LAHEAD / 4], shaped_wb_excitation[i + L_SHB_LAHEAD / 4] ); /* Q(2*Q_bwe_exc_ext) */
1050 : }
1051 :
1052 0 : IF( GT_16( voice_factors[0], 24576 ) )
1053 : {
1054 0 : curr_pow = L_shr( curr_pow, 2 ); /* Q(2*Q_bwe_exc_ext) */
1055 : }
1056 :
1057 0 : Lscale = root_a_over_b_fx( curr_pow, shl_r( Q_bwe_exc_ext, 1 ), prev_pow, shl_r( sub( st_fx->prev_Q_bwe_exc, 16 ), 1 ), &exp );
1058 :
1059 :
1060 0 : FOR( i = 0; i < L_SHB_LAHEAD / 4 - 1; i++ )
1061 : {
1062 0 : L_tmp = Mult_32_16( Lscale, shaped_wb_excitation[i] ); /* Q(16-exp+Q_bwe_exc_ext) */
1063 0 : shaped_wb_excitation[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc_ext */
1064 0 : move16();
1065 : }
1066 :
1067 0 : Lscale = root_a_fx( Lscale, 31 - exp, &exp );
1068 0 : L_tmp = Mult_32_16( Lscale, shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] ); /* Q(16-exp+Q_bwe_exc_ext) */
1069 0 : shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] = round_fx( L_shl( L_tmp, exp ) ); /* Q_bwe_exc_ext */
1070 0 : move16();
1071 : /* Update WB excitation */
1072 0 : Copy( shaped_wb_excitation + L_FRAME16k / 4, hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD / 4 );
1073 :
1074 0 : EstimateSHBGainShape_fx( SHB_OVERLAP_LEN / 2, hb_frame, Q_ns,
1075 : shaped_wb_excitation, Q_bwe_exc_ext, GainShape, subwin_wb_fx, NULL );
1076 :
1077 : /* Gain frame adjustment factor */
1078 0 : test();
1079 0 : IF( GainShape[0] && hBWE_TD->prev_wb_GainShape )
1080 : {
1081 0 : exp = norm_s( hBWE_TD->prev_wb_GainShape );
1082 0 : tmp = div_s( shl( 1, sub( 14, exp ) ), hBWE_TD->prev_wb_GainShape );
1083 0 : L_tmp = L_mult( GainShape[0], tmp ); /* Q(30 - exp) */
1084 :
1085 0 : exp1 = norm_l( L_tmp );
1086 0 : frac = Log2_norm_lc( L_shl( L_tmp, exp1 ) );
1087 0 : exp1 = sub( exp, exp1 );
1088 0 : L_tmp = Mpy_32_16( exp1, frac, 22713 );
1089 0 : temp_wb_fac_fx = round_fx( L_shl( L_tmp, 10 ) );
1090 : }
1091 : ELSE
1092 : {
1093 0 : temp_wb_fac_fx = 0;
1094 0 : move16();
1095 : }
1096 0 : L_feedback = L_mult0( temp_wb_fac_fx, temp_wb_fac_fx );
1097 0 : FOR( i = 1; i < NUM_SHB_SUBFR / 4; i++ )
1098 : {
1099 : /* temp_swb_fac = (float)log( (GainShape[i]+0.00001f) / (GainShape[i-1]+0.0001f) ); */
1100 0 : test();
1101 0 : IF( GainShape[i] && GainShape[i - 1] )
1102 : {
1103 0 : exp = norm_s( GainShape[i - 1] );
1104 0 : tmp = div_s( shl( 1, sub( 14, exp ) ), GainShape[i - 1] );
1105 0 : L_tmp = L_mult( GainShape[i], tmp ); /*Q(30 - exp) */
1106 :
1107 0 : exp1 = norm_l( L_tmp );
1108 0 : frac = Log2_norm_lc( L_shl( L_tmp, exp1 ) );
1109 0 : move16();
1110 0 : exp1 = sub( exp, exp1 );
1111 0 : L_tmp = Mpy_32_16( exp1, frac, 22713 );
1112 0 : temp_wb_fac_fx = round_fx( L_shl( L_tmp, 10 ) );
1113 : }
1114 : ELSE
1115 : {
1116 0 : temp_wb_fac_fx = 0;
1117 0 : move16();
1118 : }
1119 :
1120 0 : L_feedback = L_mac( L_feedback, temp_wb_fac_fx, temp_wb_fac_fx );
1121 : }
1122 0 : L_tmp = L_add( L_shr( L_feedback, 1 ), 1 << 21 ); /* Q30 */
1123 :
1124 0 : IF( L_tmp != 0 )
1125 : {
1126 0 : exp = norm_l( L_tmp );
1127 0 : tmp = extract_h( L_shl( L_tmp, exp ) );
1128 0 : exp = sub( sub( 30, exp ), 21 );
1129 0 : tmp = div_s( 16384, tmp ); /* Q(15+exp) */
1130 0 : L_tmp = L_shr( L_mult( 13107, tmp ), exp ); /* Q31 */
1131 0 : feedback_fx = round_fx( L_tmp ); /* Q15 */
1132 : }
1133 : ELSE
1134 : {
1135 0 : feedback_fx = 8738;
1136 0 : move16(); /* Q15 */
1137 : }
1138 :
1139 :
1140 0 : temp_wb_fac_fx = hBWE_TD->prev_wb_GainShape;
1141 0 : move16();
1142 0 : FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
1143 : {
1144 0 : GainShape[i] = add( mult_r( sub( 32767, feedback_fx ), GainShape[i] ), mult_r( feedback_fx, temp_wb_fac_fx ) );
1145 0 : move16();
1146 0 : temp_wb_fac_fx = GainShape[i];
1147 0 : move16();
1148 : }
1149 :
1150 0 : hBWE_TD->prev_wb_GainShape = GainShape[sub( shr( NUM_SHB_SUBFR, 2 ), 1 )];
1151 0 : move16();
1152 0 : p2m_in = pow_off_pk_fx( GainShape, shr( NUM_SHB_SUBFR, 2 ), 1 );
1153 0 : move16();
1154 :
1155 0 : IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
1156 : {
1157 0 : FOR( i = 0; i < 8; i++ )
1158 : {
1159 0 : GainShape[i] = RECIP_ROOT_EIGHT_FX;
1160 0 : move16();
1161 : }
1162 : }
1163 : ELSE
1164 : {
1165 0 : push_indice( st_fx->hBstr, IND_UV_FLAG, uv_flag, 1 );
1166 :
1167 : /* Quantization of the subframe gain parameter */
1168 0 : QuantizeSHBsubgains_fx( st_fx, GainShape, st_fx->extl );
1169 : }
1170 :
1171 : /* Compute the power of gains away from the peak gain after quantization */
1172 0 : p2m_out = pow_off_pk_fx( GainShape, NUM_SHB_SUBFR / 2, 2 );
1173 :
1174 : /* Estimate the gain parameter */
1175 0 : EstimateSHBFrameGain_fx( SHB_OVERLAP_LEN / 2, hb_frame, Q_ns, shaped_wb_excitation, Q_bwe_exc_ext, GainShape,
1176 : &GainFrame, window_wb_fx, subwin_wb_fx, 0 );
1177 :
1178 :
1179 : /* If there's a big difference in the power of gains away from the peak gain */
1180 : /* due to poor quantization then suppress energy of the high band. */
1181 :
1182 0 : IF( GT_32( p2m_out, L_shl( p2m_in, 1 ) ) )
1183 : {
1184 0 : L_tmp = root_a_over_b_fx( L_shl( p2m_in, 1 ), 29, p2m_out, 29, &exp_out );
1185 0 : GainFrame = L_shl( Mult_32_32( GainFrame, L_tmp ), exp_out ); /* Q18 */
1186 : }
1187 :
1188 0 : pitBufAvg_fx = 0;
1189 0 : move16();
1190 :
1191 0 : FOR( i = 0; i < NB_SUBFR; i++ )
1192 : {
1193 0 : pitBufAvg_fx = add( pitBufAvg_fx, mult_r( pitch_buf[i], 82 ) ); /*Q6 */
1194 : }
1195 0 : voicingBufAvg_fx = 0;
1196 0 : move16();
1197 0 : FOR( i = 0; i < 3; i++ )
1198 : {
1199 0 : voicingBufAvg_fx = add( voicingBufAvg_fx, mult_r( voicing_fx[i], 10912 ) ); /*Q15 */
1200 : }
1201 : /* GainFrame *= max(min((float)(pitBufAvg/voicingBufAvg), 1.0f), 0.7f); */
1202 0 : tmp = 32767;
1203 0 : move16();
1204 0 : IF( voicingBufAvg_fx > 0 )
1205 : {
1206 0 : exp = norm_s( voicingBufAvg_fx );
1207 0 : tmp = div_s( shl( 1, sub( 14, exp ) ), voicingBufAvg_fx ); /* (14-exp) */
1208 0 : L_tmp = L_mult( pitBufAvg_fx, tmp ); /* (21-exp) */
1209 0 : L_tmp = L_shl_o( L_tmp, add( exp, 10 ), &Overflow );
1210 0 : tmp = round_fx_o( L_tmp, &Overflow ); /* Q15 */
1211 : }
1212 :
1213 0 : tmp = s_max( s_min( tmp, 32767 ), 22938 ); /* Q15 */
1214 0 : GainFrame = Mult_32_16( GainFrame, tmp ); /* Q18 */
1215 :
1216 0 : test();
1217 0 : IF( LT_16( lsp_spacing_fx, 328 ) && lsp_spacing_fx )
1218 : {
1219 0 : GainFrame = Mult_32_16( GainFrame, 21299 ); /* Q18 */
1220 : }
1221 :
1222 0 : IF( EQ_16( st_fx->codec_mode, MODE1 ) )
1223 : {
1224 : /*wbbwe_em_factor = add( mult_r( 29491, st_fx->prev_wbbwe_em_factor_fx ), mult_r( 3277, wbbwe_em_factor ) ); */ /* Q15 */
1225 : }
1226 :
1227 :
1228 : /*0.25f*sum_f(voice_factors, NB_SUBFR)*/
1229 0 : L_tmp = L_mult( voice_factors[0], 8192 );
1230 0 : FOR( i = 1; i < NB_SUBFR; i++ )
1231 : {
1232 0 : L_tmp = L_mac( L_tmp, voice_factors[i], 8192 );
1233 : }
1234 0 : avg_voice_fac = round_fx( L_tmp );
1235 :
1236 0 : test();
1237 0 : test();
1238 0 : IF( st_fx->igf != 0 && EQ_16( coder_type, VOICED ) )
1239 : {
1240 : /*GainFrame *= 0.5f;*/
1241 :
1242 0 : GainFrame = Mult_32_16( GainFrame, 16384 );
1243 : }
1244 0 : ELSE IF( st_fx->igf != 0 && GT_16( avg_voice_fac, 11469 ) ) /*Q15 -> 0.35f*/
1245 : {
1246 : /*GainFrame *= 0.75f;*/
1247 0 : GainFrame = Mult_32_16( GainFrame, 24576 );
1248 : }
1249 :
1250 : /* Quantization of the frame gain parameter */
1251 0 : QuantizeSHBframegain_fx( st_fx, &GainFrame, st_fx->extl, st_fx->extl_brate, &hRF->RF_bwe_gainFr_ind );
1252 :
1253 : /* Adjust the subframe and frame gain of the synthesized SHB signal */
1254 : /* Scale the shaped excitation*/
1255 :
1256 0 : ScaleShapedSHB_fx( SHB_OVERLAP_LEN / 2, shaped_wb_excitation, hBWE_TD->syn_overlap_fx, GainShape, GainFrame, window_wb_fx, subwin_wb_fx,
1257 : &Q_bwe_exc_ext, &dummy, dummy, dummy );
1258 :
1259 0 : st_fx->prev_Q_bwe_exc = Q_bwe_exc;
1260 0 : move16();
1261 :
1262 0 : return;
1263 : }
1264 :
1265 :
1266 : /*==========================================================================*/
1267 : /* FUNCTION : void wb_tbe_enc_ivas_fx() */
1268 : /*--------------------------------------------------------------------------*/
1269 : /* PURPOSE : WB TBE encoder, 6 - 8 kHz band encoding module */
1270 : /*--------------------------------------------------------------------------*/
1271 : /* INPUT ARGUMENTS : */
1272 : /* Word16 *new_speech i : original input signal Q(-1) */
1273 : /* Word32 *bwe_exc_extended i : bandwidth extended exciatation Q(2*Q_new) */
1274 : /* Word16 voice_factors[] i : voicing factors Q15 */
1275 : /* Word16 pitch_buf[] i : pitch for each subframe Q6 */
1276 : /*--------------------------------------------------------------------------*/
1277 : /* OUTPUT ARGUMENTS : */
1278 : /* Word16 *synth o : WB SHB final synthesis */
1279 : /*--------------------------------------------------------------------------*/
1280 : /* INPUT/OUTPUT ARGUMENTS : */
1281 : /* Encoder_State *st_fx i/o: encoder state structure */
1282 : /*--------------------------------------------------------------------------*/
1283 : /* RETURN ARGUMENTS : */
1284 : /* _ None */
1285 : /*--------------------------------------------------------------------------*/
1286 : /* */
1287 : /*==========================================================================*/
1288 :
1289 7343 : void wb_tbe_enc_ivas_fx(
1290 : Encoder_State *st_fx, /* i/o: encoder state structure */
1291 : const Word16 *hb_speech, /* i : HB target signal (6-8kHz) at 16kHz Q(-1) */
1292 : const Word32 *bwe_exc_extended, /* i : bandwidth extended exciatation Q(2*Q_new) */
1293 : const Word16 Q_new, /* i : input HB speech Q factor */
1294 : const Word16 voice_factors[], /* i : voicing factors Q15 */
1295 : const Word16 pitch_buf[] /* i : pitch for each subframe Q6 */
1296 : )
1297 : {
1298 : Word16 i, j, k;
1299 : Word16 hb_old_speech[( L_LOOK_12k8 + L_SUBFR + L_FRAME ) * 5 / 16];
1300 : Word16 bwe_exc_extended_16[L_FRAME32k + NL_BUFF_OFFSET];
1301 :
1302 : Word16 shaped_wb_excitation[( L_FRAME16k + L_SHB_LAHEAD ) / 4];
1303 : Word16 shaped_wb_exc_scale[L_SHB_LAHEAD / 4];
1304 : Word16 exc4kWhtnd[L_FRAME16k / 4];
1305 : /*Word16 ana_align_delay = WBTBE_ANA_ALIGNDELAY; */ /* -L_SHB_LAHEAD/4 - 5 */
1306 : Word32 GainFrame;
1307 : Word16 GainShape[NUM_SHB_SUBFR];
1308 : Word16 lpc_wb[LPC_SHB_ORDER_WB + 1];
1309 : Word32 lpc_wb_32_fx[LPC_SHB_ORDER_WB + 1];
1310 7343 : Word16 lsp_wb[LPC_SHB_ORDER_WB], weights_lsp[LPC_SHB_ORDER_WB] = { 32767, 32767 };
1311 7343 : move16();
1312 7343 : move16();
1313 : Word16 *hb_new_speech, *hb_frame /*, hb_speech[ L_FRAME16k ]*/;
1314 : Word16 R_h[LPC_SHB_ORDER_WB + 2], R_l[LPC_SHB_ORDER_WB + 2];
1315 : Word16 Q_R;
1316 : Word32 LepsP[LPC_SHB_ORDER_WB + 1];
1317 :
1318 : Word32 prev_pow, curr_pow, Lscale;
1319 : /* Word16 scale; */
1320 : /*Word16 ramp_flag;*/
1321 : Word32 p2m_in, p2m_out;
1322 : /*Word16 cnt, max =0;*/
1323 : Word16 n_mem, Q_bwe_exc, Q_bwe_exc_ext, exp_out, Q_cur_pow, Q_prev_pow; /* Q_hb_frame; */
1324 : Word32 L_tmp, Lmax;
1325 : Word16 tmp, exp, Q_out, sc;
1326 7343 : Word16 Q_ns = -1;
1327 7343 : move16();
1328 :
1329 : Word16 pitBufAvg_fx, voicingBufAvg_fx;
1330 : Word16 vf_modified_fx[NB_SUBFR16k];
1331 : Word16 temp_wb_fac_fx, feedback_fx;
1332 : Word16 lsp_spacing_fx;
1333 : Word16 lsp_wb_temp_fx[LPC_SHB_ORDER_WB], lpc_wb_temp_fx[LPC_SHB_ORDER_WB + 1];
1334 : Word32 L_feedback;
1335 : Word16 frac, exp1;
1336 : Word16 uv_flag;
1337 7343 : Word16 dummy = 0;
1338 : Word16 avg_voice_fac;
1339 7343 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
1340 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
1341 7343 : Flag Overflow = 0;
1342 7343 : move16();
1343 : #endif
1344 7343 : move16();
1345 :
1346 : /*Word16 att = 32767;*/
1347 7343 : set16_fx( bwe_exc_extended_16, 0, L_FRAME32k + NL_BUFF_OFFSET );
1348 7343 : hb_new_speech = hb_old_speech + WBTBE_LOOK_LSUFBR_5_OVER_16;
1349 7343 : hb_frame = hb_old_speech + WBTBE_LSUBFR_5_OVER_16 + WBTBE_ANA_ALIGNDELAY;
1350 :
1351 7343 : Copy( hBWE_TD->old_speech_wb_fx, hb_old_speech, WBTBE_LOOK_LSUFBR_5_OVER_16 );
1352 7343 : Copy( hb_speech, hb_new_speech, LFRAME16K_OVER_4 );
1353 7343 : Copy( hb_old_speech + LFRAME16K_OVER_4, hBWE_TD->old_speech_wb_fx, WBTBE_LOOK_LSUFBR_5_OVER_16 );
1354 :
1355 7343 : test();
1356 7343 : test();
1357 7343 : test();
1358 7343 : test();
1359 7343 : test();
1360 7343 : test();
1361 7343 : IF( ( NE_16( st_fx->last_extl, WB_TBE ) && NE_16( st_fx->last_extl, WB_BWE ) ) && ( st_fx->clas == UNVOICED_CLAS || ( LT_16( st_fx->voicing_fx[0], 16384 ) && LT_16( st_fx->voicing_fx[1], 16384 ) && LT_16( st_fx->voicing_fx[2], 16384 ) ) ) && !st_fx->igf )
1362 : {
1363 : /* In case of unvoiced signals after switching cores, back-propagate the target signal */
1364 116 : Copy( hb_speech, hb_old_speech, WBTBE_LOOK_LSUFBR_5_OVER_16 );
1365 :
1366 116 : i = WBTBE_LOOK_LSUFBR_5_OVER_16;
1367 116 : move16();
1368 116 : k = 0;
1369 116 : move16();
1370 :
1371 2436 : FOR( j = 0; j < L_SUBFR16k; j = j + 4 )
1372 : {
1373 2320 : L_tmp = L_mult( hb_old_speech[i], ola_win_shb_switch_fold_fx[j] );
1374 2320 : hb_old_speech[i] = mac_ro( L_tmp, hb_speech[k], ola_win_shb_switch_fold_fx[L_SUBFR16k - 4 - j], &Overflow );
1375 2320 : move16();
1376 2320 : i--;
1377 2320 : k++;
1378 : }
1379 : }
1380 :
1381 7343 : autocorr_fx( hb_old_speech, LPC_SHB_ORDER_WB + 1, R_h, R_l, &Q_R, ( ( NS2SA( INT_FS_12k8, 5000000L ) + L_SUBFR + L_FRAME ) * 5 / 16 ), win_lpc_hb_wb_ivas_fx, 0, 1 );
1382 :
1383 7343 : E_LPC_lev_dur( R_h, R_l, lpc_wb_temp_fx, LepsP, LPC_SHB_ORDER_WB, NULL ); // Since 0th index will be 1 in floating point buffer, in fixed point one has to take norm of 0th index to identify the Q-factor
1384 :
1385 7343 : Copy_Scale_sig( lpc_wb_temp_fx, lpc_wb_temp_fx, LPC_SHB_ORDER_WB + 1, sub( norm_s( lpc_wb_temp_fx[0] ), 2 ) ); // Q12
1386 :
1387 : /* convert into lsps and calculate weights */
1388 58744 : FOR( i = 0; i <= LPC_SHB_ORDER_WB; i++ )
1389 : {
1390 51401 : lpc_wb_32_fx[i] = L_negate( L_deposit_h( shr( lpc_wb_temp_fx[i], 1 ) ) ); // Q27
1391 51401 : move32();
1392 : }
1393 :
1394 7343 : lpc2lsp_ivas_fx( &lpc_wb_32_fx[1], lsp_wb_temp_fx, st_fx->prev_lsp_wb_temp_fx, LPC_SHB_ORDER_WB );
1395 :
1396 51401 : FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
1397 : {
1398 44058 : st_fx->prev_lsp_wb_temp_fx[i] = lsp_wb_temp_fx[i];
1399 44058 : move16();
1400 : }
1401 :
1402 : /* lsp_spacing_fx = 16384; move16(); */
1403 7343 : lsp_spacing_fx = lsp_wb_temp_fx[0];
1404 7343 : move16();
1405 44058 : FOR( i = 1; i < LPC_SHB_ORDER_WB; i++ )
1406 : {
1407 : /*if ( i == 0 )
1408 : {
1409 : tmp = lsp_wb_temp_fx[0]; move16();
1410 : }
1411 : else
1412 : {*/
1413 36715 : tmp = sub( lsp_wb_temp_fx[i], lsp_wb_temp_fx[i - 1] );
1414 : /*} */
1415 :
1416 36715 : lsp_spacing_fx = s_min( lsp_spacing_fx, tmp );
1417 : }
1418 :
1419 : /* Spectral smoothing of autocorrelation coefficients */
1420 51401 : FOR( i = 1; i <= LPC_SHB_ORDER_WB; i++ )
1421 : {
1422 44058 : L_tmp = Mpy_32( R_h[i], R_l[i], wac_h[i - 1], wac_l[i - 1] );
1423 44058 : L_Extract( L_tmp, &R_h[i], &R_l[i] );
1424 : }
1425 7343 : R_l[0] = s_max( R_l[0], 1 );
1426 7343 : move16();
1427 :
1428 7343 : test();
1429 7343 : IF( EQ_16( st_fx->rf_mode, 1 ) || EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
1430 : {
1431 2442 : E_LPC_lev_dur( R_h, R_l, lpc_wb, LepsP, LPC_SHB_ORDER_LBR_WB, NULL );
1432 2442 : Copy_Scale_sig( lpc_wb, lpc_wb, LPC_SHB_ORDER_LBR_WB + 1, sub( norm_s( lpc_wb[0] ), 2 ) );
1433 :
1434 : /* Expand bandwidth of the LP coeffs */
1435 14652 : FOR( i = 0; i <= LPC_SHB_ORDER_LBR_WB; i++ )
1436 : {
1437 12210 : lpc_wb[i] = mult_r( lpc_wb[i], lpc_weights_fx[i] );
1438 12210 : move16();
1439 : }
1440 :
1441 : /* convert into lsps and calculate weights */
1442 14652 : FOR( i = 0; i <= LPC_SHB_ORDER_LBR_WB; i++ )
1443 : {
1444 12210 : lpc_wb_32_fx[i] = L_negate( L_shr( L_deposit_h( lpc_wb[i] ), 1 ) );
1445 12210 : move32(); /*Q27 */
1446 : }
1447 2442 : lpc2lsp_fx( &lpc_wb_32_fx[1], lsp_wb, st_fx->prev_lsp_wb_fx, LPC_SHB_ORDER_LBR_WB );
1448 :
1449 12210 : FOR( i = 0; i < LPC_SHB_ORDER_LBR_WB; i++ )
1450 : {
1451 9768 : st_fx->prev_lsp_wb_fx[i] = lsp_wb[i];
1452 9768 : move16();
1453 : }
1454 :
1455 2442 : lsp_weights_fx( lsp_wb, weights_lsp, LPC_SHB_ORDER_LBR_WB, &Q_out );
1456 :
1457 : /* Quantization of LSFs */
1458 2442 : i = closest_centroid_fx( lsp_wb, weights_lsp, lbr_wb_bwe_lsfvq_cbook_2bit_fx, 4, LPC_SHB_ORDER_LBR_WB );
1459 2442 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
1460 : {
1461 0 : hBWE_TD->lsf_WB = i;
1462 0 : move16();
1463 : }
1464 : ELSE
1465 : {
1466 2442 : push_indice( st_fx->hBstr, IND_SHB_LSF, i, NUM_BITS_LBR_WB_LSF );
1467 : }
1468 :
1469 2442 : Copy( lbr_wb_bwe_lsfvq_cbook_2bit_fx + i * LPC_SHB_ORDER_LBR_WB, lsp_wb, LPC_SHB_ORDER_LBR_WB );
1470 :
1471 2442 : lsp2lpc_fx( &lpc_wb[1], lsp_wb, st_fx->prev_lpc_wb_fx, LPC_SHB_ORDER_LBR_WB );
1472 :
1473 2442 : set16_fx( lpc_wb + LPC_SHB_ORDER_LBR_WB + 1, 0, ( LPC_SHB_ORDER_WB - LPC_SHB_ORDER_LBR_WB ) );
1474 17094 : FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
1475 : {
1476 14652 : st_fx->prev_lpc_wb_fx[i] = lpc_wb[i + 1];
1477 14652 : move16();
1478 : }
1479 :
1480 12210 : FOR( i = 1; i < LPC_SHB_ORDER_LBR_WB + 1; i++ )
1481 : {
1482 9768 : lpc_wb[i] = negate( lpc_wb[i] );
1483 9768 : move16();
1484 : }
1485 2442 : lpc_wb[0] = 4096;
1486 2442 : move16();
1487 : }
1488 : ELSE /* 13.2kbps */
1489 : {
1490 4901 : E_LPC_lev_dur( R_h, R_l, lpc_wb, LepsP, LPC_SHB_ORDER_WB, NULL );
1491 4901 : Copy_Scale_sig( lpc_wb, lpc_wb, LPC_SHB_ORDER_WB + 1, sub( norm_s( lpc_wb[0] ), 2 ) );
1492 :
1493 : /* Expand bandwidth of the LP coeffs */
1494 39208 : FOR( i = 0; i <= LPC_SHB_ORDER_WB; i++ )
1495 : {
1496 34307 : lpc_wb[i] = mult_r( lpc_wb[i], lpc_weights_fx[i] );
1497 34307 : move16();
1498 : }
1499 :
1500 : /* convert into lsps and calculate weights */
1501 39208 : FOR( i = 0; i <= LPC_SHB_ORDER_WB; i++ )
1502 : {
1503 34307 : lpc_wb_32_fx[i] = L_negate( L_shr( L_deposit_h( lpc_wb[i] ), 1 ) );
1504 34307 : move32(); /*Q27 */
1505 : }
1506 :
1507 4901 : lpc2lsp_fx( &lpc_wb_32_fx[1], lsp_wb, st_fx->prev_lsp_wb_fx, LPC_SHB_ORDER_WB );
1508 :
1509 :
1510 34307 : FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
1511 : {
1512 29406 : st_fx->prev_lsp_wb_fx[i] = lsp_wb[i];
1513 29406 : move16();
1514 : }
1515 :
1516 4901 : lsp_weights_fx( lsp_wb, weights_lsp, LPC_SHB_ORDER_WB, &Q_out );
1517 :
1518 : /* Quantization of LSFs */
1519 4901 : i = closest_centroid_fx( lsp_wb, weights_lsp, wb_bwe_lsfvq_cbook_8bit_fx, 256, LPC_SHB_ORDER_WB ); /*move16(); */
1520 :
1521 4901 : push_indice( st_fx->hBstr, IND_SHB_LSF, i, NUM_BITS_WB_LSF );
1522 4901 : Copy( wb_bwe_lsfvq_cbook_8bit_fx + i * LPC_SHB_ORDER_WB, lsp_wb, LPC_SHB_ORDER_WB );
1523 :
1524 4901 : lsp2lpc_fx( &lpc_wb[1], lsp_wb, st_fx->prev_lpc_wb_fx, LPC_SHB_ORDER_WB );
1525 :
1526 34307 : FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
1527 : {
1528 29406 : st_fx->prev_lpc_wb_fx[i] = lpc_wb[i + 1];
1529 29406 : move16();
1530 : }
1531 34307 : FOR( i = 1; i < LPC_SHB_ORDER_WB + 1; i++ )
1532 : {
1533 29406 : lpc_wb[i] = negate( lpc_wb[i] );
1534 29406 : move16();
1535 : }
1536 4901 : lpc_wb[0] = 4096;
1537 4901 : move16();
1538 : }
1539 :
1540 7343 : uv_flag = 0;
1541 7343 : move16();
1542 7343 : test();
1543 7343 : if ( EQ_32( st_fx->extl_brate, WB_TBE_1k05 ) && EQ_16( st_fx->coder_type_raw, UNVOICED ) )
1544 : {
1545 440 : uv_flag = 1;
1546 440 : move16();
1547 : }
1548 :
1549 7343 : Copy( voice_factors, vf_modified_fx, NB_SUBFR16k );
1550 7343 : IF( EQ_16( st_fx->coder_type, VOICED ) )
1551 : {
1552 4932 : FOR( i = 1; i < NB_SUBFR; i++ )
1553 : {
1554 : /*vf_modified[i] = 0.8f * voice_factors[i] + 0.2f * voice_factors[i-1];*/
1555 3699 : vf_modified_fx[i] = add( mult_r( 26214, voice_factors[i] ), mult_r( 6553, voice_factors[i - 1] ) );
1556 3699 : move16();
1557 : }
1558 1233 : IF( NE_16( st_fx->L_frame, L_FRAME ) )
1559 : {
1560 0 : vf_modified_fx[4] = add( mult_r( 26214, voice_factors[4] ), mult_r( 6553, voice_factors[3] ) );
1561 0 : move16();
1562 : }
1563 : }
1564 :
1565 : /* From low band excitation, generate highband excitation */
1566 7343 : Lmax = L_deposit_l( 0 );
1567 4706863 : FOR( i = 0; i < L_FRAME32k; i++ )
1568 : {
1569 4699520 : Lmax = L_max( Lmax, L_abs( bwe_exc_extended[i] ) );
1570 : }
1571 :
1572 7343 : Q_bwe_exc = 31;
1573 7343 : move16();
1574 7343 : if ( Lmax != 0 )
1575 : {
1576 7343 : Q_bwe_exc = norm_l( Lmax );
1577 : }
1578 7343 : Q_bwe_exc = sub( Q_bwe_exc, 3 );
1579 7343 : Q_bwe_exc = add( Q_bwe_exc, add( Q_new, Q_new ) );
1580 :
1581 7343 : find_max_mem_wb_enc( st_fx, &n_mem );
1582 :
1583 7343 : IF( GT_16( sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc ), n_mem ) )
1584 : {
1585 94 : Q_bwe_exc = add( st_fx->prev_Q_bwe_exc, n_mem );
1586 : }
1587 :
1588 7343 : IF( uv_flag )
1589 : {
1590 440 : if ( GT_16( Q_bwe_exc, 20 ) )
1591 : {
1592 206 : Q_bwe_exc = 20;
1593 206 : move16(); /* restrict this to 20 due to the Q factor requireemnt of the random number generator (keep 1 bit headroom) */
1594 : }
1595 : }
1596 :
1597 7343 : prev_pow = 0;
1598 7343 : move32();
1599 7343 : Q_prev_pow = norm_arr( hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD / 4 );
1600 : /* Compare with the guard bits needed in the subsequent operations.
1601 : * Guard bits for L_SHB_LAHEAD / 4 (expression evaluates to 5) is 3.
1602 : */
1603 7343 : IF( LT_16( Q_prev_pow, 3 ) )
1604 : {
1605 0 : FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
1606 : {
1607 0 : shaped_wb_exc_scale[i] = shr( hBWE_TD->state_syn_shbexc_fx[i], 3 );
1608 0 : move16();
1609 : }
1610 0 : Q_prev_pow = shl_r( sub( sub( st_fx->prev_Q_bwe_exc, 16 ), 3 ), 1 );
1611 0 : move16();
1612 : }
1613 : ELSE
1614 : {
1615 44058 : FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
1616 : {
1617 36715 : shaped_wb_exc_scale[i] = hBWE_TD->state_syn_shbexc_fx[i];
1618 36715 : move16();
1619 : }
1620 7343 : Q_prev_pow = shl_r( sub( st_fx->prev_Q_bwe_exc, 16 ), 1 );
1621 7343 : move16();
1622 : }
1623 :
1624 7343 : IF( st_fx->element_mode > EVS_MONO )
1625 : {
1626 7343 : tmp = sub( Q_prev_pow, 31 + 16 );
1627 7343 : prev_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /*Q_prev_pow*/
1628 : }
1629 44058 : FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
1630 : {
1631 36715 : prev_pow = L_mac0( prev_pow, shaped_wb_exc_scale[i], shaped_wb_exc_scale[i] ); /* Q_prev_pow */
1632 : }
1633 :
1634 7343 : rescale_genWB_mem_enc( st_fx, sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc ) );
1635 :
1636 7343 : sc = sub( Q_bwe_exc, add( Q_new, Q_new ) );
1637 4706863 : FOR( i = 0; i < L_FRAME32k; i++ )
1638 : {
1639 4699520 : bwe_exc_extended_16[i + NL_BUFF_OFFSET] = round_fx( L_shl( bwe_exc_extended[i], sc ) );
1640 4699520 : move16();
1641 : }
1642 :
1643 7343 : Copy( hBWE_TD->state_syn_shbexc_fx, shaped_wb_excitation, L_SHB_LAHEAD / 4 );
1644 7343 : Q_bwe_exc_ext = sub( Q_bwe_exc, 16 );
1645 :
1646 7343 : GenShapedWBExcitation_ivas_fx( shaped_wb_excitation + L_SHB_LAHEAD / 4, lpc_wb, exc4kWhtnd, hBWE_TD->mem_csfilt_fx,
1647 7343 : hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->mem_genSHBexc_filt_down_wb2_fx, hBWE_TD->mem_genSHBexc_filt_down_wb3_fx,
1648 7343 : hBWE_TD->state_lpc_syn_fx, st_fx->coder_type, bwe_exc_extended_16, Q_bwe_exc_ext, hBWE_TD->bwe_seed,
1649 7343 : vf_modified_fx, uv_flag, st_fx->igf );
1650 :
1651 7343 : curr_pow = 0;
1652 7343 : move32();
1653 7343 : Q_cur_pow = norm_arr( &shaped_wb_excitation[L_SHB_LAHEAD / 4], L_SHB_LAHEAD / 4 );
1654 : /* Compare with the guard bits needed in the subsequent operations.
1655 : * Guard bits for L_SHB_LAHEAD / 4 (expression evaluates to 5) is 3.
1656 : */
1657 7343 : IF( LT_16( Q_cur_pow, 3 ) )
1658 : {
1659 66 : FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
1660 : {
1661 55 : shaped_wb_exc_scale[i] = shr( shaped_wb_excitation[i + L_SHB_LAHEAD / 4], 3 );
1662 55 : move16();
1663 : }
1664 11 : Q_cur_pow = shl_r( sub( Q_bwe_exc_ext, 3 ), 1 );
1665 11 : move16();
1666 : }
1667 : ELSE
1668 : {
1669 43992 : FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
1670 : {
1671 36660 : shaped_wb_exc_scale[i] = shaped_wb_excitation[i + L_SHB_LAHEAD / 4];
1672 36660 : move16();
1673 : }
1674 7332 : Q_cur_pow = shl_r( Q_bwe_exc_ext, 1 );
1675 7332 : move16();
1676 : }
1677 7343 : IF( st_fx->element_mode > EVS_MONO )
1678 : {
1679 7343 : tmp = sub( Q_cur_pow, 31 + 16 );
1680 7343 : curr_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /*Q(Q_cur_pow)*/
1681 : }
1682 44058 : FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
1683 : {
1684 36715 : curr_pow = L_mac0( curr_pow, shaped_wb_exc_scale[i], shaped_wb_exc_scale[i] ); /* Q(Q_cur_pow) */
1685 : }
1686 :
1687 7343 : IF( GT_16( voice_factors[0], 24576 ) )
1688 : {
1689 254 : curr_pow = L_shr( curr_pow, 2 ); /* Q(Q_pow) */
1690 : }
1691 :
1692 7343 : Lscale = root_a_over_b_fx( curr_pow, Q_cur_pow, prev_pow, Q_prev_pow, &exp );
1693 :
1694 36715 : FOR( i = 0; i < L_SHB_LAHEAD / 4 - 1; i++ )
1695 : {
1696 29372 : L_tmp = Mpy_32_16_1( Lscale, shaped_wb_excitation[i] ); /* Q(16-exp+Q_bwe_exc_ext) */
1697 29372 : shaped_wb_excitation[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc_ext */
1698 29372 : move16();
1699 : }
1700 :
1701 7343 : Lscale = root_a_fx( Lscale, sub( 31, exp ), &exp );
1702 7343 : L_tmp = Mpy_32_16_1( Lscale, shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] ); /* Q(16-exp+Q_bwe_exc_ext) */
1703 7343 : shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc_ext */
1704 7343 : move16();
1705 :
1706 : /* Update WB excitation */
1707 7343 : Copy( shaped_wb_excitation + L_FRAME16k / 4, hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD / 4 );
1708 :
1709 7343 : EstimateSHBGainShape_fx( SHB_OVERLAP_LEN / 2, hb_frame, Q_ns,
1710 : shaped_wb_excitation, Q_bwe_exc_ext, GainShape, subwin_wb_fx, NULL );
1711 :
1712 : /* Gain frame adjustment factor */
1713 7343 : test();
1714 7343 : IF( GainShape[0] && hBWE_TD->prev_wb_GainShape )
1715 : {
1716 7312 : exp = norm_s( hBWE_TD->prev_wb_GainShape );
1717 7312 : tmp = div_s( shl( 1, sub( 14, exp ) ), hBWE_TD->prev_wb_GainShape );
1718 7312 : L_tmp = L_mult( GainShape[0], tmp ); /* Q(30 - exp) */
1719 :
1720 7312 : exp1 = norm_l( L_tmp );
1721 7312 : frac = Log2_norm_lc( L_shl( L_tmp, exp1 ) );
1722 7312 : exp1 = sub( exp, exp1 );
1723 7312 : L_tmp = Mpy_32_16( exp1, frac, 22713 );
1724 7312 : temp_wb_fac_fx = round_fx( L_shl( L_tmp, 10 ) );
1725 : }
1726 : ELSE
1727 : {
1728 31 : temp_wb_fac_fx = 0;
1729 31 : move16();
1730 : }
1731 7343 : L_feedback = L_mult0( temp_wb_fac_fx, temp_wb_fac_fx );
1732 29372 : FOR( i = 1; i < NUM_SHB_SUBFR / 4; i++ )
1733 : {
1734 : /* temp_swb_fac = (float)log( (GainShape[i]+0.00001f) / (GainShape[i-1]+0.0001f) ); */
1735 22029 : test();
1736 22029 : IF( GainShape[i] && GainShape[i - 1] )
1737 : {
1738 22029 : exp = norm_s( GainShape[i - 1] );
1739 22029 : tmp = div_s( shl( 1, sub( 14, exp ) ), GainShape[i - 1] );
1740 22029 : L_tmp = L_mult( GainShape[i], tmp ); /*Q(30 - exp) */
1741 :
1742 22029 : exp1 = norm_l( L_tmp );
1743 22029 : frac = Log2_norm_lc( L_shl( L_tmp, exp1 ) );
1744 22029 : exp1 = sub( exp, exp1 );
1745 22029 : L_tmp = Mpy_32_16( exp1, frac, 22713 );
1746 22029 : temp_wb_fac_fx = round_fx( L_shl( L_tmp, 10 ) );
1747 : }
1748 : ELSE
1749 : {
1750 0 : temp_wb_fac_fx = 0;
1751 0 : move16();
1752 : }
1753 :
1754 22029 : L_feedback = L_mac( L_feedback, temp_wb_fac_fx, temp_wb_fac_fx );
1755 : }
1756 7343 : L_tmp = L_add( L_shr( L_feedback, 1 ), 1 << 21 ); /* Q30 */
1757 :
1758 7343 : IF( L_tmp != 0 )
1759 : {
1760 7343 : exp = norm_l( L_tmp );
1761 7343 : tmp = extract_h( L_shl( L_tmp, exp ) );
1762 7343 : exp = sub( sub( 30, exp ), 21 );
1763 7343 : tmp = div_s( 16384, tmp ); /* Q(15+exp) */
1764 7343 : L_tmp = L_shr( L_mult( 13107, tmp ), exp ); /* Q31 */
1765 7343 : feedback_fx = round_fx( L_tmp ); /* Q15 */
1766 : }
1767 : ELSE
1768 : {
1769 0 : feedback_fx = 8738;
1770 0 : move16(); /* Q15 */
1771 : }
1772 :
1773 :
1774 7343 : temp_wb_fac_fx = hBWE_TD->prev_wb_GainShape;
1775 7343 : move16();
1776 36715 : FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
1777 : {
1778 29372 : GainShape[i] = add( mult_r( sub( 32767, feedback_fx ), GainShape[i] ), mult_r( feedback_fx, temp_wb_fac_fx ) );
1779 29372 : move16();
1780 29372 : temp_wb_fac_fx = GainShape[i];
1781 29372 : move16();
1782 : }
1783 :
1784 7343 : hBWE_TD->prev_wb_GainShape = GainShape[( NUM_SHB_SUBFR >> 2 ) - 1];
1785 7343 : move16();
1786 :
1787 : /* Compute the power of gains away from the peak gain prior to quantization */
1788 7343 : IF( st_fx->element_mode > EVS_MONO )
1789 : {
1790 7343 : p2m_in = pow_off_pk_corrected_fx( GainShape, NUM_SHB_SUBFR / 4, 1 );
1791 : }
1792 : ELSE
1793 : {
1794 0 : p2m_in = pow_off_pk_fx( GainShape, NUM_SHB_SUBFR / 4, 1 );
1795 : }
1796 :
1797 7343 : IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
1798 : {
1799 21978 : FOR( i = 0; i < 8; i++ )
1800 : {
1801 19536 : GainShape[i] = RECIP_ROOT_EIGHT_FX;
1802 19536 : move16();
1803 : }
1804 : }
1805 : ELSE
1806 : {
1807 4901 : push_indice( st_fx->hBstr, IND_UV_FLAG, uv_flag, 1 );
1808 :
1809 : /* Quantization of the subframe gain parameter */
1810 4901 : QuantizeSHBsubgains_ivas_fx( st_fx, GainShape, st_fx->extl );
1811 : }
1812 :
1813 : /* Compute the power of gains away from the peak gain after quantization */
1814 7343 : IF( st_fx->element_mode > EVS_MONO )
1815 : {
1816 7343 : p2m_out = pow_off_pk_corrected_fx( GainShape, NUM_SHB_SUBFR / 2, 2 );
1817 : }
1818 : ELSE
1819 : {
1820 0 : p2m_out = pow_off_pk_fx( GainShape, NUM_SHB_SUBFR / 2, 2 );
1821 : }
1822 :
1823 : /* Estimate the gain parameter */
1824 7343 : EstimateSHBFrameGain_ivas_fx( SHB_OVERLAP_LEN / 2, hb_frame, Q_ns, shaped_wb_excitation, Q_bwe_exc_ext, GainShape,
1825 : &GainFrame, window_wb_fx, subwin_wb_fx, 0 );
1826 :
1827 :
1828 : /* If there's a big difference in the power of gains away from the peak gain */
1829 : /* due to poor quantization then suppress energy of the high band. */
1830 :
1831 7343 : IF( GT_32( p2m_out, L_shl( p2m_in, 1 ) ) )
1832 : {
1833 305 : L_tmp = root_a_over_b_fx( L_shl( p2m_in, 1 ), 29, p2m_out, 29, &exp_out );
1834 305 : GainFrame = L_shl( Mpy_32_32( GainFrame, L_tmp ), exp_out ); /* Q18 */
1835 : }
1836 :
1837 7343 : pitBufAvg_fx = 0;
1838 7343 : move16();
1839 :
1840 36715 : FOR( i = 0; i < NB_SUBFR; i++ )
1841 : {
1842 29372 : pitBufAvg_fx = add( pitBufAvg_fx, mult_r( pitch_buf[i], 82 ) ); /*Q6 */
1843 : }
1844 7343 : voicingBufAvg_fx = 0;
1845 7343 : move16();
1846 29372 : FOR( i = 0; i < 3; i++ )
1847 : {
1848 22029 : voicingBufAvg_fx = add( voicingBufAvg_fx, mult_r( st_fx->voicing_fx[i], 10912 ) ); /*Q15 */
1849 : }
1850 : /* GainFrame *= max(min((float)(pitBufAvg/voicingBufAvg), 1.0f), 0.7f); */
1851 7343 : tmp = 32767;
1852 7343 : move16();
1853 7343 : IF( voicingBufAvg_fx > 0 )
1854 : {
1855 7342 : exp = norm_s( voicingBufAvg_fx );
1856 7342 : tmp = div_s( shl( 1, sub( 14, exp ) ), voicingBufAvg_fx ); /* (14-exp) */
1857 7342 : L_tmp = L_mult( pitBufAvg_fx, tmp ); /* (21-exp) */
1858 7342 : L_tmp = L_shl_o( L_tmp, add( exp, 10 ), &Overflow );
1859 7342 : tmp = round_fx_o( L_tmp, &Overflow ); /* Q15 */
1860 : }
1861 :
1862 7343 : tmp = s_max( s_min( tmp, 32767 ), 22938 ); /* Q15 */
1863 7343 : GainFrame = Mpy_32_16_1( GainFrame, tmp ); /* Q18 */
1864 :
1865 7343 : IF( LT_16( lsp_spacing_fx, 328 ) && lsp_spacing_fx )
1866 : {
1867 0 : GainFrame = Mpy_32_16_1( GainFrame, 21299 ); /* Q18 */
1868 : }
1869 :
1870 : /*0.25f*sum_f(voice_factors, NB_SUBFR)*/
1871 7343 : L_tmp = L_mult( voice_factors[0], 8192 );
1872 29372 : FOR( i = 1; i < NB_SUBFR; i++ )
1873 : {
1874 22029 : L_tmp = L_mac( L_tmp, voice_factors[i], 8192 );
1875 : }
1876 7343 : avg_voice_fac = round_fx( L_tmp );
1877 :
1878 7343 : test();
1879 7343 : test();
1880 7343 : IF( st_fx->igf != 0 && EQ_16( st_fx->coder_type, VOICED ) )
1881 : {
1882 : /*GainFrame *= 0.5f;*/
1883 0 : GainFrame = Mpy_32_16_1( GainFrame, 16384 );
1884 : }
1885 7343 : ELSE IF( st_fx->igf != 0 && GT_16( avg_voice_fac, 11469 ) ) /*Q15 -> 0.35f*/
1886 : {
1887 : /*GainFrame *= 0.75f;*/
1888 0 : GainFrame = Mpy_32_16_1( GainFrame, 24576 );
1889 : }
1890 :
1891 : /* Quantization of the frame gain parameter */
1892 7343 : IF( st_fx->rf_mode )
1893 : {
1894 0 : QuantizeSHBframegain_ivas_fx( st_fx, &GainFrame, st_fx->extl, st_fx->extl_brate, &st_fx->hRF->RF_bwe_gainFr_ind, 0 );
1895 : }
1896 : ELSE
1897 : {
1898 7343 : QuantizeSHBframegain_ivas_fx( st_fx, &GainFrame, st_fx->extl, st_fx->extl_brate, NULL, 0 );
1899 : }
1900 :
1901 : /* Adjust the subframe and frame gain of the synthesized SHB signal */
1902 : /* Scale the shaped excitation*/
1903 :
1904 7343 : scale_sig( shaped_wb_excitation, ( L_FRAME16k + L_SHB_LAHEAD ) / 4, -1 );
1905 7343 : scale_sig( hBWE_TD->syn_overlap_fx, L_SHB_LAHEAD, -1 );
1906 7343 : Q_bwe_exc_ext = sub( Q_bwe_exc_ext, 1 );
1907 7343 : ScaleShapedSHB_fx( SHB_OVERLAP_LEN / 2, shaped_wb_excitation, hBWE_TD->syn_overlap_fx, GainShape, GainFrame, window_wb_fx, subwin_wb_fx,
1908 : &Q_bwe_exc_ext, &dummy, dummy, dummy );
1909 :
1910 7343 : Scale_sig( hBWE_TD->syn_overlap_fx, L_SHB_LAHEAD, 1 );
1911 7343 : st_fx->prev_Q_bwe_exc = Q_bwe_exc;
1912 7343 : move16();
1913 :
1914 7343 : return;
1915 : }
1916 :
1917 :
1918 161212 : void fb_tbe_reset_enc_fx(
1919 : Word32 elliptic_bpf_2_48k_mem_fx[][4],
1920 : Word32 *prev_fb_energy_fx,
1921 : Word16 elliptic_bpf_2_48k_mem_fx_Q[],
1922 : Word16 *prev_fb_energy_fx_Q )
1923 : {
1924 161212 : set32_fx( elliptic_bpf_2_48k_mem_fx[0], 0, 4 );
1925 161212 : set32_fx( elliptic_bpf_2_48k_mem_fx[1], 0, 4 );
1926 161212 : set32_fx( elliptic_bpf_2_48k_mem_fx[2], 0, 4 );
1927 161212 : set32_fx( elliptic_bpf_2_48k_mem_fx[3], 0, 4 );
1928 161212 : *prev_fb_energy_fx = 0;
1929 161212 : move16();
1930 161212 : set16_fx( elliptic_bpf_2_48k_mem_fx_Q, 0, 4 );
1931 161212 : *prev_fb_energy_fx_Q = 0;
1932 161212 : move16();
1933 :
1934 161212 : return;
1935 : }
1936 :
1937 :
1938 : /*======================================================================================*/
1939 : /* FUNCTION : void swb_tbe_enc_fx () */
1940 : /*--------------------------------------------------------------------------------------*/
1941 : /* PURPOSE : SWB TBE encoder, 6 - 14 kHz (or 7.5 - 15.5 kHz) band encoding module */
1942 : /*--------------------------------------------------------------------------------------*/
1943 : /* INPUT ARGUMENTS : */
1944 : /* _(Word16) coder_type : coding type */
1945 : /* _(Word16*) shb_speech_fx : SHB target signal (6-14kHz) at 16kHz Q0 */
1946 : /* _(Word16*) bwe_exc_extended :bandwidth extended exciatation Q0 */
1947 : /* _(Word16[]) voice_factors :voicing factors Q15 */
1948 : /* _(Word16*) Q_white_exc :Q Format of White Exc */
1949 : /*--------------------------------------------------------------------------------------*/
1950 : /* OUTPUT ARGUMENTS : */
1951 : /* _(Word16[])White_exc16k_fx : shaped white excitation for the FB TBE Q_white_exc */
1952 : /* _(Word16*)fb_slope_fx : slope +ve (high freq > low freq), -ve or neutral Q12 */
1953 : /*--------------------------------------------------------------------------------------*/
1954 : /* INPUT/OUTPUT ARGUMENTS : */
1955 : /* _ Encoder_State *st_fx: : Encoder state structure */
1956 : /*--------------------------------------------------------------------------------------*/
1957 : /* RETURN ARGUMENTS : */
1958 : /* _ None */
1959 : /*--------------------------------------------------------------------------------------*/
1960 : /* CALLED FROM : TX */
1961 : /*======================================================================================*/
1962 :
1963 1350 : void swb_tbe_enc_fx(
1964 : Encoder_State *st_fx, /* i/o: encoder state structure */
1965 : const Word16 coder_type, /* i : coding type */
1966 : Word16 *shb_speech_fx, /* i : SHB target signal (6-14kHz) at 16kHz Q_shb*/
1967 : Word32 *bwe_exc_extended, /* i : bandwidth extended exciatation 2*Q_new*/
1968 : const Word16 voice_factors_fx[], /* i : voicing factors Q15*/
1969 : Word16 *White_exc16k_fx, /* o : shaped white excitation for the FB TBE Q_white_exc*/
1970 : Word16 *Q_white_exc,
1971 : Word16 Q_new,
1972 : Word16 Q_shb,
1973 : Word16 *voicing, /* i : OL maximum normalized correlation */
1974 : const Word16 pitch_buf[] /* i : subframe pitch Q6*/
1975 : )
1976 : {
1977 : Word16 i, j;
1978 :
1979 : Word16 shb_old_speech_fx[L_LOOK_16k + L_SUBFR16k + L_FRAME16k];
1980 : Word16 bwe_exc_extended_16[L_FRAME32k + NL_BUFF_OFFSET];
1981 :
1982 : Word16 shaped_shb_excitation_fx[L_FRAME16k + L_SHB_LAHEAD];
1983 : Word32 GainFrame_fx;
1984 : Word16 GainShape_fx[NUM_SHB_SUBFR];
1985 : Word16 lpc_shb_fx[LPC_SHB_ORDER + 1], lsp_shb_fx[LPC_SHB_ORDER], lsf_shb_fx[LPC_SHB_ORDER];
1986 : Word16 weights_lsp[LPC_SHB_ORDER];
1987 : Word16 Q_out;
1988 : Word16 *shb_frame_fx, *shb_new_speech_fx;
1989 : Word16 R_h[LPC_SHB_ORDER + 2]; /* Autocorrelations of windowed speech MSB */
1990 : Word16 R_l[LPC_SHB_ORDER + 2]; /* Autocorrelations of windowed speech LSB */
1991 : Word16 Q_R;
1992 : Word32 LepsP[LPC_SHB_ORDER + 1];
1993 :
1994 1350 : Word16 ana_align_delay[2] = { -L_SHB_LAHEAD - ( NL_BUFF_OFFSET / 2 ), -L_SHB_LAHEAD - ( NL_BUFF_OFFSET / 2 ) };
1995 1350 : move16();
1996 1350 : move16();
1997 : Word32 prev_pow_fx, curr_pow_fx, Lscale;
1998 : Word32 p2m_in_fx, p2m_out_fx;
1999 :
2000 : Word16 exp_out, exp, exp1, frac;
2001 : Word16 cnt, n_mem, n_mem2;
2002 : Word32 L_tmp, L_tmp1;
2003 : Word16 Q_bwe_exc;
2004 :
2005 : Word16 frGainAttenuate, frGainSmoothEn;
2006 : Word16 MA_lsp_shb_spacing;
2007 : Word16 temp_swb_fac, feedback;
2008 : Word32 L_feedback;
2009 : Word16 tmp, tmp1, tmp2;
2010 : Word32 Lmax;
2011 : Word16 sc;
2012 : Word16 lsf_shb_orig_fx[LPC_SHB_ORDER];
2013 : Word16 sd_uq_q_fx;
2014 : Word16 vf_modified_fx[NB_SUBFR16k];
2015 : Word16 pitBufAvg_fx;
2016 : Word16 voicingBufAvg_fx;
2017 : Word16 sum1, sum2;
2018 : Word16 recip, Q_recip;
2019 : const Word16 *ptr_lsp_interp_coef_fx;
2020 :
2021 : Word16 lsp_shb_1_fx[LPC_SHB_ORDER], lsp_shb_2_fx[LPC_SHB_ORDER], lsp_temp_fx[LPC_SHB_ORDER];
2022 : Word16 lpc_shb_sf_fx[4 * ( LPC_SHB_ORDER + 1 )];
2023 :
2024 : /*Word32 shb_ener_sf_fx_32[4];*/
2025 : Word32 shb_ener_sf_Q31;
2026 : Word16 shb_res_fx[L_FRAME16k];
2027 : Word16 shb_res_gshape_fx[NB_SUBFR16k];
2028 : Word32 shb_res_gshape_fx_32[NB_SUBFR16k];
2029 : Word16 vf_ind_fx;
2030 :
2031 : Word16 formant_fac_fx;
2032 : Word16 shaped_shb_excitationTemp_fx[L_FRAME16k];
2033 :
2034 : Word16 mean_vf;
2035 : Word16 lsf_diff[LPC_SHB_ORDER], w[LPC_SHB_ORDER];
2036 : Word16 refl[M];
2037 : Word16 tilt_para;
2038 : Word16 Q_bwe_exc_fb;
2039 : Word16 n_subfr_saturation;
2040 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
2041 1350 : Flag Overflow = 0;
2042 1350 : move16();
2043 : #endif
2044 1350 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
2045 1350 : RF_ENC_HANDLE hRF = st_fx->hRF;
2046 :
2047 : /* init and buffers set up */
2048 1350 : exp1 = 0; /* to avoid compilation warnings */
2049 1350 : set16_fx( shaped_shb_excitationTemp_fx, 0, L_FRAME16k );
2050 :
2051 : /* compensate for the delay in target generation and subframe LA */
2052 1350 : shb_frame_fx = shb_old_speech_fx + L_SUBFR16k + ana_align_delay[0];
2053 1350 : move16();
2054 :
2055 : /* set up the speech buffers for TBE processing*/
2056 1350 : shb_new_speech_fx = shb_old_speech_fx + ( L_LOOK_16k + L_SUBFR16k );
2057 1350 : move16();
2058 1350 : Copy( hBWE_TD->old_speech_shb_fx, shb_old_speech_fx, ( L_LOOK_16k + L_SUBFR16k ) );
2059 1350 : Copy( shb_speech_fx, shb_new_speech_fx, L_FRAME16k );
2060 1350 : Copy( shb_old_speech_fx + L_FRAME16k, hBWE_TD->old_speech_shb_fx, ( L_LOOK_16k + L_SUBFR16k ) );
2061 :
2062 : /* autocorrelation of SHB speech for 10-th order LP analysis */
2063 1350 : autocorr_fx( shb_old_speech_fx,
2064 : LPC_SHB_ORDER + 1,
2065 : R_h, /* autocorr (msb) Q15 */
2066 : R_l, /* autocorr (lsb) */
2067 : &Q_R,
2068 : NS2SA( INT_FS_16k, ACELP_LOOK_NS ) + L_SUBFR16k + L_FRAME16k,
2069 : win_lpc_shb_fx,
2070 : 0,
2071 : 1 );
2072 :
2073 :
2074 : /* Spectral smoothing of autocorrelation coefficients */
2075 1350 : test();
2076 1350 : IF( ( st_fx->rf_mode != 0 ) || EQ_32( st_fx->total_brate, ACELP_9k60 ) )
2077 : {
2078 0 : FOR( i = 1; i <= LPC_SHB_ORDER; i++ )
2079 : {
2080 0 : L_tmp = Mpy_32( R_h[i], R_l[i], wac_swb_h[i - 1], wac_swb_l[i - 1] );
2081 0 : L_Extract( L_tmp, &R_h[i], &R_l[i] );
2082 : }
2083 : }
2084 :
2085 : /* Set the autocorr[0] element to a non-negative value */
2086 1350 : R_l[0] = s_max( R_l[0], 1 );
2087 1350 : move16();
2088 :
2089 1350 : E_LPC_lev_dur( R_h, R_l, lpc_shb_fx, LepsP, LPC_SHB_ORDER, NULL ); /* LPC in Q14 */
2090 : {
2091 : Word16 enerG, lpc_shb1[M + 1];
2092 :
2093 : /* extend the lpc_shb to a 16th order gain calc */
2094 1350 : set16_fx( lpc_shb1, 0, M + 1 );
2095 1350 : Copy( lpc_shb_fx, lpc_shb1, LPC_SHB_ORDER + 1 );
2096 :
2097 : /* estimate the LP gain */
2098 1350 : enerG = Enr_1_Az_fx( lpc_shb1, 2 * L_SUBFR ); /* Q3 */
2099 :
2100 : /* if the LP gain is greater than a threshold, avoid saturation */
2101 1350 : IF( GT_16( enerG, 256 /*32 Q3*/ ) )
2102 : {
2103 1 : set16_fx( lpc_shb_fx, 0, LPC_SHB_ORDER + 1 );
2104 1 : E_LPC_lev_dur( R_h, R_l, lpc_shb_fx, LepsP, 2, NULL ); /* LPC in Q14 */
2105 : }
2106 : }
2107 :
2108 : /* this is needed as the E_LPC_lev_dur function outputs lpc in Q14 */
2109 1350 : Copy_Scale_sig( lpc_shb_fx, lpc_shb_fx, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_fx[0] ), 2 ) );
2110 :
2111 : /* Expand bandwidth of the LP coeffs */
2112 1350 : test();
2113 1350 : IF( ( st_fx->rf_mode != 0 ) || EQ_32( st_fx->total_brate, ACELP_9k60 ) )
2114 : {
2115 0 : FOR( i = 1; i <= LPC_SHB_ORDER; i++ )
2116 : {
2117 0 : lpc_shb_fx[i] = mult_r( lpc_shb_fx[i], lpc_weights_fx[i] );
2118 0 : move16();
2119 : }
2120 : }
2121 :
2122 : /* LPC to LSP conversion */
2123 : /* LPC: Q12, LSP: Q15 */
2124 1350 : E_LPC_a_lsp_conversion( lpc_shb_fx, lsp_shb_fx, hBWE_TD->prev_lsp_shb_fx, LPC_SHB_ORDER );
2125 :
2126 : /* LSP to LSF conversion */
2127 : /* LSP: Q15, LSF: Q15 */
2128 1350 : E_LPC_lsp_lsf_conversion( lsp_shb_fx, lsf_shb_fx, LPC_SHB_ORDER );
2129 :
2130 : /* Input signal filtering in case of tonal sounds in the high band
2131 : gain Frame smoothing and attenuation control */
2132 1350 : gainFrSmooth_En_fx( st_fx, shb_frame_fx, lpc_shb_fx, lsf_shb_fx, &MA_lsp_shb_spacing, &frGainAttenuate, &frGainSmoothEn );
2133 :
2134 1350 : Copy( lsp_shb_fx, hBWE_TD->prev_lsp_shb_fx, LPC_SHB_ORDER );
2135 1350 : Copy( lsf_shb_fx, lsf_shb_orig_fx, LPC_SHB_ORDER );
2136 :
2137 1350 : test();
2138 1350 : IF( ( EQ_16( st_fx->rf_mode, 1 ) ) || EQ_32( st_fx->total_brate, ACELP_9k60 ) )
2139 : {
2140 0 : lsp_weights_fx( lsf_shb_fx, weights_lsp, LPC_SHB_ORDER, &Q_out );
2141 :
2142 : /* to compensate for the 1.1* weighting done inside the function lsp_weights */
2143 : /*weights_lsp[3]*=0.909091f; weights_lsp[4]*=0.909091f; */
2144 0 : weights_lsp[3] = mult_r( weights_lsp[3], 29789 /*0.909091f Q15*/ );
2145 0 : move16();
2146 0 : weights_lsp[4] = mult_r( weights_lsp[4], 29789 /*0.909091f Q15*/ );
2147 0 : move16();
2148 :
2149 : /* 8-bit VQ, 10 dimension */
2150 0 : i = closest_centroid_fx( lsf_shb_fx, weights_lsp, swb_tbe_lsfvq_cbook_8b, 256, LPC_SHB_ORDER );
2151 0 : Copy( swb_tbe_lsfvq_cbook_8b + i * LPC_SHB_ORDER, lsf_shb_fx, LPC_SHB_ORDER );
2152 :
2153 0 : set16_fx( hBWE_TD->lsf_idx, 0, NUM_Q_LSF );
2154 0 : hBWE_TD->lsf_idx[0] = i;
2155 0 : move16();
2156 : }
2157 : ELSE
2158 : {
2159 : /* LSF quantization (21 bits) */
2160 1350 : Quant_BWE_LSF_fx( st_fx, lsf_shb_fx, lsf_shb_fx );
2161 : }
2162 :
2163 : /* space the lsfs to assert a minimum distance */
2164 1350 : space_lsfs_fx( lsf_shb_fx, LPC_SHB_ORDER );
2165 :
2166 : /* voice factor adjustment and gainframe attenuation factor */
2167 1350 : tmp = sub( lsf_shb_fx[0], lsf_shb_orig_fx[0] );
2168 1350 : L_tmp = L_mult( tmp, tmp );
2169 13500 : FOR( i = 1; i < LPC_SHB_ORDER; i++ )
2170 : {
2171 : /* Estimate the QD in lsfs between UQ and Q */
2172 12150 : tmp = sub( lsf_shb_fx[i], lsf_shb_orig_fx[i] );
2173 12150 : L_tmp = L_mac( L_tmp, tmp, tmp );
2174 : }
2175 1350 : sd_uq_q_fx = round_fx( L_tmp ); /* sd_uq_q_fx in Q15 */
2176 : /* voice factor modification to limit any spurious jumps in the middle of voiced subframes*/
2177 : /* mean(voice_factors_fx[i], 4); */
2178 :
2179 1350 : L_tmp = L_mult( voice_factors_fx[0], 8192 );
2180 1350 : L_tmp = L_mac( L_tmp, voice_factors_fx[1], 8192 );
2181 1350 : L_tmp = L_mac( L_tmp, voice_factors_fx[2], 8192 );
2182 1350 : mean_vf = mac_r( L_tmp, voice_factors_fx[3], 8192 );
2183 :
2184 1350 : Copy( voice_factors_fx, vf_modified_fx, NB_SUBFR16k );
2185 :
2186 1350 : test();
2187 1350 : IF( EQ_16( coder_type, VOICED ) || GT_16( mean_vf, 13107 /*0.4f Q15*/ ) )
2188 : {
2189 2016 : FOR( i = 1; i < NB_SUBFR; i++ )
2190 : {
2191 1512 : L_tmp = L_mult( voice_factors_fx[i], 26214 );
2192 1512 : vf_modified_fx[i] = mac_r( L_tmp, voice_factors_fx[i - 1], 6554 );
2193 1512 : move16();
2194 : }
2195 504 : IF( NE_16( st_fx->L_frame, L_FRAME ) )
2196 : {
2197 269 : L_tmp = L_mult( voice_factors_fx[4], 26214 );
2198 269 : vf_modified_fx[4] = mac_r( L_tmp, voice_factors_fx[3], 6554 );
2199 269 : move16();
2200 : }
2201 : }
2202 :
2203 : /* convert quantized LSFs to LSPs for interpolation */
2204 1350 : E_LPC_lsf_lsp_conversion( lsf_shb_fx, lsp_shb_2_fx, LPC_SHB_ORDER );
2205 :
2206 1350 : test();
2207 1350 : IF( EQ_16( st_fx->last_extl, SWB_TBE ) || EQ_16( st_fx->last_extl, FB_TBE ) )
2208 : {
2209 : /* SHB LSP values from prev. frame for interpolation */
2210 1302 : Copy( hBWE_TD->swb_lsp_prev_interp_fx, lsp_shb_1_fx, LPC_SHB_ORDER );
2211 : }
2212 : ELSE
2213 : {
2214 : /* Use current frame's LSPs; in effect no interpolation */
2215 48 : Copy( lsp_shb_2_fx, lsp_shb_1_fx, LPC_SHB_ORDER );
2216 : }
2217 :
2218 1350 : lsf_diff[0] = lsf_diff[LPC_SHB_ORDER - 1] = 16384; /*Q15*/
2219 1350 : move16();
2220 12150 : FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
2221 : {
2222 10800 : lsf_diff[i] = sub( lsf_shb_fx[i], lsf_shb_fx[i - 1] );
2223 10800 : move16();
2224 : }
2225 1350 : a2rc_fx( hBWE_TD->cur_sub_Aq_fx + 1, refl, M );
2226 :
2227 : /* LSP interpolation for 13.2 kbps and 16.4 kbps */
2228 : /* tilt_para = 6.6956f * (1.0f + refl[0]) * (1.0f + refl[0])
2229 : - 3.8714f * (1.0f + refl[0])
2230 : + 1.3041f; */
2231 1350 : tmp = add( 16384, shr( refl[0], 1 ) ); /*Q14*/
2232 1350 : tmp1 = mult( 27425 /*Q12*/, tmp ); /*Q11*/
2233 1350 : tmp1 = mult( tmp1, tmp );
2234 1350 : tmp2 = shr( mult( 31715, tmp ), 2 ); /* Q11 */
2235 1350 : tilt_para = add( sub( tmp1, tmp2 ), 1335 ); /*Q10*/
2236 :
2237 1350 : IF( NE_16( st_fx->last_extl, SWB_TBE ) )
2238 : {
2239 432 : FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
2240 : {
2241 384 : hBWE_TD->prev_lsf_diff_fx[i - 1] = mult( lsf_diff[i], 16384 ); /*Q15*/
2242 384 : move16();
2243 : }
2244 : }
2245 :
2246 1350 : IF( LE_32( st_fx->total_brate, ACELP_16k40 ) )
2247 : {
2248 746 : test();
2249 746 : test();
2250 746 : test();
2251 746 : test();
2252 746 : test();
2253 746 : IF( !( GT_16( hBWE_TD->prev_tilt_para_fx, 5120 ) && ( EQ_16( coder_type, TRANSITION ) || LT_16( tilt_para, 1024 ) ) ) &&
2254 : !( ( ( LT_16( hBWE_TD->prev_tilt_para_fx, 3072 ) && GE_16( hBWE_TD->prev_coder_type, VOICED ) ) ) && GT_16( tilt_para, 5120 ) ) )
2255 : {
2256 6552 : FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
2257 : {
2258 5824 : IF( LT_16( lsf_diff[i], hBWE_TD->prev_lsf_diff_fx[i - 1] ) )
2259 : {
2260 2537 : tmp = mult( 26214, lsf_diff[i] );
2261 2537 : tmp = div_s( tmp, hBWE_TD->prev_lsf_diff_fx[i - 1] );
2262 2537 : tmp = s_max( tmp, 16384 );
2263 2537 : w[i] = s_min( tmp, 32767 );
2264 2537 : move16();
2265 : }
2266 : ELSE
2267 : {
2268 3287 : tmp = mult( 26214, hBWE_TD->prev_lsf_diff_fx[i - 1] );
2269 3287 : tmp = div_s( tmp, lsf_diff[i] );
2270 3287 : tmp = s_max( tmp, 16384 );
2271 3287 : w[i] = s_min( tmp, 32767 );
2272 3287 : move16();
2273 : }
2274 : }
2275 728 : w[0] = w[1];
2276 728 : move16();
2277 728 : w[LPC_SHB_ORDER - 1] = w[LPC_SHB_ORDER - 2];
2278 728 : move16();
2279 :
2280 8008 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
2281 : {
2282 7280 : tmp1 = mult( lsp_shb_1_fx[i], sub( 32767, w[i] ) );
2283 7280 : tmp2 = mult( lsp_shb_2_fx[i], w[i] );
2284 7280 : lsp_temp_fx[i] = add( tmp1, tmp2 );
2285 7280 : move16();
2286 : }
2287 : }
2288 : ELSE
2289 : {
2290 18 : Copy( lsp_shb_2_fx, lsp_temp_fx, LPC_SHB_ORDER );
2291 : }
2292 : }
2293 :
2294 1350 : Copy( lsf_diff + 1, hBWE_TD->prev_lsf_diff_fx, LPC_SHB_ORDER - 2 );
2295 1350 : hBWE_TD->prev_tilt_para_fx = tilt_para;
2296 1350 : move16();
2297 :
2298 1350 : shb_ener_sf_Q31 = 0;
2299 1350 : move16();
2300 1350 : test();
2301 1350 : IF( EQ_32( st_fx->total_brate, ACELP_24k40 ) || EQ_32( st_fx->total_brate, ACELP_32k ) )
2302 : {
2303 : /* ---------- SHB LSP interpolation ---------- */
2304 604 : ptr_lsp_interp_coef_fx = interpol_frac_shb; /* Q15 */
2305 3020 : FOR( j = 0; j < 4; j++ )
2306 : {
2307 26576 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
2308 : {
2309 : /*lsp_temp_fx[i] = lsp_shb_1_fx[i]*(*ptr_lsp_interp_coef_fx) */
2310 : /* + lsp_shb_2_fx[i]*(*(ptr_lsp_interp_coef_fx+1)); */
2311 24160 : L_tmp = L_mult( lsp_shb_1_fx[i], ( *ptr_lsp_interp_coef_fx ) );
2312 24160 : lsp_temp_fx[i] = mac_r( L_tmp, lsp_shb_2_fx[i], ( *( ptr_lsp_interp_coef_fx + 1 ) ) );
2313 24160 : move16();
2314 : }
2315 2416 : ptr_lsp_interp_coef_fx += 2;
2316 :
2317 2416 : tmp = i_mult2( j, ( LPC_SHB_ORDER + 1 ) );
2318 : /* convert LSPs to LPC coefficients for SHB synthesis*/
2319 2416 : E_LPC_f_lsp_a_conversion( lsp_temp_fx, lpc_shb_sf_fx + tmp, LPC_SHB_ORDER );
2320 : /* Bring the LPCs to Q12 */
2321 2416 : Copy_Scale_sig( lpc_shb_sf_fx + tmp, lpc_shb_sf_fx + tmp, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_sf_fx[tmp] ), 2 ) );
2322 : }
2323 :
2324 : /* -------- Calculate the SHB Energy -------- */
2325 : /*shb_ener_sf[0] = 0.003125f * sum2_f( shb_frame + L_SHB_LAHEAD, 320 );*/
2326 193884 : FOR( i = L_SHB_LAHEAD; i < L_FRAME16k + L_SHB_LAHEAD; i++ )
2327 : {
2328 : /* shbEner = shbEner + in[i] * in[i] */
2329 193280 : shb_ener_sf_Q31 = L_mac0_sat( shb_ener_sf_Q31, shb_frame_fx[i], shb_frame_fx[i] );
2330 : /* o: shb_ener_sf_Q31 in (2*Q_shb) */
2331 : }
2332 604 : shb_ener_sf_Q31 = Mult_32_16( shb_ener_sf_Q31, 102 /*0.003125f Q15*/ );
2333 604 : shb_ener_sf_Q31 = L_add( 1l /*1 Q0*/, shb_ener_sf_Q31 );
2334 604 : Quant_shb_ener_sf_fx( st_fx, &shb_ener_sf_Q31, ( 2 * Q_shb ) );
2335 :
2336 : /* -------- calculate the residuals using the FOUR subframe LPCs -------- */
2337 604 : set16_fx( shb_res_fx, 0, L_FRAME16k );
2338 604 : Residu3_10_fx( lpc_shb_sf_fx, shb_frame_fx + L_SHB_LAHEAD, shb_res_fx, 80, 0 );
2339 604 : Residu3_10_fx( lpc_shb_sf_fx + ( LPC_SHB_ORDER + 1 ), shb_frame_fx + L_SHB_LAHEAD + 80, shb_res_fx + 80, 80, 0 );
2340 604 : Residu3_10_fx( lpc_shb_sf_fx + 2 * ( LPC_SHB_ORDER + 1 ), shb_frame_fx + L_SHB_LAHEAD + 160, shb_res_fx + 160, 80, 0 );
2341 604 : Residu3_10_fx( lpc_shb_sf_fx + 3 * ( LPC_SHB_ORDER + 1 ), shb_frame_fx + L_SHB_LAHEAD + 240, shb_res_fx + 240, 80, 0 );
2342 : /* i: shb_frame_fx in Q_shb */
2343 : /* o: shb_res_fx in Q_shb */
2344 :
2345 604 : set32_fx( shb_res_gshape_fx_32, 0, NB_SUBFR16k );
2346 3624 : FOR( i = 0; i < NB_SUBFR16k; i++ )
2347 : {
2348 3020 : shb_res_gshape_fx_32[i] = sum2_fx( shb_res_fx + i * 64, 64 );
2349 3020 : move32();
2350 : }
2351 : /* o: shb_res_gshape_fx_32 in (2*Q_shb+1) */
2352 :
2353 604 : maximum_32_fx( shb_res_gshape_fx_32, NB_SUBFR16k, &L_tmp );
2354 :
2355 : /* root_a_over_b_fx(shb_res_gshape_fx_32[i], (2*Q_shb+1), L_tmp, (2*Q_shb+1), &exp);*/
2356 : /* First, find 1/L_tmp, L_tmp in QA = (2*Q_shb+1) */
2357 :
2358 : /* case when den = 0 */
2359 604 : recip = 0; /*instead of 32767 to be compatible with previous root_a_over_b_fx() output */
2360 604 : Q_recip = 0;
2361 :
2362 604 : IF( L_tmp )
2363 : {
2364 604 : exp = norm_l( L_tmp );
2365 604 : tmp = extract_h( L_shl( L_tmp, exp ) );
2366 604 : recip = div_s( 16384, tmp );
2367 : // Q_recip = 31 - ( exp - 14 ); /* = 31-(exp+2*Q_shb+1-14), but adjusted by (2*Q_shb+1) for use at Mult_32_16 below */
2368 604 : Q_recip = sub( 31 + 14, exp ); /* = 31-(exp+2*Q_shb+1-14), but adjusted by (2*Q_shb+1) for use at Mult_32_16 below */
2369 604 : move16();
2370 : }
2371 :
2372 3624 : FOR( i = 0; i < NB_SUBFR16k; i++ )
2373 : {
2374 3020 : L_tmp1 = Mult_32_16( shb_res_gshape_fx_32[i], recip ); /*Q = Q_recip+1-16*/
2375 3020 : L_tmp = root_a_fx( L_tmp1, add( Q_recip, 1 - 16 ), &exp );
2376 3020 : shb_res_gshape_fx[i] = round_fx( L_shl( L_tmp, sub( exp, 1 ) ) ); /* Q14 */
2377 3020 : move16();
2378 : }
2379 :
2380 604 : Quant_shb_res_gshape_fx( st_fx, shb_res_gshape_fx );
2381 : }
2382 :
2383 : /* for 13.2 and 16.4kbps */
2384 1350 : E_LPC_f_lsp_a_conversion( lsp_temp_fx, lpc_shb_fx, LPC_SHB_ORDER );
2385 1350 : Copy_Scale_sig( lpc_shb_fx, lpc_shb_fx, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_fx[0] ), 2 ) ); /* Q12 */
2386 :
2387 : /* Save the SWB LSP values from current frame for interpolation */
2388 1350 : Copy( lsp_shb_2_fx, hBWE_TD->swb_lsp_prev_interp_fx, LPC_SHB_ORDER ); /* lsp_shb_2_fx in Q15 */
2389 :
2390 : /* -------- start of memory rescaling -------- */
2391 : /* ----- calculate optimum Q_bwe_exc and rescale memories accordingly ----- */
2392 1350 : Lmax = L_deposit_l( 0 );
2393 865350 : FOR( cnt = 0; cnt < L_FRAME32k; cnt++ )
2394 : {
2395 864000 : Lmax = L_max( Lmax, L_abs( bwe_exc_extended[cnt] ) );
2396 : }
2397 1350 : Q_bwe_exc = norm_l( Lmax );
2398 1350 : if ( Lmax == 0 )
2399 : {
2400 0 : Q_bwe_exc = 31;
2401 0 : move16();
2402 : }
2403 1350 : Q_bwe_exc = add( Q_bwe_exc, add( Q_new, Q_new ) );
2404 :
2405 : /* Account for any outliers in the memories from previous frame for rescaling to avoid saturation */
2406 1350 : find_max_mem_enc( st_fx, &n_mem, &n_mem2 );
2407 :
2408 1350 : tmp2 = add( st_fx->prev_Q_bwe_exc, n_mem );
2409 1350 : if ( GT_16( Q_bwe_exc, tmp2 ) )
2410 : {
2411 70 : Q_bwe_exc = tmp2;
2412 70 : move16();
2413 : }
2414 :
2415 : /* rescale the memories if Q_bwe_exc is different from previous frame */
2416 1350 : sc = sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc );
2417 1350 : IF( sc != 0 )
2418 : {
2419 675 : rescale_genSHB_mem_enc( st_fx, sc );
2420 : }
2421 :
2422 : /* rescale the bwe_exc_extended and bring it to 16-bit single precision with dynamic norm */
2423 1350 : Copy( hBWE_TD->old_bwe_exc_extended_fx, bwe_exc_extended_16, NL_BUFF_OFFSET );
2424 1350 : sc = sub( Q_bwe_exc, add( Q_new, Q_new ) );
2425 :
2426 865350 : FOR( cnt = 0; cnt < L_FRAME32k; cnt++ )
2427 : {
2428 864000 : bwe_exc_extended_16[cnt + NL_BUFF_OFFSET] = round_fx_sat( L_shl_sat( bwe_exc_extended[cnt], sc ) );
2429 864000 : move16();
2430 : }
2431 1350 : Copy( bwe_exc_extended_16 + L_FRAME32k, hBWE_TD->old_bwe_exc_extended_fx, NL_BUFF_OFFSET );
2432 :
2433 : /* state_syn_shbexc_fx is kept at (st_fx->prev_Q_bwe_syn) for 24.4/32kbps or is kept at Q_bwe_exc for 13.2/16.4kbps */
2434 1350 : Copy( hBWE_TD->state_syn_shbexc_fx, shaped_shb_excitation_fx, L_SHB_LAHEAD );
2435 :
2436 : /* save the previous Q factor of the buffer */
2437 1350 : st_fx->prev_Q_bwe_exc = Q_bwe_exc;
2438 1350 : move16();
2439 :
2440 1350 : Q_bwe_exc = sub( Q_bwe_exc, 16 ); /* Q_bwe_exc reflecting the single precision dynamic norm-ed buffers from here */
2441 :
2442 : /* -------- end of rescaling memories -------- */
2443 1350 : Q_bwe_exc_fb = hBWE_TD->prev_Q_bwe_exc_fb;
2444 1350 : move16();
2445 : /* Determine formant PF strength */
2446 1350 : formant_fac_fx = swb_formant_fac_fx( lpc_shb_fx[1], &hBWE_TD->tilt_mem_fx );
2447 : /* i:lpc_shb_fx Q12, o:formant_fac_fx Q15 */
2448 :
2449 : /* Calculate the 6 to 14 kHz (or 7.5 - 15.5 kHz) SHB excitation signal from the ACELP core excitation */
2450 1350 : vf_ind_fx = 20;
2451 1350 : move16();
2452 :
2453 1350 : GenShapedSHBExcitation_fx( shaped_shb_excitation_fx + L_SHB_LAHEAD, lpc_shb_fx, White_exc16k_fx,
2454 1350 : hBWE_TD->mem_csfilt_fx, hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->state_lpc_syn_fx,
2455 1350 : coder_type, bwe_exc_extended_16, hBWE_TD->bwe_seed, vf_modified_fx, st_fx->extl,
2456 : &( hBWE_TD->tbe_demph_fx ), &( hBWE_TD->tbe_premph_fx ), lpc_shb_sf_fx, shb_ener_sf_Q31,
2457 1350 : shb_res_gshape_fx, shb_res_fx, &vf_ind_fx, formant_fac_fx, hBWE_TD->fb_state_lpc_syn_fx,
2458 1350 : &( hBWE_TD->fb_tbe_demph_fx ), &Q_bwe_exc, &Q_bwe_exc_fb, Q_shb, n_mem2, st_fx->prev_Q_bwe_syn, st_fx->total_brate, 0 );
2459 :
2460 1350 : *Q_white_exc = Q_bwe_exc_fb;
2461 1350 : move16();
2462 1350 : IF( EQ_16( st_fx->extl, FB_TBE ) )
2463 : {
2464 0 : hBWE_TD->prev_Q_bwe_exc_fb = Q_bwe_exc_fb;
2465 0 : move16();
2466 : }
2467 : ELSE
2468 : {
2469 : /*Indirectly a memory reset of FB memories for next frame such that rescaling of memories would lead to 0 due to such high prev. Q value.
2470 : 51 because of 31 + 20(shift of Q_bwe_exc_fb before de-emphasis)*/
2471 1350 : hBWE_TD->prev_Q_bwe_exc_fb = 51;
2472 1350 : move16();
2473 : }
2474 :
2475 1350 : test();
2476 1350 : IF( EQ_32( st_fx->total_brate, ACELP_24k40 ) || EQ_32( st_fx->total_brate, ACELP_32k ) )
2477 : {
2478 604 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
2479 : {
2480 604 : hBWE_TD->idx_mixFac = vf_ind_fx;
2481 604 : move16();
2482 : }
2483 : ELSE
2484 : {
2485 0 : push_indice( st_fx->hBstr, IND_SHB_VF, vf_ind_fx, NUM_BITS_SHB_VF );
2486 : }
2487 : }
2488 :
2489 14850 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
2490 : {
2491 13500 : hBWE_TD->mem_stp_swb_fx[i] = shl( hBWE_TD->mem_stp_swb_fx[i], sub( Q_bwe_exc, st_fx->prev_Q_bwe_syn ) );
2492 13500 : move16();
2493 : }
2494 :
2495 6750 : FOR( i = 0; i < L_FRAME16k; i += L_SUBFR16k )
2496 : {
2497 5400 : PostShortTerm_fx( &shaped_shb_excitation_fx[L_SHB_LAHEAD + i], lpc_shb_fx, &shaped_shb_excitationTemp_fx[i], hBWE_TD->mem_stp_swb_fx,
2498 5400 : hBWE_TD->ptr_mem_stp_swb_fx, &( hBWE_TD->gain_prec_swb_fx ), hBWE_TD->mem_zero_swb_fx, formant_fac_fx );
2499 : /* i: shaped_shb_excitation_fx in Q_bwe_exc */
2500 : /* i: lpc_shb_fx in Q12 */
2501 : }
2502 1350 : Copy( shaped_shb_excitationTemp_fx, &shaped_shb_excitation_fx[L_SHB_LAHEAD], L_FRAME16k );
2503 :
2504 :
2505 1350 : tmp = sub( shl( Q_bwe_exc, 1 ), 31 );
2506 1350 : prev_pow_fx = L_shl( 21475l /*0.00001f Q31*/, tmp ); /* 2*(Q_bwe_exc) */
2507 1350 : curr_pow_fx = L_shl( 21475l /*0.00001f Q31*/, tmp ); /* 2*(Q_bwe_exc) */
2508 41850 : FOR( i = 0; i < L_SHB_LAHEAD + 10; i++ )
2509 : {
2510 40500 : prev_pow_fx = L_mac0_o( prev_pow_fx, shaped_shb_excitation_fx[i], shaped_shb_excitation_fx[i], &Overflow ); /* 2*Q_bwe_exc */
2511 40500 : curr_pow_fx = L_mac0_o( curr_pow_fx, shaped_shb_excitation_fx[i + L_SHB_LAHEAD + 10], shaped_shb_excitation_fx[i + L_SHB_LAHEAD + 10], &Overflow ); /* 2*Q_bwe_exc */
2512 : }
2513 :
2514 1350 : if ( GT_16( voice_factors_fx[0], 24576 /*0.75f Q15*/ ) )
2515 : {
2516 : /*curr_pow_fx = Mult_32_16( curr_pow_fx, 8192);*/ /* Q(2*Q_bwe_exc) */
2517 46 : curr_pow_fx = L_shr( curr_pow_fx, 2 ); /* scale by 0.25 */
2518 : }
2519 :
2520 1350 : Lscale = root_a_over_b_fx( curr_pow_fx,
2521 1350 : shl( Q_bwe_exc, 1 ),
2522 : prev_pow_fx,
2523 1350 : shl( Q_bwe_exc, 1 ),
2524 : &exp );
2525 28350 : FOR( i = 0; i < L_SHB_LAHEAD; i++ )
2526 : {
2527 27000 : L_tmp = Mult_32_16( Lscale, shaped_shb_excitation_fx[i] ); /* Q(16-exp+Q_bwe_exc) */
2528 27000 : shaped_shb_excitation_fx[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */
2529 27000 : move16();
2530 : }
2531 1350 : IF( exp < 0 )
2532 : {
2533 142 : Lscale = L_shl( Lscale, exp );
2534 142 : exp = 0;
2535 142 : move16();
2536 : }
2537 14850 : FOR( ; i < L_SHB_LAHEAD + 10; i++ )
2538 : {
2539 13500 : tmp = i_mult_o( sub( i, 19 ), 3277 /*0.1f Q15*/, &Overflow ); /* Q15 */
2540 13500 : L_tmp1 = Mult_32_16( L_shl_o( 1, sub( 31, exp ), &Overflow ), tmp ); /* Q31-exp */
2541 13500 : tmp = sub( 32767 /*1.0f Q15*/, tmp );
2542 13500 : Lscale = L_add( Mult_32_16( Lscale, tmp ), L_tmp1 );
2543 13500 : L_tmp = Mult_32_16( Lscale, shaped_shb_excitation_fx[i] ); /* Q_bwe_exc + (31-exp) - 15 */
2544 13500 : shaped_shb_excitation_fx[i] = round_fx_o( L_shl_o( L_tmp, exp, &Overflow ), &Overflow ); /* Q_bwe_exc */
2545 13500 : move16();
2546 : }
2547 :
2548 : /* Update SHB excitation */
2549 1350 : Copy( shaped_shb_excitation_fx + L_FRAME16k, hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD ); /* Q_bwe_exc */
2550 :
2551 : /* Estimate the gain-shape parameter */
2552 1350 : n_subfr_saturation = 0;
2553 1350 : move16();
2554 1350 : EstimateSHBGainShape_fx( SHB_OVERLAP_LEN, shb_frame_fx, Q_shb, shaped_shb_excitation_fx,
2555 : Q_bwe_exc, GainShape_fx, subwin_shb_fx, &n_subfr_saturation );
2556 :
2557 : /* Gain shape BWS/high band low energy fix */
2558 1350 : IF( LT_16( hBWE_TD->cldfbHBLT, 8192 /*1.0f Q13*/ ) ) /* cldfbHBLT in Q13 */
2559 : {
2560 : /* There is not much HB past 10kHz; the temporal resolution is quite coarse, so reduce the dynamic range */
2561 85 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
2562 : {
2563 : /* 0.35f +/- delta variation; delta = 0.1*(GS-0.35)*/
2564 : /* GainShape[i] = 0.315f + 0.1f * GainShape[i]; */
2565 68 : GainShape_fx[i] = mac_r( 676457349l /*0.315 Q31*/, 3277 /*0.1 Q15*/, GainShape_fx[i] );
2566 68 : move16();
2567 : }
2568 : }
2569 :
2570 : /* Gain frame adjustment factor */
2571 : /* log( (GainShape[0]) / (st->prev_wb_GainShape) )*/
2572 1350 : test();
2573 1350 : IF( GainShape_fx[0] && hBWE_TD->prev_swb_GainShape_fx )
2574 : {
2575 1348 : exp = norm_s( hBWE_TD->prev_swb_GainShape_fx );
2576 1348 : tmp = div_s( shl( 1, sub( 14, exp ) ), hBWE_TD->prev_swb_GainShape_fx );
2577 1348 : L_tmp = L_mult( GainShape_fx[0], tmp ); /*Q(30 - exp) */
2578 :
2579 1348 : exp1 = norm_l( L_tmp );
2580 1348 : frac = Log2_norm_lc( L_shl( L_tmp, exp1 ) ); /*move16(); */
2581 1348 : exp1 = sub( exp, exp1 ); /*move16(); */
2582 1348 : L_tmp = Mpy_32_16( exp1, frac, 22713 );
2583 1348 : temp_swb_fac = round_fx( L_shl( L_tmp, 10 ) );
2584 : }
2585 : ELSE
2586 : {
2587 2 : temp_swb_fac = 0;
2588 2 : move16();
2589 : }
2590 1350 : L_feedback = L_mult0( temp_swb_fac, temp_swb_fac );
2591 :
2592 :
2593 5400 : FOR( i = 1; i < NUM_SHB_SUBGAINS; i++ )
2594 : {
2595 4050 : test();
2596 4050 : IF( GainShape_fx[i] && GainShape_fx[i - 1] )
2597 : {
2598 4050 : exp = norm_s( GainShape_fx[i - 1] );
2599 4050 : tmp = div_s( shl( 1, sub( 14, exp ) ), GainShape_fx[i - 1] );
2600 4050 : L_tmp = L_mult( GainShape_fx[i], tmp ); /* Q(30 - exp) */
2601 :
2602 4050 : exp1 = norm_l( L_tmp );
2603 4050 : frac = Log2_norm_lc( L_shl( L_tmp, exp1 ) );
2604 4050 : exp1 = sub( exp, exp1 );
2605 4050 : L_tmp = Mpy_32_16( exp1, frac, 22713 );
2606 4050 : temp_swb_fac = round_fx( L_shl( L_tmp, 10 ) );
2607 : }
2608 : ELSE
2609 : {
2610 0 : temp_swb_fac = 0;
2611 0 : move16();
2612 : }
2613 :
2614 4050 : L_feedback = L_mac( L_feedback, temp_swb_fac, temp_swb_fac );
2615 : }
2616 :
2617 : /* feedback = 0.4f / (1 + 0.5f * feedback) */
2618 1350 : L_tmp = L_add( L_shr( L_feedback, 1 ), L_shl( 1, 21 ) ); /* Q21 */
2619 1350 : IF( L_tmp != 0 )
2620 : {
2621 1350 : exp = norm_l( L_tmp );
2622 1350 : tmp = extract_h( L_shl( L_tmp, exp ) );
2623 1350 : exp = sub( sub( 30, exp ), 21 );
2624 1350 : tmp = div_s( 16384, tmp ); /*Q(15+exp) */
2625 1350 : L_tmp = L_shr( L_mult( 13107, tmp ), exp ); /*Q31 */
2626 1350 : feedback = round_fx( L_tmp ); /*Q15 */
2627 : }
2628 : ELSE
2629 : {
2630 0 : feedback = 8738;
2631 0 : move16(); /*Q15 */
2632 : }
2633 :
2634 : /* voicing in Q15 */
2635 1350 : L_tmp = L_mult( voicing[0], 8192 );
2636 1350 : L_tmp = L_mac( L_tmp, voicing[1], 8192 );
2637 1350 : L_tmp = L_mac( L_tmp, voicing[2], 8192 );
2638 1350 : tmp = sum1 = round_fx( L_tmp ); /* sum1 used again below - Q13 */
2639 1350 : tmp = add( tmp, 1 ); /* To ensure change is BE */
2640 :
2641 : /* voice_factors_fx in Q15 */
2642 1350 : L_tmp = L_mult( voice_factors_fx[0], 8192 );
2643 1350 : L_tmp = L_mac( L_tmp, voice_factors_fx[1], 8192 );
2644 1350 : L_tmp = L_mac( L_tmp, voice_factors_fx[2], 8192 );
2645 1350 : L_tmp = L_mac( L_tmp, voice_factors_fx[3], 8192 );
2646 1350 : tmp1 = sum2 = round_fx( L_tmp ); /* sum2 used again below - Q13 */
2647 :
2648 :
2649 1350 : test();
2650 1350 : test();
2651 1350 : IF( EQ_16( frGainAttenuate, 1 ) || ( GT_16( tmp, 19661 ) && GT_16( tmp1, 6554 ) ) )
2652 :
2653 : {
2654 537 : temp_swb_fac = hBWE_TD->prev_swb_GainShape_fx;
2655 537 : move16();
2656 2685 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
2657 : {
2658 : /*GainShape_fx[i] = (1 - feedback) * GainShape[i] + feedback * temp_swb_fac; */
2659 2148 : GainShape_fx[i] = round_fx( L_mac( L_mult( sub( 32767, feedback ), GainShape_fx[i] ), feedback, temp_swb_fac ) );
2660 2148 : move16();
2661 2148 : temp_swb_fac = GainShape_fx[i];
2662 2148 : move16();
2663 : }
2664 : }
2665 1350 : hBWE_TD->prev_swb_GainShape_fx = GainShape_fx[3];
2666 1350 : move16();
2667 :
2668 : /* Compute the power of gains away from the peak gain prior to quantization */
2669 1350 : p2m_in_fx = pow_off_pk_fx( GainShape_fx, NUM_SHB_SUBGAINS, 1 );
2670 :
2671 : /* Quantization of the gain shape parameter */
2672 :
2673 1350 : QuantizeSHBsubgains_fx( st_fx, GainShape_fx, st_fx->extl );
2674 : /* o: GainShape_fx in Q15 */
2675 : /* Compute the power of gains away from the peak gain after quantization */
2676 1350 : p2m_out_fx = pow_off_pk_fx( GainShape_fx, NUM_SHB_SUBFR, 4 );
2677 :
2678 : /* Estimate the gain parameter */
2679 1350 : EstimateSHBFrameGain_fx( SHB_OVERLAP_LEN, shb_frame_fx, Q_shb, shaped_shb_excitation_fx, Q_bwe_exc,
2680 : GainShape_fx, &GainFrame_fx, window_shb_fx, subwin_shb_fx, n_subfr_saturation );
2681 :
2682 1350 : IF( EQ_16( st_fx->tec_tfa, 1 ) )
2683 : {
2684 604 : tfaCalcEnv_fx( shb_frame_fx, st_fx->tfa_enr );
2685 : }
2686 :
2687 : /* If there's a big difference in the power of gains away from the peak gain */
2688 : /* due to poor quantization then suppress energy of the high band. */
2689 1350 : IF( GT_32( p2m_out_fx, L_shl( p2m_in_fx, 1 ) ) )
2690 : {
2691 124 : L_tmp = root_a_over_b_fx( p2m_in_fx, 28, p2m_out_fx, 29, &exp_out );
2692 124 : GainFrame_fx = L_shl( Mult_32_32( GainFrame_fx, L_tmp ), exp_out ); /* Q18 */
2693 : }
2694 1350 : test();
2695 1350 : IF( EQ_16( frGainSmoothEn, 1 ) && LT_32( hBWE_TD->prev_gainFr_SHB_fx, GainFrame_fx ) )
2696 : {
2697 0 : GainFrame_fx = L_add( L_shr( hBWE_TD->prev_gainFr_SHB_fx, 1 ), L_shr( GainFrame_fx, 1 ) ); /* Q18 */
2698 : }
2699 :
2700 1350 : test();
2701 1350 : test();
2702 1350 : IF( EQ_16( frGainAttenuate, 1 ) && LE_16( MA_lsp_shb_spacing, 79 /*0.0024f Q15*/ ) )
2703 : {
2704 0 : exp1 = norm_l( GainFrame_fx );
2705 0 : frac = Log2_norm_lc( L_shl( GainFrame_fx, exp1 ) );
2706 0 : exp = sub( 30, add( exp1, 21 ) );
2707 0 : L_tmp = Mpy_32_16( exp, frac, 26214 ); /* Q16 */
2708 0 : frac = L_Extract_lc( L_tmp, &exp );
2709 0 : GainFrame_fx = Pow2( 30, frac );
2710 0 : exp = sub( exp, 30 );
2711 0 : GainFrame_fx = L_shl( GainFrame_fx, add( exp, 18 ) ); /* Q18 */
2712 : }
2713 1350 : ELSE IF( EQ_16( hBWE_TD->prev_frGainAtten, 1 ) && GT_32( Mult_32_16( GainFrame_fx, 10923 ), hBWE_TD->prev_gainFr_SHB_fx ) )
2714 : {
2715 : /*GainFrame *= (0.8f + 0.5f*feedback); */
2716 0 : tmp = add( 26214, mult_r( feedback, 16384 ) );
2717 0 : GainFrame_fx = Mult_32_16( GainFrame_fx, tmp ); /* Q18 */
2718 : }
2719 1350 : hBWE_TD->prev_frGainAtten = frGainAttenuate;
2720 1350 : move16();
2721 1350 : hBWE_TD->prev_gainFr_SHB_fx = GainFrame_fx;
2722 1350 : move16(); /* Q18 */
2723 :
2724 :
2725 : /* Gain attenuation when the SWB LSF quantization error is larger than a threshold */
2726 1350 : tmp = mult_r( 25600 /*400 Q6*/, sd_uq_q_fx ); /* Q6 * Q15 => Q6 */
2727 1350 : IF( EQ_16( st_fx->L_frame, L_FRAME ) )
2728 : {
2729 746 : tmp1 = mult_r( 6554 /*0.2f Q15*/, tmp ); /* Q15, Q6 => Q6 */
2730 746 : L_tmp = L_msu( 8192l /*1.0f Q13*/, tmp1, tmp ); /* Q13 */
2731 : }
2732 : ELSE
2733 : {
2734 604 : tmp1 = mult_r( 3277 /*0.1f Q15*/, tmp ); /* Q15, Q6 => Q6 */
2735 604 : L_tmp = L_msu( 8192l /*1.0f Q13*/, tmp1, tmp ); /* Q13 */
2736 : }
2737 : /* sd_uq_q = max(min(sd_uq_q, 1.0f), 0.5f); */
2738 1350 : L_tmp = L_min( L_tmp, 8192l /*1.0f Q13*/ );
2739 1350 : L_tmp = L_max( L_tmp, 4096l /*0.5f Q13*/ ); /* Q13 */
2740 : /* keep the L_tmp; dont overwrite */
2741 :
2742 : /* pitBufAvg = 0.0025f * sum_f(pitch_buf, 4); */
2743 : /* pitch_buf: Q6 */
2744 1350 : pitBufAvg_fx = mult_r( pitch_buf[0], 1311 );
2745 1350 : pitBufAvg_fx = add( pitBufAvg_fx, mult_r( pitch_buf[1], 1311 ) );
2746 1350 : pitBufAvg_fx = add( pitBufAvg_fx, mult_r( pitch_buf[2], 1311 ) );
2747 1350 : pitBufAvg_fx = add( pitBufAvg_fx, mult_r( pitch_buf[3], 1311 ) ); /* Q10 */
2748 :
2749 : /* voicingBufAvg = (sum_f(voice_factors, 4)=sum2 > 0.6f) ? 0.333f : 0.1667f; */
2750 1350 : tmp2 = 5462 /*0.1667f Q15*/;
2751 1350 : move16();
2752 1350 : if ( GT_16( sum2, 4915 /*0.6f Q13*/ ) )
2753 : {
2754 850 : tmp2 = 10912 /*0.333f Q15*/; /* Q15 */
2755 850 : move16();
2756 : }
2757 1350 : voicingBufAvg_fx = shl( mult( tmp2, sum1 ), 2 ); /* Q15 */
2758 :
2759 :
2760 : /* max(min((float)(sd_uq_q*pitBufAvg/voicingBufAvg), 1.0f), 0.6f) */
2761 : /* sd_uq_q: Q13, pitBufAvg_fx: Q6, voicingBufAvg_fx: Q15 */
2762 :
2763 : /* 1/voicingBufAvg_fx */
2764 1350 : tmp = 32767;
2765 1350 : move16();
2766 1350 : IF( voicingBufAvg_fx > 0 )
2767 : {
2768 1350 : exp = norm_s( voicingBufAvg_fx );
2769 1350 : tmp = div_s( shl( 1, sub( 14, exp ) ), voicingBufAvg_fx ); /* (14-exp) */
2770 :
2771 : /* sd_uq_q*pitBufAvg */
2772 1350 : L_tmp = Mult_32_16( L_tmp, pitBufAvg_fx ); /* Q13 * Q10 + 1 -16 => Q8 */
2773 1350 : L_tmp = Mult_32_16( L_tmp, tmp ); /* Q8 + (14 - exp) - 15 => Q7 - exp */
2774 1350 : tmp = round_fx_o( L_shl_o( L_tmp, 31 - ( 7 - exp ), &Overflow ), &Overflow ); /* Q15 */
2775 : }
2776 :
2777 1350 : tmp = s_min( tmp, 32767 /*1.0f Q15*/ );
2778 1350 : tmp = s_max( tmp, 19661 /*0.6f Q15*/ );
2779 1350 : GainFrame_fx = Mult_32_16( GainFrame_fx, tmp ); /* Q18 + Q15 + 1 - 16 : Q18 */
2780 :
2781 1350 : test();
2782 1350 : IF( EQ_16( st_fx->L_frame, L_FRAME16k ) || EQ_16( st_fx->rf_mode, 1 ) )
2783 : {
2784 : /* Compensate for energy increase mismatch due to memory-less synthesis*/
2785 604 : GainFrame_fx = Mult_32_16( GainFrame_fx, 27853 /*0.85f Q15*/ ); /* Q18 */
2786 : }
2787 :
2788 : /* Quantization of the frame gain parameter */
2789 1350 : QuantizeSHBframegain_fx( st_fx, &GainFrame_fx, st_fx->extl, 0, &hRF->RF_bwe_gainFr_ind );
2790 :
2791 : /* Adjust the subframe and frame gain of the synthesized SHB signal */
2792 : /* Scale the shaped excitation */
2793 1350 : IF( EQ_32( st_fx->extl, FB_TBE ) )
2794 : {
2795 0 : tmp = norm_l( GainFrame_fx );
2796 0 : if ( GainFrame_fx == 0 )
2797 : {
2798 0 : tmp = 31;
2799 0 : move16();
2800 : }
2801 0 : L_tmp = L_shl( GainFrame_fx, tmp ); /* 18 + tmp */
2802 :
2803 0 : tmp1 = 0;
2804 0 : move16();
2805 0 : FOR( i = 0; i < L_FRAME16k; i++ )
2806 : {
2807 0 : L_tmp1 = Mult_32_16( L_tmp, GainShape_fx[NUM_SHB_SUBFR * i / L_FRAME16k] ); /* Q : 18 + tmp +15 -15*/
2808 : /*White_exc16k_fx[i] = round_fx( L_shl(Mult_32_16( L_tmp1, White_exc16k_fx[i]),(23 - tmp -*Q_white_exc)) );*/
2809 : /*18 + tmp +*Q_white_exc -15 + 23 - tmp -*Q_white_exc -16 = 10*/
2810 0 : White_exc16k_fx[i] = round_fx( Mult_32_16( L_tmp1, White_exc16k_fx[i] ) ); /* 18 + tmp +*Q_white_exc -15 -16 */
2811 0 : move16();
2812 : /*Lmax = L_max(Lmax,White_exc16k_fx[i]);*/
2813 0 : tmp1 = s_max( tmp1, abs_s( White_exc16k_fx[i] ) );
2814 : /*White_exc16k_fx[i] =32767;*/
2815 : /*White_exc16k_fx[i] = shr(White_exc16k_fx[i],5);*/
2816 : }
2817 0 : *Q_white_exc = sub( add( *Q_white_exc, tmp ), 13 ); /* *Q_white_exc + 18 + tmp -15 -16 */
2818 0 : tmp = norm_s( tmp1 );
2819 0 : if ( tmp1 == 0 )
2820 : {
2821 0 : tmp = 15;
2822 0 : move16();
2823 : }
2824 :
2825 0 : FOR( i = 0; i < L_FRAME16k; i++ )
2826 : {
2827 0 : White_exc16k_fx[i] = shl( White_exc16k_fx[i], sub( tmp, 1 ) );
2828 0 : move16();
2829 : }
2830 0 : *Q_white_exc = sub( add( *Q_white_exc, tmp ), 1 );
2831 0 : move16();
2832 : }
2833 :
2834 : /* *Q_white_exc = Q_bwe_exc_mod; move16(); output Qwhiteexc_FB from the GenShapedSHB function*/
2835 1350 : st_fx->prev_Q_bwe_syn = Q_bwe_exc;
2836 1350 : move16();
2837 :
2838 1350 : return;
2839 : }
2840 :
2841 :
2842 : /*======================================================================================*/
2843 : /* FUNCTION : void swb_tbe_enc_ivas_fx () */
2844 : /*--------------------------------------------------------------------------------------*/
2845 : /* PURPOSE : SWB TBE encoder, 6 - 14 kHz (or 7.5 - 15.5 kHz) band encoding module */
2846 : /*--------------------------------------------------------------------------------------*/
2847 : /* INPUT ARGUMENTS : */
2848 : /* _(Word16) coder_type : coding type */
2849 : /* _(Word16*) shb_speech_fx : SHB target signal (6-14kHz) at 16kHz Q0 */
2850 : /* _(Word16*) bwe_exc_extended :bandwidth extended exciatation Q0 */
2851 : /* _(Word16[]) voice_factors :voicing factors Q15 */
2852 : /* _(Word16*) Q_white_exc :Q Format of White Exc */
2853 : /*--------------------------------------------------------------------------------------*/
2854 : /* OUTPUT ARGUMENTS : */
2855 : /* _(Word16[])White_exc16k_fx : shaped white excitation for the FB TBE Q_white_exc */
2856 : /* _(Word16*)fb_slope_fx : slope +ve (high freq > low freq), -ve or neutral Q12 */
2857 : /*--------------------------------------------------------------------------------------*/
2858 : /* INPUT/OUTPUT ARGUMENTS : */
2859 : /* _ Encoder_State *st_fx: : Encoder state structure */
2860 : /*--------------------------------------------------------------------------------------*/
2861 : /* RETURN ARGUMENTS : */
2862 : /* _ None */
2863 : /*--------------------------------------------------------------------------------------*/
2864 : /* CALLED FROM : TX */
2865 : /*======================================================================================*/
2866 :
2867 115109 : void swb_tbe_enc_ivas_fx(
2868 : Encoder_State *st_fx, /* i/o: encoder state structure */
2869 : STEREO_ICBWE_ENC_HANDLE hStereoICBWE, /* i/o: IC-BWE state structure */
2870 : Word16 *shb_speech_fx, /* i : SHB target signal (6-14kHz) at 16kHz Q_shb */
2871 : Word32 *bwe_exc_extended, /* i : bandwidth extended exciatation 2 * Q_new */
2872 : const Word16 voice_factors_fx[], /* i : voicing factors Q15 */
2873 : Word16 *White_exc16k_fx, /* o : shaped white excitation for the FB TBE Q_white_exc */
2874 : Word16 *Q_white_exc,
2875 : Word16 Q_new,
2876 : Word16 Q_shb,
2877 : Word16 *voicing, /* i : OL maximum normalized correlation */
2878 : const Word16 pitch_buf[] /* i : subframe pitch Q6 */
2879 : )
2880 : {
2881 : Word16 i, j, k, delay;
2882 :
2883 : Word16 shb_old_speech_fx[L_LOOK_16k + L_SUBFR16k + L_FRAME16k];
2884 : Word16 bwe_exc_extended_16[L_FRAME32k + NL_BUFF_OFFSET];
2885 :
2886 : Word16 shaped_shb_excitation_fx[L_FRAME16k + L_SHB_LAHEAD];
2887 : Word32 GainFrame_fx; /* Q18 */
2888 : Word16 GainShape_fx[NUM_SHB_SUBFR]; /* Q15 */
2889 : Word16 lpc_shb_fx[LPC_SHB_ORDER + 1], lsp_shb_fx[LPC_SHB_ORDER], lsf_shb_fx[LPC_SHB_ORDER];
2890 : Word16 weights_lsp[LPC_SHB_ORDER];
2891 : Word16 Q_out;
2892 : Word16 *shb_frame_fx, *shb_new_speech_fx;
2893 : Word16 R_h[LPC_SHB_ORDER + 2]; /* Autocorrelations of windowed speech MSB */
2894 : Word16 R_l[LPC_SHB_ORDER + 2]; /* Autocorrelations of windowed speech LSB */
2895 : Word16 Q_R;
2896 : Word32 LepsP[LPC_SHB_ORDER + 1];
2897 :
2898 : Word16 ana_align_delay;
2899 : Word32 prev_pow_fx, curr_pow_fx, Lscale;
2900 : Word32 p2m_in_fx, p2m_out_fx;
2901 :
2902 : Word16 exp_out, exp, exp1, frac;
2903 : Word16 cnt, n_mem, n_mem2;
2904 : Word32 L_tmp, L_tmp1;
2905 : Word16 Q_bwe_exc;
2906 :
2907 : Word16 frGainAttenuate, frGainSmoothEn;
2908 : Word16 MA_lsp_shb_spacing;
2909 : Word16 temp_swb_fac, feedback;
2910 : Word32 L_feedback;
2911 : Word16 tmp, tmp1, tmp2;
2912 : Word32 Lmax;
2913 : Word16 sc;
2914 : Word16 lsf_shb_orig_fx[LPC_SHB_ORDER];
2915 : Word16 sd_uq_q_fx;
2916 : Word16 vf_modified_fx[NB_SUBFR16k];
2917 : Word16 pitBufAvg_fx;
2918 : Word16 voicingBufAvg_fx;
2919 : Word16 sum1, sum2;
2920 : Word16 recip, Q_recip;
2921 : const Word16 *ptr_lsp_interp_coef_fx;
2922 :
2923 : Word16 lsp_shb_1_fx[LPC_SHB_ORDER], lsp_shb_2_fx[LPC_SHB_ORDER], lsp_temp_fx[LPC_SHB_ORDER];
2924 : Word16 lpc_shb_sf_fx[4 * ( LPC_SHB_ORDER + 1 )];
2925 :
2926 : /*Word32 shb_ener_sf_fx_32[4];*/
2927 : Word32 shb_ener_sf_Q31;
2928 : Word16 shb_res_fx[L_FRAME16k];
2929 : Word16 shb_res_gshape_fx[NB_SUBFR16k];
2930 : Word32 shb_res_gshape_fx_32[NB_SUBFR16k];
2931 : Word16 vf_ind_fx;
2932 :
2933 : Word16 formant_fac_fx;
2934 : // int16_t stab_check = 1;
2935 : Word16 MSFlag;
2936 : Word16 *nlExc16k_fx, *nlExc16k_e, *mixExc16k_fx, *mixExc16k_e;
2937 :
2938 : Word16 shaped_shb_excitationTemp_fx[L_FRAME16k];
2939 :
2940 : Word16 acorr_v2a_fx; /* Q15 */
2941 :
2942 : Word16 acorr_EnvSHBres[ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN], *p_acorr, shb_env_tilt_fx;
2943 : Word16 buf_EnvSHBres_fx[2 * L_FRAME4k], *p_buf, EnvSHBres_fx[L_FRAME16k], EnvSHBres_4k_fx[L_FRAME4k], EnvSHBres_4k_norm_fx[L_FRAME4k], env_mean_normf_fx[L_FRAME4k];
2944 : Word32 tmp_buf[L_FRAME4k];
2945 115109 : Word16 Q_EnvSHBres_4k_norm = Q31;
2946 115109 : move16();
2947 : Word16 GainShape_Interp_fx[NUM_SHB_SUBGAINS], GainShape_tilt_fx; /* Q15 */
2948 : Word16 seg_mean[4], den_seg_mean[4], *p_env, step;
2949 : Word16 temp, scale_fx, scale_e, pow_e, tmp_e, tmp1_e;
2950 : Word16 seg_len, seg_len_2;
2951 : Word16 den_seg_len, den_seg_len_2, fact_atten_fx; /* Q15 */
2952 : Word32 sum_gain_fx, normFact_fx;
2953 : Word32 pow0_fx, alpha_fx;
2954 : Word16 Env_error_part_fx[NUM_SHB_SUBGAINS], Env_error_fx; /* Q0 */
2955 :
2956 : Word16 mean_vf;
2957 : Word16 lsf_diff_fx[LPC_SHB_ORDER], w_fx[LPC_SHB_ORDER];
2958 : Word16 refl_fx[M];
2959 : Word16 tilt_para_fx;
2960 : Word16 Q_bwe_exc_fb;
2961 : Word16 n_subfr_saturation;
2962 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
2963 115109 : Flag Overflow = 0;
2964 115109 : move16();
2965 : #endif
2966 :
2967 115109 : Env_error_fx = 0;
2968 115109 : move16();
2969 115109 : pitBufAvg_fx = 0;
2970 115109 : move16();
2971 115109 : voicingBufAvg_fx = 0;
2972 115109 : move16();
2973 115109 : set16_fx( Env_error_part_fx, 0, NUM_SHB_SUBGAINS );
2974 115109 : shb_env_tilt_fx = 0;
2975 115109 : move16();
2976 115109 : sum1 = 0;
2977 115109 : move16();
2978 115109 : sum2 = 0;
2979 115109 : move16();
2980 :
2981 115109 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
2982 115109 : RF_ENC_HANDLE hRF = st_fx->hRF;
2983 115109 : BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
2984 :
2985 : /* init and buffers set up */
2986 115109 : exp1 = 0; /* to avoid compilation warnings */
2987 115109 : move16();
2988 :
2989 115109 : test();
2990 115109 : IF( st_fx->element_mode >= IVAS_CPE_DFT && hStereoICBWE != NULL )
2991 : {
2992 24220 : nlExc16k_fx = hStereoICBWE->nlExc16k_fx;
2993 24220 : nlExc16k_e = &hStereoICBWE->nlExc16k_e;
2994 24220 : mixExc16k_fx = hStereoICBWE->mixExc16k_fx;
2995 24220 : mixExc16k_e = &hStereoICBWE->mixExc16k_e;
2996 24220 : MSFlag = hStereoICBWE->MSFlag;
2997 24220 : move16();
2998 : }
2999 : ELSE
3000 : {
3001 90889 : nlExc16k_fx = NULL;
3002 90889 : nlExc16k_e = NULL;
3003 90889 : mixExc16k_fx = NULL;
3004 90889 : mixExc16k_e = NULL;
3005 90889 : MSFlag = 0;
3006 90889 : move16();
3007 : }
3008 :
3009 115109 : set16_fx( shaped_shb_excitationTemp_fx, 0, L_FRAME16k );
3010 :
3011 115109 : test();
3012 115109 : test();
3013 115109 : IF( EQ_16( st_fx->element_mode, IVAS_CPE_TD ) && ( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) ) )
3014 : {
3015 4283 : ana_align_delay = -L_SHB_LAHEAD - ( NL_BUFF_OFFSET / 2 ) + 20; /* 20 corresponds to the 1.25 ms CLDFB delay */
3016 4283 : move16();
3017 : }
3018 : ELSE
3019 : {
3020 110826 : ana_align_delay = -L_SHB_LAHEAD - ( NL_BUFF_OFFSET / 2 );
3021 110826 : move16();
3022 : }
3023 :
3024 : /* compensate for the delay in target generation and subframe LA */
3025 115109 : shb_frame_fx = shb_old_speech_fx + L_SUBFR16k + ana_align_delay;
3026 :
3027 115109 : set16_fx( shb_old_speech_fx, 0, L_LOOK_16k + L_SUBFR16k + L_FRAME16k );
3028 :
3029 : /* set up the speech buffers for TBE processing*/
3030 115109 : delay = L_LOOK_16k + L_SUBFR16k;
3031 115109 : move16();
3032 115109 : IF( EQ_16( st_fx->element_mode, IVAS_CPE_TD ) )
3033 : {
3034 5709 : shb_new_speech_fx = shb_old_speech_fx + delay - L_MEM_RECALC_TBE_16K;
3035 : }
3036 : ELSE
3037 : {
3038 109400 : shb_new_speech_fx = shb_old_speech_fx + delay;
3039 : }
3040 :
3041 115109 : Copy( hBWE_TD->old_speech_shb_fx, shb_old_speech_fx, delay );
3042 115109 : Copy( shb_speech_fx, shb_new_speech_fx, L_FRAME16k );
3043 115109 : Copy( shb_old_speech_fx + L_FRAME16k, hBWE_TD->old_speech_shb_fx, delay );
3044 :
3045 : /* autocorrelation of SHB speech for 10-th order LP analysis */
3046 115109 : autocorr_fx( shb_old_speech_fx,
3047 : LPC_SHB_ORDER + 1,
3048 : R_h, /* autocorr (msb) Q15 */
3049 : R_l, /* autocorr (lsb) */
3050 : &Q_R,
3051 : NS2SA( INT_FS_16k, ACELP_LOOK_NS ) + L_SUBFR16k + L_FRAME16k,
3052 : win_lpc_shb_fx,
3053 : 0,
3054 : 1 );
3055 :
3056 :
3057 : /* Spectral smoothing of autocorrelation coefficients */
3058 115109 : test();
3059 115109 : test();
3060 115109 : IF( st_fx->rf_mode || EQ_32( st_fx->extl_brate, SWB_TBE_0k95 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) )
3061 : {
3062 363000 : FOR( i = 1; i <= LPC_SHB_ORDER; i++ )
3063 : {
3064 330000 : L_tmp = Mpy_32( R_h[i], R_l[i], wac_swb_h[i - 1], wac_swb_l[i - 1] );
3065 330000 : L_Extract( L_tmp, &R_h[i], &R_l[i] );
3066 : }
3067 : }
3068 :
3069 : /* Set the autocorr[0] element to a non-negative value */
3070 115109 : R_l[0] = s_max( R_l[0], 1 );
3071 115109 : move16();
3072 :
3073 115109 : E_LPC_lev_dur( R_h, R_l, lpc_shb_fx, LepsP, LPC_SHB_ORDER, NULL ); /* LPC in Q14 */
3074 : {
3075 : Word16 enerG, lpc_shb1[M + 1];
3076 :
3077 : /* extend the lpc_shb to a 16th order gain calc */
3078 115109 : set16_fx( lpc_shb1, 0, M + 1 );
3079 115109 : Copy( lpc_shb_fx, lpc_shb1, LPC_SHB_ORDER + 1 );
3080 :
3081 : /* estimate the LP gain */
3082 115109 : enerG = Enr_1_Az_fx( lpc_shb1, 2 * L_SUBFR ); /* Q3 */
3083 :
3084 : /* if the LP gain is greater than a threshold, avoid saturation */
3085 115109 : IF( GT_16( enerG, 256 /*32 Q3*/ ) )
3086 : {
3087 49 : set16_fx( lpc_shb_fx, 0, LPC_SHB_ORDER + 1 );
3088 49 : E_LPC_lev_dur( R_h, R_l, lpc_shb_fx, LepsP, 2, NULL ); /* LPC in Q14 */
3089 : }
3090 : }
3091 :
3092 : /* this is needed as the E_LPC_lev_dur function outputs lpc in Q14 */
3093 115109 : Copy_Scale_sig( lpc_shb_fx, lpc_shb_fx, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_fx[0] ), 2 ) );
3094 :
3095 : /* Expand bandwidth of the LP coeffs */
3096 115109 : test();
3097 115109 : test();
3098 115109 : IF( st_fx->rf_mode || EQ_32( st_fx->extl_brate, SWB_TBE_0k95 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) )
3099 : {
3100 363000 : FOR( i = 1; i <= LPC_SHB_ORDER; i++ )
3101 : {
3102 330000 : lpc_shb_fx[i] = mult_r( lpc_shb_fx[i], lpc_weights_fx[i] );
3103 330000 : move16();
3104 : }
3105 : }
3106 :
3107 : /* stab_check = a2lsp( lsf_shb, lpc_shb, LPC_SHB_ORDER ); */
3108 :
3109 115109 : test();
3110 115109 : test();
3111 115109 : IF( ( NE_16( st_fx->last_extl, SWB_TBE ) && NE_16( st_fx->last_extl, FB_TBE ) ) || st_fx->ini_frame == 0 )
3112 : {
3113 102663 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
3114 : {
3115 : // hBWE_TD->prev_lsp_shb_fx[i] = i / 20.0f; // This value in float enc is lsf.
3116 93330 : hBWE_TD->prev_lsp_shb_fx[i] = lsp_shb_prev_tbl_swb_tbe_enc_fx[i]; // lsf converted to lsp as fixed enc stores lsp.
3117 93330 : move16();
3118 : }
3119 : }
3120 :
3121 : // if ( stab_check == 0 )
3122 : //{
3123 : // mvr2r( hBWE_TD->prev_lsp_shb, lsf_shb, LPC_SHB_ORDER );
3124 : // }
3125 :
3126 : /* LPC to LSP conversion */
3127 : /* LPC: Q12, LSP: Q15 */
3128 115109 : E_LPC_a_lsp_conversion( lpc_shb_fx, lsp_shb_fx, hBWE_TD->prev_lsp_shb_fx, LPC_SHB_ORDER );
3129 :
3130 : /* LSP to LSF conversion */
3131 : /* LSP: Q15, LSF: Q15 */
3132 115109 : E_LPC_lsp_lsf_conversion( lsp_shb_fx, lsf_shb_fx, LPC_SHB_ORDER );
3133 :
3134 115109 : Copy( lsp_shb_fx, hBWE_TD->prev_lsp_shb_fx, LPC_SHB_ORDER );
3135 115109 : Copy( lsf_shb_fx, lsf_shb_orig_fx, LPC_SHB_ORDER );
3136 :
3137 : /* Input signal filtering in case of tonal sounds in the high band
3138 : gain Frame smoothing and attenuation control */
3139 115109 : gainFrSmooth_En_ivas_fx( st_fx, shb_frame_fx, lpc_shb_fx, lsf_shb_fx, &MA_lsp_shb_spacing, &frGainAttenuate, &frGainSmoothEn );
3140 :
3141 115109 : test();
3142 115109 : test();
3143 115109 : IF( st_fx->rf_mode || EQ_32( st_fx->extl_brate, SWB_TBE_0k95 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) )
3144 : {
3145 33000 : lsp_weights_ivas_fx( lsf_shb_fx, weights_lsp, LPC_SHB_ORDER, &Q_out );
3146 :
3147 : /* to compensate for the 1.1* weighting done inside the function lsp_weights */
3148 : /*weights_lsp[3]*=0.909091f; weights_lsp[4]*=0.909091f; */
3149 33000 : weights_lsp[3] = mult_r( weights_lsp[3], 29789 /*0.909091f Q15*/ );
3150 33000 : move16();
3151 33000 : weights_lsp[4] = mult_r( weights_lsp[4], 29789 /*0.909091f Q15*/ );
3152 33000 : move16();
3153 :
3154 : /* 8-bit VQ, 10 dimension */
3155 33000 : i = closest_centroid_fx( lsf_shb_fx, weights_lsp, swb_tbe_lsfvq_cbook_8b, 256, LPC_SHB_ORDER );
3156 33000 : Copy( swb_tbe_lsfvq_cbook_8b + i * LPC_SHB_ORDER, lsf_shb_fx, LPC_SHB_ORDER );
3157 :
3158 33000 : set16_fx( hBWE_TD->lsf_idx, 0, NUM_Q_LSF );
3159 :
3160 33000 : IF( EQ_16( st_fx->codec_mode, MODE1 ) )
3161 : {
3162 33000 : push_indice( hBstr, IND_SHB_LSF, i, 8 );
3163 : }
3164 : ELSE
3165 : {
3166 0 : hBWE_TD->lsf_idx[0] = i;
3167 0 : move16();
3168 : }
3169 : }
3170 : ELSE
3171 : {
3172 : /* LSF quantization (21 bits) */
3173 82109 : Quant_BWE_LSF_ivas_fx( hBstr, st_fx->hBWE_TD, st_fx->codec_mode, lsf_shb_fx, lsf_shb_fx, st_fx->extl_brate );
3174 : }
3175 :
3176 : /* space the lsfs to assert a minimum distance */
3177 115109 : space_lsfs_fx( lsf_shb_fx, LPC_SHB_ORDER );
3178 :
3179 : /* voice factor adjustment and gainframe attenuation factor */
3180 115109 : tmp = sub( lsf_shb_fx[0], lsf_shb_orig_fx[0] );
3181 115109 : L_tmp = L_mult( tmp, tmp );
3182 1151090 : FOR( i = 1; i < LPC_SHB_ORDER; i++ )
3183 : {
3184 : /* Estimate the QD in lsfs between UQ and Q */
3185 1035981 : tmp = sub( lsf_shb_fx[i], lsf_shb_orig_fx[i] );
3186 1035981 : L_tmp = L_mac( L_tmp, tmp, tmp );
3187 : }
3188 115109 : sd_uq_q_fx = round_fx( L_tmp ); /* sd_uq_q_fx in Q15 */
3189 : /* voice factor modification to limit any spurious jumps in the middle of voiced subframes*/
3190 : /* mean(voice_factors_fx[i], 4); */
3191 :
3192 115109 : L_tmp = L_mult( voice_factors_fx[0], 8192 );
3193 115109 : L_tmp = L_mac( L_tmp, voice_factors_fx[1], 8192 );
3194 115109 : L_tmp = L_mac( L_tmp, voice_factors_fx[2], 8192 );
3195 115109 : mean_vf = mac_r( L_tmp, voice_factors_fx[3], 8192 );
3196 :
3197 115109 : Copy( voice_factors_fx, vf_modified_fx, NB_SUBFR16k );
3198 :
3199 115109 : test();
3200 115109 : IF( EQ_16( st_fx->coder_type, VOICED ) || GT_16( mean_vf, 13107 /*0.4f Q15*/ ) )
3201 : {
3202 100924 : FOR( i = 1; i < NB_SUBFR; i++ )
3203 : {
3204 75693 : L_tmp = L_mult( voice_factors_fx[i], 26214 );
3205 75693 : vf_modified_fx[i] = mac_r( L_tmp, voice_factors_fx[i - 1], 6554 );
3206 75693 : move16();
3207 : }
3208 25231 : IF( NE_16( st_fx->L_frame, L_FRAME ) )
3209 : {
3210 12623 : L_tmp = L_mult( voice_factors_fx[4], 26214 );
3211 12623 : vf_modified_fx[4] = mac_r( L_tmp, voice_factors_fx[3], 6554 );
3212 12623 : move16();
3213 : }
3214 : }
3215 :
3216 : /* convert quantized LSFs to LSPs for interpolation */
3217 115109 : E_LPC_lsf_lsp_conversion( lsf_shb_fx, lsp_shb_2_fx, LPC_SHB_ORDER );
3218 :
3219 115109 : test();
3220 115109 : IF( EQ_16( st_fx->last_extl, SWB_TBE ) || EQ_16( st_fx->last_extl, FB_TBE ) )
3221 : {
3222 : /* SHB LSP values from prev. frame for interpolation */
3223 108201 : Copy( hBWE_TD->swb_lsp_prev_interp_fx, lsp_shb_1_fx, LPC_SHB_ORDER );
3224 : }
3225 : ELSE
3226 : {
3227 : /* Use current frame's LSPs; in effect no interpolation */
3228 6908 : Copy( lsp_shb_2_fx, lsp_shb_1_fx, LPC_SHB_ORDER );
3229 : }
3230 :
3231 115109 : lsf_diff_fx[0] = lsf_diff_fx[sub( LPC_SHB_ORDER, 1 )] = 16384; /*Q15*/
3232 115109 : move16();
3233 1035981 : FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
3234 : {
3235 920872 : lsf_diff_fx[i] = sub( lsf_shb_fx[i], lsf_shb_fx[sub( i, 1 )] );
3236 920872 : move16();
3237 : }
3238 115109 : a2rc_fx( hBWE_TD->cur_sub_Aq_fx + 1, refl_fx, M );
3239 :
3240 : /* LSP interpolation for 13.2 kbps and 16.4 kbps */
3241 : /* tilt_para_fx = 6.6956f * (1.0f + refl_fx[0]) * (1.0f + refl_fx[0])
3242 : - 3.8714f * (1.0f + refl_fx[0])
3243 : + 1.3041f; */
3244 115109 : tmp = add( 16384, shr( refl_fx[0], 1 ) ); /*Q14*/
3245 115109 : tmp1 = mult( 27425 /*Q12*/, tmp ); /*Q11*/
3246 115109 : tmp1 = mult( tmp1, tmp );
3247 115109 : tmp2 = shr( mult( 31715, tmp ), 2 ); /* Q11 */
3248 115109 : tilt_para_fx = add( sub( tmp1, tmp2 ), 1335 ); /*Q10*/
3249 :
3250 115109 : IF( NE_16( st_fx->last_extl, SWB_TBE ) )
3251 : {
3252 432288 : FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
3253 : {
3254 384256 : hBWE_TD->prev_lsf_diff_fx[i - 1] = mult( lsf_diff_fx[i], 16384 ); /*Q15*/
3255 384256 : move16();
3256 : }
3257 : }
3258 :
3259 115109 : IF( LE_32( st_fx->extl_brate, FB_TBE_1k8 ) )
3260 : {
3261 90051 : test();
3262 90051 : test();
3263 90051 : test();
3264 90051 : test();
3265 90051 : test();
3266 90051 : IF( !( GT_16( hBWE_TD->prev_tilt_para_fx, 5120 /* 5.0f in Q10 */ ) && ( EQ_16( st_fx->coder_type, TRANSITION ) || LT_16( tilt_para_fx, 1024 /* 1.0f in Q10 */ ) ) ) &&
3267 : !( ( ( LT_16( hBWE_TD->prev_tilt_para_fx, 3072 /* 3.0f in Q10 */ ) && GE_16( hBWE_TD->prev_coder_type, VOICED ) ) ) && GT_16( tilt_para_fx, 5120 /* 5.0f in Q10 */ ) ) )
3268 : {
3269 790182 : FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
3270 : {
3271 702384 : IF( LT_16( lsf_diff_fx[i], hBWE_TD->prev_lsf_diff_fx[i - 1] ) )
3272 : {
3273 206025 : tmp = mult( 26214, lsf_diff_fx[i] );
3274 206025 : tmp = div_s( tmp, hBWE_TD->prev_lsf_diff_fx[i - 1] );
3275 206025 : tmp = s_max( tmp, 16384 );
3276 206025 : w_fx[i] = s_min( tmp, 32767 );
3277 206025 : move16();
3278 : }
3279 : ELSE
3280 : {
3281 496359 : tmp = mult( 26214, hBWE_TD->prev_lsf_diff_fx[i - 1] );
3282 496359 : tmp = div_s( tmp, lsf_diff_fx[i] );
3283 496359 : tmp = s_max( tmp, 16384 );
3284 496359 : w_fx[i] = s_min( tmp, 32767 );
3285 496359 : move16();
3286 : }
3287 : }
3288 87798 : w_fx[0] = w_fx[1];
3289 87798 : move16();
3290 87798 : w_fx[LPC_SHB_ORDER - 1] = w_fx[LPC_SHB_ORDER - 2];
3291 87798 : move16();
3292 :
3293 965778 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
3294 : {
3295 877980 : tmp1 = mult( lsp_shb_1_fx[i], sub( 32767, w_fx[i] ) );
3296 877980 : tmp2 = mult( lsp_shb_2_fx[i], w_fx[i] );
3297 877980 : lsp_temp_fx[i] = add( tmp1, tmp2 );
3298 877980 : move16();
3299 : }
3300 : }
3301 : ELSE
3302 : {
3303 2253 : Copy( lsp_shb_2_fx, lsp_temp_fx, LPC_SHB_ORDER );
3304 : }
3305 : }
3306 :
3307 115109 : Copy( lsf_diff_fx + 1, hBWE_TD->prev_lsf_diff_fx, LPC_SHB_ORDER - 2 );
3308 115109 : hBWE_TD->prev_tilt_para_fx = tilt_para_fx;
3309 115109 : move16();
3310 :
3311 115109 : shb_ener_sf_Q31 = 0;
3312 115109 : move16();
3313 115109 : test();
3314 115109 : IF( GE_32( st_fx->extl_brate, SWB_TBE_2k8 ) )
3315 : {
3316 : /* ---------- SHB LSP interpolation ---------- */
3317 25058 : ptr_lsp_interp_coef_fx = interpol_frac_shb; /* Q15 */
3318 125290 : FOR( j = 0; j < 4; j++ )
3319 : {
3320 1102552 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
3321 : {
3322 : /*lsp_temp_fx[i] = lsp_shb_1_fx[i]*(*ptr_lsp_interp_coef_fx) */
3323 : /* + lsp_shb_2_fx[i]*(*(ptr_lsp_interp_coef_fx+1)); */
3324 1002320 : L_tmp = L_mult( lsp_shb_1_fx[i], ( *ptr_lsp_interp_coef_fx ) );
3325 1002320 : lsp_temp_fx[i] = mac_r( L_tmp, lsp_shb_2_fx[i], ( *( ptr_lsp_interp_coef_fx + 1 ) ) );
3326 1002320 : move16();
3327 : }
3328 100232 : ptr_lsp_interp_coef_fx += 2;
3329 :
3330 100232 : tmp = i_mult2( j, ( LPC_SHB_ORDER + 1 ) );
3331 : /* convert LSPs to LPC coefficients for SHB synthesis*/
3332 100232 : E_LPC_f_lsp_a_conversion( lsp_temp_fx, lpc_shb_sf_fx + tmp, LPC_SHB_ORDER );
3333 : /* Bring the LPCs to Q12 */
3334 100232 : Copy_Scale_sig( lpc_shb_sf_fx + tmp, lpc_shb_sf_fx + tmp, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_sf_fx[tmp] ), 2 ) );
3335 : }
3336 :
3337 : /* -------- Calculate the SHB Energy -------- */
3338 : /*shb_ener_sf[0] = 0.003125f * sum2_f( shb_frame + L_SHB_LAHEAD, 320 );*/
3339 8043618 : FOR( i = L_SHB_LAHEAD; i < L_FRAME16k + L_SHB_LAHEAD; i++ )
3340 : {
3341 : /* shbEner = shbEner + in[i] * in[i] */
3342 8018560 : shb_ener_sf_Q31 = L_mac0_sat( shb_ener_sf_Q31, shb_frame_fx[i], shb_frame_fx[i] );
3343 : /* o: shb_ener_sf_Q31 in (2*Q_shb) */
3344 : }
3345 25058 : shb_ener_sf_Q31 = Mult_32_16( shb_ener_sf_Q31, 102 /*0.003125f Q15*/ );
3346 25058 : shb_ener_sf_Q31 = L_add( 1l /*1 Q0*/, shb_ener_sf_Q31 );
3347 25058 : Quant_shb_ener_sf_ivas_fx( st_fx, &shb_ener_sf_Q31, shl( Q_shb, 1 ) );
3348 :
3349 : /* -------- calculate the residuals using the FOUR subframe LPCs -------- */
3350 25058 : set16_fx( shb_res_fx, 0, L_FRAME16k );
3351 25058 : Residu3_10_fx( lpc_shb_sf_fx, shb_frame_fx + L_SHB_LAHEAD, shb_res_fx, 80, 0 );
3352 25058 : Residu3_10_fx( lpc_shb_sf_fx + ( LPC_SHB_ORDER + 1 ), shb_frame_fx + L_SHB_LAHEAD + 80, shb_res_fx + 80, 80, 0 );
3353 25058 : Residu3_10_fx( lpc_shb_sf_fx + 2 * ( LPC_SHB_ORDER + 1 ), shb_frame_fx + L_SHB_LAHEAD + 160, shb_res_fx + 160, 80, 0 );
3354 25058 : Residu3_10_fx( lpc_shb_sf_fx + 3 * ( LPC_SHB_ORDER + 1 ), shb_frame_fx + L_SHB_LAHEAD + 240, shb_res_fx + 240, 80, 0 );
3355 : /* i: shb_frame_fx in Q_shb */
3356 : /* o: shb_res_fx in Q_shb */
3357 :
3358 25058 : set32_fx( shb_res_gshape_fx_32, 0, NB_SUBFR16k );
3359 150348 : FOR( i = 0; i < NB_SUBFR16k; i++ )
3360 : {
3361 125290 : shb_res_gshape_fx_32[i] = sum2_fx( shb_res_fx + i_mult( i, 64 ), 64 );
3362 125290 : move32();
3363 : }
3364 : /* o: shb_res_gshape_fx_32 in (2*Q_shb+1) */
3365 :
3366 25058 : maximum_32_fx( shb_res_gshape_fx_32, NB_SUBFR16k, &L_tmp );
3367 :
3368 : /* root_a_over_b_fx(shb_res_gshape_fx_32[i], (2*Q_shb+1), L_tmp, (2*Q_shb+1), &exp);*/
3369 : /* First, find 1/L_tmp, L_tmp in QA = (2*Q_shb+1) */
3370 :
3371 : /* case when den = 0 */
3372 25058 : recip = 0; /*instead of 32767 to be compatible with previous root_a_over_b_fx() output */
3373 25058 : move16();
3374 25058 : Q_recip = 0;
3375 25058 : move16();
3376 :
3377 25058 : IF( L_tmp )
3378 : {
3379 25058 : exp = norm_l( L_tmp );
3380 25058 : tmp = extract_h( L_shl( L_tmp, exp ) );
3381 25058 : recip = div_s( 16384, tmp );
3382 : // Q_recip = sub( 31, sub( exp, 14 ) ); /* = 31-(exp+2*Q_shb+1-14), but adjusted by (2*Q_shb+1) for use at Mult_32_16 below */
3383 25058 : Q_recip = sub( 31 + 14, exp ); /* = 31-(exp+2*Q_shb+1-14), but adjusted by (2*Q_shb+1) for use at Mult_32_16 below */
3384 : }
3385 :
3386 150348 : FOR( i = 0; i < NB_SUBFR16k; i++ )
3387 : {
3388 125290 : L_tmp1 = Mult_32_16( shb_res_gshape_fx_32[i], recip ); /*Q = Q_recip+1-16*/
3389 125290 : L_tmp = root_a_fx( L_tmp1, sub( Q_recip, 15 ), &exp );
3390 125290 : shb_res_gshape_fx[i] = round_fx( L_shl( L_tmp, sub( exp, 1 ) ) ); /* Q14 */
3391 125290 : move16();
3392 : }
3393 :
3394 25058 : Quant_shb_res_gshape_ivas_fx( st_fx, shb_res_gshape_fx );
3395 : }
3396 90051 : ELSE IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
3397 : {
3398 21415 : FOR( j = 0; j < 4; j++ )
3399 : {
3400 17132 : tmp = i_mult2( j, ( LPC_SHB_ORDER + 1 ) );
3401 : /* convert LSPs to LPC coefficients for SHB synthesis*/
3402 17132 : E_LPC_f_lsp_a_conversion( lsp_temp_fx, lpc_shb_sf_fx + tmp, LPC_SHB_ORDER );
3403 : /* Bring the LPCs to Q12 */
3404 17132 : Copy_Scale_sig( lpc_shb_sf_fx + tmp, lpc_shb_sf_fx + tmp, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_sf_fx[tmp] ), 2 ) );
3405 : }
3406 :
3407 : /* -------- calculate the residuals using the FOUR subframe LPCs -------- */
3408 4283 : set16_fx( shb_res_fx, 0, L_FRAME16k );
3409 4283 : Residu3_10_fx( lpc_shb_sf_fx, shb_frame_fx + L_SHB_LAHEAD, shb_res_fx, 80, 0 );
3410 4283 : Residu3_10_fx( lpc_shb_sf_fx + ( LPC_SHB_ORDER + 1 ), shb_frame_fx + L_SHB_LAHEAD + 80, shb_res_fx + 80, 80, 0 );
3411 4283 : Residu3_10_fx( lpc_shb_sf_fx + 2 * ( LPC_SHB_ORDER + 1 ), shb_frame_fx + L_SHB_LAHEAD + 160, shb_res_fx + 160, 80, 0 );
3412 4283 : Residu3_10_fx( lpc_shb_sf_fx + 3 * ( LPC_SHB_ORDER + 1 ), shb_frame_fx + L_SHB_LAHEAD + 240, shb_res_fx + 240, 80, 0 );
3413 : }
3414 :
3415 115109 : Copy( lsp_shb_2_fx, hBWE_TD->swb_lsp_prev_interp_fx, LPC_SHB_ORDER ); /* lsp_shb_2_fx in Q15 */
3416 : /* for 13.2 and 16.4kbps */
3417 115109 : E_LPC_f_lsp_a_conversion( lsp_temp_fx, lpc_shb_fx, LPC_SHB_ORDER );
3418 115109 : Copy_Scale_sig( lpc_shb_fx, lpc_shb_fx, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_fx[0] ), 2 ) ); /* Q12 */
3419 :
3420 : /* Determine formant PF strength */
3421 115109 : formant_fac_fx = swb_formant_fac_fx( lpc_shb_fx[1], &hBWE_TD->tilt_mem_fx );
3422 : /* i:lpc_shb_fx Q12, o:formant_fac_fx Q15 */
3423 :
3424 : /* calculate SHB auto-correlation function and convert to SHB voicing factor */
3425 : // acorr_v2a = 0.0f;
3426 115109 : L_tmp = 0;
3427 115109 : move32();
3428 115109 : acorr_v2a_fx = 0;
3429 115109 : move16();
3430 :
3431 115109 : test();
3432 115109 : IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
3433 : {
3434 : /* calculate the TD envelope of the SHB residual signal */
3435 4283 : Scale_sig( hBWE_TD->mem_shb_res_fx, MAX_LEN_MA_FILTER, sub( hBWE_TD->prev_Q_shb, Q_shb ) ); /* Q(hBWE_TD->prev_Q_shb) -> Q(Q_shb) */
3436 4283 : find_td_envelope_fx( shb_res_fx, L_FRAME16k, 20, hBWE_TD->mem_shb_res_fx, EnvSHBres_fx ); /* Q_shb */
3437 :
3438 : /* downsample the TD envelope by 4 */
3439 346923 : FOR( k = 0; k < L_FRAME4k; k++ )
3440 : {
3441 342640 : EnvSHBres_4k_fx[k] = EnvSHBres_fx[4 * k]; /* Q_shb */
3442 342640 : move16();
3443 : }
3444 :
3445 : /* calculate long-term mean envelope over four consecutive segments using linear interpolation */
3446 4283 : seg_len = L_FRAME4k / 4;
3447 4283 : move16();
3448 4283 : den_seg_len = 1638; /* Q15 */
3449 4283 : move16();
3450 4283 : seg_len_2 = L_FRAME4k / 8;
3451 4283 : move16();
3452 4283 : den_seg_len_2 = 3641; /* Q15 */
3453 4283 : move16();
3454 21415 : FOR( k = 0; k < 4; k++ )
3455 : {
3456 : // seg_mean[k] = max(1.0f, mean(&EnvSHBres_4k[k * seg_len], seg_len));
3457 17132 : seg_mean[k] = s_max( 1, mean_no_sat_fx( &EnvSHBres_4k_fx[k * seg_len], seg_len ) ); /* Q_shb */
3458 17132 : move16();
3459 : // den_seg_mean[k] = 1.0f / seg_mean[k];
3460 17132 : tmp = BASOP_Util_Divide1616_Scale( MAX16B, seg_mean[k], &tmp_e );
3461 17132 : tmp_e = sub( tmp_e, sub( 15, Q_shb ) );
3462 17132 : den_seg_mean[k] = shl_sat( tmp, tmp_e ); /* Q15 */
3463 17132 : move16();
3464 : }
3465 :
3466 : /* first half of the first segment */
3467 4283 : IF( hBWE_TD->old_mean_EnvSHBres_fx == 0 )
3468 : {
3469 81 : set16_fx( env_mean_normf_fx, den_seg_mean[0], seg_len_2 );
3470 : }
3471 : ELSE
3472 : {
3473 4202 : env_mean_normf_fx[0] = hBWE_TD->old_mean_EnvSHBres_fx; /* Q15 */
3474 4202 : move16();
3475 : // step = (den_seg_mean[0] - hBWE_TD->old_mean_EnvSHBres) * den_seg_len_2;
3476 4202 : step = mult( sub( den_seg_mean[0], hBWE_TD->old_mean_EnvSHBres_fx ), den_seg_len_2 ); /* Q15 */
3477 42020 : FOR( i = 1; i < seg_len_2; i++ )
3478 : {
3479 37818 : env_mean_normf_fx[i] = add_sat( env_mean_normf_fx[i - 1], step ); /* Q15 */
3480 37818 : move16();
3481 : }
3482 : }
3483 :
3484 : /* segments 1-4 */
3485 4283 : p_env = &env_mean_normf_fx[seg_len_2];
3486 17132 : FOR( k = 1; k < 4; k++ )
3487 : {
3488 : // step = ( den_seg_mean[k] - den_seg_mean[k - 1] ) * den_seg_len;
3489 12849 : step = mult( sub( den_seg_mean[k], den_seg_mean[k - 1] ), den_seg_len );
3490 269829 : FOR( i = 0; i < seg_len; i++ )
3491 : {
3492 256980 : *p_env = add_sat( *( p_env - 1 ), step );
3493 256980 : move16();
3494 256980 : p_env++;
3495 : }
3496 : }
3497 :
3498 : /* last half of the last segment */
3499 : // set_f( p_env, den_seg_mean[3], seg_len_2 );
3500 4283 : set16_fx( p_env, den_seg_mean[3], seg_len_2 );
3501 4283 : hBWE_TD->old_mean_EnvSHBres_fx = den_seg_mean[3];
3502 4283 : move16();
3503 :
3504 : /* normalize residual SHB envelope with its long-term mean envelope */
3505 346923 : FOR( k = 0; k < L_FRAME4k; k++ )
3506 : {
3507 342640 : tmp_buf[k] = L_mult( EnvSHBres_4k_fx[k], env_mean_normf_fx[k] ); // Q_shb + Q15 + Q1 => Q_shb + Q16
3508 342640 : move32();
3509 : }
3510 4283 : Q_EnvSHBres_4k_norm = sub( getScaleFactor32( tmp_buf, L_FRAME4k ), 1 /* Guard bit */ );
3511 346923 : FOR( k = 0; k < L_FRAME4k; k++ )
3512 : {
3513 342640 : EnvSHBres_4k_norm_fx[k] = extract_h( L_shl( tmp_buf[k], Q_EnvSHBres_4k_norm ) ); // Q_shb + Q16 + Q_EnvSHBres_4k_norm - Q16 => Q_EnvSHBres_4k_norm + Q_shb
3514 342640 : move16();
3515 : }
3516 :
3517 : /* calculate tilt of the long-term mean envelope */
3518 4283 : lls_interp_n_fx( seg_mean, 4, &shb_env_tilt_fx, &temp, 0 );
3519 :
3520 : /* copy previous residual envelope to the buffer */
3521 4283 : Copy_Scale_sig( hBWE_TD->old_EnvSHBres_fx, buf_EnvSHBres_fx, L_FRAME4k, sub( hBWE_TD->prev_Q_shb, Q_shb ) ); /* Q(hBWE_TD->prev_Q_shb) -> Q(Q_shb) */
3522 :
3523 : /* subtract mean value from the normalized SHB residual envelope */
3524 4283 : p_buf = &buf_EnvSHBres_fx[L_FRAME4k];
3525 4283 : temp = mean_no_sat_fx( EnvSHBres_4k_norm_fx, L_FRAME4k ); // Q_EnvSHBres_4k_norm + Q_shb
3526 346923 : FOR( k = 0; k < L_FRAME4k; k++ )
3527 : {
3528 342640 : *p_buf++ = shr( sub( EnvSHBres_4k_norm_fx[k], temp ), Q_EnvSHBres_4k_norm ); // Q_shb
3529 342640 : move16();
3530 : }
3531 4283 : Q_EnvSHBres_4k_norm = add( Q_EnvSHBres_4k_norm, Q_shb );
3532 :
3533 : /* update memory */
3534 4283 : Copy( &buf_EnvSHBres_fx[L_FRAME4k], hBWE_TD->old_EnvSHBres_fx, L_FRAME4k ); // Q_shb
3535 :
3536 : /* calculate energy normalization factor for the auto-correlation function */
3537 : // pow0 = sum2_f( &buf_EnvSHBres[L_FRAME4k], L_FRAME4k ) + 1.0f;
3538 4283 : pow_e = sub( 15, Q_shb );
3539 4283 : pow0_fx = sum2_16_exp_fx( &buf_EnvSHBres_fx[L_FRAME4k], L_FRAME4k, &pow_e, 0 /* Give guard bits */ ) + 1;
3540 : // if (hBWE_TD->prev_enr_EnvSHBres == 1.0f)
3541 4283 : IF( BASOP_Util_Cmp_Mant32Exp( hBWE_TD->prev_enr_EnvSHBres_fx, hBWE_TD->prev_enr_EnvSHBres_e, 1, 31 ) == 0 )
3542 : {
3543 : // scale = 1.0f / pow0;
3544 616 : scale_fx = BASOP_Util_Divide3232_Scale( ONE_IN_Q31, pow0_fx, &tmp_e );
3545 616 : scale_e = sub( tmp_e, pow_e );
3546 : }
3547 : ELSE
3548 : {
3549 : // scale = 1.0f / max( pow0, hBWE_TD->prev_enr_EnvSHBres );
3550 3667 : L_tmp1 = pow0_fx;
3551 3667 : move32();
3552 3667 : IF( BASOP_Util_Cmp_Mant32Exp( L_tmp1, pow_e, hBWE_TD->prev_enr_EnvSHBres_fx, hBWE_TD->prev_enr_EnvSHBres_e ) < 0 )
3553 : {
3554 3220 : L_tmp1 = hBWE_TD->prev_enr_EnvSHBres_fx;
3555 3220 : move32();
3556 3220 : pow_e = hBWE_TD->prev_enr_EnvSHBres_e;
3557 3220 : move16();
3558 : }
3559 3667 : scale_fx = BASOP_Util_Divide3232_Scale( ONE_IN_Q31, L_tmp1, &tmp_e ); // Qs to be handled
3560 3667 : scale_e = sub( tmp_e, pow_e );
3561 : }
3562 4283 : hBWE_TD->prev_enr_EnvSHBres_fx = pow0_fx;
3563 4283 : move32();
3564 4283 : hBWE_TD->prev_enr_EnvSHBres_e = pow_e;
3565 4283 : move16();
3566 :
3567 : /* calculate normalized auto-correlation function on the residual normalized SHB envelope */
3568 4283 : p_acorr = acorr_EnvSHBres;
3569 4283 : p_buf = &buf_EnvSHBres_fx[L_FRAME4k - ENVSHBRES_ACORR_MIN];
3570 :
3571 175603 : FOR( k = 0; k < ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN; k++ )
3572 : {
3573 : //*p_acorr++ = scale * dotp(&buf_EnvSHBres[L_FRAME4k], p_buf--, L_FRAME4k);
3574 171320 : Word64 W_tmp = 0;
3575 171320 : move64();
3576 :
3577 : // tmp = *p_buf;
3578 13876920 : FOR( i = 0; i < L_FRAME4k; ++i )
3579 : {
3580 13705600 : L_tmp = L_mult( buf_EnvSHBres_fx[L_FRAME4k + i], p_buf[i] ); // 2 * Q_shb + 1
3581 13705600 : W_tmp = W_add( W_tmp, W_deposit32_l( L_tmp ) );
3582 : }
3583 171320 : p_buf--;
3584 171320 : tmp_e = W_norm( W_tmp );
3585 171320 : L_tmp1 = W_extract_h( W_shl( W_tmp, tmp_e ) ); // 2 * Q_shb + tmp_e - 31
3586 171320 : L_tmp = Mpy_32_16_1( L_tmp1, scale_fx ); /* 2 * Q_shb + tmp_e - 31 - scale_e */
3587 : //*p_acorr++ = extract_h( L_shl( L_tmp, add( 16, Q_shb ) - ( 2 * Q_shb + tmp_e - 31 - scale_e ) ) );
3588 171320 : *p_acorr++ = extract_h( L_shl( L_tmp, sub( add( 16, Q_shb ), sub( sub( add( shl( Q_shb, 1 ), tmp_e ), 31 ), scale_e ) ) ) );
3589 171320 : move16();
3590 : }
3591 :
3592 : /* calculate variance of the SHB autocorrelation function */
3593 : // acorr_v2a = sum2_f( acorr_EnvSHBres, ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN ) / ( ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN );
3594 4283 : tmp_e = sub( 15, Q_shb );
3595 4283 : L_tmp = Mpy_32_32( sum2_16_exp_fx( acorr_EnvSHBres, ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN, &tmp_e, 0 ), 53687091 /* 1 / ( ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN ) in Q31 */ );
3596 :
3597 : /* multiply with the maximum of the SHB autocorrelation function */
3598 4283 : maximum_fx( acorr_EnvSHBres, ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN, &temp );
3599 : // acorr_v2a *= temp;
3600 4283 : L_tmp = Mpy_32_16_1( L_tmp, temp ); /* 31 - tmp_e + Q_shb - 15 */
3601 :
3602 : // alpha = 25.0f;
3603 4283 : alpha_fx = -1677721600; // Q26
3604 4283 : move32();
3605 :
3606 : // acorr_v2a = 2.0f / ( 1.0f + (float) expf( -alpha * acorr_v2a ) ) - 1.0f;
3607 4283 : Word32 exp_tmp = 1459366444;
3608 4283 : Word16 exp_e = 2;
3609 4283 : Word16 res_e = 0;
3610 4283 : move32();
3611 4283 : move16();
3612 4283 : move16();
3613 :
3614 4283 : L_tmp = Mpy_32_32( alpha_fx, L_tmp ); // 31 - tmp_e + Q_shb - 15 - 5
3615 :
3616 4283 : tmp_e = sub( 31, sub( sub( add( sub( 31, tmp_e ), Q_shb ), 15 ), 5 ) );
3617 :
3618 : // L_tmp = BASOP_Util_fPow(exp_tmp, exp_e, 2, 31, &res_e);
3619 :
3620 4283 : L_tmp = BASOP_Util_fPow( exp_tmp, exp_e, L_tmp, tmp_e, &res_e );
3621 4283 : L_tmp = BASOP_Util_Add_Mant32Exp( ONE_IN_Q31, 0, L_tmp, res_e, &tmp_e );
3622 4283 : acorr_v2a_fx = BASOP_Util_Divide3232_Scale( 2, L_tmp, &res_e );
3623 :
3624 4283 : res_e = sub( add( res_e, 31 ), tmp_e );
3625 :
3626 4283 : res_e = BASOP_Util_Add_MantExp( acorr_v2a_fx, res_e, -1, 15, &acorr_v2a_fx );
3627 :
3628 : /* limit in the range 0.0 - 1.0 */
3629 : // acorr_v2a = min( 1.0f, max( 0.0f, acorr_v2a ) );
3630 4283 : acorr_v2a_fx = shl_sat( acorr_v2a_fx, res_e );
3631 :
3632 4283 : if ( acorr_v2a_fx < 0 )
3633 : {
3634 9 : acorr_v2a_fx = 0;
3635 9 : move16();
3636 : }
3637 :
3638 4283 : hBWE_TD->prev_shb_env_tilt_fx = shb_env_tilt_fx;
3639 4283 : move16();
3640 4283 : hBWE_TD->prev_Q_shb = Q_shb;
3641 4283 : move16();
3642 : }
3643 :
3644 : /* Calculate the 6 to 14 kHz (or 7.5 - 15.5 kHz) SHB excitation signal from the ACELP core excitation */
3645 115109 : vf_ind_fx = 20;
3646 115109 : move16();
3647 :
3648 : /* Save the SWB LSP values from current frame for interpolation */
3649 :
3650 : /* -------- start of memory rescaling -------- */
3651 : /* ----- calculate optimum Q_bwe_exc and rescale memories accordingly ----- */
3652 115109 : Lmax = L_deposit_l( 0 );
3653 73784869 : FOR( cnt = 0; cnt < L_FRAME32k; cnt++ )
3654 : {
3655 73669760 : Lmax = L_max( Lmax, L_abs( bwe_exc_extended[cnt] ) );
3656 : }
3657 115109 : Q_bwe_exc = norm_l( Lmax );
3658 115109 : if ( Lmax == 0 )
3659 : {
3660 0 : Q_bwe_exc = 31;
3661 0 : move16();
3662 : }
3663 115109 : Q_bwe_exc = add( Q_bwe_exc, add( Q_new, Q_new ) );
3664 :
3665 : /* Account for any outliers in the memories from previous frame for rescaling to avoid saturation */
3666 115109 : find_max_mem_enc( st_fx, &n_mem, &n_mem2 );
3667 :
3668 115109 : tmp2 = add( st_fx->prev_Q_bwe_exc, n_mem );
3669 115109 : if ( GT_16( Q_bwe_exc, tmp2 ) )
3670 : {
3671 43397 : Q_bwe_exc = tmp2;
3672 43397 : move16();
3673 : }
3674 :
3675 : /* rescale the memories if Q_bwe_exc is different from previous frame */
3676 115109 : sc = sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc );
3677 115109 : IF( sc != 0 )
3678 : {
3679 35944 : rescale_genSHB_mem_enc( st_fx, sc );
3680 : }
3681 :
3682 115109 : sc = sub( Q_bwe_exc, add( Q_new, Q_new ) );
3683 : /* rescale the bwe_exc_extended and bring it to 16-bit single precision with dynamic norm */
3684 75166177 : FOR( cnt = 0; cnt < L_FRAME32k + NL_BUFF_OFFSET; cnt++ )
3685 : {
3686 75051068 : bwe_exc_extended_16[cnt] = round_fx_sat( L_shl_sat( bwe_exc_extended[cnt], sc ) );
3687 75051068 : move16();
3688 : }
3689 :
3690 : /* state_syn_shbexc_fx is kept at (st_fx->prev_Q_bwe_syn) for 24.4/32kbps or is kept at Q_bwe_exc for 13.2/16.4kbps */
3691 115109 : Copy( hBWE_TD->state_syn_shbexc_fx, shaped_shb_excitation_fx, L_SHB_LAHEAD );
3692 :
3693 : /* save the previous Q factor of the buffer */
3694 115109 : st_fx->prev_Q_bwe_exc = Q_bwe_exc;
3695 115109 : move16();
3696 :
3697 115109 : Q_bwe_exc = sub( Q_bwe_exc, 16 ); /* Q_bwe_exc reflecting the single precision dynamic norm-ed buffers from here */
3698 :
3699 : /* -------- end of rescaling memories -------- */
3700 115109 : Q_bwe_exc_fb = hBWE_TD->prev_Q_bwe_exc_fb;
3701 115109 : move16();
3702 :
3703 115109 : GenShapedSHBExcitation_ivas_enc_fx( shaped_shb_excitation_fx + L_SHB_LAHEAD, lpc_shb_fx, White_exc16k_fx, hBWE_TD->mem_csfilt_fx,
3704 115109 : hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->state_lpc_syn_fx, st_fx->coder_type, bwe_exc_extended_16, hBWE_TD->bwe_seed,
3705 115109 : vf_modified_fx, st_fx->extl, &( hBWE_TD->tbe_demph_fx ), &( hBWE_TD->tbe_premph_fx ), lpc_shb_sf_fx, shb_ener_sf_Q31,
3706 115109 : shb_res_gshape_fx, shb_res_fx, &vf_ind_fx, formant_fac_fx, hBWE_TD->fb_state_lpc_syn_fx, &( hBWE_TD->fb_tbe_demph_fx ), &Q_bwe_exc,
3707 115109 : &Q_bwe_exc_fb, Q_shb, n_mem2, st_fx->prev_Q_bwe_syn, st_fx->total_brate, 0, st_fx->element_mode, st_fx->flag_ACELP16k, nlExc16k_fx,
3708 : nlExc16k_e, mixExc16k_fx, mixExc16k_e, st_fx->extl_brate, MSFlag, EnvSHBres_4k_norm_fx, Q_EnvSHBres_4k_norm,
3709 : &( hBWE_TD->prev_pow_exc16kWhtnd_fx32 ), &( hBWE_TD->prev_mix_factor_fx ), &Env_error_fx, Env_error_part_fx );
3710 :
3711 115109 : *Q_white_exc = Q_bwe_exc_fb;
3712 115109 : move16();
3713 115109 : IF( EQ_16( st_fx->extl, FB_TBE ) )
3714 : {
3715 43652 : hBWE_TD->prev_Q_bwe_exc_fb = Q_bwe_exc_fb;
3716 43652 : move16();
3717 : }
3718 : ELSE
3719 : {
3720 : /*Indirectly a memory reset of FB memories for next frame such that rescaling of memories would lead to 0 due to such high prev. Q value.
3721 : 51 because of 31 + 20(shift of Q_bwe_exc_fb before de-emphasis)*/
3722 71457 : hBWE_TD->prev_Q_bwe_exc_fb = 51;
3723 71457 : move16();
3724 : }
3725 :
3726 : /* fill-in missing SHB excitation */
3727 115109 : test();
3728 115109 : test();
3729 115109 : IF( ( EQ_16( st_fx->element_mode, IVAS_CPE_TD ) || EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) ) && LE_32( st_fx->last_core_brate, SID_2k40 ) )
3730 : {
3731 194 : Copy( shaped_shb_excitation_fx + L_SHB_LAHEAD, shaped_shb_excitation_fx, L_SHB_LAHEAD );
3732 : }
3733 :
3734 115109 : test();
3735 115109 : IF( GE_16( st_fx->element_mode, IVAS_CPE_DFT ) && hStereoICBWE != NULL )
3736 : {
3737 24220 : Copy( shaped_shb_excitation_fx + L_SHB_LAHEAD, hStereoICBWE->shbSynthRef_fx, L_FRAME16k );
3738 24220 : hStereoICBWE->shbSynthRef_e = sub( 15, Q_bwe_exc );
3739 24220 : move16();
3740 : }
3741 :
3742 115109 : test();
3743 115109 : test();
3744 115109 : IF( GE_32( st_fx->extl_brate, SWB_TBE_2k8 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
3745 : {
3746 29341 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
3747 : {
3748 0 : hBWE_TD->idx_mixFac = vf_ind_fx;
3749 0 : move16();
3750 : }
3751 : ELSE
3752 : {
3753 29341 : push_indice( hBstr, IND_SHB_VF, vf_ind_fx, NUM_BITS_SHB_VF );
3754 : }
3755 : }
3756 :
3757 1266199 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
3758 : {
3759 1151090 : hBWE_TD->mem_stp_swb_fx[i] = shl_sat( hBWE_TD->mem_stp_swb_fx[i], sub( Q_bwe_exc, st_fx->prev_Q_bwe_syn ) );
3760 1151090 : move16();
3761 : }
3762 :
3763 115109 : test();
3764 115109 : IF( NE_32( st_fx->extl_brate, SWB_TBE_1k10 ) && NE_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
3765 110826 : {
3766 :
3767 554130 : FOR( i = 0; i < L_FRAME16k; i += L_SUBFR16k )
3768 : {
3769 443304 : PostShortTerm_ivas_enc_fx( &shaped_shb_excitation_fx[L_SHB_LAHEAD + i], lpc_shb_fx, &shaped_shb_excitationTemp_fx[i], hBWE_TD->mem_stp_swb_fx,
3770 443304 : hBWE_TD->ptr_mem_stp_swb_fx, &( hBWE_TD->gain_prec_swb_fx ), hBWE_TD->mem_zero_swb_fx, formant_fac_fx );
3771 : /* i: shaped_shb_excitation_fx in Q_bwe_exc */
3772 : /* i: lpc_shb_fx in Q12 */
3773 : }
3774 110826 : Copy( shaped_shb_excitationTemp_fx, &shaped_shb_excitation_fx[L_SHB_LAHEAD], L_FRAME16k );
3775 :
3776 : Word16 max_val;
3777 110826 : maximum_abs_16_fx( shaped_shb_excitation_fx, L_FRAME16k + L_SHB_LAHEAD, &max_val );
3778 110826 : IF( max_val == 0 )
3779 : {
3780 0 : Lscale = ONE_IN_Q31; /* 1.0f in Q31 */
3781 0 : move32();
3782 0 : IF( GT_16( voice_factors_fx[0], 24576 /*0.75f Q15*/ ) )
3783 : {
3784 0 : Lscale = ONE_IN_Q30; /* sqrtf(0.25) = 0.5 in Q31 */
3785 0 : move32();
3786 : }
3787 0 : exp = 0;
3788 0 : move16();
3789 : }
3790 : ELSE
3791 : {
3792 : Word64 prev_pow_64fx, curr_pow_64fx;
3793 : Word16 w_shift;
3794 110826 : tmp = sub( shl( Q_bwe_exc, 1 ), 31 );
3795 110826 : prev_pow_64fx = W_shl( 21475ll /*0.00001f Q31*/, tmp ); /* 2*(Q_bwe_exc) */
3796 110826 : curr_pow_64fx = W_shl( 21475ll /*0.00001f Q31*/, tmp ); /* 2*(Q_bwe_exc) */
3797 3435606 : FOR( i = 0; i < L_SHB_LAHEAD + 10; i++ )
3798 : {
3799 3324780 : prev_pow_64fx = W_mac0_16_16( prev_pow_64fx, shaped_shb_excitation_fx[i], shaped_shb_excitation_fx[i] ); /* 2*Q_bwe_exc */
3800 3324780 : curr_pow_64fx = W_mac0_16_16( curr_pow_64fx, shaped_shb_excitation_fx[i + L_SHB_LAHEAD + 10], shaped_shb_excitation_fx[i + L_SHB_LAHEAD + 10] ); /* 2*Q_bwe_exc */
3801 : }
3802 :
3803 110826 : IF( GT_16( voice_factors_fx[0], 24576 /*0.75f Q15*/ ) )
3804 : {
3805 : /*curr_pow_fx = Mult_32_16( curr_pow_fx, 8192);*/ /* Q(2*Q_bwe_exc) */
3806 3267 : curr_pow_64fx = W_shr( curr_pow_64fx, 2 ); /* scale by 0.25 */
3807 : }
3808 :
3809 110826 : w_shift = s_min( W_norm( prev_pow_64fx ), W_norm( curr_pow_64fx ) );
3810 110826 : prev_pow_fx = W_extract_h( W_shl( prev_pow_64fx, w_shift ) ); // 2*(Q_bwe_exc)+w_shift-32
3811 110826 : curr_pow_fx = W_extract_h( W_shl( curr_pow_64fx, w_shift ) ); // 2*(Q_bwe_exc)+w_shift-32
3812 110826 : w_shift = sub( add( shl( Q_bwe_exc, 1 ), w_shift ), 32 );
3813 :
3814 110826 : Lscale = root_a_over_b_fx( curr_pow_fx,
3815 : w_shift,
3816 : prev_pow_fx,
3817 : w_shift,
3818 : &exp );
3819 : }
3820 :
3821 2327346 : FOR( i = 0; i < L_SHB_LAHEAD; i++ )
3822 : {
3823 2216520 : L_tmp = Mult_32_16( Lscale, shaped_shb_excitation_fx[i] ); /* Q(16-exp+Q_bwe_exc) */
3824 2216520 : shaped_shb_excitation_fx[i] = round_fx_o( L_shl_o( L_tmp, exp, &Overflow ), &Overflow ); /* Q_bwe_exc */
3825 2216520 : move16();
3826 : }
3827 110826 : IF( exp < 0 )
3828 : {
3829 2358 : Lscale = L_shl( Lscale, exp );
3830 2358 : exp = 0;
3831 2358 : move16();
3832 : }
3833 1219086 : FOR( ; i < L_SHB_LAHEAD + 10; i++ )
3834 : {
3835 1108260 : tmp = i_mult_o( sub( i, 19 ), 3277 /*0.1f Q15*/, &Overflow ); /* Q15 */
3836 1108260 : L_tmp1 = Mult_32_16( L_shl_o( 1, sub( 31, exp ), &Overflow ), tmp ); /* Q31-exp */
3837 1108260 : tmp = sub( 32767 /*1.0f Q15*/, tmp );
3838 1108260 : L_tmp = L_add( Mult_32_16( Lscale, tmp ), L_tmp1 );
3839 1108260 : L_tmp = Mult_32_16( L_tmp, shaped_shb_excitation_fx[i] ); /* Q_bwe_exc + (31-exp) - 15 */
3840 1108260 : shaped_shb_excitation_fx[i] = round_fx_o( L_shl_o( L_tmp, exp, &Overflow ), &Overflow ); /* Q_bwe_exc */
3841 1108260 : move16();
3842 : }
3843 : }
3844 : ELSE
3845 : {
3846 : /* reset the PF memories if the PF is not running */
3847 4283 : set16_fx( hBWE_TD->mem_stp_swb_fx, 0, LPC_SHB_ORDER );
3848 4283 : hBWE_TD->gain_prec_swb_fx = ONE_IN_Q14;
3849 4283 : move16();
3850 4283 : set16_fx( hBWE_TD->mem_zero_swb_fx, 0, LPC_SHB_ORDER );
3851 : }
3852 :
3853 : /* Update SHB excitation */
3854 115109 : Copy( shaped_shb_excitation_fx + L_FRAME16k, hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD ); /* Q_bwe_exc */
3855 :
3856 : /* Estimate the gain-shape parameter */
3857 115109 : n_subfr_saturation = 0;
3858 115109 : move16();
3859 115109 : EstimateSHBGainShape_fx( SHB_OVERLAP_LEN, shb_frame_fx, Q_shb, shaped_shb_excitation_fx,
3860 : Q_bwe_exc, GainShape_fx, subwin_shb_fx, &n_subfr_saturation );
3861 :
3862 115109 : test();
3863 115109 : IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
3864 : {
3865 : /* Gain shape attenuation in case of big error in envelope modelling */
3866 21415 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
3867 : {
3868 17132 : IF( GT_16( Env_error_part_fx[i], 100 ) )
3869 : {
3870 : /* set gain shape to the average of the remaining gains */
3871 : // GainShape[i] = ( sum_f( GainShape, NUM_SHB_SUBGAINS ) - GainShape[i] ) / ( NUM_SHB_SUBGAINS - 1 );
3872 59 : L_tmp = L_sub( sum16_32_fx( GainShape_fx, NUM_SHB_SUBGAINS ), GainShape_fx[i] );
3873 59 : tmp = BASOP_Util_Divide3232_Scale( L_tmp, NUM_SHB_SUBGAINS - 1, &exp );
3874 59 : GainShape_fx[i] = shl( tmp, sub( exp, 15 ) ); /* Q15 */
3875 59 : move16();
3876 : }
3877 : }
3878 : }
3879 :
3880 115109 : test();
3881 115109 : IF( NE_32( st_fx->extl_brate, SWB_TBE_1k10 ) && NE_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
3882 : {
3883 : /* Gain shape BWS/high band low energy fix */
3884 110826 : IF( LT_16( hBWE_TD->cldfbHBLT, 8192 /*1.0f Q13*/ ) ) /* cldfbHBLT in Q13 */
3885 : {
3886 : /* There is not much HB past 10kHz; the temporal resolution is quite coarse, so reduce the dynamic range */
3887 8215 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
3888 : {
3889 : /* 0.35f +/- delta variation; delta = 0.1*(GS-0.35)*/
3890 : /* GainShape[i] = 0.315f + 0.1f * GainShape[i]; */
3891 6572 : GainShape_fx[i] = mac_r( 676457349l /*0.315 Q31*/, 3277 /*0.1 Q15*/, GainShape_fx[i] );
3892 6572 : move16();
3893 : }
3894 : }
3895 : }
3896 :
3897 : /* high-band gain control in case of BWS */
3898 115109 : test();
3899 115109 : IF( NE_32( st_fx->extl_brate, SWB_TBE_1k10 ) && NE_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
3900 : {
3901 110826 : IF( st_fx->bwidth_sw_cnt > 0 )
3902 : {
3903 3325 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
3904 : {
3905 : // GainShape[i] *= ( (float) ( st->bwidth_sw_cnt ) / (float) BWS_TRAN_PERIOD );
3906 2660 : tmp = i_mult( st_fx->bwidth_sw_cnt, ONE_BY_BWS_TRAN_PERIOD_Q15 );
3907 2660 : GainShape_fx[i] = mult( GainShape_fx[i], tmp );
3908 2660 : move16();
3909 : }
3910 : }
3911 : }
3912 :
3913 : /* Gain frame adjustment factor */
3914 : /* log( (GainShape[0]) / (st->prev_wb_GainShape) )*/
3915 115109 : IF( hBWE_TD->prev_swb_GainShape_fx == 0 )
3916 : {
3917 3119 : exp = 13 /* norm_s(3) */;
3918 3119 : tmp = 21845 /* div_s( shl(1, sub(14, exp)), 3 /\* 0.0001 in Q15 *\/ ) */;
3919 : }
3920 : ELSE
3921 : {
3922 111990 : exp = norm_s( hBWE_TD->prev_swb_GainShape_fx );
3923 111990 : tmp = div_s( shl( 1, sub( 14, exp ) ), hBWE_TD->prev_swb_GainShape_fx );
3924 : }
3925 :
3926 115109 : L_tmp = L_mult( GainShape_fx[0], tmp ); /*Q(30 - exp) */
3927 :
3928 115109 : exp1 = norm_l( L_tmp );
3929 115109 : frac = Log2_norm_lc( L_shl( L_tmp, exp1 ) ); /*move16(); */
3930 115109 : exp1 = sub( exp, exp1 ); /*move16(); */
3931 115109 : L_tmp = Mpy_32_16( exp1, frac, 22713 );
3932 115109 : temp_swb_fac = round_fx( L_shl( L_tmp, 10 ) );
3933 :
3934 115109 : L_feedback = L_mult( temp_swb_fac, temp_swb_fac );
3935 :
3936 :
3937 460436 : FOR( i = 1; i < NUM_SHB_SUBGAINS; i++ )
3938 : {
3939 345327 : test();
3940 345327 : IF( GainShape_fx[i - 1] == 0 )
3941 : {
3942 0 : exp = 13 /* norm_s(3) */;
3943 0 : tmp = 21845 /* div_s( shl(1, sub(14, exp)), 3 /\* 0.0001 in Q15 *\/ ) */;
3944 : }
3945 : ELSE
3946 : {
3947 345327 : exp = norm_s( GainShape_fx[i - 1] );
3948 345327 : tmp = div_s( shl( 1, sub( 14, exp ) ), GainShape_fx[i - 1] );
3949 : }
3950 345327 : L_tmp = L_mult( GainShape_fx[i], tmp ); /* Q(30 - exp) */
3951 :
3952 345327 : exp1 = norm_l( L_tmp );
3953 345327 : frac = Log2_norm_lc( L_shl( L_tmp, exp1 ) );
3954 345327 : exp1 = sub( exp, exp1 );
3955 345327 : L_tmp = Mpy_32_16( exp1, frac, 22713 );
3956 345327 : temp_swb_fac = round_fx( L_shl( L_tmp, 10 ) );
3957 :
3958 345327 : L_feedback = L_mac( L_feedback, temp_swb_fac, temp_swb_fac );
3959 : }
3960 :
3961 : /* feedback = 0.4f / (1 + 0.5f * feedback) */
3962 115109 : L_tmp = L_add( L_shr( L_feedback, 1 ), L_shl( 1, 21 ) ); /* Q21 */
3963 115109 : IF( L_tmp != 0 )
3964 : {
3965 115109 : exp = norm_l( L_tmp );
3966 115109 : tmp = extract_h( L_shl( L_tmp, exp ) );
3967 115109 : exp = sub( sub( 30, exp ), 21 );
3968 115109 : tmp = div_s( 16384, tmp ); /*Q(15+exp) */
3969 115109 : L_tmp = L_shr( L_mult( 13107, tmp ), exp ); /*Q31 */
3970 115109 : feedback = round_fx( L_tmp ); /*Q15 */
3971 : }
3972 : ELSE
3973 : {
3974 0 : feedback = 8738;
3975 0 : move16(); /*Q15 */
3976 : }
3977 :
3978 : /* calculate tilt in all sub-frame gains */
3979 115109 : Copy( GainShape_fx, GainShape_Interp_fx, NUM_SHB_SUBGAINS );
3980 115109 : lls_interp_n_fx( GainShape_Interp_fx, NUM_SHB_SUBGAINS, &GainShape_tilt_fx, &temp, 1 );
3981 :
3982 115109 : test();
3983 115109 : IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
3984 : {
3985 : // 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)
3986 4283 : test();
3987 4283 : test();
3988 4283 : test();
3989 4283 : test();
3990 4283 : IF( LT_16( acorr_v2a_fx, 13107 ) /* 0.4f in Q15 */ && GE_16( vf_ind_fx, 5 ) && LT_16( abs_s( GainShape_tilt_fx ), 6554 /* 0.2f in Q15 */ ) && LT_32( L_deposit_l( shb_env_tilt_fx ), L_shl( 500, Q_shb ) ) && LT_32( L_deposit_l( hBWE_TD->prev_shb_env_tilt_fx ), L_shl( 500, hBWE_TD->prev_Q_shb ) ) )
3991 : {
3992 : /* stronger smoothing in case of unvoiced SHB residual signal with gaussian excitation */
3993 : // feedback = lin_interp(acorr_v2a, 0.4f, 0.0f, 0.0f, 0.95f, 0);
3994 2735 : feedback = lin_interp_ivas_fx( acorr_v2a_fx, 13107 /* 0.4f in Q15 */, 0, 0, 31130 /* 0.95f in Q15 */, 0 );
3995 : // feedback = min( max( feedback, 0.0f ), 1.0f );
3996 2735 : feedback = s_min( s_max( feedback, 0 ), MAX16B );
3997 :
3998 13675 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
3999 : {
4000 : // GainShape[i] = ( 1 - feedback ) * GainShape[i] + feedback * GainShape_Interp[i];
4001 10940 : GainShape_fx[i] = extract_h( L_add( L_mult( sub( MAX16B, feedback ), GainShape_fx[i] ), L_mult( feedback, GainShape_Interp_fx[i] ) ) );
4002 10940 : move16();
4003 : }
4004 : }
4005 : }
4006 : ELSE
4007 : {
4008 : /* voicing in Q15 */
4009 110826 : L_tmp = L_mult( voicing[0], 8192 );
4010 110826 : L_tmp = L_mac( L_tmp, voicing[1], 8192 );
4011 110826 : L_tmp = L_mac( L_tmp, voicing[2], 8192 );
4012 110826 : tmp = sum1 = round_fx( L_tmp ); /* sum1 used again below - Q13 */
4013 110826 : tmp = add( tmp, 1 ); /* To ensure change is BE */
4014 :
4015 : /* voice_factors_fx in Q15 */
4016 110826 : L_tmp = L_mult( voice_factors_fx[0], 8192 );
4017 110826 : L_tmp = L_mac( L_tmp, voice_factors_fx[1], 8192 );
4018 110826 : L_tmp = L_mac( L_tmp, voice_factors_fx[2], 8192 );
4019 110826 : L_tmp = L_mac( L_tmp, voice_factors_fx[3], 8192 );
4020 110826 : tmp1 = sum2 = round_fx( L_tmp ); /* sum2 used again below - Q13 */
4021 :
4022 :
4023 110826 : test();
4024 110826 : test();
4025 110826 : IF( EQ_16( frGainAttenuate, 1 ) || ( GT_16( tmp, 19661 /* 2.4f in Q13 */ ) && GT_16( tmp1, 6554 /* 0.8f in Q13 */ ) ) )
4026 :
4027 : {
4028 30954 : temp_swb_fac = hBWE_TD->prev_swb_GainShape_fx;
4029 30954 : move16();
4030 154770 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
4031 : {
4032 : /*GainShape_fx[i] = (1 - feedback) * GainShape[i] + feedback * temp_swb_fac; */
4033 123816 : GainShape_fx[i] = round_fx( L_mac( L_mult( sub( 32767, feedback ), GainShape_fx[i] ), feedback, temp_swb_fac ) );
4034 123816 : move16();
4035 123816 : temp_swb_fac = GainShape_fx[i];
4036 123816 : move16();
4037 : }
4038 : }
4039 : }
4040 :
4041 115109 : test();
4042 115109 : IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
4043 : {
4044 : /* Re-normalize gain shape before quantization */
4045 : // sum_gain = sum2_f(GainShape, NUM_SHB_SUBGAINS);
4046 4283 : tmp = 0;
4047 4283 : move16();
4048 4283 : sum_gain_fx = sum2_16_exp_fx( GainShape_fx, NUM_SHB_SUBGAINS, &tmp, 2 );
4049 4283 : IF( sum_gain_fx == 0 )
4050 : {
4051 0 : normFact_fx = 0;
4052 0 : move16();
4053 : }
4054 : ELSE
4055 : {
4056 : // normFact = (float) sqrt( 1.0f / sum_gain );
4057 4283 : normFact_fx = ISqrt32( sum_gain_fx, &tmp );
4058 : }
4059 :
4060 : // v_multc( GainShape, normFact, GainShape, NUM_SHB_SUBGAINS );
4061 21415 : FOR( i = 0; i < NUM_SHB_SUBGAINS; ++i )
4062 : {
4063 17132 : GainShape_fx[i] = extract_h( L_shl( Mpy_32_16_1( normFact_fx, GainShape_fx[i] ), tmp ) ); /* Q15 */
4064 17132 : move16();
4065 : }
4066 : }
4067 :
4068 :
4069 115109 : hBWE_TD->prev_swb_GainShape_fx = GainShape_fx[3];
4070 115109 : move16();
4071 :
4072 : /* Compute the power of gains away from the peak gain prior to quantization */
4073 115109 : IF( st_fx->element_mode > EVS_MONO )
4074 : {
4075 115109 : p2m_in_fx = pow_off_pk_corrected_fx( GainShape_fx, NUM_SHB_SUBGAINS, 1 );
4076 : }
4077 : ELSE
4078 : {
4079 0 : p2m_in_fx = pow_off_pk_fx( GainShape_fx, NUM_SHB_SUBGAINS, 1 );
4080 : }
4081 :
4082 : /* Quantization of the gain shape parameter */
4083 115109 : QuantizeSHBsubgains_ivas_fx( st_fx, GainShape_fx, st_fx->extl );
4084 : /* o: GainShape_fx in Q15 */
4085 :
4086 : /* Compute the power of gains away from the peak gain after quantization */
4087 115109 : IF( st_fx->element_mode > EVS_MONO )
4088 : {
4089 115109 : p2m_out_fx = pow_off_pk_corrected_fx( GainShape_fx, NUM_SHB_SUBFR, 4 );
4090 : }
4091 : ELSE
4092 : {
4093 0 : p2m_out_fx = pow_off_pk_fx( GainShape_fx, NUM_SHB_SUBFR, 4 );
4094 : }
4095 :
4096 : /* Gain shape smoothing after quantization */
4097 115109 : IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
4098 : {
4099 21415 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
4100 : {
4101 17132 : GainShape_Interp_fx[i] = GainShape_fx[i * NUM_SHB_SUBGAINS];
4102 17132 : move16();
4103 : }
4104 :
4105 4283 : lls_interp_n_fx( GainShape_Interp_fx, NUM_SHB_SUBGAINS, &GainShape_tilt_fx, &temp, 1 );
4106 :
4107 : // if ( vf_ind >= 6 && fabs( GainShape_tilt ) < 0.12f )
4108 4283 : test();
4109 4283 : IF( GE_16( vf_ind_fx, 6 ) && LT_16( abs_s( GainShape_tilt_fx ), 3932 /* 0.12f in Q15 */ ) )
4110 : {
4111 : // feedback = 0.3f;
4112 1867 : feedback = 9830; /* 0.3f in Q15 */
4113 1867 : move16();
4114 9335 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
4115 : {
4116 : // GainShape[i] = ( 1 - feedback ) * GainShape[i * NUM_SHB_SUBGAINS] + feedback * GainShape_Interp[i];
4117 7468 : GainShape_fx[i] = extract_h( L_add( L_mult( sub( MAX16B, feedback ), GainShape_fx[i * NUM_SHB_SUBGAINS] ), L_mult( feedback, GainShape_Interp_fx[i] ) ) ); // Q15
4118 7468 : move16();
4119 : }
4120 :
4121 29872 : FOR( i = NUM_SHB_SUBFR - 1; i > 0; i-- )
4122 : {
4123 28005 : GainShape_fx[i] = GainShape_fx[i * NUM_SHB_SUBGAINS / NUM_SHB_SUBFR];
4124 28005 : move16();
4125 : }
4126 : }
4127 : }
4128 :
4129 : /* Estimate the gain parameter */
4130 115109 : EstimateSHBFrameGain_ivas_fx( SHB_OVERLAP_LEN, shb_frame_fx, Q_shb, shaped_shb_excitation_fx, Q_bwe_exc,
4131 : GainShape_fx, &GainFrame_fx, window_shb_fx, subwin_shb_fx, n_subfr_saturation );
4132 :
4133 115109 : IF( EQ_16( st_fx->tec_tfa, 1 ) )
4134 : {
4135 0 : tfaCalcEnv_fx( shb_frame_fx, st_fx->tfa_enr );
4136 : }
4137 :
4138 : /* If there's a big difference in the power of gains away from the peak gain */
4139 : /* due to poor quantization then suppress energy of the high band. */
4140 115109 : IF( GT_32( p2m_out_fx, L_shl( p2m_in_fx, 1 ) ) )
4141 : {
4142 1625 : L_tmp = root_a_over_b_fx( p2m_in_fx, 28, p2m_out_fx, 29, &exp_out );
4143 1625 : GainFrame_fx = L_shl( Mult_32_32( GainFrame_fx, L_tmp ), exp_out ); /* Q18 */
4144 : }
4145 :
4146 : // if ( st_fx->element_mode > EVS_MONO && st_fx->L_frame != st_fx->last_L_frame && hBWE_TD->prev_gainFr_SHB != 0 && ( st_fx->last_extl == SWB_TBE || st_fx->last_extl == FB_TBE ) && st_fx->coder_type == TRANSITION && st_fx->coder_type_raw != VOICED && st_fx->clas == VOICED_CLAS && st_fx->last_clas == VOICED_CLAS && ( 3.0f * voice_factors[0] < voice_factors[( st_fx->L_frame >> 6 ) - 1] ) )
4147 115109 : test();
4148 115109 : test();
4149 115109 : test();
4150 115109 : test();
4151 115109 : test();
4152 115109 : test();
4153 115109 : test();
4154 115109 : test();
4155 115109 : test();
4156 115109 : IF( st_fx->element_mode > EVS_MONO && NE_16( st_fx->L_frame, st_fx->last_L_frame ) && hBWE_TD->prev_gainFr_SHB_fx != 0 && ( EQ_16( st_fx->last_extl, SWB_TBE ) || EQ_16( st_fx->last_extl, FB_TBE ) ) && EQ_16( st_fx->coder_type, TRANSITION ) && NE_16( st_fx->coder_type_raw, VOICED ) && EQ_16( st_fx->clas, VOICED_CLAS ) && EQ_16( st_fx->last_clas, VOICED_CLAS ) && LT_16( mult( 24576 /* 3.0f in Q13 */, voice_factors_fx[0] ), shr( voice_factors_fx[( st_fx->L_frame >> 6 ) - 1], 2 ) ) )
4157 : {
4158 : // float fac = GainFrame / hBWE_TD->prev_gainFr_SHB;
4159 47 : Word16 fac = BASOP_Util_Divide3232_Scale( GainFrame_fx, hBWE_TD->prev_gainFr_SHB_fx, &tmp_e );
4160 :
4161 : // if ( fac > 4.0f )
4162 47 : IF( BASOP_Util_Cmp_Mant32Exp( L_deposit_h( fac ), tmp_e, 1073741824, 3 ) > 0 )
4163 : {
4164 : // GainFrame = 4.0f * GainFrame / fac;
4165 0 : GainFrame_fx = BASOP_Util_Divide3216_Scale( GainFrame_fx, fac, &tmp1_e );
4166 0 : GainFrame_fx = L_shl( GainFrame_fx, add( tmp1_e, 2 ) ); /* Q18 */
4167 : }
4168 : }
4169 :
4170 115109 : test();
4171 115109 : IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
4172 : {
4173 : // if ( acorr_v2a > 0.1f && Env_error > 5.0f )
4174 4283 : test();
4175 4283 : test();
4176 4283 : IF( GT_16( acorr_v2a_fx, 3277 /* 0.1f in Q15 */ ) && GT_16( Env_error_fx, 5 ) )
4177 : {
4178 : /* attenuate frame gain in case of voiced SHB residual signal with gaussian excitation */
4179 : // fact_atten = lin_interp( Env_error, 5.0f, 1.0f, 25.0f, 0.2f, 1 );
4180 0 : fact_atten_fx = lin_interp_ivas_fx( Env_error_fx, 5, MAX16B, 25, 6554 /* 0.2f in Q15 */, 1 );
4181 : // GainFrame *= fact_atten;
4182 0 : GainFrame_fx = Mpy_32_16_1( GainFrame_fx, fact_atten_fx );
4183 : }
4184 : /* Frame gain attenuation in case of big error in envelope modelling */
4185 : // else if ( Env_error > 100.0f || hBWE_TD->prev_Env_error > 100.0f )
4186 4283 : ELSE IF( GT_16( Env_error_fx, 100 ) || GT_16( hBWE_TD->prev_Env_error_fx, 100 ) )
4187 : {
4188 : // if ( Env_error > 100.0f )
4189 134 : IF( GT_16( Env_error_fx, 100 ) )
4190 : {
4191 : // fact_atten = lin_interp( Env_error, 100.0f, 1.0f, 300.0f, 0.1f, 1 );
4192 72 : fact_atten_fx = lin_interp_ivas_fx( Env_error_fx, 100, MAX16B, 300, 3277 /* 0.1f in Q15 */, 1 );
4193 : }
4194 : ELSE
4195 : {
4196 : // fact_atten = lin_interp( hBWE_TD->prev_Env_error, 100.0f, 1.0f, 300.0f, 0.5f, 1 );
4197 62 : fact_atten_fx = lin_interp_ivas_fx( hBWE_TD->prev_Env_error_fx, 100, MAX16B, 300, ONE_IN_Q14 /* 0.5f in Q15 */, 1 );
4198 : }
4199 : // GainFrame *= fact_atten;
4200 134 : GainFrame_fx = Mpy_32_16_1( GainFrame_fx, fact_atten_fx );
4201 : }
4202 : }
4203 : ELSE
4204 : {
4205 110826 : test();
4206 110826 : IF( EQ_16( frGainSmoothEn, 1 ) && LT_32( hBWE_TD->prev_gainFr_SHB_fx, GainFrame_fx ) )
4207 : {
4208 0 : GainFrame_fx = L_add( L_shr( hBWE_TD->prev_gainFr_SHB_fx, 1 ), L_shr( GainFrame_fx, 1 ) ); /* Q18 */
4209 : }
4210 : }
4211 :
4212 115109 : test();
4213 115109 : test();
4214 115109 : IF( EQ_16( frGainAttenuate, 1 ) && LE_16( MA_lsp_shb_spacing, 79 /*0.0024f Q15*/ ) )
4215 : {
4216 17 : exp1 = norm_l( GainFrame_fx );
4217 17 : frac = Log2_norm_lc( L_shl( GainFrame_fx, exp1 ) );
4218 17 : exp = sub( 30, add( exp1, 21 ) );
4219 17 : L_tmp = Mpy_32_16( exp, frac, 26214 ); /* Q16 */
4220 17 : frac = L_Extract_lc( L_tmp, &exp );
4221 17 : GainFrame_fx = Pow2( 30, frac );
4222 17 : exp = sub( exp, 30 );
4223 17 : GainFrame_fx = L_shl( GainFrame_fx, add( exp, 18 ) ); /* Q18 */
4224 : }
4225 115092 : ELSE IF( EQ_16( hBWE_TD->prev_frGainAtten, 1 ) && GT_32( Mult_32_16( GainFrame_fx, 10923 ), hBWE_TD->prev_gainFr_SHB_fx ) )
4226 : {
4227 : /*GainFrame *= (0.8f + 0.5f*feedback); */
4228 29 : tmp = add( 13107 /* 0.8 in Q14 */, shr( feedback, 2 ) );
4229 29 : GainFrame_fx = L_shl( Mult_32_16( GainFrame_fx, tmp ), 1 ); /* Q18 */
4230 : }
4231 :
4232 115109 : hBWE_TD->prev_frGainAtten = frGainAttenuate;
4233 115109 : move16();
4234 :
4235 115109 : hBWE_TD->prev_gainFr_SHB_fx = GainFrame_fx;
4236 115109 : move16(); /* Q18 */
4237 :
4238 : // if ( GainFrame > 153.0f && st_fx->element_mode >= IVAS_CPE_DFT && hStereoICBWE != NULL )
4239 115109 : test();
4240 115109 : test();
4241 115109 : if ( GT_32( GainFrame_fx, 40108032 /* 153.0f in Q18 */ ) && GE_16( st_fx->element_mode, IVAS_CPE_DFT ) && hStereoICBWE != NULL )
4242 : {
4243 76 : hStereoICBWE->MSFlag = 1;
4244 76 : move16();
4245 : }
4246 :
4247 : /* Gain attenuation when the SWB LSF quantization error is larger than a threshold */
4248 115109 : tmp = mult_r( 25600 /*400 Q6*/, sd_uq_q_fx ); /* Q6 * Q15 => Q6 */
4249 :
4250 115109 : test();
4251 115109 : IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
4252 : {
4253 : // sd_uq_q = 1 - 0.7f * ( sd_uq_q * sd_uq_q );
4254 4283 : tmp1 = mult_r( 22938 /*0.7f Q15*/, tmp ); /* Q15, Q6 => Q6 */
4255 4283 : L_tmp = L_msu( 8192l /*1.0f Q13*/, tmp1, tmp ); /* Q13 */
4256 : }
4257 : ELSE
4258 : {
4259 110826 : IF( EQ_16( st_fx->L_frame, L_FRAME ) )
4260 : {
4261 52168 : tmp1 = mult_r( 6554 /*0.2f Q15*/, tmp ); /* Q15, Q6 => Q6 */
4262 52168 : L_tmp = L_msu( 8192l /*1.0f Q13*/, tmp1, tmp ); /* Q13 */
4263 : }
4264 : ELSE
4265 : {
4266 58658 : tmp1 = mult_r( 3277 /*0.1f Q15*/, tmp ); /* Q15, Q6 => Q6 */
4267 58658 : L_tmp = L_msu( 8192l /*1.0f Q13*/, tmp1, tmp ); /* Q13 */
4268 : }
4269 : }
4270 : /* sd_uq_q = max(min(sd_uq_q, 1.0f), 0.5f); */
4271 115109 : L_tmp = L_min( L_tmp, 8192l /*1.0f Q13*/ );
4272 115109 : L_tmp = L_max( L_tmp, 4096l /*0.5f Q13*/ ); /* Q13 */
4273 : /* keep the L_tmp; dont overwrite */
4274 :
4275 115109 : test();
4276 115109 : IF( NE_32( st_fx->extl_brate, SWB_TBE_1k10 ) && NE_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
4277 : {
4278 : /* pitBufAvg = 0.0025f * sum_f(pitch_buf, 4); */
4279 : /* pitch_buf: Q6 */
4280 110826 : pitBufAvg_fx = mult_r( pitch_buf[0], 1311 );
4281 110826 : pitBufAvg_fx = add( pitBufAvg_fx, mult_r( pitch_buf[1], 1311 ) );
4282 110826 : pitBufAvg_fx = add( pitBufAvg_fx, mult_r( pitch_buf[2], 1311 ) );
4283 110826 : pitBufAvg_fx = add( pitBufAvg_fx, mult_r( pitch_buf[3], 1311 ) ); /* Q10 */
4284 :
4285 : /* voicingBufAvg = (sum_f(voice_factors, 4)=sum2 > 0.6f) ? 0.333f : 0.1667f; */
4286 110826 : tmp2 = 5462 /*0.1667f Q15*/;
4287 110826 : move16();
4288 110826 : if ( GT_16( sum2, 4915 /*0.6f Q13*/ ) )
4289 : {
4290 65803 : tmp2 = 10912 /*0.333f Q15*/; /* Q15 */
4291 65803 : move16();
4292 : }
4293 110826 : voicingBufAvg_fx = shl( mult( tmp2, sum1 ), 2 ); /* Q15 */
4294 :
4295 : // if ( voicingBufAvg <= 0.0f && sd_uq_q * pitBufAvg != 0 )
4296 110826 : test();
4297 110826 : IF( voicingBufAvg_fx <= 0 && Mpy_32_16_1( L_tmp, pitBufAvg_fx ) != 0 )
4298 : {
4299 : // voicingBufAvg = sd_uq_q * pitBufAvg / 1.001f;
4300 13 : tmp = BASOP_Util_Divide1616_Scale( pitBufAvg_fx, 16400, &tmp_e );
4301 13 : tmp_e = sub( add( tmp_e, 5 ), 1 );
4302 13 : L_tmp1 = Mpy_32_16_1( L_tmp, tmp );
4303 13 : voicingBufAvg_fx = extract_h( L_tmp1 );
4304 : }
4305 : // else if ( voicingBufAvg <= 0.0f )
4306 110813 : ELSE IF( voicingBufAvg_fx <= 0 )
4307 : {
4308 : // voicingBufAvg = 1.0f;
4309 0 : voicingBufAvg_fx = MAX16B;
4310 0 : move16();
4311 : }
4312 : }
4313 :
4314 : /* high-band gain control in case of BWS */
4315 115109 : test();
4316 115109 : IF( NE_32( st_fx->extl_brate, SWB_TBE_1k10 ) && NE_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
4317 : {
4318 110826 : IF( st_fx->bwidth_sw_cnt > 0 )
4319 : {
4320 : // GainFrame *= ( (float) ( st_fx->bwidth_sw_cnt ) / (float) BWS_TRAN_PERIOD );
4321 665 : tmp = i_mult( st_fx->bwidth_sw_cnt, ONE_BY_BWS_TRAN_PERIOD_Q15 );
4322 665 : GainFrame_fx = Mpy_32_16_1( GainFrame_fx, tmp );
4323 : }
4324 : }
4325 :
4326 : /* Controlled gain evolution in SWB for stronger voiced segments */
4327 115109 : test();
4328 115109 : IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
4329 : {
4330 : // GainFrame *= sd_uq_q;
4331 4283 : GainFrame_fx = Mpy_32_16_1( GainFrame_fx, extract_l( L_shl( L_tmp, 2 ) ) );
4332 : }
4333 : ELSE
4334 : {
4335 : /* max(min((float)(sd_uq_q*pitBufAvg/voicingBufAvg), 1.0f), 0.6f) */
4336 : /* sd_uq_q: Q13, pitBufAvg_fx: Q6, voicingBufAvg_fx: Q15 */
4337 : /* 1/voicingBufAvg_fx */
4338 110826 : tmp = 32767;
4339 110826 : move16();
4340 110826 : IF( voicingBufAvg_fx > 0 )
4341 : {
4342 110813 : exp = norm_s( voicingBufAvg_fx );
4343 110813 : tmp = div_s( shl( 1, sub( 14, exp ) ), voicingBufAvg_fx ); /* (14-exp) */
4344 :
4345 : /* sd_uq_q*pitBufAvg */
4346 110813 : L_tmp = Mult_32_16( L_tmp, pitBufAvg_fx ); /* Q13 * Q10 + 1 -16 => Q8 */
4347 110813 : L_tmp = Mult_32_16( L_tmp, tmp ); /* Q8 + (14 - exp) - 15 => Q7 - exp */
4348 110813 : tmp = round_fx_o( L_shl_o( L_tmp, 31 - ( 7 - exp ), &Overflow ), &Overflow ); /* Q15 */
4349 : }
4350 110826 : tmp = s_min( tmp, 32767 /*1.0f Q15*/ );
4351 110826 : tmp = s_max( tmp, 19661 /*0.6f Q15*/ );
4352 110826 : GainFrame_fx = Mult_32_16( GainFrame_fx, tmp ); /* Q18 + Q15 + 1 - 16 : Q18 */
4353 : }
4354 :
4355 115109 : test();
4356 115109 : IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
4357 : {
4358 4283 : if ( EQ_16( st_fx->rf_mode, 1 ) )
4359 : {
4360 : /* Compensate for energy increase mismatch due to memory-less synthesis */
4361 : // GainFrame *= 0.85f;
4362 0 : GainFrame_fx = Mpy_32_16_1( GainFrame_fx, 27853 );
4363 : }
4364 : }
4365 : ELSE
4366 : {
4367 110826 : test();
4368 110826 : IF( EQ_16( st_fx->L_frame, L_FRAME16k ) || EQ_16( st_fx->rf_mode, 1 ) )
4369 : {
4370 : /* Compensate for energy increase mismatch due to memory-less synthesis*/
4371 58658 : GainFrame_fx = Mult_32_16( GainFrame_fx, 27853 /*0.85f Q15*/ ); /* Q18 */
4372 : }
4373 : }
4374 :
4375 : /* Quantization of the frame gain parameter */
4376 115109 : IF( st_fx->rf_mode )
4377 : {
4378 0 : QuantizeSHBframegain_ivas_fx( st_fx, &GainFrame_fx, st_fx->extl, 0, &hRF->RF_bwe_gainFr_ind, 0 );
4379 : }
4380 : ELSE
4381 : {
4382 115109 : QuantizeSHBframegain_ivas_fx( st_fx, &GainFrame_fx, st_fx->extl, 0, NULL, ( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) ) ? 1 : 0 );
4383 : }
4384 :
4385 115109 : IF( hStereoICBWE != NULL )
4386 : {
4387 24220 : push_indice( hBstr, IND_STEREO_ICBWE_MSFLAG, hStereoICBWE->MSFlag, STEREO_ICBWE_MSFLAG_BITS );
4388 : }
4389 :
4390 : /* Backup for the IC BWE parameters */
4391 115109 : test();
4392 115109 : IF( GE_16( st_fx->element_mode, IVAS_CPE_DFT ) && hStereoICBWE != NULL )
4393 : {
4394 : // Check Qs
4395 24220 : Copy_Scale_sig_16_32_DEPREC( lpc_shb_fx, hStereoICBWE->lpSHBRef_fx, LPC_SHB_ORDER + 1, 0 );
4396 24220 : hStereoICBWE->lpSHBRef_e = 31 - Q12;
4397 24220 : move16();
4398 : }
4399 :
4400 : /* Adjust the subframe and frame gain of the synthesized SHB signal */
4401 : /* Scale the shaped excitation */
4402 115109 : IF( EQ_32( st_fx->extl, FB_TBE ) )
4403 : {
4404 43652 : tmp = norm_l( GainFrame_fx );
4405 43652 : if ( GainFrame_fx == 0 )
4406 : {
4407 0 : tmp = 31;
4408 0 : move16();
4409 : }
4410 43652 : L_tmp = L_shl( GainFrame_fx, tmp ); /* 18 + tmp */
4411 :
4412 43652 : tmp1 = 0;
4413 43652 : move16();
4414 14012292 : FOR( i = 0; i < L_FRAME16k; i++ )
4415 : {
4416 13968640 : L_tmp1 = Mult_32_16( L_tmp, GainShape_fx[NUM_SHB_SUBFR * i / L_FRAME16k] ); /* Q : 18 + tmp +15 -15*/
4417 : /*White_exc16k_fx[i] = round_fx( L_shl(Mult_32_16( L_tmp1, White_exc16k_fx[i]),(23 - tmp -*Q_white_exc)) );*/
4418 : /*18 + tmp +*Q_white_exc -15 + 23 - tmp -*Q_white_exc -16 = 10*/
4419 13968640 : White_exc16k_fx[i] = round_fx( Mult_32_16( L_tmp1, White_exc16k_fx[i] ) ); /* 18 + tmp +*Q_white_exc -15 -16 */
4420 13968640 : move16();
4421 : /*Lmax = L_max(Lmax,White_exc16k_fx[i]);*/
4422 13968640 : tmp1 = s_max( tmp1, abs_s( White_exc16k_fx[i] ) );
4423 : /*White_exc16k_fx[i] =32767;*/
4424 : /*White_exc16k_fx[i] = shr(White_exc16k_fx[i],5);*/
4425 : }
4426 43652 : *Q_white_exc = sub( add( *Q_white_exc, tmp ), 13 ); /* *Q_white_exc + 18 + tmp -15 -16 */
4427 43652 : move16();
4428 43652 : tmp = norm_s( tmp1 );
4429 43652 : if ( tmp1 == 0 )
4430 : {
4431 0 : tmp = 15;
4432 0 : move16();
4433 : }
4434 :
4435 14012292 : FOR( i = 0; i < L_FRAME16k; i++ )
4436 : {
4437 13968640 : White_exc16k_fx[i] = shl( White_exc16k_fx[i], sub( tmp, 1 ) );
4438 13968640 : move16();
4439 : }
4440 43652 : *Q_white_exc = sub( add( *Q_white_exc, tmp ), 1 );
4441 43652 : move16();
4442 : }
4443 :
4444 115109 : hBWE_TD->prev_Env_error_fx = Env_error_fx;
4445 115109 : move16();
4446 :
4447 : /* *Q_white_exc = Q_bwe_exc_mod; move16(); output Qwhiteexc_FB from the GenShapedSHB function*/
4448 115109 : st_fx->prev_Q_bwe_syn = Q_bwe_exc;
4449 115109 : move16();
4450 :
4451 115109 : return;
4452 : }
4453 :
4454 :
4455 : /*==========================================================================*/
4456 : /* FUNCTION : static void EstimateSHBFrameGain_fx() */
4457 : /*--------------------------------------------------------------------------*/
4458 : /* PURPOSE : Estimate the overall gain factor needed to scale */
4459 : /* synthesized highband to original highband signal level. */
4460 : /*--------------------------------------------------------------------------*/
4461 : /* INPUT ARGUMENTS : */
4462 : /* _(Word16) length : SHB overlap length Q0 */
4463 : /* _(Word16*) oriSHB : target original SHB frame Q_oriSHB */
4464 : /* _(Word16*) synSHB : shaped SHB excitation Q_synSHB */
4465 : /* _(Word16*) win_shb : SHB window Q15 */
4466 : /* _(Word16*) subwin_shb_local : SHB subframe window Q15 */
4467 : /* _(Word16) Q_oriSHB : Q format of oriSHB */
4468 : /* _(Word16) Q_synSHB : Q format of synSHB */
4469 : /*--------------------------------------------------------------------------*/
4470 : /* OUTPUT ARGUMENTS : */
4471 : /* _(Word32*)GainFrame :estimat of gain frame Q18 */
4472 : /*--------------------------------------------------------------------------*/
4473 : /* INPUT/OUTPUT ARGUMENTS : */
4474 : /* _None */
4475 : /*--------------------------------------------------------------------------*/
4476 : /* RETURN ARGUMENTS : */
4477 : /* _ None */
4478 : /*--------------------------------------------------------------------------*/
4479 1350 : static void EstimateSHBFrameGain_fx(
4480 : const Word16 length, /* i : SHB overlap length */
4481 : const Word16 *oriSHB, /* i : target original SHB frame Q(Q_oriSHB) */
4482 : const Word16 Q_oriSHB, /* i : Q of arget original SHB frame */
4483 : const Word16 *synSHB, /* i : shaped SHB excitation Q(Q_synSHB) */
4484 : const Word16 Q_synSHB, /* i : Q of shaped SHB excitation */
4485 : Word16 *subgain, /* i : estimate of gain shape Q15 */
4486 : Word32 *GainFrame, /* o : estimat of gain frame Q18 */
4487 : const Word16 *win_shb, /* i : SHB window Q15 */
4488 : const Word16 *subwin_shb_local, /* i : SHB subframe window Q15 */
4489 : const Word16 n_subfr_saturation /* i : Number of subframes which saturated in GainShape oriNrg calculation */
4490 : )
4491 : {
4492 : const Word16 *skip;
4493 : Word16 i, j, k, l_shb_lahead, l_frame;
4494 : Word16 join_length, num_join, sig;
4495 : Word32 mod_syn[L_FRAME16k + L_SHB_LAHEAD];
4496 : Word32 oriNrg, synNrg;
4497 : Word16 sum_gain;
4498 : Word32 frame_gain;
4499 : Word32 L_tmp;
4500 : Word16 exp_out;
4501 : Word16 tmp;
4502 1350 : Word16 scaling = 0;
4503 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
4504 1350 : Flag Overflow = 0;
4505 1350 : move16();
4506 : #endif
4507 1350 : move16();
4508 :
4509 : /* initilaization */
4510 1350 : l_frame = L_FRAME16k;
4511 1350 : move16();
4512 1350 : l_shb_lahead = L_SHB_LAHEAD;
4513 1350 : move16();
4514 1350 : skip = skip_bands_SWB_TBE;
4515 :
4516 1350 : IF( EQ_16( length, SHB_OVERLAP_LEN / 2 ) )
4517 : {
4518 0 : skip = skip_bands_WB_TBE;
4519 0 : l_frame = L_FRAME16k / 4;
4520 0 : move16();
4521 0 : l_shb_lahead = L_SHB_LAHEAD / 4;
4522 0 : move16();
4523 : }
4524 :
4525 : /* apply gain for each subframe, and store noise output signal using overlap-add*/
4526 1350 : set32_fx( mod_syn, 0, l_frame + l_shb_lahead );
4527 :
4528 1350 : IF( EQ_16( length, SHB_OVERLAP_LEN / 2 ) )
4529 : {
4530 0 : sum_gain = 0;
4531 0 : move16();
4532 0 : j = skip[0];
4533 0 : move16();
4534 0 : FOR( k = 0; k < length / 2; k++ )
4535 : {
4536 0 : sum_gain = mult_r( subwin_shb_local[2 * k + 2], subgain[0] ); /* Q15 */
4537 0 : mod_syn[j + k] = L_mult( synSHB[j + k], sum_gain );
4538 0 : move32(); /* Q(16+Q_synSHB) */
4539 0 : mod_syn[j + k + length / 2] = L_mult( synSHB[j + k + length / 2], subgain[0] );
4540 0 : move32(); /* Q(16+Q_synSHB) */
4541 : }
4542 :
4543 0 : FOR( i = 1; i < NUM_SHB_SUBFR / 2; i++ )
4544 : {
4545 0 : j = skip[i];
4546 0 : move16();
4547 0 : FOR( k = 0; k < length; k++ )
4548 : {
4549 0 : L_tmp = L_mult0( subwin_shb_local[k + 1], subgain[i] ); /* Q30 */
4550 0 : sum_gain = round_fx( L_mac0( L_tmp, subwin_shb_local[length - k - 1], subgain[i - 1] ) ); /* Q14 */
4551 0 : mod_syn[j + k] = L_shl( L_mult( sum_gain, synSHB[j + k] ), 1 );
4552 0 : move32(); /* Q(16+Q_synSHB) */
4553 : }
4554 : }
4555 0 : FOR( k = 0; k < length / 2; k++ )
4556 : {
4557 0 : j = skip[i];
4558 0 : move16();
4559 0 : sum_gain = mult_r( subwin_shb_fx[length - 2 * k - 2], subgain[i - 1] ); /* Q15 */
4560 0 : mod_syn[j + k] = L_mult( synSHB[j + k], sum_gain );
4561 0 : move32(); /* Q(16+Q_synSHB) */
4562 : }
4563 : }
4564 : ELSE
4565 : {
4566 1350 : num_join = NUM_SHB_SUBFR / NUM_SHB_SUBGAINS;
4567 1350 : move16();
4568 1350 : join_length = i_mult2( num_join, length );
4569 1350 : j = 0;
4570 1350 : move16();
4571 28350 : FOR( k = 0; k < length; k++ )
4572 : {
4573 27000 : sum_gain = mult_r( subwin_shb_local[k + 1], subgain[0] ); /* Q15 */
4574 27000 : mod_syn[j] = L_mult( synSHB[j], sum_gain );
4575 27000 : move32(); /* Q(16+Q_synSHB) */
4576 27000 : j++;
4577 : }
4578 5400 : FOR( i = 0; i < NUM_SHB_SUBGAINS - 1; i++ )
4579 : {
4580 247050 : FOR( k = 0; k < join_length - length; k++ )
4581 : {
4582 243000 : mod_syn[j] = L_mult( synSHB[j], subgain[i * num_join] );
4583 243000 : move32(); /* Q(16+Q_synSHB) */
4584 243000 : j++;
4585 : }
4586 :
4587 85050 : FOR( k = 0; k < length; k++ )
4588 : {
4589 81000 : L_tmp = L_mult0( subwin_shb_local[length - k - 1], subgain[i * num_join] );
4590 81000 : tmp = round_fx( L_mac0( L_tmp, subwin_shb_local[k + 1], subgain[( i + 1 ) * num_join] ) ); /* Q14 */
4591 81000 : mod_syn[j] = L_shl( L_mult( tmp, synSHB[j] ), 1 );
4592 81000 : move32(); /* Q(16+Q_synSHB) */
4593 81000 : j++;
4594 : }
4595 : }
4596 82350 : FOR( k = 0; k < join_length - length; k++ )
4597 : {
4598 81000 : mod_syn[j] = L_mult( synSHB[j], subgain[( NUM_SHB_SUBGAINS - 1 ) * num_join] );
4599 81000 : move32(); /* Q(16+Q_synSHB)*/
4600 81000 : j++;
4601 : }
4602 28350 : FOR( k = 0; k < length; k++ )
4603 : {
4604 27000 : tmp = mult_r( subwin_shb_local[length - k - 1], subgain[( NUM_SHB_SUBGAINS - 1 ) * num_join] ); /* Q15 */
4605 27000 : mod_syn[j] = L_mult( tmp, synSHB[j] );
4606 27000 : move32(); /* Q(16+Q_synSHB ) */
4607 27000 : j++;
4608 : }
4609 : }
4610 : /* adjust frame energy */
4611 1350 : oriNrg = L_deposit_l( 0 );
4612 1350 : synNrg = L_deposit_l( 0 );
4613 :
4614 28350 : FOR( i = 0; i < l_shb_lahead; i++ )
4615 : {
4616 27000 : sig = mult_ro( oriSHB[i], win_shb[i], &Overflow ); /* Q_oriSHB */
4617 27000 : oriNrg = L_mac0_o( oriNrg, sig, sig, &Overflow ); /* 2*Q_orisHB*/
4618 27000 : sig = round_fx_o( Mult_32_16( mod_syn[i], win_shb[i] ), &Overflow ); /*Q_synSHB */
4619 27000 : synNrg = L_mac0_o( synNrg, sig, sig, &Overflow ); /* 2*Q_synSHB */
4620 : }
4621 :
4622 406350 : FOR( ; i < l_frame; i++ )
4623 : {
4624 405000 : oriNrg = L_mac0_o( oriNrg, oriSHB[i], oriSHB[i], &Overflow ); /* 2*Q_oriSHB */
4625 405000 : sig = round_fx_o( mod_syn[i], &Overflow ); /* Q_oriSHB */
4626 405000 : synNrg = L_mac0_o( synNrg, sig, sig, &Overflow ); /* 2*Q_oriSHB */
4627 : }
4628 :
4629 1350 : tmp = add( l_frame, l_shb_lahead );
4630 28350 : FOR( ; i < tmp; i++ )
4631 : {
4632 27000 : sig = mult_ro( oriSHB[i], win_shb[l_frame + l_shb_lahead - 1 - i], &Overflow ); /* Q_oriSHB */
4633 27000 : oriNrg = L_mac0_o( oriNrg, sig, sig, &Overflow ); /* 2*Q_oriSHB */
4634 :
4635 27000 : sig = round_fx_o( Mult_32_16( mod_syn[i], win_shb[l_frame + l_shb_lahead - 1 - i] ), &Overflow ); /* Q_oriSHB */
4636 27000 : synNrg = L_mac0_o( synNrg, sig, sig, &Overflow ); /* 2*Q_oriSHB */
4637 : }
4638 1350 : IF( EQ_32( oriNrg, MAX_32 ) )
4639 : {
4640 6 : scaling = negate( add( shr( n_subfr_saturation, 1 ), 1 ) );
4641 6 : oriNrg = 0;
4642 6 : move32();
4643 126 : FOR( i = 0; i < l_shb_lahead; i++ )
4644 : {
4645 120 : sig = mult_r( shl_o( oriSHB[i], scaling, &Overflow ), win_shb[i] );
4646 120 : oriNrg = L_mac0_o( oriNrg, sig, sig, &Overflow ); /* 2*Q_oriSHB + 2*scaling */
4647 : }
4648 1806 : FOR( ; i < l_frame; i++ )
4649 : {
4650 1800 : sig = shl_o( oriSHB[i], scaling, &Overflow );
4651 1800 : oriNrg = L_mac0_o( oriNrg, sig, sig, &Overflow ); /* 2*Q_oriSHB + 2*scaling */
4652 : }
4653 126 : FOR( ; i < tmp; i++ )
4654 : {
4655 120 : sig = mult_r( shl_o( oriSHB[i], scaling, &Overflow ), win_shb[l_frame + l_shb_lahead - 1 - i] );
4656 120 : oriNrg = L_mac0_o( oriNrg, sig, sig, &Overflow ); /* 2*Q_oriSHB + 2*scaling */
4657 : }
4658 : }
4659 1350 : L_tmp = root_a_over_b_fx( oriNrg, 2 * Q_oriSHB, synNrg, 2 * Q_synSHB, &exp_out );
4660 1350 : exp_out = sub( exp_out, scaling );
4661 1350 : frame_gain = L_shl_sat( L_tmp, sub( exp_out, 13 ) ); /* Q18 */
4662 1350 : *GainFrame = frame_gain;
4663 1350 : move32();
4664 :
4665 1350 : return;
4666 : }
4667 :
4668 122452 : static void EstimateSHBFrameGain_ivas_fx(
4669 : const Word16 length, /* i : SHB overlap length */
4670 : const Word16 *oriSHB, /* i : target original SHB frame Q(Q_oriSHB) */
4671 : const Word16 Q_oriSHB, /* i : Q of arget original SHB frame */
4672 : const Word16 *synSHB, /* i : shaped SHB excitation Q(Q_synSHB) */
4673 : const Word16 Q_synSHB, /* i : Q of shaped SHB excitation */
4674 : Word16 *subgain, /* i : estimate of gain shape Q15 */
4675 : Word32 *GainFrame, /* o : estimat of gain frame Q18 */
4676 : const Word16 *win_shb, /* i : SHB window Q15 */
4677 : const Word16 *subwin_shb_local, /* i : SHB subframe window Q15 */
4678 : const Word16 n_subfr_saturation /* i : Number of subframes which saturated in GainShape oriNrg calculation */
4679 : )
4680 : {
4681 : const Word16 *skip;
4682 : Word16 i, j, k, l_shb_lahead, l_frame;
4683 : Word16 join_length, num_join, sig;
4684 : Word32 mod_syn[L_FRAME16k + L_SHB_LAHEAD];
4685 : Word32 oriNrg, synNrg;
4686 : Word16 sum_gain;
4687 : Word32 frame_gain;
4688 : Word32 L_tmp;
4689 : Word16 exp_out;
4690 : Word16 tmp;
4691 122452 : Word16 scaling = 0;
4692 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
4693 122452 : Flag Overflow = 0;
4694 122452 : move32();
4695 : #endif
4696 122452 : move16();
4697 :
4698 : /* initilaization */
4699 122452 : l_frame = L_FRAME16k;
4700 122452 : move16();
4701 122452 : l_shb_lahead = L_SHB_LAHEAD;
4702 122452 : move16();
4703 122452 : skip = skip_bands_SWB_TBE;
4704 :
4705 122452 : IF( EQ_16( length, SHB_OVERLAP_LEN / 2 ) )
4706 : {
4707 7343 : skip = skip_bands_WB_TBE;
4708 7343 : l_frame = L_FRAME16k / 4;
4709 7343 : move16();
4710 7343 : l_shb_lahead = L_SHB_LAHEAD / 4;
4711 7343 : move16();
4712 : }
4713 :
4714 : /* apply gain for each subframe, and store noise output signal using overlap-add*/
4715 122452 : set32_fx( mod_syn, 0, l_frame + l_shb_lahead );
4716 :
4717 122452 : IF( EQ_16( length, SHB_OVERLAP_LEN / 2 ) )
4718 : {
4719 7343 : sum_gain = 0;
4720 7343 : move16();
4721 7343 : j = skip[0];
4722 7343 : move16();
4723 44058 : FOR( k = 0; k < length / 2; k++ )
4724 : {
4725 36715 : sum_gain = mult_r( subwin_shb_local[2 * k + 2], subgain[0] ); /* Q15 */
4726 36715 : mod_syn[j + k] = L_mult0( synSHB[j + k], sum_gain );
4727 36715 : move32(); /* Q(16+Q_synSHB) */
4728 36715 : mod_syn[j + k + length / 2] = L_mult0( synSHB[j + k + length / 2], subgain[0] );
4729 36715 : move32(); /* Q(16+Q_synSHB) */
4730 : }
4731 :
4732 58744 : FOR( i = 1; i < NUM_SHB_SUBFR / 2; i++ )
4733 : {
4734 51401 : j = skip[i];
4735 51401 : move16();
4736 565411 : FOR( k = 0; k < length; k++ )
4737 : {
4738 514010 : L_tmp = L_mult0( subwin_shb_local[k + 1], subgain[i] ); /* Q30 */
4739 514010 : sum_gain = round_fx( L_mac0( L_tmp, subwin_shb_local[length - k - 1], subgain[i - 1] ) ); /* Q14 */
4740 514010 : mod_syn[j + k] = L_mult( sum_gain, synSHB[j + k] );
4741 514010 : move32(); /* Q(16+Q_synSHB) */
4742 : }
4743 : }
4744 44058 : FOR( k = 0; k < length / 2; k++ )
4745 : {
4746 36715 : j = skip[i];
4747 36715 : move16();
4748 36715 : sum_gain = mult_r( subwin_shb_fx[length - 2 * k - 2], subgain[i - 1] ); /* Q15 */
4749 36715 : mod_syn[j + k] = L_mult0( synSHB[j + k], sum_gain );
4750 36715 : move32(); /* Q(16+Q_synSHB) */
4751 : }
4752 : }
4753 : ELSE
4754 : {
4755 115109 : num_join = NUM_SHB_SUBFR / NUM_SHB_SUBGAINS;
4756 115109 : move16();
4757 115109 : join_length = i_mult2( num_join, length );
4758 115109 : j = 0;
4759 115109 : move16();
4760 2417289 : FOR( k = 0; k < length; k++ )
4761 : {
4762 2302180 : sum_gain = mult_r( subwin_shb_local[k + 1], subgain[0] ); /* Q15 */
4763 2302180 : mod_syn[j] = L_mult0( synSHB[j], sum_gain );
4764 2302180 : move32(); /* Q(16+Q_synSHB) */
4765 2302180 : j++;
4766 : }
4767 460436 : FOR( i = 0; i < NUM_SHB_SUBGAINS - 1; i++ )
4768 : {
4769 21064947 : FOR( k = 0; k < join_length - length; k++ )
4770 : {
4771 20719620 : mod_syn[j] = L_mult0( synSHB[j], subgain[i * num_join] );
4772 20719620 : move32(); /* Q(16+Q_synSHB) */
4773 20719620 : j++;
4774 : }
4775 :
4776 7251867 : FOR( k = 0; k < length; k++ )
4777 : {
4778 6906540 : L_tmp = L_mult0( subwin_shb_local[length - k - 1], subgain[i * num_join] );
4779 6906540 : tmp = round_fx( L_mac0( L_tmp, subwin_shb_local[k + 1], subgain[( i + 1 ) * num_join] ) ); /* Q14 */
4780 6906540 : mod_syn[j] = L_mult( tmp, synSHB[j] );
4781 6906540 : move32(); /* Q(16+Q_synSHB) */
4782 6906540 : j++;
4783 : }
4784 : }
4785 7021649 : FOR( k = 0; k < join_length - length; k++ )
4786 : {
4787 6906540 : mod_syn[j] = L_mult0( synSHB[j], subgain[( NUM_SHB_SUBGAINS - 1 ) * num_join] );
4788 6906540 : move32(); /* Q(16+Q_synSHB)*/
4789 6906540 : j++;
4790 : }
4791 2417289 : FOR( k = 0; k < length; k++ )
4792 : {
4793 2302180 : tmp = mult_r( subwin_shb_local[length - k - 1], subgain[( NUM_SHB_SUBGAINS - 1 ) * num_join] ); /* Q15 */
4794 2302180 : mod_syn[j] = L_mult0( tmp, synSHB[j] );
4795 2302180 : move32(); /* Q(16+Q_synSHB ) */
4796 2302180 : j++;
4797 : }
4798 : }
4799 : /* adjust frame energy */
4800 122452 : oriNrg = L_deposit_l( 0 );
4801 122452 : synNrg = L_deposit_l( 0 );
4802 :
4803 2461347 : FOR( i = 0; i < l_shb_lahead; i++ )
4804 : {
4805 2338895 : sig = mult_ro( oriSHB[i], win_shb[i], &Overflow ); /* Q_oriSHB */
4806 2338895 : oriNrg = L_mac0_o( oriNrg, sig, sig, &Overflow ); /* 2*Q_orisHB*/
4807 2338895 : sig = round_fx_o( Mult_32_16( mod_syn[i], win_shb[i] ), &Overflow ); /*Q_synSHB */
4808 2338895 : synNrg = L_mac0_o( synNrg, sig, sig, &Overflow ); /* 2*Q_synSHB - 2*/
4809 : }
4810 :
4811 35205877 : FOR( ; i < l_frame; i++ )
4812 : {
4813 35083425 : oriNrg = L_mac0_o( oriNrg, oriSHB[i], oriSHB[i], &Overflow ); /* 2*Q_oriSHB */
4814 35083425 : sig = round_fx_o( mod_syn[i], &Overflow ); /* Q_oriSHB */
4815 35083425 : synNrg = L_mac0_o( synNrg, sig, sig, &Overflow ); /* 2*Q_oriSHB - 2*/
4816 : }
4817 :
4818 122452 : tmp = add( l_frame, l_shb_lahead );
4819 2461347 : FOR( ; i < tmp; i++ )
4820 : {
4821 2338895 : sig = mult_ro( oriSHB[i], win_shb[l_frame + l_shb_lahead - 1 - i], &Overflow ); /* Q_oriSHB */
4822 2338895 : oriNrg = L_mac0_o( oriNrg, sig, sig, &Overflow ); /* 2*Q_oriSHB */
4823 :
4824 2338895 : sig = round_fx_o( Mult_32_16( mod_syn[i], win_shb[l_frame + l_shb_lahead - 1 - i] ), &Overflow ); /* Q_oriSHB */
4825 2338895 : synNrg = L_mac0_o( synNrg, sig, sig, &Overflow ); /* 2*Q_oriSHB - 2*/
4826 : }
4827 122452 : IF( EQ_32( oriNrg, MAX_32 ) )
4828 : {
4829 37 : scaling = negate( add( shr( n_subfr_saturation, 1 ), 1 ) );
4830 37 : oriNrg = 0;
4831 37 : move32();
4832 777 : FOR( i = 0; i < l_shb_lahead; i++ )
4833 : {
4834 740 : sig = mult_r( shl_o( oriSHB[i], scaling, &Overflow ), win_shb[i] );
4835 740 : oriNrg = L_mac0_o( oriNrg, sig, sig, &Overflow ); /* 2*Q_oriSHB + 2*scaling */
4836 : }
4837 11137 : FOR( ; i < l_frame; i++ )
4838 : {
4839 11100 : sig = shl_o( oriSHB[i], scaling, &Overflow );
4840 11100 : oriNrg = L_mac0_o( oriNrg, sig, sig, &Overflow ); /* 2*Q_oriSHB + 2*scaling */
4841 : }
4842 777 : FOR( ; i < tmp; i++ )
4843 : {
4844 740 : sig = mult_r( shl_o( oriSHB[i], scaling, &Overflow ), win_shb[l_frame + l_shb_lahead - 1 - i] );
4845 740 : oriNrg = L_mac0_o( oriNrg, sig, sig, &Overflow ); /* 2*Q_oriSHB + 2*scaling */
4846 : }
4847 : }
4848 122452 : L_tmp = root_a_over_b_fx( oriNrg, shl( Q_oriSHB, 1 ), synNrg, sub( shl( Q_synSHB, 1 ), 2 ), &exp_out );
4849 122452 : exp_out = sub( exp_out, scaling );
4850 122452 : frame_gain = L_shl_sat( L_tmp, sub( exp_out, 13 ) ); /* Q18 */
4851 122452 : *GainFrame = frame_gain;
4852 122452 : move32();
4853 :
4854 122452 : return;
4855 : }
4856 :
4857 :
4858 2700 : static Word32 pow_off_pk_fx( Word16 a[], Word16 len, Word16 step )
4859 : {
4860 : Word16 i, j;
4861 : Word32 sum, L_tmp;
4862 :
4863 2700 : sum = L_shr( L_mult0( a[0], a[0] ), 1 ); /* Q29 */
4864 :
4865 2700 : j = 0;
4866 2700 : move16();
4867 12150 : FOR( i = 1; i < len; i += step )
4868 : {
4869 9450 : L_tmp = L_shr( L_mult0( a[i], a[i] ), 1 ); /* Q29 */
4870 9450 : sum = L_add( sum, L_tmp ); /* Q29 */
4871 9450 : if ( GT_16( a[i], a[j] ) )
4872 : {
4873 3175 : j = i;
4874 3175 : move16();
4875 : }
4876 : }
4877 2700 : L_tmp = L_shr( L_mult0( a[j], a[j] ), 1 ); /* Q29 */
4878 2700 : sum = L_sub( sum, L_tmp ); /* Q29 */
4879 :
4880 2700 : return ( sum );
4881 : }
4882 :
4883 :
4884 244904 : static Word32 pow_off_pk_corrected_fx( Word16 a[], Word16 len, Word16 step )
4885 : {
4886 : Word16 i, j;
4887 : Word32 sum, L_tmp;
4888 :
4889 244904 : sum = L_shr( L_mult0( a[0], a[0] ), 1 ); /* Q29 */
4890 :
4891 244904 : j = 0;
4892 244904 : move16();
4893 979616 : FOR( i = step; i < len; i += step )
4894 : {
4895 734712 : L_tmp = L_shr( L_mult0( a[i], a[i] ), 1 ); /* Q29 */
4896 734712 : sum = L_add( sum, L_tmp ); /* Q29 */
4897 734712 : if ( GT_16( a[i], a[j] ) )
4898 : {
4899 294793 : j = i;
4900 294793 : move16();
4901 : }
4902 : }
4903 244904 : L_tmp = L_shr( L_mult0( a[j], a[j] ), 1 ); /* Q29 */
4904 244904 : sum = L_sub( sum, L_tmp ); /* Q29 */
4905 :
4906 244904 : return ( sum );
4907 : }
4908 :
4909 :
4910 : /*==========================================================================*/
4911 : /* FUNCTION : static void EstimateSHBGainShape_fx() */
4912 : /*--------------------------------------------------------------------------*/
4913 : /* PURPOSE : Estimate temporal gain parameters */
4914 : /*--------------------------------------------------------------------------*/
4915 : /* INPUT ARGUMENTS : */
4916 : /* _(Word16) length : SHB overlap length Q0 */
4917 : /* _(Word16*) oriSHB : target original SHB frame Q_oriSHB */
4918 : /* _(Word16*) synSHB : shaped SHB excitation Q_synSHB */
4919 : /* _(Word16*) subwin : SHB subframe window Q15 */
4920 : /* _(Word16) Q_oriSHB : Q format of oriSHB */
4921 : /* _(Word16) Q_synSHB : Q format of synSHB */
4922 : /*--------------------------------------------------------------------------*/
4923 : /* OUTPUT ARGUMENTS : */
4924 : /* _(Word16*)subgain :estimate of gain shape Q15 */
4925 : /*--------------------------------------------------------------------------*/
4926 : /* INPUT/OUTPUT ARGUMENTS : */
4927 : /* _None */
4928 : /*--------------------------------------------------------------------------*/
4929 : /* RETURN ARGUMENTS : */
4930 : /* _ None */
4931 : /*--------------------------------------------------------------------------*/
4932 :
4933 123802 : static void EstimateSHBGainShape_fx(
4934 : const Word16 length, /* i : SHB overlap length */
4935 : const Word16 *oriSHB, /* i : target original SHB frame Q(Q_oriSHB) */
4936 : const Word16 Q_oriSHB, /* i : Q of target original SHB frame */
4937 : const Word16 *synSHB, /* i : shaped SHB excitation Q(Q_synSHB) */
4938 : const Word16 Q_synSHB, /* i : Q of shaped SHB excitation */
4939 : Word16 *subgain, /* o : estimate of gain shape Q15 */
4940 : const Word16 *subwin, /* i : SHB subframe window Q15 */
4941 : Word16 *n_subfr_saturation /* o : Number of subframes which saturated while calculating oriNrg */
4942 : )
4943 : {
4944 : const Word16 *skip;
4945 : Word16 i, j, k;
4946 : Word16 sig;
4947 : Word32 L_subgain[NUM_SHB_SUBFR];
4948 123802 : Word32 L_sum_gain = 0;
4949 : Word32 oriNrg, synNrg;
4950 : Word16 num_join, num_gains, join_length;
4951 : Word16 norm[NUM_SHB_SUBFR];
4952 123802 : Word16 n_max = -32768;
4953 : Word16 n;
4954 123802 : Word16 length2 = shl( length, 1 );
4955 : Word16 length_tmp;
4956 : Word32 L_tmp, normFact;
4957 123802 : Word16 scaling = 0;
4958 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
4959 123802 : Flag Overflow = 0;
4960 123802 : move32();
4961 : #endif
4962 123802 : move32();
4963 123802 : move16();
4964 123802 : move16();
4965 :
4966 123802 : num_join = NUM_SHB_SUBFR / NUM_SHB_SUBGAINS;
4967 123802 : move16();
4968 123802 : num_gains = NUM_SHB_SUBGAINS;
4969 123802 : move16();
4970 123802 : skip = skip_bands_SWB_TBE;
4971 :
4972 123802 : IF( EQ_16( length, SHB_OVERLAP_LEN / 2 ) )
4973 : {
4974 7343 : num_gains = NUM_SHB_SUBFR / 4;
4975 7343 : move16();
4976 7343 : skip = skip_bands_WB_TBE;
4977 : }
4978 : /* calculate and normalize the subgain */
4979 123802 : oriNrg = 0;
4980 123802 : move32();
4981 123802 : synNrg = 0;
4982 123802 : move32();
4983 :
4984 123802 : IF( EQ_16( length, SHB_OVERLAP_LEN / 2 ) )
4985 : {
4986 66087 : FOR( i = 0; i < NUM_SHB_SUBFR / 2; i++ )
4987 : {
4988 58744 : logic16();
4989 58744 : IF( s_and( i, 0x1 ) == 0 )
4990 : {
4991 29372 : oriNrg = 0;
4992 29372 : move32();
4993 29372 : synNrg = 0;
4994 29372 : move32();
4995 : }
4996 58744 : j = skip[i];
4997 58744 : move16();
4998 58744 : IF( i == 0 )
4999 : {
5000 44058 : FOR( k = 0; k < length / 2; k++ )
5001 : {
5002 36715 : sig = mult_r( oriSHB[j + k], subwin[2 * k + 2] ); /* Q_oriSHB */
5003 36715 : oriNrg = L_mac0_sat( oriNrg, sig, sig ); /* 2* Q_oriSHB */
5004 36715 : sig = mult_r( synSHB[j + k], subwin[2 * k + 2] ); /* Q_synSHB */
5005 36715 : synNrg = L_mac0_sat( synNrg, sig, sig ); /* 2* Q_synSHB */
5006 : }
5007 44058 : FOR( k = length / 2; k < length; k++ )
5008 : {
5009 36715 : sig = oriSHB[j + k];
5010 36715 : move16();
5011 36715 : oriNrg = L_mac0_sat( oriNrg, sig, sig ); /* 2* Q_oriSHB */
5012 36715 : sig = synSHB[j + k];
5013 36715 : move16();
5014 36715 : synNrg = L_mac0_sat( synNrg, sig, sig ); /* 2* Q_synSHB */
5015 : }
5016 : }
5017 : ELSE
5018 : {
5019 565411 : FOR( k = 0; k < length; k++ )
5020 : {
5021 514010 : sig = mult_r( oriSHB[j + k], subwin[k + 1] ); /* Q_oriSHB */
5022 514010 : oriNrg = L_mac0_sat( oriNrg, sig, sig ); /* 2* Q_oriSHB */
5023 514010 : sig = mult_r( synSHB[j + k], subwin[k + 1] ); /* Q_synSHB */
5024 514010 : synNrg = L_mac0_sat( synNrg, sig, sig ); /* 2* Q_synSHB */
5025 : }
5026 : }
5027 58744 : IF( EQ_16( i, NUM_SHB_SUBFR / 2 - 1 ) )
5028 : {
5029 7343 : length_tmp = sub( length2, shr( length, 1 ) );
5030 44058 : FOR( ; k < length_tmp; k++ )
5031 : {
5032 36715 : sig = mult_r( oriSHB[j + k], subwin[3 * length - 2 * k - 2] ); /* Q_oriSHB */
5033 36715 : oriNrg = L_mac0_sat( oriNrg, sig, sig ); /* 2* Q_oriSHB */
5034 :
5035 36715 : sig = mult_r( synSHB[j + k], subwin[3 * length - 2 * k - 2] ); /* Q_synSHB */
5036 36715 : synNrg = L_mac0_sat( synNrg, sig, sig ); /* 2* Q_synSHB */
5037 : }
5038 : }
5039 : ELSE
5040 : {
5041 565411 : FOR( ; k < length2; k++ )
5042 : {
5043 514010 : sig = mult_r( oriSHB[j + k], subwin[2 * length - k - 1] ); /* Q_oriSHB */
5044 514010 : oriNrg = L_mac0_sat( oriNrg, sig, sig ); /* 2* Q_oriSHB */
5045 514010 : sig = mult_r( synSHB[j + k], subwin[2 * length - k - 1] ); /* Q_synSHB */
5046 514010 : synNrg = L_mac0_sat( synNrg, sig, sig ); /* 2* Q_synSHB */
5047 : }
5048 : }
5049 :
5050 58744 : logic16();
5051 58744 : IF( EQ_16( s_and( i, 0x1 ), 1 ) )
5052 : {
5053 29372 : L_subgain[i / 2] = root_a_over_b_fx( oriNrg, shl( Q_oriSHB, 1 ), synNrg, shl( Q_synSHB, 1 ), &n );
5054 29372 : move16(); /* Q(31-n) */
5055 29372 : norm[i / 2] = n;
5056 29372 : move16();
5057 29372 : if ( GT_16( norm[i / 2], n_max ) )
5058 : {
5059 10704 : n_max = norm[i / 2];
5060 10704 : move16();
5061 : }
5062 : }
5063 : }
5064 : }
5065 : ELSE
5066 : {
5067 116459 : join_length = i_mult2( num_join, length );
5068 582295 : FOR( i = 0; i < num_gains; i++ )
5069 : {
5070 465836 : oriNrg = 0;
5071 465836 : move32();
5072 465836 : synNrg = 0;
5073 465836 : move32();
5074 :
5075 465836 : j = i_mult2( join_length, i );
5076 9782556 : FOR( k = 0; k < length; k++ )
5077 : {
5078 9316720 : sig = mult_r( oriSHB[j + k], subwin[k + 1] ); /* Q_oriSHB */
5079 9316720 : oriNrg = L_mac0_o( oriNrg, sig, sig, &Overflow ); /* 2* Q_oriSHB */
5080 9316720 : sig = mult_r( synSHB[j + k], subwin[k + 1] ); /* Q_oriSHB */
5081 9316720 : synNrg = L_mac0_o( synNrg, sig, sig, &Overflow ); /* 2* Q_oriSHB */
5082 : }
5083 :
5084 28415996 : FOR( k = 0; k < ( join_length - length ); k++ )
5085 : {
5086 27950160 : sig = mult_r( oriSHB[length + j + k], 32767 ); /* Q_oriSHB */
5087 27950160 : oriNrg = L_mac0_o( oriNrg, sig, sig, &Overflow ); /* 2* Q_oriSHB */
5088 27950160 : sig = mult_r( synSHB[length + j + k], 32767 ); /* Q_oriSHB */
5089 27950160 : synNrg = L_mac0_o( synNrg, sig, sig, &Overflow ); /* 2* Q_oriSHB */
5090 : }
5091 :
5092 9782556 : FOR( k = 0; k < length; k++ )
5093 : {
5094 9316720 : sig = mult_r( oriSHB[j + join_length + k], subwin[length - k - 1] ); /* Q_oriSHB */
5095 9316720 : oriNrg = L_mac0_o( oriNrg, sig, sig, &Overflow ); /* 2* Q_oriSHB */
5096 9316720 : sig = mult_r( synSHB[j + join_length + k], subwin[length - k - 1] ); /* Q_oriSHB */
5097 9316720 : synNrg = L_mac0_o( synNrg, sig, sig, &Overflow ); /* 2* Q_oriSHB */
5098 : }
5099 : /* Only implemented in SWB because the length of samples in SWB frame is longer, more likely to saturate */
5100 465836 : scaling = 0;
5101 465836 : move16();
5102 465836 : IF( EQ_32( oriNrg, MAX_32 ) )
5103 : {
5104 0 : IF( n_subfr_saturation != NULL )
5105 : {
5106 0 : *n_subfr_saturation = add( *n_subfr_saturation, 1 );
5107 0 : move16();
5108 : }
5109 0 : scaling = -1;
5110 0 : move16();
5111 0 : oriNrg = 0;
5112 0 : move32();
5113 0 : FOR( k = 0; k < length; k++ )
5114 : {
5115 0 : sig = mult_r( oriSHB[j + k], subwin[k + 1] );
5116 0 : oriNrg = L_mac0_sat( oriNrg, sig, sig ); /* 2*Q_oriSHB - 2*/
5117 : }
5118 0 : FOR( k = 0; k < ( join_length - length ); k++ )
5119 : {
5120 0 : sig = shr( oriSHB[length + j + k], 1 );
5121 0 : oriNrg = L_mac0_sat( oriNrg, sig, sig ); /* 2*Q_oriSHB - 2*/
5122 : }
5123 0 : FOR( k = 0; k < length; k++ )
5124 : {
5125 0 : sig = mult_r( shr( oriSHB[j + join_length + k], 1 ), subwin[length - k - 1] ); /* Q_oriSHB - 1(scaling) */
5126 0 : oriNrg = L_mac0_sat( oriNrg, sig, sig ); /* 2*Q_oriSHB - 2 */
5127 : }
5128 : }
5129 465836 : L_subgain[i] = root_a_over_b_fx( oriNrg, shl( Q_oriSHB, 1 ), synNrg, shl( Q_synSHB, 1 ), &n );
5130 465836 : move32(); /* Q(31-n) */
5131 465836 : n = sub( n, scaling );
5132 465836 : norm[i] = n;
5133 465836 : move16();
5134 465836 : if ( GT_16( norm[i], n_max ) )
5135 : {
5136 172270 : n_max = norm[i];
5137 172270 : move16();
5138 : }
5139 : }
5140 : }
5141 :
5142 619010 : FOR( i = 0; i < num_gains; i++ )
5143 : {
5144 495208 : subgain[i] = round_fx_o( L_shl_o( L_subgain[i], sub( norm[i], n_max + 1 ), &Overflow ), &Overflow ); /* Q(14-n_max) */
5145 495208 : L_sum_gain = L_mac0_o( L_sum_gain, subgain[i], subgain[i], &Overflow ); /* Q(28-2*n_max) */
5146 495208 : move16();
5147 : }
5148 :
5149 : /* normalize the subgain */
5150 123802 : n = norm_l( L_sum_gain );
5151 123802 : L_sum_gain = L_shl( L_sum_gain, n );
5152 123802 : n = sub( 31, add( n, ( sub( 28, shl( n_max, 1 ) ) ) ) );
5153 123802 : normFact = Isqrt_lc( L_sum_gain, &n );
5154 :
5155 619010 : FOR( i = 0; i < num_gains; i++ )
5156 : {
5157 495208 : L_tmp = Mpy_32_16_1( normFact, subgain[i] ); /*Q(31-n) * Q(31-norm[i]) */ /* Q(30-n-n_max) */
5158 495208 : subgain[i] = s_max( round_fx_o( L_shl_o( L_tmp, add( n, n_max + 1 ), &Overflow ), &Overflow ), 3277 /*0.1f Q15*/ ); /* Q15 */
5159 495208 : move16();
5160 : }
5161 :
5162 123802 : return;
5163 : }
5164 :
5165 :
5166 : /*==========================================================================*/
5167 : /* FUNCTION :static short closest_centroid_fx () */
5168 : /*--------------------------------------------------------------------------*/
5169 : /* PURPOSE : Determine a set of closest VQ centroids for a given input*/
5170 : /*--------------------------------------------------------------------------*/
5171 : /* INPUT ARGUMENTS : */
5172 : /* _(Word16*) data : input data Q15 */
5173 : /* _(Word16*) weights : weights Q0 */
5174 : /* _(Word16*) quantizer : quantizer table Q15 */
5175 : /* _(Word16) centroids : number of centroids Q0 */
5176 : /* _(Word16) length : dimension of quantiser Q0 */
5177 : /*--------------------------------------------------------------------------*/
5178 : /* OUTPUT ARGUMENTS : */
5179 : /* _None */
5180 : /*--------------------------------------------------------------------------*/
5181 : /* INPUT/OUTPUT ARGUMENTS : */
5182 : /* _None */
5183 : /*--------------------------------------------------------------------------*/
5184 : /* RETURN ARGUMENTS : */
5185 : /* _ Word16 : index Q0 */
5186 : /*--------------------------------------------------------------------------*/
5187 40343 : static Word16 closest_centroid_fx(
5188 : const Word16 *data, /* i : input data Qx*/
5189 : const Word16 *weights, /* i : weights */
5190 : const Word16 *quantizer, /* i : quantizer table Qx*/
5191 : const Word16 centroids, /* i : number of centroids */
5192 : const Word16 length ) /* i : dimension of quantiser */
5193 : {
5194 : Word16 i, j, index;
5195 : Word16 tmp, tmpL;
5196 : Word64 werr_64;
5197 : Word32 L_tmp, werr, best_werr;
5198 :
5199 40343 : index = 0;
5200 40343 : move16();
5201 40343 : best_werr = MAX_32;
5202 40343 : move32();
5203 :
5204 9752767 : FOR( i = 0; i < centroids; i++ )
5205 : {
5206 9712424 : werr_64 = 0;
5207 9712424 : move64();
5208 9712424 : tmpL = i_mult2( i, length );
5209 101759432 : FOR( j = 0; j < length; j++ )
5210 : {
5211 92047008 : tmp = sub( data[j], quantizer[tmpL + j] );
5212 92047008 : L_tmp = L_mult( tmp, tmp );
5213 92047008 : werr_64 = W_mac_32_16( werr_64, L_tmp, weights[j] );
5214 : }
5215 9712424 : werr = W_sat_m( werr_64 );
5216 9712424 : if ( LT_32( werr, best_werr ) )
5217 : {
5218 261240 : index = i;
5219 261240 : move16();
5220 : }
5221 9712424 : best_werr = L_min( best_werr, werr );
5222 : }
5223 40343 : return index;
5224 : }
5225 :
5226 : /*==========================================================================*/
5227 : /* FUNCTION :static short closest_centroid_lc_fx () */
5228 : /*--------------------------------------------------------------------------*/
5229 : /* PURPOSE : Determine a set of closest VQ centroids for a given input */
5230 : /* Gain shape is 4 dimensional */
5231 : /*--------------------------------------------------------------------------*/
5232 4901 : static Word16 closest_centroid_lc_fx(
5233 : const Word16 *data, /* i : input data Qx*/
5234 : const Word16 *quantizer, /* i : quantizer table Qx*/
5235 : const Word16 centroids ) /* i : number of centroids */
5236 : {
5237 : Word16 i, index, tmp, tmpL;
5238 : Word32 werr, best_werr;
5239 :
5240 4901 : index = 0;
5241 4901 : move16();
5242 4901 : best_werr = MAX_32;
5243 4901 : move32();
5244 :
5245 161733 : FOR( i = 0; i < centroids; i++ )
5246 : {
5247 : /* Gain shape dimension 4 */
5248 156832 : tmpL = shl( i, 2 );
5249 :
5250 : /* index 0 */
5251 156832 : tmp = sub( data[0], quantizer[tmpL] );
5252 156832 : werr = L_mult( tmp, tmp );
5253 :
5254 : /* index 1 */
5255 156832 : tmp = sub( data[1], quantizer[tmpL + 1] );
5256 156832 : werr = L_mac( werr, tmp, tmp );
5257 :
5258 : /* index 2 */
5259 156832 : tmp = sub( data[2], quantizer[tmpL + 2] );
5260 156832 : werr = L_mac( werr, tmp, tmp );
5261 :
5262 : /* index 3 */
5263 156832 : tmp = sub( data[3], quantizer[tmpL + 3] );
5264 156832 : werr = L_mac( werr, tmp, tmp );
5265 :
5266 156832 : if ( LT_32( werr, best_werr ) )
5267 : {
5268 23888 : index = i;
5269 23888 : move16();
5270 : }
5271 156832 : best_werr = L_min( best_werr, werr );
5272 : }
5273 :
5274 4901 : return index;
5275 : }
5276 :
5277 : /*============================================================*/
5278 : /* FUNCTION : static void QuantizeSHBsubgains_fx() */
5279 : /*------------------------------------------------------------*/
5280 : /* PURPOSE : Quantize super highband temporal gains */
5281 : /*------------------------------------------------------------*/
5282 : /* INPUT ARGUMENTS : */
5283 : /* _(Word16) extl : extension layer Q0 */
5284 : /*------------------------------------------------------------*/
5285 : /* OUTPUT ARGUMENTS : */
5286 : /* _ None */
5287 : /*------------------------------------------------------------*/
5288 : /* INPUT/OUTPUT ARGUMENTS : */
5289 : /* _(Word16[])subgains :super highband temporal gains Q15 */
5290 : /*------------------------------------------------------------*/
5291 : /* RETURN ARGUMENTS : */
5292 : /* _ None */
5293 : /*------------------------------------------------------------*/
5294 :
5295 1350 : static void QuantizeSHBsubgains_fx(
5296 : Encoder_State *st_fx, /* i/o: encoder state structure */
5297 : Word16 subgains[], /* i/o: super highband temporal gains Q15*/
5298 : const Word16 extl /* i : extension layer */
5299 : )
5300 : {
5301 : Word16 i, idxSubGain;
5302 : Word16 Unit_weights10[NUM_SHB_SUBFR];
5303 : Word16 exp, frac;
5304 : Word32 L_tmp;
5305 1350 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
5306 1350 : BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
5307 :
5308 1350 : IF( EQ_16( extl, WB_TBE ) )
5309 : {
5310 0 : set16_fx( Unit_weights10, 32767, (Word16) NUM_SHB_SUBFR / 4 );
5311 0 : FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
5312 : {
5313 0 : IF( EQ_16( subgains[i], 0 ) )
5314 : {
5315 0 : subgains[i + NUM_SHB_SUBFR / 4] = -18432;
5316 0 : move16(); /* (-72) in Q8 */
5317 : }
5318 : ELSE
5319 : {
5320 0 : L_tmp = L_deposit_h( subgains[i] ); /* Q31 */
5321 0 : exp = norm_l( L_tmp );
5322 0 : frac = Log2_norm_lc( L_shl( L_tmp, exp ) ); /* move16(); */
5323 : /*exp = -1 - exp; */
5324 0 : exp = sub( -1, exp );
5325 0 : L_tmp = Mpy_32_16( exp, frac, 24660 ); /* Q13 ; 20.0 * log10(2) in Q12*/
5326 0 : subgains[i + NUM_SHB_SUBFR / 4] = round_fx( L_shl( L_tmp, 11 ) ); /* Q8 */
5327 0 : move16();
5328 : }
5329 : }
5330 0 : idxSubGain = closest_centroid_lc_fx( subgains + NUM_SHB_SUBFR / 4, HBCB_SubGain5bit_fx, 1 << NUM_BITS_SHB_SUBGAINS );
5331 0 : Copy( HBCB_SubGain5bit_fx + idxSubGain * NUM_SHB_SUBFR / 4, subgains, NUM_SHB_SUBFR / 4 );
5332 :
5333 0 : push_indice( hBstr, IND_SHB_SUBGAIN, idxSubGain, NUM_BITS_SHB_SUBGAINS );
5334 :
5335 0 : FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
5336 : {
5337 0 : L_tmp = L_mult( subgains[i], 21771 ); /* *0.166096 in Q17 -> Q26 */
5338 0 : L_tmp = L_shr( L_tmp, 10 ); /* From Q26 to Q16 */
5339 0 : frac = L_Extract_lc( L_tmp, &exp );
5340 0 : subgains[i] = extract_l( Pow2( 14, frac ) );
5341 0 : move16();
5342 : /* Put 14 as exponent so that */
5343 : /* output of Pow2() will be: */
5344 : /* 16384 < Pow2() <= 32767 */
5345 0 : subgains[i] = shl( subgains[i], add( exp, 1 ) );
5346 0 : move16(); /*Q15 */
5347 : }
5348 :
5349 0 : FOR( i = NUM_SHB_SUBFR / 2 - 1; i >= 0; i-- )
5350 : {
5351 0 : subgains[i] = subgains[i / 2];
5352 0 : move16();
5353 : }
5354 : }
5355 : ELSE
5356 : {
5357 6750 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
5358 : {
5359 5400 : IF( EQ_16( subgains[i], 0 ) )
5360 : {
5361 0 : subgains[i] = -12288;
5362 0 : move16(); /* (-3) in Q12 */
5363 : }
5364 : ELSE
5365 : {
5366 5400 : L_tmp = L_deposit_h( subgains[i] ); /* Q31 */
5367 5400 : exp = norm_l( L_tmp );
5368 5400 : frac = Log2_norm_lc( L_shl( L_tmp, exp ) );
5369 : /*exp = -1 - exp; */
5370 5400 : exp = sub( -1, exp );
5371 :
5372 5400 : L_tmp = Mpy_32_16( exp, frac, 9864 ); /*move32(); // Q16 ; log10(2) in Q15 */
5373 5400 : subgains[i] = round_fx( L_shl( L_tmp, 12 ) ); /*Q12 */
5374 5400 : move16();
5375 : }
5376 : }
5377 :
5378 1350 : idxSubGain = (Word16) vquant_fx( subgains, 0, subgains, SHBCB_SubGain5bit_12_fx, NUM_SHB_SUBGAINS, 1 << NUM_BITS_SHB_SUBGAINS );
5379 :
5380 6750 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
5381 : {
5382 5400 : L_tmp = L_mult( subgains[i], 27213 ); /* *3.321928 in Q13 -> Q26 */
5383 5400 : L_tmp = L_shr( L_tmp, 10 ); /* From Q26 to Q16 */
5384 5400 : frac = L_Extract_lc( L_tmp, &exp );
5385 5400 : subgains[i] = extract_l( Pow2( 14, frac ) );
5386 5400 : move16();
5387 : /* Put 14 as exponent so that */
5388 : /* output of Pow2() will be: */
5389 : /* 16384 < Pow2() <= 32767 */
5390 5400 : subgains[i] = shl( subgains[i], add( exp, 1 ) );
5391 5400 : move16(); /*Q15 */
5392 : }
5393 :
5394 22950 : FOR( i = NUM_SHB_SUBFR - 1; i >= 0; i-- )
5395 : {
5396 21600 : subgains[i] = subgains[i * NUM_SHB_SUBGAINS / NUM_SHB_SUBFR];
5397 21600 : move16();
5398 : }
5399 :
5400 1350 : hBWE_TD->idxSubGains = idxSubGain;
5401 1350 : move16();
5402 1350 : IF( NE_16( st_fx->codec_mode, MODE2 ) )
5403 : {
5404 746 : push_indice( hBstr, IND_SHB_SUBGAIN, idxSubGain, NUM_BITS_SHB_SUBGAINS );
5405 : }
5406 : }
5407 :
5408 1350 : return;
5409 : }
5410 :
5411 :
5412 : /*============================================================*/
5413 : /* FUNCTION : static void QuantizeSHBsubgains_ivas_fx() */
5414 : /*------------------------------------------------------------*/
5415 : /* PURPOSE : Quantize super highband temporal gains */
5416 : /*------------------------------------------------------------*/
5417 : /* INPUT ARGUMENTS : */
5418 : /* _(Word16) extl : extension layer Q0 */
5419 : /*------------------------------------------------------------*/
5420 : /* OUTPUT ARGUMENTS : */
5421 : /* _ None */
5422 : /*------------------------------------------------------------*/
5423 : /* INPUT/OUTPUT ARGUMENTS : */
5424 : /* _(Word16[])subgains :super highband temporal gains Q15 */
5425 : /*------------------------------------------------------------*/
5426 : /* RETURN ARGUMENTS : */
5427 : /* _ None */
5428 : /*------------------------------------------------------------*/
5429 :
5430 120010 : static void QuantizeSHBsubgains_ivas_fx(
5431 : Encoder_State *st_fx, /* i/o: encoder state structure */
5432 : Word16 subgains[], /* i/o: super highband temporal gains Q15 */
5433 : const Word16 extl /* i : extension layer */
5434 : )
5435 : {
5436 : Word16 i, idxSubGain;
5437 : Word16 Unit_weights10[NUM_SHB_SUBFR];
5438 : Word16 exp, frac;
5439 : Word32 L_tmp;
5440 120010 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
5441 120010 : BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
5442 :
5443 120010 : IF( EQ_16( extl, WB_TBE ) )
5444 : {
5445 4901 : set16_fx( Unit_weights10, 32767, NUM_SHB_SUBFR / 4 );
5446 24505 : FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
5447 : {
5448 19604 : IF( EQ_16( subgains[i], 0 ) )
5449 : {
5450 0 : subgains[i + NUM_SHB_SUBFR / 4] = -18432;
5451 0 : move16(); /* (-72) in Q8 */
5452 : }
5453 : ELSE
5454 : {
5455 19604 : L_tmp = L_deposit_h( subgains[i] ); /* Q31 */
5456 19604 : exp = norm_l( L_tmp );
5457 19604 : frac = Log2_norm_lc( L_shl( L_tmp, exp ) ); /* move16(); */
5458 : /*exp = -1 - exp; */
5459 19604 : exp = sub( -1, exp );
5460 19604 : L_tmp = Mpy_32_16( exp, frac, 24660 ); /* Q13 ; 20.0 * log10(2) in Q12*/
5461 19604 : subgains[i + NUM_SHB_SUBFR / 4] = round_fx( L_shl( L_tmp, 11 ) ); /* Q8 */
5462 19604 : move16();
5463 : }
5464 : }
5465 4901 : idxSubGain = closest_centroid_lc_fx( subgains + NUM_SHB_SUBFR / 4, HBCB_SubGain5bit_fx, 1 << NUM_BITS_SHB_SUBGAINS );
5466 4901 : Copy( HBCB_SubGain5bit_fx + idxSubGain * NUM_SHB_SUBFR / 4, subgains, NUM_SHB_SUBFR / 4 );
5467 :
5468 4901 : push_indice( hBstr, IND_SHB_SUBGAIN, idxSubGain, NUM_BITS_SHB_SUBGAINS );
5469 :
5470 24505 : FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
5471 : {
5472 19604 : L_tmp = L_mult( subgains[i], 21771 ); /* *0.166096 in Q17 -> Q26 */
5473 19604 : L_tmp = L_shr( L_tmp, 10 ); /* From Q26 to Q16 */
5474 19604 : frac = L_Extract_lc( L_tmp, &exp );
5475 19604 : subgains[i] = extract_l( Pow2( 14, frac ) );
5476 19604 : move16();
5477 : /* Put 14 as exponent so that */
5478 : /* output of Pow2() will be: */
5479 : /* 16384 < Pow2() <= 32767 */
5480 19604 : subgains[i] = shl( subgains[i], add( exp, 1 ) );
5481 19604 : move16(); /*Q15 */
5482 : }
5483 :
5484 44109 : FOR( i = NUM_SHB_SUBFR / 2 - 1; i >= 0; i-- )
5485 : {
5486 39208 : subgains[i] = subgains[i / 2];
5487 39208 : move16();
5488 : }
5489 : }
5490 : ELSE
5491 : {
5492 575545 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
5493 : {
5494 460436 : IF( EQ_16( subgains[i], 0 ) )
5495 : {
5496 0 : subgains[i] = -12288;
5497 0 : move16(); /* (-3) in Q12 */
5498 : }
5499 : ELSE
5500 : {
5501 460436 : L_tmp = L_deposit_h( subgains[i] ); /* Q31 */
5502 460436 : exp = norm_l( L_tmp );
5503 460436 : frac = Log2_norm_lc( L_shl( L_tmp, exp ) );
5504 : /*exp = -1 - exp; */
5505 460436 : exp = sub( -1, exp );
5506 :
5507 460436 : L_tmp = Mpy_32_16( exp, frac, 9864 ); /*move32(); // Q16 ; log10(2) in Q15 */
5508 460436 : subgains[i] = round_fx( L_shl( L_tmp, 12 ) ); /*Q12 */
5509 460436 : move16();
5510 : }
5511 : }
5512 :
5513 115109 : idxSubGain = vquant_fx( subgains, 0, subgains, SHBCB_SubGain5bit_12_fx, NUM_SHB_SUBGAINS, 1 << NUM_BITS_SHB_SUBGAINS );
5514 :
5515 575545 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
5516 : {
5517 460436 : L_tmp = L_mult( subgains[i], 27213 ); /* *3.321928 in Q13 -> Q26 */
5518 460436 : L_tmp = L_shr( L_tmp, 10 ); /* From Q26 to Q16 */
5519 460436 : frac = L_Extract_lc( L_tmp, &exp );
5520 460436 : subgains[i] = extract_l( Pow2( 14, frac ) );
5521 460436 : move16();
5522 : /* Put 14 as exponent so that */
5523 : /* output of Pow2() will be: */
5524 : /* 16384 < Pow2() <= 32767 */
5525 460436 : subgains[i] = shl( subgains[i], add( exp, 1 ) );
5526 460436 : move16(); /*Q15 */
5527 : }
5528 :
5529 1956853 : FOR( i = NUM_SHB_SUBFR - 1; i >= 0; i-- )
5530 : {
5531 1841744 : subgains[i] = subgains[i * NUM_SHB_SUBGAINS / NUM_SHB_SUBFR];
5532 1841744 : move16();
5533 : }
5534 :
5535 115109 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
5536 : {
5537 0 : hBWE_TD->idxSubGains = idxSubGain;
5538 0 : move16();
5539 : }
5540 : ELSE
5541 : {
5542 115109 : push_indice( hBstr, IND_SHB_SUBGAIN, idxSubGain, NUM_BITS_SHB_SUBGAINS );
5543 : }
5544 : }
5545 :
5546 120010 : return;
5547 : }
5548 :
5549 :
5550 : /*-------------------------------------------------------------------*
5551 : * Quant_shb_ener_sf_fx_fx()
5552 : *
5553 : * Quantize SHB subframe energies
5554 : *-------------------------------------------------------------------*/
5555 :
5556 604 : static void Quant_shb_ener_sf_fx(
5557 : Encoder_State *st_fx, /* i/o: encoder state structure */
5558 : Word32 *shb_ener_sf_Q31, /* i/o: super highband subframe energies */
5559 : Word16 Q_ener )
5560 : {
5561 : Word16 idxSubEner_fx;
5562 : Word16 temp_shb_ener_sf_fx;
5563 : Word16 exp /*, exp2*/, frac;
5564 : Word32 L_tmp1, L_tmp;
5565 : Word32 sum;
5566 : Word16 tmp;
5567 604 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
5568 :
5569 : /* shb_ener_sf_fx[0] = log10(0.003125*shb_ener_sf[0:319]); */
5570 604 : sum = *shb_ener_sf_Q31; /* L_tmp in Q_ener = (2*Q_shb+1) */
5571 604 : move32();
5572 :
5573 604 : exp = norm_l( sum );
5574 604 : frac = Log2_norm_lc( L_shl( sum, exp ) );
5575 604 : exp = sub( 30, add( exp, Q_ener ) ); /* 30-(exp+Q_ener ) */
5576 604 : L_tmp1 = Mpy_32_16( exp, frac, 617 ); /* 2466=LOG10(2) in Q11, so answer Ltmp in Q12 */
5577 :
5578 604 : tmp = round_fx( L_shl( L_tmp1, 30 - 14 ) ); /* tmp in Q12 */
5579 :
5580 604 : temp_shb_ener_sf_fx = 0;
5581 604 : move16();
5582 604 : idxSubEner_fx = usquant_fx( tmp, &temp_shb_ener_sf_fx, 0, 86, shl( 1, NUM_BITS_SHB_ENER_SF ) ); /* 86 = 0.042f in Q11 = Qin-1 */
5583 : /* o: temp_shb_ener_sf_fx in Q12 */
5584 :
5585 : /* shb_ener_sf_fx[0] = pow(10.0, temp_shb_ener_sf_fx ); */
5586 : /* = pow(2, 3.321928*temp_shb_ener_sf_fx) */
5587 604 : L_tmp = L_mult( temp_shb_ener_sf_fx, 27213 ); /* 3.321928 in Q13 -> L_tmp in Q12+Q13+1 = Q26 */
5588 604 : L_tmp = L_shl( L_tmp, -10 ); /* bring L_tmp from Q26 to Q16 */
5589 604 : frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent */
5590 604 : L_tmp = Pow2( 14, frac );
5591 : // *shb_ener_sf_Q31 = L_shl( L_tmp, exp - 14 + Q_ener ); /* In Q_ener */
5592 604 : *shb_ener_sf_Q31 = L_shl( L_tmp, add( sub( exp, 14 ), Q_ener ) ); /* In Q_ener */
5593 604 : move32();
5594 :
5595 604 : hBWE_TD->idx_shb_fr_gain = idxSubEner_fx;
5596 604 : move16();
5597 604 : IF( NE_16( st_fx->codec_mode, MODE2 ) )
5598 : {
5599 0 : push_indice( st_fx->hBstr, IND_SHB_ENER_SF, idxSubEner_fx, NUM_BITS_SHB_ENER_SF );
5600 : }
5601 604 : return;
5602 : }
5603 :
5604 :
5605 : /*-------------------------------------------------------------------*
5606 : * Quant_shb_ener_sf_ivas_fx()
5607 : *
5608 : * Quantize SHB subframe energies
5609 : *-------------------------------------------------------------------*/
5610 :
5611 25058 : static void Quant_shb_ener_sf_ivas_fx(
5612 : Encoder_State *st_fx, /* i/o: encoder state structure */
5613 : Word32 *shb_ener_sf_Q31, /* i/o: super highband subframe energies Q_ener */
5614 : Word16 Q_ener )
5615 : {
5616 : Word16 idxSubEner_fx;
5617 : Word16 temp_shb_ener_sf_fx;
5618 : Word16 exp /*, exp2*/, frac;
5619 : Word32 L_tmp1, L_tmp;
5620 : Word32 sum;
5621 : Word16 tmp;
5622 25058 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
5623 :
5624 : /* shb_ener_sf_fx[0] = log10(0.003125*shb_ener_sf[0:319]); */
5625 25058 : sum = *shb_ener_sf_Q31; /* L_tmp in Q_ener = (2*Q_shb+1) */
5626 25058 : move32();
5627 :
5628 25058 : exp = norm_l( sum );
5629 25058 : frac = Log2_norm_lc( L_shl( sum, exp ) );
5630 25058 : exp = sub( 30, add( exp, Q_ener ) ); /* 30-(exp+Q_ener ) */
5631 25058 : L_tmp1 = Mpy_32_16( exp, frac, 308 ); /* 308=LOG10(2) in Q10, so answer Ltmp in Q11 */
5632 :
5633 25058 : tmp = extract_l( L_tmp1 ); /* tmp in Q11 */
5634 :
5635 25058 : temp_shb_ener_sf_fx = 0;
5636 25058 : move16();
5637 25058 : idxSubEner_fx = usquant_fx( tmp, &temp_shb_ener_sf_fx, 0, 43, 1 << NUM_BITS_SHB_ENER_SF ); /* 43 = 0.042f in Q10 = Qin-1 */
5638 : /* o: temp_shb_ener_sf_fx in Q11 */
5639 :
5640 : /* shb_ener_sf_fx[0] = pow(10.0, temp_shb_ener_sf_fx ); */
5641 : /* = pow(2, 3.321928*temp_shb_ener_sf_fx) */
5642 25058 : L_tmp = L_mult( temp_shb_ener_sf_fx, 27213 ); /* 3.321928 in Q13 -> L_tmp in Q11+Q13+1 = Q25 */
5643 25058 : L_tmp = L_shl( L_tmp, -9 ); /* bring L_tmp from Q25 to Q16 */
5644 25058 : frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent */
5645 25058 : L_tmp = Pow2( 14, frac );
5646 25058 : *shb_ener_sf_Q31 = L_shl( L_tmp, add( sub( exp, 14 ), Q_ener ) ); /* In Q_ener */
5647 25058 : move32();
5648 :
5649 25058 : hBWE_TD->idx_shb_fr_gain = idxSubEner_fx;
5650 25058 : move16();
5651 25058 : IF( NE_16( st_fx->codec_mode, MODE2 ) )
5652 : {
5653 25058 : push_indice( st_fx->hBstr, IND_SHB_ENER_SF, idxSubEner_fx, NUM_BITS_SHB_ENER_SF );
5654 : }
5655 25058 : return;
5656 : }
5657 :
5658 :
5659 : /*-------------------------------------------------------------------*
5660 : * Quant_shb_res_gshape_fx()
5661 : *
5662 : * Quantize SHB gain shapes in residual domain
5663 : *-------------------------------------------------------------------*/
5664 :
5665 604 : static void Quant_shb_res_gshape_fx(
5666 : Encoder_State *st_fx, /* i/o: encoder state structure */
5667 : Word16 shb_res_gshape_fx[] /* i/o: super highband gain shapes Q14 */
5668 : )
5669 : {
5670 : Word16 i, idxSubGain_fx[NB_SUBFR16k];
5671 604 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
5672 :
5673 3624 : FOR( i = 0; i < NB_SUBFR16k; i++ )
5674 : {
5675 6040 : idxSubGain_fx[i] = usquant_fx( shb_res_gshape_fx[i],
5676 3020 : &shb_res_gshape_fx[i],
5677 : 2048 /*0.125f Q14*/, /*2048 = 0.125 in Q14 */
5678 : 1024 /*0.125f Q13*/, /*1024 = 0.125 in Q13 */
5679 3020 : shl( 1, NUM_BITS_SHB_RES_GS ) );
5680 3020 : move16();
5681 :
5682 3020 : hBWE_TD->idx_res_gs[i] = idxSubGain_fx[i];
5683 3020 : move16();
5684 3020 : IF( NE_16( st_fx->codec_mode, MODE2 ) )
5685 : {
5686 : #ifdef FIX_1486_IND_SHB_RES
5687 0 : push_indice( st_fx->hBstr, IND_SHB_RES_GS + i, idxSubGain_fx[i], NUM_BITS_SHB_RES_GS );
5688 : #else
5689 : push_indice( st_fx->hBstr, IND_SHB_RES_GS1 + i, idxSubGain_fx[i], NUM_BITS_SHB_RES_GS );
5690 : #endif
5691 : }
5692 : }
5693 604 : }
5694 :
5695 :
5696 : /*-------------------------------------------------------------------*
5697 : * Quant_shb_res_gshape_ivas_fx()
5698 : *
5699 : * Quantize SHB gain shapes in residual domain
5700 : *-------------------------------------------------------------------*/
5701 :
5702 25058 : static void Quant_shb_res_gshape_ivas_fx(
5703 : Encoder_State *st, /* i/o: encoder state structure */
5704 : Word16 shb_res_gshape_fx[] /* i/o: super highband gain shapes Q14 */
5705 : )
5706 : {
5707 : Word16 i, idxSubGain_fx[NB_SUBFR16k];
5708 25058 : TD_BWE_ENC_HANDLE hBWE_TD = st->hBWE_TD;
5709 :
5710 150348 : FOR( i = 0; i < NB_SUBFR16k; i++ )
5711 : {
5712 250580 : idxSubGain_fx[i] = usquant_fx( shb_res_gshape_fx[i],
5713 125290 : &shb_res_gshape_fx[i],
5714 : 2048 /*0.125f Q14*/, /*2048 = 0.125 in Q14 */
5715 : 1024 /*0.125f Q13*/, /*1024 = 0.125 in Q13 */
5716 125290 : shl( 1, NUM_BITS_SHB_RES_GS ) );
5717 125290 : move16();
5718 :
5719 125290 : IF( EQ_16( st->codec_mode, MODE2 ) )
5720 : {
5721 0 : hBWE_TD->idx_res_gs[i] = idxSubGain_fx[i];
5722 0 : move16();
5723 : }
5724 : ELSE
5725 : {
5726 : #ifdef FIX_1486_IND_SHB_RES
5727 125290 : push_indice( st->hBstr, IND_SHB_RES_GS + i, idxSubGain_fx[i], NUM_BITS_SHB_RES_GS );
5728 : #else
5729 : push_indice( st->hBstr, IND_SHB_RES_GS1 + i, idxSubGain_fx[i], NUM_BITS_SHB_RES_GS );
5730 : #endif
5731 : }
5732 : }
5733 25058 : }
5734 :
5735 :
5736 : /*==========================================================================*/
5737 : /* FUNCTION : static void QuantizeSHBframegain_fx() */
5738 : /*--------------------------------------------------------------------------*/
5739 : /* PURPOSE : QQuantize super highband frame gain */
5740 : /*--------------------------------------------------------------------------*/
5741 : /* INPUT ARGUMENTS : */
5742 : /* Word16 extl i : extension layer */
5743 : /* Word32 extl_brate i : extension layer bitrate */
5744 : /*--------------------------------------------------------------------------*/
5745 : /* OUTPUT ARGUMENTS : */
5746 : /*--------------------------------------------------------------------------*/
5747 : /* INPUT/OUTPUT ARGUMENTS : */
5748 : /* Word32 *GainFrame i/o: Gain Q18 */
5749 : /*--------------------------------------------------------------------------*/
5750 : /* RETURN ARGUMENTS : */
5751 : /* _ None */
5752 : /*--------------------------------------------------------------------------*/
5753 : /* */
5754 : /*==========================================================================*/
5755 :
5756 1350 : static void QuantizeSHBframegain_fx(
5757 : Encoder_State *st_fx, /* i/o: encoder state structure */
5758 : Word32 *GainFrame, /* i/o: Gain Q18 */
5759 : const Word16 extl, /* i : extension layer */
5760 : Word32 extl_brate /* i : extension layer bitrate */
5761 : ,
5762 : Word16 *rf_gainFrame_ind )
5763 : {
5764 : Word16 idxFrameGain;
5765 : Word32 Q_GainFrame;
5766 1350 : Word16 Unit_weights1 = 1;
5767 1350 : move16();
5768 : Word16 exp, frac, tmp;
5769 : Word32 L_tmp;
5770 : Word32 GainFrameLog;
5771 1350 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
5772 1350 : RF_ENC_HANDLE hRF = st_fx->hRF;
5773 :
5774 1350 : IF( EQ_16( extl, WB_TBE ) )
5775 : {
5776 0 : determine_gain_weights_fx( GainFrame, &( Unit_weights1 ), 1 );
5777 0 : IF( EQ_32( extl_brate, WB_TBE_0k35 ) )
5778 : {
5779 0 : singlevectortest_gain_fx( GainFrame, 1,
5780 : 1 << NUM_BITS_SHB_FrameGain_LBR_WB, &idxFrameGain,
5781 : &Q_GainFrame, SHBCB_FrameGain16_fx );
5782 0 : test();
5783 0 : IF( GT_32( Q_GainFrame, L_shl( Mult_32_16( *GainFrame, 17367 ), 1 ) ) && idxFrameGain > 0 ) /* 1.06 = +0.5 dB */
5784 : {
5785 0 : idxFrameGain--;
5786 0 : Q_GainFrame = L_add( SHBCB_FrameGain16_fx[idxFrameGain], 0 ); /* Q18 */
5787 : }
5788 0 : hBWE_TD->gFrame_WB = idxFrameGain;
5789 0 : move16();
5790 0 : *rf_gainFrame_ind = idxFrameGain;
5791 : }
5792 : ELSE
5793 : {
5794 0 : singlevectortest_gain_fx( GainFrame, 1,
5795 : 1 << NUM_BITS_SHB_FrameGain, &idxFrameGain, &Q_GainFrame,
5796 : SHBCB_FrameGain64_fx );
5797 :
5798 0 : push_indice( st_fx->hBstr, IND_SHB_FRAMEGAIN, idxFrameGain, NUM_BITS_SHB_FrameGain );
5799 0 : *rf_gainFrame_ind = idxFrameGain;
5800 0 : move16(); /* Q18 */
5801 : }
5802 : }
5803 : ELSE
5804 : {
5805 1350 : IF( *GainFrame == 0 )
5806 : {
5807 0 : GainFrameLog = -196608;
5808 0 : move32();
5809 : }
5810 : ELSE
5811 : {
5812 1350 : exp = norm_l( *GainFrame );
5813 1350 : frac = Log2_norm_lc( L_shl( *GainFrame, exp ) );
5814 : // exp = ( 30 - exp - 18 );
5815 1350 : exp = sub( 30 - 18, exp );
5816 1350 : GainFrameLog = Mpy_32_16( exp, frac, 9864 );
5817 : /*GainFrameLog= round_fx(L_shl(L_tmp,12)); //Q12 */
5818 : }
5819 :
5820 1350 : exp = norm_s( SHB_GAIN_QDELTA_FX_15 );
5821 1350 : tmp = div_s( shl( 1, sub( 14, exp ) ), SHB_GAIN_QDELTA_FX_15 );
5822 1350 : L_tmp = Mult_32_16( L_sub( GainFrameLog, SHB_GAIN_QLOW_FX_16 ), tmp );
5823 1350 : idxFrameGain = extract_l( L_shr( L_add( L_tmp, shl( 1, sub( 14, exp ) ) ), sub( 15, exp ) ) ); /*Q0*/
5824 1350 : IF( GT_16( idxFrameGain, ( 1 << NUM_BITS_SHB_FRAMEGAIN ) - 1 ) )
5825 : {
5826 0 : idxFrameGain = ( 1 << NUM_BITS_SHB_FRAMEGAIN ) - 1;
5827 0 : move16();
5828 : }
5829 : ELSE
5830 : {
5831 1350 : if ( idxFrameGain < 0 )
5832 : {
5833 0 : idxFrameGain = 0;
5834 0 : move16();
5835 : }
5836 : }
5837 :
5838 1350 : L_tmp = SHB_GAIN_QLOW_FX_16;
5839 1350 : Q_GainFrame = L_mac( L_tmp, idxFrameGain, SHB_GAIN_QDELTA_FX_15 );
5840 :
5841 1350 : test();
5842 1358 : WHILE( GT_32( Q_GainFrame, L_add( GainFrameLog, 4866 ) ) &&
5843 : idxFrameGain != 0 )
5844 : {
5845 8 : test();
5846 8 : idxFrameGain = sub( idxFrameGain, 1 );
5847 8 : Q_GainFrame = L_mac0( SHB_GAIN_QLOW_FX_16, idxFrameGain, SHB_GAIN_QDELTA_FX_16 );
5848 : }
5849 :
5850 : /* Q_GainFrame = (float) pow(10.0, Q_GainFrame ); */
5851 : /* i: Q_GainFrame in Q16 */
5852 1350 : L_tmp = Mult_32_16( Q_GainFrame, 27213 ); /* *3.321928 in Q13 -> Q25 */
5853 1350 : L_tmp = L_shr( L_tmp, -2 ); /* From Q26 to Q16 */
5854 1350 : frac = L_Extract_lc( L_tmp, &exp );
5855 1350 : Q_GainFrame = Pow2( 30, frac );
5856 1350 : exp = sub( exp, 30 );
5857 1350 : Q_GainFrame = L_shl( Q_GainFrame, add( exp, 18 ) ); /* Q18 */
5858 :
5859 1350 : hBWE_TD->idxFrameGain = idxFrameGain;
5860 1350 : move16();
5861 1350 : IF( NE_16( st_fx->codec_mode, MODE2 ) )
5862 : {
5863 746 : push_indice( st_fx->hBstr, IND_SHB_FRAMEGAIN, idxFrameGain, NUM_BITS_SHB_FRAMEGAIN );
5864 : }
5865 1350 : *rf_gainFrame_ind = idxFrameGain;
5866 1350 : move16();
5867 : }
5868 :
5869 1350 : IF( EQ_16( st_fx->rf_mode, 1 ) ){
5870 : /*Currently intended for SWB only. Modify for WB is needed later!*/
5871 0 : IF( EQ_16( hRF->rf_frame_type, RF_NELP ) ){
5872 0 : *rf_gainFrame_ind = idxFrameGain; /* NELP Frame uses full 5 bits */
5873 0 : move16();
5874 : }
5875 : ELSE /*RF_ALLPRED, RF_GENPRED, RF_NOPRED modes*/
5876 : {
5877 0 : IF( *GainFrame <= 327680 /*1.25 in Q18*/ ) /* [0 to 1.25] range --> 0.5*/
5878 : {
5879 0 : *rf_gainFrame_ind = 0;
5880 : }
5881 0 : ELSE IF( *GainFrame <= 786432 /*3 in Q18*/ ) /* (1.25 to 3] --> 2 */
5882 : {
5883 0 : *rf_gainFrame_ind = 1;
5884 : }
5885 0 : ELSE IF( *GainFrame <= 1572864 /*6 in Q18*/ ) /* (3 to 6] --> 4 */
5886 : {
5887 0 : *rf_gainFrame_ind = 2;
5888 : }
5889 : ELSE /* (6 to Inf) --> 8 */
5890 : {
5891 0 : *rf_gainFrame_ind = 3;
5892 : }
5893 0 : move16();
5894 : }
5895 : }
5896 :
5897 1350 : *GainFrame = Q_GainFrame;
5898 1350 : move32(); /* Q18 */
5899 :
5900 1350 : return;
5901 : }
5902 :
5903 :
5904 : /*==========================================================================*/
5905 : /* FUNCTION : static void QuantizeSHBframegain_ivas_fx() */
5906 : /*--------------------------------------------------------------------------*/
5907 : /* PURPOSE : QQuantize super highband frame gain */
5908 : /*--------------------------------------------------------------------------*/
5909 : /* INPUT ARGUMENTS : */
5910 : /* Word16 extl i : extension layer */
5911 : /* Word32 extl_brate i : extension layer bitrate */
5912 : /*--------------------------------------------------------------------------*/
5913 : /* OUTPUT ARGUMENTS : */
5914 : /*--------------------------------------------------------------------------*/
5915 : /* INPUT/OUTPUT ARGUMENTS : */
5916 : /* Word32 *GainFrame i/o: Gain Q18 */
5917 : /*--------------------------------------------------------------------------*/
5918 : /* RETURN ARGUMENTS : */
5919 : /* _ None */
5920 : /*--------------------------------------------------------------------------*/
5921 : /* */
5922 : /*==========================================================================*/
5923 :
5924 122452 : static void QuantizeSHBframegain_ivas_fx(
5925 : Encoder_State *st_fx, /* i/o: encoder state structure */
5926 : Word32 *GainFrame, /* i/o: Gain Q18 */
5927 : const Word16 extl, /* i : extension layer */
5928 : Word32 extl_brate, /* i : extension layer bitrate */
5929 : Word16 *rf_gainFrame_ind,
5930 : const Word16 flag_conservative /* i : flag to indicate more conservative gain quantization */
5931 : )
5932 : {
5933 : Word16 idxFrameGain;
5934 : Word32 Q_GainFrame;
5935 122452 : Word16 Unit_weights1 = 1;
5936 : Word16 exp, frac, tmp;
5937 : Word32 L_tmp;
5938 : Word32 GainFrameLog;
5939 122452 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
5940 122452 : RF_ENC_HANDLE hRF = st_fx->hRF;
5941 122452 : move16();
5942 :
5943 122452 : BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
5944 :
5945 122452 : IF( EQ_16( extl, WB_TBE ) )
5946 : {
5947 7343 : determine_gain_weights_fx( GainFrame, &( Unit_weights1 ), 1 );
5948 7343 : IF( EQ_32( extl_brate, WB_TBE_0k35 ) )
5949 : {
5950 2442 : singlevectortest_gain_fx( GainFrame, 1,
5951 : 1 << NUM_BITS_SHB_FrameGain_LBR_WB, &idxFrameGain,
5952 : &Q_GainFrame, SHBCB_FrameGain16_fx );
5953 2442 : test();
5954 2442 : IF( GT_64( W_deposit32_l( Q_GainFrame ), W_shl( W_mult_32_16( *GainFrame, 17367 ), 15 ) ) && idxFrameGain > 0 ) /* 1.06 = +0.5 dB */
5955 : {
5956 0 : idxFrameGain--;
5957 0 : Q_GainFrame = L_add( SHBCB_FrameGain16_fx[idxFrameGain], 0 ); /* Q18 */
5958 : }
5959 :
5960 2442 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
5961 : {
5962 0 : hBWE_TD->gFrame_WB = idxFrameGain;
5963 0 : move16();
5964 : }
5965 : ELSE
5966 : {
5967 2442 : push_indice( hBstr, IND_SHB_FRAMEGAIN, idxFrameGain, NUM_BITS_SHB_FrameGain_LBR_WB );
5968 : }
5969 :
5970 2442 : if ( st_fx->rf_mode )
5971 : {
5972 0 : *rf_gainFrame_ind = idxFrameGain;
5973 0 : move16();
5974 : }
5975 : }
5976 : ELSE
5977 : {
5978 4901 : singlevectortest_gain_fx( GainFrame, 1,
5979 : 1 << NUM_BITS_SHB_FrameGain, &idxFrameGain, &Q_GainFrame,
5980 : SHBCB_FrameGain64_fx );
5981 :
5982 4901 : push_indice( hBstr, IND_SHB_FRAMEGAIN, idxFrameGain, NUM_BITS_SHB_FrameGain );
5983 :
5984 4901 : if ( st_fx->rf_mode )
5985 : {
5986 0 : *rf_gainFrame_ind = idxFrameGain;
5987 0 : move16();
5988 : }
5989 : }
5990 : }
5991 : ELSE
5992 : {
5993 115109 : IF( *GainFrame == 0 )
5994 : {
5995 37 : GainFrameLog = -196608; /* Q16 */
5996 37 : move32();
5997 : }
5998 : ELSE
5999 : {
6000 115072 : exp = norm_l( *GainFrame );
6001 115072 : frac = Log2_norm_lc( L_shl( *GainFrame, exp ) );
6002 115072 : exp = sub( 30 - 18, exp );
6003 115072 : GainFrameLog = Mpy_32_16( exp, frac, 9864 );
6004 : /*GainFrameLog= round_fx(L_shl(L_tmp,12)); //Q12 */
6005 : }
6006 :
6007 115109 : IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
6008 : {
6009 : /* idxFrameGain = (int16_t) usquant( GainFrameLog, &Q_GainFrame, SHB_GAIN_QLOW_1k75, SHB_GAIN_QDELTA_1k75, 1 << NUM_BITS_SHB_FRAMEGAIN_1k75 ); */
6010 3369 : exp = norm_s( SHB_GAIN_QDELTA_1k75_FX_15 );
6011 3369 : tmp = div_s( shl( 1, sub( 14, exp ) ), SHB_GAIN_QDELTA_1k75_FX_15 );
6012 3369 : L_tmp = Mpy_32_16_1( L_sub( GainFrameLog, SHB_GAIN_QLOW_1k75_FX ), tmp );
6013 3369 : idxFrameGain = extract_l( L_shr( L_add( L_tmp, shl( 1, sub( 14, exp ) ) ), sub( 15, exp ) ) ); /*Q0*/
6014 3369 : IF( GT_16( idxFrameGain, ( 1 << NUM_BITS_SHB_FRAMEGAIN_1k75 ) - 1 ) )
6015 : {
6016 0 : idxFrameGain = ( 1 << NUM_BITS_SHB_FRAMEGAIN_1k75 ) - 1;
6017 0 : move16();
6018 : }
6019 : ELSE
6020 : {
6021 3369 : if ( idxFrameGain < 0 )
6022 : {
6023 63 : idxFrameGain = 0;
6024 63 : move16();
6025 : }
6026 : }
6027 3369 : L_tmp = SHB_GAIN_QLOW_1k75_FX;
6028 3369 : move32();
6029 3369 : Q_GainFrame = L_mac( L_tmp, idxFrameGain, SHB_GAIN_QDELTA_1k75_FX_15 );
6030 : }
6031 : ELSE
6032 : {
6033 : /* idxFrameGain = (int16_t) usquant( GainFrameLog, &Q_GainFrame, SHB_GAIN_QLOW, SHB_GAIN_QDELTA, 1 << NUM_BITS_SHB_FRAMEGAIN ); */
6034 111740 : exp = norm_s( SHB_GAIN_QDELTA_FX_15 );
6035 111740 : tmp = div_s( shl( 1, sub( 14, exp ) ), SHB_GAIN_QDELTA_FX_15 );
6036 111740 : L_tmp = Mpy_32_16_1( L_sub( GainFrameLog, SHB_GAIN_QLOW_FX_16 ), tmp );
6037 111740 : idxFrameGain = extract_l( L_shr( L_add( L_tmp, shl( 1, sub( 14, exp ) ) ), sub( 15, exp ) ) ); /*Q0*/
6038 111740 : IF( GT_16( idxFrameGain, ( 1 << NUM_BITS_SHB_FRAMEGAIN ) - 1 ) )
6039 : {
6040 0 : idxFrameGain = ( 1 << NUM_BITS_SHB_FRAMEGAIN ) - 1;
6041 0 : move16();
6042 : }
6043 : ELSE
6044 : {
6045 111740 : if ( idxFrameGain < 0 )
6046 : {
6047 38 : idxFrameGain = 0;
6048 38 : move16();
6049 : }
6050 : }
6051 111740 : L_tmp = SHB_GAIN_QLOW_FX_16;
6052 111740 : move32();
6053 111740 : Q_GainFrame = L_mac( L_tmp, idxFrameGain, SHB_GAIN_QDELTA_FX_15 );
6054 : }
6055 :
6056 115109 : test();
6057 115556 : WHILE( GT_32( Q_GainFrame, L_add( GainFrameLog, 4866 ) ) &&
6058 : idxFrameGain != 0 )
6059 : {
6060 447 : test();
6061 447 : idxFrameGain = sub( idxFrameGain, 1 );
6062 447 : Q_GainFrame = L_mac0( SHB_GAIN_QLOW_FX_16, idxFrameGain, SHB_GAIN_QDELTA_FX_16 );
6063 : }
6064 :
6065 115109 : test();
6066 115109 : test();
6067 115109 : IF( flag_conservative && GT_32( Q_GainFrame, L_add( GainFrameLog, Mpy_32_16_1( SHB_GAIN_QDELTA_FX_16, 13435 /* 0.41 in Q15 */ ) ) ) && idxFrameGain != 0 )
6068 : {
6069 : /* be more conservative in the quantization process -> prevent energy overshoot */
6070 82 : idxFrameGain = sub( idxFrameGain, 1 );
6071 82 : Q_GainFrame = L_mac0( SHB_GAIN_QLOW_FX_16, idxFrameGain, SHB_GAIN_QDELTA_FX_16 );
6072 : }
6073 :
6074 : /* Q_GainFrame = (float) pow(10.0, Q_GainFrame ); */
6075 : /* i: Q_GainFrame in Q16 */
6076 115109 : L_tmp = Mpy_32_16_1( Q_GainFrame, 27213 ); /* *3.321928 in Q13 -> Q25 */
6077 115109 : L_tmp = L_shr( L_tmp, -2 ); /* From Q26 to Q16 */
6078 115109 : frac = L_Extract_lc( L_tmp, &exp );
6079 115109 : Q_GainFrame = Pow2( 30, frac );
6080 115109 : exp = sub( exp, 30 );
6081 115109 : Q_GainFrame = L_shl( Q_GainFrame, add( exp, 18 ) ); /* Q18 */
6082 :
6083 115109 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
6084 : {
6085 0 : st_fx->hBWE_TD->idxFrameGain = idxFrameGain;
6086 0 : move16();
6087 : }
6088 : ELSE
6089 : {
6090 115109 : IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
6091 : {
6092 3369 : push_indice( hBstr, IND_SHB_FRAMEGAIN, idxFrameGain, NUM_BITS_SHB_FRAMEGAIN_1k75 );
6093 : }
6094 : ELSE
6095 : {
6096 111740 : push_indice( hBstr, IND_SHB_FRAMEGAIN, idxFrameGain, NUM_BITS_SHB_FRAMEGAIN );
6097 : }
6098 : }
6099 :
6100 115109 : if ( st_fx->rf_mode )
6101 : {
6102 0 : *rf_gainFrame_ind = idxFrameGain;
6103 0 : move16();
6104 : }
6105 : }
6106 :
6107 122452 : IF( EQ_16( st_fx->rf_mode, 1 ) ){
6108 : /*Currently intended for SWB only. Modify for WB is needed later!*/
6109 0 : IF( EQ_16( hRF->rf_frame_type, RF_NELP ) ){
6110 0 : *rf_gainFrame_ind = idxFrameGain; /* NELP Frame uses full 5 bits */
6111 0 : move16();
6112 : }
6113 : ELSE /*RF_ALLPRED, RF_GENPRED, RF_NOPRED modes*/
6114 : {
6115 0 : IF( LE_32( *GainFrame, 327680 /*1.25 in Q18*/ ) ) /* [0 to 1.25] range --> 0.5*/
6116 : {
6117 0 : *rf_gainFrame_ind = 0;
6118 0 : move16();
6119 : }
6120 0 : ELSE IF( LE_32( *GainFrame, 786432 /*3 in Q18*/ ) ) /* (1.25 to 3] --> 2 */
6121 : {
6122 0 : *rf_gainFrame_ind = 1;
6123 0 : move16();
6124 : }
6125 0 : ELSE IF( LE_32( *GainFrame, 1572864 /*6 in Q18*/ ) ) /* (3 to 6] --> 4 */
6126 : {
6127 0 : *rf_gainFrame_ind = 2;
6128 0 : move16();
6129 : }
6130 : ELSE /* (6 to Inf) --> 8 */
6131 : {
6132 0 : *rf_gainFrame_ind = 3;
6133 0 : move16();
6134 : }
6135 : }
6136 : }
6137 :
6138 122452 : *GainFrame = Q_GainFrame;
6139 122452 : move32(); /* Q18 */
6140 :
6141 122452 : return;
6142 : }
6143 :
6144 :
6145 : /*============================================================*/
6146 : /* FUNCTION : static void determine_gain_weights_fx() */
6147 : /*------------------------------------------------------------*/
6148 : /* PURPOSE : Determine weights for gain quantization */
6149 : /*------------------------------------------------------------*/
6150 : /* INPUT ARGUMENTS : */
6151 : /* _(Word32*)gain :Gain parameter Q18 */
6152 : /* _(Word16) dims : number of gains */
6153 : /*------------------------------------------------------------*/
6154 : /* OUTPUT ARGUMENTS : */
6155 : /* _(Word16*)weights : gain weights Q12/Q6 */
6156 : /*------------------------------------------------------------*/
6157 : /* INPUT/OUTPUT ARGUMENTS : */
6158 : /* _ None */
6159 : /*------------------------------------------------------------*/
6160 : /* RETURN ARGUMENTS : */
6161 : /* _ None */
6162 : /*------------------------------------------------------------*/
6163 7343 : static void determine_gain_weights_fx(
6164 : const Word32 *gain, /* i : Gain parameter Q18 */
6165 : Word16 *weights, /* o : gain weights Q12/Q6*/
6166 : const Word16 dims /* i : number of gains */ )
6167 : {
6168 : Word16 j;
6169 : Word16 exp, exp1, frac, tmp, exp2;
6170 : Word32 L_tmp;
6171 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
6172 7343 : Flag Overflow = 0;
6173 7343 : move16();
6174 : #endif
6175 :
6176 14686 : FOR( j = 0; j < dims; j++ )
6177 : {
6178 7343 : IF( GT_32( gain[j], 8 ) ) /* 8 = 0.001 in Q13 */
6179 : {
6180 : /*weights[j] = (float)(pow (fabs (gain[j]), -0.9f)); = pow(2,(-0.9)*log2(gain[j])) */
6181 7171 : exp1 = norm_l( gain[j] );
6182 7171 : frac = Log2_norm_lc( L_shl( gain[j], exp1 ) );
6183 7171 : move16();
6184 7171 : exp = sub( 30 - 18, exp1 );
6185 7171 : move16();
6186 7171 : L_tmp = Mpy_32_16( exp, frac, -29491 ); /* Q16 */
6187 7171 : frac = L_Extract_lc( L_tmp, &exp );
6188 7171 : tmp = extract_l( Pow2( 14, frac ) ); /* Q14 */
6189 :
6190 7171 : exp2 = sub( exp, 8 );
6191 7171 : IF( LE_16( exp1, 21 ) )
6192 : {
6193 7171 : exp2 = sub( exp, 2 );
6194 : }
6195 7171 : weights[j] = shl_o( tmp, exp2, &Overflow );
6196 7171 : move16(); /* Q12 */
6197 : }
6198 : ELSE
6199 : {
6200 172 : weights[j] = 32076;
6201 172 : move16(); /* (501.187233628f) in Q6*/
6202 : }
6203 : }
6204 :
6205 7343 : return;
6206 : }
6207 :
6208 :
6209 : /*==============================================================================*/
6210 : /* FUNCTION : static singlevectortest_gain_fx () */
6211 : /*------------------------------------------------------------------------------*/
6212 : /* PURPOSE : Single stage VQ for coding */
6213 : /*------------------------------------------------------------------------------*/
6214 : /* INPUT ARGUMENTS : */
6215 : /* _(Word32*) inp : input gain vector Q18 */
6216 : /* _(Word16) dimen : dimension of the input vector */
6217 : /* _(Word16) cb_size : codebook size */
6218 : /* _(Word16*) weight : Weights for the quanitzation */
6219 : /* _(Word32*) codebook : Codebook 19Q13 */
6220 : /*------------------------------------------------------------------------------*/
6221 : /* OUTPUT ARGUMENTS : */
6222 : /* _(Word16*)index :quanitzation index */
6223 : /* _(Word32*)recon :Reconstruction 19Q13 */
6224 : /*------------------------------------------------------------------------------*/
6225 : /* INPUT/OUTPUT ARGUMENTS : */
6226 : /* _None */
6227 : /*------------------------------------------------------------------------------*/
6228 : /* RETURN ARGUMENTS : */
6229 : /* _ None */
6230 : /*------------------------------------------------------------------------------*/
6231 7343 : static void singlevectortest_gain_fx(
6232 : const Word32 *inp, /* i : input gain vector Q18*/
6233 : const Word16 dimen, /* i : dimension of the input vector */
6234 : const Word16 cb_size, /* i : codebook size */
6235 : Word16 *index, /* o : quanitzation index */
6236 : Word32 *recon, /* o : Reconstruction Q18 */
6237 : const Word32 *codebook
6238 : /* i : Codebook Q18*/ )
6239 : {
6240 : Word16 k, interNum, flag;
6241 : Word32 meanU, meanQ;
6242 : Word16 least[4];
6243 :
6244 7343 : interNum = 4;
6245 7343 : move16();
6246 :
6247 7343 : return_M_Least_fx_GainFrame( inp, codebook, cb_size, interNum, least );
6248 :
6249 7343 : meanU = sum32_fx( inp, dimen ); /* Q18 */
6250 7343 : Copy32( codebook + dimen * least[0], recon, dimen );
6251 :
6252 7343 : index[0] = least[0];
6253 7343 : move16();
6254 7343 : flag = 0;
6255 7343 : move16();
6256 36715 : FOR( k = 0; k < interNum; k++ )
6257 : {
6258 29372 : IF( flag == 0 )
6259 : {
6260 8578 : meanQ = sum32_fx( codebook + dimen * least[k], dimen ); /* Q18 */
6261 : /* if ( meanQ <= 1.1 * meanU ) */
6262 8578 : IF( LE_32( Mpy_32_16_1( meanQ, 29789 /* 1/1.1 in Q15*/ ), meanU ) )
6263 : {
6264 7145 : flag = 1;
6265 7145 : move16();
6266 7145 : Copy32( codebook + dimen * least[k], recon, dimen );
6267 7145 : index[0] = least[k];
6268 7145 : move16();
6269 : }
6270 : }
6271 : }
6272 :
6273 7343 : return;
6274 : }
6275 :
6276 :
6277 : /*==============================================================================*/
6278 : /* FUNCTION : return_M_Least_fx_GainFrame () */
6279 : /*------------------------------------------------------------------------------*/
6280 : /* PURPOSE : */
6281 : /*------------------------------------------------------------------------------*/
6282 : /* INPUT ARGUMENTS : */
6283 : /* _(Word32*) inp : input Q18 */
6284 : /* _(Word16*) weight : input gain weights */
6285 : /* _(Word16) n_cols : vector size */
6286 : /* _(Word16) num_grp : number of centroids for 1st stage */
6287 : /* _(Word16) interNum : number on short list prior to 2nd stage search */
6288 : /* _(Word32*) codebook : first stage codebook Q18 */
6289 : /*------------------------------------------------------------------------------*/
6290 : /* OUTPUT ARGUMENTS : */
6291 : /* _(Word16*)least :return value */
6292 : /*------------------------------------------------------------------------------*/
6293 : /* INPUT/OUTPUT ARGUMENTS : */
6294 : /* _None */
6295 : /*------------------------------------------------------------------------------*/
6296 : /* RETURN ARGUMENTS : */
6297 : /* _ None */
6298 : /*------------------------------------------------------------------------------*/
6299 7343 : static void return_M_Least_fx_GainFrame(
6300 : const Word32 *inp, /* i: input Q18*/
6301 : const Word32 *codebook, /* i: codebook Q18*/
6302 : const Word16 num_grp, /* i: number of centroids */
6303 : const Word16 interNum, /* i: number on short list prior to 2nd stage search*/
6304 : Word16 *least /* o: return value */ )
6305 : {
6306 : Word16 i, k;
6307 : Word32 distance[1024], mindist;
6308 :
6309 : Word16 n;
6310 : Word32 diff[1024];
6311 : Word32 max_diff;
6312 7343 : mindist = L_add( MAX_32, 0 );
6313 :
6314 7343 : diff[0] = L_sub( *inp, codebook[0] );
6315 7343 : move32();
6316 7343 : max_diff = L_abs( diff[0] );
6317 352736 : FOR( i = 1; i < num_grp; i++ )
6318 : {
6319 345393 : diff[i] = L_sub( *inp, codebook[i] );
6320 345393 : move32();
6321 345393 : max_diff = L_max( max_diff, L_abs( diff[i] ) );
6322 : }
6323 7343 : n = 31;
6324 7343 : if ( max_diff != 0 )
6325 : {
6326 7343 : n = norm_l( max_diff );
6327 : }
6328 :
6329 360079 : FOR( i = 0; i < num_grp; i++ )
6330 : {
6331 352736 : diff[i] = L_shl( diff[i], n );
6332 352736 : move32();
6333 : }
6334 :
6335 360079 : FOR( i = 0; i < num_grp; i++ )
6336 : {
6337 352736 : distance[i] = L_deposit_l( 0 );
6338 352736 : distance[i] = Mult_32_32( diff[i], diff[i] );
6339 352736 : move32();
6340 :
6341 352736 : if ( LT_32( distance[i], mindist ) )
6342 : {
6343 160499 : least[0] = i;
6344 160499 : move16();
6345 : }
6346 352736 : mindist = L_min( mindist, distance[i] );
6347 : }
6348 :
6349 7343 : distance[least[0]] = MAX_32;
6350 7343 : move32();
6351 :
6352 29372 : FOR( k = 1; k < interNum; k++ )
6353 : {
6354 22029 : mindist = L_add( MAX_32, 0 );
6355 1080237 : FOR( i = 0; i < num_grp; i++ )
6356 : {
6357 1058208 : if ( LT_32( distance[i], mindist ) )
6358 : {
6359 454859 : least[k] = i;
6360 454859 : move16();
6361 : }
6362 1058208 : mindist = L_min( mindist, distance[i] );
6363 : }
6364 :
6365 22029 : distance[least[k]] = MAX_32;
6366 22029 : move32();
6367 : }
6368 :
6369 7343 : return;
6370 : }
6371 :
6372 :
6373 : /*-------------------------------------------------------------------*
6374 : * Quant_lower_LSF_fx()
6375 : *-------------------------------------------------------------------*/
6376 :
6377 80090 : static void Quant_lower_LSF_fx(
6378 : const Word16 lsf[], /* i : Input LSFs Q15 */
6379 : Word16 lsf_q[], /* o : Quantized LSFs Q15 */
6380 : Word16 lsf_idx[] /* o : Quantized LSFs indices */
6381 : )
6382 : {
6383 : Word16 i;
6384 :
6385 80090 : lsf_idx[0] = squant_fx( lsf[0], &lsf_q[0], lsf_q_cb_fx[0], lsf_q_cb_size[0] );
6386 80090 : move16();
6387 400450 : FOR( i = 1; i < NUM_Q_LSF; i++ )
6388 : {
6389 320360 : lsf_idx[i] = squant_fx( sub( lsf[i], lsf_q[i - 1] ), &lsf_q[i], lsf_q_cb_fx[i], lsf_q_cb_size[i] );
6390 320360 : move16();
6391 320360 : lsf_q[i] = add( lsf_q[i - 1], lsf_q[i] );
6392 320360 : move16();
6393 : }
6394 :
6395 80090 : return;
6396 : }
6397 :
6398 :
6399 : /*-------------------------------------------------------------------*
6400 : * Quant_mirror_point_fx()
6401 : *-------------------------------------------------------------------*/
6402 :
6403 80090 : static Word16 Quant_mirror_point_fx(
6404 : const Word16 lsf[], /* i : Input LSFs Q15 */
6405 : const Word16 lsf_q[], /* i : Quantized LSFs Q15 */
6406 : Word16 *m /* o : Mirror point Q15 */
6407 : )
6408 : {
6409 : Word16 m_diff;
6410 : Word16 m_idx;
6411 :
6412 80090 : m_diff = mult_r( sub( lsf[NUM_Q_LSF], lsf_q[NUM_Q_LSF - 1] ), 16384 /* 0.5f in Q15 */ );
6413 :
6414 80090 : m_idx = squant_fx( m_diff, m, mirror_point_q_cb_fx, MIRROR_POINT_Q_CB_SIZE );
6415 :
6416 80090 : *m = add( lsf_q[NUM_Q_LSF - 1], *m );
6417 80090 : move16();
6418 :
6419 80090 : return m_idx;
6420 : }
6421 :
6422 :
6423 : /*-------------------------------------------------------------------*
6424 : * Find_LSF_grid()
6425 : *
6426 : * Find the best grid for the LSFs
6427 : *-------------------------------------------------------------------*/
6428 :
6429 80090 : static Word16 Find_LSF_grid_fx(
6430 : const Word16 lsf[], /* i : Input LSFs Q15 */
6431 : Word16 lsf_q[], /* o : Quantized LSFs Q15 */
6432 : const Word16 m /* i : Mirror point Q15 */
6433 : )
6434 : {
6435 : Word16 lsf_map[NUM_MAP_LSF];
6436 : Word16 grid[NUM_LSF_GRIDS][NUM_MAP_LSF];
6437 : Word16 offset;
6438 : Word16 last_q_lsf;
6439 : Word16 lsf_t[NUM_MAP_LSF];
6440 : Word16 lsf_smooth[NUM_MAP_LSF];
6441 : Word32 D, D_best;
6442 80090 : Word16 I_best = 0;
6443 : Word16 i, j;
6444 : Word16 scale;
6445 80090 : move16();
6446 :
6447 : Word16 tmp, exp, tmp1;
6448 : Word32 L_tmp;
6449 :
6450 :
6451 80090 : tmp = shl( m, 1 );
6452 80090 : lsf_map[0] = sub( tmp, lsf_q[NUM_MAP_LSF - 1 - 0] );
6453 80090 : move16();
6454 80090 : lsf_map[1] = sub( tmp, lsf_q[NUM_MAP_LSF - 1 - 1] );
6455 80090 : move16();
6456 80090 : lsf_map[2] = sub( tmp, lsf_q[NUM_MAP_LSF - 1 - 2] );
6457 80090 : move16();
6458 80090 : lsf_map[3] = sub( tmp, lsf_q[NUM_MAP_LSF - 1 - 3] );
6459 80090 : move16();
6460 80090 : lsf_map[4] = sub( tmp, lsf_q[NUM_MAP_LSF - 1 - 4] );
6461 80090 : move16();
6462 :
6463 :
6464 80090 : IF( GT_16( m, MAX_LSF_FX_2 ) )
6465 : {
6466 16708 : offset = lsf_map[0];
6467 16708 : move16();
6468 16708 : exp = norm_s( m );
6469 16708 : tmp = div_s( shl( 1, sub( 14, exp ) ), m );
6470 16708 : L_tmp = L_mult( sub( MAX_LSF_FX, m ), tmp );
6471 16708 : scale = round_fx( L_shl( L_tmp, add( exp, 1 ) ) );
6472 :
6473 100248 : FOR( i = 0; i < NUM_MAP_LSF; i++ )
6474 : {
6475 83540 : tmp = mult_r( sub( lsf_map[i], offset ), scale );
6476 83540 : lsf_map[i] = add( tmp, offset );
6477 83540 : move16();
6478 : }
6479 : }
6480 :
6481 80090 : last_q_lsf = lsf_q[NUM_Q_LSF - 1];
6482 80090 : move16();
6483 80090 : scale = sub( MAX_LSF_FX, last_q_lsf );
6484 :
6485 400450 : FOR( i = 0; i < NUM_LSF_GRIDS; i++ )
6486 : {
6487 1922160 : FOR( j = 0; j < NUM_MAP_LSF; j++ )
6488 : {
6489 1601800 : grid[i][j] = add( mult_r( lsf_grid_fx[i][j], scale ), last_q_lsf );
6490 1601800 : move16();
6491 : }
6492 : }
6493 :
6494 80090 : D_best = L_add( MAX_32, 0 );
6495 400450 : FOR( i = 0; i < NUM_LSF_GRIDS; i++ )
6496 : {
6497 320360 : D = L_deposit_l( 0 );
6498 1922160 : FOR( j = 0; j < NUM_MAP_LSF; j++ )
6499 : {
6500 : /*lsf_t[j] = (1 - grid_smoothing[j])*lsf_map[j] + grid_smoothing[j]*grid[i][j]; */
6501 1601800 : tmp = sub( 32767, grid_smoothing_fx[j] );
6502 1601800 : tmp = mult_r( tmp, lsf_map[j] );
6503 1601800 : tmp1 = mult_r( grid_smoothing_fx[j], grid[i][j] );
6504 1601800 : lsf_t[j] = add( tmp, tmp1 );
6505 1601800 : move16();
6506 :
6507 1601800 : tmp = sub( lsf_t[j], lsf[NUM_Q_LSF + j] );
6508 1601800 : D = L_mac0( D, tmp, tmp );
6509 :
6510 : /* D += (lsf_t[j] - lsf[NUM_Q_LSF + j])*(lsf_t[j] - lsf[NUM_Q_LSF + j]); */
6511 : }
6512 320360 : IF( LT_32( D, D_best ) )
6513 : {
6514 117599 : Copy( lsf_t, lsf_smooth, NUM_MAP_LSF );
6515 117599 : D_best = L_add( D, 0 );
6516 117599 : I_best = i;
6517 117599 : move16();
6518 : }
6519 : }
6520 :
6521 480540 : FOR( i = 0; i < NUM_MAP_LSF; i++ )
6522 : {
6523 400450 : lsf_q[NUM_Q_LSF + i] = lsf_smooth[i];
6524 400450 : move16();
6525 : }
6526 :
6527 80090 : return I_best;
6528 : }
6529 :
6530 : /*-------------------------------------------------------------------*
6531 : * gainFrSmooth_En_fx()
6532 : *
6533 : * Gain frame smoothing and attenuation control
6534 : *-------------------------------------------------------------------*/
6535 1350 : static void gainFrSmooth_En_fx( Encoder_State *st_fx,
6536 : Word16 *shb_frame_fx,
6537 : const Word16 *lpc_shb_fx,
6538 : const Word16 *lsp_shb_fx,
6539 : Word16 *MA_lsp_shb_spacing,
6540 : Word16 *frGainAttenuate,
6541 : Word16 *frGainSmoothEn )
6542 : {
6543 : Word16 temp_shb_frame[L_FRAME16k + L_SHB_LAHEAD];
6544 : Word32 lsp_slow_evol_rate, lsp_fast_evol_rate;
6545 : Word16 lsp_spacing;
6546 :
6547 : Word32 tempQ31;
6548 : Word16 tempQ15_1, tempQ15_2;
6549 : Word16 i;
6550 1350 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
6551 :
6552 : /* inits */
6553 1350 : *frGainAttenuate = 0;
6554 1350 : move16();
6555 1350 : *frGainSmoothEn = 0;
6556 1350 : move16();
6557 1350 : *MA_lsp_shb_spacing = 16384;
6558 1350 : move16();
6559 1350 : lsp_spacing = lsp_shb_fx[0];
6560 1350 : move16();
6561 :
6562 : /* estimate the mean square error in lsps from current frame to past frames */
6563 1350 : tempQ15_1 = sub( lsp_shb_fx[0], hBWE_TD->lsp_shb_slow_interpl_fx[0] );
6564 1350 : tempQ15_2 = sub( lsp_shb_fx[0], hBWE_TD->lsp_shb_fast_interpl_fx[0] );
6565 1350 : lsp_slow_evol_rate = L_mult( tempQ15_1, tempQ15_1 );
6566 1350 : lsp_fast_evol_rate = L_mult( tempQ15_2, tempQ15_2 );
6567 :
6568 : /* update the slow and fast lsp interp for next frame */
6569 1350 : tempQ31 = L_mult( hBWE_TD->lsp_shb_slow_interpl_fx[0], 22937 );
6570 1350 : hBWE_TD->lsp_shb_slow_interpl_fx[0] = mac_r( tempQ31, lsp_shb_fx[0], 9830 );
6571 1350 : move16();
6572 1350 : tempQ31 = L_mult( hBWE_TD->lsp_shb_fast_interpl_fx[0], 9830 );
6573 1350 : hBWE_TD->lsp_shb_fast_interpl_fx[0] = mac_r( tempQ31, lsp_shb_fx[0], 22937 );
6574 1350 : move16();
6575 :
6576 13500 : FOR( i = 1; i < LPC_SHB_ORDER; i++ )
6577 : {
6578 12150 : tempQ15_1 = sub( lsp_shb_fx[i], lsp_shb_fx[i - 1] );
6579 12150 : lsp_spacing = s_min( lsp_spacing, tempQ15_1 );
6580 :
6581 : /* estimate the mean square error in lsps from current frame to past frames */
6582 12150 : tempQ15_1 = sub( lsp_shb_fx[i], hBWE_TD->lsp_shb_slow_interpl_fx[i] );
6583 12150 : tempQ15_2 = sub( lsp_shb_fx[i], hBWE_TD->lsp_shb_fast_interpl_fx[i] );
6584 12150 : lsp_slow_evol_rate = L_mac( lsp_slow_evol_rate, tempQ15_1, tempQ15_1 );
6585 12150 : lsp_fast_evol_rate = L_mac( lsp_fast_evol_rate, tempQ15_2, tempQ15_2 );
6586 :
6587 : /* update the slow and fast interpolation lsps for next frame */
6588 12150 : tempQ31 = L_mult( hBWE_TD->lsp_shb_slow_interpl_fx[i], 22937 );
6589 12150 : hBWE_TD->lsp_shb_slow_interpl_fx[i] = mac_r( tempQ31, lsp_shb_fx[i], 9830 );
6590 12150 : move16();
6591 12150 : tempQ31 = L_mult( hBWE_TD->lsp_shb_fast_interpl_fx[i], 9830 );
6592 12150 : hBWE_TD->lsp_shb_fast_interpl_fx[i] = mac_r( tempQ31, lsp_shb_fx[i], 22937 );
6593 12150 : move16();
6594 : }
6595 :
6596 1350 : test();
6597 1350 : test();
6598 1350 : IF( NE_16( st_fx->last_extl, SWB_TBE ) && NE_16( st_fx->last_extl, FB_TBE ) && LT_16( lsp_spacing, 262 ) )
6599 : {
6600 0 : hBWE_TD->lsp_shb_spacing_fx[0] = lsp_spacing;
6601 0 : move16();
6602 0 : hBWE_TD->lsp_shb_spacing_fx[1] = lsp_spacing;
6603 0 : move16();
6604 0 : hBWE_TD->lsp_shb_spacing_fx[2] = lsp_spacing;
6605 0 : move16();
6606 0 : hBWE_TD->prev_frGainAtten = 1;
6607 0 : move16();
6608 0 : set16_fx( hBWE_TD->shb_inv_filt_mem_fx, 0, LPC_SHB_ORDER );
6609 : }
6610 :
6611 : /* Estimate the moving average LSP spacing */
6612 1350 : tempQ31 = L_mult( hBWE_TD->lsp_shb_spacing_fx[0], 3277 ); /* 0.1f */
6613 1350 : tempQ31 = L_mac( tempQ31, hBWE_TD->lsp_shb_spacing_fx[1], 6553 ); /* 0.2f */
6614 1350 : tempQ31 = L_mac( tempQ31, hBWE_TD->lsp_shb_spacing_fx[2], 9830 ); /* 0.3f */
6615 1350 : *MA_lsp_shb_spacing = mac_r( tempQ31, lsp_spacing, 13107 ); /* 0.4f */
6616 1350 : move16();
6617 :
6618 1350 : hBWE_TD->lsp_shb_spacing_fx[0] = hBWE_TD->lsp_shb_spacing_fx[1];
6619 1350 : move16();
6620 1350 : hBWE_TD->lsp_shb_spacing_fx[1] = hBWE_TD->lsp_shb_spacing_fx[2];
6621 1350 : move16();
6622 1350 : hBWE_TD->lsp_shb_spacing_fx[2] = lsp_spacing;
6623 1350 : move16();
6624 :
6625 1350 : test();
6626 1350 : test();
6627 1350 : test();
6628 1350 : IF( ( LT_16( lsp_spacing, 262 ) && ( LT_16( *MA_lsp_shb_spacing, 164 ) || EQ_16( hBWE_TD->prev_frGainAtten, 1 ) ) ) || LE_16( lsp_spacing, 105 ) )
6629 : {
6630 4 : *frGainAttenuate = 1;
6631 4 : move16();
6632 :
6633 4 : IF( NE_32( st_fx->total_brate, ACELP_24k40 ) )
6634 : {
6635 2 : Copy( shb_frame_fx, temp_shb_frame, L_FRAME16k + L_SHB_LAHEAD );
6636 2 : fir_fx( temp_shb_frame, lpc_shb_fx, shb_frame_fx, hBWE_TD->shb_inv_filt_mem_fx, L_FRAME16k + L_SHB_LAHEAD, LPC_SHB_ORDER, 1, 3 );
6637 : }
6638 : ELSE
6639 : {
6640 2 : set16_fx( hBWE_TD->shb_inv_filt_mem_fx, 0, LPC_SHB_ORDER );
6641 : }
6642 :
6643 4 : test();
6644 4 : if ( LT_32( lsp_slow_evol_rate, 2147484l /*0.001f Q31*/ ) && LT_32( lsp_fast_evol_rate, 2147484l /*0.001f Q31*/ ) )
6645 : {
6646 0 : *frGainSmoothEn = 1;
6647 0 : move16();
6648 : }
6649 : }
6650 1350 : }
6651 :
6652 : /*-------------------------------------------------------------------*
6653 : * gainFrSmooth_En_ivas_fx()
6654 : *
6655 : * Gain frame smoothing and attenuation control
6656 : *-------------------------------------------------------------------*/
6657 115109 : static void gainFrSmooth_En_ivas_fx( Encoder_State *st_fx,
6658 : Word16 *shb_frame_fx /* Q_in */,
6659 : const Word16 *lpc_shb_fx /* Q12 */,
6660 : const Word16 *lsp_shb_fx /* Q15 */,
6661 : Word16 *MA_lsp_shb_spacing /* Q15 */,
6662 : Word16 *frGainAttenuate /* Q0 */,
6663 : Word16 *frGainSmoothEn /* Q0 */ )
6664 : {
6665 : Word16 temp_shb_frame[L_FRAME16k + L_SHB_LAHEAD];
6666 : Word32 lsp_slow_evol_rate, lsp_fast_evol_rate;
6667 : Word16 lsp_slow_evol_rate_e, lsp_fast_evol_rate_e;
6668 : Word16 lsp_spacing;
6669 :
6670 : Word32 tempQ31;
6671 : Word16 tempQ15_1, tempQ15_2;
6672 : Word16 i;
6673 115109 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
6674 :
6675 : /* inits */
6676 115109 : *frGainAttenuate = 0;
6677 115109 : move16();
6678 115109 : *frGainSmoothEn = 0;
6679 115109 : move16();
6680 115109 : *MA_lsp_shb_spacing = 16384; /* 0.5f in Q15 */
6681 115109 : move16();
6682 115109 : lsp_spacing = lsp_shb_fx[0];
6683 115109 : move16();
6684 :
6685 : /* estimate the mean square error in lsps from current frame to past frames */
6686 115109 : tempQ15_1 = sub( lsp_shb_fx[0], hBWE_TD->lsp_shb_slow_interpl_fx[0] );
6687 115109 : tempQ15_2 = sub( lsp_shb_fx[0], hBWE_TD->lsp_shb_fast_interpl_fx[0] );
6688 115109 : lsp_slow_evol_rate = L_mult( tempQ15_1, tempQ15_1 );
6689 115109 : lsp_slow_evol_rate_e = 0;
6690 115109 : move16();
6691 115109 : lsp_fast_evol_rate = L_mult( tempQ15_2, tempQ15_2 );
6692 115109 : lsp_fast_evol_rate_e = 0;
6693 115109 : move16();
6694 :
6695 : /* update the slow and fast lsp interp for next frame */
6696 115109 : tempQ31 = L_mult( hBWE_TD->lsp_shb_slow_interpl_fx[0], 22937 /* 0.7f in Q15 */ );
6697 115109 : hBWE_TD->lsp_shb_slow_interpl_fx[0] = mac_r( tempQ31, lsp_shb_fx[0], 9830 /* 0.3f in Q15 */ );
6698 115109 : move16();
6699 115109 : tempQ31 = L_mult( hBWE_TD->lsp_shb_fast_interpl_fx[0], 9830 /* 0.3f in Q15 */ );
6700 115109 : hBWE_TD->lsp_shb_fast_interpl_fx[0] = mac_r( tempQ31, lsp_shb_fx[0], 22937 /* 0.7f in Q15 */ );
6701 115109 : move16();
6702 :
6703 1151090 : FOR( i = 1; i < LPC_SHB_ORDER; i++ )
6704 : {
6705 1035981 : tempQ15_1 = sub( lsp_shb_fx[i], lsp_shb_fx[i - 1] );
6706 1035981 : lsp_spacing = s_min( lsp_spacing, tempQ15_1 );
6707 :
6708 : /* estimate the mean square error in lsps from current frame to past frames */
6709 1035981 : tempQ15_1 = sub( lsp_shb_fx[i], hBWE_TD->lsp_shb_slow_interpl_fx[i] );
6710 1035981 : tempQ15_2 = sub( lsp_shb_fx[i], hBWE_TD->lsp_shb_fast_interpl_fx[i] );
6711 1035981 : lsp_slow_evol_rate = BASOP_Util_Add_Mant32Exp( lsp_slow_evol_rate, lsp_slow_evol_rate_e, L_mult( tempQ15_1, tempQ15_1 ), 0, &lsp_slow_evol_rate_e );
6712 1035981 : lsp_fast_evol_rate = BASOP_Util_Add_Mant32Exp( lsp_fast_evol_rate, lsp_fast_evol_rate_e, L_mult( tempQ15_2, tempQ15_2 ), 0, &lsp_fast_evol_rate_e );
6713 :
6714 : /* update the slow and fast interpolation lsps for next frame */
6715 1035981 : tempQ31 = L_mult( hBWE_TD->lsp_shb_slow_interpl_fx[i], 22937 /* 0.7f in Q15 */ );
6716 1035981 : hBWE_TD->lsp_shb_slow_interpl_fx[i] = mac_r( tempQ31, lsp_shb_fx[i], 9830 /* 0.3f in Q15 */ );
6717 1035981 : move16();
6718 1035981 : tempQ31 = L_mult( hBWE_TD->lsp_shb_fast_interpl_fx[i], 9830 /* 0.3f in Q15 */ );
6719 1035981 : hBWE_TD->lsp_shb_fast_interpl_fx[i] = mac_r( tempQ31, lsp_shb_fx[i], 22937 /* 0.7f in Q15 */ );
6720 1035981 : move16();
6721 : }
6722 :
6723 115109 : test();
6724 115109 : test();
6725 115109 : IF( NE_16( st_fx->last_extl, SWB_TBE ) && NE_16( st_fx->last_extl, FB_TBE ) && LT_16( lsp_spacing, 262 /* 0.008f in Q15 */ ) )
6726 : {
6727 62 : hBWE_TD->lsp_shb_spacing_fx[0] = lsp_spacing;
6728 62 : move16();
6729 62 : hBWE_TD->lsp_shb_spacing_fx[1] = lsp_spacing;
6730 62 : move16();
6731 62 : hBWE_TD->lsp_shb_spacing_fx[2] = lsp_spacing;
6732 62 : move16();
6733 62 : hBWE_TD->prev_frGainAtten = 1;
6734 62 : move16();
6735 62 : set16_fx( hBWE_TD->shb_inv_filt_mem_fx, 0, LPC_SHB_ORDER );
6736 : }
6737 :
6738 : /* Estimate the moving average LSP spacing */
6739 115109 : tempQ31 = L_mult( hBWE_TD->lsp_shb_spacing_fx[0], 3277 ); /* 0.1f in Q15 */
6740 115109 : tempQ31 = L_mac( tempQ31, hBWE_TD->lsp_shb_spacing_fx[1], 6553 ); /* 0.2f in Q15 */
6741 115109 : tempQ31 = L_mac( tempQ31, hBWE_TD->lsp_shb_spacing_fx[2], 9830 ); /* 0.3f in Q15 */
6742 115109 : *MA_lsp_shb_spacing = mac_r( tempQ31, lsp_spacing, 13107 ); /* 0.4f in Q15 */
6743 115109 : move16();
6744 :
6745 115109 : hBWE_TD->lsp_shb_spacing_fx[0] = hBWE_TD->lsp_shb_spacing_fx[1];
6746 115109 : move16();
6747 115109 : hBWE_TD->lsp_shb_spacing_fx[1] = hBWE_TD->lsp_shb_spacing_fx[2];
6748 115109 : move16();
6749 115109 : hBWE_TD->lsp_shb_spacing_fx[2] = lsp_spacing;
6750 115109 : move16();
6751 :
6752 115109 : test();
6753 115109 : test();
6754 115109 : test();
6755 115109 : IF( ( LT_16( lsp_spacing, 262 /* 0.008f in Q15 */ ) && ( LT_16( *MA_lsp_shb_spacing, 164 /* 0.005f in Q15 */ ) || EQ_16( hBWE_TD->prev_frGainAtten, 1 ) ) ) || LE_16( lsp_spacing, 105 /* 0.0032f in Q15 */ ) )
6756 : {
6757 68 : *frGainAttenuate = 1;
6758 68 : move16();
6759 :
6760 68 : IF( NE_32( st_fx->total_brate, ACELP_24k40 ) )
6761 : {
6762 68 : Copy( shb_frame_fx, temp_shb_frame, L_FRAME16k + L_SHB_LAHEAD );
6763 68 : fir_fx( temp_shb_frame, lpc_shb_fx, shb_frame_fx, hBWE_TD->shb_inv_filt_mem_fx, L_FRAME16k + L_SHB_LAHEAD, LPC_SHB_ORDER, 1, 3 );
6764 : }
6765 : ELSE
6766 : {
6767 0 : set16_fx( hBWE_TD->shb_inv_filt_mem_fx, 0, LPC_SHB_ORDER );
6768 : }
6769 :
6770 68 : test();
6771 68 : if ( ( BASOP_Util_Cmp_Mant32Exp( lsp_slow_evol_rate, lsp_slow_evol_rate_e, 2147484l /*0.001f in Q31*/, 0 ) < 0 ) && ( BASOP_Util_Cmp_Mant32Exp( lsp_fast_evol_rate, lsp_fast_evol_rate_e, 2147484l /*0.001f in Q31*/, 0 ) < 0 ) )
6772 : {
6773 0 : *frGainSmoothEn = 1;
6774 0 : move16();
6775 : }
6776 : }
6777 115109 : }
6778 :
6779 : #define MAXINT32 MAX_32
6780 3369 : static void first_VQstages_fx(
6781 : const Word16 *const *cb,
6782 : Word16 u[], /* i : vector to be encoded (prediction and mean removed) */
6783 : Word16 *levels, /* i : number of levels in each stage */
6784 : Word16 stagesVQ, /* i : number of stages */
6785 : Word16 w[], /* i : weights */
6786 : Word16 N, /* i : vector dimension */
6787 : Word16 max_inner, /* i : maximum number of swaps in inner loop */
6788 : Word16 indices_VQstage[] )
6789 : {
6790 : Word16 resid_buf[2 * LSFMBEST * M], *resid[2];
6791 : Word32 dist_buf[2 * LSFMBEST], *dist[2], en;
6792 : Word32 f_tmp, L_tmp, L_tmp1, *pTmp32;
6793 : Word16 Tmp[M], *pTmp, cs;
6794 3369 : Word16 *pTmp_short, idx_buf[2 * LSFMBEST * MAX_VQ_STAGES], parents[LSFMBEST], counter = 0, j,
6795 : m, s, c, c2, p_max, *indices[2];
6796 3369 : move16();
6797 3369 : Word16 maxC = LSFMBEST;
6798 3369 : move16();
6799 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
6800 3369 : Flag Overflow = 0;
6801 3369 : move16();
6802 : #endif
6803 :
6804 : /*float dd[16];*/
6805 : const Word16 *cb_stage, *cbp;
6806 :
6807 : /* Set pointers to previous (parent) and current node (parent node is indexed [0], current node is indexed [1]) */
6808 3369 : indices[0] = idx_buf;
6809 3369 : move16();
6810 3369 : indices[1] = idx_buf + maxC * stagesVQ;
6811 3369 : move16();
6812 3369 : resid[0] = resid_buf;
6813 3369 : move16();
6814 3369 : resid[1] = resid_buf + maxC * N;
6815 3369 : move16();
6816 3369 : dist[0] = dist_buf;
6817 3369 : move16();
6818 3369 : dist[1] = dist_buf + maxC;
6819 3369 : move16();
6820 :
6821 3369 : set16_fx( idx_buf, 0, ( const Word16 )( 2 * stagesVQ * maxC ) );
6822 3369 : set16_fx( parents, 0, maxC );
6823 :
6824 : /* Set up inital distance vector */
6825 3369 : L_tmp = L_deposit_l( 0 );
6826 23583 : FOR( j = 0; j < N; j++ )
6827 : {
6828 20214 : L_tmp1 = L_shl_o( L_mult0( u[j], w[j] ), 7, &Overflow ); /*x2.56 + Q8 + Q7 */
6829 20214 : L_tmp1 = Mult_32_16( L_tmp1, u[j] ); /*x2.56 + Q15 + x2.56 -Q15 */
6830 20214 : L_tmp = L_add( L_tmp, L_tmp1 ); /*Q0 + x2.56 +x2.56 */
6831 : }
6832 3369 : set32_fx( dist[1], L_tmp, maxC );
6833 :
6834 : /* Set up initial error (residual) vectors */
6835 3369 : pTmp = resid[1];
6836 10107 : FOR( c = 0; c < maxC; c++ )
6837 : {
6838 6738 : Copy( u, pTmp, N );
6839 6738 : pTmp += N;
6840 : }
6841 :
6842 : /*----------------------------------------------------------------*
6843 : * LSF quantization
6844 : *----------------------------------------------------------------*/
6845 :
6846 : /* Loop over all stages */
6847 3369 : m = 1;
6848 3369 : move16();
6849 6738 : FOR( s = 0; s < stagesVQ; s++ )
6850 : {
6851 : /* set codebook pointer to point to first stage */
6852 3369 : cbp = cb[s];
6853 :
6854 : /* save pointer to the beginning of the current stage */
6855 3369 : cb_stage = cbp;
6856 :
6857 : /* swap pointers to parent and current nodes */
6858 3369 : pTmp_short = indices[0];
6859 3369 : indices[0] = indices[1];
6860 3369 : indices[1] = pTmp_short;
6861 :
6862 3369 : pTmp = resid[0];
6863 3369 : resid[0] = resid[1];
6864 3369 : resid[1] = pTmp;
6865 :
6866 3369 : pTmp32 = dist[0];
6867 3369 : dist[0] = dist[1];
6868 3369 : dist[1] = pTmp32;
6869 :
6870 : /* p_max points to maximum distortion node (worst of best) */
6871 3369 : p_max = 0;
6872 3369 : move16();
6873 :
6874 : /* set distortions to a large value */
6875 3369 : set32_fx( dist[1], MAXINT32, maxC );
6876 :
6877 57273 : FOR( j = 0; j < levels[s]; j++ )
6878 : {
6879 : /* compute weighted codebook element and its energy */
6880 377328 : FOR( c2 = 0; c2 < N; c2++ )
6881 : {
6882 323424 : Tmp[c2] = shl( mult( w[c2], cbp[c2] ), 2 ); /* Q8 + x2.56 -Q15 +Q2 */
6883 323424 : move16();
6884 : }
6885 :
6886 53904 : en = L_mult( cbp[0], Tmp[0] );
6887 :
6888 323424 : FOR( c2 = 1; c2 < N; c2++ )
6889 : {
6890 269520 : en = L_mac( en, cbp[c2], Tmp[c2] ); /*x2.56 + x2.56 + Q-5 +Q1 */
6891 : }
6892 53904 : cbp += N;
6893 53904 : move16();
6894 :
6895 : /* iterate over all parent nodes */
6896 107808 : FOR( c = 0; c < m; c++ )
6897 : {
6898 53904 : pTmp = &resid[0][c * N];
6899 53904 : L_tmp = L_mult( pTmp[0], Tmp[0] );
6900 323424 : FOR( c2 = 1; c2 < N; c2++ )
6901 : {
6902 269520 : L_tmp = L_mac( L_tmp, pTmp[c2], Tmp[c2] ); /* */
6903 : }
6904 :
6905 53904 : L_tmp = L_add( dist[0][c], L_sub( en, L_shl( L_tmp, 1 ) ) );
6906 :
6907 53904 : IF( LE_32( L_tmp, dist[1][p_max] ) )
6908 : {
6909 : /* replace worst */
6910 20141 : dist[1][p_max] = L_tmp;
6911 20141 : move32();
6912 20141 : indices[1][p_max * stagesVQ + s] = j;
6913 20141 : move16();
6914 20141 : parents[p_max] = c;
6915 20141 : move16();
6916 :
6917 : /* limit number of times inner loop is entered */
6918 20141 : IF( LT_16( counter, max_inner ) )
6919 : {
6920 20141 : counter = add( counter, 1 );
6921 20141 : IF( LT_16( counter, max_inner ) )
6922 : {
6923 : /* find new worst */
6924 20141 : p_max = maximum_32_fx( dist[1], maxC, &f_tmp );
6925 : }
6926 : ELSE
6927 : {
6928 : /* find minimum distortion */
6929 0 : p_max = minimum_32_fx( dist[1], maxC, &f_tmp );
6930 : }
6931 : }
6932 : }
6933 : }
6934 : }
6935 :
6936 : /*------------------------------------------------------------*
6937 : * Compute error vectors for each node
6938 : *------------------------------------------------------------*/
6939 3369 : cs = 0;
6940 3369 : move16();
6941 10107 : FOR( c = 0; c < maxC; c++ )
6942 : {
6943 : /* subtract codebook entry from the residual vector of the parent node */
6944 6738 : pTmp = resid[1] + c * N;
6945 :
6946 6738 : Copy( resid[0] + parents[c] * N, pTmp, N );
6947 6738 : Vr_subt( pTmp, cb_stage + ( indices[1][cs + s] ) * N, pTmp, N );
6948 :
6949 : /* get indices that were used for parent node */
6950 6738 : Copy( indices[0] + parents[c] * stagesVQ, indices[1] + cs, s );
6951 6738 : cs = add( cs, stagesVQ );
6952 : }
6953 :
6954 3369 : m = maxC;
6955 3369 : move16();
6956 : }
6957 :
6958 3369 : Copy( indices[1], indices_VQstage, maxC * stagesVQ );
6959 :
6960 3369 : return;
6961 : }
6962 :
6963 20214 : static Word16 dotp_loc(
6964 : const Word16 x[], /* i : vector x[] */
6965 : const Word32 y[], /* i : vector y[] */
6966 : const Word16 n /* i : vector length */
6967 : )
6968 : {
6969 : Word16 i;
6970 : Word32 suma;
6971 20214 : Word16 guarded_bits = find_guarded_bits_fx( n );
6972 20214 : suma = L_shr( Mpy_32_16_1( y[0], x[0] ), guarded_bits );
6973 :
6974 161712 : FOR( i = 1; i < n; i++ )
6975 : {
6976 141498 : suma = L_add( suma, L_shr( Mpy_32_16_1( y[i], x[i] ), guarded_bits ) );
6977 : }
6978 20214 : suma = L_shl_sat( suma, guarded_bits );
6979 :
6980 20214 : return extract_h( suma );
6981 : }
6982 :
6983 3369 : static void LVQQuant_BWE_LSF_fx(
6984 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
6985 : const Word16 lsf_shb_fx[], /* i : unquanitzed LSFs Q15 */
6986 : Word16 Q_lsfs_fx[], /* o : quanitzed LSFs Q15 */
6987 : Word16 nbits )
6988 : {
6989 :
6990 : Word16 lsf_fx[LPC_SHB_ORDER];
6991 : Word16 lsf_q_fx[LPC_SHB_ORDER];
6992 3369 : Word16 i, c2, dim1 = 6;
6993 3369 : move16();
6994 : Word16 levels[1], numbits_VQ;
6995 : Word16 c2_fx_temp;
6996 :
6997 3369 : Word16 w_fx[8] = { 5120, 5120, 2048, 2048, 2048, 1024, 1024, 1024 }; /* Q10 */
6998 3369 : move16();
6999 3369 : move16();
7000 3369 : move16();
7001 3369 : move16();
7002 3369 : move16();
7003 3369 : move16();
7004 3369 : move16();
7005 3369 : move16();
7006 : Word16 cand_fx[LPC_SHB_ORDER], quant_fx[LPC_SHB_ORDER], e_fx[2], ftmp_fx;
7007 : const Word16 *cb_stage_fx;
7008 : Word16 indices_firstVQ[LSFMBEST];
7009 :
7010 : Word16 Idx, idx_lead, idx_scale;
7011 : UWord32 LVQ_index;
7012 : Word16 dd_fx[LATTICE_DIM], cv_out_fx[LATTICE_DIM];
7013 : Word16 bits_lvq, predictor_bits, empty_bits;
7014 :
7015 3369 : empty_bits = 0; /* This is the difference to 21 bits, if we want lower number of bits */
7016 3369 : move16();
7017 3369 : nbits = sub( nbits, empty_bits ); /* THIS IS TO SIMULATE DIFFERENT NUMBER OF input BITS - should be removed */
7018 3369 : c2 = sub( NUM_BITS_SHB_MSLVQ, nbits );
7019 3369 : numbits_VQ = config_LSF_BWE[c2 * 3];
7020 3369 : levels[0] = config_LSF_BWE[c2 * 3 + 1];
7021 3369 : move16();
7022 3369 : predictor_bits = config_LSF_BWE[c2 * 3 + 2];
7023 3369 : IF( LT_16( nbits, 19 ) )
7024 : {
7025 0 : cb_stage_fx = cb_LSF_BWE_fx[1];
7026 : }
7027 : ELSE
7028 : {
7029 3369 : cb_stage_fx = cb_LSF_BWE_fx[0];
7030 : }
7031 :
7032 37059 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
7033 : {
7034 33690 : lsf_fx[i] = sub( 16384 /* 0.5f in Q15 */, lsf_shb_fx[LPC_SHB_ORDER - 1 - i] );
7035 33690 : move16();
7036 : }
7037 :
7038 : /* remove mean */
7039 3369 : v_sub_16( lsf_fx, SHB_LSF_mean_fx, Q_lsfs_fx, LPC_SHB_ORDER );
7040 :
7041 : /* 6D multi-stage 6bit coding */
7042 3369 : first_VQstages_fx( &cb_stage_fx, Q_lsfs_fx, levels, 1, w_fx, dim1, 3000, indices_firstVQ );
7043 :
7044 :
7045 : /* 8D MSLVQ coding */
7046 3369 : bits_lvq = sub( sub( nbits, numbits_VQ ), predictor_bits );
7047 3369 : Copy( SHB_LSF_mean_fx, cand_fx, LPC_SHB_ORDER );
7048 :
7049 3369 : Idx = indices_firstVQ[0];
7050 3369 : move16();
7051 :
7052 3369 : v_add_16( cand_fx, cb_stage_fx + Idx * dim1, cand_fx, dim1 );
7053 :
7054 : /* LVQ quantization */
7055 3369 : Copy( cand_fx, quant_fx, LPC_SHB_ORDER );
7056 :
7057 3369 : v_sub_16( lsf_fx, cand_fx, dd_fx, LATTICE_DIM );
7058 :
7059 3369 : mslvq_ivas_16( dd_fx, quant_fx, cv_out_fx, &idx_lead, &idx_scale, w_fx, 0, ( nbits < 19 ), bits_lvq );
7060 :
7061 3369 : IF( EQ_16( idx_scale, -1 ) )
7062 : {
7063 0 : set16_fx( cv_out_fx, 0, LATTICE_DIM );
7064 : }
7065 3369 : v_add_16( cand_fx, quant_fx, cand_fx, LATTICE_DIM );
7066 :
7067 : /* arrange the LSF candidate vector prior to selection to an ascending order*/
7068 3369 : v_sort( cand_fx, 0, LPC_SHB_ORDER - 1 );
7069 :
7070 3369 : set16_fx( Q_lsfs_fx, 0, LPC_SHB_ORDER );
7071 : /* mvr2r( mean_lsf, Q_lsfs, LPC_SHB_ORDER ); */
7072 :
7073 3369 : v_add_16( Q_lsfs_fx, cb_stage_fx + Idx * dim1, Q_lsfs_fx, dim1 );
7074 :
7075 : /* write VQ indexes */
7076 3369 : push_indice( hBstr, IND_SHB_LSF, Idx, numbits_VQ );
7077 :
7078 : /* write MSLVQ index */
7079 3369 : LVQ_index = index_lvq_SHB_fx( idx_lead, idx_scale, bits_lvq, cv_out_fx, ( nbits < 19 ) );
7080 :
7081 3369 : IF( GT_16( bits_lvq, 15 ) )
7082 : {
7083 0 : push_indice( hBstr, IND_SHB_LSF, (Word16) ( ( LVQ_index ) & ( 0xffff >> 1 ) ), 15 );
7084 0 : push_indice( hBstr, IND_SHB_LSF, (Word16) ( ( LVQ_index ) >> 15 ) & ( 0xffff >> 1 ), bits_lvq - 15 );
7085 : }
7086 : ELSE
7087 : {
7088 3369 : push_indice( hBstr, IND_SHB_LSF, (Word16) LVQ_index, bits_lvq );
7089 : }
7090 :
7091 3369 : v_add_16( Q_lsfs_fx, quant_fx, lsf_q_fx, LATTICE_DIM ); /* quantized mean removed data for first 8 dim*/
7092 :
7093 : /* predict last 2 components */
7094 3369 : IF( predictor_bits == 0 )
7095 : {
7096 0 : lsf_q_fx[LATTICE_DIM] = dotp_loc( lsf_q_fx, LastCoefPred_0bit_fx, LATTICE_DIM );
7097 0 : move16();
7098 0 : lsf_q_fx[LATTICE_DIM + 1] = dotp_loc( lsf_q_fx, &LastCoefPred_0bit_fx[LATTICE_DIM + 1], LATTICE_DIM );
7099 0 : move16();
7100 : }
7101 : ELSE
7102 : {
7103 3369 : v_sub_16( &lsf_fx[LATTICE_DIM], &SHB_LSF_mean_fx[LATTICE_DIM], &lsf_q_fx[LATTICE_DIM], 2 );
7104 :
7105 10107 : FOR( i = 0; i < 2; i++ )
7106 : {
7107 6738 : ftmp_fx = dotp_loc( lsf_q_fx, &LastCoefPred_1bit_fx[2 * ( LATTICE_DIM + 1 ) * i], LATTICE_DIM ) - lsf_q_fx[LATTICE_DIM];
7108 :
7109 6738 : e_fx[i] = mult( ftmp_fx, ftmp_fx );
7110 6738 : move16();
7111 :
7112 6738 : ftmp_fx = dotp_loc( lsf_q_fx, &LastCoefPred_1bit_fx[2 * ( LATTICE_DIM + 1 ) * i + LATTICE_DIM + 1], LATTICE_DIM ) - lsf_q_fx[LATTICE_DIM + 1];
7113 :
7114 6738 : e_fx[i] = add_sat( e_fx[i], mult( ftmp_fx, ftmp_fx ) );
7115 6738 : move16();
7116 : }
7117 :
7118 3369 : c2_fx_temp = minimum_fx( e_fx, 2, &ftmp_fx );
7119 :
7120 3369 : lsf_q_fx[LATTICE_DIM] = dotp_loc( lsf_q_fx, &LastCoefPred_1bit_fx[2 * ( LATTICE_DIM + 1 ) * c2_fx_temp], LATTICE_DIM );
7121 3369 : move16();
7122 :
7123 3369 : lsf_q_fx[LATTICE_DIM + 1] = dotp_loc( lsf_q_fx, &LastCoefPred_1bit_fx[2 * ( LATTICE_DIM + 1 ) * c2_fx_temp + LATTICE_DIM + 1], LATTICE_DIM );
7124 3369 : move16();
7125 :
7126 3369 : push_indice( hBstr, IND_SHB_LSF, c2, 1 );
7127 : }
7128 :
7129 3369 : if ( empty_bits > 0 )
7130 : {
7131 0 : push_indice( hBstr, IND_SHB_LSF, 0, empty_bits );
7132 : }
7133 :
7134 3369 : v_add_16( SHB_LSF_mean_fx, lsf_q_fx, lsf_q_fx, LPC_SHB_ORDER );
7135 :
7136 3369 : v_sort( lsf_q_fx, 0, LPC_SHB_ORDER - 1 );
7137 :
7138 37059 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
7139 : {
7140 33690 : Q_lsfs_fx[i] = sub_sat( 16384 /* 0.5f in Q15 */, lsf_q_fx[LPC_SHB_ORDER - 1 - i] );
7141 33690 : move16();
7142 : }
7143 :
7144 3369 : return;
7145 : }
7146 :
7147 : /*-------------------------------------------------------------------*
7148 : * Quant_BWE_LSF()
7149 : *
7150 : * Quantize super highband spectral envolope
7151 : *-------------------------------------------------------------------*/
7152 :
7153 1350 : static void Quant_BWE_LSF_fx(
7154 :
7155 : Encoder_State *st_fx, /* i/o: encoder state structure */
7156 : const Word16 lsf_shb[], /* i : unquanitzed LSFs */
7157 : Word16 Q_lsfs[] /* o : quanitzed LSFs */
7158 : )
7159 : {
7160 : Word16 lsf[LPC_SHB_ORDER];
7161 : Word16 lsf_q[LPC_SHB_ORDER];
7162 : Word16 lsf_idx[NUM_Q_LSF];
7163 : Word16 i;
7164 : Word16 m_idx;
7165 : Word16 m;
7166 : Word16 grid_idx;
7167 1350 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
7168 1350 : BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
7169 :
7170 :
7171 14850 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
7172 : {
7173 13500 : lsf[i] = sub( 16384, lsf_shb[LPC_SHB_ORDER - 1 - i] );
7174 13500 : move16();
7175 : }
7176 :
7177 1350 : Quant_lower_LSF_fx( lsf, lsf_q, lsf_idx );
7178 : /* i: lsf in Q15 */
7179 : /* o: lsf_q in Q15 */
7180 :
7181 8100 : FOR( i = 0; i < NUM_Q_LSF; i++ )
7182 : {
7183 6750 : hBWE_TD->lsf_idx[i] = lsf_idx[i];
7184 6750 : move16();
7185 6750 : IF( NE_16( st_fx->codec_mode, MODE2 ) )
7186 : {
7187 3730 : push_indice( hBstr, IND_SHB_LSF, lsf_idx[i], lsf_q_num_bits[i] );
7188 : }
7189 : }
7190 :
7191 1350 : m_idx = Quant_mirror_point_fx( lsf, lsf_q, &m );
7192 :
7193 1350 : hBWE_TD->m_idx = m_idx;
7194 1350 : move16();
7195 1350 : IF( NE_16( st_fx->codec_mode, MODE2 ) )
7196 : {
7197 746 : push_indice( hBstr, IND_SHB_MIRROR, m_idx, MIRROR_POINT_BITS );
7198 : }
7199 :
7200 1350 : grid_idx = Find_LSF_grid_fx( lsf, lsf_q, m );
7201 :
7202 1350 : hBWE_TD->grid_idx = grid_idx;
7203 1350 : IF( NE_16( st_fx->codec_mode, MODE2 ) )
7204 : {
7205 746 : push_indice( hBstr, IND_SHB_GRID, grid_idx, NUM_LSF_GRID_BITS );
7206 : }
7207 :
7208 14850 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
7209 : {
7210 13500 : Q_lsfs[i] = sub( 16384, lsf_q[LPC_SHB_ORDER - 1 - i] );
7211 13500 : move16();
7212 : }
7213 :
7214 1350 : return;
7215 : }
7216 :
7217 :
7218 82109 : static void Quant_BWE_LSF_ivas_fx(
7219 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
7220 : TD_BWE_ENC_HANDLE hBWE_TD, /* i/o: TD BWE data handle */
7221 : const Word16 codec_mode, /* i : codec mode */
7222 : const Word16 lsf_shb_fx[], /* i : unquanitzed LSFs Q15 */
7223 : Word16 Q_lsfs_fx[], /* o : quanitzed LSFs Q15 */
7224 : const Word32 extl_brate /* i : extension layer bit rate */
7225 : )
7226 : {
7227 : Word16 lsf_fx[LPC_SHB_ORDER];
7228 : Word16 lsf_q_fx[LPC_SHB_ORDER];
7229 : Word16 lsf_idx[NUM_Q_LSF];
7230 : Word16 i;
7231 : Word16 m_idx;
7232 : Word16 m_fx;
7233 : Word16 grid_idx;
7234 :
7235 82109 : IF( EQ_32( extl_brate, SWB_TBE_1k75 ) )
7236 : {
7237 3369 : LVQQuant_BWE_LSF_fx( hBstr, lsf_shb_fx, Q_lsfs_fx, NUM_BITS_SHB_MSLVQ );
7238 : }
7239 : ELSE
7240 : {
7241 866140 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
7242 : {
7243 787400 : lsf_fx[i] = sub( 16384 /* 0.5f in Q15 */, lsf_shb_fx[LPC_SHB_ORDER - 1 - i] );
7244 787400 : move16();
7245 : }
7246 78740 : Quant_lower_LSF_fx( lsf_fx, lsf_q_fx, lsf_idx );
7247 :
7248 472440 : FOR( i = 0; i < NUM_Q_LSF; i++ )
7249 : {
7250 393700 : IF( EQ_16( codec_mode, MODE2 ) )
7251 : {
7252 0 : hBWE_TD->lsf_idx[i] = lsf_idx[i];
7253 0 : move16();
7254 : }
7255 : ELSE
7256 : {
7257 393700 : push_indice( hBstr, IND_SHB_LSF, lsf_idx[i], lsf_q_num_bits[i] );
7258 : }
7259 : }
7260 :
7261 78740 : m_idx = Quant_mirror_point_fx( lsf_fx, lsf_q_fx, &m_fx );
7262 :
7263 78740 : IF( EQ_16( codec_mode, MODE2 ) )
7264 : {
7265 0 : hBWE_TD->m_idx = m_idx;
7266 0 : move16();
7267 : }
7268 : ELSE
7269 : {
7270 78740 : push_indice( hBstr, IND_SHB_MIRROR, m_idx, MIRROR_POINT_BITS );
7271 : }
7272 :
7273 78740 : grid_idx = Find_LSF_grid_fx( lsf_fx, lsf_q_fx, m_fx );
7274 :
7275 78740 : IF( EQ_16( codec_mode, MODE2 ) )
7276 : {
7277 0 : hBWE_TD->grid_idx = grid_idx;
7278 0 : move16();
7279 : }
7280 : ELSE
7281 : {
7282 78740 : push_indice( hBstr, IND_SHB_GRID, grid_idx, NUM_LSF_GRID_BITS );
7283 : }
7284 :
7285 866140 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
7286 : {
7287 787400 : Q_lsfs_fx[i] = sub( 16384 /* 0.5f in Q15 */, lsf_q_fx[LPC_SHB_ORDER - 1 - i] );
7288 787400 : move16();
7289 : }
7290 : }
7291 :
7292 82109 : return;
7293 : }
7294 :
7295 : /*-------------------------------------------------------------------*
7296 : * fb_tbe_enc()
7297 : *
7298 : * FB TBE encoder, 14(resp. 15.5) - 20 kHz band encoding module
7299 : *-------------------------------------------------------------------*/
7300 :
7301 0 : void fb_tbe_enc_fx(
7302 : Encoder_State *st, /* i/o: encoder state structure */
7303 : const Word16 new_input[], /* i : input speech at 48 kHz sample rate */
7304 : const Word16 fb_exc[], /* i : FB excitation from the SWB part */
7305 : Word16 Q_fb_exc )
7306 : {
7307 : Word16 ratio;
7308 : Word16 tmp_vec[L_FRAME48k];
7309 : Word16 idxGain;
7310 : #ifdef FIX_1439_SPEEDUP_SIMPLIFY_elliptic_bpf_48k_generic
7311 : Word16 input_fhb_buffer[L_FRAME48k + 4];
7312 0 : Word16 *input_fhb = &input_fhb_buffer[0] + 4;
7313 : #else
7314 : Word16 input_fhb[L_FRAME48k];
7315 : #endif
7316 : Word16 Sample_Delay_HP;
7317 : Word32 fb_exc_energy, temp2;
7318 : Word32 L_tmp;
7319 : Word16 tmp, tmp1, tmp2, exp, exp2, exp_norm;
7320 : Word16 s_max_value, exp_temp, i;
7321 0 : TD_BWE_ENC_HANDLE hBWE_TD = st->hBWE_TD;
7322 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
7323 0 : Flag Overflow = 0;
7324 0 : move16();
7325 : #endif
7326 0 : s_max_value = 0;
7327 0 : FOR( i = 0; i < L_FRAME48k; i++ )
7328 : {
7329 0 : s_max_value = s_max( s_max_value, abs_s( new_input[i] ) );
7330 : }
7331 0 : exp_temp = norm_s( s_max_value );
7332 0 : if ( s_max_value == 0 )
7333 : {
7334 0 : exp_temp = 15;
7335 0 : move16();
7336 : }
7337 0 : exp_temp = sub( exp_temp, 1 );
7338 :
7339 0 : Copy_Scale_sig( new_input, input_fhb, L_FRAME48k, exp_temp );
7340 :
7341 : #ifdef FIX_1439_SPEEDUP_SIMPLIFY_elliptic_bpf_48k_generic
7342 0 : elliptic_bpf_48k_generic_fx(
7343 : #ifdef FIX_1439_SPEEDUP_SIMPLIFY_elliptic_bpf_48k_generic_STAGE2
7344 0 : st->element_mode,
7345 : #endif /*FIX_1439_SPEEDUP_SIMPLIFY_elliptic_bpf_48k_generic_STAGE2*/
7346 : 0, // IsUpsampled3
7347 0 : input_fhb, &exp_temp, tmp_vec, hBWE_TD->elliptic_bpf_2_48k_mem_fx, hBWE_TD->elliptic_bpf_2_48k_mem_fx_Q, full_band_bpf_2_fx );
7348 : #else
7349 : elliptic_bpf_48k_generic_fx( input_fhb, &exp_temp, tmp_vec, hBWE_TD->elliptic_bpf_2_48k_mem_fx, hBWE_TD->elliptic_bpf_2_48k_mem_fx_Q, full_band_bpf_2_fx );
7350 : #endif
7351 0 : Sample_Delay_HP = NS2SA( 48000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) - L_FRAME48k / 2;
7352 :
7353 0 : IF( NE_16( st->last_extl, FB_TBE ) )
7354 : {
7355 0 : set16_fx( hBWE_TD->old_input_fhb_fx, 0, Sample_Delay_HP );
7356 0 : hBWE_TD->old_input_fhb_fx_Q = 0;
7357 0 : move16();
7358 0 : set16_fx( tmp_vec, 0, L_FRAME16k );
7359 : }
7360 0 : Copy_Scale_sig( hBWE_TD->old_input_fhb_fx, hBWE_TD->old_input_fhb_fx, Sample_Delay_HP, sub( exp_temp, hBWE_TD->old_input_fhb_fx_Q ) );
7361 0 : hBWE_TD->old_input_fhb_fx_Q = exp_temp;
7362 0 : Copy( hBWE_TD->old_input_fhb_fx, input_fhb, Sample_Delay_HP );
7363 0 : Copy( tmp_vec, input_fhb + Sample_Delay_HP, L_FRAME48k - Sample_Delay_HP );
7364 0 : Copy( tmp_vec + L_FRAME48k - Sample_Delay_HP, hBWE_TD->old_input_fhb_fx, Sample_Delay_HP );
7365 0 : temp2 = sum2_fx_mod( input_fhb, L_FRAME48k / 2 ); /* Q11 */
7366 0 : temp2 = L_add_o( temp2, L_shl_o( hBWE_TD->prev_fb_energy_fx, sub( sub( add( exp_temp, exp_temp ), 6 ), hBWE_TD->prev_fb_energy_fx_Q ), &Overflow ), &Overflow ); /* Q11 */
7367 0 : hBWE_TD->prev_fb_energy_fx = sum2_fx_mod( input_fhb + L_FRAME48k / 2, L_FRAME48k / 2 ); /*Q11*/
7368 0 : hBWE_TD->prev_fb_energy_fx_Q = sub( add( exp_temp, exp_temp ), 6 );
7369 0 : fb_exc_energy = sum2_fx_mod( fb_exc, L_FRAME16k ); /* Q(2*Q_fb_exc+1 -7) */
7370 :
7371 : /*ratio = (float) sqrt( temp2 / fb_exc_energy );*/
7372 0 : L_tmp = L_max( 1, temp2 ); /*Q6*/
7373 0 : exp = norm_l( L_tmp );
7374 0 : tmp = extract_h( L_shl( L_tmp, exp ) );
7375 0 : exp = sub( sub( 31, sub( add( exp_temp, exp_temp ), 8 ) ), exp ); /* in Q15 (L_tmp in Q6)*/
7376 :
7377 0 : exp2 = norm_l( fb_exc_energy );
7378 0 : tmp2 = extract_h( L_shl( fb_exc_energy, exp2 ) );
7379 0 : tmp1 = sub( add( Q_fb_exc, Q_fb_exc ), 8 ); /*1 - 9*/
7380 0 : exp2 = sub( sub( 31, tmp1 ), exp2 ); /* in Q15 (L_tmp in Q6)*/
7381 :
7382 0 : exp = sub( exp2, exp ); /* Denormalize and substract */
7383 0 : IF( GT_16( tmp2, tmp ) )
7384 : {
7385 0 : tmp2 = shr( tmp2, 1 );
7386 0 : exp = add( exp, 1 );
7387 : }
7388 0 : IF( 0 != tmp )
7389 : {
7390 0 : tmp = div_s( tmp2, tmp );
7391 0 : L_tmp = L_deposit_h( tmp );
7392 0 : L_tmp = Isqrt_lc( L_tmp, &exp ); /*Q(31-exp)*/
7393 0 : L_tmp = L_max( L_shr_o( L_tmp, sub( 31, exp ), &Overflow ), 0x1 ); /* Q0 */
7394 : }
7395 : ELSE
7396 : {
7397 0 : L_tmp = 0;
7398 : }
7399 :
7400 : /* idxGain = (short)( log2_f ((float)ratio) + 0.5f );
7401 : idxGain = max( 0, min(15,idxGain) ); */
7402 0 : ratio = 0;
7403 0 : move16();
7404 0 : IF( GE_32( L_tmp, 32768 ) )
7405 : {
7406 0 : idxGain = 15;
7407 0 : move16();
7408 : }
7409 : ELSE
7410 : {
7411 0 : ratio = extract_l( L_tmp );
7412 0 : exp_norm = norm_s( ratio );
7413 0 : idxGain = sub( 14, exp_norm );
7414 0 : idxGain = s_max( 0, idxGain );
7415 : }
7416 :
7417 0 : test();
7418 0 : test();
7419 0 : IF( GT_16( idxGain, 2 ) && LT_16( idxGain, 15 ) && GT_16( ratio, add( shl( 2, sub( idxGain, 1 ) ), shl( 2, sub( idxGain, 2 ) ) ) ) )
7420 : {
7421 0 : idxGain = add( idxGain, 1 );
7422 : }
7423 :
7424 : /* ratio = (float)(1 << idxGain);*/
7425 0 : IF( st->codec_mode == MODE2 )
7426 : {
7427 0 : hBWE_TD->idxGain = idxGain;
7428 0 : move16();
7429 : }
7430 : ELSE
7431 : {
7432 0 : push_indice( st->hBstr, IND_FB_SLOPE, idxGain, 4 );
7433 : }
7434 :
7435 0 : return;
7436 : }
7437 :
7438 43652 : void fb_tbe_enc_ivas_fx(
7439 : Encoder_State *st, /* i/o: encoder state structure */
7440 : const Word16 new_input[], /* i : input speech at 48 kHz sample rate Q_new_input */
7441 : const Word16 fb_exc[], /* i : FB excitation from the SWB part Q_fb_exc */
7442 : Word16 Q_fb_exc,
7443 : Word16 Q_new_input )
7444 : {
7445 : Word16 ratio;
7446 : Word16 tmp_vec[L_FRAME48k];
7447 : Word16 idxGain;
7448 : #ifdef FIX_1439_SPEEDUP_SIMPLIFY_elliptic_bpf_48k_generic
7449 : Word16 input_fhb_new_buffer[L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ) + 4];
7450 43652 : Word16 *input_fhb_new = &input_fhb_new_buffer[0] + 4;
7451 : #else
7452 : Word16 input_fhb_new[L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS )];
7453 : #endif
7454 : Word16 input_fhb[L_FRAME48k];
7455 : Word16 Sample_Delay_HP;
7456 : Word64 fb_exc_energy;
7457 : Word16 tmp, tmp2, exp, exp2;
7458 : Word16 s_max_value, exp_temp, i;
7459 43652 : TD_BWE_ENC_HANDLE hBWE_TD = st->hBWE_TD;
7460 : Word16 q_diff, q_input_fhb;
7461 : Word64 temp1, temp2;
7462 :
7463 43652 : s_max_value = 0;
7464 43652 : move16();
7465 43913912 : FOR( i = 0; i < L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ); i++ )
7466 : {
7467 43870260 : s_max_value = s_max( s_max_value, abs_s( new_input[i - NS2SA( 48000, DELAY_FIR_RESAMPL_NS )] ) );
7468 : }
7469 43652 : exp_temp = norm_s( s_max_value );
7470 43652 : if ( s_max_value == 0 )
7471 : {
7472 8 : exp_temp = 15;
7473 8 : move16();
7474 : }
7475 :
7476 43652 : Copy_Scale_sig( new_input - NS2SA( 48000, DELAY_FIR_RESAMPL_NS ), input_fhb_new, L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ), exp_temp );
7477 43652 : exp_temp = add( exp_temp, Q_new_input );
7478 43652 : IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) )
7479 : {
7480 : #ifdef FIX_1439_SPEEDUP_SIMPLIFY_elliptic_bpf_48k_generic
7481 4957 : elliptic_bpf_48k_generic_fx(
7482 : #ifdef FIX_1439_SPEEDUP_SIMPLIFY_elliptic_bpf_48k_generic_STAGE2
7483 4957 : st->element_mode,
7484 : #endif /*FIX_1439_SPEEDUP_SIMPLIFY_elliptic_bpf_48k_generic_STAGE2*/
7485 : 0, // IsUpsampled3
7486 4957 : input_fhb_new, &exp_temp, tmp_vec, hBWE_TD->elliptic_bpf_2_48k_mem_fx, hBWE_TD->elliptic_bpf_2_48k_mem_fx_Q, full_band_bpf_2_fx );
7487 : #else
7488 : elliptic_bpf_48k_generic_fx( input_fhb_new, &exp_temp, tmp_vec, hBWE_TD->elliptic_bpf_2_48k_mem_fx, hBWE_TD->elliptic_bpf_2_48k_mem_fx_Q, full_band_bpf_2_fx );
7489 : #endif
7490 : }
7491 : ELSE
7492 : {
7493 : #ifdef FIX_1439_SPEEDUP_SIMPLIFY_elliptic_bpf_48k_generic
7494 38695 : elliptic_bpf_48k_generic_fx(
7495 : #ifdef FIX_1439_SPEEDUP_SIMPLIFY_elliptic_bpf_48k_generic_STAGE2
7496 38695 : st->element_mode,
7497 : #endif /*FIX_1439_SPEEDUP_SIMPLIFY_elliptic_bpf_48k_generic_STAGE2*/
7498 : 0, // IsUpsampled3
7499 38695 : input_fhb_new + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ), &exp_temp, tmp_vec, hBWE_TD->elliptic_bpf_2_48k_mem_fx, hBWE_TD->elliptic_bpf_2_48k_mem_fx_Q, full_band_bpf_2_fx );
7500 : #else
7501 : elliptic_bpf_48k_generic_fx( input_fhb_new + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ), &exp_temp, tmp_vec, hBWE_TD->elliptic_bpf_2_48k_mem_fx, hBWE_TD->elliptic_bpf_2_48k_mem_fx_Q, full_band_bpf_2_fx );
7502 : #endif
7503 : }
7504 :
7505 43652 : test();
7506 43652 : IF( GT_16( st->element_mode, IVAS_CPE_DFT ) || EQ_16( st->element_mode, IVAS_SCE ) )
7507 : {
7508 :
7509 38695 : IF( EQ_16( st->L_frame, L_FRAME ) )
7510 : {
7511 8626 : Sample_Delay_HP = NS2SA( 48000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS ) - L_FRAME48k / 2;
7512 : }
7513 : ELSE
7514 : {
7515 30069 : Sample_Delay_HP = NS2SA( 48000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS ) - L_FRAME48k / 2;
7516 : }
7517 : }
7518 : ELSE
7519 : {
7520 4957 : Sample_Delay_HP = NS2SA( 48000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) - L_FRAME48k / 2;
7521 : }
7522 :
7523 43652 : IF( NE_16( st->last_extl, FB_TBE ) )
7524 : {
7525 2716 : set16_fx( hBWE_TD->old_input_fhb_fx, 0, Sample_Delay_HP );
7526 2716 : hBWE_TD->old_input_fhb_fx_Q = 0;
7527 2716 : move16();
7528 2716 : set16_fx( tmp_vec, 0, L_FRAME16k );
7529 : }
7530 :
7531 43652 : q_diff = sub( exp_temp, hBWE_TD->old_input_fhb_fx_Q );
7532 43652 : Copy( hBWE_TD->old_input_fhb_fx, input_fhb, Sample_Delay_HP ); // hBWE_TD->old_input_fhb_fx_Q
7533 43652 : Copy( tmp_vec, input_fhb + Sample_Delay_HP, sub( L_FRAME48k, Sample_Delay_HP ) ); // exp_temp
7534 43652 : IF( q_diff > 0 )
7535 : {
7536 12646 : scale_sig( input_fhb + Sample_Delay_HP, sub( L_FRAME48k, Sample_Delay_HP ), negate( q_diff ) ); // hBWE_TD->old_input_fhb_fx_Q
7537 12646 : q_input_fhb = hBWE_TD->old_input_fhb_fx_Q;
7538 12646 : move16();
7539 : }
7540 : ELSE
7541 : {
7542 31006 : scale_sig( input_fhb, Sample_Delay_HP, q_diff ); // exp_temp
7543 31006 : q_input_fhb = exp_temp;
7544 31006 : move16();
7545 : }
7546 43652 : Copy( tmp_vec + L_FRAME48k - Sample_Delay_HP, hBWE_TD->old_input_fhb_fx, Sample_Delay_HP );
7547 43652 : hBWE_TD->old_input_fhb_fx_Q = exp_temp;
7548 43652 : move16();
7549 :
7550 43652 : temp2 = sum2_fx_no_sat( input_fhb, L_FRAME48k / 2 ); // 2*q_input_fhb /* Q(2*Q_input_fhb-8) */
7551 43652 : temp2 = W_add( temp2, W_shl( hBWE_TD->prev_fb_energy_fx, sub( q_input_fhb, hBWE_TD->prev_fb_energy_fx_Q ) ) ); // 2*q_input_fhb
7552 :
7553 43652 : temp1 = sum2_fx_no_sat( input_fhb + L_FRAME48k / 2, L_FRAME48k / 2 ); // 2*q_input_fhb
7554 43652 : exp = W_norm( temp1 );
7555 43652 : temp1 = W_shl( temp1, exp ); // 2*q_input_fhb+exp
7556 43652 : hBWE_TD->prev_fb_energy_fx = W_extract_h( temp1 ); // 2*q_input_fhb+exp-32
7557 43652 : move32();
7558 43652 : hBWE_TD->prev_fb_energy_fx_Q = sub( add( shl( q_input_fhb, 1 ), exp ), 32 );
7559 43652 : move16();
7560 :
7561 43652 : fb_exc_energy = sum2_fx_no_sat( fb_exc, L_FRAME16k ); // 2*Q_fb_exc
7562 :
7563 : /*ratio = (float) sqrt( temp2 / fb_exc_energy );*/
7564 43652 : exp = sub( W_norm( temp2 ), 1 );
7565 43652 : tmp = extract_h( W_extract_h( W_shl( temp2, exp ) ) ); // 2*q_input_fhb+exp-48
7566 43652 : exp = sub( 63, add( shl( q_input_fhb, 1 ), exp ) ); // 15-(2*q_input_fhb+exp-48)
7567 :
7568 43652 : exp2 = W_norm( fb_exc_energy );
7569 43652 : tmp2 = extract_h( W_extract_h( W_shl( fb_exc_energy, exp2 ) ) ); // 2*Q_fb_exc+exp2-48
7570 43652 : exp2 = sub( 63, add( shl( Q_fb_exc, 1 ), exp2 ) ); // 15-(2*Q_fb_exc+exp2-48)
7571 :
7572 43652 : ratio = 0;
7573 43652 : idxGain = 0;
7574 43652 : move16();
7575 43652 : move16();
7576 43652 : test();
7577 43652 : IF( tmp != 0 && tmp2 != 0 )
7578 : {
7579 43647 : ratio = div_s( tmp, tmp2 ); // exp: exp-exp2
7580 43647 : exp = sub( exp, exp2 );
7581 : /* ratio = (float) sqrt( temp2 / fb_exc_energy );
7582 : idxGain = (short)( log2_f ((float)ratio) + 0.5f );
7583 :
7584 : => idxGain = (short)( log2_f (sqrt( temp2 / fb_exc_energy )) + 0.5f )
7585 : = (short)(log2f(sqrt(temp2 / fb_exc_energy)) + log2f(sqrt(2)))
7586 : = (short)log2f(sqrt(2 * (temp2 / fb_exc_energy)))
7587 : increase the exp of division result by one, two accommodate the multiplication with 2 in sqrt
7588 : */
7589 43647 : exp = add( exp, 1 );
7590 43647 : ratio = Sqrt16( ratio, &exp );
7591 : /* Since ratio always lie betwwen 16384 and 32767, subtract the Q of sqrt result from 14 to get the integral part of log value */
7592 43647 : idxGain = sub( exp, 1 ); // 14 - (15-exp)
7593 :
7594 : /* idxGain = max( 0, min( 15, idxGain ) ); */
7595 43647 : idxGain = s_max( 0, s_min( 15, idxGain ) );
7596 : }
7597 :
7598 : /* ratio = (float)(1 << idxGain);*/
7599 43652 : IF( EQ_16( st->codec_mode, MODE2 ) )
7600 : {
7601 0 : hBWE_TD->idxGain = idxGain;
7602 0 : move16();
7603 : }
7604 : ELSE
7605 : {
7606 43652 : push_indice( st->hBstr, IND_FB_SLOPE, idxGain, 4 );
7607 : }
7608 :
7609 43652 : return;
7610 : }
7611 604 : void tbe_write_bitstream_fx(
7612 : Encoder_State *st_fx /* i/o: encoder state structure */
7613 : )
7614 : {
7615 : Word16 i;
7616 604 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
7617 604 : BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
7618 :
7619 :
7620 604 : test();
7621 604 : test();
7622 604 : test();
7623 604 : test();
7624 604 : test();
7625 604 : IF( ( st_fx->rf_mode || EQ_32( st_fx->total_brate, ACELP_9k60 ) ) && ( EQ_16( st_fx->bwidth, WB ) ) )
7626 : {
7627 : /* WB LSF */
7628 0 : push_next_indice( hBstr, hBWE_TD->lsf_WB, NUM_BITS_LBR_WB_LSF );
7629 :
7630 : /* WB frame */
7631 0 : push_next_indice( hBstr, hBWE_TD->gFrame_WB, NUM_BITS_SHB_FrameGain_LBR_WB );
7632 : }
7633 604 : ELSE IF( ( GE_32( st_fx->total_brate, ACELP_9k60 ) ) && ( LE_32( st_fx->total_brate, ACELP_32k ) ) &&
7634 : ( ( EQ_16( st_fx->bwidth, SWB ) ) || ( EQ_16( st_fx->bwidth, FB ) ) ) )
7635 : {
7636 : /* LSF coefficients */
7637 :
7638 604 : test();
7639 604 : IF( ( EQ_16( st_fx->rf_mode, 1 ) ) || EQ_32( st_fx->total_brate, ACELP_9k60 ) )
7640 : {
7641 0 : push_next_indice( hBstr, hBWE_TD->lsf_idx[0], 8 );
7642 : }
7643 : ELSE
7644 : {
7645 3624 : FOR( i = 0; i < NUM_Q_LSF; i++ )
7646 : {
7647 3020 : push_next_indice( hBstr, hBWE_TD->lsf_idx[i], lsf_q_num_bits[i] );
7648 : }
7649 :
7650 : /* LSF mirror points */
7651 604 : push_next_indice( hBstr, hBWE_TD->m_idx, MIRROR_POINT_BITS );
7652 :
7653 : /* LSF grid points */
7654 604 : push_next_indice( hBstr, hBWE_TD->grid_idx, NUM_LSF_GRID_BITS );
7655 : }
7656 :
7657 : /* Gain shape */
7658 604 : push_next_indice( hBstr, hBWE_TD->idxSubGains, NUM_BITS_SHB_SUBGAINS );
7659 :
7660 : /* frame gain */
7661 604 : push_next_indice( hBstr, hBWE_TD->idxFrameGain, NUM_BITS_SHB_FRAMEGAIN );
7662 :
7663 604 : IF( GE_32( st_fx->total_brate, ACELP_24k40 ) )
7664 : {
7665 : /* sub frame energy*/
7666 604 : push_next_indice( hBstr, hBWE_TD->idx_shb_fr_gain, NUM_BITS_SHB_ENER_SF );
7667 :
7668 : /* gain shapes residual */
7669 3624 : FOR( i = 0; i < NB_SUBFR16k; i++ )
7670 : {
7671 3020 : push_next_indice( hBstr, hBWE_TD->idx_res_gs[i], NUM_BITS_SHB_RES_GS );
7672 : }
7673 :
7674 : /* voicing factor */
7675 604 : push_next_indice( hBstr, hBWE_TD->idx_mixFac, NUM_BITS_SHB_VF );
7676 : }
7677 :
7678 604 : IF( EQ_16( st_fx->tec_tfa, 1 ) )
7679 : {
7680 604 : push_next_indice( hBstr, st_fx->tec_flag, BITS_TEC );
7681 604 : push_next_indice( hBstr, st_fx->tfa_flag, BITS_TFA );
7682 : }
7683 : }
7684 :
7685 604 : IF( EQ_16( st_fx->bwidth, FB ) )
7686 : {
7687 0 : push_next_indice( hBstr, hBWE_TD->idxGain, 4 );
7688 : }
7689 604 : }
7690 :
7691 :
7692 241948 : void TBEreset_enc_fx(
7693 : Encoder_State *st_fx, /* i/o: encoder state structure */
7694 : Word16 bandwidth /* i : bandwidth mode */
7695 : )
7696 : {
7697 241948 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
7698 :
7699 241948 : IF( NE_16( st_fx->last_core, ACELP_CORE ) )
7700 : {
7701 240455 : set16_fx( hBWE_TD->old_bwe_exc_fx, 0, PIT16k_MAX * 2 );
7702 240455 : hBWE_TD->bwe_non_lin_prev_scale_fx = L_deposit_l( 0 );
7703 240455 : move32();
7704 240455 : st_fx->prev_Q_bwe_exc = 31;
7705 240455 : move16();
7706 : }
7707 :
7708 241948 : test();
7709 241948 : IF( EQ_16( bandwidth, WB ) )
7710 : {
7711 10286 : wb_tbe_extras_reset_fx( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx, hBWE_TD->mem_genSHBexc_filt_down_wb3_fx );
7712 10286 : set16_fx( hBWE_TD->mem_genSHBexc_filt_down_shb_fx, 0, 7 );
7713 10286 : set16_fx( hBWE_TD->state_lpc_syn_fx, 0, 10 );
7714 10286 : set16_fx( hBWE_TD->state_syn_shbexc_fx, 0, L_SHB_LAHEAD / 4 );
7715 10286 : set16_fx( hBWE_TD->syn_overlap_fx, 0, L_SHB_LAHEAD );
7716 10286 : set32_fx( hBWE_TD->mem_csfilt_fx, 0, 2 );
7717 : }
7718 231662 : ELSE IF( ( EQ_16( bandwidth, SWB ) ) || ( EQ_16( bandwidth, FB ) ) )
7719 : {
7720 231662 : set16_fx( hBWE_TD->state_ana_filt_shb_fx, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
7721 :
7722 231662 : swb_tbe_reset_fx( hBWE_TD->mem_csfilt_fx, hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->state_lpc_syn_fx,
7723 231662 : hBWE_TD->syn_overlap_fx, hBWE_TD->state_syn_shbexc_fx, &( hBWE_TD->tbe_demph_fx ), &( hBWE_TD->tbe_premph_fx ),
7724 231662 : hBWE_TD->mem_stp_swb_fx, &( hBWE_TD->gain_prec_swb_fx ) );
7725 :
7726 231662 : IF( EQ_16( bandwidth, FB ) )
7727 : {
7728 160241 : set16_fx( hBWE_TD->fb_state_lpc_syn_fx, 0, LPC_SHB_ORDER );
7729 160241 : hBWE_TD->fb_tbe_demph_fx = 0;
7730 160241 : move16();
7731 160241 : fb_tbe_reset_enc_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx, &hBWE_TD->prev_fb_energy_fx, hBWE_TD->elliptic_bpf_2_48k_mem_fx_Q, &hBWE_TD->prev_fb_energy_fx_Q );
7732 : }
7733 : }
7734 241948 : }
7735 :
7736 : /*-------------------------------------------------------------------*
7737 : * InitSWBencBufferStates()
7738 : *
7739 : * Initialize SWB buffer states
7740 : *-------------------------------------------------------------------*/
7741 :
7742 6998 : void InitSWBencBufferStates_fx(
7743 : TD_BWE_ENC_HANDLE hBWE_TD, /* i/o: TD BWE data handle */
7744 : Word16 *shb_speech /* o : SHB target signal (6-14kHz) at 16kHz */
7745 : )
7746 : {
7747 6998 : IF( shb_speech != NULL )
7748 : {
7749 0 : set16_fx( shb_speech, 0, L_FRAME16k );
7750 : }
7751 :
7752 6998 : set16_fx( hBWE_TD->old_speech_shb_fx, 0, L_LOOK_16k + L_SUBFR16k );
7753 6998 : set16_fx( hBWE_TD->mem_shb_res_fx, 0, MAX_LEN_MA_FILTER );
7754 6998 : set16_fx( hBWE_TD->old_EnvSHBres_fx, 0, L_FRAME4k );
7755 6998 : hBWE_TD->old_mean_EnvSHBres_fx = 0;
7756 6998 : move16();
7757 6998 : hBWE_TD->prev_enr_EnvSHBres_fx = 1; /*1.0f in Q0*/
7758 6998 : move32();
7759 6998 : hBWE_TD->prev_enr_EnvSHBres_e = 31;
7760 6998 : move16();
7761 6998 : hBWE_TD->prev_shb_env_tilt_fx = 0;
7762 6998 : move16();
7763 6998 : hBWE_TD->prev_Q_shb = 15;
7764 6998 : move16();
7765 6998 : hBWE_TD->prev_pow_exc16kWhtnd_fx32 = 1; /*1.0f in Q0*/
7766 6998 : move32();
7767 6998 : hBWE_TD->prev_mix_factor_fx = 32767; /*1.0f in Q15*/
7768 6998 : move16();
7769 6998 : hBWE_TD->prev_Env_error_fx = 0;
7770 6998 : move16();
7771 :
7772 6998 : return;
7773 : }
|