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