LCOV - code coverage report
Current view: top level - lib_rend - ivas_dirac_rend.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 911 998 91.3 %
Date: 2025-05-23 08:37:30 Functions: 23 23 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.h"
      39             : #include "ivas_prot.h"
      40             : #include "ivas_prot_rend.h"
      41             : #include "ivas_cnst.h"
      42             : #include "ivas_rom_rend.h"
      43             : #ifdef DEBUGGING
      44             : #include "debug.h"
      45             : #endif
      46             : #include "wmc_auto.h"
      47             : 
      48             : 
      49             : /*-------------------------------------------------------------------------
      50             :  * ivas_dirac_allocate_parameters()
      51             :  *
      52             :  * Allocate and initialize DirAC parameters
      53             :  *-------------------------------------------------------------------------*/
      54             : 
      55        4156 : ivas_error ivas_dirac_allocate_parameters(
      56             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom, /* i/o: common data for spatial parametric rendering */
      57             :     const int16_t params_flag                             /* i  : set of parameters flag    */
      58             : )
      59             : {
      60             :     int16_t i;
      61             : 
      62        4156 :     if ( params_flag == 1 )
      63             :     {
      64        2462 :         if ( ( hSpatParamRendCom->azimuth = (int16_t **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( int16_t * ) ) ) == NULL )
      65             :         {
      66           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
      67             :         }
      68             : 
      69        2462 :         if ( ( hSpatParamRendCom->elevation = (int16_t **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( int16_t * ) ) ) == NULL )
      70             :         {
      71           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
      72             :         }
      73             : 
      74        2462 :         if ( ( hSpatParamRendCom->diffuseness_vector = (float **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( float * ) ) ) == NULL )
      75             :         {
      76           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
      77             :         }
      78             : 
      79        2462 :         if ( ( hSpatParamRendCom->energy_ratio1 = (float **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( float * ) ) ) == NULL )
      80             :         {
      81           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
      82             :         }
      83             : 
      84        2462 :         if ( ( hSpatParamRendCom->spreadCoherence = (float **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( float * ) ) ) == NULL )
      85             :         {
      86           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
      87             :         }
      88             : 
      89        2462 :         if ( ( hSpatParamRendCom->surroundingCoherence = (float **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( float * ) ) ) == NULL )
      90             :         {
      91           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
      92             :         }
      93             : 
      94       14116 :         for ( i = 0; i < hSpatParamRendCom->dirac_md_buffer_length; i++ )
      95             :         {
      96       11654 :             if ( ( hSpatParamRendCom->azimuth[i] = (int16_t *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( int16_t ) ) ) == NULL )
      97             :             {
      98           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
      99             :             }
     100       11654 :             set_s( hSpatParamRendCom->azimuth[i], 0, hSpatParamRendCom->num_freq_bands );
     101             : 
     102       11654 :             if ( ( hSpatParamRendCom->elevation[i] = (int16_t *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( int16_t ) ) ) == NULL )
     103             :             {
     104           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     105             :             }
     106       11654 :             set_s( hSpatParamRendCom->elevation[i], 0, hSpatParamRendCom->num_freq_bands );
     107             : 
     108       11654 :             if ( ( hSpatParamRendCom->diffuseness_vector[i] = (float *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( float ) ) ) == NULL )
     109             :             {
     110           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     111             :             }
     112       11654 :             set_f( hSpatParamRendCom->diffuseness_vector[i], 1.0f, hSpatParamRendCom->num_freq_bands );
     113             : 
     114       11654 :             if ( ( hSpatParamRendCom->energy_ratio1[i] = (float *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( float ) ) ) == NULL )
     115             :             {
     116           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     117             :             }
     118       11654 :             set_f( hSpatParamRendCom->energy_ratio1[i], 0.0f, hSpatParamRendCom->num_freq_bands );
     119             : 
     120       11654 :             if ( ( hSpatParamRendCom->spreadCoherence[i] = (float *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( float ) ) ) == NULL )
     121             :             {
     122           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     123             :             }
     124       11654 :             set_f( hSpatParamRendCom->spreadCoherence[i], 0.0f, hSpatParamRendCom->num_freq_bands );
     125             : 
     126       11654 :             if ( ( hSpatParamRendCom->surroundingCoherence[i] = (float *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( float ) ) ) == NULL )
     127             :             {
     128           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     129             :             }
     130       11654 :             set_f( hSpatParamRendCom->surroundingCoherence[i], 0.0f, hSpatParamRendCom->num_freq_bands );
     131             :         }
     132             :     }
     133        1694 :     else if ( params_flag == 2 )
     134             :     {
     135        1694 :         if ( ( hSpatParamRendCom->azimuth2 = (int16_t **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( int16_t * ) ) ) == NULL )
     136             :         {
     137           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     138             :         }
     139             : 
     140        1694 :         if ( ( hSpatParamRendCom->elevation2 = (int16_t **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( int16_t * ) ) ) == NULL )
     141             :         {
     142           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     143             :         }
     144             : 
     145        1694 :         if ( ( hSpatParamRendCom->energy_ratio2 = (float **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( float * ) ) ) == NULL )
     146             :         {
     147           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     148             :         }
     149             : 
     150        1694 :         if ( ( hSpatParamRendCom->spreadCoherence2 = (float **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( float * ) ) ) == NULL )
     151             :         {
     152           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     153             :         }
     154             : 
     155       10180 :         for ( i = 0; i < hSpatParamRendCom->dirac_md_buffer_length; i++ )
     156             :         {
     157        8486 :             if ( ( hSpatParamRendCom->azimuth2[i] = (int16_t *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( int16_t ) ) ) == NULL )
     158             :             {
     159           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     160             :             }
     161        8486 :             set_s( hSpatParamRendCom->azimuth2[i], 0, hSpatParamRendCom->num_freq_bands );
     162             : 
     163        8486 :             if ( ( hSpatParamRendCom->elevation2[i] = (int16_t *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( int16_t ) ) ) == NULL )
     164             :             {
     165           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     166             :             }
     167        8486 :             set_s( hSpatParamRendCom->elevation2[i], 0, hSpatParamRendCom->num_freq_bands );
     168             : 
     169        8486 :             if ( ( hSpatParamRendCom->energy_ratio2[i] = (float *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( float ) ) ) == NULL )
     170             :             {
     171           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     172             :             }
     173        8486 :             set_f( hSpatParamRendCom->energy_ratio2[i], 0.0f, hSpatParamRendCom->num_freq_bands );
     174             : 
     175        8486 :             if ( ( hSpatParamRendCom->spreadCoherence2[i] = (float *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( float ) ) ) == NULL )
     176             :             {
     177           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     178             :             }
     179        8486 :             set_f( hSpatParamRendCom->spreadCoherence2[i], 0.0f, hSpatParamRendCom->num_freq_bands );
     180             :         }
     181             :     }
     182             : 
     183        4156 :     return IVAS_ERR_OK;
     184             : }
     185             : 
     186             : 
     187             : /*-------------------------------------------------------------------------
     188             :  * ivas_spat_hSpatParamRendCom_config()
     189             :  *
     190             :  *
     191             :  *-------------------------------------------------------------------------*/
     192             : 
     193       14204 : ivas_error ivas_spat_hSpatParamRendCom_config(
     194             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE *hSpatParamRendCom_out, /* i/o: IVAS decoder structure  */
     195             :     const DIRAC_CONFIG_FLAG flag_config_inp,                   /* i/ : Flag determining if we open or reconfigure the DirAC decoder */
     196             :     const int16_t dec_param_estim_flag,
     197             :     const IVAS_FORMAT ivas_format,
     198             :     const MC_MODE mc_mode,
     199             :     const int32_t output_Fs,
     200             :     const int16_t hodirac_flag,
     201             :     const int16_t masa_ext_rend_flag )
     202             : {
     203             :     ivas_error error;
     204             :     int16_t map_idx;
     205             :     DIRAC_CONFIG_FLAG flag_config;
     206             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
     207             : 
     208       14204 :     flag_config = ( flag_config_inp == DIRAC_RECONFIGURE_MODE ) ? DIRAC_RECONFIGURE : flag_config_inp;
     209       14204 :     error = IVAS_ERR_OK;
     210             : 
     211       14204 :     hSpatParamRendCom = NULL;
     212             : 
     213       14204 :     if ( flag_config == DIRAC_RECONFIGURE )
     214             :     {
     215       12489 :         hSpatParamRendCom = *hSpatParamRendCom_out;
     216             :     }
     217        1715 :     else if ( flag_config == DIRAC_OPEN )
     218             :     {
     219             :         /*-----------------------------------------------------------------*
     220             :          * prepare library opening
     221             :          *-----------------------------------------------------------------*/
     222             : 
     223        1715 :         if ( ( hSpatParamRendCom = (SPAT_PARAM_REND_COMMON_DATA_HANDLE) malloc( sizeof( SPAT_PARAM_REND_COMMON_DATA ) ) ) == NULL )
     224             :         {
     225           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC meta\n" ) );
     226             :         }
     227             : 
     228        1715 :         *hSpatParamRendCom_out = hSpatParamRendCom;
     229             :     }
     230             : 
     231       14204 :     if ( flag_config == DIRAC_OPEN )
     232             :     {
     233        1715 :         hSpatParamRendCom->slot_size = (int16_t) ( ( output_Fs / FRAMES_PER_SEC ) / CLDFB_NO_COL_MAX );
     234        1715 :         set_s( hSpatParamRendCom->subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
     235        1715 :         set_s( hSpatParamRendCom->subframe_nbslots, JBM_CLDFB_SLOTS_IN_SUBFRAME, DEFAULT_JBM_SUBFRAMES_5MS );
     236        1715 :         hSpatParamRendCom->nb_subframes = DEFAULT_JBM_SUBFRAMES_5MS;
     237        1715 :         hSpatParamRendCom->subframes_rendered = 0;
     238        1715 :         hSpatParamRendCom->slots_rendered = 0;
     239        1715 :         hSpatParamRendCom->num_slots = DEFAULT_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME;
     240        1715 :         hSpatParamRendCom->num_freq_bands = (int16_t) ( output_Fs * INV_CLDFB_BANDWIDTH + 0.5f );
     241        1715 :         hSpatParamRendCom->numSimultaneousDirections = 0;
     242        1715 :         hSpatParamRendCom->numParametricDirections = 0;
     243        1715 :         hSpatParamRendCom->numIsmDirections = 0;
     244             :     }
     245             : 
     246             :     /*-----------------------------------------------------------------*
     247             :      * set input parameters
     248             :      *-----------------------------------------------------------------*/
     249             : 
     250       14204 :     if ( ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT ) && flag_config == DIRAC_RECONFIGURE )
     251             :     {
     252        3621 :         if ( hodirac_flag && hSpatParamRendCom->azimuth2 == NULL )
     253             :         {
     254         303 :             if ( ( error = ivas_dirac_allocate_parameters( hSpatParamRendCom, 2 ) ) != IVAS_ERR_OK )
     255             :             {
     256           0 :                 return error;
     257             :             }
     258             :         }
     259        3318 :         else if ( !hodirac_flag && hSpatParamRendCom->azimuth2 != NULL )
     260             :         {
     261         276 :             ivas_dirac_deallocate_parameters( hSpatParamRendCom, 2 );
     262             :         }
     263             :     }
     264             : 
     265       14204 :     if ( flag_config == DIRAC_OPEN )
     266             :     {
     267        1715 :         hSpatParamRendCom->dirac_md_buffer_length = 0;
     268        1715 :         hSpatParamRendCom->dirac_bs_md_write_idx = 0;
     269        1715 :         hSpatParamRendCom->dirac_read_idx = 0;
     270        1715 :         if ( mc_mode == MC_MODE_MCMASA || masa_ext_rend_flag == 1 )
     271             :         {
     272         812 :             hSpatParamRendCom->dirac_md_buffer_length = MAX_PARAM_SPATIAL_SUBFRAMES;
     273             : 
     274         812 :             set_s( hSpatParamRendCom->render_to_md_map, 0, MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME );
     275        4060 :             for ( map_idx = 0; map_idx < DEFAULT_JBM_SUBFRAMES_5MS; map_idx++ )
     276             :             {
     277        3248 :                 hSpatParamRendCom->render_to_md_map[map_idx] = map_idx;
     278             :             }
     279             :         }
     280         903 :         else if ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT )
     281             :         {
     282         489 :             hSpatParamRendCom->dirac_md_buffer_length = MAX_PARAM_SPATIAL_SUBFRAMES + DELAY_MASA_PARAM_DEC_SFR;
     283         489 :             hSpatParamRendCom->dirac_bs_md_write_idx = DELAY_MASA_PARAM_DEC_SFR;
     284             : 
     285         489 :             set_s( hSpatParamRendCom->render_to_md_map, 0, MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME );
     286        2445 :             for ( map_idx = 0; map_idx < DEFAULT_JBM_SUBFRAMES_5MS; map_idx++ )
     287             :             {
     288        1956 :                 hSpatParamRendCom->render_to_md_map[map_idx] = map_idx;
     289             :             }
     290             :         }
     291             :         else
     292             :         {
     293             :             int16_t num_slots_in_subfr;
     294         414 :             num_slots_in_subfr = dec_param_estim_flag ? CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES : 1;
     295         414 :             hSpatParamRendCom->dirac_md_buffer_length = ( MAX_PARAM_SPATIAL_SUBFRAMES + DELAY_DIRAC_PARAM_DEC_SFR );
     296         414 :             hSpatParamRendCom->dirac_bs_md_write_idx = DELAY_DIRAC_PARAM_DEC_SFR;
     297         414 :             hSpatParamRendCom->dirac_read_idx = 0;
     298             : 
     299         414 :             set_s( hSpatParamRendCom->render_to_md_map, 0, MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME );
     300        4518 :             for ( map_idx = 0; map_idx < DEFAULT_JBM_SUBFRAMES_5MS * num_slots_in_subfr; map_idx++ )
     301             :             {
     302        4104 :                 hSpatParamRendCom->render_to_md_map[map_idx] = hSpatParamRendCom->dirac_read_idx + map_idx / num_slots_in_subfr;
     303             :             }
     304             :         }
     305             : 
     306        1715 :         if ( ( error = ivas_dirac_allocate_parameters( hSpatParamRendCom, 1 ) ) != IVAS_ERR_OK )
     307             :         {
     308           0 :             return error;
     309             :         }
     310             : 
     311        1715 :         if ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT || ( ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT ) && hodirac_flag ) )
     312             :         {
     313         644 :             if ( ( error = ivas_dirac_allocate_parameters( hSpatParamRendCom, 2 ) ) != IVAS_ERR_OK )
     314             :             {
     315           0 :                 return error;
     316             :             }
     317             :         }
     318             :         else
     319             :         {
     320        1071 :             hSpatParamRendCom->azimuth2 = NULL;
     321        1071 :             hSpatParamRendCom->elevation2 = NULL;
     322        1071 :             hSpatParamRendCom->energy_ratio2 = NULL;
     323        1071 :             hSpatParamRendCom->spreadCoherence2 = NULL;
     324             :         }
     325             :     }
     326             : 
     327       14204 :     return error;
     328             : }
     329             : 
     330             : 
     331             : /*-------------------------------------------------------------------------
     332             :  * ivas_spat_hSpatParamRendCom_close()
     333             :  *
     334             :  *
     335             :  *-------------------------------------------------------------------------*/
     336             : 
     337        5171 : void ivas_spat_hSpatParamRendCom_close(
     338             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE *hSpatParamRendCom_out )
     339             : {
     340        5171 :     if ( hSpatParamRendCom_out == NULL || *hSpatParamRendCom_out == NULL )
     341             :     {
     342        3456 :         return;
     343             :     }
     344             : 
     345        1715 :     ivas_dirac_deallocate_parameters( *hSpatParamRendCom_out, 1 );
     346        1715 :     ivas_dirac_deallocate_parameters( *hSpatParamRendCom_out, 2 );
     347             : 
     348        1715 :     free( *hSpatParamRendCom_out );
     349        1715 :     *hSpatParamRendCom_out = NULL;
     350             : 
     351        1715 :     return;
     352             : }
     353             : 
     354             : 
     355             : /*-------------------------------------------------------------------------
     356             :  * ivas_dirac_rend_close()
     357             :  *
     358             :  *
     359             :  *-------------------------------------------------------------------------*/
     360             : 
     361       12332 : void ivas_dirac_rend_close(
     362             :     DIRAC_REND_HANDLE *hDirACRend_out )
     363             : {
     364             :     int16_t i, j;
     365             :     DIRAC_REND_HANDLE hDirACRend;
     366             : 
     367       12332 :     if ( hDirACRend_out == NULL || *hDirACRend_out == NULL )
     368             :     {
     369       10968 :         return;
     370             :     }
     371             : 
     372        1364 :     hDirACRend = *hDirACRend_out;
     373             : 
     374             :     /* close Output synthesis sub-module */
     375        1364 :     ivas_dirac_dec_output_synthesis_close( hDirACRend );
     376             : 
     377             :     /* close Decorrelator sub-module */
     378        1364 :     if ( hDirACRend->proto_signal_decorr_on )
     379             :     {
     380        1186 :         ivas_dirac_dec_decorr_close( &hDirACRend->h_freq_domain_decorr_ap_params, &hDirACRend->h_freq_domain_decorr_ap_state );
     381             :     }
     382             : 
     383             :     /* Params */
     384             : 
     385             :     /* free frequency axis buffer */
     386        1364 :     if ( hDirACRend->frequency_axis != NULL )
     387             :     {
     388        1364 :         free( hDirACRend->frequency_axis );
     389        1364 :         hDirACRend->frequency_axis = NULL;
     390             :     }
     391             : 
     392        1364 :     if ( hDirACRend->diffuse_response_function != NULL )
     393             :     {
     394        1364 :         free( hDirACRend->diffuse_response_function );
     395        1364 :         hDirACRend->diffuse_response_function = NULL;
     396             :     }
     397             : 
     398        1364 :     if ( hDirACRend->hoa_encoder != NULL )
     399             :     {
     400          42 :         free( hDirACRend->hoa_encoder );
     401          42 :         hDirACRend->hoa_encoder = NULL;
     402             :     }
     403             : 
     404             :     /* prototype indexing */
     405        1364 :     if ( hDirACRend->proto_index_dir != NULL )
     406             :     {
     407        1364 :         free( hDirACRend->proto_index_dir );
     408        1364 :         hDirACRend->proto_index_dir = NULL;
     409             :     }
     410             : 
     411        1364 :     if ( hDirACRend->proto_index_diff != NULL )
     412             :     {
     413        1364 :         free( hDirACRend->proto_index_diff );
     414        1364 :         hDirACRend->proto_index_dir = NULL;
     415             :     }
     416             : 
     417             :     /* States */
     418             : 
     419             :     /* free prototype signal buffers */
     420        1364 :     if ( hDirACRend->proto_frame_f != NULL )
     421             :     {
     422         653 :         free( hDirACRend->proto_frame_f );
     423         653 :         hDirACRend->proto_frame_f = NULL;
     424             :     }
     425             : 
     426        5456 :     for ( i = 0; i < DIRAC_NUM_DIMS; i++ )
     427             :     {
     428      135036 :         for ( j = 0; j < DIRAC_NO_COL_AVG_DIFF; j++ )
     429             :         {
     430      130944 :             if ( hDirACRend->buffer_intensity_real[i][j] != NULL )
     431             :             {
     432       51552 :                 free( hDirACRend->buffer_intensity_real[i][j] );
     433       51552 :                 hDirACRend->buffer_intensity_real[i][j] = NULL;
     434             :             }
     435             :         }
     436             :     }
     437        1364 :     if ( hDirACRend->buffer_energy != NULL )
     438             :     {
     439         537 :         free( hDirACRend->buffer_energy );
     440         537 :         hDirACRend->buffer_energy = NULL;
     441             :     }
     442             : 
     443        1364 :     if ( hDirACRend->masa_stereo_type_detect != NULL )
     444             :     {
     445         190 :         free( hDirACRend->masa_stereo_type_detect );
     446         190 :         hDirACRend->masa_stereo_type_detect = NULL;
     447             :     }
     448             : 
     449        1364 :     ivas_dirac_free_mem( &( hDirACRend->stack_mem ) );
     450             : 
     451        1364 :     free( *hDirACRend_out );
     452        1364 :     *hDirACRend_out = NULL;
     453             : 
     454        1364 :     return;
     455             : }
     456             : 
     457             : 
     458             : /*-------------------------------------------------------------------------
     459             :  * ivas_dirac_deallocate_parameters()
     460             :  *
     461             :  * Deallocate DirAC parameters
     462             :  *-------------------------------------------------------------------------*/
     463             : 
     464        5200 : void ivas_dirac_deallocate_parameters(
     465             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom, /* i/o: common data for spatial parametric rendering  */
     466             :     const int16_t params_flag                             /* i  : set of parameters flag                        */
     467             : )
     468             : {
     469             :     int16_t i;
     470             :     int16_t md_buffer_length;
     471             : 
     472        5200 :     if ( hSpatParamRendCom == NULL )
     473             :     {
     474           0 :         return;
     475             :     }
     476             : 
     477        5200 :     md_buffer_length = hSpatParamRendCom->dirac_md_buffer_length;
     478             : 
     479        5200 :     if ( params_flag == 1 )
     480             :     {
     481        2462 :         if ( hSpatParamRendCom->azimuth != NULL )
     482             :         {
     483       14116 :             for ( i = 0; i < md_buffer_length; i++ )
     484             :             {
     485       11654 :                 if ( hSpatParamRendCom->azimuth[i] != NULL )
     486             :                 {
     487       11654 :                     free( hSpatParamRendCom->azimuth[i] );
     488       11654 :                     hSpatParamRendCom->azimuth[i] = NULL;
     489             :                 }
     490             :             }
     491             : 
     492        2462 :             free( hSpatParamRendCom->azimuth );
     493        2462 :             hSpatParamRendCom->azimuth = NULL;
     494             :         }
     495             : 
     496        2462 :         if ( hSpatParamRendCom->elevation != NULL )
     497             :         {
     498       14116 :             for ( i = 0; i < md_buffer_length; i++ )
     499             :             {
     500       11654 :                 if ( hSpatParamRendCom->elevation[i] != NULL )
     501             :                 {
     502       11654 :                     free( hSpatParamRendCom->elevation[i] );
     503       11654 :                     hSpatParamRendCom->elevation[i] = NULL;
     504             :                 }
     505             :             }
     506             : 
     507        2462 :             free( hSpatParamRendCom->elevation );
     508        2462 :             hSpatParamRendCom->elevation = NULL;
     509             :         }
     510             : 
     511        2462 :         if ( hSpatParamRendCom->energy_ratio1 != NULL )
     512             :         {
     513       14116 :             for ( i = 0; i < md_buffer_length; i++ )
     514             :             {
     515       11654 :                 if ( hSpatParamRendCom->energy_ratio1[i] != NULL )
     516             :                 {
     517       11654 :                     free( hSpatParamRendCom->energy_ratio1[i] );
     518       11654 :                     hSpatParamRendCom->energy_ratio1[i] = NULL;
     519             :                 }
     520             :             }
     521        2462 :             free( hSpatParamRendCom->energy_ratio1 );
     522        2462 :             hSpatParamRendCom->energy_ratio1 = NULL;
     523             :         }
     524             : 
     525        2462 :         if ( hSpatParamRendCom->diffuseness_vector != NULL )
     526             :         {
     527       14116 :             for ( i = 0; i < md_buffer_length; i++ )
     528             :             {
     529       11654 :                 if ( hSpatParamRendCom->diffuseness_vector[i] != NULL )
     530             :                 {
     531       11654 :                     free( hSpatParamRendCom->diffuseness_vector[i] );
     532       11654 :                     hSpatParamRendCom->diffuseness_vector[i] = NULL;
     533             :                 }
     534             :             }
     535             : 
     536        2462 :             free( hSpatParamRendCom->diffuseness_vector );
     537        2462 :             hSpatParamRendCom->diffuseness_vector = NULL;
     538             :         }
     539             : 
     540        2462 :         if ( hSpatParamRendCom->spreadCoherence != NULL )
     541             :         {
     542       14116 :             for ( i = 0; i < md_buffer_length; i++ )
     543             :             {
     544       11654 :                 if ( hSpatParamRendCom->spreadCoherence[i] != NULL )
     545             :                 {
     546       11654 :                     free( hSpatParamRendCom->spreadCoherence[i] );
     547       11654 :                     hSpatParamRendCom->spreadCoherence[i] = NULL;
     548             :                 }
     549             :             }
     550        2462 :             free( hSpatParamRendCom->spreadCoherence );
     551        2462 :             hSpatParamRendCom->spreadCoherence = NULL;
     552             :         }
     553             : 
     554        2462 :         if ( hSpatParamRendCom->surroundingCoherence != NULL )
     555             :         {
     556       14116 :             for ( i = 0; i < md_buffer_length; i++ )
     557             :             {
     558       11654 :                 if ( hSpatParamRendCom->surroundingCoherence[i] != NULL )
     559             :                 {
     560       11654 :                     free( hSpatParamRendCom->surroundingCoherence[i] );
     561       11654 :                     hSpatParamRendCom->surroundingCoherence[i] = NULL;
     562             :                 }
     563             :             }
     564        2462 :             free( hSpatParamRendCom->surroundingCoherence );
     565        2462 :             hSpatParamRendCom->surroundingCoherence = NULL;
     566             :         }
     567             :     }
     568        2738 :     else if ( params_flag == 2 )
     569             :     {
     570        2738 :         if ( hSpatParamRendCom->azimuth2 != NULL )
     571             :         {
     572       10180 :             for ( i = 0; i < md_buffer_length; i++ )
     573             :             {
     574        8486 :                 if ( hSpatParamRendCom->azimuth2[i] != NULL )
     575             :                 {
     576        8486 :                     free( hSpatParamRendCom->azimuth2[i] );
     577        8486 :                     hSpatParamRendCom->azimuth2[i] = NULL;
     578             :                 }
     579             :             }
     580        1694 :             free( hSpatParamRendCom->azimuth2 );
     581        1694 :             hSpatParamRendCom->azimuth2 = NULL;
     582             :         }
     583             : 
     584        2738 :         if ( hSpatParamRendCom->elevation2 != NULL )
     585             :         {
     586       10180 :             for ( i = 0; i < md_buffer_length; i++ )
     587             :             {
     588        8486 :                 if ( hSpatParamRendCom->elevation2[i] != NULL )
     589             :                 {
     590        8486 :                     free( hSpatParamRendCom->elevation2[i] );
     591        8486 :                     hSpatParamRendCom->elevation2[i] = NULL;
     592             :                 }
     593             :             }
     594        1694 :             free( hSpatParamRendCom->elevation2 );
     595        1694 :             hSpatParamRendCom->elevation2 = NULL;
     596             :         }
     597             : 
     598        2738 :         if ( hSpatParamRendCom->energy_ratio2 != NULL )
     599             :         {
     600       10180 :             for ( i = 0; i < md_buffer_length; i++ )
     601             :             {
     602        8486 :                 if ( hSpatParamRendCom->energy_ratio2[i] != NULL )
     603             :                 {
     604        8486 :                     free( hSpatParamRendCom->energy_ratio2[i] );
     605        8486 :                     hSpatParamRendCom->energy_ratio2[i] = NULL;
     606             :                 }
     607             :             }
     608        1694 :             free( hSpatParamRendCom->energy_ratio2 );
     609        1694 :             hSpatParamRendCom->energy_ratio2 = NULL;
     610             :         }
     611             : 
     612        2738 :         if ( hSpatParamRendCom->spreadCoherence2 != NULL )
     613             :         {
     614       10180 :             for ( i = 0; i < md_buffer_length; i++ )
     615             :             {
     616        8486 :                 if ( hSpatParamRendCom->spreadCoherence2[i] != NULL )
     617             :                 {
     618        8486 :                     free( hSpatParamRendCom->spreadCoherence2[i] );
     619        8486 :                     hSpatParamRendCom->spreadCoherence2[i] = NULL;
     620             :                 }
     621             :             }
     622        1694 :             free( hSpatParamRendCom->spreadCoherence2 );
     623        1694 :             hSpatParamRendCom->spreadCoherence2 = NULL;
     624             :         }
     625             :     }
     626             : 
     627        5200 :     return;
     628             : }
     629             : 
     630             : 
     631             : /*-------------------------------------------------------------------------
     632             :  * ivas_dirac_alloc_mem()
     633             :  *
     634             :  * Allocate stack memory for DirAC renderer
     635             :  *------------------------------------------------------------------------*/
     636             : 
     637        6935 : ivas_error ivas_dirac_alloc_mem(
     638             :     DIRAC_REND_HANDLE hDirACRend,
     639             :     const RENDERER_TYPE renderer_type,
     640             :     const int16_t num_freq_bands,
     641             :     DIRAC_DEC_STACK_MEM_HANDLE hDirAC_mem,
     642             :     const int16_t hodirac_flag )
     643             : {
     644             :     int16_t num_freq_bands_diff, size;
     645             :     int16_t size_ho;
     646             :     int16_t size_pf;
     647             :     int16_t num_outputs_dir, num_outputs_diff;
     648             :     int16_t num_protos_dir;
     649             : 
     650        6935 :     num_protos_dir = hDirACRend->num_protos_dir;
     651             : 
     652        6935 :     num_freq_bands_diff = hDirACRend->h_output_synthesis_psd_params.max_band_decorr;
     653             : 
     654        6935 :     num_outputs_dir = hDirACRend->num_outputs_dir;
     655        6935 :     num_outputs_diff = hDirACRend->num_outputs_diff;
     656             : 
     657        6935 :     size = num_freq_bands * num_outputs_dir;
     658        6935 :     if ( hodirac_flag )
     659             :     {
     660         378 :         size_ho = size * DIRAC_HO_NUMSECTORS;
     661         378 :         size_pf = num_freq_bands * DIRAC_HO_NUMSECTORS;
     662             :     }
     663             :     else
     664             :     {
     665        6557 :         size_ho = size;
     666        6557 :         size_pf = num_freq_bands;
     667             :     }
     668             : 
     669             :     /* PSD related buffers */
     670        6935 :     hDirAC_mem->cy_auto_dir_smooth = NULL;
     671        6935 :     hDirAC_mem->proto_power_smooth = NULL;
     672        6935 :     hDirAC_mem->proto_power_diff_smooth = NULL;
     673        6935 :     hDirAC_mem->direct_responses_square = NULL;
     674        6935 :     hDirAC_mem->frame_dec_f = NULL;
     675        6935 :     if ( hDirACRend->synthesisConf != DIRAC_SYNTHESIS_GAIN_SHD )
     676             :     {
     677        4238 :         if ( ( hDirAC_mem->cy_auto_dir_smooth = (float *) malloc( sizeof( float ) * size ) ) == NULL )
     678             :         {
     679           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     680             :         }
     681        4238 :         set_zero( hDirAC_mem->cy_auto_dir_smooth, size );
     682             : 
     683        4238 :         if ( ( hDirAC_mem->proto_power_smooth = (float *) malloc( sizeof( float ) * size ) ) == NULL )
     684             :         {
     685           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     686             :         }
     687        4238 :         set_zero( hDirAC_mem->proto_power_smooth, size );
     688             : 
     689        4238 :         if ( ( hDirAC_mem->proto_power_diff_smooth = (float *) malloc( sizeof( float ) * size ) ) == NULL )
     690             :         {
     691           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     692             :         }
     693        4238 :         set_zero( hDirAC_mem->proto_power_diff_smooth, size );
     694             : 
     695        4238 :         if ( ( hDirAC_mem->direct_responses_square = (float *) malloc( sizeof( float ) * size ) ) == NULL )
     696             :         {
     697           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     698             :         }
     699        4238 :         set_zero( hDirAC_mem->direct_responses_square, size );
     700        4238 :         if ( hDirACRend->proto_signal_decorr_on && ( renderer_type != RENDERER_BINAURAL_PARAMETRIC && renderer_type != RENDERER_BINAURAL_PARAMETRIC_ROOM && renderer_type != RENDERER_STEREO_PARAMETRIC ) )
     701             :         {
     702        3769 :             if ( ( hDirAC_mem->frame_dec_f = (float *) malloc( sizeof( float ) * 2 * num_outputs_diff * num_freq_bands ) ) == NULL )
     703             :             {
     704           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     705             :             }
     706             :         }
     707             :     }
     708        6935 :     hDirACRend->h_output_synthesis_psd_state.proto_power_smooth = hDirAC_mem->proto_power_smooth;
     709        6935 :     hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth = hDirAC_mem->proto_power_diff_smooth;
     710        6935 :     hDirACRend->h_output_synthesis_psd_state.cy_auto_dir_smooth = hDirAC_mem->cy_auto_dir_smooth;
     711        6935 :     hDirACRend->h_output_synthesis_psd_state.direct_responses_square = hDirAC_mem->direct_responses_square;
     712             : 
     713             :     /* Target and smoothed nrg factors/gains */
     714        6935 :     if ( ( hDirAC_mem->cy_cross_dir_smooth = (float *) malloc( sizeof( float ) * size_ho ) ) == NULL )
     715             :     {
     716           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     717             :     }
     718        6935 :     set_zero( hDirAC_mem->cy_cross_dir_smooth, size );
     719             : 
     720        6935 :     if ( hDirACRend->synthesisConf != DIRAC_SYNTHESIS_GAIN_SHD )
     721             :     {
     722        4238 :         if ( ( hDirAC_mem->cy_auto_diff_smooth = (float *) malloc( sizeof( float ) * size ) ) == NULL )
     723             :         {
     724           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     725             :         }
     726        4238 :         set_zero( hDirAC_mem->cy_auto_diff_smooth, size );
     727             :     }
     728             :     else
     729             :     {
     730        2697 :         if ( ( hDirAC_mem->cy_auto_diff_smooth = (float *) malloc( sizeof( float ) * num_outputs_diff * num_freq_bands_diff ) ) == NULL )
     731             :         {
     732           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     733             :         }
     734        2697 :         set_zero( hDirAC_mem->cy_auto_diff_smooth, num_outputs_diff * num_freq_bands_diff );
     735             :     }
     736        6935 :     hDirACRend->h_output_synthesis_psd_state.cy_cross_dir_smooth = hDirAC_mem->cy_cross_dir_smooth;
     737        6935 :     hDirACRend->h_output_synthesis_psd_state.cy_auto_diff_smooth = hDirAC_mem->cy_auto_diff_smooth;
     738             : 
     739             :     /*Responses (gains/factors)*/
     740        6935 :     if ( ( hDirAC_mem->direct_responses = (float *) malloc( sizeof( float ) * size_ho ) ) == NULL )
     741             :     {
     742           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     743             :     }
     744        6935 :     set_zero( hDirAC_mem->direct_responses, size );
     745             : 
     746             : 
     747        6935 :     hDirACRend->h_output_synthesis_psd_state.direct_responses = hDirAC_mem->direct_responses;
     748             : 
     749             :     /* Prototypes */
     750        6935 :     hDirAC_mem->proto_direct_buffer_f = NULL;
     751        6935 :     hDirAC_mem->proto_diffuse_buffer_f = NULL;
     752        6935 :     if ( renderer_type != RENDERER_BINAURAL_PARAMETRIC && renderer_type != RENDERER_BINAURAL_PARAMETRIC_ROOM && renderer_type != RENDERER_STEREO_PARAMETRIC )
     753             :     {
     754        6935 :         if ( ( hDirAC_mem->proto_direct_buffer_f = (float *) malloc( sizeof( float ) * 2 * MAX_PARAM_SPATIAL_SUBFRAMES * num_protos_dir * num_freq_bands ) ) == NULL )
     755             :         {
     756           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     757             :         }
     758             : 
     759        6935 :         if ( hDirACRend->proto_signal_decorr_on )
     760             :         {
     761        6466 :             if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_SHD )
     762             :             {
     763        1239 :                 if ( ( hDirAC_mem->proto_diffuse_buffer_f = (float *) malloc( sizeof( float ) * 2 * MAX_PARAM_SPATIAL_SUBFRAMES * size ) ) == NULL )
     764             :                 {
     765           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     766             :                 }
     767             :             }
     768             :             else
     769             :             {
     770        5227 :                 if ( ( hDirAC_mem->proto_diffuse_buffer_f = (float *) malloc( sizeof( float ) * 2 * MAX_PARAM_SPATIAL_SUBFRAMES * num_outputs_diff * num_freq_bands ) ) == NULL )
     771             :                 {
     772           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     773             :                 }
     774             :             }
     775             :         }
     776             :     }
     777        6935 :     hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f = hDirAC_mem->proto_direct_buffer_f;
     778        6935 :     hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f = hDirAC_mem->proto_diffuse_buffer_f;
     779             : 
     780             :     /* Gains/power factors*/
     781        6935 :     hDirAC_mem->direct_power_factor = NULL;
     782        6935 :     hDirAC_mem->diffuse_power_factor = NULL;
     783             : 
     784        6935 :     if ( renderer_type != RENDERER_BINAURAL_PARAMETRIC && renderer_type != RENDERER_BINAURAL_PARAMETRIC_ROOM && renderer_type != RENDERER_STEREO_PARAMETRIC )
     785             :     {
     786        6935 :         if ( ( hDirAC_mem->direct_power_factor = (float *) malloc( sizeof( float ) * size_pf ) ) == NULL )
     787             :         {
     788           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     789             :         }
     790        6935 :         if ( ( hDirAC_mem->diffuse_power_factor = (float *) malloc( sizeof( float ) * size_pf ) ) == NULL )
     791             :         {
     792           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     793             :         }
     794             :     }
     795             : 
     796        6935 :     hDirACRend->h_output_synthesis_psd_state.direct_power_factor = hDirAC_mem->direct_power_factor;
     797        6935 :     hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor = hDirAC_mem->diffuse_power_factor;
     798             : 
     799        6935 :     hDirAC_mem->reference_power = NULL;
     800        6935 :     hDirAC_mem->onset_filter = NULL;
     801        6935 :     if ( hDirACRend->synthesisConf != DIRAC_SYNTHESIS_GAIN_SHD )
     802             :     {
     803        4238 :         if ( renderer_type != RENDERER_BINAURAL_PARAMETRIC && renderer_type != RENDERER_BINAURAL_PARAMETRIC_ROOM && renderer_type != RENDERER_STEREO_PARAMETRIC )
     804             :         {
     805        4238 :             if ( ( hDirAC_mem->reference_power = (float *) malloc( sizeof( float ) * 2 * num_freq_bands ) ) == NULL )
     806             :             {
     807           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     808             :             }
     809        4238 :             if ( hDirACRend->proto_signal_decorr_on )
     810             :             {
     811        3769 :                 if ( ( hDirAC_mem->onset_filter = (float *) malloc( sizeof( float ) * num_outputs_diff * num_freq_bands ) ) == NULL )
     812             :                 {
     813           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     814             :                 }
     815             :             }
     816             :         }
     817             :     }
     818             :     else
     819             :     {
     820        2697 :         if ( num_protos_dir > 2 )
     821             :         {
     822        2676 :             if ( ( hDirAC_mem->reference_power = (float *) malloc( sizeof( float ) * 5 * num_freq_bands ) ) == NULL )
     823             :             {
     824           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     825             :             }
     826             :         }
     827             : 
     828        2697 :         if ( hDirACRend->proto_signal_decorr_on )
     829             :         {
     830        2697 :             if ( ( hDirAC_mem->onset_filter = (float *) malloc( sizeof( float ) * 2 * num_freq_bands ) ) == NULL )
     831             :             {
     832           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     833             :             }
     834             :         }
     835             :     }
     836             : 
     837        6935 :     return IVAS_ERR_OK;
     838             : }
     839             : 
     840             : 
     841             : /*-------------------------------------------------------------------------
     842             :  * ivas_dirac_free_mem()
     843             :  *
     844             :  *
     845             :  *-------------------------------------------------------------------------*/
     846             : 
     847        6935 : void ivas_dirac_free_mem(
     848             :     DIRAC_DEC_STACK_MEM_HANDLE hDirAC_mem )
     849             : {
     850        6935 :     if ( hDirAC_mem->cy_auto_dir_smooth != NULL )
     851             :     {
     852        4238 :         free( hDirAC_mem->cy_auto_dir_smooth );
     853             :     }
     854        6935 :     if ( hDirAC_mem->proto_power_smooth != NULL )
     855             :     {
     856        4238 :         free( hDirAC_mem->proto_power_smooth );
     857             :     }
     858        6935 :     if ( hDirAC_mem->proto_power_diff_smooth != NULL )
     859             :     {
     860        4238 :         free( hDirAC_mem->proto_power_diff_smooth );
     861             :     }
     862        6935 :     if ( hDirAC_mem->direct_responses_square != NULL )
     863             :     {
     864        4238 :         free( hDirAC_mem->direct_responses_square );
     865             :     }
     866        6935 :     if ( hDirAC_mem->frame_dec_f != NULL )
     867             :     {
     868        3769 :         free( hDirAC_mem->frame_dec_f );
     869             :     }
     870        6935 :     if ( hDirAC_mem->cy_cross_dir_smooth != NULL )
     871             :     {
     872        6935 :         free( hDirAC_mem->cy_cross_dir_smooth );
     873             :     }
     874        6935 :     if ( hDirAC_mem->cy_auto_diff_smooth != NULL )
     875             :     {
     876        6935 :         free( hDirAC_mem->cy_auto_diff_smooth );
     877             :     }
     878        6935 :     if ( hDirAC_mem->direct_responses != NULL )
     879             :     {
     880        6935 :         free( hDirAC_mem->direct_responses );
     881             :     }
     882        6935 :     if ( hDirAC_mem->proto_direct_buffer_f != NULL )
     883             :     {
     884        6935 :         free( hDirAC_mem->proto_direct_buffer_f );
     885             :     }
     886        6935 :     if ( hDirAC_mem->proto_diffuse_buffer_f != NULL )
     887             :     {
     888        6466 :         free( hDirAC_mem->proto_diffuse_buffer_f );
     889             :     }
     890        6935 :     if ( hDirAC_mem->direct_power_factor != NULL )
     891             :     {
     892        6935 :         free( hDirAC_mem->direct_power_factor );
     893             :     }
     894        6935 :     if ( hDirAC_mem->diffuse_power_factor != NULL )
     895             :     {
     896        6935 :         free( hDirAC_mem->diffuse_power_factor );
     897             :     }
     898        6935 :     if ( hDirAC_mem->reference_power != NULL )
     899             :     {
     900        6914 :         free( hDirAC_mem->reference_power );
     901             :     }
     902        6935 :     if ( hDirAC_mem->onset_filter != NULL )
     903             :     {
     904        6466 :         free( hDirAC_mem->onset_filter );
     905             :     }
     906             : 
     907        6935 :     return;
     908             : }
     909             : 
     910             : 
     911             : /*-------------------------------------------------------------------------
     912             :  * compute_hoa_encoder_mtx()
     913             :  *
     914             :  *
     915             :  *------------------------------------------------------------------------*/
     916             : 
     917        1287 : void compute_hoa_encoder_mtx(
     918             :     const float *azimuth,
     919             :     const float *elevation,
     920             :     float *response,
     921             :     const int16_t num_responses,
     922             :     const int16_t ambisonics_order )
     923             : {
     924             :     int16_t k, num_sh;
     925             : 
     926        1287 :     num_sh = ivas_sba_get_nchan( ambisonics_order, 0 );
     927             : 
     928       11541 :     for ( k = 0; k < num_responses; k++ )
     929             :     {
     930       10254 :         ivas_dirac_dec_get_response( (const int16_t) azimuth[k], (const int16_t) elevation[k], &response[k * num_sh], ambisonics_order );
     931             :     }
     932             : 
     933        1287 :     return;
     934             : }
     935             : 
     936             : 
     937             : /*-------------------------------------------------------------------------
     938             :  * ivas_dirac_dec_get_frequency_axis()
     939             :  *
     940             :  * DirAC decoding initialization
     941             :  *------------------------------------------------------------------------*/
     942             : 
     943        5189 : void ivas_dirac_dec_get_frequency_axis(
     944             :     float *frequency_axis,
     945             :     const int32_t output_Fs,
     946             :     const int16_t num_freq_bands )
     947             : {
     948             :     int16_t k;
     949             :     float const_part;
     950             : 
     951             :     /* calc cldfb frequency axis */
     952        5189 :     const_part = (float) output_Fs / ( 2.0f * (float) num_freq_bands );
     953      265529 :     for ( k = 0; k < num_freq_bands; ++k )
     954             :     {
     955      260340 :         frequency_axis[k] = ( (float) k + 0.5f ) * const_part;
     956             :     }
     957             : 
     958        5189 :     return;
     959             : }
     960             : 
     961             : 
     962             : /*-------------------------------------------------------------------------
     963             :  * initDiffuseResponses()
     964             :  *
     965             :  *
     966             :  *-------------------------------------------------------------------------*/
     967             : 
     968        6935 : void initDiffuseResponses(
     969             :     float *diffuse_response_function,
     970             :     const int16_t num_channels,
     971             :     const AUDIO_CONFIG output_config,
     972             :     const IVAS_OUTPUT_SETUP hOutSetup,
     973             :     const int16_t ambisonics_order,
     974             :     const IVAS_FORMAT ivas_format,
     975             :     int16_t *num_ele_spk_no_diffuse_rendering,
     976             :     const AUDIO_CONFIG transport_config )
     977             : {
     978             :     int16_t i, l, k, idx, num_horizontal_speakers;
     979        6935 :     *num_ele_spk_no_diffuse_rendering = 0;
     980             : 
     981        6935 :     if ( output_config == IVAS_AUDIO_CONFIG_MONO )
     982             :     {
     983         331 :         diffuse_response_function[0] = 1.0f;
     984         331 :         diffuse_response_function[1] = inv_sqrt( 3.0f );
     985             :     }
     986        6604 :     else if ( !( output_config == IVAS_AUDIO_CONFIG_FOA || output_config == IVAS_AUDIO_CONFIG_HOA2 || output_config == IVAS_AUDIO_CONFIG_HOA3 ) )
     987             :     {
     988             :         /* set diffuse response function */
     989        2668 :         if ( ivas_format == MC_FORMAT && ( transport_config == IVAS_AUDIO_CONFIG_5_1 || transport_config == IVAS_AUDIO_CONFIG_7_1 ) && output_config == IVAS_AUDIO_CONFIG_5_1_4 )
     990             :         {
     991           0 :             num_horizontal_speakers = num_channels - NUM_ELEVATED_SPEAKERS;
     992             : 
     993           0 :             mvr2r( diffuse_response_CICP6, diffuse_response_function, num_horizontal_speakers );
     994           0 :             set_zero( &diffuse_response_function[num_horizontal_speakers], NUM_ELEVATED_SPEAKERS );
     995           0 :             *num_ele_spk_no_diffuse_rendering = NUM_ELEVATED_SPEAKERS;
     996             :         }
     997        2668 :         else if ( ivas_format == MC_FORMAT && ( transport_config == IVAS_AUDIO_CONFIG_5_1 || transport_config == IVAS_AUDIO_CONFIG_7_1 ) && output_config == IVAS_AUDIO_CONFIG_7_1_4 )
     998             :         {
     999         237 :             num_horizontal_speakers = num_channels - NUM_ELEVATED_SPEAKERS;
    1000             : 
    1001         237 :             set_f( diffuse_response_function, sqrtf( 1.f / ( (float) num_horizontal_speakers ) ), num_horizontal_speakers );
    1002         237 :             set_zero( &diffuse_response_function[num_horizontal_speakers], NUM_ELEVATED_SPEAKERS );
    1003         237 :             *num_ele_spk_no_diffuse_rendering = NUM_ELEVATED_SPEAKERS;
    1004             :         }
    1005        2431 :         else if ( ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT || ivas_format == MC_FORMAT ) && output_config == IVAS_AUDIO_CONFIG_5_1 && num_channels == 5 )
    1006             :         {
    1007         299 :             mvr2r( diffuse_response_CICP6, diffuse_response_function, num_channels );
    1008             :         }
    1009        2132 :         else if ( ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT || ivas_format == MC_FORMAT ) && output_config == IVAS_AUDIO_CONFIG_5_1_2 && num_channels == 7 )
    1010             :         {
    1011         602 :             mvr2r( diffuse_response_CICP14, diffuse_response_function, num_channels );
    1012             :         }
    1013        1530 :         else if ( ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT || ivas_format == MC_FORMAT ) && ( output_config == IVAS_AUDIO_CONFIG_5_1_4 ) && ( num_channels == 9 ) )
    1014             :         {
    1015         656 :             mvr2r( diffuse_response_CICP16, diffuse_response_function, num_channels );
    1016             :         }
    1017         874 :         else if ( ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT || ivas_format == MC_FORMAT ) && ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM ) )
    1018             :         {
    1019           0 :             if ( transport_config == IVAS_AUDIO_CONFIG_5_1 || transport_config == IVAS_AUDIO_CONFIG_7_1 )
    1020             :             {
    1021             :                 /* Detect loudspeakers with elevation */
    1022           0 :                 for ( i = 0, num_horizontal_speakers = 0; i < num_channels; i++ )
    1023             :                 {
    1024           0 :                     if ( fabsf( hOutSetup.ls_elevation[i] ) <= 5.f )
    1025             :                     {
    1026           0 :                         num_horizontal_speakers++;
    1027           0 :                         diffuse_response_function[i] = 1.f;
    1028             :                     }
    1029             :                     else
    1030             :                     {
    1031           0 :                         *num_ele_spk_no_diffuse_rendering += 1;
    1032           0 :                         diffuse_response_function[i] = 0.f;
    1033             :                     }
    1034             :                 }
    1035             :                 /* Diffuse only to horizontal plane if enough loudspeakers */
    1036           0 :                 if ( num_horizontal_speakers > 2 )
    1037             :                 {
    1038           0 :                     for ( i = 0; i < num_channels; i++ )
    1039             :                     {
    1040           0 :                         diffuse_response_function[i] *= sqrtf( 1.f / (float) num_horizontal_speakers );
    1041             :                     }
    1042             :                 }
    1043             :                 else
    1044             :                 {
    1045           0 :                     *num_ele_spk_no_diffuse_rendering = 0;
    1046           0 :                     set_f( diffuse_response_function, sqrtf( 1.f / (float) num_channels ), num_channels );
    1047             :                 }
    1048             :             }
    1049             :             else
    1050             :             {
    1051           0 :                 set_f( diffuse_response_function, sqrtf( 1.f / (float) num_channels ), num_channels );
    1052             :             }
    1053             :         }
    1054             :         else
    1055             :         {
    1056         874 :             set_f( diffuse_response_function, sqrtf( 1.f / (float) num_channels ), num_channels );
    1057             :         }
    1058             :     }
    1059             :     else
    1060             :     {
    1061        3936 :         idx = 0;
    1062       17472 :         for ( l = 0; l <= ambisonics_order; l++ )
    1063             :         {
    1064       62896 :             for ( k = 0; k < ( 2 * l + 1 ); k++ )
    1065             :             {
    1066       49360 :                 diffuse_response_function[idx++] = inv_sqrt( 2.0f * l + 1.0f );
    1067             :             }
    1068             :         }
    1069             :     }
    1070             : 
    1071        6935 :     return;
    1072             : }
    1073             : 
    1074             : 
    1075             : /*-------------------------------------------------------------------------
    1076             :  * protoSignalComputation_shd()
    1077             :  *
    1078             :  *
    1079             :  *-------------------------------------------------------------------------*/
    1080             : 
    1081     2863941 : void protoSignalComputation_shd(
    1082             :     float RealBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
    1083             :     float ImagBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
    1084             :     float *proto_direct_buffer_f,
    1085             :     float *proto_diffuse_buffer_f,
    1086             :     float *reference_power,
    1087             :     const int16_t slot_index,
    1088             :     const int16_t num_inputs,
    1089             :     const int16_t num_outputs_diff,
    1090             :     const int16_t num_freq_bands,
    1091             :     float *p_Rmat )
    1092             : {
    1093             :     int16_t l, k;
    1094             :     float *p_proto_direct_buffer;
    1095             :     float *p_proto_diffuse_buffer;
    1096             :     int16_t Rmat_k[4];
    1097             :     float W_real, W_imag;
    1098             :     float Y_real, Y_imag;
    1099             :     float *p_k[4];
    1100             : 
    1101     2863941 :     k = 0; /* to avoid compilation warning */
    1102             : 
    1103     2863941 :     p_proto_direct_buffer = proto_direct_buffer_f + slot_index * 2 * num_freq_bands * num_inputs;
    1104     2863941 :     p_proto_diffuse_buffer = proto_diffuse_buffer_f + slot_index * 2 * num_freq_bands * num_outputs_diff;
    1105             : 
    1106     2863941 :     if ( num_inputs == 1 )
    1107             :     {
    1108     8086404 :         for ( l = 0; l < num_freq_bands; l++ )
    1109             :         {
    1110     7953840 :             p_proto_direct_buffer[2 * l] = RealBuffer[0][0][l];
    1111     7953840 :             p_proto_direct_buffer[2 * l + 1] = ImagBuffer[0][0][l];
    1112             :         }
    1113             :     }
    1114     2731377 :     else if ( num_inputs == 2 )
    1115             :     {
    1116           0 :         if ( p_Rmat != 0 )
    1117             :         {
    1118           0 :             assert( num_inputs == 4 && "This code block should never be run with num_inputs != 4!" );
    1119             : 
    1120           0 :             for ( l = 0; l < num_freq_bands; l++ )
    1121             :             {
    1122           0 :                 W_real = RealBuffer[0][0][l] + RealBuffer[1][0][l];
    1123           0 :                 W_imag = ImagBuffer[0][0][l] + ImagBuffer[1][0][l];
    1124             : 
    1125           0 :                 Y_real = RealBuffer[0][0][l] - RealBuffer[1][0][l];
    1126           0 :                 Y_imag = ImagBuffer[0][0][l] - ImagBuffer[1][0][l];
    1127             : 
    1128           0 :                 p_proto_direct_buffer[2 * l] = W_real;
    1129           0 :                 p_proto_direct_buffer[2 * l + 1] = W_imag;
    1130           0 :                 p_proto_direct_buffer[2 * num_freq_bands + 2 * l] = p_Rmat[0] * Y_real;
    1131           0 :                 p_proto_direct_buffer[2 * num_freq_bands + 2 * l + 1] = p_Rmat[0] * Y_imag;
    1132             :             }
    1133             :         }
    1134             :         else
    1135             :         {
    1136           0 :             for ( l = 0; l < num_freq_bands; l++ )
    1137             :             {
    1138           0 :                 W_real = RealBuffer[0][0][l] + RealBuffer[1][0][l];
    1139           0 :                 W_imag = ImagBuffer[0][0][l] + ImagBuffer[1][0][l];
    1140             : 
    1141           0 :                 p_proto_direct_buffer[2 * l] = W_real;
    1142           0 :                 p_proto_direct_buffer[2 * l + 1] = W_imag;
    1143             :                 {
    1144           0 :                     p_proto_direct_buffer[2 * num_freq_bands + 2 * l] = RealBuffer[0][0][l] - RealBuffer[1][0][l];
    1145           0 :                     p_proto_direct_buffer[2 * num_freq_bands + 2 * l + 1] = ImagBuffer[0][0][l] - ImagBuffer[1][0][l];
    1146             :                 }
    1147             :             }
    1148             :         }
    1149             :     }
    1150     2731377 :     else if ( num_inputs >= 4 )
    1151             :     {
    1152     2731377 :         p_k[0] = p_proto_direct_buffer;
    1153     2731377 :         p_k[1] = p_proto_direct_buffer + 2 * num_freq_bands;
    1154     2731377 :         p_k[2] = p_proto_direct_buffer + 4 * num_freq_bands;
    1155     2731377 :         p_k[3] = p_proto_direct_buffer + 6 * num_freq_bands;
    1156     2731377 :         Rmat_k[0] = 0;
    1157     2731377 :         Rmat_k[1] = 1;
    1158     2731377 :         Rmat_k[2] = 2;
    1159     2731377 :         Rmat_k[3] = 0;
    1160             : 
    1161     2731377 :         if ( p_Rmat != 0 )
    1162             :         {
    1163      441600 :             assert( num_inputs == 4 && "This code block should never be run with num_inputs != 4!" );
    1164             : 
    1165    17337600 :             for ( l = 0; l < num_freq_bands; l++ )
    1166             :             {
    1167    16896000 :                 *( p_k[0] ) = RealBuffer[0][0][l];
    1168    16896000 :                 reference_power[l + num_freq_bands] = *( p_k[0] ) * *( p_k[0] );
    1169    16896000 :                 p_k[0]++;
    1170    16896000 :                 *( p_k[0] ) = ImagBuffer[0][0][l];
    1171    16896000 :                 reference_power[l + num_freq_bands] += *( p_k[0] ) * *( p_k[0] );
    1172    16896000 :                 p_k[0]++;
    1173    16896000 :                 reference_power[l] = 0.5f * reference_power[l + num_freq_bands];
    1174             : 
    1175    67584000 :                 for ( k = 1; k < 4; k++ )
    1176             :                 {
    1177    50688000 :                     *( p_k[k] ) = p_Rmat[3 * Rmat_k[k] + 1] * RealBuffer[1][0][l] + p_Rmat[3 * Rmat_k[k] + 2] * RealBuffer[2][0][l] + p_Rmat[3 * Rmat_k[k] + 0] * RealBuffer[3][0][l];
    1178    50688000 :                     reference_power[l + ( k + 1 ) * num_freq_bands] = *( p_k[k] ) * *( p_k[k] );
    1179    50688000 :                     p_k[k]++;
    1180    50688000 :                     *( p_k[k] ) = p_Rmat[3 * Rmat_k[k] + 1] * ImagBuffer[1][0][l] + p_Rmat[3 * Rmat_k[k] + 2] * ImagBuffer[2][0][l] + p_Rmat[3 * Rmat_k[k] + 0] * ImagBuffer[3][0][l];
    1181    50688000 :                     reference_power[l + ( k + 1 ) * num_freq_bands] += *( p_k[k] ) * *( p_k[k] );
    1182    50688000 :                     p_k[k]++;
    1183    50688000 :                     reference_power[l] += 0.5f * ( reference_power[l + ( k + 1 ) * num_freq_bands] );
    1184             :                 }
    1185             : 
    1186    67584000 :                 for ( k = 1; k < 4; k++ )
    1187             :                 {
    1188    50688000 :                     RealBuffer[k][0][l] = p_proto_direct_buffer[k * 2 * num_freq_bands + 2 * l];
    1189    50688000 :                     ImagBuffer[k][0][l] = p_proto_direct_buffer[k * 2 * num_freq_bands + 2 * l + 1];
    1190             :                 }
    1191             :             }
    1192             :         }
    1193             :         else
    1194             :         {
    1195     2289777 :             set_zero( reference_power, num_freq_bands );
    1196    11448885 :             for ( k = 0; k < 4; k++ )
    1197             :             {
    1198   520647588 :                 for ( l = 0; l < num_freq_bands; l++ )
    1199             :                 {
    1200   511488480 :                     p_proto_direct_buffer[k * 2 * num_freq_bands + 2 * l] = RealBuffer[k][0][l];
    1201   511488480 :                     p_proto_direct_buffer[k * 2 * num_freq_bands + 2 * l + 1] = ImagBuffer[k][0][l];
    1202   511488480 :                     reference_power[l + ( k + 1 ) * num_freq_bands] = RealBuffer[k][0][l] * RealBuffer[k][0][l] + ImagBuffer[k][0][l] * ImagBuffer[k][0][l];
    1203   511488480 :                     reference_power[l] += 0.5f * ( reference_power[l + ( k + 1 ) * num_freq_bands] );
    1204             :                 }
    1205             :             }
    1206             :         }
    1207             : 
    1208             :         /* Additional transport channels = planar SBA components of degree higher than 1*/
    1209     7220961 :         for ( ; k < num_inputs; k++ )
    1210             :         {
    1211   268411824 :             for ( l = 0; l < num_freq_bands; l++ )
    1212             :             {
    1213   263922240 :                 p_proto_direct_buffer[k * 2 * num_freq_bands + 2 * l] = RealBuffer[k][0][l];
    1214   263922240 :                 p_proto_direct_buffer[k * 2 * num_freq_bands + 2 * l + 1] = ImagBuffer[k][0][l];
    1215             :             }
    1216             :         }
    1217             :     }
    1218             : 
    1219             : 
    1220             :     /*Copy direct to diffuse proto*/
    1221     2863941 :     mvr2r( p_proto_direct_buffer, p_proto_diffuse_buffer, 2 * num_freq_bands * min( num_outputs_diff, num_inputs ) );
    1222             : 
    1223     2863941 :     if ( num_inputs == 1 )
    1224             :     {
    1225             :         /* Add comfort noise addition (CNA) to diffuse proto only*/
    1226     8086404 :         for ( l = 0; l < num_freq_bands; l++ )
    1227             :         {
    1228     7953840 :             p_proto_diffuse_buffer[2 * l] += RealBuffer[1][0][l];
    1229     7953840 :             p_proto_diffuse_buffer[2 * l + 1] += ImagBuffer[1][0][l];
    1230             :         }
    1231             :     }
    1232             : 
    1233     2863941 :     return;
    1234             : }
    1235             : 
    1236             : 
    1237             : /*-------------------------------------------------------------------------
    1238             :  * protoSignalComputation1()
    1239             :  *
    1240             :  *
    1241             :  *-------------------------------------------------------------------------*/
    1242             : 
    1243      384774 : void protoSignalComputation1(
    1244             :     float RealBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
    1245             :     float ImagBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
    1246             :     float *proto_frame_f,
    1247             :     float *proto_direct_buffer_f,
    1248             :     float *reference_power,
    1249             :     float *proto_power_smooth,
    1250             :     const int16_t slot_index,
    1251             :     const int16_t num_outputs_diff,
    1252             :     const int16_t num_freq_bands )
    1253             : {
    1254             :     int16_t l, k;
    1255             :     float *p_proto_buffer;
    1256             : 
    1257      384774 :     p_proto_buffer = proto_direct_buffer_f + slot_index * 2 * num_freq_bands;
    1258             : 
    1259    22387614 :     for ( l = 0; l < num_freq_bands; l++ )
    1260             :     {
    1261    22002840 :         reference_power[l] = RealBuffer[0][0][l] * RealBuffer[0][0][l] + ImagBuffer[0][0][l] * ImagBuffer[0][0][l];
    1262    22002840 :         proto_power_smooth[l] += reference_power[l];
    1263    22002840 :         p_proto_buffer[2 * l] = RealBuffer[0][0][l];
    1264    22002840 :         p_proto_buffer[2 * l + 1] = ImagBuffer[0][0][l];
    1265             : 
    1266    44005680 :         for ( k = 0; k < num_outputs_diff; k++ )
    1267             :         {
    1268    22002840 :             proto_frame_f[2 * k * num_freq_bands + 2 * l] = RealBuffer[0][0][l];
    1269    22002840 :             proto_frame_f[2 * k * num_freq_bands + 2 * l + 1] = ImagBuffer[0][0][l];
    1270             :         }
    1271             :     }
    1272             : 
    1273      384774 :     return;
    1274             : }
    1275             : 
    1276             : 
    1277             : /*-------------------------------------------------------------------------
    1278             :  * protoSignalComputation2()
    1279             :  *
    1280             :  *
    1281             :  *-------------------------------------------------------------------------*/
    1282             : 
    1283      493176 : void protoSignalComputation2(
    1284             :     float RealBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
    1285             :     float ImagBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
    1286             :     float *proto_frame_f,
    1287             :     float *proto_direct_buffer_f,
    1288             :     float *reference_power,
    1289             :     float *proto_power_smooth,
    1290             :     const int16_t isloudspeaker,
    1291             :     const int16_t slot_index,
    1292             :     const int16_t num_freq_bands,
    1293             :     MASA_STEREO_TYPE_DETECT *stereo_type_detect )
    1294             : {
    1295             :     int16_t l;
    1296             :     float *p_proto_buffer;
    1297             :     float Real_aux, Imag_aux;
    1298             : 
    1299             :     float left_bb_power, right_bb_power, total_bb_power, lr_bb_power;
    1300             :     float lr_total_bb_ratio;
    1301             :     float a, b;
    1302             : 
    1303             :     float left_hi_power, right_hi_power, total_hi_power, lr_hi_power;
    1304             :     float lr_total_hi_ratio;
    1305             :     float a2, b2;
    1306             : 
    1307             :     float sum_power;
    1308             :     float sum_total_ratio[MASA_SUM_FREQ_RANGE_BINS];
    1309             :     float min_sum_total_ratio;
    1310             :     float min_sum_total_ratio_db;
    1311             : 
    1312             :     float RealSubtract, ImagSubtract;
    1313             : 
    1314      493176 :     float interpolatorSpaced = 0.0f;
    1315      493176 :     float interpolatorDmx = 1.0f;
    1316             : 
    1317             :     int16_t dipole_freq_range[2];
    1318             :     float tempSpaced, tempDmx;
    1319             : 
    1320      493176 :     if ( isloudspeaker )
    1321             :     {
    1322      261507 :         p_proto_buffer = proto_direct_buffer_f + slot_index * 2 * num_freq_bands * 3;
    1323             : 
    1324    15635127 :         for ( l = 0; l < num_freq_bands; l++ )
    1325             :         {
    1326             :             float Left_power;
    1327             :             float Right_power;
    1328    15373620 :             Real_aux = RealBuffer[0][0][l] + RealBuffer[1][0][l];
    1329    15373620 :             Imag_aux = ImagBuffer[0][0][l] + ImagBuffer[1][0][l];
    1330             : 
    1331    15373620 :             Left_power = RealBuffer[0][0][l] * RealBuffer[0][0][l] + ImagBuffer[0][0][l] * ImagBuffer[0][0][l];
    1332    15373620 :             Right_power = RealBuffer[1][0][l] * RealBuffer[1][0][l] + ImagBuffer[1][0][l] * ImagBuffer[1][0][l];
    1333             : 
    1334    15373620 :             reference_power[l] = Left_power + Right_power;
    1335    15373620 :             proto_power_smooth[l] += Real_aux * Real_aux + Imag_aux * Imag_aux;
    1336             : 
    1337    15373620 :             p_proto_buffer[2 * l] = Real_aux;
    1338    15373620 :             p_proto_buffer[2 * l + 1] = Imag_aux;
    1339    15373620 :             proto_power_smooth[l + num_freq_bands] += RealBuffer[0][0][l] * RealBuffer[0][0][l] + ImagBuffer[0][0][l] * ImagBuffer[0][0][l];
    1340    15373620 :             p_proto_buffer[2 * num_freq_bands + 2 * l] = RealBuffer[0][0][l];
    1341    15373620 :             p_proto_buffer[2 * num_freq_bands + 2 * l + 1] = ImagBuffer[0][0][l];
    1342             : 
    1343    15373620 :             proto_power_smooth[l + 2 * num_freq_bands] += RealBuffer[1][0][l] * RealBuffer[1][0][l];
    1344    15373620 :             proto_power_smooth[l + 2 * num_freq_bands] += ImagBuffer[1][0][l] * ImagBuffer[1][0][l];
    1345    15373620 :             p_proto_buffer[4 * num_freq_bands + 2 * l] = RealBuffer[1][0][l];
    1346    15373620 :             p_proto_buffer[4 * num_freq_bands + 2 * l + 1] = ImagBuffer[1][0][l];
    1347             : 
    1348    15373620 :             proto_frame_f[2 * l] = Real_aux;
    1349    15373620 :             proto_frame_f[2 * l + 1] = Imag_aux;
    1350             : 
    1351    15373620 :             proto_frame_f[2 * num_freq_bands + 2 * l] = RealBuffer[0][0][l];
    1352    15373620 :             proto_frame_f[2 * num_freq_bands + 2 * l + 1] = ImagBuffer[0][0][l];
    1353    15373620 :             proto_frame_f[4 * num_freq_bands + 2 * l] = RealBuffer[1][0][l];
    1354    15373620 :             proto_frame_f[4 * num_freq_bands + 2 * l + 1] = ImagBuffer[1][0][l];
    1355             :         }
    1356             :     }
    1357      231669 :     else if ( stereo_type_detect != NULL )
    1358             :     {
    1359      166581 :         p_proto_buffer = proto_direct_buffer_f + slot_index * 2 * num_freq_bands * 2;
    1360             : 
    1361      166581 :         left_bb_power = 0.0f;
    1362      166581 :         right_bb_power = 0.0f;
    1363      166581 :         total_bb_power = 0.0f;
    1364             : 
    1365      166581 :         left_hi_power = 0.0f;
    1366      166581 :         right_hi_power = 0.0f;
    1367      166581 :         total_hi_power = 0.0f;
    1368             : 
    1369      166581 :         dipole_freq_range[0] = stereo_type_detect->dipole_freq_range[0];
    1370      166581 :         dipole_freq_range[1] = stereo_type_detect->dipole_freq_range[1];
    1371             : 
    1372      166581 :         a = 0.01f;      /* Temporal smoothing coefficient */
    1373      166581 :         b = 1.0f - a;   /* Temporal smoothing coefficient */
    1374      166581 :         a2 = 0.1f;      /* Temporal smoothing coefficient */
    1375      166581 :         b2 = 1.0f - a2; /* Temporal smoothing coefficient */
    1376             : 
    1377      166581 :         if ( stereo_type_detect->interpolator > 0 )
    1378             :         {
    1379          90 :             if ( stereo_type_detect->type_change_direction == MASA_STEREO_SPACED_MICS )
    1380             :             {
    1381          90 :                 interpolatorSpaced = ( (float) ( stereo_type_detect->interpolator ) ) / ( (float) MASA_STEREO_INTERPOLATION_SLOTS );
    1382          90 :                 interpolatorDmx = 1.0f - interpolatorSpaced;
    1383             :             }
    1384             :             else
    1385             :             {
    1386           0 :                 interpolatorDmx = ( (float) ( stereo_type_detect->interpolator ) ) / ( (float) MASA_STEREO_INTERPOLATION_SLOTS );
    1387           0 :                 interpolatorSpaced = 1.0f - interpolatorDmx;
    1388             :             }
    1389             :         }
    1390             : 
    1391     8841021 :         for ( l = 0; l < num_freq_bands; l++ )
    1392             :         {
    1393             :             float Left_power;
    1394             :             float Right_power;
    1395             : 
    1396             :             /* Compute sum signal */
    1397     8674440 :             Real_aux = RealBuffer[0][0][l] + RealBuffer[1][0][l];
    1398     8674440 :             Imag_aux = ImagBuffer[0][0][l] + ImagBuffer[1][0][l];
    1399             : 
    1400             :             /* Compute reference power */
    1401     8674440 :             Left_power = RealBuffer[0][0][l] * RealBuffer[0][0][l] + ImagBuffer[0][0][l] * ImagBuffer[0][0][l];
    1402     8674440 :             Right_power = RealBuffer[1][0][l] * RealBuffer[1][0][l] + ImagBuffer[1][0][l] * ImagBuffer[1][0][l];
    1403             : 
    1404     8674440 :             reference_power[l] = Left_power + Right_power;
    1405             : 
    1406     8674440 :             left_bb_power += Left_power;
    1407     8674440 :             right_bb_power += Right_power;
    1408     8674440 :             total_bb_power += reference_power[l];
    1409             : 
    1410     8674440 :             if ( l > MASA_HI_FREQ_START_BIN )
    1411             :             {
    1412     6175725 :                 left_hi_power += Left_power;
    1413     6175725 :                 right_hi_power += Right_power;
    1414     6175725 :                 total_hi_power += reference_power[l];
    1415             :             }
    1416             : 
    1417     8674440 :             if ( l < min( num_freq_bands, MASA_SUM_FREQ_RANGE_BINS ) )
    1418             :             {
    1419     4092525 :                 sum_power = Real_aux * Real_aux + Imag_aux * Imag_aux;
    1420             : 
    1421     4092525 :                 stereo_type_detect->sum_power[l] = a * sum_power + b * stereo_type_detect->sum_power[l];
    1422     4092525 :                 stereo_type_detect->total_power[l] = a * reference_power[l] + b * stereo_type_detect->total_power[l];
    1423             : 
    1424     4092525 :                 sum_total_ratio[l] = stereo_type_detect->sum_power[l] / ( stereo_type_detect->total_power[l] + EPSILON );
    1425             :             }
    1426             : 
    1427     8674440 :             if ( l == 0 )
    1428             :             {
    1429      166581 :                 RealSubtract = RealBuffer[0][0][l] - RealBuffer[1][0][l];
    1430      166581 :                 ImagSubtract = ImagBuffer[0][0][l] - ImagBuffer[1][0][l];
    1431      166581 :                 stereo_type_detect->subtract_power_y += RealSubtract * RealSubtract + ImagSubtract * ImagSubtract;
    1432             :             }
    1433             : 
    1434             :             /* Compute protos (and their power) for direct sound rendering */
    1435             : 
    1436             :             /* W prototype */
    1437     8674440 :             if ( stereo_type_detect->interpolator > 0 )
    1438             :             {
    1439        5400 :                 if ( l < ( dipole_freq_range[1] - 1 ) || l >= MASA_SUM_PROTO_START_BIN )
    1440             :                 {
    1441        4950 :                     Real_aux = interpolatorSpaced * 0.5f * Real_aux + interpolatorDmx * Real_aux;
    1442        4950 :                     Imag_aux = interpolatorSpaced * 0.5f * Imag_aux + interpolatorDmx * Imag_aux;
    1443        4950 :                     proto_power_smooth[l] += Real_aux * Real_aux + Imag_aux * Imag_aux;
    1444        4950 :                     p_proto_buffer[2 * l] = Real_aux;
    1445        4950 :                     p_proto_buffer[2 * l + 1] = Imag_aux;
    1446             :                 }
    1447             :                 else
    1448             :                 {
    1449         450 :                     tempSpaced = RealBuffer[0][0][l] * RealBuffer[0][0][l] + ImagBuffer[0][0][l] * ImagBuffer[0][0][l];
    1450         450 :                     tempDmx = Real_aux * Real_aux + Imag_aux * Imag_aux;
    1451         450 :                     proto_power_smooth[l] += interpolatorSpaced * tempSpaced + interpolatorDmx * tempDmx;
    1452         450 :                     p_proto_buffer[2 * l] = interpolatorSpaced * RealBuffer[0][0][l] + interpolatorDmx * Real_aux;
    1453         450 :                     p_proto_buffer[2 * l + 1] = interpolatorSpaced * ImagBuffer[0][0][l] + interpolatorDmx * Imag_aux;
    1454             :                 }
    1455             :             }
    1456     8669040 :             else if ( stereo_type_detect->masa_stereo_type == MASA_STEREO_SPACED_MICS )
    1457             :             {
    1458     1575360 :                 if ( l < ( dipole_freq_range[1] - 1 ) || l >= MASA_SUM_PROTO_START_BIN )
    1459             :                 {
    1460     1444080 :                     Real_aux *= 0.5f;
    1461     1444080 :                     Imag_aux *= 0.5f;
    1462     1444080 :                     proto_power_smooth[l] += Real_aux * Real_aux + Imag_aux * Imag_aux;
    1463     1444080 :                     p_proto_buffer[2 * l] = Real_aux;
    1464     1444080 :                     p_proto_buffer[2 * l + 1] = Imag_aux;
    1465             :                 }
    1466             :                 else
    1467             :                 {
    1468      131280 :                     proto_power_smooth[l] += RealBuffer[0][0][l] * RealBuffer[0][0][l] + ImagBuffer[0][0][l] * ImagBuffer[0][0][l];
    1469      131280 :                     p_proto_buffer[2 * l] = RealBuffer[0][0][l];
    1470      131280 :                     p_proto_buffer[2 * l + 1] = ImagBuffer[0][0][l];
    1471             :                 }
    1472             :             }
    1473             :             else
    1474             :             {
    1475     7093680 :                 proto_power_smooth[l] += Real_aux * Real_aux + Imag_aux * Imag_aux;
    1476     7093680 :                 p_proto_buffer[2 * l] = Real_aux;
    1477     7093680 :                 p_proto_buffer[2 * l + 1] = Imag_aux;
    1478             :             }
    1479             : 
    1480             :             /* Y prototype */
    1481     8674440 :             if ( stereo_type_detect->interpolator > 0 )
    1482             :             {
    1483        5400 :                 if ( l < ( dipole_freq_range[0] ) )
    1484             :                 {
    1485          90 :                     p_proto_buffer[2 * num_freq_bands + 2 * l] = interpolatorSpaced * p_proto_buffer[2 * l] + interpolatorDmx * ( RealBuffer[0][0][l] - RealBuffer[1][0][l] );
    1486          90 :                     p_proto_buffer[2 * num_freq_bands + 2 * l + 1] = interpolatorSpaced * p_proto_buffer[2 * l + 1] + interpolatorDmx * ( ImagBuffer[0][0][l] - ImagBuffer[1][0][l] );
    1487             :                 }
    1488        5310 :                 else if ( l < ( dipole_freq_range[1] ) )
    1489             :                 {
    1490         180 :                     p_proto_buffer[2 * num_freq_bands + 2 * l] = interpolatorSpaced * ( ImagBuffer[0][0][l] - ImagBuffer[1][0][l] ) + interpolatorDmx * ( RealBuffer[0][0][l] - RealBuffer[1][0][l] );
    1491         180 :                     p_proto_buffer[2 * num_freq_bands + 2 * l + 1] = interpolatorSpaced * ( -( RealBuffer[0][0][l] - RealBuffer[1][0][l] ) ) + interpolatorDmx * ( ImagBuffer[0][0][l] - ImagBuffer[1][0][l] );
    1492             :                 }
    1493             :                 else
    1494             :                 {
    1495        5130 :                     p_proto_buffer[2 * num_freq_bands + 2 * l] = interpolatorSpaced * p_proto_buffer[2 * l] + interpolatorDmx * ( RealBuffer[0][0][l] - RealBuffer[1][0][l] );
    1496        5130 :                     p_proto_buffer[2 * num_freq_bands + 2 * l + 1] = interpolatorSpaced * p_proto_buffer[2 * l + 1] + interpolatorDmx * ( ImagBuffer[0][0][l] - ImagBuffer[1][0][l] );
    1497             :                 }
    1498        5400 :                 proto_power_smooth[l + num_freq_bands] += p_proto_buffer[2 * num_freq_bands + 2 * l] * p_proto_buffer[2 * num_freq_bands + 2 * l] + p_proto_buffer[2 * num_freq_bands + 2 * l + 1] * p_proto_buffer[2 * num_freq_bands + 2 * l + 1];
    1499             :             }
    1500     8669040 :             else if ( stereo_type_detect->masa_stereo_type == MASA_STEREO_SPACED_MICS )
    1501             :             {
    1502     1575360 :                 if ( l < ( dipole_freq_range[0] ) ) /* proto = W */
    1503             :                 {
    1504       26256 :                     p_proto_buffer[2 * num_freq_bands + 2 * l] = p_proto_buffer[2 * l];
    1505       26256 :                     p_proto_buffer[2 * num_freq_bands + 2 * l + 1] = p_proto_buffer[2 * l + 1];
    1506       26256 :                     proto_power_smooth[l + num_freq_bands] = proto_power_smooth[l];
    1507             :                 }
    1508     1549104 :                 else if ( l < ( dipole_freq_range[1] ) ) /* proto = -i * (x1-x2) * eq */
    1509             :                 {
    1510       52512 :                     p_proto_buffer[2 * num_freq_bands + 2 * l] = ( ImagBuffer[0][0][l] - ImagBuffer[1][0][l] );
    1511       52512 :                     p_proto_buffer[2 * num_freq_bands + 2 * l + 1] = -( RealBuffer[0][0][l] - RealBuffer[1][0][l] );
    1512       52512 :                     proto_power_smooth[l + num_freq_bands] += p_proto_buffer[2 * num_freq_bands + 2 * l] * p_proto_buffer[2 * num_freq_bands + 2 * l] + p_proto_buffer[2 * num_freq_bands + 2 * l + 1] * p_proto_buffer[2 * num_freq_bands + 2 * l + 1];
    1513             :                 }
    1514             :                 else /* proto = W */
    1515             :                 {
    1516     1496592 :                     p_proto_buffer[2 * num_freq_bands + 2 * l] = p_proto_buffer[2 * l];
    1517     1496592 :                     p_proto_buffer[2 * num_freq_bands + 2 * l + 1] = p_proto_buffer[2 * l + 1];
    1518     1496592 :                     proto_power_smooth[l + num_freq_bands] = proto_power_smooth[l];
    1519             :                 }
    1520             :             }
    1521             :             else
    1522             :             {
    1523     7093680 :                 p_proto_buffer[2 * num_freq_bands + 2 * l] = RealBuffer[0][0][l] - RealBuffer[1][0][l];
    1524     7093680 :                 p_proto_buffer[2 * num_freq_bands + 2 * l + 1] = ImagBuffer[0][0][l] - ImagBuffer[1][0][l];
    1525     7093680 :                 proto_power_smooth[l + num_freq_bands] += p_proto_buffer[2 * num_freq_bands + 2 * l] * p_proto_buffer[2 * num_freq_bands + 2 * l] + p_proto_buffer[2 * num_freq_bands + 2 * l + 1] * p_proto_buffer[2 * num_freq_bands + 2 * l + 1];
    1526             :             }
    1527             : 
    1528             :             /* Compute protos for decorrelation */
    1529     8674440 :             proto_frame_f[2 * l] = Real_aux;
    1530     8674440 :             proto_frame_f[2 * l + 1] = Imag_aux;
    1531     8674440 :             proto_frame_f[2 * num_freq_bands + 2 * l] = RealBuffer[0][0][l];
    1532     8674440 :             proto_frame_f[2 * num_freq_bands + 2 * l + 1] = ImagBuffer[0][0][l];
    1533     8674440 :             proto_frame_f[4 * num_freq_bands + 2 * l] = RealBuffer[1][0][l];
    1534     8674440 :             proto_frame_f[4 * num_freq_bands + 2 * l + 1] = ImagBuffer[1][0][l];
    1535             :         }
    1536             : 
    1537      166581 :         if ( stereo_type_detect->interpolator > 0 )
    1538             :         {
    1539          90 :             stereo_type_detect->interpolator++;
    1540          90 :             if ( stereo_type_detect->interpolator == MASA_STEREO_INTERPOLATION_SLOTS )
    1541             :             {
    1542           6 :                 stereo_type_detect->interpolator = 0;
    1543           6 :                 stereo_type_detect->current_stereo_type = stereo_type_detect->type_change_direction;
    1544             :             }
    1545             :         }
    1546             : 
    1547      166581 :         stereo_type_detect->left_bb_power = a * left_bb_power + b * stereo_type_detect->left_bb_power;
    1548      166581 :         stereo_type_detect->right_bb_power = a * right_bb_power + b * stereo_type_detect->right_bb_power;
    1549      166581 :         stereo_type_detect->total_bb_power = a * total_bb_power + b * stereo_type_detect->total_bb_power;
    1550             : 
    1551      166581 :         lr_bb_power = ( stereo_type_detect->left_bb_power < stereo_type_detect->right_bb_power ) ? stereo_type_detect->left_bb_power : stereo_type_detect->right_bb_power;
    1552      166581 :         lr_bb_power *= 2.0f;
    1553      166581 :         lr_total_bb_ratio = 10.0f * log10f( lr_bb_power / ( stereo_type_detect->total_bb_power + EPSILON ) );
    1554             : 
    1555      166581 :         stereo_type_detect->left_hi_power = a2 * left_hi_power + b2 * stereo_type_detect->left_hi_power;
    1556      166581 :         stereo_type_detect->right_hi_power = a2 * right_hi_power + b2 * stereo_type_detect->right_hi_power;
    1557      166581 :         stereo_type_detect->total_hi_power = a2 * total_hi_power + b2 * stereo_type_detect->total_hi_power;
    1558             : 
    1559      166581 :         lr_hi_power = ( stereo_type_detect->left_hi_power < stereo_type_detect->right_hi_power ) ? stereo_type_detect->left_hi_power : stereo_type_detect->right_hi_power;
    1560      166581 :         lr_hi_power *= 2.0f;
    1561      166581 :         lr_total_hi_ratio = 10.0f * log10f( lr_hi_power / ( stereo_type_detect->total_hi_power + EPSILON ) );
    1562             : 
    1563      166581 :         minimum( sum_total_ratio, min( num_freq_bands, MASA_SUM_FREQ_RANGE_BINS ), &min_sum_total_ratio );
    1564      166581 :         min_sum_total_ratio_db = 10.0f * log10f( min_sum_total_ratio );
    1565             : 
    1566      166581 :         stereo_type_detect->lr_total_bb_ratio_db = lr_total_bb_ratio;
    1567      166581 :         stereo_type_detect->lr_total_hi_ratio_db = lr_total_hi_ratio;
    1568      166581 :         stereo_type_detect->min_sum_total_ratio_db = min_sum_total_ratio_db;
    1569             : 
    1570      166581 :         ivas_masa_stereotype_detection( stereo_type_detect );
    1571             :     }
    1572             :     else
    1573             :     {
    1574       65088 :         p_proto_buffer = proto_direct_buffer_f + slot_index * 2 * num_freq_bands * 2;
    1575             : 
    1576     3682368 :         for ( l = 0; l < num_freq_bands; l++ )
    1577             :         {
    1578     3617280 :             Real_aux = RealBuffer[0][0][l] + RealBuffer[1][0][l];
    1579     3617280 :             Imag_aux = ImagBuffer[0][0][l] + ImagBuffer[1][0][l];
    1580             : 
    1581     3617280 :             reference_power[l] = Real_aux * Real_aux + Imag_aux * Imag_aux;
    1582     3617280 :             proto_power_smooth[l] += reference_power[l];
    1583     3617280 :             p_proto_buffer[2 * l] = Real_aux;
    1584     3617280 :             p_proto_buffer[2 * l + 1] = Imag_aux;
    1585             : 
    1586     3617280 :             p_proto_buffer[2 * num_freq_bands + 2 * l] = RealBuffer[0][0][l] - RealBuffer[1][0][l];
    1587     3617280 :             p_proto_buffer[2 * num_freq_bands + 2 * l + 1] = ImagBuffer[0][0][l] - ImagBuffer[1][0][l];
    1588     3617280 :             proto_power_smooth[l + num_freq_bands] += p_proto_buffer[2 * num_freq_bands + 2 * l] * p_proto_buffer[2 * num_freq_bands + 2 * l] + p_proto_buffer[2 * num_freq_bands + 2 * l + 1] * p_proto_buffer[2 * num_freq_bands + 2 * l + 1];
    1589             : 
    1590     3617280 :             proto_frame_f[2 * l] = Real_aux;
    1591     3617280 :             proto_frame_f[2 * l + 1] = Imag_aux;
    1592             : 
    1593     3617280 :             proto_frame_f[2 * num_freq_bands + 2 * l] = RealBuffer[0][0][l];
    1594     3617280 :             proto_frame_f[2 * num_freq_bands + 2 * l + 1] = ImagBuffer[0][0][l];
    1595     3617280 :             proto_frame_f[4 * num_freq_bands + 2 * l] = RealBuffer[1][0][l];
    1596     3617280 :             proto_frame_f[4 * num_freq_bands + 2 * l + 1] = ImagBuffer[1][0][l];
    1597             :         }
    1598             :     }
    1599             : 
    1600      493176 :     return;
    1601             : }
    1602             : 
    1603             : 
    1604             : /*-------------------------------------------------------------------------
    1605             :  * protoSignalComputation4()
    1606             :  *
    1607             :  *
    1608             :  *-------------------------------------------------------------------------*/
    1609             : 
    1610      167952 : void protoSignalComputation4(
    1611             :     float RealBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
    1612             :     float ImagBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
    1613             :     float *proto_frame_f,
    1614             :     float *proto_direct_buffer_f,
    1615             :     float *reference_power,
    1616             :     float *proto_power_smooth,
    1617             :     const int16_t slot_index,
    1618             :     const int16_t num_outputs_diff,
    1619             :     const int16_t num_freq_bands,
    1620             :     const float *mtx_hoa_decoder,
    1621             :     const int16_t nchan_transport,
    1622             :     const int16_t *sba_map_tc_ind )
    1623             : {
    1624             :     int16_t k, l;
    1625             :     int16_t n;
    1626             :     float sq_tmp;
    1627             :     float *p_proto_buffer;
    1628             : 
    1629      167952 :     set_zero( reference_power, num_freq_bands );
    1630      839760 :     for ( k = 0; k < 4; k++ )
    1631             :     {
    1632    40980288 :         for ( l = 0; l < num_freq_bands; l++ )
    1633             :         {
    1634    40308480 :             sq_tmp = RealBuffer[k][0][l] * RealBuffer[k][0][l] + ImagBuffer[k][0][l] * ImagBuffer[k][0][l];
    1635    40308480 :             reference_power[l] += 0.5f * sq_tmp;
    1636             :         }
    1637             :     }
    1638             : 
    1639             :     /*For decorrelated diffuseness*/
    1640     1775616 :     for ( l = 0; l < num_outputs_diff; l++ )
    1641             :     {
    1642    98067504 :         for ( k = 0; k < num_freq_bands; k++ )
    1643             :         {
    1644    96459840 :             proto_frame_f[2 * l * num_freq_bands + 2 * k] = 0.f;
    1645    96459840 :             proto_frame_f[2 * l * num_freq_bands + 2 * k + 1] = 0.f;
    1646   496238400 :             for ( n = 0; n < nchan_transport; n++ )
    1647             :             {
    1648   399778560 :                 proto_frame_f[2 * l * num_freq_bands + 2 * k] += RealBuffer[n][0][k] * mtx_hoa_decoder[l * 16 + sba_map_tc_ind[n]];
    1649   399778560 :                 proto_frame_f[2 * l * num_freq_bands + 2 * k + 1] += ImagBuffer[n][0][k] * mtx_hoa_decoder[l * 16 + sba_map_tc_ind[n]];
    1650             :             }
    1651             :         }
    1652             :     }
    1653             : 
    1654      167952 :     p_proto_buffer = proto_direct_buffer_f + slot_index * 2 * num_freq_bands * num_outputs_diff;
    1655     1775616 :     for ( k = 0; k < num_outputs_diff; k++ )
    1656             :     {
    1657    98067504 :         for ( l = 0; l < num_freq_bands; l++ )
    1658             :         {
    1659    96459840 :             sq_tmp = proto_frame_f[k * 2 * num_freq_bands + 2 * l] * proto_frame_f[k * 2 * num_freq_bands + 2 * l] + proto_frame_f[k * 2 * num_freq_bands + 2 * l + 1] * proto_frame_f[k * 2 * num_freq_bands + 2 * l + 1];
    1660    96459840 :             proto_power_smooth[l + k * num_freq_bands] += sq_tmp;
    1661    96459840 :             p_proto_buffer[k * 2 * num_freq_bands + 2 * l] = proto_frame_f[k * 2 * num_freq_bands + 2 * l];
    1662    96459840 :             p_proto_buffer[k * 2 * num_freq_bands + 2 * l + 1] = proto_frame_f[k * 2 * num_freq_bands + 2 * l + 1];
    1663             :         }
    1664             :     }
    1665             : 
    1666      167952 :     return;
    1667             : }
    1668             : 
    1669             : 
    1670             : /*-------------------------------------------------------------------------
    1671             :  * ivas_dirac_dec_compute_diffuse_proto()
    1672             :  *
    1673             :  * Compute diffuse prototype buffer and smooth power, only for decorrelated bands
    1674             :  *------------------------------------------------------------------------*/
    1675             : 
    1676      946377 : void ivas_dirac_dec_compute_diffuse_proto(
    1677             :     DIRAC_REND_HANDLE hDirACRend,
    1678             :     const int16_t num_freq_bands,
    1679             :     const int16_t slot_idx /* i  : slot index         */
    1680             : )
    1681             : {
    1682             :     int16_t k, l;
    1683             :     int16_t num_freq_bands_diff;
    1684             :     float *p_diff_buffer, *p_diff_buffer_1;
    1685             :     float *p_proto_diff, *p_power_smooth, *proto_frame_dec_f;
    1686             :     DIRAC_OUTPUT_SYNTHESIS_PARAMS *h_dirac_output_synthesis_params;
    1687             :     DIRAC_OUTPUT_SYNTHESIS_STATE *h_dirac_output_synthesis_state;
    1688             :     int16_t m;
    1689             :     float *p_hoa_enc;
    1690             : 
    1691      946377 :     proto_frame_dec_f = hDirACRend->proto_frame_dec_f;
    1692      946377 :     h_dirac_output_synthesis_params = &( hDirACRend->h_output_synthesis_psd_params );
    1693      946377 :     h_dirac_output_synthesis_state = &( hDirACRend->h_output_synthesis_psd_state );
    1694             : 
    1695      946377 :     num_freq_bands_diff = h_dirac_output_synthesis_params->max_band_decorr;
    1696      946377 :     if ( num_freq_bands_diff == 0 )
    1697             :     {
    1698      167952 :         return;
    1699             :     }
    1700             : 
    1701      778425 :     p_diff_buffer = h_dirac_output_synthesis_state->proto_diffuse_buffer_f + slot_idx * 2 * num_freq_bands_diff * hDirACRend->hOutSetup.nchan_out_woLFE;
    1702      778425 :     p_diff_buffer_1 = p_diff_buffer + 1;
    1703      778425 :     p_power_smooth = h_dirac_output_synthesis_state->proto_power_diff_smooth;
    1704             : 
    1705      778425 :     if ( hDirACRend->synthesisConf != DIRAC_SYNTHESIS_PSD_SHD )
    1706             :     {
    1707     4738080 :         for ( k = 0; k < hDirACRend->hOutSetup.nchan_out_woLFE; k++ )
    1708             :         {
    1709     4144941 :             p_proto_diff = proto_frame_dec_f + k * 2 * num_freq_bands;
    1710    66319056 :             for ( l = 0; l < num_freq_bands_diff; l++ )
    1711             :             {
    1712    62174115 :                 *p_diff_buffer = *( p_proto_diff++ );
    1713    62174115 :                 *p_diff_buffer_1 = *( p_proto_diff++ );
    1714    62174115 :                 *( p_power_smooth++ ) += ( *p_diff_buffer ) * ( *p_diff_buffer ) + ( *p_diff_buffer_1 ) * ( *p_diff_buffer_1 );
    1715    62174115 :                 p_diff_buffer += 2;
    1716    62174115 :                 p_diff_buffer_1 += 2;
    1717             :             }
    1718             :         }
    1719             :     }
    1720             :     else
    1721             :     {
    1722             :         /*DIRAC_SYNTHESIS_PSD_SHD: Virtual LS->HOA encoding*/
    1723     1643454 :         for ( k = 0; k < hDirACRend->hOutSetup.nchan_out_woLFE; k++ )
    1724             :         {
    1725    23330688 :             for ( l = 0; l < num_freq_bands_diff; l++ )
    1726             :             {
    1727    21872520 :                 p_hoa_enc = hDirACRend->hoa_encoder + k;
    1728    21872520 :                 p_proto_diff = proto_frame_dec_f + 2 * l;
    1729             : 
    1730    21872520 :                 *p_diff_buffer = 0.f;
    1731    21872520 :                 *p_diff_buffer_1 = 0.f;
    1732             : 
    1733             :                 /*LS to HOA*/
    1734   196852680 :                 for ( m = 0; m < hDirACRend->num_outputs_diff; m++ )
    1735             :                 {
    1736   174980160 :                     *p_diff_buffer += ( *p_hoa_enc ) * ( *p_proto_diff );
    1737   174980160 :                     *p_diff_buffer_1 += ( *p_hoa_enc ) * ( *( p_proto_diff + 1 ) );
    1738   174980160 :                     p_hoa_enc += hDirACRend->hOutSetup.nchan_out_woLFE;
    1739   174980160 :                     p_proto_diff += 2 * num_freq_bands;
    1740             :                 }
    1741             : 
    1742    21872520 :                 *( p_power_smooth++ ) += ( *p_diff_buffer ) * ( *p_diff_buffer ) + ( *p_diff_buffer_1 ) * ( *p_diff_buffer_1 );
    1743    21872520 :                 p_diff_buffer += 2;
    1744    21872520 :                 p_diff_buffer_1 += 2;
    1745             :             }
    1746             :         }
    1747             :     }
    1748             : 
    1749      778425 :     return;
    1750             : }
    1751             : 
    1752             : 
    1753             : /*-------------------------------------------------------------------------
    1754             :  * computeDirectionAngles()
    1755             :  *
    1756             :  *------------------------------------------------------------------------*/
    1757             : 
    1758     2253249 : void computeDirectionAngles(
    1759             :     float *intensity_real_x,
    1760             :     float *intensity_real_y,
    1761             :     float *intensity_real_z,
    1762             :     const int16_t num_frequency_bands,
    1763             :     int16_t *azimuth,
    1764             :     int16_t *elevation )
    1765             : {
    1766             :     int16_t k;
    1767             :     float intensityNorm;
    1768             :     float x, y, z, radius;
    1769             : 
    1770    27038988 :     for ( k = 0; k < num_frequency_bands; ++k )
    1771             :     {
    1772    24785739 :         intensityNorm = *( intensity_real_x ) * *( intensity_real_x ) +
    1773    24785739 :                         *( intensity_real_y ) * *( intensity_real_y ) +
    1774    24785739 :                         *( intensity_real_z ) * *( intensity_real_z );
    1775             : 
    1776    24785739 :         if ( intensityNorm <= EPSILON )
    1777             :         {
    1778       78456 :             intensityNorm = 1.0f;
    1779       78456 :             x = 1.0f;
    1780       78456 :             y = 0.0f;
    1781       78456 :             z = 0.0f;
    1782       78456 :             intensity_real_x++;
    1783       78456 :             intensity_real_y++;
    1784       78456 :             intensity_real_z++;
    1785             :         }
    1786             :         else
    1787             :         {
    1788    24707283 :             intensityNorm = sqrtf( 1.f / intensityNorm );
    1789    24707283 :             x = *( intensity_real_x++ ) * intensityNorm;
    1790    24707283 :             y = *( intensity_real_y++ ) * intensityNorm;
    1791    24707283 :             z = *( intensity_real_z++ ) * intensityNorm;
    1792             :         }
    1793    24785739 :         radius = sqrtf( x * x + y * y );
    1794    24785739 :         azimuth[k] = (int16_t) ( max( -180.0f, min( 180.0f, atan2f( y, x ) / EVS_PI * 180.0f ) ) + 0.5f );
    1795    24785739 :         elevation[k] = (int16_t) ( max( -90.0f, min( 180.0f, atan2f( z, radius ) / EVS_PI * 180.0f ) ) + 0.5f );
    1796             :     }
    1797             : 
    1798     2253249 :     return;
    1799             : }
    1800             : 
    1801             : 
    1802             : /*-------------------------------------------------------------------------
    1803             :  * ivas_masa_init_stereotype_detection()
    1804             :  *
    1805             :  * Initialize stereo transport signal type detection
    1806             :  *------------------------------------------------------------------------*/
    1807             : 
    1808         424 : void ivas_masa_init_stereotype_detection(
    1809             :     MASA_STEREO_TYPE_DETECT *stereo_type_detect )
    1810             : {
    1811         424 :     stereo_type_detect->masa_stereo_type = MASA_STEREO_DOWNMIX;
    1812         424 :     stereo_type_detect->current_stereo_type = MASA_STEREO_DOWNMIX;
    1813         424 :     stereo_type_detect->type_change_direction = MASA_STEREO_DOWNMIX;
    1814             : 
    1815         424 :     stereo_type_detect->counter = 0;
    1816         424 :     stereo_type_detect->interpolator = 0;
    1817             : 
    1818         424 :     stereo_type_detect->dipole_freq_range[0] = 1;
    1819         424 :     stereo_type_detect->dipole_freq_range[1] = 3;
    1820             : 
    1821         424 :     stereo_type_detect->left_bb_power = 0.0f; /* Broadband estimates */
    1822         424 :     stereo_type_detect->right_bb_power = 0.0f;
    1823         424 :     stereo_type_detect->total_bb_power = 0.0f;
    1824             : 
    1825         424 :     stereo_type_detect->left_hi_power = 0.0f; /* High-frequency estimates */
    1826         424 :     stereo_type_detect->right_hi_power = 0.0f;
    1827         424 :     stereo_type_detect->total_hi_power = 0.0f;
    1828             : 
    1829         424 :     set_zero( stereo_type_detect->sum_power, MASA_SUM_FREQ_RANGE_BINS );
    1830         424 :     set_zero( stereo_type_detect->total_power, MASA_SUM_FREQ_RANGE_BINS );
    1831             : 
    1832         424 :     stereo_type_detect->subtract_power_y = 0.0f;
    1833         424 :     stereo_type_detect->subtract_power_y_smooth = 0.0f;
    1834         424 :     stereo_type_detect->target_power_y_smooth = 0.0f;
    1835             : 
    1836         424 :     stereo_type_detect->lr_total_bb_ratio_db = 0.0f;
    1837         424 :     stereo_type_detect->lr_total_hi_ratio_db = 0.0f;
    1838         424 :     stereo_type_detect->min_sum_total_ratio_db = 0.0f;
    1839         424 :     stereo_type_detect->subtract_target_ratio_db = 0.0f;
    1840             : 
    1841         424 :     return;
    1842             : }
    1843             : 
    1844             : 
    1845             : /*-------------------------------------------------------------------------
    1846             :  * ivas_masa_stereotype_detection()
    1847             :  *
    1848             :  * Detect the type of the transport audio signals
    1849             :  *------------------------------------------------------------------------*/
    1850             : 
    1851      166581 : void ivas_masa_stereotype_detection(
    1852             :     MASA_STEREO_TYPE_DETECT *stereo_type_detect )
    1853             : {
    1854      166581 :     float lr_total_bb_ratio_db = stereo_type_detect->lr_total_bb_ratio_db;
    1855      166581 :     float lr_total_hi_ratio_db = stereo_type_detect->lr_total_hi_ratio_db;
    1856      166581 :     float min_sum_total_ratio_db = stereo_type_detect->min_sum_total_ratio_db;
    1857      166581 :     float subtract_target_ratio_db = stereo_type_detect->subtract_target_ratio_db;
    1858             :     float change_to_spaced;
    1859             :     int16_t change_to_spaced_selection;
    1860             :     float change_to_downmix;
    1861             :     float change_to_downmix2;
    1862             :     int16_t change_to_downmix_selection;
    1863             :     float subtract_temp;
    1864             :     float min_sum_temp;
    1865             :     float lr_total_bb_temp;
    1866             :     float lr_total_hi_temp;
    1867             : 
    1868             :     /* Determine if the determined features match the spaced mic type */
    1869      166581 :     change_to_spaced_selection = 0;
    1870      166581 :     if ( subtract_target_ratio_db < -3.0f )
    1871             :     {
    1872       46150 :         subtract_temp = ( -subtract_target_ratio_db - 3.0f ) / 3.0f;
    1873       46150 :         min_sum_temp = max( -min_sum_total_ratio_db / 6.0f, 0.0f );
    1874       46150 :         lr_total_bb_temp = lr_total_bb_ratio_db / 6.0f;
    1875             : 
    1876       46150 :         change_to_spaced = subtract_temp + min_sum_temp + lr_total_bb_temp;
    1877             : 
    1878       46150 :         if ( change_to_spaced >= 1.0f )
    1879             :         {
    1880       31576 :             change_to_spaced_selection = 1;
    1881             :         }
    1882             :     }
    1883             : 
    1884             :     /* Determine if the determined features match the downmix type, according to a metric */
    1885      166581 :     change_to_downmix_selection = 0;
    1886      166581 :     if ( subtract_target_ratio_db > 0.0f )
    1887             :     {
    1888       77109 :         subtract_temp = subtract_target_ratio_db / 3.0f;
    1889       77109 :         min_sum_temp = ( min_sum_total_ratio_db + 1.0f ) / 6.0f;
    1890       77109 :         lr_total_bb_temp = -lr_total_bb_ratio_db / 6.0f;
    1891             : 
    1892       77109 :         change_to_downmix = subtract_temp + min_sum_temp + lr_total_bb_temp;
    1893             : 
    1894       77109 :         if ( change_to_downmix >= 1.0f )
    1895             :         {
    1896       35001 :             change_to_downmix_selection = 1;
    1897             :         }
    1898             :     }
    1899             : 
    1900             :     /* Determine if the determined features match the downmix type, according to another metric */
    1901      166581 :     if ( lr_total_hi_ratio_db < -12.0f )
    1902             :     {
    1903         960 :         subtract_temp = ( subtract_target_ratio_db + 4.0f ) / 3.0f;
    1904         960 :         min_sum_temp = min_sum_total_ratio_db / 6.0f;
    1905         960 :         lr_total_hi_temp = ( -lr_total_hi_ratio_db - 12.0f ) / 3.0f;
    1906             : 
    1907         960 :         change_to_downmix2 = subtract_temp + min_sum_temp + lr_total_hi_temp;
    1908             : 
    1909         960 :         if ( change_to_downmix2 >= 1.0f )
    1910             :         {
    1911         819 :             change_to_downmix_selection = 1;
    1912             :         }
    1913             :     }
    1914             : 
    1915      166581 :     if ( stereo_type_detect->counter < 400 )
    1916             :     {
    1917       55477 :         stereo_type_detect->counter++;
    1918             :     }
    1919             :     else
    1920             :     {
    1921      111104 :         if ( change_to_spaced_selection == 1 )
    1922             :         {
    1923       26352 :             stereo_type_detect->masa_stereo_type = MASA_STEREO_SPACED_MICS;
    1924             :         }
    1925       84752 :         else if ( change_to_downmix_selection == 1 )
    1926             :         {
    1927       19863 :             stereo_type_detect->masa_stereo_type = MASA_STEREO_DOWNMIX;
    1928             :         }
    1929             :     }
    1930             : 
    1931      166581 :     if ( stereo_type_detect->interpolator == 0 )
    1932             :     {
    1933      166497 :         if ( stereo_type_detect->current_stereo_type != stereo_type_detect->masa_stereo_type )
    1934             :         {
    1935           6 :             stereo_type_detect->interpolator = 1;
    1936           6 :             stereo_type_detect->type_change_direction = stereo_type_detect->masa_stereo_type;
    1937             :         }
    1938             :     }
    1939             : 
    1940      166581 :     return;
    1941             : }
    1942             : 
    1943             : 
    1944             : /*-------------------------------------------------------------------------
    1945             :  * computeIntensityVector_dec()
    1946             :  *
    1947             :  *
    1948             :  *------------------------------------------------------------------------*/
    1949             : 
    1950     2253249 : void computeIntensityVector_dec(
    1951             :     float Cldfb_RealBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
    1952             :     float Cldfb_ImagBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
    1953             :     const int16_t num_frequency_bands,
    1954             :     float *intensity_real_x,
    1955             :     float *intensity_real_y,
    1956             :     float *intensity_real_z )
    1957             : {
    1958             :     /*
    1959             :      * W = a + ib; Y = c + id
    1960             :      * real(W*Y') = ac + bd
    1961             :      */
    1962             :     int16_t i;
    1963             :     float real, img;
    1964             : 
    1965    27038988 :     for ( i = 0; i < num_frequency_bands; ++i )
    1966             :     {
    1967    24785739 :         real = Cldfb_RealBuffer[0][0][i];
    1968    24785739 :         img = Cldfb_ImagBuffer[0][0][i];
    1969    24785739 :         intensity_real_x[i] = Cldfb_RealBuffer[3][0][i] * real + Cldfb_ImagBuffer[3][0][i] * img;
    1970    24785739 :         intensity_real_y[i] = Cldfb_RealBuffer[1][0][i] * real + Cldfb_ImagBuffer[1][0][i] * img;
    1971    24785739 :         intensity_real_z[i] = Cldfb_RealBuffer[2][0][i] * real + Cldfb_ImagBuffer[2][0][i] * img;
    1972             :     }
    1973             : 
    1974     2253249 :     return;
    1975             : }
    1976             : 
    1977             : 
    1978             : /*-------------------------------------------------------------------------
    1979             :  * ivas_lfe_synth_with_cldfb()
    1980             :  *
    1981             :  *
    1982             :  *------------------------------------------------------------------------*/
    1983             : 
    1984       79680 : void ivas_lfe_synth_with_cldfb(
    1985             :     MCMASA_LFE_SYNTH_DATA_HANDLE hMasaLfeSynth,
    1986             :     float RealBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
    1987             :     float ImagBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
    1988             :     float RealBufferLfe[MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
    1989             :     float ImagBufferLfe[MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
    1990             :     const int16_t slot_index,
    1991             :     const int16_t subframe_index,
    1992             :     const int16_t nchan_transport )
    1993             : {
    1994             :     float lfeGain;
    1995             :     float transportGain;
    1996             :     float protoLfeReal, protoLfeImag;
    1997             :     int16_t i;
    1998             :     float transportEne, protoLfeEne, targetEneLfe, targetEneTrans;
    1999             : 
    2000       79680 :     set_zero( RealBufferLfe[slot_index], CLDFB_NO_CHANNELS_MAX );
    2001       79680 :     set_zero( ImagBufferLfe[slot_index], CLDFB_NO_CHANNELS_MAX );
    2002             : 
    2003       79680 :     protoLfeReal = RealBuffer[0][0][0];
    2004       79680 :     protoLfeImag = ImagBuffer[0][0][0];
    2005       79680 :     transportEne = RealBuffer[0][0][0] * RealBuffer[0][0][0] + ImagBuffer[0][0][0] * ImagBuffer[0][0][0];
    2006       83040 :     for ( i = 1; i < nchan_transport; i++ )
    2007             :     {
    2008        3360 :         protoLfeReal += RealBuffer[i][0][0];
    2009        3360 :         protoLfeImag += ImagBuffer[i][0][0];
    2010        3360 :         transportEne += RealBuffer[i][0][0] * RealBuffer[i][0][0] + ImagBuffer[i][0][0] * ImagBuffer[i][0][0];
    2011             :     }
    2012       79680 :     protoLfeEne = protoLfeReal * protoLfeReal + protoLfeImag * protoLfeImag;
    2013             : 
    2014       79680 :     targetEneLfe = transportEne * hMasaLfeSynth->lfeToTotalEnergyRatio[subframe_index];
    2015       79680 :     targetEneTrans = transportEne * max( ( 1.0f - hMasaLfeSynth->lfeToTotalEnergyRatio[subframe_index] ), 0.01f );
    2016             : 
    2017       79680 :     hMasaLfeSynth->transportEneSmooth *= MCMASA_LFE_SYNTH_ALPHA;
    2018       79680 :     hMasaLfeSynth->protoLfeEneSmooth *= MCMASA_LFE_SYNTH_ALPHA;
    2019       79680 :     hMasaLfeSynth->targetEneLfeSmooth *= MCMASA_LFE_SYNTH_ALPHA;
    2020       79680 :     hMasaLfeSynth->targetEneTransSmooth *= MCMASA_LFE_SYNTH_ALPHA;
    2021             : 
    2022       79680 :     hMasaLfeSynth->transportEneSmooth += transportEne;
    2023       79680 :     hMasaLfeSynth->protoLfeEneSmooth += protoLfeEne;
    2024       79680 :     hMasaLfeSynth->targetEneLfeSmooth += targetEneLfe;
    2025       79680 :     hMasaLfeSynth->targetEneTransSmooth += targetEneTrans;
    2026             : 
    2027       79680 :     lfeGain = min( 1.0f, sqrtf( hMasaLfeSynth->targetEneLfeSmooth / ( EPSILON + hMasaLfeSynth->protoLfeEneSmooth ) ) );
    2028       79680 :     transportGain = min( 1.0f, sqrtf( hMasaLfeSynth->targetEneTransSmooth / ( EPSILON + hMasaLfeSynth->transportEneSmooth ) ) );
    2029             : 
    2030       79680 :     RealBufferLfe[slot_index][0] = protoLfeReal * lfeGain;
    2031       79680 :     ImagBufferLfe[slot_index][0] = protoLfeImag * lfeGain;
    2032             : 
    2033       79680 :     RealBuffer[0][0][0] *= transportGain;
    2034       79680 :     ImagBuffer[0][0][0] *= transportGain;
    2035       83040 :     for ( i = 1; i < nchan_transport; i++ )
    2036             :     {
    2037        3360 :         RealBuffer[i][0][0] *= transportGain;
    2038        3360 :         ImagBuffer[i][0][0] *= transportGain;
    2039             :     }
    2040             : 
    2041       79680 :     return;
    2042             : }
    2043             : 
    2044             : 
    2045             : /*-------------------------------------------------------------------------
    2046             :  * rotateAziEle_DirAC()
    2047             :  *
    2048             :  * Apply rotation to DirAC DOAs
    2049             :  *------------------------------------------------------------------------*/
    2050             : 
    2051      441600 : void rotateAziEle_DirAC(
    2052             :     int16_t *azi,        /* i/o: array of azimuth values               */
    2053             :     int16_t *ele,        /* i/o: array of elevation values             */
    2054             :     const int16_t band1, /* i  : bands to work on (lower limit)        */
    2055             :     const int16_t band2, /* i  : bands to work on (upper bound)        */
    2056             :     const float *p_Rmat  /* i  : pointer to real-space rotation matrix */
    2057             : )
    2058             : {
    2059             :     int16_t b;
    2060             :     float dv_0, dv_1, dv_2;
    2061             :     float dv_r_0, dv_r_1, dv_r_2;
    2062             :     float w;
    2063             : 
    2064      441600 :     push_wmops( "rotateAziEle_DirAC" );
    2065             : 
    2066    12480000 :     for ( b = band1; b < band2; b++ )
    2067             :     {
    2068             : 
    2069             :         /*Conversion spherical to cartesian coordinates*/
    2070    12038400 :         w = cosf( ele[b] * PI_OVER_180 );
    2071    12038400 :         dv_0 = w * cosf( azi[b] * PI_OVER_180 );
    2072    12038400 :         dv_1 = w * sinf( azi[b] * PI_OVER_180 );
    2073    12038400 :         dv_2 = sinf( ele[b] * PI_OVER_180 );
    2074             : 
    2075    12038400 :         dv_r_0 = p_Rmat[0] * dv_0 + p_Rmat[1] * dv_1 + p_Rmat[2] * dv_2;
    2076    12038400 :         dv_r_1 = p_Rmat[3] * dv_0 + p_Rmat[4] * dv_1 + p_Rmat[5] * dv_2;
    2077    12038400 :         dv_r_2 = p_Rmat[6] * dv_0 + p_Rmat[7] * dv_1 + p_Rmat[8] * dv_2;
    2078             : 
    2079             :         /*Conversion spherical to cartesian coordinates*/
    2080    12038400 :         azi[b] = (int16_t) ( atan2f( dv_r_1, dv_r_0 ) * _180_OVER_PI );
    2081    12038400 :         ele[b] = (int16_t) ( atan2f( dv_r_2, sqrtf( dv_r_0 * dv_r_0 + dv_r_1 * dv_r_1 ) ) * _180_OVER_PI );
    2082             :     }
    2083             : 
    2084      441600 :     pop_wmops();
    2085             : 
    2086      441600 :     return;
    2087             : }
    2088             : 
    2089             : /* A reduced rewrite of the corresponding decoder side function */
    2090       40800 : static void ivas_masa_ext_dirac_render_sf(
    2091             :     MASA_EXT_REND_HANDLE hMasaExtRend, /* i/o: IVAS decoder structure                                  */
    2092             :     float *output_f[]                  /* i/o: synthesized core-coder transport channels/DirAC output  */
    2093             : )
    2094             : {
    2095             :     int16_t i, ch, idx_in, idx_lfe;
    2096             :     DIRAC_REND_HANDLE hDirACRend;
    2097             :     float dirEne;
    2098             :     float surCohEner;
    2099             :     float surCohRatio[CLDFB_NO_CHANNELS_MAX];
    2100             :     int16_t subframe_idx;
    2101             :     int16_t slot_idx, index_slot;
    2102             :     int16_t slot_idx_start, slot_idx_start_cldfb_synth, md_idx;
    2103             :     int16_t nchan_transport;
    2104             :     int16_t masa_band_mapping[MASA_FREQUENCY_BANDS + 1];
    2105             : 
    2106             :     /* CLDFB: last output channels reserved to LFT for CICPx */
    2107             :     float Cldfb_RealBuffer[MAX_OUTPUT_CHANNELS][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX];
    2108             :     float Cldfb_ImagBuffer[MAX_OUTPUT_CHANNELS][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX];
    2109             : 
    2110             :     /* local copies of azi, ele, diffuseness */
    2111             :     int16_t azimuth[CLDFB_NO_CHANNELS_MAX];
    2112             :     int16_t elevation[CLDFB_NO_CHANNELS_MAX];
    2113             :     float diffuseness_vector[CLDFB_NO_CHANNELS_MAX];
    2114             : 
    2115             :     DIRAC_DEC_STACK_MEM DirAC_mem;
    2116             :     float *reference_power, *reference_power_smooth;
    2117       40800 :     float *onset_filter, *onset_filter_subframe, *p_onset_filter = NULL;
    2118             :     uint16_t coherence_flag;
    2119             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
    2120             : 
    2121       40800 :     push_wmops( "ivas_masa_ext_dirac_render_sf" );
    2122             : 
    2123             :     /* Initialize aux buffers */
    2124       40800 :     hDirACRend = hMasaExtRend->hDirACRend;
    2125       40800 :     hSpatParamRendCom = hMasaExtRend->hSpatParamRendCom;
    2126       40800 :     nchan_transport = hMasaExtRend->nchan_input;
    2127             : 
    2128       40800 :     DirAC_mem = hDirACRend->stack_mem;
    2129             : 
    2130       40800 :     reference_power = DirAC_mem.reference_power;
    2131       40800 :     reference_power_smooth = ( DirAC_mem.reference_power == NULL ) ? NULL : DirAC_mem.reference_power + hSpatParamRendCom->num_freq_bands;
    2132       40800 :     onset_filter = DirAC_mem.onset_filter;
    2133       40800 :     onset_filter_subframe = ( DirAC_mem.onset_filter == NULL ) ? NULL : DirAC_mem.onset_filter + hSpatParamRendCom->num_freq_bands;
    2134       40800 :     coherence_flag = 1; /* There is always coherence assumed for ext rend of MASA */
    2135             : 
    2136             :     /* Construct default MASA band mapping */
    2137     1060800 :     for ( i = 0; i < MASA_FREQUENCY_BANDS + 1; i++ )
    2138             :     {
    2139     1020000 :         masa_band_mapping[i] = i;
    2140             :     }
    2141             : 
    2142             :     /* Subframe loop */
    2143       40800 :     slot_idx_start = hSpatParamRendCom->slots_rendered;
    2144       40800 :     slot_idx_start_cldfb_synth = 0;
    2145             : 
    2146       40800 :     subframe_idx = hSpatParamRendCom->subframes_rendered;
    2147       40800 :     md_idx = hSpatParamRendCom->render_to_md_map[subframe_idx];
    2148             : 
    2149             :     /* copy parameters into local buffers*/
    2150       40800 :     mvs2s( hSpatParamRendCom->azimuth[hSpatParamRendCom->render_to_md_map[subframe_idx]], azimuth, hSpatParamRendCom->num_freq_bands );
    2151       40800 :     mvs2s( hSpatParamRendCom->elevation[hSpatParamRendCom->render_to_md_map[subframe_idx]], elevation, hSpatParamRendCom->num_freq_bands );
    2152       40800 :     mvr2r( hSpatParamRendCom->diffuseness_vector[hSpatParamRendCom->render_to_md_map[subframe_idx]], diffuseness_vector, hSpatParamRendCom->num_freq_bands );
    2153             : 
    2154       40800 :     if ( hDirACRend->synthesisConf != DIRAC_SYNTHESIS_GAIN_SHD )
    2155             :     {
    2156       33600 :         set_zero( reference_power_smooth, hSpatParamRendCom->num_freq_bands );
    2157             :     }
    2158             :     else
    2159             :     {
    2160        7200 :         set_zero( onset_filter_subframe, hSpatParamRendCom->num_freq_bands );
    2161             :     }
    2162             : 
    2163             :     /* compute response */
    2164       40800 :     if ( hDirACRend->synthesisConf != DIRAC_SYNTHESIS_GAIN_SHD )
    2165             :     {
    2166       33600 :         ivas_dirac_dec_compute_power_factors( hSpatParamRendCom->num_freq_bands,
    2167             :                                               diffuseness_vector,
    2168       33600 :                                               hDirACRend->h_output_synthesis_psd_params.max_band_decorr,
    2169             :                                               hDirACRend->h_output_synthesis_psd_state.direct_power_factor,
    2170             :                                               hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor );
    2171             : 
    2172       33600 :         if ( coherence_flag )
    2173             :         {
    2174     2049600 :             for ( i = 0; i < hSpatParamRendCom->num_freq_bands; i++ )
    2175             :             {
    2176     2016000 :                 dirEne = hDirACRend->h_output_synthesis_psd_state.direct_power_factor[i];
    2177     2016000 :                 surCohEner = hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor[i] * hSpatParamRendCom->surroundingCoherence[md_idx][i];
    2178     2016000 :                 hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor[i] -= surCohEner;
    2179     2016000 :                 hDirACRend->h_output_synthesis_psd_state.direct_power_factor[i] += surCohEner;
    2180             : 
    2181     2016000 :                 surCohRatio[i] = surCohEner / ( 1e-12f + dirEne + surCohEner );
    2182             :             }
    2183             :         }
    2184             :         else
    2185             :         {
    2186           0 :             set_zero( surCohRatio, hSpatParamRendCom->num_freq_bands );
    2187             :         }
    2188             :     }
    2189             :     else
    2190             :     {
    2191        7200 :         ivas_dirac_dec_compute_gain_factors( hSpatParamRendCom->num_freq_bands,
    2192        7200 :                                              hSpatParamRendCom->diffuseness_vector[md_idx],
    2193        7200 :                                              hDirACRend->h_output_synthesis_psd_params.max_band_decorr,
    2194             :                                              hDirACRend->h_output_synthesis_psd_state.direct_power_factor,
    2195             :                                              hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor );
    2196             : 
    2197        7200 :         if ( coherence_flag )
    2198             :         {
    2199      439200 :             for ( i = 0; i < hSpatParamRendCom->num_freq_bands; i++ )
    2200             :             {
    2201      432000 :                 surCohRatio[i] = hSpatParamRendCom->surroundingCoherence[md_idx][i];
    2202             :             }
    2203             :         }
    2204             :         else
    2205             :         {
    2206           0 :             set_zero( surCohRatio, hSpatParamRendCom->num_freq_bands );
    2207             :         }
    2208             :     }
    2209             : 
    2210       40800 :     ivas_dirac_dec_compute_directional_responses( hSpatParamRendCom,
    2211             :                                                   hDirACRend,
    2212             :                                                   hMasaExtRend->hVBAPdata,
    2213             :                                                   masa_band_mapping,
    2214             :                                                   NULL,
    2215             :                                                   azimuth,
    2216             :                                                   elevation,
    2217             :                                                   md_idx,
    2218             :                                                   surCohRatio,
    2219             :                                                   0 );
    2220             : 
    2221             : 
    2222      204000 :     for ( slot_idx = 0; slot_idx < hSpatParamRendCom->subframe_nbslots[subframe_idx]; slot_idx++ )
    2223             :     {
    2224      163200 :         index_slot = slot_idx_start + slot_idx;
    2225      163200 :         md_idx = hSpatParamRendCom->render_to_md_map[subframe_idx];
    2226             : 
    2227             :         /* CLDFB Analysis*/
    2228      412800 :         for ( ch = 0; ch < nchan_transport; ch++ )
    2229             :         {
    2230      249600 :             cldfbAnalysis_ts( &( output_f[ch][hSpatParamRendCom->num_freq_bands * index_slot] ),
    2231      249600 :                               Cldfb_RealBuffer[ch][0],
    2232      249600 :                               Cldfb_ImagBuffer[ch][0],
    2233      249600 :                               hSpatParamRendCom->num_freq_bands,
    2234             :                               hMasaExtRend->cldfbAnaRend[ch] );
    2235             :         }
    2236             : 
    2237             : 
    2238      163200 :         if ( nchan_transport == 1 )
    2239             :         {
    2240             :             /* Need to set second CLDFB channel to zero as further processing assumes CNA content in it */
    2241       76800 :             set_zero( Cldfb_RealBuffer[1][0], hSpatParamRendCom->num_freq_bands );
    2242       76800 :             set_zero( Cldfb_ImagBuffer[1][0], hSpatParamRendCom->num_freq_bands );
    2243             :         }
    2244             : 
    2245             :         /*-----------------------------------------------------------------*
    2246             :          * prototype signal computation
    2247             :          *-----------------------------------------------------------------*/
    2248             : 
    2249      163200 :         if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
    2250             :         {
    2251       28800 :             protoSignalComputation_shd( Cldfb_RealBuffer, Cldfb_ImagBuffer,
    2252             :                                         hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f,
    2253             :                                         hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f,
    2254             :                                         reference_power, slot_idx, nchan_transport,
    2255       28800 :                                         hDirACRend->num_outputs_diff,
    2256       28800 :                                         hSpatParamRendCom->num_freq_bands,
    2257             :                                         0 );
    2258             :         }
    2259      134400 :         else if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_MONO )
    2260             :         {
    2261        9600 :             protoSignalComputation2( Cldfb_RealBuffer, Cldfb_ImagBuffer, hDirACRend->proto_frame_f,
    2262             :                                      hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f,
    2263             :                                      reference_power, hDirACRend->h_output_synthesis_psd_state.proto_power_smooth,
    2264        9600 :                                      0, slot_idx, hSpatParamRendCom->num_freq_bands, hDirACRend->masa_stereo_type_detect );
    2265             :         }
    2266             :         else
    2267             :         {
    2268      124800 :             switch ( nchan_transport )
    2269             :             {
    2270       76800 :                 case 2:
    2271       76800 :                     protoSignalComputation2( Cldfb_RealBuffer, Cldfb_ImagBuffer,
    2272             :                                              hDirACRend->proto_frame_f,
    2273             :                                              hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f,
    2274             :                                              reference_power,
    2275             :                                              hDirACRend->h_output_synthesis_psd_state.proto_power_smooth,
    2276       76800 :                                              hDirACRend->hOutSetup.is_loudspeaker_setup,
    2277             :                                              slot_idx,
    2278       76800 :                                              hSpatParamRendCom->num_freq_bands,
    2279             :                                              hDirACRend->masa_stereo_type_detect );
    2280       76800 :                     break;
    2281       48000 :                 case 1:
    2282       48000 :                     protoSignalComputation1( Cldfb_RealBuffer, Cldfb_ImagBuffer,
    2283             :                                              hDirACRend->proto_frame_f,
    2284             :                                              hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f,
    2285             :                                              reference_power,
    2286             :                                              hDirACRend->h_output_synthesis_psd_state.proto_power_smooth,
    2287             :                                              slot_idx,
    2288       48000 :                                              hDirACRend->num_protos_diff,
    2289       48000 :                                              hSpatParamRendCom->num_freq_bands );
    2290       48000 :                     break;
    2291           0 :                 default:
    2292           0 :                     return;
    2293             :             }
    2294             :         }
    2295             : 
    2296             :         /*-----------------------------------------------------------------*
    2297             :          * frequency domain decorrelation
    2298             :          *-----------------------------------------------------------------*/
    2299             : 
    2300      163200 :         if ( hDirACRend->proto_signal_decorr_on == 1 )
    2301             :         {
    2302             :             /* decorrelate prototype frame */
    2303      153600 :             if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
    2304             :             {
    2305       28800 :                 ivas_dirac_dec_decorr_process( hSpatParamRendCom->num_freq_bands,
    2306       28800 :                                                hDirACRend->num_outputs_diff,
    2307       28800 :                                                hDirACRend->num_protos_diff,
    2308             :                                                hDirACRend->synthesisConf,
    2309             :                                                nchan_transport,
    2310       28800 :                                                hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f + slot_idx * 2 * hSpatParamRendCom->num_freq_bands * hDirACRend->num_outputs_diff,
    2311       28800 :                                                hDirACRend->num_protos_diff,
    2312       28800 :                                                hDirACRend->proto_index_diff,
    2313       28800 :                                                hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f + slot_idx * 2 * hSpatParamRendCom->num_freq_bands * hDirACRend->num_outputs_diff + 2 * hSpatParamRendCom->num_freq_bands * min( 4, nchan_transport ),
    2314             :                                                onset_filter,
    2315             :                                                hDirACRend->h_freq_domain_decorr_ap_params,
    2316             :                                                hDirACRend->h_freq_domain_decorr_ap_state );
    2317             : 
    2318       28800 :                 v_multc( onset_filter, 0.25f, onset_filter, hSpatParamRendCom->num_freq_bands );
    2319       28800 :                 v_add( onset_filter, onset_filter_subframe, onset_filter_subframe, hSpatParamRendCom->num_freq_bands );
    2320       28800 :                 p_onset_filter = onset_filter_subframe;
    2321             :             }
    2322             :             else
    2323             :             {
    2324      124800 :                 ivas_dirac_dec_decorr_process( hSpatParamRendCom->num_freq_bands,
    2325      124800 :                                                hDirACRend->num_outputs_diff,
    2326      124800 :                                                hDirACRend->num_protos_diff,
    2327             :                                                hDirACRend->synthesisConf,
    2328             :                                                nchan_transport,
    2329      124800 :                                                hDirACRend->proto_frame_f,
    2330      124800 :                                                hDirACRend->num_protos_diff,
    2331      124800 :                                                hDirACRend->proto_index_diff,
    2332             :                                                DirAC_mem.frame_dec_f,
    2333             :                                                onset_filter,
    2334             :                                                hDirACRend->h_freq_domain_decorr_ap_params,
    2335             :                                                hDirACRend->h_freq_domain_decorr_ap_state );
    2336             : 
    2337      124800 :                 hDirACRend->proto_frame_dec_f = DirAC_mem.frame_dec_f;
    2338      124800 :                 p_onset_filter = onset_filter;
    2339             :             }
    2340             :         }
    2341             :         else
    2342             :         {
    2343        9600 :             if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
    2344             :             {
    2345           0 :                 set_f( onset_filter_subframe, 1.f, hSpatParamRendCom->num_freq_bands );
    2346           0 :                 p_onset_filter = onset_filter_subframe;
    2347             :             }
    2348             :             else
    2349             :             {
    2350             :                 /* no frequency domain decorrelation: use prototype frame */
    2351        9600 :                 hDirACRend->proto_frame_dec_f = hDirACRend->proto_frame_f;
    2352        9600 :                 p_onset_filter = NULL;
    2353             :             }
    2354             :         }
    2355             : 
    2356             :         /*-----------------------------------------------------------------*
    2357             :          * output synthesis
    2358             :          *-----------------------------------------------------------------*/
    2359             : 
    2360      163200 :         if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_LS || hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_SHD )
    2361             :         {
    2362             :             /* Compute diffuse prototypes */
    2363      124800 :             ivas_dirac_dec_compute_diffuse_proto( hDirACRend, hSpatParamRendCom->num_freq_bands, slot_idx );
    2364             :         }
    2365             : 
    2366      163200 :         ivas_dirac_dec_output_synthesis_process_slot( reference_power,
    2367             :                                                       p_onset_filter,
    2368             :                                                       azimuth,
    2369             :                                                       elevation,
    2370      163200 :                                                       hSpatParamRendCom->diffuseness_vector[md_idx],
    2371             :                                                       hSpatParamRendCom,
    2372             :                                                       hDirACRend,
    2373             :                                                       hMasaExtRend->hVBAPdata,
    2374             :                                                       hDirACRend->hOutSetup,
    2375             :                                                       nchan_transport,
    2376             :                                                       md_idx,
    2377             :                                                       0,
    2378             :                                                       0 );
    2379             : 
    2380      163200 :         if ( hDirACRend->synthesisConf != DIRAC_SYNTHESIS_GAIN_SHD )
    2381             :         {
    2382      134400 :             v_add( reference_power, reference_power_smooth, reference_power_smooth, hSpatParamRendCom->num_freq_bands );
    2383             :         }
    2384             :     }
    2385             : 
    2386       40800 :     ivas_dirac_dec_output_synthesis_get_interpolator( &hDirACRend->h_output_synthesis_psd_params, hSpatParamRendCom->subframe_nbslots[subframe_idx] );
    2387             : 
    2388             : 
    2389       40800 :     if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
    2390             :     {
    2391        7200 :         ivas_dirac_dec_output_synthesis_process_subframe_gain_shd( Cldfb_RealBuffer,
    2392             :                                                                    Cldfb_ImagBuffer,
    2393             :                                                                    hSpatParamRendCom,
    2394             :                                                                    hDirACRend,
    2395             :                                                                    nchan_transport,
    2396        7200 :                                                                    hSpatParamRendCom->subframe_nbslots[subframe_idx],
    2397             :                                                                    p_onset_filter,
    2398             :                                                                    diffuseness_vector,
    2399             :                                                                    0,
    2400             :                                                                    0 );
    2401             :     }
    2402             :     else
    2403             :     {
    2404       33600 :         ivas_dirac_dec_output_synthesis_process_subframe_psd_ls( Cldfb_RealBuffer,
    2405             :                                                                  Cldfb_ImagBuffer,
    2406             :                                                                  hSpatParamRendCom,
    2407             :                                                                  hDirACRend,
    2408       33600 :                                                                  hSpatParamRendCom->subframe_nbslots[subframe_idx],
    2409             :                                                                  diffuseness_vector,
    2410             :                                                                  reference_power_smooth,
    2411             :                                                                  1.0f,
    2412             :                                                                  0 );
    2413             :     }
    2414             : 
    2415             :     /*-----------------------------------------------------------------*
    2416             :      * CLDFB synthesis (and binaural rendering)
    2417             :      *-----------------------------------------------------------------*/
    2418             : 
    2419       40800 :     index_slot = slot_idx_start_cldfb_synth;
    2420             : 
    2421             :     {
    2422             :         float *RealBuffer[MAX_PARAM_SPATIAL_SUBFRAMES];
    2423             :         float *ImagBuffer[MAX_PARAM_SPATIAL_SUBFRAMES];
    2424             :         int16_t outchannels;
    2425             : 
    2426       40800 :         idx_in = 0;
    2427       40800 :         idx_lfe = 0;
    2428             : 
    2429       40800 :         outchannels = hDirACRend->hOutSetup.nchan_out_woLFE + hDirACRend->hOutSetup.num_lfe;
    2430             : 
    2431             :         /* Note here that compared to decoder path, there is no separate channel ever for MASA ext rend path */
    2432             : 
    2433      393600 :         for ( ch = 0; ch < outchannels; ch++ )
    2434             :         {
    2435      352800 :             if ( ( hDirACRend->hOutSetup.num_lfe > 0 ) && ( hDirACRend->hOutSetup.index_lfe[idx_lfe] == ch ) )
    2436             :             {
    2437             :                 /* No LFE for MASA rendering */
    2438       24000 :                 set_zero( &( output_f[ch][index_slot * hSpatParamRendCom->num_freq_bands] ), hSpatParamRendCom->subframe_nbslots[subframe_idx] * hSpatParamRendCom->num_freq_bands );
    2439             : 
    2440       24000 :                 if ( idx_lfe < ( hDirACRend->hOutSetup.num_lfe - 1 ) )
    2441             :                 {
    2442           0 :                     idx_lfe++;
    2443             :                 }
    2444             :             }
    2445             :             else
    2446             :             {
    2447             :                 /* open CLDFB buffer up to CLDFB_NO_CHANNELS_MAX bands for 48kHz */
    2448     1644000 :                 for ( i = 0; i < hSpatParamRendCom->subframe_nbslots[subframe_idx]; i++ )
    2449             :                 {
    2450     1315200 :                     RealBuffer[i] = Cldfb_RealBuffer[idx_in][i];
    2451     1315200 :                     ImagBuffer[i] = Cldfb_ImagBuffer[idx_in][i];
    2452             :                 }
    2453      328800 :                 cldfbSynthesis( RealBuffer, ImagBuffer, &( output_f[ch][index_slot * hSpatParamRendCom->num_freq_bands] ), hSpatParamRendCom->num_freq_bands * hSpatParamRendCom->subframe_nbslots[subframe_idx], hMasaExtRend->cldfbSynRend[idx_in] );
    2454      328800 :                 idx_in++;
    2455             :             }
    2456             :         }
    2457             :     }
    2458       40800 :     hSpatParamRendCom->slots_rendered += hSpatParamRendCom->subframe_nbslots[subframe_idx];
    2459       40800 :     hSpatParamRendCom->subframes_rendered++;
    2460             : 
    2461       40800 :     pop_wmops();
    2462             : 
    2463       40800 :     return;
    2464             : }
    2465             : 
    2466       25500 : void ivas_masa_ext_dirac_render(
    2467             :     MASA_EXT_REND_HANDLE hMasaExtRend, /* i/o: MASA renderer structure             */
    2468             :     float *output_f[],                 /* i/o: input/output signals in time domain */
    2469             :     const int16_t num_subframes        /* i  : number of subframes to render       */
    2470             : )
    2471             : {
    2472             :     int16_t subframe_idx;
    2473             :     float *output_f_local[MAX_OUTPUT_CHANNELS];
    2474             :     int16_t n, n_samples_sf;
    2475             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
    2476             : 
    2477       25500 :     hSpatParamRendCom = hMasaExtRend->hSpatParamRendCom;
    2478             : 
    2479       25500 :     n_samples_sf = JBM_CLDFB_SLOTS_IN_SUBFRAME * hSpatParamRendCom->slot_size;
    2480             : 
    2481      433500 :     for ( n = 0; n < MAX_OUTPUT_CHANNELS; n++ )
    2482             :     {
    2483      408000 :         output_f_local[n] = output_f[n];
    2484             :     }
    2485             : 
    2486       25500 :     hSpatParamRendCom->subframes_rendered = hSpatParamRendCom->dirac_read_idx;
    2487             : 
    2488       66300 :     for ( subframe_idx = 0; subframe_idx < num_subframes; subframe_idx++ )
    2489             :     {
    2490       40800 :         hSpatParamRendCom->slots_rendered = 0;
    2491       40800 :         ivas_masa_ext_dirac_render_sf( hMasaExtRend, output_f_local );
    2492      693600 :         for ( n = 0; n < MAX_OUTPUT_CHANNELS; n++ )
    2493             :         {
    2494      652800 :             output_f_local[n] += n_samples_sf;
    2495             :         }
    2496             : 
    2497       40800 :         hSpatParamRendCom->dirac_read_idx = ( hSpatParamRendCom->dirac_read_idx + 1 ) % hSpatParamRendCom->dirac_md_buffer_length;
    2498             :     }
    2499             : 
    2500       25500 :     return;
    2501             : }

Generated by: LCOV version 1.14