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