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 <assert.h>
34 : #include <stdint.h>
35 : #include "options.h"
36 : #include <math.h>
37 : #include "cnst.h"
38 : #include "prot.h"
39 : #include "ivas_prot.h"
40 : #include "ivas_prot_rend.h"
41 : #include "ivas_cnst.h"
42 : #include "ivas_rom_rend.h"
43 : #ifdef DEBUGGING
44 : #include "debug.h"
45 : #endif
46 : #include "wmc_auto.h"
47 :
48 :
49 : /*-------------------------------------------------------------------------
50 : * ivas_dirac_allocate_parameters()
51 : *
52 : * Allocate and initialize DirAC parameters
53 : *-------------------------------------------------------------------------*/
54 :
55 84968 : ivas_error ivas_dirac_allocate_parameters(
56 : SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom, /* i/o: common data for spatial parametric rendering */
57 : const int16_t params_flag /* i : set of parameters flag */
58 : )
59 : {
60 : int16_t i;
61 :
62 84968 : if ( params_flag == 1 )
63 : {
64 60546 : if ( ( hSpatParamRendCom->azimuth = (int16_t **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( int16_t * ) ) ) == NULL )
65 : {
66 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
67 : }
68 :
69 60546 : if ( ( hSpatParamRendCom->elevation = (int16_t **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( int16_t * ) ) ) == NULL )
70 : {
71 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
72 : }
73 :
74 60546 : if ( ( hSpatParamRendCom->diffuseness_vector = (float **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( float * ) ) ) == NULL )
75 : {
76 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
77 : }
78 :
79 60546 : if ( ( hSpatParamRendCom->energy_ratio1 = (float **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( float * ) ) ) == NULL )
80 : {
81 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
82 : }
83 :
84 60546 : if ( ( hSpatParamRendCom->spreadCoherence = (float **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( float * ) ) ) == NULL )
85 : {
86 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
87 : }
88 :
89 60546 : if ( ( hSpatParamRendCom->surroundingCoherence = (float **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( float * ) ) ) == NULL )
90 : {
91 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
92 : }
93 :
94 403940 : for ( i = 0; i < hSpatParamRendCom->dirac_md_buffer_length; i++ )
95 : {
96 343394 : if ( ( hSpatParamRendCom->azimuth[i] = (int16_t *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( int16_t ) ) ) == NULL )
97 : {
98 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
99 : }
100 343394 : set_s( hSpatParamRendCom->azimuth[i], 0, hSpatParamRendCom->num_freq_bands );
101 :
102 343394 : if ( ( hSpatParamRendCom->elevation[i] = (int16_t *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( int16_t ) ) ) == NULL )
103 : {
104 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
105 : }
106 343394 : set_s( hSpatParamRendCom->elevation[i], 0, hSpatParamRendCom->num_freq_bands );
107 :
108 343394 : if ( ( hSpatParamRendCom->diffuseness_vector[i] = (float *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( float ) ) ) == NULL )
109 : {
110 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
111 : }
112 343394 : set_f( hSpatParamRendCom->diffuseness_vector[i], 1.0f, hSpatParamRendCom->num_freq_bands );
113 :
114 343394 : if ( ( hSpatParamRendCom->energy_ratio1[i] = (float *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( float ) ) ) == NULL )
115 : {
116 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
117 : }
118 343394 : set_f( hSpatParamRendCom->energy_ratio1[i], 0.0f, hSpatParamRendCom->num_freq_bands );
119 :
120 343394 : if ( ( hSpatParamRendCom->spreadCoherence[i] = (float *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( float ) ) ) == NULL )
121 : {
122 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
123 : }
124 343394 : set_f( hSpatParamRendCom->spreadCoherence[i], 0.0f, hSpatParamRendCom->num_freq_bands );
125 :
126 343394 : if ( ( hSpatParamRendCom->surroundingCoherence[i] = (float *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( float ) ) ) == NULL )
127 : {
128 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
129 : }
130 343394 : set_f( hSpatParamRendCom->surroundingCoherence[i], 0.0f, hSpatParamRendCom->num_freq_bands );
131 : }
132 : }
133 24422 : else if ( params_flag == 2 )
134 : {
135 24422 : if ( ( hSpatParamRendCom->azimuth2 = (int16_t **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( int16_t * ) ) ) == NULL )
136 : {
137 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
138 : }
139 :
140 24422 : if ( ( hSpatParamRendCom->elevation2 = (int16_t **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( int16_t * ) ) ) == NULL )
141 : {
142 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
143 : }
144 :
145 24422 : if ( ( hSpatParamRendCom->energy_ratio2 = (float **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( float * ) ) ) == NULL )
146 : {
147 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
148 : }
149 :
150 24422 : if ( ( hSpatParamRendCom->spreadCoherence2 = (float **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( float * ) ) ) == NULL )
151 : {
152 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
153 : }
154 :
155 165288 : for ( i = 0; i < hSpatParamRendCom->dirac_md_buffer_length; i++ )
156 : {
157 140866 : if ( ( hSpatParamRendCom->azimuth2[i] = (int16_t *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( int16_t ) ) ) == NULL )
158 : {
159 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
160 : }
161 140866 : set_s( hSpatParamRendCom->azimuth2[i], 0, hSpatParamRendCom->num_freq_bands );
162 :
163 140866 : if ( ( hSpatParamRendCom->elevation2[i] = (int16_t *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( int16_t ) ) ) == NULL )
164 : {
165 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
166 : }
167 140866 : set_s( hSpatParamRendCom->elevation2[i], 0, hSpatParamRendCom->num_freq_bands );
168 :
169 140866 : if ( ( hSpatParamRendCom->energy_ratio2[i] = (float *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( float ) ) ) == NULL )
170 : {
171 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
172 : }
173 140866 : set_f( hSpatParamRendCom->energy_ratio2[i], 0.0f, hSpatParamRendCom->num_freq_bands );
174 :
175 140866 : if ( ( hSpatParamRendCom->spreadCoherence2[i] = (float *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( float ) ) ) == NULL )
176 : {
177 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
178 : }
179 140866 : set_f( hSpatParamRendCom->spreadCoherence2[i], 0.0f, hSpatParamRendCom->num_freq_bands );
180 : }
181 : }
182 :
183 84968 : return IVAS_ERR_OK;
184 : }
185 :
186 :
187 : /*-------------------------------------------------------------------------
188 : * ivas_spat_hSpatParamRendCom_config()
189 : *
190 : *
191 : *-------------------------------------------------------------------------*/
192 :
193 164279 : ivas_error ivas_spat_hSpatParamRendCom_config(
194 : SPAT_PARAM_REND_COMMON_DATA_HANDLE *hSpatParamRendCom_out, /* i/o: IVAS decoder structure */
195 : const DIRAC_CONFIG_FLAG flag_config_inp, /* i/ : Flag determining if we open or reconfigure the DirAC decoder */
196 : const int16_t dec_param_estim_flag,
197 : const IVAS_FORMAT ivas_format,
198 : const MC_MODE mc_mode,
199 : const int32_t output_Fs,
200 : const int16_t hodirac_flag,
201 : const int16_t masa_ext_rend_flag )
202 : {
203 : ivas_error error;
204 : int16_t map_idx;
205 : DIRAC_CONFIG_FLAG flag_config;
206 : SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
207 :
208 164279 : flag_config = ( flag_config_inp == DIRAC_RECONFIGURE_MODE ) ? DIRAC_RECONFIGURE : flag_config_inp;
209 164279 : error = IVAS_ERR_OK;
210 :
211 164279 : hSpatParamRendCom = NULL;
212 :
213 164279 : if ( flag_config == DIRAC_RECONFIGURE )
214 : {
215 106462 : hSpatParamRendCom = *hSpatParamRendCom_out;
216 : }
217 57817 : else if ( flag_config == DIRAC_OPEN )
218 : {
219 : /*-----------------------------------------------------------------*
220 : * prepare library opening
221 : *-----------------------------------------------------------------*/
222 :
223 57817 : if ( ( hSpatParamRendCom = (SPAT_PARAM_REND_COMMON_DATA_HANDLE) malloc( sizeof( SPAT_PARAM_REND_COMMON_DATA ) ) ) == NULL )
224 : {
225 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC meta\n" ) );
226 : }
227 :
228 57817 : *hSpatParamRendCom_out = hSpatParamRendCom;
229 : }
230 :
231 164279 : if ( flag_config == DIRAC_OPEN )
232 : {
233 57817 : hSpatParamRendCom->slot_size = (int16_t) ( ( output_Fs / FRAMES_PER_SEC ) / CLDFB_NO_COL_MAX );
234 57817 : set_s( hSpatParamRendCom->subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
235 57817 : set_s( hSpatParamRendCom->subframe_nbslots, JBM_CLDFB_SLOTS_IN_SUBFRAME, DEFAULT_JBM_SUBFRAMES_5MS );
236 57817 : hSpatParamRendCom->nb_subframes = DEFAULT_JBM_SUBFRAMES_5MS;
237 57817 : hSpatParamRendCom->subframes_rendered = 0;
238 57817 : hSpatParamRendCom->slots_rendered = 0;
239 57817 : hSpatParamRendCom->num_slots = DEFAULT_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME;
240 57817 : hSpatParamRendCom->num_freq_bands = (int16_t) ( output_Fs * INV_CLDFB_BANDWIDTH + 0.5f );
241 57817 : hSpatParamRendCom->numSimultaneousDirections = 0;
242 57817 : hSpatParamRendCom->numParametricDirections = 0;
243 57817 : hSpatParamRendCom->numIsmDirections = 0;
244 : }
245 :
246 : /*-----------------------------------------------------------------*
247 : * set input parameters
248 : *-----------------------------------------------------------------*/
249 :
250 164279 : if ( ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT ) && flag_config == DIRAC_RECONFIGURE )
251 : {
252 22765 : if ( hodirac_flag && hSpatParamRendCom->azimuth2 == NULL )
253 : {
254 2712 : if ( ( error = ivas_dirac_allocate_parameters( hSpatParamRendCom, 2 ) ) != IVAS_ERR_OK )
255 : {
256 0 : return error;
257 : }
258 : }
259 20053 : else if ( !hodirac_flag && hSpatParamRendCom->azimuth2 != NULL )
260 : {
261 2683 : ivas_dirac_deallocate_parameters( hSpatParamRendCom, 2 );
262 : }
263 : }
264 :
265 164279 : if ( flag_config == DIRAC_OPEN )
266 : {
267 57817 : hSpatParamRendCom->dirac_md_buffer_length = 0;
268 57817 : hSpatParamRendCom->dirac_bs_md_write_idx = 0;
269 57817 : hSpatParamRendCom->dirac_read_idx = 0;
270 57817 : if ( mc_mode == MC_MODE_MCMASA || masa_ext_rend_flag == 1 )
271 : {
272 7212 : hSpatParamRendCom->dirac_md_buffer_length = MAX_PARAM_SPATIAL_SUBFRAMES;
273 :
274 7212 : set_s( hSpatParamRendCom->render_to_md_map, 0, MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME );
275 36060 : for ( map_idx = 0; map_idx < DEFAULT_JBM_SUBFRAMES_5MS; map_idx++ )
276 : {
277 28848 : hSpatParamRendCom->render_to_md_map[map_idx] = map_idx;
278 : }
279 : }
280 50605 : else if ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT )
281 : {
282 15434 : hSpatParamRendCom->dirac_md_buffer_length = MAX_PARAM_SPATIAL_SUBFRAMES + DELAY_MASA_PARAM_DEC_SFR;
283 15434 : hSpatParamRendCom->dirac_bs_md_write_idx = DELAY_MASA_PARAM_DEC_SFR;
284 :
285 15434 : set_s( hSpatParamRendCom->render_to_md_map, 0, MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME );
286 77170 : for ( map_idx = 0; map_idx < DEFAULT_JBM_SUBFRAMES_5MS; map_idx++ )
287 : {
288 61736 : hSpatParamRendCom->render_to_md_map[map_idx] = map_idx;
289 : }
290 : }
291 : else
292 : {
293 : int16_t num_slots_in_subfr;
294 35171 : num_slots_in_subfr = dec_param_estim_flag ? CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES : 1;
295 35171 : hSpatParamRendCom->dirac_md_buffer_length = ( MAX_PARAM_SPATIAL_SUBFRAMES + DELAY_DIRAC_PARAM_DEC_SFR );
296 35171 : hSpatParamRendCom->dirac_bs_md_write_idx = DELAY_DIRAC_PARAM_DEC_SFR;
297 35171 : hSpatParamRendCom->dirac_read_idx = 0;
298 :
299 35171 : set_s( hSpatParamRendCom->render_to_md_map, 0, MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME );
300 466687 : for ( map_idx = 0; map_idx < DEFAULT_JBM_SUBFRAMES_5MS * num_slots_in_subfr; map_idx++ )
301 : {
302 431516 : hSpatParamRendCom->render_to_md_map[map_idx] = hSpatParamRendCom->dirac_read_idx + map_idx / num_slots_in_subfr;
303 : }
304 : }
305 :
306 57817 : if ( ( error = ivas_dirac_allocate_parameters( hSpatParamRendCom, 1 ) ) != IVAS_ERR_OK )
307 : {
308 0 : return error;
309 : }
310 :
311 57817 : if ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT || ( ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT ) && hodirac_flag ) )
312 : {
313 18981 : if ( ( error = ivas_dirac_allocate_parameters( hSpatParamRendCom, 2 ) ) != IVAS_ERR_OK )
314 : {
315 0 : return error;
316 : }
317 : }
318 : else
319 : {
320 38836 : hSpatParamRendCom->azimuth2 = NULL;
321 38836 : hSpatParamRendCom->elevation2 = NULL;
322 38836 : hSpatParamRendCom->energy_ratio2 = NULL;
323 38836 : hSpatParamRendCom->spreadCoherence2 = NULL;
324 : }
325 : }
326 :
327 164279 : return error;
328 : }
329 :
330 :
331 : /*-------------------------------------------------------------------------
332 : * ivas_spat_hSpatParamRendCom_close()
333 : *
334 : *
335 : *-------------------------------------------------------------------------*/
336 :
337 93148 : void ivas_spat_hSpatParamRendCom_close(
338 : SPAT_PARAM_REND_COMMON_DATA_HANDLE *hSpatParamRendCom_out )
339 : {
340 93148 : if ( hSpatParamRendCom_out == NULL || *hSpatParamRendCom_out == NULL )
341 : {
342 35331 : return;
343 : }
344 :
345 57817 : ivas_dirac_deallocate_parameters( *hSpatParamRendCom_out, 1 );
346 57817 : ivas_dirac_deallocate_parameters( *hSpatParamRendCom_out, 2 );
347 :
348 57817 : free( *hSpatParamRendCom_out );
349 57817 : *hSpatParamRendCom_out = NULL;
350 :
351 57817 : return;
352 : }
353 :
354 :
355 : /*-------------------------------------------------------------------------
356 : * ivas_dirac_rend_close()
357 : *
358 : *
359 : *-------------------------------------------------------------------------*/
360 :
361 167888 : void ivas_dirac_rend_close(
362 : DIRAC_REND_HANDLE *hDirACRend_out )
363 : {
364 : int16_t i, j;
365 : DIRAC_REND_HANDLE hDirACRend;
366 :
367 167888 : if ( hDirACRend_out == NULL || *hDirACRend_out == NULL )
368 : {
369 125064 : return;
370 : }
371 :
372 42824 : hDirACRend = *hDirACRend_out;
373 :
374 : /* close Output synthesis sub-module */
375 42824 : ivas_dirac_dec_output_synthesis_close( hDirACRend );
376 :
377 : /* close Decorrelator sub-module */
378 42824 : if ( hDirACRend->proto_signal_decorr_on )
379 : {
380 40192 : ivas_dirac_dec_decorr_close( &hDirACRend->h_freq_domain_decorr_ap_params, &hDirACRend->h_freq_domain_decorr_ap_state );
381 : }
382 :
383 : /* Params */
384 :
385 : /* free frequency axis buffer */
386 42824 : if ( hDirACRend->frequency_axis != NULL )
387 : {
388 42824 : free( hDirACRend->frequency_axis );
389 42824 : hDirACRend->frequency_axis = NULL;
390 : }
391 :
392 42824 : if ( hDirACRend->diffuse_response_function != NULL )
393 : {
394 42824 : free( hDirACRend->diffuse_response_function );
395 42824 : hDirACRend->diffuse_response_function = NULL;
396 : }
397 :
398 42824 : if ( hDirACRend->hoa_encoder != NULL )
399 : {
400 2524 : free( hDirACRend->hoa_encoder );
401 2524 : hDirACRend->hoa_encoder = NULL;
402 : }
403 :
404 : /* prototype indexing */
405 42824 : if ( hDirACRend->proto_index_dir != NULL )
406 : {
407 42824 : free( hDirACRend->proto_index_dir );
408 42824 : hDirACRend->proto_index_dir = NULL;
409 : }
410 :
411 42824 : if ( hDirACRend->proto_index_diff != NULL )
412 : {
413 42824 : free( hDirACRend->proto_index_diff );
414 42824 : hDirACRend->proto_index_dir = NULL;
415 : }
416 :
417 : /* States */
418 :
419 : /* free prototype signal buffers */
420 42824 : if ( hDirACRend->proto_frame_f != NULL )
421 : {
422 14159 : free( hDirACRend->proto_frame_f );
423 14159 : hDirACRend->proto_frame_f = NULL;
424 : }
425 :
426 171296 : for ( i = 0; i < DIRAC_NUM_DIMS; i++ )
427 : {
428 4239576 : for ( j = 0; j < DIRAC_NO_COL_AVG_DIFF; j++ )
429 : {
430 4111104 : if ( hDirACRend->buffer_intensity_real[i][j] != NULL )
431 : {
432 2465376 : free( hDirACRend->buffer_intensity_real[i][j] );
433 2465376 : hDirACRend->buffer_intensity_real[i][j] = NULL;
434 : }
435 : }
436 : }
437 42824 : if ( hDirACRend->buffer_energy != NULL )
438 : {
439 25681 : free( hDirACRend->buffer_energy );
440 25681 : hDirACRend->buffer_energy = NULL;
441 : }
442 :
443 42824 : if ( hDirACRend->masa_stereo_type_detect != NULL )
444 : {
445 1578 : free( hDirACRend->masa_stereo_type_detect );
446 1578 : hDirACRend->masa_stereo_type_detect = NULL;
447 : }
448 :
449 42824 : ivas_dirac_free_mem( &( hDirACRend->stack_mem ) );
450 :
451 42824 : free( *hDirACRend_out );
452 42824 : *hDirACRend_out = NULL;
453 :
454 42824 : return;
455 : }
456 :
457 :
458 : /*-------------------------------------------------------------------------
459 : * ivas_dirac_deallocate_parameters()
460 : *
461 : * Deallocate DirAC parameters
462 : *-------------------------------------------------------------------------*/
463 :
464 123775 : void ivas_dirac_deallocate_parameters(
465 : SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom, /* i/o: common data for spatial parametric rendering */
466 : const int16_t params_flag /* i : set of parameters flag */
467 : )
468 : {
469 : int16_t i;
470 : int16_t md_buffer_length;
471 :
472 123775 : if ( hSpatParamRendCom == NULL )
473 : {
474 0 : return;
475 : }
476 :
477 123775 : md_buffer_length = hSpatParamRendCom->dirac_md_buffer_length;
478 :
479 123775 : if ( params_flag == 1 )
480 : {
481 60546 : if ( hSpatParamRendCom->azimuth != NULL )
482 : {
483 403940 : for ( i = 0; i < md_buffer_length; i++ )
484 : {
485 343394 : if ( hSpatParamRendCom->azimuth[i] != NULL )
486 : {
487 343394 : free( hSpatParamRendCom->azimuth[i] );
488 343394 : hSpatParamRendCom->azimuth[i] = NULL;
489 : }
490 : }
491 :
492 60546 : free( hSpatParamRendCom->azimuth );
493 60546 : hSpatParamRendCom->azimuth = NULL;
494 : }
495 :
496 60546 : if ( hSpatParamRendCom->elevation != NULL )
497 : {
498 403940 : for ( i = 0; i < md_buffer_length; i++ )
499 : {
500 343394 : if ( hSpatParamRendCom->elevation[i] != NULL )
501 : {
502 343394 : free( hSpatParamRendCom->elevation[i] );
503 343394 : hSpatParamRendCom->elevation[i] = NULL;
504 : }
505 : }
506 :
507 60546 : free( hSpatParamRendCom->elevation );
508 60546 : hSpatParamRendCom->elevation = NULL;
509 : }
510 :
511 60546 : if ( hSpatParamRendCom->energy_ratio1 != NULL )
512 : {
513 403940 : for ( i = 0; i < md_buffer_length; i++ )
514 : {
515 343394 : if ( hSpatParamRendCom->energy_ratio1[i] != NULL )
516 : {
517 343394 : free( hSpatParamRendCom->energy_ratio1[i] );
518 343394 : hSpatParamRendCom->energy_ratio1[i] = NULL;
519 : }
520 : }
521 60546 : free( hSpatParamRendCom->energy_ratio1 );
522 60546 : hSpatParamRendCom->energy_ratio1 = NULL;
523 : }
524 :
525 60546 : if ( hSpatParamRendCom->diffuseness_vector != NULL )
526 : {
527 403940 : for ( i = 0; i < md_buffer_length; i++ )
528 : {
529 343394 : if ( hSpatParamRendCom->diffuseness_vector[i] != NULL )
530 : {
531 343394 : free( hSpatParamRendCom->diffuseness_vector[i] );
532 343394 : hSpatParamRendCom->diffuseness_vector[i] = NULL;
533 : }
534 : }
535 :
536 60546 : free( hSpatParamRendCom->diffuseness_vector );
537 60546 : hSpatParamRendCom->diffuseness_vector = NULL;
538 : }
539 :
540 60546 : if ( hSpatParamRendCom->spreadCoherence != NULL )
541 : {
542 403940 : for ( i = 0; i < md_buffer_length; i++ )
543 : {
544 343394 : if ( hSpatParamRendCom->spreadCoherence[i] != NULL )
545 : {
546 343394 : free( hSpatParamRendCom->spreadCoherence[i] );
547 343394 : hSpatParamRendCom->spreadCoherence[i] = NULL;
548 : }
549 : }
550 60546 : free( hSpatParamRendCom->spreadCoherence );
551 60546 : hSpatParamRendCom->spreadCoherence = NULL;
552 : }
553 :
554 60546 : if ( hSpatParamRendCom->surroundingCoherence != NULL )
555 : {
556 403940 : for ( i = 0; i < md_buffer_length; i++ )
557 : {
558 343394 : if ( hSpatParamRendCom->surroundingCoherence[i] != NULL )
559 : {
560 343394 : free( hSpatParamRendCom->surroundingCoherence[i] );
561 343394 : hSpatParamRendCom->surroundingCoherence[i] = NULL;
562 : }
563 : }
564 60546 : free( hSpatParamRendCom->surroundingCoherence );
565 60546 : hSpatParamRendCom->surroundingCoherence = NULL;
566 : }
567 : }
568 63229 : else if ( params_flag == 2 )
569 : {
570 63229 : if ( hSpatParamRendCom->azimuth2 != NULL )
571 : {
572 165288 : for ( i = 0; i < md_buffer_length; i++ )
573 : {
574 140866 : if ( hSpatParamRendCom->azimuth2[i] != NULL )
575 : {
576 140866 : free( hSpatParamRendCom->azimuth2[i] );
577 140866 : hSpatParamRendCom->azimuth2[i] = NULL;
578 : }
579 : }
580 24422 : free( hSpatParamRendCom->azimuth2 );
581 24422 : hSpatParamRendCom->azimuth2 = NULL;
582 : }
583 :
584 63229 : if ( hSpatParamRendCom->elevation2 != NULL )
585 : {
586 165288 : for ( i = 0; i < md_buffer_length; i++ )
587 : {
588 140866 : if ( hSpatParamRendCom->elevation2[i] != NULL )
589 : {
590 140866 : free( hSpatParamRendCom->elevation2[i] );
591 140866 : hSpatParamRendCom->elevation2[i] = NULL;
592 : }
593 : }
594 24422 : free( hSpatParamRendCom->elevation2 );
595 24422 : hSpatParamRendCom->elevation2 = NULL;
596 : }
597 :
598 63229 : if ( hSpatParamRendCom->energy_ratio2 != NULL )
599 : {
600 165288 : for ( i = 0; i < md_buffer_length; i++ )
601 : {
602 140866 : if ( hSpatParamRendCom->energy_ratio2[i] != NULL )
603 : {
604 140866 : free( hSpatParamRendCom->energy_ratio2[i] );
605 140866 : hSpatParamRendCom->energy_ratio2[i] = NULL;
606 : }
607 : }
608 24422 : free( hSpatParamRendCom->energy_ratio2 );
609 24422 : hSpatParamRendCom->energy_ratio2 = NULL;
610 : }
611 :
612 63229 : if ( hSpatParamRendCom->spreadCoherence2 != NULL )
613 : {
614 165288 : for ( i = 0; i < md_buffer_length; i++ )
615 : {
616 140866 : if ( hSpatParamRendCom->spreadCoherence2[i] != NULL )
617 : {
618 140866 : free( hSpatParamRendCom->spreadCoherence2[i] );
619 140866 : hSpatParamRendCom->spreadCoherence2[i] = NULL;
620 : }
621 : }
622 24422 : free( hSpatParamRendCom->spreadCoherence2 );
623 24422 : hSpatParamRendCom->spreadCoherence2 = NULL;
624 : }
625 : }
626 :
627 123775 : return;
628 : }
629 :
630 :
631 : /*-------------------------------------------------------------------------
632 : * ivas_dirac_alloc_mem()
633 : *
634 : * Allocate stack memory for DirAC renderer
635 : *------------------------------------------------------------------------*/
636 :
637 88607 : ivas_error ivas_dirac_alloc_mem(
638 : DIRAC_REND_HANDLE hDirACRend,
639 : const RENDERER_TYPE renderer_type,
640 : const int16_t num_freq_bands,
641 : DIRAC_DEC_STACK_MEM_HANDLE hDirAC_mem,
642 : const int16_t hodirac_flag )
643 : {
644 : int16_t num_freq_bands_diff, size;
645 : int16_t size_ho;
646 : int16_t size_pf;
647 : int16_t num_outputs_dir, num_outputs_diff;
648 : int16_t num_protos_dir;
649 :
650 88607 : num_protos_dir = hDirACRend->num_protos_dir;
651 :
652 88607 : num_freq_bands_diff = hDirACRend->h_output_synthesis_psd_params.max_band_decorr;
653 :
654 88607 : num_outputs_dir = hDirACRend->num_outputs_dir;
655 88607 : num_outputs_diff = hDirACRend->num_outputs_diff;
656 :
657 88607 : size = num_freq_bands * num_outputs_dir;
658 88607 : if ( hodirac_flag )
659 : {
660 7528 : size_ho = size * DIRAC_HO_NUMSECTORS;
661 7528 : size_pf = num_freq_bands * DIRAC_HO_NUMSECTORS;
662 : }
663 : else
664 : {
665 81079 : size_ho = size;
666 81079 : size_pf = num_freq_bands;
667 : }
668 :
669 : /* PSD related buffers */
670 88607 : hDirAC_mem->cy_auto_dir_smooth = NULL;
671 88607 : hDirAC_mem->proto_power_smooth = NULL;
672 88607 : hDirAC_mem->proto_power_diff_smooth = NULL;
673 88607 : hDirAC_mem->direct_responses_square = NULL;
674 88607 : hDirAC_mem->frame_dec_f = NULL;
675 88607 : if ( hDirACRend->synthesisConf != DIRAC_SYNTHESIS_GAIN_SHD )
676 : {
677 45130 : if ( ( hDirAC_mem->cy_auto_dir_smooth = (float *) malloc( sizeof( float ) * size ) ) == NULL )
678 : {
679 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
680 : }
681 45130 : set_zero( hDirAC_mem->cy_auto_dir_smooth, size );
682 :
683 45130 : if ( ( hDirAC_mem->proto_power_smooth = (float *) malloc( sizeof( float ) * size ) ) == NULL )
684 : {
685 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
686 : }
687 45130 : set_zero( hDirAC_mem->proto_power_smooth, size );
688 :
689 45130 : if ( ( hDirAC_mem->proto_power_diff_smooth = (float *) malloc( sizeof( float ) * size ) ) == NULL )
690 : {
691 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
692 : }
693 45130 : set_zero( hDirAC_mem->proto_power_diff_smooth, size );
694 :
695 45130 : if ( ( hDirAC_mem->direct_responses_square = (float *) malloc( sizeof( float ) * size ) ) == NULL )
696 : {
697 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
698 : }
699 45130 : set_zero( hDirAC_mem->direct_responses_square, size );
700 45130 : if ( hDirACRend->proto_signal_decorr_on && ( renderer_type != RENDERER_BINAURAL_PARAMETRIC && renderer_type != RENDERER_BINAURAL_PARAMETRIC_ROOM && renderer_type != RENDERER_STEREO_PARAMETRIC ) )
701 : {
702 40972 : if ( ( hDirAC_mem->frame_dec_f = (float *) malloc( sizeof( float ) * 2 * num_outputs_diff * num_freq_bands ) ) == NULL )
703 : {
704 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
705 : }
706 : }
707 : }
708 88607 : hDirACRend->h_output_synthesis_psd_state.proto_power_smooth = hDirAC_mem->proto_power_smooth;
709 88607 : hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth = hDirAC_mem->proto_power_diff_smooth;
710 88607 : hDirACRend->h_output_synthesis_psd_state.cy_auto_dir_smooth = hDirAC_mem->cy_auto_dir_smooth;
711 88607 : hDirACRend->h_output_synthesis_psd_state.direct_responses_square = hDirAC_mem->direct_responses_square;
712 :
713 : /* Target and smoothed nrg factors/gains */
714 88607 : if ( ( hDirAC_mem->cy_cross_dir_smooth = (float *) malloc( sizeof( float ) * size_ho ) ) == NULL )
715 : {
716 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
717 : }
718 88607 : set_zero( hDirAC_mem->cy_cross_dir_smooth, size );
719 :
720 88607 : if ( hDirACRend->synthesisConf != DIRAC_SYNTHESIS_GAIN_SHD )
721 : {
722 45130 : if ( ( hDirAC_mem->cy_auto_diff_smooth = (float *) malloc( sizeof( float ) * size ) ) == NULL )
723 : {
724 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
725 : }
726 45130 : set_zero( hDirAC_mem->cy_auto_diff_smooth, size );
727 : }
728 : else
729 : {
730 43477 : if ( ( hDirAC_mem->cy_auto_diff_smooth = (float *) malloc( sizeof( float ) * num_outputs_diff * num_freq_bands_diff ) ) == NULL )
731 : {
732 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
733 : }
734 43477 : set_zero( hDirAC_mem->cy_auto_diff_smooth, num_outputs_diff * num_freq_bands_diff );
735 : }
736 88607 : hDirACRend->h_output_synthesis_psd_state.cy_cross_dir_smooth = hDirAC_mem->cy_cross_dir_smooth;
737 88607 : hDirACRend->h_output_synthesis_psd_state.cy_auto_diff_smooth = hDirAC_mem->cy_auto_diff_smooth;
738 :
739 : /*Responses (gains/factors)*/
740 88607 : if ( ( hDirAC_mem->direct_responses = (float *) malloc( sizeof( float ) * size_ho ) ) == NULL )
741 : {
742 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
743 : }
744 88607 : set_zero( hDirAC_mem->direct_responses, size );
745 :
746 :
747 88607 : hDirACRend->h_output_synthesis_psd_state.direct_responses = hDirAC_mem->direct_responses;
748 :
749 : /* Prototypes */
750 88607 : hDirAC_mem->proto_direct_buffer_f = NULL;
751 88607 : hDirAC_mem->proto_diffuse_buffer_f = NULL;
752 88607 : if ( renderer_type != RENDERER_BINAURAL_PARAMETRIC && renderer_type != RENDERER_BINAURAL_PARAMETRIC_ROOM && renderer_type != RENDERER_STEREO_PARAMETRIC )
753 : {
754 88607 : if ( ( hDirAC_mem->proto_direct_buffer_f = (float *) malloc( sizeof( float ) * 2 * MAX_PARAM_SPATIAL_SUBFRAMES * num_protos_dir * num_freq_bands ) ) == NULL )
755 : {
756 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
757 : }
758 :
759 88607 : if ( hDirACRend->proto_signal_decorr_on )
760 : {
761 84449 : if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_SHD )
762 : {
763 12048 : if ( ( hDirAC_mem->proto_diffuse_buffer_f = (float *) malloc( sizeof( float ) * 2 * MAX_PARAM_SPATIAL_SUBFRAMES * size ) ) == NULL )
764 : {
765 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
766 : }
767 : }
768 : else
769 : {
770 72401 : if ( ( hDirAC_mem->proto_diffuse_buffer_f = (float *) malloc( sizeof( float ) * 2 * MAX_PARAM_SPATIAL_SUBFRAMES * num_outputs_diff * num_freq_bands ) ) == NULL )
771 : {
772 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
773 : }
774 : }
775 : }
776 : }
777 88607 : hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f = hDirAC_mem->proto_direct_buffer_f;
778 88607 : hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f = hDirAC_mem->proto_diffuse_buffer_f;
779 :
780 : /* Gains/power factors*/
781 88607 : hDirAC_mem->direct_power_factor = NULL;
782 88607 : hDirAC_mem->diffuse_power_factor = NULL;
783 :
784 88607 : if ( renderer_type != RENDERER_BINAURAL_PARAMETRIC && renderer_type != RENDERER_BINAURAL_PARAMETRIC_ROOM && renderer_type != RENDERER_STEREO_PARAMETRIC )
785 : {
786 88607 : if ( ( hDirAC_mem->direct_power_factor = (float *) malloc( sizeof( float ) * size_pf ) ) == NULL )
787 : {
788 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
789 : }
790 88607 : if ( ( hDirAC_mem->diffuse_power_factor = (float *) malloc( sizeof( float ) * size_pf ) ) == NULL )
791 : {
792 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
793 : }
794 : }
795 :
796 88607 : hDirACRend->h_output_synthesis_psd_state.direct_power_factor = hDirAC_mem->direct_power_factor;
797 88607 : hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor = hDirAC_mem->diffuse_power_factor;
798 :
799 88607 : hDirAC_mem->reference_power = NULL;
800 88607 : hDirAC_mem->onset_filter = NULL;
801 88607 : if ( hDirACRend->synthesisConf != DIRAC_SYNTHESIS_GAIN_SHD )
802 : {
803 45130 : if ( renderer_type != RENDERER_BINAURAL_PARAMETRIC && renderer_type != RENDERER_BINAURAL_PARAMETRIC_ROOM && renderer_type != RENDERER_STEREO_PARAMETRIC )
804 : {
805 45130 : if ( ( hDirAC_mem->reference_power = (float *) malloc( sizeof( float ) * 2 * num_freq_bands ) ) == NULL )
806 : {
807 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
808 : }
809 45130 : if ( hDirACRend->proto_signal_decorr_on )
810 : {
811 40972 : if ( ( hDirAC_mem->onset_filter = (float *) malloc( sizeof( float ) * num_outputs_diff * num_freq_bands ) ) == NULL )
812 : {
813 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
814 : }
815 : }
816 : }
817 : }
818 : else
819 : {
820 43477 : if ( num_protos_dir > 2 )
821 : {
822 43003 : if ( ( hDirAC_mem->reference_power = (float *) malloc( sizeof( float ) * 5 * num_freq_bands ) ) == NULL )
823 : {
824 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
825 : }
826 : }
827 :
828 43477 : if ( hDirACRend->proto_signal_decorr_on )
829 : {
830 43477 : if ( ( hDirAC_mem->onset_filter = (float *) malloc( sizeof( float ) * 2 * num_freq_bands ) ) == NULL )
831 : {
832 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
833 : }
834 : }
835 : }
836 :
837 88607 : return IVAS_ERR_OK;
838 : }
839 :
840 :
841 : /*-------------------------------------------------------------------------
842 : * ivas_dirac_free_mem()
843 : *
844 : *
845 : *-------------------------------------------------------------------------*/
846 :
847 88607 : void ivas_dirac_free_mem(
848 : DIRAC_DEC_STACK_MEM_HANDLE hDirAC_mem )
849 : {
850 88607 : if ( hDirAC_mem->cy_auto_dir_smooth != NULL )
851 : {
852 45130 : free( hDirAC_mem->cy_auto_dir_smooth );
853 : }
854 88607 : if ( hDirAC_mem->proto_power_smooth != NULL )
855 : {
856 45130 : free( hDirAC_mem->proto_power_smooth );
857 : }
858 88607 : if ( hDirAC_mem->proto_power_diff_smooth != NULL )
859 : {
860 45130 : free( hDirAC_mem->proto_power_diff_smooth );
861 : }
862 88607 : if ( hDirAC_mem->direct_responses_square != NULL )
863 : {
864 45130 : free( hDirAC_mem->direct_responses_square );
865 : }
866 88607 : if ( hDirAC_mem->frame_dec_f != NULL )
867 : {
868 40972 : free( hDirAC_mem->frame_dec_f );
869 : }
870 88607 : if ( hDirAC_mem->cy_cross_dir_smooth != NULL )
871 : {
872 88607 : free( hDirAC_mem->cy_cross_dir_smooth );
873 : }
874 88607 : if ( hDirAC_mem->cy_auto_diff_smooth != NULL )
875 : {
876 88607 : free( hDirAC_mem->cy_auto_diff_smooth );
877 : }
878 88607 : if ( hDirAC_mem->direct_responses != NULL )
879 : {
880 88607 : free( hDirAC_mem->direct_responses );
881 : }
882 88607 : if ( hDirAC_mem->proto_direct_buffer_f != NULL )
883 : {
884 88607 : free( hDirAC_mem->proto_direct_buffer_f );
885 : }
886 88607 : if ( hDirAC_mem->proto_diffuse_buffer_f != NULL )
887 : {
888 84449 : free( hDirAC_mem->proto_diffuse_buffer_f );
889 : }
890 88607 : if ( hDirAC_mem->direct_power_factor != NULL )
891 : {
892 88607 : free( hDirAC_mem->direct_power_factor );
893 : }
894 88607 : if ( hDirAC_mem->diffuse_power_factor != NULL )
895 : {
896 88607 : free( hDirAC_mem->diffuse_power_factor );
897 : }
898 88607 : if ( hDirAC_mem->reference_power != NULL )
899 : {
900 88133 : free( hDirAC_mem->reference_power );
901 : }
902 88607 : if ( hDirAC_mem->onset_filter != NULL )
903 : {
904 84449 : free( hDirAC_mem->onset_filter );
905 : }
906 :
907 88607 : return;
908 : }
909 :
910 :
911 : /*-------------------------------------------------------------------------
912 : * compute_hoa_encoder_mtx()
913 : *
914 : *
915 : *------------------------------------------------------------------------*/
916 :
917 12583 : void compute_hoa_encoder_mtx(
918 : const float *azimuth,
919 : const float *elevation,
920 : float *response,
921 : const int16_t num_responses,
922 : const int16_t ambisonics_order )
923 : {
924 : int16_t k, num_sh;
925 :
926 12583 : num_sh = ivas_sba_get_nchan( ambisonics_order, 0 );
927 :
928 113336 : for ( k = 0; k < num_responses; k++ )
929 : {
930 100753 : ivas_dirac_dec_get_response( (const int16_t) azimuth[k], (const int16_t) elevation[k], &response[k * num_sh], ambisonics_order );
931 : }
932 :
933 12583 : return;
934 : }
935 :
936 :
937 : /*-------------------------------------------------------------------------
938 : * ivas_dirac_dec_get_frequency_axis()
939 : *
940 : * DirAC decoding initialization
941 : *------------------------------------------------------------------------*/
942 :
943 85992 : void ivas_dirac_dec_get_frequency_axis(
944 : float *frequency_axis,
945 : const int32_t output_Fs,
946 : const int16_t num_freq_bands )
947 : {
948 : int16_t k;
949 : float const_part;
950 :
951 : /* calc cldfb frequency axis */
952 85992 : const_part = (float) output_Fs / ( 2.0f * (float) num_freq_bands );
953 3903432 : for ( k = 0; k < num_freq_bands; ++k )
954 : {
955 3817440 : frequency_axis[k] = ( (float) k + 0.5f ) * const_part;
956 : }
957 :
958 85992 : return;
959 : }
960 :
961 :
962 : /*-------------------------------------------------------------------------
963 : * initDiffuseResponses()
964 : *
965 : *
966 : *-------------------------------------------------------------------------*/
967 :
968 88607 : void initDiffuseResponses(
969 : float *diffuse_response_function,
970 : const int16_t num_channels,
971 : const AUDIO_CONFIG output_config,
972 : const IVAS_OUTPUT_SETUP hOutSetup,
973 : const int16_t ambisonics_order,
974 : const IVAS_FORMAT ivas_format,
975 : int16_t *num_ele_spk_no_diffuse_rendering,
976 : const AUDIO_CONFIG transport_config )
977 : {
978 : int16_t i, l, k, idx, num_horizontal_speakers;
979 88607 : *num_ele_spk_no_diffuse_rendering = 0;
980 :
981 88607 : if ( output_config == IVAS_AUDIO_CONFIG_MONO )
982 : {
983 1942 : diffuse_response_function[0] = 1.0f;
984 1942 : diffuse_response_function[1] = inv_sqrt( 3.0f );
985 : }
986 86665 : else if ( !( output_config == IVAS_AUDIO_CONFIG_FOA || output_config == IVAS_AUDIO_CONFIG_HOA2 || output_config == IVAS_AUDIO_CONFIG_HOA3 ) )
987 : {
988 : /* set diffuse response function */
989 31140 : if ( ivas_format == MC_FORMAT && ( transport_config == IVAS_AUDIO_CONFIG_5_1 || transport_config == IVAS_AUDIO_CONFIG_7_1 ) && output_config == IVAS_AUDIO_CONFIG_5_1_4 )
990 : {
991 36 : num_horizontal_speakers = num_channels - NUM_ELEVATED_SPEAKERS;
992 :
993 36 : mvr2r( diffuse_response_CICP6, diffuse_response_function, num_horizontal_speakers );
994 36 : set_zero( &diffuse_response_function[num_horizontal_speakers], NUM_ELEVATED_SPEAKERS );
995 36 : *num_ele_spk_no_diffuse_rendering = NUM_ELEVATED_SPEAKERS;
996 : }
997 31104 : else if ( ivas_format == MC_FORMAT && ( transport_config == IVAS_AUDIO_CONFIG_5_1 || transport_config == IVAS_AUDIO_CONFIG_7_1 ) && output_config == IVAS_AUDIO_CONFIG_7_1_4 )
998 : {
999 960 : num_horizontal_speakers = num_channels - NUM_ELEVATED_SPEAKERS;
1000 :
1001 960 : set_f( diffuse_response_function, sqrtf( 1.f / ( (float) num_horizontal_speakers ) ), num_horizontal_speakers );
1002 960 : set_zero( &diffuse_response_function[num_horizontal_speakers], NUM_ELEVATED_SPEAKERS );
1003 960 : *num_ele_spk_no_diffuse_rendering = NUM_ELEVATED_SPEAKERS;
1004 : }
1005 30144 : else if ( ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT || ivas_format == MC_FORMAT ) && output_config == IVAS_AUDIO_CONFIG_5_1 && num_channels == 5 )
1006 : {
1007 2561 : mvr2r( diffuse_response_CICP6, diffuse_response_function, num_channels );
1008 : }
1009 27583 : else if ( ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT || ivas_format == MC_FORMAT ) && output_config == IVAS_AUDIO_CONFIG_5_1_2 && num_channels == 7 )
1010 : {
1011 6119 : mvr2r( diffuse_response_CICP14, diffuse_response_function, num_channels );
1012 : }
1013 21464 : else if ( ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT || ivas_format == MC_FORMAT ) && ( output_config == IVAS_AUDIO_CONFIG_5_1_4 ) && ( num_channels == 9 ) )
1014 : {
1015 6658 : mvr2r( diffuse_response_CICP16, diffuse_response_function, num_channels );
1016 : }
1017 14806 : else if ( ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT || ivas_format == MC_FORMAT ) && ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM ) )
1018 : {
1019 12 : if ( transport_config == IVAS_AUDIO_CONFIG_5_1 || transport_config == IVAS_AUDIO_CONFIG_7_1 )
1020 : {
1021 : /* Detect loudspeakers with elevation */
1022 0 : for ( i = 0, num_horizontal_speakers = 0; i < num_channels; i++ )
1023 : {
1024 0 : if ( fabsf( hOutSetup.ls_elevation[i] ) <= 5.f )
1025 : {
1026 0 : num_horizontal_speakers++;
1027 0 : diffuse_response_function[i] = 1.f;
1028 : }
1029 : else
1030 : {
1031 0 : *num_ele_spk_no_diffuse_rendering += 1;
1032 0 : diffuse_response_function[i] = 0.f;
1033 : }
1034 : }
1035 : /* Diffuse only to horizontal plane if enough loudspeakers */
1036 0 : if ( num_horizontal_speakers > 2 )
1037 : {
1038 0 : for ( i = 0; i < num_channels; i++ )
1039 : {
1040 0 : diffuse_response_function[i] *= sqrtf( 1.f / (float) num_horizontal_speakers );
1041 : }
1042 : }
1043 : else
1044 : {
1045 0 : *num_ele_spk_no_diffuse_rendering = 0;
1046 0 : set_f( diffuse_response_function, sqrtf( 1.f / (float) num_channels ), num_channels );
1047 : }
1048 : }
1049 : else
1050 : {
1051 12 : set_f( diffuse_response_function, sqrtf( 1.f / (float) num_channels ), num_channels );
1052 : }
1053 : }
1054 : else
1055 : {
1056 14794 : set_f( diffuse_response_function, sqrtf( 1.f / (float) num_channels ), num_channels );
1057 : }
1058 : }
1059 : else
1060 : {
1061 55525 : idx = 0;
1062 254415 : for ( l = 0; l <= ambisonics_order; l++ )
1063 : {
1064 940598 : for ( k = 0; k < ( 2 * l + 1 ); k++ )
1065 : {
1066 741708 : diffuse_response_function[idx++] = inv_sqrt( 2.0f * l + 1.0f );
1067 : }
1068 : }
1069 : }
1070 :
1071 88607 : return;
1072 : }
1073 :
1074 :
1075 : /*-------------------------------------------------------------------------
1076 : * protoSignalComputation_shd()
1077 : *
1078 : *
1079 : *-------------------------------------------------------------------------*/
1080 :
1081 32574618 : void protoSignalComputation_shd(
1082 : float RealBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
1083 : float ImagBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
1084 : float *proto_direct_buffer_f,
1085 : float *proto_diffuse_buffer_f,
1086 : float *reference_power,
1087 : const int16_t slot_index,
1088 : const int16_t num_inputs,
1089 : const int16_t num_outputs_diff,
1090 : const int16_t num_freq_bands,
1091 : float *p_Rmat )
1092 : {
1093 : int16_t l, k;
1094 : float *p_proto_direct_buffer;
1095 : float *p_proto_diffuse_buffer;
1096 : int16_t Rmat_k[4];
1097 : float W_real, W_imag;
1098 : float Y_real, Y_imag;
1099 : float *p_k[4];
1100 :
1101 32574618 : k = 0; /* to avoid compilation warning */
1102 :
1103 32574618 : p_proto_direct_buffer = proto_direct_buffer_f + slot_index * 2 * num_freq_bands * num_inputs;
1104 32574618 : p_proto_diffuse_buffer = proto_diffuse_buffer_f + slot_index * 2 * num_freq_bands * num_outputs_diff;
1105 :
1106 32574618 : if ( num_inputs == 1 )
1107 : {
1108 39013425 : for ( l = 0; l < num_freq_bands; l++ )
1109 : {
1110 38247660 : p_proto_direct_buffer[2 * l] = RealBuffer[0][0][l];
1111 38247660 : p_proto_direct_buffer[2 * l + 1] = ImagBuffer[0][0][l];
1112 : }
1113 : }
1114 31808853 : else if ( num_inputs == 2 )
1115 : {
1116 0 : if ( p_Rmat != 0 )
1117 : {
1118 0 : assert( num_inputs == 4 && "This code block should never be run with num_inputs != 4!" );
1119 :
1120 0 : for ( l = 0; l < num_freq_bands; l++ )
1121 : {
1122 0 : W_real = RealBuffer[0][0][l] + RealBuffer[1][0][l];
1123 0 : W_imag = ImagBuffer[0][0][l] + ImagBuffer[1][0][l];
1124 :
1125 0 : Y_real = RealBuffer[0][0][l] - RealBuffer[1][0][l];
1126 0 : Y_imag = ImagBuffer[0][0][l] - ImagBuffer[1][0][l];
1127 :
1128 0 : p_proto_direct_buffer[2 * l] = W_real;
1129 0 : p_proto_direct_buffer[2 * l + 1] = W_imag;
1130 0 : p_proto_direct_buffer[2 * num_freq_bands + 2 * l] = p_Rmat[0] * Y_real;
1131 0 : p_proto_direct_buffer[2 * num_freq_bands + 2 * l + 1] = p_Rmat[0] * Y_imag;
1132 : }
1133 : }
1134 : else
1135 : {
1136 0 : for ( l = 0; l < num_freq_bands; l++ )
1137 : {
1138 0 : W_real = RealBuffer[0][0][l] + RealBuffer[1][0][l];
1139 0 : W_imag = ImagBuffer[0][0][l] + ImagBuffer[1][0][l];
1140 :
1141 0 : p_proto_direct_buffer[2 * l] = W_real;
1142 0 : p_proto_direct_buffer[2 * l + 1] = W_imag;
1143 : {
1144 0 : p_proto_direct_buffer[2 * num_freq_bands + 2 * l] = RealBuffer[0][0][l] - RealBuffer[1][0][l];
1145 0 : p_proto_direct_buffer[2 * num_freq_bands + 2 * l + 1] = ImagBuffer[0][0][l] - ImagBuffer[1][0][l];
1146 : }
1147 : }
1148 : }
1149 : }
1150 31808853 : else if ( num_inputs >= 4 )
1151 : {
1152 31808853 : p_k[0] = p_proto_direct_buffer;
1153 31808853 : p_k[1] = p_proto_direct_buffer + 2 * num_freq_bands;
1154 31808853 : p_k[2] = p_proto_direct_buffer + 4 * num_freq_bands;
1155 31808853 : p_k[3] = p_proto_direct_buffer + 6 * num_freq_bands;
1156 31808853 : Rmat_k[0] = 0;
1157 31808853 : Rmat_k[1] = 1;
1158 31808853 : Rmat_k[2] = 2;
1159 31808853 : Rmat_k[3] = 0;
1160 :
1161 31808853 : if ( p_Rmat != 0 )
1162 : {
1163 1622560 : assert( num_inputs == 4 && "This code block should never be run with num_inputs != 4!" );
1164 :
1165 63716640 : for ( l = 0; l < num_freq_bands; l++ )
1166 : {
1167 62094080 : *( p_k[0] ) = RealBuffer[0][0][l];
1168 62094080 : reference_power[l + num_freq_bands] = *( p_k[0] ) * *( p_k[0] );
1169 62094080 : p_k[0]++;
1170 62094080 : *( p_k[0] ) = ImagBuffer[0][0][l];
1171 62094080 : reference_power[l + num_freq_bands] += *( p_k[0] ) * *( p_k[0] );
1172 62094080 : p_k[0]++;
1173 62094080 : reference_power[l] = 0.5f * reference_power[l + num_freq_bands];
1174 :
1175 248376320 : for ( k = 1; k < 4; k++ )
1176 : {
1177 186282240 : *( p_k[k] ) = p_Rmat[3 * Rmat_k[k] + 1] * RealBuffer[1][0][l] + p_Rmat[3 * Rmat_k[k] + 2] * RealBuffer[2][0][l] + p_Rmat[3 * Rmat_k[k] + 0] * RealBuffer[3][0][l];
1178 186282240 : reference_power[l + ( k + 1 ) * num_freq_bands] = *( p_k[k] ) * *( p_k[k] );
1179 186282240 : p_k[k]++;
1180 186282240 : *( p_k[k] ) = p_Rmat[3 * Rmat_k[k] + 1] * ImagBuffer[1][0][l] + p_Rmat[3 * Rmat_k[k] + 2] * ImagBuffer[2][0][l] + p_Rmat[3 * Rmat_k[k] + 0] * ImagBuffer[3][0][l];
1181 186282240 : reference_power[l + ( k + 1 ) * num_freq_bands] += *( p_k[k] ) * *( p_k[k] );
1182 186282240 : p_k[k]++;
1183 186282240 : reference_power[l] += 0.5f * ( reference_power[l + ( k + 1 ) * num_freq_bands] );
1184 : }
1185 :
1186 248376320 : for ( k = 1; k < 4; k++ )
1187 : {
1188 186282240 : RealBuffer[k][0][l] = p_proto_direct_buffer[k * 2 * num_freq_bands + 2 * l];
1189 186282240 : ImagBuffer[k][0][l] = p_proto_direct_buffer[k * 2 * num_freq_bands + 2 * l + 1];
1190 : }
1191 : }
1192 : }
1193 : else
1194 : {
1195 30186293 : set_zero( reference_power, num_freq_bands );
1196 150931465 : for ( k = 0; k < 4; k++ )
1197 : {
1198 5592914132 : for ( l = 0; l < num_freq_bands; l++ )
1199 : {
1200 5472168960 : p_proto_direct_buffer[k * 2 * num_freq_bands + 2 * l] = RealBuffer[k][0][l];
1201 5472168960 : p_proto_direct_buffer[k * 2 * num_freq_bands + 2 * l + 1] = ImagBuffer[k][0][l];
1202 5472168960 : reference_power[l + ( k + 1 ) * num_freq_bands] = RealBuffer[k][0][l] * RealBuffer[k][0][l] + ImagBuffer[k][0][l] * ImagBuffer[k][0][l];
1203 5472168960 : reference_power[l] += 0.5f * ( reference_power[l + ( k + 1 ) * num_freq_bands] );
1204 : }
1205 : }
1206 : }
1207 :
1208 : /* Additional transport channels = planar SBA components of degree higher than 1*/
1209 71379557 : for ( ; k < num_inputs; k++ )
1210 : {
1211 2055118544 : for ( l = 0; l < num_freq_bands; l++ )
1212 : {
1213 2015547840 : p_proto_direct_buffer[k * 2 * num_freq_bands + 2 * l] = RealBuffer[k][0][l];
1214 2015547840 : p_proto_direct_buffer[k * 2 * num_freq_bands + 2 * l + 1] = ImagBuffer[k][0][l];
1215 : }
1216 : }
1217 : }
1218 :
1219 :
1220 : /*Copy direct to diffuse proto*/
1221 32574618 : mvr2r( p_proto_direct_buffer, p_proto_diffuse_buffer, 2 * num_freq_bands * min( num_outputs_diff, num_inputs ) );
1222 :
1223 32574618 : if ( num_inputs == 1 )
1224 : {
1225 : /* Add comfort noise addition (CNA) to diffuse proto only*/
1226 39013425 : for ( l = 0; l < num_freq_bands; l++ )
1227 : {
1228 38247660 : p_proto_diffuse_buffer[2 * l] += RealBuffer[1][0][l];
1229 38247660 : p_proto_diffuse_buffer[2 * l + 1] += ImagBuffer[1][0][l];
1230 : }
1231 : }
1232 :
1233 32574618 : return;
1234 : }
1235 :
1236 :
1237 : /*-------------------------------------------------------------------------
1238 : * protoSignalComputation1()
1239 : *
1240 : *
1241 : *-------------------------------------------------------------------------*/
1242 :
1243 3981453 : void protoSignalComputation1(
1244 : float RealBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
1245 : float ImagBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
1246 : float *proto_frame_f,
1247 : float *proto_direct_buffer_f,
1248 : float *reference_power,
1249 : float *proto_power_smooth,
1250 : const int16_t slot_index,
1251 : const int16_t num_outputs_diff,
1252 : const int16_t num_freq_bands )
1253 : {
1254 : int16_t l, k;
1255 : float *p_proto_buffer;
1256 :
1257 3981453 : p_proto_buffer = proto_direct_buffer_f + slot_index * 2 * num_freq_bands;
1258 :
1259 188820513 : for ( l = 0; l < num_freq_bands; l++ )
1260 : {
1261 184839060 : reference_power[l] = RealBuffer[0][0][l] * RealBuffer[0][0][l] + ImagBuffer[0][0][l] * ImagBuffer[0][0][l];
1262 184839060 : proto_power_smooth[l] += reference_power[l];
1263 184839060 : p_proto_buffer[2 * l] = RealBuffer[0][0][l];
1264 184839060 : p_proto_buffer[2 * l + 1] = ImagBuffer[0][0][l];
1265 :
1266 369678120 : for ( k = 0; k < num_outputs_diff; k++ )
1267 : {
1268 184839060 : proto_frame_f[2 * k * num_freq_bands + 2 * l] = RealBuffer[0][0][l];
1269 184839060 : proto_frame_f[2 * k * num_freq_bands + 2 * l + 1] = ImagBuffer[0][0][l];
1270 : }
1271 : }
1272 :
1273 3981453 : return;
1274 : }
1275 :
1276 :
1277 : /*-------------------------------------------------------------------------
1278 : * protoSignalComputation2()
1279 : *
1280 : *
1281 : *-------------------------------------------------------------------------*/
1282 :
1283 8370088 : void protoSignalComputation2(
1284 : float RealBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
1285 : float ImagBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
1286 : float *proto_frame_f,
1287 : float *proto_direct_buffer_f,
1288 : float *reference_power,
1289 : float *proto_power_smooth,
1290 : const int16_t isloudspeaker,
1291 : const int16_t slot_index,
1292 : const int16_t num_freq_bands,
1293 : MASA_STEREO_TYPE_DETECT *stereo_type_detect )
1294 : {
1295 : int16_t l;
1296 : float *p_proto_buffer;
1297 : float Real_aux, Imag_aux;
1298 :
1299 : float left_bb_power, right_bb_power, total_bb_power, lr_bb_power;
1300 : float lr_total_bb_ratio;
1301 : float a, b;
1302 :
1303 : float left_hi_power, right_hi_power, total_hi_power, lr_hi_power;
1304 : float lr_total_hi_ratio;
1305 : float a2, b2;
1306 :
1307 : float sum_power;
1308 : float sum_total_ratio[MASA_SUM_FREQ_RANGE_BINS];
1309 : float min_sum_total_ratio;
1310 : float min_sum_total_ratio_db;
1311 :
1312 : float RealSubtract, ImagSubtract;
1313 :
1314 8370088 : float interpolatorSpaced = 0.0f;
1315 8370088 : float interpolatorDmx = 1.0f;
1316 :
1317 : int16_t dipole_freq_range[2];
1318 : float tempSpaced, tempDmx;
1319 :
1320 8370088 : if ( isloudspeaker )
1321 : {
1322 5041422 : p_proto_buffer = proto_direct_buffer_f + slot_index * 2 * num_freq_bands * 3;
1323 :
1324 241998422 : for ( l = 0; l < num_freq_bands; l++ )
1325 : {
1326 : float Left_power;
1327 : float Right_power;
1328 236957000 : Real_aux = RealBuffer[0][0][l] + RealBuffer[1][0][l];
1329 236957000 : Imag_aux = ImagBuffer[0][0][l] + ImagBuffer[1][0][l];
1330 :
1331 236957000 : Left_power = RealBuffer[0][0][l] * RealBuffer[0][0][l] + ImagBuffer[0][0][l] * ImagBuffer[0][0][l];
1332 236957000 : Right_power = RealBuffer[1][0][l] * RealBuffer[1][0][l] + ImagBuffer[1][0][l] * ImagBuffer[1][0][l];
1333 :
1334 236957000 : reference_power[l] = Left_power + Right_power;
1335 236957000 : proto_power_smooth[l] += Real_aux * Real_aux + Imag_aux * Imag_aux;
1336 :
1337 236957000 : p_proto_buffer[2 * l] = Real_aux;
1338 236957000 : p_proto_buffer[2 * l + 1] = Imag_aux;
1339 236957000 : proto_power_smooth[l + num_freq_bands] += RealBuffer[0][0][l] * RealBuffer[0][0][l] + ImagBuffer[0][0][l] * ImagBuffer[0][0][l];
1340 236957000 : p_proto_buffer[2 * num_freq_bands + 2 * l] = RealBuffer[0][0][l];
1341 236957000 : p_proto_buffer[2 * num_freq_bands + 2 * l + 1] = ImagBuffer[0][0][l];
1342 :
1343 236957000 : proto_power_smooth[l + 2 * num_freq_bands] += RealBuffer[1][0][l] * RealBuffer[1][0][l];
1344 236957000 : proto_power_smooth[l + 2 * num_freq_bands] += ImagBuffer[1][0][l] * ImagBuffer[1][0][l];
1345 236957000 : p_proto_buffer[4 * num_freq_bands + 2 * l] = RealBuffer[1][0][l];
1346 236957000 : p_proto_buffer[4 * num_freq_bands + 2 * l + 1] = ImagBuffer[1][0][l];
1347 :
1348 236957000 : proto_frame_f[2 * l] = Real_aux;
1349 236957000 : proto_frame_f[2 * l + 1] = Imag_aux;
1350 :
1351 236957000 : proto_frame_f[2 * num_freq_bands + 2 * l] = RealBuffer[0][0][l];
1352 236957000 : proto_frame_f[2 * num_freq_bands + 2 * l + 1] = ImagBuffer[0][0][l];
1353 236957000 : proto_frame_f[4 * num_freq_bands + 2 * l] = RealBuffer[1][0][l];
1354 236957000 : proto_frame_f[4 * num_freq_bands + 2 * l + 1] = ImagBuffer[1][0][l];
1355 : }
1356 : }
1357 3328666 : else if ( stereo_type_detect != NULL )
1358 : {
1359 1491395 : p_proto_buffer = proto_direct_buffer_f + slot_index * 2 * num_freq_bands * 2;
1360 :
1361 1491395 : left_bb_power = 0.0f;
1362 1491395 : right_bb_power = 0.0f;
1363 1491395 : total_bb_power = 0.0f;
1364 :
1365 1491395 : left_hi_power = 0.0f;
1366 1491395 : right_hi_power = 0.0f;
1367 1491395 : total_hi_power = 0.0f;
1368 :
1369 1491395 : dipole_freq_range[0] = stereo_type_detect->dipole_freq_range[0];
1370 1491395 : dipole_freq_range[1] = stereo_type_detect->dipole_freq_range[1];
1371 :
1372 1491395 : a = 0.01f; /* Temporal smoothing coefficient */
1373 1491395 : b = 1.0f - a; /* Temporal smoothing coefficient */
1374 1491395 : a2 = 0.1f; /* Temporal smoothing coefficient */
1375 1491395 : b2 = 1.0f - a2; /* Temporal smoothing coefficient */
1376 :
1377 1491395 : if ( stereo_type_detect->interpolator > 0 )
1378 : {
1379 9345 : if ( stereo_type_detect->type_change_direction == MASA_STEREO_SPACED_MICS )
1380 : {
1381 9225 : interpolatorSpaced = ( (float) ( stereo_type_detect->interpolator ) ) / ( (float) MASA_STEREO_INTERPOLATION_SLOTS );
1382 9225 : interpolatorDmx = 1.0f - interpolatorSpaced;
1383 : }
1384 : else
1385 : {
1386 120 : interpolatorDmx = ( (float) ( stereo_type_detect->interpolator ) ) / ( (float) MASA_STEREO_INTERPOLATION_SLOTS );
1387 120 : interpolatorSpaced = 1.0f - interpolatorDmx;
1388 : }
1389 : }
1390 :
1391 72582235 : for ( l = 0; l < num_freq_bands; l++ )
1392 : {
1393 : float Left_power;
1394 : float Right_power;
1395 :
1396 : /* Compute sum signal */
1397 71090840 : Real_aux = RealBuffer[0][0][l] + RealBuffer[1][0][l];
1398 71090840 : Imag_aux = ImagBuffer[0][0][l] + ImagBuffer[1][0][l];
1399 :
1400 : /* Compute reference power */
1401 71090840 : Left_power = RealBuffer[0][0][l] * RealBuffer[0][0][l] + ImagBuffer[0][0][l] * ImagBuffer[0][0][l];
1402 71090840 : Right_power = RealBuffer[1][0][l] * RealBuffer[1][0][l] + ImagBuffer[1][0][l] * ImagBuffer[1][0][l];
1403 :
1404 71090840 : reference_power[l] = Left_power + Right_power;
1405 :
1406 71090840 : left_bb_power += Left_power;
1407 71090840 : right_bb_power += Right_power;
1408 71090840 : total_bb_power += reference_power[l];
1409 :
1410 71090840 : if ( l > MASA_HI_FREQ_START_BIN )
1411 : {
1412 48719915 : left_hi_power += Left_power;
1413 48719915 : right_hi_power += Right_power;
1414 48719915 : total_hi_power += reference_power[l];
1415 : }
1416 :
1417 71090840 : if ( l < min( num_freq_bands, MASA_SUM_FREQ_RANGE_BINS ) )
1418 : {
1419 35826315 : sum_power = Real_aux * Real_aux + Imag_aux * Imag_aux;
1420 :
1421 35826315 : stereo_type_detect->sum_power[l] = a * sum_power + b * stereo_type_detect->sum_power[l];
1422 35826315 : stereo_type_detect->total_power[l] = a * reference_power[l] + b * stereo_type_detect->total_power[l];
1423 :
1424 35826315 : sum_total_ratio[l] = stereo_type_detect->sum_power[l] / ( stereo_type_detect->total_power[l] + EPSILON );
1425 : }
1426 :
1427 71090840 : if ( l == 0 )
1428 : {
1429 1491395 : RealSubtract = RealBuffer[0][0][l] - RealBuffer[1][0][l];
1430 1491395 : ImagSubtract = ImagBuffer[0][0][l] - ImagBuffer[1][0][l];
1431 1491395 : stereo_type_detect->subtract_power_y += RealSubtract * RealSubtract + ImagSubtract * ImagSubtract;
1432 : }
1433 :
1434 : /* Compute protos (and their power) for direct sound rendering */
1435 :
1436 : /* W prototype */
1437 71090840 : if ( stereo_type_detect->interpolator > 0 )
1438 : {
1439 384300 : if ( l < ( dipole_freq_range[1] - 1 ) || l >= MASA_SUM_PROTO_START_BIN )
1440 : {
1441 337575 : Real_aux = interpolatorSpaced * 0.5f * Real_aux + interpolatorDmx * Real_aux;
1442 337575 : Imag_aux = interpolatorSpaced * 0.5f * Imag_aux + interpolatorDmx * Imag_aux;
1443 337575 : proto_power_smooth[l] += Real_aux * Real_aux + Imag_aux * Imag_aux;
1444 337575 : p_proto_buffer[2 * l] = Real_aux;
1445 337575 : p_proto_buffer[2 * l + 1] = Imag_aux;
1446 : }
1447 : else
1448 : {
1449 46725 : tempSpaced = RealBuffer[0][0][l] * RealBuffer[0][0][l] + ImagBuffer[0][0][l] * ImagBuffer[0][0][l];
1450 46725 : tempDmx = Real_aux * Real_aux + Imag_aux * Imag_aux;
1451 46725 : proto_power_smooth[l] += interpolatorSpaced * tempSpaced + interpolatorDmx * tempDmx;
1452 46725 : p_proto_buffer[2 * l] = interpolatorSpaced * RealBuffer[0][0][l] + interpolatorDmx * Real_aux;
1453 46725 : p_proto_buffer[2 * l + 1] = interpolatorSpaced * ImagBuffer[0][0][l] + interpolatorDmx * Imag_aux;
1454 : }
1455 : }
1456 70706540 : else if ( stereo_type_detect->masa_stereo_type == MASA_STEREO_SPACED_MICS )
1457 : {
1458 17037960 : if ( l < ( dipole_freq_range[1] - 1 ) || l >= MASA_SUM_PROTO_START_BIN )
1459 : {
1460 15233405 : Real_aux *= 0.5f;
1461 15233405 : Imag_aux *= 0.5f;
1462 15233405 : proto_power_smooth[l] += Real_aux * Real_aux + Imag_aux * Imag_aux;
1463 15233405 : p_proto_buffer[2 * l] = Real_aux;
1464 15233405 : p_proto_buffer[2 * l + 1] = Imag_aux;
1465 : }
1466 : else
1467 : {
1468 1804555 : proto_power_smooth[l] += RealBuffer[0][0][l] * RealBuffer[0][0][l] + ImagBuffer[0][0][l] * ImagBuffer[0][0][l];
1469 1804555 : p_proto_buffer[2 * l] = RealBuffer[0][0][l];
1470 1804555 : p_proto_buffer[2 * l + 1] = ImagBuffer[0][0][l];
1471 : }
1472 : }
1473 : else
1474 : {
1475 53668580 : proto_power_smooth[l] += Real_aux * Real_aux + Imag_aux * Imag_aux;
1476 53668580 : p_proto_buffer[2 * l] = Real_aux;
1477 53668580 : p_proto_buffer[2 * l + 1] = Imag_aux;
1478 : }
1479 :
1480 : /* Y prototype */
1481 71090840 : if ( stereo_type_detect->interpolator > 0 )
1482 : {
1483 384300 : if ( l < ( dipole_freq_range[0] ) )
1484 : {
1485 9345 : p_proto_buffer[2 * num_freq_bands + 2 * l] = interpolatorSpaced * p_proto_buffer[2 * l] + interpolatorDmx * ( RealBuffer[0][0][l] - RealBuffer[1][0][l] );
1486 9345 : p_proto_buffer[2 * num_freq_bands + 2 * l + 1] = interpolatorSpaced * p_proto_buffer[2 * l + 1] + interpolatorDmx * ( ImagBuffer[0][0][l] - ImagBuffer[1][0][l] );
1487 : }
1488 374955 : else if ( l < ( dipole_freq_range[1] ) )
1489 : {
1490 18690 : p_proto_buffer[2 * num_freq_bands + 2 * l] = interpolatorSpaced * ( ImagBuffer[0][0][l] - ImagBuffer[1][0][l] ) + interpolatorDmx * ( RealBuffer[0][0][l] - RealBuffer[1][0][l] );
1491 18690 : p_proto_buffer[2 * num_freq_bands + 2 * l + 1] = interpolatorSpaced * ( -( RealBuffer[0][0][l] - RealBuffer[1][0][l] ) ) + interpolatorDmx * ( ImagBuffer[0][0][l] - ImagBuffer[1][0][l] );
1492 : }
1493 : else
1494 : {
1495 356265 : p_proto_buffer[2 * num_freq_bands + 2 * l] = interpolatorSpaced * p_proto_buffer[2 * l] + interpolatorDmx * ( RealBuffer[0][0][l] - RealBuffer[1][0][l] );
1496 356265 : p_proto_buffer[2 * num_freq_bands + 2 * l + 1] = interpolatorSpaced * p_proto_buffer[2 * l + 1] + interpolatorDmx * ( ImagBuffer[0][0][l] - ImagBuffer[1][0][l] );
1497 : }
1498 384300 : proto_power_smooth[l + num_freq_bands] += p_proto_buffer[2 * num_freq_bands + 2 * l] * p_proto_buffer[2 * num_freq_bands + 2 * l] + p_proto_buffer[2 * num_freq_bands + 2 * l + 1] * p_proto_buffer[2 * num_freq_bands + 2 * l + 1];
1499 : }
1500 70706540 : else if ( stereo_type_detect->masa_stereo_type == MASA_STEREO_SPACED_MICS )
1501 : {
1502 17037960 : if ( l < ( dipole_freq_range[0] ) ) /* proto = W */
1503 : {
1504 360911 : p_proto_buffer[2 * num_freq_bands + 2 * l] = p_proto_buffer[2 * l];
1505 360911 : p_proto_buffer[2 * num_freq_bands + 2 * l + 1] = p_proto_buffer[2 * l + 1];
1506 360911 : proto_power_smooth[l + num_freq_bands] = proto_power_smooth[l];
1507 : }
1508 16677049 : else if ( l < ( dipole_freq_range[1] ) ) /* proto = -i * (x1-x2) * eq */
1509 : {
1510 721822 : p_proto_buffer[2 * num_freq_bands + 2 * l] = ( ImagBuffer[0][0][l] - ImagBuffer[1][0][l] );
1511 721822 : p_proto_buffer[2 * num_freq_bands + 2 * l + 1] = -( RealBuffer[0][0][l] - RealBuffer[1][0][l] );
1512 721822 : proto_power_smooth[l + num_freq_bands] += p_proto_buffer[2 * num_freq_bands + 2 * l] * p_proto_buffer[2 * num_freq_bands + 2 * l] + p_proto_buffer[2 * num_freq_bands + 2 * l + 1] * p_proto_buffer[2 * num_freq_bands + 2 * l + 1];
1513 : }
1514 : else /* proto = W */
1515 : {
1516 15955227 : p_proto_buffer[2 * num_freq_bands + 2 * l] = p_proto_buffer[2 * l];
1517 15955227 : p_proto_buffer[2 * num_freq_bands + 2 * l + 1] = p_proto_buffer[2 * l + 1];
1518 15955227 : proto_power_smooth[l + num_freq_bands] = proto_power_smooth[l];
1519 : }
1520 : }
1521 : else
1522 : {
1523 53668580 : p_proto_buffer[2 * num_freq_bands + 2 * l] = RealBuffer[0][0][l] - RealBuffer[1][0][l];
1524 53668580 : p_proto_buffer[2 * num_freq_bands + 2 * l + 1] = ImagBuffer[0][0][l] - ImagBuffer[1][0][l];
1525 53668580 : proto_power_smooth[l + num_freq_bands] += p_proto_buffer[2 * num_freq_bands + 2 * l] * p_proto_buffer[2 * num_freq_bands + 2 * l] + p_proto_buffer[2 * num_freq_bands + 2 * l + 1] * p_proto_buffer[2 * num_freq_bands + 2 * l + 1];
1526 : }
1527 :
1528 : /* Compute protos for decorrelation */
1529 71090840 : proto_frame_f[2 * l] = Real_aux;
1530 71090840 : proto_frame_f[2 * l + 1] = Imag_aux;
1531 71090840 : proto_frame_f[2 * num_freq_bands + 2 * l] = RealBuffer[0][0][l];
1532 71090840 : proto_frame_f[2 * num_freq_bands + 2 * l + 1] = ImagBuffer[0][0][l];
1533 71090840 : proto_frame_f[4 * num_freq_bands + 2 * l] = RealBuffer[1][0][l];
1534 71090840 : proto_frame_f[4 * num_freq_bands + 2 * l + 1] = ImagBuffer[1][0][l];
1535 : }
1536 :
1537 1491395 : if ( stereo_type_detect->interpolator > 0 )
1538 : {
1539 9345 : stereo_type_detect->interpolator++;
1540 9345 : if ( stereo_type_detect->interpolator == MASA_STEREO_INTERPOLATION_SLOTS )
1541 : {
1542 623 : stereo_type_detect->interpolator = 0;
1543 623 : stereo_type_detect->current_stereo_type = stereo_type_detect->type_change_direction;
1544 : }
1545 : }
1546 :
1547 1491395 : stereo_type_detect->left_bb_power = a * left_bb_power + b * stereo_type_detect->left_bb_power;
1548 1491395 : stereo_type_detect->right_bb_power = a * right_bb_power + b * stereo_type_detect->right_bb_power;
1549 1491395 : stereo_type_detect->total_bb_power = a * total_bb_power + b * stereo_type_detect->total_bb_power;
1550 :
1551 1491395 : lr_bb_power = ( stereo_type_detect->left_bb_power < stereo_type_detect->right_bb_power ) ? stereo_type_detect->left_bb_power : stereo_type_detect->right_bb_power;
1552 1491395 : lr_bb_power *= 2.0f;
1553 1491395 : lr_total_bb_ratio = 10.0f * log10f( lr_bb_power / ( stereo_type_detect->total_bb_power + EPSILON ) );
1554 :
1555 1491395 : stereo_type_detect->left_hi_power = a2 * left_hi_power + b2 * stereo_type_detect->left_hi_power;
1556 1491395 : stereo_type_detect->right_hi_power = a2 * right_hi_power + b2 * stereo_type_detect->right_hi_power;
1557 1491395 : stereo_type_detect->total_hi_power = a2 * total_hi_power + b2 * stereo_type_detect->total_hi_power;
1558 :
1559 1491395 : lr_hi_power = ( stereo_type_detect->left_hi_power < stereo_type_detect->right_hi_power ) ? stereo_type_detect->left_hi_power : stereo_type_detect->right_hi_power;
1560 1491395 : lr_hi_power *= 2.0f;
1561 1491395 : lr_total_hi_ratio = 10.0f * log10f( lr_hi_power / ( stereo_type_detect->total_hi_power + EPSILON ) );
1562 :
1563 1491395 : minimum( sum_total_ratio, min( num_freq_bands, MASA_SUM_FREQ_RANGE_BINS ), &min_sum_total_ratio );
1564 1491395 : min_sum_total_ratio_db = 10.0f * log10f( min_sum_total_ratio );
1565 :
1566 1491395 : stereo_type_detect->lr_total_bb_ratio_db = lr_total_bb_ratio;
1567 1491395 : stereo_type_detect->lr_total_hi_ratio_db = lr_total_hi_ratio;
1568 1491395 : stereo_type_detect->min_sum_total_ratio_db = min_sum_total_ratio_db;
1569 :
1570 1491395 : ivas_masa_stereotype_detection( stereo_type_detect );
1571 : }
1572 : else
1573 : {
1574 1837271 : p_proto_buffer = proto_direct_buffer_f + slot_index * 2 * num_freq_bands * 2;
1575 :
1576 85080251 : for ( l = 0; l < num_freq_bands; l++ )
1577 : {
1578 83242980 : Real_aux = RealBuffer[0][0][l] + RealBuffer[1][0][l];
1579 83242980 : Imag_aux = ImagBuffer[0][0][l] + ImagBuffer[1][0][l];
1580 :
1581 83242980 : reference_power[l] = Real_aux * Real_aux + Imag_aux * Imag_aux;
1582 83242980 : proto_power_smooth[l] += reference_power[l];
1583 83242980 : p_proto_buffer[2 * l] = Real_aux;
1584 83242980 : p_proto_buffer[2 * l + 1] = Imag_aux;
1585 :
1586 83242980 : p_proto_buffer[2 * num_freq_bands + 2 * l] = RealBuffer[0][0][l] - RealBuffer[1][0][l];
1587 83242980 : p_proto_buffer[2 * num_freq_bands + 2 * l + 1] = ImagBuffer[0][0][l] - ImagBuffer[1][0][l];
1588 83242980 : proto_power_smooth[l + num_freq_bands] += p_proto_buffer[2 * num_freq_bands + 2 * l] * p_proto_buffer[2 * num_freq_bands + 2 * l] + p_proto_buffer[2 * num_freq_bands + 2 * l + 1] * p_proto_buffer[2 * num_freq_bands + 2 * l + 1];
1589 :
1590 83242980 : proto_frame_f[2 * l] = Real_aux;
1591 83242980 : proto_frame_f[2 * l + 1] = Imag_aux;
1592 :
1593 83242980 : proto_frame_f[2 * num_freq_bands + 2 * l] = RealBuffer[0][0][l];
1594 83242980 : proto_frame_f[2 * num_freq_bands + 2 * l + 1] = ImagBuffer[0][0][l];
1595 83242980 : proto_frame_f[4 * num_freq_bands + 2 * l] = RealBuffer[1][0][l];
1596 83242980 : proto_frame_f[4 * num_freq_bands + 2 * l + 1] = ImagBuffer[1][0][l];
1597 : }
1598 : }
1599 :
1600 8370088 : return;
1601 : }
1602 :
1603 :
1604 : /*-------------------------------------------------------------------------
1605 : * protoSignalComputation4()
1606 : *
1607 : *
1608 : *-------------------------------------------------------------------------*/
1609 :
1610 1927088 : void protoSignalComputation4(
1611 : float RealBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
1612 : float ImagBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
1613 : float *proto_frame_f,
1614 : float *proto_direct_buffer_f,
1615 : float *reference_power,
1616 : float *proto_power_smooth,
1617 : const int16_t slot_index,
1618 : const int16_t num_outputs_diff,
1619 : const int16_t num_freq_bands,
1620 : const float *mtx_hoa_decoder,
1621 : const int16_t nchan_transport,
1622 : const int16_t *sba_map_tc_ind )
1623 : {
1624 : int16_t k, l;
1625 : int16_t n;
1626 : float sq_tmp;
1627 : float *p_proto_buffer;
1628 :
1629 1927088 : set_zero( reference_power, num_freq_bands );
1630 9635440 : for ( k = 0; k < 4; k++ )
1631 : {
1632 369025472 : for ( l = 0; l < num_freq_bands; l++ )
1633 : {
1634 361317120 : sq_tmp = RealBuffer[k][0][l] * RealBuffer[k][0][l] + ImagBuffer[k][0][l] * ImagBuffer[k][0][l];
1635 361317120 : reference_power[l] += 0.5f * sq_tmp;
1636 : }
1637 : }
1638 :
1639 : /*For decorrelated diffuseness*/
1640 17990832 : for ( l = 0; l < num_outputs_diff; l++ )
1641 : {
1642 782579584 : for ( k = 0; k < num_freq_bands; k++ )
1643 : {
1644 766515840 : proto_frame_f[2 * l * num_freq_bands + 2 * k] = 0.f;
1645 766515840 : proto_frame_f[2 * l * num_freq_bands + 2 * k + 1] = 0.f;
1646 3968423040 : for ( n = 0; n < nchan_transport; n++ )
1647 : {
1648 3201907200 : proto_frame_f[2 * l * num_freq_bands + 2 * k] += RealBuffer[n][0][k] * mtx_hoa_decoder[l * 16 + sba_map_tc_ind[n]];
1649 3201907200 : proto_frame_f[2 * l * num_freq_bands + 2 * k + 1] += ImagBuffer[n][0][k] * mtx_hoa_decoder[l * 16 + sba_map_tc_ind[n]];
1650 : }
1651 : }
1652 : }
1653 :
1654 1927088 : p_proto_buffer = proto_direct_buffer_f + slot_index * 2 * num_freq_bands * num_outputs_diff;
1655 17990832 : for ( k = 0; k < num_outputs_diff; k++ )
1656 : {
1657 782579584 : for ( l = 0; l < num_freq_bands; l++ )
1658 : {
1659 766515840 : sq_tmp = proto_frame_f[k * 2 * num_freq_bands + 2 * l] * proto_frame_f[k * 2 * num_freq_bands + 2 * l] + proto_frame_f[k * 2 * num_freq_bands + 2 * l + 1] * proto_frame_f[k * 2 * num_freq_bands + 2 * l + 1];
1660 766515840 : proto_power_smooth[l + k * num_freq_bands] += sq_tmp;
1661 766515840 : p_proto_buffer[k * 2 * num_freq_bands + 2 * l] = proto_frame_f[k * 2 * num_freq_bands + 2 * l];
1662 766515840 : p_proto_buffer[k * 2 * num_freq_bands + 2 * l + 1] = proto_frame_f[k * 2 * num_freq_bands + 2 * l + 1];
1663 : }
1664 : }
1665 :
1666 1927088 : return;
1667 : }
1668 :
1669 :
1670 : /*-------------------------------------------------------------------------
1671 : * ivas_dirac_dec_compute_diffuse_proto()
1672 : *
1673 : * Compute diffuse prototype buffer and smooth power, only for decorrelated bands
1674 : *------------------------------------------------------------------------*/
1675 :
1676 13651760 : void ivas_dirac_dec_compute_diffuse_proto(
1677 : DIRAC_REND_HANDLE hDirACRend,
1678 : const int16_t num_freq_bands,
1679 : const int16_t slot_idx /* i : slot index */
1680 : )
1681 : {
1682 : int16_t k, l;
1683 : int16_t num_freq_bands_diff;
1684 : float *p_diff_buffer, *p_diff_buffer_1;
1685 : float *p_proto_diff, *p_power_smooth, *proto_frame_dec_f;
1686 : DIRAC_OUTPUT_SYNTHESIS_PARAMS *h_dirac_output_synthesis_params;
1687 : DIRAC_OUTPUT_SYNTHESIS_STATE *h_dirac_output_synthesis_state;
1688 : int16_t m;
1689 : float *p_hoa_enc;
1690 :
1691 13651760 : proto_frame_dec_f = hDirACRend->proto_frame_dec_f;
1692 13651760 : h_dirac_output_synthesis_params = &( hDirACRend->h_output_synthesis_psd_params );
1693 13651760 : h_dirac_output_synthesis_state = &( hDirACRend->h_output_synthesis_psd_state );
1694 :
1695 13651760 : num_freq_bands_diff = h_dirac_output_synthesis_params->max_band_decorr;
1696 13651760 : if ( num_freq_bands_diff == 0 )
1697 : {
1698 1927088 : return;
1699 : }
1700 :
1701 11724672 : p_diff_buffer = h_dirac_output_synthesis_state->proto_diffuse_buffer_f + slot_idx * 2 * num_freq_bands_diff * hDirACRend->hOutSetup.nchan_out_woLFE;
1702 11724672 : p_diff_buffer_1 = p_diff_buffer + 1;
1703 11724672 : p_power_smooth = h_dirac_output_synthesis_state->proto_power_diff_smooth;
1704 :
1705 11724672 : if ( hDirACRend->synthesisConf != DIRAC_SYNTHESIS_PSD_SHD )
1706 : {
1707 72195116 : for ( k = 0; k < hDirACRend->hOutSetup.nchan_out_woLFE; k++ )
1708 : {
1709 63711493 : p_proto_diff = proto_frame_dec_f + k * 2 * num_freq_bands;
1710 1019383888 : for ( l = 0; l < num_freq_bands_diff; l++ )
1711 : {
1712 955672395 : *p_diff_buffer = *( p_proto_diff++ );
1713 955672395 : *p_diff_buffer_1 = *( p_proto_diff++ );
1714 955672395 : *( p_power_smooth++ ) += ( *p_diff_buffer ) * ( *p_diff_buffer ) + ( *p_diff_buffer_1 ) * ( *p_diff_buffer_1 );
1715 955672395 : p_diff_buffer += 2;
1716 955672395 : p_diff_buffer_1 += 2;
1717 : }
1718 : }
1719 : }
1720 : else
1721 : {
1722 : /*DIRAC_SYNTHESIS_PSD_SHD: Virtual LS->HOA encoding*/
1723 32610925 : for ( k = 0; k < hDirACRend->hOutSetup.nchan_out_woLFE; k++ )
1724 : {
1725 469918016 : for ( l = 0; l < num_freq_bands_diff; l++ )
1726 : {
1727 440548140 : p_hoa_enc = hDirACRend->hoa_encoder + k;
1728 440548140 : p_proto_diff = proto_frame_dec_f + 2 * l;
1729 :
1730 440548140 : *p_diff_buffer = 0.f;
1731 440548140 : *p_diff_buffer_1 = 0.f;
1732 :
1733 : /*LS to HOA*/
1734 3964933260 : for ( m = 0; m < hDirACRend->num_outputs_diff; m++ )
1735 : {
1736 3524385120 : *p_diff_buffer += ( *p_hoa_enc ) * ( *p_proto_diff );
1737 3524385120 : *p_diff_buffer_1 += ( *p_hoa_enc ) * ( *( p_proto_diff + 1 ) );
1738 3524385120 : p_hoa_enc += hDirACRend->hOutSetup.nchan_out_woLFE;
1739 3524385120 : p_proto_diff += 2 * num_freq_bands;
1740 : }
1741 :
1742 440548140 : *( p_power_smooth++ ) += ( *p_diff_buffer ) * ( *p_diff_buffer ) + ( *p_diff_buffer_1 ) * ( *p_diff_buffer_1 );
1743 440548140 : p_diff_buffer += 2;
1744 440548140 : p_diff_buffer_1 += 2;
1745 : }
1746 : }
1747 : }
1748 :
1749 11724672 : return;
1750 : }
1751 :
1752 :
1753 : /*-------------------------------------------------------------------------
1754 : * computeDirectionAngles()
1755 : *
1756 : *------------------------------------------------------------------------*/
1757 :
1758 27254357 : void computeDirectionAngles(
1759 : float *intensity_real_x,
1760 : float *intensity_real_y,
1761 : float *intensity_real_z,
1762 : const int16_t num_frequency_bands,
1763 : int16_t *azimuth,
1764 : int16_t *elevation )
1765 : {
1766 : int16_t k;
1767 : float intensityNorm;
1768 : float x, y, z, radius;
1769 :
1770 327052284 : for ( k = 0; k < num_frequency_bands; ++k )
1771 : {
1772 299797927 : intensityNorm = *( intensity_real_x ) * *( intensity_real_x ) +
1773 299797927 : *( intensity_real_y ) * *( intensity_real_y ) +
1774 299797927 : *( intensity_real_z ) * *( intensity_real_z );
1775 :
1776 299797927 : if ( intensityNorm <= EPSILON )
1777 : {
1778 2260258 : intensityNorm = 1.0f;
1779 2260258 : x = 1.0f;
1780 2260258 : y = 0.0f;
1781 2260258 : z = 0.0f;
1782 2260258 : intensity_real_x++;
1783 2260258 : intensity_real_y++;
1784 2260258 : intensity_real_z++;
1785 : }
1786 : else
1787 : {
1788 297537669 : intensityNorm = sqrtf( 1.f / intensityNorm );
1789 297537669 : x = *( intensity_real_x++ ) * intensityNorm;
1790 297537669 : y = *( intensity_real_y++ ) * intensityNorm;
1791 297537669 : z = *( intensity_real_z++ ) * intensityNorm;
1792 : }
1793 299797927 : radius = sqrtf( x * x + y * y );
1794 299797927 : azimuth[k] = (int16_t) ( max( -180.0f, min( 180.0f, atan2f( y, x ) / EVS_PI * 180.0f ) ) + 0.5f );
1795 299797927 : elevation[k] = (int16_t) ( max( -90.0f, min( 180.0f, atan2f( z, radius ) / EVS_PI * 180.0f ) ) + 0.5f );
1796 : }
1797 :
1798 27254357 : return;
1799 : }
1800 :
1801 :
1802 : /*-------------------------------------------------------------------------
1803 : * ivas_masa_init_stereotype_detection()
1804 : *
1805 : * Initialize stereo transport signal type detection
1806 : *------------------------------------------------------------------------*/
1807 :
1808 3032 : void ivas_masa_init_stereotype_detection(
1809 : MASA_STEREO_TYPE_DETECT *stereo_type_detect )
1810 : {
1811 3032 : stereo_type_detect->masa_stereo_type = MASA_STEREO_DOWNMIX;
1812 3032 : stereo_type_detect->current_stereo_type = MASA_STEREO_DOWNMIX;
1813 3032 : stereo_type_detect->type_change_direction = MASA_STEREO_DOWNMIX;
1814 :
1815 3032 : stereo_type_detect->counter = 0;
1816 3032 : stereo_type_detect->interpolator = 0;
1817 :
1818 3032 : stereo_type_detect->dipole_freq_range[0] = 1;
1819 3032 : stereo_type_detect->dipole_freq_range[1] = 3;
1820 :
1821 3032 : stereo_type_detect->left_bb_power = 0.0f; /* Broadband estimates */
1822 3032 : stereo_type_detect->right_bb_power = 0.0f;
1823 3032 : stereo_type_detect->total_bb_power = 0.0f;
1824 :
1825 3032 : stereo_type_detect->left_hi_power = 0.0f; /* High-frequency estimates */
1826 3032 : stereo_type_detect->right_hi_power = 0.0f;
1827 3032 : stereo_type_detect->total_hi_power = 0.0f;
1828 :
1829 3032 : set_zero( stereo_type_detect->sum_power, MASA_SUM_FREQ_RANGE_BINS );
1830 3032 : set_zero( stereo_type_detect->total_power, MASA_SUM_FREQ_RANGE_BINS );
1831 :
1832 3032 : stereo_type_detect->subtract_power_y = 0.0f;
1833 3032 : stereo_type_detect->subtract_power_y_smooth = 0.0f;
1834 3032 : stereo_type_detect->target_power_y_smooth = 0.0f;
1835 :
1836 3032 : stereo_type_detect->lr_total_bb_ratio_db = 0.0f;
1837 3032 : stereo_type_detect->lr_total_hi_ratio_db = 0.0f;
1838 3032 : stereo_type_detect->min_sum_total_ratio_db = 0.0f;
1839 3032 : stereo_type_detect->subtract_target_ratio_db = 0.0f;
1840 :
1841 3032 : return;
1842 : }
1843 :
1844 :
1845 : /*-------------------------------------------------------------------------
1846 : * ivas_masa_stereotype_detection()
1847 : *
1848 : * Detect the type of the transport audio signals
1849 : *------------------------------------------------------------------------*/
1850 :
1851 1491395 : void ivas_masa_stereotype_detection(
1852 : MASA_STEREO_TYPE_DETECT *stereo_type_detect )
1853 : {
1854 1491395 : float lr_total_bb_ratio_db = stereo_type_detect->lr_total_bb_ratio_db;
1855 1491395 : float lr_total_hi_ratio_db = stereo_type_detect->lr_total_hi_ratio_db;
1856 1491395 : float min_sum_total_ratio_db = stereo_type_detect->min_sum_total_ratio_db;
1857 1491395 : float subtract_target_ratio_db = stereo_type_detect->subtract_target_ratio_db;
1858 : float change_to_spaced;
1859 : int16_t change_to_spaced_selection;
1860 : float change_to_downmix;
1861 : float change_to_downmix2;
1862 : int16_t change_to_downmix_selection;
1863 : float subtract_temp;
1864 : float min_sum_temp;
1865 : float lr_total_bb_temp;
1866 : float lr_total_hi_temp;
1867 :
1868 : /* Determine if the determined features match the spaced mic type */
1869 1491395 : change_to_spaced_selection = 0;
1870 1491395 : if ( subtract_target_ratio_db < -3.0f )
1871 : {
1872 673607 : subtract_temp = ( -subtract_target_ratio_db - 3.0f ) / 3.0f;
1873 673607 : min_sum_temp = max( -min_sum_total_ratio_db / 6.0f, 0.0f );
1874 673607 : lr_total_bb_temp = lr_total_bb_ratio_db / 6.0f;
1875 :
1876 673607 : change_to_spaced = subtract_temp + min_sum_temp + lr_total_bb_temp;
1877 :
1878 673607 : if ( change_to_spaced >= 1.0f )
1879 : {
1880 569358 : change_to_spaced_selection = 1;
1881 : }
1882 : }
1883 :
1884 : /* Determine if the determined features match the downmix type, according to a metric */
1885 1491395 : change_to_downmix_selection = 0;
1886 1491395 : if ( subtract_target_ratio_db > 0.0f )
1887 : {
1888 561935 : subtract_temp = subtract_target_ratio_db / 3.0f;
1889 561935 : min_sum_temp = ( min_sum_total_ratio_db + 1.0f ) / 6.0f;
1890 561935 : lr_total_bb_temp = -lr_total_bb_ratio_db / 6.0f;
1891 :
1892 561935 : change_to_downmix = subtract_temp + min_sum_temp + lr_total_bb_temp;
1893 :
1894 561935 : if ( change_to_downmix >= 1.0f )
1895 : {
1896 387524 : change_to_downmix_selection = 1;
1897 : }
1898 : }
1899 :
1900 : /* Determine if the determined features match the downmix type, according to another metric */
1901 1491395 : if ( lr_total_hi_ratio_db < -12.0f )
1902 : {
1903 155819 : subtract_temp = ( subtract_target_ratio_db + 4.0f ) / 3.0f;
1904 155819 : min_sum_temp = min_sum_total_ratio_db / 6.0f;
1905 155819 : lr_total_hi_temp = ( -lr_total_hi_ratio_db - 12.0f ) / 3.0f;
1906 :
1907 155819 : change_to_downmix2 = subtract_temp + min_sum_temp + lr_total_hi_temp;
1908 :
1909 155819 : if ( change_to_downmix2 >= 1.0f )
1910 : {
1911 152140 : change_to_downmix_selection = 1;
1912 : }
1913 : }
1914 :
1915 1491395 : if ( stereo_type_detect->counter < 400 )
1916 : {
1917 444553 : stereo_type_detect->counter++;
1918 : }
1919 : else
1920 : {
1921 1046842 : if ( change_to_spaced_selection == 1 )
1922 : {
1923 303108 : stereo_type_detect->masa_stereo_type = MASA_STEREO_SPACED_MICS;
1924 : }
1925 743734 : else if ( change_to_downmix_selection == 1 )
1926 : {
1927 333279 : stereo_type_detect->masa_stereo_type = MASA_STEREO_DOWNMIX;
1928 : }
1929 : }
1930 :
1931 1491395 : if ( stereo_type_detect->interpolator == 0 )
1932 : {
1933 1482673 : if ( stereo_type_detect->current_stereo_type != stereo_type_detect->masa_stereo_type )
1934 : {
1935 623 : stereo_type_detect->interpolator = 1;
1936 623 : stereo_type_detect->type_change_direction = stereo_type_detect->masa_stereo_type;
1937 : }
1938 : }
1939 :
1940 1491395 : return;
1941 : }
1942 :
1943 :
1944 : /*-------------------------------------------------------------------------
1945 : * computeIntensityVector_dec()
1946 : *
1947 : *
1948 : *------------------------------------------------------------------------*/
1949 :
1950 27254357 : void computeIntensityVector_dec(
1951 : float Cldfb_RealBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
1952 : float Cldfb_ImagBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
1953 : const int16_t num_frequency_bands,
1954 : float *intensity_real_x,
1955 : float *intensity_real_y,
1956 : float *intensity_real_z )
1957 : {
1958 : /*
1959 : * W = a + ib; Y = c + id
1960 : * real(W*Y') = ac + bd
1961 : */
1962 : int16_t i;
1963 : float real, img;
1964 :
1965 327052284 : for ( i = 0; i < num_frequency_bands; ++i )
1966 : {
1967 299797927 : real = Cldfb_RealBuffer[0][0][i];
1968 299797927 : img = Cldfb_ImagBuffer[0][0][i];
1969 299797927 : intensity_real_x[i] = Cldfb_RealBuffer[3][0][i] * real + Cldfb_ImagBuffer[3][0][i] * img;
1970 299797927 : intensity_real_y[i] = Cldfb_RealBuffer[1][0][i] * real + Cldfb_ImagBuffer[1][0][i] * img;
1971 299797927 : intensity_real_z[i] = Cldfb_RealBuffer[2][0][i] * real + Cldfb_ImagBuffer[2][0][i] * img;
1972 : }
1973 :
1974 27254357 : return;
1975 : }
1976 :
1977 :
1978 : /*-------------------------------------------------------------------------
1979 : * ivas_lfe_synth_with_cldfb()
1980 : *
1981 : *
1982 : *------------------------------------------------------------------------*/
1983 :
1984 1274160 : void ivas_lfe_synth_with_cldfb(
1985 : MCMASA_LFE_SYNTH_DATA_HANDLE hMasaLfeSynth,
1986 : float RealBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
1987 : float ImagBuffer[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
1988 : float RealBufferLfe[MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
1989 : float ImagBufferLfe[MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],
1990 : const int16_t slot_index,
1991 : const int16_t subframe_index,
1992 : const int16_t nchan_transport )
1993 : {
1994 : float lfeGain;
1995 : float transportGain;
1996 : float protoLfeReal, protoLfeImag;
1997 : int16_t i;
1998 : float transportEne, protoLfeEne, targetEneLfe, targetEneTrans;
1999 :
2000 1274160 : set_zero( RealBufferLfe[slot_index], CLDFB_NO_CHANNELS_MAX );
2001 1274160 : set_zero( ImagBufferLfe[slot_index], CLDFB_NO_CHANNELS_MAX );
2002 :
2003 1274160 : protoLfeReal = RealBuffer[0][0][0];
2004 1274160 : protoLfeImag = ImagBuffer[0][0][0];
2005 1274160 : transportEne = RealBuffer[0][0][0] * RealBuffer[0][0][0] + ImagBuffer[0][0][0] * ImagBuffer[0][0][0];
2006 1424016 : for ( i = 1; i < nchan_transport; i++ )
2007 : {
2008 149856 : protoLfeReal += RealBuffer[i][0][0];
2009 149856 : protoLfeImag += ImagBuffer[i][0][0];
2010 149856 : transportEne += RealBuffer[i][0][0] * RealBuffer[i][0][0] + ImagBuffer[i][0][0] * ImagBuffer[i][0][0];
2011 : }
2012 1274160 : protoLfeEne = protoLfeReal * protoLfeReal + protoLfeImag * protoLfeImag;
2013 :
2014 1274160 : targetEneLfe = transportEne * hMasaLfeSynth->lfeToTotalEnergyRatio[subframe_index];
2015 1274160 : targetEneTrans = transportEne * max( ( 1.0f - hMasaLfeSynth->lfeToTotalEnergyRatio[subframe_index] ), 0.01f );
2016 :
2017 1274160 : hMasaLfeSynth->transportEneSmooth *= MCMASA_LFE_SYNTH_ALPHA;
2018 1274160 : hMasaLfeSynth->protoLfeEneSmooth *= MCMASA_LFE_SYNTH_ALPHA;
2019 1274160 : hMasaLfeSynth->targetEneLfeSmooth *= MCMASA_LFE_SYNTH_ALPHA;
2020 1274160 : hMasaLfeSynth->targetEneTransSmooth *= MCMASA_LFE_SYNTH_ALPHA;
2021 :
2022 1274160 : hMasaLfeSynth->transportEneSmooth += transportEne;
2023 1274160 : hMasaLfeSynth->protoLfeEneSmooth += protoLfeEne;
2024 1274160 : hMasaLfeSynth->targetEneLfeSmooth += targetEneLfe;
2025 1274160 : hMasaLfeSynth->targetEneTransSmooth += targetEneTrans;
2026 :
2027 1274160 : lfeGain = min( 1.0f, sqrtf( hMasaLfeSynth->targetEneLfeSmooth / ( EPSILON + hMasaLfeSynth->protoLfeEneSmooth ) ) );
2028 1274160 : transportGain = min( 1.0f, sqrtf( hMasaLfeSynth->targetEneTransSmooth / ( EPSILON + hMasaLfeSynth->transportEneSmooth ) ) );
2029 :
2030 1274160 : RealBufferLfe[slot_index][0] = protoLfeReal * lfeGain;
2031 1274160 : ImagBufferLfe[slot_index][0] = protoLfeImag * lfeGain;
2032 :
2033 1274160 : RealBuffer[0][0][0] *= transportGain;
2034 1274160 : ImagBuffer[0][0][0] *= transportGain;
2035 1424016 : for ( i = 1; i < nchan_transport; i++ )
2036 : {
2037 149856 : RealBuffer[i][0][0] *= transportGain;
2038 149856 : ImagBuffer[i][0][0] *= transportGain;
2039 : }
2040 :
2041 1274160 : return;
2042 : }
2043 :
2044 :
2045 : /*-------------------------------------------------------------------------
2046 : * rotateAziEle_DirAC()
2047 : *
2048 : * Apply rotation to DirAC DOAs
2049 : *------------------------------------------------------------------------*/
2050 :
2051 1622560 : void rotateAziEle_DirAC(
2052 : int16_t *azi, /* i/o: array of azimuth values */
2053 : int16_t *ele, /* i/o: array of elevation values */
2054 : const int16_t band1, /* i : bands to work on (lower limit) */
2055 : const int16_t band2, /* i : bands to work on (upper bound) */
2056 : const float *p_Rmat /* i : pointer to real-space rotation matrix */
2057 : )
2058 : {
2059 : int16_t b;
2060 : float dv_0, dv_1, dv_2;
2061 : float dv_r_0, dv_r_1, dv_r_2;
2062 : float w;
2063 :
2064 1622560 : push_wmops( "rotateAziEle_DirAC" );
2065 :
2066 45868480 : for ( b = band1; b < band2; b++ )
2067 : {
2068 :
2069 : /*Conversion spherical to cartesian coordinates*/
2070 44245920 : w = cosf( ele[b] * PI_OVER_180 );
2071 44245920 : dv_0 = w * cosf( azi[b] * PI_OVER_180 );
2072 44245920 : dv_1 = w * sinf( azi[b] * PI_OVER_180 );
2073 44245920 : dv_2 = sinf( ele[b] * PI_OVER_180 );
2074 :
2075 44245920 : dv_r_0 = p_Rmat[0] * dv_0 + p_Rmat[1] * dv_1 + p_Rmat[2] * dv_2;
2076 44245920 : dv_r_1 = p_Rmat[3] * dv_0 + p_Rmat[4] * dv_1 + p_Rmat[5] * dv_2;
2077 44245920 : dv_r_2 = p_Rmat[6] * dv_0 + p_Rmat[7] * dv_1 + p_Rmat[8] * dv_2;
2078 :
2079 : /*Conversion spherical to cartesian coordinates*/
2080 44245920 : azi[b] = (int16_t) ( atan2f( dv_r_1, dv_r_0 ) * _180_OVER_PI );
2081 44245920 : ele[b] = (int16_t) ( atan2f( dv_r_2, sqrtf( dv_r_0 * dv_r_0 + dv_r_1 * dv_r_1 ) ) * _180_OVER_PI );
2082 : }
2083 :
2084 1622560 : pop_wmops();
2085 :
2086 1622560 : return;
2087 : }
2088 :
2089 : /* A reduced rewrite of the corresponding decoder side function */
2090 48000 : static void ivas_masa_ext_dirac_render_sf(
2091 : MASA_EXT_REND_HANDLE hMasaExtRend, /* i/o: IVAS decoder structure */
2092 : float *output_f[] /* i/o: synthesized core-coder transport channels/DirAC output */
2093 : )
2094 : {
2095 : int16_t i, ch, idx_in, idx_lfe;
2096 : DIRAC_REND_HANDLE hDirACRend;
2097 : float dirEne;
2098 : float surCohEner;
2099 : float surCohRatio[CLDFB_NO_CHANNELS_MAX];
2100 : int16_t subframe_idx;
2101 : int16_t slot_idx, index_slot;
2102 : int16_t slot_idx_start, slot_idx_start_cldfb_synth, md_idx;
2103 : int16_t nchan_transport;
2104 : int16_t masa_band_mapping[MASA_FREQUENCY_BANDS + 1];
2105 :
2106 : /* CLDFB: last output channels reserved to LFT for CICPx */
2107 : float Cldfb_RealBuffer[MAX_OUTPUT_CHANNELS][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX];
2108 : float Cldfb_ImagBuffer[MAX_OUTPUT_CHANNELS][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX];
2109 :
2110 : /* local copies of azi, ele, diffuseness */
2111 : int16_t azimuth[CLDFB_NO_CHANNELS_MAX];
2112 : int16_t elevation[CLDFB_NO_CHANNELS_MAX];
2113 : float diffuseness_vector[CLDFB_NO_CHANNELS_MAX];
2114 :
2115 : DIRAC_DEC_STACK_MEM DirAC_mem;
2116 : float *reference_power, *reference_power_smooth;
2117 48000 : float *onset_filter, *onset_filter_subframe, *p_onset_filter = NULL;
2118 : uint16_t coherence_flag;
2119 : SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
2120 :
2121 48000 : push_wmops( "ivas_masa_ext_dirac_render_sf" );
2122 :
2123 : /* Initialize aux buffers */
2124 48000 : hDirACRend = hMasaExtRend->hDirACRend;
2125 48000 : hSpatParamRendCom = hMasaExtRend->hSpatParamRendCom;
2126 48000 : nchan_transport = hMasaExtRend->nchan_input;
2127 :
2128 48000 : DirAC_mem = hDirACRend->stack_mem;
2129 :
2130 48000 : reference_power = DirAC_mem.reference_power;
2131 48000 : reference_power_smooth = ( DirAC_mem.reference_power == NULL ) ? NULL : DirAC_mem.reference_power + hSpatParamRendCom->num_freq_bands;
2132 48000 : onset_filter = DirAC_mem.onset_filter;
2133 48000 : onset_filter_subframe = ( DirAC_mem.onset_filter == NULL ) ? NULL : DirAC_mem.onset_filter + hSpatParamRendCom->num_freq_bands;
2134 48000 : coherence_flag = 1; /* There is always coherence assumed for ext rend of MASA */
2135 :
2136 : /* Construct default MASA band mapping */
2137 1248000 : for ( i = 0; i < MASA_FREQUENCY_BANDS + 1; i++ )
2138 : {
2139 1200000 : masa_band_mapping[i] = i;
2140 : }
2141 :
2142 : /* Subframe loop */
2143 48000 : slot_idx_start = hSpatParamRendCom->slots_rendered;
2144 48000 : slot_idx_start_cldfb_synth = 0;
2145 :
2146 48000 : subframe_idx = hSpatParamRendCom->subframes_rendered;
2147 48000 : md_idx = hSpatParamRendCom->render_to_md_map[subframe_idx];
2148 :
2149 : /* copy parameters into local buffers*/
2150 48000 : mvs2s( hSpatParamRendCom->azimuth[hSpatParamRendCom->render_to_md_map[subframe_idx]], azimuth, hSpatParamRendCom->num_freq_bands );
2151 48000 : mvs2s( hSpatParamRendCom->elevation[hSpatParamRendCom->render_to_md_map[subframe_idx]], elevation, hSpatParamRendCom->num_freq_bands );
2152 48000 : mvr2r( hSpatParamRendCom->diffuseness_vector[hSpatParamRendCom->render_to_md_map[subframe_idx]], diffuseness_vector, hSpatParamRendCom->num_freq_bands );
2153 :
2154 48000 : if ( hDirACRend->synthesisConf != DIRAC_SYNTHESIS_GAIN_SHD )
2155 : {
2156 40800 : set_zero( reference_power_smooth, hSpatParamRendCom->num_freq_bands );
2157 : }
2158 : else
2159 : {
2160 7200 : set_zero( onset_filter_subframe, hSpatParamRendCom->num_freq_bands );
2161 : }
2162 :
2163 : /* compute response */
2164 48000 : if ( hDirACRend->synthesisConf != DIRAC_SYNTHESIS_GAIN_SHD )
2165 : {
2166 40800 : ivas_dirac_dec_compute_power_factors( hSpatParamRendCom->num_freq_bands,
2167 : diffuseness_vector,
2168 40800 : hDirACRend->h_output_synthesis_psd_params.max_band_decorr,
2169 : hDirACRend->h_output_synthesis_psd_state.direct_power_factor,
2170 : hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor );
2171 :
2172 40800 : if ( coherence_flag )
2173 : {
2174 2488800 : for ( i = 0; i < hSpatParamRendCom->num_freq_bands; i++ )
2175 : {
2176 2448000 : dirEne = hDirACRend->h_output_synthesis_psd_state.direct_power_factor[i];
2177 2448000 : surCohEner = hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor[i] * hSpatParamRendCom->surroundingCoherence[md_idx][i];
2178 2448000 : hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor[i] -= surCohEner;
2179 2448000 : hDirACRend->h_output_synthesis_psd_state.direct_power_factor[i] += surCohEner;
2180 :
2181 2448000 : surCohRatio[i] = surCohEner / ( 1e-12f + dirEne + surCohEner );
2182 : }
2183 : }
2184 : else
2185 : {
2186 0 : set_zero( surCohRatio, hSpatParamRendCom->num_freq_bands );
2187 : }
2188 : }
2189 : else
2190 : {
2191 7200 : ivas_dirac_dec_compute_gain_factors( hSpatParamRendCom->num_freq_bands,
2192 7200 : hSpatParamRendCom->diffuseness_vector[md_idx],
2193 7200 : hDirACRend->h_output_synthesis_psd_params.max_band_decorr,
2194 : hDirACRend->h_output_synthesis_psd_state.direct_power_factor,
2195 : hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor );
2196 :
2197 7200 : if ( coherence_flag )
2198 : {
2199 439200 : for ( i = 0; i < hSpatParamRendCom->num_freq_bands; i++ )
2200 : {
2201 432000 : surCohRatio[i] = hSpatParamRendCom->surroundingCoherence[md_idx][i];
2202 : }
2203 : }
2204 : else
2205 : {
2206 0 : set_zero( surCohRatio, hSpatParamRendCom->num_freq_bands );
2207 : }
2208 : }
2209 :
2210 48000 : ivas_dirac_dec_compute_directional_responses( hSpatParamRendCom,
2211 : hDirACRend,
2212 : hMasaExtRend->hVBAPdata,
2213 : masa_band_mapping,
2214 : NULL,
2215 : azimuth,
2216 : elevation,
2217 : md_idx,
2218 : surCohRatio,
2219 : 0 );
2220 :
2221 :
2222 240000 : for ( slot_idx = 0; slot_idx < hSpatParamRendCom->subframe_nbslots[subframe_idx]; slot_idx++ )
2223 : {
2224 192000 : index_slot = slot_idx_start + slot_idx;
2225 192000 : md_idx = hSpatParamRendCom->render_to_md_map[subframe_idx];
2226 :
2227 : /* CLDFB Analysis*/
2228 484800 : for ( ch = 0; ch < nchan_transport; ch++ )
2229 : {
2230 292800 : cldfbAnalysis_ts( &( output_f[ch][hSpatParamRendCom->num_freq_bands * index_slot] ),
2231 292800 : Cldfb_RealBuffer[ch][0],
2232 292800 : Cldfb_ImagBuffer[ch][0],
2233 292800 : hSpatParamRendCom->num_freq_bands,
2234 : hMasaExtRend->cldfbAnaRend[ch] );
2235 : }
2236 :
2237 :
2238 192000 : if ( nchan_transport == 1 )
2239 : {
2240 : /* Need to set second CLDFB channel to zero as further processing assumes CNA content in it */
2241 91200 : set_zero( Cldfb_RealBuffer[1][0], hSpatParamRendCom->num_freq_bands );
2242 91200 : set_zero( Cldfb_ImagBuffer[1][0], hSpatParamRendCom->num_freq_bands );
2243 : }
2244 :
2245 : /*-----------------------------------------------------------------*
2246 : * prototype signal computation
2247 : *-----------------------------------------------------------------*/
2248 :
2249 192000 : if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
2250 : {
2251 28800 : protoSignalComputation_shd( Cldfb_RealBuffer, Cldfb_ImagBuffer,
2252 : hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f,
2253 : hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f,
2254 : reference_power, slot_idx, nchan_transport,
2255 28800 : hDirACRend->num_outputs_diff,
2256 28800 : hSpatParamRendCom->num_freq_bands,
2257 : 0 );
2258 : }
2259 163200 : else if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_MONO )
2260 : {
2261 9600 : protoSignalComputation2( Cldfb_RealBuffer, Cldfb_ImagBuffer, hDirACRend->proto_frame_f,
2262 : hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f,
2263 : reference_power, hDirACRend->h_output_synthesis_psd_state.proto_power_smooth,
2264 9600 : 0, slot_idx, hSpatParamRendCom->num_freq_bands, hDirACRend->masa_stereo_type_detect );
2265 : }
2266 : else
2267 : {
2268 153600 : switch ( nchan_transport )
2269 : {
2270 91200 : case 2:
2271 91200 : protoSignalComputation2( Cldfb_RealBuffer, Cldfb_ImagBuffer,
2272 : hDirACRend->proto_frame_f,
2273 : hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f,
2274 : reference_power,
2275 : hDirACRend->h_output_synthesis_psd_state.proto_power_smooth,
2276 91200 : hDirACRend->hOutSetup.is_loudspeaker_setup,
2277 : slot_idx,
2278 91200 : hSpatParamRendCom->num_freq_bands,
2279 : hDirACRend->masa_stereo_type_detect );
2280 91200 : break;
2281 62400 : case 1:
2282 62400 : protoSignalComputation1( Cldfb_RealBuffer, Cldfb_ImagBuffer,
2283 : hDirACRend->proto_frame_f,
2284 : hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f,
2285 : reference_power,
2286 : hDirACRend->h_output_synthesis_psd_state.proto_power_smooth,
2287 : slot_idx,
2288 62400 : hDirACRend->num_protos_diff,
2289 62400 : hSpatParamRendCom->num_freq_bands );
2290 62400 : break;
2291 0 : default:
2292 0 : return;
2293 : }
2294 : }
2295 :
2296 : /*-----------------------------------------------------------------*
2297 : * frequency domain decorrelation
2298 : *-----------------------------------------------------------------*/
2299 :
2300 192000 : if ( hDirACRend->proto_signal_decorr_on == 1 )
2301 : {
2302 : /* decorrelate prototype frame */
2303 182400 : if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
2304 : {
2305 28800 : ivas_dirac_dec_decorr_process( hSpatParamRendCom->num_freq_bands,
2306 28800 : hDirACRend->num_outputs_diff,
2307 28800 : hDirACRend->num_protos_diff,
2308 : hDirACRend->synthesisConf,
2309 : nchan_transport,
2310 28800 : hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f + slot_idx * 2 * hSpatParamRendCom->num_freq_bands * hDirACRend->num_outputs_diff,
2311 28800 : hDirACRend->num_protos_diff,
2312 28800 : hDirACRend->proto_index_diff,
2313 28800 : hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f + slot_idx * 2 * hSpatParamRendCom->num_freq_bands * hDirACRend->num_outputs_diff + 2 * hSpatParamRendCom->num_freq_bands * min( 4, nchan_transport ),
2314 : onset_filter,
2315 : hDirACRend->h_freq_domain_decorr_ap_params,
2316 : hDirACRend->h_freq_domain_decorr_ap_state );
2317 :
2318 28800 : v_multc( onset_filter, 0.25f, onset_filter, hSpatParamRendCom->num_freq_bands );
2319 28800 : v_add( onset_filter, onset_filter_subframe, onset_filter_subframe, hSpatParamRendCom->num_freq_bands );
2320 28800 : p_onset_filter = onset_filter_subframe;
2321 : }
2322 : else
2323 : {
2324 153600 : ivas_dirac_dec_decorr_process( hSpatParamRendCom->num_freq_bands,
2325 153600 : hDirACRend->num_outputs_diff,
2326 153600 : hDirACRend->num_protos_diff,
2327 : hDirACRend->synthesisConf,
2328 : nchan_transport,
2329 153600 : hDirACRend->proto_frame_f,
2330 153600 : hDirACRend->num_protos_diff,
2331 153600 : hDirACRend->proto_index_diff,
2332 : DirAC_mem.frame_dec_f,
2333 : onset_filter,
2334 : hDirACRend->h_freq_domain_decorr_ap_params,
2335 : hDirACRend->h_freq_domain_decorr_ap_state );
2336 :
2337 153600 : hDirACRend->proto_frame_dec_f = DirAC_mem.frame_dec_f;
2338 153600 : p_onset_filter = onset_filter;
2339 : }
2340 : }
2341 : else
2342 : {
2343 9600 : if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
2344 : {
2345 0 : set_f( onset_filter_subframe, 1.f, hSpatParamRendCom->num_freq_bands );
2346 0 : p_onset_filter = onset_filter_subframe;
2347 : }
2348 : else
2349 : {
2350 : /* no frequency domain decorrelation: use prototype frame */
2351 9600 : hDirACRend->proto_frame_dec_f = hDirACRend->proto_frame_f;
2352 9600 : p_onset_filter = NULL;
2353 : }
2354 : }
2355 :
2356 : /*-----------------------------------------------------------------*
2357 : * output synthesis
2358 : *-----------------------------------------------------------------*/
2359 :
2360 192000 : if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_LS || hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_SHD )
2361 : {
2362 : /* Compute diffuse prototypes */
2363 153600 : ivas_dirac_dec_compute_diffuse_proto( hDirACRend, hSpatParamRendCom->num_freq_bands, slot_idx );
2364 : }
2365 :
2366 192000 : ivas_dirac_dec_output_synthesis_process_slot( reference_power,
2367 : p_onset_filter,
2368 : azimuth,
2369 : elevation,
2370 192000 : hSpatParamRendCom->diffuseness_vector[md_idx],
2371 : hSpatParamRendCom,
2372 : hDirACRend,
2373 : hMasaExtRend->hVBAPdata,
2374 : hDirACRend->hOutSetup,
2375 : nchan_transport,
2376 : md_idx,
2377 : 0,
2378 : 0 );
2379 :
2380 192000 : if ( hDirACRend->synthesisConf != DIRAC_SYNTHESIS_GAIN_SHD )
2381 : {
2382 163200 : v_add( reference_power, reference_power_smooth, reference_power_smooth, hSpatParamRendCom->num_freq_bands );
2383 : }
2384 : }
2385 :
2386 48000 : ivas_dirac_dec_output_synthesis_get_interpolator( &hDirACRend->h_output_synthesis_psd_params, hSpatParamRendCom->subframe_nbslots[subframe_idx] );
2387 :
2388 :
2389 48000 : if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
2390 : {
2391 7200 : ivas_dirac_dec_output_synthesis_process_subframe_gain_shd( Cldfb_RealBuffer,
2392 : Cldfb_ImagBuffer,
2393 : hSpatParamRendCom,
2394 : hDirACRend,
2395 : nchan_transport,
2396 7200 : hSpatParamRendCom->subframe_nbslots[subframe_idx],
2397 : p_onset_filter,
2398 : diffuseness_vector,
2399 : 0,
2400 : 0 );
2401 : }
2402 : else
2403 : {
2404 40800 : ivas_dirac_dec_output_synthesis_process_subframe_psd_ls( Cldfb_RealBuffer,
2405 : Cldfb_ImagBuffer,
2406 : hSpatParamRendCom,
2407 : hDirACRend,
2408 40800 : hSpatParamRendCom->subframe_nbslots[subframe_idx],
2409 : diffuseness_vector,
2410 : reference_power_smooth,
2411 : 1.0f,
2412 : 0 );
2413 : }
2414 :
2415 : /*-----------------------------------------------------------------*
2416 : * CLDFB synthesis (and binaural rendering)
2417 : *-----------------------------------------------------------------*/
2418 :
2419 48000 : index_slot = slot_idx_start_cldfb_synth;
2420 :
2421 : {
2422 : float *RealBuffer[MAX_PARAM_SPATIAL_SUBFRAMES];
2423 : float *ImagBuffer[MAX_PARAM_SPATIAL_SUBFRAMES];
2424 : int16_t outchannels;
2425 :
2426 48000 : idx_in = 0;
2427 48000 : idx_lfe = 0;
2428 :
2429 48000 : outchannels = hDirACRend->hOutSetup.nchan_out_woLFE + hDirACRend->hOutSetup.num_lfe;
2430 :
2431 : /* Note here that compared to decoder path, there is no separate channel ever for MASA ext rend path */
2432 :
2433 487200 : for ( ch = 0; ch < outchannels; ch++ )
2434 : {
2435 439200 : if ( ( hDirACRend->hOutSetup.num_lfe > 0 ) && ( hDirACRend->hOutSetup.index_lfe[idx_lfe] == ch ) )
2436 : {
2437 : /* No LFE for MASA rendering */
2438 24000 : set_zero( &( output_f[ch][index_slot * hSpatParamRendCom->num_freq_bands] ), hSpatParamRendCom->subframe_nbslots[subframe_idx] * hSpatParamRendCom->num_freq_bands );
2439 :
2440 24000 : if ( idx_lfe < ( hDirACRend->hOutSetup.num_lfe - 1 ) )
2441 : {
2442 0 : idx_lfe++;
2443 : }
2444 : }
2445 : else
2446 : {
2447 : /* open CLDFB buffer up to CLDFB_NO_CHANNELS_MAX bands for 48kHz */
2448 2076000 : for ( i = 0; i < hSpatParamRendCom->subframe_nbslots[subframe_idx]; i++ )
2449 : {
2450 1660800 : RealBuffer[i] = Cldfb_RealBuffer[idx_in][i];
2451 1660800 : ImagBuffer[i] = Cldfb_ImagBuffer[idx_in][i];
2452 : }
2453 415200 : cldfbSynthesis( RealBuffer, ImagBuffer, &( output_f[ch][index_slot * hSpatParamRendCom->num_freq_bands] ), hSpatParamRendCom->num_freq_bands * hSpatParamRendCom->subframe_nbslots[subframe_idx], hMasaExtRend->cldfbSynRend[idx_in] );
2454 415200 : idx_in++;
2455 : }
2456 : }
2457 : }
2458 48000 : hSpatParamRendCom->slots_rendered += hSpatParamRendCom->subframe_nbslots[subframe_idx];
2459 48000 : hSpatParamRendCom->subframes_rendered++;
2460 :
2461 48000 : pop_wmops();
2462 :
2463 48000 : return;
2464 : }
2465 :
2466 27300 : void ivas_masa_ext_dirac_render(
2467 : MASA_EXT_REND_HANDLE hMasaExtRend, /* i/o: MASA renderer structure */
2468 : float *output_f[], /* i/o: input/output signals in time domain */
2469 : const int16_t num_subframes /* i : number of subframes to render */
2470 : )
2471 : {
2472 : int16_t subframe_idx;
2473 : float *output_f_local[MAX_OUTPUT_CHANNELS];
2474 : int16_t n, n_samples_sf;
2475 : SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
2476 :
2477 27300 : hSpatParamRendCom = hMasaExtRend->hSpatParamRendCom;
2478 :
2479 27300 : n_samples_sf = JBM_CLDFB_SLOTS_IN_SUBFRAME * hSpatParamRendCom->slot_size;
2480 :
2481 464100 : for ( n = 0; n < MAX_OUTPUT_CHANNELS; n++ )
2482 : {
2483 436800 : output_f_local[n] = output_f[n];
2484 : }
2485 :
2486 27300 : hSpatParamRendCom->subframes_rendered = hSpatParamRendCom->dirac_read_idx;
2487 :
2488 75300 : for ( subframe_idx = 0; subframe_idx < num_subframes; subframe_idx++ )
2489 : {
2490 48000 : hSpatParamRendCom->slots_rendered = 0;
2491 48000 : ivas_masa_ext_dirac_render_sf( hMasaExtRend, output_f_local );
2492 816000 : for ( n = 0; n < MAX_OUTPUT_CHANNELS; n++ )
2493 : {
2494 768000 : output_f_local[n] += n_samples_sf;
2495 : }
2496 :
2497 48000 : hSpatParamRendCom->dirac_read_idx = ( hSpatParamRendCom->dirac_read_idx + 1 ) % hSpatParamRendCom->dirac_md_buffer_length;
2498 : }
2499 :
2500 27300 : return;
2501 : }
|