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 "rom_com.h"
9 : #include "prot_fx.h"
10 : #include "rom_dec.h"
11 : #include "stl.h"
12 :
13 : #include "ivas_prot_fx.h"
14 :
15 : /*-----------------------------------------------------------------*
16 : * Local functions
17 : *-----------------------------------------------------------------*/
18 :
19 : static void dequantizeSHBparams_fx_9_1( Decoder_State *st_fx, const Word16 extl, Word32 extl_brate, Word16 *Q_lsf, Word16 *Q_subgain, Word32 *Q_framegrain, Word16 *uv_flag, Word32 *Q_shb_ener_sf_32, Word16 *Q_shb_res_gshape, Word16 *Q_mixFactors );
20 :
21 : static void find_max_mem_dec( Decoder_State *st_fx, Word16 *n_mem, Word16 *n_mem2, Word16 *n_mem3 );
22 : static void rescale_genSHB_mem_dec( Decoder_State *st_fx, Word16 sf );
23 : static void find_max_mem_wb( Decoder_State *st_fx, Word16 *n_mem );
24 : static void rescale_genWB_mem( Decoder_State *st_fx, Word16 sf );
25 : static void Dequant_lower_LSF_fx( const Word16 lsf_idx[], Word16 lsf_q[] );
26 : static void Map_higher_LSF_fx( Word16 lsf_q[], const Word16 m, const Word16 grid_in[] );
27 : static void Dequant_mirror_point_fx( const Word16 lsf_q[], const Word16 m_idx, Word16 *m );
28 : static Word16 dotp_loc( const Word16 x[], const Word32 y[], const Word16 n );
29 : static void find_max_mem_dec_m3( Decoder_State *st, Word16 *n_mem3 );
30 :
31 : /* gain shape concealment code */
32 : static void gradientGainShape( Decoder_State *st_fx, Word16 *GainShape, Word32 *GainFrame );
33 :
34 : /*-------------------------------------------------------------------*
35 : * find_max_mem_dec()
36 : *
37 : * Find norm and max in TBE memories and past buffers
38 : *-------------------------------------------------------------------*/
39 106114 : static void find_max_mem_dec(
40 : Decoder_State *st_fx,
41 : Word16 *n_mem,
42 : Word16 *n_mem2,
43 : Word16 *n_mem3 )
44 : {
45 : Word16 i;
46 : Word16 n_mem_32;
47 106114 : Word16 max = 0;
48 106114 : move16();
49 106114 : Word32 Lmax = 0;
50 106114 : move32();
51 106114 : Word16 tempQ15, max2 = 0;
52 106114 : move16();
53 : Word16 max3;
54 : Word32 tempQ32, Lmax3;
55 : TD_BWE_DEC_HANDLE hBWE_TD;
56 106114 : hBWE_TD = st_fx->hBWE_TD;
57 :
58 : /* old BWE exc max */
59 106114 : IF( EQ_16( st_fx->element_mode, EVS_MONO ) )
60 : {
61 17407 : FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
62 : {
63 16068 : tempQ15 = abs_s( hBWE_TD->old_bwe_exc_extended_fx[i] );
64 16068 : max = s_max( max, tempQ15 );
65 : }
66 : }
67 :
68 : /* decimate all-pass steep memory */
69 848912 : FOR( i = 0; i < 7; i++ )
70 : {
71 742798 : tempQ15 = abs_s( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] );
72 742798 : max = s_max( max, tempQ15 );
73 : }
74 :
75 : /* -- keep norm of state_lpc_syn_fx, state_syn_shbexc_fx,
76 : and mem_stp_swb_fx separately for 24.4 and 32kbps ----*/
77 : /* findMaxMem2() inside tbe com */
78 1167254 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
79 : {
80 1061140 : tempQ15 = abs_s( hBWE_TD->state_lpc_syn_fx[i] );
81 1061140 : max2 = s_max( max2, tempQ15 );
82 : }
83 :
84 : /* findMaxMem2() inside tbe com */
85 2228394 : FOR( i = 0; i < L_SHB_LAHEAD; i++ )
86 : {
87 2122280 : tempQ15 = abs_s( hBWE_TD->state_syn_shbexc_fx[i] );
88 2122280 : max2 = s_max( max2, tempQ15 );
89 : }
90 :
91 : /* findMaxMem2() inside tbe com */
92 1167254 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
93 : {
94 1061140 : tempQ15 = abs_s( hBWE_TD->mem_stp_swb_fx[i] );
95 1061140 : max2 = s_max( max2, tempQ15 );
96 : }
97 :
98 : /* for total_brate > 16.4kbps, use n_mem2; else account for the max2 for n_mem calculation */
99 106114 : *n_mem2 = norm_s( max2 );
100 106114 : move16();
101 106114 : if ( max2 == 0 )
102 : {
103 85455 : *n_mem2 = 15;
104 85455 : move16();
105 : }
106 :
107 106114 : if ( LT_32( st_fx->total_brate, ACELP_24k40 ) )
108 : {
109 82055 : max = s_max( max, max2 );
110 : }
111 :
112 : /* de-emph and pre-emph memory */
113 106114 : tempQ15 = abs_s( hBWE_TD->tbe_demph_fx );
114 106114 : max = s_max( max, tempQ15 );
115 :
116 106114 : tempQ15 = abs_s( hBWE_TD->tbe_premph_fx );
117 106114 : max = s_max( max, tempQ15 );
118 :
119 106114 : IF( EQ_16( st_fx->extl, FB_TBE ) )
120 : {
121 447865 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
122 : {
123 407150 : tempQ15 = abs_s( hBWE_TD->fb_state_lpc_syn_fx[i] );
124 407150 : max = s_max( max, tempQ15 );
125 : }
126 : /* FB de-emph memory */
127 40715 : tempQ15 = abs_s( hBWE_TD->fb_tbe_demph_fx );
128 40715 : max = s_max( max, tempQ15 );
129 : }
130 : /* estimate the norm for 16-bit memories */
131 106114 : *n_mem = norm_s( max );
132 106114 : move16();
133 106114 : if ( max == 0 )
134 : {
135 9037 : *n_mem = 15;
136 9037 : move16();
137 : }
138 :
139 : /* estimate the norm for 32-bit memories */
140 106114 : Lmax = L_abs( hBWE_TD->mem_csfilt_fx[0] ); /* only element [0] is used in env. shaping */
141 :
142 106114 : n_mem_32 = norm_l( Lmax );
143 106114 : if ( Lmax == 0 )
144 : {
145 35505 : n_mem_32 = 31;
146 35505 : move16();
147 : }
148 :
149 106114 : tempQ15 = sub( s_min( *n_mem, n_mem_32 ), 1 );
150 106114 : *n_mem = s_max( tempQ15, 0 );
151 106114 : move16();
152 :
153 : /* --------------------------------------------------------------*/
154 : /* Find headroom for synthesis stage associated with these memories:
155 : 1. st_fx->syn_overlap_fx
156 : 2. st_fx->genSHBsynth_state_lsyn_filt_shb_local
157 : 3. st_fx->genSHBsynth_Hilbert_Mem_fx (32-bit) */
158 106114 : max3 = 0;
159 106114 : move16();
160 : /* find max in prev overlapSyn */
161 2228394 : FOR( i = 0; i < L_SHB_LAHEAD; i++ )
162 : {
163 2122280 : tempQ15 = abs_s( hBWE_TD->syn_overlap_fx[i] );
164 2122280 : max3 = s_max( max3, tempQ15 );
165 : }
166 : /* find max in prev genSHBsynth_state_lsyn_filt_shb_local_fx */
167 742798 : FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
168 : {
169 636684 : tempQ15 = abs_s( hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx[i] );
170 636684 : max3 = s_max( max3, tempQ15 );
171 : }
172 : /* find max in prev int_3_over_2_tbemem_dec_fx */
173 106114 : IF( EQ_32( st_fx->output_Fs, 48000 ) )
174 : {
175 1174256 : FOR( i = 0; i < INTERP_3_2_MEM_LEN; i++ )
176 : {
177 1100865 : tempQ15 = abs_s( hBWE_TD->int_3_over_2_tbemem_dec_fx[i] );
178 1100865 : max3 = s_max( max3, tempQ15 );
179 : }
180 : }
181 106114 : IF( EQ_32( st_fx->output_Fs, 16000 ) )
182 : {
183 34776 : FOR( i = 0; i < ( 2 * ALLPASSSECTIONS_STEEP + 1 ); i++ )
184 : {
185 30429 : tempQ15 = abs_s( hBWE_TD->mem_resamp_HB_32k_fx[i] );
186 30429 : max3 = s_max( max3, tempQ15 );
187 : }
188 : }
189 : /* estimate the norm for 16-bit memories */
190 106114 : *n_mem3 = norm_s( max3 );
191 106114 : move16();
192 106114 : if ( max3 == 0 )
193 : {
194 9139 : *n_mem3 = 15;
195 9139 : move16();
196 : }
197 :
198 106114 : Lmax3 = 0;
199 106114 : move32();
200 106114 : IF( EQ_16( st_fx->L_frame, L_FRAME ) )
201 : {
202 : /* find max in prev genSHBsynth_Hilbert_Mem_fx */
203 1094676 : FOR( i = 0; i < HILBERT_MEM_SIZE; i++ )
204 : {
205 1044918 : tempQ32 = L_abs( hBWE_TD->genSHBsynth_Hilbert_Mem_fx[i] );
206 1044918 : Lmax3 = L_max( Lmax3, tempQ32 );
207 : }
208 : }
209 :
210 : /* estimate the norm for 32-bit memories */
211 106114 : n_mem_32 = norm_l( Lmax3 );
212 106114 : if ( Lmax3 == 0 )
213 : {
214 60970 : n_mem_32 = 31;
215 60970 : move16();
216 : }
217 :
218 106114 : tempQ15 = sub( s_min( *n_mem3, n_mem_32 ), 2 ); /* very important leave at least 2 bit head room
219 : because of the Hilber transform and Q14 coeffs */
220 106114 : *n_mem3 = s_max( tempQ15, 0 );
221 106114 : move16();
222 : /* --------------------------------------------------------------*/
223 106114 : }
224 :
225 : /*-------------------------------------------------------------------*
226 : * rescale_genSHB_mem_dec()
227 : *
228 : * Rescale genSHB memories
229 : *-------------------------------------------------------------------*/
230 714 : static void rescale_genSHB_mem_dec(
231 : Decoder_State *st_fx,
232 : Word16 sf )
233 : {
234 : Word16 i;
235 : TD_BWE_DEC_HANDLE hBWE_TD;
236 714 : hBWE_TD = st_fx->hBWE_TD;
237 :
238 714 : IF( EQ_16( st_fx->element_mode, EVS_MONO ) )
239 : {
240 9282 : FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
241 : {
242 8568 : hBWE_TD->old_bwe_exc_extended_fx[i] = shl( hBWE_TD->old_bwe_exc_extended_fx[i], sf );
243 8568 : move16();
244 : }
245 : }
246 :
247 5712 : FOR( i = 0; i < 7; i++ )
248 : {
249 4998 : hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i], sf );
250 4998 : move16();
251 : }
252 :
253 : /* -- Apply memory scaling for 13.2 and 16.4k bps using sf ----*/
254 714 : IF( LT_32( st_fx->total_brate, ACELP_24k40 ) )
255 : {
256 4323 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
257 : {
258 3930 : hBWE_TD->state_lpc_syn_fx[i] = shl( hBWE_TD->state_lpc_syn_fx[i], sf );
259 3930 : move16();
260 : }
261 :
262 8253 : FOR( i = 0; i < L_SHB_LAHEAD; i++ )
263 : {
264 7860 : hBWE_TD->state_syn_shbexc_fx[i] = shl( hBWE_TD->state_syn_shbexc_fx[i], sf );
265 7860 : move16();
266 : }
267 : }
268 :
269 714 : if ( EQ_16( st_fx->extl, FB_TBE ) )
270 : {
271 : }
272 714 : hBWE_TD->mem_csfilt_fx[0] = L_shl( hBWE_TD->mem_csfilt_fx[0], sf );
273 714 : move32();
274 :
275 714 : hBWE_TD->tbe_demph_fx = shl_r( hBWE_TD->tbe_demph_fx, sf );
276 714 : move16();
277 714 : hBWE_TD->tbe_premph_fx = shl_r( hBWE_TD->tbe_premph_fx, sf );
278 714 : move16();
279 714 : }
280 :
281 6909 : void find_max_mem_wb( Decoder_State *st_fx, Word16 *n_mem )
282 : {
283 : Word16 i;
284 6909 : Word16 max = 0;
285 6909 : move16();
286 6909 : Word32 Lmax = 0;
287 6909 : move32();
288 : Word16 n_mem_32;
289 : TD_BWE_DEC_HANDLE hBWE_TD;
290 6909 : hBWE_TD = st_fx->hBWE_TD;
291 :
292 6909 : IF( EQ_16( st_fx->element_mode, EVS_MONO ) )
293 : {
294 0 : FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
295 : {
296 0 : max = s_max( max, abs_s( hBWE_TD->old_bwe_exc_extended_fx[i] ) );
297 : }
298 : }
299 55272 : FOR( i = 0; i < 7; i++ )
300 : {
301 :
302 48363 : if ( GT_16( abs_s( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] ), max ) )
303 18160 : max = abs_s( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] );
304 : }
305 :
306 55272 : FOR( i = 0; i < 7; i++ )
307 : {
308 48363 : if ( GT_16( abs_s( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx[i] ), max ) )
309 7881 : max = abs_s( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx[i] );
310 : }
311 :
312 55272 : FOR( i = 0; i < 7; i++ )
313 : {
314 :
315 48363 : if ( GT_16( abs_s( hBWE_TD->mem_genSHBexc_filt_down_wb3_fx[i] ), max ) )
316 2267 : max = abs_s( hBWE_TD->mem_genSHBexc_filt_down_wb3_fx[i] );
317 : }
318 :
319 75999 : FOR( i = 0; i < 10; i++ )
320 : {
321 :
322 69090 : if ( GT_16( abs_s( hBWE_TD->state_lpc_syn_fx[i] ), max ) )
323 2244 : max = abs_s( hBWE_TD->state_lpc_syn_fx[i] );
324 : }
325 :
326 41454 : FOR( i = 0; i < 5; i++ )
327 : {
328 :
329 34545 : if ( GT_16( abs_s( hBWE_TD->state_syn_shbexc_fx[i] ), max ) )
330 445 : max = abs_s( hBWE_TD->state_syn_shbexc_fx[i] );
331 : }
332 :
333 6909 : IF( max == 0 )
334 : {
335 161 : *n_mem = 15;
336 161 : move16();
337 : }
338 : ELSE
339 : {
340 6748 : *n_mem = norm_s( max );
341 6748 : move16();
342 : }
343 :
344 :
345 20727 : FOR( i = 0; i < 2; i++ )
346 : {
347 :
348 13818 : if ( GT_32( L_abs( hBWE_TD->mem_csfilt_fx[i] ), Lmax ) )
349 6580 : Lmax = L_abs( hBWE_TD->mem_csfilt_fx[i] );
350 : }
351 :
352 6909 : IF( Lmax == 0 )
353 : {
354 329 : n_mem_32 = 31;
355 329 : move16();
356 : }
357 : ELSE
358 : {
359 6580 : n_mem_32 = norm_l( Lmax );
360 : }
361 :
362 6909 : *n_mem = sub( s_min( *n_mem, n_mem_32 ), 1 );
363 6909 : move16();
364 6909 : *n_mem = s_max( *n_mem, 0 );
365 6909 : move16();
366 6909 : }
367 :
368 6909 : void rescale_genWB_mem( Decoder_State *st_fx, Word16 sf )
369 : {
370 : Word16 i;
371 : TD_BWE_DEC_HANDLE hBWE_TD;
372 6909 : hBWE_TD = st_fx->hBWE_TD;
373 :
374 6909 : IF( EQ_16( st_fx->element_mode, EVS_MONO ) )
375 : {
376 0 : FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
377 : {
378 0 : hBWE_TD->old_bwe_exc_extended_fx[i] = shl( hBWE_TD->old_bwe_exc_extended_fx[i], sf );
379 0 : move16();
380 : }
381 : }
382 :
383 75999 : FOR( i = 0; i < 10; i++ )
384 : {
385 69090 : hBWE_TD->state_lpc_syn_fx[i] = shl( hBWE_TD->state_lpc_syn_fx[i], sf );
386 69090 : move16();
387 : }
388 :
389 41454 : FOR( i = 0; i < 5; i++ )
390 : {
391 34545 : hBWE_TD->state_syn_shbexc_fx[i] = shl( hBWE_TD->state_syn_shbexc_fx[i], sf );
392 34545 : move16();
393 : }
394 :
395 55272 : FOR( i = 0; i < 7; i++ )
396 : {
397 48363 : hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i], sf );
398 48363 : move16();
399 : }
400 :
401 55272 : FOR( i = 0; i < 7; i++ )
402 : {
403 48363 : hBWE_TD->mem_genSHBexc_filt_down_wb2_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx[i], sf );
404 48363 : move16();
405 : }
406 :
407 55272 : FOR( i = 0; i < 7; i++ )
408 : {
409 48363 : hBWE_TD->mem_genSHBexc_filt_down_wb3_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_wb3_fx[i], sf );
410 48363 : move16();
411 : }
412 :
413 20727 : FOR( i = 0; i < 2; i++ )
414 : {
415 13818 : hBWE_TD->mem_csfilt_fx[i] = L_shl( hBWE_TD->mem_csfilt_fx[i], sf );
416 13818 : move32();
417 : }
418 :
419 6909 : return;
420 : }
421 :
422 :
423 3263 : static void InitSWBdecBuffer_fx(
424 : TD_BWE_DEC_HANDLE hBWE_TD /* TD BWE data handle */ )
425 : {
426 3263 : set16_fx( hBWE_TD->old_bwe_exc_fx, 0, ( PIT16k_MAX * 2 ) );
427 3263 : hBWE_TD->bwe_seed[0] = 23;
428 3263 : move16();
429 3263 : hBWE_TD->bwe_seed[1] = 59;
430 3263 : move16();
431 :
432 3263 : set16_fx( hBWE_TD->old_bwe_exc_extended_fx, 0, NL_BUFF_OFFSET );
433 3263 : hBWE_TD->q_old_bwe_exc_extended_fx = Q15;
434 3263 : move16();
435 3263 : hBWE_TD->bwe_non_lin_prev_scale_fx = 0;
436 3263 : move16();
437 :
438 3263 : set32_fx( hBWE_TD->genSHBsynth_Hilbert_Mem_fx, 0, HILBERT_MEM_SIZE );
439 3263 : set16_fx( hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx, 0, 2 * ALLPASSSECTIONS_STEEP ); /* Interp all pass memory */
440 3263 : set32_fx( hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx_32, 0, 2 * ALLPASSSECTIONS_STEEP ); /* Interp all pass memory */
441 :
442 3263 : hBWE_TD->syn_dm_phase = 0;
443 3263 : move16();
444 3263 : hBWE_TD->prev_fbbwe_ratio_fx = 32767 /*1.0f Q15*/;
445 3263 : move16();
446 :
447 : /* these are fd-bwe constants */
448 3263 : hBWE_TD->prev_wb_bwe_frame_pow_fx = 4194l /*0.001f Q22*/; /* Q22 */
449 3263 : move32();
450 3263 : hBWE_TD->prev_swb_bwe_frame_pow_fx = 4194l /*0.001f Q22*/; /* Q22 */
451 3263 : move32();
452 3263 : hBWE_TD->prev_ener_fx_Q = 31;
453 3263 : move16();
454 3263 : hBWE_TD->prev_Qx = 0;
455 3263 : move16();
456 3263 : hBWE_TD->prev_frame_pow_exp = 0;
457 3263 : move16();
458 3263 : hBWE_TD->prev_Q_bwe_syn = 0;
459 3263 : move16();
460 3263 : hBWE_TD->prev_Q_bwe_syn2 = 0;
461 3263 : move16();
462 3263 : hBWE_TD->prev_hb_synth_fx_exp = 31;
463 3263 : move16();
464 :
465 3263 : return;
466 : }
467 :
468 :
469 5051 : void ResetSHBbuffer_Dec_fx(
470 : TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle */
471 : const Word16 extl /* i : BWE extension layer */
472 : )
473 : {
474 : Word16 i;
475 : Word16 f;
476 : Word16 inc;
477 :
478 5051 : IF( NE_16( extl, WB_TBE ) )
479 : {
480 5051 : f = 1489;
481 5051 : move16(); /* Q15 */
482 5051 : inc = 1489;
483 5051 : move16(); /* Q15 */
484 : }
485 : ELSE
486 : {
487 0 : f = 5461;
488 0 : move16(); /* Q15 */
489 0 : inc = 5461;
490 0 : move16(); /* Q15 */
491 : }
492 :
493 : /* states for the filters used in generating SHB excitation from WB excitation*/
494 5051 : set32_fx( hBWE_TD->mem_csfilt_fx, 0, 2 );
495 :
496 : /* states for the filters used in generating SHB signal from SHB excitation*/
497 5051 : set16_fx( hBWE_TD->state_syn_shbexc_fx, 0, L_SHB_LAHEAD );
498 5051 : set16_fx( hBWE_TD->state_lpc_syn_fx, 0, LPC_SHB_ORDER );
499 :
500 5051 : IF( EQ_16( extl, FB_TBE ) )
501 : {
502 0 : set16_fx( hBWE_TD->fb_state_lpc_syn_fx, 0, LPC_SHB_ORDER );
503 0 : hBWE_TD->fb_tbe_demph_fx = 0;
504 0 : move16();
505 0 : fb_tbe_reset_synth_fx( hBWE_TD->fbbwe_hpf_mem_fx, hBWE_TD->fbbwe_hpf_mem_fx_Q, &hBWE_TD->prev_fbbwe_ratio_fx );
506 : }
507 : /* states for the filters used in generating SHB signal from SHB excitation in wideband*/
508 5051 : set16_fx( hBWE_TD->mem_genSHBexc_filt_down_shb_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
509 5051 : set16_fx( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
510 5051 : set16_fx( hBWE_TD->mem_genSHBexc_filt_down_wb3_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
511 5051 : set16_fx( hBWE_TD->state_lsyn_filt_shb_fx, 0, 2 * ALLPASSSECTIONS_STEEP );
512 5051 : set32_fx( hBWE_TD->state_lsyn_filt_shb_fx_32, 0, 2 * ALLPASSSECTIONS_STEEP );
513 5051 : set16_fx( hBWE_TD->state_lsyn_filt_dwn_shb_fx, 0, 2 * ALLPASSSECTIONS_STEEP );
514 5051 : set32_fx( hBWE_TD->state_lsyn_filt_dwn_shb_fx_32, 0, 2 * ALLPASSSECTIONS_STEEP );
515 5051 : set16_fx( hBWE_TD->state_32and48k_WB_upsample_fx, 0, 2 * ALLPASSSECTIONS_STEEP );
516 :
517 : /* States for the local synthesis filters */
518 5051 : set16_fx( hBWE_TD->syn_overlap_fx, 0, L_SHB_LAHEAD );
519 5051 : set32_fx( hBWE_TD->syn_overlap_fx_32, 0, L_SHB_LAHEAD );
520 :
521 : /* States for FEC */
522 :
523 5051 : IF( NE_16( extl, WB_TBE ) )
524 : {
525 55561 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
526 : {
527 50510 : hBWE_TD->lsp_prevfrm_fx[i] = f;
528 50510 : move16(); /*Q15*/
529 50510 : f = add( f, inc );
530 50510 : move16();
531 : }
532 : }
533 : ELSE
534 : {
535 0 : FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
536 : {
537 0 : hBWE_TD->lsp_prevfrm_fx[i] = f;
538 0 : move16(); /*Q15*/
539 0 : f = add( f, inc );
540 0 : move16();
541 : }
542 0 : FOR( ; i < LPC_SHB_ORDER; i++ )
543 : {
544 0 : hBWE_TD->lsp_prevfrm_fx[i] = 0;
545 0 : move16();
546 : }
547 : }
548 5051 : hBWE_TD->GainFrame_prevfrm_fx = 0;
549 5051 : move16(); /*Q18*/
550 5051 : hBWE_TD->GainAttn_fx = 32767;
551 5051 : move16(); /*Q15*/
552 5051 : hBWE_TD->tbe_demph_fx = 0;
553 5051 : move16();
554 5051 : hBWE_TD->tbe_premph_fx = 0;
555 5051 : move16();
556 5051 : set16_fx( hBWE_TD->mem_stp_swb_fx, 0, LPC_SHB_ORDER );
557 5051 : hBWE_TD->gain_prec_swb_fx = 16384; /*Q14 =1*/
558 5051 : move16();
559 5051 : set16_fx( hBWE_TD->GainShape_Delay_fx, 0, NUM_SHB_SUBFR / 2 );
560 5051 : hBWE_TD->prev_pow_exc16kWhtnd_fx32 = 1; /* Q0 1.f */
561 5051 : move32();
562 5051 : hBWE_TD->prev_mix_factor_fx = 32767; /*Q15 1.f*/
563 5051 : move16();
564 :
565 5051 : set16_fx( hBWE_TD->old_core_synth_fx, 0, L_FRAME16k );
566 5051 : set16_fx( hBWE_TD->old_tbe_synth_fx, 0, L_SHB_TRANSITION_LENGTH );
567 5051 : set32_fx( hBWE_TD->old_tbe_synth_fx_32, 0, L_SHB_TRANSITION_LENGTH );
568 5051 : hBWE_TD->tilt_swb_fec_fx = 0;
569 5051 : move16();
570 :
571 5051 : return;
572 : }
573 :
574 :
575 : /*==========================================================================*/
576 : /* FUNCTION : void wb_tbe_dec_fx () */
577 : /*--------------------------------------------------------------------------*/
578 : /* PURPOSE : WB TBE decoder, 6 - 8 kHz band decoding module */
579 : /*--------------------------------------------------------------------------*/
580 : /* INPUT ARGUMENTS : */
581 : /* _Word16 coder_type i : coding type */
582 : /* _Word32 *bwe_exc_extended i : bandwidth extended exciatation 2*Q_exc*/
583 : /* _Word16 Q_exc i : Q format */
584 : /* _Word16 voice_factors[] i : voicing factors Q15 */
585 : /*--------------------------------------------------------------------------*/
586 : /* OUTPUT ARGUMENTS : */
587 : /* _Word16 *synth o : WB synthesis/final synthesis Q_synth */
588 : /*--------------------------------------------------------------------------*/
589 : /* INPUT/OUTPUT ARGUMENTS : */
590 : /* Decoder_State *st_fx, i/o: decoder state structure */
591 : /*--------------------------------------------------------------------------*/
592 : /* RETURN ARGUMENTS : */
593 : /* _ None */
594 : /*--------------------------------------------------------------------------*/
595 : /* CALLED FROM : */
596 : /*==========================================================================*/
597 6989 : void ivas_wb_tbe_dec_fx(
598 : Decoder_State *st_fx, /* i/o: decoder state structure */
599 : const Word16 coder_type, /* i : coding type Q0 */
600 : Word32 *bwe_exc_extended, /* i : bandwidth extended exciatation 2*Q_exc*/
601 : const Word16 Q_exc,
602 : const Word16 voice_factors[], /* i : voicing factors Q15 */
603 : Word16 *synth, /* o : WB synthesis/final synthesis Q_synth */
604 : Word16 *Q_synth )
605 : {
606 : Word16 i;
607 : Word16 shaped_wb_excitation[( L_FRAME16k + L_SHB_LAHEAD ) / 4];
608 : Word16 shaped_wb_excitation_frac[L_FRAME16k / 4];
609 : Word16 bwe_exc_extended_16[L_FRAME32k + 40];
610 : Word16 exc4kWhtnd[L_FRAME16k / 4];
611 : Word16 lsf_wb[LPC_SHB_ORDER_WB], lpc_wb[LPC_SHB_ORDER_WB + 1], GainShape[NUM_SHB_SUBFR];
612 : Word32 GainFrame;
613 : Word16 error[L_FRAME16k];
614 : Word16 synth_frac[L_FRAME16k];
615 : Word16 upsampled_synth[L_FRAME48k];
616 : Word32 tmp_synL[L_FRAME48k], upsampled_synth_32fx[L_FRAME48k];
617 : Word32 prev_pow, curr_pow, curr_frame_pow;
618 : Word16 curr_frame_pow_exp;
619 : Word16 temp, scale, n;
620 : Word16 j;
621 :
622 : Word16 Q_bwe_exc, Q_bwe_exc_ext, Qx;
623 : Word16 n_mem, cnt;
624 6989 : Word16 max = 0;
625 6989 : move16();
626 6989 : Word32 L_tmp, Lacc, Lscale, Lmax = 0;
627 6989 : move32();
628 : Word16 tmp, exp, sc;
629 : Word16 vf_modified[NB_SUBFR16k];
630 6989 : Word16 uv_flag = 0;
631 6989 : move16();
632 6989 : Word16 dummy = 0;
633 6989 : move16();
634 : Word32 dummy2[HILBERT_MEM_SIZE];
635 : Word16 f, inc;
636 : Word64 W_tmp;
637 : #ifndef ISSUE_1866_replace_overflow_libdec
638 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
639 : Flag Overflow = 0;
640 : move32();
641 : #endif
642 : #endif
643 : TD_BWE_DEC_HANDLE hBWE_TD;
644 6989 : hBWE_TD = st_fx->hBWE_TD;
645 6989 : set32_fx( &dummy2[0], 0, HILBERT_MEM_SIZE );
646 6989 : IF( st_fx->bws_cnt == 0 )
647 : {
648 : /* Initialization */
649 6909 : set16_fx( GainShape, 11469 /*0.35f Q15*/, NUM_SHB_SUBFR );
650 6909 : GainFrame = 1;
651 6909 : move32();
652 :
653 6909 : IF( !st_fx->bfi )
654 : {
655 6876 : IF( EQ_16( st_fx->use_partial_copy, 1 ) )
656 : {
657 0 : IF( NE_16( st_fx->last_extl, WB_TBE ) )
658 : {
659 0 : hBWE_TD->GainFrame_prevfrm_fx = 0;
660 0 : move32();
661 0 : hBWE_TD->lsp_prevfrm_fx[0] = 3277 /*0.1f Q15*/;
662 0 : move16();
663 0 : FOR( i = 1; i < LPC_SHB_ORDER_LBR_WB; i++ )
664 : {
665 0 : hBWE_TD->lsp_prevfrm_fx[i] = add( hBWE_TD->lsp_prevfrm_fx[i - i], 3277 /*0.1f Q15*/ );
666 0 : move16();
667 : }
668 : }
669 0 : Copy( hBWE_TD->lsp_prevfrm_fx, lsf_wb, LPC_SHB_ORDER_LBR_WB );
670 0 : set16_fx( GainShape, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR / 2 );
671 :
672 0 : IF( EQ_16( st_fx->rf_frame_type, RF_NELP ) )
673 : {
674 : /* Frame gain */
675 0 : st_fx->rf_indx_tbeGainFr = s_and( st_fx->rf_indx_tbeGainFr, 0xF ); /* only four LSBs are valid */
676 0 : move16();
677 0 : Copy32( SHBCB_FrameGain16_fx + st_fx->rf_indx_tbeGainFr, &GainFrame, 1 );
678 0 : test();
679 0 : test();
680 0 : test();
681 0 : test();
682 0 : test();
683 0 : IF( ( st_fx->core == ACELP_CORE ) && ( st_fx->last_core == ACELP_CORE ) && !st_fx->prev_use_partial_copy && EQ_16( st_fx->prev_coder_type, UNVOICED ) && NE_32( GainFrame, hBWE_TD->GainFrame_prevfrm_fx ) && EQ_16( st_fx->last_extl, WB_TBE ) )
684 : {
685 : /*GainFrame = 0.2f*GainFrame + 0.8f*st_fx->GainFrame_prevfrm_fx;*/
686 0 : GainFrame = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx /*Q18*/, 26214 /*0.8f in Q15*/ ), Mult_32_16( GainFrame /*Q18*/, 6553 /*0.2f in Q15*/ ) /*Q18+Q15 - 15*/ ); /*Q18*/
687 : }
688 : }
689 : ELSE
690 : {
691 0 : temp = 0;
692 0 : move16();
693 : /* Frame gain */
694 0 : SWITCH( st_fx->rf_indx_tbeGainFr )
695 : {
696 0 : case 0:
697 0 : GainFrame = 131072; /* 0.5f in Q18 */
698 0 : move32();
699 0 : if ( LE_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) )
700 : {
701 0 : temp = 26214 /*0.8 Q15*/;
702 0 : move16();
703 : }
704 0 : BREAK;
705 0 : case 1:
706 0 : GainFrame = 524288; /* 2.0f in Q18 */
707 0 : move32();
708 0 : test();
709 0 : if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) )
710 : {
711 0 : temp = 26214 /*0.8 Q15*/;
712 0 : move16();
713 : }
714 0 : BREAK;
715 0 : case 2:
716 0 : GainFrame = 1048576; /* 4.0f in Q18 */
717 0 : move32();
718 0 : test();
719 0 : if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) )
720 : {
721 0 : temp = 26214 /*0.8 Q15*/;
722 0 : move16();
723 : }
724 0 : BREAK;
725 0 : case 3:
726 0 : GainFrame = 2097152; /* 8.0f in Q18 */
727 0 : move32();
728 0 : test();
729 0 : if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 4194304l /*16 Q18*/ ) )
730 : {
731 0 : temp = 26214 /*0.8 Q15*/;
732 0 : move16();
733 : }
734 0 : BREAK;
735 0 : default:
736 0 : fprintf( stderr, "RF SWB-TBE gain bits not supported." );
737 : }
738 0 : IF( EQ_16( st_fx->last_extl, WB_TBE ) )
739 : {
740 :
741 0 : GainFrame = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, temp ), Mult_32_16( GainFrame, sub( 32767, temp ) ) );
742 : }
743 0 : test();
744 0 : IF( ( st_fx->core == ACELP_CORE ) && ( st_fx->last_core == ACELP_CORE ) )
745 : {
746 0 : test();
747 0 : test();
748 0 : test();
749 0 : test();
750 0 : IF( !st_fx->prev_use_partial_copy && EQ_16( st_fx->last_coder_type, VOICED ) && EQ_16( st_fx->rf_frame_type, RF_GENPRED ) && LT_16( st_fx->prev_tilt_code_dec_fx, 1497 ) && GT_16( st_fx->prev_tilt_code_dec_fx, 200 ) )
751 : {
752 0 : GainFrame = Mult_32_16( GainFrame, 9830 /*0.3f in Q15*/ ); /*Q18*/
753 : }
754 : }
755 : }
756 : }
757 : ELSE
758 : {
759 : /* de-quantization */
760 : Word16 ignore;
761 6876 : ivas_dequantizeSHBparams_fx_9_1( st_fx, st_fx->extl, st_fx->extl_brate, lsf_wb, GainShape, &GainFrame, &uv_flag, 0, 0, 0, &ignore );
762 : }
763 : }
764 : ELSE
765 : {
766 33 : IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
767 : {
768 2 : Copy( hBWE_TD->lsp_prevfrm_fx, lsf_wb, LPC_SHB_ORDER_LBR_WB );
769 : }
770 : ELSE
771 : {
772 31 : Copy( hBWE_TD->lsp_prevfrm_fx, lsf_wb, LPC_SHB_ORDER_WB );
773 : }
774 33 : set16_fx( GainShape, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR / 2 );
775 :
776 33 : hBWE_TD->GainAttn_fx = mult_r( hBWE_TD->GainAttn_fx, 27853 /*0.85f in Q15*/ ); /*Q15*/
777 33 : move16();
778 :
779 33 : IF( EQ_16( st_fx->codec_mode, MODE1 ) )
780 : {
781 33 : GainFrame = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx /*Q18*/, hBWE_TD->GainAttn_fx /*Q15*/ ); /*Q15+Q18-15*/
782 : }
783 : ELSE
784 : {
785 0 : GainFrame = hBWE_TD->GainFrame_prevfrm_fx; /*Q18*/
786 0 : move32();
787 : }
788 : }
789 :
790 6909 : IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
791 : {
792 : /* convert LSPs back into LP coeffs */
793 2440 : lsp2lpc_fx( lpc_wb + 1, lsf_wb, hBWE_TD->prev_lpc_wb_fx, LPC_SHB_ORDER_LBR_WB );
794 2440 : set16_fx( lpc_wb + LPC_SHB_ORDER_LBR_WB + 1, 0, ( LPC_SHB_ORDER_WB - LPC_SHB_ORDER_LBR_WB ) );
795 17080 : FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
796 : {
797 14640 : hBWE_TD->prev_lpc_wb_fx[i] = lpc_wb[i + 1];
798 14640 : move16();
799 : }
800 12200 : FOR( i = 1; i < LPC_SHB_ORDER_LBR_WB + 1; i++ )
801 : {
802 9760 : lpc_wb[i] = negate( lpc_wb[i] );
803 9760 : move16();
804 : }
805 2440 : lpc_wb[0] = 4096; /*1.0f in Q12*/
806 2440 : move16();
807 : }
808 : ELSE
809 : {
810 : /* convert LSPs back into LP coeffs */
811 4469 : lsp2lpc_fx( lpc_wb + 1, lsf_wb, hBWE_TD->prev_lpc_wb_fx, LPC_SHB_ORDER_WB );
812 31283 : FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
813 : {
814 26814 : hBWE_TD->prev_lpc_wb_fx[i] = lpc_wb[i + 1];
815 26814 : move16();
816 : }
817 31283 : FOR( i = 1; i < LPC_SHB_ORDER_WB + 1; i++ )
818 : {
819 26814 : lpc_wb[i] = negate( lpc_wb[i] );
820 26814 : move16();
821 : }
822 4469 : lpc_wb[0] = 4096; /*1.0f in Q12*/
823 4469 : move16();
824 : }
825 :
826 6909 : Copy( voice_factors, vf_modified, NB_SUBFR16k );
827 6909 : IF( EQ_16( coder_type, VOICED ) )
828 : {
829 4704 : FOR( i = 1; i < NB_SUBFR; i++ )
830 : {
831 3528 : vf_modified[i] = add( mult_r( 26214, voice_factors[i] ), mult_r( 6554, voice_factors[i - 1] ) ); /* Q15 */
832 3528 : move16();
833 : }
834 1176 : IF( NE_16( st_fx->L_frame, L_FRAME ) )
835 : {
836 0 : vf_modified[4] = add( mult_r( 26214, voice_factors[4] ), mult_r( 6554, voice_factors[3] ) ); /* Q15 */
837 0 : move16();
838 : }
839 : }
840 :
841 : /* From low band excitation, generate highband excitation */
842 6909 : Lmax = 0;
843 6909 : move32();
844 4511577 : FOR( cnt = 0; cnt < L_FRAME32k + NL_BUFF_OFFSET; cnt++ )
845 : {
846 4504668 : Lmax = L_max( Lmax, L_abs( bwe_exc_extended[cnt] ) );
847 : }
848 6909 : IF( Lmax == 0 )
849 : {
850 0 : Q_bwe_exc = 31;
851 0 : move16();
852 : }
853 : ELSE
854 : {
855 6909 : Q_bwe_exc = norm_l( Lmax );
856 : }
857 6909 : Q_bwe_exc = sub( Q_bwe_exc, 3 );
858 6909 : Q_bwe_exc = add( Q_bwe_exc, add( Q_exc, Q_exc ) );
859 :
860 6909 : find_max_mem_wb( st_fx, &n_mem );
861 :
862 6909 : if ( GT_16( sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc ), n_mem ) )
863 : {
864 65 : Q_bwe_exc = add( st_fx->prev_Q_bwe_exc, n_mem );
865 : }
866 :
867 6909 : test();
868 6909 : if ( uv_flag && GT_16( Q_bwe_exc, 20 ) )
869 : {
870 185 : Q_bwe_exc = 20;
871 185 : move16(); /* restrict this to 21 due to the Q factor requireemnt of the random number generator (keep 1 bit headroom) */
872 : }
873 :
874 6909 : W_tmp = 0;
875 6909 : move64();
876 6909 : IF( st_fx->element_mode > EVS_MONO )
877 : {
878 6909 : tmp = sub( shl( sub( st_fx->prev_Q_bwe_exc, 16 ), 1 ), 31 + 16 );
879 6909 : W_tmp = L_shl( 1407374848l /*0.00001f Q47*/, tmp ); /*Q(2*(st_fx->prev_Q_bwe_exc-16))*/
880 : }
881 41454 : FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
882 : {
883 34545 : W_tmp = W_mac_16_16( W_tmp, hBWE_TD->state_syn_shbexc_fx[i], hBWE_TD->state_syn_shbexc_fx[i] ); /*Q(2*(st_fx->prev_Q_bwe_exc-16 + 1))*/
884 : }
885 6909 : exp = W_norm( W_tmp );
886 6909 : prev_pow = W_extract_h( W_shl( W_tmp, exp ) );
887 6909 : exp = sub( add( add( shl( sub( st_fx->prev_Q_bwe_exc, 16 ), 1 ), 1 ), exp ), 32 );
888 :
889 6909 : rescale_genWB_mem( st_fx, sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc ) );
890 :
891 6909 : sc = sub( Q_bwe_exc, add( Q_exc, Q_exc ) );
892 4511577 : FOR( cnt = 0; cnt < L_FRAME32k + NL_BUFF_OFFSET; cnt++ )
893 : {
894 4504668 : bwe_exc_extended_16[cnt] = round_fx( L_shl( bwe_exc_extended[cnt], sc ) );
895 4504668 : move16();
896 : }
897 :
898 6909 : Copy( hBWE_TD->state_syn_shbexc_fx, shaped_wb_excitation, L_SHB_LAHEAD / 4 );
899 :
900 6909 : Q_bwe_exc_ext = sub( Q_bwe_exc, 16 );
901 :
902 6909 : GenShapedWBExcitation_ivas_fx( shaped_wb_excitation + L_SHB_LAHEAD / 4, lpc_wb, exc4kWhtnd, hBWE_TD->mem_csfilt_fx,
903 6909 : hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->mem_genSHBexc_filt_down_wb2_fx,
904 6909 : hBWE_TD->mem_genSHBexc_filt_down_wb3_fx, hBWE_TD->state_lpc_syn_fx, coder_type,
905 6909 : bwe_exc_extended_16, Q_bwe_exc_ext, hBWE_TD->bwe_seed, vf_modified, uv_flag, st_fx->igf );
906 :
907 6909 : curr_pow = 0;
908 6909 : move32();
909 6909 : IF( st_fx->element_mode > EVS_MONO )
910 : {
911 6909 : tmp = sub( shl( Q_bwe_exc_ext, 1 ), 31 + 16 );
912 6909 : curr_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /*Q(2*(Q_bwe_exc_ext))*/
913 : }
914 41454 : FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
915 : {
916 34545 : curr_pow = L_mac0_sat( curr_pow, shaped_wb_excitation[i + L_SHB_LAHEAD / 4], shaped_wb_excitation[i + L_SHB_LAHEAD / 4] ); /* Q(2*Q_bwe_exc_ext) */
917 : }
918 :
919 6909 : if ( GT_16( voice_factors[0], 24576 ) )
920 : {
921 243 : curr_pow = L_shr( curr_pow, 2 ); /* Q(2*Q_bwe_exc_ext) */
922 : }
923 :
924 6909 : Lscale = root_a_over_b_fx( curr_pow, shl_r( Q_bwe_exc_ext, 1 ), prev_pow,
925 : exp, &exp );
926 :
927 34545 : FOR( i = 0; i < L_SHB_LAHEAD / 4 - 1; i++ )
928 : {
929 27636 : L_tmp = Mult_32_16( Lscale, shaped_wb_excitation[i] ); /* Q(16-exp+Q_bwe_exc_ext) */
930 27636 : shaped_wb_excitation[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc_ext */
931 27636 : move16();
932 : }
933 6909 : Lscale = root_a_fx( Lscale, sub( 31, exp ), &exp );
934 6909 : L_tmp = Mult_32_16( Lscale, shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] ); /* Q(16-exp+Q_bwe_exc_ext) */
935 6909 : shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] = round_fx( L_shl( L_tmp, exp ) ); /* Q_bwe_exc_ext */
936 6909 : move16();
937 :
938 : /* Update SHB excitation */
939 6909 : Copy( shaped_wb_excitation + L_FRAME16k / 4, hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD / 4 );
940 :
941 :
942 : /* Adjust the subframe and frame gain of the synthesized shb signal */
943 : /* Scale the shaped excitation */
944 6909 : ScaleShapedWB_fx( SHB_OVERLAP_LEN / 2, shaped_wb_excitation, hBWE_TD->syn_overlap_fx, GainShape, GainFrame,
945 : window_wb_fx, subwin_wb_fx,
946 6909 : Q_bwe_exc_ext, st_fx->L_frame, 0, &dummy, dummy, dummy2 );
947 :
948 6909 : max = 0;
949 6909 : move16();
950 559629 : FOR( i = 0; i < L_FRAME16k / 4; i++ )
951 : {
952 552720 : max = s_max( max, abs_s( shaped_wb_excitation[i] ) );
953 : }
954 :
955 6909 : IF( max == 0 )
956 : {
957 192 : curr_frame_pow = 1;
958 192 : move16();
959 192 : n = 0;
960 192 : move16();
961 : }
962 : ELSE
963 : {
964 6717 : n = norm_s( max );
965 544077 : FOR( i = 0; i < L_FRAME16k / 4; i++ )
966 : {
967 : #ifdef ISSUE_1866_replace_overflow_libdec
968 537360 : shaped_wb_excitation_frac[i] = shl_sat( shaped_wb_excitation[i], n ); /*Q14*/
969 : #else
970 : shaped_wb_excitation_frac[i] = shl_o( shaped_wb_excitation[i], n, &Overflow ); /*Q14*/
971 : #endif
972 537360 : move16();
973 : }
974 6717 : n = sub( 14, n );
975 6717 : curr_frame_pow = 1;
976 6717 : move32();
977 544077 : FOR( i = 0; i < L_FRAME16k / 4; i++ )
978 : {
979 : #ifdef ISSUE_1866_replace_overflow_libdec
980 537360 : L_tmp = L_mult_sat( shaped_wb_excitation_frac[i], shaped_wb_excitation_frac[i] ); /*Q29*/
981 537360 : curr_frame_pow = L_add_sat( curr_frame_pow, L_shr( L_tmp, 7 ) ); /*Q22*/
982 : #else
983 : L_tmp = L_mult_o( shaped_wb_excitation_frac[i], shaped_wb_excitation_frac[i], &Overflow ); /*Q29*/
984 : curr_frame_pow = L_add_o( curr_frame_pow, L_shr( L_tmp, 7 ), &Overflow ); /*Q22*/
985 : #endif
986 : }
987 : }
988 6909 : curr_frame_pow_exp = add( n, n );
989 :
990 6909 : IF( GT_16( hBWE_TD->prev_frame_pow_exp, curr_frame_pow_exp ) )
991 : {
992 6098 : curr_frame_pow = L_shr( curr_frame_pow, sub( hBWE_TD->prev_frame_pow_exp, curr_frame_pow_exp ) );
993 6098 : curr_frame_pow_exp = hBWE_TD->prev_frame_pow_exp;
994 6098 : move16();
995 : }
996 : ELSE
997 : {
998 811 : hBWE_TD->prev_wb_bwe_frame_pow_fx = L_shr( hBWE_TD->prev_wb_bwe_frame_pow_fx, sub( curr_frame_pow_exp, hBWE_TD->prev_frame_pow_exp ) );
999 811 : move32();
1000 : }
1001 :
1002 6909 : test();
1003 6909 : test();
1004 6909 : IF( !st_fx->bfi && ( st_fx->prev_bfi || st_fx->prev_use_partial_copy ) )
1005 : {
1006 31 : IF( GT_32( L_shr( curr_frame_pow, 1 ), hBWE_TD->prev_wb_bwe_frame_pow_fx ) )
1007 : {
1008 27 : L_tmp = root_a_over_b_fx( hBWE_TD->prev_wb_bwe_frame_pow_fx, 22, curr_frame_pow, 22, &exp );
1009 27 : scale = round_fx( L_shl( L_tmp, exp ) ); /*Q15*/
1010 :
1011 27 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
1012 27 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
1013 27 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
1014 27 : temp = round_fx( L_shl( L_tmp, exp ) ); /* Q15 */
1015 : }
1016 : ELSE
1017 : {
1018 4 : scale = temp = 32767;
1019 4 : move16(); /* Q15 */
1020 4 : move16();
1021 : }
1022 :
1023 279 : FOR( j = 0; j < 8; j++ )
1024 : {
1025 248 : GainShape[2 * j] = mult_r( GainShape[2 * j], scale );
1026 248 : GainShape[add( 2 * j, 1 )] = mult_r( GainShape[add( 2 * j, 1 )], scale );
1027 2728 : FOR( i = 0; i < L_FRAME16k / ( 4 * 8 ); i++ )
1028 : {
1029 2480 : shaped_wb_excitation[add( i, j * ( L_FRAME16k / ( 4 * 8 ) ) )] = mult_r( shaped_wb_excitation[add( i, j * ( L_FRAME16k / ( 4 * 8 ) ) )], scale );
1030 2480 : move16();
1031 : }
1032 248 : IF( temp > 0 )
1033 : {
1034 248 : IF( LT_16( scale, temp ) )
1035 : {
1036 216 : scale = div_s( scale, temp );
1037 : }
1038 : ELSE
1039 : {
1040 32 : scale = 32767;
1041 32 : move16();
1042 : }
1043 : }
1044 : ELSE
1045 : {
1046 0 : scale = 0;
1047 0 : move16();
1048 : }
1049 : }
1050 : }
1051 :
1052 6909 : hBWE_TD->prev_wb_bwe_frame_pow_fx = curr_frame_pow;
1053 6909 : move32();
1054 6909 : hBWE_TD->prev_frame_pow_exp = curr_frame_pow_exp;
1055 6909 : move16();
1056 :
1057 : /* generate 16kHz SHB signal (6 - 8 kHz) from 2kHz signal */
1058 6909 : max = 0;
1059 6909 : move16();
1060 594174 : FOR( cnt = 0; cnt < ( L_FRAME16k + L_SHB_LAHEAD ) / 4; cnt++ )
1061 : {
1062 587265 : if ( GT_16( abs_s( shaped_wb_excitation[cnt] ), max ) )
1063 : {
1064 44711 : max = abs_s( shaped_wb_excitation[cnt] );
1065 : }
1066 : }
1067 6909 : Qx = norm_s( max );
1068 6909 : if ( max == 0 )
1069 : {
1070 0 : Qx = 15;
1071 0 : move16();
1072 : }
1073 :
1074 6909 : Qx = sub( Qx, 1 ); /* 1 bit space for saturation */
1075 :
1076 6909 : max = 0;
1077 6909 : move16();
1078 48363 : FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
1079 : {
1080 41454 : if ( GT_16( abs_s( hBWE_TD->state_lsyn_filt_shb_fx[i] ), max ) )
1081 14977 : max = abs_s( hBWE_TD->state_lsyn_filt_shb_fx[i] );
1082 : }
1083 :
1084 48363 : FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
1085 : {
1086 41454 : if ( GT_16( abs_s( hBWE_TD->state_lsyn_filt_dwn_shb_fx[i] ), max ) )
1087 4017 : max = abs_s( hBWE_TD->state_lsyn_filt_dwn_shb_fx[i] );
1088 : }
1089 :
1090 6909 : IF( EQ_32( st_fx->output_Fs, 32000 ) )
1091 : {
1092 3626 : FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
1093 : {
1094 3108 : max = s_max( max, abs_s( hBWE_TD->state_32and48k_WB_upsample_fx[i] ) );
1095 : }
1096 : }
1097 6909 : IF( EQ_32( st_fx->output_Fs, 48000 ) )
1098 : {
1099 46130 : FOR( i = 0; i < INTERP_3_1_MEM_LEN; i++ )
1100 : {
1101 42835 : max = s_max( max, abs_s( hBWE_TD->mem_resamp_HB_fx[i] ) );
1102 : }
1103 : }
1104 6909 : n_mem = 15;
1105 6909 : move16();
1106 6909 : if ( max != 0 )
1107 : {
1108 6467 : n_mem = norm_s( max );
1109 : }
1110 6909 : n_mem = s_max( n_mem, 0 );
1111 :
1112 6909 : if ( GT_16( sub( Qx, hBWE_TD->prev_Qx ), n_mem ) )
1113 : {
1114 1 : Qx = add( hBWE_TD->prev_Qx, n_mem );
1115 : }
1116 :
1117 594174 : FOR( i = 0; i < ( L_FRAME16k + L_SHB_LAHEAD ) / 4; i++ )
1118 : {
1119 587265 : shaped_wb_excitation[i] = shl( shaped_wb_excitation[i], Qx );
1120 587265 : move16();
1121 : }
1122 :
1123 48363 : FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
1124 : {
1125 41454 : hBWE_TD->state_lsyn_filt_shb_fx[i] = shl( hBWE_TD->state_lsyn_filt_shb_fx[i], sub( Qx, hBWE_TD->prev_Qx ) );
1126 41454 : move16();
1127 : }
1128 :
1129 48363 : FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
1130 : {
1131 41454 : hBWE_TD->state_lsyn_filt_dwn_shb_fx[i] = shl( hBWE_TD->state_lsyn_filt_dwn_shb_fx[i], sub( Qx, hBWE_TD->prev_Qx ) );
1132 41454 : move16();
1133 : }
1134 :
1135 6909 : GenWBSynth_fx( shaped_wb_excitation, error, hBWE_TD->state_lsyn_filt_shb_fx, hBWE_TD->state_lsyn_filt_dwn_shb_fx );
1136 :
1137 6909 : Copy( error + L_FRAME16k - L_SHB_TRANSITION_LENGTH, hBWE_TD->old_tbe_synth_fx, L_SHB_TRANSITION_LENGTH );
1138 :
1139 2217789 : FOR( i = 0; i < L_FRAME16k; i++ )
1140 : {
1141 2210880 : synth[i] = mult_r( error[i], 21299 /*0.65f in Q15*/ );
1142 2210880 : move16();
1143 : }
1144 :
1145 6909 : IF( st_fx->hBWE_FD != NULL )
1146 : {
1147 6909 : max = 0;
1148 6909 : move16();
1149 2217789 : FOR( cnt = 0; cnt < L_FRAME16k; cnt++ )
1150 : {
1151 2210880 : max = s_max( max, abs_s( synth[cnt] ) );
1152 : }
1153 :
1154 6909 : IF( max == 0 )
1155 : {
1156 159 : st_fx->hBWE_FD->last_wb_bwe_ener_fx = 0;
1157 159 : move16();
1158 : }
1159 : ELSE
1160 : {
1161 6750 : n = norm_s( max );
1162 2166750 : FOR( cnt = 0; cnt < L_FRAME16k; cnt++ )
1163 : {
1164 2160000 : synth_frac[cnt] = shl( synth[cnt], n ); /*Q14*/
1165 2160000 : move16();
1166 : }
1167 6750 : n = sub( sub( 14, n ), Qx );
1168 :
1169 6750 : Lacc = 0;
1170 6750 : move32();
1171 2166750 : FOR( i = 0; i < L_FRAME16k; i++ )
1172 : {
1173 2160000 : L_tmp = L_mult( synth_frac[i], synth_frac[i] ); /* Q29 */
1174 2160000 : Lacc = L_add( Lacc, L_shr( L_tmp, 7 ) ); /* Q22 */
1175 : }
1176 :
1177 6750 : L_tmp = Mult_32_16( Lacc, 102 ); /* Q22 */
1178 6750 : exp = norm_l( L_tmp );
1179 : #ifdef ISSUE_1866_replace_overflow_libdec
1180 6750 : tmp = round_fx_sat( L_shl( L_tmp, exp ) );
1181 : #else
1182 : tmp = round_fx_o( L_shl_o( L_tmp, exp, &Overflow ), &Overflow );
1183 : #endif
1184 6750 : exp = sub( add( exp, 22 ), 30 );
1185 6750 : tmp = div_s( 16384, tmp );
1186 6750 : L_tmp = Isqrt_lc( L_deposit_h( tmp ), &exp ); /* Q(31-exp) */
1187 6750 : st_fx->hBWE_FD->last_wb_bwe_ener_fx = round_fx_sat( L_shl_sat( L_tmp, add( exp, sub( n, 12 ) ) ) ); /* Q3 */
1188 6750 : move16();
1189 : }
1190 : }
1191 :
1192 6909 : IF( EQ_32( st_fx->output_Fs, 32000 ) ) /* 32kHz sampling rate, but only WB output - interpolate */
1193 : {
1194 518 : Scale_sig( hBWE_TD->state_32and48k_WB_upsample_fx, 2 * ALLPASSSECTIONS_STEEP, sub( Qx, hBWE_TD->prev_Qx ) );
1195 518 : Interpolate_allpass_steep_fx( synth, hBWE_TD->state_32and48k_WB_upsample_fx, L_FRAME16k, upsampled_synth );
1196 518 : Copy( upsampled_synth, synth, L_FRAME32k );
1197 : }
1198 6391 : ELSE IF( EQ_32( st_fx->output_Fs, 48000 ) )
1199 : {
1200 3295 : Copy_Scale_sig_16_32_no_sat( synth, tmp_synL, L_FRAME48k, sub( Q11, Qx ) );
1201 3295 : interpolate_3_over_1_allpass_fx32( tmp_synL, L_FRAME16k, upsampled_synth_32fx, hBWE_TD->mem_resamp_HB_fx_32 );
1202 3295 : Copy_Scale_sig_32_16( hBWE_TD->mem_resamp_HB_fx_32, hBWE_TD->mem_resamp_HB_fx, INTERP_3_1_MEM_LEN, sub( Qx, Q11 ) );
1203 3295 : Copy_Scale_sig_32_16( upsampled_synth_32fx, synth, L_FRAME48k, sub( Qx, Q11 ) );
1204 : }
1205 : }
1206 : ELSE
1207 : {
1208 80 : f = 5461;
1209 80 : move16(); /* Q15 */
1210 80 : inc = 5461;
1211 80 : move16(); /* Q15 */
1212 560 : FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
1213 : {
1214 480 : lsf_wb[i] = f;
1215 480 : move16(); /*Q15*/
1216 480 : f = add_sat( f, inc );
1217 480 : move16();
1218 : }
1219 80 : GainFrame = 0; /* Q18 */
1220 80 : move32();
1221 80 : Qx = 0;
1222 80 : move16();
1223 80 : Q_bwe_exc = 31;
1224 80 : move16();
1225 80 : hBWE_TD->prev_wb_bwe_frame_pow_fx = 4194l /*0.001f Q22*/; /* Q22 */
1226 80 : move32();
1227 80 : hBWE_TD->prev_frame_pow_exp = 0;
1228 80 : move16();
1229 : }
1230 :
1231 : /* Update previous frame parameters for FEC */
1232 6989 : IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
1233 : {
1234 2448 : Copy( lsf_wb, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER_LBR_WB );
1235 : }
1236 : ELSE
1237 : {
1238 4541 : Copy( lsf_wb, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER_WB );
1239 : }
1240 6989 : hBWE_TD->GainFrame_prevfrm_fx = GainFrame; /* Q18 */
1241 6989 : move32();
1242 :
1243 6989 : if ( !st_fx->bfi )
1244 : {
1245 6956 : hBWE_TD->GainAttn_fx = 32767;
1246 6956 : move16();
1247 : }
1248 :
1249 6989 : *Q_synth = Qx;
1250 6989 : move16();
1251 :
1252 6989 : st_fx->prev_Q_bwe_exc = Q_bwe_exc;
1253 6989 : move16();
1254 6989 : hBWE_TD->prev_Qx = Qx;
1255 6989 : move16();
1256 :
1257 6989 : return;
1258 : }
1259 :
1260 :
1261 0 : void wb_tbe_dec_fx(
1262 : Decoder_State *st_fx, /* i/o: decoder state structure */
1263 : const Word16 coder_type, /* i : coding type */
1264 : Word32 *bwe_exc_extended, /* i : bandwidth extended exciatation 2*Q_exc*/
1265 : const Word16 Q_exc,
1266 : const Word16 voice_factors[], /* i : voicing factors */
1267 : Word16 *synth, /* o : WB synthesis/final synthesis */
1268 : Word16 *Q_synth )
1269 : {
1270 : Word16 i;
1271 : Word16 shaped_wb_excitation[( L_FRAME16k + L_SHB_LAHEAD ) / 4];
1272 : Word16 shaped_wb_excitation_frac[L_FRAME16k / 4];
1273 : Word16 bwe_exc_extended_16[L_FRAME32k + 40];
1274 : Word16 exc4kWhtnd[L_FRAME16k / 4];
1275 : Word16 lsf_wb[LPC_SHB_ORDER_WB], lpc_wb[LPC_SHB_ORDER_WB + 1], GainShape[NUM_SHB_SUBFR];
1276 : Word32 GainFrame;
1277 : Word16 error[L_FRAME16k];
1278 : Word16 synth_frac[L_FRAME16k];
1279 : Word16 upsampled_synth[L_FRAME48k];
1280 : Word32 prev_pow, curr_pow, curr_frame_pow;
1281 : Word16 curr_frame_pow_exp;
1282 : Word16 temp, scale, n;
1283 : Word16 j;
1284 :
1285 : Word16 Q_bwe_exc, Q_bwe_exc_ext, Qx;
1286 : Word16 n_mem, cnt;
1287 0 : Word16 max = 0;
1288 0 : move16();
1289 0 : Word32 L_tmp, Lacc, Lscale, Lmax = 0;
1290 0 : move32();
1291 : Word16 tmp, exp, sc;
1292 : Word16 vf_modified[NB_SUBFR16k];
1293 0 : Word16 uv_flag = 0;
1294 0 : move16();
1295 0 : Word16 dummy = 0;
1296 0 : move16();
1297 : Word32 dummy2[HILBERT_MEM_SIZE];
1298 : Word16 f, inc;
1299 : #ifndef ISSUE_1866_replace_overflow_libdec
1300 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
1301 : Flag Overflow = 0;
1302 : move32();
1303 : #endif
1304 : #endif
1305 : TD_BWE_DEC_HANDLE hBWE_TD;
1306 0 : hBWE_TD = st_fx->hBWE_TD;
1307 0 : set32_fx( &dummy2[0], 0, HILBERT_MEM_SIZE );
1308 0 : IF( st_fx->bws_cnt == 0 )
1309 : {
1310 : /* Initialization */
1311 0 : set16_fx( GainShape, 11469 /*0.35f Q15*/, NUM_SHB_SUBFR );
1312 0 : GainFrame = 1;
1313 0 : move32();
1314 :
1315 0 : IF( !st_fx->bfi )
1316 : {
1317 0 : IF( EQ_16( st_fx->use_partial_copy, 1 ) )
1318 : {
1319 0 : IF( NE_16( st_fx->last_extl, WB_TBE ) )
1320 : {
1321 0 : hBWE_TD->GainFrame_prevfrm_fx = 0;
1322 0 : move32();
1323 0 : hBWE_TD->lsp_prevfrm_fx[0] = 3277 /*0.1f Q15*/;
1324 0 : move16();
1325 0 : FOR( i = 1; i < LPC_SHB_ORDER_LBR_WB; i++ )
1326 : {
1327 0 : hBWE_TD->lsp_prevfrm_fx[i] = add( hBWE_TD->lsp_prevfrm_fx[i - i], 3277 /*0.1f Q15*/ ); /*Q15*/
1328 0 : move16();
1329 : }
1330 : }
1331 0 : Copy( hBWE_TD->lsp_prevfrm_fx, lsf_wb, LPC_SHB_ORDER_LBR_WB ); /*Q15*/
1332 0 : set16_fx( GainShape, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR / 2 );
1333 :
1334 0 : IF( EQ_16( st_fx->rf_frame_type, RF_NELP ) )
1335 : {
1336 : /* Frame gain */
1337 0 : st_fx->rf_indx_tbeGainFr = s_and( st_fx->rf_indx_tbeGainFr, 0xF ); /* only four LSBs are valid */
1338 0 : move16();
1339 0 : Copy32( SHBCB_FrameGain16_fx + st_fx->rf_indx_tbeGainFr, &GainFrame, 1 );
1340 0 : test();
1341 0 : test();
1342 0 : test();
1343 0 : test();
1344 0 : test();
1345 0 : IF( ( st_fx->core == ACELP_CORE ) && ( st_fx->last_core == ACELP_CORE ) && !st_fx->prev_use_partial_copy && EQ_16( st_fx->prev_coder_type, UNVOICED ) && NE_32( GainFrame, hBWE_TD->GainFrame_prevfrm_fx ) && EQ_16( st_fx->last_extl, WB_TBE ) )
1346 : {
1347 : /*GainFrame = 0.2f*GainFrame + 0.8f*st_fx->GainFrame_prevfrm_fx;*/
1348 0 : GainFrame = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 26214 ), Mult_32_16( GainFrame, 6553 ) ); /*Q18*/
1349 : }
1350 : }
1351 : ELSE
1352 : {
1353 0 : temp = 0;
1354 0 : move16();
1355 : /* Frame gain */
1356 0 : SWITCH( st_fx->rf_indx_tbeGainFr )
1357 : {
1358 0 : case 0:
1359 0 : GainFrame = 131072; /* 0.5f in Q18 */
1360 0 : move32();
1361 0 : if ( LE_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) )
1362 : {
1363 0 : temp = 26214 /*0.8 Q15*/;
1364 0 : move16();
1365 : }
1366 0 : BREAK;
1367 0 : case 1:
1368 0 : GainFrame = 524288; /* 2.0f in Q18 */
1369 0 : move32();
1370 0 : test();
1371 0 : if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) )
1372 : {
1373 0 : temp = 26214 /*0.8 Q15*/;
1374 0 : move16();
1375 : }
1376 0 : BREAK;
1377 0 : case 2:
1378 0 : GainFrame = 1048576; /* 4.0f in Q18 */
1379 0 : move32();
1380 0 : test();
1381 0 : if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) )
1382 : {
1383 0 : temp = 26214 /*0.8 Q15*/;
1384 0 : move16();
1385 : }
1386 0 : BREAK;
1387 0 : case 3:
1388 0 : GainFrame = 2097152; /* 8.0f in Q18 */
1389 0 : move32();
1390 0 : test();
1391 0 : if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 4194304l /*16 Q18*/ ) )
1392 : {
1393 0 : temp = 26214 /*0.8 Q15*/;
1394 0 : move16();
1395 : }
1396 0 : BREAK;
1397 0 : default:
1398 0 : fprintf( stderr, "RF SWB-TBE gain bits not supported." );
1399 : }
1400 0 : IF( EQ_16( st_fx->last_extl, WB_TBE ) )
1401 : {
1402 :
1403 0 : GainFrame = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, temp ), Mult_32_16( GainFrame, sub( 32767, temp ) ) );
1404 : }
1405 0 : test();
1406 0 : IF( ( st_fx->core == ACELP_CORE ) && ( st_fx->last_core == ACELP_CORE ) )
1407 : {
1408 0 : test();
1409 0 : test();
1410 0 : test();
1411 0 : test();
1412 0 : IF( !st_fx->prev_use_partial_copy && EQ_16( st_fx->last_coder_type, VOICED ) && EQ_16( st_fx->rf_frame_type, RF_GENPRED ) && LT_16( st_fx->prev_tilt_code_dec_fx, 1497 ) && GT_16( st_fx->prev_tilt_code_dec_fx, 200 ) )
1413 : {
1414 0 : GainFrame = Mult_32_16( GainFrame, 9830 ); /*Q18*/
1415 : }
1416 : }
1417 : }
1418 : }
1419 : ELSE
1420 : {
1421 : /* de-quantization */
1422 0 : dequantizeSHBparams_fx_9_1( st_fx, st_fx->extl, st_fx->extl_brate, lsf_wb, GainShape, &GainFrame, &uv_flag, 0, 0, 0 );
1423 : }
1424 : }
1425 : ELSE
1426 : {
1427 0 : IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
1428 : {
1429 0 : Copy( hBWE_TD->lsp_prevfrm_fx, lsf_wb, LPC_SHB_ORDER_LBR_WB ); /*Q15*/
1430 : }
1431 : ELSE
1432 : {
1433 0 : Copy( hBWE_TD->lsp_prevfrm_fx, lsf_wb, LPC_SHB_ORDER_WB ); /*Q15*/
1434 : }
1435 0 : set16_fx( GainShape, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR / 2 );
1436 :
1437 0 : hBWE_TD->GainAttn_fx = mult_r( hBWE_TD->GainAttn_fx, 27853 );
1438 0 : move16();
1439 :
1440 0 : IF( EQ_16( st_fx->codec_mode, MODE1 ) )
1441 : {
1442 0 : GainFrame = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, hBWE_TD->GainAttn_fx ); /*Q18*/
1443 : }
1444 : ELSE
1445 : {
1446 0 : GainFrame = hBWE_TD->GainFrame_prevfrm_fx; /*Q18*/
1447 0 : move32();
1448 : }
1449 : }
1450 :
1451 0 : IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
1452 : {
1453 : /* convert LSPs back into LP coeffs */
1454 0 : lsp2lpc_fx( lpc_wb + 1, lsf_wb, hBWE_TD->prev_lpc_wb_fx, LPC_SHB_ORDER_LBR_WB );
1455 0 : set16_fx( lpc_wb + LPC_SHB_ORDER_LBR_WB + 1, 0, LPC_SHB_ORDER_WB - LPC_SHB_ORDER_LBR_WB );
1456 0 : FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
1457 : {
1458 0 : hBWE_TD->prev_lpc_wb_fx[i] = lpc_wb[i + 1];
1459 0 : move16();
1460 : }
1461 0 : FOR( i = 1; i < LPC_SHB_ORDER_LBR_WB + 1; i++ )
1462 : {
1463 0 : lpc_wb[i] = negate( lpc_wb[i] );
1464 0 : move16();
1465 : }
1466 0 : lpc_wb[0] = 4096;
1467 0 : move16();
1468 : }
1469 : ELSE
1470 : {
1471 : /* convert LSPs back into LP coeffs */
1472 0 : lsp2lpc_fx( lpc_wb + 1, lsf_wb, hBWE_TD->prev_lpc_wb_fx, LPC_SHB_ORDER_WB );
1473 0 : FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
1474 : {
1475 0 : hBWE_TD->prev_lpc_wb_fx[i] = lpc_wb[i + 1];
1476 0 : move16();
1477 : }
1478 0 : FOR( i = 1; i < LPC_SHB_ORDER_WB + 1; i++ )
1479 : {
1480 0 : lpc_wb[i] = negate( lpc_wb[i] );
1481 0 : move16();
1482 : }
1483 0 : lpc_wb[0] = 4096;
1484 0 : move16();
1485 : }
1486 :
1487 0 : Copy( voice_factors, vf_modified, NB_SUBFR16k );
1488 0 : IF( EQ_16( coder_type, VOICED ) )
1489 : {
1490 0 : FOR( i = 1; i < NB_SUBFR; i++ )
1491 : {
1492 0 : vf_modified[i] = add( mult_r( 26214, voice_factors[i] ), mult_r( 6554, voice_factors[i - 1] ) ); /* Q15 */
1493 0 : move16();
1494 : }
1495 0 : IF( NE_16( st_fx->L_frame, L_FRAME ) )
1496 : {
1497 0 : vf_modified[4] = add( mult_r( 26214, voice_factors[4] ), mult_r( 6554, voice_factors[3] ) ); /* Q15 */
1498 0 : move16();
1499 : }
1500 : }
1501 :
1502 : /* From low band excitation, generate highband excitation */
1503 0 : Lmax = 0;
1504 0 : move32();
1505 0 : FOR( cnt = 0; cnt < L_FRAME32k; cnt++ )
1506 : {
1507 0 : Lmax = L_max( Lmax, L_abs( bwe_exc_extended[cnt] ) );
1508 : }
1509 0 : IF( Lmax == 0 )
1510 : {
1511 0 : Q_bwe_exc = 31;
1512 0 : move16();
1513 : }
1514 : ELSE
1515 : {
1516 0 : Q_bwe_exc = norm_l( Lmax );
1517 : }
1518 0 : Q_bwe_exc = sub( Q_bwe_exc, 1 );
1519 0 : Q_bwe_exc = add( Q_bwe_exc, add( Q_exc, Q_exc ) );
1520 :
1521 0 : find_max_mem_wb( st_fx, &n_mem );
1522 :
1523 0 : if ( GT_16( sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc ), n_mem ) )
1524 : {
1525 0 : Q_bwe_exc = add( st_fx->prev_Q_bwe_exc, n_mem );
1526 : }
1527 :
1528 0 : test();
1529 0 : if ( uv_flag && GT_16( Q_bwe_exc, 20 ) )
1530 : {
1531 0 : Q_bwe_exc = 20;
1532 0 : move16(); /* restrict this to 21 due to the Q factor requireemnt of the random number generator (keep 1 bit headroom) */
1533 : }
1534 :
1535 0 : prev_pow = 0;
1536 0 : move32();
1537 0 : IF( st_fx->element_mode > EVS_MONO )
1538 : {
1539 0 : tmp = sub( shl( sub( st_fx->prev_Q_bwe_exc, 16 ), 1 ), 31 + 16 );
1540 0 : prev_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /*Q(2*(st_fx->prev_Q_bwe_exc-16))*/
1541 : }
1542 0 : FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
1543 : {
1544 0 : prev_pow = L_mac0_sat( prev_pow, hBWE_TD->state_syn_shbexc_fx[i], hBWE_TD->state_syn_shbexc_fx[i] ); /*Q(2*(st_fx->prev_Q_bwe_exc-16))*/
1545 : }
1546 :
1547 0 : rescale_genWB_mem( st_fx, sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc ) );
1548 :
1549 0 : Copy( hBWE_TD->old_bwe_exc_extended_fx, bwe_exc_extended_16, NL_BUFF_OFFSET );
1550 0 : sc = sub( Q_bwe_exc, add( Q_exc, Q_exc ) );
1551 0 : FOR( cnt = 0; cnt < L_FRAME32k; cnt++ )
1552 : {
1553 0 : bwe_exc_extended_16[cnt + NL_BUFF_OFFSET] = round_fx_sat( L_shl_sat( bwe_exc_extended[cnt], sc ) );
1554 : }
1555 0 : Copy( bwe_exc_extended_16 + L_FRAME32k, hBWE_TD->old_bwe_exc_extended_fx, NL_BUFF_OFFSET );
1556 :
1557 0 : Copy( hBWE_TD->state_syn_shbexc_fx, shaped_wb_excitation, L_SHB_LAHEAD / 4 );
1558 :
1559 0 : Q_bwe_exc_ext = sub( Q_bwe_exc, 16 );
1560 :
1561 0 : GenShapedWBExcitation_fx( shaped_wb_excitation + L_SHB_LAHEAD / 4, lpc_wb, exc4kWhtnd, hBWE_TD->mem_csfilt_fx,
1562 0 : hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->mem_genSHBexc_filt_down_wb2_fx,
1563 0 : hBWE_TD->mem_genSHBexc_filt_down_wb3_fx, hBWE_TD->state_lpc_syn_fx, coder_type,
1564 0 : bwe_exc_extended_16, Q_bwe_exc_ext, hBWE_TD->bwe_seed, vf_modified, uv_flag, st_fx->igf );
1565 :
1566 0 : curr_pow = 0;
1567 0 : move32();
1568 0 : IF( st_fx->element_mode > EVS_MONO )
1569 : {
1570 0 : tmp = sub( shl( Q_bwe_exc_ext, 1 ), 31 + 16 );
1571 0 : curr_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /*Q(2*(Q_bwe_exc_ext))*/
1572 : }
1573 0 : FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
1574 : {
1575 0 : curr_pow = L_mac0_sat( curr_pow, shaped_wb_excitation[i + L_SHB_LAHEAD / 4], shaped_wb_excitation[i + L_SHB_LAHEAD / 4] ); /* Q(2*Q_bwe_exc_ext) */
1576 : }
1577 :
1578 0 : if ( GT_16( voice_factors[0], 24576 ) )
1579 : {
1580 0 : curr_pow = L_shr( curr_pow, 2 ); /* Q(2*Q_bwe_exc_ext) */
1581 : }
1582 :
1583 0 : Lscale = root_a_over_b_fx( curr_pow, shl_r( Q_bwe_exc_ext, 1 ), prev_pow,
1584 0 : shl_r( sub( st_fx->prev_Q_bwe_exc, 16 ), 1 ), &exp );
1585 :
1586 0 : FOR( i = 0; i < L_SHB_LAHEAD / 4 - 1; i++ )
1587 : {
1588 0 : L_tmp = Mult_32_16( Lscale, shaped_wb_excitation[i] ); /* Q(16-exp+Q_bwe_exc_ext) */
1589 0 : shaped_wb_excitation[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc_ext */
1590 0 : move16();
1591 : }
1592 0 : Lscale = root_a_fx( Lscale, sub( 31, exp ), &exp );
1593 0 : L_tmp = Mult_32_16( Lscale, shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] ); /* Q(16-exp+Q_bwe_exc_ext) */
1594 0 : shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] = round_fx( L_shl( L_tmp, exp ) ); /* Q_bwe_exc_ext */
1595 :
1596 : /* Update SHB excitation */
1597 0 : Copy( shaped_wb_excitation + L_FRAME16k / 4, hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD / 4 );
1598 :
1599 :
1600 : /* Adjust the subframe and frame gain of the synthesized shb signal */
1601 : /* Scale the shaped excitation */
1602 0 : ScaleShapedWB_fx( SHB_OVERLAP_LEN / 2, shaped_wb_excitation, hBWE_TD->syn_overlap_fx, GainShape, GainFrame,
1603 : window_wb_fx, subwin_wb_fx,
1604 0 : Q_bwe_exc_ext, st_fx->L_frame, 0, &dummy, dummy, dummy2 );
1605 :
1606 0 : max = 0;
1607 0 : move16();
1608 0 : FOR( i = 0; i < L_FRAME16k / 4; i++ )
1609 : {
1610 0 : max = s_max( max, shaped_wb_excitation[i] ); /*Q0*/
1611 : }
1612 :
1613 0 : IF( max == 0 )
1614 : {
1615 0 : curr_frame_pow = 1;
1616 0 : move32();
1617 0 : n = 0;
1618 0 : move16();
1619 : }
1620 : ELSE
1621 : {
1622 0 : n = norm_s( max );
1623 0 : FOR( i = 0; i < L_FRAME16k / 4; i++ )
1624 : {
1625 : #ifdef ISSUE_1866_replace_overflow_libdec
1626 0 : shaped_wb_excitation_frac[i] = shl_sat( shaped_wb_excitation[i], n ); /*Q14*/
1627 : #else
1628 : shaped_wb_excitation_frac[i] = shl_o( shaped_wb_excitation[i], n, &Overflow ); /*Q14*/
1629 : #endif
1630 0 : move16();
1631 : }
1632 0 : n = sub( 14, n );
1633 0 : curr_frame_pow = 1;
1634 0 : move32();
1635 0 : FOR( i = 0; i < L_FRAME16k / 4; i++ )
1636 : {
1637 : #ifdef ISSUE_1866_replace_overflow_libdec
1638 0 : L_tmp = L_mult_sat( shaped_wb_excitation_frac[i], shaped_wb_excitation_frac[i] ); /*Q29*/
1639 0 : curr_frame_pow = L_add_sat( curr_frame_pow, L_shr( L_tmp, 7 ) ); /*Q22*/
1640 : #else
1641 : L_tmp = L_mult_o( shaped_wb_excitation_frac[i], shaped_wb_excitation_frac[i], &Overflow ); /*Q29*/
1642 : curr_frame_pow = L_add_o( curr_frame_pow, L_shr( L_tmp, 7 ), &Overflow ); /*Q22*/
1643 : #endif
1644 : }
1645 : }
1646 0 : curr_frame_pow_exp = add( n, n );
1647 :
1648 0 : IF( GT_16( hBWE_TD->prev_frame_pow_exp, curr_frame_pow_exp ) )
1649 : {
1650 0 : curr_frame_pow = L_shr( curr_frame_pow, sub( hBWE_TD->prev_frame_pow_exp, curr_frame_pow_exp ) );
1651 0 : curr_frame_pow_exp = hBWE_TD->prev_frame_pow_exp;
1652 0 : move16();
1653 : }
1654 : ELSE
1655 : {
1656 0 : hBWE_TD->prev_wb_bwe_frame_pow_fx = L_shr( hBWE_TD->prev_wb_bwe_frame_pow_fx, sub( curr_frame_pow_exp, hBWE_TD->prev_frame_pow_exp ) );
1657 0 : move32();
1658 : }
1659 :
1660 0 : test();
1661 0 : IF( !st_fx->bfi && st_fx->prev_bfi )
1662 : {
1663 0 : IF( GT_32( L_shr( curr_frame_pow, 1 ), hBWE_TD->prev_wb_bwe_frame_pow_fx ) )
1664 : {
1665 0 : L_tmp = root_a_over_b_fx( hBWE_TD->prev_wb_bwe_frame_pow_fx, 22, curr_frame_pow, 22, &exp );
1666 0 : scale = round_fx( L_shl( L_tmp, exp ) ); /*Q15*/
1667 :
1668 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
1669 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
1670 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
1671 0 : temp = round_fx( L_shl( L_tmp, exp ) ); /* Q15 */
1672 : }
1673 : ELSE
1674 : {
1675 0 : scale = temp = 32767;
1676 0 : move16(); /* Q15 */
1677 0 : move16();
1678 : }
1679 :
1680 0 : FOR( j = 0; j < 8; j++ )
1681 : {
1682 0 : GainShape[2 * j] = mult_r( GainShape[2 * j], scale );
1683 0 : GainShape[add( 2 * j, 1 )] = mult_r( GainShape[add( 2 * j, 1 )], scale );
1684 0 : FOR( i = 0; i < L_FRAME16k / ( 4 * 8 ); i++ )
1685 : {
1686 0 : shaped_wb_excitation[add( i, j * ( L_FRAME16k / ( 4 * 8 ) ) )] = mult_r( shaped_wb_excitation[add( i, j * ( L_FRAME16k / ( 4 * 8 ) ) )], scale );
1687 : }
1688 0 : IF( temp > 0 )
1689 : {
1690 0 : IF( LT_16( scale, temp ) )
1691 : {
1692 0 : scale = div_s( scale, temp );
1693 : }
1694 : ELSE
1695 : {
1696 0 : scale = 32767;
1697 0 : move16();
1698 : }
1699 : }
1700 : ELSE
1701 : {
1702 0 : scale = 0;
1703 0 : move16();
1704 : }
1705 : }
1706 : }
1707 :
1708 0 : hBWE_TD->prev_wb_bwe_frame_pow_fx = curr_frame_pow;
1709 0 : move32();
1710 0 : hBWE_TD->prev_frame_pow_exp = curr_frame_pow_exp;
1711 0 : move16();
1712 :
1713 : /* generate 16kHz SHB signal (6 - 8 kHz) from 2kHz signal */
1714 0 : max = 0;
1715 0 : move16();
1716 0 : FOR( cnt = 0; cnt < ( L_FRAME16k + L_SHB_LAHEAD ) / 4; cnt++ )
1717 : {
1718 0 : if ( GT_16( abs_s( shaped_wb_excitation[cnt] ), max ) )
1719 : {
1720 0 : max = abs_s( shaped_wb_excitation[cnt] );
1721 : }
1722 : }
1723 0 : Qx = norm_s( max );
1724 0 : if ( max == 0 )
1725 : {
1726 0 : Qx = 15;
1727 0 : move16();
1728 : }
1729 :
1730 0 : Qx = sub( Qx, 1 ); /* 1 bit space for saturation */
1731 :
1732 0 : max = 0;
1733 0 : move16();
1734 0 : FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
1735 : {
1736 0 : if ( GT_16( abs_s( hBWE_TD->state_lsyn_filt_shb_fx[i] ), max ) )
1737 : {
1738 0 : max = abs_s( hBWE_TD->state_lsyn_filt_shb_fx[i] );
1739 : }
1740 : }
1741 :
1742 0 : FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
1743 : {
1744 0 : if ( GT_16( abs_s( hBWE_TD->state_lsyn_filt_dwn_shb_fx[i] ), max ) )
1745 : {
1746 0 : max = abs_s( hBWE_TD->state_lsyn_filt_dwn_shb_fx[i] );
1747 : }
1748 : }
1749 :
1750 0 : IF( EQ_32( st_fx->output_Fs, 32000 ) )
1751 : {
1752 0 : FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
1753 : {
1754 0 : max = s_max( max, abs_s( hBWE_TD->state_32and48k_WB_upsample_fx[i] ) );
1755 : }
1756 : }
1757 0 : IF( EQ_32( st_fx->output_Fs, 48000 ) )
1758 : {
1759 0 : FOR( i = 0; i < INTERP_3_1_MEM_LEN; i++ )
1760 : {
1761 0 : max = s_max( max, abs_s( hBWE_TD->mem_resamp_HB_fx[i] ) );
1762 : }
1763 : }
1764 0 : n_mem = 15;
1765 0 : move16();
1766 0 : if ( max != 0 )
1767 : {
1768 0 : n_mem = norm_s( max );
1769 : }
1770 0 : n_mem = s_max( n_mem, 0 );
1771 :
1772 0 : if ( GT_16( sub( Qx, hBWE_TD->prev_Qx ), n_mem ) )
1773 : {
1774 0 : Qx = add( hBWE_TD->prev_Qx, n_mem );
1775 : }
1776 :
1777 0 : FOR( i = 0; i < ( L_FRAME16k + L_SHB_LAHEAD ) / 4; i++ )
1778 : {
1779 0 : shaped_wb_excitation[i] = shl( shaped_wb_excitation[i], Qx );
1780 0 : move16();
1781 : }
1782 :
1783 0 : FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
1784 : {
1785 0 : hBWE_TD->state_lsyn_filt_shb_fx[i] = shl( hBWE_TD->state_lsyn_filt_shb_fx[i], sub( Qx, hBWE_TD->prev_Qx ) );
1786 0 : move16();
1787 : }
1788 :
1789 0 : FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
1790 : {
1791 0 : hBWE_TD->state_lsyn_filt_dwn_shb_fx[i] = shl( hBWE_TD->state_lsyn_filt_dwn_shb_fx[i], sub( Qx, hBWE_TD->prev_Qx ) );
1792 0 : move16();
1793 : }
1794 :
1795 0 : GenWBSynth_fx( shaped_wb_excitation, error, hBWE_TD->state_lsyn_filt_shb_fx, hBWE_TD->state_lsyn_filt_dwn_shb_fx );
1796 :
1797 0 : Copy( error + L_FRAME16k - L_SHB_TRANSITION_LENGTH, hBWE_TD->old_tbe_synth_fx, L_SHB_TRANSITION_LENGTH );
1798 :
1799 0 : FOR( i = 0; i < L_FRAME16k; i++ )
1800 : {
1801 0 : synth[i] = mult_r( error[i], 21299 );
1802 0 : move16();
1803 : }
1804 :
1805 0 : IF( st_fx->hBWE_FD != NULL )
1806 : {
1807 0 : max = 0;
1808 0 : move16();
1809 0 : FOR( cnt = 0; cnt < L_FRAME16k; cnt++ )
1810 : {
1811 0 : max = s_max( max, abs_s( synth[cnt] ) );
1812 : }
1813 :
1814 0 : IF( max == 0 )
1815 : {
1816 0 : st_fx->hBWE_FD->last_wb_bwe_ener_fx = 0;
1817 0 : move16();
1818 : }
1819 : ELSE
1820 : {
1821 0 : n = norm_s( max );
1822 0 : FOR( cnt = 0; cnt < L_FRAME16k; cnt++ )
1823 : {
1824 0 : synth_frac[cnt] = shl( synth[cnt], n ); /*Q14*/
1825 0 : move16();
1826 : }
1827 0 : n = sub( sub( 14, n ), Qx );
1828 :
1829 0 : Lacc = 0;
1830 0 : move32();
1831 0 : FOR( i = 0; i < L_FRAME16k; i++ )
1832 : {
1833 0 : L_tmp = L_mult( synth_frac[i], synth_frac[i] ); /* Q29 */
1834 0 : Lacc = L_add( Lacc, L_shr( L_tmp, 7 ) ); /* Q22 */
1835 : }
1836 :
1837 0 : L_tmp = Mult_32_16( Lacc, 102 ); /* Q22 */
1838 0 : exp = norm_l( L_tmp );
1839 : #ifdef ISSUE_1866_replace_overflow_libdec
1840 0 : tmp = round_fx_sat( L_shl( L_tmp, exp ) );
1841 : #else
1842 : tmp = round_fx_o( L_shl_o( L_tmp, exp, &Overflow ), &Overflow );
1843 : #endif
1844 0 : exp = sub( add( exp, 22 ), 30 );
1845 0 : tmp = div_s( 16384, tmp );
1846 0 : L_tmp = Isqrt_lc( L_deposit_h( tmp ), &exp ); /* Q(31-exp) */
1847 0 : st_fx->hBWE_FD->last_wb_bwe_ener_fx = round_fx_sat( L_shl_sat( L_tmp, add( exp, sub( n, 12 ) ) ) ); /* Q3 */
1848 0 : move16();
1849 : }
1850 : }
1851 :
1852 0 : IF( EQ_32( st_fx->output_Fs, 32000 ) ) /* 32kHz sampling rate, but only WB output - interpolate */
1853 : {
1854 0 : Scale_sig( hBWE_TD->state_32and48k_WB_upsample_fx, 2 * ALLPASSSECTIONS_STEEP, sub( Qx, hBWE_TD->prev_Qx ) );
1855 0 : Interpolate_allpass_steep_fx( synth, hBWE_TD->state_32and48k_WB_upsample_fx, L_FRAME16k, upsampled_synth );
1856 0 : Copy( upsampled_synth, synth, L_FRAME32k );
1857 : }
1858 0 : ELSE IF( EQ_32( st_fx->output_Fs, 48000 ) )
1859 : {
1860 0 : Scale_sig( hBWE_TD->mem_resamp_HB_fx, INTERP_3_1_MEM_LEN, sub( Qx, hBWE_TD->prev_Qx ) );
1861 0 : interpolate_3_over_1_allpass_fx( synth, L_FRAME16k, upsampled_synth, hBWE_TD->mem_resamp_HB_fx );
1862 0 : Copy( upsampled_synth, synth, L_FRAME48k );
1863 : }
1864 : }
1865 : ELSE
1866 : {
1867 0 : f = 5461;
1868 0 : move16(); /* Q15 */
1869 0 : inc = 5461;
1870 0 : move16(); /* Q15 */
1871 0 : FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
1872 : {
1873 0 : lsf_wb[i] = f;
1874 0 : move16(); /*Q15*/
1875 0 : f = add_sat( f, inc );
1876 0 : move16();
1877 : }
1878 0 : GainFrame = 0; /* Q18 */
1879 0 : move32();
1880 0 : Qx = 0;
1881 0 : move16();
1882 0 : Q_bwe_exc = 31;
1883 0 : move16();
1884 0 : hBWE_TD->prev_wb_bwe_frame_pow_fx = 4194l /*0.001f Q22*/; /* Q22 */
1885 0 : move32();
1886 0 : hBWE_TD->prev_frame_pow_exp = 0;
1887 0 : move16();
1888 : }
1889 :
1890 : /* Update previous frame parameters for FEC */
1891 0 : IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
1892 : {
1893 0 : Copy( lsf_wb, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER_LBR_WB ); /*Q15*/
1894 : }
1895 : ELSE
1896 : {
1897 0 : Copy( lsf_wb, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER_WB ); /*Q15*/
1898 : }
1899 0 : hBWE_TD->GainFrame_prevfrm_fx = GainFrame; /* Q18 */
1900 0 : move32();
1901 :
1902 0 : IF( !st_fx->bfi )
1903 : {
1904 0 : hBWE_TD->GainAttn_fx = 32767; /*Q15*/
1905 0 : move16();
1906 : }
1907 :
1908 0 : *Q_synth = Qx;
1909 0 : move16();
1910 :
1911 0 : st_fx->prev_Q_bwe_exc = Q_bwe_exc;
1912 0 : move16();
1913 0 : hBWE_TD->prev_Qx = Qx;
1914 0 : move16();
1915 :
1916 0 : return;
1917 : }
1918 :
1919 :
1920 : /*======================================================================================*/
1921 : /* FUNCTION : void swb_tbe_dec_fx () */
1922 : /*--------------------------------------------------------------------------------------*/
1923 : /* PURPOSE : SWB TBE decoder, 6 - 14 kHz (or 7.5 - 15.5 kHz) band decoding module */
1924 : /*--------------------------------------------------------------------------------------*/
1925 : /* INPUT ARGUMENTS : */
1926 : /* _(Word16) coder_type : coding type */
1927 : /* _(Word16*) bwe_exc_extended :bandwidth extended exciatation Q0 */
1928 : /* _(Word16[]) voice_factors :voicing factors Q15 */
1929 : /* _(Word16*) Q_white_exc :Q Format of White Exc */
1930 : /* _(Word16*) Q_synth :Q Format of Synthesis */
1931 : /*--------------------------------------------------------------------------------------*/
1932 : /* OUTPUT ARGUMENTS : */
1933 : /* _(Word16*)synth : SHB synthesis/final synthesis Q_white_exc */
1934 : /* _(Word16*)White_exc16k : shaped white excitation for the FB TBE Q_synth */
1935 : /*--------------------------------------------------------------------------------------*/
1936 : /* INPUT/OUTPUT ARGUMENTS : */
1937 : /* _ Decoder_State *st_fx: : Decoder state structure */
1938 : /*--------------------------------------------------------------------------------------*/
1939 : /* RETURN ARGUMENTS : */
1940 : /* _ None */
1941 : /*--------------------------------------------------------------------------------------*/
1942 : /* CALLED FROM : RX */
1943 : /*======================================================================================*/
1944 1339 : void swb_tbe_dec_fx(
1945 : Decoder_State *st_fx, /* i/o: decoder state structure */
1946 : const Word16 coder_type, /* i : coding type */
1947 : Word32 *bwe_exc_extended, /* i : bandwidth extended excitation 2*Q_exc */
1948 : Word16 Q_exc,
1949 : const Word16 voice_factors[], /* i : voicing factors */
1950 : const Word16 old_syn_12k8_16k[], /* i : low band synthesis */
1951 : Word16 *White_exc16k, /* o : shaped white excitation for the FB TBE */
1952 : Word16 *Q_white_exc,
1953 : Word16 *synth, /* o : SHB synthesis/final synthesis */
1954 : Word16 *Q_synth,
1955 : Word16 *pitch_buf /* i : pitch buffer Q6 */
1956 : )
1957 : {
1958 : Word16 i;
1959 : Word16 shaped_shb_excitation[L_FRAME16k + L_SHB_LAHEAD];
1960 : Word16 bwe_exc_extended_16[L_FRAME32k + NL_BUFF_OFFSET];
1961 : Word16 lsf_shb[LPC_SHB_ORDER], lpc_shb[LPC_SHB_ORDER + 1], GainShape[NUM_SHB_SUBFR];
1962 : Word32 GainFrame;
1963 : Word16 error[L_FRAME32k];
1964 : Word32 L_ener;
1965 : Word16 ener;
1966 : Word16 is_fractive;
1967 : Word32 prev_pow, curr_pow, Lscale;
1968 : Word16 scale;
1969 : Word16 exp, tmp;
1970 : Word16 j, cnt;
1971 : Word16 n_mem, n_mem2, Qx, sc;
1972 : Word16 n_mem3;
1973 : Word32 Lmax, L_tmp;
1974 : Word16 frac;
1975 :
1976 : Word32 L_tmp1, L_tmp2;
1977 : Word16 expa, expb;
1978 : Word16 fraca, fracb;
1979 : Word16 GainShape_tmp[NUM_SHB_SUBGAINS];
1980 : Word16 Q_bwe_exc;
1981 : Word16 Q_shb;
1982 : Word16 vf_modified[NB_SUBFR16k];
1983 : Word16 stemp;
1984 :
1985 : Word16 tilt_swb_fec;
1986 : Word16 Q_bwe_exc_fb;
1987 :
1988 : Word16 lsp_shb_1[LPC_SHB_ORDER], lsp_shb_2[LPC_SHB_ORDER], lsp_temp[LPC_SHB_ORDER];
1989 : Word16 lpc_shb_sf[4 * ( LPC_SHB_ORDER + 1 )];
1990 : const Word16 *ptr_lsp_interp_coef;
1991 : Word32 shb_ener_sf_32;
1992 : Word16 shb_res_gshape[NB_SUBFR16k];
1993 : Word16 mixFactors;
1994 : Word16 vind;
1995 : Word16 shb_res_dummy[L_FRAME16k];
1996 : Word16 shaped_shb_excitationTemp[L_FRAME16k];
1997 : Word32 ener_tmp[NUM_SHB_SUBGAINS];
1998 : Word16 pitch_fx;
1999 : Word16 l_subframe_fx;
2000 : Word16 formant_fac;
2001 : Word16 lsf_diff[LPC_SHB_ORDER], w[LPC_SHB_ORDER];
2002 : Word16 refl[M];
2003 : Word16 tilt_para;
2004 : Word16 tmp1, tmp2;
2005 : Word16 f_fx, inc_fx;
2006 : Word32 GainFrame_prevfrm_fx;
2007 :
2008 : Word16 synth_scale_fx;
2009 : Word16 mean_vf;
2010 : Word16 exp_ener, inv_ener;
2011 1339 : Word32 prev_ener_ratio_fx = 0; /* initialize just to avoid compiler warning */
2012 1339 : move32();
2013 : Word16 max, n, temp, shaped_shb_excitation_frac[L_FRAME16k + L_SHB_LAHEAD];
2014 : Word32 curr_frame_pow;
2015 : Word16 curr_frame_pow_exp;
2016 : Word32 L_prev_ener_shb;
2017 : TD_BWE_DEC_HANDLE hBWE_TD;
2018 1339 : hBWE_TD = st_fx->hBWE_TD;
2019 :
2020 : /* initializations */
2021 1339 : GainFrame = L_deposit_l( 0 );
2022 1339 : mixFactors = 0;
2023 1339 : move16();
2024 1339 : shb_ener_sf_32 = L_deposit_l( 0 );
2025 1339 : set16_fx( shaped_shb_excitationTemp, 0, L_FRAME16k );
2026 1339 : st_fx->hTdCngDec->shb_dtx_count_fx = 0;
2027 1339 : move16();
2028 1339 : is_fractive = 0;
2029 1339 : move16();
2030 1339 : set16_fx( shb_res_gshape, 1638 /*0.1f Q14*/, NB_SUBFR16k ); /* Q14 */
2031 1339 : Q_shb = 0; /* high band target Q factor set to zero */
2032 1339 : move16();
2033 1339 : L_tmp = calc_tilt_bwe_fx( old_syn_12k8_16k, st_fx->Q_syn2, st_fx->L_frame );
2034 1339 : tilt_swb_fec = round_fx_sat( L_shl_sat( L_tmp, 3 ) );
2035 : /* i: old_syn_12k8_16k in st_fx->Q_syn2 */
2036 : /* o: tilt_swb_fec in Q11 */
2037 1339 : test();
2038 1339 : if ( st_fx->bfi && st_fx->clas_dec != UNVOICED_CLAS )
2039 : {
2040 0 : tilt_swb_fec = hBWE_TD->tilt_swb_fec_fx;
2041 0 : move16();
2042 : }
2043 :
2044 : /* WB/SWB bandwidth switching */
2045 1339 : test();
2046 1339 : test();
2047 1339 : IF( ( GT_16( st_fx->tilt_wb_fx, 10240 ) && ( st_fx->clas_dec == UNVOICED_CLAS ) ) || GT_16( st_fx->tilt_wb_fx, 20480 ) )
2048 : {
2049 57 : test();
2050 57 : test();
2051 57 : test();
2052 57 : test();
2053 57 : test();
2054 57 : test();
2055 57 : test();
2056 57 : test();
2057 57 : IF( ( ( st_fx->prev_fractive == 0 ) &&
2058 : ( LT_32( st_fx->prev_enerLH_fx, L_shl( st_fx->enerLH_fx, 1 ) ) && GT_32( st_fx->prev_enerLH_fx, L_shr( st_fx->enerLH_fx, 1 ) ) && LT_32( st_fx->prev_enerLL_fx, L_shl( st_fx->enerLL_fx, 1 ) ) && GT_32( st_fx->prev_enerLL_fx, L_shr( st_fx->enerLL_fx, 1 ) ) ) ) ||
2059 : ( EQ_16( st_fx->prev_fractive, 1 ) &&
2060 : GT_32( L_shr( st_fx->prev_enerLH_fx, 2 ), Mult_32_16( st_fx->enerLH_fx, 24576 ) ) ) /* 24576 in Q13*/
2061 : || ( GT_32( L_shr( st_fx->enerLL_fx, 1 ), Mult_32_16( st_fx->enerLH_fx, 24576 ) ) && /*24576 = 1.5 in Q14*/
2062 : LT_16( st_fx->tilt_wb_fx, 20480 ) ) /* 20480 = 10 in Q11*/
2063 : )
2064 : {
2065 41 : is_fractive = 0;
2066 41 : move16();
2067 : }
2068 : ELSE
2069 : {
2070 16 : is_fractive = 1;
2071 16 : move16();
2072 : }
2073 : }
2074 :
2075 : /* WB/SWB bandwidth switching */
2076 1339 : IF( st_fx->bws_cnt > 0 )
2077 : {
2078 0 : f_fx = 1489; /*Q15*/
2079 0 : move16();
2080 0 : inc_fx = 1489; /*Q15*/
2081 0 : move16();
2082 0 : IF( EQ_16( is_fractive, 1 ) )
2083 : {
2084 0 : Copy( lsf_tab_fx, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER );
2085 : }
2086 : ELSE
2087 : {
2088 0 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
2089 : {
2090 0 : hBWE_TD->lsp_prevfrm_fx[i] = f_fx;
2091 0 : move16();
2092 0 : f_fx = add( f_fx, inc_fx );
2093 : }
2094 : }
2095 0 : test();
2096 0 : test();
2097 0 : test();
2098 0 : test();
2099 0 : test();
2100 0 : test();
2101 0 : test();
2102 0 : IF( ( NE_16( st_fx->last_extl, SWB_TBE ) && NE_16( st_fx->last_extl, FB_TBE ) &&
2103 : !( ( L_sub( L_shr( st_fx->prev_enerLH_fx, 1 ), st_fx->enerLH_fx ) < 0 ) && L_sub( st_fx->prev_enerLH_fx, ( L_shr( st_fx->enerLH_fx, 1 ) > 0 ) ) ) ) ||
2104 : ( sub( st_fx->last_core, ACELP_CORE ) != 0 ) || ( ( sub( st_fx->last_core, ACELP_CORE ) == 0 ) && ( L_sub( L_abs( L_sub( st_fx->last_core_brate, st_fx->core_brate ) ), 3600 ) > 0 ) ) || ( sub( s_xor( is_fractive, st_fx->prev_fractive ), 1 ) == 0 ) )
2105 : {
2106 0 : set16_fx( GainShape, 11587, NUM_SHB_SUBFR );
2107 : }
2108 : ELSE
2109 : {
2110 0 : if ( GT_16( hBWE_TD->prev_GainShape_fx, 11587 ) )
2111 : {
2112 0 : hBWE_TD->prev_GainShape_fx = 11587;
2113 0 : move16();
2114 : }
2115 0 : set16_fx( GainShape, hBWE_TD->prev_GainShape_fx, NUM_SHB_SUBFR );
2116 : }
2117 :
2118 0 : Copy( hBWE_TD->lsp_prevfrm_fx, lsf_shb, LPC_SHB_ORDER );
2119 0 : set16_fx( shb_res_gshape, 3277 /*0.2f Q14*/, NB_SUBFR16k ); /* Q14 */
2120 : }
2121 : ELSE /* No bandwidth switching */
2122 : {
2123 1339 : test();
2124 1339 : IF( NE_16( st_fx->last_extl, SWB_TBE ) && NE_16( st_fx->last_extl, FB_TBE ) )
2125 : {
2126 53 : f_fx = 1489; /*Q15*/
2127 53 : move16();
2128 53 : inc_fx = 1489; /*Q15*/
2129 53 : move16();
2130 583 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
2131 : {
2132 530 : hBWE_TD->lsp_prevfrm_fx[i] = f_fx;
2133 530 : move16();
2134 530 : f_fx = add( f_fx, inc_fx );
2135 : }
2136 : }
2137 :
2138 1339 : IF( !st_fx->bfi )
2139 : {
2140 1339 : IF( st_fx->use_partial_copy )
2141 : {
2142 0 : IF( NE_16( st_fx->last_extl, SWB_TBE ) )
2143 : {
2144 0 : hBWE_TD->GainFrame_prevfrm_fx = 0;
2145 0 : move16();
2146 0 : f_fx = 1489 /*0.045454f Q15*/;
2147 0 : move16();
2148 0 : inc_fx = 1489 /*0.045454f Q15*/;
2149 0 : move16();
2150 0 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
2151 : {
2152 0 : hBWE_TD->lsp_prevfrm_fx[i] = f_fx;
2153 0 : move16();
2154 0 : f_fx = add( f_fx, inc_fx );
2155 : }
2156 : }
2157 0 : Copy( hBWE_TD->lsp_prevfrm_fx, lsf_shb, LPC_SHB_ORDER );
2158 0 : set16_fx( GainShape, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR );
2159 :
2160 0 : IF( EQ_16( st_fx->rf_frame_type, RF_NELP ) )
2161 : {
2162 : /* Frame gain */
2163 0 : GainFrame = L_mac( SHB_GAIN_QLOW_FX, st_fx->rf_indx_tbeGainFr, SHB_GAIN_QDELTA_FX );
2164 0 : move32(); /*Q18*/
2165 0 : L_tmp = Mult_32_16( GainFrame, 27213 ); /*Q16*/ /* 3.321928 in Q13 */
2166 :
2167 0 : frac = L_Extract_lc( L_tmp, &exp );
2168 0 : L_tmp = Pow2( 30, frac );
2169 0 : GainFrame = L_shl( L_tmp, sub( exp, 12 ) ); /*Q18*/
2170 0 : test();
2171 0 : test();
2172 0 : test();
2173 0 : test();
2174 0 : test();
2175 0 : test();
2176 0 : IF( ( st_fx->core == ACELP_CORE ) && ( st_fx->last_core == ACELP_CORE ) && !st_fx->prev_use_partial_copy && EQ_16( st_fx->prev_coder_type, UNVOICED ) && NE_32( GainFrame, hBWE_TD->GainFrame_prevfrm_fx ) && NE_16( st_fx->next_coder_type, GENERIC ) && EQ_16( st_fx->last_extl, SWB_TBE ) )
2177 : {
2178 : /*GainFrame = 0.2f*GainFrame + 0.8f*st_fx->GainFrame_prevfrm_fx;*/
2179 0 : GainFrame = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 26214 ), Mult_32_16( GainFrame, 6553 ) );
2180 : }
2181 : }
2182 : ELSE
2183 : {
2184 0 : temp = 0;
2185 0 : move16();
2186 : /* Frame gain */
2187 0 : SWITCH( st_fx->rf_indx_tbeGainFr )
2188 : {
2189 0 : case 0:
2190 0 : GainFrame = 131072; /* 0.5f in Q18 */
2191 0 : move32();
2192 0 : if ( LE_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) )
2193 : {
2194 0 : temp = 26214 /*0.8 Q15*/;
2195 0 : move16();
2196 : }
2197 0 : BREAK;
2198 0 : case 1:
2199 0 : GainFrame = 524288; /* 2.0f in Q18 */
2200 0 : move32();
2201 0 : test();
2202 0 : if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) )
2203 : {
2204 0 : temp = 26214 /*0.8 Q15*/;
2205 0 : move16();
2206 : }
2207 0 : BREAK;
2208 0 : case 2:
2209 0 : GainFrame = 1048576; /* 4.0f in Q18 */
2210 0 : move32();
2211 0 : test();
2212 0 : if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) )
2213 : {
2214 0 : temp = 26214 /*0.8 Q15*/;
2215 0 : move16();
2216 : }
2217 0 : BREAK;
2218 0 : case 3:
2219 0 : GainFrame = 2097152; /* 8.0f in Q18 */
2220 0 : move32();
2221 0 : test();
2222 0 : if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 4194304l /*16Q18*/ ) )
2223 : {
2224 0 : temp = 26214 /*0.8 Q15*/;
2225 0 : move16();
2226 : }
2227 0 : BREAK;
2228 0 : default:
2229 0 : fprintf( stderr, "RF SWB-TBE gain bits not supported." );
2230 : }
2231 0 : IF( EQ_16( st_fx->last_extl, SWB_TBE ) )
2232 : {
2233 0 : GainFrame = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, temp ), Mult_32_16( GainFrame, sub( 32767, temp ) ) );
2234 : }
2235 0 : test();
2236 0 : IF( ( st_fx->core == ACELP_CORE ) && ( st_fx->last_core == ACELP_CORE ) )
2237 : {
2238 0 : test();
2239 0 : test();
2240 0 : test();
2241 0 : test();
2242 0 : if ( !st_fx->prev_use_partial_copy && EQ_16( st_fx->last_coder_type, VOICED ) && EQ_16( st_fx->rf_frame_type, RF_GENPRED ) && GT_32( GainFrame, 2097152 ) && LT_32( GainFrame, 3059606 ) )
2243 : {
2244 0 : GainFrame = Mult_32_16( GainFrame, 9830 );
2245 : }
2246 : }
2247 : }
2248 : }
2249 : ELSE
2250 : {
2251 :
2252 : /* de-quantization */
2253 1339 : dequantizeSHBparams_fx_9_1( st_fx, st_fx->extl, st_fx->extl_brate, lsf_shb, GainShape, &GainFrame, &stemp,
2254 : &shb_ener_sf_32, shb_res_gshape, &mixFactors );
2255 1339 : Q_shb = 0;
2256 1339 : move16();
2257 : /* o: shb_ener_sf_32 in (2*Q_shb) */
2258 : /* o: shb_res_gshape in Q14 */
2259 : /* o: GainShape Q15 */
2260 : /* o: GainFrame Q18 */
2261 : }
2262 : }
2263 : ELSE /* FER concealment of TBE parameters */
2264 : {
2265 0 : Copy( hBWE_TD->lsp_prevfrm_fx, lsf_shb, LPC_SHB_ORDER );
2266 :
2267 : /* Gain shape concealment */
2268 0 : IF( EQ_16( st_fx->codec_mode, MODE1 ) )
2269 : {
2270 : /* Gradient based GS estimation */
2271 0 : gradientGainShape( st_fx, GainShape, &GainFrame );
2272 : /* o: GainShape[16] in Q15 */
2273 : /* o: GainFrame in Q18 */
2274 : }
2275 : ELSE
2276 : {
2277 0 : FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
2278 : {
2279 0 : FOR( j = 0; j < 4; j++ )
2280 : {
2281 0 : GainShape[add( i * 4, j )] = mult_r( st_fx->cummulative_damping, hBWE_TD->GainShape_Delay_fx[4 + i] );
2282 0 : move16();
2283 : }
2284 : }
2285 0 : IF( GT_16( tilt_swb_fec, ( 8 << 11 ) ) ) /* tilt_swb_fec in Q11 */
2286 : {
2287 0 : IF( EQ_16( st_fx->nbLostCmpt, 1 ) )
2288 : {
2289 0 : GainFrame = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 19661 /*0.6f Q15*/ );
2290 : }
2291 0 : ELSE IF( EQ_16( st_fx->nbLostCmpt, 2 ) )
2292 : {
2293 0 : GainFrame = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 11469 /*0.35f Q15*/ );
2294 : }
2295 : ELSE
2296 : {
2297 0 : GainFrame = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 6554 /*0.2f Q15*/ );
2298 : }
2299 0 : GainFrame = Mult_32_16( GainFrame, st_fx->cummulative_damping );
2300 : }
2301 : ELSE
2302 : {
2303 0 : GainFrame = hBWE_TD->GainFrame_prevfrm_fx;
2304 0 : move16(); /* gain locking */
2305 : }
2306 : }
2307 :
2308 : /* FER concealment for 24.4kbps and 32kbps */
2309 0 : test();
2310 0 : IF( EQ_32( st_fx->total_brate, ACELP_24k40 ) || EQ_32( st_fx->total_brate, ACELP_32k ) )
2311 : {
2312 0 : IF( EQ_16( st_fx->codec_mode, MODE1 ) )
2313 : {
2314 : /*scale = st->prev1_shb_ener_sf/root_a(st->prev2_shb_ener_sf * st->prev3_shb_ener_sf); */
2315 0 : L_tmp = L_mult( extract_l( hBWE_TD->prev2_shb_ener_sf_fx ), extract_l( hBWE_TD->prev3_shb_ener_sf_fx ) ); /*Q1*/
2316 0 : tmp = round_fx( root_a_fx( L_tmp, 1, &exp ) ); /* Q = 15-exp */
2317 0 : tmp1 = extract_l( hBWE_TD->prev1_shb_ener_sf_fx ); /*Q0*/
2318 0 : i = sub( norm_s( tmp1 ), 1 );
2319 0 : tmp1 = shl( tmp1, i ); /* Qi */
2320 0 : IF( tmp == 0 )
2321 : {
2322 0 : tmp = 32767 /*1.0f Q15*/;
2323 0 : move16(); /*Q15*/
2324 : }
2325 : ELSE
2326 : {
2327 0 : scale = div_s( tmp1, tmp ); /* Q15 - Q(15-exp) + Qi = Qexp+i */
2328 0 : scale = s_max( scale, 0 );
2329 : /*scale = st->prev_res_shb_gshape * min(scale, 1.0f); */
2330 0 : tmp = shl_sat( scale, sub( sub( 15, exp ), i ) ); /*Q15*/
2331 : }
2332 0 : scale = mult_r( hBWE_TD->prev_res_shb_gshape_fx, tmp ); /* Q14 */
2333 :
2334 0 : test();
2335 0 : IF( GT_32( L_shr( hBWE_TD->prev2_shb_ener_sf_fx, 1 ), hBWE_TD->prev1_shb_ener_sf_fx ) ||
2336 : GT_32( L_shr( hBWE_TD->prev3_shb_ener_sf_fx, 1 ), hBWE_TD->prev2_shb_ener_sf_fx ) )
2337 : {
2338 : /* shb_ener_sf_32 = 0.5f * scale * st_fx->prev1_shb_ener_sf_fx; */
2339 0 : shb_ener_sf_32 = Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, scale );
2340 :
2341 0 : if ( GT_16( st_fx->nbLostCmpt, 1 ) )
2342 : {
2343 : /* shb_ener_sf_32 *= 0.5f; */
2344 0 : shb_ener_sf_32 = L_shr( shb_ener_sf_32, 1 );
2345 : }
2346 : }
2347 : ELSE
2348 : {
2349 : /* shb_ener_sf = scale * scale * st_fx->prev1_shb_ener_sf_fx; */
2350 0 : L_tmp = L_mult( scale, scale ); /* Q29 */
2351 0 : shb_ener_sf_32 = L_shl( Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, round_fx( L_tmp ) ), 2 );
2352 : }
2353 : }
2354 : ELSE
2355 : {
2356 0 : test();
2357 0 : IF( GT_32( L_shr( hBWE_TD->prev2_shb_ener_sf_fx, 1 ), hBWE_TD->prev1_shb_ener_sf_fx ) ||
2358 : GT_32( L_shr( hBWE_TD->prev3_shb_ener_sf_fx, 1 ), hBWE_TD->prev2_shb_ener_sf_fx ) )
2359 : {
2360 : /* shb_ener_sf_32 = 0.5f * st->cummulative_damping * st_fx->prev1_shb_ener_sf_fx; */
2361 0 : shb_ener_sf_32 = L_shr( Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, st_fx->cummulative_damping ), 1 );
2362 : }
2363 : ELSE
2364 : {
2365 0 : shb_ener_sf_32 = Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, st_fx->cummulative_damping );
2366 : }
2367 : }
2368 : }
2369 :
2370 0 : shb_ener_sf_32 = L_max( shb_ener_sf_32, 1l /*1.0f Q0*/ );
2371 0 : mixFactors = hBWE_TD->prev_mixFactors_fx;
2372 0 : move16();
2373 :
2374 0 : IF( EQ_16( st_fx->codec_mode, MODE1 ) )
2375 : {
2376 0 : set16_fx( shb_res_gshape, 3277 /*0.2f Q14*/, 5 ); /* Q14 */
2377 : }
2378 : ELSE
2379 : {
2380 0 : set16_fx( shb_res_gshape, 16384 /*1.0f Q14*/, 5 ); /* Q14 */
2381 : }
2382 : }
2383 : }
2384 :
2385 : /* get the gainshape delay */
2386 1339 : Copy( &hBWE_TD->GainShape_Delay_fx[4], &hBWE_TD->GainShape_Delay_fx[0], NUM_SHB_SUBFR / 4 );
2387 1339 : test();
2388 1339 : IF( ( st_fx->rf_flag != 0 ) || EQ_32( st_fx->total_brate, ACELP_9k60 ) )
2389 : {
2390 0 : FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
2391 : {
2392 0 : hBWE_TD->GainShape_Delay_fx[i + 4] = s_min( s_max( GainShape[i * 4], 3277 /*0.1f Q15*/ ), 16384 /*0.5f Q15*/ );
2393 0 : move16();
2394 : }
2395 : }
2396 : ELSE
2397 : {
2398 6695 : FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
2399 : {
2400 5356 : hBWE_TD->GainShape_Delay_fx[i + 4] = GainShape[i * 4];
2401 5356 : move16();
2402 : }
2403 : }
2404 :
2405 : /* voice factor modification to limit any spurious jumps in the middle of voiced subframes*/
2406 : /* mean(voice_factors[i], 4); */
2407 1339 : L_tmp = L_mult( voice_factors[0], 8192 );
2408 1339 : L_tmp = L_mac( L_tmp, voice_factors[1], 8192 );
2409 1339 : L_tmp = L_mac( L_tmp, voice_factors[2], 8192 );
2410 1339 : mean_vf = mac_r( L_tmp, voice_factors[3], 8192 );
2411 :
2412 1339 : Copy( voice_factors, vf_modified, NB_SUBFR16k );
2413 :
2414 1339 : test();
2415 1339 : IF( EQ_16( coder_type, VOICED ) || GT_16( mean_vf, 13107 /*0.4f Q15*/ ) )
2416 : {
2417 2056 : FOR( i = 1; i < NB_SUBFR; i++ )
2418 : {
2419 1542 : L_tmp = L_mult( voice_factors[i], 26214 /*0.8f Q15*/ );
2420 1542 : vf_modified[i] = mac_r( L_tmp, voice_factors[i - 1], 6554 /*0.2f Q15*/ );
2421 1542 : move16();
2422 : }
2423 514 : IF( NE_16( st_fx->L_frame, L_FRAME ) )
2424 : {
2425 273 : L_tmp = L_mult( voice_factors[4], 26214 /*0.8f Q15*/ );
2426 273 : vf_modified[4] = mac_r( L_tmp, voice_factors[3], 6554 /*0.2f Q15*/ );
2427 273 : move16();
2428 : }
2429 : }
2430 :
2431 : /* convert quantized LSFs to LSPs for interpolation */
2432 1339 : E_LPC_lsf_lsp_conversion( lsf_shb, lsp_shb_2, LPC_SHB_ORDER );
2433 :
2434 1339 : test();
2435 1339 : IF( EQ_16( st_fx->last_extl, SWB_TBE ) || EQ_16( st_fx->last_extl, FB_TBE ) )
2436 : {
2437 : /* SHB LSP values from prev. frame for interpolation */
2438 1286 : Copy( hBWE_TD->swb_lsp_prev_interp_fx, lsp_shb_1, LPC_SHB_ORDER );
2439 : }
2440 : ELSE
2441 : {
2442 : /* Use current frame's LSPs; in effect no interpolation */
2443 53 : Copy( lsp_shb_2, lsp_shb_1, LPC_SHB_ORDER );
2444 : }
2445 :
2446 1339 : test();
2447 1339 : test();
2448 1339 : test();
2449 1339 : IF( ( st_fx->bws_cnt == 0 ) && ( st_fx->bws_cnt1 == 0 ) && ( st_fx->prev_use_partial_copy == 0 ) && ( st_fx->use_partial_copy == 0 ) )
2450 : {
2451 1339 : lsf_diff[0] = 16384;
2452 1339 : move16(); /*Q15*/
2453 1339 : lsf_diff[LPC_SHB_ORDER - 1] = 16384;
2454 1339 : move16(); /*Q15*/
2455 12051 : FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
2456 : {
2457 10712 : lsf_diff[i] = sub( lsf_shb[i], lsf_shb[i - 1] );
2458 10712 : move16();
2459 : }
2460 :
2461 1339 : a2rc_fx( hBWE_TD->cur_sub_Aq_fx + 1, refl, M );
2462 1339 : tmp = add( 16384, shr( refl[0], 1 ) ); /*Q14*/
2463 1339 : tmp1 = mult( 27425, tmp );
2464 1339 : tmp1 = mult( tmp1, tmp ); /*Q10*/
2465 1339 : tmp2 = shr( mult( 31715, tmp ), 2 ); /*Q10*/
2466 1339 : tilt_para = add( sub( tmp1, tmp2 ), 1335 ); /*Q10*/
2467 :
2468 1339 : test();
2469 1339 : IF( NE_16( st_fx->last_extl, SWB_TBE ) && NE_16( st_fx->last_extl, FB_TBE ) )
2470 : {
2471 477 : FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
2472 : {
2473 424 : hBWE_TD->prev_lsf_diff_fx[i - 1] = shr( lsf_diff[i], 1 );
2474 424 : move16();
2475 : }
2476 : }
2477 :
2478 1339 : IF( LE_32( st_fx->total_brate, ACELP_16k40 ) )
2479 : {
2480 735 : test();
2481 735 : test();
2482 735 : test();
2483 735 : test();
2484 735 : test();
2485 735 : IF( !( GT_16( hBWE_TD->prev_tilt_para_fx, 5120 ) && ( EQ_16( coder_type, TRANSITION ) || LT_16( tilt_para, 1024 ) ) ) &&
2486 : !( ( ( LT_16( hBWE_TD->prev_tilt_para_fx, 3072 ) && GE_16( st_fx->prev_coder_type, VOICED ) ) ) && GT_16( tilt_para, 5120 ) ) )
2487 : {
2488 6453 : FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
2489 : {
2490 5736 : IF( LT_16( lsf_diff[i], hBWE_TD->prev_lsf_diff_fx[i - 1] ) )
2491 : {
2492 2489 : tmp = mult( 26214, lsf_diff[i] );
2493 :
2494 2489 : test();
2495 2489 : IF( ( hBWE_TD->prev_lsf_diff_fx[i - 1] <= 0 ) || ( tmp < 0 ) ) /* safety check in case of bit errors */
2496 : {
2497 0 : st_fx->BER_detect = 1;
2498 0 : move16();
2499 0 : tmp = 0;
2500 0 : move16();
2501 : }
2502 : ELSE
2503 : {
2504 2489 : tmp = div_s( tmp, hBWE_TD->prev_lsf_diff_fx[i - 1] );
2505 : }
2506 :
2507 2489 : tmp = s_max( tmp, 16384 );
2508 2489 : w[i] = s_min( tmp, 32767 );
2509 2489 : move16();
2510 : }
2511 : ELSE
2512 : {
2513 3247 : tmp = mult( 26214, hBWE_TD->prev_lsf_diff_fx[i - 1] );
2514 :
2515 3247 : test();
2516 3247 : IF( ( lsf_diff[i] <= 0 ) || ( tmp < 0 ) ) /* safety check in case of bit errors */
2517 : {
2518 0 : st_fx->BER_detect = 1;
2519 0 : move16();
2520 0 : tmp = 0;
2521 0 : move16();
2522 : }
2523 : ELSE
2524 : {
2525 3247 : tmp = div_s( tmp, lsf_diff[i] );
2526 : }
2527 :
2528 3247 : tmp = s_max( tmp, 16384 );
2529 3247 : w[i] = s_min( tmp, 32767 );
2530 3247 : move16();
2531 : }
2532 : }
2533 717 : w[0] = w[1];
2534 717 : move16();
2535 717 : w[LPC_SHB_ORDER - 1] = w[LPC_SHB_ORDER - 2];
2536 717 : move16();
2537 :
2538 7887 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
2539 : {
2540 7170 : tmp1 = mult( lsp_shb_1[i], sub( 32767, w[i] ) );
2541 7170 : tmp2 = mult( lsp_shb_2[i], w[i] );
2542 7170 : lsp_temp[i] = add( tmp1, tmp2 );
2543 7170 : move16();
2544 : }
2545 : }
2546 : ELSE
2547 : {
2548 18 : Copy( lsp_shb_2, lsp_temp, LPC_SHB_ORDER );
2549 : }
2550 : }
2551 :
2552 1339 : Copy( lsf_diff + 1, hBWE_TD->prev_lsf_diff_fx, LPC_SHB_ORDER - 2 );
2553 1339 : hBWE_TD->prev_tilt_para_fx = tilt_para;
2554 1339 : move16();
2555 : }
2556 : ELSE
2557 : {
2558 0 : Copy( lsp_shb_2, lsp_temp, LPC_SHB_ORDER );
2559 : }
2560 :
2561 1339 : test();
2562 1339 : IF( EQ_32( st_fx->total_brate, ACELP_24k40 ) || EQ_32( st_fx->total_brate, ACELP_32k ) )
2563 : {
2564 : /* ---------- SHB LSP interpolation ---------- */
2565 604 : ptr_lsp_interp_coef = interpol_frac_shb; /*Q15*/
2566 3020 : FOR( j = 0; j < 4; j++ )
2567 : {
2568 26576 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
2569 : {
2570 : /*lsp_temp_fx[i] = lsp_shb_1_fx[i]*(*ptr_lsp_interp_coef_fx) */
2571 : /* + lsp_shb_2_fx[i]*(*(ptr_lsp_interp_coef_fx+1));*/
2572 24160 : L_tmp = L_mult( lsp_shb_1[i], ( *ptr_lsp_interp_coef ) );
2573 24160 : lsp_temp[i] = mac_r( L_tmp, lsp_shb_2[i], ( *( ptr_lsp_interp_coef + 1 ) ) );
2574 24160 : move16();
2575 : }
2576 2416 : ptr_lsp_interp_coef += 2;
2577 :
2578 : /* convert from lsp to lsf */
2579 : /*old code: lsp2lsf_fx(lsp_temp, lsp_temp, LPC_SHB_ORDER, INT_FS_FX); */ /* input lsp_temp_fx in Q15*/
2580 :
2581 2416 : tmp = i_mult( j, LPC_SHB_ORDER + 1 );
2582 : /* convert LSPs to LP coefficients */
2583 2416 : E_LPC_f_lsp_a_conversion( lsp_temp, lpc_shb_sf + tmp, LPC_SHB_ORDER );
2584 : /* Bring the LPCs to Q12 */
2585 2416 : Copy_Scale_sig( lpc_shb_sf + tmp, lpc_shb_sf + tmp, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_sf[tmp] ), 2 ) );
2586 : }
2587 : }
2588 : /*ELSE*/
2589 : {
2590 : /* for 13.2 and 16.4kbps */
2591 1339 : E_LPC_f_lsp_a_conversion( lsp_temp, lpc_shb, LPC_SHB_ORDER );
2592 1339 : Copy_Scale_sig( lpc_shb, lpc_shb, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb[0] ), 2 ) ); /* Q12 */
2593 : }
2594 :
2595 : /* Save the SWB LSP values from current frame for interpolation */
2596 1339 : Copy( lsp_shb_2, hBWE_TD->swb_lsp_prev_interp_fx, LPC_SHB_ORDER );
2597 : /* lsp_shb_2_fx in Q15 */
2598 :
2599 : /* save the shb_ener Q18, prev_resgainshape Q14, and mixFactor Q15 values */
2600 1339 : hBWE_TD->prev3_shb_ener_sf_fx = hBWE_TD->prev2_shb_ener_sf_fx;
2601 1339 : move32();
2602 1339 : hBWE_TD->prev2_shb_ener_sf_fx = hBWE_TD->prev1_shb_ener_sf_fx;
2603 1339 : move32();
2604 1339 : hBWE_TD->prev1_shb_ener_sf_fx = shb_ener_sf_32;
2605 1339 : move32();
2606 1339 : hBWE_TD->prev_res_shb_gshape_fx = shb_res_gshape[4];
2607 1339 : move16();
2608 1339 : hBWE_TD->prev_mixFactors_fx = mixFactors;
2609 1339 : move16();
2610 :
2611 : /* SWB CNG/DTX - update memories */
2612 1339 : if ( st_fx->hTdCngDec != NULL )
2613 : {
2614 1339 : Copy( st_fx->hTdCngDec->lsp_shb_prev_fx, st_fx->hTdCngDec->lsp_shb_prev_prev_fx, LPC_SHB_ORDER ); /* Q15 */
2615 1339 : Copy( lsf_shb, st_fx->hTdCngDec->lsp_shb_prev_fx, LPC_SHB_ORDER ); /* Q15 */
2616 : }
2617 :
2618 : /* vind = (short)(mixFactors*8.0f); */
2619 1339 : vind = shl( mixFactors, 3 - 15 ); /* 3 for mpy by 8.0f, -15 to bring it to Q0 */
2620 : /* i: mixFactors in Q15 */
2621 : /* o: vind in Q0 */
2622 :
2623 : /* Determine formant PF strength */
2624 1339 : formant_fac = swb_formant_fac_fx( lpc_shb[1], &hBWE_TD->tilt_mem_fx );
2625 : /* o: formant_fac in Q15 */
2626 :
2627 : /* -------- start of memory rescaling -------- */
2628 : /* ----- calculate optimum Q_bwe_exc and rescale memories accordingly ----- */
2629 1339 : Lmax = 0;
2630 1339 : move32();
2631 858299 : FOR( cnt = 0; cnt < L_FRAME32k; cnt++ )
2632 : {
2633 856960 : Lmax = L_max( Lmax, L_abs( bwe_exc_extended[cnt] ) );
2634 : }
2635 1339 : Q_bwe_exc = norm_l( Lmax );
2636 1339 : if ( Lmax == 0 )
2637 : {
2638 0 : Q_bwe_exc = 31;
2639 0 : move16();
2640 : }
2641 1339 : Q_bwe_exc = add( Q_bwe_exc, add( Q_exc, Q_exc ) );
2642 :
2643 : /* Account for any outliers in the memories from previous frame for rescaling to avoid saturation */
2644 1339 : find_max_mem_dec( st_fx, &n_mem, &n_mem2, &n_mem3 ); /* for >=24.4, use n_mem2 lpc_syn, shb_20sample, and mem_stp_swb_fx memory */
2645 :
2646 1339 : tmp = add( st_fx->prev_Q_bwe_exc, n_mem );
2647 1339 : if ( GT_16( Q_bwe_exc, tmp ) )
2648 : {
2649 61 : Q_bwe_exc = tmp;
2650 61 : move16();
2651 : }
2652 :
2653 : /* rescale the memories if Q_bwe_exc is different from previous frame */
2654 1339 : sc = sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc );
2655 1339 : IF( sc != 0 )
2656 : {
2657 714 : rescale_genSHB_mem_dec( st_fx, sc );
2658 : }
2659 :
2660 : /* rescale the bwe_exc_extended and bring it to 16-bit single precision with dynamic norm */
2661 1339 : Copy( hBWE_TD->old_bwe_exc_extended_fx, bwe_exc_extended_16, NL_BUFF_OFFSET );
2662 1339 : sc = sub( Q_bwe_exc, add( Q_exc, Q_exc ) );
2663 :
2664 858299 : FOR( cnt = 0; cnt < L_FRAME32k; cnt++ )
2665 : {
2666 856960 : bwe_exc_extended_16[cnt + NL_BUFF_OFFSET] = round_fx_sat( L_shl_sat( bwe_exc_extended[cnt], sc ) );
2667 : }
2668 1339 : Copy( bwe_exc_extended_16 + L_FRAME32k, hBWE_TD->old_bwe_exc_extended_fx, NL_BUFF_OFFSET );
2669 :
2670 : /* 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 */
2671 1339 : Copy( hBWE_TD->state_syn_shbexc_fx, shaped_shb_excitation, L_SHB_LAHEAD );
2672 :
2673 : /* save the previous Q factor (32-bit) of the buffer */
2674 1339 : st_fx->prev_Q_bwe_exc = Q_bwe_exc;
2675 1339 : move16();
2676 :
2677 1339 : Q_bwe_exc = sub( Q_bwe_exc, 16 ); /* Q_bwe_exc reflecting the single precision dynamic norm-ed buffers from here */
2678 :
2679 : /* -------- end of rescaling memories -------- */
2680 1339 : Q_bwe_exc_fb = hBWE_TD->prev_Q_bwe_exc_fb;
2681 1339 : move16();
2682 :
2683 1339 : IF( GT_32( st_fx->total_brate, ACELP_32k ) )
2684 : {
2685 0 : FOR( j = 0; j < 4; j++ )
2686 : {
2687 0 : Copy( lpc_shb, &lpc_shb_sf[i_mult( j, LPC_SHB_ORDER + 1 )], LPC_SHB_ORDER + 1 );
2688 : }
2689 : }
2690 :
2691 : /* Calculate the 6 to 14 kHz (or 7.5 - 15.5 kHz) SHB excitation signal from the low band ACELP core excitation */
2692 1339 : GenShapedSHBExcitation_fx( shaped_shb_excitation + L_SHB_LAHEAD, lpc_shb, White_exc16k,
2693 1339 : hBWE_TD->mem_csfilt_fx, hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->state_lpc_syn_fx,
2694 1339 : coder_type, bwe_exc_extended_16, hBWE_TD->bwe_seed, vf_modified, st_fx->extl,
2695 : &( hBWE_TD->tbe_demph_fx ), &( hBWE_TD->tbe_premph_fx ), lpc_shb_sf, shb_ener_sf_32,
2696 1339 : shb_res_gshape, shb_res_dummy, &vind, formant_fac, hBWE_TD->fb_state_lpc_syn_fx,
2697 1339 : &( hBWE_TD->fb_tbe_demph_fx ), &Q_bwe_exc, &Q_bwe_exc_fb, Q_shb, n_mem2, hBWE_TD->prev_Q_bwe_syn, st_fx->total_brate, st_fx->prev_bfi );
2698 :
2699 1339 : *Q_white_exc = Q_bwe_exc_fb;
2700 1339 : move16();
2701 1339 : IF( EQ_16( st_fx->extl, FB_TBE ) )
2702 : {
2703 0 : hBWE_TD->prev_Q_bwe_exc_fb = Q_bwe_exc_fb;
2704 0 : move16();
2705 : }
2706 : ELSE
2707 : {
2708 : /*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.
2709 : 51 because of 31 + 20(shift of Q_bwe_exc_fb before de-emphasis)*/
2710 1339 : hBWE_TD->prev_Q_bwe_exc_fb = 51;
2711 1339 : move16();
2712 : }
2713 : /* rescale the TBE post proc memory */
2714 14729 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
2715 : {
2716 13390 : hBWE_TD->mem_stp_swb_fx[i] = shl( hBWE_TD->mem_stp_swb_fx[i], sub( Q_bwe_exc, hBWE_TD->prev_Q_bwe_syn ) );
2717 13390 : move16();
2718 : }
2719 :
2720 6695 : FOR( i = 0; i < L_FRAME16k; i += L_SUBFR16k )
2721 : {
2722 : /* TD BWE post-processing */
2723 5356 : PostShortTerm_fx( &shaped_shb_excitation[L_SHB_LAHEAD + i], lpc_shb, &shaped_shb_excitationTemp[i], hBWE_TD->mem_stp_swb_fx,
2724 5356 : hBWE_TD->ptr_mem_stp_swb_fx, &( hBWE_TD->gain_prec_swb_fx ), hBWE_TD->mem_zero_swb_fx, formant_fac );
2725 : }
2726 1339 : Copy( shaped_shb_excitationTemp, &shaped_shb_excitation[L_SHB_LAHEAD], L_FRAME16k ); /* Q_bwe_exc */
2727 :
2728 1339 : tmp = sub( shl( Q_bwe_exc, 1 ), 31 + 16 );
2729 1339 : prev_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /* 2*(Q_bwe_exc) */
2730 1339 : curr_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /* 2*(Q_bwe_exc) */
2731 41509 : FOR( i = 0; i < L_SHB_LAHEAD + 10; i++ )
2732 : {
2733 40170 : prev_pow = L_mac0_sat( prev_pow, shaped_shb_excitation[i], shaped_shb_excitation[i] ); /*2*Q_bwe_exc*/
2734 40170 : curr_pow = L_mac0_sat( curr_pow, shaped_shb_excitation[i + L_SHB_LAHEAD + 10], shaped_shb_excitation[i + L_SHB_LAHEAD + 10] ); /* 2*Q_bwe_exc */
2735 : }
2736 :
2737 1339 : if ( GT_16( voice_factors[0], 24576 /*0.75f Q15*/ ) )
2738 : {
2739 48 : curr_pow = L_shr( curr_pow, 2 ); /* Q(2*Q_bwe_exc) */
2740 : }
2741 :
2742 1339 : Lscale = root_a_over_b_fx( curr_pow, shl( Q_bwe_exc, 1 ), prev_pow, shl( Q_bwe_exc, 1 ), &exp );
2743 :
2744 28119 : FOR( i = 0; i < L_SHB_LAHEAD; i++ )
2745 : {
2746 26780 : L_tmp = Mult_32_16( Lscale, shaped_shb_excitation[i] ); /* Q_bwe_exc + (31-exp) - 15 */
2747 26780 : shaped_shb_excitation[i] = round_fx( L_shl( L_tmp, exp ) ); /* Q_bwe_exc */
2748 26780 : move16();
2749 : }
2750 1339 : IF( exp < 0 )
2751 : {
2752 139 : Lscale = L_shl( Lscale, exp );
2753 139 : exp = 0;
2754 139 : move16();
2755 : }
2756 : /*
2757 : code for EVS and IVAS are basically identical with the exception of i_mult_sat() which has precision issues
2758 : thus is was replaced for IVAS and kept for EVS, in order to keep EVS BE to test sequences and legacy implementations
2759 : */
2760 1339 : IF( EQ_16( st_fx->element_mode, EVS_MONO ) )
2761 : {
2762 14729 : FOR( ; i < L_SHB_LAHEAD + 10; i++ )
2763 : {
2764 13390 : temp = i_mult_sat( sub( i, 19 ), 3277 /*0.1f Q15*/ ); /* Q15 */
2765 13390 : L_tmp1 = Mult_32_16( L_shl_sat( 1, sub( 31, exp ) ), temp ); /* Q31-exp */
2766 13390 : temp = sub( 32767 /*1.0f Q15*/, temp );
2767 13390 : Lscale = L_add( Mult_32_16( Lscale, temp ), L_tmp1 );
2768 13390 : L_tmp = Mult_32_16( Lscale, shaped_shb_excitation[i] ); /* Q_bwe_exc + (31-exp) - 15 */
2769 13390 : shaped_shb_excitation[i] = round_fx( L_shl( L_tmp, exp ) ); /* Q_bwe_exc */
2770 13390 : move16();
2771 : }
2772 : }
2773 : ELSE
2774 : {
2775 0 : FOR( ; i < L_SHB_LAHEAD + 10; i++ )
2776 : {
2777 0 : temp = round_fx_sat( L_shl( L_mult( 0x6666 /* 0.1 in Q12 */, shl( sub( i, 19 ), 11 ) ), 1 ) );
2778 0 : L_tmp1 = Mult_32_16( L_shl_sat( 1, sub( 31, exp ) ), temp ); /* Q31-exp */
2779 0 : temp = sub( 32767 /*1.0f Q15*/, temp );
2780 0 : Lscale = L_add( Mult_32_16( Lscale, temp ), L_tmp1 );
2781 0 : L_tmp = Mult_32_16( Lscale, shaped_shb_excitation[i] ); /* Q_bwe_exc + (31-exp) - 15 */
2782 0 : shaped_shb_excitation[i] = round_fx( L_shl( L_tmp, exp ) ); /* Q_bwe_exc */
2783 0 : move16();
2784 : }
2785 : }
2786 :
2787 : /* Update SHB excitation */
2788 1339 : Copy( shaped_shb_excitation + L_FRAME16k, hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD ); /* Q_bwe_exc */
2789 :
2790 1339 : l_subframe_fx = L_FRAME16k / NUM_SHB_SUBGAINS;
2791 1339 : L_ener = 0;
2792 1339 : move32();
2793 6695 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
2794 : {
2795 5356 : L_tmp = 0;
2796 5356 : move32();
2797 5356 : ener_tmp[i] = 0;
2798 5356 : move32();
2799 : {
2800 5356 : Word64 tmp64 = 0;
2801 5356 : move64();
2802 433836 : FOR( j = 0; j < l_subframe_fx; j++ )
2803 : {
2804 428480 : tmp64 = W_mac0_16_16( tmp64, shaped_shb_excitation[add( i_mult( i, l_subframe_fx ), j )], shaped_shb_excitation[add( i_mult( i, l_subframe_fx ), j )] ); /* 2*Q_bwe_exc */
2805 : }
2806 5356 : L_tmp = W_sat_l( tmp64 );
2807 : }
2808 :
2809 5356 : L_tmp = Mult_32_16( L_tmp, 410 /*0.0125 Q15*/ ); /* 2*Q_bwe_exc: ener_tmp_fx in (2*Q_bwe_exc) */
2810 5356 : IF( L_tmp != 0 )
2811 : {
2812 5349 : exp = norm_l( L_tmp );
2813 5349 : tmp = extract_h( L_shl( L_tmp, exp ) );
2814 5349 : exp = sub( exp, sub( 30, i_mult( 2, Q_bwe_exc ) ) );
2815 :
2816 5349 : tmp = div_s( 16384, tmp );
2817 5349 : L_tmp = L_deposit_h( tmp );
2818 5349 : L_tmp = Isqrt_lc( L_tmp, &exp );
2819 5349 : ener_tmp[i] = L_shl_sat( L_tmp, sub( add( exp, shl( Q_bwe_exc, 1 ) ), 31 ) ); /*2 * Q_bwe_exc: Q31 -exp +exp +2 * Q_bwe_exc -31 */
2820 5349 : move32();
2821 5349 : L_ener = L_add_sat( L_ener, L_shr( ener_tmp[i], 2 ) ); /* 2*Q_bwe_exc */
2822 : }
2823 : }
2824 1339 : ener = s_max( 1, round_fx_sat( L_shl_sat( L_ener, sub( 19, shl( Q_bwe_exc, 1 ) ) ) ) ); /* Q3: 2*Q_bwe_exc+19-2*Q_bwe_exc-16 */
2825 : /* WB/SWB bandwidth switching */
2826 1339 : IF( st_fx->bws_cnt > 0 )
2827 : {
2828 0 : ener = mult( ener, 11587 );
2829 : /*bandwidth switching should be updated*/
2830 0 : if ( GT_16( st_fx->tilt_swb_fx, 16384 ) )
2831 : {
2832 0 : st_fx->prev_fractive = 1;
2833 0 : move16();
2834 : }
2835 :
2836 0 : IF( is_fractive == 0 )
2837 : {
2838 0 : IF( GT_16( st_fx->tilt_wb_fx, 2048 ) ) /*assuming st_fx->tilt_wb_fx in Q11*/
2839 : {
2840 0 : st_fx->tilt_wb_fx = 2048;
2841 0 : move16();
2842 : }
2843 0 : ELSE IF( LT_16( st_fx->tilt_wb_fx, 1024 ) )
2844 : {
2845 0 : st_fx->tilt_wb_fx = 1024;
2846 0 : move16();
2847 : }
2848 0 : test();
2849 0 : if ( EQ_16( st_fx->prev_fractive, 1 ) && GT_16( st_fx->tilt_wb_fx, 1024 ) )
2850 : {
2851 0 : st_fx->tilt_wb_fx = 1024;
2852 0 : move16();
2853 : }
2854 : }
2855 : ELSE
2856 : {
2857 0 : IF( GT_16( st_fx->tilt_wb_fx, 8192 ) )
2858 : {
2859 0 : IF( st_fx->prev_fractive == 0 )
2860 : {
2861 0 : st_fx->tilt_wb_fx = 8192;
2862 0 : move16();
2863 : }
2864 : ELSE
2865 : {
2866 0 : st_fx->tilt_wb_fx = 16384;
2867 0 : move16();
2868 : }
2869 : }
2870 : ELSE
2871 : {
2872 0 : st_fx->tilt_wb_fx = shl( st_fx->tilt_wb_fx, 2 );
2873 0 : move16();
2874 : }
2875 : }
2876 :
2877 0 : IF( ener != 0 )
2878 : {
2879 0 : L_tmp = L_shl( L_mult0( ener, st_fx->tilt_wb_fx ), sub( st_fx->Q_syn2, 14 ) ); /* 3+11 +st_fx->Q_syn2 -14 = st_fx->Q_syn2*/
2880 0 : exp_ener = norm_s( ener );
2881 0 : tmp = shl( ener, exp_ener ); /*Q(3+exp)*/
2882 0 : inv_ener = div_s( 16384, tmp ); /*Q(15+14-3-exp) = 26- exp*/
2883 :
2884 0 : test();
2885 0 : IF( GT_32( L_tmp, st_fx->enerLH_fx ) ) /*st_fx->Q_syn2*/
2886 : {
2887 0 : st_fx->tilt_wb_fx = extract_h( L_shr_sat( Mult_32_16( st_fx->enerLH_fx, inv_ener ), sub( sub( st_fx->Q_syn2, exp_ener ), 16 ) ) ); /*Q11*/
2888 0 : move16();
2889 : /*st_fx->Q_syn2 -1 + 26- exp_ener -15 -(st_fx->Q_syn2 -exp_ener -16 ) -16 +1 -1 = (11) *0.5*/
2890 : }
2891 0 : ELSE IF( LT_32( L_tmp, Mult_32_16( st_fx->enerLH_fx, 1638 ) ) && EQ_16( is_fractive, 1 ) )
2892 : {
2893 0 : st_fx->tilt_wb_fx = extract_h( L_shr_sat( Mult_32_16( st_fx->enerLH_fx, inv_ener ), sub( sub( st_fx->Q_syn2, exp_ener ), 15 ) ) ); /*Q11*/
2894 0 : move16();
2895 : /*st_fx->Q_syn2 -1 + 26- exp_ener -15 -(st_fx->Q_syn2 -exp_ener -15 ) -16 = (11) 0.25*/
2896 : }
2897 0 : L_tmp = L_mult0( st_fx->prev_ener_shb_fx, inv_ener ); /*Q(1+15+14-3-exp_ener) = 27 -exp_ener*/
2898 0 : GainFrame_prevfrm_fx = L_shr_sat( L_tmp, sub( 9, exp_ener ) ); /*27 -exp_ener -(9-exp_ener )= Q18*/
2899 : }
2900 : ELSE
2901 : {
2902 0 : GainFrame_prevfrm_fx = 0;
2903 0 : move32();
2904 : }
2905 :
2906 0 : IF( EQ_16( is_fractive, 1 ) )
2907 : {
2908 0 : GainFrame = L_shl( L_deposit_l( st_fx->tilt_wb_fx ), 10 );
2909 : }
2910 : ELSE
2911 : {
2912 0 : GainFrame = L_shl( L_deposit_l( st_fx->tilt_wb_fx ), 8 );
2913 : }
2914 :
2915 0 : test();
2916 0 : IF( EQ_16( s_and( is_fractive, st_fx->prev_fractive ), 1 ) && GT_32( GainFrame, GainFrame_prevfrm_fx ) )
2917 : {
2918 0 : GainFrame = L_add( Mult_32_16( GainFrame_prevfrm_fx, 26214 ), Mult_32_16( GainFrame, 6554 ) ); /* 18 +15 -15 = 18*/
2919 : }
2920 : ELSE
2921 : {
2922 0 : test();
2923 0 : test();
2924 0 : test();
2925 0 : test();
2926 0 : IF( ( LT_32( L_shr( st_fx->prev_enerLH_fx, 1 ), st_fx->enerLH_fx ) && GT_32( st_fx->prev_enerLH_fx, L_shr( st_fx->enerLH_fx, 1 ) ) ) && ( LT_32( L_shr( st_fx->prev_enerLL_fx, 1 ), st_fx->enerLL_fx ) && GT_32( st_fx->prev_enerLL_fx, L_shr( st_fx->enerLL_fx, 1 ) ) ) && ( s_xor( is_fractive, st_fx->prev_fractive ) == 0 ) )
2927 : {
2928 0 : GainFrame = L_add( L_shr( GainFrame, 1 ), L_shr( GainFrame_prevfrm_fx, 1 ) );
2929 : }
2930 : ELSE
2931 : {
2932 0 : test();
2933 0 : IF( ( is_fractive == 0 ) && EQ_16( st_fx->prev_fractive, 1 ) )
2934 : {
2935 0 : L_tmp1 = L_shl( Mult_32_16( GainFrame, 3277 ), 13 ); /* 31 */
2936 0 : L_tmp = L_sub( 2147483647, L_tmp1 ); /* 31 */
2937 0 : GainFrame = L_add( Mult_32_32( GainFrame, L_tmp ), Mult_32_32( GainFrame_prevfrm_fx, L_tmp1 ) ); /* 18 */
2938 : }
2939 : ELSE
2940 : {
2941 0 : GainFrame = L_add( L_shr( GainFrame, 1 ), L_shr( L_min( GainFrame_prevfrm_fx, GainFrame ), 1 ) ); /* 18 */
2942 : }
2943 : }
2944 : }
2945 :
2946 0 : GainFrame = Mult_32_16( GainFrame, i_mult( sub( N_WS2N_FRAMES, st_fx->bws_cnt ), 819 ) ); /*Q18*/
2947 : }
2948 : ELSE
2949 : {
2950 1339 : if ( st_fx->bws_cnt1 > 0 )
2951 : {
2952 0 : GainFrame = Mult_32_16( GainFrame, i_mult( st_fx->bws_cnt1, 819 ) ); /*Q18*/
2953 : }
2954 1339 : IF( GE_16( st_fx->nbLostCmpt, 1 ) )
2955 : {
2956 0 : ener = s_max( 1, ener );
2957 0 : exp_ener = norm_s( ener );
2958 0 : tmp = shl( ener, exp_ener ); /*Q(3+exp)*/
2959 0 : inv_ener = div_s( 16384, tmp ); /*Q(15+14-3-exp)*/
2960 0 : prev_ener_ratio_fx = L_shr_sat( L_mult0( st_fx->prev_ener_shb_fx, inv_ener ), sub( 9, exp_ener ) ); /*Q: 1+26-exp-9+exp = 18 */
2961 : }
2962 :
2963 1339 : IF( EQ_16( st_fx->nbLostCmpt, 1 ) )
2964 : {
2965 0 : test();
2966 0 : test();
2967 0 : test();
2968 0 : test();
2969 0 : test();
2970 0 : test();
2971 0 : test();
2972 0 : test();
2973 0 : test();
2974 0 : test();
2975 0 : IF( ( st_fx->clas_dec != UNVOICED_CLAS ) && NE_16( st_fx->clas_dec, UNVOICED_TRANSITION ) && LT_16( hBWE_TD->tilt_swb_fec_fx, 16384 ) &&
2976 : ( ( GT_32( st_fx->enerLL_fx, L_shr( st_fx->prev_enerLL_fx, 1 ) ) && LT_32( L_shr( st_fx->enerLL_fx, 1 ), st_fx->prev_enerLL_fx ) ) || ( GT_32( st_fx->enerLH_fx, L_shr( st_fx->prev_enerLH_fx, 1 ) ) && LT_32( L_shr( st_fx->enerLH_fx, 1 ), st_fx->prev_enerLH_fx ) ) ) )
2977 : {
2978 0 : IF( GT_32( L_shr( prev_ener_ratio_fx, 2 ), GainFrame ) ) /*18*/
2979 : {
2980 0 : GainFrame = L_add( Mult_32_16( prev_ener_ratio_fx, 13107 ), Mult_32_16( GainFrame, 19661 ) ); /*18*/
2981 : }
2982 0 : ELSE IF( GT_32( L_shr( prev_ener_ratio_fx, 1 ), GainFrame ) )
2983 : {
2984 0 : GainFrame = L_add( Mult_32_16( prev_ener_ratio_fx, 26214 ), Mult_32_16( GainFrame, 6554 ) );
2985 : }
2986 : ELSE
2987 : {
2988 0 : GainFrame = L_add( Mult_32_16( prev_ener_ratio_fx, 6554 ), Mult_32_16( GainFrame, 26214 ) );
2989 : }
2990 :
2991 0 : test();
2992 0 : IF( GT_16( tilt_swb_fec, hBWE_TD->tilt_swb_fec_fx ) && ( hBWE_TD->tilt_swb_fec_fx > 0 ) )
2993 : {
2994 0 : exp = norm_s( hBWE_TD->tilt_swb_fec_fx );
2995 0 : tmp = shl( hBWE_TD->tilt_swb_fec_fx, exp ); /*Q(11+exp)*/
2996 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-11-exp)*/
2997 0 : tmp = extract_h( L_shl( L_mult0( tmp, st_fx->tilt_wb_fx ), sub( exp, 1 ) ) ); /*18 -exp +11 + exp -1 -16 =12; */
2998 0 : GainFrame = L_shl( Mult_32_16( GainFrame, s_min( tmp, 20480 ) ), 3 ); /*Q18 = 18 +12 -15 +3 */
2999 : }
3000 : }
3001 0 : ELSE IF( ( ( st_fx->clas_dec != UNVOICED_CLAS ) || GT_16( hBWE_TD->tilt_swb_fec_fx, 16384 ) ) && GT_32( L_shr( prev_ener_ratio_fx, 2 ), GainFrame ) &&
3002 : ( GT_32( st_fx->enerLL_fx, L_shr( st_fx->prev_enerLL_fx, 1 ) ) || GT_32( st_fx->enerLH_fx, L_shr( st_fx->prev_enerLH_fx, 1 ) ) ) )
3003 : {
3004 0 : GainFrame = L_add( Mult_32_16( prev_ener_ratio_fx, 6554 ), Mult_32_16( GainFrame, 26214 ) );
3005 : }
3006 : }
3007 1339 : ELSE IF( GT_16( st_fx->nbLostCmpt, 1 ) )
3008 : {
3009 0 : test();
3010 0 : test();
3011 0 : test();
3012 0 : test();
3013 0 : test();
3014 0 : test();
3015 0 : test();
3016 0 : test();
3017 0 : IF( GT_32( L_shr( prev_ener_ratio_fx, 2 ), GainFrame ) && ( ( EQ_16( st_fx->codec_mode, MODE1 ) && GT_32( st_fx->enerLL_fx, st_fx->prev_enerLL_fx ) && GT_32( st_fx->enerLH_fx, st_fx->prev_enerLH_fx ) ) || EQ_16( st_fx->codec_mode, MODE2 ) ) )
3018 : {
3019 0 : test();
3020 0 : IF( GT_16( tilt_swb_fec, 20480 ) && GT_16( hBWE_TD->tilt_swb_fec_fx, 20480 ) )
3021 : {
3022 0 : GainFrame = L_min( L_add( Mult_32_16( prev_ener_ratio_fx, 26214 ), Mult_32_16( GainFrame, 6554 ) ), L_shl( Mult_32_16( GainFrame, 16384 ), 3 ) ); /*Q18*/
3023 : }
3024 : ELSE
3025 : {
3026 0 : GainFrame = L_min( L_add( Mult_32_16( prev_ener_ratio_fx, 16384 ), Mult_32_16( GainFrame, 16384 ) ), L_shl( Mult_32_16( GainFrame, 16384 ), 3 ) ); /*Q18*/
3027 : }
3028 : }
3029 0 : ELSE IF( GT_32( prev_ener_ratio_fx, GainFrame ) && ( ( EQ_16( st_fx->codec_mode, MODE1 ) && GT_32( st_fx->enerLL_fx, st_fx->prev_enerLL_fx ) && GT_32( st_fx->enerLH_fx, st_fx->prev_enerLH_fx ) ) || EQ_16( st_fx->codec_mode, MODE2 ) ) )
3030 : {
3031 0 : test();
3032 0 : IF( GT_16( tilt_swb_fec, 20480 ) && GT_16( hBWE_TD->tilt_swb_fec_fx, 20480 ) )
3033 : {
3034 0 : GainFrame = L_add( Mult_32_16( prev_ener_ratio_fx, 16384 ), Mult_32_16( GainFrame, 16384 ) );
3035 : }
3036 : ELSE
3037 : {
3038 0 : GainFrame = L_add( Mult_32_16( prev_ener_ratio_fx, 6554 ), Mult_32_16( GainFrame, 26214 ) );
3039 : }
3040 : }
3041 : }
3042 : }
3043 1339 : st_fx->prev_fractive = is_fractive;
3044 1339 : move16();
3045 :
3046 : /* Adjust the subframe and frame gain of the synthesized shb signal */
3047 1339 : IF( EQ_16( st_fx->L_frame, L_FRAME ) )
3048 : {
3049 : /* pitch = 0.25f*sum_s(pitch_buf, 4); */
3050 735 : L_tmp = L_mult( pitch_buf[0], 8192 );
3051 2940 : FOR( i = 1; i < NB_SUBFR; i++ )
3052 : {
3053 2205 : L_tmp = L_mac( L_tmp, pitch_buf[i], 8192 ); /* pitch_buf in Q6 x 0.25 in Q15 */
3054 : }
3055 735 : pitch_fx = round_fx( L_tmp ); /* Q6 */
3056 : }
3057 : ELSE
3058 : {
3059 : /* pitch_fx = 0.2f*sum_s(pitch_buf, 5); */
3060 604 : L_tmp = L_mult( pitch_buf[0], 6554 );
3061 3020 : FOR( i = 1; i < NB_SUBFR16k; i++ )
3062 : {
3063 2416 : L_tmp = L_mac( L_tmp, pitch_buf[i], 6554 ); /* pitch_buf in Q6 x 0.2 in Q15 */
3064 : }
3065 604 : pitch_fx = round_fx( L_tmp ); /* Q6 */
3066 : }
3067 :
3068 1339 : test();
3069 1339 : test();
3070 1339 : test();
3071 1339 : test();
3072 1339 : test();
3073 1339 : test();
3074 1339 : test();
3075 1339 : test();
3076 1339 : test();
3077 1339 : test();
3078 1339 : IF( ( ( GE_32( st_fx->total_brate, ACELP_24k40 ) && EQ_16( st_fx->prev_coder_type, coder_type ) && NE_16( coder_type, UNVOICED ) ) || ( LE_32( st_fx->total_brate, ACELP_16k40 ) && ( EQ_16( st_fx->prev_coder_type, coder_type ) || ( EQ_16( st_fx->prev_coder_type, VOICED ) && EQ_16( coder_type, GENERIC ) ) || ( EQ_16( st_fx->prev_coder_type, GENERIC ) && EQ_16( coder_type, VOICED ) ) ) ) ) && GT_16( pitch_fx, 4480 /*70 in Q6*/ ) && LT_16( st_fx->extl, FB_TBE ) )
3079 : {
3080 2660 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
3081 : {
3082 2128 : GainShape_tmp[i] = GainShape[i * 4]; /* Q15 */
3083 2128 : move16();
3084 : }
3085 :
3086 2660 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
3087 : {
3088 : /* if( ener_tmp_fx[i]*GainShape_tmp_fx[i] > st_fx->prev_ener_fx*st_fx->prev_GainShape_fx ) */
3089 2128 : L_tmp1 = Mult_32_16( ener_tmp[i], GainShape_tmp[i] ); /* (2*Q_bwe_exc) */
3090 2128 : L_tmp2 = Mult_32_16( hBWE_TD->prev_ener_fx, hBWE_TD->prev_GainShape_fx ); /* (2*st_fx->prev_ener_fx_Q) */
3091 2128 : tmp = sub( shl( Q_bwe_exc, 1 ), shl( hBWE_TD->prev_ener_fx_Q, 1 ) );
3092 2128 : L_tmp2 = L_shl_sat( L_tmp2, tmp ); /* new Q = (2*Q_bwe_exc) */
3093 2128 : IF( GT_32( L_tmp1, L_tmp2 ) )
3094 : {
3095 : /*GainShape_tmp_fx[i] = 0.5f*(L_tmp2/ener_tmp_fx[i] + GainShape_tmp_fx[i]);*/
3096 : /* tmp = L_tmp2/ener_tmp_fx[i]*/
3097 1225 : L_tmp = L_tmp2;
3098 1225 : move32();
3099 1225 : if ( L_tmp2 < 0 )
3100 : {
3101 0 : L_tmp = L_negate( L_tmp2 );
3102 : }
3103 :
3104 1225 : expb = norm_l( L_tmp );
3105 1225 : fracb = round_fx_sat( L_shl_sat( L_tmp, expb ) );
3106 1225 : expb = sub( 30, expb ); /* - (2*Q_bwe_exc_ext); */
3107 :
3108 1225 : expa = norm_l( ener_tmp[i] );
3109 1225 : fraca = extract_h( L_shl( ener_tmp[i], expa ) );
3110 1225 : expa = sub( 30, expa );
3111 :
3112 1225 : scale = shr( sub( fraca, fracb ), 15 );
3113 1225 : fracb = shl( fracb, scale );
3114 1225 : expb = sub( expb, scale );
3115 :
3116 1225 : tmp = div_s( fracb, fraca );
3117 1225 : exp = sub( sub( expb, expa ), 1 );
3118 1225 : tmp = shl( tmp, exp );
3119 1225 : GainShape_tmp[i] = add( tmp, shr( GainShape_tmp[i], 1 ) ); /* Q15 */
3120 1225 : move16();
3121 : }
3122 :
3123 2128 : hBWE_TD->prev_ener_fx = ener_tmp[i];
3124 2128 : move32();
3125 2128 : hBWE_TD->prev_GainShape_fx = GainShape_tmp[i];
3126 2128 : move16();
3127 2128 : hBWE_TD->prev_ener_fx_Q = Q_bwe_exc;
3128 2128 : move16();
3129 : }
3130 9044 : FOR( i = 0; i < NUM_SHB_SUBFR; i++ )
3131 : {
3132 8512 : Word16 idx = 0;
3133 8512 : move16();
3134 8512 : IF( i != 0 )
3135 : {
3136 7980 : idx = idiv1616( i_mult( i, NUM_SHB_SUBGAINS ), NUM_SHB_SUBFR );
3137 : }
3138 8512 : GainShape[i] = GainShape_tmp[idx];
3139 8512 : move16();
3140 : }
3141 : }
3142 : ELSE
3143 : {
3144 807 : hBWE_TD->prev_ener_fx_Q = Q_bwe_exc;
3145 807 : move16();
3146 : }
3147 :
3148 :
3149 : /* Back up the Q_bwe_exc associated with shaped_shb_excitation for the next frame*/
3150 1339 : hBWE_TD->prev_Q_bwe_syn = Q_bwe_exc;
3151 1339 : move16();
3152 :
3153 : /* Scale the shaped excitation */
3154 1339 : ScaleShapedSHB_fx( SHB_OVERLAP_LEN,
3155 : shaped_shb_excitation, /* i/o: Q_bwe_exc */
3156 1339 : hBWE_TD->syn_overlap_fx,
3157 : GainShape, /* Q15 */
3158 : GainFrame, /* Q18 */
3159 : window_shb_fx,
3160 : subwin_shb_fx,
3161 1339 : &Q_bwe_exc, &Qx, n_mem3, hBWE_TD->prev_Q_bwe_syn2 );
3162 :
3163 1339 : max = 0;
3164 1339 : move16();
3165 429819 : FOR( i = 0; i < L_FRAME16k; i++ )
3166 : {
3167 428480 : max = s_max( max, shaped_shb_excitation[i] ); /* Q0 */
3168 : }
3169 :
3170 1339 : IF( max == 0 )
3171 : {
3172 0 : curr_frame_pow = 0;
3173 0 : move16();
3174 0 : n = 0;
3175 0 : move16();
3176 : }
3177 : ELSE
3178 : {
3179 1339 : n = norm_s( max );
3180 1339 : max = 0;
3181 1339 : move16();
3182 429819 : FOR( i = 0; i < L_FRAME16k; i++ )
3183 : {
3184 428480 : shaped_shb_excitation_frac[i] = shl_sat( shaped_shb_excitation[i], n ); /*Q_bwe_exc+n*/
3185 428480 : move16();
3186 : }
3187 :
3188 1339 : curr_frame_pow = 0;
3189 1339 : move32();
3190 429819 : FOR( i = 0; i < L_FRAME16k; i++ )
3191 : {
3192 428480 : L_tmp = L_mult0( shaped_shb_excitation_frac[i], shaped_shb_excitation_frac[i] ); /*2*(Q_bwe_exc+n)*/
3193 428480 : curr_frame_pow = L_add( curr_frame_pow, L_shr( L_tmp, 9 ) ); /*2*(Q_bwe_exc+n)-9*/
3194 : }
3195 : }
3196 1339 : curr_frame_pow_exp = sub( shl( add( Q_bwe_exc, n ), 1 ), 9 );
3197 :
3198 :
3199 1339 : tmp = sub( hBWE_TD->prev_frame_pow_exp, curr_frame_pow_exp );
3200 1339 : IF( tmp > 0 ) /* shifting prev */
3201 : {
3202 8 : IF( GT_16( tmp, 32 ) )
3203 : {
3204 0 : hBWE_TD->prev_frame_pow_exp = add( curr_frame_pow_exp, 32 );
3205 0 : move16();
3206 0 : tmp = 32;
3207 0 : move16();
3208 : }
3209 8 : hBWE_TD->prev_swb_bwe_frame_pow_fx = L_shr( hBWE_TD->prev_swb_bwe_frame_pow_fx, tmp );
3210 8 : move32();
3211 8 : hBWE_TD->prev_frame_pow_exp = curr_frame_pow_exp;
3212 8 : move16();
3213 : }
3214 : ELSE /* shifting curr */
3215 : {
3216 1331 : IF( LT_16( tmp, -32 ) )
3217 : {
3218 0 : curr_frame_pow_exp = sub( hBWE_TD->prev_frame_pow_exp, 32 );
3219 0 : tmp = -32;
3220 0 : move16();
3221 : }
3222 1331 : curr_frame_pow = L_shr( curr_frame_pow, -tmp );
3223 1331 : curr_frame_pow_exp = hBWE_TD->prev_frame_pow_exp;
3224 1331 : move16();
3225 : }
3226 1339 : test();
3227 1339 : IF( !st_fx->bfi && st_fx->prev_bfi )
3228 : {
3229 0 : L_tmp = L_shr( curr_frame_pow, 4 );
3230 0 : L_tmp = Mult_32_16( L_tmp, 17476 );
3231 :
3232 0 : test();
3233 0 : test();
3234 0 : IF( ( GT_32( L_shr( curr_frame_pow, 1 ), hBWE_TD->prev_swb_bwe_frame_pow_fx ) ) &&
3235 : ( GT_32( hBWE_TD->prev_swb_bwe_frame_pow_fx, L_tmp ) ) && EQ_16( st_fx->prev_coder_type, UNVOICED ) )
3236 : {
3237 0 : L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow, curr_frame_pow_exp, &exp );
3238 0 : scale = round_fx( L_shl( L_tmp, exp ) ); /*Q15*/
3239 :
3240 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
3241 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
3242 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
3243 0 : temp = round_fx( L_shl( L_tmp, exp ) ); /*Q15*/
3244 : }
3245 : ELSE
3246 : {
3247 0 : scale = temp = 32767;
3248 0 : move16(); /*Q15*/
3249 : }
3250 0 : FOR( j = 0; j < 8; j++ )
3251 : {
3252 0 : GainShape[2 * j] = mult_r( GainShape[2 * j], scale );
3253 0 : move16();
3254 0 : GainShape[add( 2 * j, 1 )] = mult_r( GainShape[add( 2 * j, 1 )], scale );
3255 0 : move16();
3256 0 : FOR( i = 0; i < L_FRAME16k / 8; i++ )
3257 : {
3258 0 : shaped_shb_excitation[add( i, j * ( L_FRAME16k / 8 ) )] = mult_r( shaped_shb_excitation[add( i, j * ( L_FRAME16k / 8 ) )], scale );
3259 0 : move16();
3260 : }
3261 :
3262 0 : IF( temp > 0 )
3263 : {
3264 : /* scale <= temp, due to scale = sqrt( st->prev_swb_bwe_frame_pow_fx/curr_frame_pow ), temp = sqrt( scale, 1.f/8.f )
3265 : and curr_frame_pow > st->prev_swb_bwe_frame_pow_fx -> scale <= 1.0, sqrt(scale, 1.f/8.f) >= scale */
3266 0 : IF( LT_16( scale, temp ) )
3267 : {
3268 0 : scale = div_s( scale, temp );
3269 : }
3270 : ELSE
3271 : {
3272 0 : scale = 32767;
3273 0 : move16();
3274 : }
3275 : }
3276 : ELSE
3277 : {
3278 0 : scale = 0;
3279 0 : move16();
3280 : }
3281 : }
3282 : }
3283 :
3284 : /* adjust the FEC frame energy */
3285 1339 : IF( st_fx->bfi )
3286 : {
3287 0 : scale = temp = 4096;
3288 0 : move16(); /*Q12*/
3289 :
3290 0 : IF( EQ_16( st_fx->nbLostCmpt, 1 ) )
3291 : {
3292 0 : test();
3293 0 : test();
3294 0 : test();
3295 0 : test();
3296 0 : test();
3297 0 : test();
3298 0 : test();
3299 0 : test();
3300 0 : IF( GT_32( curr_frame_pow, hBWE_TD->prev_swb_bwe_frame_pow_fx ) &&
3301 : NE_16( st_fx->prev_coder_type, UNVOICED ) &&
3302 : ( st_fx->last_good != UNVOICED_CLAS ) )
3303 : {
3304 0 : L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow, curr_frame_pow_exp, &exp ); /*31 - exp*/
3305 0 : scale = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
3306 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
3307 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
3308 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
3309 0 : temp = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
3310 : }
3311 0 : ELSE IF( LT_32( curr_frame_pow, L_shr( hBWE_TD->prev_swb_bwe_frame_pow_fx, 1 ) ) && EQ_16( st_fx->nbLostCmpt, 1 ) &&
3312 : ( GT_32( st_fx->enerLL_fx, L_shr( st_fx->prev_enerLL_fx, 1 ) ) || GT_32( st_fx->enerLH_fx, L_shr( st_fx->prev_enerLH_fx, 1 ) ) ) &&
3313 : ( EQ_16( st_fx->prev_coder_type, UNVOICED ) || ( st_fx->last_good == UNVOICED_CLAS ) || GT_16( hBWE_TD->tilt_swb_fec_fx, 10240 ) ) )
3314 : {
3315 0 : L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow, curr_frame_pow_exp, &exp );
3316 0 : scale = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
3317 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
3318 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
3319 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
3320 0 : temp = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
3321 : }
3322 : }
3323 0 : ELSE IF( GT_16( st_fx->nbLostCmpt, 1 ) )
3324 : {
3325 0 : test();
3326 0 : test();
3327 0 : test();
3328 0 : test();
3329 0 : test();
3330 0 : IF( GT_32( curr_frame_pow, hBWE_TD->prev_swb_bwe_frame_pow_fx ) )
3331 : {
3332 0 : L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow, curr_frame_pow_exp, &exp );
3333 0 : scale = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
3334 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
3335 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
3336 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
3337 0 : temp = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
3338 : }
3339 0 : ELSE IF( LT_32( curr_frame_pow, L_shr( hBWE_TD->prev_swb_bwe_frame_pow_fx, 1 ) ) &&
3340 : ( GT_32( st_fx->enerLL_fx, L_shr( st_fx->prev_enerLL_fx, 1 ) ) || GT_32( st_fx->enerLH_fx, L_shr( st_fx->prev_enerLH_fx, 1 ) ) ) &&
3341 : ( EQ_16( st_fx->prev_coder_type, UNVOICED ) || ( st_fx->last_good == UNVOICED_CLAS ) || GT_16( hBWE_TD->tilt_swb_fec_fx, 10240 ) ) )
3342 : {
3343 0 : L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow, curr_frame_pow_exp, &exp );
3344 0 : L_tmp = L_min( L_tmp, L_shl_sat( 2, sub( 31, exp ) ) ); /*31 - exp*/
3345 0 : scale = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
3346 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
3347 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
3348 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
3349 0 : temp = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
3350 : }
3351 : }
3352 0 : FOR( j = 0; j < 8; j++ )
3353 : {
3354 0 : GainShape[2 * j] = shl_sat( mult_r( GainShape[2 * j], scale ), 3 );
3355 0 : move16(); /* 15 +12 +3-15 =15*/
3356 0 : GainShape[add( 2 * j, 1 )] = shl_sat( mult_r( GainShape[add( 2 * j, 1 )], scale ), 3 );
3357 0 : move16();
3358 0 : FOR( i = 0; i < 40; i++ )
3359 : {
3360 0 : shaped_shb_excitation[add( i, j * 40 )] = shl_sat( mult_r( shaped_shb_excitation[add( i, j * 40 )], scale ), 3 );
3361 0 : move16(); /* Q_bwe_exc +12+3 -15 =Q_bwe_exc*/
3362 : }
3363 :
3364 0 : IF( temp > 0 )
3365 : {
3366 0 : IF( LT_16( scale, temp ) )
3367 : {
3368 0 : scale = shr( div_s( scale, temp ), 3 );
3369 : }
3370 : ELSE
3371 : {
3372 0 : tmp1 = sub( norm_s( scale ), 1 );
3373 0 : tmp2 = norm_s( temp );
3374 0 : scale = div_s( shl( scale, tmp1 ), shl( temp, tmp2 ) );
3375 0 : scale = shr( scale, add( sub( tmp1, tmp2 ), 3 ) );
3376 : }
3377 : }
3378 : ELSE
3379 : {
3380 0 : scale = 0;
3381 0 : move16();
3382 : }
3383 : }
3384 : }
3385 :
3386 1339 : hBWE_TD->prev_swb_bwe_frame_pow_fx = curr_frame_pow;
3387 1339 : move32();
3388 1339 : hBWE_TD->prev_frame_pow_exp = curr_frame_pow_exp;
3389 1339 : move16();
3390 :
3391 : {
3392 1339 : Word64 prev_ener_shb64 = 0;
3393 1339 : move64();
3394 429819 : FOR( i = 0; i < L_FRAME16k; i++ )
3395 : {
3396 428480 : prev_ener_shb64 = W_mac0_16_16( prev_ener_shb64, shaped_shb_excitation[i], shaped_shb_excitation[i] ); /* Q0 */
3397 : }
3398 1339 : L_prev_ener_shb = W_sat_l( prev_ener_shb64 );
3399 : }
3400 :
3401 : /* st->prev_ener_shb = sqrt(st->prev_ener_shb/L_FRAME16k) */
3402 1339 : L_prev_ener_shb = Mult_32_16( L_prev_ener_shb, 26214 ); /* 2*Q_bwe_exc_mod+8; 26214=(1/L_FRAME16k) in Q23 */
3403 1339 : st_fx->prev_ener_shb_fx = 0;
3404 1339 : move16();
3405 :
3406 1339 : IF( L_prev_ener_shb != 0 )
3407 : {
3408 1339 : exp = norm_l( L_prev_ener_shb );
3409 1339 : tmp = extract_h( L_shl( L_prev_ener_shb, exp ) );
3410 1339 : exp = sub( exp, sub( 30, ( add( i_mult( 2, Q_bwe_exc ), 8 ) ) ) );
3411 :
3412 1339 : tmp = div_s( 16384, tmp );
3413 1339 : L_tmp = L_deposit_h( tmp );
3414 1339 : L_tmp = Isqrt_lc( L_tmp, &exp );
3415 1339 : st_fx->prev_ener_shb_fx = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 14 ) ) ); /* Q1 */
3416 1339 : move16();
3417 : }
3418 : /* st->prev_SWB_fenv[i] = sqrt(curr_frame_pow/L_FRAME16k); */
3419 1339 : L_tmp = Mult_32_16( curr_frame_pow, 26214 ); /* curr_frame_pow_exp+8; 26214=(1/L_FRAME16k) in Q23 */
3420 1339 : tmp = 0;
3421 1339 : move16();
3422 1339 : IF( L_tmp != 0 )
3423 : {
3424 1300 : exp = norm_l( L_tmp );
3425 1300 : tmp = extract_h( L_shl( L_tmp, exp ) );
3426 1300 : exp = sub( exp, sub( 30, add( curr_frame_pow_exp, 8 ) ) );
3427 :
3428 1300 : tmp = div_s( 16384, tmp );
3429 1300 : L_tmp = L_deposit_h( tmp );
3430 1300 : L_tmp = Isqrt_lc( L_tmp, &exp );
3431 1300 : tmp = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 14 ) ) ); /* Q1 */
3432 : }
3433 1339 : set16_fx( st_fx->hBWE_FD->prev_SWB_fenv_fx, tmp, SWB_FENV ); /* Q1 */
3434 :
3435 : /* rescale the memories if Q_bwe_exc is different from previous frame */
3436 1339 : sc = sub( Q_bwe_exc, hBWE_TD->prev_Q_bwe_syn2 );
3437 1339 : IF( sc != 0 )
3438 : {
3439 6125 : FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
3440 : {
3441 5250 : hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx[i] = shl( hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx[i], sc );
3442 5250 : move16();
3443 : }
3444 :
3445 875 : IF( EQ_16( st_fx->L_frame, L_FRAME ) )
3446 : {
3447 10142 : FOR( i = 0; i < HILBERT_MEM_SIZE; i++ )
3448 : {
3449 9681 : hBWE_TD->genSHBsynth_Hilbert_Mem_fx[i] = L_shl( hBWE_TD->genSHBsynth_Hilbert_Mem_fx[i], sc );
3450 9681 : move32();
3451 : }
3452 : }
3453 875 : IF( EQ_32( st_fx->output_Fs, 48000 ) )
3454 : {
3455 414 : Scale_sig( hBWE_TD->int_3_over_2_tbemem_dec_fx, INTERP_3_2_MEM_LEN, sc );
3456 : }
3457 875 : IF( EQ_32( st_fx->output_Fs, 16000 ) )
3458 : {
3459 0 : Scale_sig( hBWE_TD->mem_resamp_HB_32k_fx, 2 * ALLPASSSECTIONS_STEEP + 1, sc );
3460 : }
3461 : }
3462 : /* i: shaped_shb_excitation[320] in (Q_bwe_exc) */
3463 : /* i/o: st_fx->genSHBsynth_Hilbert_Mem_fx in (Q_bwe_exc) */
3464 : /* i/o: st_fx->genSHBsynth_state_lsyn_filt_shb_local_fx in (Q_bwe_exc) */
3465 : /* o: error in (Qx) */
3466 1339 : GenSHBSynth_fx( shaped_shb_excitation, error, hBWE_TD->genSHBsynth_Hilbert_Mem_fx,
3467 1339 : hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx, st_fx->L_frame, &( hBWE_TD->syn_dm_phase ) );
3468 :
3469 1339 : Copy( error + L_FRAME32k - L_SHB_TRANSITION_LENGTH, hBWE_TD->old_tbe_synth_fx, L_SHB_TRANSITION_LENGTH );
3470 :
3471 : /* resample SHB synthesis (if needed) and scale down */
3472 1339 : synth_scale_fx = 32767;
3473 1339 : move16(); /* 1.0 in Q15 */
3474 1339 : if ( EQ_16( st_fx->codec_mode, MODE1 ) )
3475 : {
3476 735 : synth_scale_fx = 29491;
3477 735 : move16(); /* 0.9 in Q15 */
3478 : }
3479 :
3480 1339 : IF( EQ_32( st_fx->output_Fs, 48000 ) )
3481 : {
3482 604 : IF( EQ_32( st_fx->extl, FB_TBE ) )
3483 : {
3484 0 : tmp = norm_l( GainFrame );
3485 0 : if ( GainFrame == 0 )
3486 : {
3487 0 : tmp = 31;
3488 0 : move16();
3489 : }
3490 0 : L_tmp = L_shl( GainFrame, tmp ); /* 18 + tmp */
3491 :
3492 0 : tmp1 = 0;
3493 0 : move16();
3494 0 : FOR( i = 0; i < L_FRAME16k; i++ )
3495 : {
3496 0 : Word16 idx = 0;
3497 0 : IF( i != 0 )
3498 : {
3499 0 : idx = idiv1616( i_mult( NUM_SHB_SUBFR, i ), L_FRAME16k );
3500 : }
3501 0 : L_tmp1 = Mult_32_16( L_tmp, GainShape[idx] ); /* Q : 18 + tmp +15 -15*/
3502 0 : White_exc16k[i] = round_fx( Mult_32_16( L_tmp1, White_exc16k[i] ) ); /* 18 + tmp +*Q_white_exc -15 -16 */
3503 0 : move16();
3504 0 : tmp1 = s_max( tmp1, abs_s( White_exc16k[i] ) );
3505 : }
3506 :
3507 0 : *Q_white_exc = sub( add( *Q_white_exc, tmp ), 13 ); /* *Q_white_exc + 18 + tmp -15 -16 */
3508 0 : move16();
3509 0 : tmp = norm_s( tmp1 );
3510 0 : if ( tmp1 == 0 )
3511 : {
3512 0 : tmp = 15;
3513 0 : move16();
3514 : }
3515 :
3516 0 : FOR( i = 0; i < L_FRAME16k; i++ )
3517 : {
3518 0 : White_exc16k[i] = shl( White_exc16k[i], sub( tmp, 1 ) );
3519 0 : move16();
3520 : }
3521 0 : *Q_white_exc = sub( add( *Q_white_exc, tmp ), 1 );
3522 0 : move16();
3523 : }
3524 :
3525 604 : IF( NE_16( synth_scale_fx, 32767 ) ) /* 1.0 in Q15 */
3526 : {
3527 0 : FOR( i = 0; i < L_FRAME32k; i++ )
3528 : {
3529 0 : error[i] = mult_r( error[i], synth_scale_fx );
3530 0 : move16();
3531 : }
3532 : }
3533 :
3534 604 : interpolate_3_over_2_allpass_fx( error, L_FRAME32k, synth, hBWE_TD->int_3_over_2_tbemem_dec_fx, allpass_poles_3_ov_2 );
3535 : }
3536 735 : ELSE IF( EQ_32( st_fx->output_Fs, 32000 ) )
3537 : {
3538 735 : IF( NE_16( synth_scale_fx, 32767 ) ) /* 1.0 in Q15 */
3539 : {
3540 471135 : FOR( i = 0; i < L_FRAME32k; i++ )
3541 : {
3542 470400 : synth[i] = mult_r( synth_scale_fx, error[i] );
3543 470400 : move16(); /*Qx*/
3544 : }
3545 : }
3546 : ELSE
3547 : {
3548 0 : Copy( error, synth, L_FRAME32k );
3549 : }
3550 : }
3551 0 : ELSE IF( EQ_32( st_fx->output_Fs, 16000 ) )
3552 : {
3553 0 : IF( NE_16( synth_scale_fx, 32767 ) ) /* 1.0 in Q15 */
3554 : {
3555 0 : FOR( i = 0; i < L_FRAME32k; i++ )
3556 : {
3557 0 : error[i] = mult_r( error[i], synth_scale_fx );
3558 0 : move16();
3559 : }
3560 : }
3561 :
3562 0 : Decimate_allpass_steep_fx( error, hBWE_TD->mem_resamp_HB_32k_fx, L_FRAME32k, synth );
3563 : }
3564 :
3565 : /* Update previous frame parameters for FEC */
3566 1339 : Copy( lsf_shb, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER );
3567 1339 : IF( EQ_16( st_fx->codec_mode, MODE1 ) )
3568 : {
3569 735 : hBWE_TD->GainFrame_prevfrm_fx = GainFrame;
3570 735 : move16(); /*Q18*/
3571 735 : hBWE_TD->tilt_swb_fec_fx = tilt_swb_fec;
3572 735 : move16();
3573 :
3574 735 : if ( !st_fx->bfi )
3575 : {
3576 735 : hBWE_TD->GainAttn_fx = 32767; /*Q15*/
3577 735 : move16();
3578 : }
3579 : }
3580 : ELSE
3581 : {
3582 604 : IF( !st_fx->bfi )
3583 : {
3584 604 : hBWE_TD->GainFrame_prevfrm_fx = GainFrame;
3585 604 : move16(); /*Q18*/
3586 604 : hBWE_TD->tilt_swb_fec_fx = tilt_swb_fec;
3587 604 : move16();
3588 604 : hBWE_TD->GainAttn_fx = 32767; /*Q15*/
3589 604 : move16();
3590 : }
3591 : }
3592 :
3593 1339 : hBWE_TD->prev_ener_fx = ener_tmp[NUM_SHB_SUBGAINS - 1];
3594 1339 : move32();
3595 1339 : hBWE_TD->prev_GainShape_fx = GainShape[NUM_SHB_SUBFR - 1];
3596 1339 : move32();
3597 1339 : *Q_synth = Q_bwe_exc;
3598 1339 : move16();
3599 1339 : hBWE_TD->prev_Q_bwe_syn2 = Q_bwe_exc;
3600 1339 : move16();
3601 1339 : hBWE_TD->prev_Qx = Q_bwe_exc;
3602 1339 : move16();
3603 :
3604 1339 : return;
3605 : }
3606 :
3607 0 : static void gradientGainShape(
3608 : Decoder_State *st_fx,
3609 : Word16 *GainShape,
3610 : Word32 *GainFrame )
3611 : {
3612 : Word16 i, j, tmp;
3613 : Word16 GainShapeTemp[NUM_SHB_SUBFR / 4];
3614 : Word16 GainGrad0[3];
3615 : Word16 GainGrad1[3];
3616 : Word16 GainGradFEC[4];
3617 : TD_BWE_DEC_HANDLE hBWE_TD;
3618 0 : hBWE_TD = st_fx->hBWE_TD;
3619 :
3620 : /* the previous frame gainshape gradient and the gainshape gradient pattern for the current frame */
3621 0 : FOR( j = 0; j < 3; j++ )
3622 : {
3623 0 : GainGrad0[j] = sub( shr( hBWE_TD->GainShape_Delay_fx[j + 1], 1 ), shr( hBWE_TD->GainShape_Delay_fx[j], 1 ) );
3624 0 : move16(); /* Q14 */
3625 0 : GainGrad1[j] = sub( shr( hBWE_TD->GainShape_Delay_fx[j + 5], 1 ), shr( hBWE_TD->GainShape_Delay_fx[j + 4], 1 ) );
3626 0 : move16(); /* Q14 */
3627 0 : GainGradFEC[j + 1] = add( mult_r( GainGrad0[j], 13107 ), mult_r( GainGrad1[j], 19660 ) );
3628 0 : move16(); /* Q14 */
3629 : }
3630 :
3631 : /* gradient for the first gainshape */
3632 0 : test();
3633 0 : test();
3634 0 : test();
3635 0 : IF( ( ( GT_16( shr( GainGrad1[2], 1 ), GainGrad1[1] ) ) && ( GT_16( shr( GainGrad1[1], 1 ), GainGrad1[0] ) ) ) ||
3636 : ( ( LT_16( shr( GainGrad1[2], 1 ), GainGrad1[1] ) ) && ( LT_16( shr( GainGrad1[1], 1 ), GainGrad1[0] ) ) ) )
3637 : {
3638 0 : GainGradFEC[0] = add( mult_r( GainGrad1[1], 3277 ), mult_r( GainGrad1[2], 29490 ) );
3639 0 : move16(); /* Q14 */
3640 : }
3641 : ELSE
3642 : {
3643 0 : GainGradFEC[0] = add( mult_r( GainGrad1[0], 6553 ), mult_r( GainGrad1[1], 9830 ) );
3644 0 : move16();
3645 0 : GainGradFEC[0] = add( GainGradFEC[0], mult_r( GainGrad1[2], 16384 ) );
3646 0 : move16(); /* Q14 */
3647 : }
3648 :
3649 : /* get the first gainshape template */
3650 0 : test();
3651 0 : test();
3652 0 : IF( ( EQ_16( st_fx->prev_coder_type, UNVOICED ) || ( st_fx->last_good == UNVOICED_CLAS ) ) && ( GainGradFEC[0] > 0 ) )
3653 : {
3654 0 : GainShapeTemp[0] = add( shr( hBWE_TD->GainShape_Delay_fx[7], 1 ), GainGradFEC[0] );
3655 0 : move16();
3656 : }
3657 0 : ELSE IF( GainGradFEC[0] > 0 )
3658 : {
3659 0 : GainShapeTemp[0] = add( shr( hBWE_TD->GainShape_Delay_fx[7], 1 ), mult_r( GainGradFEC[0], 16384 ) );
3660 0 : move16(); /* Q14 */
3661 : }
3662 : ELSE
3663 : {
3664 0 : GainShapeTemp[0] = shr( hBWE_TD->GainShape_Delay_fx[7], 1 );
3665 0 : move16(); /* Q14 */
3666 : }
3667 :
3668 : /*Get the second the third and the fourth gainshape template*/
3669 :
3670 0 : tmp = shr( GainGrad1[2], 3 ); /* GainGrad1[2]/8 */
3671 0 : tmp = mult_r( tmp, 26214 ); /* 0.8 in Q15 tmp*(8/10) */
3672 :
3673 0 : test();
3674 0 : IF( ( GT_16( tmp, GainGrad1[1] ) ) && ( GainGrad1[1] > 0 ) )
3675 : {
3676 0 : FOR( i = 1; i < NUM_SHB_SUBFR / 4; i++ )
3677 : {
3678 0 : GainShapeTemp[i] = add( GainShapeTemp[i - 1], mult_r( GainGradFEC[i], 26214 ) );
3679 0 : move16(); /* GainShapeTemp[i-1] + 0.8* GainShapeTemp[i] */
3680 0 : GainShapeTemp[i] = s_max( GainShapeTemp[i], 328 /*0.01f Q15*/ );
3681 0 : move16();
3682 : }
3683 : }
3684 : ELSE
3685 : {
3686 0 : test();
3687 0 : IF( ( GT_16( tmp, GainGrad1[1] ) ) && ( GainGrad1[1] < 0 ) )
3688 : {
3689 0 : FOR( i = 1; i < NUM_SHB_SUBFR / 4; i++ )
3690 : {
3691 0 : GainShapeTemp[i] = add( GainShapeTemp[i - 1], mult_r( GainGradFEC[i], 6553 ) );
3692 0 : move16(); /* GainShapeTemp[i-1] + 0.8* GainShapeTemp[i] */
3693 0 : GainShapeTemp[i] = s_max( GainShapeTemp[i], 328 /*0.01f Q15*/ );
3694 0 : move16(); /* Q14 */
3695 : }
3696 : }
3697 : ELSE
3698 : {
3699 0 : FOR( i = 1; i < NUM_SHB_SUBFR / 4; i++ )
3700 : {
3701 0 : GainShapeTemp[i] = add( GainShapeTemp[i - 1], GainGradFEC[i] );
3702 0 : move16();
3703 0 : GainShapeTemp[i] = s_max( GainShapeTemp[i], 328 /*0.01f Q15*/ );
3704 0 : move16();
3705 : }
3706 : }
3707 : }
3708 :
3709 : /* Get the gainshape and gain frame for the current frame*/
3710 0 : test();
3711 0 : test();
3712 0 : test();
3713 0 : IF( ( EQ_16( st_fx->prev_coder_type, UNVOICED ) || ( st_fx->last_good == UNVOICED_CLAS ) ) && EQ_16( st_fx->nbLostCmpt, 1 ) )
3714 : {
3715 0 : FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
3716 : {
3717 0 : FOR( j = 0; j < 4; j++ )
3718 : {
3719 0 : tmp = mult_r( GainShapeTemp[i], 19660 ); /* GainShapeTemp[i]*0.6 */
3720 :
3721 0 : IF( GT_16( 8192, tmp ) )
3722 : {
3723 0 : GainShape[i * 4 + j] = shl( tmp, 2 );
3724 0 : move16(); /* (GainShapeTemp[i]*0.6)>>1 */
3725 : }
3726 : ELSE
3727 : {
3728 0 : GainShape[i * 4 + j] = 32767;
3729 0 : move16(); /* Clipping here to avoid the a huge change of the code due to gain shape change */
3730 : }
3731 : }
3732 : }
3733 0 : hBWE_TD->GainAttn_fx = mult_r( hBWE_TD->GainAttn_fx, 31129 );
3734 0 : move16();
3735 : }
3736 0 : ELSE IF( EQ_16( st_fx->prev_coder_type, UNVOICED ) || ( st_fx->last_good == UNVOICED_CLAS ) )
3737 : {
3738 0 : FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
3739 : {
3740 0 : FOR( j = 0; j < 4; j++ )
3741 : {
3742 0 : IF( LT_16( GainShapeTemp[i], 16384 ) )
3743 : {
3744 0 : GainShape[i * 4 + j] = shl( GainShapeTemp[i], 1 );
3745 0 : move16();
3746 : }
3747 : ELSE
3748 : {
3749 0 : GainShape[i * 4 + j] = 32767;
3750 0 : move16();
3751 : }
3752 : }
3753 : }
3754 0 : hBWE_TD->GainAttn_fx = mult_r( hBWE_TD->GainAttn_fx, 31129 );
3755 0 : move16();
3756 : }
3757 0 : ELSE IF( GT_16( st_fx->nbLostCmpt, 1 ) )
3758 : {
3759 0 : FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
3760 : {
3761 0 : FOR( j = 0; j < 4; j++ )
3762 : {
3763 0 : GainShape[add( i * 4, j )] = GainShapeTemp[i];
3764 0 : move16();
3765 : }
3766 : }
3767 0 : hBWE_TD->GainAttn_fx = mult_r( hBWE_TD->GainAttn_fx, 16384 );
3768 0 : move16();
3769 : }
3770 : ELSE
3771 : {
3772 0 : FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
3773 : {
3774 0 : FOR( j = 0; j < 4; j++ )
3775 : {
3776 0 : IF( LT_16( GainShapeTemp[i], 16384 ) )
3777 : {
3778 0 : GainShape[i * 4 + j] = shl( GainShapeTemp[i], 1 );
3779 0 : move16();
3780 : }
3781 : ELSE
3782 : {
3783 0 : GainShape[i * 4 + j] = 32767;
3784 0 : move16();
3785 : }
3786 : }
3787 : }
3788 0 : hBWE_TD->GainAttn_fx = mult_r( hBWE_TD->GainAttn_fx, 27852 );
3789 0 : move16();
3790 : }
3791 :
3792 0 : *GainFrame = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, hBWE_TD->GainAttn_fx ); /* Q18 */
3793 0 : move32();
3794 0 : }
3795 :
3796 : /*-------------------------------------------------------------------*
3797 : * Dequant_lower_LSF()
3798 : *
3799 : * Dequantized the lower LSFs
3800 : *-------------------------------------------------------------------*/
3801 :
3802 :
3803 74213 : static void Dequant_lower_LSF_fx(
3804 : const Word16 lsf_idx[], /* i : LSF indices */
3805 : Word16 lsf_q[] /* o : Quantized LSFs Q15*/
3806 : )
3807 : {
3808 : Word16 i;
3809 :
3810 74213 : lsf_q[0] = lsf_q_cb_fx[0][lsf_idx[0]];
3811 74213 : move16();
3812 371065 : FOR( i = 1; i < NUM_Q_LSF; i++ )
3813 : {
3814 296852 : lsf_q[i] = add( lsf_q_cb_fx[i][lsf_idx[i]], lsf_q[i - 1] ); /*Q15*/
3815 296852 : move16();
3816 : }
3817 :
3818 74213 : return;
3819 : }
3820 :
3821 : /*-------------------------------------------------------------------*
3822 : * Map_higher_LSF()
3823 : *
3824 : * Map the higher LSFs from the lower LSFs
3825 : *-------------------------------------------------------------------*/
3826 :
3827 74213 : static void Map_higher_LSF_fx(
3828 : Word16 lsf_q[], /* i/o : Quantized lower LSFs Q15*/
3829 : const Word16 m, /* i : Mirroring point Q15*/
3830 : const Word16 grid_in[] /* i : Input LSF smoohthing grid Q15*/
3831 : )
3832 : {
3833 : Word16 lsf_map[NUM_MAP_LSF];
3834 : Word16 grid[NUM_MAP_LSF];
3835 : Word16 last_q_lsf;
3836 : Word16 lsf_smooth[NUM_MAP_LSF];
3837 : Word16 offset;
3838 : Word16 i;
3839 : Word16 scale;
3840 :
3841 445278 : FOR( i = 0; i < NUM_MAP_LSF; i++ )
3842 : {
3843 371065 : lsf_map[i] = sub( shl( m, 1 ), lsf_q[NUM_MAP_LSF - 1 - i] ); /*Q15*/
3844 371065 : move16();
3845 : }
3846 :
3847 74213 : IF( GT_16( m, MAX_LSF_FX_BY_2 ) )
3848 : {
3849 14712 : offset = lsf_map[0]; /*Q15*/
3850 14712 : move16();
3851 14712 : scale = div_s( sub( MAX_LSF_FX, m ), m );
3852 88272 : FOR( i = 0; i < NUM_MAP_LSF; i++ )
3853 : {
3854 73560 : lsf_map[i] = add( mult_r( sub( lsf_map[i], offset ), scale ), offset ); /*Q15*/
3855 73560 : move16();
3856 : }
3857 : }
3858 :
3859 74213 : last_q_lsf = lsf_q[NUM_Q_LSF - 1]; /*Q15*/
3860 74213 : move16();
3861 74213 : scale = sub( MAX_LSF_FX, last_q_lsf );
3862 :
3863 445278 : FOR( i = 0; i < NUM_MAP_LSF; i++ )
3864 : {
3865 371065 : grid[i] = add( mult_r( grid_in[i], scale ), last_q_lsf ); /*Q15*/
3866 371065 : move16();
3867 : }
3868 :
3869 445278 : FOR( i = 0; i < NUM_MAP_LSF; i++ )
3870 : {
3871 371065 : lsf_smooth[i] = sub( mult_r( grid_smoothing_fx[i], grid[i] ) /*Q15*/,
3872 371065 : mult_r( lsf_map[i], add( grid_smoothing_fx[i], -32768 /*-1.0f Q15*/ /*Q15*/ ) ) ); /*Q15*/
3873 371065 : move16();
3874 : }
3875 :
3876 445278 : FOR( i = 0; i < NUM_MAP_LSF; i++ )
3877 : {
3878 371065 : lsf_q[NUM_Q_LSF + i] = lsf_smooth[i]; /*Q15*/
3879 371065 : move16();
3880 : }
3881 :
3882 74213 : return;
3883 : }
3884 :
3885 :
3886 74213 : static void Dequant_mirror_point_fx(
3887 : const Word16 lsf_q[], /* i/o : Quantized lower LSFs Q15*/
3888 : const Word16 m_idx, /* i : Mirror point index Q0 */
3889 : Word16 *m /* i : Mirroring point Q15*/
3890 : )
3891 : {
3892 74213 : *m = add( mirror_point_q_cb_fx[m_idx], lsf_q[NUM_Q_LSF - 1] ); /*Q15*/
3893 74213 : move16();
3894 :
3895 74213 : return;
3896 : }
3897 :
3898 :
3899 6238 : static Word16 dotp_loc(
3900 : const Word16 x[], /* i : vector x[] Qx */
3901 : const Word32 y[], /* i : vector y[] Qy */
3902 : const Word16 n /* i : vector length */
3903 : )
3904 : {
3905 : Word16 i;
3906 : Word32 suma;
3907 6238 : Word16 guarded_bits = find_guarded_bits_fx( n );
3908 6238 : suma = L_shr( Mpy_32_16_1( y[0], x[0] ), guarded_bits ); /*Qx + Qy - guarded_bits*/
3909 :
3910 49904 : FOR( i = 1; i < n; i++ )
3911 : {
3912 43666 : suma = L_add( suma, L_shr( Mpy_32_16_1( y[i], x[i] ), guarded_bits ) ); /*Qx + Qy - guarded_bits*/
3913 : }
3914 6238 : suma = L_shl_sat( suma, guarded_bits ); /*Qx + Qy*/
3915 :
3916 6238 : return extract_h( suma ); /*Qx + Qy - 16*/
3917 : }
3918 :
3919 109417 : void ivas_dequantizeSHBparams_fx_9_1(
3920 : Decoder_State *st_fx,
3921 : const Word16 extl, /* i : extension layer */
3922 : Word32 extl_brate, /* i : extensiuon layer bitrate */
3923 : Word16 *Q_lsf, /* o : SHB LSF from de-quantization Q15*/
3924 : Word16 *Q_subgain, /* o : SHB subframe gains from de-quantization Q15*/
3925 : Word32 *Q_framegrain, /* o : SHB frame gain from de-quantization Q18*/
3926 : Word16 *uv_flag, /* o : unvoiced flag*/
3927 : Word32 *Q_shb_ener_sf, /* o : Q15 */
3928 : Word16 *Q_shb_res_gshape, /* o : Q14 */
3929 : Word16 *Q_mixFactors, /* o : Q15 */
3930 : Word16 *MSFlag )
3931 : {
3932 : Word16 i, j, idxLSF, idxSubGain, idxFrameGain;
3933 : Word16 Q_combined_gains[NUM_SHB_SUBFR / 4];
3934 : Word16 lsf_q[LPC_SHB_ORDER];
3935 : Word16 lsf_idx[NUM_Q_LSF];
3936 : Word16 m_idx, grid_idx;
3937 : Word16 m;
3938 : Word32 L_tmp;
3939 : Word16 tmp, frac, exp;
3940 : Word16 idx_shb_fr_gain, idx_res_gs[5], idx_mixFac;
3941 : Word16 temp_shb_ener_sf_fx;
3942 : TD_BWE_DEC_HANDLE hBWE_TD;
3943 :
3944 : UWord32 Idx_lvq;
3945 : Word16 Idx, Idx_pred;
3946 : Word16 num_bits_lvq;
3947 : Word16 out[LATTICE_DIM];
3948 : const Word16 *cb_stage;
3949 : Word16 predictor_bits;
3950 109417 : Word16 nbits = NUM_BITS_SHB_MSLVQ;
3951 109417 : move16();
3952 109417 : hBWE_TD = st_fx->hBWE_TD;
3953 :
3954 : /* LSFs */
3955 :
3956 109417 : IF( EQ_16( extl, WB_TBE ) )
3957 : {
3958 6876 : IF( EQ_32( extl_brate, WB_TBE_0k35 ) )
3959 : {
3960 2438 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
3961 : {
3962 0 : idxFrameGain = hBWE_TD->gFrame_WB;
3963 0 : move16();
3964 0 : idxLSF = hBWE_TD->lsf_WB;
3965 0 : move16();
3966 : }
3967 : ELSE
3968 : {
3969 2438 : idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FrameGain_LBR_WB );
3970 2438 : idxLSF = (Word16) get_next_indice_fx( st_fx, NUM_BITS_LBR_WB_LSF );
3971 : }
3972 :
3973 2438 : Copy( lbr_wb_bwe_lsfvq_cbook_2bit_fx + i_mult( idxLSF, LPC_SHB_ORDER_LBR_WB ), Q_lsf, LPC_SHB_ORDER_LBR_WB );
3974 2438 : set16_fx( Q_subgain, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR / 2 );
3975 2438 : Copy32( SHBCB_FrameGain16_fx + idxFrameGain, Q_framegrain, 1 );
3976 : }
3977 : ELSE
3978 : {
3979 4438 : *uv_flag = (Word16) get_next_indice_fx( st_fx, 1 );
3980 4438 : move16();
3981 4438 : idxSubGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_SUBGAINS );
3982 4438 : idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FrameGain );
3983 4438 : idxLSF = (Word16) get_next_indice_fx( st_fx, NUM_BITS_WB_LSF );
3984 4438 : Copy( wb_bwe_lsfvq_cbook_8bit_fx + i_mult( idxLSF, LPC_SHB_ORDER_WB ), Q_lsf, LPC_SHB_ORDER_WB );
3985 4438 : Copy( HBCB_SubGain5bit_fx + i_mult( idxSubGain, NUM_SHB_SUBFR / 4 ), Q_combined_gains, NUM_SHB_SUBFR / 4 );
3986 :
3987 22190 : FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
3988 : {
3989 :
3990 17752 : L_tmp = L_mult( Q_combined_gains[i], 21771 ); /*Q26 0.166096 in Q17 */
3991 17752 : L_tmp = L_shr( L_tmp, 10 );
3992 17752 : frac = L_Extract_lc( L_tmp, &exp );
3993 17752 : tmp = extract_l( Pow2( 14, frac ) ); /* Put 14 as exponent so that */
3994 : /* output of Pow2() will be: */
3995 : /* 16384 < Pow2() <= 32767 */
3996 17752 : Q_combined_gains[i] = shl( tmp, add( exp, 1 ) ); /* Q15 */
3997 17752 : move16();
3998 : }
3999 :
4000 22190 : FOR( i = 0; i < NUM_SHB_SUBFR / 2; i += 2 )
4001 : {
4002 17752 : Q_subgain[i] = Q_combined_gains[i / 2]; // Q15
4003 17752 : move16();
4004 17752 : Q_subgain[i + 1] = Q_combined_gains[i / 2]; // Q15
4005 17752 : move16();
4006 : }
4007 :
4008 : /* frame gain */
4009 4438 : Copy32( SHBCB_FrameGain64_fx + idxFrameGain, Q_framegrain, 1 );
4010 : }
4011 : }
4012 : ELSE /* SWB TBE DEC */
4013 : {
4014 102541 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
4015 : {
4016 0 : idxSubGain = hBWE_TD->idxSubGains;
4017 0 : move16();
4018 0 : idxFrameGain = hBWE_TD->idxFrameGain;
4019 0 : move16();
4020 : }
4021 : ELSE
4022 : {
4023 102541 : idxSubGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_SUBGAINS );
4024 102541 : IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
4025 : {
4026 3119 : idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FRAMEGAIN_1k75 );
4027 : }
4028 : ELSE
4029 : {
4030 99422 : idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FRAMEGAIN );
4031 : }
4032 : }
4033 :
4034 : /* Multi Source Flag */
4035 102541 : test();
4036 102541 : test();
4037 102541 : IF( GE_16( st_fx->element_mode, IVAS_CPE_DFT ) && !( EQ_16( st_fx->element_mode, IVAS_CPE_TD ) && st_fx->tdm_LRTD_flag ) )
4038 : {
4039 22531 : *MSFlag = (Word16) get_next_indice_fx( st_fx, STEREO_ICBWE_MSFLAG_BITS );
4040 22531 : move16();
4041 : }
4042 : ELSE
4043 : {
4044 80010 : *MSFlag = 0;
4045 80010 : move16();
4046 : }
4047 :
4048 102541 : IF( GE_32( st_fx->extl_brate, SWB_TBE_2k8 ) )
4049 : {
4050 22221 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
4051 : {
4052 0 : idx_shb_fr_gain = hBWE_TD->idx_shb_fr_gain;
4053 0 : move16();
4054 : }
4055 : ELSE
4056 : {
4057 22221 : idx_shb_fr_gain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_ENER_SF );
4058 : }
4059 22221 : temp_shb_ener_sf_fx = usdequant_fx( idx_shb_fr_gain, 0, 86 ); /* 86 = 0.042f in Q11 = Qin-1 */
4060 : /* o: temp_shb_ener_sf_fx in Q12 */
4061 :
4062 : /* *Q_shb_ener_sf = Pow(10.0, temp_shb_ener_sf_fx ); */
4063 : /* = pow(2, 3.321928*temp_shb_ener_sf_fx) */
4064 22221 : L_tmp = L_mult( temp_shb_ener_sf_fx, 27213 ); /* 3.321928 in Q13 -> L_tmp in Q12+Q13+1 = Q26 */
4065 22221 : L_tmp = L_shl( L_tmp, -10 ); /* bring L_tmp from Q26 to Q16 */
4066 22221 : frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent */
4067 22221 : L_tmp = Pow2( 14, frac );
4068 22221 : *Q_shb_ener_sf = L_shl( L_tmp, sub( exp, 14 ) /*exp - 14 + 0 */ ); /* In Q_ener: 2*Q_shb+1, Q_shb = 0; */
4069 22221 : move32();
4070 :
4071 133326 : FOR( i = 0; i < 5; i++ )
4072 : {
4073 111105 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
4074 : {
4075 0 : idx_res_gs[i] = hBWE_TD->idx_res_gs[i];
4076 0 : move16();
4077 : }
4078 : ELSE
4079 : {
4080 111105 : idx_res_gs[i] = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_RES_GS );
4081 111105 : move16();
4082 : }
4083 111105 : Q_shb_res_gshape[i] = usdequant_fx( idx_res_gs[i],
4084 : 2048 /*0.125f Q14*/, /*2048 = 0.125 in Q14 */
4085 : 1024 /*0.125f Q13*/ /*1024 = 0.125 in Q13 */
4086 : );
4087 111105 : move16();
4088 : /* o: Q_shb_res_gshape in Q14 */
4089 : }
4090 :
4091 22221 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
4092 : {
4093 0 : idx_mixFac = hBWE_TD->idx_mixFac;
4094 0 : move16();
4095 : }
4096 : ELSE
4097 : {
4098 22221 : idx_mixFac = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_VF );
4099 : }
4100 22221 : *Q_mixFactors = usdequant_fx( idx_mixFac, 4096 /* 0.125 in Q15 */, 2048 /* 0.125 in Q14 */ );
4101 22221 : move16();
4102 : /* o: Q_mixFactors in Q15 */
4103 : }
4104 : ELSE
4105 : {
4106 80320 : *Q_shb_ener_sf = L_deposit_l( 0 );
4107 80320 : move32();
4108 80320 : test();
4109 80320 : IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
4110 : {
4111 3999 : idx_mixFac = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_VF );
4112 3999 : *Q_mixFactors = usdequant_fx( idx_mixFac, 0 /* 0.0f in Q15*/, 2341 /*1.0f / ((1 << NUM_BITS_SHB_VF) - 1) in Q14*/ );
4113 3999 : move16();
4114 : }
4115 : ELSE
4116 : {
4117 76321 : *Q_mixFactors = 0;
4118 76321 : move16();
4119 : }
4120 80320 : set16_fx( Q_shb_res_gshape, 0, 5 );
4121 : }
4122 :
4123 : /* LSFs */
4124 102541 : test();
4125 102541 : test();
4126 102541 : test();
4127 102541 : test();
4128 102541 : test();
4129 102541 : test();
4130 102541 : test();
4131 102541 : test();
4132 102541 : test();
4133 102541 : IF( ( EQ_32( st_fx->extl_brate, SWB_TBE_0k95 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) ) && EQ_16( st_fx->codec_mode, MODE1 ) )
4134 : {
4135 26548 : set16_fx( lsf_idx, 0, 5 );
4136 26548 : lsf_idx[0] = (Word16) get_next_indice_fx( st_fx, 8 );
4137 26548 : move16();
4138 26548 : grid_idx = 0;
4139 26548 : move16();
4140 26548 : m_idx = 0;
4141 26548 : move16();
4142 :
4143 26548 : Copy( swb_tbe_lsfvq_cbook_8b + i_mult( lsf_idx[0], LPC_SHB_ORDER ), Q_lsf, LPC_SHB_ORDER );
4144 : }
4145 75993 : ELSE IF( ( st_fx->rf_flag == 0 ) && !( ( ( st_fx->element_mode == EVS_MONO ) && EQ_32( st_fx->total_brate, ACELP_9k60 ) ) || ( ( st_fx->element_mode == EVS_MONO ) && ( ( st_fx->total_brate == 0 ) ) && ( ( EQ_32( st_fx->last_total_brate, ACELP_9k60 ) ) || ( EQ_32( st_fx->last_total_brate, ACELP_13k20 ) && st_fx->rf_flag_last ) ) ) ) )
4146 : {
4147 75993 : IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
4148 : {
4149 : /* read multi-stage LVQ quantizer */
4150 3119 : IF( GE_16( nbits, 19 ) )
4151 : {
4152 3119 : cb_stage = cb_LSF_BWE_fx[0];
4153 3119 : move16();
4154 : }
4155 : ELSE
4156 : {
4157 0 : cb_stage = cb_LSF_BWE_fx[1];
4158 0 : move16();
4159 : }
4160 3119 : set16_fx( lsf_q, 0, LPC_SHB_ORDER );
4161 :
4162 : /* VQ part */
4163 3119 : num_bits_lvq = config_LSF_BWE[( NUM_BITS_SHB_MSLVQ - nbits ) * 3];
4164 3119 : Idx = get_next_indice_fx( st_fx, num_bits_lvq );
4165 3119 : v_add_16( lsf_q, cb_stage + i_mult( Idx, 6 ), lsf_q, 6 );
4166 :
4167 : /* MSLVQ part */
4168 3119 : num_bits_lvq = sub( sub( nbits, num_bits_lvq ), config_LSF_BWE[( NUM_BITS_SHB_MSLVQ - nbits ) * 3 + 2] );
4169 3119 : predictor_bits = config_LSF_BWE[( NUM_BITS_SHB_MSLVQ - nbits ) * 3 + 2];
4170 3119 : Idx_pred = 0;
4171 3119 : move16();
4172 :
4173 3119 : IF( EQ_16( num_bits_lvq, 16 ) )
4174 : {
4175 : /* MSLVQ part */
4176 0 : Idx_lvq = L_add( get_next_indice_fx( st_fx, sub( num_bits_lvq, 1 ) ), L_shl( get_next_indice_fx( st_fx, 1 ), 15 ) );
4177 0 : deindex_lvq_SHB_fx( Idx_lvq, out, num_bits_lvq, 0 );
4178 : }
4179 : ELSE
4180 : {
4181 : /* MSLVQ part */
4182 3119 : Idx_lvq = get_next_indice_fx( st_fx, num_bits_lvq );
4183 3119 : deindex_lvq_SHB_fx( Idx_lvq, out, num_bits_lvq, extract_l( LT_16( nbits, 19 ) ) );
4184 : }
4185 :
4186 : /* mvr2r( mean_lsf, Q_lsfs, LPC_SHB_ORDER ); */
4187 3119 : v_add_16( lsf_q, out, lsf_q, LATTICE_DIM ); /* quantized mean removed data for first 8 dim*/
4188 :
4189 : /* predict last 2 components */
4190 3119 : IF( predictor_bits == 0 )
4191 : {
4192 0 : lsf_q[LATTICE_DIM] = dotp_loc( lsf_q, LastCoefPred_0bit_fx, LATTICE_DIM );
4193 0 : move16();
4194 0 : lsf_q[LATTICE_DIM + 1] = dotp_loc( lsf_q, &LastCoefPred_0bit_fx[LATTICE_DIM + 1], LATTICE_DIM );
4195 0 : move16();
4196 : }
4197 : ELSE
4198 : {
4199 3119 : Idx_pred = (Word16) get_next_indice_fx( st_fx, 1 );
4200 3119 : lsf_q[LATTICE_DIM] = dotp_loc( lsf_q, &LastCoefPred_1bit_fx[2 * ( LATTICE_DIM + 1 ) * Idx_pred], LATTICE_DIM );
4201 3119 : move16();
4202 3119 : lsf_q[LATTICE_DIM + 1] = dotp_loc( lsf_q, &LastCoefPred_1bit_fx[2 * ( LATTICE_DIM + 1 ) * Idx_pred + LATTICE_DIM + 1], LATTICE_DIM );
4203 3119 : move16();
4204 : }
4205 :
4206 3119 : IF( LT_16( nbits, NUM_BITS_SHB_MSLVQ ) )
4207 : {
4208 0 : Idx_pred = (Word16) get_next_indice_fx( st_fx, sub( NUM_BITS_SHB_MSLVQ, nbits ) );
4209 : }
4210 :
4211 3119 : v_add_16( SHB_LSF_mean_fx, lsf_q, lsf_q, LPC_SHB_ORDER );
4212 3119 : v_sort( lsf_q, 0, LPC_SHB_ORDER - 1 );
4213 : }
4214 : ELSE
4215 : {
4216 : /* LSFs */
4217 72874 : test();
4218 72874 : test();
4219 72874 : test();
4220 72874 : IF( EQ_32( extl_brate, SWB_TBE_1k6 ) || EQ_32( extl_brate, FB_TBE_1k8 ) || EQ_32( extl_brate, SWB_TBE_2k8 ) || EQ_32( extl_brate, FB_TBE_3k0 ) )
4221 : {
4222 72874 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
4223 : {
4224 0 : FOR( i = 0; i < NUM_Q_LSF; i++ )
4225 : {
4226 0 : lsf_idx[i] = hBWE_TD->lsf_idx[i];
4227 0 : move16();
4228 : }
4229 : }
4230 : ELSE
4231 : {
4232 437244 : FOR( i = 0; i < NUM_Q_LSF; i++ )
4233 : {
4234 364370 : lsf_idx[i] = (Word16) get_next_indice_fx( st_fx, lsf_q_num_bits[i] );
4235 364370 : move16();
4236 : }
4237 : }
4238 : }
4239 72874 : Dequant_lower_LSF_fx( lsf_idx, lsf_q );
4240 :
4241 72874 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
4242 : {
4243 0 : m_idx = hBWE_TD->m_idx;
4244 0 : move16();
4245 : }
4246 : ELSE
4247 : {
4248 72874 : m_idx = (Word16) get_next_indice_fx( st_fx, MIRROR_POINT_BITS );
4249 : }
4250 :
4251 72874 : Dequant_mirror_point_fx( lsf_q, m_idx, &m );
4252 :
4253 : /* safety check in case of bit errors */
4254 72874 : IF( GT_16( m, MAX_LSF_FX ) )
4255 : {
4256 0 : st_fx->BER_detect = 1;
4257 0 : move16();
4258 0 : m = MAX_LSF_FX - 1;
4259 0 : move16();
4260 : }
4261 :
4262 72874 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
4263 : {
4264 0 : grid_idx = hBWE_TD->grid_idx;
4265 0 : move16();
4266 : }
4267 : ELSE
4268 : {
4269 72874 : grid_idx = (Word16) get_next_indice_fx( st_fx, NUM_LSF_GRID_BITS );
4270 : }
4271 :
4272 72874 : Map_higher_LSF_fx( lsf_q, m, lsf_grid_fx[grid_idx] );
4273 : }
4274 835923 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
4275 : {
4276 : /* safety check in case of bit errors */
4277 759930 : IF( GT_16( lsf_q[LPC_SHB_ORDER - 1 - i], MAX_LSF_FX ) )
4278 : {
4279 0 : st_fx->BER_detect = 1;
4280 0 : move16();
4281 0 : lsf_q[LPC_SHB_ORDER - 1 - i] = MAX_LSF_FX - 1;
4282 0 : move16();
4283 : }
4284 759930 : Q_lsf[i] = sub( 16384, lsf_q[LPC_SHB_ORDER - 1 - i] );
4285 759930 : move16();
4286 : }
4287 : }
4288 : ELSE
4289 : {
4290 0 : set16_fx( lsf_idx, 0, 5 );
4291 0 : Copy( hBWE_TD->lsf_idx, lsf_idx, 5 );
4292 0 : grid_idx = 0;
4293 0 : move16();
4294 0 : m_idx = 0;
4295 0 : move16();
4296 0 : Copy( swb_tbe_lsfvq_cbook_8b + i_mult( lsf_idx[0], LPC_SHB_ORDER ), Q_lsf, LPC_SHB_ORDER );
4297 : }
4298 :
4299 102541 : space_lsfs_fx( Q_lsf, LPC_SHB_ORDER );
4300 :
4301 : /* Dequantize subgain indices */
4302 102541 : j = i_mult( idxSubGain, NUM_SHB_SUBGAINS );
4303 102541 : move16();
4304 512705 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
4305 : {
4306 : /* Q_subgain[i] = (float) pow(10.0, SHBCB_SubGain5bit[j++]); */
4307 :
4308 410164 : L_tmp = L_mult( SHBCB_SubGain5bit_fx[j++], 27213 ); /*Q28 3.321928 in Q13 */
4309 410164 : L_tmp = L_shr( L_tmp, 12 );
4310 410164 : frac = L_Extract_lc( L_tmp, &exp );
4311 410164 : tmp = extract_l( Pow2( 14, frac ) ); /* Put 14 as exponent so that */
4312 : /* output of Pow2() will be: */
4313 : /* 16384 < Pow2() <= 32767 */
4314 410164 : Q_subgain[i] = shl( tmp, add( exp, 1 ) ); /*Q15*/
4315 410164 : move16();
4316 : }
4317 :
4318 1743197 : FOR( i = NUM_SHB_SUBFR - 1; i >= 0; i-- )
4319 : {
4320 1640656 : Word16 idx = 0;
4321 1640656 : move16();
4322 1640656 : IF( i != 0 )
4323 : {
4324 1538115 : idx = shr( i_mult( i, NUM_SHB_SUBGAINS ), NUM_SHB_SUBFR_LOG2 );
4325 : }
4326 1640656 : Q_subgain[i] = Q_subgain[idx];
4327 1640656 : move16();
4328 : }
4329 :
4330 : /* Frame gain */
4331 102541 : IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
4332 : {
4333 3119 : *Q_framegrain = L_mac( SHB_GAIN_QLOW_1k75_FX, idxFrameGain, SHB_GAIN_QDELTA_1k75_FX );
4334 3119 : move32();
4335 : }
4336 : ELSE
4337 : {
4338 99422 : *Q_framegrain = L_mac( SHB_GAIN_QLOW_FX, idxFrameGain, SHB_GAIN_QDELTA_FX );
4339 99422 : move32();
4340 : } /*Q18*/
4341 102541 : L_tmp = Mult_32_16( *Q_framegrain, 27213 ); /*Q16*/ /* 3.321928 in Q13 */
4342 102541 : frac = L_Extract_lc( L_tmp, &exp );
4343 102541 : L_tmp = Pow2( 30, frac );
4344 102541 : *Q_framegrain = L_shl( L_tmp, sub( exp, 12 ) ); /*Q18*/
4345 102541 : move32();
4346 : }
4347 :
4348 109417 : return;
4349 : }
4350 :
4351 :
4352 : /*==========================================================================*/
4353 : /* FUNCTION : static void dequantizeSHBparams_fx_9_1 () */
4354 : /*--------------------------------------------------------------------------*/
4355 : /* PURPOSE : Dequantize super highband spectral envolope */
4356 : /* temporal gains and frame gain */
4357 : /*--------------------------------------------------------------------------*/
4358 : /* INPUT ARGUMENTS : */
4359 : /* _Word16 extl i : extension layer */
4360 : /* _Word32 extl_brate i : extensiuon layer bitrate */
4361 : /*--------------------------------------------------------------------------*/
4362 : /* OUTPUT ARGUMENTS : */
4363 : /* _Word16 *Q_lsf, o : SHB LSF from de-quantization Q15 */
4364 : /* _Word16 *Q_subgain, o : SHB subframe gains from de-quantization Q15*/
4365 : /* _Word32 *Q_framegrain o : SHB frame gain from de-quantization Q18 */
4366 : /*--------------------------------------------------------------------------*/
4367 : /* INPUT/OUTPUT ARGUMENTS : */
4368 : /*--------------------------------------------------------------------------*/
4369 : /* RETURN ARGUMENTS : */
4370 : /* _ None */
4371 : /*--------------------------------------------------------------------------*/
4372 : /* CALLED FROM : */
4373 : /*==========================================================================*/
4374 :
4375 1339 : static void dequantizeSHBparams_fx_9_1(
4376 : Decoder_State *st_fx,
4377 : const Word16 extl, /* i : extension layer */
4378 : Word32 extl_brate, /* i : extensiuon layer bitrate */
4379 : Word16 *Q_lsf, /* o : SHB LSF from de-quantization Q15*/
4380 : Word16 *Q_subgain, /* o : SHB subframe gains from de-quantization Q15*/
4381 : Word32 *Q_framegrain, /* o : SHB frame gain from de-quantization Q18*/
4382 : Word16 *uv_flag, /* o : unvoiced flag*/
4383 : Word32 *Q_shb_ener_sf, /* o : Q15*/
4384 : Word16 *Q_shb_res_gshape, /* o : Q14*/
4385 : Word16 *Q_mixFactors /* o : Q15*/
4386 : )
4387 : {
4388 : Word16 i, j, idxLSF, idxSubGain, idxFrameGain;
4389 : Word16 Q_combined_gains[NUM_SHB_SUBFR / 4];
4390 : Word16 lsf_q[LPC_SHB_ORDER];
4391 : Word16 lsf_idx[NUM_Q_LSF];
4392 : Word16 m_idx, grid_idx;
4393 : Word16 m;
4394 : Word32 L_tmp;
4395 : Word16 tmp, frac, exp;
4396 : Word16 idx_shb_fr_gain, idx_res_gs[5], idx_mixFac;
4397 : Word16 temp_shb_ener_sf_fx;
4398 : TD_BWE_DEC_HANDLE hBWE_TD;
4399 1339 : hBWE_TD = st_fx->hBWE_TD;
4400 :
4401 : /* LSFs */
4402 :
4403 1339 : IF( EQ_16( extl, WB_TBE ) )
4404 : {
4405 0 : IF( EQ_32( extl_brate, WB_TBE_0k35 ) )
4406 : {
4407 0 : idxFrameGain = hBWE_TD->gFrame_WB;
4408 0 : move16();
4409 0 : idxLSF = hBWE_TD->lsf_WB;
4410 0 : move16();
4411 :
4412 0 : Copy( lbr_wb_bwe_lsfvq_cbook_2bit_fx + i_mult( idxLSF, LPC_SHB_ORDER_LBR_WB ), Q_lsf, LPC_SHB_ORDER_LBR_WB );
4413 0 : set16_fx( Q_subgain, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR / 2 );
4414 0 : Copy32( SHBCB_FrameGain16_fx + idxFrameGain, Q_framegrain, 1 );
4415 : }
4416 : ELSE
4417 : {
4418 0 : *uv_flag = (Word16) get_next_indice_fx( st_fx, 1 );
4419 0 : move16();
4420 0 : idxSubGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_SUBGAINS );
4421 0 : idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FrameGain );
4422 0 : idxLSF = (Word16) get_next_indice_fx( st_fx, NUM_BITS_WB_LSF );
4423 0 : Copy( wb_bwe_lsfvq_cbook_8bit_fx + i_mult( idxLSF, LPC_SHB_ORDER_WB ), Q_lsf, LPC_SHB_ORDER_WB );
4424 0 : Copy( HBCB_SubGain5bit_fx + i_mult( idxSubGain, NUM_SHB_SUBFR / 4 ), Q_combined_gains, NUM_SHB_SUBFR / 4 );
4425 :
4426 0 : FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
4427 : {
4428 :
4429 0 : L_tmp = L_mult( Q_combined_gains[i], 21771 ); /*Q26 0.166096 in Q17 */
4430 0 : L_tmp = L_shr( L_tmp, 10 );
4431 0 : frac = L_Extract_lc( L_tmp, &exp );
4432 0 : tmp = extract_l( Pow2( 14, frac ) ); /* Put 14 as exponent so that */
4433 : /* output of Pow2() will be: */
4434 : /* 16384 < Pow2() <= 32767 */
4435 0 : Q_combined_gains[i] = shl( tmp, add( exp, 1 ) ); /* Q15 */
4436 0 : move16();
4437 : }
4438 :
4439 0 : FOR( i = 0; i < NUM_SHB_SUBFR / 2; i += 2 )
4440 : {
4441 0 : Q_subgain[i] = Q_combined_gains[i / 2];
4442 0 : move16();
4443 0 : Q_subgain[i + 1] = Q_combined_gains[i / 2];
4444 0 : move16();
4445 : }
4446 :
4447 : /* frame gain */
4448 0 : Copy32( SHBCB_FrameGain64_fx + idxFrameGain, Q_framegrain, 1 );
4449 : }
4450 : }
4451 : ELSE /* SWB TBE DEC */
4452 : {
4453 1339 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
4454 : {
4455 604 : idxSubGain = hBWE_TD->idxSubGains;
4456 604 : move16();
4457 604 : idxFrameGain = hBWE_TD->idxFrameGain;
4458 604 : move16();
4459 : }
4460 : ELSE
4461 : {
4462 735 : idxSubGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_SUBGAINS );
4463 735 : idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FRAMEGAIN );
4464 : }
4465 :
4466 1339 : test();
4467 1339 : IF( EQ_32( st_fx->total_brate, ACELP_24k40 ) || EQ_32( st_fx->total_brate, ACELP_32k ) )
4468 : {
4469 604 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
4470 : {
4471 604 : idx_shb_fr_gain = hBWE_TD->idx_shb_fr_gain;
4472 604 : move16();
4473 : }
4474 : ELSE
4475 : {
4476 0 : idx_shb_fr_gain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_ENER_SF );
4477 : }
4478 604 : temp_shb_ener_sf_fx = usdequant_fx( idx_shb_fr_gain, 0, 86 ); /* 86 = 0.042f in Q11 = Qin-1 */
4479 : /* o: temp_shb_ener_sf_fx in Q12 */
4480 :
4481 : /* *Q_shb_ener_sf = Pow(10.0, temp_shb_ener_sf_fx ); */
4482 : /* = pow(2, 3.321928*temp_shb_ener_sf_fx) */
4483 604 : L_tmp = L_mult( temp_shb_ener_sf_fx, 27213 ); /* 3.321928 in Q13 -> L_tmp in Q12+Q13+1 = Q26 */
4484 604 : L_tmp = L_shl( L_tmp, -10 ); /* bring L_tmp from Q26 to Q16 */
4485 604 : frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent */
4486 604 : L_tmp = Pow2( 14, frac );
4487 604 : *Q_shb_ener_sf = L_shl( L_tmp, sub( exp, 14 ) /* exp - 14 + 0 */ ); /* In Q_ener: 2*Q_shb+1, Q_shb = 0; */
4488 604 : move32();
4489 :
4490 3624 : FOR( i = 0; i < 5; i++ )
4491 : {
4492 3020 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
4493 : {
4494 3020 : idx_res_gs[i] = hBWE_TD->idx_res_gs[i];
4495 3020 : move16();
4496 : }
4497 : ELSE
4498 : {
4499 0 : idx_res_gs[i] = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_RES_GS );
4500 0 : move16();
4501 : }
4502 3020 : Q_shb_res_gshape[i] = usdequant_fx( idx_res_gs[i],
4503 : 2048 /*0.125f Q14*/, /*2048 = 0.125 in Q14 */
4504 : 1024 /*0.125f Q13*/ /*1024 = 0.125 in Q13 */
4505 : );
4506 3020 : move16();
4507 : /* o: Q_shb_res_gshape in Q14 */
4508 : }
4509 :
4510 604 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
4511 : {
4512 604 : idx_mixFac = hBWE_TD->idx_mixFac;
4513 604 : move16();
4514 : }
4515 : ELSE
4516 : {
4517 0 : idx_mixFac = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_VF );
4518 : }
4519 604 : *Q_mixFactors = usdequant_fx( idx_mixFac, 4096 /* 0.125 in Q15 */, 2048 /* 0.125 in Q14 */ );
4520 604 : move16();
4521 : /* o: Q_mixFactors in Q15 */
4522 : }
4523 : ELSE
4524 : {
4525 735 : *Q_shb_ener_sf = L_deposit_l( 0 );
4526 735 : move32();
4527 735 : *Q_mixFactors = 0;
4528 735 : move16();
4529 735 : set16_fx( Q_shb_res_gshape, 0, 5 );
4530 : }
4531 :
4532 : /* LSFs */
4533 :
4534 :
4535 1339 : test();
4536 1339 : test();
4537 1339 : test();
4538 1339 : test();
4539 1339 : test();
4540 1339 : IF( ( ( st_fx->rf_flag == 0 ) ) && !( ( EQ_32( st_fx->total_brate, ACELP_9k60 ) ) || ( ( ( st_fx->total_brate == 0 ) ) && ( ( EQ_32( st_fx->last_total_brate, ACELP_9k60 ) ) || ( EQ_32( st_fx->last_total_brate, ACELP_13k20 ) && EQ_16( st_fx->rf_flag_last, 1 ) ) ) ) ) )
4541 :
4542 : {
4543 : /* LSFs */
4544 1339 : test();
4545 1339 : test();
4546 1339 : test();
4547 1339 : IF( EQ_32( extl_brate, SWB_TBE_1k6 ) || EQ_32( extl_brate, FB_TBE_1k8 ) || EQ_32( extl_brate, SWB_TBE_2k8 ) || EQ_32( extl_brate, FB_TBE_3k0 ) )
4548 : {
4549 1339 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
4550 : {
4551 3624 : FOR( i = 0; i < NUM_Q_LSF; i++ )
4552 : {
4553 3020 : lsf_idx[i] = hBWE_TD->lsf_idx[i];
4554 3020 : move16();
4555 : }
4556 : }
4557 : ELSE
4558 : {
4559 4410 : FOR( i = 0; i < NUM_Q_LSF; i++ )
4560 : {
4561 3675 : lsf_idx[i] = (Word16) get_next_indice_fx( st_fx, lsf_q_num_bits[i] );
4562 3675 : move16();
4563 : }
4564 : }
4565 : }
4566 1339 : Dequant_lower_LSF_fx( lsf_idx, lsf_q );
4567 :
4568 1339 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
4569 : {
4570 604 : m_idx = hBWE_TD->m_idx;
4571 604 : move16();
4572 : }
4573 : ELSE
4574 : {
4575 735 : m_idx = (Word16) get_next_indice_fx( st_fx, MIRROR_POINT_BITS );
4576 : }
4577 :
4578 1339 : Dequant_mirror_point_fx( lsf_q, m_idx, &m );
4579 :
4580 : /* safety check in case of bit errors */
4581 1339 : IF( GT_16( m, MAX_LSF_FX ) )
4582 : {
4583 0 : st_fx->BER_detect = 1;
4584 0 : move16();
4585 0 : m = MAX_LSF_FX - 1;
4586 0 : move16();
4587 : }
4588 :
4589 1339 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
4590 : {
4591 604 : grid_idx = hBWE_TD->grid_idx;
4592 604 : move16();
4593 : }
4594 : ELSE
4595 : {
4596 735 : grid_idx = (Word16) get_next_indice_fx( st_fx, NUM_LSF_GRID_BITS );
4597 : }
4598 :
4599 1339 : Map_higher_LSF_fx( lsf_q, m, lsf_grid_fx[grid_idx] );
4600 :
4601 14729 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
4602 : {
4603 : /* safety check in case of bit errors */
4604 13390 : IF( GT_16( lsf_q[LPC_SHB_ORDER - 1 - i], MAX_LSF_FX ) )
4605 : {
4606 0 : st_fx->BER_detect = 1;
4607 0 : move16();
4608 0 : lsf_q[LPC_SHB_ORDER - 1 - i] = MAX_LSF_FX - 1;
4609 0 : move16();
4610 : }
4611 13390 : Q_lsf[i] = sub( 16384, lsf_q[LPC_SHB_ORDER - 1 - i] );
4612 13390 : move16();
4613 : }
4614 : }
4615 : ELSE
4616 : {
4617 0 : set16_fx( lsf_idx, 0, 5 );
4618 0 : Copy( hBWE_TD->lsf_idx, lsf_idx, 5 );
4619 0 : grid_idx = 0;
4620 0 : move16();
4621 0 : m_idx = 0;
4622 0 : move16();
4623 0 : Copy( swb_tbe_lsfvq_cbook_8b + i_mult( lsf_idx[0], LPC_SHB_ORDER ), Q_lsf, LPC_SHB_ORDER );
4624 : }
4625 :
4626 1339 : space_lsfs_fx( Q_lsf, LPC_SHB_ORDER );
4627 :
4628 : /* Dequantize subgain indices */
4629 1339 : j = i_mult( idxSubGain, NUM_SHB_SUBGAINS );
4630 1339 : move16();
4631 6695 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
4632 : {
4633 : /* Q_subgain[i] = (float) pow(10.0, SHBCB_SubGain5bit[j++]); */
4634 :
4635 5356 : L_tmp = L_mult( SHBCB_SubGain5bit_fx[j], 27213 ); /*Q28 3.321928 in Q13 */
4636 5356 : j = add( j, 1 );
4637 5356 : L_tmp = L_shr( L_tmp, 12 );
4638 5356 : frac = L_Extract_lc( L_tmp, &exp );
4639 5356 : tmp = extract_l( Pow2( 14, frac ) ); /* Put 14 as exponent so that */
4640 : /* output of Pow2() will be: */
4641 : /* 16384 < Pow2() <= 32767 */
4642 5356 : Q_subgain[i] = shl( tmp, add( exp, 1 ) ); /*Q15*/
4643 5356 : move16();
4644 : }
4645 :
4646 22763 : FOR( i = NUM_SHB_SUBFR - 1; i >= 0; i-- )
4647 : {
4648 21424 : Word16 idx = 0;
4649 21424 : move16();
4650 21424 : IF( i != 0 )
4651 : {
4652 20085 : idx = shr( i_mult( i, NUM_SHB_SUBGAINS ), NUM_SHB_SUBFR_LOG2 );
4653 : }
4654 21424 : Q_subgain[i] = Q_subgain[idx];
4655 21424 : move16();
4656 : }
4657 :
4658 : /* Frame gain */
4659 1339 : *Q_framegrain = L_mac( SHB_GAIN_QLOW_FX, idxFrameGain, SHB_GAIN_QDELTA_FX ); /*Q18*/
4660 1339 : move32();
4661 1339 : L_tmp = Mult_32_16( *Q_framegrain, 27213 ); /*Q16*/ /* 3.321928 in Q13 */
4662 1339 : frac = L_Extract_lc( L_tmp, &exp );
4663 1339 : L_tmp = Pow2( 30, frac );
4664 1339 : *Q_framegrain = L_shl( L_tmp, sub( exp, 12 ) ); /*Q18*/
4665 1339 : move32();
4666 : }
4667 :
4668 1339 : return;
4669 : }
4670 :
4671 : /*-------------------------------------------------------------------*
4672 : * fb_tbe_dec()
4673 : *
4674 : * FB TBE decoder, 14(resp. 15.5) - 20 kHz band decoding module
4675 : *-------------------------------------------------------------------*/
4676 0 : void fb_tbe_dec_fx(
4677 : Decoder_State *st, /* i/o: encoder state structure */
4678 : const Word16 fb_exc[], /* i : FB excitation from the SWB part Q_fb_exc */
4679 : Word16 Q_fb_exc,
4680 : Word16 *hb_synth, /* o : high-band synthesis Q(15 - hb_synth_exp) */
4681 : Word16 hb_synth_exp )
4682 :
4683 : {
4684 : Word16 i;
4685 0 : Word16 ratio = 0;
4686 0 : move16();
4687 0 : Word32 fb_exc_energy = 0;
4688 0 : move32();
4689 : Word16 fb_synth[L_FRAME48k];
4690 : TD_BWE_DEC_HANDLE hBWE_TD;
4691 0 : hBWE_TD = st->hBWE_TD;
4692 :
4693 : /* decode FB slope information */
4694 0 : test();
4695 0 : test();
4696 0 : IF( EQ_16( st->extl, FB_TBE ) && !st->bfi )
4697 : {
4698 0 : IF( EQ_16( st->codec_mode, MODE2 ) )
4699 : {
4700 0 : i = hBWE_TD->idxGain;
4701 0 : move16();
4702 : }
4703 : ELSE
4704 : {
4705 0 : i = (Word16) get_next_indice_fx( st, 4 );
4706 : }
4707 0 : ratio = shl_sat( 1, i );
4708 : }
4709 0 : ELSE IF( EQ_16( st->extl, FB_TBE ) && st->bfi )
4710 : {
4711 0 : ratio = hBWE_TD->prev_fbbwe_ratio_fx;
4712 0 : move16();
4713 : }
4714 0 : fb_exc_energy = sum2_fx_mod( fb_exc, L_FRAME16k );
4715 :
4716 : /* FB TBE synthesis */
4717 0 : synthesise_fb_high_band_fx( fb_exc, Q_fb_exc, fb_synth, fb_exc_energy, ratio, st->L_frame, st->bfi, &( hBWE_TD->prev_fbbwe_ratio_fx ), hBWE_TD->fbbwe_hpf_mem_fx, hBWE_TD->fbbwe_hpf_mem_fx_Q, hb_synth_exp,
4718 0 : st->element_mode );
4719 :
4720 : /* add the fb_synth component to the hb_synth component */
4721 : /* v_add_fx( hb_synth, fb_synth, hb_synth, L_FRAME48k );*/
4722 0 : FOR( i = 0; i < L_FRAME48k; i++ )
4723 : {
4724 0 : hb_synth[i] = add_sat( hb_synth[i], fb_synth[i] );
4725 0 : move16();
4726 : }
4727 0 : return;
4728 : }
4729 :
4730 40715 : void fb_tbe_dec_ivas_fx(
4731 : Decoder_State *st, /* i/o: encoder state structure */
4732 : const Word16 fb_exc[], /* i : FB excitation from the SWB part Q_fb_exc */
4733 : Word16 Q_fb_exc,
4734 : Word32 *hb_synth, /* o : high-band synthesis Q(15 - hb_synth_exp) */
4735 : Word16 hb_synth_exp,
4736 : Word16 *fb_synth_ref, /*Q_fb_synth_ref*/
4737 : Word16 Q_fb_synth_ref,
4738 : Word16 output_frame )
4739 :
4740 : {
4741 : Word16 i;
4742 40715 : Word16 ratio = 0;
4743 40715 : move16();
4744 40715 : Word32 fb_exc_energy = 0;
4745 40715 : move32();
4746 : Word16 fb_synth[L_FRAME48k];
4747 : TD_BWE_DEC_HANDLE hBWE_TD;
4748 40715 : hBWE_TD = st->hBWE_TD;
4749 :
4750 : /* decode FB slope information */
4751 40715 : IF( EQ_16( output_frame, L_FRAME48k ) )
4752 : {
4753 35233 : test();
4754 35233 : test();
4755 35233 : IF( EQ_16( st->extl, FB_TBE ) && !st->bfi )
4756 : {
4757 34476 : IF( EQ_16( st->codec_mode, MODE2 ) )
4758 : {
4759 0 : i = hBWE_TD->idxGain;
4760 0 : move16();
4761 : }
4762 : ELSE
4763 : {
4764 34476 : i = (Word16) get_next_indice_fx( st, 4 );
4765 : }
4766 34476 : ratio = shl_sat( 1, i );
4767 : }
4768 757 : ELSE IF( EQ_16( st->extl, FB_TBE ) && st->bfi )
4769 : {
4770 757 : ratio = hBWE_TD->prev_fbbwe_ratio_fx;
4771 757 : move16();
4772 : }
4773 : }
4774 : ELSE
4775 : {
4776 5482 : IF( !st->bfi )
4777 : {
4778 5382 : st->next_bit_pos = add( st->next_bit_pos, NUM_BITS_FB_FRAMEGAIN );
4779 5382 : move16();
4780 : }
4781 :
4782 5482 : return;
4783 : }
4784 35233 : fb_exc_energy = sum2_fx_mod( fb_exc, L_FRAME16k );
4785 :
4786 : /* FB TBE synthesis */
4787 35233 : synthesise_fb_high_band_fx( fb_exc, Q_fb_exc, fb_synth, fb_exc_energy, ratio, st->L_frame, st->bfi, &( hBWE_TD->prev_fbbwe_ratio_fx ), hBWE_TD->fbbwe_hpf_mem_fx, hBWE_TD->fbbwe_hpf_mem_fx_Q, hb_synth_exp,
4788 35233 : st->element_mode );
4789 :
4790 35233 : test();
4791 35233 : IF( GE_16( st->element_mode, IVAS_CPE_DFT ) && ( st->idchan == 0 ) )
4792 : {
4793 3990 : Copy_Scale_sig( fb_synth, fb_synth_ref, L_FRAME48k, sub( Q_fb_synth_ref, add( hb_synth_exp, 16 ) ) ); // scaling is required
4794 : }
4795 : /* add the fb_synth component to the hb_synth component */
4796 : /* v_add_fx( hb_synth, fb_synth, hb_synth, L_FRAME48k );*/
4797 33858913 : FOR( i = 0; i < L_FRAME48k; i++ )
4798 : {
4799 : // hb_synth[i] = L_add( hb_synth[i], L_deposit_l(fb_synth[i]));
4800 33823680 : hb_synth[i] = L_add( hb_synth[i], L_shl( fb_synth[i], Q11 ) ); // hb_synth_exp
4801 33823680 : move16();
4802 : }
4803 35233 : return;
4804 : }
4805 :
4806 604 : void tbe_read_bitstream_fx(
4807 : Decoder_State *st_fx )
4808 : {
4809 : Word16 i;
4810 : TD_BWE_DEC_HANDLE hBWE_TD;
4811 604 : hBWE_TD = st_fx->hBWE_TD;
4812 :
4813 604 : test();
4814 604 : test();
4815 604 : test();
4816 604 : test();
4817 604 : test();
4818 604 : IF( ( EQ_16( st_fx->rf_flag, 1 ) || EQ_32( st_fx->total_brate, ACELP_9k60 ) ) && EQ_16( st_fx->bwidth, WB ) )
4819 : {
4820 : /* WB LSF */
4821 0 : hBWE_TD->lsf_WB = (Word16) get_next_indice_fx( st_fx, NUM_BITS_LBR_WB_LSF );
4822 0 : move16();
4823 :
4824 : /* WB frame gain */
4825 0 : hBWE_TD->gFrame_WB = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FrameGain_LBR_WB );
4826 0 : move16();
4827 : }
4828 604 : ELSE IF( ( GE_32( st_fx->total_brate, ACELP_9k60 ) ) && ( LE_32( st_fx->total_brate, ACELP_32k ) ) &&
4829 : ( ( EQ_16( st_fx->bwidth, SWB ) ) || ( EQ_16( st_fx->bwidth, FB ) ) ) )
4830 : {
4831 604 : test();
4832 604 : IF( ( ( st_fx->rf_flag == 0 ) ) && ( GT_32( st_fx->total_brate, ACELP_9k60 ) ) )
4833 : {
4834 3624 : FOR( i = 0; i < NUM_Q_LSF; i++ )
4835 : {
4836 3020 : hBWE_TD->lsf_idx[i] = (Word16) get_next_indice_fx( st_fx, lsf_q_num_bits[i] );
4837 3020 : move16();
4838 : }
4839 :
4840 604 : hBWE_TD->m_idx = (Word16) get_next_indice_fx( st_fx, MIRROR_POINT_BITS );
4841 604 : move16();
4842 604 : hBWE_TD->grid_idx = (Word16) get_next_indice_fx( st_fx, NUM_LSF_GRID_BITS );
4843 604 : move16();
4844 : }
4845 : ELSE
4846 : {
4847 0 : hBWE_TD->lsf_idx[0] = (Word16) get_next_indice_fx( st_fx, 8 );
4848 0 : move16();
4849 0 : hBWE_TD->m_idx = 0;
4850 0 : move16();
4851 0 : hBWE_TD->grid_idx = 0;
4852 0 : move16();
4853 : }
4854 :
4855 : /* shape gains */
4856 604 : hBWE_TD->idxSubGains = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_SUBGAINS );
4857 604 : move16();
4858 :
4859 : /* frame gain */
4860 604 : hBWE_TD->idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FRAMEGAIN );
4861 604 : move16();
4862 :
4863 604 : IF( GE_32( st_fx->total_brate, ACELP_24k40 ) )
4864 : {
4865 : /* sub frame energy*/
4866 604 : hBWE_TD->idx_shb_fr_gain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_ENER_SF );
4867 604 : move16();
4868 :
4869 : /* gain shapes residual */
4870 3624 : FOR( i = 0; i < NB_SUBFR16k; i++ )
4871 : {
4872 3020 : hBWE_TD->idx_res_gs[i] = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_RES_GS );
4873 3020 : move16();
4874 : }
4875 :
4876 : /* voicing factor */
4877 604 : hBWE_TD->idx_mixFac = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_VF );
4878 604 : move16();
4879 : }
4880 :
4881 604 : IF( EQ_16( st_fx->tec_tfa, 1 ) )
4882 : {
4883 604 : st_fx->tec_flag = (Word16) get_next_indice_fx( st_fx, BITS_TEC );
4884 604 : move16();
4885 604 : st_fx->tfa_flag = (Word16) get_next_indice_fx( st_fx, BITS_TFA );
4886 604 : move16();
4887 604 : test();
4888 604 : IF( st_fx->tfa_flag && st_fx->tec_flag )
4889 : {
4890 4 : st_fx->tec_flag = 2;
4891 4 : move16();
4892 4 : st_fx->tfa_flag = 0;
4893 4 : move16();
4894 : }
4895 : }
4896 : ELSE
4897 : {
4898 0 : st_fx->tec_flag = 0;
4899 0 : move16();
4900 0 : st_fx->tfa_flag = 0;
4901 0 : move16();
4902 : }
4903 : }
4904 :
4905 604 : IF( EQ_16( st_fx->bwidth, FB ) )
4906 : {
4907 0 : hBWE_TD->idxGain = (Word16) get_next_indice_fx( st_fx, 4 );
4908 0 : move16();
4909 : }
4910 604 : }
4911 :
4912 :
4913 : /*---------------------------------------------------------------------*
4914 : * GenTransition()
4915 : *
4916 : * Generate a highband transition signal from the gain shape overlap
4917 : * buffer to fill the gap caused by the delay alignment buffer when
4918 : * switching from TBE to IGF
4919 : *---------------------------------------------------------------------*/
4920 :
4921 22 : void GenTransition_fx(
4922 : TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle */
4923 : Word16 *output_HB, /* o : synthesized HB transitions signal st_fx->prev_Q_bwe_syn2 */
4924 : const Word32 output_Fs, /* i : output sampling rate */
4925 : Word16 rf_flag, /* i : RF flag */
4926 : Word32 total_bitrate /* i : total bitrate */
4927 : )
4928 : {
4929 : Word16 i, length;
4930 : Word16 syn_overlap_32k[L_FRAME32k];
4931 : Word32 L_tmp;
4932 22 : Word16 ol_len = 2 * SHB_OVERLAP_LEN;
4933 :
4934 : /* set targeted length of transition signal */
4935 22 : length = shl( NS2SA_FX2( output_Fs, DELAY_BWE_TOTAL_NS ), 1 );
4936 :
4937 : /* upsample overlap snippet */
4938 22 : Interpolate_allpass_steep_fx( hBWE_TD->syn_overlap_fx, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx, SHB_OVERLAP_LEN, syn_overlap_32k );
4939 :
4940 : /* perform spectral flip and downmix with overlap snippet to match HB synth */
4941 22 : test();
4942 22 : IF( ( rf_flag != 0 ) || EQ_32( total_bitrate, ACELP_9k60 ) )
4943 : {
4944 0 : flip_and_downmix_generic_fx( syn_overlap_32k, syn_overlap_32k, 2 * SHB_OVERLAP_LEN, hBWE_TD->genSHBsynth_Hilbert_Mem_fx,
4945 0 : hBWE_TD->genSHBsynth_Hilbert_Mem_fx + HILBERT_ORDER1, hBWE_TD->genSHBsynth_Hilbert_Mem_fx + ( HILBERT_ORDER1 + 2 * HILBERT_ORDER2 ),
4946 : &( hBWE_TD->syn_dm_phase ) );
4947 : }
4948 : ELSE
4949 : {
4950 462 : FOR( i = 0; i < ol_len; i = i + 2 )
4951 : {
4952 440 : syn_overlap_32k[i] = negate( syn_overlap_32k[i] );
4953 440 : move16();
4954 440 : syn_overlap_32k[i + 1] = syn_overlap_32k[i + 1];
4955 440 : move16();
4956 : }
4957 : }
4958 :
4959 : /* cross fade of overlap snippet and mirrored HB synth from previous frame */
4960 902 : FOR( i = 0; i < ol_len; i++ )
4961 : {
4962 880 : L_tmp = L_mult( window_shb_32k_fx[i], hBWE_TD->old_tbe_synth_fx[L_SHB_TRANSITION_LENGTH - 1 - i] );
4963 880 : output_HB[i] = mac_r( L_tmp, window_shb_32k_fx[2 * L_SHB_LAHEAD - 1 - i], syn_overlap_32k[i] );
4964 880 : move16();
4965 : }
4966 :
4967 : /* fill transition signal with mirrored HB synth from previous frame to fully fill delay alignment buffer gap */
4968 4026 : FOR( ; i < length; i++ )
4969 : {
4970 4004 : output_HB[i] = hBWE_TD->old_tbe_synth_fx[L_SHB_TRANSITION_LENGTH - 1 - i];
4971 4004 : move16();
4972 : }
4973 :
4974 22 : IF( EQ_32( output_Fs, 48000 ) )
4975 : {
4976 22 : interpolate_3_over_2_allpass_fx( output_HB, length, output_HB, hBWE_TD->int_3_over_2_tbemem_dec_fx, allpass_poles_3_ov_2 );
4977 : }
4978 0 : ELSE IF( EQ_32( output_Fs, 16000 ) )
4979 : {
4980 0 : Decimate_allpass_steep_fx( output_HB, hBWE_TD->mem_resamp_HB_32k_fx, L_FRAME32k, output_HB );
4981 : }
4982 :
4983 22 : return;
4984 : }
4985 :
4986 : /* IVAS 32-bit variant */
4987 5982 : void GenTransition_fx32(
4988 : TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle */
4989 : Word32 *outputHB_fx, /* o : synthesized HB transitions signal : Q11 */
4990 : const Word32 output_Fs, /* i : output sampling rate : Q0 */
4991 : const Word16 L_frame, /* i : ACELP frame length : Q0 */
4992 : const Word16 prev_Qx )
4993 : {
4994 : Word16 i, length;
4995 :
4996 : Word32 syn_overlap_32k_fx[2 * SHB_OVERLAP_LEN];
4997 :
4998 : /* set targeted length of transition signal */
4999 5982 : length = i_mult( 2, NS2SA_FX2( output_Fs, DELAY_BWE_TOTAL_NS ) );
5000 :
5001 : /* upsample overlap snippet */
5002 5982 : Interpolate_allpass_steep_fx32( hBWE_TD->syn_overlap_fx_32, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx_32, SHB_OVERLAP_LEN, syn_overlap_32k_fx );
5003 :
5004 : /* perform spectral flip and downmix with overlap snippet to match HB synth */
5005 5982 : test();
5006 5982 : IF( EQ_16( L_frame, L_FRAME ) )
5007 : {
5008 1989 : flip_and_downmix_generic_fx32( syn_overlap_32k_fx, syn_overlap_32k_fx, 2 * SHB_OVERLAP_LEN, hBWE_TD->genSHBsynth_Hilbert_Mem_fx, hBWE_TD->genSHBsynth_Hilbert_Mem_fx + HILBERT_ORDER1, hBWE_TD->genSHBsynth_Hilbert_Mem_fx + ( HILBERT_ORDER1 + 2 * HILBERT_ORDER2 ), &( hBWE_TD->syn_dm_phase ) );
5009 : }
5010 : ELSE
5011 : {
5012 163713 : FOR( i = 0; i < 2 * SHB_OVERLAP_LEN; i++ )
5013 : {
5014 159720 : IF( i % 2 == 0 )
5015 : {
5016 79860 : syn_overlap_32k_fx[i] = L_negate( syn_overlap_32k_fx[i] );
5017 : }
5018 : ELSE
5019 : {
5020 79860 : syn_overlap_32k_fx[i] = syn_overlap_32k_fx[i];
5021 : }
5022 159720 : move32();
5023 : }
5024 : }
5025 :
5026 : /* cross fade of overlap snippet and mirrored HB synth from previous frame */
5027 245262 : FOR( i = 0; i < 2 * L_SHB_LAHEAD; i++ )
5028 : {
5029 239280 : outputHB_fx[i] = L_add_sat( Mpy_32_16_1( hBWE_TD->old_tbe_synth_fx_32[L_SHB_TRANSITION_LENGTH - 1 - i], window_shb_32k_fx[i] ), Mpy_32_16_1( syn_overlap_32k_fx[i], window_shb_32k_fx[2 * L_SHB_LAHEAD - 1 - i] ) );
5030 239280 : move32();
5031 : }
5032 :
5033 : /* fill transition signal with mirrored HB synth from previous frame to fully fill delay alignment buffer gap */
5034 968388 : FOR( ; i < length; i++ )
5035 : {
5036 962406 : outputHB_fx[i] = L_shl( hBWE_TD->old_tbe_synth_fx[L_SHB_TRANSITION_LENGTH - 1 - i], sub( Q11, prev_Qx ) );
5037 962406 : move32();
5038 : }
5039 :
5040 5982 : IF( EQ_32( output_Fs, 48000 ) )
5041 : {
5042 4412 : interpolate_3_over_2_allpass_fx32( outputHB_fx, length, outputHB_fx, hBWE_TD->int_3_over_2_tbemem_dec_fx_32 );
5043 : }
5044 1570 : ELSE IF( EQ_32( output_Fs, 16000 ) )
5045 : {
5046 137 : Decimate_allpass_steep_fx32( outputHB_fx, hBWE_TD->mem_resamp_HB_32k_fx_32, L_FRAME32k, outputHB_fx );
5047 : }
5048 :
5049 5982 : return;
5050 : }
5051 :
5052 :
5053 : /*---------------------------------------------------------------------*
5054 : * GenTransition_WB()
5055 : *
5056 : *---------------------------------------------------------------------*/
5057 :
5058 0 : void GenTransition_WB_fx(
5059 : TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle */
5060 : Word16 *output, /* o : synthesized transitions signal */
5061 : const Word32 output_Fs /* i : output sampling rate */
5062 : )
5063 : {
5064 : Word16 i, length;
5065 : Word32 L_tmp;
5066 : Word16 speech_buf_16k1[L_FRAME16k], speech_buf_16k2[L_FRAME16k];
5067 : Word16 upsampled_synth[L_FRAME48k];
5068 : Word16 input_scaled[SHB_OVERLAP_LEN / 2];
5069 :
5070 : /* set targeted length of transition signal */
5071 0 : length = shl( NS2SA_FX2( output_Fs, DELAY_BWE_TOTAL_NS ), 1 );
5072 :
5073 : /* upsample overlap snippet */
5074 0 : Copy_Scale_sig( hBWE_TD->syn_overlap_fx, input_scaled, SHB_OVERLAP_LEN / 2, hBWE_TD->prev_Qx );
5075 0 : Interpolate_allpass_steep_fx( input_scaled, hBWE_TD->state_lsyn_filt_shb_fx, SHB_OVERLAP_LEN / 2, speech_buf_16k1 );
5076 0 : Interpolate_allpass_steep_fx( speech_buf_16k1, hBWE_TD->state_lsyn_filt_dwn_shb_fx, SHB_OVERLAP_LEN, speech_buf_16k2 );
5077 :
5078 : /* perform spectral flip and downmix with overlap snippet to match HB synth */
5079 0 : FOR( i = 0; i < SHB_OVERLAP_LEN; i += 2 )
5080 : {
5081 0 : speech_buf_16k2[i] = negate( speech_buf_16k2[i] );
5082 0 : speech_buf_16k2[i + 1] = speech_buf_16k2[i + 1];
5083 : }
5084 :
5085 : /* cross fade of overlap snippet and mirrored HB synth from previous frame */
5086 0 : FOR( i = 0; i < L_SHB_LAHEAD; i++ )
5087 : {
5088 0 : L_tmp = L_mult( window_shb_fx[i], hBWE_TD->old_tbe_synth_fx[L_SHB_TRANSITION_LENGTH - 1 - i] );
5089 0 : output[i] = mac_r( L_tmp, window_shb_fx[L_SHB_LAHEAD - 1 - i], speech_buf_16k2[i] );
5090 0 : move16();
5091 0 : output[i] = mult_r( output[i], 21299 );
5092 0 : move16();
5093 : }
5094 :
5095 : /* fill transition signal with mirrored HB synth from previous frame to fully fill delay alignment buffer gap */
5096 0 : FOR( ; i < length; i++ )
5097 : {
5098 0 : output[i] = mult_r( hBWE_TD->old_tbe_synth_fx[L_SHB_TRANSITION_LENGTH - 1 - i], 21299 );
5099 0 : move16();
5100 : }
5101 :
5102 : /* upsampling if necessary */
5103 0 : IF( EQ_32( output_Fs, 32000 ) )
5104 : {
5105 0 : Interpolate_allpass_steep_fx( output, hBWE_TD->mem_resamp_HB_fx, L_FRAME16k, upsampled_synth );
5106 0 : Copy( upsampled_synth, output, L_FRAME32k );
5107 : }
5108 0 : ELSE IF( EQ_32( output_Fs, 48000 ) )
5109 : {
5110 0 : interpolate_3_over_1_allpass_fx( output, L_FRAME16k, upsampled_synth, hBWE_TD->mem_resamp_HB_fx );
5111 0 : Copy( upsampled_synth, output, L_FRAME48k );
5112 : }
5113 :
5114 0 : return;
5115 : }
5116 :
5117 :
5118 : /* IVAS 32-bit variant */
5119 119 : void GenTransition_WB_fx32(
5120 : TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle */
5121 : Word32 *outputHB_fx, /* o : synthesized HB transitions signal : Q11 */
5122 : const Word32 output_Fs /* i : output sampling rate */
5123 : )
5124 : {
5125 : Word16 i, length;
5126 : Word32 speech_buf_16k1_fx[SHB_OVERLAP_LEN], speech_buf_16k2_fx[2 * SHB_OVERLAP_LEN];
5127 : Word32 upsampled_synth_fx[L_FRAME48k];
5128 :
5129 : /* set targeted length of transition signal */
5130 119 : length = i_mult( 2, NS2SA_FX2( output_Fs, DELAY_BWE_TOTAL_NS ) );
5131 :
5132 : /* upsample overlap snippet */
5133 119 : Interpolate_allpass_steep_fx32( hBWE_TD->syn_overlap_fx_32, hBWE_TD->state_lsyn_filt_shb_fx_32, SHB_OVERLAP_LEN / 2, speech_buf_16k1_fx );
5134 119 : Interpolate_allpass_steep_fx32( speech_buf_16k1_fx, hBWE_TD->state_lsyn_filt_dwn_shb_fx_32, SHB_OVERLAP_LEN, speech_buf_16k2_fx );
5135 :
5136 : /* perform spectral flip and downmix with overlap snippet to match HB synth */
5137 2499 : FOR( i = 0; i < SHB_OVERLAP_LEN; i++ )
5138 : {
5139 2380 : IF( i % 2 == 0 )
5140 : {
5141 1190 : speech_buf_16k2_fx[i] = L_negate( speech_buf_16k2_fx[i] );
5142 1190 : move32();
5143 : }
5144 : ELSE
5145 : {
5146 1190 : speech_buf_16k2_fx[i] = speech_buf_16k2_fx[i];
5147 1190 : move32();
5148 : }
5149 : }
5150 :
5151 : /* cross fade of overlap snippet and mirrored HB synth from previous frame */
5152 2499 : FOR( i = 0; i < L_SHB_LAHEAD; i++ )
5153 : {
5154 2380 : outputHB_fx[i] = L_add( Mpy_32_16_1( hBWE_TD->old_tbe_synth_fx_32[L_SHB_TRANSITION_LENGTH - 1 - i], window_shb_fx[i] ), Mpy_32_16_1( speech_buf_16k2_fx[i], window_shb_fx[L_SHB_LAHEAD - 1 - i] ) );
5155 2380 : move32();
5156 2380 : outputHB_fx[i] = Mpy_32_16_1( outputHB_fx[i], 21299 );
5157 2380 : move32();
5158 : }
5159 :
5160 : /* fill transition signal with mirrored HB synth from previous frame to fully fill delay alignment buffer gap */
5161 13649 : FOR( ; i < length; i++ )
5162 : {
5163 13530 : outputHB_fx[i] = hBWE_TD->old_tbe_synth_fx_32[L_SHB_TRANSITION_LENGTH - 1 - i];
5164 13530 : move32();
5165 13530 : outputHB_fx[i] = Mpy_32_16_1( outputHB_fx[i], 21299 );
5166 13530 : move32();
5167 : }
5168 :
5169 : /* upsampling if necessary */
5170 119 : IF( EQ_32( output_Fs, 32000 ) )
5171 : {
5172 10 : Interpolate_allpass_steep_fx32( outputHB_fx, hBWE_TD->mem_resamp_HB_fx_32, L_FRAME16k, upsampled_synth_fx );
5173 10 : Copy32( upsampled_synth_fx, outputHB_fx, L_FRAME32k );
5174 : }
5175 109 : ELSE IF( EQ_32( output_Fs, 48000 ) )
5176 : {
5177 43 : interpolate_3_over_1_allpass_fx32( outputHB_fx, L_FRAME16k, upsampled_synth_fx, hBWE_TD->mem_resamp_HB_fx_32 );
5178 43 : Copy32( upsampled_synth_fx, outputHB_fx, L_FRAME48k );
5179 : }
5180 :
5181 119 : return;
5182 : }
5183 :
5184 :
5185 : /*---------------------------------------------------------------------*
5186 : * TBEreset_dec()
5187 : *
5188 : *---------------------------------------------------------------------*/
5189 :
5190 228634 : void TBEreset_dec_fx(
5191 : Decoder_State *st_fx /* i/o: decoder state structure */
5192 : )
5193 : {
5194 : TD_BWE_DEC_HANDLE hBWE_TD;
5195 228634 : hBWE_TD = st_fx->hBWE_TD;
5196 :
5197 228634 : IF( NE_16( st_fx->last_core, ACELP_CORE ) )
5198 : {
5199 226815 : set16_fx( hBWE_TD->old_bwe_exc_fx, 0, PIT16k_MAX * 2 );
5200 226815 : hBWE_TD->bwe_non_lin_prev_scale_fx = L_deposit_l( 0 );
5201 226815 : move32();
5202 226815 : st_fx->prev_Q_bwe_exc = 31;
5203 226815 : move16();
5204 : }
5205 :
5206 228634 : test();
5207 228634 : IF( EQ_16( st_fx->bwidth, WB ) )
5208 : {
5209 9772 : wb_tbe_extras_reset_fx( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx, hBWE_TD->mem_genSHBexc_filt_down_wb3_fx );
5210 9772 : wb_tbe_extras_reset_synth_fx( hBWE_TD->state_lsyn_filt_shb_fx, hBWE_TD->state_lsyn_filt_dwn_shb_fx, hBWE_TD->state_32and48k_WB_upsample_fx, hBWE_TD->mem_resamp_HB_fx );
5211 :
5212 9772 : set16_fx( hBWE_TD->mem_genSHBexc_filt_down_shb_fx, 0, 7 );
5213 9772 : set16_fx( hBWE_TD->state_lpc_syn_fx, 0, 10 );
5214 9772 : set16_fx( hBWE_TD->state_syn_shbexc_fx, 0, L_SHB_LAHEAD / 4 );
5215 9772 : set16_fx( hBWE_TD->syn_overlap_fx, 0, L_SHB_LAHEAD );
5216 9772 : set32_fx( hBWE_TD->syn_overlap_fx_32, 0, L_SHB_LAHEAD );
5217 9772 : set32_fx( hBWE_TD->mem_csfilt_fx, 0, 2 );
5218 : }
5219 218862 : ELSE IF( EQ_16( st_fx->bwidth, SWB ) || EQ_16( st_fx->bwidth, FB ) )
5220 : {
5221 218862 : swb_tbe_reset_fx( hBWE_TD->mem_csfilt_fx, hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->state_lpc_syn_fx,
5222 218862 : hBWE_TD->syn_overlap_fx, hBWE_TD->state_syn_shbexc_fx, &( hBWE_TD->tbe_demph_fx ),
5223 218862 : &( hBWE_TD->tbe_premph_fx ), hBWE_TD->mem_stp_swb_fx, &( hBWE_TD->gain_prec_swb_fx ) );
5224 :
5225 218862 : swb_tbe_reset_synth_fx( hBWE_TD->genSHBsynth_Hilbert_Mem_fx, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx_32 );
5226 :
5227 218862 : set16_fx( hBWE_TD->GainShape_Delay_fx, 0, NUM_SHB_SUBFR / 2 );
5228 218862 : set16_fx( hBWE_TD->int_3_over_2_tbemem_dec_fx, 0, INTERP_3_2_MEM_LEN );
5229 218862 : set32_fx( hBWE_TD->int_3_over_2_tbemem_dec_fx_32, 0, INTERP_3_2_MEM_LEN );
5230 218862 : set16_fx( hBWE_TD->mem_resamp_HB_32k_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
5231 218862 : set32_fx( hBWE_TD->mem_resamp_HB_32k_fx_32, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
5232 218862 : hBWE_TD->prev_pow_exc16kWhtnd_fx32 = 1; /* Q0 1.f */
5233 218862 : move32();
5234 218862 : hBWE_TD->prev_mix_factor_fx = 32767; /* Q15 1.f */
5235 218862 : move16();
5236 :
5237 218862 : IF( EQ_16( st_fx->bwidth, FB ) )
5238 : {
5239 155986 : if ( st_fx->hBWE_FD != NULL )
5240 : {
5241 155986 : st_fx->hBWE_FD->prev_fb_ener_adjust_fx = 0;
5242 155986 : move16();
5243 : }
5244 155986 : set16_fx( hBWE_TD->fb_state_lpc_syn_fx, 0, LPC_SHB_ORDER );
5245 155986 : hBWE_TD->fb_tbe_demph_fx = 0;
5246 155986 : move16();
5247 155986 : fb_tbe_reset_synth_fx( hBWE_TD->fbbwe_hpf_mem_fx, hBWE_TD->fbbwe_hpf_mem_fx_Q, &hBWE_TD->prev_fbbwe_ratio_fx );
5248 : }
5249 : }
5250 :
5251 228634 : return;
5252 : }
5253 :
5254 :
5255 : /*-------------------------------------------------------------------*
5256 : * td_bwe_dec_init()
5257 : *
5258 : * Initialize TD BWE state structure at the decoder
5259 : *-------------------------------------------------------------------*/
5260 :
5261 3263 : void td_bwe_dec_init_fx(
5262 : TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle */
5263 : const Word16 extl, /* i : BWE extension layer */
5264 : const Word32 output_Fs /* i : output sampling rate */
5265 : )
5266 : {
5267 : Word16 i;
5268 :
5269 : /* init. SHB buffers */;
5270 3263 : InitSWBdecBuffer_fx( hBWE_TD );
5271 :
5272 : /* reset SHB buffers */
5273 3263 : ResetSHBbuffer_Dec_fx( hBWE_TD, extl );
5274 :
5275 3263 : IF( EQ_32( output_Fs, 48000 ) )
5276 : {
5277 2387 : set32_fx( hBWE_TD->fbbwe_hpf_mem_fx[0], 0, 4 );
5278 2387 : set32_fx( hBWE_TD->fbbwe_hpf_mem_fx[1], 0, 4 );
5279 2387 : set32_fx( hBWE_TD->fbbwe_hpf_mem_fx[2], 0, 4 );
5280 2387 : set32_fx( hBWE_TD->fbbwe_hpf_mem_fx[3], 0, 4 );
5281 2387 : set16_fx( hBWE_TD->fbbwe_hpf_mem_fx_Q, 0, 4 );
5282 : }
5283 :
5284 3263 : set16_fx( hBWE_TD->mem_resamp_HB_fx, 0, INTERP_3_1_MEM_LEN );
5285 3263 : set32_fx( hBWE_TD->mem_resamp_HB_fx_32, 0, INTERP_3_1_MEM_LEN );
5286 3263 : set16_fx( hBWE_TD->mem_resamp_HB_32k_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
5287 3263 : set32_fx( hBWE_TD->mem_resamp_HB_32k_fx_32, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
5288 :
5289 3263 : hBWE_TD->tilt_mem_fx = 0;
5290 3263 : move16();
5291 3263 : set16_fx( hBWE_TD->prev_lsf_diff_fx, 16384 /*0.5f in Q15*/, LPC_SHB_ORDER - 2 );
5292 3263 : hBWE_TD->prev_tilt_para_fx = 0;
5293 3263 : move16();
5294 3263 : set16_fx( hBWE_TD->cur_sub_Aq_fx, 0, M + 1 );
5295 3263 : set16_fx( hBWE_TD->int_3_over_2_tbemem_dec_fx, 0, INTERP_3_2_MEM_LEN );
5296 3263 : set32_fx( hBWE_TD->int_3_over_2_tbemem_dec_fx_32, 0, INTERP_3_2_MEM_LEN );
5297 :
5298 : /* TD BWE post-processing */
5299 3263 : hBWE_TD->ptr_mem_stp_swb_fx = hBWE_TD->mem_stp_swb_fx + LPC_SHB_ORDER - 1;
5300 3263 : set16_fx( hBWE_TD->mem_zero_swb_fx, 0, LPC_SHB_ORDER );
5301 :
5302 35893 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
5303 : {
5304 32630 : hBWE_TD->swb_lsp_prev_interp_fx[i] = swb_lsp_prev_interp_init[i];
5305 32630 : move16();
5306 : }
5307 :
5308 3263 : hBWE_TD->prev1_shb_ener_sf_fx = 32767; /* Q15*/
5309 3263 : move16();
5310 3263 : hBWE_TD->prev2_shb_ener_sf_fx = 32767; /* Q15*/
5311 3263 : move16();
5312 3263 : hBWE_TD->prev3_shb_ener_sf_fx = 32767; /* Q15*/
5313 3263 : move16();
5314 3263 : hBWE_TD->prev_res_shb_gshape_fx = 8192; /* 0.125 in Q14*/
5315 3263 : move16();
5316 3263 : hBWE_TD->prev_mixFactors_fx = 16384; /* 0.5 in Q15*/
5317 3263 : move16();
5318 3263 : hBWE_TD->prev_GainShape_fx = 0;
5319 3263 : move16();
5320 3263 : hBWE_TD->prev_Q_bwe_exc_fb = 51;
5321 3263 : move16();
5322 3263 : set16_fx( hBWE_TD->fb_state_lpc_syn_fx, 0, LPC_SHB_ORDER );
5323 3263 : hBWE_TD->fb_tbe_demph_fx = 0;
5324 3263 : move16();
5325 :
5326 3263 : set16_fx( hBWE_TD->old_hb_synth_fx, 0, L_FRAME48k );
5327 :
5328 3263 : hBWE_TD->prev_ener_fx = L_deposit_l( 0 );
5329 3263 : move32();
5330 :
5331 3263 : set16_fx( hBWE_TD->prev_lpc_wb_fx, 0, LPC_SHB_ORDER_WB );
5332 :
5333 3263 : return;
5334 : }
5335 :
5336 : /*-------------------------------------------------------------------*
5337 : * ResetSHBbuffer_Dec()
5338 : *
5339 : *
5340 : *-------------------------------------------------------------------*/
5341 :
5342 104775 : static void calc_tilt_bwe_fx_loc(
5343 : const Word32 *sp_fx, /* i : input signal : Q11 */
5344 : Word32 *tilt_fx, /* o : signal tilt : tilt_fx_q */
5345 : Word16 *tilt_fx_q, /* o : signal tilt */
5346 : const Word16 N /* i : signal length : Q0 */
5347 : )
5348 : {
5349 : Word16 i;
5350 : Word64 r0_fx, r1_fx;
5351 :
5352 104775 : r0_fx = EPSILON_FX_SMALL;
5353 104775 : move64();
5354 26927175 : FOR( i = 0; i < N; i++ )
5355 : {
5356 26822400 : r0_fx = W_add( r0_fx, W_shr( W_mult_32_32( sp_fx[i], sp_fx[i] ), 1 ) ); /*Q11*2 - 1*/
5357 : }
5358 104775 : r1_fx = W_deposit32_l( abs( L_sub( sp_fx[1], sp_fx[0] ) ) ); /*Q11*/
5359 26717625 : FOR( i = 2; i < N; i++ )
5360 : {
5361 26612850 : IF( W_mult_32_32( L_sub( sp_fx[i], sp_fx[i - 1] ) /*Q11*/, L_sub( sp_fx[i - 1], sp_fx[i - 2] ) /*Q11*/ ) /*Q11 * 2 - 1*/ < 0 )
5362 : {
5363 6565398 : r1_fx = W_add( r1_fx, W_deposit32_l( abs( L_sub( sp_fx[i], sp_fx[i - 1] ) ) ) ); /*Q11*/
5364 : }
5365 : }
5366 104775 : Word16 headroom_left_r0 = W_norm( r0_fx );
5367 104775 : Word16 headroom_left_r1 = W_norm( r1_fx );
5368 104775 : Word16 r0_q = 0, r1_q = 0;
5369 104775 : move16();
5370 104775 : move16();
5371 104775 : IF( LT_16( headroom_left_r0, 32 ) )
5372 : {
5373 104600 : r0_fx = W_shr( r0_fx, sub( 32, headroom_left_r0 ) );
5374 104600 : r0_q = sub( 31, sub( ( 2 * OUTPUT_Q ), sub( 32, headroom_left_r0 ) ) );
5375 : }
5376 : ELSE
5377 : {
5378 175 : r0_q = 31 - ( 2 * OUTPUT_Q );
5379 175 : move16();
5380 : }
5381 :
5382 104775 : IF( LT_16( headroom_left_r1, 32 ) )
5383 : {
5384 778 : r1_fx = W_shr( r1_fx, sub( 32, headroom_left_r1 ) );
5385 778 : r1_q = sub( OUTPUT_Q, sub( 32, headroom_left_r1 ) );
5386 : }
5387 : ELSE
5388 : {
5389 103997 : r1_q = OUTPUT_Q;
5390 103997 : move16();
5391 : }
5392 104775 : Word32 temp_r0_inv = ISqrt32( W_extract_l( r0_fx ), &r0_q );
5393 104775 : Word32 res = Mpy_32_32( W_extract_l( r1_fx ), temp_r0_inv );
5394 : // Word16 res_q = r1_q + ( r0_q < 0 ? ( 31 + ( -1 * r0_q ) ) : r0_q ) - 31;
5395 : Word16 res_q;
5396 104775 : IF( r0_q < 0 )
5397 : {
5398 104687 : res_q = add( r1_q, sub( add( 31, -r0_q ), 31 ) );
5399 : }
5400 : ELSE
5401 : {
5402 88 : res_q = add( r1_q, sub( r0_q, 31 ) );
5403 : }
5404 104775 : Word16 norm_res = norm_l( res );
5405 104775 : IF( norm_res > 0 )
5406 : {
5407 103991 : *tilt_fx = L_shl_sat( res, norm_res );
5408 103991 : move32();
5409 103991 : *tilt_fx_q = add( res_q, norm_res );
5410 103991 : move16();
5411 : }
5412 : ELSE
5413 : {
5414 784 : *tilt_fx = res;
5415 784 : move32();
5416 784 : *tilt_fx_q = res_q;
5417 784 : move16();
5418 : }
5419 104775 : return;
5420 : }
5421 :
5422 : /*-------------------------------------------------------------------*
5423 : * swb_tbe_dec()
5424 : *
5425 : * SWB TBE decoder, 6 - 14 kHz (or 7.5 - 15.5 kHz) band decoding module
5426 : *-------------------------------------------------------------------*/
5427 45328 : static void rescale_genSHB_mem_dec_ivas(
5428 : Decoder_State *st_fx,
5429 : Word16 sf /*Q0*/
5430 : )
5431 : {
5432 : Word16 i;
5433 : TD_BWE_DEC_HANDLE hBWE_TD;
5434 45328 : hBWE_TD = st_fx->hBWE_TD;
5435 :
5436 362624 : FOR( i = 0; i < 7; i++ )
5437 : {
5438 317296 : hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i], sf );
5439 317296 : move16();
5440 : }
5441 :
5442 : /* -- Apply memory scaling for 13.2 and 16.4k bps using sf ----*/
5443 45328 : IF( LT_32( st_fx->total_brate, ACELP_24k40 ) )
5444 : {
5445 400917 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
5446 : {
5447 364470 : hBWE_TD->state_lpc_syn_fx[i] = shl_sat( hBWE_TD->state_lpc_syn_fx[i], sf );
5448 364470 : move16();
5449 : }
5450 :
5451 765387 : FOR( i = 0; i < L_SHB_LAHEAD; i++ )
5452 : {
5453 728940 : hBWE_TD->state_syn_shbexc_fx[i] = shl_sat( hBWE_TD->state_syn_shbexc_fx[i], sf );
5454 728940 : move16();
5455 : }
5456 : }
5457 :
5458 45328 : hBWE_TD->mem_csfilt_fx[0] = L_shl( hBWE_TD->mem_csfilt_fx[0], sf );
5459 45328 : move32();
5460 :
5461 45328 : hBWE_TD->tbe_demph_fx = shl_r( hBWE_TD->tbe_demph_fx, sf );
5462 45328 : move16();
5463 45328 : hBWE_TD->tbe_premph_fx = shl_r( hBWE_TD->tbe_premph_fx, sf );
5464 45328 : move16();
5465 :
5466 45328 : return;
5467 : }
5468 :
5469 104775 : static void find_max_mem_dec_m3(
5470 : Decoder_State *st,
5471 : Word16 *n_mem3 )
5472 : {
5473 : Word16 i;
5474 : // Word16 n_mem_32;
5475 : Word16 tempQ15;
5476 : Word16 max3;
5477 : // Word32 tempQ32, Lmax3;
5478 : TD_BWE_DEC_HANDLE hBWE_TD;
5479 104775 : hBWE_TD = st->hBWE_TD;
5480 :
5481 : /* --------------------------------------------------------------*/
5482 : /* Find headroom for synthesis stage associated with these memories:
5483 : 1. st->syn_overlap_fx*/
5484 104775 : max3 = 0;
5485 104775 : move16();
5486 : /* find max in prev overlapSyn */
5487 2200275 : FOR( i = 0; i < L_SHB_LAHEAD; i++ )
5488 : {
5489 2095500 : tempQ15 = abs_s( hBWE_TD->syn_overlap_fx[i] );
5490 2095500 : max3 = s_max( max3, tempQ15 );
5491 : }
5492 : /* find max in prev genSHBsynth_state_lsyn_filt_shb_local_fx */
5493 :
5494 : /* estimate the norm for 16-bit memories */
5495 104775 : *n_mem3 = norm_s( max3 );
5496 104775 : move16();
5497 104775 : if ( max3 == 0 )
5498 : {
5499 8926 : *n_mem3 = 15;
5500 8926 : move16();
5501 : }
5502 104775 : }
5503 :
5504 : /*-------------------------------------------------------------------*
5505 : * ivas_swb_tbe_dec_fx()
5506 : *
5507 : * SWB TBE decoder, 6 - 14 kHz (or 7.5 - 15.5 kHz) band decoding module
5508 : *-------------------------------------------------------------------*/
5509 104775 : void ivas_swb_tbe_dec_fx(
5510 : Decoder_State *st, /* i/o: decoder state structure */
5511 : STEREO_ICBWE_DEC_HANDLE hStereoICBWE, /* i/o: IC-BWE state structure */
5512 : const Word32 *bwe_exc_extended_fx, /* i : bandwidth extended excitation : Q_exc */
5513 : Word16 Q_exc,
5514 : const Word16 voice_factors_fx[], /* i : voicing factors : Q15 */
5515 : const Word32 old_syn_12k8_16k_fx[], /* i : low band synthesis : old_syn_fx */
5516 : Word16 *White_exc16k_fx, /* o : shaped white excitation for the FB TBE : Q_white_exc*/
5517 : Word32 *synth_fx, /* o : SHB synthesis/final synthesis : Qx */
5518 : Word16 *pitch_buf_fx, /* i : Q6 */
5519 : Word16 *Q_white_exc )
5520 : {
5521 : Word16 i, j, cnt, n;
5522 : Word16 stemp;
5523 : TD_BWE_DEC_HANDLE hBWE_TD;
5524 : Word32 shaped_shb_excitation_fx_32[L_FRAME16k + L_SHB_LAHEAD];
5525 : Word16 bwe_exc_extended_16[L_FRAME32k + NL_BUFF_OFFSET];
5526 : Word16 shaped_shb_excitation_fx[L_FRAME16k + L_SHB_LAHEAD];
5527 : Word16 lsf_shb_fx[LPC_SHB_ORDER], lpc_shb_fx[LPC_SHB_ORDER + 1], GainShape_fx[NUM_SHB_SUBFR]; // Q12,Q12,Q15
5528 : Word32 GainFrame_fx; // Q18
5529 : Word32 error_fx[L_FRAME32k];
5530 : Word16 ener_fx;
5531 : Word32 L_ener;
5532 : Word16 is_fractive;
5533 : Word32 prev_pow_fx, curr_pow_fx, Lscale;
5534 : Word16 scale_fx;
5535 : Word16 max_val, temp_fx, shaped_shb_excitation_frac[L_FRAME16k + L_SHB_LAHEAD];
5536 : Word32 curr_frame_pow_fx;
5537 : Word16 curr_frame_pow_exp;
5538 : Word32 L_prev_ener_shb;
5539 : Word16 vf_modified_fx[NB_SUBFR16k];
5540 : Word16 f_fx, inc_fx;
5541 : Word32 GainFrame_prevfrm_fx;
5542 : Word32 tilt_swb_fec_32_fx;
5543 : Word16 tilt_swb_fec_fx_q;
5544 : Word16 tilt_swb_fec_fx;
5545 104775 : Word32 prev_ener_ratio_fx = 0; /* initialize just to avoid compiler warning */
5546 : Word16 lsp_shb_1_fx[LPC_SHB_ORDER], lsp_shb_2_fx[LPC_SHB_ORDER], lsp_temp_fx[LPC_SHB_ORDER];
5547 : Word16 lpc_shb_sf_fx[4 * ( LPC_SHB_ORDER + 1 )];
5548 : const Word16 *ptr_lsp_interp_coef_fx;
5549 : Word32 shb_ener_sf_32;
5550 : Word16 shb_res_gshape_fx[NB_SUBFR16k];
5551 : Word16 mixFactors_fx;
5552 : Word16 vind;
5553 : Word16 shb_res_dummy_fx[L_FRAME16k];
5554 : Word16 shaped_shb_excitationTemp_fx[L_FRAME16k];
5555 : Word32 ener_tmp_fx[NUM_SHB_SUBGAINS];
5556 : Word16 GainShape_tmp_fx[NUM_SHB_SUBGAINS];
5557 : Word16 pitch_fx;
5558 : Word16 l_subframe;
5559 : Word16 formant_fac_fx;
5560 : Word16 synth_scale_fx;
5561 : Word16 lsf_diff_fx[LPC_SHB_ORDER], w_fx[LPC_SHB_ORDER];
5562 : Word16 refl_fx[M];
5563 : Word16 tilt_para_fx;
5564 : Word16 *nlExc16k_fx, *mixExc16k_fx;
5565 : Word16 MSFlag;
5566 : Word16 feedback_fx;
5567 : Word16 GainShape_tilt_fx;
5568 :
5569 : // scaling
5570 : Word16 exp, tmp;
5571 : Word16 tmp1, tmp2;
5572 : Word16 mean_vf;
5573 : Word32 Lmax, L_tmp;
5574 : Word16 frac;
5575 : Word32 L_tmp1, L_tmp2;
5576 : Word16 Q_bwe_exc;
5577 : Word16 Q_bwe_exc_fb;
5578 : Word16 Q_shb;
5579 : Word16 n_mem, n_mem2, n_mem3, Qx, sc;
5580 : Word16 expa, expb;
5581 : Word16 fraca, fracb;
5582 : Word16 exp_ener, inv_ener;
5583 :
5584 104775 : hBWE_TD = st->hBWE_TD;
5585 :
5586 : /* initializations */
5587 104775 : GainFrame_fx = 0;
5588 104775 : move32();
5589 104775 : mixFactors_fx = 0;
5590 104775 : move16();
5591 104775 : shb_ener_sf_32 = 0;
5592 104775 : move32();
5593 104775 : set16_fx( shaped_shb_excitationTemp_fx, 0, L_FRAME16k );
5594 104775 : if ( st->hTdCngDec != NULL )
5595 : {
5596 76477 : st->hTdCngDec->shb_dtx_count_fx = 0;
5597 76477 : move16();
5598 : }
5599 104775 : is_fractive = 0;
5600 104775 : move16();
5601 :
5602 104775 : IF( hStereoICBWE != NULL )
5603 : {
5604 16460 : nlExc16k_fx = hStereoICBWE->nlExc16k_fx;
5605 16460 : mixExc16k_fx = hStereoICBWE->mixExc16k_fx;
5606 16460 : MSFlag = hStereoICBWE->MSFlag;
5607 16460 : move16();
5608 : }
5609 : ELSE
5610 : {
5611 88315 : nlExc16k_fx = NULL;
5612 88315 : mixExc16k_fx = NULL;
5613 88315 : MSFlag = 0;
5614 88315 : move16();
5615 : }
5616 :
5617 : /* find tilt */
5618 104775 : calc_tilt_bwe_fx_loc( old_syn_12k8_16k_fx, &tilt_swb_fec_32_fx, &tilt_swb_fec_fx_q, L_FRAME );
5619 104775 : tilt_swb_fec_fx = round_fx_sat( L_shl_sat( tilt_swb_fec_32_fx, sub( 11 + 16, tilt_swb_fec_fx_q ) ) );
5620 104775 : test();
5621 104775 : if ( st->bfi && st->clas_dec != UNVOICED_CLAS )
5622 : {
5623 1710 : tilt_swb_fec_fx = hBWE_TD->tilt_swb_fec_fx;
5624 1710 : move16();
5625 : }
5626 :
5627 : /* WB/SWB bandwidth switching */
5628 104775 : test();
5629 104775 : test();
5630 104775 : IF( ( GT_16( st->tilt_wb_fx, 10240 /*5 in Q11*/ ) && ( EQ_16( st->clas_dec, UNVOICED_CLAS ) ) ) || GT_16( st->tilt_wb_fx, 20480 /*10 in Q11*/ ) )
5631 : {
5632 7351 : test();
5633 7351 : test();
5634 7351 : test();
5635 7351 : test();
5636 7351 : test();
5637 7351 : test();
5638 7351 : test();
5639 7351 : test();
5640 7351 : IF( ( ( st->prev_fractive == 0 ) &&
5641 : ( LT_32( st->prev_enerLH_fx, L_shl( st->enerLH_fx, 1 ) ) && GT_32( st->prev_enerLH_fx, L_shr( st->enerLH_fx, 1 ) ) && LT_32( st->prev_enerLL_fx, L_shl( st->enerLL_fx, 1 ) ) && GT_32( st->prev_enerLL_fx, L_shr( st->enerLL_fx, 1 ) ) ) ) ||
5642 : ( EQ_16( st->prev_fractive, 1 ) &&
5643 : GT_32( L_shr( st->prev_enerLH_fx, 2 ), Mult_32_16( st->enerLH_fx, 24576 ) ) ) /* 24576 in Q13*/
5644 : || ( GT_32( L_shr( st->enerLL_fx, 1 ), Mult_32_16( st->enerLH_fx, 24576 ) ) && /*24576 = 1.5 in Q14*/
5645 : LT_16( st->tilt_wb_fx, 20480 ) ) /* 20480 = 10 in Q11*/
5646 : )
5647 : {
5648 0 : is_fractive = 0;
5649 : }
5650 : ELSE
5651 : {
5652 7351 : is_fractive = 1;
5653 : }
5654 7351 : move16();
5655 : }
5656 :
5657 : /* WB/SWB bandwidth switching */
5658 104775 : IF( st->bws_cnt > 0 )
5659 : {
5660 92 : f_fx = 1489; /*1.0f / 22.0f in Q15*/
5661 92 : move16();
5662 92 : inc_fx = 1489; /*1.0f / 22.0f in Q15*/
5663 92 : move16();
5664 :
5665 92 : IF( EQ_16( is_fractive, 1 ) )
5666 : {
5667 0 : Copy( lsf_tab_fx, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER );
5668 : }
5669 : ELSE
5670 : {
5671 1012 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
5672 : {
5673 920 : hBWE_TD->lsp_prevfrm_fx[i] = f_fx; /*Q15*/
5674 920 : move16();
5675 920 : f_fx = add( f_fx, inc_fx ); /*Q15*/
5676 : }
5677 : }
5678 92 : test();
5679 92 : test();
5680 92 : test();
5681 92 : test();
5682 92 : test();
5683 92 : test();
5684 92 : test();
5685 92 : IF( ( NE_16( st->last_extl, SWB_TBE ) && NE_16( st->last_extl, FB_TBE ) && !( ( L_sub( L_shr( st->prev_enerLH_fx, 1 ), st->enerLH_fx ) < 0 ) && L_sub( st->prev_enerLH_fx, ( L_shr( st->enerLH_fx, 1 ) > 0 ) ) ) ) || st->last_core != ACELP_CORE || ( ( st->last_core == ACELP_CORE ) && GT_32( abs( L_sub( st->last_core_brate, st->core_brate ) ), 3600 ) ) || EQ_16( s_xor( is_fractive, st->prev_fractive ), 1 ) )
5686 : {
5687 92 : set16_fx( GainShape_fx, 11587, NUM_SHB_SUBFR ); /*0.3536f in Q15*/
5688 : }
5689 : ELSE
5690 : {
5691 0 : if ( GT_16( hBWE_TD->prev_GainShape_fx, 11587 ) ) /*0.3536f in Q15*/
5692 : {
5693 0 : hBWE_TD->prev_GainShape_fx = 11587; /*0.3536f in Q15*/
5694 0 : move16();
5695 : }
5696 0 : set16_fx( GainShape_fx, hBWE_TD->prev_GainShape_fx, NUM_SHB_SUBFR );
5697 : }
5698 :
5699 92 : Copy( hBWE_TD->lsp_prevfrm_fx, lsf_shb_fx, LPC_SHB_ORDER );
5700 92 : set16_fx( shb_res_gshape_fx, 3277 /*0.2f Q14*/, NB_SUBFR16k ); /* Q14 */
5701 : }
5702 : ELSE
5703 : {
5704 104683 : test();
5705 104683 : IF( NE_16( st->last_extl, SWB_TBE ) && NE_16( st->last_extl, FB_TBE ) )
5706 : {
5707 6661 : f_fx = 1489; /*Q15*/
5708 6661 : move16();
5709 6661 : inc_fx = 1489; /*Q15*/
5710 6661 : move16();
5711 73271 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
5712 : {
5713 66610 : hBWE_TD->lsp_prevfrm_fx[i] = f_fx; /*Q15*/
5714 66610 : move16();
5715 66610 : f_fx = add( f_fx, inc_fx );
5716 : }
5717 : }
5718 :
5719 104683 : IF( !st->bfi )
5720 : {
5721 102541 : IF( st->use_partial_copy )
5722 : {
5723 0 : IF( NE_16( st->last_extl, SWB_TBE ) )
5724 : {
5725 0 : hBWE_TD->GainFrame_prevfrm_fx = 0;
5726 0 : move32();
5727 0 : f_fx = 1489 /*0.045454f Q15*/;
5728 0 : move16();
5729 0 : inc_fx = 1489 /*0.045454f Q15*/;
5730 0 : move16();
5731 0 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
5732 : {
5733 0 : hBWE_TD->lsp_prevfrm_fx[i] = f_fx; /*Q15*/
5734 0 : move16();
5735 0 : f_fx = add( f_fx, inc_fx ); /*Q15*/
5736 : }
5737 : }
5738 0 : Copy( hBWE_TD->lsp_prevfrm_fx, lsf_shb_fx, LPC_SHB_ORDER );
5739 0 : set16_fx( GainShape_fx, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR );
5740 :
5741 0 : IF( EQ_16( st->rf_frame_type, RF_NELP ) )
5742 : {
5743 : /* Frame gain */
5744 :
5745 0 : GainFrame_fx = L_mac( SHB_GAIN_QLOW_FX, st->rf_indx_tbeGainFr, SHB_GAIN_QDELTA_FX );
5746 0 : L_tmp = Mult_32_16( GainFrame_fx, 27213 ); /*Q16*/ /* 3.321928 in Q13 */
5747 :
5748 0 : frac = L_Extract_lc( L_tmp, &exp );
5749 0 : L_tmp = Pow2( 30, frac );
5750 0 : GainFrame_fx = L_shl( L_tmp, sub( exp, 12 ) ); /*Q18*/
5751 :
5752 0 : test();
5753 0 : test();
5754 0 : test();
5755 0 : test();
5756 0 : test();
5757 0 : test();
5758 0 : IF( ( st->core == ACELP_CORE ) && ( st->last_core == ACELP_CORE ) && !st->prev_use_partial_copy && EQ_16( st->prev_coder_type, UNVOICED ) && NE_32( GainFrame_fx, hBWE_TD->GainFrame_prevfrm_fx ) && NE_16( st->next_coder_type, GENERIC ) && EQ_16( st->last_extl, SWB_TBE ) )
5759 : {
5760 0 : GainFrame_fx = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 26214 /*0.8f in Q15*/ ), Mult_32_16( GainFrame_fx, 6553 /*0.2f in Q15*/ ) );
5761 : }
5762 : }
5763 : ELSE
5764 : {
5765 0 : temp_fx = 0;
5766 0 : move16();
5767 : /* Frame gain */
5768 0 : SWITCH( st->rf_indx_tbeGainFr )
5769 : {
5770 0 : case 0:
5771 0 : GainFrame_fx = 131072; /* 0.5f in Q18 */
5772 0 : move32();
5773 0 : if ( LE_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) )
5774 : {
5775 0 : temp_fx = 26214 /*0.8 Q15*/;
5776 0 : move16();
5777 : }
5778 0 : BREAK;
5779 0 : case 1:
5780 0 : GainFrame_fx = 524288; /* 2.0f in Q18 */
5781 0 : move32();
5782 0 : test();
5783 0 : if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) )
5784 : {
5785 0 : temp_fx = 26214 /*0.8 Q15*/;
5786 0 : move16();
5787 : }
5788 0 : BREAK;
5789 0 : case 2:
5790 0 : GainFrame_fx = 1048576; /* 4.0f in Q18 */
5791 0 : move32();
5792 0 : test();
5793 0 : if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) )
5794 : {
5795 0 : temp_fx = 26214 /*0.8 Q15*/;
5796 0 : move16();
5797 : }
5798 0 : BREAK;
5799 0 : case 3:
5800 0 : GainFrame_fx = 2097152; /* 8.0f in Q18 */
5801 0 : move32();
5802 0 : test();
5803 0 : if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 4194304l /*16Q18*/ ) )
5804 : {
5805 0 : temp_fx = 26214 /*0.8 Q15*/;
5806 0 : move16();
5807 : }
5808 0 : BREAK;
5809 0 : default:
5810 0 : fprintf( stderr, "RF SWB-TBE gain bits not supported." );
5811 : }
5812 :
5813 0 : IF( EQ_16( st->last_extl, SWB_TBE ) )
5814 : {
5815 0 : GainFrame_fx = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, temp_fx ), Mult_32_16( GainFrame_fx, sub( 32767, temp_fx ) ) );
5816 : /*Q18*/
5817 : }
5818 :
5819 0 : IF( ( st->core == ACELP_CORE ) && ( st->last_core == ACELP_CORE ) )
5820 : {
5821 0 : if ( !st->prev_use_partial_copy && EQ_16( st->last_coder_type, VOICED ) && EQ_16( st->rf_frame_type, RF_GENPRED ) && GT_32( GainFrame_fx, 2097152 /*8.0f in Q18*/ ) && LT_32( GainFrame_fx, 3059606 /*11.67f in Q18*/ ) )
5822 : {
5823 0 : GainFrame_fx = Mult_32_16( GainFrame_fx, 9830 /*0.3f in Q15*/ ); // Q18
5824 : }
5825 : }
5826 : }
5827 : }
5828 : ELSE
5829 : {
5830 : /* de-quantization */
5831 102541 : ivas_dequantizeSHBparams_fx_9_1( st, st->extl, st->extl_brate, lsf_shb_fx, GainShape_fx, &GainFrame_fx, &stemp,
5832 : &shb_ener_sf_32, shb_res_gshape_fx, &mixFactors_fx, &MSFlag );
5833 102541 : if ( hStereoICBWE != NULL )
5834 : {
5835 16209 : hStereoICBWE->MSFlag = MSFlag;
5836 16209 : move16();
5837 : }
5838 : }
5839 : }
5840 : ELSE
5841 : {
5842 2142 : Copy( hBWE_TD->lsp_prevfrm_fx, lsf_shb_fx, LPC_SHB_ORDER );
5843 2142 : test();
5844 2142 : IF( EQ_16( st->codec_mode, MODE1 ) && ( st->element_mode == EVS_MONO ) )
5845 : {
5846 0 : gradientGainShape( st, GainShape_fx, &GainFrame_fx );
5847 : }
5848 : ELSE
5849 : {
5850 10710 : FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
5851 : {
5852 42840 : FOR( j = 0; j < 4; j++ )
5853 : {
5854 34272 : GainShape_fx[i * 4 + j] = mult_r( st->cummulative_damping, hBWE_TD->GainShape_Delay_fx[4 + i] );
5855 34272 : move16();
5856 : }
5857 : }
5858 2142 : IF( GT_16( tilt_swb_fec_fx, ( 8 << 11 ) ) ) /* tilt_swb_fec_fx in Q11 */
5859 : {
5860 44 : IF( EQ_16( st->nbLostCmpt, 1 ) )
5861 : {
5862 39 : GainFrame_fx = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 19661 /*0.6f Q15*/ );
5863 : }
5864 5 : ELSE IF( EQ_16( st->nbLostCmpt, 2 ) )
5865 : {
5866 5 : GainFrame_fx = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 11469 /*0.35f Q15*/ );
5867 : }
5868 : ELSE
5869 : {
5870 0 : GainFrame_fx = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 6554 /*0.2f Q15*/ );
5871 : }
5872 44 : GainFrame_fx = Mult_32_16( GainFrame_fx, st->cummulative_damping );
5873 : }
5874 : ELSE
5875 : {
5876 2098 : GainFrame_fx = hBWE_TD->GainFrame_prevfrm_fx;
5877 2098 : move32(); /* gain locking */
5878 : }
5879 : }
5880 :
5881 2142 : IF( GE_32( st->extl_brate, SWB_TBE_2k8 ) )
5882 : {
5883 808 : test();
5884 808 : IF( EQ_16( st->codec_mode, MODE1 ) && ( st->element_mode == EVS_MONO ) )
5885 : {
5886 0 : L_tmp = L_mult( extract_l( hBWE_TD->prev2_shb_ener_sf_fx ), extract_l( hBWE_TD->prev3_shb_ener_sf_fx ) ); /*Q1*/
5887 0 : tmp = round_fx( root_a_fx( L_tmp, 1, &exp ) ); /* Q = 15-exp */
5888 0 : tmp1 = extract_l( hBWE_TD->prev1_shb_ener_sf_fx ); /*Q0*/
5889 0 : i = sub( norm_s( tmp1 ), 1 );
5890 0 : tmp1 = shl( tmp1, i ); /* Qi */
5891 0 : IF( tmp == 0 )
5892 : {
5893 0 : tmp = 32767 /*1.0f Q15*/;
5894 0 : move16(); /*Q15*/
5895 : }
5896 : ELSE
5897 : {
5898 0 : scale_fx = div_s( tmp1, tmp ); /* Q15 - Q(15-exp) + Qi = Qexp+i */
5899 0 : scale_fx = s_max( scale_fx, 0 );
5900 0 : tmp = shl_sat( scale_fx, sub( sub( 15, exp ), i ) ); /*Q15*/
5901 : }
5902 0 : scale_fx = mult_r( hBWE_TD->prev_res_shb_gshape_fx, tmp ); /* Q14 */
5903 0 : test();
5904 0 : IF( GT_32( L_shr( hBWE_TD->prev2_shb_ener_sf_fx, 1 ), hBWE_TD->prev1_shb_ener_sf_fx ) ||
5905 : GT_32( L_shr( hBWE_TD->prev3_shb_ener_sf_fx, 1 ), hBWE_TD->prev2_shb_ener_sf_fx ) )
5906 : {
5907 0 : shb_ener_sf_32 = Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, scale_fx );
5908 :
5909 0 : if ( GT_16( st->nbLostCmpt, 1 ) )
5910 : {
5911 0 : shb_ener_sf_32 = L_shr( shb_ener_sf_32, 1 );
5912 : }
5913 : }
5914 : ELSE
5915 : {
5916 0 : L_tmp = L_mult( scale_fx, scale_fx ); /* Q29 */
5917 0 : shb_ener_sf_32 = L_shl( Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, round_fx( L_tmp ) ), 2 );
5918 : }
5919 : }
5920 : ELSE
5921 : {
5922 808 : test();
5923 808 : IF( GT_32( L_shr( hBWE_TD->prev2_shb_ener_sf_fx, 1 ), hBWE_TD->prev1_shb_ener_sf_fx ) ||
5924 : GT_32( L_shr( hBWE_TD->prev3_shb_ener_sf_fx, 1 ), hBWE_TD->prev2_shb_ener_sf_fx ) )
5925 : {
5926 60 : shb_ener_sf_32 = L_shr( Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, st->cummulative_damping ), 1 );
5927 : }
5928 : ELSE
5929 : {
5930 748 : shb_ener_sf_32 = Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, st->cummulative_damping );
5931 : }
5932 : }
5933 : }
5934 :
5935 2142 : shb_ener_sf_32 = L_max( shb_ener_sf_32, 1l /*1.0f Q0*/ );
5936 2142 : mixFactors_fx = hBWE_TD->prev_mixFactors_fx;
5937 2142 : move16();
5938 :
5939 2142 : IF( EQ_16( st->codec_mode, MODE1 ) )
5940 : {
5941 2142 : set16_fx( shb_res_gshape_fx, 3277 /*0.2f Q14*/, 5 ); /* Q14 */
5942 : }
5943 : ELSE
5944 : {
5945 0 : set16_fx( shb_res_gshape_fx, 16384 /*1.0f Q14*/, 5 ); /* Q14 */
5946 : }
5947 : }
5948 : }
5949 :
5950 : /* get the gainshape delay */
5951 104775 : Copy( &hBWE_TD->GainShape_Delay_fx[4], &hBWE_TD->GainShape_Delay_fx[0], NUM_SHB_SUBFR / 4 );
5952 523875 : FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
5953 : {
5954 419100 : hBWE_TD->GainShape_Delay_fx[i + 4] = GainShape_fx[i * 4]; /*Q15*/
5955 419100 : move16();
5956 : }
5957 :
5958 104775 : L_tmp = L_mult( voice_factors_fx[0], 8192 );
5959 104775 : L_tmp = L_mac( L_tmp, voice_factors_fx[1], 8192 );
5960 104775 : L_tmp = L_mac( L_tmp, voice_factors_fx[2], 8192 );
5961 104775 : mean_vf = mac_r( L_tmp, voice_factors_fx[3], 8192 );
5962 :
5963 104775 : Copy( voice_factors_fx, vf_modified_fx, NB_SUBFR16k );
5964 :
5965 104775 : test();
5966 104775 : IF( EQ_16( st->coder_type, VOICED ) || GT_16( mean_vf, 13107 /*0.4f Q15*/ ) )
5967 : {
5968 91460 : FOR( i = 1; i < NB_SUBFR; i++ )
5969 : {
5970 68595 : L_tmp = L_mult( voice_factors_fx[i], 26214 /*0.8f Q15*/ );
5971 68595 : vf_modified_fx[i] = mac_r( L_tmp, voice_factors_fx[i - 1], 6554 /*0.2f Q15*/ );
5972 68595 : move16();
5973 : }
5974 :
5975 22865 : IF( st->L_frame != L_FRAME )
5976 : {
5977 11715 : L_tmp = L_mult( voice_factors_fx[4], 26214 /*0.8f Q15*/ );
5978 11715 : vf_modified_fx[4] = mac_r( L_tmp, voice_factors_fx[3], 6554 /*0.2f Q15*/ );
5979 11715 : move16();
5980 : }
5981 : }
5982 :
5983 104775 : test();
5984 104775 : IF( st->use_partial_copy && st->nelp_mode_dec )
5985 : {
5986 0 : set16_fx( vf_modified_fx, 0, NB_SUBFR16k );
5987 : }
5988 :
5989 : /* SHB LSF from current frame; and convert to LSP for interpolation */
5990 104775 : E_LPC_lsf_lsp_conversion( lsf_shb_fx, lsp_shb_2_fx, LPC_SHB_ORDER );
5991 :
5992 104775 : test();
5993 104775 : IF( EQ_16( st->last_extl, SWB_TBE ) || EQ_16( st->last_extl, FB_TBE ) )
5994 : {
5995 : /* SHB LSP values from prev. frame for interpolation */
5996 98022 : Copy( hBWE_TD->swb_lsp_prev_interp_fx, lsp_shb_1_fx, LPC_SHB_ORDER );
5997 : }
5998 : ELSE
5999 : {
6000 : /* Use current frame's LSPs; in effect no interpolation */
6001 6753 : Copy( lsp_shb_2_fx, lsp_shb_1_fx, LPC_SHB_ORDER );
6002 : }
6003 :
6004 104775 : test();
6005 104775 : test();
6006 104775 : test();
6007 104775 : IF( ( st->bws_cnt == 0 ) && ( st->bws_cnt1 == 0 ) && ( st->prev_use_partial_copy == 0 ) && ( st->use_partial_copy == 0 ) )
6008 : {
6009 104415 : lsf_diff_fx[0] = 16384;
6010 104415 : move16(); /*Q15*/
6011 104415 : lsf_diff_fx[LPC_SHB_ORDER - 1] = 16384;
6012 104415 : move16(); /*Q15*/
6013 939735 : FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
6014 : {
6015 835320 : lsf_diff_fx[i] = sub( lsf_shb_fx[i], lsf_shb_fx[i - 1] );
6016 835320 : move16();
6017 : }
6018 :
6019 104415 : a2rc_fx( hBWE_TD->cur_sub_Aq_fx + 1, refl_fx, M );
6020 104415 : tmp = add( 16384, shr( refl_fx[0], 1 ) ); /*Q14*/
6021 104415 : tmp1 = mult( 27425, tmp );
6022 104415 : tmp1 = mult( tmp1, tmp ); /*Q10*/
6023 104415 : tmp2 = shr( mult( 31715, tmp ), 2 ); /*Q10*/
6024 104415 : tilt_para_fx = add( sub( tmp1, tmp2 ), 1335 ); /*Q10*/
6025 :
6026 104415 : test();
6027 104415 : IF( NE_16( st->last_extl, SWB_TBE ) && NE_16( st->last_extl, FB_TBE ) )
6028 : {
6029 58995 : FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
6030 : {
6031 52440 : hBWE_TD->prev_lsf_diff_fx[i - 1] = shr( lsf_diff_fx[i], 1 );
6032 52440 : move16();
6033 : }
6034 : }
6035 :
6036 104415 : IF( LE_32( st->extl_brate, FB_TBE_1k8 ) )
6037 : {
6038 81434 : test();
6039 81434 : test();
6040 81434 : test();
6041 81434 : test();
6042 81434 : test();
6043 81434 : IF( !( GT_16( hBWE_TD->prev_tilt_para_fx, 5120 ) && ( EQ_16( st->coder_type, TRANSITION ) || LT_16( tilt_para_fx, 1024 ) ) ) &&
6044 : !( ( ( LT_16( hBWE_TD->prev_tilt_para_fx, 3072 ) && GE_16( st->prev_coder_type, VOICED ) ) ) && GT_16( tilt_para_fx, 5120 ) ) )
6045 : {
6046 715050 : FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
6047 : {
6048 635600 : IF( LT_16( lsf_diff_fx[i], hBWE_TD->prev_lsf_diff_fx[i - 1] ) )
6049 : {
6050 258079 : tmp = mult( 26214, lsf_diff_fx[i] );
6051 :
6052 258079 : test();
6053 258079 : IF( ( hBWE_TD->prev_lsf_diff_fx[i - 1] <= 0 ) || ( tmp < 0 ) ) /* safety check in case of bit errors */
6054 : {
6055 0 : st->BER_detect = 1;
6056 0 : move16();
6057 0 : tmp = 0;
6058 0 : move16();
6059 : }
6060 : ELSE
6061 : {
6062 258079 : tmp = div_s( tmp, hBWE_TD->prev_lsf_diff_fx[i - 1] );
6063 : }
6064 :
6065 258079 : tmp = s_max( tmp, 16384 );
6066 258079 : w_fx[i] = s_min( tmp, 32767 );
6067 258079 : move16();
6068 : }
6069 : ELSE
6070 : {
6071 377521 : tmp = mult( 26214, hBWE_TD->prev_lsf_diff_fx[i - 1] );
6072 :
6073 377521 : test();
6074 377521 : IF( ( lsf_diff_fx[i] <= 0 ) || ( tmp < 0 ) ) /* safety check in case of bit errors */
6075 : {
6076 0 : st->BER_detect = 1;
6077 0 : move16();
6078 0 : tmp = 0;
6079 0 : move16();
6080 : }
6081 : ELSE
6082 : {
6083 377521 : tmp = div_s( tmp, lsf_diff_fx[i] );
6084 : }
6085 :
6086 377521 : tmp = s_max( tmp, 16384 );
6087 377521 : w_fx[i] = s_min( tmp, 32767 );
6088 377521 : move16();
6089 : }
6090 : }
6091 79450 : w_fx[0] = w_fx[1];
6092 79450 : move16();
6093 79450 : w_fx[LPC_SHB_ORDER - 1] = w_fx[LPC_SHB_ORDER - 2];
6094 79450 : move16();
6095 :
6096 873950 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
6097 : {
6098 794500 : tmp1 = mult( lsp_shb_1_fx[i], sub( 32767, w_fx[i] ) );
6099 794500 : tmp2 = mult( lsp_shb_2_fx[i], w_fx[i] );
6100 794500 : lsp_temp_fx[i] = add( tmp1, tmp2 );
6101 794500 : move16();
6102 : }
6103 : }
6104 : ELSE
6105 : {
6106 1984 : Copy( lsp_shb_2_fx, lsp_temp_fx, LPC_SHB_ORDER );
6107 : }
6108 :
6109 : /* convert from lsp to lsf */
6110 81434 : lsp2lsf_fx( lsp_temp_fx, lsf_shb_fx, LPC_SHB_ORDER, 1 );
6111 : }
6112 :
6113 104415 : Copy( lsf_diff_fx + 1, hBWE_TD->prev_lsf_diff_fx, LPC_SHB_ORDER - 2 );
6114 104415 : hBWE_TD->prev_tilt_para_fx = tilt_para_fx;
6115 104415 : move16();
6116 : }
6117 : ELSE
6118 : {
6119 360 : Copy( lsp_shb_2_fx, lsp_temp_fx, LPC_SHB_ORDER );
6120 : }
6121 :
6122 104775 : IF( GE_32( st->extl_brate, SWB_TBE_2k8 ) )
6123 : {
6124 : /* SHB LSP interpolation */
6125 23029 : ptr_lsp_interp_coef_fx = interpol_frac_shb; /*Q15*/
6126 115145 : FOR( j = 0; j < 4; j++ )
6127 : {
6128 1013276 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
6129 : {
6130 921160 : L_tmp = L_mult( lsp_shb_1_fx[i], ( *ptr_lsp_interp_coef_fx ) );
6131 921160 : lsp_temp_fx[i] = mac_r( L_tmp, lsp_shb_2_fx[i], ( *( ptr_lsp_interp_coef_fx + 1 ) ) );
6132 921160 : move16();
6133 : }
6134 92116 : ptr_lsp_interp_coef_fx += 2;
6135 :
6136 92116 : tmp = i_mult( j, ( LPC_SHB_ORDER + 1 ) );
6137 : /* convert LSPs to LP coefficients */
6138 92116 : E_LPC_f_lsp_a_conversion( lsp_temp_fx, lpc_shb_sf_fx + tmp, LPC_SHB_ORDER );
6139 : }
6140 : }
6141 :
6142 : /* Save the SWB LSP values from current frame for interpolation */
6143 104775 : Copy( lsp_shb_2_fx, hBWE_TD->swb_lsp_prev_interp_fx, LPC_SHB_ORDER );
6144 :
6145 : /* save the shb_ener and mixFactor values */
6146 104775 : hBWE_TD->prev3_shb_ener_sf_fx = hBWE_TD->prev2_shb_ener_sf_fx;
6147 104775 : move32();
6148 104775 : hBWE_TD->prev2_shb_ener_sf_fx = hBWE_TD->prev1_shb_ener_sf_fx;
6149 104775 : move32();
6150 104775 : hBWE_TD->prev1_shb_ener_sf_fx = shb_ener_sf_32;
6151 104775 : move32();
6152 104775 : hBWE_TD->prev_res_shb_gshape_fx = shb_res_gshape_fx[4];
6153 104775 : move16();
6154 104775 : hBWE_TD->prev_mixFactors_fx = mixFactors_fx;
6155 104775 : move16();
6156 :
6157 : /* SWB CNG/DTX - update memories */
6158 104775 : IF( st->hTdCngDec != NULL )
6159 : {
6160 76477 : Copy( st->hTdCngDec->lsp_shb_prev_fx, st->hTdCngDec->lsp_shb_prev_prev_fx, LPC_SHB_ORDER );
6161 76477 : Copy( lsf_shb_fx, st->hTdCngDec->lsp_shb_prev_fx, LPC_SHB_ORDER );
6162 : }
6163 :
6164 : /* convert LSPs back into LP coeffs */
6165 104775 : E_LPC_f_lsp_a_conversion( lsp_temp_fx, lpc_shb_fx, LPC_SHB_ORDER );
6166 104775 : Copy_Scale_sig( lpc_shb_fx, lpc_shb_fx, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_fx[0] ), 2 ) ); /* Q12 */
6167 104775 : lpc_shb_fx[0] = ONE_IN_Q12;
6168 104775 : move16();
6169 :
6170 104775 : test();
6171 104775 : IF( EQ_32( st->extl_brate, SWB_TBE_1k10 ) || EQ_32( st->extl_brate, SWB_TBE_1k75 ) )
6172 4067 : {
6173 4067 : Word32 vind_temp = Mpy_32_32( L_shl( L_add( L_deposit_l( mixFactors_fx ), 1 ), 15 ), ( ( ( 1 << NUM_BITS_SHB_VF ) - 1 ) << 16 ) ); // check addition of 1
6174 4067 : vind = extract_l( L_shr( vind_temp, 15 ) ); /* 3 for mpy by 8.0f, -15 to bring it to Q0 */ /*mixFactors*7*/
6175 : /* i: mixFactors_fx in Q15 */
6176 : /* o: vind in Q0 */
6177 : }
6178 : ELSE
6179 : {
6180 100708 : vind = shl( mixFactors_fx, 3 - 15 ); /* 3 for mpy by 8.0f, -15 to bring it to Q0 */ /*mixFactors*8*/
6181 : /* i: mixFactors_fx in Q15 */
6182 : /* o: vind in Q0 */
6183 : }
6184 :
6185 : /* Determine formant PF strength */
6186 104775 : formant_fac_fx = swb_formant_fac_fx( lpc_shb_fx[1], &hBWE_TD->tilt_mem_fx );
6187 : /* i:lpc_shb_fx Q12, o:formant_fac_fx Q15 */
6188 104775 : IF( GT_32( st->total_brate, ACELP_32k ) )
6189 : {
6190 45030 : FOR( j = 0; j < 4; j++ )
6191 : {
6192 36024 : Copy( lpc_shb_fx, &lpc_shb_sf_fx[i_mult( j, ( LPC_SHB_ORDER + 1 ) )], LPC_SHB_ORDER + 1 );
6193 : }
6194 : }
6195 :
6196 : /* From low band excitation, generate highband excitation */
6197 :
6198 : /* -------- start of memory rescaling -------- */
6199 : /* ----- calculate optimum Q_bwe_exc and rescale memories accordingly ----- */
6200 104775 : Lmax = 0;
6201 104775 : move32();
6202 68418075 : FOR( cnt = 0; cnt < L_FRAME32k + NL_BUFF_OFFSET; cnt++ )
6203 : {
6204 68313300 : Lmax = L_max( Lmax, L_abs( bwe_exc_extended_fx[cnt] ) );
6205 : }
6206 104775 : Q_bwe_exc = norm_l( Lmax );
6207 104775 : if ( Lmax == 0 )
6208 : {
6209 372 : Q_bwe_exc = 31;
6210 372 : move16();
6211 : }
6212 104775 : Q_bwe_exc = add( Q_bwe_exc, add( Q_exc, Q_exc ) );
6213 104775 : find_max_mem_dec( st, &n_mem, &n_mem2, &n_mem3 ); /* for >=24.4, use n_mem2 lpc_syn, shb_20sample, and mem_stp_swb_fx memory */
6214 :
6215 104775 : tmp = add( st->prev_Q_bwe_exc, n_mem );
6216 104775 : if ( GT_16( Q_bwe_exc, tmp ) )
6217 : {
6218 17980 : Q_bwe_exc = tmp;
6219 17980 : move16();
6220 : }
6221 :
6222 : /* rescale the memories if Q_bwe_exc is different from previous frame */
6223 104775 : sc = sub( Q_bwe_exc, st->prev_Q_bwe_exc );
6224 104775 : IF( sc != 0 )
6225 : {
6226 45328 : rescale_genSHB_mem_dec_ivas( st, sc );
6227 : }
6228 :
6229 : /* rescale the bwe_exc_extended and bring it to 16-bit single precision with dynamic norm */
6230 104775 : sc = sub( Q_bwe_exc, add( Q_exc, Q_exc ) );
6231 :
6232 68418075 : FOR( cnt = 0; cnt < L_FRAME32k + NL_BUFF_OFFSET; cnt++ )
6233 : {
6234 68313300 : bwe_exc_extended_16[cnt] = round_fx_sat( L_shl_sat( bwe_exc_extended_fx[cnt], sc ) );
6235 68313300 : move16();
6236 : }
6237 :
6238 : /* 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 */
6239 :
6240 : /* save the previous Q factor (32-bit) of the buffer */
6241 104775 : st->prev_Q_bwe_exc = Q_bwe_exc;
6242 104775 : move16();
6243 :
6244 104775 : Q_bwe_exc = sub( Q_bwe_exc, 16 ); /* Q_bwe_exc reflecting the single precision dynamic norm-ed buffers from here */
6245 :
6246 : /* -------- end of rescaling memories -------- */
6247 :
6248 104775 : Q_bwe_exc_fb = hBWE_TD->prev_Q_bwe_exc_fb;
6249 104775 : move16();
6250 :
6251 104775 : Q_shb = 0;
6252 104775 : move16();
6253 :
6254 104775 : Copy( hBWE_TD->state_syn_shbexc_fx, shaped_shb_excitation_fx, L_SHB_LAHEAD );
6255 104775 : GenShapedSHBExcitation_ivas_dec_fx( shaped_shb_excitation_fx + L_SHB_LAHEAD, lpc_shb_fx, White_exc16k_fx,
6256 104775 : hBWE_TD->mem_csfilt_fx, hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->state_lpc_syn_fx,
6257 104775 : st->coder_type, bwe_exc_extended_16, hBWE_TD->bwe_seed, vf_modified_fx, st->extl,
6258 : &( hBWE_TD->tbe_demph_fx ), &( hBWE_TD->tbe_premph_fx ), lpc_shb_sf_fx, shb_ener_sf_32,
6259 104775 : shb_res_gshape_fx, shb_res_dummy_fx, &vind, formant_fac_fx, hBWE_TD->fb_state_lpc_syn_fx,
6260 104775 : &( hBWE_TD->fb_tbe_demph_fx ), &Q_bwe_exc, &Q_bwe_exc_fb, Q_shb, n_mem2, hBWE_TD->prev_Q_bwe_syn, st->total_brate, st->prev_bfi,
6261 104775 : st->element_mode, st->flag_ACELP16k, nlExc16k_fx, mixExc16k_fx, st->extl_brate, MSFlag,
6262 : NULL, &( hBWE_TD->prev_pow_exc16kWhtnd_fx32 ), &( hBWE_TD->prev_mix_factor_fx ), NULL, NULL );
6263 :
6264 104775 : *Q_white_exc = Q_bwe_exc_fb;
6265 104775 : move16();
6266 104775 : IF( EQ_16( st->extl, FB_TBE ) )
6267 : {
6268 40715 : hBWE_TD->prev_Q_bwe_exc_fb = Q_bwe_exc_fb;
6269 40715 : move16();
6270 : }
6271 : ELSE
6272 : {
6273 : /*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.
6274 : 51 because of 31 + 20(shift of Q_bwe_exc_fb before de-emphasis)*/
6275 64060 : hBWE_TD->prev_Q_bwe_exc_fb = 51;
6276 64060 : move16();
6277 : }
6278 :
6279 : /* rescale the TBE post proc memory */
6280 1152525 : FOR( i = 0; i < LPC_SHB_ORDER; i++ )
6281 : {
6282 1047750 : hBWE_TD->mem_stp_swb_fx[i] = shl_sat( hBWE_TD->mem_stp_swb_fx[i], sub( Q_bwe_exc, hBWE_TD->prev_Q_bwe_syn ) );
6283 1047750 : move16();
6284 : }
6285 : /* fill-in missing SHB excitation */
6286 104775 : test();
6287 104775 : test();
6288 104775 : IF( ( EQ_16( st->element_mode, IVAS_CPE_TD ) || EQ_16( st->element_mode, IVAS_CPE_DFT ) ) && LE_32( st->last_core_brate, SID_2k40 ) )
6289 : {
6290 171 : Copy( shaped_shb_excitation_fx + L_SHB_LAHEAD, shaped_shb_excitation_fx, L_SHB_LAHEAD );
6291 : }
6292 :
6293 104775 : IF( hStereoICBWE != NULL )
6294 : {
6295 16460 : Copy( shaped_shb_excitation_fx + L_SHB_LAHEAD, hStereoICBWE->shbSynthRef_fx, L_FRAME16k );
6296 : }
6297 :
6298 104775 : test();
6299 104775 : IF( NE_32( st->extl_brate, SWB_TBE_1k10 ) && NE_32( st->extl_brate, SWB_TBE_1k75 ) )
6300 : {
6301 503540 : FOR( i = 0; i < L_FRAME16k; i += L_SUBFR16k )
6302 : {
6303 : /* TD BWE post-processing */
6304 402832 : PostShortTerm_ivas_dec_fx( &shaped_shb_excitation_fx[L_SHB_LAHEAD + i], lpc_shb_fx, &shaped_shb_excitationTemp_fx[i], hBWE_TD->mem_stp_swb_fx,
6305 402832 : hBWE_TD->ptr_mem_stp_swb_fx, &( hBWE_TD->gain_prec_swb_fx ), hBWE_TD->mem_zero_swb_fx, formant_fac_fx );
6306 : }
6307 :
6308 100708 : Copy( shaped_shb_excitationTemp_fx, &shaped_shb_excitation_fx[L_SHB_LAHEAD], L_FRAME16k ); /* Q_bwe_exc */
6309 :
6310 100708 : tmp = sub( shl( Q_bwe_exc, 1 ), 31 + 16 );
6311 100708 : prev_pow_fx = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /* 2*(Q_bwe_exc) */
6312 100708 : curr_pow_fx = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /* 2*(Q_bwe_exc) */
6313 3121948 : FOR( i = 0; i < L_SHB_LAHEAD + 10; i++ )
6314 : {
6315 3021240 : prev_pow_fx = L_mac0_sat( prev_pow_fx, shaped_shb_excitation_fx[i], shaped_shb_excitation_fx[i] ); /*2*Q_bwe_exc*/
6316 3021240 : curr_pow_fx = L_mac0_sat( curr_pow_fx, shaped_shb_excitation_fx[i + L_SHB_LAHEAD + 10], shaped_shb_excitation_fx[i + L_SHB_LAHEAD + 10] ); /* 2*Q_bwe_exc */
6317 : }
6318 :
6319 100708 : if ( GT_16( voice_factors_fx[0], 24576 /*0.75f Q15*/ ) )
6320 : {
6321 2964 : curr_pow_fx = L_shr( curr_pow_fx, 2 ); /* Q(2*Q_bwe_exc) */
6322 : }
6323 :
6324 100708 : Lscale = root_a_over_b_fx( curr_pow_fx, shl( Q_bwe_exc, 1 ), prev_pow_fx, shl( Q_bwe_exc, 1 ), &exp );
6325 :
6326 2114868 : FOR( i = 0; i < L_SHB_LAHEAD; i++ )
6327 : {
6328 2014160 : L_tmp = Mult_32_16( Lscale, shaped_shb_excitation_fx[i] ); /* Q_bwe_exc + (31-exp) - 15 */
6329 2014160 : shaped_shb_excitation_fx[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */
6330 2014160 : move16();
6331 : }
6332 100708 : IF( exp < 0 )
6333 : {
6334 2642 : Lscale = L_shl( Lscale, exp );
6335 2642 : exp = 0;
6336 2642 : move16();
6337 : }
6338 : /*
6339 : code for EVS and IVAS are basically identical with the exception of i_mult_sat() which has precision issues
6340 : thus is was replaced for IVAS and kept for EVS, in order to keep EVS BE to test sequences and legacy implementations
6341 : */
6342 100708 : IF( EQ_16( st->element_mode, EVS_MONO ) )
6343 : {
6344 0 : FOR( ; i < L_SHB_LAHEAD + 10; i++ )
6345 : {
6346 0 : temp_fx = i_mult_sat( sub( i, 19 ), 3277 /*0.1f Q15*/ ); /* Q15 */
6347 0 : L_tmp1 = Mult_32_16( L_shl_sat( 1, sub( 31, exp ) ), temp_fx ); /* Q31-exp */
6348 0 : temp_fx = sub( 32767 /*1.0f Q15*/, temp_fx );
6349 0 : Lscale = L_add( Mult_32_16( Lscale, temp_fx ), L_tmp1 );
6350 0 : L_tmp = Mult_32_16( Lscale, shaped_shb_excitation_fx[i] ); /* Q_bwe_exc + (31-exp) - 15 */
6351 0 : shaped_shb_excitation_fx[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */
6352 0 : move16();
6353 : }
6354 : }
6355 : ELSE
6356 : {
6357 1107788 : FOR( ; i < L_SHB_LAHEAD + 10; i++ )
6358 : {
6359 1007080 : temp_fx = round_fx_sat( L_shl( L_mult( 0x6666 /* 0.1 in Q12 */, shl( sub( i, 19 ), 11 ) ), 1 ) );
6360 1007080 : L_tmp1 = Mult_32_16( L_shl_sat( 1, sub( 31, exp ) ), temp_fx ); /* Q31-exp */
6361 1007080 : temp_fx = sub( 32767 /*1.0f Q15*/, temp_fx );
6362 1007080 : Lscale = L_add( Mult_32_16( Lscale, temp_fx ), L_tmp1 );
6363 1007080 : L_tmp = Mult_32_16( Lscale, shaped_shb_excitation_fx[i] ); /* Q_bwe_exc + (31-exp) - 15 */
6364 1007080 : shaped_shb_excitation_fx[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */
6365 1007080 : move16();
6366 : }
6367 : }
6368 : }
6369 : ELSE
6370 : {
6371 : /* reset the PF memories if the PF is not running */
6372 4067 : set16_fx( hBWE_TD->mem_stp_swb_fx, 0, LPC_SHB_ORDER );
6373 4067 : hBWE_TD->gain_prec_swb_fx = ONE_IN_Q14;
6374 4067 : move16();
6375 4067 : set16_fx( hBWE_TD->mem_zero_swb_fx, 0, LPC_SHB_ORDER );
6376 : }
6377 :
6378 : /* Update SHB excitation */
6379 104775 : Copy( shaped_shb_excitation_fx + L_FRAME16k, hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD ); /* Q_bwe_exc */
6380 104775 : l_subframe = L_FRAME16k / NUM_SHB_SUBGAINS;
6381 104775 : move16();
6382 104775 : L_ener = EPSILON_FX_SMALL;
6383 104775 : move32();
6384 :
6385 523875 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
6386 : {
6387 419100 : L_tmp = 0;
6388 419100 : move32();
6389 419100 : ener_tmp_fx[i] = EPSILON_FX_SMALL;
6390 419100 : move32();
6391 :
6392 419100 : Word64 tmp64 = 0;
6393 419100 : move64();
6394 33947100 : FOR( j = 0; j < l_subframe; j++ )
6395 : {
6396 33528000 : tmp64 = W_mac0_16_16( tmp64, shaped_shb_excitation_fx[add( i_mult( i, l_subframe ), j )], shaped_shb_excitation_fx[add( i_mult( i, l_subframe ), j )] ); /* 2*Q_bwe_exc */
6397 : }
6398 419100 : L_tmp = W_sat_l( tmp64 );
6399 :
6400 419100 : L_tmp = Mult_32_16( L_tmp, 410 /*0.0125 Q15*/ ); /* 2*Q_bwe_exc: ener_tmp_fx in (2*Q_bwe_exc) */
6401 419100 : IF( L_tmp != 0 )
6402 : {
6403 417953 : exp = norm_l( L_tmp );
6404 417953 : tmp = extract_h( L_shl( L_tmp, exp ) );
6405 417953 : exp = sub( exp, sub( 30, i_mult( 2, Q_bwe_exc ) ) );
6406 :
6407 417953 : tmp = div_s( 16384, tmp );
6408 417953 : L_tmp = L_deposit_h( tmp );
6409 417953 : L_tmp = Isqrt_lc( L_tmp, &exp );
6410 417953 : ener_tmp_fx[i] = L_shl_sat( L_tmp, sub( add( exp, shl( Q_bwe_exc, 1 ) ), 31 ) ); /*2 * Q_bwe_exc: Q31 -exp +exp +2 * Q_bwe_exc -31 */
6411 417953 : move32();
6412 417953 : L_ener = L_add_sat( L_ener, L_shr( ener_tmp_fx[i], 2 ) ); /* 2*Q_bwe_exc */
6413 : }
6414 : }
6415 104775 : ener_fx = s_max( 1, round_fx_sat( L_shl_sat( L_ener, sub( 18, shl( Q_bwe_exc, 1 ) ) ) ) ); /* Q2: 2*Q_bwe_exc+18-2*Q_bwe_exc-16 */
6416 : /* WB/SWB bandwidth switching */
6417 104775 : IF( st->bws_cnt > 0 )
6418 : {
6419 92 : IF( is_fractive == 0 )
6420 : {
6421 92 : IF( GT_16( st->tilt_wb_fx, 2048 ) ) /*assuming st->tilt_wb_fx in Q11*/
6422 : {
6423 8 : st->tilt_wb_fx = 2048;
6424 8 : move16();
6425 : }
6426 84 : ELSE IF( LT_16( st->tilt_wb_fx, 1024 ) )
6427 : {
6428 80 : st->tilt_wb_fx = 1024;
6429 80 : move16();
6430 : }
6431 92 : test();
6432 92 : if ( EQ_16( st->prev_fractive, 1 ) && GT_16( st->tilt_wb_fx, 1024 ) )
6433 : {
6434 0 : st->tilt_wb_fx = 1024;
6435 0 : move16();
6436 : }
6437 : }
6438 : ELSE
6439 : {
6440 0 : IF( GT_16( st->tilt_wb_fx, 8192 ) )
6441 : {
6442 0 : IF( st->prev_fractive == 0 )
6443 : {
6444 0 : st->tilt_wb_fx = 8192;
6445 0 : move16();
6446 : }
6447 : ELSE
6448 : {
6449 0 : st->tilt_wb_fx = 16384;
6450 0 : move16();
6451 : }
6452 : }
6453 : ELSE
6454 : {
6455 0 : st->tilt_wb_fx = shl( st->tilt_wb_fx, 2 );
6456 0 : move16();
6457 : }
6458 : }
6459 :
6460 92 : IF( ener_fx != 0 )
6461 : {
6462 92 : L_tmp = L_shl( L_mult0( ener_fx, st->tilt_wb_fx ), sub( st->Q_syn2, 13 ) ); /* 2+11 +st->Q_syn2 -13 = st->Q_syn2*/
6463 92 : exp_ener = norm_s( ener_fx );
6464 92 : tmp = shl( ener_fx, exp_ener ); /*Q(2+exp)*/
6465 92 : inv_ener = shr( div_s( 16384, tmp ), 1 ); /*Q(15+14-2-exp-1) = 26 - exp*/
6466 :
6467 92 : test();
6468 92 : IF( GT_32( L_tmp, st->enerLH_fx ) ) /*st->Q_syn2*/
6469 : {
6470 0 : st->tilt_wb_fx = extract_h( L_shr_sat( Mult_32_16( st->enerLH_fx, inv_ener ), sub( sub( st->Q_syn2, exp_ener ), 16 ) ) ); /*Q11*/
6471 0 : move16();
6472 : /*st->Q_syn2 -1 + 26- exp_ener -15 -(st->Q_syn2 -exp_ener -16 ) -16 +1 -1 = (11) *0.5*/
6473 : }
6474 92 : ELSE IF( LT_32( L_tmp, Mult_32_16( st->enerLH_fx, 1638 ) ) && EQ_16( is_fractive, 1 ) )
6475 : {
6476 0 : st->tilt_wb_fx = extract_h( L_shr_sat( Mult_32_16( st->enerLH_fx, inv_ener ), sub( sub( st->Q_syn2, exp_ener ), 15 ) ) ); /*Q11*/
6477 0 : move16();
6478 : /*st->Q_syn2 -1 + 26- exp_ener -15 -(st->Q_syn2 -exp_ener -15 ) -16 = (11) 0.25*/
6479 : }
6480 92 : L_tmp = L_mult0( st->prev_ener_shb_fx, inv_ener ); /*Q(1+15+14-3-exp_ener) = 27 -exp_ener*/
6481 92 : GainFrame_prevfrm_fx = L_shr( L_tmp, sub( 9, exp_ener ) ); /*27 -exp_ener -(9-exp_ener )= Q18*/
6482 : }
6483 : ELSE
6484 : {
6485 0 : GainFrame_prevfrm_fx = 0;
6486 0 : move32();
6487 : }
6488 :
6489 92 : IF( EQ_16( is_fractive, 1 ) )
6490 : {
6491 0 : GainFrame_fx = L_shl( L_deposit_l( st->tilt_wb_fx ), 10 );
6492 : }
6493 : ELSE
6494 : {
6495 92 : GainFrame_fx = L_shl( L_deposit_l( st->tilt_wb_fx ), 8 );
6496 : }
6497 :
6498 92 : test();
6499 92 : IF( EQ_16( ( is_fractive & st->prev_fractive ), 1 ) && GT_32( GainFrame_fx, GainFrame_prevfrm_fx ) )
6500 : {
6501 0 : GainFrame_fx = L_add( Mult_32_16( GainFrame_prevfrm_fx, 26214 ), Mult_32_16( GainFrame_fx, 6554 ) ); /* 18 +15 -15 = 18*/
6502 : }
6503 : ELSE
6504 : {
6505 92 : test();
6506 92 : test();
6507 92 : test();
6508 92 : test();
6509 92 : IF( ( LT_32( L_shr( st->prev_enerLH_fx, 1 ), st->enerLH_fx ) && GT_32( st->prev_enerLH_fx, L_shr( st->enerLH_fx, 1 ) ) ) && ( LT_32( L_shr( st->prev_enerLL_fx, 1 ), st->enerLL_fx ) && GT_32( st->prev_enerLL_fx, L_shr( st->enerLL_fx, 1 ) ) ) && ( s_xor( is_fractive, st->prev_fractive ) == 0 ) )
6510 : {
6511 0 : GainFrame_fx = L_add( L_shr( GainFrame_fx, 1 ), L_shr( GainFrame_prevfrm_fx, 1 ) );
6512 : }
6513 : ELSE
6514 : {
6515 92 : test();
6516 92 : IF( ( is_fractive == 0 ) && EQ_16( st->prev_fractive, 1 ) )
6517 : {
6518 0 : L_tmp1 = L_shl( Mult_32_16( GainFrame_fx, 3277 ), 13 ); /* 31 */
6519 0 : L_tmp = L_sub( 2147483647, L_tmp1 ); /* 31 */
6520 0 : GainFrame_fx = L_add( Mult_32_32( GainFrame_fx, L_tmp ), Mult_32_32( GainFrame_prevfrm_fx, L_tmp1 ) ); /* 18 */
6521 : }
6522 : ELSE
6523 : {
6524 92 : GainFrame_fx = L_add( L_shr( GainFrame_fx, 1 ), L_shr( L_min( GainFrame_prevfrm_fx, GainFrame_fx ), 1 ) ); /* 18 */
6525 : }
6526 : }
6527 : }
6528 :
6529 92 : GainFrame_fx = Mult_32_16( GainFrame_fx, i_mult( sub( N_WS2N_FRAMES, st->bws_cnt ), 819 ) ); /*Q18*/
6530 : }
6531 : ELSE
6532 : {
6533 104683 : IF( st->bws_cnt1 > 0 )
6534 : {
6535 268 : GainFrame_fx = Mult_32_16( GainFrame_fx, i_mult( st->bws_cnt1, 819 ) ); /*Q18*/
6536 : }
6537 104683 : IF( GE_16( st->nbLostCmpt, 1 ) )
6538 : {
6539 2142 : ener_fx = s_max( 1, ener_fx );
6540 2142 : exp_ener = norm_s( ener_fx );
6541 2142 : tmp = shl( ener_fx, exp_ener ); /*Q(2+exp)*/
6542 2142 : inv_ener = div_s( 16384, tmp ); /*Q(15+14-2-exp)*/
6543 2142 : prev_ener_ratio_fx = L_shr_sat( L_mult0( st->prev_ener_shb_fx, inv_ener ), add( sub( 9, exp_ener ), 1 ) ); /*Q: 1+27-exp-9+exp-1 = 18 */
6544 : }
6545 :
6546 104683 : IF( EQ_16( st->nbLostCmpt, 1 ) )
6547 : {
6548 1196 : test();
6549 1196 : test();
6550 1196 : test();
6551 1196 : test();
6552 1196 : test();
6553 1196 : test();
6554 1196 : test();
6555 1196 : test();
6556 1196 : test();
6557 1196 : test();
6558 1196 : IF( ( st->clas_dec != UNVOICED_CLAS ) && NE_16( st->clas_dec, UNVOICED_TRANSITION ) && LT_16( hBWE_TD->tilt_swb_fec_fx, 16384 ) &&
6559 : ( ( GT_32( st->enerLL_fx, L_shr( st->prev_enerLL_fx, 1 ) ) && LT_32( L_shr( st->enerLL_fx, 1 ), st->prev_enerLL_fx ) ) || ( GT_32( st->enerLH_fx, L_shr( st->prev_enerLH_fx, 1 ) ) && LT_32( L_shr( st->enerLH_fx, 1 ), st->prev_enerLH_fx ) ) ) )
6560 : {
6561 0 : IF( GT_32( L_shr( prev_ener_ratio_fx, 2 ), GainFrame_fx ) ) /*18*/
6562 : {
6563 0 : GainFrame_fx = L_add( Mult_32_16( prev_ener_ratio_fx, 13107 ), Mult_32_16( GainFrame_fx, 19661 ) ); /*18*/
6564 : }
6565 0 : ELSE IF( GT_32( L_shr( prev_ener_ratio_fx, 1 ), GainFrame_fx ) )
6566 : {
6567 0 : GainFrame_fx = L_add( Mult_32_16( prev_ener_ratio_fx, 26214 ), Mult_32_16( GainFrame_fx, 6554 ) );
6568 : }
6569 : ELSE
6570 : {
6571 0 : GainFrame_fx = L_add( Mult_32_16( prev_ener_ratio_fx, 6554 ), Mult_32_16( GainFrame_fx, 26214 ) );
6572 : }
6573 :
6574 0 : test();
6575 0 : IF( GT_16( tilt_swb_fec_fx, hBWE_TD->tilt_swb_fec_fx ) && ( hBWE_TD->tilt_swb_fec_fx > 0 ) )
6576 : {
6577 0 : exp = norm_s( hBWE_TD->tilt_swb_fec_fx );
6578 0 : tmp = shl( hBWE_TD->tilt_swb_fec_fx, exp ); /*Q(11+exp)*/
6579 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-11-exp)*/
6580 0 : tmp = extract_h( L_shl( L_mult0( tmp, st->tilt_wb_fx ), sub( exp, 1 ) ) ); /*18 -exp +11 + exp -1 -16 =12; */
6581 0 : GainFrame_fx = L_shl( Mult_32_16( GainFrame_fx, s_min( tmp, 20480 ) ), 3 ); /*Q18 = 18 +12 -15 +3 */
6582 : }
6583 : }
6584 1196 : ELSE IF( ( ( st->clas_dec != UNVOICED_CLAS ) || GT_16( hBWE_TD->tilt_swb_fec_fx, 4096 ) ) && GT_32( L_shr( prev_ener_ratio_fx, 2 ), GainFrame_fx ) &&
6585 : ( GT_32( st->enerLL_fx, L_shr( st->prev_enerLL_fx, 1 ) ) || GT_32( st->enerLH_fx, L_shr( st->prev_enerLH_fx, 1 ) ) ) )
6586 : {
6587 0 : GainFrame_fx = L_add( Mult_32_16( prev_ener_ratio_fx, 6554 ), Mult_32_16( GainFrame_fx, 26214 ) );
6588 : }
6589 : }
6590 103487 : ELSE IF( GT_16( st->nbLostCmpt, 1 ) )
6591 : {
6592 946 : test();
6593 946 : test();
6594 946 : test();
6595 946 : test();
6596 946 : test();
6597 946 : test();
6598 946 : test();
6599 946 : test();
6600 946 : IF( GT_32( L_shr( prev_ener_ratio_fx, 2 ), GainFrame_fx ) && ( ( EQ_16( st->codec_mode, MODE1 ) && GT_32( st->enerLL_fx, st->prev_enerLL_fx ) && GT_32( st->enerLH_fx, st->prev_enerLH_fx ) ) || EQ_16( st->codec_mode, MODE2 ) ) )
6601 : {
6602 0 : test();
6603 0 : IF( GT_16( tilt_swb_fec_fx, 20480 /*10.0f in Q11*/ ) && GT_16( hBWE_TD->tilt_swb_fec_fx, 20480 /*10.0f in Q11*/ ) )
6604 : {
6605 0 : GainFrame_fx = L_min( L_add( Mult_32_16( prev_ener_ratio_fx, 26214 /*0.8f in Q15*/ ), Mult_32_16( GainFrame_fx, 6554 /*0.2f in Q15*/ ) ), L_shl( Mult_32_16( GainFrame_fx, 16384 /*4.0f in Q12*/ ), 3 ) ); /*Q18*/
6606 : }
6607 : ELSE
6608 : {
6609 0 : GainFrame_fx = L_min( L_add( Mult_32_16( prev_ener_ratio_fx, 16384 ), Mult_32_16( GainFrame_fx, 16384 ) ), L_shl( Mult_32_16( GainFrame_fx, 16384 ), 3 ) ); /*Q18*/
6610 : }
6611 : }
6612 946 : ELSE IF( GT_32( prev_ener_ratio_fx, GainFrame_fx ) && ( ( EQ_16( st->codec_mode, MODE1 ) && GT_32( st->enerLL_fx, st->prev_enerLL_fx ) && GT_32( st->enerLH_fx, st->prev_enerLH_fx ) ) || EQ_16( st->codec_mode, MODE2 ) ) )
6613 : {
6614 0 : test();
6615 0 : IF( GT_16( tilt_swb_fec_fx, 20480 ) && GT_16( hBWE_TD->tilt_swb_fec_fx, 20480 ) )
6616 : {
6617 0 : GainFrame_fx = L_add( Mult_32_16( prev_ener_ratio_fx, 16384 /*0.5f in Q15*/ ), Mult_32_16( GainFrame_fx, 16384 /*0.5f in Q15*/ ) ); /* Q18 */
6618 : }
6619 : ELSE
6620 : {
6621 0 : GainFrame_fx = L_add( Mult_32_16( prev_ener_ratio_fx, 6554 /*0.2f in Q15*/ ), Mult_32_16( GainFrame_fx, 26214 /*0.8f in Q15*/ ) ); /* Q18 */
6622 : }
6623 : }
6624 : }
6625 : }
6626 :
6627 104775 : st->prev_fractive = is_fractive;
6628 104775 : move16();
6629 :
6630 : /* Adjust the subframe and frame gain of the synthesized shb signal */
6631 : /* Scale the shaped excitation */
6632 104775 : IF( EQ_16( st->L_frame, L_FRAME ) )
6633 : {
6634 49023 : L_tmp = L_mult( pitch_buf_fx[0], 8192 );
6635 196092 : FOR( i = 1; i < NB_SUBFR; i++ )
6636 : {
6637 147069 : L_tmp = L_mac( L_tmp, pitch_buf_fx[i], 8192 ); /* pitch_buf in Q6 x 0.25 in Q15 */
6638 : }
6639 49023 : pitch_fx = round_fx( L_tmp ); /* Q6 */
6640 : }
6641 : ELSE
6642 : {
6643 55752 : L_tmp = L_mult( pitch_buf_fx[0], 6554 );
6644 278760 : FOR( i = 1; i < NB_SUBFR16k; i++ )
6645 : {
6646 223008 : L_tmp = L_mac( L_tmp, pitch_buf_fx[i], 6554 ); /* pitch_buf in Q6 x 0.2 in Q15 */
6647 : }
6648 55752 : pitch_fx = round_fx( L_tmp ); /* Q6 */
6649 : }
6650 :
6651 104775 : test();
6652 104775 : test();
6653 104775 : test();
6654 104775 : test();
6655 104775 : test();
6656 104775 : test();
6657 104775 : test();
6658 104775 : test();
6659 104775 : test();
6660 104775 : test();
6661 104775 : test();
6662 104775 : test();
6663 104775 : IF( ( ( GE_32( st->extl_brate, SWB_TBE_2k8 ) && EQ_16( st->prev_coder_type, st->coder_type ) && NE_16( st->coder_type, UNVOICED ) ) || ( LT_32( st->extl_brate, SWB_TBE_2k8 ) && ( EQ_16( st->prev_coder_type, st->coder_type ) || ( EQ_16( st->prev_coder_type, VOICED ) && EQ_16( st->coder_type, GENERIC ) ) || ( EQ_16( st->prev_coder_type, GENERIC ) && EQ_16( st->coder_type, VOICED ) ) ) ) ) && GT_16( pitch_fx, 4480 /*70 in Q6*/ ) && LT_16( st->extl, FB_TBE ) && NE_32( st->extl_brate, SWB_TBE_1k10 ) && NE_32( st->extl_brate, SWB_TBE_1k75 ) )
6664 : {
6665 147560 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
6666 : {
6667 118048 : GainShape_tmp_fx[i] = GainShape_fx[i * 4]; /* Q15 */
6668 118048 : move16();
6669 : }
6670 :
6671 147560 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
6672 : {
6673 118048 : L_tmp1 = Mult_32_16( ener_tmp_fx[i], GainShape_tmp_fx[i] ); /* (2*Q_bwe_exc) */
6674 118048 : L_tmp2 = Mult_32_16( hBWE_TD->prev_ener_fx, hBWE_TD->prev_GainShape_fx ); /* (2*st->prev_ener_fx_Q) */
6675 118048 : tmp = sub( shl( Q_bwe_exc, 1 ), shl( hBWE_TD->prev_ener_fx_Q, 1 ) );
6676 118048 : L_tmp2 = L_shl_sat( L_tmp2, tmp ); /* new Q = (2*Q_bwe_exc) */
6677 118048 : IF( GT_32( L_tmp1, L_tmp2 ) )
6678 : {
6679 69311 : L_tmp = L_tmp2;
6680 69311 : move32();
6681 69311 : if ( L_tmp2 < 0 )
6682 : {
6683 0 : L_tmp = L_negate( L_tmp2 );
6684 : }
6685 :
6686 69311 : expb = norm_l( L_tmp );
6687 69311 : fracb = round_fx_sat( L_shl_sat( L_tmp, expb ) );
6688 69311 : expb = sub( 30, expb ); /* - (2*Q_bwe_exc_ext); */
6689 :
6690 69311 : expa = norm_l( ener_tmp_fx[i] );
6691 69311 : fraca = extract_h( L_shl( ener_tmp_fx[i], expa ) );
6692 69311 : expa = sub( 30, expa );
6693 :
6694 69311 : scale_fx = shr( sub( fraca, fracb ), 15 );
6695 69311 : fracb = shl( fracb, scale_fx );
6696 69311 : expb = sub( expb, scale_fx );
6697 :
6698 69311 : tmp = div_s( fracb, fraca );
6699 69311 : exp = sub( sub( expb, expa ), 1 );
6700 69311 : tmp = shl( tmp, exp );
6701 69311 : GainShape_tmp_fx[i] = add( tmp, shr( GainShape_tmp_fx[i], 1 ) ); /* Q15 */
6702 69311 : move16();
6703 : }
6704 :
6705 118048 : hBWE_TD->prev_ener_fx = ener_tmp_fx[i];
6706 118048 : move32();
6707 118048 : hBWE_TD->prev_GainShape_fx = GainShape_tmp_fx[i];
6708 118048 : move16();
6709 118048 : hBWE_TD->prev_ener_fx_Q = Q_bwe_exc;
6710 118048 : move16();
6711 : }
6712 :
6713 501704 : FOR( i = 0; i < NUM_SHB_SUBFR; i++ )
6714 : {
6715 472192 : Word16 idx = 0;
6716 472192 : move16();
6717 472192 : IF( i != 0 )
6718 : {
6719 442680 : idx = shr( i_mult( i, NUM_SHB_SUBGAINS ), NUM_SHB_SUBFR_LOG2 );
6720 : }
6721 472192 : GainShape_fx[i] = GainShape_tmp_fx[idx];
6722 472192 : move16();
6723 : }
6724 : }
6725 : ELSE
6726 : {
6727 75263 : hBWE_TD->prev_ener_fx_Q = Q_bwe_exc;
6728 75263 : move16();
6729 : }
6730 104775 : hBWE_TD->prev_Q_bwe_syn = Q_bwe_exc;
6731 104775 : move16();
6732 :
6733 :
6734 : /* Gain shape smoothing after quantization */
6735 104775 : test();
6736 104775 : IF( EQ_32( st->extl_brate, SWB_TBE_1k10 ) || EQ_32( st->extl_brate, SWB_TBE_1k75 ) )
6737 : {
6738 20335 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
6739 : {
6740 16268 : GainShape_tmp_fx[i] = GainShape_fx[i * NUM_SHB_SUBGAINS];
6741 16268 : move16();
6742 : }
6743 :
6744 4067 : lls_interp_n_fx( GainShape_tmp_fx, NUM_SHB_SUBGAINS, &GainShape_tilt_fx, &temp_fx, 1 );
6745 :
6746 4067 : test();
6747 4067 : IF( GE_16( vind, 6 ) && LT_16( abs_s( GainShape_tilt_fx ), 3932 ) )
6748 : {
6749 2422 : feedback_fx = 9830;
6750 2422 : move16();
6751 12110 : FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
6752 : {
6753 9688 : GainShape_fx[i] = add_sat( mult( sub( 32767, feedback_fx ), GainShape_fx[i * NUM_SHB_SUBGAINS] ), mult( feedback_fx, GainShape_tmp_fx[i] ) );
6754 9688 : move16();
6755 : }
6756 :
6757 38752 : FOR( i = NUM_SHB_SUBFR - 1; i > 0; i-- )
6758 : {
6759 36330 : Word16 idx = 0;
6760 36330 : move16();
6761 36330 : IF( i != 0 )
6762 : {
6763 36330 : idx = shr( i_mult( i, NUM_SHB_SUBGAINS ), NUM_SHB_SUBFR_LOG2 );
6764 : }
6765 36330 : GainShape_fx[i] = GainShape_fx[idx];
6766 36330 : move16();
6767 : }
6768 : }
6769 : }
6770 :
6771 : /* fil-in missing memory */
6772 104775 : test();
6773 104775 : test();
6774 104775 : IF( ( EQ_16( st->element_mode, IVAS_CPE_TD ) || EQ_16( st->element_mode, IVAS_CPE_DFT ) ) && LE_32( st->last_core_brate, SID_2k40 ) )
6775 : {
6776 3591 : FOR( i = 0; i < L_SHB_LAHEAD; i++ )
6777 : {
6778 3420 : Word16 intermediate = mult( shaped_shb_excitation_fx[i], subwin_shb_fx[L_SHB_LAHEAD - i] );
6779 3420 : Word32 intermediate_32 = Mpy_32_16_1( Mpy_32_16_1( GainFrame_fx, window_shb_fx[L_SHB_LAHEAD - 1 - i] ), intermediate );
6780 3420 : hBWE_TD->syn_overlap_fx[i] = round_fx( L_shl_sat( intermediate_32, sub( 16, ( add( Q_bwe_exc, 18 - 15 ) ) ) ) );
6781 3420 : move16();
6782 : }
6783 : }
6784 :
6785 104775 : Word16 n_mem3_new = 0;
6786 104775 : move16();
6787 104775 : find_max_mem_dec_m3( st, &n_mem3_new );
6788 :
6789 104775 : ScaleShapedSHB_fx( SHB_OVERLAP_LEN,
6790 : shaped_shb_excitation_fx, /* i/o: Q_bwe_exc */
6791 104775 : hBWE_TD->syn_overlap_fx,
6792 : GainShape_fx, /* Q15 */
6793 : GainFrame_fx, /* Q18 */
6794 : window_shb_fx,
6795 : subwin_shb_fx,
6796 104775 : &Q_bwe_exc, &Qx, n_mem3_new, hBWE_TD->prev_Q_bwe_syn2 );
6797 :
6798 104775 : IF( hStereoICBWE != NULL )
6799 : {
6800 16460 : Copy_Scale_sig_16_32_DEPREC( lpc_shb_fx, hStereoICBWE->lpSHBRef_fx, LPC_SHB_ORDER + 1, 0 );
6801 16460 : Copy( GainShape_fx, hStereoICBWE->gshapeRef_fx, NUM_SHB_SUBFR );
6802 16460 : hStereoICBWE->gFrameRef_fx = GainFrame_fx;
6803 16460 : move32();
6804 :
6805 16460 : Copy( shaped_shb_excitation_fx, hStereoICBWE->shbSynthRef_fx, L_FRAME16k );
6806 : }
6807 :
6808 104775 : max_val = 0;
6809 104775 : move16();
6810 33632775 : FOR( i = 0; i < L_FRAME16k; i++ )
6811 : {
6812 33528000 : max_val = s_max( max_val, abs_s( shaped_shb_excitation_fx[i] ) ); /* Q0 */
6813 : }
6814 104775 : IF( max_val == 0 )
6815 : {
6816 128 : curr_frame_pow_fx = 0;
6817 128 : move32();
6818 128 : n = 0;
6819 128 : move16();
6820 : }
6821 : ELSE
6822 : {
6823 104647 : n = norm_s( max_val );
6824 104647 : max_val = 0;
6825 104647 : move16();
6826 33591687 : FOR( i = 0; i < L_FRAME16k; i++ )
6827 : {
6828 33487040 : shaped_shb_excitation_frac[i] = shl_sat( shaped_shb_excitation_fx[i], n ); /*Q_bwe_exc+n*/
6829 33487040 : move16();
6830 : }
6831 :
6832 104647 : curr_frame_pow_fx = 0;
6833 104647 : move32();
6834 33591687 : FOR( i = 0; i < L_FRAME16k; i++ )
6835 : {
6836 33487040 : L_tmp = L_mult0( shaped_shb_excitation_frac[i], shaped_shb_excitation_frac[i] ); /*2*(Q_bwe_exc+n)*/
6837 33487040 : curr_frame_pow_fx = L_add( curr_frame_pow_fx, L_shr( L_tmp, 9 ) ); /*2*(Q_bwe_exc+n)-9*/
6838 : }
6839 : }
6840 104775 : curr_frame_pow_exp = sub( shl( add( Q_bwe_exc, n ), 1 ), 9 );
6841 104775 : tmp = sub( hBWE_TD->prev_frame_pow_exp, curr_frame_pow_exp );
6842 104775 : IF( tmp > 0 ) /* shifting prev */
6843 : {
6844 921 : IF( GT_16( tmp, 32 ) )
6845 : {
6846 0 : hBWE_TD->prev_frame_pow_exp = add( curr_frame_pow_exp, 32 );
6847 0 : move16();
6848 0 : tmp = 32;
6849 0 : move16();
6850 : }
6851 921 : hBWE_TD->prev_swb_bwe_frame_pow_fx = L_shr( hBWE_TD->prev_swb_bwe_frame_pow_fx, tmp );
6852 921 : move32();
6853 921 : hBWE_TD->prev_frame_pow_exp = curr_frame_pow_exp;
6854 921 : move16();
6855 : }
6856 : ELSE /* shifting curr */
6857 : {
6858 103854 : IF( LT_16( tmp, -32 ) )
6859 : {
6860 260 : curr_frame_pow_exp = sub( hBWE_TD->prev_frame_pow_exp, 32 );
6861 260 : tmp = -32;
6862 260 : move16();
6863 : }
6864 103854 : curr_frame_pow_fx = L_shr( curr_frame_pow_fx, -tmp );
6865 103854 : curr_frame_pow_exp = hBWE_TD->prev_frame_pow_exp;
6866 103854 : move16();
6867 : }
6868 104775 : test();
6869 104775 : test();
6870 104775 : IF( !st->bfi && ( st->prev_bfi || st->prev_use_partial_copy ) )
6871 : {
6872 1216 : test();
6873 1216 : test();
6874 1216 : IF( ( GT_32( L_shr( curr_frame_pow_fx, 1 ), hBWE_TD->prev_swb_bwe_frame_pow_fx ) ) &&
6875 : ( GT_32( hBWE_TD->prev_swb_bwe_frame_pow_fx, L_tmp ) ) && EQ_16( st->prev_coder_type, UNVOICED ) )
6876 : {
6877 0 : L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow_fx, curr_frame_pow_exp, &exp );
6878 0 : scale_fx = round_fx( L_shl( L_tmp, exp ) ); /*Q15*/
6879 :
6880 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
6881 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
6882 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
6883 0 : temp_fx = round_fx( L_shl( L_tmp, exp ) ); /*Q15*/
6884 : }
6885 : ELSE
6886 : {
6887 1216 : scale_fx = temp_fx = 32767;
6888 1216 : move16(); /*Q15*/
6889 1216 : move16(); /*Q15*/
6890 : }
6891 :
6892 10944 : FOR( j = 0; j < 8; j++ )
6893 : {
6894 9728 : GainShape_fx[2 * j] = mult_r( GainShape_fx[2 * j], scale_fx );
6895 9728 : move16();
6896 9728 : GainShape_fx[2 * j + 1] = mult_r( GainShape_fx[2 * j + 1], scale_fx );
6897 9728 : move16();
6898 398848 : FOR( i = 0; i < L_FRAME16k / 8; i++ )
6899 : {
6900 389120 : shaped_shb_excitation_fx[add( i, j * ( L_FRAME16k / 8 ) )] = mult_r( shaped_shb_excitation_fx[add( i, j * ( L_FRAME16k / 8 ) )], scale_fx );
6901 389120 : move16();
6902 : }
6903 :
6904 9728 : IF( temp_fx > 0 )
6905 : {
6906 : /* scale_fx <= temp_fx, due to scale_fx = sqrt( st->prev_swb_bwe_frame_pow_fx/curr_frame_pow_fx ), temp_fx = sqrt( scale_fx, 1.f/8.f )
6907 : and curr_frame_pow_fx > st->prev_swb_bwe_frame_pow_fx -> scale_fx <= 1.0, sqrt(scale_fx, 1.f/8.f) >= scale_fx */
6908 9728 : IF( LT_16( scale_fx, temp_fx ) )
6909 : {
6910 0 : scale_fx = div_s( scale_fx, temp_fx );
6911 : }
6912 : ELSE
6913 : {
6914 9728 : scale_fx = 32767;
6915 9728 : move16();
6916 : }
6917 : }
6918 : ELSE
6919 : {
6920 0 : scale_fx = 0;
6921 0 : move16();
6922 : }
6923 : }
6924 : }
6925 :
6926 : /* adjust the FEC frame energy */
6927 104775 : IF( st->bfi )
6928 : {
6929 2154 : scale_fx = temp_fx = 4096;
6930 2154 : move16(); /*Q12*/
6931 2154 : move16();
6932 2154 : IF( EQ_16( st->nbLostCmpt, 1 ) )
6933 : {
6934 1205 : test();
6935 1205 : test();
6936 1205 : test();
6937 1205 : test();
6938 1205 : test();
6939 1205 : test();
6940 1205 : test();
6941 1205 : test();
6942 1205 : IF( GT_32( curr_frame_pow_fx, hBWE_TD->prev_swb_bwe_frame_pow_fx ) &&
6943 : NE_16( st->prev_coder_type, UNVOICED ) &&
6944 : ( st->last_good != UNVOICED_CLAS ) )
6945 : {
6946 82 : L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow_fx, curr_frame_pow_exp, &exp ); /*31 - exp*/
6947 82 : scale_fx = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
6948 82 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
6949 82 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
6950 82 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
6951 82 : temp_fx = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
6952 : }
6953 1123 : ELSE IF( LT_32( curr_frame_pow_fx, L_shr( hBWE_TD->prev_swb_bwe_frame_pow_fx, 1 ) ) && EQ_16( st->nbLostCmpt, 1 ) &&
6954 : ( GT_32( st->enerLL_fx, L_shr( st->prev_enerLL_fx, 1 ) ) || GT_32( st->enerLH_fx, L_shr( st->prev_enerLH_fx, 1 ) ) ) &&
6955 : ( EQ_16( st->prev_coder_type, UNVOICED ) || ( st->last_good == UNVOICED_CLAS ) || GT_16( hBWE_TD->tilt_swb_fec_fx, 10240 ) ) )
6956 : {
6957 0 : L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow_fx, curr_frame_pow_exp, &exp );
6958 0 : scale_fx = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
6959 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
6960 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
6961 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
6962 0 : temp_fx = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
6963 : }
6964 : }
6965 949 : ELSE IF( GT_16( st->nbLostCmpt, 1 ) )
6966 : {
6967 949 : test();
6968 949 : test();
6969 949 : test();
6970 949 : test();
6971 949 : test();
6972 949 : IF( GT_32( curr_frame_pow_fx, hBWE_TD->prev_swb_bwe_frame_pow_fx ) )
6973 : {
6974 224 : L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow_fx, curr_frame_pow_exp, &exp );
6975 224 : scale_fx = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
6976 224 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
6977 224 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
6978 224 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
6979 224 : temp_fx = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
6980 : }
6981 725 : ELSE IF( LT_32( curr_frame_pow_fx, L_shr( hBWE_TD->prev_swb_bwe_frame_pow_fx, 1 ) ) &&
6982 : ( GT_32( st->enerLL_fx, L_shr( st->prev_enerLL_fx, 1 ) ) || GT_32( st->enerLH_fx, L_shr( st->prev_enerLH_fx, 1 ) ) ) &&
6983 : ( EQ_16( st->prev_coder_type, UNVOICED ) || ( st->last_good == UNVOICED_CLAS ) || GT_16( hBWE_TD->tilt_swb_fec_fx, 10240 ) ) )
6984 : {
6985 0 : L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow_fx, curr_frame_pow_exp, &exp );
6986 0 : L_tmp = L_min( L_tmp, L_shl_sat( 2, sub( 31, exp ) ) ); /*31 - exp*/
6987 0 : scale_fx = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
6988 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
6989 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
6990 0 : L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
6991 0 : temp_fx = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
6992 : }
6993 : }
6994 :
6995 19386 : FOR( j = 0; j < 8; j++ )
6996 : {
6997 17232 : GainShape_fx[2 * j] = shl_sat( mult_r( GainShape_fx[2 * j], scale_fx ), 3 );
6998 17232 : move16(); /* 15 +12 +3-15 =15*/
6999 17232 : GainShape_fx[add( 2 * j, 1 )] = shl_sat( mult_r( GainShape_fx[add( 2 * j, 1 )], scale_fx ), 3 );
7000 17232 : move16();
7001 706512 : FOR( i = 0; i < 40; i++ )
7002 : {
7003 689280 : shaped_shb_excitation_fx[i + j * L_FRAME16k / 8] = shl( mult_r( shaped_shb_excitation_fx[i + j * L_FRAME16k / 8], scale_fx ), 3 );
7004 689280 : move16(); /* Q_bwe_exc +12+3 -15 =Q_bwe_exc*/
7005 : }
7006 :
7007 17232 : IF( temp_fx > 0 )
7008 : {
7009 17232 : IF( LT_16( scale_fx, temp_fx ) )
7010 : {
7011 2432 : scale_fx = shr( div_s( scale_fx, temp_fx ), 3 );
7012 : }
7013 : ELSE
7014 : {
7015 14800 : tmp1 = sub( norm_s( scale_fx ), 1 );
7016 14800 : tmp2 = norm_s( temp_fx );
7017 14800 : scale_fx = div_s( shl( scale_fx, tmp1 ), shl( temp_fx, tmp2 ) );
7018 14800 : scale_fx = shr( scale_fx, add( sub( tmp1, tmp2 ), 3 ) );
7019 : }
7020 : }
7021 : ELSE
7022 : {
7023 0 : scale_fx = 0;
7024 0 : move16();
7025 : }
7026 : }
7027 : }
7028 :
7029 104775 : hBWE_TD->prev_swb_bwe_frame_pow_fx = curr_frame_pow_fx;
7030 104775 : move32();
7031 104775 : hBWE_TD->prev_frame_pow_exp = curr_frame_pow_exp;
7032 104775 : move16();
7033 :
7034 104775 : Word64 prev_ener_shb64 = 0;
7035 104775 : move64();
7036 33632775 : FOR( i = 0; i < L_FRAME16k; i++ )
7037 : {
7038 33528000 : prev_ener_shb64 = W_mac0_16_16( prev_ener_shb64, shaped_shb_excitation_fx[i], shaped_shb_excitation_fx[i] ); /* Q0 */
7039 : }
7040 104775 : L_prev_ener_shb = W_sat_l( prev_ener_shb64 );
7041 :
7042 104775 : L_prev_ener_shb = Mult_32_16( L_prev_ener_shb, 26214 ); /* 2*Q_bwe_exc_mod+8; 26214=(1/L_FRAME16k) in Q23 */
7043 104775 : st->prev_ener_shb_fx = 0;
7044 104775 : move16();
7045 104775 : IF( L_prev_ener_shb != 0 )
7046 : {
7047 104628 : exp = norm_l( L_prev_ener_shb );
7048 104628 : tmp = extract_h( L_shl( L_prev_ener_shb, exp ) );
7049 104628 : exp = sub( exp, sub( 30, ( add( i_mult( 2, Q_bwe_exc ), 8 ) ) ) );
7050 :
7051 104628 : tmp = div_s( 16384, tmp );
7052 104628 : L_tmp = L_deposit_h( tmp );
7053 104628 : L_tmp = Isqrt_lc( L_tmp, &exp );
7054 104628 : st->prev_ener_shb_fx = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 14 ) ) ); /* Q1 */
7055 104628 : move16();
7056 : }
7057 :
7058 104775 : IF( st->hBWE_FD != NULL )
7059 : {
7060 104775 : L_tmp = Mult_32_16( curr_frame_pow_fx, 26214 ); /* curr_frame_pow_exp+8; 26214=(1/L_FRAME16k) in Q23 */
7061 104775 : tmp = 0;
7062 104775 : move16();
7063 104775 : IF( L_tmp != 0 )
7064 : {
7065 103903 : exp = norm_l( L_tmp );
7066 103903 : tmp = extract_h( L_shl( L_tmp, exp ) );
7067 103903 : exp = sub( exp, sub( 30, add( curr_frame_pow_exp, 8 ) ) );
7068 :
7069 103903 : tmp = div_s( 16384, tmp );
7070 103903 : L_tmp = L_deposit_h( tmp );
7071 103903 : L_tmp = Isqrt_lc( L_tmp, &exp );
7072 103903 : tmp = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 14 ) ) ); /* Q1 */
7073 : }
7074 104775 : set16_fx( st->hBWE_FD->prev_SWB_fenv_fx, tmp, SWB_FENV ); /* Q1 */
7075 : }
7076 :
7077 33632775 : FOR( i = 0; i < L_FRAME16k; i++ )
7078 : {
7079 33528000 : shaped_shb_excitation_fx_32[i] = L_shl( shaped_shb_excitation_fx[i], sub( Q11, Q_bwe_exc ) );
7080 33528000 : move32();
7081 : }
7082 :
7083 : /* generate 32kHz SHB synthesis from 12.8(16)kHz signal */
7084 104775 : GenSHBSynth_fx32( shaped_shb_excitation_fx_32, error_fx, hBWE_TD->genSHBsynth_Hilbert_Mem_fx, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx_32, st->L_frame, &( hBWE_TD->syn_dm_phase ) );
7085 104775 : Copy_Scale_sig_32_16( st->hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx_32, st->hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx, 2 * ALLPASSSECTIONS_STEEP, -( Q11 - Q_bwe_exc ) );
7086 104775 : Copy32( error_fx + L_FRAME32k - L_SHB_TRANSITION_LENGTH, hBWE_TD->old_tbe_synth_fx_32, L_SHB_TRANSITION_LENGTH );
7087 :
7088 : /* resample SHB synthesis (if needed) and scale down */
7089 104775 : synth_scale_fx = 32767;
7090 104775 : move16(); /* 1.0 in Q15 */
7091 104775 : if ( EQ_16( st->codec_mode, MODE1 ) )
7092 : {
7093 104775 : synth_scale_fx = 29491;
7094 104775 : move16(); /* 0.9 in Q15 */
7095 : }
7096 :
7097 104775 : IF( EQ_32( st->output_Fs, 48000 ) )
7098 : {
7099 72787 : IF( EQ_16( st->extl, FB_TBE ) )
7100 : {
7101 35233 : tmp = norm_l( GainFrame_fx );
7102 35233 : if ( GainFrame_fx == 0 )
7103 : {
7104 0 : tmp = 31;
7105 0 : move16();
7106 : }
7107 35233 : L_tmp = L_shl( GainFrame_fx, tmp ); /* 18 + tmp */
7108 :
7109 35233 : tmp1 = 0;
7110 35233 : move16();
7111 :
7112 35233 : Word32 idx32 = L_shr_r( 0x00333333, 10 ); /*NUM_SHB_SUBFR/L_FRAME16k*/ // Q16
7113 :
7114 11309793 : FOR( i = 0; i < L_FRAME16k; i++ )
7115 : {
7116 : Word16 idx;
7117 11274560 : idx = extract_h( imult3216( idx32, i ) ); /*Q0*/
7118 11274560 : L_tmp1 = Mult_32_16( L_tmp, GainShape_fx[idx] ); /* Q : 18 + tmp +15 -15*/
7119 11274560 : White_exc16k_fx[i] = round_fx( Mult_32_16( L_tmp1, White_exc16k_fx[i] ) ); /* 18 + tmp +*Q_white_exc -15 -16 */
7120 11274560 : move16();
7121 11274560 : tmp1 = s_max( tmp1, abs_s( White_exc16k_fx[i] ) );
7122 : }
7123 :
7124 35233 : *Q_white_exc = sub( add( *Q_white_exc, tmp ), 13 ); /* *Q_white_exc + 18 + tmp -15 -16 */
7125 35233 : move16();
7126 35233 : tmp = norm_s( tmp1 );
7127 35233 : if ( tmp1 == 0 )
7128 : {
7129 22 : tmp = 15;
7130 22 : move16();
7131 : }
7132 :
7133 11309793 : FOR( i = 0; i < L_FRAME16k; i++ )
7134 : {
7135 11274560 : White_exc16k_fx[i] = shl( White_exc16k_fx[i], sub( tmp, 1 ) );
7136 11274560 : move16();
7137 : }
7138 35233 : *Q_white_exc = sub( add( *Q_white_exc, tmp ), 1 );
7139 35233 : move16();
7140 : }
7141 :
7142 72787 : IF( NE_16( synth_scale_fx, 32767 ) ) /* 1.0 in Q15 */
7143 : {
7144 46656467 : FOR( i = 0; i < L_FRAME32k; i++ )
7145 : {
7146 46583680 : error_fx[i] = Mpy_32_16_1( error_fx[i], synth_scale_fx );
7147 46583680 : move32();
7148 : }
7149 : }
7150 72787 : interpolate_3_over_2_allpass_fx32( error_fx, L_FRAME32k, synth_fx, hBWE_TD->int_3_over_2_tbemem_dec_fx_32 );
7151 : }
7152 31988 : ELSE IF( EQ_32( st->output_Fs, 32000 ) )
7153 : {
7154 27641 : IF( NE_16( synth_scale_fx, 32767 ) ) /* 1.0 in Q15 */
7155 : {
7156 17717881 : FOR( i = 0; i < L_FRAME32k; i++ )
7157 : {
7158 17690240 : synth_fx[i] = Mpy_32_16_1( error_fx[i], synth_scale_fx );
7159 17690240 : move32(); /*Qx*/
7160 : }
7161 : }
7162 : ELSE
7163 : {
7164 0 : Copy32( error_fx, synth_fx, L_FRAME32k );
7165 : }
7166 : }
7167 4347 : ELSE IF( EQ_32( st->output_Fs, 16000 ) )
7168 : {
7169 4347 : IF( NE_16( synth_scale_fx, 32767 ) ) /* 1.0 in Q15 */
7170 : {
7171 2786427 : FOR( i = 0; i < L_FRAME32k; i++ )
7172 : {
7173 2782080 : error_fx[i] = Mpy_32_16_1( error_fx[i], synth_scale_fx );
7174 2782080 : move32();
7175 : }
7176 : }
7177 :
7178 4347 : Decimate_allpass_steep_fx32( error_fx, hBWE_TD->mem_resamp_HB_32k_fx_32, L_FRAME32k, synth_fx );
7179 : }
7180 :
7181 : /* Update previous frame parameters for FEC */
7182 104775 : Copy( lsf_shb_fx, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER );
7183 104775 : IF( EQ_16( st->codec_mode, MODE1 ) )
7184 : {
7185 104775 : hBWE_TD->GainFrame_prevfrm_fx = GainFrame_fx;
7186 104775 : move32(); /*Q18*/
7187 104775 : hBWE_TD->tilt_swb_fec_fx = tilt_swb_fec_fx;
7188 104775 : move16();
7189 :
7190 104775 : if ( !st->bfi )
7191 : {
7192 102621 : hBWE_TD->GainAttn_fx = 32767; /*1.0f in Q15*/
7193 102621 : move16();
7194 : }
7195 : }
7196 : ELSE
7197 : {
7198 0 : IF( !st->bfi )
7199 : {
7200 0 : hBWE_TD->GainFrame_prevfrm_fx = GainFrame_fx;
7201 0 : move32(); /*Q18*/
7202 0 : hBWE_TD->tilt_swb_fec_fx = tilt_swb_fec_fx;
7203 0 : move16();
7204 0 : hBWE_TD->GainAttn_fx = 32767; /*1.0f in Q15*/
7205 0 : move16();
7206 : }
7207 : }
7208 :
7209 104775 : hBWE_TD->prev_ener_fx = ener_tmp_fx[NUM_SHB_SUBGAINS - 1];
7210 104775 : move32();
7211 104775 : hBWE_TD->prev_GainShape_fx = GainShape_fx[NUM_SHB_SUBFR - 1];
7212 104775 : move16();
7213 104775 : hBWE_TD->prev_Q_bwe_syn2 = Q_bwe_exc;
7214 104775 : move16();
7215 104775 : hBWE_TD->prev_Qx = Q_bwe_exc;
7216 104775 : move16();
7217 :
7218 104775 : return;
7219 : }
|