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 :
12 : /*==================================================================================*/
13 : /* FUNCTION : void bands_and_bit_alloc_fx(); */
14 : /*----------------------------------------------------------------------------------*/
15 : /* PURPOSE : AC mode (GSC) bands and bits allocation */
16 : /*----------------------------------------------------------------------------------*/
17 : /* INPUT ARGUMENTS : */
18 : /* _ (Word16) cor_strong_limit : HF correlation */
19 : /* _ (Word16) noise_lev : dwn scaling factor Q0 */
20 : /* _ (Word32) core_brate : core codec used Q0 */
21 : /* _ (Word16) Diff_len : Lenght of the difference signal Q0 */
22 : /* _ (Word16) bits_used : Number of bit used before frequency Q0 */
23 : /* _ (Word16) idx : Energy band 14 Q0 */
24 : /* _ (Word16*) exc_diff : Difference signal to quantize (Encoder only) */
25 : /* _ (Word16) coder_type : coding type Q0 */
26 : /* _ (Word16) bwidth : input signal bandwidth Q0 */
27 : /*----------------------------------------------------------------------------------*/
28 : /* OUTPUT ARGUMENTS : */
29 : /* _ (Word16*) max_ener_band : Sorted order */
30 : /* _ (Word16*) nb_subbands : Number of subband allowed Q0 */
31 : /* _ (Word16*) concat_in : Concatened PVQ's input vector (Encoder Only) */
32 : /* _ (Word16*) pvq_len : Number of bin covered with the PVQ Q0 */
33 : /*----------------------------------------------------------------------------------*/
34 : /* INPUT/OUTPUT ARGUMENTS : */
35 : /* _ (Word16*) bit :Number of bit allowed for frequency quantization */
36 : /* _ (Word16*) Ener_per_bd_iQ : Quantized energy vector Q13 */
37 : /* _ (Word32*) bits_per_bands : Number of bit allowed per allowed subband Q18 */
38 : /*----------------------------------------------------------------------------------*/
39 : /* RETURN ARGUMENTS : */
40 : /* _ None */
41 : /*==================================================================================*/
42 8 : void bands_and_bit_alloc_fx(
43 : const Word16 cor_strong_limit, /* i : HF correlation */
44 : const Word16 noise_lev, /* i : dwn scaling factor */
45 : const Word32 core_brate, /* i : core bit rate */
46 : const Word16 Diff_len, /* i : Lenght of the difference signal (before pure spectral)*/
47 : const Word16 bits_used, /* i : Number of bit used before frequency Q */
48 : Word16 *bit, /* i/o: Number of bit allowed for frequency quantization */
49 : const Word16 *Ener_per_bd_iQ, /* i/o: Quantized energy vector */
50 : Word16 *max_ener_band, /* o : Sorted order */
51 : Word16 *out_bits_per_bands, /* i/o: Number of bit allowed per allowed subband Q3 */
52 : Word16 *nb_subbands, /* o : Number of subband allowed */
53 : const Word16 *exc_diff, /* i : Difference signal to quantize (encoder side only) */
54 : Word16 *concat_in, /* o : Concatened PVQ's input vector (encoder side only) */
55 : Word16 *pvq_len, /* o : Number of bin covered with the PVQ */
56 : const Word16 coder_type, /* i : coding type */
57 : const Word16 bwidth, /* i : input signal bandwidth */
58 : const Word16 GSC_noisy_speech, /* i : GSC noisy speech flag */
59 : const Word16 L_frame, /* i : frame length */
60 : const Word16 element_mode, /* i : element mode */
61 : const Word16 GSC_IVAS_mode /* i : GSC IVAS mode */
62 : )
63 : {
64 :
65 : Word16 bandoffset, i, j, nb_bands_max, bit_new_bands, bit_tmp, st_band, nb_bands;
66 : Word16 ener_vec[MBANDS_GN_BITALLOC16k]; /*Q12 */
67 8 : Word16 nb_tot_bands = 16;
68 : Word16 bit_index, bit_index_mem, imax;
69 : Word32 L_tmp;
70 : Word32 sum_bit, bit_fracf;
71 : Word16 etmp;
72 : Word16 tmp;
73 : Word16 Ener_per_bd_iQ_tmp[MBANDS_GN_BITALLOC16k];
74 : Word16 pos, band;
75 : Word16 SWB_bit_budget;
76 : Word32 bits_per_bands[MBANDS_GN_BITALLOC16k];
77 : Word16 w_sum_bit;
78 : (void) GSC_IVAS_mode;
79 : (void) element_mode;
80 : #ifndef ISSUE_1836_replace_overflow_libcom
81 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
82 : Flag Overflow = 0;
83 : #endif
84 : #endif
85 8 : nb_tot_bands = MBANDS_GN;
86 8 : move16();
87 8 : if ( EQ_16( L_frame, L_FRAME16k ) )
88 : {
89 0 : nb_tot_bands = MBANDS_GN_BITALLOC16k;
90 0 : move16();
91 : }
92 8 : Copy( Ener_per_bd_iQ, Ener_per_bd_iQ_tmp, nb_tot_bands );
93 :
94 8 : set32_fx( bits_per_bands, 0, MBANDS_GN_BITALLOC16k );
95 8 : set16_fx( out_bits_per_bands, 0, MBANDS_GN_BITALLOC16k );
96 :
97 : /* To adapt current energy band to PVQ freq band for sorting*/
98 : #ifdef ISSUE_1836_replace_overflow_libcom
99 8 : ener_vec[0] = add_sat( Ener_per_bd_iQ[0], Ener_per_bd_iQ[1] ); /*Q12 */
100 : #else
101 : ener_vec[0] = add_o( Ener_per_bd_iQ[0], Ener_per_bd_iQ[1], &Overflow ); /*Q12 */
102 : #endif
103 8 : Copy( Ener_per_bd_iQ_tmp + 1, ener_vec, MBANDS_GN - 1 ); /*Q12 */
104 8 : ener_vec[MBANDS_GN - 1] = ener_vec[MBANDS_GN - 2];
105 8 : move16();
106 8 : IF( EQ_16( L_frame, L_FRAME16k ) )
107 : {
108 0 : ener_vec[16] = Ener_per_bd_iQ[16];
109 0 : ener_vec[17] = mac_r( L_mult( Ener_per_bd_iQ[16], 16384 ), Ener_per_bd_iQ[17], 16384 );
110 0 : ener_vec[18] = Ener_per_bd_iQ[17];
111 0 : ener_vec[19] = mult_r( Ener_per_bd_iQ[17], 26214 );
112 0 : move16();
113 0 : move16();
114 0 : move16();
115 0 : move16();
116 : }
117 :
118 : /*------------------------------------------------------------------------
119 : * Determination of the number of bits available to the frequency domain
120 : * Allocation of a maximum number of band to be encoded
121 : *-----------------------------------------------------------------------*/
122 :
123 8 : nb_bands_max = nb_tot_bands;
124 8 : move16();
125 8 : bit_new_bands = 5;
126 8 : move16();
127 8 : bit_index = i_mult2( BRATE2IDX_fx( core_brate ), 17 );
128 8 : bit_index_mem = bit_index;
129 8 : move16();
130 :
131 8 : test();
132 8 : test();
133 8 : IF( ( EQ_16( coder_type, AUDIO ) || EQ_16( coder_type, INACTIVE ) ) && EQ_16( bwidth, NB ) )
134 : {
135 0 : IF( GE_32( core_brate, ACELP_9k60 ) )
136 : {
137 : /* *bit = (short)(core_brate*(1.0f/50) + 0.5f) - bits_used - 25; */
138 0 : L_tmp = Mult_32_16( core_brate, 20971 );
139 0 : tmp = extract_l( L_shr_r( L_tmp, 5 ) );
140 0 : *bit = sub( sub( tmp, bits_used ), 25 );
141 0 : move16();
142 : }
143 : ELSE
144 : {
145 0 : L_tmp = Mult_32_16( core_brate, 20971 );
146 0 : tmp = extract_l( L_shr_r( L_tmp, 5 ) );
147 0 : *bit = sub( sub( tmp, bits_used ), 21 );
148 0 : move16();
149 : }
150 0 : nb_tot_bands = 10;
151 0 : move16();
152 : }
153 : ELSE
154 : {
155 : /* *bit = (short)(core_brate*(1.0f/50) + 0.5f) - bits_used - GSC_freq_bits[bit_index]; */
156 :
157 8 : L_tmp = Mult_32_16( core_brate, 20971 );
158 8 : tmp = extract_l( L_shr_r( L_tmp, 5 ) );
159 8 : *bit = sub( sub( tmp, bits_used ), GSC_freq_bits[bit_index] );
160 8 : move16();
161 : }
162 :
163 8 : IF( EQ_16( GSC_noisy_speech, 1 ) )
164 : {
165 0 : SWB_bit_budget = *bit;
166 0 : move16();
167 0 : nb_bands = 5;
168 0 : move16();
169 :
170 0 : st_band = nb_bands;
171 0 : move16();
172 :
173 0 : set32_fx( bits_per_bands, 0, MBANDS_GN );
174 : /*bit_fracf = (1.0f/nb_bands)*(SWB_bit_budget); */
175 0 : bit_fracf = L_mult( div_s( 1, nb_bands ), shl( SWB_bit_budget, 2 ) ); /* Q18 */
176 :
177 0 : nb_tot_bands = sub( nb_bands_max, 6 );
178 0 : nb_tot_bands = s_min( nb_tot_bands, 16 );
179 :
180 0 : FOR( j = 0; j < 2; j++ )
181 : {
182 0 : i = j;
183 0 : move16();
184 0 : max_ener_band[j] = i;
185 0 : move16();
186 0 : ener_vec[i] = 0;
187 0 : move16();
188 : }
189 :
190 0 : FOR( ; j < nb_bands; j++ )
191 : {
192 0 : i = maximum_fx( ener_vec, nb_tot_bands, &etmp );
193 0 : max_ener_band[j] = i;
194 0 : move16();
195 0 : ener_vec[i] = 0;
196 0 : move16();
197 : }
198 :
199 0 : set32_fx( bits_per_bands, bit_fracf, nb_bands );
200 : }
201 : ELSE
202 : {
203 8 : bit_index++;
204 8 : bit_tmp = sub( *bit, GSC_freq_bits[bit_index] );
205 8 : bit_index++;
206 8 : nb_bands_max = add( nb_bands_max, GSC_freq_bits[bit_index] );
207 8 : bit_index++;
208 :
209 8 : *pvq_len = 112;
210 8 : move16();
211 8 : st_band = 7;
212 8 : move16();
213 :
214 8 : IF( LE_32( core_brate, ACELP_9k60 ) )
215 : {
216 0 : *pvq_len = 80;
217 0 : move16();
218 0 : st_band = 5;
219 0 : move16();
220 :
221 0 : IF( Diff_len == 0 )
222 : {
223 0 : nb_bands_max = add( nb_bands_max, 2 );
224 0 : bit_tmp = sub( bit_tmp, 13 );
225 : }
226 : }
227 :
228 8 : ELSE IF( Diff_len == 0 )
229 : {
230 0 : nb_bands_max = add( nb_bands_max, 2 );
231 0 : bit_tmp = sub( bit_tmp, 17 );
232 : }
233 :
234 8 : nb_bands = shr( *pvq_len, 4 );
235 :
236 : /*------------------------------------------------------------------------
237 : * Ajustement of the maximum number of bands in function of the
238 : * dynamics of the spectrum (more or less speech like)
239 : *-----------------------------------------------------------------------*/
240 8 : test();
241 8 : test();
242 8 : test();
243 8 : test();
244 8 : IF( EQ_16( coder_type, INACTIVE ) || GE_16( noise_lev, NOISE_LEVEL_SP3 ) )
245 : {
246 : /* Probably classification error -> concentrate bits on LF */
247 8 : if ( GE_32( core_brate, ACELP_8k00 ) )
248 : {
249 8 : nb_bands_max = add( nb_bands, 1 );
250 : }
251 : else
252 : {
253 0 : nb_bands_max = nb_bands;
254 0 : move16();
255 : }
256 : }
257 0 : ELSE IF( GE_16( noise_lev, NOISE_LEVEL_SP2 ) ||
258 : ( 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 */
259 : {
260 0 : nb_bands_max = sub( nb_bands_max, 2 );
261 : }
262 0 : ELSE IF( GE_16( noise_lev, NOISE_LEVEL_SP1 ) ) /* Very low dynamic, tend to speech, code less HF */
263 : {
264 0 : nb_bands_max = sub( nb_bands_max, 1 );
265 : }
266 :
267 8 : test();
268 8 : if ( ( EQ_16( bwidth, NB ) ) && GT_16( nb_bands_max, 10 ) )
269 : {
270 0 : nb_bands_max = 10;
271 0 : move16();
272 : }
273 :
274 : /*------------------------------------------------------------------------
275 : * Find extra number of band to code according to bit rate availables
276 : *-----------------------------------------------------------------------*/
277 8 : test();
278 16 : WHILE( GE_16( bit_tmp, bit_new_bands ) && LE_16( nb_bands, sub( nb_bands_max, 1 ) ) )
279 : {
280 8 : test();
281 8 : bit_tmp = sub( bit_tmp, bit_new_bands );
282 8 : nb_bands = add( nb_bands, 1 );
283 : }
284 :
285 : /*------------------------------------------------------------------------
286 : * Fractional bits to distribute on the first x bands
287 : *-----------------------------------------------------------------------*/
288 :
289 : {
290 8 : bit_fracf = L_mult( div_s( 1, st_band ), shl( bit_tmp, 2 ) ); /* Q18 */
291 : }
292 : /*------------------------------------------------------------------------
293 : * Complete the bit allocation per frequency band
294 : *-----------------------------------------------------------------------*/
295 8 : imax = 5;
296 8 : move16();
297 :
298 8 : if ( GT_32( core_brate, ACELP_9k60 ) )
299 : {
300 8 : imax = 7;
301 8 : move16();
302 : }
303 64 : FOR( i = 0; i < imax; i++ )
304 : {
305 56 : bits_per_bands[i] = L_add( GSC_freq_bits_fx[bit_index], bit_fracf );
306 56 : move32(); /* Q18 */
307 56 : bit_index = add( bit_index, 1 );
308 : }
309 :
310 8 : IF( Diff_len == 0 )
311 : {
312 0 : bit_index = add( bit_index_mem, 10 );
313 0 : FOR( i = 0; i < 7; i++ )
314 : {
315 0 : bits_per_bands[i] = L_add( bits_per_bands[i], GSC_freq_bits_fx[bit_index] );
316 0 : move32(); /*chk Q18 */
317 0 : bit_index = add( bit_index, 1 );
318 : }
319 : }
320 :
321 : /*--------------------------------------------------------------------------
322 : * Complete the bit allocation per frequency band for 16kHz high brate mode
323 : *--------------------------------------------------------------------------*/
324 :
325 : {
326 16 : FOR( j = st_band; j < nb_bands; j++ )
327 : {
328 8 : bits_per_bands[j] = L_shl( bit_new_bands, 18 );
329 8 : move32(); /*chk Q18 */
330 : }
331 : }
332 :
333 : /*--------------------------------------------------------------------------
334 : * Compute a maximum band (band offset) for the search on maximal energy
335 : * This is function of the spectral dynamic and the bitrate
336 : *--------------------------------------------------------------------------*/
337 :
338 8 : bandoffset = sub( nb_tot_bands, add( nb_bands, 2 ) );
339 :
340 8 : test();
341 8 : test();
342 8 : test();
343 8 : test();
344 8 : test();
345 8 : IF( LE_16( noise_lev, NOISE_LEVEL_SP1a ) )
346 : {
347 0 : bandoffset = sub( bandoffset, 1 );
348 : }
349 8 : ELSE IF( ( LE_32( core_brate, ACELP_13k20 ) && ( EQ_16( coder_type, INACTIVE ) || GE_16( noise_lev, NOISE_LEVEL_SP3 ) ) ) ||
350 : ( LE_32( core_brate, ACELP_13k20 ) && GE_32( core_brate, ACELP_9k60 ) && cor_strong_limit == 0 ) )
351 : {
352 8 : bandoffset = add( bandoffset, 1 );
353 : }
354 :
355 8 : bandoffset = s_max( bandoffset, 0 );
356 :
357 : /*--------------------------------------------------------------------------
358 : * Initiazed sorted vector
359 : * For the first x bands to be included in th final sorted vector
360 : * Sort the remaining bands in decrease energy order
361 : *--------------------------------------------------------------------------*/
362 136 : FOR( j = 0; j < nb_tot_bands; j++ )
363 : {
364 128 : max_ener_band[j] = -10;
365 128 : move16();
366 : }
367 64 : FOR( j = 0; j < st_band; j++ )
368 : {
369 56 : max_ener_band[j] = j;
370 56 : move16();
371 56 : ener_vec[j] = -10;
372 56 : move16();
373 : }
374 8 : pos = st_band;
375 8 : move16();
376 16 : FOR( ; j < nb_bands; j++ )
377 : {
378 8 : i = maximum_fx( ener_vec, sub( nb_tot_bands, bandoffset ), &etmp );
379 8 : pos = s_max( pos, i );
380 8 : max_ener_band[j] = i;
381 8 : move16();
382 8 : ener_vec[i] = -10;
383 8 : move16();
384 : }
385 :
386 : /* re-allocate bits to the frames such that the highest band with allocated bits is higher than the threshold */
387 8 : test();
388 8 : test();
389 8 : test();
390 8 : IF( GT_16( sub( nb_tot_bands, bandoffset ), nb_bands ) && ( GT_16( pos, 7 ) && EQ_32( core_brate, ACELP_8k00 ) ) && EQ_16( bwidth, WB ) )
391 : {
392 0 : band = sub( nb_tot_bands, add( bandoffset, nb_bands ) );
393 0 : FOR( j = 0; j < band; j++ )
394 : {
395 0 : i = maximum_fx( ener_vec, sub( nb_tot_bands, bandoffset ), &etmp );
396 0 : max_ener_band[add( nb_bands, j )] = i;
397 0 : move16();
398 0 : ener_vec[i] = -10;
399 0 : move16();
400 0 : bits_per_bands[add( nb_bands, j )] = 1310720;
401 0 : move32(); /*Q18 */
402 : }
403 0 : nb_bands = add( nb_bands, band );
404 :
405 0 : bit_tmp = i_mult2( band, 5 );
406 :
407 0 : IF( LE_16( band, 2 ) )
408 : {
409 0 : FOR( j = sub( st_band, 1 ); j < nb_bands; j++ )
410 : {
411 0 : bits_per_bands[j] = L_add( bits_per_bands[j], 262144 ); /*Q18 */
412 0 : move32();
413 : }
414 0 : bit_tmp = add( bit_tmp, add( sub( nb_bands, st_band ), 1 ) );
415 : }
416 :
417 0 : i = 0;
418 0 : move16();
419 0 : j = 0;
420 0 : move16();
421 0 : FOR( ; bit_tmp > 0; bit_tmp-- )
422 : {
423 0 : bits_per_bands[j] = L_sub( bits_per_bands[j], 262144 ); /*Q18 */
424 0 : j = add( j, 1 );
425 0 : if ( EQ_16( j, sub( st_band, i ) ) )
426 : {
427 0 : j = 0;
428 0 : move16();
429 : }
430 0 : test();
431 0 : if ( j == 0 && LT_16( i, sub( st_band, 1 ) ) )
432 : {
433 0 : i = add( i, 1 );
434 : }
435 : }
436 : }
437 : }
438 : /*--------------------------------------------------------------------------
439 : * Bit sum verification for GSC inactive at very high rate
440 : * The maximum number of bits per band of length 16 is 112
441 : * Redistribute the overage bits if needed
442 : *--------------------------------------------------------------------------*/
443 8 : sum_bit = 0;
444 8 : move16();
445 8 : j = 0;
446 8 : move16();
447 72 : FOR( i = 0; i < nb_bands; i++ )
448 : {
449 64 : L_tmp = Mult_32_16( sum_bit, 10923 );
450 :
451 64 : IF( GT_32( bits_per_bands[i], 29360128 ) ) /* 112 in Q18 */
452 : {
453 0 : sum_bit = L_add( sum_bit, L_sub( bits_per_bands[i], 29360128 ) ); /* Q18 */
454 0 : bits_per_bands[i] = 29360128;
455 0 : move32();
456 0 : j = add( i, 1 );
457 : }
458 64 : ELSE IF( GT_32( L_add( bits_per_bands[i], L_tmp ), 29360128 ) ) /* Q18 */
459 : {
460 0 : j = add( i, 1 );
461 : }
462 : }
463 :
464 8 : IF( sum_bit != 0 )
465 : {
466 0 : tmp = sub( nb_bands, j );
467 0 : sum_bit = Mult_32_16( sum_bit, div_s( 1, tmp ) ); /* Q18 */
468 0 : FOR( i = j; i < nb_bands; i++ )
469 : {
470 0 : bits_per_bands[i] = L_add( bits_per_bands[i], sum_bit );
471 0 : move32(); /* Q18 */
472 : }
473 : }
474 : /*--------------------------------------------------------------------------
475 : * second step of bit sum verification, normally sum_bit == *bit
476 : *--------------------------------------------------------------------------*/
477 8 : w_sum_bit = 0;
478 8 : move16();
479 72 : FOR( i = 0; i < nb_bands; i++ )
480 : {
481 64 : out_bits_per_bands[i] = shl( extract_l( L_shr( bits_per_bands[i], 18 ) ), 3 );
482 64 : move16();
483 64 : w_sum_bit = add( w_sum_bit, out_bits_per_bands[i] ); /* Q3 */
484 : }
485 8 : tmp = shl( *bit, 3 );
486 :
487 8 : IF( GT_16( tmp, w_sum_bit ) )
488 : {
489 8 : i = sub( nb_bands, 1 );
490 8 : move16();
491 56 : FOR( ; tmp > w_sum_bit; w_sum_bit += ( 1 << 3 ) )
492 : {
493 48 : out_bits_per_bands[i] = add( out_bits_per_bands[i], 1 << 3 );
494 48 : move16();
495 48 : i = sub( i, 1 );
496 48 : if ( i == 0 )
497 : {
498 0 : i = sub( nb_bands, 1 );
499 : }
500 : }
501 : }
502 :
503 : /*--------------------------------------------------------------------------
504 : * Recompute the real number/length of frequency bands to encode
505 : *--------------------------------------------------------------------------*/
506 8 : *nb_subbands = nb_bands;
507 8 : move16();
508 8 : *pvq_len = shl( *nb_subbands, 4 );
509 :
510 : /*--------------------------------------------------------------------------
511 : * Concatenate bands (encoder only)
512 : *--------------------------------------------------------------------------*/
513 8 : IF( exc_diff != NULL )
514 : {
515 72 : FOR( j = 0; j < nb_bands; j++ )
516 : {
517 64 : Copy( exc_diff + shl( max_ener_band[j], 4 ), concat_in + shl( j, 4 ), 16 );
518 : }
519 : }
520 :
521 8 : return;
522 : }
|