Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include <math.h>
43 : #include "cnst.h"
44 : #include "rom_com.h"
45 : #include "rom_dec.h"
46 : #include "prot.h"
47 : #include "stl.h"
48 : #include "basop_util.h"
49 : #include "wmc_auto.h"
50 :
51 : /*--------------------------------------------------------------------------*
52 : * Local function prototypes
53 : *--------------------------------------------------------------------------*/
54 :
55 : static int16_t p2a_threshold_dequant( Decoder_State *st, int16_t *p2a_flags, const int16_t bands, const int16_t p2a_bands );
56 :
57 : static void mdct_spectrum_fine_gain_dec( Decoder_State *st, float y2[], const int16_t band_start[], const int16_t band_end[], const int16_t k_sort[], const int16_t bands, const Word32 L_qint, const int16_t Ngq, const int16_t gqlevs, const int16_t gqbits );
58 :
59 : static float band_energy_dequant( Decoder_State *st, float band_energy[], const int16_t bands, const Word32 L_qint, const Word16 eref_fx, const int16_t is_transient );
60 :
61 : static void spt_shorten_domain_set_dec( Decoder_State *st, const int16_t p2a_flags[], const int16_t new_band_start[], const int16_t new_band_end[], const int16_t new_band_width[], const int16_t bands, int16_t band_start[], int16_t band_end[], int16_t band_width[], int16_t *bit_budget );
62 :
63 :
64 : /*-------------------------------------------------------------------*
65 : * hq_lr_dec()
66 : *
67 : * HQ low rate decoding routine
68 : *-------------------------------------------------------------------*/
69 :
70 102 : void hq_lr_dec(
71 : Decoder_State *st, /* i/o: decoder state structure */
72 : float yout[], /* o : transform-domain output coefs. */
73 : const int16_t inner_frame, /* i : inner frame length */
74 : int16_t num_bits, /* i : number of available bits */
75 : int16_t *is_transient /* o : transient flag */
76 : )
77 : {
78 : int16_t i, k1, pbits, p2a_flags[BANDS_MAX], bit_budget, bands, length, gqlevs, gqbits, Ngq, p2a_bands, ni_seed;
79 : int16_t band_start[BANDS_MAX], band_end[BANDS_MAX], band_width[BANDS_MAX];
80 : int16_t k_sort[BANDS_MAX], npulses[BANDS_MAX];
81 : int32_t inp_vector[L_FRAME48k];
82 : float ni_coef, ni_pd_th, pd_thresh, ld_slope;
83 : float ebits, Rk[BANDS_MAX], band_energy[BANDS_MAX], y2[L_FRAME48k], p2a_th;
84 : Word32 Rk_fx[BANDS_MAX];
85 : Word32 L_qint; /* Q29 */
86 : Word16 eref_fx; /* Q10 */
87 : Word16 bit_alloc_weight_fx; /* Q13 */
88 : float y2_ni[L_FRAME48k], y2_org[L_FRAME48k];
89 102 : int16_t hqswb_clas = 0;
90 102 : int16_t lowlength, highlength, har_bands = 0;
91 : float m[L_FRAME32k];
92 102 : float Ep[BANDS_MAX], enerH = 0.0f, enerL = 0.0f;
93 102 : int16_t lowband, highband, bw_low = 0, bw_high = 20;
94 : float Ep_tmp[BANDS_MAX];
95 : float band_energy_tmp[BANDS_MAX];
96 : int16_t last_bitalloc_max_band[2];
97 : int32_t bwe_br;
98 : int16_t trans_bit, p2a_flags_tmp[BANDS_MAX];
99 102 : int16_t adjustFlag = 0;
100 : int16_t prev_SWB_peak_pos_tmp[SPT_SHORTEN_SBNUM];
101 : int16_t j, k;
102 : int16_t flag_spt;
103 : int16_t org_band_start[SPT_SHORTEN_SBNUM];
104 : int16_t org_band_end[SPT_SHORTEN_SBNUM];
105 : int16_t org_band_width[SPT_SHORTEN_SBNUM];
106 : int16_t new_band_start[SPT_SHORTEN_SBNUM];
107 : int16_t new_band_end[SPT_SHORTEN_SBNUM];
108 : int16_t new_band_width[SPT_SHORTEN_SBNUM];
109 : Word32 L_tmp, L_tmp2, L_tmp3;
110 : Word16 exp, tmp, exp2, tmp1, tmp2, tmp3, alpha_fx, frac1;
111 : Word32 enerH_fx;
112 : Word32 enerL_fx;
113 : Word32 Ep_fx[BANDS_MAX];
114 : Word32 Ep_avrg_fx, Ep_vari_fx;
115 : Word32 Ep_avrgL_fx;
116 : Word32 Ep_peak_fx;
117 : Word32 Ep_tmp_fx[BANDS_MAX];
118 : Word16 gama_fx; /*Q15 0.85f; */
119 : Word16 beta_fx; /*Q14 1.05f; */
120 : Word32 L_band_energy[BANDS_MAX], L_band_energy_tmp[BANDS_MAX];
121 : UWord16 lo;
122 : Word16 Q_band_energy;
123 : #ifdef BASOP_NOGLOB
124 : Flag Overflow;
125 : #endif /* BASOP_NOGLOB */
126 :
127 102 : HQ_DEC_HANDLE hHQ_core = st->hHQ_core;
128 :
129 102 : set_s( last_bitalloc_max_band, 0, 2 );
130 102 : set_f( y2, 0.0f, L_FRAME48k );
131 102 : set_l( inp_vector, 0, inner_frame );
132 102 : flag_spt = 0;
133 102 : set_s( prev_SWB_peak_pos_tmp, 0, SPT_SHORTEN_SBNUM );
134 102 : bwe_br = st->core_brate;
135 :
136 :
137 102 : if ( st->bwidth == SWB && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
138 : {
139 102 : hqswb_clas = get_next_indice( st, 2 );
140 102 : num_bits -= 2;
141 :
142 102 : *is_transient = 0;
143 102 : if ( hqswb_clas == HQ_TRANSIENT )
144 : {
145 9 : *is_transient = 1;
146 : }
147 : }
148 : else
149 : {
150 : /* decode transient flag */
151 0 : *is_transient = get_next_indice( st, 1 );
152 0 : num_bits--;
153 : }
154 :
155 :
156 : /* Configure decoder for different bandwidths, bitrates, etc. */
157 102 : hq2_core_configure( inner_frame, num_bits, *is_transient, &bands, &length, band_width, band_start, band_end, &L_qint, &eref_fx, &bit_alloc_weight_fx, &gqlevs, &Ngq, &p2a_bands, &p2a_th, &pd_thresh, &ld_slope, &ni_coef, &ni_pd_th, bwe_br );
158 :
159 102 : highlength = band_end[bands - 1];
160 102 : har_bands = bands;
161 :
162 102 : if ( st->bwidth == SWB && *is_transient == 0 && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
163 : {
164 : /* reserve bits for HQ_NORMAL and HQ_HARMONIC */
165 93 : if ( hqswb_clas == HQ_NORMAL || hqswb_clas == HQ_HARMONIC )
166 : {
167 93 : num_bits -= get_usebit_npswb( hqswb_clas );
168 : }
169 :
170 93 : if ( hqswb_clas == HQ_NORMAL )
171 : {
172 93 : flag_spt = 1;
173 : }
174 : }
175 :
176 102 : if ( ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) && st->bwidth == SWB )
177 : {
178 102 : if ( hHQ_core->prev_hqswb_clas != HQ_NORMAL )
179 : {
180 9 : j = 0;
181 45 : for ( k = bands - SPT_SHORTEN_SBNUM; k < bands; k++ )
182 : {
183 36 : hHQ_core->prev_SWB_peak_pos[j] = 0;
184 36 : j++;
185 : }
186 : }
187 : }
188 :
189 : /* Spectral energy calculation/quantization */
190 102 : ebits = band_energy_dequant( st, band_energy, bands, L_qint, eref_fx, *is_transient );
191 :
192 : /* simple check: band_energy is too large, Abnormal Situation of bit errors */
193 2436 : for ( k = 0; k < bands; k++ )
194 : {
195 : /* Max: 45.0(737279,Q14) at 32kHz, highest band, Min: -6.600037(-108135,Q14) at 8kHz(NB),8kbps, is_transient (-6.7f is safty-threshold) */
196 2334 : if ( band_energy[k] > 45.0f || band_energy[k] < -6.7f )
197 : {
198 0 : st->BER_detect = 1;
199 0 : set_f( yout, 0, inner_frame );
200 0 : return;
201 : }
202 : }
203 :
204 2436 : for ( i = 0; i < bands; i++ )
205 : {
206 2334 : L_band_energy[i] = (Word32) ( band_energy[i] * pow( 2.0f, SWB_BWE_LR_Qbe ) );
207 : }
208 :
209 : /* First pass bit budget for TCQ of spectral band information */
210 102 : gqbits = (int16_t) log2_f( (float) gqlevs );
211 102 : bit_budget = num_bits - (int16_t) ceil( ebits ) - Ngq * gqbits;
212 :
213 102 : pbits = 0;
214 102 : if ( st->bwidth == SWB && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
215 : {
216 102 : if ( hqswb_clas == HQ_HARMONIC )
217 : {
218 0 : set_s( p2a_flags, 1, har_bands );
219 : }
220 : else
221 : {
222 : /* High band tonality detector based on per band peak-to-average ratio */
223 102 : pbits = p2a_threshold_dequant( st, p2a_flags, bands, p2a_bands );
224 102 : bit_budget -= pbits;
225 :
226 102 : if ( hqswb_clas == HQ_NORMAL )
227 : {
228 93 : return_bits_normal2( &bit_budget, p2a_flags, bands, bits_lagIndices_modeNormal );
229 : }
230 : }
231 : }
232 : else
233 : {
234 : /* High band tonality detector based on per band peak-to-average ratio */
235 0 : pbits = p2a_threshold_dequant( st, p2a_flags, bands, p2a_bands );
236 0 : bit_budget -= pbits;
237 : }
238 :
239 102 : if ( flag_spt == 1 )
240 : {
241 : /* initialize the desired parameters for SPT */
242 93 : spt_shorten_domain_band_save( bands, band_start, band_end, band_width, org_band_start, org_band_end, org_band_width );
243 93 : spt_shorten_domain_pre( band_start, band_end, hHQ_core->prev_SWB_peak_pos, bands, bwe_br, new_band_start, new_band_end, new_band_width );
244 93 : spt_shorten_domain_set_dec( st, p2a_flags, new_band_start, new_band_end, new_band_width, bands, band_start, band_end, band_width, &bit_budget );
245 : }
246 :
247 : /* safety check in case of bit errors */
248 102 : if ( bit_budget < 2 )
249 : {
250 0 : st->BER_detect = 1;
251 0 : set_f( yout, 0, inner_frame );
252 0 : return;
253 : }
254 :
255 : #define WMC_TOOL_SKIP
256 : /* Estimate number of bits per sub-band */
257 102 : Q_band_energy = SWB_BWE_LR_Qbe;
258 2436 : FOR( i = 0; i < bands; i++ )
259 : {
260 2334 : L_tmp = L_shl( L_band_energy[i], sub( 16, Q_band_energy ) ); /*Q16 */
261 :
262 2334 : frac1 = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */
263 2334 : L_tmp = Pow2( 30, frac1 );
264 2334 : exp = sub( exp, 30 );
265 2334 : Ep_fx[i] = L_shl( L_tmp, sub( exp, 6 ) ); /* Q -6 */
266 2334 : Ep[i] = (float) ( Ep_fx[i] / pow( 2.0, -6 ) );
267 : }
268 :
269 2436 : FOR( i = 0; i < bands; i++ )
270 : {
271 2334 : L_tmp2 = Ep_fx[i];
272 2334 : L_tmp = L_max( 1, L_tmp2 );
273 2334 : exp = norm_l( L_tmp );
274 2334 : tmp = extract_h( L_shl( L_tmp, exp ) );
275 :
276 2334 : L_tmp3 = (Word32) band_width[i];
277 2334 : exp2 = norm_l( L_tmp3 );
278 2334 : tmp2 = extract_h( L_shl( L_tmp3, exp2 ) );
279 :
280 2334 : exp2 = sub( exp, exp2 ); /* Denormalize and substract */
281 :
282 2334 : tmp3 = sub( tmp2, tmp );
283 2334 : IF( tmp3 > 0 )
284 : {
285 894 : tmp2 = shr( tmp2, 1 );
286 : }
287 2334 : IF( tmp3 > 0 )
288 : {
289 894 : exp2 = add( exp2, 1 );
290 : }
291 2334 : tmp = div_s( tmp2, tmp );
292 2334 : L_tmp = L_deposit_h( tmp );
293 2334 : L_tmp = Isqrt_lc1( L_tmp, &exp2 );
294 2334 : move32(); /*Q(31-exp2) */
295 2334 : Ep_tmp_fx[i] = L_shr( L_tmp, sub( 15, exp2 ) ); /*Q13 */
296 2334 : Ep_tmp[i] = (float) ( Ep_tmp_fx[i] / pow( 2.0, 13 ) );
297 : }
298 : #undef WMC_TOOL_SKIP
299 :
300 102 : if ( *is_transient == 0 && inner_frame == L_FRAME8k && st->core_brate <= ACELP_13k20 )
301 : {
302 : /* decode the last p2a_bands-1 subbands bit-allocation index of the previous frame */
303 0 : j = 0;
304 0 : for ( i = 0; i < 2; i++ )
305 : {
306 0 : last_bitalloc_max_band[i] = get_next_indice( st, 1 );
307 : }
308 :
309 : #define WMC_TOOL_SKIP
310 0 : lowband = 6;
311 0 : move16();
312 0 : trans_bit = 2;
313 0 : move16();
314 0 : bit_budget = sub( bit_budget, trans_bit );
315 0 : gama_fx = 27852; /*Q15 0.85f;*/
316 0 : beta_fx = 17203;
317 0 : move16(); /*Q14 1.05f; */
318 0 : set_s( &p2a_flags_tmp[bands - trans_bit], 0, 2 );
319 :
320 0 : IF( st->core_brate == ACELP_13k20 )
321 : {
322 0 : beta_fx = 13107;
323 0 : move16(); /*14 1.25f; */
324 0 : gama_fx = 31130;
325 0 : move16(); /*0.95f; */
326 0 : mvs2s( &p2a_flags[sub( bands, trans_bit )], &p2a_flags_tmp[sub( bands, trans_bit )], trans_bit );
327 : }
328 :
329 : /* calculate the the low band/high band energy and the variance/avrage of the envelopes */
330 0 : Ep_vari_fx = 0;
331 0 : move32();
332 0 : Ep_avrg_fx = 0;
333 0 : move32();
334 0 : Ep_avrgL_fx = 0;
335 0 : move32();
336 0 : Ep_peak_fx = 0;
337 0 : move32();
338 0 : FOR( i = 0; i < bands; i++ )
339 : {
340 0 : IF( sub( i, lowband ) >= 0 )
341 : {
342 0 : Ep_vari_fx = L_add( Ep_vari_fx, L_abs( L_sub( Ep_tmp_fx[i], Ep_tmp_fx[sub( i, 1 )] ) ) ); /*Q15 */
343 0 : Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
344 : }
345 : ELSE
346 : {
347 0 : Ep_avrgL_fx = L_add( Ep_avrgL_fx, Ep_tmp_fx[i] ); /*Q15 */
348 0 : IF( L_sub( Ep_tmp_fx[i], Ep_peak_fx ) > 0 )
349 : {
350 0 : Ep_peak_fx = Ep_tmp_fx[i];
351 0 : move32(); /*Q15 */
352 : }
353 : }
354 : }
355 : /* modify the last p2a_bands subbands band_energies */
356 0 : k = bands;
357 0 : mvl2l( L_band_energy, L_band_energy_tmp, k ); /*Q_band_energy */
358 0 : Mpy_32_16_ss( Ep_peak_fx, 24576, &L_tmp, &lo );
359 0 : Mpy_32_16_ss( Ep_peak_fx, shl( sub( bands, lowband ), 9 ), &L_tmp2, &lo );
360 0 : Mpy_32_16_ss( Ep_avrg_fx, 1126, &L_tmp3, &lo );
361 :
362 0 : IF( ( ( L_sub( L_tmp, L_shr( Ep_avrgL_fx, 1 ) ) < 0 && st->core_brate == ACELP_13k20 ) || st->core_brate < ACELP_13k20 ) &&
363 : L_sub( L_tmp2, L_tmp3 ) < 0 && L_sub( L_tmp2, L_shr( Ep_avrg_fx, 7 ) ) > 0 )
364 : {
365 0 : FOR( i = lowband; i < bands; i++ )
366 : {
367 0 : Mpy_32_16_ss( Ep_avrg_fx, 24576, &L_tmp, &lo );
368 0 : IF( L_sub( L_shr( Ep_tmp_fx[i], 1 ), L_tmp ) < 0 )
369 : {
370 0 : Mpy_32_16_ss( Ep_peak_fx, sub( bands, lowband ), &L_tmp, &lo );
371 0 : tmp = extract_h( L_shl( L_tmp, 14 ) ); /*Q-4 */
372 0 : IF( tmp != 0 )
373 : {
374 0 : exp = norm_s( tmp );
375 0 : tmp = shl( tmp, exp ); /*Q(exp) */
376 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
377 0 : exp = sub( 29, exp );
378 : }
379 : ELSE
380 : {
381 0 : tmp = 0x7fff;
382 0 : move16();
383 0 : exp = 0;
384 0 : move16();
385 : }
386 0 : Mpy_32_16_ss( Ep_avrg_fx, tmp, &L_tmp, &lo );
387 0 : L_tmp = L_shl( L_tmp, sub( 13, exp ) ); /*Q(13+exp-15 +13-exp +4 = 15) */
388 0 : L_tmp2 = L_add( L_tmp, 13107 ); /*15 */
389 0 : tmp2 = extract_l( L_min( L_max( L_tmp2, 16384 ), gama_fx ) ); /*15 = 15 */
390 0 : Mpy_32_16_ss( L_band_energy_tmp[i], tmp2, &L_band_energy_tmp[i], &lo );
391 : }
392 : }
393 : }
394 : ELSE
395 : {
396 0 : FOR( i = sub( bands, trans_bit ); i < bands; i++ )
397 : {
398 0 : alpha_fx = 16384;
399 0 : move16(); /*Q14 */
400 0 : IF( sub( p2a_flags_tmp[i], 1 ) == 0 )
401 : {
402 0 : Mpy_32_16_ss( Ep_tmp_fx[i], sub( bands, lowband ), &L_tmp, &lo );
403 0 : tmp = extract_h( L_shl( L_tmp, 14 ) ); /*Q-4 */
404 0 : IF( tmp != 0 )
405 : {
406 0 : exp = norm_s( tmp );
407 0 : tmp = shl( tmp, exp ); /*Q(exp) */
408 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
409 0 : exp = sub( 29, exp );
410 : }
411 : ELSE
412 : {
413 0 : tmp = 0x7fff;
414 0 : move16();
415 0 : exp = 0;
416 0 : move16();
417 : }
418 0 : Mpy_32_16_ss( Ep_vari_fx, 3277, &L_tmp, &lo );
419 0 : Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
420 0 : L_tmp = L_shl( L_tmp, sub( 12, exp ) ); /*Q(13+exp-15 +12-exp +4 = 14) */
421 :
422 0 : tmp2 = extract_h( Ep_avrg_fx ); /*Q13-16=-3 */
423 0 : IF( tmp2 != 0 )
424 : {
425 0 : exp = norm_s( tmp2 );
426 0 : tmp2 = shl( tmp2, exp ); /*Q(exp) */
427 0 : tmp2 = div_s( 16384, tmp2 ); /*Q(15+14-exp=29-exp) */
428 0 : exp = sub( 29, exp );
429 : }
430 : ELSE
431 : {
432 : /*when the divisor is zero, happens rarely*/
433 0 : tmp2 = 0x7fff;
434 0 : move16();
435 0 : exp = 0;
436 0 : move16();
437 : }
438 0 : Mpy_32_16_ss( Ep_vari_fx, 6554, &L_tmp2, &lo );
439 0 : Mpy_32_16_ss( L_tmp2, tmp2, &L_tmp2, &lo );
440 0 : L_tmp2 = L_shl( L_tmp2, sub( 13, exp ) ); /*Q(13+exp-15 +13-exp +3 = 14) */
441 0 : L_tmp = L_min( L_tmp, L_tmp2 ); /*14 */
442 0 : tmp = extract_l( L_min( L_tmp, 13107 ) ); /*14 */
443 0 : alpha_fx = add( 16384, tmp );
444 : }
445 0 : IF( sub( last_bitalloc_max_band[j++], 1 ) == 0 )
446 : {
447 0 : Mpy_32_16_ss( Ep_tmp_fx[i], sub( bands, lowband ), &L_tmp, &lo );
448 0 : tmp = extract_h( L_shl( L_tmp, 14 ) ); /*Q-2 */
449 0 : IF( tmp != 0 )
450 : {
451 0 : exp = norm_s( tmp );
452 0 : tmp = shl( tmp, exp ); /*Q(exp) */
453 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
454 0 : exp = sub( 29, exp );
455 : }
456 : ELSE
457 : {
458 0 : tmp = 0x7fff;
459 0 : move16();
460 0 : exp = 0;
461 0 : move16();
462 : }
463 0 : Mpy_32_16_ss( Ep_avrg_fx, tmp, &L_tmp, &lo );
464 : #ifndef BASOP_NOGLOB
465 : L_tmp = L_shl( L_tmp, sub( 14, exp ) ); /*Q(13+exp-15 +14-exp+2 = 14) */
466 : L_tmp = L_max( L_tmp, 16384 ); /*14 */
467 : tmp = extract_l( L_min( L_tmp, beta_fx ) ); /*14 */
468 : alpha_fx = shl( mult( alpha_fx, tmp ), 1 ); /*14+14-15 +1=14 */
469 : #else /* BASOP_NOGLOB */
470 0 : L_tmp = L_shl_o( L_tmp, sub( 14, exp ), &Overflow ); /*Q(13+exp-15 +14-exp+2 = 14) */
471 0 : L_tmp = L_max( L_tmp, 16384 ); /*14 */
472 0 : tmp = extract_l( L_min( L_tmp, beta_fx ) ); /*14 */
473 0 : alpha_fx = shl( mult( alpha_fx, tmp ), 1 ); /*14+14-15 +1=14 */
474 : #endif /* BASOP_NOGLOB */
475 : }
476 : ELSE
477 : {
478 0 : tmp2 = extract_h( Ep_avrg_fx ); /*13 -16 =-3 */
479 0 : IF( tmp2 != 0 )
480 : {
481 0 : exp = norm_s( tmp2 );
482 0 : tmp2 = shl( tmp2, exp ); /*Q(exp) */
483 0 : tmp2 = div_s( 16384, tmp2 ); /*Q(15+14-exp=29-exp) */
484 0 : exp = sub( 29, exp );
485 : }
486 : ELSE
487 : {
488 : /*when the divisor is zero, happens rarely*/
489 0 : tmp2 = 0x7fff;
490 0 : move16();
491 0 : exp = 0;
492 0 : move16();
493 : }
494 0 : Mpy_32_16_ss( Ep_tmp_fx[i], tmp2, &L_tmp, &lo );
495 0 : L_tmp = L_shl( L_tmp, sub( 19, exp ) ); /*Q(13+exp-15 +19-exp +3 = 20) */
496 0 : Mpy_32_16_ss( L_tmp, shl( sub( bands, lowband ), 9 ), &L_tmp, &lo );
497 0 : L_tmp = L_max( L_tmp, 13926 ); /*14 */
498 0 : tmp2 = extract_l( L_min( L_tmp, 16384 ) ); /*14 */
499 0 : alpha_fx = shl( mult( alpha_fx, tmp2 ), 1 ); /*14+14-15+1 =14 */
500 : }
501 0 : Mpy_32_16_ss( L_band_energy_tmp[i], alpha_fx, &L_tmp, &lo );
502 0 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q(Q_band_energy+14-15 +1= Q_band_energy) */
503 : }
504 : }
505 0 : lowband = 3;
506 0 : move16();
507 0 : Ep_avrg_fx = 0;
508 0 : move32();
509 0 : Ep_avrgL_fx = 0;
510 0 : move32();
511 0 : Ep_peak_fx = 0;
512 0 : move32();
513 0 : FOR( i = 0; i < bands; i++ )
514 : {
515 0 : IF( sub( i, lowband ) >= 0 )
516 : {
517 0 : Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
518 : }
519 : ELSE
520 : {
521 0 : Ep_avrgL_fx = L_add( Ep_avrgL_fx, L_shr( Ep_tmp_fx[i], 1 ) ); /*Q12 */
522 0 : IF( L_sub( Ep_tmp_fx[i], Ep_peak_fx ) > 0 )
523 : {
524 0 : Ep_peak_fx = Ep_tmp_fx[i];
525 0 : move32(); /*Q13 */
526 : }
527 : }
528 : }
529 0 : Mpy_32_16_ss( Ep_peak_fx, 28262, &L_tmp, &lo );
530 0 : Mpy_32_16_ss( Ep_avrgL_fx, 24576, &L_tmp2, &lo );
531 0 : IF( L_sub( L_shr( Ep_avrg_fx, 2 ), L_tmp2 ) > 0 && L_sub( L_shr( Ep_avrg_fx, 4 ), L_tmp2 ) < 0 && L_sub( L_tmp, Ep_avrgL_fx ) > 0 )
532 : {
533 0 : adjustFlag = 1;
534 0 : move16();
535 0 : FOR( i = 0; i < lowband; i++ )
536 : {
537 0 : tmp = extract_h( Ep_avrgL_fx ); /*Q-4 */
538 0 : IF( tmp != 0 )
539 : {
540 0 : exp = norm_s( tmp );
541 0 : tmp = shl( tmp, exp ); /*Q(exp) */
542 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
543 0 : exp = sub( 29, exp );
544 : }
545 : ELSE
546 : {
547 0 : tmp = 0x7fff;
548 0 : move16();
549 0 : exp = 0;
550 0 : move16();
551 : }
552 0 : Mpy_32_16_ss( Ep_peak_fx, tmp, &L_tmp, &lo );
553 0 : Mpy_32_16_ss( L_tmp, lowband, &L_tmp, &lo );
554 0 : Mpy_32_16_ss( L_tmp, 18842, &L_tmp, &lo );
555 : #ifndef BASOP_NOGLOB
556 : L_tmp = L_shl( L_tmp, sub( 27, exp ) ); /*Q14 0.5 */
557 : tmp2 = extract_l( L_min( L_tmp, 19661 ) ); /*14 */
558 : #else /* BASOP_NOGLOB */
559 0 : L_tmp = L_shl_o( L_tmp, sub( 27, exp ), &Overflow ); /*Q14 0.5 */
560 0 : tmp2 = extract_l( L_min( L_tmp, 19661 ) ); /*14 */
561 : #endif /* BASOP_NOGLOB */
562 0 : Mpy_32_16_ss( L_band_energy_tmp[i], tmp2, &L_tmp, &lo );
563 : #ifndef BASOP_NOGLOB
564 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q_band_energy */
565 : #else /* BASOP_NOGLOB */
566 0 : L_band_energy_tmp[i] = L_shl_o( L_tmp, 1, &Overflow ); /*Q_band_energy */
567 : #endif /* BASOP_NOGLOB */
568 : }
569 : }
570 : #undef WMC_TOOL_SKIP
571 :
572 0 : for ( i = 0; i < bands; i++ )
573 : {
574 0 : band_energy_tmp[i] = (float) ( L_band_energy_tmp[i] / pow( 2.0f, SWB_BWE_LR_Qbe ) );
575 : }
576 :
577 0 : hq2_bit_alloc( band_energy_tmp, bands, Rk_fx, &bit_budget, p2a_flags, bit_alloc_weight_fx, band_width, num_bits, hqswb_clas, st->bwidth, *is_transient );
578 : }
579 102 : else if ( *is_transient == 0 && inner_frame == L_FRAME16k )
580 : {
581 : #define WMC_TOOL_SKIP
582 0 : bit_budget = sub( bit_budget, 2 ); /* bits in high bands to indicate the last 2 subbands is allocated bits or not */
583 : /* decode the last p2a_bands-1 subbands bit-allocation index of the previous frame */
584 0 : for ( i = 0; i < 2; i++ )
585 : {
586 0 : last_bitalloc_max_band[i] = get_next_indice( st, 1 );
587 : }
588 0 : FOR( i = 0; i < bands; i++ )
589 : {
590 : #ifndef BASOP_NOGLOB
591 : Ep_tmp_fx[i] = L_shl( Ep_tmp_fx[i], 2 );
592 : #else /* BASOP_NOGLOB */
593 0 : Ep_tmp_fx[i] = L_shl_o( Ep_tmp_fx[i], 2, &Overflow );
594 : #endif /* BASOP_NOGLOB */
595 : }
596 0 : IF( st->core_brate == ACELP_13k20 )
597 : {
598 0 : lowband = 8;
599 0 : move16();
600 0 : highband = 15;
601 0 : move16();
602 0 : bw_low = sub( band_start[highband], band_start[lowband] );
603 0 : bw_high = sub( add( band_end[sub( bands, 1 )], 1 ), band_start[highband] );
604 : }
605 : ELSE
606 : {
607 0 : lowband = 8;
608 0 : move16();
609 0 : highband = 16;
610 0 : move16();
611 0 : bw_low = sub( band_start[highband], band_start[lowband] );
612 0 : bw_high = sub( add( band_end[sub( bands, 1 )], 1 ), band_start[highband] );
613 : }
614 : /* calculate the the low band/high band energy and the variance/avrage of the envelopes */
615 0 : enerL_fx = 0;
616 0 : move32();
617 0 : enerH_fx = 0;
618 0 : move32();
619 0 : Ep_vari_fx = 0;
620 0 : move32();
621 0 : Ep_avrg_fx = 0;
622 0 : move32();
623 0 : FOR( i = 0; i < bands; i++ )
624 : {
625 0 : IF( sub( i, lowband ) >= 0 && add( sub( i, bands ), p2a_bands ) < 0 )
626 : {
627 : #ifndef BASOP_NOGLOB
628 : Ep_vari_fx = L_add( Ep_vari_fx, L_abs( L_sub( Ep_tmp_fx[i], Ep_tmp_fx[sub( i, 1 )] ) ) ); /*Q15 */
629 : Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
630 : #else /* BASOP_NOGLOB */
631 0 : Ep_vari_fx = L_add_o( Ep_vari_fx, L_abs( L_sub( Ep_tmp_fx[i], Ep_tmp_fx[sub( i, 1 )] ) ), &Overflow ); /*Q15 */
632 0 : Ep_avrg_fx = L_add_o( Ep_avrg_fx, Ep_tmp_fx[i], &Overflow ); /*Q15 */
633 : #endif /* BASOP_NOGLOB */
634 : }
635 :
636 0 : IF( sub( i, highband ) >= 0 )
637 : {
638 : /* safety check in case of bit errors */
639 0 : if ( Ep_fx[i] > 536788991 /* max(Q30) */ )
640 : {
641 0 : st->BER_detect = 1;
642 0 : set_f( yout, 0, inner_frame );
643 0 : return;
644 : }
645 :
646 0 : enerH_fx = L_add( enerH_fx, L_shl( Ep_fx[i], 2 ) ); /*Q0 */
647 : }
648 0 : ELSE IF( sub( i, lowband ) >= 0 )
649 : {
650 : /* safety check in case of bit errors */
651 0 : if ( Ep_fx[i] > 536788991 /* max(Q30) */ )
652 : {
653 0 : st->BER_detect = 1;
654 0 : set_f( yout, 0, inner_frame );
655 0 : return;
656 : }
657 :
658 0 : enerL_fx = L_add( enerL_fx, L_shl( Ep_fx[i], 2 ) ); /*Q0 */
659 : }
660 : }
661 :
662 0 : enerL = (float) ( enerL_fx / pow( 2.0, -4 ) );
663 0 : enerH = (float) ( enerH_fx / pow( 2.0, -4 ) );
664 : /* modify the last p2a_bands subbands band_energies */
665 0 : k = bands;
666 0 : mvl2l( L_band_energy, L_band_energy_tmp, k ); /*Q_band_energy */
667 :
668 0 : L_tmp = L_max( enerH_fx, enerL_fx );
669 0 : tmp = s_max( bw_low, bw_high );
670 0 : i = norm_l( L_tmp );
671 0 : j = norm_s( tmp );
672 0 : Mpy_32_16_ss( L_shl( enerH_fx, i ), shl( bw_low, j ), &L_tmp, &lo );
673 0 : Mpy_32_16_ss( L_shl( enerL_fx, i ), shl( bw_high, j ), &L_tmp2, &lo );
674 0 : L_tmp2 = L_sub( L_tmp, L_tmp2 );
675 :
676 0 : FOR( i = sub( bands, p2a_bands ); i < bands; i++ )
677 : {
678 0 : IF( sub( p2a_flags[i], 1 ) == 0 || L_tmp2 > 0 )
679 : {
680 0 : tmp = sub( bands, p2a_bands );
681 0 : tmp = sub( tmp, lowband ); /*Q0 */
682 :
683 : #ifndef BASOP_NOGLOB
684 : tmp1 = extract_h( L_shl( Ep_avrg_fx, 1 ) ); /*Q0 */
685 : #else
686 0 : tmp1 = extract_h( L_shl_o( Ep_avrg_fx, 1, &Overflow ) ); /*Q0 */
687 : #endif
688 0 : IF( tmp1 != 0 )
689 : {
690 0 : exp = norm_s( tmp1 );
691 0 : tmp1 = shl( tmp1, exp ); /*Q(exp) */
692 0 : tmp1 = div_s( 16384, tmp1 ); /*Q(15+14-exp = 29-exp) */
693 0 : exp = sub( 29, exp );
694 : }
695 : ELSE
696 : {
697 : /*when the divisor is zero, happens rarely*/
698 0 : tmp1 = 0x7fff;
699 0 : move16();
700 0 : exp = 0;
701 0 : move16();
702 : }
703 0 : Mpy_32_16_ss( Ep_tmp_fx[i], tmp1, &L_tmp, &lo );
704 0 : Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
705 0 : Mpy_32_16_ss( L_tmp, 16384, &L_tmp, &lo );
706 0 : L_tmp = L_shl( L_tmp, sub( 32, exp ) ); /*Q15 */
707 0 : tmp = extract_l( L_min( L_tmp, 6554 ) ); /*Q15 */
708 0 : Mpy_32_16_ss( Ep_vari_fx, tmp1, &L_tmp, &lo );
709 0 : Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
710 0 : L_tmp = L_shl( L_tmp, sub( 15, exp ) ); /*Q15 */
711 0 : tmp = extract_l( L_shr( L_min( L_tmp, 13107 ), 1 ) ); /*Q14 */
712 0 : alpha_fx = add( tmp, 16384 ); /*Q14 */
713 : }
714 : ELSE
715 : {
716 0 : alpha_fx = 16384;
717 0 : move16(); /*Q14 */
718 : }
719 :
720 0 : IF( add( sub( i, bands ), p2a_bands ) > 0 )
721 : {
722 0 : tmp = sub( bands, p2a_bands );
723 0 : IF( sub( last_bitalloc_max_band[sub( i, add( tmp, 1 ) )], 1 ) == 0 )
724 : {
725 0 : tmp = sub( tmp, lowband );
726 0 : Mpy_32_16_ss( Ep_tmp_fx[i], tmp, &L_tmp, &lo );
727 : #ifndef BASOP_NOGLOB
728 : tmp = extract_h( L_shl( L_tmp, 16 ) ); /*Q0 */
729 : #else
730 0 : tmp = extract_h( L_shl_o( L_tmp, 16, &Overflow ) ); /*Q0 */
731 : #endif
732 0 : IF( tmp != 0 )
733 : {
734 0 : exp = norm_s( tmp );
735 0 : tmp = shl( tmp, exp ); /*Q(exp) */
736 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
737 0 : exp = sub( 29, exp );
738 : }
739 : ELSE
740 : {
741 0 : tmp = 0x7fff;
742 0 : move16();
743 0 : exp = 0;
744 0 : move16();
745 : }
746 0 : Mpy_32_16_ss( Ep_avrg_fx, tmp, &L_tmp, &lo );
747 0 : L_tmp = L_shl( L_tmp, sub( 14, exp ) ); /*Q14 */
748 0 : tmp = extract_l( L_min( L_max( L_tmp, 16384 ), 20480 ) ); /*Q14 */
749 0 : L_tmp = L_mult( alpha_fx, tmp ); /*Q(14+14+1=29) */
750 0 : alpha_fx = extract_l( L_shr( L_tmp, 15 ) ); /*Q14 */
751 : }
752 : ELSE
753 : {
754 0 : tmp = sub( tmp, lowband );
755 :
756 : #ifndef BASOP_NOGLOB
757 : tmp1 = extract_h( L_shl( Ep_avrg_fx, 1 ) ); /*Q0 */
758 : #else
759 0 : tmp1 = extract_h( L_shl_o( Ep_avrg_fx, 1, &Overflow ) ); /*Q0 */
760 : #endif
761 0 : IF( tmp1 != 0 )
762 : {
763 0 : exp = norm_s( tmp1 );
764 0 : tmp1 = shl( tmp1, exp ); /*Q(exp) */
765 0 : tmp1 = div_s( 16384, tmp1 ); /*Q(15+14-exp=29-exp) */
766 0 : exp = sub( 29, exp );
767 : }
768 : ELSE
769 : {
770 : /*when the divisor is zero, happens rarely*/
771 0 : tmp1 = 0x7fff;
772 0 : move16();
773 0 : exp = 0;
774 0 : move16();
775 : }
776 0 : Mpy_32_16_ss( Ep_tmp_fx[i], tmp1, &L_tmp, &lo );
777 0 : Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
778 0 : L_tmp = L_shl( L_tmp, sub( 29, exp ) ); /*Q14 */
779 0 : tmp = extract_l( L_min( L_max( L_tmp, 13926 ), 16384 ) ); /*Q14 */
780 0 : L_tmp = L_mult( alpha_fx, tmp ); /*Q(14+14+1=29) */
781 0 : alpha_fx = extract_l( L_shr( L_tmp, 15 ) ); /*Q14 */
782 : }
783 : }
784 0 : Mpy_32_16_ss( L_band_energy_tmp[i], alpha_fx, &L_tmp, &lo );
785 0 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q Q_band_energy */
786 : }
787 0 : lowband = 6;
788 0 : move16();
789 0 : Ep_avrg_fx = 0;
790 0 : move32();
791 0 : Ep_avrgL_fx = 0;
792 0 : move32();
793 0 : Ep_peak_fx = 0;
794 0 : move32();
795 0 : FOR( i = 0; i < bands; i++ )
796 : {
797 0 : IF( sub( i, lowband ) >= 0 )
798 : {
799 : #ifndef BASOP_NOGLOB
800 : Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
801 : #else /* BASOP_NOGLOB */
802 0 : Ep_avrg_fx = L_add_o( Ep_avrg_fx, Ep_tmp_fx[i], &Overflow ); /*Q15 */
803 : #endif /* BASOP_NOGLOB */
804 : }
805 : ELSE
806 : {
807 : #ifndef BASOP_NOGLOB
808 : Ep_avrgL_fx = L_add( Ep_avrgL_fx, Ep_tmp_fx[i] ); /*Q15 */
809 : #else /* BASOP_NOGLOB */
810 0 : Ep_avrgL_fx = L_add_o( Ep_avrgL_fx, Ep_tmp_fx[i], &Overflow ); /*Q15 */
811 : #endif /* BASOP_NOGLOB */
812 0 : IF( L_sub( Ep_tmp_fx[i], Ep_peak_fx ) > 0 )
813 : {
814 0 : Ep_peak_fx = Ep_tmp_fx[i];
815 0 : move32(); /*Q15 */
816 : }
817 : }
818 : }
819 :
820 0 : Mpy_32_16_ss( Ep_peak_fx, 24576, &L_tmp, &lo );
821 0 : Mpy_32_16_ss( Ep_peak_fx, 19661, &L_tmp2, &lo );
822 0 : Mpy_32_16_ss( Ep_avrgL_fx, 24576, &L_tmp3, &lo );
823 :
824 0 : IF( ( L_sub( L_shr( Ep_avrgL_fx, 1 ), Ep_avrg_fx ) > 0 && L_sub( L_tmp, L_shr( Ep_avrgL_fx, 2 ) ) > 0 && L_sub( L_shr( Ep_avrgL_fx, 1 ), L_tmp2 ) < 0 ) ||
825 : ( L_sub( L_shr( Ep_avrg_fx, 1 ), Ep_avrgL_fx ) > 0 && L_sub( L_shr( Ep_avrg_fx, 3 ), L_tmp3 ) < 0 && L_sub( L_tmp, L_shr( Ep_avrgL_fx, 2 ) ) > 0 ) )
826 : {
827 0 : adjustFlag = 1;
828 0 : move16();
829 0 : FOR( i = 0; i < lowband; i++ )
830 : {
831 : #ifndef BASOP_NOGLOB
832 : tmp = extract_h( L_shl( Ep_avrgL_fx, 1 ) ); /*Q0 */
833 : #else
834 0 : tmp = extract_h( L_shl_o( Ep_avrgL_fx, 1, &Overflow ) ); /*Q0 */
835 : #endif
836 0 : IF( tmp != 0 )
837 : {
838 0 : exp = norm_s( tmp );
839 0 : tmp = shl( tmp, exp ); /*Q(exp) */
840 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
841 0 : exp = sub( 29, exp );
842 : }
843 : ELSE
844 : {
845 0 : tmp = 0x7fff;
846 0 : move16();
847 0 : exp = 0;
848 0 : move16();
849 : }
850 0 : Mpy_32_16_ss( Ep_peak_fx, tmp, &L_tmp, &lo );
851 0 : Mpy_32_16_ss( L_tmp, lowband, &L_tmp, &lo );
852 0 : L_tmp = L_shl( L_tmp, sub( 28, exp ) ); /*Q14 0.5 */
853 0 : tmp = extract_l( L_min( L_tmp, 19661 ) ); /* Q14 */
854 0 : Mpy_32_16_ss( L_band_energy_tmp[i], tmp, &L_tmp, &lo );
855 0 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q_band_energy */
856 : }
857 : }
858 : #undef WMC_TOOL_SKIP
859 :
860 0 : for ( i = 0; i < bands; i++ )
861 : {
862 0 : band_energy_tmp[i] = (float) ( L_band_energy_tmp[i] / pow( 2.0f, SWB_BWE_LR_Qbe ) );
863 : }
864 :
865 0 : hq2_bit_alloc( band_energy_tmp, bands, Rk_fx, &bit_budget, p2a_flags, bit_alloc_weight_fx, band_width, num_bits, hqswb_clas, st->bwidth, *is_transient );
866 : }
867 102 : else if ( st->bwidth == SWB && hqswb_clas == HQ_HARMONIC && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
868 : {
869 0 : hq2_bit_alloc_har( band_energy, bit_budget, bands, Rk_fx, p2a_bands, bwe_br, p2a_flags, band_width );
870 : }
871 : else
872 : {
873 102 : hq2_bit_alloc( band_energy, bands, Rk_fx, &bit_budget, p2a_flags, bit_alloc_weight_fx, band_width, num_bits, hqswb_clas, st->bwidth, *is_transient );
874 : }
875 :
876 102 : if ( bit_budget < 0 )
877 : {
878 0 : st->BER_detect = 1;
879 0 : bit_budget = 0;
880 : }
881 :
882 102 : tcq_core_LR_dec( st, inp_vector, bit_budget, bands, band_start, band_width, Rk_fx, npulses, k_sort, p2a_flags, p2a_bands, last_bitalloc_max_band, inner_frame, adjustFlag, is_transient );
883 :
884 : /* Prepare floating Rk for next modules */
885 2436 : for ( k = 0; k < bands; k++ )
886 : {
887 2334 : Rk[k] = WORD322FL_SCALE( Rk_fx[k], SWB_BWE_LR_QRk - 1 );
888 : }
889 :
890 : /* Denormalize the coded MDCT spectrum */
891 102 : mdct_spectrum_denorm( inp_vector, y2, band_start, band_end, band_width, band_energy, npulses, bands, ld_slope, pd_thresh );
892 :
893 : /* Apply fine gain to denormalized coded spectrum */
894 102 : mdct_spectrum_fine_gain_dec( st, y2, band_start, band_end, k_sort, bands, L_qint, Ngq, gqlevs, gqbits );
895 :
896 : /*restore the band information */
897 102 : if ( flag_spt == 1 )
898 : {
899 93 : spt_shorten_domain_band_restore( bands, band_start, band_end, band_width, org_band_start, org_band_end, org_band_width );
900 : }
901 :
902 102 : mvr2r( y2, y2_org, L_FRAME32k );
903 :
904 : /* Inject noise into components having relatively low pulse energy per band */
905 102 : ni_seed = npulses[0] + npulses[1] + npulses[2] + npulses[3];
906 :
907 58038 : for ( i = 0; i < band_end[bands - 1] + 1; i++ )
908 57936 : y2_ni[i] = y2[i];
909 102 : hq2_noise_inject( y2_ni, band_start, band_end, band_width, Ep, Rk, npulses, ni_seed, bands, 0, bw_low, bw_high, enerL, enerH, hHQ_core->last_ni_gain, hHQ_core->last_env, &hHQ_core->last_max_pos_pulse, p2a_flags, p2a_bands, hqswb_clas, st->bwidth, bwe_br );
910 :
911 102 : if ( st->bwidth == SWB && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
912 : {
913 102 : if ( hqswb_clas == HQ_NORMAL || hqswb_clas == HQ_HARMONIC )
914 : {
915 93 : preset_hq2_swb( hqswb_clas, band_end, &har_bands, p2a_bands, length, bands, &lowlength, &highlength, m );
916 :
917 : /*Gap filling for the core coder*/
918 93 : swb_bwe_dec_lr( st, y2, m, bwe_br, bands, band_start, band_end, band_energy, p2a_flags, hqswb_clas, lowlength, highlength, har_bands, &hHQ_core->prev_frm_hfe2, &hHQ_core->prev_stab_hfe2, band_width, y2_ni, &ni_seed );
919 :
920 93 : post_hq2_swb( m, lowlength, highlength, hqswb_clas, har_bands, bands, p2a_flags, band_start, band_end, y2, npulses );
921 :
922 93 : if ( hqswb_clas == HQ_NORMAL )
923 : {
924 93 : spt_swb_peakpos_tmp_save( y2, bands, band_start, band_end, prev_SWB_peak_pos_tmp );
925 465 : for ( k = 0; k < SPT_SHORTEN_SBNUM; k++ )
926 : {
927 372 : if ( p2a_flags[bands - SPT_SHORTEN_SBNUM + k] == 0 || npulses[bands - SPT_SHORTEN_SBNUM + k] == 0 )
928 : {
929 366 : prev_SWB_peak_pos_tmp[k] = 0;
930 : }
931 : }
932 : }
933 :
934 93 : mvr2r( y2_ni, y2, lowlength );
935 : }
936 : else
937 : {
938 9 : mvr2r( y2_ni, y2, band_end[bands - 1] + 1 ); /* HQ_TRANSIENT */
939 : }
940 : }
941 : else
942 : {
943 0 : mvr2r( y2_ni, y2, band_end[bands - 1] + 1 ); /* NB, WB */
944 : }
945 :
946 :
947 : /* bandwidth switching */
948 102 : if ( !( st->last_inner_frame >= L_FRAME16k && st->bws_cnt > 0 ) )
949 : {
950 102 : k1 = *is_transient ? bands - 2 : bands - 6;
951 102 : st->prev_ener_shb = 0.0f;
952 :
953 678 : for ( i = k1; i < bands; i++ )
954 : {
955 576 : st->prev_ener_shb += Ep_tmp[i] / ( bands - k1 );
956 : }
957 : }
958 :
959 102 : if ( st->last_inner_frame >= L_FRAME32k && st->hBWE_FD != NULL )
960 : {
961 102 : set_f( st->hBWE_FD->prev_SWB_fenv, st->prev_ener_shb, SWB_FENV );
962 : }
963 :
964 102 : updat_prev_frm( y2, yout, bwe_br, length, inner_frame, bands, st->bwidth, *is_transient, hqswb_clas, &hHQ_core->prev_hqswb_clas, hHQ_core->prev_SWB_peak_pos, prev_SWB_peak_pos_tmp, &hHQ_core->prev_frm_hfe2, &hHQ_core->prev_stab_hfe2, st->bws_cnt );
965 :
966 102 : return;
967 : }
968 :
969 :
970 : /*------------------------------------------------------------------------------------
971 : * small_symbol_dec_tran()
972 : *
973 : * Huffman decoding of differential energies
974 : *--------------------------------------------------------------------------------------*/
975 :
976 : /*! r: bits */
977 6 : static int16_t small_symbol_dec_tran(
978 : Decoder_State *st, /* i/o: decoder state structure */
979 : int16_t *qbidx, /* o : output of dequantized differential energy */
980 : const int16_t bands, /* i : number of bands */
981 : const int16_t is_transient /* i : transient flag */
982 : )
983 : {
984 : int16_t i, bits;
985 : int16_t difidx[BANDS_MAX];
986 :
987 : /* Decoding differential energies*/
988 6 : bits = decode_envelope_indices( st, 0, bands, 0, difidx, LOW_RATE_HQ_CORE_TRAN, is_transient );
989 6 : bits += BITS_DE_FCOMP;
990 :
991 : /* counting 1 bit for band_energy_huff_coding_mode */
992 6 : bits += BITS_DE_HMODE;
993 :
994 : /* converting to original values */
995 198 : for ( i = 0; i < bands; i++ )
996 : {
997 192 : qbidx[i] = difidx[i] - LRMDCT_BE_OFFSET;
998 : }
999 :
1000 6 : return ( bits );
1001 : }
1002 :
1003 :
1004 : /*--------------------------------------------------------------------------
1005 : * small_symbol_dec()
1006 : *
1007 : * Huffman decoding of differential energies (MSB and LSB)
1008 : *--------------------------------------------------------------------------*/
1009 :
1010 : /*! r: bits */
1011 90 : static int16_t small_symbol_dec(
1012 : Decoder_State *st, /* i/o: decoder state structure */
1013 : int16_t *qbidx, /* o : output of dequantized differential energy */
1014 : const int16_t bands, /* i : number of bands */
1015 : const int16_t is_transient )
1016 : {
1017 : int16_t i, LSB, bits;
1018 : int16_t difidx[BANDS_MAX];
1019 :
1020 : /* Decoding MSB bits */
1021 90 : bits = decode_envelope_indices( st, 0, bands, 0, difidx, LOW_RATE_HQ_CORE, is_transient );
1022 90 : bits += BITS_DE_FCOMP;
1023 :
1024 : /* counting 1 bit for band_energy_huff_coding_mode */
1025 90 : bits += BITS_DE_HMODE;
1026 :
1027 : /* Decoding LSB bit packing */
1028 2070 : for ( i = 0; i < bands; ++i )
1029 : {
1030 1980 : LSB = get_next_indice( st, BITS_DE_LSB );
1031 1980 : difidx[i] = ( difidx[i] << 1 ) | LSB;
1032 : }
1033 :
1034 : /* counting bits for LSB */
1035 90 : bits += bands;
1036 :
1037 : /* converting to original values */
1038 1980 : for ( i = 1; i < bands; ++i )
1039 : {
1040 1890 : qbidx[i] = difidx[i] - DE_OFFSET1;
1041 : }
1042 :
1043 90 : qbidx[0] = difidx[0] - DE_OFFSET0;
1044 :
1045 90 : return ( bits );
1046 : }
1047 :
1048 : /*--------------------------------------------------------------------------
1049 : * decode_huff_8s()
1050 : *
1051 : *
1052 : *--------------------------------------------------------------------------*/
1053 :
1054 162 : static int16_t decode_huff_8s(
1055 : Decoder_State *st,
1056 : const int16_t *hufftab,
1057 : int16_t *rbits )
1058 : {
1059 : int16_t bit;
1060 :
1061 558 : while ( *hufftab > 0 )
1062 : {
1063 396 : *rbits += ( *hufftab & 0xf );
1064 396 : bit = get_next_indice( st, *hufftab & 0xf );
1065 396 : hufftab += ( *hufftab >> 4 ) + bit;
1066 : }
1067 :
1068 162 : return ( -*hufftab );
1069 : }
1070 :
1071 :
1072 : /*--------------------------------------------------------------------------
1073 : * large_symbol_dec()
1074 : *
1075 : *
1076 : *--------------------------------------------------------------------------*/
1077 :
1078 : /*! r: bits */
1079 6 : static int16_t large_symbol_dec(
1080 : Decoder_State *st, /* i/o: decoder state structure */
1081 : int16_t *qbidx, /* o : output of dequantized differential energy */
1082 : const int16_t bands /* i : number of bands */
1083 : )
1084 : {
1085 : int16_t i, bits;
1086 : int16_t LSB[BANDS_MAX];
1087 : int16_t basic_shift, cntbits, ns2mode;
1088 : int16_t pos_outlyer;
1089 : int16_t ns2mode0, ns2mode1;
1090 :
1091 6 : cntbits = BITS_DE_8SMODE;
1092 6 : ns2mode = get_next_indice( st, BITS_DE_8SMODE );
1093 :
1094 6 : if ( ns2mode == 0 )
1095 : {
1096 3 : ns2mode0 = get_next_indice( st, BITS_DE_8SMODE_N0 );
1097 3 : ns2mode1 = get_next_indice( st, BITS_DE_8SMODE_N1 );
1098 3 : cntbits += BITS_DE_8SMODE_N0 + BITS_DE_8SMODE_N1;
1099 :
1100 3 : if ( ns2mode0 == 0 )
1101 : {
1102 3 : if ( ns2mode1 == 1 )
1103 : {
1104 0 : pos_outlyer = get_next_indice( st, BITS_DE_8SPOS );
1105 0 : cntbits += BITS_DE_8SPOS;
1106 0 : qbidx[pos_outlyer] = get_next_indice( st, BITS_ABS_ENG ) - ABS_ENG_OFFSET;
1107 0 : cntbits += BITS_ABS_ENG;
1108 : }
1109 : else
1110 : {
1111 3 : pos_outlyer = -1;
1112 : }
1113 :
1114 3 : for ( i = 0; i < pos_outlyer; ++i )
1115 : {
1116 0 : bits = 0;
1117 0 : qbidx[i] = decode_huff_8s( st, hestable, &bits ) - 4;
1118 0 : cntbits += bits;
1119 : }
1120 :
1121 99 : for ( i = pos_outlyer + 1; i < bands; ++i )
1122 : {
1123 96 : bits = 0;
1124 96 : qbidx[i] = decode_huff_8s( st, hestable, &bits ) - 4;
1125 96 : cntbits += bits;
1126 : }
1127 : }
1128 : else
1129 : {
1130 0 : if ( ns2mode1 == 1 )
1131 : {
1132 0 : pos_outlyer = get_next_indice( st, BITS_DE_8SPOS );
1133 0 : cntbits += BITS_DE_8SPOS;
1134 0 : qbidx[0] = get_next_indice( st, BITS_ABS_ENG ) - ABS_ENG_OFFSET;
1135 0 : cntbits += BITS_ABS_ENG;
1136 0 : qbidx[pos_outlyer] = get_next_indice( st, BITS_ABS_ENG ) - ABS_ENG_OFFSET;
1137 0 : cntbits += BITS_ABS_ENG;
1138 : }
1139 : else
1140 : {
1141 0 : pos_outlyer = 0;
1142 0 : qbidx[0] = get_next_indice( st, BITS_ABS_ENG ) - ABS_ENG_OFFSET;
1143 0 : cntbits += BITS_ABS_ENG;
1144 : }
1145 :
1146 0 : for ( i = 1; i < pos_outlyer; ++i )
1147 : {
1148 0 : bits = 0;
1149 0 : qbidx[i] = decode_huff_8s( st, hestable, &bits ) - 4;
1150 0 : cntbits += bits;
1151 : }
1152 :
1153 0 : for ( i = pos_outlyer + 1; i < bands; ++i )
1154 : {
1155 0 : bits = 0;
1156 0 : qbidx[i] = ( decode_huff_8s( st, hestable, &bits ) - 4 );
1157 0 : cntbits += bits;
1158 : }
1159 : }
1160 : }
1161 : else
1162 : {
1163 3 : basic_shift = get_next_indice( st, BITS_MAX_DEPTH );
1164 3 : cntbits += BITS_MAX_DEPTH;
1165 :
1166 69 : for ( i = 0; i < bands; ++i )
1167 : {
1168 66 : bits = 0;
1169 66 : qbidx[i] = decode_huff_8s( st, hestable, &bits ) - 4;
1170 66 : qbidx[i] = qbidx[i] << basic_shift;
1171 66 : cntbits += bits;
1172 : }
1173 :
1174 69 : for ( i = 0; i < bands; ++i )
1175 : {
1176 66 : LSB[0] = get_next_indice( st, basic_shift );
1177 66 : qbidx[i] += LSB[0];
1178 66 : cntbits += basic_shift;
1179 : }
1180 : }
1181 :
1182 6 : return cntbits; /* xx bits for diff. energies + 1 bit for LC coding mode */
1183 : }
1184 :
1185 :
1186 : /*--------------------------------------------------------------------------*
1187 : * band_energy_dequant()
1188 : *
1189 : *
1190 : *--------------------------------------------------------------------------*/
1191 :
1192 102 : static float band_energy_dequant(
1193 : Decoder_State *st, /* i/o: decoder state structure */
1194 : float band_energy[],
1195 : const int16_t bands,
1196 : const Word32 L_qint, /* Q29 */
1197 : const Word16 eref_fx, /* Q10 */
1198 : const int16_t is_transient )
1199 : {
1200 : int16_t k;
1201 : int16_t deng_cmode;
1202 : int16_t bq0;
1203 : int16_t bq1[BANDS_MAX], bq2[BANDS_MAX];
1204 : float deng_bits;
1205 : Word32 L_band_energy[BANDS_MAX]; /* SWB_BWE_LR_Qbe */
1206 : Word16 exp_normd;
1207 : Word16 rev_qint_fx;
1208 : Word16 Qrev_qint;
1209 :
1210 : /* parsing energy difference coding mode */
1211 102 : deng_cmode = get_next_indice( st, BITS_DE_CMODE );
1212 :
1213 102 : if ( deng_cmode == 0 )
1214 : {
1215 6 : deng_bits = (float) large_symbol_dec( st, bq2, bands );
1216 :
1217 : /* counting 1 bit for deng coding mode */
1218 6 : deng_bits += BITS_DE_CMODE;
1219 : }
1220 : else
1221 : {
1222 96 : if ( is_transient )
1223 : {
1224 6 : deng_bits = (float) small_symbol_dec_tran( st, bq2, bands, is_transient );
1225 : }
1226 : else
1227 : {
1228 90 : deng_bits = (float) small_symbol_dec( st, bq2, bands, is_transient );
1229 : }
1230 :
1231 : /* counting 1 bit for deng coding mode */
1232 96 : deng_bits += BITS_DE_CMODE;
1233 : }
1234 :
1235 : #define WMC_TOOL_SKIP
1236 : /* Get the reference energy */
1237 102 : exp_normd = norm_l( L_qint );
1238 102 : rev_qint_fx = div_s( 0x4000, round_fx( L_shl( L_qint, exp_normd ) ) ); /* Q14-(29+exp_normd-16)+15 */
1239 102 : Qrev_qint = sub( 14 - ( 29 - 16 ) + 15, exp_normd );
1240 :
1241 102 : bq0 = (int16_t) round_fx( L_shl( L_mult( eref_fx, rev_qint_fx ), sub( 5, Qrev_qint ) ) ); /* 16-(10+Qrev_qint+1) */
1242 : #undef WMC_TOOL_SKIP
1243 :
1244 : /* Reconstruct quantized spectrum */
1245 102 : bq1[0] = bq2[0] + bq0;
1246 2334 : for ( k = 1; k < bands; k++ )
1247 : {
1248 2232 : bq1[k] = bq2[k] + bq1[k - 1];
1249 : }
1250 :
1251 2436 : for ( k = 0; k < bands; k++ )
1252 : {
1253 : #define WMC_TOOL_SKIP
1254 2334 : L_band_energy[k] = L_mls( L_qint, bq1[k] );
1255 2334 : move32(); /* 29+0-15 -> Qbe(Q14) */
1256 2334 : band_energy[k] = (float) ( L_band_energy[k] / pow( 2.0f, SWB_BWE_LR_Qbe ) );
1257 : #undef WMC_TOOL_SKIP
1258 : }
1259 :
1260 102 : if ( is_transient )
1261 : {
1262 9 : reverse_transient_frame_energies( band_energy, bands );
1263 : }
1264 :
1265 102 : return ( deng_bits );
1266 : }
1267 :
1268 :
1269 : /*--------------------------------------------------------------------------*
1270 : * p2a_threshold_dequant()
1271 : *
1272 : *
1273 : *--------------------------------------------------------------------------*/
1274 :
1275 102 : static int16_t p2a_threshold_dequant(
1276 : Decoder_State *st, /* i/o: decoder state structure */
1277 : int16_t *p2a_flags,
1278 : const int16_t bands,
1279 : const int16_t p2a_bands )
1280 : {
1281 : int16_t j, k;
1282 :
1283 1971 : for ( k = 0; k < bands - p2a_bands; k++ )
1284 : {
1285 1869 : p2a_flags[k] = 1;
1286 : }
1287 :
1288 102 : j = 0;
1289 567 : for ( k = bands - p2a_bands; k < bands; k++ )
1290 : {
1291 465 : p2a_flags[k] = get_next_indice( st, 1 );
1292 465 : j++;
1293 : }
1294 :
1295 102 : return ( j );
1296 : }
1297 :
1298 :
1299 : /*--------------------------------------------------------------------------*
1300 : * mdct_spectrum_fine_gain_dec()
1301 : *
1302 : *
1303 : *--------------------------------------------------------------------------*/
1304 :
1305 102 : static void mdct_spectrum_fine_gain_dec(
1306 : Decoder_State *st, /* i/o: decoder state structure */
1307 : float y2[],
1308 : const int16_t band_start[],
1309 : const int16_t band_end[],
1310 : const int16_t k_sort[],
1311 : const int16_t bands,
1312 : const Word32 L_qint,
1313 : const int16_t Ngq,
1314 : const int16_t gqlevs,
1315 : const int16_t gqbits )
1316 : {
1317 : int16_t i, k, imin;
1318 : float gamma;
1319 : float gain_table[MAX_GQLEVS];
1320 : Word16 exp_normn, exp_normd;
1321 : Word16 delta_fx, Qdelta;
1322 : Word32 L_delta, L_q;
1323 : Word32 L_temp;
1324 : Word16 gain_table_fx[MAX_GQLEVS];
1325 : Word16 Qgt;
1326 : Word16 temp_lo_fx, temp_hi_fx;
1327 :
1328 : #define WMC_TOOL_SKIP
1329 : /* Fine gain quantization on only the most significant energy bands */
1330 : /*delta = qint / gqlevs; */
1331 102 : exp_normn = norm_l( L_qint );
1332 102 : exp_normn = sub( exp_normn, 1 );
1333 102 : exp_normd = norm_s( gqlevs );
1334 102 : delta_fx = div_l( L_shl( L_qint, exp_normn ), shl( gqlevs, exp_normd ) );
1335 102 : Qdelta = add( sub( exp_normn, exp_normd ), 28 ); /* 29+exp_normn-(exp_normd)-1; */
1336 102 : L_delta = L_shl( L_deposit_h( delta_fx ), sub( 13, Qdelta ) );
1337 : /*q = (-qint + delta) / 2.0f; */
1338 102 : L_q = L_shr( L_sub( L_delta, L_qint ), 1 );
1339 :
1340 306 : FOR( i = 0; i < gqlevs; i++ )
1341 : {
1342 : /*gain_table[i] = (float) pow (2.0f, q * 0.5f); */
1343 204 : L_temp = L_shr( L_shr( L_q, 1 ), sub( 29, 16 ) );
1344 204 : temp_lo_fx = L_Extract_lc( L_temp, &temp_hi_fx );
1345 204 : Qgt = sub( 14, temp_hi_fx );
1346 204 : gain_table_fx[i] = extract_l( Pow2( 14, temp_lo_fx ) ); /* Qgt */
1347 :
1348 : /*q += delta; */
1349 204 : L_q = L_add( L_q, L_delta );
1350 204 : gain_table_fx[i] = shl( gain_table_fx[i], sub( 14, Qgt ) ); /* Qgt -> Q14 */
1351 204 : gain_table[i] = (float) ( gain_table_fx[i] / pow( 2.0f, 14 ) );
1352 : }
1353 : #undef WMC_TOOL_SKIP
1354 :
1355 510 : for ( k = bands - Ngq; k < bands; k++ )
1356 : {
1357 408 : imin = get_next_indice( st, gqbits );
1358 408 : gamma = gain_table[imin];
1359 :
1360 7386 : for ( i = band_start[k_sort[k]]; i <= band_end[k_sort[k]]; i++ )
1361 : {
1362 6978 : y2[i] *= gamma;
1363 : }
1364 : }
1365 :
1366 102 : return;
1367 : }
1368 :
1369 : /*--------------------------------------------------------------------------*
1370 : * spt_shorten_domain_set_dec()
1371 : *
1372 : * update the shorten band information based on p2a analysis
1373 : *--------------------------------------------------------------------------*/
1374 :
1375 93 : static void spt_shorten_domain_set_dec(
1376 : Decoder_State *st, /* i : decoder state structure */
1377 : const int16_t p2a_flags[], /* i : p2a anlysis information */
1378 : const int16_t new_band_start[], /* i : new band start position */
1379 : const int16_t new_band_end[], /* i : new band end position */
1380 : const int16_t new_band_width[], /* i : new subband band width */
1381 : const int16_t bands, /* i : total number of subbands */
1382 : int16_t band_start[], /* o : band start position */
1383 : int16_t band_end[], /* o : band end position */
1384 : int16_t band_width[], /* o : sub band band width */
1385 : int16_t *bit_budget /* i/o: bit budget */
1386 : )
1387 : {
1388 : int16_t j, k;
1389 : int16_t spt_shorten_flag[SPT_SHORTEN_SBNUM];
1390 :
1391 93 : j = 0;
1392 465 : for ( k = bands - SPT_SHORTEN_SBNUM; k < bands; k++ )
1393 : {
1394 372 : spt_shorten_flag[j] = 0;
1395 372 : if ( p2a_flags[k] == 1 )
1396 : {
1397 12 : spt_shorten_flag[j] = get_next_indice( st, 1 );
1398 12 : *bit_budget -= 1;
1399 12 : if ( spt_shorten_flag[j] == 1 )
1400 : {
1401 0 : band_start[k] = new_band_start[j];
1402 0 : band_end[k] = new_band_end[j];
1403 0 : band_width[k] = new_band_width[j];
1404 : }
1405 : }
1406 :
1407 372 : j++;
1408 : }
1409 :
1410 93 : return;
1411 : }
|