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 30 : 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 30 : j = 0;
69 30 : move16();
70 150 : FOR( k = bands - SPT_SHORTEN_SBNUM; k < bands; k++ )
71 : {
72 120 : spt_shorten_flag[j] = 0;
73 120 : move16();
74 120 : 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 120 : j++;
92 : }
93 :
94 30 : return;
95 : }
96 :
97 : /*-------------------------------------------------------------------*
98 : * hq_lr_dec_fx()
99 : *
100 : * HQ low rate decoding routine
101 : *-------------------------------------------------------------------*/
102 :
103 32 : 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 32 : HQ_DEC_HANDLE hHQ_core = st_fx->hHQ_core;
180 :
181 32 : tmp2 = 0; /* to avoid compilation flags */
182 32 : move16();
183 :
184 32 : set16_fx( last_bitalloc_max_band, 0, 2 );
185 32 : set32_fx( L_y2, 0x0L, L_FRAME48k );
186 32 : set16_fx( inp_vector_fx, 0, inner_frame );
187 32 : flag_spt_fx = 0;
188 32 : move16();
189 32 : set16_fx( prev_SWB_peak_pos_tmp_fx, 0, SPT_SHORTEN_SBNUM );
190 32 : adjustFlag = 0;
191 32 : move16();
192 32 : bw_low = 0;
193 32 : move16();
194 32 : bw_high = 20;
195 32 : move16();
196 32 : enerL_fx = L_deposit_l( 0 );
197 32 : enerH_fx = L_deposit_l( 0 );
198 :
199 32 : L_bwe_br = L_add( st_fx->core_brate, 0 );
200 32 : hqswb_clas_fx = 0;
201 32 : move16();
202 32 : test();
203 32 : test();
204 32 : IF( EQ_16( st_fx->bwidth, SWB ) && ( EQ_32( L_bwe_br, HQ_16k40 ) || EQ_32( L_bwe_br, HQ_13k20 ) ) )
205 : {
206 32 : hqswb_clas_fx = get_next_indice_fx( st_fx, 2 ); /* Q0 */
207 32 : num_bits = sub( num_bits, 2 ); /* Q0 */
208 :
209 32 : *is_transient_fx = 0;
210 32 : move16();
211 32 : if ( EQ_16( hqswb_clas_fx, HQ_TRANSIENT ) )
212 : {
213 2 : *is_transient_fx = 1; /* Q0 */
214 2 : 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 32 : 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 32 : highlength_fx = band_end[bands_fx - 1]; /* Q0 */
230 32 : move16();
231 32 : har_bands_fx = bands_fx; /* Q0 */
232 32 : move16();
233 :
234 32 : test();
235 32 : test();
236 32 : test();
237 32 : 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 30 : test();
241 30 : IF( EQ_16( hqswb_clas_fx, HQ_NORMAL ) || EQ_16( hqswb_clas_fx, HQ_HARMONIC ) )
242 : {
243 30 : num_bits = sub( num_bits, get_usebit_npswb_fx( hqswb_clas_fx ) ); /* Q0 */
244 : }
245 30 : if ( EQ_16( hqswb_clas_fx, HQ_NORMAL ) )
246 : {
247 30 : flag_spt_fx = 1; /* Q0 */
248 30 : move16();
249 : }
250 : }
251 :
252 32 : test();
253 32 : test();
254 32 : IF( ( EQ_32( L_bwe_br, HQ_16k40 ) || EQ_32( L_bwe_br, HQ_13k20 ) ) && EQ_16( st_fx->bwidth, SWB ) )
255 : {
256 32 : IF( NE_16( hHQ_core->prev_hqswb_clas, HQ_NORMAL ) )
257 : {
258 2 : j = 0;
259 2 : move16();
260 10 : FOR( k = bands_fx - SPT_SHORTEN_SBNUM; k < bands_fx; k++ )
261 : {
262 8 : hHQ_core->prev_SWB_peak_pos_fx[j] = 0;
263 8 : move16();
264 8 : j++;
265 : }
266 : }
267 : }
268 :
269 : /* Spectral energy calculation/quantization */
270 32 : 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 756 : 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 724 : test();
277 724 : 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 32 : exp_norm = norm_s( gqlevs_fx );
288 32 : gqbits_fx = sub( 14, exp_norm ); /* Q0 */
289 :
290 32 : 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 32 : pbits_fx = 0;
294 32 : move16();
295 32 : test();
296 32 : test();
297 32 : IF( EQ_16( st_fx->bwidth, SWB ) && ( EQ_32( L_bwe_br, HQ_16k40 ) || EQ_32( L_bwe_br, HQ_13k20 ) ) )
298 : {
299 32 : 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 32 : pbits_fx = p2a_threshold_dequant_fx( st_fx, p2a_flags_fx, bands_fx, p2a_bands_fx ); /* Q0 */
306 32 : bit_budget_fx = sub( bit_budget_fx, pbits_fx ); /* Q0 */
307 :
308 32 : IF( EQ_16( hqswb_clas_fx, HQ_NORMAL ) )
309 : {
310 30 : 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 32 : IF( EQ_16( flag_spt_fx, 1 ) )
321 : {
322 : /* initialize the desired parameters for SPT */
323 30 : spt_shorten_domain_band_save_fx( bands_fx, band_start, band_end, band_width, org_band_start, org_band_end, org_band_width );
324 30 : 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 30 : 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 32 : 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 32 : Q_band_energy = SWB_BWE_LR_Qbe;
338 32 : move16();
339 756 : FOR( i = 0; i < bands_fx; i++ )
340 : {
341 724 : L_tmp = L_shl( L_band_energy[i], sub( 16, Q_band_energy ) ); /*Q16 */
342 :
343 724 : frac1 = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */
344 724 : L_tmp = Pow2( 30, frac1 );
345 724 : exp = sub( exp, 30 );
346 724 : Ep_fx[i] = L_shl_sat( L_tmp, s_max( sub( exp, 6 ), -31 ) ); /* Q -6 */
347 724 : move32();
348 : }
349 :
350 756 : FOR( i = 0; i < bands_fx; i++ )
351 : {
352 724 : L_tmp2 = Ep_fx[i]; /* Q -6 */
353 724 : move32();
354 724 : L_tmp = L_max( 1, L_tmp2 );
355 724 : exp = norm_l( L_tmp );
356 724 : tmp = extract_h( L_shl( L_tmp, exp ) );
357 :
358 724 : L_tmp3 = (Word32) band_width[i];
359 724 : move16();
360 724 : exp2 = norm_l( L_tmp3 );
361 724 : tmp2 = extract_h( L_shl( L_tmp3, exp2 ) );
362 :
363 724 : exp2 = sub( exp, exp2 ); /* Denormalize and substract */
364 :
365 724 : tmp3 = sub( tmp2, tmp );
366 724 : if ( tmp3 > 0 )
367 : {
368 270 : tmp2 = shr( tmp2, 1 );
369 : }
370 724 : if ( tmp3 > 0 )
371 : {
372 270 : exp2 = add( exp2, 1 );
373 : }
374 724 : tmp = div_s( tmp2, tmp ); /* Q15 */
375 724 : L_tmp = L_deposit_h( tmp );
376 724 : L_tmp = Isqrt_lc( L_tmp, &exp2 ); /*Q(31-exp2) */
377 724 : Ep_tmp_fx[i] = L_shr( L_tmp, sub( 15, exp2 ) ); /*Q13 */
378 724 : move32();
379 : }
380 :
381 32 : test();
382 32 : test();
383 32 : test();
384 32 : test();
385 32 : test();
386 32 : test();
387 32 : 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 32 : 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 0 : enerH_fx = L_add_sat( enerH_fx, L_shl_sat( Ep_fx[i], 2 ) ); /*Q0 */
633 : }
634 0 : ELSE IF( GE_16( i, lowband ) )
635 : {
636 0 : enerL_fx = L_add_sat( enerL_fx, L_shl_sat( Ep_fx[i], 2 ) ); /*Q0 */
637 : }
638 : }
639 :
640 : /* modify the last p2a_bands subbands band_energies */
641 0 : Copy32( L_band_energy, L_band_energy_tmp, bands_fx ); /*Q_band_energy */
642 0 : L_tmp = L_max( enerH_fx, enerL_fx );
643 0 : tmp = s_max( bw_low, bw_high );
644 0 : i = norm_l( L_tmp );
645 0 : j = norm_s( tmp );
646 0 : L_tmp = Mult_32_16( L_shl( enerH_fx, i ), shl( bw_low, j ) ); /* i + j -15 */
647 0 : L_tmp2 = Mult_32_16( L_shl( enerL_fx, i ), shl( bw_high, j ) ); /*i + j -15 */
648 0 : L_tmp2 = L_sub( L_tmp, L_tmp2 );
649 0 : FOR( i = bands_fx - p2a_bands_fx; i < bands_fx; i++ )
650 : {
651 0 : test();
652 0 : IF( EQ_16( p2a_flags_fx[i], 1 ) || L_tmp2 > 0 )
653 : {
654 0 : tmp = sub( bands_fx, p2a_bands_fx );
655 0 : tmp = sub( tmp, lowband ); /*Q0 */
656 :
657 0 : tmp1 = extract_h( L_shl_sat( Ep_avrg_fx, 1 ) ); /*Q0 */
658 0 : IF( tmp1 != 0 )
659 : {
660 0 : exp = norm_s( tmp1 );
661 0 : tmp1 = shl( tmp1, exp ); /*Q(exp) */
662 0 : tmp1 = div_s( 16384, tmp1 ); /*Q(15+14-exp = 29-exp) */
663 0 : exp = sub( 29, exp );
664 : }
665 : ELSE
666 : {
667 : /*when the divisor is zero, happens rarely*/
668 0 : tmp1 = 0x7fff;
669 0 : move16();
670 0 : exp = 0;
671 0 : move16();
672 : }
673 0 : L_tmp = Mult_32_16( Ep_tmp_fx[i], tmp1 ); /*Q(15+exp-15 = exp) */
674 0 : L_tmp = Mult_32_16( L_tmp, tmp ); /*Q(exp+0-15 = exp-15) */
675 0 : L_tmp = Mult_32_16( L_tmp, 16384 ); /*Q(exp-15+13-15 = exp-17) */
676 0 : L_tmp = L_shl( L_tmp, sub( 32, exp ) ); /*Q15 */
677 0 : tmp = extract_l( L_min( L_tmp, 6554 ) ); /*Q15 */
678 0 : L_tmp = Mult_32_16( Ep_vari_fx, tmp1 ); /*Q(15+exp-15 = exp) */
679 0 : L_tmp = Mult_32_16( L_tmp, tmp ); /*Q(exp+15-15 = exp) */
680 0 : L_tmp = L_shl( L_tmp, sub( 15, exp ) ); /*Q15 */
681 0 : tmp = extract_l( L_shr( L_min( L_tmp, 13107 ), 1 ) ); /*Q14 */
682 :
683 0 : alpha_fx = add( tmp, 16384 ); /*Q14 */
684 : }
685 : ELSE
686 : {
687 0 : alpha_fx = 16384;
688 0 : move16(); /*Q14 */
689 : }
690 :
691 0 : IF( add( sub( i, bands_fx ), p2a_bands_fx ) > 0 )
692 : {
693 0 : tmp = sub( bands_fx, p2a_bands_fx );
694 0 : IF( EQ_16( last_bitalloc_max_band[i - ( tmp + 1 )], 1 ) )
695 : {
696 0 : tmp = sub( tmp, lowband );
697 0 : L_tmp = Mult_32_16( Ep_tmp_fx[i], tmp ); /*Q(15+0-15 = 0) */
698 0 : tmp = Calc_inv( L_shl_sat( L_tmp, 16 ), &exp );
699 0 : L_tmp = Mult_32_16( Ep_avrg_fx, tmp ); /*Q(15+exp-15 = exp) */
700 0 : L_tmp = L_shl( L_tmp, sub( 14, exp ) ); /*Q14 */
701 0 : tmp = extract_l( L_min( L_max( L_tmp, 16384 ), 20480 ) ); /*Q14 */
702 0 : L_tmp = L_mult( alpha_fx, tmp ); /*Q(14+14+1=29) */
703 0 : alpha_fx = extract_l( L_shr( L_tmp, 15 ) ); /*Q14 */
704 : }
705 : ELSE
706 : {
707 0 : tmp = sub( tmp, lowband );
708 :
709 0 : tmp1 = extract_h( L_shl_sat( Ep_avrg_fx, 1 ) ); /*Q0 */
710 0 : IF( tmp1 != 0 )
711 : {
712 0 : exp = norm_s( tmp1 );
713 0 : tmp1 = shl( tmp1, exp ); /*Q(exp) */
714 0 : tmp1 = div_s( 16384, tmp1 ); /*Q(15+14-exp=29-exp) */
715 0 : exp = sub( 29, exp );
716 : }
717 : ELSE
718 : {
719 : /*when the divisor is zero, happens rarely*/
720 0 : tmp1 = 0x7fff;
721 0 : move16();
722 0 : exp = 0;
723 0 : move16();
724 : }
725 0 : L_tmp = Mult_32_16( Ep_tmp_fx[i], tmp1 ); /*Q(15+exp-15 = exp) */
726 0 : L_tmp = Mult_32_16( L_tmp, tmp ); /*Q(exp+0-15 = exp-15) */
727 0 : L_tmp = L_shl( L_tmp, sub( 29, exp ) ); /*Q14 */
728 0 : tmp = extract_l( L_min( L_max( L_tmp, 13926 ), 16384 ) ); /*Q14 */
729 0 : L_tmp = L_mult( alpha_fx, tmp ); /*Q(14+14+1=29) */
730 0 : alpha_fx = extract_l( L_shr( L_tmp, 15 ) ); /*Q14 */
731 : }
732 : }
733 0 : L_tmp = Mult_32_16( L_band_energy_tmp[i], alpha_fx ); /*Q(Q_band_energy+14-15=Q_band_energy-1) */
734 0 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q Q_band_energy */
735 0 : move32();
736 : }
737 0 : lowband = 6;
738 0 : move16();
739 0 : Ep_avrg_fx = L_deposit_l( 0 );
740 0 : Ep_avrgL_fx = L_deposit_l( 0 );
741 0 : Ep_peak_fx = L_deposit_l( 0 );
742 0 : FOR( i = 0; i < bands_fx; i++ )
743 : {
744 0 : IF( GE_16( i, lowband ) )
745 : {
746 0 : Ep_avrg_fx = L_add_sat( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
747 : }
748 : ELSE
749 : {
750 0 : Ep_avrgL_fx = L_add_sat( Ep_avrgL_fx, Ep_tmp_fx[i] ); /*Q15 */
751 0 : if ( GT_32( Ep_tmp_fx[i], Ep_peak_fx ) )
752 : {
753 0 : Ep_peak_fx = L_add( Ep_tmp_fx[i], 0 ); /*Q15 */
754 : }
755 : }
756 : }
757 :
758 :
759 0 : L_tmp = Mult_32_16( Ep_peak_fx, 24576 ); /*Q(15+13-15 = 13) lowband = 6; */
760 0 : L_tmp2 = Mult_32_16( Ep_peak_fx, 19661 ); /*Q(15+14-15 = 14) */
761 0 : L_tmp3 = Mult_32_16( Ep_avrgL_fx, 24576 ); /*Q(15+12-15 = 12) */
762 :
763 0 : test();
764 0 : test();
765 0 : test();
766 0 : test();
767 0 : test();
768 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 ) ) ||
769 : ( 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 ) ) ) )
770 : {
771 0 : adjustFlag = 1;
772 0 : move16();
773 0 : FOR( i = 0; i < lowband; i++ )
774 : {
775 0 : tmp = Calc_inv( L_shl_sat( Ep_avrgL_fx, 1 ), &exp );
776 0 : L_tmp = Mult_32_16( Ep_peak_fx, tmp ); /*Q(15+exp-15 = exp) */
777 0 : L_tmp = Mult_32_16( L_tmp, lowband ); /*Q(exp+0-15 = exp-15) */
778 0 : L_tmp = L_shl( L_tmp, sub( 28, exp ) ); /*Q14 0.5 */
779 0 : tmp = extract_l( L_min( L_tmp, 19661 ) ); /*//Q14 */
780 0 : L_tmp = Mult_32_16( L_band_energy_tmp[i], tmp ); /*Q(Q_band_energy+14-15 = Q_band_energy-1) */
781 0 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q_band_energy */
782 0 : move32();
783 : }
784 : }
785 :
786 0 : hq2_bit_alloc_fx(
787 : L_band_energy_tmp, bands_fx, L_Rk, &bit_budget_fx, p2a_flags_fx, bit_alloc_weight_fx, band_width,
788 0 : num_bits, hqswb_clas_fx, st_fx->bwidth, *is_transient_fx );
789 : }
790 32 : 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 ) ) )
791 : {
792 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 );
793 : }
794 : ELSE
795 : {
796 32 : hq2_bit_alloc_fx(
797 : L_band_energy, bands_fx, L_Rk, &bit_budget_fx, p2a_flags_fx, bit_alloc_weight_fx, band_width,
798 32 : num_bits, hqswb_clas_fx, st_fx->bwidth, *is_transient_fx );
799 : }
800 :
801 32 : IF( bit_budget_fx < 0 )
802 : {
803 0 : st_fx->BER_detect = 1;
804 0 : move16();
805 0 : bit_budget_fx = 0;
806 0 : move16();
807 : }
808 32 : 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,
809 : p2a_flags_fx, p2a_bands_fx, last_bitalloc_max_band, inner_frame, adjustFlag, is_transient_fx );
810 :
811 : /* Denormalize the coded MDCT spectrum */
812 32 : 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 );
813 :
814 : /* Apply fine gain to denormalized coded spectrum */
815 32 : mdct_spectrum_fine_gain_dec_fx( st_fx, L_y2, band_start, band_end, k_sort_fx, bands_fx,
816 : L_qint, Ngq_fx, gqlevs_fx, gqbits_fx );
817 :
818 32 : test();
819 32 : test();
820 32 : test();
821 : /* Restore the band information */
822 32 : IF( EQ_16( flag_spt_fx, 1 ) )
823 : {
824 30 : spt_shorten_domain_band_restore_fx( bands_fx, band_start, band_end, band_width, org_band_start, org_band_end, org_band_width );
825 : }
826 :
827 32 : Copy32( L_y2, L_y2_org, L_FRAME32k ); /* Q12 */
828 :
829 : /* Inject noise into components having relatively low pulse energy per band */
830 32 : ni_seed_fx = add( add( add( npulses_fx[0], npulses_fx[1] ), npulses_fx[2] ), npulses_fx[3] );
831 32 : Copy32( L_y2, L_y2_ni, band_end[bands_fx - 1] + 1 ); /* Q12 */
832 :
833 32 : 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,
834 32 : hHQ_core->last_ni_gain_fx, hHQ_core->last_env_fx, &hHQ_core->last_max_pos_pulse, p2a_flags_fx, p2a_bands_fx,
835 32 : hqswb_clas_fx, st_fx->bwidth, L_bwe_br );
836 :
837 32 : test();
838 32 : test();
839 32 : IF( EQ_16( st_fx->bwidth, SWB ) && ( EQ_32( L_bwe_br, HQ_16k40 ) || EQ_32( L_bwe_br, HQ_13k20 ) ) )
840 : {
841 32 : test();
842 32 : IF( EQ_16( hqswb_clas_fx, HQ_NORMAL ) || EQ_16( hqswb_clas_fx, HQ_HARMONIC ) )
843 : {
844 30 : 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 );
845 :
846 30 : 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,
847 : 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 );
848 :
849 30 : 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 );
850 :
851 30 : IF( EQ_16( hqswb_clas_fx, HQ_NORMAL ) )
852 : {
853 30 : spt_swb_peakpos_tmp_save_fx( L_y2, bands_fx, band_start, band_end, prev_SWB_peak_pos_tmp_fx );
854 150 : FOR( k = 0; k < SPT_SHORTEN_SBNUM; k++ )
855 : {
856 120 : test();
857 120 : IF( p2a_flags_fx[bands_fx - SPT_SHORTEN_SBNUM + k] == 0 || npulses_fx[bands_fx - SPT_SHORTEN_SBNUM + k] == 0 )
858 : {
859 118 : prev_SWB_peak_pos_tmp_fx[k] = 0;
860 118 : move16();
861 : }
862 : }
863 : }
864 30 : Copy32( L_y2_ni, L_y2, lowlength_fx ); /* Q12 */
865 : }
866 : ELSE
867 : {
868 2 : Copy32( L_y2_ni, L_y2, add( band_end[bands_fx - 1], 1 ) ); /* HQ_TRANSIENT Q12*/
869 : }
870 : }
871 : ELSE
872 : {
873 0 : Copy32( L_y2_ni, L_y2, add( band_end[bands_fx - 1], 1 ) ); /* NB, WB Q12*/
874 : }
875 :
876 32 : test();
877 32 : IF( !( GE_16( st_fx->last_inner_frame, L_FRAME16k ) && st_fx->bws_cnt > 0 ) )
878 : {
879 32 : k1_fx = sub( bands_fx, 2 );
880 32 : if ( NE_16( *is_transient_fx, 1 ) )
881 : {
882 30 : k1_fx = sub( bands_fx, 6 ); /* Q1 */
883 : }
884 32 : L_tmp = L_deposit_l( 0 );
885 216 : FOR( i = k1_fx; i < bands_fx; i++ )
886 : {
887 184 : tmp = div_s( 1, sub( bands_fx, k1_fx ) ); /*Q15 */
888 184 : L_tmp = L_add( L_tmp, Mult_32_16( Ep_tmp_fx[i], tmp ) ); /*Q15 */
889 : }
890 32 : st_fx->prev_ener_shb_fx = extract_l( L_shr( L_tmp, 14 ) ); /* Q1 */
891 32 : move16();
892 : }
893 32 : test();
894 32 : IF( GE_16( st_fx->last_inner_frame, L_FRAME32k ) && st_fx->hBWE_FD != NULL )
895 : {
896 32 : set16_fx( st_fx->hBWE_FD->prev_SWB_fenv_fx, st_fx->prev_ener_shb_fx, SWB_FENV );
897 : }
898 :
899 32 : 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,
900 32 : 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 );
901 :
902 32 : return;
903 : }
904 :
905 : /*------------------------------------------------------------------------------------
906 : * small_symbol_dec_tran_fx()
907 : *
908 : * Huffman decoding of differential energies
909 : *--------------------------------------------------------------------------------------*/
910 :
911 1 : static Word16 small_symbol_dec_tran_fx(
912 : Decoder_State *st_fx, /* i/o: decoder state structure Q0*/
913 : Word16 *qbidx, /* o : output of dequantized differential energy Q0*/
914 : const Word16 bands, /* i : number of bands Q0*/
915 : const Word16 is_transient /* i : transient flag Q0*/
916 : )
917 : {
918 : Word16 i, bits;
919 : Word16 difidx[BANDS_MAX];
920 :
921 : /* Decoding differential energies*/
922 1 : bits = decode_envelope_indices_fx( st_fx, 0, bands, 0, difidx, LOW_RATE_HQ_CORE_TRAN, is_transient ); /* Q0 */
923 1 : bits = add( bits, BITS_DE_FCOMP ); /* Q0 */
924 :
925 : /* counting 1 bit for band_energy_huff_coding_mode */
926 1 : bits = add( bits, BITS_DE_HMODE ); /* Q0 */
927 :
928 : /* converting to original values */
929 33 : FOR( i = 0; i < bands; i++ )
930 : {
931 32 : qbidx[i] = sub( difidx[i], LRMDCT_BE_OFFSET ); /* Q0 */
932 32 : move16();
933 : }
934 :
935 1 : return ( bits );
936 : }
937 :
938 :
939 : /*--------------------------------------------------------------------------
940 : * small_symbol_dec()
941 : *
942 : * Huffman decoding of differential energies (MSB and LSB)
943 : *--------------------------------------------------------------------------*/
944 :
945 29 : static Word16 small_symbol_dec_fx( /* o : bits Q0*/
946 : Decoder_State *st_fx, /* i/o: decoder state structure */
947 : Word16 *qbidx, /* o : output of dequantized differential energy Q0*/
948 : const Word16 bands, /* i : number of bands Q0*/
949 : const Word16 is_transient )
950 : {
951 : Word16 i, LSB, bits;
952 : Word16 difidx[BANDS_MAX];
953 :
954 : /* Decoding MSB bits */
955 29 : bits = decode_envelope_indices_fx( st_fx, 0, bands, 0, difidx, LOW_RATE_HQ_CORE, is_transient ); /* Q0 */
956 29 : bits = add( bits, BITS_DE_FCOMP ); /* Q0 */
957 :
958 : /* counting 1 bit for band_energy_huff_coding_mode */
959 29 : bits = add( bits, BITS_DE_HMODE ); /* Q0 */
960 :
961 : /* Decoding LSB bit packing */
962 667 : FOR( i = 0; i < bands; ++i )
963 : {
964 638 : LSB = get_next_indice_fx( st_fx, BITS_DE_LSB ); /* Q0 */
965 638 : difidx[i] = s_or( shl( difidx[i], 1 ), LSB ); /* Q0 */
966 638 : move16();
967 : }
968 :
969 : /* counting bits for LSB */
970 29 : bits = add( bits, bands ); /* Q0 */
971 :
972 : /* converting to original values */
973 638 : FOR( i = 1; i < bands; ++i )
974 : {
975 609 : qbidx[i] = sub( difidx[i], DE_OFFSET1 ); /* Q0 */
976 609 : move16();
977 : }
978 :
979 29 : qbidx[0] = sub( difidx[0], DE_OFFSET0 ); /* Q0 */
980 29 : move16();
981 :
982 29 : return ( bits );
983 : }
984 :
985 :
986 54 : static Word16 decode_huff_8s_fx(
987 : Decoder_State *st_fx,
988 : const Word16 *hufftab, /* Q0 */
989 : Word16 *rbits /* Q0 */
990 : )
991 : {
992 : Word16 bit;
993 :
994 187 : WHILE( *hufftab > 0 )
995 : {
996 133 : *rbits = add( *rbits, s_and( *hufftab, 0xf ) ); /* Q0 */
997 133 : move16();
998 133 : bit = get_next_indice_fx( st_fx, s_and( *hufftab, 0xf ) ); /* Q0 */
999 133 : move16();
1000 133 : hufftab += add( shr( *hufftab, 4 ), bit ); /* Q0 */
1001 : }
1002 :
1003 54 : return negate( *hufftab );
1004 : }
1005 :
1006 2 : static Word16 large_symbol_dec_fx( /* o : bits */
1007 : Decoder_State *st_fx, /* i/o: decoder state structure */
1008 : Word16 *qbidx, /* o : output of dequantized differential energy Q0*/
1009 : const Word16 bands /* i : number of bands Q0*/
1010 : )
1011 : {
1012 : Word16 i, bits;
1013 : Word16 LSB[BANDS_MAX];
1014 : Word16 basic_shift, cntbits, ns2mode;
1015 : Word16 pos_outlyer;
1016 : Word16 ns2mode0, ns2mode1;
1017 :
1018 2 : cntbits = BITS_DE_8SMODE; /* Q0 */
1019 2 : move16();
1020 2 : ns2mode = get_next_indice_fx( st_fx, BITS_DE_8SMODE ); /* Q0 */
1021 :
1022 2 : IF( ns2mode == 0 )
1023 : {
1024 1 : ns2mode0 = get_next_indice_fx( st_fx, BITS_DE_8SMODE_N0 ); /* Q0 */
1025 1 : ns2mode1 = get_next_indice_fx( st_fx, BITS_DE_8SMODE_N1 ); /* Q0 */
1026 1 : cntbits = add( cntbits, BITS_DE_8SMODE_N0 + BITS_DE_8SMODE_N1 ); /* Q0 */
1027 :
1028 1 : IF( ns2mode0 == 0 )
1029 : {
1030 1 : IF( EQ_16( ns2mode1, 1 ) )
1031 : {
1032 0 : pos_outlyer = get_next_indice_fx( st_fx, BITS_DE_8SPOS ); /* Q0 */
1033 0 : cntbits = add( cntbits, BITS_DE_8SPOS ); /* Q0 */
1034 0 : qbidx[pos_outlyer] = sub( get_next_indice_fx( st_fx, BITS_ABS_ENG ), ABS_ENG_OFFSET ); /* Q0 */
1035 0 : move16();
1036 0 : cntbits = add( cntbits, BITS_ABS_ENG ); /* Q0 */
1037 : }
1038 : ELSE
1039 : {
1040 1 : pos_outlyer = -1; /* Q0 */
1041 1 : move16();
1042 : }
1043 :
1044 1 : FOR( i = 0; i < pos_outlyer; ++i )
1045 : {
1046 0 : bits = 0;
1047 0 : move16();
1048 0 : qbidx[i] = sub( decode_huff_8s_fx( st_fx, hestable, &bits ), 4 ); /* Q0 */
1049 0 : move16();
1050 0 : cntbits = add( cntbits, bits ); /* Q0 */
1051 : }
1052 :
1053 33 : FOR( i = pos_outlyer + 1; i < bands; ++i )
1054 : {
1055 32 : bits = 0;
1056 32 : move16();
1057 32 : qbidx[i] = sub( decode_huff_8s_fx( st_fx, hestable, &bits ), 4 ); /* Q0 */
1058 32 : move16();
1059 32 : cntbits = add( cntbits, bits ); /* Q0 */
1060 : }
1061 : }
1062 : ELSE
1063 : {
1064 0 : IF( EQ_16( ns2mode1, 1 ) )
1065 : {
1066 0 : pos_outlyer = get_next_indice_fx( st_fx, BITS_DE_8SPOS ); /* Q0 */
1067 0 : cntbits = add( cntbits, BITS_DE_8SPOS ); /* Q0 */
1068 0 : qbidx[0] = sub( get_next_indice_fx( st_fx, BITS_ABS_ENG ), ABS_ENG_OFFSET ); /* Q0 */
1069 0 : move16();
1070 0 : cntbits = add( cntbits, BITS_ABS_ENG ); /* Q0 */
1071 0 : qbidx[pos_outlyer] = sub( get_next_indice_fx( st_fx, BITS_ABS_ENG ), ABS_ENG_OFFSET ); /* Q0 */
1072 0 : move16();
1073 0 : cntbits = add( cntbits, BITS_ABS_ENG ); /* Q0 */
1074 : }
1075 : ELSE
1076 : {
1077 0 : pos_outlyer = 0;
1078 0 : move16();
1079 0 : qbidx[0] = sub( get_next_indice_fx( st_fx, BITS_ABS_ENG ), ABS_ENG_OFFSET ); /* Q0 */
1080 0 : move16();
1081 0 : cntbits = add( cntbits, BITS_ABS_ENG ); /* Q0 */
1082 : }
1083 :
1084 0 : FOR( i = 1; i < pos_outlyer; ++i )
1085 : {
1086 0 : bits = 0;
1087 0 : move16();
1088 0 : qbidx[i] = sub( decode_huff_8s_fx( st_fx, hestable, &bits ), 4 ); /* Q0 */
1089 0 : move16();
1090 0 : cntbits = add( cntbits, bits ); /* Q0 */
1091 : }
1092 :
1093 0 : FOR( i = pos_outlyer + 1; i < bands; ++i )
1094 : {
1095 0 : bits = 0;
1096 0 : move16();
1097 0 : qbidx[i] = sub( decode_huff_8s_fx( st_fx, hestable, &bits ), 4 ); /* Q0 */
1098 0 : move16();
1099 0 : cntbits = add( cntbits, bits ); /* Q0 */
1100 : }
1101 : }
1102 : }
1103 : ELSE
1104 : {
1105 1 : basic_shift = get_next_indice_fx( st_fx, BITS_MAX_DEPTH ); /* Q0 */
1106 1 : cntbits = add( cntbits, BITS_MAX_DEPTH ); /* Q0 */
1107 :
1108 23 : FOR( i = 0; i < bands; ++i )
1109 : {
1110 22 : bits = 0;
1111 22 : move16();
1112 22 : qbidx[i] = sub( decode_huff_8s_fx( st_fx, hestable, &bits ), 4 ); /* Q0 */
1113 22 : move16();
1114 22 : qbidx[i] = shl( qbidx[i], basic_shift ); /* Q0 */
1115 22 : move16();
1116 22 : cntbits = add( cntbits, bits ); /* Q0 */
1117 : }
1118 :
1119 23 : FOR( i = 0; i < bands; ++i )
1120 : {
1121 22 : LSB[0] = get_next_indice_fx( st_fx, basic_shift ); /* Q0 */
1122 22 : move16();
1123 22 : qbidx[i] = add( qbidx[i], LSB[0] ); /* Q0 */
1124 22 : move16();
1125 22 : cntbits = add( cntbits, basic_shift ); /* Q0 */
1126 : }
1127 : }
1128 :
1129 2 : return cntbits; /* xx bits for diff. energies + 1 bit for LC coding mode */
1130 : }
1131 :
1132 :
1133 : /*--------------------------------------------------------------------------*
1134 : * band_energy_dequant()
1135 : *
1136 : *
1137 : *--------------------------------------------------------------------------*/
1138 :
1139 32 : static Word16 band_energy_dequant_fx(
1140 : Decoder_State *st_fx, /* i/o: decoder state structure */
1141 : Word32 L_band_energy[], /* o : band energy Q14*/
1142 : const Word16 bands_fx, /* Q0 */
1143 : const Word32 L_qint, /* Q0 */
1144 : const Word16 eref_fx, /* Q0 */
1145 : const Word16 is_transient_fx /* Q0 */
1146 : )
1147 : {
1148 : Word16 k;
1149 : Word16 deng_cmode;
1150 : Word16 deng_bits;
1151 :
1152 : Word16 rev_qint_fx;
1153 : Word16 Qrev_qint;
1154 :
1155 : Word16 bq0_fx;
1156 : Word16 bq1_fx[BANDS_MAX];
1157 : Word16 bq2_fx[BANDS_MAX];
1158 :
1159 : Word16 exp_normd;
1160 :
1161 :
1162 : /* parsing energy difference coding mode */
1163 32 : deng_cmode = get_next_indice_fx( st_fx, BITS_DE_CMODE ); /* Q0 */
1164 :
1165 32 : IF( deng_cmode == 0 )
1166 : {
1167 2 : deng_bits = large_symbol_dec_fx( st_fx, bq2_fx, bands_fx ); /* Q0 */
1168 :
1169 : /* counting 1 bit for deng coding mode */
1170 2 : deng_bits = add( deng_bits, BITS_DE_CMODE ); /* Q0 */
1171 : }
1172 : ELSE
1173 : {
1174 30 : IF( is_transient_fx )
1175 : {
1176 1 : deng_bits = small_symbol_dec_tran_fx( st_fx, bq2_fx, bands_fx, is_transient_fx ); /* Q0 */
1177 : }
1178 : ELSE
1179 : {
1180 29 : deng_bits = small_symbol_dec_fx( st_fx, bq2_fx, bands_fx, is_transient_fx ); /* Q0 */
1181 : }
1182 :
1183 : /* counting 1 bit for deng coding mode */
1184 30 : deng_bits = add( deng_bits, BITS_DE_CMODE ); /* Q0 */
1185 : }
1186 :
1187 32 : exp_normd = norm_l( L_qint );
1188 32 : rev_qint_fx = div_s( 0x4000, round_fx( L_shl( L_qint, exp_normd ) ) ); /* Q14-(29+exp_normd-16)+15 */
1189 32 : Qrev_qint = sub( 14 - ( 29 - 16 ) + 15, exp_normd );
1190 :
1191 32 : bq0_fx = round_fx( L_shl( L_mult( eref_fx, rev_qint_fx ), sub( 5, Qrev_qint ) ) ); /* 16-(10+Qrev_qint+1) */
1192 :
1193 32 : bq1_fx[0] = add( bq2_fx[0], bq0_fx );
1194 32 : move16();
1195 724 : FOR( k = 1; k < bands_fx; k++ )
1196 : {
1197 692 : bq1_fx[k] = add( bq2_fx[k], bq1_fx[k - 1] );
1198 692 : move16();
1199 : }
1200 :
1201 756 : FOR( k = 0; k < bands_fx; k++ )
1202 : {
1203 724 : L_band_energy[k] = Mpy_32_16_1( L_qint, bq1_fx[k] );
1204 724 : move32(); /* 29+0-15 -> Qbe(Q14) */
1205 : }
1206 :
1207 32 : IF( is_transient_fx )
1208 : {
1209 2 : reverse_transient_frame_energies_fx( L_band_energy, bands_fx );
1210 : }
1211 :
1212 32 : return ( deng_bits );
1213 : }
1214 :
1215 :
1216 : /*--------------------------------------------------------------------------*
1217 : * p2a_threshold_dequant()
1218 : *
1219 : *
1220 : *--------------------------------------------------------------------------*/
1221 :
1222 32 : static Word16 p2a_threshold_dequant_fx(
1223 : Decoder_State *st_fx, /* i/o: decoder state structure */
1224 : Word16 *p2a_flags, /* o : tonaly indicator Q0*/
1225 : const Word16 bands, /* i : number of subbands Q0*/
1226 : const Word16 p2a_bands /* i : number of subbnads for computing tonality Q0*/
1227 : )
1228 : {
1229 : Word16 j, k;
1230 :
1231 32 : j = sub( bands, p2a_bands );
1232 606 : FOR( k = 0; k < j; k++ )
1233 : {
1234 574 : p2a_flags[k] = 1; /* Q0 */
1235 574 : move16();
1236 : }
1237 :
1238 32 : j = 0;
1239 32 : move16();
1240 182 : FOR( k = bands - p2a_bands; k < bands; k++ )
1241 : {
1242 150 : p2a_flags[k] = get_next_indice_fx( st_fx, 1 ); /* Q0 */
1243 150 : move16();
1244 150 : j++;
1245 : }
1246 :
1247 32 : return ( j );
1248 : }
1249 :
1250 :
1251 : /*--------------------------------------------------------------------------*
1252 : * mdct_spectrum_fine_gain_dec()
1253 : *
1254 : *
1255 : *--------------------------------------------------------------------------*/
1256 :
1257 32 : static void mdct_spectrum_fine_gain_dec_fx(
1258 : Decoder_State *st_fx, /* i/o: decoder state structure */
1259 : Word32 L_y2[], /* i/o: Q12 : decoded spectrum */
1260 : const Word16 band_start[], /* i : Q0 : table of start freq for every subband */
1261 : const Word16 band_end[], /* i : Q0 : table of end freq for every subband */
1262 : const Word16 k_sort[], /* i : Q0 : sort table by band_energy */
1263 : const Word16 bands, /* i : Q0 : nubmber of subbands */
1264 : const Word32 L_qint, /* i : Q0 : */
1265 : const Word16 Ngq, /* i : Q0 : */
1266 : const Word16 gqlevs, /* i : Q0 : quantized level */
1267 : const Word16 gqbits /* i : Q0 : quantized bits */
1268 : )
1269 : {
1270 : Word16 i, k, imin_fx;
1271 :
1272 : Word16 delta_fx, Qdelta;
1273 : Word32 L_delta;
1274 : Word32 L_q;
1275 :
1276 : Word16 gain_table_fx[MAX_GQLEVS];
1277 : Word16 Qgt;
1278 : Word16 gamma_fx; /* Q14 */
1279 :
1280 : Word16 exp_normn, exp_normd;
1281 :
1282 : Word32 L_temp;
1283 : Word16 temp_lo_fx, temp_hi_fx;
1284 :
1285 : /* Fine gain quantization on only the most significant energy bands */
1286 :
1287 32 : exp_normn = norm_l( L_qint );
1288 32 : exp_normn = sub( exp_normn, 1 );
1289 32 : exp_normd = norm_s( gqlevs );
1290 32 : delta_fx = div_l( L_shl( L_qint, exp_normn ), shl( gqlevs, exp_normd ) ); /* Q15 */
1291 32 : Qdelta = add( sub( exp_normn, exp_normd ), 28 ); /* 29+exp_normn-(exp_normd)-1; */
1292 32 : L_delta = L_shl( L_deposit_h( delta_fx ), sub( 13, Qdelta ) ); /* Q18 - Qdelta */
1293 :
1294 32 : L_q = L_shr( L_sub( L_delta, L_qint ), 1 ); /* Q -1*/
1295 :
1296 96 : FOR( i = 0; i < gqlevs; i++ )
1297 : {
1298 : /*gain_table[i] = (float) pow (2.0f, q * 0.5f); */
1299 64 : L_temp = L_shr( L_shr( L_q, 1 ), sub( 29, 16 ) );
1300 64 : temp_lo_fx = L_Extract_lc( L_temp, &temp_hi_fx );
1301 64 : Qgt = sub( 14, temp_hi_fx );
1302 64 : gain_table_fx[i] = extract_l( Pow2( 14, temp_lo_fx ) ); /* Qgt */
1303 64 : move16();
1304 :
1305 : /*q += delta; */
1306 64 : L_q = L_add( L_q, L_delta );
1307 64 : gain_table_fx[i] = shl( gain_table_fx[i], sub( 14, Qgt ) ); /* Qgt -> Q14 */
1308 64 : move16();
1309 : }
1310 :
1311 160 : FOR( k = bands - Ngq; k < bands; k++ )
1312 : {
1313 128 : imin_fx = get_next_indice_fx( st_fx, gqbits ); /* Q0 */
1314 :
1315 : /*gamma = gain_table[imin]; */
1316 128 : gamma_fx = gain_table_fx[imin_fx];
1317 128 : move16();
1318 :
1319 2329 : FOR( i = band_start[k_sort[k]]; i <= band_end[k_sort[k]]; i++ )
1320 : {
1321 : /* This IF statement for keeping same mantissa evenif y2 is plus or minus */
1322 2201 : IF( L_y2[i] >= 0x0 )
1323 : {
1324 2024 : L_y2[i] = L_shl( Mpy_32_16_1( L_y2[i], gamma_fx ), 1 ); /* Q12 */
1325 2024 : move32();
1326 : }
1327 : ELSE
1328 : {
1329 177 : L_y2[i] = L_negate( L_shl( Mpy_32_16_1( L_abs( L_y2[i] ), gamma_fx ), 1 ) ); /* Q12 */
1330 177 : move32();
1331 : }
1332 : }
1333 : }
1334 :
1335 32 : return;
1336 : }
|