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