LCOV - code coverage report
Current view: top level - lib_com - bitstream.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 662 958 69.1 %
Date: 2025-05-23 08:37:30 Functions: 35 43 81.4 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <assert.h>
      38             : #include <stdint.h>
      39             : #include "options.h"
      40             : #ifdef DEBUGGING
      41             : #include "debug.h"
      42             : #endif
      43             : #include "cnst.h"
      44             : #include "prot.h"
      45             : #include "stat_enc.h"
      46             : #include "stat_dec.h"
      47             : #include "rom_com.h"
      48             : #include "mime.h"
      49             : #include "ivas_prot.h"
      50             : #include "ivas_cnst.h"
      51             : #include "ivas_rom_com.h"
      52             : #include "wmc_auto.h"
      53             : 
      54             : #ifdef DEBUGGING
      55             : 
      56             : #define FEC_SEED 12558
      57             : 
      58             : /*-------------------------------------------------------------------*
      59             :  * Global variables
      60             :  *--------------------------------------------------------------------*/
      61             : 
      62             : FILE *FEC_pattern = NULL; /* FEC pattern file (for simulation of FEC) */
      63             : #endif
      64             : 
      65             : #define STEP_MAX_NUM_INDICES 100 /* increase the maximum number of allowed indices in the list by this amount */
      66             : 
      67             : 
      68             : /*-------------------------------------------------------------------*
      69             :  * rate2AMRWB_IOmode()
      70             :  *
      71             :  * lookup AMRWB IO mode
      72             :  *-------------------------------------------------------------------*/
      73             : 
      74        9300 : static Word16 rate2AMRWB_IOmode(
      75             :     Word32 brate /* i  : bitrate */
      76             : )
      77             : {
      78        9300 :     switch ( brate )
      79             :     {
      80             :         /* EVS AMR-WB IO modes */
      81           0 :         case SID_1k75:
      82           0 :             return AMRWB_IO_SID;
      83           0 :         case ACELP_6k60:
      84           0 :             return AMRWB_IO_6600;
      85           0 :         case ACELP_8k85:
      86           0 :             return AMRWB_IO_8850;
      87           0 :         case ACELP_12k65:
      88           0 :             return AMRWB_IO_1265;
      89           0 :         case ACELP_14k25:
      90           0 :             return AMRWB_IO_1425;
      91           0 :         case ACELP_15k85:
      92           0 :             return AMRWB_IO_1585;
      93           0 :         case ACELP_18k25:
      94           0 :             return AMRWB_IO_1825;
      95           0 :         case ACELP_19k85:
      96           0 :             return AMRWB_IO_1985;
      97           0 :         case ACELP_23k05:
      98           0 :             return AMRWB_IO_2305;
      99           0 :         case ACELP_23k85:
     100           0 :             return AMRWB_IO_2385;
     101        9300 :         default:
     102        9300 :             break;
     103             :     }
     104             : 
     105        9300 :     return -1;
     106             : }
     107             : 
     108             : /*-------------------------------------------------------------------*
     109             :  * rate2EVSmode()
     110             :  *
     111             :  * lookup EVS mode
     112             :  *-------------------------------------------------------------------*/
     113        9300 : Word16 rate2EVSmode(
     114             :     const Word32 brate, /* i  : bitrate                                               */
     115             :     int16_t *is_amr_wb  /* o  : (flag) does the bitrate belong to AMR-WB? Can be NULL */
     116             : )
     117             : {
     118        9300 :     if ( is_amr_wb != NULL )
     119             :     {
     120           0 :         *is_amr_wb = 0;
     121             :     }
     122             : 
     123        9300 :     switch ( brate )
     124             :     {
     125             :         /* EVS Primary modes */
     126           0 :         case FRAME_NO_DATA:
     127           0 :             return NO_DATA_RECEIVED;
     128           0 :         case SID_2k40:
     129           0 :             return PRIMARY_SID;
     130           0 :         case PPP_NELP_2k80:
     131           0 :             return PRIMARY_2800;
     132           0 :         case ACELP_7k20:
     133           0 :             return PRIMARY_7200;
     134           0 :         case ACELP_8k00:
     135           0 :             return PRIMARY_8000;
     136           0 :         case ACELP_9k60:
     137           0 :             return PRIMARY_9600;
     138        3150 :         case ACELP_13k20:
     139        3150 :             return PRIMARY_13200;
     140           0 :         case ACELP_16k40:
     141           0 :             return PRIMARY_16400;
     142        3150 :         case ACELP_24k40:
     143        3150 :             return PRIMARY_24400;
     144           0 :         case ACELP_32k:
     145           0 :             return PRIMARY_32000;
     146           0 :         case ACELP_48k:
     147           0 :             return PRIMARY_48000;
     148        3000 :         case ACELP_64k:
     149        3000 :             return PRIMARY_64000;
     150           0 :         case HQ_96k:
     151           0 :             return PRIMARY_96000;
     152           0 :         case HQ_128k:
     153           0 :             return PRIMARY_128000;
     154           0 :         default:
     155           0 :             break;
     156             :     }
     157             : 
     158           0 :     if ( is_amr_wb != NULL )
     159             :     {
     160           0 :         *is_amr_wb = 1;
     161             :     }
     162             : 
     163           0 :     return rate2AMRWB_IOmode( brate );
     164             : }
     165             : 
     166             : /*-------------------------------------------------------------------*
     167             :  * ind_list_realloc()
     168             :  *
     169             :  * Re-allocate the list of indices
     170             :  *-------------------------------------------------------------------*/
     171             : 
     172           0 : ivas_error ind_list_realloc(
     173             :     INDICE_HANDLE old_ind_list,    /* i  : pointer to the beginning of the old buffer of indices */
     174             :     const int16_t max_num_indices, /* i  : new maximum number of allowed indices in the list */
     175             :     Encoder_Struct *st_ivas        /* i  : IVAS encoder structure                  */
     176             : )
     177             : {
     178             :     int16_t i, n, ch, n_channels, ind_list_pos, is_metadata, ivas_max_num_indices;
     179             :     INDICE_HANDLE new_ind_list;
     180             :     BSTR_ENC_HANDLE hBstr;
     181             : 
     182           0 :     if ( st_ivas == NULL )
     183             :     {
     184           0 :         return IVAS_ERR_OK;
     185             :     }
     186             : 
     187             :     /* get the pointer to the beginning of the old buffer of indices (either metadata or core coders) */
     188           0 :     if ( old_ind_list == st_ivas->ind_list_metadata )
     189             :     {
     190           0 :         is_metadata = 1;
     191           0 :         ivas_max_num_indices = st_ivas->ivas_max_num_indices_metadata;
     192             :     }
     193             :     else
     194             :     {
     195           0 :         is_metadata = 0;
     196           0 :         ivas_max_num_indices = st_ivas->ivas_max_num_indices;
     197             :     }
     198             : 
     199             :     /* allocate new buffer of indices */
     200           0 :     if ( ( new_ind_list = (INDICE_HANDLE) malloc( max_num_indices * sizeof( Indice ) ) ) == NULL )
     201             :     {
     202           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for buffer of indices!\n" ) );
     203             :     }
     204             : 
     205             :     /* move indices from the old list to the new list */
     206           0 :     for ( i = 0; i < min( max_num_indices, ivas_max_num_indices ); i++ )
     207             :     {
     208           0 :         if ( old_ind_list[i].nb_bits > -1 )
     209             :         {
     210           0 :             new_ind_list[i].id = old_ind_list[i].id;
     211           0 :             new_ind_list[i].value = old_ind_list[i].value;
     212             :         }
     213           0 :         new_ind_list[i].nb_bits = old_ind_list[i].nb_bits;
     214             :     }
     215             : 
     216             :     /* reset nb_bits of all other indices to -1 */
     217           0 :     for ( ; i < max_num_indices; i++ )
     218             :     {
     219           0 :         new_ind_list[i].nb_bits = -1;
     220             :     }
     221             : 
     222             :     /* update parameters in all SCE elements */
     223           0 :     for ( n = 0; n < st_ivas->nSCE; n++ )
     224             :     {
     225             :         /* get the pointer to hBstr */
     226           0 :         if ( is_metadata )
     227             :         {
     228           0 :             hBstr = st_ivas->hSCE[n]->hMetaData;
     229             :         }
     230             :         else
     231             :         {
     232           0 :             hBstr = st_ivas->hSCE[n]->hCoreCoder[0]->hBstr;
     233             :         }
     234             : 
     235           0 :         if ( hBstr != NULL )
     236             :         {
     237             :             /* get the current position inside the old list */
     238           0 :             ind_list_pos = (int16_t) ( hBstr->ind_list - old_ind_list );
     239             : 
     240             :             /* set pointers in the new list */
     241           0 :             *( hBstr->ivas_ind_list_zero ) = new_ind_list;
     242           0 :             hBstr->ind_list = &new_ind_list[ind_list_pos];
     243             : 
     244             :             /* set the new maximum number of indices */
     245           0 :             *( hBstr->ivas_max_num_indices ) = max_num_indices;
     246             :         }
     247             :     }
     248             : 
     249             :     /* update parameters in all CPE elements */
     250           0 :     for ( n = 0; n < st_ivas->nCPE; n++ )
     251             :     {
     252             :         /* get the pointer to hBstr */
     253           0 :         if ( is_metadata )
     254             :         {
     255           0 :             n_channels = 1;
     256             :         }
     257             :         else
     258             :         {
     259           0 :             n_channels = CPE_CHANNELS;
     260             :         }
     261             : 
     262           0 :         for ( ch = 0; ch < n_channels; ch++ )
     263             :         {
     264           0 :             if ( is_metadata )
     265             :             {
     266           0 :                 hBstr = st_ivas->hCPE[n]->hMetaData;
     267             :             }
     268             :             else
     269             :             {
     270           0 :                 hBstr = st_ivas->hCPE[n]->hCoreCoder[ch]->hBstr;
     271             :             }
     272             : 
     273           0 :             if ( hBstr != NULL )
     274             :             {
     275             :                 /* get the current position inside the old list */
     276           0 :                 ind_list_pos = (int16_t) ( hBstr->ind_list - old_ind_list );
     277             : 
     278             :                 /* set pointers in the new list */
     279           0 :                 *( hBstr->ivas_ind_list_zero ) = new_ind_list;
     280           0 :                 hBstr->ind_list = &new_ind_list[ind_list_pos];
     281             : 
     282             :                 /* set the new maximum number of indices */
     283           0 :                 *( hBstr->ivas_max_num_indices ) = max_num_indices;
     284             :             }
     285             :         }
     286             :     }
     287             : 
     288             :     /* free the old list */
     289           0 :     free( old_ind_list );
     290             : 
     291           0 :     return IVAS_ERR_OK;
     292             : }
     293             : 
     294             : 
     295             : /*-----------------------------------------------------------------------*
     296             :  * get_ivas_max_num_indices()
     297             :  *
     298             :  * Get the maximum allowed number of indices in the encoder
     299             :  *-----------------------------------------------------------------------*/
     300             : 
     301             : /*! r: maximum number of indices */
     302        7530 : int16_t get_ivas_max_num_indices(
     303             :     const IVAS_FORMAT ivas_format, /* i  : IVAS format               */
     304             :     const int32_t ivas_total_brate /* i  : IVAS total bitrate        */
     305             : )
     306             : {
     307        7530 :     if ( ivas_format == STEREO_FORMAT )
     308             :     {
     309         370 :         if ( ivas_total_brate <= IVAS_16k4 )
     310             :         {
     311          88 :             return 300;
     312             :         }
     313         282 :         else if ( ivas_total_brate <= IVAS_24k4 )
     314             :         {
     315          43 :             return 400;
     316             :         }
     317         239 :         else if ( ivas_total_brate <= IVAS_32k )
     318             :         {
     319          54 :             return 450;
     320             :         }
     321         185 :         else if ( ivas_total_brate <= IVAS_48k )
     322             :         {
     323          38 :             return 650;
     324             :         }
     325         147 :         else if ( ivas_total_brate <= IVAS_80k )
     326             :         {
     327          73 :             return 750;
     328             :         }
     329          74 :         else if ( ivas_total_brate <= IVAS_128k )
     330             :         {
     331          74 :             return 850;
     332             :         }
     333           0 :         else if ( ivas_total_brate <= IVAS_192k )
     334             :         {
     335           0 :             return 950;
     336             :         }
     337           0 :         else if ( ivas_total_brate <= IVAS_256k )
     338             :         {
     339           0 :             return 1350;
     340             :         }
     341             :         else
     342             :         {
     343           0 :             return 1650;
     344             :         }
     345             :     }
     346        7160 :     else if ( ivas_format == ISM_FORMAT || ivas_format == MONO_FORMAT )
     347             :     {
     348        1024 :         if ( ivas_total_brate <= IVAS_16k4 )
     349             :         {
     350          30 :             return 250;
     351             :         }
     352         994 :         else if ( ivas_total_brate <= IVAS_24k4 )
     353             :         {
     354          63 :             return 350;
     355             :         }
     356         931 :         else if ( ivas_total_brate <= IVAS_32k )
     357             :         {
     358         294 :             return 450;
     359             :         }
     360         637 :         else if ( ivas_total_brate <= IVAS_48k )
     361             :         {
     362         267 :             return 550;
     363             :         }
     364         370 :         else if ( ivas_total_brate <= IVAS_64k )
     365             :         {
     366          59 :             return 620;
     367             :         }
     368         311 :         else if ( ivas_total_brate <= IVAS_80k )
     369             :         {
     370          68 :             return 670;
     371             :         }
     372         243 :         else if ( ivas_total_brate <= IVAS_96k )
     373             :         {
     374          59 :             return 780;
     375             :         }
     376         184 :         else if ( ivas_total_brate <= IVAS_128k )
     377             :         {
     378          53 :             return 880;
     379             :         }
     380         131 :         else if ( ivas_total_brate <= IVAS_192k )
     381             :         {
     382          80 :             return 950;
     383             :         }
     384          51 :         else if ( ivas_total_brate <= IVAS_256k )
     385             :         {
     386          48 :             return 1100;
     387             :         }
     388           3 :         else if ( ivas_total_brate <= IVAS_384k )
     389             :         {
     390           2 :             return 1300;
     391             :         }
     392             :         else
     393             :         {
     394           1 :             return 1650;
     395             :         }
     396             :     }
     397        6136 :     else if ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT )
     398             :     {
     399        1954 :         if ( ivas_total_brate <= IVAS_16k4 )
     400             :         {
     401         396 :             return 250;
     402             :         }
     403        1558 :         else if ( ivas_total_brate <= IVAS_24k4 )
     404             :         {
     405         148 :             return 350;
     406             :         }
     407        1410 :         else if ( ivas_total_brate <= IVAS_32k )
     408             :         {
     409         167 :             return 400;
     410             :         }
     411        1243 :         else if ( ivas_total_brate <= IVAS_48k )
     412             :         {
     413          84 :             return 650;
     414             :         }
     415        1159 :         else if ( ivas_total_brate <= IVAS_80k )
     416             :         {
     417         301 :             return 750;
     418             :         }
     419         858 :         else if ( ivas_total_brate <= IVAS_128k )
     420             :         {
     421         259 :             return 1020;
     422             :         }
     423         599 :         else if ( ivas_total_brate <= IVAS_160k )
     424             :         {
     425          85 :             return 1160;
     426             :         }
     427         514 :         else if ( ivas_total_brate <= IVAS_192k )
     428             :         {
     429         160 :             return 1220;
     430             :         }
     431         354 :         else if ( ivas_total_brate <= IVAS_256k )
     432             :         {
     433         110 :             return 1300;
     434             :         }
     435         244 :         else if ( ivas_total_brate <= IVAS_384k )
     436             :         {
     437         119 :             return 1720;
     438             :         }
     439             :         else
     440             :         {
     441         125 :             return 2000;
     442             :         }
     443             :     }
     444        4182 :     else if ( ivas_format == MASA_FORMAT )
     445             :     {
     446        1344 :         if ( ivas_total_brate <= IVAS_16k4 )
     447             :         {
     448         311 :             return 300;
     449             :         }
     450        1033 :         else if ( ivas_total_brate <= IVAS_32k )
     451             :         {
     452         241 :             return 400;
     453             :         }
     454         792 :         else if ( ivas_total_brate <= IVAS_48k )
     455             :         {
     456          82 :             return 650;
     457             :         }
     458         710 :         else if ( ivas_total_brate <= IVAS_80k )
     459             :         {
     460         254 :             return 750;
     461             :         }
     462         456 :         else if ( ivas_total_brate <= IVAS_160k )
     463             :         {
     464         260 :             return 850;
     465             :         }
     466         196 :         else if ( ivas_total_brate <= IVAS_192k )
     467             :         {
     468          60 :             return 950;
     469             :         }
     470         136 :         else if ( ivas_total_brate <= IVAS_256k )
     471             :         {
     472          31 :             return 1150;
     473             :         }
     474         105 :         else if ( ivas_total_brate <= IVAS_384k )
     475             :         {
     476          53 :             return 1450;
     477             :         }
     478             :         else
     479             :         {
     480          52 :             return 1650;
     481             :         }
     482             :     }
     483        2838 :     else if ( ivas_format == MASA_ISM_FORMAT )
     484             :     {
     485        1678 :         if ( ivas_total_brate <= IVAS_16k4 )
     486             :         {
     487         319 :             return 300;
     488             :         }
     489        1359 :         else if ( ivas_total_brate <= IVAS_32k )
     490             :         {
     491         375 :             return 400;
     492             :         }
     493         984 :         else if ( ivas_total_brate <= IVAS_48k )
     494             :         {
     495         186 :             return 650;
     496             :         }
     497         798 :         else if ( ivas_total_brate <= IVAS_80k )
     498             :         {
     499         327 :             return 750;
     500             :         }
     501         471 :         else if ( ivas_total_brate <= IVAS_160k )
     502             :         {
     503         229 :             return 1150;
     504             :         }
     505         242 :         else if ( ivas_total_brate <= IVAS_192k )
     506             :         {
     507          65 :             return 1250;
     508             :         }
     509         177 :         else if ( ivas_total_brate <= IVAS_256k )
     510             :         {
     511          67 :             return 1400;
     512             :         }
     513         110 :         else if ( ivas_total_brate <= IVAS_384k )
     514             :         {
     515          59 :             return 1650;
     516             :         }
     517             :         else
     518             :         {
     519          51 :             return 1850;
     520             :         }
     521             :     }
     522        1160 :     else if ( ivas_format == MC_FORMAT )
     523             :     {
     524        1160 :         if ( ivas_total_brate <= IVAS_16k4 )
     525             :         {
     526         130 :             return 250;
     527             :         }
     528        1030 :         else if ( ivas_total_brate <= IVAS_24k4 )
     529             :         {
     530          67 :             return 350;
     531             :         }
     532         963 :         else if ( ivas_total_brate <= IVAS_32k )
     533             :         {
     534          49 :             return 400;
     535             :         }
     536         914 :         else if ( ivas_total_brate <= IVAS_48k )
     537             :         {
     538         195 :             return 650;
     539             :         }
     540         719 :         else if ( ivas_total_brate <= IVAS_64k )
     541             :         {
     542         103 :             return 750;
     543             :         }
     544         616 :         else if ( ivas_total_brate <= IVAS_80k )
     545             :         {
     546          76 :             return 850;
     547             :         }
     548         540 :         else if ( ivas_total_brate <= IVAS_128k )
     549             :         {
     550         175 :             return 1150;
     551             :         }
     552         365 :         else if ( ivas_total_brate <= IVAS_160k )
     553             :         {
     554         101 :             return 1420;
     555             :         }
     556         264 :         else if ( ivas_total_brate <= IVAS_256k )
     557             :         {
     558         161 :             return 2120;
     559             :         }
     560         103 :         else if ( ivas_total_brate <= IVAS_384k )
     561             :         {
     562          48 :             return 2250;
     563             :         }
     564             :         else
     565             :         {
     566          55 :             return 2450;
     567             :         }
     568             :     }
     569             : 
     570           0 :     return 2450;
     571             : }
     572             : 
     573             : 
     574             : /*-----------------------------------------------------------------------*
     575             :  * get_BWE_max_num_indices()
     576             :  *
     577             :  * Get the maximum number of indices in the BWE
     578             :  *-----------------------------------------------------------------------*/
     579             : 
     580             : /*! r: maximum number of indices */
     581        3791 : int16_t get_BWE_max_num_indices(
     582             :     const int32_t extl_brate /* i  : extensiona layer bitrate  */
     583             : )
     584             : {
     585             :     /* set the maximum number of indices in the BWE */
     586        3791 :     if ( extl_brate < SWB_BWE_16k )
     587             :     {
     588        3791 :         return 30;
     589             :     }
     590             :     else
     591             :     {
     592           0 :         return 150;
     593             :     }
     594             : }
     595             : 
     596             : 
     597             : /*-----------------------------------------------------------------------*
     598             :  * get_ivas_max_num_indices_metadata()
     599             :  *
     600             :  * Set the maximum allowed number of metadata indices in the list
     601             :  *-----------------------------------------------------------------------*/
     602             : 
     603             : /*! r: maximum number of indices */
     604        7530 : int16_t get_ivas_max_num_indices_metadata(
     605             :     const IVAS_FORMAT ivas_format, /* i  : IVAS format              */
     606             :     const int32_t ivas_total_brate /* i  : IVAS total bitrate       */
     607             : )
     608             : {
     609             :     /* set the maximum required number of metadata indices */
     610        7530 :     if ( ivas_format == MONO_FORMAT )
     611             :     {
     612           3 :         return 0;
     613             :     }
     614        7527 :     else if ( ivas_format == STEREO_FORMAT )
     615             :     {
     616         370 :         if ( ivas_total_brate <= IVAS_16k4 )
     617             :         {
     618          88 :             return 60;
     619             :         }
     620             :         else
     621             :         {
     622         282 :             return 80;
     623             :         }
     624             :     }
     625        7157 :     else if ( ivas_format == ISM_FORMAT )
     626             :     {
     627        1021 :         if ( ivas_total_brate <= IVAS_16k4 )
     628             :         {
     629          29 :             return 20;
     630             :         }
     631         992 :         else if ( ivas_total_brate <= IVAS_32k )
     632             :         {
     633         356 :             return 65;
     634             :         }
     635             :         else
     636             :         {
     637         636 :             return 80;
     638             :         }
     639             :     }
     640        6136 :     else if ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT )
     641             :     {
     642        1954 :         if ( ivas_total_brate <= IVAS_16k4 )
     643             :         {
     644         396 :             return 100;
     645             :         }
     646        1558 :         else if ( ivas_total_brate <= IVAS_24k4 )
     647             :         {
     648         148 :             return 200;
     649             :         }
     650        1410 :         else if ( ivas_total_brate <= IVAS_32k )
     651             :         {
     652         167 :             return 300;
     653             :         }
     654        1243 :         else if ( ivas_total_brate <= IVAS_192k )
     655             :         {
     656         889 :             return 500;
     657             :         }
     658         354 :         else if ( ivas_total_brate <= IVAS_256k )
     659             :         {
     660         110 :             return 1050;
     661             :         }
     662         244 :         else if ( ivas_total_brate <= IVAS_384k )
     663             :         {
     664         119 :             return 2000;
     665             :         }
     666             :         else
     667             :         {
     668         125 :             return 2500;
     669             :         }
     670             :     }
     671        4182 :     else if ( ivas_format == MASA_FORMAT )
     672             :     {
     673        1344 :         if ( ivas_total_brate <= IVAS_16k4 )
     674             :         {
     675         311 :             return 80;
     676             :         }
     677        1033 :         else if ( ivas_total_brate <= IVAS_32k )
     678             :         {
     679         241 :             return 125;
     680             :         }
     681         792 :         else if ( ivas_total_brate <= IVAS_48k )
     682             :         {
     683          82 :             return 205;
     684             :         }
     685         710 :         else if ( ivas_total_brate <= IVAS_96k )
     686             :         {
     687         375 :             return 240;
     688             :         }
     689         335 :         else if ( ivas_total_brate <= IVAS_128k )
     690             :         {
     691         113 :             return 305;
     692             :         }
     693         222 :         else if ( ivas_total_brate <= IVAS_160k )
     694             :         {
     695          26 :             return 425;
     696             :         }
     697         196 :         else if ( ivas_total_brate <= IVAS_192k )
     698             :         {
     699          60 :             return 630;
     700             :         }
     701         136 :         else if ( ivas_total_brate <= IVAS_256k )
     702             :         {
     703          31 :             return 850;
     704             :         }
     705         105 :         else if ( ivas_total_brate <= IVAS_384k )
     706             :         {
     707          53 :             return 1000;
     708             :         }
     709             :         else
     710             :         {
     711          52 :             return 1750;
     712             :         }
     713             :     }
     714        2838 :     else if ( ivas_format == MASA_ISM_FORMAT )
     715             :     {
     716        1678 :         if ( ivas_total_brate <= IVAS_16k4 )
     717             :         {
     718         319 :             return 80;
     719             :         }
     720        1359 :         else if ( ivas_total_brate <= IVAS_32k )
     721             :         {
     722         375 :             return 125 + 100;
     723             :         }
     724         984 :         else if ( ivas_total_brate <= IVAS_48k )
     725             :         {
     726         186 :             return 205 + 100;
     727             :         }
     728         798 :         else if ( ivas_total_brate <= IVAS_96k )
     729             :         {
     730         420 :             return 240 + 150;
     731             :         }
     732         378 :         else if ( ivas_total_brate <= IVAS_128k )
     733             :         {
     734          71 :             return 305 + 30;
     735             :         }
     736         307 :         else if ( ivas_total_brate <= IVAS_160k )
     737             :         {
     738          65 :             return 425 + 30;
     739             :         }
     740         242 :         else if ( ivas_total_brate <= IVAS_192k )
     741             :         {
     742          65 :             return 630 + 30;
     743             :         }
     744         177 :         else if ( ivas_total_brate <= IVAS_256k )
     745             :         {
     746          67 :             return 850 + 30;
     747             :         }
     748         110 :         else if ( ivas_total_brate <= IVAS_384k )
     749             :         {
     750          59 :             return 1000 + 30;
     751             :         }
     752             :         else
     753             :         {
     754          51 :             return 1750 + 30;
     755             :         }
     756             :     }
     757        1160 :     else if ( ivas_format == MC_FORMAT )
     758             :     {
     759        1160 :         if ( ivas_total_brate <= IVAS_13k2 )
     760             :         {
     761          67 :             return 80;
     762             :         }
     763        1093 :         else if ( ivas_total_brate <= IVAS_24k4 )
     764             :         {
     765         130 :             return 100;
     766             :         }
     767         963 :         else if ( ivas_total_brate <= IVAS_64k )
     768             :         {
     769         347 :             return 210;
     770             :         }
     771         616 :         else if ( ivas_total_brate <= IVAS_96k )
     772             :         {
     773         142 :             return 220;
     774             :         }
     775             :         else
     776             :         {
     777         474 :             return 300;
     778             :         }
     779             :     }
     780             : 
     781           0 :     return 50;
     782             : }
     783             : 
     784             : /*-------------------------------------------------------------------*
     785             :  * move_indices()
     786             :  *
     787             :  * Move indices inside the buffer or among two buffers
     788             :  *-------------------------------------------------------------------*/
     789             : 
     790        3791 : void move_indices(
     791             :     INDICE_HANDLE old_ind_list, /* i/o: old location of indices */
     792             :     INDICE_HANDLE new_ind_list, /* i/o: new location of indices */
     793             :     const int16_t nb_indices    /* i  : number of moved indices */
     794             : )
     795             : {
     796             :     int16_t i;
     797             : 
     798        3791 :     if ( new_ind_list < old_ind_list )
     799             :     {
     800       15354 :         for ( i = 0; i < nb_indices; i++ )
     801             :         {
     802       11610 :             new_ind_list[i].id = old_ind_list[i].id;
     803       11610 :             new_ind_list[i].value = old_ind_list[i].value;
     804       11610 :             new_ind_list[i].nb_bits = old_ind_list[i].nb_bits;
     805             : 
     806       11610 :             old_ind_list[i].nb_bits = -1;
     807             :         }
     808             :     }
     809          47 :     else if ( new_ind_list > old_ind_list )
     810             :     {
     811         188 :         for ( i = nb_indices - 1; i >= 0; i-- )
     812             :         {
     813         141 :             new_ind_list[i].id = old_ind_list[i].id;
     814         141 :             new_ind_list[i].value = old_ind_list[i].value;
     815         141 :             new_ind_list[i].nb_bits = old_ind_list[i].nb_bits;
     816             : 
     817         141 :             old_ind_list[i].nb_bits = -1;
     818             :         }
     819             :     }
     820             : 
     821        3791 :     return;
     822             : }
     823             : 
     824             : 
     825             : /*-------------------------------------------------------------------*
     826             :  * check_ind_list_limits()
     827             :  *
     828             :  * Check, if the maximum number of indices has been reached -> reallocate
     829             :  * Check, if we will not overwrite an existing indice -> adjust the location
     830             :  *-------------------------------------------------------------------*/
     831             : 
     832   247677615 : ivas_error check_ind_list_limits(
     833             :     BSTR_ENC_HANDLE hBstr /* i/o: encoder bitstream handle                    */
     834             : )
     835             : {
     836             :     Indice *ivas_ind_list_zero, *ivas_ind_list_last;
     837             :     ivas_error error;
     838             : 
     839   247677615 :     error = IVAS_ERR_OK;
     840   247677615 :     ivas_ind_list_zero = *( hBstr->ivas_ind_list_zero );
     841             : 
     842             :     /* check, if the maximum number of indices has been reached and re-allocate the buffer */
     843             :     /* the re-allocation can be avoided by increasing the limits in get_ivas_max_num_indices() or get_ivas_max_num_indices_metadata() */
     844   247677615 :     if ( ( &hBstr->ind_list[hBstr->nb_ind_tot] - ivas_ind_list_zero ) >= *( hBstr->ivas_max_num_indices ) )
     845             :     {
     846             : #ifdef DEBUGGING
     847             :         fprintf( stderr, "Warning: The maximum number of indices %d has been exceeded in frame %d! Increase the limits in get_ivas_max_num_indices() or get_max_num_indices_metadata().\n", *( hBstr->ivas_max_num_indices ), frame );
     848             : #endif
     849             : 
     850             :         /* reallocate the buffer of indices with increased limit */
     851           0 :         if ( ( error = ind_list_realloc( *hBstr->ivas_ind_list_zero, *( hBstr->ivas_max_num_indices ) + STEP_MAX_NUM_INDICES, hBstr->st_ivas ) ) != IVAS_ERR_OK )
     852             :         {
     853           0 :             return error;
     854             :         }
     855             :     }
     856             : 
     857             :     /* check, if we will not overwrite an existing indice */
     858   247677615 :     if ( hBstr->ind_list[hBstr->nb_ind_tot].nb_bits > 0 )
     859             :     {
     860           0 :         if ( hBstr->nb_ind_tot == 0 )
     861             :         {
     862             : #ifdef DEBUGGING
     863             :             fprintf( stderr, "Warning: Trying to overwrite an existing indice ID = %d in frame %d!\n", hBstr->ind_list[hBstr->nb_ind_tot].id, frame );
     864             : #endif
     865             :             /* move the pointer to the next available empty slot */
     866           0 :             ivas_ind_list_last = &ivas_ind_list_zero[*( hBstr->ivas_max_num_indices )];
     867           0 :             while ( hBstr->ind_list[0].nb_bits > 0 && hBstr->ind_list < ivas_ind_list_last )
     868             :             {
     869           0 :                 hBstr->ind_list++;
     870             :             }
     871             : 
     872           0 :             if ( hBstr->ind_list >= ivas_ind_list_last )
     873             :             {
     874             : #ifdef DEBUGGING
     875             :                 fprintf( stderr, "Warning: The maximum number of indices %d has been exceeded in frame %d! Increase the limits in get_ivas_max_num_indices() or get_max_num_indices_metadata().\n", *( hBstr->ivas_max_num_indices ), frame );
     876             : #endif
     877             : 
     878             :                 /* no available empty slot -> need to re-allocate the buffer */
     879           0 :                 if ( ( error = ind_list_realloc( *hBstr->ivas_ind_list_zero, *( hBstr->ivas_max_num_indices ) + STEP_MAX_NUM_INDICES, hBstr->st_ivas ) ) != IVAS_ERR_OK )
     880             :                 {
     881           0 :                     return error;
     882             :                 }
     883             :             }
     884             :         }
     885             :         else
     886             :         {
     887             : #ifdef DEBUGGING
     888             :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Buffer of indices corrupted in frame %d! Attempt to overwrite indice ID = %d (value: %d, bits: %d)!\n", frame, hBstr->ind_list[hBstr->nb_ind_tot].id, hBstr->ind_list[hBstr->nb_ind_tot].value, hBstr->ind_list[hBstr->nb_ind_tot].nb_bits );
     889             : #else
     890           0 :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Buffer of indices corrupted! Attempt to overwrite indice ID = %d (value: %d, bits: %d)!\n", hBstr->ind_list[hBstr->nb_ind_tot].id, hBstr->ind_list[hBstr->nb_ind_tot].value, hBstr->ind_list[hBstr->nb_ind_tot].nb_bits );
     891             : #endif
     892             :         }
     893             :     }
     894             : 
     895   247677615 :     return error;
     896             : }
     897             : 
     898             : 
     899             : /*-------------------------------------------------------------------*
     900             :  * push_indice()
     901             :  *
     902             :  * Push a new indice into the buffer
     903             :  *-------------------------------------------------------------------*/
     904             : 
     905             : #ifdef DEBUG_BS_READ_WRITE
     906             : ivas_error push_indice_(
     907             : #else
     908    15670642 : ivas_error push_indice(
     909             : #endif
     910             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle                    */
     911             :     int16_t id,            /* i  : ID of the indice                            */
     912             :     uint16_t value,        /* i  : value of the quantized indice               */
     913             :     int16_t nb_bits        /* i  : number of bits used to quantize the indice  */
     914             : #ifdef DEBUG_BS_READ_WRITE
     915             :     ,
     916             :     int16_t line,
     917             :     const char *func
     918             : #endif
     919             : )
     920             : {
     921             :     int16_t i;
     922             :     int16_t j;
     923             :     ivas_error error;
     924             : 
     925    15670642 :     error = IVAS_ERR_OK;
     926             : 
     927             : #ifdef DEBUG_BS_READ_WRITE
     928             :     printf( "%s: %d: %d: %d\n", func, line, nb_bits, value );
     929             : #endif
     930             : #ifdef DEBUGGING
     931             :     if ( nb_bits < ( 32 - 1 ) && ( value >> nb_bits ) > 0 )
     932             :     {
     933             :         return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Indice ID = %d with value %d exceeds the range of %d bits (frame %d) !\n", id, value, nb_bits, frame );
     934             :     }
     935             : 
     936             :     if ( nb_bits > 16 )
     937             :     {
     938             :         return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Indice ID = %d with value %d is trying to allocate %d bits which exceeds 16 bits (frame %d) !\n", id, value, nb_bits, frame );
     939             :     }
     940             : 
     941             : #endif
     942             : 
     943             :     /* check the limits of the list of indices */
     944    15670642 :     if ( ( error = check_ind_list_limits( hBstr ) ) != IVAS_ERR_OK )
     945             :     {
     946             : #ifdef DEBUGGING
     947             :         return IVAS_ERROR( error, "Error occured in push_indice() while re-allocating the list of indices (frame %d) !\n", frame );
     948             : #else
     949           0 :         return IVAS_ERROR( error, "Error occured in push_indice() while re-allocating the list of indices!\n" );
     950             : #endif
     951             :     }
     952             : 
     953             :     /* find the location in the list of indices based on ID */
     954    15670642 :     i = hBstr->nb_ind_tot;
     955    54943315 :     while ( i > 0 && id < hBstr->ind_list[i - 1].id )
     956             :     {
     957    39272673 :         i--;
     958             :     }
     959             : 
     960             :     /* shift indices, if the new ID is to be written somewhere inside the list */
     961    15670642 :     if ( i < hBstr->nb_ind_tot )
     962             :     {
     963    41943382 :         for ( j = hBstr->nb_ind_tot; j > i; j-- )
     964             :         {
     965    39272673 :             hBstr->ind_list[j].id = hBstr->ind_list[j - 1].id;
     966    39272673 :             hBstr->ind_list[j].nb_bits = hBstr->ind_list[j - 1].nb_bits;
     967    39272673 :             hBstr->ind_list[j].value = hBstr->ind_list[j - 1].value;
     968             :         }
     969             :     }
     970             : 
     971             : 
     972             :     /* store the new indice in the list */
     973    15670642 :     hBstr->ind_list[i].id = id;
     974    15670642 :     hBstr->ind_list[i].value = value;
     975    15670642 :     hBstr->ind_list[i].nb_bits = nb_bits;
     976             : 
     977             :     /* updates */
     978    15670642 :     hBstr->nb_ind_tot++;
     979    15670642 :     hBstr->nb_bits_tot += nb_bits;
     980             : 
     981    15670642 :     return error;
     982             : }
     983             : 
     984             : /*-------------------------------------------------------------------*
     985             :  * push_next_indice()
     986             :  *
     987             :  * Push a new indice into the buffer at the next position
     988             :  *-------------------------------------------------------------------*/
     989             : 
     990             : #ifdef DEBUG_BS_READ_WRITE
     991             : ivas_error push_next_indice_(
     992             : #else
     993   204351974 : ivas_error push_next_indice(
     994             : #endif
     995             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle                    */
     996             :     uint16_t value,        /* i  : value of the quantized indice               */
     997             :     int16_t nb_bits        /* i  : number of bits used to quantize the indice  */
     998             : #ifdef DEBUG_BS_READ_WRITE
     999             :     ,
    1000             :     int16_t line,
    1001             :     const char *func
    1002             : #endif
    1003             : )
    1004             : {
    1005             :     int16_t prev_id;
    1006             :     ivas_error error;
    1007             : 
    1008   204351974 :     error = IVAS_ERR_OK;
    1009             : 
    1010             : #ifdef DEBUG_BS_READ_WRITE
    1011             :     printf( "%s: %d: %d: %d\n", func, line, nb_bits, value );
    1012             : #endif
    1013             : #ifdef DEBUGGING
    1014             :     if ( nb_bits < ( 32 - 1 ) && ( value >> nb_bits ) > 0 )
    1015             :     {
    1016             :         return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Indice with value %d exceeds the range of %d bits (frame %d) !\n", value, nb_bits, frame );
    1017             :     }
    1018             : 
    1019             :     if ( nb_bits > 16 )
    1020             :     {
    1021             :         return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Indice with value %d is trying to allocate %d bits which exceeds 16 bits !\n", value, nb_bits );
    1022             :     }
    1023             : 
    1024             : #endif
    1025             : 
    1026             :     /* check the limits of the list of indices */
    1027   204351974 :     if ( ( error = check_ind_list_limits( hBstr ) ) != IVAS_ERR_OK )
    1028             :     {
    1029           0 :         return error;
    1030             :     }
    1031             : 
    1032             :     /* get the id of the previous indice -> it will be re-used */
    1033   204351974 :     if ( hBstr->nb_ind_tot > 0 )
    1034             :     {
    1035   203352868 :         prev_id = hBstr->ind_list[hBstr->nb_ind_tot - 1].id;
    1036             :     }
    1037             :     else
    1038             :     {
    1039      999106 :         prev_id = 0;
    1040             :     }
    1041             : 
    1042             :     /* store the values in the list */
    1043   204351974 :     hBstr->ind_list[hBstr->nb_ind_tot].id = prev_id;
    1044   204351974 :     hBstr->ind_list[hBstr->nb_ind_tot].value = value;
    1045   204351974 :     hBstr->ind_list[hBstr->nb_ind_tot].nb_bits = nb_bits;
    1046             : 
    1047             :     /* updates */
    1048   204351974 :     hBstr->nb_ind_tot++;
    1049   204351974 :     hBstr->nb_bits_tot += nb_bits;
    1050             : 
    1051   204351974 :     return error;
    1052             : }
    1053             : 
    1054             : 
    1055             : /*-------------------------------------------------------------------*
    1056             :  * push_next_bits()
    1057             :  * Push a bit buffer into the buffer at the next position
    1058             :  *-------------------------------------------------------------------*/
    1059             : 
    1060             : #ifdef DEBUG_BS_READ_WRITE
    1061             : ivas_error push_next_bits_(
    1062             : #else
    1063     1777019 : ivas_error push_next_bits(
    1064             : #endif
    1065             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle                     */
    1066             :     const uint16_t bits[], /* i  : bit buffer to pack, sequence of single bits  */
    1067             :     const int16_t nb_bits  /* i  : number of bits to pack                       */
    1068             : #ifdef DEBUG_BS_READ_WRITE
    1069             :     ,
    1070             :     int16_t line,
    1071             :     const char *func
    1072             : #endif
    1073             : )
    1074             : {
    1075             :     uint16_t code;
    1076             :     int16_t i, nb_bits_m15;
    1077             :     Indice *ptr;
    1078             :     int16_t prev_id;
    1079             :     ivas_error error;
    1080             : 
    1081     1777019 :     error = IVAS_ERR_OK;
    1082             : #ifdef DEBUG_BS_READ_WRITE
    1083             :     printf( "%s: %d: %d\n", func, line, nb_bits );
    1084             : #endif
    1085             : 
    1086     1777019 :     ptr = &hBstr->ind_list[hBstr->nb_ind_tot];
    1087             : 
    1088             :     /* get the id of the previous indice -> will be re-used */
    1089     1777019 :     if ( hBstr->nb_ind_tot > 0 )
    1090             :     {
    1091     1765979 :         prev_id = hBstr->ind_list[hBstr->nb_ind_tot - 1].id;
    1092             :     }
    1093             :     else
    1094             :     {
    1095       11040 :         prev_id = 0;
    1096             :     }
    1097     1777019 :     nb_bits_m15 = nb_bits - 15;
    1098             : 
    1099    16108649 :     for ( i = 0; i < nb_bits_m15; i += 16 )
    1100             :     {
    1101    14331630 :         code = (uint16_t) ( ( bits[i] << 15 ) | ( ( bits[i + 1] << 14 ) | ( ( bits[i + 2] << 13 ) | ( ( bits[i + 3] << 12 ) | ( ( bits[i + 4] << 11 ) | ( ( bits[i + 5] << 10 ) | ( ( bits[i + 6] << 9 ) | ( ( bits[i + 7] << 8 ) | ( ( bits[i + 8] << 7 ) | ( ( bits[i + 9] << 6 ) | ( ( bits[i + 10] << 5 ) | ( ( bits[i + 11] << 4 ) | ( ( bits[i + 12] << 3 ) | ( ( bits[i + 13] << 2 ) | ( ( bits[i + 14] << 1 ) | bits[i + 15] ) ) ) ) ) ) ) ) ) ) ) ) ) ) );
    1102             : 
    1103             :         /* check the limits of the list of indices */
    1104    14331630 :         if ( ( error = check_ind_list_limits( hBstr ) ) != IVAS_ERR_OK )
    1105             :         {
    1106             : #ifdef DEBUGGING
    1107             :             return IVAS_ERROR( error, "Error occured in push_next_bits() while re-allocating the list of indices (frame %d) !\n", frame );
    1108             : #else
    1109           0 :             return IVAS_ERROR( error, "Error occured in push_next_bits() while re-allocating the list of indices!\n" );
    1110             : #endif
    1111             :         }
    1112    14331630 :         ptr = &hBstr->ind_list[hBstr->nb_ind_tot];
    1113             : 
    1114    14331630 :         ptr->value = code;
    1115             : #ifdef DEBUG_BS_READ_WRITE
    1116             :         printf( "code: %d\n", code );
    1117             : #endif
    1118    14331630 :         ptr->nb_bits = 16;
    1119    14331630 :         ptr->id = prev_id;
    1120    14331630 :         hBstr->nb_ind_tot++;
    1121    14331630 :         ++ptr;
    1122             :     }
    1123             : 
    1124    15100388 :     for ( ; i < nb_bits; ++i )
    1125             :     {
    1126             :         /* check the limits of the list of indices */
    1127    13323369 :         if ( ( error = check_ind_list_limits( hBstr ) ) != IVAS_ERR_OK )
    1128             :         {
    1129             : #ifdef DEBUGGING
    1130             :             return IVAS_ERROR( error, "Error occured in push_next_bits() while re-allocating the list of indices (frame %d) !\n", frame );
    1131             : #else
    1132           0 :             return IVAS_ERROR( error, "Error occured in push_next_bits() while re-allocating the list of indices!\n" );
    1133             : #endif
    1134             :         }
    1135    13323369 :         ptr = &hBstr->ind_list[hBstr->nb_ind_tot];
    1136             : 
    1137    13323369 :         ptr->value = bits[i];
    1138             : #ifdef DEBUG_BS_READ_WRITE
    1139             :         printf( "value: %d\n", ptr->value );
    1140             : #endif
    1141    13323369 :         ptr->nb_bits = 1;
    1142    13323369 :         ptr->id = prev_id;
    1143    13323369 :         hBstr->nb_ind_tot++;
    1144    13323369 :         ++ptr;
    1145             :     }
    1146             : 
    1147     1777019 :     hBstr->nb_bits_tot = hBstr->nb_bits_tot + nb_bits;
    1148             : 
    1149     1777019 :     return error;
    1150             : }
    1151             : 
    1152             : 
    1153             : /*-------------------------------------------------------------------*
    1154             :  * find_indice()
    1155             :  *
    1156             :  * Find indice based on its id
    1157             :  *-------------------------------------------------------------------*/
    1158             : 
    1159             : /*! r: result: index of the indice in the list, -1 if not found */
    1160          25 : int16_t find_indice(
    1161             :     BSTR_ENC_HANDLE hBstr, /* i  : encoder bitstream handle                    */
    1162             :     const int16_t id,      /* i  : ID of the indice                            */
    1163             :     uint16_t *value,       /* o  : value of the quantized indice               */
    1164             :     int16_t *nb_bits       /* o  : number of bits used to quantize the indice  */
    1165             : )
    1166             : {
    1167             :     int16_t i;
    1168             : 
    1169         153 :     for ( i = 0; i < hBstr->nb_ind_tot; i++ )
    1170             :     {
    1171         153 :         if ( hBstr->ind_list[i].id == id && hBstr->ind_list[i].nb_bits > 0 )
    1172             :         {
    1173          25 :             *value = hBstr->ind_list[i].value;
    1174          25 :             *nb_bits = hBstr->ind_list[i].nb_bits;
    1175          25 :             return i;
    1176             :         }
    1177             :     }
    1178             : 
    1179           0 :     return -1;
    1180             : }
    1181             : 
    1182             : 
    1183             : /*-------------------------------------------------------------------*
    1184             :  * delete_indice()
    1185             :  *
    1186             :  * Delete indice based on its id (note, that nb_ind_tot and nb_bits_tot are updated)
    1187             :  *-------------------------------------------------------------------*/
    1188             : 
    1189             : /*! r: number of deleted indices */
    1190      675300 : uint16_t delete_indice(
    1191             :     BSTR_ENC_HANDLE hBstr, /* i  : encoder bitstream handle                    */
    1192             :     const int16_t id       /* i  : ID of the indice                            */
    1193             : )
    1194             : {
    1195             :     int16_t i, j;
    1196             : 
    1197      675300 :     j = 0;
    1198     9697592 :     for ( i = 0; i < hBstr->nb_ind_tot; i++ )
    1199             :     {
    1200     9022292 :         if ( hBstr->ind_list[i].id == id )
    1201             :         {
    1202        6125 :             hBstr->nb_bits_tot -= hBstr->ind_list[i].nb_bits;
    1203        6125 :             continue;
    1204             :         }
    1205             : 
    1206     9016167 :         if ( j < i )
    1207             :         {
    1208             :             /* shift the indice left */
    1209        3862 :             hBstr->ind_list[j].id = hBstr->ind_list[i].id;
    1210        3862 :             hBstr->ind_list[j].value = hBstr->ind_list[i].value;
    1211        3862 :             hBstr->ind_list[j].nb_bits = hBstr->ind_list[i].nb_bits;
    1212             :         }
    1213             : 
    1214     9016167 :         j++;
    1215             :     }
    1216             : 
    1217      675300 :     hBstr->nb_ind_tot = j;
    1218             : 
    1219      681425 :     for ( ; j < i; j++ )
    1220             :     {
    1221             :         /* reset the shifted indices at the end of the list */
    1222        6125 :         hBstr->ind_list[j].nb_bits = -1;
    1223             :     }
    1224             : 
    1225      675300 :     return i - j;
    1226             : }
    1227             : 
    1228             : 
    1229             : /*-------------------------------------------------------------------*
    1230             :  * get_next_indice()
    1231             :  *
    1232             :  * Get the next indice from the buffer
    1233             :  *-------------------------------------------------------------------*/
    1234             : 
    1235             : /*! r: value of the indice */
    1236             : #ifdef DEBUG_BS_READ_WRITE
    1237             : uint16_t get_next_indice_(
    1238             : #else
    1239   129018690 : uint16_t get_next_indice(
    1240             : #endif
    1241             :     Decoder_State *st, /* i/o: decoder state structure                               */
    1242             :     int16_t nb_bits    /* i  : number of bits that were used to quantize the indice  */
    1243             : #ifdef DEBUG_BS_READ_WRITE
    1244             :     ,
    1245             :     int16_t line,
    1246             :     const char *func
    1247             : #endif
    1248             : )
    1249             : {
    1250             :     uint16_t value;
    1251             :     int16_t i;
    1252             :     int32_t nbits_total;
    1253             : 
    1254   129018690 :     assert( nb_bits <= 16 );
    1255             : 
    1256   129018690 :     nbits_total = st->total_brate / FRAMES_PER_SEC;
    1257             :     /* detect corrupted bitstream */
    1258   129018690 :     if ( st->next_bit_pos + nb_bits > nbits_total )
    1259             :     {
    1260           0 :         st->BER_detect = 1;
    1261           0 :         return ( 0 );
    1262             :     }
    1263             : 
    1264   129018690 :     value = 0;
    1265   598890396 :     for ( i = 0; i < nb_bits; i++ )
    1266             :     {
    1267   469871706 :         value <<= 1;
    1268   469871706 :         value += st->bit_stream[st->next_bit_pos + i];
    1269             :     }
    1270             : #ifdef DEBUG_BS_READ_WRITE
    1271             :     printf( "%s: %d: %d: %d\n", func, line, nb_bits, value );
    1272             : #endif
    1273             : 
    1274             :     /* update the position in the bitstream */
    1275   129018690 :     st->next_bit_pos += nb_bits;
    1276             : 
    1277   129018690 :     return value;
    1278             : }
    1279             : 
    1280             : /*-------------------------------------------------------------------*
    1281             :  * get_next_indice_1()
    1282             :  *
    1283             :  * Get the next 1-bit indice from the buffer
    1284             :  *-------------------------------------------------------------------*/
    1285             : 
    1286             : /*! r: value of the indice */
    1287   560911155 : uint16_t get_next_indice_1(
    1288             :     Decoder_State *st /* i/o: decoder state structure   */
    1289             : )
    1290             : {
    1291             :     int32_t nbits_total;
    1292   560911155 :     nbits_total = st->total_brate / FRAMES_PER_SEC;
    1293             :     /* detect corrupted bitstream */
    1294   560911155 :     if ( ( st->next_bit_pos + 1 > nbits_total && st->codec_mode == MODE1 ) ||
    1295   560911155 :          ( ( st->next_bit_pos + 1 > nbits_total + ( 2 * 8 ) ) && st->codec_mode == MODE2 ) /* add two zero bytes for arithmetic coder flush */
    1296             :     )
    1297             :     {
    1298           0 :         st->BER_detect = 1;
    1299           0 :         return ( 0 );
    1300             :     }
    1301             : 
    1302   560911155 :     return st->bit_stream[st->next_bit_pos++];
    1303             : }
    1304             : 
    1305             : /*-------------------------------------------------------------------*
    1306             :  * get_next_indice_tmp()
    1307             :  *
    1308             :  * update the total number of bits and the position in the bitstream
    1309             :  *-------------------------------------------------------------------*/
    1310             : 
    1311     6133494 : void get_next_indice_tmp(
    1312             :     Decoder_State *st, /* o  : decoder state structure                              */
    1313             :     int16_t nb_bits    /* i  : number of bits that were used to quantize the indice */
    1314             : )
    1315             : {
    1316             :     /* update the position in the bitstream */
    1317     6133494 :     st->next_bit_pos += nb_bits;
    1318             : 
    1319     6133494 :     return;
    1320             : }
    1321             : 
    1322             : /*-------------------------------------------------------------------*
    1323             :  * get_indice()
    1324             :  *
    1325             :  * Get indice at specific position in the buffer
    1326             :  *-------------------------------------------------------------------*/
    1327             : 
    1328             : /*! r: value of the indice */
    1329             : #ifdef DEBUG_BS_READ_WRITE
    1330             : uint16_t get_indice_(
    1331             : #else
    1332     1726299 : uint16_t get_indice(
    1333             : #endif
    1334             :     Decoder_State *st, /* i/o: decoder state structure                                    */
    1335             :     int16_t pos,       /* i  : absolute position in the bitstream (update after the read) */
    1336             :     int16_t nb_bits    /* i  : number of bits that were used to quantize the indice       */
    1337             : #ifdef DEBUG_BS_READ_WRITE
    1338             :     ,
    1339             :     int16_t line,
    1340             :     const char *func
    1341             : #endif
    1342             : )
    1343             : {
    1344             :     uint16_t value;
    1345             :     int16_t i;
    1346             :     int32_t nbits_total;
    1347             : 
    1348     1726299 :     assert( nb_bits <= 16 );
    1349             : 
    1350     1726299 :     nbits_total = st->total_brate / FRAMES_PER_SEC;
    1351             : 
    1352             :     /* detect corrupted bitstream */
    1353     1726299 :     if ( pos + nb_bits > nbits_total )
    1354             :     {
    1355           0 :         st->BER_detect = 1;
    1356           0 :         return ( 0 );
    1357             :     }
    1358             : 
    1359     1726299 :     value = 0;
    1360    10880556 :     for ( i = 0; i < nb_bits; i++ )
    1361             :     {
    1362     9154257 :         value <<= 1;
    1363     9154257 :         value += st->bit_stream[pos + i];
    1364             :     }
    1365             : #ifdef DEBUG_BS_READ_WRITE
    1366             :     printf( "%s: %d: %d: %d\n", func, line, nb_bits, value );
    1367             : #endif
    1368     1726299 :     return value;
    1369             : }
    1370             : 
    1371             : /*-------------------------------------------------------------------*
    1372             :  * get_indice_st()
    1373             :  *
    1374             :  * Get indice at specific position in the buffer
    1375             :  *-------------------------------------------------------------------*/
    1376             : 
    1377             : /*! r: value of the indice */
    1378       57324 : uint16_t get_indice_st(
    1379             :     Decoder_State *st,           /* i/o: decoder state structure                 */
    1380             :     const int32_t element_brate, /* i  : element bitrate                         */
    1381             :     const int16_t pos,           /* i  : absolute position in the bitstream      */
    1382             :     const int16_t nb_bits        /* i  : number of bits to quantize the indice   */
    1383             : )
    1384             : {
    1385             :     uint16_t value;
    1386             :     int16_t i;
    1387             : 
    1388       57324 :     assert( nb_bits <= 16 );
    1389             : 
    1390             :     /* detect corrupted bitstream */
    1391       57324 :     if ( pos + nb_bits > element_brate / FRAMES_PER_SEC )
    1392             :     {
    1393           0 :         st->BER_detect = 1;
    1394           0 :         return ( 0 );
    1395             :     }
    1396             : 
    1397       57324 :     value = 0;
    1398      186180 :     for ( i = 0; i < nb_bits; i++ )
    1399             :     {
    1400      128856 :         value <<= 1;
    1401      128856 :         value += st->bit_stream[pos + i];
    1402             :     }
    1403             : 
    1404       57324 :     return value;
    1405             : }
    1406             : 
    1407             : /*-------------------------------------------------------------------*
    1408             :  * get_indice_1()
    1409             :  *
    1410             :  * Get a 1-bit indice at specific position in the buffer
    1411             :  *-------------------------------------------------------------------*/
    1412             : 
    1413             : /*! r: value of the indice */
    1414   208642077 : uint16_t get_indice_1(
    1415             :     Decoder_State *st, /* i/o: decoder state structure     */
    1416             :     int16_t pos        /* i  : absolute position in the bitstream (update after the read) */
    1417             : )
    1418             : {
    1419             :     int32_t nbits_total;
    1420   208642077 :     nbits_total = st->total_brate / FRAMES_PER_SEC;
    1421             :     /* detect corrupted bitstream */
    1422   208642077 :     if ( pos + 1 > nbits_total )
    1423             :     {
    1424           0 :         st->BER_detect = 1;
    1425           0 :         return ( 0 );
    1426             :     }
    1427             : 
    1428   208642077 :     return st->bit_stream[pos];
    1429             : }
    1430             : 
    1431             : 
    1432             : /*-------------------------------------------------------------------*
    1433             :  * reset_indices_enc()
    1434             :  *
    1435             :  * Reset the buffer of encoder indices
    1436             :  *-------------------------------------------------------------------*/
    1437             : 
    1438     1983856 : void reset_indices_enc(
    1439             :     BSTR_ENC_HANDLE hBstr,        /* i/o: encoder bitstream handle    */
    1440             :     const int16_t max_num_indices /* i  : max number of indices       */
    1441             : )
    1442             : {
    1443             :     int16_t i;
    1444             : 
    1445     1983856 :     hBstr->nb_bits_tot = 0;
    1446     1983856 :     hBstr->nb_ind_tot = 0;
    1447             : 
    1448   283057720 :     for ( i = 0; i < max_num_indices; i++ )
    1449             :     {
    1450   281073864 :         hBstr->ind_list[i].nb_bits = -1;
    1451             :     }
    1452             : 
    1453     1983856 :     return;
    1454             : }
    1455             : 
    1456             : /*-------------------------------------------------------------------*
    1457             :  * reset_indices_dec()
    1458             :  *
    1459             :  * Reset the buffer of decoder indices
    1460             :  *-------------------------------------------------------------------*/
    1461             : 
    1462     7076178 : void reset_indices_dec(
    1463             :     Decoder_State *st )
    1464             : {
    1465     7076178 :     st->next_bit_pos = 0;
    1466             : 
    1467     7076178 :     return;
    1468             : }
    1469             : 
    1470             : 
    1471             : /*-------------------------------------------------------------------*
    1472             :  * write_indices_to_stream()
    1473             :  *
    1474             :  * writing forward or backward to a serial stream
    1475             :  *-------------------------------------------------------------------*/
    1476             : 
    1477     1500441 : static int16_t write_indices_to_stream(
    1478             :     Indice *ind_list,
    1479             :     uint16_t **pt_stream,
    1480             :     const int16_t inc,
    1481             :     const int16_t num_indices )
    1482             : {
    1483             :     int16_t i, k;
    1484             :     int16_t value, nb_bits;
    1485             :     uint16_t mask;
    1486             : #ifdef ENABLE_BITRATE_VERIFICATION
    1487             :     int16_t total_nb_bits = 0;
    1488             : #endif
    1489             : 
    1490   192004491 :     for ( i = 0; i < num_indices; i++ )
    1491             :     {
    1492   190504050 :         value = ind_list[i].value;
    1493   190504050 :         nb_bits = ind_list[i].nb_bits;
    1494             : 
    1495   190504050 :         if ( nb_bits > 0 )
    1496             :         {
    1497             : #ifdef ENABLE_BITRATE_VERIFICATION
    1498             :             total_nb_bits += nb_bits;
    1499             : #endif
    1500             :             /* mask from MSB to LSB */
    1501   190406187 :             mask = 1 << ( nb_bits - 1 );
    1502             : 
    1503             :             /* write bit by bit */
    1504  1178550163 :             for ( k = 0; k < nb_bits; k++ )
    1505             :             {
    1506   988143976 :                 if ( value & mask )
    1507             :                 {
    1508   477136556 :                     **pt_stream = 1;
    1509   477136556 :                     *pt_stream += inc;
    1510             :                 }
    1511             :                 else
    1512             :                 {
    1513   511007420 :                     **pt_stream = 0;
    1514   511007420 :                     *pt_stream += inc;
    1515             :                 }
    1516             : 
    1517   988143976 :                 mask >>= 1;
    1518             :             }
    1519             :         }
    1520             : #ifdef DEBUGGING
    1521             :         else if ( nb_bits == 0 )
    1522             :         {
    1523             :             /* fprintf( stderr, "Warning: %s: nb_bits == 0!\n", __func__ ); */
    1524             :         }
    1525             :         else
    1526             :         {
    1527             :             /* fprintf( stderr, "Warning: %s: nb_bits == %d!\n", __func__, nb_bits ); */
    1528             :         }
    1529             : #endif
    1530             :     }
    1531             : #ifdef ENABLE_BITRATE_VERIFICATION
    1532             :     return total_nb_bits;
    1533             : #else
    1534     1500441 :     return 0;
    1535             : #endif
    1536             : }
    1537             : 
    1538             : /*-------------------------------------------------------------------*
    1539             :  * write_indices_element()
    1540             :  *
    1541             :  * Bitstream writing function of one element (one SCE or one CPE)
    1542             :  *-------------------------------------------------------------------*/
    1543             : 
    1544      774868 : static ivas_error write_indices_element(
    1545             :     Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure                                          */
    1546             :     uint16_t **pt_stream,    /* i  : pointer to bitstream buffer                                     */
    1547             :     const int16_t is_SCE,    /* i  : flag to distingusih SCE and CPE                                 */
    1548             :     const int16_t element_id /* i  : id of the SCE or CPE                                            */
    1549             : )
    1550             : {
    1551             :     int16_t ch;
    1552      774868 :     Encoder_State **sts = NULL;
    1553             :     uint16_t *pt_stream_loc;
    1554             :     uint16_t *pt_stream_backup;
    1555             :     uint16_t *pt_stream_end;
    1556             :     int16_t nb_bits_tot_metadata;
    1557             :     int16_t nb_ind_tot_metadata;
    1558             : 
    1559             :     Indice *ind_list_metadata;
    1560             :     int16_t n, n_channels;
    1561             : #ifdef ENABLE_BITRATE_VERIFICATION
    1562             :     int16_t total_nb_bits;
    1563             : #endif
    1564             :     ivas_error error;
    1565             : 
    1566      774868 :     error = IVAS_ERR_OK;
    1567             : 
    1568      774868 :     ind_list_metadata = NULL;
    1569      774868 :     nb_ind_tot_metadata = 0;
    1570             : 
    1571      774868 :     if ( st_ivas->hEncoderConfig->ivas_format == MONO_FORMAT )
    1572             :     {
    1573        3100 :         sts = st_ivas->hSCE[0]->hCoreCoder;
    1574        3100 :         nb_bits_tot_metadata = 0;
    1575             :     }
    1576             :     else
    1577             :     {
    1578      771768 :         nb_bits_tot_metadata = 0;
    1579      771768 :         if ( is_SCE && st_ivas->hSCE[element_id] != NULL )
    1580             :         {
    1581      350913 :             sts = st_ivas->hSCE[element_id]->hCoreCoder;
    1582             : 
    1583      350913 :             if ( st_ivas->hSCE[element_id]->hMetaData != NULL )
    1584             :             {
    1585      180485 :                 nb_bits_tot_metadata = st_ivas->hSCE[element_id]->hMetaData->nb_bits_tot;
    1586      180485 :                 ind_list_metadata = st_ivas->hSCE[element_id]->hMetaData->ind_list;
    1587      180485 :                 nb_ind_tot_metadata = st_ivas->hSCE[element_id]->hMetaData->nb_ind_tot;
    1588             :             }
    1589             :         }
    1590      420855 :         else if ( !is_SCE && st_ivas->hCPE[element_id] != NULL )
    1591             :         {
    1592      420855 :             sts = st_ivas->hCPE[element_id]->hCoreCoder;
    1593             : 
    1594      420855 :             if ( st_ivas->hCPE[element_id]->hMetaData != NULL )
    1595             :             {
    1596      251486 :                 nb_bits_tot_metadata = st_ivas->hCPE[element_id]->hMetaData->nb_bits_tot;
    1597      251486 :                 ind_list_metadata = st_ivas->hCPE[element_id]->hMetaData->ind_list;
    1598      251486 :                 nb_ind_tot_metadata = st_ivas->hCPE[element_id]->hMetaData->nb_ind_tot;
    1599             :             }
    1600             :         }
    1601             : #ifdef DEBUGGING
    1602             :         else
    1603             :         {
    1604             :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Bitstream writing error in frame %d. Exiting!\n", frame );
    1605             :         }
    1606             : #endif
    1607             :     }
    1608             : 
    1609      774868 :     n_channels = 1;
    1610      774868 :     if ( sts[0]->element_mode > IVAS_CPE_DFT )
    1611             :     {
    1612      361176 :         n_channels = CPE_CHANNELS;
    1613             :     }
    1614             : 
    1615             :     /*----------------------------------------------------------------*
    1616             :      * Bitstream packing (conversion of individual indices into a serial stream)
    1617             :      *----------------------------------------------------------------*/
    1618             : 
    1619      774868 :     pt_stream_loc = *pt_stream;
    1620      774868 :     pt_stream_end = pt_stream_loc;
    1621             : 
    1622     1910912 :     for ( n = 0; n < n_channels; n++ )
    1623             :     {
    1624             :         /* write the metadata buffer */
    1625     1136044 :         if ( n == 0 && nb_bits_tot_metadata != 0 )
    1626             :         {
    1627      364397 :             pt_stream_backup = pt_stream_loc;
    1628             : 
    1629      868459 :             for ( ch = 0; ch < n_channels; ch++ )
    1630             :             {
    1631      504062 :                 pt_stream_loc += sts[ch]->hBstr->nb_bits_tot;
    1632             :             }
    1633      364397 :             pt_stream_loc += nb_bits_tot_metadata - 1;
    1634      364397 :             pt_stream_end = pt_stream_loc + 1;
    1635             : 
    1636             : #ifdef ENABLE_BITRATE_VERIFICATION
    1637             :             total_nb_bits =
    1638             : #endif
    1639      364397 :                 write_indices_to_stream( ind_list_metadata, &pt_stream_loc, -1,
    1640             :                                          nb_ind_tot_metadata );
    1641             : 
    1642             : #ifdef ENABLE_BITRATE_VERIFICATION
    1643             :             if ( total_nb_bits != nb_bits_tot_metadata )
    1644             :             {
    1645             :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Bitstream write size mismatch! Written bits: %d vs. Reference bits: %d\n", total_nb_bits, nb_bits_tot_metadata );
    1646             :             }
    1647             : #endif
    1648             :             /* restore previous pointer position */
    1649      364397 :             pt_stream_loc = pt_stream_backup;
    1650             :         }
    1651             : #ifdef ENABLE_BITRATE_VERIFICATION
    1652             :         total_nb_bits =
    1653             : #endif
    1654     1136044 :             write_indices_to_stream( sts[n]->hBstr->ind_list, &pt_stream_loc, 1,
    1655     1136044 :                                      sts[n]->hBstr->nb_ind_tot );
    1656             : 
    1657             : #ifdef ENABLE_BITRATE_VERIFICATION
    1658             :         if ( total_nb_bits != sts[n]->hBstr->nb_bits_tot )
    1659             :         {
    1660             :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Bitstream write size mismatch! Written bits: %d vs. Reference bits: %d\n", total_nb_bits, sts[n]->hBstr->nb_bits_tot );
    1661             :         }
    1662             : #endif
    1663     1136044 :         if ( pt_stream_loc > pt_stream_end )
    1664             :         {
    1665      581060 :             pt_stream_end = pt_stream_loc;
    1666             :         }
    1667             :     }
    1668             : 
    1669             :     /*----------------------------------------------------------------*
    1670             :      * Clearing of indices
    1671             :      * Reset index pointers
    1672             :      *----------------------------------------------------------------*/
    1673             : 
    1674      774868 :     if ( is_SCE ) /* EVS and SCE */
    1675             :     {
    1676      354013 :         if ( st_ivas->hSCE[element_id]->hMetaData != NULL )
    1677             :         {
    1678      180485 :             reset_indices_enc( st_ivas->hSCE[element_id]->hMetaData, st_ivas->hSCE[element_id]->hMetaData->nb_ind_tot );
    1679             :         }
    1680             : 
    1681      354013 :         reset_indices_enc( sts[0]->hBstr, sts[0]->hBstr->nb_ind_tot );
    1682             :     }
    1683             :     else
    1684             :     {
    1685      420855 :         if ( st_ivas->hCPE[element_id]->hMetaData != NULL )
    1686             :         {
    1687      251486 :             reset_indices_enc( st_ivas->hCPE[element_id]->hMetaData, st_ivas->hCPE[element_id]->hMetaData->nb_ind_tot );
    1688             :         }
    1689             : 
    1690     1202886 :         for ( n = 0; n < n_channels; n++ )
    1691             :         {
    1692      782031 :             reset_indices_enc( sts[n]->hBstr, sts[n]->hBstr->nb_ind_tot );
    1693             :         }
    1694             :     }
    1695             : 
    1696             :     /* update pointer */
    1697      774868 :     *pt_stream = pt_stream_end;
    1698             : 
    1699      774868 :     return error;
    1700             : }
    1701             : 
    1702             : /*-------------------------------------------------------------------*
    1703             :  * write_indices_ivas()
    1704             :  *
    1705             :  * Write the buffer of indices to a serial bitstream buffer,
    1706             :  * each bit represented as a uint16_t of value 0 or 1
    1707             :  *-------------------------------------------------------------------*/
    1708             : 
    1709      427388 : ivas_error write_indices_ivas(
    1710             :     Encoder_Struct *st_ivas, /* i/o: encoder state structure             */
    1711             :     uint16_t *bit_stream,    /* i/o: output bitstream                    */
    1712             :     uint16_t *num_bits       /* i  : number of indices written to output */
    1713             : )
    1714             : {
    1715             :     int16_t i, n;
    1716             :     uint16_t *pt_stream;
    1717             : #ifdef ENABLE_BITRATE_VERIFICATION
    1718             :     Encoder_State **sts;
    1719             :     int32_t ivas_total_brate;
    1720             :     int16_t ch;
    1721             : #endif
    1722             :     ivas_error error;
    1723             : 
    1724      427388 :     error = IVAS_ERR_OK;
    1725             : 
    1726      427388 :     pt_stream = bit_stream;
    1727  4376880508 :     for ( i = 0; i < MAX_BITS_PER_FRAME; ++i )
    1728             :     {
    1729  4376453120 :         bit_stream[i] = 0;
    1730             :     }
    1731             : 
    1732             : #ifdef ENABLE_BITRATE_VERIFICATION
    1733             :     i = 0;
    1734             : 
    1735             :     for ( n = 0; n < st_ivas->nSCE; n++ )
    1736             :     {
    1737             :         sts = st_ivas->hSCE[n]->hCoreCoder;
    1738             :         i += sts[0]->hBstr->nb_bits_tot;
    1739             : 
    1740             :         if ( st_ivas->hSCE[n]->hMetaData != NULL )
    1741             :         {
    1742             :             i += st_ivas->hSCE[n]->hMetaData->nb_bits_tot;
    1743             :         }
    1744             :     }
    1745             : 
    1746             :     for ( n = 0; n < st_ivas->nCPE; n++ )
    1747             :     {
    1748             :         sts = st_ivas->hCPE[n]->hCoreCoder;
    1749             :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1750             :         {
    1751             :             i += sts[ch]->hBstr->nb_bits_tot;
    1752             :         }
    1753             : 
    1754             :         if ( st_ivas->hCPE[n]->hMetaData != NULL )
    1755             :         {
    1756             :             i += st_ivas->hCPE[n]->hMetaData->nb_bits_tot;
    1757             :         }
    1758             :     }
    1759             : 
    1760             :     ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
    1761             :     if ( st_ivas->hEncoderConfig->Opt_SC_VBR )
    1762             :     {
    1763             :         ivas_total_brate = st_ivas->hSCE[0]->hCoreCoder[0]->total_brate;
    1764             :     }
    1765             : 
    1766             :     if ( i * FRAMES_PER_SEC != ivas_total_brate && i >= ACELP_11k60 / FRAMES_PER_SEC )
    1767             :     {
    1768             :         return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Bitstream write size mismatch! Actual bitrate: %ld vs. Reference bitrate: %d\n", i * 50L, ivas_total_brate );
    1769             :     }
    1770             : #endif
    1771             : 
    1772             :     /*-----------------------------------------------------------------*
    1773             :      * Encode Payload
    1774             :      *-----------------------------------------------------------------*/
    1775             : 
    1776      781401 :     for ( n = 0; n < st_ivas->nSCE; n++ )
    1777             :     {
    1778      354013 :         write_indices_element( st_ivas, &pt_stream, 1, n );
    1779             :     }
    1780             : 
    1781      848243 :     for ( n = 0; n < st_ivas->nCPE; n++ )
    1782             :     {
    1783      420855 :         write_indices_element( st_ivas, &pt_stream, 0, n );
    1784             :     }
    1785             : 
    1786      427388 :     *num_bits = (uint16_t) ( pt_stream - bit_stream );
    1787             : 
    1788      427388 :     return error;
    1789             : }
    1790             : 
    1791             : 
    1792             : /*---------------------------------------------------------------------*
    1793             :  * convertSerialToBytestream( )
    1794             :  *
    1795             :  * convert 16-bit short serial streams with 0x0000 and 0x0001 to a bytstream
    1796             :  *---------------------------------------------------------------------*/
    1797             : 
    1798           0 : void convertSerialToBytestream(
    1799             :     const uint16_t *const serial, /* i  : input serial bitstream with values 0 and 1  */
    1800             :     const uint16_t num_bits,      /* i  : number of bits in the input bitstream       */
    1801             :     uint8_t *const bytestream     /* o  : output compact bitstream (bytestream)       */
    1802             : )
    1803             : {
    1804             :     uint32_t i;
    1805             :     uint8_t bit, bitinbyte;
    1806             : 
    1807           0 :     for ( i = 0; i < num_bits; ++i )
    1808             :     {
    1809           0 :         bit = ( serial[i] == 0x0001 ) ? 1 : 0;
    1810           0 :         bitinbyte = bit << ( 7 - ( i & 0x7 ) );
    1811           0 :         if ( !( i & 0x7 ) )
    1812             :         {
    1813           0 :             bytestream[i >> 3] = 0;
    1814             :         }
    1815           0 :         bytestream[i >> 3] |= bitinbyte;
    1816             :     }
    1817             : 
    1818           0 :     return;
    1819             : }
    1820             : 
    1821             : /*---------------------------------------------------------------------*
    1822             :  * convertBytestreamToSerial( )
    1823             :  *
    1824             :  * convert bytestream to 16-bit short serial streams with 0x0000 and 0x0001
    1825             :  *---------------------------------------------------------------------*/
    1826             : 
    1827           0 : void convertBytestreamToSerial(
    1828             :     const uint8_t *const bytestream, /* i  : input compact bitstream (bytestream)        */
    1829             :     const uint16_t num_bits,         /* i  : number of bits in the input bitstream       */
    1830             :     uint16_t *const serial           /* o  : output serial bitstream with values 0 and 1 */
    1831             : )
    1832             : {
    1833             :     uint32_t i;
    1834             : 
    1835           0 :     for ( i = 0; i < num_bits; ++i )
    1836             :     {
    1837           0 :         serial[i] = ( bytestream[( i >> 3 )] >> ( 7 - ( i & 7 ) ) ) & 0x1;
    1838             :     }
    1839           0 : }
    1840             : 
    1841             : 
    1842             : /*-------------------------------------------------------------------*
    1843             :  * decoder_selectCodec()
    1844             :  *
    1845             :  *
    1846             :  *-------------------------------------------------------------------*/
    1847             : 
    1848        9300 : static void decoder_selectCodec(
    1849             :     Decoder_State *st,         /* i/o: decoder state structure                */
    1850             :     const int32_t total_brate, /* i  : total bitrate                          */
    1851             :     const int16_t bit0         /* i  : first bit                              */
    1852             : )
    1853             : {
    1854             :     /* set the AMR-WB IO flag */
    1855        9300 :     if ( rate2AMRWB_IOmode( total_brate ) != -1 )
    1856             :     {
    1857           0 :         st->Opt_AMR_WB = 1;
    1858             :     }
    1859        9300 :     else if ( total_brate != FRAME_NO_DATA )
    1860             :     {
    1861        9300 :         st->Opt_AMR_WB = 0;
    1862             :     }
    1863             : 
    1864        9300 :     if ( st->Opt_AMR_WB )
    1865             :     {
    1866           0 :         st->codec_mode = MODE1;
    1867             :     }
    1868             :     else
    1869             :     {
    1870        9300 :         st->codec_mode = get_codec_mode( total_brate );
    1871             : 
    1872        9300 :         if ( st->codec_mode == -1 )
    1873             :         {
    1874           0 :             switch ( total_brate )
    1875             :             {
    1876           0 :                 case 0:
    1877           0 :                     st->codec_mode = st->last_codec_mode;
    1878           0 :                     break;
    1879           0 :                 case 2400:
    1880           0 :                     st->codec_mode = st->last_codec_mode;
    1881           0 :                     break;
    1882           0 :                 case 2800:
    1883           0 :                     st->codec_mode = MODE1;
    1884           0 :                     break;
    1885           0 :                 default: /* validate that total_brate (derived from RTP packet or a file header) is one of the defined bitrates  */
    1886             : #ifdef DEBUGGING
    1887             :                     IVAS_ERROR( IVAS_ERR_INTERNAL, "Error illegal total bitrate (= %d) \n", total_brate );
    1888             : #endif
    1889           0 :                     st->codec_mode = st->last_codec_mode;
    1890           0 :                     st->bfi = 1;
    1891           0 :                     break;
    1892             :             }
    1893        9300 :         }
    1894             :     }
    1895             : 
    1896        9300 :     if ( st->ini_frame == 0 )
    1897             :     {
    1898           9 :         if ( st->codec_mode == -1 )
    1899             :         {
    1900           0 :             st->codec_mode = MODE1;
    1901             :         }
    1902           9 :         st->last_codec_mode = st->codec_mode;
    1903             :     }
    1904             : 
    1905             :     /* set SID/CNG type */
    1906        9300 :     if ( total_brate == SID_2k40 )
    1907             :     {
    1908           0 :         if ( bit0 == 0 )
    1909             :         {
    1910           0 :             st->cng_type = LP_CNG;
    1911             : 
    1912             :             /* force MODE1 when selecting LP_CNG */
    1913           0 :             st->codec_mode = MODE1;
    1914             :         }
    1915           0 :         else if ( bit0 == 1 )
    1916             :         {
    1917           0 :             st->cng_type = FD_CNG;
    1918           0 :             if ( st->last_codec_mode == MODE2 && st->last_total_brate == ACELP_13k20 )
    1919             :             {
    1920           0 :                 st->codec_mode = MODE1;
    1921             :             }
    1922             :         }
    1923             :     }
    1924             : 
    1925        9300 :     return;
    1926             : }
    1927             : 
    1928             : /*-------------------------------------------------------------------*
    1929             :  * dec_prm_core()
    1930             :  *
    1931             :  *
    1932             :  *-------------------------------------------------------------------*/
    1933             : 
    1934        3150 : static void dec_prm_core(
    1935             :     Decoder_State *st )
    1936             : {
    1937        3150 :     int16_t n, frame_size_index = -1;
    1938             : 
    1939        3150 :     st->core = -1;
    1940             : 
    1941        3150 :     if ( st->total_brate == FRAME_NO_DATA )
    1942             :     {
    1943           0 :         st->m_frame_type = ZERO_FRAME;
    1944             :     }
    1945        3150 :     else if ( st->total_brate == SID_2k40 )
    1946             :     {
    1947           0 :         st->m_frame_type = SID_FRAME;
    1948             :     }
    1949             :     else
    1950             :     {
    1951        3150 :         st->m_frame_type = ACTIVE_FRAME;
    1952       25200 :         for ( n = 0; n < FRAME_SIZE_NB; ++n )
    1953             :         {
    1954       25200 :             if ( FrameSizeConfig[n].frame_bits == st->total_brate / FRAMES_PER_SEC )
    1955             :             {
    1956        3150 :                 frame_size_index = n;
    1957        3150 :                 break;
    1958             :             }
    1959             :         }
    1960             : #ifdef DEBUGGING
    1961             :         if ( n == FRAME_SIZE_NB )
    1962             :         {
    1963             :             assert( !"Bitrate not supported: not part of EVS" );
    1964             :         }
    1965             : #endif
    1966             : 
    1967             :         /* Get audio bandwidth info */
    1968        3150 :         st->bwidth = get_next_indice( st, FrameSizeConfig[frame_size_index].bandwidth_bits );
    1969        3150 :         st->bwidth += FrameSizeConfig[frame_size_index].bandwidth_min;
    1970        3150 :         if ( st->bwidth > FB )
    1971             :         {
    1972           0 :             st->bwidth = FB;
    1973           0 :             st->BER_detect = 1;
    1974             :         }
    1975             : 
    1976        3150 :         if ( st->bwidth > SWB && st->total_brate < ACELP_16k40 )
    1977             :         {
    1978           0 :             st->bwidth = SWB;
    1979           0 :             st->BER_detect = 1;
    1980             :         }
    1981             :         /* Skip reserved bit */
    1982        3150 :         get_next_indice_tmp( st, FrameSizeConfig[frame_size_index].reserved_bits );
    1983             : 
    1984        3150 :         if ( get_next_indice_1( st ) ) /* TCX */
    1985             :         {
    1986        1338 :             if ( get_next_indice_1( st ) )
    1987             :             {
    1988         225 :                 st->core = HQ_CORE;
    1989             :             }
    1990             :             else
    1991             :             {
    1992        1113 :                 st->core = TCX_20_CORE;
    1993             :             }
    1994             :         }
    1995             :         else /* ACELP */
    1996             :         {
    1997        1812 :             st->core = ACELP_CORE;
    1998             :         }
    1999             :     }
    2000             : 
    2001        3150 :     return;
    2002             : }
    2003             : 
    2004             : /*-----------------------------------------------------------------*
    2005             :  * decision_matrix_core_dec()
    2006             :  *
    2007             :  * Read core signaling bits from the bitstream
    2008             :  * Set st->core, and st->bwidth if signalled together with the core.
    2009             :  *-----------------------------------------------------------------*/
    2010             : 
    2011        3150 : static void decision_matrix_core_dec(
    2012             :     Decoder_State *st /* i/o: decoder state structure                   */
    2013             : )
    2014             : {
    2015             :     int16_t start_idx;
    2016             :     int32_t ind;
    2017             :     int16_t nBits;
    2018             : 
    2019        3150 :     assert( st->bfi != 1 );
    2020             : 
    2021        3150 :     st->core = -1;
    2022        3150 :     st->bwidth = -1;
    2023             : 
    2024        3150 :     if ( st->total_brate == FRAME_NO_DATA || st->total_brate == SID_2k40 )
    2025             :     {
    2026           0 :         st->core = ACELP_CORE;
    2027             :     }
    2028             :     /* SC-VBR */
    2029        3150 :     else if ( st->total_brate == PPP_NELP_2k80 )
    2030             :     {
    2031           0 :         st->core = ACELP_CORE;
    2032           0 :         return;
    2033             :     }
    2034             : 
    2035             :     /*---------------------------------------------------------------------*
    2036             :      * ACELP/HQ core selection
    2037             :      *---------------------------------------------------------------------*/
    2038             : 
    2039        3150 :     if ( st->total_brate < ACELP_24k40 )
    2040             :     {
    2041        3150 :         st->core = ACELP_CORE;
    2042             :     }
    2043           0 :     else if ( st->total_brate >= ACELP_24k40 && st->total_brate <= ACELP_64k )
    2044             :     {
    2045             :         /* read the ACELP/HQ core selection bit */
    2046           0 :         st->core = get_next_indice( st, 1 ) * HQ_CORE;
    2047             :     }
    2048             :     else
    2049             :     {
    2050           0 :         st->core = HQ_CORE;
    2051             :     }
    2052             : 
    2053             :     /*-----------------------------------------------------------------*
    2054             :      * Read ACELP signaling bits from the bitstream
    2055             :      *-----------------------------------------------------------------*/
    2056             : 
    2057        3150 :     if ( st->core == ACELP_CORE )
    2058             :     {
    2059             :         /* find the section in the ACELP signaling table corresponding to bitrate */
    2060        3150 :         start_idx = 0;
    2061      110250 :         while ( acelp_sig_tbl[start_idx] != st->total_brate )
    2062             :         {
    2063      107100 :             start_idx++;
    2064             : #ifdef DEBUGGING
    2065             :             assert( ( start_idx < 194 ) && "ERROR: start_idx larger than acelp_sig_tbl[].\n" );
    2066             : #endif
    2067             :         }
    2068             : 
    2069             :         /* skip the bitrate */
    2070        3150 :         start_idx += 1;
    2071             : 
    2072             :         /* retrieve the number of bits */
    2073        3150 :         nBits = (int16_t) acelp_sig_tbl[start_idx++];
    2074             : 
    2075             :         /* retrieve the signaling indice */
    2076        3150 :         ind = acelp_sig_tbl[start_idx + get_next_indice( st, nBits )];
    2077        3150 :         st->bwidth = ( ind >> 3 ) & 0x7;
    2078             : 
    2079             :         /* convert signaling indice into signaling information */
    2080        3150 :         if ( ( ind & 0x7 ) == LR_MDCT )
    2081             :         {
    2082         927 :             st->core = HQ_CORE;
    2083             :         }
    2084             :     }
    2085             : 
    2086             :     /*-----------------------------------------------------------------*
    2087             :      * Read HQ signaling bits from the bitstream
    2088             :      * Set HQ core type
    2089             :      *-----------------------------------------------------------------*/
    2090             : 
    2091        3150 :     if ( st->core == HQ_CORE )
    2092             :     {
    2093             :         /* read the HQ/TCX core switching flag */
    2094         927 :         if ( get_next_indice( st, 1 ) )
    2095             :         {
    2096         825 :             st->core = TCX_20_CORE;
    2097             :         }
    2098             : 
    2099             :         /* For TCX: read/set band-width (needed for different I/O sampling rate support) */
    2100         927 :         if ( st->core == TCX_20_CORE && st->total_brate > ACELP_16k40 )
    2101             :         {
    2102           0 :             ind = get_next_indice( st, 2 );
    2103             : 
    2104           0 :             if ( ind == 0 )
    2105             :             {
    2106           0 :                 st->bwidth = NB;
    2107             :             }
    2108           0 :             else if ( ind == 1 )
    2109             :             {
    2110           0 :                 st->bwidth = WB;
    2111             :             }
    2112           0 :             else if ( ind == 2 )
    2113             :             {
    2114           0 :                 st->bwidth = SWB;
    2115             :             }
    2116             :             else
    2117             :             {
    2118           0 :                 st->bwidth = FB;
    2119             :             }
    2120             :         }
    2121             :     }
    2122             : 
    2123        3150 :     return;
    2124             : }
    2125             : 
    2126             : /*-------------------------------------------------------------------*
    2127             :  * mdct_switching_dec()
    2128             :  *
    2129             :  * Set up MDCT core switching if indicated in the bitstream
    2130             :  *-------------------------------------------------------------------*/
    2131             : 
    2132        9300 : void mdct_switching_dec(
    2133             :     Decoder_State *st /* i/o: decoder state structure     */
    2134             : )
    2135             : {
    2136        9300 :     if ( !st->bfi )
    2137             :     {
    2138             : 
    2139        9300 :         if ( st->Opt_AMR_WB )
    2140             :         {
    2141           0 :             return;
    2142             :         }
    2143             : 
    2144             : 
    2145        9300 :         if ( st->total_brate == ACELP_13k20 || st->total_brate == ACELP_32k )
    2146             :         {
    2147        3150 :             st->mdct_sw_enable = MODE1;
    2148             :         }
    2149        6150 :         else if ( ACELP_16k40 <= st->total_brate && st->total_brate <= ACELP_24k40 )
    2150             :         {
    2151        3150 :             st->mdct_sw_enable = MODE2;
    2152             :         }
    2153             : 
    2154        9300 :         if ( st->codec_mode == MODE1 && st->mdct_sw_enable == MODE1 )
    2155        3150 :         {
    2156             :             /* Read ahead core signaling */
    2157        3150 :             int16_t next_bit_pos_save = st->next_bit_pos;
    2158        3150 :             int16_t core_save = st->core;
    2159        3150 :             int16_t bwidth_save = st->bwidth;
    2160             : 
    2161        3150 :             decision_matrix_core_dec( st ); /* sets st->core */
    2162             : 
    2163        3150 :             if ( st->core == TCX_20_CORE )
    2164             :             {
    2165             :                 /* Trigger TCX */
    2166         825 :                 st->codec_mode = MODE2;
    2167         825 :                 st->mdct_sw = MODE1;
    2168             :             }
    2169             :             else
    2170             :             {
    2171             :                 /* Rewind bitstream */
    2172        2325 :                 st->next_bit_pos = next_bit_pos_save;
    2173        2325 :                 if ( st->bfi )
    2174             :                 {
    2175           0 :                     st->core = core_save;
    2176           0 :                     st->bwidth = bwidth_save;
    2177             :                 }
    2178             :             }
    2179             :         }
    2180        6150 :         else if ( st->codec_mode == MODE2 && st->mdct_sw_enable == MODE2 )
    2181             :         {
    2182             :             /* Read ahead core signaling */
    2183        3150 :             int16_t next_bit_pos_save = st->next_bit_pos;
    2184        3150 :             int16_t core_save = st->core;
    2185        3150 :             int16_t bwidth_save = st->bwidth;
    2186             : 
    2187        3150 :             dec_prm_core( st ); /* sets st->core */
    2188             : 
    2189        3150 :             if ( st->core == HQ_CORE )
    2190             :             {
    2191             :                 /* Trigger HQ_CORE */
    2192         225 :                 st->codec_mode = MODE1;
    2193         225 :                 st->mdct_sw = MODE2;
    2194             :             }
    2195             :             else
    2196             :             {
    2197             :                 /* Rewind bitstream */
    2198        2925 :                 st->next_bit_pos = next_bit_pos_save;
    2199        2925 :                 if ( st->bfi )
    2200             :                 {
    2201           0 :                     st->core = core_save;
    2202             :                 }
    2203             :                 /* always reset bwidth, to not interfere with BER logic */
    2204        2925 :                 st->bwidth = bwidth_save;
    2205             :             }
    2206             :         }
    2207             :     }
    2208             : 
    2209        9300 :     return;
    2210             : }
    2211             : 
    2212             : #ifdef DEBUGGING
    2213             : #ifdef ALLOW_BYTE_EP
    2214             : /*-------------------------------------------------------------------*
    2215             :  * ep_type_check()
    2216             :  *
    2217             :  *
    2218             :  *-------------------------------------------------------------------*/
    2219             : static Word16 ep_type_check()
    2220             : {
    2221             :     static int16_t ep_type = -1; /* 0=G192 (0x6b21 or 0x6b20),   1=byte(0x21 or 0x20) ,
    2222             :                                 2=ascii "0xa30" or "0xa31" , 3=short( 0x0000 or 0x0001) */
    2223             :     int16_t tmp;
    2224             : 
    2225             :     if ( ep_type < 0 )
    2226             :     {
    2227             :         tmp = 0;
    2228             : 
    2229             :         if ( fread( &tmp, sizeof( int16_t ), 1, FEC_pattern ) != 1 )
    2230             :         {
    2231             :             IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error reading first two bytes from the FEC pattern file !" );
    2232             :         }
    2233             :         else
    2234             :         {
    2235             :             switch ( tmp )
    2236             :             {
    2237             :                 case G192_SYNC_BAD_FRAME:
    2238             :                 case G192_SYNC_GOOD_FRAME:
    2239             :                     ep_type = 0; /* G192 */
    2240             :                     break;
    2241             :                 case 0x2020:     /* BAD,BAD */
    2242             :                 case 0x2021:     /* BAD,SYNC */
    2243             :                 case 0x2120:     /* SYNC,BAD */
    2244             :                 case 0x2121:     /* SYNC,SYNC */
    2245             :                     ep_type = 1; /* byte */
    2246             :                     break;
    2247             :                 case 0xa31:
    2248             :                 case 0xa30:
    2249             :                     ep_type = 2; /* ascii */
    2250             :                     break;
    2251             :                 case 0x0000:
    2252             :                 case 0x0001:
    2253             :                     ep_type = 3; /* short */
    2254             :                     break;
    2255             :                 default:
    2256             :                     IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error reading the FEC pattern file :: illegal format of the first two byte word=0x%02x ", tmp );
    2257             :                     IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Allowed formats are G192(0x6b20,0x6b21), byte(0x20,0x21), ASCII(\"0\n\" \"1\n\") or  short(0x0000, 0x0001) \n" );
    2258             :                     break;
    2259             :             }
    2260             : 
    2261             :             fseek( FEC_pattern, 0L, SEEK_SET ); /* rewind */
    2262             :         }
    2263             :     }
    2264             :     return ep_type;
    2265             : }
    2266             : 
    2267             : /*-------------------------------------------------------------------*
    2268             :  * ep_flag_check_apply()
    2269             :  *
    2270             :  *
    2271             :  *-------------------------------------------------------------------*/
    2272             : 
    2273             : static Word16 ep_flag_check_apply(
    2274             :     Word16 ep_type,
    2275             :     Word16 tmp,
    2276             :     Word16 bfi_so_far )
    2277             : {
    2278             :     if ( ep_type == 0 && ( tmp == G192_SYNC_GOOD_FRAME || tmp == G192_SYNC_BAD_FRAME ) )
    2279             :     { /* g192 validity check */
    2280             :         if ( tmp == G192_SYNC_BAD_FRAME )
    2281             :         {
    2282             :             return 1;
    2283             :         }
    2284             :     }
    2285             :     else if ( ep_type == 1 && ( tmp == 0x0021 || tmp == 0x0020 ) )
    2286             :     { /* byte  validity check*/
    2287             :         if ( tmp == 0x0020 )
    2288             :         {
    2289             :             return 1;
    2290             :         }
    2291             :     }
    2292             :     else if ( ep_type == 2 && ( tmp == 0x0a31 || tmp == 0x0a30 ) )
    2293             :     { /* ascii  validity check */
    2294             :         if ( tmp == 0x0a31 )
    2295             :         {
    2296             :             return 1;
    2297             :         }
    2298             :     }
    2299             :     else if ( ep_type == 3 && ( tmp == 0 || tmp == 1 ) )
    2300             :     { /* short validity check */
    2301             :         if ( tmp == 0x0001 )
    2302             :         {
    2303             :             return 1;
    2304             :         }
    2305             :     }
    2306             :     else
    2307             :     {
    2308             :         IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error reading the FEC pattern flag 0x%02x  from the type %d FEC pattern file , frame=%d", tmp, ep_type, frame );
    2309             :     }
    2310             : 
    2311             :     return bfi_so_far; /* no change */
    2312             : }
    2313             : 
    2314             : /*-------------------------------------------------------------------*
    2315             :  * read_error_flag ( )
    2316             :  *
    2317             :  *
    2318             :  *-------------------------------------------------------------------*/
    2319             : 
    2320             : static Word16 read_error_flag(
    2321             :     Word16 bfi_so_far )
    2322             : {
    2323             :     Word16 tmp;
    2324             :     Word16 ep_type;
    2325             :     Word16 wrap;
    2326             :     wrap = 0;
    2327             : 
    2328             :     ep_type = ep_type_check();
    2329             :     tmp = 0;
    2330             : 
    2331             :     if ( ( ep_type == 1 ) ? ( fread( &tmp, sizeof( int8_t ), 1, FEC_pattern ) != 1 ) /* read byte directly stored in short variable  */
    2332             :                           : ( fread( &tmp, sizeof( int16_t ), 1, FEC_pattern ) != 1 ) )
    2333             :     {
    2334             :         if ( feof( FEC_pattern ) != 0 )
    2335             :         {
    2336             :             wrap = 1; /*  wrap event flag   */
    2337             :             fseek( FEC_pattern, 0L, SEEK_SET );
    2338             : #ifndef WRAP_AS_EIDXOR
    2339             :             /* good frame injected in wrap event */
    2340             :             switch ( ep_type )
    2341             :             {
    2342             :                 case 0:
    2343             :                     tmp = SYNC_GOOD_FRAME;
    2344             :                     break;
    2345             :                 case 1:
    2346             :                     tmp = 0x21;
    2347             :                     break;
    2348             :                 case 2:
    2349             :                     tmp = 0x0a30;
    2350             :                     break;
    2351             :                 default:
    2352             :                     tmp = 0;
    2353             :                     break; /* 4: short */
    2354             :             }
    2355             : #endif
    2356             :         }
    2357             :         else
    2358             :         {
    2359             :             IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error reading the FEC pattern file !" );
    2360             :         }
    2361             :     }
    2362             : #ifdef WRAP_AS_EIDXOR
    2363             :     if ( wrap != 0 ) /* wrap file and read again,  try to get the next flag , */
    2364             :     {
    2365             :         tmp = 0;                                                                         /* needed for byte re-reading */
    2366             :         if ( ( ep_type == 1 ) ? ( fread( &tmp, sizeof( int8_t ), 1, FEC_pattern ) != 1 ) /* read byte directly stored in short variable */
    2367             :                               : ( fread( &tmp, sizeof( int16_t ), 1, FEC_pattern ) != 1 ) )
    2368             :         {
    2369             :             IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error reading the wrapped FEC pattern file !" );
    2370             :         }
    2371             :     }
    2372             : 
    2373             :     return ep_flag_check_apply( ep_type, tmp, bfi_so_far );
    2374             : #else
    2375             :     if ( wrap == 0 )
    2376             :     {
    2377             :         return ep_flag_check_apply( ep_type, tmp, bfi_so_far );
    2378             :     }
    2379             :     else
    2380             :     {
    2381             :         return bfi_so_far;
    2382             :     }
    2383             : #endif
    2384             : }
    2385             : #endif
    2386             : #endif
    2387             : 
    2388             : 
    2389             : #ifdef DEBUGGING
    2390             : /*-------------------------------------------------------------------*
    2391             :  * file_read_FECpattern()
    2392             :  *
    2393             :  * Simulate packet losses by reading FEC pattern from external file
    2394             :  *-------------------------------------------------------------------*/
    2395             : 
    2396             : static ivas_error file_read_FECpattern(
    2397             :     int16_t *bfi )
    2398             : {
    2399             :     ivas_error error;
    2400             : 
    2401             :     error = IVAS_ERR_OK;
    2402             :     *bfi = 0;
    2403             : 
    2404             :     /* FEC pattern file provided */
    2405             :     if ( FEC_pattern != NULL )
    2406             :     {
    2407             :         int16_t tmp = 0;
    2408             :         if ( fread( &tmp, sizeof( int16_t ), 1, FEC_pattern ) != 1 )
    2409             :         {
    2410             :             if ( feof( FEC_pattern ) != 0 )
    2411             :             {
    2412             : #ifdef WRAP_AS_EIDXOR
    2413             :                 fseek( FEC_pattern, 0L, SEEK_SET );
    2414             :                 fread( &tmp, sizeof( int16_t ), 1, FEC_pattern );
    2415             : #else
    2416             :                 tmp = 0;
    2417             :                 fseek( FEC_pattern, 0L, SEEK_SET );
    2418             : #endif
    2419             :             }
    2420             :             else
    2421             :             {
    2422             :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error reading the FEC pattern file !" );
    2423             :             }
    2424             :         }
    2425             : 
    2426             :         if ( tmp == 2609 || tmp == 1 || tmp == (uint16_t) 0x6B20 /* == G192_SYNC_BAD_FRAME */ )
    2427             :         {
    2428             :             *bfi = 1;
    2429             :         }
    2430             :         else
    2431             :         {
    2432             :             *bfi = 0;
    2433             :         }
    2434             :     }
    2435             : 
    2436             :     return error;
    2437             : }
    2438             : #endif
    2439             : 
    2440             : 
    2441             : /*-------------------------------------------------------------------*
    2442             :  * reset_elements()
    2443             :  *
    2444             :  * Simulate packet losses by reading FEC pattern from external file
    2445             :  *-------------------------------------------------------------------*/
    2446             : 
    2447     2507985 : Decoder_State **reset_elements(
    2448             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure      */
    2449             : )
    2450             : {
    2451             :     int16_t k, n;
    2452     2507985 :     Decoder_State **sts = NULL; /* to avoid compilation warning */
    2453             : 
    2454     4588977 :     for ( k = 0; k < st_ivas->nSCE; k++ )
    2455             :     {
    2456     2080992 :         sts = st_ivas->hSCE[k]->hCoreCoder;
    2457             : 
    2458     2080992 :         sts[0]->bfi = 0;
    2459     2080992 :         sts[0]->BER_detect = 0;
    2460     2080992 :         sts[0]->mdct_sw_enable = 0;
    2461     2080992 :         sts[0]->mdct_sw = 0;
    2462             : 
    2463     2080992 :         reset_indices_dec( sts[0] );
    2464             :     }
    2465             : 
    2466     5003022 :     for ( k = 0; k < st_ivas->nCPE; k++ )
    2467             :     {
    2468     2495037 :         sts = st_ivas->hCPE[k]->hCoreCoder;
    2469             : 
    2470     7485111 :         for ( n = 0; n < CPE_CHANNELS; n++ )
    2471             :         {
    2472     4990074 :             sts[n]->bfi = 0;
    2473     4990074 :             sts[n]->BER_detect = 0;
    2474     4990074 :             sts[n]->mdct_sw_enable = 0;
    2475     4990074 :             sts[n]->mdct_sw = 0;
    2476             : 
    2477     4990074 :             reset_indices_dec( sts[n] );
    2478             :         }
    2479             :     }
    2480             : 
    2481     2507985 :     return sts; /* return last decoder state */
    2482             : }
    2483             : 
    2484             : 
    2485             : /*-------------------------------------------------------------------*
    2486             :  * ivas_set_bitstream_pointers()
    2487             :  *
    2488             :  * Set bitstream pointers for every SCE/CPE Core-Decoder
    2489             :  *-------------------------------------------------------------------*/
    2490             : 
    2491     1242267 : void ivas_set_bitstream_pointers(
    2492             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure    */
    2493             : )
    2494             : {
    2495             :     int16_t k, num_bits;
    2496             :     Decoder_State **sts;
    2497             : 
    2498     1242267 :     num_bits = 0;
    2499             : 
    2500             :     /* set bitstream pointers for SCEs */
    2501     2277375 :     for ( k = 0; k < st_ivas->nSCE; k++ )
    2502             :     {
    2503     1035108 :         sts = st_ivas->hSCE[k]->hCoreCoder;
    2504     1035108 :         sts[0]->bit_stream = st_ivas->bit_stream + num_bits;
    2505     1035108 :         num_bits += (int16_t) ( st_ivas->hSCE[k]->element_brate / FRAMES_PER_SEC );
    2506             :     }
    2507             : 
    2508             :     /* set bitstream pointers for CPEs */
    2509     2478390 :     for ( k = 0; k < st_ivas->nCPE; k++ )
    2510             :     {
    2511     1236123 :         sts = st_ivas->hCPE[k]->hCoreCoder;
    2512     1236123 :         sts[0]->bit_stream = st_ivas->bit_stream + num_bits;
    2513     1236123 :         num_bits += (int16_t) ( st_ivas->hCPE[k]->element_brate / FRAMES_PER_SEC );
    2514             :     }
    2515             : 
    2516     1242267 :     return;
    2517             : }
    2518             : 
    2519             : #ifdef DEBUGGING
    2520             : 
    2521             : /*-------------------------------------------------------------------*
    2522             :  * preview_indices()
    2523             :  *
    2524             :  * Read indices from serial bitstream to the buffer to print out info
    2525             :  * about technologies.
    2526             :  *
    2527             :  * !!The read parmeters are temporary only and not used for decoding!!
    2528             :  *-------------------------------------------------------------------*/
    2529             : 
    2530             : ivas_error preview_indices(
    2531             :     Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure         */
    2532             :     uint16_t bit_stream[],   /* i  : bitstream buffer               */
    2533             :     UWord16 num_bits         /* i  : number of bits in bitstream    */
    2534             : )
    2535             : {
    2536             :     int16_t k, idx;
    2537             :     int32_t total_brate;
    2538             :     ivas_error error;
    2539             : 
    2540             :     error = IVAS_ERR_OK;
    2541             : 
    2542             :     /* convert the frame length to total bitrate */
    2543             :     total_brate = (int32_t) ( num_bits * FRAMES_PER_SEC );
    2544             : 
    2545             :     if ( st_ivas->ivas_format != MONO_FORMAT && is_DTXrate( total_brate ) == 0 )
    2546             :     {
    2547             :         /* read IVAS format */
    2548             :         k = 0;
    2549             :         if ( bit_stream[0] == 1 )
    2550             :         {
    2551             :             k = 1;
    2552             :         }
    2553             :         k <<= 1;
    2554             :         if ( bit_stream[1] == 1 )
    2555             :         {
    2556             :             k += 1;
    2557             :         }
    2558             : 
    2559             :         switch ( k )
    2560             :         {
    2561             :             case 0:
    2562             :                 st_ivas->ivas_format = STEREO_FORMAT;
    2563             :                 break;
    2564             :             case 1:
    2565             :                 st_ivas->ivas_format = MC_FORMAT;
    2566             :                 break;
    2567             :             case 2:
    2568             :                 st_ivas->ivas_format = ISM_FORMAT;
    2569             : 
    2570             :                 if ( total_brate >= IVAS_24k4 )
    2571             :                 {
    2572             :                     if ( bit_stream[2] )
    2573             :                     {
    2574             :                         if ( bit_stream[3] )
    2575             :                         {
    2576             :                             st_ivas->ivas_format = SBA_ISM_FORMAT;
    2577             :                         }
    2578             :                         else
    2579             :                         {
    2580             :                             st_ivas->ivas_format = MASA_ISM_FORMAT;
    2581             :                         }
    2582             :                     }
    2583             :                 }
    2584             :                 break;
    2585             :             case 3:
    2586             :                 if ( bit_stream[2] == 0 )
    2587             :                 {
    2588             :                     st_ivas->ivas_format = SBA_FORMAT;
    2589             :                 }
    2590             :                 else
    2591             :                 {
    2592             :                     st_ivas->ivas_format = MASA_FORMAT;
    2593             :                 }
    2594             :                 break;
    2595             :         }
    2596             :     }
    2597             :     else if ( total_brate == IVAS_SID_5k2 )
    2598             :     {
    2599             :         /* read SID format */
    2600             :         st_ivas->sid_format = 0;
    2601             :         if ( bit_stream[0] == 1 )
    2602             :         {
    2603             :             st_ivas->sid_format += 4;
    2604             :         }
    2605             :         if ( bit_stream[1] == 1 )
    2606             :         {
    2607             :             st_ivas->sid_format += 2;
    2608             :         }
    2609             :         if ( bit_stream[2] == 1 )
    2610             :         {
    2611             :             st_ivas->sid_format += 1;
    2612             :         }
    2613             : 
    2614             :         switch ( st_ivas->sid_format )
    2615             :         {
    2616             :             case SID_DFT_STEREO:
    2617             :                 st_ivas->element_mode_init = IVAS_CPE_DFT;
    2618             :                 st_ivas->ivas_format = STEREO_FORMAT;
    2619             :                 break;
    2620             :             case SID_MDCT_STEREO:
    2621             :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
    2622             :                 st_ivas->ivas_format = STEREO_FORMAT;
    2623             :                 break;
    2624             :             case SID_ISM:
    2625             :                 st_ivas->ivas_format = ISM_FORMAT;
    2626             :                 break;
    2627             :             case SID_SBA_1TC:
    2628             :                 st_ivas->ivas_format = SBA_FORMAT;
    2629             :                 st_ivas->element_mode_init = IVAS_SCE;
    2630             :                 break;
    2631             :             case SID_SBA_2TC:
    2632             :                 st_ivas->ivas_format = SBA_FORMAT;
    2633             :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
    2634             :                 break;
    2635             :             case SID_MASA_1TC:
    2636             :                 st_ivas->ivas_format = MASA_FORMAT;
    2637             :                 st_ivas->element_mode_init = IVAS_SCE;
    2638             :                 break;
    2639             :             case SID_MASA_2TC:
    2640             :                 st_ivas->ivas_format = MASA_FORMAT;
    2641             :                 if ( bit_stream[total_brate / FRAMES_PER_SEC - 1] == 1 )
    2642             :                 {
    2643             :                     st_ivas->element_mode_init = IVAS_CPE_MDCT;
    2644             :                 }
    2645             :                 else
    2646             :                 {
    2647             :                     st_ivas->element_mode_init = IVAS_CPE_DFT;
    2648             :                 }
    2649             :                 break;
    2650             :             default:
    2651             :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Invalid value %c found in SID format field.", st_ivas->sid_format );
    2652             :         }
    2653             :     }
    2654             : 
    2655             :     /* only read element mode from active frames */
    2656             :     if ( is_DTXrate( total_brate ) == 0 )
    2657             :     {
    2658             :         /* read element_mode - needed in init_decoder() */
    2659             :         if ( st_ivas->ivas_format == STEREO_FORMAT || st_ivas->ivas_format == MC_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
    2660             :         {
    2661             :             if ( st_ivas->ivas_format == MASA_FORMAT )
    2662             :             {
    2663             :                 /* read number of MASA transport channels */
    2664             :                 if ( bit_stream[num_bits - 1] == 1 )
    2665             :                 {
    2666             :                     st_ivas->nchan_transport = 2;
    2667             :                 }
    2668             :                 else
    2669             :                 {
    2670             :                     st_ivas->nchan_transport = 1;
    2671             :                 }
    2672             :             }
    2673             : 
    2674             :             if ( st_ivas->ivas_format == MC_FORMAT )
    2675             :             {
    2676             :                 /* read MC configuration */
    2677             :                 idx = 0;
    2678             :                 for ( k = 0; k < MC_LS_SETUP_BITS; k++ )
    2679             :                 {
    2680             :                     if ( bit_stream[IVAS_FORMAT_SIGNALING_NBITS + k] == 1 )
    2681             :                     {
    2682             :                         idx += ( 1 << ( MC_LS_SETUP_BITS - 1 - k ) );
    2683             :                     }
    2684             :                 }
    2685             :                 st_ivas->transport_config = ivas_mc_map_ls_setup_to_output_config( (MC_LS_SETUP) idx );
    2686             :             }
    2687             : 
    2688             :             if ( !( st_ivas->ivas_format == MASA_FORMAT && st_ivas->nchan_transport == 1 ) && st_ivas->ivas_format != MC_FORMAT )
    2689             :             {
    2690             :                 /* read stereo technology info */
    2691             :                 k = IVAS_FORMAT_SIGNALING_NBITS;
    2692             :                 if ( st_ivas->ivas_format == MASA_FORMAT )
    2693             :                 {
    2694             :                     k = IVAS_FORMAT_SIGNALING_NBITS_EXTENDED;
    2695             :                 }
    2696             : 
    2697             :                 if ( total_brate < MIN_BRATE_MDCT_STEREO )
    2698             :                 {
    2699             :                     /* 1 bit */
    2700             :                     if ( bit_stream[k] == 1 )
    2701             :                     {
    2702             :                         st_ivas->element_mode_init = 1 + IVAS_CPE_DFT;
    2703             :                     }
    2704             :                     else
    2705             :                     {
    2706             :                         st_ivas->element_mode_init = 0 + IVAS_CPE_DFT;
    2707             :                     }
    2708             :                 }
    2709             :                 else
    2710             :                 {
    2711             :                     st_ivas->element_mode_init = IVAS_CPE_MDCT;
    2712             :                 }
    2713             :             }
    2714             :         }
    2715             :         else if ( st_ivas->ivas_format == ISM_FORMAT )
    2716             :         {
    2717             :             /* read number of objects from the bitstream */
    2718             :             st_ivas->nchan_transport = 1;
    2719             : 
    2720             :             k = (int16_t) ( ( total_brate / FRAMES_PER_SEC ) - 1 );
    2721             :             while ( bit_stream[k] == 1 && st_ivas->nchan_transport < MAX_NUM_OBJECTS )
    2722             :             {
    2723             :                 st_ivas->nchan_transport++;
    2724             :                 k--;
    2725             :             }
    2726             :             st_ivas->transport_config = IVAS_AUDIO_CONFIG_EXTERNAL + st_ivas->nchan_transport;
    2727             : 
    2728             :             st_ivas->ism_mode = ivas_ism_mode_select( st_ivas->nchan_transport, total_brate );
    2729             :             st_ivas->nSCE = st_ivas->nchan_transport;
    2730             :         }
    2731             :         else if ( st_ivas->ivas_format == SBA_FORMAT )
    2732             :         {
    2733             :             /* Read SBA planar flag and SBA order */
    2734             :             st_ivas->sba_planar = ( bit_stream[IVAS_FORMAT_SIGNALING_NBITS_EXTENDED] == 1 );
    2735             :             st_ivas->sba_order = ( bit_stream[IVAS_FORMAT_SIGNALING_NBITS_EXTENDED + 2] == 1 );
    2736             :             st_ivas->sba_order += 2 * ( bit_stream[IVAS_FORMAT_SIGNALING_NBITS_EXTENDED + 1] == 1 );
    2737             : 
    2738             :             st_ivas->sba_analysis_order = ivas_sba_get_analysis_order( total_brate, st_ivas->sba_order );
    2739             : 
    2740             :             ivas_sba_config( total_brate, st_ivas->sba_analysis_order, -1, &( st_ivas->nchan_transport ), st_ivas->sba_planar, &( st_ivas->nSCE ), &( st_ivas->nCPE ), &( st_ivas->element_mode_init ) );
    2741             :         }
    2742             :         else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
    2743             :         {
    2744             :             /* read number of objects from the bitstream */
    2745             :             if ( total_brate != SID_2k40 && total_brate != FRAME_NO_DATA )
    2746             :             {
    2747             :                 st_ivas->nchan_ism = 2 * bit_stream[total_brate / FRAMES_PER_SEC - 1] + bit_stream[total_brate / FRAMES_PER_SEC - 2] + 1;
    2748             :                 st_ivas->ism_mode = ISM_SBA_MODE_DISC;
    2749             :             }
    2750             : 
    2751             :             /* Read SBA planar flag and SBA order */
    2752             :             st_ivas->sba_planar = ( bit_stream[IVAS_FORMAT_SIGNALING_NBITS_EXTENDED + IVAS_COMBINED_FORMAT_SIGNALLING_BITS] == 1 );
    2753             : 
    2754             :             if ( total_brate >= IVAS_256k )
    2755             :             {
    2756             :                 st_ivas->sba_order = ( bit_stream[IVAS_FORMAT_SIGNALING_NBITS_EXTENDED + IVAS_COMBINED_FORMAT_SIGNALLING_BITS + 2] == 1 );
    2757             :                 st_ivas->sba_order += 2 * ( bit_stream[IVAS_FORMAT_SIGNALING_NBITS_EXTENDED + IVAS_COMBINED_FORMAT_SIGNALLING_BITS + 1] == 1 );
    2758             :             }
    2759             :             else
    2760             :             {
    2761             :                 st_ivas->sba_order = 3;
    2762             :             }
    2763             : 
    2764             :             st_ivas->sba_analysis_order = ivas_sba_get_analysis_order( total_brate, st_ivas->sba_order );
    2765             : 
    2766             :             ivas_sba_config( total_brate, st_ivas->sba_analysis_order, -1, &( st_ivas->nchan_transport ), st_ivas->sba_planar, &( st_ivas->nSCE ), &( st_ivas->nCPE ), &( st_ivas->element_mode_init ) );
    2767             :         }
    2768             :         else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
    2769             :         {
    2770             :             /* read number of objects from the bitstream */
    2771             :             st_ivas->nchan_transport = 2; /* always 2 MASA transport channels */
    2772             :             st_ivas->nchan_ism = 0;
    2773             : 
    2774             :             if ( total_brate != SID_2k40 && total_brate != FRAME_NO_DATA )
    2775             :             {
    2776             :                 st_ivas->nchan_ism = 2 * bit_stream[total_brate / FRAMES_PER_SEC - 1] + bit_stream[total_brate / FRAMES_PER_SEC - 2] + 1;
    2777             :                 st_ivas->ism_mode = ivas_omasa_ism_mode_select( total_brate, st_ivas->nchan_ism );
    2778             :             }
    2779             :         }
    2780             :     }
    2781             : 
    2782             :     st_ivas->hDecoderConfig->ivas_total_brate = total_brate;
    2783             : 
    2784             :     return error;
    2785             : }
    2786             : 
    2787             : #endif
    2788             : 
    2789             : /*-------------------------------------------------------------------*
    2790             :  * read_indices()
    2791             :  *
    2792             :  * Detect SID, NO_DATA, BFI, etc. and set bitstream pointers
    2793             :  * Set ivas_total_brate
    2794             :  * Note: each bit is represented in bitsream buffer as a uint16_t of value 0 or 1
    2795             :  *-------------------------------------------------------------------*/
    2796             : 
    2797             : /*! r: 1 = reading OK, 0 = problem */
    2798     1275018 : ivas_error read_indices(
    2799             :     Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure         */
    2800             :     uint16_t bit_stream[],   /* i  : bitstream buffer               */
    2801             :     UWord16 num_bits,        /* i  : number of bits in bitstream    */
    2802             :     int16_t *prev_ft_speech,
    2803             :     int16_t *CNG,
    2804             :     int16_t bfi /* i  : bad frame indicator            */
    2805             : )
    2806             : {
    2807             :     int16_t k;
    2808             :     Decoder_State **sts;
    2809     1275018 :     int32_t total_brate = 0;
    2810             :     int16_t curr_ft_good_sp, curr_ft_bad_sp;
    2811             :     int16_t g192_sid_first, sid_upd_bad, sid_update;
    2812             :     int16_t speech_bad, speech_lost;
    2813             :     int16_t n;
    2814             :     ivas_error error;
    2815             : 
    2816     1275018 :     error = IVAS_ERR_OK;
    2817             : 
    2818     1275018 :     st_ivas->BER_detect = 0;
    2819     1275018 :     sts = reset_elements( st_ivas );
    2820             : 
    2821             : #ifdef DEBUGGING
    2822             :     file_read_FECpattern( &st_ivas->bfi );
    2823             :     st_ivas->bfi |= bfi;
    2824             : 
    2825             :     if ( bfi == FRAMEMODE_MISSING )
    2826             :     {
    2827             :         for ( k = 0; k < num_bits; k++ )
    2828             :         {
    2829             :             bit_stream[k] = 0;
    2830             :         }
    2831             :     }
    2832             : #else
    2833     1275018 :     st_ivas->bfi = bfi;
    2834             : #endif
    2835             : 
    2836             :     /* convert the frame length to total bitrate */
    2837     1275018 :     total_brate = (int32_t) ( num_bits * FRAMES_PER_SEC );
    2838             : 
    2839             :     /*  verify that a  valid  num bits value  is present */
    2840             :     /*  only AMRWB, EVS or IVAS bitrates or 0(NO DATA) are  allowed */
    2841     1275018 :     if ( st_ivas->ivas_format != MONO_FORMAT )
    2842             :     {
    2843     1265718 :         k = 0;
    2844    10741422 :         while ( k < SIZE_IVAS_BRATE_TBL && total_brate != ivas_brate_tbl[k] )
    2845             :         {
    2846     9475704 :             k++;
    2847             :         }
    2848             : 
    2849     1265718 :         if ( st_ivas->ivas_format == ISM_FORMAT && ( k < SIZE_IVAS_BRATE_TBL || total_brate <= SID_2k40 ) )
    2850             :         {
    2851      311289 :             st_ivas->element_mode_init = IVAS_SCE;
    2852             :         }
    2853      954429 :         else if ( ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT ) && ( total_brate <= SID_2k40 ) )
    2854             :         {
    2855       11817 :             st_ivas->element_mode_init = IVAS_SCE;
    2856             :         }
    2857      942612 :         else if ( k == SIZE_IVAS_BRATE_TBL )
    2858             :         {
    2859           0 :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error, illegal bitrate (%d) detected! Exiting ! \n", total_brate );
    2860             :         }
    2861             :         else
    2862             :         {
    2863      942612 :             st_ivas->element_mode_init = -1;
    2864             :         }
    2865             :     }
    2866             :     else /* AMRWB or EVS */
    2867             :     {
    2868        9300 :         st_ivas->element_mode_init = EVS_MONO;
    2869             : 
    2870        9300 :         if ( rate2EVSmode( total_brate, NULL ) < 0 ) /* negative value means that a valid rate was not found */
    2871             :         {
    2872           0 :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error, illegal bitrate (%d) detected! Exiting ! \n", total_brate );
    2873             :         }
    2874             :     }
    2875             : 
    2876             :     /* RX DTX handler*/
    2877             :     /* handle SID_FIRST, SID_BAD, SPEECH_LOST,  NO_DATA */
    2878             :     /* (total_brate, bfi , st_CNG)   =  rx_handler(received frame type, [previous frame type],  past CNG state, past core) */
    2879     1275018 :     curr_ft_good_sp = 0;
    2880     1275018 :     curr_ft_bad_sp = 0;
    2881             : 
    2882     1275018 :     if ( is_DTXrate( total_brate ) == 0 )
    2883             :     {
    2884     1214403 :         if ( st_ivas->bfi == 0 )
    2885             :         {
    2886     1186335 :             curr_ft_good_sp = 1;
    2887             :         }
    2888             :         else
    2889             :         {
    2890       28068 :             curr_ft_bad_sp = 1;
    2891             :         }
    2892             :     }
    2893             : 
    2894     1275018 :     sid_update = 0;
    2895     1275018 :     sid_upd_bad = 0;
    2896     1275018 :     if ( is_SIDrate( total_brate ) == 1 )
    2897             :     {
    2898        8940 :         if ( st_ivas->bfi == 0 )
    2899             :         {
    2900        8481 :             sid_update = 1;
    2901             :         }
    2902             :         else
    2903             :         {
    2904         459 :             sid_upd_bad = 1; /* this frame type may happen in ETSI/3GPP CS cases, a corrupt SID frames  */
    2905             :         }
    2906             :     }
    2907             : 
    2908             :     /* all zero indices/bits iSP AMRWB SID_update results in a valid LP filter with extremely high LP-filter-gain  */
    2909             :     /* all zero indices/bits  may be a result of CS bit errors  and/or  erroneously injected by gateways or by a bad dejitter handlers */
    2910     1275018 :     if ( total_brate == SID_1k75 && sid_update == 1 )
    2911             :     {
    2912             :         /* valid sid_update received, check for very risky but formally valid content  */
    2913           0 :         int16_t sum = 0;
    2914           0 :         for ( k = 0; k < num_bits; ++k )
    2915             :         {
    2916           0 :             sum += ( bit_stream[k] == 1 ); /*   check of 35 zeroes   */
    2917             :         }
    2918           0 :         if ( sum == 0 )
    2919             :         {                    /* all zeros  */
    2920           0 :             sid_upd_bad = 1; /* initial signal as corrupt (BER likely)  */
    2921             :         }
    2922             :     }
    2923             : 
    2924             :     /* AMRWB  26.173 G.192  file reader (read_serial)  does not declare/use SID_BAD ft,
    2925             :                       it declares every bad synch marked frame initially as  a lost_speech frame,
    2926             :                       and then the RXDTX handler CNG state decides the decoding mode CNG/SPEECH.
    2927             :                       While In the AMRWB ETSI/3GPP format eid a CRC error in a detected  SID_UPDATE frames triggers SID_BAD.
    2928             : 
    2929             :                       Here we inhibit use of the SID-length info, even though it is available in the G.192 file format after STL/EID-XOR .
    2930             :                    */
    2931     1275018 :     if ( sid_upd_bad )
    2932             :     {
    2933         459 :         sid_upd_bad = 0;
    2934         459 :         total_brate = FRAME_NO_DATA; /* treat SID_BAD  as a  stolen signaling frame --> SPEECH LOST */
    2935             :     }
    2936             : 
    2937     1275018 :     g192_sid_first = 0;
    2938     1275018 :     if ( st_ivas->ivas_format == MONO_FORMAT && sts[0]->core == AMR_WB_CORE && *prev_ft_speech && total_brate == FRAME_NO_DATA && st_ivas->bfi == 0 )
    2939             :     {
    2940           0 :         g192_sid_first = 1; /*  SID_FIRST detected for previous AMRWB/AMRWBIO  active frames only  */
    2941             :                             /* It is not possible to perfectly simulate rate switching conditions EVS->AMRWBIO  where:
    2942             :                                            the very first SID_FIRST detection is based on a past EVS active frame
    2943             :                                            and  a  good length 0  "SID_FIRST"(NO_DATA)   frame is sent in AMRWBIO,
    2944             :                                            due to the one frame state memory in the AMRWB legacy  G.192 SID_FIRST encoding
    2945             :                                          */
    2946             :     }
    2947             : 
    2948     1275018 :     speech_bad = 0;
    2949     1275018 :     if ( st_ivas->bfi != 0 && ( is_DTXrate( total_brate ) == 0 ) )
    2950             :     {
    2951       28068 :         speech_bad = 1; /* initial ft assumption, CNG_state decides what to do */
    2952             :     }
    2953             : 
    2954     1275018 :     speech_lost = 0;
    2955     1275018 :     if ( total_brate == FRAME_NO_DATA && st_ivas->bfi != 0 ) /*  unsent  NO_DATA or stolen NO_DATA/signaling  frame  */
    2956             :     {
    2957        8775 :         speech_lost = 1; /* initial ft assumption, CNG_state decides what to do */
    2958             :     }
    2959             : 
    2960             :     /* Do not allow decoder to enter CNG-synthesis for  any instantly  received  GOOD+LENGTH==0  frame
    2961             :                    as this frame was never transmitted, one  can not know it is good and has a a length of zero ) */
    2962     1275018 :     if ( *CNG != 0 )
    2963             :     {
    2964             :         /* We were in CNG synthesis  */
    2965       55704 :         if ( curr_ft_good_sp != 0 )
    2966             :         {
    2967             :             /* only a good speech frame makes you leave CNG synthesis */
    2968        2832 :             *CNG = 0;
    2969             :         }
    2970             :     }
    2971             :     else
    2972             :     {
    2973             :         /* We were in SPEECH synthesis  */
    2974             :         /* only a received/detected SID frame can make the decoder enter into CNG synthsis  */
    2975     1219314 :         if ( g192_sid_first || sid_update || sid_upd_bad )
    2976             :         {
    2977        3060 :             *CNG = 1;
    2978             :         }
    2979             :     }
    2980             : 
    2981             :     /* set bfi, total_brate pair  for proper decoding  */
    2982             :     /*  handle the  G.192   _simulated_ untransmitted NO_DATA frame,  setting  for decoder  SPEECH synthesis  */
    2983     1275018 :     if ( *CNG == 0 && total_brate == FRAME_NO_DATA && st_ivas->bfi == 0 )
    2984             :     {
    2985         498 :         st_ivas->bfi = 1; /*  SPEECH PLC code will now become active as in a real system */
    2986             :                           /* total_brate= 0  */
    2987             :     }
    2988             : 
    2989             :     /* handle bad/lost speech frame(and CS bad SID frame) in the decoders CNG synthesis settings pair (total_brate, bfi) */
    2990     1275018 :     if ( (
    2991     1275018 :              bfi != FRAMEMODE_FUTURE &&
    2992     1275018 :              ( *CNG != 0 ) && ( ( speech_bad != 0 ) || ( speech_lost != 0 ) ) ) || /* SP_BAD or SPEECH_LOST)   --> stay in CNG */
    2993             :          ( sid_upd_bad != 0 ) )                                                    /* SID_UPD_BAD              --> start CNG */
    2994             :     {
    2995        4590 :         st_ivas->bfi = 0; /* bfi=0 needed to activate CNG code */
    2996        4590 :         total_brate = FRAME_NO_DATA;
    2997             :     }
    2998             : 
    2999             :     /* update for next frame's G.192 file format's  odd SID_FIRST detection (primarily for AMRWBIO)  */
    3000     1275018 :     *prev_ft_speech = ( ( curr_ft_good_sp != 0 ) || ( curr_ft_bad_sp != 0 ) );
    3001             : 
    3002             :     /*   st->total brate= total_brate;   updated in a good frame below */
    3003             : 
    3004     2533932 :     for ( k = 0; k < st_ivas->nCPE; k++ )
    3005             :     {
    3006     1258914 :         sts = st_ivas->hCPE[k]->hCoreCoder;
    3007     3776742 :         for ( n = 0; n < CPE_CHANNELS; n++ )
    3008             :         {
    3009     2517828 :             sts[n]->bfi = st_ivas->bfi;
    3010             :         }
    3011             :     }
    3012             : 
    3013     2330202 :     for ( k = 0; k < st_ivas->nSCE; k++ )
    3014             :     {
    3015     1055184 :         sts = st_ivas->hSCE[k]->hCoreCoder;
    3016     1055184 :         sts[0]->bfi = st_ivas->bfi;
    3017             :     }
    3018             : 
    3019     1275018 :     if ( st_ivas->bfi == 0 )
    3020             :     {
    3021             :         /* select Mode 1 or Mode 2 */
    3022     1242267 :         if ( st_ivas->ivas_format == MONO_FORMAT ) /* EVS mono */
    3023             :         {
    3024        9300 :             decoder_selectCodec( sts[0], total_brate, bit_stream[0] );
    3025        9300 :             st_ivas->hDecoderConfig->Opt_AMR_WB = sts[0]->Opt_AMR_WB;
    3026             :         }
    3027             :         else /* IVAS */
    3028             :         {
    3029     1232967 :             st_ivas->codec_mode = MODE1;
    3030     1232967 :             st_ivas->hDecoderConfig->Opt_AMR_WB = 0;
    3031             :         }
    3032             :     }
    3033             : 
    3034             :     /* GOOD frame */
    3035     1275018 :     if ( st_ivas->bfi == 0 || st_ivas->bfi == FRAMEMODE_FUTURE )
    3036             :     {
    3037     1242267 :         st_ivas->hDecoderConfig->ivas_total_brate = total_brate;
    3038             :     }
    3039             : 
    3040     1275018 :     st_ivas->bit_stream = bit_stream;
    3041             : 
    3042     1275018 :     if ( st_ivas->ivas_format == MONO_FORMAT )
    3043             :     {
    3044        9300 :         ivas_set_bitstream_pointers( st_ivas );
    3045             :     }
    3046             : 
    3047     1275018 :     return error;
    3048             : }
    3049             : 
    3050             : 
    3051             : /*-------------------------------------------------------------------*
    3052             :  * get_rfFrameType()
    3053             :  *
    3054             :  * Extract the RF frame type
    3055             :  *-------------------------------------------------------------------*/
    3056             : 
    3057        9300 : static void get_rfFrameType(
    3058             :     Decoder_State *st,     /* i  : decoder state structure */
    3059             :     int16_t *rf_frame_type /* o  : RF frame type           */
    3060             : )
    3061             : {
    3062             :     int16_t num_bits;
    3063        9300 :     num_bits = (int16_t) ( st->total_brate / FRAMES_PER_SEC );
    3064        9300 :     if ( st->rf_flag == 1 )
    3065             :     {
    3066             :         /* the last three bits in a packet is the RF frame type */
    3067           0 :         *rf_frame_type = get_indice( st, num_bits - 3, 3 );
    3068             :     }
    3069             :     else
    3070             :     {
    3071        9300 :         *rf_frame_type = 0;
    3072             :     }
    3073             : 
    3074        9300 :     return;
    3075             : }
    3076             : 
    3077             : /*-------------------------------------------------------------------*
    3078             :  * get_rfFlag()
    3079             :  *
    3080             :  * Check if RF flag is present in the bitstream
    3081             :  *-------------------------------------------------------------------*/
    3082             : 
    3083        9300 : static void get_rfFlag(
    3084             :     Decoder_State *st, /* i  : decoder state structure    */
    3085             :     int16_t *rf_flag,  /* o  : check for the RF flag      */
    3086             :     int16_t *nBits,
    3087             :     int32_t *ind )
    3088             : {
    3089             :     int16_t start_idx, nBits_tmp;
    3090             :     int32_t ind_tmp;
    3091             : 
    3092             :     /* Init */
    3093        9300 :     *rf_flag = 0;
    3094             : 
    3095             :     /* check for rf_flag in the packet and extract the rf_frame_type and rf_fec_offset */
    3096        9300 :     if ( st->total_brate == ACELP_13k20 && ( st->bfi == FRAMEMODE_NORMAL || st->bfi == FRAMEMODE_FUTURE ) )
    3097             :     {
    3098             :         /* find the section in the ACELP signaling table corresponding to bitrate */
    3099        3150 :         start_idx = 0;
    3100      110250 :         while ( acelp_sig_tbl[start_idx] != st->total_brate )
    3101             :         {
    3102      107100 :             start_idx++;
    3103      107100 :             assert( ( start_idx < MAX_ACELP_SIG ) && "ERROR: start_idx larger than acelp_sig_tbl[].\n" );
    3104             :         }
    3105             : 
    3106             :         /* skip the bitrate */
    3107        3150 :         start_idx += 1;
    3108             : 
    3109             :         /* retrieve the number of bits */
    3110        3150 :         nBits_tmp = (int16_t) acelp_sig_tbl[start_idx++];
    3111             : 
    3112             :         /* retrieve the signaling indice */
    3113        3150 :         ind_tmp = acelp_sig_tbl[start_idx + get_indice( st, 0, nBits_tmp )];
    3114             : 
    3115             :         /* convert signaling indice into RF flag. */
    3116        3150 :         *rf_flag = ( ind_tmp >> 7 ) & 0x1;
    3117             : 
    3118        3150 :         if ( ind )
    3119             :         {
    3120        3150 :             *ind = ind_tmp;
    3121             :         }
    3122             : 
    3123        3150 :         if ( nBits )
    3124             :         {
    3125        3150 :             *nBits = nBits_tmp;
    3126             :         }
    3127             :     }
    3128             : 
    3129        9300 :     return;
    3130             : }
    3131             : 
    3132             : /*-------------------------------------------------------------------*
    3133             :  * get_rf_fec_offset()
    3134             :  *
    3135             :  * Extract the FEC offset
    3136             :  *-------------------------------------------------------------------*/
    3137             : 
    3138        9300 : static void get_rf_fec_offset(
    3139             :     Decoder_State *st,     /* i  : decoder state structure       */
    3140             :     int16_t *rf_fec_offset /* o  : RF FEC offset                 */
    3141             : )
    3142             : {
    3143             :     int16_t num_bits, tmp;
    3144        9300 :     num_bits = (int16_t) ( st->total_brate / FRAMES_PER_SEC );
    3145        9300 :     if ( st->rf_flag == 1 )
    3146             :     {
    3147             :         /* the two bits before the RF frame type contains the FEC offset  */
    3148           0 :         tmp = get_indice( st, num_bits - 5, 2 );
    3149             : 
    3150           0 :         if ( tmp == 0 )
    3151             :         {
    3152           0 :             *rf_fec_offset = 2;
    3153             :         }
    3154             :         else
    3155             :         {
    3156           0 :             *rf_fec_offset = 2 * tmp + 1;
    3157             :         }
    3158             :     }
    3159             :     else
    3160             :     {
    3161        9300 :         *rf_fec_offset = 0;
    3162             :     }
    3163             : 
    3164        9300 :     return;
    3165             : }
    3166             : 
    3167             : /*-------------------------------------------------------------------*
    3168             :  * get_rfTargetBits()
    3169             :  *
    3170             :  * Return the number of RF target bits
    3171             :  *-------------------------------------------------------------------*/
    3172             : 
    3173           0 : static void get_rfTargetBits(
    3174             :     int16_t rf_frame_type,  /* i  : RF frame type                 */
    3175             :     int16_t *rf_target_bits /* o  : Number of RF target bits      */
    3176             : )
    3177             : {
    3178             :     /* Number of RF bits for different RF coder types */
    3179             : 
    3180           0 :     switch ( rf_frame_type )
    3181             :     {
    3182           0 :         case RF_NO_DATA:
    3183           0 :             *rf_target_bits = 5;
    3184           0 :             break;
    3185           0 :         case RF_TCXFD:
    3186           0 :             *rf_target_bits = 27;
    3187           0 :             break;
    3188           0 :         case RF_TCXTD1:
    3189           0 :             *rf_target_bits = 16;
    3190           0 :             break;
    3191           0 :         case RF_TCXTD2:
    3192           0 :             *rf_target_bits = 16;
    3193           0 :             break;
    3194           0 :         case RF_ALLPRED:
    3195             :             /* Es_pred bits 3 bits, LTF: 1, pitch: 8,5,5,5, FCB: 0, gain: 7,0,7,0, Diff GFr: 4*/
    3196           0 :             *rf_target_bits = 63;
    3197           0 :             break;
    3198           0 :         case RF_NOPRED:
    3199             :             /* Es_pred bits 3 bits, LTF: 0, pitch: 0, FCB: 7,7,7,7, gain: 6,0,6,0, Diff GFr: 2*/
    3200           0 :             *rf_target_bits = 66;
    3201           0 :             break;
    3202           0 :         case RF_GENPRED:
    3203             :             /* 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*/
    3204           0 :             *rf_target_bits = 70;
    3205           0 :             break;
    3206           0 :         case RF_NELP:
    3207             :             /* gain: 19, Diff GFr: 5 */
    3208           0 :             *rf_target_bits = 45;
    3209           0 :             break;
    3210             :     }
    3211             : 
    3212           0 :     return;
    3213             : }
    3214             : 
    3215             : /*-------------------------------------------------------------------*
    3216             :  * berCheck()
    3217             :  *
    3218             :  * Check for bit errors in channel aware signaling.
    3219             :  *-------------------------------------------------------------------*/
    3220             : 
    3221           0 : static void berCheck(
    3222             :     Decoder_State *st /* i/o: decoder state structure     */
    3223             : )
    3224             : {
    3225             :     /* In case of RF flag = 1, and valid RF packet with primary and partial copy */
    3226           0 :     if ( st->bwidth == NB || st->bwidth == FB || st->coder_type >= TRANSITION )
    3227             :     {
    3228           0 :         if ( st->use_partial_copy == 1 )
    3229             :         {
    3230           0 :             st->use_partial_copy = 0;
    3231             :         }
    3232             : 
    3233           0 :         st->bfi = 1;
    3234           0 :         st->bwidth = st->last_bwidth;
    3235           0 :         st->BER_detect = 1;
    3236           0 :         st->coder_type = GENERIC;
    3237             :     }
    3238             : 
    3239           0 :     return;
    3240             : }
    3241             : 
    3242             : /*-------------------------------------------------------------------*
    3243             :  * getPartialCopyInfo()
    3244             :  *
    3245             :  * Check if the frame includes a partial copy for channel aware processing.
    3246             :  *-------------------------------------------------------------------*/
    3247             : 
    3248        9300 : void getPartialCopyInfo(
    3249             :     Decoder_State *st, /* i/o: decoder state structure       */
    3250             :     int16_t *sharpFlag )
    3251             : {
    3252        9300 :     int16_t nBits = 0;
    3253        9300 :     int32_t ind = 0;
    3254             : 
    3255             :     /* check the RF flag in the packet */
    3256        9300 :     get_rfFlag( st, &( st->rf_flag ), &nBits, &ind );
    3257             : 
    3258             :     /* get RF frame type info */
    3259        9300 :     get_rfFrameType( st, &( st->rf_frame_type ) );
    3260             : 
    3261             :     /* Get the FEC offset info */
    3262        9300 :     get_rf_fec_offset( st, &( st->rf_fec_offset ) );
    3263             : 
    3264             :     /* reset number of target bits in case of rate switching */
    3265        9300 :     st->rf_target_bits = 0;
    3266             : 
    3267             :     /* Get the number of bits used for RF*/
    3268        9300 :     if ( st->rf_flag == 1 )
    3269             :     {
    3270           0 :         st->coder_type = ind & 0x7;
    3271           0 :         st->bwidth = ( ind >> 3 ) & 0x7;
    3272           0 :         *sharpFlag = ( ind >> 6 ) & 0x1;
    3273           0 :         st->codec_mode = MODE2;
    3274           0 :         get_rfTargetBits( st->rf_frame_type, &( st->rf_target_bits ) );
    3275             : 
    3276           0 :         if ( st->bfi == FRAMEMODE_FUTURE )
    3277             :         {
    3278           0 :             st->use_partial_copy = 1;
    3279             :             /* now set the frame mode to normal mode */
    3280           0 :             if ( st->rf_frame_type >= RF_TCXFD && st->rf_frame_type <= RF_TCXTD2 )
    3281             :             {
    3282           0 :                 st->bfi = 1;
    3283           0 :                 st->core = TCX_20_CORE;
    3284             :             }
    3285             :             else
    3286             :             {
    3287           0 :                 st->bfi = FRAMEMODE_NORMAL;
    3288           0 :                 st->core = ACELP_CORE;
    3289             :             }
    3290             :         }
    3291             : 
    3292             :         /* check for bit errors */
    3293           0 :         berCheck( st );
    3294             : 
    3295           0 :         get_next_indice_tmp( st, nBits );
    3296             :     }
    3297             : 
    3298        9300 :     return;
    3299             : }
    3300             : 
    3301             : /*-------------------------------------------------------------------*
    3302             :  * get_NextCoderType()
    3303             :  *
    3304             :  * Extract the coder type of next frame
    3305             :  *-------------------------------------------------------------------*/
    3306             : 
    3307           0 : void get_NextCoderType(
    3308             :     uint8_t *bitstream,      /* i  : bitstream            */
    3309             :     int16_t *next_coder_type /* o  : next coder type      */
    3310             : )
    3311             : {
    3312             :     int16_t k;
    3313             :     int16_t start_idx;
    3314             :     int8_t bit_stream[ACELP_13k20 / FRAMES_PER_SEC];
    3315             :     int32_t tmp;
    3316             :     int16_t nBits_tmp;
    3317             : 
    3318             : 
    3319           0 :     for ( k = 0; k < ACELP_13k20 / FRAMES_PER_SEC; k++ )
    3320             :     {
    3321           0 :         bit_stream[k] = ( bitstream[k / 8] >> ( 7 - ( k % 8 ) ) ) & 0x1;
    3322             :     }
    3323           0 :     start_idx = 0;
    3324           0 :     while ( acelp_sig_tbl[start_idx] != ACELP_13k20 )
    3325             :     {
    3326           0 :         start_idx++;
    3327           0 :         assert( ( start_idx < MAX_ACELP_SIG ) && "ERROR: start_idx larger than acelp_sig_tbl[].\n" );
    3328             :     }
    3329             : 
    3330             :     /* skip the bitrate */
    3331           0 :     start_idx += 1;
    3332             : 
    3333           0 :     tmp = 0;
    3334           0 :     nBits_tmp = (int16_t) acelp_sig_tbl[start_idx++];
    3335           0 :     for ( k = 0; k < nBits_tmp; k++ )
    3336             :     {
    3337           0 :         tmp <<= 1;
    3338           0 :         tmp += bit_stream[k];
    3339             :     }
    3340             : 
    3341             :     /* retrieve the signaling indice */
    3342           0 :     *next_coder_type = acelp_sig_tbl[start_idx + tmp] & 0x7;
    3343             : 
    3344           0 :     return;
    3345             : }
    3346             : 
    3347             : 
    3348             : /*-------------------------------------------------------------------*
    3349             :  * get_indice_preview()
    3350             :  *
    3351             :  * Indices preview to parse for the presence of partial copy
    3352             :  *-------------------------------------------------------------------*/
    3353             : 
    3354           0 : static uint16_t get_indice_preview(
    3355             :     uint8_t *bitstream,
    3356             :     const int16_t bitstreamSize,
    3357             :     const int16_t pos,
    3358             :     const int16_t nb_bits )
    3359             : {
    3360             :     uint16_t value;
    3361             :     int16_t i;
    3362             :     uint16_t bitstreamShort[MAX_BITS_PER_FRAME + 16];
    3363             :     uint16_t *bitstreamShortPtr;
    3364             : 
    3365             :     /* convert bitstream from compact bytes to short values */
    3366           0 :     bitstreamShortPtr = bitstreamShort;
    3367           0 :     for ( i = 0; i < bitstreamSize; i++ )
    3368             :     {
    3369           0 :         *bitstreamShortPtr++ = ( bitstream[i / 8] >> ( 7 - ( i % 8 ) ) ) & 0x1;
    3370             :     }
    3371             : 
    3372           0 :     assert( nb_bits <= 16 );
    3373           0 :     value = 0;
    3374           0 :     for ( i = 0; i < nb_bits; i++ )
    3375             :     {
    3376           0 :         value <<= 1;
    3377           0 :         value += bitstreamShort[pos + i];
    3378             :     }
    3379           0 :     return value;
    3380             : }
    3381             : 
    3382             : 
    3383             : /*-------------------------------------------------------------------*
    3384             :  * evs_dec_previewFrame()
    3385             :  *
    3386             :  * Signalling index preview for JBM
    3387             :  *-------------------------------------------------------------------*/
    3388             : 
    3389           0 : void evs_dec_previewFrame(
    3390             :     uint8_t *bitstream,
    3391             :     int16_t bitstreamSize,
    3392             :     int16_t *partialCopyFrameType,
    3393             :     int16_t *partialCopyOffset )
    3394             : {
    3395             :     int32_t total_brate;
    3396             :     int16_t start_idx, nBits;
    3397             :     int32_t ind;
    3398             :     int16_t rf_flag;
    3399             : 
    3400           0 :     rf_flag = 0;
    3401           0 :     *partialCopyFrameType = 0;
    3402           0 :     *partialCopyOffset = 0;
    3403           0 :     total_brate = bitstreamSize * FRAMES_PER_SEC;
    3404             : 
    3405           0 :     if ( total_brate == ACELP_13k20 )
    3406             :     {
    3407             :         /* find the section in the ACELP signaling table corresponding to bitrate */
    3408           0 :         start_idx = 0;
    3409           0 :         while ( acelp_sig_tbl[start_idx] != total_brate )
    3410             :         {
    3411           0 :             start_idx++;
    3412           0 :             assert( ( start_idx < MAX_ACELP_SIG ) && "ERROR: start_idx larger than acelp_sig_tbl[].\n" );
    3413             :         }
    3414             : 
    3415             :         /* skip the bitrate */
    3416           0 :         start_idx += 1;
    3417             :         /* retrieve the number of bits */
    3418           0 :         nBits = (int16_t) acelp_sig_tbl[start_idx++];
    3419             : 
    3420             :         /* retrieve the signaling indice */
    3421           0 :         ind = acelp_sig_tbl[start_idx + get_indice_preview( bitstream, bitstreamSize, 0, nBits )];
    3422             : 
    3423             :         /* convert signaling indice into RF flag. */
    3424           0 :         rf_flag = ( ind >> 7 ) & 0x1;
    3425           0 :         if ( rf_flag != 0 )
    3426             :         {
    3427             :             /* read the FEC offset at which the partial copy is received */
    3428           0 :             ind = get_indice_preview( bitstream, bitstreamSize, ( bitstreamSize - 5 ), 2 );
    3429           0 :             if ( ind == 0 )
    3430             :             {
    3431           0 :                 *partialCopyOffset = 2;
    3432             :             }
    3433           0 :             else if ( ind == 1 )
    3434             :             {
    3435           0 :                 *partialCopyOffset = 3;
    3436             :             }
    3437           0 :             else if ( ind == 2 )
    3438             :             {
    3439           0 :                 *partialCopyOffset = 5;
    3440             :             }
    3441           0 :             else if ( ind == 3 )
    3442             :             {
    3443           0 :                 *partialCopyOffset = 7;
    3444             :             }
    3445             :             /* the last three bits in a packet is the RF frame type */
    3446           0 :             *partialCopyFrameType = get_indice_preview( bitstream, bitstreamSize, bitstreamSize - 3, 3 );
    3447             :         }
    3448             :     }
    3449             : 
    3450           0 :     return;
    3451             : }
    3452             : 
    3453             : 
    3454        1203 : void dtx_read_padding_bits(
    3455             :     DEC_CORE_HANDLE st,
    3456             :     const int16_t num_bits )
    3457             : {
    3458             :     /* TODO: temporary hack, need to decide what to do with core-coder bitrate */
    3459             :     int32_t tmp;
    3460             : 
    3461        1203 :     tmp = st->total_brate;
    3462        1203 :     st->total_brate = st->total_brate + num_bits * FRAMES_PER_SEC;
    3463        1203 :     get_next_indice( st, num_bits );
    3464        1203 :     st->total_brate = tmp;
    3465             : 
    3466        1203 :     return;
    3467             : }

Generated by: LCOV version 1.14