LCOV - code coverage report
Current view: top level - lib_dec - ivas_qmetadata_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 1384 1498 92.4 %
Date: 2025-05-23 08:37:30 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      584613 : 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      584613 :     ec_flag = 0;
     178      584613 :     start_index_0 = *index;
     179             : 
     180             :     /*Coherence flag decoding*/
     181      584613 :     bits_no_dirs_coh = 0;
     182      584613 :     all_coherence_zero = 1;
     183      584613 :     if ( hQMetaData->coherence_flag )
     184             :     {
     185             :         /* read if coherence is zero */
     186       83028 :         all_coherence_zero = bitstream[( *index )--];
     187       83028 :         bits_no_dirs_coh += 1;
     188             :     }
     189             : 
     190      584613 :     hQMetaData->all_coherence_zero = (uint8_t) all_coherence_zero;
     191             : 
     192      584613 :     if ( hQMetaData->no_directions == 2 )
     193             :     {
     194             :         /* Read which bands have 2 directions */
     195       62238 :         hQMetaData->q_direction[1].cfg.nbands = hQMetaData->numTwoDirBands;
     196       62238 :         set_c( (int8_t *) hQMetaData->twoDirBands, 0, hQMetaData->q_direction[0].cfg.nbands );
     197       62238 :         d = *index;
     198       62238 :         dif_p[0] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 );
     199       62238 :         p[0] = dif_p[0];
     200       62238 :         hQMetaData->twoDirBands[p[0]] = 1;
     201      574377 :         for ( b = 1; b < hQMetaData->numTwoDirBands; b++ )
     202             :         {
     203      512139 :             dif_p[b] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 );
     204      512139 :             p[b] = p[b - 1] + dif_p[b] + 1;
     205      512139 :             hQMetaData->twoDirBands[p[b]] = 1;
     206             :         }
     207       62238 :         bits_no_dirs_coh += ( d - *index );
     208             :     }
     209             : 
     210      584613 :     bits_diff_sum = 0;
     211      584613 :     bits_diff_sum += ivas_qmetadata_entropy_decode_diffuseness( bitstream, index, &( hQMetaData->q_direction[0] ), &diffuseness_index_max_ec_frame_pre[0] );
     212             : 
     213      584613 :     if ( hodirac_flag )
     214             :     {
     215       48663 :         if ( hQMetaData->no_directions == 2 )
     216             :         {
     217             :             /* Calculate bits for dfRatio */
     218       48663 :             dir2band = 0;
     219      583956 :             for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     220             :             {
     221      535293 :                 if ( hQMetaData->twoDirBands[b] == 1 )
     222             :                 {
     223      535293 :                     dfRatio_bits[dir2band] = ivas_get_df_ratio_bits_hodirac( hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0] );
     224      535293 :                     dir2band++;
     225             :                 }
     226             :             }
     227             : 
     228       48663 :             bits_diff_sum += ivas_qmetadata_entropy_decode_df_ratio( bitstream, index, &( hQMetaData->q_direction[1] ), dfRatio_bits );
     229             :         }
     230             :     }
     231             :     else
     232             :     {
     233      535950 :         if ( hQMetaData->no_directions == 2 )
     234             :         {
     235             :             /* Calculate bits for dfRatio */
     236       13575 :             dir2band = 0;
     237      130503 :             for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     238             :             {
     239      116928 :                 if ( hQMetaData->twoDirBands[b] == 1 )
     240             :                 {
     241       39084 :                     dfRatio_bits[dir2band] = ivas_get_df_ratio_bits( hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0] );
     242       39084 :                     dir2band++;
     243             :                 }
     244             :             }
     245             : 
     246       13575 :             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      584613 :     if ( hQMetaData->no_directions == 2 )
     252             :     {
     253       62238 :         dir2band = 0;
     254      714459 :         for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     255             :         {
     256      652221 :             if ( hQMetaData->twoDirBands[b] == 1 )
     257             :             {
     258             :                 float diffRatio, dfRatio, dir1ratio, dir2ratio;
     259             :                 int16_t dfRatio_qsteps;
     260             : 
     261      574377 :                 diffRatio = diffuseness_reconstructions[hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0]];
     262      574377 :                 dfRatio_qsteps = 1 << dfRatio_bits[dir2band];
     263             :                 /* already encoded as total and ratios in HO-DirAC */
     264      574377 :                 if ( hodirac_flag )
     265             :                 {
     266      535293 :                     dfRatio = usdequant( hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0], 0.0f, 1.f / ( dfRatio_qsteps - 1 ) );
     267      535293 :                     dir1ratio = 1.f - diffRatio;
     268      535293 :                     dir2ratio = dfRatio;
     269             :                 }
     270             :                 else
     271             :                 {
     272       39084 :                     dfRatio = usdequant( hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0], 0.5f, 0.5f / ( dfRatio_qsteps - 1 ) );
     273             : 
     274       39084 :                     dir1ratio = dfRatio * ( 1.0f - diffRatio );
     275       39084 :                     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      574377 :                 hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0] = masa_sq( 1.0f - dir1ratio, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
     280      574377 :                 if ( hodirac_flag )
     281             :                 {
     282             :                     float tmp;
     283      535293 :                     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       39084 :                     hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0] = masa_sq( 1.0f - dir2ratio, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
     288             :                 }
     289             : 
     290     2845128 :                 for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     291             :                 {
     292     2270751 :                     hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = dir1ratio;
     293     2270751 :                     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     2845128 :                 for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
     297             :                 {
     298     2270751 :                     hQMetaData->q_direction[1].band_data[dir2band].energy_ratio[m] = dir2ratio;
     299     2270751 :                     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      574377 :                 dir2band++;
     303             :             }
     304             :             else
     305             :             {
     306             :                 /* 1dir band */
     307       77844 :                 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      215769 :                 for ( m = 1; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     309             :                 {
     310      137925 :                     hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio[0];
     311      137925 :                     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     2447796 :         for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     320             :         {
     321     1925421 :             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     6221940 :             for ( m = 1; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     323             :             {
     324     4296519 :                 hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio[0];
     325     4296519 :                 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      584613 :     bits_dir_raw_pre[0] = 0;
     333      584613 :     bits_dir_raw_pre[1] = 0;
     334      584613 :     dir2band = 0;
     335     3162255 :     for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     336             :     {
     337     2577642 :         if ( hQMetaData->no_directions == 2 && hQMetaData->twoDirBands[b] == 1 )
     338      574377 :         {
     339             :             int16_t index_dirRatio1Inv, index_dirRatio2Inv, index_dirRatio1Inv_mod, index_dirRatio2Inv_mod;
     340             : 
     341      574377 :             index_dirRatio1Inv = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
     342      574377 :             index_dirRatio2Inv = hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0];
     343             : 
     344      574377 :             masa_compensate_two_dir_energy_ratio_index( index_dirRatio1Inv, index_dirRatio2Inv, &index_dirRatio1Inv_mod, &index_dirRatio2Inv_mod, hodirac_flag );
     345             : 
     346     2845128 :             for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     347             :             {
     348     2270751 :                 hQMetaData->q_direction[0].band_data[b].energy_ratio_index_mod[m] = index_dirRatio1Inv_mod;
     349     2270751 :                 hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m] = bits_direction_masa[index_dirRatio1Inv_mod];
     350     2270751 :                 bits_dir_raw_pre[0] += hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m];
     351             :             }
     352             : 
     353     2845128 :             for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
     354             :             {
     355     2270751 :                 hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index_mod[m] = index_dirRatio2Inv_mod;
     356     2270751 :                 hQMetaData->q_direction[1].band_data[dir2band].bits_sph_idx[m] = bits_direction_masa[index_dirRatio2Inv_mod];
     357     2270751 :                 bits_dir_raw_pre[1] += hQMetaData->q_direction[1].band_data[dir2band].bits_sph_idx[m];
     358             :             }
     359             : 
     360      574377 :             dir2band++;
     361             :         }
     362             :         else
     363             :         {
     364     8440974 :             for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     365             :             {
     366     6437709 :                 hQMetaData->q_direction[0].band_data[b].energy_ratio_index_mod[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
     367     6437709 :                 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     6437709 :                 bits_dir_raw_pre[0] += hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m];
     369             :             }
     370             :         }
     371             :     }
     372             : 
     373      584613 :     if ( hQMetaData->no_directions == 2 )
     374             :     {
     375       62238 :         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       62238 :         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       13575 :             bits_sur_coherence = read_surround_coherence( bitstream, index, hQMetaData );
     379             :         }
     380             :         else
     381             :         {
     382       48663 :             bits_sur_coherence = 0;
     383             :             /*Surround coherence*/
     384      583956 :             for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     385             :             {
     386      535293 :                 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       62238 :         bits_no_dirs_coh += bits_sur_coherence;
     393       62238 :         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      522375 :         no_TF = hQMetaData->q_direction[0].cfg.nbands * hQMetaData->q_direction[0].cfg.nblocks;
     398      522375 :         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       65511 :             bits_sur_coherence = read_surround_coherence( bitstream, index, hQMetaData );
     401             :         }
     402             :         else
     403             :         {
     404      456864 :             bits_sur_coherence = 0;
     405             :             /*Surround coherence*/
     406     1919379 :             for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     407             :             {
     408     1462515 :                 if ( hQMetaData->surcoh_band_data != NULL )
     409             :                 {
     410       43857 :                     set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
     411             :                 }
     412             :             }
     413             :         }
     414      522375 :         bits_no_dirs_coh += bits_sur_coherence;
     415             : 
     416      522375 :         total_bits_1dir = hQMetaData->metadata_max_bits - bits_no_dirs_coh;
     417             :     }
     418             : 
     419      584613 :     bits_dir_target = 0;
     420      584613 :     bits_dir_used = 0;
     421             : 
     422     1231464 :     for ( d = 0; d < hQMetaData->no_directions; d++ )
     423             :     {
     424      646851 :         q_direction = &hQMetaData->q_direction[d];
     425      646851 :         nbands = q_direction->cfg.nbands;
     426      646851 :         nblocks = q_direction->cfg.nblocks;
     427      646851 :         start_band = q_direction->cfg.start_band;
     428             : 
     429      646851 :         diffuseness_index_max_ec_frame = diffuseness_index_max_ec_frame_pre[0];
     430      646851 :         if ( d == 0 )
     431             :         {
     432      584613 :             bits_diff = bits_diff_sum;
     433             :         }
     434             :         else
     435             :         {
     436       62238 :             bits_diff = 0;
     437             :         }
     438      646851 :         bits_dir_raw = bits_dir_raw_pre[d];
     439             : 
     440             :         /* Read coherence, if any */
     441      646851 :         bits_coherence = 0;
     442             : 
     443      646851 :         if ( all_coherence_zero == 0 )
     444             :         {
     445       93156 :             bits_coherence = read_coherence_data( bitstream, index, hQMetaData, d, 0 );
     446             :         }
     447             :         else
     448             :         {
     449             :             /*Surround coherence*/
     450     3084321 :             for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     451             :             {
     452     2530626 :                 if ( hQMetaData->surcoh_band_data != NULL )
     453             :                 {
     454       41382 :                     set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
     455             :                 }
     456             : 
     457     2530626 :                 if ( hQMetaData->q_direction[d].coherence_band_data != NULL )
     458             :                 {
     459       41382 :                     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      646851 :         q_direction->not_in_2D = bitstream[( *index )--];
     466      646851 :         signal_bits = 1;
     467             : 
     468             :         /* Read EC signaling */
     469      646851 :         ec_flag = 0;
     470      646851 :         if ( total_bits_1dir + bits_sur_coherence <= hQMetaData->qmetadata_max_bit_req )
     471             :         {
     472      446655 :             ec_flag = bitstream[( *index )--];
     473      446655 :             signal_bits++;
     474      446655 :             if ( nblocks > 1 )
     475             :             {
     476      336216 :                 if ( ec_flag )
     477             :                 {
     478       22470 :                     ec_flag += bitstream[( *index )--];
     479       22470 :                     signal_bits++;
     480             :                 }
     481             :             }
     482             :         }
     483             : 
     484             :         /* Decode quantized directions frame-wise */
     485      646851 :         if ( ec_flag == 0 ) /* EC 1*/
     486             :         {
     487      614910 :             bits_dir = 0;
     488      614910 :             raw_flag[0] = bitstream[( *index )--];
     489      614910 :             bits_dir++;
     490             : 
     491      614910 :             if ( raw_flag[0] == 0 )
     492             :             {
     493      333723 :                 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      281187 :                 bits_dir += ivas_qmetadata_raw_decode_dir( q_direction, bitstream, index, nbands, start_band, 0 );
     498             :             }
     499             :         }
     500             :         /* Decode quantized directions band-wise */
     501       31941 :         else if ( ec_flag == 1 && ( nblocks > 1 ) ) /* EC2 */
     502             :         {
     503        5601 :             bits_dir = 0;
     504       28191 :             for ( b = start_band; b < nbands; b++ )
     505             :             {
     506       22590 :                 raw_flag[b] = bitstream[( *index )--];
     507       22590 :                 bits_dir++;
     508             :             }
     509             : 
     510             :             /* Read EC bits*/
     511        5601 :             diff_bits = bits_diff + bits_coherence + signal_bits - total_bits_1dir;
     512             : 
     513       28191 :             for ( b = start_band; b < nbands; b++ )
     514             :             {
     515       22590 :                 if ( raw_flag[b] == 0 )
     516             :                 {
     517        8508 :                     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       14082 :                     diff_bits += q_direction->band_data[b].bits_sph_idx[0] * q_direction->cfg.nblocks;
     522             :                 }
     523             :             }
     524        5601 :             diff_bits += bits_dir;
     525             : 
     526             :             /* Small requantization?*/
     527        5601 :             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       27354 :                 for ( i = q_direction->cfg.start_band; i < q_direction->cfg.nbands; i++ )
     534             :                 {
     535      109500 :                     for ( j = 0; j < q_direction->cfg.nblocks; j++ )
     536             :                     {
     537       87600 :                         bits_temp[i][j] = q_direction->band_data[i].bits_sph_idx[j];
     538             :                     }
     539             :                 }
     540             : 
     541        5454 :                 small_reduction_direction( q_direction, bits_temp, raw_flag, &diff_bits );
     542             : 
     543       27354 :                 for ( i = q_direction->cfg.start_band; i < q_direction->cfg.nbands; i++ )
     544             :                 {
     545      109500 :                     for ( j = 0; j < q_direction->cfg.nblocks; j++ )
     546             :                     {
     547       87600 :                         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       28191 :             for ( b = start_band; b < nbands; b++ )
     557             :             {
     558       22590 :                 if ( raw_flag[b] )
     559             :                 {
     560       14082 :                     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       26340 :             ec_flag = 2;
     569             : 
     570       26340 :             if ( hQMetaData->is_masa_ivas_format == 0 )
     571             :             {
     572        9645 :                 reduce_bits = bits_dir_raw - ( total_bits_1dir - bits_diff - bits_coherence - signal_bits );
     573        9645 :                 ind_order[0] = -1;
     574             :             }
     575             :             else
     576             :             {
     577       16695 :                 ind_order[0] = 0;
     578       16695 :                 reduce_bits = min( nbands * nblocks + MASA_BIT_REDUCT_PARAM, bits_dir_raw - ( total_bits_1dir - bits_diff - bits_coherence - signal_bits ) );
     579       16695 :                 if ( reduce_bits > bits_dir_raw - nbands * nblocks )
     580             :                 {
     581         441 :                     reduce_bits = bits_dir_raw - nbands * nblocks;
     582             :                 }
     583             :             }
     584       26340 :             only_reduce_bits_direction( &dummy, q_direction, reduce_bits, nbands, nblocks, ind_order );
     585             : 
     586             :             /* Read directions */
     587       26340 :             bits_dir = read_directions( q_direction, (uint8_t) nbands, (uint8_t) nblocks, bitstream, index, ind_order );
     588             :         }
     589             : 
     590      646851 :         if ( bits_coherence > 0 )
     591             :         {
     592       90579 :             if ( nblocks > 1 )
     593             :             {
     594       68784 :                 decode_spread_coherence( hQMetaData, d, nblocks, 0 );
     595             :             }
     596             :         }
     597             :         else
     598             :         {
     599     3098910 :             for ( b = start_band; b < nbands; b++ )
     600             :             {
     601     2542638 :                 if ( q_direction->coherence_band_data != NULL )
     602             :                 {
     603       53394 :                     set_c( (int8_t *) q_direction->coherence_band_data[b].spread_coherence, 0, nblocks );
     604             :                 }
     605             :             }
     606             :         }
     607      646851 :         if ( d == 0 )
     608             :         {
     609      584613 :             total_bits_1dir = hQMetaData->metadata_max_bits - ( start_index_0 - *index );
     610             :         }
     611             : 
     612      646851 :         bits_dir_target += bits_dir_raw;
     613      646851 :         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      584613 :     if ( hQMetaData->no_directions == 2 )
     657             :     {
     658       62238 :         d = hQMetaData->q_direction[1].cfg.nbands - 1;
     659       62238 :         nblocks = hQMetaData->q_direction[0].cfg.nblocks;
     660             : 
     661      714459 :         for ( b = hQMetaData->q_direction[0].cfg.nbands - 1; b >= 0; b-- )
     662             :         {
     663      652221 :             if ( hQMetaData->twoDirBands[b] == 1 )
     664             :             {
     665      574377 :                 mvr2r( hQMetaData->q_direction[1].band_data[d].azimuth, hQMetaData->q_direction[1].band_data[b].azimuth, nblocks );
     666      574377 :                 mvr2r( hQMetaData->q_direction[1].band_data[d].elevation, hQMetaData->q_direction[1].band_data[b].elevation, nblocks );
     667      574377 :                 mvr2r( hQMetaData->q_direction[1].band_data[d].energy_ratio, hQMetaData->q_direction[1].band_data[b].energy_ratio, nblocks );
     668             : 
     669      574377 :                 if ( hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0] < 7 )
     670             :                 {
     671     1959837 :                     for ( m = 0; m < nblocks; m++ )
     672             :                     {
     673     1562817 :                         hQMetaData->q_direction[1].band_data[b].azimuth[m] += hQMetaData->q_direction[0].band_data[b].azimuth[m] - 180;
     674     1562817 :                         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     1562817 :                         if ( hQMetaData->q_direction[1].band_data[b].azimuth[m] < -180 )
     679             :                         {
     680      137418 :                             hQMetaData->q_direction[1].band_data[b].azimuth[m] += 360;
     681             :                         }
     682             :                     }
     683             :                 }
     684             : 
     685      574377 :                 if ( hQMetaData->q_direction[1].coherence_band_data != NULL )
     686             :                 {
     687       39084 :                     mvc2c( hQMetaData->q_direction[1].coherence_band_data[d].spread_coherence, hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence, nblocks );
     688             :                 }
     689      574377 :                 d--;
     690             :             }
     691             :             else
     692             :             {
     693       77844 :                 set_f( hQMetaData->q_direction[1].band_data[b].azimuth, 0.0f, nblocks );
     694       77844 :                 set_f( hQMetaData->q_direction[1].band_data[b].elevation, 0.0f, nblocks );
     695       77844 :                 set_f( hQMetaData->q_direction[1].band_data[b].energy_ratio, 0.0f, nblocks );
     696             : 
     697       77844 :                 if ( hQMetaData->q_direction[1].coherence_band_data != NULL )
     698             :                 {
     699       77844 :                     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       62238 :         if ( !hodirac_flag )
     706             :         {
     707      130503 :             for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     708             :             {
     709             :                 float ratioSum;
     710             : 
     711      116928 :                 ratioSum = hQMetaData->q_direction[0].band_data[b].energy_ratio[0] + hQMetaData->q_direction[1].band_data[b].energy_ratio[0];
     712             : 
     713      116928 :                 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      584613 :     hQMetaData->ec_flag = ec_flag;
     724             : 
     725      584613 :     hQMetaData->dir_comp_ratio = (float) bits_dir_used / (float) bits_dir_target;
     726             : 
     727      584613 :     if ( hQMetaData->dir_comp_ratio > 1.0f )
     728             :     {
     729      224043 :         hQMetaData->dir_comp_ratio = 1.0f;
     730             :     }
     731             : 
     732      584613 :     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        6717 : 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        6717 :     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        6717 :     start_index_0 = *index;
     790             :     /* read number of higher inactive/not encoded  bands */
     791        6717 :     if ( bitstream[( *index )--] )
     792             :     {
     793         855 :         codedBands = MASA_MAXIMUM_CODING_SUBBANDS - ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 1 ) - 1;
     794             :     }
     795             :     else
     796             :     {
     797        5862 :         codedBands = MASA_MAXIMUM_CODING_SUBBANDS;
     798             :     }
     799        7572 :     for ( b = codedBands; b < ncoding_bands_config; b++ )
     800             :     {
     801        4275 :         for ( m = 0; m < MAX_PARAM_SPATIAL_SUBFRAMES; m++ )
     802             :         {
     803        3420 :             hQMetaData->q_direction[0].band_data[b].azimuth[m] = 0.0f;
     804        3420 :             hQMetaData->q_direction[0].band_data[b].elevation[m] = 0.0f;
     805        3420 :             hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = 0.0f;
     806             : 
     807        3420 :             if ( hQMetaData->coherence_flag && hQMetaData->q_direction[0].coherence_band_data != NULL )
     808             :             {
     809        3420 :                 hQMetaData->q_direction[0].coherence_band_data[b].spread_coherence[m] = 0u;
     810             :             }
     811             : 
     812        3420 :             if ( hQMetaData->no_directions == 2 )
     813             :             {
     814        3420 :                 hQMetaData->q_direction[1].band_data[b].azimuth[m] = 0.0f;
     815        3420 :                 hQMetaData->q_direction[1].band_data[b].elevation[m] = 0.0f;
     816        3420 :                 hQMetaData->q_direction[1].band_data[b].energy_ratio[m] = 0.0f;
     817             : 
     818        3420 :                 if ( hQMetaData->coherence_flag && hQMetaData->q_direction[1].coherence_band_data != NULL )
     819             :                 {
     820        3420 :                     hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence[m] = 0u;
     821             :                 }
     822             :             }
     823             : 
     824        3420 :             if ( hQMetaData->coherence_flag && hQMetaData->surcoh_band_data != NULL )
     825             :             {
     826        3420 :                 hQMetaData->surcoh_band_data[b].surround_coherence[m] = 0u;
     827             :             }
     828             :         }
     829             : 
     830         855 :         if ( hQMetaData->no_directions == 2 )
     831             :         {
     832         855 :             hQMetaData->twoDirBands[b] = 0;
     833             :         }
     834             :     }
     835        6717 :     sf_nbands0 = hQMetaData->q_direction[0].cfg.nbands;
     836             : 
     837        6717 :     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        6717 :     all_coherence_zero = 1;
     844        6717 :     if ( hQMetaData->coherence_flag )
     845             :     {
     846             :         /* read if coherence is zero */
     847        6717 :         all_coherence_zero = bitstream[( *index )--];
     848             : #ifdef DEBUG_MODE_QMETADATA
     849             :         bits_no_dirs_coh += 1;
     850             : #endif
     851             :     }
     852             : 
     853        6717 :     hQMetaData->all_coherence_zero = (uint8_t) all_coherence_zero;
     854             : 
     855        6717 :     if ( hQMetaData->no_directions == 2 )
     856             :     {
     857        3717 :         set_c( (int8_t *) hQMetaData->twoDirBands, 1, hQMetaData->q_direction[0].cfg.nbands );
     858             :     }
     859             : 
     860        6717 :     if ( bits_sph_idx == 11 && hQMetaData->no_directions == 2 )
     861             :     {
     862             :         /* Read which bands have 2 directions */
     863        2577 :         hQMetaData->q_direction[1].cfg.nbands = hQMetaData->numTwoDirBands;
     864        2577 :         sf_nbands1 = hQMetaData->q_direction[1].cfg.nbands;
     865        2577 :         if ( hQMetaData->q_direction[1].cfg.nbands > codedBands )
     866             :         {
     867           0 :             hQMetaData->q_direction[1].cfg.nbands = codedBands;
     868             :         }
     869        2577 :         set_c( (int8_t *) hQMetaData->twoDirBands, 0, hQMetaData->q_direction[0].cfg.nbands );
     870        2577 :         d = *index;
     871        2577 :         dif_p[0] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 );
     872        2577 :         p[0] = dif_p[0];
     873        2577 :         hQMetaData->twoDirBands[p[0]] = 1;
     874       24462 :         for ( b = 1; b < hQMetaData->q_direction[1].cfg.nbands; b++ )
     875             :         {
     876       21885 :             dif_p[b] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 );
     877       21885 :             p[b] = p[b - 1] + dif_p[b] + 1;
     878       21885 :             hQMetaData->twoDirBands[p[b]] = 1;
     879             :         }
     880             : #ifdef DEBUG_MODE_QMETADATA
     881             :         bits_no_dirs_coh += ( d - *index );
     882             : #endif
     883             :     }
     884             : 
     885        6717 :     if ( bits_sph_idx == 16 && hQMetaData->no_directions == 2 )
     886             :     {
     887        1140 :         sf_nbands1 = hQMetaData->q_direction[1].cfg.nbands;
     888        1140 :         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        6717 :         ivas_qmetadata_entropy_decode_diffuseness_hr_512( bitstream, index, &( hQMetaData->q_direction[0] ) );
     897             : 
     898        6717 :     if ( hQMetaData->no_directions == 2 )
     899             :     {
     900             : #ifdef DEBUG_MODE_QMETADATA
     901             :         bits_diff_sum +=
     902             : #endif
     903        3717 :             ivas_qmetadata_entropy_decode_diffuseness_hr_512( bitstream, index, &( hQMetaData->q_direction[1] ) );
     904             :     }
     905             : 
     906             : 
     907      167070 :     for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     908             :     {
     909      772578 :         for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     910             :         {
     911      612225 :             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        6717 :     if ( hQMetaData->no_directions == 2 )
     915             :     {
     916             :         float ratioSum;
     917        3717 :         if ( bits_sph_idx == 16 )
     918             :         {
     919       28500 :             for ( b = hQMetaData->q_direction[1].cfg.start_band; b < hQMetaData->q_direction[1].cfg.nbands; b++ )
     920             :             {
     921      136800 :                 for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
     922             :                 {
     923      109440 :                     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      109440 :                     ratioSum = hQMetaData->q_direction[0].band_data[b].energy_ratio[m] + hQMetaData->q_direction[1].band_data[b].energy_ratio[m];
     927             : 
     928      109440 :                     if ( ratioSum > 1.0f )
     929             :                     {
     930        3882 :                         hQMetaData->q_direction[0].band_data[b].energy_ratio[m] /= ratioSum;
     931        3882 :                         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        2577 :             d = 0;
     940       63570 :             for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     941             :             {
     942       60993 :                 if ( hQMetaData->twoDirBands[b] == 1 )
     943             :                 {
     944       24462 :                     pos_2dir_band[d] = b;
     945       24462 :                     d++;
     946             :                 }
     947             :                 else
     948             :                 {
     949       36531 :                     pos_2dir_band[d] = 0;
     950             :                 }
     951             :             }
     952       27039 :             for ( b = hQMetaData->q_direction[1].cfg.start_band; b < hQMetaData->q_direction[1].cfg.nbands; b++ )
     953             :             {
     954      107082 :                 for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
     955             :                 {
     956       82620 :                     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       82620 :                     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       82620 :                     if ( ratioSum > 1.0f )
     961             :                     {
     962         972 :                         hQMetaData->q_direction[0].band_data[pos_2dir_band[b]].energy_ratio[m] /= ratioSum;
     963         972 :                         hQMetaData->q_direction[1].band_data[b].energy_ratio[m] /= ratioSum;
     964             :                     }
     965             :                 }
     966             :             }
     967             :         }
     968             :     }
     969             : 
     970        6717 :     if ( hQMetaData->no_directions == 2 )
     971             :     {
     972       55539 :         for ( b = hQMetaData->q_direction[1].cfg.start_band; b < hQMetaData->q_direction[1].cfg.nbands; b++ )
     973             :         {
     974      243882 :             for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
     975             :             {
     976      192060 :                 hQMetaData->q_direction[1].band_data[b].energy_ratio_index_mod[m] = hQMetaData->q_direction[1].band_data[b].energy_ratio_index[m];
     977      192060 :                 hQMetaData->q_direction[1].band_data[b].bits_sph_idx[m] = bits_sph_idx;
     978             :             }
     979             :         }
     980             :     }
     981             : 
     982      167070 :     for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
     983             :     {
     984      772578 :         for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
     985             :         {
     986      612225 :             hQMetaData->q_direction[0].band_data[b].energy_ratio_index_mod[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
     987      612225 :             hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m] = bits_sph_idx;
     988             :         }
     989             :     }
     990             : 
     991        6717 :     if ( all_coherence_zero == 0 )
     992             :     {
     993             : #ifdef DEBUG_MODE_QMETADATA
     994             :         bits_sur_coherence =
     995             : #endif
     996        6717 :             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       17151 :     for ( d = 0; d < hQMetaData->no_directions; d++ )
    1017             :     {
    1018       10434 :         q_direction = &hQMetaData->q_direction[d];
    1019       10434 :         nbands = q_direction->cfg.nbands;
    1020       10434 :         start_band = q_direction->cfg.start_band;
    1021             : 
    1022             :         /* Read coherence, if any */
    1023       10434 :         if ( all_coherence_zero == 0 )
    1024             :         {
    1025       10434 :             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       10434 :         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        6717 :     if ( hQMetaData->no_directions == 2 )
    1077             :     {
    1078             :         /* move 2 dir data to its correct subband */
    1079        3717 :         if ( bits_sph_idx == 11 )
    1080             :         {
    1081             :             int16_t nblocks;
    1082        2577 :             d = hQMetaData->q_direction[1].cfg.nbands - 1;
    1083        2577 :             nblocks = hQMetaData->q_direction[0].cfg.nblocks;
    1084             : 
    1085       63570 :             for ( b = hQMetaData->q_direction[0].cfg.nbands - 1; b >= 0; b-- )
    1086             :             {
    1087       60993 :                 if ( hQMetaData->twoDirBands[b] == 1 )
    1088             :                 {
    1089       24462 :                     mvr2r( hQMetaData->q_direction[1].band_data[d].azimuth, hQMetaData->q_direction[1].band_data[b].azimuth, nblocks );
    1090       24462 :                     mvr2r( hQMetaData->q_direction[1].band_data[d].elevation, hQMetaData->q_direction[1].band_data[b].elevation, nblocks );
    1091       24462 :                     mvr2r( hQMetaData->q_direction[1].band_data[d].energy_ratio, hQMetaData->q_direction[1].band_data[b].energy_ratio, nblocks );
    1092             : 
    1093             : 
    1094       24462 :                     if ( hQMetaData->q_direction[1].coherence_band_data != NULL )
    1095             :                     {
    1096       24462 :                         mvc2c( hQMetaData->q_direction[1].coherence_band_data[d].spread_coherence, hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence, nblocks );
    1097             :                     }
    1098       24462 :                     d--;
    1099             :                 }
    1100             :                 else
    1101             :                 {
    1102       36531 :                     set_f( hQMetaData->q_direction[1].band_data[b].azimuth, 0.0f, nblocks );
    1103       36531 :                     set_f( hQMetaData->q_direction[1].band_data[b].elevation, 0.0f, nblocks );
    1104       36531 :                     set_f( hQMetaData->q_direction[1].band_data[b].energy_ratio, 0.0f, nblocks );
    1105             : 
    1106       36531 :                     if ( hQMetaData->q_direction[1].coherence_band_data != NULL )
    1107             :                     {
    1108       36531 :                         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       92070 :         for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
    1116             :         {
    1117      412578 :             for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
    1118             :             {
    1119             :                 float ratioSum;
    1120             : 
    1121      324225 :                 ratioSum = hQMetaData->q_direction[0].band_data[b].energy_ratio[m] + hQMetaData->q_direction[1].band_data[b].energy_ratio[m];
    1122             : 
    1123      324225 :                 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        6717 :     hQMetaData->ec_flag = 0;
    1168             : 
    1169        6717 :     hQMetaData->dir_comp_ratio = 1.0f;
    1170             : 
    1171        6717 :     if ( hQMetaData->dir_comp_ratio > 1.0f )
    1172             :     {
    1173           0 :         hQMetaData->dir_comp_ratio = 1.0f;
    1174             :     }
    1175             : 
    1176        6717 :     hQMetaData->q_direction[0].cfg.nbands = sf_nbands0;
    1177        6717 :     if ( hQMetaData->no_directions == 2 )
    1178             :     {
    1179        3717 :         hQMetaData->q_direction[1].cfg.nbands = sf_nbands1;
    1180             :     }
    1181             : 
    1182        6717 :     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        2289 : 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        2289 :     if ( ivas_format == SBA_FORMAT )
    1231             :     {
    1232        1674 :         sba_spar_bitlen = ivas_sba_spar_sid_bitlen( nchan_transport );
    1233        1674 :         metadata_sid_bits = (int16_t) ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - sba_spar_bitlen - SID_FORMAT_NBITS - SBA_ORDER_BITS - SBA_PLANAR_BITS - 1; /* -1 for inactive mode header bit*/
    1234             :     }
    1235             :     else
    1236             :     {
    1237         615 :         metadata_sid_bits = ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - SID_FORMAT_NBITS;
    1238             :     }
    1239             : 
    1240        2289 :     start_index = *index;
    1241             : 
    1242             :     /* read MASA SID descriptor */
    1243        2289 :     if ( ivas_format == MASA_FORMAT && nchan_transport == 2 )
    1244             :     {
    1245         222 :         b = bitstream[( *index )--];
    1246         222 :         if ( b )
    1247             :         {
    1248           0 :             *element_mode = IVAS_CPE_MDCT;
    1249             :         }
    1250             :         else
    1251             :         {
    1252         222 :             *element_mode = IVAS_CPE_DFT;
    1253             :         }
    1254             :     }
    1255             : 
    1256             :     /* Fix configuration for SID */
    1257        2289 :     q_direction = &hQMetaData->q_direction[0]; /* only 1 direction */
    1258        2289 :     if ( ivas_format == SBA_FORMAT )
    1259             :     {
    1260        1674 :         nbands = DIRAC_DTX_BANDS; /* only 2 bands transmitted */
    1261             :     }
    1262             :     else
    1263             :     {
    1264         615 :         nbands = 5; /* only 5 bands transmitted */
    1265             :     }
    1266             : 
    1267        2289 :     nblocks = q_direction->cfg.nblocks; /* only 1 block transmitted but up to 4 blocks re-generated */
    1268        2289 :     start_band = 0;                     /* start from band 0 */
    1269             : 
    1270             :     /* Read 2D signaling*/
    1271        2289 :     if ( ivas_format != SBA_FORMAT )
    1272             :     {
    1273         615 :         q_direction->not_in_2D = bitstream[( *index )--];
    1274             :     }
    1275             :     else
    1276             :     {
    1277        1674 :         q_direction->not_in_2D = 1;
    1278             :     }
    1279             : 
    1280        2289 :     bits_dir = 0;
    1281        2289 :     if ( ivas_format != SBA_FORMAT )
    1282             :     {
    1283             :         /* Decode diffuseness*/
    1284        3690 :         for ( b = start_band; b < nbands; b++ )
    1285             :         {
    1286        3075 :             diffuseness_index[b] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS - 4 ) + 4;
    1287        3075 :             q_direction->band_data[b].energy_ratio_index[0] = diffuseness_index[b];
    1288        3075 :             q_direction->band_data[b].bits_sph_idx[0] = bits_direction_masa[diffuseness_index[b]];
    1289        3075 :             bits_dir += q_direction->band_data[b].bits_sph_idx[0];
    1290        3075 :             q_direction->band_data[b].azimuth_m_alphabet[0] = no_phi_masa[q_direction->band_data[b].bits_sph_idx[0] - 1][0];
    1291             :         }
    1292             : 
    1293         615 :         bits_delta = metadata_sid_bits - ( start_index - *index ) - bits_dir; /* bit_diff is already read */
    1294             : 
    1295         615 :         if ( bits_delta > 0 )
    1296             :         {
    1297        2934 :             while ( bits_delta > 0 )
    1298             :             {
    1299       12618 :                 for ( b = start_band; b < nbands && ( bits_delta > 0 ); b++ )
    1300             :                 {
    1301       10299 :                     if ( q_direction->band_data[b].bits_sph_idx[0] < 11 )
    1302             :                     {
    1303       10299 :                         bits_delta -= 1;
    1304       10299 :                         q_direction->band_data[b].bits_sph_idx[0]++;
    1305             :                     }
    1306             :                 }
    1307             :             }
    1308             : 
    1309         615 :             if ( q_direction->not_in_2D == 0 )
    1310             :             {
    1311           0 :                 for ( b = start_band; b < nbands; b++ )
    1312             :                 {
    1313           0 :                     q_direction->band_data[b].azimuth_m_alphabet[0] = 1 << ( min( 8, q_direction->band_data[b].bits_sph_idx[0] ) );
    1314             :                 }
    1315             :             }
    1316             :         }
    1317             :         else
    1318             :         {
    1319           0 :             while ( bits_delta < 0 )
    1320             :             {
    1321           0 :                 for ( b = nbands - 1; b >= start_band && ( bits_delta < 0 ); b-- )
    1322             :                 {
    1323           0 :                     if ( q_direction->band_data[b].bits_sph_idx[0] >= 4 )
    1324             :                     {
    1325           0 :                         bits_delta += 1;
    1326           0 :                         q_direction->band_data[b].bits_sph_idx[0]--;
    1327             :                     }
    1328             :                 }
    1329             : 
    1330           0 :                 if ( q_direction->not_in_2D == 0 )
    1331             :                 {
    1332           0 :                     for ( b = start_band; b < nbands; b++ )
    1333             :                     {
    1334           0 :                         q_direction->band_data[b].azimuth_m_alphabet[0] = 1 << ( min( 8, q_direction->band_data[b].bits_sph_idx[0] ) );
    1335             :                     }
    1336             :                 }
    1337             :             }
    1338             :         }
    1339             :     }
    1340             :     else
    1341             :     {
    1342             :         /* Decode diffuseness*/
    1343        5022 :         for ( b = start_band; b < nbands; b++ )
    1344             :         {
    1345        3348 :             diffuseness_index[b] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS - 4 ) + 4;
    1346        3348 :             q_direction->band_data[b].energy_ratio_index[0] = diffuseness_index[b];
    1347        3348 :             q_direction->band_data[b].bits_sph_idx[0] = bits_direction_masa[diffuseness_index[b]];
    1348        3348 :             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        8712 :     for ( b = start_band; b < nbands; b++ )
    1353             :     {
    1354        6423 :         q_direction->band_data[b].energy_ratio[0] = 1.0f - diffuseness_reconstructions[diffuseness_index[b]];
    1355       31719 :         for ( i = 0; i < nblocks; i++ )
    1356             :         {
    1357       25296 :             q_direction->band_data[b].energy_ratio[i] = q_direction->band_data[b].energy_ratio[0];
    1358             :         }
    1359             :     }
    1360             : 
    1361             :     /* Decoder DOAs*/
    1362        2289 :     if ( q_direction->not_in_2D > 0 )
    1363             :     {
    1364        8712 :         for ( b = start_band; b < nbands; b++ )
    1365             :         {
    1366        6423 :             value = 0;
    1367       42522 :             for ( i = 0; i < q_direction->band_data[b].bits_sph_idx[0]; i++ )
    1368             :             {
    1369       36099 :                 value = ( value << 1 ) + bitstream[( *index )--];
    1370             :             }
    1371       31719 :             for ( i = 0; i < nblocks; i++ )
    1372             :             {
    1373       25296 :                 q_direction->band_data[b].spherical_index[i] = value;
    1374             :             }
    1375             : 
    1376        6423 :             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        6423 :             ivas_qmetadata_azimuth_elevation_to_direction_vector( avg_azimuth, avg_elevation, avg_direction_vector );
    1379        6423 :             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       25296 :             for ( m = 0; m < nblocks - 1; m++ )
    1382             :             {
    1383       18873 :                 v_add( direction_vector, avg_direction_vector, direction_vector, 3 );
    1384       18873 :                 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        6423 :             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        8778 :     while ( start_index - *index < metadata_sid_bits )
    1426             :     {
    1427        6489 :         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        2289 :     return ( start_index - *index );
    1452             : }
    1453             : 
    1454             : 
    1455             : /*-----------------------------------------------------------------------*
    1456             :  * Local function definitions for diffuseness/energy ratios
    1457             :  *-----------------------------------------------------------------------*/
    1458             : 
    1459      613215 : 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      613215 :     val = 0;
    1467     1198476 :     while ( val <= DIFF_EC_HUFF_GR0_LIMIT )
    1468             :     {
    1469     1197684 :         if ( bitstream[( *index )--] == 1 )
    1470             :         {
    1471      585261 :             val++;
    1472             :         }
    1473             :         else
    1474             :         {
    1475      612423 :             break;
    1476             :         }
    1477             :     }
    1478             : 
    1479      613215 :     if ( val == DIFF_EC_HUFF_GR0_LIMIT + 1 )
    1480             :     {
    1481         792 :         val += 2 * bitstream[( *index )];
    1482         792 :         ( *index )--;
    1483         792 :         val += bitstream[( *index )];
    1484         792 :         ( *index )--;
    1485             :     }
    1486             : 
    1487      613215 :     if ( val % 2 == 0 )
    1488             :     {
    1489      403818 :         return -( val / 2 ) + av;
    1490             :     }
    1491             :     else
    1492             :     {
    1493      209397 :         return ( val + 1 ) / 2 + av;
    1494             :     }
    1495             : }
    1496             : 
    1497             : 
    1498             : /*-------------------------------------------------------------------*
    1499             :  * ivas_qmetadata_entropy_decode_diffuseness()
    1500             :  *
    1501             :  *
    1502             :  *-------------------------------------------------------------------*/
    1503             : 
    1504      584613 : 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      584613 :     index_start = *index;
    1517      584613 :     nbands = q_direction->cfg.nbands;
    1518      584613 :     start_band = q_direction->cfg.start_band;
    1519             : 
    1520             :     /* diffuseness decoding */
    1521             :     /* Handle one band as special case*/
    1522      584613 :     if ( nbands == 1 )
    1523             :     {
    1524       11733 :         q_direction->band_data[0].energy_ratio_index[0] = 0;
    1525       46932 :         for ( b = 0; b < MASA_BITS_ER; b++ )
    1526             :         {
    1527       35199 :             q_direction->band_data[0].energy_ratio_index[0] = ( q_direction->band_data[0].energy_ratio_index[0] << 1 ) + bitstream[( *index )--];
    1528             :         }
    1529       11733 :         *diffuseness_index_max_ec_frame = 5;
    1530             : 
    1531       11733 :         return MASA_BITS_ER;
    1532             :     }
    1533             : 
    1534      572880 :     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      313758 :         if ( nbands < DIFF_EC_HUFF_BAND_LIMIT )
    1538             :         {
    1539      262191 :             if ( bitstream[( *index )--] != 0 ) /* dif_have_unique_value */
    1540             :             {
    1541      149070 :                 dif_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS ); /* dif_unique_value */
    1542             : 
    1543      590982 :                 for ( b = start_band; b < nbands; b++ )
    1544             :                 {
    1545      441912 :                     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      113121 :                 dif_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS - 1 ); /* dif_min_value */
    1551             : 
    1552      496080 :                 for ( b = start_band; b < nbands; b++ )
    1553             :                 {
    1554      382959 :                     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       51567 :             av = 0;
    1564      206268 :             for ( b = 0; b < MASA_BITS_ER; b++ )
    1565             :             {
    1566      154701 :                 av += bitstream[( *index )--] * ( 1 << ( MASA_BITS_ER - 1 - b ) );
    1567             :             }
    1568             : 
    1569       51567 :             dif_min = DIRAC_DIFFUSE_LEVELS;
    1570             :             /* read average removed data (average is added inside)*/
    1571      664782 :             for ( b = start_band; b < nbands; b++ )
    1572             :             {
    1573      613215 :                 q_direction->band_data[b].energy_ratio_index[0] = ivas_diffuseness_huff_ec_decode( bitstream, index, av );
    1574      613215 :                 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      259122 :         dif_min = DIRAC_DIFFUSE_LEVELS;
    1581             : 
    1582     1386945 :         for ( b = start_band; b < nbands; b++ )
    1583             :         {
    1584     1127823 :             q_direction->band_data[b].energy_ratio_index[0] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS );
    1585     1127823 :             dif_min = min( dif_min, q_direction->band_data[b].energy_ratio_index[0] );
    1586             :         }
    1587             :     }
    1588             : 
    1589      572880 :     *diffuseness_index_max_ec_frame = 5;
    1590             :     /* adaptively select the diffuseness_index_max_ec threshold */
    1591      572880 :     if ( dif_min > 5 )
    1592             :     {
    1593      217311 :         *diffuseness_index_max_ec_frame = DIRAC_DIFFUSE_LEVELS - 1;
    1594             :     }
    1595             : 
    1596      572880 :     return ( index_start - *index );
    1597             : }
    1598             : 
    1599             : 
    1600             : /*-------------------------------------------------------------------*
    1601             :  * ivas_qmetadata_entropy_decode_diffuseness_hr_512()
    1602             :  *
    1603             :  *
    1604             :  *-------------------------------------------------------------------*/
    1605             : 
    1606       10434 : 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       10434 :     index_start = *index;
    1618       10434 :     nbands = q_direction->cfg.nbands;
    1619       10434 :     nblocks = q_direction->cfg.nblocks;
    1620       10434 :     start_band = q_direction->cfg.start_band;
    1621             : 
    1622             :     /* diffuseness decoding */
    1623             :     /* Handle one band as special case*/
    1624       10434 :     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      222609 :     for ( b = start_band; b < nbands; b++ )
    1636             :     {
    1637     1016460 :         for ( k = 0; k < nblocks; k++ )
    1638             :         {
    1639      804285 :             q_direction->band_data[b].energy_ratio_index[k] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, HR_MASA_ER_LEVELS );
    1640             :         }
    1641             :     }
    1642             : 
    1643       10434 :     return ( index_start - *index );
    1644             : }
    1645             : 
    1646             : 
    1647             : /*-------------------------------------------------------------------*
    1648             :  * ivas_qmetadata_entropy_decode_df_ratio()
    1649             :  *
    1650             :  *
    1651             :  *-------------------------------------------------------------------*/
    1652             : 
    1653       62238 : 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       62238 :     index_start = *index;
    1671       62238 :     nbands = q_direction->cfg.nbands;
    1672       62238 :     start_band = q_direction->cfg.start_band;
    1673             : 
    1674             :     /* Handle one band as special case*/
    1675       62238 :     if ( nbands == 1 )
    1676             :     {
    1677        5163 :         q_direction->band_data[0].energy_ratio_index[0] = 0;
    1678       16008 :         for ( b = 0; b < dfRatio_bits[0]; b++ )
    1679             :         {
    1680       10845 :             q_direction->band_data[0].energy_ratio_index[0] = ( q_direction->band_data[0].energy_ratio_index[0] << 1 ) + bitstream[( *index )--];
    1681             :         }
    1682        5163 :         return dfRatio_bits[0];
    1683             :     }
    1684             : 
    1685             :     /* Calculate raw coding bits and decide what modes are possible */
    1686       57075 :     bits_raw = 0;
    1687       57075 :     max_dfRatio_bits = 0;
    1688      626289 :     for ( b = start_band; b < nbands; b++ )
    1689             :     {
    1690      569214 :         bits_raw += dfRatio_bits[b];
    1691      569214 :         max_dfRatio_bits = max( max_dfRatio_bits, dfRatio_bits[b] );
    1692             :     }
    1693             : 
    1694             :     /* Decide what modes are possible */
    1695       57075 :     if ( bits_raw >= max_dfRatio_bits + 2 + nbands )
    1696             :     {
    1697       34095 :         ec_mode = 2;
    1698             :     }
    1699       22980 :     else if ( bits_raw >= max_dfRatio_bits + 1 )
    1700             :     {
    1701       22980 :         ec_mode = 1;
    1702             :     }
    1703             :     else
    1704             :     {
    1705           0 :         ec_mode = 0;
    1706             :     }
    1707       57075 :     max_alphabet_size = 1 << max_dfRatio_bits;
    1708             : 
    1709       57075 :     dec_mode = 2; /* Default to raw decoding */
    1710       57075 :     if ( ec_mode == 1 )
    1711             :     {
    1712       22980 :         if ( bitstream[( *index )--] == 0 )
    1713             :         {
    1714         864 :             dec_mode = 1; /* Switch to one value EC coding */
    1715             :         }
    1716             :     }
    1717       34095 :     else if ( ec_mode == 2 )
    1718             :     {
    1719       34095 :         if ( bitstream[( *index )--] == 0 )
    1720             :         {
    1721        4005 :             if ( bitstream[( *index )--] == 0 )
    1722             :             {
    1723         297 :                 dec_mode = 1; /* Switch to one value EC coding */
    1724             :             }
    1725             :             else
    1726             :             {
    1727        3708 :                 dec_mode = 0; /* Use one-bit diff bandwise mode */
    1728             :             }
    1729             :         }
    1730             :     }
    1731             : 
    1732       57075 :     if ( dec_mode == 2 ) /* Raw decoding */
    1733             :     {
    1734      583326 :         for ( b = start_band; b < nbands; b++ )
    1735             :         {
    1736      531120 :             q_direction->band_data[b].energy_ratio_index[0] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, 1 << dfRatio_bits[b] );
    1737             :         }
    1738             :     }
    1739        4869 :     else if ( dec_mode == 1 ) /* One value decoding */
    1740             :     {
    1741        1161 :         ratio_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, max_alphabet_size ); /* dif_unique_value */
    1742             : 
    1743        6669 :         for ( b = start_band; b < nbands; b++ )
    1744             :         {
    1745        5508 :             q_direction->band_data[b].energy_ratio_index[0] = ratio_min;
    1746             :         }
    1747             :     }
    1748             :     else /* Bandwise 1-bit diff decoding */
    1749             :     {
    1750        3708 :         ratio_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, max_alphabet_size - 1 ); /* dif_min_value */
    1751             : 
    1752       36294 :         for ( b = start_band; b < nbands; b++ )
    1753             :         {
    1754       32586 :             q_direction->band_data[b].energy_ratio_index[0] = ratio_min + bitstream[( *index )--]; /* dif_bit_offset_values */
    1755             :         }
    1756             :     }
    1757             : 
    1758       57075 :     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      342231 : 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      342231 :     index_start = *index;
    1800      342231 :     nblocks = q_direction->cfg.nblocks;
    1801             : 
    1802      342231 :     diff_idx_min = DIRAC_DIFFUSE_LEVELS;
    1803             : 
    1804             :     /*Raw coding for high diffuseness*/
    1805     2284896 :     for ( b = start_band; b < nbands; b++ )
    1806             :     {
    1807     1942665 :         if ( hrmasa_flag )
    1808             :         {
    1809           0 :             diff_idx = 0;
    1810             :         }
    1811             :         else
    1812             :         {
    1813     1942665 :             diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
    1814             :         }
    1815             : 
    1816     1942665 :         diff_idx_min = min( diff_idx_min, diff_idx );
    1817     1942665 :         if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    1818             :         {
    1819       94959 :             elev_alph[b] = no_theta_masa[bits_direction_masa[diff_idx] - 3];
    1820             :         }
    1821             :         else
    1822             :         {
    1823     1847706 :             elev_alph[b] = no_theta_masa[bits_direction_masa[diff_idx] - 3] * 2 - 1;
    1824             :         }
    1825             : 
    1826     1942665 :         if ( q_direction->band_data[b].energy_ratio_index_mod[0] > diffuseness_index_max_ec_frame )
    1827             :         {
    1828      484020 :             bands_entropic[b] = 0;
    1829             : 
    1830      484020 :             if ( q_direction->not_in_2D > 0 )
    1831             :             {
    1832      427320 :                 decode_fixed_rate( q_direction, bitstream, index, b, nblocks );
    1833             :             }
    1834             :             else
    1835             :             {
    1836             :                 /* in 2D */
    1837      260793 :                 for ( m = 0; m < nblocks; m++ )
    1838             :                 {
    1839      204093 :                     q_direction->band_data[b].elevation[m] = 0.f;
    1840      204093 :                     q_direction->band_data[b].elevation_index[m] = 0;
    1841             : 
    1842      204093 :                     azith_alph[b][m] = no_phi_masa[bits_direction_masa[diff_idx] - 1][0];
    1843      204093 :                     q_direction->band_data[b].azimuth_index[m] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, azith_alph[b][m] );
    1844      204093 :                     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     1458645 :             bands_entropic[b] = 1;
    1851             :         }
    1852             :     }
    1853             : 
    1854             :     /*EC for the low diffuseness*/
    1855             : 
    1856             :     /*Elevation only if not 2D  */
    1857      342231 :     if ( q_direction->not_in_2D > 0 )
    1858             :     {
    1859      278808 :         if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    1860             :         {
    1861        3189 :             avg_elevation_alphabet = no_theta_masa[bits_direction_masa[diff_idx_min] - 3];
    1862        3189 :             avg_elevation_idx = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, avg_elevation_alphabet );
    1863             :         }
    1864             :         else
    1865             :         {
    1866      275619 :             avg_elevation_alphabet = no_theta_masa[bits_direction_masa[diff_idx_min] - 3] * 2 - 1;
    1867      275619 :             avg_elevation_idx = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, avg_elevation_alphabet );
    1868      275619 :             avg_elevation_idx = ivas_qmetadata_dereorder_generic( avg_elevation_idx ) + ( avg_elevation_alphabet >> 1 );
    1869             :         }
    1870             : 
    1871      278808 :         gr_param_elev = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, 4 + 1 );
    1872      278808 :         if ( gr_param_elev == 4 ) /* all the elevation distances are zero */
    1873             :         {
    1874      177540 :             for ( b = start_band; b < nbands; b++ )
    1875             :             {
    1876      136659 :                 if ( bands_entropic[b] )
    1877             :                 {
    1878             :                     int16_t tmp_index;
    1879       62466 :                     if ( hrmasa_flag )
    1880             :                     {
    1881           0 :                         diff_idx = 0;
    1882             :                     }
    1883             :                     else
    1884             :                     {
    1885       62466 :                         diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
    1886             :                     }
    1887             : 
    1888       62466 :                     if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    1889             :                     {
    1890        2088 :                         avg_elevation_index_projected = ivas_chan_project_elevation_index( avg_elevation_idx, avg_elevation_alphabet, elev_alph[b] );
    1891             :                     }
    1892             :                     else
    1893             :                     {
    1894       60378 :                         avg_elevation_index_projected = ivas_dirac_project_elevation_index( avg_elevation_idx, avg_elevation_alphabet, elev_alph[b] );
    1895             : 
    1896             :                         /*reorder elevation indexing*/
    1897       60378 :                         tmp_index = avg_elevation_index_projected - ( elev_alph[b] >> 1 );
    1898       60378 :                         if ( tmp_index < 0 )
    1899             :                         {
    1900       21813 :                             tmp_index = tmp_index * -2;
    1901             :                         }
    1902       38565 :                         else if ( tmp_index > 0 )
    1903             :                         {
    1904        8403 :                             tmp_index = tmp_index * 2 - 1;
    1905             :                         }
    1906       60378 :                         avg_elevation_index_projected = tmp_index;
    1907             :                     }
    1908             : 
    1909      264243 :                     for ( m = 0; m < nblocks; m++ )
    1910             :                     {
    1911      201777 :                         q_direction->band_data[b].elevation_index[m] = avg_elevation_index_projected;
    1912             : 
    1913             :                         /*deduce aplhabet for azimuth*/
    1914      201777 :                         if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    1915             :                         {
    1916        7209 :                             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      194568 :                             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      201777 :                         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     1732824 :             for ( b = start_band; b < nbands; b++ )
    1932             :             {
    1933     1494897 :                 if ( bands_entropic[b] )
    1934             :                 {
    1935     1141770 :                     if ( hrmasa_flag )
    1936             :                     {
    1937           0 :                         diff_idx = 0;
    1938             :                     }
    1939             :                     else
    1940             :                     {
    1941     1141770 :                         diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
    1942             :                     }
    1943             : 
    1944     5459442 :                     for ( m = 0; m < nblocks; m++ )
    1945             :                     {
    1946             :                         int16_t tmp_index;
    1947     4317672 :                         if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    1948             :                         {
    1949       24720 :                             avg_elevation_index_projected = ivas_chan_project_elevation_index( avg_elevation_idx, avg_elevation_alphabet, elev_alph[b] );
    1950       24720 :                             tmp_index = ivas_qmetadata_DecodeExtendedGR( bitstream, index, 2 * elev_alph[b] - 1, gr_param_elev );
    1951       24720 :                             if ( tmp_index % 2 )
    1952             :                             {
    1953        2541 :                                 tmp_index = avg_elevation_index_projected + ( ( tmp_index + 1 ) >> 1 );
    1954             :                             }
    1955             :                             else
    1956             :                             {
    1957       22179 :                                 tmp_index = avg_elevation_index_projected - ( tmp_index >> 1 );
    1958             :                             }
    1959       24720 :                             q_direction->band_data[b].elevation_index[m] = tmp_index;
    1960             : 
    1961             :                             /*deduce aplhabet for azimuth*/
    1962       24720 :                             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     4292952 :                             avg_elevation_index_projected = ivas_dirac_project_elevation_index( avg_elevation_idx, avg_elevation_alphabet, elev_alph[b] );
    1967             : 
    1968     4292952 :                             tmp_index = ivas_qmetadata_DecodeExtendedGR( bitstream, index, elev_alph[b], gr_param_elev );
    1969     4292952 :                             tmp_index = ivas_qmetadata_ReorderElevationDecoded( tmp_index, avg_elevation_index_projected, elev_alph[b] );
    1970             : 
    1971             :                             /*reorder elevation indexing*/
    1972     4292952 :                             tmp_index = tmp_index - ( elev_alph[b] >> 1 );
    1973     4292952 :                             if ( tmp_index < 0 )
    1974             :                             {
    1975     1712058 :                                 tmp_index = tmp_index * -2;
    1976             :                             }
    1977     2580894 :                             else if ( tmp_index > 0 )
    1978             :                             {
    1979      821130 :                                 tmp_index = tmp_index * 2 - 1;
    1980             :                             }
    1981     4292952 :                             q_direction->band_data[b].elevation_index[m] = tmp_index;
    1982             : 
    1983             :                             /*deduce aplhabet for azimuth*/
    1984     4292952 :                             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     4317672 :                         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      374532 :         for ( b = start_band; b < nbands; b++ )
    1997             :         {
    1998      311109 :             if ( bands_entropic[b] )
    1999             :             {
    2000      254409 :                 if ( hrmasa_flag )
    2001             :                 {
    2002           0 :                     diff_idx = 0;
    2003             :                 }
    2004             :                 else
    2005             :                 {
    2006      254409 :                     diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
    2007             :                 }
    2008             : 
    2009     1076835 :                 for ( m = 0; m < nblocks; m++ )
    2010             :                 {
    2011      822426 :                     q_direction->band_data[b].elevation_index[m] = 0;
    2012             : 
    2013             :                     /*deduce alphabet for azimuth*/
    2014      822426 :                     if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    2015             :                     {
    2016      145953 :                         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      676473 :                         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      822426 :                     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      342231 :     avg_azimuth_alphabet = no_phi_masa[bits_direction_masa[diff_idx_min] - 1][0]; /* average azimuth is quantized on the equatorial plane */
    2032      342231 :     avg_azimuth_index = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, avg_azimuth_alphabet );
    2033      342231 :     avg_azimuth_index = ivas_qmetadata_dereorder_generic( avg_azimuth_index );
    2034      342231 :     avg_azimuth_index = avg_azimuth_index + ( avg_azimuth_alphabet >> 1 );
    2035             : 
    2036      342231 :     gr_param_azith = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, 5 + 1 );
    2037      342231 :     if ( gr_param_azith == 5 ) /* all the azimuth distances are zero */
    2038             :     {
    2039      217587 :         for ( b = start_band; b < nbands; b++ )
    2040             :         {
    2041      168294 :             if ( bands_entropic[b] )
    2042             :             {
    2043      397569 :                 for ( m = 0; m < nblocks; m++ )
    2044             :                 {
    2045      290832 :                     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      290832 :                     if ( azith_alph[b][m] == 1 )
    2048             :                     {
    2049          33 :                         q_direction->band_data[b].azimuth[m] = 0.f;
    2050             :                     }
    2051             :                     else
    2052             :                     {
    2053      290799 :                         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      292938 :         set_zero( avg_direction_vector, 3 );
    2062      292938 :         use_adapt_avg = 0;
    2063      292938 :         idx = 0;
    2064             : 
    2065     2067309 :         for ( b = start_band; b < nbands; b++ )
    2066             :         {
    2067     1774371 :             if ( bands_entropic[b] )
    2068             :             {
    2069     6402951 :                 for ( m = 0; m < nblocks; m++ )
    2070             :                 {
    2071     5051043 :                     if ( ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( idx == MASA_LIMIT_IDX_AVG_AZI ) && ( nblocks > 1 ) )
    2072             :                     {
    2073        6153 :                         use_adapt_avg = bitstream[*index];
    2074        6153 :                         ( *index )--;
    2075             :                     }
    2076     5051043 :                     avg_azimuth_index_projected = ivas_dirac_project_azimuth_index( avg_azimuth_index, avg_azimuth_alphabet, azith_alph[b][m] );
    2077     5051043 :                     q_direction->band_data[b].azimuth_index[m] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, azith_alph[b][m], gr_param_azith );
    2078     5051043 :                     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     5051043 :                     if ( azith_alph[b][m] == 1 )
    2081             :                     {
    2082        1254 :                         q_direction->band_data[b].azimuth[m] = 0.f;
    2083             :                     }
    2084             :                     else
    2085             :                     {
    2086     5049789 :                         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     5051043 :                     if ( ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( nblocks > 1 ) )
    2089             :                     {
    2090      112140 :                         if ( idx < MASA_LIMIT_IDX_AVG_AZI )
    2091             :                         {
    2092       34944 :                             ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[b].azimuth[m], q_direction->band_data[b].elevation[m], direction_vector );
    2093       34944 :                             v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
    2094             :                         }
    2095             :                         else
    2096             :                         {
    2097       77196 :                             if ( use_adapt_avg == 1 )
    2098             :                             {
    2099       36768 :                                 if ( m == 0 )
    2100             :                                 {
    2101        9192 :                                     v_multc( avg_direction_vector, 0.5f, avg_direction_vector, 3 );
    2102             :                                 }
    2103             :                                 /*compute the average direction per already coded subband */
    2104       36768 :                                 ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[b].azimuth[m], q_direction->band_data[b].elevation[m], direction_vector );
    2105       36768 :                                 v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
    2106       36768 :                                 ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector, &avg_azimuth, &avg_elevation );
    2107       36768 :                                 avg_azimuth_index = quantize_phi( avg_azimuth + 180, 0, &avg_azimuth, avg_azimuth_alphabet );
    2108             :                             }
    2109             :                         }
    2110      112140 :                         idx++;
    2111             :                     }
    2112             :                 }
    2113             :             }
    2114             :         }
    2115             :     }
    2116             : 
    2117      342231 :     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       10434 : 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       10434 :     index_start = *index;
    2142       10434 :     nblocks = q_direction->cfg.nblocks;
    2143             : 
    2144      222609 :     for ( b = start_band; b < nbands; b++ )
    2145             :     {
    2146     1016460 :         for ( m = 0; m < nblocks; m++ )
    2147             :         {
    2148      804285 :             value = 0;
    2149    11336220 :             for ( i = 0; i < q_direction->band_data[b].bits_sph_idx[m]; i++ )
    2150             :             {
    2151    10531935 :                 value = ( value << 1 ) + bitstream[( *index )--];
    2152             :             }
    2153      804285 :             q_direction->band_data[b].spherical_index[m] = value;
    2154             : 
    2155      804285 :             if ( q_direction->band_data[b].bits_sph_idx[m] == 16 )
    2156             :             {
    2157      336960 :                 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      467325 :                 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       10434 :     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      295269 : 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      295269 :     index_start = *index;
    2191      295269 :     nblocks = q_direction->cfg.nblocks;
    2192             : 
    2193     1382763 :     for ( b = start_band; b < nbands; b++ )
    2194             :     {
    2195     1087494 :         if ( q_direction->not_in_2D > 0 )
    2196             :         {
    2197      925218 :             decode_fixed_rate( q_direction, bitstream, index, b, nblocks );
    2198             :         }
    2199             :         else
    2200             :         {
    2201      162276 :             if ( hrmasa_flag )
    2202             :             {
    2203           0 :                 diff_idx = 0;
    2204             :             }
    2205             :             else
    2206             :             {
    2207      162276 :                 diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
    2208             :             }
    2209             : 
    2210      563214 :             for ( m = 0; m < nblocks; m++ )
    2211             :             {
    2212      400938 :                 q_direction->band_data[b].elevation[m] = 0.f;
    2213      400938 :                 q_direction->band_data[b].elevation_index[m] = 0;
    2214      400938 :                 azith_alph = no_phi_masa[bits_direction_masa[diff_idx] - 1][0];
    2215      400938 :                 q_direction->band_data[b].azimuth_index[m] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, azith_alph );
    2216      400938 :                 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      295269 :     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     5754462 : 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     5754462 :     bits = 30 - norm_l( alphabet_size ); /* bits = floor(log2(alphabet_size)) */
    2246     5754462 :     tresh = ( 1U << ( bits + 1 ) ) - alphabet_size;
    2247             : 
    2248     5754462 :     value = 0;
    2249    21744585 :     for ( i = 0; i < bits; i++ )
    2250             :     {
    2251    15990123 :         value = ( value << 1 ) + bitstream[( *index )--];
    2252             :     }
    2253             : 
    2254     5754462 :     if ( value >= tresh )
    2255             :     {
    2256      769866 :         value = ( value << 1 ) - tresh + bitstream[( *index )--];
    2257             :     }
    2258             : 
    2259             : #ifdef DEBUGGING
    2260             :     assert( value < alphabet_size );
    2261             : #endif
    2262     5754462 :     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    12873111 : 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    12873111 :     msb_size = ( alph_size + ( 1 << gr_param ) - 1 ) >> gr_param; /* ceil division */
    2285    12873111 :     if ( msb_size <= 3 )
    2286             :     {
    2287     1127952 :         value = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, alph_size );
    2288             :     }
    2289             :     else
    2290             :     {
    2291    11745159 :         msb = 0;
    2292    20358015 :         while ( ( msb < msb_size - 1 ) && ( bitstream[*index] != 0 ) )
    2293             :         {
    2294     8612856 :             msb++;
    2295     8612856 :             ( *index )--;
    2296             :         }
    2297             : 
    2298    11745159 :         if ( msb == msb_size - 1 )
    2299             :         {
    2300       42690 :             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    11702469 :             ( *index )--;
    2308    11702469 :             lsb = 0;
    2309    19147290 :             for ( i = 0; i < gr_param; i++ )
    2310             :             {
    2311     7444821 :                 lsb = ( lsb << 1 ) + bitstream[( *index )--];
    2312             :             }
    2313             :         }
    2314             : 
    2315    11745159 :         value = ( msb << gr_param ) + lsb;
    2316             :     }
    2317             : 
    2318             : #ifdef DEBUGGING
    2319             :     assert( value < alph_size );
    2320             : #endif
    2321    12873111 :     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     9343995 : 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     9343995 :     dist_reorder = ivas_qmetadata_dereorder_generic( elev_dist );
    2342     9343995 :     elev_index_reorder = elev_avg + dist_reorder;
    2343             : 
    2344     9343995 :     if ( elev_index_reorder < 0 )
    2345             :     {
    2346       59199 :         elev_index_reorder += elev_alph;
    2347             :     }
    2348     9284796 :     else if ( elev_index_reorder >= elev_alph )
    2349             :     {
    2350       77976 :         elev_index_reorder -= elev_alph;
    2351             :     }
    2352             : 
    2353     9343995 :     return elev_index_reorder;
    2354             : }
    2355             : 
    2356             : 
    2357             : /*-----------------------------------------------------------------------*
    2358             :  * Local functions: requentizeEC3
    2359             :  *-----------------------------------------------------------------------*/
    2360             : 
    2361             : /*! r: number of bits read */
    2362       26340 : 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       26340 :     bit_pos = *pbit_pos;
    2379             : 
    2380       26340 :     diff = 0;
    2381       26340 :     if ( q_direction->not_in_2D )
    2382             :     {
    2383       24993 :         if ( coding_subbands > 1 )
    2384             :         {
    2385       24558 :             j = ind_order[coding_subbands - 1];
    2386       24558 :             allowed_bits = 0;
    2387             : 
    2388       95079 :             for ( k = 0; k < masa_subframes; k++ )
    2389             :             {
    2390       70521 :                 allowed_bits += q_direction->band_data[j].bits_sph_idx[k];
    2391             :             }
    2392             : 
    2393       24558 :             last_j = j - ( allowed_bits == 0 );
    2394             : 
    2395      102240 :             for ( j = 0; j < last_j; j++ )
    2396             :             {
    2397       77682 :                 i = ind_order[j];
    2398       77682 :                 bits_dir0 = (int16_t *) q_direction->band_data[i].bits_sph_idx;
    2399             : 
    2400       77682 :                 nbits = 0;
    2401       77682 :                 allowed_bits = sum_s( bits_dir0, q_direction->cfg.nblocks );
    2402       77682 :                 use_vq = 0;
    2403       77682 :                 max_nb_idx = 0;
    2404             : 
    2405      289941 :                 for ( k = 0; k < q_direction->cfg.nblocks; k++ )
    2406             :                 {
    2407      212259 :                     if ( bits_dir0[k] > use_vq )
    2408             :                     {
    2409       93672 :                         use_vq = bits_dir0[k];
    2410       93672 :                         max_nb_idx = k;
    2411             :                     }
    2412             :                 }
    2413             : 
    2414       77682 :                 if ( q_direction->cfg.nblocks == 1 )
    2415             :                 {
    2416       32823 :                     byteBuffer = 0;
    2417             :                 }
    2418             :                 else
    2419             :                 {
    2420       44859 :                     byteBuffer = 0;
    2421       44859 :                     if ( use_vq <= 1 )
    2422             :                     {
    2423          87 :                         byteBuffer = 1;
    2424             :                     }
    2425       44859 :                     if ( use_vq > 1 && use_vq <= LIMIT_USE_COMMON )
    2426             :                     {
    2427        7494 :                         bits_dir0[max_nb_idx] -= 1;
    2428        7494 :                         allowed_bits -= 1;
    2429             :                         /* read 1 bit to tell if joint of VQ coding */
    2430        7494 :                         byteBuffer = bitstream[bit_pos--];
    2431             :                     }
    2432             :                 }
    2433             : 
    2434      289941 :                 for ( k = 0; k < masa_subframes; k++ )
    2435             :                 {
    2436      212259 :                     q_direction->band_data[i].bits_sph_idx[k] = bits_dir0[k];
    2437      212259 :                     if ( bits_dir0[k] > 2 )
    2438             :                     {
    2439      201594 :                         if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    2440             :                         {
    2441       13368 :                             q_direction->band_data[i].elevation_m_alphabet[k] = no_theta_masa[bits_dir0[k] - 3];
    2442             :                         }
    2443             :                         else
    2444             :                         {
    2445      188226 :                             q_direction->band_data[i].elevation_m_alphabet[k] = no_theta_masa[bits_dir0[k] - 3] * 2 - 1;
    2446             :                         }
    2447             :                     }
    2448             :                     else
    2449             :                     {
    2450       10665 :                         q_direction->band_data[i].elevation_m_alphabet[k] = 1;
    2451             :                     }
    2452             :                 }
    2453             : 
    2454       77682 :                 if ( allowed_bits > 0 )
    2455             :                 {
    2456       77682 :                     if ( byteBuffer == 1 )
    2457             :                     {
    2458        7398 :                         nbits = read_common_direction( bitstream, q_direction, i, masa_subframes, allowed_bits, &bit_pos );
    2459             :                     }
    2460             :                     else
    2461             :                     {
    2462       70284 :                         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       16908 :                             fixed_rate = 1;
    2466       16908 :                             nbits = 0;
    2467             :                         }
    2468             :                         else
    2469             :                         {
    2470             :                             /* check if fixed_rate */
    2471       53376 :                             fixed_rate = bitstream[bit_pos--];
    2472       53376 :                             nbits = 1;
    2473             :                         }
    2474             : 
    2475       70284 :                         if ( fixed_rate == 1 )
    2476             :                         {
    2477             :                             /* decode_fixed_rate()*/
    2478       47160 :                             nbits += decode_fixed_rate( q_direction, bitstream, &bit_pos, i, masa_subframes );
    2479             :                         }
    2480             :                         else
    2481             :                         {
    2482             :                             /* decode elevation */
    2483       23124 :                             nbits += decode_elevation( q_direction, bitstream, &bit_pos, i, masa_subframes );
    2484             : 
    2485             :                             /* decode azimuth */
    2486       23124 :                             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       77682 :                 diff += nbits - allowed_bits;
    2495             : 
    2496             :                 /* update bits for next block */
    2497       77682 :                 update_bits_next_block( q_direction, &diff, ind_order[j + 1], coding_subbands, masa_subframes );
    2498             :             }
    2499             :         }
    2500             :         else
    2501             :         {
    2502         435 :             last_j = q_direction->cfg.start_band;
    2503             :         }
    2504             : 
    2505             : 
    2506       64734 :         for ( j = last_j; j < coding_subbands; j++ )
    2507             :         {
    2508       39741 :             i = ind_order[j];
    2509       39741 :             bits_dir0 = (int16_t *) q_direction->band_data[i].bits_sph_idx;
    2510             : 
    2511       39741 :             nbits = 0;
    2512       39741 :             allowed_bits = sum_s( bits_dir0, q_direction->cfg.nblocks );
    2513       39741 :             if ( allowed_bits > 0 && masa_subframes == 1 )
    2514             :             {
    2515       15852 :                 nbits += decode_fixed_rate( q_direction, bitstream, &bit_pos, i, masa_subframes );
    2516             :             }
    2517             :             else
    2518             :             {
    2519       23889 :                 if ( allowed_bits > 0 )
    2520             :                 {
    2521       23889 :                     use_vq = 0;
    2522       23889 :                     max_nb_idx = 0;
    2523      119445 :                     for ( k = 0; k < masa_subframes; k++ )
    2524             :                     {
    2525       95556 :                         if ( bits_dir0[k] > use_vq )
    2526             :                         {
    2527       32835 :                             use_vq = bits_dir0[k];
    2528       32835 :                             max_nb_idx = k;
    2529             :                         }
    2530             :                     }
    2531             : 
    2532       23889 :                     byteBuffer = 0;
    2533             : 
    2534       23889 :                     if ( use_vq > 1 && use_vq <= LIMIT_USE_COMMON )
    2535             :                     {
    2536        3345 :                         bits_dir0[max_nb_idx] -= 1;
    2537        3345 :                         allowed_bits -= 1;
    2538             : 
    2539             :                         /* read 1 bit to tell if joint of VQ coding */
    2540        3345 :                         byteBuffer = bitstream[bit_pos--];
    2541             :                     }
    2542             : 
    2543       23889 :                     if ( allowed_bits > 0 )
    2544             :                     {
    2545       23889 :                         if ( byteBuffer == 1 || use_vq <= 1 )
    2546             :                         {
    2547        3255 :                             nbits = read_common_direction( bitstream, q_direction, i, masa_subframes, allowed_bits, &bit_pos );
    2548             :                         }
    2549             :                         else
    2550             :                         {
    2551             :                             /* decode_fixed_rate()*/
    2552       20634 :                             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        5784 :         for ( j = 0; j < coding_subbands; j++ )
    2571             :         {
    2572       17982 :             for ( k = 0; k < q_direction->cfg.nblocks; k++ )
    2573             :             {
    2574       13545 :                 q_direction->band_data[j].elevation[k] = 0;
    2575       13545 :                 q_direction->band_data[j].elevation_index[k] = 0;
    2576             :             }
    2577             :         }
    2578        1347 :         nbits = decode_azimuth2D( q_direction, bitstream, coding_subbands, &bit_pos, masa_subframes );
    2579             :     }
    2580       26340 :     nbits = *pbit_pos - bit_pos;
    2581       26340 :     *pbit_pos = bit_pos;
    2582             : 
    2583       26340 :     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       23124 : 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       23124 :     nbits = 0;
    2608       23124 :     bit_pos = *pbit_pos;
    2609       23124 :     byteBuffer = 0;
    2610             : 
    2611       23124 :     j_az = 0;
    2612             :     /* check number of valid indexes to decode */
    2613      115224 :     for ( k = 0; k < masa_subframes; k++ )
    2614             :     {
    2615       92100 :         if ( q_direction->band_data[idx_subband].azimuth_index[k] < MASA_NO_INDEX )
    2616             :         {
    2617       92094 :             j_az++;
    2618             :         }
    2619             :         else
    2620             :         {
    2621           6 :             q_direction->band_data[idx_subband].azimuth[k] = 0.f; /*To be in sync with encoder values.*/
    2622             :         }
    2623             :     }
    2624             : 
    2625       23124 :     if ( j_az == 0 )
    2626             :     {
    2627           0 :         return nbits;
    2628             :     }
    2629             : 
    2630       23124 :     if ( byteBuffer == 0 )
    2631             :     {
    2632             :         /* use context */
    2633       23124 :         use_context = 0;
    2634      115224 :         for ( k = 0; k < masa_subframes; k++ )
    2635             :         {
    2636       92100 :             if ( q_direction->band_data[idx_subband].bits_sph_idx[k] <= 1 )
    2637             :             {
    2638           0 :                 use_context = 1;
    2639             :             }
    2640             :         }
    2641             : 
    2642       23124 :         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       23124 :             byteBuffer = bitstream[bit_pos--];
    2673       23124 :             if ( byteBuffer == 0 ) /* regular GR coding5 */
    2674             :             {
    2675             :                 /* read GR_order */
    2676        7542 :                 byteBuffer = bitstream[bit_pos--];
    2677        7542 :                 nbits += 1;
    2678             : 
    2679       37314 :                 for ( k = 0; k < masa_subframes; k++ )
    2680             :                 {
    2681       29772 :                     if ( q_direction->band_data[idx_subband].bits_sph_idx[k] > 0 )
    2682             :                     {
    2683       29772 :                         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       29766 :                             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       29766 :                             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           6 :                             q_direction->band_data[idx_subband].azimuth[k] = 0;
    2691           6 :                             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       15582 :                 byteBuffer = bitstream[bit_pos--];
    2706             :                 /* read min index value */
    2707       15582 :                 maximum_s( q_direction->band_data[idx_subband].azimuth_m_alphabet, masa_subframes, &max_val );
    2708       15582 :                 min_idx = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, max_val, MASA_GR_ORD_AZ );
    2709             : 
    2710       77910 :                 for ( k = 0; k < masa_subframes; k++ )
    2711             :                 {
    2712       62328 :                     if ( q_direction->band_data[idx_subband].bits_sph_idx[k] > 0 )
    2713             :                     {
    2714       62328 :                         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       62328 :                             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       62328 :                             q_direction->band_data[idx_subband].azimuth_index[k] += min_idx;
    2718       62328 :                             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       23124 :     nbits = *pbit_pos - bit_pos;
    2737             : 
    2738       23124 :     *pbit_pos = bit_pos;
    2739             : 
    2740       23124 :     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       23124 : 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       23124 :     nr_NO_INDEX = 0;
    2766       23124 :     nbits = 0;
    2767       23124 :     bit_pos = *pbit_pos;
    2768             : 
    2769      115224 :     for ( k = 0; k < masa_subframes; k++ )
    2770             :     {
    2771       92100 :         q_direction->band_data[j].elevation_index[k] = 0;
    2772       92100 :         q_direction->band_data[j].elevation[k] = 0;
    2773             : 
    2774       92100 :         if ( q_direction->band_data[j].bits_sph_idx[k] > 0 )
    2775             :         {
    2776       92100 :             if ( q_direction->band_data[j].bits_sph_idx[k] <= 2 )
    2777             :             {
    2778           9 :                 q_direction->band_data[j].elevation_index[k] = MASA_NO_INDEX;
    2779           9 :                 nr_NO_INDEX += 1;
    2780           9 :                 q_direction->band_data[j].elevation[k] = 0;
    2781           9 :                 q_direction->band_data[j].elevation_m_alphabet[k] = 1;
    2782             :             }
    2783             :             else
    2784             :             {
    2785       92091 :                 if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    2786             :                 {
    2787        7467 :                     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       84624 :                     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       23124 :     if ( nr_NO_INDEX < masa_subframes )
    2802             :     {
    2803             :         {
    2804             :             /* read if same or not */
    2805       23124 :             byteBuffer = bitstream[bit_pos--];
    2806       23124 :             if ( byteBuffer == 1 ) /* same value */
    2807             :             {
    2808             :                 /* read value */
    2809       17946 :                 byteBuffer = bitstream[bit_pos--];
    2810       17946 :                 byteBuffer = ( byteBuffer << 1 ) + bitstream[bit_pos--];
    2811       17946 :                 same_idx = byteBuffer;
    2812             : 
    2813       89334 :                 for ( k = 0; k < masa_subframes; k++ )
    2814             :                 {
    2815       71388 :                     if ( q_direction->band_data[j].elevation_index[k] < MASA_NO_INDEX )
    2816             :                     {
    2817       71388 :                         q_direction->band_data[j].elevation_index[k] = same_idx;
    2818       71388 :                         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        5178 :                 byteBuffer = bitstream[bit_pos--];
    2826        5178 :                 GR_ord_elevation = MASA_GR_ORD_EL - byteBuffer;
    2827             : 
    2828       25890 :                 for ( k = 0; k < masa_subframes; k++ )
    2829             :                 {
    2830       20712 :                     if ( q_direction->band_data[j].elevation_index[k] < MASA_NO_INDEX )
    2831             :                     {
    2832       20703 :                         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       20703 :                         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      115224 :     for ( k = 0; k < masa_subframes; k++ )
    2841             :     {
    2842       92100 :         if ( ( q_direction->band_data[j].elevation_index[k] < MASA_NO_INDEX ) &&
    2843       92091 :              ( no_phi_masa[q_direction->band_data[j].bits_sph_idx[k] - 1][q_direction->band_data[j].elevation_index[k]] <= 1 ) )
    2844             :         {
    2845           6 :             q_direction->band_data[j].azimuth_index[k] = MASA_NO_INDEX;
    2846           6 :             q_direction->band_data[j].azimuth_m_alphabet[k] = 1;
    2847             :         }
    2848             :         else
    2849             :         {
    2850       92094 :             q_direction->band_data[j].azimuth_index[k] = 0;
    2851       92094 :             if ( q_direction->band_data[j].elevation_index[k] < MASA_NO_INDEX )
    2852             :             {
    2853       92085 :                 q_direction->band_data[j].azimuth_m_alphabet[k] = no_phi_masa[q_direction->band_data[j].bits_sph_idx[k] - 1][q_direction->band_data[j].elevation_index[k]];
    2854             :             }
    2855             :             else
    2856             :             {
    2857           9 :                 q_direction->band_data[j].azimuth_m_alphabet[k] = no_phi_masa[q_direction->band_data[j].bits_sph_idx[k] - 1][0];
    2858           9 :                 q_direction->band_data[j].elevation_index[k] = 0;
    2859             :             }
    2860             :         }
    2861             :     }
    2862             : 
    2863       23124 :     nbits = *pbit_pos - bit_pos;
    2864       23124 :     *pbit_pos = bit_pos;
    2865             : 
    2866       23124 :     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     1436184 : 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     1436184 :     nbits = 0;
    2889             : 
    2890     6320232 :     for ( m = 0; m < nblocks; m++ )
    2891             :     {
    2892     4884048 :         value = 0;
    2893    25226964 :         for ( i = 0; i < q_direction->band_data[b].bits_sph_idx[m]; i++ )
    2894             :         {
    2895    20342916 :             value = ( value << 1 ) + bitstream[( *pbit_pos )--];
    2896             :         }
    2897             : 
    2898     4884048 :         q_direction->band_data[b].spherical_index[m] = value;
    2899     4884048 :         nbits += q_direction->band_data[b].bits_sph_idx[m];
    2900             : 
    2901     4884048 :         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     1436184 :     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        1347 : 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        1347 :     bit_pos = *pbit_pos;
    2930        1347 :     nbits = 0;
    2931        5784 :     for ( j = 0; j < coding_subbands; j++ )
    2932             :     {
    2933        4437 :         bits_dir0 = (int16_t *) q_direction->band_data[j].bits_sph_idx;
    2934        4437 :         allowed_bits = sum_s( bits_dir0, no_frames );
    2935             : 
    2936        4437 :         if ( allowed_bits > 0 )
    2937             :         {
    2938        4437 :             use_vq = 0;
    2939       17982 :             for ( k = 0; k < no_frames; k++ )
    2940             :             {
    2941       13545 :                 q_direction->band_data[j].elevation[k] = 0;
    2942       13545 :                 q_direction->band_data[j].elevation_index[k] = 0;
    2943             : 
    2944       13545 :                 if ( bits_dir0[k] > use_vq )
    2945             :                 {
    2946        5190 :                     use_vq = bits_dir0[k];
    2947             :                 }
    2948             :             }
    2949             : 
    2950        4437 :             if ( use_vq <= 3 && allowed_bits <= 11 )
    2951             :             {
    2952         921 :                 if ( allowed_bits <= no_frames + 1 )
    2953             :                 {
    2954         426 :                     set_f( q_direction->band_data[j].azimuth, 0.0f, no_frames );
    2955        2049 :                     for ( k = 0; k < min( allowed_bits, no_frames ); k++ )
    2956             :                     {
    2957        1623 :                         q_direction->band_data[j].azimuth[k] = ( -180.f ) * bitstream[bit_pos--];
    2958        1623 :                         nbits += 1;
    2959             :                     }
    2960             :                 }
    2961             :                 else
    2962             :                 {
    2963         495 :                     nbits += read_truncGR_azimuth( bitstream, q_direction, j, no_frames, &bit_pos );
    2964             :                 }
    2965             :             }
    2966             :             else
    2967             :             {
    2968       14097 :                 for ( k = 0; k < no_frames; k++ )
    2969             :                 {
    2970       10581 :                     Buffer = 0;
    2971       59184 :                     for ( i = 0; i < bits_dir0[k]; i++ )
    2972             :                     {
    2973       48603 :                         Buffer = ( Buffer << 1 ) + bitstream[bit_pos--];
    2974             :                     }
    2975             : 
    2976       10581 :                     nbits += bits_dir0[k];
    2977             : 
    2978       10581 :                     if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    2979             :                     {
    2980        6969 :                         q_direction->band_data[j].azimuth[k] = 360.0f / (float) ( 1 << bits_dir0[k] ) * Buffer - 180;
    2981        6969 :                         q_direction->band_data[j].azimuth[k] = companding_azimuth( q_direction->band_data[j].azimuth[k], q_direction->cfg.mc_ls_setup,
    2982        6969 :                                                                                    ( q_direction->band_data[j].elevation[k] > MC_MASA_THR_ELEVATION ), -1 );
    2983             :                     }
    2984             :                     else
    2985             :                     {
    2986        3612 :                         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        1347 :     *pbit_pos = bit_pos;
    2997             : 
    2998        1347 :     return nbits;
    2999             : }
    3000             : 
    3001             : 
    3002             : /*-------------------------------------------------------------------*
    3003             :  * set_zero_direction()
    3004             :  *
    3005             :  *
    3006             :  *-------------------------------------------------------------------*/
    3007             : 
    3008       10653 : 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       53265 :     for ( k = 0; k < len; k++ )
    3016             :     {
    3017       42612 :         q_direction->band_data[idx_band].azimuth[k] = 0;
    3018       42612 :         q_direction->band_data[idx_band].azimuth_index[k] = 0;
    3019       42612 :         q_direction->band_data[idx_band].elevation[k] = 0;
    3020       42612 :         q_direction->band_data[idx_band].elevation_index[k] = 0;
    3021       42612 :         q_direction->band_data[idx_band].spherical_index[k] = 0;
    3022             :     }
    3023             : 
    3024       10653 :     return;
    3025             : }
    3026             : 
    3027             : 
    3028             : /*-------------------------------------------------------------------*
    3029             :  * read_truncGR_azimuth()
    3030             :  *
    3031             :  *
    3032             :  *-------------------------------------------------------------------*/
    3033             : 
    3034       10818 : 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       10818 :     allowed_bits = sum_s( (int16_t *) q_direction->band_data[j].bits_sph_idx, no_subframes );
    3048       10818 :     nbits = 0;
    3049       10818 :     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       10818 :     if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    3068             :     {
    3069        1509 :         no_symb = 9;
    3070             :     }
    3071             :     else
    3072             :     {
    3073        9309 :         no_symb = 8;
    3074             :     }
    3075       10818 :     nbits = 0;
    3076             : 
    3077       10818 :     nbits = *pbit_pos;
    3078             : 
    3079       53451 :     for ( i = 0; i < no_subframes; i++ )
    3080             :     {
    3081       42633 :         idx = ivas_qmetadata_DecodeExtendedGR( bitstream, pbit_pos, no_symb, 0 );
    3082       42633 :         q_direction->band_data[j].azimuth_index[i] = idx;
    3083       42633 :         if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
    3084             :         {
    3085        6018 :             q_direction->band_data[j].azimuth[i] = cb_azi_chan[( idx + 1 ) >> 1];
    3086        6018 :             if ( idx % 2 > 0 )
    3087             :             {
    3088        2370 :                 q_direction->band_data[j].azimuth[i] = -q_direction->band_data[j].azimuth[i];
    3089             :             }
    3090             :         }
    3091             :         else
    3092             :         {
    3093       36615 :             q_direction->band_data[j].azimuth[i] = azimuth_cb[idx];
    3094             :         }
    3095             :     }
    3096             : 
    3097       10818 :     nbits -= *pbit_pos;
    3098             : 
    3099       10818 :     return nbits;
    3100             : }
    3101             : 
    3102             : 
    3103             : /*-------------------------------------------------------------------*
    3104             :  * read_common_direction()
    3105             :  *
    3106             :  *
    3107             :  *-------------------------------------------------------------------*/
    3108             : 
    3109             : /*! r: number of bits read */
    3110       10653 : 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       10653 :     bit_pos = *pbit_pos;
    3126       10653 :     nbits = 0;
    3127             : 
    3128       10653 :     set_zero_direction( q_direction, j, no_subframes );
    3129       10653 :     if ( bits_total == 0 )
    3130             :     {
    3131           0 :         return nbits;
    3132             :     }
    3133             : 
    3134       10653 :     if ( bits_total <= no_subframes + 1 )
    3135             :     {
    3136        1245 :         for ( i = 0; i < min( no_subframes, bits_total ); i++ )
    3137             :         {
    3138         996 :             byteBuffer = bitstream[bit_pos--];
    3139             :             /*qdirection->azimuth_index[j][i] = (uint16_t)byteBuffer; */
    3140         996 :             q_direction->band_data[j].azimuth[i] = azimuth_cb[byteBuffer];
    3141         996 :             nbits += 1;
    3142             :         }
    3143         249 :         *pbit_pos = bit_pos;
    3144             : 
    3145             :         /*nbits += read_truncGR_azimuth(bitstream, qdirection, j, no_subframes, pbit_pos); */
    3146         249 :         return nbits;
    3147             :     }
    3148             : 
    3149             : 
    3150       10404 :     byteBuffer = bitstream[bit_pos--];
    3151       10404 :     bits_el = 1;
    3152       10404 :     nbits += 1;
    3153             :     /* elevation is already set to 0*/
    3154       10404 :     if ( byteBuffer == 1 )
    3155             :     {
    3156        3327 :         byteBuffer = bitstream[bit_pos--];
    3157        3327 :         bits_el += 1;
    3158        3327 :         nbits += 1;
    3159        3327 :         if ( byteBuffer == 0 )
    3160             :         {
    3161       10020 :             for ( i = 0; i < no_subframes; i++ )
    3162             :             {
    3163        8016 :                 q_direction->band_data[j].elevation[i] = delta_theta_masa[2];
    3164             :             }
    3165             :         }
    3166             :         else
    3167             :         {
    3168        1323 :             byteBuffer = bitstream[bit_pos--];
    3169        1323 :             bits_el += 1;
    3170        1323 :             nbits += 1;
    3171        1323 :             if ( byteBuffer == 0 )
    3172             :             {
    3173        6465 :                 for ( i = 0; i < no_subframes; i++ )
    3174             :                 {
    3175        5172 :                     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          30 :                 byteBuffer = bitstream[bit_pos--];
    3182          30 :                 nbits += 1;
    3183          30 :                 if ( byteBuffer == 0 )
    3184             :                 {
    3185          24 :                     set_f( q_direction->band_data[j].elevation, 90.0f, no_subframes );
    3186          24 :                     set_f( q_direction->band_data[j].azimuth, 0.0f, no_subframes );
    3187             :                 }
    3188             :                 else
    3189             :                 {
    3190           6 :                     set_f( q_direction->band_data[j].elevation, -90.0f, no_subframes );
    3191           6 :                     set_f( q_direction->band_data[j].azimuth, 0.0f, no_subframes );
    3192             :                 }
    3193          30 :                 *pbit_pos = bit_pos;
    3194             : 
    3195          30 :                 return nbits;
    3196             :             }
    3197             :         }
    3198             :     }
    3199             : 
    3200       10374 :     bits_el = sum_s( (int16_t *) q_direction->band_data[j].bits_sph_idx, no_subframes ) - bits_el;
    3201             : 
    3202       10374 :     if ( bits_el <= no_subframes + 1 )
    3203             :     {
    3204          51 :         nbits += min( no_subframes, bits_el );
    3205         252 :         for ( i = 0; i < min( no_subframes, bits_el ); i++ )
    3206             :         {
    3207         201 :             byteBuffer = bitstream[bit_pos--];
    3208             :             /*qdirection->azimuth_index[j][i] = (uint16_t) byteBuffer; */
    3209         201 :             q_direction->band_data[j].azimuth[i] = azimuth_cb[byteBuffer];
    3210             :         }
    3211             :     }
    3212             :     else
    3213             :     {
    3214       10323 :         nbits += read_truncGR_azimuth( bitstream, q_direction, j, no_subframes, &bit_pos );
    3215             :     }
    3216             : 
    3217       10374 :     *pbit_pos = bit_pos;
    3218             : 
    3219             : #ifdef DEBUGGING
    3220             :     /*assert(nbits == bits_total); */
    3221             : #endif
    3222       10374 :     return nbits;
    3223             : }
    3224             : 
    3225             : 
    3226             : /*-----------------------------------------------------------------------*
    3227             :  * Local functions: coherence
    3228             :  *-----------------------------------------------------------------------*/
    3229             : 
    3230       68784 : 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       68784 :     coding_subbands_0 = hQMetaData->q_direction[0].cfg.nbands;
    3247       68784 :     coding_subbands = hQMetaData->q_direction[idx_d].cfg.nbands;
    3248       68784 :     if ( coding_subbands_0 <= 5 )
    3249             :     {
    3250      303174 :         for ( j = 0; j < 5; j++ )
    3251             :         {
    3252      252645 :             MASA_grouping[j] = j;
    3253             :         }
    3254             :     }
    3255             :     else
    3256             :     {
    3257       18255 :         if ( coding_subbands_0 <= 8 )
    3258             :         {
    3259        7470 :             mvs2s( MASA_grouping_8_to_5, MASA_grouping, 8 );
    3260             :         }
    3261       10785 :         else if ( coding_subbands_0 <= 12 )
    3262             :         {
    3263        4473 :             mvs2s( MASA_grouping_12_to_5, MASA_grouping, 12 );
    3264             :         }
    3265        6312 :         else if ( coding_subbands_0 <= 18 )
    3266             :         {
    3267        2796 :             mvs2s( MASA_grouping_18_to_5, MASA_grouping, 18 );
    3268             :         }
    3269             :         else
    3270             :         {
    3271        3516 :             if ( coding_subbands_0 <= 24 )
    3272             :             {
    3273        3516 :                 mvs2s( MASA_grouping_24_to_5, MASA_grouping, 24 );
    3274             :             }
    3275             :         }
    3276             :     }
    3277             : 
    3278       68784 :     if ( coding_subbands < coding_subbands_0 )
    3279             :     {
    3280       11106 :         d = 0;
    3281       87246 :         for ( j = 0; j < coding_subbands_0; j++ )
    3282             :         {
    3283       76140 :             if ( hQMetaData->twoDirBands[j] == 1 )
    3284             :             {
    3285       30165 :                 two_dir_band[d++] = j;
    3286             :             }
    3287             :         }
    3288             :     }
    3289             :     else
    3290             :     {
    3291       57678 :         set_s( two_dir_band, 0, coding_subbands );
    3292             :     }
    3293             : 
    3294       68784 :     q_direction = &hQMetaData->q_direction[idx_d];
    3295             : 
    3296      507702 :     for ( i = 0; i < coding_subbands; i++ )
    3297             :     {
    3298      438918 :         var_azi = var( q_direction->band_data[i].azimuth, no_frames );
    3299             : 
    3300      438918 :         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      438918 :             min_index = q_direction->band_data[i].energy_ratio_index[0];
    3308             :         }
    3309             : 
    3310      438918 :         if ( var_azi < MASA_DELTA_AZI_DCT0 )
    3311             :         {
    3312      109731 :             idx_sub_cb = MASA_NO_CV_COH * min_index;
    3313             :         }
    3314             :         else
    3315             :         {
    3316      329187 :             idx_sub_cb = MASA_NO_CV_COH * ( min_index + DIRAC_DIFFUSE_LEVELS ); /* NO_CV_COH = 8 */
    3317             :         }
    3318             : 
    3319      438918 :         dct_coh[i][0] = coherence_cb0_masa[idx_sub_cb + q_direction->coherence_band_data[i].spread_coherence_dct0_index];
    3320             : 
    3321      438918 :         if ( coding_subbands < coding_subbands_0 )
    3322             :         {
    3323       30165 :             assert( idx_d == 1 );
    3324       30165 :             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      408753 :             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     1316754 :         for ( j = 2; j < MAX_PARAM_SPATIAL_SUBFRAMES; j++ )
    3332             :         {
    3333      877836 :             dct_coh[i][j] = 0;
    3334             :         }
    3335             : 
    3336      438918 :         invdct4_transform( dct_coh[i], q_direction->coherence_band_data[i].spread_coherence );
    3337             :     }
    3338             : 
    3339       68784 :     return;
    3340             : }
    3341             : 
    3342             : 
    3343             : /*-------------------------------------------------------------------*
    3344             :  * read_huf()
    3345             :  *
    3346             :  * Read Hufman code
    3347             :  *-------------------------------------------------------------------*/
    3348             : 
    3349             : /*! r: number of bits read */
    3350       68784 : 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       68784 :     int16_t done = 0, end_pos;
    3361             :     uint16_t ByteBuffer;
    3362             :     int16_t nbits, val;
    3363             :     uint16_t i;
    3364             : 
    3365       68784 :     end_pos = start_pos;
    3366       68784 :     nbits = 0;
    3367       68784 :     val = 0;
    3368      338859 :     while ( ( done == 0 ) && ( nbits < max_len ) )
    3369             :     {
    3370      270075 :         ByteBuffer = bitstream[end_pos--];
    3371      270075 :         val = val * 2 + ( ByteBuffer & 1 );
    3372      270075 :         nbits += 1;
    3373     1279788 :         for ( i = 0; i < len; i++ )
    3374             :         {
    3375     1078497 :             if ( val == huff_code[i] )
    3376             :             {
    3377       68784 :                 *out = i;
    3378       68784 :                 done = 1;
    3379       68784 :                 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       68784 :     *num_bits_read = end_pos;
    3391             : 
    3392       68784 :     return IVAS_ERR_OK;
    3393             : }
    3394             : 
    3395             : 
    3396             : /*-------------------------------------------------------------------*
    3397             :  * read_GR_min_removed_data()
    3398             :  *
    3399             :  *
    3400             :  *-------------------------------------------------------------------*/
    3401             : 
    3402        7299 : 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        7299 :     bit_pos = *p_bit_pos;
    3417             : 
    3418             :     /* read GR order */
    3419        7299 :     byteBuffer = bitstream[bit_pos--];
    3420        7299 :     nbits = 1;
    3421             : 
    3422             :     /* read min index */
    3423        7299 :     bits_GR = bit_pos;
    3424        7299 :     min_index = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_symb, 0 );
    3425        7299 :     nbits += bits_GR - bit_pos;
    3426             : 
    3427             :     /* read GR data */
    3428      101436 :     for ( j = 0; j < no_data; j++ )
    3429             :     {
    3430       94137 :         bits_GR = bit_pos;
    3431       94137 :         if ( no_cv_vec[j] > 1 )
    3432             :         {
    3433       93219 :             decoded_idx[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_cv_vec[j] - min_index, ( byteBuffer & 1 ) );
    3434       93219 :             nbits += bits_GR - bit_pos;
    3435             :         }
    3436             :         else
    3437             :         {
    3438         918 :             decoded_idx[j] = 0;
    3439             :         }
    3440             :     }
    3441             : 
    3442      101436 :     for ( j = 0; j < no_data; j++ )
    3443             :     {
    3444       94137 :         if ( no_cv_vec[j] > 1 )
    3445             :         {
    3446       93219 :             decoded_idx[j] += min_index;
    3447             :         }
    3448             :     }
    3449             : 
    3450             : #ifdef DEBUGGING
    3451             :     assert( nbits == *p_bit_pos - bit_pos );
    3452             : #endif
    3453        7299 :     *p_bit_pos = bit_pos;
    3454             : 
    3455        7299 :     return nbits;
    3456             : }
    3457             : 
    3458             : 
    3459             : /*-------------------------------------------------------------------*
    3460             :  * decode_fixed_rate_composed_index_coherence()
    3461             :  *
    3462             :  *
    3463             :  *-------------------------------------------------------------------*/
    3464             : 
    3465       32148 : 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       32148 :     bit_pos = *p_bit_pos;
    3485       32148 :     set_s( (int16_t *) temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
    3486             : 
    3487       32148 :     no_cb = 1;
    3488       32148 :     nbits = 0;
    3489       32148 :     if ( no_bands > MASA_LIMIT_NO_BANDS_SUR_COH )
    3490             :     {
    3491             :         /* read 8-max_val with GR0 */
    3492        1986 :         bits_GR = bit_pos;
    3493        1986 :         no_vals_local = no_symb - ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_symb, 0 );
    3494        1986 :         nbits += bits_GR - bit_pos;
    3495       36954 :         for ( j = 0; j < no_bands; j++ )
    3496             :         {
    3497       34968 :             if ( no_cv_vec[j] > no_vals_local )
    3498             :             {
    3499       31029 :                 no_cv_vec[j] = no_vals_local;
    3500             :             }
    3501             :         }
    3502             :     }
    3503             : 
    3504       32148 :     half_no_bands = no_bands / 2;
    3505       32148 :     if ( sum_s( no_cv_vec, no_bands ) > MASA_COH_LIMIT_2IDX )
    3506             :     {
    3507         381 :         no_cb = 1;
    3508             : 
    3509        4635 :         for ( j = 0; j < half_no_bands; j++ )
    3510             :         {
    3511        4254 :             no_cb *= no_cv_vec[j];
    3512             :         }
    3513         381 :         no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3514         381 :         no_cb = 1;
    3515        4683 :         for ( j = half_no_bands; j < no_bands; j++ )
    3516             :         {
    3517        4302 :             no_cb *= no_cv_vec[j];
    3518             :         }
    3519         381 :         no_bits_vec1 = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3520             :     }
    3521             :     else
    3522             :     {
    3523       31767 :         no_cb = 1;
    3524      191604 :         for ( j = 0; j < no_bands; j++ )
    3525             :         {
    3526      159837 :             no_cb *= no_cv_vec[j];
    3527             :         }
    3528       31767 :         no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3529       31767 :         no_bits_vec1 = 0;
    3530             :     }
    3531       32148 :     if ( no_bits_vec1 > 0 )
    3532             :     {
    3533         381 :         idx_fr = 0;
    3534       11655 :         for ( j = 0; j < no_bits_vec; j++ )
    3535             :         {
    3536       11274 :             idx_fr = ( idx_fr << 1 ) + bitstream[bit_pos--];
    3537             :         }
    3538             : 
    3539         381 :         nbits += no_bits_vec;
    3540             : 
    3541         381 :         decode_combined_index( idx_fr, no_cv_vec, temp_index, half_no_bands );
    3542             : 
    3543         381 :         idx_fr = 0;
    3544       12150 :         for ( j = 0; j < no_bits_vec1; j++ )
    3545             :         {
    3546       11769 :             idx_fr = ( idx_fr << 1 ) + bitstream[bit_pos--];
    3547             :         }
    3548         381 :         nbits += no_bits_vec1;
    3549         381 :         decode_combined_index( idx_fr, &no_cv_vec[half_no_bands], &temp_index[half_no_bands], half_no_bands );
    3550             :     }
    3551             :     else
    3552             :     {
    3553       31767 :         idx_fr = 0;
    3554      297105 :         for ( j = 0; j < no_bits_vec; j++ )
    3555             :         {
    3556      265338 :             idx_fr = ( idx_fr << 1 ) + bitstream[bit_pos--];
    3557             :         }
    3558       31767 :         nbits += no_bits_vec;
    3559       31767 :         decode_combined_index( idx_fr, no_cv_vec, temp_index, no_bands );
    3560             :     }
    3561             : 
    3562      200541 :     for ( j = 0; j < no_bands; j++ )
    3563             :     {
    3564      168393 :         decoded_index[j] = temp_index[j];
    3565             :     }
    3566             : #ifdef DEBUGGING
    3567             :     assert( nbits == *p_bit_pos - bit_pos );
    3568             : #endif
    3569       32148 :     nbits = *p_bit_pos - bit_pos;
    3570             : 
    3571       32148 :     *p_bit_pos = bit_pos;
    3572             : 
    3573       32148 :     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       10434 : 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       10434 :     nbands = hQMetaData->q_direction[idx_dir].cfg.nbands;
    3599       10434 :     nblocks = hQMetaData->q_direction[idx_dir].cfg.nblocks;
    3600             : 
    3601       10434 :     cb_size = 1 << nbits_coh;
    3602       10434 :     delta = 256.0f / cb_size;
    3603       10434 :     nbits = *p_bit_pos;
    3604       49632 :     for ( k = 0; k < nblocks; k++ )
    3605             :     {
    3606             :         /* read method */
    3607       39198 :         if ( bitstream[( *p_bit_pos )--] == 1 )
    3608             :         {
    3609             :             /* average removed */
    3610             :             /* read average index */
    3611         405 :             min_index = 0;
    3612        1686 :             for ( i = 0; i < nbits_coh; i++ )
    3613             :             {
    3614        1281 :                 min_index = ( min_index << 1 ) + bitstream[( *p_bit_pos )--];
    3615             :             }
    3616             :             /* read GR param */
    3617         405 :             GR_param = bitstream[( *p_bit_pos )--];
    3618        5508 :             for ( j = 0; j < nbands; j++ )
    3619             :             {
    3620        5103 :                 decoded_idx = ivas_qmetadata_DecodeExtendedGR( bitstream, p_bit_pos, 2 * cb_size, GR_param );
    3621        5103 :                 if ( decoded_idx % 2 )
    3622             :                 {
    3623        2136 :                     decoded_idx = ( ( decoded_idx + 1 ) >> 1 ) + min_index;
    3624             :                 }
    3625             :                 else
    3626             :                 {
    3627        2967 :                     decoded_idx = -( decoded_idx >> 1 ) + min_index;
    3628             :                 }
    3629        5103 :                 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       38793 :             min_index = 0;
    3636      169146 :             for ( i = 0; i < nbits_coh; i++ )
    3637             :             {
    3638      130353 :                 min_index = ( min_index << 1 ) + bitstream[( *p_bit_pos )--];
    3639             :             }
    3640             : 
    3641             :             /* read GR param */
    3642       38793 :             GR_param = bitstream[( *p_bit_pos )--];
    3643      837975 :             for ( j = 0; j < nbands; j++ )
    3644             :             {
    3645      799182 :                 decoded_idx = ivas_qmetadata_DecodeExtendedGR( bitstream, p_bit_pos, cb_size - min_index, GR_param ) + min_index;
    3646      799182 :                 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       10434 :     nbits = nbits - *p_bit_pos;
    3652             : 
    3653       10434 :     return nbits;
    3654             : }
    3655             : 
    3656             : 
    3657             : /*------------------------------------------------------------------- *
    3658             :  * read_coherence_data()
    3659             :  *
    3660             :  * Read coherence data
    3661             :  *------------------------------------------------------------------- */
    3662             : 
    3663             : /*! r: number of bits read */
    3664       93156 : 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       93156 :     coding_subbands = hQMetaData->q_direction[idx_dir].cfg.nbands;
    3691       93156 :     extra_cv = (int16_t) ( coding_subbands / MASA_FACTOR_CV_COH );
    3692       93156 :     q_direction = &( hQMetaData->q_direction[idx_dir] );
    3693       93156 :     bit_pos = *p_bit_pos;
    3694       93156 :     nbits = 0;
    3695             : 
    3696       93156 :     if ( q_direction->cfg.nblocks == 1 )
    3697             :     {
    3698      206847 :         for ( j = 0; j < coding_subbands; j++ )
    3699             :         {
    3700      182475 :             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      182475 :                 idx_ER = 7 - q_direction->band_data[j].energy_ratio_index_mod[0] + extra_cv;
    3707             :             }
    3708      182475 :             no_cv_vec[j] = idx_ER + 1;
    3709             :         }
    3710             : 
    3711       24372 :         if ( sum_s( no_cv_vec, coding_subbands ) == coding_subbands )
    3712             :         {
    3713       14589 :             for ( j = 0; j < coding_subbands; j++ )
    3714             :             {
    3715       12012 :                 q_direction->coherence_band_data[j].spread_coherence[0] = 0;
    3716             :             }
    3717             : 
    3718        2577 :             return 0;
    3719             :         }
    3720       21795 :         byteBuffer = bitstream[bit_pos--];
    3721       21795 :         nbits += 1;
    3722             : 
    3723       21795 :         if ( byteBuffer & 1 )
    3724             :         {
    3725             :             /* decode GR min removed */
    3726        7299 :             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      101436 :             for ( j = 0; j < coding_subbands; j++ )
    3728             :             {
    3729       94137 :                 if ( no_cv_vec[j] > 1 )
    3730             :                 {
    3731       93219 :                     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         918 :                     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       14496 :             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       90822 :             for ( j = 0; j < coding_subbands; j++ )
    3745             :             {
    3746       76326 :                 if ( no_cv_vec[j] > 1 )
    3747             :                 {
    3748       42750 :                     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       33576 :                     q_direction->coherence_band_data[j].spread_coherence[0] = 0;
    3753             :                 }
    3754             :             }
    3755             :         }
    3756             :     }
    3757             :     else
    3758             :     {
    3759      507702 :         for ( j = 0; j < coding_subbands; j++ )
    3760             :         {
    3761      438918 :             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      438918 :                 no_cv_vec[j] = len_cb_dct0_masa[q_direction->band_data[j].energy_ratio_index[0]]; /* spread coherence DCT0*/
    3769             :             }
    3770             :         }
    3771             : 
    3772       68784 :         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         519 :             no_cb = 1;
    3777             : #ifdef DEBUGGING
    3778             :             assert( coding_subbands % 2 == 0 );
    3779             : #endif
    3780        6747 :             for ( j = 0; j < coding_subbands / 2; j++ )
    3781             :             {
    3782        6228 :                 no_cb *= no_cv_vec[j];
    3783             :             }
    3784             : 
    3785         519 :             no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3786         519 :             no_cb = 1;
    3787             : 
    3788        6747 :             for ( j = coding_subbands / 2; j < coding_subbands; j++ )
    3789             :             {
    3790        6228 :                 no_cb *= no_cv_vec[j];
    3791             :             }
    3792             : 
    3793         519 :             no_bits_vec1 = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3794         519 :             dct0_index = 0;
    3795             : 
    3796       14070 :             for ( j = 0; j < no_bits_vec; j++ )
    3797             :             {
    3798       13551 :                 dct0_index = ( dct0_index << 1 ) + bitstream[bit_pos--];
    3799             :             }
    3800             : 
    3801         519 :             nbits += no_bits_vec;
    3802         519 :             set_s( (int16_t *) spr_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
    3803             : 
    3804         519 :             decode_combined_index( dct0_index, no_cv_vec, spr_coh_temp_index, coding_subbands / 2 );
    3805             : 
    3806         519 :             dct0_index = 0;
    3807       12834 :             for ( j = 0; j < no_bits_vec1; j++ )
    3808             :             {
    3809       12315 :                 dct0_index = ( dct0_index << 1 ) + bitstream[bit_pos--];
    3810             :             }
    3811             : 
    3812         519 :             nbits += no_bits_vec1;
    3813             : 
    3814         519 :             decode_combined_index( dct0_index, &no_cv_vec[coding_subbands / 2], &spr_coh_temp_index[coding_subbands / 2], coding_subbands / 2 );
    3815             : 
    3816       12975 :             for ( j = 0; j < coding_subbands; j++ )
    3817             :             {
    3818       12456 :                 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       68265 :             no_cb = 1;
    3827             : 
    3828      494727 :             for ( j = 0; j < coding_subbands; j++ )
    3829             :             {
    3830      426462 :                 no_cb *= no_cv_vec[j];
    3831             :             }
    3832             : 
    3833       68265 :             no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3834             : 
    3835             :             /* read joint index for DCT0 */
    3836       68265 :             no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
    3837       68265 :             dct0_index = 0;
    3838             : 
    3839      792003 :             for ( j = 0; j < no_bits_vec; j++ )
    3840             :             {
    3841      723738 :                 dct0_index = ( dct0_index << 1 ) + bitstream[bit_pos--];
    3842             :             }
    3843             : 
    3844       68265 :             nbits += no_bits_vec;
    3845             : 
    3846       68265 :             set_s( (int16_t *) spr_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
    3847             : 
    3848       68265 :             decode_combined_index( dct0_index, no_cv_vec, spr_coh_temp_index, coding_subbands );
    3849             : 
    3850      494727 :             for ( j = 0; j < coding_subbands; j++ )
    3851             :             {
    3852      426462 :                 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      507702 :         for ( j = 0; j < coding_subbands; j++ )
    3858             :         {
    3859      438918 :             bits_GR = bit_pos;
    3860      438918 :             idx_dct1[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, 2 * MASA_NO_CV_COH1, 0 );
    3861      438918 :             nbits += bits_GR - bit_pos;
    3862             :         }
    3863       68784 :         bits_GR = bit_pos; /* just to store the data */
    3864             : 
    3865             :         /* read average index */
    3866       68784 :         read_huf( &bit_pos, bitstream, &av_index, bit_pos, MASA_NO_CV_COH1, huff_code_av_masa, 10 ); /* 10 is MAX_LEN*/
    3867       68784 :         nbits += ( bits_GR - bit_pos );
    3868             : 
    3869             :         /* write indexes in metadata structure  */
    3870      507702 :         for ( j = 0; j < coding_subbands; j++ )
    3871             :         {
    3872      438918 :             if ( idx_dct1[j] % 2 )
    3873             :             {
    3874        9843 :                 q_direction->coherence_band_data[j].spread_coherence_dct1_index = ( idx_dct1[j] + 1 ) / 2 + av_index;
    3875             :             }
    3876             :             else
    3877             :             {
    3878      429075 :                 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       90579 :     nbits = *p_bit_pos - bit_pos;
    3886             : 
    3887       90579 :     *p_bit_pos = bit_pos;
    3888             : 
    3889       90579 :     return nbits;
    3890             : }
    3891             : 
    3892             : 
    3893             : /*-------------------------------------------------------------------*
    3894             :  * read_surround_coherence()
    3895             :  *
    3896             :  *
    3897             :  *-------------------------------------------------------------------*/
    3898             : 
    3899       79086 : 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       79086 :     coding_subbands = hQMetaData->q_direction[0].cfg.nbands;
    3918       79086 :     q_direction = hQMetaData->q_direction;
    3919             : 
    3920       79086 :     bits_sur_coherence = 0;
    3921       79086 :     bit_pos = *p_bit_pos;
    3922             : 
    3923       79086 :     d = 0;
    3924      658920 :     for ( j = 0; j < coding_subbands; j++ )
    3925             :     {
    3926      579834 :         error_ratio_surr = 1.0f;
    3927             : 
    3928      579834 :         if ( hQMetaData->no_directions == 2 )
    3929             :         {
    3930      116928 :             d += hQMetaData->twoDirBands[j];
    3931      116928 :             idx = max( d - 1, 0 );
    3932      116928 :             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      462906 :             error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[0];
    3937             :         }
    3938             : 
    3939      579834 :         if ( error_ratio_surr <= 0 )
    3940             :         {
    3941         498 :             error_ratio_surr = 0;
    3942         498 :             no_cv_vec[j] = 1;
    3943         498 :             idx_ER[j] = masa_sq( 0.0f, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
    3944             :         }
    3945             :         else
    3946             :         {
    3947      579336 :             idx_ER[j] = masa_sq( error_ratio_surr, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
    3948      579336 :             no_cv_vec[j] = idx_cb_sur_coh_masa[idx_ER[j]] + 2;
    3949             :         }
    3950             :     }
    3951             : 
    3952       79086 :     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       79086 :     byteBuffer = bitstream[bit_pos--];
    3971       79086 :     bits_sur_coherence += 1;
    3972             : 
    3973       79086 :     if ( byteBuffer & 1 )
    3974             :     {
    3975             :         /* GR decoding */
    3976             :         /* read GR order */
    3977       61953 :         byteBuffer = bitstream[bit_pos--];
    3978       61953 :         bits_sur_coherence += 1;
    3979             : 
    3980             :         /* read min index */
    3981       61953 :         bits_GR = bit_pos;
    3982       61953 :         min_index = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, MASA_MAX_NO_CV_SUR_COH, 0 );
    3983       61953 :         bits_sur_coherence += bits_GR - bit_pos;
    3984             : 
    3985             :         /* read GR data */
    3986      562026 :         for ( j = 0; j < coding_subbands; j++ )
    3987             :         {
    3988      500073 :             bits_GR = bit_pos;
    3989             :             /* decoding for min removed */
    3990      500073 :             if ( no_cv_vec[j] > 1 )
    3991             :             {
    3992      499635 :                 idx_sur_coh[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_cv_vec[j] - min_index, ( byteBuffer & 1 ) );
    3993      499635 :                 bits_sur_coherence += bits_GR - bit_pos;
    3994             :             }
    3995             :             else
    3996             :             {
    3997         438 :                 idx_sur_coh[j] = 0;
    3998             :             }
    3999             :         }
    4000             : 
    4001      562026 :         for ( j = 0; j < coding_subbands; j++ )
    4002             :         {
    4003      500073 :             if ( no_cv_vec[j] > 1 )
    4004             :             {
    4005      499635 :                 hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j] + min_index;
    4006             :             }
    4007             :             else
    4008             :             {
    4009         438 :                 hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j];
    4010             :             }
    4011             : 
    4012      500073 :             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       17133 :         set_s( (int16_t *) sur_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
    4020             : 
    4021       17133 :         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       96894 :         for ( j = 0; j < coding_subbands; j++ )
    4024             :         {
    4025       79761 :             hQMetaData->surcoh_band_data[j].sur_coherence_index = sur_coh_temp_index[j];
    4026             :         }
    4027             : 
    4028             :         /* deindex surround coherence */
    4029       96894 :         for ( j = 0; j < coding_subbands; j++ )
    4030             :         {
    4031       79761 :             if ( no_cv_vec[j] > 1 )
    4032             :             {
    4033       71721 :                 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        8040 :                 hQMetaData->surcoh_band_data[j].surround_coherence[0] = 0;
    4038             :             }
    4039             :         }
    4040             :     }
    4041             : 
    4042      658920 :     for ( j = 0; j < coding_subbands; j++ )
    4043             :     {
    4044     2319336 :         for ( k = 1; k < MAX_PARAM_SPATIAL_SUBFRAMES; k++ )
    4045             :         {
    4046     1739502 :             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       79086 :     bits_sur_coherence = *p_bit_pos - bit_pos;
    4052       79086 :     *p_bit_pos = bit_pos;
    4053             : 
    4054       79086 :     return bits_sur_coherence;
    4055             : }
    4056             : 
    4057             : 
    4058             : /*-------------------------------------------------------------------*
    4059             :  * read_surround_coherence_hr()
    4060             :  *
    4061             :  *
    4062             :  *-------------------------------------------------------------------*/
    4063             : 
    4064        6717 : 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        6717 :     coding_subbands = hQMetaData->q_direction[0].cfg.nbands;
    4085        6717 :     q_direction = hQMetaData->q_direction;
    4086             : 
    4087        6717 :     bits_sur_coherence = 0;
    4088        6717 :     bit_pos = *p_bit_pos;
    4089             : 
    4090       32316 :     for ( sf = 0; sf < hQMetaData->q_direction[0].cfg.nblocks; sf++ )
    4091             :     {
    4092       25599 :         d = 0;
    4093      637824 :         for ( j = 0; j < coding_subbands; j++ )
    4094             :         {
    4095      612225 :             error_ratio_surr = 1.0f;
    4096      612225 :             if ( hQMetaData->no_directions == 2 )
    4097             :             {
    4098      324225 :                 d += hQMetaData->twoDirBands[j];
    4099      324225 :                 idx = max( d - 1, 0 );
    4100      324225 :                 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      288000 :                 error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[sf];
    4105             :             }
    4106             : 
    4107      612225 :             int_error_ratio_surr = (int32_t) ( MASA_SUR_COH_PRECISION * error_ratio_surr );
    4108      612225 :             error_ratio_surr = (float) ( int_error_ratio_surr * MASA_SUR_COH_THRESHOLD );
    4109             : 
    4110      612225 :             if ( error_ratio_surr <= 0 )
    4111             :             {
    4112        4926 :                 error_ratio_surr = 0;
    4113        4926 :                 no_cv_vec[j] = 1;
    4114        4926 :                 idx_ER[j] = 0;
    4115             :             }
    4116             :             else
    4117             :             {
    4118      607299 :                 idx_ER[j] = 7; /* masa_sq( error_ratio_surr, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS ); */
    4119      607299 :                 no_cv_vec[j] = idx_cb_sur_coh_masa[idx_ER[j]] + 2;
    4120             :             }
    4121             :         }
    4122             : 
    4123       25599 :         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       25599 :             byteBuffer = bitstream[bit_pos--];
    4141       25599 :             bits_sur_coherence += 1;
    4142             : 
    4143       25599 :             if ( byteBuffer & 1 )
    4144             :             {
    4145             :                 /* GR decoding */
    4146             :                 /* read GR order */
    4147       25080 :                 byteBuffer = bitstream[bit_pos--];
    4148       25080 :                 bits_sur_coherence += 1;
    4149             : 
    4150             :                 /* read min index */
    4151       25080 :                 bits_GR = bit_pos;
    4152       25080 :                 min_index = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, MASA_MAX_NO_CV_SUR_COH, 0 );
    4153       25080 :                 bits_sur_coherence += bits_GR - bit_pos;
    4154             : 
    4155             :                 /* read GR data */
    4156      624999 :                 for ( j = 0; j < coding_subbands; j++ )
    4157             :                 {
    4158      599919 :                     bits_GR = bit_pos;
    4159             :                     /* decoding for min removed */
    4160      599919 :                     if ( no_cv_vec[j] > 1 )
    4161             :                     {
    4162      595065 :                         idx_sur_coh[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_cv_vec[j] - min_index, ( byteBuffer & 1 ) );
    4163      595065 :                         bits_sur_coherence += bits_GR - bit_pos;
    4164             :                     }
    4165             :                     else
    4166             :                     {
    4167        4854 :                         idx_sur_coh[j] = 0;
    4168             :                     }
    4169             :                 }
    4170             : 
    4171      624999 :                 for ( j = 0; j < coding_subbands; j++ )
    4172             :                 {
    4173      599919 :                     if ( no_cv_vec[j] > 1 )
    4174             :                     {
    4175      595065 :                         hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j] + min_index;
    4176      595065 :                         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        4854 :                         hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j];
    4181        4854 :                         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         519 :                 set_s( (int16_t *) sur_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
    4190             : 
    4191         519 :                 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       12825 :                 for ( j = 0; j < coding_subbands; j++ )
    4194             :                 {
    4195       12306 :                     hQMetaData->surcoh_band_data[j].sur_coherence_index = sur_coh_temp_index[j];
    4196             :                 }
    4197             : 
    4198             :                 /* deindex surround coherence */
    4199       12825 :                 for ( j = 0; j < coding_subbands; j++ )
    4200             :                 {
    4201       12306 :                     if ( no_cv_vec[j] > 1 )
    4202             :                     {
    4203       12021 :                         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         285 :                         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        6717 :     bits_sur_coherence = *p_bit_pos - bit_pos;
    4216        6717 :     *p_bit_pos = bit_pos;
    4217             : 
    4218        6717 :     return bits_sur_coherence;
    4219             : }
    4220             : 
    4221             : 
    4222             : /*-------------------------------------------------------------------*
    4223             :  * decode_combined_index()
    4224             :  *
    4225             :  * Decode combined index into several indexes
    4226             :  *-------------------------------------------------------------------*/
    4227             : 
    4228      101832 : 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      101832 :     base[0] = 1;
    4239      607263 :     for ( i = 1; i < len; i++ )
    4240             :     {
    4241      505431 :         base[i] = base[i - 1] * no_cv_vec[i - 1];
    4242             :     }
    4243             : 
    4244      607263 :     for ( i = len - 1; i > 0; i-- )
    4245             :     {
    4246      505431 :         index[i] = (uint16_t) ( comb_index / base[i] );
    4247      505431 :         comb_index -= index[i] * base[i];
    4248             :     }
    4249             : 
    4250      101832 :     index[0] = (uint16_t) comb_index;
    4251             : 
    4252      101832 :     return;
    4253             : }
    4254             : 
    4255             : 
    4256        5502 : 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        5502 :     step_fx = STEP_M2T_FX;
    4269        5502 :     sign = 1;
    4270        5502 :     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        5502 :     set_s( q_idx, 0, len_stream );
    4281             :     /* read DCT 0 component */
    4282       38514 :     for ( i = 0; i < BITS_MASA2TOTTAL_DCT0; i++ )
    4283             :     {
    4284       33012 :         q_idx[0] = ( q_idx[0] << 1 ) + bit_stream[( *index )--];
    4285             :     }
    4286        5502 :     q_idx[0] *= sign;
    4287             : 
    4288        5502 :     if ( q_idx[0] != 0 )
    4289             :     {
    4290        5502 :         if ( len_stream >= 8 )
    4291             :         {
    4292             :             /* read index of last index encoded with GR2 */
    4293        4770 :             i_min = 0;
    4294        4770 :             j = 4;
    4295       23850 :             for ( i = 0; i < j; i++ )
    4296             :             {
    4297       19080 :                 i_min = ( i_min << 1 ) + bit_stream[( *index )--];
    4298             :             }
    4299             : 
    4300             :             /* read GR orders */
    4301        4770 :             GR1 = bit_stream[( *index )--] + 1;
    4302        4770 :             if ( GR1 == 2 )
    4303             :             {
    4304        3645 :                 GR2 = bit_stream[( *index )--];
    4305             :             }
    4306             :             else
    4307             :             {
    4308        1125 :                 GR2 = 0;
    4309             :             }
    4310             : 
    4311             :             /* read GR data */
    4312       27366 :             for ( i = 1; i <= i_min; i++ )
    4313             :             {
    4314       22596 :                 q_idx[i] = ivas_qmetadata_DecodeExtendedGR( bit_stream, index, 100, GR1 );
    4315             :             }
    4316       64164 :             for ( i = i_min + 1; i < len_stream; i++ )
    4317             :             {
    4318       59394 :                 q_idx[i] = ivas_qmetadata_DecodeExtendedGR( bit_stream, index, 100, GR2 );
    4319             :             }
    4320             :         }
    4321             :         else
    4322             :         {
    4323             :             /* read GR order (only one)  */
    4324         732 :             GR1 = bit_stream[( *index )--];
    4325        3591 :             for ( i = 1; i < len_stream; i++ )
    4326             :             {
    4327        2859 :                 q_idx[i] = ivas_qmetadata_DecodeExtendedGR( bit_stream, index, 100, GR1 );
    4328             :             }
    4329             :         }
    4330             :     }
    4331             : 
    4332             :     /* deindex */
    4333        5502 :     q_dct_data_fx[0] = (Word32) ( ( step_fx * q_idx[0] ) >> 6 ); /* Q25 */
    4334       90351 :     for ( i = 1; i < len_stream; i++ )
    4335             :     {
    4336       84849 :         if ( ( q_idx[i] & 1 ) == 0 )
    4337             :         {
    4338       59211 :             q_dct_data_fx[i] = (Word32) ( ( step_fx * ( -q_idx[i] ) ) >> 7 ); /* Q25 */
    4339             :         }
    4340             :         ELSE
    4341             :         {
    4342       25638 :             q_dct_data_fx[i] = (Word32) ( ( step_fx * ( q_idx[i] + 1 ) ) >> 7 ); /* Q25 */
    4343             :         }
    4344             :     }
    4345        5502 :     return;
    4346             : }
    4347             : 
    4348             : 
    4349             : /*-------------------------------------------------------------------------
    4350             :  * ivas_omasa_decode_masa_to_total()
    4351             :  *
    4352             :  *------------------------------------------------------------------------*/
    4353             : 
    4354        5502 : 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        5502 :     n_streams = 1;
    4369        5502 :     len_stream = nbands * nblocks;
    4370        5502 :     if ( len_stream == 32 )
    4371             :     {
    4372           0 :         n_streams = 4;
    4373           0 :         len_stream = 8;
    4374             :     }
    4375             : 
    4376        5502 :     set_s( q_idx, 0, nbands * nblocks );
    4377       11004 :     for ( i = 0; i < n_streams; i++ )
    4378             :     {
    4379        5502 :         read_stream_dct_coeffs_omasa( &q_idx[i * len_stream],
    4380        5502 :                                       &q_dct_data_fx[i * len_stream],
    4381             :                                       len_stream, bit_stream, index, i == 0 );
    4382             :     }
    4383             : 
    4384             :     /* inverse DCT2 transform */
    4385        5502 :     SWITCH( len_stream )
    4386             :     {
    4387          69 :         case 4:
    4388          69 :             matrix_product_q30_fx( dct4_fx, nblocks, nblocks, 1, q_dct_data_fx, nblocks, 1, 0, dct_data_tmp_fx );
    4389          69 :             mvl2l( dct_data_tmp_fx, q_dct_data_fx, nblocks ); /*Q30*/
    4390          69 :             BREAK;
    4391         663 :         case 5:
    4392         663 :             matrix_product_q30_fx( dct5_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
    4393         663 :             mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
    4394         663 :             BREAK;
    4395           0 :         case 8:
    4396           0 :             matrix_product_q30_fx( dct8_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
    4397           0 :             mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
    4398           0 :             BREAK;
    4399        1080 :         case 12:
    4400        1080 :             matrix_product_q30_fx( dct12_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
    4401        1080 :             mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
    4402        1080 :             BREAK;
    4403        3690 :         case 20:
    4404        3690 :             matrix_product_fx( dct5_fx, nbands, nbands, 1, q_dct_data_fx, nbands, nblocks, 0, dct_data_tmp_fx );
    4405        3690 :             matrix_product_q30_fx( dct_data_tmp_fx, nbands, nblocks, 0, dct4_fx, nblocks, nblocks, 0, q_dct_data_fx ); /* reuse of variable*/
    4406        3690 :             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       95853 :     for ( i = 0; i < nblocks * nbands; i++ )
    4421             :     {
    4422       90351 :         if ( q_dct_data_fx[i] >= 1052266987 && q_dct_data_fx[i] < 1052400000 )
    4423             :         {
    4424          90 :             q_dct_data_fx[i] = 1052400000;
    4425             :         }
    4426             :     }
    4427             : 
    4428        5502 :     k = 0;
    4429       22281 :     for ( i = 0; i < nblocks; i++ )
    4430             :     {
    4431      107130 :         for ( j = 0; j < nbands; j++ )
    4432             :         {
    4433       90351 :             masa_to_total_energy_ratio[i][j] = q_dct_data_fx[k] / (float) ( 1 << 30 );
    4434       90351 :             masa_to_total_energy_ratio[i][j] = max( 0.0f, masa_to_total_energy_ratio[i][j] );
    4435       90351 :             masa_to_total_energy_ratio[i][j] = min( 1.0f, masa_to_total_energy_ratio[i][j] );
    4436       90351 :             k++;
    4437             :         }
    4438             :     }
    4439             : 
    4440        5502 :     if ( nblocks == 1 )
    4441             :     {
    4442        6972 :         for ( i = 1; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
    4443             :         {
    4444       54054 :             for ( j = 0; j < nbands; j++ )
    4445             :             {
    4446       48825 :                 masa_to_total_energy_ratio[i][j] = masa_to_total_energy_ratio[0][j];
    4447             :             }
    4448             :         }
    4449             :     }
    4450             : 
    4451        5502 :     if ( nbands == 1 )
    4452             :     {
    4453         345 :         for ( j = 1; j < 5; j++ )
    4454             :         {
    4455        1380 :             for ( i = 0; i < nblocks; i++ )
    4456             :             {
    4457        1104 :                 masa_to_total_energy_ratio[i][j] = masa_to_total_energy_ratio[i][0];
    4458             :             }
    4459             :         }
    4460             :     }
    4461             : 
    4462        5502 :     return;
    4463             : }

Generated by: LCOV version 1.14