LCOV - code coverage report
Current view: top level - lib_enc - ivas_spar_encoder_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ e95243e9e67ddeb69dddf129509de1b3d95b402e Lines: 367 412 89.1 %
Date: 2025-09-14 03:13:15 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 <assert.h>
      35             : #include "options.h"
      36             : #include "ivas_prot_fx.h"
      37             : #include "prot_fx.h"
      38             : #include "ivas_rom_com.h"
      39             : #include "ivas_stat_com.h"
      40             : #include "math.h"
      41             : #include "wmc_auto.h"
      42             : #include "prot_fx_enc.h"
      43             : #ifdef DEBUGGING
      44             : #include "debug.h"
      45             : #endif
      46             : 
      47             : 
      48             : /*-------------------------------------------------------------------*
      49             :  * Local function prototypes
      50             :  *--------------------------------------------------------------------*/
      51             : 
      52             : static ivas_error ivas_spar_enc_process_fx( Encoder_Struct *st_ivas, const ENCODER_CONFIG_HANDLE hEncoderConfig, BSTR_ENC_HANDLE hMetaData, const Word16 front_vad_flag, Word32 *data_fx[], const Word16 q_data );
      53             : 
      54             : /*-------------------------------------------------------------------------
      55             :  * ivas_spar_enc_open()
      56             :  *
      57             :  * Allocate and initialize SPAR encoder handle and sub-handles
      58             :  *------------------------------------------------------------------------*/
      59        1636 : ivas_error ivas_spar_enc_open_fx(
      60             :     Encoder_Struct *st_ivas,        /* i/o: IVAS encoder handle        */
      61             :     const Word16 spar_reconfig_flag /* i  : SPAR reconfiguration flag  */
      62             : )
      63             : {
      64             :     SPAR_ENC_HANDLE hSpar;
      65             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
      66             :     IVAS_FB_CFG *fb_cfg;
      67             :     Word16 nchan_inp, nchan_transport, sba_order_internal;
      68             :     Word16 nchan_fb_in;
      69             :     Word16 table_idx, active_w_mixing;
      70             :     Word32 input_Fs, ivas_total_brate;
      71             :     ivas_error error;
      72             : 
      73        1636 :     hEncoderConfig = st_ivas->hEncoderConfig;
      74        1636 :     error = IVAS_ERR_OK;
      75        1636 :     move32();
      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 :     move16();
      89        1636 :     input_Fs = hEncoderConfig->input_Fs;
      90        1636 :     move32();
      91        1636 :     sba_order_internal = extract_l( L_min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER ) );
      92        1636 :     nchan_inp = ivas_sba_get_nchan_metadata_fx( sba_order_internal, hEncoderConfig->ivas_total_brate );
      93        1636 :     assert( nchan_inp <= hEncoderConfig->nchan_inp );
      94        1636 :     ivas_total_brate = hEncoderConfig->ivas_total_brate;
      95        1636 :     move32();
      96        1636 :     nchan_fb_in = 0;
      97        1636 :     move16();
      98        1636 :     IF( EQ_16( st_ivas->sba_analysis_order, 1 ) )
      99             :     {
     100        1377 :         nchan_fb_in = FOA_CHANNELS;
     101        1377 :         move16();
     102             :     }
     103         259 :     ELSE IF( EQ_16( st_ivas->sba_analysis_order, 2 ) )
     104             :     {
     105          73 :         nchan_fb_in = 9;
     106          73 :         move16();
     107             :     }
     108         186 :     ELSE IF( EQ_16( st_ivas->sba_analysis_order, 3 ) )
     109             :     {
     110         186 :         nchan_fb_in = 11;
     111         186 :         move16();
     112             :     }
     113             :     ELSE
     114             :     {
     115           0 :         assert( 0 && "sba_order must be 1,2, or 3!" );
     116             :     }
     117             : 
     118        1636 :     nchan_transport = ivas_get_sba_num_TCs_fx( hEncoderConfig->ivas_total_brate, sba_order_internal );
     119             : 
     120        1636 :     table_idx = ivas_get_spar_table_idx_fx( ivas_total_brate, sba_order_internal, SPAR_CONFIG_BW, NULL, NULL );
     121             : 
     122             :     /* MD handle */
     123        1636 :     IF( ( error = ivas_spar_md_enc_open_fx( &( hSpar->hMdEnc ), hEncoderConfig, sba_order_internal ) ) != IVAS_ERR_OK )
     124             :     {
     125           0 :         return error;
     126             :     }
     127             : 
     128             :     /* set FB config. */
     129        1636 :     active_w_mixing = ivas_spar_br_table_consts[table_idx].active_w;
     130        1636 :     move16();
     131        1636 :     IF( NE_32( ( error = ivas_fb_set_cfg( &fb_cfg, SBA_FORMAT, nchan_inp, nchan_transport, active_w_mixing, input_Fs, nchan_fb_in ) ), IVAS_ERR_OK ) )
     132             :     {
     133           0 :         return error;
     134             :     }
     135        1636 :     fb_cfg->remix_order = remix_order_set[hSpar->hMdEnc->spar_md_cfg.remix_unmix_order];
     136             : 
     137             :     /* FB mixer handle */
     138        1636 :     IF( NE_32( ( error = ivas_FB_mixer_open_fx( &( hSpar->hFbMixer ), input_Fs, fb_cfg, spar_reconfig_flag ) ), IVAS_ERR_OK ) )
     139             :     {
     140           0 :         return error;
     141             :     }
     142             : 
     143             :     /* Covariance handle */
     144        1636 :     IF( NE_32( ( error = ivas_spar_covar_enc_open_fx( &( hSpar->hCovEnc ), hSpar->hFbMixer->pFb, input_Fs, nchan_inp, COV_SMOOTH_SPAR, hEncoderConfig->ivas_total_brate ) ), IVAS_ERR_OK ) )
     145             :     {
     146           0 :         return error;
     147             :     }
     148        1636 :     IF( !spar_reconfig_flag )
     149             :     {
     150             :         /* Transient Detector handle */
     151         281 :         IF( NE_32( ( error = ivas_transient_det_open_fx( &( hSpar->hTranDet ), input_Fs ) ), IVAS_ERR_OK ) )
     152             :         {
     153           0 :             return error;
     154             :         }
     155             :     }
     156             : 
     157             :     /* initialization */
     158        1636 :     hSpar->hMdEnc->table_idx = -1;
     159        1636 :     move16();
     160             :     /* AGC handle */
     161        1636 :     hSpar->AGC_Enable = ivas_agc_enc_get_flag( nchan_transport );
     162        1636 :     move16();
     163        1636 :     hSpar->hAgcEnc = NULL;
     164        1636 :     IF( hSpar->AGC_Enable )
     165             :     {
     166         567 :         IF( NE_32( ( error = ivas_spar_agc_enc_open_fx( &hSpar->hAgcEnc, input_Fs, nchan_inp ) ), IVAS_ERR_OK ) )
     167             :         {
     168           0 :             return error;
     169             :         }
     170             :     }
     171             : 
     172             :     /* PCA handle */
     173        1636 :     hSpar->hPCA = NULL;
     174        1636 :     IF( hEncoderConfig->Opt_PCA_ON )
     175             :     {
     176           4 :         IF( ( hSpar->hPCA = (PCA_ENC_STATE *) malloc( sizeof( PCA_ENC_STATE ) ) ) == NULL )
     177             :         {
     178           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR PCA encoder" );
     179             :         }
     180           4 :         ivas_pca_enc_init_fx( hSpar->hPCA );
     181             :     }
     182             : 
     183             :     /* initialization */
     184        1636 :     hSpar->hMdEnc->table_idx = -1;
     185        1636 :     move16();
     186             :     /*-----------------------------------------------------------------*
     187             :      * Configuration - set SPAR high-level parameters
     188             :      *-----------------------------------------------------------------*/
     189             : 
     190        1636 :     ivas_spar_config_fx( hEncoderConfig->ivas_total_brate, s_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 );
     191             : 
     192        1636 :     IF( EQ_16( st_ivas->nchan_transport, 1 ) )
     193             :     {
     194         567 :         hEncoderConfig->element_mode_init = IVAS_SCE;
     195             :     }
     196             :     ELSE
     197             :     {
     198        1069 :         hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     199             :     }
     200        1636 :     move16();
     201             :     /*-----------------------------------------------------------------*
     202             :      * Allocate and initialize Front-VAD handle
     203             :      *-----------------------------------------------------------------*/
     204             : 
     205        1636 :     IF( !spar_reconfig_flag )
     206             :     {
     207         281 :         hSpar->front_vad_flag = 0;
     208         281 :         move16();
     209         281 :         hSpar->front_vad_dtx_flag = 0;
     210         281 :         move16();
     211         281 :         hSpar->force_front_vad = 0;
     212         281 :         move16();
     213         281 :         IF( hEncoderConfig->Opt_DTX_ON )
     214             :         {
     215          69 :             IF( NE_32( ( error = front_vad_create_fx( &( hSpar->hFrontVad ), hEncoderConfig ) ), IVAS_ERR_OK ) )
     216             :             {
     217           0 :                 return error;
     218             :             }
     219             : 
     220          69 :             IF( ( hSpar->hCoreCoderVAD = (ENC_CORE_HANDLE) malloc( sizeof( Encoder_State ) ) ) == NULL )
     221             :             {
     222           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CoreCoder structure\n" ) );
     223             :             }
     224             : 
     225          69 :             copy_encoder_config_fx( st_ivas, hSpar->hCoreCoderVAD, 1 );
     226             : 
     227          69 :             hSpar->hCoreCoderVAD->total_brate = hEncoderConfig->ivas_total_brate; /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
     228          69 :             hSpar->hCoreCoderVAD->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
     229          69 :             move32();
     230          69 :             move32();
     231          69 :             IF( NE_32( ( error = init_encoder_ivas_fx( 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 ) )
     232             :             {
     233           0 :                 return error;
     234             :             }
     235             :         }
     236             :         ELSE
     237             :         {
     238         212 :             hSpar->hCoreCoderVAD = NULL;
     239         212 :             hSpar->hFrontVad = NULL;
     240             :         }
     241             :     }
     242             : 
     243             :     /*-----------------------------------------------------------------*
     244             :      * Final assignment
     245             :      *-----------------------------------------------------------------*/
     246             : 
     247        1636 :     st_ivas->hSpar = hSpar;
     248             : 
     249        1636 :     return error;
     250             : }
     251             : /*-------------------------------------------------------------------------
     252             :  * ivas_spar_enc_close()
     253             :  *
     254             :  * Deallocate SPAR encoder handle
     255             :  *------------------------------------------------------------------------*/
     256             : 
     257        1982 : void ivas_spar_enc_close_fx(
     258             :     SPAR_ENC_HANDLE *hSpar,         /* i/o: SPAR encoder handle        */
     259             :     const Word32 input_Fs,          /* i  : input sampling rate        */
     260             :     const Word16 nchan_inp,         /* i  : number of input channels   */
     261             :     const Word16 spar_reconfig_flag /* i  : SPAR reconfiguration flag  */
     262             : )
     263             : {
     264             :     Word16 num_chans;
     265             : 
     266        1982 :     test();
     267        1982 :     IF( hSpar == NULL || *hSpar == NULL )
     268             :     {
     269         346 :         return;
     270             :     }
     271             : 
     272        1636 :     IF( !spar_reconfig_flag )
     273             :     {
     274             :         /* core-coder-VAD handle */
     275         281 :         IF( ( *hSpar )->hCoreCoderVAD != NULL )
     276             :         {
     277          69 :             destroy_core_enc_fx( ( *hSpar )->hCoreCoderVAD );
     278          69 :             ( *hSpar )->hCoreCoderVAD = NULL;
     279             :         }
     280             : 
     281             :         /* front-VAD handle */
     282         281 :         IF( ( *hSpar )->hFrontVad != NULL )
     283             :         {
     284          69 :             front_vad_destroy_fx( &( *hSpar )->hFrontVad );
     285          69 :             ( *hSpar )->hFrontVad = NULL;
     286             :         }
     287             :     }
     288             : 
     289        1636 :     num_chans = ( *hSpar )->hFbMixer->fb_cfg->num_in_chans;
     290        1636 :     move16();
     291        1636 :     assert( num_chans <= nchan_inp );
     292             : 
     293             :     /* MD handle */
     294        1636 :     ivas_spar_md_enc_close_fx( &( *hSpar )->hMdEnc );
     295             : 
     296             :     /* Covar. State handle */
     297        1636 :     ivas_spar_covar_enc_close_fx( &( *hSpar )->hCovEnc, num_chans );
     298             : 
     299             :     /* FB mixer handle */
     300        1636 :     ivas_FB_mixer_close_fx( &( *hSpar )->hFbMixer, input_Fs, spar_reconfig_flag );
     301             : 
     302             :     /* AGC */
     303        1636 :     ivas_spar_agc_enc_close_fx( &( *hSpar )->hAgcEnc );
     304             : 
     305             :     /* PCA */
     306        1636 :     IF( ( *hSpar )->hPCA != NULL )
     307             :     {
     308           4 :         free( ( *hSpar )->hPCA );
     309           4 :         ( *hSpar )->hPCA = NULL;
     310             :     }
     311             : 
     312        1636 :     IF( !spar_reconfig_flag )
     313             :     {
     314             :         /* Trans Det handle */
     315         281 :         ivas_transient_det_close_fx( &( *hSpar )->hTranDet );
     316         281 :         free( ( *hSpar ) );
     317         281 :         ( *hSpar ) = NULL;
     318             :     }
     319             : 
     320        1636 :     return;
     321             : }
     322             : 
     323             : 
     324             : /*-------------------------------------------------------------------*
     325             :  * ivas_spar_enc()
     326             :  *
     327             :  * Principal IVAS SPAR encoder routine
     328             :  *-------------------------------------------------------------------*/
     329             : 
     330      159500 : ivas_error ivas_spar_enc_fx(
     331             :     Encoder_Struct *st_ivas,  /* i/o: IVAS encoder structure              */
     332             :     Word32 *data_fx[],        /* i/o: input/transport audio channel Q11   */
     333             :     const Word16 input_frame, /* i  : input frame length                  */
     334             :     Word16 *nb_bits_metadata, /* i  : number of MD bits written           */
     335             :     BSTR_ENC_HANDLE hMetaData /* o  : MetaData handle                     */
     336             : )
     337             : {
     338             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
     339             :     Encoder_State *st0; /* used for bitstream handling */
     340             :     ivas_error error;
     341      159500 :     error = IVAS_ERR_OK;
     342      159500 :     move32();
     343      159500 :     hEncoderConfig = st_ivas->hEncoderConfig;
     344             : 
     345      159500 :     IF( st_ivas->nSCE > 0 )
     346             :     {
     347       44251 :         st0 = st_ivas->hSCE[0]->hCoreCoder[0];
     348             :     }
     349             :     ELSE
     350             :     {
     351      115249 :         st0 = st_ivas->hCPE[0]->hCoreCoder[0];
     352             :     }
     353             : 
     354      159500 :     test();
     355      159500 :     test();
     356             :     /* Write SBA signaling bits */
     357      159500 :     IF( EQ_32( hEncoderConfig->ivas_format, SBA_FORMAT ) || ( EQ_32( hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) && EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) ) )
     358             :     {
     359             :         /* Write SBA planar flag */
     360      144580 :         push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_planar, SBA_PLANAR_BITS );
     361             : 
     362             :         /* Write SBA order */
     363      144580 :         push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_order, SBA_ORDER_BITS );
     364             :     }
     365             :     ELSE /* ism_mode == ISM_MODE_NONE */
     366             :     {
     367       14920 :         IF( LT_32( hEncoderConfig->ivas_total_brate, IVAS_24k4 ) )
     368             :         {
     369             :             /* Write SBA planar flag */
     370        2881 :             push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_planar, SBA_PLANAR_BITS );
     371             : 
     372             :             /* hack to indicate OSBA bitstream at VLBR */
     373        2881 :             push_indice( st0->hBstr, IND_SMODE, 0, SBA_ORDER_BITS );
     374             : 
     375             :             /* additionally code the real SBA order */
     376        2881 :             push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_order, SBA_ORDER_BITS );
     377             :         }
     378             :         ELSE
     379             :         {
     380             :             /* Write SBA planar flag */
     381       12039 :             push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_planar, SBA_PLANAR_BITS );
     382             :             /* Write SBA order */
     383       12039 :             push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_order, SBA_ORDER_BITS );
     384             :         }
     385             :     }
     386             : 
     387      159500 :     IF( EQ_32( hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) )
     388             :     {
     389             :         /* write the number of objects in ISM_SBA format*/
     390       37000 :         push_indice( hMetaData, IND_ISM_NUM_OBJECTS, hEncoderConfig->nchan_ism - 1, NO_BITS_MASA_ISM_NO_OBJ );
     391             :     }
     392             :     /* front VAD */
     393      159500 :     IF( NE_32( ( error = front_vad_spar_fx( st_ivas->hSpar, data_fx[0], hEncoderConfig, input_frame ) ), IVAS_ERR_OK ) )
     394             :     {
     395           0 :         return error;
     396             :     }
     397             : 
     398      159500 :     IF( hEncoderConfig->sba_planar )
     399             :     {
     400       10250 :         ivas_sba_zero_vert_comp_fx( data_fx, st_ivas->sba_analysis_order, hEncoderConfig->sba_planar, input_frame );
     401             :     }
     402             : 
     403             :     /* SPAR encoder */
     404      159500 :     IF( NE_32( ( error = ivas_spar_enc_process_fx( st_ivas, hEncoderConfig, hMetaData, st_ivas->hSpar->front_vad_flag, data_fx, st_ivas->q_data_fx ) ), IVAS_ERR_OK ) )
     405             :     {
     406           0 :         return error;
     407             :     }
     408             : 
     409      159500 :     *nb_bits_metadata = hMetaData->nb_bits_tot;
     410      159500 :     move16();
     411             : 
     412      159500 :     test();
     413             :     /* Force IVAS front pre-proc decision for higher bitrates */
     414      159500 :     if ( GT_32( hEncoderConfig->ivas_total_brate, SBA_DTX_BITRATE_THRESHOLD ) || hEncoderConfig->Opt_DTX_ON == 0 )
     415             :     {
     416      141710 :         st_ivas->hSpar->front_vad_flag = 0;
     417      141710 :         move16();
     418             :     }
     419             : 
     420      159500 :     return error;
     421             : }
     422             : 
     423             : 
     424             : /*-------------------------------------------------------------------*
     425             :  * ivas_spar_cov_md_process()
     426             :  *
     427             :  * Process call for SPAR covariance and MD encoder
     428             :  *-------------------------------------------------------------------*/
     429             : 
     430      159500 : static ivas_error ivas_spar_cov_md_process_fx(
     431             :     const ENCODER_CONFIG_HANDLE hEncoderConfig,
     432             :     SPAR_ENC_HANDLE hSpar,
     433             :     const IVAS_QMETADATA_HANDLE hQMetaData,
     434             :     BSTR_ENC_HANDLE hMetaData,
     435             :     const Word16 nchan_inp,
     436             :     const Word16 sba_order,
     437             :     Word32 *ppIn_FR_real_fx[IVAS_SPAR_MAX_CH], // q_ppIn_FR
     438             :     Word32 *ppIn_FR_imag_fx[IVAS_SPAR_MAX_CH], // q_ppIn_FR
     439             :     const Word16 transient_det[2],
     440             :     const Word16 dtx_vad,
     441             :     const Word16 nchan_transport,
     442             :     Word16 *dyn_active_w_flag,
     443             :     Word16 q_ppIn_FR )
     444             : {
     445             :     Word16 i, j, i_ts, b, table_idx;
     446             :     Word16 active_w_vlbr;
     447             :     /* note: the actual dimensions of matrixes correspond to num_channels = ivas_sba_get_nchan_metadata( sba_order, ivas_total_brate ); */
     448             :     Word32 *cov_real_fx[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
     449             :     Word32 *cov_dtx_real_fx[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
     450             :     Word16 *q_cov_real[IVAS_SPAR_MAX_CH];
     451             :     Word16 *q_cov_dtx_real[IVAS_SPAR_MAX_CH];
     452             :     Word32 cov_real_buf_fx[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH][IVAS_MAX_NUM_BANDS];
     453             :     Word32 cov_dtx_real_buf_fx[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH][IVAS_MAX_NUM_BANDS];
     454             :     ivas_error error;
     455             : 
     456      159500 :     error = IVAS_ERR_OK;
     457      159500 :     move32();
     458             : 
     459      159500 :     IF( LT_32( hEncoderConfig->ivas_total_brate, IVAS_24k4 ) )
     460             :     {
     461       13553 :         active_w_vlbr = 1;
     462       13553 :         move16();
     463             :     }
     464             :     ELSE
     465             :     {
     466      145947 :         active_w_vlbr = 0;
     467      145947 :         move16();
     468             :     }
     469             : 
     470             :     /*-----------------------------------------------------------------------------------------*
     471             :      * Set SPAR bitrates
     472             :      *-----------------------------------------------------------------------------------------*/
     473             : 
     474      159500 :     table_idx = ivas_get_spar_table_idx_fx( hEncoderConfig->ivas_total_brate, sba_order, SPAR_CONFIG_BW, NULL, NULL );
     475             : 
     476      159500 :     IF( NE_16( hSpar->hMdEnc->table_idx, table_idx ) )
     477             :     {
     478        1954 :         hSpar->hMdEnc->table_idx = table_idx;
     479        1954 :         move16();
     480        1954 :         test();
     481        1954 :         IF( NE_32( hEncoderConfig->ivas_total_brate, hEncoderConfig->last_ivas_total_brate ) && !hSpar->spar_reconfig_flag )
     482             :         {
     483         318 :             IF( NE_32( ( error = ivas_spar_md_enc_init_fx( hSpar->hMdEnc, hEncoderConfig, sba_order ) ), IVAS_ERR_OK ) )
     484             :             {
     485           0 :                 return error;
     486             :             }
     487             :         }
     488             :         ELSE
     489             :         {
     490        1636 :             Word16 num_bands = SPAR_DIRAC_SPLIT_START_BAND;
     491        1636 :             move16();
     492        1636 :             if ( hSpar->hMdEnc->spar_hoa_md_flag != 0 )
     493             :             {
     494         259 :                 num_bands = IVAS_MAX_NUM_BANDS;
     495         259 :                 move16();
     496             :             }
     497        1636 :             ivas_spar_set_bitrate_config_fx( &hSpar->hMdEnc->spar_md_cfg, table_idx, num_bands, hSpar->hMdEnc->spar_hoa_dirac2spar_md_flag, 1, hEncoderConfig->Opt_PCA_ON, hSpar->AGC_Enable );
     498             :         }
     499             :     }
     500             : 
     501             :     /*-----------------------------------------------------------------------------------------*
     502             :      * Covariance process
     503             :      *-----------------------------------------------------------------------------------------*/
     504      899700 :     FOR( i = 0; i < nchan_inp; i++ )
     505             :     {
     506     4668660 :         FOR( j = 0; j < nchan_inp; j++ )
     507             :         {
     508     3928460 :             cov_real_fx[i][j] = cov_real_buf_fx[i][j];
     509     3928460 :             cov_dtx_real_fx[i][j] = cov_dtx_real_buf_fx[i][j];
     510     4352460 :             FOR( b = hSpar->hFbMixer->pFb->filterbank_num_bands; b < IVAS_MAX_NUM_BANDS; b++ )
     511             :             {
     512      424000 :                 cov_real_fx[i][j][b] = 0;
     513      424000 :                 move32();
     514      424000 :                 cov_dtx_real_fx[i][j][b] = 0;
     515      424000 :                 move32();
     516             :             }
     517             :         }
     518      740200 :         IF( ( q_cov_real[i] = (Word16 *) malloc( sizeof( Word16 ) * nchan_inp ) ) == NULL )
     519             :         {
     520           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR COV encoder Fixed" );
     521             :         }
     522      740200 :         set16_fx( q_cov_real[i], Q31, nchan_inp );
     523      740200 :         IF( ( q_cov_dtx_real[i] = (Word16 *) malloc( sizeof( Word16 ) * nchan_inp ) ) == NULL )
     524             :         {
     525           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR COV encoder Fixed" );
     526             :         }
     527      740200 :         set16_fx( q_cov_dtx_real[i], Q31, nchan_inp );
     528             :     }
     529             : 
     530      159500 :     ivas_enc_cov_handler_process_fx( hSpar->hCovEnc, ppIn_FR_real_fx, ppIn_FR_imag_fx, q_ppIn_FR, cov_real_fx, q_cov_real, cov_dtx_real_fx, q_cov_dtx_real, hSpar->hFbMixer->pFb, 0, hSpar->hFbMixer->pFb->filterbank_num_bands, nchan_inp, dtx_vad, transient_det, hSpar->hMdEnc->HOA_md_ind,
     531      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 );
     532             : 
     533      899700 :     FOR( i = 0; i < nchan_inp; i++ )
     534             :     {
     535      740200 :         free( q_cov_real[i] );
     536      740200 :         q_cov_real[i] = NULL;
     537      740200 :         free( q_cov_dtx_real[i] );
     538      740200 :         q_cov_dtx_real[i] = NULL;
     539             :     }
     540      159500 :     test();
     541      159500 :     IF( GT_16( nchan_transport, 1 ) && LE_16( nchan_transport, ( FOA_CHANNELS - 1 ) ) )
     542             :     {
     543       74079 :         push_next_indice( hMetaData, *dyn_active_w_flag, 1 );
     544       74079 :         test();
     545       74079 :         IF( EQ_16( *dyn_active_w_flag, 1 ) && EQ_16( nchan_transport, 2 ) )
     546             :         {
     547           0 :             push_next_indice( hMetaData, sub( hSpar->hMdEnc->spar_md.res_ind, nchan_transport ), 1 );
     548             :         }
     549       74079 :         IF( EQ_16( *dyn_active_w_flag, 1 ) )
     550             :         {
     551           0 :             hSpar->front_vad_flag = 1;
     552           0 :             move16();
     553             :         }
     554             :         ELSE
     555             :         {
     556       74079 :             hSpar->front_vad_flag = hSpar->front_vad_flag;
     557       74079 :             move16();
     558             :         }
     559             :     }
     560             :     ELSE
     561             :     {
     562       85421 :         IF( EQ_16( nchan_transport, FOA_CHANNELS ) )
     563             :         {
     564       41170 :             push_next_indice( hMetaData, 0, 1 );
     565             :         }
     566             :     }
     567             : 
     568             :     /*-----------------------------------------------------------------------------------------*
     569             :      * MetaData encoder
     570             :      *-----------------------------------------------------------------------------------------*/
     571             : 
     572      159500 :     IF( hSpar->hMdEnc->spar_hoa_md_flag == 0 )
     573             :     {
     574      137460 :         IF( NE_32( ( error = ivas_spar_md_enc_process_fx( hSpar->hMdEnc, hEncoderConfig, cov_real_fx, hSpar->hCovEnc->pCov_state->q_cov_real_per_band, cov_dtx_real_fx, hSpar->hCovEnc->pCov_dtx_state->q_cov_real_per_band, hMetaData, dtx_vad, nchan_inp, sba_order, hSpar->hFbMixer->prior_mixer_fx, &hSpar->hFbMixer->q_prior_mixer_fx, *dyn_active_w_flag, hQMetaData->dirac_mono_flag, hSpar->hFbMixer->fb_cfg->num_out_chans ) ), IVAS_ERR_OK ) )
     575             :         {
     576           0 :             return error;
     577             :         }
     578             :     }
     579             : 
     580      159500 :     IF( hSpar->hMdEnc->spar_hoa_dirac2spar_md_flag )
     581             :     {
     582             :         Word16 order;
     583             :         Word32 azi_dirac_fx[IVAS_MAX_NUM_BANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
     584             :         Word32 ele_dirac_fx[IVAS_MAX_NUM_BANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
     585             :         Word32 diffuseness_fx[IVAS_MAX_NUM_BANDS];
     586             :         Word32 Wscale_d_fx[IVAS_MAX_NUM_BANDS];
     587             :         Word16 d_start_band, d_end_band;
     588             :         Word16 dirac_band_idx;
     589             : 
     590      149560 :         d_start_band = hSpar->enc_param_start_band;
     591      149560 :         move16();
     592      149560 :         d_end_band = IVAS_MAX_NUM_BANDS;
     593      149560 :         move16();
     594             : 
     595      798200 :         FOR( b = d_start_band; b < d_end_band; b++ )
     596             :         {
     597      648640 :             dirac_band_idx = sub( hSpar->dirac_to_spar_md_bands[b], d_start_band );
     598     3080564 :             FOR( i_ts = 0; i_ts < hQMetaData->q_direction->cfg.nblocks; i_ts++ )
     599             :             {
     600     2431924 :                 azi_dirac_fx[b][i_ts] = hQMetaData->q_direction->band_data[dirac_band_idx].azimuth_fx[i_ts];
     601     2431924 :                 move32();
     602     2431924 :                 ele_dirac_fx[b][i_ts] = hQMetaData->q_direction->band_data[dirac_band_idx].elevation_fx[i_ts];
     603     2431924 :                 move32();
     604             :             }
     605      648640 :             diffuseness_fx[b] = L_sub( ONE_IN_Q30, hQMetaData->q_direction->band_data[dirac_band_idx].energy_ratio_fx[0] );
     606      648640 :             move32();
     607             :         }
     608      149560 :         test();
     609      149560 :         IF( GE_16( d_start_band, 6 ) && EQ_16( dtx_vad, 1 ) )
     610             :         {
     611      141655 :             Copy32( hSpar->hMdEnc->spar_md.band_coeffs[d_start_band - 1].P_quant_re_fx, hSpar->hMdEnc->spar_md.band_coeffs[d_start_band - 1].P_re_fx, IVAS_SPAR_MAX_CH - 1 );
     612             :         }
     613             : 
     614      798200 :         FOR( b = d_start_band; b < d_end_band; b++ )
     615             :         {
     616      648640 :             Word16 tmp_e, Wscale_d_e = 0;
     617      648640 :             move16();
     618      648640 :             Wscale_d_fx[b] = ONE_IN_Q31;
     619      648640 :             move32();
     620     2875120 :             FOR( i = 1; i < nchan_inp; i++ )
     621             :             {
     622     2226480 :                 Word32 tmp = 0;
     623     2226480 :                 move32();
     624     2226480 :                 tmp_e = 0;
     625     2226480 :                 move16();
     626     2226480 :                 IF( cov_real_fx[0][0][b] != 0 )
     627             :                 {
     628     2116880 :                     tmp = L_deposit_h( BASOP_Util_Divide3232_Scale( cov_real_fx[i][i][b], cov_real_fx[0][0][b], &tmp_e ) );
     629     2116880 :                     tmp_e = add( tmp_e, sub( sub( Q31, hSpar->hCovEnc->pCov_state->q_cov_real_per_band[i][i][b] ), sub( Q31, hSpar->hCovEnc->pCov_state->q_cov_real_per_band[0][0][b] ) ) );
     630             :                 }
     631     2226480 :                 Wscale_d_fx[b] = BASOP_Util_Add_Mant32Exp( tmp, tmp_e, Wscale_d_fx[b], Wscale_d_e, &Wscale_d_e );
     632     2226480 :                 move32();
     633             :             }
     634      648640 :             Wscale_d_fx[b] = L_deposit_h( BASOP_Util_Divide3232_Scale( Wscale_d_fx[b], L_add( 1, sba_order ), &tmp_e ) );
     635      648640 :             move32();
     636      648640 :             tmp_e = add( tmp_e, sub( Wscale_d_e, Q31 ) );
     637      648640 :             Wscale_d_fx[b] = Sqrt32( Wscale_d_fx[b], &tmp_e );
     638      648640 :             move32();
     639      648640 :             Wscale_d_fx[b] = L_shl_sat( Wscale_d_fx[b], sub( tmp_e, Q2 ) ); // saturating to Q29, as final value is limited to 2 below
     640      648640 :             move32();
     641      648640 :             Wscale_d_fx[b] = L_min( ONE_IN_Q30, L_max( Wscale_d_fx[b], ONE_IN_Q29 ) ); // Q29
     642      648640 :             move32();
     643             :         }
     644             : 
     645      149560 :         IF( hSpar->hMdEnc->spar_hoa_md_flag == 0 )
     646             :         {
     647      137460 :             order = sba_order;
     648      137460 :             move16();
     649             :         }
     650             :         ELSE
     651             :         {
     652       12100 :             order = 1;
     653       12100 :             move16();
     654             :         }
     655      149560 :         ivas_get_spar_md_from_dirac_enc_fx( azi_dirac_fx, ele_dirac_fx, diffuseness_fx, 1, hSpar->hMdEnc->mixer_mat_fx, &hSpar->hMdEnc->q_mixer_mat_fx, &hSpar->hMdEnc->spar_md, &hSpar->hMdEnc->spar_md_cfg, d_start_band, d_end_band, order, dtx_vad, Wscale_d_fx, hQMetaData->useLowerRes, active_w_vlbr, *dyn_active_w_flag );
     656             :     }
     657             : 
     658      159500 :     IF( hSpar->hMdEnc->spar_hoa_md_flag )
     659             :     {
     660       22040 :         error = ivas_spar_md_enc_process_fx( hSpar->hMdEnc, hEncoderConfig, cov_real_fx, hSpar->hCovEnc->pCov_state->q_cov_real_per_band, cov_dtx_real_fx, hSpar->hCovEnc->pCov_dtx_state->q_cov_real_per_band, hMetaData, dtx_vad, nchan_inp, sba_order, hSpar->hFbMixer->prior_mixer_fx, &hSpar->hFbMixer->q_prior_mixer_fx, *dyn_active_w_flag, hQMetaData->dirac_mono_flag, hSpar->hFbMixer->fb_cfg->num_out_chans );
     661             :     }
     662             : 
     663      159500 :     return error;
     664             : }
     665             : 
     666             : 
     667             : /*-----------------------------------------------------------------------------------------*
     668             :  * Function ivas_spar_enc_process()
     669             :  *
     670             :  * Process call for SPAR encoder
     671             :  *-----------------------------------------------------------------------------------------*/
     672             : 
     673      159500 : static ivas_error ivas_spar_enc_process_fx(
     674             :     Encoder_Struct *st_ivas,                    /* i/o: IVAS encoder structure          */
     675             :     const ENCODER_CONFIG_HANDLE hEncoderConfig, /* i  : configuration structure         */
     676             :     BSTR_ENC_HANDLE hMetaData,                  /* i/o: MetaData handle                 */
     677             :     const Word16 front_vad_flag,                /* i  : front-VAD decision              */
     678             :     Word32 *data_fx[],                          /* i/o: input/transport audio channels Q(q_data) */
     679             :     const Word16 q_data )
     680             : {
     681             :     Word32 pcm_tmp_fx[DIRAC_MAX_ANA_CHANS][L_FRAME48k * 2];
     682             :     Word32 *p_pcm_tmp_fx[DIRAC_MAX_ANA_CHANS];
     683             :     Word16 q_pcm_fx[DIRAC_MAX_ANA_CHANS];
     684             :     Word16 q_p_pcm_tmp_fx[DIRAC_MAX_ANA_CHANS];
     685             :     Word16 i, j, input_frame, dtx_vad;
     686             :     Word16 transient_det[2];
     687             :     Word16 hodirac_flag;
     688             :     Word32 ivas_total_brate, input_Fs;
     689             :     Word16 nchan_inp, sba_order, nchan_transport;
     690             :     Word16 in_out_mixer_map[IVAS_MAX_FB_MIXER_OUT_CH][IVAS_MAX_SPAR_FB_MIXER_IN_CH];
     691             :     ivas_error error;
     692             :     const Word16 *order;
     693      159500 :     SPAR_ENC_HANDLE hSpar = st_ivas->hSpar;
     694      159500 :     IVAS_QMETADATA_HANDLE hQMetaData = st_ivas->hQMetaData;
     695             :     Word16 ts, l_ts, num_del_samples, b, i_ts;
     696             : 
     697             :     Word32 *ppIn_FR_real_fx[IVAS_SPAR_MAX_CH], *ppIn_FR_imag_fx[IVAS_SPAR_MAX_CH];
     698             :     Word32 wyzx_del_buf[FOA_CHANNELS][IVAS_FB_1MS_48K_SAMP];
     699             :     Word16 dyn_active_w_flag;
     700             :     Word16 nchan_fb_in;
     701             : 
     702      159500 :     push_wmops( "ivas_spar_enc_process" );
     703             : 
     704             :     /*-----------------------------------------------------------------------------------------*
     705             :      * Initialization
     706             :      *-----------------------------------------------------------------------------------------*/
     707             : 
     708      159500 :     error = IVAS_ERR_OK;
     709      159500 :     move32();
     710             : 
     711      159500 :     input_Fs = hEncoderConfig->input_Fs;
     712      159500 :     move32();
     713      159500 :     ivas_total_brate = hEncoderConfig->ivas_total_brate;
     714      159500 :     move32();
     715      159500 :     num_del_samples = hSpar->hFbMixer->fb_cfg->fb_latency;
     716      159500 :     move16();
     717             : 
     718             :     Word32 res_dec, res_frac;
     719      159500 :     iDiv_and_mod_32( input_Fs, FRAMES_PER_SEC, &res_dec, &res_frac, 0 );
     720      159500 :     input_frame = extract_l( res_dec );
     721             : 
     722      159500 :     sba_order = s_min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER );
     723      159500 :     nchan_inp = ivas_sba_get_nchan_metadata_fx( sba_order, hEncoderConfig->ivas_total_brate );
     724      159500 :     assert( nchan_inp <= hEncoderConfig->nchan_inp );
     725             : 
     726      159500 :     nchan_fb_in = hSpar->hFbMixer->fb_cfg->nchan_fb_in;
     727      159500 :     move16();
     728      159500 :     nchan_transport = st_ivas->nchan_transport;
     729      159500 :     move16();
     730             : 
     731      275960 :     FOR( i = FOA_CHANNELS + 1; i < nchan_fb_in; i++ )
     732             :     {
     733      116460 :         Copy32( data_fx[HOA_keep_ind[i]], data_fx[i], input_frame );
     734             :     }
     735             :     /*-----------------------------------------------------------------------------------------*
     736             :      * Transient detector
     737             :      *-----------------------------------------------------------------------------------------*/
     738      159500 :     ivas_transient_det_process_fx( hSpar->hTranDet, data_fx[0], input_frame, transient_det );
     739             : 
     740             : #ifdef DEBUGGING
     741             :     {
     742             :         float a[2];
     743             :         a[0] = transient_det[0];
     744             :         a[1] = transient_det[1];
     745             :         dbgwrite_txt( a, 2, "fixed.txt", NULL );
     746             :     }
     747             : #endif
     748             : 
     749      159500 :     if ( LT_32( ivas_total_brate, IVAS_24k4 ) )
     750             :     {
     751       13553 :         transient_det[1] = 0;
     752       13553 :         move16();
     753             :     }
     754             : 
     755             :     /* store previous input samples for W in local buffer */
     756      159500 :     assert( num_del_samples <= IVAS_FB_1MS_48K_SAMP );
     757      159500 :     IF( hSpar->hFbMixer->fb_cfg->active_w_mixing == 0 )
     758             :     {
     759             :         /* fill delay (1 ms) buffer for all Transport channels */
     760      576245 :         FOR( i = 0; i < FOA_CHANNELS; i++ )
     761             :         {
     762      460996 :             Word16 idx = hSpar->hFbMixer->fb_cfg->remix_order[i];
     763      460996 :             move16();
     764      460996 :             Copy32( &hSpar->hFbMixer->ppFilterbank_prior_input_fx[idx][hSpar->hFbMixer->fb_cfg->prior_input_length - num_del_samples], wyzx_del_buf[idx], num_del_samples );
     765             :         }
     766             :     }
     767             : 
     768             :     /*-----------------------------------------------------------------------------------------*
     769             :      * FB mixer ingest
     770             :      *-----------------------------------------------------------------------------------------*/
     771      936000 :     FOR( i = 0; i < nchan_fb_in; i++ )
     772             :     {
     773      776500 :         p_pcm_tmp_fx[i] = pcm_tmp_fx[i];
     774             :     }
     775             : 
     776             :     /* run Filter Bank overlapping MDFT analysis first, then we can use the temporary buffer for Parameter MDFT analysis*/
     777      159500 :     ivas_fb_mixer_pcm_ingest_fx( hSpar->hFbMixer, data_fx, p_pcm_tmp_fx, input_frame, hSpar->hMdEnc->HOA_md_ind, q_data, q_p_pcm_tmp_fx );
     778      899700 :     FOR( i = 0; i < hSpar->hFbMixer->fb_cfg->num_in_chans; i++ )
     779             :     {
     780             :         /* changing q format to q_data*/
     781      740200 :         scale_sig32( p_pcm_tmp_fx[i], shl( input_frame, 1 ), sub( q_data, q_p_pcm_tmp_fx[i] ) ); // q_data
     782             :     }
     783             : 
     784             :     /* prepare Parameter MDFT analysis */
     785      159500 :     Word16 shift = Q31;
     786      159500 :     move16();
     787      936000 :     FOR( i = 0; i < nchan_fb_in; i++ )
     788             :     {
     789      776500 :         ppIn_FR_real_fx[i] = p_pcm_tmp_fx[i];
     790      776500 :         ppIn_FR_imag_fx[i] = p_pcm_tmp_fx[i] + input_frame;
     791      776500 :         p_pcm_tmp_fx[i] = data_fx[i];
     792      776500 :         shift = s_min( shift, L_norm_arr( hSpar->hFbMixer->ppFilterbank_prior_input_fx[i], hSpar->hFbMixer->fb_cfg->prior_input_length ) );
     793      776500 :         shift = s_min( shift, L_norm_arr( data_fx[i], input_frame ) );
     794             :     }
     795             : 
     796      159500 :     l_ts = shr( input_frame, MAX_PARAM_SPATIAL_SUBFRAMES_LOG2 );
     797             : 
     798      159500 :     Word16 gb = find_guarded_bits_fx( l_ts );
     799      797500 :     FOR( ts = 0; ts < MAX_PARAM_SPATIAL_SUBFRAMES; ts++ )
     800             :     {
     801     3744000 :         FOR( i = 0; i < nchan_fb_in; i++ )
     802             :         {
     803     3106000 :             scale_sig32( data_fx[i], input_frame, shift );                                                                      // Q:q_data+shift
     804     3106000 :             scale_sig32( hSpar->hFbMixer->ppFilterbank_prior_input_fx[i], hSpar->hFbMixer->fb_cfg->prior_input_length, shift ); // Q:q_data+shift
     805             :         }
     806      638000 :         ivas_fb_mixer_get_windowed_fr_fx( hSpar->hFbMixer, p_pcm_tmp_fx, ppIn_FR_real_fx, ppIn_FR_imag_fx, l_ts, l_ts, nchan_fb_in, gb );
     807     3744000 :         FOR( i = 0; i < nchan_fb_in; i++ )
     808             :         {
     809     3106000 :             scale_sig32( data_fx[i], input_frame, negate( shift ) );                                                                      // Q:q_data
     810     3106000 :             scale_sig32( hSpar->hFbMixer->ppFilterbank_prior_input_fx[i], hSpar->hFbMixer->fb_cfg->prior_input_length, negate( shift ) ); // Q:q_data
     811             :         }
     812      638000 :         ivas_fb_mixer_update_prior_input_fx( hSpar->hFbMixer, p_pcm_tmp_fx, l_ts, nchan_fb_in );
     813             : 
     814     3744000 :         FOR( i = 0; i < nchan_fb_in; i++ )
     815             :         {
     816     3106000 :             p_pcm_tmp_fx[i] += l_ts;
     817     3106000 :             ppIn_FR_real_fx[i] += l_ts;
     818     3106000 :             ppIn_FR_imag_fx[i] += l_ts;
     819     3106000 :             q_p_pcm_tmp_fx[i] = sub( q_data, gb ); // updating the q for p_pcm
     820     3106000 :             move16();
     821             :         }
     822             :     }
     823             : 
     824             :     /* turn pointers back to the local buffer, needed for the following processing */
     825      936000 :     FOR( i = 0; i < nchan_fb_in; i++ )
     826             :     {
     827      776500 :         ppIn_FR_real_fx[i] = pcm_tmp_fx[i];
     828      776500 :         ppIn_FR_imag_fx[i] = pcm_tmp_fx[i] + input_frame;
     829      776500 :         p_pcm_tmp_fx[i] = pcm_tmp_fx[i];
     830             :     }
     831      159500 :     IF( EQ_16( hEncoderConfig->Opt_DTX_ON, 1 ) )
     832             :     {
     833       18000 :         dtx_vad = front_vad_flag;
     834       18000 :         move16();
     835             :     }
     836             :     ELSE
     837             :     {
     838      141500 :         dtx_vad = 1;
     839      141500 :         move16();
     840             :     }
     841             : 
     842             :     /*-----------------------------------------------------------------------------------------*
     843             :      * DirAC encoding
     844             :      *-----------------------------------------------------------------------------------------*/
     845             : 
     846      159500 :     hodirac_flag = ivas_get_hodirac_flag_fx( ivas_total_brate, st_ivas->sba_analysis_order );
     847             : 
     848             :     /*Finding max possible q*/
     849             :     Word16 pp_fr_q;
     850      159500 :     pp_fr_q = sub( add( q_data, shift ), gb );
     851      159500 :     Word16 re_q = MAX_16, im_q = MAX_16, FR_q;
     852      159500 :     move16();
     853      159500 :     move16();
     854      936000 :     FOR( i = 0; i < nchan_fb_in; i++ )
     855             :     {
     856      776500 :         re_q = s_min( re_q, L_norm_arr( ppIn_FR_real_fx[i], input_frame ) );
     857      776500 :         im_q = s_min( im_q, L_norm_arr( ppIn_FR_imag_fx[i], input_frame ) );
     858             :     }
     859      159500 :     FR_q = s_min( re_q, im_q );
     860      936000 :     FOR( i = 0; i < nchan_fb_in; i++ )
     861             :     {
     862      776500 :         scale_sig32( ppIn_FR_real_fx[i], input_frame, FR_q ); // Q(FR_q+pp_fr_q)
     863      776500 :         scale_sig32( ppIn_FR_imag_fx[i], input_frame, FR_q ); // Q(FR_q+pp_fr_q)
     864             :     }
     865      159500 :     pp_fr_q = add( FR_q, pp_fr_q );
     866             : 
     867      159500 :     IF( NE_32( ( error = ivas_dirac_enc_fx( st_ivas->hDirAC, hQMetaData, hMetaData, data_fx, ppIn_FR_real_fx, ppIn_FR_imag_fx, pp_fr_q, input_frame, dtx_vad, hEncoderConfig->ivas_format, nchan_transport, hodirac_flag, shift ) ), IVAS_ERR_OK ) )
     868             :     {
     869           0 :         return error;
     870             :     }
     871             : 
     872             :     /* Set Energy Ratio to 0.0 if the mono flag has been set */
     873      159500 :     IF( hQMetaData->dirac_mono_flag )
     874             :     {
     875           0 :         FOR( b = hQMetaData->q_direction->cfg.start_band; b < hQMetaData->q_direction->cfg.nbands; b++ )
     876             :         {
     877           0 :             Word16 loop_count = 1;
     878           0 :             move16();
     879           0 :             IF( EQ_16( dtx_vad, 1 ) )
     880             :             {
     881           0 :                 loop_count = hQMetaData->q_direction[0].cfg.nblocks;
     882           0 :                 move16();
     883             :             }
     884           0 :             FOR( i_ts = 0; i_ts < loop_count; i_ts++ )
     885             :             {
     886           0 :                 hQMetaData->q_direction[0].band_data[b].energy_ratio_fx[i_ts] = 0;
     887           0 :                 move32();
     888             :             }
     889             :         }
     890             :     }
     891             : 
     892             :     /*-----------------------------------------------------------------------------------------*
     893             :      * Covariance and MD processing
     894             :      *-----------------------------------------------------------------------------------------*/
     895      159500 :     IF( NE_32( ( error = ivas_spar_cov_md_process_fx( hEncoderConfig, st_ivas->hSpar, st_ivas->hQMetaData, hMetaData, nchan_inp, sba_order, ppIn_FR_real_fx, ppIn_FR_imag_fx, transient_det, dtx_vad, nchan_transport, &dyn_active_w_flag, pp_fr_q ) ), IVAS_ERR_OK ) )
     896             :     {
     897           0 :         return error;
     898             :     }
     899             : 
     900      159500 :     set16_fx( q_p_pcm_tmp_fx, pp_fr_q, nchan_fb_in );
     901             :     /*-----------------------------------------------------------------------------------------*
     902             :      * FB mixer
     903             :      *-----------------------------------------------------------------------------------------*/
     904             : 
     905      159500 :     ivas_fb_mixer_get_in_out_mapping_fx( hSpar->hFbMixer->fb_cfg, in_out_mixer_map );
     906             : 
     907             : #ifdef DEBUG_SPAR_DIRAC_WRITE_OUT_PRED_PARS
     908             :     {
     909             :         static FILE *fid = 0;
     910             :         int16_t band = 9;
     911             :         if ( !fid )
     912             :         {
     913             :             fid = fopen( "pred_coeffs_enc.txt", "wt" );
     914             :         }
     915             :         fprintf( fid, "%.6f\n", hSpar->hMdEnc->mixer_mat[1][0][band] );
     916             :     }
     917             : #endif
     918             : 
     919      159500 :     ivas_fb_mixer_process_fx( hSpar->hFbMixer, hSpar->hMdEnc->mixer_mat_fx, &hSpar->hMdEnc->q_mixer_mat_fx, p_pcm_tmp_fx, q_p_pcm_tmp_fx, input_frame, in_out_mixer_map );
     920             : 
     921      434079 :     FOR( Word32 ch = ( hSpar->hFbMixer->fb_cfg->active_w_mixing == 0 ); ch < hSpar->hFbMixer->fb_cfg->num_out_chans; ch++ )
     922             :     {
     923             :         /* changing q format to q_data*/
     924      274579 :         scale_sig32( p_pcm_tmp_fx[ch], input_frame, sub( q_data, q_p_pcm_tmp_fx[ch] ) ); // q_data
     925      274579 :         q_p_pcm_tmp_fx[ch] = q_data;
     926      274579 :         move16();
     927             :     }
     928      159500 :     IF( hSpar->hFbMixer->fb_cfg->active_w_mixing == 0 )
     929             :     {
     930             :         /*cross fade between new active W channels and old passive W channel*/
     931      115249 :         IF( EQ_16( dyn_active_w_flag, 1 ) )
     932             :         {
     933           0 :             IF( NE_16( hSpar->hMdEnc->spar_md.prior_dyn_active_w_flag, dyn_active_w_flag ) )
     934             :             {
     935             :                 Word32 new_w[L_FRAME48k];
     936             :                 /* delayed W */
     937           0 :                 Copy32( wyzx_del_buf[0], p_pcm_tmp_fx[0], num_del_samples );
     938           0 :                 Copy32( data_fx[0], p_pcm_tmp_fx[0] + num_del_samples, sub( input_frame, num_del_samples ) );
     939             : 
     940           0 :                 Copy32( wyzx_del_buf[hSpar->hMdEnc->spar_md.res_ind], new_w, num_del_samples );
     941           0 :                 Copy32( data_fx[hSpar->hMdEnc->spar_md.res_ind], &new_w[num_del_samples], sub( input_frame, num_del_samples ) );
     942             : 
     943           0 :                 test();
     944           0 :                 test();
     945           0 :                 IF( hSpar->hMdEnc->spar_md.prior_dyn_active_w_flag == 0 && EQ_16( dyn_active_w_flag, 1 ) )
     946             :                 {
     947           0 :                     ivas_fb_mixer_cross_fading_fx( hSpar->hFbMixer, p_pcm_tmp_fx, p_pcm_tmp_fx[0], new_w, 0, input_frame, 0 );
     948             :                 }
     949           0 :                 ELSE IF( EQ_16( hSpar->hMdEnc->spar_md.prior_dyn_active_w_flag, 1 ) && dyn_active_w_flag == 0 )
     950             :                 {
     951           0 :                     ivas_fb_mixer_cross_fading_fx( hSpar->hFbMixer, p_pcm_tmp_fx, new_w, p_pcm_tmp_fx[0], 0, input_frame, 0 );
     952             :                 }
     953             :             }
     954             :             ELSE
     955             :             {
     956           0 :                 Copy32( wyzx_del_buf[hSpar->hMdEnc->spar_md.res_ind], p_pcm_tmp_fx[0], num_del_samples );
     957           0 :                 Copy32( data_fx[hSpar->hMdEnc->spar_md.res_ind], p_pcm_tmp_fx[0] + num_del_samples, sub( input_frame, num_del_samples ) );
     958             :             }
     959             :         }
     960             :         ELSE
     961             :         {
     962             :             /* delayed W */
     963      115249 :             Copy32( wyzx_del_buf[0], p_pcm_tmp_fx[0], num_del_samples );
     964      115249 :             Copy32( data_fx[0], p_pcm_tmp_fx[0] + num_del_samples, sub( input_frame, num_del_samples ) );
     965             :         }
     966             : 
     967      345577 :         FOR( i = 1; i < hSpar->hFbMixer->fb_cfg->num_out_chans; i++ )
     968             :         {
     969      230328 :             Word16 idx = hSpar->hFbMixer->fb_cfg->remix_order[i];
     970      230328 :             move16();
     971             : 
     972             :             /* delayed, reorderd and accumulated with (negative) prediction from W */
     973      230328 :             v_add_fx( wyzx_del_buf[idx], p_pcm_tmp_fx[i], p_pcm_tmp_fx[i], num_del_samples );
     974      230328 :             v_add_fx( data_fx[idx], p_pcm_tmp_fx[i] + num_del_samples, p_pcm_tmp_fx[i] + num_del_samples, sub( input_frame, num_del_samples ) );
     975             :         }
     976             :     }
     977             :     /*-----------------------------------------------------------------------------------------*
     978             :      * PCA encoder
     979             :      *-----------------------------------------------------------------------------------------*/
     980             : 
     981      159500 :     IF( hSpar->hPCA != NULL )
     982             :     {
     983        1750 :         ivas_pca_enc_fx( hEncoderConfig, hSpar->hPCA, hMetaData, p_pcm_tmp_fx, input_frame, FOA_CHANNELS );
     984             :     }
     985             :     ELSE
     986             :     {
     987      157750 :         test();
     988      157750 :         IF( EQ_32( ivas_total_brate, PCA_BRATE ) && EQ_16( sba_order, SBA_FOA_ORDER ) )
     989             :         {
     990             :             /* write PCA bypass bit */
     991        9870 :             push_next_indice( hMetaData, PCA_MODE_INACTIVE, 1 );
     992             :         }
     993             :     }
     994             : 
     995             : 
     996             :     /*-----------------------------------------------------------------------------------------*
     997             :      * AGC
     998             :      *-----------------------------------------------------------------------------------------*/
     999             : 
    1000      159500 :     IF( EQ_16( dtx_vad, 1 ) )
    1001             :     {
    1002      157895 :         IF( hSpar->AGC_Enable != 0 )
    1003             :         {
    1004       86818 :             FOR( i = 0; i < hSpar->hFbMixer->fb_cfg->num_out_chans; i++ )
    1005             :             {
    1006       43409 :                 q_pcm_fx[i] = q_data;
    1007       43409 :                 move16();
    1008             :             }
    1009             : 
    1010       43409 :             ivas_agc_enc_process_fx( hSpar->hAgcEnc, hMetaData, p_pcm_tmp_fx, p_pcm_tmp_fx, hSpar->hFbMixer->fb_cfg->num_out_chans, hEncoderConfig, q_pcm_fx );
    1011             : 
    1012       86818 :             FOR( i = 0; i < hSpar->hFbMixer->fb_cfg->num_out_chans; i++ )
    1013             :             {
    1014       43409 :                 scale_sig32( p_pcm_tmp_fx[i], input_frame, sub( q_data, q_pcm_fx[i] ) ); // q_data
    1015       43409 :                 q_pcm_fx[i] = q_data;
    1016       43409 :                 move16();
    1017             :             }
    1018             :         }
    1019             :     }
    1020             : 
    1021             : 
    1022             :     /*-----------------------------------------------------------------------------------------*
    1023             :      * Re-order the dmx back to ACN/SN3D format
    1024             :      *-----------------------------------------------------------------------------------------*/
    1025             : 
    1026      159500 :     order = remix_order_set[hSpar->hMdEnc->spar_md_cfg.remix_unmix_order];
    1027      159500 :     move16();
    1028             : 
    1029      549328 :     FOR( j = 0; j < nchan_transport; j++ )
    1030             :     {
    1031      389828 :         IF( LT_16( st_ivas->nchan_transport, 3 ) )
    1032             :         {
    1033      126931 :             Copy32( p_pcm_tmp_fx[j], data_fx[j], input_frame );
    1034             :         }
    1035             :         ELSE
    1036             :         {
    1037      262897 :             Copy32( p_pcm_tmp_fx[j], data_fx[order[j]], input_frame );
    1038             :         }
    1039             :     }
    1040      407672 :     FOR( ; j < IVAS_SPAR_MAX_DMX_CHS; j++ )
    1041             :     {
    1042      248172 :         set32_fx( data_fx[order[j]], 0, input_frame );
    1043             :     }
    1044             : 
    1045      159500 :     hSpar->hMdEnc->spar_md.prior_dyn_active_w_flag = dyn_active_w_flag;
    1046      159500 :     move16();
    1047             : 
    1048      159500 :     pop_wmops();
    1049             : 
    1050      159500 :     return error;
    1051             : }

Generated by: LCOV version 1.14