LCOV - code coverage report
Current view: top level - lib_com - bitstream.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 4c82f1d24d39d0296b18d775f18a006f4c7d024b Lines: 656 826 79.4 %
Date: 2025-05-18 03:18:31 Functions: 24 26 92.3 %

          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             : #include "cnst.h"
      41             : #include "prot_fx.h"
      42             : #include "stat_enc.h"
      43             : #include "stat_dec.h"
      44             : #include "rom_com.h"
      45             : #include "mime.h"
      46             : #include "ivas_cnst.h"
      47             : #include "ivas_rom_com.h"
      48             : #include "wmc_auto.h"
      49             : #include "ivas_prot_fx.h"
      50             : #include "prot_fx_enc.h"
      51             : 
      52             : 
      53             : #define STEP_MAX_NUM_INDICES 100 /* increase the maximum number of allowed indices in the list by this amount */
      54             : 
      55             : 
      56             : /*-------------------------------------------------------------------*
      57             :  * rate2AMRWB_IOmode()
      58             :  *
      59             :  * lookup AMRWB IO mode
      60             :  *-------------------------------------------------------------------*/
      61             : 
      62        3100 : static Word16 rate2AMRWB_IOmode(
      63             :     Word32 brate /* i  : bitrate */
      64             : )
      65             : {
      66        3100 :     switch ( brate )
      67             :     {
      68             :         /* EVS AMR-WB IO modes */
      69           0 :         case SID_1k75:
      70           0 :             return AMRWB_IO_SID;
      71           0 :         case ACELP_6k60:
      72           0 :             return AMRWB_IO_6600;
      73           0 :         case ACELP_8k85:
      74           0 :             return AMRWB_IO_8850;
      75           0 :         case ACELP_12k65:
      76           0 :             return AMRWB_IO_1265;
      77           0 :         case ACELP_14k25:
      78           0 :             return AMRWB_IO_1425;
      79           0 :         case ACELP_15k85:
      80           0 :             return AMRWB_IO_1585;
      81           0 :         case ACELP_18k25:
      82           0 :             return AMRWB_IO_1825;
      83           0 :         case ACELP_19k85:
      84           0 :             return AMRWB_IO_1985;
      85           0 :         case ACELP_23k05:
      86           0 :             return AMRWB_IO_2305;
      87           0 :         case ACELP_23k85:
      88           0 :             return AMRWB_IO_2385;
      89        3100 :         default:
      90        3100 :             break;
      91             :     }
      92             : 
      93        3100 :     return -1;
      94             : }
      95             : 
      96             : /*-------------------------------------------------------------------*
      97             :  * rate2EVSmode()
      98             :  *
      99             :  * lookup EVS mode
     100             :  *-------------------------------------------------------------------*/
     101        3100 : Word16 rate2EVSmode_float(
     102             :     const Word32 brate, /* i  : bitrate                                               */
     103             :     int16_t *is_amr_wb  /* o  : (flag) does the bitrate belong to AMR-WB? Can be NULL */
     104             : )
     105             : {
     106        3100 :     if ( is_amr_wb != NULL )
     107             :     {
     108           0 :         *is_amr_wb = 0;
     109             :     }
     110             : 
     111        3100 :     switch ( brate )
     112             :     {
     113             :         /* EVS Primary modes */
     114           0 :         case FRAME_NO_DATA:
     115           0 :             return NO_DATA_TYPE;
     116           0 :         case SID_2k40:
     117           0 :             return PRIMARY_SID;
     118           0 :         case PPP_NELP_2k80:
     119           0 :             return PRIMARY_2800;
     120           0 :         case ACELP_7k20:
     121           0 :             return PRIMARY_7200;
     122           0 :         case ACELP_8k00:
     123           0 :             return PRIMARY_8000;
     124           0 :         case ACELP_9k60:
     125           0 :             return PRIMARY_9600;
     126        1050 :         case ACELP_13k20:
     127        1050 :             return PRIMARY_13200;
     128           0 :         case ACELP_16k40:
     129           0 :             return PRIMARY_16400;
     130        1050 :         case ACELP_24k40:
     131        1050 :             return PRIMARY_24400;
     132           0 :         case ACELP_32k:
     133           0 :             return PRIMARY_32000;
     134           0 :         case ACELP_48k:
     135           0 :             return PRIMARY_48000;
     136        1000 :         case ACELP_64k:
     137        1000 :             return PRIMARY_64000;
     138           0 :         case HQ_96k:
     139           0 :             return PRIMARY_96000;
     140           0 :         case HQ_128k:
     141           0 :             return PRIMARY_128000;
     142           0 :         default:
     143           0 :             break;
     144             :     }
     145             : 
     146           0 :     if ( is_amr_wb != NULL )
     147             :     {
     148           0 :         *is_amr_wb = 1;
     149             :     }
     150             : 
     151           0 :     return rate2AMRWB_IOmode( brate );
     152             : }
     153             : 
     154             : /*-------------------------------------------------------------------*
     155             :  * ind_list_realloc()
     156             :  *
     157             :  * Re-allocate the list of indices
     158             :  *-------------------------------------------------------------------*/
     159             : 
     160           0 : ivas_error ind_list_realloc(
     161             :     INDICE_HANDLE old_ind_list,   /* i  : pointer to the beginning of the old buffer of indices */
     162             :     const Word16 max_num_indices, /* i  : new maximum number of allowed indices in the list */
     163             :     Encoder_Struct *st_ivas       /* i  : IVAS encoder structure                  */
     164             : )
     165             : {
     166             :     Word16 i, n, ch, n_channels, ind_list_pos, is_metadata, ivas_max_num_indices;
     167             :     INDICE_HANDLE new_ind_list;
     168             :     BSTR_ENC_HANDLE hBstr;
     169             : 
     170           0 :     IF( st_ivas == NULL )
     171             :     {
     172           0 :         return IVAS_ERR_OK;
     173             :     }
     174             : 
     175             :     /* get the pointer to the beginning of the old buffer of indices (either metadata or core coders) */
     176           0 :     IF( old_ind_list == st_ivas->ind_list_metadata )
     177             :     {
     178           0 :         is_metadata = 1;
     179           0 :         ivas_max_num_indices = st_ivas->ivas_max_num_indices_metadata;
     180             :     }
     181             :     ELSE
     182             :     {
     183           0 :         is_metadata = 0;
     184           0 :         ivas_max_num_indices = st_ivas->ivas_max_num_indices;
     185             :     }
     186           0 :     move16();
     187           0 :     move16();
     188             : 
     189             :     /* allocate new buffer of indices */
     190           0 :     IF( ( new_ind_list = (INDICE_HANDLE) malloc( max_num_indices * sizeof( Indice ) ) ) == NULL )
     191             :     {
     192           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for buffer of indices!\n" ) );
     193             :     }
     194             : 
     195             :     /* move indices from the old list to the new list */
     196           0 :     FOR( i = 0; i < s_min( max_num_indices, ivas_max_num_indices ); i++ )
     197             :     {
     198           0 :         IF( GT_16( old_ind_list[i].nb_bits, -1 ) )
     199             :         {
     200           0 :             new_ind_list[i].id = old_ind_list[i].id;
     201           0 :             new_ind_list[i].value = old_ind_list[i].value;
     202           0 :             move16();
     203           0 :             move16();
     204             :         }
     205           0 :         new_ind_list[i].nb_bits = old_ind_list[i].nb_bits;
     206           0 :         move16();
     207             :     }
     208             : 
     209             :     /* reset nb_bits of all other indices to -1 */
     210           0 :     FOR( ; i < max_num_indices; i++ )
     211             :     {
     212           0 :         new_ind_list[i].nb_bits = -1;
     213           0 :         move16();
     214             :     }
     215             : 
     216             :     /* update parameters in all SCE elements */
     217           0 :     FOR( n = 0; n < st_ivas->nSCE; n++ )
     218             :     {
     219             :         /* get the pointer to hBstr */
     220           0 :         IF( is_metadata )
     221             :         {
     222           0 :             hBstr = st_ivas->hSCE[n]->hMetaData;
     223             :         }
     224             :         ELSE
     225             :         {
     226           0 :             hBstr = st_ivas->hSCE[n]->hCoreCoder[0]->hBstr;
     227             :         }
     228             : 
     229           0 :         IF( hBstr != NULL )
     230             :         {
     231             :             /* get the current position inside the old list */
     232           0 :             ind_list_pos = (Word16) ( hBstr->ind_list - old_ind_list );
     233             : 
     234             :             /* set pointers in the new list */
     235           0 :             *( hBstr->ivas_ind_list_zero ) = new_ind_list;
     236           0 :             hBstr->ind_list = &new_ind_list[ind_list_pos];
     237             : 
     238             :             /* set the new maximum number of indices */
     239           0 :             *( hBstr->ivas_max_num_indices ) = max_num_indices;
     240           0 :             move16();
     241             :         }
     242             :     }
     243             : 
     244             :     /* update parameters in all CPE elements */
     245           0 :     FOR( n = 0; n < st_ivas->nCPE; n++ )
     246             :     {
     247             :         /* get the pointer to hBstr */
     248           0 :         IF( is_metadata )
     249             :         {
     250           0 :             n_channels = 1;
     251             :         }
     252             :         ELSE
     253             :         {
     254           0 :             n_channels = CPE_CHANNELS;
     255             :         }
     256           0 :         move16();
     257             : 
     258           0 :         FOR( ch = 0; ch < n_channels; ch++ )
     259             :         {
     260           0 :             IF( is_metadata )
     261             :             {
     262           0 :                 hBstr = st_ivas->hCPE[n]->hMetaData;
     263             :             }
     264             :             ELSE
     265             :             {
     266           0 :                 hBstr = st_ivas->hCPE[n]->hCoreCoder[ch]->hBstr;
     267             :             }
     268             : 
     269           0 :             IF( hBstr != NULL )
     270             :             {
     271             :                 /* get the current position inside the old list */
     272           0 :                 ind_list_pos = (Word16) ( hBstr->ind_list - old_ind_list );
     273             : 
     274             :                 /* set pointers in the new list */
     275           0 :                 *( hBstr->ivas_ind_list_zero ) = new_ind_list;
     276           0 :                 hBstr->ind_list = &new_ind_list[ind_list_pos];
     277             : 
     278             :                 /* set the new maximum number of indices */
     279           0 :                 *( hBstr->ivas_max_num_indices ) = max_num_indices;
     280           0 :                 move16();
     281             :             }
     282             :         }
     283             :     }
     284             : 
     285             :     /* free the old list */
     286           0 :     free( old_ind_list );
     287             : 
     288           0 :     return IVAS_ERR_OK;
     289             : }
     290             : 
     291             : 
     292             : /*-----------------------------------------------------------------------*
     293             :  * get_ivas_max_num_indices()
     294             :  *
     295             :  * Get the maximum allowed number of indices in the encoder
     296             :  *-----------------------------------------------------------------------*/
     297             : 
     298             : /*! r: maximum number of indices */
     299        7348 : Word16 get_ivas_max_num_indices_fx(
     300             :     const IVAS_FORMAT ivas_format, /* i  : IVAS format               */
     301             :     const Word32 ivas_total_brate  /* i  : IVAS total bitrate        */
     302             : )
     303             : {
     304        7348 :     test();
     305        7348 :     test();
     306        7348 :     IF( EQ_16( ivas_format, STEREO_FORMAT ) )
     307             :     {
     308         370 :         IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
     309             :         {
     310          88 :             return 300;
     311             :         }
     312         282 :         ELSE IF( LE_32( ivas_total_brate, IVAS_24k4 ) )
     313             :         {
     314          43 :             return 400;
     315             :         }
     316         239 :         ELSE IF( LE_32( ivas_total_brate, IVAS_32k ) )
     317             :         {
     318          54 :             return 450;
     319             :         }
     320         185 :         ELSE IF( LE_32( ivas_total_brate, IVAS_48k ) )
     321             :         {
     322          38 :             return 650;
     323             :         }
     324         147 :         ELSE IF( LE_32( ivas_total_brate, IVAS_80k ) )
     325             :         {
     326          73 :             return 750;
     327             :         }
     328          74 :         ELSE IF( LE_32( ivas_total_brate, IVAS_128k ) )
     329             :         {
     330          74 :             return 850;
     331             :         }
     332           0 :         ELSE IF( LE_32( ivas_total_brate, IVAS_192k ) )
     333             :         {
     334           0 :             return 950;
     335             :         }
     336           0 :         ELSE IF( LE_32( ivas_total_brate, IVAS_256k ) )
     337             :         {
     338           0 :             return 1350;
     339             :         }
     340             :         ELSE
     341             :         {
     342           0 :             return 1650;
     343             :         }
     344             :     }
     345        6978 :     ELSE IF( EQ_16( ivas_format, ISM_FORMAT ) || EQ_16( ivas_format, MONO_FORMAT ) )
     346             :     {
     347        1024 :         IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
     348             :         {
     349          30 :             return 250;
     350             :         }
     351         994 :         ELSE IF( LE_32( ivas_total_brate, IVAS_24k4 ) )
     352             :         {
     353          63 :             return 350;
     354             :         }
     355         931 :         ELSE IF( LE_32( ivas_total_brate, IVAS_32k ) )
     356             :         {
     357         294 :             return 450;
     358             :         }
     359         637 :         ELSE IF( LE_32( ivas_total_brate, IVAS_48k ) )
     360             :         {
     361         267 :             return 550;
     362             :         }
     363         370 :         ELSE IF( LE_32( ivas_total_brate, IVAS_64k ) )
     364             :         {
     365          59 :             return 620;
     366             :         }
     367         311 :         ELSE IF( LE_32( ivas_total_brate, IVAS_80k ) )
     368             :         {
     369          68 :             return 670;
     370             :         }
     371         243 :         ELSE IF( LE_32( ivas_total_brate, IVAS_96k ) )
     372             :         {
     373          59 :             return 780;
     374             :         }
     375         184 :         ELSE IF( LE_32( ivas_total_brate, IVAS_128k ) )
     376             :         {
     377          53 :             return 880;
     378             :         }
     379         131 :         ELSE IF( LE_32( ivas_total_brate, IVAS_192k ) )
     380             :         {
     381          80 :             return 950;
     382             :         }
     383          51 :         ELSE IF( LE_32( ivas_total_brate, IVAS_256k ) )
     384             :         {
     385          48 :             return 1100;
     386             :         }
     387           3 :         ELSE IF( LE_32( ivas_total_brate, IVAS_384k ) )
     388             :         {
     389           2 :             return 1300;
     390             :         }
     391             :         ELSE
     392             :         {
     393           1 :             return 1650;
     394             :         }
     395             :     }
     396        5954 :     ELSE IF( EQ_16( ivas_format, SBA_FORMAT ) || EQ_16( ivas_format, SBA_ISM_FORMAT ) )
     397             :     {
     398        1861 :         IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
     399             :         {
     400         374 :             return 250;
     401             :         }
     402        1487 :         ELSE IF( LE_32( ivas_total_brate, IVAS_24k4 ) )
     403             :         {
     404         141 :             return 350;
     405             :         }
     406        1346 :         ELSE IF( LE_32( ivas_total_brate, IVAS_32k ) )
     407             :         {
     408         162 :             return 400;
     409             :         }
     410        1184 :         ELSE IF( LE_32( ivas_total_brate, IVAS_48k ) )
     411             :         {
     412          81 :             return 650;
     413             :         }
     414        1103 :         ELSE IF( LE_32( ivas_total_brate, IVAS_80k ) )
     415             :         {
     416         287 :             return 750;
     417             :         }
     418         816 :         ELSE IF( LE_32( ivas_total_brate, IVAS_128k ) )
     419             :         {
     420         248 :             return 1020;
     421             :         }
     422         568 :         ELSE IF( LE_32( ivas_total_brate, IVAS_160k ) )
     423             :         {
     424          81 :             return 1160;
     425             :         }
     426         487 :         ELSE IF( LE_32( ivas_total_brate, IVAS_192k ) )
     427             :         {
     428         151 :             return 1220;
     429             :         }
     430         336 :         ELSE IF( LE_32( ivas_total_brate, IVAS_256k ) )
     431             :         {
     432         106 :             return 1300;
     433             :         }
     434         230 :         ELSE IF( LE_32( ivas_total_brate, IVAS_384k ) )
     435             :         {
     436         112 :             return 1720;
     437             :         }
     438             :         ELSE
     439             :         {
     440         118 :             return 2000;
     441             :         }
     442             :     }
     443        4093 :     ELSE IF( EQ_16( ivas_format, MASA_FORMAT ) )
     444             :     {
     445        1344 :         IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
     446             :         {
     447         311 :             return 300;
     448             :         }
     449        1033 :         ELSE IF( LE_32( ivas_total_brate, IVAS_32k ) )
     450             :         {
     451         241 :             return 400;
     452             :         }
     453         792 :         ELSE IF( LE_32( ivas_total_brate, IVAS_48k ) )
     454             :         {
     455          82 :             return 650;
     456             :         }
     457         710 :         ELSE IF( LE_32( ivas_total_brate, IVAS_80k ) )
     458             :         {
     459         254 :             return 750;
     460             :         }
     461         456 :         ELSE IF( LE_32( ivas_total_brate, IVAS_160k ) )
     462             :         {
     463         260 :             return 850;
     464             :         }
     465         196 :         ELSE IF( LE_32( ivas_total_brate, IVAS_192k ) )
     466             :         {
     467          60 :             return 950;
     468             :         }
     469         136 :         ELSE IF( LE_32( ivas_total_brate, IVAS_256k ) )
     470             :         {
     471          31 :             return 1150;
     472             :         }
     473         105 :         ELSE IF( LE_32( ivas_total_brate, IVAS_384k ) )
     474             :         {
     475          53 :             return 1450;
     476             :         }
     477             :         ELSE
     478             :         {
     479          52 :             return 1650;
     480             :         }
     481             :     }
     482        2749 :     ELSE IF( EQ_16( ivas_format, MASA_ISM_FORMAT ) )
     483             :     {
     484        1678 :         IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
     485             :         {
     486         319 :             return 300;
     487             :         }
     488        1359 :         ELSE IF( LE_32( ivas_total_brate, IVAS_32k ) )
     489             :         {
     490         375 :             return 400;
     491             :         }
     492         984 :         ELSE IF( LE_32( ivas_total_brate, IVAS_48k ) )
     493             :         {
     494         186 :             return 650;
     495             :         }
     496         798 :         ELSE IF( LE_32( ivas_total_brate, IVAS_80k ) )
     497             :         {
     498         327 :             return 750;
     499             :         }
     500         471 :         ELSE IF( LE_32( ivas_total_brate, IVAS_160k ) )
     501             :         {
     502         229 :             return 1150;
     503             :         }
     504         242 :         ELSE IF( LE_32( ivas_total_brate, IVAS_192k ) )
     505             :         {
     506          65 :             return 1250;
     507             :         }
     508         177 :         ELSE IF( LE_32( ivas_total_brate, IVAS_256k ) )
     509             :         {
     510          67 :             return 1400;
     511             :         }
     512         110 :         ELSE IF( LE_32( ivas_total_brate, IVAS_384k ) )
     513             :         {
     514          59 :             return 1650;
     515             :         }
     516             :         ELSE
     517             :         {
     518          51 :             return 1850;
     519             :         }
     520             :     }
     521        1071 :     ELSE IF( EQ_16( ivas_format, MC_FORMAT ) )
     522             :     {
     523        1071 :         IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
     524             :         {
     525         130 :             return 250;
     526             :         }
     527         941 :         ELSE IF( LE_32( ivas_total_brate, IVAS_24k4 ) )
     528             :         {
     529          57 :             return 350;
     530             :         }
     531         884 :         ELSE IF( LE_32( ivas_total_brate, IVAS_32k ) )
     532             :         {
     533          39 :             return 400;
     534             :         }
     535         845 :         ELSE IF( LE_32( ivas_total_brate, IVAS_48k ) )
     536             :         {
     537         189 :             return 650;
     538             :         }
     539         656 :         ELSE IF( LE_32( ivas_total_brate, IVAS_64k ) )
     540             :         {
     541          95 :             return 750;
     542             :         }
     543         561 :         ELSE IF( LE_32( ivas_total_brate, IVAS_80k ) )
     544             :         {
     545          64 :             return 850;
     546             :         }
     547         497 :         ELSE IF( LE_32( ivas_total_brate, IVAS_128k ) )
     548             :         {
     549         157 :             return 1150;
     550             :         }
     551         340 :         ELSE IF( LE_32( ivas_total_brate, IVAS_160k ) )
     552             :         {
     553          95 :             return 1420;
     554             :         }
     555         245 :         ELSE IF( LE_32( ivas_total_brate, IVAS_256k ) )
     556             :         {
     557         142 :             return 2120;
     558             :         }
     559         103 :         ELSE IF( LE_32( ivas_total_brate, IVAS_384k ) )
     560             :         {
     561          48 :             return 2250;
     562             :         }
     563             :         ELSE
     564             :         {
     565          55 :             return 2450;
     566             :         }
     567             :     }
     568             : 
     569           0 :     return 2450;
     570             : }
     571             : /*-----------------------------------------------------------------------*
     572             :  * get_BWE_max_num_indices()
     573             :  *
     574             :  * Get the maximum number of indices in the BWE
     575             :  *-----------------------------------------------------------------------*/
     576             : 
     577             : /*! r: maximum number of indices */
     578        3837 : int16_t get_BWE_max_num_indices(
     579             :     const int32_t extl_brate /* i  : extensiona layer bitrate  */
     580             : )
     581             : {
     582             :     /* set the maximum number of indices in the BWE */
     583        3837 :     if ( extl_brate < SWB_BWE_16k )
     584             :     {
     585        3837 :         return 30;
     586             :     }
     587             :     else
     588             :     {
     589           0 :         return 150;
     590             :     }
     591             : }
     592             : 
     593             : 
     594             : /*-----------------------------------------------------------------------*
     595             :  * get_ivas_max_num_indices_metadata()
     596             :  *
     597             :  * Set the maximum allowed number of metadata indices in the list
     598             :  *-----------------------------------------------------------------------*/
     599             : 
     600             : /*! r: maximum number of indices */
     601        7348 : Word16 get_ivas_max_num_indices_metadata_fx(
     602             :     const IVAS_FORMAT ivas_format, /* i  : IVAS format              */
     603             :     const Word32 ivas_total_brate  /* i  : IVAS total bitrate       */
     604             : )
     605             : {
     606             :     /* set the maximum required number of metadata indices */
     607        7348 :     test();
     608        7348 :     IF( EQ_16( ivas_format, MONO_FORMAT ) )
     609             :     {
     610           3 :         return 0;
     611             :     }
     612        7345 :     ELSE IF( EQ_16( ivas_format, STEREO_FORMAT ) )
     613             :     {
     614         370 :         IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
     615             :         {
     616          88 :             return 60;
     617             :         }
     618             :         ELSE
     619             :         {
     620         282 :             return 80;
     621             :         }
     622             :     }
     623        6975 :     ELSE IF( EQ_16( ivas_format, ISM_FORMAT ) )
     624             :     {
     625        1021 :         IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
     626             :         {
     627          29 :             return 20;
     628             :         }
     629         992 :         ELSE IF( LE_32( ivas_total_brate, IVAS_32k ) )
     630             :         {
     631         356 :             return 65;
     632             :         }
     633             :         ELSE
     634             :         {
     635         636 :             return 80;
     636             :         }
     637             :     }
     638        5954 :     ELSE IF( EQ_16( ivas_format, SBA_FORMAT ) || EQ_16( ivas_format, SBA_ISM_FORMAT ) )
     639             :     {
     640        1861 :         IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
     641             :         {
     642         374 :             return 100;
     643             :         }
     644        1487 :         ELSE IF( LE_32( ivas_total_brate, IVAS_24k4 ) )
     645             :         {
     646         141 :             return 200;
     647             :         }
     648        1346 :         ELSE IF( LE_32( ivas_total_brate, IVAS_32k ) )
     649             :         {
     650         162 :             return 300;
     651             :         }
     652        1184 :         ELSE IF( LE_32( ivas_total_brate, IVAS_192k ) )
     653             :         {
     654         848 :             return 500;
     655             :         }
     656         336 :         ELSE IF( LE_32( ivas_total_brate, IVAS_256k ) )
     657             :         {
     658         106 :             return 1050;
     659             :         }
     660         230 :         ELSE IF( LE_32( ivas_total_brate, IVAS_384k ) )
     661             :         {
     662         112 :             return 2000;
     663             :         }
     664             :         ELSE
     665             :         {
     666         118 :             return 2500;
     667             :         }
     668             :     }
     669        4093 :     ELSE IF( EQ_16( ivas_format, MASA_FORMAT ) )
     670             :     {
     671        1344 :         IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
     672             :         {
     673         311 :             return 80;
     674             :         }
     675        1033 :         ELSE IF( LE_32( ivas_total_brate, IVAS_32k ) )
     676             :         {
     677         241 :             return 125;
     678             :         }
     679         792 :         ELSE IF( LE_32( ivas_total_brate, IVAS_48k ) )
     680             :         {
     681          82 :             return 205;
     682             :         }
     683         710 :         ELSE IF( LE_32( ivas_total_brate, IVAS_96k ) )
     684             :         {
     685         375 :             return 240;
     686             :         }
     687         335 :         ELSE IF( LE_32( ivas_total_brate, IVAS_128k ) )
     688             :         {
     689         113 :             return 305;
     690             :         }
     691         222 :         ELSE IF( LE_32( ivas_total_brate, IVAS_160k ) )
     692             :         {
     693          26 :             return 425;
     694             :         }
     695         196 :         ELSE IF( LE_32( ivas_total_brate, IVAS_192k ) )
     696             :         {
     697          60 :             return 630;
     698             :         }
     699         136 :         ELSE IF( LE_32( ivas_total_brate, IVAS_256k ) )
     700             :         {
     701          31 :             return 850;
     702             :         }
     703         105 :         ELSE IF( LE_32( ivas_total_brate, IVAS_384k ) )
     704             :         {
     705          53 :             return 1000;
     706             :         }
     707             :         ELSE
     708             :         {
     709          52 :             return 1750;
     710             :         }
     711             :     }
     712        2749 :     ELSE IF( EQ_16( ivas_format, MASA_ISM_FORMAT ) )
     713             :     {
     714        1678 :         IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
     715             :         {
     716         319 :             return 80;
     717             :         }
     718        1359 :         ELSE IF( LE_32( ivas_total_brate, IVAS_32k ) )
     719             :         {
     720         375 :             return 125 + 100;
     721             :         }
     722         984 :         ELSE IF( LE_32( ivas_total_brate, IVAS_48k ) )
     723             :         {
     724         186 :             return 205 + 100;
     725             :         }
     726         798 :         ELSE IF( LE_32( ivas_total_brate, IVAS_96k ) )
     727             :         {
     728         420 :             return 240 + 150;
     729             :         }
     730         378 :         ELSE IF( LE_32( ivas_total_brate, IVAS_128k ) )
     731             :         {
     732          71 :             return 305 + 30;
     733             :         }
     734         307 :         ELSE IF( LE_32( ivas_total_brate, IVAS_160k ) )
     735             :         {
     736          65 :             return 425 + 30;
     737             :         }
     738         242 :         ELSE IF( LE_32( ivas_total_brate, IVAS_192k ) )
     739             :         {
     740          65 :             return 630 + 30;
     741             :         }
     742         177 :         ELSE IF( LE_32( ivas_total_brate, IVAS_256k ) )
     743             :         {
     744          67 :             return 850 + 30;
     745             :         }
     746         110 :         ELSE IF( LE_32( ivas_total_brate, IVAS_384k ) )
     747             :         {
     748          59 :             return 1000 + 30;
     749             :         }
     750             :         ELSE
     751             :         {
     752          51 :             return 1750 + 30;
     753             :         }
     754             :     }
     755        1071 :     ELSE IF( EQ_16( ivas_format, MC_FORMAT ) )
     756             :     {
     757        1071 :         IF( LE_32( ivas_total_brate, IVAS_13k2 ) )
     758             :         {
     759          67 :             return 80;
     760             :         }
     761        1004 :         ELSE IF( LE_32( ivas_total_brate, IVAS_24k4 ) )
     762             :         {
     763         120 :             return 100;
     764             :         }
     765         884 :         ELSE IF( LE_32( ivas_total_brate, IVAS_64k ) )
     766             :         {
     767         323 :             return 210;
     768             :         }
     769         561 :         ELSE IF( LE_32( ivas_total_brate, IVAS_96k ) )
     770             :         {
     771         120 :             return 220;
     772             :         }
     773             :         ELSE
     774             :         {
     775         441 :             return 300;
     776             :         }
     777             :     }
     778             : 
     779           0 :     return 50;
     780             : }
     781             : /*-------------------------------------------------------------------*
     782             :  * move_indices()
     783             :  *
     784             :  * Move indices inside the buffer or among two buffers
     785             :  *-------------------------------------------------------------------*/
     786             : 
     787        3837 : void move_indices(
     788             :     INDICE_HANDLE old_ind_list, /* i/o: old location of indices */
     789             :     INDICE_HANDLE new_ind_list, /* i/o: new location of indices */
     790             :     const int16_t nb_indices    /* i  : number of moved indices */
     791             : )
     792             : {
     793             :     int16_t i;
     794             : 
     795        3837 :     if ( new_ind_list < old_ind_list )
     796             :     {
     797       15645 :         for ( i = 0; i < nb_indices; i++ )
     798             :         {
     799       11838 :             new_ind_list[i].id = old_ind_list[i].id;
     800       11838 :             new_ind_list[i].value = old_ind_list[i].value;
     801       11838 :             new_ind_list[i].nb_bits = old_ind_list[i].nb_bits;
     802             : 
     803       11838 :             old_ind_list[i].nb_bits = -1;
     804             :         }
     805             :     }
     806          30 :     else if ( new_ind_list > old_ind_list )
     807             :     {
     808         120 :         for ( i = nb_indices - 1; i >= 0; i-- )
     809             :         {
     810          90 :             new_ind_list[i].id = old_ind_list[i].id;
     811          90 :             new_ind_list[i].value = old_ind_list[i].value;
     812          90 :             new_ind_list[i].nb_bits = old_ind_list[i].nb_bits;
     813             : 
     814          90 :             old_ind_list[i].nb_bits = -1;
     815             :         }
     816             :     }
     817             : 
     818        3837 :     return;
     819             : }
     820             : 
     821             : 
     822             : /*-------------------------------------------------------------------*
     823             :  * check_ind_list_limits()
     824             :  *
     825             :  * Check, if the maximum number of indices has been reached -> reallocate
     826             :  * Check, if we will not overwrite an existing indice -> adjust the location
     827             :  *-------------------------------------------------------------------*/
     828             : 
     829   246674954 : ivas_error check_ind_list_limits(
     830             :     BSTR_ENC_HANDLE hBstr /* i/o: encoder bitstream handle                    */
     831             : )
     832             : {
     833             :     Indice *ivas_ind_list_zero, *ivas_ind_list_last;
     834             :     ivas_error error;
     835             : 
     836   246674954 :     error = IVAS_ERR_OK;
     837   246674954 :     move32();
     838   246674954 :     ivas_ind_list_zero = *( hBstr->ivas_ind_list_zero );
     839             : 
     840             :     /* check, if the maximum number of indices has been reached and re-allocate the buffer */
     841             :     /* the re-allocation can be avoided by increasing the limits in get_ivas_max_num_indices() or get_ivas_max_num_indices_metadata() */
     842   246674954 :     IF( GE_16( (Word16) ( &hBstr->ind_list[hBstr->nb_ind_tot] - ivas_ind_list_zero ), *( hBstr->ivas_max_num_indices ) ) )
     843             :     {
     844             : 
     845             :         /* reallocate the buffer of indices with increased limit */
     846           0 :         IF( NE_32( ( error = ind_list_realloc( *hBstr->ivas_ind_list_zero, *( hBstr->ivas_max_num_indices ) + STEP_MAX_NUM_INDICES, hBstr->st_ivas ) ), IVAS_ERR_OK ) )
     847             :         {
     848           0 :             return error;
     849             :         }
     850             :     }
     851             : 
     852             :     /* check, if we will not overwrite an existing indice */
     853   246674954 :     IF( hBstr->ind_list[hBstr->nb_ind_tot].nb_bits > 0 )
     854             :     {
     855           0 :         IF( hBstr->nb_ind_tot == 0 )
     856             :         {
     857             :             /* move the pointer to the next available empty slot */
     858           0 :             ivas_ind_list_last = &ivas_ind_list_zero[*( hBstr->ivas_max_num_indices )];
     859           0 :             WHILE( hBstr->ind_list[0].nb_bits > 0 && hBstr->ind_list < ivas_ind_list_last )
     860             :             {
     861           0 :                 hBstr->ind_list++;
     862             :             }
     863             : 
     864           0 :             IF( hBstr->ind_list >= ivas_ind_list_last )
     865             :             {
     866             : 
     867             :                 /* no available empty slot -> need to re-allocate the buffer */
     868           0 :                 IF( NE_32( ( error = ind_list_realloc( *hBstr->ivas_ind_list_zero, *( hBstr->ivas_max_num_indices ) + STEP_MAX_NUM_INDICES, hBstr->st_ivas ) ), IVAS_ERR_OK ) )
     869             :                 {
     870           0 :                     return error;
     871             :                 }
     872             :             }
     873             :         }
     874             :         ELSE
     875             :         {
     876           0 :             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", 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 );
     877             :         }
     878             :     }
     879             : 
     880   246674954 :     return error;
     881             : }
     882             : 
     883             : 
     884             : /*-------------------------------------------------------------------*
     885             :  * push_indice()
     886             :  *
     887             :  * Push a new indice into the buffer
     888             :  *-------------------------------------------------------------------*/
     889             : 
     890    15515650 : ivas_error push_indice(
     891             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle                    */
     892             :     Word16 id,             /* i  : ID of the indice                            */
     893             :     UWord16 value,         /* i  : value of the quantized indice               */
     894             :     Word16 nb_bits         /* i  : number of bits used to quantize the indice  */
     895             : )
     896             : {
     897             :     Word16 i;
     898             :     Word16 j;
     899             :     ivas_error error;
     900             : 
     901    15515650 :     error = IVAS_ERR_OK;
     902    15515650 :     move32();
     903             : 
     904             :     /* check the limits of the list of indices */
     905    15515650 :     IF( NE_32( ( error = check_ind_list_limits( hBstr ) ), IVAS_ERR_OK ) )
     906             :     {
     907           0 :         return IVAS_ERROR( error, "Error occured in push_indice() while re-allocating the list of indices (frame %d) !\n" );
     908             :     }
     909             : 
     910             :     /* find the location in the list of indices based on ID */
     911    15515650 :     i = hBstr->nb_ind_tot;
     912    15515650 :     move16();
     913    15515650 :     test();
     914    54584096 :     WHILE( i > 0 && ( id < hBstr->ind_list[i - 1].id ) )
     915             :     {
     916    39068446 :         test();
     917    39068446 :         i = sub( i, 1 );
     918             :     }
     919             : 
     920             :     /* shift indices, if the new ID is to be written somewhere inside the list */
     921    15515650 :     IF( i < hBstr->nb_ind_tot )
     922             :     {
     923    41730305 :         FOR( j = hBstr->nb_ind_tot; j > i; j-- )
     924             :         {
     925    39068446 :             hBstr->ind_list[j].id = hBstr->ind_list[j - 1].id;
     926    39068446 :             hBstr->ind_list[j].nb_bits = hBstr->ind_list[j - 1].nb_bits;
     927    39068446 :             hBstr->ind_list[j].value = hBstr->ind_list[j - 1].value;
     928    39068446 :             move16();
     929    39068446 :             move16();
     930    39068446 :             move16();
     931             :         }
     932             :     }
     933             : 
     934             : 
     935             :     /* store the new indice in the list */
     936    15515650 :     hBstr->ind_list[i].id = id;
     937    15515650 :     hBstr->ind_list[i].value = value;
     938    15515650 :     hBstr->ind_list[i].nb_bits = nb_bits;
     939    15515650 :     move16();
     940    15515650 :     move16();
     941    15515650 :     move16();
     942             : 
     943             :     /* updates */
     944    15515650 :     hBstr->nb_ind_tot = add( hBstr->nb_ind_tot, 1 );
     945    15515650 :     hBstr->nb_bits_tot = add( hBstr->nb_bits_tot, nb_bits );
     946    15515650 :     move16();
     947    15515650 :     move16();
     948             : 
     949    15515650 :     return error;
     950             : }
     951             : 
     952             : /*-------------------------------------------------------------------*
     953             :  * push_next_indice()
     954             :  *
     955             :  * Push a new indice into the buffer at the next position
     956             :  *-------------------------------------------------------------------*/
     957   203940544 : ivas_error push_next_indice(
     958             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle                    */
     959             :     UWord16 value,         /* i  : value of the quantized indice               */
     960             :     Word16 nb_bits         /* i  : number of bits used to quantize the indice  */
     961             : )
     962             : {
     963             :     Word16 prev_id;
     964             :     ivas_error error;
     965             : 
     966   203940544 :     error = IVAS_ERR_OK;
     967   203940544 :     move32();
     968             : 
     969             : 
     970             :     /* check the limits of the list of indices */
     971   203940544 :     IF( NE_32( ( error = check_ind_list_limits( hBstr ) ), IVAS_ERR_OK ) )
     972             :     {
     973           0 :         return error;
     974             :     }
     975             : 
     976             :     /* get the id of the previous indice -> it will be re-used */
     977   203940544 :     IF( hBstr->nb_ind_tot > 0 )
     978             :     {
     979   202966408 :         prev_id = hBstr->ind_list[hBstr->nb_ind_tot - 1].id;
     980             :     }
     981             :     ELSE
     982             :     {
     983      974136 :         prev_id = 0;
     984             :     }
     985   203940544 :     move16();
     986             : 
     987             :     /* store the values in the list */
     988   203940544 :     hBstr->ind_list[hBstr->nb_ind_tot].id = prev_id;
     989   203940544 :     hBstr->ind_list[hBstr->nb_ind_tot].value = value;
     990   203940544 :     hBstr->ind_list[hBstr->nb_ind_tot].nb_bits = nb_bits;
     991   203940544 :     move16();
     992   203940544 :     move16();
     993   203940544 :     move16();
     994             : 
     995             :     /* updates */
     996   203940544 :     hBstr->nb_ind_tot = add( hBstr->nb_ind_tot, 1 );
     997   203940544 :     hBstr->nb_bits_tot = add( hBstr->nb_bits_tot, nb_bits );
     998   203940544 :     move16();
     999   203940544 :     move16();
    1000             : 
    1001   203940544 :     return error;
    1002             : }
    1003             : 
    1004             : /*-------------------------------------------------------------------*
    1005             :  * push_next_bits()
    1006             :  * Push a bit buffer into the buffer at the next position
    1007             :  *-------------------------------------------------------------------*/
    1008     1721397 : ivas_error push_next_bits(
    1009             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle                     */
    1010             :     const UWord16 bits[],  /* i  : bit buffer to pack, sequence of single bits  */
    1011             :     const Word16 nb_bits   /* i  : number of bits to pack                       */
    1012             : )
    1013             : {
    1014             :     UWord16 code;
    1015             :     Word16 i, nb_bits_m15;
    1016             :     Indice *ptr;
    1017             :     Word16 prev_id;
    1018             :     ivas_error error;
    1019             : 
    1020     1721397 :     error = IVAS_ERR_OK;
    1021     1721397 :     move32();
    1022             : 
    1023     1721397 :     ptr = &hBstr->ind_list[hBstr->nb_ind_tot];
    1024             : 
    1025             :     /* get the id of the previous indice -> will be re-used */
    1026     1721397 :     IF( hBstr->nb_ind_tot > 0 )
    1027             :     {
    1028     1710647 :         prev_id = hBstr->ind_list[hBstr->nb_ind_tot - 1].id;
    1029     1710647 :         move16();
    1030             :     }
    1031             :     ELSE
    1032             :     {
    1033       10750 :         prev_id = 0;
    1034       10750 :         move16();
    1035             :     }
    1036     1721397 :     nb_bits_m15 = sub( nb_bits, 15 );
    1037             : 
    1038    15992596 :     FOR( i = 0; i < nb_bits_m15; i += 16 )
    1039             :     {
    1040    14271199 :         code = (UWord16) L_or( L_shl( bits[i], 15 ), L_or( L_shl( bits[i + 1], 14 ), L_or( L_shl( bits[i + 2], 13 ), L_or( L_shl( bits[i + 3], 12 ), L_or( L_shl( bits[i + 4], 11 ), L_or( L_shl( bits[i + 5], 10 ), L_or( L_shl( bits[i + 6], 9 ), L_or( L_shl( bits[i + 7], 8 ), L_or( L_shl( bits[i + 8], 7 ), L_or( L_shl( bits[i + 9], 6 ), L_or( L_shl( bits[i + 10], 5 ), L_or( L_shl( bits[i + 11], 4 ), L_or( L_shl( bits[i + 12], 3 ), L_or( L_shl( bits[i + 13], 2 ), L_or( L_shl( bits[i + 14], 1 ), bits[i + 15] ) ) ) ) ) ) ) ) ) ) ) ) ) ) );
    1041             : 
    1042             :         /* check the limits of the list of indices */
    1043    14271199 :         IF( NE_32( ( error = check_ind_list_limits( hBstr ) ), IVAS_ERR_OK ) )
    1044             :         {
    1045           0 :             return IVAS_ERROR( error, "Error occured in push_next_bits() while re-allocating the list of indices (frame %d) !\n" );
    1046             :         }
    1047    14271199 :         ptr = &hBstr->ind_list[hBstr->nb_ind_tot];
    1048             : 
    1049    14271199 :         ptr->value = code;
    1050    14271199 :         ptr->nb_bits = 16;
    1051    14271199 :         ptr->id = prev_id;
    1052    14271199 :         hBstr->nb_ind_tot = add( hBstr->nb_ind_tot, 1 );
    1053    14271199 :         move16();
    1054    14271199 :         move16();
    1055    14271199 :         move16();
    1056    14271199 :         move16();
    1057             : 
    1058    14271199 :         ++ptr;
    1059             :     }
    1060             : 
    1061    14668958 :     FOR( ; i < nb_bits; ++i )
    1062             :     {
    1063             :         /* check the limits of the list of indices */
    1064    12947561 :         IF( NE_32( ( error = check_ind_list_limits( hBstr ) ), IVAS_ERR_OK ) )
    1065             :         {
    1066           0 :             return IVAS_ERROR( error, "Error occured in push_next_bits() while re-allocating the list of indices (frame %d) !\n" );
    1067             :         }
    1068    12947561 :         ptr = &hBstr->ind_list[hBstr->nb_ind_tot];
    1069             : 
    1070    12947561 :         ptr->value = bits[i];
    1071    12947561 :         ptr->nb_bits = 1;
    1072    12947561 :         ptr->id = prev_id;
    1073    12947561 :         hBstr->nb_ind_tot = add( hBstr->nb_ind_tot, 1 );
    1074    12947561 :         move16();
    1075    12947561 :         move16();
    1076    12947561 :         move16();
    1077    12947561 :         move16();
    1078             : 
    1079    12947561 :         ++ptr;
    1080             :     }
    1081             : 
    1082     1721397 :     hBstr->nb_bits_tot = add( hBstr->nb_bits_tot, nb_bits );
    1083     1721397 :     move16();
    1084             : 
    1085     1721397 :     return error;
    1086             : }
    1087             : 
    1088             : /*-------------------------------------------------------------------*
    1089             :  * find_indice()
    1090             :  *
    1091             :  * Find indice based on its id
    1092             :  *-------------------------------------------------------------------*/
    1093             : 
    1094             : /*! r: result: index of the indice in the list, -1 if not found */
    1095          22 : int16_t find_indice(
    1096             :     BSTR_ENC_HANDLE hBstr, /* i  : encoder bitstream handle                    */
    1097             :     const int16_t id,      /* i  : ID of the indice                            */
    1098             :     uint16_t *value,       /* o  : value of the quantized indice               */
    1099             :     int16_t *nb_bits       /* o  : number of bits used to quantize the indice  */
    1100             : )
    1101             : {
    1102             :     int16_t i;
    1103             : 
    1104         133 :     for ( i = 0; i < hBstr->nb_ind_tot; i++ )
    1105             :     {
    1106         133 :         if ( hBstr->ind_list[i].id == id && hBstr->ind_list[i].nb_bits > 0 )
    1107             :         {
    1108          22 :             *value = hBstr->ind_list[i].value;
    1109          22 :             *nb_bits = hBstr->ind_list[i].nb_bits;
    1110          22 :             return i;
    1111             :         }
    1112             :     }
    1113             : 
    1114           0 :     return -1;
    1115             : }
    1116             : 
    1117             : 
    1118             : /*-------------------------------------------------------------------*
    1119             :  * delete_indice()
    1120             :  *
    1121             :  * Delete indice based on its id (note, that nb_ind_tot and nb_bits_tot are updated)
    1122             :  *-------------------------------------------------------------------*/
    1123             : 
    1124             : /*! r: number of deleted indices */
    1125      686451 : uint16_t delete_indice(
    1126             :     BSTR_ENC_HANDLE hBstr, /* i  : encoder bitstream handle                    */
    1127             :     const int16_t id       /* i  : ID of the indice                            */
    1128             : )
    1129             : {
    1130             :     int16_t i, j;
    1131             : 
    1132      686451 :     j = 0;
    1133     9875269 :     for ( i = 0; i < hBstr->nb_ind_tot; i++ )
    1134             :     {
    1135     9188818 :         if ( hBstr->ind_list[i].id == id )
    1136             :         {
    1137        6287 :             hBstr->nb_bits_tot -= hBstr->ind_list[i].nb_bits;
    1138        6287 :             continue;
    1139             :         }
    1140             : 
    1141     9182531 :         if ( j < i )
    1142             :         {
    1143             :             /* shift the indice left */
    1144        3938 :             hBstr->ind_list[j].id = hBstr->ind_list[i].id;
    1145        3938 :             hBstr->ind_list[j].value = hBstr->ind_list[i].value;
    1146        3938 :             hBstr->ind_list[j].nb_bits = hBstr->ind_list[i].nb_bits;
    1147             :         }
    1148             : 
    1149     9182531 :         j++;
    1150             :     }
    1151             : 
    1152      686451 :     hBstr->nb_ind_tot = j;
    1153             : 
    1154      692738 :     for ( ; j < i; j++ )
    1155             :     {
    1156             :         /* reset the shifted indices at the end of the list */
    1157        6287 :         hBstr->ind_list[j].nb_bits = -1;
    1158             :     }
    1159             : 
    1160      686451 :     return i - j;
    1161             : }
    1162             : 
    1163             : 
    1164             : /*-------------------------------------------------------------------*
    1165             :  * get_next_indice()
    1166             :  *
    1167             :  * Get the next indice from the buffer
    1168             :  *-------------------------------------------------------------------*/
    1169             : 
    1170             : /*! r: value of the indice */
    1171        3046 : uint16_t get_next_indice(
    1172             :     Decoder_State *st, /* i/o: decoder state structure                               */
    1173             :     int16_t nb_bits    /* i  : number of bits that were used to quantize the indice  */
    1174             : )
    1175             : {
    1176             :     uint16_t value;
    1177             :     int16_t i;
    1178             :     int32_t nbits_total;
    1179             : 
    1180        3046 :     assert( nb_bits <= 16 );
    1181             : 
    1182        3046 :     nbits_total = st->total_brate / FRAMES_PER_SEC;
    1183             :     /* detect corrupted bitstream */
    1184        3046 :     if ( st->next_bit_pos + nb_bits > nbits_total )
    1185             :     {
    1186           0 :         st->BER_detect = 1;
    1187           0 :         return ( 0 );
    1188             :     }
    1189             : 
    1190        3046 :     value = 0;
    1191        7504 :     for ( i = 0; i < nb_bits; i++ )
    1192             :     {
    1193        4458 :         value <<= 1;
    1194        4458 :         value += st->bit_stream[st->next_bit_pos + i];
    1195             :     }
    1196             : 
    1197             :     /* update the position in the bitstream */
    1198        3046 :     st->next_bit_pos += nb_bits;
    1199             : 
    1200        3046 :     return value;
    1201             : }
    1202             : 
    1203             : /*-------------------------------------------------------------------*
    1204             :  * get_next_indice_1()
    1205             :  *
    1206             :  * Get the next 1-bit indice from the buffer
    1207             :  *-------------------------------------------------------------------*/
    1208             : 
    1209             : /*! r: value of the indice */
    1210           0 : uint16_t get_next_indice_1(
    1211             :     Decoder_State *st /* i/o: decoder state structure   */
    1212             : )
    1213             : {
    1214             :     int32_t nbits_total;
    1215           0 :     nbits_total = st->total_brate / FRAMES_PER_SEC;
    1216             :     /* detect corrupted bitstream */
    1217           0 :     if ( ( st->next_bit_pos + 1 > nbits_total && st->codec_mode == MODE1 ) ||
    1218           0 :          ( ( st->next_bit_pos + 1 > nbits_total + ( 2 * 8 ) ) && st->codec_mode == MODE2 ) /* add two zero bytes for arithmetic coder flush */
    1219             :     )
    1220             :     {
    1221           0 :         st->BER_detect = 1;
    1222           0 :         return ( 0 );
    1223             :     }
    1224             : 
    1225           0 :     return st->bit_stream[st->next_bit_pos++];
    1226             : }
    1227             : 
    1228             : /*-------------------------------------------------------------------*
    1229             :  * get_next_indice_tmp()
    1230             :  *
    1231             :  * update the total number of bits and the position in the bitstream
    1232             :  *-------------------------------------------------------------------*/
    1233             : 
    1234      560908 : void get_next_indice_tmp(
    1235             :     Decoder_State *st, /* o  : decoder state structure                              */
    1236             :     int16_t nb_bits    /* i  : number of bits that were used to quantize the indice */
    1237             : )
    1238             : {
    1239             :     /* update the position in the bitstream */
    1240      560908 :     st->next_bit_pos += nb_bits;
    1241             : 
    1242      560908 :     return;
    1243             : }
    1244             : 
    1245             : /*-------------------------------------------------------------------*
    1246             :  * get_indice()
    1247             :  *
    1248             :  * Get indice at specific position in the buffer
    1249             :  *-------------------------------------------------------------------*/
    1250             : 
    1251             : /*! r: value of the indice */
    1252      536584 : uint16_t get_indice(
    1253             :     Decoder_State *st, /* i/o: decoder state structure                                    */
    1254             :     int16_t pos,       /* i  : absolute position in the bitstream (update after the read) */
    1255             :     int16_t nb_bits    /* i  : number of bits that were used to quantize the indice       */
    1256             : )
    1257             : {
    1258             :     uint16_t value;
    1259             :     int16_t i;
    1260             :     int32_t nbits_total;
    1261             : 
    1262      536584 :     assert( nb_bits <= 16 );
    1263             : 
    1264      536584 :     nbits_total = st->total_brate / FRAMES_PER_SEC;
    1265             : 
    1266             :     /* detect corrupted bitstream */
    1267      536584 :     if ( pos + nb_bits > nbits_total )
    1268             :     {
    1269           0 :         st->BER_detect = 1;
    1270           0 :         return ( 0 );
    1271             :     }
    1272             : 
    1273      536584 :     value = 0;
    1274     3395718 :     for ( i = 0; i < nb_bits; i++ )
    1275             :     {
    1276     2859134 :         value <<= 1;
    1277     2859134 :         value += st->bit_stream[pos + i];
    1278             :     }
    1279      536584 :     return value;
    1280             : }
    1281             : 
    1282             : /*-------------------------------------------------------------------*
    1283             :  * get_indice_st()
    1284             :  *
    1285             :  * Get indice at specific position in the buffer
    1286             :  *-------------------------------------------------------------------*/
    1287             : 
    1288             : /*! r: value of the indice */
    1289       19352 : UWord16 get_indice_st(
    1290             :     Decoder_State *st,          /* i/o: decoder state structure                 */
    1291             :     const Word32 element_brate, /* i  : element bitrate                         */
    1292             :     const Word16 pos,           /* i  : absolute position in the bitstream      */
    1293             :     const Word16 nb_bits        /* i  : number of bits to quantize the indice   */
    1294             : )
    1295             : {
    1296             :     UWord16 value;
    1297             :     Word16 i;
    1298             : 
    1299       19352 :     assert( nb_bits <= 16 );
    1300             : 
    1301             :     /* detect corrupted bitstream */
    1302       19352 :     IF( GT_32( L_deposit_l( add( pos, nb_bits ) ), Mpy_32_32( element_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) ) )
    1303             :     {
    1304           0 :         st->BER_detect = 1;
    1305           0 :         move16();
    1306           0 :         return ( 0 );
    1307             :     }
    1308             : 
    1309       19352 :     value = 0;
    1310       19352 :     move16();
    1311       62880 :     FOR( i = 0; i < nb_bits; i++ )
    1312             :     {
    1313       43528 :         value = shl( value, 1 );
    1314       43528 :         value = add( value, st->bit_stream[pos + i] );
    1315             :     }
    1316             : 
    1317       19352 :     return value;
    1318             : }
    1319             : #define WMC_TOOL_SKIP
    1320             : 
    1321             : /*-------------------------------------------------------------------*
    1322             :  * reset_indices_dec()
    1323             :  *
    1324             :  * Reset the buffer of decoder indices
    1325             :  *-------------------------------------------------------------------*/
    1326     2162322 : void reset_indices_dec(
    1327             :     Decoder_State *st )
    1328             : {
    1329     2162322 :     st->next_bit_pos = 0;
    1330     2162322 :     move16();
    1331             : 
    1332     2162322 :     return;
    1333             : }
    1334             : /*-------------------------------------------------------------------*
    1335             :  * write_indices_to_stream()
    1336             :  *
    1337             :  * writing forward or backward to a serial stream
    1338             :  *-------------------------------------------------------------------*/
    1339     1478352 : static Word16 write_indices_to_stream_fx(
    1340             :     Indice *ind_list,
    1341             :     UWord16 **pt_stream,
    1342             :     const Word16 inc,
    1343             :     const Word16 num_indices )
    1344             : {
    1345             :     Word16 i, k;
    1346             :     Word16 value, nb_bits;
    1347             :     UWord16 mask;
    1348             : 
    1349   190877606 :     FOR( i = 0; i < num_indices; i++ )
    1350             :     {
    1351   189399254 :         value = ind_list[i].value;
    1352   189399254 :         nb_bits = ind_list[i].nb_bits;
    1353   189399254 :         move16();
    1354   189399254 :         move16();
    1355             : 
    1356   189399254 :         IF( nb_bits > 0 )
    1357             :         {
    1358             :             /* mask from MSB to LSB */
    1359   189320239 :             mask = (UWord16) L_shl( 1, sub( nb_bits, 1 ) );
    1360             : 
    1361             :             /* write bit by bit */
    1362  1173104807 :             FOR( k = 0; k < nb_bits; k++ )
    1363             :             {
    1364   983784568 :                 IF( L_and( value, mask ) )
    1365             :                 {
    1366   474995208 :                     **pt_stream = 1;
    1367   474995208 :                     move16();
    1368   474995208 :                     *pt_stream += inc;
    1369             :                 }
    1370             :                 ELSE
    1371             :                 {
    1372   508789360 :                     **pt_stream = 0;
    1373   508789360 :                     move16();
    1374   508789360 :                     *pt_stream += inc;
    1375             :                 }
    1376             : 
    1377   983784568 :                 mask = (UWord16) L_shr( mask, 1 );
    1378             :             }
    1379             :         }
    1380             :     }
    1381     1478352 :     return 0;
    1382             : }
    1383             : 
    1384             : /*-------------------------------------------------------------------*
    1385             :  * write_indices_element()
    1386             :  *
    1387             :  * Bitstream writing function of one element (one SCE or one CPE)
    1388             :  *-------------------------------------------------------------------*/
    1389      763698 : static ivas_error write_indices_element_fx(
    1390             :     Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure                                          */
    1391             :     UWord16 **pt_stream,     /* i  : pointer to bitstream buffer                                     */
    1392             :     const Word16 is_SCE,     /* i  : flag to distingusih SCE and CPE                                 */
    1393             :     const Word16 element_id  /* i  : id of the SCE or CPE                                            */
    1394             : )
    1395             : {
    1396             :     Word16 ch;
    1397      763698 :     Encoder_State **sts = NULL;
    1398             :     UWord16 *pt_stream_loc;
    1399             :     UWord16 *pt_stream_backup;
    1400             :     UWord16 *pt_stream_end;
    1401             :     Word16 nb_bits_tot_metadata;
    1402             :     Word16 nb_ind_tot_metadata;
    1403             : 
    1404             :     Indice *ind_list_metadata;
    1405             :     Word16 n, n_channels;
    1406             :     ivas_error error;
    1407             : 
    1408      763698 :     error = IVAS_ERR_OK;
    1409      763698 :     move32();
    1410             : 
    1411      763698 :     ind_list_metadata = NULL;
    1412      763698 :     nb_ind_tot_metadata = 0;
    1413      763698 :     move16();
    1414             : 
    1415      763698 :     IF( EQ_32( st_ivas->hEncoderConfig->ivas_format, MONO_FORMAT ) )
    1416             :     {
    1417        3100 :         sts = st_ivas->hSCE[0]->hCoreCoder;
    1418        3100 :         nb_bits_tot_metadata = 0;
    1419        3100 :         move16();
    1420             :     }
    1421             :     ELSE
    1422             :     {
    1423      760598 :         nb_bits_tot_metadata = 0;
    1424      760598 :         move16();
    1425      760598 :         test();
    1426      760598 :         test();
    1427      760598 :         IF( is_SCE && st_ivas->hSCE[element_id] != NULL )
    1428             :         {
    1429      350343 :             sts = st_ivas->hSCE[element_id]->hCoreCoder;
    1430             : 
    1431      350343 :             IF( st_ivas->hSCE[element_id]->hMetaData != NULL )
    1432             :             {
    1433      179915 :                 nb_bits_tot_metadata = st_ivas->hSCE[element_id]->hMetaData->nb_bits_tot;
    1434      179915 :                 ind_list_metadata = st_ivas->hSCE[element_id]->hMetaData->ind_list;
    1435      179915 :                 nb_ind_tot_metadata = st_ivas->hSCE[element_id]->hMetaData->nb_ind_tot;
    1436      179915 :                 move16();
    1437      179915 :                 move16();
    1438             :             }
    1439             :         }
    1440      410255 :         ELSE IF( !is_SCE && st_ivas->hCPE[element_id] != NULL )
    1441             :         {
    1442      410255 :             sts = st_ivas->hCPE[element_id]->hCoreCoder;
    1443             : 
    1444      410255 :             IF( st_ivas->hCPE[element_id]->hMetaData != NULL )
    1445             :             {
    1446      249056 :                 nb_bits_tot_metadata = st_ivas->hCPE[element_id]->hMetaData->nb_bits_tot;
    1447      249056 :                 ind_list_metadata = st_ivas->hCPE[element_id]->hMetaData->ind_list;
    1448      249056 :                 nb_ind_tot_metadata = st_ivas->hCPE[element_id]->hMetaData->nb_ind_tot;
    1449      249056 :                 move16();
    1450      249056 :                 move16();
    1451             :             }
    1452             :         }
    1453             :     }
    1454             : 
    1455      763698 :     n_channels = 1;
    1456      763698 :     move16();
    1457      763698 :     if ( GT_16( sts[0]->element_mode, IVAS_CPE_DFT ) )
    1458             :     {
    1459      350622 :         n_channels = CPE_CHANNELS;
    1460      350622 :         move16();
    1461             :     }
    1462             : 
    1463             :     /*----------------------------------------------------------------*
    1464             :      * Bitstream packing (conversion of individual indices into a serial stream)
    1465             :      *----------------------------------------------------------------*/
    1466             : 
    1467      763698 :     pt_stream_loc = *pt_stream;
    1468      763698 :     pt_stream_end = pt_stream_loc;
    1469             : 
    1470     1878018 :     FOR( n = 0; n < n_channels; n++ )
    1471             :     {
    1472             :         /* write the metadata buffer */
    1473     1114320 :         test();
    1474     1114320 :         IF( n == 0 && nb_bits_tot_metadata != 0 )
    1475             :         {
    1476      364032 :             pt_stream_backup = pt_stream_loc;
    1477             : 
    1478      866799 :             FOR( ch = 0; ch < n_channels; ch++ )
    1479             :             {
    1480      502767 :                 pt_stream_loc += sts[ch]->hBstr->nb_bits_tot;
    1481             :             }
    1482      364032 :             pt_stream_loc += nb_bits_tot_metadata - 1;
    1483      364032 :             pt_stream_end = pt_stream_loc + 1;
    1484             : 
    1485      364032 :             write_indices_to_stream_fx( ind_list_metadata, &pt_stream_loc, -1,
    1486             :                                         nb_ind_tot_metadata );
    1487             : 
    1488             :             /* restore previous pointer position */
    1489      364032 :             pt_stream_loc = pt_stream_backup;
    1490             :         }
    1491     1114320 :         write_indices_to_stream_fx( sts[n]->hBstr->ind_list, &pt_stream_loc, 1,
    1492     1114320 :                                     sts[n]->hBstr->nb_ind_tot );
    1493             : 
    1494     1114320 :         if ( pt_stream_loc > pt_stream_end )
    1495             :         {
    1496      564431 :             pt_stream_end = pt_stream_loc;
    1497             :         }
    1498             :     }
    1499             : 
    1500             :     /*----------------------------------------------------------------*
    1501             :      * Clearing of indices
    1502             :      * Reset index pointers
    1503             :      *----------------------------------------------------------------*/
    1504             : 
    1505      763698 :     IF( is_SCE ) /* EVS and SCE */
    1506             :     {
    1507      353443 :         IF( st_ivas->hSCE[element_id]->hMetaData != NULL )
    1508             :         {
    1509      179915 :             reset_indices_enc_fx( st_ivas->hSCE[element_id]->hMetaData, st_ivas->hSCE[element_id]->hMetaData->nb_ind_tot );
    1510             :         }
    1511             : 
    1512      353443 :         reset_indices_enc_fx( sts[0]->hBstr, sts[0]->hBstr->nb_ind_tot );
    1513             :     }
    1514             :     ELSE
    1515             :     {
    1516      410255 :         IF( st_ivas->hCPE[element_id]->hMetaData != NULL )
    1517             :         {
    1518      249056 :             reset_indices_enc_fx( st_ivas->hCPE[element_id]->hMetaData, st_ivas->hCPE[element_id]->hMetaData->nb_ind_tot );
    1519             :         }
    1520             : 
    1521     1171132 :         FOR( n = 0; n < n_channels; n++ )
    1522             :         {
    1523      760877 :             reset_indices_enc_fx( sts[n]->hBstr, sts[n]->hBstr->nb_ind_tot );
    1524             :         }
    1525             :     }
    1526             : 
    1527             :     /* update pointer */
    1528      763698 :     *pt_stream = pt_stream_end;
    1529             : 
    1530      763698 :     return error;
    1531             : }
    1532             : 
    1533             : /*-------------------------------------------------------------------*
    1534             :  * write_indices_ivas()
    1535             :  *
    1536             :  * Write the buffer of indices to a serial bitstream buffer,
    1537             :  * each bit represented as a uint16_t of value 0 or 1
    1538             :  *-------------------------------------------------------------------*/
    1539      424388 : ivas_error write_indices_ivas_fx(
    1540             :     Encoder_Struct *st_ivas, /* i/o: encoder state structure             */
    1541             :     UWord16 *bit_stream,     /* i/o: output bitstream                    */
    1542             :     UWord16 *num_bits        /* i  : number of indices written to output */
    1543             : )
    1544             : {
    1545             :     Word16 i, n;
    1546             :     UWord16 *pt_stream;
    1547             :     ivas_error error;
    1548             : 
    1549      424388 :     error = IVAS_ERR_OK;
    1550      424388 :     move32();
    1551             : 
    1552      424388 :     pt_stream = bit_stream;
    1553  4346157508 :     FOR( i = 0; i < MAX_BITS_PER_FRAME; ++i )
    1554             :     {
    1555  4345733120 :         bit_stream[i] = 0;
    1556  4345733120 :         move16();
    1557             :     }
    1558             : 
    1559             : 
    1560             :     /*-----------------------------------------------------------------*
    1561             :      * Encode Payload
    1562             :      *-----------------------------------------------------------------*/
    1563             : 
    1564      777831 :     FOR( n = 0; n < st_ivas->nSCE; n++ )
    1565             :     {
    1566      353443 :         write_indices_element_fx( st_ivas, &pt_stream, 1, n );
    1567             :     }
    1568             : 
    1569      834643 :     FOR( n = 0; n < st_ivas->nCPE; n++ )
    1570             :     {
    1571      410255 :         write_indices_element_fx( st_ivas, &pt_stream, 0, n );
    1572             :     }
    1573             : 
    1574      424388 :     *num_bits = (UWord16) ( pt_stream - bit_stream );
    1575      424388 :     move16();
    1576             : 
    1577      424388 :     return error;
    1578             : }
    1579             : 
    1580             : /*-------------------------------------------------------------------*
    1581             :  * decoder_selectCodec()
    1582             :  *
    1583             :  *
    1584             :  *-------------------------------------------------------------------*/
    1585             : 
    1586        3100 : static void decoder_selectCodec(
    1587             :     Decoder_State *st,         /* i/o: decoder state structure                */
    1588             :     const int32_t total_brate, /* i  : total bitrate                          */
    1589             :     const int16_t bit0         /* i  : first bit                              */
    1590             : )
    1591             : {
    1592             :     /* set the AMR-WB IO flag */
    1593        3100 :     if ( rate2AMRWB_IOmode( total_brate ) != -1 )
    1594             :     {
    1595           0 :         st->Opt_AMR_WB = 1;
    1596             :     }
    1597        3100 :     else if ( total_brate != FRAME_NO_DATA )
    1598             :     {
    1599        3100 :         st->Opt_AMR_WB = 0;
    1600             :     }
    1601             : 
    1602        3100 :     if ( st->Opt_AMR_WB )
    1603             :     {
    1604           0 :         st->codec_mode = MODE1;
    1605             :     }
    1606             :     else
    1607             :     {
    1608        3100 :         st->codec_mode = get_codec_mode( total_brate );
    1609             : 
    1610        3100 :         if ( st->codec_mode == -1 )
    1611             :         {
    1612           0 :             switch ( total_brate )
    1613             :             {
    1614           0 :                 case 0:
    1615           0 :                     st->codec_mode = st->last_codec_mode;
    1616           0 :                     break;
    1617           0 :                 case 2400:
    1618           0 :                     st->codec_mode = st->last_codec_mode;
    1619           0 :                     break;
    1620           0 :                 case 2800:
    1621           0 :                     st->codec_mode = MODE1;
    1622           0 :                     break;
    1623           0 :                 default: /* validate that total_brate (derived from RTP packet or a file header) is one of the defined bitrates  */
    1624           0 :                     st->codec_mode = st->last_codec_mode;
    1625           0 :                     st->bfi = 1;
    1626           0 :                     break;
    1627             :             }
    1628        3100 :         }
    1629             :     }
    1630             : 
    1631        3100 :     if ( st->ini_frame == 0 )
    1632             :     {
    1633           3 :         if ( st->codec_mode == -1 )
    1634             :         {
    1635           0 :             st->codec_mode = MODE1;
    1636             :         }
    1637           3 :         st->last_codec_mode = st->codec_mode;
    1638             :     }
    1639             : 
    1640             :     /* set SID/CNG type */
    1641        3100 :     if ( total_brate == SID_2k40 )
    1642             :     {
    1643           0 :         if ( bit0 == 0 )
    1644             :         {
    1645           0 :             st->cng_type = LP_CNG;
    1646             : 
    1647             :             /* force MODE1 when selecting LP_CNG */
    1648           0 :             st->codec_mode = MODE1;
    1649             :         }
    1650           0 :         else if ( bit0 == 1 )
    1651             :         {
    1652           0 :             st->cng_type = FD_CNG;
    1653           0 :             if ( st->last_codec_mode == MODE2 && st->last_total_brate == ACELP_13k20 )
    1654             :             {
    1655           0 :                 st->codec_mode = MODE1;
    1656             :             }
    1657             :         }
    1658             :     }
    1659             : 
    1660        3100 :     return;
    1661             : }
    1662             : 
    1663             : 
    1664             : /*-------------------------------------------------------------------*
    1665             :  * reset_elements()
    1666             :  *
    1667             :  * Simulate packet losses by reading FEC pattern from external file
    1668             :  *-------------------------------------------------------------------*/
    1669             : 
    1670      788299 : Decoder_State **reset_elements(
    1671             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure      */
    1672             : )
    1673             : {
    1674             :     Word16 k, n;
    1675      788299 :     Decoder_State **sts = NULL; /* to avoid compilation warning */
    1676             : 
    1677     1417174 :     FOR( k = 0; k < st_ivas->nSCE; k++ )
    1678             :     {
    1679      628875 :         sts = st_ivas->hSCE[k]->hCoreCoder;
    1680             : 
    1681      628875 :         sts[0]->bfi = 0;
    1682      628875 :         sts[0]->BER_detect = 0;
    1683      628875 :         sts[0]->mdct_sw_enable = 0;
    1684      628875 :         sts[0]->mdct_sw = 0;
    1685             : 
    1686      628875 :         move16();
    1687      628875 :         move16();
    1688      628875 :         move16();
    1689      628875 :         move16();
    1690             : 
    1691      628875 :         reset_indices_dec( sts[0] );
    1692             :     }
    1693             : 
    1694     1554221 :     FOR( k = 0; k < st_ivas->nCPE; k++ )
    1695             :     {
    1696      765922 :         sts = st_ivas->hCPE[k]->hCoreCoder;
    1697             : 
    1698     2297766 :         FOR( n = 0; n < CPE_CHANNELS; n++ )
    1699             :         {
    1700     1531844 :             sts[n]->bfi = 0;
    1701     1531844 :             sts[n]->BER_detect = 0;
    1702     1531844 :             sts[n]->mdct_sw_enable = 0;
    1703     1531844 :             sts[n]->mdct_sw = 0;
    1704             : 
    1705     1531844 :             move16();
    1706     1531844 :             move16();
    1707     1531844 :             move16();
    1708     1531844 :             move16();
    1709             : 
    1710     1531844 :             reset_indices_dec( sts[n] );
    1711             :         }
    1712             :     }
    1713             : 
    1714      788299 :     return sts; /* return last decoder state */
    1715             : }
    1716             : 
    1717             : /*-------------------------------------------------------------------*
    1718             :  * ivas_set_bitstream_pointers()
    1719             :  *
    1720             :  * Set bitstream pointers for every SCE/CPE Core-Decoder
    1721             :  *-------------------------------------------------------------------*/
    1722             : 
    1723      391414 : void ivas_set_bitstream_pointers(
    1724             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure    */
    1725             : )
    1726             : {
    1727             :     int16_t k, num_bits;
    1728             :     Decoder_State **sts;
    1729             : 
    1730      391414 :     num_bits = 0;
    1731             : 
    1732             :     /* set bitstream pointers for SCEs */
    1733      705105 :     for ( k = 0; k < st_ivas->nSCE; k++ )
    1734             :     {
    1735      313691 :         sts = st_ivas->hSCE[k]->hCoreCoder;
    1736      313691 :         sts[0]->bit_stream = st_ivas->bit_stream + num_bits;
    1737      313691 :         num_bits += (int16_t) ( st_ivas->hSCE[k]->element_brate / FRAMES_PER_SEC );
    1738             :     }
    1739             : 
    1740             :     /* set bitstream pointers for CPEs */
    1741      771479 :     for ( k = 0; k < st_ivas->nCPE; k++ )
    1742             :     {
    1743      380065 :         sts = st_ivas->hCPE[k]->hCoreCoder;
    1744      380065 :         sts[0]->bit_stream = st_ivas->bit_stream + num_bits;
    1745      380065 :         num_bits += (int16_t) ( st_ivas->hCPE[k]->element_brate / FRAMES_PER_SEC );
    1746             :     }
    1747             : 
    1748      391414 :     return;
    1749             : }
    1750             : 
    1751             : 
    1752             : /*-------------------------------------------------------------------*
    1753             :  * read_indices()
    1754             :  *
    1755             :  * Read indices from an ITU-T G.192 bitstream to the buffer
    1756             :  * Simulate packet losses by inserting frame erasures
    1757             :  *-------------------------------------------------------------------*/
    1758             : 
    1759             : /*! r: 1 = reading OK, 0 = problem */
    1760      399985 : ivas_error read_indices_fx(
    1761             :     Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure         */
    1762             :     UWord16 bit_stream[],    /* i  : bitstream buffer               */
    1763             :     UWord16 num_bits,        /* i  : number of bits in bitstream    */
    1764             :     Word16 *prev_ft_speech,
    1765             :     Word16 *CNG,
    1766             :     Word16 bfi /* i  : bad frame indicator            */
    1767             : )
    1768             : {
    1769             :     Word16 k;
    1770             :     Decoder_State **sts;
    1771      399985 :     Word32 total_brate = 0;
    1772      399985 :     move32();
    1773             :     Word16 curr_ft_good_sp, curr_ft_bad_sp;
    1774             :     Word16 g192_sid_first, sid_upd_bad, sid_update;
    1775             :     Word16 speech_bad, speech_lost;
    1776             :     Word16 n;
    1777             :     ivas_error error;
    1778             : 
    1779      399985 :     error = IVAS_ERR_OK;
    1780      399985 :     move32();
    1781             : 
    1782      399985 :     st_ivas->BER_detect = 0;
    1783      399985 :     move16();
    1784      399985 :     st_ivas->num_bits = num_bits;
    1785      399985 :     move16();
    1786      399985 :     sts = reset_elements( st_ivas );
    1787             : 
    1788      399985 :     st_ivas->bfi = bfi;
    1789      399985 :     move16();
    1790             : 
    1791             :     /* convert the frame length to total bitrate */
    1792      399985 :     total_brate = imult3216( num_bits, FRAMES_PER_SEC );
    1793      399985 :     move32();
    1794             : 
    1795             :     /*  verify that a  valid  num bits value  is present in the G.192 file */
    1796             :     /*  only AMRWB, EVS or IVAS bitrates or 0(NO DATA) are  allowed  in G.192 file frame reading  */
    1797      399985 :     IF( NE_32( st_ivas->ivas_format, MONO_FORMAT ) )
    1798             :     {
    1799      396885 :         k = 0;
    1800      396885 :         move16();
    1801             : 
    1802      396885 :         test();
    1803     3338374 :         WHILE( LT_16( k, SIZE_IVAS_BRATE_TBL ) && NE_32( total_brate, ivas_brate_tbl[k] ) )
    1804             :         {
    1805     2941489 :             k = add( k, 1 );
    1806             :         }
    1807             : 
    1808      396885 :         test();
    1809      396885 :         test();
    1810      396885 :         test();
    1811      396885 :         test();
    1812      396885 :         IF( EQ_32( st_ivas->ivas_format, ISM_FORMAT ) && ( LT_16( k, SIZE_IVAS_BRATE_TBL ) || LE_32( total_brate, SID_2k40 ) ) )
    1813             :         {
    1814       93770 :             st_ivas->element_mode_init = IVAS_SCE;
    1815       93770 :             move16();
    1816             :         }
    1817      303115 :         ELSE IF( ( EQ_32( st_ivas->ivas_format, SBA_FORMAT ) || EQ_32( st_ivas->ivas_format, MASA_FORMAT ) ) && LE_32( total_brate, SID_2k40 ) )
    1818             :         {
    1819        3901 :             st_ivas->element_mode_init = IVAS_SCE;
    1820        3901 :             move16();
    1821             :         }
    1822      299214 :         ELSE IF( EQ_16( k, SIZE_IVAS_BRATE_TBL ) )
    1823             :         {
    1824           0 :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error, illegal bitrate (%d) in the  G.192 frame ! Exiting ! \n", total_brate );
    1825             :         }
    1826             :         ELSE
    1827             :         {
    1828      299214 :             st_ivas->element_mode_init = -1;
    1829      299214 :             move16();
    1830             :         }
    1831             :     }
    1832             :     ELSE /* AMRWB or EVS */
    1833             :     {
    1834        3100 :         st_ivas->element_mode_init = EVS_MONO;
    1835        3100 :         move16();
    1836             : 
    1837        3100 :         IF( rate2EVSmode_float( total_brate, NULL ) < 0 ) /* negative value means that a valid rate was not found */
    1838             :         {
    1839           0 :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error, illegal bitrate (%d) in  the  G.192 frame ! Exiting ! \n", total_brate );
    1840             :         }
    1841             :     }
    1842             : 
    1843             :     /* G.192 RX DTX handler*/
    1844             :     /* handle SID_FIRST, SID_BAD, SPEECH_LOST,  NO_DATA as properly  as possible for the ITU-T  G.192 format  */
    1845             :     /* (total_brate, bfi , st_CNG)   =  rx_handler(received frame type, [previous frame type],  past CNG state, past core) */
    1846      399985 :     curr_ft_good_sp = 0;
    1847      399985 :     move16();
    1848      399985 :     curr_ft_bad_sp = 0;
    1849      399985 :     move16();
    1850             : 
    1851      399985 :     IF( is_DTXrate( total_brate ) == 0 )
    1852             :     {
    1853      380176 :         IF( st_ivas->bfi == 0 )
    1854             :         {
    1855      372669 :             curr_ft_good_sp = 1;
    1856      372669 :             move16();
    1857             :         }
    1858             :         ELSE
    1859             :         {
    1860        7507 :             curr_ft_bad_sp = 1;
    1861        7507 :             move16();
    1862             :         }
    1863             :     }
    1864             : 
    1865      399985 :     sid_update = 0;
    1866      399985 :     move16();
    1867      399985 :     sid_upd_bad = 0;
    1868      399985 :     move16();
    1869      399985 :     IF( EQ_16( is_SIDrate( total_brate ), 1 ) )
    1870             :     {
    1871        2980 :         IF( st_ivas->bfi == 0 )
    1872             :         {
    1873        2907 :             sid_update = 1;
    1874        2907 :             move16();
    1875             :         }
    1876             :         ELSE
    1877             :         {
    1878          73 :             sid_upd_bad = 1; /* this frame type may happen in ETSI/3GPP CS cases, a corrupt SID frames  */
    1879          73 :             move16();
    1880             :         }
    1881             :     }
    1882             : 
    1883             :     /* all zero indices/bits iSP AMRWB SID_update results in a valid LP filter with extremely high LP-filter-gain  */
    1884             :     /* all zero indices/bits  may be a result of CS bit errors  and/or  erroneously injected by gateways or by a bad dejitter handlers */
    1885      399985 :     test();
    1886      399985 :     IF( EQ_32( total_brate, SID_1k75 ) && EQ_16( sid_update, 1 ) )
    1887             :     {
    1888             :         /* valid sid_update received, check for very risky but formally valid content  */
    1889           0 :         Word16 sum = 0;
    1890           0 :         move16();
    1891           0 :         FOR( k = 0; k < num_bits; ++k )
    1892             :         {
    1893           0 :             sum = add( sum, extract_l( EQ_32( bit_stream[k], 1 ) ) ); /*   check of 35 zeroes   */
    1894             :         }
    1895           0 :         if ( sum == 0 )
    1896             :         {                    /* all zeros  */
    1897           0 :             sid_upd_bad = 1; /* initial signal as corrupt (BER likely)  */
    1898           0 :             move16();
    1899             :         }
    1900             :     }
    1901             : 
    1902             :     /* AMRWB  26.173 G.192  file reader (read_serial)  does not declare/use SID_BAD ft,
    1903             :                       it declares every bad synch marked frame initially as  a lost_speech frame,
    1904             :                       and then the RXDTX handler CNG state decides the decoding mode CNG/SPEECH.
    1905             :                       While In the AMRWB ETSI/3GPP format eid a CRC error in a detected  SID_UPDATE frames triggers SID_BAD.
    1906             : 
    1907             :                       Here we inhibit use of the SID-length info, even though it is available in the G.192 file format after STL/EID-XOR .
    1908             :                    */
    1909      399985 :     IF( sid_upd_bad )
    1910             :     {
    1911          73 :         sid_upd_bad = 0;
    1912          73 :         move16();
    1913          73 :         total_brate = FRAME_NO_DATA; /* treat SID_BAD  as a  stolen signaling frame --> SPEECH LOST */
    1914          73 :         move32();
    1915             :     }
    1916             : 
    1917      399985 :     g192_sid_first = 0;
    1918      399985 :     move16();
    1919             : 
    1920      399985 :     test();
    1921      399985 :     test();
    1922      399985 :     test();
    1923      399985 :     test();
    1924      399985 :     if ( EQ_32( st_ivas->ivas_format, MONO_FORMAT ) && EQ_16( sts[0]->core, AMR_WB_CORE ) && *prev_ft_speech && total_brate == FRAME_NO_DATA && st_ivas->bfi == 0 )
    1925             :     {
    1926           0 :         g192_sid_first = 1; /*  SID_FIRST detected for previous AMRWB/AMRWBIO  active frames only  */
    1927             :                             /* It is not possible to perfectly simulate rate switching conditions EVS->AMRWBIO  where:
    1928             :                                            the very first SID_FIRST detection is based on a past EVS active frame
    1929             :                                            and  a  good length 0  "SID_FIRST"(NO_DATA)   frame is sent in AMRWBIO,
    1930             :                                            due to the one frame state memory in the AMRWB legacy  G.192 SID_FIRST encoding
    1931             :                                          */
    1932           0 :         move16();
    1933             :     }
    1934             : 
    1935      399985 :     speech_bad = 0;
    1936      399985 :     move16();
    1937             : 
    1938      399985 :     test();
    1939      399985 :     if ( st_ivas->bfi != 0 && ( is_DTXrate( total_brate ) == 0 ) )
    1940             :     {
    1941        7507 :         speech_bad = 1; /* initial ft assumption, CNG_state decides what to do */
    1942        7507 :         move16();
    1943             :     }
    1944             : 
    1945      399985 :     speech_lost = 0;
    1946      399985 :     move16();
    1947             : 
    1948      399985 :     test();
    1949      399985 :     if ( total_brate == FRAME_NO_DATA && st_ivas->bfi != 0 ) /*  unsent  NO_DATA or stolen NO_DATA/signaling  frame  */
    1950             :     {
    1951        2457 :         speech_lost = 1; /* initial ft assumption, CNG_state decides what to do */
    1952        2457 :         move16();
    1953             :     }
    1954             : 
    1955             :     /* Do not allow decoder to enter CNG-synthesis for  any instantly  received  GOOD+LENGTH==0  frame
    1956             :                    as this frame was never transmitted, one  can not know it is good and has a a length of zero ) */
    1957      399985 :     IF( *CNG != 0 )
    1958             :     {
    1959             :         /* We were in CNG synthesis  */
    1960       18675 :         if ( curr_ft_good_sp != 0 )
    1961             :         {
    1962             :             /* only a good speech frame makes you leave CNG synthesis */
    1963         934 :             *CNG = 0;
    1964         934 :             move16();
    1965             :         }
    1966             :     }
    1967             :     ELSE
    1968             :     {
    1969             :         /* We were in SPEECH synthesis  */
    1970             :         /* only a received/detected SID frame can make the decoder enter into CNG synthsis  */
    1971      381310 :         test();
    1972      381310 :         test();
    1973      381310 :         if ( g192_sid_first || sid_update || sid_upd_bad )
    1974             :         {
    1975        1010 :             *CNG = 1;
    1976        1010 :             move16();
    1977             :         }
    1978             :     }
    1979             : 
    1980             :     /* set bfi, total_brate pair  for proper decoding  */
    1981             :     /*  handle the  G.192   _simulated_ untransmitted NO_DATA frame,  setting  for decoder  SPEECH synthesis  */
    1982      399985 :     test();
    1983      399985 :     test();
    1984      399985 :     if ( *CNG == 0 && total_brate == FRAME_NO_DATA && st_ivas->bfi == 0 )
    1985             :     {
    1986          80 :         st_ivas->bfi = 1; /*  SPEECH PLC code will now become active as in a real system */
    1987             :                           /* total_brate= 0  */
    1988          80 :         move16();
    1989             :     }
    1990             : 
    1991             :     /* handle bad/lost speech frame(and CS bad SID frame) in the decoders CNG synthesis settings pair (total_brate, bfi) */
    1992      399985 :     test();
    1993      399985 :     test();
    1994      399985 :     test();
    1995      399985 :     test();
    1996      399985 :     IF( (
    1997             :             NE_16( bfi, FRAMEMODE_FUTURE ) &&
    1998             :             ( *CNG != 0 ) && ( ( speech_bad != 0 ) || ( speech_lost != 0 ) ) ) || /* SP_BAD or SPEECH_LOST)   --> stay in CNG */
    1999             :         ( sid_upd_bad != 0 ) )                                                    /* SID_UPD_BAD              --> start CNG */
    2000             :     {
    2001        1479 :         st_ivas->bfi = 0; /* bfi=0 needed to activate CNG code */
    2002        1479 :         move16();
    2003        1479 :         total_brate = FRAME_NO_DATA;
    2004        1479 :         move32();
    2005             :     }
    2006             : 
    2007             :     /* update for next frame's G.192 file format's  odd SID_FIRST detection (primarily for AMRWBIO)  */
    2008      399985 :     test();
    2009      399985 :     *prev_ft_speech = ( ( curr_ft_good_sp != 0 ) || ( curr_ft_bad_sp != 0 ) );
    2010      399985 :     move16();
    2011             : 
    2012             :     /*   st->total brate= total_brate;   updated in a good frame below */
    2013             : 
    2014      785842 :     FOR( k = 0; k < st_ivas->nCPE; k++ )
    2015             :     {
    2016      385857 :         sts = st_ivas->hCPE[k]->hCoreCoder;
    2017     1157571 :         FOR( n = 0; n < CPE_CHANNELS; n++ )
    2018             :         {
    2019      771714 :             sts[n]->bfi = st_ivas->bfi;
    2020      771714 :             move16();
    2021             :         }
    2022             :     }
    2023             : 
    2024      718269 :     FOR( k = 0; k < st_ivas->nSCE; k++ )
    2025             :     {
    2026      318284 :         sts = st_ivas->hSCE[k]->hCoreCoder;
    2027      318284 :         sts[0]->bfi = st_ivas->bfi;
    2028      318284 :         move16();
    2029             :     }
    2030             : 
    2031      399985 :     IF( st_ivas->bfi == 0 )
    2032             :     {
    2033             :         /* select Mode 1 or Mode 2 */
    2034      391420 :         IF( EQ_32( st_ivas->ivas_format, MONO_FORMAT ) ) /* EVS mono */
    2035             :         {
    2036        3100 :             decoder_selectCodec( sts[0], total_brate, bit_stream[0] );
    2037        3100 :             st_ivas->hDecoderConfig->Opt_AMR_WB = sts[0]->Opt_AMR_WB;
    2038        3100 :             move16();
    2039             :         }
    2040             :         ELSE /* IVAS */
    2041             :         {
    2042      388320 :             st_ivas->codec_mode = MODE1;
    2043      388320 :             move16();
    2044      388320 :             st_ivas->hDecoderConfig->Opt_AMR_WB = 0;
    2045      388320 :             move16();
    2046             :         }
    2047             :     }
    2048             : 
    2049             :     /* GOOD frame */
    2050      399985 :     test();
    2051      399985 :     if ( st_ivas->bfi == 0 || EQ_16( st_ivas->bfi, FRAMEMODE_FUTURE ) )
    2052             :     {
    2053             :         /* GOOD frame - convert ITU-T G.192 words to short values */
    2054      391420 :         st_ivas->hDecoderConfig->ivas_total_brate = total_brate;
    2055      391420 :         move32();
    2056             :     }
    2057             : 
    2058      399985 :     st_ivas->bit_stream = bit_stream;
    2059             : 
    2060      399985 :     IF( EQ_32( st_ivas->ivas_format, MONO_FORMAT ) )
    2061             :     {
    2062        3100 :         ivas_set_bitstream_pointers( st_ivas );
    2063             :     }
    2064             : 
    2065      399985 :     return error;
    2066             : }
    2067             : #undef WMC_TOOL_SKIP

Generated by: LCOV version 1.14