LCOV - code coverage report
Current view: top level - lib_dec - ivas_qmetadata_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ b5bd5e0684ad2d9250297e1e7a0ddc986cb7b37e Lines: 1395 1498 93.1 %
Date: 2025-10-27 07:01:45 Functions: 32 32 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 <assert.h>
      34             : #include <stdint.h>
      35             : #include "options.h"
      36             : #include <math.h>
      37             : #include "ivas_cnst.h"
      38             : #include "ivas_prot.h"
      39             : #include "ivas_rom_com.h"
      40             : #include "ivas_rom_dec.h"
      41             : #include "wmc_auto.h"
      42             : #include "prot.h"
      43             : #include "basop_settings.h"
      44             : 
      45             : 
      46             : /*-----------------------------------------------------------------------*
      47             :  * Local function prototypes
      48             :  *-----------------------------------------------------------------------*/
      49             : 
      50             : static int16_t ivas_qmetadata_entropy_decode_diffuseness( uint16_t *bitstream, int16_t *index, IVAS_QDIRECTION *q_direction, uint16_t *diffuseness_index_max_ec_frame );
      51             : 
      52             : static int16_t ivas_qmetadata_entropy_decode_df_ratio( uint16_t *bitstream, int16_t *index, IVAS_QDIRECTION *q_direction, int16_t *dfRatio_bits );
      53             : 
      54             : static int16_t ivas_qmetadata_entropy_decode_dir( IVAS_QDIRECTION *q_direction, uint16_t *bitstream, int16_t *index, const uint16_t diffuseness_index_max_ec_frame, const int16_t nbands, const int16_t start_band, const int16_t hrmasa_flag );
      55             : 
      56             : static int16_t ivas_qmetadata_raw_decode_dir( IVAS_QDIRECTION *q_direction, uint16_t *bitstream, int16_t *index, const int16_t nbands, const int16_t start_band, const int16_t hrmasa_flag );
      57             : 
      58             : static uint16_t ivas_qmetadata_DecodeQuasiUniform( const uint16_t *bitstream, int16_t *index, const uint16_t alphabet_size );
      59             : 
      60             : static int16_t ivas_qmetadata_ReorderElevationDecoded( const int16_t elev_dist, const int16_t elev_avg, const int16_t elev_alph );
      61             : 
      62             : static int16_t read_directions( IVAS_QDIRECTION *q_direction, const uint8_t coding_subbands, const uint8_t masa_subframes, uint16_t *bitstream, int16_t *pbit_pos, int16_t *ind_order );
      63             : 
      64             : static int16_t read_common_direction( uint16_t *bitstream, IVAS_QDIRECTION *q_direction, const int16_t j, const int16_t no_subframes, const int16_t bits_total, int16_t *pbit_pos );
      65             : 
      66             : static int16_t decode_fixed_rate( IVAS_QDIRECTION *q_direction, const uint16_t *bitstream, int16_t *pbit_pos, const int16_t b, const int16_t nblocks );
      67             : 
      68             : static int16_t decode_azimuth( IVAS_QDIRECTION *q_direction, uint16_t *bitstream, int16_t *pbit_pos, const int16_t idx_subband, const int16_t masa_subframes );
      69             : 
      70             : static int16_t decode_elevation( IVAS_QDIRECTION *q_direction, uint16_t *bitstream, int16_t *pbit_pos, const int16_t j, const int16_t masa_subframes );
      71             : 
      72             : static int16_t decode_azimuth2D( IVAS_QDIRECTION *q_direction, uint16_t *bitstream, const int16_t coding_subbands, int16_t *pbit_pos, const int16_t no_frames );
      73             : 
      74             : static void set_zero_direction( IVAS_QDIRECTION *q_direction, const int16_t idx_band, const int16_t len );
      75             : 
      76             : static int16_t read_truncGR_azimuth( uint16_t *bitstream, IVAS_QDIRECTION *q_direction, const int16_t j, const int16_t no_subframes, int16_t *pbit_pos );
      77             : 
      78             : static ivas_error read_huf( int16_t *num_bits_read, const uint16_t *bitstream, uint16_t *out, const int16_t start_pos, const int16_t len, const int16_t *huff_code, const int16_t max_len );
      79             : 
      80             : static int16_t read_coherence_data( uint16_t *bitstream, int16_t *p_bit_pos, IVAS_QMETADATA *hQMetaData, const int16_t idx_dir, const int16_t hrmasa_flag );
      81             : 
      82             : static int16_t read_surround_coherence( uint16_t *bitstream, int16_t *p_bit_pos, IVAS_QMETADATA *hQMetaData );
      83             : 
      84             : static void decode_spread_coherence( IVAS_QMETADATA_HANDLE hQMetaData, int16_t idx_d, const int16_t no_frames, const int16_t hrmasa_flag );
      85             : 
      86             : static void decode_combined_index( uint64_t comb_index, const int16_t *no_cv_vec, uint16_t *index, const int16_t len );
      87             : 
      88             : static int16_t ivas_diffuseness_huff_ec_decode( const uint16_t *bitstream, int16_t *index, int16_t av );
      89             : 
      90             : static int16_t read_GR_min_removed_data( uint16_t *bitstream, int16_t *p_bit_pos, const int16_t *no_cv_vec, const int16_t no_data, int16_t *decoded_idx, const int16_t no_symb );
      91             : 
      92             : static int16_t decode_fixed_rate_composed_index_coherence( uint16_t *bitstream, int16_t *p_bit_pos, const int16_t no_bands, int16_t *no_cv_vec, uint16_t *decoded_index, const int16_t no_symb );
      93             : 
      94             : static int16_t ivas_qmetadata_entropy_decode_diffuseness_hr_512( uint16_t *bitstream, int16_t *index, IVAS_QDIRECTION *q_direction );
      95             : 
      96             : static int16_t ivas_qmetadata_raw_decode_dir_512( IVAS_QDIRECTION *q_direction, uint16_t *bitstream, int16_t *index, const int16_t nbands, const int16_t start_band, const SPHERICAL_GRID_DATA *sph_grid16 );
      97             : 
      98             : static int16_t read_surround_coherence_hr( uint16_t *bitstream, int16_t *p_bit_pos, IVAS_QMETADATA *hQMetaData );
      99             : 
     100             : static int16_t read_coherence_data_hr_512( uint16_t *bitstream, int16_t *p_bit_pos, IVAS_QMETADATA *hQMetaData, const int16_t idx_dir, const int16_t nbits_coh );
     101             : 
     102             : static void read_stream_dct_coeffs_omasa( int16_t *q_idx,
     103             :                                           Word32 *q_dct_data_fx,
     104             :                                           const int16_t len_stream,
     105             :                                           uint16_t *bit_stream,
     106             :                                           int16_t *index,
     107             :                                           const int16_t first_line );
     108             : 
     109             : 
     110             : /*-----------------------------------------------------------------------*
     111             :  * Global function definitions
     112             :  *-----------------------------------------------------------------------*/
     113             : 
     114             : /*-----------------------------------------------------------------------*
     115             :  * ivas_qmetadata_dec_decode()
     116             :  *
     117             :  * Main function for decoding Spatial Metadata
     118             :  *-----------------------------------------------------------------------*/
     119             : 
     120             : /*! r: number of bits read */
     121     3443150 : int16_t ivas_qmetadata_dec_decode(
     122             :     IVAS_QMETADATA_HANDLE hQMetaData, /* i/o: hQMetaData handle                 */
     123             :     uint16_t *bitstream,              /* i  : bitstream                         */
     124             :     int16_t *index,                   /* i/o: bitstream position                */
     125             :     const int16_t hodirac_flag        /* i  : flag to indicate HO-DirAC mode    */
     126             : )
     127             : {
     128             :     int16_t d, b, m;
     129             :     uint16_t diffuseness_index_max_ec_frame;
     130             :     uint16_t diffuseness_index_max_ec_frame_pre[QMETADATA_MAX_NO_DIRECTIONS];
     131             :     int16_t bits_dir_raw_pre[QMETADATA_MAX_NO_DIRECTIONS];
     132             :     int16_t dir2band;
     133             :     int16_t bits_diff_sum;
     134             :     int16_t bits_diff, bits_coherence;
     135             :     int16_t bits_dir_raw;
     136             :     int16_t bits_dir;
     137             :     int16_t nbands, nblocks, start_band;
     138             :     IVAS_QDIRECTION *q_direction;
     139             :     int16_t start_index_0;
     140             :     int16_t total_bits_1dir;
     141             :     int16_t signal_bits;
     142             :     int16_t bits_no_dirs_coh, bits_sur_coherence;
     143             :     uint16_t all_coherence_zero;
     144             :     int16_t ec_flag;
     145             :     int16_t raw_flag[MASA_MAXIMUM_CODING_SUBBANDS];
     146             :     int16_t diff_bits;
     147             :     int16_t dfRatio_bits[MASA_MAXIMUM_CODING_SUBBANDS];
     148             :     int16_t no_TF;
     149             :     int16_t p[MASA_MAXIMUM_CODING_SUBBANDS], dif_p[MASA_MAXIMUM_CODING_SUBBANDS];
     150             :     int16_t bits_dir_target;
     151             :     int16_t bits_dir_used;
     152             :     int16_t reduce_bits;
     153             :     int16_t ind_order[MASA_MAXIMUM_CODING_SUBBANDS];
     154             : 
     155             : #ifdef DEBUG_MODE_QMETADATA
     156             :     static FILE *pF = NULL;
     157             :     static FILE *pF_azi = NULL;
     158             :     static FILE *pF_ele = NULL;
     159             :     static FILE *pF_ratio = NULL;
     160             :     static FILE *pF_spcoh = NULL;
     161             :     static FILE *pF_surcoh = NULL;
     162             : 
     163             :     if ( pF == NULL )
     164             :         pF = fopen( "./res/qmetadata_dec.txt", "w" );
     165             :     if ( pF_azi == NULL )
     166             :         pF_azi = fopen( "./res/qmetadata_azi_dec.txt", "w" );
     167             :     if ( pF_ele == NULL )
     168             :         pF_ele = fopen( "./res/qmetadata_ele_dec.txt", "w" );
     169             :     if ( pF_ratio == NULL )
     170             :         pF_ratio = fopen( "./res/qmetadata_ratio_dec.txt", "w" );
     171             :     if ( pF_spcoh == NULL )
     172             :         pF_spcoh = fopen( "./res/qmetadata_spcoh_dec.txt", "w" );
     173             :     if ( pF_surcoh == NULL )
     174             :         pF_surcoh = fopen( "./res/qmetadata_surcoh_dec.txt", "w" );
     175             : #endif
     176             : 
     177     3443150 :     ec_flag = 0;
     178     3443150 :     start_index_0 = *index;
     179             : 
     180             :     /*Coherence flag decoding*/
     181     3443150 :     bits_no_dirs_coh = 0;
     182     3443150 :     all_coherence_zero = 1;
     183     3443150 :     if ( hQMetaData->coherence_flag )
     184             :     {
     185             :         /* read if coherence is zero */
     186      583096 :         all_coherence_zero = bitstream[( *index )--];
     187      583096 :         bits_no_dirs_coh += 1;
     188             :     }
     189             : 
     190     3443150 :     hQMetaData->all_coherence_zero = (uint8_t) all_coherence_zero;
     191             : 
     192     3443150 :     if ( hQMetaData->no_directions == 2 )
     193             :     {
     194             :         /* Read which bands have 2 directions */
     195      323950 :         hQMetaData->q_direction[1].cfg.nbands = hQMetaData->numTwoDirBands;
     196      323950 :         set_c( (int8_t *) hQMetaData->twoDirBands, 0, hQMetaData->q_direction[0].cfg.nbands );
     197      323950 :         d = *index;
     198      323950 :         dif_p[0] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 );
     199      323950 :         p[0] = dif_p[0];
     200      323950 :         hQMetaData->twoDirBands[p[0]] = 1;
     201     3262809 :         for ( b = 1; b < hQMetaData->numTwoDirBands; b++ )
     202             :         {
     203     2938859 :             dif_p[b] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 );
     204     2938859 :             p[b] = p[b - 1] + dif_p[b] + 1;
     205     2938859 :             hQMetaData->twoDirBands[p[b]] = 1;
     206             :         }
     207      323950 :         bits_no_dirs_coh += ( d - *index );
     208             :     }
     209             : 
     210     3443150 :     bits_diff_sum = 0;
     211     3443150 :     bits_diff_sum += ivas_qmetadata_entropy_decode_diffuseness( bitstream, index, &( hQMetaData->q_direction[0] ), &diffuseness_index_max_ec_frame_pre[0] );
     212             : 
     213     3443150 :     if ( hodirac_flag )
     214             :     {
     215      285415 :         if ( hQMetaData->no_directions == 2 )
     216             :         {
     217             :             /* Calculate bits for dfRatio */
     218      285415 :             dir2band = 0;
     219     3424980 :             for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     220             :             {
     221     3139565 :                 if ( hQMetaData->twoDirBands[b] == 1 )
     222             :                 {
     223     3139565 :                     dfRatio_bits[dir2band] = ivas_get_df_ratio_bits_hodirac( hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0] );
     224     3139565 :                     dir2band++;
     225             :                 }
     226             :             }
     227             : 
     228      285415 :             bits_diff_sum += ivas_qmetadata_entropy_decode_df_ratio( bitstream, index, &( hQMetaData->q_direction[1] ), dfRatio_bits );
     229             :         }
     230             :     }
     231             :     else
     232             :     {
     233     3157735 :         if ( hQMetaData->no_directions == 2 )
     234             :         {
     235             :             /* Calculate bits for dfRatio */
     236       38535 :             dir2band = 0;
     237      350547 :             for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     238             :             {
     239      312012 :                 if ( hQMetaData->twoDirBands[b] == 1 )
     240             :                 {
     241      123244 :                     dfRatio_bits[dir2band] = ivas_get_df_ratio_bits( hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0] );
     242      123244 :                     dir2band++;
     243             :                 }
     244             :             }
     245             : 
     246       38535 :             bits_diff_sum += ivas_qmetadata_entropy_decode_df_ratio( bitstream, index, &( hQMetaData->q_direction[1] ), dfRatio_bits );
     247             :         }
     248             :     }
     249             : 
     250             :     /* Calculate direct-to-total energy ratios for both directions from diffuse-to-total ratio and distribution factor of direct-to-total ratios */
     251     3443150 :     if ( hQMetaData->no_directions == 2 )
     252             :     {
     253      323950 :         dir2band = 0;
     254     3775527 :         for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     255             :         {
     256     3451577 :             if ( hQMetaData->twoDirBands[b] == 1 )
     257             :             {
     258             :                 float diffRatio, dfRatio, dir1ratio, dir2ratio;
     259             :                 int16_t dfRatio_qsteps;
     260             : 
     261     3262809 :                 diffRatio = diffuseness_reconstructions[hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0]];
     262     3262809 :                 dfRatio_qsteps = 1 << dfRatio_bits[dir2band];
     263             :                 /* already encoded as total and ratios in HO-DirAC */
     264     3262809 :                 if ( hodirac_flag )
     265             :                 {
     266     3139565 :                     dfRatio = usdequant( hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0], 0.0f, 1.f / ( dfRatio_qsteps - 1 ) );
     267     3139565 :                     dir1ratio = 1.f - diffRatio;
     268     3139565 :                     dir2ratio = dfRatio;
     269             :                 }
     270             :                 else
     271             :                 {
     272      123244 :                     dfRatio = usdequant( hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0], 0.5f, 0.5f / ( dfRatio_qsteps - 1 ) );
     273             : 
     274      123244 :                     dir1ratio = dfRatio * ( 1.0f - diffRatio );
     275      123244 :                     dir2ratio = ( 1.0f - diffRatio ) - dir1ratio;
     276             :                 }
     277             : 
     278             :                 /* Requantize the 1 - dirRatio separately for each direction to obtain inverted dirRatio index. These are used in further decoding. */
     279     3262809 :                 hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0] = masa_sq( 1.0f - dir1ratio, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
     280     3262809 :                 if ( hodirac_flag )
     281             :                 {
     282             :                     float tmp;
     283     3139565 :                     hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0] = usquant( dir2ratio, &tmp, 0.0f, 1.f / ( DIRAC_DIFFUSE_LEVELS - 1 ), DIRAC_DIFFUSE_LEVELS );
     284             :                 }
     285             :                 else
     286             :                 {
     287      123244 :                     hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0] = masa_sq( 1.0f - dir2ratio, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
     288             :                 }
     289             : 
     290    16280088 :                 for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     291             :                 {
     292    13017279 :                     hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = dir1ratio;
     293    13017279 :                     hQMetaData->q_direction[0].band_data[b].energy_ratio_index[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
     294             :                 }
     295             : 
     296    16280088 :                 for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
     297             :                 {
     298    13017279 :                     hQMetaData->q_direction[1].band_data[dir2band].energy_ratio[m] = dir2ratio;
     299    13017279 :                     hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[m] = hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0];
     300             :                 }
     301             : 
     302     3262809 :                 dir2band++;
     303             :             }
     304             :             else
     305             :             {
     306             :                 /* 1dir band */
     307      188768 :                 hQMetaData->q_direction[0].band_data[b].energy_ratio[0] = 1.0f - diffuseness_reconstructions[hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0]];
     308      634265 :                 for ( m = 1; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     309             :                 {
     310      445497 :                     hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio[0];
     311      445497 :                     hQMetaData->q_direction[0].band_data[b].energy_ratio_index[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
     312             :                 }
     313             :             }
     314             :         }
     315             :     }
     316             :     else
     317             :     {
     318             :         /* With 1dir band, the decoded index is directly diffuseness and we can decode to direct-to-total ratio with 1 - diff. */
     319    16056676 :         for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     320             :         {
     321    12937476 :             hQMetaData->q_direction[0].band_data[b].energy_ratio[0] = 1.0f - diffuseness_reconstructions[hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0]];
     322    43849806 :             for ( m = 1; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     323             :             {
     324    30912330 :                 hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio[0];
     325    30912330 :                 hQMetaData->q_direction[0].band_data[b].energy_ratio_index[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
     326             :             }
     327             :         }
     328             :     }
     329             : 
     330             :     /* To decode directions correctly for 2dir bands, we need to obtain the compensated direct-to-total ratios and their
     331             :      * corresponding inverted indices. */
     332     3443150 :     bits_dir_raw_pre[0] = 0;
     333     3443150 :     bits_dir_raw_pre[1] = 0;
     334     3443150 :     dir2band = 0;
     335    19832203 :     for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     336             :     {
     337    16389053 :         if ( hQMetaData->no_directions == 2 && hQMetaData->twoDirBands[b] == 1 )
     338     3262809 :         {
     339             :             int16_t index_dirRatio1Inv, index_dirRatio2Inv, index_dirRatio1Inv_mod, index_dirRatio2Inv_mod;
     340             : 
     341     3262809 :             index_dirRatio1Inv = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
     342     3262809 :             index_dirRatio2Inv = hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0];
     343             : 
     344     3262809 :             masa_compensate_two_dir_energy_ratio_index( index_dirRatio1Inv, index_dirRatio2Inv, &index_dirRatio1Inv_mod, &index_dirRatio2Inv_mod, hodirac_flag );
     345             : 
     346    16280088 :             for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     347             :             {
     348    13017279 :                 hQMetaData->q_direction[0].band_data[b].energy_ratio_index_mod[m] = index_dirRatio1Inv_mod;
     349    13017279 :                 hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m] = bits_direction_masa[index_dirRatio1Inv_mod];
     350    13017279 :                 bits_dir_raw_pre[0] += hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m];
     351             :             }
     352             : 
     353    16280088 :             for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
     354             :             {
     355    13017279 :                 hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index_mod[m] = index_dirRatio2Inv_mod;
     356    13017279 :                 hQMetaData->q_direction[1].band_data[dir2band].bits_sph_idx[m] = bits_direction_masa[index_dirRatio2Inv_mod];
     357    13017279 :                 bits_dir_raw_pre[1] += hQMetaData->q_direction[1].band_data[dir2band].bits_sph_idx[m];
     358             :             }
     359             : 
     360     3262809 :             dir2band++;
     361             :         }
     362             :         else
     363             :         {
     364    57610315 :             for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     365             :             {
     366    44484071 :                 hQMetaData->q_direction[0].band_data[b].energy_ratio_index_mod[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
     367    44484071 :                 hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m] = bits_direction_masa[hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0]];
     368    44484071 :                 bits_dir_raw_pre[0] += hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m];
     369             :             }
     370             :         }
     371             :     }
     372             : 
     373     3443150 :     if ( hQMetaData->no_directions == 2 )
     374             :     {
     375      323950 :         no_TF = hQMetaData->q_direction[0].cfg.nbands * hQMetaData->q_direction[0].cfg.nblocks + hQMetaData->q_direction[1].cfg.nbands * hQMetaData->q_direction[1].cfg.nblocks;
     376      323950 :         if ( ( all_coherence_zero == 0 ) && ( hQMetaData->metadata_max_bits - bits_no_dirs_coh - 4.3f * no_TF - bits_diff_sum >= MASA_MIN_BITS_SURR_COH ) )
     377             :         {
     378       38535 :             bits_sur_coherence = read_surround_coherence( bitstream, index, hQMetaData );
     379             :         }
     380             :         else
     381             :         {
     382      285415 :             bits_sur_coherence = 0;
     383             :             /*Surround coherence*/
     384     3424980 :             for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     385             :             {
     386     3139565 :                 if ( hQMetaData->surcoh_band_data != NULL )
     387             :                 {
     388           0 :                     set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
     389             :                 }
     390             :             }
     391             :         }
     392      323950 :         bits_no_dirs_coh += bits_sur_coherence;
     393      323950 :         total_bits_1dir = ( ( hQMetaData->metadata_max_bits - bits_no_dirs_coh ) * hQMetaData->q_direction[0].cfg.nbands * hQMetaData->q_direction[0].cfg.nblocks ) / no_TF;
     394             :     }
     395             :     else
     396             :     {
     397     3119200 :         no_TF = hQMetaData->q_direction[0].cfg.nbands * hQMetaData->q_direction[0].cfg.nblocks;
     398     3119200 :         if ( ( all_coherence_zero == 0 ) && ( hQMetaData->metadata_max_bits - bits_no_dirs_coh - 4.3f * no_TF - bits_diff_sum >= MASA_MIN_BITS_SURR_COH ) )
     399             :         {
     400      519797 :             bits_sur_coherence = read_surround_coherence( bitstream, index, hQMetaData );
     401             :         }
     402             :         else
     403             :         {
     404     2599403 :             bits_sur_coherence = 0;
     405             :             /*Surround coherence*/
     406    10635931 :             for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     407             :             {
     408     8036528 :                 if ( hQMetaData->surcoh_band_data != NULL )
     409             :                 {
     410      147051 :                     set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
     411             :                 }
     412             :             }
     413             :         }
     414     3119200 :         bits_no_dirs_coh += bits_sur_coherence;
     415             : 
     416     3119200 :         total_bits_1dir = hQMetaData->metadata_max_bits - bits_no_dirs_coh;
     417             :     }
     418             : 
     419     3443150 :     bits_dir_target = 0;
     420     3443150 :     bits_dir_used = 0;
     421             : 
     422     7210250 :     for ( d = 0; d < hQMetaData->no_directions; d++ )
     423             :     {
     424     3767100 :         q_direction = &hQMetaData->q_direction[d];
     425     3767100 :         nbands = q_direction->cfg.nbands;
     426     3767100 :         nblocks = q_direction->cfg.nblocks;
     427     3767100 :         start_band = q_direction->cfg.start_band;
     428             : 
     429     3767100 :         diffuseness_index_max_ec_frame = diffuseness_index_max_ec_frame_pre[0];
     430     3767100 :         if ( d == 0 )
     431             :         {
     432     3443150 :             bits_diff = bits_diff_sum;
     433             :         }
     434             :         else
     435             :         {
     436      323950 :             bits_diff = 0;
     437             :         }
     438     3767100 :         bits_dir_raw = bits_dir_raw_pre[d];
     439             : 
     440             :         /* Read coherence, if any */
     441     3767100 :         bits_coherence = 0;
     442             : 
     443     3767100 :         if ( all_coherence_zero == 0 )
     444             :         {
     445      601608 :             bits_coherence = read_coherence_data( bitstream, index, hQMetaData, d, 0 );
     446             :         }
     447             :         else
     448             :         {
     449             :             /*Surround coherence*/
     450    17457445 :             for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     451             :             {
     452    14291953 :                 if ( hQMetaData->surcoh_band_data != NULL )
     453             :                 {
     454      123346 :                     set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
     455             :                 }
     456             : 
     457    14291953 :                 if ( hQMetaData->q_direction[d].coherence_band_data != NULL )
     458             :                 {
     459      123346 :                     set_c( (int8_t *) hQMetaData->q_direction[d].coherence_band_data[b].spread_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
     460             :                 }
     461             :             }
     462             :         }
     463             : 
     464             :         /* Read 2D signaling*/
     465     3767100 :         q_direction->not_in_2D = bitstream[( *index )--];
     466     3767100 :         signal_bits = 1;
     467             : 
     468             :         /* Read EC signaling */
     469     3767100 :         ec_flag = 0;
     470     3767100 :         if ( total_bits_1dir + bits_sur_coherence <= hQMetaData->qmetadata_max_bit_req )
     471             :         {
     472     2627744 :             ec_flag = bitstream[( *index )--];
     473     2627744 :             signal_bits++;
     474     2627744 :             if ( nblocks > 1 )
     475             :             {
     476     1893799 :                 if ( ec_flag )
     477             :                 {
     478      185894 :                     ec_flag += bitstream[( *index )--];
     479      185894 :                     signal_bits++;
     480             :                 }
     481             :             }
     482             :         }
     483             : 
     484             :         /* Decode quantized directions frame-wise */
     485     3767100 :         if ( ec_flag == 0 ) /* EC 1*/
     486             :         {
     487     3477942 :             bits_dir = 0;
     488     3477942 :             raw_flag[0] = bitstream[( *index )--];
     489     3477942 :             bits_dir++;
     490             : 
     491     3477942 :             if ( raw_flag[0] == 0 )
     492             :             {
     493     2459774 :                 bits_dir += ivas_qmetadata_entropy_decode_dir( q_direction, bitstream, index, diffuseness_index_max_ec_frame, nbands, start_band, 0 );
     494             :             }
     495             :             else
     496             :             {
     497     1018168 :                 bits_dir += ivas_qmetadata_raw_decode_dir( q_direction, bitstream, index, nbands, start_band, 0 );
     498             :             }
     499             :         }
     500             :         /* Decode quantized directions band-wise */
     501      289158 :         else if ( ec_flag == 1 && ( nblocks > 1 ) ) /* EC2 */
     502             :         {
     503       45462 :             bits_dir = 0;
     504      214656 :             for ( b = start_band; b < nbands; b++ )
     505             :             {
     506      169194 :                 raw_flag[b] = bitstream[( *index )--];
     507      169194 :                 bits_dir++;
     508             :             }
     509             : 
     510             :             /* Read EC bits*/
     511       45462 :             diff_bits = bits_diff + bits_coherence + signal_bits - total_bits_1dir;
     512             : 
     513      214656 :             for ( b = start_band; b < nbands; b++ )
     514             :             {
     515      169194 :                 if ( raw_flag[b] == 0 )
     516             :                 {
     517       96453 :                     bits_dir += ivas_qmetadata_entropy_decode_dir( q_direction, bitstream, index, diffuseness_index_max_ec_frame, b + 1, b, 0 );
     518             :                 }
     519             :                 else
     520             :                 {
     521       72741 :                     diff_bits += q_direction->band_data[b].bits_sph_idx[0] * q_direction->cfg.nblocks;
     522             :                 }
     523             :             }
     524       45462 :             diff_bits += bits_dir;
     525             : 
     526             :             /* Small requantization?*/
     527       45462 :             if ( q_direction->not_in_2D > 0 )
     528             :             {
     529             :                 /* This is not an ideal solution but mirrors better encoder */
     530             :                 int16_t i, j;
     531             :                 uint16_t bits_temp[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
     532             : 
     533      144787 :                 for ( i = q_direction->cfg.start_band; i < q_direction->cfg.nbands; i++ )
     534             :                 {
     535      582845 :                     for ( j = 0; j < q_direction->cfg.nblocks; j++ )
     536             :                     {
     537      466276 :                         bits_temp[i][j] = q_direction->band_data[i].bits_sph_idx[j];
     538             :                     }
     539             :                 }
     540             : 
     541       28218 :                 small_reduction_direction( q_direction, bits_temp, raw_flag, &diff_bits );
     542             : 
     543      144787 :                 for ( i = q_direction->cfg.start_band; i < q_direction->cfg.nbands; i++ )
     544             :                 {
     545      582845 :                     for ( j = 0; j < q_direction->cfg.nblocks; j++ )
     546             :                     {
     547      466276 :                         q_direction->band_data[i].bits_sph_idx[j] = bits_temp[i][j];
     548             :                     }
     549             :                 }
     550             :             }
     551             : 
     552             : #ifdef DEBUGGING
     553             :             assert( ( diff_bits <= 0 ) || ( q_direction->not_in_2D == 0 ) );
     554             : #endif
     555             :             /* Read raw-coded bits*/
     556      214656 :             for ( b = start_band; b < nbands; b++ )
     557             :             {
     558      169194 :                 if ( raw_flag[b] )
     559             :                 {
     560       72741 :                     bits_dir += ivas_qmetadata_raw_decode_dir( q_direction, bitstream, index, b + 1, b, 0 );
     561             :                 }
     562             :             }
     563             :         }
     564             :         /* Decode requantized directions */
     565             :         else /* EC3 */
     566             :         {
     567             :             int16_t dummy;
     568      243696 :             ec_flag = 2;
     569             : 
     570      243696 :             if ( hQMetaData->is_masa_ivas_format == 0 )
     571             :             {
     572       87166 :                 reduce_bits = bits_dir_raw - ( total_bits_1dir - bits_diff - bits_coherence - signal_bits );
     573       87166 :                 ind_order[0] = -1;
     574             :             }
     575             :             else
     576             :             {
     577      156530 :                 ind_order[0] = 0;
     578      156530 :                 reduce_bits = min( nbands * nblocks + MASA_BIT_REDUCT_PARAM, bits_dir_raw - ( total_bits_1dir - bits_diff - bits_coherence - signal_bits ) );
     579      156530 :                 if ( reduce_bits > bits_dir_raw - nbands * nblocks )
     580             :                 {
     581         465 :                     reduce_bits = bits_dir_raw - nbands * nblocks;
     582             :                 }
     583             :             }
     584      243696 :             only_reduce_bits_direction( &dummy, q_direction, reduce_bits, nbands, nblocks, ind_order );
     585             : 
     586             :             /* Read directions */
     587      243696 :             bits_dir = read_directions( q_direction, (uint8_t) nbands, (uint8_t) nblocks, bitstream, index, ind_order );
     588             :         }
     589             : 
     590     3767100 :         if ( bits_coherence > 0 )
     591             :         {
     592      598675 :             if ( nblocks > 1 )
     593             :             {
     594      548160 :                 decode_spread_coherence( hQMetaData, d, nblocks, 0 );
     595             :             }
     596             :         }
     597             :         else
     598             :         {
     599    17474046 :             for ( b = start_band; b < nbands; b++ )
     600             :             {
     601    14305621 :                 if ( q_direction->coherence_band_data != NULL )
     602             :                 {
     603      137014 :                     set_c( (int8_t *) q_direction->coherence_band_data[b].spread_coherence, 0, nblocks );
     604             :                 }
     605             :             }
     606             :         }
     607     3767100 :         if ( d == 0 )
     608             :         {
     609     3443150 :             total_bits_1dir = hQMetaData->metadata_max_bits - ( start_index_0 - *index );
     610             :         }
     611             : 
     612     3767100 :         bits_dir_target += bits_dir_raw;
     613     3767100 :         bits_dir_used += bits_dir;
     614             : 
     615             : #ifdef DEBUG_MODE_QMETADATA
     616             :         fprintf( pF, "frame %d: diff %d coh %d surcoh %d ", frame, bits_diff, bits_coherence, bits_sur_coherence );
     617             :         fprintf( pF, "dir %d %d,%d,%d\n", ec_flag, start_index_0 - *index, total_bits_1dir, bits_dir_raw );
     618             : 
     619             :         fprintf( pF_azi, "frame %d/dir/ec %d/%d: ", frame, d, ec_flag );
     620             :         fprintf( pF_ele, "frame %d/dir/ec %d/%d: ", frame, d, ec_flag );
     621             :         fprintf( pF_ratio, "frame %d/dir %d: ", frame, d );
     622             :         fprintf( pF_spcoh, "frame %d/dir %d: ", frame, d );
     623             :         if ( d == 0 )
     624             :         {
     625             :             fprintf( pF_surcoh, "frame %d/dir %d: ", frame, d );
     626             :         }
     627             :         for ( b = start_band; b < nbands; b++ )
     628             :         {
     629             :             for ( m = 0; m < q_direction->cfg.nblocks; m++ )
     630             :             {
     631             :                 fprintf( pF_azi, " %+5.2f ", (int16_t) ( 100.f * q_direction->band_data[b].azimuth[m] ) / 100.f );
     632             :                 fprintf( pF_ele, " %+5.2f ", (int16_t) ( 100.f * q_direction->band_data[b].elevation[m] ) / 100.f );
     633             :                 fprintf( pF_ratio, " %1.3f ", q_direction->band_data[b].energy_ratio[m] );
     634             :                 if ( q_direction->coherence_band_data != NULL )
     635             :                 {
     636             :                     fprintf( pF_spcoh, " %d ", q_direction->coherence_band_data[b].spread_coherence[m] );
     637             :                 }
     638             :                 if ( d == 0 && hQMetaData->surcoh_band_data != NULL )
     639             :                 {
     640             :                     fprintf( pF_surcoh, " %d ", hQMetaData->surcoh_band_data[b].surround_coherence[m] );
     641             :                 }
     642             :             }
     643             :         }
     644             :         fprintf( pF_azi, "\n" );
     645             :         fprintf( pF_ele, "\n" );
     646             :         fprintf( pF_ratio, "\n" );
     647             :         fprintf( pF_spcoh, "\n" );
     648             :         if ( d == 0 )
     649             :         {
     650             :             fprintf( pF_surcoh, "\n" );
     651             :         }
     652             : #endif
     653             :     }
     654             : 
     655             :     /* move 2 dir data to its correct subband */
     656     3443150 :     if ( hQMetaData->no_directions == 2 )
     657             :     {
     658      323950 :         d = hQMetaData->q_direction[1].cfg.nbands - 1;
     659      323950 :         nblocks = hQMetaData->q_direction[0].cfg.nblocks;
     660             : 
     661     3775527 :         for ( b = hQMetaData->q_direction[0].cfg.nbands - 1; b >= 0; b-- )
     662             :         {
     663     3451577 :             if ( hQMetaData->twoDirBands[b] == 1 )
     664             :             {
     665     3262809 :                 mvr2r( hQMetaData->q_direction[1].band_data[d].azimuth, hQMetaData->q_direction[1].band_data[b].azimuth, nblocks );
     666     3262809 :                 mvr2r( hQMetaData->q_direction[1].band_data[d].elevation, hQMetaData->q_direction[1].band_data[b].elevation, nblocks );
     667     3262809 :                 mvr2r( hQMetaData->q_direction[1].band_data[d].energy_ratio, hQMetaData->q_direction[1].band_data[b].energy_ratio, nblocks );
     668             : 
     669     3262809 :                 if ( hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0] < 7 )
     670             :                 {
     671    14261733 :                     for ( m = 0; m < nblocks; m++ )
     672             :                     {
     673    11402973 :                         hQMetaData->q_direction[1].band_data[b].azimuth[m] += hQMetaData->q_direction[0].band_data[b].azimuth[m] - 180;
     674    11402973 :                         if ( hQMetaData->q_direction[1].band_data[b].azimuth[m] >= 180 )
     675             :                         {
     676           0 :                             hQMetaData->q_direction[1].band_data[b].azimuth[m] -= 360;
     677             :                         }
     678    11402973 :                         if ( hQMetaData->q_direction[1].band_data[b].azimuth[m] < -180 )
     679             :                         {
     680      732534 :                             hQMetaData->q_direction[1].band_data[b].azimuth[m] += 360;
     681             :                         }
     682             :                     }
     683             :                 }
     684             : 
     685     3262809 :                 if ( hQMetaData->q_direction[1].coherence_band_data != NULL )
     686             :                 {
     687      123244 :                     mvc2c( hQMetaData->q_direction[1].coherence_band_data[d].spread_coherence, hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence, nblocks );
     688             :                 }
     689     3262809 :                 d--;
     690             :             }
     691             :             else
     692             :             {
     693      188768 :                 set_f( hQMetaData->q_direction[1].band_data[b].azimuth, 0.0f, nblocks );
     694      188768 :                 set_f( hQMetaData->q_direction[1].band_data[b].elevation, 0.0f, nblocks );
     695      188768 :                 set_f( hQMetaData->q_direction[1].band_data[b].energy_ratio, 0.0f, nblocks );
     696             : 
     697      188768 :                 if ( hQMetaData->q_direction[1].coherence_band_data != NULL )
     698             :                 {
     699      188768 :                     set_c( (int8_t *) hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence, 0, nblocks );
     700             :                 }
     701             :             }
     702             :         }
     703             : 
     704             :         /* Scale energy ratios that sum to over one */
     705      323950 :         if ( !hodirac_flag )
     706             :         {
     707      350547 :             for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     708             :             {
     709             :                 float ratioSum;
     710             : 
     711      312012 :                 ratioSum = hQMetaData->q_direction[0].band_data[b].energy_ratio[0] + hQMetaData->q_direction[1].band_data[b].energy_ratio[0];
     712             : 
     713      312012 :                 if ( ratioSum > 1.0f )
     714             :                 {
     715           0 :                     set_f( hQMetaData->q_direction[0].band_data[b].energy_ratio, hQMetaData->q_direction[0].band_data[b].energy_ratio[0] / ratioSum, nblocks );
     716           0 :                     set_f( hQMetaData->q_direction[1].band_data[b].energy_ratio, hQMetaData->q_direction[1].band_data[b].energy_ratio[0] / ratioSum, nblocks );
     717             :                 }
     718             :             }
     719             :         }
     720             :     }
     721             : 
     722             :     /* Store status information for renderer use */
     723     3443150 :     hQMetaData->ec_flag = ec_flag;
     724             : 
     725     3443150 :     hQMetaData->dir_comp_ratio = (float) bits_dir_used / (float) bits_dir_target;
     726             : 
     727     3443150 :     if ( hQMetaData->dir_comp_ratio > 1.0f )
     728             :     {
     729      619336 :         hQMetaData->dir_comp_ratio = 1.0f;
     730             :     }
     731             : 
     732     3443150 :     return ( start_index_0 - *index );
     733             : }
     734             : 
     735             : 
     736             : /*-----------------------------------------------------------------------*
     737             :  * ivas_qmetadata_dec_decode_hr_384_512()
     738             :  *
     739             :  * Main function for decoding Spatial Metadata at HRs
     740             :  *-----------------------------------------------------------------------*/
     741             : 
     742             : /*! r: number of bits read */
     743       54639 : int16_t ivas_qmetadata_dec_decode_hr_384_512(
     744             :     IVAS_QMETADATA_HANDLE hQMetaData,      /* i/o: hQMetaData handle             */
     745             :     uint16_t *bitstream,                   /* i  : bitstream                     */
     746             :     int16_t *index,                        /* i/o: bitstream position            */
     747             :     const SPHERICAL_GRID_DATA *sph_grid16, /* i  : spherical grid for deindexing */
     748             :     const int16_t bits_sph_idx,
     749             :     const int16_t bits_sp_coh,
     750             :     const uint8_t ncoding_bands_config )
     751             : {
     752             :     int16_t d, b, m;
     753             : #ifdef DEBUG_MODE_QMETADATA
     754             :     int16_t bits_diff_sum;
     755             : #endif
     756             :     int16_t nbands, start_band;
     757             :     IVAS_QDIRECTION *q_direction;
     758             :     int16_t start_index_0;
     759             : #ifdef DEBUG_MODE_QMETADATA
     760             :     int16_t bits_no_dirs_coh, bits_sur_coherence;
     761             : #endif
     762             :     uint16_t all_coherence_zero;
     763             :     int16_t p[MASA_MAXIMUM_CODING_SUBBANDS], dif_p[MASA_MAXIMUM_CODING_SUBBANDS];
     764             :     int16_t codedBands, sf_nbands0, sf_nbands1;
     765       54639 :     sf_nbands1 = 1;
     766             : 
     767             : #ifdef DEBUG_MODE_QMETADATA
     768             :     static FILE *pF = NULL;
     769             :     static FILE *pF_azi = NULL;
     770             :     static FILE *pF_ele = NULL;
     771             :     static FILE *pF_ratio = NULL;
     772             :     static FILE *pF_spcoh = NULL;
     773             :     static FILE *pF_surcoh = NULL;
     774             : 
     775             :     if ( pF == NULL )
     776             :         pF = fopen( "./res/qmetadata_dec.txt", "w" );
     777             :     if ( pF_azi == NULL )
     778             :         pF_azi = fopen( "./res/qmetadata_azi_dec.txt", "w" );
     779             :     if ( pF_ele == NULL )
     780             :         pF_ele = fopen( "./res/qmetadata_ele_dec.txt", "w" );
     781             :     if ( pF_ratio == NULL )
     782             :         pF_ratio = fopen( "./res/qmetadata_ratio_dec.txt", "w" );
     783             :     if ( pF_spcoh == NULL )
     784             :         pF_spcoh = fopen( "./res/qmetadata_spcoh_dec.txt", "w" );
     785             :     if ( pF_surcoh == NULL )
     786             :         pF_surcoh = fopen( "./res/qmetadata_surcoh_dec.txt", "w" );
     787             : #endif
     788             : 
     789       54639 :     start_index_0 = *index;
     790             :     /* read number of higher inactive/not encoded  bands */
     791       54639 :     if ( bitstream[( *index )--] )
     792             :     {
     793       32503 :         codedBands = MASA_MAXIMUM_CODING_SUBBANDS - ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 1 ) - 1;
     794             :     }
     795             :     else
     796             :     {
     797       22136 :         codedBands = MASA_MAXIMUM_CODING_SUBBANDS;
     798             :     }
     799       55494 :     for ( b = codedBands; b < ncoding_bands_config; b++ )
     800             :     {
     801        4275 :         for ( m = 0; m < MAX_PARAM_SPATIAL_SUBFRAMES; m++ )
     802             :         {
     803        3420 :             hQMetaData->q_direction[0].band_data[b].azimuth[m] = 0.0f;
     804        3420 :             hQMetaData->q_direction[0].band_data[b].elevation[m] = 0.0f;
     805        3420 :             hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = 0.0f;
     806             : 
     807        3420 :             if ( hQMetaData->coherence_flag && hQMetaData->q_direction[0].coherence_band_data != NULL )
     808             :             {
     809        3420 :                 hQMetaData->q_direction[0].coherence_band_data[b].spread_coherence[m] = 0u;
     810             :             }
     811             : 
     812        3420 :             if ( hQMetaData->no_directions == 2 )
     813             :             {
     814        3420 :                 hQMetaData->q_direction[1].band_data[b].azimuth[m] = 0.0f;
     815        3420 :                 hQMetaData->q_direction[1].band_data[b].elevation[m] = 0.0f;
     816        3420 :                 hQMetaData->q_direction[1].band_data[b].energy_ratio[m] = 0.0f;
     817             : 
     818        3420 :                 if ( hQMetaData->coherence_flag && hQMetaData->q_direction[1].coherence_band_data != NULL )
     819             :                 {
     820        3420 :                     hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence[m] = 0u;
     821             :                 }
     822             :             }
     823             : 
     824        3420 :             if ( hQMetaData->coherence_flag && hQMetaData->surcoh_band_data != NULL )
     825             :             {
     826        3420 :                 hQMetaData->surcoh_band_data[b].surround_coherence[m] = 0u;
     827             :             }
     828             :         }
     829             : 
     830         855 :         if ( hQMetaData->no_directions == 2 )
     831             :         {
     832         855 :             hQMetaData->twoDirBands[b] = 0;
     833             :         }
     834             :     }
     835       54639 :     sf_nbands0 = hQMetaData->q_direction[0].cfg.nbands;
     836             : 
     837       54639 :     hQMetaData->q_direction[0].cfg.nbands = codedBands;
     838             : 
     839             :     /*Coherence flag decoding*/
     840             : #ifdef DEBUG_MODE_QMETADATA
     841             :     bits_no_dirs_coh = 0;
     842             : #endif
     843       54639 :     all_coherence_zero = 1;
     844       54639 :     if ( hQMetaData->coherence_flag )
     845             :     {
     846             :         /* read if coherence is zero */
     847       54639 :         all_coherence_zero = bitstream[( *index )--];
     848             : #ifdef DEBUG_MODE_QMETADATA
     849             :         bits_no_dirs_coh += 1;
     850             : #endif
     851             :     }
     852             : 
     853       54639 :     hQMetaData->all_coherence_zero = (uint8_t) all_coherence_zero;
     854             : 
     855       54639 :     if ( hQMetaData->no_directions == 2 )
     856             :     {
     857        3867 :         set_c( (int8_t *) hQMetaData->twoDirBands, 1, hQMetaData->q_direction[0].cfg.nbands );
     858             :     }
     859             : 
     860       54639 :     if ( bits_sph_idx == 11 && hQMetaData->no_directions == 2 )
     861             :     {
     862             :         /* Read which bands have 2 directions */
     863        2667 :         hQMetaData->q_direction[1].cfg.nbands = hQMetaData->numTwoDirBands;
     864        2667 :         sf_nbands1 = hQMetaData->q_direction[1].cfg.nbands;
     865        2667 :         if ( hQMetaData->q_direction[1].cfg.nbands > codedBands )
     866             :         {
     867           0 :             hQMetaData->q_direction[1].cfg.nbands = codedBands;
     868             :         }
     869        2667 :         set_c( (int8_t *) hQMetaData->twoDirBands, 0, hQMetaData->q_direction[0].cfg.nbands );
     870        2667 :         d = *index;
     871        2667 :         dif_p[0] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 );
     872        2667 :         p[0] = dif_p[0];
     873        2667 :         hQMetaData->twoDirBands[p[0]] = 1;
     874       25272 :         for ( b = 1; b < hQMetaData->q_direction[1].cfg.nbands; b++ )
     875             :         {
     876       22605 :             dif_p[b] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 );
     877       22605 :             p[b] = p[b - 1] + dif_p[b] + 1;
     878       22605 :             hQMetaData->twoDirBands[p[b]] = 1;
     879             :         }
     880             : #ifdef DEBUG_MODE_QMETADATA
     881             :         bits_no_dirs_coh += ( d - *index );
     882             : #endif
     883             :     }
     884             : 
     885       54639 :     if ( bits_sph_idx == 16 && hQMetaData->no_directions == 2 )
     886             :     {
     887        1200 :         sf_nbands1 = hQMetaData->q_direction[1].cfg.nbands;
     888        1200 :         if ( hQMetaData->q_direction[1].cfg.nbands > codedBands )
     889             :         {
     890           0 :             hQMetaData->q_direction[1].cfg.nbands = codedBands;
     891             :         }
     892             :     }
     893             : #ifdef DEBUG_MODE_QMETADATA
     894             :     bits_diff_sum =
     895             : #endif
     896       54639 :         ivas_qmetadata_entropy_decode_diffuseness_hr_512( bitstream, index, &( hQMetaData->q_direction[0] ) );
     897             : 
     898       54639 :     if ( hQMetaData->no_directions == 2 )
     899             :     {
     900             : #ifdef DEBUG_MODE_QMETADATA
     901             :         bits_diff_sum +=
     902             : #endif
     903        3867 :             ivas_qmetadata_entropy_decode_diffuseness_hr_512( bitstream, index, &( hQMetaData->q_direction[1] ) );
     904             :     }
     905             : 
     906             : 
     907     1286000 :     for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     908             :     {
     909     6127618 :         for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     910             :         {
     911     4896257 :             hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = 1.0f - diffuseness_reconstructions_hr[hQMetaData->q_direction[0].band_data[b].energy_ratio_index[m]];
     912             :         }
     913             :     }
     914       54639 :     if ( hQMetaData->no_directions == 2 )
     915             :     {
     916             :         float ratioSum;
     917        3867 :         if ( bits_sph_idx == 16 )
     918             :         {
     919       30000 :             for ( b = hQMetaData->q_direction[1].cfg.start_band; b < hQMetaData->q_direction[1].cfg.nbands; b++ )
     920             :             {
     921      144000 :                 for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
     922             :                 {
     923      115200 :                     hQMetaData->q_direction[1].band_data[b].energy_ratio[m] = 1.0f - diffuseness_reconstructions_hr[hQMetaData->q_direction[1].band_data[b].energy_ratio_index[m]];
     924             : 
     925             :                     /* Scale energy ratios that sum to over one */
     926      115200 :                     ratioSum = hQMetaData->q_direction[0].band_data[b].energy_ratio[m] + hQMetaData->q_direction[1].band_data[b].energy_ratio[m];
     927             : 
     928      115200 :                     if ( ratioSum > 1.0f )
     929             :                     {
     930        4107 :                         hQMetaData->q_direction[0].band_data[b].energy_ratio[m] /= ratioSum;
     931        4107 :                         hQMetaData->q_direction[1].band_data[b].energy_ratio[m] /= ratioSum;
     932             :                     }
     933             :                 }
     934             :             }
     935             :         }
     936             :         else
     937             :         {
     938             :             int16_t pos_2dir_band[MASA_MAXIMUM_CODING_SUBBANDS];
     939        2667 :             d = 0;
     940       65820 :             for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     941             :             {
     942       63153 :                 if ( hQMetaData->twoDirBands[b] == 1 )
     943             :                 {
     944       25272 :                     pos_2dir_band[d] = b;
     945       25272 :                     d++;
     946             :                 }
     947             :                 else
     948             :                 {
     949       37881 :                     pos_2dir_band[d] = 0;
     950             :                 }
     951             :             }
     952       27939 :             for ( b = hQMetaData->q_direction[1].cfg.start_band; b < hQMetaData->q_direction[1].cfg.nbands; b++ )
     953             :             {
     954      111132 :                 for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
     955             :                 {
     956       85860 :                     hQMetaData->q_direction[1].band_data[b].energy_ratio[m] = 1.0f - diffuseness_reconstructions_hr[hQMetaData->q_direction[1].band_data[b].energy_ratio_index[m]];
     957             : 
     958       85860 :                     ratioSum = hQMetaData->q_direction[0].band_data[pos_2dir_band[b]].energy_ratio[m] + hQMetaData->q_direction[1].band_data[b].energy_ratio[m];
     959             : 
     960       85860 :                     if ( ratioSum > 1.0f )
     961             :                     {
     962        1029 :                         hQMetaData->q_direction[0].band_data[pos_2dir_band[b]].energy_ratio[m] /= ratioSum;
     963        1029 :                         hQMetaData->q_direction[1].band_data[b].energy_ratio[m] /= ratioSum;
     964             :                     }
     965             :                 }
     966             :             }
     967             :         }
     968             :     }
     969             : 
     970       54639 :     if ( hQMetaData->no_directions == 2 )
     971             :     {
     972       57939 :         for ( b = hQMetaData->q_direction[1].cfg.start_band; b < hQMetaData->q_direction[1].cfg.nbands; b++ )
     973             :         {
     974      255132 :             for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
     975             :             {
     976      201060 :                 hQMetaData->q_direction[1].band_data[b].energy_ratio_index_mod[m] = hQMetaData->q_direction[1].band_data[b].energy_ratio_index[m];
     977      201060 :                 hQMetaData->q_direction[1].band_data[b].bits_sph_idx[m] = bits_sph_idx;
     978             :             }
     979             :         }
     980             :     }
     981             : 
     982     1286000 :     for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     983             :     {
     984     6127618 :         for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     985             :         {
     986     4896257 :             hQMetaData->q_direction[0].band_data[b].energy_ratio_index_mod[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
     987     4896257 :             hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m] = bits_sph_idx;
     988             :         }
     989             :     }
     990             : 
     991       54639 :     if ( all_coherence_zero == 0 )
     992             :     {
     993             : #ifdef DEBUG_MODE_QMETADATA
     994             :         bits_sur_coherence =
     995             : #endif
     996       54633 :             read_surround_coherence_hr( bitstream, index, hQMetaData );
     997             :     }
     998             :     else
     999             :     {
    1000             : #ifdef DEBUG_MODE_QMETADATA
    1001             :         bits_sur_coherence = 0;
    1002             : #endif
    1003             :         /*Surround coherence*/
    1004         150 :         for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
    1005             :         {
    1006         144 :             if ( hQMetaData->surcoh_band_data != NULL )
    1007             :             {
    1008         144 :                 set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
    1009             :             }
    1010             :         }
    1011             :     }
    1012             : #ifdef DEBUG_MODE_QMETADATA
    1013             :     bits_no_dirs_coh += bits_sur_coherence;
    1014             : #endif
    1015             : 
    1016      113145 :     for ( d = 0; d < hQMetaData->no_directions; d++ )
    1017             :     {
    1018       58506 :         q_direction = &hQMetaData->q_direction[d];
    1019       58506 :         nbands = q_direction->cfg.nbands;
    1020       58506 :         start_band = q_direction->cfg.start_band;
    1021             : 
    1022             :         /* Read coherence, if any */
    1023       58506 :         if ( all_coherence_zero == 0 )
    1024             :         {
    1025       58500 :             read_coherence_data_hr_512( bitstream, index, hQMetaData, d, bits_sp_coh );
    1026             :         }
    1027             :         else
    1028             :         {
    1029             :             /*Surround coherence*/
    1030         150 :             for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
    1031             :             {
    1032         144 :                 if ( hQMetaData->surcoh_band_data != NULL )
    1033             :                 {
    1034         144 :                     set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
    1035             :                 }
    1036             : 
    1037         144 :                 if ( hQMetaData->q_direction[d].coherence_band_data != NULL )
    1038             :                 {
    1039         144 :                     set_c( (int8_t *) hQMetaData->q_direction[d].coherence_band_data[b].spread_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
    1040             :                 }
    1041             :             }
    1042             :         }
    1043             : 
    1044             :         /* Decode quantized directions frame-wise */
    1045       58506 :         ivas_qmetadata_raw_decode_dir_512( q_direction, bitstream, index, nbands, start_band, sph_grid16 );
    1046             : 
    1047             : #ifdef DEBUG_MODE_QMETADATA
    1048             :         fprintf( pF, "frame %d: diff %d  surcoh %d ", frame, bits_diff_sum, bits_sur_coherence );
    1049             :         fprintf( pF, "dir %d\n", start_index_0 - *index );
    1050             :         fprintf( pF_azi, "frame %d/dir/ec %d: ", frame, d );
    1051             :         fprintf( pF_ele, "frame %d/dir/ec %d: ", frame, d );
    1052             :         fprintf( pF_spcoh, "frame %d/dir %d: ", frame, d );
    1053             :         fprintf( pF_ratio, "frame %d/dir %d: ", frame, d );
    1054             : 
    1055             :         for ( b = start_band; b < nbands; b++ )
    1056             :         {
    1057             :             for ( m = 0; m < q_direction->cfg.nblocks; m++ )
    1058             :             {
    1059             : 
    1060             :                 fprintf( pF_ratio, " %1.3f ", q_direction->band_data[b].energy_ratio[m] );
    1061             :                 fprintf( pF_azi, " %+5.2f ", (int16_t) ( 100.f * q_direction->band_data[b].azimuth[m] ) / 100.f );
    1062             :                 fprintf( pF_ele, " %+5.2f ", (int16_t) ( 100.f * q_direction->band_data[b].elevation[m] ) / 100.f );
    1063             :                 if ( q_direction->coherence_band_data != NULL )
    1064             :                 {
    1065             :                     fprintf( pF_spcoh, " %d ", q_direction->coherence_band_data[b].spread_coherence[m] );
    1066             :                 }
    1067             :             }
    1068             :         }
    1069             :         fprintf( pF_ratio, "\n" );
    1070             :         fprintf( pF_azi, "\n" );
    1071             :         fprintf( pF_ele, "\n" );
    1072             :         fprintf( pF_spcoh, "\n" );
    1073             : #endif
    1074             :     }
    1075             : 
    1076       54639 :     if ( hQMetaData->no_directions == 2 )
    1077             :     {
    1078             :         /* move 2 dir data to its correct subband */
    1079        3867 :         if ( bits_sph_idx == 11 )
    1080             :         {
    1081             :             int16_t nblocks;
    1082        2667 :             d = hQMetaData->q_direction[1].cfg.nbands - 1;
    1083        2667 :             nblocks = hQMetaData->q_direction[0].cfg.nblocks;
    1084             : 
    1085       65820 :             for ( b = hQMetaData->q_direction[0].cfg.nbands - 1; b >= 0; b-- )
    1086             :             {
    1087       63153 :                 if ( hQMetaData->twoDirBands[b] == 1 )
    1088             :                 {
    1089       25272 :                     mvr2r( hQMetaData->q_direction[1].band_data[d].azimuth, hQMetaData->q_direction[1].band_data[b].azimuth, nblocks );
    1090       25272 :                     mvr2r( hQMetaData->q_direction[1].band_data[d].elevation, hQMetaData->q_direction[1].band_data[b].elevation, nblocks );
    1091       25272 :                     mvr2r( hQMetaData->q_direction[1].band_data[d].energy_ratio, hQMetaData->q_direction[1].band_data[b].energy_ratio, nblocks );
    1092             : 
    1093             : 
    1094       25272 :                     if ( hQMetaData->q_direction[1].coherence_band_data != NULL )
    1095             :                     {
    1096       25272 :                         mvc2c( hQMetaData->q_direction[1].coherence_band_data[d].spread_coherence, hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence, nblocks );
    1097             :                     }
    1098       25272 :                     d--;
    1099             :                 }
    1100             :                 else
    1101             :                 {
    1102       37881 :                     set_f( hQMetaData->q_direction[1].band_data[b].azimuth, 0.0f, nblocks );
    1103       37881 :                     set_f( hQMetaData->q_direction[1].band_data[b].elevation, 0.0f, nblocks );
    1104       37881 :                     set_f( hQMetaData->q_direction[1].band_data[b].energy_ratio, 0.0f, nblocks );
    1105             : 
    1106       37881 :                     if ( hQMetaData->q_direction[1].coherence_band_data != NULL )
    1107             :                     {
    1108       37881 :                         set_c( (int8_t *) hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence, 0, nblocks );
    1109             :                     }
    1110             :                 }
    1111             :             }
    1112             :         }
    1113             : 
    1114             :         /* Scale energy ratios that sum to over one */
    1115       95820 :         for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
    1116             :         {
    1117      430578 :             for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
    1118             :             {
    1119             :                 float ratioSum;
    1120             : 
    1121      338625 :                 ratioSum = hQMetaData->q_direction[0].band_data[b].energy_ratio[m] + hQMetaData->q_direction[1].band_data[b].energy_ratio[m];
    1122             : 
    1123      338625 :                 if ( ratioSum > 1.0f )
    1124             :                 {
    1125             :                     float ratioSumInv;
    1126           0 :                     ratioSumInv = 1.0f / ratioSum;
    1127           0 :                     hQMetaData->q_direction[0].band_data[b].energy_ratio[m] *= ratioSumInv;
    1128           0 :                     hQMetaData->q_direction[1].band_data[b].energy_ratio[m] *= ratioSumInv;
    1129             :                 }
    1130             :             }
    1131             :         }
    1132             :     }
    1133             : 
    1134             : #ifdef DEBUG_MODE_QMETADATA
    1135             :     for ( d = 0; d < hQMetaData->no_directions; d++ )
    1136             :     {
    1137             :         q_direction = &hQMetaData->q_direction[d];
    1138             :         nbands = q_direction->cfg.nbands;
    1139             :         start_band = q_direction->cfg.start_band;
    1140             : 
    1141             : 
    1142             :         if ( d == 0 )
    1143             :         {
    1144             :             fprintf( pF_surcoh, "frame %d/dir %d: ", frame, d );
    1145             :         }
    1146             :         for ( b = start_band; b < nbands; b++ )
    1147             :         {
    1148             :             for ( m = 0; m < q_direction->cfg.nblocks; m++ )
    1149             :             {
    1150             : 
    1151             : 
    1152             :                 if ( d == 0 && hQMetaData->surcoh_band_data != NULL )
    1153             :                 {
    1154             :                     fprintf( pF_surcoh, " %d ", hQMetaData->surcoh_band_data[b].surround_coherence[m] );
    1155             :                 }
    1156             :             }
    1157             :         }
    1158             : 
    1159             : 
    1160             :         if ( d == 0 )
    1161             :         {
    1162             :             fprintf( pF_surcoh, "\n" );
    1163             :         }
    1164             :     }
    1165             : #endif
    1166             :     /* Store status information for renderer use */
    1167       54639 :     hQMetaData->ec_flag = 0;
    1168             : 
    1169       54639 :     hQMetaData->dir_comp_ratio = 1.0f;
    1170             : 
    1171       54639 :     if ( hQMetaData->dir_comp_ratio > 1.0f )
    1172             :     {
    1173           0 :         hQMetaData->dir_comp_ratio = 1.0f;
    1174             :     }
    1175             : 
    1176       54639 :     hQMetaData->q_direction[0].cfg.nbands = sf_nbands0;
    1177       54639 :     if ( hQMetaData->no_directions == 2 )
    1178             :     {
    1179        3867 :         hQMetaData->q_direction[1].cfg.nbands = sf_nbands1;
    1180             :     }
    1181             : 
    1182       54639 :     return ( start_index_0 - *index );
    1183             : }
    1184             : 
    1185             : 
    1186             : /*-----------------------------------------------------------------------*
    1187             :  * ivas_qmetadata_dec_sid_decode()
    1188             :  *
    1189             :  * Main function for decoding SID for Spatial Metadata
    1190             :  *-----------------------------------------------------------------------*/
    1191             : 
    1192             : /*! r: number of bits written */
    1193        2433 : int16_t ivas_qmetadata_dec_sid_decode(
    1194             :     IVAS_QMETADATA_HANDLE hQMetaData, /* i/o: q_metadata handle                    */
    1195             :     uint16_t *bitstream,              /* i  : bitstream                            */
    1196             :     int16_t *index,                   /* i/o: bitstream position                   */
    1197             :     const int16_t nchan_transport,    /* i  : number of transport channels         */
    1198             :     int16_t *element_mode,            /* o  : element mode                         */
    1199             :     const int16_t ivas_format         /* i  : IVAS format                          */
    1200             : )
    1201             : {
    1202             :     int16_t b, m, i;
    1203             :     uint16_t value;
    1204             :     uint16_t diffuseness_index[DIRAC_MAX_NBANDS];
    1205             :     int16_t nbands, nblocks, start_band;
    1206             :     IVAS_QDIRECTION *q_direction;
    1207             :     int16_t start_index;
    1208             :     float avg_elevation, avg_azimuth;
    1209             :     float avg_direction_vector[3];
    1210             :     float direction_vector[3];
    1211             :     int16_t metadata_sid_bits; /* bits allocated to SID for metadata */
    1212             :     int16_t bits_delta, bits_dir;
    1213             :     int16_t sba_spar_bitlen;
    1214             : #ifdef DEBUG_MODE_QMETADATA
    1215             :     static FILE *pF = NULL;
    1216             :     static FILE *pF_azi = NULL;
    1217             :     static FILE *pF_ele = NULL;
    1218             :     static FILE *pF_ratio = NULL;
    1219             : 
    1220             :     if ( pF == NULL )
    1221             :         pF = fopen( "./res/qmetadata_sid_dec.txt", "w" );
    1222             :     if ( pF_azi == NULL )
    1223             :         pF_azi = fopen( "./res/qmetadata_sid_azi_dec.txt", "w" );
    1224             :     if ( pF_ele == NULL )
    1225             :         pF_ele = fopen( "./res/qmetadata_sid_ele_dec.txt", "w" );
    1226             :     if ( pF_ratio == NULL )
    1227             :         pF_ratio = fopen( "./res/qmetadata_sid_ratio_dec.txt", "w" );
    1228             : #endif
    1229             : 
    1230        2433 :     if ( ivas_format == SBA_FORMAT )
    1231             :     {
    1232        1818 :         sba_spar_bitlen = ivas_sba_spar_sid_bitlen( nchan_transport );
    1233        1818 :         metadata_sid_bits = (int16_t) ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - sba_spar_bitlen - SID_FORMAT_NBITS - SBA_ORDER_BITS - SBA_PLANAR_BITS - 1; /* -1 for inactive mode header bit*/
    1234             :     }
    1235             :     else
    1236             :     {
    1237         615 :         metadata_sid_bits = ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - SID_FORMAT_NBITS;
    1238             :     }
    1239             : 
    1240        2433 :     start_index = *index;
    1241             : 
    1242             :     /* read MASA SID descriptor */
    1243        2433 :     if ( ivas_format == MASA_FORMAT && nchan_transport == 2 )
    1244             :     {
    1245         222 :         b = bitstream[( *index )--];
    1246         222 :         if ( b )
    1247             :         {
    1248           0 :             *element_mode = IVAS_CPE_MDCT;
    1249             :         }
    1250             :         else
    1251             :         {
    1252         222 :             *element_mode = IVAS_CPE_DFT;
    1253             :         }
    1254             :     }
    1255             : 
    1256             :     /* Fix configuration for SID */
    1257        2433 :     q_direction = &hQMetaData->q_direction[0]; /* only 1 direction */
    1258        2433 :     if ( ivas_format == SBA_FORMAT )
    1259             :     {
    1260        1818 :         nbands = DIRAC_DTX_BANDS; /* only 2 bands transmitted */
    1261             :     }
    1262             :     else
    1263             :     {
    1264         615 :         nbands = 5; /* only 5 bands transmitted */
    1265             :     }
    1266             : 
    1267        2433 :     nblocks = q_direction->cfg.nblocks; /* only 1 block transmitted but up to 4 blocks re-generated */
    1268        2433 :     start_band = 0;                     /* start from band 0 */
    1269             : 
    1270             :     /* Read 2D signaling*/
    1271        2433 :     if ( ivas_format != SBA_FORMAT )
    1272             :     {
    1273         615 :         q_direction->not_in_2D = bitstream[( *index )--];
    1274             :     }
    1275             :     else
    1276             :     {
    1277        1818 :         q_direction->not_in_2D = 1;
    1278             :     }
    1279             : 
    1280        2433 :     bits_dir = 0;
    1281        2433 :     if ( ivas_format != SBA_FORMAT )
    1282             :     {
    1283             :         /* Decode diffuseness*/
    1284        3690 :         for ( b = start_band; b < nbands; b++ )
    1285             :         {
    1286        3075 :             diffuseness_index[b] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS - 4 ) + 4;
    1287        3075 :             q_direction->band_data[b].energy_ratio_index[0] = diffuseness_index[b];
    1288        3075 :             q_direction->band_data[b].bits_sph_idx[0] = bits_direction_masa[diffuseness_index[b]];
    1289        3075 :             bits_dir += q_direction->band_data[b].bits_sph_idx[0];
    1290        3075 :             q_direction->band_data[b].azimuth_m_alphabet[0] = no_phi_masa[q_direction->band_data[b].bits_sph_idx[0] - 1][0];
    1291             :         }
    1292             : 
    1293         615 :         bits_delta = metadata_sid_bits - ( start_index - *index ) - bits_dir; /* bit_diff is already read */
    1294             : 
    1295         615 :         if ( bits_delta > 0 )
    1296             :         {
    1297        2934 :             while ( bits_delta > 0 )
    1298             :             {
    1299       12618 :                 for ( b = start_band; b < nbands && ( bits_delta > 0 ); b++ )
    1300             :                 {
    1301       10299 :                     if ( q_direction->band_data[b].bits_sph_idx[0] < 11 )
    1302             :                     {
    1303       10299 :                         bits_delta -= 1;
    1304       10299 :                         q_direction->band_data[b].bits_sph_idx[0]++;
    1305             :                     }
    1306             :                 }
    1307             :             }
    1308             : 
    1309         615 :             if ( q_direction->not_in_2D == 0 )
    1310             :             {
    1311           0 :                 for ( b = start_band; b < nbands; b++ )
    1312             :                 {
    1313           0 :                     q_direction->band_data[b].azimuth_m_alphabet[0] = 1 << ( min( 8, q_direction->band_data[b].bits_sph_idx[0] ) );
    1314             :                 }
    1315             :             }
    1316             :         }
    1317             :         else
    1318             :         {
    1319           0 :             while ( bits_delta < 0 )
    1320             :             {
    1321           0 :                 for ( b = nbands - 1; b >= start_band && ( bits_delta < 0 ); b-- )
    1322             :                 {
    1323           0 :                     if ( q_direction->band_data[b].bits_sph_idx[0] >= 4 )
    1324             :                     {
    1325           0 :                         bits_delta += 1;
    1326           0 :                         q_direction->band_data[b].bits_sph_idx[0]--;
    1327             :                     }
    1328             :                 }
    1329             : 
    1330           0 :                 if ( q_direction->not_in_2D == 0 )
    1331             :                 {
    1332           0 :                     for ( b = start_band; b < nbands; b++ )
    1333             :                     {
    1334           0 :                         q_direction->band_data[b].azimuth_m_alphabet[0] = 1 << ( min( 8, q_direction->band_data[b].bits_sph_idx[0] ) );
    1335             :                     }
    1336             :                 }
    1337             :             }
    1338             :         }
    1339             :     }
    1340             :     else
    1341             :     {
    1342             :         /* Decode diffuseness*/
    1343        5454 :         for ( b = start_band; b < nbands; b++ )
    1344             :         {
    1345        3636 :             diffuseness_index[b] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS - 4 ) + 4;
    1346        3636 :             q_direction->band_data[b].energy_ratio_index[0] = diffuseness_index[b];
    1347        3636 :             q_direction->band_data[b].bits_sph_idx[0] = bits_direction_masa[diffuseness_index[b]];
    1348        3636 :             q_direction->band_data[b].azimuth_m_alphabet[0] = no_phi_masa[q_direction->band_data[b].bits_sph_idx[0] - 1][0];
    1349             :         }
    1350             :     }
    1351             : 
    1352        9144 :     for ( b = start_band; b < nbands; b++ )
    1353             :     {
    1354        6711 :         q_direction->band_data[b].energy_ratio[0] = 1.0f - diffuseness_reconstructions[diffuseness_index[b]];
    1355       32823 :         for ( i = 0; i < nblocks; i++ )
    1356             :         {
    1357       26112 :             q_direction->band_data[b].energy_ratio[i] = q_direction->band_data[b].energy_ratio[0];
    1358             :         }
    1359             :     }
    1360             : 
    1361             :     /* Decoder DOAs*/
    1362        2433 :     if ( q_direction->not_in_2D > 0 )
    1363             :     {
    1364        9144 :         for ( b = start_band; b < nbands; b++ )
    1365             :         {
    1366        6711 :             value = 0;
    1367       43690 :             for ( i = 0; i < q_direction->band_data[b].bits_sph_idx[0]; i++ )
    1368             :             {
    1369       36979 :                 value = ( value << 1 ) + bitstream[( *index )--];
    1370             :             }
    1371       32823 :             for ( i = 0; i < nblocks; i++ )
    1372             :             {
    1373       26112 :                 q_direction->band_data[b].spherical_index[i] = value;
    1374             :             }
    1375             : 
    1376        6711 :             deindex_spherical_component( q_direction->band_data[b].spherical_index[0], &avg_azimuth, &avg_elevation, &q_direction->band_data[b].azimuth_index[0], &q_direction->band_data[b].elevation_index[0], q_direction->band_data[b].bits_sph_idx[0], q_direction->cfg.mc_ls_setup );
    1377             : 
    1378        6711 :             ivas_qmetadata_azimuth_elevation_to_direction_vector( avg_azimuth, avg_elevation, avg_direction_vector );
    1379        6711 :             ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[b].azimuth[nblocks - 1], q_direction->band_data[b].elevation[nblocks - 1], direction_vector );
    1380             : 
    1381       26112 :             for ( m = 0; m < nblocks - 1; m++ )
    1382             :             {
    1383       19401 :                 v_add( direction_vector, avg_direction_vector, direction_vector, 3 );
    1384       19401 :                 ivas_qmetadata_direction_vector_to_azimuth_elevation( direction_vector, &q_direction->band_data[b].azimuth[m], &q_direction->band_data[b].elevation[m] );
    1385             :             }
    1386             : 
    1387        6711 :             ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector, &q_direction->band_data[b].azimuth[nblocks - 1], &q_direction->band_data[b].elevation[nblocks - 1] );
    1388             :         }
    1389             :     }
    1390             :     else
    1391             :     {
    1392           0 :         for ( b = start_band; b < nbands; b++ )
    1393             :         {
    1394           0 :             if ( ivas_format == SBA_FORMAT )
    1395             :             {
    1396           0 :                 q_direction->band_data[b].azimuth_m_alphabet[0] = 1 << ( min( 5, q_direction->band_data[b].bits_sph_idx[0] ) );
    1397             :             }
    1398             : 
    1399           0 :             q_direction->band_data[b].azimuth_index[0] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, q_direction->band_data[b].azimuth_m_alphabet[0] );
    1400           0 :             q_direction->band_data[b].azimuth_index[0] = ivas_qmetadata_dereorder_generic( q_direction->band_data[b].azimuth_index[0] ) + ( q_direction->band_data[b].azimuth_m_alphabet[0] >> 1 );
    1401           0 :             avg_azimuth = 360.0f / (float) ( q_direction->band_data[b].azimuth_m_alphabet[0] ) * q_direction->band_data[b].azimuth_index[0] - 180;
    1402             : 
    1403           0 :             avg_elevation = 0.f;
    1404             : 
    1405           0 :             ivas_qmetadata_azimuth_elevation_to_direction_vector( avg_azimuth, avg_elevation, avg_direction_vector );
    1406           0 :             ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[b].azimuth[nblocks - 1], q_direction->band_data[b].elevation[nblocks - 1], direction_vector );
    1407             : 
    1408           0 :             for ( m = 0; m < nblocks - 1; m++ )
    1409             :             {
    1410           0 :                 v_add( direction_vector, avg_direction_vector, direction_vector, 3 );
    1411           0 :                 ivas_qmetadata_direction_vector_to_azimuth_elevation( direction_vector, &q_direction->band_data[b].azimuth[m], &q_direction->band_data[b].elevation[m] );
    1412             :             }
    1413             : 
    1414           0 :             ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector, &q_direction->band_data[b].azimuth[nblocks - 1], &q_direction->band_data[b].elevation[nblocks - 1] );
    1415             : 
    1416           0 :             for ( i = 0; i < nblocks; i++ )
    1417             :             {
    1418           0 :                 q_direction->band_data[b].spherical_index[i] = q_direction->band_data[b].azimuth_index[0];
    1419             :             }
    1420             :         }
    1421             :     }
    1422             : 
    1423             : 
    1424             :     /*Read filling bits*/
    1425        9514 :     while ( start_index - *index < metadata_sid_bits )
    1426             :     {
    1427        7081 :         b = bitstream[( *index )--];
    1428             :     }
    1429             : 
    1430             : #ifdef DEBUG_MODE_QMETADATA
    1431             :     fprintf( pF, "frame %d: all %d  ", frame, start_index - *index );
    1432             : 
    1433             :     fprintf( pF_azi, "frame %d SID: ", frame );
    1434             :     fprintf( pF_ele, "frame %d SID: ", frame );
    1435             :     fprintf( pF_ratio, "frame %d SID: ", frame );
    1436             : 
    1437             :     for ( b = start_band; b < nbands; b++ )
    1438             :     {
    1439             :         for ( m = 0; m < nblocks; m++ )
    1440             :         {
    1441             :             fprintf( pF_azi, " %+5.2f ", (int16_t) ( 100.f * q_direction->band_data[b].azimuth[m] ) / 100.f );
    1442             :             fprintf( pF_ele, " %+5.2f ", (int16_t) ( 100.f * q_direction->band_data[b].elevation[m] ) / 100.f );
    1443             :             fprintf( pF_ratio, " %1.3f ", q_direction->band_data[b].energy_ratio[m] );
    1444             :         }
    1445             :     }
    1446             :     fprintf( pF_azi, "\n" );
    1447             :     fprintf( pF_ele, "\n" );
    1448             :     fprintf( pF_ratio, "\n" );
    1449             : 
    1450             : #endif
    1451        2433 :     return ( start_index - *index );
    1452             : }
    1453             : 
    1454             : 
    1455             : /*-----------------------------------------------------------------------*
    1456             :  * Local function definitions for diffuseness/energy ratios
    1457             :  *-----------------------------------------------------------------------*/
    1458             : 
    1459     4115520 : static int16_t ivas_diffuseness_huff_ec_decode(
    1460             :     const uint16_t *bitstream,
    1461             :     int16_t *index,
    1462             :     int16_t av )
    1463             : {
    1464             :     int16_t val;
    1465             : 
    1466     4115520 :     val = 0;
    1467     9106441 :     while ( val <= DIFF_EC_HUFF_GR0_LIMIT )
    1468             :     {
    1469     9103510 :         if ( bitstream[( *index )--] == 1 )
    1470             :         {
    1471     4990921 :             val++;
    1472             :         }
    1473             :         else
    1474             :         {
    1475     4112589 :             break;
    1476             :         }
    1477             :     }
    1478             : 
    1479     4115520 :     if ( val == DIFF_EC_HUFF_GR0_LIMIT + 1 )
    1480             :     {
    1481        2931 :         val += 2 * bitstream[( *index )];
    1482        2931 :         ( *index )--;
    1483        2931 :         val += bitstream[( *index )];
    1484        2931 :         ( *index )--;
    1485             :     }
    1486             : 
    1487     4115520 :     if ( val % 2 == 0 )
    1488             :     {
    1489     2605055 :         return -( val / 2 ) + av;
    1490             :     }
    1491             :     else
    1492             :     {
    1493     1510465 :         return ( val + 1 ) / 2 + av;
    1494             :     }
    1495             : }
    1496             : 
    1497             : 
    1498             : /*-------------------------------------------------------------------*
    1499             :  * ivas_qmetadata_entropy_decode_diffuseness()
    1500             :  *
    1501             :  *
    1502             :  *-------------------------------------------------------------------*/
    1503             : 
    1504     3443150 : static int16_t ivas_qmetadata_entropy_decode_diffuseness(
    1505             :     uint16_t *bitstream, /* i  : bitstream               */
    1506             :     int16_t *index,
    1507             :     IVAS_QDIRECTION *q_direction,
    1508             :     uint16_t *diffuseness_index_max_ec_frame )
    1509             : {
    1510             :     int16_t b;
    1511             :     uint16_t dif_min;
    1512             :     int16_t index_start;
    1513             :     int16_t nbands;
    1514             :     int16_t start_band;
    1515             : 
    1516     3443150 :     index_start = *index;
    1517     3443150 :     nbands = q_direction->cfg.nbands;
    1518     3443150 :     start_band = q_direction->cfg.start_band;
    1519             : 
    1520             :     /* diffuseness decoding */
    1521             :     /* Handle one band as special case*/
    1522     3443150 :     if ( nbands == 1 )
    1523             :     {
    1524       71319 :         q_direction->band_data[0].energy_ratio_index[0] = 0;
    1525      285276 :         for ( b = 0; b < MASA_BITS_ER; b++ )
    1526             :         {
    1527      213957 :             q_direction->band_data[0].energy_ratio_index[0] = ( q_direction->band_data[0].energy_ratio_index[0] << 1 ) + bitstream[( *index )--];
    1528             :         }
    1529       71319 :         *diffuseness_index_max_ec_frame = 5;
    1530             : 
    1531       71319 :         return MASA_BITS_ER;
    1532             :     }
    1533             : 
    1534     3371831 :     if ( bitstream[( *index )--] == 0 ) /* dif_use_raw_coding */
    1535             :     {
    1536             :         /* Decode with similarity strategy with low band count. On higher band counts, decode with Huffman-coding strategy. */
    1537     1130161 :         if ( nbands < DIFF_EC_HUFF_BAND_LIMIT )
    1538             :         {
    1539      808745 :             if ( bitstream[( *index )--] != 0 ) /* dif_have_unique_value */
    1540             :             {
    1541      335983 :                 dif_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS ); /* dif_unique_value */
    1542             : 
    1543     1324113 :                 for ( b = start_band; b < nbands; b++ )
    1544             :                 {
    1545      988130 :                     q_direction->band_data[b].energy_ratio_index[0] = dif_min;
    1546             :                 }
    1547             :             }
    1548             :             else /* all diffuseness values are dif_min_value or dif_min_value + 1 */
    1549             :             {
    1550      472762 :                 dif_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS - 1 ); /* dif_min_value */
    1551             : 
    1552     2073765 :                 for ( b = start_band; b < nbands; b++ )
    1553             :                 {
    1554     1601003 :                     q_direction->band_data[b].energy_ratio_index[0] = dif_min + bitstream[( *index )--]; /* dif_bit_offset_values */
    1555             :                 }
    1556             :             }
    1557             :         }
    1558             :         else
    1559             :         {
    1560             :             int16_t av;
    1561             : 
    1562             :             /* read average on 3 bits*/
    1563      321416 :             av = 0;
    1564     1285664 :             for ( b = 0; b < MASA_BITS_ER; b++ )
    1565             :             {
    1566      964248 :                 av += bitstream[( *index )--] * ( 1 << ( MASA_BITS_ER - 1 - b ) );
    1567             :             }
    1568             : 
    1569      321416 :             dif_min = DIRAC_DIFFUSE_LEVELS;
    1570             :             /* read average removed data (average is added inside)*/
    1571     4436936 :             for ( b = start_band; b < nbands; b++ )
    1572             :             {
    1573     4115520 :                 q_direction->band_data[b].energy_ratio_index[0] = ivas_diffuseness_huff_ec_decode( bitstream, index, av );
    1574     4115520 :                 dif_min = min( dif_min, q_direction->band_data[b].energy_ratio_index[0] );
    1575             :             }
    1576             :         }
    1577             :     }
    1578             :     else /* different values for diffuseness */
    1579             :     {
    1580     2241670 :         dif_min = DIRAC_DIFFUSE_LEVELS;
    1581             : 
    1582    11854751 :         for ( b = start_band; b < nbands; b++ )
    1583             :         {
    1584     9613081 :             q_direction->band_data[b].energy_ratio_index[0] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS );
    1585     9613081 :             dif_min = min( dif_min, q_direction->band_data[b].energy_ratio_index[0] );
    1586             :         }
    1587             :     }
    1588             : 
    1589     3371831 :     *diffuseness_index_max_ec_frame = 5;
    1590             :     /* adaptively select the diffuseness_index_max_ec threshold */
    1591     3371831 :     if ( dif_min > 5 )
    1592             :     {
    1593      376860 :         *diffuseness_index_max_ec_frame = DIRAC_DIFFUSE_LEVELS - 1;
    1594             :     }
    1595             : 
    1596     3371831 :     return ( index_start - *index );
    1597             : }
    1598             : 
    1599             : 
    1600             : /*-------------------------------------------------------------------*
    1601             :  * ivas_qmetadata_entropy_decode_diffuseness_hr_512()
    1602             :  *
    1603             :  *
    1604             :  *-------------------------------------------------------------------*/
    1605             : 
    1606       58506 : static int16_t ivas_qmetadata_entropy_decode_diffuseness_hr_512(
    1607             :     uint16_t *bitstream, /* i  : bitstream               */
    1608             :     int16_t *index,
    1609             :     IVAS_QDIRECTION *q_direction )
    1610             : {
    1611             :     int16_t b, k;
    1612             : 
    1613             :     int16_t index_start;
    1614             :     int16_t nbands, nblocks;
    1615             :     int16_t start_band;
    1616             : 
    1617       58506 :     index_start = *index;
    1618       58506 :     nbands = q_direction->cfg.nbands;
    1619       58506 :     nblocks = q_direction->cfg.nblocks;
    1620       58506 :     start_band = q_direction->cfg.start_band;
    1621             : 
    1622             :     /* diffuseness decoding */
    1623             :     /* Handle one band as special case*/
    1624       58506 :     if ( nbands == 1 )
    1625             :     {
    1626           0 :         q_direction->band_data[0].energy_ratio_index[0] = 0;
    1627           0 :         for ( b = 0; b < MASA_BITS_ER_HR; b++ )
    1628             :         {
    1629           0 :             q_direction->band_data[0].energy_ratio_index[0] = ( q_direction->band_data[0].energy_ratio_index[0] << 1 ) + bitstream[( *index )--];
    1630             :         }
    1631             : 
    1632           0 :         return MASA_BITS_ER_HR;
    1633             :     }
    1634             : 
    1635     1343939 :     for ( b = start_band; b < nbands; b++ )
    1636             :     {
    1637     6382750 :         for ( k = 0; k < nblocks; k++ )
    1638             :         {
    1639     5097317 :             q_direction->band_data[b].energy_ratio_index[k] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, HR_MASA_ER_LEVELS );
    1640             :         }
    1641             :     }
    1642             : 
    1643       58506 :     return ( index_start - *index );
    1644             : }
    1645             : 
    1646             : 
    1647             : /*-------------------------------------------------------------------*
    1648             :  * ivas_qmetadata_entropy_decode_df_ratio()
    1649             :  *
    1650             :  *
    1651             :  *-------------------------------------------------------------------*/
    1652             : 
    1653      323950 : static int16_t ivas_qmetadata_entropy_decode_df_ratio(
    1654             :     uint16_t *bitstream,
    1655             :     int16_t *index,
    1656             :     IVAS_QDIRECTION *q_direction,
    1657             :     int16_t *dfRatio_bits )
    1658             : {
    1659             :     int16_t b;
    1660             :     int16_t bits_raw;
    1661             :     int16_t max_dfRatio_bits;
    1662             :     uint16_t ratio_min;
    1663             :     int16_t index_start;
    1664             :     int16_t nbands;
    1665             :     int16_t start_band;
    1666             :     int16_t ec_mode;
    1667             :     int16_t dec_mode;
    1668             :     int16_t max_alphabet_size;
    1669             : 
    1670      323950 :     index_start = *index;
    1671      323950 :     nbands = q_direction->cfg.nbands;
    1672      323950 :     start_band = q_direction->cfg.start_band;
    1673             : 
    1674             :     /* Handle one band as special case*/
    1675      323950 :     if ( nbands == 1 )
    1676             :     {
    1677       12901 :         q_direction->band_data[0].energy_ratio_index[0] = 0;
    1678       40186 :         for ( b = 0; b < dfRatio_bits[0]; b++ )
    1679             :         {
    1680       27285 :             q_direction->band_data[0].energy_ratio_index[0] = ( q_direction->band_data[0].energy_ratio_index[0] << 1 ) + bitstream[( *index )--];
    1681             :         }
    1682       12901 :         return dfRatio_bits[0];
    1683             :     }
    1684             : 
    1685             :     /* Calculate raw coding bits and decide what modes are possible */
    1686      311049 :     bits_raw = 0;
    1687      311049 :     max_dfRatio_bits = 0;
    1688     3560957 :     for ( b = start_band; b < nbands; b++ )
    1689             :     {
    1690     3249908 :         bits_raw += dfRatio_bits[b];
    1691     3249908 :         max_dfRatio_bits = max( max_dfRatio_bits, dfRatio_bits[b] );
    1692             :     }
    1693             : 
    1694             :     /* Decide what modes are possible */
    1695      311049 :     if ( bits_raw >= max_dfRatio_bits + 2 + nbands )
    1696             :     {
    1697      263117 :         ec_mode = 2;
    1698             :     }
    1699       47932 :     else if ( bits_raw >= max_dfRatio_bits + 1 )
    1700             :     {
    1701       47932 :         ec_mode = 1;
    1702             :     }
    1703             :     else
    1704             :     {
    1705           0 :         ec_mode = 0;
    1706             :     }
    1707      311049 :     max_alphabet_size = 1 << max_dfRatio_bits;
    1708             : 
    1709      311049 :     dec_mode = 2; /* Default to raw decoding */
    1710      311049 :     if ( ec_mode == 1 )
    1711             :     {
    1712       47932 :         if ( bitstream[( *index )--] == 0 )
    1713             :         {
    1714        2478 :             dec_mode = 1; /* Switch to one value EC coding */
    1715             :         }
    1716             :     }
    1717      263117 :     else if ( ec_mode == 2 )
    1718             :     {
    1719      263117 :         if ( bitstream[( *index )--] == 0 )
    1720             :         {
    1721        9398 :             if ( bitstream[( *index )--] == 0 )
    1722             :             {
    1723         695 :                 dec_mode = 1; /* Switch to one value EC coding */
    1724             :             }
    1725             :             else
    1726             :             {
    1727        8703 :                 dec_mode = 0; /* Use one-bit diff bandwise mode */
    1728             :             }
    1729             :         }
    1730             :     }
    1731             : 
    1732      311049 :     if ( dec_mode == 2 ) /* Raw decoding */
    1733             :     {
    1734     3449250 :         for ( b = start_band; b < nbands; b++ )
    1735             :         {
    1736     3150077 :             q_direction->band_data[b].energy_ratio_index[0] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, 1 << dfRatio_bits[b] );
    1737             :         }
    1738             :     }
    1739       11876 :     else if ( dec_mode == 1 ) /* One value decoding */
    1740             :     {
    1741        3173 :         ratio_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, max_alphabet_size ); /* dif_unique_value */
    1742             : 
    1743       22217 :         for ( b = start_band; b < nbands; b++ )
    1744             :         {
    1745       19044 :             q_direction->band_data[b].energy_ratio_index[0] = ratio_min;
    1746             :         }
    1747             :     }
    1748             :     else /* Bandwise 1-bit diff decoding */
    1749             :     {
    1750        8703 :         ratio_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, max_alphabet_size - 1 ); /* dif_min_value */
    1751             : 
    1752       89490 :         for ( b = start_band; b < nbands; b++ )
    1753             :         {
    1754       80787 :             q_direction->band_data[b].energy_ratio_index[0] = ratio_min + bitstream[( *index )--]; /* dif_bit_offset_values */
    1755             :         }
    1756             :     }
    1757             : 
    1758      311049 :     return ( index_start - *index );
    1759             : }
    1760             : 
    1761             : 
    1762             : /*-----------------------------------------------------------------------*
    1763             :  * Local functions (EC1)
    1764             :  *-----------------------------------------------------------------------*/
    1765             : 
    1766             : /*-------------------------------------------------------------------------
    1767             :  * ivas_qmetadata_entropy_decode_dir()
    1768             :  *
    1769             :  * Main function for entropy decoding of the directions
    1770             :  *------------------------------------------------------------------------*/
    1771             : 
    1772     2556227 : static int16_t ivas_qmetadata_entropy_decode_dir(
    1773             :     IVAS_QDIRECTION *q_direction, /* i/o: quantized direction structure   */
    1774             :     uint16_t *bitstream,          /* i  : bitstream                       */
    1775             :     int16_t *index,
    1776             :     const uint16_t diffuseness_index_max_ec_frame,
    1777             :     const int16_t nbands,
    1778             :     const int16_t start_band,
    1779             :     const int16_t hrmasa_flag /* i  : flag indicating high-rate MASA MD coding*/
    1780             : )
    1781             : {
    1782             :     int16_t b, m;
    1783             :     int16_t diff_idx;
    1784             :     int16_t diff_idx_min;
    1785             :     int16_t nblocks;
    1786             :     int16_t index_start;
    1787             : 
    1788             :     uint16_t gr_param_elev, gr_param_azith;
    1789             :     int16_t bands_entropic[MASA_MAXIMUM_CODING_SUBBANDS];
    1790             :     int16_t elev_alph[MASA_MAXIMUM_CODING_SUBBANDS];
    1791             :     int16_t azith_alph[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
    1792             : 
    1793             :     int16_t avg_elevation_alphabet, avg_azimuth_alphabet;
    1794             :     int16_t avg_elevation_idx, avg_azimuth_index;
    1795             :     int16_t avg_elevation_index_projected, avg_azimuth_index_projected;
    1796             :     float direction_vector[3], avg_direction_vector[3], avg_azimuth, avg_elevation;
    1797             :     int16_t use_adapt_avg, idx;
    1798             : 
    1799     2556227 :     index_start = *index;
    1800     2556227 :     nblocks = q_direction->cfg.nblocks;
    1801             : 
    1802     2556227 :     diff_idx_min = DIRAC_DIFFUSE_LEVELS;
    1803             : 
    1804             :     /*Raw coding for high diffuseness*/
    1805    17465052 :     for ( b = start_band; b < nbands; b++ )
    1806             :     {
    1807    14908825 :         if ( hrmasa_flag )
    1808             :         {
    1809           0 :             diff_idx = 0;
    1810             :         }
    1811             :         else
    1812             :         {
    1813    14908825 :             diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
    1814             :         }
    1815             : 
    1816    14908825 :         diff_idx_min = min( diff_idx_min, diff_idx );
    1817    14908825 :         if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    1818             :         {
    1819      330782 :             elev_alph[b] = no_theta_masa[bits_direction_masa[diff_idx] - 3];
    1820             :         }
    1821             :         else
    1822             :         {
    1823    14578043 :             elev_alph[b] = no_theta_masa[bits_direction_masa[diff_idx] - 3] * 2 - 1;
    1824             :         }
    1825             : 
    1826    14908825 :         if ( q_direction->band_data[b].energy_ratio_index_mod[0] > diffuseness_index_max_ec_frame )
    1827             :         {
    1828     3300285 :             bands_entropic[b] = 0;
    1829             : 
    1830     3300285 :             if ( q_direction->not_in_2D > 0 )
    1831             :             {
    1832     2680969 :                 decode_fixed_rate( q_direction, bitstream, index, b, nblocks );
    1833             :             }
    1834             :             else
    1835             :             {
    1836             :                 /* in 2D */
    1837     3024037 :                 for ( m = 0; m < nblocks; m++ )
    1838             :                 {
    1839     2404721 :                     q_direction->band_data[b].elevation[m] = 0.f;
    1840     2404721 :                     q_direction->band_data[b].elevation_index[m] = 0;
    1841             : 
    1842     2404721 :                     azith_alph[b][m] = no_phi_masa[bits_direction_masa[diff_idx] - 1][0];
    1843     2404721 :                     q_direction->band_data[b].azimuth_index[m] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, azith_alph[b][m] );
    1844     2404721 :                     q_direction->band_data[b].azimuth[m] = deindex_azimuth( q_direction->band_data[b].azimuth_index[m], q_direction->band_data[b].bits_sph_idx[m], 0, 1, q_direction->cfg.mc_ls_setup );
    1845             :                 }
    1846             :             }
    1847             :         }
    1848             :         else
    1849             :         {
    1850    11608540 :             bands_entropic[b] = 1;
    1851             :         }
    1852             :     }
    1853             : 
    1854             :     /*EC for the low diffuseness*/
    1855             : 
    1856             :     /*Elevation only if not 2D  */
    1857     2556227 :     if ( q_direction->not_in_2D > 0 )
    1858             :     {
    1859     1618873 :         if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    1860             :         {
    1861       49241 :             avg_elevation_alphabet = no_theta_masa[bits_direction_masa[diff_idx_min] - 3];
    1862       49241 :             avg_elevation_idx = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, avg_elevation_alphabet );
    1863             :         }
    1864             :         else
    1865             :         {
    1866     1569632 :             avg_elevation_alphabet = no_theta_masa[bits_direction_masa[diff_idx_min] - 3] * 2 - 1;
    1867     1569632 :             avg_elevation_idx = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, avg_elevation_alphabet );
    1868     1569632 :             avg_elevation_idx = ivas_qmetadata_dereorder_generic( avg_elevation_idx ) + ( avg_elevation_alphabet >> 1 );
    1869             :         }
    1870             : 
    1871     1618873 :         gr_param_elev = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, 4 + 1 );
    1872     1618873 :         if ( gr_param_elev == 4 ) /* all the elevation distances are zero */
    1873             :         {
    1874      794823 :             for ( b = start_band; b < nbands; b++ )
    1875             :             {
    1876      601830 :                 if ( bands_entropic[b] )
    1877             :                 {
    1878             :                     int16_t tmp_index;
    1879      332288 :                     if ( hrmasa_flag )
    1880             :                     {
    1881           0 :                         diff_idx = 0;
    1882             :                     }
    1883             :                     else
    1884             :                     {
    1885      332288 :                         diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
    1886             :                     }
    1887             : 
    1888      332288 :                     if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    1889             :                     {
    1890       41607 :                         avg_elevation_index_projected = ivas_chan_project_elevation_index( avg_elevation_idx, avg_elevation_alphabet, elev_alph[b] );
    1891             :                     }
    1892             :                     else
    1893             :                     {
    1894      290681 :                         avg_elevation_index_projected = ivas_dirac_project_elevation_index( avg_elevation_idx, avg_elevation_alphabet, elev_alph[b] );
    1895             : 
    1896             :                         /*reorder elevation indexing*/
    1897      290681 :                         tmp_index = avg_elevation_index_projected - ( elev_alph[b] >> 1 );
    1898      290681 :                         if ( tmp_index < 0 )
    1899             :                         {
    1900      159281 :                             tmp_index = tmp_index * -2;
    1901             :                         }
    1902      131400 :                         else if ( tmp_index > 0 )
    1903             :                         {
    1904       20520 :                             tmp_index = tmp_index * 2 - 1;
    1905             :                         }
    1906      290681 :                         avg_elevation_index_projected = tmp_index;
    1907             :                     }
    1908             : 
    1909     1260133 :                     for ( m = 0; m < nblocks; m++ )
    1910             :                     {
    1911      927845 :                         q_direction->band_data[b].elevation_index[m] = avg_elevation_index_projected;
    1912             : 
    1913             :                         /*deduce aplhabet for azimuth*/
    1914      927845 :                         if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    1915             :                         {
    1916      114288 :                             azith_alph[b][m] = no_phi_masa[bits_direction_masa[diff_idx] - 1][q_direction->band_data[b].elevation_index[m]];
    1917             :                         }
    1918             :                         else
    1919             :                         {
    1920      813557 :                             azith_alph[b][m] = no_phi_masa[bits_direction_masa[diff_idx] - 1][( q_direction->band_data[b].elevation_index[m] + 1 ) >> 1];
    1921             :                         }
    1922             : 
    1923             :                         /*decode elevation*/
    1924      927845 :                         q_direction->band_data[b].elevation[m] = deindex_elevation( &q_direction->band_data[b].elevation_index[m], q_direction->band_data[b].bits_sph_idx[m], q_direction->cfg.mc_ls_setup );
    1925             :                     }
    1926             :                 }
    1927             :             }
    1928             :         }
    1929             :         else
    1930             :         {
    1931    11291186 :             for ( b = start_band; b < nbands; b++ )
    1932             :             {
    1933     9865306 :                 if ( bands_entropic[b] )
    1934             :                 {
    1935     7453879 :                     if ( hrmasa_flag )
    1936             :                     {
    1937           0 :                         diff_idx = 0;
    1938             :                     }
    1939             :                     else
    1940             :                     {
    1941     7453879 :                         diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
    1942             :                     }
    1943             : 
    1944    36455183 :                     for ( m = 0; m < nblocks; m++ )
    1945             :                     {
    1946             :                         int16_t tmp_index;
    1947    29001304 :                         if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    1948             :                         {
    1949      374088 :                             avg_elevation_index_projected = ivas_chan_project_elevation_index( avg_elevation_idx, avg_elevation_alphabet, elev_alph[b] );
    1950      374088 :                             tmp_index = ivas_qmetadata_DecodeExtendedGR( bitstream, index, 2 * elev_alph[b] - 1, gr_param_elev );
    1951      374088 :                             if ( tmp_index % 2 )
    1952             :                             {
    1953       40148 :                                 tmp_index = avg_elevation_index_projected + ( ( tmp_index + 1 ) >> 1 );
    1954             :                             }
    1955             :                             else
    1956             :                             {
    1957      333940 :                                 tmp_index = avg_elevation_index_projected - ( tmp_index >> 1 );
    1958             :                             }
    1959      374088 :                             q_direction->band_data[b].elevation_index[m] = tmp_index;
    1960             : 
    1961             :                             /*deduce aplhabet for azimuth*/
    1962      374088 :                             azith_alph[b][m] = no_phi_masa[bits_direction_masa[diff_idx] - 1][q_direction->band_data[b].elevation_index[m]];
    1963             :                         }
    1964             :                         else
    1965             :                         {
    1966    28627216 :                             avg_elevation_index_projected = ivas_dirac_project_elevation_index( avg_elevation_idx, avg_elevation_alphabet, elev_alph[b] );
    1967             : 
    1968    28627216 :                             tmp_index = ivas_qmetadata_DecodeExtendedGR( bitstream, index, elev_alph[b], gr_param_elev );
    1969    28627216 :                             tmp_index = ivas_qmetadata_ReorderElevationDecoded( tmp_index, avg_elevation_index_projected, elev_alph[b] );
    1970             : 
    1971             :                             /*reorder elevation indexing*/
    1972    28627216 :                             tmp_index = tmp_index - ( elev_alph[b] >> 1 );
    1973    28627216 :                             if ( tmp_index < 0 )
    1974             :                             {
    1975    16701900 :                                 tmp_index = tmp_index * -2;
    1976             :                             }
    1977    11925316 :                             else if ( tmp_index > 0 )
    1978             :                             {
    1979     2989533 :                                 tmp_index = tmp_index * 2 - 1;
    1980             :                             }
    1981    28627216 :                             q_direction->band_data[b].elevation_index[m] = tmp_index;
    1982             : 
    1983             :                             /*deduce aplhabet for azimuth*/
    1984    28627216 :                             azith_alph[b][m] = no_phi_masa[bits_direction_masa[diff_idx] - 1][( q_direction->band_data[b].elevation_index[m] + 1 ) >> 1];
    1985             :                         }
    1986             : 
    1987             :                         /*decode elevation*/
    1988    29001304 :                         q_direction->band_data[b].elevation[m] = deindex_elevation( &q_direction->band_data[b].elevation_index[m], q_direction->band_data[b].bits_sph_idx[m], q_direction->cfg.mc_ls_setup );
    1989             :                     }
    1990             :                 }
    1991             :             }
    1992             :         }
    1993             :     }
    1994             :     else
    1995             :     {
    1996     5379043 :         for ( b = start_band; b < nbands; b++ )
    1997             :         {
    1998     4441689 :             if ( bands_entropic[b] )
    1999             :             {
    2000     3822373 :                 if ( hrmasa_flag )
    2001             :                 {
    2002           0 :                     diff_idx = 0;
    2003             :                 }
    2004             :                 else
    2005             :                 {
    2006     3822373 :                     diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
    2007             :                 }
    2008             : 
    2009    18203459 :                 for ( m = 0; m < nblocks; m++ )
    2010             :                 {
    2011    14381086 :                     q_direction->band_data[b].elevation_index[m] = 0;
    2012             : 
    2013             :                     /*deduce alphabet for azimuth*/
    2014    14381086 :                     if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    2015             :                     {
    2016      267774 :                         azith_alph[b][m] = no_phi_masa[bits_direction_masa[diff_idx] - 1][q_direction->band_data[b].elevation_index[m]];
    2017             :                     }
    2018             :                     else
    2019             :                     {
    2020    14113312 :                         azith_alph[b][m] = no_phi_masa[bits_direction_masa[diff_idx] - 1][( q_direction->band_data[b].elevation_index[m] + 1 ) >> 1];
    2021             :                     }
    2022             : 
    2023             :                     /*decode elevation*/
    2024    14381086 :                     q_direction->band_data[b].elevation[m] = deindex_elevation( &q_direction->band_data[b].elevation_index[m], q_direction->band_data[b].bits_sph_idx[m], q_direction->cfg.mc_ls_setup );
    2025             :                 }
    2026             :             }
    2027             :         }
    2028             :     }
    2029             : 
    2030             :     /*Azimuth*/
    2031     2556227 :     avg_azimuth_alphabet = no_phi_masa[bits_direction_masa[diff_idx_min] - 1][0]; /* average azimuth is quantized on the equatorial plane */
    2032     2556227 :     avg_azimuth_index = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, avg_azimuth_alphabet );
    2033     2556227 :     avg_azimuth_index = ivas_qmetadata_dereorder_generic( avg_azimuth_index );
    2034     2556227 :     avg_azimuth_index = avg_azimuth_index + ( avg_azimuth_alphabet >> 1 );
    2035             : 
    2036     2556227 :     gr_param_azith = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, 5 + 1 );
    2037     2556227 :     if ( gr_param_azith == 5 ) /* all the azimuth distances are zero */
    2038             :     {
    2039     1063045 :         for ( b = start_band; b < nbands; b++ )
    2040             :         {
    2041      794653 :             if ( bands_entropic[b] )
    2042             :             {
    2043     1956854 :                 for ( m = 0; m < nblocks; m++ )
    2044             :                 {
    2045     1421422 :                     q_direction->band_data[b].azimuth_index[m] = ivas_dirac_project_azimuth_index( avg_azimuth_index, avg_azimuth_alphabet, azith_alph[b][m] );
    2046             : 
    2047     1421422 :                     if ( azith_alph[b][m] == 1 )
    2048             :                     {
    2049          43 :                         q_direction->band_data[b].azimuth[m] = 0.f;
    2050             :                     }
    2051             :                     else
    2052             :                     {
    2053     1421379 :                         q_direction->band_data[b].azimuth[m] = deindex_azimuth( q_direction->band_data[b].azimuth_index[m], q_direction->band_data[b].bits_sph_idx[m], q_direction->band_data[b].elevation_index[m], 0, q_direction->cfg.mc_ls_setup );
    2054             :                     }
    2055             :                 }
    2056             :             }
    2057             :         }
    2058             :     }
    2059             :     else
    2060             :     {
    2061     2287835 :         set_zero( avg_direction_vector, 3 );
    2062     2287835 :         use_adapt_avg = 0;
    2063     2287835 :         idx = 0;
    2064             : 
    2065    16402007 :         for ( b = start_band; b < nbands; b++ )
    2066             :         {
    2067    14114172 :             if ( bands_entropic[b] )
    2068             :             {
    2069    53961921 :                 for ( m = 0; m < nblocks; m++ )
    2070             :                 {
    2071    42888813 :                     if ( ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( idx == MASA_LIMIT_IDX_AVG_AZI ) && ( nblocks > 1 ) )
    2072             :                     {
    2073       31382 :                         use_adapt_avg = bitstream[*index];
    2074       31382 :                         ( *index )--;
    2075             :                     }
    2076    42888813 :                     avg_azimuth_index_projected = ivas_dirac_project_azimuth_index( avg_azimuth_index, avg_azimuth_alphabet, azith_alph[b][m] );
    2077    42888813 :                     q_direction->band_data[b].azimuth_index[m] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, azith_alph[b][m], gr_param_azith );
    2078    42888813 :                     q_direction->band_data[b].azimuth_index[m] = ivas_qmetadata_ReorderElevationDecoded( q_direction->band_data[b].azimuth_index[m], avg_azimuth_index_projected, azith_alph[b][m] );
    2079             : 
    2080    42888813 :                     if ( azith_alph[b][m] == 1 )
    2081             :                     {
    2082        3187 :                         q_direction->band_data[b].azimuth[m] = 0.f;
    2083             :                     }
    2084             :                     else
    2085             :                     {
    2086    42885626 :                         q_direction->band_data[b].azimuth[m] = deindex_azimuth( q_direction->band_data[b].azimuth_index[m], q_direction->band_data[b].bits_sph_idx[m], q_direction->band_data[b].elevation_index[m], 0, q_direction->cfg.mc_ls_setup );
    2087             :                     }
    2088    42888813 :                     if ( ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( nblocks > 1 ) )
    2089             :                     {
    2090      589868 :                         if ( idx < MASA_LIMIT_IDX_AVG_AZI )
    2091             :                         {
    2092      193096 :                             ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[b].azimuth[m], q_direction->band_data[b].elevation[m], direction_vector );
    2093      193096 :                             v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
    2094             :                         }
    2095             :                         else
    2096             :                         {
    2097      396772 :                             if ( use_adapt_avg == 1 )
    2098             :                             {
    2099      324888 :                                 if ( m == 0 )
    2100             :                                 {
    2101       81222 :                                     v_multc( avg_direction_vector, 0.5f, avg_direction_vector, 3 );
    2102             :                                 }
    2103             :                                 /*compute the average direction per already coded subband */
    2104      324888 :                                 ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[b].azimuth[m], q_direction->band_data[b].elevation[m], direction_vector );
    2105      324888 :                                 v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
    2106      324888 :                                 ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector, &avg_azimuth, &avg_elevation );
    2107      324888 :                                 avg_azimuth_index = quantize_phi( avg_azimuth + 180, 0, &avg_azimuth, avg_azimuth_alphabet );
    2108             :                             }
    2109             :                         }
    2110      589868 :                         idx++;
    2111             :                     }
    2112             :                 }
    2113             :             }
    2114             :         }
    2115             :     }
    2116             : 
    2117     2556227 :     return ( index_start - *index );
    2118             : }
    2119             : 
    2120             : 
    2121             : /*-------------------------------------------------------------------------
    2122             :  * ivas_qmetadata_raw_decode_dir()
    2123             :  *
    2124             :  * Main function for raw decoding of the directions
    2125             :  *------------------------------------------------------------------------*/
    2126             : 
    2127       58506 : static int16_t ivas_qmetadata_raw_decode_dir_512(
    2128             :     IVAS_QDIRECTION *q_direction, /* i/o: quantized direction structure   */
    2129             :     uint16_t *bitstream,          /* i  : bitstream                       */
    2130             :     int16_t *index,
    2131             :     const int16_t nbands,
    2132             :     const int16_t start_band,
    2133             :     const SPHERICAL_GRID_DATA *sph_grid16 /* i  : spherical grid for deindexing */
    2134             : )
    2135             : {
    2136             :     int16_t b, m, i;
    2137             :     int16_t nblocks;
    2138             :     int16_t index_start;
    2139             :     uint16_t value;
    2140             : 
    2141       58506 :     index_start = *index;
    2142       58506 :     nblocks = q_direction->cfg.nblocks;
    2143             : 
    2144     1343939 :     for ( b = start_band; b < nbands; b++ )
    2145             :     {
    2146     6382750 :         for ( m = 0; m < nblocks; m++ )
    2147             :         {
    2148     5097317 :             value = 0;
    2149    70885884 :             for ( i = 0; i < q_direction->band_data[b].bits_sph_idx[m]; i++ )
    2150             :             {
    2151    65788567 :                 value = ( value << 1 ) + bitstream[( *index )--];
    2152             :             }
    2153     5097317 :             q_direction->band_data[b].spherical_index[m] = value;
    2154             : 
    2155     5097317 :             if ( q_direction->band_data[b].bits_sph_idx[m] == 16 )
    2156             :             {
    2157     1943616 :                 deindex_sph_idx( value, sph_grid16, &( q_direction->band_data[b].elevation[m] ), &( q_direction->band_data[b].azimuth[m] ) );
    2158             :             }
    2159             :             else
    2160             :             {
    2161     3153701 :                 deindex_spherical_component( q_direction->band_data[b].spherical_index[m], &q_direction->band_data[b].azimuth[m], &q_direction->band_data[b].elevation[m], &q_direction->band_data[b].azimuth_index[m], &q_direction->band_data[b].elevation_index[m], q_direction->band_data[b].bits_sph_idx[m], q_direction->cfg.mc_ls_setup );
    2162             :             }
    2163             :         }
    2164             :     }
    2165             : 
    2166       58506 :     return ( index_start - *index );
    2167             : }
    2168             : 
    2169             : 
    2170             : /*-------------------------------------------------------------------------
    2171             :  * ivas_qmetadata_raw_decode_dir()
    2172             :  *
    2173             :  * Main function for raw decoding of the directions
    2174             :  *------------------------------------------------------------------------*/
    2175             : 
    2176     1090909 : static int16_t ivas_qmetadata_raw_decode_dir(
    2177             :     IVAS_QDIRECTION *q_direction, /* i/o: quantized direction structure   */
    2178             :     uint16_t *bitstream,          /* i  : bitstream                       */
    2179             :     int16_t *index,
    2180             :     const int16_t nbands,
    2181             :     const int16_t start_band,
    2182             :     const int16_t hrmasa_flag /* i  : flag indicating high-rate MASA MD coding*/
    2183             : )
    2184             : {
    2185             :     int16_t b, m, azith_alph;
    2186             :     int16_t diff_idx;
    2187             :     int16_t nblocks;
    2188             :     int16_t index_start;
    2189             : 
    2190     1090909 :     index_start = *index;
    2191     1090909 :     nblocks = q_direction->cfg.nblocks;
    2192             : 
    2193     4723951 :     for ( b = start_band; b < nbands; b++ )
    2194             :     {
    2195     3633042 :         if ( q_direction->not_in_2D > 0 )
    2196             :         {
    2197     2420499 :             decode_fixed_rate( q_direction, bitstream, index, b, nblocks );
    2198             :         }
    2199             :         else
    2200             :         {
    2201     1212543 :             if ( hrmasa_flag )
    2202             :             {
    2203           0 :                 diff_idx = 0;
    2204             :             }
    2205             :             else
    2206             :             {
    2207     1212543 :                 diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
    2208             :             }
    2209             : 
    2210     4542342 :             for ( m = 0; m < nblocks; m++ )
    2211             :             {
    2212     3329799 :                 q_direction->band_data[b].elevation[m] = 0.f;
    2213     3329799 :                 q_direction->band_data[b].elevation_index[m] = 0;
    2214     3329799 :                 azith_alph = no_phi_masa[bits_direction_masa[diff_idx] - 1][0];
    2215     3329799 :                 q_direction->band_data[b].azimuth_index[m] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, azith_alph );
    2216     3329799 :                 q_direction->band_data[b].azimuth[m] = deindex_azimuth( q_direction->band_data[b].azimuth_index[m], q_direction->band_data[b].bits_sph_idx[m], 0, 1, q_direction->cfg.mc_ls_setup );
    2217             :             }
    2218             :         }
    2219             :     }
    2220             : 
    2221     1090909 :     return ( index_start - *index );
    2222             : }
    2223             : 
    2224             : 
    2225             : /*-------------------------------------------------------------------------
    2226             :  * ivas_qmetadata_DecodeQuasiUniform()
    2227             :  *
    2228             :  * Read the bitstream following the encoding scheme of EncodeQuasiUniform
    2229             :  *------------------------------------------------------------------------*/
    2230             : 
    2231             : /*! r: Value read from the bitstream */
    2232    36102090 : static uint16_t ivas_qmetadata_DecodeQuasiUniform(
    2233             :     const uint16_t *bitstream,   /* i  : pointer to the bitstream to read                                        */
    2234             :     int16_t *index,              /* i  : position in the bitstream to start reading (gets updated with reading)  */
    2235             :     const uint16_t alphabet_size /* i  : size of the alphabet, used to calculate the number of bits needed       */
    2236             : )
    2237             : {
    2238             :     int16_t i, bits;
    2239             :     uint16_t tresh, value;
    2240             : 
    2241             : #ifdef DEBUGGING
    2242             :     assert( ( alphabet_size >= 1 ) );
    2243             : #endif
    2244             : 
    2245    36102090 :     bits = 30 - norm_l( alphabet_size ); /* bits = floor(log2(alphabet_size)) */
    2246    36102090 :     tresh = ( 1U << ( bits + 1 ) ) - alphabet_size;
    2247             : 
    2248    36102090 :     value = 0;
    2249   148464437 :     for ( i = 0; i < bits; i++ )
    2250             :     {
    2251   112362347 :         value = ( value << 1 ) + bitstream[( *index )--];
    2252             :     }
    2253             : 
    2254    36102090 :     if ( value >= tresh )
    2255             :     {
    2256     4956744 :         value = ( value << 1 ) - tresh + bitstream[( *index )--];
    2257             :     }
    2258             : 
    2259             : #ifdef DEBUGGING
    2260             :     assert( value < alphabet_size );
    2261             : #endif
    2262    36102090 :     return value;
    2263             : }
    2264             : 
    2265             : 
    2266             : /*-------------------------------------------------------------------------
    2267             :  * ivas_qmetadata_DecodeExtendedGR()
    2268             :  *
    2269             :  * Reads the bitstream and decodes the value using the ExtendedGR algorithm
    2270             :  *------------------------------------------------------------------------*/
    2271             : 
    2272             : /*! r: Value decoded from the bitstream */
    2273    99249540 : int16_t ivas_qmetadata_DecodeExtendedGR(
    2274             :     uint16_t *bitstream,     /* i  : pointer to the bitstream to read                                            */
    2275             :     int16_t *index,          /* i/o: position in the bitstream to start reading (gets updated with reading)      */
    2276             :     const int16_t alph_size, /* i  : size of the alphabet, used to calculate the number of bits needed           */
    2277             :     const int16_t gr_param   /* i  : GR parameter that indicates the limit for the most significant bits (msb)   */
    2278             : )
    2279             : {
    2280             :     int16_t i, msb_size;
    2281             :     uint16_t value;
    2282             :     int16_t msb, lsb;
    2283             : 
    2284    99249540 :     msb_size = ( alph_size + ( 1 << gr_param ) - 1 ) >> gr_param; /* ceil division */
    2285    99249540 :     if ( msb_size <= 3 )
    2286             :     {
    2287     3093958 :         value = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, alph_size );
    2288             :     }
    2289             :     else
    2290             :     {
    2291    96155582 :         msb = 0;
    2292   171420341 :         while ( ( msb < msb_size - 1 ) && ( bitstream[*index] != 0 ) )
    2293             :         {
    2294    75264759 :             msb++;
    2295    75264759 :             ( *index )--;
    2296             :         }
    2297             : 
    2298    96155582 :         if ( msb == msb_size - 1 )
    2299             :         {
    2300      235605 :             lsb = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, alph_size - ( ( msb_size - 1 ) << gr_param ) );
    2301             :         }
    2302             :         else
    2303             :         {
    2304             : #ifdef DEBUGGING
    2305             :             assert( bitstream[*index] == 0 );
    2306             : #endif
    2307    95919977 :             ( *index )--;
    2308    95919977 :             lsb = 0;
    2309   163048718 :             for ( i = 0; i < gr_param; i++ )
    2310             :             {
    2311    67128741 :                 lsb = ( lsb << 1 ) + bitstream[( *index )--];
    2312             :             }
    2313             :         }
    2314             : 
    2315    96155582 :         value = ( msb << gr_param ) + lsb;
    2316             :     }
    2317             : 
    2318             : #ifdef DEBUGGING
    2319             :     assert( value < alph_size );
    2320             : #endif
    2321    99249540 :     return value;
    2322             : }
    2323             : 
    2324             : 
    2325             : /*-------------------------------------------------------------------------
    2326             :  * ivas_qmetadata_ReorderElevationDecoded()
    2327             :  *
    2328             :  * Calculates the correct elevation index from the decoded data
    2329             :  *------------------------------------------------------------------------*/
    2330             : 
    2331             : /*! r: Elevation index as it will be read by the dequantizer */
    2332    71516029 : static int16_t ivas_qmetadata_ReorderElevationDecoded(
    2333             :     const int16_t elev_dist, /* i  : Distance to the average extracted from the bitstream        */
    2334             :     const int16_t elev_avg,  /* i  : Average value over time-blocks extracted from the bitstream */
    2335             :     const int16_t elev_alph  /* i  : elevation alphabet                                          */
    2336             : )
    2337             : {
    2338             :     int16_t dist_reorder;
    2339             :     int16_t elev_index_reorder;
    2340             : 
    2341    71516029 :     dist_reorder = ivas_qmetadata_dereorder_generic( elev_dist );
    2342    71516029 :     elev_index_reorder = elev_avg + dist_reorder;
    2343             : 
    2344    71516029 :     if ( elev_index_reorder < 0 )
    2345             :     {
    2346      117794 :         elev_index_reorder += elev_alph;
    2347             :     }
    2348    71398235 :     else if ( elev_index_reorder >= elev_alph )
    2349             :     {
    2350      686585 :         elev_index_reorder -= elev_alph;
    2351             :     }
    2352             : 
    2353    71516029 :     return elev_index_reorder;
    2354             : }
    2355             : 
    2356             : 
    2357             : /*-----------------------------------------------------------------------*
    2358             :  * Local functions: requentizeEC3
    2359             :  *-----------------------------------------------------------------------*/
    2360             : 
    2361             : /*! r: number of bits read */
    2362      243696 : static int16_t read_directions(
    2363             :     IVAS_QDIRECTION *q_direction,  /* i/o: quantized direction structure   */
    2364             :     const uint8_t coding_subbands, /* i  : number of directions            */
    2365             :     const uint8_t masa_subframes,  /* i  : number of tiles                 */
    2366             :     uint16_t *bitstream,           /* i  : bitstream to be read            */
    2367             :     int16_t *pbit_pos,
    2368             :     int16_t *ind_order )
    2369             : {
    2370             :     int16_t j, k, allowed_bits, last_j, nbits, fixed_rate;
    2371             :     int16_t i;
    2372             :     int16_t diff;
    2373             :     uint16_t byteBuffer;
    2374             :     int16_t use_vq, max_nb_idx;
    2375             :     int16_t bit_pos;
    2376             :     int16_t *bits_dir0;
    2377             : 
    2378      243696 :     bit_pos = *pbit_pos;
    2379             : 
    2380      243696 :     diff = 0;
    2381      243696 :     if ( q_direction->not_in_2D )
    2382             :     {
    2383      234574 :         if ( coding_subbands > 1 )
    2384             :         {
    2385      233529 :             j = ind_order[coding_subbands - 1];
    2386      233529 :             allowed_bits = 0;
    2387             : 
    2388      861210 :             for ( k = 0; k < masa_subframes; k++ )
    2389             :             {
    2390      627681 :                 allowed_bits += q_direction->band_data[j].bits_sph_idx[k];
    2391             :             }
    2392             : 
    2393      233529 :             last_j = j - ( allowed_bits == 0 );
    2394             : 
    2395      915776 :             for ( j = 0; j < last_j; j++ )
    2396             :             {
    2397      682247 :                 i = ind_order[j];
    2398      682247 :                 bits_dir0 = (int16_t *) q_direction->band_data[i].bits_sph_idx;
    2399             : 
    2400      682247 :                 nbits = 0;
    2401      682247 :                 allowed_bits = sum_s( bits_dir0, q_direction->cfg.nblocks );
    2402      682247 :                 use_vq = 0;
    2403      682247 :                 max_nb_idx = 0;
    2404             : 
    2405     2496889 :                 for ( k = 0; k < q_direction->cfg.nblocks; k++ )
    2406             :                 {
    2407     1814642 :                     if ( bits_dir0[k] > use_vq )
    2408             :                     {
    2409      815957 :                         use_vq = bits_dir0[k];
    2410      815957 :                         max_nb_idx = k;
    2411             :                     }
    2412             :                 }
    2413             : 
    2414      682247 :                 if ( q_direction->cfg.nblocks == 1 )
    2415             :                 {
    2416      304782 :                     byteBuffer = 0;
    2417             :                 }
    2418             :                 else
    2419             :                 {
    2420      377465 :                     byteBuffer = 0;
    2421      377465 :                     if ( use_vq <= 1 )
    2422             :                     {
    2423         631 :                         byteBuffer = 1;
    2424             :                     }
    2425      377465 :                     if ( use_vq > 1 && use_vq <= LIMIT_USE_COMMON )
    2426             :                     {
    2427       79249 :                         bits_dir0[max_nb_idx] -= 1;
    2428       79249 :                         allowed_bits -= 1;
    2429             :                         /* read 1 bit to tell if joint of VQ coding */
    2430       79249 :                         byteBuffer = bitstream[bit_pos--];
    2431             :                     }
    2432             :                 }
    2433             : 
    2434     2496889 :                 for ( k = 0; k < masa_subframes; k++ )
    2435             :                 {
    2436     1814642 :                     q_direction->band_data[i].bits_sph_idx[k] = bits_dir0[k];
    2437     1814642 :                     if ( bits_dir0[k] > 2 )
    2438             :                     {
    2439     1707149 :                         if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    2440             :                         {
    2441      201701 :                             q_direction->band_data[i].elevation_m_alphabet[k] = no_theta_masa[bits_dir0[k] - 3];
    2442             :                         }
    2443             :                         else
    2444             :                         {
    2445     1505448 :                             q_direction->band_data[i].elevation_m_alphabet[k] = no_theta_masa[bits_dir0[k] - 3] * 2 - 1;
    2446             :                         }
    2447             :                     }
    2448             :                     else
    2449             :                     {
    2450      107493 :                         q_direction->band_data[i].elevation_m_alphabet[k] = 1;
    2451             :                     }
    2452             :                 }
    2453             : 
    2454      682247 :                 if ( allowed_bits > 0 )
    2455             :                 {
    2456      682247 :                     if ( byteBuffer == 1 )
    2457             :                     {
    2458       77971 :                         nbits = read_common_direction( bitstream, q_direction, i, masa_subframes, allowed_bits, &bit_pos );
    2459             :                     }
    2460             :                     else
    2461             :                     {
    2462      604276 :                         if ( q_direction->cfg.nblocks == 1 && q_direction->band_data[i].bits_sph_idx[0] <= MASA_MIN_BITS_TF + 1 )
    2463             :                         {
    2464             :                             /* there is fixed rate only, no need to read  */
    2465      167993 :                             fixed_rate = 1;
    2466      167993 :                             nbits = 0;
    2467             :                         }
    2468             :                         else
    2469             :                         {
    2470             :                             /* check if fixed_rate */
    2471      436283 :                             fixed_rate = bitstream[bit_pos--];
    2472      436283 :                             nbits = 1;
    2473             :                         }
    2474             : 
    2475      604276 :                         if ( fixed_rate == 1 )
    2476             :                         {
    2477             :                             /* decode_fixed_rate()*/
    2478      443962 :                             nbits += decode_fixed_rate( q_direction, bitstream, &bit_pos, i, masa_subframes );
    2479             :                         }
    2480             :                         else
    2481             :                         {
    2482             :                             /* decode elevation */
    2483      160314 :                             nbits += decode_elevation( q_direction, bitstream, &bit_pos, i, masa_subframes );
    2484             : 
    2485             :                             /* decode azimuth */
    2486      160314 :                             nbits += decode_azimuth( q_direction, bitstream, &bit_pos, i, masa_subframes );
    2487             :                         }
    2488             :                     }
    2489             :                 }
    2490             :                 else
    2491             :                 {
    2492           0 :                     set_zero_direction( q_direction, i, masa_subframes );
    2493             :                 }
    2494      682247 :                 diff += nbits - allowed_bits;
    2495             : 
    2496             :                 /* update bits for next block */
    2497      682247 :                 update_bits_next_block( q_direction, &diff, ind_order[j + 1], coding_subbands, masa_subframes );
    2498             :             }
    2499             :         }
    2500             :         else
    2501             :         {
    2502        1045 :             last_j = q_direction->cfg.start_band;
    2503             :         }
    2504             : 
    2505             : 
    2506      627492 :         for ( j = last_j; j < coding_subbands; j++ )
    2507             :         {
    2508      392918 :             i = ind_order[j];
    2509      392918 :             bits_dir0 = (int16_t *) q_direction->band_data[i].bits_sph_idx;
    2510             : 
    2511      392918 :             nbits = 0;
    2512      392918 :             allowed_bits = sum_s( bits_dir0, q_direction->cfg.nblocks );
    2513      392918 :             if ( allowed_bits > 0 && masa_subframes == 1 )
    2514             :             {
    2515      184319 :                 nbits += decode_fixed_rate( q_direction, bitstream, &bit_pos, i, masa_subframes );
    2516             :             }
    2517             :             else
    2518             :             {
    2519      208599 :                 if ( allowed_bits > 0 )
    2520             :                 {
    2521      208599 :                     use_vq = 0;
    2522      208599 :                     max_nb_idx = 0;
    2523     1042995 :                     for ( k = 0; k < masa_subframes; k++ )
    2524             :                     {
    2525      834396 :                         if ( bits_dir0[k] > use_vq )
    2526             :                         {
    2527      302602 :                             use_vq = bits_dir0[k];
    2528      302602 :                             max_nb_idx = k;
    2529             :                         }
    2530             :                     }
    2531             : 
    2532      208599 :                     byteBuffer = 0;
    2533             : 
    2534      208599 :                     if ( use_vq > 1 && use_vq <= LIMIT_USE_COMMON )
    2535             :                     {
    2536       28431 :                         bits_dir0[max_nb_idx] -= 1;
    2537       28431 :                         allowed_bits -= 1;
    2538             : 
    2539             :                         /* read 1 bit to tell if joint of VQ coding */
    2540       28431 :                         byteBuffer = bitstream[bit_pos--];
    2541             :                     }
    2542             : 
    2543      208599 :                     if ( allowed_bits > 0 )
    2544             :                     {
    2545      208599 :                         if ( byteBuffer == 1 || use_vq <= 1 )
    2546             :                         {
    2547       28332 :                             nbits = read_common_direction( bitstream, q_direction, i, masa_subframes, allowed_bits, &bit_pos );
    2548             :                         }
    2549             :                         else
    2550             :                         {
    2551             :                             /* decode_fixed_rate()*/
    2552      180267 :                             nbits += decode_fixed_rate( q_direction, bitstream, &bit_pos, i, masa_subframes );
    2553             :                         }
    2554             :                     }
    2555             :                     else
    2556             :                     {
    2557           0 :                         set_zero_direction( q_direction, i, masa_subframes );
    2558             :                     }
    2559             :                 }
    2560             :                 else
    2561             :                 {
    2562           0 :                     set_zero_direction( q_direction, i, masa_subframes );
    2563             :                 }
    2564             :             }
    2565             :         }
    2566             :     }
    2567             :     else
    2568             :     {
    2569             :         /* 2D */
    2570       43952 :         for ( j = 0; j < coding_subbands; j++ )
    2571             :         {
    2572      156672 :             for ( k = 0; k < q_direction->cfg.nblocks; k++ )
    2573             :             {
    2574      121842 :                 q_direction->band_data[j].elevation[k] = 0;
    2575      121842 :                 q_direction->band_data[j].elevation_index[k] = 0;
    2576             :             }
    2577             :         }
    2578        9122 :         nbits = decode_azimuth2D( q_direction, bitstream, coding_subbands, &bit_pos, masa_subframes );
    2579             :     }
    2580      243696 :     nbits = *pbit_pos - bit_pos;
    2581      243696 :     *pbit_pos = bit_pos;
    2582             : 
    2583      243696 :     return nbits;
    2584             : }
    2585             : 
    2586             : 
    2587             : /*-------------------------------------------------------------------*
    2588             :  * decode_azimuth()
    2589             :  *
    2590             :  * read and decode the azimuth indexes for one subband
    2591             :  *-------------------------------------------------------------------*/
    2592             : 
    2593             : /*! r: number of bits read  */
    2594      160314 : static int16_t decode_azimuth(
    2595             :     IVAS_QDIRECTION *q_direction, /* i/o: quantized metadata structure    */
    2596             :     uint16_t *bitstream,          /* i  : bitstream to be read            */
    2597             :     int16_t *pbit_pos,            /* i/o: current position in bitstream   */
    2598             :     const int16_t idx_subband,    /* i  : subband index                   */
    2599             :     const int16_t masa_subframes  /* i  : number of tiles                 */
    2600             : )
    2601             : {
    2602             :     int16_t bit_pos, nbits, k;
    2603             :     uint16_t use_context, byteBuffer;
    2604             :     uint16_t min_idx;
    2605             :     int16_t j_az, max_val;
    2606             : 
    2607      160314 :     nbits = 0;
    2608      160314 :     bit_pos = *pbit_pos;
    2609      160314 :     byteBuffer = 0;
    2610             : 
    2611      160314 :     j_az = 0;
    2612             :     /* check number of valid indexes to decode */
    2613      793500 :     for ( k = 0; k < masa_subframes; k++ )
    2614             :     {
    2615      633186 :         if ( q_direction->band_data[idx_subband].azimuth_index[k] < MASA_NO_INDEX )
    2616             :         {
    2617      633156 :             j_az++;
    2618             :         }
    2619             :         else
    2620             :         {
    2621          30 :             q_direction->band_data[idx_subband].azimuth[k] = 0.f; /*To be in sync with encoder values.*/
    2622             :         }
    2623             :     }
    2624             : 
    2625      160314 :     if ( j_az == 0 )
    2626             :     {
    2627           4 :         return nbits;
    2628             :     }
    2629             : 
    2630      160310 :     if ( byteBuffer == 0 )
    2631             :     {
    2632             :         /* use context */
    2633      160310 :         use_context = 0;
    2634      793492 :         for ( k = 0; k < masa_subframes; k++ )
    2635             :         {
    2636      633182 :             if ( q_direction->band_data[idx_subband].bits_sph_idx[k] <= 1 )
    2637             :             {
    2638           0 :                 use_context = 1;
    2639             :             }
    2640             :         }
    2641             : 
    2642      160310 :         if ( use_context == 1 )
    2643             :         {
    2644           0 :             for ( k = 0; k < masa_subframes; k++ )
    2645             :             {
    2646           0 :                 if ( q_direction->band_data[idx_subband].bits_sph_idx[k] == 0 )
    2647             :                 {
    2648           0 :                     q_direction->band_data[idx_subband].azimuth_index[k] = 0;
    2649           0 :                     q_direction->band_data[idx_subband].azimuth[k] = 0;
    2650             :                 }
    2651             :                 else
    2652             :                 {
    2653           0 :                     if ( q_direction->band_data[idx_subband].bits_sph_idx[k] == 1 )
    2654             :                     {
    2655           0 :                         byteBuffer = bitstream[bit_pos--];
    2656           0 :                         q_direction->band_data[idx_subband].azimuth_index[k] = byteBuffer;
    2657             : 
    2658           0 :                         q_direction->band_data[idx_subband].azimuth[k] = q_direction->band_data[idx_subband].azimuth_index[k] * ( -180.f );
    2659             :                     }
    2660             :                     else
    2661             :                     {
    2662           0 :                         q_direction->band_data[idx_subband].azimuth_index[k] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, q_direction->band_data[idx_subband].azimuth_m_alphabet[k], MASA_GR_ORD_AZ - ( q_direction->band_data[idx_subband].bits_sph_idx[k] == 2 ) );
    2663             : 
    2664           0 :                         q_direction->band_data[idx_subband].azimuth[k] = deindex_azimuth( q_direction->band_data[idx_subband].azimuth_index[k], q_direction->band_data[idx_subband].bits_sph_idx[k], q_direction->band_data[idx_subband].elevation_index[k], 1, q_direction->cfg.mc_ls_setup );
    2665             :                     }
    2666             :                 }
    2667             :             }
    2668             :         }
    2669             :         else
    2670             :         {
    2671             :             /* read bit to check if min removed encoding */
    2672      160310 :             byteBuffer = bitstream[bit_pos--];
    2673      160310 :             if ( byteBuffer == 0 ) /* regular GR coding5 */
    2674             :             {
    2675             :                 /* read GR_order */
    2676       57260 :                 byteBuffer = bitstream[bit_pos--];
    2677       57260 :                 nbits += 1;
    2678             : 
    2679      278242 :                 for ( k = 0; k < masa_subframes; k++ )
    2680             :                 {
    2681      220982 :                     if ( q_direction->band_data[idx_subband].bits_sph_idx[k] > 0 )
    2682             :                     {
    2683      220982 :                         if ( no_phi_masa[q_direction->band_data[idx_subband].bits_sph_idx[k] - 1][q_direction->band_data[idx_subband].elevation_index[k]] > 1 )
    2684             :                         {
    2685      220960 :                             q_direction->band_data[idx_subband].azimuth_index[k] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, q_direction->band_data[idx_subband].azimuth_m_alphabet[k], MASA_GR_ORD_AZ - byteBuffer );
    2686      220960 :                             q_direction->band_data[idx_subband].azimuth[k] = deindex_azimuth( q_direction->band_data[idx_subband].azimuth_index[k], q_direction->band_data[idx_subband].bits_sph_idx[k], q_direction->band_data[idx_subband].elevation_index[k], 1, q_direction->cfg.mc_ls_setup );
    2687             :                         }
    2688             :                         else
    2689             :                         {
    2690          22 :                             q_direction->band_data[idx_subband].azimuth[k] = 0;
    2691          22 :                             q_direction->band_data[idx_subband].azimuth_index[k] = 0;
    2692             :                         }
    2693             :                     }
    2694             :                     else
    2695             :                     {
    2696           0 :                         q_direction->band_data[idx_subband].azimuth[k] = 0;
    2697           0 :                         q_direction->band_data[idx_subband].azimuth_index[k] = 0;
    2698             :                     }
    2699             :                 }
    2700             :             }
    2701             :             else
    2702             :             {
    2703             :                 /* min removed GR coding */
    2704             :                 /* read GR_order */
    2705      103050 :                 byteBuffer = bitstream[bit_pos--];
    2706             :                 /* read min index value */
    2707      103050 :                 maximum_s( q_direction->band_data[idx_subband].azimuth_m_alphabet, masa_subframes, &max_val );
    2708      103050 :                 min_idx = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, max_val, MASA_GR_ORD_AZ );
    2709             : 
    2710      515250 :                 for ( k = 0; k < masa_subframes; k++ )
    2711             :                 {
    2712      412200 :                     if ( q_direction->band_data[idx_subband].bits_sph_idx[k] > 0 )
    2713             :                     {
    2714      412200 :                         if ( no_phi_masa[q_direction->band_data[idx_subband].bits_sph_idx[k] - 1][q_direction->band_data[idx_subband].elevation_index[k]] > 1 )
    2715             :                         {
    2716      412196 :                             q_direction->band_data[idx_subband].azimuth_index[k] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, q_direction->band_data[idx_subband].azimuth_m_alphabet[k], MASA_GR_ORD_AZ - 1 - byteBuffer );
    2717      412196 :                             q_direction->band_data[idx_subband].azimuth_index[k] += min_idx;
    2718      412196 :                             q_direction->band_data[idx_subband].azimuth[k] = deindex_azimuth( q_direction->band_data[idx_subband].azimuth_index[k], q_direction->band_data[idx_subband].bits_sph_idx[k], q_direction->band_data[idx_subband].elevation_index[k], 1, q_direction->cfg.mc_ls_setup );
    2719             :                         }
    2720             :                         else
    2721             :                         {
    2722           4 :                             q_direction->band_data[idx_subband].azimuth[k] = 0;
    2723           4 :                             q_direction->band_data[idx_subband].azimuth_index[k] = 0;
    2724             :                         }
    2725             :                     }
    2726             :                     else
    2727             :                     {
    2728           0 :                         q_direction->band_data[idx_subband].azimuth[k] = 0;
    2729           0 :                         q_direction->band_data[idx_subband].azimuth_index[k] = 0;
    2730             :                     }
    2731             :                 }
    2732             :             }
    2733             :         }
    2734             :     }
    2735             : 
    2736      160310 :     nbits = *pbit_pos - bit_pos;
    2737             : 
    2738      160310 :     *pbit_pos = bit_pos;
    2739             : 
    2740      160310 :     return nbits;
    2741             : }
    2742             : 
    2743             : 
    2744             : /*-------------------------------------------------------------------*
    2745             :  * decode_elevation()
    2746             :  *
    2747             :  * Reads the bitstream and decode the elevation index
    2748             :  *-------------------------------------------------------------------*/
    2749             : 
    2750             : /*! r: number of bits read */
    2751      160314 : static int16_t decode_elevation(
    2752             :     IVAS_QDIRECTION *q_direction, /* i/o: quantized metadata structure            */
    2753             :     uint16_t *bitstream,          /* i  : input bitstream                         */
    2754             :     int16_t *pbit_pos,            /* i/o: current position to be read in bitstream*/
    2755             :     const int16_t j,              /* i  : subband index                           */
    2756             :     const int16_t masa_subframes  /* i  : number of tiles                         */
    2757             : )
    2758             : {
    2759             :     int16_t nr_NO_INDEX, nbits;
    2760             :     int16_t bit_pos;
    2761             :     uint16_t byteBuffer;
    2762             :     int16_t k, GR_ord_elevation;
    2763             :     uint16_t same_idx;
    2764             : 
    2765      160314 :     nr_NO_INDEX = 0;
    2766      160314 :     nbits = 0;
    2767      160314 :     bit_pos = *pbit_pos;
    2768             : 
    2769      793500 :     for ( k = 0; k < masa_subframes; k++ )
    2770             :     {
    2771      633186 :         q_direction->band_data[j].elevation_index[k] = 0;
    2772      633186 :         q_direction->band_data[j].elevation[k] = 0;
    2773             : 
    2774      633186 :         if ( q_direction->band_data[j].bits_sph_idx[k] > 0 )
    2775             :         {
    2776      633186 :             if ( q_direction->band_data[j].bits_sph_idx[k] <= 2 )
    2777             :             {
    2778           9 :                 q_direction->band_data[j].elevation_index[k] = MASA_NO_INDEX;
    2779           9 :                 nr_NO_INDEX += 1;
    2780           9 :                 q_direction->band_data[j].elevation[k] = 0;
    2781           9 :                 q_direction->band_data[j].elevation_m_alphabet[k] = 1;
    2782             :             }
    2783             :             else
    2784             :             {
    2785      633177 :                 if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    2786             :                 {
    2787      106663 :                     q_direction->band_data[j].elevation_m_alphabet[k] = no_theta_masa[q_direction->band_data[j].bits_sph_idx[k] - 3];
    2788             :                 }
    2789             :                 else
    2790             :                 {
    2791      526514 :                     q_direction->band_data[j].elevation_m_alphabet[k] = no_theta_masa[q_direction->band_data[j].bits_sph_idx[k] - 3] * 2 - 1;
    2792             :                 }
    2793             :             }
    2794             :         }
    2795             :         else
    2796             :         {
    2797           0 :             nr_NO_INDEX++;
    2798             :         }
    2799             :     }
    2800             : 
    2801      160314 :     if ( nr_NO_INDEX < masa_subframes )
    2802             :     {
    2803             :         {
    2804             :             /* read if same or not */
    2805      160314 :             byteBuffer = bitstream[bit_pos--];
    2806      160314 :             if ( byteBuffer == 1 ) /* same value */
    2807             :             {
    2808             :                 /* read value */
    2809      128049 :                 byteBuffer = bitstream[bit_pos--];
    2810      128049 :                 byteBuffer = ( byteBuffer << 1 ) + bitstream[bit_pos--];
    2811      128049 :                 same_idx = byteBuffer;
    2812             : 
    2813      632187 :                 for ( k = 0; k < masa_subframes; k++ )
    2814             :                 {
    2815      504138 :                     if ( q_direction->band_data[j].elevation_index[k] < MASA_NO_INDEX )
    2816             :                     {
    2817      504138 :                         q_direction->band_data[j].elevation_index[k] = same_idx;
    2818      504138 :                         q_direction->band_data[j].elevation[k] = deindex_elevation( &q_direction->band_data[j].elevation_index[k], q_direction->band_data[j].bits_sph_idx[k], q_direction->cfg.mc_ls_setup );
    2819             :                     }
    2820             :                 }
    2821             :             }
    2822             :             else
    2823             :             {
    2824             :                 /* not same; decode mean removed GR */
    2825       32265 :                 byteBuffer = bitstream[bit_pos--];
    2826       32265 :                 GR_ord_elevation = MASA_GR_ORD_EL - byteBuffer;
    2827             : 
    2828      161313 :                 for ( k = 0; k < masa_subframes; k++ )
    2829             :                 {
    2830      129048 :                     if ( q_direction->band_data[j].elevation_index[k] < MASA_NO_INDEX )
    2831             :                     {
    2832      129039 :                         q_direction->band_data[j].elevation_index[k] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, q_direction->band_data[j].elevation_m_alphabet[k], GR_ord_elevation );
    2833      129039 :                         q_direction->band_data[j].elevation[k] = deindex_elevation( &q_direction->band_data[j].elevation_index[k], q_direction->band_data[j].bits_sph_idx[k], q_direction->cfg.mc_ls_setup );
    2834             :                     }
    2835             :                 }
    2836             :             }
    2837             :         }
    2838             :     }
    2839             : 
    2840      793500 :     for ( k = 0; k < masa_subframes; k++ )
    2841             :     {
    2842      633186 :         if ( ( q_direction->band_data[j].elevation_index[k] < MASA_NO_INDEX ) &&
    2843      633177 :              ( no_phi_masa[q_direction->band_data[j].bits_sph_idx[k] - 1][q_direction->band_data[j].elevation_index[k]] <= 1 ) )
    2844             :         {
    2845          30 :             q_direction->band_data[j].azimuth_index[k] = MASA_NO_INDEX;
    2846          30 :             q_direction->band_data[j].azimuth_m_alphabet[k] = 1;
    2847             :         }
    2848             :         else
    2849             :         {
    2850      633156 :             q_direction->band_data[j].azimuth_index[k] = 0;
    2851      633156 :             if ( q_direction->band_data[j].elevation_index[k] < MASA_NO_INDEX )
    2852             :             {
    2853      633147 :                 q_direction->band_data[j].azimuth_m_alphabet[k] = no_phi_masa[q_direction->band_data[j].bits_sph_idx[k] - 1][q_direction->band_data[j].elevation_index[k]];
    2854             :             }
    2855             :             else
    2856             :             {
    2857           9 :                 q_direction->band_data[j].azimuth_m_alphabet[k] = no_phi_masa[q_direction->band_data[j].bits_sph_idx[k] - 1][0];
    2858           9 :                 q_direction->band_data[j].elevation_index[k] = 0;
    2859             :             }
    2860             :         }
    2861             :     }
    2862             : 
    2863      160314 :     nbits = *pbit_pos - bit_pos;
    2864      160314 :     *pbit_pos = bit_pos;
    2865             : 
    2866      160314 :     return nbits;
    2867             : }
    2868             : 
    2869             : 
    2870             : /*----------------------------------------------------------------
    2871             :  * decode_fixed_rate()
    2872             :  *
    2873             :  * decoding in fixed rate case, i.e. when using the spherical indexes
    2874             :  *-----------------------------------------------------------------*/
    2875             : 
    2876             : /*! r: number of bits read */
    2877     5910016 : static int16_t decode_fixed_rate(
    2878             :     IVAS_QDIRECTION *q_direction, /* i/o: quantized metadata          */
    2879             :     const uint16_t *bitstream,    /* i  : bitstream to be read        */
    2880             :     int16_t *pbit_pos,            /* i/o: position in bitstream       */
    2881             :     const int16_t b,              /* i  : subband index               */
    2882             :     const int16_t nblocks         /* i  : number of tiles in subband  */
    2883             : )
    2884             : {
    2885             :     int16_t nbits, m, i;
    2886             :     uint16_t value;
    2887             : 
    2888     5910016 :     nbits = 0;
    2889             : 
    2890    25203650 :     for ( m = 0; m < nblocks; m++ )
    2891             :     {
    2892    19293634 :         value = 0;
    2893   106438669 :         for ( i = 0; i < q_direction->band_data[b].bits_sph_idx[m]; i++ )
    2894             :         {
    2895    87145035 :             value = ( value << 1 ) + bitstream[( *pbit_pos )--];
    2896             :         }
    2897             : 
    2898    19293634 :         q_direction->band_data[b].spherical_index[m] = value;
    2899    19293634 :         nbits += q_direction->band_data[b].bits_sph_idx[m];
    2900             : 
    2901    19293634 :         deindex_spherical_component( q_direction->band_data[b].spherical_index[m], &q_direction->band_data[b].azimuth[m], &q_direction->band_data[b].elevation[m], &q_direction->band_data[b].azimuth_index[m], &q_direction->band_data[b].elevation_index[m], q_direction->band_data[b].bits_sph_idx[m], q_direction->cfg.mc_ls_setup );
    2902             :     }
    2903             : 
    2904     5910016 :     return nbits;
    2905             : }
    2906             : 
    2907             : 
    2908             : /*-------------------------------------------------------------------*
    2909             :  * decode_azimuth2D()
    2910             :  *
    2911             :  * Azimuth bitstream reading and decoding in 2D case
    2912             :  *-------------------------------------------------------------------*/
    2913             : 
    2914             : /*! r: number of bits read */
    2915        9122 : static int16_t decode_azimuth2D(
    2916             :     IVAS_QDIRECTION *q_direction,  /* i/o: quantized metadata structure    */
    2917             :     uint16_t *bitstream,           /* i  : bitstream to be read            */
    2918             :     const int16_t coding_subbands, /* i  : number of subbands              */
    2919             :     int16_t *pbit_pos,
    2920             :     const int16_t no_frames )
    2921             : {
    2922             :     int16_t i, j, k;
    2923             :     int16_t allowed_bits, nbits;
    2924             :     int16_t use_vq;
    2925             :     uint16_t Buffer;
    2926             :     int16_t bit_pos;
    2927             :     int16_t *bits_dir0;
    2928             : 
    2929        9122 :     bit_pos = *pbit_pos;
    2930        9122 :     nbits = 0;
    2931       43952 :     for ( j = 0; j < coding_subbands; j++ )
    2932             :     {
    2933       34830 :         bits_dir0 = (int16_t *) q_direction->band_data[j].bits_sph_idx;
    2934       34830 :         allowed_bits = sum_s( bits_dir0, no_frames );
    2935             : 
    2936       34830 :         if ( allowed_bits > 0 )
    2937             :         {
    2938       34830 :             use_vq = 0;
    2939      156672 :             for ( k = 0; k < no_frames; k++ )
    2940             :             {
    2941      121842 :                 q_direction->band_data[j].elevation[k] = 0;
    2942      121842 :                 q_direction->band_data[j].elevation_index[k] = 0;
    2943             : 
    2944      121842 :                 if ( bits_dir0[k] > use_vq )
    2945             :                 {
    2946       42299 :                     use_vq = bits_dir0[k];
    2947             :                 }
    2948             :             }
    2949             : 
    2950       34830 :             if ( use_vq <= 3 && allowed_bits <= 11 )
    2951             :             {
    2952        3797 :                 if ( allowed_bits <= no_frames + 1 )
    2953             :                 {
    2954         583 :                     set_f( q_direction->band_data[j].azimuth, 0.0f, no_frames );
    2955        2834 :                     for ( k = 0; k < min( allowed_bits, no_frames ); k++ )
    2956             :                     {
    2957        2251 :                         q_direction->band_data[j].azimuth[k] = ( -180.f ) * bitstream[bit_pos--];
    2958        2251 :                         nbits += 1;
    2959             :                     }
    2960             :                 }
    2961             :                 else
    2962             :                 {
    2963        3214 :                     nbits += read_truncGR_azimuth( bitstream, q_direction, j, no_frames, &bit_pos );
    2964             :                 }
    2965             :             }
    2966             :             else
    2967             :             {
    2968      140927 :                 for ( k = 0; k < no_frames; k++ )
    2969             :                 {
    2970      109894 :                     Buffer = 0;
    2971      589076 :                     for ( i = 0; i < bits_dir0[k]; i++ )
    2972             :                     {
    2973      479182 :                         Buffer = ( Buffer << 1 ) + bitstream[bit_pos--];
    2974             :                     }
    2975             : 
    2976      109894 :                     nbits += bits_dir0[k];
    2977             : 
    2978      109894 :                     if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    2979             :                     {
    2980       35626 :                         q_direction->band_data[j].azimuth[k] = 360.0f / (float) ( 1 << bits_dir0[k] ) * Buffer - 180;
    2981       35626 :                         q_direction->band_data[j].azimuth[k] = companding_azimuth( q_direction->band_data[j].azimuth[k], q_direction->cfg.mc_ls_setup,
    2982       35626 :                                                                                    ( q_direction->band_data[j].elevation[k] > MC_MASA_THR_ELEVATION ), -1 );
    2983             :                     }
    2984             :                     else
    2985             :                     {
    2986       74268 :                         q_direction->band_data[j].azimuth[k] = 360.0f / (float) ( 1 << bits_dir0[k] ) * Buffer - 180;
    2987             :                     }
    2988             :                 }
    2989             :             }
    2990             :         }
    2991             :         else
    2992             :         {
    2993           0 :             set_zero_direction( q_direction, j, no_frames );
    2994             :         }
    2995             :     }
    2996        9122 :     *pbit_pos = bit_pos;
    2997             : 
    2998        9122 :     return nbits;
    2999             : }
    3000             : 
    3001             : 
    3002             : /*-------------------------------------------------------------------*
    3003             :  * set_zero_direction()
    3004             :  *
    3005             :  *
    3006             :  *-------------------------------------------------------------------*/
    3007             : 
    3008      106303 : static void set_zero_direction(
    3009             :     IVAS_QDIRECTION *q_direction,
    3010             :     const int16_t idx_band,
    3011             :     const int16_t len )
    3012             : {
    3013             :     int16_t k;
    3014             : 
    3015      531515 :     for ( k = 0; k < len; k++ )
    3016             :     {
    3017      425212 :         q_direction->band_data[idx_band].azimuth[k] = 0;
    3018      425212 :         q_direction->band_data[idx_band].azimuth_index[k] = 0;
    3019      425212 :         q_direction->band_data[idx_band].elevation[k] = 0;
    3020      425212 :         q_direction->band_data[idx_band].elevation_index[k] = 0;
    3021      425212 :         q_direction->band_data[idx_band].spherical_index[k] = 0;
    3022             :     }
    3023             : 
    3024      106303 :     return;
    3025             : }
    3026             : 
    3027             : 
    3028             : /*-------------------------------------------------------------------*
    3029             :  * read_truncGR_azimuth()
    3030             :  *
    3031             :  *
    3032             :  *-------------------------------------------------------------------*/
    3033             : 
    3034      107983 : static int16_t read_truncGR_azimuth(
    3035             :     uint16_t *bitstream,          /* i  : bitstream to be read            */
    3036             :     IVAS_QDIRECTION *q_direction, /* i/o: quantized metadata structure    */
    3037             :     const int16_t j,              /* i  : subband index                   */
    3038             :     const int16_t no_subframes,   /* i  : number of tiles                 */
    3039             :     int16_t *pbit_pos             /* i/o: position in bitstream           */
    3040             : )
    3041             : {
    3042             :     int16_t i;
    3043             :     int16_t nbits;
    3044             :     uint16_t idx;
    3045             :     int16_t no_symb, allowed_bits;
    3046             : 
    3047      107983 :     allowed_bits = sum_s( (int16_t *) q_direction->band_data[j].bits_sph_idx, no_subframes );
    3048      107983 :     nbits = 0;
    3049      107983 :     if ( allowed_bits <= no_subframes + 1 )
    3050             :     {
    3051           0 :         for ( i = 0; i < min( allowed_bits, no_subframes ); i++ )
    3052             :         {
    3053           0 :             if ( bitstream[( *pbit_pos )--] == 0 )
    3054             :             {
    3055           0 :                 q_direction->band_data[j].azimuth[i] = 0.0f;
    3056             :             }
    3057             :             else
    3058             :             {
    3059           0 :                 q_direction->band_data[j].azimuth[i] = -180;
    3060             :             }
    3061           0 :             nbits++;
    3062             :         }
    3063             : 
    3064           0 :         return nbits;
    3065             :     }
    3066             : 
    3067      107983 :     if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    3068             :     {
    3069       15048 :         no_symb = 9;
    3070             :     }
    3071             :     else
    3072             :     {
    3073       92935 :         no_symb = 8;
    3074             :     }
    3075      107983 :     nbits = 0;
    3076             : 
    3077      107983 :     nbits = *pbit_pos;
    3078             : 
    3079      536756 :     for ( i = 0; i < no_subframes; i++ )
    3080             :     {
    3081      428773 :         idx = ivas_qmetadata_DecodeExtendedGR( bitstream, pbit_pos, no_symb, 0 );
    3082      428773 :         q_direction->band_data[j].azimuth_index[i] = idx;
    3083      428773 :         if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    3084             :         {
    3085       58470 :             q_direction->band_data[j].azimuth[i] = cb_azi_chan[( idx + 1 ) >> 1];
    3086       58470 :             if ( idx % 2 > 0 )
    3087             :             {
    3088       25863 :                 q_direction->band_data[j].azimuth[i] = -q_direction->band_data[j].azimuth[i];
    3089             :             }
    3090             :         }
    3091             :         else
    3092             :         {
    3093      370303 :             q_direction->band_data[j].azimuth[i] = azimuth_cb[idx];
    3094             :         }
    3095             :     }
    3096             : 
    3097      107983 :     nbits -= *pbit_pos;
    3098             : 
    3099      107983 :     return nbits;
    3100             : }
    3101             : 
    3102             : 
    3103             : /*-------------------------------------------------------------------*
    3104             :  * read_common_direction()
    3105             :  *
    3106             :  *
    3107             :  *-------------------------------------------------------------------*/
    3108             : 
    3109             : /*! r: number of bits read */
    3110      106303 : static int16_t read_common_direction(
    3111             :     uint16_t *bitstream,          /* i  : bitstream to be read                        */
    3112             :     IVAS_QDIRECTION *q_direction, /* i/o: quantized direction structure               */
    3113             :     const int16_t j,              /* i  : subband index                               */
    3114             :     const int16_t no_subframes,   /* i  : number of tiles                             */
    3115             :     const int16_t bits_total,     /* i  : number of bits for subband directional data */
    3116             :     int16_t *pbit_pos             /* i/o: position in bitstream                       */
    3117             : )
    3118             : {
    3119             :     int16_t nbits;
    3120             :     int16_t bit_pos;
    3121             :     int16_t i;
    3122             :     uint16_t byteBuffer;
    3123             :     int16_t bits_el;
    3124             : 
    3125      106303 :     bit_pos = *pbit_pos;
    3126      106303 :     nbits = 0;
    3127             : 
    3128      106303 :     set_zero_direction( q_direction, j, no_subframes );
    3129      106303 :     if ( bits_total == 0 )
    3130             :     {
    3131           0 :         return nbits;
    3132             :     }
    3133             : 
    3134      106303 :     if ( bits_total <= no_subframes + 1 )
    3135             :     {
    3136        5510 :         for ( i = 0; i < min( no_subframes, bits_total ); i++ )
    3137             :         {
    3138        4408 :             byteBuffer = bitstream[bit_pos--];
    3139             :             /*qdirection->azimuth_index[j][i] = (uint16_t)byteBuffer; */
    3140        4408 :             q_direction->band_data[j].azimuth[i] = azimuth_cb[byteBuffer];
    3141        4408 :             nbits += 1;
    3142             :         }
    3143        1102 :         *pbit_pos = bit_pos;
    3144             : 
    3145             :         /*nbits += read_truncGR_azimuth(bitstream, qdirection, j, no_subframes, pbit_pos); */
    3146        1102 :         return nbits;
    3147             :     }
    3148             : 
    3149             : 
    3150      105201 :     byteBuffer = bitstream[bit_pos--];
    3151      105201 :     bits_el = 1;
    3152      105201 :     nbits += 1;
    3153             :     /* elevation is already set to 0*/
    3154      105201 :     if ( byteBuffer == 1 )
    3155             :     {
    3156       43253 :         byteBuffer = bitstream[bit_pos--];
    3157       43253 :         bits_el += 1;
    3158       43253 :         nbits += 1;
    3159       43253 :         if ( byteBuffer == 0 )
    3160             :         {
    3161       76755 :             for ( i = 0; i < no_subframes; i++ )
    3162             :             {
    3163       61404 :                 q_direction->band_data[j].elevation[i] = delta_theta_masa[2];
    3164             :             }
    3165             :         }
    3166             :         else
    3167             :         {
    3168       27902 :             byteBuffer = bitstream[bit_pos--];
    3169       27902 :             bits_el += 1;
    3170       27902 :             nbits += 1;
    3171       27902 :             if ( byteBuffer == 0 )
    3172             :             {
    3173      139345 :                 for ( i = 0; i < no_subframes; i++ )
    3174             :                 {
    3175      111476 :                     q_direction->band_data[j].elevation[i] = -delta_theta_masa[2];
    3176             :                 }
    3177             :             }
    3178             :             else
    3179             :             {
    3180             :                 /* theta is +/- 90; no azimuth is read */
    3181          33 :                 byteBuffer = bitstream[bit_pos--];
    3182          33 :                 nbits += 1;
    3183          33 :                 if ( byteBuffer == 0 )
    3184             :                 {
    3185          27 :                     set_f( q_direction->band_data[j].elevation, 90.0f, no_subframes );
    3186          27 :                     set_f( q_direction->band_data[j].azimuth, 0.0f, no_subframes );
    3187             :                 }
    3188             :                 else
    3189             :                 {
    3190           6 :                     set_f( q_direction->band_data[j].elevation, -90.0f, no_subframes );
    3191           6 :                     set_f( q_direction->band_data[j].azimuth, 0.0f, no_subframes );
    3192             :                 }
    3193          33 :                 *pbit_pos = bit_pos;
    3194             : 
    3195          33 :                 return nbits;
    3196             :             }
    3197             :         }
    3198             :     }
    3199             : 
    3200      105168 :     bits_el = sum_s( (int16_t *) q_direction->band_data[j].bits_sph_idx, no_subframes ) - bits_el;
    3201             : 
    3202      105168 :     if ( bits_el <= no_subframes + 1 )
    3203             :     {
    3204         399 :         nbits += min( no_subframes, bits_el );
    3205        1958 :         for ( i = 0; i < min( no_subframes, bits_el ); i++ )
    3206             :         {
    3207        1559 :             byteBuffer = bitstream[bit_pos--];
    3208             :             /*qdirection->azimuth_index[j][i] = (uint16_t) byteBuffer; */
    3209        1559 :             q_direction->band_data[j].azimuth[i] = azimuth_cb[byteBuffer];
    3210             :         }
    3211             :     }
    3212             :     else
    3213             :     {
    3214      104769 :         nbits += read_truncGR_azimuth( bitstream, q_direction, j, no_subframes, &bit_pos );
    3215             :     }
    3216             : 
    3217      105168 :     *pbit_pos = bit_pos;
    3218             : 
    3219             : #ifdef DEBUGGING
    3220             :     /*assert(nbits == bits_total); */
    3221             : #endif
    3222      105168 :     return nbits;
    3223             : }
    3224             : 
    3225             : 
    3226             : /*-----------------------------------------------------------------------*
    3227             :  * Local functions: coherence
    3228             :  *-----------------------------------------------------------------------*/
    3229             : 
    3230      548160 : static void decode_spread_coherence(
    3231             :     IVAS_QMETADATA_HANDLE hQMetaData, /* i/o: quantized metadata structure              */
    3232             :     int16_t idx_d,                    /* i  : direction index                           */
    3233             :     const int16_t no_frames,          /* i  : number of time subframes                  */
    3234             :     const int16_t hrmasa_flag         /* i  : flag indicating high-rate MASA MD coding  */
    3235             : )
    3236             : {
    3237             :     int16_t i, j;
    3238             :     float var_azi;
    3239             :     int16_t idx_sub_cb;
    3240             :     float dct_coh[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
    3241             :     int16_t MASA_grouping[MASA_MAXIMUM_CODING_SUBBANDS];
    3242             :     IVAS_QDIRECTION *q_direction;
    3243             :     int16_t coding_subbands, coding_subbands_0, d, two_dir_band[MASA_MAXIMUM_CODING_SUBBANDS];
    3244             :     int16_t min_index;
    3245             : 
    3246      548160 :     coding_subbands_0 = hQMetaData->q_direction[0].cfg.nbands;
    3247      548160 :     coding_subbands = hQMetaData->q_direction[idx_d].cfg.nbands;
    3248      548160 :     if ( coding_subbands_0 <= 5 )
    3249             :     {
    3250     1998870 :         for ( j = 0; j < 5; j++ )
    3251             :         {
    3252     1665725 :             MASA_grouping[j] = j;
    3253             :         }
    3254             :     }
    3255             :     else
    3256             :     {
    3257      215015 :         if ( coding_subbands_0 <= 8 )
    3258             :         {
    3259       64090 :             mvs2s( MASA_grouping_8_to_5, MASA_grouping, 8 );
    3260             :         }
    3261      150925 :         else if ( coding_subbands_0 <= 12 )
    3262             :         {
    3263       26801 :             mvs2s( MASA_grouping_12_to_5, MASA_grouping, 12 );
    3264             :         }
    3265      124124 :         else if ( coding_subbands_0 <= 18 )
    3266             :         {
    3267       44920 :             mvs2s( MASA_grouping_18_to_5, MASA_grouping, 18 );
    3268             :         }
    3269             :         else
    3270             :         {
    3271       79204 :             if ( coding_subbands_0 <= 24 )
    3272             :             {
    3273       79204 :                 mvs2s( MASA_grouping_24_to_5, MASA_grouping, 24 );
    3274             :             }
    3275             :         }
    3276             :     }
    3277             : 
    3278      548160 :     if ( coding_subbands < coding_subbands_0 )
    3279             :     {
    3280       35466 :         d = 0;
    3281      295890 :         for ( j = 0; j < coding_subbands_0; j++ )
    3282             :         {
    3283      260424 :             if ( hQMetaData->twoDirBands[j] == 1 )
    3284             :             {
    3285      111925 :                 two_dir_band[d++] = j;
    3286             :             }
    3287             :         }
    3288             :     }
    3289             :     else
    3290             :     {
    3291      512694 :         set_s( two_dir_band, 0, coding_subbands );
    3292             :     }
    3293             : 
    3294      548160 :     q_direction = &hQMetaData->q_direction[idx_d];
    3295             : 
    3296     5573014 :     for ( i = 0; i < coding_subbands; i++ )
    3297             :     {
    3298     5024854 :         var_azi = var( q_direction->band_data[i].azimuth, no_frames );
    3299             : 
    3300     5024854 :         if ( hrmasa_flag )
    3301             :         {
    3302           0 :             minimum_s( (int16_t *) ( q_direction->band_data[i].energy_ratio_index ), q_direction->cfg.nblocks, &min_index );
    3303           0 :             min_index = min_index >> 1;
    3304             :         }
    3305             :         else
    3306             :         {
    3307     5024854 :             min_index = q_direction->band_data[i].energy_ratio_index[0];
    3308             :         }
    3309             : 
    3310     5024854 :         if ( var_azi < MASA_DELTA_AZI_DCT0 )
    3311             :         {
    3312     1169597 :             idx_sub_cb = MASA_NO_CV_COH * min_index;
    3313             :         }
    3314             :         else
    3315             :         {
    3316     3855257 :             idx_sub_cb = MASA_NO_CV_COH * ( min_index + DIRAC_DIFFUSE_LEVELS ); /* NO_CV_COH = 8 */
    3317             :         }
    3318             : 
    3319     5024854 :         dct_coh[i][0] = coherence_cb0_masa[idx_sub_cb + q_direction->coherence_band_data[i].spread_coherence_dct0_index];
    3320             : 
    3321     5024854 :         if ( coding_subbands < coding_subbands_0 )
    3322             :         {
    3323      111925 :             assert( idx_d == 1 );
    3324      111925 :             dct_coh[i][1] = coherence_cb1_masa[MASA_grouping[two_dir_band[i]] * MASA_NO_CV_COH1 + q_direction->coherence_band_data[i].spread_coherence_dct1_index];
    3325             :         }
    3326             :         else
    3327             :         {
    3328     4912929 :             dct_coh[i][1] = coherence_cb1_masa[MASA_grouping[i] * MASA_NO_CV_COH1 + q_direction->coherence_band_data[i].spread_coherence_dct1_index];
    3329             :         }
    3330             : 
    3331    15074562 :         for ( j = 2; j < MAX_PARAM_SPATIAL_SUBFRAMES; j++ )
    3332             :         {
    3333    10049708 :             dct_coh[i][j] = 0;
    3334             :         }
    3335             : 
    3336     5024854 :         invdct4_transform( dct_coh[i], q_direction->coherence_band_data[i].spread_coherence );
    3337             :     }
    3338             : 
    3339      548160 :     return;
    3340             : }
    3341             : 
    3342             : 
    3343             : /*-------------------------------------------------------------------*
    3344             :  * read_huf()
    3345             :  *
    3346             :  * Read Hufman code
    3347             :  *-------------------------------------------------------------------*/
    3348             : 
    3349             : /*! r: number of bits read */
    3350      548160 : static ivas_error read_huf(
    3351             :     int16_t *num_bits_read,
    3352             :     const uint16_t *bitstream, /* i  : bitstream to be read            */
    3353             :     uint16_t *out,             /* o  : decoded value                   */
    3354             :     const int16_t start_pos,   /* i  : starting position for reading   */
    3355             :     const int16_t len,         /* i  : number of codewords             */
    3356             :     const int16_t *huff_code,  /* i  : Huffman table                   */
    3357             :     const int16_t max_len      /* i  : maximum codeword length         */
    3358             : )
    3359             : {
    3360      548160 :     int16_t done = 0, end_pos;
    3361             :     uint16_t ByteBuffer;
    3362             :     int16_t nbits, val;
    3363             :     uint16_t i;
    3364             : 
    3365      548160 :     end_pos = start_pos;
    3366      548160 :     nbits = 0;
    3367      548160 :     val = 0;
    3368     2706250 :     while ( ( done == 0 ) && ( nbits < max_len ) )
    3369             :     {
    3370     2158090 :         ByteBuffer = bitstream[end_pos--];
    3371     2158090 :         val = val * 2 + ( ByteBuffer & 1 );
    3372     2158090 :         nbits += 1;
    3373    10226317 :         for ( i = 0; i < len; i++ )
    3374             :         {
    3375     8616387 :             if ( val == huff_code[i] )
    3376             :             {
    3377      548160 :                 *out = i;
    3378      548160 :                 done = 1;
    3379      548160 :                 break;
    3380             :             }
    3381             :         }
    3382             :     }
    3383             : 
    3384             : #ifdef DEBUGGING
    3385             :     if ( nbits == max_len )
    3386             :     {
    3387             :         return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong Huffman codeword for average index in coherence encoding." );
    3388             :     }
    3389             : #endif
    3390      548160 :     *num_bits_read = end_pos;
    3391             : 
    3392      548160 :     return IVAS_ERR_OK;
    3393             : }
    3394             : 
    3395             : 
    3396             : /*-------------------------------------------------------------------*
    3397             :  * read_GR_min_removed_data()
    3398             :  *
    3399             :  *
    3400             :  *-------------------------------------------------------------------*/
    3401             : 
    3402       21615 : static int16_t read_GR_min_removed_data(
    3403             :     uint16_t *bitstream, /* i  : bitstream                       */
    3404             :     int16_t *p_bit_pos,  /* i  : position in the bitstream       */
    3405             :     const int16_t *no_cv_vec,
    3406             :     const int16_t no_data,
    3407             :     int16_t *decoded_idx,
    3408             :     const int16_t no_symb )
    3409             : {
    3410             :     int16_t j;
    3411             :     int16_t bit_pos;
    3412             :     int16_t nbits, bits_GR;
    3413             :     uint16_t byteBuffer;
    3414             :     int16_t min_index;
    3415             : 
    3416       21615 :     bit_pos = *p_bit_pos;
    3417             : 
    3418             :     /* read GR order */
    3419       21615 :     byteBuffer = bitstream[bit_pos--];
    3420       21615 :     nbits = 1;
    3421             : 
    3422             :     /* read min index */
    3423       21615 :     bits_GR = bit_pos;
    3424       21615 :     min_index = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_symb, 0 );
    3425       21615 :     nbits += bits_GR - bit_pos;
    3426             : 
    3427             :     /* read GR data */
    3428      193924 :     for ( j = 0; j < no_data; j++ )
    3429             :     {
    3430      172309 :         bits_GR = bit_pos;
    3431      172309 :         if ( no_cv_vec[j] > 1 )
    3432             :         {
    3433      167577 :             decoded_idx[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_cv_vec[j] - min_index, ( byteBuffer & 1 ) );
    3434      167577 :             nbits += bits_GR - bit_pos;
    3435             :         }
    3436             :         else
    3437             :         {
    3438        4732 :             decoded_idx[j] = 0;
    3439             :         }
    3440             :     }
    3441             : 
    3442      193924 :     for ( j = 0; j < no_data; j++ )
    3443             :     {
    3444      172309 :         if ( no_cv_vec[j] > 1 )
    3445             :         {
    3446      167577 :             decoded_idx[j] += min_index;
    3447             :         }
    3448             :     }
    3449             : 
    3450             : #ifdef DEBUGGING
    3451             :     assert( nbits == *p_bit_pos - bit_pos );
    3452             : #endif
    3453       21615 :     *p_bit_pos = bit_pos;
    3454             : 
    3455       21615 :     return nbits;
    3456             : }
    3457             : 
    3458             : 
    3459             : /*-------------------------------------------------------------------*
    3460             :  * decode_fixed_rate_composed_index_coherence()
    3461             :  *
    3462             :  *
    3463             :  *-------------------------------------------------------------------*/
    3464             : 
    3465      121731 : static int16_t decode_fixed_rate_composed_index_coherence(
    3466             :     uint16_t *bitstream, /* i  : bitstream                       */
    3467             :     int16_t *p_bit_pos,  /* i  : position in the bitstream       */
    3468             :     const int16_t no_bands,
    3469             :     int16_t *no_cv_vec,
    3470             :     uint16_t *decoded_index,
    3471             :     const int16_t no_symb )
    3472             : {
    3473             :     /* fixed rate */
    3474             :     uint64_t no_cb;
    3475             :     uint16_t temp_index[MASA_MAXIMUM_CODING_SUBBANDS];
    3476             :     uint64_t idx_fr;
    3477             :     int16_t no_bits_vec1, half_no_bands;
    3478             :     int16_t bit_pos;
    3479             :     int16_t nbits, bits_GR;
    3480             :     int16_t j;
    3481             :     int16_t no_vals_local;
    3482             :     int16_t no_bits_vec;
    3483             : 
    3484      121731 :     bit_pos = *p_bit_pos;
    3485      121731 :     set_s( (int16_t *) temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
    3486             : 
    3487      121731 :     no_cb = 1;
    3488      121731 :     nbits = 0;
    3489      121731 :     if ( no_bands > MASA_LIMIT_NO_BANDS_SUR_COH )
    3490             :     {
    3491             :         /* read 8-max_val with GR0 */
    3492       29302 :         bits_GR = bit_pos;
    3493       29302 :         no_vals_local = no_symb - ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_symb, 0 );
    3494       29302 :         nbits += bits_GR - bit_pos;
    3495      655338 :         for ( j = 0; j < no_bands; j++ )
    3496             :         {
    3497      626036 :             if ( no_cv_vec[j] > no_vals_local )
    3498             :             {
    3499      457954 :                 no_cv_vec[j] = no_vals_local;
    3500             :             }
    3501             :         }
    3502             :     }
    3503             : 
    3504      121731 :     half_no_bands = no_bands / 2;
    3505      121731 :     if ( sum_s( no_cv_vec, no_bands ) > MASA_COH_LIMIT_2IDX )
    3506             :     {
    3507       19817 :         no_cb = 1;
    3508             : 
    3509      239445 :         for ( j = 0; j < half_no_bands; j++ )
    3510             :         {
    3511      219628 :             no_cb *= no_cv_vec[j];
    3512             :         }
    3513       19817 :         no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3514       19817 :         no_cb = 1;
    3515      244143 :         for ( j = half_no_bands; j < no_bands; j++ )
    3516             :         {
    3517      224326 :             no_cb *= no_cv_vec[j];
    3518             :         }
    3519       19817 :         no_bits_vec1 = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3520             :     }
    3521             :     else
    3522             :     {
    3523      101914 :         no_cb = 1;
    3524      719476 :         for ( j = 0; j < no_bands; j++ )
    3525             :         {
    3526      617562 :             no_cb *= no_cv_vec[j];
    3527             :         }
    3528      101914 :         no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3529      101914 :         no_bits_vec1 = 0;
    3530             :     }
    3531      121731 :     if ( no_bits_vec1 > 0 )
    3532             :     {
    3533       19817 :         idx_fr = 0;
    3534      613221 :         for ( j = 0; j < no_bits_vec; j++ )
    3535             :         {
    3536      593404 :             idx_fr = ( idx_fr << 1 ) + bitstream[bit_pos--];
    3537             :         }
    3538             : 
    3539       19817 :         nbits += no_bits_vec;
    3540             : 
    3541       19817 :         decode_combined_index( idx_fr, no_cv_vec, temp_index, half_no_bands );
    3542             : 
    3543       19817 :         idx_fr = 0;
    3544      645268 :         for ( j = 0; j < no_bits_vec1; j++ )
    3545             :         {
    3546      625451 :             idx_fr = ( idx_fr << 1 ) + bitstream[bit_pos--];
    3547             :         }
    3548       19817 :         nbits += no_bits_vec1;
    3549       19817 :         decode_combined_index( idx_fr, &no_cv_vec[half_no_bands], &temp_index[half_no_bands], half_no_bands );
    3550             :     }
    3551             :     else
    3552             :     {
    3553      101914 :         idx_fr = 0;
    3554     1238781 :         for ( j = 0; j < no_bits_vec; j++ )
    3555             :         {
    3556     1136867 :             idx_fr = ( idx_fr << 1 ) + bitstream[bit_pos--];
    3557             :         }
    3558      101914 :         nbits += no_bits_vec;
    3559      101914 :         decode_combined_index( idx_fr, no_cv_vec, temp_index, no_bands );
    3560             :     }
    3561             : 
    3562     1183247 :     for ( j = 0; j < no_bands; j++ )
    3563             :     {
    3564     1061516 :         decoded_index[j] = temp_index[j];
    3565             :     }
    3566             : #ifdef DEBUGGING
    3567             :     assert( nbits == *p_bit_pos - bit_pos );
    3568             : #endif
    3569      121731 :     nbits = *p_bit_pos - bit_pos;
    3570             : 
    3571      121731 :     *p_bit_pos = bit_pos;
    3572             : 
    3573      121731 :     return nbits;
    3574             : }
    3575             : 
    3576             : 
    3577             : /*-------------------------------------------------------------------*
    3578             :  * read_coherence_data_hr_512()
    3579             :  *
    3580             :  * Read coherence data at HR
    3581             :  *-------------------------------------------------------------------*/
    3582             : 
    3583             : /*! r: number of bits read */
    3584       58500 : static int16_t read_coherence_data_hr_512(
    3585             :     uint16_t *bitstream,        /* i  : bitstream                       */
    3586             :     int16_t *p_bit_pos,         /* i  : position in the bitstream       */
    3587             :     IVAS_QMETADATA *hQMetaData, /* i/o: quantized metadata structure    */
    3588             :     const int16_t idx_dir,      /* i  : direction index                 */
    3589             :     const int16_t nbits_coh )
    3590             : {
    3591             :     int16_t j, k, i;
    3592             :     int16_t nbands, nblocks;
    3593             :     int16_t min_index, GR_param;
    3594             :     int16_t cb_size, nbits;
    3595             :     int16_t decoded_idx;
    3596             :     float delta;
    3597             : 
    3598       58500 :     nbands = hQMetaData->q_direction[idx_dir].cfg.nbands;
    3599       58500 :     nblocks = hQMetaData->q_direction[idx_dir].cfg.nblocks;
    3600             : 
    3601       58500 :     cb_size = 1 << nbits_coh;
    3602       58500 :     delta = 256.0f / cb_size;
    3603       58500 :     nbits = *p_bit_pos;
    3604      289962 :     for ( k = 0; k < nblocks; k++ )
    3605             :     {
    3606             :         /* read method */
    3607      231462 :         if ( bitstream[( *p_bit_pos )--] == 1 )
    3608             :         {
    3609             :             /* average removed */
    3610             :             /* read average index */
    3611        9790 :             min_index = 0;
    3612       43194 :             for ( i = 0; i < nbits_coh; i++ )
    3613             :             {
    3614       33404 :                 min_index = ( min_index << 1 ) + bitstream[( *p_bit_pos )--];
    3615             :             }
    3616             :             /* read GR param */
    3617        9790 :             GR_param = bitstream[( *p_bit_pos )--];
    3618      221522 :             for ( j = 0; j < nbands; j++ )
    3619             :             {
    3620      211732 :                 decoded_idx = ivas_qmetadata_DecodeExtendedGR( bitstream, p_bit_pos, 2 * cb_size, GR_param );
    3621      211732 :                 if ( decoded_idx % 2 )
    3622             :                 {
    3623       78270 :                     decoded_idx = ( ( decoded_idx + 1 ) >> 1 ) + min_index;
    3624             :                 }
    3625             :                 else
    3626             :                 {
    3627      133462 :                     decoded_idx = -( decoded_idx >> 1 ) + min_index;
    3628             :                 }
    3629      211732 :                 hQMetaData->q_direction[idx_dir].coherence_band_data[j].spread_coherence[k] = (uint8_t) ( decoded_idx * delta + delta / 2.0f );
    3630             :             }
    3631             :         }
    3632             :         else
    3633             :         {
    3634             :             /* read min_index */
    3635      221672 :             min_index = 0;
    3636      968598 :             for ( i = 0; i < nbits_coh; i++ )
    3637             :             {
    3638      746926 :                 min_index = ( min_index << 1 ) + bitstream[( *p_bit_pos )--];
    3639             :             }
    3640             : 
    3641             :             /* read GR param */
    3642      221672 :             GR_param = bitstream[( *p_bit_pos )--];
    3643     5106681 :             for ( j = 0; j < nbands; j++ )
    3644             :             {
    3645     4885009 :                 decoded_idx = ivas_qmetadata_DecodeExtendedGR( bitstream, p_bit_pos, cb_size - min_index, GR_param ) + min_index;
    3646     4885009 :                 hQMetaData->q_direction[idx_dir].coherence_band_data[j].spread_coherence[k] = (uint8_t) ( decoded_idx * delta + delta / 2.0f );
    3647             :             }
    3648             :         }
    3649             :     }
    3650             : 
    3651       58500 :     nbits = nbits - *p_bit_pos;
    3652             : 
    3653       58500 :     return nbits;
    3654             : }
    3655             : 
    3656             : 
    3657             : /*------------------------------------------------------------------- *
    3658             :  * read_coherence_data()
    3659             :  *
    3660             :  * Read coherence data
    3661             :  *------------------------------------------------------------------- */
    3662             : 
    3663             : /*! r: number of bits read */
    3664      601608 : static int16_t read_coherence_data(
    3665             :     uint16_t *bitstream,        /* i  : bitstream                               */
    3666             :     int16_t *p_bit_pos,         /* i  : position in the bitstream               */
    3667             :     IVAS_QMETADATA *hQMetaData, /* i/o: quantized metadata structure            */
    3668             :     const int16_t idx_dir,      /* i  : direction index                         */
    3669             :     const int16_t hrmasa_flag   /* i  : flag indicating high-rate MASA MD coding*/
    3670             : )
    3671             : {
    3672             :     int16_t j;
    3673             :     int16_t no_cv_vec[MASA_MAXIMUM_CODING_SUBBANDS];
    3674             :     uint64_t no_cb;
    3675             :     int16_t no_bits_vec, nbits;
    3676             :     int16_t bits_GR;
    3677             :     uint16_t idx_dct1[MASA_MAXIMUM_CODING_SUBBANDS];
    3678             :     uint16_t av_index;
    3679             :     int16_t no_bits_vec1;
    3680             :     int16_t bit_pos;
    3681             :     IVAS_QDIRECTION *q_direction;
    3682             :     int16_t coding_subbands;
    3683             :     uint64_t dct0_index;
    3684             :     int16_t decoded_idx[MASA_MAXIMUM_CODING_SUBBANDS];
    3685             :     uint16_t byteBuffer;
    3686             :     int16_t idx_ER;
    3687             :     int16_t min_index;
    3688             :     int16_t extra_cv;
    3689             : 
    3690      601608 :     coding_subbands = hQMetaData->q_direction[idx_dir].cfg.nbands;
    3691      601608 :     extra_cv = (int16_t) ( coding_subbands / MASA_FACTOR_CV_COH );
    3692      601608 :     q_direction = &( hQMetaData->q_direction[idx_dir] );
    3693      601608 :     bit_pos = *p_bit_pos;
    3694      601608 :     nbits = 0;
    3695             : 
    3696      601608 :     if ( q_direction->cfg.nblocks == 1 )
    3697             :     {
    3698      388503 :         for ( j = 0; j < coding_subbands; j++ )
    3699             :         {
    3700      335055 :             if ( hrmasa_flag )
    3701             :             {
    3702           0 :                 idx_ER = 7 - ( q_direction->band_data[j].energy_ratio_index_mod[0] >> 1 ) + extra_cv;
    3703             :             }
    3704             :             else
    3705             :             {
    3706      335055 :                 idx_ER = 7 - q_direction->band_data[j].energy_ratio_index_mod[0] + extra_cv;
    3707             :             }
    3708      335055 :             no_cv_vec[j] = idx_ER + 1;
    3709             :         }
    3710             : 
    3711       53448 :         if ( sum_s( no_cv_vec, coding_subbands ) == coding_subbands )
    3712             :         {
    3713       16601 :             for ( j = 0; j < coding_subbands; j++ )
    3714             :             {
    3715       13668 :                 q_direction->coherence_band_data[j].spread_coherence[0] = 0;
    3716             :             }
    3717             : 
    3718        2933 :             return 0;
    3719             :         }
    3720       50515 :         byteBuffer = bitstream[bit_pos--];
    3721       50515 :         nbits += 1;
    3722             : 
    3723       50515 :         if ( byteBuffer & 1 )
    3724             :         {
    3725             :             /* decode GR min removed */
    3726       21615 :             nbits += read_GR_min_removed_data( bitstream, &bit_pos, no_cv_vec, coding_subbands, decoded_idx, MASA_MAX_NO_CV_SUR_COH + extra_cv );
    3727      193924 :             for ( j = 0; j < coding_subbands; j++ )
    3728             :             {
    3729      172309 :                 if ( no_cv_vec[j] > 1 )
    3730             :                 {
    3731      167577 :                     q_direction->coherence_band_data[j].spread_coherence[0] = (uint8_t) roundf( decoded_idx[j] * ( 255.0f / (float) ( 7 - ( q_direction->band_data[j].energy_ratio_index_mod[0] >> ( hrmasa_flag ) ) + coding_subbands / MASA_FACTOR_CV_COH ) ) );
    3732             :                 }
    3733             :                 else
    3734             :                 {
    3735        4732 :                     q_direction->coherence_band_data[j].spread_coherence[0] = 0;
    3736             :                 }
    3737             :             }
    3738             :         }
    3739             :         else
    3740             :         {
    3741             :             uint16_t decoded_index[MASA_MAXIMUM_CODING_SUBBANDS];
    3742             :             /* decode joint index */
    3743       28900 :             nbits += decode_fixed_rate_composed_index_coherence( bitstream, &bit_pos, coding_subbands, no_cv_vec, decoded_index, MASA_NO_CV_COH + coding_subbands / MASA_FACTOR_CV_COH );
    3744      177978 :             for ( j = 0; j < coding_subbands; j++ )
    3745             :             {
    3746      149078 :                 if ( no_cv_vec[j] > 1 )
    3747             :                 {
    3748      101684 :                     q_direction->coherence_band_data[j].spread_coherence[0] = (uint8_t) roundf( decoded_index[j] * ( 255.0f / (float) ( 7 - ( q_direction->band_data[j].energy_ratio_index_mod[0] >> ( hrmasa_flag ) ) + coding_subbands / MASA_FACTOR_CV_COH ) ) );
    3749             :                 }
    3750             :                 else
    3751             :                 {
    3752       47394 :                     q_direction->coherence_band_data[j].spread_coherence[0] = 0;
    3753             :                 }
    3754             :             }
    3755             :         }
    3756             :     }
    3757             :     else
    3758             :     {
    3759     5573014 :         for ( j = 0; j < coding_subbands; j++ )
    3760             :         {
    3761     5024854 :             if ( hrmasa_flag )
    3762             :             {
    3763           0 :                 minimum_s( (int16_t *) ( q_direction->band_data[j].energy_ratio_index ), q_direction->cfg.nblocks, &min_index );
    3764           0 :                 no_cv_vec[j] = len_cb_dct0_masa[min_index >> 1]; /* spread coherence DCT0*/
    3765             :             }
    3766             :             else
    3767             :             {
    3768     5024854 :                 no_cv_vec[j] = len_cb_dct0_masa[q_direction->band_data[j].energy_ratio_index[0]]; /* spread coherence DCT0*/
    3769             :             }
    3770             :         }
    3771             : 
    3772      548160 :         if ( sum_s( no_cv_vec, coding_subbands ) > MASA_COH_LIMIT_2IDX )
    3773             :         {
    3774             :             uint16_t spr_coh_temp_index[MASA_MAXIMUM_CODING_SUBBANDS];
    3775             : 
    3776       10278 :             no_cb = 1;
    3777             : #ifdef DEBUGGING
    3778             :             assert( coding_subbands % 2 == 0 );
    3779             : #endif
    3780      133614 :             for ( j = 0; j < coding_subbands / 2; j++ )
    3781             :             {
    3782      123336 :                 no_cb *= no_cv_vec[j];
    3783             :             }
    3784             : 
    3785       10278 :             no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3786       10278 :             no_cb = 1;
    3787             : 
    3788      133614 :             for ( j = coding_subbands / 2; j < coding_subbands; j++ )
    3789             :             {
    3790      123336 :                 no_cb *= no_cv_vec[j];
    3791             :             }
    3792             : 
    3793       10278 :             no_bits_vec1 = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3794       10278 :             dct0_index = 0;
    3795             : 
    3796      275491 :             for ( j = 0; j < no_bits_vec; j++ )
    3797             :             {
    3798      265213 :                 dct0_index = ( dct0_index << 1 ) + bitstream[bit_pos--];
    3799             :             }
    3800             : 
    3801       10278 :             nbits += no_bits_vec;
    3802       10278 :             set_s( (int16_t *) spr_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
    3803             : 
    3804       10278 :             decode_combined_index( dct0_index, no_cv_vec, spr_coh_temp_index, coding_subbands / 2 );
    3805             : 
    3806       10278 :             dct0_index = 0;
    3807      252004 :             for ( j = 0; j < no_bits_vec1; j++ )
    3808             :             {
    3809      241726 :                 dct0_index = ( dct0_index << 1 ) + bitstream[bit_pos--];
    3810             :             }
    3811             : 
    3812       10278 :             nbits += no_bits_vec1;
    3813             : 
    3814       10278 :             decode_combined_index( dct0_index, &no_cv_vec[coding_subbands / 2], &spr_coh_temp_index[coding_subbands / 2], coding_subbands / 2 );
    3815             : 
    3816      256950 :             for ( j = 0; j < coding_subbands; j++ )
    3817             :             {
    3818      246672 :                 q_direction->coherence_band_data[j].spread_coherence_dct0_index = spr_coh_temp_index[j];
    3819             :             }
    3820             :         }
    3821             :         else
    3822             :         {
    3823             :             /* spread coherence */
    3824             :             uint16_t spr_coh_temp_index[MASA_MAXIMUM_CODING_SUBBANDS];
    3825             : 
    3826      537882 :             no_cb = 1;
    3827             : 
    3828     5316064 :             for ( j = 0; j < coding_subbands; j++ )
    3829             :             {
    3830     4778182 :                 no_cb *= no_cv_vec[j];
    3831             :             }
    3832             : 
    3833      537882 :             no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3834             : 
    3835             :             /* read joint index for DCT0 */
    3836      537882 :             no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3837      537882 :             dct0_index = 0;
    3838             : 
    3839     9064799 :             for ( j = 0; j < no_bits_vec; j++ )
    3840             :             {
    3841     8526917 :                 dct0_index = ( dct0_index << 1 ) + bitstream[bit_pos--];
    3842             :             }
    3843             : 
    3844      537882 :             nbits += no_bits_vec;
    3845             : 
    3846      537882 :             set_s( (int16_t *) spr_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
    3847             : 
    3848      537882 :             decode_combined_index( dct0_index, no_cv_vec, spr_coh_temp_index, coding_subbands );
    3849             : 
    3850     5316064 :             for ( j = 0; j < coding_subbands; j++ )
    3851             :             {
    3852     4778182 :                 q_direction->coherence_band_data[j].spread_coherence_dct0_index = spr_coh_temp_index[j];
    3853             :             }
    3854             :         }
    3855             : 
    3856             :         /* read GR data for DCT1 */
    3857     5573014 :         for ( j = 0; j < coding_subbands; j++ )
    3858             :         {
    3859     5024854 :             bits_GR = bit_pos;
    3860     5024854 :             idx_dct1[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, 2 * MASA_NO_CV_COH1, 0 );
    3861     5024854 :             nbits += bits_GR - bit_pos;
    3862             :         }
    3863      548160 :         bits_GR = bit_pos; /* just to store the data */
    3864             : 
    3865             :         /* read average index */
    3866      548160 :         read_huf( &bit_pos, bitstream, &av_index, bit_pos, MASA_NO_CV_COH1, huff_code_av_masa, 10 ); /* 10 is MAX_LEN*/
    3867      548160 :         nbits += ( bits_GR - bit_pos );
    3868             : 
    3869             :         /* write indexes in metadata structure  */
    3870     5573014 :         for ( j = 0; j < coding_subbands; j++ )
    3871             :         {
    3872     5024854 :             if ( idx_dct1[j] % 2 )
    3873             :             {
    3874      119073 :                 q_direction->coherence_band_data[j].spread_coherence_dct1_index = ( idx_dct1[j] + 1 ) / 2 + av_index;
    3875             :             }
    3876             :             else
    3877             :             {
    3878     4905781 :                 q_direction->coherence_band_data[j].spread_coherence_dct1_index = -idx_dct1[j] / 2 + av_index;
    3879             :             }
    3880             :         }
    3881             :     }
    3882             : #ifdef DEBUGGING
    3883             :     assert( nbits == *p_bit_pos - bit_pos );
    3884             : #endif
    3885      598675 :     nbits = *p_bit_pos - bit_pos;
    3886             : 
    3887      598675 :     *p_bit_pos = bit_pos;
    3888             : 
    3889      598675 :     return nbits;
    3890             : }
    3891             : 
    3892             : 
    3893             : /*-------------------------------------------------------------------*
    3894             :  * read_surround_coherence()
    3895             :  *
    3896             :  *
    3897             :  *-------------------------------------------------------------------*/
    3898             : 
    3899      558332 : static int16_t read_surround_coherence(
    3900             :     uint16_t *bitstream,       /* i  : bitstream                       */
    3901             :     int16_t *p_bit_pos,        /* i  : position in the bitstream       */
    3902             :     IVAS_QMETADATA *hQMetaData /* i/o: quantized metadata structure    */
    3903             : )
    3904             : {
    3905             :     int16_t coding_subbands;
    3906             :     int16_t no_cv_vec[MASA_MAXIMUM_CODING_SUBBANDS];
    3907             :     int16_t bit_pos;
    3908             :     float error_ratio_surr;
    3909             :     int16_t idx_ER[MASA_MAXIMUM_CODING_SUBBANDS];
    3910             :     int16_t bits_sur_coherence, bits_GR;
    3911             :     int16_t j, d, k, idx;
    3912             :     uint16_t byteBuffer;
    3913             :     uint16_t idx_sur_coh[MASA_MAXIMUM_CODING_SUBBANDS];
    3914             :     IVAS_QDIRECTION *q_direction;
    3915             :     int16_t min_index;
    3916             : 
    3917      558332 :     coding_subbands = hQMetaData->q_direction[0].cfg.nbands;
    3918      558332 :     q_direction = hQMetaData->q_direction;
    3919             : 
    3920      558332 :     bits_sur_coherence = 0;
    3921      558332 :     bit_pos = *p_bit_pos;
    3922             : 
    3923      558332 :     d = 0;
    3924     5771292 :     for ( j = 0; j < coding_subbands; j++ )
    3925             :     {
    3926     5212960 :         error_ratio_surr = 1.0f;
    3927             : 
    3928     5212960 :         if ( hQMetaData->no_directions == 2 )
    3929             :         {
    3930      312012 :             d += hQMetaData->twoDirBands[j];
    3931      312012 :             idx = max( d - 1, 0 );
    3932      312012 :             error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[0] - q_direction[1].band_data[idx].energy_ratio[0] * hQMetaData->twoDirBands[j];
    3933             :         }
    3934             :         else
    3935             :         {
    3936     4900948 :             error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[0];
    3937             :         }
    3938             : 
    3939     5212960 :         if ( error_ratio_surr <= 0 )
    3940             :         {
    3941        1675 :             error_ratio_surr = 0;
    3942        1675 :             no_cv_vec[j] = 1;
    3943        1675 :             idx_ER[j] = masa_sq( 0.0f, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
    3944             :         }
    3945             :         else
    3946             :         {
    3947     5211285 :             idx_ER[j] = masa_sq( error_ratio_surr, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
    3948     5211285 :             no_cv_vec[j] = idx_cb_sur_coh_masa[idx_ER[j]] + 2;
    3949             :         }
    3950             :     }
    3951             : 
    3952      558332 :     if ( sum_s( no_cv_vec, coding_subbands ) == coding_subbands )
    3953             :     {
    3954             :         /* surround coherence is zero */
    3955           0 :         for ( j = 0; j < coding_subbands; j++ )
    3956             :         {
    3957           0 :             for ( k = 0; k < MAX_PARAM_SPATIAL_SUBFRAMES; k++ )
    3958             :             {
    3959           0 :                 if ( hQMetaData->surcoh_band_data != NULL )
    3960             :                 {
    3961           0 :                     hQMetaData->surcoh_band_data[j].surround_coherence[k] = 0;
    3962             :                 }
    3963             :             }
    3964             :         }
    3965             : 
    3966           0 :         return bits_sur_coherence;
    3967             :     }
    3968             : 
    3969             :     /* read how the surround coherence is encoded */
    3970      558332 :     byteBuffer = bitstream[bit_pos--];
    3971      558332 :     bits_sur_coherence += 1;
    3972             : 
    3973      558332 :     if ( byteBuffer & 1 )
    3974             :     {
    3975             :         /* GR decoding */
    3976             :         /* read GR order */
    3977      481979 :         byteBuffer = bitstream[bit_pos--];
    3978      481979 :         bits_sur_coherence += 1;
    3979             : 
    3980             :         /* read min index */
    3981      481979 :         bits_GR = bit_pos;
    3982      481979 :         min_index = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, MASA_MAX_NO_CV_SUR_COH, 0 );
    3983      481979 :         bits_sur_coherence += bits_GR - bit_pos;
    3984             : 
    3985             :         /* read GR data */
    3986     5146583 :         for ( j = 0; j < coding_subbands; j++ )
    3987             :         {
    3988     4664604 :             bits_GR = bit_pos;
    3989             :             /* decoding for min removed */
    3990     4664604 :             if ( no_cv_vec[j] > 1 )
    3991             :             {
    3992     4663135 :                 idx_sur_coh[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_cv_vec[j] - min_index, ( byteBuffer & 1 ) );
    3993     4663135 :                 bits_sur_coherence += bits_GR - bit_pos;
    3994             :             }
    3995             :             else
    3996             :             {
    3997        1469 :                 idx_sur_coh[j] = 0;
    3998             :             }
    3999             :         }
    4000             : 
    4001     5146583 :         for ( j = 0; j < coding_subbands; j++ )
    4002             :         {
    4003     4664604 :             if ( no_cv_vec[j] > 1 )
    4004             :             {
    4005     4663135 :                 hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j] + min_index;
    4006             :             }
    4007             :             else
    4008             :             {
    4009        1469 :                 hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j];
    4010             :             }
    4011             : 
    4012     4664604 :             hQMetaData->surcoh_band_data[j].surround_coherence[0] = sur_coherence_cb_masa[idx_cb_sur_coh_masa[idx_ER[j]] * MASA_MAX_NO_CV_SUR_COH + hQMetaData->surcoh_band_data[j].sur_coherence_index];
    4013             :         }
    4014             :     }
    4015             :     else
    4016             :     {
    4017             :         /* fixed rate */
    4018             :         uint16_t sur_coh_temp_index[MASA_MAXIMUM_CODING_SUBBANDS];
    4019       76353 :         set_s( (int16_t *) sur_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
    4020             : 
    4021       76353 :         decode_fixed_rate_composed_index_coherence( bitstream, &bit_pos, coding_subbands, no_cv_vec, sur_coh_temp_index, MASA_MAX_NO_CV_SUR_COH );
    4022             : 
    4023      624709 :         for ( j = 0; j < coding_subbands; j++ )
    4024             :         {
    4025      548356 :             hQMetaData->surcoh_band_data[j].sur_coherence_index = sur_coh_temp_index[j];
    4026             :         }
    4027             : 
    4028             :         /* deindex surround coherence */
    4029      624709 :         for ( j = 0; j < coding_subbands; j++ )
    4030             :         {
    4031      548356 :             if ( no_cv_vec[j] > 1 )
    4032             :             {
    4033      516324 :                 hQMetaData->surcoh_band_data[j].surround_coherence[0] = sur_coherence_cb_masa[idx_cb_sur_coh_masa[idx_ER[j]] * MASA_MAX_NO_CV_SUR_COH + hQMetaData->surcoh_band_data[j].sur_coherence_index];
    4034             :             }
    4035             :             else
    4036             :             {
    4037       32032 :                 hQMetaData->surcoh_band_data[j].surround_coherence[0] = 0;
    4038             :             }
    4039             :         }
    4040             :     }
    4041             : 
    4042     5771292 :     for ( j = 0; j < coding_subbands; j++ )
    4043             :     {
    4044    20851840 :         for ( k = 1; k < MAX_PARAM_SPATIAL_SUBFRAMES; k++ )
    4045             :         {
    4046    15638880 :             hQMetaData->surcoh_band_data[j].surround_coherence[k] = hQMetaData->surcoh_band_data[j].surround_coherence[0];
    4047             :         }
    4048             :     }
    4049             : 
    4050             :     /* Replace return value with the actual read bits. bits_sur_coherence might show wrong count at this point. */
    4051      558332 :     bits_sur_coherence = *p_bit_pos - bit_pos;
    4052      558332 :     *p_bit_pos = bit_pos;
    4053             : 
    4054      558332 :     return bits_sur_coherence;
    4055             : }
    4056             : 
    4057             : 
    4058             : /*-------------------------------------------------------------------*
    4059             :  * read_surround_coherence_hr()
    4060             :  *
    4061             :  *
    4062             :  *-------------------------------------------------------------------*/
    4063             : 
    4064       54633 : static int16_t read_surround_coherence_hr(
    4065             :     uint16_t *bitstream,       /* i  : bitstream                       */
    4066             :     int16_t *p_bit_pos,        /* i  : position in the bitstream       */
    4067             :     IVAS_QMETADATA *hQMetaData /* i/o: quantized metadata structure    */
    4068             : )
    4069             : {
    4070             :     int16_t coding_subbands;
    4071             :     int16_t no_cv_vec[MASA_MAXIMUM_CODING_SUBBANDS];
    4072             :     int16_t bit_pos;
    4073             :     float error_ratio_surr;
    4074             :     int16_t idx_ER[MASA_MAXIMUM_CODING_SUBBANDS];
    4075             :     int16_t bits_sur_coherence, bits_GR;
    4076             :     int16_t j, k, sf;
    4077             :     uint16_t byteBuffer;
    4078             :     uint16_t idx_sur_coh[MASA_MAXIMUM_CODING_SUBBANDS];
    4079             :     IVAS_QDIRECTION *q_direction;
    4080             :     int16_t min_index;
    4081             :     int16_t d, idx;
    4082             :     int32_t int_error_ratio_surr;
    4083             : 
    4084       54633 :     coding_subbands = hQMetaData->q_direction[0].cfg.nbands;
    4085       54633 :     q_direction = hQMetaData->q_direction;
    4086             : 
    4087       54633 :     bits_sur_coherence = 0;
    4088       54633 :     bit_pos = *p_bit_pos;
    4089             : 
    4090      271896 :     for ( sf = 0; sf < hQMetaData->q_direction[0].cfg.nblocks; sf++ )
    4091             :     {
    4092      217263 :         d = 0;
    4093     5112944 :         for ( j = 0; j < coding_subbands; j++ )
    4094             :         {
    4095     4895681 :             error_ratio_surr = 1.0f;
    4096     4895681 :             if ( hQMetaData->no_directions == 2 )
    4097             :             {
    4098      338625 :                 d += hQMetaData->twoDirBands[j];
    4099      338625 :                 idx = max( d - 1, 0 );
    4100      338625 :                 error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[sf] - q_direction[1].band_data[idx].energy_ratio[sf] * hQMetaData->twoDirBands[j];
    4101             :             }
    4102             :             else
    4103             :             {
    4104     4557056 :                 error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[sf];
    4105             :             }
    4106             : 
    4107     4895681 :             int_error_ratio_surr = (int32_t) ( MASA_SUR_COH_PRECISION * error_ratio_surr );
    4108     4895681 :             error_ratio_surr = (float) ( int_error_ratio_surr * MASA_SUR_COH_THRESHOLD );
    4109             : 
    4110     4895681 :             if ( error_ratio_surr <= 0 )
    4111             :             {
    4112        6328 :                 error_ratio_surr = 0;
    4113        6328 :                 no_cv_vec[j] = 1;
    4114        6328 :                 idx_ER[j] = 0;
    4115             :             }
    4116             :             else
    4117             :             {
    4118     4889353 :                 idx_ER[j] = 7; /* masa_sq( error_ratio_surr, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS ); */
    4119     4889353 :                 no_cv_vec[j] = idx_cb_sur_coh_masa[idx_ER[j]] + 2;
    4120             :             }
    4121             :         }
    4122             : 
    4123      217263 :         if ( sum_s( no_cv_vec, coding_subbands ) == coding_subbands )
    4124             :         {
    4125             :             /* surround coherence is zero */
    4126           0 :             for ( j = 0; j < coding_subbands; j++ )
    4127             :             {
    4128           0 :                 for ( k = 0; k < MAX_PARAM_SPATIAL_SUBFRAMES; k++ )
    4129             :                 {
    4130           0 :                     if ( hQMetaData->surcoh_band_data != NULL )
    4131             :                     {
    4132           0 :                         hQMetaData->surcoh_band_data[j].surround_coherence[k] = 0;
    4133             :                     }
    4134             :                 }
    4135             :             }
    4136             :         }
    4137             :         else
    4138             :         {
    4139             :             /* read how the surround coherence is encoded */
    4140      217263 :             byteBuffer = bitstream[bit_pos--];
    4141      217263 :             bits_sur_coherence += 1;
    4142             : 
    4143      217263 :             if ( byteBuffer & 1 )
    4144             :             {
    4145             :                 /* GR decoding */
    4146             :                 /* read GR order */
    4147      200785 :                 byteBuffer = bitstream[bit_pos--];
    4148      200785 :                 bits_sur_coherence += 1;
    4149             : 
    4150             :                 /* read min index */
    4151      200785 :                 bits_GR = bit_pos;
    4152      200785 :                 min_index = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, MASA_MAX_NO_CV_SUR_COH, 0 );
    4153      200785 :                 bits_sur_coherence += bits_GR - bit_pos;
    4154             : 
    4155             :                 /* read GR data */
    4156     4732384 :                 for ( j = 0; j < coding_subbands; j++ )
    4157             :                 {
    4158     4531599 :                     bits_GR = bit_pos;
    4159             :                     /* decoding for min removed */
    4160     4531599 :                     if ( no_cv_vec[j] > 1 )
    4161             :                     {
    4162     4525343 :                         idx_sur_coh[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_cv_vec[j] - min_index, ( byteBuffer & 1 ) );
    4163     4525343 :                         bits_sur_coherence += bits_GR - bit_pos;
    4164             :                     }
    4165             :                     else
    4166             :                     {
    4167        6256 :                         idx_sur_coh[j] = 0;
    4168             :                     }
    4169             :                 }
    4170             : 
    4171     4732384 :                 for ( j = 0; j < coding_subbands; j++ )
    4172             :                 {
    4173     4531599 :                     if ( no_cv_vec[j] > 1 )
    4174             :                     {
    4175     4525343 :                         hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j] + min_index;
    4176     4525343 :                         hQMetaData->surcoh_band_data[j].surround_coherence[sf] = sur_coherence_cb_masa[idx_cb_sur_coh_masa[idx_ER[j]] * MASA_MAX_NO_CV_SUR_COH + hQMetaData->surcoh_band_data[j].sur_coherence_index];
    4177             :                     }
    4178             :                     else
    4179             :                     {
    4180        6256 :                         hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j];
    4181        6256 :                         hQMetaData->surcoh_band_data[j].surround_coherence[sf] = 0;
    4182             :                     }
    4183             :                 }
    4184             :             }
    4185             :             else
    4186             :             {
    4187             :                 /* fixed rate */
    4188             :                 uint16_t sur_coh_temp_index[MASA_MAXIMUM_CODING_SUBBANDS];
    4189       16478 :                 set_s( (int16_t *) sur_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
    4190             : 
    4191       16478 :                 decode_fixed_rate_composed_index_coherence( bitstream, &bit_pos, coding_subbands, no_cv_vec, sur_coh_temp_index, MASA_MAX_NO_CV_SUR_COH );
    4192             : 
    4193      380560 :                 for ( j = 0; j < coding_subbands; j++ )
    4194             :                 {
    4195      364082 :                     hQMetaData->surcoh_band_data[j].sur_coherence_index = sur_coh_temp_index[j];
    4196             :                 }
    4197             : 
    4198             :                 /* deindex surround coherence */
    4199      380560 :                 for ( j = 0; j < coding_subbands; j++ )
    4200             :                 {
    4201      364082 :                     if ( no_cv_vec[j] > 1 )
    4202             :                     {
    4203      352407 :                         hQMetaData->surcoh_band_data[j].surround_coherence[sf] = sur_coherence_cb_masa[idx_cb_sur_coh_masa[idx_ER[j]] * MASA_MAX_NO_CV_SUR_COH + hQMetaData->surcoh_band_data[j].sur_coherence_index];
    4204             :                     }
    4205             :                     else
    4206             :                     {
    4207       11675 :                         hQMetaData->surcoh_band_data[j].surround_coherence[sf] = 0;
    4208             :                     }
    4209             :                 }
    4210             :             }
    4211             :         }
    4212             :     }
    4213             : 
    4214             :     /* Replace return value with the actual read bits. bits_sur_coherence might show wrong count at this point. */
    4215       54633 :     bits_sur_coherence = *p_bit_pos - bit_pos;
    4216       54633 :     *p_bit_pos = bit_pos;
    4217             : 
    4218       54633 :     return bits_sur_coherence;
    4219             : }
    4220             : 
    4221             : 
    4222             : /*-------------------------------------------------------------------*
    4223             :  * decode_combined_index()
    4224             :  *
    4225             :  * Decode combined index into several indexes
    4226             :  *-------------------------------------------------------------------*/
    4227             : 
    4228      699986 : static void decode_combined_index(
    4229             :     uint64_t comb_index,      /* i  : index to be decoded                 */
    4230             :     const int16_t *no_cv_vec, /* i  : number of codewords for each element*/
    4231             :     uint16_t *index,          /* o  : decoded indexes                     */
    4232             :     const int16_t len         /* i  : number of elements                  */
    4233             : )
    4234             : {
    4235             :     int16_t i;
    4236             :     uint64_t base[MASA_MAXIMUM_CODING_SUBBANDS + 1];
    4237             : 
    4238      699986 :     base[0] = 1;
    4239     6081672 :     for ( i = 1; i < len; i++ )
    4240             :     {
    4241     5381686 :         base[i] = base[i - 1] * no_cv_vec[i - 1];
    4242             :     }
    4243             : 
    4244     6081672 :     for ( i = len - 1; i > 0; i-- )
    4245             :     {
    4246     5381686 :         index[i] = (uint16_t) ( comb_index / base[i] );
    4247     5381686 :         comb_index -= index[i] * base[i];
    4248             :     }
    4249             : 
    4250      699986 :     index[0] = (uint16_t) comb_index;
    4251             : 
    4252      699986 :     return;
    4253             : }
    4254             : 
    4255             : 
    4256       63232 : static void read_stream_dct_coeffs_omasa(
    4257             :     int16_t *q_idx,
    4258             :     Word32 *q_dct_data_fx,
    4259             :     const int16_t len_stream,
    4260             :     uint16_t *bit_stream,
    4261             :     int16_t *index,
    4262             :     const int16_t first_line )
    4263             : {
    4264             :     int16_t sign;
    4265             :     int16_t i, j, i_min;
    4266             :     int16_t GR1, GR2;
    4267             :     int64_t step_fx;
    4268       63232 :     step_fx = STEP_M2T_FX;
    4269       63232 :     sign = 1;
    4270       63232 :     if ( first_line == 0 )
    4271             :     {
    4272             :         /* read sign */
    4273           0 :         sign = bit_stream[( *index )--];
    4274           0 :         if ( sign == 0 )
    4275             :         {
    4276           0 :             sign = -1;
    4277             :         }
    4278             :     }
    4279             : 
    4280       63232 :     set_s( q_idx, 0, len_stream );
    4281             :     /* read DCT 0 component */
    4282      442624 :     for ( i = 0; i < BITS_MASA2TOTTAL_DCT0; i++ )
    4283             :     {
    4284      379392 :         q_idx[0] = ( q_idx[0] << 1 ) + bit_stream[( *index )--];
    4285             :     }
    4286       63232 :     q_idx[0] *= sign;
    4287             : 
    4288       63232 :     if ( q_idx[0] != 0 )
    4289             :     {
    4290       62824 :         if ( len_stream >= 8 )
    4291             :         {
    4292             :             /* read index of last index encoded with GR2 */
    4293       53500 :             i_min = 0;
    4294       53500 :             j = 4;
    4295      267500 :             for ( i = 0; i < j; i++ )
    4296             :             {
    4297      214000 :                 i_min = ( i_min << 1 ) + bit_stream[( *index )--];
    4298             :             }
    4299             : 
    4300             :             /* read GR orders */
    4301       53500 :             GR1 = bit_stream[( *index )--] + 1;
    4302       53500 :             if ( GR1 == 2 )
    4303             :             {
    4304       43870 :                 GR2 = bit_stream[( *index )--];
    4305             :             }
    4306             :             else
    4307             :             {
    4308        9630 :                 GR2 = 0;
    4309             :             }
    4310             : 
    4311             :             /* read GR data */
    4312      292805 :             for ( i = 1; i <= i_min; i++ )
    4313             :             {
    4314      239305 :                 q_idx[i] = ivas_qmetadata_DecodeExtendedGR( bit_stream, index, 100, GR1 );
    4315             :             }
    4316      822055 :             for ( i = i_min + 1; i < len_stream; i++ )
    4317             :             {
    4318      768555 :                 q_idx[i] = ivas_qmetadata_DecodeExtendedGR( bit_stream, index, 100, GR2 );
    4319             :             }
    4320             :         }
    4321             :         else
    4322             :         {
    4323             :             /* read GR order (only one)  */
    4324        9324 :             GR1 = bit_stream[( *index )--];
    4325       45674 :             for ( i = 1; i < len_stream; i++ )
    4326             :             {
    4327       36350 :                 q_idx[i] = ivas_qmetadata_DecodeExtendedGR( bit_stream, index, 100, GR1 );
    4328             :             }
    4329             :         }
    4330             :     }
    4331             : 
    4332             :     /* deindex */
    4333       63232 :     q_dct_data_fx[0] = (Word32) ( ( step_fx * q_idx[0] ) >> 6 ); /* Q25 */
    4334     1115194 :     for ( i = 1; i < len_stream; i++ )
    4335             :     {
    4336     1051962 :         if ( ( q_idx[i] & 1 ) == 0 )
    4337             :         {
    4338      693436 :             q_dct_data_fx[i] = (Word32) ( ( step_fx * ( -q_idx[i] ) ) >> 7 ); /* Q25 */
    4339             :         }
    4340             :         ELSE
    4341             :         {
    4342      358526 :             q_dct_data_fx[i] = (Word32) ( ( step_fx * ( q_idx[i] + 1 ) ) >> 7 ); /* Q25 */
    4343             :         }
    4344             :     }
    4345       63232 :     return;
    4346             : }
    4347             : 
    4348             : 
    4349             : /*-------------------------------------------------------------------------
    4350             :  * ivas_omasa_decode_masa_to_total()
    4351             :  *
    4352             :  *------------------------------------------------------------------------*/
    4353             : 
    4354       63232 : void ivas_omasa_decode_masa_to_total(
    4355             :     uint16_t *bit_stream,
    4356             :     int16_t *index,
    4357             :     float masa_to_total_energy_ratio[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS],
    4358             :     const int16_t nbands,
    4359             :     const int16_t nblocks )
    4360             : {
    4361             :     int16_t i, j, k;
    4362             :     int16_t q_idx[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
    4363             :     Word32 q_dct_data_fx[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS],
    4364             :         dct_data_tmp_fx[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
    4365             :     int16_t n_streams, len_stream;
    4366             : 
    4367             :     /* Setup coding parameters */
    4368       63232 :     n_streams = 1;
    4369       63232 :     len_stream = nbands * nblocks;
    4370       63232 :     if ( len_stream == 32 )
    4371             :     {
    4372           0 :         n_streams = 4;
    4373           0 :         len_stream = 8;
    4374             :     }
    4375             : 
    4376       63232 :     set_s( q_idx, 0, nbands * nblocks );
    4377      126464 :     for ( i = 0; i < n_streams; i++ )
    4378             :     {
    4379       63232 :         read_stream_dct_coeffs_omasa( &q_idx[i * len_stream],
    4380       63232 :                                       &q_dct_data_fx[i * len_stream],
    4381             :                                       len_stream, bit_stream, index, i == 0 );
    4382             :     }
    4383             : 
    4384             :     /* inverse DCT2 transform */
    4385       63232 :     SWITCH( len_stream )
    4386             :     {
    4387         946 :         case 4:
    4388         946 :             matrix_product_q30_fx( dct4_fx, nblocks, nblocks, 1, q_dct_data_fx, nblocks, 1, 0, dct_data_tmp_fx );
    4389         946 :             mvl2l( dct_data_tmp_fx, q_dct_data_fx, nblocks ); /*Q30*/
    4390         946 :             BREAK;
    4391        8378 :         case 5:
    4392        8378 :             matrix_product_q30_fx( dct5_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
    4393        8378 :             mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
    4394        8378 :             BREAK;
    4395           0 :         case 8:
    4396           0 :             matrix_product_q30_fx( dct8_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
    4397           0 :             mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
    4398           0 :             BREAK;
    4399        1080 :         case 12:
    4400        1080 :             matrix_product_q30_fx( dct12_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
    4401        1080 :             mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
    4402        1080 :             BREAK;
    4403       52828 :         case 20:
    4404       52828 :             matrix_product_fx( dct5_fx, nbands, nbands, 1, q_dct_data_fx, nbands, nblocks, 0, dct_data_tmp_fx );
    4405       52828 :             matrix_product_q30_fx( dct_data_tmp_fx, nbands, nblocks, 0, dct4_fx, nblocks, nblocks, 0, q_dct_data_fx ); /* reuse of variable*/
    4406       52828 :             BREAK;
    4407           0 :         case 32:
    4408           0 :             matrix_product_fx( dct8_fx, nbands, nbands, 1, q_dct_data_fx, nbands, nblocks, 0, dct_data_tmp_fx );
    4409           0 :             matrix_product_q30_fx( dct_data_tmp_fx, nbands, nblocks, 0, dct4_fx, nblocks, nblocks, 0, q_dct_data_fx );
    4410           0 :             BREAK;
    4411           0 :         default:
    4412           0 :             printf( "Incorrect number of coefficients for OMASA.\n" );
    4413           0 :             BREAK;
    4414             :     }
    4415             : 
    4416             :     /* this is to make sure the comparison to the threshold 0.98 will go the same way in */
    4417             :     /* fixed point and floating point without having to drag the fixed point values to the */
    4418             :     /* comparison place in the code; 1052266987 is 0.98 in Q30 it is not needed in the fixed point*/
    4419             : 
    4420     1178426 :     for ( i = 0; i < nblocks * nbands; i++ )
    4421             :     {
    4422     1115194 :         if ( q_dct_data_fx[i] >= 1052266987 && q_dct_data_fx[i] < 1052400000 )
    4423             :         {
    4424         366 :             q_dct_data_fx[i] = 1052400000;
    4425             :         }
    4426             :     }
    4427             : 
    4428       63232 :     k = 0;
    4429      287786 :     for ( i = 0; i < nblocks; i++ )
    4430             :     {
    4431     1339748 :         for ( j = 0; j < nbands; j++ )
    4432             :         {
    4433     1115194 :             masa_to_total_energy_ratio[i][j] = q_dct_data_fx[k] / (float) ( 1 << 30 );
    4434     1115194 :             masa_to_total_energy_ratio[i][j] = max( 0.0f, masa_to_total_energy_ratio[i][j] );
    4435     1115194 :             masa_to_total_energy_ratio[i][j] = min( 1.0f, masa_to_total_energy_ratio[i][j] );
    4436     1115194 :             k++;
    4437             :         }
    4438             :     }
    4439             : 
    4440       63232 :     if ( nblocks == 1 )
    4441             :     {
    4442       37832 :         for ( i = 1; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
    4443             :         {
    4444      192924 :             for ( j = 0; j < nbands; j++ )
    4445             :             {
    4446      164550 :                 masa_to_total_energy_ratio[i][j] = masa_to_total_energy_ratio[0][j];
    4447             :             }
    4448             :         }
    4449             :     }
    4450             : 
    4451       63232 :     if ( nbands == 1 )
    4452             :     {
    4453        4730 :         for ( j = 1; j < 5; j++ )
    4454             :         {
    4455       18920 :             for ( i = 0; i < nblocks; i++ )
    4456             :             {
    4457       15136 :                 masa_to_total_energy_ratio[i][j] = masa_to_total_energy_ratio[i][0];
    4458             :             }
    4459             :         }
    4460             :     }
    4461             : 
    4462       63232 :     return;
    4463             : }

Generated by: LCOV version 1.14