LCOV - code coverage report
Current view: top level - lib_dec - ivas_ism_metadata_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ d25741250ae73a0fdda3bc43a164fc404c15b66c Lines: 364 376 96.8 %
Date: 2026-02-27 05:21:15 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2026 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             : #include <stdint.h>
      34             : #include "options.h"
      35             : #include "ivas_cnst.h"
      36             : #include "ivas_prot.h"
      37             : #include "ivas_rom_com.h"
      38             : #include "prot.h"
      39             : #include "ivas_stat_enc.h"
      40             : #include <math.h>
      41             : #ifdef DEBUGGING
      42             : #include "debug.h"
      43             : #endif
      44             : #include "wmc_auto.h"
      45             : 
      46             : 
      47             : /*-----------------------------------------------------------------------*
      48             :  * Local functions
      49             :  *-----------------------------------------------------------------------*/
      50             : 
      51             : static void decode_angle_indices( DEC_CORE_HANDLE st0, ISM_METADATA_ANGLE_HANDLE angle, const int16_t non_diegetic_flag, int16_t *flag_abs_azimuth );
      52             : 
      53             : static int16_t decode_radius( DEC_CORE_HANDLE st0, int16_t *last_radius_idx, int16_t *flag_abs_radius );
      54             : 
      55             : 
      56             : /*-------------------------------------------------------------------------*
      57             :  * Local constants
      58             :  *-------------------------------------------------------------------------*/
      59             : 
      60             : #define IVAS_ISM_DTX_HO_MAX 5
      61             : 
      62             : #define CNG_MD_MAX_DIFF_AZIMUTH   5
      63             : #define CNG_MD_MAX_DIFF_ELEVATION 5
      64             : 
      65             : #define MAX_BITS_ISM_METADATA ( 2 * ISM_EXTENDED_METADATA_BITS + MAX_NUM_OBJECTS * ( 1 /*number of objects*/ + ISM_METADATA_MD_FLAG_BITS + 2 * ISM_METADATA_FLAG_BITS + ISM_METADATA_IS_NDP_BITS + 1 /*abs.flag*/ + ISM_AZIMUTH_NBITS + ISM_ELEVATION_NBITS + 1 /*abs.flag*/ + ISM_RADIUS_NBITS + 1 /*abs.flag*/ + ISM_AZIMUTH_NBITS + ISM_ELEVATION_NBITS ) + 10 /* margin */ ) /* max. bit-budget of ISM metadata */
      66             : 
      67             : 
      68             : /*-------------------------------------------------------------------*
      69             :  * ism_metadata_smooth()
      70             :  *
      71             :  * Smooth the metadata evolution
      72             :  *-------------------------------------------------------------------*/
      73             : 
      74       28781 : static void ism_metadata_smooth(
      75             :     ISM_METADATA_HANDLE hIsmMeta[], /* i/o: ISM metadata handles      */
      76             :     const int32_t ism_total_brate,  /* i  : ISMs total bitrate        */
      77             :     const int16_t nchan_ism         /* i  : number of objects         */
      78             : )
      79             : {
      80             :     ISM_METADATA_HANDLE hIsmMetaData;
      81             :     int16_t ch;
      82             :     float diff;
      83             : 
      84      122887 :     for ( ch = 0; ch < nchan_ism; ch++ )
      85             :     {
      86       94106 :         hIsmMetaData = hIsmMeta[ch];
      87             : 
      88             :         /* smooth azimuth */
      89       94106 :         diff = hIsmMetaData->last_true_azimuth - hIsmMetaData->last_azimuth;
      90             : 
      91       94106 :         if ( diff > ISM_AZIMUTH_MAX )
      92             :         {
      93        4462 :             diff -= ( ISM_AZIMUTH_MAX - ISM_AZIMUTH_MIN );
      94        4462 :             hIsmMetaData->last_azimuth += ( ISM_AZIMUTH_MAX - ISM_AZIMUTH_MIN );
      95             :         }
      96       89644 :         else if ( diff < ISM_AZIMUTH_MIN )
      97             :         {
      98        1309 :             diff += ( ISM_AZIMUTH_MAX - ISM_AZIMUTH_MIN );
      99             :         }
     100             : 
     101       94106 :         if ( ism_total_brate > IVAS_SID_5k2 && fabsf( diff ) > IVAS_ISM_DTX_HO_MAX * CNG_MD_MAX_DIFF_AZIMUTH )
     102             :         {
     103             :             /* skip the smoothing */
     104             :         }
     105       85953 :         else if ( fabsf( diff ) > CNG_MD_MAX_DIFF_AZIMUTH )
     106             :         {
     107       29823 :             hIsmMetaData->azimuth = hIsmMetaData->last_azimuth + sign( diff ) * CNG_MD_MAX_DIFF_AZIMUTH;
     108             :         }
     109       56130 :         else if ( diff != 0 )
     110             :         {
     111       15472 :             hIsmMetaData->azimuth = hIsmMetaData->last_true_azimuth;
     112             :         }
     113             : 
     114       94106 :         if ( hIsmMetaData->azimuth > ISM_AZIMUTH_MAX )
     115             :         {
     116        1104 :             hIsmMetaData->azimuth -= ( ISM_AZIMUTH_MAX - ISM_AZIMUTH_MIN );
     117             :         }
     118             : 
     119             :         /* smooth elevation */
     120       94106 :         diff = hIsmMetaData->last_true_elevation - hIsmMetaData->last_elevation;
     121             : 
     122       94106 :         if ( ism_total_brate > IVAS_SID_5k2 && fabsf( diff ) > IVAS_ISM_DTX_HO_MAX * CNG_MD_MAX_DIFF_ELEVATION )
     123             :         {
     124             :             /* skip the smoothing */
     125             :         }
     126       93921 :         else if ( fabsf( diff ) > CNG_MD_MAX_DIFF_ELEVATION )
     127             :         {
     128       11594 :             hIsmMetaData->elevation = hIsmMetaData->last_elevation + sign( diff ) * CNG_MD_MAX_DIFF_ELEVATION;
     129             :         }
     130             :     }
     131             : 
     132       28781 :     return;
     133             : }
     134             : 
     135             : 
     136             : /*-------------------------------------------------------------------------*
     137             :  * ivas_ism_metadata_dec()
     138             :  *
     139             :  * decode and dequantize ISM metadata
     140             :  *-------------------------------------------------------------------------*/
     141             : 
     142     2376889 : ivas_error ivas_ism_metadata_dec(
     143             :     const int32_t ism_total_brate,           /* i  : ISM  total bitrate                     */
     144             :     const int16_t nchan_ism,                 /* i  : number of ISM channels                 */
     145             :     int16_t *nchan_transport,                /* o  : number of transport channels           */
     146             :     ISM_METADATA_HANDLE hIsmMeta[],          /* i/o: ISM metadata handles                   */
     147             :     SCE_DEC_HANDLE hSCE[],                   /* i/o: SCE decoder handles                    */
     148             :     const int16_t bfi,                       /* i  : bfi flag                               */
     149             :     int16_t nb_bits_metadata[],              /* o  : number of metadata bits                */
     150             :     ISM_MODE ism_mode,                       /* i  : ISM mode                               */
     151             :     ISM_DTX_DATA_DEC hISMDTX,                /* i/o: ISM DTX structure                      */
     152             :     const PARAM_ISM_CONFIG_HANDLE hParamIsm, /* i  : Param ISM Config Handle                */
     153             :     int16_t *ism_extmeta_active,             /* i/o: Extended metadata active in renderer   */
     154             :     int16_t *ism_extmeta_cnt,                /* i/o: Number of change frames observed       */
     155             :     DEC_CORE_HANDLE st0                      /* i  : core-coder handle                      */
     156             : )
     157             : {
     158     2376889 :     int16_t ch, nb_bits_start = 0, last_bit_pos;
     159             :     int16_t idx_radius;
     160             :     int32_t element_brate[MAX_NUM_OBJECTS], total_brate[MAX_NUM_OBJECTS];
     161             :     int16_t ism_extmeta_bitstream;
     162             :     int16_t non_diegetic_flag_global;
     163             :     float yaw, pitch, radius;
     164             :     int16_t flag_abs_radius;
     165             :     int16_t flag_abs_orientation;
     166             :     int16_t flag_abs_position;
     167             :     int16_t idx_angle1;
     168             :     int16_t idx_angle2;
     169             :     int16_t next_bit_pos_orig;
     170             :     uint16_t i, bstr_meta[MAX_BITS_ISM_METADATA], *bstr_orig;
     171             :     ISM_METADATA_HANDLE hIsmMetaData;
     172             :     int16_t nchan_transport_prev, ism_metadata_flag_global;
     173             :     int16_t null_metadata_flag[MAX_NUM_OBJECTS];
     174             :     int16_t lowrate_metadata_flag[MAX_NUM_OBJECTS];
     175             :     int16_t ism_imp[MAX_NUM_OBJECTS];
     176             :     int16_t nbands, nblocks;
     177             :     int16_t md_diff_flag[MAX_NUM_OBJECTS];
     178             :     ivas_error error;
     179             : 
     180     2376889 :     push_wmops( "ism_meta_dec" );
     181             : 
     182             :     /* initialization */
     183     2376889 :     ism_metadata_flag_global = 0;
     184     2376889 :     nchan_transport_prev = *nchan_transport;
     185             : 
     186     2376889 :     last_bit_pos = (int16_t) ( ( ism_total_brate / FRAMES_PER_SEC ) - 1 );
     187     2376889 :     bstr_orig = st0->bit_stream;
     188     2376889 :     next_bit_pos_orig = st0->next_bit_pos;
     189     2376889 :     st0->next_bit_pos = 0;
     190     2376889 :     ism_extmeta_bitstream = 0;
     191     2376889 :     non_diegetic_flag_global = 0;
     192     2376889 :     set_s( null_metadata_flag, 0, nchan_ism );
     193     2376889 :     set_s( lowrate_metadata_flag, 0, nchan_ism );
     194             : 
     195             :     /* reverse the bitstream for easier reading of indices */
     196   430085293 :     for ( i = 0; i < min( MAX_BITS_ISM_METADATA, last_bit_pos ); i++ )
     197             :     {
     198   427708404 :         bstr_meta[i] = st0->bit_stream[last_bit_pos - i];
     199             :     }
     200     2376889 :     st0->bit_stream = bstr_meta;
     201     2376889 :     st0->total_brate = ism_total_brate; /* needed for BER detection in get_next_indice() */
     202             : 
     203     2376889 :     if ( !bfi )
     204             :     {
     205             :         /*----------------------------------------------------------------*
     206             :          * Read ISM common signaling
     207             :          *----------------------------------------------------------------*/
     208             : 
     209     2183221 :         if ( ism_mode == ISM_SBA_MODE_DISC )
     210             :         {
     211             :             /* number of objects was read in ivas_dec_setup() */
     212      693300 :             st0->next_bit_pos += NO_BITS_MASA_ISM_NO_OBJ;
     213             :         }
     214     1489921 :         else if ( ism_mode != ISM_MASA_MODE_DISC && ism_mode != ISM_MASA_MODE_MASA_ONE_OBJ )
     215             :         {
     216             :             /* number of objects was read in ivas_dec_setup() */
     217     1077045 :             st0->next_bit_pos += nchan_ism;
     218             : 
     219     1077045 :             ism_mode = ivas_ism_mode_select( nchan_ism, ism_total_brate );
     220             :         }
     221             : 
     222     2183221 :         if ( ism_mode == ISM_MODE_PARAM )
     223             :         {
     224      140993 :             *nchan_transport = MAX_PARAM_ISM_WAVE;
     225             :         }
     226     2042228 :         else if ( ism_mode == ISM_MODE_DISC )
     227             :         {
     228      936052 :             *nchan_transport = nchan_ism;
     229             :         }
     230             : 
     231     2183221 :         if ( *nchan_transport != nchan_transport_prev )
     232             :         {
     233           0 :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "wrong number of objects signalled!" );
     234             :         }
     235             : 
     236             :         /* read extended metadata presence flag */
     237     2183221 :         if ( ( ism_mode == ISM_MODE_DISC || ism_mode == ISM_SBA_MODE_DISC ) && ism_total_brate >= ISM_EXTENDED_METADATA_BRATE )
     238             :         {
     239     1392382 :             ism_extmeta_bitstream = get_next_indice( st0, ISM_EXTENDED_METADATA_BITS );
     240             : 
     241             :             /* read global non-diegetic object flag */
     242     1392382 :             if ( ism_extmeta_bitstream )
     243             :             {
     244      265704 :                 non_diegetic_flag_global = get_next_indice( st0, ISM_METADATA_IS_NDP_BITS );
     245             :             }
     246             :         }
     247             : 
     248             :         /* Apply hysteresis in case rate switching causes fluctuation in presence of extended metadata */
     249     2183221 :         if ( *ism_extmeta_active == -1 || *ism_extmeta_active == ism_extmeta_bitstream ) /* If first frame or bitstream matches internal state */
     250             :         {
     251     2178910 :             *ism_extmeta_active = ism_extmeta_bitstream;
     252     2178910 :             *ism_extmeta_cnt = 0;
     253             :         }
     254             :         else
     255             :         {
     256        4311 :             ( *ism_extmeta_cnt )++;
     257        4311 :             if ( *ism_extmeta_cnt == ISM_METADATA_RS_MAX_FRAMES ) /* ISM_METADATA_RS_MAX_FRAMES change frames observed - update state */
     258             :             {
     259         861 :                 *ism_extmeta_active = ism_extmeta_bitstream;
     260         861 :                 *ism_extmeta_cnt = 0;
     261             :             }
     262             :         }
     263             : 
     264             :         /* Read ISM metadata flags (one per object) */
     265     7501940 :         for ( ch = 0; ch < *nchan_transport; ch++ )
     266             :         {
     267     5318719 :             if ( ism_mode == ISM_SBA_MODE_DISC )
     268             :             {
     269     1744627 :                 ism_imp[ch] = get_next_indice( st0, 1 );
     270             :             }
     271     3574092 :             else if ( ism_mode == ISM_MASA_MODE_DISC || ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ )
     272             :             {
     273             :                 /* ISM importance flag is already read in ivas_masa_decode() */
     274      836230 :                 ism_imp[ch] = hIsmMeta[ch]->ism_imp;
     275             :             }
     276             :             else
     277             :             {
     278     2737862 :                 ism_imp[ch] = get_next_indice( st0, ISM_METADATA_FLAG_BITS );
     279             :             }
     280             : 
     281     5318719 :             if ( ism_imp[ch] > ISM_NO_META )
     282             :             {
     283     5278598 :                 hIsmMeta[ch]->ism_metadata_flag = 1;
     284             :             }
     285             :             else
     286             :             {
     287       40121 :                 hIsmMeta[ch]->ism_metadata_flag = 0;
     288             :             }
     289             : 
     290     5318719 :             ism_metadata_flag_global |= hIsmMeta[ch]->ism_metadata_flag;
     291             :         }
     292             : 
     293     2403016 :         for ( ; ch < nchan_ism; ch++ )
     294             :         {
     295      219795 :             hIsmMeta[ch]->ism_metadata_flag = 1;
     296      219795 :             ism_metadata_flag_global |= hIsmMeta[ch]->ism_metadata_flag;
     297             :         }
     298             : 
     299             :         /* read ISM_NO_META class signalling */
     300     2183221 :         if ( ism_mode == ISM_MODE_DISC || ism_mode == ISM_SBA_MODE_DISC )
     301             :         {
     302     5829855 :             for ( ch = 0; ch < *nchan_transport; ch++ )
     303             :             {
     304     4200503 :                 if ( ism_imp[ch] == ISM_NO_META )
     305             :                 {
     306             :                     /* low-rate ISM_NO_META frame */
     307       38939 :                     if ( ism_mode == ISM_MASA_MODE_DISC || ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ )
     308             :                     {
     309           0 :                         null_metadata_flag[ch] = hIsmMeta[ch]->ism_md_null_flag;
     310             :                     }
     311             :                     else
     312             :                     {
     313       38939 :                         null_metadata_flag[ch] = get_next_indice( st0, ISM_METADATA_INACTIVE_FLAG_BITS );
     314             :                     }
     315             :                 }
     316             :             }
     317             : 
     318     5829855 :             for ( ch = 0; ch < *nchan_transport; ch++ )
     319             :             {
     320     4200503 :                 if ( ism_imp[ch] == ISM_NO_META )
     321             :                 {
     322       38939 :                     if ( ism_mode == ISM_MASA_MODE_DISC || ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ )
     323             :                     {
     324           0 :                         lowrate_metadata_flag[ch] = hIsmMeta[ch]->ism_md_lowrate_flag;
     325             : 
     326           0 :                         if ( null_metadata_flag[ch] == 0 )
     327             :                         {
     328           0 :                             ism_metadata_flag_global |= lowrate_metadata_flag[ch];
     329             :                         }
     330             :                     }
     331       38939 :                     else if ( ism_mode != ISM_SBA_MODE_DISC )
     332             :                     {
     333       38939 :                         if ( null_metadata_flag[ch] )
     334             :                         {
     335             :                             /* read the true ISM class */
     336       14010 :                             ism_imp[ch] = get_next_indice( st0, ISM_METADATA_FLAG_BITS );
     337             :                         }
     338             :                         else
     339             :                         {
     340             :                             /* read presence of MD in low-rate ISM_NO_META frame flag */
     341       24929 :                             lowrate_metadata_flag[ch] = get_next_indice( st0, ISM_METADATA_INACTIVE_FLAG_BITS );
     342             : 
     343       24929 :                             ism_metadata_flag_global |= lowrate_metadata_flag[ch];
     344             :                         }
     345             :                     }
     346             :                 }
     347             :             }
     348             :         }
     349             : 
     350     2183221 :         if ( ism_metadata_flag_global )
     351             :         {
     352             :             /*----------------------------------------------------------------*
     353             :              * Metadata decoding and dequantization, loop over all objects
     354             :              *----------------------------------------------------------------*/
     355             : 
     356     2182243 :             int16_t total_bits_metadata = 0, bits_metadata_ism = 0;
     357             :             int16_t nb_bits_objcod_read;
     358             : 
     359     2182243 :             if ( ism_mode == ISM_MODE_PARAM )
     360             :             {
     361      140993 :                 nb_bits_start = st0->next_bit_pos;
     362             :             }
     363             : 
     364     7719725 :             for ( ch = 0; ch < nchan_ism; ch++ )
     365             :             {
     366     5537482 :                 hIsmMetaData = hIsmMeta[ch];
     367     5537482 :                 if ( ism_mode == ISM_MODE_DISC || ism_mode == ISM_SBA_MODE_DISC || ism_mode == ISM_MASA_MODE_DISC || ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ )
     368             :                 {
     369     5035701 :                     nb_bits_start = st0->next_bit_pos;
     370             :                 }
     371     5537482 :                 flag_abs_position = 0;
     372     5537482 :                 flag_abs_orientation = 0;
     373     5537482 :                 flag_abs_radius = 0;
     374             : 
     375     5537482 :                 if ( hIsmMeta[ch]->ism_metadata_flag || lowrate_metadata_flag[ch] )
     376             :                 {
     377     5521482 :                     if ( non_diegetic_flag_global )
     378             :                     {
     379             :                         /* read non-diegetic flag for each object */
     380      644241 :                         hIsmMetaData->non_diegetic_flag = get_next_indice( st0, ISM_METADATA_IS_NDP_BITS );
     381             :                     }
     382             :                     else
     383             :                     {
     384     4877241 :                         hIsmMetaData->non_diegetic_flag = 0;
     385             :                     }
     386             : 
     387     5521482 :                     if ( hIsmMetaData->non_diegetic_flag )
     388             :                     {
     389             :                         /* Panning gain decoding */
     390      418353 :                         decode_angle_indices( st0, &( hIsmMetaData->position_angle ), hIsmMetaData->non_diegetic_flag, &flag_abs_position );
     391      418353 :                         idx_angle1 = hIsmMetaData->position_angle.last_angle1_idx;
     392      418353 :                         idx_angle2 = 1 << ( ISM_ELEVATION_NBITS - 1 );
     393             : 
     394             :                         /* Panning gain dequantization */
     395      418353 :                         hIsmMetaData->azimuth = ism_dequant_meta( idx_angle1, ism_azimuth_borders, ISM_Q_STEP, ISM_Q_STEP_BORDER, 1 << ISM_AZIMUTH_NBITS );
     396      418353 :                         hIsmMetaData->elevation = 0.0f;
     397             : 
     398             :                         /* save for smoothing metadata evolution */
     399      418353 :                         hIsmMetaData->last_true_azimuth = hIsmMetaData->azimuth;
     400      418353 :                         hIsmMetaData->last_true_elevation = hIsmMetaData->elevation;
     401             :                     }
     402             :                     else
     403             :                     {
     404     5103129 :                         decode_angle_indices( st0, &( hIsmMetaData->position_angle ), hIsmMeta[ch]->non_diegetic_flag, &flag_abs_position );
     405     5103129 :                         idx_angle1 = hIsmMetaData->position_angle.last_angle1_idx;
     406     5103129 :                         idx_angle2 = hIsmMetaData->position_angle.last_angle2_idx;
     407             : 
     408             :                         /* Azimuth/Elevation dequantization */
     409     5103129 :                         if ( ism_mode == ISM_MODE_PARAM )
     410             :                         {
     411      501781 :                             hParamIsm->azi_index[ch] = idx_angle1;
     412      501781 :                             hParamIsm->ele_index[ch] = idx_angle2;
     413             :                         }
     414             :                         else /* ISM_MODE_DISC */
     415             :                         {
     416     4601348 :                             hIsmMetaData->azimuth = ism_dequant_meta( idx_angle1, ism_azimuth_borders, ISM_Q_STEP, ISM_Q_STEP_BORDER, 1 << ISM_AZIMUTH_NBITS );
     417     4601348 :                             hIsmMetaData->elevation = ism_dequant_meta( idx_angle2, ism_elevation_borders, ISM_Q_STEP, ISM_Q_STEP_BORDER, 1 << ISM_ELEVATION_NBITS );
     418             : 
     419             :                             /* radius/raw/pitch dequantization */
     420     4601348 :                             if ( ism_extmeta_bitstream )
     421             :                             {
     422      329613 :                                 decode_angle_indices( st0, &( hIsmMetaData->orientation_angle ), hIsmMeta[ch]->non_diegetic_flag, &flag_abs_orientation );
     423      329613 :                                 idx_angle1 = hIsmMetaData->orientation_angle.last_angle1_idx;
     424      329613 :                                 idx_angle2 = hIsmMetaData->orientation_angle.last_angle2_idx;
     425      329613 :                                 yaw = ism_dequant_meta( idx_angle1, ism_azimuth_borders, ISM_Q_STEP, ISM_Q_STEP_BORDER, 1 << ISM_AZIMUTH_NBITS );
     426      329613 :                                 pitch = ism_dequant_meta( idx_angle2, ism_elevation_borders, ISM_Q_STEP, ISM_Q_STEP_BORDER, 1 << ISM_ELEVATION_NBITS );
     427             : 
     428      329613 :                                 idx_radius = decode_radius( st0, &hIsmMetaData->last_radius_idx, &flag_abs_radius );
     429      329613 :                                 radius = usdequant( idx_radius, ISM_RADIUS_MIN, ISM_RADIUS_DELTA );
     430      329613 :                                 if ( *ism_extmeta_active == 1 )
     431             :                                 {
     432      327405 :                                     hIsmMetaData->yaw = yaw;
     433      327405 :                                     hIsmMetaData->pitch = pitch;
     434      327405 :                                     hIsmMetaData->radius = radius;
     435             :                                 }
     436             :                             }
     437             :                             else
     438             :                             {
     439     4271735 :                                 if ( *ism_extmeta_active == 0 )
     440             :                                 {
     441     4270403 :                                     hIsmMetaData->yaw = 0.0f;
     442     4270403 :                                     hIsmMetaData->pitch = 0.0f;
     443     4270403 :                                     hIsmMetaData->radius = 1.0f;
     444             :                                 }
     445             :                             }
     446             :                         }
     447             :                         /* save for smoothing metadata evolution */
     448     5103129 :                         hIsmMetaData->last_true_azimuth = hIsmMetaData->azimuth;
     449     5103129 :                         hIsmMetaData->last_true_elevation = hIsmMetaData->elevation;
     450             :                     }
     451             :                 }
     452             : 
     453             :                 /* save number of metadata bits read */
     454     5537482 :                 if ( ism_mode == ISM_MODE_DISC || ism_mode == ISM_SBA_MODE_DISC || ism_mode == ISM_MASA_MODE_DISC || ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ )
     455             :                 {
     456     5035701 :                     nb_bits_metadata[ch] = st0->next_bit_pos - nb_bits_start;
     457             :                 }
     458             :             }
     459             : 
     460     2182243 :             if ( ism_mode == ISM_MODE_PARAM )
     461             :             {
     462      140993 :                 hParamIsm->flag_noisy_speech = get_next_indice( st0, 1 );
     463             : 
     464             :                 /* Loop over multiwave to read the object indices from bitstream */
     465      422979 :                 for ( ch = 0; ch < MAX_PARAM_ISM_WAVE; ch++ )
     466             :                 {
     467     3383832 :                     for ( nbands = 0; nbands < hParamIsm->nbands; nbands++ )
     468             :                     {
     469     6203692 :                         for ( nblocks = 0; nblocks < hParamIsm->nblocks[nbands]; nblocks++ )
     470             :                         {
     471     3101846 :                             hParamIsm->obj_indices[nbands][nblocks][ch] = get_next_indice( st0, PARAM_ISM_OBJ_IND_NBITS );
     472             :                         }
     473             :                     }
     474             :                 }
     475             : 
     476             :                 /* Loop over all bands to read power ratio's from bitstream */
     477     1691916 :                 for ( nbands = 0; nbands < hParamIsm->nbands; nbands++ )
     478             :                 {
     479     3101846 :                     for ( nblocks = 0; nblocks < hParamIsm->nblocks[nbands]; nblocks++ )
     480             :                     {
     481     1550923 :                         hParamIsm->power_ratios_idx[nbands][nblocks] = get_next_indice( st0, PARAM_ISM_POW_RATIO_NBITS );
     482             :                     }
     483             :                 }
     484             : 
     485             :                 /* save number of metadata bits read */
     486      140993 :                 total_bits_metadata = st0->next_bit_pos - nb_bits_start;
     487             : 
     488             :                 /* bits per ISM*/
     489      140993 :                 bits_metadata_ism = (int16_t) ( total_bits_metadata / *nchan_transport );
     490             : 
     491      140993 :                 nb_bits_objcod_read = 0;
     492      422979 :                 for ( ch = 0; ch < *nchan_transport; ch++ )
     493             :                 {
     494      281986 :                     if ( ch == *nchan_transport - 1 )
     495             :                     {
     496      140993 :                         nb_bits_metadata[ch] = total_bits_metadata - nb_bits_objcod_read;
     497             :                     }
     498             :                     else
     499             :                     {
     500      140993 :                         nb_bits_metadata[ch] = bits_metadata_ism;
     501      140993 :                         nb_bits_objcod_read += bits_metadata_ism;
     502             :                     }
     503             :                 }
     504             :             }
     505             :         }
     506             :         else
     507             :         {
     508         978 :             set_s( nb_bits_metadata, 0, *nchan_transport );
     509             :         }
     510             :     }
     511             :     else /* BFI */
     512             :     {
     513      669710 :         for ( ch = 0; ch < nchan_ism; ch++ )
     514             :         {
     515      476042 :             hIsmMeta[ch]->last_ism_metadata_flag = hIsmMeta[ch]->ism_metadata_flag;
     516             :         }
     517             : 
     518      193668 :         set_s( nb_bits_metadata, 0, *nchan_transport );
     519             : 
     520      193668 :         if ( ism_mode == ISM_MODE_PARAM )
     521             :         {
     522       33729 :             for ( ch = 0; ch < nchan_ism; ch++ )
     523             :             {
     524       26234 :                 hParamIsm->azi_index[ch] = hParamIsm->azi_index[ch] + hParamIsm->last_az_sgn[ch] * hParamIsm->last_az_diff[ch];
     525       26234 :                 hParamIsm->ele_index[ch] = hParamIsm->ele_index[ch] + hParamIsm->last_el_sgn[ch] * hParamIsm->last_el_diff[ch];
     526       26234 :                 hIsmMeta[ch]->position_angle.last_angle1_idx = hParamIsm->azi_index[ch];
     527       26234 :                 hIsmMeta[ch]->position_angle.last_angle2_idx = hParamIsm->ele_index[ch];
     528             :             }
     529             :         }
     530             :     }
     531             : 
     532     2376889 :     if ( hISMDTX.ism_dtx_hangover_cnt < IVAS_ISM_DTX_HO_MAX )
     533             :     {
     534       21428 :         ism_metadata_smooth( hIsmMeta, ism_total_brate, nchan_ism );
     535       21428 :         hISMDTX.ism_dtx_hangover_cnt += 1;
     536             :     }
     537             : 
     538     2376889 :     if ( ism_mode == ISM_SBA_MODE_DISC )
     539             :     {
     540             :         /* set the bitstream pointer to its original position */
     541      764386 :         nb_bits_metadata[0] = st0->next_bit_pos;
     542      764386 :         st0->bit_stream = bstr_orig;
     543      764386 :         st0->next_bit_pos = next_bit_pos_orig;
     544             : 
     545             :         /* updates*/
     546      764386 :         set_s( md_diff_flag, 1, nchan_ism );
     547             : 
     548      764386 :         update_last_metadata( nchan_ism, hIsmMeta, md_diff_flag );
     549      764386 :         pop_wmops();
     550      764386 :         return IVAS_ERR_OK;
     551             :     }
     552             : 
     553     1612503 :     if ( ism_mode == ISM_MASA_MODE_DISC || ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ )
     554             :     {
     555      451613 :         ism_metadata_flag_global = 1;
     556             :     }
     557             : 
     558             :     /*----------------------------------------------------------------*
     559             :      * Configuration and decision about bitrates per channel
     560             :      *----------------------------------------------------------------*/
     561             : 
     562     1612503 :     if ( !bfi )
     563             :     {
     564     1489921 :         int16_t masa_ism_flag = 0;
     565     1489921 :         if ( ism_mode == ISM_MASA_MODE_DISC || ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ )
     566             :         {
     567      412876 :             masa_ism_flag = 1;
     568             : 
     569     1249106 :             for ( ch = 0; ch < *nchan_transport; ch++ )
     570             :             {
     571      836230 :                 element_brate[ch] = hSCE[ch]->element_brate;
     572             :             }
     573             :         }
     574             : 
     575     1489921 :         if ( ( error = ivas_ism_config( ism_total_brate, *nchan_transport, nchan_ism, hIsmMeta, ism_extmeta_bitstream, null_metadata_flag, ism_imp, element_brate, total_brate, nb_bits_metadata, masa_ism_flag ) ) != IVAS_ERR_OK )
     576             :         {
     577           0 :             return error;
     578             :         }
     579             : 
     580     5064013 :         for ( ch = 0; ch < *nchan_transport; ch++ )
     581             :         {
     582     3574092 :             hIsmMeta[ch]->last_ism_metadata_flag = hIsmMeta[ch]->ism_metadata_flag;
     583             : 
     584     3574092 :             hSCE[ch]->hCoreCoder[0]->low_rate_mode = 0;
     585     3574092 :             if ( ism_mode == ISM_MODE_DISC || ism_mode == ISM_MASA_MODE_DISC || ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ )
     586             :             {
     587     3292106 :                 if ( ism_imp[ch] == ISM_NO_META && ( ( total_brate[ch] < ACELP_8k00 && element_brate[ch] < SCE_CORE_16k_LOW_LIMIT ) ||
     588       24995 :                                                      ( total_brate[ch] <= ACELP_16k_LOW_LIMIT && element_brate[ch] >= SCE_CORE_16k_LOW_LIMIT ) ) )
     589             :                 {
     590       27090 :                     hSCE[ch]->hCoreCoder[0]->low_rate_mode = 1;
     591             :                 }
     592             :             }
     593             : 
     594     3574092 :             if ( ism_mode != ISM_MASA_MODE_DISC && ism_mode != ISM_MASA_MODE_MASA_ONE_OBJ )
     595             :             {
     596     2737862 :                 hSCE[ch]->element_brate = element_brate[ch];
     597             :             }
     598     3574092 :             hSCE[ch]->hCoreCoder[0]->total_brate = total_brate[ch];
     599             :         }
     600             : 
     601     1709716 :         for ( ; ch < nchan_ism; ch++ )
     602             :         {
     603      219795 :             hIsmMeta[ch]->last_ism_metadata_flag = hIsmMeta[ch]->ism_metadata_flag;
     604             :         }
     605             :     }
     606             :     else
     607             :     {
     608      413637 :         for ( ch = 0; ch < *nchan_transport; ch++ )
     609             :         {
     610      291055 :             hSCE[ch]->element_brate = hSCE[ch]->last_element_brate;
     611      291055 :             hSCE[ch]->hCoreCoder[0]->total_brate = hSCE[ch]->hCoreCoder[0]->last_total_brate;
     612             :         }
     613             :     }
     614             : 
     615             :     /*----------------------------------------------------------------*
     616             :      * Set bitsream pointers
     617             :      *----------------------------------------------------------------*/
     618             : 
     619             :     /* set the bitstream pointer to its original position */
     620     1612503 :     st0->bit_stream = bstr_orig;
     621     1612503 :     st0->next_bit_pos = next_bit_pos_orig;
     622             : 
     623             :     /* set bitstream pointers for each ISM */
     624     3865147 :     for ( ch = 1; ch < *nchan_transport; ch++ )
     625             :     {
     626     2252644 :         hSCE[ch]->hCoreCoder[0]->bit_stream = hSCE[ch - 1]->hCoreCoder[0]->bit_stream + ( hSCE[ch - 1]->hCoreCoder[0]->total_brate / FRAMES_PER_SEC );
     627             :     }
     628             : 
     629             :     /*----------------------------------------------------------------*
     630             :      * Updates
     631             :      *----------------------------------------------------------------*/
     632             : 
     633     1612503 :     set_s( md_diff_flag, 1, nchan_ism );
     634             : 
     635     1612503 :     update_last_metadata( nchan_ism, hIsmMeta, md_diff_flag );
     636             : 
     637     5477650 :     for ( ch = 0; ch < *nchan_transport; ch++ )
     638             :     {
     639     3865147 :         hSCE[ch]->hCoreCoder[0]->cng_ism_flag = 0;
     640             :     }
     641             : 
     642     1612503 :     pop_wmops();
     643             : 
     644     1612503 :     return IVAS_ERR_OK;
     645             : }
     646             : 
     647             : 
     648             : /*-------------------------------------------------------------------*
     649             :  * ivas_ism_reset_metadata_handle_dec()
     650             :  *
     651             :  * Reset ISM decoder metadata handle
     652             :  *-------------------------------------------------------------------*/
     653             : 
     654      176338 : void ivas_ism_reset_metadata_handle_dec(
     655             :     ISM_METADATA_HANDLE hIsmMeta /* i/o: ISM metadata handle     */
     656             : )
     657             : {
     658      176338 :     hIsmMeta->last_ism_metadata_flag = 0;
     659      176338 :     hIsmMeta->position_angle.last_angle1_idx = 0;
     660      176338 :     hIsmMeta->position_angle.last_angle2_idx = 1 << ( ISM_ELEVATION_NBITS - 1 );
     661      176338 :     hIsmMeta->orientation_angle.last_angle1_idx = 0;
     662      176338 :     hIsmMeta->orientation_angle.last_angle2_idx = 1 << ( ISM_ELEVATION_NBITS - 1 );
     663      176338 :     hIsmMeta->last_radius_idx = 8; /* Init to radius 1.0 */
     664             : 
     665      176338 :     hIsmMeta->last_true_azimuth = 0;
     666      176338 :     hIsmMeta->last_true_elevation = 0;
     667      176338 :     hIsmMeta->last_azimuth = 0;
     668      176338 :     hIsmMeta->last_elevation = 0;
     669             : 
     670      176338 :     hIsmMeta->ism_imp = -1;
     671      176338 :     hIsmMeta->ism_md_null_flag = 0;
     672      176338 :     hIsmMeta->ism_md_lowrate_flag = 0;
     673             : 
     674      176338 :     ivas_ism_reset_metadata( hIsmMeta );
     675             : 
     676      176338 :     return;
     677             : }
     678             : 
     679             : 
     680             : /*-------------------------------------------------------------------------
     681             :  * ivas_ism_metadata_dec_create()
     682             :  *
     683             :  * Create, allocate, initialize and configure IVAS decoder ISM metadata handles
     684             :  *-------------------------------------------------------------------------*/
     685             : 
     686       40045 : ivas_error ivas_ism_metadata_dec_create(
     687             :     Decoder_Struct *st_ivas,    /* i/o: IVAS decoder structure              */
     688             :     const int16_t n_ISms,       /* i  : number of separately coded objects  */
     689             :     int32_t element_brate_tmp[] /* o  : element bitrate per object          */
     690             : )
     691             : {
     692             :     int16_t ch;
     693             :     ivas_error error;
     694             : 
     695             :     /* allocate ISM metadata handles */
     696      141929 :     for ( ch = 0; ch < n_ISms; ch++ )
     697             :     {
     698      101884 :         if ( st_ivas->hIsmMetaData[ch] == NULL ) /* note: the handle can be allocated in OMASA bitrate switching from ISM_MASA_MODE_xxx_ONE_OBJ to ISM_MASA_MODE_DISC mode for 'ch==0' */
     699             :         {
     700      101884 :             if ( ( st_ivas->hIsmMetaData[ch] = (ISM_METADATA_HANDLE) malloc( sizeof( ISM_METADATA_FRAME ) ) ) == NULL )
     701             :             {
     702           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for ISM MetaData\n" ) );
     703             :             }
     704             :         }
     705             : 
     706      101884 :         ivas_ism_reset_metadata_handle_dec( st_ivas->hIsmMetaData[ch] );
     707             :     }
     708             : 
     709             :     /* sanity freeing - it can happen only in reconfiguration when a smaller number of handles than before is requested */
     710       98341 :     for ( ; ch < MAX_NUM_OBJECTS; ch++ )
     711             :     {
     712       58296 :         if ( st_ivas->hIsmMetaData[ch] != NULL )
     713             :         {
     714           0 :             free( st_ivas->hIsmMetaData[ch] );
     715           0 :             st_ivas->hIsmMetaData[ch] = NULL;
     716             :         }
     717             :     }
     718             : 
     719       40045 :     if ( element_brate_tmp != NULL )
     720             :     {
     721       37233 :         if ( ( error = ivas_ism_config( st_ivas->hDecoderConfig->ivas_total_brate, n_ISms, n_ISms, NULL, 0, NULL, NULL, element_brate_tmp, NULL, NULL, 0 ) ) != IVAS_ERR_OK )
     722             :         {
     723           0 :             return error;
     724             :         }
     725             :     }
     726             : 
     727       40045 :     st_ivas->hISMDTX.ism_dtx_hangover_cnt = IVAS_ISM_DTX_HO_MAX;
     728             : 
     729       40045 :     return IVAS_ERR_OK;
     730             : }
     731             : 
     732             : 
     733             : /*-------------------------------------------------------------------------
     734             :  * decode_angle_indices()
     735             :  *
     736             :  * Decoding of a position/orientation angle
     737             :  *-------------------------------------------------------------------------*/
     738             : 
     739     5851095 : static void decode_angle_indices(
     740             :     DEC_CORE_HANDLE st0,             /* i/o: bitstream handle                */
     741             :     ISM_METADATA_ANGLE_HANDLE angle, /* i/o: angle handle                    */
     742             :     const int16_t non_diegetic_flag, /* i  : Non diegetic flag               */
     743             :     int16_t *flag_abs_angle1         /* o  : Azimuth/yaw encoding mode       */
     744             : )
     745             : {
     746             :     int16_t idx_angle1, nbits_diff_angle1, diff, sgn;
     747             :     int16_t idx_angle2, nbits_diff_angle2;
     748             : 
     749             :     /*----------------------------------------------------------------*
     750             :      * Azimuth/yaw decoding and dequantization
     751             :      *----------------------------------------------------------------*/
     752             : 
     753             :     /* Decode azimuth/yaw index */
     754     5851095 :     if ( get_next_indice( st0, 1 ) == 1 ) /* azimuth_abs_flag */
     755             :     {
     756     1774923 :         idx_angle1 = get_next_indice( st0, ISM_AZIMUTH_NBITS );
     757     1774923 :         *flag_abs_angle1 = 1;
     758             :     }
     759             :     else
     760             :     {
     761     4076172 :         diff = 0;
     762     4076172 :         sgn = 1;
     763             : 
     764     4076172 :         if ( get_next_indice( st0, 1 ) == 0 )
     765             :         {
     766     1578959 :             nbits_diff_angle1 = 1;
     767             :         }
     768             :         else
     769             :         {
     770     2497213 :             nbits_diff_angle1 = 1;
     771             : 
     772     2497213 :             if ( get_next_indice( st0, 1 ) == 1 ) /* negative sign */
     773             :             {
     774      412773 :                 sgn = -1;
     775             :             }
     776             : 
     777     2497213 :             nbits_diff_angle1++;
     778             : 
     779             :             /* read until the stop bit */
     780     6916285 :             while ( ( nbits_diff_angle1 < ISM_AZIMUTH_NBITS - 1 ) && ( get_next_indice( st0, 1 ) == 1 ) )
     781             :             {
     782     4419072 :                 diff++;
     783     4419072 :                 nbits_diff_angle1++;
     784             :             }
     785             : 
     786     2497213 :             if ( nbits_diff_angle1 < ISM_AZIMUTH_NBITS - 1 )
     787             :             {
     788             :                 /* count stop bit */
     789     2399432 :                 nbits_diff_angle1++;
     790             :             }
     791             :         }
     792             : 
     793     4076172 :         idx_angle1 = angle->last_angle1_idx + sgn * diff;
     794             :     }
     795             : 
     796             :     /* azimuth/yaw is on a circle - check for diff coding for -180° -> 180° and vice versa changes */
     797     5851095 :     if ( idx_angle1 > ( 1 << ISM_AZIMUTH_NBITS ) - 1 )
     798             :     {
     799         437 :         idx_angle1 -= ( 1 << ISM_AZIMUTH_NBITS ) - 1; /* +180° -> -180° */
     800             :     }
     801     5850658 :     else if ( idx_angle1 < 0 )
     802             :     {
     803        9797 :         idx_angle1 += ( 1 << ISM_AZIMUTH_NBITS ) - 1; /* -180° -> +180° */
     804             :     }
     805             : 
     806             :     /* +180° == -180° */
     807     5851095 :     if ( idx_angle1 == ( 1 << ISM_AZIMUTH_NBITS ) - 1 )
     808             :     {
     809       80167 :         idx_angle1 = 0;
     810             :     }
     811             : 
     812             :     /* sanity check in case of FER or BER */
     813     5851095 :     if ( idx_angle1 < 0 || idx_angle1 > ( 1 << ISM_AZIMUTH_NBITS ) - 1 )
     814             :     {
     815           0 :         idx_angle1 = angle->last_angle1_idx;
     816             :     }
     817             : 
     818             :     /*----------------------------------------------------------------*
     819             :      * Elevation/pitch decoding and dequantization
     820             :      *----------------------------------------------------------------*/
     821             : 
     822     5851095 :     if ( non_diegetic_flag == 0 )
     823             :     {
     824             :         /* Decode elevation/pitch index */
     825     5432742 :         if ( *flag_abs_angle1 == 0 && get_next_indice( st0, 1 ) == 1 ) /* elevation_abs_flag */
     826             :         {
     827      498659 :             idx_angle2 = get_next_indice( st0, ISM_ELEVATION_NBITS );
     828             :         }
     829             :         else
     830             :         {
     831     4934083 :             diff = 0;
     832     4934083 :             sgn = 1;
     833             : 
     834     4934083 :             if ( get_next_indice( st0, 1 ) == 0 )
     835             :             {
     836     1995330 :                 nbits_diff_angle2 = 1;
     837             :             }
     838             :             else
     839             :             {
     840     2938753 :                 nbits_diff_angle2 = 1;
     841             : 
     842     2938753 :                 if ( get_next_indice( st0, 1 ) == 1 ) /* negative sign */
     843             :                 {
     844     1541360 :                     sgn = -1;
     845             :                 }
     846             : 
     847     2938753 :                 nbits_diff_angle2++;
     848             : 
     849             :                 /* read until the stop bit */
     850    10012472 :                 while ( ( nbits_diff_angle2 < ISM_ELEVATION_NBITS ) && ( get_next_indice( st0, 1 ) == 1 ) )
     851             :                 {
     852     7073719 :                     diff++;
     853     7073719 :                     nbits_diff_angle2++;
     854             :                 }
     855             : 
     856     2938753 :                 if ( nbits_diff_angle2 < ISM_ELEVATION_NBITS )
     857             :                 {
     858             :                     /* count stop bit */
     859     2055571 :                     nbits_diff_angle2++;
     860             :                 }
     861             :             }
     862             : 
     863     4934083 :             idx_angle2 = angle->last_angle2_idx + sgn * diff;
     864             :         }
     865             : 
     866             :         /* sanity check in case of FER or BER */
     867     5432742 :         if ( idx_angle2 < 0 || idx_angle2 > ( 1 << ISM_ELEVATION_NBITS ) - 1 )
     868             :         {
     869           9 :             idx_angle2 = angle->last_angle2_idx;
     870             :         }
     871             :     }
     872             :     else
     873             :     {
     874      418353 :         idx_angle2 = angle->last_angle2_idx; /* second MD parameter is not transmitted for non-diegetic object */
     875             :     }
     876             : 
     877             :     /*----------------------------------------------------------------*
     878             :      * Final updates
     879             :      *----------------------------------------------------------------*/
     880             : 
     881     5851095 :     angle->last_angle2_idx = idx_angle2;
     882     5851095 :     angle->last_angle1_idx = idx_angle1;
     883             : 
     884     5851095 :     return;
     885             : }
     886             : 
     887             : 
     888             : /*-------------------------------------------------------------------------
     889             :  * decode_radius()
     890             :  *
     891             :  * Radius decoding and dequantization
     892             :  *-------------------------------------------------------------------------*/
     893             : 
     894      329613 : static int16_t decode_radius(
     895             :     DEC_CORE_HANDLE st0,      /* i/o: bitstream handle           */
     896             :     int16_t *last_radius_idx, /* i/o: last radius index          */
     897             :     int16_t *flag_abs_radius  /* o  : Radius encoding mode       */
     898             : )
     899             : {
     900             :     int16_t idx_radius, nbits_diff_radius, diff, sgn;
     901             : 
     902             :     /* Decode radius index */
     903      329613 :     if ( get_next_indice( st0, 1 ) == 1 ) /* elevation_abs_flag */
     904             :     {
     905       45363 :         *flag_abs_radius = 1;
     906       45363 :         idx_radius = get_next_indice( st0, ISM_RADIUS_NBITS );
     907             :     }
     908             :     else
     909             :     {
     910      284250 :         diff = 0;
     911      284250 :         sgn = 1;
     912             : 
     913      284250 :         if ( get_next_indice( st0, 1 ) == 0 )
     914             :         {
     915      271518 :             nbits_diff_radius = 1;
     916             :         }
     917             :         else
     918             :         {
     919       12732 :             nbits_diff_radius = 1;
     920             : 
     921       12732 :             if ( get_next_indice( st0, 1 ) == 1 ) /* negative sign */
     922             :             {
     923        4164 :                 sgn = -1;
     924             :             }
     925             : 
     926       12732 :             nbits_diff_radius++;
     927             : 
     928             :             /* read until the stop bit */
     929       27780 :             while ( ( nbits_diff_radius < ISM_RADIUS_NBITS ) && ( get_next_indice( st0, 1 ) == 1 ) )
     930             :             {
     931       15048 :                 diff++;
     932       15048 :                 nbits_diff_radius++;
     933             :             }
     934             : 
     935       12732 :             if ( nbits_diff_radius < ISM_RADIUS_NBITS )
     936             :             {
     937             :                 /* count stop bit */
     938       12195 :                 nbits_diff_radius++;
     939             :             }
     940             :         }
     941             : 
     942      284250 :         idx_radius = *last_radius_idx + sgn * diff;
     943             :     }
     944             : 
     945             :     /* sanity check in case of FER or BER */
     946      329613 :     if ( idx_radius < 0 || idx_radius > ( 1 << ISM_RADIUS_NBITS ) - 1 )
     947             :     {
     948           0 :         idx_radius = *last_radius_idx;
     949             :     }
     950             : 
     951             :     /* Final updates */
     952      329613 :     *last_radius_idx = idx_radius;
     953             : 
     954      329613 :     return idx_radius;
     955             : }
     956             : 
     957             : /*-------------------------------------------------------------------*
     958             :  * ivas_ism_metadata_sid_dec()
     959             :  *
     960             :  * Decode ISM metadata in SID frame
     961             :  *-------------------------------------------------------------------*/
     962             : 
     963        7353 : void ivas_ism_metadata_sid_dec(
     964             :     SCE_DEC_HANDLE hSCE[MAX_SCE],   /* i/o: SCE decoder structure       */
     965             :     const int32_t ism_total_brate,  /* i  : ISM total bitrate           */
     966             :     const int16_t bfi,              /* i  : bfi flag                    */
     967             :     const int16_t nchan_ism,        /* i  : number of objects           */
     968             :     const int16_t nchan_transport,  /* i  : number of transport channels*/
     969             :     const ISM_MODE ism_mode,        /* i  : ISM mode                    */
     970             :     int16_t *flag_noisy_speech,     /* o  : noisy speech flag           */
     971             :     int16_t *sce_id_dtx,            /* o  : SCE DTX ID                  */
     972             :     ISM_METADATA_HANDLE hIsmMeta[], /* i/o: ISM metadata handles        */
     973             :     int16_t nb_bits_metadata[]      /* o  : number of metadata bits     */
     974             : )
     975             : {
     976             :     int16_t i, ch, last_bit_pos;
     977             :     float q_step, q_step_border;
     978             :     int16_t idx, idx_azimuth, idx_elevation;
     979             :     int16_t nBits_azimuth, nBits_elevation, nBits_coh, nBits_sce_id;
     980             :     int16_t md_diff_flag[MAX_NUM_OBJECTS];
     981             :     ISM_MODE ism_mode_bstr;
     982             :     DEC_CORE_HANDLE st0;
     983             :     ISM_METADATA_HANDLE hIsmMetaData;
     984             :     int16_t next_bit_pos_orig;
     985             :     uint16_t bstr_meta[IVAS_SID_5k2 / FRAMES_PER_SEC], *bstr_orig;
     986             : 
     987        7353 :     if ( ism_total_brate == FRAME_NO_DATA )
     988             :     {
     989        5369 :         ism_metadata_smooth( hIsmMeta, ism_total_brate, nchan_ism );
     990             : 
     991        5369 :         return;
     992             :     }
     993             : 
     994             :     /* initialization */
     995        1984 :     st0 = hSCE[0]->hCoreCoder[0];
     996             : 
     997        1984 :     last_bit_pos = (int16_t) ( ( ism_total_brate / FRAMES_PER_SEC ) - 1 - SID_FORMAT_NBITS );
     998        1984 :     bstr_orig = st0->bit_stream;
     999        1984 :     next_bit_pos_orig = st0->next_bit_pos;
    1000        1984 :     st0->next_bit_pos = 0;
    1001             : 
    1002             :     /* reverse the bitstream for easier reading of indices */
    1003      200384 :     for ( i = 0; i < min( MAX_BITS_ISM_METADATA, last_bit_pos ); i++ )
    1004             :     {
    1005      198400 :         bstr_meta[i] = st0->bit_stream[last_bit_pos - i];
    1006             :     }
    1007        1984 :     st0->bit_stream = bstr_meta;
    1008        1984 :     st0->total_brate = ism_total_brate; /* needed for BER detection in get_next_indice() */
    1009             : 
    1010        1984 :     if ( !bfi )
    1011             :     {
    1012             :         /*----------------------------------------------------------------*
    1013             :          * ISM common signaling
    1014             :          *----------------------------------------------------------------*/
    1015             : 
    1016             :         /* number of objects was already read in ivas_ism_get_dtx_dec() */
    1017             :         /* update the position in the bitstream */
    1018        1984 :         st0->next_bit_pos += nchan_ism;
    1019             : 
    1020             :         /* read SID metadata flag( one per object ) */
    1021        8813 :         for ( ch = 0; ch < nchan_ism; ch++ )
    1022             :         {
    1023        6829 :             md_diff_flag[ch] = get_next_indice( st0, 1 );
    1024             :         }
    1025             : 
    1026             :         /*----------------------------------------------------------------*
    1027             :          * Set quantization bits based on the number of coded objects
    1028             :          *----------------------------------------------------------------*/
    1029             : 
    1030        1984 :         ivas_get_ism_sid_quan_bitbudget( nchan_ism, &nBits_azimuth, &nBits_elevation, &q_step, &q_step_border, &nBits_coh, &nBits_sce_id );
    1031             : 
    1032             :         /*----------------------------------------------------------------*
    1033             :          * Spatial parameters, loop over TCs - 1
    1034             :          *----------------------------------------------------------------*/
    1035             : 
    1036        1984 :         *flag_noisy_speech = 0;
    1037        1984 :         *sce_id_dtx = 0;
    1038             : 
    1039             :         /* read ISM mode flag to explicitly signal number of spatial parameters */
    1040        1984 :         if ( nchan_ism > 2 )
    1041             :         {
    1042        1504 :             idx = get_next_indice( st0, 1 );
    1043        1504 :             ism_mode_bstr = (ISM_MODE) ( idx + 1 );
    1044             :             /* note: ISM mode was already read and used for configuration in in ivas_ism_dtx_dec() */
    1045             : 
    1046        1504 :             if ( ism_mode_bstr == ISM_MODE_PARAM )
    1047             :             {
    1048             :                 /* read noisy speech flag */
    1049         661 :                 *flag_noisy_speech = get_next_indice( st0, 1 );
    1050         661 :                 nBits_sce_id = 1;
    1051             :             }
    1052             :         }
    1053             : 
    1054        1984 :         if ( nchan_transport > 1 )
    1055             :         {
    1056             :             /* read sce id */
    1057        1837 :             *sce_id_dtx = get_next_indice( st0, nBits_sce_id );
    1058             : 
    1059             :             /* decode the coherence */
    1060        7197 :             for ( ch = 0; ch < nchan_transport; ch++ )
    1061             :             {
    1062        5360 :                 if ( ch == *sce_id_dtx )
    1063             :                 {
    1064       11022 :                     for ( i = 0; i < MDCT_ST_DTX_NUM_COHERENCE_BANDS; i++ )
    1065             :                     {
    1066        9185 :                         hSCE[ch]->hCoreCoder[0]->hFdCngDec->hFdCngCom->coherence[i] = 1.0f;
    1067             :                     }
    1068             : 
    1069        1837 :                     continue;
    1070             :                 }
    1071             : 
    1072        3523 :                 idx = get_next_indice( st0, nBits_coh );
    1073        3523 :                 hSCE[ch]->hCoreCoder[0]->hFdCngDec->hFdCngCom->coherence[0] = (float) ( idx ) / (float) ( ( 1 << nBits_coh ) - 1 );
    1074             : 
    1075       17615 :                 for ( i = 1; i < MDCT_ST_DTX_NUM_COHERENCE_BANDS; i++ )
    1076             :                 {
    1077       14092 :                     hSCE[ch]->hCoreCoder[0]->hFdCngDec->hFdCngCom->coherence[i] = hSCE[ch]->hCoreCoder[0]->hFdCngDec->hFdCngCom->coherence[0];
    1078             :                 }
    1079             :             }
    1080             :         }
    1081             : 
    1082        1984 :         if ( ism_mode == ISM_MODE_PARAM )
    1083             :         {
    1084        3966 :             for ( i = 0; i < MDCT_ST_DTX_NUM_COHERENCE_BANDS; i++ )
    1085             :             {
    1086        3305 :                 hSCE[*sce_id_dtx]->hCoreCoder[0]->hFdCngDec->hFdCngCom->coherence[i] = hSCE[!*sce_id_dtx]->hCoreCoder[0]->hFdCngDec->hFdCngCom->coherence[i];
    1087             :             }
    1088             :         }
    1089             : 
    1090             :         /*----------------------------------------------------------------*
    1091             :          * Metadata decoding and dequantization, loop over all objects
    1092             :          *----------------------------------------------------------------*/
    1093             : 
    1094        8813 :         for ( ch = 0; ch < nchan_ism; ch++ )
    1095             :         {
    1096        6829 :             hIsmMetaData = hIsmMeta[ch];
    1097        6829 :             if ( md_diff_flag[ch] == 1 )
    1098             :             {
    1099             :                 /* Azimuth decoding */
    1100        2289 :                 idx_azimuth = get_next_indice( st0, nBits_azimuth );
    1101        2289 :                 hIsmMetaData->azimuth = ism_dequant_meta( idx_azimuth, ism_azimuth_borders, q_step, q_step_border, 1 << nBits_azimuth );
    1102             : 
    1103             :                 /* Elevation decoding */
    1104        2289 :                 idx_elevation = get_next_indice( st0, nBits_elevation );
    1105        2289 :                 hIsmMetaData->elevation = ism_dequant_meta( idx_elevation, ism_elevation_borders, q_step, q_step_border, 1 << nBits_elevation );
    1106             : 
    1107             :                 /* update last indexes to correspond to active frames coding */
    1108        2289 :                 if ( nBits_azimuth > ISM_AZIMUTH_NBITS )
    1109             :                 {
    1110         615 :                     hIsmMetaData->position_angle.last_angle1_idx = idx_azimuth >> ( nBits_azimuth - ISM_AZIMUTH_NBITS );
    1111         615 :                     hIsmMetaData->position_angle.last_angle2_idx = idx_elevation >> ( nBits_elevation - ISM_ELEVATION_NBITS );
    1112             :                 }
    1113             :                 else
    1114             :                 {
    1115        1674 :                     hIsmMetaData->position_angle.last_angle1_idx = idx_azimuth << ( ISM_AZIMUTH_NBITS - nBits_azimuth );
    1116        1674 :                     hIsmMetaData->position_angle.last_angle2_idx = idx_elevation << ( ISM_ELEVATION_NBITS - nBits_elevation );
    1117             :                 }
    1118             : 
    1119             :                 /* save for smoothing metadata evolution */
    1120        2289 :                 hIsmMetaData->last_true_azimuth = hIsmMetaData->azimuth;
    1121        2289 :                 hIsmMetaData->last_true_elevation = hIsmMetaData->elevation;
    1122             :             }
    1123             :         }
    1124             : 
    1125             :         /* take into account padding bits as metadata bits to keep later bitrate checks valid */
    1126        1984 :         nb_bits_metadata[*sce_id_dtx] = ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC;
    1127             : 
    1128             :         /* set the bitstream pointer to its original position */
    1129        1984 :         st0->bit_stream = bstr_orig;
    1130        1984 :         st0->next_bit_pos = next_bit_pos_orig;
    1131             :     }
    1132             : 
    1133             :     /* smooth the metadata evolution */
    1134        1984 :     ism_metadata_smooth( hIsmMeta, ism_total_brate, nchan_ism );
    1135             : 
    1136        1984 :     return;
    1137             : }

Generated by: LCOV version 1.14