LCOV - code coverage report
Current view: top level - lib_com - ivas_spar_com.c (source / functions) Hit Total Coverage
Test: Coverage on main -- merged total coverage @ 9b04ec3cb36f5e8dc438cf854fa3e349998fa1e9 Lines: 677 706 95.9 %
Date: 2025-10-31 05:45:46 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     2827054 : int16_t ivas_get_bw_idx_from_sample_rate(
      97             :     const int32_t sampling_rate /* i  : sampling rate */
      98             : )
      99             : {
     100     2827054 :     int16_t bwidth = 0;
     101             : 
     102     2827054 :     switch ( sampling_rate )
     103             :     {
     104      390565 :         case 16000:
     105      390565 :             bwidth = WB;
     106      390565 :             break;
     107     1104722 :         case 32000:
     108     1104722 :             bwidth = SWB;
     109     1104722 :             break;
     110     1331767 :         case 48000:
     111     1331767 :             bwidth = FB;
     112     1331767 :             break;
     113           0 :         default:
     114           0 :             assert( !"Unsupported sample rate!" );
     115             :     }
     116             : 
     117     2827054 :     return bwidth;
     118             : }
     119             : 
     120             : 
     121             : /*-------------------------------------------------------------------------
     122             :  * ivas_spar_config()
     123             :  *
     124             :  * SPAR configuration function
     125             :  *------------------------------------------------------------------------*/
     126             : 
     127      196443 : 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      196443 :     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      196443 :         *nchan_transport = ivas_get_sba_num_TCs( ivas_total_brate, sba_order );
     151             :     }
     152             : 
     153      196443 :     *nCPE = ( *nchan_transport > 1 ) ? ( *nchan_transport + 1 ) >> 1 : 0;
     154      196443 :     *nSCE = max( 0, *nchan_transport - ( *nCPE << 1 ) );
     155             : 
     156      196443 :     if ( *nchan_transport == 1 )
     157             :     {
     158             :         /* map SPAR SID bitrate to SPAR active bitrate */
     159       62558 :         if ( ivas_total_brate == IVAS_SID_5k2 )
     160             :         {
     161           0 :             ivas_total_brate = IVAS_32k;
     162             :         }
     163       62558 :         assert( ivas_total_brate == IVAS_32k || ivas_total_brate == IVAS_24k4 || ivas_total_brate == IVAS_16k4 || ivas_total_brate == IVAS_13k2 );
     164       62558 :         if ( ivas_total_brate == IVAS_32k )
     165             :         {
     166       13067 :             *core_nominal_brate = ACELP_24k40;
     167             :         }
     168       49491 :         else if ( ivas_total_brate == IVAS_24k4 )
     169             :         {
     170       12191 :             *core_nominal_brate = ACELP_16k40;
     171             :         }
     172       37300 :         else if ( ivas_total_brate == IVAS_16k4 )
     173             :         {
     174       16401 :             *core_nominal_brate = ACELP_13k20;
     175             :         }
     176       20899 :         else if ( ivas_total_brate == IVAS_13k2 )
     177             :         {
     178       20899 :             *core_nominal_brate = ACELP_9k60;
     179             :         }
     180             :     }
     181             : 
     182      196443 :     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    23687758 : 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    23687758 :     int16_t table_idx = 0, ind1[IVAS_SPAR_BR_TABLE_LEN];
     202    23687758 :     int16_t i, j = 0, ind2 = -1;
     203             : 
     204   497442918 :     for ( i = 0; i < IVAS_SPAR_BR_TABLE_LEN; i++ )
     205             :     {
     206   473755160 :         ind1[j] = 0;
     207   473755160 :         if ( ( ivas_spar_br_table_consts[i].ivas_total_brate == ivas_total_brate ) &&
     208    36582088 :              ( ivas_spar_br_table_consts[i].sba_order == sba_order ) )
     209             :         {
     210    23687758 :             ind1[j++] = i;
     211             :         }
     212             :     }
     213             : 
     214    23687758 :     for ( i = 0; i < j; i++ )
     215             :     {
     216    23687758 :         if ( ivas_spar_br_table_consts[ind1[i]].bwidth == bwidth )
     217             :         {
     218    23687758 :             ind2 = i;
     219    23687758 :             break;
     220             :         }
     221             :     }
     222    23687758 :     assert( j > 0 );     /* to check if bitrate entry is present */
     223    23687758 :     assert( ind2 >= 0 ); /* to check if bw entry is present */
     224             : 
     225    23687758 :     table_idx = ind1[ind2];
     226             : 
     227    23687758 :     if ( ind != NULL )
     228             :     {
     229      148193 :         *ind = ind2;
     230             :     }
     231             : 
     232    23687758 :     if ( bitlen != NULL )
     233             :     {
     234      619456 :         *bitlen = ivas_get_bits_to_encode( j - 1 );
     235             :     }
     236             : 
     237    23687758 :     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    20203105 : 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    20203105 :     table_idx = ivas_get_spar_table_idx( ivas_total_brate, sba_order, SPAR_CONFIG_BW, NULL, NULL );
     256             : 
     257    20203105 :     nchan_transport = ivas_spar_br_table_consts[table_idx].nchan_transport;
     258             : 
     259    20203105 :     return nchan_transport;
     260             : }
     261             : 
     262             : 
     263             : /*-----------------------------------------------------------------------------------------*
     264             :  * Function ivas_get_pred_coeffs()
     265             :  *
     266             :  * Calculation of prediction coefficients
     267             :  *-----------------------------------------------------------------------------------------*/
     268             : 
     269    42233031 : 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    42233031 :     int16_t pred_dim = in_chans - 1;
     288             : 
     289    42233031 :     w_norm_fac = ( from_dirac == 1 ) ? 1.0f : 3.0f;
     290             : 
     291    42233031 :     if ( active_w == 0 )
     292             :     {
     293             :         float pPred_temp[IVAS_MAX_NUM_BANDS];
     294             : 
     295    32050372 :         set_zero( pPred_temp, IVAS_MAX_NUM_BANDS );
     296   160078112 :         for ( k = start_band; k < end_band; k++ )
     297             :         {
     298   128027740 :             div_factor[k] = max( 1e-20f, cov_real[0][0][k] );
     299   128027740 :             div_factor[k] = 1 / div_factor[k];
     300             :         }
     301             : 
     302   129455327 :         for ( i = 0; i < pred_dim; i++ )
     303             :         {
     304   496534243 :             for ( k = start_band; k < end_band; k++ )
     305             :             {
     306   399129288 :                 ppPred_coeffs_re[i][k] = cov_real[i + 1][0][k] * div_factor[k];
     307             : 
     308   399129288 :                 IVAS_CALCULATE_SQ_ABS_N( ppPred_coeffs_re[i][k], abs_value );
     309             : 
     310   399129288 :                 pPred_temp[k] += abs_value;
     311             :             }
     312             :         }
     313             : 
     314   160078112 :         for ( k = start_band; k < end_band; k++ )
     315             :         {
     316   128027740 :             pPred_temp[k] = sqrtf( pPred_temp[k] );
     317   128027740 :             div_factor[k] = max( 1.0f, pPred_temp[k] );
     318   128027740 :             div_factor[k] = 1 / div_factor[k];
     319             :         }
     320             : 
     321   129455327 :         for ( i = 0; i < pred_dim; i++ )
     322             :         {
     323   496534243 :             for ( k = start_band; k < end_band; k++ )
     324             :             {
     325   399129288 :                 ppPred_coeffs_re[i][k] = ppPred_coeffs_re[i][k] * div_factor[k];
     326   399129288 :                 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    10182659 :         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    10182659 :         if ( dyn_active_w_flag == 1 )
     339             :         {
     340       11298 :             activew_quad_thresh = 1.0f;
     341             :         }
     342             :         else
     343             :         {
     344    10171361 :             activew_quad_thresh = IVAS_LIN_ACTIVEW_QUAD_ACTIVEW_THRESH;
     345             :         }
     346    10182659 :         g_th_sq = activew_quad_thresh * activew_quad_thresh;
     347             : 
     348    10182659 :         set_zero( dm_alpha, IVAS_MAX_NUM_BANDS );
     349             : 
     350    40730636 :         for ( i = 1; i < in_chans; i++ )
     351             :         {
     352   149369889 :             for ( k = start_band; k < end_band; k++ )
     353             :             {
     354   118821912 :                 IVAS_CALCULATE_SQ_ABS_N( cov_real[i][0][k], abs_value );
     355   118821912 :                 dm_alpha[k] += abs_value;
     356             :             }
     357             :         }
     358             : 
     359    49789963 :         for ( k = start_band; k < end_band; k++ )
     360             :         {
     361    39607304 :             dm_alpha[k] = sqrtf( dm_alpha[k] );
     362    39607304 :             div_factor[k] = max( dm_alpha[k], 1e-20f );
     363    39607304 :             div_factor[k] = 1 / div_factor[k];
     364             :         }
     365             : 
     366    40730636 :         for ( i = 0; i < pred_dim; i++ )
     367             :         {
     368   149369889 :             for ( k = start_band; k < end_band; k++ )
     369             :             {
     370   118821912 :                 dm_v_re[i][k] = cov_real[i + 1][0][k] * div_factor[k];
     371             :             }
     372             :         }
     373             : 
     374    10182659 :         if ( dtx_vad == 0 )
     375             :         {
     376       48425 :             dm_f_local = IVAS_ACTIVEW_DM_F_DTX;
     377             :         }
     378             :         else
     379             :         {
     380    10134234 :             dm_f_local = ( active_w_vlbr ) ? IVAS_ACTIVEW_DM_F_VLBR : IVAS_ACTIVEW_DM_F;
     381             :         }
     382             : 
     383    49789963 :         for ( b = start_band; b < end_band; b++ )
     384             :         {
     385    39607304 :             set_zero( real, pred_dim );
     386             : 
     387   158429216 :             for ( j = 0; j < pred_dim; j++ )
     388             :             {
     389   475287648 :                 for ( k = 1; k < in_chans; k++ )
     390             :                 {
     391             :                     float re;
     392             : 
     393   356465736 :                     IVAS_RMULT_FLOAT( cov_real[j + 1][k][b], dm_v_re[k - 1][b], re );
     394   356465736 :                     real[j] += re;
     395             :                 }
     396             :             }
     397    39607304 :             dm_beta_re = 0;
     398   158429216 :             for ( k = 0; k < pred_dim; k++ )
     399             :             {
     400             :                 float re;
     401   118821912 :                 IVAS_RMULT_FLOAT( real[k], dm_v_re[k][b], re );
     402   118821912 :                 dm_beta_re += re;
     403             :             }
     404             : 
     405    39607304 :             dm_w = cov_real[0][0][b];
     406    39607304 :             den_f = max( dm_w, 1e-20f );
     407    39607304 :             passive_g = dm_alpha[b] / den_f;
     408             : 
     409    39607304 :             if ( dyn_active_w_flag == 1 )
     410             :             {
     411       51648 :                 dm_alpha[b] = 0.0f;
     412       51648 :                 dm_w = 0.0f;
     413      206592 :                 for ( i = 0; i < pred_dim; i++ )
     414             :                 {
     415      154944 :                     dm_v_re[i][b] = 0.0f;
     416             :                 }
     417       51648 :                 dm_v_re[res_ind - 1][b] = 1.0f;
     418       51648 :                 passive_g = activew_quad_thresh;
     419             :             }
     420             : 
     421    39607304 :             if ( passive_g < activew_quad_thresh )
     422             :             {
     423             :                 /*linear activeW*/
     424    39555272 :                 dm_y = 0;
     425             : 
     426   158221088 :                 for ( k = 1; k < in_chans; k++ )
     427             :                 {
     428   118665816 :                     dm_y += cov_real[k][k][b];
     429             :                 }
     430    39555272 :                 den_f = max( dm_y, 1e-20f );
     431    39555272 :                 den_f = max( den_f, w_norm_fac * dm_w );
     432    39555272 :                 DM_F[b] = ( dm_f_local * dm_alpha[b] ) / den_f;
     433    39555272 :                 DM_F[b] = min( 1.0f, DM_F[b] );
     434             : 
     435    39555272 :                 den_f = dm_w + ( 2 * dm_alpha[b] * DM_F[b] ) + ( DM_F[b] * DM_F[b] * dm_beta_re );
     436    39555272 :                 den_f = max( den_f, 1e-20f );
     437             : 
     438    39555272 :                 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       52032 :                 num_f = ( dm_beta_re - ( 2 * dm_alpha[b] * activew_quad_thresh ) );
     447             : 
     448       52032 :                 sqrt_val = 4 * dm_alpha[b] * dm_alpha[b] * g_th_sq;
     449       52032 :                 sqrt_val += dm_beta_re * dm_beta_re;
     450       52032 :                 sqrt_val -= 4 * dm_beta_re * g_th_sq * dm_w;
     451       52032 :                 sqrt_val = sqrtf( sqrt_val );
     452       52032 :                 num_f += sqrt_val;
     453             : 
     454       52032 :                 den_f = 2 * dm_beta_re * g_th_sq;
     455       52032 :                 den_f = max( den_f, 1e-20f );
     456       52032 :                 dm_g[b] = activew_quad_thresh;
     457       52032 :                 DM_F[b] = ( dm_g[b] * num_f ) / den_f;
     458             :             }
     459             :         }
     460             : 
     461    40730636 :         for ( i = 0; i < pred_dim; i++ )
     462             :         {
     463   149369889 :             for ( b = start_band; b < end_band; b++ )
     464             :             {
     465   118821912 :                 ppPred_coeffs_re[i][b] = dm_v_re[i][b] * dm_g[b];
     466             : 
     467   118821912 :                 ppDM_Fv_re[i][b] = dm_v_re[i][b] * DM_F[b];
     468             :             }
     469             :         }
     470             :     }
     471             : 
     472    42233031 :     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    42233031 : 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   506796372 :     for ( ch = 0; ch < IVAS_SPAR_MAX_CH; ch++ )
     502             :     {
     503   464563341 :         set_zero( postpred_cov_re[ch], IVAS_SPAR_MAX_CH );
     504             :     }
     505             : 
     506    42233031 :     if ( dtx_vad == 0 )
     507             :     {
     508       97384 :         dm_f_local = IVAS_ACTIVEW_DM_F_SCALE_DTX;
     509             :     }
     510             :     else
     511             :     {
     512    42135647 :         dm_f_local = ( active_w_vlbr ) ? IVAS_ACTIVEW_DM_F_SCALE_VLBR : IVAS_ACTIVEW_DM_F_SCALE;
     513             :     }
     514             : 
     515   209868075 :     for ( b = start_band; b < end_band; b++ )
     516             :     {
     517   167635044 :         pWscale[b] = 1;
     518             : 
     519   167635044 :         if ( ( active_w == 1 ) && ( dyn_active_w_flag == 0 ) )
     520             :         {
     521    39555656 :             float Gw_sq, g_sq = 0;
     522             : 
     523    39555656 :             if ( num_ch != pNum_dmx[b * bands_bw] )
     524             :             {
     525    39555656 :                 ivas_calc_post_pred_per_band( cov_real, mixer_mat, num_ch, b, postpred_cov_re );
     526             :             }
     527             : 
     528    39555656 :             Gw_sq = cov_real[0][0][b] / max( postpred_cov_re[0][0], IVAS_FLT_EPS );
     529             : 
     530   158222624 :             for ( ch = 0; ch < num_ch - 1; ch++ )
     531             :             {
     532   118666968 :                 IVAS_CALCULATE_SQ_ABS_N( pred_coeffs_re[ch][b], abs_val );
     533             : 
     534   118666968 :                 g_sq += abs_val;
     535             :             }
     536             : 
     537    39555656 :             pWscale[b] = sqrtf( Gw_sq );
     538    39555656 :             pWscale[b] = pWscale[b] + sqrtf( Gw_sq + ( 4 * dm_f_local * g_sq ) );
     539    39555656 :             pWscale[b] *= 0.5f;
     540             :         }
     541             :     }
     542             : 
     543    42233031 :     return;
     544             : }
     545             : 
     546             : 
     547             : /*-----------------------------------------------------------------------------------------*
     548             :  * Function ivas_create_fullr_dmx_mat()
     549             :  *
     550             :  * Calculation of downmix matrix
     551             :  *-----------------------------------------------------------------------------------------*/
     552             : 
     553    44974242 : 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    44974242 :     nbands = end_band - start_band;
     571    44974242 :     remix_unmix_order = hMdCfg->remix_unmix_order;
     572             : 
     573    44974242 :     order = remix_order_set[remix_unmix_order];
     574             : 
     575   227378888 :     for ( i = 0; i < in_chans; i++ )
     576             :     {
     577   936033296 :         for ( j = 0; j < in_chans; j++ )
     578             :         {
     579   753628650 :             set_zero( &tmp_p1_re[i][j][start_band], nbands );
     580   753628650 :             set_zero( &tmp_p2_re[i][j][start_band], nbands );
     581   753628650 :             set_zero( &down_mix_mat1_re[i][j][start_band], nbands );
     582             :         }
     583             :     }
     584             : 
     585   227378888 :     for ( j = 0; j < in_chans; j++ )
     586             :     {
     587   971426766 :         for ( b = start_band; b < end_band; b++ )
     588             :         {
     589   789022120 :             tmp_p2_re[j][j][b] = 1.0f;
     590             :         }
     591             :     }
     592             : 
     593   182404646 :     for ( j = 1; j < in_chans; j++ )
     594             :     {
     595   736720028 :         for ( b = start_band; b < end_band; b++ )
     596             :         {
     597   599289624 :             tmp_p2_re[j][0][b] = -pred_coeffs_re[j - 1][b];
     598             :         }
     599             :     }
     600             : 
     601    44974242 :     if ( active_w == 1 )
     602             :     {
     603    55307025 :         for ( j = 0; j < in_chans; j++ )
     604             :         {
     605   227411356 :             for ( b = start_band; b < end_band; b++ )
     606             :             {
     607   183165736 :                 tmp_p1_re[j][j][b] = 1.0f;
     608             :             }
     609             :         }
     610             : 
     611    44245620 :         for ( j = 1; j < in_chans; j++ )
     612             :         {
     613   170558517 :             for ( b = start_band; b < end_band; b++ )
     614             :             {
     615   137374302 :                 tmp_p1_re[0][j][b] = dm_fv_re[j - 1][b];
     616             :             }
     617             :         }
     618             : 
     619             :         /* 4x4 mult */
     620    55307025 :         for ( i = 0; i < in_chans; i++ )
     621             :         {
     622   221228100 :             for ( j = 0; j < in_chans; j++ )
     623             :             {
     624   884912400 :                 for ( k = 0; k < in_chans; k++ )
     625             :                 {
     626  3638581696 :                     for ( b = start_band; b < end_band; b++ )
     627             :                     {
     628             :                         float tmp_re;
     629  2930651776 :                         IVAS_RMULT_FLOAT( tmp_p2_re[i][k][b], tmp_p1_re[k][j][b], tmp_re );
     630  2930651776 :                         down_mix_mat1_re[i][j][b] += tmp_re;
     631             :                     }
     632             :                 }
     633             :             }
     634             :         }
     635             :     }
     636             :     else
     637             :     {
     638   172071863 :         for ( j = 0; j < in_chans; j++ )
     639             :         {
     640   714805196 :             for ( k = 0; k < in_chans; k++ )
     641             :             {
     642  3288192498 :                 for ( b = start_band; b < end_band; b++ )
     643             :                 {
     644  2711546328 :                     down_mix_mat1_re[j][k][b] = tmp_p2_re[j][k][b];
     645             :                 }
     646             :             }
     647             :         }
     648             :     }
     649             : 
     650    44974242 :     if ( remix_unmix_order != 3 )
     651             :     {
     652    44974242 :         ivas_reorder_array( down_mix_mat1_re, in_chans, order, mixer_mat, start_band, end_band );
     653             :     }
     654             : 
     655    44974242 :     return;
     656             : }
     657             : 
     658             : 
     659             : /*-----------------------------------------------------------------------------------------*
     660             :  * Function ivas_reorder_array()
     661             :  *
     662             :  * reorders the input matrix based on order
     663             :  *-----------------------------------------------------------------------------------------*/
     664             : 
     665    44974242 : 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   227378888 :     for ( i = 0; i < in_chans; i++ )
     676             :     {
     677   182404646 :         idx = order[i];
     678             : 
     679   936033296 :         for ( j = 0; j < in_chans; j++ )
     680             :         {
     681  4197837922 :             for ( b = start_band; b < end_band; b++ )
     682             :             {
     683  3444209272 :                 mixer_mat[i][j][b] = in_re[idx][j][b];
     684             :             }
     685             :         }
     686             :     }
     687             : 
     688    44974242 :     return;
     689             : }
     690             : 
     691             : 
     692             : /*-----------------------------------------------------------------------------------------*
     693             :  * Function ivas_calc_post_pred_per_band()
     694             :  *
     695             :  * Calculate post pred mat per band
     696             :  *-----------------------------------------------------------------------------------------*/
     697             : 
     698   180183732 : 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   931010796 :     for ( i = 0; i < num_ch; i++ )
     711             :     {
     712  4042256112 :         for ( j = 0; j < num_ch; j++ )
     713             :         {
     714  3291429048 :             dmx_mat_conj[i][j] = mixer_mat[j][i][band_idx];
     715             :         }
     716             :     }
     717             : 
     718   931010796 :     for ( i = 0; i < num_ch; i++ )
     719             :     {
     720   750827064 :         set_zero( postpred_cov_re[i], num_ch );
     721             :     }
     722             : 
     723             :     /* num_ch x num_ch mult */
     724   931010796 :     for ( i = 0; i < num_ch; i++ )
     725             :     {
     726  4042256112 :         for ( j = 0; j < num_ch; j++ )
     727             :         {
     728  3291429048 :             temp_mat[i][j] = 0;
     729 19306218240 :             for ( k = 0; k < num_ch; k++ )
     730             :             {
     731 16014789192 :                 IVAS_RMULT_FLOAT( cov_real[i][k][band_idx], dmx_mat_conj[k][j], tmp_re );
     732 16014789192 :                 temp_mat[i][j] += tmp_re;
     733             :             }
     734             :         }
     735             :     }
     736             : 
     737             :     /* num_ch x num_ch mult */
     738   931010796 :     for ( i = 0; i < num_ch; i++ )
     739             :     {
     740  2771955120 :         for ( j = i; j < num_ch; j++ )
     741             :         {
     742 11674237176 :             for ( k = 0; k < num_ch; k++ )
     743             :             {
     744  9653109120 :                 IVAS_RMULT_FLOAT( mixer_mat[i][k][band_idx], temp_mat[k][j], tmp_re );
     745  9653109120 :                 postpred_cov_re[i][j] += tmp_re;
     746             :             }
     747             :         }
     748             :     }
     749             : 
     750   931010796 :     for ( i = 0; i < num_ch; i++ )
     751             :     {
     752  2021128056 :         for ( j = 0; j < i; j++ )
     753             :         {
     754  1270300992 :             postpred_cov_re[i][j] = postpred_cov_re[j][i];
     755             :         }
     756             :     }
     757             : 
     758   180183732 :     return;
     759             : }
     760             : 
     761             : 
     762             : /*-----------------------------------------------------------------------------------------*
     763             :  * Function ivas_calc_p_coeffs_per_band()
     764             :  *
     765             :  * Calculate P coeffs per band
     766             :  *-----------------------------------------------------------------------------------------*/
     767             : 
     768   128139186 : 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   128139186 :     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   128139186 :     float trace = 0;
     782   128139186 :     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   128139186 :     b_ts_idx = band_idx + i_ts * IVAS_MAX_NUM_BANDS;
     788             : 
     789   128139186 :     if ( num_dmx != num_ch )
     790             :     {
     791   128139186 :         set_zero( pSparMd->band_coeffs[b_ts_idx].P_re, IVAS_SPAR_MAX_CH - 1 );
     792  1025113488 :         for ( i = 0; i < IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS; i++ )
     793             :         {
     794   896974302 :             set_zero( recon_uu_re[i], IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS );
     795             :         }
     796             : 
     797   395813156 :         for ( i = num_dmx; i < num_ch; i++ )
     798             :         {
     799   938570364 :             for ( j = num_dmx; j < num_ch; j++ )
     800             :             {
     801   670896394 :                 cov_uu_re[i - num_dmx][j - num_dmx] = postpred_cov_re[i][j];
     802             :             }
     803             :         }
     804             : 
     805   128139186 :         if ( dtx_vad == 1 )
     806             :         {
     807   259420080 :             for ( i = 1; i < num_dmx; i++ )
     808             :             {
     809   355948984 :                 for ( j = 1; j < num_dmx; j++ )
     810             :                 {
     811   224332430 :                     cov_dd_re[i - 1][j - 1] = postpred_cov_re[i][j];
     812             :                 }
     813             :             }
     814             : 
     815   127803526 :             if ( num_dmx == 2 )
     816             :             {
     817             :                 float re1, re2;
     818             : 
     819    48356114 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[0][0], cov_dd_re[0][0], re1 );
     820    48356114 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[1][0], cov_dd_re[0][0], re2 );
     821    48356114 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[0][0], re1, recon_uu_re[0][0] );
     822    48356114 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[1][0], re1, recon_uu_re[0][1] );
     823    48356114 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[0][0], re2, recon_uu_re[1][0] );
     824    48356114 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[1][0], re2, recon_uu_re[1][1] );
     825             : 
     826   145068342 :                 for ( i = 0; i < 2; i++ )
     827             :                 {
     828   290136684 :                     for ( j = 0; j < 2; j++ )
     829             :                     {
     830   193424456 :                         cov_uu_re[i][j] -= recon_uu_re[i][j];
     831             :                     }
     832             :                 }
     833             :             }
     834    79447412 :             else if ( num_dmx == 3 )
     835             :             {
     836             :                 float re1[2], re2;
     837    36902502 :                 set_f( re1, 0, 2 );
     838             : 
     839   110707506 :                 for ( j = 0; j < 2; j++ )
     840             :                 {
     841   221415012 :                     for ( k = 0; k < 2; k++ )
     842             :                     {
     843             :                         float re;
     844             : 
     845   147610008 :                         IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[0][k], cov_dd_re[k][j], re );
     846   147610008 :                         re1[j] += re;
     847             :                     }
     848             :                 }
     849             : 
     850    36902502 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[0][0], re1[0], re2 );
     851    36902502 :                 recon_uu_re[0][0] = re2;
     852    36902502 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[0][1], re1[1], re2 );
     853    36902502 :                 recon_uu_re[0][0] += re2;
     854             : 
     855    36902502 :                 cov_uu_re[0][0] -= recon_uu_re[0][0];
     856             :             }
     857    42544910 :             else if ( num_dmx == 4 )
     858             :             {
     859             :                 /* Step 1: Multiply C * cov_dd * C' */
     860             :                 float re1[3], re;
     861             : 
     862    18197880 :                 for ( i = 0; i < num_ch - num_dmx; i++ )
     863             :                 {
     864    15046068 :                     set_f( re1, 0, 3 );
     865    60184272 :                     for ( m = 0; m < num_dmx - 1; m++ )
     866             :                     {
     867   180552816 :                         for ( k = 0; k < num_dmx - 1; k++ )
     868             :                         {
     869   135414612 :                             IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[i][k], cov_dd_re[k][m], re );
     870   135414612 :                             re1[m] += re;
     871             :                         }
     872             :                     }
     873    98922192 :                     for ( j = 0; j < num_ch - num_dmx; j++ )
     874             :                     {
     875   335504496 :                         for ( m = 0; m < num_dmx - 1; m++ )
     876             :                         {
     877   251628372 :                             IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[j][m], re1[m], re );
     878   251628372 :                             recon_uu_re[i][j] += re;
     879             :                         }
     880             :                     }
     881             :                 }
     882             : 
     883             :                 /* Step 2: cov_uu - recon_uu */
     884    18197880 :                 for ( i = 0; i < num_ch - num_dmx; i++ )
     885             :                 {
     886    98922192 :                     for ( j = 0; j < num_ch - num_dmx; j++ )
     887             :                     {
     888    83876124 :                         cov_uu_re[i][j] -= recon_uu_re[i][j];
     889             :                     }
     890             :                 }
     891             :             }
     892             :         }
     893             : 
     894   128139186 :         p_norm_scaling = IVAS_P_NORM_SCALING;
     895             : 
     896   128139186 :         if ( ( dtx_vad == 0 ) && ( num_dmx == 1 ) )
     897             :         {
     898      162558 :             p_norm_scaling = IVAS_P_NORM_SCALING_DTX;
     899             :         }
     900             : 
     901   128139186 :         trace = 0.0f;
     902             : 
     903   395813156 :         for ( i = num_dmx; i < num_ch; i++ )
     904             :         {
     905             :             float tmp_out;
     906   267673970 :             IVAS_CALCULATE_RABS( cov_uu_re[i - num_dmx][i - num_dmx], tmp_out );
     907   267673970 :             trace += tmp_out;
     908             :         }
     909             : 
     910   128139186 :         factor = max( 1e-20f, postpred_cov_re[0][0] );
     911   128139186 :         factor = max( factor, ( p_norm_scaling * trace ) );
     912   128139186 :         factor = 1 / factor;
     913             : 
     914             :         /* normalise Hermitian (except for rounding) cov_uu */
     915   395813156 :         for ( i = num_dmx; i < num_ch; i++ )
     916             :         {
     917   938570364 :             for ( j = num_dmx; j < num_ch; j++ )
     918             :             {
     919   670896394 :                 if ( i == j )
     920             :                 {
     921             :                     /* force diagonal to be real */
     922   267673970 :                     cov_uu_re[i - num_dmx][j - num_dmx] *= factor;
     923             :                 }
     924             :                 else
     925             :                 {
     926             :                     /* set off-diag elements to zero */
     927   403222424 :                     cov_uu_re[i - num_dmx][j - num_dmx] = 0;
     928             :                 }
     929             :             }
     930             :         }
     931             : 
     932             :         /* take sqrt of max of diags and zero */
     933   395813156 :         for ( i = num_dmx; i < num_ch; i++ )
     934             :         {
     935   267673970 :             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   395813156 :         for ( i = num_dmx; i < num_ch; i++ )
     941             :         {
     942   938570364 :             for ( j = num_dmx; j < num_ch; j++ )
     943             :             {
     944   670896394 :                 if ( i == j )
     945             :                 {
     946   267673970 :                     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   128139186 :     return;
     953             : }
     954             : 
     955             : 
     956             : /*-----------------------------------------------------------------------------------------*
     957             :  * Function ivas_calc_c_coeffs_per_band()
     958             :  *
     959             :  * Calculate C coeffs per band
     960             :  *-----------------------------------------------------------------------------------------*/
     961             : 
     962   101072420 : 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   101072420 :     float trace_cov_dd_re = 0;
     978             :     float abs_trace;
     979             :     float re;
     980             :     int16_t b_ts_idx;
     981             : 
     982   101072420 :     b_ts_idx = band_idx + i_ts * IVAS_MAX_NUM_BANDS;
     983             : 
     984   101072420 :     if ( dtx_vad == 0 )
     985             :     {
     986      185436 :         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      185436 :         return;
     988             :     }
     989             : 
     990   279347890 :     for ( i = num_dmx; i < num_ch; i++ )
     991             :     {
     992   457904034 :         for ( j = 1; j < num_dmx; j++ )
     993             :         {
     994   279443128 :             cov_ud_re[i - num_dmx][j - 1] = postpred_cov_re[i][j];
     995             :         }
     996             :     }
     997             : 
     998   255179166 :     for ( i = 1; i < num_dmx; i++ )
     999             :     {
    1000   428002008 :         for ( j = 1; j < num_dmx; j++ )
    1001             :         {
    1002   273709826 :             cov_dd_re[i - 1][j - 1] = postpred_cov_re[i][j];
    1003             :         }
    1004             :     }
    1005             : 
    1006   255179166 :     for ( i = 0; i < num_dmx - 1; i++ )
    1007             :     {
    1008   154292182 :         trace_cov_dd_re += cov_dd_re[i][i];
    1009             :     }
    1010   100886984 :     trace_cov_dd_re *= 0.005f;
    1011             : 
    1012   100886984 :     IVAS_CALCULATE_RABS( trace_cov_dd_re, abs_trace );
    1013             : 
    1014   100886984 :     if ( abs_trace < IVAS_FLT_EPS )
    1015             :     {
    1016             :         /* protection from cases when variance of residual channels is very small */
    1017     5117354 :         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   242602249 :         for ( i = 0; i < num_dmx - 1; i++ )
    1022             :         {
    1023   146832619 :             cov_dd_re[i][i] += trace_cov_dd_re;
    1024             :         }
    1025    95769630 :         if ( ivas_is_mat_inv( cov_dd_re, num_dmx - 1 ) && ( num_dmx < FOA_CHANNELS ) )
    1026             :         {
    1027        9501 :             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    95760129 :             ivas_calc_mat_inv( cov_dd_re, num_dmx - 1, cov_dd_re_inv );
    1032             : 
    1033   265990772 :             for ( i = 0; i < num_ch - num_dmx; i++ )
    1034             :             {
    1035   438621434 :                 for ( j = 0; j < num_dmx - 1; j++ )
    1036             :                 {
    1037   268390791 :                     pSparMd->band_coeffs[b_ts_idx].C_re[i][j] = 0;
    1038   792629624 :                     for ( k = 0; k < num_dmx - 1; k++ )
    1039             :                     {
    1040   524238833 :                         IVAS_RMULT_FLOAT( cov_ud_re[i][k], cov_dd_re_inv[k][j], re );
    1041   524238833 :                         pSparMd->band_coeffs[b_ts_idx].C_re[i][j] += re;
    1042             :                     }
    1043             :                 }
    1044             :             }
    1045             :         }
    1046             :     }
    1047             : 
    1048   100886984 :     return;
    1049             : }
    1050             : 
    1051             : 
    1052             : /*-----------------------------------------------------------------------------------------*
    1053             :  * Function ivas_calc_c_p_coeffs()
    1054             :  *
    1055             :  * Calculation of C and P coeffs
    1056             :  *-----------------------------------------------------------------------------------------*/
    1057             : 
    1058   140628076 : 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   140628076 :     if ( num_dmx != num_ch )
    1074             :     {
    1075   140628076 :         ivas_calc_post_pred_per_band( cov_real, mixer_mat, num_ch, band_idx, postpred_cov_re );
    1076             : 
    1077   140628076 :         if ( num_dmx != 1 )
    1078             :         {
    1079   101072420 :             ivas_calc_c_coeffs_per_band( pSparMd, i_ts, postpred_cov_re, num_ch, num_dmx, band_idx, dtx_vad );
    1080             :         }
    1081             : 
    1082   140628076 :         if ( dyn_active_w_flag )
    1083             :         {
    1084      193040 :             for ( i = 0; i < num_ch - num_dmx; i++ )
    1085             :             {
    1086      257600 :                 for ( j = 0; j < num_dmx - 1; j++ )
    1087             :                 {
    1088      129120 :                     pSparMd->band_coeffs[band_idx + i_ts * IVAS_MAX_NUM_BANDS].C_re[i][j] = 0.0f;
    1089             :                 }
    1090             :             }
    1091             :         }
    1092   140628076 :         if ( compute_p_flag == 1 )
    1093             :         {
    1094   128139186 :             ivas_calc_p_coeffs_per_band( pSparMd, i_ts, postpred_cov_re, num_ch, dtx_vad, num_dmx, band_idx );
    1095             :         }
    1096             : 
    1097   140628076 :         if ( dyn_active_w_flag )
    1098             :         {
    1099      193040 :             for ( i = num_dmx; i < num_ch; i++ )
    1100             :             {
    1101      128480 :                 pSparMd->band_coeffs[band_idx + i_ts * IVAS_MAX_NUM_BANDS].P_re[i - num_dmx] = 0;
    1102             :             }
    1103             :         }
    1104             :     }
    1105             : 
    1106   140628076 :     return;
    1107             : }
    1108             : 
    1109             : 
    1110   196507462 : 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   196507462 :     if ( dim == IVAS_MAT_DIM_3 )
    1116             :     {
    1117             :         double re1, re2, re;
    1118    12421086 :         IVAS_RMULT_DOUBLE( in_re[1][1], in_re[2][2], re1 );
    1119    12421086 :         IVAS_RMULT_DOUBLE( in_re[1][2], in_re[2][1], re2 );
    1120    12421086 :         re = re1 - re2;
    1121             : 
    1122    12421086 :         IVAS_RMULT_DOUBLE( in_re[0][0], re, re1 );
    1123             : 
    1124    12421086 :         *det_re = re1;
    1125             : 
    1126    12421086 :         IVAS_RMULT_DOUBLE( in_re[1][0], in_re[2][2], re1 );
    1127    12421086 :         IVAS_RMULT_DOUBLE( in_re[1][2], in_re[2][0], re2 );
    1128    12421086 :         re = re1 - re2;
    1129             : 
    1130    12421086 :         IVAS_RMULT_DOUBLE( in_re[0][1], re, re1 );
    1131             : 
    1132    12421086 :         *det_re -= re1;
    1133             : 
    1134    12421086 :         IVAS_RMULT_DOUBLE( in_re[1][0], in_re[2][1], re1 );
    1135    12421086 :         IVAS_RMULT_DOUBLE( in_re[1][1], in_re[2][0], re2 );
    1136    12421086 :         re = re1 - re2;
    1137             : 
    1138    12421086 :         IVAS_RMULT_DOUBLE( in_re[0][2], re, re1 );
    1139             : 
    1140    12421086 :         *det_re += re1;
    1141             :     }
    1142   184086376 :     else if ( dim == IVAS_MAT_DIM_2 )
    1143             :     {
    1144             :         double re1, re2;
    1145   133169192 :         IVAS_RMULT_DOUBLE( in_re[0][0], in_re[1][1], re1 );
    1146   133169192 :         IVAS_RMULT_DOUBLE( in_re[0][1], in_re[1][0], re2 );
    1147   133169192 :         *det_re = re1 - re2;
    1148             :     }
    1149    50917184 :     else if ( dim == IVAS_MAT_DIM_1 )
    1150             :     {
    1151    50917184 :         *det_re = in_re[0][0];
    1152             :     }
    1153             :     else
    1154             :     {
    1155           0 :         assert( !"matrix dimention not supported!" );
    1156             :     }
    1157             : 
    1158   196507462 :     return;
    1159             : }
    1160             : 
    1161             : 
    1162             : /*-----------------------------------------------------------------------------------------*
    1163             :  * Function ivas_get_mat_cofactor()
    1164             :  *
    1165             :  * Calculate cofactor for invert matrix
    1166             :  *-----------------------------------------------------------------------------------------*/
    1167             : 
    1168    55894887 : 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    55894887 :     int16_t r = 0, c = 0;
    1176             : 
    1177   223579548 :     for ( i = 0; i < MAX_MAT_DIM; i++ )
    1178             :     {
    1179   670738644 :         for ( j = 0; j < MAX_MAT_DIM; j++ )
    1180             :         {
    1181   503053983 :             if ( i != row && j != col )
    1182             :             {
    1183             : 
    1184   223579548 :                 out_re[r][c++] = in_re[i][j];
    1185             :             }
    1186             :         }
    1187   167684661 :         if ( c == 2 )
    1188             :         {
    1189   111789774 :             r++;
    1190   111789774 :             c = 0;
    1191             :         }
    1192             :     }
    1193             : 
    1194    55894887 :     return;
    1195             : }
    1196             : 
    1197             : 
    1198             : /*-----------------------------------------------------------------------------------------*
    1199             :  * Function ivas_calc_mat_inv()
    1200             :  *
    1201             :  * Calculate Invert of a matrix
    1202             :  *-----------------------------------------------------------------------------------------*/
    1203             : 
    1204    95760129 : 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   242573746 :     for ( i = 0; i < dim; i++ )
    1215             :     {
    1216   408155296 :         for ( j = 0; j < dim; j++ )
    1217             :         {
    1218   261341679 :             dbl_in_re[i][j] = in_re[i][j];
    1219             :         }
    1220             :     }
    1221             : 
    1222    95760129 :     if ( dim == IVAS_MAT_DIM_1 )
    1223             :     {
    1224    50917184 :         det = ( dbl_in_re[0][0] * dbl_in_re[0][0] );
    1225             :         /* assert to catch cases when input is singular matrix*/
    1226    50917184 :         assert( det > 0 );
    1227    50917184 :         det = 1 / det;
    1228    50917184 :         dbl_out_re[0][0] = dbl_in_re[0][0] * det;
    1229             :     }
    1230    44842945 :     else if ( dim == IVAS_MAT_DIM_2 )
    1231             :     {
    1232             :         double det_re;
    1233             :         double re;
    1234             : 
    1235    38632402 :         ivas_calc_mat_det( dbl_in_re, dim, &det_re );
    1236    38632402 :         det = ( det_re * det_re );
    1237             :         /* assert to catch cases when input is singular matrix*/
    1238    38632402 :         assert( det > 0 );
    1239    38632402 :         det = 1 / det;
    1240             : 
    1241    38632402 :         IVAS_RMULT_DOUBLE( det_re, dbl_in_re[1][1], re );
    1242    38632402 :         dbl_out_re[0][0] = re * det;
    1243             : 
    1244    38632402 :         IVAS_RMULT_DOUBLE( det_re, dbl_in_re[0][1], re );
    1245    38632402 :         dbl_out_re[0][1] = -re * det;
    1246             : 
    1247    38632402 :         IVAS_RMULT_DOUBLE( det_re, dbl_in_re[1][0], re );
    1248    38632402 :         dbl_out_re[1][0] = -re * det;
    1249             : 
    1250    38632402 :         IVAS_RMULT_DOUBLE( det_re, dbl_in_re[0][0], re );
    1251    38632402 :         dbl_out_re[1][1] = re * det;
    1252             :     }
    1253     6210543 :     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     6210543 :         int16_t sign = 1;
    1258             : 
    1259     6210543 :         ivas_calc_mat_det( dbl_in_re, dim, &det_re );
    1260     6210543 :         det = det_re > 0 ? 1 / max( IVAS_DBL_EPS, det_re ) : 1 / min( det_re, -IVAS_DBL_EPS );
    1261             : 
    1262    24842172 :         for ( i = 0; i < dim; i++ )
    1263             :         {
    1264    74526516 :             for ( j = 0; j < dim; j++ )
    1265             :             {
    1266    55894887 :                 ivas_get_mat_cofactor( dbl_in_re, fac_re, i, j );
    1267    55894887 :                 ivas_calc_mat_det( fac_re, IVAS_MAT_DIM_2, &dbl_out_re[j][i] );
    1268    55894887 :                 dbl_out_re[j][i] = dbl_out_re[j][i] * det * sign;
    1269             : 
    1270    55894887 :                 if ( ( ( i + j ) & 1 ) == 0 )
    1271             :                 {
    1272    31052715 :                     sign = -1;
    1273             :                 }
    1274             :                 else
    1275             :                 {
    1276    24842172 :                     sign = 1;
    1277             :                 }
    1278             :             }
    1279             :         }
    1280             :     }
    1281             :     else
    1282             :     {
    1283           0 :         assert( !"matrix dimension not supported!" );
    1284             :     }
    1285             : 
    1286   242573746 :     for ( i = 0; i < dim; i++ )
    1287             :     {
    1288   408155296 :         for ( j = 0; j < dim; j++ )
    1289             :         {
    1290   261341679 :             out_re[i][j] = (float) dbl_out_re[i][j];
    1291             :         }
    1292             :     }
    1293             : 
    1294    95760129 :     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    95769630 : static int16_t ivas_is_mat_inv(
    1305             :     float in_re[MAX_MAT_DIM][MAX_MAT_DIM],
    1306             :     const int16_t dim )
    1307             : {
    1308    95769630 :     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   242602249 :     for ( i = 0; i < dim; i++ )
    1314             :     {
    1315   408212302 :         for ( j = 0; j < dim; j++ )
    1316             :         {
    1317   261379683 :             dbl_in_re[i][j] = in_re[i][j];
    1318             :         }
    1319             :     }
    1320             : 
    1321    95769630 :     ivas_calc_mat_det( dbl_in_re, dim, &det_re );
    1322             : 
    1323    95769630 :     det = ( det_re * det_re );
    1324             : 
    1325    95769630 :     if ( det < IVAS_DBL_EPS )
    1326             :     {
    1327       11320 :         is_det_zero = 1;
    1328             :     }
    1329             : 
    1330    95769630 :     return is_det_zero;
    1331             : }
    1332             : 
    1333             : 
    1334             : /*-----------------------------------------------------------------------------------------*
    1335             :  * Function ivas_compute_spar_params()
    1336             :  *
    1337             :  *
    1338             :  *-----------------------------------------------------------------------------------------*/
    1339             : 
    1340    42233031 : 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    42233031 :     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    42233031 :     ivas_create_fullr_dmx_mat( pred_coeffs_re, dm_fv_re, mixer_mat, num_ch, start_band, end_band, active_w, hSparCfg );
    1364             : 
    1365    42233031 :     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   209868075 :     for ( b = start_band; b < end_band; b++ )
    1368             :     {
    1369   167635044 :         float onebyscale = 1.0f / pWscale[b];
    1370   685586244 :         for ( i = 0; i < num_ch - 1; i++ )
    1371             :         {
    1372   517951200 :             hSparMd->band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].pred_re[i] = pred_coeffs_re[i][b] * onebyscale;
    1373             :         }
    1374             : 
    1375   853221288 :         for ( i = 0; i < num_ch; i++ )
    1376             :         {
    1377   685586244 :             mixer_mat[0][i][b] *= pWscale[b];
    1378             :         }
    1379             :     }
    1380             : 
    1381   209868075 :     for ( b = start_band; b < end_band; b++ )
    1382             :     {
    1383   167635044 :         ndm = hSparCfg->num_dmx_chans_per_band[b * bands_bw];
    1384             : 
    1385   167635044 :         if ( ndm != num_ch )
    1386             :         {
    1387   128139186 :             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    42233031 :     return;
    1414             : }
    1415             : 
    1416             : 
    1417             : /*-----------------------------------------------------------------------------------------*
    1418             :  * Function ivas_get_spar_md_from_dirac()
    1419             :  *
    1420             :  *
    1421             :  *-----------------------------------------------------------------------------------------*/
    1422             : 
    1423    16583895 : 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    16583895 :     remix_order = remix_order_set[hSpar_md_cfg->remix_unmix_order];
    1459             : 
    1460    16583895 :     num_ch = ivas_sba_get_nchan_metadata( order, IVAS_256k /*dummy value as order is always 1 in this function*/ );
    1461             : 
    1462    16583895 :     hoa2_ch = ivas_sba_get_nchan_metadata( SBA_HOA2_ORDER, IVAS_256k /*dummy value as order is always 1 in this function*/ );
    1463             : 
    1464    16583895 :     foa_ch = FOA_CHANNELS;
    1465    16583895 :     diff_norm_order1 = 3.0f;
    1466    16583895 :     diff_norm_order2 = 5.0f;
    1467    16583895 :     diff_norm_order3 = 7.0f;
    1468             : 
    1469   199006740 :     for ( i = 0; i < IVAS_MAX_FB_MIXER_OUT_CH; i++ )
    1470             :     {
    1471  2189074140 :         for ( j = 0; j < IVAS_MAX_SPAR_FB_MIXER_IN_CH; j++ )
    1472             :         {
    1473  2006651295 :             pMixer_mat[i][j] = mixer_mat_local[i][j];
    1474             :         }
    1475   182422845 :         ppMixer_mat[i] = pMixer_mat[i];
    1476             :     }
    1477             : 
    1478    16583895 :     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     8480549 :         ndm = hSpar_md_cfg->num_dmx_chans_per_band[start_band - 1];
    1484     8480549 :         P_norm[0] = 0.0f;
    1485    28885917 :         for ( i = 0; i < max( 0, foa_ch - ndm ); i++ )
    1486             :         {
    1487    20405368 :             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     8480549 :         P_norm[0] *= diff_norm_order1 / min( diff_norm_order1, max( 0, foa_ch - ndm ) );
    1490             : 
    1491     8480549 :         P_norm[1] = 0.0f;
    1492     8480549 :         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     8480549 :         P_norm[1] *= diff_norm_order2 / min( diff_norm_order2, max( 0, min( num_ch, hoa2_ch ) - ndm ) );
    1497             : 
    1498     8480549 :         P_norm[2] = 0.0f;
    1499     8480549 :         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     8480549 :         P_norm[2] *= diff_norm_order3 / min( diff_norm_order3, max( 0, num_ch - ndm ) );
    1504             : 
    1505    28885917 :         for ( i = 0; i < max( 0, foa_ch - ndm ); i++ )
    1506             :         {
    1507    20405368 :             idx = remix_order[i + ndm] - ndm;
    1508    20405368 :             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    20405368 :             P_dir_fact[idx] = P_dir_fact[idx] / max( IVAS_FLT_EPS, P_norm[0] );
    1510             :         }
    1511     8480549 :         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     8480549 :         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    56087940 :     for ( int16_t i_ts = 0; i_ts < n_ts; i_ts++ )
    1526             :     {
    1527   185124289 :         for ( band = start_band; band < end_band; band++ )
    1528             :         {
    1529   145620244 :             ndm = hSpar_md_cfg->num_dmx_chans_per_band[band];
    1530             : 
    1531             :             /*SPAR from DirAC*/
    1532   145620244 :             set_f( response_avg, 0.0f, MAX_OUTPUT_CHANNELS );
    1533             : 
    1534   145620244 :             if ( n_ts > 1 )
    1535             :             {
    1536   111402824 :                 ivas_dirac_dec_get_response( (int16_t) azi_dirac[band][i_ts], (int16_t) ele_dirac[band][i_ts], response_avg, order );
    1537             :             }
    1538    34217420 :             else if ( useLowerRes )
    1539             :             {
    1540     3128610 :                 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   155444050 :                 for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; block++ )
    1545             :                 {
    1546   124355240 :                     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    31088810 :                     num_ch_order = ivas_sba_get_nchan( order, 0 );
    1555    31088810 :                     hoa2_ch_order = ivas_sba_get_nchan( SBA_HOA2_ORDER, 0 );
    1556             : 
    1557   155444050 :                     for ( ch = 0; ch < num_ch_order; ch++ )
    1558             :                     {
    1559   621776200 :                         for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; block++ )
    1560             :                         {
    1561   497420960 :                             response_avg[ch] += response[block][ch];
    1562             :                         }
    1563   124355240 :                         response_avg[ch] /= MAX_PARAM_SPATIAL_SUBFRAMES;
    1564             :                     }
    1565             : 
    1566             :                     /*normalize 1st order*/
    1567    31088810 :                     norm = 0.0f;
    1568   124355240 :                     for ( ch = 1; ch < foa_ch; ch++ )
    1569             :                     {
    1570    93266430 :                         norm += response_avg[ch] * response_avg[ch];
    1571             :                     }
    1572    31088810 :                     norm = max( EPSILON, sqrtf( norm ) );
    1573   124355240 :                     for ( ch = 1; ch < foa_ch; ch++ )
    1574             :                     {
    1575    93266430 :                         response_avg[ch] /= norm;
    1576             :                     }
    1577             : 
    1578             :                     /*normalize 2nd order*/
    1579    31088810 :                     norm = 0.0f;
    1580    31088810 :                     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    31088810 :                     norm = max( EPSILON, sqrtf( norm ) );
    1585    31088810 :                     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    31088810 :                     norm = 0.0f;
    1592    31088810 :                     for ( ch = hoa2_ch_order; ch < num_ch_order; ch++ )
    1593             :                     {
    1594           0 :                         norm += response_avg[ch] * response_avg[ch];
    1595             :                     }
    1596    31088810 :                     norm = max( EPSILON, sqrtf( norm ) );
    1597    31088810 :                     for ( ch = hoa2_ch_order; ch < num_ch_order; ch++ )
    1598             :                     {
    1599           0 :                         response_avg[ch] /= norm;
    1600             :                     }
    1601             :                 }
    1602             :             }
    1603             : 
    1604   145620244 :             for ( i = FOA_CHANNELS + 1; i < num_ch; i++ )
    1605             :             {
    1606           0 :                 response_avg[i] = response_avg[HOA_keep_ind[i]];
    1607             :             }
    1608             : 
    1609   145620244 :             en_ratio_fac = ( 1.0f - diffuseness[band] );
    1610             : 
    1611   728101220 :             for ( i = 0; i < num_ch; i++ )
    1612             :             {
    1613  2912404880 :                 for ( j = 0; j < num_ch; j++ )
    1614             :                 {
    1615  2329923904 :                     if ( i == j )
    1616             :                     {
    1617   582480976 :                         if ( i == 0 )
    1618             :                         {
    1619   145620244 :                             cov_real_dirac[i][i][band] = 1.0f;
    1620             :                         }
    1621             :                         else
    1622             :                         {
    1623   436860732 :                             cov_real_dirac[i][j][band] = en_ratio_fac * response_avg[i] * response_avg[j];
    1624             : 
    1625   436860732 :                             if ( hSpar_md_cfg->nchan_transport <= 2 )
    1626             :                             {
    1627             : 
    1628   229664028 :                                 cov_real_dirac[i][j][band] *= en_ratio_fac;
    1629   229664028 :                                 if ( ( i >= ndm ) && ( dtx_vad == 1 ) )
    1630             :                                 {
    1631   185801898 :                                     cov_real_dirac[i][j][band] += ( 1.0f - ( en_ratio_fac * en_ratio_fac ) ) * P_dir_fact[i - ndm];
    1632             :                                 }
    1633             :                                 else
    1634             :                                 {
    1635    43862130 :                                     if ( i < foa_ch )
    1636             :                                     {
    1637    43862130 :                                         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   207196704 :                                 if ( i < foa_ch )
    1652             :                                 {
    1653   207196704 :                                     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  1747442928 :                         cov_real_dirac[i][j][band] = en_ratio_fac * response_avg[i] * response_avg[j];
    1669             :                     }
    1670             :                 }
    1671             :             }
    1672             :         }
    1673             : 
    1674   197520225 :         for ( i = 0; i < num_ch; i++ )
    1675             :         {
    1676   790080900 :             for ( j = 0; j < num_ch; j++ )
    1677             :             {
    1678   632064720 :                 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    39504045 :         active_w = ( dyn_active_w_flag == 1 ) || ( hSpar_md_cfg->active_w == 1 );
    1707             : 
    1708    39504045 :         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    39504045 :         if ( mixer_mat != NULL )
    1711             :         {
    1712    13535905 :             for ( band = start_band; band < end_band; band++ )
    1713             :             {
    1714    10931412 :                 ndm = hSpar_md_cfg->num_dmx_chans_per_band[band];
    1715             : 
    1716    36840680 :                 for ( i = 0; i < ndm; i++ )
    1717             :                 {
    1718   129546340 :                     for ( j = 0; j < num_ch; j++ )
    1719             :                     {
    1720   103637072 :                         mixer_mat[i][j][band + i_ts * IVAS_MAX_NUM_BANDS] = ppMixer_mat[i][j][band];
    1721             :                     }
    1722             :                 }
    1723             : 
    1724    28747792 :                 for ( i = ndm; i < num_ch; i++ )
    1725             :                 {
    1726    89081900 :                     for ( j = 0; j < num_ch; j++ )
    1727             :                     {
    1728    71265520 :                         mixer_mat[i][j][band + i_ts * IVAS_MAX_NUM_BANDS] = 0.0f;
    1729             :                     }
    1730             :                 }
    1731             : 
    1732    10931412 :                 if ( ( ndm == 1 ) && ( Wscale_d != NULL ) )
    1733             :                 {
    1734    17353840 :                     for ( j = 0; j < num_ch; j++ )
    1735             :                     {
    1736    13883072 :                         mixer_mat[0][j][band + i_ts * IVAS_MAX_NUM_BANDS] *= Wscale_d[band];
    1737             :                     }
    1738             :                 }
    1739             :             }
    1740             :         }
    1741             :     }
    1742             : 
    1743    16583895 :     return;
    1744             : }
    1745             : 
    1746             : 
    1747             : /*-------------------------------------------------------------------------
    1748             :  * ivas_dirac_dec_get_response()
    1749             :  *
    1750             :  * calculate reponse, 1 degree resolution
    1751             :  *------------------------------------------------------------------------*/
    1752             : 
    1753  5059859640 : 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  5059859640 :     index_azimuth = ( azimuth + 180 ) % 360;
    1768  5059859640 :     index_elevation = elevation + 90;
    1769  5059859640 :     e = index_elevation > 90 ? -1 : 1;
    1770  5059859640 :     el = index_elevation > 90 ? 180 - index_elevation : index_elevation;
    1771             : 
    1772  5059859640 :     az = index_azimuth > 180 ? 360 - index_azimuth : index_azimuth;
    1773  5059859640 :     f = index_azimuth > 180 ? -1.0f : 1.0f;
    1774             : 
    1775  5059859640 :     cos_1 = dirac_gains_trg_term[az][0];
    1776  5059859640 :     cos_2 = cos_1 * cos_1;
    1777  5059859640 :     sin_1 = f * dirac_gains_trg_term[az][1];
    1778  5059859640 :     cos_az[0] = cos_1;
    1779  5059859640 :     cos_az[1] = 2.0f * cos_2 - 1.0f;
    1780  5059859640 :     cos_az[2] = 2.0f * cos_1 * cos_az[1] - cos_az[0];
    1781  5059859640 :     sin_az[0] = sin_1;
    1782  5059859640 :     sin_az[1] = sin_1 * 2.0f * cos_1;
    1783  5059859640 :     sin_az[2] = sin_1 * ( 4.0f * cos_2 - 1.0f );
    1784             : 
    1785  5059859640 :     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 18671597177 :     for ( l = 1; l <= ambisonics_order; l++ )
    1821             :     {
    1822 13611737537 :         b_2 = l * l;
    1823 13611737537 :         b1_2 = l * l + 2 * l;
    1824 31230223684 :         for ( m = 0; m < l; m += 2 )
    1825             :         {
    1826 17618486147 :             b = b_2 + m;
    1827 17618486147 :             a = dirac_gains_P_idx[b];
    1828 17618486147 :             c = SQRT2 * dirac_gains_norm_term[a] * dirac_gains_Pnm[el][a];
    1829             : 
    1830 17618486147 :             response[b] = c * sin_az[l - m - 1];
    1831             : 
    1832 17618486147 :             b1 = b1_2 - m;
    1833 17618486147 :             response[b1] = c * cos_az[l - m - 1];
    1834             :         }
    1835             : 
    1836 22163615434 :         for ( m = 1; m < l; m += 2 )
    1837             :         {
    1838  8551877897 :             b = b_2 + m;
    1839  8551877897 :             a = dirac_gains_P_idx[b];
    1840  8551877897 :             c = SQRT2 * dirac_gains_norm_term[a] * dirac_gains_Pnm[el][a];
    1841  8551877897 :             c = c * e;
    1842             : 
    1843  8551877897 :             response[b] = c * sin_az[l - m - 1];
    1844             : 
    1845  8551877897 :             b1 = b1_2 - m;
    1846  8551877897 :             response[b1] = c * cos_az[l - m - 1];
    1847             :         }
    1848             : 
    1849 13611737537 :         b = b_2 + l;
    1850 13611737537 :         a = dirac_gains_P_idx[b];
    1851 13611737537 :         c = dirac_gains_norm_term[a] * dirac_gains_Pnm[el][a];
    1852 13611737537 :         if ( l % 2 == 1 )
    1853             :         {
    1854  9066608250 :             c = c * e;
    1855             :         }
    1856             : 
    1857 13611737537 :         response[b] = c;
    1858             :     }
    1859             : 
    1860  5059859640 :     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    11999124 : int16_t ivas_get_bits_to_encode(
    1871             :     int32_t val )
    1872             : {
    1873    11999124 :     int16_t bits_req = 0;
    1874             : 
    1875    11999124 :     assert( val >= 0 );
    1876             : 
    1877    18021466 :     while ( val )
    1878             :     {
    1879     6022342 :         bits_req++;
    1880     6022342 :         val >>= 1;
    1881             :     }
    1882             : 
    1883    11999124 :     return bits_req;
    1884             : }
    1885             : 
    1886             : 
    1887             : /*-----------------------------------------------------------------------------------------*
    1888             :  * Function ivas_spar_set_bitrate_config()
    1889             :  *
    1890             :  * Set SPAR bitrate config
    1891             :  *-----------------------------------------------------------------------------------------*/
    1892             : 
    1893      148193 : 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      148193 :     pSpar_md_cfg->nchan_transport = ivas_spar_br_table_consts[table_idx].nchan_transport;
    1914             : 
    1915      499281 :     for ( i = 0; i < pSpar_md_cfg->nchan_transport; i++ )
    1916             :     {
    1917      351088 :         pSpar_md_cfg->max_freq_per_chan[i] = ivas_spar_br_table_consts[table_idx].fpcs;
    1918             :     }
    1919             : 
    1920      148193 :     pSpar_md_cfg->active_w = ivas_spar_br_table_consts[table_idx].active_w;
    1921      148193 :     pSpar_md_cfg->agc_bits_ch_idx = ivas_spar_br_table_consts[table_idx].agc_bits_ch_idx;
    1922      148193 :     ivas_spar_get_uniform_quant_strat( pSpar_md_cfg, table_idx );
    1923             : 
    1924      148193 :     pSpar_md_cfg->quant_strat_bits = ivas_get_bits_to_encode( MAX_QUANT_STRATS );
    1925             : 
    1926             :     /* BLOCK: getEntropyCoderModels */
    1927             : 
    1928      148193 :     pSpar_md_cfg->remix_unmix_order = ivas_spar_br_table_consts[table_idx].dmx_str;
    1929             : 
    1930             :     /* bits per block*/
    1931      148193 :     total_bits = 0;
    1932      148193 :     max_bits = 0;
    1933             : 
    1934      148193 :     ivas_total_brate = ivas_spar_br_table_consts[table_idx].ivas_total_brate;
    1935      148193 :     sba_order = ivas_spar_br_table_consts[table_idx].sba_order;
    1936      148193 :     ivas_get_spar_table_idx( ivas_total_brate, sba_order, ivas_spar_br_table_consts[table_idx].bwidth, &length, &code );
    1937             : 
    1938      499281 :     for ( i = 0; i < pSpar_md_cfg->nchan_transport; i++ )
    1939             :     {
    1940      351088 :         total_bits += (int16_t) ( ivas_spar_br_table_consts[table_idx].core_brs[i][0] / FRAMES_PER_SEC );
    1941      351088 :         max_bits += (int16_t) ( ivas_spar_br_table_consts[table_idx].core_brs[i][1] / FRAMES_PER_SEC );
    1942             :     }
    1943             : 
    1944      148193 :     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      148193 :     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      148193 :     md_coding_bits_header = SPAR_NUM_CODING_STRAT_BITS + pSpar_md_cfg->quant_strat_bits;
    1948             : 
    1949      148193 :     pSpar_md_cfg->tgt_bits_per_blk -= md_coding_bits_header;
    1950      148193 :     pSpar_md_cfg->max_bits_per_blk -= md_coding_bits_header;
    1951             : 
    1952      148193 :     if ( ivas_total_brate < IVAS_24k4 )
    1953             :     {
    1954       27891 :         bands_bw = 2;
    1955             :     }
    1956             :     else
    1957             :     {
    1958      120302 :         bands_bw = 1;
    1959             :     }
    1960             : 
    1961      148193 :     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      148193 :     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      148193 :     pSpar_md_cfg->tgt_bits_per_blk += md_coding_bits_header;
    1965      148193 :     pSpar_md_cfg->max_bits_per_blk += md_coding_bits_header;
    1966             : 
    1967      148193 :     if ( enc_flag )
    1968             :     {
    1969             :         /*calculate the actual worst case bits*/
    1970       39671 :         if ( ivas_total_brate >= BRATE_SPAR_Q_STRAT )
    1971             :         {
    1972        8625 :             quant_strat = QUANT_STRAT_0;
    1973             :         }
    1974             :         else
    1975             :         {
    1976       31046 :             quant_strat = QUANT_STRAT_2;
    1977             :         }
    1978             : 
    1979       39671 :         num_PR_bits_dirac_bands = ( dirac2spar_md_flag == 1 ) ? num_bands - SPAR_DIRAC_SPLIT_START_BAND : 0;
    1980       39671 :         num_PR_bits_dirac_bands /= bands_bw;
    1981       39671 :         num_PR_bits_dirac_bands = max( 0, num_PR_bits_dirac_bands );
    1982       39671 :         num_PR_bits_dirac_bands *= DIRAC_TO_SPAR_HBR_PRED_CHS;
    1983             : 
    1984       39671 :         n_input = ivas_sba_get_nchan_metadata( sba_order, ivas_total_brate );
    1985       39671 :         n_dmx = ivas_spar_br_table_consts[table_idx].nchan_transport;
    1986       39671 :         n_dec = n_input - n_dmx;
    1987       39671 :         bits_PR = (int16_t) ceilf( log2f( ivas_spar_br_table_consts[table_idx].q_lvls[quant_strat][0] ) );
    1988       39671 :         num_PR_bits_dirac_bands *= bits_PR;
    1989       39671 :         bits_PR = bits_PR * ( n_input - 1 );
    1990       39671 :         bits_C = (int16_t) ceilf( log2f( ivas_spar_br_table_consts[table_idx].q_lvls[quant_strat][1] ) ) * ( ( n_dmx - 1 ) * n_dec );
    1991       39671 :         bits_P = (int16_t) ceilf( log2f( ivas_spar_br_table_consts[table_idx].q_lvls[quant_strat][2] ) ) * ( n_dec );
    1992       39671 :         wc_coarse_strat = bits_PR + bits_C + bits_P;
    1993       39671 :         wc_coarse_strat *= num_bands;
    1994       39671 :         wc_coarse_strat /= bands_bw;
    1995       39671 :         wc_coarse_strat -= num_PR_bits_dirac_bands;
    1996       39671 :         wc_coarse_strat += md_coding_bits_header;
    1997             : 
    1998       39671 :         if ( pSpar_md_cfg->max_bits_per_blk < wc_coarse_strat )
    1999             :         {
    2000           0 :             assert( 0 );
    2001             :         }
    2002             : 
    2003       39671 :         if ( agc_flag )
    2004             :         {
    2005       12050 :             if ( pSpar_md_cfg->nchan_transport == 1 )
    2006             :             {
    2007       12050 :                 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       27621 :             agc_bits = AGC_SIGNALLING_BITS;
    2017             :         }
    2018             : 
    2019       39671 :         if ( ivas_total_brate == PCA_BRATE && sba_order == SBA_FOA_ORDER )
    2020             :         {
    2021         898 :             pca_bits = 1;
    2022         898 :             if ( pca_flag )
    2023             :             {
    2024          24 :                 pca_bits += IVAS_PCA_QBITS + IVAS_PCA_QBITS - 1;
    2025             :             }
    2026             :         }
    2027             :         else
    2028             :         {
    2029       38773 :             pca_bits = 0;
    2030             :         }
    2031             : 
    2032       39671 :         pSpar_md_cfg->max_md_bits_spar = pSpar_md_cfg->max_bits_per_blk + agc_bits + pca_bits;
    2033             :     }
    2034             : 
    2035      148193 :     return;
    2036             : }
    2037             : 
    2038             : 
    2039             : /*-----------------------------------------------------------------------------------------*
    2040             :  * Function ivas_spar_bitrate_dist()
    2041             :  *
    2042             :  * Set SPAR bitrate distribution
    2043             :  *-----------------------------------------------------------------------------------------*/
    2044             : 
    2045      471263 : 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      471263 :     table_idx = ivas_get_spar_table_idx( ivas_total_brate, sba_order, bwidth, &bitlen, NULL );
    2058             : 
    2059      471263 :     nchan_transport = ivas_spar_br_table_consts[table_idx].nchan_transport;
    2060             : 
    2061      471263 :     sum_core_act_bits = 0;
    2062     2356315 :     for ( i = 0; i < nchan_transport; i++ )
    2063             :     {
    2064     1885052 :         core_bits_act[i] = (int16_t) ( ivas_spar_br_table_consts[table_idx].core_brs[i][0] / FRAMES_PER_SEC );
    2065             : 
    2066     1885052 :         sum_core_act_bits += core_bits_act[i];
    2067             :     }
    2068             : 
    2069      471263 :     residual_bits = nAvailBits - sum_core_act_bits;
    2070             : 
    2071             :     /* First compute core-coder bits as per bitrate distribution table and MD bitrate*/
    2072      471263 :     if ( residual_bits > 0 )
    2073             :     {
    2074       59249 :         for ( i = 0; i < nchan_transport; i++ )
    2075             :         {
    2076       59249 :             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       59249 :             core_bits_act[i] += min( residual_bits, core_range_bits[i] );
    2078       59249 :             residual_bits -= core_range_bits[i];
    2079             : 
    2080       59249 :             if ( residual_bits <= 0 )
    2081             :             {
    2082       38917 :                 break;
    2083             :             }
    2084             :         }
    2085             :     }
    2086             :     else
    2087             :     {
    2088     2161730 :         for ( i = 0; i < nchan_transport; i++ )
    2089             :         {
    2090     1729384 :             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      432346 :         overflow_bits = -residual_bits;
    2094             : 
    2095     2086642 :         for ( i = 0; i < nchan_transport; i++ )
    2096             :         {
    2097     1685075 :             core_bits_act[nchan_transport - 1 - i] -= min( overflow_bits, core_range_bits[nchan_transport - 1 - i] );
    2098     1685075 :             overflow_bits -= core_range_bits[nchan_transport - 1 - i];
    2099             : 
    2100     1685075 :             if ( overflow_bits <= 0 )
    2101             :             {
    2102       30779 :                 break;
    2103             :             }
    2104             :         }
    2105             : 
    2106      432346 :         if ( overflow_bits > 0 )
    2107             :         {
    2108             :             int16_t overflow_bits_ch;
    2109      401567 :             overflow_bits_ch = overflow_bits / nchan_transport;
    2110             : 
    2111     2007835 :             for ( i = 0; i < nchan_transport; i++ )
    2112             :             {
    2113     1606268 :                 core_bits_act[i] -= overflow_bits_ch;
    2114     1606268 :                 overflow_bits -= overflow_bits_ch;
    2115             :             }
    2116             : 
    2117      401567 :             core_bits_act[nchan_transport - 1] -= max( 0, overflow_bits );
    2118             :         }
    2119             :     }
    2120             : 
    2121     2356315 :     for ( i = 0; i < nchan_transport; i++ )
    2122             :     {
    2123     1885052 :         core_brates_act[i] = core_bits_act[i] * FRAMES_PER_SEC;
    2124             :     }
    2125             : 
    2126      471263 :     return;
    2127             : }

Generated by: LCOV version 1.14