LCOV - code coverage report
Current view: top level - lib_com - bitstream_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 3b2f07138c61dcf997bbf4165d0882f794b2995f Lines: 320 620 51.6 %
Date: 2025-05-03 01:55:50 Functions: 20 29 69.0 %

          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             : #ifndef HARM_PUSH_BIT
     246             : void push_indice_fx(
     247             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder state structure */
     248             :     Word16 id,             /* i  : ID of the indice */
     249             :     UWord16 value,         /* i  : value of the quantized indice */
     250             :     Word16 nb_bits         /* i  : number of bits used to quantize the indice */
     251             : )
     252             : {
     253             :     Word16 i;
     254             : 
     255             : 
     256             :     IF( EQ_16( hBstr->last_ind_fx, id ) )
     257             :     {
     258             :         /* indice with the same name as the previous one */
     259             :         i = hBstr->next_ind_fx;
     260             :         move16();
     261             :     }
     262             :     ELSE
     263             :     {
     264             :         /* new indice - find an empty slot in the list */
     265             :         i = id;
     266             :         move16();
     267             :         WHILE( hBstr->ind_list[i].nb_bits != -1 )
     268             :         {
     269             :             i = add( i, 1 );
     270             :         }
     271             :     }
     272             : 
     273             :     /* store the values in the list */
     274             :     hBstr->ind_list[i].value = value;
     275             :     move16();
     276             :     hBstr->ind_list[i].nb_bits = nb_bits;
     277             :     move16();
     278             : 
     279             :     /* updates */
     280             :     hBstr->next_ind_fx = add( i, 1 );
     281             :     move16();
     282             :     hBstr->last_ind_fx = id;
     283             :     move16();
     284             :     hBstr->nb_bits_tot = add( hBstr->nb_bits_tot, nb_bits );
     285             :     move16();
     286             : 
     287             :     return;
     288             : }
     289             : /*-------------------------------------------------------------------*
     290             :  * push_next_indice()            *
     291             :  * Push a new indice into the buffer at the next position
     292             :  *-------------------------------------------------------------------*/
     293             : 
     294             : void push_next_indice_fx(
     295             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder state structure */
     296             :     UWord16 value,         /* i  : value of the quantized indice */
     297             :     Word16 nb_bits         /* i  : number of bits used to quantize the indice */
     298             : )
     299             : {
     300             : 
     301             :     /* store the values in the list */
     302             :     hBstr->ind_list[hBstr->next_ind_fx].value = value;
     303             :     move16();
     304             :     hBstr->ind_list[hBstr->next_ind_fx].nb_bits = nb_bits;
     305             :     move16();
     306             :     hBstr->next_ind_fx = add( hBstr->next_ind_fx, 1 );
     307             :     move16();
     308             : 
     309             :     /* update the total number of bits already written */
     310             :     hBstr->nb_bits_tot = add( hBstr->nb_bits_tot, nb_bits );
     311             :     move16();
     312             : 
     313             :     return;
     314             : }
     315             : 
     316             : 
     317             : /*-------------------------------------------------------------------*
     318             :  * push_next_bits()
     319             :  * Push a bit buffer into the buffer at the next position
     320             :  *-------------------------------------------------------------------*/
     321             : 
     322             : void push_next_bits_fx(
     323             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder state structure */
     324             :     Word16 bits[],         /* i  : bit buffer to pack, sequence of single bits */
     325             :     Word16 nb_bits         /* i  : number of bits to pack */
     326             : )
     327             : {
     328             :     UWord16 code;
     329             :     Word16 i, nb_bits_m15;
     330             :     Indice *ptr;
     331             : 
     332             :     ptr = &hBstr->ind_list[hBstr->next_ind_fx];
     333             :     nb_bits_m15 = sub( nb_bits, 15 );
     334             :     i = 0;
     335             :     move16();
     336             :     IF( nb_bits_m15 > 0 )
     337             :     {
     338             :         FOR( ; i < nb_bits_m15; i += 16 )
     339             :         {
     340             :             code = s_or( lshl( bits[i], 15 ), s_or( lshl( bits[i + 1], 14 ), s_or( lshl( bits[i + 2], 13 ), s_or( lshl( bits[i + 3], 12 ),
     341             :                                                                                                                   s_or( lshl( bits[i + 4], 11 ), s_or( lshl( bits[i + 5], 10 ), s_or( lshl( bits[i + 6], 9 ), s_or( lshl( bits[i + 7], 8 ),
     342             :                                                                                                                                                                                                                     s_or( lshl( bits[i + 8], 7 ), s_or( lshl( bits[i + 9], 6 ), s_or( lshl( bits[i + 10], 5 ), s_or( lshl( bits[i + 11], 4 ),
     343             :                                                                                                                                                                                                                                                                                                                      s_or( lshl( bits[i + 12], 3 ), s_or( lshl( bits[i + 13], 2 ), s_or( lshl( bits[i + 14], 1 ), bits[i + 15] ) ) ) ) ) ) ) ) ) ) ) ) ) ) );
     344             : 
     345             :             ptr->value = code;
     346             :             move16();
     347             :             ptr->nb_bits = 16;
     348             :             move16();
     349             :             ++ptr;
     350             :         }
     351             :     }
     352             :     IF( LT_16( i, nb_bits ) )
     353             :     {
     354             :         FOR( ; i < nb_bits; ++i )
     355             :         {
     356             :             ptr->value = bits[i];
     357             :             move16();
     358             :             ptr->nb_bits = 1;
     359             :             move16();
     360             :             ++ptr;
     361             :         }
     362             :     }
     363             :     hBstr->next_ind_fx = (Word16) ( ptr - hBstr->ind_list );
     364             :     move16();
     365             :     hBstr->nb_bits_tot = add( hBstr->nb_bits_tot, nb_bits );
     366             :     move16();
     367             : }
     368             : #endif
     369             : 
     370             : /*-------------------------------------------------------------------*
     371             :  * get_next_indice_fx( )
     372             :  *
     373             :  * Get the next indice from the buffer
     374             :  *-------------------------------------------------------------------*/
     375             : 
     376    38690220 : UWord16 get_next_indice_fx(                       /* o  : value of the indice */
     377             :                             Decoder_State *st_fx, /* i/o: decoder state structure */
     378             :                             Word16 nb_bits        /* i  : number of bits that were used to quantize the indice */
     379             : )
     380             : {
     381             :     UWord16 value;
     382             :     Word16 i;
     383             :     Word16 nbits_total;
     384             : 
     385    38690220 :     assert( nb_bits <= 16 );
     386    38690220 :     value = 0;
     387    38690220 :     move16();
     388             : 
     389    38690220 :     nbits_total = extract_l( Mpy_32_32( st_fx->total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
     390             :     /* detect corrupted bitstream */
     391    38690220 :     IF( GT_16( add( st_fx->next_bit_pos, nb_bits ), nbits_total ) )
     392             :     {
     393           0 :         st_fx->BER_detect = 1;
     394           0 :         move16();
     395           0 :         return ( 0 );
     396             :     }
     397             : 
     398   180700861 :     FOR( i = 0; i < nb_bits; i++ )
     399             :     {
     400   142010641 :         value = (UWord16) L_shl( value, 1 );
     401   142010641 :         value = (UWord16) L_add( value, st_fx->bit_stream[add( st_fx->next_bit_pos, i )] );
     402             :     }
     403             : 
     404             :     /* update the position in the bitstream */
     405    38690220 :     st_fx->next_bit_pos = add( st_fx->next_bit_pos, nb_bits );
     406    38690220 :     move16();
     407    38690220 :     return value;
     408             : }
     409             : 
     410             : /*-------------------------------------------------------------------*
     411             :  * get_next_indice_1_fx( )
     412             :  *
     413             :  * Get the next 1-bit indice from the buffer
     414             :  *-------------------------------------------------------------------*/
     415             : 
     416   173401235 : UWord16 get_next_indice_1_fx(                      /* o  : value of the indice */
     417             :                               Decoder_State *st_fx /* i/o: decoder state structure */
     418             : )
     419             : {
     420             :     Word16 nbits_total;
     421   173401235 :     nbits_total = extract_l( Mpy_32_32( st_fx->total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
     422             :     /* detect corrupted bitstream */
     423   173401235 :     test();
     424   173401235 :     test();
     425   173401235 :     test();
     426   173401235 :     IF( ( GT_16( add( st_fx->next_bit_pos, 1 ), nbits_total ) && EQ_16( st_fx->codec_mode, MODE1 ) ) ||
     427             :         ( 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 */
     428             :     )
     429             :     {
     430           0 :         st_fx->BER_detect = 1;
     431           0 :         move16();
     432           0 :         return ( 0 );
     433             :     }
     434             : 
     435   173401235 :     return st_fx->bit_stream[st_fx->next_bit_pos++];
     436             : }
     437             : 
     438             : /*-------------------------------------------------------------------*
     439             :  * get_next_indice_tmp()
     440             :  *
     441             :  * update the total number of bits and the position in the bitstream
     442             :  *-------------------------------------------------------------------*/
     443             : 
     444     1313002 : void get_next_indice_tmp_fx(
     445             :     Decoder_State *st_fx, /* o  : decoder state structure */
     446             :     Word16 nb_bits        /* i  : number of bits that were used to quantize the indice */
     447             : )
     448             : {
     449             :     /* update the position in the bitstream */
     450     1313002 :     st_fx->next_bit_pos = add( st_fx->next_bit_pos, nb_bits );
     451     1313002 :     move16();
     452     1313002 : }
     453             : 
     454             : /*-------------------------------------------------------------------*
     455             :  * get_indice_fx( )
     456             :  *
     457             :  * Get indice at specific position in the buffer
     458             :  *-------------------------------------------------------------------*/
     459             : 
     460           0 : UWord16 get_indice_fx(                       /* o  : value of the indice */
     461             :                        Decoder_State *st_fx, /* i/o: decoder state structure */
     462             :                        Word16 pos,           /* i  : absolute position in the bitstream (update after the read) */
     463             :                        Word16 nb_bits        /* i  : number of bits that were used to quantize the indice */
     464             : )
     465             : {
     466             :     UWord16 value;
     467             :     Word16 i;
     468             : 
     469           0 :     assert( nb_bits <= 16 );
     470             : 
     471             :     /* detect corrupted bitstream */
     472           0 :     IF( GT_16( add( pos, nb_bits ), st_fx->total_num_bits ) )
     473             :     {
     474           0 :         st_fx->BER_detect = 1;
     475           0 :         move16();
     476           0 :         return ( 0 );
     477             :     }
     478             : 
     479           0 :     value = 0;
     480           0 :     move16();
     481           0 :     FOR( i = 0; i < nb_bits; i++ )
     482             :     {
     483           0 :         value = lshl( value, 1 );
     484           0 :         value = add( value, st_fx->bit_stream[pos + i] );
     485             :     }
     486             : 
     487           0 :     return value;
     488             : }
     489             : 
     490             : /*-------------------------------------------------------------------*
     491             :  * get_indice_1_fx( )
     492             :  *
     493             :  * Get a 1-bit indice at specific position in the buffer
     494             :  *-------------------------------------------------------------------*/
     495             : 
     496    64388685 : UWord16 get_indice_1_fx(                       /* o  : value of the indice */
     497             :                          Decoder_State *st_fx, /* i/o: decoder state structure */
     498             :                          Word16 pos            /* i  : absolute position in the bitstream (update after the read) */
     499             : )
     500             : {
     501             :     Word16 nbits_total;
     502    64388685 :     nbits_total = extract_l( Mpy_32_32( st_fx->total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
     503             :     /* detect corrupted bitstream */
     504    64388685 :     IF( GT_16( add( pos, 1 ), nbits_total ) )
     505             :     {
     506           0 :         st_fx->BER_detect = 1;
     507           0 :         move16();
     508           0 :         return ( 0 );
     509             :     }
     510             : 
     511    64388685 :     return st_fx->bit_stream[pos];
     512             : }
     513             : 
     514             : /*-------------------------------------------------------------------*
     515             :  * reset_indices_enc_fx()
     516             :  *
     517             :  * Reset the buffer of indices
     518             :  *-------------------------------------------------------------------*/
     519             : 
     520     1954305 : void reset_indices_enc_fx(
     521             :     BSTR_ENC_HANDLE hBstr,       /* i/o: encoder state structure */
     522             :     const Word16 max_num_indices /* i  : max number of indices       */
     523             : )
     524             : {
     525             :     Word16 i;
     526             : 
     527     1954305 :     hBstr->nb_ind_tot = 0;
     528     1954305 :     move16();
     529     1954305 :     hBstr->nb_bits_tot = 0;
     530     1954305 :     move16();
     531             : #ifndef HARM_PUSH_BIT
     532             :     hBstr->next_ind_fx = 0;
     533             :     move16();
     534             :     hBstr->last_ind_fx = -1;
     535             :     move16();
     536             : #endif
     537   281114931 :     FOR( i = 0; i < max_num_indices; i++ )
     538             :     {
     539   279160626 :         hBstr->ind_list[i].nb_bits = -1;
     540   279160626 :         move16();
     541             :     }
     542             : 
     543     1954305 :     return;
     544             : }
     545             : 
     546             : /*-------------------------------------------------------------------*
     547             :  * reset_indices_dec_fx()
     548             :  *
     549             :  * Reset the buffer of decoder indices
     550             :  *-------------------------------------------------------------------*/
     551             : 
     552           0 : void reset_indices_dec_fx(
     553             :     Decoder_State *st_fx )
     554             : {
     555           0 :     st_fx->next_bit_pos = 0;
     556           0 :     move16();
     557             : 
     558           0 :     return;
     559             : }
     560             : 
     561             : 
     562             : /*-------------------------------------------------------------------*
     563             :  * write_indices_fx()
     564             :  *
     565             :  * Write the buffer of indices to a file
     566             :  *-------------------------------------------------------------------*/
     567             : #ifndef HARM_PUSH_BIT
     568             : void write_indices_fx(
     569             :     Encoder_State *st_fx,  /* i/o: encoder state structure */
     570             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder state structure */
     571             :     FILE *file             /* i  : output bitstream file   */
     572             :     ,
     573             :     UWord8 *pFrame,    /* i: byte array with bit packet and byte aligned coded speech data */
     574             :     Word16 pFrame_size /* i: size of the binary encoded access unit [bits] */
     575             : )
     576             : {
     577             :     Word16 i, k;
     578             :     Word16 stream[2 + MAX_BITS_PER_FRAME], *pt_stream;
     579             :     Word32 mask;
     580             :     UWord8 header;
     581             :     Word16 isAmrWb = 0;
     582             :     move16();
     583             : 
     584             :     IF( st_fx->bitstreamformat == G192 )
     585             :     {
     586             :         /*-----------------------------------------------------------------*
     587             :          * Encode Sync Header and Frame Length
     588             :          *-----------------------------------------------------------------*/
     589             :         pt_stream = stream;
     590             :         FOR( i = 0; i < ( 2 + MAX_BITS_PER_FRAME ); ++i )
     591             :         {
     592             :             stream[i] = 0;
     593             :             move16();
     594             :         }
     595             :         *pt_stream++ = SYNC_GOOD_FRAME;
     596             :         move16();
     597             :         *pt_stream++ = hBstr->nb_bits_tot;
     598             :         move16();
     599             : 
     600             :         /*----------------------------------------------------------------*
     601             :          * Bitstream packing (conversion of individual indices into a serial stream)
     602             :          * Writing the serial stream into file
     603             :          *----------------------------------------------------------------*/
     604             : 
     605             :         FOR( i = 0; i < MAX_NUM_INDICES; i++ )
     606             :         {
     607             :             IF( NE_16( hBstr->ind_list[i].nb_bits, -1 ) )
     608             :             {
     609             :                 /* mask from MSB to LSB */
     610             :                 mask = L_shl( 1, sub( hBstr->ind_list[i].nb_bits, 1 ) );
     611             : 
     612             :                 /* write bit by bit */
     613             :                 FOR( k = 0; k < hBstr->ind_list[i].nb_bits; k++ )
     614             :                 {
     615             :                     IF( L_and( hBstr->ind_list[i].value, mask ) )
     616             :                     {
     617             :                         *pt_stream++ = G192_BIN1;
     618             :                         move16();
     619             :                     }
     620             :                     ELSE
     621             :                     {
     622             :                         *pt_stream++ = G192_BIN0;
     623             :                         move16();
     624             :                     }
     625             : 
     626             :                     mask = L_shr( mask, 1 );
     627             :                 }
     628             :             }
     629             :         }
     630             :     }
     631             :     ELSE
     632             :     {
     633             :         /* Create and write ToC header */
     634             :         /*  qbit always  set to  1 on encoder side  for AMRWBIO ,  no qbit in use for EVS, but set to 0(bad)  */
     635             :         header = (UWord8) ( s_or( s_or( shl( st_fx->Opt_AMR_WB, 5 ), shl( st_fx->Opt_AMR_WB, 4 ) ), rate2EVSmode( L_mult0( hBstr->nb_bits_tot, 50 ), &isAmrWb ) ) );
     636             :         move16();
     637             :         fwrite( &header, sizeof( UWord8 ), 1, file );
     638             :         /* Write speech bits */
     639             :         fwrite( pFrame, sizeof( UWord8 ), shr( add( pFrame_size, 7 ), 3 ), file );
     640             :     }
     641             : 
     642             :     /* Clearing of indices */
     643             :     FOR( i = 0; i < MAX_NUM_INDICES; i++ )
     644             :     {
     645             :         hBstr->ind_list[i].nb_bits = -1;
     646             :         move16();
     647             :     }
     648             : 
     649             : 
     650             :     IF( st_fx->bitstreamformat == G192 )
     651             :     {
     652             :         /* write the serial stream into file */
     653             :         fwrite( stream, sizeof( unsigned short ), 2 + stream[1], file );
     654             :     }
     655             :     /* reset index pointers */
     656             :     hBstr->nb_bits_tot = 0;
     657             :     move16();
     658             :     hBstr->next_ind_fx = 0;
     659             :     move16();
     660             :     hBstr->last_ind_fx = -1;
     661             :     move16();
     662             : 
     663             :     return;
     664             : }
     665             : #endif
     666             : /*-------------------------------------------------------------------*
     667             :  * write_indices_buf_fx()
     668             :  *
     669             :  * Write the buffer of indices to a file
     670             :  *-------------------------------------------------------------------*/
     671             : #ifndef HARM_PUSH_BIT
     672             : void write_indices_buf_fx(
     673             :     Encoder_State *st_fx,  /* i/o: encoder state structure */
     674             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder state structure */
     675             :     UWord16 *out_buf,      /* i  : output bitstream buf   */
     676             :     UWord8 *pFrame,        /* i: byte array with bit packet and byte aligned coded speech data */
     677             :     Word16 pFrame_size,    /* i: size of the binary encoded access unit [bits] */
     678             :     UWord16 *num_bits )
     679             : {
     680             :     Word16 i, k;
     681             :     Word16 stream[2 + MAX_BITS_PER_FRAME], *pt_stream;
     682             :     Word32 mask;
     683             :     UWord8 header;
     684             :     Word16 isAmrWb = 0;
     685             : 
     686             :     IF( st_fx->bitstreamformat == G192 )
     687             :     {
     688             :         /*-----------------------------------------------------------------*
     689             :          * Encode Sync Header and Frame Length
     690             :          *-----------------------------------------------------------------*/
     691             :         pt_stream = stream;
     692             :         FOR( i = 0; i < ( 2 + MAX_BITS_PER_FRAME ); ++i )
     693             :         {
     694             :             stream[i] = 0;
     695             :             move16();
     696             :         }
     697             :         //*pt_stream++ = (Word16) SYNC_GOOD_FRAME;
     698             :         //*pt_stream++ = hBstr->nb_bits_tot;
     699             :         *num_bits = hBstr->nb_bits_tot;
     700             :         move16();
     701             :         /*----------------------------------------------------------------*
     702             :          * Bitstream packing (conversion of individual indices into a serial stream)
     703             :          * Writing the serial stream into file
     704             :          *----------------------------------------------------------------*/
     705             :         FOR( i = 0; i < MAX_NUM_INDICES; i++ )
     706             :         {
     707             :             IF( NE_16( hBstr->ind_list[i].nb_bits, -1 ) )
     708             :             {
     709             :                 /* mask from MSB to LSB */
     710             :                 mask = L_shl( 1, ( sub( hBstr->ind_list[i].nb_bits, 1 ) ) );
     711             : 
     712             :                 /* write bit by bit */
     713             :                 FOR( k = 0; k < hBstr->ind_list[i].nb_bits; k++ )
     714             :                 {
     715             :                     IF( L_and( hBstr->ind_list[i].value, mask ) )
     716             :                     {
     717             :                         //*pt_stream++ = G192_BIN1;
     718             :                         *pt_stream++ = 1;
     719             :                         move16();
     720             :                     }
     721             :                     ELSE
     722             :                     {
     723             :                         //*pt_stream++ = G192_BIN0;
     724             :                         *pt_stream++ = 0;
     725             :                         move16();
     726             :                     }
     727             : 
     728             :                     mask = L_shr( mask, 1 );
     729             :                 }
     730             :             }
     731             :         }
     732             :     }
     733             :     ELSE
     734             :     {
     735             :         /* Create and write ToC header */
     736             :         /*  qbit always  set to  1 on encoder side  for AMRWBIO ,  no qbit in use for EVS, but set to 0(bad)  */
     737             :         header = (UWord8) ( s_or( s_or( shl( st_fx->Opt_AMR_WB, 5 ), shl( st_fx->Opt_AMR_WB, 4 ) ), rate2EVSmode( i_mult( hBstr->nb_bits_tot, 50 ), &isAmrWb ) ) );
     738             :         // fwrite(&header, sizeof(UWord8), 1, file);
     739             :         memcpy( out_buf, &header, sizeof( UWord8 ) );
     740             :         *num_bits += sizeof( UWord8 );
     741             :         /* Write speech bits */
     742             :         // fwrite(pFrame, sizeof(UWord8), (pFrame_size + 7) >> 3, file);
     743             :         memcpy( out_buf, pFrame, sizeof( UWord8 ) * ( shr( add( pFrame_size, 7 ), 3 ) ) );
     744             :         *num_bits += sizeof( UWord8 ) * ( shr( ( add( pFrame_size, 7 ) ), 3 ) );
     745             :     }
     746             : 
     747             :     /* Clearing of indices */
     748             :     FOR( i = 0; i < MAX_NUM_INDICES; i++ )
     749             :     {
     750             :         hBstr->ind_list[i].nb_bits = -1;
     751             :         move16();
     752             :     }
     753             : 
     754             : 
     755             :     IF( st_fx->bitstreamformat == G192 )
     756             :     {
     757             :         /* write the serial stream into file */
     758             :         // fwrite(stream, sizeof(unsigned short), 2 + stream[1], file);
     759             :         // FILE *ftemp = fopen( "./output/bitstreams/out.COD", "ab" );
     760             :         // fwrite( stream, sizeof( unsigned short ), 2 + stream[1], ftemp );
     761             :         // fclose( ftemp );
     762             :         memcpy( out_buf, stream, sizeof( unsigned short ) * ( *num_bits ) );
     763             :         //*num_bits += sizeof( unsigned short ) * ( 2 + stream[1] );
     764             :     }
     765             :     /* reset index pointers */
     766             :     hBstr->nb_bits_tot = 0;
     767             :     hBstr->next_ind_fx = 0;
     768             :     hBstr->last_ind_fx = -1;
     769             :     move16();
     770             :     move16();
     771             :     move16();
     772             : 
     773             :     return;
     774             : }
     775             : #endif
     776             : /*-------------------------------------------------------------------*
     777             :  * indices_to_serial()
     778             :  *
     779             :  * pack indices into serialized payload format
     780             :  *-------------------------------------------------------------------*/
     781             : 
     782           0 : void indices_to_serial(
     783             :     const Encoder_State *st_fx, /* i: encoder state structure */
     784             :     BSTR_ENC_HANDLE hBstr,      /* i/o: encoder state structure */
     785             :     UWord8 *pFrame,             /* o: byte array with bit packet and byte aligned coded speech data */
     786             :     Word16 *pFrame_size         /* o: size of the binary encoded access unit [bits] */
     787             : )
     788             : {
     789             :     Word16 i, k, j;
     790           0 :     Word16 cmi = 0, core_mode = 0;
     791             :     Word32 mask;
     792             :     Word16 amrwb_bits[( ACELP_23k85 / 50 )];
     793           0 :     UWord8 omask = 0x80;
     794           0 :     UWord8 *pt_pFrame = pFrame;
     795           0 :     Word16 isAmrWb = 0;
     796           0 :     move16();
     797           0 :     move16();
     798           0 :     move16();
     799           0 :     move16();
     800             : 
     801           0 :     IF( st_fx->Opt_AMR_WB )
     802             :     {
     803           0 :         cmi = rate2EVSmode( st_fx->total_brate, &isAmrWb );
     804           0 :         core_mode = rate2EVSmode( L_mult0( hBstr->nb_bits_tot, 50 ), &isAmrWb );
     805             : 
     806           0 :         j = 0;
     807           0 :         move16();
     808           0 :         FOR( i = 0; i < MAX_NUM_INDICES; i++ )
     809             :         {
     810           0 :             IF( NE_16( hBstr->ind_list[i].nb_bits, -1 ) )
     811             :             {
     812             :                 /* mask from MSB to LSB */
     813           0 :                 mask = L_shl( 1, sub( hBstr->ind_list[i].nb_bits, 1 ) );
     814             : 
     815             :                 /* temporarily save bit */
     816           0 :                 FOR( k = 0; k < hBstr->ind_list[i].nb_bits; k++ )
     817             :                 {
     818           0 :                     amrwb_bits[j++] = L_and( hBstr->ind_list[i].value, mask ) > 0;
     819           0 :                     move16();
     820           0 :                     mask = L_shr( mask, 1 );
     821             :                 }
     822             :             }
     823             :         }
     824             :     }
     825             : 
     826           0 :     *pFrame_size = hBstr->nb_bits_tot;
     827           0 :     move16();
     828             : 
     829             :     /*----------------------------------------------------------------*
     830             :      * Bitstream packing (conversion of individual indices into a serial stream)
     831             :      *----------------------------------------------------------------*/
     832           0 :     j = 0;
     833           0 :     move16();
     834           0 :     FOR( i = 0; i < MAX_NUM_INDICES; i++ )
     835             :     {
     836           0 :         IF( NE_16( hBstr->ind_list[i].nb_bits, -1 ) )
     837             :         {
     838             :             /* mask from MSB to LSB */
     839           0 :             mask = L_shl( 1, sub( hBstr->ind_list[i].nb_bits, 1 ) );
     840             : 
     841             :             /* write bit by bit */
     842           0 :             FOR( k = 0; k < hBstr->ind_list[i].nb_bits; k++ )
     843             :             {
     844           0 :                 IF( st_fx->Opt_AMR_WB )
     845             :                 {
     846           0 :                     pack_bit( amrwb_bits[sort_ptr[core_mode][j++]], &pt_pFrame, &omask );
     847             :                 }
     848             :                 ELSE
     849             :                 {
     850           0 :                     pack_bit( hBstr->ind_list[i].value & mask, &pt_pFrame, &omask );
     851           0 :                     j = add( j, 1 );
     852             :                 }
     853           0 :                 mask = L_shr( mask, 1 );
     854             :             }
     855             :         }
     856             :     }
     857             : 
     858           0 :     test();
     859           0 :     IF( st_fx->Opt_AMR_WB && EQ_16( core_mode, AMRWB_IO_SID ) ) /* SID UPD frame always written now  .... */
     860             :     {
     861             :         /* insert STI bit and CMI */
     862           0 :         pack_bit( 1, &pt_pFrame, &omask );
     863           0 :         FOR( mask = 0x08; mask > 0; mask >>= 1 )
     864             :         {
     865           0 :             pack_bit( cmi & mask, &pt_pFrame, &omask );
     866             :         }
     867             :     }
     868           0 : }
     869             : 
     870             : 
     871             : /*-------------------------------------------------------------------*
     872             :  * indices_to_serial_generic()
     873             :  *
     874             :  * pack indices into serialized payload format
     875             :  *-------------------------------------------------------------------*/
     876             : 
     877           0 : void indices_to_serial_generic(
     878             :     const Indice *ind_list,   /* i: indices list */
     879             :     const Word16 num_indices, /* i: number of indices to write */
     880             :     UWord8 *pFrame,           /* o: byte array with bit packet and byte aligned coded speech data */
     881             :     Word16 *pFrame_size       /* i/o: number of bits in the binary encoded access unit [bits] */
     882             : )
     883             : {
     884             :     Word16 i, k, j;
     885             :     Word32 mask;
     886             :     UWord8 omask;
     887           0 :     UWord8 *pt_pFrame = pFrame;
     888             :     Word16 nb_bits_tot;
     889             : 
     890           0 :     nb_bits_tot = 0;
     891           0 :     move16();
     892           0 :     omask = (UWord8) shr( 0x80, s_and( *pFrame_size, 0x7 ) );
     893           0 :     move16();
     894           0 :     pt_pFrame += shr( *pFrame_size, 3 );
     895             : 
     896             :     /*----------------------------------------------------------------*
     897             :      * Bitstream packing (conversion of individual indices into a serial stream)
     898             :      *----------------------------------------------------------------*/
     899           0 :     j = 0;
     900           0 :     move16();
     901           0 :     FOR( i = 0; i < num_indices; i++ ){
     902           0 :         IF( NE_16( ind_list[i].nb_bits, -1 ) ){
     903             :             /* mask from MSB to LSB */
     904           0 :             mask = L_shl( 1, sub( ind_list[i].nb_bits, 1 ) );
     905             : 
     906             :     /* write bit by bit */
     907           0 :     FOR( k = 0; k < ind_list[i].nb_bits; k++ )
     908             :     {
     909           0 :         pack_bit( ind_list[i].value & mask, &pt_pFrame, &omask );
     910           0 :         j = add( j, 1 );
     911           0 :         mask = L_shr( mask, 1 );
     912             :     }
     913           0 :     nb_bits_tot = add( nb_bits_tot, ind_list[i].nb_bits );
     914             : }
     915             : }
     916             : 
     917           0 : *pFrame_size = add( *pFrame_size, nb_bits_tot );
     918           0 : move16();
     919             : 
     920           0 : return;
     921             : }
     922             : 
     923             : 
     924        1050 : static void dec_prm_core( Decoder_State *st )
     925             : {
     926             :     Word16 n, frame_size_index, num_bits;
     927             :     UWord16 lsb;
     928             :     Word32 L_tmp;
     929             : 
     930        1050 :     frame_size_index = -1;
     931        1050 :     move16();
     932        1050 :     st->core = -1;
     933        1050 :     move16();
     934             : 
     935        1050 :     IF( st->total_brate == FRAME_NO_DATA )
     936             :     {
     937           0 :         st->m_frame_type = ZERO_FRAME;
     938           0 :         move16();
     939             :     }
     940        1050 :     ELSE IF( EQ_32( st->total_brate, SID_2k40 ) )
     941             :     {
     942           0 :         st->m_frame_type = SID_FRAME;
     943           0 :         move16();
     944             :     }
     945             :     ELSE
     946             :     {
     947        1050 :         st->m_frame_type = ACTIVE_FRAME;
     948        1050 :         move16();
     949        1050 :         Mpy_32_16_ss( st->total_brate, 5243, &L_tmp, &lsb ); /* 5243 is 1/50 in Q18. (0+18-15=3) */
     950        1050 :         num_bits = extract_l( L_shr( L_tmp, 3 ) );           /* Q0 */
     951        1050 :         assert( num_bits == st->total_brate / 50 );
     952        8400 :         FOR( n = 0; n < FRAME_SIZE_NB; ++n )
     953             :         {
     954        8400 :             IF( EQ_16( FrameSizeConfig[n].frame_bits, num_bits ) )
     955             :             {
     956        1050 :                 frame_size_index = n;
     957        1050 :                 move16();
     958        1050 :                 BREAK;
     959             :             }
     960             :         }
     961             : 
     962             :         /* Get bandwidth mode */
     963        1050 :         st->bwidth = get_next_indice_fx( st, FrameSizeConfig[frame_size_index].bandwidth_bits );
     964        1050 :         move16();
     965             : 
     966        1050 :         st->bwidth = add( st->bwidth, FrameSizeConfig[frame_size_index].bandwidth_min );
     967        1050 :         move16();
     968             : 
     969        1050 :         IF( GT_16( st->bwidth, FB ) )
     970             :         {
     971           0 :             st->bwidth = FB;
     972           0 :             move16();
     973           0 :             st->BER_detect = 1;
     974           0 :             move16();
     975             :         }
     976             : 
     977        1050 :         test();
     978        1050 :         IF( GT_16( st->bwidth, SWB ) && LT_32( st->total_brate, ACELP_16k40 ) )
     979             :         {
     980           0 :             st->bwidth = SWB;
     981           0 :             move16();
     982           0 :             st->BER_detect = 1;
     983           0 :             move16();
     984             :         }
     985             : 
     986             :         /* Skip reserved bit */
     987        1050 :         get_next_indice_tmp_fx( st, FrameSizeConfig[frame_size_index].reserved_bits );
     988             : 
     989        1050 :         IF( get_next_indice_1_fx( st ) != 0 ) /* TCX */
     990             :         {
     991         440 :             st->core = TCX_20_CORE;
     992         440 :             move16();
     993         440 :             if ( get_next_indice_1_fx( st ) != 0 )
     994             :             {
     995          66 :                 st->core = HQ_CORE;
     996          66 :                 move16();
     997             :             }
     998             :         }
     999             :         ELSE /* ACELP */
    1000             :         {
    1001         610 :             st->core = ACELP_CORE;
    1002         610 :             move16();
    1003             :         }
    1004             :     }
    1005        1050 : }
    1006             : 
    1007             : /*-----------------------------------------------------------------*
    1008             :  * decision_matrix_core_dec()
    1009             :  *
    1010             :  * Read core mode signalling bits from the bitstream
    1011             :  * Set st->core, and st->bwidth if signalled together with the core.
    1012             :  *-----------------------------------------------------------------*/
    1013             : 
    1014        1050 : static void decision_matrix_core_dec(
    1015             :     Decoder_State *st /* i/o: decoder state structure                   */
    1016             : )
    1017             : {
    1018             :     Word16 start_idx;
    1019             :     Word32 ind;
    1020             :     Word16 nBits;
    1021             : 
    1022        1050 :     assert( st->bfi != 1 );
    1023             : 
    1024        1050 :     st->core = -1;
    1025        1050 :     move16();
    1026        1050 :     st->bwidth = -1;
    1027        1050 :     move16();
    1028             : 
    1029        1050 :     test();
    1030        1050 :     IF( ( st->total_brate == FRAME_NO_DATA ) || EQ_32( st->total_brate, SID_2k40 ) )
    1031             :     {
    1032           0 :         st->core = ACELP_CORE;
    1033           0 :         move16();
    1034             :     }
    1035             :     /* SC-VBR */
    1036        1050 :     ELSE IF( EQ_32( st->total_brate, PPP_NELP_2k80 ) )
    1037             :     {
    1038           0 :         st->core = ACELP_CORE;
    1039           0 :         move16();
    1040           0 :         return;
    1041             :     }
    1042             : 
    1043             :     /*---------------------------------------------------------------------*
    1044             :      * ACELP/HQ core selection
    1045             :      *---------------------------------------------------------------------*/
    1046             : 
    1047        1050 :     test();
    1048        1050 :     IF( LT_32( st->total_brate, ACELP_24k40 ) )
    1049             :     {
    1050        1050 :         st->core = ACELP_CORE;
    1051        1050 :         move16();
    1052             :     }
    1053           0 :     ELSE IF( GE_32( st->total_brate, ACELP_24k40 ) && LE_32( st->total_brate, ACELP_64k ) )
    1054             :     {
    1055             :         /* read the ACELP/HQ core selection bit */
    1056           0 :         st->core = imult1616( get_next_indice_fx( st, 1 ), HQ_CORE );
    1057           0 :         move16();
    1058             :     }
    1059             :     ELSE
    1060             :     {
    1061           0 :         st->core = HQ_CORE;
    1062           0 :         move16();
    1063             :     }
    1064             : 
    1065             :     /*-----------------------------------------------------------------*
    1066             :      * Read ACELP signalling bits from the bitstream
    1067             :      *-----------------------------------------------------------------*/
    1068             : 
    1069        1050 :     IF( st->core == ACELP_CORE )
    1070             :     {
    1071             :         /* find the section in the ACELP signalling table corresponding to bitrate */
    1072        1050 :         start_idx = 0;
    1073        1050 :         move16();
    1074       36750 :         WHILE( acelp_sig_tbl[start_idx] != st->total_brate )
    1075             :         {
    1076       35700 :             start_idx = add( start_idx, 1 );
    1077             :         }
    1078             : 
    1079             :         /* skip the bitrate */
    1080        1050 :         start_idx = add( start_idx, 1 );
    1081             : 
    1082             :         /* retrieve the number of bits */
    1083        1050 :         nBits = extract_l( acelp_sig_tbl[start_idx] );
    1084        1050 :         start_idx = add( start_idx, 1 );
    1085             : 
    1086             :         /* retrieve the signalling indice */
    1087        1050 :         ind = acelp_sig_tbl[start_idx + get_next_indice_fx( st, nBits )];
    1088        1050 :         st->bwidth = extract_l( L_and( L_shr( ind, 3 ), 0x7 ) );
    1089        1050 :         move16();
    1090             : 
    1091             :         /* convert signalling indice into signalling information */
    1092        1050 :         if ( EQ_32( L_and( ind, 0x7 ), LR_MDCT ) )
    1093             :         {
    1094         296 :             st->core = HQ_CORE;
    1095         296 :             move16();
    1096             :         }
    1097             :     }
    1098             : 
    1099             :     /*-----------------------------------------------------------------*
    1100             :      * Read HQ signalling bits from the bitstream
    1101             :      * Set HQ core type
    1102             :      *-----------------------------------------------------------------*/
    1103             : 
    1104        1050 :     IF( EQ_16( st->core, HQ_CORE ) )
    1105             :     {
    1106             :         /* read the HQ/TCX core switching flag */
    1107         296 :         if ( get_next_indice_fx( st, 1 ) != 0 )
    1108             :         {
    1109         264 :             st->core = TCX_20_CORE;
    1110         264 :             move16();
    1111             :         }
    1112             : 
    1113             :         /* For TCX: read/set band-width (needed for different I/O sampling rate support) */
    1114         296 :         test();
    1115         296 :         IF( EQ_16( st->core, TCX_20_CORE ) && GT_32( st->total_brate, ACELP_16k40 ) )
    1116             :         {
    1117           0 :             ind = get_next_indice_fx( st, 2 );
    1118             : 
    1119           0 :             IF( ind == 0 )
    1120             :             {
    1121           0 :                 st->bwidth = NB;
    1122           0 :                 move16();
    1123             :             }
    1124           0 :             ELSE IF( EQ_32( ind, 1 ) )
    1125             :             {
    1126           0 :                 st->bwidth = WB;
    1127           0 :                 move16();
    1128             :             }
    1129           0 :             ELSE IF( EQ_32( ind, 2 ) )
    1130             :             {
    1131           0 :                 st->bwidth = SWB;
    1132           0 :                 move16();
    1133             :             }
    1134             :             ELSE
    1135             :             {
    1136           0 :                 st->bwidth = FB;
    1137           0 :                 move16();
    1138             :             }
    1139             :         }
    1140             :     }
    1141             : 
    1142        1050 :     return;
    1143             : }
    1144             : 
    1145             : /*-------------------------------------------------------------------*
    1146             :  * mdct_switching_dec()
    1147             :  *
    1148             :  * Set up MDCT core switching if indicated in the bit stream
    1149             :  *-------------------------------------------------------------------*/
    1150             : 
    1151        3100 : void mdct_switching_dec_fx(
    1152             :     Decoder_State *st /* i/o: decoder state structure                */
    1153             : )
    1154             : {
    1155        3100 :     if ( !st->bfi )
    1156             :     {
    1157        3100 :         IF( st->Opt_AMR_WB != 0 )
    1158             :         {
    1159           0 :             return;
    1160             :         }
    1161             : 
    1162        3100 :         test();
    1163        3100 :         test();
    1164        3100 :         IF( EQ_32( st->total_brate, ACELP_13k20 ) || EQ_32( st->total_brate, ACELP_32k ) )
    1165             :         {
    1166        1050 :             st->mdct_sw_enable = MODE1;
    1167        1050 :             move16();
    1168             :         }
    1169        2050 :         ELSE IF( LE_32( ACELP_16k40, st->total_brate ) && LE_32( st->total_brate, ACELP_24k40 ) )
    1170             :         {
    1171        1050 :             st->mdct_sw_enable = MODE2;
    1172        1050 :             move16();
    1173             :         }
    1174             : 
    1175        3100 :         test();
    1176        3100 :         test();
    1177        3100 :         IF( EQ_16( st->codec_mode, MODE1 ) && EQ_16( st->mdct_sw_enable, MODE1 ) )
    1178        1050 :         {
    1179             :             /* Read ahead core mode signaling */
    1180             :             Word16 next_bit_pos_save;
    1181             :             Word16 core_save;
    1182             :             Word16 bwidth_save;
    1183             : 
    1184        1050 :             next_bit_pos_save = st->next_bit_pos;
    1185        1050 :             move16();
    1186        1050 :             core_save = st->core;
    1187        1050 :             move16();
    1188        1050 :             bwidth_save = st->bwidth;
    1189        1050 :             move16();
    1190             : 
    1191        1050 :             decision_matrix_core_dec( st ); /* sets st->core */
    1192             : 
    1193        1050 :             IF( EQ_16( st->core, TCX_20_CORE ) )
    1194             :             {
    1195             :                 /* Trigger TCX */
    1196         264 :                 st->codec_mode = MODE2;
    1197         264 :                 move16();
    1198         264 :                 st->mdct_sw = MODE1;
    1199         264 :                 move16();
    1200             :             }
    1201             :             ELSE
    1202             :             {
    1203             :                 /* Rewind bitstream */
    1204         786 :                 st->next_bit_pos = next_bit_pos_save;
    1205         786 :                 move16();
    1206         786 :                 IF( st->bfi != 0 )
    1207             :                 {
    1208           0 :                     st->core = core_save;
    1209           0 :                     move16();
    1210           0 :                     st->bwidth = bwidth_save;
    1211           0 :                     move16();
    1212             :                 }
    1213             :             }
    1214             :         }
    1215        2050 :         ELSE IF( EQ_16( st->codec_mode, MODE2 ) && EQ_16( st->mdct_sw_enable, MODE2 ) )
    1216             :         {
    1217             :             /* Read ahead core mode signaling */
    1218             :             Word16 next_bit_pos_save;
    1219             :             Word16 core_save;
    1220             :             Word16 bwidth_save;
    1221             : 
    1222        1050 :             next_bit_pos_save = st->next_bit_pos;
    1223        1050 :             move16();
    1224        1050 :             core_save = st->core;
    1225        1050 :             move16();
    1226        1050 :             bwidth_save = st->bwidth;
    1227        1050 :             move16();
    1228             : 
    1229        1050 :             dec_prm_core( st ); /* sets st->core */
    1230             : 
    1231        1050 :             IF( EQ_16( st->core, HQ_CORE ) )
    1232             :             {
    1233             :                 /* Trigger HQ_CORE */
    1234          66 :                 st->codec_mode = MODE1;
    1235          66 :                 move16();
    1236          66 :                 st->mdct_sw = MODE2;
    1237          66 :                 move16();
    1238             :             }
    1239             :             ELSE
    1240             :             {
    1241             :                 /* Rewind bitstream */
    1242         984 :                 st->next_bit_pos = next_bit_pos_save;
    1243         984 :                 move16();
    1244         984 :                 if ( st->bfi != 0 )
    1245             :                 {
    1246           0 :                     st->core = core_save;
    1247           0 :                     move16();
    1248             :                 }
    1249             :                 /* always reset bwidth, to not interfere with BER logic */
    1250         984 :                 st->bwidth = bwidth_save;
    1251         984 :                 move16();
    1252             :             }
    1253             :         }
    1254             :     }
    1255             : 
    1256        3100 :     return;
    1257             : }
    1258             : 
    1259             : 
    1260             : /*-------------------------------------------------------------------*
    1261             :  * BRATE2IDX_fx()
    1262             :  *
    1263             :  * Convert Bitrate to Index Value
    1264             :  *-------------------------------------------------------------------*/
    1265             : 
    1266       43982 : Word16 BRATE2IDX_fx( Word32 brate )
    1267             : {
    1268             : 
    1269             :     Word32 L_temp;
    1270             :     Word32 L_idx;
    1271             : #define START 9
    1272             :     extern const Word16 bit_rates_div50[];
    1273             : 
    1274             :     /* This is a Fast Bit Rate Value to Index Value Binary Search */
    1275       43982 :     L_temp = L_msu0( brate, bit_rates_div50[START], 50 );
    1276       43982 :     L_temp = L_min( 6, L_max( -6, L_temp ) );
    1277       43982 :     L_idx = L_add( L_temp, START );
    1278       43982 :     L_temp = L_msu0( brate, bit_rates_div50[L_idx], 50 );
    1279       43982 :     L_temp = L_min( 3, L_max( -3, L_temp ) );
    1280       43982 :     L_idx = L_add( L_temp, L_idx );
    1281       43982 :     L_temp = L_msu0( brate, bit_rates_div50[L_idx], 50 );
    1282       43982 :     L_temp = L_min( 1, L_max( -2, L_temp ) );
    1283       43982 :     L_idx = L_add( L_temp, L_idx );
    1284       43982 :     L_temp = L_msu0( brate, bit_rates_div50[L_idx], 50 );
    1285       43982 :     if ( L_temp != 0 )
    1286       14269 :         L_idx = L_add( L_idx, 1 );
    1287       43982 :     return (Word16) L_idx;
    1288             : }
    1289             : 
    1290             : 
    1291             : /*-------------------------------------------------------------------*
    1292             :  * BRATE2IDX16k_fx()
    1293             :  *
    1294             :  * Convert Bitrate to Index Value
    1295             :  *-------------------------------------------------------------------*/
    1296             : 
    1297         114 : Word16 BRATE2IDX16k_fx( Word32 brate )
    1298             : {
    1299             :     Word32 L_temp, L_idx;
    1300             : #define START_16K 5
    1301             :     extern const Word16 bit_rates_16k_div50[];
    1302             : 
    1303         114 :     if ( EQ_32( brate, ACELP_16k40 ) )
    1304             :     {
    1305           0 :         brate = ACELP_14k80;
    1306           0 :         move16();
    1307             :     }
    1308             : 
    1309             :     /* This is a Fast Bit Rate Value to Index Value Binary Search */
    1310         114 :     L_temp = L_msu0( brate, bit_rates_16k_div50[START_16K], 50 );
    1311         114 :     L_temp = L_min( 3, L_max( -3, L_temp ) );
    1312         114 :     L_idx = L_add( L_temp, START_16K );
    1313         114 :     L_temp = L_msu0( brate, bit_rates_16k_div50[L_idx], 50 );
    1314         114 :     L_temp = L_min( 2, L_max( -2, L_temp ) );
    1315         114 :     L_idx = L_add( L_temp, L_idx );
    1316         114 :     L_temp = L_msu0( brate, bit_rates_16k_div50[L_idx], 50 );
    1317         114 :     L_temp = L_min( 1, L_max( -1, L_temp ) );
    1318         114 :     L_idx = L_add( L_temp, L_idx );
    1319             : 
    1320         114 :     return (Word16) L_idx;
    1321             : }
    1322             : 
    1323             : /*-------------------------------------------------------------------*
    1324             :  * BIT_ALLOC_IDX_fx()
    1325             :  *-------------------------------------------------------------------*/
    1326             : 
    1327         719 : Word32 BIT_ALLOC_IDX_fx( Word32 brate, Word16 ctype, Word16 sfrm, Word16 tc )
    1328             : {
    1329             :     Word32 L_temp;
    1330             :     Word16 temp;
    1331         719 :     if ( ctype == INACTIVE ) /* no sub(ctype, INACTIVE) because it is '0' */
    1332           0 :         ctype = GENERIC;
    1333         719 :     move16();
    1334         719 :     L_temp = L_mac0( -1l * 256, 1 * 256, ctype );
    1335             : 
    1336         719 :     temp = BRATE2IDX_fx( brate );
    1337         719 :     L_temp = L_mac0( L_temp, 4 * 256, temp );
    1338         719 :     IF( tc >= 0 )
    1339          24 :     L_temp = L_mac0( L_temp, ( 10 - 4 ) * 256, temp );
    1340             :     /* So either 'temp' x 4 when 'tc < 0', 'temp' x 10 otherwise */
    1341             : 
    1342         719 :     L_temp = L_mac0( L_temp, 1 * 256, s_max( 0, tc ) );
    1343             : 
    1344         719 :     L_temp = L_mac0( L_temp, s_max( 0, sfrm ), 1 );
    1345         719 :     if ( sfrm < 0 )
    1346         695 :         L_temp = L_shr( L_temp, 2 );
    1347         719 :     L_temp = L_shr( L_temp, 6 );
    1348             : 
    1349         719 :     return L_temp;
    1350             : }
    1351             : 
    1352             : /*-------------------------------------------------------------------*
    1353             :  * BIT_ALLOC_IDX_16KHZ_fx()
    1354             :  *-------------------------------------------------------------------*/
    1355             : 
    1356         114 : Word32 BIT_ALLOC_IDX_16KHZ_fx( Word32 brate, Word16 ctype, Word16 sfrm, Word16 tc )
    1357             : {
    1358             :     Word32 L_temp;
    1359             :     Word16 temp;
    1360             :     /* 'ctype' =
    1361             :        TRANSITION => 2
    1362             :        GENERIC    => 1
    1363             :        ALL Other  => 0
    1364             :        */
    1365         114 :     L_temp = L_and( shr( 0x0240l, shl( ctype, 1 ) ), 3 );
    1366             : 
    1367         114 :     temp = BRATE2IDX16k_fx( brate );
    1368         114 :     L_temp = L_mac0( L_temp, 3, temp );
    1369         114 :     IF( tc >= 0 )
    1370         114 :     L_temp = L_mac0( L_temp, ( 7 - 3 ), temp );
    1371             :     /* So either 'temp' x 3 when 'tc < 0', 'temp' x 7 otherwise */
    1372             : 
    1373         114 :     L_temp = L_mac0( L_temp, 1, s_max( 0, tc ) );
    1374             : 
    1375         114 :     IF( sfrm >= 0 )
    1376             :     {
    1377             :         /* Mult by 5 */
    1378         114 :         L_temp = L_add( L_temp, L_shl( L_temp, 2 ) );
    1379         114 :         L_temp = L_mac0( L_temp, shr( sfrm, 6 ), 1 );
    1380             :     }
    1381             : 
    1382         114 :     return L_temp;
    1383             : }
    1384             : 
    1385             : 
    1386             : /*-------------------------------------------------------------------*
    1387             :  * berCheck()
    1388             :  *
    1389             :  * Check for bit errors in channel aware signalling.
    1390             :  *-------------------------------------------------------------------*/
    1391             : 
    1392           0 : static void berCheck(
    1393             :     Decoder_State *st, /* i/o: decoder state structure     */
    1394             :     Word16 *coder_type /* i/o: coder type                  */
    1395             : )
    1396             : {
    1397             :     /* In case of RF flag = 1, and valid RF packet with primary and partial copy */
    1398           0 :     test();
    1399           0 :     test();
    1400           0 :     IF( ( EQ_16( st->bwidth, NB ) || EQ_16( st->bwidth, FB ) ) || ( GE_16( *coder_type, TRANSITION ) ) )
    1401             :     {
    1402           0 :         if ( EQ_16( st->use_partial_copy, 1 ) )
    1403             :         {
    1404           0 :             st->use_partial_copy = 0;
    1405           0 :             move16();
    1406             :         }
    1407             : 
    1408           0 :         st->bfi = 1;
    1409           0 :         move16();
    1410           0 :         st->bwidth = st->last_bwidth;
    1411           0 :         move16();
    1412           0 :         st->BER_detect = 1;
    1413           0 :         move16();
    1414           0 :         *coder_type = GENERIC;
    1415           0 :         move16();
    1416             :     }
    1417             : 
    1418           0 :     return;
    1419             : }
    1420             : 
    1421             : /*-------------------------------------------------------------------*
    1422             :  * getPartialCopyInfo()
    1423             :  *
    1424             :  * Check if the frame includes a partial copy for channel aware processing.
    1425             :  *-------------------------------------------------------------------*/
    1426             : 
    1427        3100 : void getPartialCopyInfo(
    1428             :     Decoder_State *st, /* i/o: decoder state structure       */
    1429             :     Word16 *coder_type,
    1430             :     Word16 *sharpFlag )
    1431             : {
    1432             :     Word16 nBits;
    1433             :     Word16 ind;
    1434             :     /* check the rf flag in the packet */
    1435        3100 :     get_rfFlag( st, &( st->rf_flag ), &nBits, &ind );
    1436             : 
    1437             :     /* get rf frame type info */
    1438        3100 :     get_rfFrameType( st, &( st->rf_frame_type ) );
    1439             : 
    1440             :     /* Get the FEC offset info */
    1441        3100 :     get_rf_fec_offset( st, &( st->rf_fec_offset ) );
    1442             : 
    1443             :     /* reset number of target bits in case of rate switching */
    1444        3100 :     st->rf_target_bits = 0;
    1445        3100 :     move16();
    1446             : 
    1447             :     /* Get the number of bits used for RF*/
    1448        3100 :     IF( EQ_16( st->rf_flag, 1 ) )
    1449             :     {
    1450           0 :         *coder_type = s_and( ind, 0x7 );
    1451           0 :         move16();
    1452           0 :         st->bwidth = s_and( shr( ind, 3 ), 0x7 );
    1453           0 :         move16();
    1454           0 :         *sharpFlag = s_and( shr( ind, 6 ), 0x1 );
    1455           0 :         move16();
    1456           0 :         st->codec_mode = MODE2;
    1457           0 :         move16();
    1458           0 :         get_rfTargetBits( st->rf_frame_type, &( st->rf_target_bits ) );
    1459             : 
    1460           0 :         IF( EQ_16( st->bfi, FRAMEMODE_FUTURE ) )
    1461             :         {
    1462           0 :             st->use_partial_copy = 1;
    1463           0 :             move16();
    1464             :             /* now set the frame mode to normal mode */
    1465           0 :             test();
    1466           0 :             IF( GE_16( st->rf_frame_type, RF_TCXFD ) && LE_16( st->rf_frame_type, RF_TCXTD2 ) )
    1467             :             {
    1468           0 :                 st->bfi = 1;
    1469           0 :                 move16();
    1470           0 :                 st->core = 1;
    1471           0 :                 move16();
    1472             :             }
    1473             :             ELSE
    1474             :             {
    1475           0 :                 st->bfi = FRAMEMODE_NORMAL;
    1476           0 :                 move16();
    1477           0 :                 st->core = 0;
    1478           0 :                 move16();
    1479             :             }
    1480             :         }
    1481             :         /* check for bit errors */
    1482           0 :         berCheck( st, coder_type );
    1483             : 
    1484           0 :         get_next_indice_tmp_fx( st, nBits );
    1485             :     }
    1486        3100 : }
    1487             : 
    1488             : /*-------------------------------------------------------------------*
    1489             :  * get_rfFlag()
    1490             :  *
    1491             :  * Check if rf flag is present in the bitstream
    1492             :  *-------------------------------------------------------------------*/
    1493             : 
    1494        3100 : void get_rfFlag(
    1495             :     Decoder_State *st, /* i: decoder state structure       */
    1496             :     Word16 *rf_flag,   /* o  : check for the RF flag    */
    1497             :     Word16 *nBits,
    1498             :     Word16 *ind )
    1499             : {
    1500             :     Word16 start_idx, nBits_tmp;
    1501             :     Word16 ind_tmp;
    1502             : 
    1503             :     /* Init */
    1504        3100 :     *rf_flag = 0;
    1505        3100 :     move16();
    1506             : 
    1507             :     /* check for rf_flag in the packet and extract the rf_frame_type and rf_fec_offset */
    1508        3100 :     test();
    1509        3100 :     test();
    1510        3100 :     IF( EQ_32( st->total_brate, ACELP_13k20 ) && ( ( st->bfi == FRAMEMODE_NORMAL ) || EQ_16( st->bfi, FRAMEMODE_FUTURE ) ) )
    1511             :     {
    1512             :         /* find the section in the ACELP signalling table corresponding to bitrate */
    1513        1050 :         start_idx = 0;
    1514        1050 :         move16();
    1515       36750 :         WHILE( acelp_sig_tbl[start_idx] != st->total_brate )
    1516             :         {
    1517       35700 :             start_idx = add( start_idx, 1 );
    1518       35700 :             assert( ( start_idx < MAX_ACELP_SIG ) && "ERROR: start_idx larger than acelp_sig_tbl[].\n" );
    1519             :         }
    1520             : 
    1521             :         /* skip the bitrate */
    1522        1050 :         start_idx = add( start_idx, 1 );
    1523             : 
    1524             :         /* retrieve the number of bits */
    1525        1050 :         nBits_tmp = (Word16) acelp_sig_tbl[start_idx++];
    1526        1050 :         move16();
    1527             : 
    1528             :         /* retrieve the signalling indice */
    1529        1050 :         ind_tmp = (Word16) acelp_sig_tbl[add( start_idx, get_indice( st, 0, nBits_tmp ) )];
    1530        1050 :         move16();
    1531             : 
    1532             :         /* convert signalling indice into RF flag. */
    1533        1050 :         *rf_flag = s_and( shr( ind_tmp, 7 ), 0x1 );
    1534        1050 :         move16();
    1535             : 
    1536        1050 :         if ( ind )
    1537             :         {
    1538        1050 :             *ind = ind_tmp;
    1539        1050 :             move16();
    1540             :         }
    1541             : 
    1542        1050 :         if ( nBits )
    1543             :         {
    1544        1050 :             *nBits = nBits_tmp;
    1545        1050 :             move16();
    1546             :         }
    1547             :     }
    1548        3100 : }
    1549             : 
    1550             : /*-------------------------------------------------------------------*
    1551             :  * get_rfFrameType()
    1552             :  *
    1553             :  * Extract the rf frame type
    1554             :  *-------------------------------------------------------------------*/
    1555             : 
    1556        3100 : void get_rfFrameType(
    1557             :     Decoder_State *st,    /* i  : decoder state structure       */
    1558             :     Word16 *rf_frame_type /* o  : RF frame type                 */
    1559             : )
    1560             : {
    1561        3100 :     Word16 num_bits = 0;
    1562        3100 :     move16();
    1563             : 
    1564        3100 :     IF( EQ_16( st->rf_flag, 1 ) )
    1565             :     {
    1566             :         /*num_bits = st->total_brate/50;*/
    1567           0 :         IF( EQ_32( st->total_brate, ACELP_13k20 ) )
    1568             :         {
    1569           0 :             num_bits = 264;
    1570           0 :             move16(); /* @13.2kbps */
    1571             :         }
    1572             :         ELSE
    1573             :         {
    1574             :             UWord16 lsb;
    1575             :             Word32 L_tmp;
    1576           0 :             Mpy_32_16_ss( st->total_brate, 5243, &L_tmp, &lsb ); /* 5243 is 1/50 in Q18. (0+18-15=3) */
    1577           0 :             num_bits = extract_l( L_shr( L_tmp, 3 ) );           /* Q0 */
    1578             :         }
    1579             : 
    1580             :         /* the last three bits in a packet is the RF frame type */
    1581           0 :         *rf_frame_type = get_indice( st, sub( num_bits, 3 ), 3 );
    1582           0 :         move16();
    1583             :     }
    1584             :     ELSE
    1585             :     {
    1586        3100 :         *rf_frame_type = 0;
    1587        3100 :         move16();
    1588             :     }
    1589        3100 : }
    1590             : 
    1591             : /*-------------------------------------------------------------------*
    1592             :  * get_rf_fec_offset()
    1593             :  *
    1594             :  * Extract the FEC offset
    1595             :  *-------------------------------------------------------------------*/
    1596             : 
    1597        3100 : void get_rf_fec_offset(
    1598             :     Decoder_State *st,    /* i  : decoder state structure       */
    1599             :     Word16 *rf_fec_offset /* o  : RF fec offset                 */
    1600             : )
    1601             : {
    1602             :     Word16 num_bits, tmp;
    1603             : 
    1604        3100 :     IF( EQ_16( st->rf_flag, 1 ) )
    1605             :     {
    1606             :         /*num_bits = st->total_brate/50;*/
    1607           0 :         IF( EQ_32( st->total_brate, ACELP_13k20 ) )
    1608             :         {
    1609           0 :             num_bits = 264;
    1610           0 :             move16(); /* @13.2kbps */
    1611             :         }
    1612             :         ELSE
    1613             :         {
    1614             :             UWord16 lsb;
    1615             :             Word32 L_tmp;
    1616           0 :             Mpy_32_16_ss( st->total_brate, 5243, &L_tmp, &lsb ); /* 5243 is 1/50 in Q18. (0+18-15=3) */
    1617           0 :             num_bits = extract_l( L_shr( L_tmp, 3 ) );           /* Q0 */
    1618             :         }
    1619             : 
    1620             :         /* the two bits before the rf frame type contain the fec offset  */
    1621           0 :         tmp = get_indice( st, sub( num_bits, 5 ), 2 );
    1622             : 
    1623           0 :         IF( tmp == 0 )
    1624             :         {
    1625           0 :             *rf_fec_offset = 2;
    1626           0 :             move16();
    1627             :         }
    1628             :         ELSE
    1629             :         {
    1630           0 :             *rf_fec_offset = add( shl( tmp, 1 ), 1 );
    1631           0 :             move16();
    1632             :         }
    1633             :     }
    1634             :     ELSE
    1635             :     {
    1636        3100 :         *rf_fec_offset = 0;
    1637        3100 :         move16();
    1638             :     }
    1639        3100 : }
    1640             : 
    1641             : /*-------------------------------------------------------------------*
    1642             :  * get_rfTargetBits()
    1643             :  *
    1644             :  * Return the number of RF target bits
    1645             :  *-------------------------------------------------------------------*/
    1646             : 
    1647           0 : void get_rfTargetBits(
    1648             :     Word16 rf_frame_type,  /* i  : RF frame type                 */
    1649             :     Word16 *rf_target_bits /* o  : Number of RF target bits      */
    1650             : )
    1651             : {
    1652             : 
    1653             :     /* Number of RF bits for different RF coder types */
    1654             : 
    1655           0 :     SWITCH( rf_frame_type )
    1656             :     {
    1657           0 :         case RF_NO_DATA:
    1658           0 :             *rf_target_bits = 5;
    1659           0 :             move16();
    1660           0 :             BREAK;
    1661           0 :         case RF_TCXFD:
    1662           0 :             *rf_target_bits = 27;
    1663           0 :             move16();
    1664           0 :             BREAK;
    1665           0 :         case RF_TCXTD1:
    1666           0 :             *rf_target_bits = 16;
    1667           0 :             move16();
    1668           0 :             BREAK;
    1669           0 :         case RF_TCXTD2:
    1670           0 :             *rf_target_bits = 16;
    1671           0 :             move16();
    1672           0 :             BREAK;
    1673           0 :         case RF_ALLPRED:
    1674             :             /* Es_pred bits 3 bits, LTF: 1, pitch: 8,5,5,5, FCB: 0, gain: 7,0,7,0, Diff GFr: 4*/
    1675           0 :             *rf_target_bits = 63;
    1676           0 :             move16();
    1677           0 :             BREAK;
    1678           0 :         case RF_NOPRED:
    1679             :             /* Es_pred bits 3 bits, LTF: 0, pitch: 0, FCB: 7,7,7,7, gain: 6,0,6,0, Diff GFr: 2*/
    1680           0 :             *rf_target_bits = 66;
    1681           0 :             move16();
    1682           0 :             BREAK;
    1683           0 :         case RF_GENPRED:
    1684             :             /* 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*/
    1685           0 :             *rf_target_bits = 70;
    1686           0 :             move16();
    1687           0 :             BREAK;
    1688           0 :         case RF_NELP:
    1689             :             /* gain: 19, Diff GFr: 5 */
    1690           0 :             *rf_target_bits = 45;
    1691           0 :             move16();
    1692           0 :             BREAK;
    1693             :     }
    1694           0 : }
    1695             : 
    1696             : 
    1697             : /*-------------------------------------------------------------------*
    1698             :  * get_NextCoderType_fx()
    1699             :  *
    1700             :  * Extract the coder type of next frame
    1701             :  *-------------------------------------------------------------------*/
    1702             : 
    1703          53 : void get_NextCoderType_fx(
    1704             :     UWord8 *bitsteam,       /* i : bitstream         */
    1705             :     Word16 *next_coder_type /* o : next coder type   */
    1706             : )
    1707             : {
    1708             :     Word16 k;
    1709             :     Word16 start_idx;
    1710             :     Word16 nBits_tmp;
    1711             :     Word8 bit_stream[ACELP_13k20 / 50];
    1712             :     UWord16 tmp;
    1713             : 
    1714             : 
    1715       14045 :     FOR( k = 0; k < ACELP_13k20 / 50; k++ )
    1716             :     {
    1717       13992 :         bit_stream[k] = (UWord8) s_and( shr( bitsteam[k / 8], sub( 7, s_and( k, 7 ) ) ), 0x1 );
    1718       13992 :         move16();
    1719             :     }
    1720          53 :     start_idx = 0;
    1721          53 :     move16();
    1722        1855 :     WHILE( acelp_sig_tbl[start_idx] != ACELP_13k20 )
    1723             :     {
    1724        1802 :         start_idx = add( start_idx, 1 );
    1725        1802 :         assert( ( start_idx < MAX_ACELP_SIG ) && "ERROR: start_idx larger than acelp_sig_tbl[].\n" );
    1726             :     }
    1727             : 
    1728             :     /* skip the bitrate */
    1729          53 :     start_idx = add( start_idx, 1 );
    1730             : 
    1731          53 :     tmp = 0;
    1732          53 :     move16();
    1733          53 :     nBits_tmp = extract_l( acelp_sig_tbl[start_idx++] );
    1734          53 :     move16();
    1735         318 :     FOR( k = 0; k < nBits_tmp; k++ )
    1736             :     {
    1737         265 :         tmp = lshl( tmp, 1 );
    1738         265 :         tmp = add( tmp, bit_stream[k] );
    1739             :     }
    1740             :     /* retrieve the signalling indice */
    1741          53 :     *next_coder_type = s_and( extract_l( acelp_sig_tbl[start_idx + tmp] ), 0x7 );
    1742          53 :     move16();
    1743          53 : }
    1744             : 
    1745             : /*-------------------------------------------------------------------*
    1746             :  * get_indice_preview()
    1747             :  *
    1748             :  * Indices preview to parse for the presence of partial copy
    1749             :  *-------------------------------------------------------------------*/
    1750        4281 : static UWord16 get_indice_preview(
    1751             :     UWord8 *bitstream,
    1752             :     Word16 bitstreamSize,
    1753             :     Word16 pos,
    1754             :     Word16 nb_bits )
    1755             : {
    1756             :     UWord16 value;
    1757             :     Word16 i;
    1758             :     UWord16 bitstreamShort[MAX_BITS_PER_FRAME + 16];
    1759             :     UWord16 *bitstreamShortPtr;
    1760             : 
    1761             :     /* convert bitstream from compact bytes to short values */
    1762        4281 :     bitstreamShortPtr = bitstreamShort;
    1763     1134465 :     FOR( i = 0; i < bitstreamSize; i++ )
    1764             :     {
    1765     1130184 :         *bitstreamShortPtr++ = s_and( shr( bitstream[i / 8], sub( 7, ( s_and( i, 7 ) ) ) ), 0x1 );
    1766     1130184 :         move16();
    1767             :     }
    1768             : 
    1769        4281 :     assert( nb_bits <= 16 );
    1770        4281 :     value = 0;
    1771        4281 :     move16();
    1772       20966 :     FOR( i = 0; i < nb_bits; i++ )
    1773             :     {
    1774       16685 :         value = shl( value, 1 );
    1775       16685 :         value = add( value, bitstreamShort[pos + i] );
    1776             :     }
    1777        4281 :     return value;
    1778             : }
    1779             : 
    1780             : /*-------------------------------------------------------------------*
    1781             :  * evs_dec_previewFrame()
    1782             :  *
    1783             :  * Signalling index preview
    1784             :  *-------------------------------------------------------------------*/
    1785       17523 : void evs_dec_previewFrame(
    1786             :     UWord8 *bitstream,
    1787             :     Word16 bitstreamSize,
    1788             :     Word16 *partialCopyFrameType,
    1789             :     Word16 *partialCopyOffset )
    1790             : {
    1791             :     Word32 total_brate;
    1792             :     Word16 start_idx, nBits;
    1793             :     Word32 ind;
    1794             :     Word16 rf_flag;
    1795             : 
    1796       17523 :     rf_flag = 0;
    1797       17523 :     move16();
    1798       17523 :     *partialCopyFrameType = 0;
    1799       17523 :     move16();
    1800       17523 :     *partialCopyOffset = 0;
    1801       17523 :     move16();
    1802       17523 :     total_brate = L_mult0( bitstreamSize, 50 );
    1803             : 
    1804       17523 :     IF( EQ_32( total_brate, ACELP_13k20 ) )
    1805             :     {
    1806             :         /* find the section in the ACELP signalling table corresponding to bitrate */
    1807        2393 :         start_idx = 0;
    1808        2393 :         move16();
    1809       83755 :         WHILE( acelp_sig_tbl[start_idx] != total_brate )
    1810             :         {
    1811       81362 :             start_idx = add( start_idx, 1 );
    1812       81362 :             assert( ( start_idx < MAX_ACELP_SIG ) && "ERROR: start_idx larger than acelp_sig_tbl[].\n" );
    1813             :         }
    1814             : 
    1815             :         /* skip the bitrate */
    1816        2393 :         start_idx = add( start_idx, 1 );
    1817             :         /* retrieve the number of bits */
    1818        2393 :         nBits = extract_l( acelp_sig_tbl[start_idx++] );
    1819             : 
    1820             :         /* retrieve the signalling indice */
    1821        2393 :         ind = acelp_sig_tbl[( start_idx + get_indice_preview( bitstream, bitstreamSize, 0, nBits ) )];
    1822        2393 :         move32();
    1823             : 
    1824             :         /* convert signalling indice into RF flag. */
    1825        2393 :         rf_flag = s_and( extract_l( L_shr( ind, 7 ) ), 0x1 );
    1826        2393 :         assert( rf_flag == ( ( ind >> 7 ) & 0x1 ) );
    1827        2393 :         IF( rf_flag != 0 )
    1828             :         {
    1829             :             /* read the fec offset at which the partial copy is received */
    1830         944 :             ind = get_indice_preview( bitstream, bitstreamSize, sub( bitstreamSize, 5 ), 2 );
    1831         944 :             IF( ind == 0 ) *partialCopyOffset = 2;
    1832         776 :             ELSE IF( EQ_32( ind, 1 ) ) *partialCopyOffset = 3;
    1833         742 :             ELSE IF( EQ_32( ind, 2 ) ) *partialCopyOffset = 5;
    1834         663 :             ELSE IF( EQ_32( ind, 3 ) ) *partialCopyOffset = 7;
    1835         944 :             move16();
    1836             :             /* the last three bits in a packet is the RF frame type */
    1837         944 :             *partialCopyFrameType = get_indice_preview( bitstream, bitstreamSize, bitstreamSize - 3, 3 );
    1838         944 :             move16();
    1839             :         }
    1840             :     }
    1841       17523 : }
    1842             : 
    1843         410 : void dtx_read_padding_bits_fx(
    1844             :     DEC_CORE_HANDLE st,
    1845             :     const Word16 num_bits )
    1846             : {
    1847             :     /* TODO: temporary hack, need to decide what to do with core-coder bitrate */
    1848             :     Word32 tmp;
    1849             : 
    1850         410 :     tmp = st->total_brate;
    1851         410 :     move32();
    1852         410 :     st->total_brate = L_add( st->total_brate, L_mult0( num_bits, FRAMES_PER_SEC ) );
    1853         410 :     move32();
    1854         410 :     get_next_indice_fx( st, num_bits );
    1855         410 :     st->total_brate = tmp;
    1856         410 :     move32();
    1857             : 
    1858         410 :     return;
    1859             : }

Generated by: LCOV version 1.14