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 "rom_com.h"
43 : #include "prot.h"
44 : #include "ivas_cnst.h"
45 : #include "ivas_rom_com.h"
46 : #include "wmc_auto.h"
47 :
48 : /*-------------------------------------------------------------------*
49 : * Local function prototypes
50 : *--------------------------------------------------------------------*/
51 :
52 : static int16_t BITS_ALLOC_adjust_acelp_fixed_cdk( const int16_t bits_frame, int16_t *fixed_cdk_index, const int16_t nb_subfr );
53 :
54 : static int16_t allocate_unused( const int32_t core_brate, const int16_t coder_type, const int16_t unused_bits, const int16_t nb_prm, const int16_t subfr, const int16_t prm_type, int16_t *prm_bit_mode );
55 :
56 : typedef enum
57 : {
58 : GAINSPRM = 0x0,
59 : PITCHPRM = 0x1,
60 : MID_LSFSPRM = 0x3,
61 : LSFPRM = 0x4
62 : } PRM_TYPES;
63 :
64 : /*-------------------------------------------------------------------*
65 : * BITS_ALLOC_init_config_acelp()
66 : *
67 : * initial configuration for Mode 2 ACELP
68 : *--------------------------------------------------------------------*/
69 :
70 1303490 : void BITS_ALLOC_init_config_acelp(
71 : const int32_t bit_rate,
72 : const int16_t narrowBand,
73 : const int16_t nb_subfr,
74 : ACELP_config *acelp_cfg /* o : configuration structure of ACELP */
75 : )
76 : {
77 : int16_t rate_mode_index;
78 :
79 1303490 : if ( bit_rate <= ACELP_9k60 )
80 : {
81 24897 : rate_mode_index = 0;
82 : }
83 : else
84 : {
85 1278593 : rate_mode_index = 1;
86 : }
87 :
88 1303490 : acelp_cfg->mode_index = rate_mode_index;
89 :
90 : /*LPC: midLpc should be swithced off?*/
91 1303490 : acelp_cfg->midLpc_enable = 1;
92 :
93 : /*ACELP ICB config*/
94 1303490 : if ( ( rate_mode_index == 0 ) || ( narrowBand == 1 ) )
95 : {
96 24989 : acelp_cfg->pre_emphasis = 1;
97 24989 : acelp_cfg->formant_enh = 1;
98 24989 : acelp_cfg->formant_enh_num = FORMANT_SHARPENING_G1;
99 24989 : acelp_cfg->formant_enh_den = FORMANT_SHARPENING_G2;
100 24989 : acelp_cfg->formant_tilt = 0;
101 24989 : acelp_cfg->voice_tilt = 0;
102 : }
103 : else
104 : {
105 1278501 : acelp_cfg->pre_emphasis = 0;
106 1278501 : acelp_cfg->formant_enh = 1;
107 1278501 : acelp_cfg->formant_enh_num = FORMANT_SHARPENING_G1;
108 1278501 : acelp_cfg->formant_enh_den = FORMANT_SHARPENING_G2;
109 1278501 : acelp_cfg->formant_tilt = 1;
110 1278501 : acelp_cfg->voice_tilt = 1;
111 : }
112 :
113 : /*Wide band @ 16kHz*/
114 1303490 : if ( nb_subfr == NB_SUBFR16k )
115 : {
116 630262 : acelp_cfg->pre_emphasis = 1;
117 630262 : acelp_cfg->formant_enh = 1;
118 630262 : acelp_cfg->formant_enh_num = FORMANT_SHARPENING_G1_16k;
119 630262 : acelp_cfg->formant_enh_den = FORMANT_SHARPENING_G2_16k;
120 630262 : acelp_cfg->formant_tilt = 0;
121 630262 : acelp_cfg->voice_tilt = 2;
122 : }
123 :
124 1303490 : return;
125 : }
126 :
127 : /*-------------------------------------------------------------------*
128 : * BITS_ALLOC_config_acelp()
129 : *
130 : * configure all Mode 2 ACELP coder types and allocate the bits
131 : *--------------------------------------------------------------------*/
132 :
133 35661 : int16_t BITS_ALLOC_config_acelp(
134 : const int16_t bits_frame, /* i : remaining bit budget for the frame */
135 : const int16_t coder_type, /* i : acelp coder type */
136 : ACELP_config *acelp_cfg, /* i/o: configuration structure of ACELP */
137 : const int16_t narrowBand, /* i : narrowband flag */
138 : const int16_t nb_subfr /* i : number of subframes */
139 : )
140 : {
141 : int16_t mode_index;
142 : int16_t band_index;
143 : int16_t i;
144 : int16_t remaining_bits, bits;
145 :
146 : /*Sanity check*/
147 :
148 35661 : mode_index = acelp_cfg->mode_index;
149 35661 : band_index = ( narrowBand == 0 );
150 35661 : bits = 0;
151 :
152 35661 : if ( band_index == 0 )
153 : {
154 2503 : if ( coder_type == INACTIVE )
155 : {
156 0 : acelp_cfg->formant_enh = 0;
157 : }
158 : else
159 : {
160 2503 : acelp_cfg->formant_enh = 1;
161 : }
162 : }
163 :
164 35661 : if ( band_index == 1 && nb_subfr == NB_SUBFR )
165 : {
166 :
167 9865 : if ( coder_type == INACTIVE )
168 : {
169 222 : acelp_cfg->pre_emphasis = 0;
170 222 : acelp_cfg->formant_enh = 0;
171 222 : acelp_cfg->formant_enh_num = FORMANT_SHARPENING_G1_16k;
172 222 : acelp_cfg->formant_tilt = 1;
173 222 : acelp_cfg->voice_tilt = 1;
174 : }
175 : else
176 : {
177 9643 : acelp_cfg->pre_emphasis = 1;
178 9643 : acelp_cfg->formant_enh = 1;
179 9643 : acelp_cfg->formant_enh_num = FORMANT_SHARPENING_G1;
180 9643 : acelp_cfg->formant_tilt = 0;
181 9643 : acelp_cfg->voice_tilt = 0;
182 : }
183 : }
184 :
185 35661 : if ( coder_type == UNVOICED )
186 : {
187 1899 : if ( ACELP_GAINS_MODE[mode_index][band_index][coder_type] == 6 )
188 : {
189 112 : acelp_cfg->pitch_sharpening = 0;
190 112 : acelp_cfg->phase_scrambling = 1;
191 : }
192 : else
193 : {
194 1787 : acelp_cfg->pitch_sharpening = 0;
195 1787 : acelp_cfg->phase_scrambling = 0;
196 : }
197 : }
198 : else
199 : {
200 33762 : acelp_cfg->pitch_sharpening = 1;
201 33762 : acelp_cfg->phase_scrambling = 0;
202 : }
203 :
204 35661 : if ( coder_type > ACELP_MODE_MAX )
205 : {
206 : /* keep pitch sharpening for RF_ALLPRED mode */
207 568 : acelp_cfg->pitch_sharpening = 0;
208 568 : acelp_cfg->phase_scrambling = 0;
209 : }
210 :
211 : /*Allocate bits and different modes*/
212 35661 : acelp_cfg->bpf_mode = ACELP_BPF_MODE[mode_index][band_index][coder_type];
213 35661 : bits += ACELP_BPF_BITS[acelp_cfg->bpf_mode];
214 :
215 35661 : acelp_cfg->nrg_mode = ACELP_NRG_MODE[mode_index][band_index][coder_type];
216 35661 : acelp_cfg->nrg_bits = ACELP_NRG_BITS[acelp_cfg->nrg_mode];
217 35661 : bits += acelp_cfg->nrg_bits;
218 :
219 35661 : acelp_cfg->ltp_mode = ACELP_LTP_MODE[mode_index][band_index][coder_type];
220 35661 : acelp_cfg->ltp_bits = 0;
221 35661 : acelp_cfg->ltf_mode = ACELP_LTF_MODE[mode_index][band_index][coder_type];
222 35661 : acelp_cfg->ltf_bits = ACELP_LTF_BITS[acelp_cfg->ltf_mode];
223 :
224 35661 : if ( nb_subfr == NB_SUBFR16k && acelp_cfg->ltf_bits == 4 )
225 : {
226 10714 : acelp_cfg->ltf_bits++;
227 : }
228 35661 : bits += acelp_cfg->ltf_bits;
229 :
230 :
231 201598 : for ( i = 0; i < nb_subfr; i++ )
232 : {
233 165937 : acelp_cfg->gains_mode[i] = ACELP_GAINS_MODE[mode_index][band_index][coder_type];
234 :
235 : /* skip subframe 1, 3 gain encoding, and use from subframe 0, and 3, respectively */
236 165937 : if ( coder_type >= ACELP_MODE_MAX && ( i == 1 || i == 3 ) )
237 : {
238 3146 : acelp_cfg->gains_mode[i] = 0;
239 : }
240 :
241 165937 : bits += ACELP_GAINS_BITS[acelp_cfg->gains_mode[i]];
242 165937 : bits += ACELP_LTP_BITS_SFR[acelp_cfg->ltp_mode][i];
243 165937 : acelp_cfg->ltp_bits += ACELP_LTP_BITS_SFR[acelp_cfg->ltp_mode][i];
244 : }
245 :
246 : /*Innovation*/
247 35661 : if ( bits_frame < bits )
248 : {
249 0 : printf( "\nWarning: bits per frame too low\n" );
250 0 : return -1;
251 : }
252 :
253 35661 : if ( coder_type == RF_ALLPRED )
254 : {
255 1005 : set_s( acelp_cfg->fixed_cdk_index, -1, nb_subfr );
256 : }
257 34656 : else if ( coder_type == RF_GENPRED )
258 : {
259 522 : acelp_cfg->fixed_cdk_index[0] = 0; /* 7 bits */
260 522 : acelp_cfg->fixed_cdk_index[1] = -1;
261 522 : acelp_cfg->fixed_cdk_index[2] = 0; /* 7 bits */
262 522 : acelp_cfg->fixed_cdk_index[3] = -1;
263 522 : acelp_cfg->fixed_cdk_index[4] = -1;
264 522 : bits += 14;
265 : }
266 34134 : else if ( coder_type == RF_NOPRED )
267 : {
268 6 : set_s( acelp_cfg->fixed_cdk_index, 0, nb_subfr );
269 6 : bits += 28;
270 : }
271 : else
272 : {
273 34128 : bits += BITS_ALLOC_adjust_acelp_fixed_cdk( bits_frame - bits, acelp_cfg->fixed_cdk_index, nb_subfr );
274 : }
275 :
276 35661 : remaining_bits = bits_frame - bits;
277 :
278 : /*Sanity check*/
279 35661 : if ( remaining_bits < 0 )
280 : {
281 0 : bits = -1;
282 : }
283 :
284 :
285 35661 : return ( bits );
286 : }
287 :
288 : /*-------------------------------------------------------------------*
289 : * BITS_ALLOC_adjust_acelp_fixed_cdk()
290 : *
291 : *
292 : *--------------------------------------------------------------------*/
293 :
294 34128 : static int16_t BITS_ALLOC_adjust_acelp_fixed_cdk(
295 : const int16_t bits_frame, /* i : bit budget */
296 : int16_t *fixed_cdk_index,
297 : const int16_t nb_subfr )
298 : {
299 : int16_t bits_subframe2;
300 : int16_t sfr, k, bitsused, bits_currsubframe;
301 :
302 34128 : bits_subframe2 = bits_frame;
303 :
304 34128 : if ( bits_subframe2 < ACELP_FIXED_CDK_BITS( 0 ) * nb_subfr )
305 : {
306 0 : return ( bits_frame + 1 ); /* Not enough bits for lowest mode. -> trigger alarm*/
307 : }
308 :
309 : /* search cdk-index for first subframe */
310 638838 : for ( k = 0; k < ACELP_FIXED_CDK_NB - 1; k++ )
311 : {
312 638680 : if ( ACELP_FIXED_CDK_BITS( k ) * nb_subfr > bits_subframe2 )
313 : {
314 33970 : k--; /* previous mode did not exceed bit-budget */
315 33970 : break;
316 : }
317 : }
318 :
319 34128 : if ( ACELP_FIXED_CDK_BITS( k ) * nb_subfr > bits_subframe2 )
320 : {
321 158 : k--; /* previous mode did not exceed bit-budget */
322 : }
323 34128 : fixed_cdk_index[0] = k;
324 :
325 34128 : bitsused = ACELP_FIXED_CDK_BITS( k );
326 :
327 159805 : for ( sfr = 1; sfr < nb_subfr; sfr++ )
328 : {
329 125677 : bits_currsubframe = ( sfr * bits_subframe2 + bits_subframe2 ) - bitsused * nb_subfr;
330 :
331 : /* try increasing mode while below threshold */
332 161844 : while ( ( k < ACELP_FIXED_CDK_NB - 1 ) && ( ACELP_FIXED_CDK_BITS( k + 1 ) * nb_subfr <= bits_currsubframe ) )
333 : {
334 36167 : k++;
335 : }
336 :
337 : /* try decreasing mode until below threshold */
338 142321 : while ( ACELP_FIXED_CDK_BITS( k ) * nb_subfr > bits_currsubframe )
339 : {
340 16644 : k--;
341 16644 : if ( k == 0 )
342 : {
343 0 : break;
344 : }
345 : }
346 :
347 : /* store mode */
348 125677 : fixed_cdk_index[sfr] = k;
349 125677 : bitsused += ACELP_FIXED_CDK_BITS( k );
350 : }
351 :
352 34128 : return bitsused;
353 : }
354 :
355 :
356 : /*-------------------------------------------------------------------*
357 : * fcb_table()
358 : *
359 : * Selection of fixed innovation codebook bitbudget table
360 : *--------------------------------------------------------------------*/
361 :
362 73455616 : static int16_t fcb_table(
363 : const int16_t n,
364 : const int16_t L_subfr )
365 : {
366 : int16_t out;
367 :
368 73455616 : out = PulseConfTable[n].bits;
369 73455616 : if ( L_subfr > L_SUBFR )
370 : {
371 4874 : out = fast_FCB_bits_2sfr[n];
372 : }
373 :
374 73455616 : return ( out );
375 : }
376 :
377 : /*-------------------------------------------------------------------*
378 : * acelp_FCB_allocator()
379 : *
380 : * Routine to allocate fixed innovation codebook bit-budget
381 : *--------------------------------------------------------------------*/
382 :
383 3574677 : static ivas_error acelp_FCB_allocator(
384 : int16_t *nBits, /* i/o: available bit-budget */
385 : int16_t fixed_cdk_index[], /* o : codebook index */
386 : int16_t nb_subfr, /* i : number of subframes */
387 : const int16_t L_subfr, /* i : subframe length */
388 : const int16_t coder_type, /* i : coder type */
389 : const int16_t tc_subfr, /* i : TC subframe index */
390 : const int16_t fix_first /* i : flag to indicate whether the first subframe bit-budget was fixed */
391 : )
392 : {
393 : int16_t cdbk, sfr, step;
394 : int16_t nBits_tmp;
395 : int16_t *p_fixed_cdk_index;
396 : ivas_error error;
397 : int16_t max_n;
398 :
399 3574677 : error = IVAS_ERR_OK;
400 :
401 3574677 : cdbk = coder_type; /* just to avoid warning when DEBUGGING is deactivated */
402 :
403 3574677 : p_fixed_cdk_index = fixed_cdk_index;
404 :
405 : /* TRANSITION coding: first subframe bit-budget was already fixed, glottal pulse not in the first subframe */
406 3574677 : if ( tc_subfr >= L_SUBFR && fix_first )
407 : {
408 : int16_t i;
409 :
410 81330 : for ( i = 0; i < nb_subfr; i++ )
411 : {
412 65064 : *nBits -= ACELP_FIXED_CDK_BITS( fixed_cdk_index[i] );
413 : }
414 16266 : return error;
415 : }
416 :
417 : /* TRANSITION coding: first subframe bit-budget was already fixed, glottal pulse in the first subframe */
418 3558411 : sfr = 0;
419 3558411 : if ( fix_first )
420 : {
421 108214 : *nBits -= ACELP_FIXED_CDK_BITS( fixed_cdk_index[0] );
422 108214 : sfr = 1;
423 108214 : p_fixed_cdk_index++;
424 108214 : nb_subfr = 3;
425 : }
426 :
427 : /* distribute the bit-budget equally between subframes */
428 3558411 : if ( L_subfr > L_SUBFR ) /* access fast_FCB_bits_2sfr */
429 : {
430 542 : max_n = 6;
431 : }
432 : else
433 : {
434 3557869 : max_n = ACELP_FIXED_CDK_NB;
435 : }
436 59221988 : for ( cdbk = 0; cdbk < max_n; cdbk++ )
437 : {
438 59221986 : if ( fcb_table( cdbk, L_subfr ) * nb_subfr > *nBits )
439 : {
440 3558409 : break;
441 : }
442 : }
443 3558411 : cdbk--;
444 : #ifdef DEBUGGING
445 : if ( cdbk < 0 && coder_type != TRANSITION )
446 : {
447 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Too low bit-budget for fixed innovation codebook (frame = %d). Exiting! \n", frame );
448 : }
449 : if ( ( L_subfr == L_SUBFR && cdbk >= ACELP_FIXED_CDK_NB ) || ( L_subfr == 2 * L_SUBFR && fcb_table( cdbk, L_subfr ) == 128 /*stop value*/ ) )
450 : {
451 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Too high bit-budget for fixed innovation codebook (frame = %d). Exiting! \n", frame );
452 : }
453 : #endif
454 :
455 3558411 : set_s( p_fixed_cdk_index, cdbk, nb_subfr );
456 3558411 : nBits_tmp = 0;
457 3558411 : if ( cdbk >= 0 )
458 : {
459 3558407 : nBits_tmp = fcb_table( cdbk, L_subfr );
460 : }
461 : else
462 : {
463 4 : nBits_tmp = 0;
464 : }
465 3558411 : *nBits -= nBits_tmp * nb_subfr;
466 :
467 : /* try to increase the FCB bit-budget of the first subframe(s) */
468 3558411 : if ( cdbk < ACELP_FIXED_CDK_NB - 1 )
469 : {
470 3558409 : step = fcb_table( cdbk + 1, L_subfr ) - nBits_tmp;
471 9752238 : while ( *nBits >= step )
472 : {
473 6193829 : ( *p_fixed_cdk_index )++;
474 6193829 : *nBits -= step;
475 6193829 : p_fixed_cdk_index++;
476 : }
477 :
478 : /* try to increase the FCB of the first subframe in cases when the next step is lower than the current step */
479 3558409 : if ( fixed_cdk_index[sfr] < ACELP_FIXED_CDK_NB - 1 )
480 : {
481 3558407 : step = fcb_table( fixed_cdk_index[sfr] + 1, L_subfr ) - fcb_table( fixed_cdk_index[sfr], L_subfr );
482 3558407 : if ( *nBits >= step && cdbk >= 0 )
483 : {
484 524619 : fixed_cdk_index[sfr]++;
485 524619 : *nBits -= step;
486 :
487 524619 : if ( *nBits >= step && fixed_cdk_index[sfr + 1] == fixed_cdk_index[sfr] - 1 )
488 : {
489 33184 : sfr++;
490 33184 : fixed_cdk_index[sfr]++;
491 33184 : *nBits -= step;
492 : }
493 : }
494 : }
495 : }
496 : /* TRANSITION coding: allocate highest FCBQ bit-budget to the subframe with the glottal-shape codebook */
497 3558411 : if ( tc_subfr >= L_SUBFR )
498 : {
499 : int16_t tempr;
500 :
501 203841 : SWAP( fixed_cdk_index[0], fixed_cdk_index[tc_subfr / L_SUBFR] );
502 :
503 : /* TRANSITION coding: allocate second highest FCBQ bit-budget to the last subframe */
504 203841 : if ( tc_subfr / L_SUBFR < nb_subfr - 1 )
505 : {
506 101389 : SWAP( fixed_cdk_index[( tc_subfr - L_SUBFR ) / L_SUBFR], fixed_cdk_index[nb_subfr - 1] );
507 : }
508 : }
509 :
510 : /* when subframe length > L_SUBFR, number of bits instead of codebook index is signalled */
511 3558411 : if ( L_subfr > L_SUBFR )
512 : {
513 : int16_t i, j;
514 1626 : for ( i = 0; i < nb_subfr; i++ )
515 : {
516 1084 : j = fixed_cdk_index[i];
517 1084 : fixed_cdk_index[i] = fast_FCB_bits_2sfr[j];
518 : }
519 : }
520 :
521 3558411 : return error;
522 : }
523 :
524 :
525 : /*-------------------------------------------------------------------*
526 : * config_acelp1()
527 : *
528 : * Configure ACELP bit allocation
529 : * - should be in range of <6700; 24350> for ACELP@12.8kHz
530 : * - per channel bitrate minimum is 13250 kbps for ACELP@16kHz
531 : *--------------------------------------------------------------------*/
532 :
533 5226704 : ivas_error config_acelp1(
534 : const int16_t enc_dec, /* i : encoder/decoder flag */
535 : const int32_t total_brate, /* i : total bitrate */
536 : const int32_t core_brate_inp, /* i : core bitrate */
537 : const int16_t core, /* i : core */
538 : const int16_t extl, /* i : extension layer */
539 : const int32_t extl_brate, /* i : extension layer bitrate */
540 : const int16_t L_frame, /* i : frame length at internal Fs */
541 : const int16_t GSC_noisy_speech, /* i : GSC on SWB noisy speech flag */
542 : ACELP_config *acelp_cfg, /* i : ACELP bit-allocation */
543 : const int16_t signaling_bits, /* i : number of signaling bits */
544 : const int16_t coder_type, /* i : coder type */
545 : const int16_t inactive_coder_type_flag, /* i : AVQ (0) or GSC (1) IC flag */
546 : const int16_t tc_subfr, /* i : TC subfr ID */
547 : const int16_t tc_call, /* i : TC call number (0,1,2,3,5(DEC)) */
548 : int16_t *nBits_es_Pred, /* o : number of bits for Es_pred Q */
549 : int16_t *unbits, /* o : number of unused bits */
550 : const int16_t element_mode, /* i : element mode */
551 : int16_t *uc_two_stage_flag, /* o : flag undicating two-stage UC */
552 : const int16_t tdm_lp_reuse_flag, /* i : LPC reuse flag (can be 1 only with secondary channel */
553 : const int16_t tdm_low_rate_mode, /* i : secondary channel low rate mode flag */
554 : const int16_t idchan, /* i : stereo channel ID */
555 : const int16_t tdm_Pitch_reuse_flag, /* i : primary channel pitch reuse flag*/
556 : const int16_t tdm_LRTD_flag, /* i : LRTD stereo mode flag */
557 : const int16_t GSC_IVAS_mode /* i : GSC IVAS mode */
558 : )
559 : {
560 : int16_t i, bits, nb_subfr;
561 : int16_t flag_hardcoded, coder_type_sw, fix_first;
562 : int32_t core_brate;
563 : #ifdef DEBUGGING
564 : int32_t core_brate_inpI = core_brate_inp;
565 : #endif
566 : ivas_error error;
567 :
568 5226704 : error = IVAS_ERR_OK;
569 :
570 : /*-----------------------------------------------------------------*
571 : * Set the flag indicating two-stage Unvoiced (UC) frame
572 : *-----------------------------------------------------------------*/
573 :
574 5226704 : *uc_two_stage_flag = 0;
575 5226704 : if ( coder_type == UNVOICED )
576 : {
577 52307 : if ( total_brate >= MIN_UNVOICED_TWO_STAGE_BRATE && element_mode > EVS_MONO && ( idchan == 0 || ( ( total_brate >= 8500 || extl_brate == 0 ) && tdm_LRTD_flag == 1 ) ) )
578 : {
579 51637 : *uc_two_stage_flag = 1;
580 : }
581 : }
582 :
583 : /*-----------------------------------------------------------------*
584 : * Set the number of subframes
585 : *-----------------------------------------------------------------*/
586 :
587 5226704 : if ( L_frame == L_FRAME )
588 : {
589 3031337 : nb_subfr = NB_SUBFR;
590 :
591 : #ifdef DEBUGGING
592 : if ( ( ( core_brate_inp < 5900 && coder_type > UNVOICED ) && !( core_brate_inp < MIN_TC_BRATE && coder_type == TRANSITION ) ) && !( idchan > 0 && element_mode == IVAS_CPE_TD ) && !( element_mode == IVAS_SCE && tdm_low_rate_mode ) )
593 : {
594 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Too low bitrate (%d bps) for ACELP@12k8 in frame %d. Exiting!\n", core_brate_inpI, frame );
595 : }
596 :
597 : if ( core_brate_inp > ACELP_12k8_HIGH_LIMIT && core == ACELP_CORE )
598 : {
599 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Too high bitrate (%d bps) for ACELP@12k8 in frame %d. Exiting!\n", core_brate_inpI, frame );
600 : }
601 : #endif
602 : }
603 : else /* L_frame == L_FRAME16k */
604 : {
605 2195367 : nb_subfr = NB_SUBFR16k;
606 :
607 : #ifdef DEBUGGING
608 : if ( core_brate_inp < ACELP_16k_LOW_LIMIT && core == ACELP_CORE )
609 : {
610 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Too low bitrate (%d bps) for ACELP@16k in frame %d. Exiting!\n", core_brate_inpI, frame );
611 : }
612 : #endif
613 : }
614 :
615 5226704 : coder_type_sw = coder_type;
616 5226704 : if ( core != ACELP_CORE )
617 : {
618 : /* used in acelp_core_switch_enc() */
619 714 : nb_subfr = 1;
620 714 : if ( L_frame == L_FRAME )
621 : {
622 147 : coder_type_sw = TRANSITION;
623 : }
624 : }
625 :
626 : /*-----------------------------------------------------------------*
627 : * Check if the core_brate is hard coded (to keep BE for mono core) or not
628 : *-----------------------------------------------------------------*/
629 :
630 5226704 : flag_hardcoded = 0;
631 5226704 : i = 0;
632 :
633 46899859 : while ( i < SIZE_BRATE_INTERMED_TBL )
634 : {
635 46899859 : if ( core_brate_inp == brate_intermed_tbl[i] )
636 : {
637 241196 : flag_hardcoded = 1;
638 241196 : break;
639 : }
640 :
641 46658663 : if ( core_brate_inp < brate_intermed_tbl[i] )
642 : {
643 4985508 : flag_hardcoded = 0;
644 4985508 : break;
645 : }
646 :
647 41673155 : i++;
648 : }
649 :
650 5226704 : if ( element_mode == IVAS_CPE_TD && coder_type == AUDIO &&
651 105 : core_brate_inp <= 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 */
652 : {
653 14 : i--;
654 : }
655 :
656 5226704 : core_brate = brate_intermed_tbl[i];
657 :
658 5226704 : if ( element_mode > EVS_MONO )
659 : {
660 5132858 : flag_hardcoded = 0; /* use automatic and flexible ACELP bit-budget allocation */
661 : }
662 :
663 5226704 : if ( core != ACELP_CORE && element_mode == EVS_MONO ) /* needed for mode1 core switching in EVS mono */
664 : {
665 714 : flag_hardcoded = 1;
666 : }
667 :
668 : /*-----------------------------------------------------------------*
669 : * ACELP bit allocation
670 : *-----------------------------------------------------------------*/
671 :
672 5226704 : if ( !( coder_type == TRANSITION && tc_subfr != -1 ) || enc_dec == DEC )
673 : {
674 : /* Set the bit-budget */
675 4998385 : bits = (int16_t) ( core_brate_inp / FRAMES_PER_SEC );
676 :
677 4998385 : if ( coder_type == TRANSITION && enc_dec == DEC && tc_call == 1 )
678 : {
679 289393 : bits += *nBits_es_Pred; /* equalize for 4th signaling bit estimated at the encoder in TC_0_192 */
680 : }
681 :
682 : /* Subtract signaling bits */
683 4998385 : if ( enc_dec == DEC && idchan == 1 && element_mode > EVS_MONO )
684 : {
685 37938 : bits -= TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS;
686 :
687 37938 : if ( tdm_LRTD_flag == 1 )
688 : {
689 35006 : bits += STEREO_BITS_TCA;
690 : }
691 :
692 : /* subtract TBE/BWE flag */
693 37938 : if ( extl_brate > 0 && ( extl == WB_TBE || extl == SWB_TBE || extl == FB_TBE || extl == WB_BWE || extl == SWB_BWE || extl == FB_BWE ) )
694 : {
695 26123 : bits--;
696 : }
697 : }
698 : else
699 : {
700 : /* Subtract signaling bits */
701 4960447 : bits -= signaling_bits;
702 : }
703 :
704 4998385 : if ( extl_brate > 0 && ( extl == WB_TBE || extl == SWB_TBE || extl == FB_TBE || extl == WB_BWE || extl == SWB_BWE || extl == FB_BWE ) )
705 : {
706 : /* extension layer signaling bit is counted in the extension layer bitbudget */
707 3904376 : bits++;
708 : }
709 :
710 : /*-----------------------------------------------------------------*
711 : * LSF Q bit-budget
712 : *-----------------------------------------------------------------*/
713 :
714 4998385 : if ( !tdm_lp_reuse_flag || idchan == 0 )
715 : {
716 : /* LSF Q bit-budget */
717 4992343 : acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
718 :
719 4992343 : if ( !flag_hardcoded )
720 : {
721 4903543 : if ( L_frame == L_FRAME )
722 : {
723 2821109 : if ( element_mode == IVAS_SCE && tdm_low_rate_mode )
724 : {
725 546522 : acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
726 : }
727 2274587 : else if ( ( total_brate < 7200 || coder_type == INACTIVE || coder_type == AUDIO ) && idchan == 1 )
728 : {
729 : /* TD stereo, secondary channel: do nothing */
730 12799 : acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
731 : }
732 2261788 : else if ( element_mode > EVS_MONO && coder_type == AUDIO && brate_intermed_tbl[i] < ACELP_9k60 )
733 : {
734 : /* primary channel: do nothing */
735 : }
736 2132860 : else if ( element_mode > EVS_MONO && coder_type == AUDIO /*&& brate_intermed_tbl[i] >= ACELP_9k60*/ )
737 : {
738 267117 : acelp_cfg->lsf_bits = 42;
739 : }
740 1865743 : else if ( total_brate <= 9600 || coder_type == UNVOICED )
741 : {
742 260511 : acelp_cfg->lsf_bits = 31;
743 : }
744 1605232 : else if ( total_brate <= 20000 )
745 : {
746 1510916 : acelp_cfg->lsf_bits = 36;
747 : }
748 : else
749 : {
750 94316 : acelp_cfg->lsf_bits = 41;
751 : }
752 : }
753 : else /* L_frame == L_FRAME16k */
754 : {
755 2082434 : acelp_cfg->lsf_bits = 41;
756 : }
757 : }
758 :
759 4992343 : bits -= acelp_cfg->lsf_bits;
760 :
761 : /* mid-LSF Q bit-budget */
762 4992343 : acelp_cfg->mid_lsf_bits = mid_LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
763 :
764 4992343 : if ( element_mode > EVS_MONO && coder_type == AUDIO /*&& brate_intermed_tbl[i] < ACELP_9k60*/ )
765 : {
766 396045 : acelp_cfg->mid_lsf_bits = 5;
767 : /* primary channel: do nothing */
768 : }
769 :
770 4992343 : bits -= acelp_cfg->mid_lsf_bits;
771 : }
772 6042 : else if ( tdm_lp_reuse_flag == 1 && idchan == 1 )
773 : {
774 6042 : bits -= TDM_IC_LSF_PRED_BITS;
775 : }
776 :
777 : /* gain Q bit-budget - part 1: 'Es_pred' of memory-less gain Q */
778 4998385 : if ( ( coder_type != UNVOICED && coder_type != AUDIO && coder_type != INACTIVE && !( core_brate <= ACELP_8k00 && coder_type != TRANSITION ) ) /* mid bitrates in GC and VC, low+mid bitrates in TC */ ||
779 723033 : ( coder_type == INACTIVE && !inactive_coder_type_flag ) /* AVQ inactive */
780 : )
781 : {
782 3785044 : *nBits_es_Pred = Es_pred_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, -1, -1 )];
783 3785044 : bits -= *nBits_es_Pred;
784 : }
785 1213341 : else if ( *uc_two_stage_flag )
786 : {
787 51637 : *nBits_es_Pred = 4;
788 51637 : bits -= *nBits_es_Pred;
789 : }
790 : }
791 : else
792 : {
793 228319 : bits = *unbits;
794 : }
795 :
796 5226704 : if ( coder_type == TRANSITION && tc_call == 0 )
797 : {
798 154681 : *unbits = bits;
799 154681 : return error;
800 : }
801 :
802 : /*-----------------------------------------------------------------*
803 : * Low-rate mode - bits are allocated in tdm_low_rate_enc()
804 : *-----------------------------------------------------------------*/
805 :
806 5072023 : if ( element_mode == IVAS_SCE && tdm_low_rate_mode )
807 : {
808 546522 : acelp_cfg->FEC_mode = 0;
809 546522 : acelp_cfg->ltf_mode = FULL_BAND;
810 546522 : *nBits_es_Pred = 0;
811 546522 : *unbits = 0;
812 546522 : acelp_cfg->ubits = 0;
813 :
814 546522 : return error;
815 : }
816 :
817 : /*-----------------------------------------------------------------*
818 : * Supplementary information for FEC
819 : *-----------------------------------------------------------------*/
820 :
821 4525501 : acelp_cfg->FEC_mode = 0;
822 4525501 : if ( core_brate >= ACELP_11k60 && ( idchan == 0 || element_mode == EVS_MONO ) )
823 : {
824 3645670 : acelp_cfg->FEC_mode = 1;
825 :
826 3645670 : if ( coder_type > UNVOICED && coder_type < AUDIO && coder_type != VOICED )
827 : {
828 3194150 : bits -= FEC_BITS_CLS;
829 : }
830 :
831 3645670 : if ( coder_type != TRANSITION )
832 : {
833 3137892 : if ( total_brate >= ACELP_16k40 )
834 : {
835 2006316 : acelp_cfg->FEC_mode = 2;
836 :
837 2006316 : if ( coder_type > UNVOICED && coder_type < AUDIO )
838 : {
839 1890136 : bits -= FEC_BITS_ENR;
840 : }
841 : }
842 :
843 3137892 : if ( total_brate >= ACELP_32k )
844 : {
845 276444 : acelp_cfg->FEC_mode = 3;
846 :
847 276444 : if ( coder_type > UNVOICED && coder_type < AUDIO )
848 : {
849 258962 : bits -= FEC_BITS_POS;
850 : }
851 : }
852 : }
853 : }
854 :
855 : /*-----------------------------------------------------------------*
856 : * LP filtering of the adaptive excitation
857 : *-----------------------------------------------------------------*/
858 :
859 4525501 : if ( idchan > 0 && element_mode > EVS_MONO )
860 : {
861 66570 : acelp_cfg->ltf_mode = FULL_BAND;
862 : }
863 4458931 : else if ( coder_type == UNVOICED )
864 : {
865 49722 : acelp_cfg->ltf_mode = FULL_BAND;
866 : }
867 4409209 : else if ( ( coder_type == GENERIC || coder_type == TRANSITION ) && core_brate < ACELP_11k60 )
868 : {
869 434559 : acelp_cfg->ltf_mode = LOW_PASS;
870 : }
871 3974650 : else if ( core_brate >= ACELP_11k60 && ( coder_type != AUDIO && !( coder_type == INACTIVE && L_frame == L_FRAME ) ) )
872 : {
873 3412642 : if ( coder_type == INACTIVE && L_frame == L_FRAME16k && inactive_coder_type_flag ) /* GSC Inactive @16kHz */
874 : {
875 71344 : acelp_cfg->ltf_mode = FULL_BAND;
876 : }
877 : else
878 : {
879 3341298 : acelp_cfg->ltf_mode = NORMAL_OPERATION;
880 3341298 : if ( coder_type != TRANSITION )
881 : {
882 2833520 : bits -= nb_subfr;
883 : }
884 : }
885 : }
886 : else
887 : {
888 562008 : acelp_cfg->ltf_mode = FULL_BAND;
889 : }
890 :
891 : /*-----------------------------------------------------------------*
892 : * UC bit-budget
893 : *-----------------------------------------------------------------*/
894 :
895 4525501 : if ( ( ( coder_type == UNVOICED && !( *uc_two_stage_flag ) ) || ( coder_type == INACTIVE && core_brate <= ACELP_9k60 ) ) && ( idchan == 0 || element_mode == EVS_MONO ) )
896 : {
897 13231 : bits -= NBITS_NOISENESS; /* noiseness */
898 : }
899 4525501 : if ( coder_type == UNVOICED && !( *uc_two_stage_flag ) )
900 : {
901 670 : bits -= ( 3 * NB_SUBFR ); /* tilt factor */
902 : }
903 :
904 : /*-----------------------------------------------------------------*
905 : * TC bit-budget
906 : *-----------------------------------------------------------------*/
907 :
908 4525501 : fix_first = 0;
909 4525501 : if ( coder_type == TRANSITION )
910 : {
911 591367 : if ( tc_call == 2 )
912 : {
913 127294 : fix_first = 1;
914 : }
915 :
916 : /* TC signaling */
917 591367 : if ( L_frame == L_FRAME )
918 : {
919 361138 : if ( tc_subfr == TC_0_0 )
920 : {
921 70454 : if ( enc_dec == ENC )
922 : {
923 39998 : bits -= 1; /* TC signaling */
924 : }
925 :
926 70454 : if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
927 : {
928 55375 : bits -= 3; /* LP filtering flag */
929 : }
930 : }
931 290684 : else if ( tc_subfr == TC_0_64 )
932 : {
933 41352 : if ( enc_dec == ENC )
934 : {
935 9294 : bits -= 4; /* TC signaling */
936 : }
937 :
938 41352 : if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
939 : {
940 29750 : bits -= 3; /* LP filtering flag */
941 : }
942 : }
943 249332 : else if ( tc_subfr == TC_0_128 )
944 : {
945 14343 : if ( enc_dec == ENC )
946 : {
947 5711 : bits -= 4; /* TC signaling */
948 : }
949 :
950 14343 : if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
951 : {
952 10878 : bits -= 2; /* LP filtering flag */
953 : }
954 : }
955 234989 : else if ( tc_subfr == TC_0_192 )
956 : {
957 4662 : if ( enc_dec == ENC )
958 : {
959 2153 : bits -= 3; /* TC signaling */
960 : }
961 :
962 4662 : if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
963 : {
964 3547 : bits -= 1; /* LP filtering flag */
965 : }
966 : }
967 230327 : else if ( tc_subfr == L_SUBFR )
968 : {
969 39003 : if ( enc_dec == ENC )
970 : {
971 21534 : bits -= 3; /* TC signaling */
972 : }
973 :
974 39003 : if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
975 : {
976 28684 : bits -= ( L_FRAME - tc_subfr - L_SUBFR ) / L_SUBFR; /* LP filtering flag */
977 : }
978 : }
979 : else
980 : {
981 191324 : if ( enc_dec == ENC )
982 : {
983 70161 : bits -= 4; /* TC signaling */
984 : }
985 :
986 191324 : if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
987 : {
988 149315 : bits -= ( L_FRAME - tc_subfr - L_SUBFR ) / L_SUBFR; /* LP filtering flag */
989 : }
990 : }
991 : }
992 : else /* L_frame == L_FRAME16k */
993 : {
994 230229 : if ( enc_dec == ENC )
995 : {
996 79468 : if ( tc_subfr <= 2 * L_SUBFR )
997 : {
998 53847 : bits -= 2; /* TC signaling */
999 : }
1000 : else
1001 : {
1002 25621 : bits -= 3; /* TC signaling */
1003 : }
1004 : }
1005 :
1006 230229 : bits -= ( L_FRAME16k - tc_subfr - L_SUBFR ) / L_SUBFR; /* LP filtering flag */
1007 : }
1008 :
1009 : /* glottal-shape codebook bits */
1010 591367 : bits -= ( 3 + 6 + 1 + 3 );
1011 : }
1012 :
1013 : /*-----------------------------------------------------------------*
1014 : * pitch, innovation, gains bit-budget
1015 : *-----------------------------------------------------------------*/
1016 :
1017 4525501 : acelp_cfg->fcb_mode = 0;
1018 :
1019 4525501 : if ( element_mode == IVAS_CPE_TD && tdm_low_rate_mode == 1 && coder_type != INACTIVE && coder_type != UNVOICED ) /* GENERIC low rate mode for secondary channel */
1020 : {
1021 542 : set_s( acelp_cfg->pitch_bits, 0, NB_SUBFR16k );
1022 542 : set_s( acelp_cfg->gains_mode, 0, NB_SUBFR16k );
1023 :
1024 1626 : for ( i = 0; i < 2; i++ )
1025 : {
1026 1084 : acelp_cfg->pitch_bits[i] = 0;
1027 1084 : if ( tdm_Pitch_reuse_flag == 0 )
1028 : {
1029 1084 : acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, GENERIC, 2 * i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
1030 1084 : bits -= acelp_cfg->pitch_bits[i];
1031 : }
1032 1084 : acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, GENERIC, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
1033 1084 : bits -= acelp_cfg->gains_mode[i];
1034 : }
1035 542 : acelp_cfg->fcb_mode = 1;
1036 :
1037 : #ifdef DEBUGGING
1038 : if ( bits >= 55 )
1039 : {
1040 : printf( "too much bits -> %d, LPC = %d and pitch = %d\n", bits, tdm_lp_reuse_flag, tdm_Pitch_reuse_flag );
1041 : acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, 2, 2 * L_SUBFR, GENERIC, -1, 0 );
1042 : }
1043 : else
1044 : #endif
1045 542 : if ( bits >= 16 )
1046 : {
1047 542 : acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, 2, 2 * L_SUBFR, GENERIC, -1, 0 );
1048 : }
1049 : else
1050 : {
1051 0 : acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, 2, 2 * L_SUBFR, GENERIC, -1, 0 );
1052 0 : acelp_cfg->fixed_cdk_index[1] = -1;
1053 : }
1054 542 : acelp_cfg->fixed_cdk_index[2] = -1;
1055 542 : acelp_cfg->fixed_cdk_index[3] = -1;
1056 : }
1057 4524959 : else if ( ( coder_type != INACTIVE && nb_subfr == NB_SUBFR && coder_type != AUDIO ) || /* @12.8kHz core except of GSC */
1058 2115332 : ( nb_subfr == NB_SUBFR16k && ( !inactive_coder_type_flag || coder_type != INACTIVE ) ) /* @16kHz core GC, TC, AVQ inactive */
1059 550880 : || core == HQ_CORE /* ACELP -> HQ switching in EVS */ )
1060 : {
1061 : /* pitch Q & gain Q bit-budget - part 2*/
1062 21915811 : for ( i = 0; i < nb_subfr; i++ )
1063 : {
1064 17941018 : if ( L_frame == L_FRAME )
1065 : {
1066 7720511 : if ( tdm_Pitch_reuse_flag == 1 && idchan == 1 )
1067 : {
1068 1080 : acelp_cfg->pitch_bits[i] = 0;
1069 : }
1070 : else
1071 : {
1072 7719431 : acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
1073 : }
1074 7720511 : acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type_sw, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
1075 : }
1076 : else /* L_frame == L_FRAME16k */
1077 : {
1078 10220507 : if ( tdm_Pitch_reuse_flag == 1 && idchan == 1 )
1079 : {
1080 0 : acelp_cfg->pitch_bits[i] = 0;
1081 : }
1082 : else
1083 : {
1084 10220507 : acelp_cfg->pitch_bits[i] = ACB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( core_brate, coder_type, i * L_SUBFR, TC_SUBFR2IDX_16KHZ( tc_subfr ) )];
1085 : }
1086 10220507 : acelp_cfg->gains_mode[i] = gain_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( core_brate, coder_type_sw, i * L_SUBFR, TC_SUBFR2IDX_16KHZ( tc_subfr ) )];
1087 : }
1088 :
1089 17941018 : bits -= acelp_cfg->pitch_bits[i];
1090 :
1091 17941018 : if ( coder_type == INACTIVE && acelp_cfg->gains_mode[i] == 6 /* VQ vs. SQ threshold @32 kbps */ )
1092 : {
1093 75335 : bits -= 5;
1094 : }
1095 : else
1096 : {
1097 17865683 : if ( *uc_two_stage_flag == 1 )
1098 : {
1099 206548 : acelp_cfg->gains_mode[i] = 7;
1100 : }
1101 :
1102 17865683 : bits -= acelp_cfg->gains_mode[i];
1103 : }
1104 : }
1105 :
1106 : /* algebraic codebook bit-budget */
1107 3974793 : if ( flag_hardcoded || /* EVS */
1108 3895340 : ( core_brate_inp >= MIN_BRATE_AVQ_EXC && coder_type != INACTIVE ) /* high-birate ACELP except IC */ ||
1109 158838 : ( !inactive_coder_type_flag && coder_type == INACTIVE ) /* AVQ inactive */ )
1110 : {
1111 2353919 : for ( i = 0; i < nb_subfr; i++ )
1112 : {
1113 1953483 : if ( L_frame == L_FRAME )
1114 : {
1115 183511 : acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
1116 : }
1117 : else /* L_frame == L_FRAME16k */
1118 : {
1119 1769972 : acelp_cfg->fixed_cdk_index[i] = FCB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( core_brate, coder_type, i * L_SUBFR, TC_SUBFR2IDX_16KHZ( tc_subfr ) )];
1120 : }
1121 1953483 : bits -= acelp_cfg->fixed_cdk_index[i];
1122 : }
1123 : }
1124 3574357 : else if ( !( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) )
1125 : {
1126 3574149 : if ( coder_type == UNVOICED && !( *uc_two_stage_flag ) )
1127 : {
1128 14 : i = bits / NB_SUBFR;
1129 14 : if ( i % 2 == 0 )
1130 : {
1131 14 : i--; /* must be odd */
1132 : }
1133 14 : i = min( i, 13 );
1134 : #ifdef DEBUG_MODE_TD
1135 : if ( i < 0 )
1136 : IVAS_ERROR( IVAS_ERR_INTERNAL, "ERROR::: UC negative index should not happen at frame %d\n", frame );
1137 : #endif
1138 14 : i = max( i, 0 ); /* If i == 0-> random noise generator will be used as FCB */
1139 14 : set_s( acelp_cfg->fixed_cdk_index, i, NB_SUBFR );
1140 14 : bits -= ( i * NB_SUBFR );
1141 : }
1142 : else
1143 : {
1144 :
1145 3574135 : acelp_cfg->fcb_mode = 1;
1146 3574135 : if ( element_mode == IVAS_CPE_TD )
1147 : {
1148 116254 : if ( bits >= ACELP_FIXED_CDK_BITS( 0 ) * ( nb_subfr ) ) /* enough bits for all fcb */
1149 : {
1150 116089 : acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
1151 : }
1152 165 : else if ( bits >= ACELP_FIXED_CDK_BITS( 0 ) * ( nb_subfr - 1 ) )
1153 : {
1154 0 : acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr - 1, L_SUBFR, coder_type, tc_subfr, fix_first );
1155 0 : acelp_cfg->fixed_cdk_index[3] = -1;
1156 : }
1157 165 : else if ( bits >= ACELP_FIXED_CDK_BITS( 0 ) * ( nb_subfr - 2 ) )
1158 : {
1159 165 : acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr - 2, L_SUBFR, coder_type, tc_subfr, fix_first );
1160 165 : acelp_cfg->fixed_cdk_index[2] = acelp_cfg->fixed_cdk_index[1];
1161 165 : acelp_cfg->fixed_cdk_index[1] = -1;
1162 165 : acelp_cfg->fixed_cdk_index[3] = -1;
1163 : }
1164 0 : else if ( bits >= ACELP_FIXED_CDK_BITS( 0 ) )
1165 : {
1166 0 : acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, 1, L_SUBFR, coder_type, tc_subfr, fix_first );
1167 0 : acelp_cfg->fixed_cdk_index[1] = acelp_cfg->fixed_cdk_index[0];
1168 0 : acelp_cfg->fixed_cdk_index[0] = -1;
1169 0 : acelp_cfg->fixed_cdk_index[2] = -1;
1170 0 : acelp_cfg->fixed_cdk_index[3] = -1;
1171 : }
1172 : else /* No FCB */
1173 : {
1174 : #ifdef DEBUGGING
1175 : IVAS_ERROR( IVAS_ERR_INTERNAL, "WARNING!!!, No bit allocated to FCB, check frame %d\n", frame );
1176 : #endif
1177 0 : acelp_cfg->fixed_cdk_index[0] = -1;
1178 0 : acelp_cfg->fixed_cdk_index[1] = -1;
1179 0 : acelp_cfg->fixed_cdk_index[2] = -1;
1180 0 : acelp_cfg->fixed_cdk_index[3] = -1;
1181 : }
1182 : }
1183 3457881 : else if ( element_mode != IVAS_CPE_TD && GSC_IVAS_mode > 0 && L_frame == L_FRAME16k )
1184 : {
1185 0 : bits = 100; /* 9 kbps for fcb */
1186 0 : acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
1187 : }
1188 : else
1189 : {
1190 3457881 : acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
1191 : }
1192 : }
1193 : }
1194 :
1195 : /* AVQ codebook */
1196 3974793 : if ( ( core_brate_inp >= MIN_BRATE_AVQ_EXC && coder_type != INACTIVE ) /* high-birate ACELP except IC */ ||
1197 162032 : ( !inactive_coder_type_flag && coder_type == INACTIVE ) /* AVQ inactive */ )
1198 : {
1199 2123286 : for ( i = 0; i < nb_subfr; i++ )
1200 : {
1201 1769405 : if ( flag_hardcoded )
1202 : {
1203 164490 : acelp_cfg->AVQ_cdk_bits[i] = AVQ_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( core_brate, coder_type, i * L_SUBFR, TC_SUBFR2IDX_16KHZ( tc_subfr ) )];
1204 : {
1205 164490 : bits -= acelp_cfg->AVQ_cdk_bits[i];
1206 : }
1207 : }
1208 :
1209 1769405 : bits -= G_AVQ_BITS;
1210 : }
1211 :
1212 353881 : if ( core_brate_inp >= MIN_BRATE_AVQ_EXC && core_brate_inp <= MAX_BRATE_AVQ_EXC_TD && coder_type == GENERIC )
1213 : {
1214 : /* harm. flag ACELP AVQ */
1215 159508 : bits--;
1216 : }
1217 :
1218 353881 : if ( !flag_hardcoded )
1219 : {
1220 : int16_t bit_tmp;
1221 :
1222 320983 : bit_tmp = bits / nb_subfr;
1223 320983 : set_s( acelp_cfg->AVQ_cdk_bits, bit_tmp, nb_subfr );
1224 320983 : bits -= bit_tmp * nb_subfr;
1225 :
1226 320983 : bit_tmp = bits % nb_subfr;
1227 320983 : acelp_cfg->AVQ_cdk_bits[0] += bit_tmp;
1228 320983 : bits -= bit_tmp;
1229 : }
1230 : }
1231 : }
1232 550166 : else if ( ( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) /* LBR secondary channel in TD stereo */ ||
1233 550166 : ( ( coder_type == INACTIVE || coder_type == AUDIO ) && nb_subfr == NB_SUBFR ) /* GSC @12.8kHz */ ||
1234 71344 : ( coder_type == INACTIVE && inactive_coder_type_flag ) /* AVQ inactive */ )
1235 : {
1236 : int32_t Local_BR, Pitch_BR;
1237 : int16_t Pitch_CT;
1238 :
1239 : /* as defined at the beginning of [enc,dec]_pit_exc() */
1240 550166 : if ( GSC_IVAS_mode > 0 && ( GSC_noisy_speech || core_brate > GSC_H_RATE_STG ) )
1241 : {
1242 16554 : Local_BR = ACELP_8k00;
1243 16554 : Pitch_CT = GENERIC;
1244 16554 : Pitch_BR = ACELP_8k00;
1245 16554 : if ( L_frame == L_FRAME16k )
1246 : {
1247 0 : Local_BR = ACELP_14k80;
1248 0 : if ( GSC_IVAS_mode > 0 && core_brate < IVAS_24k4 )
1249 : {
1250 0 : Local_BR = ACELP_9k60;
1251 : }
1252 0 : Pitch_BR = core_brate;
1253 : }
1254 : }
1255 533612 : else if ( GSC_noisy_speech )
1256 : {
1257 5698 : Local_BR = ACELP_7k20;
1258 5698 : Pitch_CT = GENERIC;
1259 5698 : Pitch_BR = ACELP_7k20;
1260 5698 : if ( L_frame == L_FRAME16k )
1261 : {
1262 0 : Pitch_BR = core_brate;
1263 : }
1264 : }
1265 : else
1266 : {
1267 527914 : Local_BR = ACELP_7k20;
1268 527914 : Pitch_CT = AUDIO;
1269 527914 : Pitch_BR = core_brate;
1270 :
1271 527914 : if ( L_frame == L_FRAME16k )
1272 : {
1273 71344 : Local_BR = ACELP_13k20;
1274 71344 : Pitch_CT = GENERIC;
1275 : }
1276 : }
1277 :
1278 2822174 : for ( i = 0; i < nb_subfr; i++ )
1279 : {
1280 2272008 : if ( L_frame == L_FRAME16k )
1281 : {
1282 356720 : acelp_cfg->pitch_bits[i] = ACB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( Pitch_BR, Pitch_CT, i * L_SUBFR, 0 )];
1283 356720 : acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX( Local_BR, LOCAL_CT, i * L_SUBFR, 0 )];
1284 : }
1285 : else
1286 : {
1287 1915288 : acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( Pitch_BR, Pitch_CT, i * L_SUBFR, 0 )];
1288 1915288 : acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX( Local_BR, LOCAL_CT, i * L_SUBFR, 0 )];
1289 1915288 : acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, LOCAL_CT, i * L_SUBFR, 0 )];
1290 : }
1291 : }
1292 : }
1293 :
1294 4525501 : if ( coder_type == TRANSITION && ( tc_call == 1 && tc_subfr == 0 && L_frame == L_FRAME ) )
1295 : {
1296 110812 : return error;
1297 : }
1298 :
1299 : /*-----------------------------------------------------------------*
1300 : * unused bits handling
1301 : *-----------------------------------------------------------------*/
1302 :
1303 4414689 : acelp_cfg->ubits = 0; /* these bits could be reused for something else */
1304 :
1305 4414689 : if ( flag_hardcoded && core_brate != PPP_NELP_2k80 )
1306 : {
1307 : /* unused bits */
1308 88300 : if ( coder_type == AUDIO || ( coder_type == INACTIVE && core_brate <= ACELP_24k40 ) )
1309 : {
1310 11445 : acelp_cfg->ubits = 0;
1311 : }
1312 76855 : else if ( L_frame == L_FRAME )
1313 : {
1314 43390 : acelp_cfg->ubits = reserved_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, -1, TC_SUBFR2IDX( tc_subfr ) )];
1315 : }
1316 : else
1317 : {
1318 33465 : acelp_cfg->ubits = 0;
1319 : }
1320 :
1321 88300 : bits -= acelp_cfg->ubits;
1322 : }
1323 :
1324 : /* sanity check */
1325 4414689 : if ( ( coder_type != INACTIVE && nb_subfr == NB_SUBFR && coder_type != AUDIO ) || nb_subfr == NB_SUBFR16k )
1326 : {
1327 3935153 : if ( ( L_frame == L_FRAME16k && coder_type == INACTIVE && inactive_coder_type_flag ) /* GSC Inactive @16kHz */ ||
1328 889 : ( GSC_IVAS_mode > 0 && L_frame == L_FRAME16k ) ) /* IVAS GSC @16kHz */
1329 : {
1330 71344 : acelp_cfg->ubits = 0;
1331 : }
1332 3863809 : else if ( flag_hardcoded && core == ACELP_CORE && bits != 0 )
1333 : {
1334 : #ifdef DEBUGGING
1335 : IVAS_ERROR( IVAS_ERR_INTERNAL, "ERROR: bit-budget incorrect (%d bits) in frame %d.\n", (int32_t) bits, frame );
1336 : #endif
1337 : }
1338 3863809 : else if ( bits > 0 && !( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) )
1339 : {
1340 1390471 : if ( idchan > 0 && element_mode == IVAS_CPE_TD )
1341 : {
1342 24539 : if ( !tdm_lp_reuse_flag )
1343 : {
1344 22952 : acelp_cfg->lsf_bits += bits; /* increase LSF Q bits */
1345 22952 : bits = 0;
1346 : }
1347 : else
1348 : {
1349 1587 : int16_t nb_prm = 4;
1350 :
1351 1587 : if ( tdm_low_rate_mode == 1 )
1352 : {
1353 0 : nb_prm = 2;
1354 : }
1355 : /* First add remaining bits on gains */
1356 1587 : bits -= allocate_unused( core_brate, coder_type, bits, nb_prm, 0, GAINSPRM, acelp_cfg->gains_mode );
1357 :
1358 : /* Then, Increase pitch bit budget */
1359 1587 : if ( tdm_Pitch_reuse_flag == 0 && bits > 0 )
1360 : {
1361 6 : bits -= allocate_unused( core_brate, coder_type, bits, nb_prm, 0, PITCHPRM, acelp_cfg->pitch_bits );
1362 : }
1363 :
1364 : /* Increase mid-lsf bit budget */
1365 1587 : if ( tdm_lp_reuse_flag == 0 && bits > 0 )
1366 : {
1367 0 : bits -= allocate_unused( core_brate, coder_type, bits, 1, 0, MID_LSFSPRM, &acelp_cfg->mid_lsf_bits );
1368 0 : bits -= allocate_unused( core_brate, coder_type, bits, 1, 0, LSFPRM, &acelp_cfg->lsf_bits );
1369 : }
1370 : }
1371 : #ifdef DEBUGGING
1372 : if ( idchan > 0 && bits > 0 && ( coder_type > UNVOICED || tdm_low_rate_mode == 0 ) )
1373 : {
1374 : IVAS_ERROR( IVAS_ERR_INTERNAL, "WARNING !! Unused bits in secondary channel at frame %d\n", frame );
1375 : }
1376 : #endif
1377 : }
1378 :
1379 1365932 : else if ( core == ACELP_CORE && coder_type >= UNVOICED && coder_type <= GENERIC && L_frame == L_FRAME )
1380 : {
1381 766893 : acelp_cfg->lsf_bits += bits; /* increase LSF Q bits */
1382 :
1383 766893 : if ( acelp_cfg->lsf_bits > 46 )
1384 : {
1385 2 : acelp_cfg->ubits = acelp_cfg->lsf_bits - 46;
1386 2 : acelp_cfg->lsf_bits = 46;
1387 : }
1388 766891 : else if ( acelp_cfg->lsf_bits > 42 && L_frame == L_FRAME )
1389 : {
1390 4363 : acelp_cfg->ubits = acelp_cfg->lsf_bits - 42;
1391 4363 : acelp_cfg->lsf_bits = 42;
1392 : }
1393 : }
1394 : else
1395 : {
1396 599039 : acelp_cfg->ubits = bits;
1397 : }
1398 : }
1399 : else if ( bits < 0 && !( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) )
1400 : {
1401 : #ifdef DEBUGGING
1402 : IVAS_ERROR( IVAS_ERR_INTERNAL, "ERROR: bit-budget incorrect (%d bits) in frame %d.\n", (int32_t) bits, frame );
1403 : #endif
1404 : }
1405 : }
1406 :
1407 4414689 : return error;
1408 : }
1409 :
1410 : /*-------------------------------------------------------------------*
1411 : * allocate_unused()
1412 : *
1413 : * Allocate unused bits
1414 : *--------------------------------------------------------------------*/
1415 :
1416 6372 : static int16_t allocate_unused(
1417 : const int32_t core_brate,
1418 : const int16_t coder_type,
1419 : const int16_t unused_bits,
1420 : const int16_t nb_prm,
1421 : const int16_t subfr,
1422 : const int16_t prm_type,
1423 : int16_t *prm_bit_mode )
1424 : {
1425 6372 : int16_t max_bit_per_pos = 0, bit_added = 0;
1426 :
1427 6372 : if ( prm_type == GAINSPRM )
1428 : {
1429 6348 : max_bit_per_pos = 6;
1430 6348 : if ( core_brate > ACELP_8k00 )
1431 : {
1432 5588 : max_bit_per_pos = 7;
1433 : }
1434 760 : else if ( coder_type != UNVOICED )
1435 : {
1436 760 : if ( subfr >= 1 )
1437 : {
1438 570 : max_bit_per_pos = 7;
1439 : }
1440 190 : else if ( subfr == 0 )
1441 : {
1442 190 : max_bit_per_pos = 8;
1443 : }
1444 : }
1445 0 : else if ( coder_type == UNVOICED )
1446 : {
1447 0 : max_bit_per_pos = 9; /* No real limit on UC gain bit budget of the secondary channel */
1448 : }
1449 : }
1450 24 : else if ( prm_type == PITCHPRM )
1451 : {
1452 24 : max_bit_per_pos = 6;
1453 24 : if ( subfr == 0 || subfr == 2 || nb_prm == 2 )
1454 : {
1455 12 : max_bit_per_pos = 10;
1456 : }
1457 :
1458 24 : if ( coder_type == UNVOICED )
1459 : {
1460 0 : max_bit_per_pos = 0; /* Should not allocate bits in case of unvoiced coder type */
1461 : }
1462 : }
1463 0 : else if ( prm_type == MID_LSFSPRM )
1464 : {
1465 0 : max_bit_per_pos = 5;
1466 : }
1467 0 : else if ( prm_type == LSFPRM )
1468 : {
1469 0 : max_bit_per_pos = 42;
1470 : }
1471 : else
1472 : {
1473 : #ifdef DEBUG_MODE_TD
1474 : IVAS_ERROR( IVAS_ERR_WRONG_MODE, "unknown mode in bit_alloc.c" );
1475 : #endif
1476 : }
1477 :
1478 6372 : max_bit_per_pos = min( unused_bits, max_bit_per_pos - prm_bit_mode[subfr] );
1479 6372 : if ( max_bit_per_pos < 0 )
1480 : {
1481 0 : return 0;
1482 : }
1483 6372 : else if ( max_bit_per_pos >= 0 && subfr == ( nb_prm - 1 ) )
1484 : {
1485 1593 : prm_bit_mode[subfr] += max_bit_per_pos;
1486 : }
1487 : else
1488 : {
1489 4779 : prm_bit_mode[subfr] += max_bit_per_pos;
1490 4779 : bit_added += allocate_unused( core_brate, coder_type, unused_bits - max_bit_per_pos, nb_prm, subfr + 1, prm_type, &prm_bit_mode[0] );
1491 : }
1492 :
1493 6372 : return bit_added + max_bit_per_pos;
1494 : }
1495 :
1496 :
1497 : /*-------------------------------------------------------------------*
1498 : * set_ACELP_flag()
1499 : *
1500 : * set ACELP@16kHz flag
1501 : *--------------------------------------------------------------------*/
1502 :
1503 : /*! r: ACELP16k flag */
1504 51357156 : int16_t set_ACELP_flag(
1505 : const int16_t element_mode, /* i : element mode */
1506 : const int32_t element_brate, /* i : element bitrate */
1507 : const int32_t total_brate, /* i : total bitrate per channel */
1508 : const int16_t idchan, /* i : Channel id */
1509 : const int16_t tdm_LRTD_flag, /* i : LRTD stereo mode flag */
1510 : const int16_t bwidth, /* i : audio bandwidth */
1511 : const int16_t cng_type /* i : CNG type */
1512 : )
1513 : {
1514 51357156 : if ( element_mode == IVAS_CPE_DFT && idchan == 0 && total_brate <= SID_2k40 && bwidth == WB && cng_type == LP_CNG )
1515 : {
1516 :
1517 3371 : return 1;
1518 : }
1519 51353785 : else if ( element_mode == IVAS_CPE_TD )
1520 : {
1521 135532 : if ( element_brate >= IVAS_24k4 && idchan == 0 && ( tdm_LRTD_flag == 0 || element_brate > IVAS_24k4 ) )
1522 : {
1523 47581 : return 1;
1524 : }
1525 : else
1526 : {
1527 87951 : return 0;
1528 : }
1529 : }
1530 51218253 : else if ( element_mode == IVAS_CPE_DFT )
1531 : {
1532 2000506 : if ( element_brate >= IVAS_24k4 )
1533 : {
1534 1035382 : return 1;
1535 : }
1536 : else
1537 : {
1538 965124 : return 0;
1539 : }
1540 : }
1541 49217747 : else if ( element_mode == IVAS_SCE )
1542 : {
1543 11857721 : if ( element_brate >= SCE_CORE_16k_LOW_LIMIT )
1544 : {
1545 8742004 : return 1;
1546 : }
1547 : else
1548 : {
1549 3115717 : return 0;
1550 : }
1551 : }
1552 37360026 : else if ( total_brate >= ACELP_16k_LOW_LIMIT ) /* EVS_MONO */
1553 : {
1554 36857602 : return 1;
1555 : }
1556 : else
1557 : {
1558 502424 : return 0;
1559 : }
1560 : }
|