LCOV - code coverage report
Current view: top level - lib_dec - ivas_dirac_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- merged total coverage @ a21f94bc6bac334fe001a5bad2f7b32b79038097 Lines: 849 949 89.5 %
Date: 2025-11-01 08:50:45 Functions: 11 11 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_com.h"
      43             : #include "ivas_rom_dec.h"
      44             : #include "ivas_rom_rend.h"
      45             : #ifdef DEBUGGING
      46             : #include "debug.h"
      47             : #endif
      48             : #include "wmc_auto.h"
      49             : 
      50             : 
      51             : /*-----------------------------------------------------------------------*
      52             :  * Local function prototypes
      53             :  *-----------------------------------------------------------------------*/
      54             : 
      55      346833 : static ivas_error ivas_dirac_dec_config_internal(
      56             :     Decoder_Struct *st_ivas,                /* i/o: IVAS decoder structure  */
      57             :     const DIRAC_CONFIG_FLAG flag_config_inp /* i/ : Flag determining if we open or reconfigure the DirAC decoder */
      58             : )
      59             : {
      60             :     DIRAC_DEC_HANDLE hDirAC;
      61             :     ivas_error error;
      62             :     DIRAC_CONFIG_FLAG flag_config;
      63             : 
      64      346833 :     flag_config = ( flag_config_inp == DIRAC_RECONFIGURE_MODE ) ? DIRAC_RECONFIGURE : flag_config_inp;
      65      346833 :     error = IVAS_ERR_OK;
      66             : 
      67      346833 :     hDirAC = NULL;
      68             : 
      69      346833 :     if ( flag_config == DIRAC_RECONFIGURE )
      70             :     {
      71      277143 :         hDirAC = st_ivas->hDirAC;
      72             :     }
      73       69690 :     else if ( flag_config == DIRAC_OPEN )
      74             :     {
      75             :         /*-----------------------------------------------------------------*
      76             :          * prepare library opening
      77             :          *-----------------------------------------------------------------*/
      78             : 
      79       69690 :         if ( ( hDirAC = (DIRAC_DEC_HANDLE) malloc( sizeof( DIRAC_DEC_DATA ) ) ) == NULL )
      80             :         {
      81           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
      82             :         }
      83             : 
      84       69690 :         if ( ( hDirAC->hConfig = (DIRAC_CONFIG_DATA_HANDLE) malloc( sizeof( DIRAC_CONFIG_DATA ) ) ) == NULL )
      85             :         {
      86           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC Config\n" ) );
      87             :         }
      88             : 
      89       69690 :         st_ivas->hDirAC = hDirAC;
      90             :     }
      91             : 
      92             :     /*-----------------------------------------------------------------*
      93             :      * DirAC main configuration
      94             :      *-----------------------------------------------------------------*/
      95             : 
      96      346833 :     if ( ( error = ivas_dirac_config( (void *) st_ivas, DEC ) ) != IVAS_ERR_OK )
      97             :     {
      98           0 :         return error;
      99             :     }
     100             : 
     101      346833 :     if ( flag_config == DIRAC_OPEN )
     102             :     {
     103       69690 :         hDirAC->spar_to_dirac_write_idx = ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT ) ? DELAY_DIRAC_PARAM_DEC_SFR : 0;
     104       69690 :         hDirAC->dithering_seed = DIRAC_DITH_SEED;
     105       69690 :         st_ivas->hDirAC = hDirAC;
     106             :     }
     107             : 
     108      346833 :     return error;
     109             : }
     110             : 
     111             : 
     112      166146 : static ivas_error ivas_dirac_rend_config(
     113             :     Decoder_Struct *st_ivas,                 /* i/o: IVAS decoder structure  */
     114             :     const DIRAC_CONFIG_FLAG flag_config_inp, /* i/ : Flag determining if we open or reconfigure the DirAC decoder */
     115             :     const int16_t dec_param_estim_old )
     116             : {
     117             :     DIRAC_DEC_HANDLE hDirAC;
     118             :     int16_t nchan_out_woLFE;
     119             :     int16_t nchan_transport;
     120             :     int16_t nchan_transport_old;
     121             :     int16_t num_outputs_dir_old;
     122             :     int16_t num_outputs_diff_old;
     123             :     int16_t num_protos_diff_old;
     124             :     float *proto_frame_f_old;
     125             :     int16_t proto_signal_decorr_on_old;
     126             :     uint16_t i, j, k;
     127             :     float ls_azimuth[MAX_OUTPUT_CHANNELS];
     128             :     float ls_elevation[MAX_OUTPUT_CHANNELS];
     129             :     int32_t output_Fs, ivas_total_brate;
     130             :     ivas_error error;
     131             :     int16_t nchan_transport_orig;
     132             :     int16_t hodirac_flag;
     133             :     DIRAC_CONFIG_FLAG flag_config;
     134             :     DIRAC_REND_HANDLE hDirACRend;
     135             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
     136             : 
     137      166146 :     flag_config = ( flag_config_inp == DIRAC_RECONFIGURE_MODE ) ? DIRAC_RECONFIGURE : flag_config_inp;
     138      166146 :     error = IVAS_ERR_OK;
     139             : 
     140      166146 :     hDirACRend = NULL;
     141      166146 :     output_Fs = st_ivas->hDecoderConfig->output_Fs;
     142      166146 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
     143      166146 :     hodirac_flag = ivas_get_hodirac_flag( ivas_total_brate, st_ivas->sba_analysis_order );
     144             : 
     145      166146 :     hDirAC = st_ivas->hDirAC;
     146      166146 :     hSpatParamRendCom = st_ivas->hSpatParamRendCom;
     147             : 
     148      166146 :     if ( flag_config == DIRAC_RECONFIGURE )
     149             :     {
     150      113784 :         hDirACRend = st_ivas->hDirACRend;
     151             :     }
     152       52362 :     else if ( flag_config == DIRAC_OPEN )
     153             :     {
     154             :         /*-----------------------------------------------------------------*
     155             :          * prepare library opening
     156             :          *-----------------------------------------------------------------*/
     157             : 
     158       52362 :         if ( ( hDirACRend = (DIRAC_REND_HANDLE) malloc( sizeof( DIRAC_REND_DATA ) ) ) == NULL )
     159             :         {
     160           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC renderer\n" ) );
     161             :         }
     162             : 
     163       52362 :         nchan_transport_old = 0;
     164             :     }
     165             : 
     166      166146 :     nchan_transport_old = 0;
     167      166146 :     num_outputs_dir_old = 0;
     168      166146 :     num_outputs_diff_old = 0;
     169      166146 :     num_protos_diff_old = 0;
     170             : 
     171      166146 :     nchan_transport_orig = st_ivas->nchan_transport;
     172      166146 :     if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
     173             :     {
     174       66119 :         st_ivas->nchan_transport = ivas_sba_get_nchan_metadata( st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate );
     175             :     }
     176             : 
     177      166146 :     nchan_transport = st_ivas->nchan_transport;
     178      166146 :     if ( st_ivas->ivas_format == MASA_FORMAT && ivas_total_brate < MASA_STEREO_MIN_BITRATE && ivas_total_brate > IVAS_SID_5k2 )
     179             :     {
     180       19153 :         nchan_transport = 1;
     181             :     }
     182      166146 :     if ( flag_config == DIRAC_RECONFIGURE && ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT ) )
     183             :     {
     184             :         int16_t tmp1, tmp2, tmp3;
     185       32373 :         ivas_sba_config( ivas_total_brate, st_ivas->sba_analysis_order, -1, &nchan_transport_old, st_ivas->sba_planar, &tmp1, &tmp2, &tmp3 );
     186             :     }
     187             : 
     188             :     /*-----------------------------------------------------------------*
     189             :      * output setup: for parametric binaural renderer, use output setup, otherwise internal setup
     190             :      *-----------------------------------------------------------------*/
     191             : 
     192      166146 :     hDirACRend->hOutSetup = st_ivas->hIntSetup;
     193      166146 :     nchan_out_woLFE = hDirACRend->hOutSetup.nchan_out_woLFE;
     194             : 
     195      166146 :     if ( hDirACRend->hOutSetup.ls_azimuth != NULL && hDirACRend->hOutSetup.ls_elevation != NULL )
     196             :     {
     197       87502 :         mvr2r( hDirACRend->hOutSetup.ls_azimuth, ls_azimuth, nchan_out_woLFE );
     198       87502 :         mvr2r( hDirACRend->hOutSetup.ls_elevation, ls_elevation, nchan_out_woLFE );
     199             :     }
     200             : 
     201      166146 :     if ( hDirACRend->hOutSetup.ambisonics_order == -1 )
     202             :     {
     203       92415 :         hDirACRend->hOutSetup.ambisonics_order = SBA_HOA3_ORDER; /* Order 3 is used by default in DirAC for SHD processing */
     204       92415 :         if ( hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_MONO || hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_STEREO )
     205             :         {
     206        4913 :             hDirACRend->hOutSetup.ambisonics_order = SBA_FOA_ORDER;
     207             :         }
     208             :     }
     209       73731 :     else if ( hDirACRend->hOutSetup.ambisonics_order >= SBA_FOA_ORDER )
     210             :     {
     211       73731 :         mvr2r( ls_azimuth_4d4, ls_azimuth, DIRAC_HOA_RENDERING_NUM_VIRT_DECORR_LS );
     212       73731 :         mvr2r( ls_elevation_4d4, ls_elevation, DIRAC_HOA_RENDERING_NUM_VIRT_DECORR_LS );
     213             :     }
     214             : 
     215      166146 :     if ( hDirACRend->hOutSetup.separateChannelEnabled && ( hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_5_1 || hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_7_1 || hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_5_1_2 || hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_5_1_4 || hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_7_1_4 || ( hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hLsSetupCustom->separate_ch_found ) ) )
     216             :     {
     217             :         /* Remove the channel of the separated signal from the output setup of the spatial synthesis */
     218        4835 :         hDirACRend->hOutSetup.nchan_out_woLFE--;
     219        4835 :         nchan_out_woLFE = hDirACRend->hOutSetup.nchan_out_woLFE;
     220        4835 :         mvr2r( &ls_azimuth[hDirACRend->hOutSetup.separateChannelIndex + 1], &ls_azimuth[hDirACRend->hOutSetup.separateChannelIndex], nchan_out_woLFE - hDirACRend->hOutSetup.separateChannelIndex );
     221        4835 :         mvr2r( &ls_elevation[hDirACRend->hOutSetup.separateChannelIndex + 1], &ls_elevation[hDirACRend->hOutSetup.separateChannelIndex], nchan_out_woLFE - hDirACRend->hOutSetup.separateChannelIndex );
     222             :     }
     223             : 
     224             :     /*-----------------------------------------------------------------*
     225             :      * set input parameters
     226             :      *-----------------------------------------------------------------*/
     227             : 
     228      166146 :     st_ivas->nchan_transport = nchan_transport_orig;
     229             : 
     230      166146 :     if ( nchan_transport_orig > 2 && hDirACRend->hOutSetup.is_loudspeaker_setup && st_ivas->renderer_type == RENDERER_DIRAC && !hodirac_flag )
     231             :     {
     232        3054 :         hDirACRend->synthesisConf = DIRAC_SYNTHESIS_PSD_LS;
     233        3054 :         hDirACRend->panningConf = DIRAC_PANNING_VBAP;
     234             :     }
     235      163092 :     else if ( ( st_ivas->ivas_format == MASA_FORMAT || st_ivas->mc_mode == MC_MODE_MCMASA ) && hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_MONO )
     236             :     {
     237        4913 :         hDirACRend->synthesisConf = DIRAC_SYNTHESIS_MONO;
     238        4913 :         hDirACRend->panningConf = DIRAC_PANNING_HOA3;
     239        4913 :         nchan_out_woLFE = 1;
     240             :     }
     241      158179 :     else if ( ( st_ivas->ivas_format == MASA_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT || st_ivas->mc_mode == MC_MODE_MCMASA ) && hDirACRend->hOutSetup.is_loudspeaker_setup )
     242             :     {
     243       66748 :         hDirACRend->synthesisConf = DIRAC_SYNTHESIS_PSD_LS;
     244       66748 :         hDirACRend->panningConf = DIRAC_PANNING_VBAP;
     245             :     }
     246       91431 :     else if ( ( st_ivas->ivas_format == MASA_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT ) && !hDirACRend->hOutSetup.is_loudspeaker_setup && st_ivas->nchan_transport > 1 )
     247             :     {
     248       27895 :         hDirACRend->synthesisConf = DIRAC_SYNTHESIS_PSD_SHD;
     249       27895 :         hDirACRend->panningConf = DIRAC_PANNING_HOA3;
     250             :     }
     251             :     else
     252             :     {
     253       63536 :         hDirACRend->synthesisConf = DIRAC_SYNTHESIS_GAIN_SHD;
     254       63536 :         hDirACRend->panningConf = DIRAC_PANNING_HOA3;
     255             :     }
     256             : 
     257      166146 :     if ( flag_config == DIRAC_OPEN )
     258             :     {
     259       52362 :         if ( ( hDirACRend->frequency_axis = (float *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( float ) ) ) == NULL )
     260             :         {
     261           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     262             :         }
     263       52362 :         set_f( hDirACRend->frequency_axis, 0.0f, hSpatParamRendCom->num_freq_bands );
     264             : 
     265       52362 :         ivas_dirac_dec_get_frequency_axis( hDirACRend->frequency_axis, output_Fs, hSpatParamRendCom->num_freq_bands );
     266             :     }
     267             : 
     268      166146 :     if ( ( st_ivas->ivas_format == MASA_FORMAT || st_ivas->mc_mode == MC_MODE_MCMASA ) && hDirACRend->panningConf == DIRAC_PANNING_HOA3 && nchan_transport == 2 )
     269             :     {
     270        6984 :         if ( ( flag_config == DIRAC_RECONFIGURE && hDirACRend->masa_stereo_type_detect == NULL ) || flag_config == DIRAC_OPEN )
     271             :         {
     272        4614 :             if ( ( hDirACRend->masa_stereo_type_detect = (MASA_STEREO_TYPE_DETECT *) malloc( sizeof( MASA_STEREO_TYPE_DETECT ) ) ) == NULL )
     273             :             {
     274           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     275             :             }
     276             :         }
     277        6984 :         ivas_masa_init_stereotype_detection( hDirACRend->masa_stereo_type_detect );
     278             :     }
     279             :     else
     280             :     {
     281      159162 :         if ( flag_config == DIRAC_RECONFIGURE && hDirACRend->masa_stereo_type_detect != NULL )
     282             :         {
     283        1554 :             free( hDirACRend->masa_stereo_type_detect );
     284             :         }
     285      159162 :         hDirACRend->masa_stereo_type_detect = NULL;
     286             :     }
     287             : 
     288      166146 :     hSpatParamRendCom->numIsmDirections = 0; /* By default, no ism directions, set correct number runtime when needed */
     289             : 
     290             :     /*-----------------------------------------------------------------*
     291             :      * (re)configure sub-modules
     292             :      *-----------------------------------------------------------------*/
     293             : 
     294             :     /* prototype signal computation */
     295      166146 :     if ( flag_config == DIRAC_RECONFIGURE )
     296             :     {
     297      113784 :         num_outputs_dir_old = hDirACRend->num_outputs_dir;
     298      113784 :         num_outputs_diff_old = hDirACRend->num_outputs_diff;
     299      113784 :         num_protos_diff_old = hDirACRend->num_protos_diff;
     300             :     }
     301             : 
     302             :     /* allocate output setup related arrays */
     303      166146 :     if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_LS )
     304             :     {
     305             :         /* Directional and diffuses components in output LS format */
     306       69802 :         hDirACRend->num_outputs_diff = nchan_out_woLFE;
     307       69802 :         hDirACRend->num_outputs_dir = nchan_out_woLFE;
     308             :     }
     309       96344 :     else if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
     310             :     {
     311             :         /* Directional and diffuses components in SHD */
     312             :         /* Diffuseness components up to 1st order */
     313       63536 :         hDirACRend->num_outputs_diff = ( min( hDirACRend->hOutSetup.ambisonics_order, 1 ) + 1 ) * ( min( hDirACRend->hOutSetup.ambisonics_order, 1 ) + 1 );
     314       63536 :         hDirACRend->num_outputs_dir = ivas_sba_get_nchan( hDirACRend->hOutSetup.ambisonics_order, 0 );
     315             :     }
     316       32808 :     else if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_SHD )
     317             :     {
     318       27895 :         hDirACRend->num_outputs_diff = DIRAC_HOA_RENDERING_NUM_VIRT_DECORR_LS;
     319       27895 :         hDirACRend->num_outputs_dir = nchan_out_woLFE;
     320             :     }
     321        4913 :     else if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_MONO )
     322             :     {
     323        4913 :         hDirACRend->num_outputs_diff = 1; /* There is one output channel in mono */
     324        4913 :         hDirACRend->num_outputs_dir = 2;  /* Two channels are pre-rendered for stereo type detection */
     325             :     }
     326             :     else
     327             :     {
     328           0 :         assert( 0 && "DirAC: not existing synthesis methods!" );
     329             :     }
     330             : 
     331      166146 :     if ( flag_config == DIRAC_OPEN )
     332             :     {
     333       52362 :         num_outputs_dir_old = hDirACRend->num_outputs_dir;
     334       52362 :         if ( ( hDirACRend->proto_index_dir = (int16_t *) malloc( sizeof( int16_t ) * hDirACRend->num_outputs_dir ) ) == NULL )
     335             :         {
     336           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     337             :         }
     338       52362 :         num_outputs_diff_old = hDirACRend->num_outputs_diff;
     339       52362 :         if ( ( hDirACRend->proto_index_diff = (int16_t *) malloc( sizeof( int16_t ) * hDirACRend->num_outputs_diff ) ) == NULL )
     340             :         {
     341           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     342             :         }
     343             :     }
     344             : 
     345      166146 :     if ( hDirACRend->num_outputs_dir != num_outputs_dir_old && flag_config == DIRAC_RECONFIGURE )
     346             :     {
     347        4033 :         free( hDirACRend->proto_index_dir );
     348        4033 :         if ( ( hDirACRend->proto_index_dir = (int16_t *) malloc( sizeof( int16_t ) * hDirACRend->num_outputs_dir ) ) == NULL )
     349             :         {
     350           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     351             :         }
     352             :     }
     353      166146 :     set_s( hDirACRend->proto_index_dir, 0, hDirACRend->num_outputs_dir );
     354             : 
     355      166146 :     if ( hDirACRend->num_outputs_diff != num_outputs_diff_old && flag_config == DIRAC_RECONFIGURE )
     356             :     {
     357        4033 :         free( hDirACRend->proto_index_diff );
     358        4033 :         if ( ( hDirACRend->proto_index_diff = (int16_t *) malloc( sizeof( int16_t ) * hDirACRend->num_outputs_diff ) ) == NULL )
     359             :         {
     360           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     361             :         }
     362             :     }
     363      166146 :     set_s( hDirACRend->proto_index_diff, 0, hDirACRend->num_outputs_diff );
     364             : 
     365      166146 :     hDirACRend->sba_map_tc = sba_map_tc;
     366             : 
     367      166146 :     if ( ( st_ivas->ivas_format == SBA_FORMAT ) || ( st_ivas->ivas_format == SBA_ISM_FORMAT ) )
     368             :     {
     369       66119 :         if ( st_ivas->sba_order > SBA_FOA_ORDER && ivas_total_brate >= IVAS_512k )
     370             :         {
     371        5233 :             hDirACRend->sba_map_tc = sba_map_tc_512;
     372             :         }
     373             :     }
     374             : 
     375      166146 :     if ( nchan_transport == 1 )
     376             :     {
     377       33476 :         hDirACRend->num_protos_ambi = 1;
     378       33476 :         hDirACRend->num_protos_dir = 1;
     379       33476 :         hDirACRend->num_protos_diff = 1;
     380             :     }
     381      132670 :     else if ( nchan_transport == 2 )
     382             :     {
     383       66551 :         if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
     384             :         {
     385           0 :             hDirACRend->num_protos_ambi = 2;
     386           0 :             hDirACRend->num_protos_diff = 1;
     387           0 :             hDirACRend->num_protos_dir = 2;
     388           0 :             hDirACRend->proto_index_dir[1] = 1;
     389             :         }
     390       66551 :         else if ( hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_MONO )
     391             :         {
     392             :             /* Following the foa rendering for code compatibility */
     393        4913 :             hDirACRend->num_protos_ambi = 2;
     394        4913 :             hDirACRend->num_protos_dir = 2;
     395        4913 :             hDirACRend->num_protos_diff = 3;
     396        4913 :             hDirACRend->proto_index_dir[0] = 0;
     397        4913 :             hDirACRend->proto_index_diff[0] = 0;
     398             :         }
     399             :         else
     400             :         {
     401       61638 :             hDirACRend->num_protos_ambi = 2;
     402       61638 :             hDirACRend->num_protos_diff = 3;
     403             : 
     404      565768 :             for ( k = 0; k < hDirACRend->num_outputs_diff; k++ )
     405             :             {
     406      504130 :                 if ( ls_azimuth[k] > 0.0f )
     407             :                 {
     408      234075 :                     hDirACRend->proto_index_diff[k] = 1;
     409             :                 }
     410      270055 :                 else if ( ls_azimuth[k] < 0.0f )
     411             :                 {
     412      234075 :                     hDirACRend->proto_index_diff[k] = 2;
     413             :                 }
     414             :                 else
     415             :                 {
     416       35980 :                     hDirACRend->proto_index_diff[k] = 0;
     417             :                 }
     418             :             }
     419             : 
     420       61638 :             if ( hDirACRend->hOutSetup.is_loudspeaker_setup )
     421             :             {
     422       40815 :                 hDirACRend->num_protos_dir = 3;
     423       40815 :                 mvs2s( hDirACRend->proto_index_diff, hDirACRend->proto_index_dir, nchan_out_woLFE );
     424             :             }
     425             :             else
     426             :             {
     427       20823 :                 hDirACRend->num_protos_dir = 2;
     428       20823 :                 hDirACRend->proto_index_dir[1] = 1;
     429             :             }
     430             :         }
     431             :     }
     432             :     else /* nchan_transport > 2 */
     433             :     {
     434       66119 :         hDirACRend->num_protos_ambi = 4;
     435       66119 :         if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_LS )
     436             :         {
     437        3054 :             hDirACRend->num_protos_diff = hDirACRend->num_outputs_diff;
     438       31225 :             for ( k = 0; k < hDirACRend->num_outputs_diff; k++ )
     439             :             {
     440       28171 :                 hDirACRend->proto_index_diff[k] = k;
     441             :             }
     442             : 
     443        3054 :             hDirACRend->num_protos_dir = hDirACRend->num_outputs_dir;
     444       31225 :             for ( k = 0; k < hDirACRend->num_outputs_dir; k++ )
     445             :             {
     446       28171 :                 hDirACRend->proto_index_dir[k] = k;
     447             :             }
     448             :         }
     449             :         else
     450             :         {
     451       63065 :             hDirACRend->num_protos_diff = 1;
     452       63065 :             hDirACRend->num_protos_dir = nchan_transport;
     453             : 
     454      371224 :             for ( k = 0; k < min( hDirACRend->num_outputs_dir, hDirACRend->num_protos_dir ); k++ )
     455             :             {
     456      308159 :                 if ( hDirACRend->sba_map_tc[k] < hDirACRend->num_outputs_dir )
     457             :                 {
     458      307799 :                     hDirACRend->proto_index_dir[hDirACRend->sba_map_tc[k]] = k;
     459             :                 }
     460             :             }
     461             :         }
     462             :     }
     463             : 
     464             :     /* direct/diffuse responses */
     465      166146 :     if ( flag_config == DIRAC_OPEN )
     466             :     {
     467       52362 :         if ( ( hDirACRend->diffuse_response_function = (float *) malloc( sizeof( float ) * hDirACRend->num_outputs_dir ) ) == NULL )
     468             :         {
     469           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     470             :         }
     471             :     }
     472             :     /* reallocate static memory */
     473      113784 :     else if ( flag_config == DIRAC_RECONFIGURE && hDirACRend->num_outputs_dir != num_outputs_dir_old )
     474             :     {
     475        4033 :         free( hDirACRend->diffuse_response_function );
     476        4033 :         hDirACRend->diffuse_response_function = NULL;
     477        4033 :         if ( ( hDirACRend->diffuse_response_function = (float *) malloc( sizeof( float ) * hDirACRend->num_outputs_dir ) ) == NULL )
     478             :         {
     479           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     480             :         }
     481             :     }
     482             : 
     483      166146 :     if ( ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_LS ) || ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_SHD ) || ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_MONO ) )
     484             :     {
     485      102610 :         initDiffuseResponses( hDirACRend->diffuse_response_function, nchan_out_woLFE, hDirACRend->hOutSetup.output_config,
     486      102610 :                               hDirACRend->hOutSetup, hDirACRend->hOutSetup.ambisonics_order, st_ivas->ivas_format, &hDirACRend->num_ele_spk_no_diffuse_rendering, st_ivas->transport_config );
     487             :     }
     488             :     else
     489             :     {
     490       63536 :         initDiffuseResponses( hDirACRend->diffuse_response_function, hDirACRend->num_outputs_dir, IVAS_AUDIO_CONFIG_FOA,
     491       63536 :                               hDirACRend->hOutSetup, hDirACRend->hOutSetup.ambisonics_order, st_ivas->ivas_format, &hDirACRend->num_ele_spk_no_diffuse_rendering, IVAS_AUDIO_CONFIG_INVALID );
     492             :     }
     493             : 
     494      166146 :     if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_SHD )
     495             :     {
     496       27895 :         if ( flag_config == DIRAC_OPEN )
     497             :         {
     498        2542 :             if ( ( hDirACRend->hoa_encoder = (float *) malloc( nchan_out_woLFE * hDirACRend->num_outputs_diff * sizeof( float ) ) ) == NULL )
     499             :             {
     500           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     501             :             }
     502             :         }
     503       25353 :         else if ( flag_config == DIRAC_RECONFIGURE && hDirACRend->hoa_encoder && ( hDirACRend->num_outputs_diff != num_outputs_diff_old ) )
     504             :         {
     505           0 :             free( hDirACRend->hoa_encoder );
     506           0 :             hDirACRend->hoa_encoder = NULL;
     507           0 :             if ( ( hDirACRend->hoa_encoder = (float *) malloc( nchan_out_woLFE * hDirACRend->num_outputs_diff * sizeof( float ) ) ) == NULL )
     508             :             {
     509           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     510             :             }
     511             :         }
     512       27895 :         set_f( hDirACRend->hoa_encoder, 0.0f, nchan_out_woLFE * hDirACRend->num_outputs_diff );
     513       27895 :         compute_hoa_encoder_mtx( ls_azimuth, ls_elevation, hDirACRend->hoa_encoder, hDirACRend->num_outputs_diff, hDirACRend->hOutSetup.ambisonics_order );
     514             :     }
     515             :     else
     516             :     {
     517      138251 :         if ( flag_config == DIRAC_RECONFIGURE && hDirACRend->hoa_encoder )
     518             :         {
     519           0 :             free( hDirACRend->hoa_encoder );
     520             :         }
     521      138251 :         hDirACRend->hoa_encoder = NULL;
     522             :     }
     523             : 
     524             :     /* VBAP */
     525      166146 :     if ( flag_config == DIRAC_OPEN )
     526             :     {
     527       52362 :         st_ivas->hVBAPdata = NULL;
     528             :     }
     529             : 
     530      166146 :     if ( hDirACRend->panningConf == DIRAC_PANNING_VBAP )
     531             :     {
     532       69802 :         if ( flag_config == DIRAC_RECONFIGURE && st_ivas->hVBAPdata != NULL )
     533             :         {
     534       54379 :             vbap_free_data( &( st_ivas->hVBAPdata ) );
     535             :         }
     536             : 
     537       69802 :         if ( ( error = vbap_init_data( &( st_ivas->hVBAPdata ), ls_azimuth, ls_elevation, nchan_out_woLFE, st_ivas->ivas_format ) ) != IVAS_ERR_OK )
     538             :         {
     539           0 :             return error;
     540             :         }
     541             :     }
     542       96344 :     else if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_MONO )
     543             :     {
     544        4913 :         if ( flag_config == DIRAC_RECONFIGURE && st_ivas->hVBAPdata != NULL )
     545             :         {
     546           0 :             vbap_free_data( &( st_ivas->hVBAPdata ) );
     547             :         }
     548        4913 :         hDirACRend->hoa_decoder = NULL;
     549             :     }
     550       91431 :     else if ( flag_config == DIRAC_RECONFIGURE && st_ivas->hVBAPdata != NULL )
     551             :     {
     552         780 :         vbap_free_data( &( st_ivas->hVBAPdata ) );
     553             :     }
     554             : 
     555             :     /* HOA panning/dec */
     556      166146 :     if ( flag_config == DIRAC_OPEN )
     557             :     {
     558       52362 :         hDirACRend->hoa_decoder = NULL;
     559       52362 :         if ( ( hDirACRend->panningConf == DIRAC_PANNING_HOA3 ) || st_ivas->ivas_format == SBA_FORMAT || ( nchan_transport > 2 ) )
     560             :         {
     561       39302 :             if ( hDirACRend->hOutSetup.is_loudspeaker_setup )
     562             :             {
     563       17851 :                 if ( st_ivas->hoa_dec_mtx != NULL )
     564             :                 {
     565        2336 :                     free( st_ivas->hoa_dec_mtx );
     566        2336 :                     st_ivas->hoa_dec_mtx = NULL;
     567             :                 }
     568       17851 :                 if ( ( error = ivas_sba_get_hoa_dec_matrix( hDirACRend->hOutSetup, &st_ivas->hoa_dec_mtx, hDirACRend->hOutSetup.ambisonics_order ) ) != IVAS_ERR_OK )
     569             :                 {
     570           0 :                     return error;
     571             :                 }
     572             : 
     573       17851 :                 hDirACRend->hoa_decoder = st_ivas->hoa_dec_mtx;
     574             :             }
     575             :         }
     576             :     }
     577             : 
     578             :     /* decorrelation */
     579      166146 :     proto_signal_decorr_on_old = ( flag_config == DIRAC_RECONFIGURE ) ? hDirACRend->proto_signal_decorr_on : 0;
     580      166146 :     hDirACRend->proto_signal_decorr_on = 1;
     581      166146 :     if ( ( nchan_transport > 2 ) && ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_LS ) )
     582             :     {
     583             :         /*switch off decorrelation for 4 transport channels*/
     584        3054 :         hDirACRend->proto_signal_decorr_on = 0;
     585             :     }
     586      166146 :     if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_MONO )
     587             :     {
     588        4913 :         hDirACRend->proto_signal_decorr_on = 0;
     589             :     }
     590             : 
     591      166146 :     if ( ( flag_config == DIRAC_OPEN && hDirACRend->proto_signal_decorr_on ) || ( flag_config == DIRAC_RECONFIGURE && ( hDirACRend->proto_signal_decorr_on && !proto_signal_decorr_on_old ) ) )
     592             :     {
     593       98032 :         if ( ( error = ivas_dirac_dec_decorr_open( &( hDirACRend->h_freq_domain_decorr_ap_params ), &( hDirACRend->h_freq_domain_decorr_ap_state ), hSpatParamRendCom->num_freq_bands, hDirACRend->num_outputs_diff,
     594       49016 :                                                    hDirACRend->num_protos_diff, hDirACRend->synthesisConf, hDirACRend->frequency_axis, nchan_transport > 2 ? 4 : nchan_transport, output_Fs ) ) != IVAS_ERR_OK )
     595             :         {
     596           0 :             return error;
     597             :         }
     598             :     }
     599      117130 :     else if ( flag_config == DIRAC_RECONFIGURE && ( !hDirACRend->proto_signal_decorr_on && proto_signal_decorr_on_old ) )
     600             :     {
     601         780 :         ivas_dirac_dec_decorr_close( &hDirACRend->h_freq_domain_decorr_ap_params, &hDirACRend->h_freq_domain_decorr_ap_state );
     602             :     }
     603      116350 :     else if ( flag_config == DIRAC_RECONFIGURE && hDirACRend->proto_signal_decorr_on && proto_signal_decorr_on_old )
     604             :     {
     605      109163 :         if ( nchan_transport != nchan_transport_old || hDirACRend->num_outputs_diff != num_outputs_diff_old || flag_config_inp == DIRAC_RECONFIGURE_MODE )
     606             :         {
     607             :             /* close and reopen the decorrelator */
     608      109163 :             ivas_dirac_dec_decorr_close( &hDirACRend->h_freq_domain_decorr_ap_params, &hDirACRend->h_freq_domain_decorr_ap_state );
     609             : 
     610      218326 :             if ( ( error = ivas_dirac_dec_decorr_open( &( hDirACRend->h_freq_domain_decorr_ap_params ), &( hDirACRend->h_freq_domain_decorr_ap_state ), hSpatParamRendCom->num_freq_bands, hDirACRend->num_outputs_diff,
     611      109163 :                                                        hDirACRend->num_protos_diff, hDirACRend->synthesisConf, hDirACRend->frequency_axis, nchan_transport > 2 ? 4 : nchan_transport, output_Fs ) ) != IVAS_ERR_OK )
     612             :             {
     613           0 :                 return error;
     614             :             }
     615             :         }
     616             :     }
     617             : 
     618             :     /* output synthesis */
     619      166146 :     if ( flag_config == DIRAC_OPEN )
     620             :     {
     621       52362 :         if ( ( ivas_dirac_dec_output_synthesis_open( hSpatParamRendCom, hDirACRend, st_ivas->renderer_type, nchan_transport, output_Fs, hodirac_flag ) ) != IVAS_ERR_OK )
     622             :         {
     623           0 :             return error;
     624             :         }
     625       52362 :         hDirACRend->h_output_synthesis_psd_params.use_onset_filters = hDirACRend->proto_signal_decorr_on;
     626             :     }
     627      113784 :     else if ( ( flag_config == DIRAC_RECONFIGURE ) && ( ( nchan_transport != nchan_transport_old ) || ( hDirACRend->num_outputs_diff != num_outputs_diff_old ) ) )
     628             :     {
     629      111721 :         ivas_dirac_dec_output_synthesis_close( hDirACRend );
     630             : 
     631      111721 :         if ( ( ivas_dirac_dec_output_synthesis_open( hSpatParamRendCom, hDirACRend, st_ivas->renderer_type, nchan_transport, output_Fs, hodirac_flag ) ) != IVAS_ERR_OK )
     632             :         {
     633           0 :             return error;
     634             :         }
     635      111721 :         hDirACRend->h_output_synthesis_psd_params.use_onset_filters = hDirACRend->proto_signal_decorr_on;
     636             :     }
     637             : 
     638      166146 :     if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_SHD || hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
     639             :     {
     640       91431 :         hDirACRend->h_output_synthesis_psd_params.use_onset_filters = 0;
     641             :     }
     642             : 
     643             :     /*-----------------------------------------------------------------*
     644             :      * memory allocation
     645             :      *-----------------------------------------------------------------*/
     646             : 
     647      166146 :     if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
     648             :     {
     649       63536 :         if ( flag_config == DIRAC_RECONFIGURE && hDirACRend->proto_frame_f )
     650             :         {
     651         780 :             free( hDirACRend->proto_frame_f );
     652             :         }
     653       63536 :         hDirACRend->proto_frame_f = NULL;
     654             :     }
     655             :     else
     656             :     {
     657      102610 :         if ( flag_config == DIRAC_OPEN || ( flag_config == DIRAC_RECONFIGURE && hDirACRend->proto_frame_f == NULL ) )
     658             :         {
     659       20508 :             if ( ( hDirACRend->proto_frame_f = (float *) malloc( sizeof( float ) * 2 * hDirACRend->num_protos_diff * hSpatParamRendCom->num_freq_bands ) ) == NULL )
     660             :             {
     661           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     662             :             }
     663             :         }
     664       82102 :         else if ( flag_config == DIRAC_RECONFIGURE && ( hDirACRend->num_protos_diff != num_protos_diff_old ) )
     665             :         {
     666       20526 :             proto_frame_f_old = hDirACRend->proto_frame_f;
     667       20526 :             free( proto_frame_f_old );
     668       20526 :             if ( ( hDirACRend->proto_frame_f = (float *) malloc( sizeof( float ) * 2 * hDirACRend->num_protos_diff * hSpatParamRendCom->num_freq_bands ) ) == NULL )
     669             :             {
     670           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     671             :             }
     672             :         }
     673             :     }
     674             : 
     675      166146 :     if ( flag_config == DIRAC_OPEN )
     676             :     {
     677       52362 :         hDirACRend->buffer_energy = NULL;
     678             :     }
     679             : 
     680      166146 :     if ( ( flag_config == DIRAC_OPEN && hDirAC->hConfig->dec_param_estim == TRUE ) || ( flag_config == DIRAC_RECONFIGURE && ( hDirAC->hConfig->dec_param_estim == TRUE && dec_param_estim_old == FALSE ) ) )
     681             :     {
     682       31932 :         hDirACRend->index_buffer_intensity = 0;
     683             : 
     684      127728 :         for ( i = 0; i < DIRAC_NUM_DIMS; i++ )
     685             :         {
     686     3161268 :             for ( j = 0; j < DIRAC_NO_COL_AVG_DIFF; j++ )
     687             :             {
     688     3065472 :                 if ( ( hDirACRend->buffer_intensity_real[i][j] = (float *) malloc( CLDFB_NO_CHANNELS_MAX * sizeof( float ) ) ) == NULL )
     689             :                 {
     690           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     691             :                 }
     692     3065472 :                 set_f( hDirACRend->buffer_intensity_real[i][j], 0.0f, CLDFB_NO_CHANNELS_MAX );
     693             :             }
     694             :         }
     695       31932 :         if ( hDirACRend->buffer_energy == NULL )
     696             :         {
     697       31932 :             if ( ( hDirACRend->buffer_energy = (float *) malloc( DIRAC_NO_COL_AVG_DIFF * CLDFB_NO_CHANNELS_MAX * sizeof( float ) ) ) == NULL )
     698             :             {
     699           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     700             :             }
     701             :         }
     702       31932 :         set_f( hDirACRend->buffer_energy, 0.0f, DIRAC_NO_COL_AVG_DIFF * CLDFB_NO_CHANNELS_MAX );
     703             :     }
     704      134214 :     else if ( ( flag_config == DIRAC_OPEN && hDirAC->hConfig->dec_param_estim == FALSE ) || ( flag_config == DIRAC_RECONFIGURE && ( hDirAC->hConfig->dec_param_estim == FALSE && dec_param_estim_old == TRUE ) ) )
     705             :     {
     706      106264 :         for ( i = 0; i < DIRAC_NUM_DIMS; i++ )
     707             :         {
     708     2630034 :             for ( j = 0; j < DIRAC_NO_COL_AVG_DIFF; j++ )
     709             :             {
     710     2550336 :                 if ( flag_config == DIRAC_RECONFIGURE && hDirACRend->buffer_intensity_real[i][j] )
     711             :                 {
     712      299136 :                     free( hDirACRend->buffer_intensity_real[i][j] );
     713             :                 }
     714     2550336 :                 hDirACRend->buffer_intensity_real[i][j] = NULL;
     715             :             }
     716             :         }
     717       26566 :         if ( hDirACRend->buffer_energy != NULL )
     718             :         {
     719        3116 :             free( hDirACRend->buffer_energy );
     720        3116 :             hDirACRend->buffer_energy = NULL;
     721             :         }
     722             :     }
     723             : 
     724             :     /* output synthesis */
     725      166146 :     ivas_dirac_dec_output_synthesis_init( hSpatParamRendCom, hDirACRend, nchan_out_woLFE, hodirac_flag );
     726             : 
     727             :     /* Allocate stack memory */
     728      166146 :     if ( flag_config != DIRAC_OPEN )
     729             :     {
     730      113784 :         ivas_dirac_free_mem( &( hDirACRend->stack_mem ) );
     731             :     }
     732      166146 :     if ( ( error = ivas_dirac_alloc_mem( hDirACRend, st_ivas->renderer_type, hSpatParamRendCom->num_freq_bands, &( hDirACRend->stack_mem ), hodirac_flag ) ) != IVAS_ERR_OK )
     733             :     {
     734           0 :         return error;
     735             :     }
     736             : 
     737      166146 :     if ( flag_config == DIRAC_OPEN )
     738             :     {
     739       52362 :         st_ivas->hDirACRend = hDirACRend;
     740             :     }
     741             : 
     742      166146 :     return error;
     743             : }
     744             : 
     745             : 
     746             : /*-------------------------------------------------------------------------
     747             :  * ivas_dirac_dec_config()
     748             :  *
     749             :  * Open or reconfigure decoder DirAC/MASA handle
     750             :  *-------------------------------------------------------------------------*/
     751             : 
     752      346833 : ivas_error ivas_dirac_dec_config(
     753             :     Decoder_Struct *st_ivas,                /* i/o: IVAS decoder structure  */
     754             :     const DIRAC_CONFIG_FLAG flag_config_inp /* i/ : Flag determining if we open or reconfigure the DirAC decoder */
     755             : )
     756             : {
     757             :     ivas_error error;
     758             :     int32_t output_Fs;
     759             :     int16_t hodirac_flag;
     760             :     int16_t sparfoa_flag;
     761             :     DIRAC_CONFIG_FLAG dec_config_flag;
     762             :     DIRAC_CONFIG_FLAG rend_config_flag;
     763             :     DIRAC_CONFIG_FLAG common_rend_config_flag;
     764             :     int16_t need_dirac_rend;
     765             :     int16_t need_parambin;
     766             :     int16_t dec_param_estim_old;
     767             :     int16_t dec_param_estim_new;
     768             :     int16_t num_poses, pos_idx;
     769             : 
     770      346833 :     error = IVAS_ERR_OK;
     771             : 
     772             :     /* Solve and setup flags for inits */
     773      346833 :     dec_config_flag = ( flag_config_inp == DIRAC_RECONFIGURE_MODE ) ? DIRAC_RECONFIGURE : flag_config_inp;
     774             : 
     775      346833 :     output_Fs = st_ivas->hDecoderConfig->output_Fs;
     776      346833 :     hodirac_flag = ivas_get_hodirac_flag( st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->sba_analysis_order );
     777      346833 :     dec_param_estim_old = ( dec_config_flag == DIRAC_RECONFIGURE ) ? st_ivas->hDirAC->hConfig->dec_param_estim : FALSE;
     778             : 
     779      346833 :     num_poses = 1;
     780      346833 :     if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
     781             :     {
     782        2226 :         num_poses = st_ivas->hSplitBinRend->splitrend.multiBinPoseData.num_poses;
     783             :     }
     784             : 
     785      346833 :     sparfoa_flag = 0;
     786      346833 :     if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_FOA && st_ivas->ivas_format == SBA_FORMAT && !hodirac_flag )
     787             :     {
     788           0 :         sparfoa_flag = 1;
     789             :     }
     790             : 
     791      346833 :     if ( ( error = ivas_dirac_dec_config_internal( st_ivas, dec_config_flag ) ) != IVAS_ERR_OK )
     792             :     {
     793           0 :         return error;
     794             :     }
     795             : 
     796             :     /* This is required for parambin */
     797      346833 :     if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
     798             :     {
     799      178941 :         st_ivas->hDirAC->hConfig->dec_param_estim = FALSE;
     800             :     }
     801             : 
     802      346833 :     dec_param_estim_new = st_ivas->hDirAC->hConfig->dec_param_estim;
     803             : 
     804             :     /* Setup renderers and meta */
     805             :     /* First, free everything if in reconfig and not the active renderer */
     806      346833 :     need_parambin = 0;
     807      346833 :     switch ( st_ivas->renderer_type )
     808             :     {
     809      178941 :         case RENDERER_BINAURAL_PARAMETRIC:
     810             :         case RENDERER_BINAURAL_PARAMETRIC_ROOM:
     811             :         case RENDERER_STEREO_PARAMETRIC:
     812      178941 :             need_parambin = 1;
     813      178941 :             break;
     814      167892 :         default:
     815      167892 :             need_parambin = 0;
     816             :     }
     817             : 
     818      346833 :     if ( !need_parambin )
     819             :     {
     820      167892 :         ivas_dirac_dec_close_binaural_data( st_ivas->hDiracDecBin );
     821             :     }
     822             : 
     823      346833 :     need_dirac_rend = 0;
     824      346833 :     switch ( st_ivas->renderer_type )
     825             :     {
     826      166146 :         case RENDERER_DIRAC:
     827             :         case RENDERER_BINAURAL_FASTCONV:
     828             :         case RENDERER_BINAURAL_FASTCONV_ROOM:
     829             :         case RENDERER_SBA_LINEAR_ENC:
     830             :         case RENDERER_SBA_LINEAR_DEC:
     831             :         case RENDERER_OSBA_AMBI:
     832             :         case RENDERER_OSBA_LS:
     833      166146 :             need_dirac_rend = 1;
     834      166146 :             break;
     835      180687 :         default:
     836      180687 :             need_dirac_rend = 0;
     837             :     }
     838             : 
     839      346833 :     if ( !need_dirac_rend )
     840             :     {
     841      180687 :         ivas_dirac_rend_close( &st_ivas->hDirACRend );
     842             :     }
     843             : 
     844      346833 :     if ( !sparfoa_flag )
     845             :     {
     846      346833 :         common_rend_config_flag = st_ivas->hSpatParamRendCom == NULL ? DIRAC_OPEN : flag_config_inp;
     847      346833 :         if ( ( error = ivas_spat_hSpatParamRendCom_config( &st_ivas->hSpatParamRendCom, common_rend_config_flag, dec_param_estim_new,
     848             :                                                            st_ivas->ivas_format, st_ivas->mc_mode, output_Fs, hodirac_flag, 0 ) ) != IVAS_ERR_OK )
     849             :         {
     850           0 :             return error;
     851             :         }
     852             : 
     853      346833 :         if ( need_dirac_rend )
     854             :         {
     855      166146 :             rend_config_flag = st_ivas->hDirACRend == NULL ? DIRAC_OPEN : flag_config_inp;
     856      166146 :             if ( ( error = ivas_dirac_rend_config( st_ivas, rend_config_flag, dec_param_estim_old ) ) != IVAS_ERR_OK )
     857             :             {
     858           0 :                 return error;
     859             :             }
     860             :         }
     861             : 
     862      346833 :         if ( need_parambin )
     863             :         {
     864      178941 :             if ( st_ivas->renderer_type != RENDERER_STEREO_PARAMETRIC )
     865             :             {
     866      144499 :                 if ( ( error = ivas_dirac_dec_binaural_copy_hrtfs( &st_ivas->hHrtfParambin ) ) != IVAS_ERR_OK )
     867             :                 {
     868           0 :                     return error;
     869             :                 }
     870             :             }
     871             : 
     872      178941 :             if ( st_ivas->hDiracDecBin[0] == NULL )
     873             :             {
     874       27830 :                 if ( ( error = ivas_dirac_dec_init_binaural_data( st_ivas, &( st_ivas->hHrtfParambin ) ) ) != IVAS_ERR_OK )
     875             :                 {
     876           0 :                     return error;
     877             :                 }
     878             :             }
     879             :             else
     880             :             {
     881             :                 /* This is required to keep BE in rate switching. This probably means that 1TC and 2TC MASA perform differently. */
     882      151111 :                 if ( st_ivas->hDiracDecBin[0]->h_freq_domain_decorr_ap_params != NULL && !( st_ivas->ivas_format == MASA_FORMAT && st_ivas->nSCE > 0 ) )
     883             :                 {
     884       80160 :                     ivas_dirac_dec_decorr_close( &st_ivas->hDiracDecBin[0]->h_freq_domain_decorr_ap_params, &st_ivas->hDiracDecBin[0]->h_freq_domain_decorr_ap_state );
     885             :                 }
     886             : 
     887      151111 :                 if ( ( error = ivas_td_decorr_reconfig_dec( st_ivas->ivas_format, st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->nchan_transport, st_ivas->hDecoderConfig->output_Fs, &( st_ivas->hDiracDecBin[0]->hTdDecorr ), &( st_ivas->hDiracDecBin[0]->useTdDecorr ) ) ) != IVAS_ERR_OK )
     888             :                 {
     889           0 :                     return error;
     890             :                 }
     891             : 
     892             :                 /* copy td-decorr flag to split renderer side rendereres */
     893      151111 :                 for ( pos_idx = 1; pos_idx < num_poses; pos_idx++ )
     894             :                 {
     895           0 :                     st_ivas->hDiracDecBin[pos_idx]->useTdDecorr = st_ivas->hDiracDecBin[0]->useTdDecorr;
     896             :                 }
     897             : 
     898      151111 :                 if ( !st_ivas->hDiracDecBin[0]->useTdDecorr )
     899             :                 {
     900       95218 :                     if ( st_ivas->hDiracDecBin[0]->h_freq_domain_decorr_ap_params == NULL )
     901             :                     {
     902             :                         float frequency_axis[CLDFB_NO_CHANNELS_MAX];
     903             : 
     904       80535 :                         ivas_dirac_dec_get_frequency_axis( frequency_axis, st_ivas->hDecoderConfig->output_Fs, st_ivas->hSpatParamRendCom->num_freq_bands );
     905             : 
     906       80535 :                         if ( ( error = ivas_dirac_dec_decorr_open(
     907       80535 :                                    &( st_ivas->hDiracDecBin[0]->h_freq_domain_decorr_ap_params ), &( st_ivas->hDiracDecBin[0]->h_freq_domain_decorr_ap_state ), st_ivas->hSpatParamRendCom->num_freq_bands, BINAURAL_CHANNELS, BINAURAL_CHANNELS,
     908       80535 :                                    DIRAC_SYNTHESIS_PSD_LS, frequency_axis, BINAURAL_CHANNELS, st_ivas->hDecoderConfig->output_Fs ) ) != IVAS_ERR_OK )
     909             :                         {
     910           0 :                             return error;
     911             :                         }
     912             :                     }
     913             :                 }
     914             : 
     915      302222 :                 for ( pos_idx = 0; pos_idx < num_poses; pos_idx++ )
     916             :                 {
     917      151111 :                     st_ivas->hDiracDecBin[pos_idx]->reqularizationFactor = configure_reqularization_factor( st_ivas->ivas_format, st_ivas->hDecoderConfig->ivas_total_brate );
     918             :                 }
     919             :             }
     920             :         }
     921             :     }
     922             : 
     923             : 
     924      346833 :     return error;
     925             : }
     926             : 
     927             : 
     928             : /*-------------------------------------------------------------------------
     929             :  * ivas_dirac_dec_close()
     930             :  *
     931             :  * Close DirAC memories
     932             :  *------------------------------------------------------------------------*/
     933             : 
     934      138969 : void ivas_dirac_dec_close(
     935             :     DIRAC_DEC_HANDLE *hDirAC_out )
     936             : {
     937             :     DIRAC_DEC_HANDLE hDirAC;
     938             : 
     939      138969 :     if ( hDirAC_out == NULL || *hDirAC_out == NULL )
     940             :     {
     941       69279 :         return;
     942             :     }
     943             : 
     944       69690 :     hDirAC = *hDirAC_out;
     945             : 
     946             :     /* Config & CLDFB */
     947       69690 :     if ( hDirAC->hConfig != NULL )
     948             :     {
     949       69690 :         free( hDirAC->hConfig );
     950       69690 :         hDirAC->hConfig = NULL;
     951             :     }
     952             : 
     953       69690 :     free( *hDirAC_out );
     954       69690 :     *hDirAC_out = NULL;
     955             : 
     956       69690 :     return;
     957             : }
     958             : 
     959             : 
     960             : /*-------------------------------------------------------------------------
     961             :  * ivas_dirac_dec_read_BS()
     962             :  *
     963             :  * Read DirAC parameters from the bitstream
     964             :  *------------------------------------------------------------------------*/
     965             : 
     966     9546141 : void ivas_dirac_dec_read_BS(
     967             :     const int32_t ivas_total_brate,                       /* i  : IVAS total bitrate              */
     968             :     Decoder_State *st,                                    /* i/o: decoder state structure         */
     969             :     DIRAC_DEC_HANDLE hDirAC,                              /* i/o: decoder DirAC handle            */
     970             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom, /* i/o: common spatial rendering data handle    */
     971             :     IVAS_QMETADATA_HANDLE hQMetaData,                     /* i/o: q_metadata                      */
     972             :     int16_t *nb_bits,                                     /* o  : number of bits read             */
     973             :     const int16_t last_bit_pos,                           /* i  : last read bitstream position    */
     974             :     const int16_t hodirac_flag,                           /* i  : flag to indicate HO-DirAC mode  */
     975             :     const int16_t nchan_transport,                        /* i  : number of transport channels    */
     976             :     int16_t *dirac_to_spar_md_bands                       /* o  : DirAC->SPAR MD bands            */
     977             : )
     978             : {
     979             :     int16_t i, j, b, dir, orig_dirac_bands;
     980             :     int16_t next_bit_pos_orig;
     981     9546141 :     if ( !st->bfi && ivas_total_brate > IVAS_SID_5k2 )
     982             :     {
     983     8698653 :         next_bit_pos_orig = st->next_bit_pos;
     984     8698653 :         st->next_bit_pos = (int16_t) ( ivas_total_brate / FRAMES_PER_SEC - 1 );
     985     8698653 :         if ( last_bit_pos > 0 )
     986             :         {
     987     2502474 :             st->next_bit_pos = last_bit_pos;
     988             :         }
     989             :         /* 1 bit flag for signaling metadata to read */
     990     8698653 :         b = st->bit_stream[( st->next_bit_pos )--];
     991     8698653 :         ( *nb_bits )++;
     992             : 
     993     8698653 :         if ( b == 1 ) /* WB 4TCs condition, no other metadata to read*/
     994             :         {
     995       11884 :             orig_dirac_bands = hQMetaData->q_direction[0].cfg.nbands;
     996             : 
     997       11884 :             hQMetaData->sba_inactive_mode = 1;
     998             : 
     999             :             /* if we start with a SID frame, we need to init the azi/ele arrays.*/
    1000       11884 :             if ( st->ini_frame == 0 )
    1001             :             {
    1002         270 :                 for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
    1003             :                 {
    1004         198 :                     set_zero( hQMetaData->q_direction[0].band_data[b].azimuth, MAX_PARAM_SPATIAL_SUBFRAMES );
    1005         198 :                     set_zero( hQMetaData->q_direction[0].band_data[b].elevation, MAX_PARAM_SPATIAL_SUBFRAMES );
    1006             :                 }
    1007             :             }
    1008             : 
    1009       11884 :             *nb_bits += ivas_qmetadata_dec_sid_decode( hQMetaData, st->bit_stream, &( st->next_bit_pos ), nchan_transport, NULL, SBA_FORMAT );
    1010             : 
    1011       59420 :             for ( i = 0; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
    1012             :             {
    1013       47536 :                 hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].azimuth[i] = hQMetaData->q_direction[0].band_data[1].azimuth[0];
    1014       47536 :                 hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].elevation[i] = hQMetaData->q_direction[0].band_data[1].elevation[0];
    1015       47536 :                 hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].energy_ratio[i] = hQMetaData->q_direction[0].band_data[1].energy_ratio[0];
    1016       47536 :                 hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].energy_ratio_index[i] = hQMetaData->q_direction[0].band_data[1].energy_ratio_index[0];
    1017             :             }
    1018       59420 :             for ( i = 0; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
    1019             :             {
    1020      140336 :                 for ( j = orig_dirac_bands - 2; j >= 0; j-- )
    1021             :                 {
    1022       92800 :                     hQMetaData->q_direction[0].band_data[j].azimuth[i] = hQMetaData->q_direction[0].band_data[0].azimuth[0];
    1023       92800 :                     hQMetaData->q_direction[0].band_data[j].elevation[i] = hQMetaData->q_direction[0].band_data[0].elevation[0];
    1024       92800 :                     hQMetaData->q_direction[0].band_data[j].energy_ratio[i] = hQMetaData->q_direction[0].band_data[0].energy_ratio[0];
    1025       92800 :                     hQMetaData->q_direction[0].band_data[j].energy_ratio_index[i] = hQMetaData->q_direction[0].band_data[0].energy_ratio_index[0];
    1026             :                 }
    1027             :             }
    1028             : 
    1029       11884 :             hQMetaData->q_direction->cfg.nbands = orig_dirac_bands;
    1030             :         }
    1031             :         else
    1032             :         {
    1033     8686769 :             hQMetaData->sba_inactive_mode = 0;
    1034     8686769 :             hQMetaData->is_masa_ivas_format = 0;
    1035     8686769 :             if ( hQMetaData->useLowerRes )
    1036             :             {
    1037      645914 :                 hQMetaData->q_direction[0].cfg.nblocks = 1;
    1038             :             }
    1039             :             else
    1040             :             {
    1041     8040855 :                 hQMetaData->q_direction[0].cfg.nblocks = MAX_PARAM_SPATIAL_SUBFRAMES;
    1042             :             }
    1043             : 
    1044     8686769 :             *nb_bits += ivas_qmetadata_dec_decode( hQMetaData, st->bit_stream, &( st->next_bit_pos ), hodirac_flag );
    1045             :         }
    1046             : 
    1047             : #ifdef DEBUGGING
    1048             :         assert( *nb_bits >= 0 );
    1049             : #endif
    1050             : 
    1051     8698653 :         st->next_bit_pos = next_bit_pos_orig;
    1052             :     }
    1053      847488 :     else if ( !st->bfi && ivas_total_brate == IVAS_SID_5k2 )
    1054             :     {
    1055        3344 :         next_bit_pos_orig = st->next_bit_pos;
    1056             : 
    1057             :         /* subtract mode signaling bits, since bitstream was moved after mode reading */
    1058        3344 :         st->next_bit_pos = (int16_t) ( ivas_total_brate / FRAMES_PER_SEC - 1 - SID_FORMAT_NBITS - SBA_PLANAR_BITS - SBA_ORDER_BITS );
    1059             : 
    1060             :         /* 1 bit flag for signaling metadata to read */
    1061        3344 :         b = st->bit_stream[( st->next_bit_pos )--];
    1062        3344 :         ( *nb_bits )++;
    1063        3344 :         hQMetaData->sba_inactive_mode = 1;
    1064        3344 :         orig_dirac_bands = hQMetaData->q_direction[0].cfg.nbands;
    1065             : 
    1066             :         /* if we start with a SID frame, we need to init the azi/ele arrays.*/
    1067        3344 :         if ( st->ini_frame == 0 )
    1068             :         {
    1069           0 :             for ( dir = 0; dir < hQMetaData->no_directions; dir++ )
    1070             :             {
    1071           0 :                 for ( b = 0; b < hQMetaData->q_direction[dir].cfg.nbands; b++ )
    1072             :                 {
    1073           0 :                     set_zero( hQMetaData->q_direction[dir].band_data[b].azimuth, MAX_PARAM_SPATIAL_SUBFRAMES );
    1074           0 :                     set_zero( hQMetaData->q_direction[dir].band_data[b].elevation, MAX_PARAM_SPATIAL_SUBFRAMES );
    1075             :                 }
    1076             :             }
    1077             :         }
    1078             : 
    1079        3344 :         *nb_bits += ivas_qmetadata_dec_sid_decode( hQMetaData, st->bit_stream, &( st->next_bit_pos ), nchan_transport, NULL, SBA_FORMAT );
    1080             : 
    1081       16720 :         for ( i = 0; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
    1082             :         {
    1083       13376 :             hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].azimuth[i] = hQMetaData->q_direction[0].band_data[1].azimuth[0];
    1084       13376 :             hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].elevation[i] = hQMetaData->q_direction[0].band_data[1].elevation[0];
    1085       13376 :             hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].energy_ratio[i] = hQMetaData->q_direction[0].band_data[1].energy_ratio[0];
    1086       13376 :             hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].energy_ratio_index[i] = hQMetaData->q_direction[0].band_data[1].energy_ratio_index[0];
    1087             :         }
    1088       16720 :         for ( i = 0; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
    1089             :         {
    1090       39508 :             for ( j = orig_dirac_bands - 2; j >= 0; j-- )
    1091             :             {
    1092       26132 :                 hQMetaData->q_direction[0].band_data[j].azimuth[i] = hQMetaData->q_direction[0].band_data[0].azimuth[0];
    1093       26132 :                 hQMetaData->q_direction[0].band_data[j].elevation[i] = hQMetaData->q_direction[0].band_data[0].elevation[0];
    1094       26132 :                 hQMetaData->q_direction[0].band_data[j].energy_ratio[i] = hQMetaData->q_direction[0].band_data[0].energy_ratio[0];
    1095       26132 :                 hQMetaData->q_direction[0].band_data[j].energy_ratio_index[i] = hQMetaData->q_direction[0].band_data[0].energy_ratio_index[0];
    1096             :             }
    1097             :         }
    1098             : 
    1099        3344 :         hQMetaData->q_direction->cfg.nbands = orig_dirac_bands;
    1100             : 
    1101        3344 :         st->next_bit_pos = next_bit_pos_orig;
    1102             :     }
    1103             : 
    1104     9546141 :     if ( hDirAC != NULL && hSpatParamRendCom != NULL )
    1105             :     {
    1106     5020996 :         ivas_qmetadata_to_dirac( hQMetaData, hDirAC, NULL, hSpatParamRendCom, ivas_total_brate, SBA_FORMAT, hodirac_flag, dirac_to_spar_md_bands );
    1107             :     }
    1108             : 
    1109     9546141 :     return;
    1110             : }
    1111             : 
    1112             : 
    1113             : /*-----------------------------------------------------------------------*
    1114             :  * ivas_qmetadata_to_dirac()
    1115             :  *
    1116             :  * Copy qmetedata to DirAC parameters for rendering
    1117             :  *-----------------------------------------------------------------------*/
    1118             : 
    1119     8296639 : void ivas_qmetadata_to_dirac(
    1120             :     const IVAS_QMETADATA_HANDLE hQMetaData,               /* i  : frame of MASA q_metadata        */
    1121             :     DIRAC_DEC_HANDLE hDirAC,                              /* i  : DirAC decoder structure         */
    1122             :     MASA_DECODER_HANDLE hMasa,                            /* i  : MASA decoder structure          */
    1123             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom, /* i/o: common spatial renderer data handle     */
    1124             :     const int32_t ivas_total_brate,                       /* i  : IVAS total bitrate              */
    1125             :     const IVAS_FORMAT ivas_format,                        /* i  : IVAS format                     */
    1126             :     const int16_t hodirac_flag,                           /* i  : flag to indicate HO-DirAC mode  */
    1127             :     int16_t *dirac_to_spar_md_bands                       /* o  : DirAC->SPAR MD bands            */
    1128             : )
    1129             : {
    1130             :     int16_t block, band;
    1131             :     int16_t *seed_ptr;
    1132             :     int16_t band_start, band_end, diff_idx;
    1133             :     float diffuseness;
    1134             :     int16_t b, ele, azi;
    1135             :     float azimuth, elevation;
    1136             :     IVAS_QDIRECTION *q_direction;
    1137             :     int16_t *band_mapping;
    1138             :     int16_t *band_grouping;
    1139             :     int16_t start_band;
    1140     8296639 :     int16_t nbands = 0;
    1141     8296639 :     int16_t nblocks = 0;
    1142             :     int16_t qBand_idx;
    1143     8296639 :     int16_t idx_sec = 0;
    1144     8296639 :     int16_t no_secs = 1;
    1145             : 
    1146     8296639 :     q_direction = &( hQMetaData->q_direction[0] );
    1147     8296639 :     hSpatParamRendCom->numParametricDirections = hQMetaData->no_directions;
    1148     8296639 :     hSpatParamRendCom->numSimultaneousDirections = hSpatParamRendCom->numParametricDirections + hSpatParamRendCom->numIsmDirections;
    1149             : 
    1150     8296639 :     if ( hMasa != NULL && ivas_total_brate > IVAS_SID_5k2 )
    1151     3260275 :     {
    1152             :         int16_t meta_write_index;
    1153     3260275 :         band_mapping = hMasa->data.band_mapping;
    1154             : 
    1155    16301375 :         for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; ++block )
    1156             :         {
    1157    13041100 :             meta_write_index = ( hSpatParamRendCom->dirac_bs_md_write_idx + block ) % hSpatParamRendCom->dirac_md_buffer_length;
    1158             : 
    1159   124932968 :             for ( band = 0; band < hMasa->config.numCodingBands; ++band )
    1160             :             {
    1161   791497228 :                 for ( b = MASA_band_grouping_24[band_mapping[band]]; b < MASA_band_grouping_24[band_mapping[band + 1]]; ++b )
    1162             :                 {
    1163   679605360 :                     hSpatParamRendCom->azimuth[meta_write_index][b] = (int16_t) q_direction->band_data[band].azimuth[block];
    1164   679605360 :                     hSpatParamRendCom->elevation[meta_write_index][b] = (int16_t) q_direction->band_data[band].elevation[block];
    1165   679605360 :                     hSpatParamRendCom->energy_ratio1[meta_write_index][b] = q_direction->band_data[band].energy_ratio[block];
    1166   679605360 :                     hSpatParamRendCom->diffuseness_vector[meta_write_index][b] = 1.0f - q_direction->band_data[band].energy_ratio[block];
    1167             : 
    1168   679605360 :                     if ( q_direction->coherence_band_data != NULL )
    1169             :                     {
    1170   407783440 :                         hSpatParamRendCom->spreadCoherence[meta_write_index][b] = q_direction->coherence_band_data[band].spread_coherence[block] / 255.0f;
    1171             :                     }
    1172             :                     else
    1173             :                     {
    1174   271821920 :                         hSpatParamRendCom->spreadCoherence[meta_write_index][b] = 0.0f;
    1175             :                     }
    1176             : 
    1177   679605360 :                     if ( hQMetaData->surcoh_band_data != NULL )
    1178             :                     {
    1179   407783440 :                         hSpatParamRendCom->surroundingCoherence[meta_write_index][b] = hQMetaData->surcoh_band_data[band].surround_coherence[block] / 255.0f;
    1180             :                     }
    1181             :                     else
    1182             :                     {
    1183   271821920 :                         hSpatParamRendCom->surroundingCoherence[meta_write_index][b] = 0.0f;
    1184             :                     }
    1185             :                 }
    1186             :             }
    1187             :         }
    1188             : 
    1189     3260275 :         if ( hQMetaData->no_directions == 2 )
    1190             :         {
    1191      402236 :             q_direction = &( hQMetaData->q_direction[1] );
    1192     2011180 :             for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; ++block )
    1193             :             {
    1194     1608944 :                 meta_write_index = ( hSpatParamRendCom->dirac_bs_md_write_idx + block ) % hSpatParamRendCom->dirac_md_buffer_length;
    1195             : 
    1196    20494444 :                 for ( band = 0; band < hMasa->config.numCodingBands; ++band )
    1197             :                 {
    1198   107772140 :                     for ( b = MASA_band_grouping_24[band_mapping[band]]; b < MASA_band_grouping_24[band_mapping[band + 1]]; ++b )
    1199             :                     {
    1200    88886640 :                         hSpatParamRendCom->azimuth2[meta_write_index][b] = (int16_t) q_direction->band_data[band].azimuth[block];
    1201    88886640 :                         hSpatParamRendCom->elevation2[meta_write_index][b] = (int16_t) q_direction->band_data[band].elevation[block];
    1202    88886640 :                         hSpatParamRendCom->energy_ratio2[meta_write_index][b] = q_direction->band_data[band].energy_ratio[block];
    1203    88886640 :                         hSpatParamRendCom->diffuseness_vector[meta_write_index][b] -= q_direction->band_data[band].energy_ratio[block];
    1204             : 
    1205             :                         /* Sanitize diffuseness for rare cases where floating point inaccuracy could result in negative diffuseness. */
    1206    88886640 :                         if ( hSpatParamRendCom->diffuseness_vector[meta_write_index][b] < 0.0f )
    1207             :                         {
    1208       18657 :                             hSpatParamRendCom->diffuseness_vector[meta_write_index][b] = 0.0f;
    1209             :                         }
    1210             : 
    1211    88886640 :                         if ( q_direction->coherence_band_data != NULL )
    1212             :                         {
    1213    88886640 :                             hSpatParamRendCom->spreadCoherence2[meta_write_index][b] = q_direction->coherence_band_data[band].spread_coherence[block] / 255.0f;
    1214             :                         }
    1215             :                         else
    1216             :                         {
    1217           0 :                             hSpatParamRendCom->spreadCoherence2[meta_write_index][b] = 0.0f;
    1218             :                         }
    1219             :                     }
    1220             :                 }
    1221             :             }
    1222             :         }
    1223     2858039 :         else if ( hSpatParamRendCom->azimuth2 != NULL && hSpatParamRendCom->elevation2 != NULL && hSpatParamRendCom->energy_ratio2 != NULL && hSpatParamRendCom->spreadCoherence2 != NULL )
    1224             :         {
    1225             :             /* zero out old dir2 data */
    1226    11904755 :             for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; ++block )
    1227             :             {
    1228     9523804 :                 meta_write_index = ( hSpatParamRendCom->dirac_bs_md_write_idx + block ) % hSpatParamRendCom->dirac_md_buffer_length;
    1229     9523804 :                 set_s( hSpatParamRendCom->azimuth2[meta_write_index], 0, hSpatParamRendCom->num_freq_bands );
    1230     9523804 :                 set_s( hSpatParamRendCom->elevation2[meta_write_index], 0, hSpatParamRendCom->num_freq_bands );
    1231     9523804 :                 set_zero( hSpatParamRendCom->energy_ratio2[meta_write_index], hSpatParamRendCom->num_freq_bands );
    1232     9523804 :                 set_zero( hSpatParamRendCom->spreadCoherence2[meta_write_index], hSpatParamRendCom->num_freq_bands );
    1233             :             }
    1234             :         }
    1235             :     }
    1236             :     else /* SBA mode/SID/Zero frame*/
    1237             :     {
    1238             :         int16_t tmp_write_idx_param_band;
    1239             :         int16_t tmp_write_idx_band;
    1240     5036364 :         float diffuseness_sec = 0.f;
    1241             : 
    1242             :         /* ungroup */
    1243     5036364 :         seed_ptr = &hDirAC->dithering_seed;
    1244     5036364 :         nblocks = q_direction->cfg.nblocks;
    1245     5036364 :         nbands = hDirAC->band_grouping[hDirAC->hConfig->nbands];
    1246     5036364 :         band_grouping = hDirAC->band_grouping;
    1247             : 
    1248     5036364 :         if ( ivas_total_brate <= IVAS_SID_5k2 && ivas_format != SBA_FORMAT )
    1249             :         {
    1250             :             /* SID/zero-frame: 1 direction, 5 bands, nblocks re-generated out of SID decoder*/
    1251       15368 :             start_band = 0;
    1252       15368 :             hDirAC->hConfig->nbands = 5;
    1253             : 
    1254       15368 :             ivas_dirac_config_bands( hDirAC->band_grouping, hDirAC->hConfig->nbands, nbands, NULL, 0, 0, NULL, 1 );
    1255             : 
    1256       15368 :             nbands = 5;
    1257             :         }
    1258             :         else
    1259             :         {
    1260     5020996 :             start_band = hDirAC->hConfig->enc_param_start_band;
    1261     5020996 :             if ( ivas_format == SBA_FORMAT )
    1262             :             {
    1263     5020996 :                 hDirAC->hConfig->nbands = IVAS_MAX_NUM_BANDS;
    1264             :             }
    1265             :             else
    1266             :             {
    1267           0 :                 hDirAC->hConfig->nbands = q_direction->cfg.nbands;
    1268             :             }
    1269             : 
    1270     5020996 :             ivas_dirac_config_bands( hDirAC->band_grouping, hDirAC->hConfig->nbands, nbands, dirac_to_spar_md_bands, hQMetaData->useLowerBandRes, hDirAC->hConfig->enc_param_start_band, hDirAC->hFbMdft, 0 );
    1271             : 
    1272     5020996 :             nbands = hDirAC->hConfig->nbands;
    1273     5020996 :             if ( hQMetaData->q_direction[0].cfg.nblocks == 0 )
    1274             :             {
    1275             :                 /* No transmission -> no copy from qmetadata buffers*/
    1276           0 :                 nbands = start_band;
    1277             :             }
    1278             :         }
    1279             : 
    1280             :         /* Low-Bands with no spatial data transmitted, analysis at decoder side */
    1281    40236468 :         for ( band = 0; band < start_band; band++ )
    1282             :         {
    1283    35200104 :             band_start = band_grouping[band];
    1284    35200104 :             band_end = band_grouping[band + 1];
    1285    35200104 :             tmp_write_idx_param_band = hSpatParamRendCom->dirac_bs_md_write_idx;
    1286             : 
    1287   176000520 :             for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; block++ )
    1288             :             {
    1289   334400988 :                 for ( b = band_start; b < band_end; b++ )
    1290             :                 {
    1291   193600572 :                     tmp_write_idx_band = tmp_write_idx_param_band;
    1292   193600572 :                     hSpatParamRendCom->spreadCoherence[block][b] = 0.0f;
    1293   193600572 :                     hSpatParamRendCom->surroundingCoherence[block][b] = 0.0f;
    1294             : 
    1295   193600572 :                     hSpatParamRendCom->elevation[tmp_write_idx_band][b] = 0;
    1296   193600572 :                     hSpatParamRendCom->azimuth[tmp_write_idx_band][b] = 0;
    1297   193600572 :                     hSpatParamRendCom->diffuseness_vector[tmp_write_idx_band][b] = 0.f;
    1298             : 
    1299   193600572 :                     hSpatParamRendCom->spreadCoherence[tmp_write_idx_band][b] = 0.0f;
    1300   193600572 :                     hSpatParamRendCom->surroundingCoherence[tmp_write_idx_band][b] = 0.0f;
    1301   193600572 :                     hSpatParamRendCom->energy_ratio1[tmp_write_idx_band][b] = 0;
    1302   193600572 :                     tmp_write_idx_band = ( tmp_write_idx_band + 1 ) % hSpatParamRendCom->dirac_md_buffer_length;
    1303             :                 }
    1304             :             }
    1305             :         }
    1306             : 
    1307             :         /* Bands with spatial data transmitted */
    1308     5036364 :         if ( hodirac_flag )
    1309             :         {
    1310      620983 :             no_secs = DIRAC_HO_NUMSECTORS;
    1311             :         }
    1312             : 
    1313    10693711 :         for ( idx_sec = 0; idx_sec < no_secs; idx_sec++ )
    1314             :         {
    1315    38237831 :             for ( band = start_band; band < nbands; band++ )
    1316             :             {
    1317    32580484 :                 band_start = band_grouping[band];
    1318    32580484 :                 band_end = band_grouping[band + 1];
    1319    32580484 :                 tmp_write_idx_param_band = hSpatParamRendCom->dirac_bs_md_write_idx;
    1320             : 
    1321    32580484 :                 if ( ivas_format == SBA_FORMAT )
    1322             :                 {
    1323    32503644 :                     qBand_idx = dirac_to_spar_md_bands[band] - start_band;
    1324             :                 }
    1325             :                 else
    1326             :                 {
    1327       76840 :                     qBand_idx = band;
    1328             :                 }
    1329             : 
    1330    32580484 :                 diffuseness = 1.0f - q_direction->band_data[qBand_idx].energy_ratio[0];
    1331             : #ifdef DEBUG_MODE_DIRAC
    1332             :                 dbgwrite( &diffuseness, sizeof( float ), 1, 1, "./res/dirac_dec_diffuseness.dat" );
    1333             : #endif
    1334    32580484 :                 diff_idx = q_direction->band_data[qBand_idx].energy_ratio_index[0];
    1335             : 
    1336   162902420 :                 for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; block++ )
    1337             :                 {
    1338             :                     int16_t block_qmetadata;
    1339             : 
    1340   130321936 :                     block_qmetadata = min( block, nblocks - 1 );
    1341   130321936 :                     block_qmetadata = max( block_qmetadata, 0 );
    1342             : 
    1343   130321936 :                     if ( q_direction[idx_sec].band_data[qBand_idx].azimuth[block_qmetadata] < 0.f )
    1344             :                     {
    1345    51201446 :                         q_direction[idx_sec].band_data[qBand_idx].azimuth[block_qmetadata] += 360.f;
    1346             :                     }
    1347             : 
    1348   130321936 :                     if ( hMasa == NULL && hodirac_flag )
    1349             :                     {
    1350    59614368 :                         azimuth = q_direction[idx_sec].band_data[qBand_idx].azimuth[block_qmetadata];
    1351    59614368 :                         elevation = q_direction[idx_sec].band_data[qBand_idx].elevation[block_qmetadata];
    1352    59614368 :                         diffuseness = 1.f - q_direction[0].band_data[qBand_idx].energy_ratio[block_qmetadata];
    1353    59614368 :                         diffuseness_sec = q_direction[1].band_data[qBand_idx].energy_ratio[block_qmetadata];
    1354   116265408 :                         assert( diffuseness_sec < 1.0001f && diffuseness_sec > -0.0001f );
    1355             :                     }
    1356             :                     else
    1357             :                     {
    1358    70707568 :                         azimuth = q_direction->band_data[qBand_idx].azimuth[block_qmetadata];
    1359    70707568 :                         elevation = q_direction->band_data[qBand_idx].elevation[block_qmetadata];
    1360             :                     }
    1361             : 
    1362  1003032804 :                     for ( b = band_start; b < band_end; b++ )
    1363             :                     {
    1364   872710868 :                         tmp_write_idx_band = tmp_write_idx_param_band;
    1365             : 
    1366   872710868 :                         if ( hodirac_flag )
    1367             :                         {
    1368   263473440 :                             azi = (int16_t) ( azimuth + 0.5f );
    1369   263473440 :                             ele = (int16_t) ( elevation + 0.5f );
    1370             :                         }
    1371             :                         else
    1372             :                         {
    1373   609237428 :                             azi = (int16_t) ( azimuth + rand_triangular_signed( seed_ptr ) * dirac_dithering_azi_scale[diff_idx] + 0.5f );
    1374   609237428 :                             ele = (int16_t) ( elevation + rand_triangular_signed( seed_ptr ) * dirac_dithering_ele_scale[diff_idx] + 0.5f );
    1375             :                             /* limit the elevation to [-90, 90] */
    1376   609237428 :                             ele = min( 90, ele );
    1377   609237428 :                             ele = max( -90, ele );
    1378             :                         }
    1379             : 
    1380   872710868 :                         if ( ivas_total_brate > IVAS_SID_5k2 && q_direction->coherence_band_data != NULL )
    1381             :                         {
    1382           0 :                             hSpatParamRendCom->spreadCoherence[tmp_write_idx_band][b] = q_direction->coherence_band_data[qBand_idx].spread_coherence[block] / 255.0f;
    1383             :                         }
    1384             :                         else
    1385             :                         {
    1386   872710868 :                             hSpatParamRendCom->spreadCoherence[tmp_write_idx_band][b] = 0.0f;
    1387             :                         }
    1388             : 
    1389   872710868 :                         if ( ivas_total_brate > IVAS_SID_5k2 && q_direction->coherence_band_data != NULL )
    1390             :                         {
    1391           0 :                             hSpatParamRendCom->surroundingCoherence[tmp_write_idx_band][b] = hQMetaData->surcoh_band_data[qBand_idx].surround_coherence[0] / 255.0f;
    1392             :                         }
    1393             :                         else
    1394             :                         {
    1395   872710868 :                             hSpatParamRendCom->surroundingCoherence[tmp_write_idx_band][b] = 0.0f;
    1396             :                         }
    1397             : 
    1398   872710868 :                         hSpatParamRendCom->energy_ratio1[tmp_write_idx_band][b] = q_direction->band_data[qBand_idx].energy_ratio[0];
    1399             : 
    1400   872710868 :                         hSpatParamRendCom->diffuseness_vector[tmp_write_idx_band][b] = diffuseness;
    1401             : 
    1402   872710868 :                         if ( hodirac_flag )
    1403             :                         {
    1404   263473440 :                             if ( idx_sec == 0 )
    1405             :                             {
    1406   131736720 :                                 hSpatParamRendCom->elevation[tmp_write_idx_band][b] = ele;
    1407   131736720 :                                 hSpatParamRendCom->azimuth[tmp_write_idx_band][b] = azi;
    1408   131736720 :                                 hSpatParamRendCom->energy_ratio1[tmp_write_idx_band][b] = 0.f; // not in use
    1409             :                             }
    1410             :                             else
    1411             :                             {
    1412   131736720 :                                 assert( idx_sec == 1 );
    1413   131736720 :                                 hSpatParamRendCom->elevation2[tmp_write_idx_band][b] = ele;
    1414   131736720 :                                 hSpatParamRendCom->azimuth2[tmp_write_idx_band][b] = azi;
    1415   131736720 :                                 hSpatParamRendCom->energy_ratio2[tmp_write_idx_band][b] = 1.f - diffuseness_sec;
    1416             :                             }
    1417             :                         }
    1418             :                         else
    1419             :                         {
    1420   609237428 :                             hSpatParamRendCom->elevation[tmp_write_idx_band][b] = ele;
    1421   609237428 :                             hSpatParamRendCom->azimuth[tmp_write_idx_band][b] = azi;
    1422             :                         }
    1423             :                     }
    1424   130321936 :                     tmp_write_idx_param_band = ( tmp_write_idx_param_band + 1 ) % hSpatParamRendCom->dirac_md_buffer_length;
    1425             : 
    1426             :                 } /* for ( block =...) */
    1427             :             }     /* for ( band = ...) */
    1428             :         }         /* for ( idx_sec = ...)*/
    1429             : 
    1430             :         /* Bands not transmitted -> zeroed*/
    1431     5036364 :         for ( b = band_grouping[band]; b < hSpatParamRendCom->num_freq_bands; b++ )
    1432             :         {
    1433           0 :             tmp_write_idx_band = hSpatParamRendCom->dirac_bs_md_write_idx;
    1434             : 
    1435           0 :             for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; block++ )
    1436             :             {
    1437             : 
    1438           0 :                 hSpatParamRendCom->spreadCoherence[block][b] = 0.0f;
    1439           0 :                 hSpatParamRendCom->surroundingCoherence[block][b] = 0.0f;
    1440           0 :                 hSpatParamRendCom->energy_ratio1[block][b] = 0;
    1441             : 
    1442           0 :                 hSpatParamRendCom->elevation[tmp_write_idx_band][b] = 0;
    1443           0 :                 hSpatParamRendCom->azimuth[tmp_write_idx_band][b] = 0;
    1444           0 :                 hSpatParamRendCom->diffuseness_vector[tmp_write_idx_band][b] = 0.f;
    1445           0 :                 hSpatParamRendCom->spreadCoherence[tmp_write_idx_band][b] = 0.0f;
    1446           0 :                 hSpatParamRendCom->surroundingCoherence[tmp_write_idx_band][b] = 0.0f;
    1447           0 :                 hSpatParamRendCom->energy_ratio1[tmp_write_idx_band][b] = 0;
    1448           0 :                 tmp_write_idx_band = ( tmp_write_idx_band + 1 ) % hSpatParamRendCom->dirac_md_buffer_length;
    1449             :             }
    1450             :         }
    1451             :     }
    1452             : 
    1453             :     /* update buffer write index */
    1454     8296639 :     hSpatParamRendCom->dirac_bs_md_write_idx = ( hSpatParamRendCom->dirac_bs_md_write_idx + MAX_PARAM_SPATIAL_SUBFRAMES ) % hSpatParamRendCom->dirac_md_buffer_length;
    1455             : 
    1456     8296639 :     return;
    1457             : }
    1458             : 
    1459             : 
    1460             : /*-------------------------------------------------------------------------
    1461             :  * ivas_dirac_dec_set_md_map()
    1462             :  *
    1463             :  * Set metadata index mapping for DirAC
    1464             :  *------------------------------------------------------------------------*/
    1465             : 
    1466     8736792 : void ivas_dirac_dec_set_md_map(
    1467             :     Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure     */
    1468             :     const int16_t nCldfbTs   /* i  : number of CLDFB time slots */
    1469             : )
    1470             : {
    1471             :     int16_t num_slots_in_subfr;
    1472             :     DIRAC_DEC_HANDLE hDirAC;
    1473             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
    1474             : 
    1475     8736792 :     hDirAC = st_ivas->hDirAC;
    1476     8736792 :     hSpatParamRendCom = st_ivas->hSpatParamRendCom;
    1477             : #ifdef DEBUGGING
    1478             :     assert( hSpatParamRendCom );
    1479             : #endif
    1480             : 
    1481             :     /* adapt subframes */
    1482     8736792 :     hSpatParamRendCom->num_slots = nCldfbTs;
    1483     8736792 :     hSpatParamRendCom->slots_rendered = 0;
    1484     8736792 :     num_slots_in_subfr = CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES;
    1485     8736792 :     hSpatParamRendCom->subframes_rendered = 0;
    1486             : 
    1487     8736792 :     ivas_jbm_dec_get_adapted_subframes( nCldfbTs, hSpatParamRendCom->subframe_nbslots, &hSpatParamRendCom->nb_subframes );
    1488             : 
    1489             :     /* copy also to tc buffer */
    1490             :     /* only for non-combined formats and combinded formats w/o discrete objects */
    1491     8736792 :     if ( ( st_ivas->ivas_format != MASA_ISM_FORMAT || st_ivas->ism_mode != ISM_MASA_MODE_DISC ) && !( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_SBA_MODE_DISC ) )
    1492             :     {
    1493     7095492 :         st_ivas->hTcBuffer->nb_subframes = hSpatParamRendCom->nb_subframes;
    1494     7095492 :         mvs2s( hSpatParamRendCom->subframe_nbslots, st_ivas->hTcBuffer->subframe_nbslots, hSpatParamRendCom->nb_subframes );
    1495             :     }
    1496             : 
    1497             :     /* set mapping according to dirac_read_idx */
    1498             : 
    1499     8736792 :     set_s( hSpatParamRendCom->render_to_md_map, 0, MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME );
    1500             : 
    1501     8736792 :     if ( st_ivas->ivas_format == MASA_FORMAT )
    1502             :     {
    1503     1877527 :         ivas_jbm_dec_get_md_map_even_spacing( nCldfbTs, num_slots_in_subfr, 0, hSpatParamRendCom->dirac_md_buffer_length, hSpatParamRendCom->render_to_md_map );
    1504             :     }
    1505     6859265 :     else if ( hDirAC == NULL || hDirAC->hConfig == NULL || hDirAC->hConfig->dec_param_estim == 0 )
    1506             :     {
    1507     4084548 :         ivas_jbm_dec_get_md_map( DEFAULT_JBM_CLDFB_TIMESLOTS, nCldfbTs, num_slots_in_subfr, 0, hSpatParamRendCom->dirac_md_buffer_length, hSpatParamRendCom->render_to_md_map );
    1508             :     }
    1509             :     else
    1510             :     {
    1511     2774717 :         ivas_jbm_dec_get_md_map( DEFAULT_JBM_CLDFB_TIMESLOTS, nCldfbTs, num_slots_in_subfr, hSpatParamRendCom->dirac_read_idx, hSpatParamRendCom->dirac_md_buffer_length, hSpatParamRendCom->render_to_md_map );
    1512             :     }
    1513             : 
    1514     8736792 :     if ( hDirAC == NULL || hDirAC->hConfig == NULL || hDirAC->hConfig->dec_param_estim == 0 )
    1515             :     {
    1516             :         float tmp;
    1517             :         int16_t sf_idx, slot_idx, slot_idx_abs;
    1518             : 
    1519     5962075 :         slot_idx_abs = 0;
    1520    30115916 :         for ( sf_idx = 0; sf_idx < hSpatParamRendCom->nb_subframes; sf_idx++ )
    1521             :         {
    1522    24153841 :             tmp = 0.0f;
    1523   119558202 :             for ( slot_idx = 0; slot_idx < hSpatParamRendCom->subframe_nbslots[sf_idx]; slot_idx++ )
    1524             :             {
    1525    95404361 :                 tmp += (float) hSpatParamRendCom->render_to_md_map[slot_idx_abs];
    1526    95404361 :                 slot_idx_abs++;
    1527             :             }
    1528    24153841 :             hSpatParamRendCom->render_to_md_map[sf_idx] = ( (int16_t) roundf( tmp / (float) hSpatParamRendCom->subframe_nbslots[sf_idx] ) + hSpatParamRendCom->dirac_read_idx ) % hSpatParamRendCom->dirac_md_buffer_length;
    1529             :         }
    1530             : 
    1531     5962075 :         set_s( &hSpatParamRendCom->render_to_md_map[hSpatParamRendCom->nb_subframes], 0, MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME - hSpatParamRendCom->nb_subframes );
    1532             :     }
    1533             : 
    1534     8736792 :     return;
    1535             : }
    1536             : 
    1537             : 
    1538             : /*-------------------------------------------------------------------------
    1539             :  * ivas_dirac_dec_render()
    1540             :  *
    1541             :  * DirAC decoding renderer process
    1542             :  *------------------------------------------------------------------------*/
    1543             : 
    1544     2724934 : void ivas_dirac_dec_render(
    1545             :     Decoder_Struct *st_ivas,         /* i/o: IVAS decoder handle                      */
    1546             :     const int16_t nchan_transport,   /* i  : number of transport channels             */
    1547             :     const uint16_t nSamplesAsked,    /* i  : number of CLDFB slots requested          */
    1548             :     uint16_t *nSamplesRendered,      /* o  : number of CLDFB slots rendered           */
    1549             :     uint16_t *nSamplesAvailableNext, /* o  : number of CLDFB slots still to render    */
    1550             :     float *output_f[]                /* o  : rendered time signal                     */
    1551             : )
    1552             : {
    1553             :     int16_t slots_to_render, first_sf, last_sf, subframe_idx;
    1554             :     uint16_t slot_size, n_samples_sf, ch, nchan_intern;
    1555             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
    1556             :     float *output_f_local[MAX_OUTPUT_CHANNELS];
    1557             :     float *p_output_f[MAX_OUTPUT_CHANNELS];
    1558             :     float output_f_local_buff[MAX_OUTPUT_CHANNELS][L_FRAME48k / MAX_PARAM_SPATIAL_SUBFRAMES];
    1559             : 
    1560     2724934 :     hSpatParamRendCom = st_ivas->hSpatParamRendCom;
    1561             : 
    1562     2724934 :     nchan_intern = st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe;
    1563             : #ifdef DEBUGGING
    1564             :     assert( hSpatParamRendCom );
    1565             : #endif
    1566    23353419 :     for ( ch = 0; ch < nchan_intern; ch++ )
    1567             :     {
    1568    20628485 :         output_f_local[ch] = output_f_local_buff[ch];
    1569    20628485 :         p_output_f[ch] = output_f[ch];
    1570             :     }
    1571     2724934 :     slot_size = NS2SA( st_ivas->hDecoderConfig->output_Fs, CLDFB_SLOT_NS );
    1572             : 
    1573             :     /* loop for synthesis, assume we always have to render in multiples of 5ms subframes with spills */
    1574     2724934 :     slots_to_render = min( hSpatParamRendCom->num_slots - hSpatParamRendCom->slots_rendered, nSamplesAsked / slot_size );
    1575     2724934 :     *nSamplesRendered = slots_to_render * slot_size;
    1576     2724934 :     first_sf = hSpatParamRendCom->subframes_rendered;
    1577     2724934 :     last_sf = first_sf;
    1578             : 
    1579     8483893 :     while ( slots_to_render > 0 )
    1580             :     {
    1581     5758959 :         slots_to_render -= hSpatParamRendCom->subframe_nbslots[last_sf];
    1582     5758959 :         last_sf++;
    1583             :     }
    1584             : 
    1585             : #ifdef DEBUGGING
    1586             :     assert( slots_to_render == 0 );
    1587             : #endif
    1588     8483893 :     for ( subframe_idx = first_sf; subframe_idx < last_sf; subframe_idx++ )
    1589             :     {
    1590     5758959 :         ivas_dirac_dec_render_sf( st_ivas, output_f_local, nchan_transport, NULL, NULL );
    1591             : 
    1592     5758959 :         n_samples_sf = hSpatParamRendCom->subframe_nbslots[subframe_idx] * hSpatParamRendCom->slot_size;
    1593             : 
    1594    51320203 :         for ( ch = 0; ch < nchan_intern; ch++ )
    1595             :         {
    1596             :             /* move to output */
    1597    45561244 :             if ( !( ( st_ivas->hDirACRend->hOutSetup.separateChannelEnabled ) && ( st_ivas->hDirACRend->hOutSetup.separateChannelIndex == ch || st_ivas->hDirACRend->hOutSetup.separateChannelIndex + 1 == ch ) ) )
    1598             :             {
    1599    45145764 :                 mvr2r( output_f_local_buff[ch], p_output_f[ch], n_samples_sf );
    1600             :             }
    1601             : 
    1602    45561244 :             p_output_f[ch] += n_samples_sf;
    1603             :         }
    1604             : 
    1605             :         /* update combined orientation access index */
    1606     5758959 :         ivas_combined_orientation_update_index( st_ivas->hCombinedOrientationData, n_samples_sf );
    1607             :     }
    1608             : 
    1609     2724934 :     if ( hSpatParamRendCom->slots_rendered == hSpatParamRendCom->num_slots )
    1610             :     {
    1611     1417655 :         if ( st_ivas->hDirAC->hConfig->dec_param_estim == 1 )
    1612             :         {
    1613           0 :             hSpatParamRendCom->dirac_read_idx = ( hSpatParamRendCom->dirac_read_idx + DEFAULT_JBM_CLDFB_TIMESLOTS ) % hSpatParamRendCom->dirac_md_buffer_length;
    1614             :         }
    1615             :         else
    1616             :         {
    1617     1417655 :             hSpatParamRendCom->dirac_read_idx = ( hSpatParamRendCom->dirac_read_idx + DEFAULT_JBM_SUBFRAMES_5MS ) % hSpatParamRendCom->dirac_md_buffer_length;
    1618             :         }
    1619             :     }
    1620             : 
    1621     2724934 :     *nSamplesAvailableNext = ( hSpatParamRendCom->num_slots - hSpatParamRendCom->slots_rendered ) * slot_size;
    1622             : 
    1623     2724934 :     return;
    1624             : }
    1625             : 
    1626             : 
    1627             : /*-------------------------------------------------------------------------
    1628             :  * Local functions to perform binaural rendering with optimized stack
    1629             :  *------------------------------------------------------------------------*/
    1630             : 
    1631      623038 : static void binRenderer_split(
    1632             :     BINAURAL_RENDERER_HANDLE hBinRenderer,                                                                    /* i/o: binaural renderer handle                      */
    1633             :     ISAR_DEC_SPLIT_REND_WRAPPER_HANDLE hSplitBinRend,                                                         /* i/o: ISAR split binaural rendering handle          */
    1634             :     COMBINED_ORIENTATION_HANDLE hCombinedOrientationData,                                                     /* i  : combined head and external orientation handle */
    1635             :     const int16_t numTimeSlots,                                                                               /* i  : number of time slots to render                */
    1636             :     float Cldfb_RealBuffer_Binaural[][BINAURAL_CHANNELS][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX], /* o  : Rotated Binaural signals                      */
    1637             :     float Cldfb_ImagBuffer_Binaural[][BINAURAL_CHANNELS][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX], /* o  : Rotated Binaural signals                      */
    1638             :     float RealBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],                                   /* i  : LS signals                                    */
    1639             :     float ImagBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],                                   /* i  : LS signals                                    */
    1640             : #ifndef FIX_1119_SPLIT_RENDERING_VOIP
    1641             :     const int16_t slot_idx_start,
    1642             : #endif
    1643             :     const int16_t num_freq_bands,
    1644             :     const int16_t nchan_out )
    1645             : {
    1646             :     int16_t pos_idx, slot_idx, ch;
    1647             :     float Cldfb_RealBuffer_Binaural_loc[MAX_HEAD_ROT_POSES][BINAURAL_CHANNELS][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
    1648             :     float Cldfb_ImagBuffer_Binaural_loc[MAX_HEAD_ROT_POSES][BINAURAL_CHANNELS][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
    1649             : 
    1650             :     /* Perform binaural rendering */
    1651      623038 :     ivas_binRenderer( hBinRenderer, &hSplitBinRend->splitrend.multiBinPoseData, hCombinedOrientationData, numTimeSlots,
    1652             : #ifdef SPLIT_REND_WITH_HEAD_ROT_DEBUG
    1653             :                       NULL,
    1654             : #endif
    1655             :                       Cldfb_RealBuffer_Binaural_loc, Cldfb_ImagBuffer_Binaural_loc, RealBuffer, ImagBuffer );
    1656             : 
    1657     3007382 :     for ( slot_idx = 0; slot_idx < numTimeSlots; slot_idx++ )
    1658             :     {
    1659     7153032 :         for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    1660             :         {
    1661     4768688 :             mvr2r( Cldfb_RealBuffer_Binaural_loc[0][ch][slot_idx], Cldfb_RealBuffer_Binaural[0][ch][slot_idx], num_freq_bands );
    1662     4768688 :             mvr2r( Cldfb_ImagBuffer_Binaural_loc[0][ch][slot_idx], Cldfb_ImagBuffer_Binaural[0][ch][slot_idx], num_freq_bands );
    1663             :         }
    1664             :     }
    1665             : 
    1666     2854604 :     for ( pos_idx = 0; pos_idx < hBinRenderer->numPoses; pos_idx++ )
    1667             :     {
    1668    10832474 :         for ( slot_idx = 0; slot_idx < numTimeSlots; slot_idx++ )
    1669             :         {
    1670    25802724 :             for ( ch = 0; ch < nchan_out; ch++ )
    1671             :             {
    1672             : #ifdef FIX_1119_SPLIT_RENDERING_VOIP
    1673    17201816 :                 ivas_CLDFB_RINGBUF_Push(
    1674    17201816 :                     hSplitBinRend->hMultiBinCldfbData[pos_idx * BINAURAL_CHANNELS + ch],
    1675    17201816 :                     Cldfb_RealBuffer_Binaural_loc[pos_idx][ch][slot_idx],
    1676    17201816 :                     Cldfb_ImagBuffer_Binaural_loc[pos_idx][ch][slot_idx],
    1677             :                     num_freq_bands );
    1678             : #else
    1679             :                 mvr2r( Cldfb_RealBuffer_Binaural_loc[pos_idx][ch][slot_idx], hSplitBinRend->hMultiBinCldfbData->Cldfb_RealBuffer_Binaural[( pos_idx * BINAURAL_CHANNELS ) + ch][slot_idx_start + slot_idx], num_freq_bands );
    1680             :                 mvr2r( Cldfb_ImagBuffer_Binaural_loc[pos_idx][ch][slot_idx], hSplitBinRend->hMultiBinCldfbData->Cldfb_ImagBuffer_Binaural[( pos_idx * BINAURAL_CHANNELS ) + ch][slot_idx_start + slot_idx], num_freq_bands );
    1681             : #endif
    1682             :             }
    1683             :         }
    1684             :     }
    1685             : 
    1686      623038 :     return;
    1687             : }
    1688             : 
    1689             : 
    1690     4690213 : static void binRenderer(
    1691             :     BINAURAL_RENDERER_HANDLE hBinRenderer,                                                                    /* i/o: binaural renderer handle                      */
    1692             :     COMBINED_ORIENTATION_HANDLE hCombinedOrientationData,                                                     /* i  : combined head and external orientation handle */
    1693             :     const int16_t numTimeSlots,                                                                               /* i  : number of time slots to render                */
    1694             :     float Cldfb_RealBuffer_Binaural[][BINAURAL_CHANNELS][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX], /* o  : Rotated Binaural signals                      */
    1695             :     float Cldfb_ImagBuffer_Binaural[][BINAURAL_CHANNELS][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX], /* o  : Rotated Binaural signals                      */
    1696             :     float RealBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],                                   /* i  : LS signals                                    */
    1697             :     float ImagBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX]                                    /* i  : LS signals                                    */
    1698             : )
    1699             : {
    1700             :     /* Perform binaural rendering */
    1701     4690213 :     ivas_binRenderer( hBinRenderer, NULL, hCombinedOrientationData, numTimeSlots,
    1702             : #ifdef SPLIT_REND_WITH_HEAD_ROT_DEBUG
    1703             :                       NULL,
    1704             : #endif
    1705             :                       Cldfb_RealBuffer_Binaural, Cldfb_ImagBuffer_Binaural, RealBuffer, ImagBuffer );
    1706             : 
    1707     4690213 :     return;
    1708             : }
    1709             : 
    1710             : 
    1711             : /*-------------------------------------------------------------------------
    1712             :  * ivas_dirac_dec_render_sf()
    1713             :  *
    1714             :  * DirAC decoding renderer process
    1715             :  *------------------------------------------------------------------------*/
    1716             : 
    1717    18874991 : void ivas_dirac_dec_render_sf(
    1718             :     Decoder_Struct *st_ivas,       /* i/o: IVAS decoder structure                                  */
    1719             :     float *output_f[],             /* i/o: synthesized core-coder transport channels/DirAC output  */
    1720             :     const int16_t nchan_transport, /* i  : number of transport channels                            */
    1721             :     float *pppQMfFrame_ts_re[HOA3_CHANNELS][CLDFB_NO_COL_MAX],
    1722             :     float *pppQMfFrame_ts_im[HOA3_CHANNELS][CLDFB_NO_COL_MAX] )
    1723             : {
    1724             :     int16_t i, ch, idx_in, idx_lfe;
    1725             :     DIRAC_DEC_HANDLE hDirAC;
    1726             :     DIRAC_REND_HANDLE hDirACRend;
    1727             :     float dirEne;
    1728             :     float surCohEner;
    1729             :     float surCohRatio[CLDFB_NO_CHANNELS_MAX];
    1730             :     int16_t subframe_idx;
    1731             :     int16_t slot_idx, index_slot;
    1732             :     int16_t hodirac_flag;
    1733             :     float *p_Rmat;
    1734             :     int16_t slot_idx_start, slot_idx_start_cldfb_synth, md_idx;
    1735             : 
    1736             :     /*CLDFB: last output channels reserved to LFE for CICPx*/
    1737             :     float Cldfb_RealBuffer[MAX_OUTPUT_CHANNELS][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
    1738             :     float Cldfb_ImagBuffer[MAX_OUTPUT_CHANNELS][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
    1739             :     float Cldfb_RealBuffer_Binaural[1][BINAURAL_CHANNELS][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
    1740             :     float Cldfb_ImagBuffer_Binaural[1][BINAURAL_CHANNELS][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
    1741             :     int16_t index, num_freq_bands;
    1742             : 
    1743             :     /* local copies of azi, ele, diffuseness */
    1744             :     int16_t azimuth[CLDFB_NO_CHANNELS_MAX];
    1745             :     int16_t elevation[CLDFB_NO_CHANNELS_MAX];
    1746             :     float diffuseness_vector[CLDFB_NO_CHANNELS_MAX];
    1747             : 
    1748             :     DIRAC_DEC_STACK_MEM DirAC_mem;
    1749             :     float *reference_power, *reference_power_smooth;
    1750    18874991 :     float *onset_filter, *onset_filter_subframe, *p_onset_filter = NULL;
    1751             :     uint16_t coherence_flag;
    1752             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
    1753             : 
    1754    18874991 :     push_wmops( "ivas_dirac_dec_render" );
    1755             : 
    1756             :     /* Initialize aux buffers */
    1757    18874991 :     hDirAC = st_ivas->hDirAC;
    1758    18874991 :     hDirACRend = st_ivas->hDirACRend;
    1759    18874991 :     hSpatParamRendCom = st_ivas->hSpatParamRendCom;
    1760             : 
    1761    18874991 :     DirAC_mem = hDirACRend->stack_mem;
    1762             : 
    1763    18874991 :     reference_power = DirAC_mem.reference_power;
    1764    18874991 :     reference_power_smooth = ( DirAC_mem.reference_power == NULL ) ? NULL : DirAC_mem.reference_power + hSpatParamRendCom->num_freq_bands;
    1765    18874991 :     onset_filter = DirAC_mem.onset_filter;
    1766    18874991 :     onset_filter_subframe = ( DirAC_mem.onset_filter == NULL ) ? NULL : DirAC_mem.onset_filter + hSpatParamRendCom->num_freq_bands;
    1767             : 
    1768    18874991 :     hodirac_flag = ivas_get_hodirac_flag( st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->sba_analysis_order );
    1769    18874991 :     if ( st_ivas->hQMetaData != NULL && st_ivas->ivas_format != SBA_FORMAT && st_ivas->ivas_format != SBA_ISM_FORMAT )
    1770             :     {
    1771     5758959 :         coherence_flag = st_ivas->hQMetaData->coherence_flag;
    1772             :     }
    1773             :     else
    1774             :     {
    1775    13116032 :         coherence_flag = 0;
    1776             :     }
    1777             : 
    1778             : #ifdef DEBUG_MODE_DIRAC
    1779             :     {
    1780             :         int16_t n, tmp[IVAS_SPAR_MAX_CH * L_FRAME48k];
    1781             :         char file_name[50] = { 0 };
    1782             :         const int16_t output_frame = st_ivas->hDecoderConfig->output_Fs / FRAMES_PER_SEC;
    1783             : 
    1784             :         for ( n = 0; n < nchan_transport; n++ )
    1785             :         {
    1786             :             for ( i = 0; i < output_frame; i++ )
    1787             :             {
    1788             :                 tmp[nchan_transport * i + n] = (int16_t) ( output_f[n][i] + 0.5f );
    1789             :             }
    1790             :         }
    1791             :         sprintf( file_name, "./res/ivas_dirac_dec_DMX%d.%d.pcm", nchan_transport, (int16_t) ( output_frame * 0.05 ) );
    1792             :         dbgwrite( tmp, sizeof( int16_t ), nchan_transport * output_frame, 1, file_name );
    1793             :     }
    1794             : #endif
    1795             : 
    1796             :     /* Subframe loop */
    1797    18874991 :     slot_idx_start = hSpatParamRendCom->slots_rendered;
    1798    18874991 :     slot_idx_start_cldfb_synth = 0;
    1799             : 
    1800    18874991 :     subframe_idx = hSpatParamRendCom->subframes_rendered;
    1801    18874991 :     if ( hDirAC->hConfig->dec_param_estim == FALSE )
    1802             :     {
    1803     8198241 :         md_idx = hSpatParamRendCom->render_to_md_map[subframe_idx];
    1804             :     }
    1805             :     else
    1806             :     {
    1807    10676750 :         md_idx = hSpatParamRendCom->render_to_md_map[slot_idx_start];
    1808             :     }
    1809             : 
    1810             :     /* copy parameters into local buffers*/
    1811    18874991 :     if ( hDirAC->hConfig->dec_param_estim == FALSE )
    1812             :     {
    1813     8198241 :         mvs2s( hSpatParamRendCom->azimuth[hSpatParamRendCom->render_to_md_map[subframe_idx]], azimuth, hSpatParamRendCom->num_freq_bands );
    1814     8198241 :         mvs2s( hSpatParamRendCom->elevation[hSpatParamRendCom->render_to_md_map[subframe_idx]], elevation, hSpatParamRendCom->num_freq_bands );
    1815     8198241 :         mvr2r( hSpatParamRendCom->diffuseness_vector[hSpatParamRendCom->render_to_md_map[subframe_idx]], diffuseness_vector, hSpatParamRendCom->num_freq_bands );
    1816             :     }
    1817             :     else
    1818             :     {
    1819    10676750 :         set_zero( diffuseness_vector, hSpatParamRendCom->num_freq_bands );
    1820             :     }
    1821             : 
    1822    18874991 :     if ( hDirACRend->synthesisConf != DIRAC_SYNTHESIS_GAIN_SHD )
    1823             :     {
    1824     6147725 :         set_zero( reference_power_smooth, hSpatParamRendCom->num_freq_bands );
    1825             :     }
    1826             :     else
    1827             :     {
    1828    12727266 :         set_zero( onset_filter_subframe, hSpatParamRendCom->num_freq_bands );
    1829             :     }
    1830             : 
    1831    18874991 :     if ( st_ivas->hCombinedOrientationData && st_ivas->hCombinedOrientationData->enableCombinedOrientation[st_ivas->hCombinedOrientationData->subframe_idx] )
    1832             :     {
    1833     1786484 :         p_Rmat = &st_ivas->hCombinedOrientationData->Rmat[st_ivas->hCombinedOrientationData->subframe_idx][0][0];
    1834     1786484 :         if ( st_ivas->hCombinedOrientationData->shd_rot_max_order == 0 )
    1835             :         {
    1836     1420244 :             num_freq_bands = hDirAC->band_grouping[hDirAC->hConfig->enc_param_start_band];
    1837     1420244 :             if ( hDirAC->hConfig->dec_param_estim == FALSE )
    1838             :             {
    1839           0 :                 rotateAziEle_DirAC( azimuth, elevation, num_freq_bands, hSpatParamRendCom->num_freq_bands, p_Rmat );
    1840             :             }
    1841             :         }
    1842             :     }
    1843             :     else
    1844             :     {
    1845    17088507 :         p_Rmat = 0;
    1846             :     }
    1847             : 
    1848    18874991 :     if ( hDirAC->hConfig->dec_param_estim == FALSE )
    1849             :     {
    1850             :         /* compute response */
    1851     8198241 :         if ( hDirACRend->synthesisConf != DIRAC_SYNTHESIS_GAIN_SHD )
    1852             :         {
    1853     5420789 :             ivas_dirac_dec_compute_power_factors( hSpatParamRendCom->num_freq_bands,
    1854             :                                                   diffuseness_vector,
    1855     5420789 :                                                   hDirACRend->h_output_synthesis_psd_params.max_band_decorr,
    1856             :                                                   hDirACRend->h_output_synthesis_psd_state.direct_power_factor,
    1857             :                                                   hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor );
    1858             : 
    1859     5420789 :             if ( coherence_flag )
    1860             :             {
    1861   169032973 :                 for ( i = 0; i < hSpatParamRendCom->num_freq_bands; i++ )
    1862             :                 {
    1863   165736220 :                     dirEne = hDirACRend->h_output_synthesis_psd_state.direct_power_factor[i];
    1864   165736220 :                     surCohEner = hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor[i] * hSpatParamRendCom->surroundingCoherence[md_idx][i];
    1865   165736220 :                     hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor[i] -= surCohEner;
    1866   165736220 :                     hDirACRend->h_output_synthesis_psd_state.direct_power_factor[i] += surCohEner;
    1867             : 
    1868   165736220 :                     surCohRatio[i] = surCohEner / ( 1e-12f + dirEne + surCohEner );
    1869             :                 }
    1870             :             }
    1871             :             else
    1872             :             {
    1873     2124036 :                 set_zero( surCohRatio, hSpatParamRendCom->num_freq_bands );
    1874             :             }
    1875             :         }
    1876             :         else
    1877             :         {
    1878     2777452 :             ivas_dirac_dec_compute_gain_factors( hSpatParamRendCom->num_freq_bands,
    1879     2777452 :                                                  hSpatParamRendCom->diffuseness_vector[md_idx],
    1880     2777452 :                                                  hDirACRend->h_output_synthesis_psd_params.max_band_decorr,
    1881             :                                                  hDirACRend->h_output_synthesis_psd_state.direct_power_factor,
    1882             :                                                  hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor );
    1883             : 
    1884     2777452 :             if ( coherence_flag )
    1885             :             {
    1886     8586394 :                 for ( i = 0; i < hSpatParamRendCom->num_freq_bands; i++ )
    1887             :                 {
    1888     8423880 :                     surCohRatio[i] = hSpatParamRendCom->surroundingCoherence[md_idx][i];
    1889             :                 }
    1890             :             }
    1891             :             else
    1892             :             {
    1893     2614938 :                 set_zero( surCohRatio, hSpatParamRendCom->num_freq_bands );
    1894             :             }
    1895             :         }
    1896     8198241 :         ivas_dirac_dec_compute_directional_responses( hSpatParamRendCom,
    1897             :                                                       hDirACRend,
    1898             :                                                       st_ivas->hVBAPdata,
    1899     8198241 :                                                       st_ivas->hMasa == NULL ? NULL : st_ivas->hMasa->data.band_mapping,
    1900             :                                                       st_ivas->hMasaIsmData,
    1901             :                                                       azimuth,
    1902             :                                                       elevation,
    1903             :                                                       md_idx,
    1904             :                                                       surCohRatio,
    1905             :                                                       hodirac_flag );
    1906             :     }
    1907             : 
    1908    18874991 :     if ( st_ivas->ivas_format == MASA_ISM_FORMAT && nchan_transport == 2 )
    1909             :     {
    1910     8230397 :         for ( slot_idx = 0; slot_idx < hSpatParamRendCom->subframe_nbslots[subframe_idx]; slot_idx++ )
    1911             :         {
    1912     6548481 :             index_slot = slot_idx_start + slot_idx;
    1913             : 
    1914             :             /* CLDFB Analysis*/
    1915    19645443 :             for ( ch = 0; ch < nchan_transport; ch++ )
    1916             :             {
    1917    13096962 :                 cldfbAnalysis_ts( &( st_ivas->hTcBuffer->tc[hDirACRend->sba_map_tc[ch]][hSpatParamRendCom->num_freq_bands * index_slot] ),
    1918    13096962 :                                   Cldfb_RealBuffer_Binaural[0][ch][slot_idx], /* note: it is a tmp. buffer at this point */
    1919    13096962 :                                   Cldfb_ImagBuffer_Binaural[0][ch][slot_idx], /* note: it is a tmp. buffer at this point */
    1920    13096962 :                                   hSpatParamRendCom->num_freq_bands,
    1921             :                                   st_ivas->cldfbAnaDec[ch] );
    1922             :             }
    1923             :         }
    1924             : 
    1925     1681916 :         if ( st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
    1926             :         {
    1927      378014 :             ivas_omasa_preProcessStereoTransportsForEditedObjects( st_ivas, Cldfb_RealBuffer_Binaural[0], Cldfb_ImagBuffer_Binaural[0], hSpatParamRendCom->num_freq_bands, subframe_idx );
    1928             :         }
    1929             :     }
    1930             : 
    1931    93798895 :     for ( slot_idx = 0; slot_idx < hSpatParamRendCom->subframe_nbslots[subframe_idx]; slot_idx++ )
    1932             :     {
    1933    74923904 :         index_slot = slot_idx_start + slot_idx;
    1934    74923904 :         if ( hDirAC->hConfig->dec_param_estim == TRUE )
    1935             :         {
    1936    42514324 :             md_idx = hSpatParamRendCom->render_to_md_map[index_slot];
    1937             :         }
    1938             :         else
    1939             :         {
    1940    32409580 :             md_idx = hSpatParamRendCom->render_to_md_map[subframe_idx];
    1941             :         }
    1942    74923904 :         if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
    1943             :         {
    1944   326309284 :             for ( ch = 0; ch < nchan_transport; ch++ )
    1945             :             {
    1946   274067548 :                 mvr2r( pppQMfFrame_ts_re[ch][slot_idx], Cldfb_RealBuffer[ch][0], hSpatParamRendCom->num_freq_bands );
    1947   274067548 :                 mvr2r( pppQMfFrame_ts_im[ch][slot_idx], Cldfb_ImagBuffer[ch][0], hSpatParamRendCom->num_freq_bands );
    1948             :             }
    1949             :         }
    1950    22682168 :         else if ( st_ivas->ivas_format == MASA_ISM_FORMAT && nchan_transport == 2 )
    1951             :         {
    1952    19645443 :             for ( ch = 0; ch < nchan_transport; ch++ )
    1953             :             {
    1954    13096962 :                 mvr2r( Cldfb_RealBuffer_Binaural[0][ch][slot_idx], Cldfb_RealBuffer[ch][0], hSpatParamRendCom->num_freq_bands );
    1955    13096962 :                 mvr2r( Cldfb_ImagBuffer_Binaural[0][ch][slot_idx], Cldfb_ImagBuffer[ch][0], hSpatParamRendCom->num_freq_bands );
    1956             :             }
    1957             :         }
    1958             :         else
    1959             :         {
    1960             :             /* CLDFB Analysis*/
    1961    40213145 :             for ( ch = 0; ch < nchan_transport; ch++ )
    1962             :             {
    1963    24079458 :                 cldfbAnalysis_ts( &( st_ivas->hTcBuffer->tc[hDirACRend->sba_map_tc[ch]][hSpatParamRendCom->num_freq_bands * index_slot] ),
    1964    24079458 :                                   Cldfb_RealBuffer[ch][0],
    1965    24079458 :                                   Cldfb_ImagBuffer[ch][0],
    1966    24079458 :                                   hSpatParamRendCom->num_freq_bands,
    1967             :                                   st_ivas->cldfbAnaDec[ch] );
    1968             :             }
    1969             :         }
    1970             : 
    1971             :         /* CNG in DirAC, extra CLDFB ana for CNA*/
    1972    74923904 :         if ( st_ivas->nchan_transport == 1 && st_ivas->hSCE[0]->hCoreCoder[0] != NULL && st_ivas->hSCE[0]->hCoreCoder[0]->cna_dirac_flag && !( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT ) )
    1973             :         {
    1974     1325155 :             Decoder_State *st = st_ivas->hSCE[0]->hCoreCoder[0];
    1975             : 
    1976     2650310 :             generate_masking_noise_dirac( st->hFdCngDec->hFdCngCom,
    1977             :                                           st_ivas->cldfbAnaDec[1],
    1978     1325155 :                                           &st_ivas->hTcBuffer->tc[1][hSpatParamRendCom->num_freq_bands * hSpatParamRendCom->slots_rendered],
    1979             :                                           Cldfb_RealBuffer[1][0],
    1980             :                                           Cldfb_ImagBuffer[1][0],
    1981             :                                           slot_idx,
    1982     1325155 :                                           st->cna_dirac_flag && st->flag_cna,
    1983     1325155 :                                           ( st->core_brate == FRAME_NO_DATA || st->core_brate == SID_2k40 ) && st->cng_type == FD_CNG && st->cng_sba_flag );
    1984             :         }
    1985             : 
    1986             :         /* LFE synthesis */
    1987    74923904 :         if ( st_ivas->mc_mode == MC_MODE_MCMASA && !hDirACRend->hOutSetup.separateChannelEnabled && !( hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && hDirACRend->hOutSetup.num_lfe == 0 ) )
    1988             :         {
    1989     2097008 :             ivas_lfe_synth_with_cldfb( st_ivas->hMasa->hMasaLfeSynth,
    1990             :                                        Cldfb_RealBuffer, Cldfb_ImagBuffer,
    1991             :                                        Cldfb_RealBuffer[MAX_OUTPUT_CHANNELS - 1], Cldfb_ImagBuffer[MAX_OUTPUT_CHANNELS - 1],
    1992             :                                        slot_idx,
    1993             :                                        md_idx,
    1994             :                                        nchan_transport );
    1995             :         }
    1996             : 
    1997             :         /*-----------------------------------------------------------------*
    1998             :          * protoype signal computation
    1999             :          *-----------------------------------------------------------------*/
    2000             : 
    2001    74923904 :         if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
    2002             :         {
    2003    50659147 :             if ( st_ivas->hCombinedOrientationData && st_ivas->hCombinedOrientationData->enableCombinedOrientation[st_ivas->hCombinedOrientationData->subframe_idx] && st_ivas->hCombinedOrientationData->shd_rot_max_order == 0 )
    2004             :             {
    2005     5602884 :                 protoSignalComputation_shd( Cldfb_RealBuffer, Cldfb_ImagBuffer,
    2006             :                                             hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f,
    2007             :                                             hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f,
    2008             :                                             reference_power, slot_idx, nchan_transport,
    2009     5602884 :                                             hDirACRend->num_outputs_diff,
    2010     5602884 :                                             hSpatParamRendCom->num_freq_bands,
    2011             :                                             p_Rmat );
    2012             :             }
    2013             :             else
    2014             :             {
    2015    45056263 :                 protoSignalComputation_shd( Cldfb_RealBuffer, Cldfb_ImagBuffer,
    2016             :                                             hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f,
    2017             :                                             hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f,
    2018             :                                             reference_power, slot_idx, nchan_transport,
    2019    45056263 :                                             hDirACRend->num_outputs_diff,
    2020    45056263 :                                             hSpatParamRendCom->num_freq_bands,
    2021             :                                             0 );
    2022             :             }
    2023             :         }
    2024    24264757 :         else if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_MONO )
    2025             :         {
    2026     1426700 :             protoSignalComputation2( Cldfb_RealBuffer, Cldfb_ImagBuffer, hDirACRend->proto_frame_f,
    2027             :                                      hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f,
    2028             :                                      reference_power, hDirACRend->h_output_synthesis_psd_state.proto_power_smooth,
    2029     1426700 :                                      0, slot_idx, hSpatParamRendCom->num_freq_bands, hDirACRend->masa_stereo_type_detect );
    2030             :         }
    2031             :         else
    2032             :         {
    2033    22838057 :             switch ( nchan_transport )
    2034             :             {
    2035     2907744 :                 case 11:
    2036             :                 case 8:
    2037             :                 case 6:
    2038             :                 case 4:
    2039     2907744 :                     protoSignalComputation4( Cldfb_RealBuffer, Cldfb_ImagBuffer,
    2040             :                                              hDirACRend->proto_frame_f,
    2041             :                                              hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f,
    2042             :                                              reference_power,
    2043             :                                              hDirACRend->h_output_synthesis_psd_state.proto_power_smooth,
    2044     2907744 :                                              slot_idx, hDirACRend->num_outputs_diff,
    2045     2907744 :                                              hSpatParamRendCom->num_freq_bands,
    2046             :                                              hDirACRend->hoa_decoder,
    2047             :                                              nchan_transport,
    2048             :                                              hDirACRend->sba_map_tc );
    2049     2907744 :                     break;
    2050    13067552 :                 case 2:
    2051    13067552 :                     protoSignalComputation2( Cldfb_RealBuffer, Cldfb_ImagBuffer,
    2052             :                                              hDirACRend->proto_frame_f,
    2053             :                                              hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f,
    2054             :                                              reference_power,
    2055             :                                              hDirACRend->h_output_synthesis_psd_state.proto_power_smooth,
    2056    13067552 :                                              hDirACRend->hOutSetup.is_loudspeaker_setup,
    2057             :                                              slot_idx,
    2058    13067552 :                                              hSpatParamRendCom->num_freq_bands,
    2059             :                                              hDirACRend->masa_stereo_type_detect );
    2060    13067552 :                     break;
    2061     6862761 :                 case 1:
    2062     6862761 :                     protoSignalComputation1( Cldfb_RealBuffer, Cldfb_ImagBuffer,
    2063             :                                              hDirACRend->proto_frame_f,
    2064             :                                              hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f,
    2065             :                                              reference_power,
    2066             :                                              hDirACRend->h_output_synthesis_psd_state.proto_power_smooth,
    2067             :                                              slot_idx,
    2068     6862761 :                                              hDirACRend->num_protos_diff,
    2069     6862761 :                                              hSpatParamRendCom->num_freq_bands );
    2070     6862761 :                     break;
    2071           0 :                 default:
    2072           0 :                     return;
    2073             :             }
    2074             :         }
    2075             : 
    2076             : 
    2077             :         /*-----------------------------------------------------------------*
    2078             :          * Compute DirAC parameters at decoder side
    2079             :          *-----------------------------------------------------------------*/
    2080             : 
    2081    74923904 :         if ( hDirAC->hConfig->dec_param_estim == TRUE )
    2082             :         {
    2083    42514324 :             mvs2s( &hSpatParamRendCom->azimuth[md_idx][hDirAC->hConfig->enc_param_start_band], &azimuth[hDirAC->hConfig->enc_param_start_band], hSpatParamRendCom->num_freq_bands - hDirAC->hConfig->enc_param_start_band );
    2084    42514324 :             mvs2s( &hSpatParamRendCom->elevation[md_idx][hDirAC->hConfig->enc_param_start_band], &elevation[hDirAC->hConfig->enc_param_start_band], hSpatParamRendCom->num_freq_bands - hDirAC->hConfig->enc_param_start_band );
    2085             : #ifdef IVAS_RTPDUMP
    2086    42514324 :             if ( ( st_ivas->hDecoderConfig->Opt_Headrotation || st_ivas->hDecoderConfig->Opt_ExternalOrientation || st_ivas->hCombinedOrientationData ) && st_ivas->hCombinedOrientationData->shd_rot_max_order == 0 )
    2087             : #else
    2088             :             if ( ( st_ivas->hDecoderConfig->Opt_Headrotation || st_ivas->hDecoderConfig->Opt_ExternalOrientation ) && st_ivas->hCombinedOrientationData->shd_rot_max_order == 0 )
    2089             : #endif
    2090             :             {
    2091     5602884 :                 num_freq_bands = hDirAC->band_grouping[hDirAC->hConfig->enc_param_start_band];
    2092     5602884 :                 rotateAziEle_DirAC( azimuth, elevation, num_freq_bands, hSpatParamRendCom->num_freq_bands, p_Rmat );
    2093             :             }
    2094             : 
    2095    42514324 :             hDirACRend->index_buffer_intensity = ( hDirACRend->index_buffer_intensity % DIRAC_NO_COL_AVG_DIFF ) + 1; /* averaging_length = 32 */
    2096             : 
    2097    42514324 :             index = hDirACRend->index_buffer_intensity;
    2098             : 
    2099    42514324 :             num_freq_bands = hDirAC->band_grouping[hDirAC->hConfig->enc_param_start_band];
    2100             : 
    2101    42514324 :             computeIntensityVector_dec( Cldfb_RealBuffer,
    2102             :                                         Cldfb_ImagBuffer,
    2103             :                                         num_freq_bands,
    2104    42514324 :                                         hDirACRend->buffer_intensity_real[0][index - 1],
    2105    42514324 :                                         hDirACRend->buffer_intensity_real[1][index - 1],
    2106    42514324 :                                         hDirACRend->buffer_intensity_real[2][index - 1] );
    2107             : 
    2108    42514324 :             computeDirectionAngles( hDirACRend->buffer_intensity_real[0][index - 1],
    2109    42514324 :                                     hDirACRend->buffer_intensity_real[1][index - 1],
    2110    42514324 :                                     hDirACRend->buffer_intensity_real[2][index - 1],
    2111             :                                     num_freq_bands,
    2112             :                                     azimuth,
    2113             :                                     elevation );
    2114             : 
    2115    42514324 :             mvr2r( reference_power, &( hDirACRend->buffer_energy[( index - 1 ) * num_freq_bands] ), num_freq_bands );
    2116             : 
    2117    42514324 :             computeDiffuseness( hDirACRend->buffer_intensity_real, hDirACRend->buffer_energy, num_freq_bands, hSpatParamRendCom->diffuseness_vector[md_idx] );
    2118             :         }
    2119             : 
    2120             : #ifdef DEBUG_MODE_DIRAC
    2121             :         {
    2122             :             static FILE *fp_direction_vector = NULL, *fp_diffuseness = NULL, *fp_referencePower = NULL;
    2123             : 
    2124             : 
    2125             :             if ( fp_direction_vector == NULL )
    2126             :                 fp_direction_vector = fopen( "./res/dbg_direction_vector_C_dec.bin", "wb" );
    2127             :             if ( fp_diffuseness == NULL )
    2128             :                 fp_diffuseness = fopen( "./res/dbg_diffuseness_C_dec.bin", "wb" );
    2129             :             if ( fp_referencePower == NULL )
    2130             :                 fp_referencePower = fopen( "./res/dbg_reference_power_C_dec.bin", "wb" );
    2131             : 
    2132             : 
    2133             :             for ( i = 0; i < hSpatParamRendCom->num_freq_bands; i++ )
    2134             :             {
    2135             :                 float radius_length;
    2136             :                 float dv[3];
    2137             : 
    2138             :                 if ( hDirAC->hConfig->dec_param_estim == FALSE )
    2139             :                 {
    2140             :                     radius_length = cos( hDirAC->elevation[subframe_idx][i] * PI_OVER_180 );
    2141             :                     dv[0] = radius_length * cos( hDirAC->azimuth[subframe_idx][i] * PI_OVER_180 );
    2142             :                     dv[1] = radius_length * sin( hDirAC->azimuth[subframe_idx][i] * PI_OVER_180 );
    2143             :                     dv[2] = sin( hDirAC->elevation[subframe_idx][i] * PI_OVER_180 );
    2144             : 
    2145             :                     fwrite( dv, sizeof( float ), 3, fp_direction_vector );
    2146             :                     fwrite( &( hDirAC->diffuseness_vector[0][i] ), sizeof( float ), 1, fp_diffuseness );
    2147             :                     if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
    2148             :                     {
    2149             :                         reference_power[i] = Cldfb_RealBuffer[0][0][i] * Cldfb_RealBuffer[0][0][i] + Cldfb_ImagBuffer[0][0][i] * Cldfb_ImagBuffer[0][0][i];
    2150             :                     }
    2151             :                     fwrite( &( reference_power[i] ), sizeof( float ), 1, fp_referencePower );
    2152             :                 }
    2153             :                 else
    2154             :                 {
    2155             :                     radius_length = cos( hDirAC->elevation[index_slot][i] * PI_OVER_180 );
    2156             :                     dv[0] = radius_length * cos( hDirAC->azimuth[index_slot][i] * PI_OVER_180 );
    2157             :                     dv[1] = radius_length * sin( hDirAC->azimuth[index_slot][i] * PI_OVER_180 );
    2158             :                     dv[2] = sin( hDirAC->elevation[index_slot][i] * PI_OVER_180 );
    2159             : 
    2160             :                     fwrite( dv, sizeof( float ), 3, fp_direction_vector );
    2161             :                     fwrite( &( hDirAC->diffuseness_vector[index_slot][i] ), sizeof( float ), 1, fp_diffuseness );
    2162             :                     fwrite( &( reference_power[i] ), sizeof( float ), 1, fp_referencePower );
    2163             :                 }
    2164             :             }
    2165             :         }
    2166             : #endif
    2167             : 
    2168             :         /*-----------------------------------------------------------------*
    2169             :          * frequency domain decorrelation
    2170             :          *-----------------------------------------------------------------*/
    2171             : 
    2172    74923904 :         if ( hDirACRend->proto_signal_decorr_on == 1 )
    2173             :         {
    2174             :             /* decorrelate prototype frame */
    2175    70589460 :             if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
    2176             :             {
    2177    50659147 :                 ivas_dirac_dec_decorr_process( hSpatParamRendCom->num_freq_bands,
    2178    50659147 :                                                hDirACRend->num_outputs_diff,
    2179    50659147 :                                                hDirACRend->num_protos_diff,
    2180             :                                                hDirACRend->synthesisConf,
    2181             :                                                nchan_transport,
    2182    50659147 :                                                hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f + slot_idx * 2 * hSpatParamRendCom->num_freq_bands * hDirACRend->num_outputs_diff,
    2183    50659147 :                                                hDirACRend->num_protos_diff,
    2184    50659147 :                                                hDirACRend->proto_index_diff,
    2185    50659147 :                                                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 ),
    2186             :                                                onset_filter,
    2187             :                                                hDirACRend->h_freq_domain_decorr_ap_params,
    2188             :                                                hDirACRend->h_freq_domain_decorr_ap_state );
    2189             : 
    2190    50659147 :                 v_multc( onset_filter, 0.25f, onset_filter, hSpatParamRendCom->num_freq_bands );
    2191    50659147 :                 v_add( onset_filter, onset_filter_subframe, onset_filter_subframe, hSpatParamRendCom->num_freq_bands );
    2192    50659147 :                 p_onset_filter = onset_filter_subframe;
    2193             :             }
    2194             :             else
    2195             :             {
    2196    19930313 :                 ivas_dirac_dec_decorr_process( hSpatParamRendCom->num_freq_bands,
    2197    19930313 :                                                hDirACRend->num_outputs_diff,
    2198    19930313 :                                                hDirACRend->num_protos_diff,
    2199             :                                                hDirACRend->synthesisConf,
    2200             :                                                nchan_transport,
    2201    19930313 :                                                hDirACRend->proto_frame_f,
    2202    19930313 :                                                hDirACRend->num_protos_diff,
    2203    19930313 :                                                hDirACRend->proto_index_diff,
    2204             :                                                DirAC_mem.frame_dec_f,
    2205             :                                                onset_filter,
    2206             :                                                hDirACRend->h_freq_domain_decorr_ap_params,
    2207             :                                                hDirACRend->h_freq_domain_decorr_ap_state );
    2208             : 
    2209    19930313 :                 hDirACRend->proto_frame_dec_f = DirAC_mem.frame_dec_f;
    2210    19930313 :                 p_onset_filter = onset_filter;
    2211             :             }
    2212             :         }
    2213             :         else
    2214             :         {
    2215     4334444 :             if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
    2216             :             {
    2217           0 :                 set_f( onset_filter_subframe, 1.f, hSpatParamRendCom->num_freq_bands );
    2218           0 :                 p_onset_filter = onset_filter_subframe;
    2219             :             }
    2220             :             else
    2221             :             {
    2222             :                 /* no frequency domain decorrelation: use prototype frame */
    2223     4334444 :                 hDirACRend->proto_frame_dec_f = hDirACRend->proto_frame_f;
    2224     4334444 :                 p_onset_filter = NULL;
    2225             :             }
    2226             :         }
    2227             : 
    2228             :         /*-----------------------------------------------------------------*
    2229             :          * output synthesis
    2230             :          *-----------------------------------------------------------------*/
    2231             : 
    2232    74923904 :         if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_LS || hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_SHD )
    2233             :         {
    2234             :             /*Compute diffuse prototypes*/
    2235    22838057 :             ivas_dirac_dec_compute_diffuse_proto( hDirACRend, hSpatParamRendCom->num_freq_bands, slot_idx );
    2236             :         }
    2237             : 
    2238             :         /*Compute PSDs*/
    2239    74923904 :         ivas_dirac_dec_output_synthesis_process_slot( reference_power,
    2240             :                                                       p_onset_filter,
    2241             :                                                       azimuth,
    2242             :                                                       elevation,
    2243    74923904 :                                                       hSpatParamRendCom->diffuseness_vector[md_idx],
    2244             :                                                       hSpatParamRendCom,
    2245             :                                                       hDirACRend,
    2246             :                                                       st_ivas->hVBAPdata,
    2247             :                                                       hDirACRend->hOutSetup,
    2248             :                                                       nchan_transport,
    2249             :                                                       md_idx,
    2250             :                                                       hodirac_flag,
    2251    74923904 :                                                       hDirAC->hConfig->dec_param_estim );
    2252             : 
    2253    74923904 :         if ( hDirAC->hConfig->dec_param_estim )
    2254             :         {
    2255    42514324 :             float fac = 1.0f / (float) hSpatParamRendCom->subframe_nbslots[subframe_idx];
    2256    42514324 :             v_multc_acc( hSpatParamRendCom->diffuseness_vector[md_idx], fac, diffuseness_vector, hSpatParamRendCom->num_freq_bands );
    2257             :         }
    2258             : 
    2259    74923904 :         if ( hDirACRend->synthesisConf != DIRAC_SYNTHESIS_GAIN_SHD )
    2260             :         {
    2261    24264757 :             v_add( reference_power, reference_power_smooth, reference_power_smooth, hSpatParamRendCom->num_freq_bands );
    2262             :         }
    2263             :     }
    2264             : 
    2265    18874991 :     ivas_dirac_dec_output_synthesis_get_interpolator( &hDirACRend->h_output_synthesis_psd_params, hSpatParamRendCom->subframe_nbslots[subframe_idx] );
    2266             : 
    2267             : 
    2268    18874991 :     if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
    2269             :     {
    2270    12727266 :         ivas_dirac_dec_output_synthesis_process_subframe_gain_shd( Cldfb_RealBuffer,
    2271             :                                                                    Cldfb_ImagBuffer,
    2272             :                                                                    hSpatParamRendCom,
    2273             :                                                                    hDirACRend,
    2274             :                                                                    nchan_transport,
    2275    12727266 :                                                                    hSpatParamRendCom->subframe_nbslots[subframe_idx],
    2276             :                                                                    p_onset_filter,
    2277             :                                                                    diffuseness_vector,
    2278             :                                                                    hodirac_flag,
    2279    12727266 :                                                                    hDirAC->hConfig->dec_param_estim );
    2280             :     }
    2281             :     else
    2282             :     {
    2283             :         /* Determine encoding quality based additional smoothing factor */
    2284     6147725 :         float qualityBasedSmFactor = 1.0f;
    2285             : 
    2286     6147725 :         if ( st_ivas->hMasa != NULL )
    2287             :         {
    2288     5420789 :             qualityBasedSmFactor = st_ivas->hMasa->data.dir_decode_quality;
    2289     5420789 :             qualityBasedSmFactor *= qualityBasedSmFactor;
    2290             :         }
    2291             : 
    2292     6147725 :         ivas_dirac_dec_output_synthesis_process_subframe_psd_ls( Cldfb_RealBuffer,
    2293             :                                                                  Cldfb_ImagBuffer,
    2294             :                                                                  hSpatParamRendCom,
    2295             :                                                                  hDirACRend,
    2296     6147725 :                                                                  hSpatParamRendCom->subframe_nbslots[subframe_idx],
    2297             :                                                                  diffuseness_vector,
    2298             :                                                                  reference_power_smooth,
    2299             :                                                                  qualityBasedSmFactor,
    2300     6147725 :                                                                  hDirAC->hConfig->enc_param_start_band );
    2301             :     }
    2302             : 
    2303             :     /*-----------------------------------------------------------------*
    2304             :      * CLDFB synthesis (and binaural rendering)
    2305             :      *-----------------------------------------------------------------*/
    2306             : 
    2307    18874991 :     index_slot = slot_idx_start_cldfb_synth;
    2308             : 
    2309    18874991 :     if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
    2310             :     {
    2311             :         /* render objects in combined format onto the CICP19 channels for BINAURAL_ROOM_IR */
    2312     5313251 :         if ( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_SBA_MODE_DISC && st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
    2313             :         {
    2314             :             int16_t j, k, l;
    2315             :             int16_t num_objects, nchan_out_woLFE;
    2316             :             int16_t az1, el1;
    2317             :             int16_t n_slots_to_render;
    2318             :             int16_t n_samples_to_render;
    2319             :             float gain, prev_gain;
    2320             : 
    2321      700900 :             num_objects = st_ivas->nchan_ism;
    2322      700900 :             nchan_out_woLFE = st_ivas->hIntSetup.nchan_out_woLFE;
    2323      700900 :             n_slots_to_render = st_ivas->hSpar->subframe_nbslots[st_ivas->hSpar->subframes_rendered];
    2324      700900 :             n_samples_to_render = hSpatParamRendCom->num_freq_bands * n_slots_to_render;
    2325             : 
    2326      700900 :             if ( st_ivas->hCombinedOrientationData && st_ivas->hCombinedOrientationData->enableCombinedOrientation[0] )
    2327             :             {
    2328           0 :                 ivas_jbm_dec_get_adapted_linear_interpolator( n_samples_to_render, n_samples_to_render, st_ivas->hIsmRendererData->interpolator );
    2329           0 :                 st_ivas->hIsmRendererData->interp_offset = 0;
    2330             :             }
    2331             : 
    2332     2999452 :             for ( i = 0; i < num_objects; i++ )
    2333             :             {
    2334             :                 /* Combined rotation: rotate the object positions depending the head and external orientations */
    2335     2298552 :                 if ( st_ivas->hCombinedOrientationData != NULL && st_ivas->hCombinedOrientationData->enableCombinedOrientation[0] == 1 )
    2336             :                 {
    2337           0 :                     rotateAziEle( st_ivas->hIsmMetaData[i]->azimuth, st_ivas->hIsmMetaData[i]->elevation, &az1, &el1, st_ivas->hCombinedOrientationData->Rmat[0], st_ivas->hIntSetup.is_planar_setup );
    2338           0 :                     if ( st_ivas->hEFAPdata != NULL )
    2339             :                     {
    2340           0 :                         efap_determine_gains( st_ivas->hEFAPdata, st_ivas->hIsmRendererData->gains[i], az1, el1, EFAP_MODE_EFAP );
    2341           0 :                         v_multc( st_ivas->hIsmRendererData->gains[i], st_ivas->hIsmMetaData[i]->edited_gain, st_ivas->hIsmRendererData->gains[i], nchan_out_woLFE );
    2342             :                     }
    2343             :                 }
    2344             : 
    2345    27582624 :                 for ( j = 0; j < nchan_out_woLFE; j++ )
    2346             :                 {
    2347             : 
    2348    25284072 :                     gain = st_ivas->hIsmRendererData->gains[i][j];
    2349    25284072 :                     prev_gain = st_ivas->hIsmRendererData->prev_gains[i][j];
    2350    25284072 :                     if ( fabsf( gain ) > 0.0f || fabsf( prev_gain ) > 0.0f )
    2351             :                     {
    2352             :                         float *tc_re, *tc_im;
    2353             :                         float *w1, w2;
    2354             : 
    2355    12071816 :                         w1 = &st_ivas->hIsmRendererData->interpolator[st_ivas->hIsmRendererData->interp_offset];
    2356             : 
    2357    12071816 :                         tc_re = pppQMfFrame_ts_re[nchan_transport + i][0];
    2358    12071816 :                         tc_im = pppQMfFrame_ts_im[nchan_transport + i][0];
    2359             : 
    2360    60359080 :                         for ( k = 0; k < n_slots_to_render; k++ )
    2361             :                         {
    2362             :                             float g;
    2363             : 
    2364    48287264 :                             w2 = 1.0f - *w1;
    2365    48287264 :                             g = ( *w1 * gain + w2 * prev_gain );
    2366             : 
    2367  2540321504 :                             for ( l = 0; l < hSpatParamRendCom->num_freq_bands; l++ )
    2368             :                             {
    2369  2492034240 :                                 Cldfb_RealBuffer[j][k][l] += g * *( tc_re++ );
    2370  2492034240 :                                 Cldfb_ImagBuffer[j][k][l] += g * *( tc_im++ );
    2371             :                             }
    2372    48287264 :                             w1 += hSpatParamRendCom->num_freq_bands;
    2373             :                         }
    2374             :                     }
    2375             : 
    2376             :                     /* update here only in case of head rotation */
    2377    25284072 :                     if ( st_ivas->hCombinedOrientationData != NULL && st_ivas->hCombinedOrientationData->enableCombinedOrientation[0] == 1 )
    2378             :                     {
    2379           0 :                         st_ivas->hIsmRendererData->prev_gains[i][j] = gain;
    2380             :                     }
    2381             :                 }
    2382             :             }
    2383      700900 :             st_ivas->hIsmRendererData->interp_offset += hSpatParamRendCom->num_freq_bands * st_ivas->hSpar->subframe_nbslots[st_ivas->hSpar->subframes_rendered];
    2384             :         }
    2385             : 
    2386     5313251 :         if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    2387             :         {
    2388      623038 :             if ( st_ivas->hSplitBinRend->hCldfbDataOut != NULL )
    2389             :             {
    2390     1072322 :                 for ( slot_idx = 0; slot_idx < hSpatParamRendCom->subframe_nbslots[subframe_idx]; slot_idx++ )
    2391             :                 {
    2392    14420454 :                     for ( ch = 0; ch < st_ivas->hBinRenderer->nInChannels; ch++ )
    2393             :                     {
    2394    13572192 :                         mvr2r( Cldfb_RealBuffer[ch][slot_idx], st_ivas->hSplitBinRend->hCldfbDataOut->Cldfb_RealBuffer[ch][slot_idx_start + slot_idx], hSpatParamRendCom->num_freq_bands );
    2395    13572192 :                         mvr2r( Cldfb_ImagBuffer[ch][slot_idx], st_ivas->hSplitBinRend->hCldfbDataOut->Cldfb_ImagBuffer[ch][slot_idx_start + slot_idx], hSpatParamRendCom->num_freq_bands );
    2396             :                     }
    2397             :                 }
    2398      224060 :                 st_ivas->hSplitBinRend->hCldfbDataOut->config = st_ivas->hIntSetup.output_config;
    2399             :             }
    2400             :         }
    2401             : 
    2402             :         /* Perform binaural rendering */
    2403     5313251 :         if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    2404             :         {
    2405      623038 :             binRenderer_split( st_ivas->hBinRenderer, st_ivas->hSplitBinRend, st_ivas->hCombinedOrientationData, hSpatParamRendCom->subframe_nbslots[subframe_idx],
    2406             :                                Cldfb_RealBuffer_Binaural, Cldfb_ImagBuffer_Binaural, Cldfb_RealBuffer, Cldfb_ImagBuffer,
    2407             : #ifndef FIX_1119_SPLIT_RENDERING_VOIP
    2408             :                                slot_idx_start,
    2409             : #endif
    2410      623038 :                                hSpatParamRendCom->num_freq_bands, st_ivas->hDecoderConfig->nchan_out );
    2411             :         }
    2412             :         else
    2413             :         {
    2414     4690213 :             binRenderer( st_ivas->hBinRenderer, st_ivas->hCombinedOrientationData, hSpatParamRendCom->subframe_nbslots[subframe_idx],
    2415             :                          Cldfb_RealBuffer_Binaural, Cldfb_ImagBuffer_Binaural, Cldfb_RealBuffer, Cldfb_ImagBuffer );
    2416             :         }
    2417             : 
    2418             :         /* Inverse CLDFB*/
    2419    15939753 :         for ( ch = 0; ch < st_ivas->hDecoderConfig->nchan_out; ch++ )
    2420             :         {
    2421             :             /* open CLDFB buffer up to CLDFB_NO_CHANNELS_MAX bands for 48kHz */
    2422             :             float *RealBuffer[CLDFB_SLOTS_PER_SUBFRAME];
    2423             :             float *ImagBuffer[CLDFB_SLOTS_PER_SUBFRAME];
    2424             : 
    2425    52902482 :             for ( i = 0; i < hSpatParamRendCom->subframe_nbslots[subframe_idx]; i++ )
    2426             :             {
    2427    42275980 :                 RealBuffer[i] = Cldfb_RealBuffer_Binaural[0][ch][i];
    2428    42275980 :                 ImagBuffer[i] = Cldfb_ImagBuffer_Binaural[0][ch][i];
    2429             :             }
    2430             : 
    2431    10626502 :             cldfbSynthesis( RealBuffer, ImagBuffer, &( output_f[ch][index_slot * hSpatParamRendCom->num_freq_bands] ), hSpatParamRendCom->num_freq_bands * hSpatParamRendCom->subframe_nbslots[subframe_idx], st_ivas->cldfbSynDec[ch] );
    2432             :         }
    2433             :     }
    2434    13561740 :     else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
    2435             :     {
    2436    90795397 :         for ( ch = 0; ch < hDirACRend->hOutSetup.nchan_out_woLFE; ch++ )
    2437             :         {
    2438   413245032 :             for ( slot_idx = 0; slot_idx < hSpatParamRendCom->subframe_nbslots[subframe_idx]; slot_idx++ )
    2439             :             {
    2440   330252416 :                 mvr2r( Cldfb_RealBuffer[ch][slot_idx], pppQMfFrame_ts_re[ch][slot_idx], hSpatParamRendCom->num_freq_bands );
    2441   330252416 :                 mvr2r( Cldfb_ImagBuffer[ch][slot_idx], pppQMfFrame_ts_im[ch][slot_idx], hSpatParamRendCom->num_freq_bands );
    2442             :             }
    2443             :         }
    2444             :     }
    2445             :     else
    2446             :     {
    2447             :         float *RealBuffer[CLDFB_SLOTS_PER_SUBFRAME];
    2448             :         float *ImagBuffer[CLDFB_SLOTS_PER_SUBFRAME];
    2449             :         int16_t outchannels;
    2450             : 
    2451     5758959 :         idx_in = 0;
    2452     5758959 :         idx_lfe = 0;
    2453             : 
    2454     5758959 :         outchannels = hDirACRend->hOutSetup.nchan_out_woLFE + hDirACRend->hOutSetup.num_lfe;
    2455     5758959 :         if ( hDirACRend->hOutSetup.separateChannelEnabled && ( hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_5_1 ||
    2456      101352 :                                                                hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_7_1 ||
    2457       95412 :                                                                hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_5_1_2 ||
    2458       89472 :                                                                hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_5_1_4 ||
    2459       61388 :                                                                hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_7_1_4 ||
    2460           0 :                                                                ( hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hLsSetupCustom->separate_ch_found ) ) )
    2461             :         {
    2462      207740 :             outchannels++;
    2463             :         }
    2464             : 
    2465     5758959 :         if ( hDirACRend->hOutSetup.separateChannelEnabled && hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    2466           0 :         {
    2467             :             float tmp_separated[L_FRAME48k / MAX_PARAM_SPATIAL_SUBFRAMES];
    2468             :             float tmp_lfe[L_FRAME48k / MAX_PARAM_SPATIAL_SUBFRAMES];
    2469           0 :             const int16_t subframe_start_sample = index_slot * hSpatParamRendCom->num_freq_bands;
    2470           0 :             const int16_t num_samples_subframe = hSpatParamRendCom->num_freq_bands * hSpatParamRendCom->subframe_nbslots[subframe_idx];
    2471             : 
    2472             :             /* Move the separated and the LFE channels to temporary variables as spatial synthesis may overwrite current channels */
    2473           0 :             mvr2r( &( output_f[st_ivas->hOutSetup.separateChannelIndex][subframe_start_sample] ), tmp_separated, num_samples_subframe );
    2474           0 :             if ( hDirACRend->hOutSetup.num_lfe > 0 )
    2475             :             {
    2476           0 :                 mvr2r( &( output_f[LFE_CHANNEL][subframe_start_sample] ), tmp_lfe, num_samples_subframe );
    2477             :             }
    2478             : 
    2479           0 :             for ( ch = 0; ch < outchannels; ch++ )
    2480             :             {
    2481           0 :                 if ( ( hDirACRend->hOutSetup.num_lfe > 0 ) && ( hDirACRend->hOutSetup.index_lfe[idx_lfe] == ch ) )
    2482             :                 {
    2483             :                     /* Move the LFE channel to the correct place */
    2484           0 :                     mvr2r( tmp_lfe, &( output_f[ch][subframe_start_sample] ), num_samples_subframe );
    2485             : 
    2486           0 :                     if ( idx_lfe < ( hDirACRend->hOutSetup.num_lfe - 1 ) )
    2487             :                     {
    2488           0 :                         idx_lfe++;
    2489             :                     }
    2490             :                 }
    2491           0 :                 else if ( ( st_ivas->hLsSetupCustom->separate_ch_found ) && ( hDirACRend->hOutSetup.separateChannelIndex == ch ) )
    2492             :                 {
    2493             :                     /*  Move the separated channel to the correct place. Thus, the separated channel is
    2494             :                      *  combined with the synthesized channels here when there is a matching channel. */
    2495           0 :                     mvr2r( tmp_separated, &( output_f[ch][subframe_start_sample] ), num_samples_subframe );
    2496             :                 }
    2497             :                 else
    2498             :                 {
    2499             :                     /* open CLDFB buffer up to CLDFB_NO_CHANNELS_MAX bands for 48kHz */
    2500           0 :                     for ( i = 0; i < hSpatParamRendCom->subframe_nbslots[subframe_idx]; i++ )
    2501             :                     {
    2502           0 :                         RealBuffer[i] = Cldfb_RealBuffer[idx_in][i];
    2503           0 :                         ImagBuffer[i] = Cldfb_ImagBuffer[idx_in][i];
    2504             :                     }
    2505           0 :                     cldfbSynthesis( RealBuffer, ImagBuffer, &( output_f[ch][subframe_start_sample] ), num_samples_subframe, st_ivas->cldfbSynDec[idx_in] );
    2506             : 
    2507           0 :                     if ( !st_ivas->hLsSetupCustom->separate_ch_found )
    2508             :                     {
    2509             :                         /* Pan the separated channel and mix with the synthesized channels. Thus, the separated channel
    2510             :                          * is combined with the synthesized channels here when there is no matching channel. */
    2511           0 :                         v_multc_acc( tmp_separated, st_ivas->hLsSetupCustom->separate_ch_gains[idx_in], &( output_f[ch][subframe_start_sample] ), num_samples_subframe );
    2512             :                     }
    2513             : 
    2514           0 :                     idx_in++;
    2515             :                 }
    2516             :             }
    2517             :         }
    2518             :         else
    2519             :         {
    2520    51320203 :             for ( ch = 0; ch < outchannels; ch++ )
    2521             :             {
    2522    45561244 :                 if ( ( hDirACRend->hOutSetup.num_lfe > 0 ) && ( hDirACRend->hOutSetup.index_lfe[idx_lfe] == ch ) )
    2523             :                 {
    2524     3657250 :                     if ( st_ivas->mc_mode == MC_MODE_MCMASA && !hDirACRend->hOutSetup.separateChannelEnabled )
    2525             :                     {
    2526     2621260 :                         for ( i = 0; i < hSpatParamRendCom->subframe_nbslots[subframe_idx]; i++ )
    2527             :                         {
    2528     2097008 :                             RealBuffer[i] = Cldfb_RealBuffer[MAX_OUTPUT_CHANNELS - 1][i];
    2529     2097008 :                             ImagBuffer[i] = Cldfb_ImagBuffer[MAX_OUTPUT_CHANNELS - 1][i];
    2530             :                         }
    2531      524252 :                         cldfbSynthesis( RealBuffer, ImagBuffer, &( output_f[ch][index_slot * hSpatParamRendCom->num_freq_bands] ), hSpatParamRendCom->num_freq_bands * hSpatParamRendCom->subframe_nbslots[subframe_idx], st_ivas->cldfbSynDec[hDirACRend->hOutSetup.nchan_out_woLFE + idx_lfe] );
    2532             :                     }
    2533     3132998 :                     else if ( st_ivas->mc_mode == MC_MODE_MCMASA && hDirACRend->hOutSetup.separateChannelEnabled )
    2534             :                     {
    2535             :                         /* LFE has been synthesized in the time domain, do nothing. */
    2536             :                     }
    2537             :                     else
    2538             :                     {
    2539     2925258 :                         set_zero( &( output_f[ch][index_slot * hSpatParamRendCom->num_freq_bands] ), hSpatParamRendCom->subframe_nbslots[subframe_idx] * hSpatParamRendCom->num_freq_bands );
    2540             :                     }
    2541             : 
    2542     3657250 :                     if ( idx_lfe < ( hDirACRend->hOutSetup.num_lfe - 1 ) )
    2543             :                     {
    2544           0 :                         idx_lfe++;
    2545             :                     }
    2546             :                 }
    2547    41903994 :                 else if ( ( hDirACRend->hOutSetup.separateChannelEnabled ) && ( hDirACRend->hOutSetup.separateChannelIndex == ch ) )
    2548             :                 {
    2549             :                     /* The separated channel is already set to output_f[hOutSetup.separateChannelIndex]. Thus, the separated
    2550             :                      * channel is combined with the synthesized channels here. */
    2551             :                 }
    2552             :                 else
    2553             :                 {
    2554             :                     /* open CLDFB buffer up to CLDFB_NO_CHANNELS_MAX bands for 48kHz */
    2555   206909206 :                     for ( i = 0; i < hSpatParamRendCom->subframe_nbslots[subframe_idx]; i++ )
    2556             :                     {
    2557   165212952 :                         RealBuffer[i] = Cldfb_RealBuffer[idx_in][i];
    2558   165212952 :                         ImagBuffer[i] = Cldfb_ImagBuffer[idx_in][i];
    2559             :                     }
    2560    41696254 :                     cldfbSynthesis( RealBuffer, ImagBuffer, &( output_f[ch][index_slot * hSpatParamRendCom->num_freq_bands] ), hSpatParamRendCom->num_freq_bands * hSpatParamRendCom->subframe_nbslots[subframe_idx], st_ivas->cldfbSynDec[idx_in] );
    2561    41696254 :                     idx_in++;
    2562             :                 }
    2563             :             }
    2564             :         }
    2565             :     }
    2566    18874991 :     hSpatParamRendCom->slots_rendered += hSpatParamRendCom->subframe_nbslots[subframe_idx];
    2567    18874991 :     hSpatParamRendCom->subframes_rendered++;
    2568             : 
    2569    18874991 :     pop_wmops();
    2570             : 
    2571    18874991 :     return;
    2572             : }

Generated by: LCOV version 1.14