Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 : #include <stdint.h>
5 : #include <stdlib.h>
6 : #include <assert.h>
7 : #include "options.h"
8 : #include "prot_fx.h"
9 : #include "ivas_prot_fx.h"
10 : #include "basop_util.h"
11 : #include "rom_com.h"
12 :
13 : #include "ivas_cnst.h"
14 : #include "ivas_rom_com.h"
15 : #ifndef SWAP
16 : #define SWAP( a, b ) \
17 : { \
18 : tempr = ( a ); \
19 : ( a ) = ( b ); \
20 : ( b ) = tempr; \
21 : }
22 : #endif
23 : /*-------------------------------------------------------------------*
24 : * Local function prototypes
25 : *--------------------------------------------------------------------*/
26 : static Word16 BITS_ALLOC_adjust_acelp_fixed_cdk( const Word16 bits_frame, Word16 *fixed_cdk_index, const Word16 nb_subfr );
27 : static Word16 BITS_ALLOC_adjust_generic( const Word16 bits_frame, Word16 *fixed_cdk_index, const Word16 nb_subfr, const Word16 *pulseconfigbits, const Word16 pulseconfig_size );
28 : static Word16 allocate_unused( const Word32 core_brate, const Word16 coder_type, const Word16 unused_bits, const Word16 nb_prm, const Word16 subfr, const Word16 prm_type, Word16 *prm_bit_mode );
29 :
30 : typedef enum
31 : {
32 : GAINSPRM = 0x0,
33 : PITCHPRM = 0x1,
34 : MID_LSFSPRM = 0x3,
35 : LSFPRM = 0x4
36 : } PRM_TYPES;
37 :
38 : /*
39 : * function BITS_ALLOC_init_config_acelp()
40 : *
41 : * description: initial configuration for ACELP
42 : *
43 : * return: void
44 : */
45 23329 : void BITS_ALLOC_init_config_acelp(
46 : const Word32 bit_rate,
47 : const Word16 narrowBand,
48 : const Word16 nb_subfr,
49 : ACELP_config *pConfigAcelp /*o: configuration structure of ACELP*/
50 : )
51 : {
52 : Word8 rate_mode_index;
53 :
54 23329 : rate_mode_index = (Word8) GT_32( bit_rate, ACELP_9k60 );
55 :
56 23329 : pConfigAcelp->mode_index = rate_mode_index;
57 23329 : move16();
58 :
59 : /*LPC: midLpc should be swithced off?*/
60 23329 : pConfigAcelp->midLpc_enable = 1;
61 23329 : move16();
62 :
63 : /*ACELP ICB config*/
64 23329 : test();
65 23329 : IF( ( rate_mode_index == 0 ) || ( narrowBand != 0 ) )
66 : {
67 425 : move16();
68 425 : move16();
69 425 : move16();
70 425 : move16();
71 425 : move16();
72 425 : move16();
73 425 : pConfigAcelp->pre_emphasis = 1;
74 425 : pConfigAcelp->formant_enh = 1;
75 425 : pConfigAcelp->formant_enh_num = FORMANT_SHARPENING_G1;
76 425 : pConfigAcelp->formant_enh_den = FORMANT_SHARPENING_G2;
77 425 : pConfigAcelp->formant_tilt = 0;
78 425 : pConfigAcelp->voice_tilt = 0;
79 : }
80 : ELSE
81 : {
82 22904 : move16();
83 22904 : move16();
84 22904 : move16();
85 22904 : move16();
86 22904 : move16();
87 22904 : move16();
88 22904 : pConfigAcelp->pre_emphasis = 0;
89 22904 : pConfigAcelp->formant_enh = 1;
90 22904 : pConfigAcelp->formant_enh_num = FORMANT_SHARPENING_G1;
91 22904 : pConfigAcelp->formant_enh_den = FORMANT_SHARPENING_G2;
92 22904 : pConfigAcelp->formant_tilt = 1;
93 22904 : pConfigAcelp->voice_tilt = 1;
94 : }
95 :
96 : /*Wide band @ 16kHz*/
97 23329 : IF( EQ_16( nb_subfr, NB_SUBFR16k ) )
98 : {
99 12013 : move16();
100 12013 : move16();
101 12013 : move16();
102 12013 : move16();
103 12013 : move16();
104 12013 : move16();
105 12013 : pConfigAcelp->pre_emphasis = 1;
106 12013 : pConfigAcelp->formant_enh = 1;
107 12013 : pConfigAcelp->formant_enh_num = FORMANT_SHARPENING_G1_16k;
108 12013 : pConfigAcelp->formant_enh_den = FORMANT_SHARPENING_G2_16k;
109 12013 : pConfigAcelp->formant_tilt = 0;
110 12013 : pConfigAcelp->voice_tilt = 2;
111 : }
112 23329 : }
113 :
114 :
115 : /*
116 : * function BITS_ALLOC_config_acelp()
117 : *
118 : * description: configure all Mode 2 ACELP coder types and allocate the bits
119 : *
120 : * return: bit demand
121 : */
122 604 : Word16 BITS_ALLOC_config_acelp(
123 : const Word16 bits_frame, /* i: remaining bit budget for the frame*/
124 : const Word16 coder_type, /* i: acelp coder type*/
125 : ACELP_config *pConfigAcelp, /* i/o: configuration structure of ACELP*/
126 : const Word16 narrowBand, /* i : narrowband flag */
127 : const Word16 nb_subfr /* i : number of subframes */
128 : )
129 : {
130 : Word16 mode_index;
131 : Word16 band_index;
132 : Word16 i;
133 : Word16 remaining_bits, bits;
134 :
135 :
136 604 : move16();
137 604 : move16();
138 604 : move16();
139 604 : mode_index = pConfigAcelp->mode_index;
140 604 : band_index = ( narrowBand == 0 );
141 604 : bits = 0;
142 :
143 604 : IF( band_index == 0 )
144 : {
145 0 : move16();
146 0 : pConfigAcelp->formant_enh = 1;
147 0 : if ( coder_type == INACTIVE )
148 : {
149 0 : move16();
150 0 : pConfigAcelp->formant_enh = 0;
151 : }
152 : }
153 :
154 604 : test();
155 604 : IF( EQ_16( band_index, 1 ) && EQ_16( nb_subfr, 4 ) )
156 : {
157 0 : IF( coder_type == INACTIVE )
158 : {
159 0 : pConfigAcelp->pre_emphasis = 0;
160 0 : move16();
161 0 : pConfigAcelp->formant_enh = 0;
162 0 : move16();
163 0 : pConfigAcelp->formant_enh_num = FORMANT_SHARPENING_G1_16k;
164 0 : move16();
165 0 : pConfigAcelp->voice_tilt = 1;
166 0 : move16();
167 0 : pConfigAcelp->formant_tilt = 1;
168 0 : move16();
169 : }
170 : ELSE
171 : {
172 0 : pConfigAcelp->pre_emphasis = 1;
173 0 : move16();
174 0 : pConfigAcelp->formant_enh = 1;
175 0 : move16();
176 0 : pConfigAcelp->formant_enh_num = FORMANT_SHARPENING_G1;
177 0 : move16();
178 0 : pConfigAcelp->voice_tilt = 0;
179 0 : move16();
180 0 : pConfigAcelp->formant_tilt = 0;
181 0 : move16();
182 : }
183 : }
184 604 : IF( EQ_16( coder_type, UNVOICED ) )
185 : {
186 26 : IF( EQ_16( ACELP_GAINS_MODE[mode_index][band_index][coder_type], 6 ) )
187 : {
188 0 : pConfigAcelp->pitch_sharpening = 0;
189 0 : move16();
190 0 : pConfigAcelp->phase_scrambling = 1;
191 0 : move16();
192 : }
193 : ELSE
194 : {
195 26 : pConfigAcelp->pitch_sharpening = 0;
196 26 : move16();
197 26 : pConfigAcelp->phase_scrambling = 0;
198 26 : move16();
199 : }
200 : }
201 : ELSE
202 : {
203 578 : pConfigAcelp->pitch_sharpening = 1;
204 578 : move16();
205 578 : pConfigAcelp->phase_scrambling = 0;
206 578 : move16();
207 : }
208 :
209 604 : IF( GT_16( coder_type, ACELP_MODE_MAX ) ) /* keep pitch sharpening for RF_ALLPRED mode */
210 : {
211 0 : pConfigAcelp->pitch_sharpening = 0;
212 0 : move16();
213 0 : pConfigAcelp->phase_scrambling = 0;
214 0 : move16();
215 : }
216 :
217 : /*Allocate bits and different modes*/
218 604 : move16();
219 604 : pConfigAcelp->bpf_mode = ACELP_BPF_MODE[mode_index][band_index][coder_type];
220 604 : bits = add( bits, ACELP_BPF_BITS[pConfigAcelp->bpf_mode] );
221 :
222 604 : move16();
223 604 : move16();
224 604 : pConfigAcelp->nrg_mode = ACELP_NRG_MODE[mode_index][band_index][coder_type];
225 604 : pConfigAcelp->nrg_bits = ACELP_NRG_BITS[pConfigAcelp->nrg_mode];
226 604 : bits = add( bits, pConfigAcelp->nrg_bits );
227 :
228 604 : move16();
229 604 : pConfigAcelp->ltp_mode = ACELP_LTP_MODE[mode_index][band_index][coder_type];
230 :
231 604 : move16();
232 604 : pConfigAcelp->ltp_bits = 0;
233 :
234 604 : move16();
235 604 : pConfigAcelp->ltf_mode = ACELP_LTF_MODE[mode_index][band_index][coder_type];
236 :
237 604 : move16();
238 604 : pConfigAcelp->ltf_bits = ACELP_LTF_BITS[pConfigAcelp->ltf_mode];
239 604 : test();
240 604 : IF( EQ_16( nb_subfr, 5 ) && EQ_16( pConfigAcelp->ltf_bits, 4 ) )
241 : {
242 268 : pConfigAcelp->ltf_bits = add( pConfigAcelp->ltf_bits, 1 );
243 268 : move16();
244 : }
245 604 : bits = add( bits, pConfigAcelp->ltf_bits );
246 :
247 :
248 3624 : FOR( i = 0; i < nb_subfr; i++ )
249 : {
250 3020 : pConfigAcelp->gains_mode[i] = ACELP_GAINS_MODE[mode_index][band_index][coder_type];
251 3020 : move16();
252 :
253 : /* skip subframe 1, 3 gain encoding, and use from subframe 0, and 3, respectively */
254 3020 : test();
255 3020 : test();
256 3020 : if ( GE_16( coder_type, ACELP_MODE_MAX ) && ( EQ_16( i, 1 ) || EQ_16( i, 3 ) ) )
257 : {
258 0 : pConfigAcelp->gains_mode[i] = 0;
259 0 : move16();
260 : }
261 :
262 3020 : bits = add( bits, ACELP_GAINS_BITS[pConfigAcelp->gains_mode[i]] );
263 :
264 3020 : move16();
265 3020 : bits = add( bits, ACELP_LTP_BITS_SFR[pConfigAcelp->ltp_mode][i] );
266 3020 : pConfigAcelp->ltp_bits = add( pConfigAcelp->ltp_bits, ACELP_LTP_BITS_SFR[pConfigAcelp->ltp_mode][i] );
267 : }
268 :
269 : /*Innovation*/
270 :
271 604 : IF( LT_16( bits_frame, bits ) )
272 : {
273 0 : printf( "Warning: bits per frame too low\n" );
274 0 : return -1;
275 : }
276 :
277 604 : IF( EQ_16( coder_type, RF_ALLPRED ) )
278 : {
279 0 : set16_fx( pConfigAcelp->fixed_cdk_index, -1, nb_subfr );
280 : }
281 604 : ELSE IF( EQ_16( coder_type, RF_GENPRED ) )
282 : {
283 0 : pConfigAcelp->fixed_cdk_index[0] = 0; /* 7 bits */
284 0 : move16();
285 0 : pConfigAcelp->fixed_cdk_index[1] = -1;
286 0 : move16();
287 0 : pConfigAcelp->fixed_cdk_index[2] = 0; /* 7 bits */
288 0 : move16();
289 0 : pConfigAcelp->fixed_cdk_index[3] = -1;
290 0 : move16();
291 0 : pConfigAcelp->fixed_cdk_index[4] = -1;
292 0 : move16();
293 0 : bits = add( bits, 14 );
294 : }
295 604 : ELSE IF( EQ_16( coder_type, RF_NOPRED ) )
296 : {
297 0 : set16_fx( pConfigAcelp->fixed_cdk_index, 0, nb_subfr );
298 0 : bits = add( bits, 28 );
299 : }
300 : ELSE
301 : {
302 604 : bits = add( bits, BITS_ALLOC_adjust_acelp_fixed_cdk( sub( bits_frame, bits ), pConfigAcelp->fixed_cdk_index, nb_subfr ) );
303 : }
304 :
305 604 : remaining_bits = sub( bits_frame, bits );
306 :
307 : /*Sanity check*/
308 604 : if ( remaining_bits < 0 )
309 : {
310 0 : move16();
311 0 : bits = -1;
312 : }
313 :
314 :
315 604 : return ( bits );
316 : }
317 :
318 :
319 604 : static Word16 BITS_ALLOC_adjust_generic(
320 : const Word16 bits_frame, /*i: bit budget*/
321 : Word16 *fixed_cdk_index,
322 : const Word16 nb_subfr,
323 : const Word16 *pulseconfigbits,
324 : const Word16 pulseconfig_size )
325 : {
326 : Word16 bits_subframe2, inb_subfr;
327 : Word16 sfr, k, bitsused, bits_currsubframe;
328 :
329 604 : bits_subframe2 = bits_frame;
330 604 : move16();
331 604 : inb_subfr = 8192 /*1.0f/NB_SUBFR Q15*/;
332 604 : move16();
333 604 : if ( EQ_16( nb_subfr, NB_SUBFR16k ) )
334 : {
335 604 : inb_subfr = 6554 /*1.0f/NB_SUBFR16k Q15*/;
336 604 : move16();
337 : }
338 :
339 604 : IF( LT_16( bits_subframe2, i_mult2( pulseconfigbits[0], nb_subfr ) ) ) /* not in final code - not instrumented */
340 : {
341 0 : return add( bits_frame, 1 ); /* Not enough bits for lowest mode. -> trigger alarm*/
342 : }
343 :
344 : /* search cdk-index for first subframe */
345 16179 : FOR( k = 0; k < pulseconfig_size - 1; k++ )
346 : {
347 :
348 16179 : IF( GT_16( i_mult2( pulseconfigbits[k], nb_subfr ), bits_subframe2 ) )
349 : {
350 604 : k = sub( k, 1 ); /* previous mode did not exceed bit-budget */
351 604 : BREAK;
352 : }
353 : }
354 :
355 604 : if ( GT_16( i_mult2( pulseconfigbits[k], nb_subfr ), bits_subframe2 ) )
356 : {
357 0 : k = sub( k, 1 ); /* previous mode did not exceed bit-budget */
358 : }
359 :
360 604 : move16();
361 604 : fixed_cdk_index[0] = k;
362 604 : bitsused = i_mult2( pulseconfigbits[k], nb_subfr );
363 :
364 3020 : FOR( sfr = 1; sfr < nb_subfr; sfr++ )
365 : {
366 : /*bits_currsubframe = (int)(((float)sfr+1.0f)*bits_subframe) - bitsused;*/
367 2416 : bits_currsubframe = sub( add( i_mult2( sfr, bits_subframe2 ), bits_subframe2 ), bitsused );
368 :
369 : /* try increasing mode while below threshold */
370 2416 : test();
371 2808 : WHILE( ( ( k < sub( pulseconfig_size, 1 ) ) ) && ( ( i_mult2( pulseconfigbits[k + 1], nb_subfr ) <= bits_currsubframe ) ) )
372 : {
373 392 : test();
374 392 : k = add( k, 1 );
375 : }
376 :
377 : /* try decreasing mode until below threshold */
378 2679 : WHILE( ( i_mult2( pulseconfigbits[k], nb_subfr ) > bits_currsubframe ) )
379 : {
380 263 : k = sub( k, 1 );
381 :
382 263 : IF( k == 0 )
383 : {
384 0 : BREAK;
385 : }
386 : }
387 :
388 : /* store mode */
389 2416 : move16();
390 2416 : fixed_cdk_index[sfr] = k;
391 2416 : bitsused = add( bitsused, i_mult2( pulseconfigbits[k], nb_subfr ) );
392 : }
393 :
394 604 : return mult_r( bitsused, inb_subfr );
395 : }
396 :
397 604 : static Word16 BITS_ALLOC_adjust_acelp_fixed_cdk(
398 : const Word16 bits_frame, /*i: bit budget*/
399 : Word16 *fixed_cdk_index,
400 : const Word16 nb_subfr )
401 : {
402 : Word16 bitsused;
403 :
404 :
405 604 : bitsused = BITS_ALLOC_adjust_generic( bits_frame, fixed_cdk_index, nb_subfr, ACELP_CDK_BITS, ACELP_FIXED_CDK_NB );
406 :
407 :
408 604 : return bitsused;
409 : }
410 :
411 :
412 : /*-------------------------------------------------------------------*
413 : * fcb_table()
414 : *
415 : * Selection of fixed innovation codebook bitbudget table
416 : *--------------------------------------------------------------------*/
417 :
418 2231980 : static Word16 fcb_table(
419 : const Word16 n,
420 : const Word16 L_subfr )
421 : {
422 : Word16 out;
423 : // PMT("Not floating point computation, but fixed point operator are still missing ")
424 :
425 2231980 : out = PulseConfTable[n].bits;
426 2231980 : move16();
427 2231980 : if ( GT_16( L_subfr, L_SUBFR ) )
428 : {
429 0 : out = fast_FCB_bits_2sfr[n];
430 0 : move16();
431 : }
432 :
433 2231980 : return ( out );
434 : }
435 :
436 : /*-------------------------------------------------------------------*
437 : * acelp_FCB_allocator_fx()
438 : *
439 : * Routine to allocate fixed innovation codebook bit-budget
440 : *--------------------------------------------------------------------*/
441 :
442 112308 : static ivas_error acelp_FCB_allocator_fx(
443 : Word16 *nBits, /* i/o: available bit-budget */
444 : Word16 fixed_cdk_index[], /* o : codebook index Q0 */
445 : Word16 nb_subfr, /* i : number of subframes */
446 : const Word16 L_subfr, /* i : subframe length */
447 : const Word16 coder_type, /* i : coder type */
448 : const Word16 tc_subfr, /* i : TC subframe index */
449 : const Word16 fix_first /* i : flag to indicate whether the first subframe bit-budget was fixed */
450 : )
451 : {
452 : Word16 cdbk, sfr, step;
453 : Word16 nBits_tmp;
454 : Word16 *p_fixed_cdk_index;
455 : Word16 max_n;
456 : ivas_error error;
457 : // PMT("Not floating point computation, but fixed point operator are still missing ")
458 :
459 112308 : error = IVAS_ERR_OK;
460 112308 : move32();
461 :
462 112308 : cdbk = coder_type; /* just to avoid warning when DEBUGGING is deactivated */
463 112308 : move16();
464 :
465 112308 : p_fixed_cdk_index = fixed_cdk_index;
466 :
467 : /* TRANSITION coding: first subframe bit-budget was already fixed, glottal pulse not in the first subframe */
468 112308 : test();
469 112308 : IF( GE_16( tc_subfr, L_SUBFR ) && fix_first )
470 : {
471 : Word16 i;
472 :
473 0 : FOR( i = 0; i < nb_subfr; i++ )
474 : {
475 0 : *nBits = sub( *nBits, ACELP_FIXED_CDK_BITS( fixed_cdk_index[i] ) );
476 0 : move16();
477 : }
478 0 : return error;
479 : }
480 :
481 : /* TRANSITION coding: first subframe bit-budget was already fixed, glottal pulse in the first subframe */
482 112308 : sfr = 0;
483 112308 : move16();
484 112308 : IF( fix_first )
485 : {
486 3620 : *nBits = sub( *nBits, ACELP_FIXED_CDK_BITS( fixed_cdk_index[0] ) );
487 3620 : move16();
488 3620 : sfr = 1;
489 3620 : move16();
490 3620 : p_fixed_cdk_index++;
491 3620 : nb_subfr = 3;
492 3620 : move16();
493 : }
494 :
495 : /* distribute the bit-budget equally between subframes */
496 112308 : IF( GT_16( L_subfr, L_SUBFR ) ) /* access fast_FCB_bits_2sfr */
497 : {
498 0 : max_n = 6;
499 0 : move16();
500 : }
501 : ELSE
502 : {
503 112308 : max_n = ACELP_FIXED_CDK_NB;
504 112308 : move16();
505 : }
506 1782748 : FOR( cdbk = 0; cdbk < max_n; cdbk++ )
507 : {
508 1782748 : IF( GT_32( L_mult0( fcb_table( cdbk, L_subfr ), nb_subfr ), L_deposit_l( *nBits ) ) )
509 : {
510 112308 : BREAK;
511 : }
512 : }
513 112308 : cdbk = sub( cdbk, 1 );
514 :
515 112308 : set16_fx( p_fixed_cdk_index, cdbk, nb_subfr );
516 112308 : nBits_tmp = 0;
517 112308 : move16();
518 112308 : IF( cdbk >= 0 )
519 : {
520 112308 : nBits_tmp = fcb_table( cdbk, L_subfr );
521 : }
522 : ELSE
523 : {
524 0 : nBits_tmp = 0;
525 0 : move16();
526 : }
527 112308 : *nBits = sub( *nBits, i_mult( nBits_tmp, nb_subfr ) );
528 112308 : move16();
529 :
530 : /* try to increase the FCB bit-budget of the first subframe(s) */
531 112308 : IF( LT_16( cdbk, ACELP_FIXED_CDK_NB - 1 ) )
532 : {
533 112308 : step = sub( fcb_table( add( cdbk, 1 ), L_subfr ), nBits_tmp );
534 302103 : WHILE( *nBits >= step )
535 : {
536 189795 : ( *p_fixed_cdk_index )++;
537 189795 : *nBits = sub( *nBits, step );
538 189795 : move16();
539 189795 : p_fixed_cdk_index++;
540 : }
541 :
542 : /* try to increase the FCB of the first subframe in cases when the next step is lower than the current step */
543 112308 : IF( LT_16( fixed_cdk_index[sfr], ACELP_FIXED_CDK_NB - 1 ) )
544 : {
545 112308 : step = sub( fcb_table( add( fixed_cdk_index[sfr], 1 ), L_subfr ), fcb_table( fixed_cdk_index[sfr], L_subfr ) );
546 112308 : test();
547 112308 : IF( GE_16( *nBits, step ) && cdbk >= 0 )
548 : {
549 17193 : fixed_cdk_index[sfr] = add( fixed_cdk_index[sfr], 1 );
550 17193 : move16();
551 17193 : *nBits = sub( *nBits, step );
552 17193 : move16();
553 17193 : test();
554 17193 : IF( GE_16( *nBits, step ) && EQ_16( fixed_cdk_index[sfr + 1], sub( fixed_cdk_index[sfr], 1 ) ) )
555 : {
556 1512 : sfr = add( sfr, 1 );
557 1512 : fixed_cdk_index[sfr] = add( fixed_cdk_index[sfr], 1 );
558 1512 : move16();
559 1512 : *nBits = sub( *nBits, step );
560 1512 : move16();
561 : }
562 : }
563 : }
564 : }
565 : /* TRANSITION coding: allocate highest FCBQ bit-budget to the subframe with the glottal-shape codebook */
566 112308 : IF( GE_16( tc_subfr, L_SUBFR ) )
567 : {
568 : Word16 tempr;
569 :
570 6035 : SWAP( fixed_cdk_index[0], fixed_cdk_index[tc_subfr / L_SUBFR] );
571 :
572 : /* TRANSITION coding: allocate second highest FCBQ bit-budget to the last subframe */
573 6035 : IF( LT_16( idiv1616( tc_subfr, L_SUBFR ), sub( nb_subfr, 1 ) ) )
574 : {
575 2944 : SWAP( fixed_cdk_index[sub( tc_subfr, L_SUBFR ) / L_SUBFR], fixed_cdk_index[nb_subfr - 1] );
576 : }
577 : }
578 :
579 : /* when subframe length > L_SUBFR, number of bits instead of codebook index is signalled */
580 112308 : IF( GT_16( L_subfr, L_SUBFR ) )
581 : {
582 : Word16 i, j;
583 0 : FOR( i = 0; i < nb_subfr; i++ )
584 : {
585 0 : j = fixed_cdk_index[i];
586 0 : move16();
587 0 : fixed_cdk_index[i] = fast_FCB_bits_2sfr[j];
588 0 : move16();
589 : }
590 : }
591 :
592 112308 : return error;
593 : }
594 :
595 :
596 : /*-------------------------------------------------------------------*
597 : * config_acelp1_fx()
598 : *
599 : * Configure ACELP bit allocation
600 : * - should be in range of <6700; 24350> for ACELP@12.8kHz
601 : * - per channel bitrate minimum is 13250 kbps for ACELP@16kHz
602 : *--------------------------------------------------------------------*/
603 :
604 160079 : ivas_error config_acelp1_fx(
605 : const Word16 enc_dec, /* i : encoder/decoder flag */
606 : const Word32 total_brate, /* i : total bitrate */
607 : const Word32 core_brate_inp, /* i : core bitrate */
608 : const Word16 core, /* i : core */
609 : const Word16 extl, /* i : extension layer */
610 : const Word32 extl_brate, /* i : extension layer bitrate */
611 : const Word16 L_frame, /* i : frame length at internal Fs */
612 : const Word16 GSC_noisy_speech, /* i : GSC on SWB noisy speech flag */
613 : ACELP_config *acelp_cfg, /* i : ACELP bit-allocation */
614 : const Word16 signaling_bits, /* i : number of signaling bits */
615 : const Word16 coder_type, /* i : coder type */
616 : const Word16 inactive_coder_type_flag, /* i : AVQ (0) or GSC (1) IC flag */
617 : const Word16 tc_subfr, /* i : TC subfr ID */
618 : const Word16 tc_call, /* i : TC call number (0,1,2,3,5(DEC)) */
619 : Word16 *nBits_es_Pred, /* o : number of bits for Es_pred Q */
620 : Word16 *unbits, /* o : number of unused bits */
621 : const Word16 element_mode, /* i : element mode */
622 : Word16 *uc_two_stage_flag, /* o : flag undicating two-stage UC */
623 : const Word16 tdm_lp_reuse_flag, /* i : LPC reuse flag (can be 1 only with secondary channel */
624 : const Word16 tdm_low_rate_mode, /* i : secondary channel low rate mode flag */
625 : const Word16 idchan, /* i : stereo channel ID */
626 : #ifndef NONBE_1325_TD_STEREO_QUANT_LSF_SEC
627 : const Word16 active_cnt, /* i : Active frame counter */
628 : #endif
629 : const Word16 tdm_Pitch_reuse_flag, /* i : primary channel pitch reuse flag*/
630 : const Word16 tdm_LRTD_flag, /* i : LRTD stereo mode flag */
631 : const Word16 GSC_IVAS_mode /* i : GSC IVAS mode */
632 : )
633 : {
634 : Word16 i, bits, nb_subfr;
635 : Word16 flag_hardcoded, coder_type_sw, fix_first;
636 : Word32 core_brate;
637 : ivas_error error;
638 :
639 160079 : error = IVAS_ERR_OK;
640 160079 : move32();
641 :
642 : /*-----------------------------------------------------------------*
643 : * Set the flag indicating two-stage Unvoiced (UC) frame
644 : *-----------------------------------------------------------------*/
645 :
646 160079 : *uc_two_stage_flag = 0;
647 160079 : move16();
648 :
649 160079 : IF( EQ_16( coder_type, UNVOICED ) )
650 : {
651 1397 : test();
652 1397 : test();
653 1397 : test();
654 1397 : test();
655 1397 : test();
656 1397 : IF( GE_32( total_brate, MIN_UNVOICED_TWO_STAGE_BRATE ) && element_mode > EVS_MONO && ( idchan == 0 || ( ( GE_32( total_brate, 8500 ) || extl_brate == 0 ) && EQ_16( tdm_LRTD_flag, 1 ) ) ) )
657 : {
658 1372 : *uc_two_stage_flag = 1;
659 1372 : move16();
660 : }
661 : }
662 :
663 : /*-----------------------------------------------------------------*
664 : * Set the number of subframes
665 : *-----------------------------------------------------------------*/
666 :
667 160079 : IF( EQ_16( L_frame, L_FRAME ) )
668 : {
669 88767 : nb_subfr = NB_SUBFR;
670 88767 : move16();
671 : }
672 : ELSE /* L_frame == L_FRAME16k */
673 : {
674 71312 : nb_subfr = NB_SUBFR16k;
675 71312 : move16();
676 : }
677 :
678 160079 : coder_type_sw = coder_type;
679 160079 : move16();
680 160079 : IF( core != ACELP_CORE )
681 : {
682 : /* used in acelp_core_switch_enc() */
683 25 : nb_subfr = 1;
684 25 : move16();
685 25 : if ( EQ_16( L_frame, L_FRAME ) )
686 : {
687 5 : coder_type_sw = TRANSITION;
688 5 : move16();
689 : }
690 : }
691 :
692 : /*-----------------------------------------------------------------*
693 : * Check if the core_brate is hard coded (to keep BE for mono core) or not
694 : *-----------------------------------------------------------------*/
695 :
696 160079 : flag_hardcoded = 0;
697 160079 : move16();
698 160079 : i = 0;
699 160079 : move16();
700 :
701 1490935 : WHILE( i < SIZE_BRATE_INTERMED_TBL )
702 : {
703 1490935 : IF( EQ_32( core_brate_inp, brate_intermed_tbl[i] ) )
704 : {
705 7025 : flag_hardcoded = 1;
706 7025 : move16();
707 7025 : BREAK;
708 : }
709 :
710 1483910 : IF( LT_32( core_brate_inp, brate_intermed_tbl[i] ) )
711 : {
712 153054 : flag_hardcoded = 0;
713 153054 : move16();
714 153054 : BREAK;
715 : }
716 :
717 1330856 : i = add( i, 1 );
718 : }
719 :
720 160079 : test();
721 160079 : test();
722 160079 : test();
723 160109 : if ( EQ_16( element_mode, IVAS_CPE_TD ) && EQ_16( coder_type, AUDIO ) &&
724 60 : LE_32( core_brate_inp, STEREO_GSC_BIT_RATE_ALLOC ) && EQ_32( brate_intermed_tbl[i], ACELP_9k60 ) ) /* Bit allocation should be mapped to 8 kb/s instead of 9.6 kb/s in this case */
725 : {
726 4 : i = sub( i, 1 );
727 : }
728 :
729 160079 : core_brate = brate_intermed_tbl[i];
730 160079 : move32();
731 :
732 160079 : if ( element_mode > EVS_MONO )
733 : {
734 158586 : flag_hardcoded = 0; /* use automatic and flexible ACELP bit-budget allocation */
735 158586 : move16();
736 : }
737 :
738 160079 : test();
739 160079 : if ( core != ACELP_CORE && element_mode == EVS_MONO ) /* needed for mode1 core switching in EVS mono */
740 : {
741 25 : flag_hardcoded = 1;
742 25 : move16();
743 : }
744 :
745 : /*-----------------------------------------------------------------*
746 : * ACELP bit allocation
747 : *-----------------------------------------------------------------*/
748 :
749 160079 : test();
750 160079 : test();
751 160079 : IF( !( EQ_16( coder_type, TRANSITION ) && NE_16( tc_subfr, -1 ) ) || EQ_16( enc_dec, DEC ) )
752 : {
753 : /* Set the bit-budget */
754 160079 : bits = divide3216( core_brate_inp, FRAMES_PER_SEC / 2 );
755 :
756 160079 : test();
757 160079 : test();
758 160079 : IF( EQ_16( coder_type, TRANSITION ) && EQ_16( enc_dec, DEC ) && EQ_16( tc_call, 1 ) )
759 : {
760 13093 : bits = add( bits, *nBits_es_Pred ); /* equalize for 4th signaling bit estimated at the encoder in TC_0_192 */
761 : }
762 :
763 160079 : test();
764 160079 : test();
765 : /* Subtract signaling bits */
766 160079 : IF( EQ_16( enc_dec, DEC ) && EQ_16( idchan, 1 ) && element_mode > EVS_MONO )
767 : {
768 3630 : bits = sub( bits, TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS );
769 :
770 3630 : IF( EQ_16( tdm_LRTD_flag, 1 ) )
771 : {
772 3549 : bits = add( bits, STEREO_BITS_TCA );
773 : }
774 :
775 3630 : test();
776 3630 : test();
777 3630 : test();
778 3630 : test();
779 3630 : test();
780 3630 : test();
781 : /* subtract TBE/BWE flag */
782 3630 : IF( extl_brate > 0 && ( EQ_16( extl, WB_TBE ) || EQ_16( extl, SWB_TBE ) || EQ_16( extl, FB_TBE ) || EQ_16( extl, WB_BWE ) || EQ_16( extl, SWB_BWE ) || EQ_16( extl, FB_BWE ) ) )
783 : {
784 2773 : bits = sub( bits, 1 );
785 : }
786 : }
787 : ELSE
788 : {
789 : /* Subtract signaling bits */
790 156449 : bits = sub( bits, signaling_bits );
791 : }
792 :
793 160079 : test();
794 160079 : test();
795 160079 : test();
796 160079 : test();
797 160079 : test();
798 160079 : test();
799 160079 : IF( extl_brate > 0 && ( EQ_16( extl, WB_TBE ) || EQ_16( extl, SWB_TBE ) || EQ_16( extl, FB_TBE ) || EQ_16( extl, WB_BWE ) || EQ_16( extl, SWB_BWE ) || EQ_16( extl, FB_BWE ) ) )
800 : {
801 : /* extension layer signaling bit is counted in the extension layer bitbudget */
802 143247 : bits = add( bits, 1 );
803 : }
804 :
805 : /*-----------------------------------------------------------------*
806 : * LSF Q bit-budget
807 : *-----------------------------------------------------------------*/
808 :
809 160079 : test();
810 160079 : test();
811 : #ifndef NONBE_1325_TD_STEREO_QUANT_LSF_SEC
812 : test();
813 : #endif
814 160079 : IF( !tdm_lp_reuse_flag || idchan == 0 )
815 : {
816 : /* LSF Q bit-budget */
817 159938 : acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
818 159938 : move16();
819 :
820 159938 : IF( !flag_hardcoded )
821 : {
822 158445 : IF( EQ_16( L_frame, L_FRAME ) )
823 : {
824 87822 : test();
825 87822 : test();
826 87822 : test();
827 87822 : test();
828 87822 : test();
829 87822 : test();
830 87822 : test();
831 87822 : IF( EQ_16( element_mode, IVAS_SCE ) && tdm_low_rate_mode )
832 : {
833 6725 : acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
834 6725 : move16();
835 : }
836 81097 : ELSE IF( ( LT_32( total_brate, 7200 ) || coder_type == INACTIVE || EQ_16( coder_type, AUDIO ) ) && EQ_16( idchan, 1 ) )
837 : {
838 : /* TD stereo, secondary channel: do nothing */
839 50 : acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
840 50 : move16();
841 : }
842 81047 : ELSE IF( element_mode > EVS_MONO && EQ_16( coder_type, AUDIO ) && LT_32( brate_intermed_tbl[i], ACELP_9k60 ) )
843 : {
844 : /* primary channel: do nothing */
845 : }
846 70699 : ELSE IF( element_mode > EVS_MONO && EQ_16( coder_type, AUDIO ) /*&& brate_intermed_tbl[i] >= ACELP_9k60*/ )
847 : {
848 9226 : acelp_cfg->lsf_bits = 42;
849 9226 : move16();
850 : }
851 61473 : ELSE IF( LE_32( total_brate, 9600 ) || EQ_16( coder_type, UNVOICED ) )
852 : {
853 8565 : acelp_cfg->lsf_bits = 31;
854 8565 : move16();
855 : }
856 52908 : ELSE IF( LE_32( total_brate, 20000 ) )
857 : {
858 51527 : acelp_cfg->lsf_bits = 36;
859 51527 : move16();
860 : }
861 : ELSE
862 : {
863 1381 : acelp_cfg->lsf_bits = 41;
864 1381 : move16();
865 : }
866 : }
867 : ELSE /* L_frame == L_FRAME16k */
868 : {
869 70623 : acelp_cfg->lsf_bits = 41;
870 70623 : move16();
871 : }
872 : }
873 :
874 159938 : bits = sub( bits, acelp_cfg->lsf_bits );
875 :
876 : /* mid-LSF Q bit-budget */
877 159938 : acelp_cfg->mid_lsf_bits = mid_LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
878 159938 : move16();
879 :
880 159938 : test();
881 159938 : if ( element_mode > EVS_MONO && EQ_16( coder_type, AUDIO ) /*&& brate_intermed_tbl[i] < ACELP_9k60*/ )
882 : {
883 19574 : acelp_cfg->mid_lsf_bits = 5;
884 19574 : move16();
885 : /* primary channel: do nothing */
886 : }
887 :
888 159938 : bits = sub( bits, acelp_cfg->mid_lsf_bits );
889 : }
890 : #ifdef NONBE_1325_TD_STEREO_QUANT_LSF_SEC
891 141 : ELSE IF( EQ_16( tdm_lp_reuse_flag, 1 ) && EQ_16( idchan, 1 ) )
892 : #else
893 : ELSE IF( EQ_16( tdm_lp_reuse_flag, 1 ) && EQ_16( idchan, 1 ) && NE_16( active_cnt, 1 ) )
894 : #endif
895 : {
896 141 : bits = sub( bits, TDM_IC_LSF_PRED_BITS );
897 : }
898 :
899 160079 : test();
900 160079 : test();
901 160079 : test();
902 160079 : test();
903 160079 : test();
904 160079 : test();
905 : /* gain Q bit-budget - part 1: 'Es_pred' of memory-less gain Q */
906 160079 : IF( ( NE_16( coder_type, UNVOICED ) && NE_16( coder_type, AUDIO ) && coder_type != INACTIVE && !( LE_32( core_brate, ACELP_8k00 ) && NE_16( coder_type, TRANSITION ) ) ) /* mid bitrates in GC and VC, low+mid bitrates in TC */ ||
907 : ( coder_type == INACTIVE && !inactive_coder_type_flag ) /* AVQ inactive */ )
908 : {
909 121059 : *nBits_es_Pred = Es_pred_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, -1, -1 )];
910 121059 : move16();
911 121059 : bits = sub( bits, *nBits_es_Pred );
912 : }
913 39020 : ELSE IF( *uc_two_stage_flag )
914 : {
915 1372 : *nBits_es_Pred = 4;
916 1372 : move16();
917 1372 : bits = sub( bits, *nBits_es_Pred );
918 : }
919 : }
920 : ELSE
921 : {
922 0 : bits = *unbits;
923 0 : move16();
924 : }
925 :
926 160079 : test();
927 160079 : IF( EQ_16( coder_type, TRANSITION ) && EQ_16( tc_call, 0 ) )
928 : {
929 0 : *unbits = bits;
930 0 : move16();
931 0 : return error;
932 : }
933 :
934 : /*-----------------------------------------------------------------*
935 : * Low-rate mode - bits are allocated in tdm_low_rate_enc()
936 : *-----------------------------------------------------------------*/
937 :
938 160079 : test();
939 160079 : IF( EQ_16( element_mode, IVAS_SCE ) && tdm_low_rate_mode )
940 : {
941 6725 : acelp_cfg->FEC_mode = 0;
942 6725 : move16();
943 6725 : acelp_cfg->ltf_mode = FULL_BAND;
944 6725 : move16();
945 6725 : *nBits_es_Pred = 0;
946 6725 : move16();
947 6725 : *unbits = 0;
948 6725 : move16();
949 6725 : acelp_cfg->ubits = 0;
950 6725 : move16();
951 :
952 6725 : return error;
953 : }
954 :
955 : /*-----------------------------------------------------------------*
956 : * Supplementary information for FEC
957 : *-----------------------------------------------------------------*/
958 :
959 153354 : acelp_cfg->FEC_mode = 0;
960 153354 : move16();
961 153354 : test();
962 153354 : test();
963 153354 : IF( GE_32( core_brate, ACELP_11k60 ) && ( idchan == 0 || element_mode == EVS_MONO ) )
964 : {
965 121193 : acelp_cfg->FEC_mode = 1;
966 121193 : move16();
967 :
968 121193 : test();
969 121193 : test();
970 121193 : if ( GT_16( coder_type, UNVOICED ) && LT_16( coder_type, AUDIO ) && NE_16( coder_type, VOICED ) )
971 : {
972 101302 : bits = sub( bits, FEC_BITS_CLS );
973 : }
974 :
975 121193 : IF( NE_16( coder_type, TRANSITION ) )
976 : {
977 106557 : IF( GE_32( total_brate, ACELP_16k40 ) )
978 : {
979 67032 : acelp_cfg->FEC_mode = 2;
980 67032 : move16();
981 :
982 67032 : test();
983 67032 : IF( GT_16( coder_type, UNVOICED ) && LT_16( coder_type, AUDIO ) )
984 : {
985 59281 : bits = sub( bits, FEC_BITS_ENR );
986 : }
987 : }
988 :
989 106557 : IF( GE_32( total_brate, ACELP_32k ) )
990 : {
991 10612 : acelp_cfg->FEC_mode = 3;
992 10612 : move16();
993 :
994 10612 : test();
995 10612 : IF( GT_16( coder_type, UNVOICED ) && LT_16( coder_type, AUDIO ) )
996 : {
997 9272 : bits = sub( bits, FEC_BITS_POS );
998 : }
999 : }
1000 : }
1001 : }
1002 :
1003 : /*-----------------------------------------------------------------*
1004 : * LP filtering of the adaptive excitation
1005 : *-----------------------------------------------------------------*/
1006 :
1007 153354 : test();
1008 153354 : test();
1009 153354 : test();
1010 153354 : test();
1011 153354 : test();
1012 153354 : test();
1013 153354 : IF( idchan > 0 && element_mode > EVS_MONO )
1014 : {
1015 3630 : acelp_cfg->ltf_mode = FULL_BAND;
1016 3630 : move16();
1017 : }
1018 149724 : ELSE IF( EQ_16( coder_type, UNVOICED ) )
1019 : {
1020 1303 : acelp_cfg->ltf_mode = FULL_BAND;
1021 1303 : move16();
1022 : }
1023 148421 : ELSE IF( ( EQ_16( coder_type, GENERIC ) || EQ_16( coder_type, TRANSITION ) ) && LT_32( core_brate, ACELP_11k60 ) )
1024 : {
1025 10046 : acelp_cfg->ltf_mode = LOW_PASS;
1026 10046 : move16();
1027 : }
1028 138375 : ELSE IF( GE_32( core_brate, ACELP_11k60 ) && ( NE_16( coder_type, AUDIO ) && !( coder_type == INACTIVE && EQ_16( L_frame, L_FRAME ) ) ) )
1029 : {
1030 112522 : test();
1031 112522 : test();
1032 112522 : IF( coder_type == INACTIVE && EQ_16( L_frame, L_FRAME16k ) && inactive_coder_type_flag ) /* GSC Inactive @16kHz */
1033 : {
1034 5066 : acelp_cfg->ltf_mode = FULL_BAND;
1035 5066 : move16();
1036 : }
1037 : ELSE
1038 : {
1039 107456 : acelp_cfg->ltf_mode = NORMAL_OPERATION;
1040 107456 : move16();
1041 107456 : if ( coder_type != TRANSITION )
1042 : {
1043 92820 : bits = sub( bits, nb_subfr );
1044 : }
1045 : }
1046 : }
1047 : ELSE
1048 : {
1049 25853 : acelp_cfg->ltf_mode = FULL_BAND;
1050 25853 : move16();
1051 : }
1052 :
1053 : /*-----------------------------------------------------------------*
1054 : * UC bit-budget
1055 : *-----------------------------------------------------------------*/
1056 :
1057 153354 : test();
1058 153354 : test();
1059 153354 : test();
1060 153354 : test();
1061 153354 : test();
1062 153354 : IF( ( ( EQ_16( coder_type, UNVOICED ) && !( *uc_two_stage_flag ) ) || ( coder_type == INACTIVE && LE_32( core_brate, ACELP_9k60 ) ) ) && ( idchan == 0 || element_mode == EVS_MONO ) )
1063 : {
1064 716 : bits = sub( bits, NBITS_NOISENESS ); /* noiseness */
1065 : }
1066 153354 : test();
1067 153354 : IF( EQ_16( coder_type, UNVOICED ) && !( *uc_two_stage_flag ) )
1068 : {
1069 25 : bits = sub( bits, ( 3 * NB_SUBFR ) ); /* tilt factor */
1070 : }
1071 :
1072 : /*-----------------------------------------------------------------*
1073 : * TC bit-budget
1074 : *-----------------------------------------------------------------*/
1075 :
1076 153354 : fix_first = 0;
1077 153354 : move16();
1078 153354 : IF( EQ_16( coder_type, TRANSITION ) )
1079 : {
1080 16765 : IF( EQ_16( tc_call, 2 ) )
1081 : {
1082 3672 : fix_first = 1;
1083 3672 : move16();
1084 : }
1085 :
1086 : /* TC signaling */
1087 16765 : IF( EQ_16( L_frame, L_FRAME ) )
1088 : {
1089 10350 : IF( EQ_16( tc_subfr, TC_0_0 ) )
1090 : {
1091 1614 : IF( enc_dec == ENC )
1092 : {
1093 0 : bits = sub( bits, 1 ); /* TC signaling */
1094 : }
1095 :
1096 1614 : IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) )
1097 : {
1098 1249 : bits = sub( bits, 3 ); /* LP filtering flag */
1099 : }
1100 : }
1101 8736 : ELSE IF( EQ_16( tc_subfr, TC_0_64 ) )
1102 : {
1103 1213 : IF( enc_dec == ENC )
1104 : {
1105 0 : bits = sub( bits, 4 ); /* TC signaling */
1106 : }
1107 :
1108 1213 : IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) )
1109 : {
1110 949 : bits = sub( bits, 3 ); /* LP filtering flag */
1111 : }
1112 : }
1113 7523 : ELSE IF( EQ_16( tc_subfr, TC_0_128 ) )
1114 : {
1115 644 : IF( enc_dec == ENC )
1116 : {
1117 0 : bits = sub( bits, 4 ); /* TC signaling */
1118 : }
1119 :
1120 644 : IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) )
1121 : {
1122 549 : bits = sub( bits, 2 ); /* LP filtering flag */
1123 : }
1124 : }
1125 6879 : ELSE IF( EQ_16( tc_subfr, TC_0_192 ) )
1126 : {
1127 201 : IF( enc_dec == ENC )
1128 : {
1129 0 : bits = sub( bits, 3 ); /* TC signaling */
1130 : }
1131 :
1132 201 : IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) )
1133 : {
1134 172 : bits = sub( bits, 1 ); /* LP filtering flag */
1135 : }
1136 : }
1137 6678 : ELSE IF( EQ_16( tc_subfr, L_SUBFR ) )
1138 : {
1139 1019 : IF( enc_dec == ENC )
1140 : {
1141 0 : bits = sub( bits, 3 ); /* TC signaling */
1142 : }
1143 :
1144 1019 : test();
1145 1019 : IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) && NE_16( tc_subfr, 192 ) )
1146 : {
1147 797 : bits = sub( bits, idiv1616( sub( L_FRAME - L_SUBFR, tc_subfr ), L_SUBFR ) ); /* LP filtering flag */
1148 : }
1149 : }
1150 : ELSE
1151 : {
1152 5659 : IF( enc_dec == ENC )
1153 : {
1154 0 : bits = sub( bits, 4 ); /* TC signaling */
1155 : }
1156 :
1157 5659 : test();
1158 5659 : IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) && NE_16( tc_subfr, 192 ) )
1159 : {
1160 3265 : bits = sub( bits, idiv1616( sub( L_FRAME - L_SUBFR, tc_subfr ), L_SUBFR ) ); /* LP filtering flag */
1161 : }
1162 : }
1163 : }
1164 : ELSE /* L_frame == L_FRAME16k */
1165 : {
1166 6415 : IF( enc_dec == ENC )
1167 : {
1168 0 : IF( LE_16( tc_subfr, 2 * L_SUBFR ) )
1169 : {
1170 0 : bits = sub( bits, 2 ); /* TC signaling */
1171 : }
1172 : ELSE
1173 : {
1174 0 : bits = sub( bits, 3 ); /* TC signaling */
1175 : }
1176 : }
1177 :
1178 6415 : IF( NE_16( tc_subfr, 256 ) )
1179 : {
1180 4516 : bits = sub( bits, idiv1616( sub( L_FRAME16k - L_SUBFR, tc_subfr ), L_SUBFR ) ); /* LP filtering flag */
1181 : }
1182 : }
1183 :
1184 : /* glottal-shape codebook bits */
1185 16765 : bits = sub( bits, ( 3 + 6 + 1 + 3 ) );
1186 : }
1187 :
1188 : /*-----------------------------------------------------------------*
1189 : * pitch, innovation, gains bit-budget
1190 : *-----------------------------------------------------------------*/
1191 :
1192 153354 : acelp_cfg->fcb_mode = 0;
1193 153354 : move16();
1194 :
1195 153354 : test();
1196 153354 : test();
1197 153354 : test();
1198 153354 : test();
1199 153354 : test();
1200 153354 : test();
1201 153354 : test();
1202 153354 : test();
1203 153354 : test();
1204 153354 : test();
1205 153354 : test();
1206 153354 : test();
1207 153354 : test();
1208 153354 : test();
1209 153354 : test();
1210 153354 : test();
1211 153354 : IF( EQ_16( element_mode, IVAS_CPE_TD ) && EQ_16( tdm_low_rate_mode, 1 ) && coder_type != INACTIVE && NE_16( coder_type, UNVOICED ) ) /* GENERIC low rate mode for secondary channel */
1212 : {
1213 0 : set16_fx( acelp_cfg->pitch_bits, 0, NB_SUBFR16k );
1214 0 : set16_fx( acelp_cfg->gains_mode, 0, NB_SUBFR16k );
1215 :
1216 0 : FOR( i = 0; i < 2; i++ )
1217 : {
1218 0 : acelp_cfg->pitch_bits[i] = 0;
1219 0 : move16();
1220 0 : IF( tdm_Pitch_reuse_flag == 0 )
1221 : {
1222 0 : acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, GENERIC, 2 * i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
1223 0 : move16();
1224 0 : bits = sub( bits, acelp_cfg->pitch_bits[i] );
1225 : }
1226 0 : acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, GENERIC, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
1227 0 : move16();
1228 0 : bits = sub( bits, acelp_cfg->gains_mode[i] );
1229 : }
1230 0 : acelp_cfg->fcb_mode = 1;
1231 0 : move16();
1232 :
1233 0 : IF( GE_16( bits, 16 ) )
1234 : {
1235 0 : acelp_FCB_allocator_fx( &bits, acelp_cfg->fixed_cdk_index, 2, 2 * L_SUBFR, GENERIC, -1, 0 );
1236 : }
1237 : ELSE
1238 : {
1239 0 : acelp_FCB_allocator_fx( &bits, acelp_cfg->fixed_cdk_index, 2, 2 * L_SUBFR, GENERIC, -1, 0 );
1240 0 : acelp_cfg->fixed_cdk_index[1] = -1;
1241 0 : move16();
1242 : }
1243 0 : acelp_cfg->fixed_cdk_index[2] = -1;
1244 0 : move16();
1245 0 : acelp_cfg->fixed_cdk_index[3] = -1;
1246 0 : move16();
1247 : }
1248 153354 : ELSE IF( ( coder_type != INACTIVE && EQ_16( nb_subfr, NB_SUBFR ) && NE_16( coder_type, AUDIO ) ) /* @12.8kHz core except of GSC */ ||
1249 : ( EQ_16( nb_subfr, NB_SUBFR16k ) && ( !inactive_coder_type_flag || coder_type != INACTIVE ) ) /* @16kHz core GC, TC, AVQ inactive */ ||
1250 : EQ_16( core, HQ_CORE ) /* ACELP -> HQ switching in EVS */ )
1251 : {
1252 : /* pitch Q & gain Q bit-budget - part 2*/
1253 694521 : FOR( i = 0; i < nb_subfr; i++ )
1254 : {
1255 568847 : IF( EQ_16( L_frame, L_FRAME ) )
1256 : {
1257 237697 : test();
1258 237697 : IF( EQ_16( tdm_Pitch_reuse_flag, 1 ) && EQ_16( idchan, 1 ) )
1259 : {
1260 128 : acelp_cfg->pitch_bits[i] = 0;
1261 128 : move16();
1262 : }
1263 : ELSE
1264 : {
1265 237569 : acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
1266 237569 : move16();
1267 : }
1268 237697 : acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type_sw, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
1269 237697 : move16();
1270 : }
1271 : ELSE /* L_frame == L_FRAME16k */
1272 : {
1273 331150 : test();
1274 331150 : IF( EQ_16( tdm_Pitch_reuse_flag, 1 ) && EQ_16( idchan, 1 ) )
1275 : {
1276 0 : acelp_cfg->pitch_bits[i] = 0;
1277 0 : move16();
1278 : }
1279 : ELSE
1280 : {
1281 331150 : acelp_cfg->pitch_bits[i] = ACB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( core_brate, coder_type, i_mult( i, L_SUBFR ), TC_SUBFR2IDX_16KHZ( tc_subfr ) )];
1282 331150 : move16();
1283 : }
1284 331150 : acelp_cfg->gains_mode[i] = gain_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( core_brate, coder_type_sw, i_mult( i, L_SUBFR ), TC_SUBFR2IDX_16KHZ( tc_subfr ) )];
1285 331150 : move16();
1286 : }
1287 :
1288 568847 : bits = sub( bits, acelp_cfg->pitch_bits[i] );
1289 :
1290 568847 : test();
1291 568847 : IF( coder_type == INACTIVE && EQ_16( acelp_cfg->gains_mode[i], 6 ) /* VQ vs. SQ threshold @32 kbps */ )
1292 : {
1293 3815 : bits = sub( bits, 5 );
1294 : }
1295 : ELSE
1296 : {
1297 565032 : IF( EQ_16( *uc_two_stage_flag, 1 ) )
1298 : {
1299 5488 : acelp_cfg->gains_mode[i] = 7;
1300 5488 : move16();
1301 : }
1302 :
1303 565032 : bits = sub( bits, acelp_cfg->gains_mode[i] );
1304 : }
1305 : }
1306 :
1307 125674 : test();
1308 125674 : test();
1309 125674 : test();
1310 125674 : test();
1311 125674 : test();
1312 125674 : test();
1313 : /* algebraic codebook bit-budget */
1314 125674 : IF( flag_hardcoded /* EVS */ ||
1315 : ( GE_32( core_brate_inp, MIN_BRATE_AVQ_EXC ) && coder_type != INACTIVE ) /* high-birate ACELP except IC */ ||
1316 : ( !inactive_coder_type_flag && coder_type == INACTIVE ) /* AVQ inactive */ )
1317 : {
1318 79153 : FOR( i = 0; i < nb_subfr; i++ )
1319 : {
1320 65813 : IF( EQ_16( L_frame, L_FRAME ) )
1321 : {
1322 3153 : acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, i_mult( i, L_SUBFR ), TC_SUBFR2IDX( tc_subfr ) )];
1323 3153 : move16();
1324 : }
1325 : ELSE /* L_frame == L_FRAME16k */
1326 : {
1327 62660 : acelp_cfg->fixed_cdk_index[i] = FCB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( core_brate, coder_type, i_mult( i, L_SUBFR ), TC_SUBFR2IDX_16KHZ( tc_subfr ) )];
1328 62660 : move16();
1329 : }
1330 65813 : bits = sub( bits, acelp_cfg->fixed_cdk_index[i] );
1331 : }
1332 : }
1333 112334 : ELSE IF( !( EQ_16( coder_type, UNVOICED ) && EQ_16( tdm_low_rate_mode, 1 ) && EQ_16( element_mode, IVAS_CPE_TD ) ) )
1334 : {
1335 112308 : test();
1336 112308 : IF( EQ_16( coder_type, UNVOICED ) && !( *uc_two_stage_flag ) )
1337 : {
1338 0 : i = idiv1616( bits, NB_SUBFR );
1339 0 : IF( s_and( i, 1 ) == 0 )
1340 : {
1341 0 : i = sub( i, 1 ); /* must be odd */
1342 : }
1343 0 : i = s_min( i, 13 );
1344 0 : i = s_max( i, 0 ); /* If i == 0-> random noise generator will be used as FCB */
1345 0 : set16_fx( acelp_cfg->fixed_cdk_index, i, NB_SUBFR );
1346 0 : bits = sub( bits, imult1616( i, NB_SUBFR ) );
1347 : }
1348 : ELSE
1349 : {
1350 112308 : acelp_cfg->fcb_mode = 1;
1351 112308 : move16();
1352 112308 : test();
1353 112308 : test();
1354 112308 : IF( EQ_16( element_mode, IVAS_CPE_TD ) )
1355 : {
1356 7181 : IF( GE_16( bits, imult1616( ACELP_FIXED_CDK_BITS( 0 ), nb_subfr ) ) ) /* enough bits for all fcb */
1357 : {
1358 7163 : acelp_FCB_allocator_fx( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
1359 : }
1360 18 : ELSE IF( GE_16( bits, imult1616( ACELP_FIXED_CDK_BITS( 0 ), sub( nb_subfr, 1 ) ) ) )
1361 : {
1362 0 : acelp_FCB_allocator_fx( &bits, acelp_cfg->fixed_cdk_index, sub( nb_subfr, 1 ), L_SUBFR, coder_type, tc_subfr, fix_first );
1363 0 : acelp_cfg->fixed_cdk_index[3] = -1;
1364 0 : move16();
1365 : }
1366 18 : ELSE IF( GE_32( bits, imult1616( ACELP_FIXED_CDK_BITS( 0 ), sub( nb_subfr, 2 ) ) ) )
1367 : {
1368 18 : acelp_FCB_allocator_fx( &bits, acelp_cfg->fixed_cdk_index, sub( nb_subfr, 2 ), L_SUBFR, coder_type, tc_subfr, fix_first );
1369 18 : acelp_cfg->fixed_cdk_index[2] = acelp_cfg->fixed_cdk_index[1];
1370 18 : move16();
1371 18 : acelp_cfg->fixed_cdk_index[1] = -1;
1372 18 : move16();
1373 18 : acelp_cfg->fixed_cdk_index[3] = -1;
1374 18 : move16();
1375 : }
1376 0 : ELSE IF( GE_32( bits, ACELP_FIXED_CDK_BITS( 0 ) ) )
1377 : {
1378 0 : acelp_FCB_allocator_fx( &bits, acelp_cfg->fixed_cdk_index, 1, L_SUBFR, coder_type, tc_subfr, fix_first );
1379 0 : acelp_cfg->fixed_cdk_index[1] = acelp_cfg->fixed_cdk_index[0];
1380 0 : move16();
1381 0 : acelp_cfg->fixed_cdk_index[0] = -1;
1382 0 : move16();
1383 0 : acelp_cfg->fixed_cdk_index[2] = -1;
1384 0 : move16();
1385 0 : acelp_cfg->fixed_cdk_index[3] = -1;
1386 0 : move16();
1387 : }
1388 : ELSE /* No FCB */
1389 : {
1390 0 : acelp_cfg->fixed_cdk_index[0] = -1;
1391 0 : move16();
1392 0 : acelp_cfg->fixed_cdk_index[1] = -1;
1393 0 : move16();
1394 0 : acelp_cfg->fixed_cdk_index[2] = -1;
1395 0 : move16();
1396 0 : acelp_cfg->fixed_cdk_index[3] = -1;
1397 0 : move16();
1398 : }
1399 : }
1400 105127 : ELSE IF( NE_16( element_mode, IVAS_CPE_TD ) && GSC_IVAS_mode > 0 && EQ_16( L_frame, L_FRAME16k ) )
1401 : {
1402 0 : bits = 100; /* 9 kbps for fcb */
1403 0 : move16();
1404 0 : acelp_FCB_allocator_fx( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
1405 : }
1406 : ELSE
1407 : {
1408 105127 : acelp_FCB_allocator_fx( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
1409 : }
1410 : }
1411 : }
1412 :
1413 125674 : test();
1414 125674 : test();
1415 125674 : test();
1416 : /* AVQ codebook */
1417 125674 : IF( ( GE_32( core_brate_inp, MIN_BRATE_AVQ_EXC ) && coder_type != INACTIVE ) /* high-birate ACELP except IC */ ||
1418 : ( !inactive_coder_type_flag && coder_type == INACTIVE ) /* AVQ inactive */ )
1419 : {
1420 75168 : FOR( i = 0; i < nb_subfr; i++ )
1421 : {
1422 62640 : IF( flag_hardcoded )
1423 : {
1424 3345 : acelp_cfg->AVQ_cdk_bits[i] = AVQ_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( core_brate, coder_type, i_mult( i, L_SUBFR ), TC_SUBFR2IDX_16KHZ( tc_subfr ) )];
1425 3345 : move16();
1426 : {
1427 3345 : bits = sub( bits, acelp_cfg->AVQ_cdk_bits[i] );
1428 : }
1429 : }
1430 :
1431 62640 : bits = sub( bits, G_AVQ_BITS );
1432 : }
1433 :
1434 12528 : test();
1435 12528 : test();
1436 12528 : IF( GE_32( core_brate_inp, MIN_BRATE_AVQ_EXC ) && LE_32( core_brate_inp, MAX_BRATE_AVQ_EXC_TD ) && EQ_16( coder_type, GENERIC ) )
1437 : {
1438 : /* harm. flag ACELP AVQ */
1439 6914 : bits = sub( bits, 1 );
1440 : }
1441 :
1442 12528 : IF( !flag_hardcoded )
1443 : {
1444 : Word16 bit_tmp;
1445 :
1446 11859 : bit_tmp = idiv1616( bits, nb_subfr );
1447 11859 : set16_fx( acelp_cfg->AVQ_cdk_bits, bit_tmp, nb_subfr );
1448 11859 : bits = sub( bits, imult1616( bit_tmp, nb_subfr ) );
1449 :
1450 11859 : bit_tmp = bits % nb_subfr;
1451 11859 : acelp_cfg->AVQ_cdk_bits[0] = add( acelp_cfg->AVQ_cdk_bits[0], bit_tmp );
1452 11859 : bits = sub( bits, bit_tmp );
1453 : }
1454 : }
1455 : }
1456 27680 : ELSE IF( ( EQ_16( coder_type, UNVOICED ) && EQ_16( tdm_low_rate_mode, 1 ) && EQ_16( element_mode, IVAS_CPE_TD ) ) /* LBR secondary channel in TD stereo */ ||
1457 : ( ( coder_type == INACTIVE || EQ_16( coder_type, AUDIO ) ) && EQ_16( nb_subfr, NB_SUBFR ) ) /* GSC @12.8kHz */ ||
1458 : ( coder_type == INACTIVE && inactive_coder_type_flag ) /* AVQ inactive */ )
1459 : {
1460 : Word32 Local_BR, Pitch_BR;
1461 : Word16 Pitch_CT;
1462 :
1463 : /* as defined at the beginning of [enc,dec]_pit_exc() */
1464 27680 : test();
1465 27680 : test();
1466 27680 : IF( GSC_IVAS_mode > 0 && ( GSC_noisy_speech || GT_32( core_brate, GSC_H_RATE_STG ) ) )
1467 : {
1468 139 : Local_BR = ACELP_8k00;
1469 139 : move32();
1470 139 : Pitch_CT = GENERIC;
1471 139 : move16();
1472 139 : Pitch_BR = ACELP_8k00;
1473 139 : move32();
1474 139 : IF( EQ_16( L_frame, L_FRAME16k ) )
1475 : {
1476 0 : Local_BR = ACELP_14k80;
1477 0 : move32();
1478 0 : test();
1479 0 : if ( GSC_IVAS_mode > 0 && LT_32( core_brate, IVAS_24k4 ) )
1480 : {
1481 0 : Local_BR = ACELP_9k60;
1482 0 : move32();
1483 : }
1484 0 : Pitch_BR = core_brate;
1485 0 : move32();
1486 : }
1487 : }
1488 27541 : ELSE IF( GSC_noisy_speech )
1489 : {
1490 695 : Local_BR = ACELP_7k20;
1491 695 : move32();
1492 695 : Pitch_CT = GENERIC;
1493 695 : move16();
1494 695 : Pitch_BR = ACELP_7k20;
1495 695 : move32();
1496 695 : if ( EQ_16( L_frame, L_FRAME16k ) )
1497 : {
1498 0 : Pitch_BR = core_brate;
1499 0 : move16();
1500 : }
1501 : }
1502 : ELSE
1503 : {
1504 26846 : Local_BR = ACELP_7k20;
1505 26846 : move32();
1506 26846 : Pitch_CT = AUDIO;
1507 26846 : move16();
1508 26846 : Pitch_BR = core_brate;
1509 26846 : move32();
1510 :
1511 26846 : IF( EQ_16( L_frame, L_FRAME16k ) )
1512 : {
1513 5066 : Local_BR = ACELP_13k20;
1514 5066 : move32();
1515 5066 : Pitch_CT = GENERIC;
1516 5066 : move16();
1517 : }
1518 : }
1519 :
1520 143466 : FOR( i = 0; i < nb_subfr; i++ )
1521 : {
1522 115786 : IF( EQ_16( L_frame, L_FRAME16k ) )
1523 : {
1524 25330 : acelp_cfg->pitch_bits[i] = ACB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( Pitch_BR, Pitch_CT, i_mult( i, L_SUBFR ), 0 )];
1525 25330 : move16();
1526 25330 : acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX( Local_BR, LOCAL_CT, i_mult( i, L_SUBFR ), 0 )];
1527 25330 : move16();
1528 : }
1529 : ELSE
1530 : {
1531 90456 : acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( Pitch_BR, Pitch_CT, i_mult( i, L_SUBFR ), 0 )];
1532 90456 : move16();
1533 90456 : acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX( Local_BR, LOCAL_CT, i_mult( i, L_SUBFR ), 0 )];
1534 90456 : move16();
1535 90456 : acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, LOCAL_CT, i_mult( i, L_SUBFR ), 0 )];
1536 90456 : move16();
1537 : }
1538 : }
1539 : }
1540 :
1541 153354 : test();
1542 153354 : test();
1543 153354 : test();
1544 153354 : IF( EQ_16( coder_type, TRANSITION ) && ( EQ_16( tc_call, 1 ) && tc_subfr == 0 && EQ_16( L_frame, L_FRAME ) ) )
1545 : {
1546 3672 : return error;
1547 : }
1548 :
1549 : /*-----------------------------------------------------------------*
1550 : * unused bits handling
1551 : *-----------------------------------------------------------------*/
1552 :
1553 149682 : acelp_cfg->ubits = 0; /* these bits could be reused for something else */
1554 149682 : move16();
1555 :
1556 149682 : test();
1557 149682 : IF( flag_hardcoded && NE_32( core_brate, PPP_NELP_2k80 ) )
1558 : {
1559 : /* unused bits */
1560 1441 : test();
1561 1441 : test();
1562 1441 : IF( EQ_16( coder_type, AUDIO ) || ( coder_type == INACTIVE && LE_32( core_brate, ACELP_24k40 ) ) )
1563 : {
1564 12 : acelp_cfg->ubits = 0;
1565 12 : move16();
1566 : }
1567 1429 : ELSE IF( EQ_16( L_frame, L_FRAME ) )
1568 : {
1569 740 : acelp_cfg->ubits = reserved_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, -1, TC_SUBFR2IDX( tc_subfr ) )];
1570 740 : move16();
1571 : }
1572 : ELSE
1573 : {
1574 689 : acelp_cfg->ubits = 0;
1575 689 : move16();
1576 : }
1577 :
1578 1441 : bits = sub( bits, acelp_cfg->ubits );
1579 : }
1580 :
1581 : /* sanity check */
1582 149682 : test();
1583 149682 : test();
1584 149682 : test();
1585 149682 : IF( ( coder_type != INACTIVE && EQ_16( nb_subfr, NB_SUBFR ) && NE_16( coder_type, AUDIO ) ) || EQ_16( nb_subfr, NB_SUBFR16k ) )
1586 : {
1587 127043 : test();
1588 127043 : test();
1589 127043 : test();
1590 127043 : test();
1591 127043 : test();
1592 127043 : test();
1593 127043 : test();
1594 127043 : test();
1595 127043 : test();
1596 127043 : test();
1597 127043 : test();
1598 127043 : test();
1599 127043 : IF( ( EQ_16( L_frame, L_FRAME16k ) && coder_type == INACTIVE && inactive_coder_type_flag ) /* GSC Inactive @16kHz */ ||
1600 : ( GSC_IVAS_mode > 0 && EQ_16( L_frame, L_FRAME16k ) ) ) /* IVAS GSC @16kHz */
1601 : {
1602 5066 : acelp_cfg->ubits = 0;
1603 5066 : move16();
1604 : }
1605 121977 : ELSE IF( flag_hardcoded && core == ACELP_CORE && bits != 0 )
1606 : {
1607 : }
1608 121977 : ELSE IF( bits > 0 && !( EQ_16( coder_type, UNVOICED ) && EQ_16( tdm_low_rate_mode, 1 ) && EQ_16( element_mode, IVAS_CPE_TD ) ) )
1609 : {
1610 42871 : test();
1611 42871 : test();
1612 42871 : test();
1613 42871 : test();
1614 42871 : IF( idchan > 0 && EQ_16( element_mode, IVAS_CPE_TD ) )
1615 : {
1616 1725 : IF( !tdm_lp_reuse_flag )
1617 : {
1618 1662 : acelp_cfg->lsf_bits = add( acelp_cfg->lsf_bits, bits ); /* increase LSF Q bits */
1619 1662 : move16();
1620 1662 : bits = 0;
1621 1662 : move16();
1622 : }
1623 : ELSE
1624 : {
1625 63 : Word16 nb_prm = 4;
1626 63 : move16();
1627 :
1628 63 : if ( EQ_16( tdm_low_rate_mode, 1 ) )
1629 : {
1630 0 : nb_prm = 2;
1631 0 : move16();
1632 : }
1633 : /* First add remaining bits on gains */
1634 63 : bits = sub( bits, allocate_unused( core_brate, coder_type, bits, nb_prm, 0, GAINSPRM, acelp_cfg->gains_mode ) );
1635 :
1636 : /* Then, Increase pitch bit budget */
1637 63 : test();
1638 63 : IF( tdm_Pitch_reuse_flag == 0 && bits > 0 )
1639 : {
1640 0 : bits = sub( bits, allocate_unused( core_brate, coder_type, bits, nb_prm, 0, PITCHPRM, acelp_cfg->pitch_bits ) );
1641 : }
1642 :
1643 : /* Increase mid-lsf bit budget */
1644 63 : test();
1645 63 : IF( tdm_lp_reuse_flag == 0 && bits > 0 )
1646 : {
1647 0 : bits = sub( bits, allocate_unused( core_brate, coder_type, bits, 1, 0, MID_LSFSPRM, &acelp_cfg->mid_lsf_bits ) );
1648 0 : bits = sub( bits, allocate_unused( core_brate, coder_type, bits, 1, 0, LSFPRM, &acelp_cfg->lsf_bits ) );
1649 : }
1650 : }
1651 : }
1652 :
1653 41146 : ELSE IF( core == ACELP_CORE && GE_16( coder_type, UNVOICED ) && LE_16( coder_type, GENERIC ) && EQ_16( L_frame, L_FRAME ) )
1654 : {
1655 22591 : acelp_cfg->lsf_bits = add( acelp_cfg->lsf_bits, bits ); /* increase LSF Q bits */
1656 22591 : move16();
1657 :
1658 22591 : test();
1659 22591 : IF( GT_16( acelp_cfg->lsf_bits, 46 ) )
1660 : {
1661 0 : acelp_cfg->ubits = sub( acelp_cfg->lsf_bits, 46 );
1662 0 : move16();
1663 0 : acelp_cfg->lsf_bits = 46;
1664 0 : move16();
1665 : }
1666 22591 : ELSE IF( GT_16( acelp_cfg->lsf_bits, 42 ) && EQ_16( L_frame, L_FRAME ) )
1667 : {
1668 113 : acelp_cfg->ubits = sub( acelp_cfg->lsf_bits, 42 );
1669 113 : move16();
1670 113 : acelp_cfg->lsf_bits = 42;
1671 113 : move16();
1672 : }
1673 : }
1674 : ELSE
1675 : {
1676 18555 : acelp_cfg->ubits = bits;
1677 18555 : move16();
1678 : }
1679 : }
1680 : /*else if ( bits < 0 && !( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) )
1681 : {
1682 : }*/
1683 : }
1684 :
1685 149682 : return error;
1686 : }
1687 :
1688 : /*-------------------------------------------------------------------*
1689 : * allocate_unused()
1690 : *
1691 : * Allocate unused bits
1692 : *--------------------------------------------------------------------*/
1693 :
1694 252 : static Word16 allocate_unused(
1695 : const Word32 core_brate,
1696 : const Word16 coder_type,
1697 : const Word16 unused_bits,
1698 : const Word16 nb_prm,
1699 : const Word16 subfr,
1700 : const Word16 prm_type,
1701 : Word16 *prm_bit_mode )
1702 : {
1703 252 : Word16 max_bit_per_pos = 0, bit_added = 0;
1704 252 : move16();
1705 252 : move16();
1706 : // PMT("Not floating point computation, but fixed point operator are still missing ")
1707 :
1708 252 : IF( prm_type == GAINSPRM )
1709 : {
1710 252 : max_bit_per_pos = 6;
1711 252 : move16();
1712 252 : IF( GT_32( core_brate, ACELP_8k00 ) )
1713 : {
1714 204 : max_bit_per_pos = 7;
1715 204 : move16();
1716 : }
1717 48 : ELSE IF( NE_16( coder_type, UNVOICED ) )
1718 : {
1719 48 : IF( GE_16( subfr, 1 ) )
1720 : {
1721 36 : max_bit_per_pos = 7;
1722 36 : move16();
1723 : }
1724 12 : ELSE IF( subfr == 0 )
1725 : {
1726 12 : max_bit_per_pos = 8;
1727 12 : move16();
1728 : }
1729 : }
1730 0 : ELSE IF( EQ_16( coder_type, UNVOICED ) )
1731 : {
1732 0 : max_bit_per_pos = 9; /* No real limit on UC gain bit budget of the secondary channel */
1733 0 : move16();
1734 : }
1735 : }
1736 0 : ELSE IF( EQ_16( prm_type, PITCHPRM ) )
1737 : {
1738 0 : max_bit_per_pos = 6;
1739 0 : move16();
1740 0 : test();
1741 0 : test();
1742 0 : if ( subfr == 0 || EQ_16( subfr, 2 ) || EQ_16( nb_prm, 2 ) )
1743 : {
1744 0 : max_bit_per_pos = 10;
1745 0 : move16();
1746 : }
1747 :
1748 0 : if ( EQ_16( coder_type, UNVOICED ) )
1749 : {
1750 0 : max_bit_per_pos = 0; /* Should not allocate bits in case of unvoiced coder type */
1751 0 : move16();
1752 : }
1753 : }
1754 0 : ELSE IF( EQ_16( prm_type, MID_LSFSPRM ) )
1755 : {
1756 0 : max_bit_per_pos = 5;
1757 0 : move16();
1758 : }
1759 0 : ELSE IF( EQ_16( prm_type, LSFPRM ) )
1760 : {
1761 0 : max_bit_per_pos = 42;
1762 0 : move16();
1763 : }
1764 : ELSE
1765 : {
1766 : #ifdef DEBUG_MODE_TD
1767 : IVAS_ERROR( IVAS_ERR_WRONG_MODE, "unknown mode in bit_alloc.c" );
1768 : #endif
1769 : }
1770 :
1771 252 : max_bit_per_pos = s_min( unused_bits, sub( max_bit_per_pos, prm_bit_mode[subfr] ) );
1772 252 : test();
1773 252 : IF( max_bit_per_pos < 0 )
1774 : {
1775 0 : return 0;
1776 : }
1777 252 : ELSE IF( max_bit_per_pos >= 0 && EQ_16( subfr, sub( nb_prm, 1 ) ) )
1778 : {
1779 63 : prm_bit_mode[subfr] = add( prm_bit_mode[subfr], max_bit_per_pos );
1780 63 : move16();
1781 : }
1782 : ELSE
1783 : {
1784 189 : prm_bit_mode[subfr] = add( prm_bit_mode[subfr], max_bit_per_pos );
1785 189 : move16();
1786 189 : bit_added = add( bit_added, allocate_unused( core_brate, coder_type, sub( unused_bits, max_bit_per_pos ), nb_prm, add( subfr, 1 ), prm_type, &prm_bit_mode[0] ) );
1787 : }
1788 :
1789 252 : return add( bit_added, max_bit_per_pos );
1790 : }
1791 :
1792 :
1793 : /*-------------------------------------------------------------------*
1794 : * set_ACELP_flag()
1795 : *
1796 : * set ACELP@16kHz flag
1797 : *--------------------------------------------------------------------*/
1798 :
1799 : /*! r: ACELP16k flag */
1800 11257 : Word16 set_ACELP_flag(
1801 : const Word16 element_mode, /* i : element mode */
1802 : const Word32 element_brate, /* i : element bitrate */
1803 : const Word32 total_brate, /* i : total bitrate per channel */
1804 : const Word16 idchan, /* i : Channel id */
1805 : const Word16 tdm_LRTD_flag, /* i : LRTD stereo mode flag */
1806 : const Word16 bwidth, /* i : audio bandwidth */
1807 : const Word16 cng_type /* i : CNG type */
1808 : )
1809 : {
1810 : // PMT("Not floating point computation, but fixed point operator are still missing ")
1811 11257 : test();
1812 11257 : test();
1813 11257 : test();
1814 11257 : test();
1815 11257 : IF( EQ_16( element_mode, IVAS_CPE_DFT ) && ( idchan == 0 ) && LE_32( total_brate, SID_2k40 ) && EQ_16( bwidth, WB ) && ( cng_type == LP_CNG ) )
1816 : {
1817 0 : return 1;
1818 : }
1819 11257 : ELSE IF( EQ_16( element_mode, IVAS_CPE_TD ) )
1820 : {
1821 0 : test();
1822 0 : test();
1823 0 : test();
1824 0 : IF( GE_32( element_brate, IVAS_24k4 ) && ( idchan == 0 ) && ( ( tdm_LRTD_flag == 0 ) || GT_32( element_brate, IVAS_24k4 ) ) )
1825 : {
1826 0 : return 1;
1827 : }
1828 : ELSE
1829 : {
1830 0 : return 0;
1831 : }
1832 : }
1833 11257 : ELSE IF( EQ_16( element_mode, IVAS_CPE_DFT ) )
1834 : {
1835 196 : IF( GE_32( element_brate, IVAS_24k4 ) )
1836 : {
1837 0 : return 1;
1838 : }
1839 : ELSE
1840 : {
1841 196 : return 0;
1842 : }
1843 : }
1844 11061 : ELSE IF( EQ_16( element_mode, IVAS_SCE ) )
1845 : {
1846 1053 : IF( GE_32( element_brate, SCE_CORE_16k_LOW_LIMIT ) )
1847 : {
1848 0 : return 1;
1849 : }
1850 : ELSE
1851 : {
1852 1053 : return 0;
1853 : }
1854 : }
1855 10008 : ELSE IF( GE_32( total_brate, ACELP_16k_LOW_LIMIT ) ) /* EVS_MONO */
1856 : {
1857 2050 : return 1;
1858 : }
1859 : ELSE
1860 : {
1861 7958 : return 0;
1862 : }
1863 : }
1864 :
1865 : /*-------------------------------------------------------------------*
1866 : * set_ACELP_flag_IVAS()
1867 : *
1868 : * set ACELP@16kHz flag
1869 : *--------------------------------------------------------------------*/
1870 :
1871 : /*! r: ACELP16k flag */
1872 1076895 : Word16 set_ACELP_flag_IVAS(
1873 : const Word16 element_mode, /* i : element mode */
1874 : const Word32 element_brate, /* i : element bitrate */
1875 : const Word32 total_brate, /* i : total bitrate per channel */
1876 : const Word16 idchan, /* i : Channel id */
1877 : const Word16 tdm_LRTD_flag, /* i : LRTD stereo mode flag */
1878 : const Word16 bwidth, /* i : audio bandwidth */
1879 : const Word16 cng_type /* i : CNG type */
1880 : )
1881 : {
1882 1076895 : test();
1883 1076895 : test();
1884 1076895 : test();
1885 1076895 : test();
1886 1076895 : IF( EQ_16( element_mode, IVAS_CPE_DFT ) && ( idchan == 0 ) && LE_32( total_brate, SID_2k40 ) && EQ_16( bwidth, WB ) && ( cng_type == LP_CNG ) )
1887 : {
1888 366 : return 1;
1889 : }
1890 1076529 : ELSE IF( EQ_16( element_mode, IVAS_CPE_TD ) )
1891 : {
1892 7372 : test();
1893 7372 : test();
1894 7372 : test();
1895 7372 : IF( GE_32( element_brate, IVAS_24k4 ) && ( idchan == 0 ) && ( ( tdm_LRTD_flag == 0 ) || GT_32( element_brate, IVAS_24k4 ) ) )
1896 : {
1897 2015 : return 1;
1898 : }
1899 : ELSE
1900 : {
1901 5357 : return 0;
1902 : }
1903 : }
1904 1069157 : ELSE IF( EQ_16( element_mode, IVAS_CPE_DFT ) )
1905 : {
1906 59326 : IF( GE_32( element_brate, IVAS_24k4 ) )
1907 : {
1908 27247 : return 1;
1909 : }
1910 : ELSE
1911 : {
1912 32079 : return 0;
1913 : }
1914 : }
1915 1009831 : ELSE IF( EQ_16( element_mode, IVAS_SCE ) )
1916 : {
1917 315143 : IF( GE_32( element_brate, SCE_CORE_16k_LOW_LIMIT ) )
1918 : {
1919 223884 : return 1;
1920 : }
1921 : ELSE
1922 : {
1923 91259 : return 0;
1924 : }
1925 : }
1926 694688 : ELSE IF( GE_32( total_brate, ACELP_16k_LOW_LIMIT ) ) /* EVS_MONO */
1927 : {
1928 686344 : return 1;
1929 : }
1930 : ELSE
1931 : {
1932 8344 : return 0;
1933 : }
1934 : }
|