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