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 50629 : 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 50629 : 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 50629 : nb_tot_bands = MBANDS_GN;
110 50629 : move16();
111 50629 : if ( EQ_16( L_frame, L_FRAME16k ) )
112 : {
113 5383 : nb_tot_bands = MBANDS_GN_BITALLOC16k;
114 5383 : move16();
115 : }
116 :
117 50629 : set32_fx( bits_per_bands, 0, MBANDS_GN_BITALLOC16k );
118 50629 : set16_fx( out_bits_per_bands, 0, MBANDS_GN_BITALLOC16k );
119 :
120 : /* To adapt current energy band to PVQ freq band for sorting*/
121 50629 : ener_vec[0] = add_sat( Ener_per_bd_iQ[0], Ener_per_bd_iQ[1] ); /*Q12 */
122 50629 : move16();
123 50629 : Copy( Ener_per_bd_iQ + 1, ener_vec, MBANDS_GN - 1 ); /*Q12 */
124 50629 : ener_vec[MBANDS_GN - 1] = ener_vec[MBANDS_GN - 2];
125 50629 : move16();
126 50629 : IF( EQ_16( L_frame, L_FRAME16k ) )
127 : {
128 5383 : ener_vec[16] = Ener_per_bd_iQ[16]; /*Q12*/
129 5383 : 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 5383 : ener_vec[18] = Ener_per_bd_iQ[17]; /*Q12*/
131 5383 : ener_vec[19] = mult_r( Ener_per_bd_iQ[17], 26214 /*0.8 in Q15*/ ); /*Q12*/
132 5383 : move16();
133 5383 : move16();
134 5383 : move16();
135 5383 : 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 50629 : nb_bands_max = nb_tot_bands;
144 50629 : move16();
145 50629 : bit_new_bands = 5;
146 50629 : move16();
147 :
148 50629 : test();
149 50629 : if ( GT_32( core_brate, ACELP_16k40 ) && EQ_16( L_frame, L_FRAME16k ) )
150 : {
151 4887 : bit_new_bands = 7;
152 4887 : move16();
153 : }
154 :
155 50629 : i = 0;
156 50629 : move16();
157 238457 : WHILE( LT_16( i, SIZE_BRATE_INTERMED_TBL ) )
158 : {
159 238457 : IF( LE_32( core_brate, brate_intermed_tbl[i] ) )
160 : {
161 50245 : BREAK;
162 : }
163 :
164 188212 : IF( EQ_32( brate_intermed_tbl[i], ACELP_24k40 ) )
165 : {
166 384 : BREAK;
167 : }
168 :
169 187828 : i = add( i, 1 );
170 : }
171 :
172 50629 : test();
173 50629 : test();
174 50629 : test();
175 76767 : if ( ( element_mode > EVS_MONO ) && EQ_16( coder_type, AUDIO ) &&
176 41217 : 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 3397 : i = sub( i, 1 );
179 : }
180 :
181 50629 : bit_index = i_mult2( BRATE2IDX_fx( brate_intermed_tbl[i] ), 17 );
182 50629 : bit_index_mem = bit_index;
183 50629 : move16();
184 :
185 50629 : test();
186 50629 : test();
187 50629 : 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 50629 : L_tmp = Mult_32_16( core_brate, 20971 /*1/50 in Q20*/ );
212 50629 : tmp = extract_l( L_shr_r( L_tmp, 5 ) );
213 50629 : *bit = sub( sub( tmp, bits_used ), GSC_freq_bits[bit_index] );
214 50629 : move16();
215 : }
216 :
217 50629 : IF( EQ_16( L_frame, L_FRAME16k ) )
218 : {
219 5383 : *bit = sub( *bit, 8 );
220 5383 : move16();
221 : }
222 :
223 50629 : test();
224 50629 : IF( coder_type == INACTIVE && LE_32( core_brate, GSC_LRES_GAINQ_LIMIT ) ) /* can happen only for 2nd channel inactive */
225 : {
226 15284 : *bit = add( *bit, GSC_LRES_NB_NITS );
227 15284 : move16();
228 : }
229 :
230 50629 : IF( *bit > 0 )
231 : {
232 49891 : IF( GSC_IVAS_mode > 0 )
233 : {
234 13796 : 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 13796 : nb_bands = nb_tot_bands;
236 : }
237 36095 : ELSE IF( EQ_16( GSC_noisy_speech, 1 ) )
238 : {
239 16798 : SWB_bit_budget = *bit; /*Q0*/
240 16798 : move32();
241 16798 : nb_bands = 5;
242 16798 : move16();
243 16798 : fzero_val = 0;
244 16798 : move16();
245 :
246 16798 : if ( element_mode > EVS_MONO )
247 : {
248 16798 : fzero_val = MIN16B;
249 16798 : move16();
250 : }
251 :
252 16798 : test();
253 16798 : IF( EQ_16( coder_type, UNVOICED ) && element_mode > EVS_MONO )
254 : {
255 54 : nb_bands = 3;
256 54 : move16();
257 :
258 54 : if ( GT_32( SWB_bit_budget, 20 ) )
259 : {
260 54 : nb_bands = 5;
261 54 : move16();
262 : }
263 : }
264 16744 : ELSE IF( LT_16( bwidth, SWB ) )
265 : {
266 15552 : nb_bands = 7;
267 15552 : move16();
268 : }
269 :
270 16798 : st_band = nb_bands;
271 16798 : move16();
272 :
273 16798 : set32_fx( bits_per_bands, 0, MBANDS_GN );
274 : /*bit_fracf = (1.0f/nb_bands)*(SWB_bit_budget); */
275 16798 : bit_fracf = Find_bit_frac_fx( nb_bands, extract_l( SWB_bit_budget ) ); /*Q18*/
276 16798 : nb_tot_bands = sub( nb_bands_max, 6 );
277 16798 : nb_tot_bands = s_min( nb_tot_bands, 16 );
278 :
279 50394 : FOR( j = 0; j < 2; j++ )
280 : {
281 33596 : i = j;
282 33596 : move16();
283 33596 : max_ener_band[j] = i;
284 33596 : move16();
285 33596 : ener_vec[i] = fzero_val;
286 33596 : move16();
287 : }
288 :
289 16798 : IF( LT_16( bwidth, SWB ) )
290 : {
291 15588 : test();
292 15588 : if ( EQ_16( coder_type, UNVOICED ) && element_mode > EVS_MONO )
293 : {
294 36 : nb_tot_bands = 5;
295 36 : move16();
296 : }
297 :
298 93456 : FOR( ; j < nb_bands; j++ )
299 : {
300 77868 : i = maximum_fx( ener_vec, nb_tot_bands, &etmp );
301 77868 : max_ener_band[j] = i;
302 77868 : move16();
303 77868 : ener_vec[i] = fzero_val;
304 77868 : move16();
305 : }
306 : }
307 : ELSE
308 : {
309 4840 : FOR( ; j < nb_bands; j++ )
310 : {
311 3630 : i = maximum_fx( ener_vec, nb_tot_bands, &etmp );
312 3630 : max_ener_band[j] = i;
313 3630 : move16();
314 3630 : ener_vec[i] = fzero_val;
315 3630 : move16();
316 : }
317 : }
318 :
319 16798 : set32_fx( bits_per_bands, bit_fracf, nb_bands );
320 : }
321 : ELSE
322 : {
323 19297 : bit_index = add( bit_index, 1 );
324 19297 : bit_tmp = sub( *bit, GSC_freq_bits[bit_index] );
325 19297 : bit_index = add( bit_index, 1 );
326 19297 : nb_bands_max = add( nb_bands_max, GSC_freq_bits[bit_index] );
327 19297 : bit_index = add( bit_index, 1 );
328 :
329 19297 : *pvq_len = 112;
330 19297 : move16();
331 19297 : st_band = 7;
332 19297 : move16();
333 :
334 19297 : test();
335 19297 : IF( EQ_16( L_frame, L_FRAME16k ) && GT_32( core_brate, ACELP_16k40 ) )
336 : {
337 4887 : *pvq_len = 160;
338 4887 : move16();
339 4887 : st_band = 10;
340 4887 : move16();
341 4887 : nb_bands = shr( *pvq_len, 4 );
342 4887 : bit_tmp = sub( bit_tmp, 35 );
343 4887 : bit_new_bands = 5;
344 4887 : move16();
345 : }
346 :
347 19297 : IF( LE_32( core_brate, ACELP_9k60 ) )
348 : {
349 11358 : *pvq_len = 80;
350 11358 : move16();
351 11358 : st_band = 5;
352 11358 : move16();
353 :
354 11358 : IF( Diff_len == 0 )
355 : {
356 196 : nb_bands_max = add( nb_bands_max, 2 );
357 196 : bit_tmp = sub( bit_tmp, 13 );
358 : }
359 : }
360 :
361 7939 : ELSE IF( Diff_len == 0 )
362 : {
363 4163 : nb_bands_max = add( nb_bands_max, 2 );
364 4163 : bit_tmp = sub( bit_tmp, 17 );
365 : }
366 :
367 19297 : nb_bands = shr( *pvq_len, 4 );
368 19297 : 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 19297 : test();
375 19297 : test();
376 19297 : test();
377 19297 : test();
378 19297 : IF( coder_type == INACTIVE || GE_16( noise_lev, NOISE_LEVEL_SP3 ) )
379 : {
380 : /* Probably classification error -> concentrate bits on LF */
381 12454 : IF( EQ_16( L_frame, L_FRAME16k ) && GE_32( core_brate, ACELP_24k40 ) )
382 : {
383 384 : nb_bands_max = sub( nb_tot_bands, 2 );
384 : }
385 12070 : ELSE IF( GE_32( core_brate, ACELP_16k40 ) )
386 : {
387 4793 : nb_bands_max = add( nb_bands, 2 );
388 : }
389 7277 : ELSE IF( GE_32( core_brate, ACELP_8k00 ) )
390 : {
391 3649 : nb_bands_max = add( nb_bands, 1 );
392 : }
393 : ELSE
394 : {
395 3628 : nb_bands_max = nb_bands;
396 3628 : move16();
397 : }
398 : }
399 6843 : 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 6823 : nb_bands_max = sub( nb_bands_max, 2 );
403 : }
404 20 : ELSE IF( GE_16( noise_lev, NOISE_LEVEL_SP1 ) ) /* Very low dynamic, tend to speech, code less HF */
405 : {
406 12 : nb_bands_max = sub( nb_bands_max, 1 );
407 : }
408 :
409 19297 : IF( EQ_16( L_frame, L_FRAME16k ) )
410 : {
411 5383 : IF( LT_32( core_brate, ACELP_24k40 ) )
412 : {
413 4999 : nb_bands_max = sub( nb_bands_max, 4 );
414 : }
415 384 : ELSE IF( LT_32( core_brate, ACELP_32k ) )
416 : {
417 384 : test();
418 384 : IF( GT_16( Diff_len, 0 ) || GE_16( noise_lev, NOISE_LEVEL_SP2 ) )
419 : {
420 384 : nb_bands_max = sub( nb_bands_max, 2 );
421 384 : 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 19297 : test();
431 19297 : 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 19297 : test();
441 26777 : WHILE( GE_16( bit_tmp, bit_new_bands ) && LE_16( nb_bands, sub( nb_bands_max, 1 ) ) )
442 : {
443 7480 : test();
444 7480 : bit_tmp = sub( bit_tmp, bit_new_bands );
445 7480 : nb_bands = add( nb_bands, 1 );
446 : }
447 :
448 : /*------------------------------------------------------------------------
449 : * Fractional bits to distribute on the first x bands
450 : *-----------------------------------------------------------------------*/
451 :
452 19297 : test();
453 19297 : 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 19297 : bit_fracf = Find_bit_frac_fx( st_band, bit_tmp ); /*Q18*/
461 : }
462 :
463 : /*------------------------------------------------------------------------
464 : * Complete the bit allocation per frequency band
465 : *-----------------------------------------------------------------------*/
466 19297 : imax = 5;
467 19297 : move16();
468 :
469 19297 : IF( GT_32( core_brate, ACELP_9k60 ) )
470 : {
471 7939 : imax = 7;
472 7939 : move16();
473 : }
474 131660 : FOR( i = 0; i < imax; i++ )
475 : {
476 112363 : bits_per_bands[i] = L_add( GSC_freq_bits_fx_Q18[bit_index], bit_fracf ); /*Q18*/
477 112363 : move32(); /* Q18 */
478 112363 : bit_index = add( bit_index, 1 );
479 : }
480 :
481 19297 : IF( EQ_16( L_frame, L_FRAME16k ) && GT_32( core_brate, ACELP_16k40 ) )
482 : {
483 4887 : bit_index = 0;
484 4887 : move16();
485 4887 : i = sub( imax, 1 );
486 4887 : bits_per_bands[i] = L_add( bits_per_bands[i], L_shl( Compl_GSC_freq_bits[bit_index], Q18 ) ); /*Q18*/
487 4887 : move32();
488 4887 : i = add( i, 1 );
489 4887 : bit_index = add( bit_index, 1 );
490 :
491 19548 : FOR( ; i < 10; i++ )
492 : {
493 14661 : 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 14661 : move32();
495 14661 : bit_index = add( bit_index, 1 );
496 : }
497 : }
498 :
499 19297 : IF( Diff_len == 0 )
500 : {
501 4359 : bit_index = add( bit_index_mem, 10 );
502 34872 : FOR( i = 0; i < 7; i++ )
503 : {
504 30513 : bits_per_bands[i] = L_add( bits_per_bands[i], GSC_freq_bits_fx_Q18[bit_index] ); /*Q18*/
505 30513 : move32(); /*chk Q18 */
506 30513 : bit_index = add( bit_index, 1 );
507 : }
508 : }
509 :
510 19297 : IF( bit_fracf < 0 )
511 : {
512 49929 : FOR( j = 0; j < nb_tot_bands; j++ )
513 : {
514 46992 : bits_per_bands[j] = L_max( bits_per_bands[j], 0 );
515 46992 : move32();
516 : }
517 : }
518 :
519 : /*--------------------------------------------------------------------------
520 : * Complete the bit allocation per frequency band for 16kHz high brate mode
521 : *--------------------------------------------------------------------------*/
522 :
523 19297 : test();
524 19297 : 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 26777 : FOR( j = st_band; j < nb_bands; j++ )
548 : {
549 7480 : bits_per_bands[j] = L_shl( bit_new_bands, 18 );
550 7480 : 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 19297 : bandoffset = sub( nb_tot_bands, add( nb_bands, 2 ) );
560 :
561 19297 : test();
562 19297 : test();
563 19297 : test();
564 19297 : test();
565 19297 : test();
566 19297 : IF( LE_16( noise_lev, NOISE_LEVEL_SP1a ) )
567 : {
568 8 : bandoffset = sub( bandoffset, 1 );
569 : }
570 19289 : 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 5733 : bandoffset = add( bandoffset, 1 );
574 : }
575 :
576 19297 : 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 349581 : FOR( j = 0; j < nb_tot_bands; j++ )
584 : {
585 330284 : max_ener_band[j] = -10;
586 330284 : move16();
587 : }
588 146321 : FOR( j = 0; j < st_band; j++ )
589 : {
590 127024 : max_ener_band[j] = j;
591 127024 : move16();
592 127024 : ener_vec[j] = -10;
593 127024 : move16();
594 : }
595 19297 : pos = st_band;
596 19297 : move16();
597 26777 : FOR( ; j < nb_bands; j++ )
598 : {
599 7480 : i = maximum_fx( ener_vec, sub( nb_tot_bands, bandoffset ), &etmp );
600 7480 : pos = s_max( pos, i );
601 7480 : max_ener_band[j] = i;
602 7480 : move16();
603 7480 : ener_vec[i] = -10;
604 7480 : move16();
605 : }
606 :
607 : /* re-allocate bits to the frames such that the highest band with allocated bits is higher than the threshold */
608 19297 : test();
609 19297 : test();
610 19297 : test();
611 19297 : 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 49891 : sum_bit = 0;
666 49891 : move16();
667 49891 : j = 0;
668 49891 : move16();
669 417271 : FOR( i = 0; i < nb_bands; i++ )
670 : {
671 367380 : L_tmp = Mult_32_16( sum_bit, 10923 /*1/3 in Q15*/ );
672 :
673 367380 : 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 367380 : 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 49891 : 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 49891 : sum_bit = 0;
702 49891 : move32();
703 417271 : FOR( i = 0; i < nb_bands; i++ )
704 : {
705 367380 : bits_per_bands[i] = L_shl( L_shr( bits_per_bands[i], Q18 ), Q18 );
706 367380 : move32();
707 367380 : sum_bit = L_add( sum_bit, L_shr( bits_per_bands[i], Q18 ) ); /*Q0*/
708 : }
709 :
710 49891 : test();
711 49891 : 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 13775 : reajust_bits_fx( bits_per_bands, 0, nb_bands, (Word16) sum_bit, *bit );
714 : }
715 : ELSE
716 : {
717 36116 : reajust_bits_fx( bits_per_bands, sub( nb_bands, 1 ), 0, (Word16) sum_bit, *bit );
718 : }
719 49891 : w_sum_bit = 0;
720 49891 : move16();
721 417271 : FOR( i = 0; i < nb_bands; i++ )
722 : {
723 367380 : out_bits_per_bands[i] = shl( extract_l( L_shr( bits_per_bands[i], 18 ) ), 3 );
724 367380 : move16();
725 367380 : w_sum_bit = add( w_sum_bit, out_bits_per_bands[i] ); /* Q3 */
726 : }
727 49891 : tmp = shl( *bit, 3 );
728 :
729 : /*--------------------------------------------------------------------------
730 : * Recompute the real number/length of frequency bands to encode
731 : *--------------------------------------------------------------------------*/
732 49891 : *nb_subbands = nb_bands;
733 49891 : move16();
734 49891 : *pvq_len = shl( *nb_subbands, 4 );
735 49891 : move16();
736 :
737 : /*--------------------------------------------------------------------------
738 : * Concatenate bands (encoder only)
739 : *--------------------------------------------------------------------------*/
740 49891 : IF( exc_diff != NULL )
741 : {
742 209762 : FOR( j = 0; j < nb_bands; j++ )
743 : {
744 184683 : Copy( exc_diff + shl( max_ener_band[j], 4 ), concat_in + shl( j, 4 ), 16 );
745 : }
746 : }
747 : }
748 : ELSE /* *bit == 0 */
749 : {
750 738 : set16_fx( out_bits_per_bands, 0, nb_tot_bands );
751 738 : *nb_subbands = 0;
752 738 : move16();
753 738 : *pvq_len = 0;
754 738 : move16();
755 : }
756 :
757 50629 : return;
758 : }
759 :
760 :
761 : /*-------------------------------------------------------------------*
762 : * reajust_bits()
763 : *
764 : *
765 : *-------------------------------------------------------------------*/
766 :
767 49891 : 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 49891 : incr = 1;
778 49891 : move16();
779 49891 : if ( LT_16( end_band, st_band ) )
780 : {
781 36116 : incr = -1;
782 36116 : move16();
783 : }
784 :
785 49891 : 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 49891 : bit_bdgt = bit_bdgt_in;
797 49891 : move16();
798 49891 : sum_bit = sum_bit_in;
799 49891 : move16();
800 49891 : amount_to_add = 1;
801 49891 : move16();
802 : }
803 :
804 49891 : i = st_band;
805 49891 : move16();
806 228268 : WHILE( GT_16( bit_bdgt, sum_bit ) )
807 : {
808 : /* if (amount_to_add > 0 || (amount_to_add < 0 && bits_per_bands[i] > 1))*/
809 178377 : test();
810 178377 : test();
811 178377 : 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 178377 : bits_per_bands[i] = L_add( bits_per_bands[i], L_shl( amount_to_add, Q18 ) ); /*Q18*/
815 178377 : move32();
816 178377 : sum_bit = add( sum_bit, abs_s( amount_to_add ) );
817 : }
818 :
819 178377 : i = add( i, incr );
820 178377 : if ( EQ_16( i, end_band ) )
821 : {
822 4418 : i = st_band;
823 4418 : move16();
824 : }
825 : }
826 :
827 49891 : 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 36095 : 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 36095 : inv_bandQ15 = 6553; /*Q15*/
844 36095 : move16();
845 36095 : IF( EQ_16( nb_band, 7 ) )
846 : {
847 18604 : inv_bandQ15 = 4681; /*Q15*/
848 18604 : move16();
849 : }
850 17491 : ELSE IF( EQ_16( nb_band, 3 ) )
851 : {
852 0 : inv_bandQ15 = 10922; /*Q15*/
853 0 : move16();
854 : }
855 17491 : ELSE IF( EQ_16( nb_band, 4 ) )
856 : {
857 0 : inv_bandQ15 = 8192; /*Q15*/
858 0 : move16();
859 : }
860 17491 : ELSE IF( EQ_16( nb_band, 5 ) )
861 : {
862 12604 : inv_bandQ15 = 6536; /*Q15*/
863 12604 : move16();
864 : }
865 4887 : ELSE IF( EQ_16( nb_band, 10 ) )
866 : {
867 4887 : inv_bandQ15 = 3277; /*Q15*/
868 4887 : 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 36095 : L_out = L_mult( inv_bandQ15, shl( remaining_bits, 2 ) ); /*Q18*/
877 :
878 36095 : return ( L_out );
879 : }
880 :
881 :
882 : /* o : Number of band to encode */
883 13796 : 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 13796 : SWB_bit_budget = *bit; /* Q0 */
903 13796 : st_band = 5;
904 13796 : nb_bands_max = nb_tot_bands_in;
905 :
906 13796 : move16();
907 13796 : move16();
908 13796 : move16();
909 :
910 13796 : 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 13796 : 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 13796 : 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 13796 : if ( LE_16( GSC_IVAS_mode, 3 ) )
925 : {
926 13796 : nb_bands_max = sub( nb_bands_max, 6 );
927 : }
928 13796 : IF( EQ_16( GSC_IVAS_mode, 2 ) )
929 : {
930 : /* bit_fracf += 0.1f; */
931 620 : bit_fracf = L_add( bit_fracf, Q18_0_1 ); /* Q18*/
932 620 : nb_bands_max = sub( nb_bands_max, 1 );
933 : }
934 13796 : IF( EQ_16( GSC_IVAS_mode, 3 ) )
935 : {
936 : /* bit_fracf -= 0.1f; */
937 8810 : bit_fracf = L_sub( bit_fracf, Q18_0_1 ); /* Q18*/
938 8810 : 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 13796 : nb_bands_adj = Q18_1_0;
946 13796 : move32();
947 13796 : test();
948 13796 : test();
949 13796 : 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 4309 : nb_bands_adj = L_sub( Mpy_32_32( Q31_0_0125, L_shl( SWB_bit_budget, Q18 ) ), Q18_0_75 ); /* Q18 */
953 : }
954 9487 : 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 12 : 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 13796 : nb_bands_max = round_fx( Mpy_32_16_1( L_shl( nb_bands_adj, 5 ), shl( nb_bands_max, 10 - 2 ) ) ); /* Q0 */
962 13796 : 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 13796 : 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 13796 : mb = Mpy_32_32( Q31_0_17, bit_fracf ); /* Q18 */
973 :
974 : /* mp = 2 * DSR_NB_PULSE;*/
975 13796 : mp = Q18_2_0xDSR_NB_PULSE;
976 13796 : move32();
977 :
978 13796 : test();
979 13796 : IF( LT_32( core_brate, GSC_L_RATE_STG ) && EQ_16( GSC_IVAS_mode, 3 ) )
980 : {
981 : /* mp = 1.5f * DSR_NB_PULSE;*/
982 8514 : mp = Q18_1_5xDSR_NB_PULSE;
983 8514 : move32();
984 : }
985 5282 : ELSE IF( LT_32( core_brate, GSC_L_RATE_STG ) )
986 : {
987 : /* mp = DSR_NB_PULSE;*/
988 4921 : mp = Q18_DSR_NB_PULSE;
989 4921 : 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 13796 : d_tmp = Find_norm_inv_fx( L_add( mp, mb ), &e_div );
998 13796 : Ltmp = Mpy_32_16_1( L_sub( L_shl( SWB_bit_budget, Q18 ), bit_fracf ), d_tmp ); /* Perform mult by 1/den */
999 13796 : 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 13796 : mp = L_min( mp, mb );
1001 :
1002 13796 : tmp16 = sub( add( nb_tot_bands_loc, st_band ), nb_bands_max );
1003 13796 : IF( tmp16 > 0 )
1004 : {
1005 : /* bit_adj = ( ( mb + mp ) / 2 ) * ( nb_tot_bands_loc + st_band - nb_bands_max ); */
1006 4528 : 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 4528 : bit_adj = L_max( 0, bit_adj );
1008 4528 : nb_tot_bands_loc = sub( nb_bands_max, st_band );
1009 : /* bit_fracf += bit_adj; */
1010 4528 : bit_fracf = L_add( bit_fracf, bit_adj ); /* Q18 */
1011 : }
1012 13796 : nb_tot_bands_loc = add( nb_tot_bands_loc, st_band );
1013 :
1014 : /* Allocate bits to LF */
1015 : /* etmp = 0.23f; */
1016 13796 : etmp_32fx = Q31_0_23;
1017 13796 : move32();
1018 82776 : FOR( j = 0; j < st_band; j++ )
1019 : {
1020 68980 : i = j;
1021 68980 : move16();
1022 68980 : max_ener_band[j] = i;
1023 68980 : move16();
1024 68980 : ener_vec[i] = MIN16B;
1025 68980 : move16();
1026 : /* bits_per_bands[j] = etmp * bit_fracf; */
1027 68980 : bits_per_bands[j] = Mpy_32_32( bit_fracf, etmp_32fx ); /* 18 + 31 + 1 - 32 = Q18 */
1028 68980 : move32();
1029 : /* etmp -= 0.015f; */
1030 68980 : etmp_32fx = L_sub( etmp_32fx, Q31_0_015 ); /* Q18 */
1031 : }
1032 :
1033 : /* SWB_bit_budget -= bit_fracf; */
1034 13796 : SWB_bit_budget = L_sub( L_shl( SWB_bit_budget, Q18 ), bit_fracf ); /* Q0->Q18 */
1035 :
1036 : /* Find low energy band in HF */
1037 13796 : set16_fx( nb_pulse_per_band, 2, MBANDS_GN_BITALLOC16k );
1038 33166 : FOR( i = st_band + 2; i < nb_tot_bands_loc - 1; i++ )
1039 : {
1040 19370 : test();
1041 19370 : 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 6570 : nb_pulse_per_band[i] = 1;
1044 6570 : move16();
1045 : }
1046 : }
1047 62598 : FOR( j = st_band; j < nb_tot_bands_loc; j++ )
1048 : {
1049 48802 : IF( GT_16( j, 6 ) )
1050 : {
1051 28949 : i = maximum_fx( ener_vec, nb_tot_bands_loc, &etmp );
1052 : }
1053 : ELSE
1054 : {
1055 19853 : i = j;
1056 19853 : move16();
1057 : }
1058 48802 : max_ener_band[j] = i;
1059 48802 : move16();
1060 48802 : ener_vec[i] = MIN16B;
1061 48802 : move16();
1062 : }
1063 :
1064 : /* Recompute the final bit distribution for HF */
1065 13796 : 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 9993 : d_tmp = Find_norm_inv_fx( L_deposit_h( sub( nb_tot_bands_loc, st_band ) ), &e_div );
1073 9993 : 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 9993 : bit_fracf = L_shr( Mpy_32_16_1( L_sub( mb, mp ), d_tmp ), e_div ); /* Q18 */
1077 :
1078 9993 : mb = L_sub( mb, bit_fracf );
1079 : /* Do the distribution */
1080 58795 : FOR( j = st_band; j < nb_tot_bands_loc; j++ )
1081 : {
1082 48802 : bits_per_bands[max_ener_band[j]] = Q18_DSR_NB_PULSE;
1083 48802 : move32();
1084 48802 : if ( GT_16( nb_pulse_per_band[max_ener_band[j]], 1 ) )
1085 : {
1086 42232 : bits_per_bands[max_ener_band[j]] = mb;
1087 42232 : move32();
1088 : }
1089 48802 : mb = L_sub( mb, bit_fracf );
1090 48802 : 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 13796 : IF( SWB_bit_budget > 0 )
1096 : {
1097 13789 : i = sub( st_band, 1 );
1098 66504 : WHILE( SWB_bit_budget > 0 )
1099 : {
1100 : /* bits_per_bands[i]++; */
1101 52715 : bits_per_bands[i] = L_add( bits_per_bands[i], Q18_1_0 ); /*Q18*/
1102 52715 : move32();
1103 : /* SWB_bit_budget--; */
1104 52715 : SWB_bit_budget = L_sub( SWB_bit_budget, Q18_1_0 ); /*Q18*/
1105 52715 : i = sub( i, 1 );
1106 52715 : if ( EQ_16( i, -1 ) )
1107 : {
1108 4609 : i = sub( st_band, 1 );
1109 : }
1110 : }
1111 : }
1112 :
1113 : /*nb_bands = nb_tot_bands_loc;*/
1114 :
1115 13796 : sum_bit = 0;
1116 13796 : move32();
1117 13796 : j = 0;
1118 13796 : move16();
1119 131578 : FOR( i = 0; i < nb_tot_bands_loc; i++ )
1120 : {
1121 : /* if (bits_per_bands[i] > 112) */
1122 117782 : 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 117782 : 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 13796 : 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 13796 : return nb_tot_bands_loc;
1153 : }
1154 :
1155 :
1156 : /* Find normalized 1 / ToDivide */
1157 23789 : static Word16 Find_norm_inv_fx(
1158 : const Word32 ToDivide,
1159 : Word16 *e_div )
1160 : {
1161 : Word16 d_tmp, e_tmp;
1162 :
1163 23789 : e_tmp = norm_l( ToDivide );
1164 23789 : d_tmp = round_fx( L_shl( ToDivide, e_tmp ) );
1165 23789 : d_tmp = div_s( 16384, d_tmp ); /* 1.0 in Q14, dividend is normalize so >= 16384 as required for the division */
1166 23789 : *e_div = sub( 14, e_tmp );
1167 23789 : move16();
1168 :
1169 23789 : return d_tmp;
1170 : }
|