LCOV - code coverage report
Current view: top level - lib_dec - ivas_qmetadata_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- conformance test test_26252.py @ a21f94bc6bac334fe001a5bad2f7b32b79038097 Lines: 1384 1498 92.4 %
Date: 2025-11-01 05:07:43 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      337522 : 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      337522 :     ec_flag = 0;
     178      337522 :     start_index_0 = *index;
     179             : 
     180             :     /*Coherence flag decoding*/
     181      337522 :     bits_no_dirs_coh = 0;
     182      337522 :     all_coherence_zero = 1;
     183      337522 :     if ( hQMetaData->coherence_flag )
     184             :     {
     185             :         /* read if coherence is zero */
     186       58294 :         all_coherence_zero = bitstream[( *index )--];
     187       58294 :         bits_no_dirs_coh += 1;
     188             :     }
     189             : 
     190      337522 :     hQMetaData->all_coherence_zero = (uint8_t) all_coherence_zero;
     191             : 
     192      337522 :     if ( hQMetaData->no_directions == 2 )
     193             :     {
     194             :         /* Read which bands have 2 directions */
     195       52964 :         hQMetaData->q_direction[1].cfg.nbands = hQMetaData->numTwoDirBands;
     196       52964 :         set_c( (int8_t *) hQMetaData->twoDirBands, 0, hQMetaData->q_direction[0].cfg.nbands );
     197       52964 :         d = *index;
     198       52964 :         dif_p[0] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 );
     199       52964 :         p[0] = dif_p[0];
     200       52964 :         hQMetaData->twoDirBands[p[0]] = 1;
     201      427193 :         for ( b = 1; b < hQMetaData->numTwoDirBands; b++ )
     202             :         {
     203      374229 :             dif_p[b] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 );
     204      374229 :             p[b] = p[b - 1] + dif_p[b] + 1;
     205      374229 :             hQMetaData->twoDirBands[p[b]] = 1;
     206             :         }
     207       52964 :         bits_no_dirs_coh += ( d - *index );
     208             :     }
     209             : 
     210      337522 :     bits_diff_sum = 0;
     211      337522 :     bits_diff_sum += ivas_qmetadata_entropy_decode_diffuseness( bitstream, index, &( hQMetaData->q_direction[0] ), &diffuseness_index_max_ec_frame_pre[0] );
     212             : 
     213      337522 :     if ( hodirac_flag )
     214             :     {
     215       32881 :         if ( hQMetaData->no_directions == 2 )
     216             :         {
     217             :             /* Calculate bits for dfRatio */
     218       32881 :             dir2band = 0;
     219      394572 :             for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     220             :             {
     221      361691 :                 if ( hQMetaData->twoDirBands[b] == 1 )
     222             :                 {
     223      361691 :                     dfRatio_bits[dir2band] = ivas_get_df_ratio_bits_hodirac( hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0] );
     224      361691 :                     dir2band++;
     225             :                 }
     226             :             }
     227             : 
     228       32881 :             bits_diff_sum += ivas_qmetadata_entropy_decode_df_ratio( bitstream, index, &( hQMetaData->q_direction[1] ), dfRatio_bits );
     229             :         }
     230             :     }
     231             :     else
     232             :     {
     233      304641 :         if ( hQMetaData->no_directions == 2 )
     234             :         {
     235             :             /* Calculate bits for dfRatio */
     236       20083 :             dir2band = 0;
     237      175581 :             for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     238             :             {
     239      155498 :                 if ( hQMetaData->twoDirBands[b] == 1 )
     240             :                 {
     241       65502 :                     dfRatio_bits[dir2band] = ivas_get_df_ratio_bits( hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0] );
     242       65502 :                     dir2band++;
     243             :                 }
     244             :             }
     245             : 
     246       20083 :             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      337522 :     if ( hQMetaData->no_directions == 2 )
     252             :     {
     253       52964 :         dir2band = 0;
     254      570153 :         for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     255             :         {
     256      517189 :             if ( hQMetaData->twoDirBands[b] == 1 )
     257             :             {
     258             :                 float diffRatio, dfRatio, dir1ratio, dir2ratio;
     259             :                 int16_t dfRatio_qsteps;
     260             : 
     261      427193 :                 diffRatio = diffuseness_reconstructions[hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0]];
     262      427193 :                 dfRatio_qsteps = 1 << dfRatio_bits[dir2band];
     263             :                 /* already encoded as total and ratios in HO-DirAC */
     264      427193 :                 if ( hodirac_flag )
     265             :                 {
     266      361691 :                     dfRatio = usdequant( hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0], 0.0f, 1.f / ( dfRatio_qsteps - 1 ) );
     267      361691 :                     dir1ratio = 1.f - diffRatio;
     268      361691 :                     dir2ratio = dfRatio;
     269             :                 }
     270             :                 else
     271             :                 {
     272       65502 :                     dfRatio = usdequant( hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0], 0.5f, 0.5f / ( dfRatio_qsteps - 1 ) );
     273             : 
     274       65502 :                     dir1ratio = dfRatio * ( 1.0f - diffRatio );
     275       65502 :                     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      427193 :                 hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0] = masa_sq( 1.0f - dir1ratio, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
     280      427193 :                 if ( hodirac_flag )
     281             :                 {
     282             :                     float tmp;
     283      361691 :                     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       65502 :                     hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0] = masa_sq( 1.0f - dir2ratio, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
     288             :                 }
     289             : 
     290     2127046 :                 for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     291             :                 {
     292     1699853 :                     hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = dir1ratio;
     293     1699853 :                     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     2127046 :                 for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
     297             :                 {
     298     1699853 :                     hQMetaData->q_direction[1].band_data[dir2band].energy_ratio[m] = dir2ratio;
     299     1699853 :                     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      427193 :                 dir2band++;
     303             :             }
     304             :             else
     305             :             {
     306             :                 /* 1dir band */
     307       89996 :                 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      328115 :                 for ( m = 1; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     309             :                 {
     310      238119 :                     hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio[0];
     311      238119 :                     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     1435250 :         for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     320             :         {
     321     1150692 :             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     3735246 :             for ( m = 1; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     323             :             {
     324     2584554 :                 hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio[0];
     325     2584554 :                 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      337522 :     bits_dir_raw_pre[0] = 0;
     333      337522 :     bits_dir_raw_pre[1] = 0;
     334      337522 :     dir2band = 0;
     335     2005403 :     for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     336             :     {
     337     1667881 :         if ( hQMetaData->no_directions == 2 && hQMetaData->twoDirBands[b] == 1 )
     338      427193 :         {
     339             :             int16_t index_dirRatio1Inv, index_dirRatio2Inv, index_dirRatio1Inv_mod, index_dirRatio2Inv_mod;
     340             : 
     341      427193 :             index_dirRatio1Inv = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
     342      427193 :             index_dirRatio2Inv = hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0];
     343             : 
     344      427193 :             masa_compensate_two_dir_energy_ratio_index( index_dirRatio1Inv, index_dirRatio2Inv, &index_dirRatio1Inv_mod, &index_dirRatio2Inv_mod, hodirac_flag );
     345             : 
     346     2127046 :             for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     347             :             {
     348     1699853 :                 hQMetaData->q_direction[0].band_data[b].energy_ratio_index_mod[m] = index_dirRatio1Inv_mod;
     349     1699853 :                 hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m] = bits_direction_masa[index_dirRatio1Inv_mod];
     350     1699853 :                 bits_dir_raw_pre[0] += hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m];
     351             :             }
     352             : 
     353     2127046 :             for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
     354             :             {
     355     1699853 :                 hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index_mod[m] = index_dirRatio2Inv_mod;
     356     1699853 :                 hQMetaData->q_direction[1].band_data[dir2band].bits_sph_idx[m] = bits_direction_masa[index_dirRatio2Inv_mod];
     357     1699853 :                 bits_dir_raw_pre[1] += hQMetaData->q_direction[1].band_data[dir2band].bits_sph_idx[m];
     358             :             }
     359             : 
     360      427193 :             dir2band++;
     361             :         }
     362             :         else
     363             :         {
     364     5304049 :             for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     365             :             {
     366     4063361 :                 hQMetaData->q_direction[0].band_data[b].energy_ratio_index_mod[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
     367     4063361 :                 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     4063361 :                 bits_dir_raw_pre[0] += hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m];
     369             :             }
     370             :         }
     371             :     }
     372             : 
     373      337522 :     if ( hQMetaData->no_directions == 2 )
     374             :     {
     375       52964 :         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       52964 :         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       20083 :             bits_sur_coherence = read_surround_coherence( bitstream, index, hQMetaData );
     379             :         }
     380             :         else
     381             :         {
     382       32881 :             bits_sur_coherence = 0;
     383             :             /*Surround coherence*/
     384      394572 :             for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     385             :             {
     386      361691 :                 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       52964 :         bits_no_dirs_coh += bits_sur_coherence;
     393       52964 :         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      284558 :         no_TF = hQMetaData->q_direction[0].cfg.nbands * hQMetaData->q_direction[0].cfg.nblocks;
     398      284558 :         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       36591 :             bits_sur_coherence = read_surround_coherence( bitstream, index, hQMetaData );
     401             :         }
     402             :         else
     403             :         {
     404      247967 :             bits_sur_coherence = 0;
     405             :             /*Surround coherence*/
     406     1063855 :             for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     407             :             {
     408      815888 :                 if ( hQMetaData->surcoh_band_data != NULL )
     409             :                 {
     410       18393 :                     set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
     411             :                 }
     412             :             }
     413             :         }
     414      284558 :         bits_no_dirs_coh += bits_sur_coherence;
     415             : 
     416      284558 :         total_bits_1dir = hQMetaData->metadata_max_bits - bits_no_dirs_coh;
     417             :     }
     418             : 
     419      337522 :     bits_dir_target = 0;
     420      337522 :     bits_dir_used = 0;
     421             : 
     422      728008 :     for ( d = 0; d < hQMetaData->no_directions; d++ )
     423             :     {
     424      390486 :         q_direction = &hQMetaData->q_direction[d];
     425      390486 :         nbands = q_direction->cfg.nbands;
     426      390486 :         nblocks = q_direction->cfg.nblocks;
     427      390486 :         start_band = q_direction->cfg.start_band;
     428             : 
     429      390486 :         diffuseness_index_max_ec_frame = diffuseness_index_max_ec_frame_pre[0];
     430      390486 :         if ( d == 0 )
     431             :         {
     432      337522 :             bits_diff = bits_diff_sum;
     433             :         }
     434             :         else
     435             :         {
     436       52964 :             bits_diff = 0;
     437             :         }
     438      390486 :         bits_dir_raw = bits_dir_raw_pre[d];
     439             : 
     440             :         /* Read coherence, if any */
     441      390486 :         bits_coherence = 0;
     442             : 
     443      390486 :         if ( all_coherence_zero == 0 )
     444             :         {
     445       76922 :             bits_coherence = read_coherence_data( bitstream, index, hQMetaData, d, 0 );
     446             :         }
     447             :         else
     448             :         {
     449             :             /*Surround coherence*/
     450     1852009 :             for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     451             :             {
     452     1538445 :                 if ( hQMetaData->surcoh_band_data != NULL )
     453             :                 {
     454       17568 :                     set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
     455             :                 }
     456             : 
     457     1538445 :                 if ( hQMetaData->q_direction[d].coherence_band_data != NULL )
     458             :                 {
     459       17568 :                     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      390486 :         q_direction->not_in_2D = bitstream[( *index )--];
     466      390486 :         signal_bits = 1;
     467             : 
     468             :         /* Read EC signaling */
     469      390486 :         ec_flag = 0;
     470      390486 :         if ( total_bits_1dir + bits_sur_coherence <= hQMetaData->qmetadata_max_bit_req )
     471             :         {
     472      244314 :             ec_flag = bitstream[( *index )--];
     473      244314 :             signal_bits++;
     474      244314 :             if ( nblocks > 1 )
     475             :             {
     476      182277 :                 if ( ec_flag )
     477             :                 {
     478       16226 :                     ec_flag += bitstream[( *index )--];
     479       16226 :                     signal_bits++;
     480             :                 }
     481             :             }
     482             :         }
     483             : 
     484             :         /* Decode quantized directions frame-wise */
     485      390486 :         if ( ec_flag == 0 ) /* EC 1*/
     486             :         {
     487      363827 :             bits_dir = 0;
     488      363827 :             raw_flag[0] = bitstream[( *index )--];
     489      363827 :             bits_dir++;
     490             : 
     491      363827 :             if ( raw_flag[0] == 0 )
     492             :             {
     493      212746 :                 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      151081 :                 bits_dir += ivas_qmetadata_raw_decode_dir( q_direction, bitstream, index, nbands, start_band, 0 );
     498             :             }
     499             :         }
     500             :         /* Decode quantized directions band-wise */
     501       26659 :         else if ( ec_flag == 1 && ( nblocks > 1 ) ) /* EC2 */
     502             :         {
     503        2705 :             bits_dir = 0;
     504       14709 :             for ( b = start_band; b < nbands; b++ )
     505             :             {
     506       12004 :                 raw_flag[b] = bitstream[( *index )--];
     507       12004 :                 bits_dir++;
     508             :             }
     509             : 
     510             :             /* Read EC bits*/
     511        2705 :             diff_bits = bits_diff + bits_coherence + signal_bits - total_bits_1dir;
     512             : 
     513       14709 :             for ( b = start_band; b < nbands; b++ )
     514             :             {
     515       12004 :                 if ( raw_flag[b] == 0 )
     516             :                 {
     517        6001 :                     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        6003 :                     diff_bits += q_direction->band_data[b].bits_sph_idx[0] * q_direction->cfg.nblocks;
     522             :                 }
     523             :             }
     524        2705 :             diff_bits += bits_dir;
     525             : 
     526             :             /* Small requantization?*/
     527        2705 :             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       14186 :                 for ( i = q_direction->cfg.start_band; i < q_direction->cfg.nbands; i++ )
     534             :                 {
     535       57825 :                     for ( j = 0; j < q_direction->cfg.nblocks; j++ )
     536             :                     {
     537       46260 :                         bits_temp[i][j] = q_direction->band_data[i].bits_sph_idx[j];
     538             :                     }
     539             :                 }
     540             : 
     541        2621 :                 small_reduction_direction( q_direction, bits_temp, raw_flag, &diff_bits );
     542             : 
     543       14186 :                 for ( i = q_direction->cfg.start_band; i < q_direction->cfg.nbands; i++ )
     544             :                 {
     545       57825 :                     for ( j = 0; j < q_direction->cfg.nblocks; j++ )
     546             :                     {
     547       46260 :                         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       14709 :             for ( b = start_band; b < nbands; b++ )
     557             :             {
     558       12004 :                 if ( raw_flag[b] )
     559             :                 {
     560        6003 :                     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       23954 :             ec_flag = 2;
     569             : 
     570       23954 :             if ( hQMetaData->is_masa_ivas_format == 0 )
     571             :             {
     572        3341 :                 reduce_bits = bits_dir_raw - ( total_bits_1dir - bits_diff - bits_coherence - signal_bits );
     573        3341 :                 ind_order[0] = -1;
     574             :             }
     575             :             else
     576             :             {
     577       20613 :                 ind_order[0] = 0;
     578       20613 :                 reduce_bits = min( nbands * nblocks + MASA_BIT_REDUCT_PARAM, bits_dir_raw - ( total_bits_1dir - bits_diff - bits_coherence - signal_bits ) );
     579       20613 :                 if ( reduce_bits > bits_dir_raw - nbands * nblocks )
     580             :                 {
     581         155 :                     reduce_bits = bits_dir_raw - nbands * nblocks;
     582             :                 }
     583             :             }
     584       23954 :             only_reduce_bits_direction( &dummy, q_direction, reduce_bits, nbands, nblocks, ind_order );
     585             : 
     586             :             /* Read directions */
     587       23954 :             bits_dir = read_directions( q_direction, (uint8_t) nbands, (uint8_t) nblocks, bitstream, index, ind_order );
     588             :         }
     589             : 
     590      390486 :         if ( bits_coherence > 0 )
     591             :         {
     592       76061 :             if ( nblocks > 1 )
     593             :             {
     594       68756 :                 decode_spread_coherence( hQMetaData, d, nblocks, 0 );
     595             :             }
     596             :         }
     597             :         else
     598             :         {
     599     1856884 :             for ( b = start_band; b < nbands; b++ )
     600             :             {
     601     1542459 :                 if ( q_direction->coherence_band_data != NULL )
     602             :                 {
     603       21582 :                     set_c( (int8_t *) q_direction->coherence_band_data[b].spread_coherence, 0, nblocks );
     604             :                 }
     605             :             }
     606             :         }
     607      390486 :         if ( d == 0 )
     608             :         {
     609      337522 :             total_bits_1dir = hQMetaData->metadata_max_bits - ( start_index_0 - *index );
     610             :         }
     611             : 
     612      390486 :         bits_dir_target += bits_dir_raw;
     613      390486 :         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      337522 :     if ( hQMetaData->no_directions == 2 )
     657             :     {
     658       52964 :         d = hQMetaData->q_direction[1].cfg.nbands - 1;
     659       52964 :         nblocks = hQMetaData->q_direction[0].cfg.nblocks;
     660             : 
     661      570153 :         for ( b = hQMetaData->q_direction[0].cfg.nbands - 1; b >= 0; b-- )
     662             :         {
     663      517189 :             if ( hQMetaData->twoDirBands[b] == 1 )
     664             :             {
     665      427193 :                 mvr2r( hQMetaData->q_direction[1].band_data[d].azimuth, hQMetaData->q_direction[1].band_data[b].azimuth, nblocks );
     666      427193 :                 mvr2r( hQMetaData->q_direction[1].band_data[d].elevation, hQMetaData->q_direction[1].band_data[b].elevation, nblocks );
     667      427193 :                 mvr2r( hQMetaData->q_direction[1].band_data[d].energy_ratio, hQMetaData->q_direction[1].band_data[b].energy_ratio, nblocks );
     668             : 
     669      427193 :                 if ( hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0] < 7 )
     670             :                 {
     671     1533639 :                     for ( m = 0; m < nblocks; m++ )
     672             :                     {
     673     1225227 :                         hQMetaData->q_direction[1].band_data[b].azimuth[m] += hQMetaData->q_direction[0].band_data[b].azimuth[m] - 180;
     674     1225227 :                         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     1225227 :                         if ( hQMetaData->q_direction[1].band_data[b].azimuth[m] < -180 )
     679             :                         {
     680      118226 :                             hQMetaData->q_direction[1].band_data[b].azimuth[m] += 360;
     681             :                         }
     682             :                     }
     683             :                 }
     684             : 
     685      427193 :                 if ( hQMetaData->q_direction[1].coherence_band_data != NULL )
     686             :                 {
     687       65502 :                     mvc2c( hQMetaData->q_direction[1].coherence_band_data[d].spread_coherence, hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence, nblocks );
     688             :                 }
     689      427193 :                 d--;
     690             :             }
     691             :             else
     692             :             {
     693       89996 :                 set_f( hQMetaData->q_direction[1].band_data[b].azimuth, 0.0f, nblocks );
     694       89996 :                 set_f( hQMetaData->q_direction[1].band_data[b].elevation, 0.0f, nblocks );
     695       89996 :                 set_f( hQMetaData->q_direction[1].band_data[b].energy_ratio, 0.0f, nblocks );
     696             : 
     697       89996 :                 if ( hQMetaData->q_direction[1].coherence_band_data != NULL )
     698             :                 {
     699       89996 :                     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       52964 :         if ( !hodirac_flag )
     706             :         {
     707      175581 :             for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     708             :             {
     709             :                 float ratioSum;
     710             : 
     711      155498 :                 ratioSum = hQMetaData->q_direction[0].band_data[b].energy_ratio[0] + hQMetaData->q_direction[1].band_data[b].energy_ratio[0];
     712             : 
     713      155498 :                 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      337522 :     hQMetaData->ec_flag = ec_flag;
     724             : 
     725      337522 :     hQMetaData->dir_comp_ratio = (float) bits_dir_used / (float) bits_dir_target;
     726             : 
     727      337522 :     if ( hQMetaData->dir_comp_ratio > 1.0f )
     728             :     {
     729      117057 :         hQMetaData->dir_comp_ratio = 1.0f;
     730             :     }
     731             : 
     732      337522 :     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        2289 : 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        2289 :     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        2289 :     start_index_0 = *index;
     790             :     /* read number of higher inactive/not encoded  bands */
     791        2289 :     if ( bitstream[( *index )--] )
     792             :     {
     793         285 :         codedBands = MASA_MAXIMUM_CODING_SUBBANDS - ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 1 ) - 1;
     794             :     }
     795             :     else
     796             :     {
     797        2004 :         codedBands = MASA_MAXIMUM_CODING_SUBBANDS;
     798             :     }
     799        2574 :     for ( b = codedBands; b < ncoding_bands_config; b++ )
     800             :     {
     801        1425 :         for ( m = 0; m < MAX_PARAM_SPATIAL_SUBFRAMES; m++ )
     802             :         {
     803        1140 :             hQMetaData->q_direction[0].band_data[b].azimuth[m] = 0.0f;
     804        1140 :             hQMetaData->q_direction[0].band_data[b].elevation[m] = 0.0f;
     805        1140 :             hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = 0.0f;
     806             : 
     807        1140 :             if ( hQMetaData->coherence_flag && hQMetaData->q_direction[0].coherence_band_data != NULL )
     808             :             {
     809        1140 :                 hQMetaData->q_direction[0].coherence_band_data[b].spread_coherence[m] = 0u;
     810             :             }
     811             : 
     812        1140 :             if ( hQMetaData->no_directions == 2 )
     813             :             {
     814        1140 :                 hQMetaData->q_direction[1].band_data[b].azimuth[m] = 0.0f;
     815        1140 :                 hQMetaData->q_direction[1].band_data[b].elevation[m] = 0.0f;
     816        1140 :                 hQMetaData->q_direction[1].band_data[b].energy_ratio[m] = 0.0f;
     817             : 
     818        1140 :                 if ( hQMetaData->coherence_flag && hQMetaData->q_direction[1].coherence_band_data != NULL )
     819             :                 {
     820        1140 :                     hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence[m] = 0u;
     821             :                 }
     822             :             }
     823             : 
     824        1140 :             if ( hQMetaData->coherence_flag && hQMetaData->surcoh_band_data != NULL )
     825             :             {
     826        1140 :                 hQMetaData->surcoh_band_data[b].surround_coherence[m] = 0u;
     827             :             }
     828             :         }
     829             : 
     830         285 :         if ( hQMetaData->no_directions == 2 )
     831             :         {
     832         285 :             hQMetaData->twoDirBands[b] = 0;
     833             :         }
     834             :     }
     835        2289 :     sf_nbands0 = hQMetaData->q_direction[0].cfg.nbands;
     836             : 
     837        2289 :     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        2289 :     all_coherence_zero = 1;
     844        2289 :     if ( hQMetaData->coherence_flag )
     845             :     {
     846             :         /* read if coherence is zero */
     847        2289 :         all_coherence_zero = bitstream[( *index )--];
     848             : #ifdef DEBUG_MODE_QMETADATA
     849             :         bits_no_dirs_coh += 1;
     850             : #endif
     851             :     }
     852             : 
     853        2289 :     hQMetaData->all_coherence_zero = (uint8_t) all_coherence_zero;
     854             : 
     855        2289 :     if ( hQMetaData->no_directions == 2 )
     856             :     {
     857        1289 :         set_c( (int8_t *) hQMetaData->twoDirBands, 1, hQMetaData->q_direction[0].cfg.nbands );
     858             :     }
     859             : 
     860        2289 :     if ( bits_sph_idx == 11 && hQMetaData->no_directions == 2 )
     861             :     {
     862             :         /* Read which bands have 2 directions */
     863         889 :         hQMetaData->q_direction[1].cfg.nbands = hQMetaData->numTwoDirBands;
     864         889 :         sf_nbands1 = hQMetaData->q_direction[1].cfg.nbands;
     865         889 :         if ( hQMetaData->q_direction[1].cfg.nbands > codedBands )
     866             :         {
     867           0 :             hQMetaData->q_direction[1].cfg.nbands = codedBands;
     868             :         }
     869         889 :         set_c( (int8_t *) hQMetaData->twoDirBands, 0, hQMetaData->q_direction[0].cfg.nbands );
     870         889 :         d = *index;
     871         889 :         dif_p[0] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 );
     872         889 :         p[0] = dif_p[0];
     873         889 :         hQMetaData->twoDirBands[p[0]] = 1;
     874        8424 :         for ( b = 1; b < hQMetaData->q_direction[1].cfg.nbands; b++ )
     875             :         {
     876        7535 :             dif_p[b] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 );
     877        7535 :             p[b] = p[b - 1] + dif_p[b] + 1;
     878        7535 :             hQMetaData->twoDirBands[p[b]] = 1;
     879             :         }
     880             : #ifdef DEBUG_MODE_QMETADATA
     881             :         bits_no_dirs_coh += ( d - *index );
     882             : #endif
     883             :     }
     884             : 
     885        2289 :     if ( bits_sph_idx == 16 && hQMetaData->no_directions == 2 )
     886             :     {
     887         400 :         sf_nbands1 = hQMetaData->q_direction[1].cfg.nbands;
     888         400 :         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        2289 :         ivas_qmetadata_entropy_decode_diffuseness_hr_512( bitstream, index, &( hQMetaData->q_direction[0] ) );
     897             : 
     898        2289 :     if ( hQMetaData->no_directions == 2 )
     899             :     {
     900             : #ifdef DEBUG_MODE_QMETADATA
     901             :         bits_diff_sum +=
     902             : #endif
     903        1289 :             ivas_qmetadata_entropy_decode_diffuseness_hr_512( bitstream, index, &( hQMetaData->q_direction[1] ) );
     904             :     }
     905             : 
     906             : 
     907       56940 :     for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     908             :     {
     909      263526 :         for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     910             :         {
     911      208875 :             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        2289 :     if ( hQMetaData->no_directions == 2 )
     915             :     {
     916             :         float ratioSum;
     917        1289 :         if ( bits_sph_idx == 16 )
     918             :         {
     919       10000 :             for ( b = hQMetaData->q_direction[1].cfg.start_band; b < hQMetaData->q_direction[1].cfg.nbands; b++ )
     920             :             {
     921       48000 :                 for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
     922             :                 {
     923       38400 :                     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       38400 :                     ratioSum = hQMetaData->q_direction[0].band_data[b].energy_ratio[m] + hQMetaData->q_direction[1].band_data[b].energy_ratio[m];
     927             : 
     928       38400 :                     if ( ratioSum > 1.0f )
     929             :                     {
     930        1369 :                         hQMetaData->q_direction[0].band_data[b].energy_ratio[m] /= ratioSum;
     931        1369 :                         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         889 :             d = 0;
     940       21940 :             for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     941             :             {
     942       21051 :                 if ( hQMetaData->twoDirBands[b] == 1 )
     943             :                 {
     944        8424 :                     pos_2dir_band[d] = b;
     945        8424 :                     d++;
     946             :                 }
     947             :                 else
     948             :                 {
     949       12627 :                     pos_2dir_band[d] = 0;
     950             :                 }
     951             :             }
     952        9313 :             for ( b = hQMetaData->q_direction[1].cfg.start_band; b < hQMetaData->q_direction[1].cfg.nbands; b++ )
     953             :             {
     954       37044 :                 for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
     955             :                 {
     956       28620 :                     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       28620 :                     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       28620 :                     if ( ratioSum > 1.0f )
     961             :                     {
     962         343 :                         hQMetaData->q_direction[0].band_data[pos_2dir_band[b]].energy_ratio[m] /= ratioSum;
     963         343 :                         hQMetaData->q_direction[1].band_data[b].energy_ratio[m] /= ratioSum;
     964             :                     }
     965             :                 }
     966             :             }
     967             :         }
     968             :     }
     969             : 
     970        2289 :     if ( hQMetaData->no_directions == 2 )
     971             :     {
     972       19313 :         for ( b = hQMetaData->q_direction[1].cfg.start_band; b < hQMetaData->q_direction[1].cfg.nbands; b++ )
     973             :         {
     974       85044 :             for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
     975             :             {
     976       67020 :                 hQMetaData->q_direction[1].band_data[b].energy_ratio_index_mod[m] = hQMetaData->q_direction[1].band_data[b].energy_ratio_index[m];
     977       67020 :                 hQMetaData->q_direction[1].band_data[b].bits_sph_idx[m] = bits_sph_idx;
     978             :             }
     979             :         }
     980             :     }
     981             : 
     982       56940 :     for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     983             :     {
     984      263526 :         for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     985             :         {
     986      208875 :             hQMetaData->q_direction[0].band_data[b].energy_ratio_index_mod[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
     987      208875 :             hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m] = bits_sph_idx;
     988             :         }
     989             :     }
     990             : 
     991        2289 :     if ( all_coherence_zero == 0 )
     992             :     {
     993             : #ifdef DEBUG_MODE_QMETADATA
     994             :         bits_sur_coherence =
     995             : #endif
     996        2289 :             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           0 :         for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
    1005             :         {
    1006           0 :             if ( hQMetaData->surcoh_band_data != NULL )
    1007             :             {
    1008           0 :                 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        5867 :     for ( d = 0; d < hQMetaData->no_directions; d++ )
    1017             :     {
    1018        3578 :         q_direction = &hQMetaData->q_direction[d];
    1019        3578 :         nbands = q_direction->cfg.nbands;
    1020        3578 :         start_band = q_direction->cfg.start_band;
    1021             : 
    1022             :         /* Read coherence, if any */
    1023        3578 :         if ( all_coherence_zero == 0 )
    1024             :         {
    1025        3578 :             read_coherence_data_hr_512( bitstream, index, hQMetaData, d, bits_sp_coh );
    1026             :         }
    1027             :         else
    1028             :         {
    1029             :             /*Surround coherence*/
    1030           0 :             for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
    1031             :             {
    1032           0 :                 if ( hQMetaData->surcoh_band_data != NULL )
    1033             :                 {
    1034           0 :                     set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
    1035             :                 }
    1036             : 
    1037           0 :                 if ( hQMetaData->q_direction[d].coherence_band_data != NULL )
    1038             :                 {
    1039           0 :                     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        3578 :         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        2289 :     if ( hQMetaData->no_directions == 2 )
    1077             :     {
    1078             :         /* move 2 dir data to its correct subband */
    1079        1289 :         if ( bits_sph_idx == 11 )
    1080             :         {
    1081             :             int16_t nblocks;
    1082         889 :             d = hQMetaData->q_direction[1].cfg.nbands - 1;
    1083         889 :             nblocks = hQMetaData->q_direction[0].cfg.nblocks;
    1084             : 
    1085       21940 :             for ( b = hQMetaData->q_direction[0].cfg.nbands - 1; b >= 0; b-- )
    1086             :             {
    1087       21051 :                 if ( hQMetaData->twoDirBands[b] == 1 )
    1088             :                 {
    1089        8424 :                     mvr2r( hQMetaData->q_direction[1].band_data[d].azimuth, hQMetaData->q_direction[1].band_data[b].azimuth, nblocks );
    1090        8424 :                     mvr2r( hQMetaData->q_direction[1].band_data[d].elevation, hQMetaData->q_direction[1].band_data[b].elevation, nblocks );
    1091        8424 :                     mvr2r( hQMetaData->q_direction[1].band_data[d].energy_ratio, hQMetaData->q_direction[1].band_data[b].energy_ratio, nblocks );
    1092             : 
    1093             : 
    1094        8424 :                     if ( hQMetaData->q_direction[1].coherence_band_data != NULL )
    1095             :                     {
    1096        8424 :                         mvc2c( hQMetaData->q_direction[1].coherence_band_data[d].spread_coherence, hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence, nblocks );
    1097             :                     }
    1098        8424 :                     d--;
    1099             :                 }
    1100             :                 else
    1101             :                 {
    1102       12627 :                     set_f( hQMetaData->q_direction[1].band_data[b].azimuth, 0.0f, nblocks );
    1103       12627 :                     set_f( hQMetaData->q_direction[1].band_data[b].elevation, 0.0f, nblocks );
    1104       12627 :                     set_f( hQMetaData->q_direction[1].band_data[b].energy_ratio, 0.0f, nblocks );
    1105             : 
    1106       12627 :                     if ( hQMetaData->q_direction[1].coherence_band_data != NULL )
    1107             :                     {
    1108       12627 :                         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       31940 :         for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
    1116             :         {
    1117      143526 :             for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
    1118             :             {
    1119             :                 float ratioSum;
    1120             : 
    1121      112875 :                 ratioSum = hQMetaData->q_direction[0].band_data[b].energy_ratio[m] + hQMetaData->q_direction[1].band_data[b].energy_ratio[m];
    1122             : 
    1123      112875 :                 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        2289 :     hQMetaData->ec_flag = 0;
    1168             : 
    1169        2289 :     hQMetaData->dir_comp_ratio = 1.0f;
    1170             : 
    1171        2289 :     if ( hQMetaData->dir_comp_ratio > 1.0f )
    1172             :     {
    1173           0 :         hQMetaData->dir_comp_ratio = 1.0f;
    1174             :     }
    1175             : 
    1176        2289 :     hQMetaData->q_direction[0].cfg.nbands = sf_nbands0;
    1177        2289 :     if ( hQMetaData->no_directions == 2 )
    1178             :     {
    1179        1289 :         hQMetaData->q_direction[1].cfg.nbands = sf_nbands1;
    1180             :     }
    1181             : 
    1182        2289 :     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         769 : 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         769 :     if ( ivas_format == SBA_FORMAT )
    1231             :     {
    1232         564 :         sba_spar_bitlen = ivas_sba_spar_sid_bitlen( nchan_transport );
    1233         564 :         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         205 :         metadata_sid_bits = ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - SID_FORMAT_NBITS;
    1238             :     }
    1239             : 
    1240         769 :     start_index = *index;
    1241             : 
    1242             :     /* read MASA SID descriptor */
    1243         769 :     if ( ivas_format == MASA_FORMAT && nchan_transport == 2 )
    1244             :     {
    1245          74 :         b = bitstream[( *index )--];
    1246          74 :         if ( b )
    1247             :         {
    1248           0 :             *element_mode = IVAS_CPE_MDCT;
    1249             :         }
    1250             :         else
    1251             :         {
    1252          74 :             *element_mode = IVAS_CPE_DFT;
    1253             :         }
    1254             :     }
    1255             : 
    1256             :     /* Fix configuration for SID */
    1257         769 :     q_direction = &hQMetaData->q_direction[0]; /* only 1 direction */
    1258         769 :     if ( ivas_format == SBA_FORMAT )
    1259             :     {
    1260         564 :         nbands = DIRAC_DTX_BANDS; /* only 2 bands transmitted */
    1261             :     }
    1262             :     else
    1263             :     {
    1264         205 :         nbands = 5; /* only 5 bands transmitted */
    1265             :     }
    1266             : 
    1267         769 :     nblocks = q_direction->cfg.nblocks; /* only 1 block transmitted but up to 4 blocks re-generated */
    1268         769 :     start_band = 0;                     /* start from band 0 */
    1269             : 
    1270             :     /* Read 2D signaling*/
    1271         769 :     if ( ivas_format != SBA_FORMAT )
    1272             :     {
    1273         205 :         q_direction->not_in_2D = bitstream[( *index )--];
    1274             :     }
    1275             :     else
    1276             :     {
    1277         564 :         q_direction->not_in_2D = 1;
    1278             :     }
    1279             : 
    1280         769 :     bits_dir = 0;
    1281         769 :     if ( ivas_format != SBA_FORMAT )
    1282             :     {
    1283             :         /* Decode diffuseness*/
    1284        1230 :         for ( b = start_band; b < nbands; b++ )
    1285             :         {
    1286        1025 :             diffuseness_index[b] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS - 4 ) + 4;
    1287        1025 :             q_direction->band_data[b].energy_ratio_index[0] = diffuseness_index[b];
    1288        1025 :             q_direction->band_data[b].bits_sph_idx[0] = bits_direction_masa[diffuseness_index[b]];
    1289        1025 :             bits_dir += q_direction->band_data[b].bits_sph_idx[0];
    1290        1025 :             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         205 :         bits_delta = metadata_sid_bits - ( start_index - *index ) - bits_dir; /* bit_diff is already read */
    1294             : 
    1295         205 :         if ( bits_delta > 0 )
    1296             :         {
    1297         978 :             while ( bits_delta > 0 )
    1298             :             {
    1299        4206 :                 for ( b = start_band; b < nbands && ( bits_delta > 0 ); b++ )
    1300             :                 {
    1301        3433 :                     if ( q_direction->band_data[b].bits_sph_idx[0] < 11 )
    1302             :                     {
    1303        3433 :                         bits_delta -= 1;
    1304        3433 :                         q_direction->band_data[b].bits_sph_idx[0]++;
    1305             :                     }
    1306             :                 }
    1307             :             }
    1308             : 
    1309         205 :             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        1692 :         for ( b = start_band; b < nbands; b++ )
    1344             :         {
    1345        1128 :             diffuseness_index[b] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS - 4 ) + 4;
    1346        1128 :             q_direction->band_data[b].energy_ratio_index[0] = diffuseness_index[b];
    1347        1128 :             q_direction->band_data[b].bits_sph_idx[0] = bits_direction_masa[diffuseness_index[b]];
    1348        1128 :             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        2922 :     for ( b = start_band; b < nbands; b++ )
    1353             :     {
    1354        2153 :         q_direction->band_data[b].energy_ratio[0] = 1.0f - diffuseness_reconstructions[diffuseness_index[b]];
    1355       10597 :         for ( i = 0; i < nblocks; i++ )
    1356             :         {
    1357        8444 :             q_direction->band_data[b].energy_ratio[i] = q_direction->band_data[b].energy_ratio[0];
    1358             :         }
    1359             :     }
    1360             : 
    1361             :     /* Decoder DOAs*/
    1362         769 :     if ( q_direction->not_in_2D > 0 )
    1363             :     {
    1364        2922 :         for ( b = start_band; b < nbands; b++ )
    1365             :         {
    1366        2153 :             value = 0;
    1367       14222 :             for ( i = 0; i < q_direction->band_data[b].bits_sph_idx[0]; i++ )
    1368             :             {
    1369       12069 :                 value = ( value << 1 ) + bitstream[( *index )--];
    1370             :             }
    1371       10597 :             for ( i = 0; i < nblocks; i++ )
    1372             :             {
    1373        8444 :                 q_direction->band_data[b].spherical_index[i] = value;
    1374             :             }
    1375             : 
    1376        2153 :             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        2153 :             ivas_qmetadata_azimuth_elevation_to_direction_vector( avg_azimuth, avg_elevation, avg_direction_vector );
    1379        2153 :             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        8444 :             for ( m = 0; m < nblocks - 1; m++ )
    1382             :             {
    1383        6291 :                 v_add( direction_vector, avg_direction_vector, direction_vector, 3 );
    1384        6291 :                 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        2153 :             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        2950 :     while ( start_index - *index < metadata_sid_bits )
    1426             :     {
    1427        2181 :         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         769 :     return ( start_index - *index );
    1452             : }
    1453             : 
    1454             : 
    1455             : /*-----------------------------------------------------------------------*
    1456             :  * Local function definitions for diffuseness/energy ratios
    1457             :  *-----------------------------------------------------------------------*/
    1458             : 
    1459      458234 : 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      458234 :     val = 0;
    1467      929106 :     while ( val <= DIFF_EC_HUFF_GR0_LIMIT )
    1468             :     {
    1469      928773 :         if ( bitstream[( *index )--] == 1 )
    1470             :         {
    1471      470872 :             val++;
    1472             :         }
    1473             :         else
    1474             :         {
    1475      457901 :             break;
    1476             :         }
    1477             :     }
    1478             : 
    1479      458234 :     if ( val == DIFF_EC_HUFF_GR0_LIMIT + 1 )
    1480             :     {
    1481         333 :         val += 2 * bitstream[( *index )];
    1482         333 :         ( *index )--;
    1483         333 :         val += bitstream[( *index )];
    1484         333 :         ( *index )--;
    1485             :     }
    1486             : 
    1487      458234 :     if ( val % 2 == 0 )
    1488             :     {
    1489      299906 :         return -( val / 2 ) + av;
    1490             :     }
    1491             :     else
    1492             :     {
    1493      158328 :         return ( val + 1 ) / 2 + av;
    1494             :     }
    1495             : }
    1496             : 
    1497             : 
    1498             : /*-------------------------------------------------------------------*
    1499             :  * ivas_qmetadata_entropy_decode_diffuseness()
    1500             :  *
    1501             :  *
    1502             :  *-------------------------------------------------------------------*/
    1503             : 
    1504      337522 : 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      337522 :     index_start = *index;
    1517      337522 :     nbands = q_direction->cfg.nbands;
    1518      337522 :     start_band = q_direction->cfg.start_band;
    1519             : 
    1520             :     /* diffuseness decoding */
    1521             :     /* Handle one band as special case*/
    1522      337522 :     if ( nbands == 1 )
    1523             :     {
    1524        9225 :         q_direction->band_data[0].energy_ratio_index[0] = 0;
    1525       36900 :         for ( b = 0; b < MASA_BITS_ER; b++ )
    1526             :         {
    1527       27675 :             q_direction->band_data[0].energy_ratio_index[0] = ( q_direction->band_data[0].energy_ratio_index[0] << 1 ) + bitstream[( *index )--];
    1528             :         }
    1529        9225 :         *diffuseness_index_max_ec_frame = 5;
    1530             : 
    1531        9225 :         return MASA_BITS_ER;
    1532             :     }
    1533             : 
    1534      328297 :     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      160916 :         if ( nbands < DIFF_EC_HUFF_BAND_LIMIT )
    1538             :         {
    1539      123361 :             if ( bitstream[( *index )--] != 0 ) /* dif_have_unique_value */
    1540             :             {
    1541       70430 :                 dif_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS ); /* dif_unique_value */
    1542             : 
    1543      280390 :                 for ( b = start_band; b < nbands; b++ )
    1544             :                 {
    1545      209960 :                     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       52931 :                 dif_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS - 1 ); /* dif_min_value */
    1551             : 
    1552      237940 :                 for ( b = start_band; b < nbands; b++ )
    1553             :                 {
    1554      185009 :                     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       37555 :             av = 0;
    1564      150220 :             for ( b = 0; b < MASA_BITS_ER; b++ )
    1565             :             {
    1566      112665 :                 av += bitstream[( *index )--] * ( 1 << ( MASA_BITS_ER - 1 - b ) );
    1567             :             }
    1568             : 
    1569       37555 :             dif_min = DIRAC_DIFFUSE_LEVELS;
    1570             :             /* read average removed data (average is added inside)*/
    1571      495789 :             for ( b = start_band; b < nbands; b++ )
    1572             :             {
    1573      458234 :                 q_direction->band_data[b].energy_ratio_index[0] = ivas_diffuseness_huff_ec_decode( bitstream, index, av );
    1574      458234 :                 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      167381 :         dif_min = DIRAC_DIFFUSE_LEVELS;
    1581             : 
    1582      972834 :         for ( b = start_band; b < nbands; b++ )
    1583             :         {
    1584      805453 :             q_direction->band_data[b].energy_ratio_index[0] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS );
    1585      805453 :             dif_min = min( dif_min, q_direction->band_data[b].energy_ratio_index[0] );
    1586             :         }
    1587             :     }
    1588             : 
    1589      328297 :     *diffuseness_index_max_ec_frame = 5;
    1590             :     /* adaptively select the diffuseness_index_max_ec threshold */
    1591      328297 :     if ( dif_min > 5 )
    1592             :     {
    1593      107474 :         *diffuseness_index_max_ec_frame = DIRAC_DIFFUSE_LEVELS - 1;
    1594             :     }
    1595             : 
    1596      328297 :     return ( index_start - *index );
    1597             : }
    1598             : 
    1599             : 
    1600             : /*-------------------------------------------------------------------*
    1601             :  * ivas_qmetadata_entropy_decode_diffuseness_hr_512()
    1602             :  *
    1603             :  *
    1604             :  *-------------------------------------------------------------------*/
    1605             : 
    1606        3578 : 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        3578 :     index_start = *index;
    1618        3578 :     nbands = q_direction->cfg.nbands;
    1619        3578 :     nblocks = q_direction->cfg.nblocks;
    1620        3578 :     start_band = q_direction->cfg.start_band;
    1621             : 
    1622             :     /* diffuseness decoding */
    1623             :     /* Handle one band as special case*/
    1624        3578 :     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       76253 :     for ( b = start_band; b < nbands; b++ )
    1636             :     {
    1637      348570 :         for ( k = 0; k < nblocks; k++ )
    1638             :         {
    1639      275895 :             q_direction->band_data[b].energy_ratio_index[k] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, HR_MASA_ER_LEVELS );
    1640             :         }
    1641             :     }
    1642             : 
    1643        3578 :     return ( index_start - *index );
    1644             : }
    1645             : 
    1646             : 
    1647             : /*-------------------------------------------------------------------*
    1648             :  * ivas_qmetadata_entropy_decode_df_ratio()
    1649             :  *
    1650             :  *
    1651             :  *-------------------------------------------------------------------*/
    1652             : 
    1653       52964 : 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       52964 :     index_start = *index;
    1671       52964 :     nbands = q_direction->cfg.nbands;
    1672       52964 :     start_band = q_direction->cfg.start_band;
    1673             : 
    1674             :     /* Handle one band as special case*/
    1675       52964 :     if ( nbands == 1 )
    1676             :     {
    1677        6741 :         q_direction->band_data[0].energy_ratio_index[0] = 0;
    1678       20978 :         for ( b = 0; b < dfRatio_bits[0]; b++ )
    1679             :         {
    1680       14237 :             q_direction->band_data[0].energy_ratio_index[0] = ( q_direction->band_data[0].energy_ratio_index[0] << 1 ) + bitstream[( *index )--];
    1681             :         }
    1682        6741 :         return dfRatio_bits[0];
    1683             :     }
    1684             : 
    1685             :     /* Calculate raw coding bits and decide what modes are possible */
    1686       46223 :     bits_raw = 0;
    1687       46223 :     max_dfRatio_bits = 0;
    1688      466675 :     for ( b = start_band; b < nbands; b++ )
    1689             :     {
    1690      420452 :         bits_raw += dfRatio_bits[b];
    1691      420452 :         max_dfRatio_bits = max( max_dfRatio_bits, dfRatio_bits[b] );
    1692             :     }
    1693             : 
    1694             :     /* Decide what modes are possible */
    1695       46223 :     if ( bits_raw >= max_dfRatio_bits + 2 + nbands )
    1696             :     {
    1697       27931 :         ec_mode = 2;
    1698             :     }
    1699       18292 :     else if ( bits_raw >= max_dfRatio_bits + 1 )
    1700             :     {
    1701       18292 :         ec_mode = 1;
    1702             :     }
    1703             :     else
    1704             :     {
    1705           0 :         ec_mode = 0;
    1706             :     }
    1707       46223 :     max_alphabet_size = 1 << max_dfRatio_bits;
    1708             : 
    1709       46223 :     dec_mode = 2; /* Default to raw decoding */
    1710       46223 :     if ( ec_mode == 1 )
    1711             :     {
    1712       18292 :         if ( bitstream[( *index )--] == 0 )
    1713             :         {
    1714         879 :             dec_mode = 1; /* Switch to one value EC coding */
    1715             :         }
    1716             :     }
    1717       27931 :     else if ( ec_mode == 2 )
    1718             :     {
    1719       27931 :         if ( bitstream[( *index )--] == 0 )
    1720             :         {
    1721        2237 :             if ( bitstream[( *index )--] == 0 )
    1722             :             {
    1723         137 :                 dec_mode = 1; /* Switch to one value EC coding */
    1724             :             }
    1725             :             else
    1726             :             {
    1727        2100 :                 dec_mode = 0; /* Use one-bit diff bandwise mode */
    1728             :             }
    1729             :         }
    1730             :     }
    1731             : 
    1732       46223 :     if ( dec_mode == 2 ) /* Raw decoding */
    1733             :     {
    1734      442131 :         for ( b = start_band; b < nbands; b++ )
    1735             :         {
    1736      399024 :             q_direction->band_data[b].energy_ratio_index[0] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, 1 << dfRatio_bits[b] );
    1737             :         }
    1738             :     }
    1739        3116 :     else if ( dec_mode == 1 ) /* One value decoding */
    1740             :     {
    1741        1016 :         ratio_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, max_alphabet_size ); /* dif_unique_value */
    1742             : 
    1743        5405 :         for ( b = start_band; b < nbands; b++ )
    1744             :         {
    1745        4389 :             q_direction->band_data[b].energy_ratio_index[0] = ratio_min;
    1746             :         }
    1747             :     }
    1748             :     else /* Bandwise 1-bit diff decoding */
    1749             :     {
    1750        2100 :         ratio_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, max_alphabet_size - 1 ); /* dif_min_value */
    1751             : 
    1752       19139 :         for ( b = start_band; b < nbands; b++ )
    1753             :         {
    1754       17039 :             q_direction->band_data[b].energy_ratio_index[0] = ratio_min + bitstream[( *index )--]; /* dif_bit_offset_values */
    1755             :         }
    1756             :     }
    1757             : 
    1758       46223 :     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      218747 : 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      218747 :     index_start = *index;
    1800      218747 :     nblocks = q_direction->cfg.nblocks;
    1801             : 
    1802      218747 :     diff_idx_min = DIRAC_DIFFUSE_LEVELS;
    1803             : 
    1804             :     /*Raw coding for high diffuseness*/
    1805     1559039 :     for ( b = start_band; b < nbands; b++ )
    1806             :     {
    1807     1340292 :         if ( hrmasa_flag )
    1808             :         {
    1809           0 :             diff_idx = 0;
    1810             :         }
    1811             :         else
    1812             :         {
    1813     1340292 :             diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
    1814             :         }
    1815             : 
    1816     1340292 :         diff_idx_min = min( diff_idx_min, diff_idx );
    1817     1340292 :         if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    1818             :         {
    1819       31971 :             elev_alph[b] = no_theta_masa[bits_direction_masa[diff_idx] - 3];
    1820             :         }
    1821             :         else
    1822             :         {
    1823     1308321 :             elev_alph[b] = no_theta_masa[bits_direction_masa[diff_idx] - 3] * 2 - 1;
    1824             :         }
    1825             : 
    1826     1340292 :         if ( q_direction->band_data[b].energy_ratio_index_mod[0] > diffuseness_index_max_ec_frame )
    1827             :         {
    1828      339200 :             bands_entropic[b] = 0;
    1829             : 
    1830      339200 :             if ( q_direction->not_in_2D > 0 )
    1831             :             {
    1832      313528 :                 decode_fixed_rate( q_direction, bitstream, index, b, nblocks );
    1833             :             }
    1834             :             else
    1835             :             {
    1836             :                 /* in 2D */
    1837      114323 :                 for ( m = 0; m < nblocks; m++ )
    1838             :                 {
    1839       88651 :                     q_direction->band_data[b].elevation[m] = 0.f;
    1840       88651 :                     q_direction->band_data[b].elevation_index[m] = 0;
    1841             : 
    1842       88651 :                     azith_alph[b][m] = no_phi_masa[bits_direction_masa[diff_idx] - 1][0];
    1843       88651 :                     q_direction->band_data[b].azimuth_index[m] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, azith_alph[b][m] );
    1844       88651 :                     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     1001092 :             bands_entropic[b] = 1;
    1851             :         }
    1852             :     }
    1853             : 
    1854             :     /*EC for the low diffuseness*/
    1855             : 
    1856             :     /*Elevation only if not 2D  */
    1857      218747 :     if ( q_direction->not_in_2D > 0 )
    1858             :     {
    1859      189564 :         if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    1860             :         {
    1861        1063 :             avg_elevation_alphabet = no_theta_masa[bits_direction_masa[diff_idx_min] - 3];
    1862        1063 :             avg_elevation_idx = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, avg_elevation_alphabet );
    1863             :         }
    1864             :         else
    1865             :         {
    1866      188501 :             avg_elevation_alphabet = no_theta_masa[bits_direction_masa[diff_idx_min] - 3] * 2 - 1;
    1867      188501 :             avg_elevation_idx = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, avg_elevation_alphabet );
    1868      188501 :             avg_elevation_idx = ivas_qmetadata_dereorder_generic( avg_elevation_idx ) + ( avg_elevation_alphabet >> 1 );
    1869             :         }
    1870             : 
    1871      189564 :         gr_param_elev = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, 4 + 1 );
    1872      189564 :         if ( gr_param_elev == 4 ) /* all the elevation distances are zero */
    1873             :         {
    1874       91404 :             for ( b = start_band; b < nbands; b++ )
    1875             :             {
    1876       69903 :                 if ( bands_entropic[b] )
    1877             :                 {
    1878             :                     int16_t tmp_index;
    1879       34028 :                     if ( hrmasa_flag )
    1880             :                     {
    1881           0 :                         diff_idx = 0;
    1882             :                     }
    1883             :                     else
    1884             :                     {
    1885       34028 :                         diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
    1886             :                     }
    1887             : 
    1888       34028 :                     if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    1889             :                     {
    1890         696 :                         avg_elevation_index_projected = ivas_chan_project_elevation_index( avg_elevation_idx, avg_elevation_alphabet, elev_alph[b] );
    1891             :                     }
    1892             :                     else
    1893             :                     {
    1894       33332 :                         avg_elevation_index_projected = ivas_dirac_project_elevation_index( avg_elevation_idx, avg_elevation_alphabet, elev_alph[b] );
    1895             : 
    1896             :                         /*reorder elevation indexing*/
    1897       33332 :                         tmp_index = avg_elevation_index_projected - ( elev_alph[b] >> 1 );
    1898       33332 :                         if ( tmp_index < 0 )
    1899             :                         {
    1900       14316 :                             tmp_index = tmp_index * -2;
    1901             :                         }
    1902       19016 :                         else if ( tmp_index > 0 )
    1903             :                         {
    1904        3610 :                             tmp_index = tmp_index * 2 - 1;
    1905             :                         }
    1906       33332 :                         avg_elevation_index_projected = tmp_index;
    1907             :                     }
    1908             : 
    1909      135904 :                     for ( m = 0; m < nblocks; m++ )
    1910             :                     {
    1911      101876 :                         q_direction->band_data[b].elevation_index[m] = avg_elevation_index_projected;
    1912             : 
    1913             :                         /*deduce aplhabet for azimuth*/
    1914      101876 :                         if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    1915             :                         {
    1916        2403 :                             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       99473 :                             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      101876 :                         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     1299079 :             for ( b = start_band; b < nbands; b++ )
    1932             :             {
    1933     1131016 :                 if ( bands_entropic[b] )
    1934             :                 {
    1935      853363 :                     if ( hrmasa_flag )
    1936             :                     {
    1937           0 :                         diff_idx = 0;
    1938             :                     }
    1939             :                     else
    1940             :                     {
    1941      853363 :                         diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
    1942             :                     }
    1943             : 
    1944     4144112 :                     for ( m = 0; m < nblocks; m++ )
    1945             :                     {
    1946             :                         int16_t tmp_index;
    1947     3290749 :                         if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    1948             :                         {
    1949        8240 :                             avg_elevation_index_projected = ivas_chan_project_elevation_index( avg_elevation_idx, avg_elevation_alphabet, elev_alph[b] );
    1950        8240 :                             tmp_index = ivas_qmetadata_DecodeExtendedGR( bitstream, index, 2 * elev_alph[b] - 1, gr_param_elev );
    1951        8240 :                             if ( tmp_index % 2 )
    1952             :                             {
    1953         847 :                                 tmp_index = avg_elevation_index_projected + ( ( tmp_index + 1 ) >> 1 );
    1954             :                             }
    1955             :                             else
    1956             :                             {
    1957        7393 :                                 tmp_index = avg_elevation_index_projected - ( tmp_index >> 1 );
    1958             :                             }
    1959        8240 :                             q_direction->band_data[b].elevation_index[m] = tmp_index;
    1960             : 
    1961             :                             /*deduce aplhabet for azimuth*/
    1962        8240 :                             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     3282509 :                             avg_elevation_index_projected = ivas_dirac_project_elevation_index( avg_elevation_idx, avg_elevation_alphabet, elev_alph[b] );
    1967             : 
    1968     3282509 :                             tmp_index = ivas_qmetadata_DecodeExtendedGR( bitstream, index, elev_alph[b], gr_param_elev );
    1969     3282509 :                             tmp_index = ivas_qmetadata_ReorderElevationDecoded( tmp_index, avg_elevation_index_projected, elev_alph[b] );
    1970             : 
    1971             :                             /*reorder elevation indexing*/
    1972     3282509 :                             tmp_index = tmp_index - ( elev_alph[b] >> 1 );
    1973     3282509 :                             if ( tmp_index < 0 )
    1974             :                             {
    1975     1486123 :                                 tmp_index = tmp_index * -2;
    1976             :                             }
    1977     1796386 :                             else if ( tmp_index > 0 )
    1978             :                             {
    1979      503404 :                                 tmp_index = tmp_index * 2 - 1;
    1980             :                             }
    1981     3282509 :                             q_direction->band_data[b].elevation_index[m] = tmp_index;
    1982             : 
    1983             :                             /*deduce aplhabet for azimuth*/
    1984     3282509 :                             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     3290749 :                         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      168556 :         for ( b = start_band; b < nbands; b++ )
    1997             :         {
    1998      139373 :             if ( bands_entropic[b] )
    1999             :             {
    2000      113701 :                 if ( hrmasa_flag )
    2001             :                 {
    2002           0 :                     diff_idx = 0;
    2003             :                 }
    2004             :                 else
    2005             :                 {
    2006      113701 :                     diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
    2007             :                 }
    2008             : 
    2009      459047 :                 for ( m = 0; m < nblocks; m++ )
    2010             :                 {
    2011      345346 :                     q_direction->band_data[b].elevation_index[m] = 0;
    2012             : 
    2013             :                     /*deduce alphabet for azimuth*/
    2014      345346 :                     if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    2015             :                     {
    2016       49300 :                         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      296046 :                         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      345346 :                     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      218747 :     avg_azimuth_alphabet = no_phi_masa[bits_direction_masa[diff_idx_min] - 1][0]; /* average azimuth is quantized on the equatorial plane */
    2032      218747 :     avg_azimuth_index = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, avg_azimuth_alphabet );
    2033      218747 :     avg_azimuth_index = ivas_qmetadata_dereorder_generic( avg_azimuth_index );
    2034      218747 :     avg_azimuth_index = avg_azimuth_index + ( avg_azimuth_alphabet >> 1 );
    2035             : 
    2036      218747 :     gr_param_azith = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, 5 + 1 );
    2037      218747 :     if ( gr_param_azith == 5 ) /* all the azimuth distances are zero */
    2038             :     {
    2039       95707 :         for ( b = start_band; b < nbands; b++ )
    2040             :         {
    2041       73988 :             if ( bands_entropic[b] )
    2042             :             {
    2043      170253 :                 for ( m = 0; m < nblocks; m++ )
    2044             :                 {
    2045      123096 :                     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      123096 :                     if ( azith_alph[b][m] == 1 )
    2048             :                     {
    2049          11 :                         q_direction->band_data[b].azimuth[m] = 0.f;
    2050             :                     }
    2051             :                     else
    2052             :                     {
    2053      123085 :                         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      197028 :         set_zero( avg_direction_vector, 3 );
    2062      197028 :         use_adapt_avg = 0;
    2063      197028 :         idx = 0;
    2064             : 
    2065     1463332 :         for ( b = start_band; b < nbands; b++ )
    2066             :         {
    2067     1266304 :             if ( bands_entropic[b] )
    2068             :             {
    2069     4568810 :                 for ( m = 0; m < nblocks; m++ )
    2070             :                 {
    2071     3614875 :                     if ( ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( idx == MASA_LIMIT_IDX_AVG_AZI ) && ( nblocks > 1 ) )
    2072             :                     {
    2073        2072 :                         use_adapt_avg = bitstream[*index];
    2074        2072 :                         ( *index )--;
    2075             :                     }
    2076     3614875 :                     avg_azimuth_index_projected = ivas_dirac_project_azimuth_index( avg_azimuth_index, avg_azimuth_alphabet, azith_alph[b][m] );
    2077     3614875 :                     q_direction->band_data[b].azimuth_index[m] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, azith_alph[b][m], gr_param_azith );
    2078     3614875 :                     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     3614875 :                     if ( azith_alph[b][m] == 1 )
    2081             :                     {
    2082         599 :                         q_direction->band_data[b].azimuth[m] = 0.f;
    2083             :                     }
    2084             :                     else
    2085             :                     {
    2086     3614276 :                         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     3614875 :                     if ( ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( nblocks > 1 ) )
    2089             :                     {
    2090       37832 :                         if ( idx < MASA_LIMIT_IDX_AVG_AZI )
    2091             :                         {
    2092       11828 :                             ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[b].azimuth[m], q_direction->band_data[b].elevation[m], direction_vector );
    2093       11828 :                             v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
    2094             :                         }
    2095             :                         else
    2096             :                         {
    2097       26004 :                             if ( use_adapt_avg == 1 )
    2098             :                             {
    2099       12416 :                                 if ( m == 0 )
    2100             :                                 {
    2101        3104 :                                     v_multc( avg_direction_vector, 0.5f, avg_direction_vector, 3 );
    2102             :                                 }
    2103             :                                 /*compute the average direction per already coded subband */
    2104       12416 :                                 ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[b].azimuth[m], q_direction->band_data[b].elevation[m], direction_vector );
    2105       12416 :                                 v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
    2106       12416 :                                 ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector, &avg_azimuth, &avg_elevation );
    2107       12416 :                                 avg_azimuth_index = quantize_phi( avg_azimuth + 180, 0, &avg_azimuth, avg_azimuth_alphabet );
    2108             :                             }
    2109             :                         }
    2110       37832 :                         idx++;
    2111             :                     }
    2112             :                 }
    2113             :             }
    2114             :         }
    2115             :     }
    2116             : 
    2117      218747 :     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        3578 : 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        3578 :     index_start = *index;
    2142        3578 :     nblocks = q_direction->cfg.nblocks;
    2143             : 
    2144       76253 :     for ( b = start_band; b < nbands; b++ )
    2145             :     {
    2146      348570 :         for ( m = 0; m < nblocks; m++ )
    2147             :         {
    2148      275895 :             value = 0;
    2149     3891540 :             for ( i = 0; i < q_direction->band_data[b].bits_sph_idx[m]; i++ )
    2150             :             {
    2151     3615645 :                 value = ( value << 1 ) + bitstream[( *index )--];
    2152             :             }
    2153      275895 :             q_direction->band_data[b].spherical_index[m] = value;
    2154             : 
    2155      275895 :             if ( q_direction->band_data[b].bits_sph_idx[m] == 16 )
    2156             :             {
    2157      116160 :                 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      159735 :                 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        3578 :     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      157084 : 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      157084 :     index_start = *index;
    2191      157084 :     nblocks = q_direction->cfg.nblocks;
    2192             : 
    2193      761179 :     for ( b = start_band; b < nbands; b++ )
    2194             :     {
    2195      604095 :         if ( q_direction->not_in_2D > 0 )
    2196             :         {
    2197      522927 :             decode_fixed_rate( q_direction, bitstream, index, b, nblocks );
    2198             :         }
    2199             :         else
    2200             :         {
    2201       81168 :             if ( hrmasa_flag )
    2202             :             {
    2203           0 :                 diff_idx = 0;
    2204             :             }
    2205             :             else
    2206             :             {
    2207       81168 :                 diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
    2208             :             }
    2209             : 
    2210      271914 :             for ( m = 0; m < nblocks; m++ )
    2211             :             {
    2212      190746 :                 q_direction->band_data[b].elevation[m] = 0.f;
    2213      190746 :                 q_direction->band_data[b].elevation_index[m] = 0;
    2214      190746 :                 azith_alph = no_phi_masa[bits_direction_masa[diff_idx] - 1][0];
    2215      190746 :                 q_direction->band_data[b].azimuth_index[m] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, azith_alph );
    2216      190746 :                 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      157084 :     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     3443113 : 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     3443113 :     bits = 30 - norm_l( alphabet_size ); /* bits = floor(log2(alphabet_size)) */
    2246     3443113 :     tresh = ( 1U << ( bits + 1 ) ) - alphabet_size;
    2247             : 
    2248     3443113 :     value = 0;
    2249    12760487 :     for ( i = 0; i < bits; i++ )
    2250             :     {
    2251     9317374 :         value = ( value << 1 ) + bitstream[( *index )--];
    2252             :     }
    2253             : 
    2254     3443113 :     if ( value >= tresh )
    2255             :     {
    2256      421703 :         value = ( value << 1 ) - tresh + bitstream[( *index )--];
    2257             :     }
    2258             : 
    2259             : #ifdef DEBUGGING
    2260             :     assert( value < alphabet_size );
    2261             : #endif
    2262     3443113 :     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     9460475 : 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     9460475 :     msb_size = ( alph_size + ( 1 << gr_param ) - 1 ) >> gr_param; /* ceil division */
    2285     9460475 :     if ( msb_size <= 3 )
    2286             :     {
    2287      703340 :         value = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, alph_size );
    2288             :     }
    2289             :     else
    2290             :     {
    2291     8757135 :         msb = 0;
    2292    15425412 :         while ( ( msb < msb_size - 1 ) && ( bitstream[*index] != 0 ) )
    2293             :         {
    2294     6668277 :             msb++;
    2295     6668277 :             ( *index )--;
    2296             :         }
    2297             : 
    2298     8757135 :         if ( msb == msb_size - 1 )
    2299             :         {
    2300       34752 :             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     8722383 :             ( *index )--;
    2308     8722383 :             lsb = 0;
    2309    14204775 :             for ( i = 0; i < gr_param; i++ )
    2310             :             {
    2311     5482392 :                 lsb = ( lsb << 1 ) + bitstream[( *index )--];
    2312             :             }
    2313             :         }
    2314             : 
    2315     8757135 :         value = ( msb << gr_param ) + lsb;
    2316             :     }
    2317             : 
    2318             : #ifdef DEBUGGING
    2319             :     assert( value < alph_size );
    2320             : #endif
    2321     9460475 :     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     6897384 : 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     6897384 :     dist_reorder = ivas_qmetadata_dereorder_generic( elev_dist );
    2342     6897384 :     elev_index_reorder = elev_avg + dist_reorder;
    2343             : 
    2344     6897384 :     if ( elev_index_reorder < 0 )
    2345             :     {
    2346       30426 :         elev_index_reorder += elev_alph;
    2347             :     }
    2348     6866958 :     else if ( elev_index_reorder >= elev_alph )
    2349             :     {
    2350       51700 :         elev_index_reorder -= elev_alph;
    2351             :     }
    2352             : 
    2353     6897384 :     return elev_index_reorder;
    2354             : }
    2355             : 
    2356             : 
    2357             : /*-----------------------------------------------------------------------*
    2358             :  * Local functions: requentizeEC3
    2359             :  *-----------------------------------------------------------------------*/
    2360             : 
    2361             : /*! r: number of bits read */
    2362       23954 : 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       23954 :     bit_pos = *pbit_pos;
    2379             : 
    2380       23954 :     diff = 0;
    2381       23954 :     if ( q_direction->not_in_2D )
    2382             :     {
    2383       22838 :         if ( coding_subbands > 1 )
    2384             :         {
    2385       22687 :             j = ind_order[coding_subbands - 1];
    2386       22687 :             allowed_bits = 0;
    2387             : 
    2388       82682 :             for ( k = 0; k < masa_subframes; k++ )
    2389             :             {
    2390       59995 :                 allowed_bits += q_direction->band_data[j].bits_sph_idx[k];
    2391             :             }
    2392             : 
    2393       22687 :             last_j = j - ( allowed_bits == 0 );
    2394             : 
    2395      120404 :             for ( j = 0; j < last_j; j++ )
    2396             :             {
    2397       97717 :                 i = ind_order[j];
    2398       97717 :                 bits_dir0 = (int16_t *) q_direction->band_data[i].bits_sph_idx;
    2399             : 
    2400       97717 :                 nbits = 0;
    2401       97717 :                 allowed_bits = sum_s( bits_dir0, q_direction->cfg.nblocks );
    2402       97717 :                 use_vq = 0;
    2403       97717 :                 max_nb_idx = 0;
    2404             : 
    2405      386957 :                 for ( k = 0; k < q_direction->cfg.nblocks; k++ )
    2406             :                 {
    2407      289240 :                     if ( bits_dir0[k] > use_vq )
    2408             :                     {
    2409      116284 :                         use_vq = bits_dir0[k];
    2410      116284 :                         max_nb_idx = k;
    2411             :                     }
    2412             :                 }
    2413             : 
    2414       97717 :                 if ( q_direction->cfg.nblocks == 1 )
    2415             :                 {
    2416       33876 :                     byteBuffer = 0;
    2417             :                 }
    2418             :                 else
    2419             :                 {
    2420       63841 :                     byteBuffer = 0;
    2421       63841 :                     if ( use_vq <= 1 )
    2422             :                     {
    2423         403 :                         byteBuffer = 1;
    2424             :                     }
    2425       63841 :                     if ( use_vq > 1 && use_vq <= LIMIT_USE_COMMON )
    2426             :                     {
    2427        8493 :                         bits_dir0[max_nb_idx] -= 1;
    2428        8493 :                         allowed_bits -= 1;
    2429             :                         /* read 1 bit to tell if joint of VQ coding */
    2430        8493 :                         byteBuffer = bitstream[bit_pos--];
    2431             :                     }
    2432             :                 }
    2433             : 
    2434      386957 :                 for ( k = 0; k < masa_subframes; k++ )
    2435             :                 {
    2436      289240 :                     q_direction->band_data[i].bits_sph_idx[k] = bits_dir0[k];
    2437      289240 :                     if ( bits_dir0[k] > 2 )
    2438             :                     {
    2439      270230 :                         if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    2440             :                         {
    2441        4456 :                             q_direction->band_data[i].elevation_m_alphabet[k] = no_theta_masa[bits_dir0[k] - 3];
    2442             :                         }
    2443             :                         else
    2444             :                         {
    2445      265774 :                             q_direction->band_data[i].elevation_m_alphabet[k] = no_theta_masa[bits_dir0[k] - 3] * 2 - 1;
    2446             :                         }
    2447             :                     }
    2448             :                     else
    2449             :                     {
    2450       19010 :                         q_direction->band_data[i].elevation_m_alphabet[k] = 1;
    2451             :                     }
    2452             :                 }
    2453             : 
    2454       97717 :                 if ( allowed_bits > 0 )
    2455             :                 {
    2456       97717 :                     if ( byteBuffer == 1 )
    2457             :                     {
    2458        8833 :                         nbits = read_common_direction( bitstream, q_direction, i, masa_subframes, allowed_bits, &bit_pos );
    2459             :                     }
    2460             :                     else
    2461             :                     {
    2462       88884 :                         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       18548 :                             fixed_rate = 1;
    2466       18548 :                             nbits = 0;
    2467             :                         }
    2468             :                         else
    2469             :                         {
    2470             :                             /* check if fixed_rate */
    2471       70336 :                             fixed_rate = bitstream[bit_pos--];
    2472       70336 :                             nbits = 1;
    2473             :                         }
    2474             : 
    2475       88884 :                         if ( fixed_rate == 1 )
    2476             :                         {
    2477             :                             /* decode_fixed_rate()*/
    2478       49851 :                             nbits += decode_fixed_rate( q_direction, bitstream, &bit_pos, i, masa_subframes );
    2479             :                         }
    2480             :                         else
    2481             :                         {
    2482             :                             /* decode elevation */
    2483       39033 :                             nbits += decode_elevation( q_direction, bitstream, &bit_pos, i, masa_subframes );
    2484             : 
    2485             :                             /* decode azimuth */
    2486       39033 :                             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       97717 :                 diff += nbits - allowed_bits;
    2495             : 
    2496             :                 /* update bits for next block */
    2497       97717 :                 update_bits_next_block( q_direction, &diff, ind_order[j + 1], coding_subbands, masa_subframes );
    2498             :             }
    2499             :         }
    2500             :         else
    2501             :         {
    2502         151 :             last_j = q_direction->cfg.start_band;
    2503             :         }
    2504             : 
    2505             : 
    2506       70498 :         for ( j = last_j; j < coding_subbands; j++ )
    2507             :         {
    2508       47660 :             i = ind_order[j];
    2509       47660 :             bits_dir0 = (int16_t *) q_direction->band_data[i].bits_sph_idx;
    2510             : 
    2511       47660 :             nbits = 0;
    2512       47660 :             allowed_bits = sum_s( bits_dir0, q_direction->cfg.nblocks );
    2513       47660 :             if ( allowed_bits > 0 && masa_subframes == 1 )
    2514             :             {
    2515       18146 :                 nbits += decode_fixed_rate( q_direction, bitstream, &bit_pos, i, masa_subframes );
    2516             :             }
    2517             :             else
    2518             :             {
    2519       29514 :                 if ( allowed_bits > 0 )
    2520             :                 {
    2521       29514 :                     use_vq = 0;
    2522       29514 :                     max_nb_idx = 0;
    2523      147570 :                     for ( k = 0; k < masa_subframes; k++ )
    2524             :                     {
    2525      118056 :                         if ( bits_dir0[k] > use_vq )
    2526             :                         {
    2527       34312 :                             use_vq = bits_dir0[k];
    2528       34312 :                             max_nb_idx = k;
    2529             :                         }
    2530             :                     }
    2531             : 
    2532       29514 :                     byteBuffer = 0;
    2533             : 
    2534       29514 :                     if ( use_vq > 1 && use_vq <= LIMIT_USE_COMMON )
    2535             :                     {
    2536        3956 :                         bits_dir0[max_nb_idx] -= 1;
    2537        3956 :                         allowed_bits -= 1;
    2538             : 
    2539             :                         /* read 1 bit to tell if joint of VQ coding */
    2540        3956 :                         byteBuffer = bitstream[bit_pos--];
    2541             :                     }
    2542             : 
    2543       29514 :                     if ( allowed_bits > 0 )
    2544             :                     {
    2545       29514 :                         if ( byteBuffer == 1 || use_vq <= 1 )
    2546             :                         {
    2547        3927 :                             nbits = read_common_direction( bitstream, q_direction, i, masa_subframes, allowed_bits, &bit_pos );
    2548             :                         }
    2549             :                         else
    2550             :                         {
    2551             :                             /* decode_fixed_rate()*/
    2552       25587 :                             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        6426 :         for ( j = 0; j < coding_subbands; j++ )
    2571             :         {
    2572       23589 :             for ( k = 0; k < q_direction->cfg.nblocks; k++ )
    2573             :             {
    2574       18279 :                 q_direction->band_data[j].elevation[k] = 0;
    2575       18279 :                 q_direction->band_data[j].elevation_index[k] = 0;
    2576             :             }
    2577             :         }
    2578        1116 :         nbits = decode_azimuth2D( q_direction, bitstream, coding_subbands, &bit_pos, masa_subframes );
    2579             :     }
    2580       23954 :     nbits = *pbit_pos - bit_pos;
    2581       23954 :     *pbit_pos = bit_pos;
    2582             : 
    2583       23954 :     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       39033 : 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       39033 :     nbits = 0;
    2608       39033 :     bit_pos = *pbit_pos;
    2609       39033 :     byteBuffer = 0;
    2610             : 
    2611       39033 :     j_az = 0;
    2612             :     /* check number of valid indexes to decode */
    2613      194922 :     for ( k = 0; k < masa_subframes; k++ )
    2614             :     {
    2615      155889 :         if ( q_direction->band_data[idx_subband].azimuth_index[k] < MASA_NO_INDEX )
    2616             :         {
    2617      155887 :             j_az++;
    2618             :         }
    2619             :         else
    2620             :         {
    2621           2 :             q_direction->band_data[idx_subband].azimuth[k] = 0.f; /*To be in sync with encoder values.*/
    2622             :         }
    2623             :     }
    2624             : 
    2625       39033 :     if ( j_az == 0 )
    2626             :     {
    2627           0 :         return nbits;
    2628             :     }
    2629             : 
    2630       39033 :     if ( byteBuffer == 0 )
    2631             :     {
    2632             :         /* use context */
    2633       39033 :         use_context = 0;
    2634      194922 :         for ( k = 0; k < masa_subframes; k++ )
    2635             :         {
    2636      155889 :             if ( q_direction->band_data[idx_subband].bits_sph_idx[k] <= 1 )
    2637             :             {
    2638           0 :                 use_context = 1;
    2639             :             }
    2640             :         }
    2641             : 
    2642       39033 :         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       39033 :             byteBuffer = bitstream[bit_pos--];
    2673       39033 :             if ( byteBuffer == 0 ) /* regular GR coding5 */
    2674             :             {
    2675             :                 /* read GR_order */
    2676       10963 :                 byteBuffer = bitstream[bit_pos--];
    2677       10963 :                 nbits += 1;
    2678             : 
    2679       54572 :                 for ( k = 0; k < masa_subframes; k++ )
    2680             :                 {
    2681       43609 :                     if ( q_direction->band_data[idx_subband].bits_sph_idx[k] > 0 )
    2682             :                     {
    2683       43609 :                         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       43607 :                             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       43607 :                             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           2 :                             q_direction->band_data[idx_subband].azimuth[k] = 0;
    2691           2 :                             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       28070 :                 byteBuffer = bitstream[bit_pos--];
    2706             :                 /* read min index value */
    2707       28070 :                 maximum_s( q_direction->band_data[idx_subband].azimuth_m_alphabet, masa_subframes, &max_val );
    2708       28070 :                 min_idx = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, max_val, MASA_GR_ORD_AZ );
    2709             : 
    2710      140350 :                 for ( k = 0; k < masa_subframes; k++ )
    2711             :                 {
    2712      112280 :                     if ( q_direction->band_data[idx_subband].bits_sph_idx[k] > 0 )
    2713             :                     {
    2714      112280 :                         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      112280 :                             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      112280 :                             q_direction->band_data[idx_subband].azimuth_index[k] += min_idx;
    2718      112280 :                             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           0 :                             q_direction->band_data[idx_subband].azimuth[k] = 0;
    2723           0 :                             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       39033 :     nbits = *pbit_pos - bit_pos;
    2737             : 
    2738       39033 :     *pbit_pos = bit_pos;
    2739             : 
    2740       39033 :     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       39033 : 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       39033 :     nr_NO_INDEX = 0;
    2766       39033 :     nbits = 0;
    2767       39033 :     bit_pos = *pbit_pos;
    2768             : 
    2769      194922 :     for ( k = 0; k < masa_subframes; k++ )
    2770             :     {
    2771      155889 :         q_direction->band_data[j].elevation_index[k] = 0;
    2772      155889 :         q_direction->band_data[j].elevation[k] = 0;
    2773             : 
    2774      155889 :         if ( q_direction->band_data[j].bits_sph_idx[k] > 0 )
    2775             :         {
    2776      155889 :             if ( q_direction->band_data[j].bits_sph_idx[k] <= 2 )
    2777             :             {
    2778           3 :                 q_direction->band_data[j].elevation_index[k] = MASA_NO_INDEX;
    2779           3 :                 nr_NO_INDEX += 1;
    2780           3 :                 q_direction->band_data[j].elevation[k] = 0;
    2781           3 :                 q_direction->band_data[j].elevation_m_alphabet[k] = 1;
    2782             :             }
    2783             :             else
    2784             :             {
    2785      155886 :                 if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    2786             :                 {
    2787        2489 :                     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      153397 :                     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       39033 :     if ( nr_NO_INDEX < masa_subframes )
    2802             :     {
    2803             :         {
    2804             :             /* read if same or not */
    2805       39033 :             byteBuffer = bitstream[bit_pos--];
    2806       39033 :             if ( byteBuffer == 1 ) /* same value */
    2807             :             {
    2808             :                 /* read value */
    2809       28609 :                 byteBuffer = bitstream[bit_pos--];
    2810       28609 :                 byteBuffer = ( byteBuffer << 1 ) + bitstream[bit_pos--];
    2811       28609 :                 same_idx = byteBuffer;
    2812             : 
    2813      142802 :                 for ( k = 0; k < masa_subframes; k++ )
    2814             :                 {
    2815      114193 :                     if ( q_direction->band_data[j].elevation_index[k] < MASA_NO_INDEX )
    2816             :                     {
    2817      114193 :                         q_direction->band_data[j].elevation_index[k] = same_idx;
    2818      114193 :                         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       10424 :                 byteBuffer = bitstream[bit_pos--];
    2826       10424 :                 GR_ord_elevation = MASA_GR_ORD_EL - byteBuffer;
    2827             : 
    2828       52120 :                 for ( k = 0; k < masa_subframes; k++ )
    2829             :                 {
    2830       41696 :                     if ( q_direction->band_data[j].elevation_index[k] < MASA_NO_INDEX )
    2831             :                     {
    2832       41693 :                         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       41693 :                         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      194922 :     for ( k = 0; k < masa_subframes; k++ )
    2841             :     {
    2842      155889 :         if ( ( q_direction->band_data[j].elevation_index[k] < MASA_NO_INDEX ) &&
    2843      155886 :              ( no_phi_masa[q_direction->band_data[j].bits_sph_idx[k] - 1][q_direction->band_data[j].elevation_index[k]] <= 1 ) )
    2844             :         {
    2845           2 :             q_direction->band_data[j].azimuth_index[k] = MASA_NO_INDEX;
    2846           2 :             q_direction->band_data[j].azimuth_m_alphabet[k] = 1;
    2847             :         }
    2848             :         else
    2849             :         {
    2850      155887 :             q_direction->band_data[j].azimuth_index[k] = 0;
    2851      155887 :             if ( q_direction->band_data[j].elevation_index[k] < MASA_NO_INDEX )
    2852             :             {
    2853      155884 :                 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           3 :                 q_direction->band_data[j].azimuth_m_alphabet[k] = no_phi_masa[q_direction->band_data[j].bits_sph_idx[k] - 1][0];
    2858           3 :                 q_direction->band_data[j].elevation_index[k] = 0;
    2859             :             }
    2860             :         }
    2861             :     }
    2862             : 
    2863       39033 :     nbits = *pbit_pos - bit_pos;
    2864       39033 :     *pbit_pos = bit_pos;
    2865             : 
    2866       39033 :     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      930039 : 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      930039 :     nbits = 0;
    2889             : 
    2890     4150530 :     for ( m = 0; m < nblocks; m++ )
    2891             :     {
    2892     3220491 :         value = 0;
    2893    16947554 :         for ( i = 0; i < q_direction->band_data[b].bits_sph_idx[m]; i++ )
    2894             :         {
    2895    13727063 :             value = ( value << 1 ) + bitstream[( *pbit_pos )--];
    2896             :         }
    2897             : 
    2898     3220491 :         q_direction->band_data[b].spherical_index[m] = value;
    2899     3220491 :         nbits += q_direction->band_data[b].bits_sph_idx[m];
    2900             : 
    2901     3220491 :         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      930039 :     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        1116 : 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        1116 :     bit_pos = *pbit_pos;
    2930        1116 :     nbits = 0;
    2931        6426 :     for ( j = 0; j < coding_subbands; j++ )
    2932             :     {
    2933        5310 :         bits_dir0 = (int16_t *) q_direction->band_data[j].bits_sph_idx;
    2934        5310 :         allowed_bits = sum_s( bits_dir0, no_frames );
    2935             : 
    2936        5310 :         if ( allowed_bits > 0 )
    2937             :         {
    2938        5310 :             use_vq = 0;
    2939       23589 :             for ( k = 0; k < no_frames; k++ )
    2940             :             {
    2941       18279 :                 q_direction->band_data[j].elevation[k] = 0;
    2942       18279 :                 q_direction->band_data[j].elevation_index[k] = 0;
    2943             : 
    2944       18279 :                 if ( bits_dir0[k] > use_vq )
    2945             :                 {
    2946        6117 :                     use_vq = bits_dir0[k];
    2947             :                 }
    2948             :             }
    2949             : 
    2950        5310 :             if ( use_vq <= 3 && allowed_bits <= 11 )
    2951             :             {
    2952        1781 :                 if ( allowed_bits <= no_frames + 1 )
    2953             :                 {
    2954         217 :                     set_f( q_direction->band_data[j].azimuth, 0.0f, no_frames );
    2955        1058 :                     for ( k = 0; k < min( allowed_bits, no_frames ); k++ )
    2956             :                     {
    2957         841 :                         q_direction->band_data[j].azimuth[k] = ( -180.f ) * bitstream[bit_pos--];
    2958         841 :                         nbits += 1;
    2959             :                     }
    2960             :                 }
    2961             :                 else
    2962             :                 {
    2963        1564 :                     nbits += read_truncGR_azimuth( bitstream, q_direction, j, no_frames, &bit_pos );
    2964             :                 }
    2965             :             }
    2966             :             else
    2967             :             {
    2968       15032 :                 for ( k = 0; k < no_frames; k++ )
    2969             :                 {
    2970       11503 :                     Buffer = 0;
    2971       57490 :                     for ( i = 0; i < bits_dir0[k]; i++ )
    2972             :                     {
    2973       45987 :                         Buffer = ( Buffer << 1 ) + bitstream[bit_pos--];
    2974             :                     }
    2975             : 
    2976       11503 :                     nbits += bits_dir0[k];
    2977             : 
    2978       11503 :                     if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    2979             :                     {
    2980        2463 :                         q_direction->band_data[j].azimuth[k] = 360.0f / (float) ( 1 << bits_dir0[k] ) * Buffer - 180;
    2981        2463 :                         q_direction->band_data[j].azimuth[k] = companding_azimuth( q_direction->band_data[j].azimuth[k], q_direction->cfg.mc_ls_setup,
    2982        2463 :                                                                                    ( q_direction->band_data[j].elevation[k] > MC_MASA_THR_ELEVATION ), -1 );
    2983             :                     }
    2984             :                     else
    2985             :                     {
    2986        9040 :                         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        1116 :     *pbit_pos = bit_pos;
    2997             : 
    2998        1116 :     return nbits;
    2999             : }
    3000             : 
    3001             : 
    3002             : /*-------------------------------------------------------------------*
    3003             :  * set_zero_direction()
    3004             :  *
    3005             :  *
    3006             :  *-------------------------------------------------------------------*/
    3007             : 
    3008       12760 : 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       63800 :     for ( k = 0; k < len; k++ )
    3016             :     {
    3017       51040 :         q_direction->band_data[idx_band].azimuth[k] = 0;
    3018       51040 :         q_direction->band_data[idx_band].azimuth_index[k] = 0;
    3019       51040 :         q_direction->band_data[idx_band].elevation[k] = 0;
    3020       51040 :         q_direction->band_data[idx_band].elevation_index[k] = 0;
    3021       51040 :         q_direction->band_data[idx_band].spherical_index[k] = 0;
    3022             :     }
    3023             : 
    3024       12760 :     return;
    3025             : }
    3026             : 
    3027             : 
    3028             : /*-------------------------------------------------------------------*
    3029             :  * read_truncGR_azimuth()
    3030             :  *
    3031             :  *
    3032             :  *-------------------------------------------------------------------*/
    3033             : 
    3034       13362 : 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       13362 :     allowed_bits = sum_s( (int16_t *) q_direction->band_data[j].bits_sph_idx, no_subframes );
    3048       13362 :     nbits = 0;
    3049       13362 :     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       13362 :     if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    3068             :     {
    3069         503 :         no_symb = 9;
    3070             :     }
    3071             :     else
    3072             :     {
    3073       12859 :         no_symb = 8;
    3074             :     }
    3075       13362 :     nbits = 0;
    3076             : 
    3077       13362 :     nbits = *pbit_pos;
    3078             : 
    3079       66489 :     for ( i = 0; i < no_subframes; i++ )
    3080             :     {
    3081       53127 :         idx = ivas_qmetadata_DecodeExtendedGR( bitstream, pbit_pos, no_symb, 0 );
    3082       53127 :         q_direction->band_data[j].azimuth_index[i] = idx;
    3083       53127 :         if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    3084             :         {
    3085        2006 :             q_direction->band_data[j].azimuth[i] = cb_azi_chan[( idx + 1 ) >> 1];
    3086        2006 :             if ( idx % 2 > 0 )
    3087             :             {
    3088         790 :                 q_direction->band_data[j].azimuth[i] = -q_direction->band_data[j].azimuth[i];
    3089             :             }
    3090             :         }
    3091             :         else
    3092             :         {
    3093       51121 :             q_direction->band_data[j].azimuth[i] = azimuth_cb[idx];
    3094             :         }
    3095             :     }
    3096             : 
    3097       13362 :     nbits -= *pbit_pos;
    3098             : 
    3099       13362 :     return nbits;
    3100             : }
    3101             : 
    3102             : 
    3103             : /*-------------------------------------------------------------------*
    3104             :  * read_common_direction()
    3105             :  *
    3106             :  *
    3107             :  *-------------------------------------------------------------------*/
    3108             : 
    3109             : /*! r: number of bits read */
    3110       12760 : 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       12760 :     bit_pos = *pbit_pos;
    3126       12760 :     nbits = 0;
    3127             : 
    3128       12760 :     set_zero_direction( q_direction, j, no_subframes );
    3129       12760 :     if ( bits_total == 0 )
    3130             :     {
    3131           0 :         return nbits;
    3132             :     }
    3133             : 
    3134       12760 :     if ( bits_total <= no_subframes + 1 )
    3135             :     {
    3136        3480 :         for ( i = 0; i < min( no_subframes, bits_total ); i++ )
    3137             :         {
    3138        2784 :             byteBuffer = bitstream[bit_pos--];
    3139             :             /*qdirection->azimuth_index[j][i] = (uint16_t)byteBuffer; */
    3140        2784 :             q_direction->band_data[j].azimuth[i] = azimuth_cb[byteBuffer];
    3141        2784 :             nbits += 1;
    3142             :         }
    3143         696 :         *pbit_pos = bit_pos;
    3144             : 
    3145             :         /*nbits += read_truncGR_azimuth(bitstream, qdirection, j, no_subframes, pbit_pos); */
    3146         696 :         return nbits;
    3147             :     }
    3148             : 
    3149             : 
    3150       12064 :     byteBuffer = bitstream[bit_pos--];
    3151       12064 :     bits_el = 1;
    3152       12064 :     nbits += 1;
    3153             :     /* elevation is already set to 0*/
    3154       12064 :     if ( byteBuffer == 1 )
    3155             :     {
    3156        1919 :         byteBuffer = bitstream[bit_pos--];
    3157        1919 :         bits_el += 1;
    3158        1919 :         nbits += 1;
    3159        1919 :         if ( byteBuffer == 0 )
    3160             :         {
    3161        3775 :             for ( i = 0; i < no_subframes; i++ )
    3162             :             {
    3163        3020 :                 q_direction->band_data[j].elevation[i] = delta_theta_masa[2];
    3164             :             }
    3165             :         }
    3166             :         else
    3167             :         {
    3168        1164 :             byteBuffer = bitstream[bit_pos--];
    3169        1164 :             bits_el += 1;
    3170        1164 :             nbits += 1;
    3171        1164 :             if ( byteBuffer == 0 )
    3172             :             {
    3173        5765 :                 for ( i = 0; i < no_subframes; i++ )
    3174             :                 {
    3175        4612 :                     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          11 :                 byteBuffer = bitstream[bit_pos--];
    3182          11 :                 nbits += 1;
    3183          11 :                 if ( byteBuffer == 0 )
    3184             :                 {
    3185           9 :                     set_f( q_direction->band_data[j].elevation, 90.0f, no_subframes );
    3186           9 :                     set_f( q_direction->band_data[j].azimuth, 0.0f, no_subframes );
    3187             :                 }
    3188             :                 else
    3189             :                 {
    3190           2 :                     set_f( q_direction->band_data[j].elevation, -90.0f, no_subframes );
    3191           2 :                     set_f( q_direction->band_data[j].azimuth, 0.0f, no_subframes );
    3192             :                 }
    3193          11 :                 *pbit_pos = bit_pos;
    3194             : 
    3195          11 :                 return nbits;
    3196             :             }
    3197             :         }
    3198             :     }
    3199             : 
    3200       12053 :     bits_el = sum_s( (int16_t *) q_direction->band_data[j].bits_sph_idx, no_subframes ) - bits_el;
    3201             : 
    3202       12053 :     if ( bits_el <= no_subframes + 1 )
    3203             :     {
    3204         255 :         nbits += min( no_subframes, bits_el );
    3205        1240 :         for ( i = 0; i < min( no_subframes, bits_el ); i++ )
    3206             :         {
    3207         985 :             byteBuffer = bitstream[bit_pos--];
    3208             :             /*qdirection->azimuth_index[j][i] = (uint16_t) byteBuffer; */
    3209         985 :             q_direction->band_data[j].azimuth[i] = azimuth_cb[byteBuffer];
    3210             :         }
    3211             :     }
    3212             :     else
    3213             :     {
    3214       11798 :         nbits += read_truncGR_azimuth( bitstream, q_direction, j, no_subframes, &bit_pos );
    3215             :     }
    3216             : 
    3217       12053 :     *pbit_pos = bit_pos;
    3218             : 
    3219             : #ifdef DEBUGGING
    3220             :     /*assert(nbits == bits_total); */
    3221             : #endif
    3222       12053 :     return nbits;
    3223             : }
    3224             : 
    3225             : 
    3226             : /*-----------------------------------------------------------------------*
    3227             :  * Local functions: coherence
    3228             :  *-----------------------------------------------------------------------*/
    3229             : 
    3230       68756 : 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       68756 :     coding_subbands_0 = hQMetaData->q_direction[0].cfg.nbands;
    3247       68756 :     coding_subbands = hQMetaData->q_direction[idx_d].cfg.nbands;
    3248       68756 :     if ( coding_subbands_0 <= 5 )
    3249             :     {
    3250      254370 :         for ( j = 0; j < 5; j++ )
    3251             :         {
    3252      211975 :             MASA_grouping[j] = j;
    3253             :         }
    3254             :     }
    3255             :     else
    3256             :     {
    3257       26361 :         if ( coding_subbands_0 <= 8 )
    3258             :         {
    3259        7352 :             mvs2s( MASA_grouping_8_to_5, MASA_grouping, 8 );
    3260             :         }
    3261       19009 :         else if ( coding_subbands_0 <= 12 )
    3262             :         {
    3263       11451 :             mvs2s( MASA_grouping_12_to_5, MASA_grouping, 12 );
    3264             :         }
    3265        7558 :         else if ( coding_subbands_0 <= 18 )
    3266             :         {
    3267        1516 :             mvs2s( MASA_grouping_18_to_5, MASA_grouping, 18 );
    3268             :         }
    3269             :         else
    3270             :         {
    3271        6042 :             if ( coding_subbands_0 <= 24 )
    3272             :             {
    3273        6042 :                 mvs2s( MASA_grouping_24_to_5, MASA_grouping, 24 );
    3274             :             }
    3275             :         }
    3276             :     }
    3277             : 
    3278       68756 :     if ( coding_subbands < coding_subbands_0 )
    3279             :     {
    3280       19260 :         d = 0;
    3281      161162 :         for ( j = 0; j < coding_subbands_0; j++ )
    3282             :         {
    3283      141902 :             if ( hQMetaData->twoDirBands[j] == 1 )
    3284             :             {
    3285       62529 :                 two_dir_band[d++] = j;
    3286             :             }
    3287             :         }
    3288             :     }
    3289             :     else
    3290             :     {
    3291       49496 :         set_s( two_dir_band, 0, coding_subbands );
    3292             :     }
    3293             : 
    3294       68756 :     q_direction = &hQMetaData->q_direction[idx_d];
    3295             : 
    3296      564350 :     for ( i = 0; i < coding_subbands; i++ )
    3297             :     {
    3298      495594 :         var_azi = var( q_direction->band_data[i].azimuth, no_frames );
    3299             : 
    3300      495594 :         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      495594 :             min_index = q_direction->band_data[i].energy_ratio_index[0];
    3308             :         }
    3309             : 
    3310      495594 :         if ( var_azi < MASA_DELTA_AZI_DCT0 )
    3311             :         {
    3312      128761 :             idx_sub_cb = MASA_NO_CV_COH * min_index;
    3313             :         }
    3314             :         else
    3315             :         {
    3316      366833 :             idx_sub_cb = MASA_NO_CV_COH * ( min_index + DIRAC_DIFFUSE_LEVELS ); /* NO_CV_COH = 8 */
    3317             :         }
    3318             : 
    3319      495594 :         dct_coh[i][0] = coherence_cb0_masa[idx_sub_cb + q_direction->coherence_band_data[i].spread_coherence_dct0_index];
    3320             : 
    3321      495594 :         if ( coding_subbands < coding_subbands_0 )
    3322             :         {
    3323       62529 :             assert( idx_d == 1 );
    3324       62529 :             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      433065 :             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     1486782 :         for ( j = 2; j < MAX_PARAM_SPATIAL_SUBFRAMES; j++ )
    3332             :         {
    3333      991188 :             dct_coh[i][j] = 0;
    3334             :         }
    3335             : 
    3336      495594 :         invdct4_transform( dct_coh[i], q_direction->coherence_band_data[i].spread_coherence );
    3337             :     }
    3338             : 
    3339       68756 :     return;
    3340             : }
    3341             : 
    3342             : 
    3343             : /*-------------------------------------------------------------------*
    3344             :  * read_huf()
    3345             :  *
    3346             :  * Read Hufman code
    3347             :  *-------------------------------------------------------------------*/
    3348             : 
    3349             : /*! r: number of bits read */
    3350       68756 : 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       68756 :     int16_t done = 0, end_pos;
    3361             :     uint16_t ByteBuffer;
    3362             :     int16_t nbits, val;
    3363             :     uint16_t i;
    3364             : 
    3365       68756 :     end_pos = start_pos;
    3366       68756 :     nbits = 0;
    3367       68756 :     val = 0;
    3368      341218 :     while ( ( done == 0 ) && ( nbits < max_len ) )
    3369             :     {
    3370      272462 :         ByteBuffer = bitstream[end_pos--];
    3371      272462 :         val = val * 2 + ( ByteBuffer & 1 );
    3372      272462 :         nbits += 1;
    3373     1292567 :         for ( i = 0; i < len; i++ )
    3374             :         {
    3375     1088861 :             if ( val == huff_code[i] )
    3376             :             {
    3377       68756 :                 *out = i;
    3378       68756 :                 done = 1;
    3379       68756 :                 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       68756 :     *num_bits_read = end_pos;
    3391             : 
    3392       68756 :     return IVAS_ERR_OK;
    3393             : }
    3394             : 
    3395             : 
    3396             : /*-------------------------------------------------------------------*
    3397             :  * read_GR_min_removed_data()
    3398             :  *
    3399             :  *
    3400             :  *-------------------------------------------------------------------*/
    3401             : 
    3402        2433 : 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        2433 :     bit_pos = *p_bit_pos;
    3417             : 
    3418             :     /* read GR order */
    3419        2433 :     byteBuffer = bitstream[bit_pos--];
    3420        2433 :     nbits = 1;
    3421             : 
    3422             :     /* read min index */
    3423        2433 :     bits_GR = bit_pos;
    3424        2433 :     min_index = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_symb, 0 );
    3425        2433 :     nbits += bits_GR - bit_pos;
    3426             : 
    3427             :     /* read GR data */
    3428       33812 :     for ( j = 0; j < no_data; j++ )
    3429             :     {
    3430       31379 :         bits_GR = bit_pos;
    3431       31379 :         if ( no_cv_vec[j] > 1 )
    3432             :         {
    3433       31073 :             decoded_idx[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_cv_vec[j] - min_index, ( byteBuffer & 1 ) );
    3434       31073 :             nbits += bits_GR - bit_pos;
    3435             :         }
    3436             :         else
    3437             :         {
    3438         306 :             decoded_idx[j] = 0;
    3439             :         }
    3440             :     }
    3441             : 
    3442       33812 :     for ( j = 0; j < no_data; j++ )
    3443             :     {
    3444       31379 :         if ( no_cv_vec[j] > 1 )
    3445             :         {
    3446       31073 :             decoded_idx[j] += min_index;
    3447             :         }
    3448             :     }
    3449             : 
    3450             : #ifdef DEBUGGING
    3451             :     assert( nbits == *p_bit_pos - bit_pos );
    3452             : #endif
    3453        2433 :     *p_bit_pos = bit_pos;
    3454             : 
    3455        2433 :     return nbits;
    3456             : }
    3457             : 
    3458             : 
    3459             : /*-------------------------------------------------------------------*
    3460             :  * decode_fixed_rate_composed_index_coherence()
    3461             :  *
    3462             :  *
    3463             :  *-------------------------------------------------------------------*/
    3464             : 
    3465       11952 : 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       11952 :     bit_pos = *p_bit_pos;
    3485       11952 :     set_s( (int16_t *) temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
    3486             : 
    3487       11952 :     no_cb = 1;
    3488       11952 :     nbits = 0;
    3489       11952 :     if ( no_bands > MASA_LIMIT_NO_BANDS_SUR_COH )
    3490             :     {
    3491             :         /* read 8-max_val with GR0 */
    3492        1425 :         bits_GR = bit_pos;
    3493        1425 :         no_vals_local = no_symb - ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_symb, 0 );
    3494        1425 :         nbits += bits_GR - bit_pos;
    3495       24373 :         for ( j = 0; j < no_bands; j++ )
    3496             :         {
    3497       22948 :             if ( no_cv_vec[j] > no_vals_local )
    3498             :             {
    3499       18105 :                 no_cv_vec[j] = no_vals_local;
    3500             :             }
    3501             :         }
    3502             :     }
    3503             : 
    3504       11952 :     half_no_bands = no_bands / 2;
    3505       11952 :     if ( sum_s( no_cv_vec, no_bands ) > MASA_COH_LIMIT_2IDX )
    3506             :     {
    3507         267 :         no_cb = 1;
    3508             : 
    3509        3353 :         for ( j = 0; j < half_no_bands; j++ )
    3510             :         {
    3511        3086 :             no_cb *= no_cv_vec[j];
    3512             :         }
    3513         267 :         no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3514         267 :         no_cb = 1;
    3515        3369 :         for ( j = half_no_bands; j < no_bands; j++ )
    3516             :         {
    3517        3102 :             no_cb *= no_cv_vec[j];
    3518             :         }
    3519         267 :         no_bits_vec1 = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3520             :     }
    3521             :     else
    3522             :     {
    3523       11685 :         no_cb = 1;
    3524       75363 :         for ( j = 0; j < no_bands; j++ )
    3525             :         {
    3526       63678 :             no_cb *= no_cv_vec[j];
    3527             :         }
    3528       11685 :         no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3529       11685 :         no_bits_vec1 = 0;
    3530             :     }
    3531       11952 :     if ( no_bits_vec1 > 0 )
    3532             :     {
    3533         267 :         idx_fr = 0;
    3534        8299 :         for ( j = 0; j < no_bits_vec; j++ )
    3535             :         {
    3536        8032 :             idx_fr = ( idx_fr << 1 ) + bitstream[bit_pos--];
    3537             :         }
    3538             : 
    3539         267 :         nbits += no_bits_vec;
    3540             : 
    3541         267 :         decode_combined_index( idx_fr, no_cv_vec, temp_index, half_no_bands );
    3542             : 
    3543         267 :         idx_fr = 0;
    3544        8946 :         for ( j = 0; j < no_bits_vec1; j++ )
    3545             :         {
    3546        8679 :             idx_fr = ( idx_fr << 1 ) + bitstream[bit_pos--];
    3547             :         }
    3548         267 :         nbits += no_bits_vec1;
    3549         267 :         decode_combined_index( idx_fr, &no_cv_vec[half_no_bands], &temp_index[half_no_bands], half_no_bands );
    3550             :     }
    3551             :     else
    3552             :     {
    3553       11685 :         idx_fr = 0;
    3554      108176 :         for ( j = 0; j < no_bits_vec; j++ )
    3555             :         {
    3556       96491 :             idx_fr = ( idx_fr << 1 ) + bitstream[bit_pos--];
    3557             :         }
    3558       11685 :         nbits += no_bits_vec;
    3559       11685 :         decode_combined_index( idx_fr, no_cv_vec, temp_index, no_bands );
    3560             :     }
    3561             : 
    3562       81818 :     for ( j = 0; j < no_bands; j++ )
    3563             :     {
    3564       69866 :         decoded_index[j] = temp_index[j];
    3565             :     }
    3566             : #ifdef DEBUGGING
    3567             :     assert( nbits == *p_bit_pos - bit_pos );
    3568             : #endif
    3569       11952 :     nbits = *p_bit_pos - bit_pos;
    3570             : 
    3571       11952 :     *p_bit_pos = bit_pos;
    3572             : 
    3573       11952 :     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        3578 : 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        3578 :     nbands = hQMetaData->q_direction[idx_dir].cfg.nbands;
    3599        3578 :     nblocks = hQMetaData->q_direction[idx_dir].cfg.nblocks;
    3600             : 
    3601        3578 :     cb_size = 1 << nbits_coh;
    3602        3578 :     delta = 256.0f / cb_size;
    3603        3578 :     nbits = *p_bit_pos;
    3604       17044 :     for ( k = 0; k < nblocks; k++ )
    3605             :     {
    3606             :         /* read method */
    3607       13466 :         if ( bitstream[( *p_bit_pos )--] == 1 )
    3608             :         {
    3609             :             /* average removed */
    3610             :             /* read average index */
    3611         138 :             min_index = 0;
    3612         574 :             for ( i = 0; i < nbits_coh; i++ )
    3613             :             {
    3614         436 :                 min_index = ( min_index << 1 ) + bitstream[( *p_bit_pos )--];
    3615             :             }
    3616             :             /* read GR param */
    3617         138 :             GR_param = bitstream[( *p_bit_pos )--];
    3618        1866 :             for ( j = 0; j < nbands; j++ )
    3619             :             {
    3620        1728 :                 decoded_idx = ivas_qmetadata_DecodeExtendedGR( bitstream, p_bit_pos, 2 * cb_size, GR_param );
    3621        1728 :                 if ( decoded_idx % 2 )
    3622             :                 {
    3623         716 :                     decoded_idx = ( ( decoded_idx + 1 ) >> 1 ) + min_index;
    3624             :                 }
    3625             :                 else
    3626             :                 {
    3627        1012 :                     decoded_idx = -( decoded_idx >> 1 ) + min_index;
    3628             :                 }
    3629        1728 :                 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       13328 :             min_index = 0;
    3636       58130 :             for ( i = 0; i < nbits_coh; i++ )
    3637             :             {
    3638       44802 :                 min_index = ( min_index << 1 ) + bitstream[( *p_bit_pos )--];
    3639             :             }
    3640             : 
    3641             :             /* read GR param */
    3642       13328 :             GR_param = bitstream[( *p_bit_pos )--];
    3643      287495 :             for ( j = 0; j < nbands; j++ )
    3644             :             {
    3645      274167 :                 decoded_idx = ivas_qmetadata_DecodeExtendedGR( bitstream, p_bit_pos, cb_size - min_index, GR_param ) + min_index;
    3646      274167 :                 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        3578 :     nbits = nbits - *p_bit_pos;
    3652             : 
    3653        3578 :     return nbits;
    3654             : }
    3655             : 
    3656             : 
    3657             : /*------------------------------------------------------------------- *
    3658             :  * read_coherence_data()
    3659             :  *
    3660             :  * Read coherence data
    3661             :  *------------------------------------------------------------------- */
    3662             : 
    3663             : /*! r: number of bits read */
    3664       76922 : 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       76922 :     coding_subbands = hQMetaData->q_direction[idx_dir].cfg.nbands;
    3691       76922 :     extra_cv = (int16_t) ( coding_subbands / MASA_FACTOR_CV_COH );
    3692       76922 :     q_direction = &( hQMetaData->q_direction[idx_dir] );
    3693       76922 :     bit_pos = *p_bit_pos;
    3694       76922 :     nbits = 0;
    3695             : 
    3696       76922 :     if ( q_direction->cfg.nblocks == 1 )
    3697             :     {
    3698       69201 :         for ( j = 0; j < coding_subbands; j++ )
    3699             :         {
    3700       61035 :             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       61035 :                 idx_ER = 7 - q_direction->band_data[j].energy_ratio_index_mod[0] + extra_cv;
    3707             :             }
    3708       61035 :             no_cv_vec[j] = idx_ER + 1;
    3709             :         }
    3710             : 
    3711        8166 :         if ( sum_s( no_cv_vec, coding_subbands ) == coding_subbands )
    3712             :         {
    3713        4875 :             for ( j = 0; j < coding_subbands; j++ )
    3714             :             {
    3715        4014 :                 q_direction->coherence_band_data[j].spread_coherence[0] = 0;
    3716             :             }
    3717             : 
    3718         861 :             return 0;
    3719             :         }
    3720        7305 :         byteBuffer = bitstream[bit_pos--];
    3721        7305 :         nbits += 1;
    3722             : 
    3723        7305 :         if ( byteBuffer & 1 )
    3724             :         {
    3725             :             /* decode GR min removed */
    3726        2433 :             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       33812 :             for ( j = 0; j < coding_subbands; j++ )
    3728             :             {
    3729       31379 :                 if ( no_cv_vec[j] > 1 )
    3730             :                 {
    3731       31073 :                     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         306 :                     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        4872 :             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       30514 :             for ( j = 0; j < coding_subbands; j++ )
    3745             :             {
    3746       25642 :                 if ( no_cv_vec[j] > 1 )
    3747             :                 {
    3748       14359 :                     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       11283 :                     q_direction->coherence_band_data[j].spread_coherence[0] = 0;
    3753             :                 }
    3754             :             }
    3755             :         }
    3756             :     }
    3757             :     else
    3758             :     {
    3759      564350 :         for ( j = 0; j < coding_subbands; j++ )
    3760             :         {
    3761      495594 :             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      495594 :                 no_cv_vec[j] = len_cb_dct0_masa[q_direction->band_data[j].energy_ratio_index[0]]; /* spread coherence DCT0*/
    3769             :             }
    3770             :         }
    3771             : 
    3772       68756 :         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         939 :             no_cb = 1;
    3777             : #ifdef DEBUGGING
    3778             :             assert( coding_subbands % 2 == 0 );
    3779             : #endif
    3780       12207 :             for ( j = 0; j < coding_subbands / 2; j++ )
    3781             :             {
    3782       11268 :                 no_cb *= no_cv_vec[j];
    3783             :             }
    3784             : 
    3785         939 :             no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3786         939 :             no_cb = 1;
    3787             : 
    3788       12207 :             for ( j = coding_subbands / 2; j < coding_subbands; j++ )
    3789             :             {
    3790       11268 :                 no_cb *= no_cv_vec[j];
    3791             :             }
    3792             : 
    3793         939 :             no_bits_vec1 = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3794         939 :             dct0_index = 0;
    3795             : 
    3796       25780 :             for ( j = 0; j < no_bits_vec; j++ )
    3797             :             {
    3798       24841 :                 dct0_index = ( dct0_index << 1 ) + bitstream[bit_pos--];
    3799             :             }
    3800             : 
    3801         939 :             nbits += no_bits_vec;
    3802         939 :             set_s( (int16_t *) spr_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
    3803             : 
    3804         939 :             decode_combined_index( dct0_index, no_cv_vec, spr_coh_temp_index, coding_subbands / 2 );
    3805             : 
    3806         939 :             dct0_index = 0;
    3807       23325 :             for ( j = 0; j < no_bits_vec1; j++ )
    3808             :             {
    3809       22386 :                 dct0_index = ( dct0_index << 1 ) + bitstream[bit_pos--];
    3810             :             }
    3811             : 
    3812         939 :             nbits += no_bits_vec1;
    3813             : 
    3814         939 :             decode_combined_index( dct0_index, &no_cv_vec[coding_subbands / 2], &spr_coh_temp_index[coding_subbands / 2], coding_subbands / 2 );
    3815             : 
    3816       23475 :             for ( j = 0; j < coding_subbands; j++ )
    3817             :             {
    3818       22536 :                 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       67817 :             no_cb = 1;
    3827             : 
    3828      540875 :             for ( j = 0; j < coding_subbands; j++ )
    3829             :             {
    3830      473058 :                 no_cb *= no_cv_vec[j];
    3831             :             }
    3832             : 
    3833       67817 :             no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3834             : 
    3835             :             /* read joint index for DCT0 */
    3836       67817 :             no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3837       67817 :             dct0_index = 0;
    3838             : 
    3839      893333 :             for ( j = 0; j < no_bits_vec; j++ )
    3840             :             {
    3841      825516 :                 dct0_index = ( dct0_index << 1 ) + bitstream[bit_pos--];
    3842             :             }
    3843             : 
    3844       67817 :             nbits += no_bits_vec;
    3845             : 
    3846       67817 :             set_s( (int16_t *) spr_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
    3847             : 
    3848       67817 :             decode_combined_index( dct0_index, no_cv_vec, spr_coh_temp_index, coding_subbands );
    3849             : 
    3850      540875 :             for ( j = 0; j < coding_subbands; j++ )
    3851             :             {
    3852      473058 :                 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      564350 :         for ( j = 0; j < coding_subbands; j++ )
    3858             :         {
    3859      495594 :             bits_GR = bit_pos;
    3860      495594 :             idx_dct1[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, 2 * MASA_NO_CV_COH1, 0 );
    3861      495594 :             nbits += bits_GR - bit_pos;
    3862             :         }
    3863       68756 :         bits_GR = bit_pos; /* just to store the data */
    3864             : 
    3865             :         /* read average index */
    3866       68756 :         read_huf( &bit_pos, bitstream, &av_index, bit_pos, MASA_NO_CV_COH1, huff_code_av_masa, 10 ); /* 10 is MAX_LEN*/
    3867       68756 :         nbits += ( bits_GR - bit_pos );
    3868             : 
    3869             :         /* write indexes in metadata structure  */
    3870      564350 :         for ( j = 0; j < coding_subbands; j++ )
    3871             :         {
    3872      495594 :             if ( idx_dct1[j] % 2 )
    3873             :             {
    3874        7021 :                 q_direction->coherence_band_data[j].spread_coherence_dct1_index = ( idx_dct1[j] + 1 ) / 2 + av_index;
    3875             :             }
    3876             :             else
    3877             :             {
    3878      488573 :                 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       76061 :     nbits = *p_bit_pos - bit_pos;
    3886             : 
    3887       76061 :     *p_bit_pos = bit_pos;
    3888             : 
    3889       76061 :     return nbits;
    3890             : }
    3891             : 
    3892             : 
    3893             : /*-------------------------------------------------------------------*
    3894             :  * read_surround_coherence()
    3895             :  *
    3896             :  *
    3897             :  *-------------------------------------------------------------------*/
    3898             : 
    3899       56674 : 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       56674 :     coding_subbands = hQMetaData->q_direction[0].cfg.nbands;
    3918       56674 :     q_direction = hQMetaData->q_direction;
    3919             : 
    3920       56674 :     bits_sur_coherence = 0;
    3921       56674 :     bit_pos = *p_bit_pos;
    3922             : 
    3923       56674 :     d = 0;
    3924      546976 :     for ( j = 0; j < coding_subbands; j++ )
    3925             :     {
    3926      490302 :         error_ratio_surr = 1.0f;
    3927             : 
    3928      490302 :         if ( hQMetaData->no_directions == 2 )
    3929             :         {
    3930      155498 :             d += hQMetaData->twoDirBands[j];
    3931      155498 :             idx = max( d - 1, 0 );
    3932      155498 :             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      334804 :             error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[0];
    3937             :         }
    3938             : 
    3939      490302 :         if ( error_ratio_surr <= 0 )
    3940             :         {
    3941        1137 :             error_ratio_surr = 0;
    3942        1137 :             no_cv_vec[j] = 1;
    3943        1137 :             idx_ER[j] = masa_sq( 0.0f, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
    3944             :         }
    3945             :         else
    3946             :         {
    3947      489165 :             idx_ER[j] = masa_sq( error_ratio_surr, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
    3948      489165 :             no_cv_vec[j] = idx_cb_sur_coh_masa[idx_ER[j]] + 2;
    3949             :         }
    3950             :     }
    3951             : 
    3952       56674 :     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       56674 :     byteBuffer = bitstream[bit_pos--];
    3971       56674 :     bits_sur_coherence += 1;
    3972             : 
    3973       56674 :     if ( byteBuffer & 1 )
    3974             :     {
    3975             :         /* GR decoding */
    3976             :         /* read GR order */
    3977       49770 :         byteBuffer = bitstream[bit_pos--];
    3978       49770 :         bits_sur_coherence += 1;
    3979             : 
    3980             :         /* read min index */
    3981       49770 :         bits_GR = bit_pos;
    3982       49770 :         min_index = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, MASA_MAX_NO_CV_SUR_COH, 0 );
    3983       49770 :         bits_sur_coherence += bits_GR - bit_pos;
    3984             : 
    3985             :         /* read GR data */
    3986      500022 :         for ( j = 0; j < coding_subbands; j++ )
    3987             :         {
    3988      450252 :             bits_GR = bit_pos;
    3989             :             /* decoding for min removed */
    3990      450252 :             if ( no_cv_vec[j] > 1 )
    3991             :             {
    3992      449273 :                 idx_sur_coh[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_cv_vec[j] - min_index, ( byteBuffer & 1 ) );
    3993      449273 :                 bits_sur_coherence += bits_GR - bit_pos;
    3994             :             }
    3995             :             else
    3996             :             {
    3997         979 :                 idx_sur_coh[j] = 0;
    3998             :             }
    3999             :         }
    4000             : 
    4001      500022 :         for ( j = 0; j < coding_subbands; j++ )
    4002             :         {
    4003      450252 :             if ( no_cv_vec[j] > 1 )
    4004             :             {
    4005      449273 :                 hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j] + min_index;
    4006             :             }
    4007             :             else
    4008             :             {
    4009         979 :                 hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j];
    4010             :             }
    4011             : 
    4012      450252 :             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        6904 :         set_s( (int16_t *) sur_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
    4020             : 
    4021        6904 :         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       46954 :         for ( j = 0; j < coding_subbands; j++ )
    4024             :         {
    4025       40050 :             hQMetaData->surcoh_band_data[j].sur_coherence_index = sur_coh_temp_index[j];
    4026             :         }
    4027             : 
    4028             :         /* deindex surround coherence */
    4029       46954 :         for ( j = 0; j < coding_subbands; j++ )
    4030             :         {
    4031       40050 :             if ( no_cv_vec[j] > 1 )
    4032             :             {
    4033       30710 :                 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        9340 :                 hQMetaData->surcoh_band_data[j].surround_coherence[0] = 0;
    4038             :             }
    4039             :         }
    4040             :     }
    4041             : 
    4042      546976 :     for ( j = 0; j < coding_subbands; j++ )
    4043             :     {
    4044     1961208 :         for ( k = 1; k < MAX_PARAM_SPATIAL_SUBFRAMES; k++ )
    4045             :         {
    4046     1470906 :             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       56674 :     bits_sur_coherence = *p_bit_pos - bit_pos;
    4052       56674 :     *p_bit_pos = bit_pos;
    4053             : 
    4054       56674 :     return bits_sur_coherence;
    4055             : }
    4056             : 
    4057             : 
    4058             : /*-------------------------------------------------------------------*
    4059             :  * read_surround_coherence_hr()
    4060             :  *
    4061             :  *
    4062             :  *-------------------------------------------------------------------*/
    4063             : 
    4064        2289 : 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        2289 :     coding_subbands = hQMetaData->q_direction[0].cfg.nbands;
    4085        2289 :     q_direction = hQMetaData->q_direction;
    4086             : 
    4087        2289 :     bits_sur_coherence = 0;
    4088        2289 :     bit_pos = *p_bit_pos;
    4089             : 
    4090       11022 :     for ( sf = 0; sf < hQMetaData->q_direction[0].cfg.nblocks; sf++ )
    4091             :     {
    4092        8733 :         d = 0;
    4093      217608 :         for ( j = 0; j < coding_subbands; j++ )
    4094             :         {
    4095      208875 :             error_ratio_surr = 1.0f;
    4096      208875 :             if ( hQMetaData->no_directions == 2 )
    4097             :             {
    4098      112875 :                 d += hQMetaData->twoDirBands[j];
    4099      112875 :                 idx = max( d - 1, 0 );
    4100      112875 :                 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       96000 :                 error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[sf];
    4105             :             }
    4106             : 
    4107      208875 :             int_error_ratio_surr = (int32_t) ( MASA_SUR_COH_PRECISION * error_ratio_surr );
    4108      208875 :             error_ratio_surr = (float) ( int_error_ratio_surr * MASA_SUR_COH_THRESHOLD );
    4109             : 
    4110      208875 :             if ( error_ratio_surr <= 0 )
    4111             :             {
    4112        1736 :                 error_ratio_surr = 0;
    4113        1736 :                 no_cv_vec[j] = 1;
    4114        1736 :                 idx_ER[j] = 0;
    4115             :             }
    4116             :             else
    4117             :             {
    4118      207139 :                 idx_ER[j] = 7; /* masa_sq( error_ratio_surr, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS ); */
    4119      207139 :                 no_cv_vec[j] = idx_cb_sur_coh_masa[idx_ER[j]] + 2;
    4120             :             }
    4121             :         }
    4122             : 
    4123        8733 :         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        8733 :             byteBuffer = bitstream[bit_pos--];
    4141        8733 :             bits_sur_coherence += 1;
    4142             : 
    4143        8733 :             if ( byteBuffer & 1 )
    4144             :             {
    4145             :                 /* GR decoding */
    4146             :                 /* read GR order */
    4147        8557 :                 byteBuffer = bitstream[bit_pos--];
    4148        8557 :                 bits_sur_coherence += 1;
    4149             : 
    4150             :                 /* read min index */
    4151        8557 :                 bits_GR = bit_pos;
    4152        8557 :                 min_index = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, MASA_MAX_NO_CV_SUR_COH, 0 );
    4153        8557 :                 bits_sur_coherence += bits_GR - bit_pos;
    4154             : 
    4155             :                 /* read GR data */
    4156      213258 :                 for ( j = 0; j < coding_subbands; j++ )
    4157             :                 {
    4158      204701 :                     bits_GR = bit_pos;
    4159             :                     /* decoding for min removed */
    4160      204701 :                     if ( no_cv_vec[j] > 1 )
    4161             :                     {
    4162      202989 :                         idx_sur_coh[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_cv_vec[j] - min_index, ( byteBuffer & 1 ) );
    4163      202989 :                         bits_sur_coherence += bits_GR - bit_pos;
    4164             :                     }
    4165             :                     else
    4166             :                     {
    4167        1712 :                         idx_sur_coh[j] = 0;
    4168             :                     }
    4169             :                 }
    4170             : 
    4171      213258 :                 for ( j = 0; j < coding_subbands; j++ )
    4172             :                 {
    4173      204701 :                     if ( no_cv_vec[j] > 1 )
    4174             :                     {
    4175      202989 :                         hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j] + min_index;
    4176      202989 :                         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        1712 :                         hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j];
    4181        1712 :                         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         176 :                 set_s( (int16_t *) sur_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
    4190             : 
    4191         176 :                 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        4350 :                 for ( j = 0; j < coding_subbands; j++ )
    4194             :                 {
    4195        4174 :                     hQMetaData->surcoh_band_data[j].sur_coherence_index = sur_coh_temp_index[j];
    4196             :                 }
    4197             : 
    4198             :                 /* deindex surround coherence */
    4199        4350 :                 for ( j = 0; j < coding_subbands; j++ )
    4200             :                 {
    4201        4174 :                     if ( no_cv_vec[j] > 1 )
    4202             :                     {
    4203        4079 :                         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          95 :                         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        2289 :     bits_sur_coherence = *p_bit_pos - bit_pos;
    4216        2289 :     *p_bit_pos = bit_pos;
    4217             : 
    4218        2289 :     return bits_sur_coherence;
    4219             : }
    4220             : 
    4221             : 
    4222             : /*-------------------------------------------------------------------*
    4223             :  * decode_combined_index()
    4224             :  *
    4225             :  * Decode combined index into several indexes
    4226             :  *-------------------------------------------------------------------*/
    4227             : 
    4228       81914 : 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       81914 :     base[0] = 1;
    4239      565444 :     for ( i = 1; i < len; i++ )
    4240             :     {
    4241      483530 :         base[i] = base[i - 1] * no_cv_vec[i - 1];
    4242             :     }
    4243             : 
    4244      565444 :     for ( i = len - 1; i > 0; i-- )
    4245             :     {
    4246      483530 :         index[i] = (uint16_t) ( comb_index / base[i] );
    4247      483530 :         comb_index -= index[i] * base[i];
    4248             :     }
    4249             : 
    4250       81914 :     index[0] = (uint16_t) comb_index;
    4251             : 
    4252       81914 :     return;
    4253             : }
    4254             : 
    4255             : 
    4256        6834 : 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        6834 :     step_fx = STEP_M2T_FX;
    4269        6834 :     sign = 1;
    4270        6834 :     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        6834 :     set_s( q_idx, 0, len_stream );
    4281             :     /* read DCT 0 component */
    4282       47838 :     for ( i = 0; i < BITS_MASA2TOTTAL_DCT0; i++ )
    4283             :     {
    4284       41004 :         q_idx[0] = ( q_idx[0] << 1 ) + bit_stream[( *index )--];
    4285             :     }
    4286        6834 :     q_idx[0] *= sign;
    4287             : 
    4288        6834 :     if ( q_idx[0] != 0 )
    4289             :     {
    4290        6834 :         if ( len_stream >= 8 )
    4291             :         {
    4292             :             /* read index of last index encoded with GR2 */
    4293        6590 :             i_min = 0;
    4294        6590 :             j = 4;
    4295       32950 :             for ( i = 0; i < j; i++ )
    4296             :             {
    4297       26360 :                 i_min = ( i_min << 1 ) + bit_stream[( *index )--];
    4298             :             }
    4299             : 
    4300             :             /* read GR orders */
    4301        6590 :             GR1 = bit_stream[( *index )--] + 1;
    4302        6590 :             if ( GR1 == 2 )
    4303             :             {
    4304        5203 :                 GR2 = bit_stream[( *index )--];
    4305             :             }
    4306             :             else
    4307             :             {
    4308        1387 :                 GR2 = 0;
    4309             :             }
    4310             : 
    4311             :             /* read GR data */
    4312       37066 :             for ( i = 1; i <= i_min; i++ )
    4313             :             {
    4314       30476 :                 q_idx[i] = ivas_qmetadata_DecodeExtendedGR( bit_stream, index, 100, GR1 );
    4315             :             }
    4316       98444 :             for ( i = i_min + 1; i < len_stream; i++ )
    4317             :             {
    4318       91854 :                 q_idx[i] = ivas_qmetadata_DecodeExtendedGR( bit_stream, index, 100, GR2 );
    4319             :             }
    4320             :         }
    4321             :         else
    4322             :         {
    4323             :             /* read GR order (only one)  */
    4324         244 :             GR1 = bit_stream[( *index )--];
    4325        1197 :             for ( i = 1; i < len_stream; i++ )
    4326             :             {
    4327         953 :                 q_idx[i] = ivas_qmetadata_DecodeExtendedGR( bit_stream, index, 100, GR1 );
    4328             :             }
    4329             :         }
    4330             :     }
    4331             : 
    4332             :     /* deindex */
    4333        6834 :     q_dct_data_fx[0] = (Word32) ( ( step_fx * q_idx[0] ) >> 6 ); /* Q25 */
    4334      130117 :     for ( i = 1; i < len_stream; i++ )
    4335             :     {
    4336      123283 :         if ( ( q_idx[i] & 1 ) == 0 )
    4337             :         {
    4338       86769 :             q_dct_data_fx[i] = (Word32) ( ( step_fx * ( -q_idx[i] ) ) >> 7 ); /* Q25 */
    4339             :         }
    4340             :         ELSE
    4341             :         {
    4342       36514 :             q_dct_data_fx[i] = (Word32) ( ( step_fx * ( q_idx[i] + 1 ) ) >> 7 ); /* Q25 */
    4343             :         }
    4344             :     }
    4345        6834 :     return;
    4346             : }
    4347             : 
    4348             : 
    4349             : /*-------------------------------------------------------------------------
    4350             :  * ivas_omasa_decode_masa_to_total()
    4351             :  *
    4352             :  *------------------------------------------------------------------------*/
    4353             : 
    4354        6834 : 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        6834 :     n_streams = 1;
    4369        6834 :     len_stream = nbands * nblocks;
    4370        6834 :     if ( len_stream == 32 )
    4371             :     {
    4372           0 :         n_streams = 4;
    4373           0 :         len_stream = 8;
    4374             :     }
    4375             : 
    4376        6834 :     set_s( q_idx, 0, nbands * nblocks );
    4377       13668 :     for ( i = 0; i < n_streams; i++ )
    4378             :     {
    4379        6834 :         read_stream_dct_coeffs_omasa( &q_idx[i * len_stream],
    4380        6834 :                                       &q_dct_data_fx[i * len_stream],
    4381             :                                       len_stream, bit_stream, index, i == 0 );
    4382             :     }
    4383             : 
    4384             :     /* inverse DCT2 transform */
    4385        6834 :     SWITCH( len_stream )
    4386             :     {
    4387          23 :         case 4:
    4388          23 :             matrix_product_q30_fx( dct4_fx, nblocks, nblocks, 1, q_dct_data_fx, nblocks, 1, 0, dct_data_tmp_fx );
    4389          23 :             mvl2l( dct_data_tmp_fx, q_dct_data_fx, nblocks ); /*Q30*/
    4390          23 :             BREAK;
    4391         221 :         case 5:
    4392         221 :             matrix_product_q30_fx( dct5_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
    4393         221 :             mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
    4394         221 :             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         360 :         case 12:
    4400         360 :             matrix_product_q30_fx( dct12_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
    4401         360 :             mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
    4402         360 :             BREAK;
    4403        6230 :         case 20:
    4404        6230 :             matrix_product_fx( dct5_fx, nbands, nbands, 1, q_dct_data_fx, nbands, nblocks, 0, dct_data_tmp_fx );
    4405        6230 :             matrix_product_q30_fx( dct_data_tmp_fx, nbands, nblocks, 0, dct4_fx, nblocks, nblocks, 0, q_dct_data_fx ); /* reuse of variable*/
    4406        6230 :             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      136951 :     for ( i = 0; i < nblocks * nbands; i++ )
    4421             :     {
    4422      130117 :         if ( q_dct_data_fx[i] >= 1052266987 && q_dct_data_fx[i] < 1052400000 )
    4423             :         {
    4424         201 :             q_dct_data_fx[i] = 1052400000;
    4425             :         }
    4426             :     }
    4427             : 
    4428        6834 :     k = 0;
    4429       32427 :     for ( i = 0; i < nblocks; i++ )
    4430             :     {
    4431      155710 :         for ( j = 0; j < nbands; j++ )
    4432             :         {
    4433      130117 :             masa_to_total_energy_ratio[i][j] = q_dct_data_fx[k] / (float) ( 1 << 30 );
    4434      130117 :             masa_to_total_energy_ratio[i][j] = max( 0.0f, masa_to_total_energy_ratio[i][j] );
    4435      130117 :             masa_to_total_energy_ratio[i][j] = min( 1.0f, masa_to_total_energy_ratio[i][j] );
    4436      130117 :             k++;
    4437             :         }
    4438             :     }
    4439             : 
    4440        6834 :     if ( nblocks == 1 )
    4441             :     {
    4442        2324 :         for ( i = 1; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
    4443             :         {
    4444       18018 :             for ( j = 0; j < nbands; j++ )
    4445             :             {
    4446       16275 :                 masa_to_total_energy_ratio[i][j] = masa_to_total_energy_ratio[0][j];
    4447             :             }
    4448             :         }
    4449             :     }
    4450             : 
    4451        6834 :     if ( nbands == 1 )
    4452             :     {
    4453         115 :         for ( j = 1; j < 5; j++ )
    4454             :         {
    4455         460 :             for ( i = 0; i < nblocks; i++ )
    4456             :             {
    4457         368 :                 masa_to_total_energy_ratio[i][j] = masa_to_total_energy_ratio[i][0];
    4458             :             }
    4459             :         }
    4460             :     }
    4461             : 
    4462        6834 :     return;
    4463             : }

Generated by: LCOV version 1.14