LCOV - code coverage report
Current view: top level - lib_com - ivas_spar_com.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 665 706 94.2 %
Date: 2025-05-29 08:28:55 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     2603870 : int16_t ivas_get_bw_idx_from_sample_rate(
      97             :     const int32_t sampling_rate /* i  : sampling rate */
      98             : )
      99             : {
     100     2603870 :     int16_t bwidth = 0;
     101             : 
     102     2603870 :     switch ( sampling_rate )
     103             :     {
     104      384433 :         case 16000:
     105      384433 :             bwidth = WB;
     106      384433 :             break;
     107     1071628 :         case 32000:
     108     1071628 :             bwidth = SWB;
     109     1071628 :             break;
     110     1147809 :         case 48000:
     111     1147809 :             bwidth = FB;
     112     1147809 :             break;
     113           0 :         default:
     114           0 :             assert( !"Unsupported sample rate!" );
     115             :     }
     116             : 
     117     2603870 :     return bwidth;
     118             : }
     119             : 
     120             : 
     121             : /*-------------------------------------------------------------------------
     122             :  * ivas_spar_config()
     123             :  *
     124             :  * SPAR configuration function
     125             :  *------------------------------------------------------------------------*/
     126             : 
     127      131133 : 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      131133 :     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      131133 :         *nchan_transport = ivas_get_sba_num_TCs( ivas_total_brate, sba_order );
     151             :     }
     152             : 
     153      131133 :     *nCPE = ( *nchan_transport > 1 ) ? ( *nchan_transport + 1 ) >> 1 : 0;
     154      131133 :     *nSCE = max( 0, *nchan_transport - ( *nCPE << 1 ) );
     155             : 
     156      131133 :     if ( *nchan_transport == 1 )
     157             :     {
     158             :         /* map SPAR SID bitrate to SPAR active bitrate */
     159       43091 :         if ( ivas_total_brate == IVAS_SID_5k2 )
     160             :         {
     161           0 :             ivas_total_brate = IVAS_32k;
     162             :         }
     163       43091 :         assert( ivas_total_brate == IVAS_32k || ivas_total_brate == IVAS_24k4 || ivas_total_brate == IVAS_16k4 || ivas_total_brate == IVAS_13k2 );
     164       43091 :         if ( ivas_total_brate == IVAS_32k )
     165             :         {
     166        8611 :             *core_nominal_brate = ACELP_24k40;
     167             :         }
     168       34480 :         else if ( ivas_total_brate == IVAS_24k4 )
     169             :         {
     170        7840 :             *core_nominal_brate = ACELP_16k40;
     171             :         }
     172       26640 :         else if ( ivas_total_brate == IVAS_16k4 )
     173             :         {
     174       11488 :             *core_nominal_brate = ACELP_13k20;
     175             :         }
     176       15152 :         else if ( ivas_total_brate == IVAS_13k2 )
     177             :         {
     178       15152 :             *core_nominal_brate = ACELP_9k60;
     179             :         }
     180             :     }
     181             : 
     182      131133 :     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    18199543 : 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    18199543 :     int16_t table_idx = 0, ind1[IVAS_SPAR_BR_TABLE_LEN];
     202    18199543 :     int16_t i, j = 0, ind2 = -1;
     203             : 
     204   382190403 :     for ( i = 0; i < IVAS_SPAR_BR_TABLE_LEN; i++ )
     205             :     {
     206   363990860 :         ind1[j] = 0;
     207   363990860 :         if ( ( ivas_spar_br_table_consts[i].ivas_total_brate == ivas_total_brate ) &&
     208    27920117 :              ( ivas_spar_br_table_consts[i].sba_order == sba_order ) )
     209             :         {
     210    18199543 :             ind1[j++] = i;
     211             :         }
     212             :     }
     213             : 
     214    18199543 :     for ( i = 0; i < j; i++ )
     215             :     {
     216    18199543 :         if ( ivas_spar_br_table_consts[ind1[i]].bwidth == bwidth )
     217             :         {
     218    18199543 :             ind2 = i;
     219    18199543 :             break;
     220             :         }
     221             :     }
     222    18199543 :     assert( j > 0 );     /* to check if bitrate entry is present */
     223    18199543 :     assert( ind2 >= 0 ); /* to check if bw entry is present */
     224             : 
     225    18199543 :     table_idx = ind1[ind2];
     226             : 
     227    18199543 :     if ( ind != NULL )
     228             :     {
     229      108422 :         *ind = ind2;
     230             :     }
     231             : 
     232    18199543 :     if ( bitlen != NULL )
     233             :     {
     234      521465 :         *bitlen = ivas_get_bits_to_encode( j - 1 );
     235             :     }
     236             : 
     237    18199543 :     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    15063214 : 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    15063214 :     table_idx = ivas_get_spar_table_idx( ivas_total_brate, sba_order, SPAR_CONFIG_BW, NULL, NULL );
     256             : 
     257    15063214 :     nchan_transport = ivas_spar_br_table_consts[table_idx].nchan_transport;
     258             : 
     259    15063214 :     return nchan_transport;
     260             : }
     261             : 
     262             : 
     263             : /*-----------------------------------------------------------------------------------------*
     264             :  * Function ivas_get_pred_coeffs()
     265             :  *
     266             :  * Calculation of prediction coefficients
     267             :  *-----------------------------------------------------------------------------------------*/
     268             : 
     269    31754050 : 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    31754050 :     int16_t pred_dim = in_chans - 1;
     288             : 
     289    31754050 :     w_norm_fac = ( from_dirac == 1 ) ? 1.0f : 3.0f;
     290             : 
     291    31754050 :     if ( active_w == 0 )
     292             :     {
     293             :         float pPred_temp[IVAS_MAX_NUM_BANDS];
     294             : 
     295    23321257 :         set_zero( pPred_temp, IVAS_MAX_NUM_BANDS );
     296   116076417 :         for ( k = start_band; k < end_band; k++ )
     297             :         {
     298    92755160 :             div_factor[k] = max( 1e-20f, cov_real[0][0][k] );
     299    92755160 :             div_factor[k] = 1 / div_factor[k];
     300             :         }
     301             : 
     302    94252147 :         for ( i = 0; i < pred_dim; i++ )
     303             :         {
     304   360801798 :             for ( k = start_band; k < end_band; k++ )
     305             :             {
     306   289870908 :                 ppPred_coeffs_re[i][k] = cov_real[i + 1][0][k] * div_factor[k];
     307             : 
     308   289870908 :                 IVAS_CALCULATE_SQ_ABS_N( ppPred_coeffs_re[i][k], abs_value );
     309             : 
     310   289870908 :                 pPred_temp[k] += abs_value;
     311             :             }
     312             :         }
     313             : 
     314   116076417 :         for ( k = start_band; k < end_band; k++ )
     315             :         {
     316    92755160 :             pPred_temp[k] = sqrtf( pPred_temp[k] );
     317    92755160 :             div_factor[k] = max( 1.0f, pPred_temp[k] );
     318    92755160 :             div_factor[k] = 1 / div_factor[k];
     319             :         }
     320             : 
     321    94252147 :         for ( i = 0; i < pred_dim; i++ )
     322             :         {
     323   360801798 :             for ( k = start_band; k < end_band; k++ )
     324             :             {
     325   289870908 :                 ppPred_coeffs_re[i][k] = ppPred_coeffs_re[i][k] * div_factor[k];
     326   289870908 :                 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     8432793 :         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     8432793 :         if ( dyn_active_w_flag == 1 )
     339             :         {
     340           0 :             activew_quad_thresh = 1.0f;
     341             :         }
     342             :         else
     343             :         {
     344     8432793 :             activew_quad_thresh = IVAS_LIN_ACTIVEW_QUAD_ACTIVEW_THRESH;
     345             :         }
     346     8432793 :         g_th_sq = activew_quad_thresh * activew_quad_thresh;
     347             : 
     348     8432793 :         set_zero( dm_alpha, IVAS_MAX_NUM_BANDS );
     349             : 
     350    33731172 :         for ( i = 1; i < in_chans; i++ )
     351             :         {
     352   123464703 :             for ( k = start_band; k < end_band; k++ )
     353             :             {
     354    98166324 :                 IVAS_CALCULATE_SQ_ABS_N( cov_real[i][0][k], abs_value );
     355    98166324 :                 dm_alpha[k] += abs_value;
     356             :             }
     357             :         }
     358             : 
     359    41154901 :         for ( k = start_band; k < end_band; k++ )
     360             :         {
     361    32722108 :             dm_alpha[k] = sqrtf( dm_alpha[k] );
     362    32722108 :             div_factor[k] = max( dm_alpha[k], 1e-20f );
     363    32722108 :             div_factor[k] = 1 / div_factor[k];
     364             :         }
     365             : 
     366    33731172 :         for ( i = 0; i < pred_dim; i++ )
     367             :         {
     368   123464703 :             for ( k = start_band; k < end_band; k++ )
     369             :             {
     370    98166324 :                 dm_v_re[i][k] = cov_real[i + 1][0][k] * div_factor[k];
     371             :             }
     372             :         }
     373             : 
     374     8432793 :         if ( dtx_vad == 0 )
     375             :         {
     376       42149 :             dm_f_local = IVAS_ACTIVEW_DM_F_DTX;
     377             :         }
     378             :         else
     379             :         {
     380     8390644 :             dm_f_local = ( active_w_vlbr ) ? IVAS_ACTIVEW_DM_F_VLBR : IVAS_ACTIVEW_DM_F;
     381             :         }
     382             : 
     383    41154901 :         for ( b = start_band; b < end_band; b++ )
     384             :         {
     385    32722108 :             set_zero( real, pred_dim );
     386             : 
     387   130888432 :             for ( j = 0; j < pred_dim; j++ )
     388             :             {
     389   392665296 :                 for ( k = 1; k < in_chans; k++ )
     390             :                 {
     391             :                     float re;
     392             : 
     393   294498972 :                     IVAS_RMULT_FLOAT( cov_real[j + 1][k][b], dm_v_re[k - 1][b], re );
     394   294498972 :                     real[j] += re;
     395             :                 }
     396             :             }
     397    32722108 :             dm_beta_re = 0;
     398   130888432 :             for ( k = 0; k < pred_dim; k++ )
     399             :             {
     400             :                 float re;
     401    98166324 :                 IVAS_RMULT_FLOAT( real[k], dm_v_re[k][b], re );
     402    98166324 :                 dm_beta_re += re;
     403             :             }
     404             : 
     405    32722108 :             dm_w = cov_real[0][0][b];
     406    32722108 :             den_f = max( dm_w, 1e-20f );
     407    32722108 :             passive_g = dm_alpha[b] / den_f;
     408             : 
     409    32722108 :             if ( dyn_active_w_flag == 1 )
     410             :             {
     411           0 :                 dm_alpha[b] = 0.0f;
     412           0 :                 dm_w = 0.0f;
     413           0 :                 for ( i = 0; i < pred_dim; i++ )
     414             :                 {
     415           0 :                     dm_v_re[i][b] = 0.0f;
     416             :                 }
     417           0 :                 dm_v_re[res_ind - 1][b] = 1.0f;
     418           0 :                 passive_g = activew_quad_thresh;
     419             :             }
     420             : 
     421    32722108 :             if ( passive_g < activew_quad_thresh )
     422             :             {
     423             :                 /*linear activeW*/
     424    32721724 :                 dm_y = 0;
     425             : 
     426   130886896 :                 for ( k = 1; k < in_chans; k++ )
     427             :                 {
     428    98165172 :                     dm_y += cov_real[k][k][b];
     429             :                 }
     430    32721724 :                 den_f = max( dm_y, 1e-20f );
     431    32721724 :                 den_f = max( den_f, w_norm_fac * dm_w );
     432    32721724 :                 DM_F[b] = ( dm_f_local * dm_alpha[b] ) / den_f;
     433    32721724 :                 DM_F[b] = min( 1.0f, DM_F[b] );
     434             : 
     435    32721724 :                 den_f = dm_w + ( 2 * dm_alpha[b] * DM_F[b] ) + ( DM_F[b] * DM_F[b] * dm_beta_re );
     436    32721724 :                 den_f = max( den_f, 1e-20f );
     437             : 
     438    32721724 :                 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         384 :                 num_f = ( dm_beta_re - ( 2 * dm_alpha[b] * activew_quad_thresh ) );
     447             : 
     448         384 :                 sqrt_val = 4 * dm_alpha[b] * dm_alpha[b] * g_th_sq;
     449         384 :                 sqrt_val += dm_beta_re * dm_beta_re;
     450         384 :                 sqrt_val -= 4 * dm_beta_re * g_th_sq * dm_w;
     451         384 :                 sqrt_val = sqrtf( sqrt_val );
     452         384 :                 num_f += sqrt_val;
     453             : 
     454         384 :                 den_f = 2 * dm_beta_re * g_th_sq;
     455         384 :                 den_f = max( den_f, 1e-20f );
     456         384 :                 dm_g[b] = activew_quad_thresh;
     457         384 :                 DM_F[b] = ( dm_g[b] * num_f ) / den_f;
     458             :             }
     459             :         }
     460             : 
     461    33731172 :         for ( i = 0; i < pred_dim; i++ )
     462             :         {
     463   123464703 :             for ( b = start_band; b < end_band; b++ )
     464             :             {
     465    98166324 :                 ppPred_coeffs_re[i][b] = dm_v_re[i][b] * dm_g[b];
     466             : 
     467    98166324 :                 ppDM_Fv_re[i][b] = dm_v_re[i][b] * DM_F[b];
     468             :             }
     469             :         }
     470             :     }
     471             : 
     472    31754050 :     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    31754050 : 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   381048600 :     for ( ch = 0; ch < IVAS_SPAR_MAX_CH; ch++ )
     502             :     {
     503   349294550 :         set_zero( postpred_cov_re[ch], IVAS_SPAR_MAX_CH );
     504             :     }
     505             : 
     506    31754050 :     if ( dtx_vad == 0 )
     507             :     {
     508       80713 :         dm_f_local = IVAS_ACTIVEW_DM_F_SCALE_DTX;
     509             :     }
     510             :     else
     511             :     {
     512    31673337 :         dm_f_local = ( active_w_vlbr ) ? IVAS_ACTIVEW_DM_F_SCALE_VLBR : IVAS_ACTIVEW_DM_F_SCALE;
     513             :     }
     514             : 
     515   157231318 :     for ( b = start_band; b < end_band; b++ )
     516             :     {
     517   125477268 :         pWscale[b] = 1;
     518             : 
     519   125477268 :         if ( ( active_w == 1 ) && ( dyn_active_w_flag == 0 ) )
     520             :         {
     521    32722108 :             float Gw_sq, g_sq = 0;
     522             : 
     523    32722108 :             if ( num_ch != pNum_dmx[b * bands_bw] )
     524             :             {
     525    32722108 :                 ivas_calc_post_pred_per_band( cov_real, mixer_mat, num_ch, b, postpred_cov_re );
     526             :             }
     527             : 
     528    32722108 :             Gw_sq = cov_real[0][0][b] / max( postpred_cov_re[0][0], IVAS_FLT_EPS );
     529             : 
     530   130888432 :             for ( ch = 0; ch < num_ch - 1; ch++ )
     531             :             {
     532    98166324 :                 IVAS_CALCULATE_SQ_ABS_N( pred_coeffs_re[ch][b], abs_val );
     533             : 
     534    98166324 :                 g_sq += abs_val;
     535             :             }
     536             : 
     537    32722108 :             pWscale[b] = sqrtf( Gw_sq );
     538    32722108 :             pWscale[b] = pWscale[b] + sqrtf( Gw_sq + ( 4 * dm_f_local * g_sq ) );
     539    32722108 :             pWscale[b] *= 0.5f;
     540             :         }
     541             :     }
     542             : 
     543    31754050 :     return;
     544             : }
     545             : 
     546             : 
     547             : /*-----------------------------------------------------------------------------------------*
     548             :  * Function ivas_create_fullr_dmx_mat()
     549             :  *
     550             :  * Calculation of downmix matrix
     551             :  *-----------------------------------------------------------------------------------------*/
     552             : 
     553    34276393 : 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    34276393 :     nbands = end_band - start_band;
     571    34276393 :     remix_unmix_order = hMdCfg->remix_unmix_order;
     572             : 
     573    34276393 :     order = remix_order_set[remix_unmix_order];
     574             : 
     575   173316203 :     for ( i = 0; i < in_chans; i++ )
     576             :     {
     577   713480876 :         for ( j = 0; j < in_chans; j++ )
     578             :         {
     579   574441066 :             set_zero( &tmp_p1_re[i][j][start_band], nbands );
     580   574441066 :             set_zero( &tmp_p2_re[i][j][start_band], nbands );
     581   574441066 :             set_zero( &down_mix_mat1_re[i][j][start_band], nbands );
     582             :         }
     583             :     }
     584             : 
     585   173316203 :     for ( j = 0; j < in_chans; j++ )
     586             :     {
     587   744922754 :         for ( b = start_band; b < end_band; b++ )
     588             :         {
     589   605882944 :             tmp_p2_re[j][j][b] = 1.0f;
     590             :         }
     591             :     }
     592             : 
     593   139039810 :     for ( j = 1; j < in_chans; j++ )
     594             :     {
     595   564978339 :         for ( b = start_band; b < end_band; b++ )
     596             :         {
     597   460214922 :             tmp_p2_re[j][0][b] = -pred_coeffs_re[j - 1][b];
     598             :         }
     599             :     }
     600             : 
     601    34276393 :     if ( active_w == 1 )
     602             :     {
     603    46409110 :         for ( j = 0; j < in_chans; j++ )
     604             :         {
     605   192016160 :             for ( b = start_band; b < end_band; b++ )
     606             :             {
     607   154888872 :                 tmp_p1_re[j][j][b] = 1.0f;
     608             :             }
     609             :         }
     610             : 
     611    37127288 :         for ( j = 1; j < in_chans; j++ )
     612             :         {
     613   144012120 :             for ( b = start_band; b < end_band; b++ )
     614             :             {
     615   116166654 :                 tmp_p1_re[0][j][b] = dm_fv_re[j - 1][b];
     616             :             }
     617             :         }
     618             : 
     619             :         /* 4x4 mult */
     620    46409110 :         for ( i = 0; i < in_chans; i++ )
     621             :         {
     622   185636440 :             for ( j = 0; j < in_chans; j++ )
     623             :             {
     624   742545760 :                 for ( k = 0; k < in_chans; k++ )
     625             :                 {
     626  3072258560 :                     for ( b = start_band; b < end_band; b++ )
     627             :                     {
     628             :                         float tmp_re;
     629  2478221952 :                         IVAS_RMULT_FLOAT( tmp_p2_re[i][k][b], tmp_p1_re[k][j][b], tmp_re );
     630  2478221952 :                         down_mix_mat1_re[i][j][b] += tmp_re;
     631             :                     }
     632             :                 }
     633             :             }
     634             :         }
     635             :     }
     636             :     else
     637             :     {
     638   126907093 :         for ( j = 0; j < in_chans; j++ )
     639             :         {
     640   527844436 :             for ( k = 0; k < in_chans; k++ )
     641             :             {
     642  2449290114 :                 for ( b = start_band; b < end_band; b++ )
     643             :                 {
     644  2023358200 :                     down_mix_mat1_re[j][k][b] = tmp_p2_re[j][k][b];
     645             :                 }
     646             :             }
     647             :         }
     648             :     }
     649             : 
     650    34276393 :     if ( remix_unmix_order != 3 )
     651             :     {
     652    34276393 :         ivas_reorder_array( down_mix_mat1_re, in_chans, order, mixer_mat, start_band, end_band );
     653             :     }
     654             : 
     655    34276393 :     return;
     656             : }
     657             : 
     658             : 
     659             : /*-----------------------------------------------------------------------------------------*
     660             :  * Function ivas_reorder_array()
     661             :  *
     662             :  * reorders the input matrix based on order
     663             :  *-----------------------------------------------------------------------------------------*/
     664             : 
     665    34276393 : 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   173316203 :     for ( i = 0; i < in_chans; i++ )
     676             :     {
     677   139039810 :         idx = order[i];
     678             : 
     679   713480876 :         for ( j = 0; j < in_chans; j++ )
     680             :         {
     681  3217354754 :             for ( b = start_band; b < end_band; b++ )
     682             :             {
     683  2642913688 :                 mixer_mat[i][j][b] = in_re[idx][j][b];
     684             :             }
     685             :         }
     686             :     }
     687             : 
     688    34276393 :     return;
     689             : }
     690             : 
     691             : 
     692             : /*-----------------------------------------------------------------------------------------*
     693             :  * Function ivas_calc_post_pred_per_band()
     694             :  *
     695             :  * Calculate post pred mat per band
     696             :  *-----------------------------------------------------------------------------------------*/
     697             : 
     698   139714948 : 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   721785596 :     for ( i = 0; i < num_ch; i++ )
     711             :     {
     712  3129735152 :         for ( j = 0; j < num_ch; j++ )
     713             :         {
     714  2547664504 :             dmx_mat_conj[i][j] = mixer_mat[j][i][band_idx];
     715             :         }
     716             :     }
     717             : 
     718   721785596 :     for ( i = 0; i < num_ch; i++ )
     719             :     {
     720   582070648 :         set_zero( postpred_cov_re[i], num_ch );
     721             :     }
     722             : 
     723             :     /* num_ch x num_ch mult */
     724   721785596 :     for ( i = 0; i < num_ch; i++ )
     725             :     {
     726  3129735152 :         for ( j = 0; j < num_ch; j++ )
     727             :         {
     728  2547664504 :             temp_mat[i][j] = 0;
     729 14884569440 :             for ( k = 0; k < num_ch; k++ )
     730             :             {
     731 12336904936 :                 IVAS_RMULT_FLOAT( cov_real[i][k][band_idx], dmx_mat_conj[k][j], tmp_re );
     732 12336904936 :                 temp_mat[i][j] += tmp_re;
     733             :             }
     734             :         }
     735             :     }
     736             : 
     737             :     /* num_ch x num_ch mult */
     738   721785596 :     for ( i = 0; i < num_ch; i++ )
     739             :     {
     740  2146938224 :         for ( j = i; j < num_ch; j++ )
     741             :         {
     742  9007152296 :             for ( k = 0; k < num_ch; k++ )
     743             :             {
     744  7442284720 :                 IVAS_RMULT_FLOAT( mixer_mat[i][k][band_idx], temp_mat[k][j], tmp_re );
     745  7442284720 :                 postpred_cov_re[i][j] += tmp_re;
     746             :             }
     747             :         }
     748             :     }
     749             : 
     750   721785596 :     for ( i = 0; i < num_ch; i++ )
     751             :     {
     752  1564867576 :         for ( j = 0; j < i; j++ )
     753             :         {
     754   982796928 :             postpred_cov_re[i][j] = postpred_cov_re[j][i];
     755             :         }
     756             :     }
     757             : 
     758   139714948 :     return;
     759             : }
     760             : 
     761             : 
     762             : /*-----------------------------------------------------------------------------------------*
     763             :  * Function ivas_calc_p_coeffs_per_band()
     764             :  *
     765             :  * Calculate P coeffs per band
     766             :  *-----------------------------------------------------------------------------------------*/
     767             : 
     768    95730900 : 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    95730900 :     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    95730900 :     float trace = 0;
     782    95730900 :     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    95730900 :     b_ts_idx = band_idx + i_ts * IVAS_MAX_NUM_BANDS;
     788             : 
     789    95730900 :     if ( num_dmx != num_ch )
     790             :     {
     791    95730900 :         set_zero( pSparMd->band_coeffs[b_ts_idx].P_re, IVAS_SPAR_MAX_CH - 1 );
     792   765847200 :         for ( i = 0; i < IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS; i++ )
     793             :         {
     794   670116300 :             set_zero( recon_uu_re[i], IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS );
     795             :         }
     796             : 
     797   300011568 :         for ( i = num_dmx; i < num_ch; i++ )
     798             :         {
     799   724581992 :             for ( j = num_dmx; j < num_ch; j++ )
     800             :             {
     801   520301324 :                 cov_uu_re[i - num_dmx][j - num_dmx] = postpred_cov_re[i][j];
     802             :             }
     803             :         }
     804             : 
     805    95730900 :         if ( dtx_vad == 1 )
     806             :         {
     807   189843354 :             for ( i = 1; i < num_dmx; i++ )
     808             :             {
     809   256810516 :                 for ( j = 1; j < num_dmx; j++ )
     810             :                 {
     811   162425898 :                     cov_dd_re[i - 1][j - 1] = postpred_cov_re[i][j];
     812             :                 }
     813             :             }
     814             : 
     815    95458736 :             if ( num_dmx == 2 )
     816             :             {
     817             :                 float re1, re2;
     818             : 
     819    33879254 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[0][0], cov_dd_re[0][0], re1 );
     820    33879254 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[1][0], cov_dd_re[0][0], re2 );
     821    33879254 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[0][0], re1, recon_uu_re[0][0] );
     822    33879254 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[1][0], re1, recon_uu_re[0][1] );
     823    33879254 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[0][0], re2, recon_uu_re[1][0] );
     824    33879254 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[1][0], re2, recon_uu_re[1][1] );
     825             : 
     826   101637762 :                 for ( i = 0; i < 2; i++ )
     827             :                 {
     828   203275524 :                     for ( j = 0; j < 2; j++ )
     829             :                     {
     830   135517016 :                         cov_uu_re[i][j] -= recon_uu_re[i][j];
     831             :                     }
     832             :                 }
     833             :             }
     834    61579482 :             else if ( num_dmx == 3 )
     835             :             {
     836             :                 float re1[2], re2;
     837    26484724 :                 set_f( re1, 0, 2 );
     838             : 
     839    79454172 :                 for ( j = 0; j < 2; j++ )
     840             :                 {
     841   158908344 :                     for ( k = 0; k < 2; k++ )
     842             :                     {
     843             :                         float re;
     844             : 
     845   105938896 :                         IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[0][k], cov_dd_re[k][j], re );
     846   105938896 :                         re1[j] += re;
     847             :                     }
     848             :                 }
     849             : 
     850    26484724 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[0][0], re1[0], re2 );
     851    26484724 :                 recon_uu_re[0][0] = re2;
     852    26484724 :                 IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[0][1], re1[1], re2 );
     853    26484724 :                 recon_uu_re[0][0] += re2;
     854             : 
     855    26484724 :                 cov_uu_re[0][0] -= recon_uu_re[0][0];
     856             :             }
     857    35094758 :             else if ( num_dmx == 4 )
     858             :             {
     859             :                 /* Step 1: Multiply C * cov_dd * C' */
     860             :                 float re1[3], re;
     861             : 
     862    14117400 :                 for ( i = 0; i < num_ch - num_dmx; i++ )
     863             :                 {
     864    11605428 :                     set_f( re1, 0, 3 );
     865    46421712 :                     for ( m = 0; m < num_dmx - 1; m++ )
     866             :                     {
     867   139265136 :                         for ( k = 0; k < num_dmx - 1; k++ )
     868             :                         {
     869   104448852 :                             IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[i][k], cov_dd_re[k][m], re );
     870   104448852 :                             re1[m] += re;
     871             :                         }
     872             :                     }
     873    74874672 :                     for ( j = 0; j < num_ch - num_dmx; j++ )
     874             :                     {
     875   253076976 :                         for ( m = 0; m < num_dmx - 1; m++ )
     876             :                         {
     877   189807732 :                             IVAS_RMULT_FLOAT( pSparMd->band_coeffs[b_ts_idx].C_re[j][m], re1[m], re );
     878   189807732 :                             recon_uu_re[i][j] += re;
     879             :                         }
     880             :                     }
     881             :                 }
     882             : 
     883             :                 /* Step 2: cov_uu - recon_uu */
     884    14117400 :                 for ( i = 0; i < num_ch - num_dmx; i++ )
     885             :                 {
     886    74874672 :                     for ( j = 0; j < num_ch - num_dmx; j++ )
     887             :                     {
     888    63269244 :                         cov_uu_re[i][j] -= recon_uu_re[i][j];
     889             :                     }
     890             :                 }
     891             :             }
     892             :         }
     893             : 
     894    95730900 :         p_norm_scaling = IVAS_P_NORM_SCALING;
     895             : 
     896    95730900 :         if ( ( dtx_vad == 0 ) && ( num_dmx == 1 ) )
     897             :         {
     898      139322 :             p_norm_scaling = IVAS_P_NORM_SCALING_DTX;
     899             :         }
     900             : 
     901    95730900 :         trace = 0.0f;
     902             : 
     903   300011568 :         for ( i = num_dmx; i < num_ch; i++ )
     904             :         {
     905             :             float tmp_out;
     906   204280668 :             IVAS_CALCULATE_RABS( cov_uu_re[i - num_dmx][i - num_dmx], tmp_out );
     907   204280668 :             trace += tmp_out;
     908             :         }
     909             : 
     910    95730900 :         factor = max( 1e-20f, postpred_cov_re[0][0] );
     911    95730900 :         factor = max( factor, ( p_norm_scaling * trace ) );
     912    95730900 :         factor = 1 / factor;
     913             : 
     914             :         /* normalise Hermitian (except for rounding) cov_uu */
     915   300011568 :         for ( i = num_dmx; i < num_ch; i++ )
     916             :         {
     917   724581992 :             for ( j = num_dmx; j < num_ch; j++ )
     918             :             {
     919   520301324 :                 if ( i == j )
     920             :                 {
     921             :                     /* force diagonal to be real */
     922   204280668 :                     cov_uu_re[i - num_dmx][j - num_dmx] *= factor;
     923             :                 }
     924             :                 else
     925             :                 {
     926             :                     /* set off-diag elements to zero */
     927   316020656 :                     cov_uu_re[i - num_dmx][j - num_dmx] = 0;
     928             :                 }
     929             :             }
     930             :         }
     931             : 
     932             :         /* take sqrt of max of diags and zero */
     933   300011568 :         for ( i = num_dmx; i < num_ch; i++ )
     934             :         {
     935   204280668 :             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   300011568 :         for ( i = num_dmx; i < num_ch; i++ )
     941             :         {
     942   724581992 :             for ( j = num_dmx; j < num_ch; j++ )
     943             :             {
     944   520301324 :                 if ( i == j )
     945             :                 {
     946   204280668 :                     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    95730900 :     return;
     953             : }
     954             : 
     955             : 
     956             : /*-----------------------------------------------------------------------------------------*
     957             :  * Function ivas_calc_c_coeffs_per_band()
     958             :  *
     959             :  * Calculate C coeffs per band
     960             :  *-----------------------------------------------------------------------------------------*/
     961             : 
     962    74270732 : 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    74270732 :     float trace_cov_dd_re = 0;
     978             :     float abs_trace;
     979             :     float re;
     980             :     int16_t b_ts_idx;
     981             : 
     982    74270732 :     b_ts_idx = band_idx + i_ts * IVAS_MAX_NUM_BANDS;
     983             : 
     984    74270732 :     if ( dtx_vad == 0 )
     985             :     {
     986      143906 :         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      143906 :         return;
     988             :     }
     989             : 
     990   205413906 :     for ( i = num_dmx; i < num_ch; i++ )
     991             :     {
     992   339125412 :         for ( j = 1; j < num_dmx; j++ )
     993             :         {
     994   207838332 :             cov_ud_re[i - num_dmx][j - 1] = postpred_cov_re[i][j];
     995             :         }
     996             :     }
     997             : 
     998   188431080 :     for ( i = 1; i < num_dmx; i++ )
     999             :     {
    1000   319011252 :         for ( j = 1; j < num_dmx; j++ )
    1001             :         {
    1002   204706998 :             cov_dd_re[i - 1][j - 1] = postpred_cov_re[i][j];
    1003             :         }
    1004             :     }
    1005             : 
    1006   188431080 :     for ( i = 0; i < num_dmx - 1; i++ )
    1007             :     {
    1008   114304254 :         trace_cov_dd_re += cov_dd_re[i][i];
    1009             :     }
    1010    74126826 :     trace_cov_dd_re *= 0.005f;
    1011             : 
    1012    74126826 :     IVAS_CALCULATE_RABS( trace_cov_dd_re, abs_trace );
    1013             : 
    1014    74126826 :     if ( abs_trace < IVAS_FLT_EPS )
    1015             :     {
    1016             :         /* protection from cases when variance of residual channels is very small */
    1017     3260585 :         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   180451505 :         for ( i = 0; i < num_dmx - 1; i++ )
    1022             :         {
    1023   109585264 :             cov_dd_re[i][i] += trace_cov_dd_re;
    1024             :         }
    1025    70866241 :         if ( ivas_is_mat_inv( cov_dd_re, num_dmx - 1 ) && ( num_dmx < FOA_CHANNELS ) )
    1026             :         {
    1027        4066 :             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    70862175 :             ivas_calc_mat_inv( cov_dd_re, num_dmx - 1, cov_dd_re_inv );
    1032             : 
    1033   196843761 :             for ( i = 0; i < num_ch - num_dmx; i++ )
    1034             :             {
    1035   326723685 :                 for ( j = 0; j < num_dmx - 1; j++ )
    1036             :                 {
    1037   200742099 :                     pSparMd->band_coeffs[b_ts_idx].C_re[i][j] = 0;
    1038   596949610 :                     for ( k = 0; k < num_dmx - 1; k++ )
    1039             :                     {
    1040   396207511 :                         IVAS_RMULT_FLOAT( cov_ud_re[i][k], cov_dd_re_inv[k][j], re );
    1041   396207511 :                         pSparMd->band_coeffs[b_ts_idx].C_re[i][j] += re;
    1042             :                     }
    1043             :                 }
    1044             :             }
    1045             :         }
    1046             :     }
    1047             : 
    1048    74126826 :     return;
    1049             : }
    1050             : 
    1051             : 
    1052             : /*-----------------------------------------------------------------------------------------*
    1053             :  * Function ivas_calc_c_p_coeffs()
    1054             :  *
    1055             :  * Calculation of C and P coeffs
    1056             :  *-----------------------------------------------------------------------------------------*/
    1057             : 
    1058   106992840 : 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   106992840 :     if ( num_dmx != num_ch )
    1074             :     {
    1075   106992840 :         ivas_calc_post_pred_per_band( cov_real, mixer_mat, num_ch, band_idx, postpred_cov_re );
    1076             : 
    1077   106992840 :         if ( num_dmx != 1 )
    1078             :         {
    1079    74270732 :             ivas_calc_c_coeffs_per_band( pSparMd, i_ts, postpred_cov_re, num_ch, num_dmx, band_idx, dtx_vad );
    1080             :         }
    1081             : 
    1082   106992840 :         if ( dyn_active_w_flag )
    1083             :         {
    1084           0 :             for ( i = 0; i < num_ch - num_dmx; i++ )
    1085             :             {
    1086           0 :                 for ( j = 0; j < num_dmx - 1; j++ )
    1087             :                 {
    1088           0 :                     pSparMd->band_coeffs[band_idx + i_ts * IVAS_MAX_NUM_BANDS].C_re[i][j] = 0.0f;
    1089             :                 }
    1090             :             }
    1091             :         }
    1092   106992840 :         if ( compute_p_flag == 1 )
    1093             :         {
    1094    95730900 :             ivas_calc_p_coeffs_per_band( pSparMd, i_ts, postpred_cov_re, num_ch, dtx_vad, num_dmx, band_idx );
    1095             :         }
    1096             : 
    1097   106992840 :         if ( dyn_active_w_flag )
    1098             :         {
    1099           0 :             for ( i = num_dmx; i < num_ch; i++ )
    1100             :             {
    1101           0 :                 pSparMd->band_coeffs[band_idx + i_ts * IVAS_MAX_NUM_BANDS].P_re[i - num_dmx] = 0;
    1102             :             }
    1103             :         }
    1104             :     }
    1105             : 
    1106   106992840 :     return;
    1107             : }
    1108             : 
    1109             : 
    1110   149176518 : 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   149176518 :     if ( dim == IVAS_MAT_DIM_3 )
    1116             :     {
    1117             :         double re1, re2, re;
    1118     9898830 :         IVAS_RMULT_DOUBLE( in_re[1][1], in_re[2][2], re1 );
    1119     9898830 :         IVAS_RMULT_DOUBLE( in_re[1][2], in_re[2][1], re2 );
    1120     9898830 :         re = re1 - re2;
    1121             : 
    1122     9898830 :         IVAS_RMULT_DOUBLE( in_re[0][0], re, re1 );
    1123             : 
    1124     9898830 :         *det_re = re1;
    1125             : 
    1126     9898830 :         IVAS_RMULT_DOUBLE( in_re[1][0], in_re[2][2], re1 );
    1127     9898830 :         IVAS_RMULT_DOUBLE( in_re[1][2], in_re[2][0], re2 );
    1128     9898830 :         re = re1 - re2;
    1129             : 
    1130     9898830 :         IVAS_RMULT_DOUBLE( in_re[0][1], re, re1 );
    1131             : 
    1132     9898830 :         *det_re -= re1;
    1133             : 
    1134     9898830 :         IVAS_RMULT_DOUBLE( in_re[1][0], in_re[2][1], re1 );
    1135     9898830 :         IVAS_RMULT_DOUBLE( in_re[1][1], in_re[2][0], re2 );
    1136     9898830 :         re = re1 - re2;
    1137             : 
    1138     9898830 :         IVAS_RMULT_DOUBLE( in_re[0][2], re, re1 );
    1139             : 
    1140     9898830 :         *det_re += re1;
    1141             :     }
    1142   139277688 :     else if ( dim == IVAS_MAT_DIM_2 )
    1143             :     {
    1144             :         double re1, re2;
    1145   102181055 :         IVAS_RMULT_DOUBLE( in_re[0][0], in_re[1][1], re1 );
    1146   102181055 :         IVAS_RMULT_DOUBLE( in_re[0][1], in_re[1][0], re2 );
    1147   102181055 :         *det_re = re1 - re2;
    1148             :     }
    1149    37096633 :     else if ( dim == IVAS_MAT_DIM_1 )
    1150             :     {
    1151    37096633 :         *det_re = in_re[0][0];
    1152             :     }
    1153             :     else
    1154             :     {
    1155           0 :         assert( !"matrix dimention not supported!" );
    1156             :     }
    1157             : 
    1158   149176518 :     return;
    1159             : }
    1160             : 
    1161             : 
    1162             : /*-----------------------------------------------------------------------------------------*
    1163             :  * Function ivas_get_mat_cofactor()
    1164             :  *
    1165             :  * Calculate cofactor for invert matrix
    1166             :  *-----------------------------------------------------------------------------------------*/
    1167             : 
    1168    44544735 : 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    44544735 :     int16_t r = 0, c = 0;
    1176             : 
    1177   178178940 :     for ( i = 0; i < MAX_MAT_DIM; i++ )
    1178             :     {
    1179   534536820 :         for ( j = 0; j < MAX_MAT_DIM; j++ )
    1180             :         {
    1181   400902615 :             if ( i != row && j != col )
    1182             :             {
    1183             : 
    1184   178178940 :                 out_re[r][c++] = in_re[i][j];
    1185             :             }
    1186             :         }
    1187   133634205 :         if ( c == 2 )
    1188             :         {
    1189    89089470 :             r++;
    1190    89089470 :             c = 0;
    1191             :         }
    1192             :     }
    1193             : 
    1194    44544735 :     return;
    1195             : }
    1196             : 
    1197             : 
    1198             : /*-----------------------------------------------------------------------------------------*
    1199             :  * Function ivas_calc_mat_inv()
    1200             :  *
    1201             :  * Calculate Invert of a matrix
    1202             :  *-----------------------------------------------------------------------------------------*/
    1203             : 
    1204    70862175 : 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   180439307 :     for ( i = 0; i < dim; i++ )
    1215             :     {
    1216   306483008 :         for ( j = 0; j < dim; j++ )
    1217             :         {
    1218   196905876 :             dbl_in_re[i][j] = in_re[i][j];
    1219             :         }
    1220             :     }
    1221             : 
    1222    70862175 :     if ( dim == IVAS_MAT_DIM_1 )
    1223             :     {
    1224    37096633 :         det = ( dbl_in_re[0][0] * dbl_in_re[0][0] );
    1225             :         /* assert to catch cases when input is singular matrix*/
    1226    37096633 :         assert( det > 0 );
    1227    37096633 :         det = 1 / det;
    1228    37096633 :         dbl_out_re[0][0] = dbl_in_re[0][0] * det;
    1229             :     }
    1230    33765542 :     else if ( dim == IVAS_MAT_DIM_2 )
    1231             :     {
    1232             :         double det_re;
    1233             :         double re;
    1234             : 
    1235    28816127 :         ivas_calc_mat_det( dbl_in_re, dim, &det_re );
    1236    28816127 :         det = ( det_re * det_re );
    1237             :         /* assert to catch cases when input is singular matrix*/
    1238    28816127 :         assert( det > 0 );
    1239    28816127 :         det = 1 / det;
    1240             : 
    1241    28816127 :         IVAS_RMULT_DOUBLE( det_re, dbl_in_re[1][1], re );
    1242    28816127 :         dbl_out_re[0][0] = re * det;
    1243             : 
    1244    28816127 :         IVAS_RMULT_DOUBLE( det_re, dbl_in_re[0][1], re );
    1245    28816127 :         dbl_out_re[0][1] = -re * det;
    1246             : 
    1247    28816127 :         IVAS_RMULT_DOUBLE( det_re, dbl_in_re[1][0], re );
    1248    28816127 :         dbl_out_re[1][0] = -re * det;
    1249             : 
    1250    28816127 :         IVAS_RMULT_DOUBLE( det_re, dbl_in_re[0][0], re );
    1251    28816127 :         dbl_out_re[1][1] = re * det;
    1252             :     }
    1253     4949415 :     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     4949415 :         int16_t sign = 1;
    1258             : 
    1259     4949415 :         ivas_calc_mat_det( dbl_in_re, dim, &det_re );
    1260     4949415 :         det = det_re > 0 ? 1 / max( IVAS_DBL_EPS, det_re ) : 1 / min( det_re, -IVAS_DBL_EPS );
    1261             : 
    1262    19797660 :         for ( i = 0; i < dim; i++ )
    1263             :         {
    1264    59392980 :             for ( j = 0; j < dim; j++ )
    1265             :             {
    1266    44544735 :                 ivas_get_mat_cofactor( dbl_in_re, fac_re, i, j );
    1267    44544735 :                 ivas_calc_mat_det( fac_re, IVAS_MAT_DIM_2, &dbl_out_re[j][i] );
    1268    44544735 :                 dbl_out_re[j][i] = dbl_out_re[j][i] * det * sign;
    1269             : 
    1270    44544735 :                 if ( ( ( i + j ) & 1 ) == 0 )
    1271             :                 {
    1272    24747075 :                     sign = -1;
    1273             :                 }
    1274             :                 else
    1275             :                 {
    1276    19797660 :                     sign = 1;
    1277             :                 }
    1278             :             }
    1279             :         }
    1280             :     }
    1281             :     else
    1282             :     {
    1283           0 :         assert( !"matrix dimension not supported!" );
    1284             :     }
    1285             : 
    1286   180439307 :     for ( i = 0; i < dim; i++ )
    1287             :     {
    1288   306483008 :         for ( j = 0; j < dim; j++ )
    1289             :         {
    1290   196905876 :             out_re[i][j] = (float) dbl_out_re[i][j];
    1291             :         }
    1292             :     }
    1293             : 
    1294    70862175 :     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    70866241 : static int16_t ivas_is_mat_inv(
    1305             :     float in_re[MAX_MAT_DIM][MAX_MAT_DIM],
    1306             :     const int16_t dim )
    1307             : {
    1308    70866241 :     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   180451505 :     for ( i = 0; i < dim; i++ )
    1314             :     {
    1315   306507404 :         for ( j = 0; j < dim; j++ )
    1316             :         {
    1317   196922140 :             dbl_in_re[i][j] = in_re[i][j];
    1318             :         }
    1319             :     }
    1320             : 
    1321    70866241 :     ivas_calc_mat_det( dbl_in_re, dim, &det_re );
    1322             : 
    1323    70866241 :     det = ( det_re * det_re );
    1324             : 
    1325    70866241 :     if ( det < IVAS_DBL_EPS )
    1326             :     {
    1327        4117 :         is_det_zero = 1;
    1328             :     }
    1329             : 
    1330    70866241 :     return is_det_zero;
    1331             : }
    1332             : 
    1333             : 
    1334             : /*-----------------------------------------------------------------------------------------*
    1335             :  * Function ivas_compute_spar_params()
    1336             :  *
    1337             :  *
    1338             :  *-----------------------------------------------------------------------------------------*/
    1339             : 
    1340    31754050 : 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    31754050 :     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    31754050 :     ivas_create_fullr_dmx_mat( pred_coeffs_re, dm_fv_re, mixer_mat, num_ch, start_band, end_band, active_w, hSparCfg );
    1364             : 
    1365    31754050 :     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   157231318 :     for ( b = start_band; b < end_band; b++ )
    1368             :     {
    1369   125477268 :         float onebyscale = 1.0f / pWscale[b];
    1370   513514500 :         for ( i = 0; i < num_ch - 1; i++ )
    1371             :         {
    1372   388037232 :             hSparMd->band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].pred_re[i] = pred_coeffs_re[i][b] * onebyscale;
    1373             :         }
    1374             : 
    1375   638991768 :         for ( i = 0; i < num_ch; i++ )
    1376             :         {
    1377   513514500 :             mixer_mat[0][i][b] *= pWscale[b];
    1378             :         }
    1379             :     }
    1380             : 
    1381   157231318 :     for ( b = start_band; b < end_band; b++ )
    1382             :     {
    1383   125477268 :         ndm = hSparCfg->num_dmx_chans_per_band[b * bands_bw];
    1384             : 
    1385   125477268 :         if ( ndm != num_ch )
    1386             :         {
    1387    95730900 :             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    31754050 :     return;
    1414             : }
    1415             : 
    1416             : 
    1417             : /*-----------------------------------------------------------------------------------------*
    1418             :  * Function ivas_get_spar_md_from_dirac()
    1419             :  *
    1420             :  *
    1421             :  *-----------------------------------------------------------------------------------------*/
    1422             : 
    1423    12468807 : 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    12468807 :     remix_order = remix_order_set[hSpar_md_cfg->remix_unmix_order];
    1459             : 
    1460    12468807 :     num_ch = ivas_sba_get_nchan_metadata( order, IVAS_256k /*dummy value as order is always 1 in this function*/ );
    1461             : 
    1462    12468807 :     hoa2_ch = ivas_sba_get_nchan_metadata( SBA_HOA2_ORDER, IVAS_256k /*dummy value as order is always 1 in this function*/ );
    1463             : 
    1464    12468807 :     foa_ch = FOA_CHANNELS;
    1465    12468807 :     diff_norm_order1 = 3.0f;
    1466    12468807 :     diff_norm_order2 = 5.0f;
    1467    12468807 :     diff_norm_order3 = 7.0f;
    1468             : 
    1469   149625684 :     for ( i = 0; i < IVAS_MAX_FB_MIXER_OUT_CH; i++ )
    1470             :     {
    1471  1645882524 :         for ( j = 0; j < IVAS_MAX_SPAR_FB_MIXER_IN_CH; j++ )
    1472             :         {
    1473  1508725647 :             pMixer_mat[i][j] = mixer_mat_local[i][j];
    1474             :         }
    1475   137156877 :         ppMixer_mat[i] = pMixer_mat[i];
    1476             :     }
    1477             : 
    1478    12468807 :     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     6423688 :         ndm = hSpar_md_cfg->num_dmx_chans_per_band[start_band - 1];
    1484     6423688 :         P_norm[0] = 0.0f;
    1485    22118742 :         for ( i = 0; i < max( 0, foa_ch - ndm ); i++ )
    1486             :         {
    1487    15695054 :             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     6423688 :         P_norm[0] *= diff_norm_order1 / min( diff_norm_order1, max( 0, foa_ch - ndm ) );
    1490             : 
    1491     6423688 :         P_norm[1] = 0.0f;
    1492     6423688 :         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     6423688 :         P_norm[1] *= diff_norm_order2 / min( diff_norm_order2, max( 0, min( num_ch, hoa2_ch ) - ndm ) );
    1497             : 
    1498     6423688 :         P_norm[2] = 0.0f;
    1499     6423688 :         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     6423688 :         P_norm[2] *= diff_norm_order3 / min( diff_norm_order3, max( 0, num_ch - ndm ) );
    1504             : 
    1505    22118742 :         for ( i = 0; i < max( 0, foa_ch - ndm ); i++ )
    1506             :         {
    1507    15695054 :             idx = remix_order[i + ndm] - ndm;
    1508    15695054 :             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    15695054 :             P_dir_fact[idx] = P_dir_fact[idx] / max( IVAS_FLT_EPS, P_norm[0] );
    1510             :         }
    1511     6423688 :         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     6423688 :         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    41711931 :     for ( int16_t i_ts = 0; i_ts < n_ts; i_ts++ )
    1526             :     {
    1527   134607278 :         for ( band = start_band; band < end_band; band++ )
    1528             :         {
    1529   105364154 :             ndm = hSpar_md_cfg->num_dmx_chans_per_band[band];
    1530             : 
    1531             :             /*SPAR from DirAC*/
    1532   105364154 :             set_f( response_avg, 0.0f, MAX_OUTPUT_CHANNELS );
    1533             : 
    1534   105364154 :             if ( n_ts > 1 )
    1535             :             {
    1536    79326472 :                 ivas_dirac_dec_get_response( (int16_t) azi_dirac[band][i_ts], (int16_t) ele_dirac[band][i_ts], response_avg, order );
    1537             :             }
    1538    26037682 :             else if ( useLowerRes )
    1539             :             {
    1540     2282070 :                 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   118778060 :                 for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; block++ )
    1545             :                 {
    1546    95022448 :                     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    23755612 :                     num_ch_order = ivas_sba_get_nchan( order, 0 );
    1555    23755612 :                     hoa2_ch_order = ivas_sba_get_nchan( SBA_HOA2_ORDER, 0 );
    1556             : 
    1557   118778060 :                     for ( ch = 0; ch < num_ch_order; ch++ )
    1558             :                     {
    1559   475112240 :                         for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; block++ )
    1560             :                         {
    1561   380089792 :                             response_avg[ch] += response[block][ch];
    1562             :                         }
    1563    95022448 :                         response_avg[ch] /= MAX_PARAM_SPATIAL_SUBFRAMES;
    1564             :                     }
    1565             : 
    1566             :                     /*normalize 1st order*/
    1567    23755612 :                     norm = 0.0f;
    1568    95022448 :                     for ( ch = 1; ch < foa_ch; ch++ )
    1569             :                     {
    1570    71266836 :                         norm += response_avg[ch] * response_avg[ch];
    1571             :                     }
    1572    23755612 :                     norm = max( EPSILON, sqrtf( norm ) );
    1573    95022448 :                     for ( ch = 1; ch < foa_ch; ch++ )
    1574             :                     {
    1575    71266836 :                         response_avg[ch] /= norm;
    1576             :                     }
    1577             : 
    1578             :                     /*normalize 2nd order*/
    1579    23755612 :                     norm = 0.0f;
    1580    23755612 :                     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    23755612 :                     norm = max( EPSILON, sqrtf( norm ) );
    1585    23755612 :                     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    23755612 :                     norm = 0.0f;
    1592    23755612 :                     for ( ch = hoa2_ch_order; ch < num_ch_order; ch++ )
    1593             :                     {
    1594           0 :                         norm += response_avg[ch] * response_avg[ch];
    1595             :                     }
    1596    23755612 :                     norm = max( EPSILON, sqrtf( norm ) );
    1597    23755612 :                     for ( ch = hoa2_ch_order; ch < num_ch_order; ch++ )
    1598             :                     {
    1599           0 :                         response_avg[ch] /= norm;
    1600             :                     }
    1601             :                 }
    1602             :             }
    1603             : 
    1604   105364154 :             for ( i = FOA_CHANNELS + 1; i < num_ch; i++ )
    1605             :             {
    1606           0 :                 response_avg[i] = response_avg[HOA_keep_ind[i]];
    1607             :             }
    1608             : 
    1609   105364154 :             en_ratio_fac = ( 1.0f - diffuseness[band] );
    1610             : 
    1611   526820770 :             for ( i = 0; i < num_ch; i++ )
    1612             :             {
    1613  2107283080 :                 for ( j = 0; j < num_ch; j++ )
    1614             :                 {
    1615  1685826464 :                     if ( i == j )
    1616             :                     {
    1617   421456616 :                         if ( i == 0 )
    1618             :                         {
    1619   105364154 :                             cov_real_dirac[i][i][band] = 1.0f;
    1620             :                         }
    1621             :                         else
    1622             :                         {
    1623   316092462 :                             cov_real_dirac[i][j][band] = en_ratio_fac * response_avg[i] * response_avg[j];
    1624             : 
    1625   316092462 :                             if ( hSpar_md_cfg->nchan_transport <= 2 )
    1626             :                             {
    1627             : 
    1628   167119746 :                                 cov_real_dirac[i][j][band] *= en_ratio_fac;
    1629   167119746 :                                 if ( ( i >= ndm ) && ( dtx_vad == 1 ) )
    1630             :                                 {
    1631   137581470 :                                     cov_real_dirac[i][j][band] += ( 1.0f - ( en_ratio_fac * en_ratio_fac ) ) * P_dir_fact[i - ndm];
    1632             :                                 }
    1633             :                                 else
    1634             :                                 {
    1635    29538276 :                                     if ( i < foa_ch )
    1636             :                                     {
    1637    29538276 :                                         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   148972716 :                                 if ( i < foa_ch )
    1652             :                                 {
    1653   148972716 :                                     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  1264369848 :                         cov_real_dirac[i][j][band] = en_ratio_fac * response_avg[i] * response_avg[j];
    1669             :                     }
    1670             :                 }
    1671             :             }
    1672             :         }
    1673             : 
    1674   146215620 :         for ( i = 0; i < num_ch; i++ )
    1675             :         {
    1676   584862480 :             for ( j = 0; j < num_ch; j++ )
    1677             :             {
    1678   467889984 :                 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    29243124 :         active_w = ( dyn_active_w_flag == 1 ) || ( hSpar_md_cfg->active_w == 1 );
    1707             : 
    1708    29243124 :         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    29243124 :         if ( mixer_mat != NULL )
    1711             :         {
    1712    12545605 :             for ( band = start_band; band < end_band; band++ )
    1713             :             {
    1714    10128932 :                 ndm = hSpar_md_cfg->num_dmx_chans_per_band[band];
    1715             : 
    1716    33621928 :                 for ( i = 0; i < ndm; i++ )
    1717             :                 {
    1718   117464980 :                     for ( j = 0; j < num_ch; j++ )
    1719             :                     {
    1720    93971984 :                         mixer_mat[i][j][band + i_ts * IVAS_MAX_NUM_BANDS] = ppMixer_mat[i][j][band];
    1721             :                     }
    1722             :                 }
    1723             : 
    1724    27151664 :                 for ( i = ndm; i < num_ch; i++ )
    1725             :                 {
    1726    85113660 :                     for ( j = 0; j < num_ch; j++ )
    1727             :                     {
    1728    68090928 :                         mixer_mat[i][j][band + i_ts * IVAS_MAX_NUM_BANDS] = 0.0f;
    1729             :                     }
    1730             :                 }
    1731             : 
    1732    10128932 :                 if ( ( ndm == 1 ) && ( Wscale_d != NULL ) )
    1733             :                 {
    1734    16805420 :                     for ( j = 0; j < num_ch; j++ )
    1735             :                     {
    1736    13444336 :                         mixer_mat[0][j][band + i_ts * IVAS_MAX_NUM_BANDS] *= Wscale_d[band];
    1737             :                     }
    1738             :                 }
    1739             :             }
    1740             :         }
    1741             :     }
    1742             : 
    1743    12468807 :     return;
    1744             : }
    1745             : 
    1746             : 
    1747             : /*-------------------------------------------------------------------------
    1748             :  * ivas_dirac_dec_get_response()
    1749             :  *
    1750             :  * calculate reponse, 1 degree resolution
    1751             :  *------------------------------------------------------------------------*/
    1752             : 
    1753  2302510499 : 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  2302510499 :     index_azimuth = ( azimuth + 180 ) % 360;
    1768  2302510499 :     index_elevation = elevation + 90;
    1769  2302510499 :     e = index_elevation > 90 ? -1 : 1;
    1770  2302510499 :     el = index_elevation > 90 ? 180 - index_elevation : index_elevation;
    1771             : 
    1772  2302510499 :     az = index_azimuth > 180 ? 360 - index_azimuth : index_azimuth;
    1773  2302510499 :     f = index_azimuth > 180 ? -1.0f : 1.0f;
    1774             : 
    1775  2302510499 :     cos_1 = dirac_gains_trg_term[az][0];
    1776  2302510499 :     cos_2 = cos_1 * cos_1;
    1777  2302510499 :     sin_1 = f * dirac_gains_trg_term[az][1];
    1778  2302510499 :     cos_az[0] = cos_1;
    1779  2302510499 :     cos_az[1] = 2.0f * cos_2 - 1.0f;
    1780  2302510499 :     cos_az[2] = 2.0f * cos_1 * cos_az[1] - cos_az[0];
    1781  2302510499 :     sin_az[0] = sin_1;
    1782  2302510499 :     sin_az[1] = sin_1 * 2.0f * cos_1;
    1783  2302510499 :     sin_az[2] = sin_1 * ( 4.0f * cos_2 - 1.0f );
    1784             : 
    1785  2302510499 :     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  8486736202 :     for ( l = 1; l <= ambisonics_order; l++ )
    1821             :     {
    1822  6184225703 :         b_2 = l * l;
    1823  6184225703 :         b1_2 = l * l + 2 * l;
    1824 14146082088 :         for ( m = 0; m < l; m += 2 )
    1825             :         {
    1826  7961856385 :             b = b_2 + m;
    1827  7961856385 :             a = dirac_gains_P_idx[b];
    1828  7961856385 :             c = SQRT2 * dirac_gains_norm_term[a] * dirac_gains_Pnm[el][a];
    1829             : 
    1830  7961856385 :             response[b] = c * sin_az[l - m - 1];
    1831             : 
    1832  7961856385 :             b1 = b1_2 - m;
    1833  7961856385 :             response[b1] = c * cos_az[l - m - 1];
    1834             :         }
    1835             : 
    1836 10065940907 :         for ( m = 1; m < l; m += 2 )
    1837             :         {
    1838  3881715204 :             b = b_2 + m;
    1839  3881715204 :             a = dirac_gains_P_idx[b];
    1840  3881715204 :             c = SQRT2 * dirac_gains_norm_term[a] * dirac_gains_Pnm[el][a];
    1841  3881715204 :             c = c * e;
    1842             : 
    1843  3881715204 :             response[b] = c * sin_az[l - m - 1];
    1844             : 
    1845  3881715204 :             b1 = b1_2 - m;
    1846  3881715204 :             response[b1] = c * cos_az[l - m - 1];
    1847             :         }
    1848             : 
    1849  6184225703 :         b = b_2 + l;
    1850  6184225703 :         a = dirac_gains_P_idx[b];
    1851  6184225703 :         c = dirac_gains_norm_term[a] * dirac_gains_Pnm[el][a];
    1852  6184225703 :         if ( l % 2 == 1 )
    1853             :         {
    1854  4080141181 :             c = c * e;
    1855             :         }
    1856             : 
    1857  6184225703 :         response[b] = c;
    1858             :     }
    1859             : 
    1860  2302510499 :     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     8726704 : int16_t ivas_get_bits_to_encode(
    1871             :     int32_t val )
    1872             : {
    1873     8726704 :     int16_t bits_req = 0;
    1874             : 
    1875     8726704 :     assert( val >= 0 );
    1876             : 
    1877    13151060 :     while ( val )
    1878             :     {
    1879     4424356 :         bits_req++;
    1880     4424356 :         val >>= 1;
    1881             :     }
    1882             : 
    1883     8726704 :     return bits_req;
    1884             : }
    1885             : 
    1886             : 
    1887             : /*-----------------------------------------------------------------------------------------*
    1888             :  * Function ivas_spar_set_bitrate_config()
    1889             :  *
    1890             :  * Set SPAR bitrate config
    1891             :  *-----------------------------------------------------------------------------------------*/
    1892             : 
    1893      108422 : 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      108422 :     pSpar_md_cfg->nchan_transport = ivas_spar_br_table_consts[table_idx].nchan_transport;
    1914             : 
    1915      364101 :     for ( i = 0; i < pSpar_md_cfg->nchan_transport; i++ )
    1916             :     {
    1917      255679 :         pSpar_md_cfg->max_freq_per_chan[i] = ivas_spar_br_table_consts[table_idx].fpcs;
    1918             :     }
    1919             : 
    1920      108422 :     pSpar_md_cfg->active_w = ivas_spar_br_table_consts[table_idx].active_w;
    1921      108422 :     pSpar_md_cfg->agc_bits_ch_idx = ivas_spar_br_table_consts[table_idx].agc_bits_ch_idx;
    1922      108422 :     ivas_spar_get_uniform_quant_strat( pSpar_md_cfg, table_idx );
    1923             : 
    1924      108422 :     pSpar_md_cfg->quant_strat_bits = ivas_get_bits_to_encode( MAX_QUANT_STRATS );
    1925             : 
    1926             :     /* BLOCK: getEntropyCoderModels */
    1927             : 
    1928      108422 :     pSpar_md_cfg->remix_unmix_order = ivas_spar_br_table_consts[table_idx].dmx_str;
    1929             : 
    1930             :     /* bits per block*/
    1931      108422 :     total_bits = 0;
    1932      108422 :     max_bits = 0;
    1933             : 
    1934      108422 :     ivas_total_brate = ivas_spar_br_table_consts[table_idx].ivas_total_brate;
    1935      108422 :     sba_order = ivas_spar_br_table_consts[table_idx].sba_order;
    1936      108422 :     ivas_get_spar_table_idx( ivas_total_brate, sba_order, ivas_spar_br_table_consts[table_idx].bwidth, &length, &code );
    1937             : 
    1938      364101 :     for ( i = 0; i < pSpar_md_cfg->nchan_transport; i++ )
    1939             :     {
    1940      255679 :         total_bits += (int16_t) ( ivas_spar_br_table_consts[table_idx].core_brs[i][0] / FRAMES_PER_SEC );
    1941      255679 :         max_bits += (int16_t) ( ivas_spar_br_table_consts[table_idx].core_brs[i][1] / FRAMES_PER_SEC );
    1942             :     }
    1943             : 
    1944      108422 :     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      108422 :     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      108422 :     md_coding_bits_header = SPAR_NUM_CODING_STRAT_BITS + pSpar_md_cfg->quant_strat_bits;
    1948             : 
    1949      108422 :     pSpar_md_cfg->tgt_bits_per_blk -= md_coding_bits_header;
    1950      108422 :     pSpar_md_cfg->max_bits_per_blk -= md_coding_bits_header;
    1951             : 
    1952      108422 :     if ( ivas_total_brate < IVAS_24k4 )
    1953             :     {
    1954       21443 :         bands_bw = 2;
    1955             :     }
    1956             :     else
    1957             :     {
    1958       86979 :         bands_bw = 1;
    1959             :     }
    1960             : 
    1961      108422 :     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      108422 :     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      108422 :     pSpar_md_cfg->tgt_bits_per_blk += md_coding_bits_header;
    1965      108422 :     pSpar_md_cfg->max_bits_per_blk += md_coding_bits_header;
    1966             : 
    1967      108422 :     if ( enc_flag )
    1968             :     {
    1969             :         /*calculate the actual worst case bits*/
    1970       35424 :         if ( ivas_total_brate >= BRATE_SPAR_Q_STRAT )
    1971             :         {
    1972        7007 :             quant_strat = QUANT_STRAT_0;
    1973             :         }
    1974             :         else
    1975             :         {
    1976       28417 :             quant_strat = QUANT_STRAT_2;
    1977             :         }
    1978             : 
    1979       35424 :         num_PR_bits_dirac_bands = ( dirac2spar_md_flag == 1 ) ? num_bands - SPAR_DIRAC_SPLIT_START_BAND : 0;
    1980       35424 :         num_PR_bits_dirac_bands /= bands_bw;
    1981       35424 :         num_PR_bits_dirac_bands = max( 0, num_PR_bits_dirac_bands );
    1982       35424 :         num_PR_bits_dirac_bands *= DIRAC_TO_SPAR_HBR_PRED_CHS;
    1983             : 
    1984       35424 :         n_input = ivas_sba_get_nchan_metadata( sba_order, ivas_total_brate );
    1985       35424 :         n_dmx = ivas_spar_br_table_consts[table_idx].nchan_transport;
    1986       35424 :         n_dec = n_input - n_dmx;
    1987       35424 :         bits_PR = (int16_t) ceilf( log2f( ivas_spar_br_table_consts[table_idx].q_lvls[quant_strat][0] ) );
    1988       35424 :         num_PR_bits_dirac_bands *= bits_PR;
    1989       35424 :         bits_PR = bits_PR * ( n_input - 1 );
    1990       35424 :         bits_C = (int16_t) ceilf( log2f( ivas_spar_br_table_consts[table_idx].q_lvls[quant_strat][1] ) ) * ( ( n_dmx - 1 ) * n_dec );
    1991       35424 :         bits_P = (int16_t) ceilf( log2f( ivas_spar_br_table_consts[table_idx].q_lvls[quant_strat][2] ) ) * ( n_dec );
    1992       35424 :         wc_coarse_strat = bits_PR + bits_C + bits_P;
    1993       35424 :         wc_coarse_strat *= num_bands;
    1994       35424 :         wc_coarse_strat /= bands_bw;
    1995       35424 :         wc_coarse_strat -= num_PR_bits_dirac_bands;
    1996       35424 :         wc_coarse_strat += md_coding_bits_header;
    1997             : 
    1998       35424 :         if ( pSpar_md_cfg->max_bits_per_blk < wc_coarse_strat )
    1999             :         {
    2000           0 :             assert( 0 );
    2001             :         }
    2002             : 
    2003       35424 :         if ( agc_flag )
    2004             :         {
    2005       11037 :             if ( pSpar_md_cfg->nchan_transport == 1 )
    2006             :             {
    2007       11037 :                 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       24387 :             agc_bits = AGC_SIGNALLING_BITS;
    2017             :         }
    2018             : 
    2019       35424 :         if ( ivas_total_brate == PCA_BRATE && sba_order == SBA_FOA_ORDER )
    2020             :         {
    2021         492 :             pca_bits = 1;
    2022         492 :             if ( pca_flag )
    2023             :             {
    2024          16 :                 pca_bits += IVAS_PCA_QBITS + IVAS_PCA_QBITS - 1;
    2025             :             }
    2026             :         }
    2027             :         else
    2028             :         {
    2029       34932 :             pca_bits = 0;
    2030             :         }
    2031             : 
    2032       35424 :         pSpar_md_cfg->max_md_bits_spar = pSpar_md_cfg->max_bits_per_blk + agc_bits + pca_bits;
    2033             :     }
    2034             : 
    2035      108422 :     return;
    2036             : }
    2037             : 
    2038             : 
    2039             : /*-----------------------------------------------------------------------------------------*
    2040             :  * Function ivas_spar_bitrate_dist()
    2041             :  *
    2042             :  * Set SPAR bitrate distribution
    2043             :  *-----------------------------------------------------------------------------------------*/
    2044             : 
    2045      413043 : 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      413043 :     table_idx = ivas_get_spar_table_idx( ivas_total_brate, sba_order, bwidth, &bitlen, NULL );
    2058             : 
    2059      413043 :     nchan_transport = ivas_spar_br_table_consts[table_idx].nchan_transport;
    2060             : 
    2061      413043 :     sum_core_act_bits = 0;
    2062     2065215 :     for ( i = 0; i < nchan_transport; i++ )
    2063             :     {
    2064     1652172 :         core_bits_act[i] = (int16_t) ( ivas_spar_br_table_consts[table_idx].core_brs[i][0] / FRAMES_PER_SEC );
    2065             : 
    2066     1652172 :         sum_core_act_bits += core_bits_act[i];
    2067             :     }
    2068             : 
    2069      413043 :     residual_bits = nAvailBits - sum_core_act_bits;
    2070             : 
    2071             :     /* First compute core-coder bits as per bitrate distribution table and MD bitrate*/
    2072      413043 :     if ( residual_bits > 0 )
    2073             :     {
    2074       42433 :         for ( i = 0; i < nchan_transport; i++ )
    2075             :         {
    2076       42433 :             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       42433 :             core_bits_act[i] += min( residual_bits, core_range_bits[i] );
    2078       42433 :             residual_bits -= core_range_bits[i];
    2079             : 
    2080       42433 :             if ( residual_bits <= 0 )
    2081             :             {
    2082       31359 :                 break;
    2083             :             }
    2084             :         }
    2085             :     }
    2086             :     else
    2087             :     {
    2088     1908420 :         for ( i = 0; i < nchan_transport; i++ )
    2089             :         {
    2090     1526736 :             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      381684 :         overflow_bits = -residual_bits;
    2094             : 
    2095     1842460 :         for ( i = 0; i < nchan_transport; i++ )
    2096             :         {
    2097     1488294 :             core_bits_act[nchan_transport - 1 - i] -= min( overflow_bits, core_range_bits[nchan_transport - 1 - i] );
    2098     1488294 :             overflow_bits -= core_range_bits[nchan_transport - 1 - i];
    2099             : 
    2100     1488294 :             if ( overflow_bits <= 0 )
    2101             :             {
    2102       27518 :                 break;
    2103             :             }
    2104             :         }
    2105             : 
    2106      381684 :         if ( overflow_bits > 0 )
    2107             :         {
    2108             :             int16_t overflow_bits_ch;
    2109      354166 :             overflow_bits_ch = overflow_bits / nchan_transport;
    2110             : 
    2111     1770830 :             for ( i = 0; i < nchan_transport; i++ )
    2112             :             {
    2113     1416664 :                 core_bits_act[i] -= overflow_bits_ch;
    2114     1416664 :                 overflow_bits -= overflow_bits_ch;
    2115             :             }
    2116             : 
    2117      354166 :             core_bits_act[nchan_transport - 1] -= max( 0, overflow_bits );
    2118             :         }
    2119             :     }
    2120             : 
    2121     2065215 :     for ( i = 0; i < nchan_transport; i++ )
    2122             :     {
    2123     1652172 :         core_brates_act[i] = core_bits_act[i] * FRAMES_PER_SEC;
    2124             :     }
    2125             : 
    2126      413043 :     return;
    2127             : }

Generated by: LCOV version 1.14