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.452 Aug 12, 2021. Version 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdlib.h>
38 : #include <assert.h>
39 : #include <string.h>
40 : #include "options.h"
41 : #include "ivas_cnst.h" /* Common constants */
42 : #include "prot_fx.h" /* Function prototypes */
43 : #include "ivas_prot_fx.h"
44 : #include "basop_util.h"
45 : #include "rom_com.h"
46 : #include "mime.h"
47 :
48 : #ifdef DEBUGGING
49 : /*-------------------------------------------------------------------*
50 : * Global variables
51 : *--------------------------------------------------------------------*/
52 :
53 : int16_t FEC_seed = 12558; /* Seed for random FEC generator */
54 : FILE *FEC_pattern = NULL; /* FEC pattern file (for simulation of FEC) */
55 : float FEC_random = 0; /* FEC rate in percent (for simulation of FEC) */
56 : /*-------------------------------------------------------------------*
57 : * file_read_FECpattern()
58 : *
59 : * Simulate packet losses by reading FEC pattern from external file
60 : *-------------------------------------------------------------------*/
61 :
62 : static int16_t file_read_FECpattern( void )
63 : {
64 : int16_t bfi = 0;
65 :
66 : /* FEC pattern file provided */
67 : if ( FEC_pattern != NULL )
68 : {
69 : int16_t tmp = 0;
70 : if ( fread( &tmp, sizeof( int16_t ), 1, FEC_pattern ) != 1 )
71 : {
72 : if ( feof( FEC_pattern ) != 0 )
73 : {
74 : tmp = 0;
75 : fseek( FEC_pattern, 0L, SEEK_SET );
76 : }
77 : else
78 : {
79 : fprintf( stderr, "\nError reading the FEC pattern file !" );
80 : exit( -1 );
81 : }
82 : }
83 :
84 : if ( tmp == 2609 || tmp == 1 || tmp == SYNC_BAD_FRAME )
85 : {
86 : bfi = 1;
87 : }
88 : else
89 : {
90 : bfi = 0;
91 : }
92 : }
93 :
94 : /* random FEC simulation requested */
95 : else if ( FEC_random > 0 )
96 : {
97 : float ftmp = (float) /*own_random*/ Random( &FEC_seed ) + 32768.0f;
98 : if ( ftmp <= FEC_random / 100.0f * 65535.0f )
99 : {
100 : bfi = 1;
101 : }
102 : else
103 : {
104 : bfi = 0;
105 : }
106 : }
107 :
108 : return bfi;
109 : }
110 : #endif
111 : /*-------------------------------------------------------------------*
112 : * pack_bit()
113 : *
114 : * insert a bit into packed octet
115 : *-------------------------------------------------------------------*/
116 0 : void pack_bit(
117 : const Word16 bit, /* i: bit to be packed */
118 : UWord8 **pt, /* i/o: pointer to octet array into which bit will be placed */
119 : UWord8 *omask /* i/o: output mask to indicate where in the octet the bit is to be written */
120 : )
121 : {
122 0 : if ( EQ_16( *omask, 0x80 ) )
123 : {
124 0 : **pt = 0;
125 0 : move16();
126 : }
127 0 : IF( bit != 0 )
128 : {
129 0 : **pt = (UWord8) s_or( **pt, *omask );
130 0 : move16();
131 : }
132 0 : *omask = (UWord8) shr( *omask, 1 );
133 0 : move16();
134 0 : IF( *omask == 0 )
135 : {
136 0 : *omask = 0x80;
137 0 : move16();
138 0 : ( *pt )++;
139 : }
140 :
141 0 : return;
142 : }
143 :
144 :
145 : /*-------------------------------------------------------------------*
146 : * rate2AMRWB_IOmode()
147 : *
148 : * lookup AMRWB IO mode
149 : *-------------------------------------------------------------------*/
150 0 : static Word16 rate2AMRWB_IOmode(
151 : Word32 brate /* i: bitrate */
152 : )
153 : {
154 0 : SWITCH( brate )
155 : {
156 : /* EVS AMR-WB IO modes */
157 0 : case SID_1k75:
158 0 : return AMRWB_IO_SID;
159 0 : case ACELP_6k60:
160 0 : return AMRWB_IO_6600;
161 0 : case ACELP_8k85:
162 0 : return AMRWB_IO_8850;
163 0 : case ACELP_12k65:
164 0 : return AMRWB_IO_1265;
165 0 : case ACELP_14k25:
166 0 : return AMRWB_IO_1425;
167 0 : case ACELP_15k85:
168 0 : return AMRWB_IO_1585;
169 0 : case ACELP_18k25:
170 0 : return AMRWB_IO_1825;
171 0 : case ACELP_19k85:
172 0 : return AMRWB_IO_1985;
173 0 : case ACELP_23k05:
174 0 : return AMRWB_IO_2305;
175 0 : case ACELP_23k85:
176 0 : return AMRWB_IO_2385;
177 0 : default:
178 0 : break;
179 : }
180 0 : return -1;
181 : }
182 :
183 : /*-------------------------------------------------------------------*
184 : * rate2EVSmode()
185 : *
186 : * lookup EVS mode
187 : *-------------------------------------------------------------------*/
188 0 : Word16 rate2EVSmode(
189 : const Word32 brate, /* i: bitrate */
190 : Word16 *is_amr_wb /* o : (flag) does the bitrate belong to AMR-WB? Can be NULL */
191 : )
192 : {
193 0 : if ( is_amr_wb != NULL )
194 : {
195 0 : *is_amr_wb = 0;
196 0 : move16();
197 : }
198 0 : SWITCH( brate )
199 : {
200 : /* EVS Primary modes */
201 0 : case FRAME_NO_DATA:
202 0 : return NO_DATA_TYPE;
203 0 : case SID_2k40:
204 0 : return PRIMARY_SID;
205 0 : case PPP_NELP_2k80:
206 0 : return PRIMARY_2800;
207 0 : case ACELP_7k20:
208 0 : return PRIMARY_7200;
209 0 : case ACELP_8k00:
210 0 : return PRIMARY_8000;
211 0 : case ACELP_9k60:
212 0 : return PRIMARY_9600;
213 0 : case ACELP_13k20:
214 0 : return PRIMARY_13200;
215 0 : case ACELP_16k40:
216 0 : return PRIMARY_16400;
217 0 : case ACELP_24k40:
218 0 : return PRIMARY_24400;
219 0 : case ACELP_32k:
220 0 : return PRIMARY_32000;
221 0 : case ACELP_48k:
222 0 : return PRIMARY_48000;
223 0 : case ACELP_64k:
224 0 : return PRIMARY_64000;
225 0 : case HQ_96k:
226 0 : return PRIMARY_96000;
227 0 : case HQ_128k:
228 0 : return PRIMARY_128000;
229 0 : default:
230 0 : BREAK;
231 : }
232 0 : if ( is_amr_wb != NULL )
233 : {
234 0 : *is_amr_wb = 1;
235 0 : move16();
236 : }
237 0 : return rate2AMRWB_IOmode( brate );
238 : }
239 :
240 : /*-------------------------------------------------------------------*
241 : * push_indice( )
242 : *
243 : * Push a new indice into the buffer
244 : *-------------------------------------------------------------------*/
245 :
246 : /*-------------------------------------------------------------------*
247 : * get_next_indice_fx( )
248 : *
249 : * Get the next indice from the buffer
250 : *-------------------------------------------------------------------*/
251 :
252 38690220 : UWord16 get_next_indice_fx( /* o : value of the indice */
253 : Decoder_State *st_fx, /* i/o: decoder state structure */
254 : Word16 nb_bits /* i : number of bits that were used to quantize the indice */
255 : )
256 : {
257 : UWord16 value;
258 : Word16 i;
259 : Word16 nbits_total;
260 :
261 38690220 : assert( nb_bits <= 16 );
262 38690220 : value = 0;
263 38690220 : move16();
264 :
265 38690220 : nbits_total = extract_l( Mpy_32_32( st_fx->total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
266 : /* detect corrupted bitstream */
267 38690220 : IF( GT_16( add( st_fx->next_bit_pos, nb_bits ), nbits_total ) )
268 : {
269 0 : st_fx->BER_detect = 1;
270 0 : move16();
271 0 : return ( 0 );
272 : }
273 :
274 180700861 : FOR( i = 0; i < nb_bits; i++ )
275 : {
276 142010641 : value = (UWord16) L_shl( value, 1 );
277 142010641 : value = (UWord16) L_add( value, st_fx->bit_stream[add( st_fx->next_bit_pos, i )] );
278 : }
279 :
280 : /* update the position in the bitstream */
281 38690220 : st_fx->next_bit_pos = add( st_fx->next_bit_pos, nb_bits );
282 38690220 : move16();
283 38690220 : return value;
284 : }
285 :
286 : /*-------------------------------------------------------------------*
287 : * get_next_indice_1_fx( )
288 : *
289 : * Get the next 1-bit indice from the buffer
290 : *-------------------------------------------------------------------*/
291 :
292 173401235 : UWord16 get_next_indice_1_fx( /* o : value of the indice */
293 : Decoder_State *st_fx /* i/o: decoder state structure */
294 : )
295 : {
296 : Word16 nbits_total;
297 173401235 : nbits_total = extract_l( Mpy_32_32( st_fx->total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
298 : /* detect corrupted bitstream */
299 173401235 : test();
300 173401235 : test();
301 173401235 : test();
302 173401235 : IF( ( GT_16( add( st_fx->next_bit_pos, 1 ), nbits_total ) && EQ_16( st_fx->codec_mode, MODE1 ) ) ||
303 : ( GT_16( add( st_fx->next_bit_pos, 1 ), add( nbits_total, 2 * 8 ) ) && EQ_16( st_fx->codec_mode, MODE2 ) ) /* add two zero bytes for arithmetic coder flush */
304 : )
305 : {
306 0 : st_fx->BER_detect = 1;
307 0 : move16();
308 0 : return ( 0 );
309 : }
310 :
311 173401235 : return st_fx->bit_stream[st_fx->next_bit_pos++];
312 : }
313 :
314 : /*-------------------------------------------------------------------*
315 : * get_next_indice_tmp()
316 : *
317 : * update the total number of bits and the position in the bitstream
318 : *-------------------------------------------------------------------*/
319 :
320 1313002 : void get_next_indice_tmp_fx(
321 : Decoder_State *st_fx, /* o : decoder state structure */
322 : Word16 nb_bits /* i : number of bits that were used to quantize the indice */
323 : )
324 : {
325 : /* update the position in the bitstream */
326 1313002 : st_fx->next_bit_pos = add( st_fx->next_bit_pos, nb_bits );
327 1313002 : move16();
328 1313002 : }
329 :
330 : /*-------------------------------------------------------------------*
331 : * get_indice_fx( )
332 : *
333 : * Get indice at specific position in the buffer
334 : *-------------------------------------------------------------------*/
335 :
336 0 : UWord16 get_indice_fx( /* o : value of the indice */
337 : Decoder_State *st_fx, /* i/o: decoder state structure */
338 : Word16 pos, /* i : absolute position in the bitstream (update after the read) */
339 : Word16 nb_bits /* i : number of bits that were used to quantize the indice */
340 : )
341 : {
342 : UWord16 value;
343 : Word16 i;
344 :
345 0 : assert( nb_bits <= 16 );
346 :
347 : /* detect corrupted bitstream */
348 0 : IF( GT_16( add( pos, nb_bits ), st_fx->total_num_bits ) )
349 : {
350 0 : st_fx->BER_detect = 1;
351 0 : move16();
352 0 : return ( 0 );
353 : }
354 :
355 0 : value = 0;
356 0 : move16();
357 0 : FOR( i = 0; i < nb_bits; i++ )
358 : {
359 0 : value = lshl( value, 1 );
360 0 : value = add( value, st_fx->bit_stream[pos + i] );
361 : }
362 :
363 0 : return value;
364 : }
365 :
366 : /*-------------------------------------------------------------------*
367 : * get_indice_1_fx( )
368 : *
369 : * Get a 1-bit indice at specific position in the buffer
370 : *-------------------------------------------------------------------*/
371 :
372 64388685 : UWord16 get_indice_1_fx( /* o : value of the indice */
373 : Decoder_State *st_fx, /* i/o: decoder state structure */
374 : Word16 pos /* i : absolute position in the bitstream (update after the read) */
375 : )
376 : {
377 : Word16 nbits_total;
378 64388685 : nbits_total = extract_l( Mpy_32_32( st_fx->total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
379 : /* detect corrupted bitstream */
380 64388685 : IF( GT_16( add( pos, 1 ), nbits_total ) )
381 : {
382 0 : st_fx->BER_detect = 1;
383 0 : move16();
384 0 : return ( 0 );
385 : }
386 :
387 64388685 : return st_fx->bit_stream[pos];
388 : }
389 :
390 : /*-------------------------------------------------------------------*
391 : * reset_indices_enc_fx()
392 : *
393 : * Reset the buffer of indices
394 : *-------------------------------------------------------------------*/
395 :
396 1955440 : void reset_indices_enc_fx(
397 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder state structure */
398 : const Word16 max_num_indices /* i : max number of indices */
399 : )
400 : {
401 : Word16 i;
402 :
403 1955440 : hBstr->nb_ind_tot = 0;
404 1955440 : move16();
405 1955440 : hBstr->nb_bits_tot = 0;
406 1955440 : move16();
407 281921164 : FOR( i = 0; i < max_num_indices; i++ )
408 : {
409 279965724 : hBstr->ind_list[i].nb_bits = -1;
410 279965724 : move16();
411 : }
412 :
413 1955440 : return;
414 : }
415 :
416 : /*-------------------------------------------------------------------*
417 : * reset_indices_dec_fx()
418 : *
419 : * Reset the buffer of decoder indices
420 : *-------------------------------------------------------------------*/
421 :
422 0 : void reset_indices_dec_fx(
423 : Decoder_State *st_fx )
424 : {
425 0 : st_fx->next_bit_pos = 0;
426 0 : move16();
427 :
428 0 : return;
429 : }
430 :
431 :
432 : /*-------------------------------------------------------------------*
433 : * write_indices_fx()
434 : *
435 : * Write the buffer of indices to a file
436 : *-------------------------------------------------------------------*/
437 : /*-------------------------------------------------------------------*
438 : * write_indices_buf_fx()
439 : *
440 : * Write the buffer of indices to a file
441 : *-------------------------------------------------------------------*/
442 : /*-------------------------------------------------------------------*
443 : * indices_to_serial()
444 : *
445 : * pack indices into serialized payload format
446 : *-------------------------------------------------------------------*/
447 :
448 0 : void indices_to_serial(
449 : const Encoder_State *st_fx, /* i: encoder state structure */
450 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder state structure */
451 : UWord8 *pFrame, /* o: byte array with bit packet and byte aligned coded speech data */
452 : Word16 *pFrame_size /* o: size of the binary encoded access unit [bits] */
453 : )
454 : {
455 : Word16 i, k, j;
456 0 : Word16 cmi = 0, core_mode = 0;
457 : Word32 mask;
458 : Word16 amrwb_bits[( ACELP_23k85 / 50 )];
459 0 : UWord8 omask = 0x80;
460 0 : UWord8 *pt_pFrame = pFrame;
461 0 : Word16 isAmrWb = 0;
462 0 : move16();
463 0 : move16();
464 0 : move16();
465 0 : move16();
466 :
467 0 : IF( st_fx->Opt_AMR_WB )
468 : {
469 0 : cmi = rate2EVSmode( st_fx->total_brate, &isAmrWb );
470 0 : core_mode = rate2EVSmode( L_mult0( hBstr->nb_bits_tot, 50 ), &isAmrWb );
471 :
472 0 : j = 0;
473 0 : move16();
474 0 : FOR( i = 0; i < MAX_NUM_INDICES; i++ )
475 : {
476 0 : IF( NE_16( hBstr->ind_list[i].nb_bits, -1 ) )
477 : {
478 : /* mask from MSB to LSB */
479 0 : mask = L_shl( 1, sub( hBstr->ind_list[i].nb_bits, 1 ) );
480 :
481 : /* temporarily save bit */
482 0 : FOR( k = 0; k < hBstr->ind_list[i].nb_bits; k++ )
483 : {
484 0 : amrwb_bits[j++] = L_and( hBstr->ind_list[i].value, mask ) > 0;
485 0 : move16();
486 0 : mask = L_shr( mask, 1 );
487 : }
488 : }
489 : }
490 : }
491 :
492 0 : *pFrame_size = hBstr->nb_bits_tot;
493 0 : move16();
494 :
495 : /*----------------------------------------------------------------*
496 : * Bitstream packing (conversion of individual indices into a serial stream)
497 : *----------------------------------------------------------------*/
498 0 : j = 0;
499 0 : move16();
500 0 : FOR( i = 0; i < MAX_NUM_INDICES; i++ )
501 : {
502 0 : IF( NE_16( hBstr->ind_list[i].nb_bits, -1 ) )
503 : {
504 : /* mask from MSB to LSB */
505 0 : mask = L_shl( 1, sub( hBstr->ind_list[i].nb_bits, 1 ) );
506 :
507 : /* write bit by bit */
508 0 : FOR( k = 0; k < hBstr->ind_list[i].nb_bits; k++ )
509 : {
510 0 : IF( st_fx->Opt_AMR_WB )
511 : {
512 0 : pack_bit( amrwb_bits[sort_ptr[core_mode][j++]], &pt_pFrame, &omask );
513 : }
514 : ELSE
515 : {
516 0 : pack_bit( hBstr->ind_list[i].value & mask, &pt_pFrame, &omask );
517 0 : j = add( j, 1 );
518 : }
519 0 : mask = L_shr( mask, 1 );
520 : }
521 : }
522 : }
523 :
524 0 : test();
525 0 : IF( st_fx->Opt_AMR_WB && EQ_16( core_mode, AMRWB_IO_SID ) ) /* SID UPD frame always written now .... */
526 : {
527 : /* insert STI bit and CMI */
528 0 : pack_bit( 1, &pt_pFrame, &omask );
529 0 : FOR( mask = 0x08; mask > 0; mask >>= 1 )
530 : {
531 0 : pack_bit( cmi & mask, &pt_pFrame, &omask );
532 : }
533 : }
534 0 : }
535 :
536 :
537 : /*-------------------------------------------------------------------*
538 : * indices_to_serial_generic()
539 : *
540 : * pack indices into serialized payload format
541 : *-------------------------------------------------------------------*/
542 :
543 0 : void indices_to_serial_generic(
544 : const Indice *ind_list, /* i: indices list */
545 : const Word16 num_indices, /* i: number of indices to write */
546 : UWord8 *pFrame, /* o: byte array with bit packet and byte aligned coded speech data */
547 : Word16 *pFrame_size /* i/o: number of bits in the binary encoded access unit [bits] */
548 : )
549 : {
550 : Word16 i, k, j;
551 : Word32 mask;
552 : UWord8 omask;
553 0 : UWord8 *pt_pFrame = pFrame;
554 : Word16 nb_bits_tot;
555 :
556 0 : nb_bits_tot = 0;
557 0 : move16();
558 0 : omask = (UWord8) shr( 0x80, s_and( *pFrame_size, 0x7 ) );
559 0 : move16();
560 0 : pt_pFrame += shr( *pFrame_size, 3 );
561 :
562 : /*----------------------------------------------------------------*
563 : * Bitstream packing (conversion of individual indices into a serial stream)
564 : *----------------------------------------------------------------*/
565 0 : j = 0;
566 0 : move16();
567 0 : FOR( i = 0; i < num_indices; i++ ){
568 0 : IF( NE_16( ind_list[i].nb_bits, -1 ) ){
569 : /* mask from MSB to LSB */
570 0 : mask = L_shl( 1, sub( ind_list[i].nb_bits, 1 ) );
571 :
572 : /* write bit by bit */
573 0 : FOR( k = 0; k < ind_list[i].nb_bits; k++ )
574 : {
575 0 : pack_bit( ind_list[i].value & mask, &pt_pFrame, &omask );
576 0 : j = add( j, 1 );
577 0 : mask = L_shr( mask, 1 );
578 : }
579 0 : nb_bits_tot = add( nb_bits_tot, ind_list[i].nb_bits );
580 : }
581 : }
582 :
583 0 : *pFrame_size = add( *pFrame_size, nb_bits_tot );
584 0 : move16();
585 :
586 0 : return;
587 : }
588 :
589 :
590 1050 : static void dec_prm_core( Decoder_State *st )
591 : {
592 : Word16 n, frame_size_index, num_bits;
593 : UWord16 lsb;
594 : Word32 L_tmp;
595 :
596 1050 : frame_size_index = -1;
597 1050 : move16();
598 1050 : st->core = -1;
599 1050 : move16();
600 :
601 1050 : IF( st->total_brate == FRAME_NO_DATA )
602 : {
603 0 : st->m_frame_type = ZERO_FRAME;
604 0 : move16();
605 : }
606 1050 : ELSE IF( EQ_32( st->total_brate, SID_2k40 ) )
607 : {
608 0 : st->m_frame_type = SID_FRAME;
609 0 : move16();
610 : }
611 : ELSE
612 : {
613 1050 : st->m_frame_type = ACTIVE_FRAME;
614 1050 : move16();
615 1050 : Mpy_32_16_ss( st->total_brate, 5243, &L_tmp, &lsb ); /* 5243 is 1/50 in Q18. (0+18-15=3) */
616 1050 : num_bits = extract_l( L_shr( L_tmp, 3 ) ); /* Q0 */
617 1050 : assert( num_bits == st->total_brate / 50 );
618 8400 : FOR( n = 0; n < FRAME_SIZE_NB; ++n )
619 : {
620 8400 : IF( EQ_16( FrameSizeConfig[n].frame_bits, num_bits ) )
621 : {
622 1050 : frame_size_index = n;
623 1050 : move16();
624 1050 : BREAK;
625 : }
626 : }
627 :
628 : /* Get bandwidth mode */
629 1050 : st->bwidth = get_next_indice_fx( st, FrameSizeConfig[frame_size_index].bandwidth_bits );
630 1050 : move16();
631 :
632 1050 : st->bwidth = add( st->bwidth, FrameSizeConfig[frame_size_index].bandwidth_min );
633 1050 : move16();
634 :
635 1050 : IF( GT_16( st->bwidth, FB ) )
636 : {
637 0 : st->bwidth = FB;
638 0 : move16();
639 0 : st->BER_detect = 1;
640 0 : move16();
641 : }
642 :
643 1050 : test();
644 1050 : IF( GT_16( st->bwidth, SWB ) && LT_32( st->total_brate, ACELP_16k40 ) )
645 : {
646 0 : st->bwidth = SWB;
647 0 : move16();
648 0 : st->BER_detect = 1;
649 0 : move16();
650 : }
651 :
652 : /* Skip reserved bit */
653 1050 : get_next_indice_tmp_fx( st, FrameSizeConfig[frame_size_index].reserved_bits );
654 :
655 1050 : IF( get_next_indice_1_fx( st ) != 0 ) /* TCX */
656 : {
657 440 : st->core = TCX_20_CORE;
658 440 : move16();
659 440 : if ( get_next_indice_1_fx( st ) != 0 )
660 : {
661 66 : st->core = HQ_CORE;
662 66 : move16();
663 : }
664 : }
665 : ELSE /* ACELP */
666 : {
667 610 : st->core = ACELP_CORE;
668 610 : move16();
669 : }
670 : }
671 1050 : }
672 :
673 : /*-----------------------------------------------------------------*
674 : * decision_matrix_core_dec()
675 : *
676 : * Read core mode signalling bits from the bitstream
677 : * Set st->core, and st->bwidth if signalled together with the core.
678 : *-----------------------------------------------------------------*/
679 :
680 1050 : static void decision_matrix_core_dec(
681 : Decoder_State *st /* i/o: decoder state structure */
682 : )
683 : {
684 : Word16 start_idx;
685 : Word32 ind;
686 : Word16 nBits;
687 :
688 1050 : assert( st->bfi != 1 );
689 :
690 1050 : st->core = -1;
691 1050 : move16();
692 1050 : st->bwidth = -1;
693 1050 : move16();
694 :
695 1050 : test();
696 1050 : IF( ( st->total_brate == FRAME_NO_DATA ) || EQ_32( st->total_brate, SID_2k40 ) )
697 : {
698 0 : st->core = ACELP_CORE;
699 0 : move16();
700 : }
701 : /* SC-VBR */
702 1050 : ELSE IF( EQ_32( st->total_brate, PPP_NELP_2k80 ) )
703 : {
704 0 : st->core = ACELP_CORE;
705 0 : move16();
706 0 : return;
707 : }
708 :
709 : /*---------------------------------------------------------------------*
710 : * ACELP/HQ core selection
711 : *---------------------------------------------------------------------*/
712 :
713 1050 : test();
714 1050 : IF( LT_32( st->total_brate, ACELP_24k40 ) )
715 : {
716 1050 : st->core = ACELP_CORE;
717 1050 : move16();
718 : }
719 0 : ELSE IF( GE_32( st->total_brate, ACELP_24k40 ) && LE_32( st->total_brate, ACELP_64k ) )
720 : {
721 : /* read the ACELP/HQ core selection bit */
722 0 : st->core = imult1616( get_next_indice_fx( st, 1 ), HQ_CORE );
723 0 : move16();
724 : }
725 : ELSE
726 : {
727 0 : st->core = HQ_CORE;
728 0 : move16();
729 : }
730 :
731 : /*-----------------------------------------------------------------*
732 : * Read ACELP signalling bits from the bitstream
733 : *-----------------------------------------------------------------*/
734 :
735 1050 : IF( st->core == ACELP_CORE )
736 : {
737 : /* find the section in the ACELP signalling table corresponding to bitrate */
738 1050 : start_idx = 0;
739 1050 : move16();
740 36750 : WHILE( acelp_sig_tbl[start_idx] != st->total_brate )
741 : {
742 35700 : start_idx = add( start_idx, 1 );
743 : }
744 :
745 : /* skip the bitrate */
746 1050 : start_idx = add( start_idx, 1 );
747 :
748 : /* retrieve the number of bits */
749 1050 : nBits = extract_l( acelp_sig_tbl[start_idx] );
750 1050 : start_idx = add( start_idx, 1 );
751 :
752 : /* retrieve the signalling indice */
753 1050 : ind = acelp_sig_tbl[start_idx + get_next_indice_fx( st, nBits )];
754 1050 : st->bwidth = extract_l( L_and( L_shr( ind, 3 ), 0x7 ) );
755 1050 : move16();
756 :
757 : /* convert signalling indice into signalling information */
758 1050 : if ( EQ_32( L_and( ind, 0x7 ), LR_MDCT ) )
759 : {
760 296 : st->core = HQ_CORE;
761 296 : move16();
762 : }
763 : }
764 :
765 : /*-----------------------------------------------------------------*
766 : * Read HQ signalling bits from the bitstream
767 : * Set HQ core type
768 : *-----------------------------------------------------------------*/
769 :
770 1050 : IF( EQ_16( st->core, HQ_CORE ) )
771 : {
772 : /* read the HQ/TCX core switching flag */
773 296 : if ( get_next_indice_fx( st, 1 ) != 0 )
774 : {
775 264 : st->core = TCX_20_CORE;
776 264 : move16();
777 : }
778 :
779 : /* For TCX: read/set band-width (needed for different I/O sampling rate support) */
780 296 : test();
781 296 : IF( EQ_16( st->core, TCX_20_CORE ) && GT_32( st->total_brate, ACELP_16k40 ) )
782 : {
783 0 : ind = get_next_indice_fx( st, 2 );
784 :
785 0 : IF( ind == 0 )
786 : {
787 0 : st->bwidth = NB;
788 0 : move16();
789 : }
790 0 : ELSE IF( EQ_32( ind, 1 ) )
791 : {
792 0 : st->bwidth = WB;
793 0 : move16();
794 : }
795 0 : ELSE IF( EQ_32( ind, 2 ) )
796 : {
797 0 : st->bwidth = SWB;
798 0 : move16();
799 : }
800 : ELSE
801 : {
802 0 : st->bwidth = FB;
803 0 : move16();
804 : }
805 : }
806 : }
807 :
808 1050 : return;
809 : }
810 :
811 : /*-------------------------------------------------------------------*
812 : * mdct_switching_dec()
813 : *
814 : * Set up MDCT core switching if indicated in the bit stream
815 : *-------------------------------------------------------------------*/
816 :
817 3100 : void mdct_switching_dec_fx(
818 : Decoder_State *st /* i/o: decoder state structure */
819 : )
820 : {
821 3100 : if ( !st->bfi )
822 : {
823 3100 : IF( st->Opt_AMR_WB != 0 )
824 : {
825 0 : return;
826 : }
827 :
828 3100 : test();
829 3100 : test();
830 3100 : IF( EQ_32( st->total_brate, ACELP_13k20 ) || EQ_32( st->total_brate, ACELP_32k ) )
831 : {
832 1050 : st->mdct_sw_enable = MODE1;
833 1050 : move16();
834 : }
835 2050 : ELSE IF( LE_32( ACELP_16k40, st->total_brate ) && LE_32( st->total_brate, ACELP_24k40 ) )
836 : {
837 1050 : st->mdct_sw_enable = MODE2;
838 1050 : move16();
839 : }
840 :
841 3100 : test();
842 3100 : test();
843 3100 : IF( EQ_16( st->codec_mode, MODE1 ) && EQ_16( st->mdct_sw_enable, MODE1 ) )
844 1050 : {
845 : /* Read ahead core mode signaling */
846 : Word16 next_bit_pos_save;
847 : Word16 core_save;
848 : Word16 bwidth_save;
849 :
850 1050 : next_bit_pos_save = st->next_bit_pos;
851 1050 : move16();
852 1050 : core_save = st->core;
853 1050 : move16();
854 1050 : bwidth_save = st->bwidth;
855 1050 : move16();
856 :
857 1050 : decision_matrix_core_dec( st ); /* sets st->core */
858 :
859 1050 : IF( EQ_16( st->core, TCX_20_CORE ) )
860 : {
861 : /* Trigger TCX */
862 264 : st->codec_mode = MODE2;
863 264 : move16();
864 264 : st->mdct_sw = MODE1;
865 264 : move16();
866 : }
867 : ELSE
868 : {
869 : /* Rewind bitstream */
870 786 : st->next_bit_pos = next_bit_pos_save;
871 786 : move16();
872 786 : IF( st->bfi != 0 )
873 : {
874 0 : st->core = core_save;
875 0 : move16();
876 0 : st->bwidth = bwidth_save;
877 0 : move16();
878 : }
879 : }
880 : }
881 2050 : ELSE IF( EQ_16( st->codec_mode, MODE2 ) && EQ_16( st->mdct_sw_enable, MODE2 ) )
882 : {
883 : /* Read ahead core mode signaling */
884 : Word16 next_bit_pos_save;
885 : Word16 core_save;
886 : Word16 bwidth_save;
887 :
888 1050 : next_bit_pos_save = st->next_bit_pos;
889 1050 : move16();
890 1050 : core_save = st->core;
891 1050 : move16();
892 1050 : bwidth_save = st->bwidth;
893 1050 : move16();
894 :
895 1050 : dec_prm_core( st ); /* sets st->core */
896 :
897 1050 : IF( EQ_16( st->core, HQ_CORE ) )
898 : {
899 : /* Trigger HQ_CORE */
900 66 : st->codec_mode = MODE1;
901 66 : move16();
902 66 : st->mdct_sw = MODE2;
903 66 : move16();
904 : }
905 : ELSE
906 : {
907 : /* Rewind bitstream */
908 984 : st->next_bit_pos = next_bit_pos_save;
909 984 : move16();
910 984 : if ( st->bfi != 0 )
911 : {
912 0 : st->core = core_save;
913 0 : move16();
914 : }
915 : /* always reset bwidth, to not interfere with BER logic */
916 984 : st->bwidth = bwidth_save;
917 984 : move16();
918 : }
919 : }
920 : }
921 :
922 3100 : return;
923 : }
924 :
925 :
926 : /*-------------------------------------------------------------------*
927 : * BRATE2IDX_fx()
928 : *
929 : * Convert Bitrate to Index Value
930 : *-------------------------------------------------------------------*/
931 :
932 43959 : Word16 BRATE2IDX_fx( Word32 brate )
933 : {
934 :
935 : Word32 L_temp;
936 : Word32 L_idx;
937 : #define START 9
938 : extern const Word16 bit_rates_div50[];
939 :
940 : /* This is a Fast Bit Rate Value to Index Value Binary Search */
941 43959 : L_temp = L_msu0( brate, bit_rates_div50[START], 50 );
942 43959 : L_temp = L_min( 6, L_max( -6, L_temp ) );
943 43959 : L_idx = L_add( L_temp, START );
944 43959 : L_temp = L_msu0( brate, bit_rates_div50[L_idx], 50 );
945 43959 : L_temp = L_min( 3, L_max( -3, L_temp ) );
946 43959 : L_idx = L_add( L_temp, L_idx );
947 43959 : L_temp = L_msu0( brate, bit_rates_div50[L_idx], 50 );
948 43959 : L_temp = L_min( 1, L_max( -2, L_temp ) );
949 43959 : L_idx = L_add( L_temp, L_idx );
950 43959 : L_temp = L_msu0( brate, bit_rates_div50[L_idx], 50 );
951 43959 : if ( L_temp != 0 )
952 14311 : L_idx = L_add( L_idx, 1 );
953 43959 : return (Word16) L_idx;
954 : }
955 :
956 :
957 : /*-------------------------------------------------------------------*
958 : * BRATE2IDX16k_fx()
959 : *
960 : * Convert Bitrate to Index Value
961 : *-------------------------------------------------------------------*/
962 :
963 123 : Word16 BRATE2IDX16k_fx( Word32 brate )
964 : {
965 : Word32 L_temp, L_idx;
966 : #define START_16K 5
967 : extern const Word16 bit_rates_16k_div50[];
968 :
969 123 : if ( EQ_32( brate, ACELP_16k40 ) )
970 : {
971 0 : brate = ACELP_14k80;
972 0 : move16();
973 : }
974 :
975 : /* This is a Fast Bit Rate Value to Index Value Binary Search */
976 123 : L_temp = L_msu0( brate, bit_rates_16k_div50[START_16K], 50 );
977 123 : L_temp = L_min( 3, L_max( -3, L_temp ) );
978 123 : L_idx = L_add( L_temp, START_16K );
979 123 : L_temp = L_msu0( brate, bit_rates_16k_div50[L_idx], 50 );
980 123 : L_temp = L_min( 2, L_max( -2, L_temp ) );
981 123 : L_idx = L_add( L_temp, L_idx );
982 123 : L_temp = L_msu0( brate, bit_rates_16k_div50[L_idx], 50 );
983 123 : L_temp = L_min( 1, L_max( -1, L_temp ) );
984 123 : L_idx = L_add( L_temp, L_idx );
985 :
986 123 : return (Word16) L_idx;
987 : }
988 :
989 : /*-------------------------------------------------------------------*
990 : * BIT_ALLOC_IDX_fx()
991 : *-------------------------------------------------------------------*/
992 :
993 722 : Word32 BIT_ALLOC_IDX_fx( Word32 brate, Word16 ctype, Word16 sfrm, Word16 tc )
994 : {
995 : Word32 L_temp;
996 : Word16 temp;
997 722 : if ( ctype == INACTIVE ) /* no sub(ctype, INACTIVE) because it is '0' */
998 0 : ctype = GENERIC;
999 722 : move16();
1000 722 : L_temp = L_mac0( -1l * 256, 1 * 256, ctype );
1001 :
1002 722 : temp = BRATE2IDX_fx( brate );
1003 722 : L_temp = L_mac0( L_temp, 4 * 256, temp );
1004 722 : IF( tc >= 0 )
1005 27 : L_temp = L_mac0( L_temp, ( 10 - 4 ) * 256, temp );
1006 : /* So either 'temp' x 4 when 'tc < 0', 'temp' x 10 otherwise */
1007 :
1008 722 : L_temp = L_mac0( L_temp, 1 * 256, s_max( 0, tc ) );
1009 :
1010 722 : L_temp = L_mac0( L_temp, s_max( 0, sfrm ), 1 );
1011 722 : if ( sfrm < 0 )
1012 695 : L_temp = L_shr( L_temp, 2 );
1013 722 : L_temp = L_shr( L_temp, 6 );
1014 :
1015 722 : return L_temp;
1016 : }
1017 :
1018 : /*-------------------------------------------------------------------*
1019 : * BIT_ALLOC_IDX_16KHZ_fx()
1020 : *-------------------------------------------------------------------*/
1021 :
1022 123 : Word32 BIT_ALLOC_IDX_16KHZ_fx( Word32 brate, Word16 ctype, Word16 sfrm, Word16 tc )
1023 : {
1024 : Word32 L_temp;
1025 : Word16 temp;
1026 : /* 'ctype' =
1027 : TRANSITION => 2
1028 : GENERIC => 1
1029 : ALL Other => 0
1030 : */
1031 123 : L_temp = L_and( shr( 0x0240l, shl( ctype, 1 ) ), 3 );
1032 :
1033 123 : temp = BRATE2IDX16k_fx( brate );
1034 123 : L_temp = L_mac0( L_temp, 3, temp );
1035 123 : IF( tc >= 0 )
1036 123 : L_temp = L_mac0( L_temp, ( 7 - 3 ), temp );
1037 : /* So either 'temp' x 3 when 'tc < 0', 'temp' x 7 otherwise */
1038 :
1039 123 : L_temp = L_mac0( L_temp, 1, s_max( 0, tc ) );
1040 :
1041 123 : IF( sfrm >= 0 )
1042 : {
1043 : /* Mult by 5 */
1044 123 : L_temp = L_add( L_temp, L_shl( L_temp, 2 ) );
1045 123 : L_temp = L_mac0( L_temp, shr( sfrm, 6 ), 1 );
1046 : }
1047 :
1048 123 : return L_temp;
1049 : }
1050 :
1051 :
1052 : /*-------------------------------------------------------------------*
1053 : * berCheck()
1054 : *
1055 : * Check for bit errors in channel aware signalling.
1056 : *-------------------------------------------------------------------*/
1057 :
1058 0 : static void berCheck(
1059 : Decoder_State *st, /* i/o: decoder state structure */
1060 : Word16 *coder_type /* i/o: coder type */
1061 : )
1062 : {
1063 : /* In case of RF flag = 1, and valid RF packet with primary and partial copy */
1064 0 : test();
1065 0 : test();
1066 0 : IF( ( EQ_16( st->bwidth, NB ) || EQ_16( st->bwidth, FB ) ) || ( GE_16( *coder_type, TRANSITION ) ) )
1067 : {
1068 0 : if ( EQ_16( st->use_partial_copy, 1 ) )
1069 : {
1070 0 : st->use_partial_copy = 0;
1071 0 : move16();
1072 : }
1073 :
1074 0 : st->bfi = 1;
1075 0 : move16();
1076 0 : st->bwidth = st->last_bwidth;
1077 0 : move16();
1078 0 : st->BER_detect = 1;
1079 0 : move16();
1080 0 : *coder_type = GENERIC;
1081 0 : move16();
1082 : }
1083 :
1084 0 : return;
1085 : }
1086 :
1087 : /*-------------------------------------------------------------------*
1088 : * getPartialCopyInfo()
1089 : *
1090 : * Check if the frame includes a partial copy for channel aware processing.
1091 : *-------------------------------------------------------------------*/
1092 :
1093 3100 : void getPartialCopyInfo(
1094 : Decoder_State *st, /* i/o: decoder state structure */
1095 : Word16 *coder_type,
1096 : Word16 *sharpFlag )
1097 : {
1098 : Word16 nBits;
1099 : Word16 ind;
1100 : /* check the rf flag in the packet */
1101 3100 : get_rfFlag( st, &( st->rf_flag ), &nBits, &ind );
1102 :
1103 : /* get rf frame type info */
1104 3100 : get_rfFrameType( st, &( st->rf_frame_type ) );
1105 :
1106 : /* Get the FEC offset info */
1107 3100 : get_rf_fec_offset( st, &( st->rf_fec_offset ) );
1108 :
1109 : /* reset number of target bits in case of rate switching */
1110 3100 : st->rf_target_bits = 0;
1111 3100 : move16();
1112 :
1113 : /* Get the number of bits used for RF*/
1114 3100 : IF( EQ_16( st->rf_flag, 1 ) )
1115 : {
1116 0 : *coder_type = s_and( ind, 0x7 );
1117 0 : move16();
1118 0 : st->bwidth = s_and( shr( ind, 3 ), 0x7 );
1119 0 : move16();
1120 0 : *sharpFlag = s_and( shr( ind, 6 ), 0x1 );
1121 0 : move16();
1122 0 : st->codec_mode = MODE2;
1123 0 : move16();
1124 0 : get_rfTargetBits( st->rf_frame_type, &( st->rf_target_bits ) );
1125 :
1126 0 : IF( EQ_16( st->bfi, FRAMEMODE_FUTURE ) )
1127 : {
1128 0 : st->use_partial_copy = 1;
1129 0 : move16();
1130 : /* now set the frame mode to normal mode */
1131 0 : test();
1132 0 : IF( GE_16( st->rf_frame_type, RF_TCXFD ) && LE_16( st->rf_frame_type, RF_TCXTD2 ) )
1133 : {
1134 0 : st->bfi = 1;
1135 0 : move16();
1136 0 : st->core = 1;
1137 0 : move16();
1138 : }
1139 : ELSE
1140 : {
1141 0 : st->bfi = FRAMEMODE_NORMAL;
1142 0 : move16();
1143 0 : st->core = 0;
1144 0 : move16();
1145 : }
1146 : }
1147 : /* check for bit errors */
1148 0 : berCheck( st, coder_type );
1149 :
1150 0 : get_next_indice_tmp_fx( st, nBits );
1151 : }
1152 3100 : }
1153 :
1154 : /*-------------------------------------------------------------------*
1155 : * get_rfFlag()
1156 : *
1157 : * Check if rf flag is present in the bitstream
1158 : *-------------------------------------------------------------------*/
1159 :
1160 3100 : void get_rfFlag(
1161 : Decoder_State *st, /* i: decoder state structure */
1162 : Word16 *rf_flag, /* o : check for the RF flag */
1163 : Word16 *nBits,
1164 : Word16 *ind )
1165 : {
1166 : Word16 start_idx, nBits_tmp;
1167 : Word16 ind_tmp;
1168 :
1169 : /* Init */
1170 3100 : *rf_flag = 0;
1171 3100 : move16();
1172 :
1173 : /* check for rf_flag in the packet and extract the rf_frame_type and rf_fec_offset */
1174 3100 : test();
1175 3100 : test();
1176 3100 : IF( EQ_32( st->total_brate, ACELP_13k20 ) && ( ( st->bfi == FRAMEMODE_NORMAL ) || EQ_16( st->bfi, FRAMEMODE_FUTURE ) ) )
1177 : {
1178 : /* find the section in the ACELP signalling table corresponding to bitrate */
1179 1050 : start_idx = 0;
1180 1050 : move16();
1181 36750 : WHILE( acelp_sig_tbl[start_idx] != st->total_brate )
1182 : {
1183 35700 : start_idx = add( start_idx, 1 );
1184 35700 : assert( ( start_idx < MAX_ACELP_SIG ) && "ERROR: start_idx larger than acelp_sig_tbl[].\n" );
1185 : }
1186 :
1187 : /* skip the bitrate */
1188 1050 : start_idx = add( start_idx, 1 );
1189 :
1190 : /* retrieve the number of bits */
1191 1050 : nBits_tmp = (Word16) acelp_sig_tbl[start_idx++];
1192 1050 : move16();
1193 :
1194 : /* retrieve the signalling indice */
1195 1050 : ind_tmp = (Word16) acelp_sig_tbl[add( start_idx, get_indice( st, 0, nBits_tmp ) )];
1196 1050 : move16();
1197 :
1198 : /* convert signalling indice into RF flag. */
1199 1050 : *rf_flag = s_and( shr( ind_tmp, 7 ), 0x1 );
1200 1050 : move16();
1201 :
1202 1050 : if ( ind )
1203 : {
1204 1050 : *ind = ind_tmp;
1205 1050 : move16();
1206 : }
1207 :
1208 1050 : if ( nBits )
1209 : {
1210 1050 : *nBits = nBits_tmp;
1211 1050 : move16();
1212 : }
1213 : }
1214 3100 : }
1215 :
1216 : /*-------------------------------------------------------------------*
1217 : * get_rfFrameType()
1218 : *
1219 : * Extract the rf frame type
1220 : *-------------------------------------------------------------------*/
1221 :
1222 3100 : void get_rfFrameType(
1223 : Decoder_State *st, /* i : decoder state structure */
1224 : Word16 *rf_frame_type /* o : RF frame type */
1225 : )
1226 : {
1227 3100 : Word16 num_bits = 0;
1228 3100 : move16();
1229 :
1230 3100 : IF( EQ_16( st->rf_flag, 1 ) )
1231 : {
1232 : /*num_bits = st->total_brate/50;*/
1233 0 : IF( EQ_32( st->total_brate, ACELP_13k20 ) )
1234 : {
1235 0 : num_bits = 264;
1236 0 : move16(); /* @13.2kbps */
1237 : }
1238 : ELSE
1239 : {
1240 : UWord16 lsb;
1241 : Word32 L_tmp;
1242 0 : Mpy_32_16_ss( st->total_brate, 5243, &L_tmp, &lsb ); /* 5243 is 1/50 in Q18. (0+18-15=3) */
1243 0 : num_bits = extract_l( L_shr( L_tmp, 3 ) ); /* Q0 */
1244 : }
1245 :
1246 : /* the last three bits in a packet is the RF frame type */
1247 0 : *rf_frame_type = get_indice( st, sub( num_bits, 3 ), 3 );
1248 0 : move16();
1249 : }
1250 : ELSE
1251 : {
1252 3100 : *rf_frame_type = 0;
1253 3100 : move16();
1254 : }
1255 3100 : }
1256 :
1257 : /*-------------------------------------------------------------------*
1258 : * get_rf_fec_offset()
1259 : *
1260 : * Extract the FEC offset
1261 : *-------------------------------------------------------------------*/
1262 :
1263 3100 : void get_rf_fec_offset(
1264 : Decoder_State *st, /* i : decoder state structure */
1265 : Word16 *rf_fec_offset /* o : RF fec offset */
1266 : )
1267 : {
1268 : Word16 num_bits, tmp;
1269 :
1270 3100 : IF( EQ_16( st->rf_flag, 1 ) )
1271 : {
1272 : /*num_bits = st->total_brate/50;*/
1273 0 : IF( EQ_32( st->total_brate, ACELP_13k20 ) )
1274 : {
1275 0 : num_bits = 264;
1276 0 : move16(); /* @13.2kbps */
1277 : }
1278 : ELSE
1279 : {
1280 : UWord16 lsb;
1281 : Word32 L_tmp;
1282 0 : Mpy_32_16_ss( st->total_brate, 5243, &L_tmp, &lsb ); /* 5243 is 1/50 in Q18. (0+18-15=3) */
1283 0 : num_bits = extract_l( L_shr( L_tmp, 3 ) ); /* Q0 */
1284 : }
1285 :
1286 : /* the two bits before the rf frame type contain the fec offset */
1287 0 : tmp = get_indice( st, sub( num_bits, 5 ), 2 );
1288 :
1289 0 : IF( tmp == 0 )
1290 : {
1291 0 : *rf_fec_offset = 2;
1292 0 : move16();
1293 : }
1294 : ELSE
1295 : {
1296 0 : *rf_fec_offset = add( shl( tmp, 1 ), 1 );
1297 0 : move16();
1298 : }
1299 : }
1300 : ELSE
1301 : {
1302 3100 : *rf_fec_offset = 0;
1303 3100 : move16();
1304 : }
1305 3100 : }
1306 :
1307 : /*-------------------------------------------------------------------*
1308 : * get_rfTargetBits()
1309 : *
1310 : * Return the number of RF target bits
1311 : *-------------------------------------------------------------------*/
1312 :
1313 0 : void get_rfTargetBits(
1314 : Word16 rf_frame_type, /* i : RF frame type */
1315 : Word16 *rf_target_bits /* o : Number of RF target bits */
1316 : )
1317 : {
1318 :
1319 : /* Number of RF bits for different RF coder types */
1320 :
1321 0 : SWITCH( rf_frame_type )
1322 : {
1323 0 : case RF_NO_DATA:
1324 0 : *rf_target_bits = 5;
1325 0 : move16();
1326 0 : BREAK;
1327 0 : case RF_TCXFD:
1328 0 : *rf_target_bits = 27;
1329 0 : move16();
1330 0 : BREAK;
1331 0 : case RF_TCXTD1:
1332 0 : *rf_target_bits = 16;
1333 0 : move16();
1334 0 : BREAK;
1335 0 : case RF_TCXTD2:
1336 0 : *rf_target_bits = 16;
1337 0 : move16();
1338 0 : BREAK;
1339 0 : case RF_ALLPRED:
1340 : /* Es_pred bits 3 bits, LTF: 1, pitch: 8,5,5,5, FCB: 0, gain: 7,0,7,0, Diff GFr: 4*/
1341 0 : *rf_target_bits = 63;
1342 0 : move16();
1343 0 : BREAK;
1344 0 : case RF_NOPRED:
1345 : /* Es_pred bits 3 bits, LTF: 0, pitch: 0, FCB: 7,7,7,7, gain: 6,0,6,0, Diff GFr: 2*/
1346 0 : *rf_target_bits = 66;
1347 0 : move16();
1348 0 : BREAK;
1349 0 : case RF_GENPRED:
1350 : /* Es_pred bits 3 bits, LTF: 1, pitch: 8,0,8,0, FCB: 6,7,5,5, gain: 5,0,5,0, Diff GFr: 0*/
1351 0 : *rf_target_bits = 70;
1352 0 : move16();
1353 0 : BREAK;
1354 0 : case RF_NELP:
1355 : /* gain: 19, Diff GFr: 5 */
1356 0 : *rf_target_bits = 45;
1357 0 : move16();
1358 0 : BREAK;
1359 : }
1360 0 : }
1361 :
1362 :
1363 : /*-------------------------------------------------------------------*
1364 : * get_NextCoderType_fx()
1365 : *
1366 : * Extract the coder type of next frame
1367 : *-------------------------------------------------------------------*/
1368 :
1369 53 : void get_NextCoderType_fx(
1370 : UWord8 *bitsteam, /* i : bitstream */
1371 : Word16 *next_coder_type /* o : next coder type */
1372 : )
1373 : {
1374 : Word16 k;
1375 : Word16 start_idx;
1376 : Word16 nBits_tmp;
1377 : Word8 bit_stream[ACELP_13k20 / 50];
1378 : UWord16 tmp;
1379 :
1380 :
1381 14045 : FOR( k = 0; k < ACELP_13k20 / 50; k++ )
1382 : {
1383 13992 : bit_stream[k] = (UWord8) s_and( shr( bitsteam[k / 8], sub( 7, s_and( k, 7 ) ) ), 0x1 );
1384 13992 : move16();
1385 : }
1386 53 : start_idx = 0;
1387 53 : move16();
1388 1855 : WHILE( acelp_sig_tbl[start_idx] != ACELP_13k20 )
1389 : {
1390 1802 : start_idx = add( start_idx, 1 );
1391 1802 : assert( ( start_idx < MAX_ACELP_SIG ) && "ERROR: start_idx larger than acelp_sig_tbl[].\n" );
1392 : }
1393 :
1394 : /* skip the bitrate */
1395 53 : start_idx = add( start_idx, 1 );
1396 :
1397 53 : tmp = 0;
1398 53 : move16();
1399 53 : nBits_tmp = extract_l( acelp_sig_tbl[start_idx++] );
1400 53 : move16();
1401 318 : FOR( k = 0; k < nBits_tmp; k++ )
1402 : {
1403 265 : tmp = lshl( tmp, 1 );
1404 265 : tmp = add( tmp, bit_stream[k] );
1405 : }
1406 : /* retrieve the signalling indice */
1407 53 : *next_coder_type = s_and( extract_l( acelp_sig_tbl[start_idx + tmp] ), 0x7 );
1408 53 : move16();
1409 53 : }
1410 :
1411 : /*-------------------------------------------------------------------*
1412 : * get_indice_preview()
1413 : *
1414 : * Indices preview to parse for the presence of partial copy
1415 : *-------------------------------------------------------------------*/
1416 4281 : static UWord16 get_indice_preview(
1417 : UWord8 *bitstream,
1418 : Word16 bitstreamSize,
1419 : Word16 pos,
1420 : Word16 nb_bits )
1421 : {
1422 : UWord16 value;
1423 : Word16 i;
1424 : UWord16 bitstreamShort[MAX_BITS_PER_FRAME + 16];
1425 : UWord16 *bitstreamShortPtr;
1426 :
1427 : /* convert bitstream from compact bytes to short values */
1428 4281 : bitstreamShortPtr = bitstreamShort;
1429 1134465 : FOR( i = 0; i < bitstreamSize; i++ )
1430 : {
1431 1130184 : *bitstreamShortPtr++ = s_and( shr( bitstream[i / 8], sub( 7, ( s_and( i, 7 ) ) ) ), 0x1 );
1432 1130184 : move16();
1433 : }
1434 :
1435 4281 : assert( nb_bits <= 16 );
1436 4281 : value = 0;
1437 4281 : move16();
1438 20966 : FOR( i = 0; i < nb_bits; i++ )
1439 : {
1440 16685 : value = shl( value, 1 );
1441 16685 : value = add( value, bitstreamShort[pos + i] );
1442 : }
1443 4281 : return value;
1444 : }
1445 :
1446 : /*-------------------------------------------------------------------*
1447 : * evs_dec_previewFrame()
1448 : *
1449 : * Signalling index preview
1450 : *-------------------------------------------------------------------*/
1451 17523 : void evs_dec_previewFrame(
1452 : UWord8 *bitstream,
1453 : Word16 bitstreamSize,
1454 : Word16 *partialCopyFrameType,
1455 : Word16 *partialCopyOffset )
1456 : {
1457 : Word32 total_brate;
1458 : Word16 start_idx, nBits;
1459 : Word32 ind;
1460 : Word16 rf_flag;
1461 :
1462 17523 : rf_flag = 0;
1463 17523 : move16();
1464 17523 : *partialCopyFrameType = 0;
1465 17523 : move16();
1466 17523 : *partialCopyOffset = 0;
1467 17523 : move16();
1468 17523 : total_brate = L_mult0( bitstreamSize, 50 );
1469 :
1470 17523 : IF( EQ_32( total_brate, ACELP_13k20 ) )
1471 : {
1472 : /* find the section in the ACELP signalling table corresponding to bitrate */
1473 2393 : start_idx = 0;
1474 2393 : move16();
1475 83755 : WHILE( acelp_sig_tbl[start_idx] != total_brate )
1476 : {
1477 81362 : start_idx = add( start_idx, 1 );
1478 81362 : assert( ( start_idx < MAX_ACELP_SIG ) && "ERROR: start_idx larger than acelp_sig_tbl[].\n" );
1479 : }
1480 :
1481 : /* skip the bitrate */
1482 2393 : start_idx = add( start_idx, 1 );
1483 : /* retrieve the number of bits */
1484 2393 : nBits = extract_l( acelp_sig_tbl[start_idx++] );
1485 :
1486 : /* retrieve the signalling indice */
1487 2393 : ind = acelp_sig_tbl[( start_idx + get_indice_preview( bitstream, bitstreamSize, 0, nBits ) )];
1488 2393 : move32();
1489 :
1490 : /* convert signalling indice into RF flag. */
1491 2393 : rf_flag = s_and( extract_l( L_shr( ind, 7 ) ), 0x1 );
1492 2393 : assert( rf_flag == ( ( ind >> 7 ) & 0x1 ) );
1493 2393 : IF( rf_flag != 0 )
1494 : {
1495 : /* read the fec offset at which the partial copy is received */
1496 944 : ind = get_indice_preview( bitstream, bitstreamSize, sub( bitstreamSize, 5 ), 2 );
1497 944 : IF( ind == 0 ) *partialCopyOffset = 2;
1498 776 : ELSE IF( EQ_32( ind, 1 ) ) *partialCopyOffset = 3;
1499 742 : ELSE IF( EQ_32( ind, 2 ) ) *partialCopyOffset = 5;
1500 663 : ELSE IF( EQ_32( ind, 3 ) ) *partialCopyOffset = 7;
1501 944 : move16();
1502 : /* the last three bits in a packet is the RF frame type */
1503 944 : *partialCopyFrameType = get_indice_preview( bitstream, bitstreamSize, bitstreamSize - 3, 3 );
1504 944 : move16();
1505 : }
1506 : }
1507 17523 : }
1508 :
1509 410 : void dtx_read_padding_bits_fx(
1510 : DEC_CORE_HANDLE st,
1511 : const Word16 num_bits )
1512 : {
1513 : /* TODO: temporary hack, need to decide what to do with core-coder bitrate */
1514 : Word32 tmp;
1515 :
1516 410 : tmp = st->total_brate;
1517 410 : move32();
1518 410 : st->total_brate = L_add( st->total_brate, L_mult0( num_bits, FRAMES_PER_SEC ) );
1519 410 : move32();
1520 410 : get_next_indice_fx( st, num_bits );
1521 410 : st->total_brate = tmp;
1522 410 : move32();
1523 :
1524 410 : return;
1525 : }
|