LCOV - code coverage report
Current view: top level - lib_enc - ivas_spar_encoder.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 228 261 87.4 %
Date: 2025-05-23 08:37:30 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <stdint.h>
      34             : #include "options.h"
      35             : #ifdef DEBUGGING
      36             : #include "debug.h"
      37             : #endif
      38             : #include "ivas_prot.h"
      39             : #include "ivas_rom_com.h"
      40             : #include "ivas_stat_com.h"
      41             : #include "prot.h"
      42             : #include "math.h"
      43             : #include <assert.h>
      44             : #include "wmc_auto.h"
      45             : 
      46             : 
      47             : /*-------------------------------------------------------------------*
      48             :  * Local function prototypes
      49             :  *--------------------------------------------------------------------*/
      50             : 
      51             : static ivas_error ivas_spar_enc_process( Encoder_Struct *st_ivas, const ENCODER_CONFIG_HANDLE hEncoderConfig, BSTR_ENC_HANDLE hMetaData, const int16_t front_vad_flag, float *data_f[] );
      52             : 
      53             : 
      54             : /*-------------------------------------------------------------------------
      55             :  * ivas_spar_enc_open()
      56             :  *
      57             :  * Allocate and initialize SPAR encoder handle and sub-handles
      58             :  *------------------------------------------------------------------------*/
      59             : 
      60        1636 : ivas_error ivas_spar_enc_open(
      61             :     Encoder_Struct *st_ivas,         /* i/o: IVAS encoder handle        */
      62             :     const int16_t spar_reconfig_flag /* i  : SPAR reconfiguration flag  */
      63             : )
      64             : {
      65             :     SPAR_ENC_HANDLE hSpar;
      66             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
      67             :     IVAS_FB_CFG *fb_cfg;
      68             :     int16_t nchan_inp, nchan_transport, sba_order_internal;
      69             :     int16_t nchan_fb_in;
      70             :     int16_t table_idx, active_w_mixing;
      71             :     int32_t input_Fs, ivas_total_brate;
      72             :     ivas_error error;
      73             : 
      74        1636 :     hEncoderConfig = st_ivas->hEncoderConfig;
      75        1636 :     error = IVAS_ERR_OK;
      76        1636 :     hSpar = st_ivas->hSpar;
      77             : 
      78        1636 :     if ( !spar_reconfig_flag )
      79             :     {
      80             :         /* SPAR encoder handle */
      81         281 :         if ( ( hSpar = (SPAR_ENC_HANDLE) malloc( sizeof( SPAR_ENC_DATA ) ) ) == NULL )
      82             :         {
      83           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR encoder" );
      84             :         }
      85             :     }
      86             : 
      87        1636 :     hSpar->spar_reconfig_flag = 0;
      88        1636 :     input_Fs = hEncoderConfig->input_Fs;
      89        1636 :     sba_order_internal = min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER );
      90        1636 :     nchan_inp = ivas_sba_get_nchan_metadata( sba_order_internal, hEncoderConfig->ivas_total_brate );
      91        1636 :     assert( nchan_inp <= hEncoderConfig->nchan_inp );
      92        1636 :     ivas_total_brate = hEncoderConfig->ivas_total_brate;
      93             : 
      94        1636 :     nchan_fb_in = 0;
      95        1636 :     if ( st_ivas->sba_analysis_order == 1 )
      96             :     {
      97        1377 :         nchan_fb_in = FOA_CHANNELS;
      98             :     }
      99         259 :     else if ( st_ivas->sba_analysis_order == 2 )
     100             :     {
     101          73 :         nchan_fb_in = 9;
     102             :     }
     103         186 :     else if ( st_ivas->sba_analysis_order == 3 )
     104             :     {
     105         186 :         nchan_fb_in = 11;
     106             :     }
     107             :     else
     108             :     {
     109           0 :         assert( 0 && "sba_order must be 1,2, or 3!" );
     110             :     }
     111             : 
     112        1636 :     nchan_transport = ivas_get_sba_num_TCs( hEncoderConfig->ivas_total_brate, sba_order_internal );
     113             : 
     114        1636 :     table_idx = ivas_get_spar_table_idx( ivas_total_brate, sba_order_internal, SPAR_CONFIG_BW, NULL, NULL );
     115             : 
     116             :     /* MD handle */
     117        1636 :     if ( ( error = ivas_spar_md_enc_open( &( hSpar->hMdEnc ), hEncoderConfig, sba_order_internal ) ) != IVAS_ERR_OK )
     118             :     {
     119           0 :         return error;
     120             :     }
     121             : 
     122             :     /* set FB config. */
     123        1636 :     active_w_mixing = ivas_spar_br_table_consts[table_idx].active_w;
     124        1636 :     if ( ( error = ivas_fb_set_cfg( &fb_cfg, SBA_FORMAT, nchan_inp, nchan_transport, active_w_mixing, input_Fs, nchan_fb_in ) ) != IVAS_ERR_OK )
     125             :     {
     126           0 :         return error;
     127             :     }
     128        1636 :     fb_cfg->remix_order = remix_order_set[hSpar->hMdEnc->spar_md_cfg.remix_unmix_order];
     129             : 
     130             :     /* FB mixer handle */
     131        1636 :     if ( ( error = ivas_FB_mixer_open( &( hSpar->hFbMixer ), input_Fs, fb_cfg, spar_reconfig_flag ) ) != IVAS_ERR_OK )
     132             :     {
     133           0 :         return error;
     134             :     }
     135             : 
     136             :     /* Covariance handle */
     137        1636 :     if ( ( error = ivas_spar_covar_enc_open( &( hSpar->hCovEnc ), hSpar->hFbMixer->pFb, input_Fs, nchan_inp, COV_SMOOTH_SPAR, hEncoderConfig->ivas_total_brate ) ) != IVAS_ERR_OK )
     138             :     {
     139           0 :         return error;
     140             :     }
     141             : 
     142        1636 :     if ( !spar_reconfig_flag )
     143             :     {
     144             :         /* Transient Detector handle */
     145         281 :         if ( ( error = ivas_transient_det_open( &( hSpar->hTranDet ), input_Fs ) ) != IVAS_ERR_OK )
     146             :         {
     147           0 :             return error;
     148             :         }
     149             :     }
     150             : 
     151             :     /* initialization */
     152        1636 :     hSpar->hMdEnc->table_idx = -1;
     153             : 
     154             :     /* AGC handle */
     155             : #ifdef DEBUG_AGC_ENCODER_CMD_OPTION
     156             :     hSpar->AGC_Enable = ivas_agc_enc_get_flag( hEncoderConfig->Opt_AGC_ON, nchan_transport );
     157             : #else
     158        1636 :     hSpar->AGC_Enable = ivas_agc_enc_get_flag( nchan_transport );
     159             : #endif
     160             : 
     161        1636 :     hSpar->hAgcEnc = NULL;
     162        1636 :     if ( hSpar->AGC_Enable )
     163             :     {
     164         567 :         if ( ( error = ivas_spar_agc_enc_open( &hSpar->hAgcEnc, input_Fs, nchan_inp ) ) != IVAS_ERR_OK )
     165             :         {
     166           0 :             return error;
     167             :         }
     168             :     }
     169             : 
     170             :     /* PCA handle */
     171        1636 :     hSpar->hPCA = NULL;
     172        1636 :     if ( hEncoderConfig->Opt_PCA_ON )
     173             :     {
     174           4 :         if ( ( hSpar->hPCA = (PCA_ENC_STATE *) malloc( sizeof( PCA_ENC_STATE ) ) ) == NULL )
     175             :         {
     176           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR PCA encoder" );
     177             :         }
     178           4 :         ivas_pca_enc_init( hSpar->hPCA );
     179             :     }
     180             : 
     181             :     /* initialization */
     182        1636 :     hSpar->hMdEnc->table_idx = -1;
     183             : 
     184             :     /*-----------------------------------------------------------------*
     185             :      * Configuration - set SPAR high-level parameters
     186             :      *-----------------------------------------------------------------*/
     187             : 
     188        1636 :     ivas_spar_config( hEncoderConfig->ivas_total_brate, min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER ), &st_ivas->nchan_transport, &st_ivas->nSCE, &st_ivas->nCPE, &hSpar->core_nominal_brate, -1 );
     189             : 
     190        1636 :     if ( st_ivas->nchan_transport == 1 )
     191             :     {
     192         567 :         hEncoderConfig->element_mode_init = IVAS_SCE;
     193             :     }
     194             :     else
     195             :     {
     196        1069 :         hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     197             :     }
     198             : 
     199             :     /*-----------------------------------------------------------------*
     200             :      * Allocate and initialize Front-VAD handle
     201             :      *-----------------------------------------------------------------*/
     202             : 
     203        1636 :     if ( !spar_reconfig_flag )
     204             :     {
     205         281 :         hSpar->front_vad_flag = 0;
     206         281 :         hSpar->front_vad_dtx_flag = 0;
     207         281 :         hSpar->force_front_vad = 0;
     208             : 
     209         281 :         if ( hEncoderConfig->Opt_DTX_ON )
     210             :         {
     211          69 :             if ( ( error = front_vad_create( &( hSpar->hFrontVad ), hEncoderConfig ) ) != IVAS_ERR_OK )
     212             :             {
     213           0 :                 return error;
     214             :             }
     215             : 
     216          69 :             if ( ( hSpar->hCoreCoderVAD = (ENC_CORE_HANDLE) malloc( sizeof( Encoder_State ) ) ) == NULL )
     217             :             {
     218           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CoreCoder structure\n" ) );
     219             :             }
     220             : 
     221          69 :             copy_encoder_config( st_ivas, hSpar->hCoreCoderVAD, 1 );
     222             : 
     223          69 :             hSpar->hCoreCoderVAD->total_brate = hEncoderConfig->ivas_total_brate; /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
     224          69 :             hSpar->hCoreCoderVAD->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
     225             : 
     226          69 :             if ( ( error = init_encoder( hSpar->hCoreCoderVAD, st_ivas, 0, hEncoderConfig->var_SID_rate_flag, hEncoderConfig->interval_SID, 1, st_ivas->ism_mode, hSpar->hCoreCoderVAD->total_brate ) ) != IVAS_ERR_OK )
     227             :             {
     228           0 :                 return error;
     229             :             }
     230             :         }
     231             :         else
     232             :         {
     233         212 :             hSpar->hCoreCoderVAD = NULL;
     234         212 :             hSpar->hFrontVad = NULL;
     235             :         }
     236             :     }
     237             : 
     238             :     /*-----------------------------------------------------------------*
     239             :      * Final assignment
     240             :      *-----------------------------------------------------------------*/
     241             : 
     242        1636 :     st_ivas->hSpar = hSpar;
     243             : 
     244        1636 :     return error;
     245             : }
     246             : 
     247             : 
     248             : /*-------------------------------------------------------------------------
     249             :  * ivas_spar_enc_close()
     250             :  *
     251             :  * Deallocate SPAR encoder handle
     252             :  *------------------------------------------------------------------------*/
     253             : 
     254        1982 : void ivas_spar_enc_close(
     255             :     SPAR_ENC_HANDLE *hSpar,          /* i/o: SPAR encoder handle        */
     256             :     const int32_t input_Fs,          /* i  : input sampling rate        */
     257             :     const int16_t nchan_inp,         /* i  : number of input channels   */
     258             :     const int16_t spar_reconfig_flag /* i  : SPAR reconfiguration flag  */
     259             : )
     260             : {
     261             :     int16_t num_chans;
     262             : 
     263        1982 :     if ( hSpar == NULL || *hSpar == NULL )
     264             :     {
     265         346 :         return;
     266             :     }
     267             : 
     268        1636 :     if ( !spar_reconfig_flag )
     269             :     {
     270             :         /* core-coder-VAD handle */
     271         281 :         if ( ( *hSpar )->hCoreCoderVAD != NULL )
     272             :         {
     273          69 :             destroy_core_enc( ( *hSpar )->hCoreCoderVAD );
     274          69 :             ( *hSpar )->hCoreCoderVAD = NULL;
     275             :         }
     276             : 
     277             :         /* front-VAD handle */
     278         281 :         if ( ( *hSpar )->hFrontVad != NULL )
     279             :         {
     280          69 :             front_vad_destroy( &( *hSpar )->hFrontVad );
     281          69 :             ( *hSpar )->hFrontVad = NULL;
     282             :         }
     283             :     }
     284             : 
     285        1636 :     num_chans = ( *hSpar )->hFbMixer->fb_cfg->num_in_chans;
     286        1636 :     assert( num_chans <= nchan_inp );
     287             : 
     288             :     /* MD handle */
     289        1636 :     ivas_spar_md_enc_close( &( *hSpar )->hMdEnc );
     290             : 
     291             :     /* Covar. State handle */
     292        1636 :     ivas_spar_covar_enc_close( &( *hSpar )->hCovEnc, num_chans );
     293             : 
     294             :     /* FB mixer handle */
     295        1636 :     ivas_FB_mixer_close( &( *hSpar )->hFbMixer, input_Fs, spar_reconfig_flag );
     296             : 
     297             :     /* AGC */
     298        1636 :     ivas_spar_agc_enc_close( &( *hSpar )->hAgcEnc );
     299             : 
     300             :     /* PCA */
     301        1636 :     if ( ( *hSpar )->hPCA != NULL )
     302             :     {
     303           4 :         free( ( *hSpar )->hPCA );
     304           4 :         ( *hSpar )->hPCA = NULL;
     305             :     }
     306             : 
     307        1636 :     if ( !spar_reconfig_flag )
     308             :     {
     309             :         /* Trans Det handle */
     310         281 :         ivas_transient_det_close( &( *hSpar )->hTranDet );
     311         281 :         free( ( *hSpar ) );
     312         281 :         ( *hSpar ) = NULL;
     313             :     }
     314             : 
     315        1636 :     return;
     316             : }
     317             : 
     318             : 
     319             : /*-------------------------------------------------------------------*
     320             :  * ivas_spar_enc()
     321             :  *
     322             :  * Principal IVAS SPAR encoder routine
     323             :  *-------------------------------------------------------------------*/
     324             : 
     325      159500 : ivas_error ivas_spar_enc(
     326             :     Encoder_Struct *st_ivas,   /* i/o: IVAS encoder structure              */
     327             :     float *data_f[],           /* i/o: input/transport audio channels      */
     328             :     const int16_t input_frame, /* i  : input frame length                  */
     329             :     int16_t *nb_bits_metadata, /* i  : number of MD bits written           */
     330             :     BSTR_ENC_HANDLE hMetaData  /* o  : MetaData handle                     */
     331             : )
     332             : {
     333             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
     334             :     Encoder_State *st0; /* used for bitstream handling */
     335             :     ivas_error error;
     336             : 
     337      159500 :     error = IVAS_ERR_OK;
     338      159500 :     hEncoderConfig = st_ivas->hEncoderConfig;
     339      159500 :     st0 = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[0]->hCoreCoder[0] : st_ivas->hCPE[0]->hCoreCoder[0];
     340             : 
     341             :     /* Write SBA signaling bits */
     342      159500 :     if ( hEncoderConfig->ivas_format == SBA_FORMAT || ( hEncoderConfig->ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_SBA_MODE_DISC ) )
     343             :     {
     344             :         /* Write SBA planar flag */
     345      144580 :         push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_planar, SBA_PLANAR_BITS );
     346             : 
     347             :         /* Write SBA order */
     348      144580 :         push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_order, SBA_ORDER_BITS );
     349             :     }
     350             :     else /* ism_mode == ISM_MODE_NONE */
     351             :     {
     352       14920 :         if ( hEncoderConfig->ivas_total_brate < IVAS_24k4 )
     353             :         {
     354             :             /* Write SBA planar flag */
     355        2881 :             push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_planar, SBA_PLANAR_BITS );
     356             : 
     357             :             /* hack to indicate OSBA format (SBA order = 0) at low bitrates */
     358        2881 :             push_indice( st0->hBstr, IND_SMODE, 0, SBA_ORDER_BITS );
     359             : 
     360             :             /* additionally code the real SBA order */
     361        2881 :             push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_order, SBA_ORDER_BITS );
     362             :         }
     363             :         else
     364             :         {
     365             :             /* Write SBA planar flag */
     366       12039 :             push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_planar, SBA_PLANAR_BITS );
     367             : 
     368             :             /* Write SBA order */
     369       12039 :             push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_order, SBA_ORDER_BITS );
     370             :         }
     371             :     }
     372             : 
     373      159500 :     if ( hEncoderConfig->ivas_format == SBA_ISM_FORMAT )
     374             :     {
     375             :         /* write the number of objects in ISM_SBA format*/
     376       37000 :         push_indice( hMetaData, IND_ISM_NUM_OBJECTS, hEncoderConfig->nchan_ism - 1, NO_BITS_MASA_ISM_NO_OBJ );
     377             :     }
     378             : 
     379             :     /* front VAD */
     380      159500 :     if ( ( error = front_vad_spar( st_ivas->hSpar, data_f[0], hEncoderConfig, input_frame ) ) != IVAS_ERR_OK )
     381             :     {
     382           0 :         return error;
     383             :     }
     384             : 
     385      159500 :     if ( hEncoderConfig->sba_planar )
     386             :     {
     387       10250 :         ivas_sba_zero_vert_comp( data_f, st_ivas->sba_analysis_order, hEncoderConfig->sba_planar, input_frame );
     388             :     }
     389             : 
     390             :     /* SPAR encoder */
     391      159500 :     if ( ( error = ivas_spar_enc_process( st_ivas, hEncoderConfig, hMetaData, st_ivas->hSpar->front_vad_flag, data_f ) ) != IVAS_ERR_OK )
     392             :     {
     393           0 :         return error;
     394             :     }
     395             : 
     396      159500 :     *nb_bits_metadata = hMetaData->nb_bits_tot;
     397             : 
     398             :     /* Force IVAS front pre-proc decision for higher bitrates */
     399      159500 :     if ( hEncoderConfig->ivas_total_brate > SBA_DTX_BITRATE_THRESHOLD || hEncoderConfig->Opt_DTX_ON == 0 )
     400             :     {
     401      141710 :         st_ivas->hSpar->front_vad_flag = 0;
     402             :     }
     403             : 
     404      159500 :     return error;
     405             : }
     406             : 
     407             : 
     408             : /*-------------------------------------------------------------------*
     409             :  * ivas_spar_cov_md_process()
     410             :  *
     411             :  * Process call for SPAR covariance and MD encoder
     412             :  *-------------------------------------------------------------------*/
     413             : 
     414      159500 : static ivas_error ivas_spar_cov_md_process(
     415             :     const ENCODER_CONFIG_HANDLE hEncoderConfig,
     416             :     SPAR_ENC_HANDLE hSpar,
     417             :     const IVAS_QMETADATA_HANDLE hQMetaData,
     418             :     BSTR_ENC_HANDLE hMetaData,
     419             :     const int16_t nchan_inp,
     420             :     const int16_t sba_order,
     421             :     float *ppIn_FR_real[IVAS_SPAR_MAX_CH],
     422             :     float *ppIn_FR_imag[IVAS_SPAR_MAX_CH],
     423             :     const int16_t transient_det[2],
     424             :     const int16_t dtx_vad,
     425             :     const int16_t nchan_transport,
     426             :     int16_t *dyn_active_w_flag )
     427             : {
     428             :     int16_t i, j, i_ts, b, table_idx;
     429             :     int16_t active_w_vlbr;
     430             :     /* note: the actual dimensions of matrixes correspond to num_channels = ivas_sba_get_nchan_metadata( sba_order, ivas_total_brate ); */
     431             :     float *cov_real[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
     432             :     float *cov_dtx_real[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
     433             :     float cov_real_buf[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH][IVAS_MAX_NUM_BANDS];
     434             :     float cov_dtx_real_buf[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH][IVAS_MAX_NUM_BANDS];
     435             :     ivas_error error;
     436             : 
     437      159500 :     error = IVAS_ERR_OK;
     438             : 
     439      159500 :     active_w_vlbr = ( hEncoderConfig->ivas_total_brate < IVAS_24k4 ) ? 1 : 0;
     440             : 
     441             :     /*-----------------------------------------------------------------------------------------*
     442             :      * Set SPAR bitrates
     443             :      *-----------------------------------------------------------------------------------------*/
     444             : 
     445      159500 :     table_idx = ivas_get_spar_table_idx( hEncoderConfig->ivas_total_brate, sba_order, SPAR_CONFIG_BW, NULL, NULL );
     446             : 
     447      159500 :     if ( hSpar->hMdEnc->table_idx != table_idx )
     448             :     {
     449        1954 :         hSpar->hMdEnc->table_idx = table_idx;
     450        1954 :         if ( hEncoderConfig->ivas_total_brate != hEncoderConfig->last_ivas_total_brate && !hSpar->spar_reconfig_flag )
     451             :         {
     452         318 :             if ( ( error = ivas_spar_md_enc_init( hSpar->hMdEnc, hEncoderConfig, sba_order ) ) != IVAS_ERR_OK )
     453             :             {
     454           0 :                 return error;
     455             :             }
     456             :         }
     457             :         else
     458             :         {
     459        1636 :             ivas_spar_set_bitrate_config( &hSpar->hMdEnc->spar_md_cfg, table_idx, ( hSpar->hMdEnc->spar_hoa_md_flag ) ? IVAS_MAX_NUM_BANDS : SPAR_DIRAC_SPLIT_START_BAND, hSpar->hMdEnc->spar_hoa_dirac2spar_md_flag, 1, hEncoderConfig->Opt_PCA_ON, hSpar->AGC_Enable );
     460             :         }
     461             :     }
     462             : 
     463             :     /*-----------------------------------------------------------------------------------------*
     464             :      * Covariance process
     465             :      *-----------------------------------------------------------------------------------------*/
     466             : 
     467      899700 :     for ( i = 0; i < nchan_inp; i++ )
     468             :     {
     469     4668660 :         for ( j = 0; j < nchan_inp; j++ )
     470             :         {
     471     3928460 :             cov_real[i][j] = cov_real_buf[i][j];
     472     3928460 :             cov_dtx_real[i][j] = cov_dtx_real_buf[i][j];
     473     4352460 :             for ( b = hSpar->hFbMixer->pFb->filterbank_num_bands; b < IVAS_MAX_NUM_BANDS; b++ )
     474             :             {
     475      424000 :                 cov_real[i][j][b] = 0.0f;
     476      424000 :                 cov_dtx_real[i][j][b] = 0.0f;
     477             :             }
     478             :         }
     479             :     }
     480             : 
     481      159500 :     ivas_enc_cov_handler_process( hSpar->hCovEnc, ppIn_FR_real, ppIn_FR_imag, cov_real, cov_dtx_real, hSpar->hFbMixer->pFb, 0, hSpar->hFbMixer->pFb->filterbank_num_bands, nchan_inp, dtx_vad, transient_det, hSpar->hMdEnc->HOA_md_ind,
     482      159500 :                                   &hSpar->hMdEnc->spar_md.res_ind, remix_order_set[hSpar->hMdEnc->spar_md_cfg.remix_unmix_order], dyn_active_w_flag, nchan_transport, 1 );
     483             : 
     484      159500 :     if ( nchan_transport > 1 && nchan_transport <= ( FOA_CHANNELS - 1 ) )
     485             :     {
     486       74079 :         push_next_indice( hMetaData, *dyn_active_w_flag, 1 );
     487       74079 :         if ( ( *dyn_active_w_flag == 1 ) && ( nchan_transport == 2 ) )
     488             :         {
     489           0 :             push_next_indice( hMetaData, hSpar->hMdEnc->spar_md.res_ind - nchan_transport, 1 );
     490             :         }
     491       74079 :         hSpar->front_vad_flag = ( *dyn_active_w_flag == 1 ) ? 1 : hSpar->front_vad_flag;
     492             :     }
     493             :     else
     494             :     {
     495       85421 :         if ( nchan_transport == FOA_CHANNELS )
     496             :         {
     497       41170 :             push_next_indice( hMetaData, 0, 1 );
     498             :         }
     499             :     }
     500             : 
     501             :     /*-----------------------------------------------------------------------------------------*
     502             :      * MetaData encoder
     503             :      *-----------------------------------------------------------------------------------------*/
     504             : 
     505      159500 :     if ( hSpar->hMdEnc->spar_hoa_md_flag == 0 )
     506             :     {
     507      137460 :         if ( ( error = ivas_spar_md_enc_process( hSpar->hMdEnc, hEncoderConfig, cov_real, cov_dtx_real, hMetaData, dtx_vad, nchan_inp, sba_order, hSpar->hFbMixer->prior_mixer, *dyn_active_w_flag, hQMetaData->dirac_mono_flag ) ) != IVAS_ERR_OK )
     508             :         {
     509           0 :             return error;
     510             :         }
     511             :     }
     512             : 
     513      159500 :     if ( hSpar->hMdEnc->spar_hoa_dirac2spar_md_flag )
     514             :     {
     515             :         float azi_dirac[IVAS_MAX_NUM_BANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
     516             :         float ele_dirac[IVAS_MAX_NUM_BANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
     517             :         float diffuseness[IVAS_MAX_NUM_BANDS];
     518             :         float Wscale_d[IVAS_MAX_NUM_BANDS];
     519             :         int16_t d_start_band, d_end_band;
     520             :         int16_t dirac_band_idx;
     521             : 
     522      149560 :         d_start_band = hSpar->enc_param_start_band;
     523      149560 :         d_end_band = IVAS_MAX_NUM_BANDS;
     524             : 
     525      798200 :         for ( b = d_start_band; b < d_end_band; b++ )
     526             :         {
     527      648640 :             dirac_band_idx = hSpar->dirac_to_spar_md_bands[b] - d_start_band;
     528     3080564 :             for ( i_ts = 0; i_ts < hQMetaData->q_direction->cfg.nblocks; i_ts++ )
     529             :             {
     530     2431924 :                 azi_dirac[b][i_ts] = hQMetaData->q_direction->band_data[dirac_band_idx].azimuth[i_ts];
     531     2431924 :                 ele_dirac[b][i_ts] = hQMetaData->q_direction->band_data[dirac_band_idx].elevation[i_ts];
     532             :             }
     533      648640 :             diffuseness[b] = 1.0f - hQMetaData->q_direction->band_data[dirac_band_idx].energy_ratio[0];
     534             :         }
     535             : 
     536      149560 :         if ( d_start_band >= 6 && dtx_vad == 1 )
     537             :         {
     538      139433 :             mvr2r( hSpar->hMdEnc->spar_md.band_coeffs[d_start_band - 1].P_quant_re, hSpar->hMdEnc->spar_md.band_coeffs[d_start_band - 1].P_re, IVAS_SPAR_MAX_CH - 1 );
     539             :         }
     540             : 
     541      798200 :         for ( b = d_start_band; b < d_end_band; b++ )
     542             :         {
     543      648640 :             Wscale_d[b] = 1.0f;
     544     2875120 :             for ( i = 1; i < nchan_inp; i++ )
     545             :             {
     546     2226480 :                 Wscale_d[b] += cov_real[i][i][b] / max( EPSILON, cov_real[0][0][b] );
     547             :             }
     548      648640 :             Wscale_d[b] = Wscale_d[b] / ( 1.0f + (float) sba_order ); /*DirAC normalized signal variance sums to 1 + order*/
     549      648640 :             Wscale_d[b] = sqrtf( Wscale_d[b] );
     550      648640 :             Wscale_d[b] = min( 2.0f, max( Wscale_d[b], 1.0f ) );
     551             :         }
     552             : 
     553      149560 :         ivas_get_spar_md_from_dirac( azi_dirac, ele_dirac, diffuseness, 1, hSpar->hMdEnc->mixer_mat, &hSpar->hMdEnc->spar_md, &hSpar->hMdEnc->spar_md_cfg, d_start_band, d_end_band, ( hSpar->hMdEnc->spar_hoa_md_flag ) ? 1 : sba_order, dtx_vad, Wscale_d, hQMetaData->useLowerRes, active_w_vlbr, *dyn_active_w_flag );
     554             :     }
     555             : 
     556      159500 :     if ( hSpar->hMdEnc->spar_hoa_md_flag )
     557             :     {
     558       22040 :         error = ivas_spar_md_enc_process( hSpar->hMdEnc, hEncoderConfig, cov_real, cov_dtx_real, hMetaData, dtx_vad, nchan_inp, sba_order, hSpar->hFbMixer->prior_mixer, *dyn_active_w_flag, hQMetaData->dirac_mono_flag );
     559             :     }
     560             : 
     561      159500 :     return error;
     562             : }
     563             : 
     564             : 
     565             : /*-----------------------------------------------------------------------------------------*
     566             :  * Function ivas_spar_enc_process()
     567             :  *
     568             :  * Process call for SPAR encoder
     569             :  *-----------------------------------------------------------------------------------------*/
     570             : 
     571      159500 : static ivas_error ivas_spar_enc_process(
     572             :     Encoder_Struct *st_ivas,                    /* i/o: IVAS encoder structure          */
     573             :     const ENCODER_CONFIG_HANDLE hEncoderConfig, /* i  : configuration structure         */
     574             :     BSTR_ENC_HANDLE hMetaData,                  /* i/o: MetaData handle                 */
     575             :     const int16_t front_vad_flag,               /* i  : front-VAD decision              */
     576             :     float *data_f[]                             /* i/o: input/transport audio channels  */
     577             : )
     578             : {
     579             :     float pcm_tmp[DIRAC_MAX_ANA_CHANS][L_FRAME48k * 2];
     580             :     float *p_pcm_tmp[DIRAC_MAX_ANA_CHANS];
     581             :     int16_t i, j, input_frame, dtx_vad;
     582             :     int16_t transient_det[2];
     583             :     int16_t hodirac_flag;
     584             :     int32_t ivas_total_brate, input_Fs;
     585             :     int16_t nchan_inp, sba_order, nchan_transport;
     586             :     int16_t in_out_mixer_map[IVAS_MAX_FB_MIXER_OUT_CH][IVAS_MAX_SPAR_FB_MIXER_IN_CH];
     587             :     ivas_error error;
     588             :     const int16_t *order;
     589      159500 :     SPAR_ENC_HANDLE hSpar = st_ivas->hSpar;
     590      159500 :     IVAS_QMETADATA_HANDLE hQMetaData = st_ivas->hQMetaData;
     591             :     int16_t ts, l_ts, num_del_samples, b, i_ts;
     592             : 
     593             :     float *ppIn_FR_real[IVAS_SPAR_MAX_CH], *ppIn_FR_imag[IVAS_SPAR_MAX_CH];
     594             :     float wyzx_del_buf[FOA_CHANNELS][IVAS_FB_1MS_48K_SAMP];
     595             :     int16_t dyn_active_w_flag;
     596             :     int16_t nchan_fb_in;
     597             : 
     598      159500 :     push_wmops( "ivas_spar_enc_process" );
     599             : 
     600             :     /*-----------------------------------------------------------------------------------------*
     601             :      * Initialization
     602             :      *-----------------------------------------------------------------------------------------*/
     603             : 
     604      159500 :     error = IVAS_ERR_OK;
     605             : 
     606      159500 :     input_Fs = hEncoderConfig->input_Fs;
     607      159500 :     ivas_total_brate = hEncoderConfig->ivas_total_brate;
     608      159500 :     num_del_samples = hSpar->hFbMixer->fb_cfg->fb_latency;
     609             : 
     610      159500 :     input_frame = (int16_t) ( input_Fs / FRAMES_PER_SEC );
     611      159500 :     sba_order = min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER );
     612      159500 :     nchan_inp = ivas_sba_get_nchan_metadata( sba_order, hEncoderConfig->ivas_total_brate );
     613      159500 :     assert( nchan_inp <= hEncoderConfig->nchan_inp );
     614             : 
     615      159500 :     nchan_fb_in = hSpar->hFbMixer->fb_cfg->nchan_fb_in;
     616      159500 :     nchan_transport = st_ivas->nchan_transport;
     617             : 
     618      275960 :     for ( i = FOA_CHANNELS + 1; i < nchan_fb_in; i++ )
     619             :     {
     620      116460 :         mvr2r( data_f[HOA_keep_ind[i]], data_f[i], input_frame );
     621             :     }
     622             : 
     623             :     /*-----------------------------------------------------------------------------------------*
     624             :      * Transient detector
     625             :      *-----------------------------------------------------------------------------------------*/
     626             : 
     627      159500 :     ivas_transient_det_process( hSpar->hTranDet, data_f[0], input_frame, transient_det );
     628             : 
     629      159500 :     if ( ivas_total_brate < IVAS_24k4 )
     630             :     {
     631       13553 :         transient_det[1] = 0;
     632             :     }
     633             : 
     634             :     /* store previous input samples for W in local buffer */
     635      159500 :     assert( num_del_samples <= IVAS_FB_1MS_48K_SAMP );
     636      159500 :     if ( hSpar->hFbMixer->fb_cfg->active_w_mixing == 0 )
     637             :     {
     638             :         /* fill delay (1 ms) buffer for all Transport channels */
     639      576245 :         for ( i = 0; i < FOA_CHANNELS; i++ )
     640             :         {
     641      460996 :             int16_t idx = hSpar->hFbMixer->fb_cfg->remix_order[i];
     642      460996 :             mvr2r( &hSpar->hFbMixer->ppFilterbank_prior_input[idx][hSpar->hFbMixer->fb_cfg->prior_input_length - num_del_samples], wyzx_del_buf[idx], num_del_samples );
     643             :         }
     644             :     }
     645             : 
     646             :     /*-----------------------------------------------------------------------------------------*
     647             :      * FB mixer ingest
     648             :      *-----------------------------------------------------------------------------------------*/
     649             : 
     650      936000 :     for ( i = 0; i < nchan_fb_in; i++ )
     651             :     {
     652      776500 :         p_pcm_tmp[i] = pcm_tmp[i];
     653             :     }
     654             : 
     655             :     /* run Filter Bank overlapping MDFT analysis first, then we can use the temporary buffer for Parameter MDFT analysis*/
     656      159500 :     ivas_fb_mixer_pcm_ingest( hSpar->hFbMixer, data_f, p_pcm_tmp, input_frame, hSpar->hMdEnc->HOA_md_ind );
     657             : 
     658             :     /* prepare Parameter MDFT analysis */
     659      936000 :     for ( i = 0; i < nchan_fb_in; i++ )
     660             :     {
     661      776500 :         ppIn_FR_real[i] = p_pcm_tmp[i];
     662      776500 :         ppIn_FR_imag[i] = p_pcm_tmp[i] + input_frame;
     663      776500 :         p_pcm_tmp[i] = data_f[i];
     664             :     }
     665             : 
     666      159500 :     l_ts = input_frame / MAX_PARAM_SPATIAL_SUBFRAMES;
     667             : 
     668      797500 :     for ( ts = 0; ts < MAX_PARAM_SPATIAL_SUBFRAMES; ts++ )
     669             :     {
     670      638000 :         ivas_fb_mixer_get_windowed_fr( hSpar->hFbMixer, p_pcm_tmp, ppIn_FR_real, ppIn_FR_imag, l_ts, l_ts, nchan_fb_in );
     671             : 
     672      638000 :         ivas_fb_mixer_update_prior_input( hSpar->hFbMixer, p_pcm_tmp, l_ts, nchan_fb_in );
     673             : 
     674     3744000 :         for ( i = 0; i < nchan_fb_in; i++ )
     675             :         {
     676     3106000 :             p_pcm_tmp[i] += l_ts;
     677     3106000 :             ppIn_FR_real[i] += l_ts;
     678     3106000 :             ppIn_FR_imag[i] += l_ts;
     679             :         }
     680             :     }
     681             : 
     682             :     /* turn pointers back to the local buffer, needed for the following processing */
     683      936000 :     for ( i = 0; i < nchan_fb_in; i++ )
     684             :     {
     685      776500 :         ppIn_FR_real[i] = pcm_tmp[i];
     686      776500 :         ppIn_FR_imag[i] = pcm_tmp[i] + input_frame;
     687      776500 :         p_pcm_tmp[i] = pcm_tmp[i];
     688             :     }
     689             : 
     690      159500 :     dtx_vad = ( hEncoderConfig->Opt_DTX_ON == 1 ) ? front_vad_flag : 1;
     691             : 
     692             :     /*-----------------------------------------------------------------------------------------*
     693             :      * DirAC encoding
     694             :      *-----------------------------------------------------------------------------------------*/
     695             : 
     696      159500 :     hodirac_flag = ivas_get_hodirac_flag( ivas_total_brate, st_ivas->sba_analysis_order );
     697             : 
     698      159500 :     if ( ( error = ivas_dirac_enc( st_ivas->hDirAC, hQMetaData, hMetaData, data_f, ppIn_FR_real, ppIn_FR_imag, input_frame, dtx_vad, hEncoderConfig->ivas_format, nchan_transport, hodirac_flag ) ) != IVAS_ERR_OK )
     699             :     {
     700           0 :         return error;
     701             :     }
     702             : 
     703             :     /* Set Energy Ratio to 0.0 if the mono flag has been set */
     704      159500 :     if ( hQMetaData->dirac_mono_flag )
     705             :     {
     706           0 :         for ( b = hQMetaData->q_direction->cfg.start_band; b < hQMetaData->q_direction->cfg.nbands; b++ )
     707             :         {
     708           0 :             for ( i_ts = 0; i_ts < ( ( dtx_vad == 1 ) ? hQMetaData->q_direction[0].cfg.nblocks : 1 ); i_ts++ )
     709             :             {
     710           0 :                 hQMetaData->q_direction[0].band_data[b].energy_ratio[i_ts] = 0.0f;
     711             :             }
     712             :         }
     713             :     }
     714             : 
     715             :     /*-----------------------------------------------------------------------------------------*
     716             :      * Covariance and MD processing
     717             :      *-----------------------------------------------------------------------------------------*/
     718             : 
     719      159500 :     if ( ( error = ivas_spar_cov_md_process( hEncoderConfig, st_ivas->hSpar, st_ivas->hQMetaData, hMetaData, nchan_inp, sba_order, ppIn_FR_real, ppIn_FR_imag, transient_det, dtx_vad, nchan_transport, &dyn_active_w_flag ) ) != IVAS_ERR_OK )
     720             :     {
     721           0 :         return error;
     722             :     }
     723             : 
     724             : #ifdef DEBUG_LBR_SBA
     725             :     /* Dumping SPAR Coefficients */
     726             :     char f_name[100];
     727             :     int16_t nbands = 6;
     728             :     int16_t num_subframes = 1;
     729             :     int16_t num_elements = 6;
     730             :     int16_t num_block_group = 1;
     731             :     int16_t byte_size = sizeof( float );
     732             : 
     733             :     sprintf( f_name, "SBA_MD_values.bin" );
     734             :     ( frame == 0 ) ? dbgwrite( &nbands, sizeof( nbands ), 1, 1, f_name ) : false;
     735             :     ( frame == 0 ) ? dbgwrite( &num_elements, sizeof( num_elements ), 1, 1, f_name ) : false;
     736             :     ( frame == 0 ) ? dbgwrite( &num_subframes, sizeof( num_subframes ), 1, 1, f_name ) : false;
     737             :     ( frame == 0 ) ? dbgwrite( &num_block_group, sizeof( num_block_group ), 1, 1, f_name ) : false;
     738             :     ( frame == 0 ) ? dbgwrite( &byte_size, sizeof( byte_size ), 1, 1, f_name ) : false;
     739             : 
     740             :     for ( b = 0; b < nbands; b++ )
     741             :     {
     742             :         for ( int16_t sf = 0; sf < num_subframes; sf++ )
     743             :         {
     744             :             for ( int16_t bl = 0; bl < num_block_group; bl++ )
     745             :             {
     746             :                 dbgwrite( &hSpar->hMdEnc->spar_md.band_coeffs[b].pred_re[0], byte_size, 1, 1, f_name );
     747             :                 dbgwrite( &hSpar->hMdEnc->spar_md.band_coeffs[b].pred_re[1], byte_size, 1, 1, f_name );
     748             :                 dbgwrite( &hSpar->hMdEnc->spar_md.band_coeffs[b].pred_re[2], byte_size, 1, 1, f_name );
     749             :                 dbgwrite( &hSpar->hMdEnc->spar_md.band_coeffs[b].P_re[0], byte_size, 1, 1, f_name );
     750             :                 dbgwrite( &hSpar->hMdEnc->spar_md.band_coeffs[b].P_re[1], byte_size, 1, 1, f_name );
     751             :                 dbgwrite( &hSpar->hMdEnc->spar_md.band_coeffs[b].P_re[2], byte_size, 1, 1, f_name );
     752             :                 // fprintf(stdout, "%f\t%f\t%f\t%d\t%d\n", dirac_md_kbps, spar_md_kbps, sba_md_kbps, qsi, code_strat );
     753             :             }
     754             :         }
     755             :     }
     756             : #endif
     757             : #ifdef DEBUG_LBR_SBA
     758             :     /* Dumping SPAR Coefficients */
     759             :     nbands = 6;
     760             :     num_subframes = 1;
     761             :     num_elements = 6;
     762             :     num_block_group = 1;
     763             :     byte_size = sizeof( float );
     764             : 
     765             :     sprintf( f_name, "SBA_MD_values_quant.bin" );
     766             :     ( frame == 0 ) ? dbgwrite( &nbands, sizeof( nbands ), 1, 1, f_name ) : false;
     767             :     ( frame == 0 ) ? dbgwrite( &num_elements, sizeof( num_elements ), 1, 1, f_name ) : false;
     768             :     ( frame == 0 ) ? dbgwrite( &num_subframes, sizeof( num_subframes ), 1, 1, f_name ) : false;
     769             :     ( frame == 0 ) ? dbgwrite( &num_block_group, sizeof( num_block_group ), 1, 1, f_name ) : false;
     770             :     ( frame == 0 ) ? dbgwrite( &byte_size, sizeof( byte_size ), 1, 1, f_name ) : false;
     771             : 
     772             :     for ( b = 0; b < nbands; b++ )
     773             :     {
     774             :         for ( int16_t sf = 0; sf < num_subframes; sf++ )
     775             :         {
     776             :             for ( int16_t bl = 0; bl < num_block_group; bl++ )
     777             :             {
     778             :                 dbgwrite( &hSpar->hMdEnc->spar_md.band_coeffs[b].pred_quant_re[0], byte_size, 1, 1, f_name );
     779             :                 dbgwrite( &hSpar->hMdEnc->spar_md.band_coeffs[b].pred_quant_re[1], byte_size, 1, 1, f_name );
     780             :                 dbgwrite( &hSpar->hMdEnc->spar_md.band_coeffs[b].pred_quant_re[2], byte_size, 1, 1, f_name );
     781             :                 dbgwrite( &hSpar->hMdEnc->spar_md.band_coeffs[b].P_quant_re[0], byte_size, 1, 1, f_name );
     782             :                 dbgwrite( &hSpar->hMdEnc->spar_md.band_coeffs[b].P_quant_re[1], byte_size, 1, 1, f_name );
     783             :                 dbgwrite( &hSpar->hMdEnc->spar_md.band_coeffs[b].P_quant_re[2], byte_size, 1, 1, f_name );
     784             :                 // fprintf(stdout, "%f\t%f\t%f\t%d\t%d\n", dirac_md_kbps, spar_md_kbps, sba_md_kbps, qsi, code_strat );
     785             :             }
     786             :         }
     787             :     }
     788             : #endif
     789             :     /*-----------------------------------------------------------------------------------------*
     790             :      * FB mixer
     791             :      *-----------------------------------------------------------------------------------------*/
     792             : 
     793      159500 :     ivas_fb_mixer_get_in_out_mapping( hSpar->hFbMixer->fb_cfg, in_out_mixer_map );
     794             : 
     795             : #ifdef DEBUG_SBA_MD_DUMP
     796             :     {
     797             :         int16_t k;
     798             :         static FILE *f_mat = 0;
     799             : 
     800             :         if ( f_mat == 0 )
     801             :             f_mat = fopen( "mixer_mat_enc", "w" );
     802             : 
     803             :         for ( i = 0; i < hSpar->hFbMixer->fb_cfg->num_out_chans; i++ )
     804             :         {
     805             :             for ( j = 0; j < hSpar->hFbMixer->fb_cfg->num_in_chans; j++ )
     806             :             {
     807             :                 for ( k = 0; k < hSpar->hFbMixer->pFb->filterbank_num_bands; k++ )
     808             :                 {
     809             :                     fprintf( f_mat, "%f\n", hSpar->hMdEnc->mixer_mat[i][j][k] );
     810             : 
     811             :                     if ( ( in_out_mixer_map[i][j] == 0 ) && ( fabs( hSpar->hMdEnc->mixer_mat[i][j][k] ) > 1e-20 ) )
     812             :                     {
     813             :                         assert( 0 && "Non zero value in unexpected mixer map!!!" );
     814             :                     }
     815             :                 }
     816             :             }
     817             :         }
     818             :     }
     819             : #endif
     820             : #ifdef DEBUG_SPAR_DIRAC_WRITE_OUT_PRED_PARS
     821             :     {
     822             :         static FILE *fid = 0;
     823             :         int16_t band = 9;
     824             :         if ( !fid )
     825             :         {
     826             :             fid = fopen( "pred_coeffs_enc.txt", "wt" );
     827             :         }
     828             :         fprintf( fid, "%.6f\n", hSpar->hMdEnc->mixer_mat[1][0][band] );
     829             :     }
     830             : #endif
     831             : 
     832      159500 :     ivas_fb_mixer_process( hSpar->hFbMixer, hSpar->hMdEnc->mixer_mat, p_pcm_tmp, input_frame, in_out_mixer_map );
     833             : 
     834             : #ifdef DEBUG_SBA_AUDIO_DUMP
     835             :     ivas_spar_dump_signal_wav( input_frame, p_pcm_tmp, NULL, nchan_transport, spar_foa_enc_wav[1], "ivas_fb_mixer_process()" );
     836             : #endif
     837      159500 :     if ( hSpar->hFbMixer->fb_cfg->active_w_mixing == 0 )
     838             :     {
     839             : 
     840             :         /*cross fade between new active W channels and old passive W channel*/
     841      115249 :         if ( dyn_active_w_flag == 1 )
     842             :         {
     843           0 :             if ( hSpar->hMdEnc->spar_md.prior_dyn_active_w_flag != dyn_active_w_flag )
     844             :             {
     845             :                 float new_w[L_FRAME48k];
     846             : 
     847             :                 /* delayed W */
     848           0 :                 mvr2r( wyzx_del_buf[0], p_pcm_tmp[0], num_del_samples );
     849           0 :                 mvr2r( data_f[0], p_pcm_tmp[0] + num_del_samples, input_frame - num_del_samples );
     850             : 
     851           0 :                 mvr2r( wyzx_del_buf[hSpar->hMdEnc->spar_md.res_ind], new_w, num_del_samples );
     852           0 :                 mvr2r( data_f[hSpar->hMdEnc->spar_md.res_ind], &new_w[num_del_samples], input_frame - num_del_samples );
     853             : 
     854           0 :                 if ( hSpar->hMdEnc->spar_md.prior_dyn_active_w_flag == 0 && dyn_active_w_flag == 1 )
     855             :                 {
     856           0 :                     ivas_fb_mixer_cross_fading( hSpar->hFbMixer, p_pcm_tmp, p_pcm_tmp[0], new_w, 0, input_frame, 0 );
     857             :                 }
     858           0 :                 else if ( hSpar->hMdEnc->spar_md.prior_dyn_active_w_flag == 1 && dyn_active_w_flag == 0 )
     859             :                 {
     860           0 :                     ivas_fb_mixer_cross_fading( hSpar->hFbMixer, p_pcm_tmp, new_w, p_pcm_tmp[0], 0, input_frame, 0 );
     861             :                 }
     862             :             }
     863             :             else
     864             :             {
     865           0 :                 mvr2r( wyzx_del_buf[hSpar->hMdEnc->spar_md.res_ind], p_pcm_tmp[0], num_del_samples );
     866           0 :                 mvr2r( data_f[hSpar->hMdEnc->spar_md.res_ind], p_pcm_tmp[0] + num_del_samples, input_frame - num_del_samples );
     867             :             }
     868             :         }
     869             :         else
     870             :         {
     871             :             /* delayed W */
     872      115249 :             mvr2r( wyzx_del_buf[0], p_pcm_tmp[0], num_del_samples );
     873      115249 :             mvr2r( data_f[0], p_pcm_tmp[0] + num_del_samples, input_frame - num_del_samples );
     874             :         }
     875             : 
     876      345577 :         for ( i = 1; i < hSpar->hFbMixer->fb_cfg->num_out_chans; i++ )
     877             :         {
     878      230328 :             int16_t idx = hSpar->hFbMixer->fb_cfg->remix_order[i];
     879             : 
     880             :             /* delayed, reorderd and accumulated with (negative) prediction from W */
     881      230328 :             v_add( wyzx_del_buf[idx], p_pcm_tmp[i], p_pcm_tmp[i], num_del_samples );
     882      230328 :             v_add( data_f[idx], p_pcm_tmp[i] + num_del_samples, p_pcm_tmp[i] + num_del_samples, input_frame - num_del_samples );
     883             :         }
     884             :     }
     885             : 
     886             :     /*-----------------------------------------------------------------------------------------*
     887             :      * PCA encoder
     888             :      *-----------------------------------------------------------------------------------------*/
     889             : 
     890      159500 :     if ( hSpar->hPCA != NULL )
     891             :     {
     892        1750 :         ivas_pca_enc( hEncoderConfig, hSpar->hPCA, hMetaData, p_pcm_tmp, input_frame, FOA_CHANNELS );
     893             :     }
     894             :     else
     895             :     {
     896      157750 :         if ( ivas_total_brate == PCA_BRATE && sba_order == SBA_FOA_ORDER )
     897             :         {
     898             :             /* write PCA bypass bit */
     899        9870 :             push_next_indice( hMetaData, PCA_MODE_INACTIVE, 1 );
     900             :         }
     901             :     }
     902             : 
     903             : #ifdef DEBUG_LBR_SBA
     904             :     for ( int16_t t = 0; t < 960; t++ )
     905             :     {
     906             :         for ( int16_t c = 0; c < hSpar->hFbMixer->fb_cfg->num_out_chans; c++ )
     907             :         {
     908             :             float val = p_pcm_tmp[c][t]; // / MAX16B_FLT;
     909             :             dbgwrite( &val, sizeof( float ), 1, 1, "int_enc_dmx.raw" );
     910             :         }
     911             :     }
     912             : #endif
     913             : #ifdef DEBUG_SBA_AUDIO_DUMP
     914             :     ivas_spar_dump_signal_wav( input_frame, p_pcm_tmp, NULL, nchan_transport, spar_foa_enc_wav[0], "ivas_pca_enc()" );
     915             : #endif
     916             : 
     917             :     /*-----------------------------------------------------------------------------------------*
     918             :      * AGC
     919             :      *-----------------------------------------------------------------------------------------*/
     920             : 
     921      159500 :     if ( dtx_vad == 1 )
     922             :     {
     923      155673 :         if ( hSpar->AGC_Enable != 0 )
     924             :         {
     925       42108 :             ivas_agc_enc_process( hSpar->hAgcEnc, hMetaData, p_pcm_tmp, p_pcm_tmp, hSpar->hFbMixer->fb_cfg->num_out_chans, hEncoderConfig );
     926             :         }
     927             :     }
     928             : #ifdef DEBUG_SBA_AUDIO_DUMP
     929             :     /* Dump audio signal after ivas_agc_enc_process */
     930             :     ivas_spar_dump_signal_wav( input_frame, p_pcm_tmp, NULL, nchan_transport, spar_foa_enc_wav[2], "ivas_agc_enc_process()" );
     931             : #endif
     932             : 
     933             : #ifdef DEBUG_SPAR_BYPASS_EVS_CODEC
     934             :     {
     935             :         static FILE *fid_enc = 0;
     936             :         static float delay_buf[576 * 4] = { 0 };
     937             :         int16_t smp, ch, buf_idx, framelen = input_frame, delay = 576;
     938             :         if ( !fid_enc )
     939             :         {
     940             :             fid_enc = fopen( "evs_input_float.raw", "wb" );
     941             :         }
     942             : 
     943             :         /* write out buffer */
     944             :         for ( smp = 0; smp < delay * nchan_transport; smp++ )
     945             :         {
     946             :             fwrite( &delay_buf[smp], sizeof( float ), 1, fid_enc );
     947             :         }
     948             : 
     949             :         for ( smp = 0; smp < framelen - delay; smp++ )
     950             :         {
     951             :             for ( ch = 0; ch < nchan_transport; ch++ )
     952             :             {
     953             :                 fwrite( &p_pcm_tmp[ch][smp], sizeof( float ), 1, fid_enc );
     954             :             }
     955             :         }
     956             : 
     957             :         /* update delay buffer*/
     958             :         buf_idx = 0;
     959             :         for ( ; smp < framelen; smp++ )
     960             :         {
     961             :             for ( ch = 0; ch < nchan_transport; ch++ )
     962             :             {
     963             :                 delay_buf[buf_idx++] = p_pcm_tmp[ch][smp];
     964             :             }
     965             :         }
     966             :     }
     967             : #endif
     968             : 
     969             :     /*-----------------------------------------------------------------------------------------*
     970             :      * Re-order the dmx back to ACN/SN3D format
     971             :      *-----------------------------------------------------------------------------------------*/
     972             : 
     973      159500 :     order = remix_order_set[hSpar->hMdEnc->spar_md_cfg.remix_unmix_order];
     974             : 
     975      549328 :     for ( j = 0; j < nchan_transport; j++ )
     976             :     {
     977      389828 :         if ( st_ivas->nchan_transport < 3 )
     978             :         {
     979      126931 :             mvr2r( p_pcm_tmp[j], data_f[j], input_frame );
     980             :         }
     981             :         else
     982             :         {
     983      262897 :             mvr2r( p_pcm_tmp[j], data_f[order[j]], input_frame );
     984             :         }
     985             :     }
     986      407672 :     for ( ; j < IVAS_SPAR_MAX_DMX_CHS; j++ )
     987             :     {
     988      248172 :         set_f( data_f[order[j]], 0.0f, input_frame );
     989             :     }
     990             : 
     991      159500 :     hSpar->hMdEnc->spar_md.prior_dyn_active_w_flag = dyn_active_w_flag;
     992             : 
     993      159500 :     pop_wmops();
     994             : 
     995      159500 :     return error;
     996             : }

Generated by: LCOV version 1.14