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 : #ifdef BASOP_NOGLOB
340 778 : 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 778 : Ep[i] = (float) ( Ep_fx[i] / pow( 2.0, -6 ) );
345 : }
346 :
347 812 : FOR( i = 0; i < bands; i++ )
348 : {
349 778 : L_tmp2 = Ep_fx[i];
350 778 : L_tmp = L_max( 1, L_tmp2 );
351 778 : exp = norm_l( L_tmp );
352 778 : tmp = extract_h( L_shl( L_tmp, exp ) );
353 :
354 778 : L_tmp3 = (Word32) band_width[i];
355 778 : exp2 = norm_l( L_tmp3 );
356 778 : tmp2 = extract_h( L_shl( L_tmp3, exp2 ) );
357 :
358 778 : exp2 = sub( exp, exp2 ); /* Denormalize and substract */
359 :
360 778 : tmp3 = sub( tmp2, tmp );
361 778 : IF( tmp3 > 0 )
362 : {
363 298 : tmp2 = shr( tmp2, 1 );
364 : }
365 778 : IF( tmp3 > 0 )
366 : {
367 298 : exp2 = add( exp2, 1 );
368 : }
369 778 : tmp = div_s( tmp2, tmp );
370 778 : L_tmp = L_deposit_h( tmp );
371 778 : L_tmp = Isqrt_lc1( L_tmp, &exp2 );
372 778 : move32(); /*Q(31-exp2) */
373 778 : Ep_tmp_fx[i] = L_shr( L_tmp, sub( 15, exp2 ) ); /*Q13 */
374 : }
375 : #undef WMC_TOOL_SKIP
376 :
377 34 : if ( is_transient == 0 && inner_frame == L_FRAME8k && st->core_brate <= ACELP_13k20 )
378 : {
379 : #define WMC_TOOL_SKIP
380 0 : lowband = 6;
381 0 : move16();
382 0 : trans_bit = 2;
383 0 : move16();
384 0 : bit_budget = sub( bit_budget, trans_bit );
385 0 : gama_fx = 27852; /*Q15 0.85f; */
386 0 : beta_fx = 17203;
387 0 : move16(); /*Q14 1.05f; */
388 0 : set_s( &p2a_flags_tmp[bands - trans_bit], 0, 2 );
389 :
390 0 : IF( st->core_brate == ACELP_13k20 )
391 : {
392 0 : beta_fx = 13107;
393 0 : move16(); /*14 1.25f; */
394 0 : gama_fx = 31130;
395 0 : move16(); /*0.95f; */
396 0 : 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 0 : Ep_vari_fx = 0;
401 0 : move32();
402 0 : Ep_avrg_fx = 0;
403 0 : move32();
404 0 : Ep_avrgL_fx = 0;
405 0 : move32();
406 0 : Ep_peak_fx = 0;
407 0 : move32();
408 0 : FOR( i = 0; i < bands; i++ )
409 : {
410 0 : IF( sub( i, lowband ) >= 0 )
411 : {
412 0 : Ep_vari_fx = L_add( Ep_vari_fx, L_abs( L_sub( Ep_tmp_fx[i], Ep_tmp_fx[sub( i, 1 )] ) ) ); /*Q15 */
413 0 : Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
414 : }
415 : ELSE
416 : {
417 : #ifdef BASOP_NOGLOB
418 0 : 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 0 : IF( L_sub( Ep_tmp_fx[i], Ep_peak_fx ) > 0 )
423 : {
424 0 : Ep_peak_fx = Ep_tmp_fx[i];
425 0 : move32(); /*Q15 */
426 : }
427 : }
428 : }
429 : /* modify the last p2a_bands subbands band_energies */
430 0 : k = bands;
431 0 : mvl2l( L_band_energy, L_band_energy_tmp, k ); /*Q_band_energy */
432 0 : Mpy_32_16_ss( Ep_peak_fx, 24576, &L_tmp, &lo );
433 0 : Mpy_32_16_ss( Ep_peak_fx, shl( sub( bands, lowband ), 9 ), &L_tmp2, &lo );
434 0 : Mpy_32_16_ss( Ep_avrg_fx, 1126, &L_tmp3, &lo );
435 :
436 0 : 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 0 : FOR( i = lowband; i < bands; i++ )
440 : {
441 0 : Mpy_32_16_ss( Ep_avrg_fx, 24576, &L_tmp, &lo );
442 0 : IF( L_sub( L_shr( Ep_tmp_fx[i], 1 ), L_tmp ) < 0 )
443 : {
444 0 : Mpy_32_16_ss( Ep_peak_fx, sub( bands, lowband ), &L_tmp, &lo );
445 0 : tmp = extract_h( L_shl( L_tmp, 14 ) ); /*Q-4 */
446 0 : IF( tmp != 0 )
447 : {
448 0 : exp = norm_s( tmp );
449 0 : tmp = shl( tmp, exp ); /*Q(exp) */
450 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
451 0 : 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 0 : Mpy_32_16_ss( Ep_avrg_fx, tmp, &L_tmp, &lo );
462 0 : L_tmp = L_shl( L_tmp, sub( 13, exp ) ); /*Q(13+exp-15 +13-exp +4 = 15) */
463 0 : L_tmp2 = L_add( L_tmp, 13107 ); /*15 */
464 0 : tmp2 = extract_l( L_min( L_max( L_tmp2, 16384 ), gama_fx ) ); /*15 = 15 */
465 0 : Mpy_32_16_ss( L_band_energy_tmp[i], tmp2, &L_band_energy_tmp[i], &lo );
466 : }
467 : }
468 : }
469 : ELSE
470 : {
471 0 : j = 0;
472 0 : FOR( i = sub( bands, trans_bit ); i < bands; i++ )
473 : {
474 0 : alpha_fx = 16384;
475 0 : move16(); /*Q14 */
476 0 : 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 0 : IF( sub( hHQ_core->last_bitalloc_max_band[j++], 1 ) == 0 )
522 : {
523 0 : Mpy_32_16_ss( Ep_tmp_fx[i], sub( bands, lowband ), &L_tmp, &lo );
524 0 : tmp = extract_h( L_shl( L_tmp, 14 ) ); /*Q-2 */
525 0 : IF( tmp != 0 )
526 : {
527 0 : exp = norm_s( tmp );
528 0 : tmp = shl( tmp, exp ); /*Q(exp) */
529 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
530 0 : exp = sub( 29, exp );
531 : }
532 : ELSE
533 : {
534 0 : tmp = 0x7fff;
535 0 : move16();
536 0 : exp = 0;
537 0 : move16();
538 : }
539 0 : 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 0 : L_tmp = L_shl_o( L_tmp, sub( 14, exp ), &Overflow ); /*Q(13+exp-15 +14-exp+2 = 14) */
547 0 : L_tmp = L_max( L_tmp, 16384 ); /*14 */
548 0 : tmp = extract_l( L_min( L_tmp, beta_fx ) ); /*14 */
549 0 : alpha_fx = shl( mult( alpha_fx, tmp ), 1 ); /*14+14-15 +1=14 */
550 : #endif /* BASOP_NOGLOB */
551 : }
552 : ELSE
553 : {
554 0 : tmp2 = extract_h( Ep_avrg_fx ); /*13 -16 =-3 */
555 0 : IF( tmp2 != 0 )
556 : {
557 0 : exp = norm_s( tmp2 );
558 0 : tmp2 = shl( tmp2, exp ); /*Q(exp) */
559 0 : tmp2 = div_s( 16384, tmp2 ); /*Q(15+14-exp=29-exp) */
560 0 : 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 0 : Mpy_32_16_ss( Ep_tmp_fx[i], tmp2, &L_tmp, &lo );
571 0 : L_tmp = L_shl( L_tmp, sub( 19, exp ) ); /*Q(13+exp-15 +19-exp +3 = 20) */
572 0 : Mpy_32_16_ss( L_tmp, shl( sub( bands, lowband ), 9 ), &L_tmp, &lo );
573 0 : L_tmp = L_max( L_tmp, 13926 ); /*14 */
574 0 : tmp2 = extract_l( L_min( L_tmp, 16384 ) ); /*14 */
575 0 : alpha_fx = shl( mult( alpha_fx, tmp2 ), 1 ); /*14+14-15+1 =14 */
576 : }
577 0 : Mpy_32_16_ss( L_band_energy_tmp[i], alpha_fx, &L_tmp, &lo );
578 0 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q(Q_band_energy+14-15 +1= Q_band_energy) */
579 : }
580 : }
581 0 : lowband = 3;
582 0 : move16();
583 0 : Ep_avrg_fx = 0;
584 0 : move32();
585 0 : Ep_avrgL_fx = 0;
586 0 : move32();
587 0 : Ep_peak_fx = 0;
588 0 : move32();
589 0 : FOR( i = 0; i < bands; i++ )
590 : {
591 0 : IF( sub( i, lowband ) >= 0 )
592 : {
593 : #ifdef BASOP_NOGLOB
594 0 : 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 0 : Ep_avrgL_fx = L_add( Ep_avrgL_fx, L_shr( Ep_tmp_fx[i], 1 ) ); /*Q12 */
602 0 : IF( L_sub( Ep_tmp_fx[i], Ep_peak_fx ) > 0 )
603 : {
604 0 : Ep_peak_fx = Ep_tmp_fx[i];
605 0 : move32(); /*Q13 */
606 : }
607 : }
608 : }
609 0 : Mpy_32_16_ss( Ep_peak_fx, 28262, &L_tmp, &lo );
610 0 : Mpy_32_16_ss( Ep_avrgL_fx, 24576, &L_tmp2, &lo );
611 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 )
612 : {
613 0 : adjustFlag = 1;
614 0 : move16();
615 0 : FOR( i = 0; i < lowband; i++ )
616 : {
617 0 : tmp = extract_h( Ep_avrgL_fx ); /*Q-4 */
618 0 : IF( tmp != 0 )
619 : {
620 0 : exp = norm_s( tmp );
621 0 : tmp = shl( tmp, exp ); /*Q(exp) */
622 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
623 0 : 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 0 : Mpy_32_16_ss( Ep_peak_fx, tmp, &L_tmp, &lo );
634 0 : Mpy_32_16_ss( L_tmp, lowband, &L_tmp, &lo );
635 0 : 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 0 : L_tmp = L_shl_o( L_tmp, sub( 27, exp ), &Overflow ); /*Q14 0.5 */
641 0 : tmp2 = extract_l( L_min( L_tmp, 19661 ) ); /*14 */
642 : #endif /* BASOP_NOGLOB */
643 0 : Mpy_32_16_ss( L_band_energy_tmp[i], tmp2, &L_tmp, &lo );
644 0 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q_band_energy */
645 : }
646 : }
647 : #undef WMC_TOOL_SKIP
648 :
649 0 : for ( i = 0; i < bands; i++ )
650 : {
651 0 : band_energy_tmp[i] = (float) ( L_band_energy_tmp[i] / pow( 2.0f, SWB_BWE_LR_Qbe ) );
652 : }
653 :
654 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 );
655 :
656 : /* encode the last p2a_bands-1 subbands bit-allocation index of the previous frame */
657 0 : for ( i = 0; i < 2; i++ )
658 : {
659 0 : push_indice( hBstr, IND_HQ2_LAST_BA_MAX_BAND, hHQ_core->last_bitalloc_max_band[i], 1 );
660 : }
661 : }
662 34 : else if ( is_transient == 0 && inner_frame == L_FRAME16k )
663 : {
664 : #define WMC_TOOL_SKIP
665 0 : bit_budget = sub( bit_budget, 2 ); /* bits in high bands to indicate the last 2 subbands is allocated bits or not */
666 0 : 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 0 : Ep_tmp_fx[i] = L_shl_o( Ep_tmp_fx[i], 2, &Overflow );
672 : #endif /* BASOP_NOGLOB */
673 : }
674 0 : IF( st->core_brate == ACELP_13k20 )
675 : {
676 0 : lowband = 8;
677 0 : move16();
678 0 : highband = 15;
679 0 : move16();
680 0 : bw_low = sub( band_start[highband], band_start[lowband] );
681 0 : bw_high = sub( add( band_end[sub( bands, 1 )], 1 ), band_start[highband] );
682 : }
683 : ELSE
684 : {
685 0 : lowband = 8;
686 0 : move16();
687 0 : highband = 16;
688 0 : move16();
689 0 : bw_low = sub( band_start[highband], band_start[lowband] );
690 0 : 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 0 : enerL_fx = 0;
694 0 : move32();
695 0 : enerH_fx = 0;
696 0 : move32();
697 0 : Ep_vari_fx = 0;
698 0 : move32();
699 0 : Ep_avrg_fx = 0;
700 0 : move32();
701 0 : FOR( i = 0; i < bands; i++ )
702 : {
703 0 : IF( sub( i, lowband ) >= 0 && add( sub( i, bands ), p2a_bands ) < 0 )
704 : {
705 0 : 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 0 : Ep_avrg_fx = L_add_o( Ep_avrg_fx, Ep_tmp_fx[i], &Overflow ); /*Q15 */
710 : #endif /* BASOP_NOGLOB */
711 : }
712 :
713 0 : IF( sub( i, highband ) >= 0 )
714 : {
715 0 : enerH_fx = L_add( enerH_fx, L_shl( Ep_fx[i], 2 ) ); /*Q0 */
716 : }
717 0 : ELSE IF( sub( i, lowband ) >= 0 )
718 : {
719 0 : enerL_fx = L_add( enerL_fx, L_shl( Ep_fx[i], 2 ) ); /*Q0 */
720 : }
721 : }
722 0 : enerL = (float) ( enerL_fx / pow( 2.0, -4 ) );
723 0 : enerH = (float) ( enerH_fx / pow( 2.0, -4 ) );
724 : /* modify the last p2a_bands subbands band_energies */
725 0 : k = bands;
726 0 : mvl2l( L_band_energy, L_band_energy_tmp, k ); /*Q_band_energy */
727 :
728 0 : L_tmp = L_max( enerH_fx, enerL_fx );
729 0 : tmp = s_max( bw_low, bw_high );
730 0 : i = norm_l( L_tmp );
731 0 : j = norm_s( tmp );
732 0 : Mpy_32_16_ss( L_shl( enerH_fx, i ), shl( bw_low, j ), &L_tmp, &lo );
733 0 : Mpy_32_16_ss( L_shl( enerL_fx, i ), shl( bw_high, j ), &L_tmp2, &lo );
734 0 : L_tmp2 = L_sub( L_tmp, L_tmp2 );
735 :
736 0 : FOR( i = sub( bands, p2a_bands ); i < bands; i++ )
737 : {
738 0 : IF( sub( p2a_flags[i], 1 ) == 0 || L_tmp2 > 0 )
739 : {
740 0 : tmp = sub( bands, p2a_bands );
741 0 : tmp = sub( tmp, lowband ); /*Q0 */
742 :
743 : #ifndef BASOP_NOGLOB
744 : tmp1 = extract_h( L_shl( Ep_avrg_fx, 1 ) ); /*Q0 */
745 : #else
746 0 : tmp1 = extract_h( L_shl_o( Ep_avrg_fx, 1, &Overflow ) ); /*Q0 */
747 : #endif
748 0 : IF( tmp1 != 0 )
749 : {
750 0 : exp = norm_s( tmp1 );
751 0 : tmp1 = shl( tmp1, exp ); /*Q(exp) */
752 0 : tmp1 = div_s( 16384, tmp1 ); /*Q(15+14-exp = 29-exp) */
753 0 : exp = sub( 29, exp );
754 : }
755 : ELSE
756 : {
757 0 : tmp1 = 0x7fff;
758 0 : move16();
759 0 : exp = 0;
760 0 : move16();
761 : }
762 0 : Mpy_32_16_ss( Ep_tmp_fx[i], tmp1, &L_tmp, &lo );
763 0 : Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
764 0 : Mpy_32_16_ss( L_tmp, 16384, &L_tmp, &lo );
765 0 : L_tmp = L_shl( L_tmp, sub( 32, exp ) ); /*Q15 */
766 0 : tmp = extract_l( L_min( L_tmp, 6554 ) ); /*Q15 */
767 0 : Mpy_32_16_ss( Ep_vari_fx, tmp1, &L_tmp, &lo );
768 0 : Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
769 0 : L_tmp = L_shl( L_tmp, sub( 15, exp ) ); /*Q15 */
770 0 : tmp = extract_l( L_shr( L_min( L_tmp, 13107 ), 1 ) ); /*Q14 */
771 0 : alpha_fx = add( tmp, 16384 ); /*Q14 */
772 : }
773 : ELSE
774 : {
775 0 : alpha_fx = 16384;
776 0 : move16(); /*Q14 */
777 : }
778 :
779 0 : IF( add( sub( i, bands ), p2a_bands ) > 0 )
780 : {
781 0 : tmp = sub( bands, p2a_bands );
782 0 : IF( sub( hHQ_core->last_bitalloc_max_band[sub( i, add( tmp, 1 ) )], 1 ) == 0 )
783 : {
784 0 : tmp = sub( tmp, lowband );
785 0 : 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 0 : tmp = extract_h( L_shl_o( L_tmp, 16, &Overflow ) ); /*Q0 */
790 : #endif /* BASOP_NOGLOB */
791 0 : IF( tmp != 0 )
792 : {
793 0 : exp = norm_s( tmp );
794 0 : tmp = shl( tmp, exp ); /*Q(exp) */
795 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
796 0 : exp = sub( 29, exp );
797 : }
798 : ELSE
799 : {
800 0 : tmp = 0x7fff;
801 0 : move16();
802 0 : exp = 0;
803 0 : move16();
804 : }
805 0 : Mpy_32_16_ss( Ep_avrg_fx, tmp, &L_tmp, &lo );
806 0 : L_tmp = L_shl( L_tmp, sub( 14, exp ) ); /*Q14 */
807 0 : tmp = extract_l( L_min( L_max( L_tmp, 16384 ), 20480 ) ); /*Q14 */
808 0 : L_tmp = L_mult( alpha_fx, tmp ); /*Q(14+14+1=29) */
809 0 : alpha_fx = extract_l( L_shr( L_tmp, 15 ) ); /*Q14*/
810 : }
811 : ELSE
812 : {
813 0 : tmp = sub( tmp, lowband );
814 :
815 : #ifndef BASOP_NOGLOB
816 : tmp1 = extract_h( L_shl( Ep_avrg_fx, 1 ) ); /*Q0 */
817 : #else
818 0 : tmp1 = extract_h( L_shl_o( Ep_avrg_fx, 1, &Overflow ) ); /*Q0 */
819 : #endif
820 0 : IF( tmp1 != 0 )
821 : {
822 0 : exp = norm_s( tmp1 );
823 0 : tmp1 = shl( tmp1, exp ); /*Q(exp) */
824 0 : tmp1 = div_s( 16384, tmp1 ); /*Q(15+14-exp=29-exp) */
825 0 : exp = sub( 29, exp );
826 : }
827 : ELSE
828 : {
829 0 : tmp1 = 0x7fff;
830 0 : move16();
831 0 : exp = 0;
832 0 : move16();
833 : }
834 0 : Mpy_32_16_ss( Ep_tmp_fx[i], tmp1, &L_tmp, &lo );
835 0 : Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
836 0 : L_tmp = L_shl( L_tmp, sub( 29, exp ) ); /*Q14 */
837 0 : tmp = extract_l( L_min( L_max( L_tmp, 13926 ), 16384 ) ); /*Q14 */
838 0 : L_tmp = L_mult( alpha_fx, tmp ); /*Q(14+14+1=29) */
839 0 : alpha_fx = extract_l( L_shr( L_tmp, 15 ) ); /*Q14 */
840 : }
841 : }
842 0 : Mpy_32_16_ss( L_band_energy_tmp[i], alpha_fx, &L_tmp, &lo );
843 0 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q Q_band_energy */
844 : }
845 0 : lowband = 6;
846 0 : move16();
847 0 : Ep_avrg_fx = 0;
848 0 : move32();
849 0 : Ep_avrgL_fx = 0;
850 0 : move32();
851 0 : Ep_peak_fx = 0;
852 0 : move32();
853 0 : FOR( i = 0; i < bands; i++ )
854 : {
855 0 : 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 0 : 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 0 : Ep_avrgL_fx = L_add_o( Ep_avrgL_fx, Ep_tmp_fx[i], &Overflow ); /*Q15 */
869 : #endif /* BASOP_NOGLOB */
870 0 : IF( L_sub( Ep_tmp_fx[i], Ep_peak_fx ) > 0 )
871 : {
872 0 : Ep_peak_fx = Ep_tmp_fx[i];
873 0 : move32(); /*Q15 */
874 : }
875 : }
876 : }
877 :
878 0 : Mpy_32_16_ss( Ep_peak_fx, 24576, &L_tmp, &lo );
879 0 : Mpy_32_16_ss( Ep_peak_fx, 19661, &L_tmp2, &lo );
880 0 : Mpy_32_16_ss( Ep_avrgL_fx, 24576, &L_tmp3, &lo );
881 :
882 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 ) ||
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 0 : adjustFlag = 1;
886 0 : move16();
887 0 : 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 0 : tmp = extract_h( L_shl_o( Ep_avrgL_fx, 1, &Overflow ) ); /*Q0 */
893 : #endif
894 0 : IF( tmp != 0 )
895 : {
896 0 : exp = norm_s( tmp );
897 0 : tmp = shl( tmp, exp ); /*Q(exp) */
898 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
899 0 : exp = sub( 29, exp );
900 : }
901 : ELSE
902 : {
903 0 : tmp = 0x7fff;
904 0 : move16();
905 0 : exp = 0;
906 0 : move16();
907 : }
908 0 : Mpy_32_16_ss( Ep_peak_fx, tmp, &L_tmp, &lo );
909 0 : Mpy_32_16_ss( L_tmp, lowband, &L_tmp, &lo );
910 0 : L_tmp = L_shl( L_tmp, sub( 28, exp ) ); /*Q14 0.5 */
911 0 : tmp = extract_l( L_min( L_tmp, 19661 ) ); /*Q14 */
912 0 : Mpy_32_16_ss( L_band_energy_tmp[i], tmp, &L_tmp, &lo );
913 0 : L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q_band_energy */
914 : }
915 : }
916 : #undef WMC_TOOL_SKIP
917 :
918 0 : for ( i = 0; i < bands; i++ )
919 : {
920 0 : band_energy_tmp[i] = (float) ( L_band_energy_tmp[i] / pow( 2.0f, SWB_BWE_LR_Qbe ) );
921 : }
922 :
923 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 );
924 :
925 : /* encode the last p2a_bands-1 subbands bit-allocation index of the previous frame */
926 0 : for ( i = 0; i < 2; i++ )
927 : {
928 0 : push_indice( hBstr, IND_HQ2_LAST_BA_MAX_BAND, hHQ_core->last_bitalloc_max_band[i], 1 );
929 : }
930 : }
931 34 : 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 0 : 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 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 );
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 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 );
947 : #endif
948 :
949 34 : if ( ( inner_frame == L_FRAME8k && st->core_brate <= ACELP_13k20 ) || inner_frame == L_FRAME16k )
950 : {
951 0 : j = 0;
952 0 : for ( i = 2; i > 0; i-- )
953 : {
954 0 : if ( npulses[bands - i] > 0 )
955 : {
956 0 : hHQ_core->last_bitalloc_max_band[j] = 1;
957 : }
958 : else
959 : {
960 0 : hHQ_core->last_bitalloc_max_band[j] = 0;
961 : }
962 0 : j++;
963 : }
964 : }
965 :
966 : /* Prepare floating Rk for next modules */
967 812 : for ( k = 0; k < bands; k++ )
968 : {
969 778 : Rk[k] = WORD322FL_SCALE( Rk_fx[k], SWB_BWE_LR_QRk - 1 );
970 : }
971 :
972 : /* Denormalize the coded MDCT spectrum */
973 34 : 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 34 : 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 34 : if ( flag_spt == 1 )
980 : {
981 31 : 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 34 : ni_seed = npulses[0] + npulses[1] + npulses[2] + npulses[3];
986 :
987 19346 : for ( i = 0; i < band_end[bands - 1] + 1; i++ )
988 19312 : y2_ni[i] = y2[i];
989 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 );
990 :
991 34 : if ( st->bwidth == SWB && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
992 : {
993 34 : if ( hqswb_clas == HQ_NORMAL || hqswb_clas == HQ_HARMONIC )
994 : {
995 31 : preset_hq2_swb( hqswb_clas, band_end, &har_bands, p2a_bands, length, bands, &lowlength, &highlength, m );
996 :
997 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 );
998 :
999 31 : post_hq2_swb( m, lowlength, highlength, hqswb_clas, har_bands, bands, p2a_flags, band_start, band_end, y2, npulses );
1000 :
1001 31 : if ( hqswb_clas == HQ_NORMAL )
1002 : {
1003 31 : spt_swb_peakpos_tmp_save( y2, bands, band_start, band_end, prev_SWB_peak_pos_tmp );
1004 155 : for ( k = 0; k < SPT_SHORTEN_SBNUM; k++ )
1005 : {
1006 124 : if ( p2a_flags[bands - SPT_SHORTEN_SBNUM + k] == 0 || npulses[bands - SPT_SHORTEN_SBNUM + k] == 0 )
1007 : {
1008 122 : prev_SWB_peak_pos_tmp[k] = 0;
1009 : }
1010 : }
1011 : }
1012 :
1013 31 : mvr2r( y2_ni, y2, lowlength );
1014 : }
1015 : else
1016 : {
1017 3 : mvr2r( y2_ni, y2, band_end[bands - 1] + 1 ); /* HQ_TRANSIENT */
1018 : }
1019 : }
1020 : else
1021 : {
1022 0 : mvr2r( y2_ni, y2, band_end[bands - 1] + 1 ); /* NB, WB */
1023 : }
1024 :
1025 :
1026 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 );
1027 :
1028 34 : if ( st->bwidth != SWB )
1029 : {
1030 : /* reset HQ classifier memories */
1031 0 : hHQ_core->mode_count = 0;
1032 0 : hHQ_core->mode_count1 = 0;
1033 : }
1034 34 : if ( hqswb_clas != HQ_HARMONIC && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) && st->bwidth == SWB )
1035 : {
1036 34 : hHQ_core->prev_frm_index[0] = -1;
1037 34 : hHQ_core->prev_frm_index[1] = -1;
1038 : }
1039 : /* update number of unused bits */
1040 34 : *num_bits = 0;
1041 :
1042 34 : hHQ_core->hvq_hangover = 0;
1043 :
1044 34 : 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 5 : 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 165 : for ( i = 0; i < BANDS; i++ )
1068 : {
1069 160 : difidx[i] = qbidx[i] + LRMDCT_BE_OFFSET;
1070 : }
1071 :
1072 165 : for ( i = 0; i < BANDS; ++i )
1073 : {
1074 160 : 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 5 : if ( flag_pack == 0 )
1083 : {
1084 : /* estimating # of bits */
1085 3 : bits = encode_envelope_indices( hBstr, BANDS, -1, difidx, hLCmode, flag_pack, LOW_RATE_HQ_CORE_TRAN, is_transient );
1086 3 : bits += BITS_DE_FCOMP; /* xx bits for diff. energies + BITS_DE_FCOMP bits for first energies */
1087 : }
1088 : else
1089 : {
1090 2 : bits = 0;
1091 2 : encode_envelope_indices( hBstr, BANDS, -1, difidx, hLCmode, flag_pack, LOW_RATE_HQ_CORE_TRAN, is_transient );
1092 : }
1093 :
1094 5 : 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 61 : 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 61 : difidx[0] = qbidx[0] + DE_OFFSET0;
1120 :
1121 1342 : for ( i = 1; i < BANDS; ++i )
1122 : {
1123 1281 : difidx[i] = qbidx[i] + DE_OFFSET1;
1124 : }
1125 :
1126 1403 : for ( i = 0; i < BANDS; ++i )
1127 : {
1128 1342 : 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 1403 : for ( i = 0; i < BANDS; ++i )
1137 : {
1138 1342 : LSB[i] = difidx[i] & 1;
1139 1342 : difidx[i] >>= 1;
1140 : }
1141 :
1142 : /* Preparing lossless coding input */
1143 61 : if ( flag_pack == 0 )
1144 : {
1145 : /* estimating # of bits */
1146 : /* Encoding MSB bits */
1147 31 : bits = encode_envelope_indices( hBstr, BANDS, -1, difidx, hLCmode, flag_pack, LOW_RATE_HQ_CORE, is_transient );
1148 31 : bits += BITS_DE_FCOMP; /* xx bits for diff. energies + BITS_DE_FCOMP bits for first energies */
1149 :
1150 : /* Encoding LSB bit packing */
1151 31 : bits += BANDS;
1152 : }
1153 : else
1154 : {
1155 : /* Encoding MSB bits */
1156 30 : bits = 0;
1157 30 : encode_envelope_indices( hBstr, BANDS, -1, difidx, hLCmode, flag_pack, LOW_RATE_HQ_CORE, is_transient );
1158 :
1159 : /* Encoding LSB bit packing */
1160 690 : for ( i = 0; i < BANDS; ++i )
1161 : {
1162 660 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, LSB[i], BITS_DE_LSB );
1163 : }
1164 : }
1165 :
1166 61 : 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 36 : 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 36 : 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 36 : cnt_outlyer0 = 0;
1196 36 : cnt_outlyer = 0;
1197 36 : bitsmode0 = 0;
1198 36 : bitsmode1 = 0;
1199 36 : pos_outlyer = 0;
1200 36 : lsbdepth1 = 0;
1201 :
1202 36 : if ( flag_pack == 0 || ( flag_pack == 1 && *hLCmode0 == 0 ) )
1203 : {
1204 35 : if ( qbidx[0] > ABS_ENG_OFFSET - 1 || qbidx[0] < -ABS_ENG_OFFSET )
1205 : {
1206 0 : cnt_outlyer0 = 2;
1207 : }
1208 35 : else if ( qbidx[0] > 3 || qbidx[0] < -4 )
1209 : {
1210 28 : cnt_outlyer0 = 1;
1211 : }
1212 : else
1213 : {
1214 7 : cnt_outlyer0 = 0;
1215 : }
1216 :
1217 35 : cnt_outlyer = 0;
1218 35 : pos_outlyer = -1;
1219 810 : for ( i = 1; i < BANDS; ++i )
1220 : {
1221 775 : if ( qbidx[i] > 3 || qbidx[i] < -4 )
1222 : {
1223 210 : cnt_outlyer++;
1224 210 : pos_outlyer = i;
1225 : }
1226 :
1227 775 : if ( qbidx[i] > ABS_ENG_OFFSET - 1 || qbidx[i] < -ABS_ENG_OFFSET )
1228 : {
1229 0 : cnt_outlyer++;
1230 : }
1231 : }
1232 :
1233 35 : if ( cnt_outlyer0 == 0 && cnt_outlyer <= 1 )
1234 : {
1235 5 : bitsmode0 = BITS_DE_8SMODE + BITS_DE_8SMODE_N0 + BITS_DE_8SMODE_N1;
1236 5 : if ( cnt_outlyer == 1 )
1237 : {
1238 : /* 01 */
1239 1 : bitsmode0 += BITS_DE_8SPOS + BITS_ABS_ENG;
1240 : }
1241 :
1242 6 : for ( i = 0; i < pos_outlyer; ++i )
1243 : {
1244 1 : tdifidx0[i] = qbidx[i];
1245 1 : bitsmode0 += hessize[tdifidx0[i] + 4];
1246 : }
1247 :
1248 153 : for ( i = pos_outlyer + 1; i < BANDS; ++i )
1249 : {
1250 148 : tdifidx0[i] = qbidx[i];
1251 148 : bitsmode0 += hessize[tdifidx0[i] + 4];
1252 : }
1253 : }
1254 30 : else if ( cnt_outlyer0 == 1 && cnt_outlyer <= 1 )
1255 : {
1256 2 : bitsmode0 = BITS_DE_8SMODE + BITS_DE_8SMODE_N0 + BITS_DE_8SMODE_N1;
1257 2 : tdifidx0[0] = qbidx[0];
1258 2 : bitsmode0 += BITS_ABS_ENG;
1259 2 : if ( cnt_outlyer == 1 )
1260 : {
1261 : /* 11 */
1262 2 : bitsmode0 += BITS_DE_8SPOS + BITS_ABS_ENG;
1263 : }
1264 : else
1265 : {
1266 0 : pos_outlyer = 0;
1267 : }
1268 :
1269 3 : for ( i = 1; i < pos_outlyer; ++i )
1270 : {
1271 1 : tdifidx0[i] = qbidx[i];
1272 1 : bitsmode0 += hessize[tdifidx0[i] + 4];
1273 : }
1274 :
1275 41 : for ( i = pos_outlyer + 1; i < BANDS; ++i )
1276 : {
1277 39 : tdifidx0[i] = qbidx[i];
1278 39 : bitsmode0 += hessize[tdifidx0[i] + 4];
1279 : }
1280 : }
1281 : else
1282 : {
1283 28 : bitsmode0 = 20000;
1284 : }
1285 : }
1286 :
1287 36 : if ( flag_pack == 0 || ( flag_pack == 1 && *hLCmode0 == 1 ) )
1288 : {
1289 : /* components 0 range : -256~255 */
1290 35 : max_q = MINIMUM_ENERGY_LOWBRATE;
1291 35 : min_q = MAXIMUM_ENERGY_LOWBRATE;
1292 835 : for ( i = 0; i < BANDS; ++i )
1293 : {
1294 800 : if ( qbidx[i] > max_q )
1295 : {
1296 127 : max_q = qbidx[i];
1297 : }
1298 :
1299 800 : if ( qbidx[i] < min_q )
1300 : {
1301 54 : min_q = qbidx[i];
1302 : }
1303 : }
1304 :
1305 : /* Counting bits for transmitting all components using same method */
1306 99 : for ( i = 0;; ++i )
1307 : {
1308 99 : if ( max_q <= ( ( 2 << ( i + 1 ) ) - 1 ) && min_q >= -( 2 << ( i + 1 ) ) )
1309 : {
1310 35 : break;
1311 : }
1312 : }
1313 35 : basic_shift = i;
1314 :
1315 35 : min_bits = 1000;
1316 35 : min_bits_pos = basic_shift;
1317 140 : for ( offset0 = basic_shift; offset0 < basic_shift + 3; offset0++ )
1318 : {
1319 105 : max_q = MINIMUM_ENERGY_LOWBRATE;
1320 105 : min_q = MAXIMUM_ENERGY_LOWBRATE;
1321 :
1322 105 : bitsmode1 = BITS_DE_8SMODE + BITS_MAX_DEPTH;
1323 2505 : for ( i = 0; i < BANDS; ++i )
1324 : {
1325 2400 : bitsmode1 += ( hessize[( qbidx[i] >> offset0 ) + 4] + ( offset0 ) );
1326 : }
1327 :
1328 105 : if ( min_bits > bitsmode1 )
1329 : {
1330 45 : min_bits_pos = offset0;
1331 45 : min_bits = bitsmode1;
1332 : }
1333 : }
1334 :
1335 35 : bitsmode1 = min_bits;
1336 35 : lsbdepth1 = min_bits_pos;
1337 :
1338 835 : for ( i = 0; i < BANDS; ++i )
1339 : {
1340 800 : LSB1[i] = qbidx[i] & ( ( 1 << lsbdepth1 ) - 1 );
1341 800 : tdifidx1[i] = qbidx[i] >> lsbdepth1;
1342 : }
1343 : }
1344 :
1345 : /* Preparing lossless coding input */
1346 36 : if ( flag_pack == 0 )
1347 : {
1348 : /* estimating # of bits */
1349 : /* Encoding MSB bits */
1350 34 : if ( bitsmode0 < bitsmode1 )
1351 : {
1352 4 : bits = bitsmode0;
1353 4 : *hLCmode0 = 0;
1354 : }
1355 : else
1356 : {
1357 30 : bits = bitsmode1;
1358 30 : *hLCmode0 = 1;
1359 : }
1360 : }
1361 : else
1362 : {
1363 : /* Encoding MSB bits */
1364 2 : if ( *hLCmode0 == 0 )
1365 : {
1366 1 : push_indice( hBstr, IND_HQ2_DENG_8SMODE, 0, BITS_DE_8SMODE );
1367 1 : bits = BITS_DE_8SMODE;
1368 1 : if ( cnt_outlyer0 == 0 )
1369 : {
1370 1 : push_indice( hBstr, IND_HQ2_DENG_8SMODE_N0, 0, BITS_DE_8SMODE_N0 );
1371 1 : bits += BITS_DE_8SMODE_N0;
1372 1 : if ( cnt_outlyer == 1 )
1373 : {
1374 : /* 01 */
1375 0 : push_indice( hBstr, IND_HQ2_DENG_8SMODE_N1, 1, BITS_DE_8SMODE_N1 );
1376 0 : bits += BITS_DE_8SMODE_N1;
1377 0 : push_indice( hBstr, IND_HQ2_DENG_8SPOS, pos_outlyer, BITS_DE_8SPOS );
1378 0 : bits += BITS_DE_8SPOS;
1379 0 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, qbidx[pos_outlyer] + ABS_ENG_OFFSET, BITS_ABS_ENG );
1380 0 : bits += BITS_ABS_ENG;
1381 : }
1382 : else
1383 : {
1384 : /* 00 */
1385 1 : push_indice( hBstr, IND_HQ2_DENG_8SMODE_N1, 0, BITS_DE_8SMODE_N1 );
1386 1 : bits += BITS_DE_8SMODE_N1;
1387 : }
1388 :
1389 1 : for ( i = 0; i < pos_outlyer; ++i )
1390 : {
1391 0 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, hescode[tdifidx0[i] + 4], hessize[tdifidx0[i] + 4] );
1392 0 : bitsmode0 += hessize[tdifidx0[i] + 4];
1393 : }
1394 :
1395 33 : for ( i = pos_outlyer + 1; i < BANDS; ++i )
1396 : {
1397 32 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, hescode[tdifidx0[i] + 4], hessize[tdifidx0[i] + 4] );
1398 32 : bitsmode0 += hessize[tdifidx0[i] + 4];
1399 : }
1400 : }
1401 0 : else if ( cnt_outlyer0 == 1 )
1402 : {
1403 0 : push_indice( hBstr, IND_HQ2_DENG_8SMODE_N0, 1, BITS_DE_8SMODE_N0 );
1404 0 : bits += BITS_DE_8SMODE_N0;
1405 0 : if ( cnt_outlyer == 1 )
1406 : {
1407 0 : push_indice( hBstr, IND_HQ2_DENG_8SMODE_N1, 1, BITS_DE_8SMODE_N1 );
1408 0 : bits += BITS_DE_8SMODE_N1;
1409 0 : push_indice( hBstr, IND_HQ2_DENG_8SPOS, pos_outlyer, BITS_DE_8SPOS );
1410 0 : bits += BITS_DE_8SPOS;
1411 0 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, qbidx[0] + ABS_ENG_OFFSET, BITS_ABS_ENG );
1412 0 : bits += BITS_ABS_ENG;
1413 0 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, qbidx[pos_outlyer] + ABS_ENG_OFFSET, BITS_ABS_ENG );
1414 0 : bits += BITS_ABS_ENG;
1415 : }
1416 : else
1417 : {
1418 0 : push_indice( hBstr, IND_HQ2_DENG_8SMODE_N1, 0, BITS_DE_8SMODE_N1 );
1419 0 : bits += BITS_DE_8SMODE_N1;
1420 0 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, qbidx[0] + ABS_ENG_OFFSET, BITS_ABS_ENG );
1421 0 : bits += BITS_ABS_ENG;
1422 : }
1423 :
1424 0 : for ( i = 1; i < pos_outlyer; ++i )
1425 : {
1426 0 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, hescode[tdifidx0[i] + 4], hessize[tdifidx0[i] + 4] );
1427 0 : bits += hessize[tdifidx0[i] + 4];
1428 : }
1429 :
1430 0 : for ( i = pos_outlyer + 1; i < BANDS; ++i )
1431 : {
1432 0 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, hescode[tdifidx0[i] + 4], hessize[tdifidx0[i] + 4] );
1433 0 : bits += hessize[tdifidx0[i] + 4];
1434 : }
1435 : }
1436 : }
1437 : else
1438 : {
1439 1 : bits = BITS_DE_8SMODE + BITS_MAX_DEPTH;
1440 1 : push_indice( hBstr, IND_HQ2_DENG_8SMODE, 1, BITS_DE_8SMODE );
1441 1 : push_indice( hBstr, IND_HQ2_DENG_8SDEPTH, lsbdepth1, BITS_MAX_DEPTH );
1442 :
1443 23 : for ( i = 0; i < BANDS; ++i )
1444 : {
1445 22 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, hescode[tdifidx1[i] + 4], hessize[tdifidx1[i] + 4] );
1446 22 : bits += hessize[tdifidx1[i] + 4];
1447 : }
1448 :
1449 1 : if ( lsbdepth1 > 0 )
1450 : {
1451 23 : for ( i = 0; i < BANDS; ++i )
1452 : {
1453 22 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, LSB1[i], lsbdepth1 );
1454 : }
1455 1 : bits += BANDS * lsbdepth1;
1456 : }
1457 : }
1458 : }
1459 :
1460 36 : 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 34 : 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 34 : 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 812 : for ( k = 0; k < bands; k++ )
1498 : {
1499 778 : E = 0.0f;
1500 20090 : for ( i = band_start[k]; i <= band_end[k]; i++ )
1501 : {
1502 19312 : E += t_audio[i] * t_audio[i];
1503 : }
1504 :
1505 778 : band_energy[k] = (float) log2_f( E + 0.18e-1f );
1506 : }
1507 :
1508 34 : if ( is_transient )
1509 : {
1510 3 : reverse_transient_frame_energies( band_energy, bands );
1511 : }
1512 :
1513 : /* Quantize the reference and band energies */
1514 812 : for ( k = 0; k < bands; k++ )
1515 778 : L_band_energy[k] = (Word32) ( band_energy[k] * pow( 2.0f, SWB_BWE_LR_Qbe ) );
1516 :
1517 : #define WMC_TOOL_SKIP
1518 34 : exp_normd = norm_l( L_qint );
1519 34 : rev_qint_fx = div_s( 0x4000, round_fx( L_shl( L_qint, exp_normd ) ) ); /* Q14-(29+exp_normd-16)+15 */
1520 34 : Qrev_qint = sub( 14 - ( 29 - 16 ) + 15, exp_normd );
1521 :
1522 34 : bq0 = round_fx( L_shl( L_mult( eref_fx, rev_qint_fx ), sub( 5, Qrev_qint ) ) ); /* 16-(10+Qrev_qint+1) */
1523 812 : FOR( k = 0; k < bands; k++ )
1524 : {
1525 : /*bq1[k] = round_f (band_energy[k] / qint); */
1526 778 : L_tmp = L_mls( L_band_energy[k], rev_qint_fx ); /* Q14+Qrev_qint-15 */
1527 778 : 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 34 : if ( is_transient )
1532 : {
1533 3 : mvs2s( bq1, bq1_temp, bands );
1534 :
1535 : /* Calculate the differential energies */
1536 3 : diffcod_lrmdct( bands, bq0, bq1_temp, bq2_temp, is_transient );
1537 : }
1538 :
1539 : /* Calculate the differential energies */
1540 34 : bq2[0] = bq1[0] - bq0;
1541 778 : for ( k = 1; k < bands; k++ )
1542 : {
1543 744 : bq2[k] = bq1[k] - bq1[k - 1];
1544 : }
1545 :
1546 : /* Modifying qbidx to be located in the range -256~255 */
1547 812 : for ( i = 0; i < bands; ++i )
1548 : {
1549 778 : if ( bq2[i] > MAXIMUM_ENERGY_LOWBRATE )
1550 : {
1551 0 : bq2[i] = MAXIMUM_ENERGY_LOWBRATE;
1552 : }
1553 778 : 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 34 : ebits = large_symbol_enc( hBstr, bq2, bands, &hLCmode0, 0 );
1561 :
1562 34 : if ( is_transient )
1563 : {
1564 : /* Get number of bits by Huffman coding */
1565 3 : 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 31 : hbits = small_symbol_enc( hBstr, bq2, bands, &hLCmode1, 0, is_transient );
1571 : }
1572 :
1573 : /* comparing used bits */
1574 34 : if ( ebits < hbits || hbits == -1 )
1575 : {
1576 2 : deng_cmode = 0;
1577 2 : push_indice( hBstr, IND_HQ2_DENG_MODE, deng_cmode, BITS_DE_CMODE );
1578 2 : large_symbol_enc( hBstr, bq2, bands, &hLCmode0, 1 );
1579 2 : deng_bits = ebits + BITS_DE_CMODE;
1580 : }
1581 : else
1582 : {
1583 : /* setting energy difference coding mode and storing it */
1584 32 : deng_cmode = 1;
1585 32 : push_indice( hBstr, IND_HQ2_DENG_MODE, deng_cmode, BITS_DE_CMODE );
1586 :
1587 32 : deng_bits = hbits + BITS_DE_CMODE;
1588 :
1589 : /* packing indice */
1590 32 : if ( is_transient )
1591 : {
1592 2 : mvs2s( bq2_temp, bq2, bands );
1593 :
1594 2 : small_symbol_enc_tran( hBstr, bq2, bands, &hLCmode1, 1, is_transient );
1595 : }
1596 : else
1597 : {
1598 30 : small_symbol_enc( hBstr, bq2, bands, &hLCmode1, 1, is_transient );
1599 : }
1600 : }
1601 :
1602 : /* Reconstruct quantized spectrum */
1603 34 : bq1[0] = bq2[0] + bq0;
1604 778 : for ( k = 1; k < bands; k++ )
1605 : {
1606 744 : bq1[k] = bq2[k] + bq1[k - 1];
1607 : }
1608 :
1609 812 : for ( k = 0; k < bands; k++ )
1610 : {
1611 : #define WMC_TOOL_SKIP
1612 778 : L_band_energy[k] = L_mls( L_qint, (Word16) bq1[k] );
1613 778 : move32(); /* 29+0-15 -> Qbe(Q14) */
1614 : #undef WMC_TOOL_SKIP
1615 778 : band_energy[k] = (float) ( L_band_energy[k] / pow( 2.0f, SWB_BWE_LR_Qbe ) );
1616 : }
1617 :
1618 34 : if ( is_transient )
1619 : {
1620 3 : reverse_transient_frame_energies( band_energy, bands );
1621 : }
1622 :
1623 34 : return ( deng_bits );
1624 : }
1625 :
1626 :
1627 : /*-------------------------------------------------------------------*
1628 : * p2a_threshold_quant()
1629 : *
1630 : *
1631 : *-------------------------------------------------------------------*/
1632 :
1633 34 : 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 34 : set_s( p2a_flags, 1, bands );
1649 :
1650 34 : j = 0;
1651 189 : for ( k = bands - p2a_bands; k < bands; k++ )
1652 : {
1653 155 : a = 0.0f;
1654 155 : p = 0.0f;
1655 11129 : for ( i = band_start[k]; i <= band_end[k]; i++ )
1656 : {
1657 10974 : e = t_audio[i] * t_audio[i];
1658 10974 : if ( e > p )
1659 : {
1660 815 : p = e;
1661 : }
1662 10974 : a += e;
1663 : }
1664 :
1665 155 : if ( a > 0.0f )
1666 : {
1667 155 : a /= band_width[k];
1668 155 : p2a = 10.0f * (float) log10( p / a );
1669 :
1670 155 : if ( p2a <= p2a_th )
1671 : {
1672 148 : p2a_flags[k] = 0;
1673 : }
1674 : }
1675 :
1676 155 : push_indice( hBstr, IND_HQ2_P2A_FLAGS, p2a_flags[k], 1 );
1677 155 : j++;
1678 : }
1679 :
1680 34 : return ( j );
1681 : }
1682 :
1683 :
1684 : /*-------------------------------------------------------------------*
1685 : * mdct_spectrum_fine_gain_enc()
1686 : *
1687 : *
1688 : *-------------------------------------------------------------------*/
1689 :
1690 34 : 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 34 : exp_normn = norm_l( L_qint );
1719 34 : exp_normn = sub( exp_normn, 1 );
1720 34 : exp_normd = norm_s( gqlevs );
1721 34 : delta_fx = div_l( L_shl( L_qint, exp_normn ), shl( gqlevs, exp_normd ) );
1722 34 : Qdelta = add( sub( exp_normn, exp_normd ), 28 ); /* 29+exp_normn-(exp_normd)-1; */
1723 34 : L_delta = L_shl( L_deposit_h( delta_fx ), sub( 13, Qdelta ) );
1724 : /*q = (-qint + delta) / 2.0f; */
1725 34 : L_q = L_shr( L_sub( L_delta, L_qint ), 1 );
1726 :
1727 102 : FOR( i = 0; i < gqlevs; i++ )
1728 : {
1729 : /*gain_table[i] = (float) pow (2.0f, q * 0.5f); */
1730 68 : L_temp = L_shr( L_shr( L_q, 1 ), sub( 29, 16 ) );
1731 68 : temp_lo_fx = L_Extract_lc( L_temp, &temp_hi_fx );
1732 68 : Qgt = sub( 14, temp_hi_fx );
1733 68 : gain_table_fx[i] = extract_l( Pow2( 14, temp_lo_fx ) ); /* Qgt */
1734 :
1735 : /*q += delta; */
1736 68 : L_q = L_add( L_q, L_delta );
1737 68 : gain_table_fx[i] = shl( gain_table_fx[i], sub( 14, Qgt ) ); /* Qgt -> Q14 */
1738 68 : gain_table[i] = (float) ( gain_table_fx[i] / pow( 2.0f, 14 ) );
1739 : }
1740 : #undef WMC_TOOL_SKIP
1741 :
1742 170 : for ( k = bands - Ngq; k < bands; k++ )
1743 : {
1744 136 : Eyy = 0.0f;
1745 136 : Exy = 0.0f;
1746 2462 : for ( i = band_start[k_sort[k]]; i <= band_end[k_sort[k]]; i++ )
1747 : {
1748 2326 : Eyy += y2[i] * y2[i];
1749 2326 : Exy += ybuf[i] * y2[i];
1750 : }
1751 :
1752 136 : if ( Eyy > 0.0f && Exy > 0.0f )
1753 : {
1754 136 : gamma = Exy / Eyy;
1755 136 : dmin = FLT_MAX;
1756 136 : imin = -1;
1757 408 : for ( i = 0; i < gqlevs; i++ )
1758 : {
1759 272 : d = (float) fabs( gamma - gain_table[i] );
1760 272 : if ( d < dmin )
1761 : {
1762 166 : dmin = d;
1763 166 : imin = i;
1764 : }
1765 : }
1766 :
1767 136 : gamma = gain_table[imin];
1768 :
1769 2462 : for ( i = band_start[k_sort[k]]; i <= band_end[k_sort[k]]; i++ )
1770 : {
1771 2326 : y2[i] *= gamma;
1772 : }
1773 : }
1774 : else
1775 : {
1776 0 : imin = 0;
1777 : }
1778 :
1779 136 : push_indice( hBstr, IND_HQ2_SUBBAND_GAIN, imin, gqbits );
1780 : }
1781 :
1782 34 : return;
1783 : }
|