From 17d1c8801c3134b5e68130ec578f089ab7df68e8 Mon Sep 17 00:00:00 2001 From: Archit Tamarapu Date: Thu, 16 May 2024 17:24:06 +0200 Subject: [PATCH 1/5] tune/reduce memory allocation for EFAP --- lib_com/ivas_cnst.h | 7 +++++- lib_com/options.h | 1 + lib_dec/ivas_ism_renderer.c | 7 ++++++ lib_rend/ivas_efap.c | 49 ++++++++++++++++++++++++++++++++++--- lib_rend/ivas_stat_rend.h | 38 ++++++++++++++++++++-------- 5 files changed, 87 insertions(+), 15 deletions(-) diff --git a/lib_com/ivas_cnst.h b/lib_com/ivas_cnst.h index 9d842f9b37..4b5249c506 100755 --- a/lib_com/ivas_cnst.h +++ b/lib_com/ivas_cnst.h @@ -1458,12 +1458,17 @@ typedef enum /*----------------------------------------------------------------------------------* * Amplitude Panning (EFAP, VBAP) constants *----------------------------------------------------------------------------------*/ - +#ifndef FIX_1050_EFAP_ALLOC #define PANNING_AZI_RESOLUTION 2 #define PANNING_ELE_RESOLUTION 5 +#endif #define EFAP_MAX_CHAN_NUM 5 /* Maximum number of channels that constitute a polygon, 4 or 5 */ +#ifdef FIX_1050_EFAP_ALLOC +#define EFAP_MAX_POLY_SET 36 /* Upper bound on number of polygons */ +#else #define EFAP_MAX_POLY_SET 50 /* Upper bound on number of polygons; with a Speaker setup of 16.0, we obtain 44 polygons/triangles in the matlab implementation. */ +#endif #define EFAP_MODE_EFAP 0 /* EFAP Panning */ #define EFAP_MODE_EFIP 1 /* EFIP Panning */ diff --git a/lib_com/options.h b/lib_com/options.h index 65657cb7e9..8c589a8098 100644 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -169,6 +169,7 @@ #define FIX_989_TD_REND_ROM /* Eri: Clean-up for TD renderer and completion of ROM generation tool */ #define FIX_1068_ASAN_IN_MC_2_BINAURAL_ROOM_IR /* issue 1068 : Memory leak in MC to BINAURAL_ROOM decoding with bitrate switching*/ +#define FIX_1050_EFAP_ALLOC /* FhG: issue 1050: reduction of memory allocated to EFAP handle */ /* #################### End BE switches ################################## */ diff --git a/lib_dec/ivas_ism_renderer.c b/lib_dec/ivas_ism_renderer.c index b890dd98e5..743a99275c 100644 --- a/lib_dec/ivas_ism_renderer.c +++ b/lib_dec/ivas_ism_renderer.c @@ -66,7 +66,14 @@ ivas_error ivas_ism_renderer_open( return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for ISM renderer\n" ) ); } +#ifdef FIX_1050_EFAP_ALLOC + if ( st_ivas->hIntSetup.is_loudspeaker_setup && + st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_STEREO && + st_ivas->hIntSetup.ls_azimuth != NULL && st_ivas->hIntSetup.ls_elevation != NULL && + st_ivas->hEFAPdata == NULL ) +#else if ( st_ivas->hIntSetup.is_loudspeaker_setup && st_ivas->hIntSetup.ls_azimuth != NULL && st_ivas->hIntSetup.ls_elevation != NULL && st_ivas->hEFAPdata == NULL ) +#endif { if ( ( error = efap_init_data( &( st_ivas->hEFAPdata ), st_ivas->hIntSetup.ls_azimuth, st_ivas->hIntSetup.ls_elevation, st_ivas->hIntSetup.nchan_out_woLFE, EFAP_MODE_EFAP ) ) != IVAS_ERR_OK ) { diff --git a/lib_rend/ivas_efap.c b/lib_rend/ivas_efap.c index 3caf13783f..94a670a00c 100644 --- a/lib_rend/ivas_efap.c +++ b/lib_rend/ivas_efap.c @@ -51,6 +51,12 @@ #define EFAP_MAX_SIZE_TMP_BUFF 30 #define EFAP_MAX_GHOST_LS 5 /* Maximum number of ghost Loudspeakers, for memory allocation purpose */ #define POLY_THRESH 1e-4f +#ifdef FIX_1050_EFAP_ALLOC +#ifdef DEBUG_EFAP_POLY_TOFILE +#define PANNING_AZI_RESOLUTION 2 +#define PANNING_ELE_RESOLUTION 5 +#endif +#endif /*-----------------------------------------------------------------------* @@ -147,6 +153,9 @@ ivas_error efap_init_data( ) { /* Handle instance declaration */ +#ifdef FIX_1050_EFAP_ALLOC + int8_t polyset_size; +#endif EFAP *efap; ivas_error error; @@ -163,13 +172,13 @@ ivas_error efap_init_data( * Allocate memory *-----------------------------------------------------------------*/ - /* Memory Allocations for efap */ + /* Memory allocation for main EFAP structure */ if ( ( efap = (EFAP *) malloc( sizeof( EFAP ) ) ) == NULL ) { return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for EFAP handle\n" ) ); } - /* Memory Allocation and update for aziSpk & eleSpk arrays*/ + /* Memory allocation and update for aziSpk & eleSpk arrays*/ if ( ( efap->aziSpk = (float *) malloc( num_speaker_nodes * sizeof( float ) ) ) == NULL ) { return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for EFAP speaker azimuths\n" ) ); @@ -179,7 +188,7 @@ ivas_error efap_init_data( return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for EFAP speaker elevations\n" ) ); } - /* Memory Allocation for vertexArray */ + /* Memory allocation for vertexArray */ if ( ( efap->vtxData.vertexArray = (EFAP_VERTEX *) malloc( ( num_speaker_nodes + EFAP_MAX_GHOST_LS ) * sizeof( EFAP_VERTEX ) ) ) == NULL ) { return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for EFAP Vertex Array\n" ) ); @@ -190,6 +199,22 @@ ivas_error efap_init_data( { return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for EFAP bufferS\n" ) ); } +#ifdef FIX_1050_EFAP_ALLOC + /* calculate upper bound of number of polygons required (heuristic) */ + polyset_size = 2 * num_speaker_nodes + 6; + + /* Memory allocation for the polyset array */ + if ( ( efap->polyData.polysetArray = (EFAP_POLYSET *) malloc( polyset_size * sizeof( EFAP_POLYSET ) ) ) == NULL ) + { + return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for EFAP bufferS\n" ) ); + } + + /* Memory allocation for the triangle array */ + if ( ( efap->polyData.triArray = (EFAP_LS_TRIANGLE *) malloc( polyset_size * sizeof( EFAP_LS_TRIANGLE ) ) ) == NULL ) + { + return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for EFAP bufferS\n" ) ); + } +#endif /*-----------------------------------------------------------------* * Initialize values @@ -333,6 +358,14 @@ void efap_free_data( free( ( *hEFAPdata )->vtxData.vtxOrder ); ( *hEFAPdata )->vtxData.vtxOrder = NULL; +#ifdef FIX_1050_EFAP_ALLOC + + free( ( *hEFAPdata )->polyData.polysetArray ); + ( *hEFAPdata )->vtxData.vtxOrder = NULL; + + free( ( *hEFAPdata )->polyData.triArray ); + ( *hEFAPdata )->vtxData.vtxOrder = NULL; +#endif free( ( *hEFAPdata )->bufferLong ); ( *hEFAPdata )->bufferLong = NULL; @@ -401,7 +434,11 @@ static ivas_error poly_init( if ( efap->vtxData.vertexArray[n].ele > 90.0 - 1e-6 || efap->vtxData.vertexArray[n].ele < 1e-6 - 90.0 ) { +#ifdef FIX_1050_EFAP_ALLOC + efap->vtxData.vertexArray[n].isNaN = true; +#else efap->vtxData.vertexArray[n].isNaN = 1; +#endif } } @@ -1493,8 +1530,12 @@ static void add_vertex( /* Final Idx */ vtxArray[pos].idx = (int16_t) idxAziTmp + 181 * (int16_t) idxEleTmp; - /* Setting the nan flag to 0 */ +/* Setting the nan flag to 0 */ +#ifdef FIX_1050_EFAP_ALLOC + vtxArray[pos].isNaN = false; +#else vtxArray[pos].isNaN = 0; +#endif /* Set the default downmix type */ vtxArray[pos].dmxType = dmxType; diff --git a/lib_rend/ivas_stat_rend.h b/lib_rend/ivas_stat_rend.h index d1f0fe4bca..3c8f42277d 100644 --- a/lib_rend/ivas_stat_rend.h +++ b/lib_rend/ivas_stat_rend.h @@ -580,11 +580,15 @@ typedef struct ivas_binaural_rendering_conv_module_struct typedef struct EFAP_VERTEX { - float azi; /* azimuth of the loudspeaker */ - float ele; /* elevation of the loudspeaker */ - float pos[3]; /* [x y z] cartesian coordinate vector */ - int16_t idx; /* integer, that corresponds to the first index for the LS in the 1D output */ - int16_t isNaN; /* used to indicate if the vertex is a virtual speaker */ + float azi; /* azimuth of the loudspeaker */ + float ele; /* elevation of the loudspeaker */ + float pos[3]; /* [x y z] cartesian coordinate vector */ + int16_t idx; /* integer, that corresponds to the first index for the LS in the 1D output */ +#ifdef FIX_1050_EFAP_ALLOC + bool isNaN; /* used to indicate if the vertex is a virtual speaker */ +#else + int16_t isNaN; /* used to indicate if the vertex is a virtual speaker */ +#endif EFAP_VTX_DMX_TYPE dmxType; /* virtual speaker downmix type */ } EFAP_VERTEX; @@ -599,9 +603,15 @@ typedef struct EFAP_VERTEX_DATA typedef struct EFAP_POLYSET { - int16_t chan[EFAP_MAX_CHAN_NUM]; /* An array indicating the loudspeaker index of the polygon vertices */ - int16_t isNaN[EFAP_MAX_CHAN_NUM]; /* Indicates if one of the vertices isNaN */ - int16_t numChan; /* An integer between 0 and EFAP_MAX_CHAN_NUM corresponding to the number of vertices of the polygon */ +#ifdef FIX_1050_EFAP_ALLOC + int8_t chan[EFAP_MAX_CHAN_NUM]; /* An array indicating the loudspeaker index of the polygon vertices */ + int8_t isNaN[EFAP_MAX_CHAN_NUM]; /* Indicates if one of the vertices isNaN */ + int8_t numChan; /* An integer between 0 and EFAP_MAX_CHAN_NUM corresponding to the number of vertices of the polygon */ +#else + int16_t chan[EFAP_MAX_CHAN_NUM]; /* An array indicating the loudspeaker index of the polygon vertices */ + int16_t isNaN[EFAP_MAX_CHAN_NUM]; /* Indicates if one of the vertices isNaN */ + int16_t numChan; /* An integer between 0 and EFAP_MAX_CHAN_NUM corresponding to the number of vertices of the polygon */ +#endif float polyAzi[EFAP_MAX_CHAN_NUM]; /* An array (same length as "chan"), with the azimuth of the channels */ float polyEle[EFAP_MAX_CHAN_NUM]; /* An array (same length as "chan"), with the elevation of the channels */ @@ -615,10 +625,18 @@ typedef struct EFAP_LS_TRIANGLE typedef struct EFAP_POLYSET_DATA { +#ifdef FIX_1050_EFAP_ALLOC + EFAP_POLYSET *polysetArray; /* Array of polygons */ +#else EFAP_POLYSET polysetArray[EFAP_MAX_POLY_SET]; /* Array of polygons */ - int16_t numPoly; /* Number of polygons */ +#endif + int16_t numPoly; /* Number of polygons */ +#ifdef FIX_1050_EFAP_ALLOC + EFAP_LS_TRIANGLE *triArray; /* Array of triangles */ +#else EFAP_LS_TRIANGLE triArray[EFAP_MAX_POLY_SET]; /* Array of triangles */ - int16_t numTri; /* Number of triangles */ +#endif + int16_t numTri; /* Number of triangles */ } EFAP_POLYSET_DATA; -- GitLab From b2f6f4b38286ce0f2468bbfba3de5865217cd139 Mon Sep 17 00:00:00 2001 From: Archit Tamarapu Date: Thu, 23 May 2024 14:00:39 +0200 Subject: [PATCH 2/5] fix upper limit on polygon count and MSVC warnings --- lib_com/ivas_cnst.h | 2 +- lib_rend/ivas_efap.c | 6 +++++- lib_rend/ivas_stat_rend.h | 8 +++----- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib_com/ivas_cnst.h b/lib_com/ivas_cnst.h index 4b5249c506..157006adee 100755 --- a/lib_com/ivas_cnst.h +++ b/lib_com/ivas_cnst.h @@ -1465,7 +1465,7 @@ typedef enum #define EFAP_MAX_CHAN_NUM 5 /* Maximum number of channels that constitute a polygon, 4 or 5 */ #ifdef FIX_1050_EFAP_ALLOC -#define EFAP_MAX_POLY_SET 36 /* Upper bound on number of polygons */ +#define EFAP_MAX_POLY_SET 40 /* Upper bound on number of polygons; (2 * num_spk + 8) = 40 for a speaker setup of 16.0 */ #else #define EFAP_MAX_POLY_SET 50 /* Upper bound on number of polygons; with a Speaker setup of 16.0, we obtain 44 polygons/triangles in the matlab implementation. */ #endif diff --git a/lib_rend/ivas_efap.c b/lib_rend/ivas_efap.c index 94a670a00c..6b4659cf8a 100644 --- a/lib_rend/ivas_efap.c +++ b/lib_rend/ivas_efap.c @@ -201,7 +201,7 @@ ivas_error efap_init_data( } #ifdef FIX_1050_EFAP_ALLOC /* calculate upper bound of number of polygons required (heuristic) */ - polyset_size = 2 * num_speaker_nodes + 6; + polyset_size = (int8_t) 2 * num_speaker_nodes + 8; /* Memory allocation for the polyset array */ if ( ( efap->polyData.polysetArray = (EFAP_POLYSET *) malloc( polyset_size * sizeof( EFAP_POLYSET ) ) ) == NULL ) @@ -471,6 +471,10 @@ static ivas_error poly_init( { for ( j = 0; j < lengthTri2PolySorted[n]; ++j ) { + if ( (m + 2) > EFAP_MAX_POLY_SET ) + { + fprintf( stderr, "Polygon count: %d\n", m + 2 ); + } assert( ( m + 2 < EFAP_MAX_POLY_SET ) && "EFAP: maximum polygons exceeded!" ); /* add two new polygons with azimuths wrapped to differing bounds */ diff --git a/lib_rend/ivas_stat_rend.h b/lib_rend/ivas_stat_rend.h index 3c8f42277d..db5c99ab70 100644 --- a/lib_rend/ivas_stat_rend.h +++ b/lib_rend/ivas_stat_rend.h @@ -603,15 +603,13 @@ typedef struct EFAP_VERTEX_DATA typedef struct EFAP_POLYSET { + int16_t chan[EFAP_MAX_CHAN_NUM]; /* An array indicating the loudspeaker index of the polygon vertices */ #ifdef FIX_1050_EFAP_ALLOC - int8_t chan[EFAP_MAX_CHAN_NUM]; /* An array indicating the loudspeaker index of the polygon vertices */ - int8_t isNaN[EFAP_MAX_CHAN_NUM]; /* Indicates if one of the vertices isNaN */ - int8_t numChan; /* An integer between 0 and EFAP_MAX_CHAN_NUM corresponding to the number of vertices of the polygon */ + bool isNaN[EFAP_MAX_CHAN_NUM]; /* Indicates if one of the vertices isNaN */ #else - int16_t chan[EFAP_MAX_CHAN_NUM]; /* An array indicating the loudspeaker index of the polygon vertices */ int16_t isNaN[EFAP_MAX_CHAN_NUM]; /* Indicates if one of the vertices isNaN */ - int16_t numChan; /* An integer between 0 and EFAP_MAX_CHAN_NUM corresponding to the number of vertices of the polygon */ #endif + int16_t numChan; /* An integer between 0 and EFAP_MAX_CHAN_NUM corresponding to the number of vertices of the polygon */ float polyAzi[EFAP_MAX_CHAN_NUM]; /* An array (same length as "chan"), with the azimuth of the channels */ float polyEle[EFAP_MAX_CHAN_NUM]; /* An array (same length as "chan"), with the elevation of the channels */ -- GitLab From 62bb9e1dcff6dc3d29922f7a723e1818f86abbbc Mon Sep 17 00:00:00 2001 From: Archit Tamarapu Date: Thu, 23 May 2024 15:33:48 +0200 Subject: [PATCH 3/5] remove code added for debugging --- lib_rend/ivas_efap.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib_rend/ivas_efap.c b/lib_rend/ivas_efap.c index 6b4659cf8a..bae6c901d7 100644 --- a/lib_rend/ivas_efap.c +++ b/lib_rend/ivas_efap.c @@ -471,10 +471,6 @@ static ivas_error poly_init( { for ( j = 0; j < lengthTri2PolySorted[n]; ++j ) { - if ( (m + 2) > EFAP_MAX_POLY_SET ) - { - fprintf( stderr, "Polygon count: %d\n", m + 2 ); - } assert( ( m + 2 < EFAP_MAX_POLY_SET ) && "EFAP: maximum polygons exceeded!" ); /* add two new polygons with azimuths wrapped to differing bounds */ -- GitLab From a661a1c4387697506729ede63f1acb0733d60d20 Mon Sep 17 00:00:00 2001 From: Archit Tamarapu Date: Fri, 24 May 2024 10:39:37 +0200 Subject: [PATCH 4/5] [fix] MSVC warning --- lib_rend/ivas_efap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib_rend/ivas_efap.c b/lib_rend/ivas_efap.c index bae6c901d7..fa9adbb546 100644 --- a/lib_rend/ivas_efap.c +++ b/lib_rend/ivas_efap.c @@ -201,7 +201,7 @@ ivas_error efap_init_data( } #ifdef FIX_1050_EFAP_ALLOC /* calculate upper bound of number of polygons required (heuristic) */ - polyset_size = (int8_t) 2 * num_speaker_nodes + 8; + polyset_size = (int8_t) ( 2 * num_speaker_nodes + 8 ); /* Memory allocation for the polyset array */ if ( ( efap->polyData.polysetArray = (EFAP_POLYSET *) malloc( polyset_size * sizeof( EFAP_POLYSET ) ) ) == NULL ) -- GitLab From 49904b5779117ec4921ccdd19f87c2261859c365 Mon Sep 17 00:00:00 2001 From: Archit Tamarapu Date: Mon, 27 May 2024 10:42:35 +0200 Subject: [PATCH 5/5] [fix] update EFAP max polygon count with values from worst case testing --- lib_com/ivas_cnst.h | 2 +- lib_rend/ivas_efap.c | 7 +++++-- lib_rend/ivas_rom_rend.c | 7 +++++++ lib_rend/ivas_rom_rend.h | 9 +++++++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib_com/ivas_cnst.h b/lib_com/ivas_cnst.h index 157006adee..b803c9860a 100755 --- a/lib_com/ivas_cnst.h +++ b/lib_com/ivas_cnst.h @@ -1465,7 +1465,7 @@ typedef enum #define EFAP_MAX_CHAN_NUM 5 /* Maximum number of channels that constitute a polygon, 4 or 5 */ #ifdef FIX_1050_EFAP_ALLOC -#define EFAP_MAX_POLY_SET 40 /* Upper bound on number of polygons; (2 * num_spk + 8) = 40 for a speaker setup of 16.0 */ +#define EFAP_MAX_POLY_SET 54 /* Upper bound on number of polygons; found to be 54 in the worst case for a speaker setup of 16.0 */ #else #define EFAP_MAX_POLY_SET 50 /* Upper bound on number of polygons; with a Speaker setup of 16.0, we obtain 44 polygons/triangles in the matlab implementation. */ #endif diff --git a/lib_rend/ivas_efap.c b/lib_rend/ivas_efap.c index fa9adbb546..5be932d60f 100644 --- a/lib_rend/ivas_efap.c +++ b/lib_rend/ivas_efap.c @@ -38,6 +38,9 @@ #include "prot.h" #include "ivas_prot.h" #include "ivas_prot_rend.h" +#ifdef FIX_1050_EFAP_ALLOC +#include "ivas_rom_rend.h" +#endif #include "ivas_stat_dec.h" #ifdef DEBUGGING #include "debug.h" @@ -200,8 +203,8 @@ ivas_error efap_init_data( return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for EFAP bufferS\n" ) ); } #ifdef FIX_1050_EFAP_ALLOC - /* calculate upper bound of number of polygons required (heuristic) */ - polyset_size = (int8_t) ( 2 * num_speaker_nodes + 8 ); + /* get upper bound of number of polygons required */ + polyset_size = efap_poly_limit[num_speaker_nodes - 1]; /* Memory allocation for the polyset array */ if ( ( efap->polyData.polysetArray = (EFAP_POLYSET *) malloc( polyset_size * sizeof( EFAP_POLYSET ) ) ) == NULL ) diff --git a/lib_rend/ivas_rom_rend.c b/lib_rend/ivas_rom_rend.c index 90a2feb86a..61871f2e33 100644 --- a/lib_rend/ivas_rom_rend.c +++ b/lib_rend/ivas_rom_rend.c @@ -388,8 +388,15 @@ const float ivas_reverb_default_DSR[IVAS_REVERB_DEFAULT_N_BANDS] = const float ls_azimuth_CICP1[1] = { 0.0f }; const float ls_elevation_CICP1[1] = { 0.0f }; +#ifdef FIX_1050_EFAP_ALLOC +/*----------------------------------------------------------------------------------* + * EFAP ROM tables + *----------------------------------------------------------------------------------*/ +const int8_t efap_poly_limit[MAX_OUTPUT_CHANNELS] = {22, 22, 22, 26, 30, 34, 36, 42, 42, 44, 47, 51, 52, 54, 54, 54}; + +#endif /*----------------------------------------------------------------------------------* * LS Renderer ROM tables *----------------------------------------------------------------------------------*/ diff --git a/lib_rend/ivas_rom_rend.h b/lib_rend/ivas_rom_rend.h index 9988962dbe..2041585dbe 100644 --- a/lib_rend/ivas_rom_rend.h +++ b/lib_rend/ivas_rom_rend.h @@ -125,6 +125,15 @@ extern const float ls_azimuth_CICP1[1]; extern const float ls_elevation_CICP1[1]; +#ifdef FIX_1050_EFAP_ALLOC +/*----------------------------------------------------------------------------------* + * EFAP ROM tables + *----------------------------------------------------------------------------------*/ + +extern const int8_t efap_poly_limit[MAX_OUTPUT_CHANNELS]; + + +#endif /*----------------------------------------------------------------------------------* * LS Configuration Converter ROM tables *----------------------------------------------------------------------------------*/ -- GitLab