Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <stdio.h>
6 : #include <stdint.h>
7 : #include "options.h"
8 : #include "cnst.h"
9 : #include "rom_dec.h"
10 : #include "prot_fx.h"
11 : #include "rom_com.h"
12 :
13 : /*--------------------------------------------------------------------------*
14 : * Local function prototypes
15 : *--------------------------------------------------------------------------*/
16 : static Word16 p2a_threshold_dequant_fx( Decoder_State *st_fx, Word16 *p2a_flags, const Word16 bands, const Word16 p2a_bands );
17 :
18 : static void mdct_spectrum_fine_gain_dec_fx( Decoder_State *st_fx, Word32 L_y2[], const Word16 band_start[], const Word16 band_end[], const Word16 k_sort[], const Word16 bands, const Word32 L_qint, const Word16 Ngq, const Word16 gqlevs, const Word16 gqbits );
19 :
20 : static Word16 band_energy_dequant_fx( Decoder_State *st_fx, Word32 L_band_energy[], const Word16 bands, const Word32 L_qint, const Word16 eref_fx, const Word16 is_transient_fx );
21 :
22 0 : static Word16 Calc_inv( Word32 L_tmp, Word16 *exp )
23 : {
24 : Word16 exp2, tmp;
25 :
26 0 : tmp = extract_h( L_tmp );
27 :
28 0 : IF( tmp != 0 )
29 : {
30 0 : exp2 = norm_s( tmp );
31 0 : tmp = shl( tmp, exp2 ); /*Q(exp) */
32 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
33 0 : *exp = sub( 29, exp2 );
34 0 : move16();
35 : }
36 : ELSE
37 : {
38 0 : tmp = 0x7fff; /* 1 in Q15 */
39 0 : move16();
40 0 : *exp = 0;
41 0 : move16();
42 : }
43 0 : return tmp;
44 : }
45 :
46 : /*--------------------------------------------------------------------------*
47 : * spt_shorten_domain_set_dec()
48 : *
49 : * update the shorten band information based on p2a analysis
50 : *--------------------------------------------------------------------------*/
51 :
52 31 : static void spt_shorten_domain_set_dec_fx(
53 : Decoder_State *st_fx, /* i: encoder state structure */
54 : const Word16 p2a_flags[], /* i: p2a anlysis information Q0*/
55 : const Word16 new_band_start[], /* i: new band start position Q0*/
56 : const Word16 new_band_end[], /* i: new band end position Q0*/
57 : const Word16 new_band_width[], /* i: new subband band width Q0*/
58 : const Word16 bands, /* i: total number of subbands Q0*/
59 : Word16 band_start[], /* o: band start position Q0*/
60 : Word16 band_end[], /* o: band end position Q0*/
61 : Word16 band_width[], /* o: sub band band width Q0*/
62 : Word16 *bit_budget /* i/o: bit budget Q0*/
63 : )
64 : {
65 : Word16 j, k;
66 : Word16 spt_shorten_flag[SPT_SHORTEN_SBNUM];
67 :
68 31 : j = 0;
69 31 : move16();
70 155 : FOR( k = bands - SPT_SHORTEN_SBNUM; k < bands; k++ )
71 : {
72 124 : spt_shorten_flag[j] = 0;
73 124 : move16();
74 124 : IF( EQ_16( p2a_flags[k], 1 ) )
75 : {
76 4 : spt_shorten_flag[j] = get_next_indice_fx( st_fx, 1 ); /* Q0 */
77 4 : move16();
78 4 : *bit_budget = sub( *bit_budget, 1 ); /* Q0 */
79 4 : move16();
80 4 : IF( EQ_16( spt_shorten_flag[j], 1 ) )
81 : {
82 0 : band_start[k] = new_band_start[j]; /* Q0 */
83 0 : move16();
84 0 : band_end[k] = new_band_end[j]; /* Q0 */
85 0 : move16();
86 0 : band_width[k] = new_band_width[j]; /* Q0 */
87 0 : move16();
88 : }
89 : }
90 :
91 124 : j++;
92 : }
93 :
94 31 : return;
95 : }
96 :
97 : /*-------------------------------------------------------------------*
98 : * hq_lr_dec_fx()
99 : *
100 : * HQ low rate decoding routine
101 : *-------------------------------------------------------------------*/
102 :
103 34 : void hq_lr_dec_fx(
104 : Decoder_State *st_fx, /* i/o: : decoder state structure */
105 : Word32 L_yout[], /* o : Q12 : transform-domain output coefs. */
106 : const Word16 inner_frame, /* i : Q0 : inner frame length */
107 : Word16 num_bits, /* i : Q0 : number of available bits */
108 : Word16 *is_transient_fx /* o : Q0 : transient flag */
109 : )
110 : {
111 : Word16 i, j, k;
112 :
113 : Word32 L_y2[L_FRAME48k];
114 : Word32 L_y2_ni[L_FRAME48k];
115 : Word32 L_y2_org[L_FRAME48k];
116 : Word16 inp_vector_fx[L_FRAME48k];
117 : Word16 flag_spt_fx;
118 : Word32 L_m[L_FRAME48k];
119 : Word32 L_band_energy[BANDS_MAX];
120 : Word32 L_band_energy_tmp[BANDS_MAX];
121 :
122 : Word16 npulses_fx[BANDS_MAX];
123 : Word16 lowlength_fx, highlength_fx, hqswb_clas_fx, har_bands_fx, bands_fx;
124 : Word16 p2a_flags_fx[BANDS_MAX];
125 : Word32 L_bwe_br;
126 : Word16 prev_SWB_peak_pos_tmp_fx[SPT_SHORTEN_SBNUM];
127 :
128 : Word16 band_start[BANDS_MAX], band_end[BANDS_MAX], band_width[BANDS_MAX], trans_bit_fx;
129 :
130 : /* hq2_core_configure */
131 : Word32 L_qint;
132 : /*Word16 Qqint=29;*/
133 :
134 : Word16 eref_fx /* Qeref=10 */;
135 : Word16 bit_alloc_weight_fx /* Qbaw=13 */;
136 : Word16 ld_slope_fx /* Qldslope=15 */;
137 : Word16 p2a_th_fx /* Qp2ath=11 */;
138 : Word16 pd_thresh_fx /* Qpdth=15 */;
139 : Word16 ni_coef_fx /* Qnicoef=14 */;
140 :
141 : Word32 L_Rk[BANDS_MAX];
142 : Word16 bit_budget_fx;
143 :
144 : Word16 ni_seed_fx;
145 : Word16 length_fx;
146 : Word16 pbits_fx;
147 :
148 : Word16 k1_fx;
149 : Word16 gqlevs_fx, gqbits_fx, Ngq_fx, p2a_bands_fx;
150 : Word16 ebits_fx;
151 : Word16 exp_norm;
152 : Word16 org_band_start[SPT_SHORTEN_SBNUM];
153 : Word16 org_band_end[SPT_SHORTEN_SBNUM];
154 : Word16 org_band_width[SPT_SHORTEN_SBNUM];
155 :
156 : Word16 new_band_start[SPT_SHORTEN_SBNUM];
157 : Word16 new_band_end[SPT_SHORTEN_SBNUM];
158 : Word16 new_band_width[SPT_SHORTEN_SBNUM];
159 :
160 : Word16 k_sort_fx[BANDS_MAX];
161 : Word16 last_bitalloc_max_band[2];
162 : Word32 L_tmp;
163 : Word16 lowband, highband, p2a_flags_tmp[BANDS_MAX];
164 : Word32 L_tmp2, L_tmp3;
165 : Word16 exp, exp2, tmp, tmp1, tmp2, tmp3, frac1, alpha_fx, Q_band_energy;
166 : Word32 enerH_fx;
167 : Word32 enerL_fx;
168 : Word32 Ep_fx[BANDS_MAX];
169 : Word32 Ep_avrg_fx, Ep_vari_fx;
170 : Word32 Ep_avrgL_fx;
171 : Word32 Ep_peak_fx;
172 : Word32 Ep_tmp_fx[BANDS_MAX];
173 : Word16 gama_fx; /*Q15 0.85f;// */
174 : Word16 beta_fx; /*Q14 1.05f; */
175 : Word16 adjustFlag;
176 : Word16 bw_low, bw_high;
177 :
178 :
179 34 : HQ_DEC_HANDLE hHQ_core = st_fx->hHQ_core;
180 :
181 34 : tmp2 = 0; /* to avoid compilation flags */
182 34 : move16();
183 :
184 34 : set16_fx( last_bitalloc_max_band, 0, 2 );
185 34 : set32_fx( L_y2, 0x0L, L_FRAME48k );
186 34 : set16_fx( inp_vector_fx, 0, inner_frame );
187 34 : flag_spt_fx = 0;
188 34 : move16();
189 34 : set16_fx( prev_SWB_peak_pos_tmp_fx, 0, SPT_SHORTEN_SBNUM );
190 34 : adjustFlag = 0;
191 34 : move16();
192 34 : bw_low = 0;
193 34 : move16();
194 34 : bw_high = 20;
195 34 : move16();
196 34 : enerL_fx = L_deposit_l( 0 );
197 34 : enerH_fx = L_deposit_l( 0 );
198 :
199 34 : L_bwe_br = L_add( st_fx->core_brate, 0 );
200 34 : hqswb_clas_fx = 0;
201 34 : move16();
202 34 : test();
203 34 : test();
204 34 : IF( EQ_16( st_fx->bwidth, SWB ) && ( EQ_32( L_bwe_br, HQ_16k40 ) || EQ_32( L_bwe_br, HQ_13k20 ) ) )
205 : {
206 34 : hqswb_clas_fx = get_next_indice_fx( st_fx, 2 ); /* Q0 */
207 34 : num_bits = sub( num_bits, 2 ); /* Q0 */
208 :
209 34 : *is_transient_fx = 0;
210 34 : move16();
211 34 : if ( EQ_16( hqswb_clas_fx, HQ_TRANSIENT ) )
212 : {
213 3 : *is_transient_fx = 1; /* Q0 */
214 3 : move16();
215 : }
216 : }
217 : ELSE
218 : {
219 : /* decode transient flag */
220 0 : *is_transient_fx = get_next_indice_fx( st_fx, 1 ); /* Q0 */
221 0 : move16();
222 0 : num_bits = sub( num_bits, 1 ); /* Q0 */
223 : }
224 :
225 : /* Configure decoder for different bandwidths, bit rates, etc. */
226 34 : hq2_core_configure_fx( inner_frame, num_bits, *is_transient_fx, &bands_fx, &length_fx, band_width, band_start, band_end,
227 : &L_qint, &eref_fx, &bit_alloc_weight_fx, &gqlevs_fx, &Ngq_fx, &p2a_bands_fx, &p2a_th_fx, &pd_thresh_fx, &ld_slope_fx, &ni_coef_fx, L_bwe_br );
228 :
229 34 : highlength_fx = band_end[bands_fx - 1]; /* Q0 */
230 34 : move16();
231 34 : har_bands_fx = bands_fx; /* Q0 */
232 34 : move16();
233 :
234 34 : test();
235 34 : test();
236 34 : test();
237 34 : IF( EQ_16( st_fx->bwidth, SWB ) && *is_transient_fx == 0 && ( EQ_32( L_bwe_br, HQ_16k40 ) || EQ_32( L_bwe_br, HQ_13k20 ) ) )
238 : {
239 : /* reserve bits for HQ_NORMAL2 and HQ_HARMONIC modes */
240 31 : test();
241 31 : IF( EQ_16( hqswb_clas_fx, HQ_NORMAL ) || EQ_16( hqswb_clas_fx, HQ_HARMONIC ) )
242 : {
243 31 : num_bits = sub( num_bits, get_usebit_npswb_fx( hqswb_clas_fx ) ); /* Q0 */
244 : }
245 31 : if ( EQ_16( hqswb_clas_fx, HQ_NORMAL ) )
246 : {
247 31 : flag_spt_fx = 1; /* Q0 */
248 31 : move16();
249 : }
250 : }
251 :
252 34 : test();
253 34 : test();
254 34 : IF( ( EQ_32( L_bwe_br, HQ_16k40 ) || EQ_32( L_bwe_br, HQ_13k20 ) ) && EQ_16( st_fx->bwidth, SWB ) )
255 : {
256 34 : IF( NE_16( hHQ_core->prev_hqswb_clas, HQ_NORMAL ) )
257 : {
258 3 : j = 0;
259 3 : move16();
260 15 : FOR( k = bands_fx - SPT_SHORTEN_SBNUM; k < bands_fx; k++ )
261 : {
262 12 : hHQ_core->prev_SWB_peak_pos_fx[j] = 0;
263 12 : move16();
264 12 : j++;
265 : }
266 : }
267 : }
268 :
269 : /* Spectral energy calculation/quantization */
270 34 : ebits_fx = band_energy_dequant_fx( st_fx, L_band_energy, bands_fx, L_qint, eref_fx, *is_transient_fx ); /* Q0 */
271 :
272 : /* simple check: band_energy is too large, Abnormal Situation of bit errors */
273 812 : FOR( k = 0; k < bands_fx; k++ )
274 : {
275 : /* Max: 45.0(737279,Q14) at 32kHz, highest band, Min: -6.600037(-108135,Q14) at 8kHz(NB),8kbps, is_transient (-6.7f(-109772,Q14) is safty-threshold) */
276 778 : test();
277 778 : IF( GT_32( L_band_energy[k], 737279L /* 45.0 in Q14 */ ) || LT_32( L_band_energy[k], -109772L /* -6.7 in Q14 */ ) )
278 : {
279 0 : st_fx->BER_detect = 1; /* Q0 */
280 0 : move16();
281 0 : set32_fx( L_yout, 0x0L, inner_frame );
282 0 : return;
283 : }
284 : }
285 :
286 : /* First pass bit budget for TCQ of spectral band information */
287 34 : exp_norm = norm_s( gqlevs_fx );
288 34 : gqbits_fx = sub( 14, exp_norm ); /* Q0 */
289 :
290 34 : bit_budget_fx = sub( sub( num_bits, ebits_fx ), round_fx( L_shl( L_mult( Ngq_fx, gqbits_fx ), 15 ) ) ); /* (*num_bits) - (short) ceil (ebits) - Ngq * gqbits; Q0*/
291 :
292 :
293 34 : pbits_fx = 0;
294 34 : move16();
295 34 : test();
296 34 : test();
297 34 : IF( EQ_16( st_fx->bwidth, SWB ) && ( EQ_32( L_bwe_br, HQ_16k40 ) || EQ_32( L_bwe_br, HQ_13k20 ) ) )
298 : {
299 34 : IF( EQ_16( hqswb_clas_fx, HQ_HARMONIC ) )
300 : {
301 0 : set16_fx( p2a_flags_fx, 1, har_bands_fx );
302 : }
303 : ELSE
304 : {
305 34 : pbits_fx = p2a_threshold_dequant_fx( st_fx, p2a_flags_fx, bands_fx, p2a_bands_fx ); /* Q0 */
306 34 : bit_budget_fx = sub( bit_budget_fx, pbits_fx ); /* Q0 */
307 :
308 34 : IF( EQ_16( hqswb_clas_fx, HQ_NORMAL ) )
309 : {
310 31 : return_bits_normal2_fx( &bit_budget_fx, p2a_flags_fx, bands_fx, bits_lagIndices_modeNormal );
311 : }
312 : }
313 : }
314 : ELSE
315 : {
316 0 : pbits_fx = p2a_threshold_dequant_fx( st_fx, p2a_flags_fx, bands_fx, p2a_bands_fx ); /* Q0 */
317 0 : bit_budget_fx = sub( bit_budget_fx, pbits_fx ); /* Q0 */
318 : }
319 :
320 34 : IF( EQ_16( flag_spt_fx, 1 ) )
321 : {
322 : /* initialize the desired parameters for SPT */
323 31 : spt_shorten_domain_band_save_fx( bands_fx, band_start, band_end, band_width, org_band_start, org_band_end, org_band_width );
324 31 : spt_shorten_domain_pre_fx( band_start, band_end, hHQ_core->prev_SWB_peak_pos_fx, bands_fx, L_bwe_br, new_band_start, new_band_end, new_band_width );
325 31 : spt_shorten_domain_set_dec_fx( st_fx, p2a_flags_fx, new_band_start, new_band_end, new_band_width, bands_fx, band_start, band_end, band_width, &bit_budget_fx );
326 : }
327 :
328 : /* safety check in case of bit errors */
329 34 : IF( LT_16( bit_budget_fx, 2 ) )
330 : {
331 0 : st_fx->BER_detect = 1; /* Q0 */
332 0 : move16();
333 0 : set32_fx( L_yout, 0x0, inner_frame );
334 0 : return;
335 : }
336 :
337 34 : Q_band_energy = SWB_BWE_LR_Qbe;
338 34 : move16();
339 812 : FOR( i = 0; i < bands_fx; i++ )
340 : {
341 778 : L_tmp = L_shl( L_band_energy[i], sub( 16, Q_band_energy ) ); /*Q16 */
342 :
343 778 : frac1 = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */
344 778 : L_tmp = Pow2( 30, frac1 );
345 778 : exp = sub( exp, 30 );
346 778 : Ep_fx[i] = L_shl_sat( L_tmp, s_max( sub( exp, 6 ), -31 ) ); /* Q -6 */
347 778 : move32();
348 : }
349 :
350 812 : FOR( i = 0; i < bands_fx; i++ )
351 : {
352 778 : L_tmp2 = Ep_fx[i]; /* Q -6 */
353 778 : move32();
354 778 : L_tmp = L_max( 1, L_tmp2 );
355 778 : exp = norm_l( L_tmp );
356 778 : tmp = extract_h( L_shl( L_tmp, exp ) );
357 :
358 778 : L_tmp3 = (Word32) band_width[i];
359 778 : move16();
360 778 : exp2 = norm_l( L_tmp3 );
361 778 : tmp2 = extract_h( L_shl( L_tmp3, exp2 ) );
362 :
363 778 : exp2 = sub( exp, exp2 ); /* Denormalize and substract */
364 :
365 778 : tmp3 = sub( tmp2, tmp );
366 778 : if ( tmp3 > 0 )
367 : {
368 298 : tmp2 = shr( tmp2, 1 );
369 : }
370 778 : if ( tmp3 > 0 )
371 : {
372 298 : exp2 = add( exp2, 1 );
373 : }
374 778 : tmp = div_s( tmp2, tmp ); /* Q15 */
375 778 : L_tmp = L_deposit_h( tmp );
376 778 : L_tmp = Isqrt_lc( L_tmp, &exp2 ); /*Q(31-exp2) */
377 778 : Ep_tmp_fx[i] = L_shr( L_tmp, sub( 15, exp2 ) ); /*Q13 */
378 778 : move32();
379 : }
380 :
381 34 : test();
382 34 : test();
383 34 : test();
384 34 : test();
385 34 : test();
386 34 : test();
387 34 : IF( *is_transient_fx == 0 && EQ_16( inner_frame, L_FRAME8k ) && LE_32( st_fx->core_brate, ACELP_13k20 ) )
388 : {
389 : /* decode the last p2a_bands-1 subbands bit-allocation index of the previous frame */
390 0 : j = 0;
391 0 : move16();
392 0 : FOR( i = 0; i < 2; i++ )
393 : {
394 0 : last_bitalloc_max_band[i] = get_next_indice_fx( st_fx, 1 ); /* Q0 */
395 0 : move16();
396 : }
397 0 : lowband = 6; /* Q0 */
398 0 : move16();
399 0 : trans_bit_fx = 2; /* Q0 */
400 0 : move16();
401 0 : bit_budget_fx = sub( bit_budget_fx, trans_bit_fx ); /* Q0 */
402 0 : gama_fx = 27852;
403 0 : move16(); /*Q15 0.85f; */
404 0 : beta_fx = 17203;
405 0 : move16(); /*Q14 1.05f; */
406 :
407 0 : set16_fx( &p2a_flags_tmp[bands_fx - trans_bit_fx], 0, 2 );
408 :
409 0 : IF( EQ_32( st_fx->core_brate, ACELP_13k20 ) )
410 : {
411 0 : beta_fx = 13107;
412 0 : move16(); /*14 1.25f; */
413 0 : gama_fx = 31130;
414 0 : move16(); /*0.95f; */
415 0 : Copy( &p2a_flags_fx[bands_fx - trans_bit_fx], &p2a_flags_tmp[bands_fx - trans_bit_fx], trans_bit_fx );
416 : }
417 :
418 : /* calculate the the low band/high band energy and the variance/avrage of the envelopes */
419 0 : Ep_vari_fx = L_deposit_l( 0 );
420 0 : Ep_avrg_fx = L_deposit_l( 0 );
421 0 : Ep_avrgL_fx = L_deposit_l( 0 );
422 0 : Ep_peak_fx = L_deposit_l( 0 );
423 0 : FOR( i = 0; i < bands_fx; i++ )
424 : {
425 0 : IF( GE_16( i, lowband ) )
426 : {
427 0 : Ep_vari_fx = L_add_sat( Ep_vari_fx, L_abs( L_sub_sat( Ep_tmp_fx[i], Ep_tmp_fx[i - 1] ) ) ); /*Q15 */
428 0 : Ep_avrg_fx = L_add_sat( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
429 : }
430 : ELSE
431 : {
432 0 : Ep_avrgL_fx = L_add_sat( Ep_avrgL_fx, Ep_tmp_fx[i] ); /*Q15 */
433 0 : if ( GT_32( Ep_tmp_fx[i], Ep_peak_fx ) )
434 : {
435 0 : Ep_peak_fx = L_add( Ep_tmp_fx[i], 0 ); /*Q15 */
436 : }
437 : }
438 : }
439 : /* modify the last p2a_bands subbands band_energies */
440 0 : Copy32( L_band_energy, L_band_energy_tmp, bands_fx ); /*Q_band_energy */
441 0 : L_tmp = Mult_32_16( Ep_peak_fx, 24576 ); /*Q(13+14-15 = 12) 1.5 lowband = 6; */
442 0 : L_tmp2 = Mult_32_16( Ep_peak_fx, shl( sub( bands_fx, lowband ), 9 ) ); /*Q(13+9-15 = 7) */
443 0 : L_tmp3 = Mult_32_16( Ep_avrg_fx, 1126 ); /*Q(13+9-15 = 7) */
444 :
445 0 : test();
446 0 : test();
447 0 : test();
448 0 : test();
449 0 : IF( ( ( LT_32( L_tmp, L_shr( Ep_avrgL_fx, 1 ) ) && EQ_32( st_fx->core_brate, ACELP_13k20 ) ) || LT_32( st_fx->core_brate, ACELP_13k20 ) ) &&
450 : LT_32( L_tmp2, L_tmp3 ) && GT_32( L_tmp2, L_shr( Ep_avrg_fx, 7 ) ) )
451 : {
452 0 : FOR( i = lowband; i < bands_fx; i++ )
453 : {
454 0 : L_tmp = Mult_32_16( Ep_avrg_fx, 24576 ); /*Q(13+14-15 = 12) 1.5 */
455 0 : IF( LT_32( L_shr( Ep_tmp_fx[i], 1 ), L_tmp ) )
456 : {
457 0 : L_tmp = Mult_32_16( Ep_peak_fx, sub( bands_fx, lowband ) ); /*Q(13+0-15 = -2) */
458 0 : tmp = Calc_inv( L_shl( L_tmp, 14 ), &exp );
459 0 : L_tmp = L_shl( Mult_32_16( Ep_avrg_fx, tmp ), sub( 13, exp ) ); /*Q(13+exp-15 +13-exp +4 = 15) */
460 0 : L_tmp2 = L_add( L_tmp, 13107 ); /*15 */
461 0 : tmp2 = extract_l( L_min( L_max( L_tmp2, 16384 ), gama_fx ) ); /*15 = 15 */
462 0 : L_band_energy_tmp[i] = Mult_32_16( L_band_energy_tmp[i], tmp2 ); /*Q(Q_band_energy+15-15 = Q_band_energy) */
463 0 : move32();
464 : }
465 : }
466 : }
467 : ELSE
468 : {
469 0 : FOR( i = bands_fx - trans_bit_fx; i < bands_fx; i++ )
470 : {
471 0 : alpha_fx = 16384;
472 0 : move16(); /*Q14 */
473 0 : IF( EQ_16( p2a_flags_tmp[i], 1 ) )
474 : {
475 0 : L_tmp = Mult_32_16( Ep_tmp_fx[i], sub( bands_fx, lowband ) ); /*Q(13+0-15 = -2) */
476 0 : tmp = Calc_inv( L_shl( L_tmp, 14 ), &exp );
477 0 : L_tmp = Mult_32_16( Ep_vari_fx, 3277 ); /*13+15-15=13 */
478 0 : L_tmp = L_shl( Mult_32_16( L_tmp, tmp ), sub( 12, exp ) ); /*Q(13+exp-15 +12-exp +4 = 14) */
479 :
480 0 : tmp2 = extract_h( Ep_avrg_fx ); /*Q13-16=-3 */
481 0 : IF( tmp2 != 0 )
482 : {
483 0 : exp = norm_s( tmp2 );
484 0 : tmp2 = shl( tmp2, exp ); /*Q(exp) */
485 0 : tmp2 = div_s( 16384, tmp2 ); /*Q(15+14-exp=29-exp) */
486 0 : exp = sub( 29, exp );
487 : }
488 : ELSE
489 : {
490 : /*when the divisor is zero, happens rarely*/
491 0 : tmp2 = 0x7fff;
492 0 : move16();
493 0 : exp = 0;
494 0 : move16();
495 : }
496 0 : L_tmp2 = Mult_32_16( Ep_vari_fx, 6554 ); /*13+15-15=13 */
497 0 : L_tmp2 = L_shl( Mult_32_16( L_tmp2, tmp2 ), sub( 13, exp ) ); /*Q(13+exp-15 +13-exp +3 = 14) */
498 0 : L_tmp = L_min( L_tmp, L_tmp2 ); /*14 */
499 0 : tmp = extract_l( L_min( L_tmp, 13107 ) ); /*14 */
500 0 : alpha_fx = add( 16384, tmp );
501 : }
502 0 : IF( EQ_16( last_bitalloc_max_band[j++], 1 ) )
503 : {
504 0 : L_tmp = Mult_32_16( Ep_tmp_fx[i], sub( bands_fx, lowband ) ); /*Q(13+0-15 = -2) */
505 0 : tmp = Calc_inv( L_shl_sat( L_tmp, 14 ), &exp );
506 0 : L_tmp = L_shl_sat( Mult_32_16( Ep_avrg_fx, tmp ), sub( 14, exp ) ); /*Q(13+exp-15 +14-exp+2 = 14) */
507 0 : L_tmp = L_max( L_tmp, 16384 ); /*14 */
508 0 : tmp = extract_l( L_min( L_tmp, beta_fx ) ); /*14 */
509 0 : alpha_fx = shl( mult( alpha_fx, tmp ), 1 ); /*14+14-15 +1=14 */
510 : }
511 : ELSE
512 : {
513 0 : tmp2 = extract_h( Ep_avrg_fx ); /*13 -16 =-3 */
514 0 : IF( tmp2 != 0 )
515 : {
516 0 : exp = norm_s( tmp2 );
517 0 : tmp2 = shl( tmp2, exp ); /*Q(exp) */
518 0 : tmp2 = div_s( 16384, tmp2 ); /*Q(15+14-exp=29-exp) */
519 0 : exp = sub( 29, exp );
520 : }
521 : ELSE
522 : {
523 : /*when the divisor is zero, happens rarely*/
524 0 : tmp2 = 0x7fff;
525 0 : move16();
526 0 : exp = 0;
527 0 : move16();
528 : }
529 0 : L_tmp = L_shl( Mult_32_16( Ep_tmp_fx[i], tmp2 ), sub( 19, exp ) ); /*Q(13+exp-15 +19-exp +3 = 20) */
530 0 : L_tmp = Mult_32_16( L_tmp, shl( sub( bands_fx, lowband ), 9 ) ); /*20 +9 -15 =14 */
531 0 : L_tmp = L_max( L_tmp, 13926 ); /*14 */
532 0 : tmp2 = extract_l( L_min( L_tmp, 16384 ) ); /*14 */
533 0 : alpha_fx = shl( mult( alpha_fx, tmp2 ), 1 ); /*14+14-15+1 =14 */
534 : }
535 0 : L_band_energy_tmp[i] = L_shl( Mult_32_16( L_band_energy_tmp[i], alpha_fx ), 1 ); /*Q(Q_band_energy+14-15 +1= Q_band_energy) */
536 0 : move32();
537 : }
538 : }
539 0 : lowband = 3; /* Q0 */
540 0 : move16();
541 0 : Ep_avrg_fx = L_deposit_l( 0 );
542 0 : Ep_avrgL_fx = L_deposit_l( 0 );
543 0 : Ep_peak_fx = L_deposit_l( 0 );
544 0 : FOR( i = 0; i < bands_fx; i++ )
545 : {
546 0 : IF( GE_16( i, lowband ) )
547 : {
548 0 : Ep_avrg_fx = L_add_sat( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
549 : }
550 : ELSE
551 : {
552 0 : Ep_avrgL_fx = L_add( Ep_avrgL_fx, L_shr( Ep_tmp_fx[i], 1 ) ); /*Q12 */
553 0 : if ( GT_32( Ep_tmp_fx[i], Ep_peak_fx ) )
554 : {
555 0 : Ep_peak_fx = L_add( Ep_tmp_fx[i], 0 ); /*Q13 */
556 : }
557 : }
558 : }
559 0 : L_tmp = Mult_32_16( Ep_peak_fx, 28262 ); /*Q(13+14-15 = 12) 1.725 lowband = 3; */
560 0 : L_tmp2 = Mult_32_16( Ep_avrgL_fx, 24576 ); /*Q(12+14-15 = 11) */
561 0 : test();
562 0 : test();
563 0 : IF( GT_32( L_shr( Ep_avrg_fx, 2 ), L_tmp2 ) && LT_32( L_shr( Ep_avrg_fx, 4 ), L_tmp2 ) && GT_32( L_tmp, Ep_avrgL_fx ) )
564 : {
565 0 : adjustFlag = 1;
566 0 : move16();
567 0 : FOR( i = 0; i < lowband; i++ )
568 : {
569 0 : tmp = Calc_inv( Ep_avrgL_fx, &exp );
570 0 : L_tmp = Mult_32_16( Ep_peak_fx, tmp ); /*Q(13+exp-15+4 = exp+2) */
571 0 : L_tmp = Mult_32_16( L_tmp, lowband ); /*Q(exp+2+0-15 = exp-13) */
572 0 : L_tmp = Mult_32_16( L_tmp, 18842 ); /*Q(exp-13+16-16 = exp-13) */
573 0 : L_tmp = L_shl( L_tmp, sub( 27, exp ) ); /*Q14 0.5 */
574 0 : tmp2 = extract_l( L_min( L_tmp, 19661 ) ); /*14 */
575 0 : L_tmp = Mult_32_16( L_band_energy_tmp[i], tmp2 ); /*Q(Q_band_energy+14-15 = Q_band_energy-1) */
576 0 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q_band_energy */
577 0 : move32();
578 : }
579 : }
580 0 : hq2_bit_alloc_fx(
581 : L_band_energy_tmp, bands_fx, L_Rk, &bit_budget_fx, p2a_flags_fx, bit_alloc_weight_fx, band_width,
582 0 : num_bits, hqswb_clas_fx, st_fx->bwidth, *is_transient_fx );
583 : }
584 34 : ELSE IF( *is_transient_fx == 0 && EQ_16( inner_frame, L_FRAME16k ) )
585 : {
586 0 : bit_budget_fx = sub( bit_budget_fx, 2 ); /* bits in high bands to indicate the last 2 subbands is allocated bits or not Q0*/
587 :
588 0 : FOR( i = 0; i < 2; i++ )
589 : {
590 0 : last_bitalloc_max_band[i] = get_next_indice_fx( st_fx, 1 ); /* Q0 */
591 0 : move16();
592 : }
593 0 : FOR( i = 0; i < bands_fx; i++ )
594 : {
595 0 : Ep_tmp_fx[i] = L_shl_sat( Ep_tmp_fx[i], 2 ); /* Q15 */
596 0 : move32();
597 : }
598 0 : IF( EQ_32( st_fx->core_brate, ACELP_13k20 ) )
599 : {
600 0 : lowband = 8; /* Q0 */
601 0 : move16();
602 0 : highband = 15; /* Q0 */
603 0 : move16();
604 0 : bw_low = sub( band_start[highband], band_start[lowband] ); /* Q0 */
605 0 : bw_high = sub( add( band_end[bands_fx - 1], 1 ), band_start[highband] ); /* Q0 */
606 : }
607 : ELSE
608 : {
609 0 : lowband = 8; /* Q0 */
610 0 : move16();
611 0 : highband = 16; /* Q0 */
612 0 : move16();
613 0 : bw_low = sub( band_start[highband], band_start[lowband] ); /* Q0 */
614 0 : bw_high = sub( add( band_end[bands_fx - 1], 1 ), band_start[highband] ); /* Q0 */
615 : }
616 : /* calculate the the low band/high band energy and the variance/avrage of the envelopes */
617 0 : enerL_fx = L_deposit_l( 0 );
618 0 : enerH_fx = L_deposit_l( 0 );
619 0 : Ep_vari_fx = L_deposit_l( 0 );
620 0 : Ep_avrg_fx = L_deposit_l( 0 );
621 0 : FOR( i = 0; i < bands_fx; i++ )
622 : {
623 0 : test();
624 0 : IF( GE_16( i, lowband ) && add( sub( i, bands_fx ), p2a_bands_fx ) < 0 )
625 : {
626 0 : Ep_vari_fx = L_add_sat( Ep_vari_fx, L_abs( L_sub( Ep_tmp_fx[i], Ep_tmp_fx[i - 1] ) ) ); /*Q15 */
627 0 : Ep_avrg_fx = L_add_sat( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
628 : }
629 :
630 0 : IF( GE_16( i, highband ) )
631 : {
632 :
633 : #ifdef NONBE_1118_EVS_LR_HQ_BITERROR
634 : /* safety check in case of bit errors */
635 0 : IF( GT_32( Ep_fx[i], 536788991 /* max(Q30) */ ) )
636 : {
637 0 : st_fx->BER_detect = 1;
638 0 : move16();
639 0 : set32_fx( L_yout, 0, inner_frame );
640 0 : return;
641 : }
642 :
643 : #endif
644 0 : enerH_fx = L_add_sat( enerH_fx, L_shl_sat( Ep_fx[i], 2 ) ); /*Q0 */
645 : }
646 0 : ELSE IF( GE_16( i, lowband ) )
647 : {
648 : #ifdef NONBE_1118_EVS_LR_HQ_BITERROR
649 : /* safety check in case of bit errors */
650 0 : IF( GT_32( Ep_fx[i], 536788991 /* max(Q30) */ ) )
651 : {
652 0 : st_fx->BER_detect = 1;
653 0 : move16();
654 0 : set32_fx( L_yout, 0, inner_frame );
655 0 : return;
656 : }
657 :
658 : #endif
659 0 : enerL_fx = L_add_sat( enerL_fx, L_shl_sat( Ep_fx[i], 2 ) ); /*Q0 */
660 : }
661 : }
662 :
663 : /* modify the last p2a_bands subbands band_energies */
664 0 : Copy32( L_band_energy, L_band_energy_tmp, bands_fx ); /*Q_band_energy */
665 0 : L_tmp = L_max( enerH_fx, enerL_fx );
666 0 : tmp = s_max( bw_low, bw_high );
667 0 : i = norm_l( L_tmp );
668 0 : j = norm_s( tmp );
669 0 : L_tmp = Mult_32_16( L_shl( enerH_fx, i ), shl( bw_low, j ) ); /* i + j -15 */
670 0 : L_tmp2 = Mult_32_16( L_shl( enerL_fx, i ), shl( bw_high, j ) ); /*i + j -15 */
671 0 : L_tmp2 = L_sub( L_tmp, L_tmp2 );
672 0 : FOR( i = bands_fx - p2a_bands_fx; i < bands_fx; i++ )
673 : {
674 0 : test();
675 0 : IF( EQ_16( p2a_flags_fx[i], 1 ) || L_tmp2 > 0 )
676 : {
677 0 : tmp = sub( bands_fx, p2a_bands_fx );
678 0 : tmp = sub( tmp, lowband ); /*Q0 */
679 :
680 0 : tmp1 = extract_h( L_shl_sat( Ep_avrg_fx, 1 ) ); /*Q0 */
681 0 : IF( tmp1 != 0 )
682 : {
683 0 : exp = norm_s( tmp1 );
684 0 : tmp1 = shl( tmp1, exp ); /*Q(exp) */
685 0 : tmp1 = div_s( 16384, tmp1 ); /*Q(15+14-exp = 29-exp) */
686 0 : exp = sub( 29, exp );
687 : }
688 : ELSE
689 : {
690 : /*when the divisor is zero, happens rarely*/
691 0 : tmp1 = 0x7fff;
692 0 : move16();
693 0 : exp = 0;
694 0 : move16();
695 : }
696 0 : L_tmp = Mult_32_16( Ep_tmp_fx[i], tmp1 ); /*Q(15+exp-15 = exp) */
697 0 : L_tmp = Mult_32_16( L_tmp, tmp ); /*Q(exp+0-15 = exp-15) */
698 0 : L_tmp = Mult_32_16( L_tmp, 16384 ); /*Q(exp-15+13-15 = exp-17) */
699 0 : L_tmp = L_shl( L_tmp, sub( 32, exp ) ); /*Q15 */
700 0 : tmp = extract_l( L_min( L_tmp, 6554 ) ); /*Q15 */
701 0 : L_tmp = Mult_32_16( Ep_vari_fx, tmp1 ); /*Q(15+exp-15 = exp) */
702 0 : L_tmp = Mult_32_16( L_tmp, tmp ); /*Q(exp+15-15 = exp) */
703 0 : L_tmp = L_shl( L_tmp, sub( 15, exp ) ); /*Q15 */
704 0 : tmp = extract_l( L_shr( L_min( L_tmp, 13107 ), 1 ) ); /*Q14 */
705 :
706 0 : alpha_fx = add( tmp, 16384 ); /*Q14 */
707 : }
708 : ELSE
709 : {
710 0 : alpha_fx = 16384;
711 0 : move16(); /*Q14 */
712 : }
713 :
714 0 : IF( add( sub( i, bands_fx ), p2a_bands_fx ) > 0 )
715 : {
716 0 : tmp = sub( bands_fx, p2a_bands_fx );
717 0 : IF( EQ_16( last_bitalloc_max_band[i - ( tmp + 1 )], 1 ) )
718 : {
719 0 : tmp = sub( tmp, lowband );
720 0 : L_tmp = Mult_32_16( Ep_tmp_fx[i], tmp ); /*Q(15+0-15 = 0) */
721 0 : tmp = Calc_inv( L_shl_sat( L_tmp, 16 ), &exp );
722 0 : L_tmp = Mult_32_16( Ep_avrg_fx, tmp ); /*Q(15+exp-15 = exp) */
723 0 : L_tmp = L_shl( L_tmp, sub( 14, exp ) ); /*Q14 */
724 0 : tmp = extract_l( L_min( L_max( L_tmp, 16384 ), 20480 ) ); /*Q14 */
725 0 : L_tmp = L_mult( alpha_fx, tmp ); /*Q(14+14+1=29) */
726 0 : alpha_fx = extract_l( L_shr( L_tmp, 15 ) ); /*Q14 */
727 : }
728 : ELSE
729 : {
730 0 : tmp = sub( tmp, lowband );
731 :
732 0 : tmp1 = extract_h( L_shl_sat( Ep_avrg_fx, 1 ) ); /*Q0 */
733 0 : IF( tmp1 != 0 )
734 : {
735 0 : exp = norm_s( tmp1 );
736 0 : tmp1 = shl( tmp1, exp ); /*Q(exp) */
737 0 : tmp1 = div_s( 16384, tmp1 ); /*Q(15+14-exp=29-exp) */
738 0 : exp = sub( 29, exp );
739 : }
740 : ELSE
741 : {
742 : /*when the divisor is zero, happens rarely*/
743 0 : tmp1 = 0x7fff;
744 0 : move16();
745 0 : exp = 0;
746 0 : move16();
747 : }
748 0 : L_tmp = Mult_32_16( Ep_tmp_fx[i], tmp1 ); /*Q(15+exp-15 = exp) */
749 0 : L_tmp = Mult_32_16( L_tmp, tmp ); /*Q(exp+0-15 = exp-15) */
750 0 : L_tmp = L_shl( L_tmp, sub( 29, exp ) ); /*Q14 */
751 0 : tmp = extract_l( L_min( L_max( L_tmp, 13926 ), 16384 ) ); /*Q14 */
752 0 : L_tmp = L_mult( alpha_fx, tmp ); /*Q(14+14+1=29) */
753 0 : alpha_fx = extract_l( L_shr( L_tmp, 15 ) ); /*Q14 */
754 : }
755 : }
756 0 : L_tmp = Mult_32_16( L_band_energy_tmp[i], alpha_fx ); /*Q(Q_band_energy+14-15=Q_band_energy-1) */
757 0 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q Q_band_energy */
758 0 : move32();
759 : }
760 0 : lowband = 6;
761 0 : move16();
762 0 : Ep_avrg_fx = L_deposit_l( 0 );
763 0 : Ep_avrgL_fx = L_deposit_l( 0 );
764 0 : Ep_peak_fx = L_deposit_l( 0 );
765 0 : FOR( i = 0; i < bands_fx; i++ )
766 : {
767 0 : IF( GE_16( i, lowband ) )
768 : {
769 0 : Ep_avrg_fx = L_add_sat( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
770 : }
771 : ELSE
772 : {
773 0 : Ep_avrgL_fx = L_add_sat( Ep_avrgL_fx, Ep_tmp_fx[i] ); /*Q15 */
774 0 : if ( GT_32( Ep_tmp_fx[i], Ep_peak_fx ) )
775 : {
776 0 : Ep_peak_fx = L_add( Ep_tmp_fx[i], 0 ); /*Q15 */
777 : }
778 : }
779 : }
780 :
781 :
782 0 : L_tmp = Mult_32_16( Ep_peak_fx, 24576 ); /*Q(15+13-15 = 13) lowband = 6; */
783 0 : L_tmp2 = Mult_32_16( Ep_peak_fx, 19661 ); /*Q(15+14-15 = 14) */
784 0 : L_tmp3 = Mult_32_16( Ep_avrgL_fx, 24576 ); /*Q(15+12-15 = 12) */
785 :
786 0 : test();
787 0 : test();
788 0 : test();
789 0 : test();
790 0 : test();
791 0 : IF( ( GT_32( L_shr( Ep_avrgL_fx, 1 ), Ep_avrg_fx ) && GT_32( L_tmp, L_shr( Ep_avrgL_fx, 2 ) ) && LT_32( L_shr( Ep_avrgL_fx, 1 ), L_tmp2 ) ) ||
792 : ( GT_32( L_shr( Ep_avrg_fx, 1 ), Ep_avrgL_fx ) && LT_32( L_shr( Ep_avrg_fx, 3 ), L_tmp3 ) && GT_32( L_tmp, L_shr( Ep_avrgL_fx, 2 ) ) ) )
793 : {
794 0 : adjustFlag = 1;
795 0 : move16();
796 0 : FOR( i = 0; i < lowband; i++ )
797 : {
798 0 : tmp = Calc_inv( L_shl_sat( Ep_avrgL_fx, 1 ), &exp );
799 0 : L_tmp = Mult_32_16( Ep_peak_fx, tmp ); /*Q(15+exp-15 = exp) */
800 0 : L_tmp = Mult_32_16( L_tmp, lowband ); /*Q(exp+0-15 = exp-15) */
801 0 : L_tmp = L_shl( L_tmp, sub( 28, exp ) ); /*Q14 0.5 */
802 0 : tmp = extract_l( L_min( L_tmp, 19661 ) ); /*//Q14 */
803 0 : L_tmp = Mult_32_16( L_band_energy_tmp[i], tmp ); /*Q(Q_band_energy+14-15 = Q_band_energy-1) */
804 0 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q_band_energy */
805 0 : move32();
806 : }
807 : }
808 :
809 0 : hq2_bit_alloc_fx(
810 : L_band_energy_tmp, bands_fx, L_Rk, &bit_budget_fx, p2a_flags_fx, bit_alloc_weight_fx, band_width,
811 0 : num_bits, hqswb_clas_fx, st_fx->bwidth, *is_transient_fx );
812 : }
813 34 : ELSE IF( EQ_16( st_fx->bwidth, SWB ) && EQ_16( hqswb_clas_fx, HQ_HARMONIC ) && ( EQ_32( L_bwe_br, HQ_16k40 ) || EQ_32( L_bwe_br, HQ_13k20 ) ) )
814 : {
815 0 : hq2_bit_alloc_har_fx( L_band_energy, bit_budget_fx, bands_fx, L_Rk, p2a_bands_fx, L_bwe_br, p2a_flags_fx, band_width );
816 : }
817 : ELSE
818 : {
819 34 : hq2_bit_alloc_fx(
820 : L_band_energy, bands_fx, L_Rk, &bit_budget_fx, p2a_flags_fx, bit_alloc_weight_fx, band_width,
821 34 : num_bits, hqswb_clas_fx, st_fx->bwidth, *is_transient_fx );
822 : }
823 :
824 34 : IF( bit_budget_fx < 0 )
825 : {
826 0 : st_fx->BER_detect = 1;
827 0 : move16();
828 0 : bit_budget_fx = 0;
829 0 : move16();
830 : }
831 34 : tcq_core_LR_dec_fx( st_fx, /*inp_vector*/ inp_vector_fx, bit_budget_fx, bands_fx, band_start, band_width, /*Rk, */ L_Rk, npulses_fx, k_sort_fx,
832 : p2a_flags_fx, p2a_bands_fx, last_bitalloc_max_band, inner_frame, adjustFlag, is_transient_fx );
833 :
834 : /* Denormalize the coded MDCT spectrum */
835 34 : mdct_spectrum_denorm_fx( inp_vector_fx, L_y2, band_start, band_end, band_width, L_band_energy, npulses_fx, bands_fx, ld_slope_fx, pd_thresh_fx );
836 :
837 : /* Apply fine gain to denormalized coded spectrum */
838 34 : mdct_spectrum_fine_gain_dec_fx( st_fx, L_y2, band_start, band_end, k_sort_fx, bands_fx,
839 : L_qint, Ngq_fx, gqlevs_fx, gqbits_fx );
840 :
841 34 : test();
842 34 : test();
843 34 : test();
844 : /* Restore the band information */
845 34 : IF( EQ_16( flag_spt_fx, 1 ) )
846 : {
847 31 : spt_shorten_domain_band_restore_fx( bands_fx, band_start, band_end, band_width, org_band_start, org_band_end, org_band_width );
848 : }
849 :
850 34 : Copy32( L_y2, L_y2_org, L_FRAME32k ); /* Q12 */
851 :
852 : /* Inject noise into components having relatively low pulse energy per band */
853 34 : ni_seed_fx = add( add( add( npulses_fx[0], npulses_fx[1] ), npulses_fx[2] ), npulses_fx[3] );
854 34 : Copy32( L_y2, L_y2_ni, band_end[bands_fx - 1] + 1 ); /* Q12 */
855 :
856 34 : hq2_noise_inject_fx( L_y2_ni, band_start, band_end, band_width, Ep_fx, L_Rk, npulses_fx, ni_seed_fx, bands_fx, 0, bw_low, bw_high, enerL_fx, enerH_fx,
857 34 : hHQ_core->last_ni_gain_fx, hHQ_core->last_env_fx, &hHQ_core->last_max_pos_pulse, p2a_flags_fx, p2a_bands_fx,
858 34 : hqswb_clas_fx, st_fx->bwidth, L_bwe_br );
859 :
860 34 : test();
861 34 : test();
862 34 : IF( EQ_16( st_fx->bwidth, SWB ) && ( EQ_32( L_bwe_br, HQ_16k40 ) || EQ_32( L_bwe_br, HQ_13k20 ) ) )
863 : {
864 34 : test();
865 34 : IF( EQ_16( hqswb_clas_fx, HQ_NORMAL ) || EQ_16( hqswb_clas_fx, HQ_HARMONIC ) )
866 : {
867 31 : preset_hq2_swb_fx( hqswb_clas_fx, band_end, &har_bands_fx, p2a_bands_fx, length_fx, bands_fx, &lowlength_fx, &highlength_fx, L_m );
868 :
869 31 : swb_bwe_dec_lr_fx( st_fx, L_y2, SWB_BWE_LR_Qs, L_m, L_bwe_br, bands_fx, band_start, band_end, L_band_energy, SWB_BWE_LR_Qbe,
870 : p2a_flags_fx, hqswb_clas_fx, lowlength_fx, highlength_fx, har_bands_fx, &hHQ_core->prev_frm_hfe2, &hHQ_core->prev_stab_hfe2, band_width, L_y2_ni, &ni_seed_fx );
871 :
872 31 : post_hq2_swb_fx( L_m, lowlength_fx, highlength_fx, hqswb_clas_fx, har_bands_fx, bands_fx, p2a_flags_fx, band_start, band_end, L_y2, npulses_fx );
873 :
874 31 : IF( EQ_16( hqswb_clas_fx, HQ_NORMAL ) )
875 : {
876 31 : spt_swb_peakpos_tmp_save_fx( L_y2, bands_fx, band_start, band_end, prev_SWB_peak_pos_tmp_fx );
877 155 : FOR( k = 0; k < SPT_SHORTEN_SBNUM; k++ )
878 : {
879 124 : test();
880 124 : IF( p2a_flags_fx[bands_fx - SPT_SHORTEN_SBNUM + k] == 0 || npulses_fx[bands_fx - SPT_SHORTEN_SBNUM + k] == 0 )
881 : {
882 122 : prev_SWB_peak_pos_tmp_fx[k] = 0;
883 122 : move16();
884 : }
885 : }
886 : }
887 31 : Copy32( L_y2_ni, L_y2, lowlength_fx ); /* Q12 */
888 : }
889 : ELSE
890 : {
891 3 : Copy32( L_y2_ni, L_y2, add( band_end[bands_fx - 1], 1 ) ); /* HQ_TRANSIENT Q12*/
892 : }
893 : }
894 : ELSE
895 : {
896 0 : Copy32( L_y2_ni, L_y2, add( band_end[bands_fx - 1], 1 ) ); /* NB, WB Q12*/
897 : }
898 :
899 34 : test();
900 34 : IF( !( GE_16( st_fx->last_inner_frame, L_FRAME16k ) && st_fx->bws_cnt > 0 ) )
901 : {
902 34 : k1_fx = sub( bands_fx, 2 );
903 34 : if ( NE_16( *is_transient_fx, 1 ) )
904 : {
905 31 : k1_fx = sub( bands_fx, 6 ); /* Q1 */
906 : }
907 34 : L_tmp = L_deposit_l( 0 );
908 226 : FOR( i = k1_fx; i < bands_fx; i++ )
909 : {
910 192 : tmp = div_s( 1, sub( bands_fx, k1_fx ) ); /*Q15 */
911 192 : L_tmp = L_add( L_tmp, Mult_32_16( Ep_tmp_fx[i], tmp ) ); /*Q15 */
912 : }
913 34 : st_fx->prev_ener_shb_fx = extract_l( L_shr( L_tmp, 14 ) ); /* Q1 */
914 34 : move16();
915 : }
916 34 : test();
917 34 : IF( GE_16( st_fx->last_inner_frame, L_FRAME32k ) && st_fx->hBWE_FD != NULL )
918 : {
919 34 : set16_fx( st_fx->hBWE_FD->prev_SWB_fenv_fx, st_fx->prev_ener_shb_fx, SWB_FENV );
920 : }
921 :
922 34 : updat_prev_frm_fx( L_y2, L_yout, L_bwe_br, length_fx, inner_frame, bands_fx, st_fx->bwidth, *is_transient_fx, hqswb_clas_fx, &hHQ_core->prev_hqswb_clas,
923 34 : hHQ_core->prev_SWB_peak_pos_fx, prev_SWB_peak_pos_tmp_fx, &hHQ_core->prev_frm_hfe2, &hHQ_core->prev_stab_hfe2, st_fx->bws_cnt );
924 :
925 34 : return;
926 : }
927 :
928 : /*------------------------------------------------------------------------------------
929 : * small_symbol_dec_tran_fx()
930 : *
931 : * Huffman decoding of differential energies
932 : *--------------------------------------------------------------------------------------*/
933 :
934 2 : static Word16 small_symbol_dec_tran_fx(
935 : Decoder_State *st_fx, /* i/o: decoder state structure Q0*/
936 : Word16 *qbidx, /* o : output of dequantized differential energy Q0*/
937 : const Word16 bands, /* i : number of bands Q0*/
938 : const Word16 is_transient /* i : transient flag Q0*/
939 : )
940 : {
941 : Word16 i, bits;
942 : Word16 difidx[BANDS_MAX];
943 :
944 : /* Decoding differential energies*/
945 2 : bits = decode_envelope_indices_fx( st_fx, 0, bands, 0, difidx, LOW_RATE_HQ_CORE_TRAN, is_transient ); /* Q0 */
946 2 : bits = add( bits, BITS_DE_FCOMP ); /* Q0 */
947 :
948 : /* counting 1 bit for band_energy_huff_coding_mode */
949 2 : bits = add( bits, BITS_DE_HMODE ); /* Q0 */
950 :
951 : /* converting to original values */
952 66 : FOR( i = 0; i < bands; i++ )
953 : {
954 64 : qbidx[i] = sub( difidx[i], LRMDCT_BE_OFFSET ); /* Q0 */
955 64 : move16();
956 : }
957 :
958 2 : return ( bits );
959 : }
960 :
961 :
962 : /*--------------------------------------------------------------------------
963 : * small_symbol_dec()
964 : *
965 : * Huffman decoding of differential energies (MSB and LSB)
966 : *--------------------------------------------------------------------------*/
967 :
968 30 : static Word16 small_symbol_dec_fx( /* o : bits Q0*/
969 : Decoder_State *st_fx, /* i/o: decoder state structure */
970 : Word16 *qbidx, /* o : output of dequantized differential energy Q0*/
971 : const Word16 bands, /* i : number of bands Q0*/
972 : const Word16 is_transient )
973 : {
974 : Word16 i, LSB, bits;
975 : Word16 difidx[BANDS_MAX];
976 :
977 : /* Decoding MSB bits */
978 30 : bits = decode_envelope_indices_fx( st_fx, 0, bands, 0, difidx, LOW_RATE_HQ_CORE, is_transient ); /* Q0 */
979 30 : bits = add( bits, BITS_DE_FCOMP ); /* Q0 */
980 :
981 : /* counting 1 bit for band_energy_huff_coding_mode */
982 30 : bits = add( bits, BITS_DE_HMODE ); /* Q0 */
983 :
984 : /* Decoding LSB bit packing */
985 690 : FOR( i = 0; i < bands; ++i )
986 : {
987 660 : LSB = get_next_indice_fx( st_fx, BITS_DE_LSB ); /* Q0 */
988 660 : difidx[i] = s_or( shl( difidx[i], 1 ), LSB ); /* Q0 */
989 660 : move16();
990 : }
991 :
992 : /* counting bits for LSB */
993 30 : bits = add( bits, bands ); /* Q0 */
994 :
995 : /* converting to original values */
996 660 : FOR( i = 1; i < bands; ++i )
997 : {
998 630 : qbidx[i] = sub( difidx[i], DE_OFFSET1 ); /* Q0 */
999 630 : move16();
1000 : }
1001 :
1002 30 : qbidx[0] = sub( difidx[0], DE_OFFSET0 ); /* Q0 */
1003 30 : move16();
1004 :
1005 30 : return ( bits );
1006 : }
1007 :
1008 :
1009 54 : static Word16 decode_huff_8s_fx(
1010 : Decoder_State *st_fx,
1011 : const Word16 *hufftab, /* Q0 */
1012 : Word16 *rbits /* Q0 */
1013 : )
1014 : {
1015 : Word16 bit;
1016 :
1017 186 : WHILE( *hufftab > 0 )
1018 : {
1019 132 : *rbits = add( *rbits, s_and( *hufftab, 0xf ) ); /* Q0 */
1020 132 : move16();
1021 132 : bit = get_next_indice_fx( st_fx, s_and( *hufftab, 0xf ) ); /* Q0 */
1022 132 : move16();
1023 132 : hufftab += add( shr( *hufftab, 4 ), bit ); /* Q0 */
1024 : }
1025 :
1026 54 : return negate( *hufftab );
1027 : }
1028 :
1029 2 : static Word16 large_symbol_dec_fx( /* o : bits */
1030 : Decoder_State *st_fx, /* i/o: decoder state structure */
1031 : Word16 *qbidx, /* o : output of dequantized differential energy Q0*/
1032 : const Word16 bands /* i : number of bands Q0*/
1033 : )
1034 : {
1035 : Word16 i, bits;
1036 : Word16 LSB[BANDS_MAX];
1037 : Word16 basic_shift, cntbits, ns2mode;
1038 : Word16 pos_outlyer;
1039 : Word16 ns2mode0, ns2mode1;
1040 :
1041 2 : cntbits = BITS_DE_8SMODE; /* Q0 */
1042 2 : move16();
1043 2 : ns2mode = get_next_indice_fx( st_fx, BITS_DE_8SMODE ); /* Q0 */
1044 :
1045 2 : IF( ns2mode == 0 )
1046 : {
1047 1 : ns2mode0 = get_next_indice_fx( st_fx, BITS_DE_8SMODE_N0 ); /* Q0 */
1048 1 : ns2mode1 = get_next_indice_fx( st_fx, BITS_DE_8SMODE_N1 ); /* Q0 */
1049 1 : cntbits = add( cntbits, BITS_DE_8SMODE_N0 + BITS_DE_8SMODE_N1 ); /* Q0 */
1050 :
1051 1 : IF( ns2mode0 == 0 )
1052 : {
1053 1 : IF( EQ_16( ns2mode1, 1 ) )
1054 : {
1055 0 : pos_outlyer = get_next_indice_fx( st_fx, BITS_DE_8SPOS ); /* Q0 */
1056 0 : cntbits = add( cntbits, BITS_DE_8SPOS ); /* Q0 */
1057 0 : qbidx[pos_outlyer] = sub( get_next_indice_fx( st_fx, BITS_ABS_ENG ), ABS_ENG_OFFSET ); /* Q0 */
1058 0 : move16();
1059 0 : cntbits = add( cntbits, BITS_ABS_ENG ); /* Q0 */
1060 : }
1061 : ELSE
1062 : {
1063 1 : pos_outlyer = -1; /* Q0 */
1064 1 : move16();
1065 : }
1066 :
1067 1 : FOR( i = 0; i < pos_outlyer; ++i )
1068 : {
1069 0 : bits = 0;
1070 0 : move16();
1071 0 : qbidx[i] = sub( decode_huff_8s_fx( st_fx, hestable, &bits ), 4 ); /* Q0 */
1072 0 : move16();
1073 0 : cntbits = add( cntbits, bits ); /* Q0 */
1074 : }
1075 :
1076 33 : FOR( i = pos_outlyer + 1; i < bands; ++i )
1077 : {
1078 32 : bits = 0;
1079 32 : move16();
1080 32 : qbidx[i] = sub( decode_huff_8s_fx( st_fx, hestable, &bits ), 4 ); /* Q0 */
1081 32 : move16();
1082 32 : cntbits = add( cntbits, bits ); /* Q0 */
1083 : }
1084 : }
1085 : ELSE
1086 : {
1087 0 : IF( EQ_16( ns2mode1, 1 ) )
1088 : {
1089 0 : pos_outlyer = get_next_indice_fx( st_fx, BITS_DE_8SPOS ); /* Q0 */
1090 0 : cntbits = add( cntbits, BITS_DE_8SPOS ); /* Q0 */
1091 0 : qbidx[0] = sub( get_next_indice_fx( st_fx, BITS_ABS_ENG ), ABS_ENG_OFFSET ); /* Q0 */
1092 0 : move16();
1093 0 : cntbits = add( cntbits, BITS_ABS_ENG ); /* Q0 */
1094 0 : qbidx[pos_outlyer] = sub( get_next_indice_fx( st_fx, BITS_ABS_ENG ), ABS_ENG_OFFSET ); /* Q0 */
1095 0 : move16();
1096 0 : cntbits = add( cntbits, BITS_ABS_ENG ); /* Q0 */
1097 : }
1098 : ELSE
1099 : {
1100 0 : pos_outlyer = 0;
1101 0 : move16();
1102 0 : qbidx[0] = sub( get_next_indice_fx( st_fx, BITS_ABS_ENG ), ABS_ENG_OFFSET ); /* Q0 */
1103 0 : move16();
1104 0 : cntbits = add( cntbits, BITS_ABS_ENG ); /* Q0 */
1105 : }
1106 :
1107 0 : FOR( i = 1; i < pos_outlyer; ++i )
1108 : {
1109 0 : bits = 0;
1110 0 : move16();
1111 0 : qbidx[i] = sub( decode_huff_8s_fx( st_fx, hestable, &bits ), 4 ); /* Q0 */
1112 0 : move16();
1113 0 : cntbits = add( cntbits, bits ); /* Q0 */
1114 : }
1115 :
1116 0 : FOR( i = pos_outlyer + 1; i < bands; ++i )
1117 : {
1118 0 : bits = 0;
1119 0 : move16();
1120 0 : qbidx[i] = sub( decode_huff_8s_fx( st_fx, hestable, &bits ), 4 ); /* Q0 */
1121 0 : move16();
1122 0 : cntbits = add( cntbits, bits ); /* Q0 */
1123 : }
1124 : }
1125 : }
1126 : ELSE
1127 : {
1128 1 : basic_shift = get_next_indice_fx( st_fx, BITS_MAX_DEPTH ); /* Q0 */
1129 1 : cntbits = add( cntbits, BITS_MAX_DEPTH ); /* Q0 */
1130 :
1131 23 : FOR( i = 0; i < bands; ++i )
1132 : {
1133 22 : bits = 0;
1134 22 : move16();
1135 22 : qbidx[i] = sub( decode_huff_8s_fx( st_fx, hestable, &bits ), 4 ); /* Q0 */
1136 22 : move16();
1137 22 : qbidx[i] = shl( qbidx[i], basic_shift ); /* Q0 */
1138 22 : move16();
1139 22 : cntbits = add( cntbits, bits ); /* Q0 */
1140 : }
1141 :
1142 23 : FOR( i = 0; i < bands; ++i )
1143 : {
1144 22 : LSB[0] = get_next_indice_fx( st_fx, basic_shift ); /* Q0 */
1145 22 : move16();
1146 22 : qbidx[i] = add( qbidx[i], LSB[0] ); /* Q0 */
1147 22 : move16();
1148 22 : cntbits = add( cntbits, basic_shift ); /* Q0 */
1149 : }
1150 : }
1151 :
1152 2 : return cntbits; /* xx bits for diff. energies + 1 bit for LC coding mode */
1153 : }
1154 :
1155 :
1156 : /*--------------------------------------------------------------------------*
1157 : * band_energy_dequant()
1158 : *
1159 : *
1160 : *--------------------------------------------------------------------------*/
1161 :
1162 34 : static Word16 band_energy_dequant_fx(
1163 : Decoder_State *st_fx, /* i/o: decoder state structure */
1164 : Word32 L_band_energy[], /* o : band energy Q14*/
1165 : const Word16 bands_fx, /* Q0 */
1166 : const Word32 L_qint, /* Q0 */
1167 : const Word16 eref_fx, /* Q0 */
1168 : const Word16 is_transient_fx /* Q0 */
1169 : )
1170 : {
1171 : Word16 k;
1172 : Word16 deng_cmode;
1173 : Word16 deng_bits;
1174 :
1175 : Word16 rev_qint_fx;
1176 : Word16 Qrev_qint;
1177 :
1178 : Word16 bq0_fx;
1179 : Word16 bq1_fx[BANDS_MAX];
1180 : Word16 bq2_fx[BANDS_MAX];
1181 :
1182 : Word16 exp_normd;
1183 :
1184 :
1185 : /* parsing energy difference coding mode */
1186 34 : deng_cmode = get_next_indice_fx( st_fx, BITS_DE_CMODE ); /* Q0 */
1187 :
1188 34 : IF( deng_cmode == 0 )
1189 : {
1190 2 : deng_bits = large_symbol_dec_fx( st_fx, bq2_fx, bands_fx ); /* Q0 */
1191 :
1192 : /* counting 1 bit for deng coding mode */
1193 2 : deng_bits = add( deng_bits, BITS_DE_CMODE ); /* Q0 */
1194 : }
1195 : ELSE
1196 : {
1197 32 : IF( is_transient_fx )
1198 : {
1199 2 : deng_bits = small_symbol_dec_tran_fx( st_fx, bq2_fx, bands_fx, is_transient_fx ); /* Q0 */
1200 : }
1201 : ELSE
1202 : {
1203 30 : deng_bits = small_symbol_dec_fx( st_fx, bq2_fx, bands_fx, is_transient_fx ); /* Q0 */
1204 : }
1205 :
1206 : /* counting 1 bit for deng coding mode */
1207 32 : deng_bits = add( deng_bits, BITS_DE_CMODE ); /* Q0 */
1208 : }
1209 :
1210 34 : exp_normd = norm_l( L_qint );
1211 34 : rev_qint_fx = div_s( 0x4000, round_fx( L_shl( L_qint, exp_normd ) ) ); /* Q14-(29+exp_normd-16)+15 */
1212 34 : Qrev_qint = sub( 14 - ( 29 - 16 ) + 15, exp_normd );
1213 :
1214 34 : bq0_fx = round_fx( L_shl( L_mult( eref_fx, rev_qint_fx ), sub( 5, Qrev_qint ) ) ); /* 16-(10+Qrev_qint+1) */
1215 :
1216 34 : bq1_fx[0] = add( bq2_fx[0], bq0_fx );
1217 34 : move16();
1218 778 : FOR( k = 1; k < bands_fx; k++ )
1219 : {
1220 744 : bq1_fx[k] = add( bq2_fx[k], bq1_fx[k - 1] );
1221 744 : move16();
1222 : }
1223 :
1224 812 : FOR( k = 0; k < bands_fx; k++ )
1225 : {
1226 778 : L_band_energy[k] = Mpy_32_16_1( L_qint, bq1_fx[k] );
1227 778 : move32(); /* 29+0-15 -> Qbe(Q14) */
1228 : }
1229 :
1230 34 : IF( is_transient_fx )
1231 : {
1232 3 : reverse_transient_frame_energies_fx( L_band_energy, bands_fx );
1233 : }
1234 :
1235 34 : return ( deng_bits );
1236 : }
1237 :
1238 :
1239 : /*--------------------------------------------------------------------------*
1240 : * p2a_threshold_dequant()
1241 : *
1242 : *
1243 : *--------------------------------------------------------------------------*/
1244 :
1245 34 : static Word16 p2a_threshold_dequant_fx(
1246 : Decoder_State *st_fx, /* i/o: decoder state structure */
1247 : Word16 *p2a_flags, /* o : tonaly indicator Q0*/
1248 : const Word16 bands, /* i : number of subbands Q0*/
1249 : const Word16 p2a_bands /* i : number of subbnads for computing tonality Q0*/
1250 : )
1251 : {
1252 : Word16 j, k;
1253 :
1254 34 : j = sub( bands, p2a_bands );
1255 657 : FOR( k = 0; k < j; k++ )
1256 : {
1257 623 : p2a_flags[k] = 1; /* Q0 */
1258 623 : move16();
1259 : }
1260 :
1261 34 : j = 0;
1262 34 : move16();
1263 189 : FOR( k = bands - p2a_bands; k < bands; k++ )
1264 : {
1265 155 : p2a_flags[k] = get_next_indice_fx( st_fx, 1 ); /* Q0 */
1266 155 : move16();
1267 155 : j++;
1268 : }
1269 :
1270 34 : return ( j );
1271 : }
1272 :
1273 :
1274 : /*--------------------------------------------------------------------------*
1275 : * mdct_spectrum_fine_gain_dec()
1276 : *
1277 : *
1278 : *--------------------------------------------------------------------------*/
1279 :
1280 34 : static void mdct_spectrum_fine_gain_dec_fx(
1281 : Decoder_State *st_fx, /* i/o: decoder state structure */
1282 : Word32 L_y2[], /* i/o: Q12 : decoded spectrum */
1283 : const Word16 band_start[], /* i : Q0 : table of start freq for every subband */
1284 : const Word16 band_end[], /* i : Q0 : table of end freq for every subband */
1285 : const Word16 k_sort[], /* i : Q0 : sort table by band_energy */
1286 : const Word16 bands, /* i : Q0 : nubmber of subbands */
1287 : const Word32 L_qint, /* i : Q0 : */
1288 : const Word16 Ngq, /* i : Q0 : */
1289 : const Word16 gqlevs, /* i : Q0 : quantized level */
1290 : const Word16 gqbits /* i : Q0 : quantized bits */
1291 : )
1292 : {
1293 : Word16 i, k, imin_fx;
1294 :
1295 : Word16 delta_fx, Qdelta;
1296 : Word32 L_delta;
1297 : Word32 L_q;
1298 :
1299 : Word16 gain_table_fx[MAX_GQLEVS];
1300 : Word16 Qgt;
1301 : Word16 gamma_fx; /* Q14 */
1302 :
1303 : Word16 exp_normn, exp_normd;
1304 :
1305 : Word32 L_temp;
1306 : Word16 temp_lo_fx, temp_hi_fx;
1307 :
1308 : /* Fine gain quantization on only the most significant energy bands */
1309 :
1310 34 : exp_normn = norm_l( L_qint );
1311 34 : exp_normn = sub( exp_normn, 1 );
1312 34 : exp_normd = norm_s( gqlevs );
1313 34 : delta_fx = div_l( L_shl( L_qint, exp_normn ), shl( gqlevs, exp_normd ) ); /* Q15 */
1314 34 : Qdelta = add( sub( exp_normn, exp_normd ), 28 ); /* 29+exp_normn-(exp_normd)-1; */
1315 34 : L_delta = L_shl( L_deposit_h( delta_fx ), sub( 13, Qdelta ) ); /* Q18 - Qdelta */
1316 :
1317 34 : L_q = L_shr( L_sub( L_delta, L_qint ), 1 ); /* Q -1*/
1318 :
1319 102 : FOR( i = 0; i < gqlevs; i++ )
1320 : {
1321 : /*gain_table[i] = (float) pow (2.0f, q * 0.5f); */
1322 68 : L_temp = L_shr( L_shr( L_q, 1 ), sub( 29, 16 ) );
1323 68 : temp_lo_fx = L_Extract_lc( L_temp, &temp_hi_fx );
1324 68 : Qgt = sub( 14, temp_hi_fx );
1325 68 : gain_table_fx[i] = extract_l( Pow2( 14, temp_lo_fx ) ); /* Qgt */
1326 68 : move16();
1327 :
1328 : /*q += delta; */
1329 68 : L_q = L_add( L_q, L_delta );
1330 68 : gain_table_fx[i] = shl( gain_table_fx[i], sub( 14, Qgt ) ); /* Qgt -> Q14 */
1331 68 : move16();
1332 : }
1333 :
1334 170 : FOR( k = bands - Ngq; k < bands; k++ )
1335 : {
1336 136 : imin_fx = get_next_indice_fx( st_fx, gqbits ); /* Q0 */
1337 :
1338 : /*gamma = gain_table[imin]; */
1339 136 : gamma_fx = gain_table_fx[imin_fx];
1340 136 : move16();
1341 :
1342 2462 : FOR( i = band_start[k_sort[k]]; i <= band_end[k_sort[k]]; i++ )
1343 : {
1344 : /* This IF statement for keeping same mantissa evenif y2 is plus or minus */
1345 2326 : IF( L_y2[i] >= 0x0 )
1346 : {
1347 2138 : L_y2[i] = L_shl( Mpy_32_16_1( L_y2[i], gamma_fx ), 1 ); /* Q12 */
1348 2138 : move32();
1349 : }
1350 : ELSE
1351 : {
1352 188 : L_y2[i] = L_negate( L_shl( Mpy_32_16_1( L_abs( L_y2[i] ), gamma_fx ), 1 ) ); /* Q12 */
1353 188 : move32();
1354 : }
1355 : }
1356 : }
1357 :
1358 34 : return;
1359 : }
|