LCOV - code coverage report
Current view: top level - lib_rend - ivas_crend.c (source / functions) Hit Total Coverage
Test: Coverage on main -- conformance test test_26252.py @ efe53129c9ed87a5067dd0a8fb9dca41db9c4add Lines: 721 849 84.9 %
Date: 2026-02-12 06:30:15 Functions: 15 17 88.2 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2026 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 <stdint.h>
      34             : #include "options.h"
      35             : #include "prot.h"
      36             : #include "ivas_prot.h"
      37             : #include "ivas_prot_rend.h"
      38             : #include "ivas_cnst.h"
      39             : #include "ivas_rom_rend.h"
      40             : #include <math.h>
      41             : #include "ivas_rom_binaural_crend_head.h"
      42             : #include "ivas_stat_rend.h"
      43             : #include "lib_rend.h"
      44             : #ifdef DEBUGGING
      45             : #include "debug.h"
      46             : #endif
      47             : #include "wmc_auto.h"
      48             : 
      49             : 
      50             : /*-------------------------------------------------------------------------
      51             :  * ivas_Crend_hrtf_init()
      52             :  *
      53             :  * Initialize hHrtf handle
      54             :  *------------------------------------------------------------------------*/
      55             : 
      56         849 : ivas_error ivas_Crend_hrtf_init(
      57             :     HRTFS_CREND_DATA *hHrtf /* i/o: HRTF handle     */
      58             : )
      59             : {
      60             :     int16_t i, j;
      61             : 
      62         849 :     if ( hHrtf == NULL )
      63             :     {
      64           0 :         return IVAS_ERR_WRONG_PARAMS;
      65             :     }
      66             : 
      67         849 :     hHrtf->latency_s = 0;
      68         849 :     hHrtf->gain_lfe = 0;
      69         849 :     hHrtf->max_num_ir = 0;
      70         849 :     hHrtf->max_num_iterations = 0;
      71         849 :     hHrtf->index_frequency_max_diffuse = 0;
      72         849 :     hHrtf->same_inv_diffuse_weight = 1;
      73             : 
      74       14433 :     for ( i = 0; i < MAX_INTERN_CHANNELS; i++ )
      75             :     {
      76       40752 :         for ( j = 0; j < BINAURAL_CHANNELS; j++ )
      77             :         {
      78       27168 :             hHrtf->inv_diffuse_weight[j][i] = 0;
      79       27168 :             hHrtf->num_iterations[i][j] = 0;
      80       27168 :             hHrtf->pIndex_frequency_max[i][j] = NULL;
      81       27168 :             hHrtf->pOut_to_bin_re[i][j] = NULL;
      82       27168 :             hHrtf->pOut_to_bin_im[i][j] = NULL;
      83       27168 :             hHrtf->pOut_to_bin_re_dyn[i][j] = NULL;
      84       27168 :             hHrtf->pOut_to_bin_im_dyn[i][j] = NULL;
      85       27168 :             hHrtf->pIndex_frequency_max_dyn[i][j] = NULL;
      86             :         }
      87             :     }
      88             : 
      89        2547 :     for ( j = 0; j < BINAURAL_CHANNELS; j++ )
      90             :     {
      91        1698 :         hHrtf->num_iterations_diffuse[j] = 0;
      92        1698 :         hHrtf->pIndex_frequency_max_diffuse[j] = NULL;
      93        1698 :         hHrtf->pOut_to_bin_diffuse_re[j] = NULL;
      94        1698 :         hHrtf->pOut_to_bin_diffuse_im[j] = NULL;
      95        1698 :         hHrtf->pIndex_frequency_max_diffuse_dyn[j] = NULL;
      96        1698 :         hHrtf->pOut_to_bin_diffuse_re_dyn[j] = NULL;
      97        1698 :         hHrtf->pOut_to_bin_diffuse_im_dyn[j] = NULL;
      98             :     }
      99             : 
     100         849 :     hHrtf->init_from_rom = 1;
     101             : 
     102         849 :     return IVAS_ERR_OK;
     103             : }
     104             : 
     105             : 
     106             : /*-------------------------------------------------------------------------
     107             :  * ivas_hrtf_open()
     108             :  *
     109             :  * Open hHrtf handle for Crend renderer
     110             :  *------------------------------------------------------------------------*/
     111             : 
     112         821 : static ivas_error ivas_hrtf_open(
     113             :     HRTFS_CREND_HANDLE *hHrtf_out /* o  : HRTF handle     */
     114             : )
     115             : {
     116             :     HRTFS_CREND_HANDLE hHrtf;
     117             :     ivas_error error;
     118             : 
     119         821 :     if ( *hHrtf_out == NULL )
     120             :     {
     121         821 :         if ( ( hHrtf = (HRTFS_CREND_HANDLE) malloc( sizeof( HRTFS_CREND_DATA ) ) ) == NULL )
     122             :         {
     123           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Crend HRTFS Handle\n" );
     124             :         }
     125             : 
     126         821 :         if ( ( error = ivas_Crend_hrtf_init( hHrtf ) ) != IVAS_ERR_OK )
     127             :         {
     128           0 :             return error;
     129             :         }
     130             : 
     131         821 :         *hHrtf_out = hHrtf;
     132             :     }
     133             :     else
     134             :     {
     135           0 :         return IVAS_ERR_INTERNAL;
     136             :     }
     137             : 
     138         821 :     return IVAS_ERR_OK;
     139             : }
     140             : 
     141             : /*-------------------------------------------------------------------------
     142             :  * ivas_hrtf_close()
     143             :  *
     144             :  * Close hHrtf handle
     145             :  *------------------------------------------------------------------------*/
     146             : 
     147         821 : static void ivas_hrtf_close(
     148             :     HRTFS_CREND_HANDLE *hHrtf /* i/o: Crend HRTF handle         */
     149             : )
     150             : {
     151         821 :     if ( hHrtf == NULL || *hHrtf == NULL )
     152             :     {
     153           0 :         return;
     154             :     }
     155             : 
     156         821 :     free( *hHrtf );
     157         821 :     *hHrtf = NULL;
     158             : 
     159         821 :     return;
     160             : }
     161             : 
     162             : 
     163             : /*-------------------------------------------------------------------------
     164             :  * ivas_rend_initCrend()
     165             :  *
     166             :  * Allocate and initialize crend renderer handle
     167             :  *------------------------------------------------------------------------*/
     168             : 
     169         849 : static ivas_error ivas_rend_initCrend(
     170             :     CREND_WRAPPER *pCrend,
     171             :     const AUDIO_CONFIG inConfig,
     172             :     const AUDIO_CONFIG outConfig,
     173             :     HRTFS_CREND_HANDLE hHrtfCrend,
     174             :     const int16_t ext_rend_flag,
     175             :     const int32_t output_Fs )
     176             : {
     177             :     int16_t i, j, tmp, tmp2;
     178             :     int16_t nchan_in;
     179             :     IVAS_REND_AudioConfigType inConfigType;
     180             :     HRTFS_CREND_HANDLE hHrtf;
     181             :     ivas_error error;
     182             : 
     183         849 :     inConfigType = getAudioConfigType( inConfig );
     184         849 :     hHrtf = pCrend->hHrtfCrend;
     185             : 
     186             :     /* Do all error checks up front so that the nested if later is easier */
     187         849 :     if ( inConfigType != IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED && inConfigType != IVAS_REND_AUDIO_CONFIG_TYPE_AMBISONICS )
     188             :     {
     189           0 :         return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Encountered unsupported input config in Crend" );
     190             :     }
     191             : 
     192         849 :     if ( outConfig != IVAS_AUDIO_CONFIG_BINAURAL && outConfig != IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR && outConfig != IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB && outConfig != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED && outConfig != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
     193             :     {
     194           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Encountered unsupported output type in Crend" );
     195             :     }
     196             : 
     197         849 :     if ( hHrtf == NULL )
     198             :     {
     199         849 :         if ( hHrtfCrend != NULL && ext_rend_flag == 0 )
     200             :         {
     201             :             /* HRTF set loaded from binary file */
     202          28 :             hHrtf = hHrtfCrend;
     203          28 :             hHrtf->init_from_rom = 0;
     204             :         }
     205             :         else
     206             :         {
     207             :             /* create new handle when HRTF is loaded from ROM, or external renderer is used */
     208         821 :             if ( ( error = ivas_hrtf_open( &hHrtf ) ) != IVAS_ERR_OK )
     209             :             {
     210           0 :                 return error;
     211             :             }
     212             : 
     213         821 :             hHrtf->init_from_rom = 1;
     214         821 :             if ( hHrtfCrend != NULL )
     215             :             {
     216           0 :                 hHrtf->init_from_rom = 0;
     217             :             }
     218             :         }
     219             :     }
     220             : 
     221         849 :     if ( ( error = getAudioConfigNumChannels( inConfig, &nchan_in ) ) != IVAS_ERR_OK )
     222             :     {
     223           0 :         return error;
     224             :     }
     225         849 :     hHrtf->max_num_ir = nchan_in;
     226             : 
     227         849 :     if ( hHrtf->max_num_ir <= 0 )
     228             :     {
     229           0 :         return IVAS_ERR_INTERNAL_FATAL;
     230             :     }
     231             : 
     232         849 :     if ( hHrtf->init_from_rom )
     233             :     {
     234         821 :         if ( inConfigType == IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED )
     235             :         {
     236         687 :             hHrtf->max_num_ir -= 1; /* subtract LFE */
     237         687 :             hHrtf->gain_lfe = GAIN_LFE;
     238             : 
     239         687 :             if ( output_Fs == 48000 )
     240             :             {
     241         402 :                 if ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
     242             :                 {
     243         285 :                     hHrtf->latency_s = CRendBin_Combined_BRIR_latency_s;
     244         285 :                     hHrtf->max_num_iterations = CRendBin_Combined_BRIR_max_num_iterations_48kHz;
     245         285 :                     hHrtf->index_frequency_max_diffuse = CRendBin_Combined_BRIR_index_frequency_max_diffuse_48kHz;
     246             :                 }
     247             :                 else
     248             :                 {
     249         117 :                     hHrtf->latency_s = CRendBin_Combined_HRIR_latency_s;
     250         117 :                     hHrtf->max_num_iterations = CRendBin_Combined_HRIR_max_num_iterations_48kHz;
     251         117 :                     hHrtf->index_frequency_max_diffuse = CRendBin_Combined_HRIR_index_frequency_max_diffuse_48kHz;
     252             :                 }
     253             : 
     254        1206 :                 for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     255             :                 {
     256         804 :                     if ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
     257             :                     {
     258         570 :                         hHrtf->num_iterations_diffuse[j] = CRendBin_Combined_BRIR_num_iterations_diffuse_48kHz[j];
     259         570 :                         hHrtf->pIndex_frequency_max_diffuse[j] = CRendBin_Combined_BRIR_pIndex_frequency_max_diffuse_48kHz[j];
     260         570 :                         hHrtf->pOut_to_bin_diffuse_re[j] = CRendBin_Combined_BRIR_coeff_diffuse_re_48kHz[j];
     261         570 :                         hHrtf->pOut_to_bin_diffuse_im[j] = CRendBin_Combined_BRIR_coeff_diffuse_im_48kHz[j];
     262             :                     }
     263             :                     else
     264             :                     {
     265         234 :                         hHrtf->num_iterations_diffuse[j] = CRendBin_Combined_HRIR_num_iterations_diffuse_48kHz[j];
     266         234 :                         hHrtf->pIndex_frequency_max_diffuse[j] = CRendBin_Combined_HRIR_pIndex_frequency_max_diffuse_48kHz[j];
     267         234 :                         hHrtf->pOut_to_bin_diffuse_re[j] = CRendBin_Combined_HRIR_coeff_diffuse_re_48kHz[j];
     268         234 :                         hHrtf->pOut_to_bin_diffuse_im[j] = CRendBin_Combined_HRIR_coeff_diffuse_im_48kHz[j];
     269             :                     }
     270             :                 }
     271             :             }
     272         285 :             else if ( output_Fs == 32000 )
     273             :             {
     274         151 :                 if ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
     275             :                 {
     276         142 :                     hHrtf->latency_s = CRendBin_Combined_BRIR_latency_s;
     277         142 :                     hHrtf->max_num_iterations = CRendBin_Combined_BRIR_max_num_iterations_32kHz;
     278         142 :                     hHrtf->index_frequency_max_diffuse = CRendBin_Combined_BRIR_index_frequency_max_diffuse_32kHz;
     279             :                 }
     280             :                 else
     281             :                 {
     282           9 :                     hHrtf->latency_s = CRendBin_Combined_HRIR_latency_s;
     283           9 :                     hHrtf->max_num_iterations = CRendBin_Combined_HRIR_max_num_iterations_32kHz;
     284           9 :                     hHrtf->index_frequency_max_diffuse = CRendBin_Combined_HRIR_index_frequency_max_diffuse_32kHz;
     285             :                 }
     286             : 
     287         453 :                 for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     288             :                 {
     289         302 :                     if ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
     290             :                     {
     291         284 :                         hHrtf->num_iterations_diffuse[j] = CRendBin_Combined_BRIR_num_iterations_diffuse_32kHz[j];
     292         284 :                         hHrtf->pIndex_frequency_max_diffuse[j] = CRendBin_Combined_BRIR_pIndex_frequency_max_diffuse_32kHz[j];
     293         284 :                         hHrtf->pOut_to_bin_diffuse_re[j] = CRendBin_Combined_BRIR_coeff_diffuse_re_32kHz[j];
     294         284 :                         hHrtf->pOut_to_bin_diffuse_im[j] = CRendBin_Combined_BRIR_coeff_diffuse_im_32kHz[j];
     295             :                     }
     296             :                     else
     297             :                     {
     298          18 :                         hHrtf->num_iterations_diffuse[j] = CRendBin_Combined_HRIR_num_iterations_diffuse_32kHz[j];
     299          18 :                         hHrtf->pIndex_frequency_max_diffuse[j] = CRendBin_Combined_HRIR_pIndex_frequency_max_diffuse_32kHz[j];
     300          18 :                         hHrtf->pOut_to_bin_diffuse_re[j] = CRendBin_Combined_HRIR_coeff_diffuse_re_32kHz[j];
     301          18 :                         hHrtf->pOut_to_bin_diffuse_im[j] = CRendBin_Combined_HRIR_coeff_diffuse_im_32kHz[j];
     302             :                     }
     303             :                 }
     304             :             }
     305         134 :             else if ( output_Fs == 16000 )
     306             :             {
     307         134 :                 if ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
     308             :                 {
     309          65 :                     hHrtf->latency_s = CRendBin_Combined_BRIR_latency_s;
     310             : 
     311          65 :                     hHrtf->max_num_iterations = CRendBin_Combined_BRIR_max_num_iterations_16kHz;
     312          65 :                     hHrtf->index_frequency_max_diffuse = CRendBin_Combined_BRIR_index_frequency_max_diffuse_16kHz;
     313             :                 }
     314             :                 else
     315             :                 {
     316          69 :                     hHrtf->latency_s = CRendBin_Combined_HRIR_latency_s;
     317          69 :                     hHrtf->max_num_iterations = CRendBin_Combined_HRIR_max_num_iterations_16kHz;
     318          69 :                     hHrtf->index_frequency_max_diffuse = CRendBin_Combined_HRIR_index_frequency_max_diffuse_16kHz;
     319             :                 }
     320             : 
     321         402 :                 for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     322             :                 {
     323         268 :                     if ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
     324             :                     {
     325         130 :                         hHrtf->num_iterations_diffuse[j] = CRendBin_Combined_BRIR_num_iterations_diffuse_16kHz[j];
     326         130 :                         hHrtf->pIndex_frequency_max_diffuse[j] = CRendBin_Combined_BRIR_pIndex_frequency_max_diffuse_16kHz[j];
     327         130 :                         hHrtf->pOut_to_bin_diffuse_re[j] = CRendBin_Combined_BRIR_coeff_diffuse_re_16kHz[j];
     328         130 :                         hHrtf->pOut_to_bin_diffuse_im[j] = CRendBin_Combined_BRIR_coeff_diffuse_im_16kHz[j];
     329             :                     }
     330             :                     else
     331             :                     {
     332         138 :                         hHrtf->num_iterations_diffuse[j] = CRendBin_Combined_HRIR_num_iterations_diffuse_16kHz[j];
     333         138 :                         hHrtf->pIndex_frequency_max_diffuse[j] = CRendBin_Combined_HRIR_pIndex_frequency_max_diffuse_16kHz[j];
     334         138 :                         hHrtf->pOut_to_bin_diffuse_re[j] = CRendBin_Combined_HRIR_coeff_diffuse_re_16kHz[j];
     335         138 :                         hHrtf->pOut_to_bin_diffuse_im[j] = CRendBin_Combined_HRIR_coeff_diffuse_im_16kHz[j];
     336             :                     }
     337             :                 }
     338             :             }
     339             :             else
     340             :             {
     341           0 :                 return IVAS_ERROR( IVAS_ERR_INVALID_SAMPLING_RATE, "Encountered Unsupported sampling rate in Crend" );
     342             :             }
     343             : 
     344        6812 :             for ( i = 0; i < hHrtf->max_num_ir; i++ )
     345             :             {
     346        6125 :                 if ( inConfig == IVAS_AUDIO_CONFIG_5_1 )
     347             :                 {
     348        1130 :                     tmp = channelIndex_CICP6[i];
     349             :                 }
     350        4995 :                 else if ( inConfig == IVAS_AUDIO_CONFIG_7_1 )
     351             :                 {
     352          14 :                     tmp = channelIndex_CICP12[i];
     353             :                 }
     354        4981 :                 else if ( inConfig == IVAS_AUDIO_CONFIG_5_1_2 )
     355             :                 {
     356          49 :                     tmp = channelIndex_CICP14[i];
     357             :                 }
     358        4932 :                 else if ( inConfig == IVAS_AUDIO_CONFIG_5_1_4 )
     359             :                 {
     360         180 :                     tmp = channelIndex_CICP16[i];
     361             :                 }
     362        4752 :                 else if ( inConfig == IVAS_AUDIO_CONFIG_7_1_4 )
     363             :                 {
     364        4752 :                     tmp = channelIndex_CICP19[i];
     365             :                 }
     366             :                 else
     367             :                 {
     368           0 :                     return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Channel configuration not specified!\n\n" );
     369             :                 }
     370             : 
     371        6125 :                 if ( output_Fs == 48000 )
     372             :                 {
     373       10278 :                     for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     374             :                     {
     375        6852 :                         if ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
     376             :                         {
     377        5442 :                             hHrtf->inv_diffuse_weight[j][i] = CRendBin_Combined_BRIR_inv_diffuse_weight_48kHz[j][tmp];
     378        5442 :                             hHrtf->num_iterations[i][j] = CRendBin_Combined_BRIR_num_iterations_48kHz[tmp][j];
     379        5442 :                             hHrtf->pIndex_frequency_max[i][j] = CRendBin_Combined_BRIR_pIndex_frequency_max_48kHz[tmp][j];
     380        5442 :                             hHrtf->pOut_to_bin_re[i][j] = CRendBin_Combined_BRIR_coeff_re_48kHz[tmp][j];
     381        5442 :                             hHrtf->pOut_to_bin_im[i][j] = CRendBin_Combined_BRIR_coeff_im_48kHz[tmp][j];
     382             :                         }
     383             :                         else
     384             :                         {
     385        1410 :                             hHrtf->inv_diffuse_weight[j][i] = CRendBin_Combined_HRIR_inv_diffuse_weight_48kHz[j][tmp];
     386        1410 :                             hHrtf->num_iterations[i][j] = CRendBin_Combined_HRIR_num_iterations_48kHz[tmp][j];
     387        1410 :                             hHrtf->pIndex_frequency_max[i][j] = CRendBin_Combined_HRIR_pIndex_frequency_max_48kHz[tmp][j];
     388        1410 :                             hHrtf->pOut_to_bin_re[i][j] = CRendBin_Combined_HRIR_coeff_re_48kHz[tmp][j];
     389        1410 :                             hHrtf->pOut_to_bin_im[i][j] = CRendBin_Combined_HRIR_coeff_im_48kHz[tmp][j];
     390             :                         }
     391             :                     }
     392             :                 }
     393        2699 :                 else if ( output_Fs == 32000 )
     394             :                 {
     395        4893 :                     for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     396             :                     {
     397        3262 :                         if ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
     398             :                         {
     399        3116 :                             hHrtf->inv_diffuse_weight[j][i] = CRendBin_Combined_BRIR_inv_diffuse_weight_32kHz[j][tmp];
     400        3116 :                             hHrtf->num_iterations[i][j] = CRendBin_Combined_BRIR_num_iterations_32kHz[tmp][j];
     401        3116 :                             hHrtf->pIndex_frequency_max[i][j] = CRendBin_Combined_BRIR_pIndex_frequency_max_32kHz[tmp][j];
     402        3116 :                             hHrtf->pOut_to_bin_re[i][j] = CRendBin_Combined_BRIR_coeff_re_32kHz[tmp][j];
     403        3116 :                             hHrtf->pOut_to_bin_im[i][j] = CRendBin_Combined_BRIR_coeff_im_32kHz[tmp][j];
     404             :                         }
     405             :                         else
     406             :                         {
     407         146 :                             hHrtf->inv_diffuse_weight[j][i] = CRendBin_Combined_HRIR_inv_diffuse_weight_32kHz[j][tmp];
     408         146 :                             hHrtf->num_iterations[i][j] = CRendBin_Combined_HRIR_num_iterations_32kHz[tmp][j];
     409         146 :                             hHrtf->pIndex_frequency_max[i][j] = CRendBin_Combined_HRIR_pIndex_frequency_max_32kHz[tmp][j];
     410         146 :                             hHrtf->pOut_to_bin_re[i][j] = CRendBin_Combined_HRIR_coeff_re_32kHz[tmp][j];
     411         146 :                             hHrtf->pOut_to_bin_im[i][j] = CRendBin_Combined_HRIR_coeff_im_32kHz[tmp][j];
     412             :                         }
     413             :                     }
     414             :                 }
     415        1068 :                 else if ( output_Fs == 16000 )
     416             :                 {
     417        3204 :                     for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     418             :                     {
     419        2136 :                         if ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
     420             :                         {
     421        1390 :                             hHrtf->inv_diffuse_weight[j][i] = CRendBin_Combined_BRIR_inv_diffuse_weight_16kHz[j][tmp];
     422        1390 :                             hHrtf->num_iterations[i][j] = CRendBin_Combined_BRIR_num_iterations_16kHz[tmp][j];
     423        1390 :                             hHrtf->pIndex_frequency_max[i][j] = CRendBin_Combined_BRIR_pIndex_frequency_max_16kHz[tmp][j];
     424        1390 :                             hHrtf->pOut_to_bin_re[i][j] = CRendBin_Combined_BRIR_coeff_re_16kHz[tmp][j];
     425        1390 :                             hHrtf->pOut_to_bin_im[i][j] = CRendBin_Combined_BRIR_coeff_im_16kHz[tmp][j];
     426             :                         }
     427             :                         else
     428             :                         {
     429         746 :                             hHrtf->inv_diffuse_weight[j][i] = CRendBin_Combined_HRIR_inv_diffuse_weight_16kHz[j][tmp];
     430         746 :                             hHrtf->num_iterations[i][j] = CRendBin_Combined_HRIR_num_iterations_16kHz[tmp][j];
     431         746 :                             hHrtf->pIndex_frequency_max[i][j] = CRendBin_Combined_HRIR_pIndex_frequency_max_16kHz[tmp][j];
     432         746 :                             hHrtf->pOut_to_bin_re[i][j] = CRendBin_Combined_HRIR_coeff_re_16kHz[tmp][j];
     433         746 :                             hHrtf->pOut_to_bin_im[i][j] = CRendBin_Combined_HRIR_coeff_im_16kHz[tmp][j];
     434             :                         }
     435             :                     }
     436             :                 }
     437             :                 else
     438             :                 {
     439           0 :                     return IVAS_ERROR( IVAS_ERR_INVALID_SAMPLING_RATE, "Encountered Unsupported sampling rate in Crend" );
     440             :                 }
     441             :             }
     442             :         }
     443         134 :         else if ( inConfigType == IVAS_REND_AUDIO_CONFIG_TYPE_AMBISONICS )
     444             :         {
     445         134 :             if ( inConfig == IVAS_AUDIO_CONFIG_HOA3 )
     446             :             {
     447          80 :                 if ( output_Fs == 48000 )
     448             :                 {
     449          60 :                     hHrtf->latency_s = CRendBin_HOA3_HRIR_latency_s;
     450          60 :                     hHrtf->max_num_iterations = CRendBin_HOA3_HRIR_max_num_iterations_48kHz;
     451          60 :                     hHrtf->index_frequency_max_diffuse = CRendBin_HOA3_HRIR_index_frequency_max_diffuse_48kHz;
     452             : 
     453        1020 :                     for ( i = 0; i < hHrtf->max_num_ir; i++ )
     454             :                     {
     455        2880 :                         for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     456             :                         {
     457        1920 :                             hHrtf->inv_diffuse_weight[j][i] = CRendBin_HOA3_HRIR_inv_diffuse_weight_48kHz[j][i];
     458        1920 :                             hHrtf->num_iterations[i][j] = CRendBin_HOA3_HRIR_num_iterations_48kHz[i][j];
     459        1920 :                             hHrtf->pIndex_frequency_max[i][j] = CRendBin_HOA3_HRIR_pIndex_frequency_max_48kHz[i][j];
     460        1920 :                             hHrtf->pOut_to_bin_re[i][j] = CRendBin_HOA3_HRIR_coeff_re_48kHz[i][j];
     461        1920 :                             hHrtf->pOut_to_bin_im[i][j] = CRendBin_HOA3_HRIR_coeff_im_48kHz[i][j];
     462             :                         }
     463             :                     }
     464         180 :                     for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     465             :                     {
     466         120 :                         hHrtf->num_iterations_diffuse[j] = CRendBin_HOA3_HRIR_num_iterations_diffuse_48kHz[j];
     467         120 :                         hHrtf->pIndex_frequency_max_diffuse[j] = CRendBin_HOA3_HRIR_pIndex_frequency_max_diffuse_48kHz[j];
     468         120 :                         hHrtf->pOut_to_bin_diffuse_re[j] = CRendBin_HOA3_HRIR_coeff_diffuse_re_48kHz[j];
     469         120 :                         hHrtf->pOut_to_bin_diffuse_im[j] = CRendBin_HOA3_HRIR_coeff_diffuse_im_48kHz[j];
     470             :                     }
     471             :                 }
     472          20 :                 else if ( output_Fs == 32000 )
     473             :                 {
     474           9 :                     hHrtf->latency_s = CRendBin_HOA3_HRIR_latency_s;
     475           9 :                     hHrtf->max_num_iterations = CRendBin_HOA3_HRIR_max_num_iterations_32kHz;
     476           9 :                     hHrtf->index_frequency_max_diffuse = CRendBin_HOA3_HRIR_index_frequency_max_diffuse_32kHz;
     477             : 
     478         153 :                     for ( i = 0; i < hHrtf->max_num_ir; i++ )
     479             :                     {
     480         432 :                         for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     481             :                         {
     482         288 :                             hHrtf->inv_diffuse_weight[j][i] = CRendBin_HOA3_HRIR_inv_diffuse_weight_32kHz[j][i];
     483         288 :                             hHrtf->num_iterations[i][j] = CRendBin_HOA3_HRIR_num_iterations_32kHz[i][j];
     484         288 :                             hHrtf->pIndex_frequency_max[i][j] = CRendBin_HOA3_HRIR_pIndex_frequency_max_32kHz[i][j];
     485         288 :                             hHrtf->pOut_to_bin_re[i][j] = CRendBin_HOA3_HRIR_coeff_re_32kHz[i][j];
     486         288 :                             hHrtf->pOut_to_bin_im[i][j] = CRendBin_HOA3_HRIR_coeff_im_32kHz[i][j];
     487             :                         }
     488             :                     }
     489             : 
     490          27 :                     for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     491             :                     {
     492          18 :                         hHrtf->num_iterations_diffuse[j] = CRendBin_HOA3_HRIR_num_iterations_diffuse_32kHz[j];
     493          18 :                         hHrtf->pIndex_frequency_max_diffuse[j] = CRendBin_HOA3_HRIR_pIndex_frequency_max_diffuse_32kHz[j];
     494          18 :                         hHrtf->pOut_to_bin_diffuse_re[j] = CRendBin_HOA3_HRIR_coeff_diffuse_re_32kHz[j];
     495          18 :                         hHrtf->pOut_to_bin_diffuse_im[j] = CRendBin_HOA3_HRIR_coeff_diffuse_im_32kHz[j];
     496             :                     }
     497             :                 }
     498          11 :                 else if ( output_Fs == 16000 )
     499             :                 {
     500          11 :                     hHrtf->latency_s = CRendBin_HOA3_HRIR_latency_s;
     501          11 :                     hHrtf->max_num_iterations = CRendBin_HOA3_HRIR_max_num_iterations_16kHz;
     502          11 :                     hHrtf->index_frequency_max_diffuse = CRendBin_HOA3_HRIR_index_frequency_max_diffuse_16kHz;
     503             : 
     504         187 :                     for ( i = 0; i < hHrtf->max_num_ir; i++ )
     505             :                     {
     506         528 :                         for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     507             :                         {
     508         352 :                             hHrtf->inv_diffuse_weight[j][i] = CRendBin_HOA3_HRIR_inv_diffuse_weight_16kHz[j][i];
     509         352 :                             hHrtf->num_iterations[i][j] = CRendBin_HOA3_HRIR_num_iterations_16kHz[i][j];
     510         352 :                             hHrtf->pIndex_frequency_max[i][j] = CRendBin_HOA3_HRIR_pIndex_frequency_max_16kHz[i][j];
     511         352 :                             hHrtf->pOut_to_bin_re[i][j] = CRendBin_HOA3_HRIR_coeff_re_16kHz[i][j];
     512         352 :                             hHrtf->pOut_to_bin_im[i][j] = CRendBin_HOA3_HRIR_coeff_im_16kHz[i][j];
     513             :                         }
     514             :                     }
     515             : 
     516          33 :                     for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     517             :                     {
     518          22 :                         hHrtf->num_iterations_diffuse[j] = CRendBin_HOA3_HRIR_num_iterations_diffuse_16kHz[j];
     519          22 :                         hHrtf->pIndex_frequency_max_diffuse[j] = CRendBin_HOA3_HRIR_pIndex_frequency_max_diffuse_16kHz[j];
     520          22 :                         hHrtf->pOut_to_bin_diffuse_re[j] = CRendBin_HOA3_HRIR_coeff_diffuse_re_16kHz[j];
     521          22 :                         hHrtf->pOut_to_bin_diffuse_im[j] = CRendBin_HOA3_HRIR_coeff_diffuse_im_16kHz[j];
     522             :                     }
     523             :                 }
     524             :                 else
     525             :                 {
     526           0 :                     return IVAS_ERROR( IVAS_ERR_INVALID_SAMPLING_RATE, "Encountered Unsupported sampling rate in Crend" );
     527             :                 }
     528             :             }
     529          54 :             else if ( inConfig == IVAS_AUDIO_CONFIG_HOA2 )
     530             :             {
     531          20 :                 if ( output_Fs == 48000 )
     532             :                 {
     533          12 :                     hHrtf->latency_s = CRendBin_HOA2_HRIR_latency_s;
     534          12 :                     hHrtf->max_num_iterations = CRendBin_HOA2_HRIR_max_num_iterations_48kHz;
     535          12 :                     hHrtf->index_frequency_max_diffuse = CRendBin_HOA2_HRIR_index_frequency_max_diffuse_48kHz;
     536             : 
     537         120 :                     for ( i = 0; i < hHrtf->max_num_ir; i++ )
     538             :                     {
     539         324 :                         for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     540             :                         {
     541         216 :                             hHrtf->inv_diffuse_weight[j][i] = CRendBin_HOA2_HRIR_inv_diffuse_weight_48kHz[j][i];
     542         216 :                             hHrtf->num_iterations[i][j] = CRendBin_HOA2_HRIR_num_iterations_48kHz[i][j];
     543         216 :                             hHrtf->pIndex_frequency_max[i][j] = CRendBin_HOA2_HRIR_pIndex_frequency_max_48kHz[i][j];
     544         216 :                             hHrtf->pOut_to_bin_re[i][j] = CRendBin_HOA2_HRIR_coeff_re_48kHz[i][j];
     545         216 :                             hHrtf->pOut_to_bin_im[i][j] = CRendBin_HOA2_HRIR_coeff_im_48kHz[i][j];
     546             :                         }
     547             :                     }
     548          36 :                     for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     549             :                     {
     550          24 :                         hHrtf->num_iterations_diffuse[j] = CRendBin_HOA2_HRIR_num_iterations_diffuse_48kHz[j];
     551          24 :                         hHrtf->pIndex_frequency_max_diffuse[j] = CRendBin_HOA2_HRIR_pIndex_frequency_max_diffuse_48kHz[j];
     552          24 :                         hHrtf->pOut_to_bin_diffuse_re[j] = CRendBin_HOA2_HRIR_coeff_diffuse_re_48kHz[j];
     553          24 :                         hHrtf->pOut_to_bin_diffuse_im[j] = CRendBin_HOA2_HRIR_coeff_diffuse_im_48kHz[j];
     554             :                     }
     555             :                 }
     556           8 :                 else if ( output_Fs == 32000 )
     557             :                 {
     558           4 :                     hHrtf->latency_s = CRendBin_HOA2_HRIR_latency_s;
     559           4 :                     hHrtf->max_num_iterations = CRendBin_HOA2_HRIR_max_num_iterations_32kHz;
     560           4 :                     hHrtf->index_frequency_max_diffuse = CRendBin_HOA2_HRIR_index_frequency_max_diffuse_32kHz;
     561             : 
     562          40 :                     for ( i = 0; i < hHrtf->max_num_ir; i++ )
     563             :                     {
     564         108 :                         for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     565             :                         {
     566          72 :                             hHrtf->inv_diffuse_weight[j][i] = CRendBin_HOA2_HRIR_inv_diffuse_weight_32kHz[j][i];
     567          72 :                             hHrtf->num_iterations[i][j] = CRendBin_HOA2_HRIR_num_iterations_32kHz[i][j];
     568          72 :                             hHrtf->pIndex_frequency_max[i][j] = CRendBin_HOA2_HRIR_pIndex_frequency_max_32kHz[i][j];
     569          72 :                             hHrtf->pOut_to_bin_re[i][j] = CRendBin_HOA2_HRIR_coeff_re_32kHz[i][j];
     570          72 :                             hHrtf->pOut_to_bin_im[i][j] = CRendBin_HOA2_HRIR_coeff_im_32kHz[i][j];
     571             :                         }
     572             :                     }
     573             : 
     574          12 :                     for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     575             :                     {
     576           8 :                         hHrtf->num_iterations_diffuse[j] = CRendBin_HOA2_HRIR_num_iterations_diffuse_32kHz[j];
     577           8 :                         hHrtf->pIndex_frequency_max_diffuse[j] = CRendBin_HOA2_HRIR_pIndex_frequency_max_diffuse_32kHz[j];
     578           8 :                         hHrtf->pOut_to_bin_diffuse_re[j] = CRendBin_HOA2_HRIR_coeff_diffuse_re_32kHz[j];
     579           8 :                         hHrtf->pOut_to_bin_diffuse_im[j] = CRendBin_HOA2_HRIR_coeff_diffuse_im_32kHz[j];
     580             :                     }
     581             :                 }
     582           4 :                 else if ( output_Fs == 16000 )
     583             :                 {
     584           4 :                     hHrtf->latency_s = CRendBin_HOA2_HRIR_latency_s;
     585           4 :                     hHrtf->max_num_iterations = CRendBin_HOA2_HRIR_max_num_iterations_16kHz;
     586           4 :                     hHrtf->index_frequency_max_diffuse = CRendBin_HOA2_HRIR_index_frequency_max_diffuse_16kHz;
     587             : 
     588          40 :                     for ( i = 0; i < hHrtf->max_num_ir; i++ )
     589             :                     {
     590         108 :                         for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     591             :                         {
     592          72 :                             hHrtf->inv_diffuse_weight[j][i] = CRendBin_HOA2_HRIR_inv_diffuse_weight_16kHz[j][i];
     593          72 :                             hHrtf->num_iterations[i][j] = CRendBin_HOA2_HRIR_num_iterations_16kHz[i][j];
     594          72 :                             hHrtf->pIndex_frequency_max[i][j] = CRendBin_HOA2_HRIR_pIndex_frequency_max_16kHz[i][j];
     595          72 :                             hHrtf->pOut_to_bin_re[i][j] = CRendBin_HOA2_HRIR_coeff_re_16kHz[i][j];
     596          72 :                             hHrtf->pOut_to_bin_im[i][j] = CRendBin_HOA2_HRIR_coeff_im_16kHz[i][j];
     597             :                         }
     598             :                     }
     599             : 
     600          12 :                     for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     601             :                     {
     602           8 :                         hHrtf->num_iterations_diffuse[j] = CRendBin_HOA2_HRIR_num_iterations_diffuse_16kHz[j];
     603           8 :                         hHrtf->pIndex_frequency_max_diffuse[j] = CRendBin_HOA2_HRIR_pIndex_frequency_max_diffuse_16kHz[j];
     604           8 :                         hHrtf->pOut_to_bin_diffuse_re[j] = CRendBin_HOA2_HRIR_coeff_diffuse_re_16kHz[j];
     605           8 :                         hHrtf->pOut_to_bin_diffuse_im[j] = CRendBin_HOA2_HRIR_coeff_diffuse_im_16kHz[j];
     606             :                     }
     607             :                 }
     608             :                 else
     609             :                 {
     610           0 :                     return IVAS_ERROR( IVAS_ERR_INVALID_SAMPLING_RATE, "Encountered Unsupported sampling rate in Crend" );
     611             :                 }
     612             :             }
     613          34 :             else if ( inConfig == IVAS_AUDIO_CONFIG_FOA )
     614             :             {
     615          34 :                 if ( output_Fs == 48000 )
     616             :                 {
     617          17 :                     hHrtf->latency_s = CRendBin_FOA_HRIR_latency_s;
     618          17 :                     hHrtf->max_num_iterations = CRendBin_FOA_HRIR_max_num_iterations_48kHz;
     619          17 :                     hHrtf->index_frequency_max_diffuse = CRendBin_FOA_HRIR_index_frequency_max_diffuse_48kHz;
     620             : 
     621          85 :                     for ( i = 0; i < hHrtf->max_num_ir; i++ )
     622             :                     {
     623         204 :                         for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     624             :                         {
     625         136 :                             hHrtf->inv_diffuse_weight[j][i] = CRendBin_FOA_HRIR_inv_diffuse_weight_48kHz[j][i];
     626         136 :                             hHrtf->num_iterations[i][j] = CRendBin_FOA_HRIR_num_iterations_48kHz[i][j];
     627         136 :                             hHrtf->pIndex_frequency_max[i][j] = CRendBin_FOA_HRIR_pIndex_frequency_max_48kHz[i][j];
     628         136 :                             hHrtf->pOut_to_bin_re[i][j] = CRendBin_FOA_HRIR_coeff_re_48kHz[i][j];
     629         136 :                             hHrtf->pOut_to_bin_im[i][j] = CRendBin_FOA_HRIR_coeff_im_48kHz[i][j];
     630             :                         }
     631             :                     }
     632          51 :                     for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     633             :                     {
     634          34 :                         hHrtf->num_iterations_diffuse[j] = CRendBin_FOA_HRIR_num_iterations_diffuse_48kHz[j];
     635          34 :                         hHrtf->pIndex_frequency_max_diffuse[j] = CRendBin_FOA_HRIR_pIndex_frequency_max_diffuse_48kHz[j];
     636          34 :                         hHrtf->pOut_to_bin_diffuse_re[j] = CRendBin_FOA_HRIR_coeff_diffuse_re_48kHz[j];
     637          34 :                         hHrtf->pOut_to_bin_diffuse_im[j] = CRendBin_FOA_HRIR_coeff_diffuse_im_48kHz[j];
     638             :                     }
     639             :                 }
     640          17 :                 else if ( output_Fs == 32000 )
     641             :                 {
     642           9 :                     hHrtf->latency_s = CRendBin_FOA_HRIR_latency_s;
     643           9 :                     hHrtf->max_num_iterations = CRendBin_FOA_HRIR_max_num_iterations_32kHz;
     644           9 :                     hHrtf->index_frequency_max_diffuse = CRendBin_FOA_HRIR_index_frequency_max_diffuse_32kHz;
     645             : 
     646          45 :                     for ( i = 0; i < hHrtf->max_num_ir; i++ )
     647             :                     {
     648         108 :                         for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     649             :                         {
     650          72 :                             hHrtf->inv_diffuse_weight[j][i] = CRendBin_FOA_HRIR_inv_diffuse_weight_32kHz[j][i];
     651          72 :                             hHrtf->num_iterations[i][j] = CRendBin_FOA_HRIR_num_iterations_32kHz[i][j];
     652          72 :                             hHrtf->pIndex_frequency_max[i][j] = CRendBin_FOA_HRIR_pIndex_frequency_max_32kHz[i][j];
     653          72 :                             hHrtf->pOut_to_bin_re[i][j] = CRendBin_FOA_HRIR_coeff_re_32kHz[i][j];
     654          72 :                             hHrtf->pOut_to_bin_im[i][j] = CRendBin_FOA_HRIR_coeff_im_32kHz[i][j];
     655             :                         }
     656             :                     }
     657             : 
     658          27 :                     for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     659             :                     {
     660          18 :                         hHrtf->num_iterations_diffuse[j] = CRendBin_FOA_HRIR_num_iterations_diffuse_32kHz[j];
     661          18 :                         hHrtf->pIndex_frequency_max_diffuse[j] = CRendBin_FOA_HRIR_pIndex_frequency_max_diffuse_32kHz[j];
     662          18 :                         hHrtf->pOut_to_bin_diffuse_re[j] = CRendBin_FOA_HRIR_coeff_diffuse_re_32kHz[j];
     663          18 :                         hHrtf->pOut_to_bin_diffuse_im[j] = CRendBin_FOA_HRIR_coeff_diffuse_im_32kHz[j];
     664             :                     }
     665             :                 }
     666           8 :                 else if ( output_Fs == 16000 )
     667             :                 {
     668           8 :                     hHrtf->latency_s = CRendBin_FOA_HRIR_latency_s;
     669           8 :                     hHrtf->max_num_iterations = CRendBin_FOA_HRIR_max_num_iterations_16kHz;
     670           8 :                     hHrtf->index_frequency_max_diffuse = CRendBin_FOA_HRIR_index_frequency_max_diffuse_16kHz;
     671             : 
     672          40 :                     for ( i = 0; i < hHrtf->max_num_ir; i++ )
     673             :                     {
     674          96 :                         for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     675             :                         {
     676          64 :                             hHrtf->inv_diffuse_weight[j][i] = CRendBin_FOA_HRIR_inv_diffuse_weight_16kHz[j][i];
     677          64 :                             hHrtf->num_iterations[i][j] = CRendBin_FOA_HRIR_num_iterations_16kHz[i][j];
     678          64 :                             hHrtf->pIndex_frequency_max[i][j] = CRendBin_FOA_HRIR_pIndex_frequency_max_16kHz[i][j];
     679          64 :                             hHrtf->pOut_to_bin_re[i][j] = CRendBin_FOA_HRIR_coeff_re_16kHz[i][j];
     680          64 :                             hHrtf->pOut_to_bin_im[i][j] = CRendBin_FOA_HRIR_coeff_im_16kHz[i][j];
     681             :                         }
     682             :                     }
     683             : 
     684          24 :                     for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     685             :                     {
     686          16 :                         hHrtf->num_iterations_diffuse[j] = CRendBin_FOA_HRIR_num_iterations_diffuse_16kHz[j];
     687          16 :                         hHrtf->pIndex_frequency_max_diffuse[j] = CRendBin_FOA_HRIR_pIndex_frequency_max_diffuse_16kHz[j];
     688          16 :                         hHrtf->pOut_to_bin_diffuse_re[j] = CRendBin_FOA_HRIR_coeff_diffuse_re_16kHz[j];
     689          16 :                         hHrtf->pOut_to_bin_diffuse_im[j] = CRendBin_FOA_HRIR_coeff_diffuse_im_16kHz[j];
     690             :                     }
     691             :                 }
     692             :                 else
     693             :                 {
     694           0 :                     return IVAS_ERROR( IVAS_ERR_INVALID_SAMPLING_RATE, "Encountered Unsupported sampling rate in Crend" );
     695             :                 }
     696             :             }
     697             :             else
     698             :             {
     699           0 :                 return IVAS_ERROR( IVAS_ERR_INVALID_INPUT_FORMAT, "Encountered unsupported input config in Crend" );
     700             :             }
     701             :         }
     702             :         else
     703             :         {
     704           0 :             return IVAS_ERROR( IVAS_ERR_INTERNAL, "Unsupported renderer type in Crend" );
     705             :         }
     706             :     }
     707             :     else
     708             :     {
     709          28 :         if ( inConfigType == IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED )
     710             :         {
     711          28 :             hHrtf->max_num_ir -= 1; /* subtract LFE */
     712          28 :             hHrtf->gain_lfe = GAIN_LFE;
     713             :         }
     714             : 
     715          28 :         if ( ext_rend_flag == 1 )
     716             :         {
     717           0 :             if ( hHrtfCrend == NULL )
     718             :             {
     719           0 :                 return IVAS_ERROR( IVAS_ERR_UNEXPECTED_NULL_POINTER, "Invalid function parameters " );
     720             :             }
     721           0 :             hHrtf->latency_s = hHrtfCrend->latency_s;
     722           0 :             hHrtf->max_num_iterations = hHrtfCrend->max_num_iterations;
     723           0 :             hHrtf->index_frequency_max_diffuse = hHrtfCrend->index_frequency_max_diffuse;
     724             : 
     725           0 :             for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     726             :             {
     727           0 :                 hHrtf->num_iterations_diffuse[j] = hHrtfCrend->num_iterations_diffuse[j];
     728           0 :                 hHrtf->pIndex_frequency_max_diffuse[j] = hHrtfCrend->pIndex_frequency_max_diffuse[j];
     729           0 :                 hHrtf->pOut_to_bin_diffuse_re[j] = hHrtfCrend->pOut_to_bin_diffuse_re[j];
     730           0 :                 hHrtf->pOut_to_bin_diffuse_im[j] = hHrtfCrend->pOut_to_bin_diffuse_im[j];
     731             :             }
     732             :         }
     733             : 
     734          28 :         if ( inConfig == IVAS_AUDIO_CONFIG_7_1 && ext_rend_flag == 0 )
     735             :         {
     736             :             /* hack to enable pointers swapping - needed when indexes of 'channelIndex_CICPx[]' are not in increasing order */
     737           0 :             for ( i = 5; i < hHrtf->max_num_ir; i++ )
     738             :             {
     739           0 :                 tmp = channelIndex_CICP12[i];
     740           0 :                 tmp2 = channelIndex_CICP14[i];
     741             : 
     742           0 :                 for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     743             :                 {
     744           0 :                     hHrtf->inv_diffuse_weight[j][tmp2] = hHrtfCrend->inv_diffuse_weight[j][tmp];
     745           0 :                     hHrtf->num_iterations[tmp2][j] = hHrtfCrend->num_iterations[tmp][j];
     746           0 :                     hHrtf->pIndex_frequency_max[tmp2][j] = hHrtfCrend->pIndex_frequency_max[tmp][j];
     747           0 :                     hHrtf->pOut_to_bin_re[tmp2][j] = hHrtfCrend->pOut_to_bin_re[tmp][j];
     748           0 :                     hHrtf->pOut_to_bin_im[tmp2][j] = hHrtfCrend->pOut_to_bin_im[tmp][j];
     749             :                 }
     750             :             }
     751             :         }
     752             : 
     753         332 :         for ( i = 0; i < hHrtf->max_num_ir; i++ )
     754             :         {
     755         304 :             if ( inConfig == IVAS_AUDIO_CONFIG_5_1 )
     756             :             {
     757           0 :                 tmp = channelIndex_CICP6[i];
     758             :             }
     759         304 :             else if ( inConfig == IVAS_AUDIO_CONFIG_7_1 )
     760             :             {
     761           0 :                 if ( ext_rend_flag == 0 )
     762             :                 {
     763           0 :                     tmp = channelIndex_CICP14[i];
     764             :                 }
     765             :                 else
     766             :                 {
     767           0 :                     tmp = channelIndex_CICP12[i];
     768             :                 }
     769             :             }
     770         304 :             else if ( inConfig == IVAS_AUDIO_CONFIG_5_1_2 )
     771             :             {
     772           0 :                 tmp = channelIndex_CICP14[i];
     773             :             }
     774         304 :             else if ( inConfig == IVAS_AUDIO_CONFIG_5_1_4 )
     775             :             {
     776          18 :                 tmp = channelIndex_CICP16[i];
     777             :             }
     778         286 :             else if ( inConfig == IVAS_AUDIO_CONFIG_7_1_4 )
     779             :             {
     780         286 :                 tmp = channelIndex_CICP19[i];
     781             :             }
     782           0 :             else if ( inConfigType == IVAS_REND_AUDIO_CONFIG_TYPE_AMBISONICS )
     783             :             {
     784           0 :                 tmp = i;
     785             :             }
     786             :             else
     787             :             {
     788           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Channel configuration not specified!\n\n" );
     789             :             }
     790             : 
     791         912 :             for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     792             :             {
     793         608 :                 hHrtf->inv_diffuse_weight[j][i] = hHrtfCrend->inv_diffuse_weight[j][tmp];
     794         608 :                 hHrtf->num_iterations[i][j] = hHrtfCrend->num_iterations[tmp][j];
     795             : 
     796         608 :                 if ( ext_rend_flag == 0 )
     797             :                 {
     798             :                     /* in case of HRTF handle reuse, swap the pointers for correct deallocation at the exit */
     799             : #define WMC_TOOL_SKIP
     800         608 :                     swap( hHrtf->pIndex_frequency_max[i][j], hHrtf->pIndex_frequency_max[tmp][j], const uint16_t * );
     801             :                     MOVE( 3 );
     802         608 :                     swap( hHrtf->pOut_to_bin_re[i][j], hHrtf->pOut_to_bin_re[tmp][j], const float * );
     803             :                     MOVE( 3 );
     804         608 :                     swap( hHrtf->pOut_to_bin_im[i][j], hHrtf->pOut_to_bin_im[tmp][j], const float * );
     805             :                     MOVE( 3 );
     806             : #undef WMC_TOOL_SKIP
     807             :                 }
     808             :                 else
     809             :                 {
     810           0 :                     hHrtf->pIndex_frequency_max[i][j] = hHrtfCrend->pIndex_frequency_max[tmp][j];
     811           0 :                     hHrtf->pOut_to_bin_re[i][j] = hHrtfCrend->pOut_to_bin_re[tmp][j];
     812           0 :                     hHrtf->pOut_to_bin_im[i][j] = hHrtfCrend->pOut_to_bin_im[tmp][j];
     813             :                 }
     814             :             }
     815             :         }
     816             :     }
     817             : 
     818         849 :     hHrtf->same_inv_diffuse_weight = 1;
     819        8874 :     for ( i = 0; i < hHrtf->max_num_ir; i++ )
     820             :     {
     821        8025 :         if ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
     822             :         {
     823        5192 :             if ( hHrtf->inv_diffuse_weight[0][i] != hHrtf->inv_diffuse_weight[1][i] )
     824             :             {
     825           0 :                 hHrtf->same_inv_diffuse_weight = 0;
     826           0 :                 break;
     827             :             }
     828             :         }
     829             :     }
     830             : 
     831         849 :     pCrend->hHrtfCrend = hHrtf;
     832             : 
     833         849 :     return IVAS_ERR_OK;
     834             : }
     835             : 
     836             : 
     837             : /*-------------------------------------------------------------------------
     838             :  * ivas_shoebox_data_init()
     839             :  *
     840             :  * Initialize shoebox_data_t handle
     841             :  *------------------------------------------------------------------------*/
     842             : 
     843          56 : static ivas_error ivas_shoebox_data_init(
     844             :     shoebox_data_t *hShoeboxData /* i/o: shoebox_data_t handle     */
     845             : )
     846             : {
     847             :     int16_t i;
     848             : 
     849          56 :     if ( hShoeboxData == NULL )
     850             :     {
     851           0 :         return IVAS_ERR_WRONG_PARAMS;
     852             :     }
     853             : 
     854        8456 :     for ( i = 0; i < 150; i++ )
     855             :     {
     856        8400 :         hShoeboxData->data[i] = 0.0f;
     857             :     }
     858         112 :     for ( i = 0; i < 1; i++ )
     859             :     {
     860          56 :         hShoeboxData->size[i] = 0;
     861             :     }
     862             : 
     863          56 :     return IVAS_ERR_OK;
     864             : }
     865             : 
     866             : 
     867             : /*-------------------------------------------------------------------------
     868             :  * ivas_shoebox_output_init()
     869             :  *
     870             :  * Initialize shoebox_output_t handle
     871             :  *------------------------------------------------------------------------*/
     872             : 
     873          14 : static ivas_error ivas_shoebox_output_init(
     874             :     shoebox_output_t *hShoeboxOutput /* i/o: shoebox_output_t handle     */
     875             : )
     876             : {
     877             :     ivas_error error;
     878             : 
     879          14 :     if ( hShoeboxOutput == NULL )
     880             :     {
     881           0 :         return IVAS_ERR_WRONG_PARAMS;
     882             :     }
     883             : 
     884          14 :     hShoeboxOutput->n_sources = 0;
     885          14 :     hShoeboxOutput->n_ref = 0;
     886             : 
     887          14 :     if ( ( error = ivas_shoebox_data_init( &hShoeboxOutput->times ) ) != IVAS_ERR_OK )
     888             :     {
     889           0 :         return error;
     890             :     }
     891             : 
     892          14 :     if ( ( error = ivas_shoebox_data_init( &hShoeboxOutput->gains ) ) != IVAS_ERR_OK )
     893             :     {
     894           0 :         return error;
     895             :     }
     896             : 
     897          14 :     if ( ( error = ivas_shoebox_data_init( &hShoeboxOutput->az_angle ) ) != IVAS_ERR_OK )
     898             :     {
     899           0 :         return error;
     900             :     }
     901             : 
     902          14 :     if ( ( error = ivas_shoebox_data_init( &hShoeboxOutput->el_angle ) ) != IVAS_ERR_OK )
     903             :     {
     904           0 :         return error;
     905             :     }
     906             : 
     907          14 :     return IVAS_ERR_OK;
     908             : }
     909             : 
     910             : 
     911             : /*-------------------------------------------------------------------------
     912             :  * ivas_shoebox_config_init()
     913             :  *
     914             :  * Initialize shoebox_config_t handle
     915             :  *------------------------------------------------------------------------*/
     916             : 
     917          14 : static ivas_error ivas_shoebox_config_init_params(
     918             :     shoebox_config_t *hShoeboxConfig /* i/o: shoebox_config_t handle     */
     919             : )
     920             : {
     921             :     int16_t i;
     922             : 
     923          14 :     if ( hShoeboxConfig == NULL )
     924             :     {
     925           0 :         return IVAS_ERR_WRONG_PARAMS;
     926             :     }
     927             : 
     928          14 :     hShoeboxConfig->room_L = 0.0f;
     929          14 :     hShoeboxConfig->room_W = 0.0f;
     930          14 :     hShoeboxConfig->room_H = 0.0f;
     931             : 
     932          98 :     for ( i = 0; i < IVAS_ROOM_ABS_COEFF; i++ )
     933             :     {
     934          84 :         hShoeboxConfig->abs_coeff[i] = 0.0f;
     935             :     }
     936          56 :     for ( i = 0; i < 3; i++ )
     937             :     {
     938          42 :         hShoeboxConfig->list_orig[i] = 0.0f;
     939             :     }
     940             : 
     941          14 :     return IVAS_ERR_OK;
     942             : }
     943             : 
     944             : 
     945             : /*-------------------------------------------------------------------------
     946             :  * ivas_shoebox_obj_init()
     947             :  *
     948             :  * Initialize shoebox_obj_t handle
     949             :  *------------------------------------------------------------------------*/
     950             : 
     951          14 : static ivas_error ivas_shoebox_obj_init(
     952             :     shoebox_obj_t *hShoeboxObj /* i/o: shoebox_obj_t handle     */
     953             : )
     954             : {
     955             :     int16_t i;
     956             :     ivas_error error;
     957             : 
     958          14 :     if ( hShoeboxObj == NULL )
     959             :     {
     960           0 :         return IVAS_ERR_WRONG_PARAMS;
     961             :     }
     962             : 
     963          14 :     hShoeboxObj->isCartesian = 0;
     964          14 :     hShoeboxObj->isRelative = 0;
     965          14 :     hShoeboxObj->isZHeight = 0;
     966          14 :     hShoeboxObj->isRadians = 0;
     967          14 :     hShoeboxObj->MAX_SOURCES = 0;
     968          14 :     hShoeboxObj->max_bands = 0;
     969          14 :     hShoeboxObj->REF_ORDER = 0;
     970             : 
     971        1064 :     for ( i = 0; i < 75; i++ )
     972             :     {
     973        1050 :         hShoeboxObj->src_pos[i] = 0.0f;
     974             :     }
     975         364 :     for ( i = 0; i < 25; i++ )
     976             :     {
     977         350 :         hShoeboxObj->src_dist[i] = 0.0f;
     978             :     }
     979          56 :     for ( i = 0; i < 3; i++ )
     980             :     {
     981          42 :         hShoeboxObj->list_pos[i] = 0.0f;
     982             :     }
     983             : 
     984          14 :     hShoeboxObj->nSrc = 0;
     985          14 :     hShoeboxObj->radius = 0.0f;
     986          14 :     hShoeboxObj->min_wall_dist = 0.0f;
     987          14 :     hShoeboxObj->soundspeed = 0.0f;
     988          14 :     hShoeboxObj->air_coeff = 0.0f;
     989             : 
     990          14 :     if ( ( error = ivas_shoebox_config_init_params( &hShoeboxObj->cal ) ) != IVAS_ERR_OK )
     991             :     {
     992           0 :         return error;
     993             :     }
     994             : 
     995          14 :     return IVAS_ERR_OK;
     996             : }
     997             : 
     998             : 
     999             : /*-------------------------------------------------------------------------
    1000             :  * ivas_er_init_handle()
    1001             :  *
    1002             :  * Initialize early reflections handle
    1003             :  *------------------------------------------------------------------------*/
    1004             : 
    1005          14 : static ivas_error ivas_er_init_handle(
    1006             :     er_struct_t *reflections /* i/o: early reflections handle     */
    1007             : )
    1008             : {
    1009             :     int16_t i;
    1010             :     ivas_error error;
    1011             : 
    1012          14 :     if ( reflections == NULL )
    1013             :     {
    1014           0 :         return IVAS_ERR_WRONG_PARAMS;
    1015             :     }
    1016             : 
    1017          14 :     reflections->audio_config = IVAS_AUDIO_CONFIG_INVALID;
    1018          14 :     reflections->use_er = 0;
    1019          14 :     reflections->is_ready = 0;
    1020          14 :     reflections->circ_len = 0;
    1021          14 :     reflections->circ_insert = 0;
    1022          14 :     reflections->n_total_reflections = 0;
    1023          14 :     reflections->is_cartesian = 0;
    1024          14 :     reflections->is_relative = 0;
    1025          14 :     reflections->max_frame_size = 0;
    1026          14 :     reflections->output_Fs = 0.0f;
    1027             : 
    1028        1064 :     for ( i = 0; i < 75; i++ )
    1029             :     {
    1030        1050 :         reflections->source_positions[i] = 0.0f;
    1031             :     }
    1032          56 :     for ( i = 0; i < 3; i++ )
    1033             :     {
    1034          42 :         reflections->user_origin[i] = 0.0f;
    1035          42 :         if ( i == 2 )
    1036             :         {
    1037          14 :             reflections->user_origin[i] = ER_LIST_HEIGHT;
    1038             :         }
    1039             :     }
    1040             : 
    1041          14 :     reflections->circ_buffers = NULL;
    1042          14 :     reflections->closest_ch_idx = NULL;
    1043             : 
    1044          14 :     if ( ( error = ivas_shoebox_output_init( &reflections->shoebox_data ) ) != IVAS_ERR_OK )
    1045             :     {
    1046           0 :         return error;
    1047             :     }
    1048             : 
    1049          14 :     if ( ( error = ivas_shoebox_obj_init( &reflections->shoebox_lib ) ) != IVAS_ERR_OK )
    1050             :     {
    1051           0 :         return error;
    1052             :     }
    1053             : 
    1054          14 :     return IVAS_ERR_OK;
    1055             : }
    1056             : 
    1057             : 
    1058             : /*-------------------------------------------------------------------------
    1059             :  * ivas_rend_initCrendWrapper()
    1060             :  *
    1061             :  * Allocate and initialize crend renderer handle
    1062             :  *------------------------------------------------------------------------*/
    1063             : 
    1064         849 : ivas_error ivas_rend_initCrendWrapper(
    1065             :     CREND_WRAPPER_HANDLE *pCrend,
    1066             :     const int16_t num_poses )
    1067             : {
    1068             :     int16_t i;
    1069             :     CREND_HANDLE hCrend;
    1070             :     int16_t pos_idx;
    1071             : 
    1072         849 :     if ( pCrend == NULL )
    1073             :     {
    1074           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for renderer handle" );
    1075             :     }
    1076             : 
    1077         849 :     if ( ( *pCrend = (CREND_WRAPPER_HANDLE) malloc( sizeof( CREND_WRAPPER ) ) ) == NULL )
    1078             :     {
    1079           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Crend Wrapper\n" );
    1080             :     }
    1081             : 
    1082         849 :     ( *pCrend )->binaural_latency_ns = 0;
    1083         849 :     ( *pCrend )->hHrtfCrend = NULL;
    1084             : 
    1085        7641 :     for ( pos_idx = 0; pos_idx < MAX_HEAD_ROT_POSES; pos_idx++ )
    1086             :     {
    1087        6792 :         ( *pCrend )->hCrend[pos_idx] = NULL;
    1088             :     }
    1089             : 
    1090        1814 :     for ( pos_idx = 0; pos_idx < num_poses; pos_idx++ )
    1091             :     {
    1092         965 :         hCrend = NULL;
    1093         965 :         if ( ( hCrend = (CREND_HANDLE) malloc( sizeof( CREND_DATA ) ) ) == NULL )
    1094             :         {
    1095           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for renderer handle" );
    1096             :         }
    1097             : 
    1098         965 :         hCrend->lfe_delay_line = NULL;
    1099             : 
    1100       16405 :         for ( i = 0; i < MAX_INTERN_CHANNELS; i++ )
    1101             :         {
    1102       15440 :             hCrend->freq_buffer_re[i] = NULL;
    1103       15440 :             hCrend->freq_buffer_im[i] = NULL;
    1104             :         }
    1105             : 
    1106        2895 :         for ( i = 0; i < BINAURAL_CHANNELS; i++ )
    1107             :         {
    1108        1930 :             hCrend->prev_out_buffer[i] = NULL;
    1109             :         }
    1110             : 
    1111         965 :         hCrend->freq_buffer_re_diffuse[0] = NULL;
    1112         965 :         hCrend->freq_buffer_re_diffuse[1] = NULL;
    1113         965 :         hCrend->freq_buffer_im_diffuse[0] = NULL;
    1114         965 :         hCrend->freq_buffer_im_diffuse[1] = NULL;
    1115         965 :         hCrend->hReverb = NULL;
    1116         965 :         hCrend->reflections = NULL;
    1117         965 :         hCrend->delay_line_rw_index = 0;
    1118         965 :         hCrend->diffuse_delay_line_rw_index = 0;
    1119         965 :         hCrend->hTrack = NULL;
    1120         965 :         hCrend->m_fYaw = 0;
    1121         965 :         hCrend->m_fPitch = 0;
    1122         965 :         hCrend->m_fRoll = 0;
    1123             : 
    1124         965 :         ( *pCrend )->hCrend[pos_idx] = hCrend;
    1125             :     }
    1126             : 
    1127         849 :     return IVAS_ERR_OK;
    1128             : }
    1129             : 
    1130             : 
    1131             : /*-------------------------------------------------------------------------
    1132             :  * ivas_rend_openMultiBinCrend()
    1133             :  *
    1134             :  * Allocate and initialize crend renderer handle
    1135             :  *------------------------------------------------------------------------*/
    1136             : 
    1137           0 : ivas_error ivas_rend_openMultiBinCrend(
    1138             :     CREND_WRAPPER_HANDLE *pCrend,
    1139             :     const AUDIO_CONFIG inConfig,
    1140             :     const AUDIO_CONFIG outConfig,
    1141             :     const MULTI_BIN_REND_POSE_DATA *pMultiBinPoseData,
    1142             :     const int32_t output_Fs )
    1143             : {
    1144           0 :     return ivas_rend_openCrend( pCrend, inConfig, outConfig, NULL /*hRendCfg*/, NULL, NULL /* hHrtfStatistics */, output_Fs, 0, pMultiBinPoseData->num_poses );
    1145             : }
    1146             : 
    1147             : 
    1148             : /*-------------------------------------------------------------------------
    1149             :  * ivas_rend_openCrend()
    1150             :  *
    1151             :  * Allocate and initialize crend renderer handle
    1152             :  *------------------------------------------------------------------------*/
    1153             : 
    1154         849 : ivas_error ivas_rend_openCrend(
    1155             :     CREND_WRAPPER_HANDLE *pCrend,
    1156             :     const AUDIO_CONFIG inConfig,
    1157             :     const AUDIO_CONFIG outConfig,
    1158             :     RENDER_CONFIG_DATA *hRendCfg,
    1159             :     HRTFS_CREND_HANDLE hHrtfCrend,
    1160             :     HRTFS_STATISTICS_HANDLE hHrtfStatistics,
    1161             :     const int32_t output_Fs,
    1162             :     const int16_t ext_rend_flag,
    1163             :     const int16_t num_poses )
    1164             : {
    1165             :     int16_t i, subframe_length;
    1166             :     int32_t max_total_ir_len;
    1167             :     HRTFS_CREND_HANDLE hHrtf;
    1168             :     CREND_HANDLE hCrend;
    1169             :     ivas_error error;
    1170             :     int16_t pos_idx;
    1171             : 
    1172         849 :     if ( ( error = ivas_rend_initCrendWrapper( pCrend, num_poses ) ) != IVAS_ERR_OK )
    1173             :     {
    1174           0 :         return error;
    1175             :     }
    1176             : 
    1177         849 :     subframe_length = (int16_t) ( output_Fs / FRAMES_PER_SEC ) / MAX_PARAM_SPATIAL_SUBFRAMES;
    1178             : 
    1179         849 :     if ( ( *pCrend )->hHrtfCrend == NULL )
    1180             :     {
    1181         849 :         if ( ( error = ivas_rend_initCrend( *pCrend, inConfig, outConfig, hHrtfCrend, ext_rend_flag, output_Fs ) ) != IVAS_ERR_OK )
    1182             :         {
    1183           0 :             return error;
    1184             :         }
    1185             :     }
    1186             : 
    1187        1814 :     for ( pos_idx = 0; pos_idx < num_poses; pos_idx++ )
    1188             :     {
    1189         965 :         hCrend = ( *pCrend )->hCrend[pos_idx];
    1190         965 :         hHrtf = ( *pCrend )->hHrtfCrend;
    1191             : 
    1192         965 :         if ( hHrtf != NULL )
    1193             :         {
    1194         965 :             max_total_ir_len = hHrtf->max_num_iterations * subframe_length;
    1195             : 
    1196       10846 :             for ( i = 0; i < hHrtf->max_num_ir; i++ )
    1197             :             {
    1198        9881 :                 if ( ( hCrend->freq_buffer_re[i] = (float *) malloc( sizeof( float ) * max_total_ir_len ) ) == NULL )
    1199             :                 {
    1200           0 :                     return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Crend" );
    1201             :                 }
    1202        9881 :                 set_zero_l( hCrend->freq_buffer_re[i], max_total_ir_len );
    1203             : 
    1204        9881 :                 if ( ( hCrend->freq_buffer_im[i] = (float *) malloc( sizeof( float ) * max_total_ir_len ) ) == NULL )
    1205             :                 {
    1206           0 :                     return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Crend" );
    1207             :                 }
    1208        9881 :                 set_zero_l( hCrend->freq_buffer_im[i], max_total_ir_len );
    1209             :             }
    1210             : 
    1211        2895 :             for ( i = 0; i < BINAURAL_CHANNELS; i++ )
    1212             :             {
    1213        1930 :                 if ( ( hCrend->prev_out_buffer[i] = (float *) malloc( sizeof( float ) * subframe_length ) ) == NULL )
    1214             :                 {
    1215           0 :                     return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Crend" );
    1216             :                 }
    1217        1930 :                 set_zero_l( hCrend->prev_out_buffer[i], subframe_length );
    1218             :             }
    1219             : 
    1220         965 :             max_total_ir_len = hHrtf->num_iterations_diffuse[0] * subframe_length;
    1221             : 
    1222         965 :             if ( max_total_ir_len > 0 )
    1223             :             {
    1224         512 :                 if ( ( hCrend->freq_buffer_re_diffuse[0] = (float *) malloc( sizeof( float ) * max_total_ir_len ) ) == NULL )
    1225             :                 {
    1226           0 :                     return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Crend" );
    1227             :                 }
    1228         512 :                 if ( hHrtf->same_inv_diffuse_weight == 0 )
    1229             :                 {
    1230           0 :                     if ( ( hCrend->freq_buffer_re_diffuse[1] = (float *) malloc( sizeof( float ) * max_total_ir_len ) ) == NULL )
    1231             :                     {
    1232           0 :                         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Crend" );
    1233             :                     }
    1234             :                 }
    1235             :                 else
    1236             :                 {
    1237         512 :                     hCrend->freq_buffer_re_diffuse[1] = NULL;
    1238             :                 }
    1239         512 :                 set_zero_l( hCrend->freq_buffer_re_diffuse[0], max_total_ir_len );
    1240         512 :                 if ( hCrend->freq_buffer_re_diffuse[1] != NULL )
    1241             :                 {
    1242           0 :                     set_zero_l( hCrend->freq_buffer_re_diffuse[1], max_total_ir_len );
    1243             :                 }
    1244             : 
    1245         512 :                 if ( ( hCrend->freq_buffer_im_diffuse[0] = (float *) malloc( sizeof( float ) * max_total_ir_len ) ) == NULL )
    1246             :                 {
    1247           0 :                     return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Crend" );
    1248             :                 }
    1249         512 :                 if ( hHrtf->same_inv_diffuse_weight == 0 )
    1250             :                 {
    1251           0 :                     if ( ( hCrend->freq_buffer_im_diffuse[1] = (float *) malloc( sizeof( float ) * max_total_ir_len ) ) == NULL )
    1252             :                     {
    1253           0 :                         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Crend" );
    1254             :                     }
    1255             :                 }
    1256             :                 else
    1257             :                 {
    1258         512 :                     hCrend->freq_buffer_im_diffuse[1] = NULL;
    1259             :                 }
    1260         512 :                 set_zero_l( hCrend->freq_buffer_im_diffuse[0], max_total_ir_len );
    1261         512 :                 if ( hCrend->freq_buffer_im_diffuse[1] != NULL )
    1262             :                 {
    1263           0 :                     set_zero_l( hCrend->freq_buffer_im_diffuse[1], max_total_ir_len );
    1264             :                 }
    1265             :             }
    1266             :             else
    1267             :             {
    1268         453 :                 hCrend->freq_buffer_re_diffuse[0] = NULL;
    1269         453 :                 hCrend->freq_buffer_im_diffuse[0] = NULL;
    1270         453 :                 hCrend->freq_buffer_re_diffuse[1] = NULL;
    1271         453 :                 hCrend->freq_buffer_im_diffuse[1] = NULL;
    1272             :             }
    1273         965 :             max_total_ir_len = (int32_t) ( hHrtf->latency_s * output_Fs + 0.5f ) + subframe_length;
    1274         965 :             if ( max_total_ir_len > 0 )
    1275             :             {
    1276         965 :                 if ( ( hCrend->lfe_delay_line = (float *) malloc( sizeof( float ) * max_total_ir_len ) ) == NULL )
    1277             :                 {
    1278           0 :                     return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Crend" );
    1279             :                 }
    1280         965 :                 set_zero_l( hCrend->lfe_delay_line, max_total_ir_len );
    1281             :             }
    1282             :             else
    1283             :             {
    1284           0 :                 hCrend->lfe_delay_line = NULL;
    1285             :             }
    1286             : 
    1287         965 :             if ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
    1288             :             {
    1289         153 :                 if ( ( error = ivas_reverb_open( &( hCrend->hReverb ), hHrtfStatistics, hRendCfg, output_Fs ) ) != IVAS_ERR_OK )
    1290             :                 {
    1291           0 :                     return error;
    1292             :                 }
    1293             : 
    1294         153 :                 if ( hRendCfg->roomAcoustics.use_er == 1 )
    1295             :                 {
    1296             : 
    1297             :                     /* Allocate memory for reflections */
    1298          14 :                     hCrend->reflections = (er_struct_t *) malloc( sizeof( er_struct_t ) );
    1299          14 :                     if ( !hCrend->reflections )
    1300             :                     {
    1301           0 :                         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Early Reflections" );
    1302             :                     }
    1303          14 :                     if ( ( error = ivas_er_init_handle( hCrend->reflections ) ) != IVAS_ERR_OK )
    1304             :                     {
    1305           0 :                         return error;
    1306             :                     }
    1307             : 
    1308          14 :                     hCrend->reflections->use_er = hRendCfg->roomAcoustics.use_er;
    1309          14 :                     hCrend->reflections->lowComplexity = hRendCfg->roomAcoustics.lowComplexity;
    1310             : 
    1311             :                     /* Set sample rate and frame size */
    1312          14 :                     hCrend->reflections->output_Fs = (float) output_Fs;
    1313          14 :                     hCrend->reflections->max_frame_size = (int16_t) ( output_Fs / FRAMES_PER_SEC );
    1314             : 
    1315             :                     /* Init Shoebox */
    1316          14 :                     ivas_shoebox_config_init( &hCrend->reflections->shoebox_lib.cal, hRendCfg );
    1317             : 
    1318             :                     /* Init and compute Reflections */
    1319          14 :                     if ( ( error = ivas_er_init( hCrend->reflections, inConfig ) ) != IVAS_ERR_OK )
    1320             :                     {
    1321           0 :                         return error;
    1322             :                     }
    1323             :                 }
    1324             :                 else
    1325             :                 {
    1326         139 :                     hCrend->reflections = NULL;
    1327             :                 }
    1328             :             }
    1329             :             else
    1330             :             {
    1331         812 :                 hCrend->hReverb = NULL;
    1332             :             }
    1333             : 
    1334         965 :             ( *pCrend )->binaural_latency_ns = (int32_t) ( ( *pCrend )->hHrtfCrend->latency_s * 1000000000.f );
    1335             :         }
    1336             : 
    1337         965 :         ( *pCrend )->hCrend[pos_idx] = hCrend;
    1338             :     }
    1339             : 
    1340         849 :     return IVAS_ERR_OK;
    1341             : }
    1342             : 
    1343             : 
    1344             : /*-------------------------------------------------------------------------
    1345             :  * ivas_rend_closeCrend()
    1346             :  *
    1347             :  * Deallocate Crend renderer handle
    1348             :  *------------------------------------------------------------------------*/
    1349             : 
    1350       36430 : void ivas_rend_closeCrend(
    1351             :     CREND_WRAPPER_HANDLE *pCrend )
    1352             : {
    1353             :     int16_t i;
    1354             :     int16_t pos_idx;
    1355             :     CREND_HANDLE hCrend;
    1356             : 
    1357       36430 :     if ( pCrend == NULL || *pCrend == NULL )
    1358             :     {
    1359       35581 :         return;
    1360             :     }
    1361             : 
    1362         849 :     if ( ( *pCrend )->hHrtfCrend != NULL && ( *pCrend )->hHrtfCrend->init_from_rom )
    1363             :     {
    1364         821 :         ivas_hrtf_close( &( *pCrend )->hHrtfCrend );
    1365             :     }
    1366             : 
    1367        7641 :     for ( pos_idx = 0; pos_idx < MAX_HEAD_ROT_POSES; pos_idx++ )
    1368             :     {
    1369        6792 :         hCrend = ( *pCrend )->hCrend[pos_idx];
    1370        6792 :         if ( hCrend != NULL )
    1371             :         {
    1372       16405 :             for ( i = 0; i < MAX_INTERN_CHANNELS; i++ )
    1373             :             {
    1374       15440 :                 if ( hCrend->freq_buffer_re[i] != NULL )
    1375             :                 {
    1376        9881 :                     free( hCrend->freq_buffer_re[i] );
    1377        9881 :                     hCrend->freq_buffer_re[i] = NULL;
    1378             :                 }
    1379       15440 :                 if ( hCrend->freq_buffer_im[i] != NULL )
    1380             :                 {
    1381        9881 :                     free( hCrend->freq_buffer_im[i] );
    1382        9881 :                     hCrend->freq_buffer_im[i] = NULL;
    1383             :                 }
    1384             :             }
    1385             : 
    1386        2895 :             for ( i = 0; i < BINAURAL_CHANNELS; i++ )
    1387             :             {
    1388        1930 :                 if ( hCrend->prev_out_buffer[i] != NULL )
    1389             :                 {
    1390        1930 :                     free( hCrend->prev_out_buffer[i] );
    1391        1930 :                     hCrend->prev_out_buffer[i] = NULL;
    1392             :                 }
    1393             :             }
    1394             : 
    1395         965 :             if ( hCrend->lfe_delay_line != NULL )
    1396             :             {
    1397         965 :                 free( hCrend->lfe_delay_line );
    1398         965 :                 hCrend->lfe_delay_line = NULL;
    1399             :             }
    1400         965 :             if ( hCrend->freq_buffer_re_diffuse[0] != NULL )
    1401             :             {
    1402         512 :                 free( hCrend->freq_buffer_re_diffuse[0] );
    1403         512 :                 hCrend->freq_buffer_re_diffuse[0] = NULL;
    1404             :             }
    1405             : 
    1406         965 :             if ( hCrend->freq_buffer_im_diffuse[0] != NULL )
    1407             :             {
    1408         512 :                 free( hCrend->freq_buffer_im_diffuse[0] );
    1409         512 :                 hCrend->freq_buffer_im_diffuse[0] = NULL;
    1410             :             }
    1411             : 
    1412         965 :             if ( hCrend->freq_buffer_re_diffuse[1] != NULL )
    1413             :             {
    1414           0 :                 free( hCrend->freq_buffer_re_diffuse[1] );
    1415           0 :                 hCrend->freq_buffer_re_diffuse[1] = NULL;
    1416             :             }
    1417             : 
    1418         965 :             if ( hCrend->freq_buffer_im_diffuse[1] != NULL )
    1419             :             {
    1420           0 :                 free( hCrend->freq_buffer_im_diffuse[1] );
    1421           0 :                 hCrend->freq_buffer_im_diffuse[1] = NULL;
    1422             :             }
    1423             : 
    1424         965 :             if ( hCrend->hTrack != NULL )
    1425             :             {
    1426           0 :                 free( hCrend->hTrack );
    1427           0 :                 hCrend->hTrack = NULL;
    1428             :             }
    1429             : 
    1430         965 :             ivas_reverb_close( &hCrend->hReverb );
    1431             : 
    1432         965 :             if ( hCrend->reflections != NULL )
    1433             :             {
    1434          14 :                 if ( hCrend->reflections->closest_ch_idx != NULL )
    1435             :                 {
    1436          14 :                     free( hCrend->reflections->closest_ch_idx );
    1437          14 :                     hCrend->reflections->closest_ch_idx = NULL;
    1438             :                 }
    1439             : 
    1440          14 :                 if ( hCrend->reflections->circ_buffers != NULL )
    1441             :                 {
    1442          14 :                     free( hCrend->reflections->circ_buffers );
    1443          14 :                     hCrend->reflections->circ_buffers = NULL;
    1444             :                 }
    1445             : 
    1446          14 :                 free( hCrend->reflections );
    1447          14 :                 hCrend->reflections = NULL;
    1448             :             }
    1449             : 
    1450         965 :             free( hCrend );
    1451         965 :             hCrend = NULL;
    1452         965 :             ( *pCrend )->hCrend[pos_idx] = hCrend;
    1453             :         }
    1454             :     }
    1455             : 
    1456         849 :     free( *pCrend );
    1457         849 :     *pCrend = NULL;
    1458             : 
    1459         849 :     return;
    1460             : }
    1461             : 
    1462             : 
    1463             : /*-------------------------------------------------------------------------
    1464             :  * ivas_rend_closeCldfbRend()
    1465             :  *
    1466             :  * Close CLDFB based fastconv binaural renderer memories
    1467             :  *------------------------------------------------------------------------*/
    1468             : 
    1469           0 : void ivas_rend_closeCldfbRend(
    1470             :     CLDFB_REND_WRAPPER *pCldfbRend )
    1471             : {
    1472           0 :     if ( pCldfbRend->hCldfbRend->hInputSetup != NULL )
    1473             :     {
    1474           0 :         free( pCldfbRend->hCldfbRend->hInputSetup );
    1475           0 :         pCldfbRend->hCldfbRend->hInputSetup = NULL;
    1476             :     }
    1477             : 
    1478           0 :     ivas_binRenderer_close( &pCldfbRend->hCldfbRend );
    1479             : 
    1480           0 :     ivas_HRTF_fastconv_binary_close( &pCldfbRend->hHrtfFastConv );
    1481             : 
    1482           0 :     return;
    1483             : }
    1484             : 
    1485             : 
    1486             : /*-----------------------------------------------------------------------------------------*
    1487             :  * Function ivas_rend_crendConvolver()
    1488             :  *
    1489             :  * Convolver block
    1490             :  *-----------------------------------------------------------------------------------------*/
    1491             : 
    1492     1476834 : static ivas_error ivas_rend_crendConvolver(
    1493             :     const CREND_WRAPPER *pCrend,
    1494             :     AUDIO_CONFIG inConfig,
    1495             :     AUDIO_CONFIG outConfig,
    1496             :     float *pcm_in[],
    1497             :     float *pcm_out[],
    1498             :     const int32_t output_Fs,
    1499             :     const int16_t i_ts,
    1500             :     const int16_t pos_idx )
    1501             : {
    1502             :     int16_t i, j, k, m;
    1503             :     int16_t subframe_length, idx_in;
    1504             :     int16_t lfe_idx_in;
    1505             :     int32_t offset, offset_in, offset_diffuse;
    1506             :     int16_t nchan_in, nchan_out;
    1507             :     const float *pIn;
    1508             :     const float *pFreq_filt_re, *pFreq_filt_im;
    1509     1476834 :     float *pFreq_buf_re = NULL, *pFreq_buf_im = NULL;
    1510     1476834 :     float *pFreq_buf2_re = NULL, *pFreq_buf2_im = NULL;
    1511             :     float pOut[2 /*Re,Im*/ * L_FRAME48k / MAX_PARAM_SPATIAL_SUBFRAMES];
    1512             :     float tmp_out_re[L_FRAME48k / MAX_PARAM_SPATIAL_SUBFRAMES], tmp_out_im[L_FRAME48k / MAX_PARAM_SPATIAL_SUBFRAMES];
    1513             :     CREND_HANDLE hCrend;
    1514             :     ivas_error error;
    1515             : 
    1516     1476834 :     hCrend = pCrend->hCrend[pos_idx];
    1517             : 
    1518     1476834 :     if ( ( error = getAudioConfigNumChannels( inConfig, &nchan_in ) ) != IVAS_ERR_OK )
    1519             :     {
    1520           0 :         return error;
    1521             :     }
    1522             : 
    1523     1476834 :     if ( ( error = getAudioConfigNumChannels( outConfig, &nchan_out ) ) != IVAS_ERR_OK )
    1524             :     {
    1525           0 :         return error;
    1526             :     }
    1527             : 
    1528     1476834 :     subframe_length = (int16_t) ( output_Fs / FRAMES_PER_SEC ) / MAX_PARAM_SPATIAL_SUBFRAMES;
    1529             : 
    1530     1476834 :     lfe_idx_in = -1;
    1531     1476834 :     if ( getAudioConfigType( inConfig ) == IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED )
    1532             :     {
    1533     1011285 :         if ( inConfig != IVAS_AUDIO_CONFIG_LS_CUSTOM )
    1534             :         {
    1535     1011285 :             lfe_idx_in = LFE_CHANNEL;
    1536             :         }
    1537             :         else
    1538             :         {
    1539           0 :             assert( 0 && "Custom LS not supported in CRend" );
    1540             :         }
    1541             :     }
    1542             : 
    1543     1476834 :     offset = hCrend->delay_line_rw_index * subframe_length;                 /* subframe_length * ( pCrend->hHrtfCrend->max_num_iterations - 1 ); */
    1544     1476834 :     offset_diffuse = hCrend->diffuse_delay_line_rw_index * subframe_length; /* subframe_length *( pCrend->hHrtfCrend->num_iterations_diffuse[0] - 1 ); */
    1545             : 
    1546     1476834 :     if ( pCrend->hHrtfCrend->num_iterations_diffuse[0] > 0 )
    1547             :     {
    1548      905577 :         set_zero( &hCrend->freq_buffer_re_diffuse[0][offset_diffuse], subframe_length );
    1549      905577 :         set_zero( &hCrend->freq_buffer_im_diffuse[0][offset_diffuse], subframe_length );
    1550      905577 :         if ( pCrend->hHrtfCrend->same_inv_diffuse_weight == 0 )
    1551             :         {
    1552           0 :             set_zero( &hCrend->freq_buffer_re_diffuse[1][offset_diffuse], subframe_length );
    1553           0 :             set_zero( &hCrend->freq_buffer_im_diffuse[1][offset_diffuse], subframe_length );
    1554             :         }
    1555             :     }
    1556             : 
    1557     1476834 :     if ( pCrend->hHrtfCrend->num_iterations_diffuse[0] > 0 )
    1558             :     {
    1559      905577 :         if ( pCrend->hHrtfCrend->same_inv_diffuse_weight )
    1560             :         {
    1561      905577 :             pFreq_buf_re = &hCrend->freq_buffer_re_diffuse[0][offset_diffuse];
    1562      905577 :             pFreq_buf_im = &hCrend->freq_buffer_im_diffuse[0][offset_diffuse];
    1563             :         }
    1564             :         else
    1565             :         {
    1566           0 :             pFreq_buf_re = &hCrend->freq_buffer_re_diffuse[0][offset_diffuse];
    1567           0 :             pFreq_buf_im = &hCrend->freq_buffer_im_diffuse[0][offset_diffuse];
    1568           0 :             pFreq_buf2_re = &hCrend->freq_buffer_re_diffuse[1][offset_diffuse];
    1569           0 :             pFreq_buf2_im = &hCrend->freq_buffer_im_diffuse[1][offset_diffuse];
    1570             :         }
    1571             :     }
    1572             : 
    1573     1476834 :     i = 0;
    1574    18252722 :     for ( idx_in = 0; idx_in < nchan_in; idx_in++ )
    1575             :     {
    1576    16775888 :         pIn = &pcm_in[idx_in][i_ts * subframe_length];
    1577    16775888 :         if ( idx_in != lfe_idx_in )
    1578             :         {
    1579    15764603 :             if ( pCrend->hHrtfCrend->num_iterations_diffuse[0] > 0 )
    1580             :             {
    1581     9768079 :                 if ( pCrend->hHrtfCrend->same_inv_diffuse_weight )
    1582             :                 {
    1583     9768079 :                     pFreq_filt_re = &hCrend->freq_buffer_re[i][offset];
    1584     9768079 :                     pFreq_filt_im = &hCrend->freq_buffer_im[i][offset];
    1585             : 
    1586   922178979 :                     for ( k = 0; k < pCrend->hHrtfCrend->index_frequency_max_diffuse; k++ )
    1587             :                     {
    1588   912410900 :                         pFreq_buf_re[k] += pFreq_filt_re[k] * pCrend->hHrtfCrend->inv_diffuse_weight[0][i];
    1589   912410900 :                         pFreq_buf_im[k] += pFreq_filt_im[k] * pCrend->hHrtfCrend->inv_diffuse_weight[0][i];
    1590             :                     }
    1591             :                 }
    1592             :                 else
    1593             :                 {
    1594           0 :                     pFreq_filt_re = &hCrend->freq_buffer_re[i][offset];
    1595           0 :                     pFreq_filt_im = &hCrend->freq_buffer_im[i][offset];
    1596             : 
    1597           0 :                     for ( k = 0; k < pCrend->hHrtfCrend->index_frequency_max_diffuse; k++ )
    1598             :                     {
    1599           0 :                         pFreq_buf_re[k] += pFreq_filt_re[k] * pCrend->hHrtfCrend->inv_diffuse_weight[0][i];
    1600           0 :                         pFreq_buf_im[k] += pFreq_filt_im[k] * pCrend->hHrtfCrend->inv_diffuse_weight[0][i];
    1601           0 :                         pFreq_buf2_re[k] += pFreq_filt_re[k] * pCrend->hHrtfCrend->inv_diffuse_weight[1][i];
    1602           0 :                         pFreq_buf2_im[k] += pFreq_filt_im[k] * pCrend->hHrtfCrend->inv_diffuse_weight[1][i];
    1603             :                     }
    1604             :                 }
    1605             :             }
    1606             : 
    1607    15764603 :             ivas_mdft( pIn, &hCrend->freq_buffer_re[i][offset], &hCrend->freq_buffer_im[i][offset], subframe_length, subframe_length );
    1608    15764603 :             i++;
    1609             :         }
    1610             :     }
    1611             : 
    1612     4430502 :     for ( j = 0; j < nchan_out; j++ )
    1613             :     {
    1614     2953668 :         set_zero( tmp_out_re, subframe_length );
    1615     2953668 :         set_zero( tmp_out_im, subframe_length );
    1616             : 
    1617     2953668 :         i = 0;
    1618    36505444 :         for ( idx_in = 0; idx_in < nchan_in; idx_in++ )
    1619             :         {
    1620    33551776 :             if ( idx_in != lfe_idx_in )
    1621             :             {
    1622    31529206 :                 offset = 0;
    1623   477357580 :                 for ( m = 0; m < pCrend->hHrtfCrend->num_iterations[i][j]; m++ )
    1624             :                 {
    1625   445828374 :                     offset_in = ( hCrend->delay_line_rw_index + pCrend->hHrtfCrend->max_num_iterations - pCrend->hHrtfCrend->num_iterations[i][j] + m + 1 );
    1626   445828374 :                     offset_in = offset_in % ( pCrend->hHrtfCrend->max_num_iterations );
    1627   445828374 :                     offset_in = offset_in * subframe_length;
    1628   445828374 :                     pFreq_buf_re = &hCrend->freq_buffer_re[i][offset_in];
    1629   445828374 :                     pFreq_buf_im = &hCrend->freq_buffer_im[i][offset_in];
    1630   445828374 :                     pFreq_filt_re = &pCrend->hHrtfCrend->pOut_to_bin_re[i][j][offset];
    1631   445828374 :                     pFreq_filt_im = &pCrend->hHrtfCrend->pOut_to_bin_im[i][j][offset];
    1632             : 
    1633 54080643710 :                     for ( k = 0; k < pCrend->hHrtfCrend->pIndex_frequency_max[i][j][m]; k++ )
    1634             :                     {
    1635 53634815336 :                         tmp_out_re[k] += pFreq_buf_re[k] * pFreq_filt_re[k] - pFreq_buf_im[k] * pFreq_filt_im[k];
    1636 53634815336 :                         tmp_out_im[k] += pFreq_buf_re[k] * pFreq_filt_im[k] + pFreq_buf_im[k] * pFreq_filt_re[k];
    1637             :                     }
    1638   445828374 :                     offset = offset + k;
    1639             :                 }
    1640    31529206 :                 i++;
    1641             :             }
    1642             :         }
    1643             : 
    1644     2953668 :         offset = 0;
    1645    75399828 :         for ( m = 0; m < pCrend->hHrtfCrend->num_iterations_diffuse[j]; m++ )
    1646             :         {
    1647    72446160 :             offset_diffuse = ( hCrend->diffuse_delay_line_rw_index + m + 1 );
    1648    72446160 :             offset_diffuse = offset_diffuse % pCrend->hHrtfCrend->num_iterations_diffuse[0];
    1649    72446160 :             offset_diffuse = offset_diffuse * subframe_length;
    1650    72446160 :             if ( pCrend->hHrtfCrend->same_inv_diffuse_weight )
    1651             :             {
    1652    72446160 :                 pFreq_buf_re = &hCrend->freq_buffer_re_diffuse[0][offset_diffuse];
    1653    72446160 :                 pFreq_buf_im = &hCrend->freq_buffer_im_diffuse[0][offset_diffuse];
    1654             :             }
    1655             :             else
    1656             :             {
    1657           0 :                 pFreq_buf_re = &hCrend->freq_buffer_re_diffuse[j][offset_diffuse];
    1658           0 :                 pFreq_buf_im = &hCrend->freq_buffer_im_diffuse[j][offset_diffuse];
    1659             :             }
    1660    72446160 :             pFreq_filt_re = &pCrend->hHrtfCrend->pOut_to_bin_diffuse_re[j][offset];
    1661    72446160 :             pFreq_filt_im = &pCrend->hHrtfCrend->pOut_to_bin_diffuse_im[j][offset];
    1662             : 
    1663  5138783722 :             for ( k = 0; k < pCrend->hHrtfCrend->pIndex_frequency_max_diffuse[j][m]; k++ )
    1664             :             {
    1665  5066337562 :                 tmp_out_re[k] += pFreq_buf_re[k] * pFreq_filt_re[k] - pFreq_buf_im[k] * pFreq_filt_im[k];
    1666  5066337562 :                 tmp_out_im[k] += pFreq_buf_re[k] * pFreq_filt_im[k] + pFreq_buf_im[k] * pFreq_filt_re[k];
    1667             :             }
    1668    72446160 :             offset = offset + k;
    1669             :         }
    1670             : 
    1671     2953668 :         ivas_imdft( tmp_out_re, tmp_out_im, pOut, subframe_length );
    1672             : 
    1673     2953668 :         pFreq_buf_re = &pcm_out[j][i_ts * subframe_length];
    1674   563977508 :         for ( k = 0; k < subframe_length; k++ )
    1675             :         {
    1676   561023840 :             pFreq_buf_re[k] = pOut[k] + hCrend->prev_out_buffer[j][k];
    1677   561023840 :             hCrend->prev_out_buffer[j][k] = pOut[k + subframe_length];
    1678             :         }
    1679             :     }
    1680             : 
    1681     1476834 :     hCrend->delay_line_rw_index++;
    1682     1476834 :     hCrend->delay_line_rw_index = hCrend->delay_line_rw_index % ( pCrend->hHrtfCrend->max_num_iterations );
    1683     1476834 :     if ( pCrend->hHrtfCrend->num_iterations_diffuse[0] > 0 )
    1684             :     {
    1685      905577 :         hCrend->diffuse_delay_line_rw_index++;
    1686      905577 :         hCrend->diffuse_delay_line_rw_index = hCrend->diffuse_delay_line_rw_index % ( pCrend->hHrtfCrend->num_iterations_diffuse[0] );
    1687             :     }
    1688             : 
    1689     1476834 :     return IVAS_ERR_OK;
    1690             : }
    1691             : 
    1692             : 
    1693             : /*-----------------------------------------------------------------------------------------*
    1694             :  * Function ivas_rend_crendProcessSubframe()
    1695             :  *
    1696             :  *
    1697             :  *-----------------------------------------------------------------------------------------*/
    1698             : 
    1699      706337 : ivas_error ivas_rend_crendProcessSubframe(
    1700             :     const CREND_WRAPPER *pCrend,                                /* i/o: Crend wrapper handle                    */
    1701             :     const AUDIO_CONFIG inConfig,                                /* i  : input audio configuration               */
    1702             :     const AUDIO_CONFIG outConfig,                               /* i  : output audio configuration              */
    1703             :     const DECODER_CONFIG_HANDLE hDecoderConfig,                 /* i  : decoder config. structure               */
    1704             :     const COMBINED_ORIENTATION_HANDLE hCombinedOrientationData, /* i  : combined orientation handle             */
    1705             :     const IVAS_OUTPUT_SETUP_HANDLE hIntSetup,                   /* i  : internal setup handle                   */
    1706             :     const EFAP_HANDLE hEFAPdata,                                /* i  : EFAP handle                             */
    1707             :     DECODER_TC_BUFFER_HANDLE hTcBuffer,                         /* i/o: TC handle                               */
    1708             :     float *input_f[],                                           /* i  : transport channels                      */
    1709             :     float *output[],                                            /* i/o: input/output audio channels             */
    1710             :     const int16_t n_samples_to_render,                          /* i  : output frame length per channel         */
    1711             :     const int32_t output_Fs,                                    /* i  : output sampling rate                    */
    1712             :     const int16_t pos_idx                                       /* i  : pose index                              */
    1713             : )
    1714             : {
    1715             :     int16_t subframe_idx, subframe_len;
    1716             :     int16_t nchan_out, nchan_in, ch, first_sf, last_sf, slot_size, slots_to_render;
    1717             :     float *tc_local[MAX_OUTPUT_CHANNELS];
    1718             :     float *p_output[BINAURAL_CHANNELS];
    1719             :     float pcm_tmp[BINAURAL_CHANNELS][L_FRAME48k / MAX_PARAM_SPATIAL_SUBFRAMES];
    1720             :     float *p_pcm_tmp[BINAURAL_CHANNELS];
    1721             :     IVAS_REND_AudioConfigType inConfigType;
    1722             :     ivas_error error;
    1723             :     int8_t combinedOrientationEnabled;
    1724             :     CREND_HANDLE hCrend;
    1725             : 
    1726      706337 :     hCrend = pCrend->hCrend[pos_idx];
    1727             : 
    1728      706337 :     combinedOrientationEnabled = 0;
    1729      706337 :     if ( hCombinedOrientationData != NULL )
    1730             :     {
    1731       27697 :         if ( hCombinedOrientationData->enableCombinedOrientation[0] != 0 )
    1732             :         {
    1733       27697 :             combinedOrientationEnabled = 1;
    1734             :         }
    1735             :     }
    1736             : 
    1737      706337 :     push_wmops( "ivas_rend_crendProcessSubframe" );
    1738      706337 :     inConfigType = getAudioConfigType( inConfig );
    1739             : 
    1740      706337 :     if ( ( error = getAudioConfigNumChannels( outConfig, &nchan_out ) ) != IVAS_ERR_OK )
    1741             :     {
    1742           0 :         return error;
    1743             :     }
    1744             : 
    1745      706337 :     if ( ( error = getAudioConfigNumChannels( inConfig, &nchan_in ) ) != IVAS_ERR_OK )
    1746             :     {
    1747           0 :         return error;
    1748             :     }
    1749             : 
    1750     8812297 :     for ( ch = 0; ch < nchan_in; ch++ )
    1751             :     {
    1752     8105960 :         tc_local[ch] = input_f[ch];
    1753             :     }
    1754             : 
    1755     2119011 :     for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    1756             :     {
    1757     1412674 :         p_pcm_tmp[ch] = pcm_tmp[ch];
    1758     1412674 :         p_output[ch] = output[ch];
    1759             :     }
    1760             : 
    1761      706337 :     if ( hTcBuffer != NULL )
    1762             :     {
    1763       48928 :         slot_size = hTcBuffer->n_samples_granularity;
    1764             : 
    1765             :         /* loop for synthesis, assume we always have to render in multiples of 5ms subframes with spills */
    1766       48928 :         slots_to_render = min( hTcBuffer->num_slots - hTcBuffer->slots_rendered, n_samples_to_render / slot_size );
    1767       48928 :         first_sf = hTcBuffer->subframes_rendered;
    1768       48928 :         last_sf = first_sf;
    1769             : 
    1770      240150 :         while ( slots_to_render > 0 )
    1771             :         {
    1772      191222 :             slots_to_render -= hTcBuffer->subframe_nbslots[last_sf];
    1773      191222 :             last_sf++;
    1774             :         }
    1775       48928 :         subframe_len = -1; /* will be set later */
    1776             :     }
    1777             :     else
    1778             :     {
    1779      657409 :         subframe_len = (int16_t) ( output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) );
    1780      657409 :         first_sf = 0;
    1781      657409 :         last_sf = n_samples_to_render / subframe_len;
    1782             :     }
    1783             : 
    1784     2183171 :     for ( subframe_idx = first_sf; subframe_idx < last_sf; subframe_idx++ )
    1785             :     {
    1786     1476834 :         if ( hTcBuffer != NULL )
    1787             :         {
    1788      191222 :             subframe_len = hTcBuffer->subframe_nbslots[subframe_idx] * hTcBuffer->n_samples_granularity;
    1789             :         }
    1790             : 
    1791             :         /* Early Reflections */
    1792     1476834 :         if ( hCrend->reflections != NULL )
    1793             :         {
    1794       45860 :             if ( hCrend->reflections->use_er == 1 && hCrend->reflections->is_ready == 1 )
    1795             :             {
    1796       25820 :                 if ( ( error = ivas_er_process( hCrend->reflections, subframe_len, 0, tc_local, inConfig ) ) != IVAS_ERR_OK )
    1797             :                 {
    1798           0 :                     return error;
    1799             :                 }
    1800             :             }
    1801             :         }
    1802             : 
    1803     1476834 :         if ( hDecoderConfig && combinedOrientationEnabled )
    1804             :         {
    1805             :             /* Rotation in SHD for:
    1806             :                 MC with elevation (5_1_2 / 5_1_4 / 7_1_4) -> BINAURAL
    1807             :                 SBA SPAR -> BINAURAL or BINAURAL_ROOM
    1808             :             */
    1809      108961 :             if ( inConfig == IVAS_AUDIO_CONFIG_FOA || inConfig == IVAS_AUDIO_CONFIG_HOA2 || inConfig == IVAS_AUDIO_CONFIG_HOA3 )
    1810             :             {
    1811       92961 :                 rotateFrame_shd( hCombinedOrientationData, tc_local, subframe_len, *hIntSetup, 0 );
    1812             :             }
    1813             :             /* Rotation in SD for MC -> BINAURAL_ROOM */
    1814       16000 :             else if ( ( hIntSetup != NULL ) && hIntSetup->is_loudspeaker_setup )
    1815             :             {
    1816       16000 :                 rotateFrame_sd( hCombinedOrientationData, tc_local, subframe_len, *hIntSetup, hEFAPdata, 0 );
    1817             :             }
    1818             :         }
    1819             : 
    1820     1476834 :         if ( ( inConfigType == IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED ) || ( inConfigType == IVAS_REND_AUDIO_CONFIG_TYPE_AMBISONICS ) )
    1821             :         {
    1822     1476834 :             if ( ( error = ivas_rend_crendConvolver( pCrend, inConfig, outConfig, tc_local, p_pcm_tmp, output_Fs, 0, pos_idx ) ) != IVAS_ERR_OK )
    1823             :             {
    1824           0 :                 return error;
    1825             :             }
    1826             : 
    1827     1476834 :             if ( pCrend->hCrend[0]->hReverb != NULL )
    1828             :             {
    1829      299493 :                 if ( ( error = ivas_reverb_process( pCrend->hCrend[pos_idx]->hReverb, inConfig, 1, tc_local, p_pcm_tmp, 0 ) ) != IVAS_ERR_OK )
    1830             :                 {
    1831           0 :                     return error;
    1832             :                 }
    1833             :             }
    1834             : 
    1835    18252722 :             for ( ch = 0; ch < nchan_in; ch++ )
    1836             :             {
    1837    16775888 :                 tc_local[ch] += subframe_len;
    1838             :             }
    1839             : 
    1840     1476834 :             if ( hTcBuffer != NULL )
    1841             :             {
    1842      191222 :                 hTcBuffer->slots_rendered += hTcBuffer->subframe_nbslots[subframe_idx];
    1843             :             }
    1844             :         }
    1845             :         else
    1846             :         {
    1847           0 :             return IVAS_ERR_INVALID_INPUT_FORMAT;
    1848             :         }
    1849             : 
    1850     4430502 :         for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    1851             :         {
    1852             :             /* move to output */
    1853     2953668 :             mvr2r( pcm_tmp[ch], p_output[ch], subframe_len );
    1854             : 
    1855     2953668 :             p_output[ch] += subframe_len;
    1856             :         }
    1857             : 
    1858             :         /* update combined orientation access index */
    1859     1476834 :         ivas_combined_orientation_update_index( hCombinedOrientationData, subframe_len );
    1860             :     }
    1861             : 
    1862      706337 :     if ( hTcBuffer != NULL )
    1863             :     {
    1864       48928 :         hTcBuffer->subframes_rendered = last_sf;
    1865             :     }
    1866             : 
    1867      706337 :     pop_wmops();
    1868             : 
    1869      706337 :     return IVAS_ERR_OK;
    1870             : }
    1871             : 
    1872             : 
    1873             : /*-----------------------------------------------------------------------------------------*
    1874             :  * Function ivas_rend_crend_ProcessSubframesSplitBin()
    1875             :  *
    1876             :  * Process call for IVAS Crend renderer
    1877             :  *-----------------------------------------------------------------------------------------*/
    1878             : 
    1879        5562 : ivas_error ivas_rend_crendProcessSubframesSplitBin(
    1880             :     const CREND_WRAPPER *pCrend,                                /* i/o: Crend wrapper handle                    */
    1881             :     const AUDIO_CONFIG inConfig,                                /* i  : input audio configuration               */
    1882             :     const AUDIO_CONFIG outConfig,                               /* i  : output audio configuration              */
    1883             :     const MULTI_BIN_REND_POSE_DATA *pMultiBinPoseData,          /* i/o: pose correction data handle             */
    1884             :     const DECODER_CONFIG_HANDLE hDecoderConfig,                 /* i  : decoder config. structure               */
    1885             :     const COMBINED_ORIENTATION_HANDLE hCombinedOrientationData, /* i  : combined orientation handle             */
    1886             :     const IVAS_OUTPUT_SETUP_HANDLE hIntSetup,                   /* i  : internal setup handle                   */
    1887             :     const EFAP_HANDLE hEFAPdata,                                /* i  : EFAP handle                             */
    1888             :     DECODER_TC_BUFFER_HANDLE hTcBuffer,                         /* i/o: TC handle                               */
    1889             :     float *input_f[],                                           /* i  : transport channels                      */
    1890             :     float *output[],                                            /* i/o: input/output audio channels             */
    1891             :     const int16_t n_samples_to_render,                          /* i  : output frame length per channel         */
    1892             :     const int32_t output_Fs                                     /* i  : output sampling rate                    */
    1893             : )
    1894             : {
    1895             :     int16_t i, j;
    1896             :     int16_t sf;
    1897             :     int16_t pos_idx;
    1898             :     ivas_error error;
    1899             :     float gain_lfe;
    1900             :     float tmpLfeBuffer[L_FRAME48k];
    1901             :     int16_t original_subframes_rendered, original_slots_rendered;
    1902             :     float tmpInputBuffer[MAX_OUTPUT_CHANNELS][L_FRAME48k];
    1903             :     float *p_tmpInputBuffer[MAX_OUTPUT_CHANNELS];
    1904             :     float tmpSplitBinBuffer[MAX_OUTPUT_CHANNELS][L_FRAME48k];
    1905             :     COMBINED_ORIENTATION_DATA combinedOrientationDataLocal;
    1906             :     COMBINED_ORIENTATION_HANDLE pCombinedOrientationDataLocal;
    1907             : 
    1908             :     /* save current head positions */
    1909        5562 :     pCombinedOrientationDataLocal = hCombinedOrientationData;
    1910        5562 :     combinedOrientationDataLocal = *pCombinedOrientationDataLocal;
    1911        5562 :     original_subframes_rendered = hTcBuffer->subframes_rendered;
    1912        5562 :     original_slots_rendered = hTcBuffer->slots_rendered;
    1913             : 
    1914             :     /* copy input */
    1915       94554 :     for ( i = 0; i < hIntSetup->nchan_out_woLFE; ++i )
    1916             :     {
    1917       88992 :         mvr2r( input_f[i], tmpInputBuffer[i], n_samples_to_render );
    1918             :     }
    1919             : 
    1920       94554 :     for ( i = 0; i < MAX_OUTPUT_CHANNELS; ++i )
    1921             :     {
    1922       88992 :         p_tmpInputBuffer[i] = tmpInputBuffer[i];
    1923             :     }
    1924             : 
    1925             :     /* save current head positions */
    1926        5562 :     pCombinedOrientationDataLocal = hCombinedOrientationData;
    1927        5562 :     combinedOrientationDataLocal = *pCombinedOrientationDataLocal;
    1928        5562 :     if ( pMultiBinPoseData->poseCorrectionMode == ISAR_SPLIT_REND_POSE_CORRECTION_MODE_CLDFB )
    1929             :     {
    1930       16068 :         for ( sf = 1; sf < hCombinedOrientationData->num_subframes; ++sf )
    1931             :         {
    1932       12051 :             combinedOrientationDataLocal.Quaternions[sf] = combinedOrientationDataLocal.Quaternions[0];
    1933       48204 :             for ( i = 0; i < 3; i++ )
    1934             :             {
    1935      144612 :                 for ( j = 0; j < 3; j++ )
    1936             :                 {
    1937      108459 :                     combinedOrientationDataLocal.Rmat[sf][i][j] = combinedOrientationDataLocal.Rmat[0][i][j];
    1938             :                 }
    1939             :             }
    1940             :         }
    1941             :     }
    1942             : 
    1943             :     /* copy LFE to tmpLfeBuffer and apply gain only once */
    1944        5562 :     if ( hIntSetup->num_lfe > 0 && hIntSetup->index_lfe[0] != -1 )
    1945             :     {
    1946           0 :         mvr2r( output[hIntSetup->index_lfe[0]], tmpLfeBuffer, n_samples_to_render );
    1947           0 :         gain_lfe = ( ( pCrend != NULL ) && ( pCrend->hHrtfCrend != NULL ) ) ? pCrend->hHrtfCrend->gain_lfe : GAIN_LFE;
    1948           0 :         v_multc( tmpLfeBuffer, gain_lfe, tmpLfeBuffer, n_samples_to_render );
    1949             :     }
    1950             :     else
    1951             :     {
    1952        5562 :         set_zero( tmpLfeBuffer, n_samples_to_render );
    1953             :     }
    1954             : 
    1955       29046 :     for ( pos_idx = 0; pos_idx < pMultiBinPoseData->num_poses; ++pos_idx )
    1956             :     {
    1957             :         /* Update head positions */
    1958             :         IVAS_QUATERNION Quaternions_orig[MAX_PARAM_SPATIAL_SUBFRAMES], Quaternions_abs;
    1959      117420 :         for ( i = 0; i < hCombinedOrientationData->num_subframes; i++ )
    1960             :         {
    1961       93936 :             Quaternions_orig[i] = combinedOrientationDataLocal.Quaternions[i];
    1962       93936 :             Quaternions_abs.w = -3.0f;
    1963       93936 :             Quat2EulerDegree( combinedOrientationDataLocal.Quaternions[i], &Quaternions_abs.z, &Quaternions_abs.y, &Quaternions_abs.x ); /*order in Quat2Euler seems to be reversed ?*/
    1964             : 
    1965       93936 :             Quaternions_abs.x += pMultiBinPoseData->relative_head_poses[pos_idx][0];
    1966       93936 :             Quaternions_abs.y += pMultiBinPoseData->relative_head_poses[pos_idx][1];
    1967       93936 :             Quaternions_abs.z += pMultiBinPoseData->relative_head_poses[pos_idx][2];
    1968       93936 :             combinedOrientationDataLocal.Quaternions[i] = Quaternions_abs;
    1969       93936 :             QuatToRotMat( combinedOrientationDataLocal.Quaternions[i], combinedOrientationDataLocal.Rmat[i] );
    1970             :         }
    1971             : 
    1972       23484 :         pCombinedOrientationDataLocal = &combinedOrientationDataLocal;
    1973             : 
    1974       23484 :         hTcBuffer->subframes_rendered = original_subframes_rendered;
    1975       23484 :         hTcBuffer->slots_rendered = original_slots_rendered;
    1976             : 
    1977             :         /* update combined orientation access index */
    1978       23484 :         ivas_combined_orientation_set_to_start_index( pCombinedOrientationDataLocal );
    1979             : 
    1980       93936 :         for ( i = 0; i < 3; i++ )
    1981             :         {
    1982       70452 :             mvr2r( hCombinedOrientationData->Rmat_prev[pos_idx][i], pCombinedOrientationDataLocal->Rmat_prev[0][i], 3 );
    1983             :         }
    1984             : 
    1985       23484 :         if ( ( error = ivas_rend_crendProcessSubframe( pCrend, inConfig, outConfig, hDecoderConfig, pCombinedOrientationDataLocal,
    1986             :                                                        hIntSetup, hEFAPdata, hTcBuffer, p_tmpInputBuffer, p_tmpInputBuffer, n_samples_to_render, output_Fs, pos_idx ) ) != IVAS_ERR_OK )
    1987             :         {
    1988           0 :             return error;
    1989             :         }
    1990             : 
    1991       93936 :         for ( i = 0; i < 3; i++ )
    1992             :         {
    1993       70452 :             mvr2r( pCombinedOrientationDataLocal->Rmat_prev[0][i], hCombinedOrientationData->Rmat_prev[pos_idx][i], 3 );
    1994             :         }
    1995             : 
    1996       70452 :         for ( i = 0; i < BINAURAL_CHANNELS; ++i )
    1997             :         {
    1998             :             /* accumulate LFE to output */
    1999       46968 :             v_add( tmpInputBuffer[i], tmpLfeBuffer, tmpInputBuffer[i], n_samples_to_render );
    2000             : 
    2001             :             /* move to split bin output buffer */
    2002       46968 :             mvr2r( tmpInputBuffer[i], tmpSplitBinBuffer[pos_idx * BINAURAL_CHANNELS + i], n_samples_to_render );
    2003             :         }
    2004             : 
    2005             :         /* overwrite rendered channels with input again for next iteration */
    2006      399228 :         for ( i = 0; i < hIntSetup->nchan_out_woLFE; ++i )
    2007             :         {
    2008      375744 :             mvr2r( input_f[i], tmpInputBuffer[i], n_samples_to_render );
    2009             :         }
    2010             : 
    2011             :         /* restore original headrotation data */
    2012      117420 :         for ( i = 0; i < hCombinedOrientationData->num_subframes; i++ )
    2013             :         {
    2014       93936 :             combinedOrientationDataLocal.Quaternions[i] = Quaternions_orig[i];
    2015             :         }
    2016             :     }
    2017             : 
    2018             :     /* copy split binaural rendered signals to final output */
    2019       52530 :     for ( i = 0; i < BINAURAL_CHANNELS * pMultiBinPoseData->num_poses; ++i )
    2020             :     {
    2021       46968 :         mvr2r( tmpSplitBinBuffer[i], output[i], n_samples_to_render );
    2022             :     }
    2023             : 
    2024             :     /* update main combined orientation access index */
    2025        5562 :     ivas_combined_orientation_update_index( hCombinedOrientationData, n_samples_to_render );
    2026             : 
    2027        5562 :     return IVAS_ERR_OK;
    2028             : }

Generated by: LCOV version 1.14