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