LCOV - code coverage report
Current view: top level - lib_enc - ivas_dirac_enc_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ cede165d26d1b794bfc5f5f6f9ec19d4d64a9a3b Lines: 750 793 94.6 %
Date: 2025-11-01 03:16:20 Functions: 10 10 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 <assert.h>
      34             : #include <stdint.h>
      35             : #include "options.h"
      36             : #include <math.h>
      37             : #include "cnst.h"
      38             : #include "prot_fx.h"
      39             : #include "ivas_prot_fx.h"
      40             : #include "ivas_cnst.h"
      41             : #include "ivas_rom_com.h"
      42             : #include "wmc_auto.h"
      43             : 
      44             : 
      45             : /*-------------------------------------------------------------------------
      46             :  * Local function prototypes
      47             :  *------------------------------------------------------------------------*/
      48             : 
      49             : static void computeIntensityVector_enc_fx(
      50             :     const DIRAC_ENC_HANDLE hDirAC,
      51             :     Word32 Cldfb_RealBuffer[DIRAC_MAX_ANA_CHANS][DIRAC_NO_FB_BANDS_MAX],
      52             :     Word32 Cldfb_ImagBuffer[DIRAC_MAX_ANA_CHANS][DIRAC_NO_FB_BANDS_MAX],
      53             :     const Word16 enc_param_start_band, /* i  : first band to process */
      54             :     const Word16 num_frequency_bands,
      55             :     Word32 intensity_real[DIRAC_NUM_DIMS][DIRAC_MAX_NBANDS],
      56             :     Word16 q_cldfb,
      57             :     Word16 q_intensity_real[DIRAC_MAX_NBANDS] );
      58             : 
      59             : 
      60             : /*-------------------------------------------------------------------------
      61             :  * ivas_dirac_enc_open()
      62             :  *
      63             :  * Initialize DirAC handle
      64             :  *------------------------------------------------------------------------*/
      65             : 
      66         282 : ivas_error ivas_dirac_enc_open_fx(
      67             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder handle          */
      68             : )
      69             : {
      70             :     Word16 i, j;
      71             :     DIRAC_ENC_HANDLE hDirAC;
      72             :     Word32 dirac_slot_ns;
      73             :     ivas_error error;
      74             : 
      75         282 :     error = IVAS_ERR_OK;
      76         282 :     move16();
      77             : 
      78         282 :     IF( ( hDirAC = (DIRAC_ENC_HANDLE) malloc( sizeof( DIRAC_ENC_DATA ) ) ) == NULL )
      79             :     {
      80           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
      81             :     }
      82             : 
      83         282 :     IF( ( hDirAC->hConfig = (DIRAC_CONFIG_DATA_HANDLE) malloc( sizeof( DIRAC_CONFIG_DATA ) ) ) == NULL )
      84             :     {
      85           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC Config\n" ) );
      86             :     }
      87         282 :     hDirAC->firstrun_sector_params = 1; /* Q0 */
      88         282 :     move16();
      89             : 
      90         282 :     set32_fx( hDirAC->sec_I_vec_smth_x_fx[0], 0, NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
      91         282 :     set16_fx( hDirAC->sec_I_vec_smth_x_exp[0], 0, NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
      92         282 :     set32_fx( hDirAC->sec_I_vec_smth_y_fx[0], 0, NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
      93         282 :     set16_fx( hDirAC->sec_I_vec_smth_y_exp[0], 0, NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
      94         282 :     set32_fx( hDirAC->sec_I_vec_smth_z_fx[0], 0, NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
      95         282 :     set16_fx( hDirAC->sec_I_vec_smth_z_exp[0], 0, NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
      96         282 :     set32_fx( hDirAC->energy_smth_fx[0], 0, NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
      97         282 :     set16_fx( hDirAC->energy_smth_exp[0], 0, NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
      98         282 :     set32_fx( hDirAC->ele_prev_fx, 0, NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
      99         282 :     set32_fx( hDirAC->azi_prev_fx, 0, NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
     100             :     /*-----------------------------------------------------------------*
     101             :      * DirAC main configuration
     102             :      *-----------------------------------------------------------------*/
     103             : 
     104         282 :     st_ivas->hDirAC = hDirAC;
     105             : 
     106         282 :     IF( ( error = ivas_dirac_config_fx( (void *) st_ivas, ENC ) ) != IVAS_ERR_OK )
     107             :     {
     108           0 :         return error;
     109             :     }
     110             : 
     111             :     /* set FB config. */
     112         282 :     hDirAC->hFbMixer = NULL;
     113             : 
     114        3948 :     FOR( i = 0; i < DIRAC_MAX_NBANDS + 1; i++ )
     115             :     {
     116             :         /* adapt band grouping to from CLDFB to MDFT bands */
     117        3666 :         hDirAC->band_grouping[i] = imult1616( hDirAC->band_grouping[i], CLDFB_TO_MDFT_FAC ); /* Q0 */
     118        3666 :         move16();
     119             :     }
     120         282 :     dirac_slot_ns = DIRAC_SLOT_ENC_NS;
     121         282 :     move16();
     122             :     /* initialize delay for SPAR/DirAC delay synchronization */
     123             : 
     124             :     /* intensity 3-dim */
     125        1128 :     FOR( i = 0; i < DIRAC_NUM_DIMS; i++ )
     126             :     {
     127         846 :         IF( ( hDirAC->direction_vector_m_fx[i] = (Word32 **) malloc( MAX_PARAM_SPATIAL_SUBFRAMES * sizeof( Word32 * ) ) ) == NULL )
     128             :         {
     129           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     130             :         }
     131             : 
     132        4230 :         FOR( j = 0; j < MAX_PARAM_SPATIAL_SUBFRAMES; j++ )
     133             :         {
     134        3384 :             IF( ( hDirAC->direction_vector_m_fx[i][j] = (Word32 *) malloc( DIRAC_MAX_NBANDS * sizeof( Word32 ) ) ) == NULL )
     135             :             {
     136           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     137             :             }
     138        3384 :             set32_fx( hDirAC->direction_vector_m_fx[i][j], 0, DIRAC_MAX_NBANDS );
     139             :         }
     140             :     }
     141             : 
     142         282 :     Word16 tmp_e = 0;
     143         282 :     move16();
     144         282 :     Word16 tmp = BASOP_Util_Divide3232_Scale( (Word32) DIRAC_NO_COL_AVG_DIFF_NS, dirac_slot_ns, &tmp_e ); /* exp(tmp_e) */
     145         282 :     hDirAC->no_col_avg_diff = shr( tmp, sub( 15, tmp_e ) );
     146         282 :     move16();
     147             : 
     148         282 :     set16_fx( (Word16 *) hDirAC->buffer_energy_q, 0, DIRAC_NO_COL_AVG_DIFF * DIRAC_MAX_NBANDS );
     149         282 :     set16_fx( (Word16 *) hDirAC->buffer_intensity_real_q, 0, DIRAC_NUM_DIMS * DIRAC_NO_COL_AVG_DIFF * DIRAC_MAX_NBANDS );
     150             : 
     151        1128 :     FOR( i = 0; i < DIRAC_NUM_DIMS; i++ )
     152             :     {
     153         846 :         IF( ( hDirAC->buffer_intensity_real_fx[i] = (Word32 **) malloc( hDirAC->no_col_avg_diff * sizeof( Word32 * ) ) ) == NULL )
     154             :         {
     155           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     156             :         }
     157        7614 :         FOR( j = 0; j < hDirAC->no_col_avg_diff; j++ )
     158             :         {
     159        6768 :             IF( ( hDirAC->buffer_intensity_real_fx[i][j] = (Word32 *) malloc( DIRAC_MAX_NBANDS * sizeof( Word32 ) ) ) == NULL )
     160             :             {
     161           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     162             :             }
     163        6768 :             set32_fx( hDirAC->buffer_intensity_real_fx[i][j], 0, DIRAC_MAX_NBANDS );
     164        6768 :             set16_fx( hDirAC->buffer_intensity_real_q[i][j], Q31, hDirAC->hConfig->nbands );
     165             :         }
     166             :     }
     167             : 
     168         282 :     IF( ( hDirAC->buffer_energy_fx = (Word32 *) malloc( DIRAC_MAX_NBANDS * hDirAC->no_col_avg_diff * sizeof( Word32 ) ) ) == NULL )
     169             :     {
     170           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     171             :     }
     172         282 :     set32_fx( hDirAC->buffer_energy_fx, 0, DIRAC_MAX_NBANDS * hDirAC->no_col_avg_diff );
     173         282 :     set16_fx( hDirAC->buffer_energy_q, Q31, DIRAC_MAX_NBANDS * hDirAC->no_col_avg_diff );
     174             : 
     175         282 :     IF( st_ivas->hQMetaData->useLowerRes )
     176             :     {
     177          37 :         hDirAC->block_grouping[0] = 0;
     178          37 :         hDirAC->block_grouping[1] = MAX_PARAM_SPATIAL_SUBFRAMES;
     179          37 :         move16();
     180          37 :         move16();
     181             :     }
     182             :     ELSE
     183             :     {
     184         245 :         Copy( DirAC_block_grouping_5ms_MDFT, hDirAC->block_grouping, MAX_PARAM_SPATIAL_SUBFRAMES + 1 );
     185             :     }
     186             : 
     187         282 :     hDirAC->index_buffer_intensity = 0;
     188         282 :     hDirAC->mono_frame_count = 0;
     189         282 :     move16();
     190         282 :     move16();
     191             : 
     192         282 :     st_ivas->hDirAC = hDirAC;
     193         282 :     st_ivas->hSpar->enc_param_start_band = st_ivas->hDirAC->hConfig->enc_param_start_band; /* Q0 */
     194         282 :     move16();
     195             : 
     196         282 :     return error;
     197             : }
     198             : 
     199             : 
     200             : /*-------------------------------------------------------------------------
     201             :  * ivas_dirac_enc_reconfigure()
     202             :  *
     203             :  * Reconfigure DirAC handle
     204             :  *------------------------------------------------------------------------*/
     205             : 
     206        1673 : ivas_error ivas_dirac_enc_reconfigure(
     207             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder handle          */
     208             : )
     209             : {
     210             :     DIRAC_ENC_HANDLE hDirAC;
     211             :     ivas_error error;
     212             : 
     213        1673 :     error = IVAS_ERR_OK;
     214        1673 :     move32();
     215             : 
     216        1673 :     hDirAC = st_ivas->hDirAC;
     217             : 
     218             :     /*-----------------------------------------------------------------*
     219             :      * DirAC main configuration
     220             :      *-----------------------------------------------------------------*/
     221             : 
     222        1673 :     IF( NE_32( ( error = ivas_dirac_config_fx( (void *) st_ivas, ENC ) ), IVAS_ERR_OK ) )
     223             :     {
     224           0 :         return error;
     225             :     }
     226             : 
     227        1673 :     IF( st_ivas->hQMetaData->useLowerRes )
     228             :     {
     229         359 :         hDirAC->block_grouping[0] = 0;
     230         359 :         move16();
     231         359 :         hDirAC->block_grouping[1] = MAX_PARAM_SPATIAL_SUBFRAMES;
     232         359 :         move16();
     233             :     }
     234             :     ELSE
     235             :     {
     236        1314 :         MVR2R_WORD16( DirAC_block_grouping_5ms_MDFT, hDirAC->block_grouping, MAX_PARAM_SPATIAL_SUBFRAMES + 1 );
     237             :     }
     238             : 
     239        1673 :     return error;
     240             : }
     241             : 
     242             : 
     243             : /*-------------------------------------------------------------------------
     244             :  * ivas_dirac_enc_close()
     245             :  *
     246             :  * Close DirAC encoder handle
     247             :  *------------------------------------------------------------------------*/
     248         628 : void ivas_dirac_enc_close_fx(
     249             :     DIRAC_ENC_HANDLE *hDirAC_out, /* i/o: encoder DirAC handle     */
     250             :     const Word32 input_Fs         /* i  : input sampling rate      Q0*/
     251             : )
     252             : {
     253             :     Word16 i, j;
     254             :     DIRAC_ENC_HANDLE hDirAC;
     255             : 
     256         628 :     test();
     257         628 :     IF( hDirAC_out == NULL || *hDirAC_out == NULL )
     258             :     {
     259         346 :         return;
     260             :     }
     261             : 
     262         282 :     hDirAC = *hDirAC_out;
     263             : 
     264         282 :     IF( hDirAC->hFbMixer != NULL )
     265             :     {
     266           0 :         ivas_FB_mixer_close_fx( &hDirAC->hFbMixer, input_Fs, 0 );
     267             :     }
     268             :     /* intensity 3-dim */
     269        1128 :     FOR( i = 0; i < DIRAC_NUM_DIMS; i++ )
     270             :     {
     271        4230 :         FOR( j = 0; j < MAX_PARAM_SPATIAL_SUBFRAMES; j++ )
     272             :         {
     273        3384 :             free( hDirAC->direction_vector_m_fx[i][j] );
     274        3384 :             hDirAC->direction_vector_m_fx[i][j] = NULL;
     275             :         }
     276             : 
     277        7614 :         FOR( j = 0; j < hDirAC->no_col_avg_diff; j++ )
     278             :         {
     279        6768 :             IF( hDirAC->buffer_intensity_real_fx[i][j] != 0 )
     280             :             {
     281        6768 :                 free( hDirAC->buffer_intensity_real_fx[i][j] );
     282        6768 :                 hDirAC->buffer_intensity_real_fx[i][j] = NULL;
     283             :             }
     284             :         }
     285             : 
     286         846 :         free( hDirAC->buffer_intensity_real_fx[i] );
     287         846 :         hDirAC->buffer_intensity_real_fx[i] = NULL;
     288             : 
     289         846 :         free( hDirAC->direction_vector_m_fx[i] );
     290         846 :         hDirAC->direction_vector_m_fx[i] = NULL;
     291             :     }
     292             : 
     293         282 :     free( hDirAC->buffer_energy_fx );
     294         282 :     hDirAC->buffer_energy_fx = NULL;
     295             : 
     296         282 :     IF( hDirAC->hConfig != NULL )
     297             :     {
     298         282 :         free( hDirAC->hConfig );
     299         282 :         hDirAC->hConfig = NULL;
     300             :     }
     301             : 
     302         282 :     free( *hDirAC_out );
     303         282 :     *hDirAC_out = NULL;
     304             : 
     305         282 :     return;
     306             : }
     307             : 
     308             : /*-------------------------------------------------------------------------
     309             :  * ivas_dirac_enc()
     310             :  *
     311             :  * DirAC Encoder
     312             :  *
     313             :  *------------------------------------------------------------------------*/
     314             : 
     315      160500 : ivas_error ivas_dirac_enc_fx(
     316             :     DIRAC_ENC_HANDLE hDirAC,          /* i/o: encoder DirAC handle                          */
     317             :     IVAS_QMETADATA_HANDLE hQMetaData, /* i/o: q_metadata handle                             */
     318             :     BSTR_ENC_HANDLE hMetaData,        /* i/o: Metadata bitstream handle                     */
     319             :     Word32 *data_f_fx[],              /* i/o: SBA channels                                                                      Qx*/
     320             :     Word32 **pp_fr_real_fx,           /* o  : real freq domain values                                           pp_fr_q*/
     321             :     Word32 **pp_fr_imag_fx,           /* o  : imag freq domain values                                           pp_fr_q*/
     322             :     Word16 pp_fr_q,
     323             :     const Word16 input_frame,      /* i  : input frame length                                                           Q0*/
     324             :     const Word16 dtx_vad,          /* i  : DTX vad flag                                                                         Q0*/
     325             :     const IVAS_FORMAT ivas_format, /* i  : ivas format                                                                          */
     326             :     const Word16 nchan_transport,  /* i  : number of transport channels                     */
     327             :     const Word16 hodirac_flag,     /* i  : hodirac flag                                                                         Q0*/
     328             :     const Word16 shift )
     329             : {
     330             :     Word16 orig_dirac_bands;
     331             :     Word32 dir_fx[3], avg_dir_fx[3];
     332             :     Word16 i, j, b, i_ts;
     333             :     ivas_error error;
     334      160500 :     push_wmops( "ivas_dirac_enc" );
     335             : 
     336             :     Word16 nchan_fb_in;
     337             : 
     338      160500 :     IF( hodirac_flag )
     339             :     {
     340       16240 :         nchan_fb_in = HOA2_CHANNELS;
     341       16240 :         move16();
     342             :     }
     343             :     ELSE
     344             :     {
     345      144260 :         nchan_fb_in = FOA_CHANNELS;
     346      144260 :         move16();
     347             :     }
     348             : 
     349      160500 :     ivas_dirac_param_est_enc_fx( hDirAC, hQMetaData->q_direction, hQMetaData->useLowerRes, data_f_fx, pp_fr_real_fx, pp_fr_imag_fx, pp_fr_q, input_frame, ivas_format, hodirac_flag, nchan_fb_in, &( hDirAC->mono_frame_count ), &( hQMetaData->dirac_mono_flag ), shift );
     350             : 
     351      160500 :     IF( hQMetaData->q_direction->cfg.nbands > 0 )
     352             :     {
     353      160500 :         orig_dirac_bands = hQMetaData->q_direction[0].cfg.nbands; /* Q0 */
     354      160500 :         move16();
     355             : 
     356      160500 :         IF( EQ_16( dtx_vad, 1 ) )
     357             :         {
     358             :             /* WB 4TC mode bit  : disable for now*/
     359      158895 :             push_next_indice( hMetaData, 0, 1 );
     360             : 
     361             :             /* Set Energy Ratio to 0.0 if the mono flag is set, before the metadata is encoded */
     362      158895 :             IF( hQMetaData->dirac_mono_flag )
     363             :             {
     364          64 :                 IF( EQ_16( dtx_vad, 1 ) )
     365             :                 {
     366         256 :                     FOR( b = hQMetaData->q_direction->cfg.start_band; b < hQMetaData->q_direction->cfg.nbands; b++ )
     367             :                     {
     368         960 :                         FOR( i_ts = 0; i_ts < hQMetaData->q_direction[0].cfg.nblocks; i_ts++ )
     369             :                         {
     370         768 :                             hQMetaData->q_direction[0].band_data[b].energy_ratio_fx[i_ts] = 0;
     371         768 :                             hQMetaData->q_direction[0].band_data[b].azimuth_fx[i_ts] = 0;
     372         768 :                             hQMetaData->q_direction[0].band_data[b].elevation_fx[i_ts] = 0;
     373         768 :                             move32();
     374         768 :                             move32();
     375         768 :                             move32();
     376             :                         }
     377             :                     }
     378             :                 }
     379             :                 ELSE
     380             :                 {
     381           0 :                     FOR( b = hQMetaData->q_direction->cfg.start_band; b < hQMetaData->q_direction->cfg.nbands; b++ )
     382             :                     {
     383           0 :                         FOR( i_ts = 0; i_ts < 1; i_ts++ )
     384             :                         {
     385           0 :                             hQMetaData->q_direction[0].band_data[b].energy_ratio_fx[i_ts] = 0;
     386           0 :                             hQMetaData->q_direction[0].band_data[b].azimuth_fx[i_ts] = 0;
     387           0 :                             hQMetaData->q_direction[0].band_data[b].elevation_fx[i_ts] = 0;
     388           0 :                             move32();
     389           0 :                             move32();
     390           0 :                             move32();
     391             :                         }
     392             :                     }
     393             :                 }
     394             :             }
     395             : 
     396      158895 :             IF( ( error = ivas_qmetadata_enc_encode_fx( hMetaData, hQMetaData, hodirac_flag ) ) != IVAS_ERR_OK )
     397             :             {
     398           0 :                 return error;
     399             :             }
     400             :         }
     401             :         ELSE
     402             :         {
     403        1605 :             hQMetaData->q_direction[0].cfg.nbands = DIRAC_DTX_BANDS;
     404        1605 :             move16();
     405             :             /* compute directions */
     406        7473 :             FOR( i = 0; i < hQMetaData->q_direction[0].cfg.nblocks; i++ )
     407             :             {
     408        5868 :                 set32_fx( avg_dir_fx, 0, 3 );
     409        5868 :                 Word16 avg_dir_e = 0;
     410        5868 :                 Word16 energySum_e = 0;
     411        5868 :                 Word32 energySum_fx = 0;
     412        5868 :                 move32();
     413        5868 :                 move16();
     414        5868 :                 move16();
     415             : 
     416             :                 /* combine all DirAC bands except the last one, handle last band separately, last band covers BW above WB */
     417       17420 :                 FOR( j = 0; j < sub( orig_dirac_bands, 1 ); j++ )
     418             :                 {
     419       11552 :                     ivas_qmetadata_azimuth_elevation_to_direction_vector_fx( hQMetaData->q_direction[0].band_data[j].azimuth_fx[i], hQMetaData->q_direction[0].band_data[j].elevation_fx[i], &dir_fx[0] );
     420             : 
     421       11552 :                     Word32 vecLen_fx = Mpy_32_32( hQMetaData->q_direction[0].band_data[j].energy_ratio_fx[i] /*Q30*/, hDirAC->buffer_energy_fx[i * orig_dirac_bands + j] ); /* hDirAC->buffer_energy_q[i * orig_dirac_bands + j] - 1 */
     422       11552 :                     avg_dir_fx[0] = L_add( avg_dir_fx[0], Mpy_32_32( dir_fx[0], vecLen_fx ) );                                                                              // Q30 + hDirAC->buffer_energy_q[i * orig_dirac_bands + j] - 1 - 31
     423       11552 :                     avg_dir_fx[1] = L_add( avg_dir_fx[1], Mpy_32_32( dir_fx[1], vecLen_fx ) );                                                                              // Q30 + hDirAC->buffer_energy_q[i * orig_dirac_bands + j] - 1 - 31
     424       11552 :                     avg_dir_fx[2] = L_add( avg_dir_fx[2], Mpy_32_32( dir_fx[2], vecLen_fx ) );                                                                              // Q30 + hDirAC->buffer_energy_q[i * orig_dirac_bands + j] - 1 - 31
     425       11552 :                     move32();
     426       11552 :                     move32();
     427       11552 :                     move32();
     428       11552 :                     avg_dir_e = sub( 31, ( sub( add( Q30, hDirAC->buffer_energy_q[i * orig_dirac_bands + j] ), 1 - 31 ) ) );
     429       11552 :                     energySum_fx = BASOP_Util_Add_Mant32Exp( energySum_fx, energySum_e, hDirAC->buffer_energy_fx[i * orig_dirac_bands + j], sub( 31, hDirAC->buffer_energy_q[i * orig_dirac_bands + j] ), &energySum_e ); /* exp(energySum_e) */
     430             :                 }
     431             : 
     432        5868 :                 Word16 x = getScaleFactor32( avg_dir_fx, 3 );
     433        5868 :                 Scale_sig32( avg_dir_fx, 3, sub( x, 1 ) );
     434        5868 :                 avg_dir_e = sub( avg_dir_e, sub( x, 1 ) );
     435             : 
     436        5868 :                 ivas_qmetadata_direction_vector_to_azimuth_elevation_fx( &avg_dir_fx[0], Q30, &hQMetaData->q_direction[0].band_data[0].azimuth_fx[i], &hQMetaData->q_direction[0].band_data[0].elevation_fx[i] );
     437             : 
     438        5868 :                 Word32 tmp = dotp_fx32( avg_dir_fx, avg_dir_fx, 3 ); // 2*avg_dir_e
     439        5868 :                 Word16 tmp_e = shl( avg_dir_e, 1 );
     440        5868 :                 tmp = Sqrt32( tmp, &tmp_e );
     441        5868 :                 Word16 er_tmp_e = 0;
     442        5868 :                 move16();
     443        5868 :                 Word16 er_tmp = 0;
     444        5868 :                 move16();
     445        5868 :                 Word16 denom_e = 0;
     446        5868 :                 move16();
     447        5868 :                 Word32 denom = BASOP_Util_Add_Mant32Exp( energySum_fx, energySum_e, EPSILON_FX_M, EPSILON_FX_E, &denom_e ); /* exp(denom_e) */
     448             : 
     449        5868 :                 er_tmp = BASOP_Util_Divide3232_Scale( tmp, denom, &er_tmp_e ); /* exp(er_tmp_e) */
     450        5868 :                 er_tmp_e = add( er_tmp_e, sub( tmp_e, denom_e ) );
     451        5868 :                 er_tmp = shr( er_tmp, sub( 1, ( er_tmp_e ) ) ); // Q14
     452             : 
     453        5868 :                 hQMetaData->q_direction[0].band_data[0].energy_ratio_fx[i] = L_deposit_h( er_tmp );                                                         // Q30
     454        5868 :                 hQMetaData->q_direction[0].band_data[1].azimuth_fx[i] = hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].azimuth_fx[i];           // Q22
     455        5868 :                 hQMetaData->q_direction[0].band_data[1].elevation_fx[i] = hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].elevation_fx[i];       // Q22
     456        5868 :                 hQMetaData->q_direction[0].band_data[1].energy_ratio_fx[i] = hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].energy_ratio_fx[i]; // Q30
     457        5868 :                 move32();
     458        5868 :                 move32();
     459        5868 :                 move32();
     460        5868 :                 move32();
     461             :             }
     462             : 
     463             :             /* 1 bit to indicate mode MD coding : temp solution*/
     464        1605 :             push_next_indice( hMetaData, 1, 1 );
     465             : 
     466             :             /* encode SID parameters */
     467        1605 :             ivas_qmetadata_enc_sid_encode_fx( hMetaData, hQMetaData, -1, nchan_transport, SBA_FORMAT );
     468             :         }
     469             : 
     470      160500 :         Word16 len = 0;
     471      160500 :         move16();
     472      160500 :         IF( EQ_16( dtx_vad, 1 ) )
     473             :         {
     474      158895 :             len = hQMetaData->q_direction[0].cfg.nblocks;
     475      158895 :             move16();
     476             :         }
     477             :         ELSE
     478             :         {
     479        1605 :             len = 1;
     480        1605 :             move16();
     481             :         }
     482      756946 :         FOR( b = hQMetaData->q_direction->cfg.start_band; b < hQMetaData->q_direction->cfg.nbands; b++ )
     483             :         {
     484     2892386 :             FOR( i_ts = 0; i_ts < len; i_ts++ )
     485             :             {
     486     2295940 :                 hQMetaData->q_direction->band_data[b].azimuth_fx[i_ts] = hQMetaData->q_direction->band_data[b].q_azimuth_fx[i_ts];                                                               // Q22
     487     2295940 :                 hQMetaData->q_direction->band_data[b].elevation_fx[i_ts] = hQMetaData->q_direction->band_data[b].q_elevation_fx[i_ts];                                                           // Q22
     488     2295940 :                 hQMetaData->q_direction[0].band_data[b].energy_ratio_fx[0] = L_sub( ONE_IN_Q30, diffuseness_reconstructions_fx[hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0]] ); // Q30
     489     2295940 :                 move32();
     490     2295940 :                 move32();
     491     2295940 :                 move32();
     492             :             }
     493             :         }
     494             : 
     495      160500 :         IF( dtx_vad == 0 )
     496             :         {
     497        8025 :             FOR( i = 0; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
     498             :             {
     499        6420 :                 hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].azimuth_fx[i] = hQMetaData->q_direction[0].band_data[1].azimuth_fx[0];           // Q22
     500        6420 :                 hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].elevation_fx[i] = hQMetaData->q_direction[0].band_data[1].elevation_fx[0];       // Q22
     501        6420 :                 hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].energy_ratio_fx[i] = hQMetaData->q_direction[0].band_data[1].energy_ratio_fx[0]; // Q30
     502        6420 :                 move32();
     503        6420 :                 move32();
     504        6420 :                 move32();
     505             :             }
     506             : 
     507        8025 :             FOR( i = 0; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
     508             :             {
     509       18524 :                 FOR( j = sub( orig_dirac_bands, 2 ); j >= 0; j-- )
     510             :                 {
     511       12104 :                     hQMetaData->q_direction[0].band_data[j].azimuth_fx[i] = hQMetaData->q_direction[0].band_data[0].azimuth_fx[0];           // Q22
     512       12104 :                     hQMetaData->q_direction[0].band_data[j].elevation_fx[i] = hQMetaData->q_direction[0].band_data[0].elevation_fx[0];       // Q22
     513       12104 :                     hQMetaData->q_direction[0].band_data[j].energy_ratio_fx[i] = hQMetaData->q_direction[0].band_data[0].energy_ratio_fx[0]; // Q30
     514       12104 :                     move32();
     515       12104 :                     move32();
     516       12104 :                     move32();
     517             :                 }
     518             :             }
     519             : 
     520        1605 :             hQMetaData->q_direction->cfg.nbands = orig_dirac_bands; /* Q0 */
     521        1605 :             move16();
     522             :         }
     523             :     }
     524             : 
     525      160500 :     pop_wmops();
     526             : 
     527      160500 :     return IVAS_ERR_OK;
     528             : }
     529             : 
     530             : 
     531             : /*-------------------------------------------------------------------------
     532             :  * ivas_dirac_get_mono_flag()
     533             :  *
     534             :  *
     535             :  *-------------------------------------------------------------------------*/
     536             : 
     537      642000 : static Word16 ivas_dirac_get_mono_flag_fx(
     538             :     const Word16 *band_grouping,                                         /* i  : Band grouping for estimation                   Q0*/
     539             :     Word32 Cldfb_RealBuffer[DIRAC_MAX_ANA_CHANS][DIRAC_NO_FB_BANDS_MAX], /* i  : Real part of input signal                              e_Cldfb*/
     540             :     Word32 Cldfb_ImagBuffer[DIRAC_MAX_ANA_CHANS][DIRAC_NO_FB_BANDS_MAX], /* i  : Imag part of input signal                              e_Cldfb*/
     541             :     Word16 e_Cldfb,                                                      /* i  : Exponent of cldfb buffer                               */
     542             :     const Word16 nchan_ana,                                              /* i  : number of analysis channels                    Q0*/
     543             :     Word16 *mono_frame_count )                                           /* i/o: current number of mono frames count    Q0*/
     544             : {
     545             :     Word16 brange[2];
     546             :     Word16 i, j, ch_idx;
     547             :     Word32 other_ch_band_power;
     548             :     Word32 W_band_power;
     549      642000 :     Word16 any_mc_band = 0;
     550      642000 :     Word16 any_mono_band = 0;
     551      642000 :     Word16 local_mono_flag = 0;
     552             :     Word32 W_band_power_norm;
     553      642000 :     Word32 threshold = 0;
     554      642000 :     Word16 W_band_power_e = 0;
     555      642000 :     Word16 other_ch_band_power_e = 0;
     556      642000 :     move16();
     557      642000 :     move16();
     558      642000 :     move16();
     559      642000 :     move16();
     560      642000 :     move16();
     561      642000 :     move32();
     562             : 
     563             :     /* Banded Power Calculations */
     564     8346000 :     FOR( i = 0; i < DIRAC_MAX_NBANDS; i++ )
     565             :     {
     566     7704000 :         W_band_power = 0;
     567     7704000 :         other_ch_band_power = 0;
     568     7704000 :         move32();
     569     7704000 :         move32();
     570             : 
     571     7704000 :         brange[0] = band_grouping[i];     /* Q0 */
     572     7704000 :         brange[1] = band_grouping[i + 1]; /* Q0 */
     573     7704000 :         move16();
     574     7704000 :         move16();
     575             : 
     576     7704000 :         Word64 acc = 0, acc1 = 0;
     577     7704000 :         move64();
     578     7704000 :         move64();
     579             : 
     580     7704000 :         Word16 shift1 = 0;
     581     7704000 :         Word16 shift2 = 0;
     582     7704000 :         move16();
     583     7704000 :         move16();
     584             : 
     585     7704000 :         Word16 gb = find_guarded_bits_fx( sub( brange[1], brange[0] ) );
     586             : 
     587             :         /* Loop over the W channel bins to calculate the power in the band */
     588   117729600 :         FOR( j = brange[0]; j < brange[1]; j++ )
     589             :         {
     590   110025600 :             acc = W_mac_32_32( acc, L_shr( Cldfb_RealBuffer[0][j], gb ), L_shr( Cldfb_RealBuffer[0][j], gb ) ); /*Q:(2*(31-e_Cldfb-gb)+1*/
     591   110025600 :             acc = W_mac_32_32( acc, L_shr( Cldfb_ImagBuffer[0][j], gb ), L_shr( Cldfb_ImagBuffer[0][j], gb ) ); /*Q:(2*(31-e_Cldfb-gb)+1*/
     592             :         }
     593     7704000 :         shift1 = W_norm( acc );
     594     7704000 :         W_band_power = W_extract_h( W_shl( acc, shift1 ) ); /*Q:(2*(31-e_Cldfb-gb)+1+shift1-32*/
     595     7704000 :         W_band_power_e = sub( shl( add( e_Cldfb, gb ), 1 ), shift1 );
     596             : 
     597     7704000 :         gb = find_guarded_bits_fx( imult1616( sub( brange[1], brange[0] ), sub( nchan_ana, 1 ) ) );
     598             :         /* Loop over the other channels and bins to calculate the power in the band */
     599    30816000 :         FOR( ch_idx = 1; ch_idx < nchan_ana; ch_idx++ )
     600             :         {
     601             :             /* abs()^2 */
     602   353188800 :             FOR( j = brange[0]; j < brange[1]; j++ )
     603             :             {
     604   330076800 :                 acc1 = W_mac_32_32( acc1, L_shr( Cldfb_RealBuffer[ch_idx][j], gb ), L_shr( Cldfb_RealBuffer[ch_idx][j], gb ) ); /*Q:(2*(31-e_Cldfb-gb)+1*/
     605   330076800 :                 acc1 = W_mac_32_32( acc1, L_shr( Cldfb_ImagBuffer[ch_idx][j], gb ), L_shr( Cldfb_ImagBuffer[ch_idx][j], gb ) ); /*Q:(2*(31-e_Cldfb-gb)+1*/
     606             :             }
     607             :         }
     608     7704000 :         shift2 = W_norm( acc1 );
     609     7704000 :         other_ch_band_power = W_extract_h( W_shl( acc1, shift2 ) ); /*Q:(2*(31-e_Cldfb-gb)+1+shift2-32*/
     610     7704000 :         other_ch_band_power_e = sub( shl( add( e_Cldfb, gb ), 1 ), shift2 );
     611             : 
     612     7704000 :         IF( BASOP_Util_Cmp_Mant32Exp( other_ch_band_power, other_ch_band_power_e, EPSILON_FX_M, EPSILON_FX_E ) < 0 )
     613             :         {
     614      772816 :             if ( BASOP_Util_Cmp_Mant32Exp( W_band_power, W_band_power_e, DIRAC_MONO_THRESH_SILENCE_FX, 31 ) > 0 )
     615             :             {
     616         136 :                 any_mono_band = 1;
     617         136 :                 move16();
     618             :             }
     619             :         }
     620             :         ELSE
     621             :         {
     622     6931184 :             test();
     623     6931184 :             IF( ( BASOP_Util_Cmp_Mant32Exp( W_band_power, W_band_power_e, DIRAC_MONO_THRESH_SILENCE_FX, 31 ) > 0 ) || ( BASOP_Util_Cmp_Mant32Exp( other_ch_band_power, other_ch_band_power_e, DIRAC_MONO_THRESH_SILENCE_FX, 31 ) > 0 ) )
     624             :             {
     625     6312355 :                 Word16 tmp_div_e = 0;
     626     6312355 :                 move16();
     627     6312355 :                 Word16 tmp_div = BASOP_Util_Divide3232_Scale( W_band_power, DIRAC_MONO_NORM_FACTOR_M, &tmp_div_e ); /* exp(tmp_div_e) */
     628     6312355 :                 tmp_div_e = add( tmp_div_e, sub( W_band_power_e, DIRAC_MONO_NORM_FACTOR_E ) );
     629     6312355 :                 Word16 W_band_power_norm_e = 0;
     630     6312355 :                 move16();
     631     6312355 :                 Word16 flag = BASOP_Util_Cmp_Mant32Exp( tmp_div, tmp_div_e, ONE_IN_Q14, 1 );
     632     6312355 :                 IF( EQ_16( flag, 1 ) )
     633             :                 {
     634        9382 :                     W_band_power_norm = 1;
     635        9382 :                     move16();
     636        9382 :                     W_band_power_norm_e = 31;
     637        9382 :                     move16();
     638             :                 }
     639             :                 ELSE
     640             :                 {
     641     6302973 :                     W_band_power_norm = L_deposit_h( tmp_div );
     642     6302973 :                     W_band_power_norm_e = tmp_div_e;
     643     6302973 :                     move16();
     644             :                 }
     645     6312355 :                 Word32 tmp_2 = Mpy_32_32( W_band_power_norm, DIRAC_MONO_MAX_THRESH_FX ); // W_band_power_norm_e+31
     646     6312355 :                 Word32 tmp_3 = W_extract_h( W_shl( DIRAC_MONO_MIN_THRESH_FX, sub( 31, add( W_band_power_norm_e, 31 ) ) ) );
     647     6312355 :                 tmp_3 = DIRAC_MONO_MIN_THRESH_FX; // exp31
     648     6312355 :                 move32();
     649     6312355 :                 Word16 flag2 = BASOP_Util_Cmp_Mant32Exp( tmp_2, add( W_band_power_norm_e, 31 ), tmp_3, 31 );
     650             : 
     651     6312355 :                 Word16 threshold_e = 0;
     652     6312355 :                 move16();
     653     6312355 :                 IF( EQ_16( flag2, 1 ) )
     654             :                 {
     655     1920348 :                     threshold = tmp_2;
     656     1920348 :                     move32();
     657     1920348 :                     threshold_e = add( W_band_power_norm_e, 31 );
     658             :                 }
     659             :                 ELSE
     660             :                 {
     661     4392007 :                     threshold = DIRAC_MONO_MIN_THRESH_FX;
     662     4392007 :                     threshold_e = 31;
     663     4392007 :                     move32();
     664     4392007 :                     move16();
     665             :                 }
     666     6312355 :                 IF( BASOP_Util_Cmp_Mant32Exp( W_band_power, W_band_power_e, Mpy_32_32( other_ch_band_power, threshold ), other_ch_band_power_e + threshold_e ) == 1 )
     667             :                 {
     668        8807 :                     any_mono_band = 1;
     669        8807 :                     move16();
     670             :                 }
     671             :                 ELSE
     672             :                 {
     673     6303548 :                     any_mc_band = 1;
     674     6303548 :                     move16();
     675             :                 }
     676             :             }
     677             :         }
     678             :     }
     679             : 
     680             :     /* If any band contains multi-channel content it's not mono */
     681      642000 :     IF( any_mc_band )
     682             :     {
     683      628255 :         local_mono_flag = 0;
     684      628255 :         move16();
     685             :     }
     686             :     ELSE
     687             :     {
     688             :         /* If any band contains mono content the frame is mono. */
     689       13745 :         if ( any_mono_band )
     690             :         {
     691         678 :             local_mono_flag = 1;
     692         678 :             move16();
     693             :         }
     694             :     }
     695             : 
     696             :     /* Hysteresis - only after DIRAC_MONO_FRAME_THRESH frames on mono will the actual mono flag be set */
     697      642000 :     IF( local_mono_flag )
     698             :     {
     699         678 :         IF( LT_16( *mono_frame_count, DIRAC_MONO_FRAME_THRESH ) )
     700             :         {
     701         425 :             *mono_frame_count = add( *mono_frame_count, 1 ); /* Q0 */
     702         425 :             move16();
     703             :         }
     704             :     }
     705             :     ELSE
     706             :     {
     707             :         /* Instantaneously disable actual mono flag if multi-channel content is observed */
     708      641322 :         if ( any_mc_band )
     709             :         {
     710      628255 :             *mono_frame_count = 0;
     711      628255 :             move16();
     712             :         }
     713             :     }
     714             : 
     715             :     /* Final check if there has been mono for DIRAC_MONO_FRAME_THRESH number of frames than the content is declared mono */
     716      642000 :     IF( EQ_16( *mono_frame_count, DIRAC_MONO_FRAME_THRESH ) )
     717             :     {
     718         255 :         return 1;
     719             :     }
     720             :     ELSE
     721             :     {
     722      641745 :         return 0;
     723             :     }
     724             : }
     725             : 
     726             : /*-------------------------------------------------------------------------
     727             :  * computeReferencePower_enc_fx_dirac()
     728             :  *
     729             :  *
     730             :  *-------------------------------------------------------------------------*/
     731             : 
     732      642000 : void computeReferencePower_enc_fx_dirac(
     733             :     const Word16 *band_grouping,                      /* i  : Band grouping for estimation    */
     734             :     Word32 Cldfb_RealBuffer[][DIRAC_NO_FB_BANDS_MAX], /* i  : Real part of input signal       e_Cldfb*/
     735             :     Word32 Cldfb_ImagBuffer[][DIRAC_NO_FB_BANDS_MAX], /* i  : Imag part of input signal       e_Cldfb*/
     736             :     Word16 e_Cldfb,                                   /* i  : exponent of Cldfb buffer            */
     737             :     Word32 *reference_power,                          /* o  : Estimated power                 e_reference*/
     738             :     Word16 *e_reference,                              /* o  : exponent of reference_power         */
     739             :     const Word16 enc_param_start_band,                /* i  : first band to process           Q0*/
     740             :     const Word16 num_freq_bands,                      /* i  : Number of frequency bands       Q0*/
     741             :     const IVAS_FORMAT ivas_format,                    /* i  : ivas_format                     */
     742             :     Word16 ref_power_w,                               /* i  : use 0 if hodirac is enabled     Q0*/
     743             :     const Word16 nchan_ana,                           /* i  : number of analysis channels     Q0*/
     744             :     Word16 *mono_frame_count,                         /* i/o: Mono Frame Count                Q0*/
     745             :     Word16 *dirac_mono_flag                           /* i/o: Mono Flag                       Q0*/
     746             : )
     747             : {
     748             :     Word16 brange[2];
     749             :     Word16 ch_idx, i, j;
     750             :     Word64 reference_power_temp[DIRAC_MAX_NBANDS];
     751             :     Word64 reference_power_W[DIRAC_MAX_NBANDS];
     752      642000 :     Word16 e_reference_W = 0, e_reference_temp = 0;
     753             :     Word16 min_norm;
     754      642000 :     move16();
     755      642000 :     move16();
     756             : 
     757      642000 :     IF( dirac_mono_flag != NULL )
     758             :     {
     759      642000 :         *dirac_mono_flag = ivas_dirac_get_mono_flag_fx( band_grouping, Cldfb_RealBuffer, Cldfb_ImagBuffer, e_Cldfb, nchan_ana, mono_frame_count ); /* Q0 */
     760      642000 :         move16();
     761             :     }
     762      642000 :     Word16 gb = 0;
     763      642000 :     move16();
     764     3033468 :     FOR( i = 0; i < num_freq_bands; i++ )
     765             :     {
     766     2391468 :         brange[0] = band_grouping[i + enc_param_start_band];
     767     2391468 :         brange[1] = band_grouping[i + enc_param_start_band + 1];
     768     2391468 :         move16();
     769     2391468 :         move16();
     770     2391468 :         gb = s_max( gb, find_guarded_bits_fx( sub( brange[1], brange[0] ) ) );
     771             :     }
     772      642000 :     e_reference_W = sub( 31, sub( add( shl( sub( 31, e_Cldfb ), 1 ), 1 ), gb ) );
     773      642000 :     e_reference_temp = e_reference_W;
     774      642000 :     move16();
     775     3033468 :     FOR( i = 0; i < num_freq_bands; i++ )
     776             :     {
     777     2391468 :         brange[0] = band_grouping[i + enc_param_start_band];
     778     2391468 :         brange[1] = band_grouping[i + enc_param_start_band + 1];
     779     2391468 :         move16();
     780     2391468 :         move16();
     781             : 
     782     2391468 :         reference_power_temp[i] = 0;
     783     2391468 :         move64();
     784             : 
     785     2391468 :         reference_power_W[i] = 0;
     786     2391468 :         move64();
     787             : 
     788    89477228 :         FOR( j = brange[0]; j < brange[1]; j++ )
     789             :         {
     790    87085760 :             reference_power_W[i] = W_add( reference_power_W[i], W_shr( W_mult_32_32( Cldfb_RealBuffer[0][j], Cldfb_RealBuffer[0][j] ), gb ) ); /* exp(2 * e_Cldfb) */
     791    87085760 :             reference_power_W[i] = W_add( reference_power_W[i], W_shr( W_mult_32_32( Cldfb_ImagBuffer[0][j], Cldfb_ImagBuffer[0][j] ), gb ) ); /* exp(2 * e_Cldfb) */
     792    87085760 :             move64();
     793    87085760 :             move64();
     794             :         }
     795     2391468 :         reference_power_temp[i] = W_add( reference_power_temp[i], reference_power_W[i] ); /* exp(2 * e_Cldfb) */
     796     2391468 :         move64();
     797             : 
     798     9565872 :         FOR( ch_idx = 1; ch_idx < nchan_ana; ch_idx++ )
     799             :         {
     800             :             /* abs()^2 */
     801   268431684 :             FOR( j = brange[0]; j < brange[1]; j++ )
     802             :             {
     803   261257280 :                 reference_power_temp[i] = W_add( reference_power_temp[i], W_shr( W_mult_32_32( Cldfb_RealBuffer[ch_idx][j], Cldfb_RealBuffer[ch_idx][j] ), gb ) ); /* exp(2 * e_Cldfb) */
     804   261257280 :                 reference_power_temp[i] = W_add( reference_power_temp[i], W_shr( W_mult_32_32( Cldfb_ImagBuffer[ch_idx][j], Cldfb_ImagBuffer[ch_idx][j] ), gb ) ); /* exp(2 * e_Cldfb) */
     805   261257280 :                 move64();
     806   261257280 :                 move64();
     807             :             }
     808             :         }
     809             :     }
     810             : 
     811     3033468 :     FOR( i = 0; i < num_freq_bands; i++ )
     812             :     {
     813     2391468 :         reference_power_temp[i] = W_shr( reference_power_temp[i], 1 ); /* exp(2 * e_Cldfb + 1) */
     814     2391468 :         move64();
     815             :     }
     816             : 
     817      642000 :     test();
     818      642000 :     IF( EQ_16( ivas_format, SBA_FORMAT ) && EQ_16( ref_power_w, 1 ) )
     819             :     {
     820     1784672 :         FOR( i = 0; i < num_freq_bands; i++ )
     821             :         {
     822     1327832 :             IF( BASOP_Util_Cmp_Mant32Exp( W_extract_h( reference_power_temp[i] ), e_reference_temp, W_extract_h( reference_power_W[i] ), e_reference_W ) < 0 )
     823             :             {
     824      620618 :                 reference_power_temp[i] = reference_power_W[i]; /* exp(2 * e_Cldfb + 1 ) */
     825      620618 :                 move64();
     826             :             }
     827             :         }
     828             :     }
     829             : 
     830     3033468 :     FOR( i = 0; i < num_freq_bands; i++ )
     831             :     {
     832     2391468 :         min_norm = W_norm( reference_power_temp[i] );
     833     2391468 :         reference_power[i] = W_extract_h( W_shl( reference_power_temp[i], min_norm ) ); // Right shift with min_norm and extract high bits
     834     2391468 :         move32();
     835     2391468 :         e_reference[i] = sub( 31, sub( add( sub( 31, e_reference_temp ), min_norm ), 32 ) );
     836     2391468 :         move16();
     837             :     }
     838      642000 :     move16();
     839             : 
     840      642000 :     return;
     841             : }
     842             : 
     843       46720 : void computeReferencePower_enc_fx(
     844             :     const Word16 *band_grouping,                      /* i  : Band grouping for estimation    Q0*/
     845             :     Word32 Cldfb_RealBuffer[][DIRAC_NO_FB_BANDS_MAX], /* i  : Real part of input signal       inp_q*/
     846             :     Word32 Cldfb_ImagBuffer[][DIRAC_NO_FB_BANDS_MAX], /* i  : Imag part of input signal       inp_q*/
     847             :     Word32 *reference_power,                          /* o  : Estimated power                 ref_exp*/
     848             :     const Word16 enc_param_start_band,                /* i  : first band to process           Q0*/
     849             :     const Word16 num_freq_bands,                      /* i  : Number of frequency bands       Q0*/
     850             :     const IVAS_FORMAT ivas_format,                    /* i  : ivas_format                     */
     851             :     Word16 ref_power_w,                               /* i  : use 0 if hodirac is enabled     Q0*/
     852             :     const Word16 nchan_ana,                           /* i  : number of analysis channels     Q0*/
     853             :     Word16 inp_q,                                     /* i  : q of cld buffers     */
     854             :     Word16 q_reference_power[DIRAC_NO_FB_BANDS_MAX] )
     855             : {
     856             :     Word16 brange[2];
     857             :     Word16 ch_idx, i, j;
     858             :     Word16 num_bins, guard_bits, norm;
     859             : 
     860             :     Word64 reference_power_tmp[DIRAC_MAX_NBANDS];
     861             :     Word64 reference_power_W[DIRAC_MAX_NBANDS];
     862             : 
     863       46720 :     Word16 shift_ref_power = sub( shl( inp_q, 1 ), 31 );
     864             : 
     865      280320 :     FOR( i = 0; i < num_freq_bands; i++ )
     866             :     {
     867      233600 :         brange[0] = band_grouping[i + enc_param_start_band]; /* Q0 */
     868      233600 :         move16();
     869      233600 :         brange[1] = band_grouping[i + enc_param_start_band + 1]; /* Q0 */
     870      233600 :         move16();
     871      233600 :         reference_power[i] = 0;
     872      233600 :         move32();
     873      233600 :         reference_power_W[i] = 0;
     874      233600 :         move64();
     875      233600 :         reference_power_tmp[i] = 0;
     876      233600 :         move64();
     877             : 
     878      233600 :         num_bins = sub( brange[1], brange[0] );
     879      233600 :         guard_bits = find_guarded_bits_fx( num_bins );
     880             : 
     881    11382400 :         FOR( j = brange[0]; j < brange[1]; j++ )
     882             :         {
     883    11148800 :             reference_power_W[i] = W_add( reference_power_W[i], W_shr( W_mac_32_32( W_mult_32_32( Cldfb_RealBuffer[0][j], Cldfb_RealBuffer[0][j] ), Cldfb_ImagBuffer[0][j], Cldfb_ImagBuffer[0][j] ), guard_bits ) ); // 2*inp_q + 1 - guard_bits
     884    11148800 :             move64();
     885             :         }
     886      233600 :         reference_power_tmp[i] = W_add( reference_power_tmp[i], reference_power_W[i] ); // 2*inp_q + 1 - guard_bits
     887      233600 :         move64();
     888      934400 :         FOR( ch_idx = 1; ch_idx < nchan_ana; ch_idx++ )
     889             :         {
     890             :             /* abs()^2 */
     891    34147200 :             FOR( j = brange[0]; j < brange[1]; j++ )
     892             :             {
     893    33446400 :                 reference_power_tmp[i] = W_add( reference_power_tmp[i], W_shr( W_mac_32_32( W_mult_32_32( Cldfb_RealBuffer[ch_idx][j], Cldfb_RealBuffer[ch_idx][j] ), Cldfb_ImagBuffer[ch_idx][j], Cldfb_ImagBuffer[ch_idx][j] ), guard_bits ) ); // 2*inp_q + 1 - guard_bits
     894    33446400 :                 move64();
     895             :             }
     896             :         }
     897      233600 :         q_reference_power[i] = sub( shift_ref_power, guard_bits );
     898      233600 :         move16();
     899             :     }
     900      280320 :     FOR( i = 0; i < num_freq_bands; i++ )
     901             :     {
     902      233600 :         reference_power_tmp[i] = W_shr( reference_power_tmp[i], 1 ); // 2*inp_q + 1
     903      233600 :         move64();
     904             :     }
     905             : 
     906       46720 :     test();
     907       46720 :     IF( EQ_16( ivas_format, SBA_FORMAT ) && EQ_16( ref_power_w, 1 ) )
     908             :     {
     909           0 :         FOR( i = 0; i < num_freq_bands; i++ )
     910             :         {
     911           0 :             IF( LT_64( reference_power_tmp[i], reference_power_W[i] ) )
     912             :             {
     913           0 :                 reference_power_tmp[i] = reference_power_W[i]; // 2*inp_q + 1
     914           0 :                 move64();
     915             :             }
     916             :         }
     917             :     }
     918             : 
     919      280320 :     FOR( i = 0; i < num_freq_bands; i++ )
     920             :     {
     921      233600 :         norm = 63;
     922      233600 :         move16();
     923      233600 :         IF( reference_power_tmp[i] != 0 )
     924             :         {
     925      232115 :             norm = W_norm( reference_power_tmp[i] );
     926             :         }
     927      233600 :         reference_power[i] = W_extract_h( W_shl( reference_power_tmp[i], norm ) ); // 2 * inp_q + 1 - guard_bits + norm - 32
     928      233600 :         move32();
     929      233600 :         q_reference_power[i] = add( q_reference_power[i], norm );
     930      233600 :         move16();
     931             :     }
     932             : 
     933       46720 :     return;
     934             : }
     935             : /*-------------------------------------------------------------------------
     936             :  * ivas_dirac_param_est_enc()
     937             :  *
     938             :  *
     939             :  *------------------------------------------------------------------------*/
     940             : 
     941      160500 : void ivas_dirac_param_est_enc_fx(
     942             :     DIRAC_ENC_HANDLE hDirAC,
     943             :     IVAS_QDIRECTION *q_direction,
     944             :     const UWord8 useLowerRes, /* Q0 */
     945             :     Word32 *data_f_fx[],      /* Qx */
     946             :     Word32 **pp_fr_real_fx,   /* pp_fr_q */
     947             :     Word32 **pp_fr_imag_fx,   /* pp_fr_q */
     948             :     Word16 pp_fr_q,
     949             :     const Word16 input_frame, /* Q0 */
     950             :     const IVAS_FORMAT ivas_format,
     951             :     const Word16 hodirac_flag, /* Q0 */
     952             :     const Word16 nchan_fb_in,  /* Q0 */
     953             :     Word16 *mono_frame_count,  /* Q0 */
     954             :     Word16 *dirac_mono_flag,   /* Q0 */
     955             :     const Word16 shift )
     956             : {
     957             :     Word16 i, j, k, d, ts, index, l_ts, num_freq_bands;
     958             :     Word16 band_m_idx, block_m_idx;
     959             :     Word32 dir_v_fx[DIRAC_NUM_DIMS];
     960             :     Word32 *pcm_in_fx[DIRAC_MAX_ANA_CHANS];
     961             :     Word32 Cldfb_RealBuffer_fx[DIRAC_MAX_ANA_CHANS][DIRAC_NO_FB_BANDS_MAX];
     962             :     Word32 Cldfb_ImagBuffer_fx[DIRAC_MAX_ANA_CHANS][DIRAC_NO_FB_BANDS_MAX];
     963             :     Word32 *p_Cldfb_RealBuffer_fx[DIRAC_MAX_ANA_CHANS];
     964             :     Word32 *p_Cldfb_ImagBuffer_fx[DIRAC_MAX_ANA_CHANS];
     965             :     Word16 cldfb_q;
     966             :     Word32 intensity_real_fx[DIRAC_NUM_DIMS][DIRAC_MAX_NBANDS];
     967             :     Word16 intensity_real_q[DIRAC_MAX_NBANDS];
     968             :     Word32 direction_vector_fx[DIRAC_NUM_DIMS][DIRAC_MAX_NBANDS];
     969             :     Word16 direction_vector_q;
     970             :     Word32 diffuseness_vector_fx[DIRAC_MAX_NBANDS];
     971             :     Word16 diffuseness_vector_q;
     972             :     Word32 renormalization_factor_fx[DIRAC_MAX_NBANDS];
     973             :     Word16 renormalization_factor_exp[DIRAC_MAX_NBANDS];
     974             :     Word32 renormalization_factor_diff_fx[DIRAC_MAX_NBANDS];
     975             :     Word16 renormalization_factor_diff_exp[DIRAC_MAX_NBANDS];
     976             :     Word32 norm_tmp_fx;
     977             :     Word16 norm_tmp_exp;
     978             :     Word16 mrange[2];
     979             :     Word16 num_blocks;
     980             :     Word32 reference_power_fx[CLDFB_NO_COL_MAX][DIRAC_NO_FB_BANDS_MAX];
     981             :     Word16 reference_power_exp[CLDFB_NO_COL_MAX][DIRAC_NO_FB_BANDS_MAX];
     982             :     Word32 azi_secs_fx[2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS];
     983             :     Word32 ele_secs_fx[2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS];
     984             :     Word32 diff_secs_fx[2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS];
     985             :     Word16 diff_secs_exp[2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS];
     986             :     Word32 ene_secs_fx[2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS];
     987             :     Word16 ene_secs_exp[2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS];
     988             : 
     989      160500 :     push_wmops( "dirac_enc_param_est" );
     990      160500 :     num_freq_bands = hDirAC->hConfig->nbands; /* Q0 */
     991      160500 :     move16();
     992             : 
     993             :     /* Initialization */
     994      160500 :     cldfb_q = 0;
     995      160500 :     move16();
     996             : 
     997             : 
     998      160500 :     direction_vector_q = 0;
     999      160500 :     move16();
    1000      160500 :     diffuseness_vector_q = 0;
    1001      160500 :     move16();
    1002             : 
    1003      160500 :     l_ts = shr( input_frame, MAX_PARAM_SPATIAL_SUBFRAMES_LOG2 ); /* Q0 */
    1004      160500 :     IF( useLowerRes )
    1005             :     {
    1006       13553 :         q_direction->cfg.nblocks = 1;
    1007       13553 :         move16();
    1008       13553 :         num_blocks = 1;
    1009       13553 :         move16();
    1010             :     }
    1011             :     ELSE
    1012             :     {
    1013      146947 :         q_direction->cfg.nblocks = MAX_PARAM_SPATIAL_SUBFRAMES;
    1014      146947 :         move16();
    1015      146947 :         num_blocks = MAX_PARAM_SPATIAL_SUBFRAMES;
    1016      146947 :         move16();
    1017             :     }
    1018             : 
    1019             :     /* Need to initialize renormalization_factors, direction_m and diffuseness_m */
    1020      160500 :     set_zero_fx( hDirAC->diffuseness_m_fx, hDirAC->hConfig->nbands );
    1021      160500 :     set16_zero_fx( hDirAC->diffuseness_m_exp, hDirAC->hConfig->nbands );
    1022      160500 :     set_zero_fx( renormalization_factor_diff_fx, hDirAC->hConfig->nbands );
    1023      160500 :     set16_zero_fx( renormalization_factor_diff_exp, hDirAC->hConfig->nbands );
    1024     2728500 :     FOR( i = 0; i < CLDFB_NO_COL_MAX; i++ )
    1025             :     {
    1026     2568000 :         set16_zero_fx( reference_power_exp[i], DIRAC_NO_FB_BANDS_MAX );
    1027             :     }
    1028             : 
    1029      160500 :     set_zero_fx( azi_secs_fx, 2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS );
    1030      160500 :     set_zero_fx( ele_secs_fx, 2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS );
    1031      160500 :     set_zero_fx( diff_secs_fx, 2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS );
    1032      160500 :     set16_zero_fx( diff_secs_exp, 2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS );
    1033      160500 :     set_zero_fx( ene_secs_fx, 2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS );
    1034      160500 :     set16_zero_fx( ene_secs_exp, 2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS );
    1035             : 
    1036             :     /* Copy current frame to memory for delay compensation */
    1037      883700 :     FOR( i = 0; i < nchan_fb_in; i++ )
    1038             :     {
    1039      723200 :         pcm_in_fx[i] = data_f_fx[i];
    1040      723200 :         p_Cldfb_RealBuffer_fx[i] = &Cldfb_RealBuffer_fx[i][0];
    1041      723200 :         p_Cldfb_ImagBuffer_fx[i] = &Cldfb_ImagBuffer_fx[i][0];
    1042             :     }
    1043             : 
    1044             :     /* do processing over all CLDFB time slots */
    1045      761841 :     FOR( block_m_idx = 0; block_m_idx < num_blocks; block_m_idx++ )
    1046             :     {
    1047      601341 :         mrange[0] = hDirAC->block_grouping[block_m_idx]; /* Q0 */
    1048      601341 :         move16();
    1049      601341 :         mrange[1] = hDirAC->block_grouping[block_m_idx + 1];
    1050      601341 :         move16();
    1051             : 
    1052      601341 :         set32_fx( renormalization_factor_fx, EPSILON_FX, hDirAC->hConfig->nbands );
    1053      601341 :         set16_fx( renormalization_factor_exp, 0, hDirAC->hConfig->nbands );
    1054      601341 :         set_zero_fx( hDirAC->direction_vector_m_fx[0][block_m_idx], hDirAC->hConfig->nbands );
    1055      601341 :         set_zero_fx( hDirAC->direction_vector_m_fx[1][block_m_idx], hDirAC->hConfig->nbands );
    1056      601341 :         set_zero_fx( hDirAC->direction_vector_m_fx[2][block_m_idx], hDirAC->hConfig->nbands );
    1057             :         Word64 dir[DIRAC_NUM_DIMS][DIRAC_MAX_NBANDS];
    1058      601341 :         Word16 direction_vector_exp_tmp[DIRAC_NUM_DIMS][MAX_PARAM_SPATIAL_SUBFRAMES][DIRAC_MAX_NBANDS] = { 0 };
    1059     2405364 :         FOR( i = 0; i < DIRAC_NUM_DIMS; i++ )
    1060             :         {
    1061     1804023 :             set64_fx( dir[i], 0, DIRAC_MAX_NBANDS );
    1062     9020115 :             FOR( j = 0; j < MAX_PARAM_SPATIAL_SUBFRAMES; j++ )
    1063             :             {
    1064     7216092 :                 set16_fx( direction_vector_exp_tmp[i][j], 0, DIRAC_MAX_NBANDS );
    1065             :             }
    1066             :         }
    1067      601341 :         hDirAC->direction_vector_m_exp[block_m_idx] = 0;
    1068      601341 :         move16();
    1069             : 
    1070     1243341 :         FOR( ts = mrange[0]; ts < mrange[1]; ts++ )
    1071             :         {
    1072      642000 :             IF( hDirAC->hFbMixer )
    1073             :             {
    1074           0 :                 FOR( i = 0; i < nchan_fb_in; i++ )
    1075             :                 {
    1076           0 :                     scale_sig32( pcm_in_fx[i], input_frame, shift );                                                                      // Q:Qx+shift
    1077           0 :                     scale_sig32( hDirAC->hFbMixer->ppFilterbank_prior_input_fx[i], hDirAC->hFbMixer->fb_cfg->prior_input_length, shift ); // Q:Qx+shift
    1078             :                 }
    1079           0 :                 ivas_fb_mixer_get_windowed_fr_fx( hDirAC->hFbMixer, pcm_in_fx, p_Cldfb_RealBuffer_fx, p_Cldfb_ImagBuffer_fx, l_ts, l_ts, hDirAC->hFbMixer->fb_cfg->num_in_chans, 0 );
    1080           0 :                 FOR( i = 0; i < nchan_fb_in; i++ )
    1081             :                 {
    1082           0 :                     scale_sig32( pcm_in_fx[i], input_frame, negate( shift ) );                                                                      // Q:Qx
    1083           0 :                     scale_sig32( hDirAC->hFbMixer->ppFilterbank_prior_input_fx[i], hDirAC->hFbMixer->fb_cfg->prior_input_length, negate( shift ) ); // Q:Qx
    1084             :                 }
    1085           0 :                 ivas_fb_mixer_update_prior_input_fx( hDirAC->hFbMixer, pcm_in_fx, l_ts, hDirAC->hFbMixer->fb_cfg->num_in_chans );
    1086             : 
    1087           0 :                 FOR( i = 0; i < nchan_fb_in; i++ )
    1088             :                 {
    1089           0 :                     pcm_in_fx[i] += l_ts;
    1090             :                 }
    1091             :             }
    1092             :             ELSE
    1093             :             {
    1094     3534800 :                 FOR( i = 0; i < nchan_fb_in; i++ )
    1095             :                 {
    1096     2892800 :                     Copy32( &pp_fr_real_fx[i][ts * l_ts], Cldfb_RealBuffer_fx[i], l_ts ); /* pp_fr_q */
    1097     2892800 :                     Copy32( &pp_fr_imag_fx[i][ts * l_ts], Cldfb_ImagBuffer_fx[i], l_ts ); /* pp_fr_q */
    1098             :                 }
    1099      642000 :                 cldfb_q = pp_fr_q;
    1100      642000 :                 move16();
    1101             :             }
    1102             : 
    1103      642000 :             Word16 ref_power_w = 1;
    1104      642000 :             move16();
    1105      642000 :             if ( hodirac_flag )
    1106             :             {
    1107       64960 :                 ref_power_w = 0;
    1108       64960 :                 move16();
    1109             :             }
    1110             : 
    1111     1284000 :             computeReferencePower_enc_fx_dirac(
    1112      642000 :                 hDirAC->band_grouping,
    1113             :                 Cldfb_RealBuffer_fx,
    1114             :                 Cldfb_ImagBuffer_fx,
    1115      642000 :                 sub( 31, cldfb_q ),
    1116      642000 :                 reference_power_fx[ts],
    1117      642000 :                 reference_power_exp[ts],
    1118      642000 :                 hDirAC->hConfig->enc_param_start_band,
    1119             :                 num_freq_bands,
    1120             :                 ivas_format,
    1121             :                 ref_power_w,
    1122             :                 FOA_CHANNELS,
    1123             :                 mono_frame_count,
    1124             :                 dirac_mono_flag );
    1125             : 
    1126      642000 :             computeIntensityVector_enc_fx(
    1127             :                 hDirAC,
    1128             :                 Cldfb_RealBuffer_fx,
    1129             :                 Cldfb_ImagBuffer_fx,
    1130      642000 :                 hDirAC->hConfig->enc_param_start_band,
    1131             :                 num_freq_bands,
    1132             :                 intensity_real_fx,
    1133             :                 cldfb_q,
    1134             :                 intensity_real_q );
    1135             : 
    1136      642000 :             IF( !hodirac_flag )
    1137             :             {
    1138      577040 :                 computeDirectionVectors_fx2(
    1139             :                     intensity_real_fx[0],
    1140             :                     intensity_real_fx[1],
    1141             :                     intensity_real_fx[2],
    1142      577040 :                     hDirAC->hConfig->enc_param_start_band,
    1143             :                     num_freq_bands,
    1144             :                     direction_vector_fx[0],
    1145             :                     direction_vector_fx[1],
    1146             :                     direction_vector_fx[2],
    1147             :                     31, intensity_real_q );
    1148             : 
    1149      577040 :                 direction_vector_q = Q30;
    1150      577040 :                 move16();
    1151             :             }
    1152             : 
    1153             :             /* fill buffers of length "averaging_length" time slots for intensity and energy */
    1154      642000 :             hDirAC->index_buffer_intensity = add( ( hDirAC->index_buffer_intensity % hDirAC->no_col_avg_diff ), 1 ); /* averaging_length = 32  Q0*/
    1155      642000 :             move16();
    1156      642000 :             index = hDirAC->index_buffer_intensity; /* Q0 */
    1157      642000 :             move16();
    1158     2568000 :             FOR( i = 0; i < DIRAC_NUM_DIMS; i++ )
    1159             :             {
    1160             :                 /* only real part needed */
    1161     1926000 :                 Copy32( intensity_real_fx[i], &( hDirAC->buffer_intensity_real_fx[i][index - 1][0] ), num_freq_bands ); /* intensity_real_q */
    1162     1926000 :                 Copy( intensity_real_q, &hDirAC->buffer_intensity_real_q[i][index - 1][0], num_freq_bands );
    1163             :             }
    1164      642000 :             Copy32( reference_power_fx[ts], &( hDirAC->buffer_energy_fx[( index - 1 ) * num_freq_bands] ), num_freq_bands ); /* exp(reference_power_exp) */
    1165     3033468 :             FOR( i = 0; i < num_freq_bands; i++ )
    1166             :             {
    1167     2391468 :                 hDirAC->buffer_energy_q[( index - 1 ) * num_freq_bands + i] = sub( 31, reference_power_exp[ts][i] );
    1168     2391468 :                 move16();
    1169             :             }
    1170             : 
    1171      642000 :             Word16 buffer_intensity_real_single_q, min_shift = MAX_16;
    1172      642000 :             move16();
    1173      642000 :             buffer_intensity_real_single_q = hDirAC->buffer_intensity_real_q[0][0][0];
    1174      642000 :             move16();
    1175     2568000 :             FOR( i = 0; i < DIRAC_NUM_DIMS; i++ )
    1176             :             {
    1177    17334000 :                 FOR( j = 0; j < hDirAC->no_col_avg_diff; j++ )
    1178             :                 {
    1179    15408000 :                     min_shift = s_min( min_shift, L_norm_arr( hDirAC->buffer_intensity_real_fx[i][j], num_freq_bands ) );
    1180    72803232 :                     FOR( k = 0; k < num_freq_bands; k++ )
    1181             :                     {
    1182    57395232 :                         buffer_intensity_real_single_q = s_min( buffer_intensity_real_single_q, hDirAC->buffer_intensity_real_q[i][j][k] );
    1183             :                     }
    1184             :                 }
    1185             :             }
    1186      642000 :             buffer_intensity_real_single_q = add( buffer_intensity_real_single_q, min_shift );
    1187     2568000 :             FOR( i = 0; i < DIRAC_NUM_DIMS; i++ )
    1188             :             {
    1189    17334000 :                 FOR( j = 0; j < hDirAC->no_col_avg_diff; j++ )
    1190             :                 {
    1191    72803232 :                     FOR( k = 0; k < num_freq_bands; k++ )
    1192             :                     {
    1193    57395232 :                         hDirAC->buffer_intensity_real_fx[i][j][k] = L_shl( hDirAC->buffer_intensity_real_fx[i][j][k], sub( buffer_intensity_real_single_q, hDirAC->buffer_intensity_real_q[i][j][k] ) ); /* buffer_intensity_real_single_q */
    1194    57395232 :                         move32();
    1195    57395232 :                         hDirAC->buffer_intensity_real_q[i][j][k] = buffer_intensity_real_single_q;
    1196    57395232 :                         move16();
    1197             :                     }
    1198             :                 }
    1199             :             }
    1200             : 
    1201             :             Word16 buffer_energy_single_q;
    1202             :             Word16 buffer_len;
    1203      642000 :             buffer_len = imult1616( DIRAC_MAX_NBANDS, hDirAC->no_col_avg_diff );
    1204      642000 :             minimum_fx( hDirAC->buffer_energy_q, buffer_len, &buffer_energy_single_q );
    1205    62274000 :             FOR( i = 0; i < buffer_len; i++ )
    1206             :             {
    1207    61632000 :                 hDirAC->buffer_energy_fx[i] = L_shl( hDirAC->buffer_energy_fx[i], sub( buffer_energy_single_q, hDirAC->buffer_energy_q[i] ) ); /* buffer_energy_single_q */
    1208    61632000 :                 move32();
    1209    61632000 :                 hDirAC->buffer_energy_q[i] = buffer_energy_single_q;
    1210    61632000 :                 move16();
    1211             :             }
    1212             : 
    1213             :             Word16 tmp_q_buffer_for_intensity[DIRAC_MAX_NBANDS * DIRAC_NO_COL_AVG_DIFF];
    1214             :             Word16 tmp_q_buffer_for_energy[DIRAC_MAX_NBANDS * DIRAC_NO_COL_AVG_DIFF];
    1215             : 
    1216      642000 :             set16_fx( tmp_q_buffer_for_intensity, buffer_intensity_real_single_q, buffer_len );
    1217      642000 :             set16_fx( tmp_q_buffer_for_energy, buffer_energy_single_q, buffer_len );
    1218             : 
    1219      642000 :             computeDiffuseness_mdft_fx( hDirAC->buffer_intensity_real_fx, hDirAC->buffer_energy_fx, num_freq_bands, hDirAC->no_col_avg_diff, diffuseness_vector_fx, tmp_q_buffer_for_intensity, tmp_q_buffer_for_energy, &diffuseness_vector_q );
    1220             : 
    1221      642000 :             IF( hodirac_flag )
    1222             :             {
    1223       64960 :                 calculate_hodirac_sector_parameters_fx(
    1224             :                     hDirAC,
    1225             :                     Cldfb_RealBuffer_fx,
    1226             :                     Cldfb_ImagBuffer_fx,
    1227             :                     cldfb_q,
    1228             :                     HODIRAC_BETA,
    1229       64960 :                     hDirAC->band_grouping,
    1230       64960 :                     hDirAC->hConfig->nbands,
    1231       64960 :                     hDirAC->hConfig->enc_param_start_band,
    1232             :                     azi_secs_fx,
    1233             :                     ele_secs_fx,
    1234             :                     diff_secs_fx,
    1235             :                     diff_secs_exp,
    1236             :                     ene_secs_fx,
    1237             :                     ene_secs_exp );
    1238             :             }
    1239             : 
    1240      642000 :             IF( hodirac_flag )
    1241             :             {
    1242      779520 :                 FOR( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
    1243             :                 {
    1244      714560 :                     Word32 tmp_diff = L_shr( diffuseness_vector_fx[band_m_idx], sub( diffuseness_vector_q, 30 ) ); // diffueseness_vector_q -> Q30
    1245      714560 :                     hDirAC->diffuseness_m_fx[band_m_idx] = BASOP_Util_Add_Mant32Exp( hDirAC->diffuseness_m_fx[band_m_idx], hDirAC->diffuseness_m_exp[band_m_idx],
    1246      714560 :                                                                                      Mpy_32_32( reference_power_fx[ts][band_m_idx], tmp_diff ), add( reference_power_exp[ts][band_m_idx], 1 ), &hDirAC->diffuseness_m_exp[band_m_idx] ); /* exp(hDirAC->diffuseness_m_exp) */
    1247      714560 :                     move32();
    1248      714560 :                     renormalization_factor_diff_fx[band_m_idx] = BASOP_Util_Add_Mant32Exp( renormalization_factor_diff_fx[band_m_idx], renormalization_factor_diff_exp[band_m_idx], reference_power_fx[ts][band_m_idx], reference_power_exp[ts][band_m_idx], &renormalization_factor_diff_exp[band_m_idx] ); /* exp(renormalization_factor_diff_exp) */
    1249      714560 :                     move32();
    1250             :                 }
    1251             :             }
    1252             :             ELSE
    1253             :             {
    1254     2253948 :                 FOR( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
    1255             :                 {
    1256     1676908 :                     Word32 tmp_diff = L_shr( diffuseness_vector_fx[band_m_idx], sub( diffuseness_vector_q, 30 ) ); // diffueseness_vector_q -> Q30
    1257     1676908 :                     norm_tmp_exp = add( reference_power_exp[ts][band_m_idx], 1 );
    1258     1676908 :                     norm_tmp_fx = Mpy_32_32( reference_power_fx[ts][band_m_idx], L_sub( ONE_IN_Q30, tmp_diff ) );
    1259             : 
    1260     1676908 :                     dir[0][band_m_idx] = W_mult_32_32( norm_tmp_fx, direction_vector_fx[0][band_m_idx] );
    1261     1676908 :                     move64();
    1262     1676908 :                     dir[1][band_m_idx] = W_mult_32_32( norm_tmp_fx, direction_vector_fx[1][band_m_idx] );
    1263     1676908 :                     move64();
    1264     1676908 :                     dir[2][band_m_idx] = W_mult_32_32( norm_tmp_fx, direction_vector_fx[2][band_m_idx] );
    1265     1676908 :                     move64();
    1266     1676908 :                     Word16 norm = 62;
    1267     1676908 :                     move16();
    1268     1676908 :                     IF( dir[0][band_m_idx] != 0 )
    1269             :                     {
    1270     1624972 :                         norm = W_norm( dir[0][band_m_idx] );
    1271             :                     }
    1272     1676908 :                     IF( dir[1][band_m_idx] != 0 )
    1273             :                     {
    1274     1623492 :                         norm = s_min( norm, W_norm( dir[1][band_m_idx] ) );
    1275             :                     }
    1276     1676908 :                     IF( dir[2][band_m_idx] != 0 )
    1277             :                     {
    1278     1521567 :                         norm = s_min( norm, W_norm( dir[2][band_m_idx] ) );
    1279             :                     }
    1280     1676908 :                     Word32 tmp = W_extract_h( W_shl( dir[0][band_m_idx], norm ) );
    1281     1676908 :                     Word16 tmp_e = sub( Q31, add( sub( direction_vector_q, norm_tmp_exp ), sub( norm, 0 ) ) );
    1282     1676908 :                     hDirAC->direction_vector_m_fx[0][block_m_idx][band_m_idx] = BASOP_Util_Add_Mant32Exp( hDirAC->direction_vector_m_fx[0][block_m_idx][band_m_idx], direction_vector_exp_tmp[0][block_m_idx][band_m_idx], tmp, tmp_e, &direction_vector_exp_tmp[0][block_m_idx][band_m_idx] );
    1283     1676908 :                     move32();
    1284     1676908 :                     tmp = W_extract_h( W_shl( dir[1][band_m_idx], norm ) );
    1285     1676908 :                     hDirAC->direction_vector_m_fx[1][block_m_idx][band_m_idx] = BASOP_Util_Add_Mant32Exp( hDirAC->direction_vector_m_fx[1][block_m_idx][band_m_idx], direction_vector_exp_tmp[1][block_m_idx][band_m_idx], tmp, tmp_e, &direction_vector_exp_tmp[1][block_m_idx][band_m_idx] );
    1286     1676908 :                     move32();
    1287     1676908 :                     tmp = W_extract_h( W_shl( dir[2][band_m_idx], norm ) );
    1288     1676908 :                     hDirAC->direction_vector_m_fx[2][block_m_idx][band_m_idx] = BASOP_Util_Add_Mant32Exp( hDirAC->direction_vector_m_fx[2][block_m_idx][band_m_idx], direction_vector_exp_tmp[2][block_m_idx][band_m_idx], tmp, tmp_e, &direction_vector_exp_tmp[2][block_m_idx][band_m_idx] );
    1289     1676908 :                     move32();
    1290     1676908 :                     renormalization_factor_fx[band_m_idx] = BASOP_Util_Add_Mant32Exp( renormalization_factor_fx[band_m_idx], renormalization_factor_exp[band_m_idx], norm_tmp_fx, norm_tmp_exp, &renormalization_factor_exp[band_m_idx] ); /* exp(norm_tmp_exp + gbits) */
    1291     1676908 :                     move32();
    1292     1676908 :                     hDirAC->diffuseness_m_fx[band_m_idx] = BASOP_Util_Add_Mant32Exp( hDirAC->diffuseness_m_fx[band_m_idx], hDirAC->diffuseness_m_exp[band_m_idx],
    1293     1676908 :                                                                                      Mpy_32_32( reference_power_fx[ts][band_m_idx], tmp_diff ), add( reference_power_exp[ts][band_m_idx], 1 ), &hDirAC->diffuseness_m_exp[band_m_idx] ); /* exp(hDirAC->diffuseness_m_exp) */
    1294     1676908 :                     move32();
    1295     3353816 :                     renormalization_factor_diff_fx[band_m_idx] = BASOP_Util_Add_Mant32Exp( renormalization_factor_diff_fx[band_m_idx], renormalization_factor_diff_exp[band_m_idx],
    1296     1676908 :                                                                                            reference_power_fx[ts][band_m_idx], reference_power_exp[ts][band_m_idx], &renormalization_factor_diff_exp[band_m_idx] ); /* exp(renormalization_factor_diff_exp) */
    1297     1676908 :                     move32();
    1298             :                 }
    1299             :             }
    1300             :         }
    1301             : 
    1302      601341 :         IF( !hodirac_flag )
    1303             :         {
    1304     2131971 :             FOR( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
    1305             :             {
    1306     1595590 :                 renormalization_factor_fx[band_m_idx] = EPSILON_FX;
    1307     1595590 :                 move32();
    1308     1595590 :                 renormalization_factor_exp[band_m_idx] = 0;
    1309     1595590 :                 move16();
    1310     1595590 :                 Word16 tmp_e = shl( direction_vector_exp_tmp[0][block_m_idx][band_m_idx], 1 );
    1311     1595590 :                 renormalization_factor_fx[band_m_idx] = BASOP_Util_Add_Mant32Exp( renormalization_factor_fx[band_m_idx], renormalization_factor_exp[band_m_idx], Mpy_32_32( hDirAC->direction_vector_m_fx[0][block_m_idx][band_m_idx], hDirAC->direction_vector_m_fx[0][block_m_idx][band_m_idx] ), tmp_e, &renormalization_factor_exp[band_m_idx] ); /* norm_tmp_exp + ( 31 - direction_vector_q ) + gbits */
    1312     1595590 :                 move32();
    1313     1595590 :                 tmp_e = shl( direction_vector_exp_tmp[1][block_m_idx][band_m_idx], 1 );
    1314     1595590 :                 renormalization_factor_fx[band_m_idx] = BASOP_Util_Add_Mant32Exp( renormalization_factor_fx[band_m_idx], renormalization_factor_exp[band_m_idx], Mpy_32_32( hDirAC->direction_vector_m_fx[1][block_m_idx][band_m_idx], hDirAC->direction_vector_m_fx[1][block_m_idx][band_m_idx] ), tmp_e, &renormalization_factor_exp[band_m_idx] ); /* norm_tmp_exp + ( 31 - direction_vector_q ) + gbits */
    1315     1595590 :                 move32();
    1316     1595590 :                 tmp_e = shl( direction_vector_exp_tmp[2][block_m_idx][band_m_idx], 1 );
    1317     1595590 :                 renormalization_factor_fx[band_m_idx] = BASOP_Util_Add_Mant32Exp( renormalization_factor_fx[band_m_idx], renormalization_factor_exp[band_m_idx], Mpy_32_32( hDirAC->direction_vector_m_fx[2][block_m_idx][band_m_idx], hDirAC->direction_vector_m_fx[2][block_m_idx][band_m_idx] ), tmp_e, &renormalization_factor_exp[band_m_idx] ); /* norm_tmp_exp + ( 31 - direction_vector_q ) + gbits */
    1318     1595590 :                 move32();
    1319     1595590 :                 IF( renormalization_factor_fx[band_m_idx] == 0 )
    1320             :                 {
    1321       50016 :                     renormalization_factor_fx[band_m_idx] = EPSILON_FX;
    1322       50016 :                     move32();
    1323       50016 :                     renormalization_factor_exp[band_m_idx] = 0;
    1324       50016 :                     move16();
    1325             :                 }
    1326     1595590 :                 renormalization_factor_fx[band_m_idx] = Sqrt32( renormalization_factor_fx[band_m_idx], &renormalization_factor_exp[band_m_idx] );
    1327     1595590 :                 move32();
    1328             : 
    1329     1595590 :                 IF( GT_32( renormalization_factor_fx[band_m_idx], EPSILON_FX ) )
    1330             :                 {
    1331             :                     Word32 tmp32;
    1332             : 
    1333     1595590 :                     tmp32 = hDirAC->direction_vector_m_fx[0][block_m_idx][band_m_idx];
    1334     1595590 :                     move32();
    1335     1595590 :                     tmp32 = L_deposit_h( BASOP_Util_Divide3232_Scale( tmp32, renormalization_factor_fx[band_m_idx], &tmp_e ) ); /* exp(tmp_e + ( hDirAC->direction_vector_m_exp[block_m_idx] - renormalization_factor_exp)) */
    1336     1595590 :                     tmp_e = add( tmp_e, sub( direction_vector_exp_tmp[0][block_m_idx][band_m_idx], renormalization_factor_exp[band_m_idx] ) );
    1337     1595590 :                     hDirAC->direction_vector_m_fx[0][block_m_idx][band_m_idx] = L_shr_sat( tmp32, sub( 1, tmp_e ) ); // Q30 saturation needs to check
    1338     1595590 :                     move32();
    1339             : 
    1340     1595590 :                     tmp32 = hDirAC->direction_vector_m_fx[1][block_m_idx][band_m_idx]; // Q30
    1341     1595590 :                     move32();
    1342     1595590 :                     tmp32 = L_deposit_h( BASOP_Util_Divide3232_Scale( tmp32, renormalization_factor_fx[band_m_idx], &tmp_e ) ); /* exp(tmp_e + ( hDirAC->direction_vector_m_exp[block_m_idx] - renormalization_factor_exp)) */
    1343     1595590 :                     tmp_e = add( tmp_e, sub( direction_vector_exp_tmp[1][block_m_idx][band_m_idx], renormalization_factor_exp[band_m_idx] ) );
    1344     1595590 :                     hDirAC->direction_vector_m_fx[1][block_m_idx][band_m_idx] = L_shr_sat( tmp32, sub( 1, tmp_e ) ); // Q30 saturation needs to check
    1345     1595590 :                     move32();
    1346             : 
    1347     1595590 :                     tmp32 = hDirAC->direction_vector_m_fx[2][block_m_idx][band_m_idx]; // Q30
    1348     1595590 :                     move32();
    1349     1595590 :                     tmp32 = L_deposit_h( BASOP_Util_Divide3232_Scale( tmp32, renormalization_factor_fx[band_m_idx], &tmp_e ) ); /* exp(tmp_e + ( hDirAC->direction_vector_m_exp[block_m_idx] - renormalization_factor_exp)) */
    1350     1595590 :                     tmp_e = add( tmp_e, sub( direction_vector_exp_tmp[2][block_m_idx][band_m_idx], renormalization_factor_exp[band_m_idx] ) );
    1351     1595590 :                     hDirAC->direction_vector_m_fx[2][block_m_idx][band_m_idx] = L_shr_sat( tmp32, sub( 1, tmp_e ) ); // Q30 saturation needs to check
    1352     1595590 :                     move32();
    1353             :                 }
    1354             :                 ELSE
    1355             :                 {
    1356           0 :                     hDirAC->direction_vector_m_fx[0][block_m_idx][band_m_idx] = ONE_IN_Q30;
    1357           0 :                     move32();
    1358           0 :                     hDirAC->direction_vector_m_fx[1][block_m_idx][band_m_idx] = 0;
    1359           0 :                     move32();
    1360           0 :                     hDirAC->direction_vector_m_fx[2][block_m_idx][band_m_idx] = 0;
    1361           0 :                     move32();
    1362             :                 }
    1363             : 
    1364             :                 /* save the elevation and azimuth values to be used later by the ivas_dirac_QuantizeParameters function */
    1365     6382360 :                 FOR( d = 0; d < DIRAC_NUM_DIMS; d++ )
    1366             :                 {
    1367     4786770 :                     dir_v_fx[d] = hDirAC->direction_vector_m_fx[d][block_m_idx][band_m_idx]; // Q30
    1368     4786770 :                     move32();
    1369             :                 }
    1370             : 
    1371     1595590 :                 Scale_sig32( dir_v_fx, 3, -1 );
    1372     1595590 :                 ivas_qmetadata_direction_vector_to_azimuth_elevation_fx(
    1373             :                     dir_v_fx,
    1374             :                     Q29,
    1375     1595590 :                     &q_direction->band_data[band_m_idx].azimuth_fx[block_m_idx],
    1376     1595590 :                     &q_direction->band_data[band_m_idx].elevation_fx[block_m_idx] );
    1377             :             }
    1378      536381 :             hDirAC->direction_vector_m_exp[block_m_idx] = 1;
    1379      536381 :             move16();
    1380             :         }
    1381             : 
    1382             :         /* Sectors */
    1383      601341 :         IF( hodirac_flag )
    1384             :         {
    1385             :             Word32 L_tmp, L_tmp1;
    1386             :             Word16 e_tmp, e_tmp1, exp_diff;
    1387      779520 :             FOR( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
    1388             :             {
    1389      714560 :                 q_direction->band_data[band_m_idx].azimuth_fx[block_m_idx] = L_shr( azi_secs_fx[band_m_idx], 1 ); // Q23 -> Q22
    1390      714560 :                 move32();
    1391      714560 :                 q_direction->band_data[band_m_idx].elevation_fx[block_m_idx] = L_shr( ele_secs_fx[band_m_idx], 1 ); // Q23 -> Q22
    1392      714560 :                 move32();
    1393             : 
    1394      714560 :                 q_direction[1].band_data[band_m_idx].azimuth_fx[block_m_idx] = L_shr( azi_secs_fx[num_freq_bands + band_m_idx], 1 ); // Q23 -> Q22
    1395      714560 :                 move32();
    1396      714560 :                 q_direction[1].band_data[band_m_idx].elevation_fx[block_m_idx] = L_shr( ele_secs_fx[num_freq_bands + band_m_idx], 1 ); // Q23 -> Q22
    1397      714560 :                 move32();
    1398             : 
    1399      714560 :                 L_tmp = BASOP_Util_Add_Mant32Exp( ONE_IN_Q30, Q1, L_negate( diff_secs_fx[band_m_idx] ), diff_secs_exp[band_m_idx], &e_tmp );                                     /* exp(e_tmp) */
    1400      714560 :                 L_tmp1 = BASOP_Util_Add_Mant32Exp( ONE_IN_Q30, Q1, L_negate( diff_secs_fx[num_freq_bands + band_m_idx] ), diff_secs_exp[num_freq_bands + band_m_idx], &e_tmp1 ); /* exp(e_tmp1) */
    1401      714560 :                 L_tmp1 = BASOP_Util_Add_Mant32Exp( L_tmp, e_tmp, L_tmp1, e_tmp1, &e_tmp1 );                                                                                      /* exp(e_tmp1) */
    1402      714560 :                 IF( L_tmp1 == 0 )
    1403             :                 {
    1404        4000 :                     q_direction[1].band_data[band_m_idx].energy_ratio_fx[block_m_idx] = L_shl( div_l( L_tmp, EPSILON_FX ), Q30 - Q15 ); // Q30
    1405        4000 :                     move32();
    1406             :                 }
    1407             :                 ELSE
    1408             :                 {
    1409      710560 :                     L_tmp = L_deposit_h( BASOP_Util_Divide3232_Scale( L_tmp, L_add( L_tmp1, EPSILON_FX ), &exp_diff ) ); /* exp(exp_diff) */
    1410      710560 :                     exp_diff = add( exp_diff, sub( e_tmp, e_tmp1 ) );
    1411      710560 :                     q_direction[1].band_data[band_m_idx].energy_ratio_fx[block_m_idx] = L_shl( L_tmp, sub( exp_diff, Q1 ) ); // Q30
    1412      710560 :                     move32();
    1413             :                 }
    1414             :             }
    1415             :         }
    1416             :     }
    1417             : 
    1418             :     /* Diffuseness */
    1419      758367 :     FOR( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
    1420             :     {
    1421      597867 :         IF( GT_32( renormalization_factor_diff_fx[band_m_idx], EPSILON_FX ) )
    1422             :         {
    1423             :             Word16 tmp_e;
    1424      585738 :             hDirAC->diffuseness_m_fx[band_m_idx] = L_deposit_h( BASOP_Util_Divide3232_Scale( hDirAC->diffuseness_m_fx[band_m_idx], renormalization_factor_diff_fx[band_m_idx], &tmp_e ) ); /* exp(tmp_e) */
    1425      585738 :             move32();
    1426      585738 :             tmp_e = add( tmp_e, sub( hDirAC->diffuseness_m_exp[band_m_idx], renormalization_factor_diff_exp[band_m_idx] ) );
    1427      585738 :             hDirAC->diffuseness_m_fx[band_m_idx] = L_shr( hDirAC->diffuseness_m_fx[band_m_idx], sub( 1, tmp_e ) ); // to Q30
    1428      585738 :             move32();
    1429             :         }
    1430             :         ELSE
    1431             :         {
    1432       12129 :             hDirAC->diffuseness_m_fx[band_m_idx] = 0;
    1433       12129 :             move32();
    1434             :         }
    1435      597867 :         hDirAC->diffuseness_m_exp[band_m_idx] = 1;
    1436      597867 :         move16();
    1437             : 
    1438             :         /* set coherence to zero and convert diffuseness to energy ratio*/
    1439             : 
    1440      597867 :         set32_fx( q_direction->band_data[band_m_idx].energy_ratio_fx, L_sub( ONE_IN_Q30, hDirAC->diffuseness_m_fx[band_m_idx] ), q_direction->cfg.nblocks );
    1441             : 
    1442     2908017 :         FOR( block_m_idx = 0; block_m_idx < q_direction->cfg.nblocks; block_m_idx++ )
    1443             :         {
    1444     2310150 :             IF( q_direction->coherence_band_data != NULL )
    1445             :             {
    1446           0 :                 q_direction->coherence_band_data[band_m_idx].spread_coherence[block_m_idx] = 0;
    1447           0 :                 move16();
    1448             :             }
    1449             :         }
    1450             :     }
    1451             : 
    1452      160500 :     pop_wmops();
    1453      160500 :     return;
    1454             : }
    1455             : 
    1456             : 
    1457             : /*-------------------------------------------------------------------------
    1458             :  * computeIntensityVector_enc()
    1459             :  *
    1460             :  *
    1461             :  *------------------------------------------------------------------------*/
    1462             : 
    1463      642000 : static void computeIntensityVector_enc_fx(
    1464             :     const DIRAC_ENC_HANDLE hDirAC,
    1465             :     Word32 Cldfb_RealBuffer[DIRAC_MAX_ANA_CHANS][DIRAC_NO_FB_BANDS_MAX], /* q_cldfb */
    1466             :     Word32 Cldfb_ImagBuffer[DIRAC_MAX_ANA_CHANS][DIRAC_NO_FB_BANDS_MAX], /* q_cldfb */
    1467             :     const Word16 enc_param_start_band,                                   /* i  : first band to process  Q0*/
    1468             :     const Word16 num_frequency_bands,                                    /* Q0 */
    1469             :     Word32 intensity_real[DIRAC_NUM_DIMS][DIRAC_MAX_NBANDS],             /* q_intensity_real */
    1470             :     Word16 q_cldfb,
    1471             :     Word16 q_intensity_real[DIRAC_MAX_NBANDS] )
    1472             : {
    1473             :     /* Reminder
    1474             :      * X = a + ib; Y = c + id
    1475             :      * X*Y = ac - bd + i(ad +bc)
    1476             :      */
    1477             :     Word16 i, j;
    1478             :     Word32 real, img;
    1479             :     Word16 brange[2];
    1480      642000 :     Word16 shift_value = sub( shl( q_cldfb, 1 ), 31 );
    1481     3033468 :     FOR( i = 0; i < num_frequency_bands; i++ )
    1482             :     {
    1483     2391468 :         brange[0] = hDirAC->band_grouping[i + enc_param_start_band]; /* Q0 */
    1484     2391468 :         move16();
    1485     2391468 :         brange[1] = hDirAC->band_grouping[i + enc_param_start_band + 1]; /* Q0 */
    1486     2391468 :         move16();
    1487     2391468 :         Word16 num_bins = sub( brange[1], brange[0] );
    1488     2391468 :         Word16 gb = find_guarded_bits_fx( num_bins );
    1489             :         Word16 norm;
    1490     2391468 :         intensity_real[0][i] = 0;
    1491     2391468 :         move32();
    1492     2391468 :         intensity_real[1][i] = 0;
    1493     2391468 :         move32();
    1494     2391468 :         intensity_real[2][i] = 0;
    1495     2391468 :         move32();
    1496             : 
    1497     2391468 :         Word64 tmp_1 = 0, tmp_2 = 0, tmp_3 = 0;
    1498     2391468 :         move64();
    1499     2391468 :         move64();
    1500     2391468 :         move64();
    1501             : 
    1502    89477228 :         FOR( j = brange[0]; j < brange[1]; j++ )
    1503             :         {
    1504    87085760 :             real = Cldfb_RealBuffer[0][j];
    1505    87085760 :             move32();
    1506    87085760 :             img = Cldfb_ImagBuffer[0][j];
    1507    87085760 :             move32();
    1508             :             Word64 t1, t2, t3;
    1509    87085760 :             t1 = W_mac_32_32( W_mult_32_32( Cldfb_RealBuffer[3][j], real ), Cldfb_ImagBuffer[3][j], img ); /* 2 * q_cldfb + 1 */
    1510    87085760 :             t2 = W_mac_32_32( W_mult_32_32( Cldfb_RealBuffer[1][j], real ), Cldfb_ImagBuffer[1][j], img ); /* 2 * q_cldfb + 1 */
    1511    87085760 :             t3 = W_mac_32_32( W_mult_32_32( Cldfb_RealBuffer[2][j], real ), Cldfb_ImagBuffer[2][j], img ); /* 2 * q_cldfb + 1 */
    1512    87085760 :             t1 = W_shr( t1, gb );
    1513    87085760 :             t2 = W_shr( t2, gb );
    1514    87085760 :             t3 = W_shr( t3, gb );
    1515             :             /* Intensity is XYZ order, audio is WYZX order. */
    1516    87085760 :             tmp_1 = W_add( tmp_1, t1 ); /* 2 * q_cldfb + 1 */
    1517    87085760 :             tmp_2 = W_add( tmp_2, t2 ); /* 2 * q_cldfb + 1 */
    1518    87085760 :             tmp_3 = W_add( tmp_3, t3 ); /* 2 * q_cldfb + 1 */
    1519             :         }
    1520     2391468 :         norm = 63;
    1521     2391468 :         move16();
    1522     2391468 :         IF( tmp_1 != 0 )
    1523             :         {
    1524     2334086 :             norm = s_min( norm, W_norm( tmp_1 ) );
    1525             :         }
    1526     2391468 :         IF( tmp_2 != 0 )
    1527             :         {
    1528     2334109 :             norm = s_min( norm, W_norm( tmp_2 ) );
    1529             :         }
    1530     2391468 :         IF( tmp_3 != 0 )
    1531             :         {
    1532     2136701 :             norm = s_min( norm, W_norm( tmp_3 ) );
    1533             :         }
    1534     2391468 :         intensity_real[0][i] = W_extract_h( W_shl( tmp_1, norm ) ); // shift_value - (gb - norm)
    1535     2391468 :         move32();
    1536     2391468 :         intensity_real[1][i] = W_extract_h( W_shl( tmp_2, norm ) ); // shift_value - (gb - norm)
    1537     2391468 :         move32();
    1538     2391468 :         intensity_real[2][i] = W_extract_h( W_shl( tmp_3, norm ) ); // shift_value - (gb - norm)
    1539     2391468 :         q_intensity_real[i] = sub( shift_value, sub( gb, norm ) );
    1540     2391468 :         move16();
    1541             :     }
    1542             : 
    1543      642000 :     return;
    1544             : }
    1545             : 
    1546             : /*-------------------------------------------------------------------------
    1547             :  * computeDiffuseness_mdft()
    1548             :  *
    1549             :  *
    1550             :  *------------------------------------------------------------------------*/
    1551      688720 : void computeDiffuseness_mdft_fx(
    1552             :     Word32 **buffer_intensity[DIRAC_NUM_DIMS], /* q_factor_intensity */
    1553             :     const Word32 *buffer_energy,               /* q_factor_energy */
    1554             :     const Word16 num_freq_bands,               /* Q0 */
    1555             :     const UWord16 no_col_avg_diff,             /* Q0 */
    1556             :     Word32 *diffuseness,                       /* q_diffuseness */
    1557             :     Word16 *q_factor_intensity,
    1558             :     Word16 *q_factor_energy,
    1559             :     Word16 *q_diffuseness /*Ouput Q*/
    1560             : )
    1561             : {
    1562             :     Word32 intensity_slow[DIRAC_NUM_DIMS * MASA_FREQUENCY_BANDS];
    1563             :     Word16 intensity_slow_e[DIRAC_NUM_DIMS * MASA_FREQUENCY_BANDS];
    1564             :     Word32 intensity_slow_abs[MASA_FREQUENCY_BANDS];
    1565             :     Word16 intensity_slow_abs_e[MASA_FREQUENCY_BANDS];
    1566             :     Word32 energy_slow[MASA_FREQUENCY_BANDS];
    1567             :     Word16 energy_slow_e[MASA_FREQUENCY_BANDS];
    1568             :     Word16 i, j, k;
    1569             :     Word32 tmp;
    1570             :     Word32 *p_tmp;
    1571             :     const Word32 *p_tmp_c;
    1572             :     Word16 *p_tmp_e, tmp_q;
    1573             : 
    1574             :     /* Compute Intensity slow and energy slow buffer_intensity and buffer_energy */
    1575             : 
    1576      688720 :     set_zero_fx( intensity_slow, i_mult( DIRAC_NUM_DIMS, MASA_FREQUENCY_BANDS ) );
    1577      688720 :     set16_fx( intensity_slow_e, 0, i_mult( DIRAC_NUM_DIMS, MASA_FREQUENCY_BANDS ) );
    1578      688720 :     set_zero_fx( intensity_slow_abs, MASA_FREQUENCY_BANDS );
    1579      688720 :     set16_fx( intensity_slow_abs_e, 0, no_col_avg_diff );
    1580      688720 :     set_zero_fx( energy_slow, MASA_FREQUENCY_BANDS );
    1581      688720 :     set16_fx( energy_slow_e, 0, no_col_avg_diff );
    1582             : 
    1583     6198480 :     FOR( i = 0; i < no_col_avg_diff; ++i )
    1584             :     {
    1585             :         /* Energy slow */
    1586     5509760 :         p_tmp_c = buffer_energy + i_mult( i, num_freq_bands );
    1587     5509760 :         tmp_q = q_factor_energy[i];
    1588     5509760 :         move16();
    1589             : 
    1590    26510304 :         FOR( k = 0; k < num_freq_bands; k++ )
    1591             :         {
    1592    21000544 :             energy_slow[k] = BASOP_Util_Add_Mant32Exp( energy_slow[k], energy_slow_e[k], p_tmp_c[k], sub( 31, tmp_q ), &energy_slow_e[k] ); /* exp(energy_slow_e)*/
    1593    21000544 :             move32();
    1594             :         }
    1595             : 
    1596             :         /* Intensity slow */
    1597    22039040 :         FOR( j = 0; j < DIRAC_NUM_DIMS; ++j )
    1598             :         {
    1599    16529280 :             p_tmp = buffer_intensity[j][i];
    1600    79530912 :             FOR( k = 0; k < num_freq_bands; k++ )
    1601             :             {
    1602    63001632 :                 intensity_slow[j * num_freq_bands + k] = BASOP_Util_Add_Mant32Exp( intensity_slow[j * num_freq_bands + k], intensity_slow_e[j * num_freq_bands + k], p_tmp[k], sub( 31, q_factor_intensity[i] ), &intensity_slow_e[j * num_freq_bands + k] ); /* exp(intensity_slow_e) */
    1603    63001632 :                 move32();
    1604             :             }
    1605             :         }
    1606             :     }
    1607             : 
    1608             :     /* intensity_slow.^2 + intensity_slow_abs*/
    1609     2754880 :     FOR( j = 0; j < DIRAC_NUM_DIMS; ++j )
    1610             :     {
    1611     2066160 :         p_tmp = intensity_slow + i_mult( j, num_freq_bands );
    1612     2066160 :         p_tmp_e = intensity_slow_e + i_mult( j, num_freq_bands );
    1613             : 
    1614     9941364 :         FOR( k = 0; k < num_freq_bands; k++ )
    1615             :         {
    1616     7875204 :             p_tmp[k] = Mpy_32_32( p_tmp[k], p_tmp[k] ); /* 2 * (31-p_tmp_e) - 31 */
    1617     7875204 :             move32();
    1618     7875204 :             tmp_q = sub( shl( sub( 31, p_tmp_e[k] ), 1 ), 31 );
    1619     7875204 :             intensity_slow_abs[k] = BASOP_Util_Add_Mant32Exp( intensity_slow_abs[k], intensity_slow_abs_e[k], p_tmp[k], sub( 31, tmp_q ), &intensity_slow_abs_e[k] ); /* exp(intensity_slow_abs_e) */
    1620     7875204 :             move32();
    1621             :         }
    1622             :     }
    1623             : 
    1624             :     /* Compute Diffuseness */
    1625      688720 :     p_tmp = intensity_slow_abs;
    1626      688720 :     tmp_q = 0;
    1627      688720 :     move16();
    1628     3313788 :     FOR( i = 0; i < num_freq_bands; ++i )
    1629             :     {
    1630     2625068 :         tmp = Sqrt32( p_tmp[i], &intensity_slow_abs_e[i] );
    1631             : 
    1632     2625068 :         tmp = L_deposit_h( BASOP_Util_Divide3232_Scale( tmp, L_add( energy_slow[i], EPSILLON_FX ), &tmp_q ) ); /* exp(tmp_q) */
    1633     2625068 :         tmp_q = add( tmp_q, sub( intensity_slow_abs_e[i], energy_slow_e[i] ) );
    1634     2625068 :         tmp = L_shl_sat( tmp, tmp_q );
    1635     2625068 :         tmp = L_sub( ONE_IN_Q31, tmp );
    1636     2625068 :         diffuseness[i] = tmp;
    1637     2625068 :         move32();
    1638             : 
    1639     2625068 :         IF( LT_32( tmp, ONE_IN_Q31 ) )
    1640             :         {
    1641     2569670 :             IF( tmp < 0 )
    1642             :             {
    1643           0 :                 diffuseness[i] = 0;
    1644           0 :                 move32();
    1645             :             }
    1646             :         }
    1647             :         ELSE
    1648             :         {
    1649       55398 :             diffuseness[i] = ONE_IN_Q31;
    1650       55398 :             move32();
    1651             :         }
    1652             :     }
    1653      688720 :     *q_diffuseness = Q31;
    1654      688720 :     move16();
    1655             : 
    1656      688720 :     return;
    1657             : }

Generated by: LCOV version 1.14