LCOV - code coverage report
Current view: top level - lib_com - ivas_spar_com.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 655 706 92.8 %
Date: 2025-05-23 08:37:30 Functions: 22 22 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 "math.h"
      35             : #include "options.h"
      36             : #ifdef DEBUGGING
      37             : #include "debug.h"
      38             : #endif
      39             : #include "ivas_stat_com.h"
      40             : #include "prot.h"
      41             : #include "ivas_prot.h"
      42             : #include "rom_com.h"
      43             : #include "ivas_rom_com.h"
      44             : #include "cnst.h"
      45             : #include <assert.h>
      46             : #include "wmc_auto.h"
      47             : 
      48             : 
      49             : /*------------------------------------------------------------------------------------------*
      50             :  * Local constants
      51             :  *------------------------------------------------------------------------------------------*/
      52             : 
      53             : #define IVAS_FLT_EPS ( 1e-10F )
      54             : #define IVAS_DBL_EPS ( (double) 1e-20 )
      55             : 
      56             : #define IVAS_REMIX_MULT_FAC ( 0.5f )
      57             : 
      58             : #define IVAS_ACTIVEW_DM_F                    ( 1.0f )
      59             : #define IVAS_ACTIVEW_DM_F_DTX                ( 0.25f )
      60             : #define IVAS_ACTIVEW_DM_F_VLBR               ( 0.25f )
      61             : #define IVAS_LIN_ACTIVEW_QUAD_ACTIVEW_THRESH ( 3.0f )
      62             : 
      63             : #define IVAS_P_NORM_SCALING     ( 1.0f )
      64             : #define IVAS_P_NORM_SCALING_DTX ( 0.75f )
      65             : 
      66             : #define IVAS_MAT_DIM_3 ( 3 )
      67             : #define IVAS_MAT_DIM_2 ( 2 )
      68             : #define IVAS_MAT_DIM_1 ( 1 )
      69             : #define MAX_MAT_DIM    ( FOA_CHANNELS - 1 )
      70             : 
      71             : /*------------------------------------------------------------------------------------------*
      72             :  * Static functions declaration
      73             :  *------------------------------------------------------------------------------------------*/
      74             : 
      75             : 
      76             : static void ivas_get_pred_coeffs( float *pppCov_mat_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH], float ppPred_coeffs_re[IVAS_SPAR_MAX_CH - 1][IVAS_MAX_NUM_BANDS], float ppDM_Fv_re[IVAS_SPAR_MAX_CH - 1][IVAS_MAX_NUM_BANDS], const int16_t in_chans, const int16_t start_band, const int16_t end_band, const int16_t active_w, const int16_t active_w_vlbr, const int16_t dtx_vad, const int16_t from_dirac, const int16_t dyn_active_w_flag, const int16_t res_ind );
      77             : 
      78             : static void ivas_reorder_array( float in_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH][IVAS_MAX_NUM_BANDS], const int16_t in_chans, const int16_t order[IVAS_SPAR_MAX_CH], float ***mixer_mat, const int16_t start_band, const int16_t end_band );
      79             : 
      80             : static void ivas_get_Wscaling_factor( float *pppCov_mat_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH], float pred_coeffs_re[IVAS_SPAR_MAX_CH - 1][IVAS_MAX_NUM_BANDS], float ***mixer_mat, const int16_t start_band, const int16_t end_band, const int16_t dtx_vad, const int16_t num_ch, const int16_t *pNum_dmx, const int16_t bands_bw, const int16_t active_w, const int16_t active_w_vlbr, float *pWscale, const int16_t dyn_active_w_flag );
      81             : 
      82             : static void ivas_calc_post_pred_per_band( float *pppCov_mat_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH], float ***mixer_mat, const int16_t num_ch, const int16_t band_idx, float postpred_cov_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH] );
      83             : 
      84             : static int16_t ivas_is_mat_inv( float in_re[MAX_MAT_DIM][MAX_MAT_DIM], const int16_t dim );
      85             : 
      86             : static void ivas_calc_mat_inv( float in_re[MAX_MAT_DIM][MAX_MAT_DIM], const int16_t dim, float out_re[MAX_MAT_DIM][MAX_MAT_DIM] );
      87             : 
      88             : 
      89             : /*-----------------------------------------------------------------------------------------*
      90             :  * Function ivas_get_bw_idx_from_sample_rate()
      91             :  *
      92             :  * Get bwidth index from sample rate.
      93             :  *-----------------------------------------------------------------------------------------*/
      94             : 
      95             : /*! r: audio BW index */
      96      161772 : int16_t ivas_get_bw_idx_from_sample_rate(
      97             :     const int32_t sampling_rate /* i  : sampling rate */
      98             : )
      99             : {
     100      161772 :     int16_t bwidth = 0;
     101             : 
     102      161772 :     switch ( sampling_rate )
     103             :     {
     104       12324 :         case 16000:
     105       12324 :             bwidth = WB;
     106       12324 :             break;
     107       71418 :         case 32000:
     108       71418 :             bwidth = SWB;
     109       71418 :             break;
     110       78030 :         case 48000:
     111       78030 :             bwidth = FB;
     112       78030 :             break;
     113           0 :         default:
     114           0 :             assert( !"Unsupported sample rate!" );
     115             :     }
     116             : 
     117      161772 :     return bwidth;
     118             : }
     119             : 
     120             : 
     121             : /*-------------------------------------------------------------------------
     122             :  * ivas_spar_config()
     123             :  *
     124             :  * SPAR configuration function
     125             :  *------------------------------------------------------------------------*/
     126             : 
     127       13236 : void ivas_spar_config(
     128             :     int32_t ivas_total_brate,    /* i  : codec total bitrate                  */
     129             :     const int16_t sba_order,     /* i  : Ambisonic (SBA) order                */
     130             :     int16_t *nchan_transport,    /* o  : number of transport channels         */
     131             :     int16_t *nSCE,               /* o  : number of SCEs                       */
     132             :     int16_t *nCPE,               /* o  : number of CPEs                       */
     133             :     int32_t *core_nominal_brate, /* o  : core-coding nominal bitrate          */
     134             :     const int16_t sid_format     /* i  : IVAS format indicator from SID frame */
     135             : )
     136             : {
     137       13236 :     if ( ivas_total_brate == IVAS_SID_5k2 )
     138             :     {
     139           0 :         if ( sid_format == SID_SBA_1TC )
     140             :         {
     141           0 :             *nchan_transport = 1;
     142             :         }
     143             :         else
     144             :         {
     145           0 :             *nchan_transport = 2;
     146             :         }
     147             :     }
     148             :     else
     149             :     {
     150       13236 :         *nchan_transport = ivas_get_sba_num_TCs( ivas_total_brate, sba_order );
     151             :     }
     152             : 
     153       13236 :     *nCPE = ( *nchan_transport > 1 ) ? ( *nchan_transport + 1 ) >> 1 : 0;
     154       13236 :     *nSCE = max( 0, *nchan_transport - ( *nCPE << 1 ) );
     155             : 
     156       13236 :     if ( *nchan_transport == 1 )
     157             :     {
     158             :         /* map SPAR SID bitrate to SPAR active bitrate */
     159        4668 :         if ( ivas_total_brate == IVAS_SID_5k2 )
     160             :         {
     161           0 :             ivas_total_brate = IVAS_32k;
     162             :         }
     163        4668 :         assert( ivas_total_brate == IVAS_32k || ivas_total_brate == IVAS_24k4 || ivas_total_brate == IVAS_16k4 || ivas_total_brate == IVAS_13k2 );
     164        4668 :         if ( ivas_total_brate == IVAS_32k )
     165             :         {
     166         992 :             *core_nominal_brate = ACELP_24k40;
     167             :         }
     168        3676 :         else if ( ivas_total_brate == IVAS_24k4 )
     169             :         {
     170         956 :             *core_nominal_brate = ACELP_16k40;
     171             :         }
     172        2720 :         else if ( ivas_total_brate == IVAS_16k4 )
     173             :         {
     174        1420 :             *core_nominal_brate = ACELP_13k20;
     175             :         }
     176        1300 :         else if ( ivas_total_brate == IVAS_13k2 )
     177             :         {
     178        1300 :             *core_nominal_brate = ACELP_9k60;
     179             :         }
     180             :     }
     181             : 
     182       13236 :     return;
     183             : }
     184             : 
     185             : 
     186             : /*-----------------------------------------------------------------------------------------*
     187             :  * Function ivas_get_spar_table_idx()
     188             :  *
     189             :  * Get SPAR table index
     190             :  *-----------------------------------------------------------------------------------------*/
     191             : 
     192             : /*! r: config. table index */
     193     1253450 : int16_t ivas_get_spar_table_idx(
     194             :     const int32_t ivas_total_brate, /* i  : IVAS total bitrate      */
     195             :     const int16_t sba_order,        /* i  : Ambisonic (SBA) order   */
     196             :     const int16_t bwidth,           /* i  : audio bandwidth         */
     197             :     int16_t *bitlen,                /* o  : number of bits          */
     198             :     int16_t *ind                    /* o  : indice                  */
     199             : )
     200             : {
     201     1253450 :     int16_t table_idx = 0, ind1[IVAS_SPAR_BR_TABLE_LEN];
     202     1253450 :     int16_t i, j = 0, ind2 = -1;
     203             : 
     204    26322450 :     for ( i = 0; i < IVAS_SPAR_BR_TABLE_LEN; i++ )
     205             :     {
     206    25069000 :         ind1[j] = 0;
     207    25069000 :         if ( ( ivas_spar_br_table_consts[i].ivas_total_brate == ivas_total_brate ) &&
     208     1966216 :              ( ivas_spar_br_table_consts[i].sba_order == sba_order ) )
     209             :         {
     210     1253450 :             ind1[j++] = i;
     211             :         }
     212             :     }
     213             : 
     214     1253450 :     for ( i = 0; i < j; i++ )
     215             :     {
     216     1253450 :         if ( ivas_spar_br_table_consts[ind1[i]].bwidth == bwidth )
     217             :         {
     218     1253450 :             ind2 = i;
     219     1253450 :             break;
     220             :         }
     221             :     }
     222     1253450 :     assert( j > 0 );     /* to check if bitrate entry is present */
     223     1253450 :     assert( ind2 >= 0 ); /* to check if bw entry is present */
     224             : 
     225     1253450 :     table_idx = ind1[ind2];
     226             : 
     227     1253450 :     if ( ind != NULL )
     228             :     {
     229        9452 :         *ind = ind2;
     230             :     }
     231             : 
     232     1253450 :     if ( bitlen != NULL )
     233             :     {
     234       34102 :         *bitlen = ivas_get_bits_to_encode( j - 1 );
     235             :     }
     236             : 
     237     1253450 :     return table_idx;
     238             : }
     239             : 
     240             : 
     241             : /*-------------------------------------------------------------------*
     242             :  * ivas_get_sba_num_TCs()
     243             :  *
     244             :  * Return number of TCs in SBA format
     245             :  *-------------------------------------------------------------------*/
     246             : 
     247             : /*! r: number of transport channels */
     248     1051350 : int16_t ivas_get_sba_num_TCs(
     249             :     const int32_t ivas_total_brate, /* i  : IVAS total bitrate      */
     250             :     const int16_t sba_order         /* i  : Ambisonic (SBA) order   */
     251             : )
     252             : {
     253             :     int16_t table_idx, nchan_transport;
     254             : 
     255     1051350 :     table_idx = ivas_get_spar_table_idx( ivas_total_brate, sba_order, SPAR_CONFIG_BW, NULL, NULL );
     256             : 
     257     1051350 :     nchan_transport = ivas_spar_br_table_consts[table_idx].nchan_transport;
     258             : 
     259     1051350 :     return nchan_transport;
     260             : }
     261             : 
     262             : 
     263             : /*-----------------------------------------------------------------------------------------*
     264             :  * Function ivas_get_pred_coeffs()
     265             :  *
     266             :  * Calculation of prediction coefficients
     267             :  *-----------------------------------------------------------------------------------------*/
     268             : 
     269     2171037 : static void ivas_get_pred_coeffs(
     270             :     float *cov_real[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH],
     271             :     float ppPred_coeffs_re[IVAS_SPAR_MAX_CH - 1][IVAS_MAX_NUM_BANDS],
     272             :     float ppDM_Fv_re[IVAS_SPAR_MAX_CH - 1][IVAS_MAX_NUM_BANDS],
     273             :     const int16_t in_chans,
     274             :     const int16_t start_band,
     275             :     const int16_t end_band,
     276             :     const int16_t active_w,
     277             :     const int16_t active_w_vlbr,
     278             :     const int16_t dtx_vad,
     279             :     const int16_t from_dirac,
     280             :     const int16_t dyn_active_w_flag,
     281             :     const int16_t res_ind )
     282             : {
     283             :     int16_t i, j, k, b;
     284             :     float abs_value;
     285             :     float w_norm_fac;
     286             :     float div_factor[IVAS_MAX_NUM_BANDS];
     287     2171037 :     int16_t pred_dim = in_chans - 1;
     288             : 
     289     2171037 :     w_norm_fac = ( from_dirac == 1 ) ? 1.0f : 3.0f;
     290             : 
     291     2171037 :     if ( active_w == 0 )
     292             :     {
     293             :         float pPred_temp[IVAS_MAX_NUM_BANDS];
     294             : 
     295     1726663 :         set_zero( pPred_temp, IVAS_MAX_NUM_BANDS );
     296     8992997 :         for ( k = start_band; k < end_band; k++ )
     297             :         {
     298     7266334 :             div_factor[k] = max( 1e-20f, cov_real[0][0][k] );
     299     7266334 :             div_factor[k] = 1 / div_factor[k];
     300             :         }
     301             : 
     302     7008852 :         for ( i = 0; i < pred_dim; i++ )
     303             :         {
     304    28307591 :             for ( k = start_band; k < end_band; k++ )
     305             :             {
     306    23025402 :                 ppPred_coeffs_re[i][k] = cov_real[i + 1][0][k] * div_factor[k];
     307             : 
     308    23025402 :                 IVAS_CALCULATE_SQ_ABS_N( ppPred_coeffs_re[i][k], abs_value );
     309             : 
     310    23025402 :                 pPred_temp[k] += abs_value;
     311             :             }
     312             :         }
     313             : 
     314     8992997 :         for ( k = start_band; k < end_band; k++ )
     315             :         {
     316     7266334 :             pPred_temp[k] = sqrtf( pPred_temp[k] );
     317     7266334 :             div_factor[k] = max( 1.0f, pPred_temp[k] );
     318     7266334 :             div_factor[k] = 1 / div_factor[k];
     319             :         }
     320             : 
     321     7008852 :         for ( i = 0; i < pred_dim; i++ )
     322             :         {
     323    28307591 :             for ( k = start_band; k < end_band; k++ )
     324             :             {
     325    23025402 :                 ppPred_coeffs_re[i][k] = ppPred_coeffs_re[i][k] * div_factor[k];
     326    23025402 :                 ppDM_Fv_re[i][k] = 0;
     327             :             }
     328             :         }
     329             :     }
     330             :     else
     331             :     {
     332             :         float dm_alpha[IVAS_MAX_NUM_BANDS], dm_v_re[IVAS_SPAR_MAX_CH - 1][IVAS_MAX_NUM_BANDS];
     333      444374 :         float real[IVAS_SPAR_MAX_CH - 1], dm_beta_re = 0, dm_g[IVAS_MAX_NUM_BANDS];
     334             :         float dm_f_local, dm_w, dm_y, DM_F[IVAS_MAX_NUM_BANDS];
     335             :         float num_f, den_f, passive_g;
     336             :         float activew_quad_thresh, g_th_sq;
     337             : 
     338      444374 :         if ( dyn_active_w_flag == 1 )
     339             :         {
     340           0 :             activew_quad_thresh = 1.0f;
     341             :         }
     342             :         else
     343             :         {
     344      444374 :             activew_quad_thresh = IVAS_LIN_ACTIVEW_QUAD_ACTIVEW_THRESH;
     345             :         }
     346      444374 :         g_th_sq = activew_quad_thresh * activew_quad_thresh;
     347             : 
     348      444374 :         set_zero( dm_alpha, IVAS_MAX_NUM_BANDS );
     349             : 
     350     1777496 :         for ( i = 1; i < in_chans; i++ )
     351             :         {
     352     6701664 :             for ( k = start_band; k < end_band; k++ )
     353             :             {
     354     5368542 :                 IVAS_CALCULATE_SQ_ABS_N( cov_real[i][0][k], abs_value );
     355     5368542 :                 dm_alpha[k] += abs_value;
     356             :             }
     357             :         }
     358             : 
     359     2233888 :         for ( k = start_band; k < end_band; k++ )
     360             :         {
     361     1789514 :             dm_alpha[k] = sqrtf( dm_alpha[k] );
     362     1789514 :             div_factor[k] = max( dm_alpha[k], 1e-20f );
     363     1789514 :             div_factor[k] = 1 / div_factor[k];
     364             :         }
     365             : 
     366     1777496 :         for ( i = 0; i < pred_dim; i++ )
     367             :         {
     368     6701664 :             for ( k = start_band; k < end_band; k++ )
     369             :             {
     370     5368542 :                 dm_v_re[i][k] = cov_real[i + 1][0][k] * div_factor[k];
     371             :             }
     372             :         }
     373             : 
     374      444374 :         if ( dtx_vad == 0 )
     375             :         {
     376        6956 :             dm_f_local = IVAS_ACTIVEW_DM_F_DTX;
     377             :         }
     378             :         else
     379             :         {
     380      437418 :             dm_f_local = ( active_w_vlbr ) ? IVAS_ACTIVEW_DM_F_VLBR : IVAS_ACTIVEW_DM_F;
     381             :         }
     382             : 
     383     2233888 :         for ( b = start_band; b < end_band; b++ )
     384             :         {
     385     1789514 :             set_zero( real, pred_dim );
     386             : 
     387     7158056 :             for ( j = 0; j < pred_dim; j++ )
     388             :             {
     389    21474168 :                 for ( k = 1; k < in_chans; k++ )
     390             :                 {
     391             :                     float re;
     392             : 
     393    16105626 :                     IVAS_RMULT_FLOAT( cov_real[j + 1][k][b], dm_v_re[k - 1][b], re );
     394    16105626 :                     real[j] += re;
     395             :                 }
     396             :             }
     397     1789514 :             dm_beta_re = 0;
     398     7158056 :             for ( k = 0; k < pred_dim; k++ )
     399             :             {
     400             :                 float re;
     401     5368542 :                 IVAS_RMULT_FLOAT( real[k], dm_v_re[k][b], re );
     402     5368542 :                 dm_beta_re += re;
     403             :             }
     404             : 
     405     1789514 :             dm_w = cov_real[0][0][b];
     406     1789514 :             den_f = max( dm_w, 1e-20f );
     407     1789514 :             passive_g = dm_alpha[b] / den_f;
     408             : 
     409     1789514 :             if ( dyn_active_w_flag == 1 )
     410             :             {
     411           0 :                 dm_alpha[b] = 0.0f;
     412           0 :                 dm_w = 0.0f;
     413           0 :                 for ( i = 0; i < pred_dim; i++ )
     414             :                 {
     415           0 :                     dm_v_re[i][b] = 0.0f;
     416             :                 }
     417           0 :                 dm_v_re[res_ind - 1][b] = 1.0f;
     418           0 :                 passive_g = activew_quad_thresh;
     419             :             }
     420             : 
     421     1789514 :             if ( passive_g < activew_quad_thresh )
     422             :             {
     423             :                 /*linear activeW*/
     424     1789514 :                 dm_y = 0;
     425             : 
     426     7158056 :                 for ( k = 1; k < in_chans; k++ )
     427             :                 {
     428     5368542 :                     dm_y += cov_real[k][k][b];
     429             :                 }
     430     1789514 :                 den_f = max( dm_y, 1e-20f );
     431     1789514 :                 den_f = max( den_f, w_norm_fac * dm_w );
     432     1789514 :                 DM_F[b] = ( dm_f_local * dm_alpha[b] ) / den_f;
     433     1789514 :                 DM_F[b] = min( 1.0f, DM_F[b] );
     434             : 
     435     1789514 :                 den_f = dm_w + ( 2 * dm_alpha[b] * DM_F[b] ) + ( DM_F[b] * DM_F[b] * dm_beta_re );
     436     1789514 :                 den_f = max( den_f, 1e-20f );
     437             : 
     438     1789514 :                 dm_g[b] = ( dm_alpha[b] + ( DM_F[b] * dm_beta_re ) ) / den_f;
     439             :             }
     440             :             else
     441             :             {
     442             :                 float sqrt_val;
     443             : 
     444             :                 /* quadratic activeW */
     445             : 
     446           0 :                 num_f = ( dm_beta_re - ( 2 * dm_alpha[b] * activew_quad_thresh ) );
     447             : 
     448           0 :                 sqrt_val = 4 * dm_alpha[b] * dm_alpha[b] * g_th_sq;
     449           0 :                 sqrt_val += dm_beta_re * dm_beta_re;
     450           0 :                 sqrt_val -= 4 * dm_beta_re * g_th_sq * dm_w;
     451           0 :                 sqrt_val = sqrtf( sqrt_val );
     452           0 :                 num_f += sqrt_val;
     453             : 
     454           0 :                 den_f = 2 * dm_beta_re * g_th_sq;
     455           0 :                 den_f = max( den_f, 1e-20f );
     456           0 :                 dm_g[b] = activew_quad_thresh;
     457           0 :                 DM_F[b] = ( dm_g[b] * num_f ) / den_f;
     458             :             }
     459             :         }
     460             : 
     461     1777496 :         for ( i = 0; i < pred_dim; i++ )
     462             :         {
     463     6701664 :             for ( b = start_band; b < end_band; b++ )
     464             :             {
     465     5368542 :                 ppPred_coeffs_re[i][b] = dm_v_re[i][b] * dm_g[b];
     466             : 
     467     5368542 :                 ppDM_Fv_re[i][b] = dm_v_re[i][b] * DM_F[b];
     468             :             }
     469             :         }
     470             :     }
     471             : 
     472     2171037 :     return;
     473             : }
     474             : 
     475             : 
     476             : /*-----------------------------------------------------------------------------------------*
     477             :  * Function ivas_get_Wscaling_factor()
     478             :  *
     479             :  * Calculation of scaling factor for post predicted W channel
     480             :  *-----------------------------------------------------------------------------------------*/
     481             : 
     482     2171037 : static void ivas_get_Wscaling_factor(
     483             :     float *cov_real[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH],
     484             :     float pred_coeffs_re[IVAS_SPAR_MAX_CH - 1][IVAS_MAX_NUM_BANDS],
     485             :     float ***mixer_mat,
     486             :     const int16_t start_band,
     487             :     const int16_t end_band,
     488             :     const int16_t dtx_vad,
     489             :     const int16_t num_ch,
     490             :     const int16_t *pNum_dmx,
     491             :     const int16_t bands_bw,
     492             :     const int16_t active_w,
     493             :     const int16_t active_w_vlbr,
     494             :     float *pWscale,
     495             :     const int16_t dyn_active_w_flag )
     496             : {
     497             :     int16_t b, ch;
     498             :     float dm_f_local, abs_val;
     499             :     float postpred_cov_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
     500             : 
     501    26052444 :     for ( ch = 0; ch < IVAS_SPAR_MAX_CH; ch++ )
     502             :     {
     503    23881407 :         set_zero( postpred_cov_re[ch], IVAS_SPAR_MAX_CH );
     504             :     }
     505             : 
     506     2171037 :     if ( dtx_vad == 0 )
     507             :     {
     508       15109 :         dm_f_local = IVAS_ACTIVEW_DM_F_SCALE_DTX;
     509             :     }
     510             :     else
     511             :     {
     512     2155928 :         dm_f_local = ( active_w_vlbr ) ? IVAS_ACTIVEW_DM_F_SCALE_VLBR : IVAS_ACTIVEW_DM_F_SCALE;
     513             :     }
     514             : 
     515    11226885 :     for ( b = start_band; b < end_band; b++ )
     516             :     {
     517     9055848 :         pWscale[b] = 1;
     518             : 
     519     9055848 :         if ( ( active_w == 1 ) && ( dyn_active_w_flag == 0 ) )
     520             :         {
     521     1789514 :             float Gw_sq, g_sq = 0;
     522             : 
     523     1789514 :             if ( num_ch != pNum_dmx[b * bands_bw] )
     524             :             {
     525     1789514 :                 ivas_calc_post_pred_per_band( cov_real, mixer_mat, num_ch, b, postpred_cov_re );
     526             :             }
     527             : 
     528     1789514 :             Gw_sq = cov_real[0][0][b] / max( postpred_cov_re[0][0], IVAS_FLT_EPS );
     529             : 
     530     7158056 :             for ( ch = 0; ch < num_ch - 1; ch++ )
     531             :             {
     532     5368542 :                 IVAS_CALCULATE_SQ_ABS_N( pred_coeffs_re[ch][b], abs_val );
     533             : 
     534     5368542 :                 g_sq += abs_val;
     535             :             }
     536             : 
     537     1789514 :             pWscale[b] = sqrtf( Gw_sq );
     538     1789514 :             pWscale[b] = pWscale[b] + sqrtf( Gw_sq + ( 4 * dm_f_local * g_sq ) );
     539     1789514 :             pWscale[b] *= 0.5f;
     540             :         }
     541             :     }
     542             : 
     543     2171037 :     return;
     544             : }
     545             : 
     546             : 
     547             : /*-----------------------------------------------------------------------------------------*
     548             :  * Function ivas_create_fullr_dmx_mat()
     549             :  *
     550             :  * Calculation of downmix matrix
     551             :  *-----------------------------------------------------------------------------------------*/
     552             : 
     553     2331590 : void ivas_create_fullr_dmx_mat(
     554             :     float pred_coeffs_re[IVAS_SPAR_MAX_CH - 1][IVAS_MAX_NUM_BANDS],
     555             :     float dm_fv_re[IVAS_SPAR_MAX_CH - 1][IVAS_MAX_NUM_BANDS],
     556             :     float ***mixer_mat,
     557             :     const int16_t in_chans,
     558             :     const int16_t start_band,
     559             :     const int16_t end_band,
     560             :     const int16_t active_w,
     561             :     ivas_spar_md_com_cfg *hMdCfg )
     562             : {
     563             :     int16_t i, j, k, b;
     564             :     const int16_t *order;
     565             :     float tmp_p1_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH][IVAS_MAX_NUM_BANDS];
     566             :     float tmp_p2_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH][IVAS_MAX_NUM_BANDS];
     567             :     float down_mix_mat1_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH][IVAS_MAX_NUM_BANDS];
     568             :     int16_t remix_unmix_order;
     569             :     int16_t nbands;
     570     2331590 :     nbands = end_band - start_band;
     571     2331590 :     remix_unmix_order = hMdCfg->remix_unmix_order;
     572             : 
     573     2331590 :     order = remix_order_set[remix_unmix_order];
     574             : 
     575    11862350 :     for ( i = 0; i < in_chans; i++ )
     576             :     {
     577    49589120 :         for ( j = 0; j < in_chans; j++ )
     578             :         {
     579    40058360 :             set_zero( &tmp_p1_re[i][j][start_band], nbands );
     580    40058360 :             set_zero( &tmp_p2_re[i][j][start_band], nbands );
     581    40058360 :             set_zero( &down_mix_mat1_re[i][j][start_band], nbands );
     582             :         }
     583             :     }
     584             : 
     585    11862350 :     for ( j = 0; j < in_chans; j++ )
     586             :     {
     587    53396672 :         for ( b = start_band; b < end_band; b++ )
     588             :         {
     589    43865912 :             tmp_p2_re[j][j][b] = 1.0f;
     590             :         }
     591             :     }
     592             : 
     593     9530760 :     for ( j = 1; j < in_chans; j++ )
     594             :     {
     595    40711804 :         for ( b = start_band; b < end_band; b++ )
     596             :         {
     597    33512634 :             tmp_p2_re[j][0][b] = -pred_coeffs_re[j - 1][b];
     598             :         }
     599             :     }
     600             : 
     601     2331590 :     if ( active_w == 1 )
     602             :     {
     603     2447380 :         for ( j = 0; j < in_chans; j++ )
     604             :         {
     605    10299024 :             for ( b = start_band; b < end_band; b++ )
     606             :             {
     607     8341120 :                 tmp_p1_re[j][j][b] = 1.0f;
     608             :             }
     609             :         }
     610             : 
     611     1957904 :         for ( j = 1; j < in_chans; j++ )
     612             :         {
     613     7724268 :             for ( b = start_band; b < end_band; b++ )
     614             :             {
     615     6255840 :                 tmp_p1_re[0][j][b] = dm_fv_re[j - 1][b];
     616             :             }
     617             :         }
     618             : 
     619             :         /* 4x4 mult */
     620     2447380 :         for ( i = 0; i < in_chans; i++ )
     621             :         {
     622     9789520 :             for ( j = 0; j < in_chans; j++ )
     623             :             {
     624    39158080 :                 for ( k = 0; k < in_chans; k++ )
     625             :                 {
     626   164784384 :                     for ( b = start_band; b < end_band; b++ )
     627             :                     {
     628             :                         float tmp_re;
     629   133457920 :                         IVAS_RMULT_FLOAT( tmp_p2_re[i][k][b], tmp_p1_re[k][j][b], tmp_re );
     630   133457920 :                         down_mix_mat1_re[i][j][b] += tmp_re;
     631             :                     }
     632             :                 }
     633             :             }
     634             :         }
     635             :     }
     636             :     else
     637             :     {
     638     9414970 :         for ( j = 0; j < in_chans; j++ )
     639             :         {
     640    39799600 :             for ( k = 0; k < in_chans; k++ )
     641             :             {
     642   197549752 :                 for ( b = start_band; b < end_band; b++ )
     643             :                 {
     644   165323008 :                     down_mix_mat1_re[j][k][b] = tmp_p2_re[j][k][b];
     645             :                 }
     646             :             }
     647             :         }
     648             :     }
     649             : 
     650     2331590 :     if ( remix_unmix_order != 3 )
     651             :     {
     652     2331590 :         ivas_reorder_array( down_mix_mat1_re, in_chans, order, mixer_mat, start_band, end_band );
     653             :     }
     654             : 
     655     2331590 :     return;
     656             : }
     657             : 
     658             : 
     659             : /*-----------------------------------------------------------------------------------------*
     660             :  * Function ivas_reorder_array()
     661             :  *
     662             :  * reorders the input matrix based on order
     663             :  *-----------------------------------------------------------------------------------------*/
     664             : 
     665     2331590 : static void ivas_reorder_array(
     666             :     float in_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH][IVAS_MAX_NUM_BANDS],
     667             :     const int16_t in_chans,
     668             :     const int16_t order[IVAS_SPAR_MAX_CH],
     669             :     float ***mixer_mat,
     670             :     const int16_t start_band,
     671             :     const int16_t end_band )
     672             : {
     673             :     int16_t i, j, b, idx;
     674             : 
     675    11862350 :     for ( i = 0; i < in_chans; i++ )
     676             :     {
     677     9530760 :         idx = order[i];
     678             : 
     679    49589120 :         for ( j = 0; j < in_chans; j++ )
     680             :         {
     681   238745848 :             for ( b = start_band; b < end_band; b++ )
     682             :             {
     683   198687488 :                 mixer_mat[i][j][b] = in_re[idx][j][b];
     684             :             }
     685             :         }
     686             :     }
     687             : 
     688     2331590 :     return;
     689             : }
     690             : 
     691             : 
     692             : /*-----------------------------------------------------------------------------------------*
     693             :  * Function ivas_calc_post_pred_per_band()
     694             :  *
     695             :  * Calculate post pred mat per band
     696             :  *-----------------------------------------------------------------------------------------*/
     697             : 
     698     9620676 : static void ivas_calc_post_pred_per_band(
     699             :     float *cov_real[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH],
     700             :     float ***mixer_mat,
     701             :     const int16_t num_ch,
     702             :     const int16_t band_idx,
     703             :     float postpred_cov_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH] )
     704             : {
     705             :     int16_t i, j, k;
     706             :     float dmx_mat_conj[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
     707             :     float temp_mat[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
     708             :     float tmp_re;
     709             : 
     710    50556180 :     for ( i = 0; i < num_ch; i++ )
     711             :     {
     712   227901360 :         for ( j = 0; j < num_ch; j++ )
     713             :         {
     714   186965856 :             dmx_mat_conj[i][j] = mixer_mat[j][i][band_idx];
     715             :         }
     716             :     }
     717             : 
     718    50556180 :     for ( i = 0; i < num_ch; i++ )
     719             :     {
     720    40935504 :         set_zero( postpred_cov_re[i], num_ch );
     721             :     }
     722             : 
     723             :     /* num_ch x num_ch mult */
     724    50556180 :     for ( i = 0; i < num_ch; i++ )
     725             :     {
     726   227901360 :         for ( j = 0; j < num_ch; j++ )
     727             :         {
     728   186965856 :             temp_mat[i][j] = 0;
     729  1162395840 :             for ( k = 0; k < num_ch; k++ )
     730             :             {
     731   975429984 :                 IVAS_RMULT_FLOAT( cov_real[i][k][band_idx], dmx_mat_conj[k][j], tmp_re );
     732   975429984 :                 temp_mat[i][j] += tmp_re;
     733             :             }
     734             :         }
     735             :     }
     736             : 
     737             :     /* num_ch x num_ch mult */
     738    50556180 :     for ( i = 0; i < num_ch; i++ )
     739             :     {
     740   154886184 :         for ( j = i; j < num_ch; j++ )
     741             :         {
     742   695148600 :             for ( k = 0; k < num_ch; k++ )
     743             :             {
     744   581197920 :                 IVAS_RMULT_FLOAT( mixer_mat[i][k][band_idx], temp_mat[k][j], tmp_re );
     745   581197920 :                 postpred_cov_re[i][j] += tmp_re;
     746             :             }
     747             :         }
     748             :     }
     749             : 
     750    50556180 :     for ( i = 0; i < num_ch; i++ )
     751             :     {
     752   113950680 :         for ( j = 0; j < i; j++ )
     753             :         {
     754    73015176 :             postpred_cov_re[i][j] = postpred_cov_re[j][i];
     755             :         }
     756             :     }
     757             : 
     758     9620676 :     return;
     759             : }
     760             : 
     761             : 
     762             : /*-----------------------------------------------------------------------------------------*
     763             :  * Function ivas_calc_p_coeffs_per_band()
     764             :  *
     765             :  * Calculate P coeffs per band
     766             :  *-----------------------------------------------------------------------------------------*/
     767             : 
     768     6982538 : static void ivas_calc_p_coeffs_per_band(
     769             :     ivas_spar_md_t *pSparMd,
     770             :     const int16_t i_ts,
     771             :     float postpred_cov_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH],
     772             :     const int16_t num_ch,
     773             :     const int16_t dtx_vad,
     774             :     const int16_t num_dmx,
     775             :     const int16_t band_idx )
     776             : {
     777             :     int16_t i, j, k;
     778             :     int16_t m;
     779     6982538 :     float factor = 0;
     780             :     float recon_uu_re[IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS][IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS];
     781     6982538 :     float trace = 0;
     782     6982538 :     float p_norm_scaling = 0;
     783             :     float cov_dd_re[IVAS_SPAR_MAX_CH - 1][IVAS_SPAR_MAX_CH - 1];
     784             :     float cov_uu_re[IVAS_SPAR_MAX_CH - 1][IVAS_SPAR_MAX_CH - 1];
     785             :     int16_t b_ts_idx;
     786             : 
     787     6982538 :     b_ts_idx = band_idx + i_ts * IVAS_MAX_NUM_BANDS;
     788             : 
     789     6982538 :     if ( num_dmx != num_ch )
     790             :     {
     791     6982538 :         set_zero( pSparMd->band_coeffs[b_ts_idx].P_re, IVAS_SPAR_MAX_CH - 1 );
     792    55860304 :         for ( i = 0; i < IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS; i++ )
     793             :         {
     794    48877766 :             set_zero( recon_uu_re[i], IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS );
     795             :         }
     796             : 
     797    21202730 :         for ( i = num_dmx; i < num_ch; i++ )
     798             :         {
     799    50050800 :             for ( j = num_dmx; j < num_ch; j++ )
     800             :             {
     801    35830608 :                 cov_uu_re[i - num_dmx][j - num_dmx] = postpred_cov_re[i][j];
     802             :             }
     803             :         }
     804             : 
     805     6982538 :         if ( dtx_vad == 1 )
     806             :         {
     807    14854754 :             for ( i = 1; i < num_dmx; i++ )
     808             :             {
     809    21900012 :                 for ( j = 1; j < num_dmx; j++ )
     810             :                 {
     811    13975284 :                     cov_dd_re[i - 1][j - 1] = postpred_cov_re[i][j];
     812             :                 }
     813             :             }
     814             : 
     815     6930026 :             if ( num_dmx == 2 )
     816             :             {
     817             :                 float re1, re2;
     818             : 
     819     2667612 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[0][0], cov_dd_re[0][0], re1 );
     820     2667612 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[1][0], cov_dd_re[0][0], re2 );
     821     2667612 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[0][0], re1, recon_uu_re[0][0] );
     822     2667612 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[1][0], re1, recon_uu_re[0][1] );
     823     2667612 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[0][0], re2, recon_uu_re[1][0] );
     824     2667612 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[1][0], re2, recon_uu_re[1][1] );
     825             : 
     826     8002836 :                 for ( i = 0; i < 2; i++ )
     827             :                 {
     828    16005672 :                     for ( j = 0; j < 2; j++ )
     829             :                     {
     830    10670448 :                         cov_uu_re[i][j] -= recon_uu_re[i][j];
     831             :                     }
     832             :                 }
     833             :             }
     834     4262414 :             else if ( num_dmx == 3 )
     835             :             {
     836             :                 float re1[2], re2;
     837     2231838 :                 set_f( re1, 0, 2 );
     838             : 
     839     6695514 :                 for ( j = 0; j < 2; j++ )
     840             :                 {
     841    13391028 :                     for ( k = 0; k < 2; k++ )
     842             :                     {
     843             :                         float re;
     844             : 
     845     8927352 :                         IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[0][k], cov_dd_re[k][j], re );
     846     8927352 :                         re1[j] += re;
     847             :                     }
     848             :                 }
     849             : 
     850     2231838 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[0][0], re1[0], re2 );
     851     2231838 :                 recon_uu_re[0][0] = re2;
     852     2231838 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[0][1], re1[1], re2 );
     853     2231838 :                 recon_uu_re[0][0] += re2;
     854             : 
     855     2231838 :                 cov_uu_re[0][0] -= recon_uu_re[0][0];
     856             :             }
     857     2030576 :             else if ( num_dmx == 4 )
     858             :             {
     859             :                 /* Step 1: Multiply C * cov_dd * C' */
     860             :                 float re1[3], re;
     861             : 
     862     1490880 :                 for ( i = 0; i < num_ch - num_dmx; i++ )
     863             :                 {
     864     1226400 :                     set_f( re1, 0, 3 );
     865     4905600 :                     for ( m = 0; m < num_dmx - 1; m++ )
     866             :                     {
     867    14716800 :                         for ( k = 0; k < num_dmx - 1; k++ )
     868             :                         {
     869    11037600 :                             IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[i][k], cov_dd_re[k][m], re );
     870    11037600 :                             re1[m] += re;
     871             :                         }
     872             :                     }
     873     7932720 :                     for ( j = 0; j < num_ch - num_dmx; j++ )
     874             :                     {
     875    26825280 :                         for ( m = 0; m < num_dmx - 1; m++ )
     876             :                         {
     877    20118960 :                             IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[j][m], re1[m], re );
     878    20118960 :                             recon_uu_re[i][j] += re;
     879             :                         }
     880             :                     }
     881             :                 }
     882             : 
     883             :                 /* Step 2: cov_uu - recon_uu */
     884     1490880 :                 for ( i = 0; i < num_ch - num_dmx; i++ )
     885             :                 {
     886     7932720 :                     for ( j = 0; j < num_ch - num_dmx; j++ )
     887             :                     {
     888     6706320 :                         cov_uu_re[i][j] -= recon_uu_re[i][j];
     889             :                     }
     890             :                 }
     891             :             }
     892             :         }
     893             : 
     894     6982538 :         p_norm_scaling = IVAS_P_NORM_SCALING;
     895             : 
     896     6982538 :         if ( ( dtx_vad == 0 ) && ( num_dmx == 1 ) )
     897             :         {
     898       23418 :             p_norm_scaling = IVAS_P_NORM_SCALING_DTX;
     899             :         }
     900             : 
     901     6982538 :         trace = 0.0f;
     902             : 
     903    21202730 :         for ( i = num_dmx; i < num_ch; i++ )
     904             :         {
     905             :             float tmp_out;
     906    14220192 :             IVAS_CALCULATE_RABS( cov_uu_re[i - num_dmx][i - num_dmx], tmp_out );
     907    14220192 :             trace += tmp_out;
     908             :         }
     909             : 
     910     6982538 :         factor = max( 1e-20f, postpred_cov_re[0][0] );
     911     6982538 :         factor = max( factor, ( p_norm_scaling * trace ) );
     912     6982538 :         factor = 1 / factor;
     913             : 
     914             :         /* normalise Hermitian (except for rounding) cov_uu */
     915    21202730 :         for ( i = num_dmx; i < num_ch; i++ )
     916             :         {
     917    50050800 :             for ( j = num_dmx; j < num_ch; j++ )
     918             :             {
     919    35830608 :                 if ( i == j )
     920             :                 {
     921             :                     /* force diagonal to be real */
     922    14220192 :                     cov_uu_re[i - num_dmx][j - num_dmx] *= factor;
     923             :                 }
     924             :                 else
     925             :                 {
     926             :                     /* set off-diag elements to zero */
     927    21610416 :                     cov_uu_re[i - num_dmx][j - num_dmx] = 0;
     928             :                 }
     929             :             }
     930             :         }
     931             : 
     932             :         /* take sqrt of max of diags and zero */
     933    21202730 :         for ( i = num_dmx; i < num_ch; i++ )
     934             :         {
     935    14220192 :             cov_uu_re[i - num_dmx][i - num_dmx] = sqrtf( max( 0.0f, cov_uu_re[i - num_dmx][i - num_dmx] ) );
     936             :             /* cov_uu_im[i - num_dmx][i - num_dmx] = 0; */
     937             :         }
     938             : 
     939             :         /* save into MD struct */
     940    21202730 :         for ( i = num_dmx; i < num_ch; i++ )
     941             :         {
     942    50050800 :             for ( j = num_dmx; j < num_ch; j++ )
     943             :             {
     944    35830608 :                 if ( i == j )
     945             :                 {
     946    14220192 :                     pSparMd->band_coeffs[b_ts_idx].P_re[j - num_dmx] = cov_uu_re[i - num_dmx][j - num_dmx];
     947             :                 }
     948             :             }
     949             :         }
     950             :     }
     951             : 
     952     6982538 :     return;
     953             : }
     954             : 
     955             : 
     956             : /*-----------------------------------------------------------------------------------------*
     957             :  * Function ivas_calc_c_coeffs_per_band()
     958             :  *
     959             :  * Calculate C coeffs per band
     960             :  *-----------------------------------------------------------------------------------------*/
     961             : 
     962     6041648 : static void ivas_calc_c_coeffs_per_band(
     963             :     ivas_spar_md_t *pSparMd,
     964             :     const int16_t i_ts,
     965             :     float postpred_cov_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH],
     966             :     const int16_t num_ch,
     967             :     const int16_t num_dmx,
     968             :     const int16_t band_idx,
     969             :     const int16_t dtx_vad )
     970             : {
     971             :     int16_t i, j, k;
     972             : 
     973             :     /* worst case for cov_ud is actually 12 x 3 */
     974             :     float cov_ud_re[IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS][IVAS_SPAR_MAX_DMX_CHS - 1];
     975             :     float cov_dd_re[FOA_CHANNELS - 1][FOA_CHANNELS - 1];
     976             :     float cov_dd_re_inv[FOA_CHANNELS - 1][FOA_CHANNELS - 1];
     977     6041648 :     float trace_cov_dd_re = 0;
     978             :     float abs_trace;
     979             :     float re;
     980             :     int16_t b_ts_idx;
     981             : 
     982     6041648 :     b_ts_idx = band_idx + i_ts * IVAS_MAX_NUM_BANDS;
     983             : 
     984     6041648 :     if ( dtx_vad == 0 )
     985             :     {
     986       32462 :         set_zero( &pSparMd->band_coeffs[b_ts_idx].C_re[0][0], ( IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS ) * ( IVAS_SPAR_MAX_DMX_CHS - 1 ) );
     987       32462 :         return;
     988             :     }
     989             : 
     990    16928688 :     for ( i = num_dmx; i < num_ch; i++ )
     991             :     {
     992    29238354 :         for ( j = 1; j < num_dmx; j++ )
     993             :         {
     994    18318852 :             cov_ud_re[i - num_dmx][j - 1] = postpred_cov_re[i][j];
     995             :         }
     996             :     }
     997             : 
     998    15570042 :     for ( i = 1; i < num_dmx; i++ )
     999             :     {
    1000    27282972 :         for ( j = 1; j < num_dmx; j++ )
    1001             :         {
    1002    17722116 :             cov_dd_re[i - 1][j - 1] = postpred_cov_re[i][j];
    1003             :         }
    1004             :     }
    1005             : 
    1006    15570042 :     for ( i = 0; i < num_dmx - 1; i++ )
    1007             :     {
    1008     9560856 :         trace_cov_dd_re += cov_dd_re[i][i];
    1009             :     }
    1010     6009186 :     trace_cov_dd_re *= 0.005f;
    1011             : 
    1012     6009186 :     IVAS_CALCULATE_RABS( trace_cov_dd_re, abs_trace );
    1013             : 
    1014     6009186 :     if ( abs_trace < IVAS_FLT_EPS )
    1015             :     {
    1016             :         /* protection from cases when variance of residual channels is very small */
    1017      290360 :         set_zero( &pSparMd->band_coeffs[b_ts_idx].C_re[0][0], ( IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS ) * ( IVAS_SPAR_MAX_DMX_CHS - 1 ) );
    1018             :     }
    1019             :     else
    1020             :     {
    1021    14846023 :         for ( i = 0; i < num_dmx - 1; i++ )
    1022             :         {
    1023     9127197 :             cov_dd_re[i][i] += trace_cov_dd_re;
    1024             :         }
    1025     5718826 :         if ( ivas_is_mat_inv( cov_dd_re, num_dmx - 1 ) && ( num_dmx < FOA_CHANNELS ) )
    1026             :         {
    1027         705 :             set_zero( &pSparMd->band_coeffs[b_ts_idx].C_re[0][0], ( IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS ) * ( IVAS_SPAR_MAX_DMX_CHS - 1 ) );
    1028             :         }
    1029             :         else
    1030             :         {
    1031     5718121 :             ivas_calc_mat_inv( cov_dd_re, num_dmx - 1, cov_dd_re_inv );
    1032             : 
    1033    16179257 :             for ( i = 0; i < num_ch - num_dmx; i++ )
    1034             :             {
    1035    28150578 :                 for ( j = 0; j < num_dmx - 1; j++ )
    1036             :                 {
    1037    17689442 :                     pSparMd->band_coeffs[b_ts_idx].C_re[i][j] = 0;
    1038    54700616 :                     for ( k = 0; k < num_dmx - 1; k++ )
    1039             :                     {
    1040    37011174 :                         IVAS_RMULT_FLOAT( cov_ud_re[i][k], cov_dd_re_inv[k][j], re );
    1041    37011174 :                         pSparMd->band_coeffs[b_ts_idx].C_re[i][j] += re;
    1042             :                     }
    1043             :                 }
    1044             :             }
    1045             :         }
    1046             :     }
    1047             : 
    1048     6009186 :     return;
    1049             : }
    1050             : 
    1051             : 
    1052             : /*-----------------------------------------------------------------------------------------*
    1053             :  * Function ivas_calc_c_p_coeffs()
    1054             :  *
    1055             :  * Calculation of C and P coeffs
    1056             :  *-----------------------------------------------------------------------------------------*/
    1057             : 
    1058     7831162 : void ivas_calc_c_p_coeffs(
    1059             :     ivas_spar_md_t *pSparMd,
    1060             :     float *cov_real[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH],
    1061             :     const int16_t i_ts,
    1062             :     float ***mixer_mat,
    1063             :     const int16_t num_ch,
    1064             :     const int16_t num_dmx,
    1065             :     const int16_t band_idx,
    1066             :     const int16_t dtx_vad,
    1067             :     const int16_t compute_p_flag,
    1068             :     const int16_t dyn_active_w_flag )
    1069             : {
    1070             :     int16_t i, j;
    1071             :     float postpred_cov_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
    1072             : 
    1073     7831162 :     if ( num_dmx != num_ch )
    1074             :     {
    1075     7831162 :         ivas_calc_post_pred_per_band( cov_real, mixer_mat, num_ch, band_idx, postpred_cov_re );
    1076             : 
    1077     7831162 :         if ( num_dmx != 1 )
    1078             :         {
    1079     6041648 :             ivas_calc_c_coeffs_per_band( pSparMd, i_ts, postpred_cov_re, num_ch, num_dmx, band_idx, dtx_vad );
    1080             :         }
    1081             : 
    1082     7831162 :         if ( dyn_active_w_flag )
    1083             :         {
    1084           0 :             for ( i = 0; i < num_ch - num_dmx; i++ )
    1085             :             {
    1086           0 :                 for ( j = 0; j < num_dmx - 1; j++ )
    1087             :                 {
    1088           0 :                     pSparMd->band_coeffs[band_idx + i_ts * IVAS_MAX_NUM_BANDS].C_re[i][j] = 0.0f;
    1089             :                 }
    1090             :             }
    1091             :         }
    1092     7831162 :         if ( compute_p_flag == 1 )
    1093             :         {
    1094     6982538 :             ivas_calc_p_coeffs_per_band( pSparMd, i_ts, postpred_cov_re, num_ch, dtx_vad, num_dmx, band_idx );
    1095             :         }
    1096             : 
    1097     7831162 :         if ( dyn_active_w_flag )
    1098             :         {
    1099           0 :             for ( i = num_dmx; i < num_ch; i++ )
    1100             :             {
    1101           0 :                 pSparMd->band_coeffs[band_idx + i_ts * IVAS_MAX_NUM_BANDS].P_re[i - num_dmx] = 0;
    1102             :             }
    1103             :         }
    1104             :     }
    1105             : 
    1106     7831162 :     return;
    1107             : }
    1108             : 
    1109             : 
    1110    13304412 : static void ivas_calc_mat_det(
    1111             :     double in_re[MAX_MAT_DIM][MAX_MAT_DIM],
    1112             :     const int16_t dim,
    1113             :     double *det_re )
    1114             : {
    1115    13304412 :     if ( dim == IVAS_MAT_DIM_3 )
    1116             :     {
    1117             :         double re1, re2, re;
    1118     1044480 :         IVAS_RMULT_DOUBLE( in_re[1][1], in_re[2][2], re1 );
    1119     1044480 :         IVAS_RMULT_DOUBLE( in_re[1][2], in_re[2][1], re2 );
    1120     1044480 :         re = re1 - re2;
    1121             : 
    1122     1044480 :         IVAS_RMULT_DOUBLE( in_re[0][0], re, re1 );
    1123             : 
    1124     1044480 :         *det_re = re1;
    1125             : 
    1126     1044480 :         IVAS_RMULT_DOUBLE( in_re[1][0], in_re[2][2], re1 );
    1127     1044480 :         IVAS_RMULT_DOUBLE( in_re[1][2], in_re[2][0], re2 );
    1128     1044480 :         re = re1 - re2;
    1129             : 
    1130     1044480 :         IVAS_RMULT_DOUBLE( in_re[0][1], re, re1 );
    1131             : 
    1132     1044480 :         *det_re -= re1;
    1133             : 
    1134     1044480 :         IVAS_RMULT_DOUBLE( in_re[1][0], in_re[2][1], re1 );
    1135     1044480 :         IVAS_RMULT_DOUBLE( in_re[1][1], in_re[2][0], re2 );
    1136     1044480 :         re = re1 - re2;
    1137             : 
    1138     1044480 :         IVAS_RMULT_DOUBLE( in_re[0][2], re, re1 );
    1139             : 
    1140     1044480 :         *det_re += re1;
    1141             :     }
    1142    12259932 :     else if ( dim == IVAS_MAT_DIM_2 )
    1143             :     {
    1144             :         double re1, re2;
    1145     9427237 :         IVAS_RMULT_DOUBLE( in_re[0][0], in_re[1][1], re1 );
    1146     9427237 :         IVAS_RMULT_DOUBLE( in_re[0][1], in_re[1][0], re2 );
    1147     9427237 :         *det_re = re1 - re2;
    1148             :     }
    1149     2832695 :     else if ( dim == IVAS_MAT_DIM_1 )
    1150             :     {
    1151     2832695 :         *det_re = in_re[0][0];
    1152             :     }
    1153             :     else
    1154             :     {
    1155           0 :         assert( !"matrix dimention not supported!" );
    1156             :     }
    1157             : 
    1158    13304412 :     return;
    1159             : }
    1160             : 
    1161             : 
    1162             : /*-----------------------------------------------------------------------------------------*
    1163             :  * Function ivas_get_mat_cofactor()
    1164             :  *
    1165             :  * Calculate cofactor for invert matrix
    1166             :  *-----------------------------------------------------------------------------------------*/
    1167             : 
    1168     4700160 : static void ivas_get_mat_cofactor(
    1169             :     double in_re[MAX_MAT_DIM][MAX_MAT_DIM],
    1170             :     double out_re[MAX_MAT_DIM][MAX_MAT_DIM],
    1171             :     const int16_t row,
    1172             :     const int16_t col )
    1173             : {
    1174             :     int16_t i, j;
    1175     4700160 :     int16_t r = 0, c = 0;
    1176             : 
    1177    18800640 :     for ( i = 0; i < MAX_MAT_DIM; i++ )
    1178             :     {
    1179    56401920 :         for ( j = 0; j < MAX_MAT_DIM; j++ )
    1180             :         {
    1181    42301440 :             if ( i != row && j != col )
    1182             :             {
    1183             : 
    1184    18800640 :                 out_re[r][c++] = in_re[i][j];
    1185             :             }
    1186             :         }
    1187    14100480 :         if ( c == 2 )
    1188             :         {
    1189     9400320 :             r++;
    1190     9400320 :             c = 0;
    1191             :         }
    1192             :     }
    1193             : 
    1194     4700160 :     return;
    1195             : }
    1196             : 
    1197             : 
    1198             : /*-----------------------------------------------------------------------------------------*
    1199             :  * Function ivas_calc_mat_inv()
    1200             :  *
    1201             :  * Calculate Invert of a matrix
    1202             :  *-----------------------------------------------------------------------------------------*/
    1203             : 
    1204     5718121 : static void ivas_calc_mat_inv(
    1205             :     float in_re[MAX_MAT_DIM][MAX_MAT_DIM],
    1206             :     const int16_t dim,
    1207             :     float out_re[MAX_MAT_DIM][MAX_MAT_DIM] )
    1208             : {
    1209             :     double det;
    1210             :     int16_t i, j;
    1211             :     double dbl_in_re[MAX_MAT_DIM][MAX_MAT_DIM];
    1212             :     double dbl_out_re[MAX_MAT_DIM][MAX_MAT_DIM];
    1213             : 
    1214    14843908 :     for ( i = 0; i < dim; i++ )
    1215             :     {
    1216    26111386 :         for ( j = 0; j < dim; j++ )
    1217             :         {
    1218    16985599 :             dbl_in_re[i][j] = in_re[i][j];
    1219             :         }
    1220             :     }
    1221             : 
    1222     5718121 :     if ( dim == IVAS_MAT_DIM_1 )
    1223             :     {
    1224     2832695 :         det = ( dbl_in_re[0][0] * dbl_in_re[0][0] );
    1225             :         /* assert to catch cases when input is singular matrix*/
    1226     2832695 :         assert( det > 0 );
    1227     2832695 :         det = 1 / det;
    1228     2832695 :         dbl_out_re[0][0] = dbl_in_re[0][0] * det;
    1229             :     }
    1230     2885426 :     else if ( dim == IVAS_MAT_DIM_2 )
    1231             :     {
    1232             :         double det_re;
    1233             :         double re;
    1234             : 
    1235     2363186 :         ivas_calc_mat_det( dbl_in_re, dim, &det_re );
    1236     2363186 :         det = ( det_re * det_re );
    1237             :         /* assert to catch cases when input is singular matrix*/
    1238     2363186 :         assert( det > 0 );
    1239     2363186 :         det = 1 / det;
    1240             : 
    1241     2363186 :         IVAS_RMULT_DOUBLE( det_re, dbl_in_re[1][1], re );
    1242     2363186 :         dbl_out_re[0][0] = re * det;
    1243             : 
    1244     2363186 :         IVAS_RMULT_DOUBLE( det_re, dbl_in_re[0][1], re );
    1245     2363186 :         dbl_out_re[0][1] = -re * det;
    1246             : 
    1247     2363186 :         IVAS_RMULT_DOUBLE( det_re, dbl_in_re[1][0], re );
    1248     2363186 :         dbl_out_re[1][0] = -re * det;
    1249             : 
    1250     2363186 :         IVAS_RMULT_DOUBLE( det_re, dbl_in_re[0][0], re );
    1251     2363186 :         dbl_out_re[1][1] = re * det;
    1252             :     }
    1253      522240 :     else if ( dim == IVAS_MAT_DIM_3 )
    1254             :     {
    1255             :         double fac_re[IVAS_MAT_DIM_3][IVAS_MAT_DIM_3];
    1256             :         double det_re;
    1257      522240 :         int16_t sign = 1;
    1258             : 
    1259      522240 :         ivas_calc_mat_det( dbl_in_re, dim, &det_re );
    1260      522240 :         det = det_re > 0 ? 1 / max( IVAS_DBL_EPS, det_re ) : 1 / min( det_re, -IVAS_DBL_EPS );
    1261             : 
    1262     2088960 :         for ( i = 0; i < dim; i++ )
    1263             :         {
    1264     6266880 :             for ( j = 0; j < dim; j++ )
    1265             :             {
    1266     4700160 :                 ivas_get_mat_cofactor( dbl_in_re, fac_re, i, j );
    1267     4700160 :                 ivas_calc_mat_det( fac_re, IVAS_MAT_DIM_2, &dbl_out_re[j][i] );
    1268     4700160 :                 dbl_out_re[j][i] = dbl_out_re[j][i] * det * sign;
    1269             : 
    1270     4700160 :                 if ( ( ( i + j ) & 1 ) == 0 )
    1271             :                 {
    1272     2611200 :                     sign = -1;
    1273             :                 }
    1274             :                 else
    1275             :                 {
    1276     2088960 :                     sign = 1;
    1277             :                 }
    1278             :             }
    1279             :         }
    1280             :     }
    1281             :     else
    1282             :     {
    1283           0 :         assert( !"matrix dimension not supported!" );
    1284             :     }
    1285             : 
    1286    14843908 :     for ( i = 0; i < dim; i++ )
    1287             :     {
    1288    26111386 :         for ( j = 0; j < dim; j++ )
    1289             :         {
    1290    16985599 :             out_re[i][j] = (float) dbl_out_re[i][j];
    1291             :         }
    1292             :     }
    1293             : 
    1294     5718121 :     return;
    1295             : }
    1296             : 
    1297             : 
    1298             : /*-----------------------------------------------------------------------------------------*
    1299             :  * Function ivas_is_mat_inv()
    1300             :  *
    1301             :  * Check if matrix is invertible or not by checking if determinant is 0 or very close to 0
    1302             :  *-----------------------------------------------------------------------------------------*/
    1303             : 
    1304     5718826 : static int16_t ivas_is_mat_inv(
    1305             :     float in_re[MAX_MAT_DIM][MAX_MAT_DIM],
    1306             :     const int16_t dim )
    1307             : {
    1308     5718826 :     int16_t is_det_zero = 0;
    1309             :     double det, det_re;
    1310             :     int16_t i, j;
    1311             :     double dbl_in_re[MAX_MAT_DIM][MAX_MAT_DIM];
    1312             : 
    1313    14846023 :     for ( i = 0; i < dim; i++ )
    1314             :     {
    1315    26115616 :         for ( j = 0; j < dim; j++ )
    1316             :         {
    1317    16988419 :             dbl_in_re[i][j] = in_re[i][j];
    1318             :         }
    1319             :     }
    1320             : 
    1321     5718826 :     ivas_calc_mat_det( dbl_in_re, dim, &det_re );
    1322             : 
    1323     5718826 :     det = ( det_re * det_re );
    1324             : 
    1325     5718826 :     if ( det < IVAS_DBL_EPS )
    1326             :     {
    1327         705 :         is_det_zero = 1;
    1328             :     }
    1329             : 
    1330     5718826 :     return is_det_zero;
    1331             : }
    1332             : 
    1333             : 
    1334             : /*-----------------------------------------------------------------------------------------*
    1335             :  * Function ivas_compute_spar_params()
    1336             :  *
    1337             :  *
    1338             :  *-----------------------------------------------------------------------------------------*/
    1339             : 
    1340     2171037 : void ivas_compute_spar_params(
    1341             :     float *cov_real[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH],
    1342             :     float dm_fv_re[IVAS_SPAR_MAX_CH - 1][IVAS_MAX_NUM_BANDS],
    1343             :     const int16_t i_ts,
    1344             :     float ***mixer_mat,
    1345             :     const int16_t start_band,
    1346             :     const int16_t end_band,
    1347             :     const int16_t dtx_vad,
    1348             :     const int16_t num_ch,
    1349             :     const int16_t bands_bw,
    1350             :     const int16_t active_w,
    1351             :     const int16_t active_w_vlbr,
    1352             :     ivas_spar_md_com_cfg *hSparCfg,
    1353             :     ivas_spar_md_t *hSparMd,
    1354             :     float *pWscale,
    1355             :     const int16_t from_dirac,
    1356             :     const int16_t dyn_active_w_flag )
    1357             : {
    1358             :     float pred_coeffs_re[IVAS_SPAR_MAX_CH - 1][IVAS_MAX_NUM_BANDS];
    1359             :     int16_t b, i, ndm;
    1360             : 
    1361     2171037 :     ivas_get_pred_coeffs( cov_real, pred_coeffs_re, dm_fv_re, num_ch, start_band, end_band, active_w, active_w_vlbr, dtx_vad, from_dirac, dyn_active_w_flag, hSparMd->res_ind );
    1362             : 
    1363     2171037 :     ivas_create_fullr_dmx_mat( pred_coeffs_re, dm_fv_re, mixer_mat, num_ch, start_band, end_band, active_w, hSparCfg );
    1364             : 
    1365     2171037 :     ivas_get_Wscaling_factor( cov_real, pred_coeffs_re, mixer_mat, start_band, end_band, dtx_vad, num_ch, hSparCfg->num_dmx_chans_per_band, bands_bw, active_w, active_w_vlbr, pWscale, dyn_active_w_flag );
    1366             : 
    1367    11226885 :     for ( b = start_band; b < end_band; b++ )
    1368             :     {
    1369     9055848 :         float onebyscale = 1.0f / pWscale[b];
    1370    37449792 :         for ( i = 0; i < num_ch - 1; i++ )
    1371             :         {
    1372    28393944 :             hSparMd->band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].pred_re[i] = pred_coeffs_re[i][b] * onebyscale;
    1373             :         }
    1374             : 
    1375    46505640 :         for ( i = 0; i < num_ch; i++ )
    1376             :         {
    1377    37449792 :             mixer_mat[0][i][b] *= pWscale[b];
    1378             :         }
    1379             :     }
    1380             : 
    1381    11226885 :     for ( b = start_band; b < end_band; b++ )
    1382             :     {
    1383     9055848 :         ndm = hSparCfg->num_dmx_chans_per_band[b * bands_bw];
    1384             : 
    1385     9055848 :         if ( ndm != num_ch )
    1386             :         {
    1387     6982538 :             ivas_calc_c_p_coeffs( hSparMd, cov_real, i_ts, mixer_mat, num_ch, ndm, b, dtx_vad, 1, dyn_active_w_flag );
    1388             : 
    1389             : #ifdef SPAR_HOA_DBG
    1390             :             /* if (b == 0) */
    1391             :             {
    1392             :                 fprintf( stdout, "\n\nUnquantised C, P coeffs -- band %d:\n", b );
    1393             : 
    1394             :                 for ( int16_t ii = 0; ii < num_ch; ii++ )
    1395             :                 {
    1396             :                     fprintf( stdout, "%f |", hSparMd->band_coeffs[b].pred_re[ii] );
    1397             : 
    1398             :                     if ( ii < num_ch - ndm )
    1399             :                     {
    1400             :                         for ( int16_t jj = 0; jj < ndm - 1; jj++ )
    1401             :                         {
    1402             :                             fprintf( stdout, "%f,\t", hSparMd->band_coeffs[b].C_re[ii][jj] );
    1403             :                         }
    1404             :                         fprintf( stdout, "| %f", hSparMd->band_coeffs[b].P_re[ii] );
    1405             :                     }
    1406             :                     fprintf( stdout, "\n" );
    1407             :                 }
    1408             :             }
    1409             : #endif
    1410             :         }
    1411             :     }
    1412             : 
    1413     2171037 :     return;
    1414             : }
    1415             : 
    1416             : 
    1417             : /*-----------------------------------------------------------------------------------------*
    1418             :  * Function ivas_get_spar_md_from_dirac()
    1419             :  *
    1420             :  *
    1421             :  *-----------------------------------------------------------------------------------------*/
    1422             : 
    1423      866512 : void ivas_get_spar_md_from_dirac(
    1424             :     float azi_dirac[IVAS_MAX_NUM_BANDS][MAX_PARAM_SPATIAL_SUBFRAMES],
    1425             :     float ele_dirac[IVAS_MAX_NUM_BANDS][MAX_PARAM_SPATIAL_SUBFRAMES],
    1426             :     float diffuseness[IVAS_MAX_NUM_BANDS],
    1427             :     const int16_t n_ts,
    1428             :     float ***mixer_mat,
    1429             :     ivas_spar_md_t *hSpar_md,
    1430             :     ivas_spar_md_com_cfg *hSpar_md_cfg,
    1431             :     const int16_t start_band,
    1432             :     const int16_t end_band,
    1433             :     const int16_t order,
    1434             :     const int16_t dtx_vad,
    1435             :     float Wscale_d[IVAS_MAX_NUM_BANDS],
    1436             :     const uint8_t useLowerRes,
    1437             :     const int16_t active_w_vlbr,
    1438             :     const int16_t dyn_active_w_flag )
    1439             : {
    1440             :     int16_t num_ch, band, i, j;
    1441             :     int16_t block, ch;
    1442             :     float response_avg[MAX_OUTPUT_CHANNELS];
    1443             :     float response[MAX_PARAM_SPATIAL_SUBFRAMES][MAX_OUTPUT_CHANNELS];
    1444             :     float cov_real_dirac[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH][IVAS_MAX_NUM_BANDS];
    1445             :     float *pCov_real[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
    1446             :     float dm_fv_re[IVAS_SPAR_MAX_CH - 1][IVAS_MAX_NUM_BANDS];
    1447             :     float Wscale[IVAS_MAX_NUM_BANDS];
    1448             :     float mixer_mat_local[IVAS_MAX_FB_MIXER_OUT_CH][IVAS_MAX_SPAR_FB_MIXER_IN_CH][IVAS_MAX_NUM_BANDS];
    1449             :     float **ppMixer_mat[IVAS_MAX_FB_MIXER_OUT_CH];
    1450             :     float *pMixer_mat[IVAS_MAX_FB_MIXER_OUT_CH][IVAS_MAX_SPAR_FB_MIXER_IN_CH];
    1451             :     float en_ratio_fac, diff_norm_order1, diff_norm_order2, diff_norm_order3;
    1452             :     int16_t active_w;
    1453             : 
    1454             :     int16_t ndm, foa_ch, hoa2_ch;
    1455             :     float P_dir_fact[IVAS_SPAR_MAX_CH - 1];
    1456             :     const int16_t *remix_order;
    1457             : 
    1458      866512 :     remix_order = remix_order_set[hSpar_md_cfg->remix_unmix_order];
    1459             : 
    1460      866512 :     num_ch = ivas_sba_get_nchan_metadata( order, IVAS_256k /*dummy value as order is always 1 in this function*/ );
    1461             : 
    1462      866512 :     hoa2_ch = ivas_sba_get_nchan_metadata( SBA_HOA2_ORDER, IVAS_256k /*dummy value as order is always 1 in this function*/ );
    1463             : 
    1464      866512 :     foa_ch = FOA_CHANNELS;
    1465      866512 :     diff_norm_order1 = 3.0f;
    1466      866512 :     diff_norm_order2 = 5.0f;
    1467      866512 :     diff_norm_order3 = 7.0f;
    1468             : 
    1469    10398144 :     for ( i = 0; i < IVAS_MAX_FB_MIXER_OUT_CH; i++ )
    1470             :     {
    1471   114379584 :         for ( j = 0; j < IVAS_MAX_SPAR_FB_MIXER_IN_CH; j++ )
    1472             :         {
    1473   104847952 :             pMixer_mat[i][j] = mixer_mat_local[i][j];
    1474             :         }
    1475     9531632 :         ppMixer_mat[i] = pMixer_mat[i];
    1476             :     }
    1477             : 
    1478      866512 :     if ( ( start_band >= 6 && hSpar_md_cfg->nchan_transport <= 2 && ( dtx_vad == 1 ) ) || ( useLowerRes && start_band >= 3 && hSpar_md_cfg->nchan_transport <= 2 && ( dtx_vad == 1 ) ) )
    1479             :     {
    1480             :         float P_norm[3];
    1481             :         int16_t idx;
    1482             : 
    1483      422783 :         ndm = hSpar_md_cfg->num_dmx_chans_per_band[start_band - 1];
    1484      422783 :         P_norm[0] = 0.0f;
    1485     1424250 :         for ( i = 0; i < max( 0, foa_ch - ndm ); i++ )
    1486             :         {
    1487     1001467 :             P_norm[0] += hSpar_md->band_coeffs[start_band - 1].P_re[i] * hSpar_md->band_coeffs[start_band - 1].P_re[i];
    1488             :         }
    1489      422783 :         P_norm[0] *= diff_norm_order1 / min( diff_norm_order1, max( 0, foa_ch - ndm ) );
    1490             : 
    1491      422783 :         P_norm[1] = 0.0f;
    1492      422783 :         for ( ; i < max( 0, min( num_ch, hoa2_ch ) - ndm ); i++ )
    1493             :         {
    1494           0 :             P_norm[1] += hSpar_md->band_coeffs[start_band - 1].P_re[i] * hSpar_md->band_coeffs[start_band - 1].P_re[i];
    1495             :         }
    1496      422783 :         P_norm[1] *= diff_norm_order2 / min( diff_norm_order2, max( 0, min( num_ch, hoa2_ch ) - ndm ) );
    1497             : 
    1498      422783 :         P_norm[2] = 0.0f;
    1499      422783 :         for ( ; i < num_ch - ndm; i++ )
    1500             :         {
    1501           0 :             P_norm[2] += hSpar_md->band_coeffs[start_band - 1].P_re[i] * hSpar_md->band_coeffs[start_band - 1].P_re[i];
    1502             :         }
    1503      422783 :         P_norm[2] *= diff_norm_order3 / min( diff_norm_order3, max( 0, num_ch - ndm ) );
    1504             : 
    1505     1424250 :         for ( i = 0; i < max( 0, foa_ch - ndm ); i++ )
    1506             :         {
    1507     1001467 :             idx = remix_order[i + ndm] - ndm;
    1508     1001467 :             P_dir_fact[idx] = hSpar_md->band_coeffs[start_band - 1].P_re[i] * hSpar_md->band_coeffs[start_band - 1].P_re[i];
    1509     1001467 :             P_dir_fact[idx] = P_dir_fact[idx] / max( IVAS_FLT_EPS, P_norm[0] );
    1510             :         }
    1511      422783 :         for ( ; i < max( 0, min( num_ch, hoa2_ch ) - ndm ); i++ )
    1512             :         {
    1513           0 :             idx = remix_order[i + ndm] - ndm;
    1514           0 :             P_dir_fact[idx] = hSpar_md->band_coeffs[start_band - 1].P_re[i] * hSpar_md->band_coeffs[start_band - 1].P_re[i];
    1515           0 :             P_dir_fact[idx] = P_dir_fact[idx] / max( IVAS_FLT_EPS, P_norm[1] );
    1516             :         }
    1517      422783 :         for ( ; i < num_ch - ndm; i++ )
    1518             :         {
    1519           0 :             idx = remix_order[i + ndm] - ndm;
    1520           0 :             P_dir_fact[idx] = hSpar_md->band_coeffs[start_band - 1].P_re[i] * hSpar_md->band_coeffs[start_band - 1].P_re[i];
    1521           0 :             P_dir_fact[idx] = P_dir_fact[idx] / max( IVAS_FLT_EPS, P_norm[2] );
    1522             :         }
    1523             :     }
    1524             : 
    1525     2878049 :     for ( int16_t i_ts = 0; i_ts < n_ts; i_ts++ )
    1526             :     {
    1527     9776831 :         for ( band = start_band; band < end_band; band++ )
    1528             :         {
    1529     7765294 :             ndm = hSpar_md_cfg->num_dmx_chans_per_band[band];
    1530             : 
    1531             :             /*SPAR from DirAC*/
    1532     7765294 :             set_f( response_avg, 0.0f, MAX_OUTPUT_CHANNELS );
    1533             : 
    1534     7765294 :             if ( n_ts > 1 )
    1535             :             {
    1536     5822832 :                 ivas_dirac_dec_get_response( (int16_t) azi_dirac[band][i_ts], (int16_t) ele_dirac[band][i_ts], response_avg, order );
    1537             :             }
    1538     1942462 :             else if ( useLowerRes )
    1539             :             {
    1540      189104 :                 ivas_dirac_dec_get_response( (int16_t) azi_dirac[band][0], (int16_t) ele_dirac[band][0], response_avg, order );
    1541             :             }
    1542             :             else
    1543             :             {
    1544     8766790 :                 for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; block++ )
    1545             :                 {
    1546     7013432 :                     ivas_dirac_dec_get_response( (int16_t) azi_dirac[band][block], (int16_t) ele_dirac[band][block], &( response[block][0] ), order );
    1547             :                 }
    1548             : 
    1549             :                 /* average responses in all subframes*/
    1550             :                 {
    1551             :                     float norm;
    1552             :                     int16_t num_ch_order, hoa2_ch_order;
    1553             : 
    1554     1753358 :                     num_ch_order = ivas_sba_get_nchan( order, 0 );
    1555     1753358 :                     hoa2_ch_order = ivas_sba_get_nchan( SBA_HOA2_ORDER, 0 );
    1556             : 
    1557     8766790 :                     for ( ch = 0; ch < num_ch_order; ch++ )
    1558             :                     {
    1559    35067160 :                         for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; block++ )
    1560             :                         {
    1561    28053728 :                             response_avg[ch] += response[block][ch];
    1562             :                         }
    1563     7013432 :                         response_avg[ch] /= MAX_PARAM_SPATIAL_SUBFRAMES;
    1564             :                     }
    1565             : 
    1566             :                     /*normalize 1st order*/
    1567     1753358 :                     norm = 0.0f;
    1568     7013432 :                     for ( ch = 1; ch < foa_ch; ch++ )
    1569             :                     {
    1570     5260074 :                         norm += response_avg[ch] * response_avg[ch];
    1571             :                     }
    1572     1753358 :                     norm = max( EPSILON, sqrtf( norm ) );
    1573     7013432 :                     for ( ch = 1; ch < foa_ch; ch++ )
    1574             :                     {
    1575     5260074 :                         response_avg[ch] /= norm;
    1576             :                     }
    1577             : 
    1578             :                     /*normalize 2nd order*/
    1579     1753358 :                     norm = 0.0f;
    1580     1753358 :                     for ( ch = foa_ch; ch < min( hoa2_ch_order, num_ch_order ); ch++ )
    1581             :                     {
    1582           0 :                         norm += response_avg[ch] * response_avg[ch];
    1583             :                     }
    1584     1753358 :                     norm = max( EPSILON, sqrtf( norm ) );
    1585     1753358 :                     for ( ch = foa_ch; ch < min( hoa2_ch_order, num_ch_order ); ch++ )
    1586             :                     {
    1587           0 :                         response_avg[ch] /= norm;
    1588             :                     }
    1589             : 
    1590             :                     /*normalize 3rd order*/
    1591     1753358 :                     norm = 0.0f;
    1592     1753358 :                     for ( ch = hoa2_ch_order; ch < num_ch_order; ch++ )
    1593             :                     {
    1594           0 :                         norm += response_avg[ch] * response_avg[ch];
    1595             :                     }
    1596     1753358 :                     norm = max( EPSILON, sqrtf( norm ) );
    1597     1753358 :                     for ( ch = hoa2_ch_order; ch < num_ch_order; ch++ )
    1598             :                     {
    1599           0 :                         response_avg[ch] /= norm;
    1600             :                     }
    1601             :                 }
    1602             :             }
    1603             : 
    1604     7765294 :             for ( i = FOA_CHANNELS + 1; i < num_ch; i++ )
    1605             :             {
    1606           0 :                 response_avg[i] = response_avg[HOA_keep_ind[i]];
    1607             :             }
    1608             : 
    1609     7765294 :             en_ratio_fac = ( 1.0f - diffuseness[band] );
    1610             : 
    1611    38826470 :             for ( i = 0; i < num_ch; i++ )
    1612             :             {
    1613   155305880 :                 for ( j = 0; j < num_ch; j++ )
    1614             :                 {
    1615   124244704 :                     if ( i == j )
    1616             :                     {
    1617    31061176 :                         if ( i == 0 )
    1618             :                         {
    1619     7765294 :                             cov_real_dirac[i][i][band] = 1.0f;
    1620             :                         }
    1621             :                         else
    1622             :                         {
    1623    23295882 :                             cov_real_dirac[i][j][band] = en_ratio_fac * response_avg[i] * response_avg[j];
    1624             : 
    1625    23295882 :                             if ( hSpar_md_cfg->nchan_transport <= 2 )
    1626             :                             {
    1627             : 
    1628    11625294 :                                 cov_real_dirac[i][j][band] *= en_ratio_fac;
    1629    11625294 :                                 if ( ( i >= ndm ) && ( dtx_vad == 1 ) )
    1630             :                                 {
    1631     9140356 :                                     cov_real_dirac[i][j][band] += ( 1.0f - ( en_ratio_fac * en_ratio_fac ) ) * P_dir_fact[i - ndm];
    1632             :                                 }
    1633             :                                 else
    1634             :                                 {
    1635     2484938 :                                     if ( i < foa_ch )
    1636             :                                     {
    1637     2484938 :                                         cov_real_dirac[i][j][band] += ( 1.0f - ( en_ratio_fac * en_ratio_fac ) ) / diff_norm_order1;
    1638             :                                     }
    1639           0 :                                     else if ( i < hoa2_ch )
    1640             :                                     {
    1641           0 :                                         cov_real_dirac[i][j][band] += ( 1.0f - ( en_ratio_fac * en_ratio_fac ) ) / diff_norm_order2;
    1642             :                                     }
    1643             :                                     else
    1644             :                                     {
    1645           0 :                                         cov_real_dirac[i][j][band] += ( 1.0f - ( en_ratio_fac * en_ratio_fac ) ) / diff_norm_order3;
    1646             :                                     }
    1647             :                                 }
    1648             :                             }
    1649             :                             else
    1650             :                             {
    1651    11670588 :                                 if ( i < foa_ch )
    1652             :                                 {
    1653    11670588 :                                     cov_real_dirac[i][j][band] += ( 1.0f - en_ratio_fac ) / diff_norm_order1;
    1654             :                                 }
    1655           0 :                                 else if ( i < hoa2_ch )
    1656             :                                 {
    1657           0 :                                     cov_real_dirac[i][j][band] += ( 1.0f - en_ratio_fac ) / diff_norm_order2;
    1658             :                                 }
    1659             :                                 else
    1660             :                                 {
    1661           0 :                                     cov_real_dirac[i][j][band] += ( 1.0f - en_ratio_fac ) / diff_norm_order3;
    1662             :                                 }
    1663             :                             }
    1664             :                         }
    1665             :                     }
    1666             :                     else
    1667             :                     {
    1668    93183528 :                         cov_real_dirac[i][j][band] = en_ratio_fac * response_avg[i] * response_avg[j];
    1669             :                     }
    1670             :                 }
    1671             :             }
    1672             :         }
    1673             : 
    1674    10057685 :         for ( i = 0; i < num_ch; i++ )
    1675             :         {
    1676    40230740 :             for ( j = 0; j < num_ch; j++ )
    1677             :             {
    1678    32184592 :                 pCov_real[i][j] = cov_real_dirac[i][j];
    1679             :             }
    1680             :         }
    1681             : 
    1682             : #ifdef DEBUG_SBA_MD_DUMP
    1683             :         {
    1684             :             static FILE *fid = 0;
    1685             :             int16_t k = 0;
    1686             :             float tmp_buf[10];
    1687             :             if ( !fid )
    1688             :             {
    1689             :                 fid = fopen( "cov_real_dirac.txt", "wt" );
    1690             :             }
    1691             : 
    1692             :             for ( i = 0; i < num_ch; i++ )
    1693             :             {
    1694             :                 for ( j = 0; j < num_ch; j++ )
    1695             :                 {
    1696             :                     for ( k = start_band; k < end_band; k++ )
    1697             :                     {
    1698             :                         fprintf( fid, "%.6f\n", cov_real_dirac[i][j][k] );
    1699             :                     }
    1700             :                 }
    1701             :             }
    1702             :             fprintf( fid, "\n" );
    1703             :         }
    1704             : #endif
    1705             : 
    1706     2011537 :         active_w = ( dyn_active_w_flag == 1 ) || ( hSpar_md_cfg->active_w == 1 );
    1707             : 
    1708     2011537 :         ivas_compute_spar_params( pCov_real, dm_fv_re, i_ts, ppMixer_mat, start_band, end_band, dtx_vad, num_ch, 1, active_w, active_w_vlbr, hSpar_md_cfg, hSpar_md, Wscale, 1, dyn_active_w_flag );
    1709             : 
    1710     2011537 :         if ( mixer_mat != NULL )
    1711             :         {
    1712      798200 :             for ( band = start_band; band < end_band; band++ )
    1713             :             {
    1714      648640 :                 ndm = hSpar_md_cfg->num_dmx_chans_per_band[band];
    1715             : 
    1716     2250512 :                 for ( i = 0; i < ndm; i++ )
    1717             :                 {
    1718     8009360 :                     for ( j = 0; j < num_ch; j++ )
    1719             :                     {
    1720     6407488 :                         mixer_mat[i][j][band + i_ts * IVAS_MAX_NUM_BANDS] = ppMixer_mat[i][j][band];
    1721             :                     }
    1722             :                 }
    1723             : 
    1724     1641328 :                 for ( i = ndm; i < num_ch; i++ )
    1725             :                 {
    1726     4963440 :                     for ( j = 0; j < num_ch; j++ )
    1727             :                     {
    1728     3970752 :                         mixer_mat[i][j][band + i_ts * IVAS_MAX_NUM_BANDS] = 0.0f;
    1729             :                     }
    1730             :                 }
    1731             : 
    1732      648640 :                 if ( ( ndm == 1 ) && ( Wscale_d != NULL ) )
    1733             :                 {
    1734      885020 :                     for ( j = 0; j < num_ch; j++ )
    1735             :                     {
    1736      708016 :                         mixer_mat[0][j][band + i_ts * IVAS_MAX_NUM_BANDS] *= Wscale_d[band];
    1737             :                     }
    1738             :                 }
    1739             :             }
    1740             :         }
    1741             :     }
    1742             : 
    1743      866512 :     return;
    1744             : }
    1745             : 
    1746             : 
    1747             : /*-------------------------------------------------------------------------
    1748             :  * ivas_dirac_dec_get_response()
    1749             :  *
    1750             :  * calculate reponse, 1 degree resolution
    1751             :  *------------------------------------------------------------------------*/
    1752             : 
    1753   243132460 : void ivas_dirac_dec_get_response(
    1754             :     const int16_t azimuth,
    1755             :     const int16_t elevation,
    1756             :     float *response,
    1757             :     const int16_t ambisonics_order )
    1758             : {
    1759             :     int16_t index_azimuth, index_elevation;
    1760             :     int16_t el, e, az;
    1761             :     float cos_1, cos_2, sin_1, cos_az[3];
    1762             :     float sin_az[3];
    1763             :     float f, c;
    1764             :     int16_t l, m;
    1765             :     int16_t b, b1, b_2, b1_2, a;
    1766             : 
    1767   243132460 :     index_azimuth = ( azimuth + 180 ) % 360;
    1768   243132460 :     index_elevation = elevation + 90;
    1769   243132460 :     e = index_elevation > 90 ? -1 : 1;
    1770   243132460 :     el = index_elevation > 90 ? 180 - index_elevation : index_elevation;
    1771             : 
    1772   243132460 :     az = index_azimuth > 180 ? 360 - index_azimuth : index_azimuth;
    1773   243132460 :     f = index_azimuth > 180 ? -1.0f : 1.0f;
    1774             : 
    1775   243132460 :     cos_1 = dirac_gains_trg_term[az][0];
    1776   243132460 :     cos_2 = cos_1 * cos_1;
    1777   243132460 :     sin_1 = f * dirac_gains_trg_term[az][1];
    1778   243132460 :     cos_az[0] = cos_1;
    1779   243132460 :     cos_az[1] = 2.0f * cos_2 - 1.0f;
    1780   243132460 :     cos_az[2] = 2.0f * cos_1 * cos_az[1] - cos_az[0];
    1781   243132460 :     sin_az[0] = sin_1;
    1782   243132460 :     sin_az[1] = sin_1 * 2.0f * cos_1;
    1783   243132460 :     sin_az[2] = sin_1 * ( 4.0f * cos_2 - 1.0f );
    1784             : 
    1785   243132460 :     response[0] = 1.0f;
    1786             :     /* Un-optimized code - for reference */
    1787             :     /*     for( l = 1; l<= ambisonics_order; l++ ) */
    1788             :     /*     { */
    1789             :     /*         int16_t b, b1, a; */
    1790             :     /*         float c; */
    1791             :     /*         for( m = 0; m < l; m++ ) */
    1792             :     /*         { */
    1793             :     /*             b = l*l+m; */
    1794             :     /*             a = dirac_gains_P_idx[b]; */
    1795             :     /*             c = SQRT2 * dirac_gains_norm_term[a] * dirac_gains_Pnm[el][a]; */
    1796             : 
    1797             :     /*             if( m%2 == 1 ) */
    1798             :     /*             { */
    1799             :     /*                 c = c*e; */
    1800             :     /*             } */
    1801             : 
    1802             :     /*             response[b] = c * sin_az[l-m-1]; */
    1803             : 
    1804             :     /*             b1 = l*l+2*l-m; */
    1805             :     /*             response[b1] = c * cos_az[l-m-1]; */
    1806             : 
    1807             :     /*         } */
    1808             : 
    1809             :     /*         b = l*l+l; */
    1810             :     /*         a = dirac_gains_P_idx[b]; */
    1811             :     /*         c = dirac_gains_norm_term[a] * dirac_gains_Pnm[el][a]; */
    1812             :     /*         if( l%2 == 1) */
    1813             :     /*         { */
    1814             :     /*             c = c*e; */
    1815             :     /*         } */
    1816             : 
    1817             :     /*         response[b] = c; */
    1818             :     /*     } */
    1819             : 
    1820   916141334 :     for ( l = 1; l <= ambisonics_order; l++ )
    1821             :     {
    1822   673008874 :         b_2 = l * l;
    1823   673008874 :         b1_2 = l * l + 2 * l;
    1824  1549082174 :         for ( m = 0; m < l; m += 2 )
    1825             :         {
    1826   876073300 :             b = b_2 + m;
    1827   876073300 :             a = dirac_gains_P_idx[b];
    1828   876073300 :             c = SQRT2 * dirac_gains_norm_term[a] * dirac_gains_Pnm[el][a];
    1829             : 
    1830   876073300 :             response[b] = c * sin_az[l - m - 1];
    1831             : 
    1832   876073300 :             b1 = b1_2 - m;
    1833   876073300 :             response[b1] = c * cos_az[l - m - 1];
    1834             :         }
    1835             : 
    1836  1102885288 :         for ( m = 1; m < l; m += 2 )
    1837             :         {
    1838   429876414 :             b = b_2 + m;
    1839   429876414 :             a = dirac_gains_P_idx[b];
    1840   429876414 :             c = SQRT2 * dirac_gains_norm_term[a] * dirac_gains_Pnm[el][a];
    1841   429876414 :             c = c * e;
    1842             : 
    1843   429876414 :             response[b] = c * sin_az[l - m - 1];
    1844             : 
    1845   429876414 :             b1 = b1_2 - m;
    1846   429876414 :             response[b1] = c * cos_az[l - m - 1];
    1847             :         }
    1848             : 
    1849   673008874 :         b = b_2 + l;
    1850   673008874 :         a = dirac_gains_P_idx[b];
    1851   673008874 :         c = dirac_gains_norm_term[a] * dirac_gains_Pnm[el][a];
    1852   673008874 :         if ( l % 2 == 1 )
    1853             :         {
    1854   446196886 :             c = c * e;
    1855             :         }
    1856             : 
    1857   673008874 :         response[b] = c;
    1858             :     }
    1859             : 
    1860   243132460 :     return;
    1861             : }
    1862             : 
    1863             : 
    1864             : /*-----------------------------------------------------------------------------------------*
    1865             :  * Function ivas_get_bits_to_encode
    1866             :  *
    1867             :  * Get number of bits required to encode the input value
    1868             :  *-----------------------------------------------------------------------------------------*/
    1869             : 
    1870      661812 : int16_t ivas_get_bits_to_encode(
    1871             :     int32_t val )
    1872             : {
    1873      661812 :     int16_t bits_req = 0;
    1874             : 
    1875      661812 :     assert( val >= 0 );
    1876             : 
    1877     1144282 :     while ( val )
    1878             :     {
    1879      482470 :         bits_req++;
    1880      482470 :         val >>= 1;
    1881             :     }
    1882             : 
    1883      661812 :     return bits_req;
    1884             : }
    1885             : 
    1886             : 
    1887             : /*-----------------------------------------------------------------------------------------*
    1888             :  * Function ivas_spar_set_bitrate_config()
    1889             :  *
    1890             :  * Set SPAR bitrate config
    1891             :  *-----------------------------------------------------------------------------------------*/
    1892             : 
    1893        9452 : void ivas_spar_set_bitrate_config(
    1894             :     ivas_spar_md_com_cfg *pSpar_md_cfg, /* i/o: SPAR MD config. handle  */
    1895             :     const int16_t table_idx,            /* i  : config. table index     */
    1896             :     const int16_t num_bands,            /* i  : number of bands         */
    1897             :     const int16_t dirac2spar_md_flag,
    1898             :     const int16_t enc_flag,
    1899             :     const int16_t pca_flag,
    1900             :     const int16_t agc_flag )
    1901             : {
    1902             :     int32_t ivas_total_brate;
    1903             :     int16_t i, total_bits, max_bits, code, length;
    1904             :     int16_t sba_order;
    1905             :     int16_t md_coding_bits_header;
    1906             :     int16_t agc_bits, pca_bits, num_PR_bits_dirac_bands;
    1907             :     int16_t bits_PR, bits_C, bits_P;
    1908             :     int16_t wc_coarse_strat;
    1909             :     int16_t n_input, n_dmx, n_dec;
    1910             :     int16_t quant_strat;
    1911             :     int16_t bands_bw;
    1912             : 
    1913        9452 :     pSpar_md_cfg->nchan_transport = ivas_spar_br_table_consts[table_idx].nchan_transport;
    1914             : 
    1915       30880 :     for ( i = 0; i < pSpar_md_cfg->nchan_transport; i++ )
    1916             :     {
    1917       21428 :         pSpar_md_cfg->max_freq_per_chan[i] = ivas_spar_br_table_consts[table_idx].fpcs;
    1918             :     }
    1919             : 
    1920        9452 :     pSpar_md_cfg->active_w = ivas_spar_br_table_consts[table_idx].active_w;
    1921        9452 :     pSpar_md_cfg->agc_bits_ch_idx = ivas_spar_br_table_consts[table_idx].agc_bits_ch_idx;
    1922        9452 :     ivas_spar_get_uniform_quant_strat( pSpar_md_cfg, table_idx );
    1923             : 
    1924        9452 :     pSpar_md_cfg->quant_strat_bits = ivas_get_bits_to_encode( MAX_QUANT_STRATS );
    1925             : 
    1926             :     /* BLOCK: getEntropyCoderModels */
    1927             : 
    1928        9452 :     pSpar_md_cfg->remix_unmix_order = ivas_spar_br_table_consts[table_idx].dmx_str;
    1929             : 
    1930             :     /* bits per block*/
    1931        9452 :     total_bits = 0;
    1932        9452 :     max_bits = 0;
    1933             : 
    1934        9452 :     ivas_total_brate = ivas_spar_br_table_consts[table_idx].ivas_total_brate;
    1935        9452 :     sba_order = ivas_spar_br_table_consts[table_idx].sba_order;
    1936        9452 :     ivas_get_spar_table_idx( ivas_total_brate, sba_order, ivas_spar_br_table_consts[table_idx].bwidth, &length, &code );
    1937             : 
    1938       30880 :     for ( i = 0; i < pSpar_md_cfg->nchan_transport; i++ )
    1939             :     {
    1940       21428 :         total_bits += (int16_t) ( ivas_spar_br_table_consts[table_idx].core_brs[i][0] / FRAMES_PER_SEC );
    1941       21428 :         max_bits += (int16_t) ( ivas_spar_br_table_consts[table_idx].core_brs[i][1] / FRAMES_PER_SEC );
    1942             :     }
    1943             : 
    1944        9452 :     pSpar_md_cfg->tgt_bits_per_blk = (int16_t) ( ivas_total_brate / FRAMES_PER_SEC ) - IVAS_FORMAT_SIGNALING_NBITS_EXTENDED - SBA_PLANAR_BITS - SBA_ORDER_BITS - length - total_bits;
    1945        9452 :     pSpar_md_cfg->max_bits_per_blk = (int16_t) ( ivas_total_brate / FRAMES_PER_SEC ) - IVAS_FORMAT_SIGNALING_NBITS_EXTENDED - SBA_PLANAR_BITS - SBA_ORDER_BITS - length - max_bits;
    1946             : 
    1947        9452 :     md_coding_bits_header = SPAR_NUM_CODING_STRAT_BITS + pSpar_md_cfg->quant_strat_bits;
    1948             : 
    1949        9452 :     pSpar_md_cfg->tgt_bits_per_blk -= md_coding_bits_header;
    1950        9452 :     pSpar_md_cfg->max_bits_per_blk -= md_coding_bits_header;
    1951             : 
    1952        9452 :     if ( ivas_total_brate < IVAS_24k4 )
    1953             :     {
    1954        1905 :         bands_bw = 2;
    1955             :     }
    1956             :     else
    1957             :     {
    1958        7547 :         bands_bw = 1;
    1959             :     }
    1960             : 
    1961        9452 :     pSpar_md_cfg->tgt_bits_per_blk = (int16_t) ceilf( ( 1.0f * pSpar_md_cfg->tgt_bits_per_blk * num_bands ) / IVAS_MAX_NUM_BANDS );
    1962        9452 :     pSpar_md_cfg->max_bits_per_blk = (int16_t) ceilf( ( 1.0f * pSpar_md_cfg->max_bits_per_blk * num_bands ) / IVAS_MAX_NUM_BANDS );
    1963             : 
    1964        9452 :     pSpar_md_cfg->tgt_bits_per_blk += md_coding_bits_header;
    1965        9452 :     pSpar_md_cfg->max_bits_per_blk += md_coding_bits_header;
    1966             : 
    1967        9452 :     if ( enc_flag )
    1968             :     {
    1969             :         /*calculate the actual worst case bits*/
    1970        3590 :         if ( ivas_total_brate >= BRATE_SPAR_Q_STRAT )
    1971             :         {
    1972         708 :             quant_strat = QUANT_STRAT_0;
    1973             :         }
    1974             :         else
    1975             :         {
    1976        2882 :             quant_strat = QUANT_STRAT_2;
    1977             :         }
    1978             : 
    1979        3590 :         num_PR_bits_dirac_bands = ( dirac2spar_md_flag == 1 ) ? num_bands - SPAR_DIRAC_SPLIT_START_BAND : 0;
    1980        3590 :         num_PR_bits_dirac_bands /= bands_bw;
    1981        3590 :         num_PR_bits_dirac_bands = max( 0, num_PR_bits_dirac_bands );
    1982        3590 :         num_PR_bits_dirac_bands *= DIRAC_TO_SPAR_HBR_PRED_CHS;
    1983             : 
    1984        3590 :         n_input = ivas_sba_get_nchan_metadata( sba_order, ivas_total_brate );
    1985        3590 :         n_dmx = ivas_spar_br_table_consts[table_idx].nchan_transport;
    1986        3590 :         n_dec = n_input - n_dmx;
    1987        3590 :         bits_PR = (int16_t) ceilf( log2f( ivas_spar_br_table_consts[table_idx].q_lvls[quant_strat][0] ) );
    1988        3590 :         num_PR_bits_dirac_bands *= bits_PR;
    1989        3590 :         bits_PR = bits_PR * ( n_input - 1 );
    1990        3590 :         bits_C = (int16_t) ceilf( log2f( ivas_spar_br_table_consts[table_idx].q_lvls[quant_strat][1] ) ) * ( ( n_dmx - 1 ) * n_dec );
    1991        3590 :         bits_P = (int16_t) ceilf( log2f( ivas_spar_br_table_consts[table_idx].q_lvls[quant_strat][2] ) ) * ( n_dec );
    1992        3590 :         wc_coarse_strat = bits_PR + bits_C + bits_P;
    1993        3590 :         wc_coarse_strat *= num_bands;
    1994        3590 :         wc_coarse_strat /= bands_bw;
    1995        3590 :         wc_coarse_strat -= num_PR_bits_dirac_bands;
    1996        3590 :         wc_coarse_strat += md_coding_bits_header;
    1997             : 
    1998        3590 :         if ( pSpar_md_cfg->max_bits_per_blk < wc_coarse_strat )
    1999             :         {
    2000           0 :             assert( 0 );
    2001             :         }
    2002             : 
    2003        3590 :         if ( agc_flag )
    2004             :         {
    2005        1278 :             if ( pSpar_md_cfg->nchan_transport == 1 )
    2006             :             {
    2007        1278 :                 agc_bits = AGC_BITS_PER_CH;
    2008             :             }
    2009             :             else
    2010             :             {
    2011           0 :                 agc_bits = AGC_BITS_PER_CH * pSpar_md_cfg->nchan_transport + AGC_SIGNALLING_BITS;
    2012             :             }
    2013             :         }
    2014             :         else
    2015             :         {
    2016        2312 :             agc_bits = AGC_SIGNALLING_BITS;
    2017             :         }
    2018             : 
    2019        3590 :         if ( ivas_total_brate == PCA_BRATE && sba_order == SBA_FOA_ORDER )
    2020             :         {
    2021          74 :             pca_bits = 1;
    2022          74 :             if ( pca_flag )
    2023             :             {
    2024           8 :                 pca_bits += IVAS_PCA_QBITS + IVAS_PCA_QBITS - 1;
    2025             :             }
    2026             :         }
    2027             :         else
    2028             :         {
    2029        3516 :             pca_bits = 0;
    2030             :         }
    2031             : 
    2032        3590 :         pSpar_md_cfg->max_md_bits_spar = pSpar_md_cfg->max_bits_per_blk + agc_bits + pca_bits;
    2033             :     }
    2034             : 
    2035        9452 :     return;
    2036             : }
    2037             : 
    2038             : 
    2039             : /*-----------------------------------------------------------------------------------------*
    2040             :  * Function ivas_spar_bitrate_dist()
    2041             :  *
    2042             :  * Set SPAR bitrate distribution
    2043             :  *-----------------------------------------------------------------------------------------*/
    2044             : 
    2045       24650 : void ivas_spar_bitrate_dist(
    2046             :     int32_t core_brates_act[],      /* o  : bitrates per core-coder     */
    2047             :     const int16_t nAvailBits,       /* i  : number of available bits    */
    2048             :     const int32_t ivas_total_brate, /* i  : IVAS total bitrate          */
    2049             :     const int16_t sba_order,        /* i  : Ambisonic (SBA) order       */
    2050             :     const int16_t bwidth            /* i  : audio bandwidth             */
    2051             : )
    2052             : {
    2053             :     int16_t i, nchan_transport, table_idx, bitlen;
    2054             :     int16_t core_bits_act[FOA_CHANNELS], core_range_bits[FOA_CHANNELS];
    2055             :     int16_t sum_core_act_bits, residual_bits, overflow_bits;
    2056             : 
    2057       24650 :     table_idx = ivas_get_spar_table_idx( ivas_total_brate, sba_order, bwidth, &bitlen, NULL );
    2058             : 
    2059       24650 :     nchan_transport = ivas_spar_br_table_consts[table_idx].nchan_transport;
    2060             : 
    2061       24650 :     sum_core_act_bits = 0;
    2062      123250 :     for ( i = 0; i < nchan_transport; i++ )
    2063             :     {
    2064       98600 :         core_bits_act[i] = (int16_t) ( ivas_spar_br_table_consts[table_idx].core_brs[i][0] / FRAMES_PER_SEC );
    2065             : 
    2066       98600 :         sum_core_act_bits += core_bits_act[i];
    2067             :     }
    2068             : 
    2069       24650 :     residual_bits = nAvailBits - sum_core_act_bits;
    2070             : 
    2071             :     /* First compute core-coder bits as per bitrate distribution table and MD bitrate*/
    2072       24650 :     if ( residual_bits > 0 )
    2073             :     {
    2074        4099 :         for ( i = 0; i < nchan_transport; i++ )
    2075             :         {
    2076        4099 :             core_range_bits[i] = (int16_t) ( ( ivas_spar_br_table_consts[table_idx].core_brs[i][2] - ivas_spar_br_table_consts[table_idx].core_brs[i][0] ) / FRAMES_PER_SEC );
    2077        4099 :             core_bits_act[i] += min( residual_bits, core_range_bits[i] );
    2078        4099 :             residual_bits -= core_range_bits[i];
    2079             : 
    2080        4099 :             if ( residual_bits <= 0 )
    2081             :             {
    2082        2703 :                 break;
    2083             :             }
    2084             :         }
    2085             :     }
    2086             :     else
    2087             :     {
    2088      109735 :         for ( i = 0; i < nchan_transport; i++ )
    2089             :         {
    2090       87788 :             core_range_bits[i] = (int16_t) ( ( ivas_spar_br_table_consts[table_idx].core_brs[i][0] - ivas_spar_br_table_consts[table_idx].core_brs[i][1] ) / FRAMES_PER_SEC );
    2091             :         }
    2092             : 
    2093       21947 :         overflow_bits = -residual_bits;
    2094             : 
    2095      103902 :         for ( i = 0; i < nchan_transport; i++ )
    2096             :         {
    2097       84276 :             core_bits_act[nchan_transport - 1 - i] -= min( overflow_bits, core_range_bits[nchan_transport - 1 - i] );
    2098       84276 :             overflow_bits -= core_range_bits[nchan_transport - 1 - i];
    2099             : 
    2100       84276 :             if ( overflow_bits <= 0 )
    2101             :             {
    2102        2321 :                 break;
    2103             :             }
    2104             :         }
    2105             : 
    2106       21947 :         if ( overflow_bits > 0 )
    2107             :         {
    2108             :             int16_t overflow_bits_ch;
    2109       19626 :             overflow_bits_ch = overflow_bits / nchan_transport;
    2110             : 
    2111       98130 :             for ( i = 0; i < nchan_transport; i++ )
    2112             :             {
    2113       78504 :                 core_bits_act[i] -= overflow_bits_ch;
    2114       78504 :                 overflow_bits -= overflow_bits_ch;
    2115             :             }
    2116             : 
    2117       19626 :             core_bits_act[nchan_transport - 1] -= max( 0, overflow_bits );
    2118             :         }
    2119             :     }
    2120             : 
    2121      123250 :     for ( i = 0; i < nchan_transport; i++ )
    2122             :     {
    2123       98600 :         core_brates_act[i] = core_bits_act[i] * FRAMES_PER_SEC;
    2124             :     }
    2125             : 
    2126       24650 :     return;
    2127             : }

Generated by: LCOV version 1.14