Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 : #include <stdint.h>
5 : #include "options.h" /* Compilation switches */
6 : #include "cnst.h" /* Common constants */
7 : #include "rom_com.h" /* Static table prototypes */
8 : #include "prot_fx.h" /* Function prototypes */
9 : #include "assert.h" /* Debug prototypes */
10 : #include "stl.h"
11 : #include "ivas_prot_fx.h"
12 :
13 : static void reajust_bits_fx( Word32 *bits_per_bands, const Word16 st_band, const Word16 end_band, const Word16 sum_bit_in, const Word16 bit_bdgt_in );
14 :
15 : /*-------------------------------------------------------------------*
16 : * Local constants
17 : *-------------------------------------------------------------------*/
18 : #define Q15_0_33 10922 /* 0.33 */
19 : #define Q18_0_1 26214 /* 0.1 */
20 : #define Q18_0_50 131072 /* 0.50 */
21 : #define Q18_0_75 196608 /* 0.75 */
22 : #define Q18_0_76 199229 /* 76/100 */
23 : #define Q18_1_0 262144 /* 1.0 */
24 : #define Q18_1_2 314573 /* 1.2 */
25 : #define Q18_112 29360128 /* 112 */
26 : #define Q18_DSR_NB_PULSE 1179648 /* 4.5 */
27 : #define Q18_1_5xDSR_NB_PULSE 1769472 /* 1.5x4.5 */
28 : #define Q18_2_0xDSR_NB_PULSE ( Q18_DSR_NB_PULSE << 1 ) /* 2.0x4.5 */
29 :
30 : #define Q31_0_00125 2684355 /* 0.125/100 */
31 : #define Q31_0_0125 26843546 /* 0.0125 */
32 : #define Q31_0_015 32212255 /* 0.0125 */
33 : #define Q31_0_02 42949673 /* 0.02 */
34 : #define Q31_0_17 365072220 /* 0.17 */
35 : #define Q31_0_23 493921239 /* 0.23 */
36 : /*-----------------------------------------------------------------*
37 : * Local functions
38 : *-----------------------------------------------------------------*/
39 : static Word16 Find_norm_inv_fx( const Word32 ToDivide, Word16 *e_div );
40 : static Word16 Find_bit_alloc_IVAS_fx( const Word32 core_brate, const Word16 GSC_IVAS_mode, const Word16 Diff_len, const Word16 nb_tot_bands, Word16 *bit, Word16 *max_ener_band, Word16 *ener_vec, Word32 *bits_per_bands );
41 : static Word32 Find_bit_frac_fx( const Word16 nb_band, const Word16 remaining_bits );
42 : /*==================================================================================*/
43 : /* FUNCTION : void bands_and_bit_alloc_ivas_fx(); */
44 : /*----------------------------------------------------------------------------------*/
45 : /* PURPOSE : AC mode (GSC) bands and bits allocation */
46 : /*----------------------------------------------------------------------------------*/
47 : /* INPUT ARGUMENTS : */
48 : /* _ (Word16) cor_strong_limit : HF correlation */
49 : /* _ (Word16) noise_lev : dwn scaling factor Q0 */
50 : /* _ (Word32) core_brate : core codec used Q0 */
51 : /* _ (Word16) Diff_len : Lenght of the difference signal Q0 */
52 : /* _ (Word16) bits_used : Number of bit used before frequency Q0 */
53 : /* _ (Word16) idx : Energy band 14 Q0 */
54 : /* _ (Word16*) exc_diff : Difference signal to quantize (Encoder only) */
55 : /* _ (Word16) coder_type : coding type Q0 */
56 : /* _ (Word16) bwidth : input signal bandwidth Q0 */
57 : /*----------------------------------------------------------------------------------*/
58 : /* OUTPUT ARGUMENTS : */
59 : /* _ (Word16*) max_ener_band : Sorted order */
60 : /* _ (Word16*) nb_subbands : Number of subband allowed Q0 */
61 : /* _ (Word16*) concat_in : Concatened PVQ's input vector (Encoder Only) */
62 : /* _ (Word16*) pvq_len : Number of bin covered with the PVQ Q0 */
63 : /*----------------------------------------------------------------------------------*/
64 : /* INPUT/OUTPUT ARGUMENTS : */
65 : /* _ (Word16*) bit :Number of bit allowed for frequency quantization */
66 : /* _ (Word16*) Ener_per_bd_iQ : Quantized energy vector Q12 */
67 : /* _ (Word32*) bits_per_bands : Number of bit allowed per allowed subband Q18 */
68 : /*----------------------------------------------------------------------------------*/
69 : /* RETURN ARGUMENTS : */
70 : /* _ None */
71 : /*==================================================================================*/
72 20560 : void bands_and_bit_alloc_ivas_fx(
73 : const Word16 cor_strong_limit, /* i : HF correlation */
74 : const Word16 noise_lev, /* i : dwn scaling factor */
75 : const Word32 core_brate, /* i : core bit rate */
76 : const Word16 Diff_len, /* i : Lenght of the difference signal (before pure spectral)*/
77 : const Word16 bits_used, /* i : Number of bit used before frequency Q */
78 : Word16 *bit, /* i/o: Number of bit allowed for frequency quantization */
79 : const Word16 *Ener_per_bd_iQ, /* i/o: Quantized energy vector Q12 */
80 : Word16 *max_ener_band, /* o : Sorted order */
81 : Word16 *out_bits_per_bands, /* i/o: Number of bit allowed per allowed subband Q3 */
82 : Word16 *nb_subbands, /* o : Number of subband allowed */
83 : const Word16 *exc_diff, /* i : Difference signal to quantize (encoder side only) */
84 : Word16 *concat_in, /* o : Concatened PVQ's input vector (encoder side only) */
85 : Word16 *pvq_len, /* o : Number of bin covered with the PVQ */
86 : const Word16 coder_type, /* i : coding type */
87 : const Word16 bwidth, /* i : input signal bandwidth */
88 : const Word16 GSC_noisy_speech, /* i : GSC noisy speech flag */
89 : const Word16 L_frame, /* i : frame length */
90 : const Word16 element_mode, /* i : element mode */
91 : const Word16 GSC_IVAS_mode /* i : GSC IVAS mode */
92 : )
93 : {
94 :
95 : Word16 bandoffset, i, j, nb_bands_max, bit_new_bands, bit_tmp, st_band, nb_bands;
96 : Word16 ener_vec[MBANDS_GN_BITALLOC16k]; /*Q12 */
97 20560 : Word16 nb_tot_bands = 16;
98 : Word16 bit_index, bit_index_mem, imax;
99 : Word32 L_tmp;
100 : Word32 sum_bit /*Q0*/, bit_fracf /*Q18*/;
101 : Word16 etmp;
102 : Word16 tmp;
103 : Word16 pos, band;
104 : Word32 SWB_bit_budget; /* Q0 -> Q18 */
105 : Word32 bits_per_bands[MBANDS_GN_BITALLOC16k]; /* Q18 */
106 : Word16 w_sum_bit;
107 : Word16 fzero_val;
108 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
109 20560 : Flag Overflow = 0;
110 : #endif
111 20560 : nb_tot_bands = MBANDS_GN;
112 20560 : move16();
113 20560 : if ( EQ_16( L_frame, L_FRAME16k ) )
114 : {
115 2525 : nb_tot_bands = MBANDS_GN_BITALLOC16k;
116 2525 : move16();
117 : }
118 :
119 20560 : set32_fx( bits_per_bands, 0, MBANDS_GN_BITALLOC16k );
120 20560 : set16_fx( out_bits_per_bands, 0, MBANDS_GN_BITALLOC16k );
121 :
122 : /* To adapt current energy band to PVQ freq band for sorting*/
123 20560 : ener_vec[0] = add_o( Ener_per_bd_iQ[0], Ener_per_bd_iQ[1], &Overflow ); /*Q12 */
124 20560 : move16();
125 20560 : Copy( Ener_per_bd_iQ + 1, ener_vec, MBANDS_GN - 1 ); /*Q12 */
126 20560 : ener_vec[MBANDS_GN - 1] = ener_vec[MBANDS_GN - 2];
127 20560 : move16();
128 20560 : IF( EQ_16( L_frame, L_FRAME16k ) )
129 : {
130 2525 : ener_vec[16] = Ener_per_bd_iQ[16]; /*Q12*/
131 2525 : ener_vec[17] = mac_r( L_mult( Ener_per_bd_iQ[16], 16384 /*0.5 in Q15*/ ), Ener_per_bd_iQ[17], 16384 /*0.5 in Q15*/ ); /*Q12*/
132 2525 : ener_vec[18] = Ener_per_bd_iQ[17]; /*Q12*/
133 2525 : ener_vec[19] = mult_r( Ener_per_bd_iQ[17], 26214 /*0.8 in Q15*/ ); /*Q12*/
134 2525 : move16();
135 2525 : move16();
136 2525 : move16();
137 2525 : move16();
138 : }
139 :
140 : /*------------------------------------------------------------------------
141 : * Determination of the number of bits available to the frequency domain
142 : * Allocation of a maximum number of band to be encoded
143 : *-----------------------------------------------------------------------*/
144 :
145 20560 : nb_bands_max = nb_tot_bands;
146 20560 : move16();
147 20560 : bit_new_bands = 5;
148 20560 : move16();
149 :
150 20560 : test();
151 20560 : if ( GT_32( core_brate, ACELP_16k40 ) && EQ_16( L_frame, L_FRAME16k ) )
152 : {
153 2291 : bit_new_bands = 7;
154 2291 : move16();
155 : }
156 :
157 20560 : i = 0;
158 20560 : move16();
159 96943 : WHILE( LT_16( i, SIZE_BRATE_INTERMED_TBL ) )
160 : {
161 96943 : IF( LE_32( core_brate, brate_intermed_tbl[i] ) )
162 : {
163 20373 : BREAK;
164 : }
165 :
166 76570 : IF( EQ_32( brate_intermed_tbl[i], ACELP_24k40 ) )
167 : {
168 187 : BREAK;
169 : }
170 :
171 76383 : i = add( i, 1 );
172 : }
173 :
174 20560 : test();
175 20560 : test();
176 20560 : test();
177 30333 : if ( ( element_mode > EVS_MONO ) && EQ_16( coder_type, AUDIO ) &&
178 16177 : LE_32( core_brate, STEREO_GSC_BIT_RATE_ALLOC ) && EQ_32( brate_intermed_tbl[i], ACELP_9k60 ) ) /* Bit allocation should be mapped to 8 kb/s instead of 9.6 kb/s in this case */
179 : {
180 1230 : i = sub( i, 1 );
181 : }
182 :
183 20560 : bit_index = i_mult2( BRATE2IDX_fx( brate_intermed_tbl[i] ), 17 );
184 20560 : bit_index_mem = bit_index;
185 20560 : move16();
186 :
187 20560 : test();
188 20560 : test();
189 20560 : IF( ( EQ_16( coder_type, AUDIO ) || ( coder_type == INACTIVE ) ) && ( bwidth == NB ) )
190 : {
191 0 : IF( GE_32( core_brate, ACELP_9k60 ) )
192 : {
193 : /* *bit = (short)(core_brate*(1.0f/50) + 0.5f) - bits_used - 25; */
194 0 : L_tmp = Mult_32_16( core_brate, 20971 /*1/50 in Q20*/ );
195 0 : tmp = extract_l( L_shr_r( L_tmp, 5 ) );
196 0 : *bit = sub( sub( tmp, bits_used ), 25 ); /*Q0*/
197 0 : move16();
198 : }
199 : ELSE
200 : {
201 0 : L_tmp = Mult_32_16( core_brate, 20971 /*1/50 in Q20*/ );
202 0 : tmp = extract_l( L_shr_r( L_tmp, 5 ) );
203 0 : *bit = sub( sub( tmp, bits_used ), 21 ); /*Q0*/
204 0 : move16();
205 : }
206 0 : nb_tot_bands = 10;
207 0 : move16();
208 : }
209 : ELSE
210 : {
211 : /* *bit = (short)(core_brate*(1.0f/50) + 0.5f) - bits_used - GSC_freq_bits[bit_index]; */
212 :
213 20560 : L_tmp = Mult_32_16( core_brate, 20971 /*1/50 in Q20*/ );
214 20560 : tmp = extract_l( L_shr_r( L_tmp, 5 ) );
215 20560 : *bit = sub( sub( tmp, bits_used ), GSC_freq_bits[bit_index] );
216 20560 : move16();
217 : }
218 :
219 20560 : IF( EQ_16( L_frame, L_FRAME16k ) )
220 : {
221 2525 : *bit = sub( *bit, 8 );
222 2525 : move16();
223 : }
224 :
225 20560 : test();
226 20560 : IF( coder_type == INACTIVE && LE_32( core_brate, GSC_LRES_GAINQ_LIMIT ) ) /* can happen only for 2nd channel inactive */
227 : {
228 6314 : *bit = add( *bit, GSC_LRES_NB_NITS );
229 6314 : move16();
230 : }
231 :
232 20560 : IF( *bit > 0 )
233 : {
234 20560 : IF( GSC_IVAS_mode > 0 )
235 : {
236 4128 : nb_tot_bands = Find_bit_alloc_IVAS_fx( core_brate, GSC_IVAS_mode, Diff_len, nb_tot_bands, bit, max_ener_band, ener_vec, bits_per_bands );
237 4128 : nb_bands = nb_tot_bands;
238 : }
239 16432 : ELSE IF( EQ_16( GSC_noisy_speech, 1 ) )
240 : {
241 7458 : SWB_bit_budget = *bit; /*Q0*/
242 7458 : move32();
243 7458 : nb_bands = 5;
244 7458 : move16();
245 7458 : fzero_val = 0;
246 7458 : move16();
247 :
248 7458 : if ( element_mode > EVS_MONO )
249 : {
250 7458 : fzero_val = MIN16B;
251 7458 : move16();
252 : }
253 :
254 7458 : test();
255 7458 : IF( EQ_16( coder_type, UNVOICED ) && element_mode > EVS_MONO )
256 : {
257 27 : nb_bands = 3;
258 27 : move16();
259 :
260 27 : if ( GT_32( SWB_bit_budget, 20 ) )
261 : {
262 27 : nb_bands = 5;
263 27 : move16();
264 : }
265 : }
266 7431 : ELSE IF( LT_16( bwidth, SWB ) )
267 : {
268 6736 : nb_bands = 7;
269 6736 : move16();
270 : }
271 :
272 7458 : st_band = nb_bands;
273 7458 : move16();
274 :
275 7458 : set32_fx( bits_per_bands, 0, MBANDS_GN );
276 : /*bit_fracf = (1.0f/nb_bands)*(SWB_bit_budget); */
277 7458 : bit_fracf = Find_bit_frac_fx( nb_bands, extract_l( SWB_bit_budget ) ); /*Q18*/
278 7458 : nb_tot_bands = sub( nb_bands_max, 6 );
279 7458 : nb_tot_bands = s_min( nb_tot_bands, 16 );
280 :
281 22374 : FOR( j = 0; j < 2; j++ )
282 : {
283 14916 : i = j;
284 14916 : move16();
285 14916 : max_ener_band[j] = i;
286 14916 : move16();
287 14916 : ener_vec[i] = fzero_val;
288 14916 : move16();
289 : }
290 :
291 7458 : IF( LT_16( bwidth, SWB ) )
292 : {
293 6754 : test();
294 6754 : if ( EQ_16( coder_type, UNVOICED ) && element_mode > EVS_MONO )
295 : {
296 18 : nb_tot_bands = 5;
297 18 : move16();
298 : }
299 :
300 40488 : FOR( ; j < nb_bands; j++ )
301 : {
302 33734 : i = maximum_fx( ener_vec, nb_tot_bands, &etmp );
303 33734 : max_ener_band[j] = i;
304 33734 : move16();
305 33734 : ener_vec[i] = fzero_val;
306 33734 : move16();
307 : }
308 : }
309 : ELSE
310 : {
311 2816 : FOR( ; j < nb_bands; j++ )
312 : {
313 2112 : i = maximum_fx( ener_vec, nb_tot_bands, &etmp );
314 2112 : max_ener_band[j] = i;
315 2112 : move16();
316 2112 : ener_vec[i] = fzero_val;
317 2112 : move16();
318 : }
319 : }
320 :
321 7458 : set32_fx( bits_per_bands, bit_fracf, nb_bands );
322 : }
323 : ELSE
324 : {
325 8974 : bit_index = add( bit_index, 1 );
326 8974 : bit_tmp = sub( *bit, GSC_freq_bits[bit_index] );
327 8974 : bit_index = add( bit_index, 1 );
328 8974 : nb_bands_max = add( nb_bands_max, GSC_freq_bits[bit_index] );
329 8974 : bit_index = add( bit_index, 1 );
330 :
331 8974 : *pvq_len = 112;
332 8974 : move16();
333 8974 : st_band = 7;
334 8974 : move16();
335 :
336 8974 : test();
337 8974 : IF( EQ_16( L_frame, L_FRAME16k ) && GT_32( core_brate, ACELP_16k40 ) )
338 : {
339 2291 : *pvq_len = 160;
340 2291 : move16();
341 2291 : st_band = 10;
342 2291 : move16();
343 2291 : nb_bands = shr( *pvq_len, 4 );
344 2291 : bit_tmp = sub( bit_tmp, 35 );
345 2291 : bit_new_bands = 5;
346 2291 : move16();
347 : }
348 :
349 8974 : IF( LE_32( core_brate, ACELP_9k60 ) )
350 : {
351 5218 : *pvq_len = 80;
352 5218 : move16();
353 5218 : st_band = 5;
354 5218 : move16();
355 :
356 5218 : IF( Diff_len == 0 )
357 : {
358 113 : nb_bands_max = add( nb_bands_max, 2 );
359 113 : bit_tmp = sub( bit_tmp, 13 );
360 : }
361 : }
362 :
363 3756 : ELSE IF( Diff_len == 0 )
364 : {
365 1967 : nb_bands_max = add( nb_bands_max, 2 );
366 1967 : bit_tmp = sub( bit_tmp, 17 );
367 : }
368 :
369 8974 : nb_bands = shr( *pvq_len, 4 );
370 8974 : nb_bands_max = s_min( nb_bands_max, MBANDS_GN_BITALLOC16k );
371 :
372 : /*------------------------------------------------------------------------
373 : * Ajustement of the maximum number of bands in function of the
374 : * dynamics of the spectrum (more or less speech like)
375 : *-----------------------------------------------------------------------*/
376 8974 : test();
377 8974 : test();
378 8974 : test();
379 8974 : test();
380 8974 : IF( coder_type == INACTIVE || GE_16( noise_lev, NOISE_LEVEL_SP3 ) )
381 : {
382 : /* Probably classification error -> concentrate bits on LF */
383 5844 : IF( EQ_16( L_frame, L_FRAME16k ) && GE_32( core_brate, ACELP_24k40 ) )
384 : {
385 187 : nb_bands_max = sub( nb_tot_bands, 2 );
386 : }
387 5657 : ELSE IF( GE_32( core_brate, ACELP_16k40 ) )
388 : {
389 2243 : nb_bands_max = add( nb_bands, 2 );
390 : }
391 3414 : ELSE IF( GE_32( core_brate, ACELP_8k00 ) )
392 : {
393 1771 : nb_bands_max = add( nb_bands, 1 );
394 : }
395 : ELSE
396 : {
397 1643 : nb_bands_max = nb_bands;
398 1643 : move16();
399 : }
400 : }
401 3130 : ELSE IF( GE_16( noise_lev, NOISE_LEVEL_SP2 ) ||
402 : ( LE_32( core_brate, ACELP_13k20 ) && GE_32( core_brate, ACELP_9k60 ) && cor_strong_limit == 0 ) ) /* Very low dynamic, tend to speech, do not try to code HF at all */
403 : {
404 3125 : nb_bands_max = sub( nb_bands_max, 2 );
405 : }
406 5 : ELSE IF( GE_16( noise_lev, NOISE_LEVEL_SP1 ) ) /* Very low dynamic, tend to speech, code less HF */
407 : {
408 2 : nb_bands_max = sub( nb_bands_max, 1 );
409 : }
410 :
411 8974 : IF( EQ_16( L_frame, L_FRAME16k ) )
412 : {
413 2525 : IF( LT_32( core_brate, ACELP_24k40 ) )
414 : {
415 2338 : nb_bands_max = sub( nb_bands_max, 4 );
416 : }
417 187 : ELSE IF( LT_32( core_brate, ACELP_32k ) )
418 : {
419 187 : test();
420 187 : IF( GT_16( Diff_len, 0 ) || GE_16( noise_lev, NOISE_LEVEL_SP2 ) )
421 : {
422 187 : nb_bands_max = sub( nb_bands_max, 2 );
423 187 : bit_new_bands = shl( bit_new_bands, 1 );
424 : }
425 : }
426 0 : ELSE IF( core_brate >= ACELP_32k )
427 : {
428 0 : bit_new_bands = shl( bit_new_bands, 1 );
429 : }
430 : }
431 :
432 8974 : test();
433 8974 : if ( ( bwidth == NB ) && GT_16( nb_bands_max, 10 ) )
434 : {
435 0 : nb_bands_max = 10;
436 0 : move16();
437 : }
438 :
439 : /*------------------------------------------------------------------------
440 : * Find extra number of band to code according to bit rate availables
441 : *-----------------------------------------------------------------------*/
442 8974 : test();
443 12566 : WHILE( GE_16( bit_tmp, bit_new_bands ) && LE_16( nb_bands, sub( nb_bands_max, 1 ) ) )
444 : {
445 3592 : test();
446 3592 : bit_tmp = sub( bit_tmp, bit_new_bands );
447 3592 : nb_bands = add( nb_bands, 1 );
448 : }
449 :
450 : /*------------------------------------------------------------------------
451 : * Fractional bits to distribute on the first x bands
452 : *-----------------------------------------------------------------------*/
453 :
454 8974 : test();
455 8974 : IF( EQ_16( L_frame, L_FRAME16k ) && GT_32( core_brate, ACELP_32k ) )
456 : {
457 0 : bit_fracf = 0;
458 0 : move32();
459 : }
460 : ELSE
461 : {
462 8974 : bit_fracf = Find_bit_frac_fx( st_band, bit_tmp ); /*Q18*/
463 : }
464 :
465 : /*------------------------------------------------------------------------
466 : * Complete the bit allocation per frequency band
467 : *-----------------------------------------------------------------------*/
468 8974 : imax = 5;
469 8974 : move16();
470 :
471 8974 : IF( GT_32( core_brate, ACELP_9k60 ) )
472 : {
473 3756 : imax = 7;
474 3756 : move16();
475 : }
476 61356 : FOR( i = 0; i < imax; i++ )
477 : {
478 52382 : bits_per_bands[i] = L_add( GSC_freq_bits_fx_Q18[bit_index], bit_fracf ); /*Q18*/
479 52382 : move32(); /* Q18 */
480 52382 : bit_index = add( bit_index, 1 );
481 : }
482 :
483 8974 : IF( EQ_16( L_frame, L_FRAME16k ) && GT_32( core_brate, ACELP_16k40 ) )
484 : {
485 2291 : bit_index = 0;
486 2291 : move16();
487 2291 : i = sub( imax, 1 );
488 2291 : bits_per_bands[i] = L_add( bits_per_bands[i], L_shl( Compl_GSC_freq_bits[bit_index], Q18 ) ); /*Q18*/
489 2291 : move32();
490 2291 : i = add( i, 1 );
491 2291 : bit_index = add( bit_index, 1 );
492 :
493 9164 : FOR( ; i < 10; i++ )
494 : {
495 6873 : bits_per_bands[i] = L_add( bits_per_bands[i], L_add( L_shl( Compl_GSC_freq_bits[bit_index], Q18 ), bit_fracf ) ); /*Q18*/
496 6873 : move32();
497 6873 : bit_index = add( bit_index, 1 );
498 : }
499 : }
500 :
501 8974 : IF( Diff_len == 0 )
502 : {
503 2080 : bit_index = add( bit_index_mem, 10 );
504 16640 : FOR( i = 0; i < 7; i++ )
505 : {
506 14560 : bits_per_bands[i] = L_add( bits_per_bands[i], GSC_freq_bits_fx_Q18[bit_index] ); /*Q18*/
507 14560 : move32(); /*chk Q18 */
508 14560 : bit_index = add( bit_index, 1 );
509 : }
510 : }
511 :
512 8974 : IF( bit_fracf < 0 )
513 : {
514 21114 : FOR( j = 0; j < nb_tot_bands; j++ )
515 : {
516 19872 : bits_per_bands[j] = L_max( bits_per_bands[j], 0 );
517 19872 : move32();
518 : }
519 : }
520 :
521 : /*--------------------------------------------------------------------------
522 : * Complete the bit allocation per frequency band for 16kHz high brate mode
523 : *--------------------------------------------------------------------------*/
524 :
525 8974 : test();
526 8974 : IF( EQ_16( L_frame, L_FRAME16k ) && GT_32( core_brate, ACELP_32k ) )
527 : {
528 0 : FOR( j = st_band; j < nb_bands; j++ )
529 : {
530 0 : bits_per_bands[j] = L_shl( bit_new_bands, Q18 );
531 0 : move32();
532 : }
533 :
534 0 : bit_fracf = L_shl( L_mult0( idiv1616( 16384, nb_bands ), bit_tmp ), 4 ); /*Q18*/
535 :
536 0 : etmp = divide3216( L_shr( bit_fracf, Q2 ), add( nb_bands, 1 ) ); /* Q15 */
537 :
538 0 : bit_fracf = L_shl( etmp, Q3 ); /* Q18 */
539 :
540 0 : FOR( j = nb_bands - 1; j >= 0; j-- )
541 : {
542 0 : bits_per_bands[j] = L_add( bits_per_bands[j], L_shl( etmp, Q3 ) ); /* Q18 */
543 0 : move32();
544 0 : etmp = extract_l( L_add( etmp, L_shr( bit_fracf, Q3 ) ) );
545 : }
546 : }
547 : ELSE
548 : {
549 12566 : FOR( j = st_band; j < nb_bands; j++ )
550 : {
551 3592 : bits_per_bands[j] = L_shl( bit_new_bands, 18 );
552 3592 : move32(); /*chk Q18 */
553 : }
554 : }
555 :
556 : /*--------------------------------------------------------------------------
557 : * Compute a maximum band (band offset) for the search on maximal energy
558 : * This is function of the spectral dynamic and the bitrate
559 : *--------------------------------------------------------------------------*/
560 :
561 8974 : bandoffset = sub( nb_tot_bands, add( nb_bands, 2 ) );
562 :
563 8974 : test();
564 8974 : test();
565 8974 : test();
566 8974 : test();
567 8974 : test();
568 8974 : IF( LE_16( noise_lev, NOISE_LEVEL_SP1a ) )
569 : {
570 3 : bandoffset = sub( bandoffset, 1 );
571 : }
572 8971 : ELSE IF( ( LE_32( core_brate, ACELP_13k20 ) && ( ( coder_type == INACTIVE ) || GE_16( noise_lev, NOISE_LEVEL_SP3 ) ) ) ||
573 : ( LE_32( core_brate, ACELP_13k20 ) && GE_32( core_brate, ACELP_9k60 ) && cor_strong_limit == 0 ) )
574 : {
575 2698 : bandoffset = add( bandoffset, 1 );
576 : }
577 :
578 8974 : bandoffset = s_max( bandoffset, 0 );
579 :
580 : /*--------------------------------------------------------------------------
581 : * Initiazed sorted vector
582 : * For the first x bands to be included in th final sorted vector
583 : * Sort the remaining bands in decrease energy order
584 : *--------------------------------------------------------------------------*/
585 162658 : FOR( j = 0; j < nb_tot_bands; j++ )
586 : {
587 153684 : max_ener_band[j] = -10;
588 153684 : move16();
589 : }
590 68229 : FOR( j = 0; j < st_band; j++ )
591 : {
592 59255 : max_ener_band[j] = j;
593 59255 : move16();
594 59255 : ener_vec[j] = -10;
595 59255 : move16();
596 : }
597 8974 : pos = st_band;
598 8974 : move16();
599 12566 : FOR( ; j < nb_bands; j++ )
600 : {
601 3592 : i = maximum_fx( ener_vec, sub( nb_tot_bands, bandoffset ), &etmp );
602 3592 : pos = s_max( pos, i );
603 3592 : max_ener_band[j] = i;
604 3592 : move16();
605 3592 : ener_vec[i] = -10;
606 3592 : move16();
607 : }
608 :
609 : /* re-allocate bits to the frames such that the highest band with allocated bits is higher than the threshold */
610 8974 : test();
611 8974 : test();
612 8974 : test();
613 8974 : IF( GT_16( sub( nb_tot_bands, bandoffset ), nb_bands ) && ( GT_16( pos, 7 ) && EQ_32( core_brate, ACELP_8k00 ) ) && EQ_16( bwidth, WB ) )
614 : {
615 0 : band = sub( nb_tot_bands, add( bandoffset, nb_bands ) );
616 0 : FOR( j = 0; j < band; j++ )
617 : {
618 0 : i = maximum_fx( ener_vec, sub( nb_tot_bands, bandoffset ), &etmp );
619 0 : max_ener_band[nb_bands + j] = i;
620 0 : move16();
621 0 : ener_vec[i] = -10;
622 0 : move16();
623 0 : bits_per_bands[nb_bands + j] = 1310720; /*5 in Q18*/
624 0 : move32(); /*Q18 */
625 : }
626 0 : nb_bands = add( nb_bands, band );
627 :
628 0 : bit_tmp = i_mult2( band, 5 );
629 :
630 0 : IF( LE_16( band, 2 ) )
631 : {
632 0 : FOR( j = st_band - 1; j < nb_bands; j++ )
633 : {
634 0 : bits_per_bands[j] = L_add( bits_per_bands[j], 262144 ); /*Q18 */
635 0 : move32();
636 : }
637 0 : bit_tmp = add( bit_tmp, add( sub( nb_bands, st_band ), 1 ) );
638 : }
639 :
640 0 : i = 0;
641 0 : move16();
642 0 : j = 0;
643 0 : move16();
644 0 : FOR( ; bit_tmp > 0; bit_tmp-- )
645 : {
646 0 : bits_per_bands[j] = L_sub( bits_per_bands[j], ONE_IN_Q18 ); /*Q18 */
647 0 : j = add( j, 1 );
648 0 : IF( EQ_16( j, sub( st_band, i ) ) )
649 : {
650 0 : j = 0;
651 0 : move16();
652 : }
653 0 : test();
654 0 : if ( j == 0 && LT_16( i, sub( st_band, 1 ) ) )
655 : {
656 0 : i = add( i, 1 );
657 : }
658 : }
659 : }
660 : }
661 :
662 : /*--------------------------------------------------------------------------
663 : * Bit sum verification for GSC inactive at very high rate
664 : * The maximum number of bits per band of length 16 is 112
665 : * Redistribute the overage bits if needed
666 : *--------------------------------------------------------------------------*/
667 20560 : sum_bit = 0;
668 20560 : move16();
669 20560 : j = 0;
670 20560 : move16();
671 167312 : FOR( i = 0; i < nb_bands; i++ )
672 : {
673 146752 : L_tmp = Mult_32_16( sum_bit, 10923 /*1/3 in Q15*/ );
674 :
675 146752 : IF( GT_32( bits_per_bands[i], 29360128 ) ) /* 112 in Q18 */
676 : {
677 0 : sum_bit = L_add( sum_bit, L_sub( bits_per_bands[i], 29360128 /* 112 in Q18 */ ) ); /* Q18 */
678 0 : bits_per_bands[i] = 29360128 /* 112 in Q18 */;
679 0 : move32();
680 0 : j = add( i, 1 );
681 : }
682 146752 : ELSE IF( GT_32( L_add( bits_per_bands[i], L_tmp ), 29360128 ) ) /* Q18 */
683 : {
684 0 : j = add( i, 1 );
685 : }
686 : }
687 :
688 20560 : IF( sum_bit != 0 )
689 : {
690 0 : tmp = sub( nb_bands, j );
691 0 : sum_bit = Mult_32_16( sum_bit, div_s( 1, tmp ) ); /* Q18 */
692 0 : FOR( i = j; i < nb_bands; i++ )
693 : {
694 0 : bits_per_bands[i] = L_add( bits_per_bands[i], sum_bit );
695 0 : move32(); /* Q18 */
696 : }
697 : }
698 :
699 : /*--------------------------------------------------------------------------
700 : * second step of bit sum verification, normally sum_bit == *bit
701 : *--------------------------------------------------------------------------*/
702 :
703 20560 : sum_bit = 0;
704 20560 : move32();
705 167312 : FOR( i = 0; i < nb_bands; i++ )
706 : {
707 146752 : bits_per_bands[i] = L_shl( L_shr( bits_per_bands[i], Q18 ), Q18 );
708 146752 : move32();
709 146752 : sum_bit = L_add( sum_bit, L_shr( bits_per_bands[i], Q18 ) ); /*Q0*/
710 : }
711 :
712 20560 : test();
713 20560 : IF( GSC_IVAS_mode != 0 && LT_32( sum_bit, *bit ) ) /* If we need to add bits, we are doing it on the LF */
714 : {
715 4118 : reajust_bits_fx( bits_per_bands, 0, nb_bands, (Word16) sum_bit, *bit );
716 : }
717 : ELSE
718 : {
719 16442 : reajust_bits_fx( bits_per_bands, sub( nb_bands, 1 ), 0, (Word16) sum_bit, *bit );
720 : }
721 20560 : w_sum_bit = 0;
722 20560 : move16();
723 167312 : FOR( i = 0; i < nb_bands; i++ )
724 : {
725 146752 : out_bits_per_bands[i] = shl( extract_l( L_shr( bits_per_bands[i], 18 ) ), 3 );
726 146752 : move16();
727 146752 : w_sum_bit = add( w_sum_bit, out_bits_per_bands[i] ); /* Q3 */
728 : }
729 20560 : tmp = shl( *bit, 3 );
730 :
731 : /*--------------------------------------------------------------------------
732 : * Recompute the real number/length of frequency bands to encode
733 : *--------------------------------------------------------------------------*/
734 20560 : *nb_subbands = nb_bands;
735 20560 : move16();
736 20560 : *pvq_len = shl( *nb_subbands, 4 );
737 20560 : move16();
738 :
739 : /*--------------------------------------------------------------------------
740 : * Concatenate bands (encoder only)
741 : *--------------------------------------------------------------------------*/
742 20560 : IF( exc_diff != NULL )
743 : {
744 0 : FOR( j = 0; j < nb_bands; j++ )
745 : {
746 0 : Copy( exc_diff + shl( max_ener_band[j], 4 ), concat_in + shl( j, 4 ), 16 );
747 : }
748 : }
749 : }
750 : ELSE /* *bit == 0 */
751 : {
752 0 : set16_fx( out_bits_per_bands, 0, nb_tot_bands );
753 0 : *nb_subbands = 0;
754 0 : move16();
755 0 : *pvq_len = 0;
756 0 : move16();
757 : }
758 :
759 20560 : return;
760 : }
761 :
762 :
763 : /*-------------------------------------------------------------------*
764 : * reajust_bits()
765 : *
766 : *
767 : *-------------------------------------------------------------------*/
768 :
769 20560 : static void reajust_bits_fx(
770 : Word32 *bits_per_bands, /*i : Q18*/
771 : const Word16 st_band,
772 : const Word16 end_band,
773 : const Word16 sum_bit_in, /*i : Q0*/
774 : const Word16 bit_bdgt_in /*i : Q0*/ )
775 : {
776 : Word16 i, amount_to_add, incr;
777 : Word16 bit_bdgt, sum_bit;
778 :
779 20560 : incr = 1;
780 20560 : move16();
781 20560 : if ( LT_16( end_band, st_band ) )
782 : {
783 16442 : incr = -1;
784 16442 : move16();
785 : }
786 :
787 20560 : IF( LT_16( bit_bdgt_in, sum_bit_in ) )
788 : {
789 0 : amount_to_add = -1;
790 0 : move16();
791 0 : bit_bdgt = sum_bit_in;
792 0 : move16();
793 0 : sum_bit = bit_bdgt_in;
794 0 : move16();
795 : }
796 : ELSE
797 : {
798 20560 : bit_bdgt = bit_bdgt_in;
799 20560 : move16();
800 20560 : sum_bit = sum_bit_in;
801 20560 : move16();
802 20560 : amount_to_add = 1;
803 20560 : move16();
804 : }
805 :
806 20560 : i = st_band;
807 20560 : move16();
808 92501 : WHILE( GT_16( bit_bdgt, sum_bit ) )
809 : {
810 : /* if (amount_to_add > 0 || (amount_to_add < 0 && bits_per_bands[i] > 1))*/
811 71941 : test();
812 71941 : test();
813 71941 : IF( amount_to_add > 0 || ( amount_to_add < 0 && GT_32( bits_per_bands[i], ONE_IN_Q18 ) ) )
814 : {
815 : /* bits_per_bands[i] += amount_to_add;*/
816 71941 : bits_per_bands[i] = L_add( bits_per_bands[i], L_shl( amount_to_add, Q18 ) ); /*Q18*/
817 71941 : move32();
818 71941 : sum_bit = add( sum_bit, abs_s( amount_to_add ) );
819 : }
820 :
821 71941 : i = add( i, incr );
822 71941 : if ( EQ_16( i, end_band ) )
823 : {
824 2099 : i = st_band;
825 2099 : move16();
826 : }
827 : }
828 :
829 20560 : return;
830 : }
831 :
832 : /*-------------------------------------------------------------------*
833 : * Find_bit_frac()
834 : *
835 : * Computes the fraction of the remaining bit budget to allocate to the bands
836 : *-------------------------------------------------------------------*/
837 :
838 16432 : static Word32 Find_bit_frac_fx(
839 : const Word16 nb_band,
840 : const Word16 remaining_bits )
841 : {
842 : Word16 inv_bandQ15;
843 : Word32 L_out;
844 :
845 16432 : inv_bandQ15 = 6553; /*Q15*/
846 16432 : move16();
847 16432 : IF( EQ_16( nb_band, 7 ) )
848 : {
849 8201 : inv_bandQ15 = 4681; /*Q15*/
850 8201 : move16();
851 : }
852 8231 : ELSE IF( EQ_16( nb_band, 3 ) )
853 : {
854 0 : inv_bandQ15 = 10922; /*Q15*/
855 0 : move16();
856 : }
857 8231 : ELSE IF( EQ_16( nb_band, 4 ) )
858 : {
859 0 : inv_bandQ15 = 8192; /*Q15*/
860 0 : move16();
861 : }
862 8231 : ELSE IF( EQ_16( nb_band, 5 ) )
863 : {
864 5940 : inv_bandQ15 = 6536; /*Q15*/
865 5940 : move16();
866 : }
867 2291 : ELSE IF( EQ_16( nb_band, 10 ) )
868 : {
869 2291 : inv_bandQ15 = 3277; /*Q15*/
870 2291 : move16();
871 : }
872 : ELSE
873 : {
874 : #ifdef DEBUGGING
875 : printf( "1/%d NOT DEFINED in Find_bit_frac\n", nb_band );
876 : #endif
877 : }
878 16432 : L_out = L_mult( inv_bandQ15, shl( remaining_bits, 2 ) ); /*Q18*/
879 :
880 16432 : return ( L_out );
881 : }
882 :
883 :
884 : /* o : Number of band to encode */
885 4128 : static Word16 Find_bit_alloc_IVAS_fx(
886 : const Word32 core_brate, /* i : core bit rate */
887 : const Word16 GSC_IVAS_mode, /* i : GSC IVAS mode */
888 : const Word16 Diff_len, /* i : Length of the difference signal (before pure spectral)*/
889 : const Word16 nb_tot_bands_in, /* i : total number of band */
890 : Word16 *bit, /* i/o: Number of bit allowed for frequency quantization */
891 : Word16 *max_ener_band, /* i/o: Energy based sorted order */
892 : Word16 *ener_vec, /* i/o: Energy per band order */
893 : Word32 *bits_per_bands /* o : Number of bit allowed per allowed subband Q18 */
894 : )
895 : {
896 : Word32 mp, mb, nb_bands_adj, bit_adj;
897 : Word16 nb_pulse_per_band[MBANDS_GN_BITALLOC16k];
898 : Word32 SWB_bit_budget; /* Q0 -> Q18 */
899 : Word16 i, j, nb_bands_max, st_band, nb_tot_bands_loc, etmp;
900 : Word32 sum_bit /*Q18*/, bit_fracf /*Q18*/;
901 : Word16 d_tmp, e_div, tmp16;
902 : Word32 Ltmp, etmp_32fx;
903 :
904 4128 : SWB_bit_budget = *bit; /* Q0 */
905 4128 : st_band = 5;
906 4128 : nb_bands_max = nb_tot_bands_in;
907 :
908 4128 : move16();
909 4128 : move16();
910 4128 : move16();
911 :
912 4128 : set32_fx( bits_per_bands, 0, MBANDS_GN_BITALLOC16k );
913 :
914 : /* Decide the percentage of bits allocated to LF (between 50-75%) depending of the temporal contribution in GSC */
915 : /* bit_fracf = ( -0.125f * Diff_len + 76.0f ) / 100; */
916 4128 : bit_fracf = L_add( Mpy_32_32( -Q31_0_00125, L_shl( Diff_len, Q18 ) ), Q18_0_76 ); /* Q18 */
917 :
918 : /* bit_fracf = check_bounds(bit_fracf, 0.50f, 0.75f); */
919 4128 : bit_fracf = check_bounds_l( bit_fracf, Q18_0_50, Q18_0_75 ); /* Q18 */
920 :
921 : /* Adjustment of the bitrate between LF and HF base on the content type */
922 : /* 1 = new GSC bit alloc
923 : 2 = GSC bit alloc for tc frame
924 : 3 = more music like (should not happen often given music is coded with dft) */
925 :
926 4128 : if ( LE_16( GSC_IVAS_mode, 3 ) )
927 : {
928 4128 : nb_bands_max = sub( nb_bands_max, 6 );
929 : }
930 4128 : IF( EQ_16( GSC_IVAS_mode, 2 ) )
931 : {
932 : /* bit_fracf += 0.1f; */
933 218 : bit_fracf = L_add( bit_fracf, Q18_0_1 ); /* Q18*/
934 218 : nb_bands_max = sub( nb_bands_max, 1 );
935 : }
936 4128 : IF( EQ_16( GSC_IVAS_mode, 3 ) )
937 : {
938 : /* bit_fracf -= 0.1f; */
939 2279 : bit_fracf = L_sub( bit_fracf, Q18_0_1 ); /* Q18*/
940 2279 : nb_bands_max = add( nb_bands_max, 3 );
941 : }
942 :
943 : /* First find how much we want to share between LF and HF, at low bitrate, a miminum of bits is needed in LF by limitating the number of bands*/
944 : /* Adjust the number of band based on the content type and bitrate */
945 :
946 : /* nb_bands_adj = 1.0f; */
947 4128 : nb_bands_adj = Q18_1_0;
948 4128 : move32();
949 4128 : test();
950 4128 : test();
951 4128 : IF( EQ_16( GSC_IVAS_mode, 1 ) && LT_32( core_brate, GSC_L_RATE_STG ) )
952 : {
953 : /* nb_bands_adj = 0.0125f * SWB_bit_budget - 0.75f;*/
954 1611 : nb_bands_adj = L_sub( Mpy_32_32( Q31_0_0125, L_shl( SWB_bit_budget, Q18 ) ), Q18_0_75 ); /* Q18 */
955 : }
956 2517 : ELSE IF( NE_16( GSC_IVAS_mode, 2 ) && GT_32( core_brate, GSC_H_RATE_STG ) )
957 : {
958 : /*nb_bands_adj = 0.02f * SWB_bit_budget - 1.2f;*/
959 0 : nb_bands_adj = L_sub( Mpy_32_32( Q31_0_02, L_shl( SWB_bit_budget, Q18 ) ), Q18_1_2 ); /* Q18 */
960 : }
961 : /*nb_bands_max = (int16_t)(nb_bands_max * nb_bands_adj + 0.5f);*/
962 :
963 4128 : nb_bands_max = round_fx( Mpy_32_16_1( L_shl( nb_bands_adj, 5 ), shl( nb_bands_max, 10 - 2 ) ) ); /* Q0 */
964 4128 : nb_bands_max = check_bounds_s_fx( nb_bands_max, 5, nb_tot_bands_in );
965 :
966 : /* bit_fracf *= SWB_bit_budget;*/
967 :
968 : /* At this point bit_fracf has a value below 1.0 */
969 4128 : bit_fracf = Mpy_32_16_1( L_shl( bit_fracf, 10 ), extract_l( L_shl( SWB_bit_budget, 5 ) ) ); /* (Q(18+10)*Q(0+5) + 1 - 16 = Q18 */
970 :
971 : /* Estimation of the number of bit used in HF */
972 : /* with only the first weighting The number of bits in max_ener_band[st_band-1] = 17% of bit_fracf */
973 : /* mb = .17f * bit_fracf;*/
974 4128 : mb = Mpy_32_32( Q31_0_17, bit_fracf ); /* Q18 */
975 :
976 : /* mp = 2 * DSR_NB_PULSE;*/
977 4128 : mp = Q18_2_0xDSR_NB_PULSE;
978 4128 : move32();
979 :
980 4128 : test();
981 4128 : IF( LT_32( core_brate, GSC_L_RATE_STG ) && EQ_16( GSC_IVAS_mode, 3 ) )
982 : {
983 : /* mp = 1.5f * DSR_NB_PULSE;*/
984 2233 : mp = Q18_1_5xDSR_NB_PULSE;
985 2233 : move32();
986 : }
987 1895 : ELSE IF( LT_32( core_brate, GSC_L_RATE_STG ) )
988 : {
989 : /* mp = DSR_NB_PULSE;*/
990 1822 : mp = Q18_DSR_NB_PULSE;
991 1822 : move32();
992 : }
993 :
994 : /* We want max_ener_band[st_band] <= max_ener_band[st_band-1] and max_ener_band[nb_bands_max-1] <= max_ener_band[st_band]*/
995 : /* We will estimate the number of bits to allocate of HF and put the remaining bits, if any, back on LF */
996 : /* compute the total possible number of band to be coded */
997 :
998 : /* nb_tot_bands = (int16_t)((SWB_bit_budget - bit_fracf) / (mp + (mb - mp) / 2.0f)); */
999 4128 : d_tmp = Find_norm_inv_fx( L_add( mp, mb ), &e_div );
1000 4128 : Ltmp = Mpy_32_16_1( L_sub( L_shl( SWB_bit_budget, Q18 ), bit_fracf ), d_tmp ); /* Perform mult by 1/den */
1001 4128 : nb_tot_bands_loc = extract_h( L_shl( Ltmp, sub( 1, e_div ) ) ); /* adjust exponent: 1 is to take into account the / 2.0f, and e_div for the num and den of the division*/
1002 4128 : mp = L_min( mp, mb );
1003 :
1004 4128 : tmp16 = sub( add( nb_tot_bands_loc, st_band ), nb_bands_max );
1005 4128 : IF( tmp16 > 0 )
1006 : {
1007 : /* bit_adj = ( ( mb + mp ) / 2 ) * ( nb_tot_bands_loc + st_band - nb_bands_max ); */
1008 1687 : bit_adj = Mpy_32_16_1( L_shl( L_add( mb, mp ), 5 ), shl( tmp16, 10 - 1 ) ); /* Q18+5 * Q0+10 + 1 -1 - 16 = Q18 (-1 is to cover for the /2 in the equation) */
1009 1687 : bit_adj = L_max( 0, bit_adj );
1010 1687 : nb_tot_bands_loc = sub( nb_bands_max, st_band );
1011 : /* bit_fracf += bit_adj; */
1012 1687 : bit_fracf = L_add( bit_fracf, bit_adj ); /* Q18 */
1013 : }
1014 4128 : nb_tot_bands_loc = add( nb_tot_bands_loc, st_band );
1015 :
1016 : /* Allocate bits to LF */
1017 : /* etmp = 0.23f; */
1018 4128 : etmp_32fx = Q31_0_23;
1019 4128 : move32();
1020 24768 : FOR( j = 0; j < st_band; j++ )
1021 : {
1022 20640 : i = j;
1023 20640 : move16();
1024 20640 : max_ener_band[j] = i;
1025 20640 : move16();
1026 20640 : ener_vec[i] = MIN16B;
1027 20640 : move16();
1028 : /* bits_per_bands[j] = etmp * bit_fracf; */
1029 20640 : bits_per_bands[j] = Mpy_32_32( bit_fracf, etmp_32fx ); /* 18 + 31 + 1 - 32 = Q18 */
1030 20640 : move32();
1031 : /* etmp -= 0.015f; */
1032 20640 : etmp_32fx = L_sub( etmp_32fx, Q31_0_015 ); /* Q18 */
1033 : }
1034 :
1035 : /* SWB_bit_budget -= bit_fracf; */
1036 4128 : SWB_bit_budget = L_sub( L_shl( SWB_bit_budget, Q18 ), bit_fracf ); /* Q0->Q18 */
1037 :
1038 : /* Find low energy band in HF */
1039 4128 : set16_fx( nb_pulse_per_band, 2, MBANDS_GN_BITALLOC16k );
1040 8921 : FOR( i = st_band + 2; i < nb_tot_bands_loc - 1; i++ )
1041 : {
1042 4793 : test();
1043 4793 : if ( LT_16( ener_vec[i], ener_vec[i - 1] ) && LT_16( ener_vec[i], ener_vec[i + 1] ) ) /* i +1 and i -1 can be considered as 2 ptrs */
1044 : {
1045 1508 : nb_pulse_per_band[i] = 1;
1046 1508 : move16();
1047 : }
1048 : }
1049 16631 : FOR( j = st_band; j < nb_tot_bands_loc; j++ )
1050 : {
1051 12503 : IF( GT_16( j, 6 ) )
1052 : {
1053 7271 : i = maximum_fx( ener_vec, nb_tot_bands_loc, &etmp );
1054 : }
1055 : ELSE
1056 : {
1057 5232 : i = j;
1058 5232 : move16();
1059 : }
1060 12503 : max_ener_band[j] = i;
1061 12503 : move16();
1062 12503 : ener_vec[i] = MIN16B;
1063 12503 : move16();
1064 : }
1065 :
1066 : /* Recompute the final bit distribution for HF */
1067 4128 : IF( GT_16( nb_tot_bands_loc, st_band ) )
1068 : {
1069 : /* mb = ( SWB_bit_budget * 2 / ( nb_tot_bands_loc - st_band ) ) - mp; */
1070 : /* bit_fracf = ( mb - mp ) / ( nb_tot_bands_loc - st_band ); */
1071 : /* This is not bit exact because of the precision lost */
1072 :
1073 : /* mb = ( SWB_bit_budget * 2 / ( nb_tot_bands_loc - st_band ) ) - mp; */
1074 2643 : d_tmp = Find_norm_inv_fx( L_deposit_h( sub( nb_tot_bands_loc, st_band ) ), &e_div );
1075 2643 : mb = L_sub( L_shr( Mpy_32_16_1( L_shl( SWB_bit_budget, 1 ), d_tmp ), e_div ), mp ); /* Q18 */
1076 :
1077 : /* bit_fracf = ( mb - mp ) / ( nb_tot_bands_loc - st_band ); */
1078 2643 : bit_fracf = L_shr( Mpy_32_16_1( L_sub( mb, mp ), d_tmp ), e_div ); /* Q18 */
1079 :
1080 2643 : mb = L_sub( mb, bit_fracf );
1081 : /* Do the distribution */
1082 15146 : FOR( j = st_band; j < nb_tot_bands_loc; j++ )
1083 : {
1084 12503 : bits_per_bands[max_ener_band[j]] = Q18_DSR_NB_PULSE;
1085 12503 : move32();
1086 12503 : if ( GT_16( nb_pulse_per_band[max_ener_band[j]], 1 ) )
1087 : {
1088 10995 : bits_per_bands[max_ener_band[j]] = mb;
1089 10995 : move32();
1090 : }
1091 12503 : mb = L_sub( mb, bit_fracf );
1092 12503 : SWB_bit_budget = L_sub( SWB_bit_budget, bits_per_bands[max_ener_band[j]] ); /* Q18 */
1093 : }
1094 : }
1095 :
1096 : /* Series of verification in case bit allocated != the budget */
1097 4128 : IF( SWB_bit_budget > 0 )
1098 : {
1099 4123 : i = sub( st_band, 1 );
1100 19403 : WHILE( SWB_bit_budget > 0 )
1101 : {
1102 : /* bits_per_bands[i]++; */
1103 15280 : bits_per_bands[i] = L_add( bits_per_bands[i], Q18_1_0 ); /*Q18*/
1104 15280 : move32();
1105 : /* SWB_bit_budget--; */
1106 15280 : SWB_bit_budget = L_sub( SWB_bit_budget, Q18_1_0 ); /*Q18*/
1107 15280 : i = sub( i, 1 );
1108 15280 : if ( EQ_16( i, -1 ) )
1109 : {
1110 1249 : i = sub( st_band, 1 );
1111 : }
1112 : }
1113 : }
1114 :
1115 : /*nb_bands = nb_tot_bands_loc;*/
1116 :
1117 4128 : sum_bit = 0;
1118 4128 : move32();
1119 4128 : j = 0;
1120 4128 : move16();
1121 37271 : FOR( i = 0; i < nb_tot_bands_loc; i++ )
1122 : {
1123 : /* if (bits_per_bands[i] > 112) */
1124 33143 : IF( GT_32( bits_per_bands[i], Q18_112 ) )
1125 : {
1126 : /* sum_bit += bits_per_bands[i] - 112; */
1127 0 : sum_bit = L_add( sum_bit, L_sub( bits_per_bands[i], Q18_112 ) );
1128 : /* bits_per_bands[i] = 112; */
1129 0 : bits_per_bands[i] = Q18_112;
1130 0 : move32();
1131 0 : j = add( j, add( i, 1 ) );
1132 : }
1133 :
1134 : /* safety check for overage bit reallocation */
1135 : /* else if (bits_per_bands[i] + sum_bit / 3 > 112) */
1136 33143 : ELSE IF( GT_32( L_add( bits_per_bands[i], Mpy_32_16_1( sum_bit, Q15_0_33 ) ), Q18_112 ) )
1137 : {
1138 0 : j = add( j, add( i, 1 ) );
1139 : }
1140 : }
1141 :
1142 4128 : IF( sum_bit != 0 )
1143 : {
1144 : /* sum_bit /= (nb_bands - j); */
1145 0 : d_tmp = Find_norm_inv_fx( L_deposit_h( sub( nb_tot_bands_loc, j ) ), &e_div );
1146 0 : sum_bit = L_shr( Mpy_32_16_1( sum_bit, d_tmp ), e_div ); /* Q18 */
1147 0 : FOR( i = j; i < nb_tot_bands_loc; i++ )
1148 : {
1149 0 : bits_per_bands[i] = L_add( bits_per_bands[i], sum_bit );
1150 0 : move32();
1151 : }
1152 : }
1153 :
1154 4128 : return nb_tot_bands_loc;
1155 : }
1156 :
1157 :
1158 : /* Find normalized 1 / ToDivide */
1159 6771 : static Word16 Find_norm_inv_fx(
1160 : const Word32 ToDivide,
1161 : Word16 *e_div )
1162 : {
1163 : Word16 d_tmp, e_tmp;
1164 :
1165 6771 : e_tmp = norm_l( ToDivide );
1166 6771 : d_tmp = round_fx( L_shl( ToDivide, e_tmp ) );
1167 6771 : d_tmp = div_s( 16384, d_tmp ); /* 1.0 in Q14, dividend is normalize so >= 16384 as required for the division */
1168 6771 : *e_div = sub( 14, e_tmp );
1169 6771 : move16();
1170 :
1171 6771 : return d_tmp;
1172 : }
|