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 @ 574a190e3c6896c6c4ed10d7f23649709a0c4347 Lines: 738 793 93.1 %
Date: 2025-06-27 02:59:36 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      155895 :             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      155895 :             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      155895 :             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        1605 :             hQMetaData->q_direction[0].cfg.nbands = DIRAC_DTX_BANDS;
     401        1605 :             move16();
     402             :             /* compute directions */
     403        7473 :             FOR( i = 0; i < hQMetaData->q_direction[0].cfg.nblocks; i++ )
     404             :             {
     405        5868 :                 set32_fx( avg_dir_fx, 0, 3 );
     406        5868 :                 Word16 avg_dir_e = 0;
     407        5868 :                 Word16 energySum_e = 0;
     408        5868 :                 Word32 energySum_fx = 0;
     409        5868 :                 move32();
     410        5868 :                 move16();
     411        5868 :                 move16();
     412             : 
     413             :                 /* combine all DirAC bands except the last one, handle last band separately, last band covers BW above WB */
     414       17420 :                 FOR( j = 0; j < sub( orig_dirac_bands, 1 ); j++ )
     415             :                 {
     416       11552 :                     ivas_qmetadata_azimuth_elevation_to_direction_vector_fx( hQMetaData->q_direction[0].band_data[j].azimuth_fx[i], hQMetaData->q_direction[0].band_data[j].elevation_fx[i], &dir_fx[0] );
     417             : 
     418       11552 :                     Word32 vecLen_fx = Mpy_32_32( hQMetaData->q_direction[0].band_data[j].energy_ratio_fx[i] /*Q30*/, hDirAC->buffer_energy_fx[i * orig_dirac_bands + j] ); /* hDirAC->buffer_energy_q[i * orig_dirac_bands + j] - 1 */
     419       11552 :                     avg_dir_fx[0] = L_add( avg_dir_fx[0], Mpy_32_32( dir_fx[0], vecLen_fx ) );                                                                              // Q30 + hDirAC->buffer_energy_q[i * orig_dirac_bands + j] - 1 - 31
     420       11552 :                     avg_dir_fx[1] = L_add( avg_dir_fx[1], Mpy_32_32( dir_fx[1], vecLen_fx ) );                                                                              // Q30 + hDirAC->buffer_energy_q[i * orig_dirac_bands + j] - 1 - 31
     421       11552 :                     avg_dir_fx[2] = L_add( avg_dir_fx[2], Mpy_32_32( dir_fx[2], vecLen_fx ) );                                                                              // Q30 + hDirAC->buffer_energy_q[i * orig_dirac_bands + j] - 1 - 31
     422       11552 :                     move32();
     423       11552 :                     move32();
     424       11552 :                     move32();
     425       11552 :                     avg_dir_e = sub( 31, ( sub( add( Q30, hDirAC->buffer_energy_q[i * orig_dirac_bands + j] ), 1 - 31 ) ) );
     426       11552 :                     energySum_fx = BASOP_Util_Add_Mant32Exp( energySum_fx, energySum_e, hDirAC->buffer_energy_fx[i * orig_dirac_bands + j], sub( 31, hDirAC->buffer_energy_q[i * orig_dirac_bands + j] ), &energySum_e ); /* exp(energySum_e) */
     427             :                 }
     428             : 
     429        5868 :                 Word16 x = getScaleFactor32( avg_dir_fx, 3 );
     430        5868 :                 Scale_sig32( avg_dir_fx, 3, sub( x, 1 ) );
     431        5868 :                 avg_dir_e = sub( avg_dir_e, sub( x, 1 ) );
     432             : 
     433        5868 :                 ivas_qmetadata_direction_vector_to_azimuth_elevation_fx( &avg_dir_fx[0], Q30, &hQMetaData->q_direction[0].band_data[0].azimuth_fx[i], &hQMetaData->q_direction[0].band_data[0].elevation_fx[i] );
     434             : 
     435        5868 :                 Word32 tmp = dotp_fixed( avg_dir_fx, avg_dir_fx, 3 ); // 2*avg_dir_e
     436        5868 :                 Word16 tmp_e = shl( avg_dir_e, 1 );
     437        5868 :                 tmp = Sqrt32( tmp, &tmp_e );
     438        5868 :                 Word16 er_tmp_e = 0;
     439        5868 :                 move16();
     440        5868 :                 Word16 er_tmp = 0;
     441        5868 :                 move16();
     442        5868 :                 Word16 denom_e = 0;
     443        5868 :                 move16();
     444        5868 :                 Word32 denom = BASOP_Util_Add_Mant32Exp( energySum_fx, energySum_e, EPSILON_FX_M, EPSILON_FX_E, &denom_e ); /* exp(denom_e) */
     445             : 
     446        5868 :                 er_tmp = BASOP_Util_Divide3232_Scale( tmp, denom, &er_tmp_e ); /* exp(er_tmp_e) */
     447        5868 :                 er_tmp_e = add( er_tmp_e, sub( tmp_e, denom_e ) );
     448        5868 :                 er_tmp = shr( er_tmp, sub( 1, ( er_tmp_e ) ) ); // Q14
     449             : 
     450        5868 :                 hQMetaData->q_direction[0].band_data[0].energy_ratio_fx[i] = L_deposit_h( er_tmp );                                                         // Q30
     451        5868 :                 hQMetaData->q_direction[0].band_data[1].azimuth_fx[i] = hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].azimuth_fx[i];           // Q22
     452        5868 :                 hQMetaData->q_direction[0].band_data[1].elevation_fx[i] = hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].elevation_fx[i];       // Q22
     453        5868 :                 hQMetaData->q_direction[0].band_data[1].energy_ratio_fx[i] = hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].energy_ratio_fx[i]; // Q30
     454        5868 :                 move32();
     455        5868 :                 move32();
     456        5868 :                 move32();
     457        5868 :                 move32();
     458             :             }
     459             : 
     460             :             /* 1 bit to indicate mode MD coding : temp solution*/
     461        1605 :             push_next_indice( hMetaData, 1, 1 );
     462             : 
     463             :             /* encode SID parameters */
     464        1605 :             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      155895 :             len = hQMetaData->q_direction[0].cfg.nblocks;
     472      155895 :             move16();
     473             :         }
     474             :         ELSE
     475             :         {
     476        1605 :             len = 1;
     477        1605 :             move16();
     478             :         }
     479      761416 :         FOR( b = hQMetaData->q_direction->cfg.start_band; b < hQMetaData->q_direction->cfg.nbands; b++ )
     480             :         {
     481     2931116 :             FOR( i_ts = 0; i_ts < len; i_ts++ )
     482             :             {
     483     2327200 :                 hQMetaData->q_direction->band_data[b].azimuth_fx[i_ts] = hQMetaData->q_direction->band_data[b].q_azimuth_fx[i_ts];                                                               // Q22
     484     2327200 :                 hQMetaData->q_direction->band_data[b].elevation_fx[i_ts] = hQMetaData->q_direction->band_data[b].q_elevation_fx[i_ts];                                                           // Q22
     485     2327200 :                 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     2327200 :                 move32();
     487     2327200 :                 move32();
     488     2327200 :                 move32();
     489             :             }
     490             :         }
     491             : 
     492      157500 :         IF( dtx_vad == 0 )
     493             :         {
     494        8025 :             FOR( i = 0; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
     495             :             {
     496        6420 :                 hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].azimuth_fx[i] = hQMetaData->q_direction[0].band_data[1].azimuth_fx[0];           // Q22
     497        6420 :                 hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].elevation_fx[i] = hQMetaData->q_direction[0].band_data[1].elevation_fx[0];       // Q22
     498        6420 :                 hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].energy_ratio_fx[i] = hQMetaData->q_direction[0].band_data[1].energy_ratio_fx[0]; // Q30
     499        6420 :                 move32();
     500        6420 :                 move32();
     501        6420 :                 move32();
     502             :             }
     503             : 
     504        8025 :             FOR( i = 0; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
     505             :             {
     506       18524 :                 FOR( j = sub( orig_dirac_bands, 2 ); j >= 0; j-- )
     507             :                 {
     508       12104 :                     hQMetaData->q_direction[0].band_data[j].azimuth_fx[i] = hQMetaData->q_direction[0].band_data[0].azimuth_fx[0];           // Q22
     509       12104 :                     hQMetaData->q_direction[0].band_data[j].elevation_fx[i] = hQMetaData->q_direction[0].band_data[0].elevation_fx[0];       // Q22
     510       12104 :                     hQMetaData->q_direction[0].band_data[j].energy_ratio_fx[i] = hQMetaData->q_direction[0].band_data[0].energy_ratio_fx[0]; // Q30
     511       12104 :                     move32();
     512       12104 :                     move32();
     513       12104 :                     move32();
     514             :                 }
     515             :             }
     516             : 
     517        1605 :             hQMetaData->q_direction->cfg.nbands = orig_dirac_bands; /* Q0 */
     518        1605 :             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 q_reference_power[DIRAC_NO_FB_BANDS_MAX] )
     853             : {
     854             :     Word16 brange[2];
     855             :     Word16 ch_idx, i, j;
     856             :     Word16 num_bins, guard_bits, norm;
     857             : 
     858             :     Word64 reference_power_tmp[DIRAC_MAX_NBANDS];
     859             :     Word64 reference_power_W[DIRAC_MAX_NBANDS];
     860             : 
     861       45880 :     Word16 shift_ref_power = sub( shl( inp_q, 1 ), 31 );
     862             : 
     863      275280 :     FOR( i = 0; i < num_freq_bands; i++ )
     864             :     {
     865      229400 :         brange[0] = band_grouping[i + enc_param_start_band]; /* Q0 */
     866      229400 :         move16();
     867      229400 :         brange[1] = band_grouping[i + enc_param_start_band + 1]; /* Q0 */
     868      229400 :         move16();
     869      229400 :         reference_power[i] = 0;
     870      229400 :         move32();
     871      229400 :         reference_power_W[i] = 0;
     872      229400 :         move64();
     873      229400 :         reference_power_tmp[i] = 0;
     874      229400 :         move64();
     875             : 
     876      229400 :         num_bins = sub( brange[1], brange[0] );
     877      229400 :         guard_bits = find_guarded_bits_fx( num_bins );
     878             : 
     879    11176600 :         FOR( j = brange[0]; j < brange[1]; j++ )
     880             :         {
     881    10947200 :             reference_power_W[i] = W_add( reference_power_W[i], W_shr( W_mac_32_32( W_mult_32_32( Cldfb_RealBuffer[0][j], Cldfb_RealBuffer[0][j] ), Cldfb_ImagBuffer[0][j], Cldfb_ImagBuffer[0][j] ), guard_bits ) ); // 2*inp_q + 1 - guard_bits
     882    10947200 :             move64();
     883             :         }
     884      229400 :         reference_power_tmp[i] = W_add( reference_power_tmp[i], reference_power_W[i] ); // 2*inp_q + 1 - guard_bits
     885      229400 :         move64();
     886      917600 :         FOR( ch_idx = 1; ch_idx < nchan_ana; ch_idx++ )
     887             :         {
     888             :             /* abs()^2 */
     889    33529800 :             FOR( j = brange[0]; j < brange[1]; j++ )
     890             :             {
     891    32841600 :                 reference_power_tmp[i] = W_add( reference_power_tmp[i], W_shr( W_mac_32_32( W_mult_32_32( Cldfb_RealBuffer[ch_idx][j], Cldfb_RealBuffer[ch_idx][j] ), Cldfb_ImagBuffer[ch_idx][j], Cldfb_ImagBuffer[ch_idx][j] ), guard_bits ) ); // 2*inp_q + 1 - guard_bits
     892    32841600 :                 move64();
     893             :             }
     894             :         }
     895      229400 :         q_reference_power[i] = sub( shift_ref_power, guard_bits );
     896      229400 :         move16();
     897             :     }
     898      275280 :     FOR( i = 0; i < num_freq_bands; i++ )
     899             :     {
     900      229400 :         reference_power_tmp[i] = W_shr( reference_power_tmp[i], 1 ); // 2*inp_q + 1
     901      229400 :         move64();
     902             :     }
     903             : 
     904       45880 :     test();
     905       45880 :     IF( EQ_16( ivas_format, SBA_FORMAT ) && EQ_16( ref_power_w, 1 ) )
     906             :     {
     907           0 :         FOR( i = 0; i < num_freq_bands; i++ )
     908             :         {
     909           0 :             IF( LT_64( reference_power_tmp[i], reference_power_W[i] ) )
     910             :             {
     911           0 :                 reference_power_tmp[i] = reference_power_W[i]; // 2*inp_q + 1
     912           0 :                 move64();
     913             :             }
     914             :         }
     915             :     }
     916             : 
     917      275280 :     FOR( i = 0; i < num_freq_bands; i++ )
     918             :     {
     919      229400 :         norm = 63;
     920      229400 :         move16();
     921      229400 :         IF( reference_power_tmp[i] != 0 )
     922             :         {
     923      228015 :             norm = W_norm( reference_power_tmp[i] );
     924             :         }
     925      229400 :         reference_power[i] = W_extract_h( W_shl( reference_power_tmp[i], norm ) ); // 2 * inp_q + 1 - guard_bits + norm - 32
     926      229400 :         move32();
     927      229400 :         q_reference_power[i] = add( q_reference_power[i], norm );
     928      229400 :         move16();
     929             :     }
     930             : 
     931       45880 :     return;
     932             : }
     933             : /*-------------------------------------------------------------------------
     934             :  * ivas_dirac_param_est_enc()
     935             :  *
     936             :  *
     937             :  *------------------------------------------------------------------------*/
     938             : 
     939      157500 : void ivas_dirac_param_est_enc_fx(
     940             :     DIRAC_ENC_HANDLE hDirAC,
     941             :     IVAS_QDIRECTION *q_direction,
     942             :     const UWord8 useLowerRes, /* Q0 */
     943             :     Word32 *data_f_fx[],      /* Qx */
     944             :     Word32 **pp_fr_real_fx,   /* pp_fr_q */
     945             :     Word32 **pp_fr_imag_fx,   /* pp_fr_q */
     946             :     Word16 pp_fr_q,
     947             :     const Word16 input_frame, /* Q0 */
     948             :     const IVAS_FORMAT ivas_format,
     949             :     const Word16 hodirac_flag, /* Q0 */
     950             :     const Word16 nchan_fb_in,  /* Q0 */
     951             :     Word16 *mono_frame_count,  /* Q0 */
     952             :     Word16 *dirac_mono_flag,   /* Q0 */
     953             :     const Word16 shift )
     954             : {
     955             :     Word16 i, j, k, d, ts, index, l_ts, num_freq_bands;
     956             :     Word16 band_m_idx, block_m_idx;
     957             :     Word32 dir_v_fx[DIRAC_NUM_DIMS];
     958             :     Word32 *pcm_in_fx[DIRAC_MAX_ANA_CHANS];
     959             :     Word32 Cldfb_RealBuffer_fx[DIRAC_MAX_ANA_CHANS][DIRAC_NO_FB_BANDS_MAX];
     960             :     Word32 Cldfb_ImagBuffer_fx[DIRAC_MAX_ANA_CHANS][DIRAC_NO_FB_BANDS_MAX];
     961             :     Word32 *p_Cldfb_RealBuffer_fx[DIRAC_MAX_ANA_CHANS];
     962             :     Word32 *p_Cldfb_ImagBuffer_fx[DIRAC_MAX_ANA_CHANS];
     963             :     Word16 cldfb_q;
     964             :     Word32 intensity_real_fx[DIRAC_NUM_DIMS][DIRAC_MAX_NBANDS];
     965             :     Word16 intensity_real_q[DIRAC_MAX_NBANDS];
     966             :     Word32 direction_vector_fx[DIRAC_NUM_DIMS][DIRAC_MAX_NBANDS];
     967             :     Word16 direction_vector_q;
     968             :     Word32 diffuseness_vector_fx[DIRAC_MAX_NBANDS];
     969             :     Word16 diffuseness_vector_q;
     970             :     Word32 renormalization_factor_fx[DIRAC_MAX_NBANDS];
     971             :     Word16 renormalization_factor_exp[DIRAC_MAX_NBANDS];
     972             :     Word32 renormalization_factor_diff_fx[DIRAC_MAX_NBANDS];
     973             :     Word16 renormalization_factor_diff_exp[DIRAC_MAX_NBANDS];
     974             :     Word32 norm_tmp_fx;
     975             :     Word16 norm_tmp_exp;
     976             :     Word16 mrange[2];
     977             :     Word16 num_blocks;
     978             :     Word32 reference_power_fx[CLDFB_NO_COL_MAX][DIRAC_NO_FB_BANDS_MAX];
     979             :     Word16 reference_power_exp[CLDFB_NO_COL_MAX][DIRAC_NO_FB_BANDS_MAX];
     980             :     Word32 azi_secs_fx[2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS];
     981             :     Word32 ele_secs_fx[2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS];
     982             :     Word32 diff_secs_fx[2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS];
     983             :     Word16 diff_secs_exp[2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS];
     984             :     Word32 ene_secs_fx[2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS];
     985             :     Word16 ene_secs_exp[2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS];
     986             : 
     987      157500 :     push_wmops( "dirac_enc_param_est" );
     988      157500 :     num_freq_bands = hDirAC->hConfig->nbands; /* Q0 */
     989      157500 :     move16();
     990             : 
     991             :     /* Initialization */
     992      157500 :     cldfb_q = 0;
     993      157500 :     move16();
     994             : 
     995             : 
     996      157500 :     direction_vector_q = 0;
     997      157500 :     move16();
     998      157500 :     diffuseness_vector_q = 0;
     999      157500 :     move16();
    1000             : 
    1001      157500 :     l_ts = idiv1616( input_frame, MAX_PARAM_SPATIAL_SUBFRAMES ); /* Q0 */
    1002      157500 :     IF( useLowerRes )
    1003             :     {
    1004       13323 :         q_direction->cfg.nblocks = 1;
    1005       13323 :         move16();
    1006       13323 :         num_blocks = 1;
    1007       13323 :         move16();
    1008             :     }
    1009             :     ELSE
    1010             :     {
    1011      144177 :         q_direction->cfg.nblocks = MAX_PARAM_SPATIAL_SUBFRAMES;
    1012      144177 :         move16();
    1013      144177 :         num_blocks = MAX_PARAM_SPATIAL_SUBFRAMES;
    1014      144177 :         move16();
    1015             :     }
    1016             : 
    1017             :     /* Need to initialize renormalization_factors, direction_m and diffuseness_m */
    1018      157500 :     set_zero_fx( hDirAC->diffuseness_m_fx, hDirAC->hConfig->nbands );
    1019      157500 :     set16_zero_fx( hDirAC->diffuseness_m_exp, hDirAC->hConfig->nbands );
    1020      157500 :     set_zero_fx( renormalization_factor_diff_fx, hDirAC->hConfig->nbands );
    1021      157500 :     set16_zero_fx( renormalization_factor_diff_exp, hDirAC->hConfig->nbands );
    1022     2677500 :     FOR( i = 0; i < CLDFB_NO_COL_MAX; i++ )
    1023             :     {
    1024     2520000 :         set16_zero_fx( reference_power_exp[i], DIRAC_NO_FB_BANDS_MAX );
    1025             :     }
    1026             : 
    1027      157500 :     set_zero_fx( azi_secs_fx, 2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS );
    1028      157500 :     set_zero_fx( ele_secs_fx, 2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS );
    1029      157500 :     set_zero_fx( diff_secs_fx, 2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS );
    1030      157500 :     set16_zero_fx( diff_secs_exp, 2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS );
    1031      157500 :     set_zero_fx( ene_secs_fx, 2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS );
    1032      157500 :     set16_zero_fx( ene_secs_exp, 2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS );
    1033             : 
    1034             :     /* Copy current frame to memory for delay compensation */
    1035      868700 :     FOR( i = 0; i < nchan_fb_in; i++ )
    1036             :     {
    1037      711200 :         pcm_in_fx[i] = data_f_fx[i];
    1038      711200 :         p_Cldfb_RealBuffer_fx[i] = &Cldfb_RealBuffer_fx[i][0];
    1039      711200 :         p_Cldfb_ImagBuffer_fx[i] = &Cldfb_ImagBuffer_fx[i][0];
    1040             :     }
    1041             : 
    1042             :     /* do processing over all CLDFB time slots */
    1043      747531 :     FOR( block_m_idx = 0; block_m_idx < num_blocks; block_m_idx++ )
    1044             :     {
    1045      590031 :         mrange[0] = hDirAC->block_grouping[block_m_idx]; /* Q0 */
    1046      590031 :         move16();
    1047      590031 :         mrange[1] = hDirAC->block_grouping[block_m_idx + 1];
    1048      590031 :         move16();
    1049             : 
    1050      590031 :         set32_fx( renormalization_factor_fx, EPSILON_FX, hDirAC->hConfig->nbands );
    1051      590031 :         set16_fx( renormalization_factor_exp, 0, hDirAC->hConfig->nbands );
    1052      590031 :         set_zero_fx( hDirAC->direction_vector_m_fx[0][block_m_idx], hDirAC->hConfig->nbands );
    1053      590031 :         set_zero_fx( hDirAC->direction_vector_m_fx[1][block_m_idx], hDirAC->hConfig->nbands );
    1054      590031 :         set_zero_fx( hDirAC->direction_vector_m_fx[2][block_m_idx], hDirAC->hConfig->nbands );
    1055             :         Word64 dir[DIRAC_NUM_DIMS][DIRAC_MAX_NBANDS];
    1056      590031 :         Word16 direction_vector_exp_tmp[DIRAC_NUM_DIMS][MAX_PARAM_SPATIAL_SUBFRAMES][DIRAC_MAX_NBANDS] = { 0 };
    1057     2360124 :         FOR( i = 0; i < DIRAC_NUM_DIMS; i++ )
    1058             :         {
    1059     1770093 :             set64_fx( dir[i], 0, DIRAC_MAX_NBANDS );
    1060     8850465 :             FOR( j = 0; j < MAX_PARAM_SPATIAL_SUBFRAMES; j++ )
    1061             :             {
    1062     7080372 :                 set16_fx( direction_vector_exp_tmp[i][j], 0, DIRAC_MAX_NBANDS );
    1063             :             }
    1064             :         }
    1065      590031 :         hDirAC->direction_vector_m_exp[block_m_idx] = 0;
    1066      590031 :         move16();
    1067             : 
    1068     1220031 :         FOR( ts = mrange[0]; ts < mrange[1]; ts++ )
    1069             :         {
    1070      630000 :             IF( hDirAC->hFbMixer )
    1071             :             {
    1072           0 :                 FOR( i = 0; i < nchan_fb_in; i++ )
    1073             :                 {
    1074           0 :                     scale_sig32( pcm_in_fx[i], input_frame, shift );                                                                      // Q:Qx+shift
    1075           0 :                     scale_sig32( hDirAC->hFbMixer->ppFilterbank_prior_input_fx[i], hDirAC->hFbMixer->fb_cfg->prior_input_length, shift ); // Q:Qx+shift
    1076             :                 }
    1077           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 );
    1078           0 :                 FOR( i = 0; i < nchan_fb_in; i++ )
    1079             :                 {
    1080           0 :                     scale_sig32( pcm_in_fx[i], input_frame, negate( shift ) );                                                                      // Q:Qx
    1081           0 :                     scale_sig32( hDirAC->hFbMixer->ppFilterbank_prior_input_fx[i], hDirAC->hFbMixer->fb_cfg->prior_input_length, negate( shift ) ); // Q:Qx
    1082             :                 }
    1083           0 :                 ivas_fb_mixer_update_prior_input_fx( hDirAC->hFbMixer, pcm_in_fx, l_ts, hDirAC->hFbMixer->fb_cfg->num_in_chans );
    1084             : 
    1085           0 :                 FOR( i = 0; i < nchan_fb_in; i++ )
    1086             :                 {
    1087           0 :                     pcm_in_fx[i] += l_ts;
    1088             :                 }
    1089             :             }
    1090             :             ELSE
    1091             :             {
    1092     3474800 :                 FOR( i = 0; i < nchan_fb_in; i++ )
    1093             :                 {
    1094     2844800 :                     Copy32( &pp_fr_real_fx[i][ts * l_ts], Cldfb_RealBuffer_fx[i], l_ts ); /* pp_fr_q */
    1095     2844800 :                     Copy32( &pp_fr_imag_fx[i][ts * l_ts], Cldfb_ImagBuffer_fx[i], l_ts ); /* pp_fr_q */
    1096             :                 }
    1097      630000 :                 cldfb_q = pp_fr_q;
    1098      630000 :                 move16();
    1099             :             }
    1100             : 
    1101      630000 :             Word16 ref_power_w = 1;
    1102      630000 :             move16();
    1103      630000 :             if ( hodirac_flag )
    1104             :             {
    1105       64960 :                 ref_power_w = 0;
    1106       64960 :                 move16();
    1107             :             }
    1108             : 
    1109     1260000 :             computeReferencePower_enc_fx_dirac(
    1110      630000 :                 hDirAC->band_grouping,
    1111             :                 Cldfb_RealBuffer_fx,
    1112             :                 Cldfb_ImagBuffer_fx,
    1113      630000 :                 sub( 31, cldfb_q ),
    1114      630000 :                 reference_power_fx[ts],
    1115      630000 :                 reference_power_exp[ts],
    1116      630000 :                 hDirAC->hConfig->enc_param_start_band,
    1117             :                 num_freq_bands,
    1118             :                 ivas_format,
    1119             :                 ref_power_w,
    1120             :                 FOA_CHANNELS,
    1121             :                 mono_frame_count,
    1122             :                 dirac_mono_flag );
    1123             : 
    1124      630000 :             computeIntensityVector_enc_fx(
    1125             :                 hDirAC,
    1126             :                 Cldfb_RealBuffer_fx,
    1127             :                 Cldfb_ImagBuffer_fx,
    1128      630000 :                 hDirAC->hConfig->enc_param_start_band,
    1129             :                 num_freq_bands,
    1130             :                 intensity_real_fx,
    1131             :                 cldfb_q,
    1132             :                 intensity_real_q );
    1133      630000 :             IF( !hodirac_flag )
    1134             :             {
    1135      565040 :                 computeDirectionVectors_fixed(
    1136             :                     intensity_real_fx[0],
    1137             :                     intensity_real_fx[1],
    1138             :                     intensity_real_fx[2],
    1139      565040 :                     hDirAC->hConfig->enc_param_start_band,
    1140             :                     num_freq_bands,
    1141             :                     direction_vector_fx[0],
    1142             :                     direction_vector_fx[1],
    1143             :                     direction_vector_fx[2],
    1144             :                     31, intensity_real_q );
    1145             : 
    1146      565040 :                 direction_vector_q = Q30;
    1147      565040 :                 move16();
    1148             :             }
    1149             : 
    1150             :             /* fill buffers of length "averaging_length" time slots for intensity and energy */
    1151      630000 :             hDirAC->index_buffer_intensity = add( ( hDirAC->index_buffer_intensity % hDirAC->no_col_avg_diff ), 1 ); /* averaging_length = 32  Q0*/
    1152      630000 :             move16();
    1153      630000 :             index = hDirAC->index_buffer_intensity; /* Q0 */
    1154      630000 :             move16();
    1155     2520000 :             FOR( i = 0; i < DIRAC_NUM_DIMS; i++ )
    1156             :             {
    1157             :                 /* only real part needed */
    1158     1890000 :                 Copy32( intensity_real_fx[i], &( hDirAC->buffer_intensity_real_fx[i][index - 1][0] ), num_freq_bands ); /* intensity_real_q */
    1159     1890000 :                 Copy( intensity_real_q, &hDirAC->buffer_intensity_real_q[i][index - 1][0], num_freq_bands );
    1160             :             }
    1161      630000 :             Copy32( reference_power_fx[ts], &( hDirAC->buffer_energy_fx[( index - 1 ) * num_freq_bands] ), num_freq_bands ); /* exp(reference_power_exp) */
    1162     3051348 :             FOR( i = 0; i < num_freq_bands; i++ )
    1163             :             {
    1164     2421348 :                 hDirAC->buffer_energy_q[( index - 1 ) * num_freq_bands + i] = sub( 31, reference_power_exp[ts][i] );
    1165     2421348 :                 move16();
    1166             :             }
    1167             : 
    1168      630000 :             Word16 buffer_intensity_real_single_q, min_shift = MAX_16;
    1169      630000 :             move16();
    1170      630000 :             buffer_intensity_real_single_q = hDirAC->buffer_intensity_real_q[0][0][0];
    1171      630000 :             move16();
    1172     2520000 :             FOR( i = 0; i < DIRAC_NUM_DIMS; i++ )
    1173             :             {
    1174    17010000 :                 FOR( j = 0; j < hDirAC->no_col_avg_diff; j++ )
    1175             :                 {
    1176    15120000 :                     min_shift = s_min( min_shift, L_norm_arr( hDirAC->buffer_intensity_real_fx[i][j], num_freq_bands ) );
    1177    73232352 :                     FOR( k = 0; k < num_freq_bands; k++ )
    1178             :                     {
    1179    58112352 :                         buffer_intensity_real_single_q = s_min( buffer_intensity_real_single_q, hDirAC->buffer_intensity_real_q[i][j][k] );
    1180             :                     }
    1181             :                 }
    1182             :             }
    1183      630000 :             buffer_intensity_real_single_q = add( buffer_intensity_real_single_q, min_shift );
    1184     2520000 :             FOR( i = 0; i < DIRAC_NUM_DIMS; i++ )
    1185             :             {
    1186    17010000 :                 FOR( j = 0; j < hDirAC->no_col_avg_diff; j++ )
    1187             :                 {
    1188    73232352 :                     FOR( k = 0; k < num_freq_bands; k++ )
    1189             :                     {
    1190    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 */
    1191    58112352 :                         move32();
    1192    58112352 :                         hDirAC->buffer_intensity_real_q[i][j][k] = buffer_intensity_real_single_q;
    1193    58112352 :                         move16();
    1194             :                     }
    1195             :                 }
    1196             :             }
    1197             : 
    1198             :             Word16 buffer_energy_single_q;
    1199             :             Word16 buffer_len;
    1200      630000 :             buffer_len = imult1616( DIRAC_MAX_NBANDS, hDirAC->no_col_avg_diff );
    1201      630000 :             minimum_fx( hDirAC->buffer_energy_q, buffer_len, &buffer_energy_single_q );
    1202    61110000 :             FOR( i = 0; i < buffer_len; i++ )
    1203             :             {
    1204    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 */
    1205    60480000 :                 move32();
    1206    60480000 :                 hDirAC->buffer_energy_q[i] = buffer_energy_single_q;
    1207    60480000 :                 move16();
    1208             :             }
    1209             : 
    1210             :             Word16 tmp_q_buffer_for_intensity[DIRAC_MAX_NBANDS * DIRAC_NO_COL_AVG_DIFF];
    1211             :             Word16 tmp_q_buffer_for_energy[DIRAC_MAX_NBANDS * DIRAC_NO_COL_AVG_DIFF];
    1212             : 
    1213      630000 :             set16_fx( tmp_q_buffer_for_intensity, buffer_intensity_real_single_q, buffer_len );
    1214      630000 :             set16_fx( tmp_q_buffer_for_energy, buffer_energy_single_q, buffer_len );
    1215             : 
    1216      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 );
    1217             : 
    1218      630000 :             IF( hodirac_flag )
    1219             :             {
    1220       64960 :                 calculate_hodirac_sector_parameters_fx(
    1221             :                     hDirAC,
    1222             :                     Cldfb_RealBuffer_fx,
    1223             :                     Cldfb_ImagBuffer_fx,
    1224             :                     cldfb_q,
    1225             :                     HODIRAC_BETA,
    1226       64960 :                     hDirAC->band_grouping,
    1227       64960 :                     hDirAC->hConfig->nbands,
    1228       64960 :                     hDirAC->hConfig->enc_param_start_band,
    1229             :                     azi_secs_fx,
    1230             :                     ele_secs_fx,
    1231             :                     diff_secs_fx,
    1232             :                     diff_secs_exp,
    1233             :                     ene_secs_fx,
    1234             :                     ene_secs_exp );
    1235             :             }
    1236             : 
    1237      630000 :             IF( hodirac_flag )
    1238             :             {
    1239      844480 :                 FOR( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
    1240             :                 {
    1241      779520 :                     Word32 tmp_diff = L_shr( diffuseness_vector_fx[band_m_idx], sub( diffuseness_vector_q, 30 ) ); // diffueseness_vector_q -> Q30
    1242      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],
    1243      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) */
    1244      779520 :                     move32();
    1245      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) */
    1246      779520 :                     move32();
    1247             :                 }
    1248             :             }
    1249             :             ELSE
    1250             :             {
    1251     2206868 :                 FOR( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
    1252             :                 {
    1253     1641828 :                     Word32 tmp_diff = L_shr( diffuseness_vector_fx[band_m_idx], sub( diffuseness_vector_q, 30 ) ); // diffueseness_vector_q -> Q30
    1254     1641828 :                     norm_tmp_exp = add( reference_power_exp[ts][band_m_idx], 1 );
    1255     1641828 :                     norm_tmp_fx = Mpy_32_32( reference_power_fx[ts][band_m_idx], L_sub( ONE_IN_Q30, tmp_diff ) );
    1256             : 
    1257     1641828 :                     dir[0][band_m_idx] = W_mult_32_32( norm_tmp_fx, direction_vector_fx[0][band_m_idx] );
    1258     1641828 :                     move64();
    1259     1641828 :                     dir[1][band_m_idx] = W_mult_32_32( norm_tmp_fx, direction_vector_fx[1][band_m_idx] );
    1260     1641828 :                     move64();
    1261     1641828 :                     dir[2][band_m_idx] = W_mult_32_32( norm_tmp_fx, direction_vector_fx[2][band_m_idx] );
    1262     1641828 :                     move64();
    1263     1641828 :                     Word16 norm = 62;
    1264     1641828 :                     move16();
    1265     1641828 :                     IF( dir[0][band_m_idx] != 0 )
    1266             :                     {
    1267     1589892 :                         norm = W_norm( dir[0][band_m_idx] );
    1268             :                     }
    1269     1641828 :                     IF( dir[1][band_m_idx] != 0 )
    1270             :                     {
    1271     1588412 :                         norm = s_min( norm, W_norm( dir[1][band_m_idx] ) );
    1272             :                     }
    1273     1641828 :                     IF( dir[2][band_m_idx] != 0 )
    1274             :                     {
    1275     1486487 :                         norm = s_min( norm, W_norm( dir[2][band_m_idx] ) );
    1276             :                     }
    1277     1641828 :                     Word32 tmp = W_extract_h( W_shl( dir[0][band_m_idx], norm ) );
    1278     1641828 :                     Word16 tmp_e = sub( Q31, add( sub( direction_vector_q, norm_tmp_exp ), sub( norm, 0 ) ) );
    1279     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] );
    1280     1641828 :                     move32();
    1281     1641828 :                     tmp = W_extract_h( W_shl( dir[1][band_m_idx], norm ) );
    1282     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] );
    1283     1641828 :                     move32();
    1284     1641828 :                     tmp = W_extract_h( W_shl( dir[2][band_m_idx], norm ) );
    1285     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] );
    1286     1641828 :                     move32();
    1287     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) */
    1288     1641828 :                     move32();
    1289     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],
    1290     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) */
    1291     1641828 :                     move32();
    1292     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],
    1293     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) */
    1294     1641828 :                     move32();
    1295             :                 }
    1296             :             }
    1297             :         }
    1298             : 
    1299      590031 :         IF( !hodirac_flag )
    1300             :         {
    1301     2086961 :             FOR( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
    1302             :             {
    1303     1561890 :                 renormalization_factor_fx[band_m_idx] = EPSILON_FX;
    1304     1561890 :                 move32();
    1305     1561890 :                 renormalization_factor_exp[band_m_idx] = 0;
    1306     1561890 :                 move16();
    1307     1561890 :                 Word16 tmp_e = shl( direction_vector_exp_tmp[0][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[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 */
    1309     1561890 :                 move32();
    1310     1561890 :                 tmp_e = shl( direction_vector_exp_tmp[1][block_m_idx][band_m_idx], 1 );
    1311     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 */
    1312     1561890 :                 move32();
    1313     1561890 :                 tmp_e = shl( direction_vector_exp_tmp[2][block_m_idx][band_m_idx], 1 );
    1314     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 */
    1315     1561890 :                 move32();
    1316     1561890 :                 IF( renormalization_factor_fx[band_m_idx] == 0 )
    1317             :                 {
    1318       50016 :                     renormalization_factor_fx[band_m_idx] = EPSILON_FX;
    1319       50016 :                     move32();
    1320       50016 :                     renormalization_factor_exp[band_m_idx] = 0;
    1321       50016 :                     move16();
    1322             :                 }
    1323     1561890 :                 renormalization_factor_fx[band_m_idx] = Sqrt32( renormalization_factor_fx[band_m_idx], &renormalization_factor_exp[band_m_idx] );
    1324     1561890 :                 move32();
    1325             : 
    1326     1561890 :                 IF( GT_32( renormalization_factor_fx[band_m_idx], EPSILON_FX ) )
    1327             :                 {
    1328             :                     Word32 tmp32;
    1329             : 
    1330     1561890 :                     tmp32 = hDirAC->direction_vector_m_fx[0][block_m_idx][band_m_idx];
    1331     1561890 :                     move32();
    1332     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)) */
    1333     1561890 :                     tmp_e = add( tmp_e, sub( direction_vector_exp_tmp[0][block_m_idx][band_m_idx], renormalization_factor_exp[band_m_idx] ) );
    1334     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
    1335     1561890 :                     move32();
    1336             : 
    1337     1561890 :                     tmp32 = hDirAC->direction_vector_m_fx[1][block_m_idx][band_m_idx]; // Q30
    1338     1561890 :                     move32();
    1339     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)) */
    1340     1561890 :                     tmp_e = add( tmp_e, sub( direction_vector_exp_tmp[1][block_m_idx][band_m_idx], renormalization_factor_exp[band_m_idx] ) );
    1341     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
    1342     1561890 :                     move32();
    1343             : 
    1344     1561890 :                     tmp32 = hDirAC->direction_vector_m_fx[2][block_m_idx][band_m_idx]; // Q30
    1345     1561890 :                     move32();
    1346     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)) */
    1347     1561890 :                     tmp_e = add( tmp_e, sub( direction_vector_exp_tmp[2][block_m_idx][band_m_idx], renormalization_factor_exp[band_m_idx] ) );
    1348     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
    1349     1561890 :                     move32();
    1350             :                 }
    1351             :                 ELSE
    1352             :                 {
    1353           0 :                     hDirAC->direction_vector_m_fx[0][block_m_idx][band_m_idx] = ONE_IN_Q30;
    1354           0 :                     move32();
    1355           0 :                     hDirAC->direction_vector_m_fx[1][block_m_idx][band_m_idx] = 0;
    1356           0 :                     move32();
    1357           0 :                     hDirAC->direction_vector_m_fx[2][block_m_idx][band_m_idx] = 0;
    1358           0 :                     move32();
    1359             :                 }
    1360             : 
    1361             :                 /* save the elevation and azimuth values to be used later by the ivas_dirac_QuantizeParameters function */
    1362     6247560 :                 FOR( d = 0; d < DIRAC_NUM_DIMS; d++ )
    1363             :                 {
    1364     4685670 :                     dir_v_fx[d] = hDirAC->direction_vector_m_fx[d][block_m_idx][band_m_idx]; // Q30
    1365     4685670 :                     move32();
    1366             :                 }
    1367             : 
    1368     1561890 :                 Scale_sig32( dir_v_fx, 3, -1 );
    1369     1561890 :                 ivas_qmetadata_direction_vector_to_azimuth_elevation_fx(
    1370             :                     dir_v_fx,
    1371             :                     Q29,
    1372     1561890 :                     &q_direction->band_data[band_m_idx].azimuth_fx[block_m_idx],
    1373     1561890 :                     &q_direction->band_data[band_m_idx].elevation_fx[block_m_idx] );
    1374             :             }
    1375      525071 :             hDirAC->direction_vector_m_exp[block_m_idx] = 1;
    1376      525071 :             move16();
    1377             :         }
    1378             : 
    1379             :         /* Sectors */
    1380      590031 :         IF( hodirac_flag )
    1381             :         {
    1382             :             Word32 L_tmp, L_tmp1;
    1383             :             Word16 e_tmp, e_tmp1, exp_diff;
    1384      844480 :             FOR( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
    1385             :             {
    1386      779520 :                 q_direction->band_data[band_m_idx].azimuth_fx[block_m_idx] = L_shr( azi_secs_fx[band_m_idx], 1 ); // Q23 -> Q22
    1387      779520 :                 move32();
    1388      779520 :                 q_direction->band_data[band_m_idx].elevation_fx[block_m_idx] = L_shr( ele_secs_fx[band_m_idx], 1 ); // Q23 -> Q22
    1389      779520 :                 move32();
    1390             : 
    1391      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
    1392      779520 :                 move32();
    1393      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
    1394      779520 :                 move32();
    1395             : 
    1396      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) */
    1397      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) */
    1398      779520 :                 L_tmp1 = BASOP_Util_Add_Mant32Exp( L_tmp, e_tmp, L_tmp1, e_tmp1, &e_tmp1 );                                                                                      /* exp(e_tmp1) */
    1399      779520 :                 IF( L_tmp1 == 0 )
    1400             :                 {
    1401       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
    1402       68960 :                     move32();
    1403             :                 }
    1404             :                 ELSE
    1405             :                 {
    1406      710560 :                     L_tmp = L_deposit_h( BASOP_Util_Divide3232_Scale( L_tmp, L_add( L_tmp1, EPSILON_FX ), &exp_diff ) ); /* exp(exp_diff) */
    1407      710560 :                     exp_diff = add( exp_diff, sub( e_tmp, e_tmp1 ) );
    1408      710560 :                     q_direction[1].band_data[band_m_idx].energy_ratio_fx[block_m_idx] = L_shl( L_tmp, sub( exp_diff, Q1 ) ); // Q30
    1409      710560 :                     move32();
    1410             :                 }
    1411             :             }
    1412             :         }
    1413             :     }
    1414             : 
    1415             :     /* Diffuseness */
    1416      762837 :     FOR( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
    1417             :     {
    1418      605337 :         IF( GT_32( renormalization_factor_diff_fx[band_m_idx], EPSILON_FX ) )
    1419             :         {
    1420             :             Word16 tmp_e;
    1421      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) */
    1422      576968 :             move32();
    1423      576968 :             tmp_e = add( tmp_e, sub( hDirAC->diffuseness_m_exp[band_m_idx], renormalization_factor_diff_exp[band_m_idx] ) );
    1424      576968 :             hDirAC->diffuseness_m_fx[band_m_idx] = L_shr( hDirAC->diffuseness_m_fx[band_m_idx], sub( 1, tmp_e ) ); // to Q30
    1425      576968 :             move32();
    1426             :         }
    1427             :         ELSE
    1428             :         {
    1429       28369 :             hDirAC->diffuseness_m_fx[band_m_idx] = 0;
    1430       28369 :             move32();
    1431             :         }
    1432      605337 :         hDirAC->diffuseness_m_exp[band_m_idx] = 1;
    1433      605337 :         move16();
    1434             : 
    1435             :         /* set coherence to zero and convert diffuseness to energy ratio*/
    1436             : 
    1437      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 );
    1438             : 
    1439     2946747 :         FOR( block_m_idx = 0; block_m_idx < q_direction->cfg.nblocks; block_m_idx++ )
    1440             :         {
    1441     2341410 :             IF( q_direction->coherence_band_data != NULL )
    1442             :             {
    1443           0 :                 q_direction->coherence_band_data[band_m_idx].spread_coherence[block_m_idx] = 0;
    1444           0 :                 move16();
    1445             :             }
    1446             :         }
    1447             :     }
    1448             : 
    1449      157500 :     pop_wmops();
    1450      157500 :     return;
    1451             : }
    1452             : 
    1453             : 
    1454             : /*-------------------------------------------------------------------------
    1455             :  * computeIntensityVector_enc()
    1456             :  *
    1457             :  *
    1458             :  *------------------------------------------------------------------------*/
    1459      630000 : static void computeIntensityVector_enc_fx(
    1460             :     const DIRAC_ENC_HANDLE hDirAC,
    1461             :     Word32 Cldfb_RealBuffer[DIRAC_MAX_ANA_CHANS][DIRAC_NO_FB_BANDS_MAX], /* q_cldfb */
    1462             :     Word32 Cldfb_ImagBuffer[DIRAC_MAX_ANA_CHANS][DIRAC_NO_FB_BANDS_MAX], /* q_cldfb */
    1463             :     const Word16 enc_param_start_band,                                   /* i  : first band to process  Q0*/
    1464             :     const Word16 num_frequency_bands,                                    /* Q0 */
    1465             :     Word32 intensity_real[DIRAC_NUM_DIMS][DIRAC_MAX_NBANDS]              /* q_intensity_real */
    1466             :     ,
    1467             :     Word16 q_cldfb,
    1468             :     Word16 q_intensity_real[DIRAC_MAX_NBANDS] )
    1469             : {
    1470             :     /* Reminder
    1471             :      * X = a + ib; Y = c + id
    1472             :      * X*Y = ac - bd + i(ad +bc)
    1473             :      */
    1474             :     Word16 i, j;
    1475             :     Word32 real, img;
    1476             :     Word16 brange[2];
    1477      630000 :     Word16 shift_value = sub( shl( q_cldfb, 1 ), 31 );
    1478     3051348 :     FOR( i = 0; i < num_frequency_bands; i++ )
    1479             :     {
    1480     2421348 :         brange[0] = hDirAC->band_grouping[i + enc_param_start_band]; /* Q0 */
    1481     2421348 :         move16();
    1482     2421348 :         brange[1] = hDirAC->band_grouping[i + enc_param_start_band + 1]; /* Q0 */
    1483     2421348 :         move16();
    1484     2421348 :         Word16 num_bins = sub( brange[1], brange[0] );
    1485     2421348 :         Word16 gb = find_guarded_bits_fx( num_bins );
    1486             :         Word16 norm;
    1487     2421348 :         intensity_real[0][i] = 0;
    1488     2421348 :         move32();
    1489     2421348 :         intensity_real[1][i] = 0;
    1490     2421348 :         move32();
    1491     2421348 :         intensity_real[2][i] = 0;
    1492     2421348 :         move32();
    1493             : 
    1494     2421348 :         Word64 tmp_1 = 0, tmp_2 = 0, tmp_3 = 0;
    1495     2421348 :         move64();
    1496     2421348 :         move64();
    1497     2421348 :         move64();
    1498             : 
    1499    87737228 :         FOR( j = brange[0]; j < brange[1]; j++ )
    1500             :         {
    1501    85315880 :             real = Cldfb_RealBuffer[0][j];
    1502    85315880 :             move32();
    1503    85315880 :             img = Cldfb_ImagBuffer[0][j];
    1504    85315880 :             move32();
    1505             :             Word64 t1, t2, t3;
    1506    85315880 :             t1 = W_mac_32_32( W_mult_32_32( Cldfb_RealBuffer[3][j], real ), Cldfb_ImagBuffer[3][j], img ); /* 2 * q_cldfb + 1 */
    1507    85315880 :             t2 = W_mac_32_32( W_mult_32_32( Cldfb_RealBuffer[1][j], real ), Cldfb_ImagBuffer[1][j], img ); /* 2 * q_cldfb + 1 */
    1508    85315880 :             t3 = W_mac_32_32( W_mult_32_32( Cldfb_RealBuffer[2][j], real ), Cldfb_ImagBuffer[2][j], img ); /* 2 * q_cldfb + 1 */
    1509    85315880 :             t1 = W_shr( t1, gb );
    1510    85315880 :             t2 = W_shr( t2, gb );
    1511    85315880 :             t3 = W_shr( t3, gb );
    1512             :             /* Intensity is XYZ order, audio is WYZX order. */
    1513    85315880 :             tmp_1 = W_add( tmp_1, t1 ); /* 2 * q_cldfb + 1 */
    1514    85315880 :             tmp_2 = W_add( tmp_2, t2 ); /* 2 * q_cldfb + 1 */
    1515    85315880 :             tmp_3 = W_add( tmp_3, t3 ); /* 2 * q_cldfb + 1 */
    1516             :         }
    1517     2421348 :         norm = 63;
    1518     2421348 :         move16();
    1519     2421348 :         IF( tmp_1 != 0 )
    1520             :         {
    1521     2299006 :             norm = s_min( norm, W_norm( tmp_1 ) );
    1522             :         }
    1523     2421348 :         IF( tmp_2 != 0 )
    1524             :         {
    1525     2299029 :             norm = s_min( norm, W_norm( tmp_2 ) );
    1526             :         }
    1527     2421348 :         IF( tmp_3 != 0 )
    1528             :         {
    1529     2101621 :             norm = s_min( norm, W_norm( tmp_3 ) );
    1530             :         }
    1531     2421348 :         intensity_real[0][i] = W_extract_h( W_shl( tmp_1, norm ) ); // shift_value - (gb - norm)
    1532     2421348 :         move32();
    1533     2421348 :         intensity_real[1][i] = W_extract_h( W_shl( tmp_2, norm ) ); // shift_value - (gb - norm)
    1534     2421348 :         move32();
    1535     2421348 :         intensity_real[2][i] = W_extract_h( W_shl( tmp_3, norm ) ); // shift_value - (gb - norm)
    1536     2421348 :         q_intensity_real[i] = sub( shift_value, sub( gb, norm ) );
    1537     2421348 :         move16();
    1538             :     }
    1539             : 
    1540      630000 :     return;
    1541             : }
    1542             : 
    1543             : /*-------------------------------------------------------------------------
    1544             :  * computeDiffuseness_mdft()
    1545             :  *
    1546             :  *
    1547             :  *------------------------------------------------------------------------*/
    1548      675880 : void computeDiffuseness_mdft_fx(
    1549             :     Word32 **buffer_intensity[DIRAC_NUM_DIMS], /* q_factor_intensity */
    1550             :     const Word32 *buffer_energy,               /* q_factor_energy */
    1551             :     const Word16 num_freq_bands,               /* Q0 */
    1552             :     const UWord16 no_col_avg_diff,             /* Q0 */
    1553             :     Word32 *diffuseness,                       /* q_diffuseness */
    1554             :     Word16 *q_factor_intensity,
    1555             :     Word16 *q_factor_energy,
    1556             :     Word16 *q_diffuseness /*Ouput Q*/
    1557             : )
    1558             : {
    1559             :     Word32 intensity_slow[DIRAC_NUM_DIMS * MASA_FREQUENCY_BANDS];
    1560             :     Word16 intensity_slow_e[DIRAC_NUM_DIMS * MASA_FREQUENCY_BANDS];
    1561             :     Word32 intensity_slow_abs[MASA_FREQUENCY_BANDS];
    1562             :     Word16 intensity_slow_abs_e[MASA_FREQUENCY_BANDS];
    1563             :     Word32 energy_slow[MASA_FREQUENCY_BANDS];
    1564             :     Word16 energy_slow_e[MASA_FREQUENCY_BANDS];
    1565             :     Word16 i, j, k;
    1566             :     Word32 tmp;
    1567             :     Word32 *p_tmp;
    1568             :     const Word32 *p_tmp_c;
    1569             :     Word16 *p_tmp_e, tmp_q;
    1570             : 
    1571             :     /* Compute Intensity slow and energy slow buffer_intensity and buffer_energy */
    1572             : 
    1573      675880 :     set_zero_fx( intensity_slow, i_mult( DIRAC_NUM_DIMS, MASA_FREQUENCY_BANDS ) );
    1574      675880 :     set16_fx( intensity_slow_e, 0, i_mult( DIRAC_NUM_DIMS, MASA_FREQUENCY_BANDS ) );
    1575      675880 :     set_zero_fx( intensity_slow_abs, MASA_FREQUENCY_BANDS );
    1576      675880 :     set16_fx( intensity_slow_abs_e, 0, no_col_avg_diff );
    1577      675880 :     set_zero_fx( energy_slow, MASA_FREQUENCY_BANDS );
    1578      675880 :     set16_fx( energy_slow_e, 0, no_col_avg_diff );
    1579             : 
    1580     6082920 :     FOR( i = 0; i < no_col_avg_diff; ++i )
    1581             :     {
    1582             :         /* Energy slow */
    1583     5407040 :         p_tmp_c = buffer_energy + i_mult( i, num_freq_bands );
    1584     5407040 :         tmp_q = q_factor_energy[i];
    1585     5407040 :         move16();
    1586             : 
    1587    26613024 :         FOR( k = 0; k < num_freq_bands; k++ )
    1588             :         {
    1589    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)*/
    1590    21205984 :             move32();
    1591             :         }
    1592             : 
    1593             :         /* Intensity slow */
    1594    21628160 :         FOR( j = 0; j < DIRAC_NUM_DIMS; ++j )
    1595             :         {
    1596    16221120 :             p_tmp = buffer_intensity[j][i];
    1597    79839072 :             FOR( k = 0; k < num_freq_bands; k++ )
    1598             :             {
    1599    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) */
    1600    63617952 :                 move32();
    1601             :             }
    1602             :         }
    1603             :     }
    1604             : 
    1605             :     /* intensity_slow.^2 + intensity_slow_abs*/
    1606     2703520 :     FOR( j = 0; j < DIRAC_NUM_DIMS; ++j )
    1607             :     {
    1608     2027640 :         p_tmp = intensity_slow + i_mult( j, num_freq_bands );
    1609     2027640 :         p_tmp_e = intensity_slow_e + i_mult( j, num_freq_bands );
    1610             : 
    1611     9979884 :         FOR( k = 0; k < num_freq_bands; k++ )
    1612             :         {
    1613     7952244 :             p_tmp[k] = Mpy_32_32( p_tmp[k], p_tmp[k] ); /* 2 * (31-p_tmp_e) - 31 */
    1614     7952244 :             move32();
    1615     7952244 :             tmp_q = sub( shl( sub( 31, p_tmp_e[k] ), 1 ), 31 );
    1616     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) */
    1617     7952244 :             move32();
    1618             :         }
    1619             :     }
    1620             : 
    1621             :     /* Compute Diffuseness */
    1622      675880 :     p_tmp = intensity_slow_abs;
    1623      675880 :     tmp_q = 0;
    1624      675880 :     move16();
    1625     3326628 :     FOR( i = 0; i < num_freq_bands; ++i )
    1626             :     {
    1627     2650748 :         tmp = Sqrt32( p_tmp[i], &intensity_slow_abs_e[i] );
    1628             : 
    1629     2650748 :         tmp = L_deposit_h( BASOP_Util_Divide3232_Scale( tmp, L_add( energy_slow[i], EPSILLON_FX ), &tmp_q ) ); /* exp(tmp_q) */
    1630     2650748 :         tmp_q = add( tmp_q, sub( intensity_slow_abs_e[i], energy_slow_e[i] ) );
    1631     2650748 :         tmp = L_shl_sat( tmp, tmp_q );
    1632     2650748 :         tmp = L_sub( ONE_IN_Q31, tmp );
    1633     2650748 :         diffuseness[i] = tmp;
    1634     2650748 :         move32();
    1635             : 
    1636     2650748 :         IF( LT_32( tmp, ONE_IN_Q31 ) )
    1637             :         {
    1638     2530490 :             IF( tmp < 0 )
    1639             :             {
    1640           0 :                 diffuseness[i] = 0;
    1641           0 :                 move32();
    1642             :             }
    1643             :         }
    1644             :         ELSE
    1645             :         {
    1646      120258 :             diffuseness[i] = ONE_IN_Q31;
    1647      120258 :             move32();
    1648             :         }
    1649             :     }
    1650      675880 :     *q_diffuseness = Q31;
    1651      675880 :     move16();
    1652             : 
    1653      675880 :     return;
    1654             : }

Generated by: LCOV version 1.14