LCOV - code coverage report
Current view: top level - lib_rend - ivas_hrtf.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 102 111 91.9 %
Date: 2025-05-23 08:37:30 Functions: 13 13 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <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             : #include "ivas_rom_binaural_crend_head.h"
      41             : 
      42             : /*-----------------------------------------------------------------------*
      43             :  * ivas_HRTF_binary_open()
      44             :  *
      45             :  * Allocate HRTF binary handle for TD renderer
      46             :  *-----------------------------------------------------------------------*/
      47             : 
      48         171 : ivas_error ivas_HRTF_binary_open(
      49             :     TDREND_HRFILT_FiltSet_t **hHrtfTD )
      50             : {
      51             :     /* Allocate HR filter set for headphones configuration */
      52         171 :     *hHrtfTD = (TDREND_HRFILT_FiltSet_t *) malloc( sizeof( TDREND_HRFILT_FiltSet_t ) );
      53         171 :     if ( *hHrtfTD == NULL )
      54             :     {
      55           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HRTF binary!" );
      56             :     }
      57             : 
      58         171 :     set_c( (int8_t *) ( *hHrtfTD ), 0, (int32_t) sizeof( TDREND_HRFILT_FiltSet_t ) );
      59             : 
      60         171 :     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        2120 : void ivas_HRTF_binary_close(
      71             :     TDREND_HRFILT_FiltSet_t **hHrtfTD )
      72             : {
      73        2120 :     if ( hHrtfTD == NULL || *hHrtfTD == NULL )
      74             :     {
      75        1949 :         return;
      76             :     }
      77             : 
      78         171 :     free( *hHrtfTD );
      79         171 :     *hHrtfTD = NULL;
      80             : 
      81         171 :     return;
      82             : }
      83             : 
      84             : 
      85             : /*-----------------------------------------------------------------------*
      86             :  * ivas_HRTF_CRend_binary_open()
      87             :  *
      88             :  * Allocate HRTF binary handle for Crend renderer
      89             :  *-----------------------------------------------------------------------*/
      90             : 
      91          84 : ivas_error ivas_HRTF_CRend_binary_open(
      92             :     HRTFS_CREND_DATA **hHrtfCrend )
      93             : {
      94             :     ivas_error error;
      95             : 
      96          84 :     if ( ( *hHrtfCrend = (HRTFS_CREND_HANDLE) malloc( sizeof( HRTFS_CREND_DATA ) ) ) == NULL )
      97             :     {
      98           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Crend HRTFS Handle\n" );
      99             :     }
     100             : 
     101          84 :     if ( ( error = ivas_Crend_hrtf_init( *hHrtfCrend ) ) != IVAS_ERR_OK )
     102             :     {
     103           0 :         return error;
     104             :     }
     105             : 
     106          84 :     return IVAS_ERR_OK;
     107             : }
     108             : 
     109             : 
     110             : /*-----------------------------------------------------------------------*
     111             :  * ivas_HRTF_CRend_binary_open_buffers_uint16()
     112             :  *
     113             :  * Allocate buffer with dynamic length for HRTF binary Crend handle
     114             :  *-----------------------------------------------------------------------*/
     115             : 
     116        2640 : ivas_error ivas_HRTF_CRend_binary_open_buffers_uint16(
     117             :     uint16_t **buffer,      /* o  : buffer to allocate      */
     118             :     const uint32_t mem_size /* i  : size of buffer          */
     119             : )
     120             : {
     121        2640 :     *buffer = (uint16_t *) malloc( mem_size );
     122             : 
     123        2640 :     if ( *buffer == NULL )
     124             :     {
     125           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HRTF binary data\n" );
     126             :     }
     127             : 
     128        2640 :     return IVAS_ERR_OK;
     129             : }
     130             : 
     131             : 
     132             : /*-----------------------------------------------------------------------*
     133             :  * ivas_HRTF_CRend_binary_open_buffers_float()
     134             :  *
     135             :  * Allocate buffer with dynamic length for HRTF binary Crend handle
     136             :  *-----------------------------------------------------------------------*/
     137             : 
     138        5280 : ivas_error ivas_HRTF_CRend_binary_open_buffers_float(
     139             :     float **buffer,         /* o  : buffer to allocate      */
     140             :     const uint32_t mem_size /* i  : size of buffer          */
     141             : )
     142             : {
     143        5280 :     *buffer = (float *) malloc( mem_size );
     144             : 
     145        5280 :     if ( *buffer == NULL )
     146             :     {
     147           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HRTF binary data\n" );
     148             :     }
     149             : 
     150        5280 :     return IVAS_ERR_OK;
     151             : }
     152             : 
     153             : 
     154             : /*-------------------------------------------------------------------*
     155             :  * ivas_HRTF_CRend_binary_close()
     156             :  *
     157             :  * Close HRTF CRend binary handle for Crend renderer
     158             :  *-------------------------------------------------------------------*/
     159             : 
     160        3770 : void ivas_HRTF_CRend_binary_close(
     161             :     HRTFS_CREND_DATA **hHrtfCrend )
     162             : {
     163             :     uint16_t i, j;
     164             : 
     165        3770 :     if ( *hHrtfCrend == NULL || hHrtfCrend == NULL )
     166             :     {
     167        3686 :         return;
     168             :     }
     169             : 
     170        1428 :     for ( i = 0; i < MAX_INTERN_CHANNELS; i++ )
     171             :     {
     172        4032 :         for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     173             :         {
     174        2688 :             if ( ( *hHrtfCrend )->pIndex_frequency_max[i][j] != NULL )
     175             :             {
     176        2520 :                 free( ( *hHrtfCrend )->pIndex_frequency_max_dyn[i][j] );
     177             :             }
     178        2688 :             if ( ( *hHrtfCrend )->pOut_to_bin_re[i][j] != NULL )
     179             :             {
     180        2520 :                 free( ( *hHrtfCrend )->pOut_to_bin_re_dyn[i][j] );
     181             :             }
     182        2688 :             if ( ( *hHrtfCrend )->pOut_to_bin_im[i][j] != NULL )
     183             :             {
     184        2520 :                 free( ( *hHrtfCrend )->pOut_to_bin_im_dyn[i][j] );
     185             :             }
     186             :         }
     187             :     }
     188         252 :     for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     189             :     {
     190         168 :         if ( ( *hHrtfCrend )->pIndex_frequency_max_diffuse[j] != NULL )
     191             :         {
     192         120 :             free( ( *hHrtfCrend )->pIndex_frequency_max_diffuse_dyn[j] );
     193             :         }
     194         168 :         if ( ( *hHrtfCrend )->pOut_to_bin_diffuse_re[j] != NULL )
     195             :         {
     196         120 :             free( ( *hHrtfCrend )->pOut_to_bin_diffuse_re_dyn[j] );
     197             :         }
     198         168 :         if ( ( *hHrtfCrend )->pOut_to_bin_diffuse_im[j] != NULL )
     199             :         {
     200         120 :             free( ( *hHrtfCrend )->pOut_to_bin_diffuse_im_dyn[j] );
     201             :         }
     202             :     }
     203             : 
     204          84 :     free( *hHrtfCrend );
     205          84 :     *hHrtfCrend = NULL;
     206             : 
     207          84 :     return;
     208             : }
     209             : 
     210             : 
     211             : /*-----------------------------------------------------------------------*
     212             :  * ivas_HRTF_fastconv_binary_open()
     213             :  *
     214             :  * Allocate HRTF binary handle for FASTCONV renderer
     215             :  *-----------------------------------------------------------------------*/
     216             : 
     217         204 : ivas_error ivas_HRTF_fastconv_binary_open(
     218             :     HRTFS_FASTCONV **hHrtfFastConv )
     219             : {
     220         204 :     *hHrtfFastConv = (HRTFS_FASTCONV *) malloc( sizeof( HRTFS_FASTCONV ) );
     221         204 :     if ( *hHrtfFastConv == NULL )
     222             :     {
     223           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FASTCONV HRTF tables!" );
     224             :     }
     225             : 
     226         204 :     ivas_init_binaural_hrtf( *hHrtfFastConv );
     227             : 
     228         204 :     return IVAS_ERR_OK;
     229             : }
     230             : 
     231             : 
     232             : /*-----------------------------------------------------------------------*
     233             :  * ivas_HRTF_fastconv_binary_close()
     234             :  *
     235             :  * Close HRTF binary handle for FASTCONV renderer
     236             :  *-----------------------------------------------------------------------*/
     237             : 
     238        3644 : void ivas_HRTF_fastconv_binary_close(
     239             :     HRTFS_FASTCONV **hHrtfFastConv )
     240             : {
     241        3644 :     if ( hHrtfFastConv == NULL || *hHrtfFastConv == NULL )
     242             :     {
     243        3302 :         return;
     244             :     }
     245             : 
     246         342 :     free( *hHrtfFastConv );
     247         342 :     *hHrtfFastConv = NULL;
     248             : 
     249         342 :     return;
     250             : }
     251             : 
     252             : 
     253             : /*-----------------------------------------------------------------------*
     254             :  * ivas_HRTF_parambin_binary_open()
     255             :  *
     256             :  * Allocate HRTF binary handle for parametric binauralizer
     257             :  *-----------------------------------------------------------------------*/
     258             : 
     259         270 : ivas_error ivas_HRTF_parambin_binary_open(
     260             :     HRTFS_PARAMBIN **hHrtfParambin )
     261             : {
     262         270 :     *hHrtfParambin = (HRTFS_PARAMBIN *) malloc( sizeof( HRTFS_PARAMBIN ) );
     263         270 :     if ( *hHrtfParambin == NULL )
     264             :     {
     265           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for parametric binauralizer HRTF tables!" );
     266             :     }
     267             : 
     268         270 :     set_c( (int8_t *) ( *hHrtfParambin ), 0, (int32_t) sizeof( HRTFS_PARAMBIN ) );
     269             : 
     270         270 :     return IVAS_ERR_OK;
     271             : }
     272             : 
     273             : 
     274             : /*-----------------------------------------------------------------------*
     275             :  * ivas_HRTF_parambin_binary_close()
     276             :  *
     277             :  * Close HRTF binary handle for parametric binauralizer
     278             :  *-----------------------------------------------------------------------*/
     279             : 
     280        3566 : void ivas_HRTF_parambin_binary_close(
     281             :     HRTFS_PARAMBIN **hHrtfParambin )
     282             : {
     283        3566 :     if ( hHrtfParambin == NULL || *hHrtfParambin == NULL )
     284             :     {
     285        2959 :         return;
     286             :     }
     287             : 
     288         607 :     free( *hHrtfParambin );
     289         607 :     *hHrtfParambin = NULL;
     290             : 
     291         607 :     return;
     292             : }
     293             : 
     294             : /*-----------------------------------------------------------------------*
     295             :  * ivas_HRTF_statistics_binary_open()
     296             :  *
     297             :  * Allocate HRTF binary handle for statistics handler
     298             :  *-----------------------------------------------------------------------*/
     299             : 
     300           9 : ivas_error ivas_HRTF_statistics_binary_open(
     301             :     HRTFS_STATISTICS **hHrtfStatistics )
     302             : {
     303           9 :     *hHrtfStatistics = (HRTFS_STATISTICS *) malloc( sizeof( HRTFS_STATISTICS ) );
     304           9 :     if ( *hHrtfStatistics == NULL )
     305             :     {
     306           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for statistics HRTF tables!" );
     307             :     }
     308             : 
     309           9 :     return IVAS_ERR_OK;
     310             : }
     311             : 
     312             : 
     313             : /*-----------------------------------------------------------------------*
     314             :  * ivas_HRTF_statistics_close()
     315             :  *
     316             :  * Close HRTF binary handle for statistics handler
     317             :  *-----------------------------------------------------------------------*/
     318             : 
     319        4585 : void ivas_HRTF_statistics_close(
     320             :     HRTFS_STATISTICS **hHrtfStatistics )
     321             : {
     322        4585 :     if ( hHrtfStatistics == NULL || *hHrtfStatistics == NULL )
     323             :     {
     324        4312 :         return;
     325             :     }
     326             : 
     327         273 :     free( *hHrtfStatistics );
     328         273 :     *hHrtfStatistics = NULL;
     329             : 
     330         273 :     return;
     331             : }
     332             : 
     333             : /*-----------------------------------------------------------------------*
     334             :  * ivas_HRTF_statistics_init()
     335             :  *
     336             :  * Allocates HRTF statistics handle and initializes from ROM
     337             :  *-----------------------------------------------------------------------*/
     338             : 
     339         273 : ivas_error ivas_HRTF_statistics_init(
     340             :     HRTFS_STATISTICS_HANDLE *hHrtfStatistics,
     341             :     const int32_t sampleRate )
     342             : {
     343             :     HRTFS_STATISTICS *HrtfStatistics;
     344             : 
     345         273 :     if ( hHrtfStatistics != NULL && *hHrtfStatistics != NULL )
     346             :     {
     347             :         /* Tables already loaded from file */
     348           9 :         return IVAS_ERR_OK;
     349             :     }
     350             : 
     351             :     /* Initialise tables from ROM */
     352         264 :     if ( ( HrtfStatistics = (HRTFS_STATISTICS *) malloc( sizeof( HRTFS_STATISTICS ) ) ) == NULL )
     353             :     {
     354           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for statistics HRTF tables!" );
     355             :     }
     356             : 
     357         264 :     HrtfStatistics->average_energy_l = NULL;
     358         264 :     HrtfStatistics->average_energy_r = NULL;
     359         264 :     HrtfStatistics->inter_aural_coherence = NULL;
     360             : 
     361         264 :     switch ( sampleRate )
     362             :     {
     363         228 :         case 48000:
     364         228 :             HrtfStatistics->average_energy_l = defaultHRIR_left_avg_power_48kHz;
     365         228 :             HrtfStatistics->average_energy_r = defaultHRIR_right_avg_power_48kHz;
     366         228 :             HrtfStatistics->inter_aural_coherence = defaultHRIR_coherence_48kHz;
     367         228 :             break;
     368          24 :         case 32000:
     369          24 :             HrtfStatistics->average_energy_l = defaultHRIR_left_avg_power_32kHz;
     370          24 :             HrtfStatistics->average_energy_r = defaultHRIR_right_avg_power_32kHz;
     371          24 :             HrtfStatistics->inter_aural_coherence = defaultHRIR_coherence_32kHz;
     372          24 :             break;
     373          12 :         case 16000:
     374          12 :             HrtfStatistics->average_energy_l = defaultHRIR_left_avg_power_16kHz;
     375          12 :             HrtfStatistics->average_energy_r = defaultHRIR_right_avg_power_16kHz;
     376          12 :             HrtfStatistics->inter_aural_coherence = defaultHRIR_coherence_16kHz;
     377          12 :             break;
     378             :     }
     379         264 :     HrtfStatistics->fromROM = TRUE;
     380             : 
     381         264 :     *hHrtfStatistics = HrtfStatistics;
     382             : 
     383         264 :     return IVAS_ERR_OK;
     384             : }

Generated by: LCOV version 1.14