LCOV - code coverage report
Current view: top level - lib_enc - ivas_mc_paramupmix_enc_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 3b2f07138c61dcf997bbf4165d0882f794b2995f Lines: 420 440 95.5 %
Date: 2025-05-03 01:55:50 Functions: 12 12 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 <math.h>
      34             : #include <assert.h>
      35             : #include "options.h"
      36             : #include "cnst.h"
      37             : #include "rom_enc.h"
      38             : #include "rom_com.h"
      39             : #include "prot_fx.h"
      40             : #include "ivas_prot_fx.h"
      41             : #include "basop_util.h"
      42             : #include "ivas_rom_com_fx.h"
      43             : #include "ivas_cnst.h"
      44             : #include "ivas_rom_com.h"
      45             : #include "ivas_rom_enc.h"
      46             : #include "wmc_auto.h"
      47             : #ifdef DEBUGGING
      48             : #include "debug.h"
      49             : #endif
      50             : 
      51             : /*-------------------------------------------------------------------------
      52             :  * Local function prototypes
      53             :  *------------------------------------------------------------------------*/
      54             : 
      55             : static void ivas_mc_paramupmix_dmx_fx( MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix, Word32 *data_f[], const Word16 input_frame );
      56             : 
      57             : static ivas_error ivas_mc_paramupmix_param_est_enc_fx( MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix, Word32 *input_frame_t_fx[], Word16 Q_input_frame_t, const Word16 input_frame, Word32 alphas_fx[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS], Word16 *exp_alphas, Word32 betas_fx[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS], Word16 *exp_betas );
      58             : 
      59             : static void get_huff_table_fx( const PAR_TYPE par_type, HUFF_TAB *df0, HUFF_TAB *df );
      60             : 
      61             : static void write_huff_bits_fx( const Word32 value, const UWord16 length, UWord16 bit_buffer[MC_PARAMUPMIX_MAX_BITS], Word16 *bit_pos );
      62             : 
      63             : static void huffman_encode_fx( const Word32 *vqPrev, const Word32 *vq, const PAR_TYPE parType, const Word16 nq, UWord16 bit_buffer[MC_PARAMUPMIX_MAX_BITS], Word16 *bit_pos );
      64             : 
      65             : static void put_ec_data_fx( MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix, const Word16 ch, const Word32 pars_fx[IVAS_MAX_NUM_BANDS], Word16 exp_paras, const Word32 alphas_fx[IVAS_MAX_NUM_BANDS], Word16 exp_alphas, const PAR_TYPE parType, UWord16 bit_buffer[MC_PARAMUPMIX_MAX_BITS], Word16 *bit_pos );
      66             : 
      67             : static void quantize_alpha_fx( const Word32 *alpha_fx, Word16 exp_alpha, Word16 *pnq, Word32 aq[IVAS_MAX_NUM_BANDS], Word32 *adeq_fx, Word16 *exp_adeq );
      68             : 
      69             : 
      70             : static void quantize_pars_fx( const Word32 *v_fx, Word16 exp_v, const Word16 nq, const Word32 *data_fx, Word32 vq[IVAS_MAX_NUM_BANDS], Word32 *vdeq_fx, Word16 *exp_vdeq );
      71             : 
      72             : /*-------------------------------------------------------------------------
      73             :  * ivas_mc_paramupmix_enc()
      74             :  *
      75             :  * MC ParamUpmix Encoder main encoding function
      76             :  *------------------------------------------------------------------------*/
      77             : 
      78         790 : void ivas_mc_paramupmix_enc_fx(
      79             :     Encoder_Struct *st_ivas, /* i/o: IVAS Encoder handle             */
      80             :     BSTR_ENC_HANDLE hBStr,   /* i/o: IVAS Metadata bitstream handle  */
      81             :     Word32 *data_fx[],       // st_ivas->q_data_fx
      82             :     const Word16 input_frame /* i  : input frame length              */
      83             : )
      84             : {
      85             :     MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix;
      86             :     Word16 i;
      87             :     UWord16 bit_buffer[MC_PARAMUPMIX_MAX_BITS];
      88             :     Word16 bit_pos;
      89             : 
      90         790 :     push_wmops( "mc_paramupmix_enc" );
      91             : 
      92         790 :     hMCParamUpmix = st_ivas->hMCParamUpmix;
      93         790 :     bit_pos = 0;
      94             : 
      95             :     /* Parameter estimation */
      96             :     Word32 alphas_fx[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS]; // exp_alphas
      97             :     Word32 betas_fx[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS];  // exp_betas
      98         790 :     Word16 exp_alphas = 31, exp_betas = 31;
      99         790 :     move16();
     100         790 :     move16();
     101             : 
     102         790 :     ivas_mc_paramupmix_param_est_enc_fx( hMCParamUpmix, data_fx, st_ivas->q_data_fx, input_frame, alphas_fx, &exp_alphas, betas_fx, &exp_betas );
     103             : 
     104        3950 :     FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
     105             :     {
     106        3160 :         put_ec_data_fx( hMCParamUpmix, i, alphas_fx[i], exp_alphas, NULL, 0, ALPHA, bit_buffer, &bit_pos );
     107        3160 :         put_ec_data_fx( hMCParamUpmix, i, betas_fx[i], exp_betas, alphas_fx[i], exp_alphas, BETA, bit_buffer, &bit_pos );
     108             :     }
     109             : 
     110             :     /* push the PARAM UPMIX MC side info from the temporary buffer into the medatdata bitstream*/
     111         790 :     push_next_bits( hBStr, bit_buffer, bit_pos );
     112             : 
     113             :     /* DMX generation*/
     114         790 :     ivas_mc_paramupmix_dmx_fx( hMCParamUpmix, data_fx, input_frame );
     115             : 
     116         790 :     pop_wmops();
     117             : 
     118         790 :     return;
     119             : }
     120             : 
     121             : 
     122             : /*-------------------------------------------------------------------------
     123             :  * ivas_mc_paramupmix_enc_open()
     124             :  *
     125             :  * Initialize MC ParamUpmix encoder handle
     126             :  *------------------------------------------------------------------------*/
     127          12 : ivas_error ivas_mc_paramupmix_enc_open_fx(
     128             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder handle          */
     129             : )
     130             : {
     131             :     IVAS_FB_CFG *fb_cfg;
     132             :     MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix;
     133             :     Word32 input_Fs;
     134             :     Word32 input_frame;
     135             :     Word16 i, k, b, j;
     136             :     ivas_error error;
     137             : 
     138          12 :     error = IVAS_ERR_OK;
     139          12 :     move16();
     140          12 :     input_Fs = st_ivas->hEncoderConfig->input_Fs;
     141          12 :     move16();
     142          12 :     input_frame = (Word32) extract_l( Mpy_32_32( st_ivas->hEncoderConfig->input_Fs, ONE_BY_FRAMES_PER_SEC_Q31 ) );
     143             : 
     144             :     /* Sanity Checks */
     145          12 :     IF( ( hMCParamUpmix = (MC_PARAMUPMIX_ENC_HANDLE) malloc( sizeof( MC_PARAMUPMIX_ENC_DATA ) ) ) == NULL )
     146             :     {
     147           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MC_MODE_PARAMUPMIX\n" ) );
     148             :     }
     149          60 :     FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
     150             :     {
     151         144 :         FOR( k = 0; k < MC_PARAMUPMIX_NCH; k++ )
     152             :         {
     153          96 :             IF( ( hMCParamUpmix->midside_fx[i][k] = (Word32 *) malloc( sizeof( Word32 ) * input_frame ) ) == NULL )
     154             :             {
     155           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MC_MODE_PARAMUPMIX\n" ) );
     156             :             }
     157          96 :             set_zero_fx( hMCParamUpmix->midside_fx[i][k], (Word16) input_frame );
     158             :         }
     159             :     }
     160          12 :     hMCParamUpmix->first_frame = 1;
     161          12 :     move16();
     162             : 
     163             :     /* MC_LS_SETUP_5_1_2 is the only current configuration */
     164          12 :     st_ivas->nchan_transport = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
     165          12 :     move16();
     166             : 
     167             :     /* set core coder dependent on the number of transport channels */
     168          12 :     SWITCH( st_ivas->nchan_transport )
     169             :     {
     170          12 :         case 8:
     171          12 :             st_ivas->nCPE = 4;
     172          12 :             move16();
     173          12 :             st_ivas->nSCE = 0;
     174          12 :             move16();
     175          12 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     176          12 :             move16();
     177          12 :             break;
     178             :     }
     179             : 
     180             :     /* Transient Detector handle */
     181         108 :     FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
     182             :     {
     183          96 :         IF( NE_32( ( error = ivas_transient_det_open_fx( &( hMCParamUpmix->hTranDet[i] ), input_Fs ) ), IVAS_ERR_OK ) )
     184             :         {
     185           0 :             return error;
     186             :         }
     187             :     }
     188             : 
     189             :     /* set FB config. */
     190             :     /* need to set num output channels to a value > 0 to get pFb != NULL */
     191          12 :     IF( NE_32( ( error = ivas_fb_set_cfg( &fb_cfg, MC_FORMAT, MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH, MC_PARAMUPMIX_COMBINATIONS, 0, input_Fs, 0 ) ), IVAS_ERR_OK ) )
     192             :     {
     193           0 :         return error;
     194             :     }
     195             : 
     196          12 :     fb_cfg->remix_order = mc_paramupmix_fb_remix_order;
     197          12 :     move16();
     198             :     /* override latency, could be moved to ivas_fb_set_cfg */
     199             :     /* assuming parameters are calculated at end of frame, compensate for MCT delay and half of decoder fb */
     200             :     /* still 1.5ms off, since MCT delay is not large enough */
     201             :     /* param at end of frame */
     202          12 :     fb_cfg->prior_input_length = (Word16) ( NS2SA_FX2( input_Fs, 12000000L ) + NS2SA_FX2( input_Fs, DELAY_FB_4_NS / 2 ) - input_frame / 2 - NS2SA_FX2( input_Fs, DELAY_FB_1_NS / 2 ) );
     203          12 :     fb_cfg->prior_input_length = s_max( fb_cfg->prior_input_length, (Word16) input_frame / MAX_PARAM_SPATIAL_SUBFRAMES );
     204             : 
     205             :     /* Allocate and initialize FB mixer handle */
     206          12 :     IF( NE_32( ( error = ivas_FB_mixer_open_fx( &( hMCParamUpmix->hFbMixer ), input_Fs, fb_cfg, 0 ) ), IVAS_ERR_OK ) )
     207             :     {
     208           0 :         return error;
     209             :     }
     210             : 
     211          60 :     FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
     212             :     {
     213             :         /* Covariance handle */
     214          48 :         IF( NE_32( ( error = ivas_spar_covar_enc_open_fx( &( hMCParamUpmix->hCovEnc[i] ), hMCParamUpmix->hFbMixer->pFb, input_Fs, MC_PARAMUPMIX_NCH + 1, COV_SMOOTH_MC, st_ivas->hEncoderConfig->ivas_total_brate ) ), IVAS_ERR_OK ) )
     215             :         {
     216           0 :             return error;
     217             :         }
     218             :     }
     219             : 
     220          60 :     FOR( b = 0; b < MC_PARAMUPMIX_COMBINATIONS; b++ )
     221             :     {
     222          48 :         IF( ( hMCParamUpmix->cov_real_fx[b] = (Word32 ***) malloc( MC_PARAMUPMIX_NCH * sizeof( Word32 ** ) ) ) == NULL )
     223             :         {
     224           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR cov real matrix" );
     225             :         }
     226         144 :         FOR( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
     227             :         {
     228          96 :             IF( ( hMCParamUpmix->cov_real_fx[b][i] = (Word32 **) malloc( MC_PARAMUPMIX_NCH * sizeof( Word32 * ) ) ) == NULL )
     229             :             {
     230           0 :                 return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR cov real matrix" );
     231             :             }
     232         288 :             FOR( j = 0; j < MC_PARAMUPMIX_NCH; j++ )
     233             :             {
     234         192 :                 IF( ( hMCParamUpmix->cov_real_fx[b][i][j] = (Word32 *) malloc( IVAS_MAX_NUM_BANDS * sizeof( Word32 ) ) ) == NULL )
     235             :                 {
     236           0 :                     return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR cov real matrix" );
     237             :                 }
     238             :             }
     239             :         }
     240             : 
     241          48 :         IF( ( hMCParamUpmix->cov_dtx_real_fx[b] = (Word32 ***) malloc( MC_PARAMUPMIX_NCH * sizeof( Word32 ** ) ) ) == NULL )
     242             :         {
     243           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR cov dtx real matrix" );
     244             :         }
     245         144 :         FOR( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
     246             :         {
     247          96 :             IF( ( hMCParamUpmix->cov_dtx_real_fx[b][i] = (Word32 **) malloc( MC_PARAMUPMIX_NCH * sizeof( Word32 * ) ) ) == NULL )
     248             :             {
     249           0 :                 return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR cov dtx real matrix" );
     250             :             }
     251         288 :             FOR( j = 0; j < MC_PARAMUPMIX_NCH; j++ )
     252             :             {
     253         192 :                 IF( ( hMCParamUpmix->cov_dtx_real_fx[b][i][j] = (Word32 *) malloc( IVAS_MAX_NUM_BANDS * sizeof( Word32 ) ) ) == NULL )
     254             :                 {
     255           0 :                     return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR cov dtx real matrix" );
     256             :                 }
     257             :             }
     258             :         }
     259             :     }
     260             : 
     261          12 :     st_ivas->hMCParamUpmix = hMCParamUpmix;
     262             : 
     263          12 :     return error;
     264             : }
     265             : 
     266             : /*-------------------------------------------------------------------------
     267             :  * ivas_mc_paramupmix_enc_close()
     268             :  *
     269             :  * Close MC Param-Upmix encoder handle
     270             :  *------------------------------------------------------------------------*/
     271        1465 : void ivas_mc_paramupmix_enc_close_fx(
     272             :     MC_PARAMUPMIX_ENC_HANDLE *hMCParamUpmix, /* i/o: MC Param-Upmix encoder handle  */
     273             :     const Word32 input_Fs                    /* i  : input sampling rate            */
     274             : )
     275             : {
     276             :     Word16 i, k;
     277             :     Word16 b, j;
     278             : 
     279        1465 :     test();
     280        1465 :     IF( hMCParamUpmix == NULL || *hMCParamUpmix == NULL )
     281             :     {
     282        1453 :         return;
     283             :     }
     284             : 
     285          60 :     FOR( b = 0; b < MC_PARAMUPMIX_COMBINATIONS; b++ )
     286             :     {
     287          48 :         IF( ( *hMCParamUpmix )->cov_real_fx[b] != NULL )
     288             :         {
     289         144 :             FOR( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
     290             :             {
     291          96 :                 IF( ( *hMCParamUpmix )->cov_real_fx[b][i] != NULL )
     292             :                 {
     293         288 :                     FOR( j = 0; j < MC_PARAMUPMIX_NCH; j++ )
     294             :                     {
     295         192 :                         IF( ( *hMCParamUpmix )->cov_real_fx[b][i][j] != NULL )
     296             :                         {
     297         192 :                             free( ( *hMCParamUpmix )->cov_real_fx[b][i][j] );
     298             :                         }
     299             :                     }
     300          96 :                     free( ( *hMCParamUpmix )->cov_real_fx[b][i] );
     301             :                 }
     302             :             }
     303          48 :             free( ( *hMCParamUpmix )->cov_real_fx[b] );
     304             :         }
     305             : 
     306          48 :         IF( ( *hMCParamUpmix )->cov_dtx_real_fx[b] != NULL )
     307             :         {
     308         144 :             FOR( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
     309             :             {
     310          96 :                 IF( ( *hMCParamUpmix )->cov_dtx_real_fx[b][i] != NULL )
     311             :                 {
     312         288 :                     FOR( j = 0; j < MC_PARAMUPMIX_NCH; j++ )
     313             :                     {
     314         192 :                         IF( ( *hMCParamUpmix )->cov_dtx_real_fx[b][i][j] != NULL )
     315             :                         {
     316         192 :                             free( ( *hMCParamUpmix )->cov_dtx_real_fx[b][i][j] );
     317             :                         }
     318             :                     }
     319          96 :                     free( ( *hMCParamUpmix )->cov_dtx_real_fx[b][i] );
     320             :                 }
     321             :             }
     322          48 :             free( ( *hMCParamUpmix )->cov_dtx_real_fx[b] );
     323             :         }
     324             :     }
     325             : 
     326          60 :     FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
     327             :     {
     328         144 :         FOR( k = 0; k < MC_PARAMUPMIX_NCH; k++ )
     329             :         {
     330          96 :             free( ( *hMCParamUpmix )->midside_fx[i][k] );
     331             :         }
     332             :     }
     333             : 
     334         108 :     FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
     335             :     {
     336          96 :         ivas_transient_det_close_fx( &( *hMCParamUpmix )->hTranDet[i] );
     337             :     }
     338             : 
     339          12 :     IF( ( *hMCParamUpmix )->hFbMixer != NULL )
     340             :     {
     341          12 :         ivas_FB_mixer_close_fx( &( *hMCParamUpmix )->hFbMixer, input_Fs, 0 );
     342             :     }
     343             : 
     344          60 :     FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
     345             :     {
     346             :         /* Covariance handle */
     347          48 :         IF( ( *hMCParamUpmix )->hCovEnc[i] != NULL )
     348             :         {
     349          48 :             ivas_spar_covar_enc_close_fx( &( *hMCParamUpmix )->hCovEnc[i], ( MC_PARAMUPMIX_NCH + 1 ) );
     350             :         }
     351             :     }
     352             : 
     353          12 :     free( *hMCParamUpmix );
     354          12 :     *hMCParamUpmix = NULL;
     355             : 
     356          12 :     return;
     357             : }
     358             : 
     359             : /*****************************************************************************************/
     360             : /* local functions                                                                       */
     361             : /*****************************************************************************************/
     362             : 
     363        6320 : static void get_huff_table_fx(
     364             :     const PAR_TYPE par_type,
     365             :     HUFF_TAB *df0,
     366             :     HUFF_TAB *df )
     367             : {
     368        6320 :     SWITCH( par_type )
     369             :     {
     370        3160 :         case ALPHA:
     371        3160 :             df0->value = huff_alpha_table.df0.value;
     372        3160 :             move16();
     373        3160 :             df0->length = huff_alpha_table.df0.length;
     374        3160 :             move16();
     375        3160 :             df->value = huff_alpha_table.df.value;
     376        3160 :             move16();
     377        3160 :             df->length = huff_alpha_table.df.length;
     378        3160 :             move16();
     379        3160 :             break;
     380        3160 :         case BETA:
     381        3160 :             df0->value = huff_beta_table.df0.value;
     382        3160 :             move16();
     383        3160 :             df0->length = huff_beta_table.df0.length;
     384        3160 :             move16();
     385        3160 :             df->value = huff_beta_table.df.value;
     386        3160 :             move16();
     387        3160 :             df->length = huff_beta_table.df.length;
     388        3160 :             move16();
     389        3160 :             break;
     390             :     }
     391             : 
     392        6320 :     return;
     393             : }
     394             : 
     395             : 
     396       75840 : static void write_huff_bits_fx(
     397             :     const Word32 value,
     398             :     const UWord16 length,
     399             :     UWord16 bit_buffer[MC_PARAMUPMIX_MAX_BITS],
     400             :     Word16 *bit_pos )
     401             : {
     402             :     Word16 k;
     403             : 
     404      235311 :     FOR( k = length - 1; k >= 0; k-- )
     405             :     {
     406      159471 :         bit_buffer[( *bit_pos )++] = (UWord16) ( ( L_shr( value, k ) ) & 1 );
     407             :     }
     408             : 
     409       75840 :     return;
     410             : }
     411             : 
     412        6320 : static void huffman_encode_fx(
     413             :     const Word32 *vqPrev,
     414             :     const Word32 *vq,
     415             :     const PAR_TYPE parType,
     416             :     const Word16 nq,
     417             :     UWord16 bit_buffer[MC_PARAMUPMIX_MAX_BITS],
     418             :     Word16 *bit_pos )
     419             : {
     420             :     Word16 iv;
     421             :     Word32 icode;
     422             :     Word16 offset;
     423             :     HUFF_TAB df0, df;
     424             : 
     425        6320 :     get_huff_table_fx( parType, &df0, &df );
     426             : 
     427        6320 :     offset = sub( nq, 1 ); /* range [-(nquant - 1), nquant - 1] */
     428             : 
     429       82160 :     FOR( iv = 0; iv < IVAS_MAX_NUM_BANDS; iv++ )
     430             :     {
     431       75840 :         IF( iv == 0 )
     432             :         {
     433        6320 :             icode = vq[iv];
     434             :         }
     435             :         ELSE
     436             :         {
     437       69520 :             icode = L_add( L_sub( vq[iv], vq[iv - 1] ), offset );
     438             :         }
     439       75840 :         move16();
     440             : 
     441       75840 :         icode = L_add( L_sub( vq[iv], vqPrev[iv] ), offset );
     442             :     }
     443             : 
     444             :     /* Write the bitstream */
     445        6320 :     bit_buffer[( *bit_pos )++] = (UWord16) 0 & 1;
     446        6320 :     move16();
     447       82160 :     FOR( iv = 0; iv < IVAS_MAX_NUM_BANDS; iv++ )
     448             :     {
     449       75840 :         IF( iv == 0 )
     450             :         {
     451        6320 :             icode = vq[iv];
     452        6320 :             move32();
     453        6320 :             write_huff_bits_fx( df0.value[icode], df0.length[icode], bit_buffer, bit_pos );
     454             :         }
     455             :         ELSE
     456             :         {
     457       69520 :             icode = L_add( L_sub( vq[iv], vq[iv - 1] ), offset );
     458       69520 :             write_huff_bits_fx( df.value[icode], df.length[icode], bit_buffer, bit_pos );
     459             :         }
     460             :     }
     461             : 
     462        6320 :     return;
     463             : }
     464             : 
     465        6320 : static void quantize_pars_fx(
     466             :     const Word32 *v_fx, // exp_v
     467             :     Word16 exp_v,
     468             :     const Word16 nq,
     469             :     const Word32 *data_fx, // Q28
     470             :     Word32 vq[IVAS_MAX_NUM_BANDS],
     471             :     Word32 *vdeq_fx, // exp_vdeq
     472             :     Word16 *exp_vdeq )
     473             : {
     474             :     Word16 iv, iq, iq0, iq1;
     475             : 
     476       82160 :     FOR( iv = 0; iv < IVAS_MAX_NUM_BANDS; iv++ )
     477             :     {
     478       75840 :         iq0 = 0;
     479       75840 :         iq1 = sub( nq, 1 );
     480             : 
     481      455040 :         WHILE( ( iq1 - iq0 ) > 1 )
     482             :         {
     483      379200 :             iq = shr( add( iq0, iq1 ), 1 );
     484      379200 :             Word16 cmp_1 = BASOP_Util_Cmp_Mant32Exp( v_fx[iv], exp_v, data_fx[iq], 31 - Q28 );
     485      379200 :             IF( EQ_16( cmp_1, negate( 1 ) ) )
     486             :             {
     487      207008 :                 iq1 = iq;
     488             :             }
     489             :             ELSE
     490             :             {
     491      172192 :                 iq0 = iq;
     492             :             }
     493             :         }
     494             : 
     495       75840 :         Word16 exp_var1 = 0;
     496       75840 :         move16();
     497       75840 :         Word32 var1 = BASOP_Util_Add_Mant32Exp( v_fx[iv], exp_v, L_negate( data_fx[iq0] ), 31 - Q28, &exp_var1 );
     498       75840 :         var1 = L_abs( var1 );
     499             : 
     500       75840 :         Word16 exp_var2 = 0;
     501       75840 :         move16();
     502       75840 :         Word32 var2 = BASOP_Util_Add_Mant32Exp( v_fx[iv], exp_v, L_negate( data_fx[iq1] ), 31 - Q28, &exp_var2 );
     503       75840 :         var2 = L_abs( var2 );
     504             : 
     505       75840 :         Word16 cmp_2 = BASOP_Util_Cmp_Mant32Exp( var1, exp_var1, var2, exp_var2 );
     506             : 
     507       75840 :         IF( EQ_16( cmp_2, negate( 1 ) ) )
     508             :         {
     509       36866 :             vq[iv] = iq0;
     510       36866 :             vdeq_fx[iv] = data_fx[iq0];
     511             :         }
     512             :         ELSE
     513             :         {
     514       38974 :             vq[iv] = iq1;
     515       38974 :             vdeq_fx[iv] = data_fx[iq1];
     516             :         }
     517       75840 :         move16();
     518       75840 :         move32();
     519             :     }
     520             : 
     521        6320 :     *exp_vdeq = sub( 31, Q28 );
     522        6320 :     move16();
     523             : 
     524        6320 :     return;
     525             : }
     526             : 
     527        6320 : static void quantize_alpha_fx(
     528             :     const Word32 *alpha_fx, // 31 - exp_alpha
     529             :     Word16 exp_alpha,
     530             :     Word16 *pnq,
     531             :     Word32 aq[IVAS_MAX_NUM_BANDS],
     532             :     Word32 *adeq_fx, // 31 - exp_adeq
     533             :     Word16 *exp_adeq )
     534             : {
     535             :     Word16 nq;
     536             :     const Word32 *data_fx;
     537             : 
     538        6320 :     nq = ivas_mc_paramupmix_alpha_quant_table_fx.nquant; // Q0
     539        6320 :     move16();
     540        6320 :     data_fx = ivas_mc_paramupmix_alpha_quant_table_fx.data; // Q28
     541             : 
     542        6320 :     quantize_pars_fx( alpha_fx, exp_alpha, nq, data_fx, aq, adeq_fx, exp_adeq );
     543             : 
     544        6320 :     *pnq = nq;
     545        6320 :     move16();
     546             : 
     547        6320 :     return;
     548             : }
     549             : 
     550        3160 : static void quantize_beta_fx(
     551             :     const Word32 *beta_fx,
     552             :     Word16 exp_beta,
     553             :     const Word32 aq[IVAS_MAX_NUM_BANDS],
     554             :     Word16 *pnq,
     555             :     Word32 bq[IVAS_MAX_NUM_BANDS],
     556             :     Word32 *bdeq_fx,
     557             :     Word16 *exp_bdeq )
     558             : {
     559             :     Word16 iv, iq, iq0, iq1;
     560             : 
     561        3160 :     const ACPL_QUANT_TABLE_FX *tables_fx = ivas_mc_paramupmix_beta_quant_table_fx; // Q28
     562             : 
     563             :     ACPL_QUANT_TABLE_FX quant_table_fx;
     564             : 
     565       41080 :     FOR( iv = 0; iv < IVAS_MAX_NUM_BANDS; iv++ )
     566             :     {
     567       37920 :         quant_table_fx = tables_fx[ivas_param_upmx_mx_qmap[aq[iv]]]; // Q28
     568             : 
     569       37920 :         iq0 = 0;
     570       37920 :         move16();
     571       37920 :         iq1 = sub( quant_table_fx.nquant, 1 );
     572             : 
     573      151680 :         WHILE( ( ( iq1 - iq0 ) > 1 ) )
     574             :         {
     575      113760 :             iq = shr( add( iq0, iq1 ), 1 );
     576             : 
     577      113760 :             Word16 cmp_1 = BASOP_Util_Cmp_Mant32Exp( beta_fx[iv], exp_beta, quant_table_fx.data[iq], 31 - Q28 );
     578             : 
     579      113760 :             IF( EQ_16( cmp_1, negate( 1 ) ) )
     580             :             {
     581       88478 :                 iq1 = iq;
     582             :             }
     583             :             ELSE
     584             :             {
     585       25282 :                 iq0 = iq;
     586             :             }
     587      113760 :             move16();
     588             :         }
     589             : 
     590       37920 :         Word16 exp_var1 = 0;
     591       37920 :         move16();
     592       37920 :         Word32 var1 = BASOP_Util_Add_Mant32Exp( beta_fx[iv], exp_beta, L_negate( quant_table_fx.data[iq0] ), 31 - Q28, &exp_var1 );
     593       37920 :         var1 = L_abs( var1 );
     594             : 
     595       37920 :         Word16 exp_var2 = 0;
     596       37920 :         move16();
     597       37920 :         Word32 var2 = BASOP_Util_Add_Mant32Exp( beta_fx[iv], exp_beta, L_negate( quant_table_fx.data[iq1] ), 31 - Q28, &exp_var2 );
     598       37920 :         var2 = L_abs( var2 );
     599             : 
     600       37920 :         Word16 cmp_2 = BASOP_Util_Cmp_Mant32Exp( var1, exp_var1, var2, exp_var2 );
     601             : 
     602       37920 :         IF( EQ_16( cmp_2, negate( 1 ) ) )
     603             :         {
     604       21049 :             bq[iv] = iq0;
     605       21049 :             bdeq_fx[iv] = quant_table_fx.data[iq0]; // Q28
     606             :         }
     607             :         ELSE
     608             :         {
     609       16871 :             bq[iv] = iq1;
     610       16871 :             bdeq_fx[iv] = quant_table_fx.data[iq1]; // Q28
     611             :         }
     612       37920 :         move16();
     613       37920 :         move32();
     614             :     }
     615        3160 :     *exp_bdeq = sub( 31, Q28 );
     616             : 
     617        3160 :     *pnq = ivas_mc_paramupmix_beta_quant_table_fx[0].nquant;
     618        3160 :     move16();
     619             : 
     620        3160 :     return;
     621             : }
     622             : 
     623        6320 : static void put_ec_data_fx(
     624             :     MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix,
     625             :     const Word16 ch,
     626             :     const Word32 pars_fx[IVAS_MAX_NUM_BANDS], // Q(31 - exp_paras)
     627             :     Word16 exp_paras,
     628             :     const Word32 alphas_fx[IVAS_MAX_NUM_BANDS], // Q(31 - exp_alphas)
     629             :     Word16 exp_alphas,
     630             :     const PAR_TYPE parType,
     631             :     UWord16 bit_buffer[MC_PARAMUPMIX_MAX_BITS],
     632             :     Word16 *bit_pos )
     633             : {
     634             :     Word16 nq;
     635             :     Word32 alphaQuant[IVAS_MAX_NUM_BANDS];
     636             :     Word32 betaQuant[IVAS_MAX_NUM_BANDS];
     637             :     Word32 alphaDequant_fx[IVAS_MAX_NUM_BANDS]; // 31 - exp_alphaDequant
     638             :     Word32 betaDequant_fx[IVAS_MAX_NUM_BANDS];  // 31 - exp_betaDequant
     639        6320 :     Word16 exp_alphaDequant = 31, exp_betaDequant = 31;
     640             : 
     641             : 
     642        6320 :     IF( EQ_16( parType, ALPHA ) )
     643             :     {
     644        3160 :         quantize_alpha_fx( pars_fx, exp_paras, &nq, alphaQuant, alphaDequant_fx, &exp_alphaDequant );
     645             :     }
     646             :     ELSE
     647             :     {
     648        3160 :         quantize_alpha_fx( alphas_fx, exp_alphas, &nq, alphaQuant, alphaDequant_fx, &exp_alphaDequant );
     649        3160 :         quantize_beta_fx( pars_fx, exp_paras, alphaQuant, &nq, betaQuant, betaDequant_fx, &exp_betaDequant );
     650             :     }
     651             : 
     652        6320 :     IF( hMCParamUpmix->first_frame )
     653             :     {
     654          96 :         Copy32( &( alphaQuant[0] ), &( hMCParamUpmix->alpha_quant_prev[ch][0] ), IVAS_MAX_NUM_BANDS );
     655          96 :         IF( EQ_16( parType, BETA ) )
     656             :         {
     657          48 :             Copy32( &( betaQuant[0] ), &( hMCParamUpmix->beta_quant_prev[ch][0] ), IVAS_MAX_NUM_BANDS );
     658          48 :             if ( EQ_16( ch, ( MC_PARAMUPMIX_COMBINATIONS - 1 ) ) )
     659             :             {
     660          12 :                 hMCParamUpmix->first_frame = 0;
     661          12 :                 move16();
     662             :             }
     663             :         }
     664             :     }
     665             : 
     666             :     /* Always one parameter set per frame for transient frames. Original PS framing is used internally. */
     667        6320 :     IF( EQ_16( parType, ALPHA ) )
     668             :     {
     669        3160 :         huffman_encode_fx( hMCParamUpmix->alpha_quant_prev[ch], alphaQuant, ALPHA, nq, bit_buffer, bit_pos );
     670             :     }
     671             :     ELSE
     672             :     {
     673        3160 :         huffman_encode_fx( hMCParamUpmix->beta_quant_prev[ch], betaQuant, BETA, nq, bit_buffer, bit_pos );
     674             :     }
     675             : 
     676        6320 :     IF( EQ_16( parType, ALPHA ) )
     677             :     {
     678        3160 :         Copy32( alphaQuant, hMCParamUpmix->alpha_quant_prev[ch], IVAS_MAX_NUM_BANDS );
     679             :     }
     680             :     ELSE
     681             :     {
     682        3160 :         Copy32( betaQuant, hMCParamUpmix->beta_quant_prev[ch], IVAS_MAX_NUM_BANDS );
     683             :     }
     684             : 
     685        6320 :     return;
     686             : }
     687             : 
     688             : /*-------------------------------------------------------------------------
     689             :  * ivas_mc_paramupmix_dmx()
     690             :  *
     691             :  * Computes the time domain down mix signal
     692             :  *------------------------------------------------------------------------*/
     693             : 
     694         790 : static void ivas_mc_paramupmix_dmx_fx(
     695             :     MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix, /* i/o: MC ParamUpmix encoder handle     */
     696             :     Word32 *data_fx[],
     697             :     /* i/o: Input, downmix out               */ // st_ivas->q_data_fx
     698             :     const Word16 input_frame                    /* i  : Input frame length               */
     699             : )
     700             : {
     701             :     Word16 i, l;
     702         790 :     const Word16 chan1s[4] = { 4, 5, 8, 9 };
     703         790 :     const Word16 chan2s[4] = { 6, 7, 10, 11 };
     704         790 :     const Word16 chanOut[4] = { 4, 5, 6, 7 };
     705         790 :     const Word16 chanZero[4] = { 8, 9, 10, 11 };
     706         790 :     move16();
     707         790 :     move16();
     708         790 :     move16();
     709         790 :     move16();
     710         790 :     move16();
     711         790 :     move16();
     712         790 :     move16();
     713         790 :     move16();
     714         790 :     move16();
     715         790 :     move16();
     716         790 :     move16();
     717         790 :     move16();
     718         790 :     move16();
     719         790 :     move16();
     720         790 :     move16();
     721         790 :     move16();
     722             : 
     723             :     /*  boxes = { 0 1 2 3 [4 6] [5 7] [8 10] [9 11] }; */
     724             :     /* 9+11 -> 7 */
     725             :     /* 8+10 -> 6 */
     726             :     /* 5+7  -> 5 */
     727             :     /* 4+6  -> 4 */
     728        3950 :     FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
     729             :     {
     730     3036760 :         FOR( l = 0; l < input_frame; l++ )
     731             :         {
     732             :             /* mid */
     733     3033600 :             hMCParamUpmix->midside_fx[i][0][l] = L_shr( L_add( data_fx[chan1s[i]][l], data_fx[chan2s[i]][l] ), 1 ); // st_ivas->q_data_fx
     734     3033600 :             move32();
     735             :             /* side */
     736     3033600 :             hMCParamUpmix->midside_fx[i][1][l] = L_shr( L_sub( data_fx[chan1s[i]][l], data_fx[chan2s[i]][l] ), 1 ); // st_ivas->q_data_fx
     737     3033600 :             move32();
     738     3033600 :             data_fx[chanOut[i]][l] = hMCParamUpmix->midside_fx[i][0][l];
     739     3033600 :             move32();
     740             :         }
     741             :     }
     742             : 
     743        3950 :     FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
     744             :     {
     745        3160 :         set_zero_fx( data_fx[chanZero[i]], input_frame );
     746             :     }
     747             : 
     748         790 :     return;
     749             : }
     750             : 
     751             : 
     752             : /*-------------------------------------------------------------------------
     753             :  * ivas_mc_paramupmix_param_est_enc()
     754             :  *
     755             :  * run the CLDFB analysis on the input signal
     756             :  * estimate the input and down mix covariances
     757             :  *------------------------------------------------------------------------*/
     758             : 
     759         790 : static ivas_error ivas_mc_paramupmix_param_est_enc_fx(
     760             :     MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix, /* i/o: MC Param-Upmix encoder handle              */
     761             :     Word32 *data_f_fx[],                    // Q_data_f
     762             :     Word16 Q_data_f,
     763             :     const Word16 input_frame,                                         /* i  : Input frame length                         */
     764             :     Word32 alphas_fx[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS], // 31 - exp_alphas
     765             :     Word16 *exp_alphas,
     766             :     Word32 betas_fx[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS], // 31 - exp_betas
     767             :     Word16 *exp_betas )
     768             : {
     769             : 
     770             :     Word32 *pcm_in_fx[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH];
     771             :     Word32 FR_Real_Mid_fx[L_FRAME48k], FR_Imag_Mid_fx[L_FRAME48k];
     772             :     Word32 fr_realbuffer_fx[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH][L_FRAME48k];
     773             :     Word32 fr_imagbuffer_fx[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH][L_FRAME48k];
     774             :     Word32 *p_fr_realbuffer_fx[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH];
     775             :     Word32 *p_fr_imagbuffer_fx[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH];
     776             :     Word32 *cov_real_fx[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];     // q_cov_real
     777             :     Word32 *cov_dtx_real_fx[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH]; // q_cov_dtx_real
     778             :     Word16 *q_cov_real[IVAS_SPAR_MAX_CH];
     779             :     Word16 *q_cov_dtx_real[IVAS_SPAR_MAX_CH];
     780             :     Word32 *pp_in_fr_real_fx[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH], *pp_in_fr_imag_fx[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH]; // q_ppIn_FR
     781             :     Word16 q_ppIn_FR;
     782             : 
     783             :     Word32 rxy_fx, ryy_fx;
     784             :     Word32 rxx_fx;
     785             :     Word32 rxxest_fx, drxx_fx;
     786             :     Word16 exp_drxx;
     787             :     Word16 wetaux_fx, exp_wetaux;
     788             :     Word16 cmat_fx, exp_cmat;
     789             : 
     790             :     Word16 l_ts;
     791             :     Word16 b, i, j, ts, bnd;
     792             :     Word16 maxbands;
     793             :     Word16 transient_det[MC_PARAMUPMIX_COMBINATIONS][2];
     794             :     Word16 transient_det_l[2], transient_det_r[2];
     795         790 :     const Word16 chan1s[MC_PARAMUPMIX_COMBINATIONS] = { 4, 5, 8, 9 };
     796         790 :     const Word16 chan2s[MC_PARAMUPMIX_COMBINATIONS] = { 6, 7, 10, 11 };
     797         790 :     const Word16 HOA_md_ind[IVAS_SPAR_MAX_CH] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
     798         790 :     move16();
     799         790 :     move16();
     800         790 :     move16();
     801         790 :     move16();
     802         790 :     move16();
     803         790 :     move16();
     804         790 :     move16();
     805         790 :     move16();
     806         790 :     move16();
     807         790 :     move16();
     808         790 :     move16();
     809         790 :     move16();
     810         790 :     move16();
     811         790 :     move16();
     812         790 :     move16();
     813         790 :     move16();
     814         790 :     move16();
     815         790 :     move16();
     816         790 :     move16();
     817             : 
     818        3950 :     FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
     819             :     {
     820        3160 :         pcm_in_fx[2 * i] = data_f_fx[chan1s[i]];
     821        3160 :         pcm_in_fx[2 * i + 1] = data_f_fx[chan2s[i]];
     822             : 
     823        3160 :         Scale_sig32( pcm_in_fx[2 * i], input_frame, sub( Q14, Q_data_f ) );     // Q14
     824        3160 :         Scale_sig32( pcm_in_fx[2 * i + 1], input_frame, sub( Q14, Q_data_f ) ); // Q14
     825             :     }
     826             : 
     827             :     /*-----------------------------------------------------------------------------------------*
     828             :      * Transient detector
     829             :      *-----------------------------------------------------------------------------------------*/
     830             : 
     831        3950 :     FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
     832             :     {
     833        3160 :         ivas_transient_det_process_fx( hMCParamUpmix->hTranDet[2 * i], pcm_in_fx[2 * i], input_frame, transient_det_l );
     834        3160 :         ivas_transient_det_process_fx( hMCParamUpmix->hTranDet[2 * i + 1], pcm_in_fx[2 * i + 1], input_frame, transient_det_r );
     835        3160 :         test();
     836        3160 :         transient_det[i][0] = transient_det_l[0] || transient_det_r[0];
     837        3160 :         move16();
     838        3160 :         test();
     839        3160 :         transient_det[i][1] = transient_det_l[0] || transient_det_r[0];
     840        3160 :         move16();
     841             :         /* should probably be transient_det_l[1] || transient_det_r[1] , but choosing 0 reproduces the before merge state */
     842             :     }
     843             : 
     844        7110 :     FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
     845             :     {
     846        6320 :         p_fr_realbuffer_fx[i] = fr_realbuffer_fx[i];
     847        6320 :         p_fr_imagbuffer_fx[i] = fr_imagbuffer_fx[i];
     848             :     }
     849             : 
     850             :     /* prepare Parameter MDFT analysis */
     851        7110 :     FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
     852             :     {
     853        6320 :         pp_in_fr_real_fx[i] = p_fr_realbuffer_fx[i];
     854        6320 :         pp_in_fr_imag_fx[i] = p_fr_imagbuffer_fx[i];
     855             :     }
     856             : 
     857             :     // l_ts = input_frame / MAX_PARAM_SPATIAL_SUBFRAMES;
     858         790 :     l_ts = idiv1616( input_frame, MAX_PARAM_SPATIAL_SUBFRAMES );
     859         790 :     move16();
     860             : 
     861         790 :     Word16 gb = find_guarded_bits_fx( l_ts );
     862             : 
     863        3950 :     FOR( ts = 0; ts < MAX_PARAM_SPATIAL_SUBFRAMES; ts++ )
     864             :     {
     865        3160 :         ivas_fb_mixer_get_windowed_fr_fx( hMCParamUpmix->hFbMixer, pcm_in_fx, pp_in_fr_real_fx, pp_in_fr_imag_fx, l_ts, l_ts, hMCParamUpmix->hFbMixer->fb_cfg->num_in_chans, gb );
     866             : 
     867        3160 :         ivas_fb_mixer_update_prior_input_fx( hMCParamUpmix->hFbMixer, pcm_in_fx, l_ts, hMCParamUpmix->hFbMixer->fb_cfg->num_in_chans );
     868             : 
     869       28440 :         FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
     870             :         {
     871       25280 :             pcm_in_fx[i] += l_ts;
     872       25280 :             pp_in_fr_real_fx[i] += l_ts;
     873       25280 :             pp_in_fr_imag_fx[i] += l_ts;
     874             :         }
     875             :     }
     876             : 
     877        3950 :     FOR( ts = 0; ts < MAX_PARAM_SPATIAL_SUBFRAMES; ts++ )
     878             :     {
     879       28440 :         FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
     880             :         {
     881       25280 :             pcm_in_fx[i] -= l_ts;
     882       25280 :             pp_in_fr_real_fx[i] -= l_ts;
     883       25280 :             pp_in_fr_imag_fx[i] -= l_ts;
     884             :         }
     885             :     }
     886             : 
     887        3950 :     FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
     888             :     {
     889        3160 :         Scale_sig32( pcm_in_fx[2 * i], input_frame, sub( Q_data_f, Q14 ) );
     890        3160 :         Scale_sig32( pcm_in_fx[2 * i + 1], input_frame, sub( Q_data_f, Q14 ) );
     891             :     }
     892             : 
     893             :     /*-----------------------------------------------------------------------------------------*
     894             :      * Covariance process
     895             :      *-----------------------------------------------------------------------------------------*/
     896             : 
     897        3950 :     FOR( b = 0; b < MC_PARAMUPMIX_COMBINATIONS; b++ )
     898             :     {
     899        3160 :         pp_in_fr_real_fx[0] = p_fr_realbuffer_fx[2 * b]; // q_ppIn_FR
     900        3160 :         pp_in_fr_imag_fx[0] = p_fr_imagbuffer_fx[2 * b]; // q_ppIn_FR
     901        3160 :         pp_in_fr_real_fx[1] = FR_Real_Mid_fx;            // q_ppIn_FR
     902        3160 :         pp_in_fr_imag_fx[1] = FR_Imag_Mid_fx;            // q_ppIn_FR
     903             : 
     904        3160 :         v_add_fx( pp_in_fr_real_fx[0], p_fr_realbuffer_fx[2 * b + 1], pp_in_fr_real_fx[1], L_FRAME48k );
     905        3160 :         v_add_fx( pp_in_fr_imag_fx[0], p_fr_imagbuffer_fx[2 * b + 1], pp_in_fr_imag_fx[1], L_FRAME48k );
     906             : 
     907        9480 :         FOR( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
     908             :         {
     909       18960 :             FOR( j = 0; j < MC_PARAMUPMIX_NCH; j++ )
     910             :             {
     911       12640 :                 cov_real_fx[i][j] = hMCParamUpmix->cov_real_fx[b][i][j]; // q_cov_real
     912       12640 :                 move32();
     913       12640 :                 cov_dtx_real_fx[i][j] = hMCParamUpmix->cov_dtx_real_fx[b][i][j]; // q_cov_dtx_real
     914       12640 :                 move32();
     915             :             }
     916        6320 :             IF( ( q_cov_real[i] = (Word16 *) malloc( sizeof( Word16 ) * MC_PARAMUPMIX_NCH ) ) == NULL )
     917             :             {
     918           0 :                 return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR COV encoder Fixed" );
     919             :             }
     920        6320 :             set_s( q_cov_real[i], Q31, MC_PARAMUPMIX_NCH );
     921        6320 :             IF( ( q_cov_dtx_real[i] = (Word16 *) malloc( sizeof( Word16 ) * MC_PARAMUPMIX_NCH ) ) == NULL )
     922             :             {
     923           0 :                 return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR COV encoder Fixed" );
     924             :             }
     925        6320 :             set_s( q_cov_dtx_real[i], Q31, MC_PARAMUPMIX_NCH );
     926             :         }
     927             : 
     928        3160 :         q_ppIn_FR = sub( Q14, gb );
     929        3160 :         ivas_enc_cov_handler_process_fx( hMCParamUpmix->hCovEnc[b], pp_in_fr_real_fx, pp_in_fr_imag_fx, q_ppIn_FR, cov_real_fx, q_cov_real, cov_dtx_real_fx, q_cov_dtx_real, hMCParamUpmix->hFbMixer->pFb, 0, hMCParamUpmix->hFbMixer->pFb->filterbank_num_bands, MC_PARAMUPMIX_NCH, 0 /*dtx_vad*/, transient_det[b], HOA_md_ind, NULL, NULL, NULL, 0, 0 );
     930             : 
     931        9480 :         FOR( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
     932             :         {
     933        6320 :             free( q_cov_real[i] );
     934        6320 :             q_cov_real[i] = NULL;
     935        6320 :             free( q_cov_dtx_real[i] );
     936        6320 :             q_cov_dtx_real[i] = NULL;
     937             :         }
     938             :     }
     939             : 
     940         790 :     maxbands = hMCParamUpmix->hFbMixer->pFb->filterbank_num_bands;
     941         790 :     move16();
     942             :     Word16 exp_alpha_buff[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS];
     943             :     Word16 exp_beta_buff[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS];
     944        3950 :     FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
     945             :     {
     946        3160 :         set16_fx( &exp_alpha_buff[i][0], 0, IVAS_MAX_NUM_BANDS );
     947        3160 :         set16_fx( &exp_beta_buff[i][0], 0, IVAS_MAX_NUM_BANDS );
     948             :     }
     949             : 
     950        3950 :     FOR( b = 0; b < MC_PARAMUPMIX_COMBINATIONS; b++ )
     951             :     {
     952       41080 :         FOR( bnd = 0; bnd < maxbands; bnd++ )
     953             :         {
     954       37920 :             rxy_fx = hMCParamUpmix->cov_real_fx[b][1][0][bnd];
     955       37920 :             move32();
     956       37920 :             ryy_fx = hMCParamUpmix->cov_real_fx[b][1][1][bnd];
     957       37920 :             move32();
     958             : 
     959       37920 :             exp_cmat = 0;
     960       37920 :             move16();
     961       37920 :             cmat_fx = BASOP_Util_Divide3232_Scale( rxy_fx, L_add( ryy_fx, EPSILON_FX ), &exp_cmat );
     962       37920 :             exp_cmat = sub( add( exp_cmat, sub( 31, ( hMCParamUpmix->hCovEnc[b]->pCov_state->q_cov_real_per_band[1][0][bnd] ) ) ), sub( 31, ( hMCParamUpmix->hCovEnc[b]->pCov_state->q_cov_real_per_band[1][1][bnd] ) ) );
     963             : 
     964       37920 :             Word16 alpha_fx = 0, exp_alpha_var = 0;
     965       37920 :             move16();
     966       37920 :             move16();
     967       37920 :             exp_alpha_var = BASOP_Util_Add_MantExp( cmat_fx, add( exp_cmat, 1 ), negate( ONE_IN_Q14 ), 1, &alpha_fx );
     968             : 
     969       37920 :             rxx_fx = hMCParamUpmix->cov_real_fx[b][0][0][bnd];
     970       37920 :             move32();
     971       37920 :             Word32 tmp_2 = L_mult( cmat_fx, cmat_fx ); // exp_cmat * 2
     972       37920 :             rxxest_fx = Mpy_32_32( tmp_2, ryy_fx );
     973             : 
     974       37920 :             exp_drxx = 0;
     975       37920 :             move16();
     976       37920 :             drxx_fx = BASOP_Util_Add_Mant32Exp( rxx_fx, sub( 31, ( hMCParamUpmix->hCovEnc[b]->pCov_state->q_cov_real_per_band[0][0][bnd] ) ), L_negate( rxxest_fx ),
     977       37920 :                                                 add( add( exp_cmat, exp_cmat ), sub( 31, ( hMCParamUpmix->hCovEnc[b]->pCov_state->q_cov_real_per_band[1][1][bnd] ) ) ), &exp_drxx );
     978       37920 :             drxx_fx = L_max( drxx_fx, 0 );
     979             : 
     980       37920 :             exp_wetaux = 0;
     981       37920 :             move16();
     982       37920 :             wetaux_fx = BASOP_Util_Divide3232_Scale( drxx_fx, L_add( ryy_fx, EPSILON_FX ), &exp_wetaux );
     983       37920 :             exp_wetaux = sub( add( exp_wetaux, exp_drxx ), sub( 31, ( hMCParamUpmix->hCovEnc[b]->pCov_state->q_cov_real_per_band[1][1][bnd] ) ) );
     984             : 
     985       37920 :             Word16 sqrt_wetaux_fx = 0, exp_sqrt_wetaux = 0;
     986       37920 :             move16();
     987       37920 :             move16();
     988             : 
     989       37920 :             IF( wetaux_fx != 0 )
     990             :             {
     991       37865 :                 Word16 tmp_4 = 0, exp_tmp4 = 0;
     992       37865 :                 move16();
     993       37865 :                 move16();
     994       37865 :                 BASOP_Util_Sqrt_InvSqrt_MantExp( wetaux_fx, exp_wetaux, &sqrt_wetaux_fx, &exp_sqrt_wetaux, &tmp_4, &exp_tmp4 );
     995             :             }
     996             :             ELSE
     997             :             {
     998          55 :                 exp_sqrt_wetaux = -1;
     999          55 :                 move16();
    1000             :             }
    1001             : 
    1002       37920 :             Word16 exp_betas_var = add( exp_sqrt_wetaux, 1 );
    1003             : 
    1004       37920 :             alphas_fx[b][bnd] = L_deposit_h( alpha_fx );
    1005       37920 :             move16();
    1006       37920 :             betas_fx[b][bnd] = L_deposit_h( sqrt_wetaux_fx );
    1007       37920 :             move16();
    1008       37920 :             exp_alpha_buff[b][bnd] = exp_alpha_var;
    1009       37920 :             move16();
    1010       37920 :             exp_beta_buff[b][bnd] = exp_betas_var;
    1011       37920 :             move16();
    1012             :         }
    1013             :     }
    1014             : 
    1015         790 :     Word16 max_exp_alpha_buff = 0, max_exp_beta_buff = 0;
    1016         790 :     move16();
    1017         790 :     move16();
    1018        3950 :     FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
    1019             :     {
    1020       41080 :         FOR( j = 0; j < maxbands; j++ )
    1021             :         {
    1022       37920 :             max_exp_alpha_buff = s_max( max_exp_alpha_buff, exp_alpha_buff[i][j] );
    1023       37920 :             max_exp_beta_buff = s_max( max_exp_beta_buff, exp_beta_buff[i][j] );
    1024             :         }
    1025             :     }
    1026        3950 :     FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
    1027             :     {
    1028       41080 :         FOR( j = 0; j < maxbands; j++ )
    1029             :         {
    1030       37920 :             alphas_fx[i][j] = L_shr( alphas_fx[i][j], sub( max_exp_alpha_buff, exp_alpha_buff[i][j] ) );
    1031       37920 :             move16();
    1032       37920 :             betas_fx[i][j] = L_shr( betas_fx[i][j], sub( max_exp_beta_buff, exp_beta_buff[i][j] ) );
    1033       37920 :             move16();
    1034             :         }
    1035             :     }
    1036         790 :     *exp_alphas = max_exp_alpha_buff;
    1037         790 :     *exp_betas = max_exp_beta_buff;
    1038         790 :     move16();
    1039         790 :     move16();
    1040             : 
    1041         790 :     IF( LT_16( maxbands, IVAS_MAX_NUM_BANDS ) )
    1042             :     {
    1043           0 :         FOR( b = 0; b < MC_PARAMUPMIX_COMBINATIONS; b++ )
    1044             :         {
    1045           0 :             FOR( bnd = maxbands; bnd < IVAS_MAX_NUM_BANDS; bnd++ )
    1046             :             {
    1047           0 :                 alphas_fx[b][bnd] = 0;
    1048           0 :                 move16();
    1049           0 :                 betas_fx[b][bnd] = 0;
    1050           0 :                 move16();
    1051             :             }
    1052             :         }
    1053             :     }
    1054             : 
    1055             : 
    1056         790 :     return IVAS_ERR_OK;
    1057             : }

Generated by: LCOV version 1.14