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 2110095 : 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 2110095 : if ( bit_rate <= ACELP_9k60 )
80 : {
81 34537 : rate_mode_index = 0;
82 : }
83 : else
84 : {
85 2075558 : rate_mode_index = 1;
86 : }
87 :
88 2110095 : acelp_cfg->mode_index = rate_mode_index;
89 :
90 : /*LPC: midLpc should be swithced off?*/
91 2110095 : acelp_cfg->midLpc_enable = 1;
92 :
93 : /*ACELP ICB config*/
94 2110095 : if ( ( rate_mode_index == 0 ) || ( narrowBand == 1 ) )
95 : {
96 34637 : acelp_cfg->pre_emphasis = 1;
97 34637 : acelp_cfg->formant_enh = 1;
98 34637 : acelp_cfg->formant_enh_num = FORMANT_SHARPENING_G1;
99 34637 : acelp_cfg->formant_enh_den = FORMANT_SHARPENING_G2;
100 34637 : acelp_cfg->formant_tilt = 0;
101 34637 : acelp_cfg->voice_tilt = 0;
102 : }
103 : else
104 : {
105 2075458 : acelp_cfg->pre_emphasis = 0;
106 2075458 : acelp_cfg->formant_enh = 1;
107 2075458 : acelp_cfg->formant_enh_num = FORMANT_SHARPENING_G1;
108 2075458 : acelp_cfg->formant_enh_den = FORMANT_SHARPENING_G2;
109 2075458 : acelp_cfg->formant_tilt = 1;
110 2075458 : acelp_cfg->voice_tilt = 1;
111 : }
112 :
113 : /*Wide band @ 16kHz*/
114 2110095 : if ( nb_subfr == NB_SUBFR16k )
115 : {
116 994159 : acelp_cfg->pre_emphasis = 1;
117 994159 : acelp_cfg->formant_enh = 1;
118 994159 : acelp_cfg->formant_enh_num = FORMANT_SHARPENING_G1_16k;
119 994159 : acelp_cfg->formant_enh_den = FORMANT_SHARPENING_G2_16k;
120 994159 : acelp_cfg->formant_tilt = 0;
121 994159 : acelp_cfg->voice_tilt = 2;
122 : }
123 :
124 2110095 : 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 49499 : 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 49499 : mode_index = acelp_cfg->mode_index;
149 49499 : band_index = ( narrowBand == 0 );
150 49499 : bits = 0;
151 :
152 49499 : if ( band_index == 0 )
153 : {
154 2509 : if ( coder_type == INACTIVE )
155 : {
156 0 : acelp_cfg->formant_enh = 0;
157 : }
158 : else
159 : {
160 2509 : acelp_cfg->formant_enh = 1;
161 : }
162 : }
163 :
164 49499 : 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 49499 : if ( coder_type == UNVOICED )
186 : {
187 2929 : 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 2817 : acelp_cfg->pitch_sharpening = 0;
195 2817 : acelp_cfg->phase_scrambling = 0;
196 : }
197 : }
198 : else
199 : {
200 46570 : acelp_cfg->pitch_sharpening = 1;
201 46570 : acelp_cfg->phase_scrambling = 0;
202 : }
203 :
204 49499 : 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 49499 : acelp_cfg->bpf_mode = ACELP_BPF_MODE[mode_index][band_index][coder_type];
213 49499 : bits += ACELP_BPF_BITS[acelp_cfg->bpf_mode];
214 :
215 49499 : acelp_cfg->nrg_mode = ACELP_NRG_MODE[mode_index][band_index][coder_type];
216 49499 : acelp_cfg->nrg_bits = ACELP_NRG_BITS[acelp_cfg->nrg_mode];
217 49499 : bits += acelp_cfg->nrg_bits;
218 :
219 49499 : acelp_cfg->ltp_mode = ACELP_LTP_MODE[mode_index][band_index][coder_type];
220 49499 : acelp_cfg->ltp_bits = 0;
221 49499 : acelp_cfg->ltf_mode = ACELP_LTF_MODE[mode_index][band_index][coder_type];
222 49499 : acelp_cfg->ltf_bits = ACELP_LTF_BITS[acelp_cfg->ltf_mode];
223 :
224 49499 : if ( nb_subfr == NB_SUBFR16k && acelp_cfg->ltf_bits == 4 )
225 : {
226 16284 : acelp_cfg->ltf_bits++;
227 : }
228 49499 : bits += acelp_cfg->ltf_bits;
229 :
230 :
231 284620 : for ( i = 0; i < nb_subfr; i++ )
232 : {
233 235121 : 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 235121 : if ( coder_type >= ACELP_MODE_MAX && ( i == 1 || i == 3 ) )
237 : {
238 3146 : acelp_cfg->gains_mode[i] = 0;
239 : }
240 :
241 235121 : bits += ACELP_GAINS_BITS[acelp_cfg->gains_mode[i]];
242 235121 : bits += ACELP_LTP_BITS_SFR[acelp_cfg->ltp_mode][i];
243 235121 : acelp_cfg->ltp_bits += ACELP_LTP_BITS_SFR[acelp_cfg->ltp_mode][i];
244 : }
245 :
246 : /*Innovation*/
247 49499 : if ( bits_frame < bits )
248 : {
249 0 : printf( "\nWarning: bits per frame too low\n" );
250 0 : return -1;
251 : }
252 :
253 49499 : if ( coder_type == RF_ALLPRED )
254 : {
255 1005 : set_s( acelp_cfg->fixed_cdk_index, -1, nb_subfr );
256 : }
257 48494 : 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 47972 : 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 47966 : bits += BITS_ALLOC_adjust_acelp_fixed_cdk( bits_frame - bits, acelp_cfg->fixed_cdk_index, nb_subfr );
274 : }
275 :
276 49499 : remaining_bits = bits_frame - bits;
277 :
278 : /*Sanity check*/
279 49499 : if ( remaining_bits < 0 )
280 : {
281 0 : bits = -1;
282 : }
283 :
284 :
285 49499 : return ( bits );
286 : }
287 :
288 : /*-------------------------------------------------------------------*
289 : * BITS_ALLOC_adjust_acelp_fixed_cdk()
290 : *
291 : *
292 : *--------------------------------------------------------------------*/
293 :
294 47966 : 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 47966 : bits_subframe2 = bits_frame;
303 :
304 47966 : 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 1011134 : for ( k = 0; k < ACELP_FIXED_CDK_NB - 1; k++ )
311 : {
312 1010976 : if ( ACELP_FIXED_CDK_BITS( k ) * nb_subfr > bits_subframe2 )
313 : {
314 47808 : k--; /* previous mode did not exceed bit-budget */
315 47808 : break;
316 : }
317 : }
318 :
319 47966 : if ( ACELP_FIXED_CDK_BITS( k ) * nb_subfr > bits_subframe2 )
320 : {
321 158 : k--; /* previous mode did not exceed bit-budget */
322 : }
323 47966 : fixed_cdk_index[0] = k;
324 :
325 47966 : bitsused = ACELP_FIXED_CDK_BITS( k );
326 :
327 228989 : for ( sfr = 1; sfr < nb_subfr; sfr++ )
328 : {
329 181023 : bits_currsubframe = ( sfr * bits_subframe2 + bits_subframe2 ) - bitsused * nb_subfr;
330 :
331 : /* try increasing mode while below threshold */
332 226586 : while ( ( k < ACELP_FIXED_CDK_NB - 1 ) && ( ACELP_FIXED_CDK_BITS( k + 1 ) * nb_subfr <= bits_currsubframe ) )
333 : {
334 45563 : k++;
335 : }
336 :
337 : /* try decreasing mode until below threshold */
338 203855 : while ( ACELP_FIXED_CDK_BITS( k ) * nb_subfr > bits_currsubframe )
339 : {
340 22832 : k--;
341 22832 : if ( k == 0 )
342 : {
343 0 : break;
344 : }
345 : }
346 :
347 : /* store mode */
348 181023 : fixed_cdk_index[sfr] = k;
349 181023 : bitsused += ACELP_FIXED_CDK_BITS( k );
350 : }
351 :
352 47966 : return bitsused;
353 : }
354 :
355 :
356 : /*-------------------------------------------------------------------*
357 : * fcb_table()
358 : *
359 : * Selection of fixed innovation codebook bitbudget table
360 : *--------------------------------------------------------------------*/
361 :
362 115024133 : static int16_t fcb_table(
363 : const int16_t n,
364 : const int16_t L_subfr )
365 : {
366 : int16_t out;
367 :
368 115024133 : out = PulseConfTable[n].bits;
369 115024133 : if ( L_subfr > L_SUBFR )
370 : {
371 5122 : out = fast_FCB_bits_2sfr[n];
372 : }
373 :
374 115024133 : return ( out );
375 : }
376 :
377 : /*-------------------------------------------------------------------*
378 : * acelp_FCB_allocator()
379 : *
380 : * Routine to allocate fixed innovation codebook bit-budget
381 : *--------------------------------------------------------------------*/
382 :
383 5655879 : 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 5655879 : error = IVAS_ERR_OK;
400 :
401 5655879 : cdbk = coder_type; /* just to avoid warning when DEBUGGING is deactivated */
402 :
403 5655879 : 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 5655879 : if ( tc_subfr >= L_SUBFR && fix_first )
407 : {
408 : int16_t i;
409 :
410 96985 : for ( i = 0; i < nb_subfr; i++ )
411 : {
412 77588 : *nBits -= ACELP_FIXED_CDK_BITS( fixed_cdk_index[i] );
413 : }
414 19397 : return error;
415 : }
416 :
417 : /* TRANSITION coding: first subframe bit-budget was already fixed, glottal pulse in the first subframe */
418 5636482 : sfr = 0;
419 5636482 : if ( fix_first )
420 : {
421 188273 : *nBits -= ACELP_FIXED_CDK_BITS( fixed_cdk_index[0] );
422 188273 : sfr = 1;
423 188273 : p_fixed_cdk_index++;
424 188273 : nb_subfr = 3;
425 : }
426 :
427 : /* distribute the bit-budget equally between subframes */
428 5636482 : if ( L_subfr > L_SUBFR ) /* access fast_FCB_bits_2sfr */
429 : {
430 570 : max_n = 6;
431 : }
432 : else
433 : {
434 5635912 : max_n = ACELP_FIXED_CDK_NB;
435 : }
436 92478237 : for ( cdbk = 0; cdbk < max_n; cdbk++ )
437 : {
438 92478233 : if ( fcb_table( cdbk, L_subfr ) * nb_subfr > *nBits )
439 : {
440 5636478 : break;
441 : }
442 : }
443 5636482 : 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 5636482 : set_s( p_fixed_cdk_index, cdbk, nb_subfr );
456 5636482 : nBits_tmp = 0;
457 5636482 : if ( cdbk >= 0 )
458 : {
459 5636474 : nBits_tmp = fcb_table( cdbk, L_subfr );
460 : }
461 : else
462 : {
463 8 : nBits_tmp = 0;
464 : }
465 5636482 : *nBits -= nBits_tmp * nb_subfr;
466 :
467 : /* try to increase the FCB bit-budget of the first subframe(s) */
468 5636482 : if ( cdbk < ACELP_FIXED_CDK_NB - 1 )
469 : {
470 5636478 : step = fcb_table( cdbk + 1, L_subfr ) - nBits_tmp;
471 15378316 : while ( *nBits >= step )
472 : {
473 9741838 : ( *p_fixed_cdk_index )++;
474 9741838 : *nBits -= step;
475 9741838 : 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 5636478 : if ( fixed_cdk_index[sfr] < ACELP_FIXED_CDK_NB - 1 )
480 : {
481 5636474 : step = fcb_table( fixed_cdk_index[sfr] + 1, L_subfr ) - fcb_table( fixed_cdk_index[sfr], L_subfr );
482 5636474 : if ( *nBits >= step && cdbk >= 0 )
483 : {
484 824699 : fixed_cdk_index[sfr]++;
485 824699 : *nBits -= step;
486 :
487 824699 : if ( *nBits >= step && fixed_cdk_index[sfr + 1] == fixed_cdk_index[sfr] - 1 )
488 : {
489 55999 : sfr++;
490 55999 : fixed_cdk_index[sfr]++;
491 55999 : *nBits -= step;
492 : }
493 : }
494 : }
495 : }
496 : /* TRANSITION coding: allocate highest FCBQ bit-budget to the subframe with the glottal-shape codebook */
497 5636482 : if ( tc_subfr >= L_SUBFR )
498 : {
499 : int16_t tempr;
500 :
501 350568 : 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 350568 : if ( tc_subfr / L_SUBFR < nb_subfr - 1 )
505 : {
506 173227 : 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 5636482 : if ( L_subfr > L_SUBFR )
512 : {
513 : int16_t i, j;
514 1710 : for ( i = 0; i < nb_subfr; i++ )
515 : {
516 1140 : j = fixed_cdk_index[i];
517 1140 : fixed_cdk_index[i] = fast_FCB_bits_2sfr[j];
518 : }
519 : }
520 :
521 5636482 : 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 8637126 : 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 8637126 : error = IVAS_ERR_OK;
569 :
570 : /*-----------------------------------------------------------------*
571 : * Set the flag indicating two-stage Unvoiced (UC) frame
572 : *-----------------------------------------------------------------*/
573 :
574 8637126 : *uc_two_stage_flag = 0;
575 8637126 : if ( coder_type == UNVOICED )
576 : {
577 88681 : 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 87861 : *uc_two_stage_flag = 1;
580 : }
581 : }
582 :
583 : /*-----------------------------------------------------------------*
584 : * Set the number of subframes
585 : *-----------------------------------------------------------------*/
586 :
587 8637126 : if ( L_frame == L_FRAME )
588 : {
589 5205408 : 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 3431718 : 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 8637126 : coder_type_sw = coder_type;
616 8637126 : if ( core != ACELP_CORE )
617 : {
618 : /* used in acelp_core_switch_enc() */
619 1098 : nb_subfr = 1;
620 1098 : if ( L_frame == L_FRAME )
621 : {
622 233 : 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 8637126 : flag_hardcoded = 0;
631 8637126 : i = 0;
632 :
633 76308830 : while ( i < SIZE_BRATE_INTERMED_TBL )
634 : {
635 76308830 : if ( core_brate_inp == brate_intermed_tbl[i] )
636 : {
637 369044 : flag_hardcoded = 1;
638 369044 : break;
639 : }
640 :
641 75939786 : if ( core_brate_inp < brate_intermed_tbl[i] )
642 : {
643 8268082 : flag_hardcoded = 0;
644 8268082 : break;
645 : }
646 :
647 67671704 : i++;
648 : }
649 :
650 8637126 : if ( element_mode == IVAS_CPE_TD && coder_type == AUDIO &&
651 141 : 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 20 : i--;
654 : }
655 :
656 8637126 : core_brate = brate_intermed_tbl[i];
657 :
658 8637126 : if ( element_mode > EVS_MONO )
659 : {
660 8504951 : flag_hardcoded = 0; /* use automatic and flexible ACELP bit-budget allocation */
661 : }
662 :
663 8637126 : if ( core != ACELP_CORE && element_mode == EVS_MONO ) /* needed for mode1 core switching in EVS mono */
664 : {
665 1098 : flag_hardcoded = 1;
666 : }
667 :
668 : /*-----------------------------------------------------------------*
669 : * ACELP bit allocation
670 : *-----------------------------------------------------------------*/
671 :
672 8637126 : if ( !( coder_type == TRANSITION && tc_subfr != -1 ) || enc_dec == DEC )
673 : {
674 : /* Set the bit-budget */
675 8362339 : bits = (int16_t) ( core_brate_inp / FRAMES_PER_SEC );
676 :
677 8362339 : if ( coder_type == TRANSITION && enc_dec == DEC && tc_call == 1 )
678 : {
679 578026 : bits += *nBits_es_Pred; /* equalize for 4th signaling bit estimated at the encoder in TC_0_192 */
680 : }
681 :
682 : /* Subtract signaling bits */
683 8362339 : if ( enc_dec == DEC && idchan == 1 && element_mode > EVS_MONO )
684 : {
685 92052 : bits -= TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS;
686 :
687 92052 : if ( tdm_LRTD_flag == 1 )
688 : {
689 84892 : bits += STEREO_BITS_TCA;
690 : }
691 :
692 : /* subtract TBE/BWE flag */
693 92052 : if ( extl_brate > 0 && ( extl == WB_TBE || extl == SWB_TBE || extl == FB_TBE || extl == WB_BWE || extl == SWB_BWE || extl == FB_BWE ) )
694 : {
695 63379 : bits--;
696 : }
697 : }
698 : else
699 : {
700 : /* Subtract signaling bits */
701 8270287 : bits -= signaling_bits;
702 : }
703 :
704 8362339 : 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 6614842 : bits++;
708 : }
709 :
710 : /*-----------------------------------------------------------------*
711 : * LSF Q bit-budget
712 : *-----------------------------------------------------------------*/
713 :
714 8362339 : if ( !tdm_lp_reuse_flag || idchan == 0 )
715 : {
716 : /* LSF Q bit-budget */
717 8350120 : acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
718 :
719 8350120 : if ( !flag_hardcoded )
720 : {
721 8223466 : if ( L_frame == L_FRAME )
722 : {
723 4935234 : if ( element_mode == IVAS_SCE && tdm_low_rate_mode )
724 : {
725 1077666 : acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
726 : }
727 3857568 : else if ( ( total_brate < 7200 || coder_type == INACTIVE || coder_type == AUDIO ) && idchan == 1 )
728 : {
729 : /* TD stereo, secondary channel: do nothing */
730 24898 : acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
731 : }
732 3832670 : else if ( element_mode > EVS_MONO && coder_type == AUDIO && brate_intermed_tbl[i] < ACELP_9k60 )
733 : {
734 : /* primary channel: do nothing */
735 : }
736 3631997 : else if ( element_mode > EVS_MONO && coder_type == AUDIO /*&& brate_intermed_tbl[i] >= ACELP_9k60*/ )
737 : {
738 494565 : acelp_cfg->lsf_bits = 42;
739 : }
740 3137432 : else if ( total_brate <= 9600 || coder_type == UNVOICED )
741 : {
742 396669 : acelp_cfg->lsf_bits = 31;
743 : }
744 2740763 : else if ( total_brate <= 20000 )
745 : {
746 2561492 : acelp_cfg->lsf_bits = 36;
747 : }
748 : else
749 : {
750 179271 : acelp_cfg->lsf_bits = 41;
751 : }
752 : }
753 : else /* L_frame == L_FRAME16k */
754 : {
755 3288232 : acelp_cfg->lsf_bits = 41;
756 : }
757 : }
758 :
759 8350120 : bits -= acelp_cfg->lsf_bits;
760 :
761 : /* mid-LSF Q bit-budget */
762 8350120 : acelp_cfg->mid_lsf_bits = mid_LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
763 :
764 8350120 : if ( element_mode > EVS_MONO && coder_type == AUDIO /*&& brate_intermed_tbl[i] < ACELP_9k60*/ )
765 : {
766 695238 : acelp_cfg->mid_lsf_bits = 5;
767 : /* primary channel: do nothing */
768 : }
769 :
770 8350120 : bits -= acelp_cfg->mid_lsf_bits;
771 : }
772 12219 : else if ( tdm_lp_reuse_flag == 1 && idchan == 1 )
773 : {
774 12219 : bits -= TDM_IC_LSF_PRED_BITS;
775 : }
776 :
777 : /* gain Q bit-budget - part 1: 'Es_pred' of memory-less gain Q */
778 8362339 : 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 1449256 : ( coder_type == INACTIVE && !inactive_coder_type_flag ) /* AVQ inactive */
780 : )
781 : {
782 6097597 : *nBits_es_Pred = Es_pred_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, -1, -1 )];
783 6097597 : bits -= *nBits_es_Pred;
784 : }
785 2264742 : else if ( *uc_two_stage_flag )
786 : {
787 87861 : *nBits_es_Pred = 4;
788 87861 : bits -= *nBits_es_Pred;
789 : }
790 : }
791 : else
792 : {
793 274787 : bits = *unbits;
794 : }
795 :
796 8637126 : if ( coder_type == TRANSITION && tc_call == 0 )
797 : {
798 185504 : *unbits = bits;
799 185504 : return error;
800 : }
801 :
802 : /*-----------------------------------------------------------------*
803 : * Low-rate mode - bits are allocated in tdm_low_rate_enc()
804 : *-----------------------------------------------------------------*/
805 :
806 8451622 : if ( element_mode == IVAS_SCE && tdm_low_rate_mode )
807 : {
808 1077666 : acelp_cfg->FEC_mode = 0;
809 1077666 : acelp_cfg->ltf_mode = FULL_BAND;
810 1077666 : *nBits_es_Pred = 0;
811 1077666 : *unbits = 0;
812 1077666 : acelp_cfg->ubits = 0;
813 :
814 1077666 : return error;
815 : }
816 :
817 : /*-----------------------------------------------------------------*
818 : * Supplementary information for FEC
819 : *-----------------------------------------------------------------*/
820 :
821 7373956 : acelp_cfg->FEC_mode = 0;
822 7373956 : if ( core_brate >= ACELP_11k60 && ( idchan == 0 || element_mode == EVS_MONO ) )
823 : {
824 6036773 : acelp_cfg->FEC_mode = 1;
825 :
826 6036773 : if ( coder_type > UNVOICED && coder_type < AUDIO && coder_type != VOICED )
827 : {
828 5165393 : bits -= FEC_BITS_CLS;
829 : }
830 :
831 6036773 : if ( coder_type != TRANSITION )
832 : {
833 5166465 : if ( total_brate >= ACELP_16k40 )
834 : {
835 3194999 : acelp_cfg->FEC_mode = 2;
836 :
837 3194999 : if ( coder_type > UNVOICED && coder_type < AUDIO )
838 : {
839 2944194 : bits -= FEC_BITS_ENR;
840 : }
841 : }
842 :
843 5166465 : if ( total_brate >= ACELP_32k )
844 : {
845 505234 : acelp_cfg->FEC_mode = 3;
846 :
847 505234 : if ( coder_type > UNVOICED && coder_type < AUDIO )
848 : {
849 466159 : bits -= FEC_BITS_POS;
850 : }
851 : }
852 : }
853 : }
854 :
855 : /*-----------------------------------------------------------------*
856 : * LP filtering of the adaptive excitation
857 : *-----------------------------------------------------------------*/
858 :
859 7373956 : if ( idchan > 0 && element_mode > EVS_MONO )
860 : {
861 123229 : acelp_cfg->ltf_mode = FULL_BAND;
862 : }
863 7250727 : else if ( coder_type == UNVOICED )
864 : {
865 83765 : acelp_cfg->ltf_mode = FULL_BAND;
866 : }
867 7166962 : else if ( ( coder_type == GENERIC || coder_type == TRANSITION ) && core_brate < ACELP_11k60 )
868 : {
869 608155 : acelp_cfg->ltf_mode = LOW_PASS;
870 : }
871 6558807 : else if ( core_brate >= ACELP_11k60 && ( coder_type != AUDIO && !( coder_type == INACTIVE && L_frame == L_FRAME ) ) )
872 : {
873 5581875 : if ( coder_type == INACTIVE && L_frame == L_FRAME16k && inactive_coder_type_flag ) /* GSC Inactive @16kHz */
874 : {
875 149875 : acelp_cfg->ltf_mode = FULL_BAND;
876 : }
877 : else
878 : {
879 5432000 : acelp_cfg->ltf_mode = NORMAL_OPERATION;
880 5432000 : if ( coder_type != TRANSITION )
881 : {
882 4561692 : bits -= nb_subfr;
883 : }
884 : }
885 : }
886 : else
887 : {
888 976932 : acelp_cfg->ltf_mode = FULL_BAND;
889 : }
890 :
891 : /*-----------------------------------------------------------------*
892 : * UC bit-budget
893 : *-----------------------------------------------------------------*/
894 :
895 7373956 : if ( ( ( coder_type == UNVOICED && !( *uc_two_stage_flag ) ) || ( coder_type == INACTIVE && core_brate <= ACELP_9k60 ) ) && ( idchan == 0 || element_mode == EVS_MONO ) )
896 : {
897 23627 : bits -= NBITS_NOISENESS; /* noiseness */
898 : }
899 7373956 : if ( coder_type == UNVOICED && !( *uc_two_stage_flag ) )
900 : {
901 820 : bits -= ( 3 * NB_SUBFR ); /* tilt factor */
902 : }
903 :
904 : /*-----------------------------------------------------------------*
905 : * TC bit-budget
906 : *-----------------------------------------------------------------*/
907 :
908 7373956 : fix_first = 0;
909 7373956 : if ( coder_type == TRANSITION )
910 : {
911 999433 : if ( tc_call == 2 )
912 : {
913 211382 : fix_first = 1;
914 : }
915 :
916 : /* TC signaling */
917 999433 : if ( L_frame == L_FRAME )
918 : {
919 612686 : if ( tc_subfr == TC_0_0 )
920 : {
921 120965 : if ( enc_dec == ENC )
922 : {
923 49042 : bits -= 1; /* TC signaling */
924 : }
925 :
926 120965 : if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
927 : {
928 96287 : bits -= 3; /* LP filtering flag */
929 : }
930 : }
931 491721 : else if ( tc_subfr == TC_0_64 )
932 : {
933 59557 : if ( enc_dec == ENC )
934 : {
935 11196 : bits -= 4; /* TC signaling */
936 : }
937 :
938 59557 : if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
939 : {
940 44145 : bits -= 3; /* LP filtering flag */
941 : }
942 : }
943 432164 : else if ( tc_subfr == TC_0_128 )
944 : {
945 26253 : if ( enc_dec == ENC )
946 : {
947 6782 : bits -= 4; /* TC signaling */
948 : }
949 :
950 26253 : if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
951 : {
952 20411 : bits -= 2; /* LP filtering flag */
953 : }
954 : }
955 405911 : else if ( tc_subfr == TC_0_192 )
956 : {
957 9499 : if ( enc_dec == ENC )
958 : {
959 2634 : bits -= 3; /* TC signaling */
960 : }
961 :
962 9499 : if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
963 : {
964 7248 : bits -= 1; /* LP filtering flag */
965 : }
966 : }
967 396412 : else if ( tc_subfr == L_SUBFR )
968 : {
969 64783 : if ( enc_dec == ENC )
970 : {
971 25718 : bits -= 3; /* TC signaling */
972 : }
973 :
974 64783 : if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
975 : {
976 50444 : bits -= ( L_FRAME - tc_subfr - L_SUBFR ) / L_SUBFR; /* LP filtering flag */
977 : }
978 : }
979 : else
980 : {
981 331629 : if ( enc_dec == ENC )
982 : {
983 84030 : bits -= 4; /* TC signaling */
984 : }
985 :
986 331629 : if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
987 : {
988 265026 : bits -= ( L_FRAME - tc_subfr - L_SUBFR ) / L_SUBFR; /* LP filtering flag */
989 : }
990 : }
991 : }
992 : else /* L_frame == L_FRAME16k */
993 : {
994 386747 : if ( enc_dec == ENC )
995 : {
996 95385 : if ( tc_subfr <= 2 * L_SUBFR )
997 : {
998 63779 : bits -= 2; /* TC signaling */
999 : }
1000 : else
1001 : {
1002 31606 : bits -= 3; /* TC signaling */
1003 : }
1004 : }
1005 :
1006 386747 : bits -= ( L_FRAME16k - tc_subfr - L_SUBFR ) / L_SUBFR; /* LP filtering flag */
1007 : }
1008 :
1009 : /* glottal-shape codebook bits */
1010 999433 : bits -= ( 3 + 6 + 1 + 3 );
1011 : }
1012 :
1013 : /*-----------------------------------------------------------------*
1014 : * pitch, innovation, gains bit-budget
1015 : *-----------------------------------------------------------------*/
1016 :
1017 7373956 : acelp_cfg->fcb_mode = 0;
1018 :
1019 7373956 : 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 570 : set_s( acelp_cfg->pitch_bits, 0, NB_SUBFR16k );
1022 570 : set_s( acelp_cfg->gains_mode, 0, NB_SUBFR16k );
1023 :
1024 1710 : for ( i = 0; i < 2; i++ )
1025 : {
1026 1140 : acelp_cfg->pitch_bits[i] = 0;
1027 1140 : if ( tdm_Pitch_reuse_flag == 0 )
1028 : {
1029 1140 : acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, GENERIC, 2 * i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
1030 1140 : bits -= acelp_cfg->pitch_bits[i];
1031 : }
1032 1140 : acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, GENERIC, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
1033 1140 : bits -= acelp_cfg->gains_mode[i];
1034 : }
1035 570 : 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 570 : if ( bits >= 16 )
1046 : {
1047 570 : 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 570 : acelp_cfg->fixed_cdk_index[2] = -1;
1055 570 : acelp_cfg->fixed_cdk_index[3] = -1;
1056 : }
1057 7373386 : else if ( ( coder_type != INACTIVE && nb_subfr == NB_SUBFR && coder_type != AUDIO ) || /* @12.8kHz core except of GSC */
1058 3335468 : ( nb_subfr == NB_SUBFR16k && ( !inactive_coder_type_flag || coder_type != INACTIVE ) ) /* @16kHz core GC, TC, AVQ inactive */
1059 1011085 : || core == HQ_CORE /* ACELP -> HQ switching in EVS */ )
1060 : {
1061 : /* pitch Q & gain Q bit-budget - part 2*/
1062 34999294 : for ( i = 0; i < nb_subfr; i++ )
1063 : {
1064 28635895 : if ( L_frame == L_FRAME )
1065 : {
1066 12707065 : if ( tdm_Pitch_reuse_flag == 1 && idchan == 1 )
1067 : {
1068 1816 : acelp_cfg->pitch_bits[i] = 0;
1069 : }
1070 : else
1071 : {
1072 12705249 : acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
1073 : }
1074 12707065 : 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 15928830 : if ( tdm_Pitch_reuse_flag == 1 && idchan == 1 )
1079 : {
1080 0 : acelp_cfg->pitch_bits[i] = 0;
1081 : }
1082 : else
1083 : {
1084 15928830 : 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 15928830 : 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 28635895 : bits -= acelp_cfg->pitch_bits[i];
1090 :
1091 28635895 : if ( coder_type == INACTIVE && acelp_cfg->gains_mode[i] == 6 /* VQ vs. SQ threshold @32 kbps */ )
1092 : {
1093 192005 : bits -= 5;
1094 : }
1095 : else
1096 : {
1097 28443890 : if ( *uc_two_stage_flag == 1 )
1098 : {
1099 351444 : acelp_cfg->gains_mode[i] = 7;
1100 : }
1101 :
1102 28443890 : bits -= acelp_cfg->gains_mode[i];
1103 : }
1104 : }
1105 :
1106 : /* algebraic codebook bit-budget */
1107 6363399 : if ( flag_hardcoded || /* EVS */
1108 6252607 : ( core_brate_inp >= MIN_BRATE_AVQ_EXC && coder_type != INACTIVE ) /* high-birate ACELP except IC */ ||
1109 312843 : ( !inactive_coder_type_flag && coder_type == INACTIVE ) /* AVQ inactive */ )
1110 : {
1111 4179446 : for ( i = 0; i < nb_subfr; i++ )
1112 : {
1113 3471730 : if ( L_frame == L_FRAME )
1114 : {
1115 250065 : 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 3221665 : 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 3471730 : bits -= acelp_cfg->fixed_cdk_index[i];
1122 : }
1123 : }
1124 5655683 : else if ( !( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) )
1125 : {
1126 5655337 : if ( coder_type == UNVOICED && !( *uc_two_stage_flag ) )
1127 : {
1128 28 : i = bits / NB_SUBFR;
1129 28 : if ( i % 2 == 0 )
1130 : {
1131 28 : i--; /* must be odd */
1132 : }
1133 28 : 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 28 : i = max( i, 0 ); /* If i == 0-> random noise generator will be used as FCB */
1139 28 : set_s( acelp_cfg->fixed_cdk_index, i, NB_SUBFR );
1140 28 : bits -= ( i * NB_SUBFR );
1141 : }
1142 : else
1143 : {
1144 :
1145 5655309 : acelp_cfg->fcb_mode = 1;
1146 5655309 : if ( element_mode == IVAS_CPE_TD )
1147 : {
1148 213174 : if ( bits >= ACELP_FIXED_CDK_BITS( 0 ) * ( nb_subfr ) ) /* enough bits for all fcb */
1149 : {
1150 212879 : acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
1151 : }
1152 295 : else if ( bits >= ACELP_FIXED_CDK_BITS( 0 ) * ( nb_subfr - 1 ) )
1153 : {
1154 12 : acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr - 1, L_SUBFR, coder_type, tc_subfr, fix_first );
1155 12 : acelp_cfg->fixed_cdk_index[3] = -1;
1156 : }
1157 283 : else if ( bits >= ACELP_FIXED_CDK_BITS( 0 ) * ( nb_subfr - 2 ) )
1158 : {
1159 283 : acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr - 2, L_SUBFR, coder_type, tc_subfr, fix_first );
1160 283 : acelp_cfg->fixed_cdk_index[2] = acelp_cfg->fixed_cdk_index[1];
1161 283 : acelp_cfg->fixed_cdk_index[1] = -1;
1162 283 : 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 5442135 : 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 5442135 : 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 6363399 : if ( ( core_brate_inp >= MIN_BRATE_AVQ_EXC && coder_type != INACTIVE ) /* high-birate ACELP except IC */ ||
1197 318443 : ( !inactive_coder_type_flag && coder_type == INACTIVE ) /* AVQ inactive */ )
1198 : {
1199 3864960 : for ( i = 0; i < nb_subfr; i++ )
1200 : {
1201 3220800 : if ( flag_hardcoded )
1202 : {
1203 236180 : 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 236180 : bits -= acelp_cfg->AVQ_cdk_bits[i];
1206 : }
1207 : }
1208 :
1209 3220800 : bits -= G_AVQ_BITS;
1210 : }
1211 :
1212 644160 : 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 282598 : bits--;
1216 : }
1217 :
1218 644160 : if ( !flag_hardcoded )
1219 : {
1220 : int16_t bit_tmp;
1221 :
1222 596924 : bit_tmp = bits / nb_subfr;
1223 596924 : set_s( acelp_cfg->AVQ_cdk_bits, bit_tmp, nb_subfr );
1224 596924 : bits -= bit_tmp * nb_subfr;
1225 :
1226 596924 : bit_tmp = bits % nb_subfr;
1227 596924 : acelp_cfg->AVQ_cdk_bits[0] += bit_tmp;
1228 596924 : bits -= bit_tmp;
1229 : }
1230 : }
1231 : }
1232 1009987 : else if ( ( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) /* LBR secondary channel in TD stereo */ ||
1233 1009987 : ( ( coder_type == INACTIVE || coder_type == AUDIO ) && nb_subfr == NB_SUBFR ) /* GSC @12.8kHz */ ||
1234 149875 : ( 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 1009987 : if ( GSC_IVAS_mode > 0 && ( GSC_noisy_speech || core_brate > GSC_H_RATE_STG ) )
1241 : {
1242 36744 : Local_BR = ACELP_8k00;
1243 36744 : Pitch_CT = GENERIC;
1244 36744 : Pitch_BR = ACELP_8k00;
1245 36744 : 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 973243 : else if ( GSC_noisy_speech )
1256 : {
1257 8546 : Local_BR = ACELP_7k20;
1258 8546 : Pitch_CT = GENERIC;
1259 8546 : Pitch_BR = ACELP_7k20;
1260 8546 : if ( L_frame == L_FRAME16k )
1261 : {
1262 0 : Pitch_BR = core_brate;
1263 : }
1264 : }
1265 : else
1266 : {
1267 964697 : Local_BR = ACELP_7k20;
1268 964697 : Pitch_CT = AUDIO;
1269 964697 : Pitch_BR = core_brate;
1270 :
1271 964697 : if ( L_frame == L_FRAME16k )
1272 : {
1273 149875 : Local_BR = ACELP_13k20;
1274 149875 : Pitch_CT = GENERIC;
1275 : }
1276 : }
1277 :
1278 5199810 : for ( i = 0; i < nb_subfr; i++ )
1279 : {
1280 4189823 : if ( L_frame == L_FRAME16k )
1281 : {
1282 749375 : acelp_cfg->pitch_bits[i] = ACB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( Pitch_BR, Pitch_CT, i * L_SUBFR, 0 )];
1283 749375 : 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 3440448 : acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( Pitch_BR, Pitch_CT, i * L_SUBFR, 0 )];
1288 3440448 : acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX( Local_BR, LOCAL_CT, i * L_SUBFR, 0 )];
1289 3440448 : acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, LOCAL_CT, i * L_SUBFR, 0 )];
1290 : }
1291 : }
1292 : }
1293 :
1294 7373956 : if ( coder_type == TRANSITION && ( tc_call == 1 && tc_subfr == 0 && L_frame == L_FRAME ) )
1295 : {
1296 191753 : return error;
1297 : }
1298 :
1299 : /*-----------------------------------------------------------------*
1300 : * unused bits handling
1301 : *-----------------------------------------------------------------*/
1302 :
1303 7182203 : acelp_cfg->ubits = 0; /* these bits could be reused for something else */
1304 :
1305 7182203 : if ( flag_hardcoded && core_brate != PPP_NELP_2k80 )
1306 : {
1307 : /* unused bits */
1308 125495 : if ( coder_type == AUDIO || ( coder_type == INACTIVE && core_brate <= ACELP_24k40 ) )
1309 : {
1310 18183 : acelp_cfg->ubits = 0;
1311 : }
1312 107312 : else if ( L_frame == L_FRAME )
1313 : {
1314 59211 : acelp_cfg->ubits = reserved_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, -1, TC_SUBFR2IDX( tc_subfr ) )];
1315 : }
1316 : else
1317 : {
1318 48101 : acelp_cfg->ubits = 0;
1319 : }
1320 :
1321 125495 : bits -= acelp_cfg->ubits;
1322 : }
1323 :
1324 : /* sanity check */
1325 7182203 : if ( ( coder_type != INACTIVE && nb_subfr == NB_SUBFR && coder_type != AUDIO ) || nb_subfr == NB_SUBFR16k )
1326 : {
1327 6320993 : if ( ( L_frame == L_FRAME16k && coder_type == INACTIVE && inactive_coder_type_flag ) /* GSC Inactive @16kHz */ ||
1328 1110 : ( GSC_IVAS_mode > 0 && L_frame == L_FRAME16k ) ) /* IVAS GSC @16kHz */
1329 : {
1330 149875 : acelp_cfg->ubits = 0;
1331 : }
1332 6171118 : 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 6171118 : else if ( bits > 0 && !( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) )
1339 : {
1340 2246849 : if ( idchan > 0 && element_mode == IVAS_CPE_TD )
1341 : {
1342 44276 : if ( !tdm_lp_reuse_flag )
1343 : {
1344 41183 : acelp_cfg->lsf_bits += bits; /* increase LSF Q bits */
1345 41183 : bits = 0;
1346 : }
1347 : else
1348 : {
1349 3093 : int16_t nb_prm = 4;
1350 :
1351 3093 : if ( tdm_low_rate_mode == 1 )
1352 : {
1353 0 : nb_prm = 2;
1354 : }
1355 : /* First add remaining bits on gains */
1356 3093 : bits -= allocate_unused( core_brate, coder_type, bits, nb_prm, 0, GAINSPRM, acelp_cfg->gains_mode );
1357 :
1358 : /* Then, Increase pitch bit budget */
1359 3093 : if ( tdm_Pitch_reuse_flag == 0 && bits > 0 )
1360 : {
1361 12 : 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 3093 : 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 2202573 : else if ( core == ACELP_CORE && coder_type >= UNVOICED && coder_type <= GENERIC && L_frame == L_FRAME )
1380 : {
1381 1258068 : acelp_cfg->lsf_bits += bits; /* increase LSF Q bits */
1382 :
1383 1258068 : if ( acelp_cfg->lsf_bits > 46 )
1384 : {
1385 4 : acelp_cfg->ubits = acelp_cfg->lsf_bits - 46;
1386 4 : acelp_cfg->lsf_bits = 46;
1387 : }
1388 1258064 : else if ( acelp_cfg->lsf_bits > 42 && L_frame == L_FRAME )
1389 : {
1390 7693 : acelp_cfg->ubits = acelp_cfg->lsf_bits - 42;
1391 7693 : acelp_cfg->lsf_bits = 42;
1392 : }
1393 : }
1394 : else
1395 : {
1396 944505 : 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 7182203 : return error;
1408 : }
1409 :
1410 : /*-------------------------------------------------------------------*
1411 : * allocate_unused()
1412 : *
1413 : * Allocate unused bits
1414 : *--------------------------------------------------------------------*/
1415 :
1416 12420 : 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 12420 : int16_t max_bit_per_pos = 0, bit_added = 0;
1426 :
1427 12420 : if ( prm_type == GAINSPRM )
1428 : {
1429 12372 : max_bit_per_pos = 6;
1430 12372 : if ( core_brate > ACELP_8k00 )
1431 : {
1432 10876 : max_bit_per_pos = 7;
1433 : }
1434 1496 : else if ( coder_type != UNVOICED )
1435 : {
1436 1496 : if ( subfr >= 1 )
1437 : {
1438 1122 : max_bit_per_pos = 7;
1439 : }
1440 374 : else if ( subfr == 0 )
1441 : {
1442 374 : 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 48 : else if ( prm_type == PITCHPRM )
1451 : {
1452 48 : max_bit_per_pos = 6;
1453 48 : if ( subfr == 0 || subfr == 2 || nb_prm == 2 )
1454 : {
1455 24 : max_bit_per_pos = 10;
1456 : }
1457 :
1458 48 : 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 12420 : max_bit_per_pos = min( unused_bits, max_bit_per_pos - prm_bit_mode[subfr] );
1479 12420 : if ( max_bit_per_pos < 0 )
1480 : {
1481 0 : return 0;
1482 : }
1483 12420 : else if ( max_bit_per_pos >= 0 && subfr == ( nb_prm - 1 ) )
1484 : {
1485 3105 : prm_bit_mode[subfr] += max_bit_per_pos;
1486 : }
1487 : else
1488 : {
1489 9315 : prm_bit_mode[subfr] += max_bit_per_pos;
1490 9315 : 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 12420 : 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 79701244 : 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 79701244 : if ( element_mode == IVAS_CPE_DFT && idchan == 0 && total_brate <= SID_2k40 && bwidth == WB && cng_type == LP_CNG )
1515 : {
1516 :
1517 5891 : return 1;
1518 : }
1519 79695353 : else if ( element_mode == IVAS_CPE_TD )
1520 : {
1521 250154 : if ( element_brate >= IVAS_24k4 && idchan == 0 && ( tdm_LRTD_flag == 0 || element_brate > IVAS_24k4 ) )
1522 : {
1523 91008 : return 1;
1524 : }
1525 : else
1526 : {
1527 159146 : return 0;
1528 : }
1529 : }
1530 79445199 : else if ( element_mode == IVAS_CPE_DFT )
1531 : {
1532 3789254 : if ( element_brate >= IVAS_24k4 )
1533 : {
1534 1954435 : return 1;
1535 : }
1536 : else
1537 : {
1538 1834819 : return 0;
1539 : }
1540 : }
1541 75655945 : else if ( element_mode == IVAS_SCE )
1542 : {
1543 17910974 : if ( element_brate >= SCE_CORE_16k_LOW_LIMIT )
1544 : {
1545 13012208 : return 1;
1546 : }
1547 : else
1548 : {
1549 4898766 : return 0;
1550 : }
1551 : }
1552 57744971 : else if ( total_brate >= ACELP_16k_LOW_LIMIT ) /* EVS_MONO */
1553 : {
1554 56924409 : return 1;
1555 : }
1556 : else
1557 : {
1558 820562 : return 0;
1559 : }
1560 : }
|