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_enc.h"
45 : #include "rom_com.h"
46 : #include "prot.h"
47 : #include "stl.h"
48 : #include "basop_util.h"
49 : #include "wmc_auto.h"
50 :
51 :
52 : /*--------------------------------------------------------------------------*
53 : * Local function prototypes
54 : *--------------------------------------------------------------------------*/
55 :
56 : static int16_t small_symbol_enc( BSTR_ENC_HANDLE hBstr, const int16_t *qbidx, const int16_t bands, int16_t *hLCmode, const int16_t flag_pack, const int16_t is_transient );
57 :
58 : static int16_t small_symbol_enc_tran( BSTR_ENC_HANDLE hBstr, const int16_t *qbidx, const int16_t bands, int16_t *hLCmode, const int16_t flag_pack, const int16_t is_transient );
59 :
60 : static float band_energy_quant( BSTR_ENC_HANDLE hBstr, const float *t_audio, const int16_t band_start[], const int16_t band_end[], float band_energy[], const int16_t bands, const Word32 L_qint, const Word16 eref_fx, const int16_t is_transient );
61 :
62 : static int16_t p2a_threshold_quant( BSTR_ENC_HANDLE hBstr, const float *t_audio, const int16_t band_start[], const int16_t band_end[], const int16_t band_width[], const int16_t bands, const int16_t p2a_bands, const float p2a_th, int16_t *p2a_flags );
63 :
64 : static void mdct_spectrum_fine_gain_enc( BSTR_ENC_HANDLE hBstr, const float ybuf[], 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 );
65 :
66 :
67 : /*--------------------------------------------------------------------------*
68 : * spt_shorten_domain_set()
69 : *
70 : * Track the spectral peak based on peak -avg analysis
71 : *--------------------------------------------------------------------------*/
72 :
73 31 : static void spt_shorten_domain_set(
74 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
75 : HQ_ENC_HANDLE hHQ_core, /* i/o: HQ core encoder handle */
76 : const float t_audio[], /* i : input spectrum */
77 : const int16_t p2a_flags[], /* i : p2a anlysis information */
78 : const int16_t new_band_start[], /* i : new band start position */
79 : const int16_t new_band_end[], /* i : new band end position */
80 : const int16_t new_band_width[], /* i : new subband band width */
81 : const int16_t bands, /* i : total number of subbands */
82 : int16_t band_start[], /* i/o: band start position */
83 : int16_t band_end[], /* i/o: band end position */
84 : int16_t band_width[], /* i : sub band band width */
85 : int16_t *bit_budget /* i/o: bit budget */
86 : )
87 : {
88 : int16_t i, j, k;
89 : int16_t kpos;
90 : float max_y2;
91 : int16_t max_y2_pos;
92 : int16_t spt_shorten_flag[SPT_SHORTEN_SBNUM];
93 :
94 31 : kpos = 0;
95 31 : j = 0;
96 155 : for ( k = bands - SPT_SHORTEN_SBNUM; k < bands; k++ )
97 : {
98 124 : if ( p2a_flags[k] == 1 )
99 : {
100 4 : spt_shorten_flag[j] = 0;
101 4 : if ( hHQ_core->prev_SWB_peak_pos[kpos] != 0 )
102 : {
103 0 : max_y2 = 0.0f;
104 0 : max_y2_pos = 0;
105 0 : for ( i = band_start[k]; i <= band_end[k]; i++ )
106 : {
107 0 : if ( max_y2 < fabs( t_audio[i] ) )
108 : {
109 0 : max_y2 = (float) fabs( t_audio[i] );
110 0 : max_y2_pos = i;
111 : }
112 : }
113 0 : if ( max_y2_pos >= new_band_start[j] && max_y2_pos <= new_band_end[j] )
114 : {
115 0 : band_start[k] = new_band_start[j];
116 0 : band_end[k] = new_band_end[j];
117 0 : band_width[k] = new_band_width[j];
118 0 : spt_shorten_flag[j] = 1;
119 : }
120 : }
121 4 : push_indice( hBstr, IND_HQ2_SPT_SHORTEN, spt_shorten_flag[j], 1 );
122 4 : *bit_budget -= 1;
123 : }
124 :
125 124 : kpos++;
126 124 : j++;
127 : }
128 :
129 31 : return;
130 : }
131 :
132 : /*--------------------------------------------------------------------------*
133 : * hq_lr_enc()
134 : *
135 : * HQ low rate encoding routine
136 : *--------------------------------------------------------------------------*/
137 :
138 34 : void hq_lr_enc(
139 : Encoder_State *st, /* i/o: encoder state structure */
140 : float t_audio[], /* i/o: transform-domain coefs. */
141 : const int16_t inner_frame, /* i : inner frame length */
142 : int16_t *num_bits, /* i/o: number of available bits */
143 : const int16_t is_transient /* i : transient flag */
144 : )
145 : {
146 : int16_t i, k1, k2;
147 : int16_t bit_budget, pbits;
148 : int16_t bands, length, ni_seed, gqlevs, gqbits, Ngq, p2a_bands;
149 : int16_t p2a_flags[BANDS_MAX];
150 : int16_t band_start[BANDS_MAX], band_end[BANDS_MAX], band_width[BANDS_MAX];
151 : float band_energy[BANDS_MAX], Rk[BANDS_MAX];
152 : Word32 Rk_fx[BANDS_MAX];
153 : float ebits;
154 : float p2a_th, ni_coef, ni_pd_th, pd_thresh, ld_slope;
155 : Word32 L_qint; /* Q29 */
156 : Word16 eref_fx; /* Q10 */
157 : Word16 bit_alloc_weight_fx; /* Q13 */
158 : int16_t k_sort[BANDS_MAX];
159 : int16_t npulses[BANDS_MAX];
160 : int32_t inp_vector[L_FRAME48k];
161 : float y2[L_FRAME48k];
162 : float y2_ni[L_FRAME48k];
163 : int16_t hqswb_clas;
164 : int16_t lowlength;
165 : int16_t highlength;
166 : float m[L_FRAME32k];
167 : int16_t har_bands;
168 34 : float Ep[BANDS_MAX], enerH = 0.0f, enerL = 0.0f;
169 34 : int16_t lowband, highband, bw_low = 0, bw_high = 20;
170 : float band_energy_tmp[BANDS_MAX];
171 : int32_t bwe_br;
172 : int16_t trans_bit, p2a_flags_tmp[BANDS_MAX];
173 34 : int16_t adjustFlag = 0;
174 : int16_t prev_SWB_peak_pos_tmp[SPT_SHORTEN_SBNUM];
175 : int16_t k, j;
176 : int16_t flag_spt;
177 : int16_t org_band_start[SPT_SHORTEN_SBNUM];
178 : int16_t org_band_end[SPT_SHORTEN_SBNUM];
179 : int16_t org_band_width[SPT_SHORTEN_SBNUM];
180 : int16_t new_band_start[SPT_SHORTEN_SBNUM];
181 : int16_t new_band_end[SPT_SHORTEN_SBNUM];
182 : int16_t new_band_width[SPT_SHORTEN_SBNUM];
183 34 : int16_t bws_cnt = 0;
184 : Word32 L_tmp, L_tmp2, L_tmp3;
185 : Word16 exp, tmp, exp2, tmp1, tmp2, tmp3, alpha_fx, frac1;
186 : Word32 enerH_fx;
187 : Word32 enerL_fx;
188 : Word32 Ep_fx[BANDS_MAX];
189 : Word32 Ep_avrg_fx, Ep_vari_fx;
190 : Word32 Ep_avrgL_fx;
191 : Word32 Ep_peak_fx;
192 : Word32 Ep_tmp_fx[BANDS_MAX];
193 : Word16 gama_fx; /*Q15 0.85f; */
194 : Word16 beta_fx; /*Q14 1.05f; */
195 : Word32 L_band_energy[BANDS_MAX], L_band_energy_tmp[BANDS_MAX];
196 : UWord16 lo;
197 : Word16 Q_band_energy;
198 : #ifdef BASOP_NOGLOB
199 : Flag Overflow;
200 : #endif /* BASOP_NOGLOB */
201 :
202 34 : BSTR_ENC_HANDLE hBstr = st->hBstr;
203 34 : HQ_ENC_HANDLE hHQ_core = st->hHQ_core;
204 :
205 34 : set_f( y2, 0.0f, L_FRAME48k );
206 34 : set_l( inp_vector, 0, inner_frame );
207 34 : flag_spt = 0;
208 34 : set_s( prev_SWB_peak_pos_tmp, 0, SPT_SHORTEN_SBNUM );
209 :
210 34 : bwe_br = st->core_brate;
211 34 : hqswb_clas = HQ_NORMAL;
212 34 : if ( st->bwidth == SWB && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
213 : {
214 34 : if ( is_transient == 1 )
215 : {
216 3 : hqswb_clas = HQ_TRANSIENT;
217 : }
218 : else
219 : {
220 : /* classification of HQ_HARMONIC and HQ_NORMAL frames for SWB BWE */
221 31 : hqswb_clas = peak_avrg_ratio( st->total_brate, t_audio, NUMC_N, &hHQ_core->mode_count, &hHQ_core->mode_count1 );
222 : }
223 :
224 : /* write the classification information into the bitstream */
225 34 : push_indice( hBstr, IND_HQ2_SWB_CLAS, hqswb_clas, 2 );
226 34 : ( *num_bits ) -= 2;
227 :
228 34 : if ( hqswb_clas == HQ_NORMAL )
229 : {
230 31 : flag_spt = 1;
231 : }
232 : }
233 : else
234 : {
235 : /* write the transient bit into the bitstream */
236 0 : push_indice( hBstr, IND_HQ2_SWB_CLAS, is_transient, 1 );
237 :
238 : /* subtract one bit for the transient flag */
239 0 : ( *num_bits )--;
240 : }
241 :
242 : /* Configure encoder for different bandwidths, bitrates, etc. */
243 34 : 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 );
244 :
245 34 : highlength = band_end[bands - 1];
246 34 : har_bands = bands;
247 :
248 34 : if ( st->bwidth == SWB && is_transient == 0 && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
249 : {
250 : /* reserve bits for HQ_NORMAL and HQ_HARMONIC modes */
251 31 : if ( hqswb_clas == HQ_NORMAL || hqswb_clas == HQ_HARMONIC )
252 : {
253 31 : ( *num_bits ) -= get_usebit_npswb( hqswb_clas );
254 : }
255 : }
256 :
257 34 : if ( ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) && st->bwidth == SWB )
258 : {
259 34 : if ( hHQ_core->prev_hqswb_clas != HQ_NORMAL )
260 : {
261 3 : j = 0;
262 15 : for ( k = bands - SPT_SHORTEN_SBNUM; k < bands; k++ )
263 : {
264 12 : hHQ_core->prev_SWB_peak_pos[j] = 0;
265 12 : j++;
266 : }
267 : }
268 : }
269 :
270 : /* Check if input frame is larger than coded bandwidth */
271 34 : if ( inner_frame > length && is_transient )
272 : {
273 : /* If so, collapse transient frame (4 short transforms) to remove uncoded coefficients */
274 12 : for ( i = 1; i < NUM_TIME_SWITCHING_BLOCKS; i++ )
275 : {
276 9 : k1 = i * length / NUM_TIME_SWITCHING_BLOCKS;
277 9 : k2 = i * inner_frame / NUM_TIME_SWITCHING_BLOCKS;
278 :
279 9 : mvr2r( &t_audio[k2], &t_audio[k1], length / NUM_TIME_SWITCHING_BLOCKS );
280 : }
281 : }
282 :
283 : /* Spectral energy calculation/quantization */
284 34 : ebits = band_energy_quant( hBstr, t_audio, band_start, band_end, band_energy, bands, L_qint, eref_fx, is_transient );
285 :
286 812 : for ( i = 0; i < bands; i++ )
287 : {
288 778 : L_band_energy[i] = (Word32) ( band_energy[i] * pow( 2.0f, SWB_BWE_LR_Qbe ) );
289 : }
290 :
291 : /* First pass bit budget for TCQ of spectral band information */
292 34 : gqbits = (int16_t) log2_f( (float) gqlevs );
293 34 : bit_budget = ( *num_bits ) - (int16_t) ceil( ebits ) - Ngq * gqbits;
294 :
295 34 : pbits = 0;
296 34 : if ( st->bwidth == SWB && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
297 : {
298 34 : if ( hqswb_clas == HQ_HARMONIC )
299 : {
300 0 : set_s( p2a_flags, 1, har_bands );
301 : }
302 : else
303 : {
304 : /* High band tonality detector based on per band peak-to-average ratio */
305 34 : pbits = p2a_threshold_quant( hBstr, t_audio, band_start, band_end, band_width, bands, p2a_bands, p2a_th, p2a_flags );
306 34 : bit_budget -= pbits;
307 :
308 34 : if ( hqswb_clas == HQ_NORMAL )
309 : {
310 31 : return_bits_normal2( &bit_budget, p2a_flags, bands, bits_lagIndices_modeNormal );
311 : }
312 : }
313 : }
314 : else
315 : {
316 : /* High band tonality detector based on per band peak-to-average ratio */
317 0 : pbits = p2a_threshold_quant( hBstr, t_audio, band_start, band_end, band_width, bands, p2a_bands, p2a_th, p2a_flags );
318 0 : bit_budget -= pbits;
319 : }
320 :
321 34 : if ( flag_spt == 1 )
322 : {
323 : /* initialize the desired parameters for SPT */
324 31 : spt_shorten_domain_band_save( bands, band_start, band_end, band_width, org_band_start, org_band_end, org_band_width );
325 31 : 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 );
326 31 : spt_shorten_domain_set( hBstr, hHQ_core, t_audio, p2a_flags, new_band_start, new_band_end, new_band_width, bands, band_start, band_end, band_width, &bit_budget );
327 : }
328 :
329 : #define WMC_TOOL_SKIP
330 : /* Estimate number of bits per band */
331 34 : Q_band_energy = SWB_BWE_LR_Qbe;
332 812 : FOR( i = 0; i < bands; i++ )
333 : {
334 778 : L_tmp = L_shl( L_band_energy[i], sub( 16, Q_band_energy ) ); /*Q16 */
335 :
336 778 : frac1 = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */
337 778 : L_tmp = Pow2( 30, frac1 );
338 778 : exp = sub( exp, 30 );
339 778 : Ep_fx[i] = L_shl( L_tmp, sub( exp, 6 ) ); /* Q -6 */
340 778 : Ep[i] = (float) ( Ep_fx[i] / pow( 2.0, -6 ) );
341 : }
342 :
343 812 : FOR( i = 0; i < bands; i++ )
344 : {
345 778 : L_tmp2 = Ep_fx[i];
346 778 : L_tmp = L_max( 1, L_tmp2 );
347 778 : exp = norm_l( L_tmp );
348 778 : tmp = extract_h( L_shl( L_tmp, exp ) );
349 :
350 778 : L_tmp3 = (Word32) band_width[i];
351 778 : exp2 = norm_l( L_tmp3 );
352 778 : tmp2 = extract_h( L_shl( L_tmp3, exp2 ) );
353 :
354 778 : exp2 = sub( exp, exp2 ); /* Denormalize and substract */
355 :
356 778 : tmp3 = sub( tmp2, tmp );
357 778 : IF( tmp3 > 0 )
358 : {
359 298 : tmp2 = shr( tmp2, 1 );
360 : }
361 778 : IF( tmp3 > 0 )
362 : {
363 298 : exp2 = add( exp2, 1 );
364 : }
365 778 : tmp = div_s( tmp2, tmp );
366 778 : L_tmp = L_deposit_h( tmp );
367 778 : L_tmp = Isqrt_lc1( L_tmp, &exp2 );
368 778 : move32(); /*Q(31-exp2) */
369 778 : Ep_tmp_fx[i] = L_shr( L_tmp, sub( 15, exp2 ) ); /*Q13 */
370 : }
371 : #undef WMC_TOOL_SKIP
372 :
373 34 : if ( is_transient == 0 && inner_frame == L_FRAME8k && st->core_brate <= ACELP_13k20 )
374 : {
375 : #define WMC_TOOL_SKIP
376 0 : lowband = 6;
377 0 : move16();
378 0 : trans_bit = 2;
379 0 : move16();
380 0 : bit_budget = sub( bit_budget, trans_bit );
381 0 : gama_fx = 27852; /*Q15 0.85f; */
382 0 : beta_fx = 17203;
383 0 : move16(); /*Q14 1.05f; */
384 0 : set_s( &p2a_flags_tmp[bands - trans_bit], 0, 2 );
385 :
386 0 : IF( st->core_brate == ACELP_13k20 )
387 : {
388 0 : beta_fx = 13107;
389 0 : move16(); /*14 1.25f; */
390 0 : gama_fx = 31130;
391 0 : move16(); /*0.95f; */
392 0 : mvs2s( &p2a_flags[sub( bands, trans_bit )], &p2a_flags_tmp[sub( bands, trans_bit )], trans_bit );
393 : }
394 :
395 : /* calculate the the low band/high band energy and the variance/avrage of the envelopes */
396 0 : Ep_vari_fx = 0;
397 0 : move32();
398 0 : Ep_avrg_fx = 0;
399 0 : move32();
400 0 : Ep_avrgL_fx = 0;
401 0 : move32();
402 0 : Ep_peak_fx = 0;
403 0 : move32();
404 0 : FOR( i = 0; i < bands; i++ )
405 : {
406 0 : IF( sub( i, lowband ) >= 0 )
407 : {
408 0 : Ep_vari_fx = L_add( Ep_vari_fx, L_abs( L_sub( Ep_tmp_fx[i], Ep_tmp_fx[sub( i, 1 )] ) ) ); /*Q15 */
409 0 : Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
410 : }
411 : ELSE
412 : {
413 : #ifdef BASOP_NOGLOB
414 0 : Ep_avrgL_fx = L_add_o( Ep_avrgL_fx, Ep_tmp_fx[i], &Overflow ); /*Q15 */
415 : #else
416 : Ep_avrgL_fx = L_add( Ep_avrgL_fx, Ep_tmp_fx[i] ); /*Q15 */
417 : #endif
418 0 : IF( L_sub( Ep_tmp_fx[i], Ep_peak_fx ) > 0 )
419 : {
420 0 : Ep_peak_fx = Ep_tmp_fx[i];
421 0 : move32(); /*Q15 */
422 : }
423 : }
424 : }
425 : /* modify the last p2a_bands subbands band_energies */
426 0 : k = bands;
427 0 : mvl2l( L_band_energy, L_band_energy_tmp, k ); /*Q_band_energy */
428 0 : Mpy_32_16_ss( Ep_peak_fx, 24576, &L_tmp, &lo );
429 0 : Mpy_32_16_ss( Ep_peak_fx, shl( sub( bands, lowband ), 9 ), &L_tmp2, &lo );
430 0 : Mpy_32_16_ss( Ep_avrg_fx, 1126, &L_tmp3, &lo );
431 :
432 0 : IF( ( ( L_sub( L_tmp, L_shr( Ep_avrgL_fx, 1 ) ) < 0 && st->core_brate == ACELP_13k20 ) || st->core_brate < ACELP_13k20 ) &&
433 : L_sub( L_tmp2, L_tmp3 ) < 0 && L_sub( L_tmp2, L_shr( Ep_avrg_fx, 7 ) ) > 0 )
434 : {
435 0 : FOR( i = lowband; i < bands; i++ )
436 : {
437 0 : Mpy_32_16_ss( Ep_avrg_fx, 24576, &L_tmp, &lo );
438 0 : IF( L_sub( L_shr( Ep_tmp_fx[i], 1 ), L_tmp ) < 0 )
439 : {
440 0 : Mpy_32_16_ss( Ep_peak_fx, sub( bands, lowband ), &L_tmp, &lo );
441 0 : tmp = extract_h( L_shl( L_tmp, 14 ) ); /*Q-4 */
442 0 : IF( tmp != 0 )
443 : {
444 0 : exp = norm_s( tmp );
445 0 : tmp = shl( tmp, exp ); /*Q(exp) */
446 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
447 0 : exp = sub( 29, exp );
448 : }
449 : ELSE
450 : {
451 : /*when the divisor is zero, happens rarely*/
452 0 : tmp = 0x7fff;
453 0 : move16();
454 0 : exp = 0;
455 0 : move16();
456 : }
457 0 : Mpy_32_16_ss( Ep_avrg_fx, tmp, &L_tmp, &lo );
458 0 : L_tmp = L_shl( L_tmp, sub( 13, exp ) ); /*Q(13+exp-15 +13-exp +4 = 15) */
459 0 : L_tmp2 = L_add( L_tmp, 13107 ); /*15 */
460 0 : tmp2 = extract_l( L_min( L_max( L_tmp2, 16384 ), gama_fx ) ); /*15 = 15 */
461 0 : Mpy_32_16_ss( L_band_energy_tmp[i], tmp2, &L_band_energy_tmp[i], &lo );
462 : }
463 : }
464 : }
465 : ELSE
466 : {
467 0 : j = 0;
468 0 : FOR( i = sub( bands, trans_bit ); i < bands; i++ )
469 : {
470 0 : alpha_fx = 16384;
471 0 : move16(); /*Q14 */
472 0 : IF( sub( p2a_flags_tmp[i], 1 ) == 0 )
473 : {
474 0 : Mpy_32_16_ss( Ep_tmp_fx[i], sub( bands, lowband ), &L_tmp, &lo );
475 0 : tmp = extract_h( L_shl( L_tmp, 14 ) ); /*Q-4 */
476 0 : IF( tmp != 0 )
477 : {
478 0 : exp = norm_s( tmp );
479 0 : tmp = shl( tmp, exp ); /*Q(exp) */
480 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
481 0 : exp = sub( 29, exp );
482 : }
483 : ELSE
484 : {
485 : /*when the divisor is zero, happens rarely*/
486 0 : tmp = 0x7fff;
487 0 : move16();
488 0 : exp = 0;
489 0 : move16();
490 : }
491 0 : Mpy_32_16_ss( Ep_vari_fx, 3277, &L_tmp, &lo );
492 0 : Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
493 0 : L_tmp = L_shl( L_tmp, sub( 12, exp ) ); /*Q(13+exp-15 +12-exp +4 = 14) */
494 :
495 0 : tmp2 = extract_h( Ep_avrg_fx ); /*Q13-16=-3 */
496 0 : IF( tmp2 != 0 )
497 : {
498 0 : exp = norm_s( tmp2 );
499 0 : tmp2 = shl( tmp2, exp ); /*Q(exp) */
500 0 : tmp2 = div_s( 16384, tmp2 ); /*Q(15+14-exp=29-exp) */
501 0 : exp = sub( 29, exp );
502 : }
503 : ELSE
504 : {
505 0 : tmp2 = 0x7fff;
506 0 : move16();
507 0 : exp = 0;
508 0 : move16();
509 : }
510 0 : Mpy_32_16_ss( Ep_vari_fx, 6554, &L_tmp2, &lo );
511 0 : Mpy_32_16_ss( L_tmp2, tmp2, &L_tmp2, &lo );
512 0 : L_tmp2 = L_shl( L_tmp2, sub( 13, exp ) ); /*Q(13+exp-15 +13-exp +3 = 14) */
513 0 : L_tmp = L_min( L_tmp, L_tmp2 ); /*14 */
514 0 : tmp = extract_l( L_min( L_tmp, 13107 ) ); /*14 */
515 0 : alpha_fx = add( 16384, tmp );
516 : }
517 0 : IF( sub( hHQ_core->last_bitalloc_max_band[j++], 1 ) == 0 )
518 : {
519 0 : Mpy_32_16_ss( Ep_tmp_fx[i], sub( bands, lowband ), &L_tmp, &lo );
520 0 : tmp = extract_h( L_shl( L_tmp, 14 ) ); /*Q-2 */
521 0 : IF( tmp != 0 )
522 : {
523 0 : exp = norm_s( tmp );
524 0 : tmp = shl( tmp, exp ); /*Q(exp) */
525 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
526 0 : exp = sub( 29, exp );
527 : }
528 : ELSE
529 : {
530 0 : tmp = 0x7fff;
531 0 : move16();
532 0 : exp = 0;
533 0 : move16();
534 : }
535 0 : Mpy_32_16_ss( Ep_avrg_fx, tmp, &L_tmp, &lo );
536 : #ifndef BASOP_NOGLOB
537 : L_tmp = L_shl( L_tmp, sub( 14, exp ) ); /*Q(13+exp-15 +14-exp+2 = 14) */
538 : L_tmp = L_max( L_tmp, 16384 ); /*14 */
539 : tmp = extract_l( L_min( L_tmp, beta_fx ) ); /*14 */
540 : alpha_fx = shl( mult( alpha_fx, tmp ), 1 ); /*14+14-15 +1=14 */
541 : #else /* BASOP_NOGLOB */
542 0 : L_tmp = L_shl_o( L_tmp, sub( 14, exp ), &Overflow ); /*Q(13+exp-15 +14-exp+2 = 14) */
543 0 : L_tmp = L_max( L_tmp, 16384 ); /*14 */
544 0 : tmp = extract_l( L_min( L_tmp, beta_fx ) ); /*14 */
545 0 : alpha_fx = shl( mult( alpha_fx, tmp ), 1 ); /*14+14-15 +1=14 */
546 : #endif /* BASOP_NOGLOB */
547 : }
548 : ELSE
549 : {
550 0 : tmp2 = extract_h( Ep_avrg_fx ); /*13 -16 =-3 */
551 0 : IF( tmp2 != 0 )
552 : {
553 0 : exp = norm_s( tmp2 );
554 0 : tmp2 = shl( tmp2, exp ); /*Q(exp) */
555 0 : tmp2 = div_s( 16384, tmp2 ); /*Q(15+14-exp=29-exp) */
556 0 : exp = sub( 29, exp );
557 : }
558 : ELSE
559 : {
560 : /*when the divisor is zero, happens rarely*/
561 0 : tmp2 = 0x7fff;
562 0 : move16();
563 0 : exp = 0;
564 0 : move16();
565 : }
566 0 : Mpy_32_16_ss( Ep_tmp_fx[i], tmp2, &L_tmp, &lo );
567 0 : L_tmp = L_shl( L_tmp, sub( 19, exp ) ); /*Q(13+exp-15 +19-exp +3 = 20) */
568 0 : Mpy_32_16_ss( L_tmp, shl( sub( bands, lowband ), 9 ), &L_tmp, &lo );
569 0 : L_tmp = L_max( L_tmp, 13926 ); /*14 */
570 0 : tmp2 = extract_l( L_min( L_tmp, 16384 ) ); /*14 */
571 0 : alpha_fx = shl( mult( alpha_fx, tmp2 ), 1 ); /*14+14-15+1 =14 */
572 : }
573 0 : Mpy_32_16_ss( L_band_energy_tmp[i], alpha_fx, &L_tmp, &lo );
574 0 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q(Q_band_energy+14-15 +1= Q_band_energy) */
575 : }
576 : }
577 0 : lowband = 3;
578 0 : move16();
579 0 : Ep_avrg_fx = 0;
580 0 : move32();
581 0 : Ep_avrgL_fx = 0;
582 0 : move32();
583 0 : Ep_peak_fx = 0;
584 0 : move32();
585 0 : FOR( i = 0; i < bands; i++ )
586 : {
587 0 : IF( sub( i, lowband ) >= 0 )
588 : {
589 : #ifdef BASOP_NOGLOB
590 0 : Ep_avrg_fx = L_add_o( Ep_avrg_fx, Ep_tmp_fx[i], &Overflow ); /*Q15 */
591 : #else
592 : Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
593 : #endif
594 : }
595 : ELSE
596 : {
597 0 : Ep_avrgL_fx = L_add( Ep_avrgL_fx, L_shr( Ep_tmp_fx[i], 1 ) ); /*Q12 */
598 0 : IF( L_sub( Ep_tmp_fx[i], Ep_peak_fx ) > 0 )
599 : {
600 0 : Ep_peak_fx = Ep_tmp_fx[i];
601 0 : move32(); /*Q13 */
602 : }
603 : }
604 : }
605 0 : Mpy_32_16_ss( Ep_peak_fx, 28262, &L_tmp, &lo );
606 0 : Mpy_32_16_ss( Ep_avrgL_fx, 24576, &L_tmp2, &lo );
607 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 )
608 : {
609 0 : adjustFlag = 1;
610 0 : move16();
611 0 : FOR( i = 0; i < lowband; i++ )
612 : {
613 0 : tmp = extract_h( Ep_avrgL_fx ); /*Q-4 */
614 0 : IF( tmp != 0 )
615 : {
616 0 : exp = norm_s( tmp );
617 0 : tmp = shl( tmp, exp ); /*Q(exp) */
618 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
619 0 : exp = sub( 29, exp );
620 : }
621 : ELSE
622 : {
623 : /*when the divisor is zero, happens rarely*/
624 0 : tmp = 0x7fff;
625 0 : move16();
626 0 : exp = 0;
627 0 : move16();
628 : }
629 0 : Mpy_32_16_ss( Ep_peak_fx, tmp, &L_tmp, &lo );
630 0 : Mpy_32_16_ss( L_tmp, lowband, &L_tmp, &lo );
631 0 : Mpy_32_16_ss( L_tmp, 18842, &L_tmp, &lo );
632 : #ifndef BASOP_NOGLOB
633 : L_tmp = L_shl( L_tmp, sub( 27, exp ) ); /*Q14 0.5 */
634 : tmp2 = extract_l( L_min( L_tmp, 19661 ) ); /*14 */
635 : #else /* BASOP_NOGLOB */
636 0 : L_tmp = L_shl_o( L_tmp, sub( 27, exp ), &Overflow ); /*Q14 0.5 */
637 0 : tmp2 = extract_l( L_min( L_tmp, 19661 ) ); /*14 */
638 : #endif /* BASOP_NOGLOB */
639 0 : Mpy_32_16_ss( L_band_energy_tmp[i], tmp2, &L_tmp, &lo );
640 0 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q_band_energy */
641 : }
642 : }
643 : #undef WMC_TOOL_SKIP
644 :
645 0 : for ( i = 0; i < bands; i++ )
646 : {
647 0 : band_energy_tmp[i] = (float) ( L_band_energy_tmp[i] / pow( 2.0f, SWB_BWE_LR_Qbe ) );
648 : }
649 :
650 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 );
651 :
652 : /* encode the last p2a_bands-1 subbands bit-allocation index of the previous frame */
653 0 : for ( i = 0; i < 2; i++ )
654 : {
655 0 : push_indice( hBstr, IND_HQ2_LAST_BA_MAX_BAND, hHQ_core->last_bitalloc_max_band[i], 1 );
656 : }
657 : }
658 34 : else if ( is_transient == 0 && inner_frame == L_FRAME16k )
659 : {
660 : #define WMC_TOOL_SKIP
661 0 : bit_budget = sub( bit_budget, 2 ); /* bits in high bands to indicate the last 2 subbands is allocated bits or not */
662 0 : FOR( i = 0; i < bands; i++ )
663 : {
664 : #ifndef BASOP_NOGLOB
665 : Ep_tmp_fx[i] = L_shl( Ep_tmp_fx[i], 2 );
666 : #else /* BASOP_NOGLOB */
667 0 : Ep_tmp_fx[i] = L_shl_o( Ep_tmp_fx[i], 2, &Overflow );
668 : #endif /* BASOP_NOGLOB */
669 : }
670 0 : IF( st->core_brate == ACELP_13k20 )
671 : {
672 0 : lowband = 8;
673 0 : move16();
674 0 : highband = 15;
675 0 : move16();
676 0 : bw_low = sub( band_start[highband], band_start[lowband] );
677 0 : bw_high = sub( add( band_end[sub( bands, 1 )], 1 ), band_start[highband] );
678 : }
679 : ELSE
680 : {
681 0 : lowband = 8;
682 0 : move16();
683 0 : highband = 16;
684 0 : move16();
685 0 : bw_low = sub( band_start[highband], band_start[lowband] );
686 0 : bw_high = sub( add( band_end[sub( bands, 1 )], 1 ), band_start[highband] );
687 : }
688 : /* calculate the the low band/high band energy and the variance/avrage of the envelopes */
689 0 : enerL_fx = 0;
690 0 : move32();
691 0 : enerH_fx = 0;
692 0 : move32();
693 0 : Ep_vari_fx = 0;
694 0 : move32();
695 0 : Ep_avrg_fx = 0;
696 0 : move32();
697 0 : FOR( i = 0; i < bands; i++ )
698 : {
699 0 : IF( sub( i, lowband ) >= 0 && add( sub( i, bands ), p2a_bands ) < 0 )
700 : {
701 0 : Ep_vari_fx = L_add( Ep_vari_fx, L_abs( L_sub( Ep_tmp_fx[i], Ep_tmp_fx[sub( i, 1 )] ) ) ); /*Q15 */
702 : #ifndef BASOP_NOGLOB
703 : Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
704 : #else /* BASOP_NOGLOB */
705 0 : Ep_avrg_fx = L_add_o( Ep_avrg_fx, Ep_tmp_fx[i], &Overflow ); /*Q15 */
706 : #endif /* BASOP_NOGLOB */
707 : }
708 :
709 0 : IF( sub( i, highband ) >= 0 )
710 : {
711 0 : enerH_fx = L_add( enerH_fx, L_shl( Ep_fx[i], 2 ) ); /*Q0 */
712 : }
713 0 : ELSE IF( sub( i, lowband ) >= 0 )
714 : {
715 0 : enerL_fx = L_add( enerL_fx, L_shl( Ep_fx[i], 2 ) ); /*Q0 */
716 : }
717 : }
718 0 : enerL = (float) ( enerL_fx / pow( 2.0, -4 ) );
719 0 : enerH = (float) ( enerH_fx / pow( 2.0, -4 ) );
720 : /* modify the last p2a_bands subbands band_energies */
721 0 : k = bands;
722 0 : mvl2l( L_band_energy, L_band_energy_tmp, k ); /*Q_band_energy */
723 :
724 0 : L_tmp = L_max( enerH_fx, enerL_fx );
725 0 : tmp = s_max( bw_low, bw_high );
726 0 : i = norm_l( L_tmp );
727 0 : j = norm_s( tmp );
728 0 : Mpy_32_16_ss( L_shl( enerH_fx, i ), shl( bw_low, j ), &L_tmp, &lo );
729 0 : Mpy_32_16_ss( L_shl( enerL_fx, i ), shl( bw_high, j ), &L_tmp2, &lo );
730 0 : L_tmp2 = L_sub( L_tmp, L_tmp2 );
731 :
732 0 : FOR( i = sub( bands, p2a_bands ); i < bands; i++ )
733 : {
734 0 : IF( sub( p2a_flags[i], 1 ) == 0 || L_tmp2 > 0 )
735 : {
736 0 : tmp = sub( bands, p2a_bands );
737 0 : tmp = sub( tmp, lowband ); /*Q0 */
738 :
739 : #ifndef BASOP_NOGLOB
740 : tmp1 = extract_h( L_shl( Ep_avrg_fx, 1 ) ); /*Q0 */
741 : #else
742 0 : tmp1 = extract_h( L_shl_o( Ep_avrg_fx, 1, &Overflow ) ); /*Q0 */
743 : #endif
744 0 : IF( tmp1 != 0 )
745 : {
746 0 : exp = norm_s( tmp1 );
747 0 : tmp1 = shl( tmp1, exp ); /*Q(exp) */
748 0 : tmp1 = div_s( 16384, tmp1 ); /*Q(15+14-exp = 29-exp) */
749 0 : exp = sub( 29, exp );
750 : }
751 : ELSE
752 : {
753 0 : tmp1 = 0x7fff;
754 0 : move16();
755 0 : exp = 0;
756 0 : move16();
757 : }
758 0 : Mpy_32_16_ss( Ep_tmp_fx[i], tmp1, &L_tmp, &lo );
759 0 : Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
760 0 : Mpy_32_16_ss( L_tmp, 16384, &L_tmp, &lo );
761 0 : L_tmp = L_shl( L_tmp, sub( 32, exp ) ); /*Q15 */
762 0 : tmp = extract_l( L_min( L_tmp, 6554 ) ); /*Q15 */
763 0 : Mpy_32_16_ss( Ep_vari_fx, tmp1, &L_tmp, &lo );
764 0 : Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
765 0 : L_tmp = L_shl( L_tmp, sub( 15, exp ) ); /*Q15 */
766 0 : tmp = extract_l( L_shr( L_min( L_tmp, 13107 ), 1 ) ); /*Q14 */
767 0 : alpha_fx = add( tmp, 16384 ); /*Q14 */
768 : }
769 : ELSE
770 : {
771 0 : alpha_fx = 16384;
772 0 : move16(); /*Q14 */
773 : }
774 :
775 0 : IF( add( sub( i, bands ), p2a_bands ) > 0 )
776 : {
777 0 : tmp = sub( bands, p2a_bands );
778 0 : IF( sub( hHQ_core->last_bitalloc_max_band[sub( i, add( tmp, 1 ) )], 1 ) == 0 )
779 : {
780 0 : tmp = sub( tmp, lowband );
781 0 : Mpy_32_16_ss( Ep_tmp_fx[i], tmp, &L_tmp, &lo );
782 : #ifndef BASOP_NOGLOB
783 : tmp = extract_h( L_shl( L_tmp, 16 ) ); /*Q0 */
784 : #else /* BASOP_NOGLOB */
785 0 : tmp = extract_h( L_shl_o( L_tmp, 16, &Overflow ) ); /*Q0 */
786 : #endif /* BASOP_NOGLOB */
787 0 : IF( tmp != 0 )
788 : {
789 0 : exp = norm_s( tmp );
790 0 : tmp = shl( tmp, exp ); /*Q(exp) */
791 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
792 0 : exp = sub( 29, exp );
793 : }
794 : ELSE
795 : {
796 0 : tmp = 0x7fff;
797 0 : move16();
798 0 : exp = 0;
799 0 : move16();
800 : }
801 0 : Mpy_32_16_ss( Ep_avrg_fx, tmp, &L_tmp, &lo );
802 0 : L_tmp = L_shl( L_tmp, sub( 14, exp ) ); /*Q14 */
803 0 : tmp = extract_l( L_min( L_max( L_tmp, 16384 ), 20480 ) ); /*Q14 */
804 0 : L_tmp = L_mult( alpha_fx, tmp ); /*Q(14+14+1=29) */
805 0 : alpha_fx = extract_l( L_shr( L_tmp, 15 ) ); /*Q14*/
806 : }
807 : ELSE
808 : {
809 0 : tmp = sub( tmp, lowband );
810 :
811 : #ifndef BASOP_NOGLOB
812 : tmp1 = extract_h( L_shl( Ep_avrg_fx, 1 ) ); /*Q0 */
813 : #else
814 0 : tmp1 = extract_h( L_shl_o( Ep_avrg_fx, 1, &Overflow ) ); /*Q0 */
815 : #endif
816 0 : IF( tmp1 != 0 )
817 : {
818 0 : exp = norm_s( tmp1 );
819 0 : tmp1 = shl( tmp1, exp ); /*Q(exp) */
820 0 : tmp1 = div_s( 16384, tmp1 ); /*Q(15+14-exp=29-exp) */
821 0 : exp = sub( 29, exp );
822 : }
823 : ELSE
824 : {
825 0 : tmp1 = 0x7fff;
826 0 : move16();
827 0 : exp = 0;
828 0 : move16();
829 : }
830 0 : Mpy_32_16_ss( Ep_tmp_fx[i], tmp1, &L_tmp, &lo );
831 0 : Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
832 0 : L_tmp = L_shl( L_tmp, sub( 29, exp ) ); /*Q14 */
833 0 : tmp = extract_l( L_min( L_max( L_tmp, 13926 ), 16384 ) ); /*Q14 */
834 0 : L_tmp = L_mult( alpha_fx, tmp ); /*Q(14+14+1=29) */
835 0 : alpha_fx = extract_l( L_shr( L_tmp, 15 ) ); /*Q14 */
836 : }
837 : }
838 0 : Mpy_32_16_ss( L_band_energy_tmp[i], alpha_fx, &L_tmp, &lo );
839 0 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q Q_band_energy */
840 : }
841 0 : lowband = 6;
842 0 : move16();
843 0 : Ep_avrg_fx = 0;
844 0 : move32();
845 0 : Ep_avrgL_fx = 0;
846 0 : move32();
847 0 : Ep_peak_fx = 0;
848 0 : move32();
849 0 : FOR( i = 0; i < bands; i++ )
850 : {
851 0 : IF( sub( i, lowband ) >= 0 )
852 : {
853 : #ifndef BASOP_NOGLOB
854 : Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
855 : #else /* BASOP_NOGLOB */
856 0 : Ep_avrg_fx = L_add_o( Ep_avrg_fx, Ep_tmp_fx[i], &Overflow ); /*Q15 */
857 : #endif /* BASOP_NOGLOB */
858 : }
859 : ELSE
860 : {
861 : #ifndef BASOP_NOGLOB
862 : Ep_avrgL_fx = L_add( Ep_avrgL_fx, Ep_tmp_fx[i] ); /*Q15 */
863 : #else /* BASOP_NOGLOB */
864 0 : Ep_avrgL_fx = L_add_o( Ep_avrgL_fx, Ep_tmp_fx[i], &Overflow ); /*Q15 */
865 : #endif /* BASOP_NOGLOB */
866 0 : IF( L_sub( Ep_tmp_fx[i], Ep_peak_fx ) > 0 )
867 : {
868 0 : Ep_peak_fx = Ep_tmp_fx[i];
869 0 : move32(); /*Q15 */
870 : }
871 : }
872 : }
873 :
874 0 : Mpy_32_16_ss( Ep_peak_fx, 24576, &L_tmp, &lo );
875 0 : Mpy_32_16_ss( Ep_peak_fx, 19661, &L_tmp2, &lo );
876 0 : Mpy_32_16_ss( Ep_avrgL_fx, 24576, &L_tmp3, &lo );
877 :
878 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 ) ||
879 : ( 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 ) )
880 : {
881 0 : adjustFlag = 1;
882 0 : move16();
883 0 : FOR( i = 0; i < lowband; i++ )
884 : {
885 : #ifndef BASOP_NOGLOB
886 : tmp = extract_h( L_shl( Ep_avrgL_fx, 1 ) ); /*Q0 */
887 : #else
888 0 : tmp = extract_h( L_shl_o( Ep_avrgL_fx, 1, &Overflow ) ); /*Q0 */
889 : #endif
890 0 : IF( tmp != 0 )
891 : {
892 0 : exp = norm_s( tmp );
893 0 : tmp = shl( tmp, exp ); /*Q(exp) */
894 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
895 0 : exp = sub( 29, exp );
896 : }
897 : ELSE
898 : {
899 0 : tmp = 0x7fff;
900 0 : move16();
901 0 : exp = 0;
902 0 : move16();
903 : }
904 0 : Mpy_32_16_ss( Ep_peak_fx, tmp, &L_tmp, &lo );
905 0 : Mpy_32_16_ss( L_tmp, lowband, &L_tmp, &lo );
906 0 : L_tmp = L_shl( L_tmp, sub( 28, exp ) ); /*Q14 0.5 */
907 0 : tmp = extract_l( L_min( L_tmp, 19661 ) ); /*Q14 */
908 0 : Mpy_32_16_ss( L_band_energy_tmp[i], tmp, &L_tmp, &lo );
909 0 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q_band_energy */
910 : }
911 : }
912 : #undef WMC_TOOL_SKIP
913 :
914 0 : for ( i = 0; i < bands; i++ )
915 : {
916 0 : band_energy_tmp[i] = (float) ( L_band_energy_tmp[i] / pow( 2.0f, SWB_BWE_LR_Qbe ) );
917 : }
918 :
919 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 );
920 :
921 : /* encode the last p2a_bands-1 subbands bit-allocation index of the previous frame */
922 0 : for ( i = 0; i < 2; i++ )
923 : {
924 0 : push_indice( hBstr, IND_HQ2_LAST_BA_MAX_BAND, hHQ_core->last_bitalloc_max_band[i], 1 );
925 : }
926 : }
927 34 : else if ( st->bwidth == SWB && hqswb_clas == HQ_HARMONIC && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
928 : {
929 : /* bit allocation for harmonic mode */
930 0 : hq2_bit_alloc_har( band_energy, bit_budget, bands, Rk_fx, p2a_bands, bwe_br, p2a_flags, band_width );
931 : }
932 : else
933 : {
934 :
935 : /* estimate number of bits per band */
936 34 : 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 );
937 : }
938 :
939 : #ifdef DEBUGGING
940 : tcq_core_LR_enc( hBstr, st->idchan, inp_vector, t_audio, y2, bit_budget, bands, band_start, band_end, band_width, Rk_fx, npulses, k_sort, p2a_flags, p2a_bands, hHQ_core->last_bitalloc_max_band, inner_frame, adjustFlag, is_transient );
941 : #else
942 34 : tcq_core_LR_enc( hBstr, inp_vector, t_audio, y2, bit_budget, bands, band_start, band_end, band_width, Rk_fx, npulses, k_sort, p2a_flags, p2a_bands, hHQ_core->last_bitalloc_max_band, inner_frame, adjustFlag, is_transient );
943 : #endif
944 :
945 34 : if ( ( inner_frame == L_FRAME8k && st->core_brate <= ACELP_13k20 ) || inner_frame == L_FRAME16k )
946 : {
947 0 : j = 0;
948 0 : for ( i = 2; i > 0; i-- )
949 : {
950 0 : if ( npulses[bands - i] > 0 )
951 : {
952 0 : hHQ_core->last_bitalloc_max_band[j] = 1;
953 : }
954 : else
955 : {
956 0 : hHQ_core->last_bitalloc_max_band[j] = 0;
957 : }
958 0 : j++;
959 : }
960 : }
961 :
962 : /* Prepare floating Rk for next modules */
963 812 : for ( k = 0; k < bands; k++ )
964 : {
965 778 : Rk[k] = WORD322FL_SCALE( Rk_fx[k], SWB_BWE_LR_QRk - 1 );
966 : }
967 :
968 : /* Denormalize the coded MDCT spectrum */
969 34 : mdct_spectrum_denorm( inp_vector, y2, band_start, band_end, band_width, band_energy, npulses, bands, ld_slope, pd_thresh );
970 :
971 : /* Apply fine gain quantization to denormalized coded spectrum */
972 34 : mdct_spectrum_fine_gain_enc( hBstr, t_audio, y2, band_start, band_end, k_sort, bands, L_qint, Ngq, gqlevs, gqbits );
973 :
974 : /* reStore the subband information*/
975 34 : if ( flag_spt == 1 )
976 : {
977 31 : spt_shorten_domain_band_restore( bands, band_start, band_end, band_width, org_band_start, org_band_end, org_band_width );
978 : }
979 :
980 : /* Inject noise into components having relatively low pulse energy per band */
981 34 : ni_seed = npulses[0] + npulses[1] + npulses[2] + npulses[3];
982 :
983 19346 : for ( i = 0; i < band_end[bands - 1] + 1; i++ )
984 19312 : y2_ni[i] = y2[i];
985 34 : 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 );
986 :
987 34 : if ( st->bwidth == SWB && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
988 : {
989 34 : if ( hqswb_clas == HQ_NORMAL || hqswb_clas == HQ_HARMONIC )
990 : {
991 31 : preset_hq2_swb( hqswb_clas, band_end, &har_bands, p2a_bands, length, bands, &lowlength, &highlength, m );
992 :
993 31 : swb_bwe_enc_lr( st, y2, t_audio, m, bwe_br, bands, band_start, band_end, band_energy, p2a_flags, hqswb_clas, lowlength, highlength, hHQ_core->prev_frm_index, har_bands, &hHQ_core->prev_frm_hfe2, &hHQ_core->prev_stab_hfe2, band_width, y2_ni, &ni_seed );
994 :
995 31 : post_hq2_swb( m, lowlength, highlength, hqswb_clas, har_bands, bands, p2a_flags, band_start, band_end, y2, npulses );
996 :
997 31 : if ( hqswb_clas == HQ_NORMAL )
998 : {
999 31 : spt_swb_peakpos_tmp_save( y2, bands, band_start, band_end, prev_SWB_peak_pos_tmp );
1000 155 : for ( k = 0; k < SPT_SHORTEN_SBNUM; k++ )
1001 : {
1002 124 : if ( p2a_flags[bands - SPT_SHORTEN_SBNUM + k] == 0 || npulses[bands - SPT_SHORTEN_SBNUM + k] == 0 )
1003 : {
1004 122 : prev_SWB_peak_pos_tmp[k] = 0;
1005 : }
1006 : }
1007 : }
1008 :
1009 31 : mvr2r( y2_ni, y2, lowlength );
1010 : }
1011 : else
1012 : {
1013 3 : mvr2r( y2_ni, y2, band_end[bands - 1] + 1 ); /* HQ_TRANSIENT */
1014 : }
1015 : }
1016 : else
1017 : {
1018 0 : mvr2r( y2_ni, y2, band_end[bands - 1] + 1 ); /* NB, WB */
1019 : }
1020 :
1021 :
1022 34 : updat_prev_frm( y2, t_audio, 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, bws_cnt );
1023 :
1024 34 : if ( st->bwidth != SWB )
1025 : {
1026 : /* reset HQ classifier memories */
1027 0 : hHQ_core->mode_count = 0;
1028 0 : hHQ_core->mode_count1 = 0;
1029 : }
1030 34 : if ( hqswb_clas != HQ_HARMONIC && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) && st->bwidth == SWB )
1031 : {
1032 34 : hHQ_core->prev_frm_index[0] = -1;
1033 34 : hHQ_core->prev_frm_index[1] = -1;
1034 : }
1035 : /* update number of unused bits */
1036 34 : *num_bits = 0;
1037 :
1038 34 : hHQ_core->hvq_hangover = 0;
1039 :
1040 34 : return;
1041 : }
1042 :
1043 : /*--------------------------------------------------------------------------*
1044 : * small_symbol_enc_tran()
1045 : *
1046 : * Huffman encoding of differential energies, estimating or packing bits
1047 : * if flag_pack = 0, LC mode info. is output else LC mode info. is input
1048 : * if flag_pack = 0, estimatng else packing bits
1049 : *--------------------------------------------------------------------------*/
1050 :
1051 : /*! r: bits */
1052 5 : static int16_t small_symbol_enc_tran(
1053 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
1054 : const int16_t *qbidx, /* i : input of dequantized differential energy */
1055 : const int16_t BANDS, /* i : number of bands */
1056 : int16_t *hLCmode, /* i/o: LC mode info */
1057 : const int16_t flag_pack, /* i : indicator of packing or estimating bits */
1058 : const int16_t is_transient )
1059 : {
1060 : int16_t i, bits;
1061 : int16_t difidx[BANDS_MAX];
1062 :
1063 165 : for ( i = 0; i < BANDS; i++ )
1064 : {
1065 160 : difidx[i] = qbidx[i] + LRMDCT_BE_OFFSET;
1066 : }
1067 :
1068 165 : for ( i = 0; i < BANDS; ++i )
1069 : {
1070 160 : if ( difidx[i] > LRMDCT_BE_LIMIT || difidx[i] < 0 )
1071 : {
1072 : /* Huffman cannot encode this vector */
1073 0 : return -1;
1074 : }
1075 : }
1076 :
1077 : /* Preparing lossless coding input */
1078 5 : if ( flag_pack == 0 )
1079 : {
1080 : /* estimating # of bits */
1081 3 : bits = encode_envelope_indices( hBstr, BANDS, -1, difidx, hLCmode, flag_pack, LOW_RATE_HQ_CORE_TRAN, is_transient );
1082 3 : bits += BITS_DE_FCOMP; /* xx bits for diff. energies + BITS_DE_FCOMP bits for first energies */
1083 : }
1084 : else
1085 : {
1086 2 : bits = 0;
1087 2 : encode_envelope_indices( hBstr, BANDS, -1, difidx, hLCmode, flag_pack, LOW_RATE_HQ_CORE_TRAN, is_transient );
1088 : }
1089 :
1090 5 : return bits + BITS_DE_HMODE; /* xx bits for diff. energies + 1 bit for LC coding mode */
1091 : }
1092 :
1093 :
1094 : /*--------------------------------------------------------------------------*
1095 : * small_symbol_enc()
1096 : *
1097 : * Huffman encoding of differential energies, estimating or packing bits
1098 : * if flag_pack = 0, LC mode info. is output else LC mode info. is input
1099 : * if flag_pack = 0, estimatng else packing bits
1100 : *--------------------------------------------------------------------------*/
1101 :
1102 : /*! r: bits */
1103 61 : static int16_t small_symbol_enc(
1104 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
1105 : const int16_t *qbidx, /* i : input of dequantized differential energy */
1106 : const int16_t BANDS, /* i : number of bands */
1107 : int16_t *hLCmode, /* i/o: LC mode info */
1108 : const int16_t flag_pack, /* i : indicator of packing or estimating bits */
1109 : const int16_t is_transient )
1110 : {
1111 : int16_t i, bits;
1112 : int16_t difidx[BANDS_MAX], LSB[BANDS_MAX];
1113 :
1114 : /* Preparing lossless coding input */
1115 61 : difidx[0] = qbidx[0] + DE_OFFSET0;
1116 :
1117 1342 : for ( i = 1; i < BANDS; ++i )
1118 : {
1119 1281 : difidx[i] = qbidx[i] + DE_OFFSET1;
1120 : }
1121 :
1122 1403 : for ( i = 0; i < BANDS; ++i )
1123 : {
1124 1342 : if ( difidx[i] >= DE_LIMIT || difidx[i] < 0 )
1125 : {
1126 : /* Huffman cannot encode this vector */
1127 0 : return -1;
1128 : }
1129 : }
1130 :
1131 : /* splitting MSB and LSB */
1132 1403 : for ( i = 0; i < BANDS; ++i )
1133 : {
1134 1342 : LSB[i] = difidx[i] & 1;
1135 1342 : difidx[i] >>= 1;
1136 : }
1137 :
1138 : /* Preparing lossless coding input */
1139 61 : if ( flag_pack == 0 )
1140 : {
1141 : /* estimating # of bits */
1142 : /* Encoding MSB bits */
1143 31 : bits = encode_envelope_indices( hBstr, BANDS, -1, difidx, hLCmode, flag_pack, LOW_RATE_HQ_CORE, is_transient );
1144 31 : bits += BITS_DE_FCOMP; /* xx bits for diff. energies + BITS_DE_FCOMP bits for first energies */
1145 :
1146 : /* Encoding LSB bit packing */
1147 31 : bits += BANDS;
1148 : }
1149 : else
1150 : {
1151 : /* Encoding MSB bits */
1152 30 : bits = 0;
1153 30 : encode_envelope_indices( hBstr, BANDS, -1, difidx, hLCmode, flag_pack, LOW_RATE_HQ_CORE, is_transient );
1154 :
1155 : /* Encoding LSB bit packing */
1156 690 : for ( i = 0; i < BANDS; ++i )
1157 : {
1158 660 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, LSB[i], BITS_DE_LSB );
1159 : }
1160 : }
1161 :
1162 61 : return bits + BITS_DE_HMODE; /* xx bits for diff. energies + 1 bit for LC coding mode */
1163 : }
1164 :
1165 :
1166 : /*--------------------------------------------------------------------------*
1167 : * large_symbol_enc()
1168 : *
1169 : *
1170 : *--------------------------------------------------------------------------*/
1171 :
1172 : /*! r: bits */
1173 36 : static int16_t large_symbol_enc(
1174 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
1175 : int16_t *qbidx, /* i : input of dequantized differential energy */
1176 : const int16_t BANDS, /* i : number of bands */
1177 : int16_t *hLCmode0, /* i/o: LC mode info */
1178 : const int16_t flag_pack /* i : indicator of packing or estimating bits */
1179 : )
1180 : {
1181 : int16_t i, bits;
1182 : int16_t LSB1[BANDS_MAX];
1183 36 : int16_t min_q = 513, max_q = -1, offset0;
1184 : int16_t min_bits, min_bits_pos;
1185 : int16_t tdifidx0[BANDS_MAX], tdifidx1[BANDS_MAX];
1186 : int16_t basic_shift;
1187 : int16_t bitsmode0, bitsmode1;
1188 : int16_t lsbdepth1;
1189 : int16_t cnt_outlyer, pos_outlyer, cnt_outlyer0;
1190 :
1191 36 : cnt_outlyer0 = 0;
1192 36 : cnt_outlyer = 0;
1193 36 : bitsmode0 = 0;
1194 36 : bitsmode1 = 0;
1195 36 : pos_outlyer = 0;
1196 36 : lsbdepth1 = 0;
1197 :
1198 36 : if ( flag_pack == 0 || ( flag_pack == 1 && *hLCmode0 == 0 ) )
1199 : {
1200 35 : if ( qbidx[0] > ABS_ENG_OFFSET - 1 || qbidx[0] < -ABS_ENG_OFFSET )
1201 : {
1202 0 : cnt_outlyer0 = 2;
1203 : }
1204 35 : else if ( qbidx[0] > 3 || qbidx[0] < -4 )
1205 : {
1206 28 : cnt_outlyer0 = 1;
1207 : }
1208 : else
1209 : {
1210 7 : cnt_outlyer0 = 0;
1211 : }
1212 :
1213 35 : cnt_outlyer = 0;
1214 35 : pos_outlyer = -1;
1215 810 : for ( i = 1; i < BANDS; ++i )
1216 : {
1217 775 : if ( qbidx[i] > 3 || qbidx[i] < -4 )
1218 : {
1219 210 : cnt_outlyer++;
1220 210 : pos_outlyer = i;
1221 : }
1222 :
1223 775 : if ( qbidx[i] > ABS_ENG_OFFSET - 1 || qbidx[i] < -ABS_ENG_OFFSET )
1224 : {
1225 0 : cnt_outlyer++;
1226 : }
1227 : }
1228 :
1229 35 : if ( cnt_outlyer0 == 0 && cnt_outlyer <= 1 )
1230 : {
1231 5 : bitsmode0 = BITS_DE_8SMODE + BITS_DE_8SMODE_N0 + BITS_DE_8SMODE_N1;
1232 5 : if ( cnt_outlyer == 1 )
1233 : {
1234 : /* 01 */
1235 1 : bitsmode0 += BITS_DE_8SPOS + BITS_ABS_ENG;
1236 : }
1237 :
1238 6 : for ( i = 0; i < pos_outlyer; ++i )
1239 : {
1240 1 : tdifidx0[i] = qbidx[i];
1241 1 : bitsmode0 += hessize[tdifidx0[i] + 4];
1242 : }
1243 :
1244 153 : for ( i = pos_outlyer + 1; i < BANDS; ++i )
1245 : {
1246 148 : tdifidx0[i] = qbidx[i];
1247 148 : bitsmode0 += hessize[tdifidx0[i] + 4];
1248 : }
1249 : }
1250 30 : else if ( cnt_outlyer0 == 1 && cnt_outlyer <= 1 )
1251 : {
1252 2 : bitsmode0 = BITS_DE_8SMODE + BITS_DE_8SMODE_N0 + BITS_DE_8SMODE_N1;
1253 2 : tdifidx0[0] = qbidx[0];
1254 2 : bitsmode0 += BITS_ABS_ENG;
1255 2 : if ( cnt_outlyer == 1 )
1256 : {
1257 : /* 11 */
1258 2 : bitsmode0 += BITS_DE_8SPOS + BITS_ABS_ENG;
1259 : }
1260 : else
1261 : {
1262 0 : pos_outlyer = 0;
1263 : }
1264 :
1265 3 : for ( i = 1; i < pos_outlyer; ++i )
1266 : {
1267 1 : tdifidx0[i] = qbidx[i];
1268 1 : bitsmode0 += hessize[tdifidx0[i] + 4];
1269 : }
1270 :
1271 41 : for ( i = pos_outlyer + 1; i < BANDS; ++i )
1272 : {
1273 39 : tdifidx0[i] = qbidx[i];
1274 39 : bitsmode0 += hessize[tdifidx0[i] + 4];
1275 : }
1276 : }
1277 : else
1278 : {
1279 28 : bitsmode0 = 20000;
1280 : }
1281 : }
1282 :
1283 36 : if ( flag_pack == 0 || ( flag_pack == 1 && *hLCmode0 == 1 ) )
1284 : {
1285 : /* components 0 range : -256~255 */
1286 35 : max_q = MINIMUM_ENERGY_LOWBRATE;
1287 35 : min_q = MAXIMUM_ENERGY_LOWBRATE;
1288 835 : for ( i = 0; i < BANDS; ++i )
1289 : {
1290 800 : if ( qbidx[i] > max_q )
1291 : {
1292 127 : max_q = qbidx[i];
1293 : }
1294 :
1295 800 : if ( qbidx[i] < min_q )
1296 : {
1297 54 : min_q = qbidx[i];
1298 : }
1299 : }
1300 :
1301 : /* Counting bits for transmitting all components using same method */
1302 99 : for ( i = 0;; ++i )
1303 : {
1304 99 : if ( max_q <= ( ( 2 << ( i + 1 ) ) - 1 ) && min_q >= -( 2 << ( i + 1 ) ) )
1305 : {
1306 35 : break;
1307 : }
1308 : }
1309 35 : basic_shift = i;
1310 :
1311 35 : min_bits = 1000;
1312 35 : min_bits_pos = basic_shift;
1313 140 : for ( offset0 = basic_shift; offset0 < basic_shift + 3; offset0++ )
1314 : {
1315 105 : max_q = MINIMUM_ENERGY_LOWBRATE;
1316 105 : min_q = MAXIMUM_ENERGY_LOWBRATE;
1317 :
1318 105 : bitsmode1 = BITS_DE_8SMODE + BITS_MAX_DEPTH;
1319 2505 : for ( i = 0; i < BANDS; ++i )
1320 : {
1321 2400 : bitsmode1 += ( hessize[( qbidx[i] >> offset0 ) + 4] + ( offset0 ) );
1322 : }
1323 :
1324 105 : if ( min_bits > bitsmode1 )
1325 : {
1326 45 : min_bits_pos = offset0;
1327 45 : min_bits = bitsmode1;
1328 : }
1329 : }
1330 :
1331 35 : bitsmode1 = min_bits;
1332 35 : lsbdepth1 = min_bits_pos;
1333 :
1334 835 : for ( i = 0; i < BANDS; ++i )
1335 : {
1336 800 : LSB1[i] = qbidx[i] & ( ( 1 << lsbdepth1 ) - 1 );
1337 800 : tdifidx1[i] = qbidx[i] >> lsbdepth1;
1338 : }
1339 : }
1340 :
1341 : /* Preparing lossless coding input */
1342 36 : if ( flag_pack == 0 )
1343 : {
1344 : /* estimating # of bits */
1345 : /* Encoding MSB bits */
1346 34 : if ( bitsmode0 < bitsmode1 )
1347 : {
1348 4 : bits = bitsmode0;
1349 4 : *hLCmode0 = 0;
1350 : }
1351 : else
1352 : {
1353 30 : bits = bitsmode1;
1354 30 : *hLCmode0 = 1;
1355 : }
1356 : }
1357 : else
1358 : {
1359 : /* Encoding MSB bits */
1360 2 : if ( *hLCmode0 == 0 )
1361 : {
1362 1 : push_indice( hBstr, IND_HQ2_DENG_8SMODE, 0, BITS_DE_8SMODE );
1363 1 : bits = BITS_DE_8SMODE;
1364 1 : if ( cnt_outlyer0 == 0 )
1365 : {
1366 1 : push_indice( hBstr, IND_HQ2_DENG_8SMODE_N0, 0, BITS_DE_8SMODE_N0 );
1367 1 : bits += BITS_DE_8SMODE_N0;
1368 1 : if ( cnt_outlyer == 1 )
1369 : {
1370 : /* 01 */
1371 0 : push_indice( hBstr, IND_HQ2_DENG_8SMODE_N1, 1, BITS_DE_8SMODE_N1 );
1372 0 : bits += BITS_DE_8SMODE_N1;
1373 0 : push_indice( hBstr, IND_HQ2_DENG_8SPOS, pos_outlyer, BITS_DE_8SPOS );
1374 0 : bits += BITS_DE_8SPOS;
1375 0 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, qbidx[pos_outlyer] + ABS_ENG_OFFSET, BITS_ABS_ENG );
1376 0 : bits += BITS_ABS_ENG;
1377 : }
1378 : else
1379 : {
1380 : /* 00 */
1381 1 : push_indice( hBstr, IND_HQ2_DENG_8SMODE_N1, 0, BITS_DE_8SMODE_N1 );
1382 1 : bits += BITS_DE_8SMODE_N1;
1383 : }
1384 :
1385 1 : for ( i = 0; i < pos_outlyer; ++i )
1386 : {
1387 0 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, hescode[tdifidx0[i] + 4], hessize[tdifidx0[i] + 4] );
1388 0 : bitsmode0 += hessize[tdifidx0[i] + 4];
1389 : }
1390 :
1391 33 : for ( i = pos_outlyer + 1; i < BANDS; ++i )
1392 : {
1393 32 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, hescode[tdifidx0[i] + 4], hessize[tdifidx0[i] + 4] );
1394 32 : bitsmode0 += hessize[tdifidx0[i] + 4];
1395 : }
1396 : }
1397 0 : else if ( cnt_outlyer0 == 1 )
1398 : {
1399 0 : push_indice( hBstr, IND_HQ2_DENG_8SMODE_N0, 1, BITS_DE_8SMODE_N0 );
1400 0 : bits += BITS_DE_8SMODE_N0;
1401 0 : if ( cnt_outlyer == 1 )
1402 : {
1403 0 : push_indice( hBstr, IND_HQ2_DENG_8SMODE_N1, 1, BITS_DE_8SMODE_N1 );
1404 0 : bits += BITS_DE_8SMODE_N1;
1405 0 : push_indice( hBstr, IND_HQ2_DENG_8SPOS, pos_outlyer, BITS_DE_8SPOS );
1406 0 : bits += BITS_DE_8SPOS;
1407 0 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, qbidx[0] + ABS_ENG_OFFSET, BITS_ABS_ENG );
1408 0 : bits += BITS_ABS_ENG;
1409 0 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, qbidx[pos_outlyer] + ABS_ENG_OFFSET, BITS_ABS_ENG );
1410 0 : bits += BITS_ABS_ENG;
1411 : }
1412 : else
1413 : {
1414 0 : push_indice( hBstr, IND_HQ2_DENG_8SMODE_N1, 0, BITS_DE_8SMODE_N1 );
1415 0 : bits += BITS_DE_8SMODE_N1;
1416 0 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, qbidx[0] + ABS_ENG_OFFSET, BITS_ABS_ENG );
1417 0 : bits += BITS_ABS_ENG;
1418 : }
1419 :
1420 0 : for ( i = 1; i < pos_outlyer; ++i )
1421 : {
1422 0 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, hescode[tdifidx0[i] + 4], hessize[tdifidx0[i] + 4] );
1423 0 : bits += hessize[tdifidx0[i] + 4];
1424 : }
1425 :
1426 0 : for ( i = pos_outlyer + 1; i < BANDS; ++i )
1427 : {
1428 0 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, hescode[tdifidx0[i] + 4], hessize[tdifidx0[i] + 4] );
1429 0 : bits += hessize[tdifidx0[i] + 4];
1430 : }
1431 : }
1432 : }
1433 : else
1434 : {
1435 1 : bits = BITS_DE_8SMODE + BITS_MAX_DEPTH;
1436 1 : push_indice( hBstr, IND_HQ2_DENG_8SMODE, 1, BITS_DE_8SMODE );
1437 1 : push_indice( hBstr, IND_HQ2_DENG_8SDEPTH, lsbdepth1, BITS_MAX_DEPTH );
1438 :
1439 23 : for ( i = 0; i < BANDS; ++i )
1440 : {
1441 22 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, hescode[tdifidx1[i] + 4], hessize[tdifidx1[i] + 4] );
1442 22 : bits += hessize[tdifidx1[i] + 4];
1443 : }
1444 :
1445 1 : if ( lsbdepth1 > 0 )
1446 : {
1447 23 : for ( i = 0; i < BANDS; ++i )
1448 : {
1449 22 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, LSB1[i], lsbdepth1 );
1450 : }
1451 1 : bits += BANDS * lsbdepth1;
1452 : }
1453 : }
1454 : }
1455 :
1456 36 : return bits; /* xx bits for diff. energies + 1 bit for LC coding mode */
1457 : }
1458 :
1459 : /*-------------------------------------------------------------------*
1460 : * band_energy_quant()
1461 : *
1462 : *
1463 : *-------------------------------------------------------------------*/
1464 :
1465 34 : static float band_energy_quant(
1466 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
1467 : const float *t_audio,
1468 : const int16_t band_start[],
1469 : const int16_t band_end[],
1470 : float band_energy[],
1471 : const int16_t bands,
1472 : const Word32 L_qint, /* Q29 */
1473 : const Word16 eref_fx, /* Q10 */
1474 : const int16_t is_transient )
1475 : {
1476 : int16_t i, k;
1477 : float E;
1478 : int16_t ebits;
1479 : int16_t hLCmode0, hLCmode1, deng_bits;
1480 : int16_t bq1_temp[BANDS_MAX], bq2_temp[BANDS_MAX];
1481 : int16_t bq0;
1482 : int16_t bq1[BANDS_MAX];
1483 : int16_t bq2[BANDS_MAX];
1484 34 : int16_t deng_cmode = 0;
1485 : int16_t hbits;
1486 : Word32 L_tmp;
1487 : Word32 L_band_energy[BANDS_MAX];
1488 : Word16 exp_normd;
1489 : Word16 rev_qint_fx;
1490 : Word16 Qrev_qint;
1491 :
1492 : /* Calculate the band energies */
1493 812 : for ( k = 0; k < bands; k++ )
1494 : {
1495 778 : E = 0.0f;
1496 20090 : for ( i = band_start[k]; i <= band_end[k]; i++ )
1497 : {
1498 19312 : E += t_audio[i] * t_audio[i];
1499 : }
1500 :
1501 778 : band_energy[k] = (float) log2_f( E + 0.18e-1f );
1502 : }
1503 :
1504 34 : if ( is_transient )
1505 : {
1506 3 : reverse_transient_frame_energies( band_energy, bands );
1507 : }
1508 :
1509 : /* Quantize the reference and band energies */
1510 812 : for ( k = 0; k < bands; k++ )
1511 778 : L_band_energy[k] = (Word32) ( band_energy[k] * pow( 2.0f, SWB_BWE_LR_Qbe ) );
1512 :
1513 : #define WMC_TOOL_SKIP
1514 34 : exp_normd = norm_l( L_qint );
1515 34 : rev_qint_fx = div_s( 0x4000, round_fx( L_shl( L_qint, exp_normd ) ) ); /* Q14-(29+exp_normd-16)+15 */
1516 34 : Qrev_qint = sub( 14 - ( 29 - 16 ) + 15, exp_normd );
1517 :
1518 34 : bq0 = round_fx( L_shl( L_mult( eref_fx, rev_qint_fx ), sub( 5, Qrev_qint ) ) ); /* 16-(10+Qrev_qint+1) */
1519 812 : FOR( k = 0; k < bands; k++ )
1520 : {
1521 : /*bq1[k] = round_f (band_energy[k] / qint); */
1522 778 : L_tmp = L_mls( L_band_energy[k], rev_qint_fx ); /* Q14+Qrev_qint-15 */
1523 778 : bq1[k] = round_fx( L_shl( L_tmp, sub( 17, Qrev_qint ) ) ); /* 16-(14+Qrev_qint-15) */
1524 : }
1525 : #undef WMC_TOOL_SKIP
1526 :
1527 34 : if ( is_transient )
1528 : {
1529 3 : mvs2s( bq1, bq1_temp, bands );
1530 :
1531 : /* Calculate the differential energies */
1532 3 : diffcod_lrmdct( bands, bq0, bq1_temp, bq2_temp, is_transient );
1533 : }
1534 :
1535 : /* Calculate the differential energies */
1536 34 : bq2[0] = bq1[0] - bq0;
1537 778 : for ( k = 1; k < bands; k++ )
1538 : {
1539 744 : bq2[k] = bq1[k] - bq1[k - 1];
1540 : }
1541 :
1542 : /* Modifying qbidx to be located in the range -256~255 */
1543 812 : for ( i = 0; i < bands; ++i )
1544 : {
1545 778 : if ( bq2[i] > MAXIMUM_ENERGY_LOWBRATE )
1546 : {
1547 0 : bq2[i] = MAXIMUM_ENERGY_LOWBRATE;
1548 : }
1549 778 : if ( bq2[i] < MINIMUM_ENERGY_LOWBRATE )
1550 : {
1551 0 : bq2[i] = MINIMUM_ENERGY_LOWBRATE;
1552 : }
1553 : }
1554 :
1555 : /* Get number of bits by Huffman0 coding */
1556 34 : ebits = large_symbol_enc( hBstr, bq2, bands, &hLCmode0, 0 );
1557 :
1558 34 : if ( is_transient )
1559 : {
1560 : /* Get number of bits by Huffman coding */
1561 3 : hbits = small_symbol_enc_tran( hBstr, bq2_temp, bands, &hLCmode1, 0, is_transient );
1562 : }
1563 : else
1564 : {
1565 : /* Get number of bits by Huffman coding */
1566 31 : hbits = small_symbol_enc( hBstr, bq2, bands, &hLCmode1, 0, is_transient );
1567 : }
1568 :
1569 : /* comparing used bits */
1570 34 : if ( ebits < hbits || hbits == -1 )
1571 : {
1572 2 : deng_cmode = 0;
1573 2 : push_indice( hBstr, IND_HQ2_DENG_MODE, deng_cmode, BITS_DE_CMODE );
1574 2 : large_symbol_enc( hBstr, bq2, bands, &hLCmode0, 1 );
1575 2 : deng_bits = ebits + BITS_DE_CMODE;
1576 : }
1577 : else
1578 : {
1579 : /* setting energy difference coding mode and storing it */
1580 32 : deng_cmode = 1;
1581 32 : push_indice( hBstr, IND_HQ2_DENG_MODE, deng_cmode, BITS_DE_CMODE );
1582 :
1583 32 : deng_bits = hbits + BITS_DE_CMODE;
1584 :
1585 : /* packing indice */
1586 32 : if ( is_transient )
1587 : {
1588 2 : mvs2s( bq2_temp, bq2, bands );
1589 :
1590 2 : small_symbol_enc_tran( hBstr, bq2, bands, &hLCmode1, 1, is_transient );
1591 : }
1592 : else
1593 : {
1594 30 : small_symbol_enc( hBstr, bq2, bands, &hLCmode1, 1, is_transient );
1595 : }
1596 : }
1597 :
1598 : /* Reconstruct quantized spectrum */
1599 34 : bq1[0] = bq2[0] + bq0;
1600 778 : for ( k = 1; k < bands; k++ )
1601 : {
1602 744 : bq1[k] = bq2[k] + bq1[k - 1];
1603 : }
1604 :
1605 812 : for ( k = 0; k < bands; k++ )
1606 : {
1607 : #define WMC_TOOL_SKIP
1608 778 : L_band_energy[k] = L_mls( L_qint, (Word16) bq1[k] );
1609 778 : move32(); /* 29+0-15 -> Qbe(Q14) */
1610 : #undef WMC_TOOL_SKIP
1611 778 : band_energy[k] = (float) ( L_band_energy[k] / pow( 2.0f, SWB_BWE_LR_Qbe ) );
1612 : }
1613 :
1614 34 : if ( is_transient )
1615 : {
1616 3 : reverse_transient_frame_energies( band_energy, bands );
1617 : }
1618 :
1619 34 : return ( deng_bits );
1620 : }
1621 :
1622 :
1623 : /*-------------------------------------------------------------------*
1624 : * p2a_threshold_quant()
1625 : *
1626 : *
1627 : *-------------------------------------------------------------------*/
1628 :
1629 34 : static int16_t p2a_threshold_quant(
1630 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
1631 : const float *t_audio,
1632 : const int16_t band_start[],
1633 : const int16_t band_end[],
1634 : const int16_t band_width[],
1635 : const int16_t bands,
1636 : const int16_t p2a_bands,
1637 : const float p2a_th,
1638 : int16_t *p2a_flags )
1639 : {
1640 : int16_t i, j, k;
1641 : float p, a, e;
1642 : float p2a;
1643 :
1644 34 : set_s( p2a_flags, 1, bands );
1645 :
1646 34 : j = 0;
1647 189 : for ( k = bands - p2a_bands; k < bands; k++ )
1648 : {
1649 155 : a = 0.0f;
1650 155 : p = 0.0f;
1651 11129 : for ( i = band_start[k]; i <= band_end[k]; i++ )
1652 : {
1653 10974 : e = t_audio[i] * t_audio[i];
1654 10974 : if ( e > p )
1655 : {
1656 815 : p = e;
1657 : }
1658 10974 : a += e;
1659 : }
1660 :
1661 155 : if ( a > 0.0f )
1662 : {
1663 155 : a /= band_width[k];
1664 155 : p2a = 10.0f * (float) log10( p / a );
1665 :
1666 155 : if ( p2a <= p2a_th )
1667 : {
1668 148 : p2a_flags[k] = 0;
1669 : }
1670 : }
1671 :
1672 155 : push_indice( hBstr, IND_HQ2_P2A_FLAGS, p2a_flags[k], 1 );
1673 155 : j++;
1674 : }
1675 :
1676 34 : return ( j );
1677 : }
1678 :
1679 :
1680 : /*-------------------------------------------------------------------*
1681 : * mdct_spectrum_fine_gain_enc()
1682 : *
1683 : *
1684 : *-------------------------------------------------------------------*/
1685 :
1686 34 : static void mdct_spectrum_fine_gain_enc(
1687 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
1688 : const float ybuf[],
1689 : float y2[],
1690 : const int16_t band_start[],
1691 : const int16_t band_end[],
1692 : const int16_t k_sort[],
1693 : const int16_t bands,
1694 : const Word32 L_qint,
1695 : const int16_t Ngq,
1696 : const int16_t gqlevs,
1697 : const int16_t gqbits )
1698 : {
1699 : int16_t i, k, imin;
1700 : float Eyy, Exy, gamma;
1701 : float dmin, d;
1702 : float gain_table[MAX_GQLEVS];
1703 : Word16 exp_normn, exp_normd;
1704 : Word16 delta_fx, Qdelta;
1705 : Word32 L_delta, L_q;
1706 : Word32 L_temp;
1707 : Word16 gain_table_fx[MAX_GQLEVS];
1708 : Word16 Qgt;
1709 : Word16 temp_lo_fx, temp_hi_fx;
1710 :
1711 : #define WMC_TOOL_SKIP
1712 : /* Fine gain quantization on only the most significant energy bands */
1713 : /*delta = qint / gqlevs; */
1714 34 : exp_normn = norm_l( L_qint );
1715 34 : exp_normn = sub( exp_normn, 1 );
1716 34 : exp_normd = norm_s( gqlevs );
1717 34 : delta_fx = div_l( L_shl( L_qint, exp_normn ), shl( gqlevs, exp_normd ) );
1718 34 : Qdelta = add( sub( exp_normn, exp_normd ), 28 ); /* 29+exp_normn-(exp_normd)-1; */
1719 34 : L_delta = L_shl( L_deposit_h( delta_fx ), sub( 13, Qdelta ) );
1720 : /*q = (-qint + delta) / 2.0f; */
1721 34 : L_q = L_shr( L_sub( L_delta, L_qint ), 1 );
1722 :
1723 102 : FOR( i = 0; i < gqlevs; i++ )
1724 : {
1725 : /*gain_table[i] = (float) pow (2.0f, q * 0.5f); */
1726 68 : L_temp = L_shr( L_shr( L_q, 1 ), sub( 29, 16 ) );
1727 68 : temp_lo_fx = L_Extract_lc( L_temp, &temp_hi_fx );
1728 68 : Qgt = sub( 14, temp_hi_fx );
1729 68 : gain_table_fx[i] = extract_l( Pow2( 14, temp_lo_fx ) ); /* Qgt */
1730 :
1731 : /*q += delta; */
1732 68 : L_q = L_add( L_q, L_delta );
1733 68 : gain_table_fx[i] = shl( gain_table_fx[i], sub( 14, Qgt ) ); /* Qgt -> Q14 */
1734 68 : gain_table[i] = (float) ( gain_table_fx[i] / pow( 2.0f, 14 ) );
1735 : }
1736 : #undef WMC_TOOL_SKIP
1737 :
1738 170 : for ( k = bands - Ngq; k < bands; k++ )
1739 : {
1740 136 : Eyy = 0.0f;
1741 136 : Exy = 0.0f;
1742 2462 : for ( i = band_start[k_sort[k]]; i <= band_end[k_sort[k]]; i++ )
1743 : {
1744 2326 : Eyy += y2[i] * y2[i];
1745 2326 : Exy += ybuf[i] * y2[i];
1746 : }
1747 :
1748 136 : if ( Eyy > 0.0f && Exy > 0.0f )
1749 : {
1750 136 : gamma = Exy / Eyy;
1751 136 : dmin = FLT_MAX;
1752 136 : imin = -1;
1753 408 : for ( i = 0; i < gqlevs; i++ )
1754 : {
1755 272 : d = (float) fabs( gamma - gain_table[i] );
1756 272 : if ( d < dmin )
1757 : {
1758 166 : dmin = d;
1759 166 : imin = i;
1760 : }
1761 : }
1762 :
1763 136 : gamma = gain_table[imin];
1764 :
1765 2462 : for ( i = band_start[k_sort[k]]; i <= band_end[k_sort[k]]; i++ )
1766 : {
1767 2326 : y2[i] *= gamma;
1768 : }
1769 : }
1770 : else
1771 : {
1772 0 : imin = 0;
1773 : }
1774 :
1775 136 : push_indice( hBstr, IND_HQ2_SUBBAND_GAIN, imin, gqbits );
1776 : }
1777 :
1778 34 : return;
1779 : }
|