LCOV - code coverage report
Current view: top level - lib_dec - ivas_spar_md_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- conformance test test_26252.py @ a21f94bc6bac334fe001a5bad2f7b32b79038097 Lines: 900 976 92.2 %
Date: 2025-11-01 05:07:43 Functions: 30 30 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <stdint.h>
      34             : #include "options.h"
      35             : #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        2163 : 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        2163 :     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        2163 :     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        2163 :     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       12659 :     for ( i = 0; i < num_channels; i++ )
     116             :     {
     117       10496 :         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       70066 :         for ( j = 0; j < num_channels; j++ )
     122             :         {
     123       59570 :             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        2163 :     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       12659 :     for ( i = 0; i < num_channels; i++ )
     135             :     {
     136       10496 :         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       70066 :         for ( j = 0; j < num_channels; j++ )
     141             :         {
     142       59570 :             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        2163 :     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       12659 :     for ( i = 0; i < num_channels; i++ )
     154             :     {
     155       10496 :         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       70066 :         for ( j = 0; j < num_channels; j++ )
     160             :         {
     161       59570 :             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        2163 :     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       12659 :     for ( i = 0; i < num_channels; i++ )
     173             :     {
     174       10496 :         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       70066 :         for ( j = 0; j < num_channels; j++ )
     179             :         {
     180       59570 :             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        2163 :     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       12659 :     for ( i = 0; i < num_channels; i++ )
     192             :     {
     193       10496 :         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       70066 :         for ( j = 0; j < num_channels; j++ )
     198             :         {
     199       59570 :             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        2163 :     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       12659 :     for ( i = 0; i < num_channels; i++ )
     211             :     {
     212       10496 :         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       70066 :         for ( j = 0; j < num_channels; j++ )
     217             :         {
     218       59570 :             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        2163 :     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       12659 :     for ( i = 0; i < num_channels; i++ )
     230             :     {
     231       10496 :         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       70066 :         for ( j = 0; j < num_channels; j++ )
     236             :         {
     237       59570 :             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       12659 :     for ( i = 0; i < num_channels; i++ )
     244             :     {
     245       70066 :         for ( j = 0; j < num_channels; j++ )
     246             :         {
     247      774410 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
     248             :             {
     249      714840 :                 hMdDec->spar_coeffs_prev.C_re[i][j][k] = 0.0f;
     250      714840 :                 hMdDec->spar_coeffs_prev.P_re[i][j][k] = 0.0f;
     251      714840 :                 hMdDec->spar_coeffs_tar.C_re[i][j][k] = 0.0f;
     252      714840 :                 hMdDec->spar_coeffs_tar.P_re[i][j][k] = 0.0f;
     253             :             }
     254             :         }
     255             :     }
     256             : 
     257        2163 :     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     2360043 : 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     2360043 :     num_subframes = MAX_PARAM_SPATIAL_SUBFRAMES;
     277     2360043 :     if ( sba_order > SBA_FOA_ORDER )
     278             :     {
     279      552206 :         if ( ivas_total_brate >= IVAS_512k )
     280             :         {
     281      192256 :             num_subframes = 1;
     282             :         }
     283             :     }
     284             : 
     285     2360043 :     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      132365 :         num_subframes = 1;
     289             :     }
     290             : 
     291     2360043 :     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        2117 : 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        2117 :     error = IVAS_ERR_OK;
     315             : 
     316        2117 :     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        2117 :     num_md_sub_frames = ivas_get_spar_dec_md_num_subframes( sba_order, hDecoderConfig->ivas_total_brate, last_active_ivas_total_brate );
     322             : 
     323        2117 :     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        2117 :     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        2117 :         hMdDec->table_idx = ivas_get_spar_table_idx( hDecoderConfig->ivas_total_brate, sba_order, SPAR_CONFIG_BW, NULL, NULL );
     342             :     }
     343             : 
     344        2117 :     if ( ( error = ivas_spar_md_dec_init( hMdDec, hDecoderConfig, num_channels, sba_order ) ) != IVAS_ERR_OK )
     345             :     {
     346           0 :         return error;
     347             :     }
     348             : 
     349        2117 :     *hMdDec_out = hMdDec;
     350             : 
     351        2117 :     return error;
     352             : }
     353             : 
     354             : 
     355             : /*-------------------------------------------------------------------------
     356             :  * ivas_spar_md_dec_matrix_close()
     357             :  *
     358             :  * Deallocate SPAR MD decoder matrices
     359             :  *------------------------------------------------------------------------*/
     360             : 
     361        2163 : 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        2163 :     if ( hMdDecoder->spar_md.band_coeffs != NULL )
     369             :     {
     370        2163 :         free( hMdDecoder->spar_md.band_coeffs );
     371        2163 :         hMdDecoder->spar_md.band_coeffs = NULL;
     372             :     }
     373        2163 :     if ( hMdDecoder->band_coeffs_prev != NULL )
     374             :     {
     375        2163 :         free( hMdDecoder->band_coeffs_prev );
     376        2163 :         hMdDecoder->band_coeffs_prev = NULL;
     377             :     }
     378             : 
     379        2163 :     if ( hMdDecoder->mixer_mat != NULL )
     380             :     {
     381       12659 :         for ( i = 0; i < num_channels; i++ )
     382             :         {
     383       70066 :             for ( j = 0; j < num_channels; j++ )
     384             :             {
     385       59570 :                 free( hMdDecoder->mixer_mat[i][j] );
     386             :             }
     387       10496 :             free( hMdDecoder->mixer_mat[i] );
     388             :         }
     389        2163 :         free( hMdDecoder->mixer_mat );
     390             :     }
     391             : 
     392        2163 :     if ( hMdDecoder->spar_coeffs.C_re != NULL )
     393             :     {
     394       12659 :         for ( i = 0; i < num_channels; i++ )
     395             :         {
     396       70066 :             for ( j = 0; j < num_channels; j++ )
     397             :             {
     398       59570 :                 free( hMdDecoder->spar_coeffs.C_re[i][j] );
     399             :             }
     400       10496 :             free( hMdDecoder->spar_coeffs.C_re[i] );
     401             :         }
     402        2163 :         free( hMdDecoder->spar_coeffs.C_re );
     403             :     }
     404             : 
     405        2163 :     if ( hMdDecoder->spar_coeffs.P_re != NULL )
     406             :     {
     407       12659 :         for ( i = 0; i < num_channels; i++ )
     408             :         {
     409       70066 :             for ( j = 0; j < num_channels; j++ )
     410             :             {
     411       59570 :                 free( hMdDecoder->spar_coeffs.P_re[i][j] );
     412             :             }
     413       10496 :             free( hMdDecoder->spar_coeffs.P_re[i] );
     414             :         }
     415        2163 :         free( hMdDecoder->spar_coeffs.P_re );
     416             :     }
     417             : 
     418        2163 :     if ( hMdDecoder->spar_coeffs_prev.C_re != NULL )
     419             :     {
     420       12659 :         for ( i = 0; i < num_channels; i++ )
     421             :         {
     422       70066 :             for ( j = 0; j < num_channels; j++ )
     423             :             {
     424       59570 :                 free( hMdDecoder->spar_coeffs_prev.C_re[i][j] );
     425             :             }
     426       10496 :             free( hMdDecoder->spar_coeffs_prev.C_re[i] );
     427             :         }
     428        2163 :         free( hMdDecoder->spar_coeffs_prev.C_re );
     429             :     }
     430             : 
     431        2163 :     if ( hMdDecoder->spar_coeffs_prev.P_re != NULL )
     432             :     {
     433       12659 :         for ( i = 0; i < num_channels; i++ )
     434             :         {
     435       70066 :             for ( j = 0; j < num_channels; j++ )
     436             :             {
     437       59570 :                 free( hMdDecoder->spar_coeffs_prev.P_re[i][j] );
     438             :             }
     439       10496 :             free( hMdDecoder->spar_coeffs_prev.P_re[i] );
     440             :         }
     441        2163 :         free( hMdDecoder->spar_coeffs_prev.P_re );
     442             :     }
     443             : 
     444        2163 :     if ( hMdDecoder->spar_coeffs_tar.C_re != NULL )
     445             :     {
     446       12659 :         for ( i = 0; i < num_channels; i++ )
     447             :         {
     448       70066 :             for ( j = 0; j < num_channels; j++ )
     449             :             {
     450       59570 :                 free( hMdDecoder->spar_coeffs_tar.C_re[i][j] );
     451             :             }
     452       10496 :             free( hMdDecoder->spar_coeffs_tar.C_re[i] );
     453             :         }
     454        2163 :         free( hMdDecoder->spar_coeffs_tar.C_re );
     455             :     }
     456             : 
     457        2163 :     if ( hMdDecoder->spar_coeffs_tar.P_re != NULL )
     458             :     {
     459       12659 :         for ( i = 0; i < num_channels; i++ )
     460             :         {
     461       70066 :             for ( j = 0; j < num_channels; j++ )
     462             :             {
     463       59570 :                 free( hMdDecoder->spar_coeffs_tar.P_re[i][j] );
     464             :             }
     465       10496 :             free( hMdDecoder->spar_coeffs_tar.P_re[i] );
     466             :         }
     467        2163 :         free( hMdDecoder->spar_coeffs_tar.P_re );
     468             :     }
     469             : 
     470        2163 :     return;
     471             : }
     472             : 
     473             : 
     474             : /*-------------------------------------------------------------------------
     475             :  * ivas_spar_md_dec_close()
     476             :  *
     477             :  * Deallocate SPAR MD decoder handle
     478             :  *------------------------------------------------------------------------*/
     479             : 
     480        2117 : 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        2117 :     hMdDecoder = *hMdDec;
     488        2117 :     num_channels = hMdDecoder->spar_md_cfg.num_umx_chs;
     489             : 
     490        2117 :     ivas_spar_md_dec_matrix_close( hMdDecoder, num_channels );
     491             : 
     492        2117 :     free( *hMdDec );
     493        2117 :     *hMdDec = NULL;
     494             : 
     495        2117 :     return;
     496             : }
     497             : 
     498             : 
     499             : /*-----------------------------------------------------------------------------------------*
     500             :  * Function ivas_spar_md_dec_init()
     501             :  *
     502             :  * SPAR MD decoder initialization
     503             :  *-----------------------------------------------------------------------------------------*/
     504             : 
     505        2465 : 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        2465 :     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        2465 :     ivas_sba_get_spar_hoa_ch_ind( num_channels, hDecoderConfig->ivas_total_brate, hMdDec->HOA_md_ind );
     520             : 
     521        2465 :     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        2465 :     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        2465 :     nchan_transport = hMdDec->spar_md_cfg.nchan_transport;
     526             : 
     527             :     /* get FB coefficients */
     528       32045 :     for ( i = 0; i < IVAS_MAX_NUM_BANDS; i++ )
     529             :     {
     530       29580 :         pFC[i] = ivas_fb_fcs_12band_1ms[i] * hDecoderConfig->output_Fs * 0.5f;
     531             :     }
     532             : 
     533        2465 :     if ( ( error = ivas_spar_set_dec_config( hMdDec, nchan_transport, pFC ) ) != IVAS_ERR_OK )
     534             :     {
     535           0 :         return error;
     536             :     }
     537             : 
     538        2465 :     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        2465 :     PR_minmax[0] = hMdDec->spar_md_cfg.quant_strat[0].PR.min;
     545        2465 :     PR_minmax[1] = hMdDec->spar_md_cfg.quant_strat[0].PR.max;
     546        2465 :     ivas_spar_quant_dtx_init( &hMdDec->spar_md, PR_minmax );
     547             : 
     548        2465 :     ivas_spar_arith_coeffs_com_init( &hMdDec->arith_coeffs, &hMdDec->spar_md_cfg, hMdDec->table_idx, DEC );
     549        2465 :     ivas_spar_huff_coeffs_com_init( &hMdDec->huff_coeffs, &hMdDec->spar_md_cfg, hMdDec->table_idx, DEC );
     550             : 
     551        2465 :     hMdDec->spar_md_cfg.prev_quant_idx = -1;
     552             : 
     553             :     /* initialize PLC state */
     554        2465 :     set_s( hMdDec->valid_bands, 0, IVAS_MAX_NUM_BANDS );
     555        2465 :     set_s( hMdDec->base_band_age, 0, IVAS_MAX_NUM_BANDS );
     556        2465 :     set_s( hMdDec->base_band_coeffs_age, 0, IVAS_MAX_NUM_BANDS );
     557        2465 :     hMdDec->spar_plc_num_lost_frames = 0;
     558        2465 :     hMdDec->spar_plc_enable_fadeout_flag = 1;
     559        2465 :     hMdDec->dtx_md_smoothing_cntr = 1;
     560             : 
     561        2465 :     ivas_clear_band_coeffs( hMdDec->spar_md.band_coeffs, IVAS_MAX_NUM_BANDS );
     562        2465 :     ivas_clear_band_coeffs( hMdDec->band_coeffs_prev, IVAS_MAX_NUM_BANDS );
     563        2465 :     ivas_clear_band_coeff_idx( hMdDec->spar_md.band_coeffs_idx, IVAS_MAX_NUM_BANDS );
     564        2465 :     ivas_clear_band_coeff_idx( hMdDec->spar_md_prev.band_coeffs_idx, IVAS_MAX_NUM_BANDS );
     565        2465 :     ivas_clear_band_coeff_idx( hMdDec->spar_md_prev.band_coeffs_idx_mapped, IVAS_MAX_NUM_BANDS );
     566             : 
     567        2465 :     hMdDec->spar_md.dtx_vad = 0;
     568        2465 :     hMdDec->td_decorr_flag = 1;
     569             : 
     570        2465 :     set_f( hMdDec->spar_md.en_ratio_slow, 0.0f, IVAS_MAX_NUM_BANDS );
     571        2465 :     set_f( hMdDec->spar_md.ref_pow_slow, 0.0f, IVAS_MAX_NUM_BANDS );
     572             : 
     573        2465 :     set_zero( hMdDec->smooth_fac, IVAS_MAX_NUM_BANDS );
     574       32045 :     for ( i = 0; i < IVAS_MAX_NUM_BANDS; i++ )
     575             :     {
     576       29580 :         set_zero( hMdDec->smooth_buf[i], 2 * SBA_DIRAC_NRG_SMOOTH_LONG + 1 );
     577             :     }
     578             : 
     579       29580 :     for ( i = 0; i < IVAS_SPAR_MAX_CH; i++ )
     580             :     {
     581      325380 :         for ( j = 0; j < IVAS_SPAR_MAX_CH; j++ )
     582             :         {
     583      298265 :             set_zero( hMdDec->mixer_mat_prev2[i][j], IVAS_MAX_NUM_BANDS );
     584             :         }
     585             :     }
     586        2465 :     hMdDec->first_valid_frame = 1;
     587             : 
     588        2465 :     return IVAS_ERR_OK;
     589             : }
     590             : 
     591             : 
     592             : /*-----------------------------------------------------------------------------------------*
     593             :  * Function ivas_spar_set_dec_config()
     594             :  *
     595             :  * Set configuration for SPAR MD decoder
     596             :  *-----------------------------------------------------------------------------------------*/
     597             : 
     598        2465 : static ivas_error ivas_spar_set_dec_config(
     599             :     ivas_spar_md_dec_state_t *hMdDec,
     600             :     const int16_t nchan_transport,
     601             :     float *pFC )
     602             : {
     603             :     int16_t i, j, nchan, dmx_ch;
     604             : 
     605        8463 :     for ( i = 0; i < nchan_transport; i++ )
     606             :     {
     607        5998 :         hMdDec->spar_md_cfg.max_freq_per_chan[i] = ivas_spar_br_table_consts[hMdDec->table_idx].fpcs;
     608             :     }
     609             : 
     610        2465 :     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 );
     611             : 
     612        2465 :     switch ( nchan )
     613             :     {
     614        2086 :         case 4: /* FOA_CHANNELS */
     615        2086 :             hMdDec->num_decorr = IVAS_TD_DECORR_OUT_3CH;
     616        2086 :             break;
     617          31 :         case 9: /* IVAS_HOA_2_CH */
     618          31 :             hMdDec->num_decorr = IVAS_TD_DECORR_OUT_5CH;
     619          31 :             break;
     620          60 :         case 6: /* IVAS_HOA_2_CH */
     621          60 :             hMdDec->num_decorr = IVAS_TD_DECORR_OUT_2CH;
     622          60 :             break;
     623         149 :         case 8: /* IVAS_HOA_3_CH */
     624         149 :             hMdDec->num_decorr = IVAS_TD_DECORR_OUT_4CH;
     625         149 :             break;
     626             :     }
     627             : 
     628        2465 :     hMdDec->spar_md_cfg.num_umx_chs = nchan;
     629             : 
     630        2465 :     dmx_ch = 0;
     631       32045 :     for ( i = 0; i < IVAS_MAX_NUM_BANDS; i++ )
     632             :     {
     633       29580 :         dmx_ch = 0;
     634      101556 :         for ( j = 0; j < nchan_transport; j++ )
     635             :         {
     636       71976 :             if ( pFC[i] < hMdDec->spar_md_cfg.max_freq_per_chan[j] )
     637             :             {
     638       71976 :                 dmx_ch += 1;
     639             :             }
     640             :         }
     641             : 
     642       29580 :         hMdDec->spar_md_cfg.num_dmx_chans_per_band[i] = hMdDec->spar_md_cfg.nchan_transport;
     643       29580 :         hMdDec->spar_md_cfg.num_decorr_per_band[i] = nchan - hMdDec->spar_md_cfg.nchan_transport;
     644             :     }
     645             : 
     646        2465 :     hMdDec->spar_md_cfg.nchan_transport = dmx_ch;
     647             : 
     648        2465 :     return IVAS_ERR_OK;
     649             : }
     650             : 
     651             : 
     652             : /*-----------------------------------------------------------------------------------------*
     653             :  * Function ivas_dec_mono_sba_handling()
     654             :  *
     655             :  *
     656             :  *-----------------------------------------------------------------------------------------*/
     657             : 
     658      230567 : static void ivas_dec_mono_sba_handling(
     659             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder handle                                 */
     660             : )
     661             : {
     662             :     int16_t mono_flag, b, block;
     663             : 
     664      230567 :     mono_flag = 1;
     665             : 
     666     1173504 :     for ( b = 0; b < st_ivas->hQMetaData->q_direction[0].cfg.nbands; b++ )
     667             :     {
     668     4714685 :         for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; ++block )
     669             :         {
     670     3771748 :             float azimuth = st_ivas->hQMetaData->q_direction[0].band_data[b].azimuth[block];
     671     3771748 :             float elevation = st_ivas->hQMetaData->q_direction[0].band_data[b].azimuth[block];
     672     3771748 :             float energy_ratio = st_ivas->hQMetaData->q_direction[0].band_data[0].energy_ratio[block];
     673     3771748 :             if (
     674      729199 :                 ( azimuth != 0.0f ) ||
     675      729199 :                 ( elevation != 0.0f ) ||
     676             :                 ( energy_ratio > 0.15f ) ) /* 0.15f is just above the lowest quantised value. */
     677             :             {
     678     3394805 :                 mono_flag = 0;
     679             :             }
     680             :         }
     681             :     }
     682             : 
     683             :     /* Combine the SPAR prediction coefs flag with the azimuth, elevation and energy ratio flag.*/
     684      230567 :     mono_flag = mono_flag && ivas_spar_chk_zero_coefs( st_ivas );
     685             : 
     686      230567 :     if ( mono_flag )
     687             :     {
     688             :         /* Set Energy Ratio values to be zero */
     689         352 :         for ( b = 0; b < st_ivas->hQMetaData->q_direction[0].cfg.nbands; b++ )
     690             :         {
     691         264 :             set_zero( st_ivas->hQMetaData->q_direction[0].band_data[b].energy_ratio, MAX_PARAM_SPATIAL_SUBFRAMES );
     692             :         }
     693          88 :         if ( st_ivas->hDirAC != NULL )
     694             :         {
     695         469 :             for ( block = 0; block < st_ivas->hSpatParamRendCom->dirac_md_buffer_length; ++block )
     696             :             {
     697             :                 /* Set directional Energy Ratio values to be zero */
     698         402 :                 set_zero( st_ivas->hSpatParamRendCom->energy_ratio1[block], st_ivas->hSpatParamRendCom->num_freq_bands );
     699         402 :                 if ( st_ivas->hQMetaData->no_directions == 2 )
     700             :                 {
     701           0 :                     set_zero( st_ivas->hSpatParamRendCom->energy_ratio2[block], st_ivas->hSpatParamRendCom->num_freq_bands );
     702             :                 }
     703             :                 /* Set Diffuseness values to be 1.0 */
     704         402 :                 set_f( st_ivas->hSpatParamRendCom->diffuseness_vector[block], 1.0f, st_ivas->hSpatParamRendCom->num_freq_bands );
     705             :             }
     706             :         }
     707             :     }
     708             : 
     709      230567 :     return;
     710             : }
     711             : 
     712             : 
     713             : /*-----------------------------------------------------------------------------------------*
     714             :  * Function ivas_spar_md_dec_process()
     715             :  *
     716             :  * SPAR Meta Data decoder process
     717             :  *-----------------------------------------------------------------------------------------*/
     718             : 
     719      230567 : void ivas_spar_md_dec_process(
     720             :     Decoder_Struct *st_ivas,     /* i/o: IVAS decoder handle                                */
     721             :     Decoder_State *st0,          /* i/o: decoder state structure - for bitstream handling   */
     722             :     const int16_t num_bands_out, /* i  : number of output bands                             */
     723             :     const int16_t sba_order      /* i  : Ambisonic (SBA) order                              */
     724             : )
     725             : {
     726             :     int16_t j, k, b, bw, dtx_vad, nB, i_ts;
     727             :     ivas_spar_md_dec_state_t *hMdDec;
     728             :     int16_t num_md_chs;
     729             :     int16_t num_md_sub_frames;
     730             :     int16_t dyn_active_w_flag;
     731             :     int16_t active_w_vlbr;
     732             : 
     733      230567 :     hMdDec = st_ivas->hSpar->hMdDec;
     734             : 
     735      230567 :     active_w_vlbr = ( st_ivas->hDecoderConfig->ivas_total_brate < IVAS_24k4 ) ? 1 : 0;
     736             : 
     737      230567 :     num_md_chs = ivas_sba_get_nchan_metadata( sba_order, st_ivas->hDecoderConfig->ivas_total_brate );
     738             : 
     739      230567 :     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 );
     740             : 
     741      230567 :     if ( hMdDec->spar_md_cfg.nchan_transport > 1 && hMdDec->spar_md_cfg.nchan_transport <= 3 )
     742             :     {
     743       88801 :         hMdDec->spar_md.res_ind = 0;
     744       88801 :         dyn_active_w_flag = get_next_indice( st0, 1 );
     745       88801 :         if ( dyn_active_w_flag == 1 )
     746             :         {
     747           0 :             if ( hMdDec->spar_md_cfg.nchan_transport == 2 )
     748             :             {
     749           0 :                 hMdDec->spar_md.res_ind = get_next_indice( st0, 1 );
     750           0 :                 hMdDec->spar_md.res_ind += hMdDec->spar_md_cfg.nchan_transport;
     751             :             }
     752           0 :             else if ( hMdDec->spar_md_cfg.nchan_transport == 3 )
     753             :             {
     754           0 :                 hMdDec->spar_md.res_ind = remix_order_set[hMdDec->spar_md_cfg.remix_unmix_order][hMdDec->spar_md_cfg.nchan_transport];
     755             :             }
     756             :         }
     757             :     }
     758             :     else
     759             :     {
     760      141766 :         dyn_active_w_flag = 0;
     761      141766 :         if ( hMdDec->spar_md_cfg.nchan_transport == FOA_CHANNELS )
     762             :         {
     763      102876 :             get_next_indice( st0, 1 );
     764             :         }
     765             :     }
     766             : 
     767      230567 :     ivas_spar_dec_parse_md_bs( hMdDec, st0, &nB, &bw, &dtx_vad, st_ivas->hDecoderConfig->ivas_total_brate,
     768      230567 :                                st_ivas->hQMetaData->sba_inactive_mode );
     769             : 
     770      230567 :     assert( nB == hMdDec->spar_md.num_bands );
     771      230567 :     assert( bw == 1 );
     772      230567 :     ivas_spar_md_fill_invalid_bandcoeffs(
     773             :         hMdDec->spar_md.band_coeffs,
     774             :         hMdDec->band_coeffs_prev,
     775      230567 :         &hMdDec->valid_bands[0],
     776             :         &hMdDec->base_band_coeffs_age[0],
     777             :         &hMdDec->first_valid_frame,
     778             :         nB );
     779             : 
     780      230567 :     ivas_dec_mono_sba_handling( st_ivas );
     781             : 
     782             :     /* SPAR to DirAC conversion */
     783      230567 :     if ( hMdDec->spar_hoa_dirac2spar_md_flag == 1 )
     784             :     {
     785      204072 :         ivas_spar_to_dirac( st_ivas, hMdDec, dtx_vad, num_bands_out, bw, dyn_active_w_flag );
     786             :     }
     787             : 
     788             :     /* set correct number of bands*/
     789      230567 :     nB = IVAS_MAX_NUM_BANDS;
     790             : #ifdef DEBUG_LBR_SBA
     791             :     /* Dumping SPAR Coefficients */
     792             :     char f_name[100];
     793             :     int16_t nbands = 4; // 6 total, just looking at SPAR
     794             :     int16_t num_subframes = 1;
     795             :     int16_t num_elements = 6;
     796             :     int16_t num_block_group = 1;
     797             :     int16_t byte_size = sizeof( float );
     798             : 
     799             :     sprintf( f_name, "SBA_MD_values_dec.bin" );
     800             :     ( frame == 0 ) ? dbgwrite( &nbands, sizeof( nbands ), 1, 1, f_name ) : false;
     801             :     ( frame == 0 ) ? dbgwrite( &num_elements, sizeof( num_elements ), 1, 1, f_name ) : false;
     802             :     ( frame == 0 ) ? dbgwrite( &num_subframes, sizeof( num_subframes ), 1, 1, f_name ) : false;
     803             :     ( frame == 0 ) ? dbgwrite( &num_block_group, sizeof( num_block_group ), 1, 1, f_name ) : false;
     804             :     ( frame == 0 ) ? dbgwrite( &byte_size, sizeof( byte_size ), 1, 1, f_name ) : false;
     805             : 
     806             :     for ( b = 0; b < nbands; b++ )
     807             :     {
     808             :         for ( int16_t sf = 0; sf < num_subframes; sf++ )
     809             :         {
     810             :             for ( int16_t bl = 0; bl < num_block_group; bl++ )
     811             :             {
     812             :                 dbgwrite( &hMdDec->spar_md.band_coeffs[b].pred_re[0], byte_size, 1, 1, f_name );
     813             :                 dbgwrite( &hMdDec->spar_md.band_coeffs[b].pred_re[1], byte_size, 1, 1, f_name );
     814             :                 dbgwrite( &hMdDec->spar_md.band_coeffs[b].pred_re[2], byte_size, 1, 1, f_name );
     815             :                 dbgwrite( &hMdDec->spar_md.band_coeffs[b].P_re[0], byte_size, 1, 1, f_name );
     816             :                 dbgwrite( &hMdDec->spar_md.band_coeffs[b].P_re[1], byte_size, 1, 1, f_name );
     817             :                 dbgwrite( &hMdDec->spar_md.band_coeffs[b].P_re[2], byte_size, 1, 1, f_name );
     818             :                 // fprintf(stdout, "%f\t%f\t%f\t%d\t%d\n", dirac_md_kbps, spar_md_kbps, sba_md_kbps, qsi, code_strat );
     819             :             }
     820             :         }
     821             :     }
     822             : #endif
     823             : 
     824             :     /* expand DirAC MD to all time slots */
     825      807347 :     for ( i_ts = 1; i_ts < num_md_sub_frames; i_ts++ )
     826             :     {
     827     5436576 :         for ( b = 0; b < hMdDec->spar_md.num_bands; b++ )
     828             :         {
     829    53457756 :             for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
     830             :             {
     831    48597960 :                 hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].pred_re[j] = hMdDec->spar_md.band_coeffs[b].pred_re[j];
     832             :             }
     833             : 
     834    38878368 :             for ( j = 0; j < IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS; j++ )
     835             :             {
     836   136074288 :                 for ( k = 0; k < IVAS_SPAR_MAX_DMX_CHS - 1; k++ )
     837             :                 {
     838   102055716 :                     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];
     839             :                 }
     840             :             }
     841             : 
     842    53457756 :             for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
     843             :             {
     844    48597960 :                 hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].P_re[j] = hMdDec->spar_md.band_coeffs[b].P_re[j];
     845             :             }
     846             :         }
     847             :     }
     848             : 
     849      230567 :     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 );
     850             : 
     851             : #ifdef DEBUG_SPAR_DIRAC_WRITE_OUT_PRED_PARS
     852             :     {
     853             :         static FILE *fid = 0;
     854             :         int16_t band = 9;
     855             :         if ( !fid )
     856             :         {
     857             :             fid = fopen( "pred_coeffs_dec.txt", "wt" );
     858             :         }
     859             :         fprintf( fid, "%.6f\n", hMdDec->mixer_mat[1][0][band] );
     860             :     }
     861             : #endif
     862      230567 :     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 );
     863             : 
     864             : 
     865      230567 :     hMdDec->dtx_md_smoothing_cntr = 1;
     866             : 
     867      230567 :     return;
     868             : }
     869             : 
     870             : 
     871             : /*-----------------------------------------------------------------------------------------*
     872             :  * Function ivas_spar_chk_zero_coefs()
     873             :  *
     874             :  * Check for zeroed SPAR coefficients
     875             :  *-----------------------------------------------------------------------------------------*/
     876             : 
     877         814 : int16_t ivas_spar_chk_zero_coefs(
     878             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder handle        */
     879             : )
     880             : {
     881             :     int16_t j, k, b;
     882             :     ivas_spar_md_dec_state_t *hMdDec;
     883         814 :     int16_t mono = 1;
     884             :     int16_t ndec, ndm;
     885             : 
     886         814 :     hMdDec = st_ivas->hSpar->hMdDec;
     887         814 :     ndec = hMdDec->spar_md_cfg.num_decorr_per_band[0];
     888         814 :     ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[0];
     889             : 
     890        7326 :     for ( b = 0; b < min( hMdDec->spar_md.num_bands, SPAR_DIRAC_SPLIT_START_BAND ); b++ )
     891             :     {
     892       26128 :         for ( j = 0; j < ndm + ndec - 1; j++ )
     893             :         {
     894       19616 :             if ( hMdDec->spar_md.band_coeffs[b].pred_re[j] != 0.0f )
     895             :             {
     896        9627 :                 mono = 0;
     897             :             }
     898             :         }
     899       23544 :         for ( j = 0; j < ndec; j++ )
     900             :         {
     901       20104 :             for ( k = 0; k < ndm - 1; k++ )
     902             :             {
     903        3072 :                 if ( hMdDec->spar_md.band_coeffs[b].C_re[j][k] != 0.0f )
     904             :                 {
     905         716 :                     mono = 0;
     906             :                 }
     907             :             }
     908             :         }
     909       23544 :         for ( j = 0; j < ndec; j++ )
     910             :         {
     911       17032 :             if ( hMdDec->spar_md.band_coeffs[b].P_re[j] != 0.0f )
     912             :             {
     913       15321 :                 mono = 0;
     914             :             }
     915             :         }
     916             :     }
     917             : 
     918         814 :     return mono;
     919             : }
     920             : 
     921             : 
     922             : /*-----------------------------------------------------------------------------------------*
     923             :  * Function ivas_spar_smooth_md_dtx()
     924             :  *
     925             :  * Smooth MD during no data frame during DTX
     926             :  *-----------------------------------------------------------------------------------------*/
     927             : 
     928        2190 : void ivas_spar_smooth_md_dtx(
     929             :     ivas_spar_md_dec_state_t *hMdDec, /* i/o: SPAR MD decoder handle        */
     930             :     const int16_t num_bands_out,      /* i  : number of output bands        */
     931             :     const int16_t num_md_sub_frames   /* i  : number of metadata subframes  */
     932             : )
     933             : {
     934             :     int16_t j, k, b, dmx_ch;
     935             :     float ramp, tar, prev, new_val;
     936             : 
     937        2190 :     ramp = (float) hMdDec->dtx_md_smoothing_cntr / IVAS_DEFAULT_DTX_CNG_RAMP;
     938             : 
     939       28470 :     for ( b = 0; b < num_bands_out; b++ )
     940             :     {
     941       26280 :         dmx_ch = hMdDec->spar_md_cfg.num_dmx_chans_per_band[b];
     942             : 
     943      105120 :         for ( j = 1; j < FOA_CHANNELS; j++ )
     944             :         {
     945      277092 :             for ( k = dmx_ch; k < FOA_CHANNELS; k++ )
     946             :             {
     947      198252 :                 prev = hMdDec->spar_coeffs_prev.P_re[j][k][b];
     948      198252 :                 tar = hMdDec->spar_coeffs_tar.P_re[j][k][b];
     949      198252 :                 new_val = prev + ( ramp * ( tar - prev ) );
     950      198252 :                 hMdDec->spar_coeffs.P_re[j][k][b] = new_val;
     951             :             }
     952             :         }
     953             : 
     954      131400 :         for ( j = 0; j < FOA_CHANNELS; j++ )
     955             :         {
     956      261264 :             for ( k = 0; k < dmx_ch; k++ )
     957             :             {
     958      156144 :                 prev = hMdDec->spar_coeffs_prev.C_re[j][k][b];
     959      156144 :                 tar = hMdDec->spar_coeffs_tar.C_re[j][k][b];
     960      156144 :                 new_val = prev + ( ramp * ( tar - prev ) );
     961      156144 :                 hMdDec->spar_coeffs.C_re[j][k][b] = new_val;
     962             :             }
     963             :         }
     964             :     }
     965             : 
     966             :     /* expand MD to all time slots */
     967        8523 :     for ( int16_t i_ts = 1; i_ts < num_md_sub_frames; i_ts++ )
     968             :     {
     969       82329 :         for ( b = 0; b < num_bands_out; b++ )
     970             :         {
     971       75996 :             dmx_ch = hMdDec->spar_md_cfg.num_dmx_chans_per_band[b];
     972             : 
     973      303984 :             for ( j = 1; j < FOA_CHANNELS; j++ )
     974             :             {
     975      797148 :                 for ( k = dmx_ch; k < FOA_CHANNELS; k++ )
     976             : 
     977             :                 {
     978      569160 :                     hMdDec->spar_coeffs.P_re[j][k][b + i_ts * IVAS_MAX_NUM_BANDS] = hMdDec->spar_coeffs.P_re[j][k][b];
     979             :                 }
     980             :             }
     981             : 
     982      379980 :             for ( j = 0; j < FOA_CHANNELS; j++ )
     983             :             {
     984      761040 :                 for ( k = 0; k < dmx_ch; k++ )
     985             :                 {
     986      457056 :                     hMdDec->spar_coeffs.C_re[j][k][b + i_ts * IVAS_MAX_NUM_BANDS] = hMdDec->spar_coeffs.C_re[j][k][b];
     987             :                 }
     988             :             }
     989             :         }
     990             :     }
     991             : 
     992        2190 :     hMdDec->dtx_md_smoothing_cntr = min( hMdDec->dtx_md_smoothing_cntr + 1, IVAS_DEFAULT_DTX_CNG_RAMP );
     993             : 
     994        2190 :     return;
     995             : }
     996             : 
     997             : 
     998             : /*-----------------------------------------------------------------------------------------*
     999             :  * Function ivas_spar_setup_md_smoothing()
    1000             :  *
    1001             :  * Set up smoothing of SPAR MD when SID update frame is received
    1002             :  *-----------------------------------------------------------------------------------------*/
    1003             : 
    1004         125 : void ivas_spar_setup_md_smoothing(
    1005             :     ivas_spar_md_dec_state_t *hMdDec, /* i/o: SPAR MD decoder handle        */
    1006             :     const int16_t num_bands_out,      /* i  : number of output bands        */
    1007             :     const int16_t num_md_sub_frames   /* i  : number of metadata subframes  */
    1008             : )
    1009             : {
    1010             :     /* copy the coeffs */
    1011             :     int16_t num_channels, i, j, k;
    1012             : 
    1013         125 :     num_channels = hMdDec->spar_md_cfg.num_umx_chs;
    1014             : 
    1015         625 :     for ( i = 0; i < num_channels; i++ )
    1016             :     {
    1017        2500 :         for ( j = 0; j < num_channels; j++ )
    1018             :         {
    1019       26000 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1020             :             {
    1021       24000 :                 hMdDec->spar_coeffs_prev.C_re[i][j][k] = hMdDec->spar_coeffs_tar.C_re[i][j][k];
    1022             :             }
    1023             :         }
    1024             :     }
    1025             : 
    1026         625 :     for ( i = 0; i < num_channels; i++ )
    1027             :     {
    1028        2500 :         for ( j = 0; j < num_channels; j++ )
    1029             :         {
    1030       26000 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1031             :             {
    1032       24000 :                 hMdDec->spar_coeffs_prev.P_re[i][j][k] = hMdDec->spar_coeffs_tar.P_re[i][j][k];
    1033             :             }
    1034             :         }
    1035             :     }
    1036             : 
    1037         625 :     for ( i = 0; i < num_channels; i++ )
    1038             :     {
    1039        2500 :         for ( j = 0; j < num_channels; j++ )
    1040             :         {
    1041       26000 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1042             :             {
    1043       24000 :                 hMdDec->spar_coeffs_tar.C_re[i][j][k] = hMdDec->spar_coeffs.C_re[i][j][k];
    1044             :             }
    1045             :         }
    1046             :     }
    1047             : 
    1048         625 :     for ( i = 0; i < num_channels; i++ )
    1049             :     {
    1050        2500 :         for ( j = 0; j < num_channels; j++ )
    1051             :         {
    1052       26000 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1053             :             {
    1054       24000 :                 hMdDec->spar_coeffs_tar.P_re[i][j][k] = hMdDec->spar_coeffs.P_re[i][j][k];
    1055             :             }
    1056             :         }
    1057             :     }
    1058             : 
    1059         125 :     ivas_spar_smooth_md_dtx( hMdDec, num_bands_out, num_md_sub_frames );
    1060             : 
    1061         125 :     return;
    1062             : }
    1063             : 
    1064             : 
    1065             : /*-----------------------------------------------------------------------------------------*
    1066             :  * Function ivas_spar_update_md_hist()
    1067             :  *
    1068             :  * Update previous and target MD
    1069             :  *-----------------------------------------------------------------------------------------*/
    1070             : 
    1071      230442 : void ivas_spar_update_md_hist(
    1072             :     ivas_spar_md_dec_state_t *hMdDec /* i/o: SPAR MD decoder handle */
    1073             : )
    1074             : {
    1075             :     int16_t num_channels, i, j, k;
    1076             : 
    1077      230442 :     num_channels = hMdDec->spar_md_cfg.num_umx_chs;
    1078             : 
    1079     1403369 :     for ( i = 0; i < num_channels; i++ )
    1080             :     {
    1081     8370062 :         for ( j = 0; j < num_channels; j++ )
    1082             :         {
    1083    93562755 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1084             :             {
    1085    86365620 :                 hMdDec->spar_coeffs_prev.C_re[i][j][k] = hMdDec->spar_coeffs.C_re[i][j][k];
    1086             :             }
    1087             :         }
    1088             :     }
    1089             : 
    1090     1403369 :     for ( i = 0; i < num_channels; i++ )
    1091             :     {
    1092     8370062 :         for ( j = 0; j < num_channels; j++ )
    1093             :         {
    1094    93562755 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1095             :             {
    1096    86365620 :                 hMdDec->spar_coeffs_prev.P_re[i][j][k] = hMdDec->spar_coeffs.P_re[i][j][k];
    1097             :             }
    1098             :         }
    1099             :     }
    1100             : 
    1101     1403369 :     for ( i = 0; i < num_channels; i++ )
    1102             :     {
    1103     8370062 :         for ( j = 0; j < num_channels; j++ )
    1104             :         {
    1105    93562755 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1106             :             {
    1107    86365620 :                 hMdDec->spar_coeffs_tar.C_re[i][j][k] = hMdDec->spar_coeffs.C_re[i][j][k];
    1108             :             }
    1109             :         }
    1110             :     }
    1111             : 
    1112     1403369 :     for ( i = 0; i < num_channels; i++ )
    1113             :     {
    1114     8370062 :         for ( j = 0; j < num_channels; j++ )
    1115             :         {
    1116    93562755 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1117             :             {
    1118    86365620 :                 hMdDec->spar_coeffs_tar.P_re[i][j][k] = hMdDec->spar_coeffs.P_re[i][j][k];
    1119             :             }
    1120             :         }
    1121             :     }
    1122             : 
    1123      230442 :     return;
    1124             : }
    1125             : 
    1126             : 
    1127             : /*-----------------------------------------------------------------------------------------*
    1128             :  * Function ivas_get_spar_matrices()
    1129             :  *
    1130             :  * Get SPAR matrices
    1131             :  *-----------------------------------------------------------------------------------------*/
    1132             : 
    1133      230567 : static void ivas_get_spar_matrices(
    1134             :     ivas_spar_md_dec_state_t *hMdDec,
    1135             :     const int16_t num_bands_out,
    1136             :     const int16_t n_ts,
    1137             :     const int16_t bw,
    1138             :     const int16_t dtx_vad,
    1139             :     const int16_t nB,
    1140             :     const int16_t numch_out,
    1141             :     const int16_t active_w_vlbr,
    1142             :     const int16_t dyn_active_w_flag )
    1143             : {
    1144             :     int16_t num_bands, dmx_ch, split_band;
    1145             :     int16_t i, j, k, m, b, i_ts, active_w;
    1146             :     const int16_t *order;
    1147             :     float active_w_dm_fac, re;
    1148             : 
    1149             : 
    1150      230567 :     num_bands = num_bands_out;
    1151      230567 :     order = remix_order_set[hMdDec->spar_md_cfg.remix_unmix_order];
    1152             : 
    1153      230567 :     split_band = SPAR_DIRAC_SPLIT_START_BAND;
    1154      230567 :     if ( split_band >= IVAS_MAX_NUM_BANDS )
    1155             :     {
    1156             :         /*store previous 4x4 parameters for linear interpolation to current*/
    1157           0 :         for ( i = 0; i < numch_out; i++ )
    1158             :         {
    1159           0 :             for ( j = 0; j < numch_out; j++ )
    1160             :             {
    1161           0 :                 for ( b = 0; b < num_bands; b++ )
    1162             :                 {
    1163           0 :                     hMdDec->mixer_mat_prev[0][i][j][b] = hMdDec->mixer_mat[i][j][b];
    1164             :                 }
    1165             :             }
    1166             :         }
    1167             :     }
    1168             : 
    1169      230567 :     if ( bw == IVAS_RED_BAND_FACT )
    1170             :     {
    1171           0 :         num_bands = num_bands >> 1;
    1172             :     }
    1173             : 
    1174      230567 :     active_w = ( dyn_active_w_flag == 1 ) || ( hMdDec->spar_md_cfg.active_w == 1 );
    1175      230567 :     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 );
    1176             : 
    1177     1037914 :     for ( i_ts = 0; i_ts < n_ts; i_ts++ )
    1178             :     {
    1179     4498938 :         for ( i = 0; i < numch_out; i++ )
    1180             :         {
    1181    22582710 :             for ( j = 0; j < numch_out; j++ )
    1182             :             {
    1183    18891119 :                 set_zero( &hMdDec->spar_coeffs.C_re[i][j][i_ts * IVAS_MAX_NUM_BANDS], IVAS_MAX_NUM_BANDS );
    1184    18891119 :                 set_zero( &hMdDec->spar_coeffs.P_re[i][j][i_ts * IVAS_MAX_NUM_BANDS], IVAS_MAX_NUM_BANDS );
    1185             :             }
    1186             :         }
    1187      807347 :         num_bands = min( num_bands, nB );
    1188             : 
    1189    10400411 :         for ( b = 0; b < num_bands; b++ )
    1190             :         {
    1191             :             float tmp_C1_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
    1192             :             float tmp_C2_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
    1193             :             float tmp_dm_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
    1194             : 
    1195     9593064 :             dmx_ch = hMdDec->spar_md_cfg.num_dmx_chans_per_band[bw * b];
    1196             : 
    1197    53495756 :             for ( j = 0; j < numch_out; j++ )
    1198             :             {
    1199    43902692 :                 set_zero( tmp_C1_re[j], numch_out );
    1200    43902692 :                 set_zero( tmp_C2_re[j], numch_out );
    1201    43902692 :                 set_zero( tmp_dm_re[j], numch_out );
    1202             : 
    1203    43902692 :                 tmp_C1_re[j][j] = 1.0f;
    1204    43902692 :                 tmp_C2_re[j][j] = 1.0f;
    1205    43902692 :                 tmp_dm_re[j][j] = 1.0f;
    1206             :             }
    1207             : 
    1208    43902692 :             for ( j = 1; j < numch_out; j++ )
    1209             :             {
    1210    34309628 :                 tmp_C1_re[j][0] = hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].pred_re[j - 1];
    1211             :             }
    1212             : 
    1213     9593064 :             if ( active_w == 1 )
    1214             :             {
    1215     5630640 :                 for ( j = 1; j < numch_out; j++ )
    1216             :                 {
    1217     4222980 :                     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];
    1218             :                 }
    1219             : 
    1220     1407660 :                 IVAS_RMULT_FLOAT( tmp_C2_re[0][1], tmp_C1_re[1][0], re );
    1221     1407660 :                 tmp_dm_re[0][0] = 1 + re;
    1222             : 
    1223     1407660 :                 IVAS_RMULT_FLOAT( tmp_C2_re[0][2], tmp_C1_re[2][0], re );
    1224     1407660 :                 tmp_dm_re[0][0] += re;
    1225             : 
    1226     1407660 :                 IVAS_RMULT_FLOAT( tmp_C2_re[0][3], tmp_C1_re[3][0], re );
    1227     1407660 :                 tmp_dm_re[0][0] += re;
    1228             : 
    1229     1407660 :                 if ( dyn_active_w_flag == 1 )
    1230             :                 {
    1231           0 :                     tmp_dm_re[0][0] *= IVAS_SPAR_DYN_ACTIVEW_THRESH;
    1232             :                 }
    1233             : 
    1234     1407660 :                 tmp_dm_re[0][1] = tmp_C2_re[0][1];
    1235             : 
    1236     1407660 :                 tmp_dm_re[0][2] = tmp_C2_re[0][2];
    1237             : 
    1238     1407660 :                 tmp_dm_re[0][3] = tmp_C2_re[0][3];
    1239             : 
    1240     1407660 :                 tmp_dm_re[1][0] = tmp_C1_re[1][0];
    1241             : 
    1242     1407660 :                 tmp_dm_re[2][0] = tmp_C1_re[2][0];
    1243             : 
    1244     1407660 :                 tmp_dm_re[3][0] = tmp_C1_re[3][0];
    1245             : 
    1246     1407660 :                 if ( hMdDec->spar_md_cfg.remix_unmix_order != 3 )
    1247             :                 {
    1248     1407660 :                     ivas_mat_col_rearrange( tmp_dm_re, order, i_ts, hMdDec->mixer_mat, b, numch_out );
    1249             :                 }
    1250             :             }
    1251             :             else
    1252             :             {
    1253     8185404 :                 if ( hMdDec->spar_md_cfg.remix_unmix_order != 3 )
    1254             :                 {
    1255     8185404 :                     ivas_mat_col_rearrange( tmp_C1_re, order, i_ts, hMdDec->mixer_mat, b, numch_out );
    1256             :                 }
    1257             :             }
    1258             : 
    1259     9593064 :             if ( dmx_ch > 0 )
    1260             :             {
    1261             :                 float tmpC_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
    1262             :                 float tmpP_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
    1263             : 
    1264    53495756 :                 for ( j = 0; j < numch_out; j++ )
    1265             :                 {
    1266    43902692 :                     set_zero( tmpC_re[j], numch_out );
    1267    43902692 :                     set_zero( tmpP_re[j], numch_out );
    1268             :                 }
    1269             : 
    1270    53495756 :                 for ( j = 0; j < numch_out; j++ )
    1271             :                 {
    1272    43902692 :                     set_zero( tmpC_re[j], numch_out );
    1273             :                 }
    1274             : 
    1275    36899980 :                 for ( k = 0; k < dmx_ch; k++ )
    1276             :                 {
    1277    27306916 :                     tmpC_re[k][k] = 1;
    1278             :                 }
    1279             : 
    1280    26188840 :                 for ( j = dmx_ch; j < numch_out; j++ )
    1281             :                 {
    1282    41632908 :                     for ( k = 1; k < dmx_ch; k++ )
    1283             :                     {
    1284    25037132 :                         tmpC_re[j][k] = hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].C_re[j - dmx_ch][k - 1];
    1285             :                     }
    1286             :                 }
    1287             : 
    1288    26188840 :                 for ( j = dmx_ch; j < numch_out; j++ )
    1289             :                 {
    1290    68625288 :                     for ( k = dmx_ch; k < numch_out; k++ )
    1291             :                     {
    1292    52029512 :                         if ( ( j - dmx_ch ) == ( k - dmx_ch ) )
    1293             :                         {
    1294    16595776 :                             tmpP_re[j][k] = hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].P_re[k - dmx_ch];
    1295             :                         }
    1296             :                         else
    1297             :                         {
    1298    35433736 :                             tmpP_re[j][k] = 0;
    1299             :                         }
    1300             :                     }
    1301             :                 }
    1302             : 
    1303    43902692 :                 for ( j = 1; j < numch_out; j++ )
    1304             :                 {
    1305   111376272 :                     for ( k = dmx_ch; k < numch_out; k++ )
    1306             :                     {
    1307   617474116 :                         for ( m = 0; m < numch_out; m++ )
    1308             :                         {
    1309   540407472 :                             IVAS_RMULT_FLOAT( hMdDec->mixer_mat[j][m][b + i_ts * IVAS_MAX_NUM_BANDS], tmpP_re[m][k], re );
    1310   540407472 :                             hMdDec->spar_coeffs.P_re[j][k][( b * bw ) + i_ts * IVAS_MAX_NUM_BANDS] += re;
    1311             :                         }
    1312             :                     }
    1313             :                 }
    1314             : 
    1315    53495756 :                 for ( j = 0; j < numch_out; j++ )
    1316             :                 {
    1317   175252100 :                     for ( k = 0; k < dmx_ch; k++ )
    1318             :                     {
    1319   854351280 :                         for ( m = 0; m < numch_out; m++ )
    1320             :                         {
    1321   723001872 :                             IVAS_RMULT_FLOAT( hMdDec->mixer_mat[j][m][b + i_ts * IVAS_MAX_NUM_BANDS], tmpC_re[m][k], re );
    1322   723001872 :                             hMdDec->spar_coeffs.C_re[j][k][( b * bw ) + i_ts * IVAS_MAX_NUM_BANDS] += re;
    1323             :                         }
    1324             :                     }
    1325             :                 }
    1326             : 
    1327     9593064 :                 hMdDec->spar_coeffs.C_re[0][0][( b * bw ) + i_ts * IVAS_MAX_NUM_BANDS] =
    1328     9593064 :                     max( 0, hMdDec->spar_coeffs.C_re[0][0][( b * bw ) + i_ts * IVAS_MAX_NUM_BANDS] );
    1329             :             }
    1330             :         }
    1331             : 
    1332             :         /* band mixing */
    1333      807347 :         if ( bw == IVAS_RED_BAND_FACT )
    1334             :         {
    1335           0 :             for ( b = 0; b < num_bands_out; b = b + bw )
    1336             :             {
    1337           0 :                 dmx_ch = hMdDec->spar_md_cfg.num_dmx_chans_per_band[b];
    1338           0 :                 for ( j = 0; j < numch_out; j++ )
    1339             :                 {
    1340           0 :                     for ( k = dmx_ch; k < numch_out; k++ )
    1341             :                     {
    1342           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];
    1343             :                     }
    1344             :                 }
    1345             : 
    1346           0 :                 for ( j = 0; j < numch_out; j++ )
    1347             :                 {
    1348           0 :                     for ( k = 0; k < dmx_ch; k++ )
    1349             :                     {
    1350           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];
    1351             :                     }
    1352             :                 }
    1353             :             }
    1354             :         }
    1355             :     }
    1356             : 
    1357      230567 :     return;
    1358             : }
    1359             : 
    1360             : 
    1361             : /*-----------------------------------------------------------------------------------------*
    1362             :  * Function ivas_mat_col_rearrange()
    1363             :  *
    1364             :  * reorders the input matrix based on order
    1365             :  *-----------------------------------------------------------------------------------------*/
    1366             : 
    1367     9593064 : static void ivas_mat_col_rearrange(
    1368             :     float in_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH],
    1369             :     const int16_t order[IVAS_SPAR_MAX_CH],
    1370             :     const int16_t i_ts,
    1371             :     float ***mixer_mat,
    1372             :     const int16_t bands,
    1373             :     const int16_t num_ch )
    1374             : {
    1375             :     int16_t i, j, idx;
    1376             : 
    1377    53495756 :     for ( i = 0; i < num_ch; i++ )
    1378             :     {
    1379    43902692 :         idx = order[i];
    1380             : 
    1381   268914520 :         for ( j = 0; j < num_ch; j++ )
    1382             :         {
    1383   225011828 :             mixer_mat[j][i][bands + i_ts * IVAS_MAX_NUM_BANDS] = in_re[j][idx];
    1384             :         }
    1385             :     }
    1386             : 
    1387     9593064 :     return;
    1388             : }
    1389             : 
    1390             : 
    1391             : /*-----------------------------------------------------------------------------------------*
    1392             :  * Function ivas_spar_dec_gen_umx_mat()
    1393             :  *
    1394             :  * generates upmix matrix
    1395             :  *-----------------------------------------------------------------------------------------*/
    1396             : 
    1397      293903 : void ivas_spar_dec_gen_umx_mat(
    1398             :     ivas_spar_md_dec_state_t *hMdDec, /* i/o: SPAR MD decoder handle        */
    1399             :     const int16_t nchan_transport,    /* i  : number of transport channels  */
    1400             :     const int16_t num_bands_out,      /* i  : number of output bands        */
    1401             :     const int16_t bfi,                /* i  : bad frame indicator           */
    1402             :     const int16_t num_md_sub_frames )
    1403             : {
    1404             :     int16_t i, j, b, i_ts, num_out_ch;
    1405             : 
    1406      293903 :     num_out_ch = hMdDec->spar_md_cfg.num_umx_chs;
    1407             : 
    1408     1338685 :     for ( i_ts = 0; i_ts < num_md_sub_frames; i_ts++ )
    1409             :     {
    1410     1044782 :         if ( hMdDec->td_decorr_flag == 1 )
    1411             :         {
    1412     5684290 :             for ( i = 0; i < num_out_ch; i++ )
    1413             :             {
    1414    17332764 :                 for ( j = 0; j < nchan_transport; j++ )
    1415             :                 {
    1416   164038808 :                     for ( b = 0; b < num_bands_out; b++ )
    1417             :                     {
    1418   151345552 :                         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];
    1419             :                     }
    1420             :                 }
    1421             :             }
    1422             : 
    1423     5684290 :             for ( i = 0; i < num_out_ch; i++ )
    1424             :             {
    1425    14640892 :                 for ( j = nchan_transport; j < num_out_ch; j++ )
    1426             :                 {
    1427   129047640 :                     for ( b = 0; b < num_bands_out; b++ )
    1428             :                     {
    1429   119046256 :                         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];
    1430             :                     }
    1431             :                 }
    1432             :             }
    1433             :         }
    1434             :         else
    1435             :         {
    1436           0 :             for ( i = 0; i < num_out_ch; i++ )
    1437             :             {
    1438           0 :                 for ( j = 0; j < nchan_transport; j++ )
    1439             :                 {
    1440           0 :                     for ( b = 0; b < num_bands_out; b++ )
    1441             :                     {
    1442           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];
    1443             :                     }
    1444             :                 }
    1445             :             }
    1446             :         }
    1447             : 
    1448             : #ifdef DEBUG_SBA_MD_DUMP
    1449             :         {
    1450             :             static FILE *f_mat = 0;
    1451             : 
    1452             :             if ( f_mat == 0 )
    1453             :                 f_mat = fopen( "mixer_mat_dec", "w" );
    1454             : 
    1455             :             for ( i = 0; i < num_out_ch; i++ )
    1456             :             {
    1457             :                 for ( j = 0; j < num_out_ch; j++ )
    1458             :                 {
    1459             :                     for ( b = 0; b < num_bands_out; b++ )
    1460             :                     {
    1461             :                         fprintf( f_mat, "%f\n", hMdDec->mixer_mat[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] );
    1462             :                     }
    1463             :                 }
    1464             :             }
    1465             :         }
    1466             : #endif
    1467             :     }
    1468             : 
    1469      293903 :     ivas_spar_dec_compute_ramp_down_post_matrix( hMdDec, num_bands_out, bfi, num_md_sub_frames );
    1470             : 
    1471      293903 :     return;
    1472             : }
    1473             : 
    1474       12348 : static void ivas_spar_md_band_upmix(
    1475             :     ivas_band_coeffs_t *band_coeffs,
    1476             :     int16_t *nB,
    1477             :     int16_t *bands_bw,
    1478             :     int16_t *valid_bands,
    1479             :     int16_t bw_final,
    1480             :     int16_t ndec,
    1481             :     int16_t ndm )
    1482             : {
    1483             :     int16_t i, ii, jj, b, idx, bw_fact;
    1484             : 
    1485       12348 :     bw_fact = *bands_bw / bw_final;
    1486       60612 :     for ( i = *nB - 1; i >= 0; i-- )
    1487             :     {
    1488             : 
    1489      147048 :         for ( b = bw_fact - 1; b >= 0; b-- )
    1490             :         {
    1491       98784 :             idx = i * bw_fact + b;
    1492      395136 :             for ( ii = 0; ii < ndec + ndm - 1; ii++ )
    1493             :             {
    1494      296352 :                 band_coeffs[idx].pred_re[ii] = band_coeffs[i].pred_re[ii];
    1495             :             }
    1496      392584 :             for ( ii = 0; ii < ndec; ii++ )
    1497             :             {
    1498      298904 :                 for ( jj = 0; jj < ndm - 1; jj++ )
    1499             :                 {
    1500        5104 :                     band_coeffs[idx].C_re[ii][jj] = band_coeffs[i].C_re[ii][jj];
    1501             :                 }
    1502             :             }
    1503      392584 :             for ( jj = 0; jj < ndec; jj++ )
    1504             :             {
    1505      293800 :                 band_coeffs[idx].P_re[jj] = band_coeffs[i].P_re[jj];
    1506             :             }
    1507       98784 :             valid_bands[idx] = valid_bands[i];
    1508             :         }
    1509             :     }
    1510       12348 :     *nB = ( *nB ) * ( *bands_bw ) / bw_final;
    1511       12348 :     *bands_bw = bw_final;
    1512             : 
    1513       12348 :     return;
    1514             : }
    1515             : 
    1516             : /*-----------------------------------------------------------------------------------------*
    1517             :  * Function ivas_spar_dec_parse_md_bs()
    1518             :  *
    1519             :  * Parse SPAR MD bitstream
    1520             :  *-----------------------------------------------------------------------------------------*/
    1521             : 
    1522      230567 : static void ivas_spar_dec_parse_md_bs(
    1523             :     ivas_spar_md_dec_state_t *hMdDec,
    1524             :     Decoder_State *st0,
    1525             :     int16_t *nB,
    1526             :     int16_t *bands_bw,
    1527             :     int16_t *dtx_vad,
    1528             :     const int32_t ivas_total_brate,
    1529             :     const int16_t sba_inactive_mode )
    1530             : {
    1531             :     int16_t i, j, k, num_bands;
    1532             :     int16_t ii, jj, ndec, ndm;
    1533             :     uint16_t qsi;
    1534             :     ivas_quant_strat_t qs;
    1535             :     int16_t strat, no_ec;
    1536             :     int16_t do_diff[IVAS_MAX_NUM_BANDS];
    1537             :     float quant[IVAS_SPAR_MAX_C_COEFF];
    1538             :     int16_t do_repeat[IVAS_MAX_NUM_BANDS];
    1539      230567 :     *dtx_vad = 1;
    1540      230567 :     *bands_bw = 1;
    1541      230567 :     qsi = 0;
    1542      230567 :     num_bands = hMdDec->spar_md.num_bands;
    1543             : 
    1544      230567 :     if ( ivas_total_brate > IVAS_SID_5k2 )
    1545             :     {
    1546      230243 :         if ( hMdDec->spar_md_cfg.quant_strat_bits > 0 )
    1547             :         {
    1548      230243 :             if ( ivas_total_brate >= BRATE_SPAR_Q_STRAT )
    1549             :             {
    1550             :                 /*only one bit written for quantization strategy to indicate either a fixed quantization strategy or dtx_vad==0 */
    1551      102876 :                 qsi = get_next_indice( st0, 1 );
    1552      102876 :                 if ( qsi == 1 )
    1553             :                 {
    1554           0 :                     *dtx_vad = 0;
    1555             :                 }
    1556             :             }
    1557             :             else
    1558             :             {
    1559      127367 :                 if ( sba_inactive_mode == 1 )
    1560             :                 {
    1561         240 :                     *dtx_vad = 0;
    1562         240 :                     qsi = hMdDec->spar_md_cfg.quant_strat_bits + 1;
    1563             :                 }
    1564             :                 else
    1565             :                 {
    1566      127127 :                     qsi = get_next_indice( st0, hMdDec->spar_md_cfg.quant_strat_bits );
    1567             :                 }
    1568             :             }
    1569             :         }
    1570             :         else
    1571             :         {
    1572           0 :             qsi = 0;
    1573             :         }
    1574             :     }
    1575             :     else
    1576             :     {
    1577         324 :         *dtx_vad = 0;
    1578             :     }
    1579             : 
    1580      230567 :     hMdDec->dtx_vad = *dtx_vad;
    1581             : 
    1582      230567 :     if ( *dtx_vad == 0 )
    1583             :     {
    1584         564 :         *nB = SPAR_DTX_BANDS;
    1585         564 :         *bands_bw = num_bands / *nB;
    1586             : 
    1587        1692 :         for ( i = 0; i < *nB; i++ )
    1588             :         {
    1589       12408 :             for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
    1590             :             {
    1591       11280 :                 hMdDec->spar_md.band_coeffs[i].pred_re[j] = 0;
    1592       11280 :                 hMdDec->spar_md.band_coeffs[i].P_re[j] = 0;
    1593             :             }
    1594        1128 :             hMdDec->valid_bands[i] = 1;
    1595             :         }
    1596             : 
    1597        5076 :         for ( i = 0; i < num_bands; i++ )
    1598             :         {
    1599       36096 :             for ( j = 0; j < ( IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS ); j++ )
    1600             :             {
    1601      126336 :                 for ( k = 0; k < ( IVAS_SPAR_MAX_DMX_CHS - 1 ); k++ )
    1602             :                 {
    1603       94752 :                     hMdDec->spar_md.band_coeffs[i].C_re[j][k] = 0;
    1604             :                 }
    1605             :             }
    1606             :         }
    1607             : 
    1608         564 :         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 );
    1609             : 
    1610         564 :         if ( *bands_bw != 1 )
    1611             :         {
    1612         564 :             ndec = hMdDec->spar_md_cfg.num_decorr_per_band[0];
    1613         564 :             ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[0];
    1614         564 :             ivas_spar_md_band_upmix(
    1615             :                 hMdDec->spar_md.band_coeffs,
    1616             :                 nB,
    1617             :                 bands_bw,
    1618         564 :                 hMdDec->valid_bands,
    1619             :                 1,
    1620             :                 ndec,
    1621             :                 ndm );
    1622             :         }
    1623             : 
    1624         564 :         return;
    1625             :     }
    1626             : 
    1627      230003 :     qs = hMdDec->spar_md_cfg.quant_strat[qsi];
    1628             : 
    1629      230003 :     strat = get_next_indice( st0, 3 );
    1630             : 
    1631      230003 :     no_ec = 0;
    1632             : 
    1633      230003 :     if ( strat < 2 )
    1634             :     {
    1635      115498 :         *bands_bw = strat + 1;
    1636      115498 :         *nB = num_bands / *bands_bw;
    1637     1225994 :         for ( i = 0; i < *nB; i++ )
    1638             :         {
    1639     1110496 :             do_diff[i] = 0;
    1640     1110496 :             do_repeat[i] = 0;
    1641             :         }
    1642             :     }
    1643      114505 :     else if ( strat < 4 )
    1644             :     {
    1645       10365 :         *bands_bw = strat - 1;
    1646       10365 :         *nB = num_bands / *bands_bw;
    1647       92873 :         for ( i = 0; i < *nB; i++ )
    1648             :         {
    1649       82508 :             do_diff[i] = 0;
    1650       82508 :             do_repeat[i] = 0;
    1651             :         }
    1652       10365 :         no_ec = 1;
    1653             :     }
    1654      104140 :     else if ( ivas_total_brate < IVAS_24k4 )
    1655             :     {
    1656       11351 :         *bands_bw = 2;
    1657       11351 :         *nB = num_bands / *bands_bw;
    1658             : 
    1659       56755 :         for ( i = 0; i < *nB; i++ )
    1660             :         {
    1661       45404 :             do_diff[i] = 0;
    1662       45404 :             do_repeat[i] = ( ( strat % 2 ) == ( ( i + 1 ) % 2 ) );
    1663             :         }
    1664             :     }
    1665             :     else
    1666             :     {
    1667       92789 :         *bands_bw = 1;
    1668       92789 :         *nB = num_bands;
    1669             : 
    1670      835101 :         for ( i = 0; i < *nB; i++ )
    1671             :         {
    1672      742312 :             do_diff[i] = ( ( ( i + 1 ) & 3 ) != strat - 4 );
    1673      742312 :             do_repeat[i] = 0;
    1674             :         }
    1675       92789 :         if ( hMdDec->spar_md_cfg.prev_quant_idx >= 0 )
    1676             :         {
    1677       92768 :             ivas_map_prior_coeffs_quant( &hMdDec->spar_md_prev, &hMdDec->spar_md_cfg, qsi, *nB );
    1678             :         }
    1679             :     }
    1680             : #ifdef SPAR_HOA_DBG
    1681             :     fprintf( stdout, "\n\n no_ec = %d, strat = %d\n", no_ec, strat );
    1682             : #endif
    1683      230003 :     hMdDec->spar_md_cfg.prev_quant_idx = qsi;
    1684             : 
    1685      230003 :     if ( no_ec == 0 )
    1686             :     {
    1687      219638 :         ivas_decode_arith_bs( hMdDec, st0, qsi, *nB, *bands_bw, do_diff, strat, ivas_total_brate );
    1688             :     }
    1689             :     else
    1690             :     {
    1691       10365 :         ivas_decode_huffman_bs( hMdDec, st0, qsi, *nB, *bands_bw );
    1692             :     }
    1693             : 
    1694     2210723 :     for ( i = 0; i < *nB; i++ )
    1695             :     {
    1696     1980720 :         ndec = hMdDec->spar_md_cfg.num_decorr_per_band[( *bands_bw ) * i];
    1697     1980720 :         ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[( *bands_bw ) * i];
    1698             : 
    1699     1980720 :         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 );
    1700             : 
    1701     1980720 :         j = 0;
    1702     6926164 :         for ( ii = 0; ii < ndec; ii++ )
    1703             :         {
    1704    15402880 :             for ( jj = 0; jj < ndm - 1; jj++ )
    1705             :             {
    1706    10457436 :                 quant[j] = hMdDec->spar_md.band_coeffs[i].C_re[ii][jj];
    1707    10457436 :                 j++;
    1708             :             }
    1709             :         }
    1710             : 
    1711     1980720 :         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 ) );
    1712             : 
    1713     1980720 :         j = 0;
    1714     6926164 :         for ( ii = 0; ii < ndec; ii++ )
    1715             :         {
    1716    15402880 :             for ( jj = 0; jj < ndm - 1; jj++ )
    1717             :             {
    1718    10457436 :                 hMdDec->spar_md.band_coeffs[i].C_re[ii][jj] = quant[j];
    1719    10457436 :                 j++;
    1720             :             }
    1721             :         }
    1722             : 
    1723     1980720 :         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 );
    1724             : 
    1725             :         /* Store prior coefficient indices */
    1726    10936788 :         for ( j = 0; j < ndm + ndec - 1; j++ )
    1727             :         {
    1728     8956068 :             hMdDec->spar_md_prev.band_coeffs_idx[i].pred_index_re[j] = hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j];
    1729             :         }
    1730    12438156 :         for ( j = 0; j < ndec * ( ndm - 1 ); j++ )
    1731             :         {
    1732    10457436 :             hMdDec->spar_md_prev.band_coeffs_idx[i].drct_index_re[j] = hMdDec->spar_md.band_coeffs_idx[i].drct_index_re[j];
    1733             :         }
    1734     6926164 :         for ( j = 0; j < ndec; j++ )
    1735             :         {
    1736     4945444 :             hMdDec->spar_md_prev.band_coeffs_idx[i].decd_index_re[j] = hMdDec->spar_md.band_coeffs_idx[i].decd_index_re[j];
    1737             :         }
    1738     1980720 :         hMdDec->valid_bands[i] |= ( do_diff[i] == 0 && do_repeat[i] == 0 ) ? 1 : 0;
    1739             :     }
    1740             : 
    1741      230003 :     ndec = hMdDec->spar_md_cfg.num_decorr_per_band[0];
    1742      230003 :     ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[0];
    1743      230003 :     if ( *bands_bw != 1 )
    1744             :     {
    1745       11784 :         ivas_spar_md_band_upmix(
    1746             :             hMdDec->spar_md.band_coeffs,
    1747             :             nB,
    1748             :             bands_bw,
    1749       11784 :             hMdDec->valid_bands,
    1750             :             1,
    1751             :             ndec,
    1752             :             ndm );
    1753             :     }
    1754             : 
    1755             : 
    1756      230003 :     return;
    1757             : }
    1758             : 
    1759             : 
    1760             : /*-----------------------------------------------------------------------------------------*
    1761             :  * Function ivas_decode_arith_bs()
    1762             :  *
    1763             :  * Decode bitstream with arith decoder
    1764             :  *-----------------------------------------------------------------------------------------*/
    1765             : 
    1766      219638 : static void ivas_decode_arith_bs(
    1767             :     ivas_spar_md_dec_state_t *hMdDec,
    1768             :     Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
    1769             :     const uint16_t qsi,
    1770             :     const int16_t nB,
    1771             :     const int16_t bands_bw,
    1772             :     int16_t *pDo_diff,
    1773             :     const int16_t strat,
    1774             :     const int32_t ivas_total_brate )
    1775             : {
    1776             :     int16_t i, ndm, ndec;
    1777             :     int16_t j;
    1778             :     ivas_cell_dim_t pred_cell_dims[IVAS_MAX_NUM_BANDS];
    1779             :     ivas_cell_dim_t drct_cell_dims[IVAS_MAX_NUM_BANDS];
    1780             :     ivas_cell_dim_t decd_cell_dims[IVAS_MAX_NUM_BANDS];
    1781             :     ivas_cell_dim_t decx_cell_dims[IVAS_MAX_NUM_BANDS];
    1782             :     int16_t symbol_arr_re[IVAS_MAX_INPUT_LEN];
    1783             :     int16_t symbol_arr_old_re[IVAS_MAX_INPUT_LEN];
    1784             :     int16_t any_diff;
    1785             : 
    1786     2117850 :     for ( i = 0; i < nB; i++ )
    1787             :     {
    1788     1898212 :         ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[bands_bw * i];
    1789     1898212 :         ndec = hMdDec->spar_md_cfg.num_decorr_per_band[bands_bw * i];
    1790     1898212 :         if ( ( ivas_total_brate < IVAS_24k4 ) && ( strat > 3 ) && ( ( ( i % 2 == 1 ) && ( strat % 2 == 0 ) ) || ( ( i % 2 == 0 ) && ( strat % 2 == 1 ) ) ) )
    1791             :         {
    1792       22702 :             pred_cell_dims[i].dim1 = 0;
    1793       22702 :             pred_cell_dims[i].dim2 = 0;
    1794       22702 :             drct_cell_dims[i].dim1 = 0;
    1795       22702 :             drct_cell_dims[i].dim2 = 0;
    1796       22702 :             decd_cell_dims[i].dim1 = 0;
    1797       22702 :             decd_cell_dims[i].dim2 = 0;
    1798       22702 :             decx_cell_dims[i].dim1 = 0;
    1799       22702 :             decx_cell_dims[i].dim2 = 0;
    1800             :         }
    1801             :         else
    1802             :         {
    1803     1875510 :             pred_cell_dims[i].dim1 = ndm + ndec - 1;
    1804     1875510 :             if ( hMdDec->spar_hoa_md_flag && hMdDec->spar_hoa_dirac2spar_md_flag )
    1805             :             {
    1806      245556 :                 if ( i >= SPAR_DIRAC_SPLIT_START_BAND )
    1807             :                 {
    1808       81852 :                     pred_cell_dims[i].dim1 -= ( FOA_CHANNELS - 1 );
    1809             :                 }
    1810             :             }
    1811     1875510 :             pred_cell_dims[i].dim2 = 1;
    1812     1875510 :             drct_cell_dims[i].dim1 = ndec;
    1813     1875510 :             drct_cell_dims[i].dim2 = ndm - 1;
    1814     1875510 :             decd_cell_dims[i].dim1 = ndec;
    1815     1875510 :             decd_cell_dims[i].dim2 = 1;
    1816     1875510 :             decx_cell_dims[i].dim1 = ( ndec * ( ndec - 1 ) ) >> 1;
    1817     1875510 :             decx_cell_dims[i].dim2 = 1;
    1818             :         }
    1819             :     }
    1820             : 
    1821      219638 :     any_diff = 0;
    1822     1398877 :     for ( i = 0; i < nB; i++ )
    1823             :     {
    1824     1272028 :         if ( pDo_diff[i] != 0 )
    1825             :         {
    1826       92789 :             any_diff = 1;
    1827       92789 :             break;
    1828             :         }
    1829             :     }
    1830             : 
    1831      219638 :     if ( any_diff == 1 )
    1832             :     {
    1833       92789 :         if ( hMdDec->spar_hoa_md_flag && hMdDec->spar_hoa_dirac2spar_md_flag )
    1834             :         {
    1835           0 :             for ( i = 0; i < nB; i++ )
    1836             :             {
    1837           0 :                 if ( i >= SPAR_DIRAC_SPLIT_START_BAND )
    1838             :                 {
    1839           0 :                     for ( j = 0; j < pred_cell_dims[i].dim1; j++ )
    1840             :                     {
    1841           0 :                         hMdDec->spar_md_prev.band_coeffs_idx_mapped[i].pred_index_re[j] =
    1842           0 :                             hMdDec->spar_md_prev.band_coeffs_idx_mapped[i].pred_index_re[j + ( FOA_CHANNELS - 1 )];
    1843             :                     }
    1844             :                 }
    1845             :             }
    1846             :         }
    1847             : 
    1848       92789 :         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 );
    1849             :     }
    1850             : 
    1851      219638 :     ivas_arith_decode_cmplx_cell_array( &hMdDec->arith_coeffs.pred_arith_re[qsi], &hMdDec->arith_coeffs.pred_arith_re_diff[qsi],
    1852             :                                         st0, pred_cell_dims, pDo_diff, nB, symbol_arr_re, symbol_arr_old_re );
    1853             : 
    1854      219638 :     ivas_fill_band_coeffs_idx( hMdDec->spar_md.band_coeffs_idx, nB, symbol_arr_re, pred_cell_dims, PRED_COEFF );
    1855             : 
    1856      219638 :     if ( hMdDec->spar_hoa_md_flag && hMdDec->spar_hoa_dirac2spar_md_flag )
    1857             :     {
    1858      266019 :         for ( i = 0; i < nB; i++ )
    1859             :         {
    1860      245556 :             if ( i >= SPAR_DIRAC_SPLIT_START_BAND )
    1861             :             {
    1862      363244 :                 for ( j = pred_cell_dims[i].dim1 - 1; j >= 0; j-- )
    1863             :                 {
    1864      281392 :                     hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j + ( FOA_CHANNELS - 1 )] =
    1865      281392 :                         hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j];
    1866             :                 }
    1867      327408 :                 for ( j = 0; j < FOA_CHANNELS - 1; j++ )
    1868             :                 {
    1869      245556 :                     hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j] = 0;
    1870             :                 }
    1871             :             }
    1872             :         }
    1873             :     }
    1874             : 
    1875      219638 :     if ( any_diff == 1 )
    1876             :     {
    1877       92789 :         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 );
    1878             :     }
    1879             : 
    1880      219638 :     ivas_arith_decode_cmplx_cell_array( &hMdDec->arith_coeffs.drct_arith_re[qsi], &hMdDec->arith_coeffs.drct_arith_re_diff[qsi],
    1881             :                                         st0, drct_cell_dims, pDo_diff, nB, symbol_arr_re, symbol_arr_old_re );
    1882             : 
    1883      219638 :     ivas_fill_band_coeffs_idx( hMdDec->spar_md.band_coeffs_idx, nB, symbol_arr_re, drct_cell_dims, DRCT_COEFF );
    1884             : 
    1885      219638 :     if ( any_diff == 1 )
    1886             :     {
    1887       92789 :         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 );
    1888             :     }
    1889             : 
    1890      219638 :     ivas_arith_decode_cmplx_cell_array( &hMdDec->arith_coeffs.decd_arith_re[qsi], &hMdDec->arith_coeffs.decd_arith_re_diff[qsi],
    1891             :                                         st0, decd_cell_dims, pDo_diff, nB, symbol_arr_re, symbol_arr_old_re );
    1892             : 
    1893      219638 :     ivas_fill_band_coeffs_idx( hMdDec->spar_md.band_coeffs_idx, nB, symbol_arr_re, decd_cell_dims, DECD_COEFF );
    1894             : 
    1895      219638 :     if ( any_diff == 1 )
    1896             :     {
    1897       92789 :         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 );
    1898             :     }
    1899             : 
    1900      219638 :     ivas_fill_band_coeffs_idx( hMdDec->spar_md.band_coeffs_idx, nB, symbol_arr_re, decx_cell_dims, DECX_COEFF );
    1901             : 
    1902      219638 :     return;
    1903             : }
    1904             : 
    1905             : 
    1906             : /*-----------------------------------------------------------------------------------------*
    1907             :  * Function ivas_fill_band_coeffs_idx()
    1908             :  *
    1909             :  * Copy pred band coeffs to arr
    1910             :  *-----------------------------------------------------------------------------------------*/
    1911             : 
    1912      878552 : static void ivas_fill_band_coeffs_idx(
    1913             :     ivas_band_coeffs_ind_t *pBands_idx,
    1914             :     const int16_t nB,
    1915             :     int16_t *pSymbol_re,
    1916             :     ivas_cell_dim_t *pCell_dims,
    1917             :     const ivas_coeffs_type_t coeff_type )
    1918             : {
    1919             :     int16_t i, len;
    1920      878552 :     int16_t *pPtr_idx = NULL;
    1921             : 
    1922     8471400 :     for ( i = 0; i < nB; i++ )
    1923             :     {
    1924     7592848 :         switch ( coeff_type )
    1925             :         {
    1926     1898212 :             case PRED_COEFF:
    1927             :             {
    1928     1898212 :                 pPtr_idx = pBands_idx[i].pred_index_re;
    1929     1898212 :                 break;
    1930             :             }
    1931     1898212 :             case DRCT_COEFF:
    1932             :             {
    1933     1898212 :                 pPtr_idx = pBands_idx[i].drct_index_re;
    1934     1898212 :                 break;
    1935             :             }
    1936     1898212 :             case DECD_COEFF:
    1937             :             {
    1938     1898212 :                 pPtr_idx = pBands_idx[i].decd_index_re;
    1939     1898212 :                 break;
    1940             :             }
    1941     1898212 :             case DECX_COEFF:
    1942             :             {
    1943     1898212 :                 break;
    1944             :             }
    1945             : 
    1946           0 :             default:
    1947           0 :                 assert( !"unsupported config!" );
    1948             :         }
    1949             : 
    1950     7592848 :         if ( coeff_type != DECX_COEFF )
    1951             :         {
    1952     5694636 :             len = pCell_dims[i].dim1 * pCell_dims[i].dim2;
    1953     5694636 :             mvs2s( pSymbol_re, pPtr_idx, len );
    1954     5694636 :             pSymbol_re += len;
    1955             :         }
    1956             :     }
    1957             : 
    1958      878552 :     return;
    1959             : }
    1960             : 
    1961             : 
    1962             : /*-----------------------------------------------------------------------------------------*
    1963             :  * Function ivas_decode_huffman_bs()
    1964             :  *
    1965             :  * Decode bitstream with huffman decoder
    1966             :  *-----------------------------------------------------------------------------------------*/
    1967             : 
    1968       10365 : static void ivas_decode_huffman_bs(
    1969             :     ivas_spar_md_dec_state_t *hMdDec,
    1970             :     Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
    1971             :     const uint16_t qsi,
    1972             :     const int16_t nB,
    1973             :     const int16_t bands_bw )
    1974             : {
    1975             :     int16_t i, j;
    1976             :     int16_t ndm, ndec;
    1977             :     int16_t pred_dim, drct_dim, decd_dim, pred_offset;
    1978             : 
    1979       92873 :     for ( i = 0; i < nB; i++ )
    1980             :     {
    1981       82508 :         ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[bands_bw * i];
    1982       82508 :         ndec = hMdDec->spar_md_cfg.num_decorr_per_band[bands_bw * i];
    1983             : 
    1984       82508 :         pred_dim = ndec + ndm - 1;
    1985       82508 :         drct_dim = ndec * ( ndm - 1 );
    1986       82508 :         decd_dim = ndec;
    1987       82508 :         pred_offset = 0;
    1988             : 
    1989       82508 :         if ( hMdDec->spar_hoa_md_flag && hMdDec->spar_hoa_dirac2spar_md_flag )
    1990             :         {
    1991           0 :             if ( i >= SPAR_DIRAC_SPLIT_START_BAND )
    1992             :             {
    1993           0 :                 pred_offset = FOA_CHANNELS - 1;
    1994             :             }
    1995             :         }
    1996             : 
    1997      330032 :         for ( j = pred_offset; j < pred_dim; j++ )
    1998             :         {
    1999      247524 :             ivas_huffman_decode( &hMdDec->huff_coeffs.pred_huff_re[qsi], st0, &hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j] );
    2000             :         }
    2001             : 
    2002       82508 :         if ( hMdDec->spar_hoa_md_flag && hMdDec->spar_hoa_dirac2spar_md_flag )
    2003             :         {
    2004           0 :             if ( i >= SPAR_DIRAC_SPLIT_START_BAND )
    2005             :             {
    2006           0 :                 for ( j = 0; j < pred_offset; j++ )
    2007             :                 {
    2008           0 :                     hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j] = 0;
    2009             :                 }
    2010             :             }
    2011             :         }
    2012             : 
    2013       84972 :         for ( j = 0; j < drct_dim; j++ )
    2014             :         {
    2015        2464 :             ivas_huffman_decode( &hMdDec->huff_coeffs.drct_huff_re[qsi], st0, &hMdDec->spar_md.band_coeffs_idx[i].drct_index_re[j] );
    2016             :         }
    2017             : 
    2018       86984 :         for ( j = 0; j < decd_dim; j++ )
    2019             :         {
    2020        4476 :             ivas_huffman_decode( &hMdDec->huff_coeffs.decd_huff_re[qsi], st0, &hMdDec->spar_md.band_coeffs_idx[i].decd_index_re[j] );
    2021             :         }
    2022             :     }
    2023             : 
    2024       10365 :     return;
    2025             : }
    2026             : 
    2027      461134 : static void ivas_spar_plc_get_band_age(
    2028             :     const int16_t *valid_bands,
    2029             :     int16_t *base_band_age,
    2030             :     const int16_t num_bands,
    2031             :     int16_t last_valid_band_idx[IVAS_MAX_NUM_BANDS],
    2032             :     int16_t valid_band_idx[IVAS_MAX_NUM_BANDS],
    2033             :     int16_t *all_valid,
    2034             :     int16_t *b_idx )
    2035             : {
    2036             :     int16_t b, idx;
    2037             : 
    2038      461134 :     set_s( valid_band_idx, 0, IVAS_MAX_NUM_BANDS );
    2039      461134 :     set_s( last_valid_band_idx, 0, IVAS_MAX_NUM_BANDS );
    2040      461134 :     idx = -1;
    2041      461134 :     *all_valid = 1;
    2042     5236198 :     for ( b = 0; b < num_bands; b++ )
    2043             :     {
    2044     4775064 :         if ( valid_bands[b] != 0 )
    2045             :         {
    2046     4727892 :             base_band_age[b] = 0; /* reset band age */
    2047     4727892 :             idx++;
    2048     4727892 :             valid_band_idx[idx] = b;
    2049             :         }
    2050             :         else
    2051             :         {
    2052       47172 :             base_band_age[b] += 1; /* increment the age of invalid bands */
    2053             : 
    2054       47172 :             if ( base_band_age[b] > 3 )
    2055             :             {
    2056        5460 :                 last_valid_band_idx[b] = idx;
    2057             :             }
    2058       47172 :             *all_valid = 0;
    2059             :         }
    2060             :     }
    2061      461134 :     *b_idx = idx;
    2062             : 
    2063      461134 :     return;
    2064             : }
    2065             : 
    2066        5460 : static void ivas_spar_get_plc_interp_weights(
    2067             :     int16_t valid_band_idx[IVAS_MAX_NUM_BANDS],
    2068             :     int16_t last_valid_band_idx,
    2069             :     int16_t idx,
    2070             :     int16_t b,
    2071             :     float *w,
    2072             :     int16_t *id0,
    2073             :     int16_t *id1 )
    2074             : {
    2075        5460 :     if ( last_valid_band_idx < 0 ) /* Extrapolation */
    2076             :     {
    2077        1566 :         *id1 = valid_band_idx[0];
    2078        1566 :         *id0 = 0;
    2079        1566 :         *w = 1;
    2080             :     }
    2081        3894 :     else if ( last_valid_band_idx == idx ) /* Extrapolation */
    2082             :     {
    2083         427 :         *id1 = valid_band_idx[last_valid_band_idx];
    2084         427 :         *id0 = valid_band_idx[last_valid_band_idx];
    2085         427 :         *w = 0;
    2086             :     }
    2087             :     else /* Interpolation */
    2088             :     {
    2089        3467 :         *id0 = valid_band_idx[last_valid_band_idx];
    2090        3467 :         *id1 = valid_band_idx[last_valid_band_idx + 1];
    2091        3467 :         *w = ( (float) ( b - *id0 ) ) / ( *id1 - *id0 );
    2092             :     }
    2093        5460 :     return;
    2094             : }
    2095             : 
    2096             : /*-----------------------------------------------------------------------------------------*
    2097             :  * Function ivas_spar_md_fill_invalid_bands()
    2098             :  *
    2099             :  * Fill invalid bands in interpolation/extrapolation of valid bands
    2100             :  * when PLC is to be done with partial time differential coding
    2101             :  *-----------------------------------------------------------------------------------------*/
    2102      230567 : static void ivas_spar_md_fill_invalid_bands(
    2103             :     ivas_spar_dec_matrices_t *pSpar_coeffs,
    2104             :     ivas_spar_dec_matrices_t *pSpar_coeffs_prev,
    2105             :     const int16_t *valid_bands,
    2106             :     int16_t *base_band_age,
    2107             :     const int16_t num_bands,
    2108             :     const int16_t num_channels,
    2109             :     const int16_t num_md_sub_frames )
    2110             : {
    2111             :     int16_t i, j, b, all_valid;
    2112      230567 :     int16_t valid_band_idx[IVAS_MAX_NUM_BANDS], idx = -1;
    2113             :     int16_t last_valid_band_idx[IVAS_MAX_NUM_BANDS];
    2114      230567 :     float w = 0;
    2115      230567 :     ivas_spar_plc_get_band_age( valid_bands, base_band_age, num_bands,
    2116             :                                 last_valid_band_idx, valid_band_idx, &all_valid, &idx );
    2117      230567 :     assert( idx > 0 ); /* some bands should be valid */
    2118             : 
    2119      230567 :     if ( all_valid == 0 )
    2120             :     {
    2121       67438 :         for ( b = 0; b < num_bands; b++ )
    2122             :         {
    2123             :             /* check against non zero in if and else if */
    2124       62078 :             if ( base_band_age[b] > 3 ) /* old invalid bands */
    2125             :             {
    2126             :                 int16_t id0, id1;
    2127        2730 :                 ivas_spar_get_plc_interp_weights( valid_band_idx, last_valid_band_idx[b],
    2128             :                                                   idx, b, &w, &id0, &id1 );
    2129       13650 :                 for ( i = 0; i < num_channels; i++ )
    2130             :                 {
    2131       54600 :                     for ( j = 0; j < num_channels; j++ )
    2132             :                     {
    2133       43680 :                         pSpar_coeffs->C_re[i][j][b] = ( 1 - w ) * pSpar_coeffs->C_re[i][j][id0] + w * pSpar_coeffs->C_re[i][j][id1];
    2134       43680 :                         pSpar_coeffs->P_re[i][j][b] = ( 1 - w ) * pSpar_coeffs->P_re[i][j][id0] + w * pSpar_coeffs->P_re[i][j][id1];
    2135             :                     }
    2136             :                 }
    2137             :             }
    2138             :             else /* young invalid bands */
    2139             :             {
    2140       59348 :                 if ( valid_bands[b] == 0 )
    2141             :                 {
    2142      104280 :                     for ( i = 0; i < num_channels; i++ )
    2143             :                     {
    2144      417120 :                         for ( j = 0; j < num_channels; j++ )
    2145             :                         {
    2146      333696 :                             pSpar_coeffs->C_re[i][j][b] = pSpar_coeffs_prev->C_re[i][j][b];
    2147      333696 :                             pSpar_coeffs->P_re[i][j][b] = pSpar_coeffs_prev->P_re[i][j][b];
    2148             :                         }
    2149             :                     }
    2150             :                 }
    2151             :             }
    2152             : 
    2153       62078 :             if ( valid_bands[b] == 0 )
    2154             :             {
    2155             :                 int16_t i_ts;
    2156      117930 :                 for ( i = 0; i < num_channels; i++ )
    2157             :                 {
    2158      471720 :                     for ( j = 0; j < num_channels; j++ )
    2159             :                     {
    2160     1488672 :                         for ( i_ts = 1; i_ts < num_md_sub_frames; i_ts++ )
    2161             :                         {
    2162     1111296 :                             pSpar_coeffs->C_re[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] = pSpar_coeffs->C_re[i][j][b];
    2163     1111296 :                             pSpar_coeffs->P_re[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] = pSpar_coeffs->P_re[i][j][b];
    2164             :                         }
    2165             :                     }
    2166             :                 }
    2167             :             }
    2168             :         }
    2169             :     }
    2170             : 
    2171      230567 :     return;
    2172             : }
    2173             : 
    2174      230567 : static void ivas_spar_md_fill_invalid_bandcoeffs(
    2175             :     ivas_band_coeffs_t *pBand_coeffs,
    2176             :     ivas_band_coeffs_t *pBand_coeffs_prev,
    2177             :     const int16_t *valid_bands,
    2178             :     int16_t *base_band_age,
    2179             :     int16_t *first_valid_frame,
    2180             :     const int16_t num_bands )
    2181             : {
    2182             :     int16_t j, k, b, all_valid;
    2183      230567 :     int16_t valid_band_idx[IVAS_MAX_NUM_BANDS], idx = -1;
    2184             :     int16_t last_valid_band_idx[IVAS_MAX_NUM_BANDS];
    2185      230567 :     float w = 0;
    2186             : 
    2187      230567 :     ivas_spar_plc_get_band_age( valid_bands, base_band_age, num_bands,
    2188             :                                 last_valid_band_idx, valid_band_idx, &all_valid, &idx );
    2189             : 
    2190      230567 :     assert( idx > 0 ); /* some bands should be valid */
    2191             : 
    2192      230567 :     if ( all_valid == 0 )
    2193             :     {
    2194       48240 :         for ( b = 0; b < num_bands; b++ )
    2195             :         {
    2196             :             /* check against non zero in if and else if */
    2197       42880 :             if ( ( base_band_age[b] > 3 ) || ( *first_valid_frame == 0 ) ) /* old invalid bands */
    2198        2730 :             {
    2199             :                 int16_t id0, id1;
    2200        2730 :                 ivas_spar_get_plc_interp_weights( valid_band_idx, last_valid_band_idx[b],
    2201             :                                                   idx, b, &w, &id0, &id1 );
    2202             : 
    2203       30030 :                 for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
    2204             :                 {
    2205       27300 :                     pBand_coeffs[b].pred_re[j] = ( 1 - w ) * pBand_coeffs[id0].pred_re[j] + w * pBand_coeffs[id1].pred_re[j];
    2206             :                 }
    2207             : 
    2208       21840 :                 for ( j = 0; j < IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS; j++ )
    2209             :                 {
    2210       76440 :                     for ( k = 0; k < IVAS_SPAR_MAX_DMX_CHS - 1; k++ )
    2211             :                     {
    2212       57330 :                         pBand_coeffs[b].C_re[j][k] = ( 1 - w ) * pBand_coeffs[id0].C_re[j][k] + w * pBand_coeffs[id1].C_re[j][k];
    2213             :                     }
    2214             :                 }
    2215             : 
    2216       30030 :                 for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
    2217             :                 {
    2218       27300 :                     pBand_coeffs[b].P_re[j] = ( 1 - w ) * pBand_coeffs[id0].P_re[j] + w * pBand_coeffs[id1].P_re[j];
    2219             :                 }
    2220             :             }
    2221             :             else /* young invalid bands */
    2222             :             {
    2223       40150 :                 if ( valid_bands[b] == 0 )
    2224             :                 {
    2225      229416 :                     for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
    2226             :                     {
    2227      208560 :                         pBand_coeffs[b].pred_re[j] = pBand_coeffs_prev[b].pred_re[j];
    2228             :                     }
    2229             : 
    2230      166848 :                     for ( j = 0; j < IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS; j++ )
    2231             :                     {
    2232      583968 :                         for ( k = 0; k < IVAS_SPAR_MAX_DMX_CHS - 1; k++ )
    2233             :                         {
    2234      437976 :                             pBand_coeffs[b].C_re[j][k] = pBand_coeffs_prev[b].C_re[j][k];
    2235             :                         }
    2236             :                     }
    2237             : 
    2238      229416 :                     for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
    2239             :                     {
    2240      208560 :                         pBand_coeffs[b].P_re[j] = pBand_coeffs_prev[b].P_re[j];
    2241             :                     }
    2242             :                 }
    2243             :             }
    2244             :         }
    2245             :     }
    2246             :     else
    2247             :     {
    2248      225207 :         *first_valid_frame = 1;
    2249             :     }
    2250             : 
    2251      230567 :     return;
    2252             : }
    2253             : 
    2254             : 
    2255             : /*-----------------------------------------------------------------------------------------*
    2256             :  * Function ivas_spar_dec_compute_ramp_down_post_matrix()
    2257             :  *
    2258             :  *
    2259             :  *-----------------------------------------------------------------------------------------*/
    2260             : 
    2261      293903 : static void ivas_spar_dec_compute_ramp_down_post_matrix(
    2262             :     ivas_spar_md_dec_state_t *hMdDec,
    2263             :     const int16_t num_bands_out,
    2264             :     const int16_t bfi,
    2265             :     const int16_t num_md_sub_frames )
    2266             : {
    2267             :     int16_t num_in_ch, num_out_ch, i, j, b;
    2268             : 
    2269      293903 :     num_in_ch = hMdDec->spar_md_cfg.num_umx_chs;
    2270      293903 :     num_out_ch = hMdDec->spar_md_cfg.num_umx_chs;
    2271             : 
    2272      293903 :     if ( bfi == 0 )
    2273             :     {
    2274      283893 :         hMdDec->spar_plc_num_lost_frames = 0;
    2275             :     }
    2276             :     else
    2277             :     {
    2278       10010 :         if ( hMdDec->td_decorr_flag == 0 )
    2279             :         {
    2280           0 :             assert( 0 );
    2281             :         }
    2282             : 
    2283       10010 :         hMdDec->spar_plc_num_lost_frames += 1;
    2284       10010 :         hMdDec->spar_plc_num_lost_frames = min( hMdDec->spar_plc_num_lost_frames, 100 );
    2285             : 
    2286       10010 :         if ( hMdDec->spar_plc_num_lost_frames > ivas_spar_dec_plc_num_frames_keep )
    2287             :         {
    2288             :             int16_t num_fade_frames;
    2289             :             int16_t gain_dB;
    2290             :             float gain;
    2291             :             float post_matrix[IVAS_SPAR_MAX_CH];
    2292             : 
    2293        2038 :             num_fade_frames = max( hMdDec->spar_plc_num_lost_frames - ivas_spar_dec_plc_num_frames_keep, 0 );
    2294        2038 :             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;
    2295        2038 :             gain = powf( 10, ( ( (float) gain_dB ) / 20 ) );
    2296             : 
    2297       24456 :             for ( i = 0; i < IVAS_SPAR_MAX_CH; i++ )
    2298             :             {
    2299       22418 :                 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 );
    2300       22418 :                 post_matrix[i] *= gain;
    2301             :             }
    2302             : 
    2303             :             /* apply the post matrix */
    2304       10190 :             for ( int16_t i_ts = 0; i_ts < num_md_sub_frames; i_ts++ )
    2305             :             {
    2306       40760 :                 for ( i = 0; i < num_out_ch; i++ )
    2307             :                 {
    2308      163040 :                     for ( j = 0; j < num_in_ch; j++ )
    2309             :                     {
    2310     1611648 :                         for ( b = 0; b < num_bands_out; b++ )
    2311             :                         {
    2312     1481216 :                             hMdDec->mixer_mat[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] *= post_matrix[i];
    2313             :                         }
    2314             :                     }
    2315             :                 }
    2316             :             }
    2317             :         }
    2318             :     }
    2319             : 
    2320      293903 :     return;
    2321             : }
    2322             : 
    2323             : 
    2324             : /*-----------------------------------------------------------------------------------------*
    2325             :  * Function ivas_spar_unquant_dtx_indicies()
    2326             :  *
    2327             :  * Unquantize SPAR MD DYX indices
    2328             :  *-----------------------------------------------------------------------------------------*/
    2329             : 
    2330         564 : static void ivas_spar_unquant_dtx_indicies(
    2331             :     ivas_spar_md_t *pSpar_md,
    2332             :     const int16_t nB,
    2333             :     const int16_t bw,
    2334             :     int16_t *ndm_per_band )
    2335             : {
    2336             :     int16_t i, b;
    2337             :     int16_t q_lvl;
    2338             :     float val;
    2339             :     int16_t idx;
    2340             :     float pr_min_max[2];
    2341             : 
    2342         564 :     pr_min_max[0] = pSpar_md->min_max[0];
    2343         564 :     pr_min_max[1] = pSpar_md->min_max[1];
    2344             : 
    2345        1692 :     for ( b = 0; b < nB; b++ )
    2346             :     {
    2347        4512 :         for ( i = 0; i < FOA_CHANNELS - 1; i++ )
    2348             :         {
    2349        3384 :             q_lvl = dtx_pr_real_q_levels[ndm_per_band[bw * b] - 1][i];
    2350        3384 :             idx = pSpar_md->band_coeffs_idx[b].pred_index_re[i];
    2351        3384 :             ivas_deindex_real_index( &idx, q_lvl, pr_min_max[0], pr_min_max[1], &val, 1 );
    2352        3384 :             pSpar_md->band_coeffs[b].pred_re[i] = val;
    2353             :         }
    2354             : 
    2355        3874 :         for ( i = 0; i < FOA_CHANNELS - ndm_per_band[bw * b]; i++ )
    2356             :         {
    2357        2746 :             q_lvl = dtx_pd_real_q_levels[ndm_per_band[bw * b] - 1][i];
    2358        2746 :             idx = pSpar_md->band_coeffs_idx[b].decd_index_re[i];
    2359        2746 :             ivas_deindex_real_index( &idx, q_lvl, dtx_pd_real_min_max[0], dtx_pd_real_min_max[1], &val, 1 );
    2360        2746 :             pSpar_md->band_coeffs[b].P_re[i] = val;
    2361             :         }
    2362             :     }
    2363             : 
    2364         564 :     return;
    2365             : }
    2366             : 
    2367             : 
    2368             : /*-----------------------------------------------------------------------------------------*
    2369             :  * Function ivas_parse_parameter_bitstream_dtx()
    2370             :  *
    2371             :  * parse DTX bitstream parameters
    2372             :  *-----------------------------------------------------------------------------------------*/
    2373             : 
    2374         564 : static void ivas_parse_parameter_bitstream_dtx(
    2375             :     ivas_spar_md_t *pSpar_md,
    2376             :     Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
    2377             :     const int16_t bw,
    2378             :     const int16_t num_bands,
    2379             :     int16_t *num_dmx_per_band,
    2380             :     int16_t *num_dec_per_band )
    2381             : {
    2382             :     int16_t i, j, ndec, ndm;
    2383             :     float val;
    2384             :     int16_t idx;
    2385             :     float pr_min_max[2];
    2386             :     int16_t pr_q_lvls, pr, pd, pd_q_lvls, pr_pd_bits;
    2387             :     int16_t zero_pad_bits, sid_bits_len;
    2388             :     int16_t sba_spar_bitlen;
    2389             : 
    2390         564 :     sid_bits_len = st0->next_bit_pos;
    2391         564 :     pr_min_max[0] = pSpar_md->min_max[0];
    2392         564 :     pr_min_max[1] = pSpar_md->min_max[1];
    2393             : 
    2394        1692 :     for ( i = 0; i < num_bands; i++ )
    2395             :     {
    2396        1128 :         ndec = num_dec_per_band[bw * i];
    2397        1128 :         ndm = num_dmx_per_band[bw * i];
    2398             : 
    2399        4512 :         for ( j = 0; j < FOA_CHANNELS - 1; j++ )
    2400             :         {
    2401             :             int16_t pr_idx_1, pr_idx_2, pd_idx_1, pd_idx_2;
    2402             :             uint16_t value;
    2403             : 
    2404        3384 :             pr_idx_1 = pr_pr_idx_pairs[ndm - 1][j][0];
    2405        3384 :             pr_idx_2 = pr_pr_idx_pairs[ndm - 1][j][1];
    2406        3384 :             pd_idx_1 = pr_pd_idx_pairs[ndm - 1][j][0];
    2407        3384 :             pd_idx_2 = pr_pd_idx_pairs[ndm - 1][j][1];
    2408             : 
    2409        3384 :             if ( pr_idx_1 != 0 || pd_idx_1 != 0 || pr_idx_2 != 0 || pd_idx_2 != 0 )
    2410             :             {
    2411        3384 :                 pr_q_lvls = dtx_pr_real_q_levels[ndm - 1][pd_idx_1 - 1];
    2412             : 
    2413        3384 :                 if ( ( j + 1 ) > ndec )
    2414             :                 {
    2415         638 :                     pd_q_lvls = 1;
    2416             :                 }
    2417             :                 else
    2418             :                 {
    2419        2746 :                     pd_q_lvls = dtx_pd_real_q_levels[ndm - 1][pd_idx_2 - 1];
    2420             :                 }
    2421             : 
    2422        3384 :                 pr_pd_bits = ivas_get_bits_to_encode( pd_q_lvls * pr_q_lvls );
    2423             : 
    2424        3384 :                 value = get_next_indice( st0, pr_pd_bits );
    2425             : 
    2426        3384 :                 pr = (int16_t) floor( value / pd_q_lvls );
    2427        3384 :                 pd = value - pr * pd_q_lvls;
    2428        3384 :                 val = dtx_pd_real_min_max[0];
    2429        3384 :                 ivas_quantise_real_values( &val, pd_q_lvls, dtx_pd_real_min_max[0], dtx_pd_real_min_max[1], &idx, &val, 1 );
    2430        3384 :                 pd = pd + idx;
    2431             : 
    2432        3384 :                 val = pr_min_max[0];
    2433        3384 :                 ivas_quantise_real_values( &val, pr_q_lvls, pr_min_max[0], pr_min_max[1], &idx, &val, 1 );
    2434        3384 :                 pr = pr + idx;
    2435             : 
    2436        3384 :                 if ( ( j + 1 ) <= ndec )
    2437             :                 {
    2438        2746 :                     pSpar_md->band_coeffs_idx[i].decd_index_re[pd_idx_2 - 1] = pd;
    2439             :                 }
    2440             : 
    2441        3384 :                 pSpar_md->band_coeffs_idx[i].pred_index_re[pd_idx_1 - 1] = pr;
    2442             :             }
    2443             :         }
    2444             :     }
    2445             : 
    2446         564 :     sid_bits_len = st0->next_bit_pos - sid_bits_len;
    2447         564 :     sba_spar_bitlen = ivas_sba_spar_sid_bitlen( num_dmx_per_band[0] );
    2448         564 :     zero_pad_bits = sba_spar_bitlen - sid_bits_len;
    2449             : 
    2450         564 :     assert( zero_pad_bits >= 0 );
    2451         564 :     if ( num_dmx_per_band[0] == 2 )
    2452             :     {
    2453         319 :         zero_pad_bits -= 1;
    2454             :     }
    2455             : 
    2456        1521 :     for ( j = 0; j < zero_pad_bits; j++ )
    2457             :     {
    2458         957 :         get_next_indice( st0, 1 );
    2459             :     }
    2460             : 
    2461         564 :     ivas_spar_unquant_dtx_indicies( pSpar_md, num_bands, bw, num_dmx_per_band );
    2462             : 
    2463         564 :     return;
    2464             : }
    2465             : 
    2466             : 
    2467             : /*-----------------------------------------------------------------------------------------*
    2468             :  * Function ivas_deindex_real_index()
    2469             :  *
    2470             :  * Deindex real index
    2471             :  *-----------------------------------------------------------------------------------------*/
    2472             : 
    2473     5948290 : static ivas_error ivas_deindex_real_index(
    2474             :     const int16_t *index,
    2475             :     const int16_t q_levels,
    2476             :     const float min_value,
    2477             :     const float max_value,
    2478             :     float *quant,
    2479             :     const int16_t dim )
    2480             : {
    2481             :     int16_t i;
    2482             :     float q_step;
    2483             : 
    2484     5948290 :     if ( q_levels == 0 )
    2485             :     {
    2486           0 :         return IVAS_ERR_INTERNAL;
    2487             :     }
    2488             : 
    2489     5948290 :     if ( q_levels == 1 )
    2490             :     {
    2491     2498744 :         for ( i = 0; i < dim; i++ )
    2492             :         {
    2493     1342032 :             quant[i] = 0;
    2494             :         }
    2495             :     }
    2496             :     else
    2497             :     {
    2498     4791578 :         q_step = ( max_value - min_value ) / ( q_levels - 1 );
    2499    31825248 :         for ( i = 0; i < dim; i++ )
    2500             :         {
    2501    27033670 :             quant[i] = index[i] * q_step;
    2502             :         }
    2503             :     }
    2504             : 
    2505     5948290 :     return IVAS_ERR_OK;
    2506             : }
    2507             : 
    2508             : 
    2509             : /*-----------------------------------------------------------------------------------------*
    2510             :  * Function ivas_spar_to_dirac()
    2511             :  *
    2512             :  *
    2513             :  *-----------------------------------------------------------------------------------------*/
    2514             : 
    2515      204072 : void ivas_spar_to_dirac(
    2516             :     Decoder_Struct *st_ivas,
    2517             :     ivas_spar_md_dec_state_t *hMdDec, /* i/o: SPAR MD decoder handle      */
    2518             :     const int16_t dtx_vad,            /* i  : DTX frame flag              */
    2519             :     const int16_t num_bands_out,      /* i  : number of output bands      */
    2520             :     const int16_t bw,                 /* i  : band joining factor         */
    2521             :     const int16_t dyn_active_w_flag )
    2522             : {
    2523             :     DIRAC_DEC_HANDLE hDirAC;
    2524             :     int16_t start_band, end_band, band, qmf_band_start, qmf_band_end;
    2525             :     int16_t block, b;
    2526             :     int16_t *band_grouping;
    2527             :     float diffuseness[IVAS_MAX_NUM_BANDS];
    2528             :     int16_t sba_order_internal;
    2529             :     float azi_dirac[IVAS_MAX_NUM_BANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
    2530             :     float ele_dirac[IVAS_MAX_NUM_BANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
    2531             :     int16_t azi[IVAS_MAX_NUM_BANDS];
    2532             :     int16_t ele[IVAS_MAX_NUM_BANDS];
    2533             :     float dvx[IVAS_MAX_NUM_BANDS], dvy[IVAS_MAX_NUM_BANDS], dvz[IVAS_MAX_NUM_BANDS];
    2534             :     float radius;
    2535             :     float en_ratio, res_pow;
    2536             :     int16_t num_slots_in_subfr;
    2537             :     int16_t tmp_write_idx_param_band;
    2538             :     int16_t tmp_write_idx_band;
    2539             :     float pred_re_20ms[IVAS_MAX_NUM_BANDS][IVAS_SPAR_MAX_CH - 1];
    2540             :     int16_t pred_idx;
    2541             :     int16_t *dirac_to_spar_md_bands;
    2542             :     int16_t enc_param_start_band;
    2543             :     int16_t active_w_vlbr;
    2544             :     int16_t i, num_subframes;
    2545             :     int16_t active_w;
    2546             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
    2547             : 
    2548      204072 :     active_w = ( dyn_active_w_flag == 1 ) || ( hMdDec->spar_md_cfg.active_w == 1 );
    2549      204072 :     sba_order_internal = min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER );
    2550      204072 :     start_band = 0;
    2551      204072 :     end_band = min( num_bands_out, SPAR_DIRAC_SPLIT_START_BAND ) / bw;
    2552             : 
    2553      204072 :     hDirAC = st_ivas->hDirAC;
    2554      204072 :     hSpatParamRendCom = st_ivas->hSpatParamRendCom;
    2555             : 
    2556      204072 :     dirac_to_spar_md_bands = st_ivas->hSpar->dirac_to_spar_md_bands;
    2557      204072 :     enc_param_start_band = st_ivas->hSpar->enc_param_start_band / bw;
    2558      204072 :     active_w_vlbr = ( st_ivas->hDecoderConfig->ivas_total_brate < IVAS_24k4 ) ? 1 : 0;
    2559             : 
    2560      204072 :     if ( hDirAC != NULL && ivas_get_hodirac_flag( st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->sba_analysis_order ) == 0 )
    2561             :     {
    2562      150377 :         band_grouping = hDirAC->band_grouping;
    2563      150377 :         num_slots_in_subfr = st_ivas->hDirAC->hConfig->dec_param_estim ? CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES : 1;
    2564             : 
    2565     1353393 :         for ( band = start_band; band < end_band; band++ )
    2566             :         {
    2567             :             float PR[3], Pd[3], dvnorm, g_pred;
    2568             : 
    2569     1203016 :             PR[0] = hMdDec->spar_md.band_coeffs[band].pred_re[2];
    2570     1203016 :             PR[1] = hMdDec->spar_md.band_coeffs[band].pred_re[0];
    2571     1203016 :             PR[2] = hMdDec->spar_md.band_coeffs[band].pred_re[1];
    2572     1203016 :             g_pred = PR[0] * PR[0] + PR[1] * PR[1] + PR[2] * PR[2];
    2573     1203016 :             if ( g_pred <= EPSILON )
    2574             :             {
    2575       38836 :                 dvx[band] = 1.0f;
    2576       38836 :                 dvy[band] = 0.0f;
    2577       38836 :                 dvz[band] = 0.0f;
    2578             :             }
    2579             :             else
    2580             :             {
    2581     1164180 :                 g_pred = sqrtf( g_pred );
    2582     1164180 :                 dvnorm = 1.0f / g_pred;
    2583     1164180 :                 dvx[band] = PR[0] * dvnorm;
    2584     1164180 :                 dvy[band] = PR[1] * dvnorm;
    2585     1164180 :                 dvz[band] = PR[2] * dvnorm;
    2586             :             }
    2587             : 
    2588     1203016 :             radius = sqrtf( dvx[band] * dvx[band] + dvy[band] * dvy[band] );
    2589     1203016 :             azi[band] = (int16_t) ( max( -180.0f, min( 180.0f, atan2f( dvy[band], dvx[band] ) / EVS_PI * 180.0f ) ) + 0.5f );
    2590     1203016 :             ele[band] = (int16_t) ( max( -90.0f, min( 180.0f, atan2f( dvz[band], radius ) / EVS_PI * 180.0f ) ) + 0.5f );
    2591             : 
    2592     1203016 :             if ( st_ivas->nchan_transport == 1 )
    2593             :             {
    2594             :                 float w_en_norm, f_scale;
    2595      174216 :                 if ( active_w )
    2596             :                 {
    2597      174216 :                     if ( dtx_vad == 0 )
    2598             :                     {
    2599         888 :                         f_scale = IVAS_ACTIVEW_DM_F_SCALE_DTX;
    2600             :                     }
    2601             :                     else
    2602             :                     {
    2603      173328 :                         f_scale = ( active_w_vlbr ) ? IVAS_ACTIVEW_DM_F_SCALE_VLBR : IVAS_ACTIVEW_DM_F_SCALE;
    2604             :                     }
    2605             :                 }
    2606             :                 else
    2607             :                 {
    2608           0 :                     f_scale = 0.0f;
    2609             :                 }
    2610             : 
    2611      174216 :                 w_en_norm = ( 1.0f - ( f_scale * g_pred * g_pred ) );
    2612      174216 :                 w_en_norm *= w_en_norm;
    2613             : 
    2614      174216 :                 Pd[0] = hMdDec->spar_md.band_coeffs[band].P_re[1];
    2615      174216 :                 Pd[1] = hMdDec->spar_md.band_coeffs[band].P_re[0];
    2616      174216 :                 Pd[2] = hMdDec->spar_md.band_coeffs[band].P_re[2];
    2617      174216 :                 en_ratio = PR[0] * PR[0] + PR[1] * PR[1] + PR[2] * PR[2];
    2618      174216 :                 res_pow = w_en_norm + en_ratio + ( Pd[0] * Pd[0] + Pd[1] * Pd[1] + Pd[2] * Pd[2] );
    2619      174216 :                 res_pow *= 0.5f;
    2620      174216 :                 hMdDec->spar_md.en_ratio_slow[band] = 0.75f * hMdDec->spar_md.en_ratio_slow[band] + 0.25f * en_ratio;
    2621      174216 :                 hMdDec->spar_md.ref_pow_slow[band] = 0.75f * hMdDec->spar_md.ref_pow_slow[band] + 0.25f * res_pow;
    2622      174216 :                 en_ratio = sqrtf( hMdDec->spar_md.en_ratio_slow[band] ) / ( hMdDec->spar_md.ref_pow_slow[band] + EPSILON );
    2623             :             }
    2624             :             else
    2625             :             {
    2626     1028800 :                 en_ratio = PR[0] * PR[0] + PR[1] * PR[1] + PR[2] * PR[2];
    2627     1028800 :                 hMdDec->spar_md.en_ratio_slow[band] = 0.75f * hMdDec->spar_md.en_ratio_slow[band] + 0.25f * en_ratio;
    2628     1028800 :                 en_ratio = sqrtf( hMdDec->spar_md.en_ratio_slow[band] );
    2629             :             }
    2630     1203016 :             diffuseness[band] = 1.0f - en_ratio; /*compute diffuseness*/
    2631     1203016 :             diffuseness[band] = ( ( diffuseness[band] < 1.0f ) ? ( ( diffuseness[band] < 0.0f ) ? 0.f : diffuseness[band] ) : 1.0f );
    2632             :         }
    2633             : 
    2634     1353393 :         for ( band = start_band; band < end_band; band++ )
    2635             :         {
    2636             :             int16_t azi_dith, ele_dith;
    2637     1203016 :             tmp_write_idx_param_band = hDirAC->spar_to_dirac_write_idx;
    2638             : 
    2639     1203016 :             en_ratio = 1.0f - diffuseness[band];
    2640     1203016 :             masa_sq( 1.0f - en_ratio, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
    2641             : 
    2642     1203016 :             qmf_band_start = band_grouping[band];
    2643     1203016 :             qmf_band_end = band_grouping[band + 1];
    2644             : 
    2645     6015080 :             for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; block++ )
    2646             :             {
    2647             :                 int16_t ts_start, ts_end, ts;
    2648             : 
    2649     4812064 :                 ts_start = DirAC_block_grouping[block];
    2650     4812064 :                 ts_end = DirAC_block_grouping[block + 1];
    2651    11428652 :                 for ( b = qmf_band_start; b < qmf_band_end; b++ )
    2652             :                 {
    2653             : 
    2654     6616588 :                     azi_dith = azi[band];
    2655     6616588 :                     ele_dith = ele[band];
    2656             : 
    2657     6616588 :                     hSpatParamRendCom->energy_ratio1[block][b] = en_ratio;
    2658     6616588 :                     tmp_write_idx_band = tmp_write_idx_param_band;
    2659             : 
    2660     6616588 :                     if ( hDirAC->hConfig->dec_param_estim == FALSE )
    2661             :                     {
    2662     2383304 :                         hSpatParamRendCom->elevation[tmp_write_idx_band][b] = ele_dith;
    2663     2383304 :                         hSpatParamRendCom->azimuth[tmp_write_idx_band][b] = azi_dith;
    2664     2383304 :                         hSpatParamRendCom->diffuseness_vector[tmp_write_idx_band][b] = diffuseness[band];
    2665             :                     }
    2666             :                     else
    2667             :                     {
    2668    21166420 :                         for ( ts = ts_start; ts < ts_end; ts++ )
    2669             :                         {
    2670    16933136 :                             hSpatParamRendCom->elevation[tmp_write_idx_band][b] = ele_dith;
    2671    16933136 :                             hSpatParamRendCom->azimuth[tmp_write_idx_band][b] = azi_dith;
    2672    16933136 :                             hSpatParamRendCom->diffuseness_vector[tmp_write_idx_band][b] = diffuseness[band];
    2673    16933136 :                             tmp_write_idx_band = ( tmp_write_idx_band + 1 ) % hSpatParamRendCom->dirac_md_buffer_length;
    2674             :                         }
    2675             :                     }
    2676             :                 }
    2677     4812064 :                 tmp_write_idx_param_band = ( tmp_write_idx_param_band + num_slots_in_subfr ) % hSpatParamRendCom->dirac_md_buffer_length;
    2678             :             }
    2679             :         }
    2680             : 
    2681             :         /* update buffer write index */
    2682      150377 :         if ( hDirAC->hConfig->dec_param_estim == FALSE )
    2683             :         {
    2684       54166 :             hDirAC->spar_to_dirac_write_idx = ( hDirAC->spar_to_dirac_write_idx + MAX_PARAM_SPATIAL_SUBFRAMES ) % hSpatParamRendCom->dirac_md_buffer_length;
    2685             :         }
    2686             :         else
    2687             :         {
    2688       96211 :             hDirAC->spar_to_dirac_write_idx = ( hDirAC->spar_to_dirac_write_idx + CLDFB_NO_COL_MAX ) % hSpatParamRendCom->dirac_md_buffer_length;
    2689             :         }
    2690             :     }
    2691             :     else
    2692             :     {
    2693       53695 :         band = end_band;
    2694             :     }
    2695             : 
    2696             :     /*read DirAC metadata, convert DirAC to SPAR*/
    2697      996252 :     for ( ; band < num_bands_out / bw; band++ )
    2698             :     {
    2699             :         int16_t dirac_band_idx;
    2700             : 
    2701      792180 :         dirac_band_idx = dirac_to_spar_md_bands[band] - enc_param_start_band;
    2702             : 
    2703      792180 :         num_subframes = MAX_PARAM_SPATIAL_SUBFRAMES;
    2704      792180 :         if ( st_ivas->hQMetaData->useLowerRes )
    2705             :         {
    2706       46804 :             num_subframes = 1;
    2707             :         }
    2708             : 
    2709     3820488 :         for ( block = 0; block < num_subframes; block++ )
    2710             :         {
    2711     3028308 :             if ( st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].azimuth[block] < 0.f )
    2712             :             {
    2713      303463 :                 st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].azimuth[block] += 360.f;
    2714             :             }
    2715     3028308 :             azi_dirac[band][block] = st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].azimuth[block];
    2716     3028308 :             ele_dirac[band][block] = st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].elevation[block];
    2717             :         }
    2718             : 
    2719      792180 :         diffuseness[band] = 1.0f - st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].energy_ratio[0];
    2720             :     }
    2721             : 
    2722             :     /* DirAC MD averaged over 4 subframes and converted to SPAR format similar to encoder processing */
    2723      204072 :     if ( hMdDec->spar_md_cfg.nchan_transport > 1 )
    2724             :     {
    2725      165182 :         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 );
    2726             : 
    2727             :         /* temporarily copy frame-wise prediction coefficients in DirAC bands*/
    2728      660728 :         for ( pred_idx = 0; pred_idx < FOA_CHANNELS - 1; pred_idx++ )
    2729             :         {
    2730     2477730 :             for ( band = SPAR_DIRAC_SPLIT_START_BAND; band < IVAS_MAX_NUM_BANDS; band++ )
    2731             :             {
    2732     1982184 :                 pred_re_20ms[band][pred_idx] = hMdDec->spar_md.band_coeffs[band].pred_re[pred_idx];
    2733             :             }
    2734             :         }
    2735             :     }
    2736             : 
    2737             :     int16_t num_md_sub_frames;
    2738      204072 :     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 );
    2739      204072 :     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 );
    2740             : 
    2741      204072 :     if ( st_ivas->hQMetaData->useLowerRes && dtx_vad )
    2742             :     {
    2743       58920 :         for ( band = SPAR_DIRAC_SPLIT_START_BAND; band < IVAS_MAX_NUM_BANDS; band++ )
    2744             :         {
    2745       47136 :             for ( block = 1; block < num_md_sub_frames; block++ )
    2746             :             {
    2747           0 :                 for ( i = 0; i < FOA_CHANNELS - 1; i++ ) /* pred coefficient index (index 0, 1, 2 predicts Y, Z, X respectively) */
    2748             :                 {
    2749           0 :                     hMdDec->spar_md.band_coeffs[band + block * IVAS_MAX_NUM_BANDS].pred_re[i] = hMdDec->spar_md.band_coeffs[band].pred_re[i];
    2750             :                 }
    2751           0 :                 for ( i = 0; i < FOA_CHANNELS - 1; i++ ) /* pred coefficient index (index 0, 1, 2 predicts Y, Z, X respectively) */
    2752             :                 {
    2753           0 :                     hMdDec->spar_md.band_coeffs[band + block * IVAS_MAX_NUM_BANDS].P_re[i] = hMdDec->spar_md.band_coeffs[band].P_re[i];
    2754             :                 }
    2755             :             }
    2756             :         }
    2757             :     }
    2758             : 
    2759             :     /* expand DirAC TC 20ms MD for residual channels to all subframes*/
    2760      984924 :     for ( block = 0; block < num_md_sub_frames; block++ )
    2761             :     {
    2762     3904260 :         for ( band = SPAR_DIRAC_SPLIT_START_BAND; band < IVAS_MAX_NUM_BANDS; band++ )
    2763             :         {
    2764    12493632 :             for ( pred_idx = 0; pred_idx < FOA_CHANNELS - 1; pred_idx++ ) /* pred coefficient index (index 0, 1, 2 predicts Y, Z, X respectively) */
    2765             :             {
    2766     9370224 :                 if ( ivas_is_res_channel( pred_idx + 1, hMdDec->spar_md_cfg.nchan_transport ) )
    2767             :                 {
    2768             :                     /* use 20ms coefficients only for residual channels */
    2769     5627600 :                     hMdDec->spar_md.band_coeffs[band + block * IVAS_MAX_NUM_BANDS].pred_re[pred_idx] = pred_re_20ms[band][pred_idx];
    2770             :                 }
    2771             :             }
    2772             :         }
    2773             :     }
    2774             : 
    2775      996252 :     for ( b = end_band * bw; b < num_bands_out; b++ )
    2776             :     {
    2777      792180 :         hMdDec->valid_bands[b] = 1;
    2778             :     }
    2779             : 
    2780      204072 :     return;
    2781             : }

Generated by: LCOV version 1.14