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 2540 : ivas_error ivas_HRTF_td_binary_open(
49 : TDREND_HRFILT_FiltSet_t **hHrtfTD )
50 : {
51 : /* Allocate HR filter set for headphones configuration */
52 2540 : *hHrtfTD = (TDREND_HRFILT_FiltSet_t *) malloc( sizeof( TDREND_HRFILT_FiltSet_t ) );
53 2540 : if ( *hHrtfTD == NULL )
54 : {
55 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HRTF binary!" );
56 : }
57 :
58 2540 : set_c( (int8_t *) ( *hHrtfTD ), 0, (int32_t) sizeof( TDREND_HRFILT_FiltSet_t ) );
59 :
60 2540 : 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 99714 : void ivas_HRTF_td_binary_close(
71 : TDREND_HRFILT_FiltSet_t **hHrtfTD )
72 : {
73 99714 : if ( hHrtfTD == NULL || *hHrtfTD == NULL )
74 : {
75 97174 : return;
76 : }
77 :
78 : int16_t i;
79 :
80 2540 : if ( ( *hHrtfTD )->ModelParams.modelROM )
81 : {
82 0 : BSplineModelEvalDealloc( &( *hHrtfTD )->ModelParams, &( *hHrtfTD )->ModelEval );
83 : }
84 : else
85 : {
86 2540 : if ( ( *hHrtfTD )->ModelParams.UseItdModel )
87 : {
88 2540 : free( ( *hHrtfTD )->ModelParamsITD.elevKSeq_dyn );
89 2540 : free( ( *hHrtfTD )->ModelParamsITD.azimKSeq_dyn );
90 2540 : free( ( *hHrtfTD )->ModelParamsITD.W_dyn );
91 2540 : free( ( *hHrtfTD )->ModelParamsITD.azimBsShape_dyn );
92 2540 : free( ( *hHrtfTD )->ModelParamsITD.elevBsShape_dyn );
93 2540 : free( ( *hHrtfTD )->ModelParamsITD.azimBsLen_dyn );
94 2540 : free( ( *hHrtfTD )->ModelParamsITD.azimBsStart_dyn );
95 2540 : free( ( *hHrtfTD )->ModelParamsITD.elevBsLen_dyn );
96 2540 : free( ( *hHrtfTD )->ModelParamsITD.elevBsStart_dyn );
97 : }
98 2540 : free( ( *hHrtfTD )->ModelParams.elevKSeq_dyn );
99 2540 : free( ( *hHrtfTD )->ModelParams.azim_start_idx_dyn );
100 2540 : free( ( *hHrtfTD )->ModelParams.azimDim3_dyn );
101 2540 : free( ( *hHrtfTD )->ModelParams.AlphaL_dyn );
102 2540 : free( ( *hHrtfTD )->ModelParams.AlphaR_dyn );
103 2540 : free( ( *hHrtfTD )->ModelParams.azimSegSamples_dyn );
104 :
105 2540 : free( ( *hHrtfTD )->ModelParams.azimShapeIdx_dyn );
106 2540 : free( ( *hHrtfTD )->ModelParams.azimShapeSampFactor_dyn );
107 2540 : free( ( *hHrtfTD )->ModelParams.elevBsLen_dyn );
108 2540 : free( ( *hHrtfTD )->ModelParams.elevBsStart_dyn );
109 2540 : free( ( *hHrtfTD )->ModelParams.elevBsShape_dyn );
110 :
111 5080 : for ( i = 0; i < ( *hHrtfTD )->ModelParams.num_unique_azim_splines; i++ )
112 : {
113 2540 : free( ( *hHrtfTD )->ModelParams.azimBsShape_dyn[i] );
114 : }
115 2540 : free( ( *hHrtfTD )->ModelParams.azimBsShape_dyn );
116 :
117 2540 : 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 40640 : for ( i = 0; i < ( *hHrtfTD )->ModelParams.elevDim3; i++ )
119 : {
120 38100 : free( ( *hHrtfTD )->ModelParams.azimKSeq[i] );
121 : }
122 2540 : free( ( *hHrtfTD )->ModelParams.azimKSeq );
123 :
124 2540 : free( ( *hHrtfTD )->ModelParams.EL_dyn );
125 2540 : free( ( *hHrtfTD )->ModelParams.ER_dyn );
126 :
127 2540 : free( ( *hHrtfTD )->ModelEval.hrfModL );
128 2540 : free( ( *hHrtfTD )->ModelEval.hrfModR );
129 : }
130 :
131 2540 : free( *hHrtfTD );
132 2540 : *hHrtfTD = NULL;
133 :
134 2540 : return;
135 : }
136 :
137 : /*-----------------------------------------------------------------------*
138 : * ivas_HRTF_CRend_binary_open()
139 : *
140 : * Allocate HRTF binary handle for Crend renderer
141 : *-----------------------------------------------------------------------*/
142 :
143 853 : ivas_error ivas_HRTF_CRend_binary_open(
144 : HRTFS_CREND_DATA **hHrtfCrend )
145 : {
146 : ivas_error error;
147 :
148 853 : 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 853 : if ( ( error = ivas_Crend_hrtf_init( *hHrtfCrend ) ) != IVAS_ERR_OK )
154 : {
155 0 : return error;
156 : }
157 :
158 853 : 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 27940 : 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 27940 : *buffer = (int16_t *) malloc( mem_size );
174 :
175 27940 : 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 27940 : 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 26852 : 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 26852 : *buffer = (uint16_t *) malloc( mem_size );
196 :
197 26852 : 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 26852 : 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 129767 : 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 129767 : *buffer = (float *) malloc( mem_size );
218 :
219 129767 : 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 129767 : 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 5080 : 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 5080 : *buffer = (float **) malloc( mem_size );
240 :
241 5080 : 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 5080 : 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 2540 : 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 2540 : *buffer = (const float **) malloc( mem_size );
262 :
263 2540 : 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 2540 : 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 99621 : void ivas_HRTF_CRend_binary_close(
279 : HRTFS_CREND_DATA **hHrtfCrend )
280 : {
281 : uint16_t i, j;
282 :
283 99621 : if ( *hHrtfCrend == NULL || hHrtfCrend == NULL )
284 : {
285 98768 : return;
286 : }
287 :
288 14501 : for ( i = 0; i < MAX_INTERN_CHANNELS; i++ )
289 : {
290 40944 : for ( j = 0; j < BINAURAL_CHANNELS; j++ )
291 : {
292 27296 : if ( ( *hHrtfCrend )->pIndex_frequency_max[i][j] != NULL )
293 : {
294 25590 : free( ( *hHrtfCrend )->pIndex_frequency_max_dyn[i][j] );
295 : }
296 27296 : if ( ( *hHrtfCrend )->pOut_to_bin_re[i][j] != NULL )
297 : {
298 25590 : free( ( *hHrtfCrend )->pOut_to_bin_re_dyn[i][j] );
299 : }
300 27296 : if ( ( *hHrtfCrend )->pOut_to_bin_im[i][j] != NULL )
301 : {
302 25590 : free( ( *hHrtfCrend )->pOut_to_bin_im_dyn[i][j] );
303 : }
304 : }
305 : }
306 2559 : for ( j = 0; j < BINAURAL_CHANNELS; j++ )
307 : {
308 1706 : if ( ( *hHrtfCrend )->pIndex_frequency_max_diffuse[j] != NULL )
309 : {
310 1262 : free( ( *hHrtfCrend )->pIndex_frequency_max_diffuse_dyn[j] );
311 : }
312 1706 : if ( ( *hHrtfCrend )->pOut_to_bin_diffuse_re[j] != NULL )
313 : {
314 1262 : free( ( *hHrtfCrend )->pOut_to_bin_diffuse_re_dyn[j] );
315 : }
316 1706 : if ( ( *hHrtfCrend )->pOut_to_bin_diffuse_im[j] != NULL )
317 : {
318 1262 : free( ( *hHrtfCrend )->pOut_to_bin_diffuse_im_dyn[j] );
319 : }
320 : }
321 :
322 853 : free( *hHrtfCrend );
323 853 : *hHrtfCrend = NULL;
324 :
325 853 : return;
326 : }
327 :
328 :
329 : /*-----------------------------------------------------------------------*
330 : * ivas_HRTF_fastconv_binary_open()
331 : *
332 : * Allocate HRTF binary handle for FASTCONV renderer
333 : *-----------------------------------------------------------------------*/
334 :
335 2283 : ivas_error ivas_HRTF_fastconv_binary_open(
336 : HRTFS_FASTCONV **hHrtfFastConv )
337 : {
338 2283 : *hHrtfFastConv = (HRTFS_FASTCONV *) malloc( sizeof( HRTFS_FASTCONV ) );
339 2283 : if ( *hHrtfFastConv == NULL )
340 : {
341 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FASTCONV HRTF tables!" );
342 : }
343 :
344 2283 : ivas_init_binaural_hrtf( *hHrtfFastConv );
345 :
346 2283 : 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 99290 : void ivas_HRTF_fastconv_binary_close(
357 : HRTFS_FASTCONV **hHrtfFastConv )
358 : {
359 99290 : if ( hHrtfFastConv == NULL || *hHrtfFastConv == NULL )
360 : {
361 92029 : return;
362 : }
363 :
364 : /* Fastconv HRTF memories */
365 7261 : ivas_binaural_hrtf_close( hHrtfFastConv );
366 :
367 7261 : free( *hHrtfFastConv );
368 7261 : *hHrtfFastConv = NULL;
369 :
370 7261 : return;
371 : }
372 :
373 :
374 : /*-----------------------------------------------------------------------*
375 : * ivas_HRTF_parambin_binary_open()
376 : *
377 : * Allocate HRTF binary handle for parametric binauralizer
378 : *-----------------------------------------------------------------------*/
379 :
380 4896 : ivas_error ivas_HRTF_parambin_binary_open(
381 : HRTFS_PARAMBIN **hHrtfParambin )
382 : {
383 4896 : *hHrtfParambin = (HRTFS_PARAMBIN *) malloc( sizeof( HRTFS_PARAMBIN ) );
384 4896 : 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 4896 : set_c( (int8_t *) ( *hHrtfParambin ), 0, (int32_t) sizeof( HRTFS_PARAMBIN ) );
390 :
391 4896 : 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 100954 : void ivas_HRTF_parambin_binary_close(
402 : HRTFS_PARAMBIN **hHrtfParambin )
403 : {
404 100954 : if ( hHrtfParambin == NULL || *hHrtfParambin == NULL )
405 : {
406 84948 : return;
407 : }
408 :
409 16006 : free( *hHrtfParambin );
410 16006 : *hHrtfParambin = NULL;
411 :
412 16006 : return;
413 : }
414 :
415 : /*-----------------------------------------------------------------------*
416 : * ivas_HRTF_statistics_binary_open()
417 : *
418 : * Allocate HRTF binary handle for statistics handler
419 : *-----------------------------------------------------------------------*/
420 :
421 696 : ivas_error ivas_HRTF_statistics_binary_open(
422 : HRTFS_STATISTICS **hHrtfStatistics )
423 : {
424 696 : *hHrtfStatistics = (HRTFS_STATISTICS *) malloc( sizeof( HRTFS_STATISTICS ) );
425 696 : if ( *hHrtfStatistics == NULL )
426 : {
427 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for statistics HRTF tables!" );
428 : }
429 :
430 696 : set_c( (int8_t *) ( *hHrtfStatistics ), 0, (int32_t) sizeof( HRTFS_STATISTICS ) );
431 :
432 696 : 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 97734 : void ivas_HRTF_statistics_binary_close(
443 : HRTFS_STATISTICS **hHrtfStatistics )
444 : {
445 97734 : if ( hHrtfStatistics == NULL || *hHrtfStatistics == NULL )
446 : {
447 87427 : return;
448 : }
449 :
450 10307 : if ( ( hHrtfStatistics != NULL ) && ( *hHrtfStatistics != NULL ) && ( ( *hHrtfStatistics )->fromROM == FALSE ) )
451 : {
452 801 : if ( ( *hHrtfStatistics )->average_energy_l_dyn != NULL )
453 : {
454 801 : free( ( *hHrtfStatistics )->average_energy_l_dyn );
455 : }
456 801 : if ( ( *hHrtfStatistics )->average_energy_r_dyn != NULL )
457 : {
458 801 : free( ( *hHrtfStatistics )->average_energy_r_dyn );
459 : }
460 801 : if ( ( *hHrtfStatistics )->inter_aural_coherence_dyn != NULL )
461 : {
462 801 : free( ( *hHrtfStatistics )->inter_aural_coherence_dyn );
463 : }
464 : }
465 :
466 10307 : free( *hHrtfStatistics );
467 10307 : *hHrtfStatistics = NULL;
468 :
469 10307 : return;
470 : }
|