LCOV - code coverage report
Current view: top level - lib_dec - ivas_masa_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ b5bd5e0684ad2d9250297e1e7a0ddc986cb7b37e Lines: 911 976 93.3 %
Date: 2025-10-27 07:01:45 Functions: 18 18 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <stdint.h>
      34             : #include "options.h"
      35             : #include <math.h>
      36             : #include "ivas_cnst.h"
      37             : #include "ivas_prot.h"
      38             : #include "ivas_prot_rend.h"
      39             : #include "ivas_rom_com.h"
      40             : #include "ivas_stat_dec.h"
      41             : #include "prot.h"
      42             : #ifdef DEBUGGING
      43             : #include "debug.h"
      44             : #endif
      45             : #include <assert.h>
      46             : #include "wmc_auto.h"
      47             : 
      48             : /*-----------------------------------------------------------------------*
      49             :  * Local constants
      50             :  *-----------------------------------------------------------------------*/
      51             : 
      52             : #define SPAR_META_DELAY_SUBFRAMES 2                              /* Number of subframes to delay the SPAR metadata */
      53             : #define SPH_IDX_FRONT             ( MASA_NO_POINTS_EQUATOR / 2 ) /* Spherical index corresponding to front direction for setting as default value */
      54             : 
      55             : 
      56             : /*-----------------------------------------------------------------------*
      57             :  * Local function prototypes
      58             :  *-----------------------------------------------------------------------*/
      59             : 
      60             : static void index_16bits( IVAS_QMETADATA_HANDLE hQMetaData, SPHERICAL_GRID_DATA *Sph_Grid16 );
      61             : 
      62             : static void create_masa_ext_out_meta( MASA_DECODER *hMasa, IVAS_QMETADATA_HANDLE hQMetaData, const int16_t nchan_transport );
      63             : 
      64             : static void replicate_subframes( IVAS_QMETADATA_HANDLE hQMetaData );
      65             : 
      66             : static void restore_lowbitrate_masa( IVAS_QMETADATA_HANDLE hQMetaData, const int16_t low_bitrate_mode, const int16_t numCodingBands );
      67             : 
      68             : static ivas_error init_lfe_synth_data( Decoder_Struct *st_ivas, MASA_DECODER_HANDLE hMasa );
      69             : 
      70             : static void compute_foa_cov_matrix( float foaCov[FOA_CHANNELS][FOA_CHANNELS], float inCov[FOA_CHANNELS][FOA_CHANNELS], float mixMtx[FOA_CHANNELS][FOA_CHANNELS] );
      71             : 
      72             : static int16_t decode_lfe_to_total_energy_ratio( MCMASA_LFE_SYNTH_DATA_HANDLE hMasaLfeSynth, uint16_t *bitstream, int16_t *index, const int32_t ivas_total_brate );
      73             : 
      74             : static ivas_error ivas_masa_dec_config( Decoder_Struct *st_ivas );
      75             : 
      76             : static int16_t ivas_decode_masaism_metadata( IVAS_QMETADATA_HANDLE hQMetaData, MASA_DECODER_HANDLE hMasa, MASA_ISM_DATA_HANDLE hMasaIsmData, const int16_t nchan_ism, uint16_t *bit_stream, int16_t *next_bit_pos, const int16_t idx_separated_object, const int16_t ism_imp, const int16_t dirac_bs_md_write_idx, const int16_t dirac_md_buffer_length );
      77             : 
      78             : static void decode_index_slice( int16_t index, int16_t *ratio_idx_ism, const int16_t nchan_ism, const int16_t K );
      79             : 
      80             : static void decode_ism_ratios( uint16_t *bit_stream, int16_t *next_bit_pos, float masa_to_total_energy_ratio[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS], float ratio_ism[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS][MAX_NUM_OBJECTS], const int16_t nchan_ism, const int16_t nbands, const int16_t nblocks, const int16_t idx_separated_object );
      81             : 
      82             : static void read_ism_ratio_index( int16_t ratio_ism_idx[MASA_FREQUENCY_BANDS][MAX_NUM_OBJECTS], const int16_t nchan_ism, const int16_t numCodingBands, const int16_t sf, int16_t ratio_ism_idx_prev_sf[MASA_FREQUENCY_BANDS][MAX_NUM_OBJECTS], uint16_t *bit_stream, int16_t *next_bit_pos, float *masa_to_total_energy_ratio, const int16_t idx_sep_obj, int16_t *num_zeros );
      83             : 
      84             : 
      85             : /*-----------------------------------------------------------------------*
      86             :  * ivas_masa_decode()
      87             :  *
      88             :  * MASA metadata decoder
      89             :  *-----------------------------------------------------------------------*/
      90             : 
      91     1111980 : ivas_error ivas_masa_decode(
      92             :     Decoder_Struct *st_ivas, /* i/o: IVAS decoder struct                    */
      93             :     Decoder_State *st,       /* i/o: decoder state structure with bitstream */
      94             :     int16_t *nb_bits_read    /* o  : number of bits read                    */
      95             : )
      96             : {
      97             :     uint16_t byteBuffer;
      98             :     int16_t next_bit_pos_orig;
      99             :     int32_t masa_brate, ivas_total_brate;
     100             :     MASA_DECODER_HANDLE hMasa;
     101             :     IVAS_QMETADATA_HANDLE hQMetaData;
     102             :     IVAS_FORMAT ivas_format;
     103             :     int16_t low_bitrate_mode, tmp_elem_mode;
     104             :     ivas_error error;
     105             :     int16_t obj;
     106             :     int16_t i, ch, ism_imp;
     107             :     int16_t dirac_bs_md_write_idx;
     108             :     int32_t masa_total_brate;
     109             : 
     110     1111980 :     dirac_bs_md_write_idx = 0;
     111     1111980 :     ism_imp = 0;
     112             : 
     113     1111980 :     error = IVAS_ERR_OK;
     114     1111980 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
     115             : 
     116     1111980 :     low_bitrate_mode = -1; /* This means that LBR mode is not used. */
     117             : 
     118     1111980 :     if ( st_ivas->hOutSetup.separateChannelEnabled || st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_DISC )
     119             :     {
     120      492895 :         masa_brate = st_ivas->hCPE[0]->element_brate;
     121             :     }
     122             :     else
     123             :     {
     124      619085 :         masa_brate = ivas_total_brate;
     125             :     }
     126             : 
     127     1111980 :     hMasa = st_ivas->hMasa;
     128     1111980 :     hQMetaData = st_ivas->hQMetaData;
     129     1111980 :     ivas_format = st_ivas->ivas_format;
     130             : 
     131     1111980 :     hMasa->data.dir_decode_quality = 1.0f; /* Set to default of max quality */
     132     1111980 :     hQMetaData->is_masa_ivas_format = 1;
     133             : 
     134     1111980 :     *nb_bits_read = 0;
     135     1111980 :     next_bit_pos_orig = st->next_bit_pos;
     136     1111980 :     if ( masa_brate == IVAS_SID_5k2 )
     137             :     {
     138         615 :         st->next_bit_pos = (int16_t) ( ( masa_brate / FRAMES_PER_SEC ) - 1 - SID_FORMAT_NBITS );
     139             :     }
     140             :     else
     141             :     {
     142     1111365 :         st->next_bit_pos = (int16_t) ( ( masa_brate / FRAMES_PER_SEC ) - 1 );
     143             :     }
     144             : 
     145     1111980 :     if ( ivas_format == MASA_FORMAT && ( masa_brate == IVAS_SID_5k2 || ivas_total_brate == FRAME_NO_DATA ) )
     146             :     {
     147        4353 :         hMasa->config.numberOfDirections = 1;
     148             :     }
     149             : 
     150     1111980 :     if ( !st->bfi && ivas_total_brate > IVAS_SID_5k2 )
     151             :     {
     152     1017656 :         if ( !( ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCMASA ) )
     153             :         {
     154      876921 :             if ( ivas_format == MASA_FORMAT )
     155             :             {
     156             :                 /* re-read the number of objects, needed in case of bad frame  */
     157      453091 :                 ch = 5 - ( st_ivas->bit_stream[( ivas_total_brate / FRAMES_PER_SEC ) - 3] + 2 * st_ivas->bit_stream[( ivas_total_brate / FRAMES_PER_SEC ) - 2] );
     158             : 
     159      453091 :                 if ( ch == 5 )
     160             :                 {
     161      327653 :                     ch = 0;
     162             :                 }
     163      453091 :                 st_ivas->nchan_ism = ch;
     164             :             }
     165             : 
     166      876921 :             if ( ivas_format == MASA_FORMAT && st_ivas->nchan_ism > 0 )
     167             :             {
     168             :                 /* there was OMASA in the input */
     169      125438 :                 hMasa->config.input_ivas_format = MASA_ISM_FORMAT;
     170      125438 :                 if ( st_ivas->nchan_ism < 3 )
     171             :                 {
     172             :                     /* was read in ivas_init_dec() to distinguish between 1 and 2 objects */
     173       57238 :                     if ( st_ivas->bit_stream[( ivas_total_brate / FRAMES_PER_SEC ) - 1] == 0 )
     174             :                     {
     175       27769 :                         st_ivas->nchan_ism = 1;
     176             :                     }
     177       57238 :                     st->next_bit_pos -= MASA_TRANSP_BITS;
     178       57238 :                     *nb_bits_read += MASA_TRANSP_BITS;
     179             : 
     180             :                     /* the two reserved bits were already read in ivas_init_dec()*/
     181       57238 :                     byteBuffer = st->bit_stream[( st->next_bit_pos )--];
     182       57238 :                     byteBuffer = st->bit_stream[( st->next_bit_pos )--];
     183       57238 :                     *nb_bits_read += MASA_HEADER_BITS;
     184             :                     /* read number of directions */
     185       57238 :                     byteBuffer = st->bit_stream[( st->next_bit_pos )--];
     186       57238 :                     ( *nb_bits_read )++;
     187       57238 :                     hMasa->config.numberOfDirections = (uint8_t) ( byteBuffer + 1 );
     188             :                 }
     189             :                 else
     190             :                 {
     191             :                     /* if there are 3 or 4 objects the number of transport channels bit is given to MASA format
     192             :                     and used to read number of directions*/
     193       68200 :                     byteBuffer = st->bit_stream[( st->next_bit_pos )--];
     194       68200 :                     ( *nb_bits_read )++;
     195       68200 :                     hMasa->config.numberOfDirections = (uint8_t) ( byteBuffer + 1 );
     196             : 
     197             :                     /* the two reserved bits were already read in ivas_init_dec()*/
     198       68200 :                     byteBuffer = st->bit_stream[( st->next_bit_pos )--];
     199       68200 :                     byteBuffer = st->bit_stream[( st->next_bit_pos )--];
     200       68200 :                     *nb_bits_read += MASA_HEADER_BITS;
     201             :                 }
     202             :             }
     203             :             else
     204             :             {
     205      751483 :                 if ( ivas_format != MASA_ISM_FORMAT )
     206             :                 {
     207             :                     /* number of transport channels is always 2 for MASA_ISM format */
     208             :                     /* the number of MASA transport channels was read in ivas_dec_setup() */
     209      327653 :                     st->next_bit_pos -= MASA_TRANSP_BITS;
     210      327653 :                     *nb_bits_read += MASA_TRANSP_BITS;
     211             :                 }
     212             : 
     213      751483 :                 if ( ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode != ISM_MODE_NONE )
     214             :                 {
     215             :                     /* the number of objects  was read */
     216      423830 :                     st->next_bit_pos -= NO_BITS_MASA_ISM_NO_OBJ;
     217      423830 :                     *nb_bits_read += NO_BITS_MASA_ISM_NO_OBJ;
     218             : 
     219      423830 :                     if ( st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
     220             :                     {
     221             :                         /* read index of separated object */
     222             :                         /* nchan_ism should be > 1*/
     223       63232 :                         byteBuffer = st->bit_stream[( st->next_bit_pos )--];
     224       63232 :                         ( *nb_bits_read )++;
     225       63232 :                         st_ivas->hMasaIsmData->idx_separated_ism = 2 * byteBuffer + st->bit_stream[( st->next_bit_pos )--];
     226       63232 :                         ( *nb_bits_read )++;
     227             :                     }
     228             :                     else
     229             :                     {
     230      360598 :                         st_ivas->hMasaIsmData->idx_separated_ism = -1;
     231             :                     }
     232             : 
     233             :                     /* read ISM importance flag (one per object) */
     234      423830 :                     if ( st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
     235             :                     {
     236       63232 :                         ism_imp = 0;
     237      189696 :                         for ( i = 0; i < ISM_METADATA_FLAG_BITS; i++ )
     238             :                         {
     239      126464 :                             byteBuffer = st->bit_stream[( st->next_bit_pos )--];
     240      126464 :                             ( *nb_bits_read )++;
     241      126464 :                             ism_imp = ( ism_imp << 1 ) + byteBuffer;
     242             :                         }
     243       63232 :                         st_ivas->hIsmMetaData[0]->ism_imp = ism_imp;
     244             :                     }
     245             : 
     246      423830 :                     if ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ )
     247             :                     {
     248       46297 :                         ism_imp = 0;
     249      138891 :                         for ( i = 0; i < ISM_METADATA_FLAG_BITS; i++ )
     250             :                         {
     251       92594 :                             byteBuffer = st->bit_stream[( st->next_bit_pos )--];
     252       92594 :                             ( *nb_bits_read )++;
     253       92594 :                             ism_imp = ( ism_imp << 1 ) + byteBuffer;
     254             :                         }
     255       46297 :                         st_ivas->hIsmMetaData[0]->ism_imp = ism_imp;
     256             : 
     257             :                         /* reset */
     258       46297 :                         st_ivas->hIsmMetaData[0]->ism_md_null_flag = 0;
     259       46297 :                         st_ivas->hIsmMetaData[0]->ism_md_lowrate_flag = 0;
     260       46297 :                         if ( st_ivas->hIsmMetaData[0]->ism_imp == ISM_NO_META )
     261             :                         {
     262             :                             /* read flags */
     263           0 :                             st_ivas->hIsmMetaData[0]->ism_md_null_flag = st->bit_stream[( st->next_bit_pos )--];
     264           0 :                             ( *nb_bits_read ) += ISM_METADATA_MD_FLAG_BITS;
     265           0 :                             st_ivas->hIsmMetaData[0]->ism_md_lowrate_flag = st->bit_stream[( st->next_bit_pos )--];
     266           0 :                             ( *nb_bits_read ) += ISM_METADATA_INACTIVE_FLAG_BITS;
     267             :                         }
     268             :                     }
     269      377533 :                     else if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
     270             :                     {
     271      993954 :                         for ( ch = 0; ch < st_ivas->nchan_ism; ch++ )
     272             :                         {
     273      679653 :                             ism_imp = 0;
     274     2038959 :                             for ( i = 0; i < ISM_METADATA_FLAG_BITS; i++ )
     275             :                             {
     276     1359306 :                                 byteBuffer = st->bit_stream[( st->next_bit_pos )--];
     277     1359306 :                                 ( *nb_bits_read )++;
     278     1359306 :                                 ism_imp = ( ism_imp << 1 ) + byteBuffer;
     279             :                             }
     280      679653 :                             st_ivas->hIsmMetaData[ch]->ism_imp = ism_imp;
     281             : 
     282             :                             /* reset */
     283      679653 :                             st_ivas->hIsmMetaData[ch]->ism_md_null_flag = 0;
     284      679653 :                             st_ivas->hIsmMetaData[ch]->ism_md_lowrate_flag = 0;
     285      679653 :                             if ( st_ivas->hIsmMetaData[ch]->ism_imp == ISM_NO_META )
     286             :                             {
     287             :                                 /* read flags */
     288           0 :                                 st_ivas->hIsmMetaData[ch]->ism_md_null_flag = st->bit_stream[( st->next_bit_pos )--];
     289           0 :                                 ( *nb_bits_read ) += ISM_METADATA_MD_FLAG_BITS;
     290           0 :                                 st_ivas->hIsmMetaData[ch]->ism_md_lowrate_flag = st->bit_stream[( st->next_bit_pos )--];
     291           0 :                                 ( *nb_bits_read ) += ISM_METADATA_INACTIVE_FLAG_BITS;
     292             :                             }
     293             :                         }
     294      314301 :                         st_ivas->flag_omasa_brate = 0;
     295      314301 :                         if ( st_ivas->nchan_ism >= 3 && ivas_total_brate == IVAS_128k )
     296             :                         {
     297       16968 :                             st_ivas->flag_omasa_brate = st->bit_stream[( st->next_bit_pos )--];
     298       16968 :                             ( *nb_bits_read ) += 1;
     299             :                         }
     300             :                     }
     301             :                 }
     302             : 
     303      751483 :                 byteBuffer = st->bit_stream[( st->next_bit_pos )--];
     304      751483 :                 byteBuffer = byteBuffer + 2 * st->bit_stream[( st->next_bit_pos )--];
     305             : 
     306      751483 :                 if ( byteBuffer == 0 && ivas_format == MASA_FORMAT )
     307             :                 {
     308      327653 :                     hMasa->config.input_ivas_format = MASA_FORMAT;
     309             :                 }
     310             :                 else
     311             :                 {
     312      423830 :                     hMasa->config.input_ivas_format = MASA_ISM_FORMAT;
     313             :                 }
     314      751483 :                 *nb_bits_read += MASA_HEADER_BITS;
     315             : 
     316             :                 /* read number of directions */
     317      751483 :                 byteBuffer = st->bit_stream[( st->next_bit_pos )--];
     318      751483 :                 ( *nb_bits_read )++;
     319      751483 :                 hMasa->config.numberOfDirections = (uint8_t) ( byteBuffer + 1 );
     320             :             }
     321             :         }
     322             :         else
     323             :         {
     324      140735 :             hMasa->config.numberOfDirections = 1;
     325             :         }
     326             : 
     327     1017656 :         if ( !( ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCMASA ) )
     328             :         {
     329             :             /* read subframe mode */
     330      876921 :             byteBuffer = st->bit_stream[( st->next_bit_pos )--];
     331      876921 :             ( *nb_bits_read )++;
     332      876921 :             hMasa->config.joinedSubframes = (uint8_t) byteBuffer;
     333             :         }
     334             :         else
     335             :         {
     336      140735 :             hMasa->config.joinedSubframes = FALSE;
     337             :         }
     338             : 
     339     1017656 :         if ( st_ivas->mc_mode == MC_MODE_MCMASA )
     340             :         {
     341      140735 :             *nb_bits_read += decode_lfe_to_total_energy_ratio( hMasa->hMasaLfeSynth, st->bit_stream, &st->next_bit_pos, ivas_total_brate );
     342             :         }
     343             : 
     344             :         /* Once we know incoming configuration, we can config decoder further based on bitrate etc. */
     345     1017656 :         if ( ( error = ivas_masa_dec_config( st_ivas ) ) != IVAS_ERR_OK )
     346             :         {
     347           0 :             return error;
     348             :         }
     349             : 
     350             :         /* If we are under metadata bit budget limit and joined subframes is not signalled, then read LBR mode. */
     351     1017656 :         if ( hMasa->config.max_metadata_bits < MINIMUM_BIT_BUDGET_NORMAL_META && hMasa->config.joinedSubframes == FALSE )
     352             :         {
     353             :             /* read low bitrate mode */
     354      426758 :             byteBuffer = st->bit_stream[( st->next_bit_pos )--];
     355      426758 :             ( *nb_bits_read )++;
     356      426758 :             low_bitrate_mode = byteBuffer;
     357             : 
     358      426758 :             if ( low_bitrate_mode == 1 )
     359             :             {
     360      355439 :                 hQMetaData->q_direction[0].cfg.nblocks = 1;
     361             :             }
     362             :             else
     363             :             {
     364       71319 :                 hQMetaData->q_direction[0].cfg.nbands = 1;
     365             :             }
     366             :         }
     367             : 
     368             :         /* Remove already read bits from the bit budget */
     369     1017656 :         hQMetaData->metadata_max_bits -= *nb_bits_read;
     370             : 
     371     1017656 :         if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
     372             :         {
     373      423830 :             if ( st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
     374             :             {
     375       63232 :                 if ( st_ivas->hDirAC != NULL )
     376             :                 {
     377      110720 :                     *nb_bits_read += ivas_decode_masaism_metadata( hQMetaData, st_ivas->hMasa, st_ivas->hMasaIsmData, st_ivas->nchan_ism, st->bit_stream, &st->next_bit_pos,
     378       55360 :                                                                    st_ivas->hMasaIsmData->idx_separated_ism, ism_imp, st_ivas->hSpatParamRendCom->dirac_bs_md_write_idx, st_ivas->hSpatParamRendCom->dirac_md_buffer_length );
     379             : 
     380      299610 :                     for ( obj = 0; obj <= st_ivas->nchan_ism; obj++ )
     381             :                     {
     382      244250 :                         if ( st_ivas->hMasaIsmData->idx_separated_ism == obj )
     383             :                         {
     384             :                             int16_t sf;
     385             :                             int16_t meta_write_index;
     386             : 
     387      276800 :                             for ( sf = 0; sf < MAX_PARAM_SPATIAL_SUBFRAMES; sf++ )
     388             :                             {
     389      221440 :                                 meta_write_index = ( st_ivas->hSpatParamRendCom->dirac_bs_md_write_idx + sf ) % st_ivas->hSpatParamRendCom->dirac_md_buffer_length;
     390      221440 :                                 st_ivas->hMasaIsmData->azimuth_separated_ism[meta_write_index] = st_ivas->hMasaIsmData->azimuth_ism[obj][meta_write_index];
     391      221440 :                                 st_ivas->hMasaIsmData->elevation_separated_ism[meta_write_index] = st_ivas->hMasaIsmData->elevation_ism[obj][meta_write_index];
     392             :                             }
     393             :                         }
     394             :                     }
     395             :                 }
     396        7872 :                 else if ( st_ivas->renderer_type == RENDERER_OMASA_OBJECT_EXT )
     397             :                 {
     398        3588 :                     *nb_bits_read += ivas_decode_masaism_metadata( hQMetaData, st_ivas->hMasa, st_ivas->hMasaIsmData, st_ivas->nchan_ism, st->bit_stream, &st->next_bit_pos,
     399        3588 :                                                                    st_ivas->hMasaIsmData->idx_separated_ism, ism_imp, st_ivas->hSpatParamRendCom->dirac_bs_md_write_idx, st_ivas->hSpatParamRendCom->dirac_md_buffer_length );
     400             :                 }
     401             :                 else
     402             :                 {
     403        4284 :                     *nb_bits_read += ivas_decode_masaism_metadata( hQMetaData, st_ivas->hMasa, st_ivas->hMasaIsmData, st_ivas->nchan_ism, st->bit_stream, &st->next_bit_pos,
     404        4284 :                                                                    st_ivas->hMasaIsmData->idx_separated_ism, ism_imp, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
     405             :                 }
     406             :             }
     407             :         }
     408             : 
     409     1017656 :         masa_total_brate = ivas_total_brate;
     410     1017656 :         if ( ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode == ISM_MASA_MODE_DISC )
     411             :         {
     412      314301 :             masa_total_brate = calculate_cpe_brate_MASA_ISM( st_ivas->ism_mode, ivas_total_brate, st_ivas->nchan_ism );
     413             :         }
     414             : 
     415     1017656 :         if ( masa_total_brate >= IVAS_384k )
     416             :         {
     417       54639 :             if ( masa_total_brate >= IVAS_512k )
     418             :             {
     419       20286 :                 *nb_bits_read += ivas_qmetadata_dec_decode_hr_384_512( hQMetaData, st->bit_stream, &st->next_bit_pos, hMasa->data.sph_grid16, 16, 4, hMasa->config.numCodingBands );
     420             :             }
     421             :             else
     422             :             {
     423       34353 :                 *nb_bits_read += ivas_qmetadata_dec_decode_hr_384_512( hQMetaData, st->bit_stream, &st->next_bit_pos, hMasa->data.sph_grid16, 11, 3, hMasa->config.numCodingBands );
     424             :             }
     425             :         }
     426             :         else
     427             :         {
     428      963017 :             *nb_bits_read += ivas_qmetadata_dec_decode( hQMetaData, st->bit_stream, &st->next_bit_pos, 0 );
     429             :         }
     430             : 
     431     1017656 :         if ( st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode != ISM_MASA_MODE_DISC && st_ivas->ism_mode != ISM_MASA_MODE_MASA_ONE_OBJ )
     432             :         {
     433             :             /* Modify spatial metadata based on the MASA-to-total energy ratios */
     434       63232 :             ivas_omasa_modify_masa_energy_ratios( hQMetaData, st_ivas->hMasaIsmData->masa_to_total_energy_ratio );
     435             :         }
     436             : 
     437             :         /* Get direction decoding quality. EC 1 and 2 are handled by the default value. */
     438     1017656 :         if ( hQMetaData->ec_flag == 2 )
     439             :         {
     440      147614 :             hMasa->data.dir_decode_quality = hQMetaData->dir_comp_ratio;
     441             :         }
     442             : 
     443     1017656 :         hMasa->config.coherencePresent = !hQMetaData->all_coherence_zero;
     444             : 
     445     1017656 :         if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL && ( st_ivas->ivas_format == MASA_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT ) )
     446             :         {
     447       60129 :             index_16bits( hQMetaData, hMasa->data.sph_grid16 );
     448             :         }
     449             : 
     450             :         /* If LBR mode is not disabled, then we restore metadata to 5 bands and 4 subframes based on the LBR mode. */
     451     1017656 :         if ( low_bitrate_mode != -1 )
     452             :         {
     453      426758 :             restore_lowbitrate_masa( hQMetaData, low_bitrate_mode, hMasa->config.numCodingBands );
     454             :         }
     455      590898 :         else if ( hMasa->config.joinedSubframes == TRUE )
     456             :         {
     457       18090 :             replicate_subframes( hQMetaData );
     458             :         }
     459             :     }
     460       94324 :     else if ( !st->bfi && ivas_format == MASA_FORMAT && ivas_total_brate == IVAS_SID_5k2 )
     461             :     {
     462         615 :         if ( hQMetaData->q_direction == NULL )
     463             :         {
     464             :             /* replicate ivas_masa_dec_config() in case that first good received frame is SID frame */
     465           0 :             if ( ( error = ivas_masa_dec_config( st_ivas ) ) != IVAS_ERR_OK )
     466             :             {
     467           0 :                 return error;
     468             :             }
     469             : 
     470           0 :             ivas_masa_set_elements( ivas_total_brate, st_ivas->mc_mode, st_ivas->nchan_transport, hQMetaData, &st_ivas->element_mode_init, &st_ivas->nSCE, &st_ivas->nCPE, st_ivas->ivas_format, st_ivas->ism_mode, 0 );
     471             : 
     472           0 :             hQMetaData->metadata_max_bits = ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC;
     473             : 
     474           0 :             if ( ( error = ivas_qmetadata_allocate_memory( hQMetaData, 5, 1, 0 ) ) != IVAS_ERR_OK )
     475             :             {
     476           0 :                 return error;
     477             :             }
     478             : 
     479           0 :             hQMetaData->numTwoDirBands = hMasa->config.numTwoDirBands;
     480           0 :             hQMetaData->useLowerRes = 0;
     481             : 
     482           0 :             hQMetaData->q_direction->cfg.nbands = 5;
     483           0 :             hQMetaData->q_direction->cfg.nblocks = 4;
     484             : 
     485           0 :             if ( ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCMASA )
     486             :             {
     487           0 :                 hQMetaData->q_direction->cfg.mc_ls_setup = ivas_mc_map_output_config_to_mc_ls_setup( st_ivas->transport_config );
     488             :             }
     489             :             else
     490             :             {
     491           0 :                 hQMetaData->q_direction->cfg.mc_ls_setup = MC_LS_SETUP_INVALID;
     492             :             }
     493             :         }
     494             : 
     495         615 :         tmp_elem_mode = -1;
     496         615 :         *nb_bits_read += ivas_qmetadata_dec_sid_decode( hQMetaData, st->bit_stream, &( st->next_bit_pos ), st_ivas->nchan_transport, &tmp_elem_mode, ivas_format );
     497         615 :         if ( st_ivas->nchan_transport == 2 )
     498             :         {
     499         222 :             assert( st_ivas->nCPE > 0 );
     500         222 :             st_ivas->hCPE[0]->element_mode = tmp_elem_mode;
     501             :         }
     502         615 :         *nb_bits_read += SID_FORMAT_NBITS;
     503             :     }
     504       93709 :     else if ( !st->bfi && ivas_format == MASA_FORMAT && ivas_total_brate == FRAME_NO_DATA )
     505             :     {
     506        3738 :         if ( hQMetaData->q_direction == NULL )
     507             :         {
     508           0 :             if ( ( error = ivas_masa_dec_config( st_ivas ) ) != IVAS_ERR_OK )
     509             :             {
     510           0 :                 return error;
     511             :             }
     512             :         }
     513             :     }
     514             : 
     515     1111980 :     if ( st_ivas->hDirAC != NULL )
     516             :     {
     517      977769 :         dirac_bs_md_write_idx = st_ivas->hSpatParamRendCom->dirac_bs_md_write_idx; /* Store the write-index for this frame */
     518      977769 :         ivas_qmetadata_to_dirac( hQMetaData, st_ivas->hDirAC, hMasa, st_ivas->hSpatParamRendCom, ivas_total_brate, ivas_format, 0, 0 );
     519             :     }
     520      134211 :     else if ( st_ivas->renderer_type == RENDERER_OMASA_OBJECT_EXT )
     521             :     {
     522        3984 :         st_ivas->hSpatParamRendCom->dirac_bs_md_write_idx = ( st_ivas->hSpatParamRendCom->dirac_bs_md_write_idx + MAX_PARAM_SPATIAL_SUBFRAMES ) % st_ivas->hSpatParamRendCom->dirac_md_buffer_length;
     523             :     }
     524             : 
     525     1111980 :     if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
     526             :     {
     527      467186 :         if ( hQMetaData->q_direction == NULL )
     528             :         {
     529           0 :             if ( ( error = ivas_masa_dec_config( st_ivas ) ) != IVAS_ERR_OK )
     530             :             {
     531           0 :                 return error;
     532             :             }
     533             :         }
     534             : 
     535      467186 :         if ( st_ivas->hDirAC != NULL )
     536             :         {
     537             :             int16_t b;
     538             :             int16_t block;
     539             :             int16_t meta_write_index;
     540             : 
     541      616049 :             for ( i = 0; i < st_ivas->hSpatParamRendCom->numIsmDirections; i++ )
     542             :             {
     543     1029345 :                 for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; block++ )
     544             :                 {
     545      823476 :                     meta_write_index = ( dirac_bs_md_write_idx + block ) % st_ivas->hSpatParamRendCom->dirac_md_buffer_length;
     546             : 
     547    36886836 :                     for ( b = 0; b < st_ivas->hSpatParamRendCom->num_freq_bands; b++ )
     548             :                     {
     549    36063360 :                         st_ivas->hSpatParamRendCom->diffuseness_vector[meta_write_index][b] -= st_ivas->hMasaIsmData->energy_ratio_ism[i][meta_write_index][b];
     550             :                     }
     551             :                 }
     552             :             }
     553             : 
     554     2050900 :             for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; block++ )
     555             :             {
     556     1640720 :                 meta_write_index = ( dirac_bs_md_write_idx + block ) % st_ivas->hSpatParamRendCom->dirac_md_buffer_length;
     557             : 
     558    71615600 :                 for ( b = 0; b < st_ivas->hSpatParamRendCom->num_freq_bands; b++ )
     559             :                 {
     560    69974880 :                     st_ivas->hSpatParamRendCom->diffuseness_vector[meta_write_index][b] = max( 0.0f, st_ivas->hSpatParamRendCom->diffuseness_vector[meta_write_index][b] );
     561             :                 }
     562             :             }
     563             :         }
     564             :     }
     565             : 
     566     1111980 :     st->next_bit_pos = next_bit_pos_orig;
     567             : 
     568     1111980 :     if ( ivas_format == MASA_ISM_FORMAT )
     569             :     {
     570             :         int32_t cpe_brate;
     571      467186 :         cpe_brate = calculate_cpe_brate_MASA_ISM( st_ivas->ism_mode, ivas_total_brate, st_ivas->nchan_ism );
     572             : 
     573      467186 :         if ( st_ivas->nCPE == 1 && st_ivas->hCPE[0]->hStereoDft != NULL && st_ivas->hCPE[0]->hStereoDft->hConfig != NULL )
     574             :         {
     575      135375 :             st_ivas->hCPE[0]->hStereoDft->hConfig->force_mono_transmission = cpe_brate < MASA_STEREO_MIN_BITRATE ? 1 : 0;
     576             : 
     577      135375 :             if ( ivas_total_brate <= IVAS_SID_5k2 )
     578             :             {
     579           0 :                 st_ivas->hCPE[0]->hStereoDft->hConfig->force_mono_transmission = 0;
     580             :             }
     581             :         }
     582             :     }
     583             :     else
     584             :     {
     585      644794 :         if ( ivas_format == MASA_FORMAT && st_ivas->nCPE == 1 && st_ivas->hCPE[0]->hStereoDft != NULL && st_ivas->hCPE[0]->hStereoDft->hConfig != NULL )
     586             :         {
     587      190311 :             st_ivas->hCPE[0]->hStereoDft->hConfig->force_mono_transmission = ivas_total_brate < MASA_STEREO_MIN_BITRATE ? 1 : 0;
     588             : 
     589      190311 :             if ( ivas_total_brate <= IVAS_SID_5k2 )
     590             :             {
     591        1590 :                 st_ivas->hCPE[0]->hStereoDft->hConfig->force_mono_transmission = 0;
     592             :             }
     593             :         }
     594             :     }
     595             : 
     596     1111980 :     if ( ivas_format == MASA_FORMAT && st_ivas->nCPE == 1 )
     597             :     {
     598      318103 :         st_ivas->hCPE[0]->hCoreCoder[0]->masa_sid_format = 0;
     599             : 
     600      318103 :         if ( st_ivas->hDecoderConfig->last_ivas_total_brate <= IVAS_SID_5k2 )
     601             :         {
     602        1590 :             st_ivas->hCPE[0]->hCoreCoder[0]->masa_sid_format = 1;
     603             : 
     604        1590 :             if ( ivas_total_brate >= IVAS_SID_5k2 )
     605             :             {
     606         222 :                 st_ivas->hCPE[0]->element_brate = ivas_total_brate;
     607             :             }
     608             :         }
     609             :     }
     610             : 
     611     1111980 :     if ( ( st_ivas->ivas_format == MASA_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT ) && st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
     612             :     {
     613       66453 :         create_masa_ext_out_meta( hMasa, hQMetaData, st_ivas->nchan_transport );
     614             :     }
     615             : 
     616     1111980 :     return error /* *nb_bits_read*/;
     617             : }
     618             : 
     619             : 
     620             : /*-------------------------------------------------------------------*
     621             :  * ivas_masa_dec_open()
     622             :  *
     623             :  *
     624             :  *-------------------------------------------------------------------*/
     625             : 
     626       20336 : ivas_error ivas_masa_dec_open(
     627             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder handle  */
     628             : )
     629             : {
     630             :     MASA_DECODER_HANDLE hMasa;
     631             :     ivas_error error;
     632             :     int16_t i;
     633             :     int32_t ism_total_brate;
     634             : 
     635       20336 :     error = IVAS_ERR_OK;
     636             : 
     637       20336 :     if ( ( hMasa = (MASA_DECODER_HANDLE) malloc( sizeof( MASA_DECODER ) ) ) == NULL )
     638             :     {
     639           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MASA decoder\n" ) );
     640             :     }
     641             : 
     642       20336 :     ism_total_brate = 0;
     643             :     /* ISM metadata */
     644       20336 :     if ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->hIsmMetaData[0] != NULL )
     645             :     {
     646             :         /* these are not needed -> clean. EXT metafile writer in OMASA needs only the number of ISMs and writes default null-data */
     647           0 :         ivas_ism_metadata_close( st_ivas->hIsmMetaData, 0 );
     648             :     }
     649       20336 :     if ( st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->nSCE > 0 && ( st_ivas->ism_mode == ISM_MASA_MODE_DISC || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ ) )
     650             :     {
     651       23334 :         for ( i = 0; i < st_ivas->nSCE; i++ )
     652             :         {
     653       15172 :             ism_total_brate += st_ivas->hSCE[i]->element_brate;
     654             :         }
     655             :     }
     656             : 
     657       20336 :     ivas_masa_set_elements( st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->mc_mode, st_ivas->nchan_transport, st_ivas->hQMetaData, &st_ivas->element_mode_init, &st_ivas->nSCE, &st_ivas->nCPE, st_ivas->ivas_format, st_ivas->ism_mode, ism_total_brate );
     658             : 
     659       20336 :     mvs2s( DirAC_block_grouping, hMasa->config.block_grouping, MAX_PARAM_SPATIAL_SUBFRAMES + 1 );
     660       20336 :     mvs2s( MASA_band_grouping_24, hMasa->config.band_grouping, MASA_FREQUENCY_BANDS + 1 );
     661       20336 :     hMasa->config.numberOfDirections = 1;
     662       20336 :     hMasa->config.joinedSubframes = FALSE;
     663             : 
     664             :     /* Create spherical grid only for external output */
     665       20336 :     if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL && ( st_ivas->ivas_format == MASA_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT ) )
     666             :     {
     667        1041 :         if ( ( hMasa->data.sph_grid16 = (SPHERICAL_GRID_DATA *) malloc( sizeof( SPHERICAL_GRID_DATA ) ) ) == NULL )
     668             :         {
     669           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MASA decoder\n" ) );
     670             :         }
     671             : 
     672        1041 :         generate_gridEq( hMasa->data.sph_grid16 );
     673        1041 :         if ( ( hMasa->data.extOutMeta = (MASA_DECODER_EXT_OUT_META *) malloc( sizeof( MASA_DECODER_EXT_OUT_META ) ) ) == NULL )
     674             :         {
     675           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MASA decoder\n" ) );
     676             :         }
     677             :     }
     678             :     else
     679             :     {
     680       19295 :         hMasa->data.sph_grid16 = NULL;
     681       19295 :         hMasa->data.extOutMeta = NULL;
     682             :     }
     683             : 
     684       20336 :     if ( st_ivas->mc_mode == MC_MODE_MCMASA )
     685             :     {
     686        4047 :         error = init_lfe_synth_data( st_ivas, hMasa );
     687             :     }
     688             :     else
     689             :     {
     690       16289 :         hMasa->hMasaLfeSynth = NULL;
     691             :     }
     692             : 
     693       20336 :     st_ivas->hMasa = hMasa;
     694             : 
     695             :     /* allocate transport channels*/
     696       20336 :     if ( st_ivas->hTcBuffer == NULL && st_ivas->renderer_type != RENDERER_DISABLE && st_ivas->renderer_type != RENDERER_BINAURAL_PARAMETRIC && st_ivas->renderer_type != RENDERER_BINAURAL_PARAMETRIC_ROOM && st_ivas->renderer_type != RENDERER_STEREO_PARAMETRIC )
     697             :     {
     698             :         int16_t nchan_to_allocate, nchan_transport;
     699             :         int16_t granularity;
     700             :         TC_BUFFER_MODE buffer_mode;
     701             : 
     702       10315 :         buffer_mode = TC_BUFFER_MODE_RENDERER;
     703       10315 :         if ( st_ivas->mc_mode == MC_MODE_MCMASA && ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_STEREO || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_MONO ) )
     704             :         {
     705          83 :             buffer_mode = TC_BUFFER_MODE_BUFFER;
     706             :         }
     707       10232 :         else if ( st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
     708             :         {
     709         507 :             buffer_mode = TC_BUFFER_MODE_BUFFER;
     710             :         }
     711             : 
     712       10315 :         nchan_transport = ivas_jbm_dec_get_num_tc_channels( st_ivas );
     713       10315 :         nchan_to_allocate = nchan_transport;
     714             : 
     715       10315 :         if ( st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
     716             :         {
     717         507 :             nchan_transport = 1;
     718         507 :             nchan_to_allocate = 1;
     719             :         }
     720        9808 :         else if ( st_ivas->ivas_format == MASA_ISM_FORMAT && ( st_ivas->renderer_type == RENDERER_OMASA_MIX_EXT || st_ivas->renderer_type == RENDERER_OMASA_OBJECT_EXT ) )
     721             :         {
     722         125 :             nchan_transport = st_ivas->nchan_transport + st_ivas->nchan_ism;
     723         125 :             nchan_to_allocate = st_ivas->nchan_transport + st_ivas->nchan_ism;
     724             :         }
     725        9683 :         else if ( st_ivas->nchan_transport == 1 && ( st_ivas->renderer_type == RENDERER_DIRAC ) )
     726             :         {
     727             :             /* addtl channel for CNG */
     728        2005 :             nchan_to_allocate++;
     729             :         }
     730             : 
     731       10315 :         granularity = ivas_jbm_dec_get_render_granularity( st_ivas->renderer_type, RENDERER_DISABLE, st_ivas->hDecoderConfig->output_Fs );
     732             : 
     733       10315 :         if ( ( error = ivas_jbm_dec_tc_buffer_open( st_ivas, buffer_mode, nchan_transport, nchan_to_allocate, nchan_to_allocate, granularity ) ) != IVAS_ERR_OK )
     734             :         {
     735           0 :             return error;
     736             :         }
     737             :     }
     738             : 
     739       20336 :     return error;
     740             : }
     741             : 
     742             : 
     743             : /*-----------------------------------------------------------------------*
     744             :  * ivas_masa_dec_close()
     745             :  *
     746             :  * close MASA decoder
     747             :  *-----------------------------------------------------------------------*/
     748             : 
     749       90209 : void ivas_masa_dec_close(
     750             :     MASA_DECODER_HANDLE *hMasa_out /* i/o: MASA metadata structure      */
     751             : )
     752             : {
     753             :     MASA_DECODER_HANDLE hMasa;
     754             : 
     755       90209 :     if ( hMasa_out == NULL || *hMasa_out == NULL )
     756             :     {
     757       69873 :         return;
     758             :     }
     759             : 
     760       20336 :     hMasa = *hMasa_out;
     761             : 
     762             :     /* Free spherical grid memory if in use */
     763       20336 :     if ( hMasa->data.sph_grid16 != NULL )
     764             :     {
     765        3041 :         free( hMasa->data.sph_grid16 );
     766        3041 :         hMasa->data.sph_grid16 = NULL;
     767             :     }
     768             : 
     769       20336 :     if ( hMasa->data.extOutMeta != NULL )
     770             :     {
     771        1041 :         free( hMasa->data.extOutMeta );
     772        1041 :         hMasa->data.extOutMeta = NULL;
     773             :     }
     774             : 
     775       20336 :     if ( hMasa->hMasaLfeSynth != NULL )
     776             :     {
     777        4047 :         if ( hMasa->hMasaLfeSynth->lfeSynthRingBuffer != NULL )
     778             :         {
     779         319 :             free( hMasa->hMasaLfeSynth->lfeSynthRingBuffer );
     780         319 :             hMasa->hMasaLfeSynth->lfeSynthRingBuffer = NULL;
     781             :         }
     782        4047 :         if ( hMasa->hMasaLfeSynth->lfeSynthRingBuffer2 != NULL )
     783             :         {
     784         319 :             free( hMasa->hMasaLfeSynth->lfeSynthRingBuffer2 );
     785         319 :             hMasa->hMasaLfeSynth->lfeSynthRingBuffer2 = NULL;
     786             :         }
     787        4047 :         if ( hMasa->hMasaLfeSynth->delayBuffer_syncLp != NULL )
     788             :         {
     789         319 :             free( hMasa->hMasaLfeSynth->delayBuffer_syncLp );
     790         319 :             hMasa->hMasaLfeSynth->delayBuffer_syncLp = NULL;
     791             :         }
     792        4047 :         if ( hMasa->hMasaLfeSynth->delayBuffer_syncDirAC != NULL )
     793             :         {
     794         319 :             free( hMasa->hMasaLfeSynth->delayBuffer_syncDirAC );
     795         319 :             hMasa->hMasaLfeSynth->delayBuffer_syncDirAC = NULL;
     796             :         }
     797        4047 :         free( hMasa->hMasaLfeSynth );
     798        4047 :         hMasa->hMasaLfeSynth = NULL;
     799             :     }
     800             : 
     801       20336 :     free( *hMasa_out );
     802       20336 :     *hMasa_out = NULL;
     803             : 
     804       20336 :     return;
     805             : }
     806             : 
     807             : 
     808             : /*-------------------------------------------------------------------*
     809             :  * ivas_masa_dec_config()
     810             :  *
     811             :  * Frame-by-frame configuration of MASA decoder
     812             :  *-------------------------------------------------------------------*/
     813             : 
     814     1017656 : static ivas_error ivas_masa_dec_config(
     815             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder handle  */
     816             : )
     817             : {
     818             :     int16_t i;
     819             :     MASA_DECODER_HANDLE hMasa;
     820             :     uint8_t maxBand;
     821             :     int16_t maxBin;
     822             :     ivas_error error;
     823             :     int32_t ivas_total_brate;
     824             :     int32_t ism_total_brate;
     825     1017656 :     error = IVAS_ERR_OK;
     826     1017656 :     hMasa = st_ivas->hMasa;
     827             : 
     828     1017656 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
     829     1017656 :     ism_total_brate = 0;
     830     1017656 :     if ( st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->nSCE > 0 && ( st_ivas->ism_mode == ISM_MASA_MODE_DISC || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ ) )
     831             :     {
     832     1213012 :         for ( i = 0; i < st_ivas->nSCE; i++ )
     833             :         {
     834      789182 :             ism_total_brate += st_ivas->hSCE[i]->element_brate;
     835             :         }
     836             :     }
     837             : 
     838     1017656 :     ivas_masa_set_elements( ivas_total_brate, st_ivas->mc_mode, st_ivas->nchan_transport, st_ivas->hQMetaData, &st_ivas->element_mode_init, &st_ivas->nSCE, &st_ivas->nCPE, st_ivas->ivas_format, st_ivas->ism_mode, ism_total_brate );
     839             : 
     840     1017656 :     if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
     841             :     {
     842      423830 :         ivas_masa_set_coding_config( &( hMasa->config ), hMasa->data.band_mapping, st_ivas->hCPE[0]->element_brate, st_ivas->nchan_transport, MC_MODE_NONE );
     843             :     }
     844             :     else
     845             :     {
     846      593826 :         ivas_masa_set_coding_config( &( hMasa->config ), hMasa->data.band_mapping, st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->nchan_transport, ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCMASA ) );
     847             :     }
     848             : 
     849     1017656 :     if ( ( st_ivas->ivas_format == MASA_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT ) && st_ivas->hDecoderConfig->ivas_total_brate == IVAS_512k )
     850             :     {
     851       60006 :         hMasa->config.mergeRatiosOverSubframes = 0;
     852             : 
     853             :         /* initialize spherical grid */
     854       60006 :         if ( hMasa->data.sph_grid16 == NULL )
     855             :         {
     856        2000 :             if ( ( hMasa->data.sph_grid16 = (SPHERICAL_GRID_DATA *) malloc( sizeof( SPHERICAL_GRID_DATA ) ) ) == NULL )
     857             :             {
     858           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MASA data handle\n" ) );
     859             :             }
     860        2000 :             generate_gridEq( hMasa->data.sph_grid16 );
     861             :         }
     862             :     }
     863     1017656 :     st_ivas->hQMetaData->metadata_max_bits = hMasa->config.max_metadata_bits;
     864     1017656 :     st_ivas->hQMetaData->bandMap = hMasa->data.band_mapping;
     865     1017656 :     st_ivas->hQMetaData->nchan_transport = st_ivas->nchan_transport;
     866             : 
     867     1017656 :     if ( ( error = ivas_qmetadata_allocate_memory( st_ivas->hQMetaData, hMasa->config.numCodingBands, hMasa->config.numberOfDirections, hMasa->config.useCoherence ) ) != IVAS_ERR_OK )
     868             :     {
     869           0 :         return error;
     870             :     }
     871             : 
     872     1017656 :     st_ivas->hQMetaData->numTwoDirBands = st_ivas->hMasa->config.numTwoDirBands;
     873     1017656 :     st_ivas->hQMetaData->useLowerRes = 0;
     874             : 
     875     2077714 :     for ( i = 0; i < st_ivas->hQMetaData->no_directions; i++ )
     876             :     {
     877     1060058 :         st_ivas->hQMetaData->q_direction[i].cfg.nbands = hMasa->config.numCodingBands;
     878     1060058 :         st_ivas->hQMetaData->q_direction[i].cfg.nblocks = hMasa->config.joinedSubframes == TRUE ? 1 : MAX_PARAM_SPATIAL_SUBFRAMES;
     879             : 
     880     1060058 :         if ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCMASA )
     881             :         {
     882      140735 :             st_ivas->hQMetaData->q_direction[i].cfg.mc_ls_setup = ivas_mc_map_output_config_to_mc_ls_setup( st_ivas->transport_config );
     883             :         }
     884             :         else
     885             :         {
     886      919323 :             st_ivas->hQMetaData->q_direction[i].cfg.mc_ls_setup = MC_LS_SETUP_INVALID;
     887             :         }
     888             :     }
     889             : 
     890     1017656 :     ivas_set_qmetadata_maxbit_req( st_ivas->hQMetaData, st_ivas->ivas_format );
     891             : 
     892             :     /* Find maximum band usable */
     893     1017656 :     maxBin = (int16_t) ( st_ivas->hDecoderConfig->output_Fs * INV_CLDFB_BANDWIDTH );
     894     1017656 :     maxBand = 0;
     895    25066727 :     while ( maxBand <= MASA_FREQUENCY_BANDS && MASA_band_grouping_24[maxBand] <= maxBin )
     896             :     {
     897    24049071 :         maxBand++;
     898             :     }
     899     1017656 :     maxBand--;
     900             : 
     901     1017656 :     if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
     902             :     {
     903             :         /* need to apply the sampling rate correction also for the EXT output MASA meta buffer */
     904       68877 :         masa_sample_rate_band_correction( &( hMasa->config ), hMasa->data.band_mapping, st_ivas->hQMetaData, maxBand, 0, hMasa->data.extOutMeta );
     905             :     }
     906             :     else
     907             :     {
     908      948779 :         masa_sample_rate_band_correction( &( hMasa->config ), hMasa->data.band_mapping, st_ivas->hQMetaData, maxBand, 0, NULL );
     909             :     }
     910             : 
     911     1017656 :     return error;
     912             : }
     913             : 
     914             : 
     915             : /*-------------------------------------------------------------------*
     916             :  * ivas_masa_prerender()
     917             :  *
     918             :  * Apply gaining and copying of transport signals when needed
     919             :  *-------------------------------------------------------------------*/
     920             : 
     921      493613 : void ivas_masa_prerender(
     922             :     Decoder_Struct *st_ivas,     /* i/o: IVAS decoder handle                         */
     923             :     float *output[],             /* i/o: synthesized core-coder transport channels   */
     924             :     const int16_t output_frame,  /* i  : output frame length per channel             */
     925             :     const int16_t nchan_remapped /* i  : number of transports used in core           */
     926             : )
     927             : {
     928      493613 :     if ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->nchan_transport == 2 && nchan_remapped == 1 )
     929             :     {
     930      130185 :         if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
     931             :         {
     932        7668 :             mvr2r( output[0], output[1], output_frame ); /* Copy mono signal to stereo output channels */
     933             :         }
     934             :         else
     935             :         {
     936      122517 :             if ( st_ivas->renderer_type == RENDERER_DIRAC || st_ivas->renderer_type == RENDERER_DISABLE )
     937             :             {
     938       67911 :                 v_multc( output[0], SQRT2, output[0], output_frame ); /* Gain transport signal when transmitting mono with cpe in order to match loudness */
     939             :             }
     940             :         }
     941             :     }
     942             : 
     943      493613 :     return;
     944             : }
     945             : 
     946             : 
     947             : /*-------------------------------------------------------------------*
     948             :  * Local functions
     949             :  *-------------------------------------------------------------------*/
     950             : 
     951       60129 : static void index_16bits(
     952             :     IVAS_QMETADATA_HANDLE hQMetaData,
     953             :     SPHERICAL_GRID_DATA *Sph_Grid16 )
     954             : {
     955             :     int16_t d, band, block;
     956             : 
     957      126780 :     for ( d = 0; d < hQMetaData->no_directions; d++ )
     958             :     {
     959             :         /* Note: The band information is read from the first direction as it contains the full information.  */
     960      680912 :         for ( band = 0; band < hQMetaData->q_direction[0].cfg.nbands; band++ )
     961             :         {
     962     2655835 :             for ( block = 0; block < hQMetaData->q_direction[0].cfg.nblocks; block++ )
     963             :             {
     964     2041574 :                 hQMetaData->q_direction[d].band_data[band].spherical_index[block] = index_theta_phi_16( &( hQMetaData->q_direction[d].band_data[band].elevation[block] ),
     965     2041574 :                                                                                                         &( hQMetaData->q_direction[d].band_data[band].azimuth[block] ), Sph_Grid16 );
     966             :             }
     967             :         }
     968             :     }
     969             : 
     970       60129 :     return;
     971             : }
     972             : 
     973             : 
     974             : /* Replicate subframe data when there is only one subframe sent */
     975       18090 : static void replicate_subframes(
     976             :     IVAS_QMETADATA_HANDLE hQMetaData )
     977             : {
     978             :     int16_t sf, band, dir, nbands, ndirs;
     979             : 
     980       18090 :     nbands = hQMetaData->q_direction->cfg.nbands;
     981       18090 :     ndirs = hQMetaData->no_directions;
     982             : 
     983       72360 :     for ( sf = 1; sf < MAX_PARAM_SPATIAL_SUBFRAMES; sf++ )
     984             :     {
     985      661239 :         for ( band = 0; band < nbands; band++ )
     986             :         {
     987     1399158 :             for ( dir = 0; dir < ndirs; dir++ )
     988             :             {
     989      792189 :                 hQMetaData->q_direction[dir].band_data[band].azimuth[sf] = hQMetaData->q_direction[dir].band_data[band].azimuth[0];
     990      792189 :                 hQMetaData->q_direction[dir].band_data[band].elevation[sf] = hQMetaData->q_direction[dir].band_data[band].elevation[0];
     991      792189 :                 hQMetaData->q_direction[dir].band_data[band].spherical_index[sf] = hQMetaData->q_direction[dir].band_data[band].spherical_index[0];
     992      792189 :                 hQMetaData->q_direction[dir].band_data[band].energy_ratio[sf] = hQMetaData->q_direction[dir].band_data[band].energy_ratio[0];
     993             : 
     994      792189 :                 if ( hQMetaData->q_direction[dir].coherence_band_data != NULL )
     995             :                 {
     996      625482 :                     hQMetaData->q_direction[dir].coherence_band_data[band].spread_coherence[sf] = hQMetaData->q_direction[dir].coherence_band_data[band].spread_coherence[0];
     997             :                 }
     998             :             }
     999             : 
    1000      606969 :             if ( hQMetaData->surcoh_band_data != NULL )
    1001             :             {
    1002      440262 :                 hQMetaData->surcoh_band_data[band].surround_coherence[sf] = hQMetaData->surcoh_band_data[band].surround_coherence[0];
    1003             :             }
    1004             :         }
    1005             :     }
    1006             : 
    1007       18090 :     return;
    1008             : }
    1009             : 
    1010             : 
    1011      426758 : static void restore_lowbitrate_masa(
    1012             :     IVAS_QMETADATA_HANDLE hQMetaData,
    1013             :     const int16_t low_bitrate_mode,
    1014             :     const int16_t numCodingBands )
    1015             : {
    1016             :     int16_t sf, band;
    1017             : 
    1018      426758 :     if ( low_bitrate_mode == 1 )
    1019             :     {
    1020             :         /* With signal 1, we are in 5 frequency bands, 1 subframe mode. */
    1021             :         /* Replicate data to all subframes */
    1022     1421756 :         for ( sf = 1; sf < MAX_PARAM_SPATIAL_SUBFRAMES; sf++ )
    1023             :         {
    1024     6397902 :             for ( band = 0; band < numCodingBands; band++ )
    1025             :             {
    1026     5331585 :                 hQMetaData->q_direction[0].band_data[band].azimuth[sf] = hQMetaData->q_direction[0].band_data[band].azimuth[0];
    1027     5331585 :                 hQMetaData->q_direction[0].band_data[band].elevation[sf] = hQMetaData->q_direction[0].band_data[band].elevation[0];
    1028     5331585 :                 hQMetaData->q_direction[0].band_data[band].spherical_index[sf] = hQMetaData->q_direction[0].band_data[band].spherical_index[0];
    1029     5331585 :                 hQMetaData->q_direction[0].band_data[band].energy_ratio[sf] = hQMetaData->q_direction[0].band_data[band].energy_ratio[0];
    1030     5331585 :                 if ( hQMetaData->q_direction[0].coherence_band_data != NULL )
    1031             :                 {
    1032      693300 :                     hQMetaData->q_direction[0].coherence_band_data[band].spread_coherence[sf] = hQMetaData->q_direction[0].coherence_band_data[band].spread_coherence[0];
    1033             :                 }
    1034     5331585 :                 if ( hQMetaData->surcoh_band_data != NULL )
    1035             :                 {
    1036      693300 :                     hQMetaData->surcoh_band_data[band].surround_coherence[sf] = hQMetaData->surcoh_band_data[band].surround_coherence[0];
    1037             :                 }
    1038             :             }
    1039             :         }
    1040      355439 :         hQMetaData->q_direction->cfg.nblocks = 4;
    1041             :     }
    1042             :     else
    1043             :     {
    1044             :         /* With signal 0, we are in 1 frequency bands, 4 subframes mode. */
    1045             :         /* Replicate data to all bands */
    1046      356595 :         for ( sf = 0; sf < MAX_PARAM_SPATIAL_SUBFRAMES; sf++ )
    1047             :         {
    1048     1426380 :             for ( band = 1; band < numCodingBands; band++ )
    1049             :             {
    1050     1141104 :                 hQMetaData->q_direction[0].band_data[band].azimuth[sf] = hQMetaData->q_direction[0].band_data[0].azimuth[sf];
    1051     1141104 :                 hQMetaData->q_direction[0].band_data[band].elevation[sf] = hQMetaData->q_direction[0].band_data[0].elevation[sf];
    1052     1141104 :                 hQMetaData->q_direction[0].band_data[band].spherical_index[sf] = hQMetaData->q_direction[0].band_data[0].spherical_index[sf];
    1053     1141104 :                 hQMetaData->q_direction[0].band_data[band].energy_ratio[sf] = hQMetaData->q_direction[0].band_data[0].energy_ratio[sf];
    1054     1141104 :                 if ( hQMetaData->q_direction[0].coherence_band_data != NULL )
    1055             :                 {
    1056      157856 :                     hQMetaData->q_direction[0].coherence_band_data[band].spread_coherence[sf] = hQMetaData->q_direction[0].coherence_band_data[0].spread_coherence[sf];
    1057             :                 }
    1058     1141104 :                 if ( hQMetaData->surcoh_band_data != NULL )
    1059             :                 {
    1060      157856 :                     hQMetaData->surcoh_band_data[band].surround_coherence[sf] = hQMetaData->surcoh_band_data[0].surround_coherence[sf];
    1061             :                 }
    1062             :             }
    1063             :         }
    1064       71319 :         hQMetaData->q_direction->cfg.nbands = numCodingBands;
    1065             :     }
    1066             : 
    1067      426758 :     return;
    1068             : }
    1069             : 
    1070             : 
    1071        4047 : static ivas_error init_lfe_synth_data(
    1072             :     Decoder_Struct *st_ivas,  /* i  : IVAS decoder struct     */
    1073             :     MASA_DECODER_HANDLE hMasa /* i/o: MASA decoder structure  */
    1074             : )
    1075             : {
    1076             :     int32_t output_Fs;
    1077             :     AUDIO_CONFIG output_config;
    1078             : 
    1079        4047 :     output_Fs = st_ivas->hDecoderConfig->output_Fs;
    1080        4047 :     output_config = st_ivas->hDecoderConfig->output_config;
    1081             : 
    1082        4047 :     if ( ( hMasa->hMasaLfeSynth = (MCMASA_LFE_SYNTH_DATA_HANDLE) malloc( sizeof( MCMASA_LFE_SYNTH_DATA ) ) ) == NULL )
    1083             :     {
    1084           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MASA decoder\n" ) );
    1085             :     }
    1086             : 
    1087        4047 :     hMasa->hMasaLfeSynth->transportEneSmooth = 0.0f;
    1088        4047 :     hMasa->hMasaLfeSynth->protoLfeEneSmooth = 0.0f;
    1089        4047 :     hMasa->hMasaLfeSynth->targetEneLfeSmooth = 0.0f;
    1090        4047 :     hMasa->hMasaLfeSynth->targetEneTransSmooth = 0.0f;
    1091             : 
    1092        4047 :     set_zero( hMasa->hMasaLfeSynth->lfeToTotalEnergyRatio, MAX_PARAM_SPATIAL_SUBFRAMES );
    1093        4047 :     hMasa->hMasaLfeSynth->lfeGainPrevIndex = 0;
    1094             : 
    1095        4047 :     if ( st_ivas->hOutSetup.separateChannelEnabled &&
    1096         528 :          ( output_config == IVAS_AUDIO_CONFIG_5_1 || output_config == IVAS_AUDIO_CONFIG_7_1 ||
    1097         468 :            output_config == IVAS_AUDIO_CONFIG_5_1_2 ||
    1098         436 :            output_config == IVAS_AUDIO_CONFIG_5_1_4 || output_config == IVAS_AUDIO_CONFIG_7_1_4 ||
    1099         347 :            output_config == IVAS_AUDIO_CONFIG_EXTERNAL ||
    1100         317 :            output_config == IVAS_AUDIO_CONFIG_FOA || output_config == IVAS_AUDIO_CONFIG_HOA2 ||
    1101         242 :            output_config == IVAS_AUDIO_CONFIG_HOA3 ||
    1102           0 :            ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.num_lfe > 0 ) ) )
    1103         319 :     {
    1104             :         int16_t bufferSize;
    1105             :         int16_t i;
    1106             :         int16_t slot_size;
    1107             : 
    1108             :         /* Ring buffer for the filterbank of the LFE synthesis.
    1109             :          * The filterbank is using moving average lowpass filter with the crossover of 120 Hz. */
    1110         319 :         bufferSize = (int16_t) ( ( output_Fs / FRAMES_PER_SEC ) / MAX_PARAM_SPATIAL_SUBFRAMES );
    1111         319 :         if ( ( hMasa->hMasaLfeSynth->lfeSynthRingBuffer = (float *) malloc( bufferSize * sizeof( float ) ) ) == NULL )
    1112             :         {
    1113           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MASA decoder\n" ) );
    1114             :         }
    1115         319 :         set_zero( hMasa->hMasaLfeSynth->lfeSynthRingBuffer, bufferSize );
    1116         319 :         hMasa->hMasaLfeSynth->ringBufferLoPointer = 0;
    1117         319 :         hMasa->hMasaLfeSynth->ringBufferHiPointer = bufferSize / 2;
    1118         319 :         hMasa->hMasaLfeSynth->lowpassSum = 0.0f;
    1119         319 :         hMasa->hMasaLfeSynth->ringBufferSize = bufferSize;
    1120             : 
    1121             :         /* Ring buffer for additional lowpass filter for the LFE signal.
    1122             :          * Moving average lowpass filter with the crossover of 240 Hz. */
    1123         319 :         bufferSize /= 2;
    1124         319 :         if ( ( hMasa->hMasaLfeSynth->lfeSynthRingBuffer2 = (float *) malloc( bufferSize * sizeof( float ) ) ) == NULL )
    1125             :         {
    1126           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MASA decoder\n" ) );
    1127             :         }
    1128         319 :         set_zero( hMasa->hMasaLfeSynth->lfeSynthRingBuffer2, bufferSize );
    1129         319 :         hMasa->hMasaLfeSynth->ringBufferLoPointer2 = 0;
    1130         319 :         hMasa->hMasaLfeSynth->lowpassSum2 = 0.0f;
    1131         319 :         hMasa->hMasaLfeSynth->ringBufferSize2 = bufferSize;
    1132             : 
    1133             :         /* Delay buffer for matching the delay of the lowpass filter */
    1134         319 :         bufferSize /= 2; /* The delay of the moving average lowpass filter is bufferSize / 2 */
    1135         319 :         if ( ( hMasa->hMasaLfeSynth->delayBuffer_syncLp = (float *) malloc( bufferSize * sizeof( float ) ) ) == NULL )
    1136             :         {
    1137           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MASA decoder\n" ) );
    1138             :         }
    1139         319 :         set_zero( hMasa->hMasaLfeSynth->delayBuffer_syncLp, bufferSize );
    1140         319 :         hMasa->hMasaLfeSynth->delayBuffer_syncLp_size = bufferSize;
    1141             : 
    1142             :         /* Delay buffer for syncing with DirAC rendering */
    1143         319 :         bufferSize = NS2SA( output_Fs, IVAS_FB_DEC_DELAY_NS ) - hMasa->hMasaLfeSynth->ringBufferSize / 2 - hMasa->hMasaLfeSynth->ringBufferSize2 / 2;
    1144         319 :         if ( ( hMasa->hMasaLfeSynth->delayBuffer_syncDirAC = (float *) malloc( bufferSize * sizeof( float ) ) ) == NULL )
    1145             :         {
    1146           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MASA decoder\n" ) );
    1147             :         }
    1148         319 :         set_zero( hMasa->hMasaLfeSynth->delayBuffer_syncDirAC, bufferSize );
    1149         319 :         hMasa->hMasaLfeSynth->delayBuffer_syncDirAC_size = bufferSize;
    1150             : 
    1151             :         /* Interpolation between slots */
    1152         319 :         hMasa->hMasaLfeSynth->lfeGainPrev = 0.0f;
    1153         319 :         hMasa->hMasaLfeSynth->transportGainPrev = 1.0f;
    1154         319 :         slot_size = (int16_t) ( ( output_Fs / FRAMES_PER_SEC ) / CLDFB_NO_COL_MAX );
    1155       13939 :         for ( i = 0; i < slot_size; i++ )
    1156             :         {
    1157       13620 :             hMasa->hMasaLfeSynth->interpolator[i] = ( (float) ( i + 1 ) ) / ( (float) slot_size );
    1158             :         }
    1159             :     }
    1160        3728 :     else if ( st_ivas->hOutSetup.separateChannelEnabled && output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.num_lfe == 0 )
    1161           0 :     {
    1162             :         int16_t bufferSize;
    1163             : 
    1164             :         /* Delay buffer for syncing with DirAC rendering */
    1165           0 :         bufferSize = NS2SA( output_Fs, IVAS_FB_DEC_DELAY_NS );
    1166           0 :         if ( ( hMasa->hMasaLfeSynth->delayBuffer_syncDirAC = (float *) malloc( bufferSize * sizeof( float ) ) ) == NULL )
    1167             :         {
    1168           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MASA decoder\n" ) );
    1169             :         }
    1170           0 :         set_zero( hMasa->hMasaLfeSynth->delayBuffer_syncDirAC, bufferSize );
    1171           0 :         hMasa->hMasaLfeSynth->delayBuffer_syncDirAC_size = bufferSize;
    1172             : 
    1173           0 :         hMasa->hMasaLfeSynth->lfeSynthRingBuffer = NULL;
    1174           0 :         hMasa->hMasaLfeSynth->lfeSynthRingBuffer2 = NULL;
    1175           0 :         hMasa->hMasaLfeSynth->delayBuffer_syncLp = NULL;
    1176             :     }
    1177             :     else
    1178             :     {
    1179        3728 :         hMasa->hMasaLfeSynth->lfeSynthRingBuffer = NULL;
    1180        3728 :         hMasa->hMasaLfeSynth->lfeSynthRingBuffer2 = NULL;
    1181        3728 :         hMasa->hMasaLfeSynth->delayBuffer_syncLp = NULL;
    1182        3728 :         hMasa->hMasaLfeSynth->delayBuffer_syncDirAC = NULL;
    1183             :     }
    1184             : 
    1185        4047 :     return IVAS_ERR_OK;
    1186             : }
    1187             : 
    1188             : 
    1189             : /*! r: Number of bits read */
    1190      140735 : static int16_t decode_lfe_to_total_energy_ratio(
    1191             :     MCMASA_LFE_SYNTH_DATA_HANDLE hMasaLfeSynth, /* i/o: McMASA LFE structure */
    1192             :     uint16_t *bitstream,                        /* i  : bitstream            */
    1193             :     int16_t *index,                             /* i/o: bitstream position   */
    1194             :     const int32_t ivas_total_brate              /* i  : total bitrate        */
    1195             : )
    1196             : {
    1197             :     int16_t i;
    1198             :     int16_t lfeToTotalEnergyRatioIndices[3];
    1199             :     int16_t VQBits;
    1200             :     float log2LFEaverage;
    1201             :     float lfeToTotalEnergyRatioTemp;
    1202             :     uint16_t byteBuffer;
    1203             :     int16_t lfeBitsRead;
    1204             : 
    1205      140735 :     lfeBitsRead = 0;
    1206      140735 :     byteBuffer = bitstream[( *index )--];
    1207      140735 :     lfeBitsRead += 1;
    1208      140735 :     lfeToTotalEnergyRatioIndices[0] = byteBuffer; /* First LFE index */
    1209             : 
    1210      140735 :     if ( ivas_total_brate == IVAS_13k2 ) /* 1-bit adaptive LFE gain quantizer at 13.2 kbps */
    1211             :     {
    1212       22810 :         lfeToTotalEnergyRatioTemp = hMasaLfeSynth->lfeToTotalEnergyRatio[3]; /* Take memory from the last subframe */
    1213       22810 :         if ( lfeToTotalEnergyRatioIndices[0] == 1 )
    1214             :         {
    1215        4694 :             if ( hMasaLfeSynth->lfeGainPrevIndex == 1 )
    1216             :             {
    1217        2864 :                 lfeToTotalEnergyRatioTemp = lfeToTotalEnergyRatioTemp + MCMASA_LFE_THETA * MCMASA_LFE_BETA; /* larger "bump-up" energy */
    1218             :             }
    1219             :             else
    1220             :             {
    1221        1830 :                 lfeToTotalEnergyRatioTemp = lfeToTotalEnergyRatioTemp + MCMASA_LFE_BETA; /* "bump-up" energy */
    1222             :             }
    1223             :         }
    1224             :         else
    1225             :         {
    1226       18116 :             lfeToTotalEnergyRatioTemp = MCMASA_LFE_ALPHA * lfeToTotalEnergyRatioTemp; /* exponential decay */
    1227             :         }
    1228       22810 :         if ( lfeToTotalEnergyRatioTemp > 1.0f )
    1229             :         {
    1230           0 :             lfeToTotalEnergyRatioTemp = 1.0f;
    1231             :         }
    1232       22810 :         hMasaLfeSynth->lfeGainPrevIndex = lfeToTotalEnergyRatioIndices[0];
    1233      114050 :         for ( i = 0; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
    1234             :         {
    1235       91240 :             hMasaLfeSynth->lfeToTotalEnergyRatio[i] = lfeToTotalEnergyRatioTemp;
    1236             :         }
    1237             :     }
    1238             :     else /* Bitrates >= 16.4 kbps */
    1239             :     {
    1240      117925 :         if ( lfeToTotalEnergyRatioIndices[0] == 0 )
    1241             :         {
    1242      302150 :             for ( i = 0; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
    1243             :             {
    1244      241720 :                 hMasaLfeSynth->lfeToTotalEnergyRatio[i] = 0.0f;
    1245             :             }
    1246             :         }
    1247             :         else
    1248             :         {
    1249       57495 :             byteBuffer = bitstream[( *index )--] << 2;
    1250       57495 :             byteBuffer += bitstream[( *index )--] << 1;
    1251       57495 :             byteBuffer += bitstream[( *index )--];
    1252       57495 :             lfeBitsRead += 3;
    1253       57495 :             lfeToTotalEnergyRatioIndices[1] = byteBuffer; /* Scalar index */
    1254       57495 :             log2LFEaverage = usdequant( lfeToTotalEnergyRatioIndices[1], MCMASA_LFE_QLOW, MCMASA_LFE_DELTA );
    1255             : 
    1256             :             /* 16.4 kbps sends only scalar gain, above it VQ is used */
    1257       57495 :             if ( ivas_total_brate >= IVAS_24k4 )
    1258             :             {
    1259             :                 /* Depending on average (scalar) gain more bits are sent for VQ LFE gain */
    1260       47210 :                 switch ( lfeToTotalEnergyRatioIndices[1] )
    1261             :                 {
    1262        9682 :                     case 0:
    1263             :                     case 1:
    1264        9682 :                         VQBits = 0;
    1265        9682 :                         break;
    1266        6657 :                     case 2:
    1267        6657 :                         VQBits = 1;
    1268        6657 :                         break;
    1269        3730 :                     case 3:
    1270        3730 :                         VQBits = 2;
    1271        3730 :                         break;
    1272        4753 :                     case 4:
    1273        4753 :                         VQBits = 3;
    1274        4753 :                         break;
    1275       22388 :                     default:
    1276       22388 :                         VQBits = 4;
    1277             :                 }
    1278       47210 :                 byteBuffer = 0;
    1279      165138 :                 for ( i = 0; i < VQBits; i++ )
    1280             :                 {
    1281      117928 :                     byteBuffer += bitstream[( *index )--] << ( VQBits - 1 - i );
    1282      117928 :                     lfeBitsRead += 1;
    1283             :                 }
    1284       47210 :                 lfeToTotalEnergyRatioIndices[2] = byteBuffer; /* VQ index */
    1285             :             }
    1286             : 
    1287      287475 :             for ( i = 0; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
    1288             :             {
    1289      229980 :                 if ( ivas_total_brate == IVAS_16k4 )
    1290             :                 {
    1291       41140 :                     hMasaLfeSynth->lfeToTotalEnergyRatio[i] = exp2f( log2LFEaverage );
    1292             :                 }
    1293             :                 else
    1294             :                 {
    1295      188840 :                     hMasaLfeSynth->lfeToTotalEnergyRatio[i] = exp2f( log2LFEaverage + McMASA_LFEGain_vectors[4 * lfeToTotalEnergyRatioIndices[2] + i] );
    1296             :                 }
    1297      229980 :                 hMasaLfeSynth->lfeToTotalEnergyRatio[i] = min( hMasaLfeSynth->lfeToTotalEnergyRatio[i], 1.0f );
    1298      229980 :                 hMasaLfeSynth->lfeToTotalEnergyRatio[i] = max( hMasaLfeSynth->lfeToTotalEnergyRatio[i], 0.0f );
    1299             :             }
    1300             :         }
    1301             :     }
    1302             : 
    1303      140735 :     return lfeBitsRead;
    1304             : }
    1305             : 
    1306             : 
    1307             : /*-------------------------------------------------------------------*
    1308             :  * ivas_masa_dec_reconfigure()
    1309             :  *
    1310             :  * Reconfigure IVAS MASA decoder
    1311             :  *-------------------------------------------------------------------*/
    1312             : 
    1313       11823 : ivas_error ivas_masa_dec_reconfigure(
    1314             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure  */
    1315             : )
    1316             : {
    1317             :     int16_t n, tmp, num_bits;
    1318             :     int16_t sce_id, cpe_id;
    1319             :     uint16_t *bit_stream;
    1320             :     Decoder_State **sts;
    1321             :     int32_t ivas_total_brate, last_ivas_total_brate;
    1322             :     int16_t numCldfbAnalyses_old, numCldfbSyntheses_old;
    1323             :     int16_t nchan_out_buff;
    1324             :     ivas_error error;
    1325             :     int16_t pos_idx;
    1326             :     int32_t ism_total_brate;
    1327             : 
    1328       11823 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
    1329       11823 :     last_ivas_total_brate = st_ivas->hDecoderConfig->last_ivas_total_brate;
    1330             : 
    1331             :     /* Copy state to TC buffer if granularity matches and we are not in OMASA EXT rendering mode */
    1332       11823 :     if ( st_ivas->hSpatParamRendCom != NULL && st_ivas->hSpatParamRendCom->slot_size == st_ivas->hTcBuffer->n_samples_granularity && st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_EXTERNAL )
    1333             :     {
    1334       10206 :         mvs2s( st_ivas->hSpatParamRendCom->subframe_nbslots, st_ivas->hTcBuffer->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
    1335       10206 :         st_ivas->hTcBuffer->nb_subframes = st_ivas->hSpatParamRendCom->nb_subframes;
    1336       10206 :         st_ivas->hTcBuffer->subframes_rendered = st_ivas->hSpatParamRendCom->subframes_rendered;
    1337             :     }
    1338             : 
    1339       11823 :     ivas_init_dec_get_num_cldfb_instances( st_ivas, &numCldfbAnalyses_old, &numCldfbSyntheses_old );
    1340             : 
    1341             :     /* renderer might have changed, reselect */
    1342       11823 :     ivas_renderer_select( st_ivas );
    1343             : 
    1344       11823 :     if ( ( st_ivas->renderer_type == RENDERER_DIRAC && st_ivas->hDirACRend == NULL ) ||
    1345       11655 :          ( ( st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM ) && st_ivas->hDiracDecBin[0] == NULL ) )
    1346             :     {
    1347             :         /* init a new DirAC dec */
    1348         168 :         if ( ( error = ivas_dirac_dec_config( st_ivas, DIRAC_OPEN ) ) != IVAS_ERR_OK )
    1349             :         {
    1350           0 :             return error;
    1351             :         }
    1352             :     }
    1353       11655 :     else if ( st_ivas->renderer_type == RENDERER_DISABLE || st_ivas->renderer_type == RENDERER_MONO_DOWNMIX || st_ivas->renderer_type == RENDERER_OMASA_MIX_EXT )
    1354             :     {
    1355             :         /* close all unnecessary parametric decoding and rendering */
    1356        1116 :         ivas_dirac_dec_close_binaural_data( st_ivas->hDiracDecBin );
    1357        1116 :         ivas_dirac_rend_close( &( st_ivas->hDirACRend ) );
    1358        1116 :         ivas_spat_hSpatParamRendCom_close( &( st_ivas->hSpatParamRendCom ) );
    1359        1116 :         ivas_dirac_dec_close( &( st_ivas->hDirAC ) );
    1360             :     }
    1361             :     /* possible reconfigure is done later */
    1362             : 
    1363             :     /*-----------------------------------------------------------------*
    1364             :      * Allocate and initialize SCE/CPE and other handles
    1365             :      *-----------------------------------------------------------------*/
    1366             : 
    1367       11823 :     bit_stream = st_ivas->hSCE[0] != NULL ? st_ivas->hSCE[0]->hCoreCoder[0]->bit_stream : st_ivas->hCPE[0]->hCoreCoder[0]->bit_stream;
    1368             : 
    1369       11823 :     num_bits = 0;
    1370             : 
    1371       24381 :     for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    1372             :     {
    1373       12558 :         st_ivas->hSCE[sce_id]->element_brate = ivas_total_brate / st_ivas->nchan_transport;
    1374       12558 :         st_ivas->hSCE[sce_id]->hCoreCoder[0]->total_brate = st_ivas->hSCE[sce_id]->element_brate; /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
    1375       12558 :         sts = st_ivas->hSCE[sce_id]->hCoreCoder;
    1376       12558 :         sts[0]->bit_stream = bit_stream + num_bits;
    1377       12558 :         num_bits += (int16_t) ( st_ivas->hSCE[sce_id]->element_brate / FRAMES_PER_SEC );
    1378             : 
    1379       12558 :         if ( ( st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM ) && st_ivas->hDiracDecBin[0] != NULL )
    1380             :         {
    1381        5304 :             if ( ( error = ivas_dirac_dec_config( st_ivas, DIRAC_RECONFIGURE ) ) != IVAS_ERR_OK )
    1382             :             {
    1383           0 :                 return error;
    1384             :             }
    1385             :         }
    1386             :     }
    1387             : 
    1388       21276 :     for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    1389             :     {
    1390        9453 :         st_ivas->hCPE[cpe_id]->element_brate = ( ivas_total_brate / st_ivas->nchan_transport ) * CPE_CHANNELS;
    1391             : 
    1392             :         /* prepare bitstream buffers */
    1393       28359 :         for ( n = 0; n < CPE_CHANNELS; n++ )
    1394             :         {
    1395       18906 :             st_ivas->hCPE[cpe_id]->hCoreCoder[n]->total_brate = st_ivas->hCPE[cpe_id]->element_brate / ( st_ivas->nCPE > 1 ? 1 : CPE_CHANNELS ); /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
    1396             :         }
    1397        9453 :         sts = st_ivas->hCPE[cpe_id]->hCoreCoder;
    1398        9453 :         sts[0]->bit_stream = bit_stream + num_bits;
    1399        9453 :         num_bits += (int16_t) ( st_ivas->hCPE[cpe_id]->element_brate / FRAMES_PER_SEC );
    1400        9453 :         if ( ( ivas_total_brate < MASA_STEREO_MIN_BITRATE && last_ivas_total_brate >= MASA_STEREO_MIN_BITRATE ) ||
    1401        7242 :              ( ivas_total_brate < MASA_STEREO_MIN_BITRATE && last_ivas_total_brate == FRAME_NO_DATA ) ||
    1402          24 :              ( ivas_total_brate < MASA_STEREO_MIN_BITRATE && last_ivas_total_brate == IVAS_SID_5k2 ) )
    1403             :         {
    1404        2235 :             st_ivas->hCPE[cpe_id]->nchan_out = 1;
    1405        2235 :             if ( ( st_ivas->renderer_type == RENDERER_DIRAC && st_ivas->hDirACRend != NULL ) || ( ( st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM ) && st_ivas->hDiracDecBin[0] != NULL ) )
    1406             :             {
    1407        1881 :                 if ( ( error = ivas_dirac_dec_config( st_ivas, DIRAC_RECONFIGURE ) ) != IVAS_ERR_OK )
    1408             :                 {
    1409           0 :                     return error;
    1410             :                 }
    1411             :             }
    1412             :         }
    1413        7218 :         else if ( ivas_total_brate >= MASA_STEREO_MIN_BITRATE && last_ivas_total_brate < MASA_STEREO_MIN_BITRATE )
    1414             :         {
    1415        2274 :             st_ivas->hCPE[cpe_id]->nchan_out = CPE_CHANNELS;
    1416             : 
    1417        2274 :             if ( ( st_ivas->renderer_type == RENDERER_DIRAC && st_ivas->hDirACRend != NULL ) || ( ( st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM ) && st_ivas->hDiracDecBin[0] != NULL ) )
    1418             :             {
    1419        2043 :                 if ( ( error = ivas_dirac_dec_config( st_ivas, DIRAC_RECONFIGURE ) ) != IVAS_ERR_OK )
    1420             :                 {
    1421           0 :                     return error;
    1422             :                 }
    1423             :             }
    1424             :         }
    1425             :     }
    1426             : 
    1427      106407 :     for ( pos_idx = 0; pos_idx < MAX_HEAD_ROT_POSES; pos_idx++ )
    1428             :     {
    1429       94584 :         if ( st_ivas->hDiracDecBin[pos_idx] != NULL )
    1430             :         {
    1431             :             /* regularization factor is bitrate-dependent */
    1432        5235 :             st_ivas->hDiracDecBin[pos_idx]->reqularizationFactor = configure_reqularization_factor( st_ivas->ivas_format, st_ivas->hDecoderConfig->ivas_total_brate );
    1433             :         }
    1434             :     }
    1435             : 
    1436       11823 :     if ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->last_ivas_format == MASA_FORMAT ) /* note: switching within OMASA is handled in ivas_omasa_dec_config() */
    1437             :     {
    1438             :         /*-----------------------------------------------------------------*
    1439             :          * TD Decorrelator
    1440             :          *-----------------------------------------------------------------*/
    1441             : 
    1442        4551 :         if ( st_ivas->hDiracDecBin[0] != NULL )
    1443             :         {
    1444        2031 :             if ( ( error = ivas_td_decorr_reconfig_dec( st_ivas->ivas_format, st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->nchan_transport, st_ivas->hDecoderConfig->output_Fs, &( st_ivas->hDiracDecBin[0]->hTdDecorr ), &( st_ivas->hDiracDecBin[0]->useTdDecorr ) ) ) != IVAS_ERR_OK )
    1445             :             {
    1446           0 :                 return error;
    1447             :             }
    1448             :         }
    1449             : 
    1450             :         /*-----------------------------------------------------------------*
    1451             :          * CLDFB instances
    1452             :          *-----------------------------------------------------------------*/
    1453             : 
    1454        4551 :         if ( ( error = ivas_cldfb_dec_reconfig( st_ivas, st_ivas->nchan_transport, numCldfbAnalyses_old, numCldfbSyntheses_old ) ) != IVAS_ERR_OK )
    1455             :         {
    1456           0 :             return error;
    1457             :         }
    1458             :     }
    1459             : 
    1460             :     /*-----------------------------------------------------------------*
    1461             :      * Set-up MASA coding elements and bitrates
    1462             :      *-----------------------------------------------------------------*/
    1463             : 
    1464       11823 :     ism_total_brate = 0;
    1465       11823 :     if ( st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->nSCE > 0 && ( st_ivas->ism_mode == ISM_MASA_MODE_DISC || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ ) )
    1466             :     {
    1467        8973 :         for ( n = 0; n < st_ivas->nSCE; n++ )
    1468             :         {
    1469        5937 :             ism_total_brate += st_ivas->hSCE[n]->element_brate;
    1470             :         }
    1471             :     }
    1472             : 
    1473       11823 :     ivas_masa_set_elements( ivas_total_brate, st_ivas->mc_mode, st_ivas->nchan_transport, st_ivas->hQMetaData, &tmp, &tmp, &tmp, st_ivas->ivas_format, st_ivas->ism_mode, ism_total_brate );
    1474             : 
    1475             :     /*-----------------------------------------------------------------*
    1476             :      * JBM TC buffers
    1477             :      *-----------------------------------------------------------------*/
    1478             : 
    1479             :     {
    1480             :         int16_t tc_nchan_to_allocate;
    1481             :         int16_t tc_nchan_transport;
    1482             :         TC_BUFFER_MODE buffer_mode_new;
    1483             :         int16_t n_samples_granularity;
    1484             : 
    1485       11823 :         n_samples_granularity = ivas_jbm_dec_get_render_granularity( st_ivas->renderer_type, ivas_renderer_secondary_select( st_ivas ), st_ivas->hDecoderConfig->output_Fs );
    1486       11823 :         buffer_mode_new = ivas_jbm_dec_get_tc_buffer_mode( st_ivas );
    1487       11823 :         tc_nchan_transport = ivas_jbm_dec_get_num_tc_channels( st_ivas );
    1488             : 
    1489       11823 :         tc_nchan_to_allocate = tc_nchan_transport;
    1490       11823 :         if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
    1491             :         {
    1492        5235 :             if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
    1493             :             {
    1494        2250 :                 tc_nchan_to_allocate = BINAURAL_CHANNELS + st_ivas->nchan_ism;
    1495             :             }
    1496             :             else
    1497             :             {
    1498        2985 :                 tc_nchan_to_allocate = BINAURAL_CHANNELS;
    1499        2985 :                 if ( st_ivas->hDiracDecBin[0] != NULL && st_ivas->hDiracDecBin[0]->useTdDecorr )
    1500             :                 {
    1501        1461 :                     tc_nchan_to_allocate = 2 * BINAURAL_CHANNELS;
    1502             :                 }
    1503             :             }
    1504             : 
    1505        5235 :             if ( st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC && st_ivas->ism_mode == ISM_MASA_MODE_DISC )
    1506             :             {
    1507         435 :                 if ( n_samples_granularity > st_ivas->hTcBuffer->n_samples_granularity )
    1508             :                 {
    1509         363 :                     if ( ( error = ivas_jbm_dec_set_discard_samples( st_ivas ) ) != IVAS_ERR_OK )
    1510             :                     {
    1511           0 :                         return error;
    1512             :                     }
    1513             :                 }
    1514             :             }
    1515         513 :             else if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC && st_ivas->ism_mode != ISM_MASA_MODE_DISC )
    1516             :             {
    1517         513 :                 if ( n_samples_granularity < st_ivas->hTcBuffer->n_samples_granularity )
    1518             :                 {
    1519             :                     /* flush already done in IVAS_DEC_ReadFormat() */
    1520             :                 }
    1521             :             }
    1522             :         }
    1523        6588 :         else if ( st_ivas->nchan_transport == 1 && ( st_ivas->renderer_type == RENDERER_DIRAC && st_ivas->hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD ) )
    1524             :         {
    1525             :             /* addtl channel for CNG */
    1526         624 :             tc_nchan_to_allocate++;
    1527             :         }
    1528        5964 :         else if ( st_ivas->ivas_format == MASA_ISM_FORMAT && ( st_ivas->renderer_type == RENDERER_OMASA_MIX_EXT || st_ivas->renderer_type == RENDERER_OMASA_OBJECT_EXT ) )
    1529             :         {
    1530         111 :             tc_nchan_transport = st_ivas->nchan_transport + st_ivas->nchan_ism;
    1531         111 :             tc_nchan_to_allocate = st_ivas->nchan_transport + st_ivas->nchan_ism;
    1532             :         }
    1533             : 
    1534       11823 :         if ( tc_nchan_transport != st_ivas->hTcBuffer->nchan_transport_jbm || tc_nchan_to_allocate != st_ivas->hTcBuffer->nchan_transport_internal || buffer_mode_new != st_ivas->hTcBuffer->tc_buffer_mode )
    1535             :         {
    1536        7188 :             if ( ( error = ivas_jbm_dec_tc_buffer_reconfigure( st_ivas, buffer_mode_new, tc_nchan_transport, tc_nchan_to_allocate, tc_nchan_to_allocate, n_samples_granularity ) ) != IVAS_ERR_OK )
    1537             :             {
    1538           0 :                 return error;
    1539             :             }
    1540             :         }
    1541             : 
    1542       11823 :         if ( st_ivas->hSpatParamRendCom != NULL && st_ivas->hSpatParamRendCom->slot_size == st_ivas->hTcBuffer->n_samples_granularity )
    1543             :         {
    1544       10215 :             mvs2s( st_ivas->hTcBuffer->subframe_nbslots, st_ivas->hSpatParamRendCom->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
    1545       10215 :             st_ivas->hSpatParamRendCom->nb_subframes = st_ivas->hTcBuffer->nb_subframes;
    1546       10215 :             st_ivas->hSpatParamRendCom->subframes_rendered = st_ivas->hTcBuffer->subframes_rendered;
    1547             :         }
    1548             : 
    1549       11823 :         if ( st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC && st_ivas->ism_mode == ISM_MASA_MODE_DISC )
    1550             :         {
    1551         435 :             int16_t granularityMultiplier = st_ivas->hTcBuffer->n_samples_granularity / st_ivas->hSpatParamRendCom->slot_size;
    1552             : 
    1553        3915 :             for ( n = 0; n < MAX_JBM_SUBFRAMES_5MS; n++ )
    1554             :             {
    1555        3480 :                 st_ivas->hSpatParamRendCom->subframe_nbslots[n] = st_ivas->hTcBuffer->subframe_nbslots[n] * granularityMultiplier;
    1556             :             }
    1557             :         }
    1558             :     }
    1559             : 
    1560             :     /*-----------------------------------------------------------------*
    1561             :      * floating-point output audio buffers
    1562             :      *-----------------------------------------------------------------*/
    1563             : 
    1564       11823 :     if ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->last_ivas_format == MASA_FORMAT ) /* note: switching with OMASA is addressed in ivas_omasa_dec_config() */
    1565             :     {
    1566        4551 :         nchan_out_buff = ivas_get_nchan_buffers_dec( st_ivas, -1, -1 );
    1567        4551 :         if ( ( error = ivas_output_buff_dec( st_ivas->p_output_f, nchan_out_buff, st_ivas->hDecoderConfig->Opt_tsm, st_ivas->hTcBuffer ) ) != IVAS_ERR_OK )
    1568             :         {
    1569           0 :             return error;
    1570             :         }
    1571             :     }
    1572             : 
    1573       11823 :     return IVAS_ERR_OK;
    1574             : }
    1575             : 
    1576             : 
    1577             : /*-------------------------------------------------------------------*
    1578             :  * ivas_spar_param_to_masa_param_mapping()
    1579             :  *
    1580             :  * Determine MASA metadata from the SPAR metadata
    1581             :  *-------------------------------------------------------------------*/
    1582             : 
    1583     1912616 : void ivas_spar_param_to_masa_param_mapping(
    1584             :     Decoder_Struct *st_ivas,                                       /* i/o: IVAS decoder struct               */
    1585             :     float inRe[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX], /* i  : Input audio in CLDFB domain, real */
    1586             :     float inIm[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX], /* i  : Input audio in CLDFB domain, imag */
    1587             :     const int16_t subframe                                         /* i  : Subframe to map                   */
    1588             : )
    1589             : {
    1590             :     int16_t i, j, band, bin, slot, ch, nBins, nchan_transport;
    1591             :     int16_t mixer_mat_index;
    1592             :     int16_t dirac_write_idx;
    1593             :     DIRAC_DEC_HANDLE hDirAC;
    1594             :     DIFFUSE_DISTRIBUTION_HANDLE hDiffuseDist;
    1595             :     float mixer_mat_sf_bands_real[SPAR_DIRAC_SPLIT_START_BAND][FOA_CHANNELS][FOA_CHANNELS];
    1596             :     float mixer_mat_sf_bins_real[CLDFB_NO_CHANNELS_MAX][FOA_CHANNELS][FOA_CHANNELS];
    1597             :     int16_t *band_grouping;
    1598             :     int16_t band_start, band_end;
    1599             :     float transportSignalEnergies[2][CLDFB_NO_CHANNELS_MAX];
    1600             :     float transportSignalCrossCorrelation[CLDFB_NO_CHANNELS_MAX];
    1601             :     float instEne;
    1602             :     float inCovarianceMtx[FOA_CHANNELS][FOA_CHANNELS];
    1603             :     float foaCovarianceMtx[FOA_CHANNELS][FOA_CHANNELS];
    1604             :     float Iy, Iz, Ix, E, azi, ele, I, ratio;
    1605             :     float diffuseGainX, diffuseGainY, diffuseGainZ, diffuseGainSum;
    1606             :     int16_t slot_idx, slot_idx_start, sf;
    1607             :     SPAR_DEC_HANDLE hSpar;
    1608             :     float slot_fac;
    1609             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
    1610             : 
    1611             :     /* Set values */
    1612     1912616 :     hDirAC = st_ivas->hDirAC;
    1613     1912616 :     hSpatParamRendCom = st_ivas->hSpatParamRendCom;
    1614     1912616 :     hSpatParamRendCom->numParametricDirections = 1;
    1615     1912616 :     hSpatParamRendCom->numSimultaneousDirections = 1;
    1616     1912616 :     hDiffuseDist = st_ivas->hDiracDecBin[0]->hDiffuseDist;
    1617     1912616 :     nchan_transport = st_ivas->nchan_transport;
    1618     1912616 :     band_grouping = hDirAC->band_grouping;
    1619     1912616 :     hSpar = st_ivas->hSpar;
    1620     1912616 :     dirac_write_idx = hSpatParamRendCom->render_to_md_map[subframe];
    1621             : 
    1622             :     /* Init arrays */
    1623     9563080 :     for ( i = 0; i < FOA_CHANNELS; i++ )
    1624             :     {
    1625     7650464 :         set_zero( inCovarianceMtx[i], FOA_CHANNELS );
    1626             :     }
    1627             : 
    1628             :     /* Delay the SPAR mixing matrices to have them synced with the audio */
    1629     1912616 :     slot_idx_start = hSpar->slots_rendered;
    1630     1912616 :     slot_fac = 1.0f / (float) hSpar->subframe_nbslots[subframe];
    1631             : 
    1632     9555512 :     for ( slot_idx = 0; slot_idx < hSpar->subframe_nbslots[subframe]; slot_idx++ )
    1633             :     {
    1634     7642896 :         sf = hSpar->render_to_md_map[slot_idx + slot_idx_start] / JBM_CLDFB_SLOTS_IN_SUBFRAME;
    1635             : 
    1636     7642896 :         if ( ( sf < SPAR_META_DELAY_SUBFRAMES ) )
    1637             :         {
    1638     3822120 :             mixer_mat_index = sf + MAX_PARAM_SPATIAL_SUBFRAMES - SPAR_META_DELAY_SUBFRAMES + 1;
    1639    34399080 :             for ( band = 0; band < SPAR_DIRAC_SPLIT_START_BAND; band++ )
    1640             :             {
    1641   152884800 :                 for ( i = 0; i < FOA_CHANNELS; i++ )
    1642             :                 {
    1643   611539200 :                     for ( j = 0; j < FOA_CHANNELS; j++ )
    1644             :                     {
    1645   489231360 :                         mixer_mat_sf_bands_real[band][i][j] = slot_fac * st_ivas->hSpar->hMdDec->mixer_mat_prev[mixer_mat_index][i][j][band];
    1646             :                     }
    1647             :                 }
    1648             :             }
    1649             :         }
    1650             :         else
    1651             :         {
    1652     3820776 :             mixer_mat_index = ( ivas_get_spar_dec_md_num_subframes( st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->last_active_ivas_total_brate ) == 1 ) ? 0 : ( sf - SPAR_META_DELAY_SUBFRAMES );
    1653    34386984 :             for ( band = 0; band < SPAR_DIRAC_SPLIT_START_BAND; band++ )
    1654             :             {
    1655   152831040 :                 for ( i = 0; i < FOA_CHANNELS; i++ )
    1656             :                 {
    1657   611324160 :                     for ( j = 0; j < FOA_CHANNELS; j++ )
    1658             :                     {
    1659   489059328 :                         mixer_mat_sf_bands_real[band][i][j] = slot_fac * st_ivas->hSpar->hMdDec->mixer_mat[i][j][band + mixer_mat_index * IVAS_MAX_NUM_BANDS];
    1660             :                     }
    1661             :                 }
    1662             :             }
    1663             :         }
    1664             :     }
    1665             : 
    1666             :     /* Map the mixing matrices from the frequency bands to frequency bins */
    1667     1912616 :     bin = 0;
    1668    17213544 :     for ( band = 0; band < SPAR_DIRAC_SPLIT_START_BAND; band++ )
    1669             :     {
    1670    15300928 :         band_start = band_grouping[band];
    1671    15300928 :         band_end = band_grouping[band + 1];
    1672    36339704 :         for ( bin = band_start; bin < band_end; bin++ )
    1673             :         {
    1674   105193880 :             for ( i = 0; i < FOA_CHANNELS; i++ )
    1675             :             {
    1676   420775520 :                 for ( j = 0; j < FOA_CHANNELS; j++ )
    1677             :                 {
    1678   336620416 :                     mixer_mat_sf_bins_real[bin][i][j] = mixer_mat_sf_bands_real[band][i][j];
    1679             :                 }
    1680             :             }
    1681             :         }
    1682             :     }
    1683     1912616 :     nBins = bin;
    1684             : 
    1685             :     /* Determine MASA metadata */
    1686             :     /* Determine transport signal energies and cross correlations when more than 1 TC */
    1687     1912616 :     if ( nchan_transport == 2 )
    1688             :     {
    1689      938810 :         set_zero( transportSignalEnergies[0], nBins );
    1690      938810 :         set_zero( transportSignalEnergies[1], nBins );
    1691      938810 :         set_zero( transportSignalCrossCorrelation, nBins );
    1692             : 
    1693     4688804 :         for ( slot = 0; slot < hSpatParamRendCom->subframe_nbslots[subframe]; slot++ )
    1694             :         {
    1695    44999928 :             for ( bin = 0; bin < nBins; bin++ )
    1696             :             {
    1697   123749802 :                 for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    1698             :                 {
    1699    82499868 :                     instEne = ( inRe[ch][slot][bin] * inRe[ch][slot][bin] );
    1700    82499868 :                     instEne += ( inIm[ch][slot][bin] * inIm[ch][slot][bin] );
    1701    82499868 :                     transportSignalEnergies[ch][bin] += instEne;
    1702             :                 }
    1703    41249934 :                 transportSignalCrossCorrelation[bin] += inRe[0][slot][bin] * inRe[1][slot][bin];
    1704    41249934 :                 transportSignalCrossCorrelation[bin] += inIm[0][slot][bin] * inIm[1][slot][bin];
    1705             :             }
    1706             :         }
    1707             :     }
    1708             : 
    1709     1912616 :     if ( hDiffuseDist != NULL )
    1710             :     {
    1711     1912616 :         set_zero( hDiffuseDist->diffuseRatioX, CLDFB_NO_CHANNELS_MAX );
    1712     1912616 :         set_zero( hDiffuseDist->diffuseRatioY, CLDFB_NO_CHANNELS_MAX );
    1713     1912616 :         set_zero( hDiffuseDist->diffuseRatioZ, CLDFB_NO_CHANNELS_MAX );
    1714             :     }
    1715             : 
    1716    22951392 :     for ( bin = 0; bin < nBins; bin++ )
    1717             :     {
    1718             :         /* Set the energy of the first transport signal */
    1719    21038776 :         if ( nchan_transport == 1 )
    1720             :         {
    1721    10711866 :             inCovarianceMtx[0][0] = 1.0f; /* In case of 1TC, fixed value can be used */
    1722             :         }
    1723             :         else
    1724             :         {
    1725    10326910 :             inCovarianceMtx[0][0] = transportSignalEnergies[0][bin]; /* In case of 2TC, use actual energies */
    1726             :         }
    1727             : 
    1728             :         /* Decorrelated channels assumed to have the same energy as the source channel */
    1729    21038776 :         inCovarianceMtx[1][1] = inCovarianceMtx[0][0];
    1730    21038776 :         inCovarianceMtx[2][2] = inCovarianceMtx[0][0];
    1731    21038776 :         inCovarianceMtx[3][3] = inCovarianceMtx[0][0];
    1732             : 
    1733             :         /* In case residuals were transmitted, use their actual energies and cross correlations */
    1734    21038776 :         if ( nchan_transport == 2 )
    1735             :         {
    1736    10326910 :             inCovarianceMtx[1][1] = transportSignalEnergies[1][bin];
    1737    10326910 :             inCovarianceMtx[0][1] = transportSignalCrossCorrelation[bin];
    1738    10326910 :             inCovarianceMtx[1][0] = inCovarianceMtx[0][1];
    1739             :         }
    1740             : 
    1741    21038776 :         compute_foa_cov_matrix( foaCovarianceMtx, inCovarianceMtx, mixer_mat_sf_bins_real[bin] );
    1742             : 
    1743             :         /* Estimate MASA metadata */
    1744    21038776 :         Iy = foaCovarianceMtx[0][1];                                                                                      /* Intensity in Y direction */
    1745    21038776 :         Iz = foaCovarianceMtx[0][2];                                                                                      /* Intensity in Z direction */
    1746    21038776 :         Ix = foaCovarianceMtx[0][3];                                                                                      /* Intensity in X direction */
    1747    21038776 :         I = sqrtf( Ix * Ix + Iy * Iy + Iz * Iz );                                                                         /* Intensity vector length */
    1748    21038776 :         E = ( foaCovarianceMtx[0][0] + foaCovarianceMtx[1][1] + foaCovarianceMtx[2][2] + foaCovarianceMtx[3][3] ) / 2.0f; /* Overall energy */
    1749    21038776 :         azi = atan2f( Iy, Ix );                                                                                           /* Azimuth */
    1750    21038776 :         ele = atan2f( Iz, sqrtf( Ix * Ix + Iy * Iy ) );                                                                   /* Elevation */
    1751    21038776 :         ratio = I / fmaxf( 1e-12f, E );                                                                                   /* Energy ratio */
    1752    21038776 :         ratio = fmaxf( 0.0f, fminf( 1.0f, ratio ) );
    1753             : 
    1754    21038776 :         hSpatParamRendCom->azimuth[dirac_write_idx][bin] = (int16_t) roundf( azi / PI_OVER_180 );
    1755    21038776 :         hSpatParamRendCom->elevation[dirac_write_idx][bin] = (int16_t) roundf( ele / PI_OVER_180 );
    1756    21038776 :         hSpatParamRendCom->energy_ratio1[dirac_write_idx][bin] = ratio;
    1757    21038776 :         hSpatParamRendCom->diffuseness_vector[dirac_write_idx][bin] = 1.0f - ratio;
    1758             : 
    1759    21038776 :         hSpatParamRendCom->spreadCoherence[dirac_write_idx][bin] = 0.0f;
    1760    21038776 :         hSpatParamRendCom->surroundingCoherence[dirac_write_idx][bin] = 0.0f;
    1761             : 
    1762             :         /* Determine directional distribution of the indirect audio based on the SPAR mixing matrices (and the transport audio signals when 2 TC) */
    1763    21038776 :         if ( hDiffuseDist != NULL )
    1764             :         {
    1765    21038776 :             if ( nchan_transport == 1 )
    1766             :             {
    1767    10711866 :                 diffuseGainY = fabsf( mixer_mat_sf_bins_real[bin][1][1] );
    1768    10711866 :                 diffuseGainX = fabsf( mixer_mat_sf_bins_real[bin][3][2] );
    1769    10711866 :                 diffuseGainZ = fabsf( mixer_mat_sf_bins_real[bin][2][3] );
    1770             :             }
    1771    10326910 :             else if ( nchan_transport == 2 )
    1772             :             {
    1773    10326910 :                 diffuseGainY = fabsf( mixer_mat_sf_bins_real[bin][1][1] * transportSignalEnergies[1][bin] );
    1774    10326910 :                 diffuseGainX = fabsf( mixer_mat_sf_bins_real[bin][3][2] * transportSignalEnergies[0][bin] ) + fabsf( mixer_mat_sf_bins_real[bin][3][1] * transportSignalEnergies[1][bin] );
    1775    10326910 :                 diffuseGainZ = fabsf( mixer_mat_sf_bins_real[bin][2][3] * transportSignalEnergies[0][bin] ) + fabsf( mixer_mat_sf_bins_real[bin][2][1] * transportSignalEnergies[1][bin] );
    1776             :             }
    1777             :             else
    1778             :             {
    1779           0 :                 diffuseGainY = 1.0f;
    1780           0 :                 diffuseGainX = 1.0f;
    1781           0 :                 diffuseGainZ = 1.0f;
    1782             :             }
    1783             : 
    1784    21038776 :             diffuseGainSum = diffuseGainY + diffuseGainX + diffuseGainZ;
    1785             : 
    1786    21038776 :             if ( diffuseGainSum == 0.0f )
    1787             :             {
    1788      300469 :                 hDiffuseDist->diffuseRatioX[bin] = 1.0f / 3.0f;
    1789      300469 :                 hDiffuseDist->diffuseRatioY[bin] = 1.0f / 3.0f;
    1790      300469 :                 hDiffuseDist->diffuseRatioZ[bin] = 1.0f / 3.0f;
    1791             :             }
    1792             :             else
    1793             :             {
    1794    20738307 :                 hDiffuseDist->diffuseRatioX[bin] = diffuseGainX / ( diffuseGainSum + EPSILON );
    1795    20738307 :                 hDiffuseDist->diffuseRatioY[bin] = diffuseGainY / ( diffuseGainSum + EPSILON );
    1796    20738307 :                 hDiffuseDist->diffuseRatioZ[bin] = diffuseGainZ / ( diffuseGainSum + EPSILON );
    1797             :             }
    1798             :         }
    1799             :     }
    1800             : 
    1801     1912616 :     return;
    1802             : }
    1803             : 
    1804             : 
    1805             : /* Estimate FOA properties: foaCov = mixMtx * inCov * mixMtx' */
    1806    21038776 : static void compute_foa_cov_matrix(
    1807             :     float foaCov[FOA_CHANNELS][FOA_CHANNELS], /* o  : Estimated FOA covariance matrix */
    1808             :     float inCov[FOA_CHANNELS][FOA_CHANNELS],  /* i  : Input covariance matrix         */
    1809             :     float mixMtx[FOA_CHANNELS][FOA_CHANNELS]  /* i  : Mixing matrix                   */
    1810             : )
    1811             : {
    1812             :     float tmpMtx[FOA_CHANNELS][FOA_CHANNELS];
    1813             :     int16_t i, j, k;
    1814             : 
    1815             :     /* tmpMtx = mixMtx * inCov */
    1816   105193880 :     for ( i = 0; i < FOA_CHANNELS; i++ )
    1817             :     {
    1818   420775520 :         for ( j = 0; j < FOA_CHANNELS; j++ )
    1819             :         {
    1820   336620416 :             tmpMtx[i][j] = 0.0f;
    1821  1683102080 :             for ( k = 0; k < FOA_CHANNELS; k++ )
    1822             :             {
    1823  1346481664 :                 tmpMtx[i][j] += mixMtx[i][k] * inCov[k][j];
    1824             :             }
    1825             :         }
    1826             :     }
    1827             : 
    1828             :     /* foaCov = inCov * mixMtx' */
    1829   105193880 :     for ( i = 0; i < FOA_CHANNELS; i++ )
    1830             :     {
    1831   420775520 :         for ( j = 0; j < FOA_CHANNELS; j++ )
    1832             :         {
    1833   336620416 :             foaCov[i][j] = 0.0f;
    1834  1683102080 :             for ( k = 0; k < FOA_CHANNELS; k++ )
    1835             :             {
    1836  1346481664 :                 foaCov[i][j] += tmpMtx[i][k] * mixMtx[j][k];
    1837             :             }
    1838             :         }
    1839             :     }
    1840             : 
    1841    21038776 :     return;
    1842             : }
    1843             : 
    1844       66453 : static void create_masa_ext_out_meta(
    1845             :     MASA_DECODER *hMasa,
    1846             :     IVAS_QMETADATA_HANDLE hQMetaData,
    1847             :     const int16_t nchan_transport )
    1848             : {
    1849             :     int16_t i, sf, b_old, b_new, dir;
    1850             :     MASA_DECRIPTIVE_META *descMeta;
    1851             :     int16_t *bandMap;
    1852             :     uint8_t numCodingBands;
    1853             :     uint8_t numDirections;
    1854             :     MASA_DECODER_EXT_OUT_META *extOutMeta;
    1855             : 
    1856       66453 :     numDirections = hMasa->config.numberOfDirections;
    1857       66453 :     numCodingBands = hMasa->config.numCodingBands;
    1858       66453 :     bandMap = hMasa->data.band_mapping;
    1859       66453 :     extOutMeta = hMasa->data.extOutMeta;
    1860       66453 :     descMeta = &hMasa->data.extOutMeta->descriptiveMeta;
    1861             : 
    1862             :     /* Construct descriptive meta */
    1863      598077 :     for ( i = 0; i < 8; i++ )
    1864             :     {
    1865      531624 :         descMeta->formatDescriptor[i] = ivasmasaFormatDescriptor[i];
    1866             :     }
    1867       66453 :     descMeta->numberOfDirections = numDirections - 1;
    1868       66453 :     descMeta->numberOfChannels = (uint8_t) ( nchan_transport - 1 );
    1869             :     /* Following correspond to "unknown" values until transmission is implemented */
    1870       66453 :     descMeta->sourceFormat = 0x0u;
    1871       66453 :     descMeta->transportDefinition = 0x0u;
    1872       66453 :     descMeta->channelAngle = 0x0u;
    1873       66453 :     descMeta->channelDistance = 0x0u;
    1874       66453 :     descMeta->channelLayout = 0x0u;
    1875             : 
    1876             :     /* Construct spatial metadata from qmetadata */
    1877      332265 :     for ( sf = 0; sf < MAX_PARAM_SPATIAL_SUBFRAMES; sf++ )
    1878             :     {
    1879      557892 :         for ( dir = 0; dir < numDirections; dir++ )
    1880             :         {
    1881             :             /* Spherical index */
    1882     2956096 :             for ( b_old = 0; b_old < numCodingBands; b_old++ )
    1883             :             {
    1884     9329608 :                 for ( b_new = bandMap[b_old]; b_new < bandMap[b_old + 1]; b_new++ )
    1885             :                 {
    1886     6665592 :                     extOutMeta->directionIndex[dir][sf][b_new] = hQMetaData->q_direction[dir].band_data[b_old].spherical_index[sf];
    1887             :                 }
    1888             :             }
    1889             : 
    1890             :             /* Direct-to-total ratio */
    1891     2956096 :             for ( b_old = 0; b_old < numCodingBands; b_old++ )
    1892             :             {
    1893     9329608 :                 for ( b_new = bandMap[b_old]; b_new < bandMap[b_old + 1]; b_new++ )
    1894             :                 {
    1895     6665592 :                     extOutMeta->directToTotalRatio[dir][sf][b_new] = (uint8_t) floorf( hQMetaData->q_direction[dir].band_data[b_old].energy_ratio[sf] * UINT8_MAX );
    1896             :                 }
    1897             :             }
    1898             : 
    1899             :             /* Spread coherence */
    1900      292080 :             if ( hQMetaData->q_direction[dir].coherence_band_data != NULL )
    1901             :             {
    1902     2360944 :                 for ( b_old = 0; b_old < numCodingBands; b_old++ )
    1903             :                 {
    1904     6599048 :                     for ( b_new = bandMap[b_old]; b_new < bandMap[b_old + 1]; b_new++ )
    1905             :                     {
    1906     4430992 :                         extOutMeta->spreadCoherence[dir][sf][b_new] = hQMetaData->q_direction[dir].coherence_band_data[b_old].spread_coherence[sf];
    1907             :                     }
    1908             :                 }
    1909             :             }
    1910             :             else
    1911             :             {
    1912     2479800 :                 for ( i = 0; i < MASA_FREQUENCY_BANDS; i++ )
    1913             :                 {
    1914     2380608 :                     extOutMeta->spreadCoherence[dir][sf][i] = 0;
    1915             :                 }
    1916             :             }
    1917             :         }
    1918             : 
    1919             :         /* Fill second direction with zero energy data for EXT output */
    1920      265812 :         if ( numDirections == 1 )
    1921             :         {
    1922     5988600 :             for ( i = 0; i < MASA_FREQUENCY_BANDS; i++ )
    1923             :             {
    1924     5749056 :                 extOutMeta->directionIndex[1][sf][i] = SPH_IDX_FRONT;
    1925             :             }
    1926             : 
    1927     5988600 :             for ( i = 0; i < MASA_FREQUENCY_BANDS; i++ )
    1928             :             {
    1929     5749056 :                 extOutMeta->directToTotalRatio[1][sf][i] = 0;
    1930             :             }
    1931             : 
    1932     5988600 :             for ( i = 0; i < MASA_FREQUENCY_BANDS; i++ )
    1933             :             {
    1934     5749056 :                 extOutMeta->spreadCoherence[1][sf][i] = 0;
    1935             :             }
    1936             :         }
    1937             : 
    1938             :         /* Common spatial meta */
    1939             :         /* Diffuse-to-total ratio = 1 - sum(direct-to-total ratios) */
    1940     2617924 :         for ( b_old = 0; b_old < numCodingBands; b_old++ )
    1941             :         {
    1942     8387272 :             for ( b_new = bandMap[b_old]; b_new < bandMap[b_old + 1]; b_new++ )
    1943             :             {
    1944     6035160 :                 extOutMeta->diffuseToTotalRatio[sf][b_new] = UINT8_MAX;
    1945    12700752 :                 for ( dir = 0; dir < numDirections; dir++ )
    1946             :                 {
    1947     6665592 :                     extOutMeta->diffuseToTotalRatio[sf][b_new] -= (uint8_t) floorf( hQMetaData->q_direction[dir].band_data[b_old].energy_ratio[sf] * UINT8_MAX );
    1948             :                 }
    1949             :             }
    1950             :         }
    1951             : 
    1952             :         /* Surround coherence */
    1953      265812 :         if ( hQMetaData->surcoh_band_data != NULL )
    1954             :         {
    1955     2022772 :             for ( b_old = 0; b_old < numCodingBands; b_old++ )
    1956             :             {
    1957     5656712 :                 for ( b_new = bandMap[b_old]; b_new < bandMap[b_old + 1]; b_new++ )
    1958             :                 {
    1959     3800560 :                     extOutMeta->surroundCoherence[sf][b_new] = hQMetaData->surcoh_band_data[b_old].surround_coherence[sf];
    1960             :                 }
    1961             :             }
    1962             :         }
    1963             :         else
    1964             :         {
    1965     2479800 :             for ( i = 0; i < MASA_FREQUENCY_BANDS; i++ )
    1966             :             {
    1967     2380608 :                 extOutMeta->surroundCoherence[sf][i] = 0;
    1968             :             }
    1969             :         }
    1970             :     }
    1971             : 
    1972       66453 :     return;
    1973             : }
    1974             : 
    1975      227205 : static void decode_index_slice(
    1976             :     int16_t index,           /* i  : index to decode                        */
    1977             :     int16_t *ratio_idx_ism,  /* o  : decodec array of integers              */
    1978             :     const int16_t nchan_ism, /* i  : number of elements in array (objects)  */
    1979             :     const int16_t K          /* i  : sum of array elements                  */
    1980             : )
    1981             : {
    1982             :     int16_t i, j, sum, elem;
    1983             :     int16_t base[MAX_NUM_OBJECTS];
    1984             : 
    1985      227205 :     switch ( nchan_ism )
    1986             :     {
    1987        9288 :         case 2:
    1988        9288 :             ratio_idx_ism[0] = index;
    1989        9288 :             ratio_idx_ism[1] = K - ratio_idx_ism[0];
    1990        9288 :             break;
    1991      217917 :         case 3:
    1992             :         case 4:
    1993             :         {
    1994      217917 :             j = 0;
    1995    13141299 :             while ( index >= 0 )
    1996             :             {
    1997    12923382 :                 if ( valid_ratio_index( j, K, nchan_ism - 1 ) )
    1998             :                 {
    1999     5253087 :                     index--;
    2000             :                 }
    2001    12923382 :                 j++;
    2002             :             }
    2003      217917 :             j--;
    2004      217917 :             base[0] = 1;
    2005      591599 :             for ( i = 1; i < nchan_ism - 1; i++ )
    2006             :             {
    2007      373682 :                 base[i] = base[i - 1] * 10;
    2008             :             }
    2009      217917 :             sum = 0;
    2010      809516 :             for ( i = nchan_ism - 2; i >= 0; i-- )
    2011             :             {
    2012      591599 :                 elem = j / base[i];
    2013      591599 :                 ratio_idx_ism[nchan_ism - i - 2] = elem;
    2014      591599 :                 sum += elem;
    2015      591599 :                 j -= elem * base[i];
    2016             :             }
    2017      217917 :             ratio_idx_ism[nchan_ism - 1] = K - sum;
    2018             :         }
    2019             : 
    2020      217917 :         default:
    2021      217917 :             break;
    2022             :     }
    2023             : 
    2024      227205 :     return;
    2025             : }
    2026             : 
    2027             : 
    2028      224554 : static void read_ism_ratio_index(
    2029             :     int16_t ratio_ism_idx[MASA_FREQUENCY_BANDS][MAX_NUM_OBJECTS],         /* o  : ISM read ratio indexes                                        */
    2030             :     const int16_t nchan_ism,                                              /* i  : number of objects                                             */
    2031             :     const int16_t numCodingBands,                                         /* i  : number of subbands                                            */
    2032             :     const int16_t sf,                                                     /* i  : index of subframe                                             */
    2033             :     int16_t ratio_ism_idx_prev_sf[MASA_FREQUENCY_BANDS][MAX_NUM_OBJECTS], /* i  : previous subframe ISM ratio indexes                           */
    2034             :     uint16_t *bit_stream,                                                 /* i  : bitstream                                                     */
    2035             :     int16_t *next_bit_pos,                                                /* i/o: position in bitstream                                         */
    2036             :     float *masa_to_total_energy_ratio,                                    /* i  : masa to total ratios                                          */
    2037             :     const int16_t idx_sep_obj,                                            /* i  : index of separated index, -1 if none                          */
    2038             :     int16_t *num_zeros                                                    /* i/o: number of zero values in first subframe for separated object  */
    2039             : )
    2040             : {
    2041             :     int16_t b, i, b_signif;
    2042             :     int16_t index;
    2043             :     int16_t GR_order, differential_subframe;
    2044             :     int16_t buf;
    2045             :     int16_t no_levels_ratio_ism;
    2046             :     int16_t bits_index;
    2047             :     int16_t ratio_ism_idx_ref[MAX_NUM_OBJECTS];
    2048             :     int16_t idx_sep_obj_local, shift_one;
    2049             : 
    2050      224554 :     idx_sep_obj_local = idx_sep_obj;
    2051      224554 :     if ( idx_sep_obj > -1 )
    2052             :     {
    2053      224554 :         if ( idx_sep_obj == nchan_ism - 1 && nchan_ism > 2 )
    2054             :         {
    2055        5226 :             idx_sep_obj_local = 0;
    2056             :         }
    2057             :     }
    2058             : 
    2059      224554 :     b_signif = 0;
    2060      224554 :     no_levels_ratio_ism = ( ( 1 << PARAM_ISM_POW_RATIO_NBITS ) - 1 );
    2061      236692 :     while ( ( b_signif < numCodingBands ) && ( masa_to_total_energy_ratio[b_signif] >= MASA2TOTAL_THR ) )
    2062             :     {
    2063             :         /* distribute evenly the objects */
    2064       12138 :         distribute_evenly_ism( ratio_ism_idx[b_signif], no_levels_ratio_ism, nchan_ism );
    2065       12138 :         b_signif++;
    2066             :     }
    2067             : 
    2068      224554 :     if ( b_signif == numCodingBands )
    2069             :     {
    2070        1988 :         return;
    2071             :     }
    2072             :     else
    2073             :     {
    2074             : 
    2075      222566 :         if ( sf == 0 )
    2076             :         {
    2077       62461 :             bits_index = bits_index_ism_ratio( nchan_ism );
    2078             : 
    2079             :             /* read coding type */
    2080       62461 :             if ( bit_stream[( *next_bit_pos )--] == 1 )
    2081             :             {
    2082             :                 /* independent coding*/
    2083      260301 :                 for ( b = 0; b < numCodingBands; b++ )
    2084             :                 {
    2085      216835 :                     if ( masa_to_total_energy_ratio[b] < MASA2TOTAL_THR )
    2086             :                     {
    2087      208210 :                         index = 0;
    2088     1605828 :                         for ( i = 0; i < bits_index; i++ )
    2089             :                         {
    2090     1397618 :                             index = ( index << 1 ) + bit_stream[( *next_bit_pos )--];
    2091             :                         }
    2092      208210 :                         decode_index_slice( index, ratio_ism_idx[b], nchan_ism, no_levels_ratio_ism );
    2093      208210 :                         if ( idx_sep_obj > -1 && ratio_ism_idx[b][idx_sep_obj_local] == 0 )
    2094             :                         {
    2095      202426 :                             ( *num_zeros )++;
    2096             :                         }
    2097             :                     }
    2098             :                     else
    2099             :                     {
    2100             :                         /* distribute evenly the objects */
    2101        8625 :                         distribute_evenly_ism( ratio_ism_idx[b], no_levels_ratio_ism, nchan_ism );
    2102             :                     }
    2103             :                 }
    2104             :             }
    2105             :             else
    2106             :             {
    2107             :                 /* differential coding */
    2108       18995 :                 index = 0;
    2109      112508 :                 for ( i = 0; i < bits_index; i++ )
    2110             :                 {
    2111       93513 :                     index = ( index << 1 ) + bit_stream[( *next_bit_pos )--];
    2112             :                 }
    2113       18995 :                 decode_index_slice( index, ratio_ism_idx[b_signif], nchan_ism, no_levels_ratio_ism );
    2114       18995 :                 if ( idx_sep_obj > -1 && ratio_ism_idx[b_signif][idx_sep_obj_local] == 0 )
    2115             :                 {
    2116       18640 :                     ( *num_zeros )++;
    2117             :                 }
    2118       18995 :                 mvs2s( ratio_ism_idx[b_signif], ratio_ism_idx_ref, nchan_ism );
    2119       98937 :                 for ( b = b_signif + 1; b < numCodingBands; b++ )
    2120             :                 {
    2121       79942 :                     if ( masa_to_total_energy_ratio[b] < MASA2TOTAL_THR )
    2122             :                     {
    2123       71238 :                         ratio_ism_idx[b][nchan_ism - 1] = no_levels_ratio_ism;
    2124      200629 :                         for ( i = 0; i < nchan_ism - 1; i++ )
    2125             :                         {
    2126      129391 :                             buf = ivas_qmetadata_DecodeExtendedGR( bit_stream, next_bit_pos, 100, 0 );
    2127      129391 :                             if ( ( buf % 2 ) == 0 )
    2128             :                             {
    2129      114763 :                                 ratio_ism_idx[b][i] = -( buf >> 1 );
    2130             :                             }
    2131             :                             else
    2132             :                             {
    2133       14628 :                                 ratio_ism_idx[b][i] = ( ( buf + 1 ) >> 1 );
    2134             :                             }
    2135      129391 :                             ratio_ism_idx[b][i] = ratio_ism_idx[b][i] + ratio_ism_idx_ref[i];
    2136      129391 :                             ratio_ism_idx[b][nchan_ism - 1] -= ratio_ism_idx[b][i];
    2137             :                         }
    2138       71238 :                         mvs2s( ratio_ism_idx[b], ratio_ism_idx_ref, nchan_ism );
    2139       71238 :                         if ( idx_sep_obj > -1 && ratio_ism_idx[b][idx_sep_obj_local] == 0 )
    2140             :                         {
    2141       69396 :                             ( *num_zeros )++;
    2142             :                         }
    2143             :                     }
    2144             :                     else
    2145             :                     {
    2146             :                         /* distribute evenly the objects */
    2147        8704 :                         distribute_evenly_ism( ratio_ism_idx[b], no_levels_ratio_ism, nchan_ism );
    2148             :                     }
    2149             :                 }
    2150             :             }
    2151             :         }
    2152             :         else
    2153             :         {
    2154      160105 :             if ( numCodingBands > 1 )
    2155             :             {
    2156             :                 /* read prediction type */
    2157      157365 :                 differential_subframe = bit_stream[( *next_bit_pos )--];
    2158             :             }
    2159             :             else
    2160             :             {
    2161        2740 :                 differential_subframe = 1;
    2162             :             }
    2163             : 
    2164      160105 :             if ( *num_zeros == numCodingBands )
    2165             :             {
    2166      130071 :                 shift_one = 1;
    2167             :             }
    2168             :             else
    2169             :             {
    2170       30034 :                 shift_one = 0;
    2171             :             }
    2172             : 
    2173      160105 :             if ( shift_one == 1 && nchan_ism == 2 )
    2174             :             {
    2175             :                 /* nothing has been sent ; values can be inferred */
    2176        5370 :                 for ( b = b_signif; b < numCodingBands; b++ )
    2177             :                 {
    2178        2685 :                     if ( masa_to_total_energy_ratio[b] < MASA2TOTAL_THR )
    2179             :                     {
    2180        2685 :                         if ( idx_sep_obj_local == 0 )
    2181             :                         {
    2182        2655 :                             ratio_ism_idx[b][0] = 0;
    2183        2655 :                             ratio_ism_idx[b][1] = 7;
    2184             :                         }
    2185             :                         else
    2186             :                         {
    2187          30 :                             ratio_ism_idx[b][0] = 7;
    2188          30 :                             ratio_ism_idx[b][1] = 0;
    2189             :                         }
    2190             :                     }
    2191             :                 }
    2192             :             }
    2193             :             else
    2194             :             {
    2195             :                 /* read GR order */
    2196      157420 :                 GR_order = bit_stream[( *next_bit_pos )--];
    2197      942211 :                 for ( b = b_signif; b < numCodingBands; b++ )
    2198             :                 {
    2199      784791 :                     if ( masa_to_total_energy_ratio[b] < MASA2TOTAL_THR )
    2200             :                     {
    2201     2109902 :                         for ( i = 0; i < nchan_ism - 1 - shift_one; i++ )
    2202             :                         {
    2203     1359889 :                             buf = ivas_qmetadata_DecodeExtendedGR( bit_stream, next_bit_pos, 100, GR_order );
    2204     1359889 :                             if ( ( buf % 2 ) == 0 )
    2205             :                             {
    2206     1048454 :                                 ratio_ism_idx[b][i] = -( buf >> 1 );
    2207             :                             }
    2208             :                             else
    2209             :                             {
    2210      311435 :                                 ratio_ism_idx[b][i] = ( ( buf + 1 ) >> 1 );
    2211             :                             }
    2212             :                         }
    2213             : 
    2214             :                         /* insert separated obj */
    2215      750013 :                         if ( shift_one )
    2216             :                         {
    2217     2185089 :                             for ( i = nchan_ism - 1; i > idx_sep_obj_local; i-- )
    2218             :                             {
    2219     1562297 :                                 ratio_ism_idx[b][i] = ratio_ism_idx[b][i - 1];
    2220             :                             }
    2221      622792 :                             ratio_ism_idx[b][idx_sep_obj_local] = 0; /* this is only difference; need to pdate later as well  */
    2222             :                         }
    2223             :                     }
    2224             :                 }
    2225      157420 :                 if ( differential_subframe )
    2226             :                 {
    2227             :                     /* differential to previous subframe */
    2228      885220 :                     for ( b = b_signif; b < numCodingBands; b++ )
    2229             :                     {
    2230      737359 :                         if ( masa_to_total_energy_ratio[b] < MASA2TOTAL_THR )
    2231             :                         {
    2232      706359 :                             ratio_ism_idx[b][nchan_ism - 1] = no_levels_ratio_ism;
    2233     2576865 :                             for ( i = 0; i < nchan_ism - 1; i++ )
    2234             :                             {
    2235     1870506 :                                 ratio_ism_idx[b][i] = ratio_ism_idx[b][i] + ratio_ism_idx_prev_sf[b][i];
    2236     1870506 :                                 if ( shift_one && i == idx_sep_obj_local )
    2237             :                                 {
    2238      599175 :                                     ratio_ism_idx[b][i] = 0;
    2239             :                                 }
    2240     1870506 :                                 ratio_ism_idx[b][nchan_ism - 1] -= ratio_ism_idx[b][i];
    2241             :                             }
    2242             :                         }
    2243             :                         else
    2244             :                         {
    2245             :                             /* distribute evenly the objects */
    2246       31000 :                             distribute_evenly_ism( ratio_ism_idx[b], no_levels_ratio_ism, nchan_ism );
    2247             :                         }
    2248             :                     }
    2249             :                 }
    2250             :                 else
    2251             :                 {
    2252             :                     /* difference to previous subband */
    2253        9559 :                     ratio_ism_idx[b_signif][nchan_ism - 1] = no_levels_ratio_ism;
    2254             : 
    2255             :                     /* first significant subband - differential to previous subframe */
    2256       34161 :                     for ( i = 0; i < nchan_ism - 1; i++ )
    2257             :                     {
    2258       24602 :                         ratio_ism_idx[b_signif][i] = ratio_ism_idx[b_signif][i] + ratio_ism_idx_prev_sf[b_signif][i];
    2259       24602 :                         if ( shift_one && i == idx_sep_obj_local )
    2260             :                         {
    2261        4905 :                             ratio_ism_idx[b_signif][i] = 0;
    2262             :                         }
    2263       24602 :                         ratio_ism_idx[b_signif][nchan_ism - 1] -= ratio_ism_idx[b_signif][i];
    2264             :                     }
    2265             : 
    2266             :                     /* rest of subbands differential to previous subband */
    2267        9559 :                     mvs2s( ratio_ism_idx[b_signif], ratio_ism_idx_ref, nchan_ism );
    2268       47432 :                     for ( b = b_signif + 1; b < numCodingBands; b++ )
    2269             :                     {
    2270       37873 :                         if ( masa_to_total_energy_ratio[b] < MASA2TOTAL_THR )
    2271             :                         {
    2272       34095 :                             ratio_ism_idx[b][nchan_ism - 1] = no_levels_ratio_ism;
    2273      121668 :                             for ( i = 0; i < nchan_ism - 1; i++ )
    2274             :                             {
    2275       87573 :                                 ratio_ism_idx[b][i] = ratio_ism_idx[b][i] + ratio_ism_idx_ref[i];
    2276       87573 :                                 if ( shift_one && i == idx_sep_obj_local )
    2277             :                                 {
    2278       18712 :                                     ratio_ism_idx[b][i] = 0;
    2279             :                                 }
    2280       87573 :                                 ratio_ism_idx[b][nchan_ism - 1] -= ratio_ism_idx[b][i];
    2281             :                             }
    2282       34095 :                             mvs2s( ratio_ism_idx[b], ratio_ism_idx_ref, nchan_ism );
    2283             :                         }
    2284             :                         else
    2285             :                         {
    2286             :                             /* distribute evenly the objects */
    2287        3778 :                             distribute_evenly_ism( ratio_ism_idx[b], no_levels_ratio_ism, nchan_ism );
    2288             :                         }
    2289             :                     }
    2290             :                 }
    2291             :             }
    2292             :         }
    2293             : 
    2294      222566 :         return;
    2295             :     }
    2296             : }
    2297             : 
    2298             : 
    2299       63232 : static void decode_ism_ratios(
    2300             :     uint16_t *bit_stream,                                                                /* i  : bitstream                      */
    2301             :     int16_t *next_bit_pos,                                                               /* i/o: position in bitstream          */
    2302             :     float masa_to_total_energy_ratio[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS], /* i  : masa_to_total energy ratios    */
    2303             :     float ratio_ism[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS][MAX_NUM_OBJECTS], /* o  : ISM ratios                     */
    2304             :     const int16_t n_ism,                                                                 /* i  : number of objects              */
    2305             :     const int16_t nbands,                                                                /* i  : number of subbands             */
    2306             :     const int16_t numSf,                                                                 /* i  : number of subframes            */
    2307             :     const int16_t idx_separated_object                                                   /* i  : index of separated object      */
    2308             : )
    2309             : {
    2310             :     int16_t sf, band;
    2311             :     int16_t ratio_ism_idx[MASA_FREQUENCY_BANDS][MAX_NUM_OBJECTS];
    2312             :     int16_t ratio_ism_idx_prev_sf[MASA_FREQUENCY_BANDS][MAX_NUM_OBJECTS];
    2313             :     float tmp;
    2314             :     int16_t num_zeros;
    2315       63232 :     num_zeros = 0;
    2316             : 
    2317             :     /* hQMetaData->q_direction->cfg.nblocks; */
    2318      287786 :     for ( sf = 0; sf < numSf; sf++ )
    2319             :     {
    2320             :         /* read ism ratio indexes */
    2321      224554 :         read_ism_ratio_index( ratio_ism_idx, n_ism, nbands, sf, ratio_ism_idx_prev_sf, bit_stream, next_bit_pos, masa_to_total_energy_ratio[sf], idx_separated_object, &num_zeros );
    2322             :         /* save previous subframe index values */
    2323      224554 :         if ( sf < numSf - 1 )
    2324             :         {
    2325      956580 :             for ( band = 0; band < nbands; band++ )
    2326             :             {
    2327      795258 :                 mvs2s( ratio_ism_idx[band], ratio_ism_idx_prev_sf[band], n_ism );
    2328             :             }
    2329             :         }
    2330             : 
    2331             :         /* reconstructed values */
    2332     1339748 :         for ( band = 0; band < nbands; band++ )
    2333             :         {
    2334     1115194 :             reconstruct_ism_ratios( ratio_ism_idx[band], n_ism, 1.0f / (float) ( ( 1 << PARAM_ISM_POW_RATIO_NBITS ) - 1 ), ratio_ism[sf][band] );
    2335             :         }
    2336             : 
    2337      224554 :         if ( ( n_ism > 2 ) && ( idx_separated_object == n_ism - 1 ) )
    2338             :         {
    2339             :             /* rotate */
    2340       31398 :             for ( band = 0; band < nbands; band++ )
    2341             :             {
    2342       26172 :                 if ( masa_to_total_energy_ratio[sf][band] < MASA2TOTAL_THR )
    2343             :                 {
    2344       23473 :                     tmp = ratio_ism[sf][band][n_ism - 1];
    2345       23473 :                     ratio_ism[sf][band][n_ism - 1] = ratio_ism[sf][band][0];
    2346       23473 :                     ratio_ism[sf][band][0] = tmp;
    2347             :                 }
    2348             :             }
    2349             :         }
    2350             : 
    2351      224554 :         if ( nbands == 1 )
    2352             :         {
    2353       18920 :             for ( band = 1; band < 5; band++ )
    2354             :             {
    2355       15136 :                 mvr2r( ratio_ism[sf][0], ratio_ism[sf][band], n_ism );
    2356             :             }
    2357             :         }
    2358             :     }
    2359             : 
    2360       63232 :     if ( numSf == 1 )
    2361             :     {
    2362       37832 :         for ( sf = 1; sf < MAX_PARAM_SPATIAL_SUBFRAMES; sf++ )
    2363             :         {
    2364      192924 :             for ( band = 0; band < nbands; band++ )
    2365             :             {
    2366      164550 :                 mvr2r( ratio_ism[0][band], ratio_ism[sf][band], n_ism );
    2367             :             }
    2368             :         }
    2369             :     }
    2370             : 
    2371       63232 :     return;
    2372             : }
    2373             : 
    2374             : 
    2375       63232 : static int16_t ivas_decode_masaism_metadata(
    2376             :     IVAS_QMETADATA_HANDLE hQMetaData,
    2377             :     MASA_DECODER_HANDLE hMasa,
    2378             :     MASA_ISM_DATA_HANDLE hMasaIsmData,
    2379             :     const int16_t nchan_ism,
    2380             :     uint16_t *bit_stream,
    2381             :     int16_t *next_bit_pos,
    2382             :     const int16_t idx_separated_object,
    2383             :     const int16_t ism_imp,
    2384             :     const int16_t dirac_bs_md_write_idx,
    2385             :     const int16_t dirac_md_buffer_length )
    2386             : {
    2387             :     int16_t sf, band, dir, nbands, nblocks, obj, i;
    2388             :     float energy_ratio_ism[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS][MAX_NUM_OBJECTS];
    2389             :     int16_t *band_mapping;
    2390             :     int16_t b;
    2391             :     int16_t bits_ism[MAX_NUM_OBJECTS], index;
    2392             :     uint16_t idx_el, idx_az;
    2393             :     float azimuth, elevation;
    2394             :     int16_t nb_bits_read;
    2395             :     float delta_phi;
    2396             :     int16_t meta_write_index;
    2397             : 
    2398       63232 :     nb_bits_read = *next_bit_pos;
    2399       63232 :     nbands = hQMetaData->q_direction->cfg.nbands;
    2400       63232 :     nblocks = hQMetaData->q_direction->cfg.nblocks;
    2401             : 
    2402             :     /* Read MASA-to-total energy ratios */
    2403       63232 :     ivas_omasa_decode_masa_to_total( bit_stream, next_bit_pos, hMasaIsmData->masa_to_total_energy_ratio, nbands, nblocks );
    2404       63232 :     if ( nchan_ism > 1 )
    2405             :     {
    2406             :         /* read ISM ratios */
    2407       63232 :         decode_ism_ratios( bit_stream, next_bit_pos, hMasaIsmData->masa_to_total_energy_ratio, energy_ratio_ism, nchan_ism, nbands, nblocks, idx_separated_object );
    2408             :     }
    2409             :     else
    2410             :     {
    2411           0 :         for ( sf = 0; sf < nblocks; sf++ )
    2412             :         {
    2413           0 :             for ( band = 0; band < nbands; band++ )
    2414             :             {
    2415           0 :                 energy_ratio_ism[sf][band][0] = 1.0f;
    2416             :             }
    2417             :         }
    2418             :     }
    2419             : 
    2420             :     /* read ISM metadata */
    2421       63232 :     calculate_nbits_meta( nchan_ism, energy_ratio_ism, hMasaIsmData->masa_to_total_energy_ratio, nblocks, nbands, bits_ism, idx_separated_object, ism_imp );
    2422             : 
    2423      278570 :     for ( obj = 0; obj < nchan_ism; obj++ )
    2424             :     {
    2425      215338 :         hMasaIsmData->bits_ism[obj] = bits_ism[obj];
    2426      215338 :         index = 0;
    2427      215338 :         if ( bits_ism[obj] < 8 ) /* if low resolution, can look to the past */
    2428             :         {
    2429             :             /* read if same as previous */
    2430       45930 :             if ( bit_stream[( *next_bit_pos )--] )
    2431             :             {
    2432        1997 :                 azimuth = hMasaIsmData->q_azimuth_old[obj];
    2433        1997 :                 elevation = hMasaIsmData->q_elevation_old[obj];
    2434             :             }
    2435             :             else
    2436             :             {
    2437      324590 :                 for ( i = 0; i < bits_ism[obj]; i++ )
    2438             :                 {
    2439      280657 :                     index = ( index << 1 ) + bit_stream[( *next_bit_pos )--];
    2440             :                 }
    2441       43933 :                 deindex_spherical_component( index, &azimuth, &elevation, &idx_az, &idx_el, bits_ism[obj], MC_LS_SETUP_INVALID );
    2442             : 
    2443       43933 :                 if ( azimuth * hMasaIsmData->q_azimuth_old[obj] > 0 )
    2444             :                 {
    2445       24861 :                     delta_phi = 180.0f / (float) ( no_phi_masa[bits_ism[obj] - 1][idx_el] ); /* 360/2*/
    2446             :                     /* this is equivalent to testing if 'azimuth - hMasaIsmData->q_azimuth_old[obj] > delta_phi' with limited precision */
    2447       24861 :                     if ( roundf( 100 * ( no_phi_masa[bits_ism[obj] - 1][idx_el] ) * ( azimuth - hMasaIsmData->q_azimuth_old[obj] ) ) / 100.0f > 180.0f )
    2448             :                     {
    2449        2059 :                         azimuth -= delta_phi;
    2450             :                     }
    2451             :                     else
    2452             :                     {
    2453             :                         /* this is equivalent to testing if 'hMasaIsmData->q_azimuth_old[obj] - azimuth > delta_phi' with limited precision */
    2454       22802 :                         if ( roundf( 100 * ( no_phi_masa[bits_ism[obj] - 1][idx_el] ) * ( hMasaIsmData->q_azimuth_old[obj] - azimuth ) ) / 100.0f > 180.0f )
    2455             :                         {
    2456        1684 :                             azimuth += delta_phi;
    2457             :                         }
    2458             :                     }
    2459             :                 }
    2460             : 
    2461       43933 :                 hMasaIsmData->q_azimuth_old[obj] = azimuth;
    2462       43933 :                 hMasaIsmData->q_elevation_old[obj] = elevation;
    2463             :             }
    2464             :         }
    2465             :         else
    2466             :         {
    2467     1870307 :             for ( i = 0; i < bits_ism[obj]; i++ )
    2468             :             {
    2469     1700899 :                 index = ( index << 1 ) + bit_stream[( *next_bit_pos )--];
    2470             :             }
    2471      169408 :             deindex_spherical_component( index, &azimuth, &elevation, &idx_az, &idx_el, bits_ism[obj], MC_LS_SETUP_INVALID );
    2472      169408 :             hMasaIsmData->q_azimuth_old[obj] = azimuth;
    2473      169408 :             hMasaIsmData->q_elevation_old[obj] = elevation;
    2474             :         }
    2475             : 
    2476     1076690 :         for ( sf = 0; sf < MAX_PARAM_SPATIAL_SUBFRAMES; sf++ )
    2477             :         {
    2478      861352 :             meta_write_index = ( dirac_bs_md_write_idx + sf ) % dirac_md_buffer_length;
    2479      861352 :             hMasaIsmData->azimuth_ism[obj][meta_write_index] = (int16_t) rint( azimuth );
    2480      861352 :             hMasaIsmData->elevation_ism[obj][meta_write_index] = (int16_t) rint( elevation );
    2481             :         }
    2482             :     }
    2483             : 
    2484             :     /* Modify ISM metadata based on the MASA-to-total energy ratios */
    2485      287786 :     for ( sf = 0; sf < nblocks; sf++ )
    2486             :     {
    2487     1339748 :         for ( band = 0; band < nbands; band++ )
    2488             :         {
    2489     5109094 :             for ( dir = 0; dir < nchan_ism; dir++ )
    2490             :             {
    2491     3993900 :                 energy_ratio_ism[sf][band][dir] *= ( 1.0f - hMasaIsmData->masa_to_total_energy_ratio[sf][band] );
    2492             :             }
    2493             :         }
    2494             :     }
    2495             : 
    2496             :     /* Set data to struct in bins */
    2497       63232 :     band_mapping = hMasa->data.band_mapping;
    2498      386952 :     for ( band = 0; band < hMasa->config.numCodingBands; ++band )
    2499             :     {
    2500     3083240 :         for ( b = MASA_band_grouping_24[band_mapping[band]]; b < MASA_band_grouping_24[band_mapping[band + 1]]; ++b )
    2501             :         {
    2502    13797600 :             for ( sf = 0; sf < MAX_PARAM_SPATIAL_SUBFRAMES; ++sf )
    2503             :             {
    2504    11038080 :                 if ( nblocks == 1 )
    2505             :                 {
    2506     1657680 :                     i = 0;
    2507             :                 }
    2508             :                 else
    2509             :                 {
    2510     9380400 :                     i = sf;
    2511             :                 }
    2512             : 
    2513    11038080 :                 meta_write_index = ( dirac_bs_md_write_idx + sf ) % dirac_md_buffer_length;
    2514             : 
    2515    48962400 :                 for ( dir = 0; dir < nchan_ism; dir++ )
    2516             :                 {
    2517    37924320 :                     hMasaIsmData->energy_ratio_ism[dir][meta_write_index][b] = energy_ratio_ism[i][band][dir];
    2518             :                 }
    2519             : 
    2520    11038080 :                 if ( hMasaIsmData->hExtData != NULL )
    2521             :                 {
    2522      607680 :                     hMasaIsmData->hExtData->masa_render_masa_to_total[meta_write_index][b] = hMasaIsmData->masa_to_total_energy_ratio[i][band];
    2523             :                 }
    2524             :             }
    2525             :         }
    2526             :     }
    2527             : 
    2528       63232 :     return ( nb_bits_read - *next_bit_pos );
    2529             : }

Generated by: LCOV version 1.14