LCOV - code coverage report
Current view: top level - lib_rend - ivas_hrtf.c (source / functions) Hit Total Coverage
Test: Coverage on main -- merged total coverage @ efe53129c9ed87a5067dd0a8fb9dca41db9c4add Lines: 130 142 91.5 %
Date: 2026-02-12 08:06:51 Functions: 15 15 100.0 %

          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_rend.h"
      37             : #include "ivas_error.h"
      38             : #include "wmc_auto.h"
      39             : #include "ivas_prot.h"
      40             : 
      41             : 
      42             : /*-----------------------------------------------------------------------*
      43             :  * ivas_HRTF_td_binary_open()
      44             :  *
      45             :  * Allocate HRTF binary handle for TD renderer
      46             :  *-----------------------------------------------------------------------*/
      47             : 
      48        6263 : ivas_error ivas_HRTF_td_binary_open(
      49             :     TDREND_HRFILT_FiltSet_t **hHrtfTD )
      50             : {
      51             :     /* Allocate HR filter set for headphones configuration */
      52        6263 :     *hHrtfTD = (TDREND_HRFILT_FiltSet_t *) malloc( sizeof( TDREND_HRFILT_FiltSet_t ) );
      53        6263 :     if ( *hHrtfTD == NULL )
      54             :     {
      55           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HRTF binary!" );
      56             :     }
      57             : 
      58        6263 :     set_c( (int8_t *) ( *hHrtfTD ), 0, (int32_t) sizeof( TDREND_HRFILT_FiltSet_t ) );
      59             : 
      60        6263 :     return IVAS_ERR_OK;
      61             : }
      62             : 
      63             : 
      64             : /*-------------------------------------------------------------------*
      65             :  * ivas_HRTF_binary_close()
      66             :  *
      67             :  * Close HRTF binary handle for TD renderer
      68             :  *-------------------------------------------------------------------*/
      69             : 
      70      122202 : void ivas_HRTF_td_binary_close(
      71             :     TDREND_HRFILT_FiltSet_t **hHrtfTD )
      72             : {
      73      122202 :     if ( hHrtfTD == NULL || *hHrtfTD == NULL )
      74             :     {
      75      115939 :         return;
      76             :     }
      77             : 
      78             :     int16_t i;
      79             : 
      80        6263 :     if ( ( *hHrtfTD )->ModelParams.modelROM )
      81             :     {
      82           0 :         BSplineModelEvalDealloc( &( *hHrtfTD )->ModelParams, &( *hHrtfTD )->ModelEval );
      83             :     }
      84             :     else
      85             :     {
      86        6263 :         if ( ( *hHrtfTD )->ModelParams.UseItdModel )
      87             :         {
      88        6263 :             free( ( *hHrtfTD )->ModelParamsITD.elevKSeq_dyn );
      89        6263 :             free( ( *hHrtfTD )->ModelParamsITD.azimKSeq_dyn );
      90        6263 :             free( ( *hHrtfTD )->ModelParamsITD.W_dyn );
      91        6263 :             free( ( *hHrtfTD )->ModelParamsITD.azimBsShape_dyn );
      92        6263 :             free( ( *hHrtfTD )->ModelParamsITD.elevBsShape_dyn );
      93        6263 :             free( ( *hHrtfTD )->ModelParamsITD.azimBsLen_dyn );
      94        6263 :             free( ( *hHrtfTD )->ModelParamsITD.azimBsStart_dyn );
      95        6263 :             free( ( *hHrtfTD )->ModelParamsITD.elevBsLen_dyn );
      96        6263 :             free( ( *hHrtfTD )->ModelParamsITD.elevBsStart_dyn );
      97             :         }
      98        6263 :         free( ( *hHrtfTD )->ModelParams.elevKSeq_dyn );
      99        6263 :         free( ( *hHrtfTD )->ModelParams.azim_start_idx_dyn );
     100        6263 :         free( ( *hHrtfTD )->ModelParams.azimDim3_dyn );
     101        6263 :         free( ( *hHrtfTD )->ModelParams.AlphaL_dyn );
     102        6263 :         free( ( *hHrtfTD )->ModelParams.AlphaR_dyn );
     103        6263 :         free( ( *hHrtfTD )->ModelParams.azimSegSamples_dyn );
     104             : 
     105        6263 :         free( ( *hHrtfTD )->ModelParams.azimShapeIdx_dyn );
     106        6263 :         free( ( *hHrtfTD )->ModelParams.azimShapeSampFactor_dyn );
     107        6263 :         free( ( *hHrtfTD )->ModelParams.elevBsLen_dyn );
     108        6263 :         free( ( *hHrtfTD )->ModelParams.elevBsStart_dyn );
     109        6263 :         free( ( *hHrtfTD )->ModelParams.elevBsShape_dyn );
     110             : 
     111       12526 :         for ( i = 0; i < ( *hHrtfTD )->ModelParams.num_unique_azim_splines; i++ )
     112             :         {
     113        6263 :             free( ( *hHrtfTD )->ModelParams.azimBsShape_dyn[i] );
     114             :         }
     115        6263 :         free( ( *hHrtfTD )->ModelParams.azimBsShape_dyn );
     116             : 
     117        6263 :         free( (void *) ( *hHrtfTD )->ModelParams.azimBsShape ); /* void* cast needed to please both gcc and Visual studio compilers. Deallocating const float** should be fine and gcc agrees, but Visual studio complains. */
     118      100208 :         for ( i = 0; i < ( *hHrtfTD )->ModelParams.elevDim3; i++ )
     119             :         {
     120       93945 :             free( ( *hHrtfTD )->ModelParams.azimKSeq[i] );
     121             :         }
     122        6263 :         free( ( *hHrtfTD )->ModelParams.azimKSeq );
     123             : 
     124        6263 :         free( ( *hHrtfTD )->ModelParams.EL_dyn );
     125        6263 :         free( ( *hHrtfTD )->ModelParams.ER_dyn );
     126             : 
     127        6263 :         free( ( *hHrtfTD )->ModelEval.hrfModL );
     128        6263 :         free( ( *hHrtfTD )->ModelEval.hrfModR );
     129             :     }
     130             : 
     131        6263 :     free( *hHrtfTD );
     132        6263 :     *hHrtfTD = NULL;
     133             : 
     134        6263 :     return;
     135             : }
     136             : 
     137             : /*-----------------------------------------------------------------------*
     138             :  * ivas_HRTF_CRend_binary_open()
     139             :  *
     140             :  * Allocate HRTF binary handle for Crend renderer
     141             :  *-----------------------------------------------------------------------*/
     142             : 
     143        2288 : ivas_error ivas_HRTF_CRend_binary_open(
     144             :     HRTFS_CREND_DATA **hHrtfCrend )
     145             : {
     146             :     ivas_error error;
     147             : 
     148        2288 :     if ( ( *hHrtfCrend = (HRTFS_CREND_HANDLE) malloc( sizeof( HRTFS_CREND_DATA ) ) ) == NULL )
     149             :     {
     150           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Crend HRTFS Handle\n" );
     151             :     }
     152             : 
     153        2288 :     if ( ( error = ivas_Crend_hrtf_init( *hHrtfCrend ) ) != IVAS_ERR_OK )
     154             :     {
     155           0 :         return error;
     156             :     }
     157             : 
     158        2288 :     return IVAS_ERR_OK;
     159             : }
     160             : 
     161             : 
     162             : /*-----------------------------------------------------------------------*
     163             :  * ivas_HRTF_binary_open_buffers_int16()
     164             :  *
     165             :  * Allocate buffer with dynamic length for HRTF binary tables
     166             :  *-----------------------------------------------------------------------*/
     167             : 
     168       68893 : ivas_error ivas_HRTF_binary_open_buffers_int16(
     169             :     int16_t **buffer,       /* o  : buffer to allocate      */
     170             :     const uint32_t mem_size /* i  : size of buffer          */
     171             : )
     172             : {
     173       68893 :     *buffer = (int16_t *) malloc( mem_size );
     174             : 
     175       68893 :     if ( *buffer == NULL )
     176             :     {
     177           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HRTF binary data\n" );
     178             :     }
     179             : 
     180       68893 :     return IVAS_ERR_OK;
     181             : }
     182             : 
     183             : 
     184             : /*-----------------------------------------------------------------------*
     185             :  * ivas_HRTF_binary_open_buffers_uint16()
     186             :  *
     187             :  * Allocate buffer with dynamic length for HRTF binary tables
     188             :  *-----------------------------------------------------------------------*/
     189             : 
     190       70788 : ivas_error ivas_HRTF_binary_open_buffers_uint16(
     191             :     uint16_t **buffer,      /* o  : buffer to allocate      */
     192             :     const uint32_t mem_size /* i  : size of buffer          */
     193             : )
     194             : {
     195       70788 :     *buffer = (uint16_t *) malloc( mem_size );
     196             : 
     197       70788 :     if ( *buffer == NULL )
     198             :     {
     199           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HRTF binary data\n" );
     200             :     }
     201             : 
     202       70788 :     return IVAS_ERR_OK;
     203             : }
     204             : 
     205             : 
     206             : /*-----------------------------------------------------------------------*
     207             :  * ivas_HRTF_binary_open_buffers_float()
     208             :  *
     209             :  * Allocate buffer with dynamic length for HRTF binary tables
     210             :  *-----------------------------------------------------------------------*/
     211             : 
     212      325642 : ivas_error ivas_HRTF_binary_open_buffers_float(
     213             :     float **buffer,         /* o  : buffer to allocate      */
     214             :     const uint32_t mem_size /* i  : size of buffer          */
     215             : )
     216             : {
     217      325642 :     *buffer = (float *) malloc( mem_size );
     218             : 
     219      325642 :     if ( *buffer == NULL )
     220             :     {
     221           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HRTF binary data\n" );
     222             :     }
     223             : 
     224      325642 :     return IVAS_ERR_OK;
     225             : }
     226             : 
     227             : 
     228             : /*-----------------------------------------------------------------------*
     229             :  * ivas_HRTF_binary_open_buffers_float_ptr()
     230             :  *
     231             :  * Allocate buffer with dynamic length for HRTF binary tables
     232             :  *-----------------------------------------------------------------------*/
     233             : 
     234       12526 : ivas_error ivas_HRTF_binary_open_buffers_float_ptr(
     235             :     float ***buffer,        /* o  : buffer to allocate      */
     236             :     const uint32_t mem_size /* i  : size of buffer          */
     237             : )
     238             : {
     239       12526 :     *buffer = (float **) malloc( mem_size );
     240             : 
     241       12526 :     if ( *buffer == NULL )
     242             :     {
     243           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HRTF binary data\n" );
     244             :     }
     245             : 
     246       12526 :     return IVAS_ERR_OK;
     247             : }
     248             : 
     249             : 
     250             : /*-----------------------------------------------------------------------*
     251             :  * ivas_HRTF_binary_open_buffers_float_ptr()
     252             :  *
     253             :  * Allocate buffer with dynamic length for HRTF binary tables
     254             :  *-----------------------------------------------------------------------*/
     255             : 
     256        6263 : ivas_error ivas_HRTF_binary_open_buffers_float_ptr_const(
     257             :     const float ***buffer,  /* o  : buffer to allocate      */
     258             :     const uint32_t mem_size /* i  : size of buffer          */
     259             : )
     260             : {
     261        6263 :     *buffer = (const float **) malloc( mem_size );
     262             : 
     263        6263 :     if ( *buffer == NULL )
     264             :     {
     265           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HRTF binary data\n" );
     266             :     }
     267             : 
     268        6263 :     return IVAS_ERR_OK;
     269             : }
     270             : 
     271             : 
     272             : /*-------------------------------------------------------------------*
     273             :  * ivas_HRTF_CRend_binary_close()
     274             :  *
     275             :  * Close HRTF CRend binary handle for Crend renderer
     276             :  *-------------------------------------------------------------------*/
     277             : 
     278      121336 : void ivas_HRTF_CRend_binary_close(
     279             :     HRTFS_CREND_DATA **hHrtfCrend )
     280             : {
     281             :     uint16_t i, j;
     282             : 
     283      121336 :     if ( *hHrtfCrend == NULL || hHrtfCrend == NULL )
     284             :     {
     285      119048 :         return;
     286             :     }
     287             : 
     288       38896 :     for ( i = 0; i < MAX_INTERN_CHANNELS; i++ )
     289             :     {
     290      109824 :         for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     291             :         {
     292       73216 :             if ( ( *hHrtfCrend )->pIndex_frequency_max[i][j] != NULL )
     293             :             {
     294       68640 :                 free( ( *hHrtfCrend )->pIndex_frequency_max_dyn[i][j] );
     295             :             }
     296       73216 :             if ( ( *hHrtfCrend )->pOut_to_bin_re[i][j] != NULL )
     297             :             {
     298       68640 :                 free( ( *hHrtfCrend )->pOut_to_bin_re_dyn[i][j] );
     299             :             }
     300       73216 :             if ( ( *hHrtfCrend )->pOut_to_bin_im[i][j] != NULL )
     301             :             {
     302       68640 :                 free( ( *hHrtfCrend )->pOut_to_bin_im_dyn[i][j] );
     303             :             }
     304             :         }
     305             :     }
     306        6864 :     for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     307             :     {
     308        4576 :         if ( ( *hHrtfCrend )->pIndex_frequency_max_diffuse[j] != NULL )
     309             :         {
     310        2148 :             free( ( *hHrtfCrend )->pIndex_frequency_max_diffuse_dyn[j] );
     311             :         }
     312        4576 :         if ( ( *hHrtfCrend )->pOut_to_bin_diffuse_re[j] != NULL )
     313             :         {
     314        2148 :             free( ( *hHrtfCrend )->pOut_to_bin_diffuse_re_dyn[j] );
     315             :         }
     316        4576 :         if ( ( *hHrtfCrend )->pOut_to_bin_diffuse_im[j] != NULL )
     317             :         {
     318        2148 :             free( ( *hHrtfCrend )->pOut_to_bin_diffuse_im_dyn[j] );
     319             :         }
     320             :     }
     321             : 
     322        2288 :     free( *hHrtfCrend );
     323        2288 :     *hHrtfCrend = NULL;
     324             : 
     325        2288 :     return;
     326             : }
     327             : 
     328             : 
     329             : /*-----------------------------------------------------------------------*
     330             :  * ivas_HRTF_fastconv_binary_open()
     331             :  *
     332             :  * Allocate HRTF binary handle for FASTCONV renderer
     333             :  *-----------------------------------------------------------------------*/
     334             : 
     335        5039 : ivas_error ivas_HRTF_fastconv_binary_open(
     336             :     HRTFS_FASTCONV **hHrtfFastConv )
     337             : {
     338        5039 :     *hHrtfFastConv = (HRTFS_FASTCONV *) malloc( sizeof( HRTFS_FASTCONV ) );
     339        5039 :     if ( *hHrtfFastConv == NULL )
     340             :     {
     341           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FASTCONV HRTF tables!" );
     342             :     }
     343             : 
     344        5039 :     ivas_init_binaural_hrtf( *hHrtfFastConv );
     345             : 
     346        5039 :     return IVAS_ERR_OK;
     347             : }
     348             : 
     349             : 
     350             : /*-----------------------------------------------------------------------*
     351             :  * ivas_HRTF_fastconv_binary_close()
     352             :  *
     353             :  * Close HRTF binary handle for FASTCONV renderer
     354             :  *-----------------------------------------------------------------------*/
     355             : 
     356      119496 : void ivas_HRTF_fastconv_binary_close(
     357             :     HRTFS_FASTCONV **hHrtfFastConv )
     358             : {
     359      119496 :     if ( hHrtfFastConv == NULL || *hHrtfFastConv == NULL )
     360             :     {
     361      108964 :         return;
     362             :     }
     363             : 
     364             :     /* Fastconv HRTF memories */
     365       10532 :     ivas_binaural_hrtf_close( hHrtfFastConv );
     366             : 
     367       10532 :     free( *hHrtfFastConv );
     368       10532 :     *hHrtfFastConv = NULL;
     369             : 
     370       10532 :     return;
     371             : }
     372             : 
     373             : 
     374             : /*-----------------------------------------------------------------------*
     375             :  * ivas_HRTF_parambin_binary_open()
     376             :  *
     377             :  * Allocate HRTF binary handle for parametric binauralizer
     378             :  *-----------------------------------------------------------------------*/
     379             : 
     380        8637 : ivas_error ivas_HRTF_parambin_binary_open(
     381             :     HRTFS_PARAMBIN **hHrtfParambin )
     382             : {
     383        8637 :     *hHrtfParambin = (HRTFS_PARAMBIN *) malloc( sizeof( HRTFS_PARAMBIN ) );
     384        8637 :     if ( *hHrtfParambin == NULL )
     385             :     {
     386           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for parametric binauralizer HRTF tables!" );
     387             :     }
     388             : 
     389        8637 :     set_c( (int8_t *) ( *hHrtfParambin ), 0, (int32_t) sizeof( HRTFS_PARAMBIN ) );
     390             : 
     391        8637 :     return IVAS_ERR_OK;
     392             : }
     393             : 
     394             : 
     395             : /*-----------------------------------------------------------------------*
     396             :  * ivas_HRTF_parambin_binary_close()
     397             :  *
     398             :  * Close HRTF binary handle for parametric binauralizer
     399             :  *-----------------------------------------------------------------------*/
     400             : 
     401      117113 : void ivas_HRTF_parambin_binary_close(
     402             :     HRTFS_PARAMBIN **hHrtfParambin )
     403             : {
     404      117113 :     if ( hHrtfParambin == NULL || *hHrtfParambin == NULL )
     405             :     {
     406       95742 :         return;
     407             :     }
     408             : 
     409       21371 :     free( *hHrtfParambin );
     410       21371 :     *hHrtfParambin = NULL;
     411             : 
     412       21371 :     return;
     413             : }
     414             : 
     415             : /*-----------------------------------------------------------------------*
     416             :  * ivas_HRTF_statistics_binary_open()
     417             :  *
     418             :  * Allocate HRTF binary handle for statistics handler
     419             :  *-----------------------------------------------------------------------*/
     420             : 
     421         708 : ivas_error ivas_HRTF_statistics_binary_open(
     422             :     HRTFS_STATISTICS **hHrtfStatistics )
     423             : {
     424         708 :     *hHrtfStatistics = (HRTFS_STATISTICS *) malloc( sizeof( HRTFS_STATISTICS ) );
     425         708 :     if ( *hHrtfStatistics == NULL )
     426             :     {
     427           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for statistics HRTF tables!" );
     428             :     }
     429             : 
     430         708 :     set_c( (int8_t *) ( *hHrtfStatistics ), 0, (int32_t) sizeof( HRTFS_STATISTICS ) );
     431             : 
     432         708 :     return IVAS_ERR_OK;
     433             : }
     434             : 
     435             : 
     436             : /*-----------------------------------------------------------------------*
     437             :  * ivas_HRTF_statistics_close()
     438             :  *
     439             :  * Close HRTF binary handle for statistics handler
     440             :  *-----------------------------------------------------------------------*/
     441             : 
     442      107419 : void ivas_HRTF_statistics_binary_close(
     443             :     HRTFS_STATISTICS **hHrtfStatistics )
     444             : {
     445      107419 :     if ( hHrtfStatistics == NULL || *hHrtfStatistics == NULL )
     446             :     {
     447       95552 :         return;
     448             :     }
     449             : 
     450       11867 :     if ( ( hHrtfStatistics != NULL ) && ( *hHrtfStatistics != NULL ) && ( ( *hHrtfStatistics )->fromROM == FALSE ) )
     451             :     {
     452         813 :         if ( ( *hHrtfStatistics )->average_energy_l_dyn != NULL )
     453             :         {
     454         813 :             free( ( *hHrtfStatistics )->average_energy_l_dyn );
     455             :         }
     456         813 :         if ( ( *hHrtfStatistics )->average_energy_r_dyn != NULL )
     457             :         {
     458         813 :             free( ( *hHrtfStatistics )->average_energy_r_dyn );
     459             :         }
     460         813 :         if ( ( *hHrtfStatistics )->inter_aural_coherence_dyn != NULL )
     461             :         {
     462         813 :             free( ( *hHrtfStatistics )->inter_aural_coherence_dyn );
     463             :         }
     464             :     }
     465             : 
     466       11867 :     free( *hHrtfStatistics );
     467       11867 :     *hHrtfStatistics = NULL;
     468             : 
     469       11867 :     return;
     470             : }

Generated by: LCOV version 1.14