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 : #ifdef BASOP_NOGLOB
266 2334 : Ep_fx[i] = L_shl_o( L_tmp, s_max( sub( exp, 6 ), -31 ), &Overflow ); /* Q -6 */
267 : #else
268 : Ep_fx[i] = L_shl( L_tmp, s_max( sub( exp, 6 ), -31 ) ); /* Q -6 */
269 : #endif
270 2334 : Ep[i] = (float) ( Ep_fx[i] / pow( 2.0, -6 ) );
271 : }
272 :
273 2436 : FOR( i = 0; i < bands; i++ )
274 : {
275 2334 : L_tmp2 = Ep_fx[i];
276 2334 : L_tmp = L_max( 1, L_tmp2 );
277 2334 : exp = norm_l( L_tmp );
278 2334 : tmp = extract_h( L_shl( L_tmp, exp ) );
279 :
280 2334 : L_tmp3 = (Word32) band_width[i];
281 2334 : exp2 = norm_l( L_tmp3 );
282 2334 : tmp2 = extract_h( L_shl( L_tmp3, exp2 ) );
283 :
284 2334 : exp2 = sub( exp, exp2 ); /* Denormalize and substract */
285 :
286 2334 : tmp3 = sub( tmp2, tmp );
287 2334 : IF( tmp3 > 0 )
288 : {
289 894 : tmp2 = shr( tmp2, 1 );
290 : }
291 2334 : IF( tmp3 > 0 )
292 : {
293 894 : exp2 = add( exp2, 1 );
294 : }
295 2334 : tmp = div_s( tmp2, tmp );
296 2334 : L_tmp = L_deposit_h( tmp );
297 2334 : L_tmp = Isqrt_lc1( L_tmp, &exp2 );
298 2334 : move32(); /*Q(31-exp2) */
299 2334 : Ep_tmp_fx[i] = L_shr( L_tmp, sub( 15, exp2 ) ); /*Q13 */
300 2334 : Ep_tmp[i] = (float) ( Ep_tmp_fx[i] / pow( 2.0, 13 ) );
301 : }
302 : #undef WMC_TOOL_SKIP
303 :
304 102 : if ( *is_transient == 0 && inner_frame == L_FRAME8k && st->core_brate <= ACELP_13k20 )
305 : {
306 : /* decode the last p2a_bands-1 subbands bit-allocation index of the previous frame */
307 0 : j = 0;
308 0 : for ( i = 0; i < 2; i++ )
309 : {
310 0 : last_bitalloc_max_band[i] = get_next_indice( st, 1 );
311 : }
312 :
313 : #define WMC_TOOL_SKIP
314 0 : lowband = 6;
315 0 : move16();
316 0 : trans_bit = 2;
317 0 : move16();
318 0 : bit_budget = sub( bit_budget, trans_bit );
319 0 : gama_fx = 27852; /*Q15 0.85f;*/
320 0 : beta_fx = 17203;
321 0 : move16(); /*Q14 1.05f; */
322 0 : set_s( &p2a_flags_tmp[bands - trans_bit], 0, 2 );
323 :
324 0 : IF( st->core_brate == ACELP_13k20 )
325 : {
326 0 : beta_fx = 13107;
327 0 : move16(); /*14 1.25f; */
328 0 : gama_fx = 31130;
329 0 : move16(); /*0.95f; */
330 0 : mvs2s( &p2a_flags[sub( bands, trans_bit )], &p2a_flags_tmp[sub( bands, trans_bit )], trans_bit );
331 : }
332 :
333 : /* calculate the the low band/high band energy and the variance/avrage of the envelopes */
334 0 : Ep_vari_fx = 0;
335 0 : move32();
336 0 : Ep_avrg_fx = 0;
337 0 : move32();
338 0 : Ep_avrgL_fx = 0;
339 0 : move32();
340 0 : Ep_peak_fx = 0;
341 0 : move32();
342 0 : FOR( i = 0; i < bands; i++ )
343 : {
344 0 : IF( sub( i, lowband ) >= 0 )
345 : {
346 0 : Ep_vari_fx = L_add( Ep_vari_fx, L_abs( L_sub( Ep_tmp_fx[i], Ep_tmp_fx[sub( i, 1 )] ) ) ); /*Q15 */
347 0 : Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
348 : }
349 : ELSE
350 : {
351 0 : Ep_avrgL_fx = L_add( Ep_avrgL_fx, Ep_tmp_fx[i] ); /*Q15 */
352 0 : IF( L_sub( Ep_tmp_fx[i], Ep_peak_fx ) > 0 )
353 : {
354 0 : Ep_peak_fx = Ep_tmp_fx[i];
355 0 : move32(); /*Q15 */
356 : }
357 : }
358 : }
359 : /* modify the last p2a_bands subbands band_energies */
360 0 : k = bands;
361 0 : mvl2l( L_band_energy, L_band_energy_tmp, k ); /*Q_band_energy */
362 0 : Mpy_32_16_ss( Ep_peak_fx, 24576, &L_tmp, &lo );
363 0 : Mpy_32_16_ss( Ep_peak_fx, shl( sub( bands, lowband ), 9 ), &L_tmp2, &lo );
364 0 : Mpy_32_16_ss( Ep_avrg_fx, 1126, &L_tmp3, &lo );
365 :
366 0 : IF( ( ( L_sub( L_tmp, L_shr( Ep_avrgL_fx, 1 ) ) < 0 && st->core_brate == ACELP_13k20 ) || st->core_brate < ACELP_13k20 ) &&
367 : L_sub( L_tmp2, L_tmp3 ) < 0 && L_sub( L_tmp2, L_shr( Ep_avrg_fx, 7 ) ) > 0 )
368 : {
369 0 : FOR( i = lowband; i < bands; i++ )
370 : {
371 0 : Mpy_32_16_ss( Ep_avrg_fx, 24576, &L_tmp, &lo );
372 0 : IF( L_sub( L_shr( Ep_tmp_fx[i], 1 ), L_tmp ) < 0 )
373 : {
374 0 : Mpy_32_16_ss( Ep_peak_fx, sub( bands, lowband ), &L_tmp, &lo );
375 0 : tmp = extract_h( L_shl( L_tmp, 14 ) ); /*Q-4 */
376 0 : IF( tmp != 0 )
377 : {
378 0 : exp = norm_s( tmp );
379 0 : tmp = shl( tmp, exp ); /*Q(exp) */
380 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
381 0 : exp = sub( 29, exp );
382 : }
383 : ELSE
384 : {
385 0 : tmp = 0x7fff;
386 0 : move16();
387 0 : exp = 0;
388 0 : move16();
389 : }
390 0 : Mpy_32_16_ss( Ep_avrg_fx, tmp, &L_tmp, &lo );
391 0 : L_tmp = L_shl( L_tmp, sub( 13, exp ) ); /*Q(13+exp-15 +13-exp +4 = 15) */
392 0 : L_tmp2 = L_add( L_tmp, 13107 ); /*15 */
393 0 : tmp2 = extract_l( L_min( L_max( L_tmp2, 16384 ), gama_fx ) ); /*15 = 15 */
394 0 : Mpy_32_16_ss( L_band_energy_tmp[i], tmp2, &L_band_energy_tmp[i], &lo );
395 : }
396 : }
397 : }
398 : ELSE
399 : {
400 0 : FOR( i = sub( bands, trans_bit ); i < bands; i++ )
401 : {
402 0 : alpha_fx = 16384;
403 0 : move16(); /*Q14 */
404 0 : IF( sub( p2a_flags_tmp[i], 1 ) == 0 )
405 : {
406 0 : Mpy_32_16_ss( Ep_tmp_fx[i], sub( bands, lowband ), &L_tmp, &lo );
407 0 : tmp = extract_h( L_shl( L_tmp, 14 ) ); /*Q-4 */
408 0 : IF( tmp != 0 )
409 : {
410 0 : exp = norm_s( tmp );
411 0 : tmp = shl( tmp, exp ); /*Q(exp) */
412 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
413 0 : exp = sub( 29, exp );
414 : }
415 : ELSE
416 : {
417 0 : tmp = 0x7fff;
418 0 : move16();
419 0 : exp = 0;
420 0 : move16();
421 : }
422 0 : Mpy_32_16_ss( Ep_vari_fx, 3277, &L_tmp, &lo );
423 0 : Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
424 0 : L_tmp = L_shl( L_tmp, sub( 12, exp ) ); /*Q(13+exp-15 +12-exp +4 = 14) */
425 :
426 0 : tmp2 = extract_h( Ep_avrg_fx ); /*Q13-16=-3 */
427 0 : IF( tmp2 != 0 )
428 : {
429 0 : exp = norm_s( tmp2 );
430 0 : tmp2 = shl( tmp2, exp ); /*Q(exp) */
431 0 : tmp2 = div_s( 16384, tmp2 ); /*Q(15+14-exp=29-exp) */
432 0 : exp = sub( 29, exp );
433 : }
434 : ELSE
435 : {
436 : /*when the divisor is zero, happens rarely*/
437 0 : tmp2 = 0x7fff;
438 0 : move16();
439 0 : exp = 0;
440 0 : move16();
441 : }
442 0 : Mpy_32_16_ss( Ep_vari_fx, 6554, &L_tmp2, &lo );
443 0 : Mpy_32_16_ss( L_tmp2, tmp2, &L_tmp2, &lo );
444 0 : L_tmp2 = L_shl( L_tmp2, sub( 13, exp ) ); /*Q(13+exp-15 +13-exp +3 = 14) */
445 0 : L_tmp = L_min( L_tmp, L_tmp2 ); /*14 */
446 0 : tmp = extract_l( L_min( L_tmp, 13107 ) ); /*14 */
447 0 : alpha_fx = add( 16384, tmp );
448 : }
449 0 : IF( sub( last_bitalloc_max_band[j++], 1 ) == 0 )
450 : {
451 0 : Mpy_32_16_ss( Ep_tmp_fx[i], sub( bands, lowband ), &L_tmp, &lo );
452 0 : tmp = extract_h( L_shl( L_tmp, 14 ) ); /*Q-2 */
453 0 : IF( tmp != 0 )
454 : {
455 0 : exp = norm_s( tmp );
456 0 : tmp = shl( tmp, exp ); /*Q(exp) */
457 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
458 0 : exp = sub( 29, exp );
459 : }
460 : ELSE
461 : {
462 0 : tmp = 0x7fff;
463 0 : move16();
464 0 : exp = 0;
465 0 : move16();
466 : }
467 0 : Mpy_32_16_ss( Ep_avrg_fx, tmp, &L_tmp, &lo );
468 : #ifndef BASOP_NOGLOB
469 : L_tmp = L_shl( L_tmp, sub( 14, exp ) ); /*Q(13+exp-15 +14-exp+2 = 14) */
470 : L_tmp = L_max( L_tmp, 16384 ); /*14 */
471 : tmp = extract_l( L_min( L_tmp, beta_fx ) ); /*14 */
472 : alpha_fx = shl( mult( alpha_fx, tmp ), 1 ); /*14+14-15 +1=14 */
473 : #else /* BASOP_NOGLOB */
474 0 : L_tmp = L_shl_o( L_tmp, sub( 14, exp ), &Overflow ); /*Q(13+exp-15 +14-exp+2 = 14) */
475 0 : L_tmp = L_max( L_tmp, 16384 ); /*14 */
476 0 : tmp = extract_l( L_min( L_tmp, beta_fx ) ); /*14 */
477 0 : alpha_fx = shl( mult( alpha_fx, tmp ), 1 ); /*14+14-15 +1=14 */
478 : #endif /* BASOP_NOGLOB */
479 : }
480 : ELSE
481 : {
482 0 : tmp2 = extract_h( Ep_avrg_fx ); /*13 -16 =-3 */
483 0 : IF( tmp2 != 0 )
484 : {
485 0 : exp = norm_s( tmp2 );
486 0 : tmp2 = shl( tmp2, exp ); /*Q(exp) */
487 0 : tmp2 = div_s( 16384, tmp2 ); /*Q(15+14-exp=29-exp) */
488 0 : exp = sub( 29, exp );
489 : }
490 : ELSE
491 : {
492 : /*when the divisor is zero, happens rarely*/
493 0 : tmp2 = 0x7fff;
494 0 : move16();
495 0 : exp = 0;
496 0 : move16();
497 : }
498 0 : Mpy_32_16_ss( Ep_tmp_fx[i], tmp2, &L_tmp, &lo );
499 0 : L_tmp = L_shl( L_tmp, sub( 19, exp ) ); /*Q(13+exp-15 +19-exp +3 = 20) */
500 0 : Mpy_32_16_ss( L_tmp, shl( sub( bands, lowband ), 9 ), &L_tmp, &lo );
501 0 : L_tmp = L_max( L_tmp, 13926 ); /*14 */
502 0 : tmp2 = extract_l( L_min( L_tmp, 16384 ) ); /*14 */
503 0 : alpha_fx = shl( mult( alpha_fx, tmp2 ), 1 ); /*14+14-15+1 =14 */
504 : }
505 0 : Mpy_32_16_ss( L_band_energy_tmp[i], alpha_fx, &L_tmp, &lo );
506 0 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q(Q_band_energy+14-15 +1= Q_band_energy) */
507 : }
508 : }
509 0 : lowband = 3;
510 0 : move16();
511 0 : Ep_avrg_fx = 0;
512 0 : move32();
513 0 : Ep_avrgL_fx = 0;
514 0 : move32();
515 0 : Ep_peak_fx = 0;
516 0 : move32();
517 0 : FOR( i = 0; i < bands; i++ )
518 : {
519 0 : IF( sub( i, lowband ) >= 0 )
520 : {
521 0 : Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
522 : }
523 : ELSE
524 : {
525 0 : Ep_avrgL_fx = L_add( Ep_avrgL_fx, L_shr( Ep_tmp_fx[i], 1 ) ); /*Q12 */
526 0 : IF( L_sub( Ep_tmp_fx[i], Ep_peak_fx ) > 0 )
527 : {
528 0 : Ep_peak_fx = Ep_tmp_fx[i];
529 0 : move32(); /*Q13 */
530 : }
531 : }
532 : }
533 0 : Mpy_32_16_ss( Ep_peak_fx, 28262, &L_tmp, &lo );
534 0 : Mpy_32_16_ss( Ep_avrgL_fx, 24576, &L_tmp2, &lo );
535 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 )
536 : {
537 0 : adjustFlag = 1;
538 0 : move16();
539 0 : FOR( i = 0; i < lowband; i++ )
540 : {
541 0 : tmp = extract_h( Ep_avrgL_fx ); /*Q-4 */
542 0 : IF( tmp != 0 )
543 : {
544 0 : exp = norm_s( tmp );
545 0 : tmp = shl( tmp, exp ); /*Q(exp) */
546 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
547 0 : exp = sub( 29, exp );
548 : }
549 : ELSE
550 : {
551 0 : tmp = 0x7fff;
552 0 : move16();
553 0 : exp = 0;
554 0 : move16();
555 : }
556 0 : Mpy_32_16_ss( Ep_peak_fx, tmp, &L_tmp, &lo );
557 0 : Mpy_32_16_ss( L_tmp, lowband, &L_tmp, &lo );
558 0 : Mpy_32_16_ss( L_tmp, 18842, &L_tmp, &lo );
559 : #ifndef BASOP_NOGLOB
560 : L_tmp = L_shl( L_tmp, sub( 27, exp ) ); /*Q14 0.5 */
561 : tmp2 = extract_l( L_min( L_tmp, 19661 ) ); /*14 */
562 : #else /* BASOP_NOGLOB */
563 0 : L_tmp = L_shl_o( L_tmp, sub( 27, exp ), &Overflow ); /*Q14 0.5 */
564 0 : tmp2 = extract_l( L_min( L_tmp, 19661 ) ); /*14 */
565 : #endif /* BASOP_NOGLOB */
566 0 : Mpy_32_16_ss( L_band_energy_tmp[i], tmp2, &L_tmp, &lo );
567 : #ifndef BASOP_NOGLOB
568 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q_band_energy */
569 : #else /* BASOP_NOGLOB */
570 0 : L_band_energy_tmp[i] = L_shl_o( L_tmp, 1, &Overflow ); /*Q_band_energy */
571 : #endif /* BASOP_NOGLOB */
572 : }
573 : }
574 : #undef WMC_TOOL_SKIP
575 :
576 0 : for ( i = 0; i < bands; i++ )
577 : {
578 0 : band_energy_tmp[i] = (float) ( L_band_energy_tmp[i] / pow( 2.0f, SWB_BWE_LR_Qbe ) );
579 : }
580 :
581 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 );
582 : }
583 102 : else if ( *is_transient == 0 && inner_frame == L_FRAME16k )
584 : {
585 : #define WMC_TOOL_SKIP
586 0 : bit_budget = sub( bit_budget, 2 ); /* bits in high bands to indicate the last 2 subbands is allocated bits or not */
587 : /* decode the last p2a_bands-1 subbands bit-allocation index of the previous frame */
588 0 : for ( i = 0; i < 2; i++ )
589 : {
590 0 : last_bitalloc_max_band[i] = get_next_indice( st, 1 );
591 : }
592 0 : FOR( i = 0; i < bands; i++ )
593 : {
594 : #ifndef BASOP_NOGLOB
595 : Ep_tmp_fx[i] = L_shl( Ep_tmp_fx[i], 2 );
596 : #else /* BASOP_NOGLOB */
597 0 : Ep_tmp_fx[i] = L_shl_o( Ep_tmp_fx[i], 2, &Overflow );
598 : #endif /* BASOP_NOGLOB */
599 : }
600 0 : IF( st->core_brate == ACELP_13k20 )
601 : {
602 0 : lowband = 8;
603 0 : move16();
604 0 : highband = 15;
605 0 : move16();
606 0 : bw_low = sub( band_start[highband], band_start[lowband] );
607 0 : bw_high = sub( add( band_end[sub( bands, 1 )], 1 ), band_start[highband] );
608 : }
609 : ELSE
610 : {
611 0 : lowband = 8;
612 0 : move16();
613 0 : highband = 16;
614 0 : move16();
615 0 : bw_low = sub( band_start[highband], band_start[lowband] );
616 0 : bw_high = sub( add( band_end[sub( bands, 1 )], 1 ), band_start[highband] );
617 : }
618 : /* calculate the the low band/high band energy and the variance/avrage of the envelopes */
619 0 : enerL_fx = 0;
620 0 : move32();
621 0 : enerH_fx = 0;
622 0 : move32();
623 0 : Ep_vari_fx = 0;
624 0 : move32();
625 0 : Ep_avrg_fx = 0;
626 0 : move32();
627 0 : FOR( i = 0; i < bands; i++ )
628 : {
629 0 : IF( sub( i, lowband ) >= 0 && add( sub( i, bands ), p2a_bands ) < 0 )
630 : {
631 : #ifndef BASOP_NOGLOB
632 : Ep_vari_fx = L_add( Ep_vari_fx, L_abs( L_sub( Ep_tmp_fx[i], Ep_tmp_fx[sub( i, 1 )] ) ) ); /*Q15 */
633 : Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
634 : #else /* BASOP_NOGLOB */
635 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 */
636 0 : Ep_avrg_fx = L_add_o( Ep_avrg_fx, Ep_tmp_fx[i], &Overflow ); /*Q15 */
637 : #endif /* BASOP_NOGLOB */
638 : }
639 :
640 0 : IF( sub( i, highband ) >= 0 )
641 : {
642 : /* safety check in case of bit errors */
643 0 : if ( Ep_fx[i] > 536788991 /* max(Q30) */ )
644 : {
645 0 : st->BER_detect = 1;
646 0 : set_f( yout, 0, inner_frame );
647 0 : return;
648 : }
649 :
650 0 : enerH_fx = L_add( enerH_fx, L_shl( Ep_fx[i], 2 ) ); /*Q0 */
651 : }
652 0 : ELSE IF( sub( i, lowband ) >= 0 )
653 : {
654 : /* safety check in case of bit errors */
655 0 : if ( Ep_fx[i] > 536788991 /* max(Q30) */ )
656 : {
657 0 : st->BER_detect = 1;
658 0 : set_f( yout, 0, inner_frame );
659 0 : return;
660 : }
661 :
662 0 : enerL_fx = L_add( enerL_fx, L_shl( Ep_fx[i], 2 ) ); /*Q0 */
663 : }
664 : }
665 :
666 0 : enerL = (float) ( enerL_fx / pow( 2.0, -4 ) );
667 0 : enerH = (float) ( enerH_fx / pow( 2.0, -4 ) );
668 : /* modify the last p2a_bands subbands band_energies */
669 0 : k = bands;
670 0 : mvl2l( L_band_energy, L_band_energy_tmp, k ); /*Q_band_energy */
671 :
672 0 : L_tmp = L_max( enerH_fx, enerL_fx );
673 0 : tmp = s_max( bw_low, bw_high );
674 0 : i = norm_l( L_tmp );
675 0 : j = norm_s( tmp );
676 0 : Mpy_32_16_ss( L_shl( enerH_fx, i ), shl( bw_low, j ), &L_tmp, &lo );
677 0 : Mpy_32_16_ss( L_shl( enerL_fx, i ), shl( bw_high, j ), &L_tmp2, &lo );
678 0 : L_tmp2 = L_sub( L_tmp, L_tmp2 );
679 :
680 0 : FOR( i = sub( bands, p2a_bands ); i < bands; i++ )
681 : {
682 0 : IF( sub( p2a_flags[i], 1 ) == 0 || L_tmp2 > 0 )
683 : {
684 0 : tmp = sub( bands, p2a_bands );
685 0 : tmp = sub( tmp, lowband ); /*Q0 */
686 :
687 : #ifndef BASOP_NOGLOB
688 : tmp1 = extract_h( L_shl( Ep_avrg_fx, 1 ) ); /*Q0 */
689 : #else
690 0 : tmp1 = extract_h( L_shl_o( Ep_avrg_fx, 1, &Overflow ) ); /*Q0 */
691 : #endif
692 0 : IF( tmp1 != 0 )
693 : {
694 0 : exp = norm_s( tmp1 );
695 0 : tmp1 = shl( tmp1, exp ); /*Q(exp) */
696 0 : tmp1 = div_s( 16384, tmp1 ); /*Q(15+14-exp = 29-exp) */
697 0 : exp = sub( 29, exp );
698 : }
699 : ELSE
700 : {
701 : /*when the divisor is zero, happens rarely*/
702 0 : tmp1 = 0x7fff;
703 0 : move16();
704 0 : exp = 0;
705 0 : move16();
706 : }
707 0 : Mpy_32_16_ss( Ep_tmp_fx[i], tmp1, &L_tmp, &lo );
708 0 : Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
709 0 : Mpy_32_16_ss( L_tmp, 16384, &L_tmp, &lo );
710 0 : L_tmp = L_shl( L_tmp, sub( 32, exp ) ); /*Q15 */
711 0 : tmp = extract_l( L_min( L_tmp, 6554 ) ); /*Q15 */
712 0 : Mpy_32_16_ss( Ep_vari_fx, tmp1, &L_tmp, &lo );
713 0 : Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
714 0 : L_tmp = L_shl( L_tmp, sub( 15, exp ) ); /*Q15 */
715 0 : tmp = extract_l( L_shr( L_min( L_tmp, 13107 ), 1 ) ); /*Q14 */
716 0 : alpha_fx = add( tmp, 16384 ); /*Q14 */
717 : }
718 : ELSE
719 : {
720 0 : alpha_fx = 16384;
721 0 : move16(); /*Q14 */
722 : }
723 :
724 0 : IF( add( sub( i, bands ), p2a_bands ) > 0 )
725 : {
726 0 : tmp = sub( bands, p2a_bands );
727 0 : IF( sub( last_bitalloc_max_band[sub( i, add( tmp, 1 ) )], 1 ) == 0 )
728 : {
729 0 : tmp = sub( tmp, lowband );
730 0 : Mpy_32_16_ss( Ep_tmp_fx[i], tmp, &L_tmp, &lo );
731 : #ifndef BASOP_NOGLOB
732 : tmp = extract_h( L_shl( L_tmp, 16 ) ); /*Q0 */
733 : #else
734 0 : tmp = extract_h( L_shl_o( L_tmp, 16, &Overflow ) ); /*Q0 */
735 : #endif
736 0 : IF( tmp != 0 )
737 : {
738 0 : exp = norm_s( tmp );
739 0 : tmp = shl( tmp, exp ); /*Q(exp) */
740 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
741 0 : exp = sub( 29, exp );
742 : }
743 : ELSE
744 : {
745 0 : tmp = 0x7fff;
746 0 : move16();
747 0 : exp = 0;
748 0 : move16();
749 : }
750 0 : Mpy_32_16_ss( Ep_avrg_fx, tmp, &L_tmp, &lo );
751 0 : L_tmp = L_shl( L_tmp, sub( 14, exp ) ); /*Q14 */
752 0 : tmp = extract_l( L_min( L_max( L_tmp, 16384 ), 20480 ) ); /*Q14 */
753 0 : L_tmp = L_mult( alpha_fx, tmp ); /*Q(14+14+1=29) */
754 0 : alpha_fx = extract_l( L_shr( L_tmp, 15 ) ); /*Q14 */
755 : }
756 : ELSE
757 : {
758 0 : tmp = sub( tmp, lowband );
759 :
760 : #ifndef BASOP_NOGLOB
761 : tmp1 = extract_h( L_shl( Ep_avrg_fx, 1 ) ); /*Q0 */
762 : #else
763 0 : tmp1 = extract_h( L_shl_o( Ep_avrg_fx, 1, &Overflow ) ); /*Q0 */
764 : #endif
765 0 : IF( tmp1 != 0 )
766 : {
767 0 : exp = norm_s( tmp1 );
768 0 : tmp1 = shl( tmp1, exp ); /*Q(exp) */
769 0 : tmp1 = div_s( 16384, tmp1 ); /*Q(15+14-exp=29-exp) */
770 0 : exp = sub( 29, exp );
771 : }
772 : ELSE
773 : {
774 : /*when the divisor is zero, happens rarely*/
775 0 : tmp1 = 0x7fff;
776 0 : move16();
777 0 : exp = 0;
778 0 : move16();
779 : }
780 0 : Mpy_32_16_ss( Ep_tmp_fx[i], tmp1, &L_tmp, &lo );
781 0 : Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
782 0 : L_tmp = L_shl( L_tmp, sub( 29, exp ) ); /*Q14 */
783 0 : tmp = extract_l( L_min( L_max( L_tmp, 13926 ), 16384 ) ); /*Q14 */
784 0 : L_tmp = L_mult( alpha_fx, tmp ); /*Q(14+14+1=29) */
785 0 : alpha_fx = extract_l( L_shr( L_tmp, 15 ) ); /*Q14 */
786 : }
787 : }
788 0 : Mpy_32_16_ss( L_band_energy_tmp[i], alpha_fx, &L_tmp, &lo );
789 0 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q Q_band_energy */
790 : }
791 0 : lowband = 6;
792 0 : move16();
793 0 : Ep_avrg_fx = 0;
794 0 : move32();
795 0 : Ep_avrgL_fx = 0;
796 0 : move32();
797 0 : Ep_peak_fx = 0;
798 0 : move32();
799 0 : FOR( i = 0; i < bands; i++ )
800 : {
801 0 : IF( sub( i, lowband ) >= 0 )
802 : {
803 : #ifndef BASOP_NOGLOB
804 : Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
805 : #else /* BASOP_NOGLOB */
806 0 : Ep_avrg_fx = L_add_o( Ep_avrg_fx, Ep_tmp_fx[i], &Overflow ); /*Q15 */
807 : #endif /* BASOP_NOGLOB */
808 : }
809 : ELSE
810 : {
811 : #ifndef BASOP_NOGLOB
812 : Ep_avrgL_fx = L_add( Ep_avrgL_fx, Ep_tmp_fx[i] ); /*Q15 */
813 : #else /* BASOP_NOGLOB */
814 0 : Ep_avrgL_fx = L_add_o( Ep_avrgL_fx, Ep_tmp_fx[i], &Overflow ); /*Q15 */
815 : #endif /* BASOP_NOGLOB */
816 0 : IF( L_sub( Ep_tmp_fx[i], Ep_peak_fx ) > 0 )
817 : {
818 0 : Ep_peak_fx = Ep_tmp_fx[i];
819 0 : move32(); /*Q15 */
820 : }
821 : }
822 : }
823 :
824 0 : Mpy_32_16_ss( Ep_peak_fx, 24576, &L_tmp, &lo );
825 0 : Mpy_32_16_ss( Ep_peak_fx, 19661, &L_tmp2, &lo );
826 0 : Mpy_32_16_ss( Ep_avrgL_fx, 24576, &L_tmp3, &lo );
827 :
828 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 ) ||
829 : ( 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 ) )
830 : {
831 0 : adjustFlag = 1;
832 0 : move16();
833 0 : FOR( i = 0; i < lowband; i++ )
834 : {
835 : #ifndef BASOP_NOGLOB
836 : tmp = extract_h( L_shl( Ep_avrgL_fx, 1 ) ); /*Q0 */
837 : #else
838 0 : tmp = extract_h( L_shl_o( Ep_avrgL_fx, 1, &Overflow ) ); /*Q0 */
839 : #endif
840 0 : IF( tmp != 0 )
841 : {
842 0 : exp = norm_s( tmp );
843 0 : tmp = shl( tmp, exp ); /*Q(exp) */
844 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
845 0 : exp = sub( 29, exp );
846 : }
847 : ELSE
848 : {
849 0 : tmp = 0x7fff;
850 0 : move16();
851 0 : exp = 0;
852 0 : move16();
853 : }
854 0 : Mpy_32_16_ss( Ep_peak_fx, tmp, &L_tmp, &lo );
855 0 : Mpy_32_16_ss( L_tmp, lowband, &L_tmp, &lo );
856 0 : L_tmp = L_shl( L_tmp, sub( 28, exp ) ); /*Q14 0.5 */
857 0 : tmp = extract_l( L_min( L_tmp, 19661 ) ); /* Q14 */
858 0 : Mpy_32_16_ss( L_band_energy_tmp[i], tmp, &L_tmp, &lo );
859 0 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q_band_energy */
860 : }
861 : }
862 : #undef WMC_TOOL_SKIP
863 :
864 0 : for ( i = 0; i < bands; i++ )
865 : {
866 0 : band_energy_tmp[i] = (float) ( L_band_energy_tmp[i] / pow( 2.0f, SWB_BWE_LR_Qbe ) );
867 : }
868 :
869 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 );
870 : }
871 102 : else if ( st->bwidth == SWB && hqswb_clas == HQ_HARMONIC && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
872 : {
873 0 : hq2_bit_alloc_har( band_energy, bit_budget, bands, Rk_fx, p2a_bands, bwe_br, p2a_flags, band_width );
874 : }
875 : else
876 : {
877 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 );
878 : }
879 :
880 102 : if ( bit_budget < 0 )
881 : {
882 0 : st->BER_detect = 1;
883 0 : bit_budget = 0;
884 : }
885 :
886 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 );
887 :
888 : /* Prepare floating Rk for next modules */
889 2436 : for ( k = 0; k < bands; k++ )
890 : {
891 2334 : Rk[k] = WORD322FL_SCALE( Rk_fx[k], SWB_BWE_LR_QRk - 1 );
892 : }
893 :
894 : /* Denormalize the coded MDCT spectrum */
895 102 : mdct_spectrum_denorm( inp_vector, y2, band_start, band_end, band_width, band_energy, npulses, bands, ld_slope, pd_thresh );
896 :
897 : /* Apply fine gain to denormalized coded spectrum */
898 102 : mdct_spectrum_fine_gain_dec( st, y2, band_start, band_end, k_sort, bands, L_qint, Ngq, gqlevs, gqbits );
899 :
900 : /*restore the band information */
901 102 : if ( flag_spt == 1 )
902 : {
903 93 : spt_shorten_domain_band_restore( bands, band_start, band_end, band_width, org_band_start, org_band_end, org_band_width );
904 : }
905 :
906 102 : mvr2r( y2, y2_org, L_FRAME32k );
907 :
908 : /* Inject noise into components having relatively low pulse energy per band */
909 102 : ni_seed = npulses[0] + npulses[1] + npulses[2] + npulses[3];
910 :
911 58038 : for ( i = 0; i < band_end[bands - 1] + 1; i++ )
912 57936 : y2_ni[i] = y2[i];
913 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 );
914 :
915 102 : if ( st->bwidth == SWB && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
916 : {
917 102 : if ( hqswb_clas == HQ_NORMAL || hqswb_clas == HQ_HARMONIC )
918 : {
919 93 : preset_hq2_swb( hqswb_clas, band_end, &har_bands, p2a_bands, length, bands, &lowlength, &highlength, m );
920 :
921 : /*Gap filling for the core coder*/
922 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 );
923 :
924 93 : post_hq2_swb( m, lowlength, highlength, hqswb_clas, har_bands, bands, p2a_flags, band_start, band_end, y2, npulses );
925 :
926 93 : if ( hqswb_clas == HQ_NORMAL )
927 : {
928 93 : spt_swb_peakpos_tmp_save( y2, bands, band_start, band_end, prev_SWB_peak_pos_tmp );
929 465 : for ( k = 0; k < SPT_SHORTEN_SBNUM; k++ )
930 : {
931 372 : if ( p2a_flags[bands - SPT_SHORTEN_SBNUM + k] == 0 || npulses[bands - SPT_SHORTEN_SBNUM + k] == 0 )
932 : {
933 366 : prev_SWB_peak_pos_tmp[k] = 0;
934 : }
935 : }
936 : }
937 :
938 93 : mvr2r( y2_ni, y2, lowlength );
939 : }
940 : else
941 : {
942 9 : mvr2r( y2_ni, y2, band_end[bands - 1] + 1 ); /* HQ_TRANSIENT */
943 : }
944 : }
945 : else
946 : {
947 0 : mvr2r( y2_ni, y2, band_end[bands - 1] + 1 ); /* NB, WB */
948 : }
949 :
950 :
951 : /* bandwidth switching */
952 102 : if ( !( st->last_inner_frame >= L_FRAME16k && st->bws_cnt > 0 ) )
953 : {
954 102 : k1 = *is_transient ? bands - 2 : bands - 6;
955 102 : st->prev_ener_shb = 0.0f;
956 :
957 678 : for ( i = k1; i < bands; i++ )
958 : {
959 576 : st->prev_ener_shb += Ep_tmp[i] / ( bands - k1 );
960 : }
961 : }
962 :
963 102 : if ( st->last_inner_frame >= L_FRAME32k && st->hBWE_FD != NULL )
964 : {
965 102 : set_f( st->hBWE_FD->prev_SWB_fenv, st->prev_ener_shb, SWB_FENV );
966 : }
967 :
968 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 );
969 :
970 102 : return;
971 : }
972 :
973 :
974 : /*------------------------------------------------------------------------------------
975 : * small_symbol_dec_tran()
976 : *
977 : * Huffman decoding of differential energies
978 : *--------------------------------------------------------------------------------------*/
979 :
980 : /*! r: bits */
981 6 : static int16_t small_symbol_dec_tran(
982 : Decoder_State *st, /* i/o: decoder state structure */
983 : int16_t *qbidx, /* o : output of dequantized differential energy */
984 : const int16_t bands, /* i : number of bands */
985 : const int16_t is_transient /* i : transient flag */
986 : )
987 : {
988 : int16_t i, bits;
989 : int16_t difidx[BANDS_MAX];
990 :
991 : /* Decoding differential energies*/
992 6 : bits = decode_envelope_indices( st, 0, bands, 0, difidx, LOW_RATE_HQ_CORE_TRAN, is_transient );
993 6 : bits += BITS_DE_FCOMP;
994 :
995 : /* counting 1 bit for band_energy_huff_coding_mode */
996 6 : bits += BITS_DE_HMODE;
997 :
998 : /* converting to original values */
999 198 : for ( i = 0; i < bands; i++ )
1000 : {
1001 192 : qbidx[i] = difidx[i] - LRMDCT_BE_OFFSET;
1002 : }
1003 :
1004 6 : return ( bits );
1005 : }
1006 :
1007 :
1008 : /*--------------------------------------------------------------------------
1009 : * small_symbol_dec()
1010 : *
1011 : * Huffman decoding of differential energies (MSB and LSB)
1012 : *--------------------------------------------------------------------------*/
1013 :
1014 : /*! r: bits */
1015 90 : static int16_t small_symbol_dec(
1016 : Decoder_State *st, /* i/o: decoder state structure */
1017 : int16_t *qbidx, /* o : output of dequantized differential energy */
1018 : const int16_t bands, /* i : number of bands */
1019 : const int16_t is_transient )
1020 : {
1021 : int16_t i, LSB, bits;
1022 : int16_t difidx[BANDS_MAX];
1023 :
1024 : /* Decoding MSB bits */
1025 90 : bits = decode_envelope_indices( st, 0, bands, 0, difidx, LOW_RATE_HQ_CORE, is_transient );
1026 90 : bits += BITS_DE_FCOMP;
1027 :
1028 : /* counting 1 bit for band_energy_huff_coding_mode */
1029 90 : bits += BITS_DE_HMODE;
1030 :
1031 : /* Decoding LSB bit packing */
1032 2070 : for ( i = 0; i < bands; ++i )
1033 : {
1034 1980 : LSB = get_next_indice( st, BITS_DE_LSB );
1035 1980 : difidx[i] = ( difidx[i] << 1 ) | LSB;
1036 : }
1037 :
1038 : /* counting bits for LSB */
1039 90 : bits += bands;
1040 :
1041 : /* converting to original values */
1042 1980 : for ( i = 1; i < bands; ++i )
1043 : {
1044 1890 : qbidx[i] = difidx[i] - DE_OFFSET1;
1045 : }
1046 :
1047 90 : qbidx[0] = difidx[0] - DE_OFFSET0;
1048 :
1049 90 : return ( bits );
1050 : }
1051 :
1052 : /*--------------------------------------------------------------------------
1053 : * decode_huff_8s()
1054 : *
1055 : *
1056 : *--------------------------------------------------------------------------*/
1057 :
1058 162 : static int16_t decode_huff_8s(
1059 : Decoder_State *st,
1060 : const int16_t *hufftab,
1061 : int16_t *rbits )
1062 : {
1063 : int16_t bit;
1064 :
1065 558 : while ( *hufftab > 0 )
1066 : {
1067 396 : *rbits += ( *hufftab & 0xf );
1068 396 : bit = get_next_indice( st, *hufftab & 0xf );
1069 396 : hufftab += ( *hufftab >> 4 ) + bit;
1070 : }
1071 :
1072 162 : return ( -*hufftab );
1073 : }
1074 :
1075 :
1076 : /*--------------------------------------------------------------------------
1077 : * large_symbol_dec()
1078 : *
1079 : *
1080 : *--------------------------------------------------------------------------*/
1081 :
1082 : /*! r: bits */
1083 6 : static int16_t large_symbol_dec(
1084 : Decoder_State *st, /* i/o: decoder state structure */
1085 : int16_t *qbidx, /* o : output of dequantized differential energy */
1086 : const int16_t bands /* i : number of bands */
1087 : )
1088 : {
1089 : int16_t i, bits;
1090 : int16_t LSB[BANDS_MAX];
1091 : int16_t basic_shift, cntbits, ns2mode;
1092 : int16_t pos_outlyer;
1093 : int16_t ns2mode0, ns2mode1;
1094 :
1095 6 : cntbits = BITS_DE_8SMODE;
1096 6 : ns2mode = get_next_indice( st, BITS_DE_8SMODE );
1097 :
1098 6 : if ( ns2mode == 0 )
1099 : {
1100 3 : ns2mode0 = get_next_indice( st, BITS_DE_8SMODE_N0 );
1101 3 : ns2mode1 = get_next_indice( st, BITS_DE_8SMODE_N1 );
1102 3 : cntbits += BITS_DE_8SMODE_N0 + BITS_DE_8SMODE_N1;
1103 :
1104 3 : if ( ns2mode0 == 0 )
1105 : {
1106 3 : if ( ns2mode1 == 1 )
1107 : {
1108 0 : pos_outlyer = get_next_indice( st, BITS_DE_8SPOS );
1109 0 : cntbits += BITS_DE_8SPOS;
1110 0 : qbidx[pos_outlyer] = get_next_indice( st, BITS_ABS_ENG ) - ABS_ENG_OFFSET;
1111 0 : cntbits += BITS_ABS_ENG;
1112 : }
1113 : else
1114 : {
1115 3 : pos_outlyer = -1;
1116 : }
1117 :
1118 3 : for ( i = 0; i < pos_outlyer; ++i )
1119 : {
1120 0 : bits = 0;
1121 0 : qbidx[i] = decode_huff_8s( st, hestable, &bits ) - 4;
1122 0 : cntbits += bits;
1123 : }
1124 :
1125 99 : for ( i = pos_outlyer + 1; i < bands; ++i )
1126 : {
1127 96 : bits = 0;
1128 96 : qbidx[i] = decode_huff_8s( st, hestable, &bits ) - 4;
1129 96 : cntbits += bits;
1130 : }
1131 : }
1132 : else
1133 : {
1134 0 : if ( ns2mode1 == 1 )
1135 : {
1136 0 : pos_outlyer = get_next_indice( st, BITS_DE_8SPOS );
1137 0 : cntbits += BITS_DE_8SPOS;
1138 0 : qbidx[0] = get_next_indice( st, BITS_ABS_ENG ) - ABS_ENG_OFFSET;
1139 0 : cntbits += BITS_ABS_ENG;
1140 0 : qbidx[pos_outlyer] = get_next_indice( st, BITS_ABS_ENG ) - ABS_ENG_OFFSET;
1141 0 : cntbits += BITS_ABS_ENG;
1142 : }
1143 : else
1144 : {
1145 0 : pos_outlyer = 0;
1146 0 : qbidx[0] = get_next_indice( st, BITS_ABS_ENG ) - ABS_ENG_OFFSET;
1147 0 : cntbits += BITS_ABS_ENG;
1148 : }
1149 :
1150 0 : for ( i = 1; i < pos_outlyer; ++i )
1151 : {
1152 0 : bits = 0;
1153 0 : qbidx[i] = decode_huff_8s( st, hestable, &bits ) - 4;
1154 0 : cntbits += bits;
1155 : }
1156 :
1157 0 : for ( i = pos_outlyer + 1; i < bands; ++i )
1158 : {
1159 0 : bits = 0;
1160 0 : qbidx[i] = ( decode_huff_8s( st, hestable, &bits ) - 4 );
1161 0 : cntbits += bits;
1162 : }
1163 : }
1164 : }
1165 : else
1166 : {
1167 3 : basic_shift = get_next_indice( st, BITS_MAX_DEPTH );
1168 3 : cntbits += BITS_MAX_DEPTH;
1169 :
1170 69 : for ( i = 0; i < bands; ++i )
1171 : {
1172 66 : bits = 0;
1173 66 : qbidx[i] = decode_huff_8s( st, hestable, &bits ) - 4;
1174 66 : qbidx[i] = qbidx[i] << basic_shift;
1175 66 : cntbits += bits;
1176 : }
1177 :
1178 69 : for ( i = 0; i < bands; ++i )
1179 : {
1180 66 : LSB[0] = get_next_indice( st, basic_shift );
1181 66 : qbidx[i] += LSB[0];
1182 66 : cntbits += basic_shift;
1183 : }
1184 : }
1185 :
1186 6 : return cntbits; /* xx bits for diff. energies + 1 bit for LC coding mode */
1187 : }
1188 :
1189 :
1190 : /*--------------------------------------------------------------------------*
1191 : * band_energy_dequant()
1192 : *
1193 : *
1194 : *--------------------------------------------------------------------------*/
1195 :
1196 102 : static float band_energy_dequant(
1197 : Decoder_State *st, /* i/o: decoder state structure */
1198 : float band_energy[],
1199 : const int16_t bands,
1200 : const Word32 L_qint, /* Q29 */
1201 : const Word16 eref_fx, /* Q10 */
1202 : const int16_t is_transient )
1203 : {
1204 : int16_t k;
1205 : int16_t deng_cmode;
1206 : int16_t bq0;
1207 : int16_t bq1[BANDS_MAX], bq2[BANDS_MAX];
1208 : float deng_bits;
1209 : Word32 L_band_energy[BANDS_MAX]; /* SWB_BWE_LR_Qbe */
1210 : Word16 exp_normd;
1211 : Word16 rev_qint_fx;
1212 : Word16 Qrev_qint;
1213 :
1214 : /* parsing energy difference coding mode */
1215 102 : deng_cmode = get_next_indice( st, BITS_DE_CMODE );
1216 :
1217 102 : if ( deng_cmode == 0 )
1218 : {
1219 6 : deng_bits = (float) large_symbol_dec( st, bq2, bands );
1220 :
1221 : /* counting 1 bit for deng coding mode */
1222 6 : deng_bits += BITS_DE_CMODE;
1223 : }
1224 : else
1225 : {
1226 96 : if ( is_transient )
1227 : {
1228 6 : deng_bits = (float) small_symbol_dec_tran( st, bq2, bands, is_transient );
1229 : }
1230 : else
1231 : {
1232 90 : deng_bits = (float) small_symbol_dec( st, bq2, bands, is_transient );
1233 : }
1234 :
1235 : /* counting 1 bit for deng coding mode */
1236 96 : deng_bits += BITS_DE_CMODE;
1237 : }
1238 :
1239 : #define WMC_TOOL_SKIP
1240 : /* Get the reference energy */
1241 102 : exp_normd = norm_l( L_qint );
1242 102 : rev_qint_fx = div_s( 0x4000, round_fx( L_shl( L_qint, exp_normd ) ) ); /* Q14-(29+exp_normd-16)+15 */
1243 102 : Qrev_qint = sub( 14 - ( 29 - 16 ) + 15, exp_normd );
1244 :
1245 102 : bq0 = (int16_t) round_fx( L_shl( L_mult( eref_fx, rev_qint_fx ), sub( 5, Qrev_qint ) ) ); /* 16-(10+Qrev_qint+1) */
1246 : #undef WMC_TOOL_SKIP
1247 :
1248 : /* Reconstruct quantized spectrum */
1249 102 : bq1[0] = bq2[0] + bq0;
1250 2334 : for ( k = 1; k < bands; k++ )
1251 : {
1252 2232 : bq1[k] = bq2[k] + bq1[k - 1];
1253 : }
1254 :
1255 2436 : for ( k = 0; k < bands; k++ )
1256 : {
1257 : #define WMC_TOOL_SKIP
1258 2334 : L_band_energy[k] = L_mls( L_qint, bq1[k] );
1259 2334 : move32(); /* 29+0-15 -> Qbe(Q14) */
1260 2334 : band_energy[k] = (float) ( L_band_energy[k] / pow( 2.0f, SWB_BWE_LR_Qbe ) );
1261 : #undef WMC_TOOL_SKIP
1262 : }
1263 :
1264 102 : if ( is_transient )
1265 : {
1266 9 : reverse_transient_frame_energies( band_energy, bands );
1267 : }
1268 :
1269 102 : return ( deng_bits );
1270 : }
1271 :
1272 :
1273 : /*--------------------------------------------------------------------------*
1274 : * p2a_threshold_dequant()
1275 : *
1276 : *
1277 : *--------------------------------------------------------------------------*/
1278 :
1279 102 : static int16_t p2a_threshold_dequant(
1280 : Decoder_State *st, /* i/o: decoder state structure */
1281 : int16_t *p2a_flags,
1282 : const int16_t bands,
1283 : const int16_t p2a_bands )
1284 : {
1285 : int16_t j, k;
1286 :
1287 1971 : for ( k = 0; k < bands - p2a_bands; k++ )
1288 : {
1289 1869 : p2a_flags[k] = 1;
1290 : }
1291 :
1292 102 : j = 0;
1293 567 : for ( k = bands - p2a_bands; k < bands; k++ )
1294 : {
1295 465 : p2a_flags[k] = get_next_indice( st, 1 );
1296 465 : j++;
1297 : }
1298 :
1299 102 : return ( j );
1300 : }
1301 :
1302 :
1303 : /*--------------------------------------------------------------------------*
1304 : * mdct_spectrum_fine_gain_dec()
1305 : *
1306 : *
1307 : *--------------------------------------------------------------------------*/
1308 :
1309 102 : static void mdct_spectrum_fine_gain_dec(
1310 : Decoder_State *st, /* i/o: decoder state structure */
1311 : float y2[],
1312 : const int16_t band_start[],
1313 : const int16_t band_end[],
1314 : const int16_t k_sort[],
1315 : const int16_t bands,
1316 : const Word32 L_qint,
1317 : const int16_t Ngq,
1318 : const int16_t gqlevs,
1319 : const int16_t gqbits )
1320 : {
1321 : int16_t i, k, imin;
1322 : float gamma;
1323 : float gain_table[MAX_GQLEVS];
1324 : Word16 exp_normn, exp_normd;
1325 : Word16 delta_fx, Qdelta;
1326 : Word32 L_delta, L_q;
1327 : Word32 L_temp;
1328 : Word16 gain_table_fx[MAX_GQLEVS];
1329 : Word16 Qgt;
1330 : Word16 temp_lo_fx, temp_hi_fx;
1331 :
1332 : #define WMC_TOOL_SKIP
1333 : /* Fine gain quantization on only the most significant energy bands */
1334 : /*delta = qint / gqlevs; */
1335 102 : exp_normn = norm_l( L_qint );
1336 102 : exp_normn = sub( exp_normn, 1 );
1337 102 : exp_normd = norm_s( gqlevs );
1338 102 : delta_fx = div_l( L_shl( L_qint, exp_normn ), shl( gqlevs, exp_normd ) );
1339 102 : Qdelta = add( sub( exp_normn, exp_normd ), 28 ); /* 29+exp_normn-(exp_normd)-1; */
1340 102 : L_delta = L_shl( L_deposit_h( delta_fx ), sub( 13, Qdelta ) );
1341 : /*q = (-qint + delta) / 2.0f; */
1342 102 : L_q = L_shr( L_sub( L_delta, L_qint ), 1 );
1343 :
1344 306 : FOR( i = 0; i < gqlevs; i++ )
1345 : {
1346 : /*gain_table[i] = (float) pow (2.0f, q * 0.5f); */
1347 204 : L_temp = L_shr( L_shr( L_q, 1 ), sub( 29, 16 ) );
1348 204 : temp_lo_fx = L_Extract_lc( L_temp, &temp_hi_fx );
1349 204 : Qgt = sub( 14, temp_hi_fx );
1350 204 : gain_table_fx[i] = extract_l( Pow2( 14, temp_lo_fx ) ); /* Qgt */
1351 :
1352 : /*q += delta; */
1353 204 : L_q = L_add( L_q, L_delta );
1354 204 : gain_table_fx[i] = shl( gain_table_fx[i], sub( 14, Qgt ) ); /* Qgt -> Q14 */
1355 204 : gain_table[i] = (float) ( gain_table_fx[i] / pow( 2.0f, 14 ) );
1356 : }
1357 : #undef WMC_TOOL_SKIP
1358 :
1359 510 : for ( k = bands - Ngq; k < bands; k++ )
1360 : {
1361 408 : imin = get_next_indice( st, gqbits );
1362 408 : gamma = gain_table[imin];
1363 :
1364 7386 : for ( i = band_start[k_sort[k]]; i <= band_end[k_sort[k]]; i++ )
1365 : {
1366 6978 : y2[i] *= gamma;
1367 : }
1368 : }
1369 :
1370 102 : return;
1371 : }
1372 :
1373 : /*--------------------------------------------------------------------------*
1374 : * spt_shorten_domain_set_dec()
1375 : *
1376 : * update the shorten band information based on p2a analysis
1377 : *--------------------------------------------------------------------------*/
1378 :
1379 93 : static void spt_shorten_domain_set_dec(
1380 : Decoder_State *st, /* i : decoder state structure */
1381 : const int16_t p2a_flags[], /* i : p2a anlysis information */
1382 : const int16_t new_band_start[], /* i : new band start position */
1383 : const int16_t new_band_end[], /* i : new band end position */
1384 : const int16_t new_band_width[], /* i : new subband band width */
1385 : const int16_t bands, /* i : total number of subbands */
1386 : int16_t band_start[], /* o : band start position */
1387 : int16_t band_end[], /* o : band end position */
1388 : int16_t band_width[], /* o : sub band band width */
1389 : int16_t *bit_budget /* i/o: bit budget */
1390 : )
1391 : {
1392 : int16_t j, k;
1393 : int16_t spt_shorten_flag[SPT_SHORTEN_SBNUM];
1394 :
1395 93 : j = 0;
1396 465 : for ( k = bands - SPT_SHORTEN_SBNUM; k < bands; k++ )
1397 : {
1398 372 : spt_shorten_flag[j] = 0;
1399 372 : if ( p2a_flags[k] == 1 )
1400 : {
1401 12 : spt_shorten_flag[j] = get_next_indice( st, 1 );
1402 12 : *bit_budget -= 1;
1403 12 : if ( spt_shorten_flag[j] == 1 )
1404 : {
1405 0 : band_start[k] = new_band_start[j];
1406 0 : band_end[k] = new_band_end[j];
1407 0 : band_width[k] = new_band_width[j];
1408 : }
1409 : }
1410 :
1411 372 : j++;
1412 : }
1413 :
1414 93 : return;
1415 : }
|