LCOV - code coverage report
Current view: top level - lib_dec - ivas_spar_md_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 900 976 92.2 %
Date: 2025-05-23 08:37:30 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        5034 : 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        5034 :     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        5034 :     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        5034 :     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       28695 :     for ( i = 0; i < num_channels; i++ )
     116             :     {
     117       23661 :         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      150750 :         for ( j = 0; j < num_channels; j++ )
     122             :         {
     123      127089 :             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        5034 :     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       28695 :     for ( i = 0; i < num_channels; i++ )
     135             :     {
     136       23661 :         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      150750 :         for ( j = 0; j < num_channels; j++ )
     141             :         {
     142      127089 :             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        5034 :     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       28695 :     for ( i = 0; i < num_channels; i++ )
     154             :     {
     155       23661 :         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      150750 :         for ( j = 0; j < num_channels; j++ )
     160             :         {
     161      127089 :             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        5034 :     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       28695 :     for ( i = 0; i < num_channels; i++ )
     173             :     {
     174       23661 :         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      150750 :         for ( j = 0; j < num_channels; j++ )
     179             :         {
     180      127089 :             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        5034 :     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       28695 :     for ( i = 0; i < num_channels; i++ )
     192             :     {
     193       23661 :         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      150750 :         for ( j = 0; j < num_channels; j++ )
     198             :         {
     199      127089 :             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        5034 :     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       28695 :     for ( i = 0; i < num_channels; i++ )
     211             :     {
     212       23661 :         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      150750 :         for ( j = 0; j < num_channels; j++ )
     217             :         {
     218      127089 :             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        5034 :     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       28695 :     for ( i = 0; i < num_channels; i++ )
     230             :     {
     231       23661 :         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      150750 :         for ( j = 0; j < num_channels; j++ )
     236             :         {
     237      127089 :             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       28695 :     for ( i = 0; i < num_channels; i++ )
     244             :     {
     245      150750 :         for ( j = 0; j < num_channels; j++ )
     246             :         {
     247     1652157 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
     248             :             {
     249     1525068 :                 hMdDec->spar_coeffs_prev.C_re[i][j][k] = 0.0f;
     250     1525068 :                 hMdDec->spar_coeffs_prev.P_re[i][j][k] = 0.0f;
     251     1525068 :                 hMdDec->spar_coeffs_tar.C_re[i][j][k] = 0.0f;
     252     1525068 :                 hMdDec->spar_coeffs_tar.P_re[i][j][k] = 0.0f;
     253             :             }
     254             :         }
     255             :     }
     256             : 
     257        5034 :     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     4591242 : 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     4591242 :     num_subframes = MAX_PARAM_SPATIAL_SUBFRAMES;
     277     4591242 :     if ( sba_order > SBA_FOA_ORDER )
     278             :     {
     279     1025484 :         if ( ivas_total_brate >= IVAS_512k )
     280             :         {
     281      206700 :             num_subframes = 1;
     282             :         }
     283             :     }
     284             : 
     285     4591242 :     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      379737 :         num_subframes = 1;
     289             :     }
     290             : 
     291     4591242 :     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        4908 : 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        4908 :     error = IVAS_ERR_OK;
     315             : 
     316        4908 :     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        4908 :     num_md_sub_frames = ivas_get_spar_dec_md_num_subframes( sba_order, hDecoderConfig->ivas_total_brate, last_active_ivas_total_brate );
     322             : 
     323        4908 :     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        4908 :     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        4908 :         hMdDec->table_idx = ivas_get_spar_table_idx( hDecoderConfig->ivas_total_brate, sba_order, SPAR_CONFIG_BW, NULL, NULL );
     342             :     }
     343             : 
     344        4908 :     if ( ( error = ivas_spar_md_dec_init( hMdDec, hDecoderConfig, num_channels, sba_order ) ) != IVAS_ERR_OK )
     345             :     {
     346           0 :         return error;
     347             :     }
     348             : 
     349        4908 :     *hMdDec_out = hMdDec;
     350             : 
     351        4908 :     return error;
     352             : }
     353             : 
     354             : 
     355             : /*-------------------------------------------------------------------------
     356             :  * ivas_spar_md_dec_matrix_close()
     357             :  *
     358             :  * Deallocate SPAR MD decoder matrices
     359             :  *------------------------------------------------------------------------*/
     360             : 
     361        5034 : 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        5034 :     if ( hMdDecoder->spar_md.band_coeffs != NULL )
     369             :     {
     370        5034 :         free( hMdDecoder->spar_md.band_coeffs );
     371        5034 :         hMdDecoder->spar_md.band_coeffs = NULL;
     372             :     }
     373        5034 :     if ( hMdDecoder->band_coeffs_prev != NULL )
     374             :     {
     375        5034 :         free( hMdDecoder->band_coeffs_prev );
     376        5034 :         hMdDecoder->band_coeffs_prev = NULL;
     377             :     }
     378             : 
     379        5034 :     if ( hMdDecoder->mixer_mat != NULL )
     380             :     {
     381       28695 :         for ( i = 0; i < num_channels; i++ )
     382             :         {
     383      150750 :             for ( j = 0; j < num_channels; j++ )
     384             :             {
     385      127089 :                 free( hMdDecoder->mixer_mat[i][j] );
     386             :             }
     387       23661 :             free( hMdDecoder->mixer_mat[i] );
     388             :         }
     389        5034 :         free( hMdDecoder->mixer_mat );
     390             :     }
     391             : 
     392        5034 :     if ( hMdDecoder->spar_coeffs.C_re != NULL )
     393             :     {
     394       28695 :         for ( i = 0; i < num_channels; i++ )
     395             :         {
     396      150750 :             for ( j = 0; j < num_channels; j++ )
     397             :             {
     398      127089 :                 free( hMdDecoder->spar_coeffs.C_re[i][j] );
     399             :             }
     400       23661 :             free( hMdDecoder->spar_coeffs.C_re[i] );
     401             :         }
     402        5034 :         free( hMdDecoder->spar_coeffs.C_re );
     403             :     }
     404             : 
     405        5034 :     if ( hMdDecoder->spar_coeffs.P_re != NULL )
     406             :     {
     407       28695 :         for ( i = 0; i < num_channels; i++ )
     408             :         {
     409      150750 :             for ( j = 0; j < num_channels; j++ )
     410             :             {
     411      127089 :                 free( hMdDecoder->spar_coeffs.P_re[i][j] );
     412             :             }
     413       23661 :             free( hMdDecoder->spar_coeffs.P_re[i] );
     414             :         }
     415        5034 :         free( hMdDecoder->spar_coeffs.P_re );
     416             :     }
     417             : 
     418        5034 :     if ( hMdDecoder->spar_coeffs_prev.C_re != NULL )
     419             :     {
     420       28695 :         for ( i = 0; i < num_channels; i++ )
     421             :         {
     422      150750 :             for ( j = 0; j < num_channels; j++ )
     423             :             {
     424      127089 :                 free( hMdDecoder->spar_coeffs_prev.C_re[i][j] );
     425             :             }
     426       23661 :             free( hMdDecoder->spar_coeffs_prev.C_re[i] );
     427             :         }
     428        5034 :         free( hMdDecoder->spar_coeffs_prev.C_re );
     429             :     }
     430             : 
     431        5034 :     if ( hMdDecoder->spar_coeffs_prev.P_re != NULL )
     432             :     {
     433       28695 :         for ( i = 0; i < num_channels; i++ )
     434             :         {
     435      150750 :             for ( j = 0; j < num_channels; j++ )
     436             :             {
     437      127089 :                 free( hMdDecoder->spar_coeffs_prev.P_re[i][j] );
     438             :             }
     439       23661 :             free( hMdDecoder->spar_coeffs_prev.P_re[i] );
     440             :         }
     441        5034 :         free( hMdDecoder->spar_coeffs_prev.P_re );
     442             :     }
     443             : 
     444        5034 :     if ( hMdDecoder->spar_coeffs_tar.C_re != NULL )
     445             :     {
     446       28695 :         for ( i = 0; i < num_channels; i++ )
     447             :         {
     448      150750 :             for ( j = 0; j < num_channels; j++ )
     449             :             {
     450      127089 :                 free( hMdDecoder->spar_coeffs_tar.C_re[i][j] );
     451             :             }
     452       23661 :             free( hMdDecoder->spar_coeffs_tar.C_re[i] );
     453             :         }
     454        5034 :         free( hMdDecoder->spar_coeffs_tar.C_re );
     455             :     }
     456             : 
     457        5034 :     if ( hMdDecoder->spar_coeffs_tar.P_re != NULL )
     458             :     {
     459       28695 :         for ( i = 0; i < num_channels; i++ )
     460             :         {
     461      150750 :             for ( j = 0; j < num_channels; j++ )
     462             :             {
     463      127089 :                 free( hMdDecoder->spar_coeffs_tar.P_re[i][j] );
     464             :             }
     465       23661 :             free( hMdDecoder->spar_coeffs_tar.P_re[i] );
     466             :         }
     467        5034 :         free( hMdDecoder->spar_coeffs_tar.P_re );
     468             :     }
     469             : 
     470        5034 :     return;
     471             : }
     472             : 
     473             : 
     474             : /*-------------------------------------------------------------------------
     475             :  * ivas_spar_md_dec_close()
     476             :  *
     477             :  * Deallocate SPAR MD decoder handle
     478             :  *------------------------------------------------------------------------*/
     479             : 
     480        4908 : 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        4908 :     hMdDecoder = *hMdDec;
     488        4908 :     num_channels = hMdDecoder->spar_md_cfg.num_umx_chs;
     489             : 
     490        4908 :     ivas_spar_md_dec_matrix_close( hMdDecoder, num_channels );
     491             : 
     492        4908 :     free( *hMdDec );
     493        4908 :     *hMdDec = NULL;
     494             : 
     495        4908 :     return;
     496             : }
     497             : 
     498             : 
     499             : /*-----------------------------------------------------------------------------------------*
     500             :  * Function ivas_spar_md_dec_init()
     501             :  *
     502             :  * SPAR MD decoder initialization
     503             :  *-----------------------------------------------------------------------------------------*/
     504             : 
     505        5862 : 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        5862 :     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        5862 :     ivas_sba_get_spar_hoa_ch_ind( num_channels, hDecoderConfig->ivas_total_brate, hMdDec->HOA_md_ind );
     520             : 
     521        5862 :     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        5862 :     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        5862 :     nchan_transport = hMdDec->spar_md_cfg.nchan_transport;
     526             : 
     527             :     /* get FB coefficients */
     528       76206 :     for ( i = 0; i < IVAS_MAX_NUM_BANDS; i++ )
     529             :     {
     530       70344 :         pFC[i] = ivas_fb_fcs_12band_1ms[i] * hDecoderConfig->output_Fs * 0.5f;
     531             :     }
     532             : 
     533        5862 :     if ( ( error = ivas_spar_set_dec_config( hMdDec, nchan_transport, pFC ) ) != IVAS_ERR_OK )
     534             :     {
     535           0 :         return error;
     536             :     }
     537             : 
     538        5862 :     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        5862 :     PR_minmax[0] = hMdDec->spar_md_cfg.quant_strat[0].PR.min;
     545        5862 :     PR_minmax[1] = hMdDec->spar_md_cfg.quant_strat[0].PR.max;
     546        5862 :     ivas_spar_quant_dtx_init( &hMdDec->spar_md, PR_minmax );
     547             : 
     548        5862 :     ivas_spar_arith_coeffs_com_init( &hMdDec->arith_coeffs, &hMdDec->spar_md_cfg, hMdDec->table_idx, DEC );
     549        5862 :     ivas_spar_huff_coeffs_com_init( &hMdDec->huff_coeffs, &hMdDec->spar_md_cfg, hMdDec->table_idx, DEC );
     550             : 
     551        5862 :     hMdDec->spar_md_cfg.prev_quant_idx = -1;
     552             : 
     553             :     /* initialize PLC state */
     554        5862 :     set_s( hMdDec->valid_bands, 0, IVAS_MAX_NUM_BANDS );
     555        5862 :     set_s( hMdDec->base_band_age, 0, IVAS_MAX_NUM_BANDS );
     556        5862 :     set_s( hMdDec->base_band_coeffs_age, 0, IVAS_MAX_NUM_BANDS );
     557        5862 :     hMdDec->spar_plc_num_lost_frames = 0;
     558        5862 :     hMdDec->spar_plc_enable_fadeout_flag = 1;
     559        5862 :     hMdDec->dtx_md_smoothing_cntr = 1;
     560             : 
     561        5862 :     ivas_clear_band_coeffs( hMdDec->spar_md.band_coeffs, IVAS_MAX_NUM_BANDS );
     562        5862 :     ivas_clear_band_coeffs( hMdDec->band_coeffs_prev, IVAS_MAX_NUM_BANDS );
     563        5862 :     ivas_clear_band_coeff_idx( hMdDec->spar_md.band_coeffs_idx, IVAS_MAX_NUM_BANDS );
     564        5862 :     ivas_clear_band_coeff_idx( hMdDec->spar_md_prev.band_coeffs_idx, IVAS_MAX_NUM_BANDS );
     565        5862 :     ivas_clear_band_coeff_idx( hMdDec->spar_md_prev.band_coeffs_idx_mapped, IVAS_MAX_NUM_BANDS );
     566             : 
     567        5862 :     hMdDec->spar_md.dtx_vad = 0;
     568        5862 :     hMdDec->td_decorr_flag = 1;
     569             : 
     570        5862 :     set_f( hMdDec->spar_md.en_ratio_slow, 0.0f, IVAS_MAX_NUM_BANDS );
     571        5862 :     set_f( hMdDec->spar_md.ref_pow_slow, 0.0f, IVAS_MAX_NUM_BANDS );
     572             : 
     573        5862 :     set_zero( hMdDec->smooth_fac, IVAS_MAX_NUM_BANDS );
     574       76206 :     for ( i = 0; i < IVAS_MAX_NUM_BANDS; i++ )
     575             :     {
     576       70344 :         set_zero( hMdDec->smooth_buf[i], 2 * SBA_DIRAC_NRG_SMOOTH_LONG + 1 );
     577             :     }
     578             : 
     579       70344 :     for ( i = 0; i < IVAS_SPAR_MAX_CH; i++ )
     580             :     {
     581      773784 :         for ( j = 0; j < IVAS_SPAR_MAX_CH; j++ )
     582             :         {
     583      709302 :             set_zero( hMdDec->mixer_mat_prev2[i][j], IVAS_MAX_NUM_BANDS );
     584             :         }
     585             :     }
     586        5862 :     hMdDec->first_valid_frame = 1;
     587             : 
     588        5862 :     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        5862 : 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       19089 :     for ( i = 0; i < nchan_transport; i++ )
     606             :     {
     607       13227 :         hMdDec->spar_md_cfg.max_freq_per_chan[i] = ivas_spar_br_table_consts[hMdDec->table_idx].fpcs;
     608             :     }
     609             : 
     610        5862 :     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        5862 :     switch ( nchan )
     613             :     {
     614        5085 :         case 4: /* FOA_CHANNELS */
     615        5085 :             hMdDec->num_decorr = IVAS_TD_DECORR_OUT_3CH;
     616        5085 :             break;
     617          72 :         case 9: /* IVAS_HOA_2_CH */
     618          72 :             hMdDec->num_decorr = IVAS_TD_DECORR_OUT_5CH;
     619          72 :             break;
     620         147 :         case 6: /* IVAS_HOA_2_CH */
     621         147 :             hMdDec->num_decorr = IVAS_TD_DECORR_OUT_2CH;
     622         147 :             break;
     623         345 :         case 8: /* IVAS_HOA_3_CH */
     624         345 :             hMdDec->num_decorr = IVAS_TD_DECORR_OUT_4CH;
     625         345 :             break;
     626             :     }
     627             : 
     628        5862 :     hMdDec->spar_md_cfg.num_umx_chs = nchan;
     629             : 
     630        5862 :     dmx_ch = 0;
     631       76206 :     for ( i = 0; i < IVAS_MAX_NUM_BANDS; i++ )
     632             :     {
     633       70344 :         dmx_ch = 0;
     634      229068 :         for ( j = 0; j < nchan_transport; j++ )
     635             :         {
     636      158724 :             if ( pFC[i] < hMdDec->spar_md_cfg.max_freq_per_chan[j] )
     637             :             {
     638      158724 :                 dmx_ch += 1;
     639             :             }
     640             :         }
     641             : 
     642       70344 :         hMdDec->spar_md_cfg.num_dmx_chans_per_band[i] = hMdDec->spar_md_cfg.nchan_transport;
     643       70344 :         hMdDec->spar_md_cfg.num_decorr_per_band[i] = nchan - hMdDec->spar_md_cfg.nchan_transport;
     644             :     }
     645             : 
     646        5862 :     hMdDec->spar_md_cfg.nchan_transport = dmx_ch;
     647             : 
     648        5862 :     return IVAS_ERR_OK;
     649             : }
     650             : 
     651             : 
     652             : /*-----------------------------------------------------------------------------------------*
     653             :  * Function ivas_dec_mono_sba_handling()
     654             :  *
     655             :  *
     656             :  *-----------------------------------------------------------------------------------------*/
     657             : 
     658      445536 : 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      445536 :     mono_flag = 1;
     665             : 
     666     2137392 :     for ( b = 0; b < st_ivas->hQMetaData->q_direction[0].cfg.nbands; b++ )
     667             :     {
     668     8459280 :         for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; ++block )
     669             :         {
     670     6767424 :             float azimuth = st_ivas->hQMetaData->q_direction[0].band_data[b].azimuth[block];
     671     6767424 :             float elevation = st_ivas->hQMetaData->q_direction[0].band_data[b].azimuth[block];
     672     6767424 :             float energy_ratio = st_ivas->hQMetaData->q_direction[0].band_data[0].energy_ratio[block];
     673     6767424 :             if (
     674     1469136 :                 ( azimuth != 0.0f ) ||
     675     1469136 :                 ( elevation != 0.0f ) ||
     676             :                 ( energy_ratio > 0.15f ) ) /* 0.15f is just above the lowest quantised value. */
     677             :             {
     678     5891883 :                 mono_flag = 0;
     679             :             }
     680             :         }
     681             :     }
     682             : 
     683             :     /* Combine the SPAR prediction coefs flag with the azimuth, elevation and energy ratio flag.*/
     684      445536 :     mono_flag = mono_flag && ivas_spar_chk_zero_coefs( st_ivas );
     685             : 
     686      445536 :     if ( mono_flag )
     687             :     {
     688             :         /* Set Energy Ratio values to be zero */
     689         288 :         for ( b = 0; b < st_ivas->hQMetaData->q_direction[0].cfg.nbands; b++ )
     690             :         {
     691         216 :             set_zero( st_ivas->hQMetaData->q_direction[0].band_data[b].energy_ratio, MAX_PARAM_SPATIAL_SUBFRAMES );
     692             :         }
     693          72 :         if ( st_ivas->hDirAC != NULL )
     694             :         {
     695          63 :             for ( block = 0; block < st_ivas->hSpatParamRendCom->dirac_md_buffer_length; ++block )
     696             :             {
     697             :                 /* Set directional Energy Ratio values to be zero */
     698          54 :                 set_zero( st_ivas->hSpatParamRendCom->energy_ratio1[block], st_ivas->hSpatParamRendCom->num_freq_bands );
     699          54 :                 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          54 :                 set_f( st_ivas->hSpatParamRendCom->diffuseness_vector[block], 1.0f, st_ivas->hSpatParamRendCom->num_freq_bands );
     705             :             }
     706             :         }
     707             :     }
     708             : 
     709      445536 :     return;
     710             : }
     711             : 
     712             : 
     713             : /*-----------------------------------------------------------------------------------------*
     714             :  * Function ivas_spar_md_dec_process()
     715             :  *
     716             :  * SPAR Meta Data decoder process
     717             :  *-----------------------------------------------------------------------------------------*/
     718             : 
     719      445536 : 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      445536 :     hMdDec = st_ivas->hSpar->hMdDec;
     734             : 
     735      445536 :     active_w_vlbr = ( st_ivas->hDecoderConfig->ivas_total_brate < IVAS_24k4 ) ? 1 : 0;
     736             : 
     737      445536 :     num_md_chs = ivas_sba_get_nchan_metadata( sba_order, st_ivas->hDecoderConfig->ivas_total_brate );
     738             : 
     739      445536 :     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      445536 :     if ( hMdDec->spar_md_cfg.nchan_transport > 1 && hMdDec->spar_md_cfg.nchan_transport <= 3 )
     742             :     {
     743      209898 :         hMdDec->spar_md.res_ind = 0;
     744      209898 :         dyn_active_w_flag = get_next_indice( st0, 1 );
     745      209898 :         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      235638 :         dyn_active_w_flag = 0;
     761      235638 :         if ( hMdDec->spar_md_cfg.nchan_transport == FOA_CHANNELS )
     762             :         {
     763      121128 :             get_next_indice( st0, 1 );
     764             :         }
     765             :     }
     766             : 
     767      445536 :     ivas_spar_dec_parse_md_bs( hMdDec, st0, &nB, &bw, &dtx_vad, st_ivas->hDecoderConfig->ivas_total_brate,
     768      445536 :                                st_ivas->hQMetaData->sba_inactive_mode );
     769             : 
     770      445536 :     assert( nB == hMdDec->spar_md.num_bands );
     771      445536 :     assert( bw == 1 );
     772      445536 :     ivas_spar_md_fill_invalid_bandcoeffs(
     773             :         hMdDec->spar_md.band_coeffs,
     774             :         hMdDec->band_coeffs_prev,
     775      445536 :         &hMdDec->valid_bands[0],
     776             :         &hMdDec->base_band_coeffs_age[0],
     777             :         &hMdDec->first_valid_frame,
     778             :         nB );
     779             : 
     780      445536 :     ivas_dec_mono_sba_handling( st_ivas );
     781             : 
     782             :     /* SPAR to DirAC conversion */
     783      445536 :     if ( hMdDec->spar_hoa_dirac2spar_md_flag == 1 )
     784             :     {
     785      415731 :         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      445536 :     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     1590561 :     for ( i_ts = 1; i_ts < num_md_sub_frames; i_ts++ )
     826             :     {
     827    10740033 :         for ( b = 0; b < hMdDec->spar_md.num_bands; b++ )
     828             :         {
     829   105545088 :             for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
     830             :             {
     831    95950080 :                 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    76760064 :             for ( j = 0; j < IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS; j++ )
     835             :             {
     836   268660224 :                 for ( k = 0; k < IVAS_SPAR_MAX_DMX_CHS - 1; k++ )
     837             :                 {
     838   201495168 :                     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   105545088 :             for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
     843             :             {
     844    95950080 :                 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      445536 :     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      445536 :     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      445536 :     hMdDec->dtx_md_smoothing_cntr = 1;
     866             : 
     867      445536 :     return;
     868             : }
     869             : 
     870             : 
     871             : /*-----------------------------------------------------------------------------------------*
     872             :  * Function ivas_spar_chk_zero_coefs()
     873             :  *
     874             :  * Check for zeroed SPAR coefficients
     875             :  *-----------------------------------------------------------------------------------------*/
     876             : 
     877        2217 : 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        2217 :     int16_t mono = 1;
     884             :     int16_t ndec, ndm;
     885             : 
     886        2217 :     hMdDec = st_ivas->hSpar->hMdDec;
     887        2217 :     ndec = hMdDec->spar_md_cfg.num_decorr_per_band[0];
     888        2217 :     ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[0];
     889             : 
     890       19953 :     for ( b = 0; b < min( hMdDec->spar_md.num_bands, SPAR_DIRAC_SPLIT_START_BAND ); b++ )
     891             :     {
     892       71184 :         for ( j = 0; j < ndm + ndec - 1; j++ )
     893             :         {
     894       53448 :             if ( hMdDec->spar_md.band_coeffs[b].pred_re[j] != 0.0f )
     895             :             {
     896       28446 :                 mono = 0;
     897             :             }
     898             :         }
     899       66576 :         for ( j = 0; j < ndec; j++ )
     900             :         {
     901       54984 :             for ( k = 0; k < ndm - 1; k++ )
     902             :             {
     903        6144 :                 if ( hMdDec->spar_md.band_coeffs[b].C_re[j][k] != 0.0f )
     904             :                 {
     905        2148 :                     mono = 0;
     906             :                 }
     907             :             }
     908             :         }
     909       66576 :         for ( j = 0; j < ndec; j++ )
     910             :         {
     911       48840 :             if ( hMdDec->spar_md.band_coeffs[b].P_re[j] != 0.0f )
     912             :             {
     913       45273 :                 mono = 0;
     914             :             }
     915             :         }
     916             :     }
     917             : 
     918        2217 :     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        6570 : 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        6570 :     ramp = (float) hMdDec->dtx_md_smoothing_cntr / IVAS_DEFAULT_DTX_CNG_RAMP;
     938             : 
     939       85410 :     for ( b = 0; b < num_bands_out; b++ )
     940             :     {
     941       78840 :         dmx_ch = hMdDec->spar_md_cfg.num_dmx_chans_per_band[b];
     942             : 
     943      315360 :         for ( j = 1; j < FOA_CHANNELS; j++ )
     944             :         {
     945      831276 :             for ( k = dmx_ch; k < FOA_CHANNELS; k++ )
     946             :             {
     947      594756 :                 prev = hMdDec->spar_coeffs_prev.P_re[j][k][b];
     948      594756 :                 tar = hMdDec->spar_coeffs_tar.P_re[j][k][b];
     949      594756 :                 new_val = prev + ( ramp * ( tar - prev ) );
     950      594756 :                 hMdDec->spar_coeffs.P_re[j][k][b] = new_val;
     951             :             }
     952             :         }
     953             : 
     954      394200 :         for ( j = 0; j < FOA_CHANNELS; j++ )
     955             :         {
     956      783792 :             for ( k = 0; k < dmx_ch; k++ )
     957             :             {
     958      468432 :                 prev = hMdDec->spar_coeffs_prev.C_re[j][k][b];
     959      468432 :                 tar = hMdDec->spar_coeffs_tar.C_re[j][k][b];
     960      468432 :                 new_val = prev + ( ramp * ( tar - prev ) );
     961      468432 :                 hMdDec->spar_coeffs.C_re[j][k][b] = new_val;
     962             :             }
     963             :         }
     964             :     }
     965             : 
     966             :     /* expand MD to all time slots */
     967       25569 :     for ( int16_t i_ts = 1; i_ts < num_md_sub_frames; i_ts++ )
     968             :     {
     969      246987 :         for ( b = 0; b < num_bands_out; b++ )
     970             :         {
     971      227988 :             dmx_ch = hMdDec->spar_md_cfg.num_dmx_chans_per_band[b];
     972             : 
     973      911952 :             for ( j = 1; j < FOA_CHANNELS; j++ )
     974             :             {
     975     2391444 :                 for ( k = dmx_ch; k < FOA_CHANNELS; k++ )
     976             : 
     977             :                 {
     978     1707480 :                     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     1139940 :             for ( j = 0; j < FOA_CHANNELS; j++ )
     983             :             {
     984     2283120 :                 for ( k = 0; k < dmx_ch; k++ )
     985             :                 {
     986     1371168 :                     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        6570 :     hMdDec->dtx_md_smoothing_cntr = min( hMdDec->dtx_md_smoothing_cntr + 1, IVAS_DEFAULT_DTX_CNG_RAMP );
     993             : 
     994        6570 :     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         375 : 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         375 :     num_channels = hMdDec->spar_md_cfg.num_umx_chs;
    1014             : 
    1015        1875 :     for ( i = 0; i < num_channels; i++ )
    1016             :     {
    1017        7500 :         for ( j = 0; j < num_channels; j++ )
    1018             :         {
    1019       78000 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1020             :             {
    1021       72000 :                 hMdDec->spar_coeffs_prev.C_re[i][j][k] = hMdDec->spar_coeffs_tar.C_re[i][j][k];
    1022             :             }
    1023             :         }
    1024             :     }
    1025             : 
    1026        1875 :     for ( i = 0; i < num_channels; i++ )
    1027             :     {
    1028        7500 :         for ( j = 0; j < num_channels; j++ )
    1029             :         {
    1030       78000 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1031             :             {
    1032       72000 :                 hMdDec->spar_coeffs_prev.P_re[i][j][k] = hMdDec->spar_coeffs_tar.P_re[i][j][k];
    1033             :             }
    1034             :         }
    1035             :     }
    1036             : 
    1037        1875 :     for ( i = 0; i < num_channels; i++ )
    1038             :     {
    1039        7500 :         for ( j = 0; j < num_channels; j++ )
    1040             :         {
    1041       78000 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1042             :             {
    1043       72000 :                 hMdDec->spar_coeffs_tar.C_re[i][j][k] = hMdDec->spar_coeffs.C_re[i][j][k];
    1044             :             }
    1045             :         }
    1046             :     }
    1047             : 
    1048        1875 :     for ( i = 0; i < num_channels; i++ )
    1049             :     {
    1050        7500 :         for ( j = 0; j < num_channels; j++ )
    1051             :         {
    1052       78000 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1053             :             {
    1054       72000 :                 hMdDec->spar_coeffs_tar.P_re[i][j][k] = hMdDec->spar_coeffs.P_re[i][j][k];
    1055             :             }
    1056             :         }
    1057             :     }
    1058             : 
    1059         375 :     ivas_spar_smooth_md_dtx( hMdDec, num_bands_out, num_md_sub_frames );
    1060             : 
    1061         375 :     return;
    1062             : }
    1063             : 
    1064             : 
    1065             : /*-----------------------------------------------------------------------------------------*
    1066             :  * Function ivas_spar_update_md_hist()
    1067             :  *
    1068             :  * Update previous and target MD
    1069             :  *-----------------------------------------------------------------------------------------*/
    1070             : 
    1071      445161 : 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      445161 :     num_channels = hMdDec->spar_md_cfg.num_umx_chs;
    1078             : 
    1079     2532162 :     for ( i = 0; i < num_channels; i++ )
    1080             :     {
    1081    13336086 :         for ( j = 0; j < num_channels; j++ )
    1082             :         {
    1083   146238105 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1084             :             {
    1085   134989020 :                 hMdDec->spar_coeffs_prev.C_re[i][j][k] = hMdDec->spar_coeffs.C_re[i][j][k];
    1086             :             }
    1087             :         }
    1088             :     }
    1089             : 
    1090     2532162 :     for ( i = 0; i < num_channels; i++ )
    1091             :     {
    1092    13336086 :         for ( j = 0; j < num_channels; j++ )
    1093             :         {
    1094   146238105 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1095             :             {
    1096   134989020 :                 hMdDec->spar_coeffs_prev.P_re[i][j][k] = hMdDec->spar_coeffs.P_re[i][j][k];
    1097             :             }
    1098             :         }
    1099             :     }
    1100             : 
    1101     2532162 :     for ( i = 0; i < num_channels; i++ )
    1102             :     {
    1103    13336086 :         for ( j = 0; j < num_channels; j++ )
    1104             :         {
    1105   146238105 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1106             :             {
    1107   134989020 :                 hMdDec->spar_coeffs_tar.C_re[i][j][k] = hMdDec->spar_coeffs.C_re[i][j][k];
    1108             :             }
    1109             :         }
    1110             :     }
    1111             : 
    1112     2532162 :     for ( i = 0; i < num_channels; i++ )
    1113             :     {
    1114    13336086 :         for ( j = 0; j < num_channels; j++ )
    1115             :         {
    1116   146238105 :             for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
    1117             :             {
    1118   134989020 :                 hMdDec->spar_coeffs_tar.P_re[i][j][k] = hMdDec->spar_coeffs.P_re[i][j][k];
    1119             :             }
    1120             :         }
    1121             :     }
    1122             : 
    1123      445161 :     return;
    1124             : }
    1125             : 
    1126             : 
    1127             : /*-----------------------------------------------------------------------------------------*
    1128             :  * Function ivas_get_spar_matrices()
    1129             :  *
    1130             :  * Get SPAR matrices
    1131             :  *-----------------------------------------------------------------------------------------*/
    1132             : 
    1133      445536 : 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      445536 :     num_bands = num_bands_out;
    1151      445536 :     order = remix_order_set[hMdDec->spar_md_cfg.remix_unmix_order];
    1152             : 
    1153      445536 :     split_band = SPAR_DIRAC_SPLIT_START_BAND;
    1154      445536 :     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      445536 :     if ( bw == IVAS_RED_BAND_FACT )
    1170             :     {
    1171           0 :         num_bands = num_bands >> 1;
    1172             :     }
    1173             : 
    1174      445536 :     active_w = ( dyn_active_w_flag == 1 ) || ( hMdDec->spar_md_cfg.active_w == 1 );
    1175      445536 :     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     2036097 :     for ( i_ts = 0; i_ts < n_ts; i_ts++ )
    1178             :     {
    1179     8592954 :         for ( i = 0; i < numch_out; i++ )
    1180             :         {
    1181    40381350 :             for ( j = 0; j < numch_out; j++ )
    1182             :             {
    1183    33378957 :                 set_zero( &hMdDec->spar_coeffs.C_re[i][j][i_ts * IVAS_MAX_NUM_BANDS], IVAS_MAX_NUM_BANDS );
    1184    33378957 :                 set_zero( &hMdDec->spar_coeffs.P_re[i][j][i_ts * IVAS_MAX_NUM_BANDS], IVAS_MAX_NUM_BANDS );
    1185             :             }
    1186             :         }
    1187     1590561 :         num_bands = min( num_bands, nB );
    1188             : 
    1189    20391993 :         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    18801432 :             dmx_ch = hMdDec->spar_md_cfg.num_dmx_chans_per_band[bw * b];
    1196             : 
    1197   101640948 :             for ( j = 0; j < numch_out; j++ )
    1198             :             {
    1199    82839516 :                 set_zero( tmp_C1_re[j], numch_out );
    1200    82839516 :                 set_zero( tmp_C2_re[j], numch_out );
    1201    82839516 :                 set_zero( tmp_dm_re[j], numch_out );
    1202             : 
    1203    82839516 :                 tmp_C1_re[j][j] = 1.0f;
    1204    82839516 :                 tmp_C2_re[j][j] = 1.0f;
    1205    82839516 :                 tmp_dm_re[j][j] = 1.0f;
    1206             :             }
    1207             : 
    1208    82839516 :             for ( j = 1; j < numch_out; j++ )
    1209             :             {
    1210    64038084 :                 tmp_C1_re[j][0] = hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].pred_re[j - 1];
    1211             :             }
    1212             : 
    1213    18801432 :             if ( active_w == 1 )
    1214             :             {
    1215    16675920 :                 for ( j = 1; j < numch_out; j++ )
    1216             :                 {
    1217    12506940 :                     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     4168980 :                 IVAS_RMULT_FLOAT( tmp_C2_re[0][1], tmp_C1_re[1][0], re );
    1221     4168980 :                 tmp_dm_re[0][0] = 1 + re;
    1222             : 
    1223     4168980 :                 IVAS_RMULT_FLOAT( tmp_C2_re[0][2], tmp_C1_re[2][0], re );
    1224     4168980 :                 tmp_dm_re[0][0] += re;
    1225             : 
    1226     4168980 :                 IVAS_RMULT_FLOAT( tmp_C2_re[0][3], tmp_C1_re[3][0], re );
    1227     4168980 :                 tmp_dm_re[0][0] += re;
    1228             : 
    1229     4168980 :                 if ( dyn_active_w_flag == 1 )
    1230             :                 {
    1231           0 :                     tmp_dm_re[0][0] *= IVAS_SPAR_DYN_ACTIVEW_THRESH;
    1232             :                 }
    1233             : 
    1234     4168980 :                 tmp_dm_re[0][1] = tmp_C2_re[0][1];
    1235             : 
    1236     4168980 :                 tmp_dm_re[0][2] = tmp_C2_re[0][2];
    1237             : 
    1238     4168980 :                 tmp_dm_re[0][3] = tmp_C2_re[0][3];
    1239             : 
    1240     4168980 :                 tmp_dm_re[1][0] = tmp_C1_re[1][0];
    1241             : 
    1242     4168980 :                 tmp_dm_re[2][0] = tmp_C1_re[2][0];
    1243             : 
    1244     4168980 :                 tmp_dm_re[3][0] = tmp_C1_re[3][0];
    1245             : 
    1246     4168980 :                 if ( hMdDec->spar_md_cfg.remix_unmix_order != 3 )
    1247             :                 {
    1248     4168980 :                     ivas_mat_col_rearrange( tmp_dm_re, order, i_ts, hMdDec->mixer_mat, b, numch_out );
    1249             :                 }
    1250             :             }
    1251             :             else
    1252             :             {
    1253    14632452 :                 if ( hMdDec->spar_md_cfg.remix_unmix_order != 3 )
    1254             :                 {
    1255    14632452 :                     ivas_mat_col_rearrange( tmp_C1_re, order, i_ts, hMdDec->mixer_mat, b, numch_out );
    1256             :                 }
    1257             :             }
    1258             : 
    1259    18801432 :             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   101640948 :                 for ( j = 0; j < numch_out; j++ )
    1265             :                 {
    1266    82839516 :                     set_zero( tmpC_re[j], numch_out );
    1267    82839516 :                     set_zero( tmpP_re[j], numch_out );
    1268             :                 }
    1269             : 
    1270   101640948 :                 for ( j = 0; j < numch_out; j++ )
    1271             :                 {
    1272    82839516 :                     set_zero( tmpC_re[j], numch_out );
    1273             :                 }
    1274             : 
    1275    66108900 :                 for ( k = 0; k < dmx_ch; k++ )
    1276             :                 {
    1277    47307468 :                     tmpC_re[k][k] = 1;
    1278             :                 }
    1279             : 
    1280    54333480 :                 for ( j = dmx_ch; j < numch_out; j++ )
    1281             :                 {
    1282    78346404 :                     for ( k = 1; k < dmx_ch; k++ )
    1283             :                     {
    1284    42814356 :                         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    54333480 :                 for ( j = dmx_ch; j < numch_out; j++ )
    1289             :                 {
    1290   132923304 :                     for ( k = dmx_ch; k < numch_out; k++ )
    1291             :                     {
    1292    97391256 :                         if ( ( j - dmx_ch ) == ( k - dmx_ch ) )
    1293             :                         {
    1294    35532048 :                             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    61859208 :                             tmpP_re[j][k] = 0;
    1299             :                         }
    1300             :                     }
    1301             :                 }
    1302             : 
    1303    82839516 :                 for ( j = 1; j < numch_out; j++ )
    1304             :                 {
    1305   204243696 :                     for ( k = dmx_ch; k < numch_out; k++ )
    1306             :                     {
    1307   972684588 :                         for ( m = 0; m < numch_out; m++ )
    1308             :                         {
    1309   832478976 :                             IVAS_RMULT_FLOAT( hMdDec->mixer_mat[j][m][b + i_ts * IVAS_MAX_NUM_BANDS], tmpP_re[m][k], re );
    1310   832478976 :                             hMdDec->spar_coeffs.P_re[j][k][( b * bw ) + i_ts * IVAS_MAX_NUM_BANDS] += re;
    1311             :                         }
    1312             :                     }
    1313             :                 }
    1314             : 
    1315   101640948 :                 for ( j = 0; j < numch_out; j++ )
    1316             :                 {
    1317   302604540 :                     for ( k = 0; k < dmx_ch; k++ )
    1318             :                     {
    1319  1355403600 :                         for ( m = 0; m < numch_out; m++ )
    1320             :                         {
    1321  1135638576 :                             IVAS_RMULT_FLOAT( hMdDec->mixer_mat[j][m][b + i_ts * IVAS_MAX_NUM_BANDS], tmpC_re[m][k], re );
    1322  1135638576 :                             hMdDec->spar_coeffs.C_re[j][k][( b * bw ) + i_ts * IVAS_MAX_NUM_BANDS] += re;
    1323             :                         }
    1324             :                     }
    1325             :                 }
    1326             : 
    1327    18801432 :                 hMdDec->spar_coeffs.C_re[0][0][( b * bw ) + i_ts * IVAS_MAX_NUM_BANDS] =
    1328    18801432 :                     max( 0, hMdDec->spar_coeffs.C_re[0][0][( b * bw ) + i_ts * IVAS_MAX_NUM_BANDS] );
    1329             :             }
    1330             :         }
    1331             : 
    1332             :         /* band mixing */
    1333     1590561 :         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      445536 :     return;
    1358             : }
    1359             : 
    1360             : 
    1361             : /*-----------------------------------------------------------------------------------------*
    1362             :  * Function ivas_mat_col_rearrange()
    1363             :  *
    1364             :  * reorders the input matrix based on order
    1365             :  *-----------------------------------------------------------------------------------------*/
    1366             : 
    1367    18801432 : 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   101640948 :     for ( i = 0; i < num_ch; i++ )
    1378             :     {
    1379    82839516 :         idx = order[i];
    1380             : 
    1381   478342200 :         for ( j = 0; j < num_ch; j++ )
    1382             :         {
    1383   395502684 :             mixer_mat[j][i][bands + i_ts * IVAS_MAX_NUM_BANDS] = in_re[j][idx];
    1384             :         }
    1385             :     }
    1386             : 
    1387    18801432 :     return;
    1388             : }
    1389             : 
    1390             : 
    1391             : /*-----------------------------------------------------------------------------------------*
    1392             :  * Function ivas_spar_dec_gen_umx_mat()
    1393             :  *
    1394             :  * generates upmix matrix
    1395             :  *-----------------------------------------------------------------------------------------*/
    1396             : 
    1397      574437 : 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      574437 :     num_out_ch = hMdDec->spar_md_cfg.num_umx_chs;
    1407             : 
    1408     2640147 :     for ( i_ts = 0; i_ts < num_md_sub_frames; i_ts++ )
    1409             :     {
    1410     2065710 :         if ( hMdDec->td_decorr_flag == 1 )
    1411             :         {
    1412    10937220 :             for ( i = 0; i < num_out_ch; i++ )
    1413             :             {
    1414    30267294 :                 for ( j = 0; j < nchan_transport; j++ )
    1415             :                 {
    1416   275224632 :                     for ( b = 0; b < num_bands_out; b++ )
    1417             :                     {
    1418   253828848 :                         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    10937220 :             for ( i = 0; i < num_out_ch; i++ )
    1424             :             {
    1425    28122216 :                 for ( j = nchan_transport; j < num_out_ch; j++ )
    1426             :                 {
    1427   247348122 :                     for ( b = 0; b < num_bands_out; b++ )
    1428             :                     {
    1429   228097416 :                         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      574437 :     ivas_spar_dec_compute_ramp_down_post_matrix( hMdDec, num_bands_out, bfi, num_md_sub_frames );
    1470             : 
    1471      574437 :     return;
    1472             : }
    1473             : 
    1474       35664 : 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       35664 :     bw_fact = *bands_bw / bw_final;
    1486      174972 :     for ( i = *nB - 1; i >= 0; i-- )
    1487             :     {
    1488             : 
    1489      424620 :         for ( b = bw_fact - 1; b >= 0; b-- )
    1490             :         {
    1491      285312 :             idx = i * bw_fact + b;
    1492     1141248 :             for ( ii = 0; ii < ndec + ndm - 1; ii++ )
    1493             :             {
    1494      855936 :                 band_coeffs[idx].pred_re[ii] = band_coeffs[i].pred_re[ii];
    1495             :             }
    1496     1133592 :             for ( ii = 0; ii < ndec; ii++ )
    1497             :             {
    1498      863592 :                 for ( jj = 0; jj < ndm - 1; jj++ )
    1499             :                 {
    1500       15312 :                     band_coeffs[idx].C_re[ii][jj] = band_coeffs[i].C_re[ii][jj];
    1501             :                 }
    1502             :             }
    1503     1133592 :             for ( jj = 0; jj < ndec; jj++ )
    1504             :             {
    1505      848280 :                 band_coeffs[idx].P_re[jj] = band_coeffs[i].P_re[jj];
    1506             :             }
    1507      285312 :             valid_bands[idx] = valid_bands[i];
    1508             :         }
    1509             :     }
    1510       35664 :     *nB = ( *nB ) * ( *bands_bw ) / bw_final;
    1511       35664 :     *bands_bw = bw_final;
    1512             : 
    1513       35664 :     return;
    1514             : }
    1515             : 
    1516             : /*-----------------------------------------------------------------------------------------*
    1517             :  * Function ivas_spar_dec_parse_md_bs()
    1518             :  *
    1519             :  * Parse SPAR MD bitstream
    1520             :  *-----------------------------------------------------------------------------------------*/
    1521             : 
    1522      445536 : 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      445536 :     *dtx_vad = 1;
    1540      445536 :     *bands_bw = 1;
    1541      445536 :     qsi = 0;
    1542      445536 :     num_bands = hMdDec->spar_md.num_bands;
    1543             : 
    1544      445536 :     if ( ivas_total_brate > IVAS_SID_5k2 )
    1545             :     {
    1546      444564 :         if ( hMdDec->spar_md_cfg.quant_strat_bits > 0 )
    1547             :         {
    1548      444564 :             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      121128 :                 qsi = get_next_indice( st0, 1 );
    1552      121128 :                 if ( qsi == 1 )
    1553             :                 {
    1554           0 :                     *dtx_vad = 0;
    1555             :                 }
    1556             :             }
    1557             :             else
    1558             :             {
    1559      323436 :                 if ( sba_inactive_mode == 1 )
    1560             :                 {
    1561         702 :                     *dtx_vad = 0;
    1562         702 :                     qsi = hMdDec->spar_md_cfg.quant_strat_bits + 1;
    1563             :                 }
    1564             :                 else
    1565             :                 {
    1566      322734 :                     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         972 :         *dtx_vad = 0;
    1578             :     }
    1579             : 
    1580      445536 :     hMdDec->dtx_vad = *dtx_vad;
    1581             : 
    1582      445536 :     if ( *dtx_vad == 0 )
    1583             :     {
    1584        1674 :         *nB = SPAR_DTX_BANDS;
    1585        1674 :         *bands_bw = num_bands / *nB;
    1586             : 
    1587        5022 :         for ( i = 0; i < *nB; i++ )
    1588             :         {
    1589       36828 :             for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
    1590             :             {
    1591       33480 :                 hMdDec->spar_md.band_coeffs[i].pred_re[j] = 0;
    1592       33480 :                 hMdDec->spar_md.band_coeffs[i].P_re[j] = 0;
    1593             :             }
    1594        3348 :             hMdDec->valid_bands[i] = 1;
    1595             :         }
    1596             : 
    1597       15066 :         for ( i = 0; i < num_bands; i++ )
    1598             :         {
    1599      107136 :             for ( j = 0; j < ( IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS ); j++ )
    1600             :             {
    1601      374976 :                 for ( k = 0; k < ( IVAS_SPAR_MAX_DMX_CHS - 1 ); k++ )
    1602             :                 {
    1603      281232 :                     hMdDec->spar_md.band_coeffs[i].C_re[j][k] = 0;
    1604             :                 }
    1605             :             }
    1606             :         }
    1607             : 
    1608        1674 :         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        1674 :         if ( *bands_bw != 1 )
    1611             :         {
    1612        1674 :             ndec = hMdDec->spar_md_cfg.num_decorr_per_band[0];
    1613        1674 :             ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[0];
    1614        1674 :             ivas_spar_md_band_upmix(
    1615             :                 hMdDec->spar_md.band_coeffs,
    1616             :                 nB,
    1617             :                 bands_bw,
    1618        1674 :                 hMdDec->valid_bands,
    1619             :                 1,
    1620             :                 ndec,
    1621             :                 ndm );
    1622             :         }
    1623             : 
    1624        1674 :         return;
    1625             :     }
    1626             : 
    1627      443862 :     qs = hMdDec->spar_md_cfg.quant_strat[qsi];
    1628             : 
    1629      443862 :     strat = get_next_indice( st0, 3 );
    1630             : 
    1631      443862 :     no_ec = 0;
    1632             : 
    1633      443862 :     if ( strat < 2 )
    1634             :     {
    1635      172656 :         *bands_bw = strat + 1;
    1636      172656 :         *nB = num_bands / *bands_bw;
    1637     1814484 :         for ( i = 0; i < *nB; i++ )
    1638             :         {
    1639     1641828 :             do_diff[i] = 0;
    1640     1641828 :             do_repeat[i] = 0;
    1641             :         }
    1642             :     }
    1643      271206 :     else if ( strat < 4 )
    1644             :     {
    1645        8031 :         *bands_bw = strat - 1;
    1646        8031 :         *nB = num_bands / *bands_bw;
    1647       71187 :         for ( i = 0; i < *nB; i++ )
    1648             :         {
    1649       63156 :             do_diff[i] = 0;
    1650       63156 :             do_repeat[i] = 0;
    1651             :         }
    1652        8031 :         no_ec = 1;
    1653             :     }
    1654      263175 :     else if ( ivas_total_brate < IVAS_24k4 )
    1655             :     {
    1656       32823 :         *bands_bw = 2;
    1657       32823 :         *nB = num_bands / *bands_bw;
    1658             : 
    1659      164115 :         for ( i = 0; i < *nB; i++ )
    1660             :         {
    1661      131292 :             do_diff[i] = 0;
    1662      131292 :             do_repeat[i] = ( ( strat % 2 ) == ( ( i + 1 ) % 2 ) );
    1663             :         }
    1664             :     }
    1665             :     else
    1666             :     {
    1667      230352 :         *bands_bw = 1;
    1668      230352 :         *nB = num_bands;
    1669             : 
    1670     2073168 :         for ( i = 0; i < *nB; i++ )
    1671             :         {
    1672     1842816 :             do_diff[i] = ( ( ( i + 1 ) & 3 ) != strat - 4 );
    1673     1842816 :             do_repeat[i] = 0;
    1674             :         }
    1675      230352 :         if ( hMdDec->spar_md_cfg.prev_quant_idx >= 0 )
    1676             :         {
    1677      230289 :             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      443862 :     hMdDec->spar_md_cfg.prev_quant_idx = qsi;
    1684             : 
    1685      443862 :     if ( no_ec == 0 )
    1686             :     {
    1687      435831 :         ivas_decode_arith_bs( hMdDec, st0, qsi, *nB, *bands_bw, do_diff, strat, ivas_total_brate );
    1688             :     }
    1689             :     else
    1690             :     {
    1691        8031 :         ivas_decode_huffman_bs( hMdDec, st0, qsi, *nB, *bands_bw );
    1692             :     }
    1693             : 
    1694     4122954 :     for ( i = 0; i < *nB; i++ )
    1695             :     {
    1696     3679092 :         ndec = hMdDec->spar_md_cfg.num_decorr_per_band[( *bands_bw ) * i];
    1697     3679092 :         ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[( *bands_bw ) * i];
    1698             : 
    1699     3679092 :         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     3679092 :         j = 0;
    1702    12258960 :         for ( ii = 0; ii < ndec; ii++ )
    1703             :         {
    1704    22951776 :             for ( jj = 0; jj < ndm - 1; jj++ )
    1705             :             {
    1706    14371908 :                 quant[j] = hMdDec->spar_md.band_coeffs[i].C_re[ii][jj];
    1707    14371908 :                 j++;
    1708             :             }
    1709             :         }
    1710             : 
    1711     3679092 :         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     3679092 :         j = 0;
    1714    12258960 :         for ( ii = 0; ii < ndec; ii++ )
    1715             :         {
    1716    22951776 :             for ( jj = 0; jj < ndm - 1; jj++ )
    1717             :             {
    1718    14371908 :                 hMdDec->spar_md.band_coeffs[i].C_re[ii][jj] = quant[j];
    1719    14371908 :                 j++;
    1720             :             }
    1721             :         }
    1722             : 
    1723     3679092 :         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    18392652 :         for ( j = 0; j < ndm + ndec - 1; j++ )
    1727             :         {
    1728    14713560 :             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    18051000 :         for ( j = 0; j < ndec * ( ndm - 1 ); j++ )
    1731             :         {
    1732    14371908 :             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    12258960 :         for ( j = 0; j < ndec; j++ )
    1735             :         {
    1736     8579868 :             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     3679092 :         hMdDec->valid_bands[i] |= ( do_diff[i] == 0 && do_repeat[i] == 0 ) ? 1 : 0;
    1739             :     }
    1740             : 
    1741      443862 :     ndec = hMdDec->spar_md_cfg.num_decorr_per_band[0];
    1742      443862 :     ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[0];
    1743      443862 :     if ( *bands_bw != 1 )
    1744             :     {
    1745       33990 :         ivas_spar_md_band_upmix(
    1746             :             hMdDec->spar_md.band_coeffs,
    1747             :             nB,
    1748             :             bands_bw,
    1749       33990 :             hMdDec->valid_bands,
    1750             :             1,
    1751             :             ndec,
    1752             :             ndm );
    1753             :     }
    1754             : 
    1755             : 
    1756      443862 :     return;
    1757             : }
    1758             : 
    1759             : 
    1760             : /*-----------------------------------------------------------------------------------------*
    1761             :  * Function ivas_decode_arith_bs()
    1762             :  *
    1763             :  * Decode bitstream with arith decoder
    1764             :  *-----------------------------------------------------------------------------------------*/
    1765             : 
    1766      435831 : 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     4051767 :     for ( i = 0; i < nB; i++ )
    1787             :     {
    1788     3615936 :         ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[bands_bw * i];
    1789     3615936 :         ndec = hMdDec->spar_md_cfg.num_decorr_per_band[bands_bw * i];
    1790     3615936 :         if ( ( ivas_total_brate < IVAS_24k4 ) && ( strat > 3 ) && ( ( ( i % 2 == 1 ) && ( strat % 2 == 0 ) ) || ( ( i % 2 == 0 ) && ( strat % 2 == 1 ) ) ) )
    1791             :         {
    1792       65646 :             pred_cell_dims[i].dim1 = 0;
    1793       65646 :             pred_cell_dims[i].dim2 = 0;
    1794       65646 :             drct_cell_dims[i].dim1 = 0;
    1795       65646 :             drct_cell_dims[i].dim2 = 0;
    1796       65646 :             decd_cell_dims[i].dim1 = 0;
    1797       65646 :             decd_cell_dims[i].dim2 = 0;
    1798       65646 :             decx_cell_dims[i].dim1 = 0;
    1799       65646 :             decx_cell_dims[i].dim2 = 0;
    1800             :         }
    1801             :         else
    1802             :         {
    1803     3550290 :             pred_cell_dims[i].dim1 = ndm + ndec - 1;
    1804     3550290 :             if ( hMdDec->spar_hoa_md_flag && hMdDec->spar_hoa_dirac2spar_md_flag )
    1805             :             {
    1806      434808 :                 if ( i >= SPAR_DIRAC_SPLIT_START_BAND )
    1807             :                 {
    1808      144936 :                     pred_cell_dims[i].dim1 -= ( FOA_CHANNELS - 1 );
    1809             :                 }
    1810             :             }
    1811     3550290 :             pred_cell_dims[i].dim2 = 1;
    1812     3550290 :             drct_cell_dims[i].dim1 = ndec;
    1813     3550290 :             drct_cell_dims[i].dim2 = ndm - 1;
    1814     3550290 :             decd_cell_dims[i].dim1 = ndec;
    1815     3550290 :             decd_cell_dims[i].dim2 = 1;
    1816     3550290 :             decx_cell_dims[i].dim1 = ( ndec * ( ndec - 1 ) ) >> 1;
    1817     3550290 :             decx_cell_dims[i].dim2 = 1;
    1818             :         }
    1819             :     }
    1820             : 
    1821      435831 :     any_diff = 0;
    1822     2266764 :     for ( i = 0; i < nB; i++ )
    1823             :     {
    1824     2061285 :         if ( pDo_diff[i] != 0 )
    1825             :         {
    1826      230352 :             any_diff = 1;
    1827      230352 :             break;
    1828             :         }
    1829             :     }
    1830             : 
    1831      435831 :     if ( any_diff == 1 )
    1832             :     {
    1833      230352 :         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      230352 :         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      435831 :     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      435831 :     ivas_fill_band_coeffs_idx( hMdDec->spar_md.band_coeffs_idx, nB, symbol_arr_re, pred_cell_dims, PRED_COEFF );
    1855             : 
    1856      435831 :     if ( hMdDec->spar_hoa_md_flag && hMdDec->spar_hoa_dirac2spar_md_flag )
    1857             :     {
    1858      471042 :         for ( i = 0; i < nB; i++ )
    1859             :         {
    1860      434808 :             if ( i >= SPAR_DIRAC_SPLIT_START_BAND )
    1861             :             {
    1862      589992 :                 for ( j = pred_cell_dims[i].dim1 - 1; j >= 0; j-- )
    1863             :                 {
    1864      445056 :                     hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j + ( FOA_CHANNELS - 1 )] =
    1865      445056 :                         hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j];
    1866             :                 }
    1867      579744 :                 for ( j = 0; j < FOA_CHANNELS - 1; j++ )
    1868             :                 {
    1869      434808 :                     hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j] = 0;
    1870             :                 }
    1871             :             }
    1872             :         }
    1873             :     }
    1874             : 
    1875      435831 :     if ( any_diff == 1 )
    1876             :     {
    1877      230352 :         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      435831 :     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      435831 :     ivas_fill_band_coeffs_idx( hMdDec->spar_md.band_coeffs_idx, nB, symbol_arr_re, drct_cell_dims, DRCT_COEFF );
    1884             : 
    1885      435831 :     if ( any_diff == 1 )
    1886             :     {
    1887      230352 :         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      435831 :     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      435831 :     ivas_fill_band_coeffs_idx( hMdDec->spar_md.band_coeffs_idx, nB, symbol_arr_re, decd_cell_dims, DECD_COEFF );
    1894             : 
    1895      435831 :     if ( any_diff == 1 )
    1896             :     {
    1897      230352 :         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      435831 :     ivas_fill_band_coeffs_idx( hMdDec->spar_md.band_coeffs_idx, nB, symbol_arr_re, decx_cell_dims, DECX_COEFF );
    1901             : 
    1902      435831 :     return;
    1903             : }
    1904             : 
    1905             : 
    1906             : /*-----------------------------------------------------------------------------------------*
    1907             :  * Function ivas_fill_band_coeffs_idx()
    1908             :  *
    1909             :  * Copy pred band coeffs to arr
    1910             :  *-----------------------------------------------------------------------------------------*/
    1911             : 
    1912     1743324 : 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     1743324 :     int16_t *pPtr_idx = NULL;
    1921             : 
    1922    16207068 :     for ( i = 0; i < nB; i++ )
    1923             :     {
    1924    14463744 :         switch ( coeff_type )
    1925             :         {
    1926     3615936 :             case PRED_COEFF:
    1927             :             {
    1928     3615936 :                 pPtr_idx = pBands_idx[i].pred_index_re;
    1929     3615936 :                 break;
    1930             :             }
    1931     3615936 :             case DRCT_COEFF:
    1932             :             {
    1933     3615936 :                 pPtr_idx = pBands_idx[i].drct_index_re;
    1934     3615936 :                 break;
    1935             :             }
    1936     3615936 :             case DECD_COEFF:
    1937             :             {
    1938     3615936 :                 pPtr_idx = pBands_idx[i].decd_index_re;
    1939     3615936 :                 break;
    1940             :             }
    1941     3615936 :             case DECX_COEFF:
    1942             :             {
    1943     3615936 :                 break;
    1944             :             }
    1945             : 
    1946           0 :             default:
    1947           0 :                 assert( !"unsupported config!" );
    1948             :         }
    1949             : 
    1950    14463744 :         if ( coeff_type != DECX_COEFF )
    1951             :         {
    1952    10847808 :             len = pCell_dims[i].dim1 * pCell_dims[i].dim2;
    1953    10847808 :             mvs2s( pSymbol_re, pPtr_idx, len );
    1954    10847808 :             pSymbol_re += len;
    1955             :         }
    1956             :     }
    1957             : 
    1958     1743324 :     return;
    1959             : }
    1960             : 
    1961             : 
    1962             : /*-----------------------------------------------------------------------------------------*
    1963             :  * Function ivas_decode_huffman_bs()
    1964             :  *
    1965             :  * Decode bitstream with huffman decoder
    1966             :  *-----------------------------------------------------------------------------------------*/
    1967             : 
    1968        8031 : 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       71187 :     for ( i = 0; i < nB; i++ )
    1980             :     {
    1981       63156 :         ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[bands_bw * i];
    1982       63156 :         ndec = hMdDec->spar_md_cfg.num_decorr_per_band[bands_bw * i];
    1983             : 
    1984       63156 :         pred_dim = ndec + ndm - 1;
    1985       63156 :         drct_dim = ndec * ( ndm - 1 );
    1986       63156 :         decd_dim = ndec;
    1987       63156 :         pred_offset = 0;
    1988             : 
    1989       63156 :         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      252624 :         for ( j = pred_offset; j < pred_dim; j++ )
    1998             :         {
    1999      189468 :             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       63156 :         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       69780 :         for ( j = 0; j < drct_dim; j++ )
    2014             :         {
    2015        6624 :             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       75432 :         for ( j = 0; j < decd_dim; j++ )
    2019             :         {
    2020       12276 :             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        8031 :     return;
    2025             : }
    2026             : 
    2027      891072 : 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      891072 :     set_s( valid_band_idx, 0, IVAS_MAX_NUM_BANDS );
    2039      891072 :     set_s( last_valid_band_idx, 0, IVAS_MAX_NUM_BANDS );
    2040      891072 :     idx = -1;
    2041      891072 :     *all_valid = 1;
    2042     9993624 :     for ( b = 0; b < num_bands; b++ )
    2043             :     {
    2044     9102552 :         if ( valid_bands[b] != 0 )
    2045             :         {
    2046     8990016 :             base_band_age[b] = 0; /* reset band age */
    2047     8990016 :             idx++;
    2048     8990016 :             valid_band_idx[idx] = b;
    2049             :         }
    2050             :         else
    2051             :         {
    2052      112536 :             base_band_age[b] += 1; /* increment the age of invalid bands */
    2053             : 
    2054      112536 :             if ( base_band_age[b] > 3 )
    2055             :             {
    2056       16380 :                 last_valid_band_idx[b] = idx;
    2057             :             }
    2058      112536 :             *all_valid = 0;
    2059             :         }
    2060             :     }
    2061      891072 :     *b_idx = idx;
    2062             : 
    2063      891072 :     return;
    2064             : }
    2065             : 
    2066       16380 : 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       16380 :     if ( last_valid_band_idx < 0 ) /* Extrapolation */
    2076             :     {
    2077        4698 :         *id1 = valid_band_idx[0];
    2078        4698 :         *id0 = 0;
    2079        4698 :         *w = 1;
    2080             :     }
    2081       11682 :     else if ( last_valid_band_idx == idx ) /* Extrapolation */
    2082             :     {
    2083        1281 :         *id1 = valid_band_idx[last_valid_band_idx];
    2084        1281 :         *id0 = valid_band_idx[last_valid_band_idx];
    2085        1281 :         *w = 0;
    2086             :     }
    2087             :     else /* Interpolation */
    2088             :     {
    2089       10401 :         *id0 = valid_band_idx[last_valid_band_idx];
    2090       10401 :         *id1 = valid_band_idx[last_valid_band_idx + 1];
    2091       10401 :         *w = ( (float) ( b - *id0 ) ) / ( *id1 - *id0 );
    2092             :     }
    2093       16380 :     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      445536 : 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      445536 :     int16_t valid_band_idx[IVAS_MAX_NUM_BANDS], idx = -1;
    2113             :     int16_t last_valid_band_idx[IVAS_MAX_NUM_BANDS];
    2114      445536 :     float w = 0;
    2115      445536 :     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      445536 :     assert( idx > 0 ); /* some bands should be valid */
    2118             : 
    2119      445536 :     if ( all_valid == 0 )
    2120             :     {
    2121      155904 :         for ( b = 0; b < num_bands; b++ )
    2122             :         {
    2123             :             /* check against non zero in if and else if */
    2124      143394 :             if ( base_band_age[b] > 3 ) /* old invalid bands */
    2125             :             {
    2126             :                 int16_t id0, id1;
    2127        8190 :                 ivas_spar_get_plc_interp_weights( valid_band_idx, last_valid_band_idx[b],
    2128             :                                                   idx, b, &w, &id0, &id1 );
    2129       40950 :                 for ( i = 0; i < num_channels; i++ )
    2130             :                 {
    2131      163800 :                     for ( j = 0; j < num_channels; j++ )
    2132             :                     {
    2133      131040 :                         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      131040 :                         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      135204 :                 if ( valid_bands[b] == 0 )
    2141             :                 {
    2142      240390 :                     for ( i = 0; i < num_channels; i++ )
    2143             :                     {
    2144      961560 :                         for ( j = 0; j < num_channels; j++ )
    2145             :                         {
    2146      769248 :                             pSpar_coeffs->C_re[i][j][b] = pSpar_coeffs_prev->C_re[i][j][b];
    2147      769248 :                             pSpar_coeffs->P_re[i][j][b] = pSpar_coeffs_prev->P_re[i][j][b];
    2148             :                         }
    2149             :                     }
    2150             :                 }
    2151             :             }
    2152             : 
    2153      143394 :             if ( valid_bands[b] == 0 )
    2154             :             {
    2155             :                 int16_t i_ts;
    2156      281340 :                 for ( i = 0; i < num_channels; i++ )
    2157             :                 {
    2158     1125360 :                     for ( j = 0; j < num_channels; j++ )
    2159             :                     {
    2160     3538656 :                         for ( i_ts = 1; i_ts < num_md_sub_frames; i_ts++ )
    2161             :                         {
    2162     2638368 :                             pSpar_coeffs->C_re[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] = pSpar_coeffs->C_re[i][j][b];
    2163     2638368 :                             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      445536 :     return;
    2172             : }
    2173             : 
    2174      445536 : 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      445536 :     int16_t valid_band_idx[IVAS_MAX_NUM_BANDS], idx = -1;
    2184             :     int16_t last_valid_band_idx[IVAS_MAX_NUM_BANDS];
    2185      445536 :     float w = 0;
    2186             : 
    2187      445536 :     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      445536 :     assert( idx > 0 ); /* some bands should be valid */
    2191             : 
    2192      445536 :     if ( all_valid == 0 )
    2193             :     {
    2194      112590 :         for ( b = 0; b < num_bands; b++ )
    2195             :         {
    2196             :             /* check against non zero in if and else if */
    2197      100080 :             if ( ( base_band_age[b] > 3 ) || ( *first_valid_frame == 0 ) ) /* old invalid bands */
    2198        8190 :             {
    2199             :                 int16_t id0, id1;
    2200        8190 :                 ivas_spar_get_plc_interp_weights( valid_band_idx, last_valid_band_idx[b],
    2201             :                                                   idx, b, &w, &id0, &id1 );
    2202             : 
    2203       90090 :                 for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
    2204             :                 {
    2205       81900 :                     pBand_coeffs[b].pred_re[j] = ( 1 - w ) * pBand_coeffs[id0].pred_re[j] + w * pBand_coeffs[id1].pred_re[j];
    2206             :                 }
    2207             : 
    2208       65520 :                 for ( j = 0; j < IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS; j++ )
    2209             :                 {
    2210      229320 :                     for ( k = 0; k < IVAS_SPAR_MAX_DMX_CHS - 1; k++ )
    2211             :                     {
    2212      171990 :                         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       90090 :                 for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
    2217             :                 {
    2218       81900 :                     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       91890 :                 if ( valid_bands[b] == 0 )
    2224             :                 {
    2225      528858 :                     for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
    2226             :                     {
    2227      480780 :                         pBand_coeffs[b].pred_re[j] = pBand_coeffs_prev[b].pred_re[j];
    2228             :                     }
    2229             : 
    2230      384624 :                     for ( j = 0; j < IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS; j++ )
    2231             :                     {
    2232     1346184 :                         for ( k = 0; k < IVAS_SPAR_MAX_DMX_CHS - 1; k++ )
    2233             :                         {
    2234     1009638 :                             pBand_coeffs[b].C_re[j][k] = pBand_coeffs_prev[b].C_re[j][k];
    2235             :                         }
    2236             :                     }
    2237             : 
    2238      528858 :                     for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
    2239             :                     {
    2240      480780 :                         pBand_coeffs[b].P_re[j] = pBand_coeffs_prev[b].P_re[j];
    2241             :                     }
    2242             :                 }
    2243             :             }
    2244             :         }
    2245             :     }
    2246             :     else
    2247             :     {
    2248      433026 :         *first_valid_frame = 1;
    2249             :     }
    2250             : 
    2251      445536 :     return;
    2252             : }
    2253             : 
    2254             : 
    2255             : /*-----------------------------------------------------------------------------------------*
    2256             :  * Function ivas_spar_dec_compute_ramp_down_post_matrix()
    2257             :  *
    2258             :  *
    2259             :  *-----------------------------------------------------------------------------------------*/
    2260             : 
    2261      574437 : 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      574437 :     num_in_ch = hMdDec->spar_md_cfg.num_umx_chs;
    2270      574437 :     num_out_ch = hMdDec->spar_md_cfg.num_umx_chs;
    2271             : 
    2272      574437 :     if ( bfi == 0 )
    2273             :     {
    2274      554454 :         hMdDec->spar_plc_num_lost_frames = 0;
    2275             :     }
    2276             :     else
    2277             :     {
    2278       19983 :         if ( hMdDec->td_decorr_flag == 0 )
    2279             :         {
    2280           0 :             assert( 0 );
    2281             :         }
    2282             : 
    2283       19983 :         hMdDec->spar_plc_num_lost_frames += 1;
    2284       19983 :         hMdDec->spar_plc_num_lost_frames = min( hMdDec->spar_plc_num_lost_frames, 100 );
    2285             : 
    2286       19983 :         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        6114 :             num_fade_frames = max( hMdDec->spar_plc_num_lost_frames - ivas_spar_dec_plc_num_frames_keep, 0 );
    2294        6114 :             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        6114 :             gain = powf( 10, ( ( (float) gain_dB ) / 20 ) );
    2296             : 
    2297       73368 :             for ( i = 0; i < IVAS_SPAR_MAX_CH; i++ )
    2298             :             {
    2299       67254 :                 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       67254 :                 post_matrix[i] *= gain;
    2301             :             }
    2302             : 
    2303             :             /* apply the post matrix */
    2304       30570 :             for ( int16_t i_ts = 0; i_ts < num_md_sub_frames; i_ts++ )
    2305             :             {
    2306      122280 :                 for ( i = 0; i < num_out_ch; i++ )
    2307             :                 {
    2308      489120 :                     for ( j = 0; j < num_in_ch; j++ )
    2309             :                     {
    2310     4834944 :                         for ( b = 0; b < num_bands_out; b++ )
    2311             :                         {
    2312     4443648 :                             hMdDec->mixer_mat[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] *= post_matrix[i];
    2313             :                         }
    2314             :                     }
    2315             :                 }
    2316             :             }
    2317             :         }
    2318             :     }
    2319             : 
    2320      574437 :     return;
    2321             : }
    2322             : 
    2323             : 
    2324             : /*-----------------------------------------------------------------------------------------*
    2325             :  * Function ivas_spar_unquant_dtx_indicies()
    2326             :  *
    2327             :  * Unquantize SPAR MD DYX indices
    2328             :  *-----------------------------------------------------------------------------------------*/
    2329             : 
    2330        1674 : 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        1674 :     pr_min_max[0] = pSpar_md->min_max[0];
    2343        1674 :     pr_min_max[1] = pSpar_md->min_max[1];
    2344             : 
    2345        5022 :     for ( b = 0; b < nB; b++ )
    2346             :     {
    2347       13392 :         for ( i = 0; i < FOA_CHANNELS - 1; i++ )
    2348             :         {
    2349       10044 :             q_lvl = dtx_pr_real_q_levels[ndm_per_band[bw * b] - 1][i];
    2350       10044 :             idx = pSpar_md->band_coeffs_idx[b].pred_index_re[i];
    2351       10044 :             ivas_deindex_real_index( &idx, q_lvl, pr_min_max[0], pr_min_max[1], &val, 1 );
    2352       10044 :             pSpar_md->band_coeffs[b].pred_re[i] = val;
    2353             :         }
    2354             : 
    2355       11478 :         for ( i = 0; i < FOA_CHANNELS - ndm_per_band[bw * b]; i++ )
    2356             :         {
    2357        8130 :             q_lvl = dtx_pd_real_q_levels[ndm_per_band[bw * b] - 1][i];
    2358        8130 :             idx = pSpar_md->band_coeffs_idx[b].decd_index_re[i];
    2359        8130 :             ivas_deindex_real_index( &idx, q_lvl, dtx_pd_real_min_max[0], dtx_pd_real_min_max[1], &val, 1 );
    2360        8130 :             pSpar_md->band_coeffs[b].P_re[i] = val;
    2361             :         }
    2362             :     }
    2363             : 
    2364        1674 :     return;
    2365             : }
    2366             : 
    2367             : 
    2368             : /*-----------------------------------------------------------------------------------------*
    2369             :  * Function ivas_parse_parameter_bitstream_dtx()
    2370             :  *
    2371             :  * parse DTX bitstream parameters
    2372             :  *-----------------------------------------------------------------------------------------*/
    2373             : 
    2374        1674 : 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        1674 :     sid_bits_len = st0->next_bit_pos;
    2391        1674 :     pr_min_max[0] = pSpar_md->min_max[0];
    2392        1674 :     pr_min_max[1] = pSpar_md->min_max[1];
    2393             : 
    2394        5022 :     for ( i = 0; i < num_bands; i++ )
    2395             :     {
    2396        3348 :         ndec = num_dec_per_band[bw * i];
    2397        3348 :         ndm = num_dmx_per_band[bw * i];
    2398             : 
    2399       13392 :         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       10044 :             pr_idx_1 = pr_pr_idx_pairs[ndm - 1][j][0];
    2405       10044 :             pr_idx_2 = pr_pr_idx_pairs[ndm - 1][j][1];
    2406       10044 :             pd_idx_1 = pr_pd_idx_pairs[ndm - 1][j][0];
    2407       10044 :             pd_idx_2 = pr_pd_idx_pairs[ndm - 1][j][1];
    2408             : 
    2409       10044 :             if ( pr_idx_1 != 0 || pd_idx_1 != 0 || pr_idx_2 != 0 || pd_idx_2 != 0 )
    2410             :             {
    2411       10044 :                 pr_q_lvls = dtx_pr_real_q_levels[ndm - 1][pd_idx_1 - 1];
    2412             : 
    2413       10044 :                 if ( ( j + 1 ) > ndec )
    2414             :                 {
    2415        1914 :                     pd_q_lvls = 1;
    2416             :                 }
    2417             :                 else
    2418             :                 {
    2419        8130 :                     pd_q_lvls = dtx_pd_real_q_levels[ndm - 1][pd_idx_2 - 1];
    2420             :                 }
    2421             : 
    2422       10044 :                 pr_pd_bits = ivas_get_bits_to_encode( pd_q_lvls * pr_q_lvls );
    2423             : 
    2424       10044 :                 value = get_next_indice( st0, pr_pd_bits );
    2425             : 
    2426       10044 :                 pr = (int16_t) floor( value / pd_q_lvls );
    2427       10044 :                 pd = value - pr * pd_q_lvls;
    2428       10044 :                 val = dtx_pd_real_min_max[0];
    2429       10044 :                 ivas_quantise_real_values( &val, pd_q_lvls, dtx_pd_real_min_max[0], dtx_pd_real_min_max[1], &idx, &val, 1 );
    2430       10044 :                 pd = pd + idx;
    2431             : 
    2432       10044 :                 val = pr_min_max[0];
    2433       10044 :                 ivas_quantise_real_values( &val, pr_q_lvls, pr_min_max[0], pr_min_max[1], &idx, &val, 1 );
    2434       10044 :                 pr = pr + idx;
    2435             : 
    2436       10044 :                 if ( ( j + 1 ) <= ndec )
    2437             :                 {
    2438        8130 :                     pSpar_md->band_coeffs_idx[i].decd_index_re[pd_idx_2 - 1] = pd;
    2439             :                 }
    2440             : 
    2441       10044 :                 pSpar_md->band_coeffs_idx[i].pred_index_re[pd_idx_1 - 1] = pr;
    2442             :             }
    2443             :         }
    2444             :     }
    2445             : 
    2446        1674 :     sid_bits_len = st0->next_bit_pos - sid_bits_len;
    2447        1674 :     sba_spar_bitlen = ivas_sba_spar_sid_bitlen( num_dmx_per_band[0] );
    2448        1674 :     zero_pad_bits = sba_spar_bitlen - sid_bits_len;
    2449             : 
    2450        1674 :     assert( zero_pad_bits >= 0 );
    2451        1674 :     if ( num_dmx_per_band[0] == 2 )
    2452             :     {
    2453         957 :         zero_pad_bits -= 1;
    2454             :     }
    2455             : 
    2456        4545 :     for ( j = 0; j < zero_pad_bits; j++ )
    2457             :     {
    2458        2871 :         get_next_indice( st0, 1 );
    2459             :     }
    2460             : 
    2461        1674 :     ivas_spar_unquant_dtx_indicies( pSpar_md, num_bands, bw, num_dmx_per_band );
    2462             : 
    2463        1674 :     return;
    2464             : }
    2465             : 
    2466             : 
    2467             : /*-----------------------------------------------------------------------------------------*
    2468             :  * Function ivas_deindex_real_index()
    2469             :  *
    2470             :  * Deindex real index
    2471             :  *-----------------------------------------------------------------------------------------*/
    2472             : 
    2473    11055450 : 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    11055450 :     if ( q_levels == 0 )
    2485             :     {
    2486           0 :         return IVAS_ERR_INTERNAL;
    2487             :     }
    2488             : 
    2489    11055450 :     if ( q_levels == 1 )
    2490             :     {
    2491     2977944 :         for ( i = 0; i < dim; i++ )
    2492             :         {
    2493     1322136 :             quant[i] = 0;
    2494             :         }
    2495             :     }
    2496             :     else
    2497             :     {
    2498     9399642 :         q_step = ( max_value - min_value ) / ( q_levels - 1 );
    2499    51894708 :         for ( i = 0; i < dim; i++ )
    2500             :         {
    2501    42495066 :             quant[i] = index[i] * q_step;
    2502             :         }
    2503             :     }
    2504             : 
    2505    11055450 :     return IVAS_ERR_OK;
    2506             : }
    2507             : 
    2508             : 
    2509             : /*-----------------------------------------------------------------------------------------*
    2510             :  * Function ivas_spar_to_dirac()
    2511             :  *
    2512             :  *
    2513             :  *-----------------------------------------------------------------------------------------*/
    2514             : 
    2515      415731 : 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      415731 :     active_w = ( dyn_active_w_flag == 1 ) || ( hMdDec->spar_md_cfg.active_w == 1 );
    2549      415731 :     sba_order_internal = min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER );
    2550      415731 :     start_band = 0;
    2551      415731 :     end_band = min( num_bands_out, SPAR_DIRAC_SPLIT_START_BAND ) / bw;
    2552             : 
    2553      415731 :     hDirAC = st_ivas->hDirAC;
    2554      415731 :     hSpatParamRendCom = st_ivas->hSpatParamRendCom;
    2555             : 
    2556      415731 :     dirac_to_spar_md_bands = st_ivas->hSpar->dirac_to_spar_md_bands;
    2557      415731 :     enc_param_start_band = st_ivas->hSpar->enc_param_start_band / bw;
    2558      415731 :     active_w_vlbr = ( st_ivas->hDecoderConfig->ivas_total_brate < IVAS_24k4 ) ? 1 : 0;
    2559             : 
    2560      415731 :     if ( hDirAC != NULL && ivas_get_hodirac_flag( st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->sba_analysis_order ) == 0 )
    2561             :     {
    2562      257946 :         band_grouping = hDirAC->band_grouping;
    2563      257946 :         num_slots_in_subfr = st_ivas->hDirAC->hConfig->dec_param_estim ? CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES : 1;
    2564             : 
    2565     2321514 :         for ( band = start_band; band < end_band; band++ )
    2566             :         {
    2567             :             float PR[3], Pd[3], dvnorm, g_pred;
    2568             : 
    2569     2063568 :             PR[0] = hMdDec->spar_md.band_coeffs[band].pred_re[2];
    2570     2063568 :             PR[1] = hMdDec->spar_md.band_coeffs[band].pred_re[0];
    2571     2063568 :             PR[2] = hMdDec->spar_md.band_coeffs[band].pred_re[1];
    2572     2063568 :             g_pred = PR[0] * PR[0] + PR[1] * PR[1] + PR[2] * PR[2];
    2573     2063568 :             if ( g_pred <= EPSILON )
    2574             :             {
    2575       93279 :                 dvx[band] = 1.0f;
    2576       93279 :                 dvy[band] = 0.0f;
    2577       93279 :                 dvz[band] = 0.0f;
    2578             :             }
    2579             :             else
    2580             :             {
    2581     1970289 :                 g_pred = sqrtf( g_pred );
    2582     1970289 :                 dvnorm = 1.0f / g_pred;
    2583     1970289 :                 dvx[band] = PR[0] * dvnorm;
    2584     1970289 :                 dvy[band] = PR[1] * dvnorm;
    2585     1970289 :                 dvz[band] = PR[2] * dvnorm;
    2586             :             }
    2587             : 
    2588     2063568 :             radius = sqrtf( dvx[band] * dvx[band] + dvy[band] * dvy[band] );
    2589     2063568 :             azi[band] = (int16_t) ( max( -180.0f, min( 180.0f, atan2f( dvy[band], dvx[band] ) / EVS_PI * 180.0f ) ) + 0.5f );
    2590     2063568 :             ele[band] = (int16_t) ( max( -90.0f, min( 180.0f, atan2f( dvz[band], radius ) / EVS_PI * 180.0f ) ) + 0.5f );
    2591             : 
    2592     2063568 :             if ( st_ivas->nchan_transport == 1 )
    2593             :             {
    2594             :                 float w_en_norm, f_scale;
    2595      514008 :                 if ( active_w )
    2596             :                 {
    2597      514008 :                     if ( dtx_vad == 0 )
    2598             :                     {
    2599        2664 :                         f_scale = IVAS_ACTIVEW_DM_F_SCALE_DTX;
    2600             :                     }
    2601             :                     else
    2602             :                     {
    2603      511344 :                         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      514008 :                 w_en_norm = ( 1.0f - ( f_scale * g_pred * g_pred ) );
    2612      514008 :                 w_en_norm *= w_en_norm;
    2613             : 
    2614      514008 :                 Pd[0] = hMdDec->spar_md.band_coeffs[band].P_re[1];
    2615      514008 :                 Pd[1] = hMdDec->spar_md.band_coeffs[band].P_re[0];
    2616      514008 :                 Pd[2] = hMdDec->spar_md.band_coeffs[band].P_re[2];
    2617      514008 :                 en_ratio = PR[0] * PR[0] + PR[1] * PR[1] + PR[2] * PR[2];
    2618      514008 :                 res_pow = w_en_norm + en_ratio + ( Pd[0] * Pd[0] + Pd[1] * Pd[1] + Pd[2] * Pd[2] );
    2619      514008 :                 res_pow *= 0.5f;
    2620      514008 :                 hMdDec->spar_md.en_ratio_slow[band] = 0.75f * hMdDec->spar_md.en_ratio_slow[band] + 0.25f * en_ratio;
    2621      514008 :                 hMdDec->spar_md.ref_pow_slow[band] = 0.75f * hMdDec->spar_md.ref_pow_slow[band] + 0.25f * res_pow;
    2622      514008 :                 en_ratio = sqrtf( hMdDec->spar_md.en_ratio_slow[band] ) / ( hMdDec->spar_md.ref_pow_slow[band] + EPSILON );
    2623             :             }
    2624             :             else
    2625             :             {
    2626     1549560 :                 en_ratio = PR[0] * PR[0] + PR[1] * PR[1] + PR[2] * PR[2];
    2627     1549560 :                 hMdDec->spar_md.en_ratio_slow[band] = 0.75f * hMdDec->spar_md.en_ratio_slow[band] + 0.25f * en_ratio;
    2628     1549560 :                 en_ratio = sqrtf( hMdDec->spar_md.en_ratio_slow[band] );
    2629             :             }
    2630     2063568 :             diffuseness[band] = 1.0f - en_ratio; /*compute diffuseness*/
    2631     2063568 :             diffuseness[band] = ( ( diffuseness[band] < 1.0f ) ? ( ( diffuseness[band] < 0.0f ) ? 0.f : diffuseness[band] ) : 1.0f );
    2632             :         }
    2633             : 
    2634     2321514 :         for ( band = start_band; band < end_band; band++ )
    2635             :         {
    2636             :             int16_t azi_dith, ele_dith;
    2637     2063568 :             tmp_write_idx_param_band = hDirAC->spar_to_dirac_write_idx;
    2638             : 
    2639     2063568 :             en_ratio = 1.0f - diffuseness[band];
    2640     2063568 :             masa_sq( 1.0f - en_ratio, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
    2641             : 
    2642     2063568 :             qmf_band_start = band_grouping[band];
    2643     2063568 :             qmf_band_end = band_grouping[band + 1];
    2644             : 
    2645    10317840 :             for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; block++ )
    2646             :             {
    2647             :                 int16_t ts_start, ts_end, ts;
    2648             : 
    2649     8254272 :                 ts_start = DirAC_block_grouping[block];
    2650     8254272 :                 ts_end = DirAC_block_grouping[block + 1];
    2651    19603896 :                 for ( b = qmf_band_start; b < qmf_band_end; b++ )
    2652             :                 {
    2653             : 
    2654    11349624 :                     azi_dith = azi[band];
    2655    11349624 :                     ele_dith = ele[band];
    2656             : 
    2657    11349624 :                     hSpatParamRendCom->energy_ratio1[block][b] = en_ratio;
    2658    11349624 :                     tmp_write_idx_band = tmp_write_idx_param_band;
    2659             : 
    2660    11349624 :                     if ( hDirAC->hConfig->dec_param_estim == FALSE )
    2661             :                     {
    2662     4903272 :                         hSpatParamRendCom->elevation[tmp_write_idx_band][b] = ele_dith;
    2663     4903272 :                         hSpatParamRendCom->azimuth[tmp_write_idx_band][b] = azi_dith;
    2664     4903272 :                         hSpatParamRendCom->diffuseness_vector[tmp_write_idx_band][b] = diffuseness[band];
    2665             :                     }
    2666             :                     else
    2667             :                     {
    2668    32231760 :                         for ( ts = ts_start; ts < ts_end; ts++ )
    2669             :                         {
    2670    25785408 :                             hSpatParamRendCom->elevation[tmp_write_idx_band][b] = ele_dith;
    2671    25785408 :                             hSpatParamRendCom->azimuth[tmp_write_idx_band][b] = azi_dith;
    2672    25785408 :                             hSpatParamRendCom->diffuseness_vector[tmp_write_idx_band][b] = diffuseness[band];
    2673    25785408 :                             tmp_write_idx_band = ( tmp_write_idx_band + 1 ) % hSpatParamRendCom->dirac_md_buffer_length;
    2674             :                         }
    2675             :                     }
    2676             :                 }
    2677     8254272 :                 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      257946 :         if ( hDirAC->hConfig->dec_param_estim == FALSE )
    2683             :         {
    2684      111438 :             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      146508 :             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      157785 :         band = end_band;
    2694             :     }
    2695             : 
    2696             :     /*read DirAC metadata, convert DirAC to SPAR*/
    2697     2006331 :     for ( ; band < num_bands_out / bw; band++ )
    2698             :     {
    2699             :         int16_t dirac_band_idx;
    2700             : 
    2701     1590600 :         dirac_band_idx = dirac_to_spar_md_bands[band] - enc_param_start_band;
    2702             : 
    2703     1590600 :         num_subframes = MAX_PARAM_SPATIAL_SUBFRAMES;
    2704     1590600 :         if ( st_ivas->hQMetaData->useLowerRes )
    2705             :         {
    2706      134892 :             num_subframes = 1;
    2707             :         }
    2708             : 
    2709     7548324 :         for ( block = 0; block < num_subframes; block++ )
    2710             :         {
    2711     5957724 :             if ( st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].azimuth[block] < 0.f )
    2712             :             {
    2713      891495 :                 st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].azimuth[block] += 360.f;
    2714             :             }
    2715     5957724 :             azi_dirac[band][block] = st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].azimuth[block];
    2716     5957724 :             ele_dirac[band][block] = st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].elevation[block];
    2717             :         }
    2718             : 
    2719     1590600 :         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      415731 :     if ( hMdDec->spar_md_cfg.nchan_transport > 1 )
    2724             :     {
    2725      301221 :         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     1204884 :         for ( pred_idx = 0; pred_idx < FOA_CHANNELS - 1; pred_idx++ )
    2729             :         {
    2730     4518315 :             for ( band = SPAR_DIRAC_SPLIT_START_BAND; band < IVAS_MAX_NUM_BANDS; band++ )
    2731             :             {
    2732     3614652 :                 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      415731 :     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      415731 :     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      415731 :     if ( st_ivas->hQMetaData->useLowerRes && dtx_vad )
    2742             :     {
    2743      169950 :         for ( band = SPAR_DIRAC_SPLIT_START_BAND; band < IVAS_MAX_NUM_BANDS; band++ )
    2744             :         {
    2745      135960 :             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     1976487 :     for ( block = 0; block < num_md_sub_frames; block++ )
    2761             :     {
    2762     7803780 :         for ( band = SPAR_DIRAC_SPLIT_START_BAND; band < IVAS_MAX_NUM_BANDS; band++ )
    2763             :         {
    2764    24972096 :             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    18729072 :                 if ( ivas_is_res_channel( pred_idx + 1, hMdDec->spar_md_cfg.nchan_transport ) )
    2767             :                 {
    2768             :                     /* use 20ms coefficients only for residual channels */
    2769     9267120 :                     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     2006331 :     for ( b = end_band * bw; b < num_bands_out; b++ )
    2776             :     {
    2777     1590600 :         hMdDec->valid_bands[b] = 1;
    2778             :     }
    2779             : 
    2780      415731 :     return;
    2781             : }

Generated by: LCOV version 1.14