Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include <math.h>
43 : #include "cnst.h"
44 : #include "rom_com.h"
45 : #include "prot.h"
46 : #include "ivas_prot.h"
47 : #include "wmc_auto.h"
48 :
49 :
50 : /*-------------------------------------------------------------------*
51 : * Local constants
52 : *-------------------------------------------------------------------*/
53 :
54 : #define Q15_0_33 10922 /* 0.33 */
55 : #define Q18_0_1 26214 /* 0.1 */
56 : #define Q18_0_50 131072 /* 0.50 */
57 : #define Q18_0_75 196608 /* 0.75 */
58 : #define Q18_0_76 199229 /* 76/100 */
59 : #define Q18_1_0 262144 /* 1.0 */
60 : #define Q18_1_2 314573 /* 1.2 */
61 : #define Q18_112 29360128 /* 112 */
62 : #define Q18_DSR_NB_PULSE 1179648 /* 4.5 */
63 : #define Q18_1_5xDSR_NB_PULSE 1769472 /* 1.5x4.5 */
64 : #define Q18_2_0xDSR_NB_PULSE ( Q18_DSR_NB_PULSE << 1 ) /* 2.0x4.5 */
65 :
66 : #define Q31_0_00125 2684355 /* 0.125/100 */
67 : #define Q31_0_0125 26843546 /* 0.0125 */
68 : #define Q31_0_015 32212255 /* 0.0125 */
69 : #define Q31_0_02 42949673 /* 0.02 */
70 : #define Q31_0_17 365072220 /* 0.17 */
71 : #define Q31_0_23 493921239 /* 0.23 */
72 :
73 :
74 : /*-------------------------------------------------------------------*
75 : * Local function prototypes
76 : *-------------------------------------------------------------------*/
77 :
78 : static float Find_bit_frac( const int16_t nb_band, const int16_t remaining_bits );
79 :
80 : static void reajust_bits( float *bits_per_bands, const int16_t st_band, const int16_t end_band, const int16_t sum_bit_in, const int16_t bit_bdgt_in );
81 :
82 : static Word16 Find_norm_inv( const Word32 ToDivide, Word16 *e_div );
83 :
84 : static Word16 Find_bit_alloc_IVAS_int( const Word32 core_brate, const Word16 GSC_IVAS_mode, const Word16 Diff_len, const Word16 nb_tot_bands, const Word16 L_frame, Word16 *bit, Word16 *max_ener_band, float *ener_vec, float *bits_per_bands );
85 :
86 : static Word16 maximum_fx( const Word16 *vec_fx, const Word16 lvec_fx, Word16 *max_fx );
87 :
88 :
89 : /*-------------------------------------------------------------------*
90 : * bands_and_bit_alloc()
91 : *
92 : * AC mode (GSC) bands and bits allocation
93 : *-------------------------------------------------------------------*/
94 :
95 90525 : void bands_and_bit_alloc(
96 : const int16_t cor_strong_limit, /* i : HF correlation */
97 : const int16_t noise_lev, /* i : dwn scaling factor */
98 : const int32_t core_brate, /* i : core bitrate */
99 : const int16_t Diff_len, /* i : Lenght of the difference signal (before pure spectral)*/
100 : const int16_t bits_used, /* i : Number of bit used before frequency Q */
101 : int16_t *bit, /* i/o: Number of bit allowed for frequency quantization */
102 : float *Ener_per_bd_iQ, /* i/o: Quantized energy vector */
103 : int16_t *max_ener_band, /* o : Sorted order */
104 : int16_t *bits_per_bands_s, /* i/o: Number of bit allowed per allowed subband (Q3) */
105 : int16_t *nb_subbands, /* o : Number of subband allowed */
106 : const float *exc_diff, /* i : Difference signal to quantize (encoder side only) */
107 : float *concat_in, /* o : Concatened PVQ's input vector (encoder side only) */
108 : int16_t *pvq_len, /* o : Number of bin covered with the PVQ */
109 : const int16_t coder_type, /* i : coding type */
110 : const int16_t bwidth, /* i : input signal bandwidth */
111 : const int16_t GSC_noisy_speech, /* i : GSC noisy speech flag */
112 : const int16_t L_frame, /* i : frame length */
113 : const int16_t element_mode, /* i : element mode */
114 : const int16_t GSC_IVAS_mode /* i : GSC IVAS mode */
115 : )
116 : {
117 : int16_t bandoffset, i, j, nb_bands_max, bit_new_bands, bit_tmp, st_band, nb_bands;
118 : float bit_fracf, etmp;
119 : float sum_bit;
120 : float ener_vec[MBANDS_GN_BITALLOC16k];
121 : int16_t nb_tot_bands;
122 : int16_t bit_index, bit_index_mem, imax;
123 : int16_t pos, band;
124 : float SWB_bit_budget;
125 : float bits_per_bands[MBANDS_GN_BITALLOC16k];
126 : float fzero_val;
127 :
128 : /* initializations */
129 90525 : nb_tot_bands = 16;
130 90525 : set_f( bits_per_bands, 0.0f, MBANDS_GN_BITALLOC16k );
131 :
132 90525 : if ( L_frame == L_FRAME16k )
133 : {
134 10509 : nb_tot_bands = MBANDS_GN_BITALLOC16k;
135 : }
136 :
137 : /* To adapt current energy band to PVQ freq band for sorting*/
138 90525 : ener_vec[0] = Ener_per_bd_iQ[0] + Ener_per_bd_iQ[1];
139 90525 : mvr2r( Ener_per_bd_iQ + 1, ener_vec, 15 );
140 90525 : ener_vec[15] = ener_vec[14];
141 :
142 90525 : if ( L_frame == L_FRAME16k )
143 : {
144 10509 : ener_vec[16] = Ener_per_bd_iQ[16];
145 10509 : ener_vec[17] = ( Ener_per_bd_iQ[16] + Ener_per_bd_iQ[17] ) * 0.5f;
146 10509 : ener_vec[18] = Ener_per_bd_iQ[17];
147 10509 : ener_vec[19] = Ener_per_bd_iQ[17] * 0.8f;
148 : }
149 :
150 1580961 : for ( i = 0; i < nb_tot_bands; i++ )
151 : {
152 1490436 : ener_vec[i] = (float) ( (int16_t) ( ener_vec[i] * 4096.f + 0.5f ) );
153 : }
154 :
155 : /*------------------------------------------------------------------------
156 : * Determination of the number of bits available to the frequency domain
157 : * Allocation of a maximum number of band to be encoded
158 : *-----------------------------------------------------------------------*/
159 :
160 90525 : nb_bands_max = nb_tot_bands;
161 90525 : bit_new_bands = 5;
162 :
163 90525 : if ( core_brate > ACELP_16k40 && L_frame == L_FRAME16k )
164 : {
165 9569 : bit_new_bands = 7;
166 : }
167 :
168 90525 : i = 0;
169 420191 : while ( i < SIZE_BRATE_INTERMED_TBL )
170 : {
171 420191 : if ( core_brate <= brate_intermed_tbl[i] )
172 : {
173 89717 : break;
174 : }
175 :
176 330474 : if ( brate_intermed_tbl[i] == ACELP_24k40 )
177 : {
178 808 : break;
179 : }
180 :
181 329666 : i++;
182 : }
183 :
184 90525 : if ( element_mode > EVS_MONO && coder_type == AUDIO &&
185 28265 : core_brate <= STEREO_GSC_BIT_RATE_ALLOC && brate_intermed_tbl[i] == ACELP_9k60 ) /* Bit allocation should be mapped to 8 kb/s instead of 9.6 kb/s in this case */
186 : {
187 5469 : i--;
188 : }
189 :
190 90525 : bit_index = BRATE2IDX( brate_intermed_tbl[i] ) * 17;
191 :
192 90525 : bit_index_mem = bit_index;
193 :
194 90525 : if ( ( coder_type == AUDIO || coder_type == INACTIVE ) && bwidth == NB )
195 : {
196 0 : if ( core_brate >= ACELP_9k60 )
197 : {
198 0 : *bit = (int16_t) ( core_brate * ( 1.0f / FRAMES_PER_SEC ) + 0.5f ) - bits_used - 25;
199 : }
200 : else
201 : {
202 0 : *bit = (int16_t) ( core_brate * ( 1.0f / FRAMES_PER_SEC ) + 0.5f ) - bits_used - 21;
203 : }
204 :
205 0 : nb_tot_bands = 10;
206 : }
207 : else
208 : {
209 90525 : *bit = (int16_t) ( core_brate * ( 1.0f / FRAMES_PER_SEC ) + 0.5f ) - bits_used - GSC_freq_bits[bit_index];
210 : }
211 :
212 90525 : if ( L_frame == L_FRAME16k )
213 : {
214 10509 : *bit -= 8;
215 : }
216 :
217 90525 : if ( coder_type == INACTIVE && core_brate <= GSC_LRES_GAINQ_LIMIT ) /* can happen only for 2nd channel inactive */
218 : {
219 28014 : *bit += GSC_LRES_NB_NITS;
220 : }
221 :
222 90525 : if ( *bit > 0 )
223 : {
224 90525 : if ( GSC_IVAS_mode > 0 )
225 : {
226 19627 : nb_tot_bands = (int16_t) Find_bit_alloc_IVAS_int( (Word32) core_brate, (Word16) GSC_IVAS_mode, (Word16) Diff_len, (Word16) nb_tot_bands, (Word16) L_frame, (Word16 *) bit, (Word16 *) max_ener_band, ener_vec, bits_per_bands );
227 19627 : nb_bands = nb_tot_bands;
228 : }
229 70898 : else if ( GSC_noisy_speech )
230 : {
231 32814 : SWB_bit_budget = *bit;
232 32814 : nb_bands = 5;
233 :
234 32814 : fzero_val = 0.0f;
235 32814 : if ( element_mode > EVS_MONO )
236 : {
237 32814 : fzero_val = MIN16B_FLT;
238 : }
239 :
240 32814 : if ( coder_type == UNVOICED && element_mode > EVS_MONO )
241 : {
242 108 : nb_bands = 3;
243 108 : if ( SWB_bit_budget > 20 )
244 : {
245 108 : nb_bands = 5;
246 : }
247 : }
248 32706 : else if ( bwidth < SWB )
249 : {
250 29962 : nb_bands = 7;
251 : }
252 :
253 32814 : st_band = nb_bands;
254 :
255 32814 : set_f( bits_per_bands, 0, MBANDS_GN );
256 :
257 32814 : bit_fracf = Find_bit_frac( nb_bands, (int16_t) SWB_bit_budget ); /* Supplementary bits distributed only on first bands */
258 :
259 32814 : nb_tot_bands = nb_bands_max - 6;
260 :
261 32814 : if ( nb_tot_bands > 16 )
262 : {
263 0 : nb_tot_bands = 16;
264 : }
265 :
266 98442 : for ( j = 0; j < 2; j++ )
267 : {
268 65628 : i = j;
269 65628 : max_ener_band[j] = i;
270 65628 : ener_vec[i] = fzero_val;
271 : }
272 :
273 32814 : if ( bwidth < SWB )
274 : {
275 30034 : if ( coder_type == UNVOICED && element_mode > EVS_MONO )
276 : {
277 72 : nb_tot_bands = 5;
278 : }
279 :
280 180060 : for ( ; j < nb_bands; j++ )
281 : {
282 150026 : i = maximum( ener_vec, nb_tot_bands, &etmp );
283 150026 : max_ener_band[j] = i;
284 150026 : ener_vec[i] = fzero_val;
285 : }
286 : }
287 : else
288 : {
289 11120 : for ( ; j < nb_bands; j++ )
290 : {
291 8340 : i = maximum( ener_vec, nb_tot_bands, &etmp );
292 8340 : max_ener_band[j] = i;
293 8340 : ener_vec[i] = fzero_val;
294 : }
295 : }
296 :
297 32814 : set_f( bits_per_bands, bit_fracf, nb_bands );
298 : }
299 : else
300 : {
301 38084 : bit_index++;
302 38084 : bit_tmp = *bit - GSC_freq_bits[bit_index];
303 38084 : bit_index++;
304 38084 : nb_bands_max += GSC_freq_bits[bit_index];
305 38084 : bit_index++;
306 :
307 38084 : *pvq_len = 112;
308 38084 : st_band = 7;
309 :
310 38084 : if ( L_frame == L_FRAME16k && core_brate > ACELP_16k40 )
311 : {
312 9569 : *pvq_len = 160;
313 9569 : st_band = 10;
314 9569 : nb_bands = *pvq_len / 16;
315 9569 : bit_tmp -= 35;
316 9569 : bit_new_bands = 5;
317 : }
318 :
319 38084 : if ( core_brate <= ACELP_9k60 )
320 : {
321 22486 : *pvq_len = 80;
322 22486 : st_band = 5;
323 22486 : if ( Diff_len == 0 )
324 : {
325 719 : nb_bands_max += 2;
326 719 : bit_tmp -= 13;
327 : }
328 : }
329 15598 : else if ( Diff_len == 0 )
330 : {
331 8195 : nb_bands_max += 2;
332 8195 : bit_tmp -= 17;
333 : }
334 :
335 38084 : nb_bands = *pvq_len / 16;
336 38084 : nb_bands_max = min( nb_bands_max, MBANDS_GN_BITALLOC16k );
337 :
338 : /*------------------------------------------------------------------------
339 : * Adjustement of the maximum number of bands in function of the
340 : * dynamics of the spectrum (more or less speech like)
341 : *-----------------------------------------------------------------------*/
342 :
343 38084 : if ( coder_type == INACTIVE || noise_lev >= NOISE_LEVEL_SP3 )
344 : {
345 : /* Probably classification error -> concentrate bits on LF */
346 24636 : if ( L_frame == L_FRAME16k && core_brate >= ACELP_24k40 )
347 : {
348 808 : nb_bands_max = nb_tot_bands - 2;
349 : }
350 23828 : else if ( core_brate >= ACELP_16k40 )
351 : {
352 9309 : nb_bands_max = nb_bands + 2;
353 : }
354 14519 : else if ( core_brate >= ACELP_8k00 )
355 : {
356 7451 : nb_bands_max = nb_bands + 1;
357 : }
358 : else
359 : {
360 7068 : nb_bands_max = nb_bands;
361 : }
362 : }
363 13448 : else if ( noise_lev >= NOISE_LEVEL_SP2 || ( core_brate <= ACELP_13k20 && core_brate >= ACELP_9k60 && cor_strong_limit == 0 ) ) /* Very low dynamic, tend to speech, do not try to code HF at all */
364 : {
365 13428 : nb_bands_max -= 2;
366 : }
367 20 : else if ( noise_lev >= NOISE_LEVEL_SP1 ) /* Very low dynamic, tend to speech, code less HF */
368 : {
369 4 : nb_bands_max -= 1;
370 : }
371 :
372 38084 : if ( L_frame == L_FRAME16k )
373 : {
374 10509 : if ( core_brate < ACELP_24k40 )
375 : {
376 9701 : nb_bands_max -= 4;
377 : }
378 808 : else if ( core_brate < ACELP_32k )
379 : {
380 808 : if ( Diff_len > 0 || noise_lev >= NOISE_LEVEL_SP2 )
381 : {
382 808 : nb_bands_max -= 2;
383 808 : bit_new_bands *= 2;
384 : }
385 : }
386 0 : else if ( core_brate >= ACELP_32k )
387 : {
388 0 : bit_new_bands *= 2;
389 : }
390 : }
391 :
392 38084 : if ( bwidth == NB && nb_bands_max > 10 )
393 : {
394 0 : nb_bands_max = 10;
395 : }
396 :
397 : /*------------------------------------------------------------------------
398 : * Find extra number of band to code according to bitrate availables
399 : *-----------------------------------------------------------------------*/
400 :
401 53187 : while ( bit_tmp >= bit_new_bands && nb_bands <= nb_bands_max - 1 )
402 : {
403 15103 : bit_tmp -= bit_new_bands;
404 15103 : nb_bands++;
405 : }
406 :
407 : /*------------------------------------------------------------------------
408 : * Fractional bits to distribute on the first x bands
409 : *-----------------------------------------------------------------------*/
410 :
411 38084 : if ( L_frame == L_FRAME16k && core_brate > ACELP_32k )
412 : {
413 0 : bit_fracf = 0;
414 : }
415 : else
416 : {
417 38084 : bit_fracf = Find_bit_frac( st_band, bit_tmp ); /* Supplementary bits distributed only on first bands */
418 : }
419 :
420 : /*------------------------------------------------------------------------
421 : * Complete the bit allocation per frequency band
422 : *-----------------------------------------------------------------------*/
423 :
424 38084 : imax = 5;
425 38084 : if ( core_brate > ACELP_9k60 )
426 : {
427 15598 : imax = 7;
428 : }
429 :
430 259700 : for ( i = 0; i < imax; i++ )
431 : {
432 221616 : bits_per_bands[i] = GSC_freq_bits[bit_index] + bit_fracf;
433 221616 : bit_index++;
434 : }
435 :
436 38084 : if ( L_frame == L_FRAME16k && core_brate > ACELP_16k40 )
437 : {
438 9569 : bit_index = 0;
439 9569 : i = imax - 1;
440 9569 : bits_per_bands[i] += Compl_GSC_freq_bits[bit_index];
441 9569 : i++;
442 9569 : bit_index++;
443 :
444 38276 : for ( ; i < 10; i++ )
445 : {
446 28707 : bits_per_bands[i] += Compl_GSC_freq_bits[bit_index] + bit_fracf;
447 28707 : bit_index++;
448 : }
449 : }
450 :
451 38084 : if ( Diff_len == 0 )
452 : {
453 8914 : bit_index = bit_index_mem + 10;
454 71312 : for ( i = 0; i < 7; i++ )
455 : {
456 62398 : bits_per_bands[i] += GSC_freq_bits[bit_index];
457 62398 : bit_index++;
458 : }
459 : }
460 :
461 38084 : if ( bit_fracf < 0 )
462 : {
463 102323 : for ( j = 0; j < nb_tot_bands; j++ )
464 : {
465 96304 : bits_per_bands[j] = max( bits_per_bands[j], 0 );
466 : }
467 : }
468 :
469 : /*--------------------------------------------------------------------------
470 : * Complete the bit allocation per frequency band for 16kHz high brate mode
471 : *--------------------------------------------------------------------------*/
472 :
473 38084 : if ( L_frame == L_FRAME16k && core_brate > ACELP_32k )
474 : {
475 0 : for ( j = st_band; j < nb_bands; j++ )
476 : {
477 0 : bits_per_bands[j] = bit_new_bands;
478 : }
479 :
480 0 : bit_fracf = ( 1.0f / nb_bands ) * ( bit_tmp );
481 :
482 0 : etmp = 2.0f * bit_fracf / ( nb_bands + 1 );
483 0 : bit_fracf = etmp;
484 0 : for ( j = nb_bands - 1; j >= 0; j-- )
485 : {
486 0 : bits_per_bands[j] += etmp;
487 0 : etmp += bit_fracf;
488 : }
489 : }
490 : else
491 : {
492 53187 : for ( j = st_band; j < nb_bands; j++ )
493 : {
494 15103 : bits_per_bands[j] = bit_new_bands;
495 : }
496 : }
497 :
498 : /*--------------------------------------------------------------------------
499 : * Compute a maximum band (band offset) for the search on maximal energy
500 : * This is function of the spectral dynamic and the bitrate
501 : *--------------------------------------------------------------------------*/
502 :
503 38084 : bandoffset = nb_tot_bands - ( nb_bands + 2 );
504 :
505 38084 : if ( noise_lev <= NOISE_LEVEL_SP1a )
506 : {
507 16 : bandoffset--;
508 : }
509 38068 : else if ( ( core_brate <= ACELP_13k20 && ( coder_type == INACTIVE || noise_lev >= NOISE_LEVEL_SP3 ) ) || ( core_brate <= ACELP_13k20 && core_brate >= ACELP_9k60 && cor_strong_limit == 0 ) )
510 : {
511 11644 : bandoffset++;
512 : }
513 :
514 38084 : if ( bandoffset < 0 )
515 : {
516 0 : bandoffset = 0;
517 : }
518 :
519 : /*--------------------------------------------------------------------------
520 : * Initiazed sorted vector
521 : * For the first x bands to be included in th final sorted vector
522 : * Sort the remaining bands in decrease energy order
523 : *--------------------------------------------------------------------------*/
524 :
525 689464 : for ( j = 0; j < nb_tot_bands; j++ )
526 : {
527 651380 : max_ener_band[j] = -10;
528 : }
529 :
530 288407 : for ( j = 0; j < st_band; j++ )
531 : {
532 250323 : max_ener_band[j] = j;
533 250323 : ener_vec[j] = -10;
534 : }
535 :
536 38084 : pos = st_band;
537 53187 : for ( ; j < nb_bands; j++ )
538 : {
539 15103 : i = maximum( ener_vec, nb_tot_bands - bandoffset, &etmp );
540 15103 : if ( i > pos )
541 : {
542 6769 : pos = i;
543 : }
544 15103 : max_ener_band[j] = i;
545 15103 : ener_vec[i] = -10;
546 : }
547 :
548 : /* re-allocate bits to the frames such that the highest band with allocated bits is higher than the threshold */
549 38084 : if ( nb_tot_bands - bandoffset > nb_bands && ( pos > 7 && core_brate == ACELP_8k00 ) && bwidth == WB )
550 : {
551 0 : band = nb_tot_bands - bandoffset - nb_bands;
552 0 : for ( j = 0; j < band; j++ )
553 : {
554 0 : i = maximum( ener_vec, nb_tot_bands - bandoffset, &etmp );
555 0 : max_ener_band[nb_bands + j] = i;
556 0 : ener_vec[i] = -10;
557 0 : bits_per_bands[nb_bands + j] = 5;
558 : }
559 0 : nb_bands += band;
560 :
561 0 : bit_tmp = 5 * band;
562 0 : if ( band <= 2 )
563 : {
564 0 : for ( j = st_band - 1; j < nb_bands; j++ )
565 : {
566 0 : bits_per_bands[j] += 1;
567 : }
568 0 : bit_tmp += nb_bands - st_band + 1;
569 : }
570 :
571 0 : i = 0;
572 0 : j = 0;
573 0 : while ( bit_tmp > 0 )
574 : {
575 0 : bits_per_bands[j] -= 1;
576 :
577 0 : if ( j == st_band - 1 - i )
578 : {
579 0 : j = 0;
580 : }
581 : else
582 : {
583 0 : ++j;
584 : }
585 :
586 0 : if ( j == 0 && i < st_band - 1 )
587 : {
588 0 : i++;
589 : }
590 :
591 0 : bit_tmp -= 1;
592 : }
593 : }
594 :
595 : /*--------------------------------------------------------------------------
596 : * Bit sum verification for GSC inactive at very high rate
597 : * The maximum number of bits per band of length 16 is 112
598 : * Redistribute the overage bits if needed
599 : *--------------------------------------------------------------------------*/
600 :
601 38084 : sum_bit = 0;
602 38084 : j = 0;
603 303510 : for ( i = 0; i < nb_bands; i++ )
604 : {
605 265426 : if ( bits_per_bands[i] > 112 )
606 : {
607 0 : sum_bit += bits_per_bands[i] - 112;
608 0 : bits_per_bands[i] = 112;
609 0 : j = i + 1;
610 : }
611 :
612 : /* safety check for overage bit reallocation */
613 265426 : else if ( bits_per_bands[i] + sum_bit / 3 > 112 )
614 : {
615 0 : j = i + 1;
616 : }
617 : }
618 :
619 38084 : if ( sum_bit != 0 )
620 : {
621 0 : sum_bit /= ( nb_bands - j );
622 0 : for ( i = j; i < nb_bands; i++ )
623 : {
624 0 : bits_per_bands[i] += sum_bit;
625 : }
626 : }
627 : }
628 :
629 : /*--------------------------------------------------------------------------
630 : * second step of bit sum verification, normally sum_bit == *bit
631 : *--------------------------------------------------------------------------*/
632 :
633 90525 : sum_bit = 0.00f;
634 740676 : for ( i = 0; i < nb_bands; i++ )
635 : {
636 650151 : bits_per_bands[i] = (float) floor( bits_per_bands[i] );
637 650151 : sum_bit += bits_per_bands[i];
638 : }
639 :
640 90525 : if ( GSC_IVAS_mode != 0 && sum_bit < *bit ) /* If we need to add bits, we are doing it on the LF */
641 : {
642 19590 : reajust_bits( bits_per_bands, 0, nb_bands, (int16_t) sum_bit, *bit );
643 : }
644 : else
645 : {
646 70935 : reajust_bits( bits_per_bands, nb_bands - 1, 0, (int16_t) sum_bit, *bit );
647 : }
648 :
649 : /*--------------------------------------------------------------------------
650 : * Recompute the real number/length of frequency bands to encode
651 : *--------------------------------------------------------------------------*/
652 :
653 90525 : *nb_subbands = nb_bands;
654 90525 : *pvq_len = *nb_subbands * 16;
655 :
656 : /*--------------------------------------------------------------------------
657 : * Concatenate bands (encoder only)
658 : *--------------------------------------------------------------------------*/
659 :
660 90525 : if ( exc_diff != NULL )
661 : {
662 186633 : for ( j = 0; j < nb_bands; j++ )
663 : {
664 163827 : mvr2r( exc_diff + max_ener_band[j] * 16, concat_in + j * 16, 16 );
665 : }
666 : }
667 :
668 90525 : set_s( bits_per_bands_s, 0, nb_tot_bands );
669 :
670 740676 : for ( j = 0; j < nb_bands; j++ )
671 : {
672 650151 : bits_per_bands_s[j] = ( (int16_t) bits_per_bands[j] ) << 3;
673 : }
674 : }
675 : else /* *bit == 0 */
676 : {
677 0 : set_s( bits_per_bands_s, 0, nb_tot_bands );
678 0 : *nb_subbands = 0;
679 0 : *pvq_len = 0;
680 : }
681 90525 : return;
682 : }
683 :
684 :
685 : /*-------------------------------------------------------------------*
686 : * reajust_bits()
687 : *
688 : *
689 : *-------------------------------------------------------------------*/
690 :
691 90525 : static void reajust_bits(
692 : float *bits_per_bands,
693 : const int16_t st_band,
694 : const int16_t end_band,
695 : const int16_t sum_bit_in,
696 : const int16_t bit_bdgt_in )
697 : {
698 : int16_t i, amount_to_add, incr;
699 : int16_t bit_bdgt, sum_bit;
700 :
701 90525 : incr = 1;
702 90525 : if ( end_band < st_band )
703 : {
704 70935 : incr = -1;
705 : }
706 :
707 90525 : if ( bit_bdgt_in < sum_bit_in )
708 : {
709 0 : amount_to_add = -1;
710 0 : bit_bdgt = sum_bit_in;
711 0 : sum_bit = bit_bdgt_in;
712 : }
713 : else
714 : {
715 90525 : bit_bdgt = bit_bdgt_in;
716 90525 : sum_bit = sum_bit_in;
717 90525 : amount_to_add = 1;
718 : }
719 :
720 90525 : i = st_band;
721 408605 : while ( bit_bdgt > sum_bit )
722 : {
723 318080 : if ( amount_to_add > 0 || ( amount_to_add < 0 && bits_per_bands[i] > 1 ) )
724 : {
725 318080 : bits_per_bands[i] += amount_to_add;
726 318080 : sum_bit += (int16_t) abs( amount_to_add );
727 : }
728 :
729 318080 : i += incr;
730 318080 : if ( i == end_band )
731 : {
732 8874 : i = st_band;
733 : }
734 : }
735 :
736 90525 : return;
737 : }
738 :
739 :
740 : /*-------------------------------------------------------------------*
741 : * Find_bit_frac()
742 : *
743 : * Computes the fraction of the remaining bit budget to allocate to the bands
744 : *-------------------------------------------------------------------*/
745 :
746 70898 : static float Find_bit_frac(
747 : const int16_t nb_band,
748 : const int16_t remaining_bits )
749 : {
750 : float var_out;
751 : int16_t inv_bandQ15;
752 : int32_t L_num;
753 :
754 70898 : inv_bandQ15 = 6553;
755 70898 : if ( nb_band == 7 )
756 : {
757 35991 : inv_bandQ15 = 4681;
758 : }
759 34907 : else if ( nb_band == 3 )
760 : {
761 0 : inv_bandQ15 = 10922;
762 : }
763 34907 : else if ( nb_band == 4 )
764 : {
765 0 : inv_bandQ15 = 8192;
766 : }
767 34907 : else if ( nb_band == 5 )
768 : {
769 25338 : inv_bandQ15 = 6536;
770 : }
771 9569 : else if ( nb_band == 10 )
772 : {
773 9569 : inv_bandQ15 = 3277;
774 : }
775 : else
776 : {
777 0 : inv_bandQ15 = (int16_t) ( ( 1.0f / nb_band ) * 32678 + 0.5f );
778 : #ifdef DEBUGGING
779 : printf( "1/%d NOT DEFINED in Find_bit_frac\n", nb_band );
780 : #endif
781 : }
782 :
783 70898 : L_num = inv_bandQ15 * remaining_bits;
784 70898 : L_num *= 8;
785 70898 : var_out = L_num / 262144.0f;
786 :
787 70898 : return ( var_out );
788 : }
789 :
790 :
791 : /*-------------------------------------------------------------------*
792 : * Find_bit_alloc_IVAS_int()
793 : *
794 : *
795 : *-------------------------------------------------------------------*/
796 :
797 : /*! r: Number of bands to encode */
798 19627 : static Word16 Find_bit_alloc_IVAS_int(
799 : const Word32 core_brate, /* i : core bit rate */
800 : const Word16 GSC_IVAS_mode, /* i : GSC IVAS mode */
801 : const Word16 Diff_len, /* i : Length of the difference signal (before pure spectral)*/
802 : const Word16 nb_tot_bands_in, /* i : total number of band */
803 : const Word16 L_frame, /* i : frame length */
804 : Word16 *bit, /* i/o: Number of bit allowed for frequency quantization */
805 : Word16 *max_ener_band, /* i/o: Energy based sorted order */
806 : float *ener_vec_io, /* i/o: Energy per band order */
807 : float *bits_per_bands_o /* o : Number of bit allowed per allowed sub-band Q3 */
808 : )
809 : {
810 : Word32 mp, mb, nb_bands_adj, bit_adj;
811 : Word16 nb_pulse_per_band[MBANDS_GN_BITALLOC16k];
812 : Word32 SWB_bit_budget; /* Q0 -> Q18 */
813 : Word16 i, j, nb_bands_max, st_band, nb_tot_bands_loc, etmp;
814 : Word32 sum_bit /*Q18*/, bit_fracf /*Q18*/;
815 : Word16 d_tmp, e_div, tmp16, ener_vec[MBANDS_GN_BITALLOC16k];
816 : Word32 Ltmp, etmp_32fx, bits_per_bands[MBANDS_GN_BITALLOC16k];
817 :
818 19627 : SWB_bit_budget = *bit; /* Q0 */
819 19627 : st_band = 5;
820 19627 : nb_bands_max = nb_tot_bands_in;
821 :
822 333659 : for ( i = 0; i < MBANDS_GN; i++ )
823 : {
824 314032 : ener_vec[i] = (Word16) ( ener_vec_io[i] ); /* Q12 -> Q12 */
825 : }
826 :
827 19627 : if ( L_frame == L_FRAME16k )
828 : {
829 0 : for ( i = MBANDS_GN; i < MBANDS_GN_BITALLOC16k; i++ )
830 : {
831 0 : ener_vec[i] = (Word16) ( ener_vec_io[i] * 4096.0 + 0.5f ); /* Q0 -> Q12 */
832 : }
833 : }
834 :
835 19627 : set_l( bits_per_bands, 0, MBANDS_GN_BITALLOC16k );
836 :
837 : /* Decide the pourcentage of bits allocated to LF (between 50-75%) depending of the temporal contribution in GSC */
838 : /* bit_fracf = ( -0.125f * Diff_len + 76.0f ) / 100; */
839 19627 : bit_fracf = L_add( Mpy_32_32( -Q31_0_00125, L_shl( Diff_len, 18 ) ), Q18_0_76 ); /* Q18 */
840 :
841 : /* bit_fracf = check_bounds(bit_fracf, 0.50f, 0.75f); */
842 19627 : bit_fracf = min( max( bit_fracf, Q18_0_50 ), Q18_0_75 );
843 :
844 : /* Adjusment of the bitrate between LF and HF base on the content type */
845 : /* 1 = new GSC bit alloc
846 : 2 = GSC bit alloc for tc frame
847 : 3 = more music like (should not happen often given music is coded with dft) */
848 :
849 19627 : if ( GSC_IVAS_mode <= 3 )
850 : {
851 19627 : nb_bands_max -= 6;
852 : }
853 :
854 19627 : if ( GSC_IVAS_mode == 2 )
855 : {
856 : /* bit_fracf += 0.1f; */
857 1004 : bit_fracf += Q18_0_1; /* Q18*/
858 1004 : nb_bands_max -= 1;
859 : }
860 :
861 19627 : if ( GSC_IVAS_mode == 3 )
862 : {
863 : /* bit_fracf -= 0.1f; */
864 11432 : bit_fracf -= Q18_0_1; /* Q18*/
865 11432 : nb_bands_max += 3;
866 : }
867 :
868 : /* 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*/
869 : /* Adjust the number of band based on the content type and bitrate */
870 :
871 : /* nb_bands_adj = 1.0f; */
872 19627 : nb_bands_adj = Q18_1_0;
873 19627 : if ( GSC_IVAS_mode == 1 && core_brate < GSC_L_RATE_STG )
874 : {
875 : /* nb_bands_adj = 0.0125f * SWB_bit_budget - 0.75f;*/
876 7051 : nb_bands_adj = L_sub( Mpy_32_32( Q31_0_0125, L_shl( SWB_bit_budget, 18 ) ), Q18_0_75 ); /* Q18 */
877 : }
878 12576 : else if ( GSC_IVAS_mode != 2 && core_brate > GSC_H_RATE_STG )
879 : {
880 : /*nb_bands_adj = 0.02f * SWB_bit_budget - 1.2f;*/
881 24 : nb_bands_adj = L_sub( Mpy_32_32( Q31_0_02, L_shl( SWB_bit_budget, 18 ) ), Q18_1_2 ); /* Q18 */
882 : }
883 : /*nb_bands_max = (int16_t)(nb_bands_max * nb_bands_adj + 0.5f);*/
884 :
885 19627 : nb_bands_max = round_fx( Mpy_32_16_1( L_shl( nb_bands_adj, 5 ), shl( nb_bands_max, 10 - 2 ) ) ); /* Q0 */
886 19627 : nb_bands_max = check_bounds_s( nb_bands_max, 5, nb_tot_bands_in );
887 :
888 : /* bit_fracf *= SWB_bit_budget;*/
889 : /* At this point bit_fracf has a value below 1.0 */
890 19627 : 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 */
891 :
892 : /* Estimation of the number of bit used in HF */
893 : /* with only the first weighting The number of bits in max_ener_band[st_band-1] = 17% of bit_fracf */
894 : /* mb = .17f * bit_fracf;*/
895 19627 : mb = Mpy_32_32( Q31_0_17, bit_fracf ); /* Q18 */
896 :
897 : /* mp = 2 * DSR_NB_PULSE;*/
898 19627 : mp = Q18_2_0xDSR_NB_PULSE;
899 19627 : if ( core_brate < GSC_L_RATE_STG && GSC_IVAS_mode == 3 )
900 : {
901 : /* mp = 1.5f * DSR_NB_PULSE;*/
902 11207 : mp = Q18_1_5xDSR_NB_PULSE;
903 : }
904 8420 : else if ( core_brate < GSC_L_RATE_STG )
905 : {
906 : /* mp = DSR_NB_PULSE;*/
907 8031 : mp = Q18_DSR_NB_PULSE;
908 : }
909 :
910 : /* 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]*/
911 : /* We will estimate the number of bits to allocate of HF and put the remaining bits, if any, back on LF */
912 : /* compute the total possible number of band to be coded */
913 :
914 : /* nb_tot_bands = (int16_t)((SWB_bit_budget - bit_fracf) / (mp + (mb - mp) / 2.0f)); */
915 19627 : d_tmp = Find_norm_inv( L_add( mp, mb ), &e_div );
916 19627 : Ltmp = Mpy_32_16_1( L_sub( L_shl( SWB_bit_budget, 18 ), bit_fracf ), d_tmp ); /* Perform mult by 1/den */
917 19627 : 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*/
918 :
919 19627 : mp = min( mp, mb );
920 19627 : tmp16 = sub( add( nb_tot_bands_loc, st_band ), nb_bands_max );
921 19627 : if ( tmp16 > 0 )
922 : {
923 : /* bit_adj = ( ( mb + mp ) / 2 ) * ( nb_tot_bands_loc + st_band - nb_bands_max ); */
924 7388 : 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) */
925 7388 : bit_adj = L_max( 0, bit_adj );
926 7388 : nb_tot_bands_loc = nb_bands_max - st_band;
927 7388 : bit_fracf += bit_adj; /* Q18 */
928 : }
929 19627 : nb_tot_bands_loc += st_band;
930 :
931 : /* Allocate bits to LF */
932 : /* etmp = 0.23f; */
933 19627 : etmp_32fx = Q31_0_23;
934 117762 : for ( j = 0; j < st_band; j++ )
935 : {
936 98135 : i = j;
937 98135 : max_ener_band[j] = i;
938 98135 : ener_vec[i] = MIN16B;
939 : /* bits_per_bands[j] = etmp * bit_fracf; */
940 98135 : bits_per_bands[j] = Mpy_32_32( bit_fracf, etmp_32fx ); /* 18 + 31 + 1 - 32 = Q18 */
941 : /* etmp -= 0.015f; */
942 98135 : etmp_32fx -= Q31_0_015; /* Q18 */
943 : }
944 :
945 : /* SWB_bit_budget -= bit_fracf; */
946 19627 : SWB_bit_budget = L_sub( L_shl( SWB_bit_budget, 18 ), bit_fracf ); /* Q0->Q18 */
947 :
948 : /* Find low energy band in HF */
949 19627 : set_s( nb_pulse_per_band, 2, MBANDS_GN_BITALLOC16k );
950 43846 : for ( i = st_band + 2; i < nb_tot_bands_loc - 1; i++ )
951 : {
952 24219 : if ( ener_vec[i] < ener_vec[i - 1] && ener_vec[i] < ener_vec[i + 1] ) /* i +1 and i -1 can be considered as 2 ptrs */
953 : {
954 7738 : nb_pulse_per_band[i] = 1;
955 : }
956 : }
957 82223 : for ( j = st_band; j < nb_tot_bands_loc; j++ )
958 : {
959 62596 : if ( j > 6 )
960 : {
961 36578 : i = maximum_fx( ener_vec, nb_tot_bands_loc, &etmp );
962 : }
963 : else
964 : {
965 26018 : i = j;
966 : }
967 62596 : max_ener_band[j] = i;
968 62596 : ener_vec[i] = MIN16B;
969 : }
970 :
971 : /* Recompute the final bit distribution for HF */
972 19627 : if ( nb_tot_bands_loc > st_band )
973 : {
974 : /* mb = ( SWB_bit_budget * 2 / ( nb_tot_bands_loc - st_band ) ) - mp; */
975 13129 : d_tmp = Find_norm_inv( L_deposit_h( sub( nb_tot_bands_loc, st_band ) ), &e_div );
976 13129 : mb = L_sub( L_shr( Mpy_32_16_1( L_shl( SWB_bit_budget, 1 ), d_tmp ), e_div ), mp ); /* Q18 */
977 : /* bit_fracf = ( mb - mp ) / ( nb_tot_bands_loc - st_band ); */
978 13129 : bit_fracf = L_shr( Mpy_32_16_1( L_sub( mb, mp ), d_tmp ), e_div ); /* Q18 */
979 :
980 13129 : mb -= bit_fracf;
981 : /* Do the distribution */
982 75725 : for ( j = st_band; j < nb_tot_bands_loc; j++ )
983 : {
984 62596 : bits_per_bands[max_ener_band[j]] = Q18_DSR_NB_PULSE;
985 62596 : if ( nb_pulse_per_band[max_ener_band[j]] > 1 )
986 : {
987 54858 : bits_per_bands[max_ener_band[j]] = mb;
988 : }
989 62596 : mb -= bit_fracf;
990 62596 : SWB_bit_budget -= bits_per_bands[max_ener_band[j]]; /* Q18 */
991 : }
992 : }
993 :
994 : /* Series of verification in case bit allocated != the budget */
995 19627 : if ( SWB_bit_budget > 0 )
996 : {
997 19614 : i = st_band - 1;
998 92408 : while ( SWB_bit_budget > 0 )
999 : {
1000 : /* bits_per_bands[i]++; */
1001 72794 : bits_per_bands[i] += Q18_1_0;
1002 : /* SWB_bit_budget--; */
1003 72794 : SWB_bit_budget -= Q18_1_0;
1004 72794 : i--;
1005 72794 : if ( i == -1 )
1006 : {
1007 5855 : i = st_band - 1;
1008 : }
1009 : }
1010 : }
1011 :
1012 : /*nb_bands = nb_tot_bands_loc;*/
1013 :
1014 19627 : sum_bit = 0;
1015 19627 : j = 0;
1016 180358 : for ( i = 0; i < nb_tot_bands_loc; i++ )
1017 : {
1018 : /* if (bits_per_bands[i] > 112) */
1019 160731 : if ( bits_per_bands[i] > Q18_112 )
1020 : {
1021 : /* sum_bit += bits_per_bands[i] - 112; */
1022 0 : sum_bit = L_add( sum_bit, L_sub( bits_per_bands[i], Q18_112 ) );
1023 : /* bits_per_bands[i] = 112; */
1024 0 : bits_per_bands[i] = Q18_112;
1025 0 : j = add( j, add( i, 1 ) );
1026 : }
1027 : /* safety check for overage bit reallocation */
1028 : /* else if (bits_per_bands[i] + sum_bit / 3 > 112) */
1029 160731 : else if ( L_add( bits_per_bands[i], Mpy_32_16_1( sum_bit, Q15_0_33 ) ) > Q18_112 )
1030 : {
1031 0 : j = add( j, add( i, 1 ) );
1032 : }
1033 : }
1034 :
1035 19627 : if ( sum_bit != 0 )
1036 : {
1037 : /* sum_bit /= (nb_bands - j); */
1038 0 : d_tmp = Find_norm_inv( L_deposit_h( sub( nb_tot_bands_loc, j ) ), &e_div );
1039 0 : sum_bit = L_shr( Mpy_32_16_1( sum_bit, d_tmp ), e_div ); /* Q18 */
1040 0 : for ( i = j; i < nb_tot_bands_loc; i++ )
1041 : {
1042 0 : bits_per_bands[i] = L_add( bits_per_bands[i], sum_bit );
1043 : }
1044 : }
1045 :
1046 412167 : for ( i = 0; i < MBANDS_GN_BITALLOC16k; i++ )
1047 : {
1048 392540 : bits_per_bands_o[i] = (float) bits_per_bands[i] / 262144.0f; /* Q18 -> float */
1049 : }
1050 :
1051 19627 : return nb_tot_bands_loc;
1052 : }
1053 :
1054 :
1055 : /*-------------------------------------------------------------------*
1056 : * Find_norm_inv()
1057 : *
1058 : * Find normalized 1 / ToDivide
1059 : *-------------------------------------------------------------------*/
1060 :
1061 32756 : static Word16 Find_norm_inv(
1062 : const Word32 ToDivide,
1063 : Word16 *e_div )
1064 : {
1065 : Word16 d_tmp, e_tmp;
1066 :
1067 32756 : e_tmp = norm_l( ToDivide );
1068 32756 : d_tmp = round_fx( L_shl( ToDivide, e_tmp ) );
1069 32756 : d_tmp = div_s( 16384, d_tmp ); /* 1.0 in Q14, dividend is normalize so >= 16384 as required for the division */
1070 32756 : *e_div = sub( 14, e_tmp );
1071 32756 : move16();
1072 :
1073 32756 : return d_tmp;
1074 : }
1075 :
1076 :
1077 : /*-------------------------------------------------------------------*
1078 : * maximum_fx()
1079 : *
1080 : *
1081 : *-------------------------------------------------------------------*/
1082 :
1083 : /*! r: index of the maximum value in the input vector */
1084 36578 : static Word16 maximum_fx(
1085 : const Word16 *vec_fx, /* i : input vector */
1086 : const Word16 lvec_fx, /* i : length of input vector */
1087 : Word16 *max_fx /* o : maximum value in the input vector */
1088 : )
1089 : {
1090 : Word16 j, ind;
1091 : Word16 tmp;
1092 :
1093 36578 : ind = 0;
1094 36578 : tmp = vec_fx[0];
1095 :
1096 381918 : for ( j = 1; j < lvec_fx; j++ )
1097 : {
1098 345340 : if ( vec_fx[j] > tmp )
1099 : {
1100 54287 : ind = j;
1101 : }
1102 345340 : tmp = s_max( tmp, vec_fx[j] );
1103 : }
1104 36578 : *max_fx = tmp;
1105 :
1106 36578 : return ind;
1107 : }
|