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

Generated by: LCOV version 1.14