LCOV - code coverage report
Current view: top level - lib_dec - ivas_spar_md_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- merged total coverage @ efe53129c9ed87a5067dd0a8fb9dca41db9c4add Lines: 908 978 92.8 %
Date: 2026-02-12 08:06:51 Functions: 30 30 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2026 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             : #ifdef DEBUGGING
      36             : #include "debug.h"
      37             : #endif
      38             : #include "math.h"
      39             : #include "prot.h"
      40             : #include "ivas_prot.h"
      41             : #include "ivas_rom_com.h"
      42             : #include <assert.h>
      43             : #include "wmc_auto.h"
      44             : #include "ivas_stat_dec.h"
      45             : 
      46             : 
      47             : /*------------------------------------------------------------------------------------------*
      48             :  * Local constants
      49             :  *------------------------------------------------------------------------------------------*/
      50             : 
      51             : #define IVAS_DEFAULT_DTX_CNG_RAMP ( 8 )
      52             : 
      53             : /* PLC constants */
      54             : static const int16_t ivas_spar_dec_plc_num_frames_keep = 9;
      55             : static const int16_t ivas_spar_dec_plc_num_frames_fade_out = 9;
      56             : static const int16_t ivas_spar_dec_plc_per_frame_ramp_down_gain_dB = 3;
      57             : static const int16_t ivas_spar_dec_plc_max_num_frames_ramp_down = 33;
      58             : static const int16_t ivas_spar_dec_plc_spatial_target[IVAS_SPAR_MAX_CH] = { 1, 0, 0, 0, 0, 0, 0, 0 };
      59             : 
      60             : 
      61             : /*------------------------------------------------------------------------------------------*
      62             :  * Static functions declaration
      63             :  *------------------------------------------------------------------------------------------*/
      64             : 
      65             : static void ivas_get_spar_matrices( ivas_spar_md_dec_state_t *hMdDec, const int16_t num_bands_out, const int16_t n_ts, const int16_t bw, const int16_t dtx_vad, const int16_t nB, const int16_t numch_out, const int16_t active_w_vlbr, const int16_t dyn_active_w_flag );
      66             : 
      67             : static void ivas_decode_arith_bs( ivas_spar_md_dec_state_t *hMdDec, Decoder_State *st, const uint16_t qsi, const int16_t nB, const int16_t bands_bw, int16_t *pDo_diff, const int16_t strat, const int32_t ivas_total_brate );
      68             : 
      69             : static void ivas_decode_huffman_bs( ivas_spar_md_dec_state_t *hMdDec, Decoder_State *st, const uint16_t qsi, const int16_t nB, const int16_t bands_bw );
      70             : 
      71             : static void ivas_fill_band_coeffs_idx( ivas_band_coeffs_ind_t *pBands_idx, const int16_t nB, int16_t *pSymbol_re, ivas_cell_dim_t *pCell_dims, ivas_coeffs_type_t coeff_type );
      72             : 
      73             : static void ivas_mat_col_rearrange( float in_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH], const int16_t order[IVAS_SPAR_MAX_CH], const int16_t i_ts, float ***mixer_mat, const int16_t bands, const int16_t num_ch );
      74             : 
      75             : static void ivas_spar_dec_compute_ramp_down_post_matrix( ivas_spar_md_dec_state_t *hMdDec, const int16_t num_bands, const int16_t bfi, const int16_t num_md_sub_frames );
      76             : 
      77             : static void ivas_spar_md_fill_invalid_bands( ivas_spar_dec_matrices_t *pSpar_coeffs, ivas_spar_dec_matrices_t *pSpar_coeffs_prev, const int16_t *valid_bands, int16_t *base_band_age, const int16_t num_bands, const int16_t numch_out, const int16_t num_md_sub_frames );
      78             : 
      79             : static void ivas_spar_md_fill_invalid_bandcoeffs( ivas_band_coeffs_t *pBand_coeffs, ivas_band_coeffs_t *pBand_coeffs_prev, const int16_t *valid_bands, int16_t *base_band_age, int16_t *first_valid_frame, const int16_t num_bands );
      80             : static ivas_error ivas_spar_set_dec_config( ivas_spar_md_dec_state_t *hMdDec, const int16_t nchan_transport, float *pFC );
      81             : 
      82             : static void ivas_parse_parameter_bitstream_dtx( ivas_spar_md_t *pSpar_md, Decoder_State *st, const int16_t bw, const int16_t num_bands, int16_t *num_dmx_per_band, int16_t *num_dec_per_band );
      83             : 
      84             : static ivas_error ivas_deindex_real_index( const int16_t *index, const int16_t q_levels, const float min_value, const float max_value, float *quant, const int16_t num_ch_dim2 );
      85             : 
      86             : static void ivas_spar_dec_parse_md_bs( ivas_spar_md_dec_state_t *hMdDec, Decoder_State *st, int16_t *nB, int16_t *bands_bw, int16_t *dtx_vad, const int32_t ivas_total_brate, const int16_t sba_inactive_mode );
      87             : 
      88             : 
      89             : /*-------------------------------------------------------------------------
      90             :  * ivas_spar_md_dec_matrix_open()
      91             :  *
      92             :  * Allocate and initialize SPAR MD decoder matrices
      93             :  *------------------------------------------------------------------------*/
      94             : 
      95       98605 : ivas_error ivas_spar_md_dec_matrix_open(
      96             :     ivas_spar_md_dec_state_t *hMdDec, /* i/o: SPAR MD decoder handle       */
      97             :     const int16_t num_channels,       /* i  : number of internal channels  */
      98             :     const int16_t num_md_sub_frames   /* i  : number of MD subframes       */
      99             : )
     100             : {
     101             :     int16_t i, j;
     102             :     int16_t k;
     103       98605 :     if ( ( hMdDec->spar_md.band_coeffs = (ivas_band_coeffs_t *) malloc( IVAS_MAX_NUM_BANDS * num_md_sub_frames * sizeof( ivas_band_coeffs_t ) ) ) == NULL )
     104             :     {
     105           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for band_coeffs in SPAR MD" );
     106             :     }
     107       98605 :     if ( ( hMdDec->band_coeffs_prev = (ivas_band_coeffs_t *) malloc( IVAS_MAX_NUM_BANDS * sizeof( ivas_band_coeffs_t ) ) ) == NULL )
     108             :     {
     109           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for band_coeffs in SPAR MD" );
     110             :     }
     111       98605 :     if ( ( hMdDec->mixer_mat = (float ***) malloc( num_channels * sizeof( float ** ) ) ) == NULL )
     112             :     {
     113           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     114             :     }
     115      568146 :     for ( i = 0; i < num_channels; i++ )
     116             :     {
     117      469541 :         if ( ( hMdDec->mixer_mat[i] = (float **) malloc( num_channels * sizeof( float * ) ) ) == NULL )
     118             :         {
     119           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     120             :         }
     121     3032942 :         for ( j = 0; j < num_channels; j++ )
     122             :         {
     123     2563401 :             if ( ( hMdDec->mixer_mat[i][j] = (float *) malloc( num_md_sub_frames * IVAS_MAX_NUM_BANDS * sizeof( float ) ) ) == NULL )
     124             :             {
     125           0 :                 return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     126             :             }
     127             :         }
     128             :     }
     129             : 
     130       98605 :     if ( ( hMdDec->spar_coeffs.C_re = (float ***) malloc( num_channels * sizeof( float ** ) ) ) == NULL )
     131             :     {
     132           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     133             :     }
     134      568146 :     for ( i = 0; i < num_channels; i++ )
     135             :     {
     136      469541 :         if ( ( hMdDec->spar_coeffs.C_re[i] = (float **) malloc( num_channels * sizeof( float * ) ) ) == NULL )
     137             :         {
     138           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     139             :         }
     140     3032942 :         for ( j = 0; j < num_channels; j++ )
     141             :         {
     142     2563401 :             if ( ( hMdDec->spar_coeffs.C_re[i][j] = (float *) malloc( num_md_sub_frames * IVAS_MAX_NUM_BANDS * sizeof( float ) ) ) == NULL )
     143             :             {
     144           0 :                 return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     145             :             }
     146             :         }
     147             :     }
     148             : 
     149       98605 :     if ( ( hMdDec->spar_coeffs.P_re = (float ***) malloc( num_channels * sizeof( float ** ) ) ) == NULL )
     150             :     {
     151           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     152             :     }
     153      568146 :     for ( i = 0; i < num_channels; i++ )
     154             :     {
     155      469541 :         if ( ( hMdDec->spar_coeffs.P_re[i] = (float **) malloc( num_channels * sizeof( float * ) ) ) == NULL )
     156             :         {
     157           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     158             :         }
     159     3032942 :         for ( j = 0; j < num_channels; j++ )
     160             :         {
     161     2563401 :             if ( ( hMdDec->spar_coeffs.P_re[i][j] = (float *) malloc( num_md_sub_frames * IVAS_MAX_NUM_BANDS * sizeof( float ) ) ) == NULL )
     162             :             {
     163           0 :                 return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     164             :             }
     165             :         }
     166             :     }
     167             : 
     168       98605 :     if ( ( hMdDec->spar_coeffs_prev.C_re = (float ***) malloc( num_channels * sizeof( float ** ) ) ) == NULL )
     169             :     {
     170           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     171             :     }
     172      568146 :     for ( i = 0; i < num_channels; i++ )
     173             :     {
     174      469541 :         if ( ( hMdDec->spar_coeffs_prev.C_re[i] = (float **) malloc( num_channels * sizeof( float * ) ) ) == NULL )
     175             :         {
     176           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     177             :         }
     178     3032942 :         for ( j = 0; j < num_channels; j++ )
     179             :         {
     180     2563401 :             if ( ( hMdDec->spar_coeffs_prev.C_re[i][j] = (float *) malloc( IVAS_MAX_NUM_BANDS * sizeof( float ) ) ) == NULL )
     181             :             {
     182           0 :                 return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     183             :             }
     184             :         }
     185             :     }
     186             : 
     187       98605 :     if ( ( hMdDec->spar_coeffs_prev.P_re = (float ***) malloc( num_channels * sizeof( float ** ) ) ) == NULL )
     188             :     {
     189           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     190             :     }
     191      568146 :     for ( i = 0; i < num_channels; i++ )
     192             :     {
     193      469541 :         if ( ( hMdDec->spar_coeffs_prev.P_re[i] = (float **) malloc( num_channels * sizeof( float * ) ) ) == NULL )
     194             :         {
     195           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     196             :         }
     197     3032942 :         for ( j = 0; j < num_channels; j++ )
     198             :         {
     199     2563401 :             if ( ( hMdDec->spar_coeffs_prev.P_re[i][j] = (float *) malloc( IVAS_MAX_NUM_BANDS * sizeof( float ) ) ) == NULL )
     200             :             {
     201           0 :                 return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     202             :             }
     203             :         }
     204             :     }
     205             : 
     206       98605 :     if ( ( hMdDec->spar_coeffs_tar.C_re = (float ***) malloc( num_channels * sizeof( float ** ) ) ) == NULL )
     207             :     {
     208           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     209             :     }
     210      568146 :     for ( i = 0; i < num_channels; i++ )
     211             :     {
     212      469541 :         if ( ( hMdDec->spar_coeffs_tar.C_re[i] = (float **) malloc( num_channels * sizeof( float * ) ) ) == NULL )
     213             :         {
     214           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     215             :         }
     216     3032942 :         for ( j = 0; j < num_channels; j++ )
     217             :         {
     218     2563401 :             if ( ( hMdDec->spar_coeffs_tar.C_re[i][j] = (float *) malloc( IVAS_MAX_NUM_BANDS * sizeof( float ) ) ) == NULL )
     219             :             {
     220           0 :                 return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     221             :             }
     222             :         }
     223             :     }
     224             : 
     225       98605 :     if ( ( hMdDec->spar_coeffs_tar.P_re = (float ***) malloc( num_channels * sizeof( float ** ) ) ) == NULL )
     226             :     {
     227           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     228             :     }
     229      568146 :     for ( i = 0; i < num_channels; i++ )
     230             :     {
     231      469541 :         if ( ( hMdDec->spar_coeffs_tar.P_re[i] = (float **) malloc( num_channels * sizeof( float * ) ) ) == NULL )
     232             :         {
     233           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     234             :         }
     235     3032942 :         for ( j = 0; j < num_channels; j++ )
     236             :         {
     237     2563401 :             if ( ( hMdDec->spar_coeffs_tar.P_re[i][j] = (float *) malloc( IVAS_MAX_NUM_BANDS * sizeof( float ) ) ) == NULL )
     238             :             {
     239           0 :                 return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
     240             :             }
     241             :         }
     242             :     }
     243      568146 :     for ( i = 0; i < num_channels; i++ )
     244             :     {
     245     3032942 :         for ( j = 0; j < num_channels; j++ )
     246             :         {
     247    33324213 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
     248             :             {
     249    30760812 :                 hMdDec->spar_coeffs_prev.C_re[i][j][k] = 0.0f;
     250    30760812 :                 hMdDec->spar_coeffs_prev.P_re[i][j][k] = 0.0f;
     251    30760812 :                 hMdDec->spar_coeffs_tar.C_re[i][j][k] = 0.0f;
     252    30760812 :                 hMdDec->spar_coeffs_tar.P_re[i][j][k] = 0.0f;
     253             :             }
     254             :         }
     255             :     }
     256             : 
     257       98605 :     return IVAS_ERR_OK;
     258             : }
     259             : 
     260             : 
     261             : /*-------------------------------------------------------------------------
     262             :  * ivas_get_spar_dec_md_num_subframes()
     263             :  *
     264             :  * return number of MD subframes
     265             :  *------------------------------------------------------------------------*/
     266             : 
     267             : /*! r: number of MD subframes */
     268    86650493 : int16_t ivas_get_spar_dec_md_num_subframes(
     269             :     const int16_t sba_order,             /* i  : Ambisonic (SBA) order        */
     270             :     const int32_t ivas_total_brate,      /* i  : IVAS total bitrate           */
     271             :     const int32_t ivas_last_active_brate /* i  : IVAS last active bitrate     */
     272             : )
     273             : {
     274             :     int16_t num_subframes;
     275             : 
     276    86650493 :     num_subframes = MAX_PARAM_SPATIAL_SUBFRAMES;
     277    86650493 :     if ( sba_order > SBA_FOA_ORDER )
     278             :     {
     279    16862861 :         if ( ivas_total_brate >= IVAS_512k )
     280             :         {
     281     2943652 :             num_subframes = 1;
     282             :         }
     283             :     }
     284             : 
     285    86650493 :     if ( ( ivas_total_brate <= IVAS_SID_5k2 && ivas_last_active_brate < IVAS_24k4 ) || ( ivas_total_brate > IVAS_SID_5k2 && ivas_total_brate < IVAS_24k4 ) )
     286             :     {
     287             : 
     288     7147395 :         num_subframes = 1;
     289             :     }
     290             : 
     291    86650493 :     return ( num_subframes );
     292             : }
     293             : 
     294             : 
     295             : /*-------------------------------------------------------------------------
     296             :  * ivas_spar_md_dec_open()
     297             :  *
     298             :  * Allocate and initialize SPAR MD decoder handle
     299             :  *------------------------------------------------------------------------*/
     300             : 
     301       96580 : ivas_error ivas_spar_md_dec_open(
     302             :     ivas_spar_md_dec_state_t **hMdDec_out,      /* i/o: SPAR MD decoder handle      */
     303             :     const DECODER_CONFIG_HANDLE hDecoderConfig, /* i  : configuration structure     */
     304             :     const int16_t num_channels,                 /* i  : number of internal channels */
     305             :     const int16_t sba_order,                    /* i  : SBA order                   */
     306             :     const int16_t sid_format,                   /* i  : SID format                  */
     307             :     const int32_t last_active_ivas_total_brate  /* i  : IVAS last active bitrate    */
     308             : )
     309             : {
     310             :     ivas_spar_md_dec_state_t *hMdDec;
     311             :     ivas_error error;
     312             :     int16_t num_md_sub_frames;
     313             : 
     314       96580 :     error = IVAS_ERR_OK;
     315             : 
     316       96580 :     if ( ( hMdDec = (ivas_spar_md_dec_state_t *) malloc( sizeof( ivas_spar_md_dec_state_t ) ) ) == NULL )
     317             :     {
     318           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD decoder" );
     319             :     }
     320             : 
     321       96580 :     num_md_sub_frames = ivas_get_spar_dec_md_num_subframes( sba_order, hDecoderConfig->ivas_total_brate, last_active_ivas_total_brate );
     322             : 
     323       96580 :     if ( ( error = ivas_spar_md_dec_matrix_open( hMdDec, num_channels, num_md_sub_frames ) ) != IVAS_ERR_OK )
     324             :     {
     325           0 :         return error;
     326             :     }
     327             : 
     328       96580 :     if ( hDecoderConfig->ivas_total_brate == IVAS_SID_5k2 )
     329             :     {
     330           0 :         if ( sid_format == SID_SBA_2TC )
     331             :         {
     332           0 :             hMdDec->table_idx = ivas_get_spar_table_idx( IVAS_48k, sba_order, SPAR_CONFIG_BW, NULL, NULL );
     333             :         }
     334             :         else
     335             :         {
     336           0 :             hMdDec->table_idx = ivas_get_spar_table_idx( IVAS_24k4, sba_order, SPAR_CONFIG_BW, NULL, NULL );
     337             :         }
     338             :     }
     339             :     else
     340             :     {
     341       96580 :         hMdDec->table_idx = ivas_get_spar_table_idx( hDecoderConfig->ivas_total_brate, sba_order, SPAR_CONFIG_BW, NULL, NULL );
     342             :     }
     343             : 
     344       96580 :     if ( ( error = ivas_spar_md_dec_init( hMdDec, hDecoderConfig, num_channels, sba_order ) ) != IVAS_ERR_OK )
     345             :     {
     346           0 :         return error;
     347             :     }
     348             : 
     349       96580 :     *hMdDec_out = hMdDec;
     350             : 
     351       96580 :     return error;
     352             : }
     353             : 
     354             : 
     355             : /*-------------------------------------------------------------------------
     356             :  * ivas_spar_md_dec_matrix_close()
     357             :  *
     358             :  * Deallocate SPAR MD decoder matrices
     359             :  *------------------------------------------------------------------------*/
     360             : 
     361       98605 : void ivas_spar_md_dec_matrix_close(
     362             :     ivas_spar_md_dec_state_t *hMdDecoder, /* i/o: SPAR MD decoder handle      */
     363             :     const int16_t num_channels            /* i  : number of internal channels */
     364             : )
     365             : {
     366             :     int16_t i, j;
     367             : 
     368       98605 :     if ( hMdDecoder->spar_md.band_coeffs != NULL )
     369             :     {
     370       98605 :         free( hMdDecoder->spar_md.band_coeffs );
     371       98605 :         hMdDecoder->spar_md.band_coeffs = NULL;
     372             :     }
     373       98605 :     if ( hMdDecoder->band_coeffs_prev != NULL )
     374             :     {
     375       98605 :         free( hMdDecoder->band_coeffs_prev );
     376       98605 :         hMdDecoder->band_coeffs_prev = NULL;
     377             :     }
     378             : 
     379       98605 :     if ( hMdDecoder->mixer_mat != NULL )
     380             :     {
     381      568146 :         for ( i = 0; i < num_channels; i++ )
     382             :         {
     383     3032942 :             for ( j = 0; j < num_channels; j++ )
     384             :             {
     385     2563401 :                 free( hMdDecoder->mixer_mat[i][j] );
     386             :             }
     387      469541 :             free( hMdDecoder->mixer_mat[i] );
     388             :         }
     389       98605 :         free( hMdDecoder->mixer_mat );
     390             :     }
     391             : 
     392       98605 :     if ( hMdDecoder->spar_coeffs.C_re != NULL )
     393             :     {
     394      568146 :         for ( i = 0; i < num_channels; i++ )
     395             :         {
     396     3032942 :             for ( j = 0; j < num_channels; j++ )
     397             :             {
     398     2563401 :                 free( hMdDecoder->spar_coeffs.C_re[i][j] );
     399             :             }
     400      469541 :             free( hMdDecoder->spar_coeffs.C_re[i] );
     401             :         }
     402       98605 :         free( hMdDecoder->spar_coeffs.C_re );
     403             :     }
     404             : 
     405       98605 :     if ( hMdDecoder->spar_coeffs.P_re != NULL )
     406             :     {
     407      568146 :         for ( i = 0; i < num_channels; i++ )
     408             :         {
     409     3032942 :             for ( j = 0; j < num_channels; j++ )
     410             :             {
     411     2563401 :                 free( hMdDecoder->spar_coeffs.P_re[i][j] );
     412             :             }
     413      469541 :             free( hMdDecoder->spar_coeffs.P_re[i] );
     414             :         }
     415       98605 :         free( hMdDecoder->spar_coeffs.P_re );
     416             :     }
     417             : 
     418       98605 :     if ( hMdDecoder->spar_coeffs_prev.C_re != NULL )
     419             :     {
     420      568146 :         for ( i = 0; i < num_channels; i++ )
     421             :         {
     422     3032942 :             for ( j = 0; j < num_channels; j++ )
     423             :             {
     424     2563401 :                 free( hMdDecoder->spar_coeffs_prev.C_re[i][j] );
     425             :             }
     426      469541 :             free( hMdDecoder->spar_coeffs_prev.C_re[i] );
     427             :         }
     428       98605 :         free( hMdDecoder->spar_coeffs_prev.C_re );
     429             :     }
     430             : 
     431       98605 :     if ( hMdDecoder->spar_coeffs_prev.P_re != NULL )
     432             :     {
     433      568146 :         for ( i = 0; i < num_channels; i++ )
     434             :         {
     435     3032942 :             for ( j = 0; j < num_channels; j++ )
     436             :             {
     437     2563401 :                 free( hMdDecoder->spar_coeffs_prev.P_re[i][j] );
     438             :             }
     439      469541 :             free( hMdDecoder->spar_coeffs_prev.P_re[i] );
     440             :         }
     441       98605 :         free( hMdDecoder->spar_coeffs_prev.P_re );
     442             :     }
     443             : 
     444       98605 :     if ( hMdDecoder->spar_coeffs_tar.C_re != NULL )
     445             :     {
     446      568146 :         for ( i = 0; i < num_channels; i++ )
     447             :         {
     448     3032942 :             for ( j = 0; j < num_channels; j++ )
     449             :             {
     450     2563401 :                 free( hMdDecoder->spar_coeffs_tar.C_re[i][j] );
     451             :             }
     452      469541 :             free( hMdDecoder->spar_coeffs_tar.C_re[i] );
     453             :         }
     454       98605 :         free( hMdDecoder->spar_coeffs_tar.C_re );
     455             :     }
     456             : 
     457       98605 :     if ( hMdDecoder->spar_coeffs_tar.P_re != NULL )
     458             :     {
     459      568146 :         for ( i = 0; i < num_channels; i++ )
     460             :         {
     461     3032942 :             for ( j = 0; j < num_channels; j++ )
     462             :             {
     463     2563401 :                 free( hMdDecoder->spar_coeffs_tar.P_re[i][j] );
     464             :             }
     465      469541 :             free( hMdDecoder->spar_coeffs_tar.P_re[i] );
     466             :         }
     467       98605 :         free( hMdDecoder->spar_coeffs_tar.P_re );
     468             :     }
     469             : 
     470       98605 :     return;
     471             : }
     472             : 
     473             : 
     474             : /*-------------------------------------------------------------------------
     475             :  * ivas_spar_md_dec_close()
     476             :  *
     477             :  * Deallocate SPAR MD decoder handle
     478             :  *------------------------------------------------------------------------*/
     479             : 
     480       96580 : void ivas_spar_md_dec_close(
     481             :     ivas_spar_md_dec_state_t **hMdDec /* i/o: SPAR MD decoder handle     */
     482             : )
     483             : {
     484             :     ivas_spar_md_dec_state_t *hMdDecoder;
     485             :     int16_t num_channels;
     486             : 
     487       96580 :     hMdDecoder = *hMdDec;
     488       96580 :     num_channels = hMdDecoder->spar_md_cfg.num_umx_chs;
     489             : 
     490       96580 :     ivas_spar_md_dec_matrix_close( hMdDecoder, num_channels );
     491             : 
     492       96580 :     free( *hMdDec );
     493       96580 :     *hMdDec = NULL;
     494             : 
     495       96580 :     return;
     496             : }
     497             : 
     498             : 
     499             : /*-----------------------------------------------------------------------------------------*
     500             :  * Function ivas_spar_md_dec_init()
     501             :  *
     502             :  * SPAR MD decoder initialization
     503             :  *-----------------------------------------------------------------------------------------*/
     504             : 
     505      108594 : ivas_error ivas_spar_md_dec_init(
     506             :     ivas_spar_md_dec_state_t *hMdDec,           /* i/o: SPAR MD decoder handle       */
     507             :     const DECODER_CONFIG_HANDLE hDecoderConfig, /* i  : configuration structure      */
     508             :     const int16_t num_channels,                 /* i  : number of internal channels  */
     509             :     const int16_t sba_order                     /* i  : SBA order                    */
     510             : )
     511             : {
     512             :     int16_t i, j;
     513             :     int16_t nchan_transport;
     514             :     float pFC[IVAS_MAX_NUM_BANDS], PR_minmax[2];
     515             :     ivas_error error;
     516             : 
     517      108594 :     ivas_sba_get_spar_hoa_md_flag( sba_order, hDecoderConfig->ivas_total_brate, &hMdDec->spar_hoa_md_flag, &hMdDec->spar_hoa_dirac2spar_md_flag );
     518             : 
     519      108594 :     ivas_sba_get_spar_hoa_ch_ind( num_channels, hDecoderConfig->ivas_total_brate, hMdDec->HOA_md_ind );
     520             : 
     521      108594 :     hMdDec->spar_md.num_bands = ( hMdDec->spar_hoa_md_flag ) ? IVAS_MAX_NUM_BANDS : min( IVAS_MAX_NUM_BANDS, SPAR_DIRAC_SPLIT_START_BAND );
     522             : 
     523      108594 :     ivas_spar_set_bitrate_config( &hMdDec->spar_md_cfg, hMdDec->table_idx, hMdDec->spar_md.num_bands, hMdDec->spar_hoa_dirac2spar_md_flag, 0, 0, 0 );
     524             : 
     525      108594 :     nchan_transport = hMdDec->spar_md_cfg.nchan_transport;
     526             : 
     527             :     /* get FB coefficients */
     528     1411722 :     for ( i = 0; i < IVAS_MAX_NUM_BANDS; i++ )
     529             :     {
     530     1303128 :         pFC[i] = ivas_fb_fcs_12band_1ms[i] * hDecoderConfig->output_Fs * 0.5f;
     531             :     }
     532             : 
     533      108594 :     if ( ( error = ivas_spar_set_dec_config( hMdDec, nchan_transport, pFC ) ) != IVAS_ERR_OK )
     534             :     {
     535           0 :         return error;
     536             :     }
     537             : 
     538      108594 :     if ( nchan_transport != 2 && ( ( hMdDec->spar_md_cfg.remix_unmix_order == 2 ) || ( hMdDec->spar_md_cfg.remix_unmix_order == 1 ) ) )
     539             :     {
     540           0 :         return IVAS_ERR_INTERNAL;
     541             :     }
     542             : 
     543             :     /* DTX quant init */
     544      108594 :     PR_minmax[0] = hMdDec->spar_md_cfg.quant_strat[0].PR.min;
     545      108594 :     PR_minmax[1] = hMdDec->spar_md_cfg.quant_strat[0].PR.max;
     546      108594 :     ivas_spar_quant_dtx_init( &hMdDec->spar_md, PR_minmax );
     547             : 
     548      108594 :     ivas_spar_arith_coeffs_com_init( &hMdDec->arith_coeffs, &hMdDec->spar_md_cfg, hMdDec->table_idx, DEC );
     549      108594 :     ivas_spar_huff_coeffs_com_init( &hMdDec->huff_coeffs, &hMdDec->spar_md_cfg, hMdDec->table_idx, DEC );
     550             : 
     551      108594 :     hMdDec->spar_md_cfg.prev_quant_idx = -1;
     552             : 
     553             :     /* initialize PLC state */
     554      108594 :     set_s( hMdDec->valid_bands, 0, IVAS_MAX_NUM_BANDS );
     555      108594 :     set_s( hMdDec->base_band_age, 0, IVAS_MAX_NUM_BANDS );
     556      108594 :     set_s( hMdDec->base_band_coeffs_age, 0, IVAS_MAX_NUM_BANDS );
     557      108594 :     hMdDec->spar_plc_num_lost_frames = 0;
     558      108594 :     hMdDec->spar_plc_enable_fadeout_flag = 1;
     559      108594 :     hMdDec->dtx_md_smoothing_cntr = 1;
     560             : 
     561      108594 :     ivas_clear_band_coeffs( hMdDec->spar_md.band_coeffs, IVAS_MAX_NUM_BANDS );
     562      108594 :     ivas_clear_band_coeffs( hMdDec->band_coeffs_prev, IVAS_MAX_NUM_BANDS );
     563      108594 :     ivas_clear_band_coeff_idx( hMdDec->spar_md.band_coeffs_idx, IVAS_MAX_NUM_BANDS );
     564      108594 :     ivas_clear_band_coeff_idx( hMdDec->spar_md_prev.band_coeffs_idx, IVAS_MAX_NUM_BANDS );
     565      108594 :     ivas_clear_band_coeff_idx( hMdDec->spar_md_prev.band_coeffs_idx_mapped, IVAS_MAX_NUM_BANDS );
     566             : 
     567      108594 :     hMdDec->spar_md.dtx_vad = 0;
     568      108594 :     hMdDec->td_decorr_flag = 1;
     569             : 
     570      108594 :     set_f( hMdDec->spar_md.en_ratio_slow, 0.0f, IVAS_MAX_NUM_BANDS );
     571      108594 :     set_f( hMdDec->spar_md.ref_pow_slow, 0.0f, IVAS_MAX_NUM_BANDS );
     572             : 
     573      108594 :     set_zero( hMdDec->smooth_fac, IVAS_MAX_NUM_BANDS );
     574     1411722 :     for ( i = 0; i < IVAS_MAX_NUM_BANDS; i++ )
     575             :     {
     576     1303128 :         set_zero( hMdDec->smooth_buf[i], 2 * SBA_DIRAC_NRG_SMOOTH_LONG + 1 );
     577             :     }
     578             : 
     579     1303128 :     for ( i = 0; i < IVAS_SPAR_MAX_CH; i++ )
     580             :     {
     581    14334408 :         for ( j = 0; j < IVAS_SPAR_MAX_CH; j++ )
     582             :         {
     583    13139874 :             set_zero( hMdDec->mixer_mat_prev2[i][j], IVAS_MAX_NUM_BANDS );
     584             :         }
     585             :     }
     586      108594 :     hMdDec->first_valid_frame = 1;
     587             : 
     588      108594 :     hMdDec->spar_md.res_ind = 0;
     589      108594 :     hMdDec->spar_md.prior_dyn_active_w_flag = 0;
     590             : 
     591      108594 :     return IVAS_ERR_OK;
     592             : }
     593             : 
     594             : 
     595             : /*-----------------------------------------------------------------------------------------*
     596             :  * Function ivas_spar_set_dec_config()
     597             :  *
     598             :  * Set configuration for SPAR MD decoder
     599             :  *-----------------------------------------------------------------------------------------*/
     600             : 
     601      108594 : static ivas_error ivas_spar_set_dec_config(
     602             :     ivas_spar_md_dec_state_t *hMdDec,
     603             :     const int16_t nchan_transport,
     604             :     float *pFC )
     605             : {
     606             :     int16_t i, j, nchan, dmx_ch;
     607             : 
     608      364496 :     for ( i = 0; i < nchan_transport; i++ )
     609             :     {
     610      255902 :         hMdDec->spar_md_cfg.max_freq_per_chan[i] = ivas_spar_br_table_consts[hMdDec->table_idx].fpcs;
     611             :     }
     612             : 
     613      108594 :     nchan = ivas_sba_get_nchan_metadata( ivas_spar_br_table_consts[hMdDec->table_idx].sba_order, ivas_spar_br_table_consts[hMdDec->table_idx].ivas_total_brate );
     614             : 
     615      108594 :     switch ( nchan )
     616             :     {
     617       91281 :         case 4: /* FOA_CHANNELS */
     618       91281 :             hMdDec->num_decorr = IVAS_TD_DECORR_OUT_3CH;
     619       91281 :             break;
     620        2610 :         case 9: /* IVAS_HOA_2_CH */
     621        2610 :             hMdDec->num_decorr = IVAS_TD_DECORR_OUT_5CH;
     622        2610 :             break;
     623        4198 :         case 6: /* IVAS_HOA_2_CH */
     624        4198 :             hMdDec->num_decorr = IVAS_TD_DECORR_OUT_2CH;
     625        4198 :             break;
     626        6326 :         case 8: /* IVAS_HOA_3_CH */
     627        6326 :             hMdDec->num_decorr = IVAS_TD_DECORR_OUT_4CH;
     628        6326 :             break;
     629             :     }
     630             : 
     631      108594 :     hMdDec->spar_md_cfg.num_umx_chs = nchan;
     632             : 
     633      108594 :     dmx_ch = 0;
     634     1411722 :     for ( i = 0; i < IVAS_MAX_NUM_BANDS; i++ )
     635             :     {
     636     1303128 :         dmx_ch = 0;
     637     4373952 :         for ( j = 0; j < nchan_transport; j++ )
     638             :         {
     639     3070824 :             if ( pFC[i] < hMdDec->spar_md_cfg.max_freq_per_chan[j] )
     640             :             {
     641     3070824 :                 dmx_ch += 1;
     642             :             }
     643             :         }
     644             : 
     645     1303128 :         hMdDec->spar_md_cfg.num_dmx_chans_per_band[i] = hMdDec->spar_md_cfg.nchan_transport;
     646     1303128 :         hMdDec->spar_md_cfg.num_decorr_per_band[i] = nchan - hMdDec->spar_md_cfg.nchan_transport;
     647             :     }
     648             : 
     649      108594 :     hMdDec->spar_md_cfg.nchan_transport = dmx_ch;
     650             : 
     651      108594 :     return IVAS_ERR_OK;
     652             : }
     653             : 
     654             : 
     655             : /*-----------------------------------------------------------------------------------------*
     656             :  * Function ivas_dec_mono_sba_handling()
     657             :  *
     658             :  *
     659             :  *-----------------------------------------------------------------------------------------*/
     660             : 
     661     8709329 : static void ivas_dec_mono_sba_handling(
     662             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder handle                                 */
     663             : )
     664             : {
     665             :     int16_t mono_flag, b, block;
     666             : 
     667     8709329 :     mono_flag = 1;
     668             : 
     669    39798731 :     for ( b = 0; b < st_ivas->hQMetaData->q_direction[0].cfg.nbands; b++ )
     670             :     {
     671   155447010 :         for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; ++block )
     672             :         {
     673   124357608 :             float azimuth = st_ivas->hQMetaData->q_direction[0].band_data[b].azimuth[block];
     674   124357608 :             float elevation = st_ivas->hQMetaData->q_direction[0].band_data[b].azimuth[block];
     675   124357608 :             float energy_ratio = st_ivas->hQMetaData->q_direction[0].band_data[0].energy_ratio[block];
     676   124357608 :             if (
     677    30836227 :                 ( azimuth != 0.0f ) ||
     678    30836227 :                 ( elevation != 0.0f ) ||
     679             :                 ( energy_ratio > 0.15f ) ) /* 0.15f is just above the lowest quantised value. */
     680             :             {
     681   110151441 :                 mono_flag = 0;
     682             :             }
     683             :         }
     684             :     }
     685             : 
     686             :     /* Combine the SPAR prediction coefs flag with the azimuth, elevation and energy ratio flag.*/
     687     8709329 :     mono_flag = mono_flag && ivas_spar_chk_zero_coefs( st_ivas );
     688             : 
     689     8709329 :     if ( mono_flag )
     690             :     {
     691             :         /* Set Energy Ratio values to be zero */
     692        3256 :         for ( b = 0; b < st_ivas->hQMetaData->q_direction[0].cfg.nbands; b++ )
     693             :         {
     694        2442 :             set_zero( st_ivas->hQMetaData->q_direction[0].band_data[b].energy_ratio, MAX_PARAM_SPATIAL_SUBFRAMES );
     695             :         }
     696         814 :         if ( st_ivas->hDirAC != NULL )
     697             :         {
     698        5110 :             for ( block = 0; block < st_ivas->hSpatParamRendCom->dirac_md_buffer_length; ++block )
     699             :             {
     700             :                 /* Set directional Energy Ratio values to be zero */
     701        4380 :                 set_zero( st_ivas->hSpatParamRendCom->energy_ratio1[block], st_ivas->hSpatParamRendCom->num_freq_bands );
     702        4380 :                 if ( st_ivas->hQMetaData->no_directions == 2 )
     703             :                 {
     704           0 :                     set_zero( st_ivas->hSpatParamRendCom->energy_ratio2[block], st_ivas->hSpatParamRendCom->num_freq_bands );
     705             :                 }
     706             :                 /* Set Diffuseness values to be 1.0 */
     707        4380 :                 set_f( st_ivas->hSpatParamRendCom->diffuseness_vector[block], 1.0f, st_ivas->hSpatParamRendCom->num_freq_bands );
     708             :             }
     709             :         }
     710             :     }
     711             : 
     712     8709329 :     return;
     713             : }
     714             : 
     715             : 
     716             : /*-----------------------------------------------------------------------------------------*
     717             :  * Function ivas_spar_md_dec_process()
     718             :  *
     719             :  * SPAR Meta Data decoder process
     720             :  *-----------------------------------------------------------------------------------------*/
     721             : 
     722     8709329 : void ivas_spar_md_dec_process(
     723             :     Decoder_Struct *st_ivas,     /* i/o: IVAS decoder handle                                */
     724             :     Decoder_State *st0,          /* i/o: decoder state structure - for bitstream handling   */
     725             :     const int16_t num_bands_out, /* i  : number of output bands                             */
     726             :     const int16_t sba_order      /* i  : Ambisonic (SBA) order                              */
     727             : )
     728             : {
     729             :     int16_t j, k, b, bw, dtx_vad, nB, i_ts;
     730             :     ivas_spar_md_dec_state_t *hMdDec;
     731             :     int16_t num_md_chs;
     732             :     int16_t num_md_sub_frames;
     733             :     int16_t dyn_active_w_flag;
     734             :     int16_t active_w_vlbr;
     735             : 
     736     8709329 :     hMdDec = st_ivas->hSpar->hMdDec;
     737             : 
     738     8709329 :     active_w_vlbr = ( st_ivas->hDecoderConfig->ivas_total_brate < IVAS_24k4 ) ? 1 : 0;
     739             : 
     740     8709329 :     num_md_chs = ivas_sba_get_nchan_metadata( sba_order, st_ivas->hDecoderConfig->ivas_total_brate );
     741             : 
     742     8709329 :     num_md_sub_frames = ivas_get_spar_dec_md_num_subframes( sba_order, st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->last_active_ivas_total_brate );
     743             : 
     744     8709329 :     if ( hMdDec->spar_md_cfg.nchan_transport > 1 && hMdDec->spar_md_cfg.nchan_transport <= 3 )
     745             :     {
     746     3881214 :         hMdDec->spar_md.res_ind = 0;
     747     3881214 :         dyn_active_w_flag = get_next_indice( st0, 1 );
     748     3881214 :         if ( dyn_active_w_flag == 1 )
     749             :         {
     750        1936 :             if ( hMdDec->spar_md_cfg.nchan_transport == 2 )
     751             :             {
     752        1920 :                 hMdDec->spar_md.res_ind = get_next_indice( st0, 1 );
     753        1920 :                 hMdDec->spar_md.res_ind += hMdDec->spar_md_cfg.nchan_transport;
     754             :             }
     755          16 :             else if ( hMdDec->spar_md_cfg.nchan_transport == 3 )
     756             :             {
     757          16 :                 hMdDec->spar_md.res_ind = remix_order_set[hMdDec->spar_md_cfg.remix_unmix_order][hMdDec->spar_md_cfg.nchan_transport];
     758             :             }
     759             :         }
     760             :     }
     761             :     else
     762             :     {
     763     4828115 :         dyn_active_w_flag = 0;
     764     4828115 :         if ( hMdDec->spar_md_cfg.nchan_transport == FOA_CHANNELS )
     765             :         {
     766     2231451 :             get_next_indice( st0, 1 );
     767             :         }
     768             :     }
     769             : 
     770     8709329 :     ivas_spar_dec_parse_md_bs( hMdDec, st0, &nB, &bw, &dtx_vad, st_ivas->hDecoderConfig->ivas_total_brate,
     771     8709329 :                                st_ivas->hQMetaData->sba_inactive_mode );
     772             : 
     773     8709329 :     assert( nB == hMdDec->spar_md.num_bands );
     774     8709329 :     assert( bw == 1 );
     775     8709329 :     ivas_spar_md_fill_invalid_bandcoeffs(
     776             :         hMdDec->spar_md.band_coeffs,
     777             :         hMdDec->band_coeffs_prev,
     778     8709329 :         &hMdDec->valid_bands[0],
     779             :         &hMdDec->base_band_coeffs_age[0],
     780             :         &hMdDec->first_valid_frame,
     781             :         nB );
     782             : 
     783     8709329 :     ivas_dec_mono_sba_handling( st_ivas );
     784             : 
     785             :     /* SPAR to DirAC conversion */
     786     8709329 :     if ( hMdDec->spar_hoa_dirac2spar_md_flag == 1 )
     787             :     {
     788     8294019 :         ivas_spar_to_dirac( st_ivas, hMdDec, dtx_vad, num_bands_out, bw, dyn_active_w_flag );
     789             :     }
     790             : 
     791             :     /* set correct number of bands*/
     792     8709329 :     nB = IVAS_MAX_NUM_BANDS;
     793             : #ifdef DEBUG_LBR_SBA
     794             :     /* Dumping SPAR Coefficients */
     795             :     char f_name[100];
     796             :     int16_t nbands = 4; // 6 total, just looking at SPAR
     797             :     int16_t num_subframes = 1;
     798             :     int16_t num_elements = 6;
     799             :     int16_t num_block_group = 1;
     800             :     int16_t byte_size = sizeof( float );
     801             : 
     802             :     sprintf( f_name, "SBA_MD_values_dec.bin" );
     803             :     ( frame == 0 ) ? dbgwrite( &nbands, sizeof( nbands ), 1, 1, f_name ) : false;
     804             :     ( frame == 0 ) ? dbgwrite( &num_elements, sizeof( num_elements ), 1, 1, f_name ) : false;
     805             :     ( frame == 0 ) ? dbgwrite( &num_subframes, sizeof( num_subframes ), 1, 1, f_name ) : false;
     806             :     ( frame == 0 ) ? dbgwrite( &num_block_group, sizeof( num_block_group ), 1, 1, f_name ) : false;
     807             :     ( frame == 0 ) ? dbgwrite( &byte_size, sizeof( byte_size ), 1, 1, f_name ) : false;
     808             : 
     809             :     for ( b = 0; b < nbands; b++ )
     810             :     {
     811             :         for ( int16_t sf = 0; sf < num_subframes; sf++ )
     812             :         {
     813             :             for ( int16_t bl = 0; bl < num_block_group; bl++ )
     814             :             {
     815             :                 dbgwrite( &hMdDec->spar_md.band_coeffs[b].pred_re[0], byte_size, 1, 1, f_name );
     816             :                 dbgwrite( &hMdDec->spar_md.band_coeffs[b].pred_re[1], byte_size, 1, 1, f_name );
     817             :                 dbgwrite( &hMdDec->spar_md.band_coeffs[b].pred_re[2], byte_size, 1, 1, f_name );
     818             :                 dbgwrite( &hMdDec->spar_md.band_coeffs[b].P_re[0], byte_size, 1, 1, f_name );
     819             :                 dbgwrite( &hMdDec->spar_md.band_coeffs[b].P_re[1], byte_size, 1, 1, f_name );
     820             :                 dbgwrite( &hMdDec->spar_md.band_coeffs[b].P_re[2], byte_size, 1, 1, f_name );
     821             :                 // fprintf(stdout, "%f\t%f\t%f\t%d\t%d\n", dirac_md_kbps, spar_md_kbps, sba_md_kbps, qsi, code_strat );
     822             :             }
     823             :         }
     824             :     }
     825             : #endif
     826             : 
     827             :     /* expand DirAC MD to all time slots */
     828    31650119 :     for ( i_ts = 1; i_ts < num_md_sub_frames; i_ts++ )
     829             :     {
     830   213146538 :         for ( b = 0; b < hMdDec->spar_md.num_bands; b++ )
     831             :         {
     832  2092263228 :             for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
     833             :             {
     834  1902057480 :                 hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].pred_re[j] = hMdDec->spar_md.band_coeffs[b].pred_re[j];
     835             :             }
     836             : 
     837  1521645984 :             for ( j = 0; j < IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS; j++ )
     838             :             {
     839  5325760944 :                 for ( k = 0; k < IVAS_SPAR_MAX_DMX_CHS - 1; k++ )
     840             :                 {
     841  3994320708 :                     hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].C_re[j][k] = hMdDec->spar_md.band_coeffs[b].C_re[j][k];
     842             :                 }
     843             :             }
     844             : 
     845  2092263228 :             for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
     846             :             {
     847  1902057480 :                 hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].P_re[j] = hMdDec->spar_md.band_coeffs[b].P_re[j];
     848             :             }
     849             :         }
     850             :     }
     851             : 
     852     8709329 :     ivas_get_spar_matrices( hMdDec, num_bands_out, num_md_sub_frames, bw, dtx_vad, nB, num_md_chs, active_w_vlbr, dyn_active_w_flag );
     853             : 
     854             : #ifdef DEBUG_SPAR_DIRAC_WRITE_OUT_PRED_PARS
     855             :     {
     856             :         static FILE *fid = 0;
     857             :         int16_t band = 9;
     858             :         if ( !fid )
     859             :         {
     860             :             fid = fopen( "pred_coeffs_dec.txt", "wt" );
     861             :         }
     862             :         fprintf( fid, "%.6f\n", hMdDec->mixer_mat[1][0][band] );
     863             :     }
     864             : #endif
     865     8709329 :     ivas_spar_md_fill_invalid_bands( &hMdDec->spar_coeffs, &hMdDec->spar_coeffs_prev, &hMdDec->valid_bands[0], &hMdDec->base_band_age[0], num_bands_out, num_md_chs, num_md_sub_frames );
     866             : 
     867             : 
     868     8709329 :     hMdDec->dtx_md_smoothing_cntr = 1;
     869             : 
     870     8709329 :     return;
     871             : }
     872             : 
     873             : 
     874             : /*-----------------------------------------------------------------------------------------*
     875             :  * Function ivas_spar_chk_zero_coefs()
     876             :  *
     877             :  * Check for zeroed SPAR coefficients
     878             :  *-----------------------------------------------------------------------------------------*/
     879             : 
     880       41831 : int16_t ivas_spar_chk_zero_coefs(
     881             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder handle        */
     882             : )
     883             : {
     884             :     int16_t j, k, b;
     885             :     ivas_spar_md_dec_state_t *hMdDec;
     886       41831 :     int16_t mono = 1;
     887             :     int16_t ndec, ndm;
     888             : 
     889       41831 :     hMdDec = st_ivas->hSpar->hMdDec;
     890       41831 :     ndec = hMdDec->spar_md_cfg.num_decorr_per_band[0];
     891       41831 :     ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[0];
     892             : 
     893      376479 :     for ( b = 0; b < min( hMdDec->spar_md.num_bands, SPAR_DIRAC_SPLIT_START_BAND ); b++ )
     894             :     {
     895     1376608 :         for ( j = 0; j < ndm + ndec - 1; j++ )
     896             :         {
     897     1041960 :             if ( hMdDec->spar_md.band_coeffs[b].pred_re[j] != 0.0f )
     898             :             {
     899      565961 :                 mono = 0;
     900             :             }
     901             :         }
     902     1109792 :         for ( j = 0; j < ndec; j++ )
     903             :         {
     904     1020280 :             for ( k = 0; k < ndm - 1; k++ )
     905             :             {
     906      245136 :                 if ( hMdDec->spar_md.band_coeffs[b].C_re[j][k] != 0.0f )
     907             :                 {
     908       29161 :                     mono = 0;
     909             :                 }
     910             :             }
     911             :         }
     912     1109792 :         for ( j = 0; j < ndec; j++ )
     913             :         {
     914      775144 :             if ( hMdDec->spar_md.band_coeffs[b].P_re[j] != 0.0f )
     915             :             {
     916      639450 :                 mono = 0;
     917             :             }
     918             :         }
     919             :     }
     920             : 
     921       41831 :     return mono;
     922             : }
     923             : 
     924             : 
     925             : /*-----------------------------------------------------------------------------------------*
     926             :  * Function ivas_spar_smooth_md_dtx()
     927             :  *
     928             :  * Smooth MD during no data frame during DTX
     929             :  *-----------------------------------------------------------------------------------------*/
     930             : 
     931       15243 : void ivas_spar_smooth_md_dtx(
     932             :     ivas_spar_md_dec_state_t *hMdDec, /* i/o: SPAR MD decoder handle        */
     933             :     const int16_t num_bands_out,      /* i  : number of output bands        */
     934             :     const int16_t num_md_sub_frames   /* i  : number of metadata subframes  */
     935             : )
     936             : {
     937             :     int16_t j, k, b, dmx_ch;
     938             :     float ramp, tar, prev, new_val;
     939             : 
     940       15243 :     ramp = (float) hMdDec->dtx_md_smoothing_cntr / IVAS_DEFAULT_DTX_CNG_RAMP;
     941             : 
     942      194391 :     for ( b = 0; b < num_bands_out; b++ )
     943             :     {
     944      179148 :         dmx_ch = hMdDec->spar_md_cfg.num_dmx_chans_per_band[b];
     945             : 
     946      716592 :         for ( j = 1; j < FOA_CHANNELS; j++ )
     947             :         {
     948     1961064 :             for ( k = dmx_ch; k < FOA_CHANNELS; k++ )
     949             :             {
     950     1423620 :                 prev = hMdDec->spar_coeffs_prev.P_re[j][k][b];
     951     1423620 :                 tar = hMdDec->spar_coeffs_tar.P_re[j][k][b];
     952     1423620 :                 new_val = prev + ( ramp * ( tar - prev ) );
     953     1423620 :                 hMdDec->spar_coeffs.P_re[j][k][b] = new_val;
     954             :             }
     955             :         }
     956             : 
     957      895740 :         for ( j = 0; j < FOA_CHANNELS; j++ )
     958             :         {
     959     1684800 :             for ( k = 0; k < dmx_ch; k++ )
     960             :             {
     961      968208 :                 prev = hMdDec->spar_coeffs_prev.C_re[j][k][b];
     962      968208 :                 tar = hMdDec->spar_coeffs_tar.C_re[j][k][b];
     963      968208 :                 new_val = prev + ( ramp * ( tar - prev ) );
     964      968208 :                 hMdDec->spar_coeffs.C_re[j][k][b] = new_val;
     965             :             }
     966             :         }
     967             :     }
     968             : 
     969             :     /* expand MD to all time slots */
     970       58716 :     for ( int16_t i_ts = 1; i_ts < num_md_sub_frames; i_ts++ )
     971             :     {
     972      553845 :         for ( b = 0; b < num_bands_out; b++ )
     973             :         {
     974      510372 :             dmx_ch = hMdDec->spar_md_cfg.num_dmx_chans_per_band[b];
     975             : 
     976     2041488 :             for ( j = 1; j < FOA_CHANNELS; j++ )
     977             :             {
     978     5558328 :                 for ( k = dmx_ch; k < FOA_CHANNELS; k++ )
     979             : 
     980             :                 {
     981     4027212 :                     hMdDec->spar_coeffs.P_re[j][k][b + i_ts * IVAS_MAX_NUM_BANDS] = hMdDec->spar_coeffs.P_re[j][k][b];
     982             :                 }
     983             :             }
     984             : 
     985     2551860 :             for ( j = 0; j < FOA_CHANNELS; j++ )
     986             :             {
     987     4837824 :                 for ( k = 0; k < dmx_ch; k++ )
     988             :                 {
     989     2796336 :                     hMdDec->spar_coeffs.C_re[j][k][b + i_ts * IVAS_MAX_NUM_BANDS] = hMdDec->spar_coeffs.C_re[j][k][b];
     990             :                 }
     991             :             }
     992             :         }
     993             :     }
     994             : 
     995       15243 :     hMdDec->dtx_md_smoothing_cntr = min( hMdDec->dtx_md_smoothing_cntr + 1, IVAS_DEFAULT_DTX_CNG_RAMP );
     996             : 
     997       15243 :     return;
     998             : }
     999             : 
    1000             : 
    1001             : /*-----------------------------------------------------------------------------------------*
    1002             :  * Function ivas_spar_setup_md_smoothing()
    1003             :  *
    1004             :  * Set up smoothing of SPAR MD when SID update frame is received
    1005             :  *-----------------------------------------------------------------------------------------*/
    1006             : 
    1007         840 : void ivas_spar_setup_md_smoothing(
    1008             :     ivas_spar_md_dec_state_t *hMdDec, /* i/o: SPAR MD decoder handle        */
    1009             :     const int16_t num_bands_out,      /* i  : number of output bands        */
    1010             :     const int16_t num_md_sub_frames   /* i  : number of metadata subframes  */
    1011             : )
    1012             : {
    1013             :     /* copy the coeffs */
    1014             :     int16_t num_channels, i, j, k;
    1015             : 
    1016         840 :     num_channels = hMdDec->spar_md_cfg.num_umx_chs;
    1017             : 
    1018        4200 :     for ( i = 0; i < num_channels; i++ )
    1019             :     {
    1020       16800 :         for ( j = 0; j < num_channels; j++ )
    1021             :         {
    1022      174720 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1023             :             {
    1024      161280 :                 hMdDec->spar_coeffs_prev.C_re[i][j][k] = hMdDec->spar_coeffs_tar.C_re[i][j][k];
    1025             :             }
    1026             :         }
    1027             :     }
    1028             : 
    1029        4200 :     for ( i = 0; i < num_channels; i++ )
    1030             :     {
    1031       16800 :         for ( j = 0; j < num_channels; j++ )
    1032             :         {
    1033      174720 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1034             :             {
    1035      161280 :                 hMdDec->spar_coeffs_prev.P_re[i][j][k] = hMdDec->spar_coeffs_tar.P_re[i][j][k];
    1036             :             }
    1037             :         }
    1038             :     }
    1039             : 
    1040        4200 :     for ( i = 0; i < num_channels; i++ )
    1041             :     {
    1042       16800 :         for ( j = 0; j < num_channels; j++ )
    1043             :         {
    1044      174720 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1045             :             {
    1046      161280 :                 hMdDec->spar_coeffs_tar.C_re[i][j][k] = hMdDec->spar_coeffs.C_re[i][j][k];
    1047             :             }
    1048             :         }
    1049             :     }
    1050             : 
    1051        4200 :     for ( i = 0; i < num_channels; i++ )
    1052             :     {
    1053       16800 :         for ( j = 0; j < num_channels; j++ )
    1054             :         {
    1055      174720 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1056             :             {
    1057      161280 :                 hMdDec->spar_coeffs_tar.P_re[i][j][k] = hMdDec->spar_coeffs.P_re[i][j][k];
    1058             :             }
    1059             :         }
    1060             :     }
    1061             : 
    1062         840 :     ivas_spar_smooth_md_dtx( hMdDec, num_bands_out, num_md_sub_frames );
    1063             : 
    1064         840 :     return;
    1065             : }
    1066             : 
    1067             : 
    1068             : /*-----------------------------------------------------------------------------------------*
    1069             :  * Function ivas_spar_update_md_hist()
    1070             :  *
    1071             :  * Update previous and target MD
    1072             :  *-----------------------------------------------------------------------------------------*/
    1073             : 
    1074     8708489 : void ivas_spar_update_md_hist(
    1075             :     ivas_spar_md_dec_state_t *hMdDec /* i/o: SPAR MD decoder handle */
    1076             : )
    1077             : {
    1078             :     int16_t num_channels, i, j, k;
    1079             : 
    1080     8708489 :     num_channels = hMdDec->spar_md_cfg.num_umx_chs;
    1081             : 
    1082    47934329 :     for ( i = 0; i < num_channels; i++ )
    1083             :     {
    1084   237150446 :         for ( j = 0; j < num_channels; j++ )
    1085             :         {
    1086  2573019878 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1087             :             {
    1088  2375095272 :                 hMdDec->spar_coeffs_prev.C_re[i][j][k] = hMdDec->spar_coeffs.C_re[i][j][k];
    1089             :             }
    1090             :         }
    1091             :     }
    1092             : 
    1093    47934329 :     for ( i = 0; i < num_channels; i++ )
    1094             :     {
    1095   237150446 :         for ( j = 0; j < num_channels; j++ )
    1096             :         {
    1097  2573019878 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1098             :             {
    1099  2375095272 :                 hMdDec->spar_coeffs_prev.P_re[i][j][k] = hMdDec->spar_coeffs.P_re[i][j][k];
    1100             :             }
    1101             :         }
    1102             :     }
    1103             : 
    1104    47934329 :     for ( i = 0; i < num_channels; i++ )
    1105             :     {
    1106   237150446 :         for ( j = 0; j < num_channels; j++ )
    1107             :         {
    1108  2573019878 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1109             :             {
    1110  2375095272 :                 hMdDec->spar_coeffs_tar.C_re[i][j][k] = hMdDec->spar_coeffs.C_re[i][j][k];
    1111             :             }
    1112             :         }
    1113             :     }
    1114             : 
    1115    47934329 :     for ( i = 0; i < num_channels; i++ )
    1116             :     {
    1117   237150446 :         for ( j = 0; j < num_channels; j++ )
    1118             :         {
    1119  2573019878 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1120             :             {
    1121  2375095272 :                 hMdDec->spar_coeffs_tar.P_re[i][j][k] = hMdDec->spar_coeffs.P_re[i][j][k];
    1122             :             }
    1123             :         }
    1124             :     }
    1125             : 
    1126     8708489 :     return;
    1127             : }
    1128             : 
    1129             : 
    1130             : /*-----------------------------------------------------------------------------------------*
    1131             :  * Function ivas_get_spar_matrices()
    1132             :  *
    1133             :  * Get SPAR matrices
    1134             :  *-----------------------------------------------------------------------------------------*/
    1135             : 
    1136     8709329 : static void ivas_get_spar_matrices(
    1137             :     ivas_spar_md_dec_state_t *hMdDec,
    1138             :     const int16_t num_bands_out,
    1139             :     const int16_t n_ts,
    1140             :     const int16_t bw,
    1141             :     const int16_t dtx_vad,
    1142             :     const int16_t nB,
    1143             :     const int16_t numch_out,
    1144             :     const int16_t active_w_vlbr,
    1145             :     const int16_t dyn_active_w_flag )
    1146             : {
    1147             :     int16_t num_bands, dmx_ch, split_band;
    1148             :     int16_t i, j, k, m, b, i_ts, active_w;
    1149             :     const int16_t *order;
    1150             :     float active_w_dm_fac, re;
    1151             : 
    1152             : 
    1153     8709329 :     num_bands = num_bands_out;
    1154     8709329 :     order = remix_order_set[hMdDec->spar_md_cfg.remix_unmix_order];
    1155             : 
    1156     8709329 :     split_band = SPAR_DIRAC_SPLIT_START_BAND;
    1157     8709329 :     if ( split_band >= IVAS_MAX_NUM_BANDS )
    1158             :     {
    1159             :         /*store previous 4x4 parameters for linear interpolation to current*/
    1160           0 :         for ( i = 0; i < numch_out; i++ )
    1161             :         {
    1162           0 :             for ( j = 0; j < numch_out; j++ )
    1163             :             {
    1164           0 :                 for ( b = 0; b < num_bands; b++ )
    1165             :                 {
    1166           0 :                     hMdDec->mixer_mat_prev[0][i][j][b] = hMdDec->mixer_mat[i][j][b];
    1167             :                 }
    1168             :             }
    1169             :         }
    1170             :     }
    1171             : 
    1172     8709329 :     if ( bw == IVAS_RED_BAND_FACT )
    1173             :     {
    1174           0 :         num_bands = num_bands >> 1;
    1175             :     }
    1176             : 
    1177     8709329 :     active_w = ( dyn_active_w_flag == 1 ) || ( hMdDec->spar_md_cfg.active_w == 1 );
    1178     8709329 :     active_w_dm_fac = ( dtx_vad == 0 ) ? IVAS_ACTIVEW_DM_F_SCALE_DTX : ( ( active_w_vlbr ) ? IVAS_ACTIVEW_DM_F_SCALE_VLBR : IVAS_ACTIVEW_DM_F_SCALE );
    1179             : 
    1180    40359448 :     for ( i_ts = 0; i_ts < n_ts; i_ts++ )
    1181             :     {
    1182   167772527 :         for ( i = 0; i < numch_out; i++ )
    1183             :         {
    1184   759574910 :             for ( j = 0; j < numch_out; j++ )
    1185             :             {
    1186   623452502 :                 set_zero( &hMdDec->spar_coeffs.C_re[i][j][i_ts * IVAS_MAX_NUM_BANDS], IVAS_MAX_NUM_BANDS );
    1187   623452502 :                 set_zero( &hMdDec->spar_coeffs.P_re[i][j][i_ts * IVAS_MAX_NUM_BANDS], IVAS_MAX_NUM_BANDS );
    1188             :             }
    1189             :         }
    1190    31650119 :         num_bands = min( num_bands, nB );
    1191             : 
    1192   400306313 :         for ( b = 0; b < num_bands; b++ )
    1193             :         {
    1194             :             float tmp_C1_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
    1195             :             float tmp_C2_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
    1196             :             float tmp_dm_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
    1197             : 
    1198   368656194 :             dmx_ch = hMdDec->spar_md_cfg.num_dmx_chans_per_band[bw * b];
    1199             : 
    1200  1955110586 :             for ( j = 0; j < numch_out; j++ )
    1201             :             {
    1202  1586454392 :                 set_zero( tmp_C1_re[j], numch_out );
    1203  1586454392 :                 set_zero( tmp_C2_re[j], numch_out );
    1204  1586454392 :                 set_zero( tmp_dm_re[j], numch_out );
    1205             : 
    1206  1586454392 :                 tmp_C1_re[j][j] = 1.0f;
    1207  1586454392 :                 tmp_C2_re[j][j] = 1.0f;
    1208  1586454392 :                 tmp_dm_re[j][j] = 1.0f;
    1209             :             }
    1210             : 
    1211  1586454392 :             for ( j = 1; j < numch_out; j++ )
    1212             :             {
    1213  1217798198 :                 tmp_C1_re[j][0] = hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].pred_re[j - 1];
    1214             :             }
    1215             : 
    1216   368656194 :             if ( active_w == 1 )
    1217             :             {
    1218   390670920 :                 for ( j = 1; j < numch_out; j++ )
    1219             :                 {
    1220   293003190 :                     tmp_C2_re[0][j] = active_w_dm_fac * -hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].pred_re[j - 1];
    1221             :                 }
    1222             : 
    1223    97667730 :                 IVAS_RMULT_FLOAT( tmp_C2_re[0][1], tmp_C1_re[1][0], re );
    1224    97667730 :                 tmp_dm_re[0][0] = 1 + re;
    1225             : 
    1226    97667730 :                 IVAS_RMULT_FLOAT( tmp_C2_re[0][2], tmp_C1_re[2][0], re );
    1227    97667730 :                 tmp_dm_re[0][0] += re;
    1228             : 
    1229    97667730 :                 IVAS_RMULT_FLOAT( tmp_C2_re[0][3], tmp_C1_re[3][0], re );
    1230    97667730 :                 tmp_dm_re[0][0] += re;
    1231             : 
    1232    97667730 :                 if ( dyn_active_w_flag == 1 )
    1233             :                 {
    1234       92928 :                     tmp_dm_re[0][0] *= IVAS_SPAR_DYN_ACTIVEW_THRESH;
    1235             :                 }
    1236             : 
    1237    97667730 :                 tmp_dm_re[0][1] = tmp_C2_re[0][1];
    1238             : 
    1239    97667730 :                 tmp_dm_re[0][2] = tmp_C2_re[0][2];
    1240             : 
    1241    97667730 :                 tmp_dm_re[0][3] = tmp_C2_re[0][3];
    1242             : 
    1243    97667730 :                 tmp_dm_re[1][0] = tmp_C1_re[1][0];
    1244             : 
    1245    97667730 :                 tmp_dm_re[2][0] = tmp_C1_re[2][0];
    1246             : 
    1247    97667730 :                 tmp_dm_re[3][0] = tmp_C1_re[3][0];
    1248             : 
    1249    97667730 :                 if ( hMdDec->spar_md_cfg.remix_unmix_order != 3 )
    1250             :                 {
    1251    97667730 :                     ivas_mat_col_rearrange( tmp_dm_re, order, i_ts, hMdDec->mixer_mat, b, numch_out );
    1252             :                 }
    1253             :             }
    1254             :             else
    1255             :             {
    1256   270988464 :                 if ( hMdDec->spar_md_cfg.remix_unmix_order != 3 )
    1257             :                 {
    1258   270988464 :                     ivas_mat_col_rearrange( tmp_C1_re, order, i_ts, hMdDec->mixer_mat, b, numch_out );
    1259             :                 }
    1260             :             }
    1261             : 
    1262   368656194 :             if ( dmx_ch > 0 )
    1263             :             {
    1264             :                 float tmpC_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
    1265             :                 float tmpP_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
    1266             : 
    1267  1955110586 :                 for ( j = 0; j < numch_out; j++ )
    1268             :                 {
    1269  1586454392 :                     set_zero( tmpC_re[j], numch_out );
    1270  1586454392 :                     set_zero( tmpP_re[j], numch_out );
    1271             :                 }
    1272             : 
    1273  1955110586 :                 for ( j = 0; j < numch_out; j++ )
    1274             :                 {
    1275  1586454392 :                     set_zero( tmpC_re[j], numch_out );
    1276             :                 }
    1277             : 
    1278  1266607132 :                 for ( k = 0; k < dmx_ch; k++ )
    1279             :                 {
    1280   897950938 :                     tmpC_re[k][k] = 1;
    1281             :                 }
    1282             : 
    1283  1057159648 :                 for ( j = dmx_ch; j < numch_out; j++ )
    1284             :                 {
    1285  1386844110 :                     for ( k = 1; k < dmx_ch; k++ )
    1286             :                     {
    1287   698340656 :                         tmpC_re[j][k] = hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].C_re[j - dmx_ch][k - 1];
    1288             :                     }
    1289             :                 }
    1290             : 
    1291  1057159648 :                 for ( j = dmx_ch; j < numch_out; j++ )
    1292             :                 {
    1293  2537574848 :                     for ( k = dmx_ch; k < numch_out; k++ )
    1294             :                     {
    1295  1849071394 :                         if ( ( j - dmx_ch ) == ( k - dmx_ch ) )
    1296             :                         {
    1297   688503454 :                             tmpP_re[j][k] = hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].P_re[k - dmx_ch];
    1298             :                         }
    1299             :                         else
    1300             :                         {
    1301  1160567940 :                             tmpP_re[j][k] = 0;
    1302             :                         }
    1303             :                     }
    1304             :                 }
    1305             : 
    1306  1586454392 :                 for ( j = 1; j < numch_out; j++ )
    1307             :                 {
    1308  3765210248 :                     for ( k = dmx_ch; k < numch_out; k++ )
    1309             :                     {
    1310 16577340434 :                         for ( m = 0; m < numch_out; m++ )
    1311             :                         {
    1312 14029928384 :                             IVAS_RMULT_FLOAT( hMdDec->mixer_mat[j][m][b + i_ts * IVAS_MAX_NUM_BANDS], tmpP_re[m][k], re );
    1313 14029928384 :                             hMdDec->spar_coeffs.P_re[j][k][( b * bw ) + i_ts * IVAS_MAX_NUM_BANDS] += re;
    1314             :                         }
    1315             :                     }
    1316             :                 }
    1317             : 
    1318  1955110586 :                 for ( j = 0; j < numch_out; j++ )
    1319             :                 {
    1320  5625576608 :                     for ( k = 0; k < dmx_ch; k++ )
    1321             :                     {
    1322 23912491688 :                         for ( m = 0; m < numch_out; m++ )
    1323             :                         {
    1324 19873369472 :                             IVAS_RMULT_FLOAT( hMdDec->mixer_mat[j][m][b + i_ts * IVAS_MAX_NUM_BANDS], tmpC_re[m][k], re );
    1325 19873369472 :                             hMdDec->spar_coeffs.C_re[j][k][( b * bw ) + i_ts * IVAS_MAX_NUM_BANDS] += re;
    1326             :                         }
    1327             :                     }
    1328             :                 }
    1329             : 
    1330   368656194 :                 hMdDec->spar_coeffs.C_re[0][0][( b * bw ) + i_ts * IVAS_MAX_NUM_BANDS] =
    1331   368656194 :                     max( 0, hMdDec->spar_coeffs.C_re[0][0][( b * bw ) + i_ts * IVAS_MAX_NUM_BANDS] );
    1332             :             }
    1333             :         }
    1334             : 
    1335             :         /* band mixing */
    1336    31650119 :         if ( bw == IVAS_RED_BAND_FACT )
    1337             :         {
    1338           0 :             for ( b = 0; b < num_bands_out; b = b + bw )
    1339             :             {
    1340           0 :                 dmx_ch = hMdDec->spar_md_cfg.num_dmx_chans_per_band[b];
    1341           0 :                 for ( j = 0; j < numch_out; j++ )
    1342             :                 {
    1343           0 :                     for ( k = dmx_ch; k < numch_out; k++ )
    1344             :                     {
    1345           0 :                         hMdDec->spar_coeffs.P_re[j][k][( b + 1 ) + i_ts * IVAS_MAX_NUM_BANDS] = hMdDec->spar_coeffs.P_re[j][k][b + i_ts * IVAS_MAX_NUM_BANDS];
    1346             :                     }
    1347             :                 }
    1348             : 
    1349           0 :                 for ( j = 0; j < numch_out; j++ )
    1350             :                 {
    1351           0 :                     for ( k = 0; k < dmx_ch; k++ )
    1352             :                     {
    1353           0 :                         hMdDec->spar_coeffs.C_re[j][k][( b + 1 ) + i_ts * IVAS_MAX_NUM_BANDS] = hMdDec->spar_coeffs.C_re[j][k][b + i_ts * IVAS_MAX_NUM_BANDS];
    1354             :                     }
    1355             :                 }
    1356             :             }
    1357             :         }
    1358             :     }
    1359             : 
    1360     8709329 :     return;
    1361             : }
    1362             : 
    1363             : 
    1364             : /*-----------------------------------------------------------------------------------------*
    1365             :  * Function ivas_mat_col_rearrange()
    1366             :  *
    1367             :  * reorders the input matrix based on order
    1368             :  *-----------------------------------------------------------------------------------------*/
    1369             : 
    1370   368656194 : static void ivas_mat_col_rearrange(
    1371             :     float in_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH],
    1372             :     const int16_t order[IVAS_SPAR_MAX_CH],
    1373             :     const int16_t i_ts,
    1374             :     float ***mixer_mat,
    1375             :     const int16_t bands,
    1376             :     const int16_t num_ch )
    1377             : {
    1378             :     int16_t i, j, idx;
    1379             : 
    1380  1955110586 :     for ( i = 0; i < num_ch; i++ )
    1381             :     {
    1382  1586454392 :         idx = order[i];
    1383             : 
    1384  8861492112 :         for ( j = 0; j < num_ch; j++ )
    1385             :         {
    1386  7275037720 :             mixer_mat[j][i][bands + i_ts * IVAS_MAX_NUM_BANDS] = in_re[j][idx];
    1387             :         }
    1388             :     }
    1389             : 
    1390   368656194 :     return;
    1391             : }
    1392             : 
    1393             : 
    1394             : /*-----------------------------------------------------------------------------------------*
    1395             :  * Function ivas_spar_dec_gen_umx_mat()
    1396             :  *
    1397             :  * generates upmix matrix
    1398             :  *-----------------------------------------------------------------------------------------*/
    1399             : 
    1400    10936779 : void ivas_spar_dec_gen_umx_mat(
    1401             :     ivas_spar_md_dec_state_t *hMdDec, /* i/o: SPAR MD decoder handle        */
    1402             :     const int16_t nchan_transport,    /* i  : number of transport channels  */
    1403             :     const int16_t num_bands_out,      /* i  : number of output bands        */
    1404             :     const int16_t bfi,                /* i  : bad frame indicator           */
    1405             :     const int16_t num_md_sub_frames )
    1406             : {
    1407             :     int16_t i, j, b, i_ts, num_out_ch;
    1408             : 
    1409    10936779 :     num_out_ch = hMdDec->spar_md_cfg.num_umx_chs;
    1410             : 
    1411    50746791 :     for ( i_ts = 0; i_ts < num_md_sub_frames; i_ts++ )
    1412             :     {
    1413    39810012 :         if ( hMdDec->td_decorr_flag == 1 )
    1414             :         {
    1415   208430183 :             for ( i = 0; i < num_out_ch; i++ )
    1416             :             {
    1417   568602235 :                 for ( j = 0; j < nchan_transport; j++ )
    1418             :                 {
    1419  5074742064 :                     for ( b = 0; b < num_bands_out; b++ )
    1420             :                     {
    1421  4674760000 :                         hMdDec->mixer_mat[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] = hMdDec->spar_coeffs.C_re[i][j][b + i_ts * IVAS_MAX_NUM_BANDS];
    1422             :                     }
    1423             :                 }
    1424             :             }
    1425             : 
    1426   208430183 :             for ( i = 0; i < num_out_ch; i++ )
    1427             :             {
    1428   521426086 :                 for ( j = nchan_transport; j < num_out_ch; j++ )
    1429             :                 {
    1430  4470938559 :                     for ( b = 0; b < num_bands_out; b++ )
    1431             :                     {
    1432  4118132644 :                         hMdDec->mixer_mat[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] = hMdDec->spar_coeffs.P_re[i][j][b + i_ts * IVAS_MAX_NUM_BANDS];
    1433             :                     }
    1434             :                 }
    1435             :             }
    1436             :         }
    1437             :         else
    1438             :         {
    1439           0 :             for ( i = 0; i < num_out_ch; i++ )
    1440             :             {
    1441           0 :                 for ( j = 0; j < nchan_transport; j++ )
    1442             :                 {
    1443           0 :                     for ( b = 0; b < num_bands_out; b++ )
    1444             :                     {
    1445           0 :                         hMdDec->mixer_mat[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] = hMdDec->spar_coeffs.C_re[i][j][b + i_ts * IVAS_MAX_NUM_BANDS];
    1446             :                     }
    1447             :                 }
    1448             :             }
    1449             :         }
    1450             : 
    1451             : #ifdef DEBUG_SBA_MD_DUMP
    1452             :         {
    1453             :             static FILE *f_mat = 0;
    1454             : 
    1455             :             if ( f_mat == 0 )
    1456             :                 f_mat = fopen( "mixer_mat_dec", "w" );
    1457             : 
    1458             :             for ( i = 0; i < num_out_ch; i++ )
    1459             :             {
    1460             :                 for ( j = 0; j < num_out_ch; j++ )
    1461             :                 {
    1462             :                     for ( b = 0; b < num_bands_out; b++ )
    1463             :                     {
    1464             :                         fprintf( f_mat, "%f\n", hMdDec->mixer_mat[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] );
    1465             :                     }
    1466             :                 }
    1467             :             }
    1468             :         }
    1469             : #endif
    1470             :     }
    1471             : 
    1472    10936779 :     ivas_spar_dec_compute_ramp_down_post_matrix( hMdDec, num_bands_out, bfi, num_md_sub_frames );
    1473             : 
    1474    10936779 :     return;
    1475             : }
    1476             : 
    1477      661754 : static void ivas_spar_md_band_upmix(
    1478             :     ivas_band_coeffs_t *band_coeffs,
    1479             :     int16_t *nB,
    1480             :     int16_t *bands_bw,
    1481             :     int16_t *valid_bands,
    1482             :     int16_t bw_final,
    1483             :     int16_t ndec,
    1484             :     int16_t ndm )
    1485             : {
    1486             :     int16_t i, ii, jj, b, idx, bw_fact;
    1487             : 
    1488      661754 :     bw_fact = *bands_bw / bw_final;
    1489     3277890 :     for ( i = *nB - 1; i >= 0; i-- )
    1490             :     {
    1491             : 
    1492     7910168 :         for ( b = bw_fact - 1; b >= 0; b-- )
    1493             :         {
    1494     5294032 :             idx = i * bw_fact + b;
    1495    21176128 :             for ( ii = 0; ii < ndec + ndm - 1; ii++ )
    1496             :             {
    1497    15882096 :                 band_coeffs[idx].pred_re[ii] = band_coeffs[i].pred_re[ii];
    1498             :             }
    1499    21115960 :             for ( ii = 0; ii < ndec; ii++ )
    1500             :             {
    1501    15942264 :                 for ( jj = 0; jj < ndm - 1; jj++ )
    1502             :                 {
    1503      120336 :                     band_coeffs[idx].C_re[ii][jj] = band_coeffs[i].C_re[ii][jj];
    1504             :                 }
    1505             :             }
    1506    21115960 :             for ( jj = 0; jj < ndec; jj++ )
    1507             :             {
    1508    15821928 :                 band_coeffs[idx].P_re[jj] = band_coeffs[i].P_re[jj];
    1509             :             }
    1510     5294032 :             valid_bands[idx] = valid_bands[i];
    1511             :         }
    1512             :     }
    1513      661754 :     *nB = ( *nB ) * ( *bands_bw ) / bw_final;
    1514      661754 :     *bands_bw = bw_final;
    1515             : 
    1516      661754 :     return;
    1517             : }
    1518             : 
    1519             : /*-----------------------------------------------------------------------------------------*
    1520             :  * Function ivas_spar_dec_parse_md_bs()
    1521             :  *
    1522             :  * Parse SPAR MD bitstream
    1523             :  *-----------------------------------------------------------------------------------------*/
    1524             : 
    1525     8709329 : static void ivas_spar_dec_parse_md_bs(
    1526             :     ivas_spar_md_dec_state_t *hMdDec,
    1527             :     Decoder_State *st0,
    1528             :     int16_t *nB,
    1529             :     int16_t *bands_bw,
    1530             :     int16_t *dtx_vad,
    1531             :     const int32_t ivas_total_brate,
    1532             :     const int16_t sba_inactive_mode )
    1533             : {
    1534             :     int16_t i, j, k, num_bands;
    1535             :     int16_t ii, jj, ndec, ndm;
    1536             :     uint16_t qsi;
    1537             :     ivas_quant_strat_t qs;
    1538             :     int16_t strat, no_ec;
    1539             :     int16_t do_diff[IVAS_MAX_NUM_BANDS];
    1540             :     float quant[IVAS_SPAR_MAX_C_COEFF];
    1541             :     int16_t do_repeat[IVAS_MAX_NUM_BANDS];
    1542     8709329 :     *dtx_vad = 1;
    1543     8709329 :     *bands_bw = 1;
    1544     8709329 :     qsi = 0;
    1545     8709329 :     num_bands = hMdDec->spar_md.num_bands;
    1546             : 
    1547     8709329 :     if ( ivas_total_brate > IVAS_SID_5k2 )
    1548             :     {
    1549     8706425 :         if ( hMdDec->spar_md_cfg.quant_strat_bits > 0 )
    1550             :         {
    1551     8706425 :             if ( ivas_total_brate >= BRATE_SPAR_Q_STRAT )
    1552             :             {
    1553             :                 /*only one bit written for quantization strategy to indicate either a fixed quantization strategy or dtx_vad==0 */
    1554     2231451 :                 qsi = get_next_indice( st0, 1 );
    1555     2231451 :                 if ( qsi == 1 )
    1556             :                 {
    1557           0 :                     *dtx_vad = 0;
    1558             :                 }
    1559             :             }
    1560             :             else
    1561             :             {
    1562     6474974 :                 if ( sba_inactive_mode == 1 )
    1563             :                 {
    1564       12536 :                     *dtx_vad = 0;
    1565       12536 :                     qsi = hMdDec->spar_md_cfg.quant_strat_bits + 1;
    1566             :                 }
    1567             :                 else
    1568             :                 {
    1569     6462438 :                     qsi = get_next_indice( st0, hMdDec->spar_md_cfg.quant_strat_bits );
    1570             :                 }
    1571             :             }
    1572             :         }
    1573             :         else
    1574             :         {
    1575           0 :             qsi = 0;
    1576             :         }
    1577             :     }
    1578             :     else
    1579             :     {
    1580        2904 :         *dtx_vad = 0;
    1581             :     }
    1582             : 
    1583     8709329 :     hMdDec->dtx_vad = *dtx_vad;
    1584             : 
    1585     8709329 :     if ( *dtx_vad == 0 )
    1586             :     {
    1587       15440 :         *nB = SPAR_DTX_BANDS;
    1588       15440 :         *bands_bw = num_bands / *nB;
    1589             : 
    1590       46320 :         for ( i = 0; i < *nB; i++ )
    1591             :         {
    1592      339680 :             for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
    1593             :             {
    1594      308800 :                 hMdDec->spar_md.band_coeffs[i].pred_re[j] = 0;
    1595      308800 :                 hMdDec->spar_md.band_coeffs[i].P_re[j] = 0;
    1596             :             }
    1597       30880 :             hMdDec->valid_bands[i] = 1;
    1598             :         }
    1599             : 
    1600      138960 :         for ( i = 0; i < num_bands; i++ )
    1601             :         {
    1602      988160 :             for ( j = 0; j < ( IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS ); j++ )
    1603             :             {
    1604     3458560 :                 for ( k = 0; k < ( IVAS_SPAR_MAX_DMX_CHS - 1 ); k++ )
    1605             :                 {
    1606     2593920 :                     hMdDec->spar_md.band_coeffs[i].C_re[j][k] = 0;
    1607             :                 }
    1608             :             }
    1609             :         }
    1610             : 
    1611       15440 :         ivas_parse_parameter_bitstream_dtx( &hMdDec->spar_md, st0, *bands_bw, *nB, hMdDec->spar_md_cfg.num_dmx_chans_per_band, hMdDec->spar_md_cfg.num_decorr_per_band );
    1612             : 
    1613       15440 :         if ( *bands_bw != 1 )
    1614             :         {
    1615       15440 :             ndec = hMdDec->spar_md_cfg.num_decorr_per_band[0];
    1616       15440 :             ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[0];
    1617       15440 :             ivas_spar_md_band_upmix(
    1618             :                 hMdDec->spar_md.band_coeffs,
    1619             :                 nB,
    1620             :                 bands_bw,
    1621       15440 :                 hMdDec->valid_bands,
    1622             :                 1,
    1623             :                 ndec,
    1624             :                 ndm );
    1625             :         }
    1626             : 
    1627       15440 :         return;
    1628             :     }
    1629             : 
    1630     8693889 :     qs = hMdDec->spar_md_cfg.quant_strat[qsi];
    1631             : 
    1632     8693889 :     strat = get_next_indice( st0, 3 );
    1633             : 
    1634     8693889 :     no_ec = 0;
    1635             : 
    1636     8693889 :     if ( strat < 2 )
    1637             :     {
    1638     2928840 :         *bands_bw = strat + 1;
    1639     2928840 :         *nB = num_bands / *bands_bw;
    1640    30197528 :         for ( i = 0; i < *nB; i++ )
    1641             :         {
    1642    27268688 :             do_diff[i] = 0;
    1643    27268688 :             do_repeat[i] = 0;
    1644             :         }
    1645             :     }
    1646     5765049 :     else if ( strat < 4 )
    1647             :     {
    1648      206588 :         *bands_bw = strat - 1;
    1649      206588 :         *nB = num_bands / *bands_bw;
    1650     1829812 :         for ( i = 0; i < *nB; i++ )
    1651             :         {
    1652     1623224 :             do_diff[i] = 0;
    1653     1623224 :             do_repeat[i] = 0;
    1654             :         }
    1655      206588 :         no_ec = 1;
    1656             :     }
    1657     5558461 :     else if ( ivas_total_brate < IVAS_24k4 )
    1658             :     {
    1659      626507 :         *bands_bw = 2;
    1660      626507 :         *nB = num_bands / *bands_bw;
    1661             : 
    1662     3132535 :         for ( i = 0; i < *nB; i++ )
    1663             :         {
    1664     2506028 :             do_diff[i] = 0;
    1665     2506028 :             do_repeat[i] = ( ( strat % 2 ) == ( ( i + 1 ) % 2 ) );
    1666             :         }
    1667             :     }
    1668             :     else
    1669             :     {
    1670     4931954 :         *bands_bw = 1;
    1671     4931954 :         *nB = num_bands;
    1672             : 
    1673    44387586 :         for ( i = 0; i < *nB; i++ )
    1674             :         {
    1675    39455632 :             do_diff[i] = ( ( ( i + 1 ) & 3 ) != strat - 4 );
    1676    39455632 :             do_repeat[i] = 0;
    1677             :         }
    1678     4931954 :         if ( hMdDec->spar_md_cfg.prev_quant_idx >= 0 )
    1679             :         {
    1680     4931377 :             ivas_map_prior_coeffs_quant( &hMdDec->spar_md_prev, &hMdDec->spar_md_cfg, qsi, *nB );
    1681             :         }
    1682             :     }
    1683             : #ifdef SPAR_HOA_DBG
    1684             :     fprintf( stdout, "\n\n no_ec = %d, strat = %d\n", no_ec, strat );
    1685             : #endif
    1686     8693889 :     hMdDec->spar_md_cfg.prev_quant_idx = qsi;
    1687             : 
    1688     8693889 :     if ( no_ec == 0 )
    1689             :     {
    1690     8487301 :         ivas_decode_arith_bs( hMdDec, st0, qsi, *nB, *bands_bw, do_diff, strat, ivas_total_brate );
    1691             :     }
    1692             :     else
    1693             :     {
    1694      206588 :         ivas_decode_huffman_bs( hMdDec, st0, qsi, *nB, *bands_bw );
    1695             :     }
    1696             : 
    1697    79547461 :     for ( i = 0; i < *nB; i++ )
    1698             :     {
    1699    70853572 :         ndec = hMdDec->spar_md_cfg.num_decorr_per_band[( *bands_bw ) * i];
    1700    70853572 :         ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[( *bands_bw ) * i];
    1701             : 
    1702    70853572 :         ivas_deindex_real_index( hMdDec->spar_md.band_coeffs_idx[i].pred_index_re, qs.PR.q_levels[0], qs.PR.min, qs.PR.max, hMdDec->spar_md.band_coeffs[i].pred_re, ndm + ndec - 1 );
    1703             : 
    1704    70853572 :         j = 0;
    1705   226396100 :         for ( ii = 0; ii < ndec; ii++ )
    1706             :         {
    1707   375629440 :             for ( jj = 0; jj < ndm - 1; jj++ )
    1708             :             {
    1709   220086912 :                 quant[j] = hMdDec->spar_md.band_coeffs[i].C_re[ii][jj];
    1710   220086912 :                 j++;
    1711             :             }
    1712             :         }
    1713             : 
    1714    70853572 :         ivas_deindex_real_index( hMdDec->spar_md.band_coeffs_idx[i].drct_index_re, qs.C.q_levels[0], qs.C.min, qs.C.max, quant, ndec * ( ndm - 1 ) );
    1715             : 
    1716    70853572 :         j = 0;
    1717   226396100 :         for ( ii = 0; ii < ndec; ii++ )
    1718             :         {
    1719   375629440 :             for ( jj = 0; jj < ndm - 1; jj++ )
    1720             :             {
    1721   220086912 :                 hMdDec->spar_md.band_coeffs[i].C_re[ii][jj] = quant[j];
    1722   220086912 :                 j++;
    1723             :             }
    1724             :         }
    1725             : 
    1726    70853572 :         ivas_deindex_real_index( hMdDec->spar_md.band_coeffs_idx[i].decd_index_re, qs.P_r.q_levels[0], qs.P_r.min, qs.P_r.max, hMdDec->spar_md.band_coeffs[i].P_re, ndm + ndec - 1 );
    1727             : 
    1728             :         /* Store prior coefficient indices */
    1729   336116896 :         for ( j = 0; j < ndm + ndec - 1; j++ )
    1730             :         {
    1731   265263324 :             hMdDec->spar_md_prev.band_coeffs_idx[i].pred_index_re[j] = hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j];
    1732             :         }
    1733   290940484 :         for ( j = 0; j < ndec * ( ndm - 1 ); j++ )
    1734             :         {
    1735   220086912 :             hMdDec->spar_md_prev.band_coeffs_idx[i].drct_index_re[j] = hMdDec->spar_md.band_coeffs_idx[i].drct_index_re[j];
    1736             :         }
    1737   226396100 :         for ( j = 0; j < ndec; j++ )
    1738             :         {
    1739   155542528 :             hMdDec->spar_md_prev.band_coeffs_idx[i].decd_index_re[j] = hMdDec->spar_md.band_coeffs_idx[i].decd_index_re[j];
    1740             :         }
    1741    70853572 :         hMdDec->valid_bands[i] |= ( do_diff[i] == 0 && do_repeat[i] == 0 ) ? 1 : 0;
    1742             :     }
    1743             : 
    1744     8693889 :     ndec = hMdDec->spar_md_cfg.num_decorr_per_band[0];
    1745     8693889 :     ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[0];
    1746     8693889 :     if ( *bands_bw != 1 )
    1747             :     {
    1748      646314 :         ivas_spar_md_band_upmix(
    1749             :             hMdDec->spar_md.band_coeffs,
    1750             :             nB,
    1751             :             bands_bw,
    1752      646314 :             hMdDec->valid_bands,
    1753             :             1,
    1754             :             ndec,
    1755             :             ndm );
    1756             :     }
    1757             : 
    1758             : 
    1759     8693889 :     return;
    1760             : }
    1761             : 
    1762             : 
    1763             : /*-----------------------------------------------------------------------------------------*
    1764             :  * Function ivas_decode_arith_bs()
    1765             :  *
    1766             :  * Decode bitstream with arith decoder
    1767             :  *-----------------------------------------------------------------------------------------*/
    1768             : 
    1769     8487301 : static void ivas_decode_arith_bs(
    1770             :     ivas_spar_md_dec_state_t *hMdDec,
    1771             :     Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
    1772             :     const uint16_t qsi,
    1773             :     const int16_t nB,
    1774             :     const int16_t bands_bw,
    1775             :     int16_t *pDo_diff,
    1776             :     const int16_t strat,
    1777             :     const int32_t ivas_total_brate )
    1778             : {
    1779             :     int16_t i, ndm, ndec;
    1780             :     int16_t j;
    1781             :     ivas_cell_dim_t pred_cell_dims[IVAS_MAX_NUM_BANDS];
    1782             :     ivas_cell_dim_t drct_cell_dims[IVAS_MAX_NUM_BANDS];
    1783             :     ivas_cell_dim_t decd_cell_dims[IVAS_MAX_NUM_BANDS];
    1784             :     ivas_cell_dim_t decx_cell_dims[IVAS_MAX_NUM_BANDS];
    1785             :     int16_t symbol_arr_re[IVAS_MAX_INPUT_LEN];
    1786             :     int16_t symbol_arr_old_re[IVAS_MAX_INPUT_LEN];
    1787             :     int16_t any_diff;
    1788             : 
    1789    77717649 :     for ( i = 0; i < nB; i++ )
    1790             :     {
    1791    69230348 :         ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[bands_bw * i];
    1792    69230348 :         ndec = hMdDec->spar_md_cfg.num_decorr_per_band[bands_bw * i];
    1793    69230348 :         if ( ( ivas_total_brate < IVAS_24k4 ) && ( strat > 3 ) && ( ( ( i % 2 == 1 ) && ( strat % 2 == 0 ) ) || ( ( i % 2 == 0 ) && ( strat % 2 == 1 ) ) ) )
    1794             :         {
    1795     1253014 :             pred_cell_dims[i].dim1 = 0;
    1796     1253014 :             pred_cell_dims[i].dim2 = 0;
    1797     1253014 :             drct_cell_dims[i].dim1 = 0;
    1798     1253014 :             drct_cell_dims[i].dim2 = 0;
    1799     1253014 :             decd_cell_dims[i].dim1 = 0;
    1800     1253014 :             decd_cell_dims[i].dim2 = 0;
    1801     1253014 :             decx_cell_dims[i].dim1 = 0;
    1802     1253014 :             decx_cell_dims[i].dim2 = 0;
    1803             :         }
    1804             :         else
    1805             :         {
    1806    67977334 :             pred_cell_dims[i].dim1 = ndm + ndec - 1;
    1807    67977334 :             if ( hMdDec->spar_hoa_md_flag && hMdDec->spar_hoa_dirac2spar_md_flag )
    1808             :             {
    1809     6679428 :                 if ( i >= SPAR_DIRAC_SPLIT_START_BAND )
    1810             :                 {
    1811     2226476 :                     pred_cell_dims[i].dim1 -= ( FOA_CHANNELS - 1 );
    1812             :                 }
    1813             :             }
    1814    67977334 :             pred_cell_dims[i].dim2 = 1;
    1815    67977334 :             drct_cell_dims[i].dim1 = ndec;
    1816    67977334 :             drct_cell_dims[i].dim2 = ndm - 1;
    1817    67977334 :             decd_cell_dims[i].dim1 = ndec;
    1818    67977334 :             decd_cell_dims[i].dim2 = 1;
    1819    67977334 :             decx_cell_dims[i].dim1 = ( ndec * ( ndec - 1 ) ) >> 1;
    1820    67977334 :             decx_cell_dims[i].dim2 = 1;
    1821             :         }
    1822             :     }
    1823             : 
    1824     8487301 :     any_diff = 0;
    1825    39500281 :     for ( i = 0; i < nB; i++ )
    1826             :     {
    1827    35944934 :         if ( pDo_diff[i] != 0 )
    1828             :         {
    1829     4931954 :             any_diff = 1;
    1830     4931954 :             break;
    1831             :         }
    1832             :     }
    1833             : 
    1834     8487301 :     if ( any_diff == 1 )
    1835             :     {
    1836     4931954 :         if ( hMdDec->spar_hoa_md_flag && hMdDec->spar_hoa_dirac2spar_md_flag )
    1837             :         {
    1838           0 :             for ( i = 0; i < nB; i++ )
    1839             :             {
    1840           0 :                 if ( i >= SPAR_DIRAC_SPLIT_START_BAND )
    1841             :                 {
    1842           0 :                     for ( j = 0; j < pred_cell_dims[i].dim1; j++ )
    1843             :                     {
    1844           0 :                         hMdDec->spar_md_prev.band_coeffs_idx_mapped[i].pred_index_re[j] =
    1845           0 :                             hMdDec->spar_md_prev.band_coeffs_idx_mapped[i].pred_index_re[j + ( FOA_CHANNELS - 1 )];
    1846             :                     }
    1847             :                 }
    1848             :             }
    1849             :         }
    1850             : 
    1851     4931954 :         ivas_copy_band_coeffs_idx_to_arr( hMdDec->spar_md_prev.band_coeffs_idx_mapped, nB, symbol_arr_old_re, pred_cell_dims, PRED_COEFF );
    1852             :     }
    1853             : 
    1854     8487301 :     ivas_arith_decode_cmplx_cell_array( &hMdDec->arith_coeffs.pred_arith_re[qsi], &hMdDec->arith_coeffs.pred_arith_re_diff[qsi],
    1855             :                                         st0, pred_cell_dims, pDo_diff, nB, symbol_arr_re, symbol_arr_old_re );
    1856             : 
    1857     8487301 :     ivas_fill_band_coeffs_idx( hMdDec->spar_md.band_coeffs_idx, nB, symbol_arr_re, pred_cell_dims, PRED_COEFF );
    1858             : 
    1859     8487301 :     if ( hMdDec->spar_hoa_md_flag && hMdDec->spar_hoa_dirac2spar_md_flag )
    1860             :     {
    1861     7236047 :         for ( i = 0; i < nB; i++ )
    1862             :         {
    1863     6679428 :             if ( i >= SPAR_DIRAC_SPLIT_START_BAND )
    1864             :             {
    1865     9066540 :                 for ( j = pred_cell_dims[i].dim1 - 1; j >= 0; j-- )
    1866             :                 {
    1867     6840064 :                     hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j + ( FOA_CHANNELS - 1 )] =
    1868     6840064 :                         hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j];
    1869             :                 }
    1870     8905904 :                 for ( j = 0; j < FOA_CHANNELS - 1; j++ )
    1871             :                 {
    1872     6679428 :                     hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j] = 0;
    1873             :                 }
    1874             :             }
    1875             :         }
    1876             :     }
    1877             : 
    1878     8487301 :     if ( any_diff == 1 )
    1879             :     {
    1880     4931954 :         ivas_copy_band_coeffs_idx_to_arr( hMdDec->spar_md_prev.band_coeffs_idx_mapped, nB, symbol_arr_old_re, drct_cell_dims, DRCT_COEFF );
    1881             :     }
    1882             : 
    1883     8487301 :     ivas_arith_decode_cmplx_cell_array( &hMdDec->arith_coeffs.drct_arith_re[qsi], &hMdDec->arith_coeffs.drct_arith_re_diff[qsi],
    1884             :                                         st0, drct_cell_dims, pDo_diff, nB, symbol_arr_re, symbol_arr_old_re );
    1885             : 
    1886     8487301 :     ivas_fill_band_coeffs_idx( hMdDec->spar_md.band_coeffs_idx, nB, symbol_arr_re, drct_cell_dims, DRCT_COEFF );
    1887             : 
    1888     8487301 :     if ( any_diff == 1 )
    1889             :     {
    1890     4931954 :         ivas_copy_band_coeffs_idx_to_arr( hMdDec->spar_md_prev.band_coeffs_idx_mapped, nB, symbol_arr_old_re, decd_cell_dims, DECD_COEFF );
    1891             :     }
    1892             : 
    1893     8487301 :     ivas_arith_decode_cmplx_cell_array( &hMdDec->arith_coeffs.decd_arith_re[qsi], &hMdDec->arith_coeffs.decd_arith_re_diff[qsi],
    1894             :                                         st0, decd_cell_dims, pDo_diff, nB, symbol_arr_re, symbol_arr_old_re );
    1895             : 
    1896     8487301 :     ivas_fill_band_coeffs_idx( hMdDec->spar_md.band_coeffs_idx, nB, symbol_arr_re, decd_cell_dims, DECD_COEFF );
    1897             : 
    1898     8487301 :     if ( any_diff == 1 )
    1899             :     {
    1900     4931954 :         ivas_copy_band_coeffs_idx_to_arr( hMdDec->spar_md_prev.band_coeffs_idx_mapped, nB, symbol_arr_old_re, decx_cell_dims, DECX_COEFF );
    1901             :     }
    1902             : 
    1903     8487301 :     ivas_fill_band_coeffs_idx( hMdDec->spar_md.band_coeffs_idx, nB, symbol_arr_re, decx_cell_dims, DECX_COEFF );
    1904             : 
    1905     8487301 :     return;
    1906             : }
    1907             : 
    1908             : 
    1909             : /*-----------------------------------------------------------------------------------------*
    1910             :  * Function ivas_fill_band_coeffs_idx()
    1911             :  *
    1912             :  * Copy pred band coeffs to arr
    1913             :  *-----------------------------------------------------------------------------------------*/
    1914             : 
    1915    33949204 : static void ivas_fill_band_coeffs_idx(
    1916             :     ivas_band_coeffs_ind_t *pBands_idx,
    1917             :     const int16_t nB,
    1918             :     int16_t *pSymbol_re,
    1919             :     ivas_cell_dim_t *pCell_dims,
    1920             :     const ivas_coeffs_type_t coeff_type )
    1921             : {
    1922             :     int16_t i, len;
    1923    33949204 :     int16_t *pPtr_idx = NULL;
    1924             : 
    1925   310870596 :     for ( i = 0; i < nB; i++ )
    1926             :     {
    1927   276921392 :         switch ( coeff_type )
    1928             :         {
    1929    69230348 :             case PRED_COEFF:
    1930             :             {
    1931    69230348 :                 pPtr_idx = pBands_idx[i].pred_index_re;
    1932    69230348 :                 break;
    1933             :             }
    1934    69230348 :             case DRCT_COEFF:
    1935             :             {
    1936    69230348 :                 pPtr_idx = pBands_idx[i].drct_index_re;
    1937    69230348 :                 break;
    1938             :             }
    1939    69230348 :             case DECD_COEFF:
    1940             :             {
    1941    69230348 :                 pPtr_idx = pBands_idx[i].decd_index_re;
    1942    69230348 :                 break;
    1943             :             }
    1944    69230348 :             case DECX_COEFF:
    1945             :             {
    1946    69230348 :                 break;
    1947             :             }
    1948             : 
    1949           0 :             default:
    1950           0 :                 assert( !"unsupported config!" );
    1951             :         }
    1952             : 
    1953   276921392 :         if ( coeff_type != DECX_COEFF )
    1954             :         {
    1955   207691044 :             len = pCell_dims[i].dim1 * pCell_dims[i].dim2;
    1956   207691044 :             mvs2s( pSymbol_re, pPtr_idx, len );
    1957   207691044 :             pSymbol_re += len;
    1958             :         }
    1959             :     }
    1960             : 
    1961    33949204 :     return;
    1962             : }
    1963             : 
    1964             : 
    1965             : /*-----------------------------------------------------------------------------------------*
    1966             :  * Function ivas_decode_huffman_bs()
    1967             :  *
    1968             :  * Decode bitstream with huffman decoder
    1969             :  *-----------------------------------------------------------------------------------------*/
    1970             : 
    1971      206588 : static void ivas_decode_huffman_bs(
    1972             :     ivas_spar_md_dec_state_t *hMdDec,
    1973             :     Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
    1974             :     const uint16_t qsi,
    1975             :     const int16_t nB,
    1976             :     const int16_t bands_bw )
    1977             : {
    1978             :     int16_t i, j;
    1979             :     int16_t ndm, ndec;
    1980             :     int16_t pred_dim, drct_dim, decd_dim, pred_offset;
    1981             : 
    1982     1829812 :     for ( i = 0; i < nB; i++ )
    1983             :     {
    1984     1623224 :         ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[bands_bw * i];
    1985     1623224 :         ndec = hMdDec->spar_md_cfg.num_decorr_per_band[bands_bw * i];
    1986             : 
    1987     1623224 :         pred_dim = ndec + ndm - 1;
    1988     1623224 :         drct_dim = ndec * ( ndm - 1 );
    1989     1623224 :         decd_dim = ndec;
    1990     1623224 :         pred_offset = 0;
    1991             : 
    1992     1623224 :         if ( hMdDec->spar_hoa_md_flag && hMdDec->spar_hoa_dirac2spar_md_flag )
    1993             :         {
    1994           0 :             if ( i >= SPAR_DIRAC_SPLIT_START_BAND )
    1995             :             {
    1996           0 :                 pred_offset = FOA_CHANNELS - 1;
    1997             :             }
    1998             :         }
    1999             : 
    2000     6492896 :         for ( j = pred_offset; j < pred_dim; j++ )
    2001             :         {
    2002     4869672 :             ivas_huffman_decode( &hMdDec->huff_coeffs.pred_huff_re[qsi], st0, &hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j] );
    2003             :         }
    2004             : 
    2005     1623224 :         if ( hMdDec->spar_hoa_md_flag && hMdDec->spar_hoa_dirac2spar_md_flag )
    2006             :         {
    2007           0 :             if ( i >= SPAR_DIRAC_SPLIT_START_BAND )
    2008             :             {
    2009           0 :                 for ( j = 0; j < pred_offset; j++ )
    2010             :                 {
    2011           0 :                     hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j] = 0;
    2012             :                 }
    2013             :             }
    2014             :         }
    2015             : 
    2016     1695400 :         for ( j = 0; j < drct_dim; j++ )
    2017             :         {
    2018       72176 :             ivas_huffman_decode( &hMdDec->huff_coeffs.drct_huff_re[qsi], st0, &hMdDec->spar_md.band_coeffs_idx[i].drct_index_re[j] );
    2019             :         }
    2020             : 
    2021     1857816 :         for ( j = 0; j < decd_dim; j++ )
    2022             :         {
    2023      234592 :             ivas_huffman_decode( &hMdDec->huff_coeffs.decd_huff_re[qsi], st0, &hMdDec->spar_md.band_coeffs_idx[i].decd_index_re[j] );
    2024             :         }
    2025             :     }
    2026             : 
    2027      206588 :     return;
    2028             : }
    2029             : 
    2030    17418658 : static void ivas_spar_plc_get_band_age(
    2031             :     const int16_t *valid_bands,
    2032             :     int16_t *base_band_age,
    2033             :     const int16_t num_bands,
    2034             :     int16_t last_valid_band_idx[IVAS_MAX_NUM_BANDS],
    2035             :     int16_t valid_band_idx[IVAS_MAX_NUM_BANDS],
    2036             :     int16_t *all_valid,
    2037             :     int16_t *b_idx )
    2038             : {
    2039             :     int16_t b, idx;
    2040             : 
    2041    17418658 :     set_s( valid_band_idx, 0, IVAS_MAX_NUM_BANDS );
    2042    17418658 :     set_s( last_valid_band_idx, 0, IVAS_MAX_NUM_BANDS );
    2043    17418658 :     idx = -1;
    2044    17418658 :     *all_valid = 1;
    2045   192476202 :     for ( b = 0; b < num_bands; b++ )
    2046             :     {
    2047   175057544 :         if ( valid_bands[b] != 0 )
    2048             :         {
    2049   170712256 :             base_band_age[b] = 0; /* reset band age */
    2050   170712256 :             idx++;
    2051   170712256 :             valid_band_idx[idx] = b;
    2052             :         }
    2053             :         else
    2054             :         {
    2055     4345288 :             base_band_age[b] += 1; /* increment the age of invalid bands */
    2056             : 
    2057     4345288 :             if ( base_band_age[b] > 3 )
    2058             :             {
    2059      684472 :                 last_valid_band_idx[b] = idx;
    2060             :             }
    2061     4345288 :             *all_valid = 0;
    2062             :         }
    2063             :     }
    2064    17418658 :     *b_idx = idx;
    2065             : 
    2066    17418658 :     return;
    2067             : }
    2068             : 
    2069      684472 : static void ivas_spar_get_plc_interp_weights(
    2070             :     int16_t valid_band_idx[IVAS_MAX_NUM_BANDS],
    2071             :     int16_t last_valid_band_idx,
    2072             :     int16_t idx,
    2073             :     int16_t b,
    2074             :     float *w,
    2075             :     int16_t *id0,
    2076             :     int16_t *id1 )
    2077             : {
    2078      684472 :     if ( last_valid_band_idx < 0 ) /* Extrapolation */
    2079             :     {
    2080      145004 :         *id1 = valid_band_idx[0];
    2081      145004 :         *id0 = 0;
    2082      145004 :         *w = 1;
    2083             :     }
    2084      539468 :     else if ( last_valid_band_idx == idx ) /* Extrapolation */
    2085             :     {
    2086       53200 :         *id1 = valid_band_idx[last_valid_band_idx];
    2087       53200 :         *id0 = valid_band_idx[last_valid_band_idx];
    2088       53200 :         *w = 0;
    2089             :     }
    2090             :     else /* Interpolation */
    2091             :     {
    2092      486268 :         *id0 = valid_band_idx[last_valid_band_idx];
    2093      486268 :         *id1 = valid_band_idx[last_valid_band_idx + 1];
    2094      486268 :         *w = ( (float) ( b - *id0 ) ) / ( *id1 - *id0 );
    2095             :     }
    2096      684472 :     return;
    2097             : }
    2098             : 
    2099             : /*-----------------------------------------------------------------------------------------*
    2100             :  * Function ivas_spar_md_fill_invalid_bands()
    2101             :  *
    2102             :  * Fill invalid bands in interpolation/extrapolation of valid bands
    2103             :  * when PLC is to be done with partial time differential coding
    2104             :  *-----------------------------------------------------------------------------------------*/
    2105     8709329 : static void ivas_spar_md_fill_invalid_bands(
    2106             :     ivas_spar_dec_matrices_t *pSpar_coeffs,
    2107             :     ivas_spar_dec_matrices_t *pSpar_coeffs_prev,
    2108             :     const int16_t *valid_bands,
    2109             :     int16_t *base_band_age,
    2110             :     const int16_t num_bands,
    2111             :     const int16_t num_channels,
    2112             :     const int16_t num_md_sub_frames )
    2113             : {
    2114             :     int16_t i, j, b, all_valid;
    2115     8709329 :     int16_t valid_band_idx[IVAS_MAX_NUM_BANDS], idx = -1;
    2116             :     int16_t last_valid_band_idx[IVAS_MAX_NUM_BANDS];
    2117     8709329 :     float w = 0;
    2118     8709329 :     ivas_spar_plc_get_band_age( valid_bands, base_band_age, num_bands,
    2119             :                                 last_valid_band_idx, valid_band_idx, &all_valid, &idx );
    2120     8709329 :     assert( idx > 0 ); /* some bands should be valid */
    2121             : 
    2122     8709329 :     if ( all_valid == 0 )
    2123             :     {
    2124     5916144 :         for ( b = 0; b < num_bands; b++ )
    2125             :         {
    2126             :             /* check against non zero in if and else if */
    2127     5438342 :             if ( base_band_age[b] > 3 ) /* old invalid bands */
    2128             :             {
    2129             :                 int16_t id0, id1;
    2130      342236 :                 ivas_spar_get_plc_interp_weights( valid_band_idx, last_valid_band_idx[b],
    2131             :                                                   idx, b, &w, &id0, &id1 );
    2132     1711180 :                 for ( i = 0; i < num_channels; i++ )
    2133             :                 {
    2134     6844720 :                     for ( j = 0; j < num_channels; j++ )
    2135             :                     {
    2136     5475776 :                         pSpar_coeffs->C_re[i][j][b] = ( 1 - w ) * pSpar_coeffs->C_re[i][j][id0] + w * pSpar_coeffs->C_re[i][j][id1];
    2137     5475776 :                         pSpar_coeffs->P_re[i][j][b] = ( 1 - w ) * pSpar_coeffs->P_re[i][j][id0] + w * pSpar_coeffs->P_re[i][j][id1];
    2138             :                     }
    2139             :                 }
    2140             :             }
    2141             :             else /* young invalid bands */
    2142             :             {
    2143     5096106 :                 if ( valid_bands[b] == 0 )
    2144             :                 {
    2145     9152040 :                     for ( i = 0; i < num_channels; i++ )
    2146             :                     {
    2147    36608160 :                         for ( j = 0; j < num_channels; j++ )
    2148             :                         {
    2149    29286528 :                             pSpar_coeffs->C_re[i][j][b] = pSpar_coeffs_prev->C_re[i][j][b];
    2150    29286528 :                             pSpar_coeffs->P_re[i][j][b] = pSpar_coeffs_prev->P_re[i][j][b];
    2151             :                         }
    2152             :                     }
    2153             :                 }
    2154             :             }
    2155             : 
    2156     5438342 :             if ( valid_bands[b] == 0 )
    2157             :             {
    2158             :                 int16_t i_ts;
    2159    10863220 :                 for ( i = 0; i < num_channels; i++ )
    2160             :                 {
    2161    43452880 :                     for ( j = 0; j < num_channels; j++ )
    2162             :                     {
    2163   135510464 :                         for ( i_ts = 1; i_ts < num_md_sub_frames; i_ts++ )
    2164             :                         {
    2165   100748160 :                             pSpar_coeffs->C_re[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] = pSpar_coeffs->C_re[i][j][b];
    2166   100748160 :                             pSpar_coeffs->P_re[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] = pSpar_coeffs->P_re[i][j][b];
    2167             :                         }
    2168             :                     }
    2169             :                 }
    2170             :             }
    2171             :         }
    2172             :     }
    2173             : 
    2174     8709329 :     return;
    2175             : }
    2176             : 
    2177     8709329 : static void ivas_spar_md_fill_invalid_bandcoeffs(
    2178             :     ivas_band_coeffs_t *pBand_coeffs,
    2179             :     ivas_band_coeffs_t *pBand_coeffs_prev,
    2180             :     const int16_t *valid_bands,
    2181             :     int16_t *base_band_age,
    2182             :     int16_t *first_valid_frame,
    2183             :     const int16_t num_bands )
    2184             : {
    2185             :     int16_t j, k, b, all_valid;
    2186     8709329 :     int16_t valid_band_idx[IVAS_MAX_NUM_BANDS], idx = -1;
    2187             :     int16_t last_valid_band_idx[IVAS_MAX_NUM_BANDS];
    2188     8709329 :     float w = 0;
    2189             : 
    2190     8709329 :     ivas_spar_plc_get_band_age( valid_bands, base_band_age, num_bands,
    2191             :                                 last_valid_band_idx, valid_band_idx, &all_valid, &idx );
    2192             : 
    2193     8709329 :     assert( idx > 0 ); /* some bands should be valid */
    2194             : 
    2195     8709329 :     if ( all_valid == 0 )
    2196             :     {
    2197     4300218 :         for ( b = 0; b < num_bands; b++ )
    2198             :         {
    2199             :             /* check against non zero in if and else if */
    2200     3822416 :             if ( ( base_band_age[b] > 3 ) || ( *first_valid_frame == 0 ) ) /* old invalid bands */
    2201      342236 :             {
    2202             :                 int16_t id0, id1;
    2203      342236 :                 ivas_spar_get_plc_interp_weights( valid_band_idx, last_valid_band_idx[b],
    2204             :                                                   idx, b, &w, &id0, &id1 );
    2205             : 
    2206     3764596 :                 for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
    2207             :                 {
    2208     3422360 :                     pBand_coeffs[b].pred_re[j] = ( 1 - w ) * pBand_coeffs[id0].pred_re[j] + w * pBand_coeffs[id1].pred_re[j];
    2209             :                 }
    2210             : 
    2211     2737888 :                 for ( j = 0; j < IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS; j++ )
    2212             :                 {
    2213     9582608 :                     for ( k = 0; k < IVAS_SPAR_MAX_DMX_CHS - 1; k++ )
    2214             :                     {
    2215     7186956 :                         pBand_coeffs[b].C_re[j][k] = ( 1 - w ) * pBand_coeffs[id0].C_re[j][k] + w * pBand_coeffs[id1].C_re[j][k];
    2216             :                     }
    2217             :                 }
    2218             : 
    2219     3764596 :                 for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
    2220             :                 {
    2221     3422360 :                     pBand_coeffs[b].P_re[j] = ( 1 - w ) * pBand_coeffs[id0].P_re[j] + w * pBand_coeffs[id1].P_re[j];
    2222             :                 }
    2223             :             }
    2224             :             else /* young invalid bands */
    2225             :             {
    2226     3480180 :                 if ( valid_bands[b] == 0 )
    2227             :                 {
    2228    20134488 :                     for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
    2229             :                     {
    2230    18304080 :                         pBand_coeffs[b].pred_re[j] = pBand_coeffs_prev[b].pred_re[j];
    2231             :                     }
    2232             : 
    2233    14643264 :                     for ( j = 0; j < IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS; j++ )
    2234             :                     {
    2235    51251424 :                         for ( k = 0; k < IVAS_SPAR_MAX_DMX_CHS - 1; k++ )
    2236             :                         {
    2237    38438568 :                             pBand_coeffs[b].C_re[j][k] = pBand_coeffs_prev[b].C_re[j][k];
    2238             :                         }
    2239             :                     }
    2240             : 
    2241    20134488 :                     for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
    2242             :                     {
    2243    18304080 :                         pBand_coeffs[b].P_re[j] = pBand_coeffs_prev[b].P_re[j];
    2244             :                     }
    2245             :                 }
    2246             :             }
    2247             :         }
    2248             :     }
    2249             :     else
    2250             :     {
    2251     8231527 :         *first_valid_frame = 1;
    2252             :     }
    2253             : 
    2254     8709329 :     return;
    2255             : }
    2256             : 
    2257             : 
    2258             : /*-----------------------------------------------------------------------------------------*
    2259             :  * Function ivas_spar_dec_compute_ramp_down_post_matrix()
    2260             :  *
    2261             :  *
    2262             :  *-----------------------------------------------------------------------------------------*/
    2263             : 
    2264    10936779 : static void ivas_spar_dec_compute_ramp_down_post_matrix(
    2265             :     ivas_spar_md_dec_state_t *hMdDec,
    2266             :     const int16_t num_bands_out,
    2267             :     const int16_t bfi,
    2268             :     const int16_t num_md_sub_frames )
    2269             : {
    2270             :     int16_t num_in_ch, num_out_ch, i, j, b;
    2271             : 
    2272    10936779 :     num_in_ch = hMdDec->spar_md_cfg.num_umx_chs;
    2273    10936779 :     num_out_ch = hMdDec->spar_md_cfg.num_umx_chs;
    2274             : 
    2275    10936779 :     if ( bfi == 0 )
    2276             :     {
    2277    10090564 :         hMdDec->spar_plc_num_lost_frames = 0;
    2278             :     }
    2279             :     else
    2280             :     {
    2281      846215 :         if ( hMdDec->td_decorr_flag == 0 )
    2282             :         {
    2283           0 :             assert( 0 );
    2284             :         }
    2285             : 
    2286      846215 :         hMdDec->spar_plc_num_lost_frames += 1;
    2287      846215 :         hMdDec->spar_plc_num_lost_frames = min( hMdDec->spar_plc_num_lost_frames, 100 );
    2288             : 
    2289      846215 :         if ( hMdDec->spar_plc_num_lost_frames > ivas_spar_dec_plc_num_frames_keep )
    2290             :         {
    2291             :             int16_t num_fade_frames;
    2292             :             int16_t gain_dB;
    2293             :             float gain;
    2294             :             float post_matrix[IVAS_SPAR_MAX_CH];
    2295             : 
    2296      218341 :             num_fade_frames = max( hMdDec->spar_plc_num_lost_frames - ivas_spar_dec_plc_num_frames_keep, 0 );
    2297      218341 :             gain_dB = -min( num_fade_frames, ivas_spar_dec_plc_max_num_frames_ramp_down ) * ivas_spar_dec_plc_per_frame_ramp_down_gain_dB;
    2298      218341 :             gain = powf( 10, ( ( (float) gain_dB ) / 20 ) );
    2299             : 
    2300     2620092 :             for ( i = 0; i < IVAS_SPAR_MAX_CH; i++ )
    2301             :             {
    2302     2401751 :                 post_matrix[i] = 1 + min( ( (float) num_fade_frames ) / ivas_spar_dec_plc_num_frames_fade_out, 1 ) * ( ivas_spar_dec_plc_spatial_target[i] - 1 );
    2303     2401751 :                 post_matrix[i] *= gain;
    2304             :             }
    2305             : 
    2306             :             /* apply the post matrix */
    2307     1079555 :             for ( int16_t i_ts = 0; i_ts < num_md_sub_frames; i_ts++ )
    2308             :             {
    2309     4306070 :                 for ( i = 0; i < num_out_ch; i++ )
    2310             :                 {
    2311    17224280 :                     for ( j = 0; j < num_in_ch; j++ )
    2312             :                     {
    2313   169942752 :                         for ( b = 0; b < num_bands_out; b++ )
    2314             :                         {
    2315   156163328 :                             hMdDec->mixer_mat[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] *= post_matrix[i];
    2316             :                         }
    2317             :                     }
    2318             :                 }
    2319             :             }
    2320             :         }
    2321             :     }
    2322             : 
    2323    10936779 :     return;
    2324             : }
    2325             : 
    2326             : 
    2327             : /*-----------------------------------------------------------------------------------------*
    2328             :  * Function ivas_spar_unquant_dtx_indicies()
    2329             :  *
    2330             :  * Unquantize SPAR MD DYX indices
    2331             :  *-----------------------------------------------------------------------------------------*/
    2332             : 
    2333       15440 : static void ivas_spar_unquant_dtx_indicies(
    2334             :     ivas_spar_md_t *pSpar_md,
    2335             :     const int16_t nB,
    2336             :     const int16_t bw,
    2337             :     int16_t *ndm_per_band )
    2338             : {
    2339             :     int16_t i, b;
    2340             :     int16_t q_lvl;
    2341             :     float val;
    2342             :     int16_t idx;
    2343             :     float pr_min_max[2];
    2344             : 
    2345       15440 :     pr_min_max[0] = pSpar_md->min_max[0];
    2346       15440 :     pr_min_max[1] = pSpar_md->min_max[1];
    2347             : 
    2348       46320 :     for ( b = 0; b < nB; b++ )
    2349             :     {
    2350      123520 :         for ( i = 0; i < FOA_CHANNELS - 1; i++ )
    2351             :         {
    2352       92640 :             q_lvl = dtx_pr_real_q_levels[ndm_per_band[bw * b] - 1][i];
    2353       92640 :             idx = pSpar_md->band_coeffs_idx[b].pred_index_re[i];
    2354       92640 :             ivas_deindex_real_index( &idx, q_lvl, pr_min_max[0], pr_min_max[1], &val, 1 );
    2355       92640 :             pSpar_md->band_coeffs[b].pred_re[i] = val;
    2356             :         }
    2357             : 
    2358      108478 :         for ( i = 0; i < FOA_CHANNELS - ndm_per_band[bw * b]; i++ )
    2359             :         {
    2360       77598 :             q_lvl = dtx_pd_real_q_levels[ndm_per_band[bw * b] - 1][i];
    2361       77598 :             idx = pSpar_md->band_coeffs_idx[b].decd_index_re[i];
    2362       77598 :             ivas_deindex_real_index( &idx, q_lvl, dtx_pd_real_min_max[0], dtx_pd_real_min_max[1], &val, 1 );
    2363       77598 :             pSpar_md->band_coeffs[b].P_re[i] = val;
    2364             :         }
    2365             :     }
    2366             : 
    2367       15440 :     return;
    2368             : }
    2369             : 
    2370             : 
    2371             : /*-----------------------------------------------------------------------------------------*
    2372             :  * Function ivas_parse_parameter_bitstream_dtx()
    2373             :  *
    2374             :  * parse DTX bitstream parameters
    2375             :  *-----------------------------------------------------------------------------------------*/
    2376             : 
    2377       15440 : static void ivas_parse_parameter_bitstream_dtx(
    2378             :     ivas_spar_md_t *pSpar_md,
    2379             :     Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
    2380             :     const int16_t bw,
    2381             :     const int16_t num_bands,
    2382             :     int16_t *num_dmx_per_band,
    2383             :     int16_t *num_dec_per_band )
    2384             : {
    2385             :     int16_t i, j, ndec, ndm;
    2386             :     float val;
    2387             :     int16_t idx;
    2388             :     float pr_min_max[2];
    2389             :     int16_t pr_q_lvls, pr, pd, pd_q_lvls, pr_pd_bits;
    2390             :     int16_t zero_pad_bits, sid_bits_len;
    2391             :     int16_t sba_spar_bitlen;
    2392             : 
    2393       15440 :     sid_bits_len = st0->next_bit_pos;
    2394       15440 :     pr_min_max[0] = pSpar_md->min_max[0];
    2395       15440 :     pr_min_max[1] = pSpar_md->min_max[1];
    2396             : 
    2397       46320 :     for ( i = 0; i < num_bands; i++ )
    2398             :     {
    2399       30880 :         ndec = num_dec_per_band[bw * i];
    2400       30880 :         ndm = num_dmx_per_band[bw * i];
    2401             : 
    2402      123520 :         for ( j = 0; j < FOA_CHANNELS - 1; j++ )
    2403             :         {
    2404             :             int16_t pr_idx_1, pr_idx_2, pd_idx_1, pd_idx_2;
    2405             :             uint16_t value;
    2406             : 
    2407       92640 :             pr_idx_1 = pr_pr_idx_pairs[ndm - 1][j][0];
    2408       92640 :             pr_idx_2 = pr_pr_idx_pairs[ndm - 1][j][1];
    2409       92640 :             pd_idx_1 = pr_pd_idx_pairs[ndm - 1][j][0];
    2410       92640 :             pd_idx_2 = pr_pd_idx_pairs[ndm - 1][j][1];
    2411             : 
    2412       92640 :             if ( pr_idx_1 != 0 || pd_idx_1 != 0 || pr_idx_2 != 0 || pd_idx_2 != 0 )
    2413             :             {
    2414       92640 :                 pr_q_lvls = dtx_pr_real_q_levels[ndm - 1][pd_idx_1 - 1];
    2415             : 
    2416       92640 :                 if ( ( j + 1 ) > ndec )
    2417             :                 {
    2418       15042 :                     pd_q_lvls = 1;
    2419             :                 }
    2420             :                 else
    2421             :                 {
    2422       77598 :                     pd_q_lvls = dtx_pd_real_q_levels[ndm - 1][pd_idx_2 - 1];
    2423             :                 }
    2424             : 
    2425       92640 :                 pr_pd_bits = ivas_get_bits_to_encode( pd_q_lvls * pr_q_lvls );
    2426             : 
    2427       92640 :                 value = get_next_indice( st0, pr_pd_bits );
    2428             : 
    2429       92640 :                 pr = (int16_t) floor( value / pd_q_lvls );
    2430       92640 :                 pd = value - pr * pd_q_lvls;
    2431       92640 :                 val = dtx_pd_real_min_max[0];
    2432       92640 :                 ivas_quantise_real_values( &val, pd_q_lvls, dtx_pd_real_min_max[0], dtx_pd_real_min_max[1], &idx, &val, 1 );
    2433       92640 :                 pd = pd + idx;
    2434             : 
    2435       92640 :                 val = pr_min_max[0];
    2436       92640 :                 ivas_quantise_real_values( &val, pr_q_lvls, pr_min_max[0], pr_min_max[1], &idx, &val, 1 );
    2437       92640 :                 pr = pr + idx;
    2438             : 
    2439       92640 :                 if ( ( j + 1 ) <= ndec )
    2440             :                 {
    2441       77598 :                     pSpar_md->band_coeffs_idx[i].decd_index_re[pd_idx_2 - 1] = pd;
    2442             :                 }
    2443             : 
    2444       92640 :                 pSpar_md->band_coeffs_idx[i].pred_index_re[pd_idx_1 - 1] = pr;
    2445             :             }
    2446             :         }
    2447             :     }
    2448             : 
    2449       15440 :     sid_bits_len = st0->next_bit_pos - sid_bits_len;
    2450       15440 :     sba_spar_bitlen = ivas_sba_spar_sid_bitlen( num_dmx_per_band[0] );
    2451       15440 :     zero_pad_bits = sba_spar_bitlen - sid_bits_len;
    2452             : 
    2453       15440 :     assert( zero_pad_bits >= 0 );
    2454       15440 :     if ( num_dmx_per_band[0] == 2 )
    2455             :     {
    2456        7521 :         zero_pad_bits -= 1;
    2457             :     }
    2458             : 
    2459       38003 :     for ( j = 0; j < zero_pad_bits; j++ )
    2460             :     {
    2461       22563 :         get_next_indice( st0, 1 );
    2462             :     }
    2463             : 
    2464       15440 :     ivas_spar_unquant_dtx_indicies( pSpar_md, num_bands, bw, num_dmx_per_band );
    2465             : 
    2466       15440 :     return;
    2467             : }
    2468             : 
    2469             : 
    2470             : /*-----------------------------------------------------------------------------------------*
    2471             :  * Function ivas_deindex_real_index()
    2472             :  *
    2473             :  * Deindex real index
    2474             :  *-----------------------------------------------------------------------------------------*/
    2475             : 
    2476   212730954 : static ivas_error ivas_deindex_real_index(
    2477             :     const int16_t *index,
    2478             :     const int16_t q_levels,
    2479             :     const float min_value,
    2480             :     const float max_value,
    2481             :     float *quant,
    2482             :     const int16_t dim )
    2483             : {
    2484             :     int16_t i;
    2485             :     float q_step;
    2486             : 
    2487   212730954 :     if ( q_levels == 0 )
    2488             :     {
    2489           0 :         return IVAS_ERR_INTERNAL;
    2490             :     }
    2491             : 
    2492   212730954 :     if ( q_levels == 1 )
    2493             :     {
    2494    68505584 :         for ( i = 0; i < dim; i++ )
    2495             :         {
    2496    30228528 :             quant[i] = 0;
    2497             :         }
    2498             :     }
    2499             :     else
    2500             :     {
    2501   174453898 :         q_step = ( max_value - min_value ) / ( q_levels - 1 );
    2502   895009168 :         for ( i = 0; i < dim; i++ )
    2503             :         {
    2504   720555270 :             quant[i] = index[i] * q_step;
    2505             :         }
    2506             :     }
    2507             : 
    2508   212730954 :     return IVAS_ERR_OK;
    2509             : }
    2510             : 
    2511             : 
    2512             : /*-----------------------------------------------------------------------------------------*
    2513             :  * Function ivas_spar_to_dirac()
    2514             :  *
    2515             :  *
    2516             :  *-----------------------------------------------------------------------------------------*/
    2517             : 
    2518     8294019 : void ivas_spar_to_dirac(
    2519             :     Decoder_Struct *st_ivas,
    2520             :     ivas_spar_md_dec_state_t *hMdDec, /* i/o: SPAR MD decoder handle      */
    2521             :     const int16_t dtx_vad,            /* i  : DTX frame flag              */
    2522             :     const int16_t num_bands_out,      /* i  : number of output bands      */
    2523             :     const int16_t bw,                 /* i  : band joining factor         */
    2524             :     const int16_t dyn_active_w_flag )
    2525             : {
    2526             :     DIRAC_DEC_HANDLE hDirAC;
    2527             :     int16_t start_band, end_band, band, qmf_band_start, qmf_band_end;
    2528             :     int16_t block, b;
    2529             :     int16_t *band_grouping;
    2530             :     float diffuseness[IVAS_MAX_NUM_BANDS];
    2531             :     int16_t sba_order_internal;
    2532             :     float azi_dirac[IVAS_MAX_NUM_BANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
    2533             :     float ele_dirac[IVAS_MAX_NUM_BANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
    2534             :     int16_t azi[IVAS_MAX_NUM_BANDS];
    2535             :     int16_t ele[IVAS_MAX_NUM_BANDS];
    2536             :     float dvx[IVAS_MAX_NUM_BANDS], dvy[IVAS_MAX_NUM_BANDS], dvz[IVAS_MAX_NUM_BANDS];
    2537             :     float radius;
    2538             :     float en_ratio, res_pow;
    2539             :     int16_t num_slots_in_subfr;
    2540             :     int16_t tmp_write_idx_param_band;
    2541             :     int16_t tmp_write_idx_band;
    2542             :     float pred_re_20ms[IVAS_MAX_NUM_BANDS][IVAS_SPAR_MAX_CH - 1];
    2543             :     int16_t pred_idx;
    2544             :     int16_t *dirac_to_spar_md_bands;
    2545             :     int16_t enc_param_start_band;
    2546             :     int16_t active_w_vlbr;
    2547             :     int16_t i, num_subframes;
    2548             :     int16_t active_w;
    2549             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
    2550             : 
    2551     8294019 :     active_w = ( dyn_active_w_flag == 1 ) || ( hMdDec->spar_md_cfg.active_w == 1 );
    2552     8294019 :     sba_order_internal = min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER );
    2553     8294019 :     start_band = 0;
    2554     8294019 :     end_band = min( num_bands_out, SPAR_DIRAC_SPLIT_START_BAND ) / bw;
    2555             : 
    2556     8294019 :     hDirAC = st_ivas->hDirAC;
    2557     8294019 :     hSpatParamRendCom = st_ivas->hSpatParamRendCom;
    2558             : 
    2559     8294019 :     dirac_to_spar_md_bands = st_ivas->hSpar->dirac_to_spar_md_bands;
    2560     8294019 :     enc_param_start_band = st_ivas->hSpar->enc_param_start_band / bw;
    2561     8294019 :     active_w_vlbr = ( st_ivas->hDecoderConfig->ivas_total_brate < IVAS_24k4 ) ? 1 : 0;
    2562             : 
    2563     8294019 :     if ( hDirAC != NULL && ivas_get_hodirac_flag( st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->sba_analysis_order ) == 0 )
    2564             :     {
    2565     4230378 :         band_grouping = hDirAC->band_grouping;
    2566     4230378 :         num_slots_in_subfr = st_ivas->hDirAC->hConfig->dec_param_estim ? CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES : 1;
    2567             : 
    2568    38073402 :         for ( band = start_band; band < end_band; band++ )
    2569             :         {
    2570             :             float PR[3], Pd[3], dvnorm, g_pred;
    2571             : 
    2572    33843024 :             PR[0] = hMdDec->spar_md.band_coeffs[band].pred_re[2];
    2573    33843024 :             PR[1] = hMdDec->spar_md.band_coeffs[band].pred_re[0];
    2574    33843024 :             PR[2] = hMdDec->spar_md.band_coeffs[band].pred_re[1];
    2575    33843024 :             g_pred = PR[0] * PR[0] + PR[1] * PR[1] + PR[2] * PR[2];
    2576    33843024 :             if ( g_pred <= EPSILON )
    2577             :             {
    2578     1145420 :                 dvx[band] = 1.0f;
    2579     1145420 :                 dvy[band] = 0.0f;
    2580     1145420 :                 dvz[band] = 0.0f;
    2581             :             }
    2582             :             else
    2583             :             {
    2584    32697604 :                 g_pred = sqrtf( g_pred );
    2585    32697604 :                 dvnorm = 1.0f / g_pred;
    2586    32697604 :                 dvx[band] = PR[0] * dvnorm;
    2587    32697604 :                 dvy[band] = PR[1] * dvnorm;
    2588    32697604 :                 dvz[band] = PR[2] * dvnorm;
    2589             :             }
    2590             : 
    2591    33843024 :             radius = sqrtf( dvx[band] * dvx[band] + dvy[band] * dvy[band] );
    2592    33843024 :             azi[band] = (int16_t) ( max( -180.0f, min( 180.0f, atan2f( dvy[band], dvx[band] ) / EVS_PI * 180.0f ) ) + 0.5f );
    2593    33843024 :             ele[band] = (int16_t) ( max( -90.0f, min( 180.0f, atan2f( dvz[band], radius ) / EVS_PI * 180.0f ) ) + 0.5f );
    2594             : 
    2595    33843024 :             if ( st_ivas->nchan_transport == 1 )
    2596             :             {
    2597             :                 float w_en_norm, f_scale;
    2598     9569688 :                 if ( active_w )
    2599             :                 {
    2600     9569688 :                     if ( dtx_vad == 0 )
    2601             :                     {
    2602       15384 :                         f_scale = IVAS_ACTIVEW_DM_F_SCALE_DTX;
    2603             :                     }
    2604             :                     else
    2605             :                     {
    2606     9554304 :                         f_scale = ( active_w_vlbr ) ? IVAS_ACTIVEW_DM_F_SCALE_VLBR : IVAS_ACTIVEW_DM_F_SCALE;
    2607             :                     }
    2608             :                 }
    2609             :                 else
    2610             :                 {
    2611           0 :                     f_scale = 0.0f;
    2612             :                 }
    2613             : 
    2614     9569688 :                 w_en_norm = ( 1.0f - ( f_scale * g_pred * g_pred ) );
    2615     9569688 :                 w_en_norm *= w_en_norm;
    2616             : 
    2617     9569688 :                 Pd[0] = hMdDec->spar_md.band_coeffs[band].P_re[1];
    2618     9569688 :                 Pd[1] = hMdDec->spar_md.band_coeffs[band].P_re[0];
    2619     9569688 :                 Pd[2] = hMdDec->spar_md.band_coeffs[band].P_re[2];
    2620     9569688 :                 en_ratio = PR[0] * PR[0] + PR[1] * PR[1] + PR[2] * PR[2];
    2621     9569688 :                 res_pow = w_en_norm + en_ratio + ( Pd[0] * Pd[0] + Pd[1] * Pd[1] + Pd[2] * Pd[2] );
    2622     9569688 :                 res_pow *= 0.5f;
    2623     9569688 :                 hMdDec->spar_md.en_ratio_slow[band] = 0.75f * hMdDec->spar_md.en_ratio_slow[band] + 0.25f * en_ratio;
    2624     9569688 :                 hMdDec->spar_md.ref_pow_slow[band] = 0.75f * hMdDec->spar_md.ref_pow_slow[band] + 0.25f * res_pow;
    2625     9569688 :                 en_ratio = sqrtf( hMdDec->spar_md.en_ratio_slow[band] ) / ( hMdDec->spar_md.ref_pow_slow[band] + EPSILON );
    2626             :             }
    2627             :             else
    2628             :             {
    2629    24273336 :                 en_ratio = PR[0] * PR[0] + PR[1] * PR[1] + PR[2] * PR[2];
    2630    24273336 :                 hMdDec->spar_md.en_ratio_slow[band] = 0.75f * hMdDec->spar_md.en_ratio_slow[band] + 0.25f * en_ratio;
    2631    24273336 :                 en_ratio = sqrtf( hMdDec->spar_md.en_ratio_slow[band] );
    2632             :             }
    2633    33843024 :             diffuseness[band] = 1.0f - en_ratio; /*compute diffuseness*/
    2634    33843024 :             diffuseness[band] = ( ( diffuseness[band] < 1.0f ) ? ( ( diffuseness[band] < 0.0f ) ? 0.f : diffuseness[band] ) : 1.0f );
    2635             :         }
    2636             : 
    2637    38073402 :         for ( band = start_band; band < end_band; band++ )
    2638             :         {
    2639             :             int16_t azi_dith, ele_dith;
    2640    33843024 :             tmp_write_idx_param_band = hDirAC->spar_to_dirac_write_idx;
    2641             : 
    2642    33843024 :             en_ratio = 1.0f - diffuseness[band];
    2643    33843024 :             masa_sq( 1.0f - en_ratio, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
    2644             : 
    2645    33843024 :             qmf_band_start = band_grouping[band];
    2646    33843024 :             qmf_band_end = band_grouping[band + 1];
    2647             : 
    2648   169215120 :             for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; block++ )
    2649             :             {
    2650             :                 int16_t ts_start, ts_end, ts;
    2651             : 
    2652   135372096 :                 ts_start = DirAC_block_grouping[block];
    2653   135372096 :                 ts_end = DirAC_block_grouping[block + 1];
    2654   321508728 :                 for ( b = qmf_band_start; b < qmf_band_end; b++ )
    2655             :                 {
    2656             : 
    2657   186136632 :                     azi_dith = azi[band];
    2658   186136632 :                     ele_dith = ele[band];
    2659             : 
    2660   186136632 :                     hSpatParamRendCom->energy_ratio1[block][b] = en_ratio;
    2661   186136632 :                     tmp_write_idx_band = tmp_write_idx_param_band;
    2662             : 
    2663   186136632 :                     if ( hDirAC->hConfig->dec_param_estim == FALSE )
    2664             :                     {
    2665    69460248 :                         hSpatParamRendCom->elevation[tmp_write_idx_band][b] = ele_dith;
    2666    69460248 :                         hSpatParamRendCom->azimuth[tmp_write_idx_band][b] = azi_dith;
    2667    69460248 :                         hSpatParamRendCom->diffuseness_vector[tmp_write_idx_band][b] = diffuseness[band];
    2668             :                     }
    2669             :                     else
    2670             :                     {
    2671   583381920 :                         for ( ts = ts_start; ts < ts_end; ts++ )
    2672             :                         {
    2673   466705536 :                             hSpatParamRendCom->elevation[tmp_write_idx_band][b] = ele_dith;
    2674   466705536 :                             hSpatParamRendCom->azimuth[tmp_write_idx_band][b] = azi_dith;
    2675   466705536 :                             hSpatParamRendCom->diffuseness_vector[tmp_write_idx_band][b] = diffuseness[band];
    2676   466705536 :                             tmp_write_idx_band = ( tmp_write_idx_band + 1 ) % hSpatParamRendCom->dirac_md_buffer_length;
    2677             :                         }
    2678             :                     }
    2679             :                 }
    2680   135372096 :                 tmp_write_idx_param_band = ( tmp_write_idx_param_band + num_slots_in_subfr ) % hSpatParamRendCom->dirac_md_buffer_length;
    2681             :             }
    2682             :         }
    2683             : 
    2684             :         /* update buffer write index */
    2685     4230378 :         if ( hDirAC->hConfig->dec_param_estim == FALSE )
    2686             :         {
    2687     1578642 :             hDirAC->spar_to_dirac_write_idx = ( hDirAC->spar_to_dirac_write_idx + MAX_PARAM_SPATIAL_SUBFRAMES ) % hSpatParamRendCom->dirac_md_buffer_length;
    2688             :         }
    2689             :         else
    2690             :         {
    2691     2651736 :             hDirAC->spar_to_dirac_write_idx = ( hDirAC->spar_to_dirac_write_idx + CLDFB_NO_COL_MAX ) % hSpatParamRendCom->dirac_md_buffer_length;
    2692             :         }
    2693             :     }
    2694             :     else
    2695             :     {
    2696     4063641 :         band = end_band;
    2697             :     }
    2698             : 
    2699             :     /*read DirAC metadata, convert DirAC to SPAR*/
    2700    38517103 :     for ( ; band < num_bands_out / bw; band++ )
    2701             :     {
    2702             :         int16_t dirac_band_idx;
    2703             : 
    2704    30223084 :         dirac_band_idx = dirac_to_spar_md_bands[band] - enc_param_start_band;
    2705             : 
    2706    30223084 :         num_subframes = MAX_PARAM_SPATIAL_SUBFRAMES;
    2707    30223084 :         if ( st_ivas->hQMetaData->useLowerRes )
    2708             :         {
    2709     2344858 :             num_subframes = 1;
    2710             :         }
    2711             : 
    2712   144080846 :         for ( block = 0; block < num_subframes; block++ )
    2713             :         {
    2714   113857762 :             if ( st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].azimuth[block] < 0.f )
    2715             :             {
    2716    20751379 :                 st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].azimuth[block] += 360.f;
    2717             :             }
    2718   113857762 :             azi_dirac[band][block] = st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].azimuth[block];
    2719   113857762 :             ele_dirac[band][block] = st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].elevation[block];
    2720             :         }
    2721             : 
    2722    30223084 :         diffuseness[band] = 1.0f - st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].energy_ratio[0];
    2723             :     }
    2724             : 
    2725             :     /* DirAC MD averaged over 4 subframes and converted to SPAR format similar to encoder processing */
    2726     8294019 :     if ( hMdDec->spar_md_cfg.nchan_transport > 1 )
    2727             :     {
    2728     5697355 :         ivas_get_spar_md_from_dirac( azi_dirac, ele_dirac, diffuseness, 1, NULL, &hMdDec->spar_md, &hMdDec->spar_md_cfg, end_band, num_bands_out, ( hMdDec->spar_hoa_md_flag ) ? 1 : sba_order_internal, dtx_vad, NULL, st_ivas->hQMetaData->useLowerRes, active_w_vlbr, dyn_active_w_flag );
    2729             : 
    2730             :         /* temporarily copy frame-wise prediction coefficients in DirAC bands*/
    2731    22789420 :         for ( pred_idx = 0; pred_idx < FOA_CHANNELS - 1; pred_idx++ )
    2732             :         {
    2733    85460325 :             for ( band = SPAR_DIRAC_SPLIT_START_BAND; band < IVAS_MAX_NUM_BANDS; band++ )
    2734             :             {
    2735    68368260 :                 pred_re_20ms[band][pred_idx] = hMdDec->spar_md.band_coeffs[band].pred_re[pred_idx];
    2736             :             }
    2737             :         }
    2738             :     }
    2739             : 
    2740             :     int16_t num_md_sub_frames;
    2741     8294019 :     num_md_sub_frames = ivas_get_spar_dec_md_num_subframes( sba_order_internal, st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->last_active_ivas_total_brate );
    2742     8294019 :     ivas_get_spar_md_from_dirac( azi_dirac, ele_dirac, diffuseness, num_md_sub_frames, NULL, &hMdDec->spar_md, &hMdDec->spar_md_cfg, end_band, num_bands_out / bw, ( hMdDec->spar_hoa_md_flag ) ? 1 : sba_order_internal, dtx_vad, NULL, st_ivas->hQMetaData->useLowerRes, active_w_vlbr, dyn_active_w_flag );
    2743             : 
    2744     8294019 :     if ( st_ivas->hQMetaData->useLowerRes && dtx_vad )
    2745             :     {
    2746     3231570 :         for ( band = SPAR_DIRAC_SPLIT_START_BAND; band < IVAS_MAX_NUM_BANDS; band++ )
    2747             :         {
    2748     2585256 :             for ( block = 1; block < num_md_sub_frames; block++ )
    2749             :             {
    2750           0 :                 for ( i = 0; i < FOA_CHANNELS - 1; i++ ) /* pred coefficient index (index 0, 1, 2 predicts Y, Z, X respectively) */
    2751             :                 {
    2752           0 :                     hMdDec->spar_md.band_coeffs[band + block * IVAS_MAX_NUM_BANDS].pred_re[i] = hMdDec->spar_md.band_coeffs[band].pred_re[i];
    2753             :                 }
    2754           0 :                 for ( i = 0; i < FOA_CHANNELS - 1; i++ ) /* pred coefficient index (index 0, 1, 2 predicts Y, Z, X respectively) */
    2755             :                 {
    2756           0 :                     hMdDec->spar_md.band_coeffs[band + block * IVAS_MAX_NUM_BANDS].P_re[i] = hMdDec->spar_md.band_coeffs[band].P_re[i];
    2757             :                 }
    2758             :             }
    2759             :         }
    2760             :     }
    2761             : 
    2762             :     /* expand DirAC TC 20ms MD for residual channels to all subframes*/
    2763    39528828 :     for ( block = 0; block < num_md_sub_frames; block++ )
    2764             :     {
    2765   156174045 :         for ( band = SPAR_DIRAC_SPLIT_START_BAND; band < IVAS_MAX_NUM_BANDS; band++ )
    2766             :         {
    2767   499756944 :             for ( pred_idx = 0; pred_idx < FOA_CHANNELS - 1; pred_idx++ ) /* pred coefficient index (index 0, 1, 2 predicts Y, Z, X respectively) */
    2768             :             {
    2769   374817708 :                 if ( ivas_is_res_channel( pred_idx + 1, hMdDec->spar_md_cfg.nchan_transport ) )
    2770             :                 {
    2771             :                     /* use 20ms coefficients only for residual channels */
    2772   176300752 :                     hMdDec->spar_md.band_coeffs[band + block * IVAS_MAX_NUM_BANDS].pred_re[pred_idx] = pred_re_20ms[band][pred_idx];
    2773             :                 }
    2774             :             }
    2775             :         }
    2776             :     }
    2777             : 
    2778    38517103 :     for ( b = end_band * bw; b < num_bands_out; b++ )
    2779             :     {
    2780    30223084 :         hMdDec->valid_bands[b] = 1;
    2781             :     }
    2782             : 
    2783     8294019 :     return;
    2784             : }

Generated by: LCOV version 1.14