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 @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 359 372 96.5 %
Date: 2025-05-23 08:37:30 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #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       25143 : 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      104697 :     for ( ch = 0; ch < nchan_ism; ch++ )
      85             :     {
      86       79554 :         hIsmMetaData = hIsmMeta[ch];
      87             : 
      88             :         /* smooth azimuth */
      89       79554 :         diff = hIsmMetaData->last_true_azimuth - hIsmMetaData->last_azimuth;
      90             : 
      91       79554 :         if ( diff > ISM_AZIMUTH_MAX )
      92             :         {
      93        3792 :             diff -= ( ISM_AZIMUTH_MAX - ISM_AZIMUTH_MIN );
      94        3792 :             hIsmMetaData->last_azimuth += ( ISM_AZIMUTH_MAX - ISM_AZIMUTH_MIN );
      95             :         }
      96       75762 :         else if ( diff < ISM_AZIMUTH_MIN )
      97             :         {
      98        1137 :             diff += ( ISM_AZIMUTH_MAX - ISM_AZIMUTH_MIN );
      99             :         }
     100             : 
     101       79554 :         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       72669 :         else if ( fabsf( diff ) > CNG_MD_MAX_DIFF_AZIMUTH )
     106             :         {
     107       26451 :             hIsmMetaData->azimuth = hIsmMetaData->last_azimuth + sign( diff ) * CNG_MD_MAX_DIFF_AZIMUTH;
     108             :         }
     109       46218 :         else if ( diff != 0 )
     110             :         {
     111       13986 :             hIsmMetaData->azimuth = hIsmMetaData->last_true_azimuth;
     112             :         }
     113             : 
     114       79554 :         if ( hIsmMetaData->azimuth > ISM_AZIMUTH_MAX )
     115             :         {
     116        1026 :             hIsmMetaData->azimuth -= ( ISM_AZIMUTH_MAX - ISM_AZIMUTH_MIN );
     117             :         }
     118             : 
     119             :         /* smooth elevation */
     120       79554 :         diff = hIsmMetaData->last_true_elevation - hIsmMetaData->last_elevation;
     121             : 
     122       79554 :         if ( ism_total_brate > IVAS_SID_5k2 && diff > IVAS_ISM_DTX_HO_MAX * CNG_MD_MAX_DIFF_ELEVATION )
     123             :         {
     124             :             /* skip the smoothing */
     125             :         }
     126       79464 :         else if ( fabsf( diff ) > CNG_MD_MAX_DIFF_ELEVATION )
     127             :         {
     128       10590 :             hIsmMetaData->elevation = hIsmMetaData->last_elevation + sign( diff ) * CNG_MD_MAX_DIFF_ELEVATION;
     129             :         }
     130             :     }
     131             : 
     132       25143 :     return;
     133             : }
     134             : 
     135             : 
     136             : /*-------------------------------------------------------------------------*
     137             :  * ivas_ism_metadata_dec()
     138             :  *
     139             :  * decode and dequantize ISM metadata
     140             :  *-------------------------------------------------------------------------*/
     141             : 
     142      387009 : 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      387009 :     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      387009 :     push_wmops( "ism_meta_dec" );
     181             : 
     182             :     /* initialization */
     183      387009 :     ism_metadata_flag_global = 0;
     184      387009 :     nchan_transport_prev = *nchan_transport;
     185             : 
     186      387009 :     last_bit_pos = (int16_t) ( ( ism_total_brate / FRAMES_PER_SEC ) - 1 );
     187      387009 :     bstr_orig = st0->bit_stream;
     188      387009 :     next_bit_pos_orig = st0->next_bit_pos;
     189      387009 :     st0->next_bit_pos = 0;
     190      387009 :     ism_extmeta_bitstream = 0;
     191      387009 :     non_diegetic_flag_global = 0;
     192      387009 :     set_s( null_metadata_flag, 0, nchan_ism );
     193      387009 :     set_s( lowrate_metadata_flag, 0, nchan_ism );
     194             : 
     195             :     /* reverse the bitstream for easier reading of indices */
     196    70048629 :     for ( i = 0; i < min( MAX_BITS_ISM_METADATA, last_bit_pos ); i++ )
     197             :     {
     198    69661620 :         bstr_meta[i] = st0->bit_stream[last_bit_pos - i];
     199             :     }
     200      387009 :     st0->bit_stream = bstr_meta;
     201      387009 :     st0->total_brate = ism_total_brate; /* needed for BER detection in get_next_indice() */
     202             : 
     203      387009 :     if ( !bfi )
     204             :     {
     205             :         /*----------------------------------------------------------------*
     206             :          * Read ISM common signaling
     207             :          *----------------------------------------------------------------*/
     208      381753 :         if ( ism_mode == ISM_SBA_MODE_DISC )
     209             :         {
     210             :             /* number of objects was read in ivas_dec_setup() */
     211       65820 :             st0->next_bit_pos += NO_BITS_MASA_ISM_NO_OBJ;
     212             :         }
     213      315933 :         else if ( ism_mode != ISM_MASA_MODE_DISC && ism_mode != ISM_MASA_MODE_MASA_ONE_OBJ )
     214             :         {
     215             :             /* number of objects was read in ivas_dec_setup() */
     216      300603 :             st0->next_bit_pos += nchan_ism;
     217             : 
     218      300603 :             ism_mode = ivas_ism_mode_select( nchan_ism, ism_total_brate );
     219             :         }
     220             : 
     221      381753 :         if ( ism_mode == ISM_MODE_PARAM )
     222             :         {
     223       59829 :             *nchan_transport = MAX_PARAM_ISM_WAVE;
     224             :         }
     225      321924 :         else if ( ism_mode == ISM_MODE_DISC )
     226             :         {
     227      240774 :             *nchan_transport = nchan_ism;
     228             :         }
     229             : 
     230      381753 :         if ( *nchan_transport != nchan_transport_prev )
     231             :         {
     232           0 :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "wrong number of objects signalled!" );
     233             :         }
     234             : 
     235             :         /* read extended metadata presence flag */
     236      381753 :         if ( ( ism_mode == ISM_MODE_DISC || ism_mode == ISM_SBA_MODE_DISC ) && ism_total_brate >= ISM_EXTENDED_METADATA_BRATE )
     237             :         {
     238      237702 :             ism_extmeta_bitstream = get_next_indice( st0, ISM_EXTENDED_METADATA_BITS );
     239             : 
     240             :             /* read global non-diegetic object flag */
     241      237702 :             if ( ism_extmeta_bitstream )
     242             :             {
     243       24048 :                 non_diegetic_flag_global = get_next_indice( st0, ISM_METADATA_IS_NDP_BITS );
     244             :             }
     245             :         }
     246             : 
     247             :         /* Apply hysteresis in case rate switching causes fluctuation in presence of extended metadata */
     248      381753 :         if ( *ism_extmeta_active == -1 || *ism_extmeta_active == ism_extmeta_bitstream ) /* If first frame or bitstream matches internal state */
     249             :         {
     250      381162 :             *ism_extmeta_active = ism_extmeta_bitstream;
     251      381162 :             *ism_extmeta_cnt = 0;
     252             :         }
     253             :         else
     254             :         {
     255         591 :             ( *ism_extmeta_cnt )++;
     256         591 :             if ( *ism_extmeta_cnt == ISM_METADATA_RS_MAX_FRAMES ) /* ISM_METADATA_RS_MAX_FRAMES change frames observed - update state */
     257             :             {
     258         117 :                 *ism_extmeta_active = ism_extmeta_bitstream;
     259         117 :                 *ism_extmeta_cnt = 0;
     260             :             }
     261             :         }
     262             : 
     263             :         /* Read ISM metadata flags (one per object) */
     264     1400454 :         for ( ch = 0; ch < *nchan_transport; ch++ )
     265             :         {
     266     1018701 :             if ( ism_mode == ISM_SBA_MODE_DISC )
     267             :             {
     268      206841 :                 ism_imp[ch] = get_next_indice( st0, 1 );
     269             :             }
     270      811860 :             else if ( ism_mode == ISM_MASA_MODE_DISC || ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ )
     271             :             {
     272             :                 /* ISM importance flag is already read in ivas_masa_decode() */
     273       31050 :                 ism_imp[ch] = hIsmMeta[ch]->ism_imp;
     274             :             }
     275             :             else
     276             :             {
     277      780810 :                 ism_imp[ch] = get_next_indice( st0, ISM_METADATA_FLAG_BITS );
     278             :             }
     279             : 
     280     1018701 :             if ( ism_imp[ch] > ISM_NO_META )
     281             :             {
     282      985080 :                 hIsmMeta[ch]->ism_metadata_flag = 1;
     283             :             }
     284             :             else
     285             :             {
     286       33621 :                 hIsmMeta[ch]->ism_metadata_flag = 0;
     287             :             }
     288             : 
     289     1018701 :             ism_metadata_flag_global |= hIsmMeta[ch]->ism_metadata_flag;
     290             :         }
     291             : 
     292      481812 :         for ( ; ch < nchan_ism; ch++ )
     293             :         {
     294      100059 :             hIsmMeta[ch]->ism_metadata_flag = 1;
     295      100059 :             ism_metadata_flag_global |= hIsmMeta[ch]->ism_metadata_flag;
     296             :         }
     297             : 
     298             :         /* read ISM_NO_META class signalling */
     299      381753 :         if ( ism_mode == ISM_MODE_DISC || ism_mode == ISM_SBA_MODE_DISC )
     300             :         {
     301     1174587 :             for ( ch = 0; ch < *nchan_transport; ch++ )
     302             :             {
     303      867993 :                 if ( ism_imp[ch] == ISM_NO_META )
     304             :                 {
     305             :                     /* low-rate ISM_NO_META frame */
     306       33621 :                     if ( ism_mode == ISM_MASA_MODE_DISC || ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ )
     307             :                     {
     308           0 :                         null_metadata_flag[ch] = hIsmMeta[ch]->ism_md_null_flag;
     309             :                     }
     310             :                     else
     311             :                     {
     312       33621 :                         null_metadata_flag[ch] = get_next_indice( st0, ISM_METADATA_INACTIVE_FLAG_BITS );
     313             :                     }
     314             :                 }
     315             :             }
     316             : 
     317     1174587 :             for ( ch = 0; ch < *nchan_transport; ch++ )
     318             :             {
     319      867993 :                 if ( ism_imp[ch] == ISM_NO_META )
     320             :                 {
     321       33621 :                     if ( ism_mode == ISM_MASA_MODE_DISC || ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ )
     322             :                     {
     323           0 :                         lowrate_metadata_flag[ch] = hIsmMeta[ch]->ism_md_lowrate_flag;
     324             : 
     325           0 :                         if ( null_metadata_flag[ch] == 0 )
     326             :                         {
     327           0 :                             ism_metadata_flag_global |= lowrate_metadata_flag[ch];
     328             :                         }
     329             :                     }
     330       33621 :                     else if ( ism_mode != ISM_SBA_MODE_DISC )
     331             :                     {
     332       33621 :                         if ( null_metadata_flag[ch] )
     333             :                         {
     334             :                             /* read the true ISM class */
     335       14010 :                             ism_imp[ch] = get_next_indice( st0, ISM_METADATA_FLAG_BITS );
     336             :                         }
     337             :                         else
     338             :                         {
     339             :                             /* read presence of MD in low-rate ISM_NO_META frame flag */
     340       19611 :                             lowrate_metadata_flag[ch] = get_next_indice( st0, ISM_METADATA_INACTIVE_FLAG_BITS );
     341             : 
     342       19611 :                             ism_metadata_flag_global |= lowrate_metadata_flag[ch];
     343             :                         }
     344             :                     }
     345             :                 }
     346             :             }
     347             :         }
     348             : 
     349      381753 :         if ( ism_metadata_flag_global )
     350             :         {
     351             :             /*----------------------------------------------------------------*
     352             :              * Metadata decoding and dequantization, loop over all objects
     353             :              *----------------------------------------------------------------*/
     354             : 
     355      381702 :             int16_t total_bits_metadata = 0, bits_metadata_ism = 0;
     356             :             int16_t nb_bits_objcod_read;
     357             : 
     358      381702 :             if ( ism_mode == ISM_MODE_PARAM )
     359             :             {
     360       59829 :                 nb_bits_start = st0->next_bit_pos;
     361             :             }
     362             : 
     363     1500357 :             for ( ch = 0; ch < nchan_ism; ch++ )
     364             :             {
     365     1118655 :                 hIsmMetaData = hIsmMeta[ch];
     366     1118655 :                 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 )
     367             :                 {
     368      898938 :                     nb_bits_start = st0->next_bit_pos;
     369             :                 }
     370     1118655 :                 flag_abs_position = 0;
     371     1118655 :                 flag_abs_orientation = 0;
     372     1118655 :                 flag_abs_radius = 0;
     373             : 
     374     1118655 :                 if ( hIsmMeta[ch]->ism_metadata_flag || lowrate_metadata_flag[ch] )
     375             :                 {
     376     1103952 :                     if ( non_diegetic_flag_global )
     377             :                     {
     378             :                         /* read non-diegetic flag for each object */
     379        7245 :                         hIsmMetaData->non_diegetic_flag = get_next_indice( st0, ISM_METADATA_IS_NDP_BITS );
     380             :                     }
     381             :                     else
     382             :                     {
     383     1096707 :                         hIsmMetaData->non_diegetic_flag = 0;
     384             :                     }
     385             : 
     386     1103952 :                     if ( hIsmMetaData->non_diegetic_flag )
     387             :                     {
     388             :                         /* Panning gain decoding */
     389        2415 :                         decode_angle_indices( st0, &( hIsmMetaData->position_angle ), hIsmMetaData->non_diegetic_flag, &flag_abs_position );
     390        2415 :                         idx_angle1 = hIsmMetaData->position_angle.last_angle1_idx;
     391        2415 :                         idx_angle2 = 1 << ( ISM_ELEVATION_NBITS - 1 );
     392             : 
     393             :                         /* Panning gain dequantization */
     394        2415 :                         hIsmMetaData->azimuth = ism_dequant_meta( idx_angle1, ism_azimuth_borders, ISM_Q_STEP, ISM_Q_STEP_BORDER, 1 << ISM_AZIMUTH_NBITS );
     395        2415 :                         hIsmMetaData->elevation = 0.0f;
     396             : 
     397             :                         /* save for smoothing metadata evolution */
     398        2415 :                         hIsmMetaData->last_true_azimuth = hIsmMetaData->azimuth;
     399        2415 :                         hIsmMetaData->last_true_elevation = hIsmMetaData->elevation;
     400             :                     }
     401             :                     else
     402             :                     {
     403     1101537 :                         decode_angle_indices( st0, &( hIsmMetaData->position_angle ), hIsmMeta[ch]->non_diegetic_flag, &flag_abs_position );
     404     1101537 :                         idx_angle1 = hIsmMetaData->position_angle.last_angle1_idx;
     405     1101537 :                         idx_angle2 = hIsmMetaData->position_angle.last_angle2_idx;
     406             : 
     407             :                         /* Azimuth/Elevation dequantization */
     408     1101537 :                         if ( ism_mode == ISM_MODE_PARAM )
     409             :                         {
     410      219717 :                             hParamIsm->azi_index[ch] = idx_angle1;
     411      219717 :                             hParamIsm->ele_index[ch] = idx_angle2;
     412             :                         }
     413             :                         else /* ISM_MODE_DISC */
     414             :                         {
     415      881820 :                             hIsmMetaData->azimuth = ism_dequant_meta( idx_angle1, ism_azimuth_borders, ISM_Q_STEP, ISM_Q_STEP_BORDER, 1 << ISM_AZIMUTH_NBITS );
     416      881820 :                             hIsmMetaData->elevation = ism_dequant_meta( idx_angle2, ism_elevation_borders, ISM_Q_STEP, ISM_Q_STEP_BORDER, 1 << ISM_ELEVATION_NBITS );
     417             : 
     418             :                             /* radius/raw/pitch dequantization */
     419      881820 :                             if ( ism_extmeta_bitstream )
     420             :                             {
     421       75075 :                                 decode_angle_indices( st0, &( hIsmMetaData->orientation_angle ), hIsmMeta[ch]->non_diegetic_flag, &flag_abs_orientation );
     422       75075 :                                 idx_angle1 = hIsmMetaData->orientation_angle.last_angle1_idx;
     423       75075 :                                 idx_angle2 = hIsmMetaData->orientation_angle.last_angle2_idx;
     424       75075 :                                 yaw = ism_dequant_meta( idx_angle1, ism_azimuth_borders, ISM_Q_STEP, ISM_Q_STEP_BORDER, 1 << ISM_AZIMUTH_NBITS );
     425       75075 :                                 pitch = ism_dequant_meta( idx_angle2, ism_elevation_borders, ISM_Q_STEP, ISM_Q_STEP_BORDER, 1 << ISM_ELEVATION_NBITS );
     426             : 
     427       75075 :                                 idx_radius = decode_radius( st0, &hIsmMetaData->last_radius_idx, &flag_abs_radius );
     428       75075 :                                 radius = usdequant( idx_radius, ISM_RADIUS_MIN, ISM_RADIUS_DELTA );
     429       75075 :                                 if ( *ism_extmeta_active == 1 )
     430             :                                 {
     431       74355 :                                     hIsmMetaData->yaw = yaw;
     432       74355 :                                     hIsmMetaData->pitch = pitch;
     433       74355 :                                     hIsmMetaData->radius = radius;
     434             :                                 }
     435             :                             }
     436             :                             else
     437             :                             {
     438      806745 :                                 if ( *ism_extmeta_active == 0 )
     439             :                                 {
     440      806529 :                                     hIsmMetaData->yaw = 0.0f;
     441      806529 :                                     hIsmMetaData->pitch = 0.0f;
     442      806529 :                                     hIsmMetaData->radius = 1.0f;
     443             :                                 }
     444             :                             }
     445             :                         }
     446             :                         /* save for smoothing metadata evolution */
     447     1101537 :                         hIsmMetaData->last_true_azimuth = hIsmMetaData->azimuth;
     448     1101537 :                         hIsmMetaData->last_true_elevation = hIsmMetaData->elevation;
     449             :                     }
     450             :                 }
     451             :                 /* save number of metadata bits read */
     452     1118655 :                 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 )
     453             :                 {
     454      898938 :                     nb_bits_metadata[ch] = st0->next_bit_pos - nb_bits_start;
     455             :                 }
     456             :             }
     457             : 
     458      381702 :             if ( ism_mode == ISM_MODE_PARAM )
     459             :             {
     460       59829 :                 hParamIsm->flag_noisy_speech = get_next_indice( st0, 1 );
     461             : 
     462             :                 /* Loop over multiwave to read the object indices from bitstream */
     463      179487 :                 for ( ch = 0; ch < MAX_PARAM_ISM_WAVE; ch++ )
     464             :                 {
     465     1435896 :                     for ( nbands = 0; nbands < hParamIsm->nbands; nbands++ )
     466             :                     {
     467     2632476 :                         for ( nblocks = 0; nblocks < hParamIsm->nblocks[nbands]; nblocks++ )
     468             :                         {
     469     1316238 :                             hParamIsm->obj_indices[nbands][nblocks][ch] = get_next_indice( st0, PARAM_ISM_OBJ_IND_NBITS );
     470             :                         }
     471             :                     }
     472             :                 }
     473             : 
     474             :                 /* Loop over all bands to read power ratio's from bitstream */
     475      717948 :                 for ( nbands = 0; nbands < hParamIsm->nbands; nbands++ )
     476             :                 {
     477     1316238 :                     for ( nblocks = 0; nblocks < hParamIsm->nblocks[nbands]; nblocks++ )
     478             :                     {
     479      658119 :                         hParamIsm->power_ratios_idx[nbands][nblocks] = get_next_indice( st0, PARAM_ISM_POW_RATIO_NBITS );
     480             :                     }
     481             :                 }
     482             : 
     483             :                 /* save number of metadata bits read */
     484       59829 :                 total_bits_metadata = st0->next_bit_pos - nb_bits_start;
     485             : 
     486             :                 /* bits per ISM*/
     487       59829 :                 bits_metadata_ism = (int16_t) ( total_bits_metadata / *nchan_transport );
     488             : 
     489       59829 :                 nb_bits_objcod_read = 0;
     490      179487 :                 for ( ch = 0; ch < *nchan_transport; ch++ )
     491             :                 {
     492      119658 :                     if ( ch == *nchan_transport - 1 )
     493             :                     {
     494       59829 :                         nb_bits_metadata[ch] = total_bits_metadata - nb_bits_objcod_read;
     495             :                     }
     496             :                     else
     497             :                     {
     498       59829 :                         nb_bits_metadata[ch] = bits_metadata_ism;
     499       59829 :                         nb_bits_objcod_read += bits_metadata_ism;
     500             :                     }
     501             :                 }
     502             :             }
     503             :         }
     504             :         else
     505             :         {
     506          51 :             set_s( nb_bits_metadata, 0, *nchan_transport );
     507             :         }
     508             :     }
     509             :     else /* BFI */
     510             :     {
     511       19503 :         for ( ch = 0; ch < nchan_ism; ch++ )
     512             :         {
     513       14247 :             hIsmMeta[ch]->last_ism_metadata_flag = hIsmMeta[ch]->ism_metadata_flag;
     514             :         }
     515             : 
     516        5256 :         set_s( nb_bits_metadata, 0, *nchan_transport );
     517             : 
     518        5256 :         if ( ism_mode == ISM_MODE_PARAM )
     519             :         {
     520        6009 :             for ( ch = 0; ch < nchan_ism; ch++ )
     521             :             {
     522        4674 :                 hParamIsm->azi_index[ch] = hParamIsm->azi_index[ch] + hParamIsm->last_az_sgn[ch] * hParamIsm->last_az_diff[ch];
     523        4674 :                 hParamIsm->ele_index[ch] = hParamIsm->ele_index[ch] + hParamIsm->last_el_sgn[ch] * hParamIsm->last_el_diff[ch];
     524        4674 :                 hIsmMeta[ch]->position_angle.last_angle1_idx = hParamIsm->azi_index[ch];
     525        4674 :                 hIsmMeta[ch]->position_angle.last_angle2_idx = hParamIsm->ele_index[ch];
     526             :             }
     527             :         }
     528             :     }
     529             : 
     530      387009 :     if ( hISMDTX.ism_dtx_hangover_cnt < IVAS_ISM_DTX_HO_MAX )
     531             :     {
     532       18744 :         ism_metadata_smooth( hIsmMeta, ism_total_brate, nchan_ism );
     533       18744 :         hISMDTX.ism_dtx_hangover_cnt += 1;
     534             :     }
     535             : 
     536      387009 :     if ( ism_mode == ISM_SBA_MODE_DISC )
     537             :     {
     538             :         /* set the bitstream pointer to its original position */
     539       66246 :         nb_bits_metadata[0] = st0->next_bit_pos;
     540       66246 :         st0->bit_stream = bstr_orig;
     541       66246 :         st0->next_bit_pos = next_bit_pos_orig;
     542             : 
     543             :         /* updates*/
     544       66246 :         set_s( md_diff_flag, 1, nchan_ism );
     545             : 
     546       66246 :         update_last_metadata( nchan_ism, hIsmMeta, md_diff_flag );
     547       66246 :         pop_wmops();
     548       66246 :         return IVAS_ERR_OK;
     549             :     }
     550             : 
     551      320763 :     if ( ism_mode == ISM_MASA_MODE_DISC || ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ )
     552             :     {
     553       15651 :         ism_metadata_flag_global = 1;
     554             :     }
     555             : 
     556             :     /*----------------------------------------------------------------*
     557             :      * Configuration and decision about bitrates per channel
     558             :      *----------------------------------------------------------------*/
     559             : 
     560      320763 :     if ( !bfi )
     561             :     {
     562      315933 :         int16_t masa_ism_flag = 0;
     563      315933 :         if ( ism_mode == ISM_MASA_MODE_DISC || ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ )
     564             :         {
     565       15330 :             masa_ism_flag = 1;
     566             : 
     567       46380 :             for ( ch = 0; ch < *nchan_transport; ch++ )
     568             :             {
     569       31050 :                 element_brate[ch] = hSCE[ch]->element_brate;
     570             :             }
     571             :         }
     572             : 
     573      315933 :         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 )
     574             :         {
     575           0 :             return error;
     576             :         }
     577             : 
     578     1127793 :         for ( ch = 0; ch < *nchan_transport; ch++ )
     579             :         {
     580      811860 :             hIsmMeta[ch]->last_ism_metadata_flag = hIsmMeta[ch]->ism_metadata_flag;
     581             : 
     582      811860 :             hSCE[ch]->hCoreCoder[0]->low_rate_mode = 0;
     583      811860 :             if ( ism_mode == ISM_MODE_DISC || ism_mode == ISM_MASA_MODE_DISC || ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ )
     584             :             {
     585      692202 :                 if ( ism_imp[ch] == ISM_NO_META && ( ( total_brate[ch] < ACELP_8k00 && element_brate[ch] < SCE_CORE_16k_LOW_LIMIT ) ||
     586       19542 :                                                      ( total_brate[ch] <= ACELP_16k_LOW_LIMIT && element_brate[ch] >= SCE_CORE_16k_LOW_LIMIT ) ) )
     587             :                 {
     588       22428 :                     hSCE[ch]->hCoreCoder[0]->low_rate_mode = 1;
     589             :                 }
     590             :             }
     591             : 
     592      811860 :             if ( ism_mode != ISM_MASA_MODE_DISC && ism_mode != ISM_MASA_MODE_MASA_ONE_OBJ )
     593             :             {
     594      780810 :                 hSCE[ch]->element_brate = element_brate[ch];
     595             :             }
     596      811860 :             hSCE[ch]->hCoreCoder[0]->total_brate = total_brate[ch];
     597             :         }
     598             : 
     599      415992 :         for ( ; ch < nchan_ism; ch++ )
     600             :         {
     601      100059 :             hIsmMeta[ch]->last_ism_metadata_flag = hIsmMeta[ch]->ism_metadata_flag;
     602             :         }
     603             :     }
     604             :     else
     605             :     {
     606       15822 :         for ( ch = 0; ch < *nchan_transport; ch++ )
     607             :         {
     608       10992 :             hSCE[ch]->element_brate = hSCE[ch]->last_element_brate;
     609       10992 :             hSCE[ch]->hCoreCoder[0]->total_brate = hSCE[ch]->hCoreCoder[0]->last_total_brate;
     610             :         }
     611             :     }
     612             : 
     613             :     /*----------------------------------------------------------------*
     614             :      * Set bitsream pointers
     615             :      *----------------------------------------------------------------*/
     616             : 
     617             :     /* set the bitstream pointer to its original position */
     618      320763 :     st0->bit_stream = bstr_orig;
     619      320763 :     st0->next_bit_pos = next_bit_pos_orig;
     620             : 
     621             :     /* set bitstream pointers for each ISM */
     622      822852 :     for ( ch = 1; ch < *nchan_transport; ch++ )
     623             :     {
     624      502089 :         hSCE[ch]->hCoreCoder[0]->bit_stream = hSCE[ch - 1]->hCoreCoder[0]->bit_stream + ( hSCE[ch - 1]->hCoreCoder[0]->total_brate / FRAMES_PER_SEC );
     625             :     }
     626             : 
     627             :     /*----------------------------------------------------------------*
     628             :      * Updates
     629             :      *----------------------------------------------------------------*/
     630             : 
     631      320763 :     set_s( md_diff_flag, 1, nchan_ism );
     632             : 
     633      320763 :     update_last_metadata( nchan_ism, hIsmMeta, md_diff_flag );
     634             : 
     635     1143615 :     for ( ch = 0; ch < *nchan_transport; ch++ )
     636             :     {
     637      822852 :         hSCE[ch]->hCoreCoder[0]->cng_ism_flag = 0;
     638             :     }
     639             : 
     640      320763 :     pop_wmops();
     641             : 
     642      320763 :     return IVAS_ERR_OK;
     643             : }
     644             : 
     645             : 
     646             : /*-------------------------------------------------------------------*
     647             :  * ivas_ism_reset_metadata_handle_dec()
     648             :  *
     649             :  * Reset ISM decoder metadata handle
     650             :  *-------------------------------------------------------------------*/
     651             : 
     652       18588 : void ivas_ism_reset_metadata_handle_dec(
     653             :     ISM_METADATA_HANDLE hIsmMeta /* i/o: ISM metadata handle     */
     654             : )
     655             : {
     656       18588 :     hIsmMeta->last_ism_metadata_flag = 0;
     657       18588 :     hIsmMeta->position_angle.last_angle1_idx = 0;
     658       18588 :     hIsmMeta->position_angle.last_angle2_idx = 1 << ( ISM_ELEVATION_NBITS - 1 );
     659       18588 :     hIsmMeta->orientation_angle.last_angle1_idx = 0;
     660       18588 :     hIsmMeta->orientation_angle.last_angle2_idx = 1 << ( ISM_ELEVATION_NBITS - 1 );
     661       18588 :     hIsmMeta->last_radius_idx = 8; /* Init to radius 1.0 */
     662             : 
     663       18588 :     hIsmMeta->last_true_azimuth = 0;
     664       18588 :     hIsmMeta->last_true_elevation = 0;
     665       18588 :     hIsmMeta->last_azimuth = 0;
     666       18588 :     hIsmMeta->last_elevation = 0;
     667             : 
     668       18588 :     hIsmMeta->ism_imp = -1;
     669       18588 :     hIsmMeta->ism_md_null_flag = 0;
     670       18588 :     hIsmMeta->ism_md_lowrate_flag = 0;
     671             : 
     672       18588 :     ivas_ism_reset_metadata( hIsmMeta );
     673             : 
     674       18588 :     return;
     675             : }
     676             : 
     677             : 
     678             : /*-------------------------------------------------------------------------
     679             :  * ivas_ism_metadata_dec_create()
     680             :  *
     681             :  * Create, allocate, initialize and configure IVAS decoder ISM metadata handles
     682             :  *-------------------------------------------------------------------------*/
     683             : 
     684         954 : ivas_error ivas_ism_metadata_dec_create(
     685             :     Decoder_Struct *st_ivas,    /* i/o: IVAS decoder structure              */
     686             :     const int16_t n_ISms,       /* i  : number of separately coded objects  */
     687             :     int32_t element_brate_tmp[] /* o  : element bitrate per object          */
     688             : )
     689             : {
     690             :     int16_t ch;
     691             :     ivas_error error;
     692             : 
     693             :     /* allocate ISM metadata handles */
     694        4008 :     for ( ch = 0; ch < n_ISms; ch++ )
     695             :     {
     696        3054 :         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' */
     697             :         {
     698        3054 :             if ( ( st_ivas->hIsmMetaData[ch] = (ISM_METADATA_HANDLE) malloc( sizeof( ISM_METADATA_FRAME ) ) ) == NULL )
     699             :             {
     700           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for ISM MetaData\n" ) );
     701             :             }
     702             :         }
     703             : 
     704        3054 :         ivas_ism_reset_metadata_handle_dec( st_ivas->hIsmMetaData[ch] );
     705             :     }
     706             : 
     707             :     /* sanity freeing - it can happen only in reconfiguration when a smaller number of handles than before is requested */
     708        1716 :     for ( ; ch < MAX_NUM_OBJECTS; ch++ )
     709             :     {
     710         762 :         if ( st_ivas->hIsmMetaData[ch] != NULL )
     711             :         {
     712           0 :             free( st_ivas->hIsmMetaData[ch] );
     713           0 :             st_ivas->hIsmMetaData[ch] = NULL;
     714             :         }
     715             :     }
     716             : 
     717         954 :     if ( element_brate_tmp != NULL )
     718             :     {
     719         888 :         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 )
     720             :         {
     721           0 :             return error;
     722             :         }
     723             :     }
     724             : 
     725         954 :     st_ivas->hISMDTX.ism_dtx_hangover_cnt = IVAS_ISM_DTX_HO_MAX;
     726             : 
     727         954 :     return IVAS_ERR_OK;
     728             : }
     729             : 
     730             : 
     731             : /*-------------------------------------------------------------------------
     732             :  * decode_angle_indices()
     733             :  *
     734             :  * Decoding of a position/orientation angle
     735             :  *-------------------------------------------------------------------------*/
     736             : 
     737     1179027 : static void decode_angle_indices(
     738             :     DEC_CORE_HANDLE st0,             /* i/o: bitstream handle                */
     739             :     ISM_METADATA_ANGLE_HANDLE angle, /* i/o: angle handle                    */
     740             :     const int16_t non_diegetic_flag, /* i  : Non diegetic flag               */
     741             :     int16_t *flag_abs_angle1         /* o  : Azimuth/yaw encoding mode       */
     742             : )
     743             : {
     744             :     int16_t idx_angle1, nbits_diff_angle1, diff, sgn;
     745             :     int16_t idx_angle2, nbits_diff_angle2;
     746             : 
     747             :     /*----------------------------------------------------------------*
     748             :      * Azimuth/yaw decoding and dequantization
     749             :      *----------------------------------------------------------------*/
     750             : 
     751             :     /* Decode azimuth/yaw index */
     752     1179027 :     if ( get_next_indice( st0, 1 ) == 1 ) /* azimuth_abs_flag */
     753             :     {
     754      374271 :         idx_angle1 = get_next_indice( st0, ISM_AZIMUTH_NBITS );
     755      374271 :         *flag_abs_angle1 = 1;
     756             :     }
     757             :     else
     758             :     {
     759      804756 :         diff = 0;
     760      804756 :         sgn = 1;
     761             : 
     762      804756 :         if ( get_next_indice( st0, 1 ) == 0 )
     763             :         {
     764      183786 :             nbits_diff_angle1 = 1;
     765             :         }
     766             :         else
     767             :         {
     768      620970 :             nbits_diff_angle1 = 1;
     769             : 
     770      620970 :             if ( get_next_indice( st0, 1 ) == 1 ) /* negative sign */
     771             :             {
     772      202899 :                 sgn = -1;
     773             :             }
     774             : 
     775      620970 :             nbits_diff_angle1++;
     776             : 
     777             :             /* read until the stop bit */
     778     1781478 :             while ( ( nbits_diff_angle1 < ISM_AZIMUTH_NBITS - 1 ) && ( get_next_indice( st0, 1 ) == 1 ) )
     779             :             {
     780     1160508 :                 diff++;
     781     1160508 :                 nbits_diff_angle1++;
     782             :             }
     783             : 
     784      620970 :             if ( nbits_diff_angle1 < ISM_AZIMUTH_NBITS - 1 )
     785             :             {
     786             :                 /* count stop bit */
     787      567900 :                 nbits_diff_angle1++;
     788             :             }
     789             :         }
     790      804756 :         idx_angle1 = angle->last_angle1_idx + sgn * diff;
     791             :     }
     792             : 
     793             :     /* azimuth/yaw is on a circle - check for diff coding for -180° -> 180° and vice versa changes */
     794     1179027 :     if ( idx_angle1 > ( 1 << ISM_AZIMUTH_NBITS ) - 1 )
     795             :     {
     796           6 :         idx_angle1 -= ( 1 << ISM_AZIMUTH_NBITS ) - 1; /* +180° -> -180° */
     797             :     }
     798     1179021 :     else if ( idx_angle1 < 0 )
     799             :     {
     800        4032 :         idx_angle1 += ( 1 << ISM_AZIMUTH_NBITS ) - 1; /* -180° -> +180° */
     801             :     }
     802             : 
     803             :     /* +180° == -180° */
     804     1179027 :     if ( idx_angle1 == ( 1 << ISM_AZIMUTH_NBITS ) - 1 )
     805             :     {
     806       10080 :         idx_angle1 = 0;
     807             :     }
     808             : 
     809             :     /* sanity check in case of FER or BER */
     810     1179027 :     if ( idx_angle1 < 0 || idx_angle1 > ( 1 << ISM_AZIMUTH_NBITS ) - 1 )
     811             :     {
     812           0 :         idx_angle1 = angle->last_angle1_idx;
     813             :     }
     814             : 
     815             :     /*----------------------------------------------------------------*
     816             :      * Elevation/pitch decoding and dequantization
     817             :      *----------------------------------------------------------------*/
     818             : 
     819     1179027 :     if ( non_diegetic_flag == 0 )
     820             :     {
     821             :         /* Decode elevation/pitch index */
     822     1176612 :         if ( *flag_abs_angle1 == 0 && get_next_indice( st0, 1 ) == 1 ) /* elevation_abs_flag */
     823             :         {
     824       98655 :             idx_angle2 = get_next_indice( st0, ISM_ELEVATION_NBITS );
     825             :         }
     826             :         else
     827             :         {
     828     1077957 :             diff = 0;
     829     1077957 :             sgn = 1;
     830             : 
     831     1077957 :             if ( get_next_indice( st0, 1 ) == 0 )
     832             :             {
     833      442737 :                 nbits_diff_angle2 = 1;
     834             :             }
     835             :             else
     836             :             {
     837      635220 :                 nbits_diff_angle2 = 1;
     838             : 
     839      635220 :                 if ( get_next_indice( st0, 1 ) == 1 ) /* negative sign */
     840             :                 {
     841      318249 :                     sgn = -1;
     842             :                 }
     843             : 
     844      635220 :                 nbits_diff_angle2++;
     845             : 
     846             :                 /* read until the stop bit */
     847     2268822 :                 while ( ( nbits_diff_angle2 < ISM_ELEVATION_NBITS ) && ( get_next_indice( st0, 1 ) == 1 ) )
     848             :                 {
     849     1633602 :                     diff++;
     850     1633602 :                     nbits_diff_angle2++;
     851             :                 }
     852             : 
     853      635220 :                 if ( nbits_diff_angle2 < ISM_ELEVATION_NBITS )
     854             :                 {
     855             :                     /* count stop bit */
     856      421572 :                     nbits_diff_angle2++;
     857             :                 }
     858             :             }
     859             : 
     860     1077957 :             idx_angle2 = angle->last_angle2_idx + sgn * diff;
     861             :         }
     862             : 
     863             :         /* sanity check in case of FER or BER */
     864     1176612 :         if ( idx_angle2 < 0 || idx_angle2 > ( 1 << ISM_ELEVATION_NBITS ) - 1 )
     865             :         {
     866           0 :             idx_angle2 = angle->last_angle2_idx;
     867             :         }
     868             :     }
     869             :     else
     870             :     {
     871        2415 :         idx_angle2 = angle->last_angle2_idx; /* second MD parameter is not transmitted for non-diegetic object */
     872             :     }
     873             : 
     874             :     /*----------------------------------------------------------------*
     875             :      * Final updates
     876             :      *----------------------------------------------------------------*/
     877             : 
     878     1179027 :     angle->last_angle2_idx = idx_angle2;
     879     1179027 :     angle->last_angle1_idx = idx_angle1;
     880             : 
     881     1179027 :     return;
     882             : }
     883             : 
     884             : 
     885             : /*-------------------------------------------------------------------------
     886             :  * decode_radius()
     887             :  *
     888             :  * Radius decoding and dequantization
     889             :  *-------------------------------------------------------------------------*/
     890             : 
     891       75075 : static int16_t decode_radius(
     892             :     DEC_CORE_HANDLE st0,      /* i/o: bitstream handle           */
     893             :     int16_t *last_radius_idx, /* i/o: last radius index          */
     894             :     int16_t *flag_abs_radius  /* o  : Radius encoding mode       */
     895             : )
     896             : {
     897             :     int16_t idx_radius, nbits_diff_radius, diff, sgn;
     898             : 
     899             :     /* Decode radius index */
     900       75075 :     if ( get_next_indice( st0, 1 ) == 1 ) /* elevation_abs_flag */
     901             :     {
     902       21126 :         *flag_abs_radius = 1;
     903       21126 :         idx_radius = get_next_indice( st0, ISM_RADIUS_NBITS );
     904             :     }
     905             :     else
     906             :     {
     907       53949 :         diff = 0;
     908       53949 :         sgn = 1;
     909             : 
     910       53949 :         if ( get_next_indice( st0, 1 ) == 0 )
     911             :         {
     912       44871 :             nbits_diff_radius = 1;
     913             :         }
     914             :         else
     915             :         {
     916        9078 :             nbits_diff_radius = 1;
     917             : 
     918        9078 :             if ( get_next_indice( st0, 1 ) == 1 ) /* negative sign */
     919             :             {
     920        4164 :                 sgn = -1;
     921             :             }
     922             : 
     923        9078 :             nbits_diff_radius++;
     924             : 
     925             :             /* read until the stop bit */
     926       20472 :             while ( ( nbits_diff_radius < ISM_RADIUS_NBITS ) && ( get_next_indice( st0, 1 ) == 1 ) )
     927             :             {
     928       11394 :                 diff++;
     929       11394 :                 nbits_diff_radius++;
     930             :             }
     931             : 
     932        9078 :             if ( nbits_diff_radius < ISM_RADIUS_NBITS )
     933             :             {
     934             :                 /* count stop bit */
     935        8541 :                 nbits_diff_radius++;
     936             :             }
     937             :         }
     938       53949 :         idx_radius = *last_radius_idx + sgn * diff;
     939             :     }
     940             : 
     941             :     /* sanity check in case of FER or BER */
     942       75075 :     if ( idx_radius < 0 || idx_radius > ( 1 << ISM_RADIUS_NBITS ) - 1 )
     943             :     {
     944           0 :         idx_radius = *last_radius_idx;
     945             :     }
     946             : 
     947             :     /* Final updates */
     948       75075 :     *last_radius_idx = idx_radius;
     949             : 
     950       75075 :     return idx_radius;
     951             : }
     952             : 
     953             : /*-------------------------------------------------------------------*
     954             :  * ivas_ism_metadata_sid_dec()
     955             :  *
     956             :  * Decode ISM metadata in SID frame
     957             :  *-------------------------------------------------------------------*/
     958             : 
     959        6399 : void ivas_ism_metadata_sid_dec(
     960             :     SCE_DEC_HANDLE hSCE[MAX_SCE],   /* i/o: SCE decoder structure       */
     961             :     const int32_t ism_total_brate,  /* i  : ISM total bitrate           */
     962             :     const int16_t bfi,              /* i  : bfi flag                    */
     963             :     const int16_t nchan_ism,        /* i  : number of objects           */
     964             :     const int16_t nchan_transport,  /* i  : number of transport channels*/
     965             :     const ISM_MODE ism_mode,        /* i  : ISM mode                    */
     966             :     int16_t *flag_noisy_speech,     /* o  : noisy speech flag           */
     967             :     int16_t *sce_id_dtx,            /* o  : SCE DTX ID                  */
     968             :     ISM_METADATA_HANDLE hIsmMeta[], /* i/o: ISM metadata handles        */
     969             :     int16_t nb_bits_metadata[]      /* o  : number of metadata bits     */
     970             : )
     971             : {
     972             :     int16_t i, ch, last_bit_pos;
     973             :     float q_step, q_step_border;
     974             :     int16_t idx, idx_azimuth, idx_elevation;
     975             :     int16_t nBits_azimuth, nBits_elevation, nBits_coh, nBits_sce_id;
     976             :     int16_t md_diff_flag[MAX_NUM_OBJECTS];
     977             :     ISM_MODE ism_mode_bstr;
     978             :     DEC_CORE_HANDLE st0;
     979             :     ISM_METADATA_HANDLE hIsmMetaData;
     980             :     int16_t next_bit_pos_orig;
     981             :     uint16_t bstr_meta[IVAS_SID_5k2 / FRAMES_PER_SEC], *bstr_orig;
     982             : 
     983        6399 :     if ( ism_total_brate == FRAME_NO_DATA )
     984             :     {
     985        4773 :         ism_metadata_smooth( hIsmMeta, ism_total_brate, nchan_ism );
     986             : 
     987        4773 :         return;
     988             :     }
     989             : 
     990             :     /* initialization */
     991        1626 :     st0 = hSCE[0]->hCoreCoder[0];
     992             : 
     993        1626 :     last_bit_pos = (int16_t) ( ( ism_total_brate / FRAMES_PER_SEC ) - 1 - SID_FORMAT_NBITS );
     994        1626 :     bstr_orig = st0->bit_stream;
     995        1626 :     next_bit_pos_orig = st0->next_bit_pos;
     996        1626 :     st0->next_bit_pos = 0;
     997             : 
     998             :     /* reverse the bitstream for easier reading of indices */
     999      164226 :     for ( i = 0; i < min( MAX_BITS_ISM_METADATA, last_bit_pos ); i++ )
    1000             :     {
    1001      162600 :         bstr_meta[i] = st0->bit_stream[last_bit_pos - i];
    1002             :     }
    1003        1626 :     st0->bit_stream = bstr_meta;
    1004        1626 :     st0->total_brate = ism_total_brate; /* needed for BER detection in get_next_indice() */
    1005             : 
    1006        1626 :     if ( !bfi )
    1007             :     {
    1008             :         /*----------------------------------------------------------------*
    1009             :          * ISm common signaling
    1010             :          *----------------------------------------------------------------*/
    1011             : 
    1012             :         /* number of objects was already read in ivas_ism_get_dtx_dec() */
    1013             :         /* update the position in the bitstream */
    1014        1626 :         st0->next_bit_pos += nchan_ism;
    1015             : 
    1016             :         /* read SID metadata flag( one per object ) */
    1017        7023 :         for ( ch = 0; ch < nchan_ism; ch++ )
    1018             :         {
    1019        5397 :             md_diff_flag[ch] = get_next_indice( st0, 1 );
    1020             :         }
    1021             : 
    1022             :         /*----------------------------------------------------------------*
    1023             :          * Set quantization bits based on the number of coded objects
    1024             :          *----------------------------------------------------------------*/
    1025             : 
    1026        1626 :         ivas_get_ism_sid_quan_bitbudget( nchan_ism, &nBits_azimuth, &nBits_elevation, &q_step, &q_step_border, &nBits_coh, &nBits_sce_id );
    1027             : 
    1028             :         /*----------------------------------------------------------------*
    1029             :          * Spatial parameters, loop over TCs - 1
    1030             :          *----------------------------------------------------------------*/
    1031             : 
    1032        1626 :         *flag_noisy_speech = 0;
    1033        1626 :         *sce_id_dtx = 0;
    1034             : 
    1035             :         /* read ISM mode flag to explicitly signal number of spatial parameters */
    1036        1626 :         if ( nchan_ism > 2 )
    1037             :         {
    1038        1146 :             idx = get_next_indice( st0, 1 );
    1039        1146 :             ism_mode_bstr = (ISM_MODE) ( idx + 1 );
    1040             :             /* note: ISM mode was already read and used for configuration in in ivas_ism_dtx_dec() */
    1041             : 
    1042        1146 :             if ( ism_mode_bstr == ISM_MODE_PARAM )
    1043             :             {
    1044             :                 /* read noisy speech flag */
    1045         321 :                 *flag_noisy_speech = get_next_indice( st0, 1 );
    1046         321 :                 nBits_sce_id = 1;
    1047             :             }
    1048             :         }
    1049             : 
    1050        1626 :         if ( nchan_transport > 1 )
    1051             :         {
    1052             :             /* read sce id */
    1053        1479 :             *sce_id_dtx = get_next_indice( st0, nBits_sce_id );
    1054             : 
    1055             :             /* decode the coherence */
    1056        6087 :             for ( ch = 0; ch < nchan_transport; ch++ )
    1057             :             {
    1058        4608 :                 if ( ch == *sce_id_dtx )
    1059             :                 {
    1060        1479 :                     hSCE[ch]->hCoreCoder[0]->hFdCngDec->hFdCngCom->coherence = 1.0f;
    1061        1479 :                     continue;
    1062             :                 }
    1063             : 
    1064        3129 :                 idx = get_next_indice( st0, nBits_coh );
    1065        3129 :                 hSCE[ch]->hCoreCoder[0]->hFdCngDec->hFdCngCom->coherence = (float) ( idx ) / (float) ( ( 1 << nBits_coh ) - 1 );
    1066             :             }
    1067             :         }
    1068             : 
    1069        1626 :         if ( ism_mode == ISM_MODE_PARAM )
    1070             :         {
    1071         321 :             hSCE[*sce_id_dtx]->hCoreCoder[0]->hFdCngDec->hFdCngCom->coherence = hSCE[!*sce_id_dtx]->hCoreCoder[0]->hFdCngDec->hFdCngCom->coherence;
    1072             :         }
    1073             : 
    1074             :         /*----------------------------------------------------------------*
    1075             :          * Metadata decoding and dequantization, loop over all objects
    1076             :          *----------------------------------------------------------------*/
    1077             : 
    1078        7023 :         for ( ch = 0; ch < nchan_ism; ch++ )
    1079             :         {
    1080        5397 :             hIsmMetaData = hIsmMeta[ch];
    1081        5397 :             if ( md_diff_flag[ch] == 1 )
    1082             :             {
    1083             :                 /* Azimuth decoding */
    1084        1905 :                 idx_azimuth = get_next_indice( st0, nBits_azimuth );
    1085        1905 :                 hIsmMetaData->azimuth = ism_dequant_meta( idx_azimuth, ism_azimuth_borders, q_step, q_step_border, 1 << nBits_azimuth );
    1086             : 
    1087             :                 /* Elevation decoding */
    1088        1905 :                 idx_elevation = get_next_indice( st0, nBits_elevation );
    1089        1905 :                 hIsmMetaData->elevation = ism_dequant_meta( idx_elevation, ism_elevation_borders, q_step, q_step_border, 1 << nBits_elevation );
    1090             : 
    1091             :                 /* update last indexes to correspond to active frames coding */
    1092        1905 :                 if ( nBits_azimuth > ISM_AZIMUTH_NBITS )
    1093             :                 {
    1094         615 :                     hIsmMetaData->position_angle.last_angle1_idx = idx_azimuth >> ( nBits_azimuth - ISM_AZIMUTH_NBITS );
    1095         615 :                     hIsmMetaData->position_angle.last_angle2_idx = idx_elevation >> ( nBits_elevation - ISM_ELEVATION_NBITS );
    1096             :                 }
    1097             :                 else
    1098             :                 {
    1099        1290 :                     hIsmMetaData->position_angle.last_angle1_idx = idx_azimuth << ( ISM_AZIMUTH_NBITS - nBits_azimuth );
    1100        1290 :                     hIsmMetaData->position_angle.last_angle2_idx = idx_elevation << ( ISM_ELEVATION_NBITS - nBits_elevation );
    1101             :                 }
    1102             : 
    1103             :                 /* save for smoothing metadata evolution */
    1104        1905 :                 hIsmMetaData->last_true_azimuth = hIsmMetaData->azimuth;
    1105        1905 :                 hIsmMetaData->last_true_elevation = hIsmMetaData->elevation;
    1106             :             }
    1107             :         }
    1108             : 
    1109             :         /* take into account padding bits as metadata bits to keep later bitrate checks valid */
    1110        1626 :         nb_bits_metadata[*sce_id_dtx] = ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC;
    1111             : 
    1112             :         /* set the bitstream pointer to its original position */
    1113        1626 :         st0->bit_stream = bstr_orig;
    1114        1626 :         st0->next_bit_pos = next_bit_pos_orig;
    1115             :     }
    1116             : 
    1117             :     /* smooth the metadata evolution */
    1118        1626 :     ism_metadata_smooth( hIsmMeta, ism_total_brate, nchan_ism );
    1119             : 
    1120        1626 :     return;
    1121             : }

Generated by: LCOV version 1.14