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_cnst.h"
41 : #include "ivas_rom_com.h"
42 : #ifdef DEBUGGING
43 : #include "debug.h"
44 : #endif
45 : #include "wmc_auto.h"
46 :
47 : /*-------------------------------------------------------------------------
48 : * Local function prototypes
49 : *------------------------------------------------------------------------*/
50 :
51 : static void computeIntensityVector_enc( DIRAC_ENC_HANDLE hDirAC, float Cldfb_RealBuffer[DIRAC_MAX_ANA_CHANS][DIRAC_NO_FB_BANDS_MAX], float Cldfb_ImagBuffer[DIRAC_MAX_ANA_CHANS][DIRAC_NO_FB_BANDS_MAX], const int16_t enc_param_start_band, const int16_t num_frequency_bands, float intensity_real[DIRAC_NUM_DIMS][DIRAC_MAX_NBANDS] );
52 :
53 :
54 : /*-------------------------------------------------------------------------
55 : * ivas_dirac_enc_open()
56 : *
57 : * Initialize DirAC handle
58 : *------------------------------------------------------------------------*/
59 :
60 281 : ivas_error ivas_dirac_enc_open(
61 : Encoder_Struct *st_ivas /* i/o: IVAS encoder handle */
62 : )
63 : {
64 : int16_t i, j;
65 : DIRAC_ENC_HANDLE hDirAC;
66 : int32_t dirac_slot_ns;
67 : ivas_error error;
68 :
69 281 : error = IVAS_ERR_OK;
70 :
71 281 : if ( ( hDirAC = (DIRAC_ENC_HANDLE) malloc( sizeof( DIRAC_ENC_DATA ) ) ) == NULL )
72 : {
73 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
74 : }
75 :
76 281 : if ( ( hDirAC->hConfig = (DIRAC_CONFIG_DATA_HANDLE) malloc( sizeof( DIRAC_CONFIG_DATA ) ) ) == NULL )
77 : {
78 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC Config\n" ) );
79 : }
80 281 : hDirAC->firstrun_sector_params = 1;
81 281 : set_zero( hDirAC->sec_I_vec_smth_x[0], NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
82 281 : set_zero( hDirAC->sec_I_vec_smth_y[0], NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
83 281 : set_zero( hDirAC->sec_I_vec_smth_z[0], NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
84 281 : set_zero( hDirAC->azi_prev, NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
85 281 : set_zero( hDirAC->ele_prev, NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
86 281 : set_zero( hDirAC->energy_smth[0], NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
87 : /*-----------------------------------------------------------------*
88 : * DirAC main configuration
89 : *-----------------------------------------------------------------*/
90 :
91 281 : st_ivas->hDirAC = hDirAC;
92 :
93 281 : if ( ( error = ivas_dirac_config( (void *) st_ivas, ENC ) ) != IVAS_ERR_OK )
94 : {
95 0 : return error;
96 : }
97 :
98 : /* set FB config. */
99 281 : hDirAC->hFbMixer = NULL;
100 :
101 3934 : for ( i = 0; i < DIRAC_MAX_NBANDS + 1; i++ )
102 : {
103 : /* adapt band grouping to from CLDFB to MDFT bands */
104 3653 : hDirAC->band_grouping[i] *= CLDFB_TO_MDFT_FAC;
105 : }
106 281 : dirac_slot_ns = DIRAC_SLOT_ENC_NS;
107 :
108 : /* initialize delay for SPAR/DirAC delay synchronization */
109 :
110 : /* intensity 3-dim */
111 1124 : for ( i = 0; i < DIRAC_NUM_DIMS; i++ )
112 : {
113 843 : if ( ( hDirAC->direction_vector_m[i] = (float **) malloc( MAX_PARAM_SPATIAL_SUBFRAMES * sizeof( float * ) ) ) == NULL )
114 : {
115 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
116 : }
117 :
118 4215 : for ( j = 0; j < MAX_PARAM_SPATIAL_SUBFRAMES; j++ )
119 : {
120 3372 : if ( ( hDirAC->direction_vector_m[i][j] = (float *) malloc( DIRAC_MAX_NBANDS * sizeof( float ) ) ) == NULL )
121 : {
122 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
123 : }
124 3372 : set_f( hDirAC->direction_vector_m[i][j], 0.0f, DIRAC_MAX_NBANDS );
125 : }
126 : }
127 :
128 281 : hDirAC->no_col_avg_diff = (int16_t) ( DIRAC_NO_COL_AVG_DIFF_NS / dirac_slot_ns );
129 1124 : for ( i = 0; i < DIRAC_NUM_DIMS; i++ )
130 : {
131 843 : if ( ( hDirAC->buffer_intensity_real[i] = (float **) malloc( hDirAC->no_col_avg_diff * sizeof( float * ) ) ) == NULL )
132 : {
133 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
134 : }
135 7587 : for ( j = 0; j < hDirAC->no_col_avg_diff; j++ )
136 : {
137 6744 : if ( ( hDirAC->buffer_intensity_real[i][j] = (float *) malloc( DIRAC_MAX_NBANDS * sizeof( float ) ) ) == NULL )
138 : {
139 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
140 : }
141 6744 : set_f( hDirAC->buffer_intensity_real[i][j], 0.0f, DIRAC_MAX_NBANDS );
142 : }
143 : }
144 :
145 281 : if ( ( hDirAC->buffer_energy = (float *) malloc( DIRAC_MAX_NBANDS * hDirAC->no_col_avg_diff * sizeof( float ) ) ) == NULL )
146 : {
147 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
148 : }
149 281 : set_f( hDirAC->buffer_energy, 0.0f, DIRAC_MAX_NBANDS * hDirAC->no_col_avg_diff );
150 :
151 281 : if ( st_ivas->hQMetaData->useLowerRes )
152 : {
153 37 : hDirAC->block_grouping[0] = 0;
154 37 : hDirAC->block_grouping[1] = MAX_PARAM_SPATIAL_SUBFRAMES;
155 : }
156 : else
157 : {
158 244 : mvs2s( DirAC_block_grouping_5ms_MDFT, hDirAC->block_grouping, MAX_PARAM_SPATIAL_SUBFRAMES + 1 );
159 : }
160 :
161 281 : hDirAC->index_buffer_intensity = 0;
162 281 : hDirAC->mono_frame_count = 0;
163 :
164 281 : st_ivas->hDirAC = hDirAC;
165 281 : st_ivas->hSpar->enc_param_start_band = st_ivas->hDirAC->hConfig->enc_param_start_band;
166 :
167 281 : return error;
168 : }
169 :
170 :
171 : /*-------------------------------------------------------------------------
172 : * ivas_dirac_enc_reconfigure()
173 : *
174 : * Reconfigure DirAC handle
175 : *------------------------------------------------------------------------*/
176 :
177 1673 : ivas_error ivas_dirac_enc_reconfigure(
178 : Encoder_Struct *st_ivas /* i/o: IVAS encoder handle */
179 : )
180 : {
181 : DIRAC_ENC_HANDLE hDirAC;
182 : ivas_error error;
183 :
184 1673 : error = IVAS_ERR_OK;
185 :
186 1673 : hDirAC = st_ivas->hDirAC;
187 :
188 : /*-----------------------------------------------------------------*
189 : * DirAC main configuration
190 : *-----------------------------------------------------------------*/
191 :
192 1673 : if ( ( error = ivas_dirac_config( (void *) st_ivas, ENC ) ) != IVAS_ERR_OK )
193 : {
194 0 : return error;
195 : }
196 :
197 1673 : if ( st_ivas->hQMetaData->useLowerRes )
198 : {
199 359 : hDirAC->block_grouping[0] = 0;
200 359 : hDirAC->block_grouping[1] = MAX_PARAM_SPATIAL_SUBFRAMES;
201 : }
202 : else
203 : {
204 1314 : mvs2s( DirAC_block_grouping_5ms_MDFT, hDirAC->block_grouping, MAX_PARAM_SPATIAL_SUBFRAMES + 1 );
205 : }
206 :
207 1673 : return error;
208 : }
209 :
210 :
211 : /*-------------------------------------------------------------------------
212 : * ivas_dirac_enc_close()
213 : *
214 : * Close DirAC encoder handle
215 : *------------------------------------------------------------------------*/
216 :
217 627 : void ivas_dirac_enc_close(
218 : DIRAC_ENC_HANDLE *hDirAC_out, /* i/o: encoder DirAC handle */
219 : const int32_t input_Fs /* i : input sampling rate */
220 : )
221 : {
222 : int16_t i, j;
223 : DIRAC_ENC_HANDLE hDirAC;
224 :
225 627 : if ( hDirAC_out == NULL || *hDirAC_out == NULL )
226 : {
227 346 : return;
228 : }
229 :
230 281 : hDirAC = *hDirAC_out;
231 :
232 281 : if ( hDirAC->hFbMixer != NULL )
233 : {
234 0 : ivas_FB_mixer_close( &hDirAC->hFbMixer, input_Fs, 0 );
235 : }
236 : /* intensity 3-dim */
237 1124 : for ( i = 0; i < DIRAC_NUM_DIMS; i++ )
238 : {
239 4215 : for ( j = 0; j < MAX_PARAM_SPATIAL_SUBFRAMES; j++ )
240 : {
241 3372 : free( hDirAC->direction_vector_m[i][j] );
242 3372 : hDirAC->direction_vector_m[i][j] = NULL;
243 : }
244 :
245 7587 : for ( j = 0; j < hDirAC->no_col_avg_diff; j++ )
246 : {
247 6744 : if ( hDirAC->buffer_intensity_real[i][j] != 0 )
248 : {
249 6744 : free( hDirAC->buffer_intensity_real[i][j] );
250 6744 : hDirAC->buffer_intensity_real[i][j] = NULL;
251 : }
252 : }
253 :
254 843 : free( hDirAC->buffer_intensity_real[i] );
255 843 : hDirAC->buffer_intensity_real[i] = NULL;
256 :
257 843 : free( hDirAC->direction_vector_m[i] );
258 843 : hDirAC->direction_vector_m[i] = NULL;
259 : }
260 :
261 281 : free( hDirAC->buffer_energy );
262 281 : hDirAC->buffer_energy = NULL;
263 :
264 281 : if ( hDirAC->hConfig != NULL )
265 : {
266 281 : free( hDirAC->hConfig );
267 281 : hDirAC->hConfig = NULL;
268 : }
269 :
270 281 : free( *hDirAC_out );
271 281 : *hDirAC_out = NULL;
272 :
273 281 : return;
274 : }
275 :
276 :
277 : /*-------------------------------------------------------------------------
278 : * ivas_dirac_enc()
279 : *
280 : * DirAC Encoder
281 : *
282 : *------------------------------------------------------------------------*/
283 :
284 159500 : ivas_error ivas_dirac_enc(
285 : DIRAC_ENC_HANDLE hDirAC, /* i/o: encoder DirAC handle */
286 : IVAS_QMETADATA_HANDLE hQMetaData, /* i/o: q_metadata handle */
287 : BSTR_ENC_HANDLE hMetaData, /* i/o: Metadata bitstream handle */
288 : float *data_f[], /* i/o: SBA channels */
289 : float **ppIn_FR_real, /* o : real freq domain values */
290 : float **ppIn_FR_imag, /* o : imag freq domain values */
291 : const int16_t input_frame, /* i : input frame length */
292 : const int16_t dtx_vad, /* i : DTX vad flag */
293 : const IVAS_FORMAT ivas_format, /* i : ivas format */
294 : const int16_t nchan_transport, /* i : number of transport channels */
295 : const int16_t hodirac_flag /* i : hodirac flag */
296 : )
297 : {
298 : int16_t orig_dirac_bands;
299 : float dir[3], avg_dir[3];
300 : float energySum, vecLen;
301 : int16_t i, j, b, i_ts;
302 : ivas_error error;
303 159500 : push_wmops( "ivas_dirac_enc" );
304 :
305 159500 : ivas_dirac_param_est_enc( hDirAC, hQMetaData->q_direction, hQMetaData->useLowerRes, data_f, ppIn_FR_real, ppIn_FR_imag, input_frame, ivas_format, hodirac_flag, hodirac_flag ? HOA2_CHANNELS : FOA_CHANNELS, &( hDirAC->mono_frame_count ), &( hQMetaData->dirac_mono_flag ) );
306 :
307 159500 : if ( hQMetaData->q_direction->cfg.nbands > 0 )
308 : {
309 159500 : orig_dirac_bands = hQMetaData->q_direction[0].cfg.nbands;
310 :
311 159500 : if ( dtx_vad == 1 )
312 : {
313 : /* WB 4TC mode bit : disable for now*/
314 155673 : push_next_indice( hMetaData, 0, 1 );
315 :
316 : /* Set Energy Ratio to 0.0 if the mono flag is set, before the metadata is encoded */
317 155673 : if ( hQMetaData->dirac_mono_flag )
318 : {
319 0 : for ( b = hQMetaData->q_direction->cfg.start_band; b < hQMetaData->q_direction->cfg.nbands; b++ )
320 : {
321 0 : for ( i_ts = 0; i_ts < ( ( dtx_vad == 1 ) ? hQMetaData->q_direction[0].cfg.nblocks : 1 ); i_ts++ )
322 : {
323 0 : hQMetaData->q_direction[0].band_data[b].energy_ratio[i_ts] = 0.0f;
324 0 : hQMetaData->q_direction[0].band_data[b].azimuth[i_ts] = 0.0f;
325 0 : hQMetaData->q_direction[0].band_data[b].elevation[i_ts] = 0.0f;
326 : }
327 : }
328 : }
329 :
330 155673 : if ( ( error = ivas_qmetadata_enc_encode( hMetaData, hQMetaData, hodirac_flag ) ) != IVAS_ERR_OK )
331 : {
332 0 : return error;
333 : }
334 : }
335 : else
336 : {
337 3827 : hQMetaData->q_direction[0].cfg.nbands = DIRAC_DTX_BANDS;
338 :
339 : /* compute directions */
340 16459 : for ( i = 0; i < hQMetaData->q_direction[0].cfg.nblocks; i++ )
341 : {
342 12632 : set_zero( dir, 3 );
343 12632 : set_zero( avg_dir, 3 );
344 12632 : energySum = 0.0f;
345 :
346 : /* combine all DirAC bands except the last one, handle last band separately, last band covers BW above WB */
347 37004 : for ( j = 0; j < orig_dirac_bands - 1; j++ )
348 : {
349 24372 : ivas_qmetadata_azimuth_elevation_to_direction_vector( hQMetaData->q_direction[0].band_data[j].azimuth[i], hQMetaData->q_direction[0].band_data[j].elevation[i], &dir[0] );
350 24372 : vecLen = hQMetaData->q_direction[0].band_data[j].energy_ratio[i] * hDirAC->buffer_energy[i * orig_dirac_bands + j];
351 :
352 24372 : avg_dir[0] += dir[0] * vecLen;
353 24372 : avg_dir[1] += dir[1] * vecLen;
354 24372 : avg_dir[2] += dir[2] * vecLen;
355 :
356 24372 : energySum += hDirAC->buffer_energy[i * orig_dirac_bands + j];
357 : }
358 :
359 12632 : ivas_qmetadata_direction_vector_to_azimuth_elevation( &avg_dir[0], &hQMetaData->q_direction[0].band_data[0].azimuth[i], &hQMetaData->q_direction[0].band_data[0].elevation[i] );
360 12632 : hQMetaData->q_direction[0].band_data[0].energy_ratio[i] = sqrtf( dotp( avg_dir, avg_dir, 3 ) ) / ( energySum + EPSILON );
361 :
362 12632 : hQMetaData->q_direction[0].band_data[1].azimuth[i] = hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].azimuth[i];
363 12632 : hQMetaData->q_direction[0].band_data[1].elevation[i] = hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].elevation[i];
364 12632 : hQMetaData->q_direction[0].band_data[1].energy_ratio[i] = hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].energy_ratio[i];
365 : }
366 :
367 : /* 1 bit to indicate mode MD coding : temp solution*/
368 3827 : push_next_indice( hMetaData, 1, 1 );
369 :
370 : /* encode SID parameters */
371 3827 : ivas_qmetadata_enc_sid_encode( hMetaData, hQMetaData, -1, nchan_transport, SBA_FORMAT );
372 : }
373 :
374 751432 : for ( b = hQMetaData->q_direction->cfg.start_band; b < hQMetaData->q_direction->cfg.nbands; b++ )
375 : {
376 2860732 : for ( i_ts = 0; i_ts < ( ( dtx_vad == 1 ) ? hQMetaData->q_direction[0].cfg.nblocks : 1 ); i_ts++ )
377 : {
378 2268800 : hQMetaData->q_direction->band_data[b].azimuth[i_ts] = hQMetaData->q_direction->band_data[b].q_azimuth[i_ts];
379 2268800 : hQMetaData->q_direction->band_data[b].elevation[i_ts] = hQMetaData->q_direction->band_data[b].q_elevation[i_ts];
380 2268800 : hQMetaData->q_direction[0].band_data[b].energy_ratio[0] = 1.0f - diffuseness_reconstructions[hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0]];
381 : }
382 : }
383 :
384 159500 : if ( dtx_vad == 0 )
385 : {
386 19135 : for ( i = 0; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
387 : {
388 15308 : hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].azimuth[i] = hQMetaData->q_direction[0].band_data[1].azimuth[0];
389 15308 : hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].elevation[i] = hQMetaData->q_direction[0].band_data[1].elevation[0];
390 15308 : hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].energy_ratio[i] = hQMetaData->q_direction[0].band_data[1].energy_ratio[0];
391 : }
392 :
393 19135 : for ( i = 0; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
394 : {
395 42356 : for ( j = orig_dirac_bands - 2; j >= 0; j-- )
396 : {
397 27048 : hQMetaData->q_direction[0].band_data[j].azimuth[i] = hQMetaData->q_direction[0].band_data[0].azimuth[0];
398 27048 : hQMetaData->q_direction[0].band_data[j].elevation[i] = hQMetaData->q_direction[0].band_data[0].elevation[0];
399 27048 : hQMetaData->q_direction[0].band_data[j].energy_ratio[i] = hQMetaData->q_direction[0].band_data[0].energy_ratio[0];
400 : }
401 : }
402 :
403 3827 : hQMetaData->q_direction->cfg.nbands = orig_dirac_bands;
404 : }
405 : }
406 159500 : pop_wmops();
407 :
408 159500 : return IVAS_ERR_OK;
409 : }
410 :
411 :
412 : /*-------------------------------------------------------------------------
413 : * ivas_dirac_get_mono_flag()
414 : *
415 : *
416 : *-------------------------------------------------------------------------*/
417 :
418 638000 : static int16_t ivas_dirac_get_mono_flag(
419 : const int16_t *band_grouping, /* i : Band grouping for estimation */
420 : float Cldfb_RealBuffer[DIRAC_MAX_ANA_CHANS][DIRAC_NO_FB_BANDS_MAX], /* i : Real part of input signal */
421 : float Cldfb_ImagBuffer[DIRAC_MAX_ANA_CHANS][DIRAC_NO_FB_BANDS_MAX], /* i : Imag part of input signal */
422 : const int16_t nchan_ana, /* i : number of analysis channels */
423 : int16_t *mono_frame_count ) /* i/o: current number of mono frames count */
424 : {
425 : int16_t brange[2];
426 : int16_t i, j, ch_idx;
427 : float other_ch_band_power;
428 : float W_band_power;
429 638000 : int16_t any_mc_band = 0;
430 638000 : int16_t any_mono_band = 0;
431 638000 : int16_t local_mono_flag = 0;
432 : float W_band_power_norm;
433 638000 : float threshold = 0;
434 :
435 : /* Banded Power Calculations */
436 8294000 : for ( i = 0; i < DIRAC_MAX_NBANDS; i++ )
437 : {
438 7656000 : W_band_power = 0;
439 7656000 : other_ch_band_power = 0;
440 :
441 7656000 : brange[0] = band_grouping[i];
442 7656000 : brange[1] = band_grouping[i + 1];
443 :
444 : /* Loop over the W channel bins to calculate the power in the band */
445 116721600 : for ( j = brange[0]; j < brange[1]; j++ )
446 : {
447 109065600 : W_band_power += ( Cldfb_RealBuffer[0][j] * Cldfb_RealBuffer[0][j] ) + ( Cldfb_ImagBuffer[0][j] * Cldfb_ImagBuffer[0][j] );
448 : }
449 :
450 : /* Loop over the other channels and bins to calculate the power in the band */
451 30624000 : for ( ch_idx = 1; ch_idx < nchan_ana; ch_idx++ )
452 : {
453 : /* abs()^2 */
454 350164800 : for ( j = brange[0]; j < brange[1]; j++ )
455 : {
456 327196800 : other_ch_band_power += ( Cldfb_RealBuffer[ch_idx][j] * Cldfb_RealBuffer[ch_idx][j] ) + ( Cldfb_ImagBuffer[ch_idx][j] * Cldfb_ImagBuffer[ch_idx][j] );
457 : }
458 : }
459 7656000 : if ( other_ch_band_power < EPSILON )
460 : {
461 748713 : if ( W_band_power > DIRAC_MONO_THRESH_SILENCE )
462 : {
463 0 : any_mono_band = 1;
464 : }
465 : }
466 : else
467 : {
468 6907287 : if ( ( W_band_power > DIRAC_MONO_THRESH_SILENCE ) || ( other_ch_band_power > DIRAC_MONO_THRESH_SILENCE ) )
469 : {
470 6270289 : W_band_power_norm = min( W_band_power, DIRAC_MONO_NORM_FACTOR ) * DIRAC_MONO_ONE_ON_NORM_FACTOR;
471 6270289 : threshold = max( W_band_power_norm * DIRAC_MONO_MAX_THRESH, DIRAC_MONO_MIN_THRESH );
472 6270289 : if ( W_band_power / other_ch_band_power > threshold )
473 : {
474 1439 : any_mono_band = 1;
475 : }
476 : else
477 : {
478 6268850 : any_mc_band = 1;
479 : }
480 : }
481 : }
482 : }
483 :
484 : /* If any band contains multi-channel content it's not mono */
485 638000 : if ( any_mc_band )
486 : {
487 624593 : local_mono_flag = 0;
488 : }
489 : else
490 : {
491 : /* If any band contains mono content the frame is mono. */
492 13407 : if ( any_mono_band )
493 : {
494 341 : local_mono_flag = 1;
495 : }
496 : }
497 :
498 : /* Hysteresis - only after DIRAC_MONO_FRAME_THRESH frames on mono will the actual mono flag be set */
499 638000 : if ( local_mono_flag )
500 : {
501 341 : if ( *mono_frame_count < DIRAC_MONO_FRAME_THRESH )
502 : {
503 341 : ( *mono_frame_count )++;
504 : }
505 : }
506 : else
507 : {
508 : /* Instantaneously disable actual mono flag if multi-channel content is observed */
509 637659 : if ( any_mc_band )
510 : {
511 624593 : *mono_frame_count = 0;
512 : }
513 : }
514 :
515 : /* Final check if there has been mono for DIRAC_MONO_FRAME_THRESH number of frames than the content is declared mono */
516 638000 : if ( *mono_frame_count == DIRAC_MONO_FRAME_THRESH )
517 : {
518 0 : return 1;
519 : }
520 : else
521 : {
522 638000 : return 0;
523 : }
524 : }
525 :
526 :
527 : /*-------------------------------------------------------------------------
528 : * computeReferencePower_enc()
529 : *
530 : *
531 : *-------------------------------------------------------------------------*/
532 :
533 684720 : void computeReferencePower_enc(
534 : const int16_t *band_grouping, /* i : Band grouping for estimation */
535 : float Cldfb_RealBuffer[][DIRAC_NO_FB_BANDS_MAX], /* i : Real part of input signal */
536 : float Cldfb_ImagBuffer[][DIRAC_NO_FB_BANDS_MAX], /* i : Imag part of input signal */
537 : float *reference_power, /* o : Estimated power */
538 : const int16_t enc_param_start_band, /* i : first band to process */
539 : const int16_t num_freq_bands, /* i : Number of frequency bands */
540 : const IVAS_FORMAT ivas_format, /* i : ivas_format */
541 : int16_t ref_power_w, /* i : use 0 if hodirac is enabled */
542 : const int16_t nchan_ana, /* i : number of analysis channels */
543 : int16_t *mono_frame_count, /* i/o: Mono Frame Count */
544 : int16_t *dirac_mono_flag /* i/o: Mono Flag */
545 : )
546 : {
547 : int16_t brange[2];
548 : int16_t ch_idx, i, j;
549 : float reference_power_W[DIRAC_MAX_NBANDS];
550 :
551 684720 : if ( dirac_mono_flag != NULL )
552 : {
553 638000 : *dirac_mono_flag = ivas_dirac_get_mono_flag( band_grouping, Cldfb_RealBuffer, Cldfb_ImagBuffer, nchan_ana, mono_frame_count );
554 : }
555 :
556 3297788 : for ( i = 0; i < num_freq_bands; i++ )
557 : {
558 2613068 : brange[0] = band_grouping[i + enc_param_start_band];
559 2613068 : brange[1] = band_grouping[i + enc_param_start_band + 1];
560 :
561 2613068 : reference_power[i] = 0;
562 :
563 2613068 : reference_power_W[i] = 0;
564 100063628 : for ( j = brange[0]; j < brange[1]; j++ )
565 : {
566 97450560 : reference_power_W[i] += ( Cldfb_RealBuffer[0][j] * Cldfb_RealBuffer[0][j] ) + ( Cldfb_ImagBuffer[0][j] * Cldfb_ImagBuffer[0][j] );
567 : }
568 2613068 : reference_power[i] += reference_power_W[i];
569 :
570 10452272 : for ( ch_idx = 1; ch_idx < nchan_ana; ch_idx++ )
571 : {
572 : /* abs()^2 */
573 300190884 : for ( j = brange[0]; j < brange[1]; j++ )
574 : {
575 292351680 : reference_power[i] += ( Cldfb_RealBuffer[ch_idx][j] * Cldfb_RealBuffer[ch_idx][j] ) + ( Cldfb_ImagBuffer[ch_idx][j] * Cldfb_ImagBuffer[ch_idx][j] );
576 : }
577 : }
578 : }
579 :
580 684720 : v_multc( reference_power, 0.5f, reference_power, num_freq_bands );
581 :
582 684720 : if ( ivas_format == SBA_FORMAT && ref_power_w == 1 )
583 : {
584 1768672 : for ( i = 0; i < num_freq_bands; i++ )
585 : {
586 1315832 : reference_power[i] = max( reference_power[i], reference_power_W[i] );
587 : }
588 : }
589 :
590 684720 : return;
591 : }
592 :
593 :
594 : /*-------------------------------------------------------------------------
595 : * ivas_dirac_param_est_enc()
596 : *
597 : *
598 : *------------------------------------------------------------------------*/
599 :
600 159500 : void ivas_dirac_param_est_enc(
601 : DIRAC_ENC_HANDLE hDirAC,
602 : IVAS_QDIRECTION *q_direction,
603 : const uint8_t useLowerRes,
604 : float *data_f[],
605 : float **pp_fr_real,
606 : float **pp_fr_imag,
607 : const int16_t input_frame,
608 : const IVAS_FORMAT ivas_format,
609 : const int16_t hodirac_flag,
610 : const int16_t nchan_fb_in,
611 : int16_t *mono_frame_count,
612 : int16_t *dirac_mono_flag )
613 : {
614 : int16_t i, d, ts, index, l_ts, num_freq_bands;
615 : int16_t band_m_idx, block_m_idx;
616 : float dir_v[DIRAC_NUM_DIMS];
617 : float *pcm_in[DIRAC_MAX_ANA_CHANS];
618 : float Cldfb_RealBuffer[DIRAC_MAX_ANA_CHANS][DIRAC_NO_FB_BANDS_MAX];
619 : float Cldfb_ImagBuffer[DIRAC_MAX_ANA_CHANS][DIRAC_NO_FB_BANDS_MAX];
620 : float *p_Cldfb_RealBuffer[DIRAC_MAX_ANA_CHANS];
621 : float *p_Cldfb_ImagBuffer[DIRAC_MAX_ANA_CHANS];
622 : float intensity_real[DIRAC_NUM_DIMS][DIRAC_MAX_NBANDS];
623 : float direction_vector[DIRAC_NUM_DIMS][DIRAC_MAX_NBANDS];
624 : float diffuseness_vector[DIRAC_MAX_NBANDS];
625 : float renormalization_factor[DIRAC_MAX_NBANDS];
626 : float renormalization_factor_diff[DIRAC_MAX_NBANDS];
627 : float norm_tmp;
628 : int16_t mrange[2];
629 : int16_t num_blocks;
630 : float reference_power[CLDFB_NO_COL_MAX][DIRAC_NO_FB_BANDS_MAX];
631 : float azi_secs[2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS];
632 : float ele_secs[2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS];
633 : float diff_secs[2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS];
634 : float ene_secs[2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS];
635 :
636 159500 : push_wmops( "dirac_enc_param_est" );
637 159500 : num_freq_bands = hDirAC->hConfig->nbands;
638 :
639 : /* Initialization */
640 159500 : l_ts = input_frame / MAX_PARAM_SPATIAL_SUBFRAMES;
641 159500 : if ( useLowerRes )
642 : {
643 13553 : q_direction->cfg.nblocks = 1;
644 13553 : num_blocks = 1;
645 : }
646 : else
647 : {
648 145947 : q_direction->cfg.nblocks = MAX_PARAM_SPATIAL_SUBFRAMES;
649 145947 : num_blocks = MAX_PARAM_SPATIAL_SUBFRAMES;
650 : }
651 :
652 : /* Need to initialize renormalization_factors, direction_m and diffuseness_m */
653 754367 : for ( i = 0; i < hDirAC->hConfig->nbands; i++ )
654 : {
655 594867 : renormalization_factor_diff[i] = 0;
656 594867 : hDirAC->diffuseness_m[i] = 0;
657 : }
658 :
659 159500 : set_zero( azi_secs, 2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS );
660 159500 : set_zero( ele_secs, 2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS );
661 159500 : set_zero( diff_secs, 2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS );
662 159500 : set_zero( ene_secs, 2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS );
663 :
664 : /* Copy current frame to memory for delay compensation */
665 878700 : for ( i = 0; i < nchan_fb_in; i++ )
666 : {
667 719200 : pcm_in[i] = data_f[i];
668 719200 : p_Cldfb_RealBuffer[i] = &Cldfb_RealBuffer[i][0];
669 719200 : p_Cldfb_ImagBuffer[i] = &Cldfb_ImagBuffer[i][0];
670 : }
671 :
672 : /* do processing over all CLDFB time slots */
673 756841 : for ( block_m_idx = 0; block_m_idx < num_blocks; block_m_idx++ )
674 : {
675 597341 : mrange[0] = hDirAC->block_grouping[block_m_idx];
676 597341 : mrange[1] = hDirAC->block_grouping[block_m_idx + 1];
677 :
678 2895491 : for ( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
679 : {
680 2298150 : renormalization_factor[band_m_idx] = EPSILON;
681 2298150 : hDirAC->direction_vector_m[0][block_m_idx][band_m_idx] = 0;
682 2298150 : hDirAC->direction_vector_m[1][block_m_idx][band_m_idx] = 0;
683 2298150 : hDirAC->direction_vector_m[2][block_m_idx][band_m_idx] = 0;
684 : }
685 :
686 1235341 : for ( ts = mrange[0]; ts < mrange[1]; ts++ )
687 : {
688 638000 : if ( hDirAC->hFbMixer )
689 : {
690 0 : ivas_fb_mixer_get_windowed_fr( hDirAC->hFbMixer, pcm_in, p_Cldfb_RealBuffer, p_Cldfb_ImagBuffer, l_ts, l_ts, hDirAC->hFbMixer->fb_cfg->num_in_chans );
691 :
692 0 : ivas_fb_mixer_update_prior_input( hDirAC->hFbMixer, pcm_in, l_ts, hDirAC->hFbMixer->fb_cfg->num_in_chans );
693 :
694 0 : for ( i = 0; i < nchan_fb_in; i++ )
695 : {
696 0 : pcm_in[i] += l_ts;
697 : }
698 : }
699 : else
700 : {
701 : #ifdef DEBUGGING
702 : assert( pp_fr_real );
703 : assert( pp_fr_imag );
704 : #endif
705 3514800 : for ( i = 0; i < nchan_fb_in; i++ )
706 : {
707 2876800 : mvr2r( &pp_fr_real[i][ts * l_ts], Cldfb_RealBuffer[i], l_ts );
708 2876800 : mvr2r( &pp_fr_imag[i][ts * l_ts], Cldfb_ImagBuffer[i], l_ts );
709 : }
710 : }
711 :
712 638000 : computeReferencePower_enc(
713 638000 : hDirAC->band_grouping,
714 : Cldfb_RealBuffer,
715 : Cldfb_ImagBuffer,
716 638000 : reference_power[ts],
717 638000 : hDirAC->hConfig->enc_param_start_band,
718 : num_freq_bands,
719 : ivas_format,
720 : hodirac_flag ? 0 : 1,
721 : FOA_CHANNELS,
722 : mono_frame_count,
723 : dirac_mono_flag );
724 :
725 638000 : computeIntensityVector_enc(
726 : hDirAC,
727 : Cldfb_RealBuffer,
728 : Cldfb_ImagBuffer,
729 638000 : hDirAC->hConfig->enc_param_start_band,
730 : num_freq_bands,
731 : intensity_real );
732 :
733 638000 : if ( !hodirac_flag )
734 : {
735 573040 : computeDirectionVectors(
736 : intensity_real[0],
737 : intensity_real[1],
738 : intensity_real[2],
739 573040 : hDirAC->hConfig->enc_param_start_band,
740 : num_freq_bands,
741 : direction_vector[0],
742 : direction_vector[1],
743 : direction_vector[2] );
744 : }
745 :
746 : /* fill buffers of length "averaging_length" time slots for intensity and energy */
747 638000 : hDirAC->index_buffer_intensity = ( hDirAC->index_buffer_intensity % hDirAC->no_col_avg_diff ) + 1; /* averaging_length = 32 */
748 638000 : index = hDirAC->index_buffer_intensity;
749 2552000 : for ( i = 0; i < DIRAC_NUM_DIMS; i++ )
750 : {
751 : /* only real part needed */
752 1914000 : mvr2r( intensity_real[i], &( hDirAC->buffer_intensity_real[i][index - 1][0] ), num_freq_bands );
753 : }
754 638000 : mvr2r( reference_power[ts], &( hDirAC->buffer_energy[( index - 1 ) * num_freq_bands] ), num_freq_bands );
755 :
756 638000 : computeDiffuseness_mdft( hDirAC->buffer_intensity_real, hDirAC->buffer_energy, num_freq_bands, hDirAC->no_col_avg_diff, diffuseness_vector );
757 :
758 638000 : if ( hodirac_flag )
759 : {
760 : #ifdef DEBUGGING
761 : assert( l_ts <= DIRAC_NO_FB_BANDS_MAX );
762 : #endif
763 64960 : calculate_hodirac_sector_parameters(
764 : hDirAC,
765 : Cldfb_RealBuffer,
766 : Cldfb_ImagBuffer,
767 : 0.20f,
768 64960 : hDirAC->band_grouping,
769 64960 : hDirAC->hConfig->nbands,
770 64960 : hDirAC->hConfig->enc_param_start_band,
771 : azi_secs,
772 : ele_secs,
773 : diff_secs,
774 : ene_secs );
775 : }
776 :
777 638000 : if ( hodirac_flag )
778 : {
779 779520 : for ( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
780 : {
781 714560 : hDirAC->diffuseness_m[band_m_idx] += reference_power[ts][band_m_idx] * diffuseness_vector[band_m_idx];
782 714560 : renormalization_factor_diff[band_m_idx] += reference_power[ts][band_m_idx];
783 : }
784 : }
785 : else
786 : {
787 2237948 : for ( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
788 : {
789 1664908 : norm_tmp = reference_power[ts][band_m_idx] * ( 1 - diffuseness_vector[band_m_idx] );
790 :
791 1664908 : hDirAC->direction_vector_m[0][block_m_idx][band_m_idx] += norm_tmp * direction_vector[0][band_m_idx];
792 1664908 : hDirAC->direction_vector_m[1][block_m_idx][band_m_idx] += norm_tmp * direction_vector[1][band_m_idx];
793 1664908 : hDirAC->direction_vector_m[2][block_m_idx][band_m_idx] += norm_tmp * direction_vector[2][band_m_idx];
794 1664908 : renormalization_factor[band_m_idx] += norm_tmp;
795 :
796 1664908 : hDirAC->diffuseness_m[band_m_idx] += reference_power[ts][band_m_idx] * diffuseness_vector[band_m_idx];
797 1664908 : renormalization_factor_diff[band_m_idx] += reference_power[ts][band_m_idx];
798 : }
799 : }
800 : }
801 :
802 597341 : if ( !hodirac_flag )
803 : {
804 2115971 : for ( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
805 : {
806 1583590 : renormalization_factor[band_m_idx] = EPSILON;
807 6334360 : for ( d = 0; d < DIRAC_NUM_DIMS; d++ )
808 : {
809 4750770 : renormalization_factor[band_m_idx] += ( hDirAC->direction_vector_m[d][block_m_idx][band_m_idx] * hDirAC->direction_vector_m[d][block_m_idx][band_m_idx] );
810 : }
811 1583590 : renormalization_factor[band_m_idx] = sqrtf( renormalization_factor[band_m_idx] );
812 :
813 1583590 : if ( renormalization_factor[band_m_idx] > EPSILON )
814 : {
815 1583590 : hDirAC->direction_vector_m[0][block_m_idx][band_m_idx] /= renormalization_factor[band_m_idx];
816 1583590 : hDirAC->direction_vector_m[1][block_m_idx][band_m_idx] /= renormalization_factor[band_m_idx];
817 1583590 : hDirAC->direction_vector_m[2][block_m_idx][band_m_idx] /= renormalization_factor[band_m_idx];
818 : }
819 : else
820 : {
821 0 : hDirAC->direction_vector_m[0][block_m_idx][band_m_idx] = 1;
822 0 : hDirAC->direction_vector_m[1][block_m_idx][band_m_idx] = 0;
823 0 : hDirAC->direction_vector_m[2][block_m_idx][band_m_idx] = 0;
824 : }
825 :
826 : /* save the elevation and azimuth values to be used later by the ivas_dirac_QuantizeParameters function */
827 6334360 : for ( d = 0; d < DIRAC_NUM_DIMS; d++ )
828 : {
829 4750770 : dir_v[d] = hDirAC->direction_vector_m[d][block_m_idx][band_m_idx];
830 : }
831 :
832 1583590 : ivas_qmetadata_direction_vector_to_azimuth_elevation(
833 : dir_v,
834 1583590 : &q_direction->band_data[band_m_idx].azimuth[block_m_idx],
835 1583590 : &q_direction->band_data[band_m_idx].elevation[block_m_idx] );
836 : }
837 : }
838 :
839 : /* Sectors */
840 597341 : if ( hodirac_flag )
841 : {
842 779520 : for ( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
843 : {
844 714560 : q_direction->band_data[band_m_idx].azimuth[block_m_idx] = azi_secs[band_m_idx];
845 714560 : q_direction->band_data[band_m_idx].elevation[block_m_idx] = ele_secs[band_m_idx];
846 : // q_direction->band_data[band_m_idx].energy_ratio[block_m_idx] = 1.f - diffuseness_vector[band_m_idx]; // set later
847 :
848 714560 : q_direction[1].band_data[band_m_idx].azimuth[block_m_idx] = azi_secs[num_freq_bands + band_m_idx];
849 714560 : q_direction[1].band_data[band_m_idx].elevation[block_m_idx] = ele_secs[num_freq_bands + band_m_idx];
850 714560 : q_direction[1].band_data[band_m_idx].energy_ratio[block_m_idx] = ( 1.f - diff_secs[band_m_idx] ) / ( ( 1.f - diff_secs[band_m_idx] ) + ( 1.f - diff_secs[num_freq_bands + band_m_idx] ) + EPSILON );
851 : }
852 : }
853 : }
854 :
855 : /* Diffuseness */
856 754367 : for ( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
857 : {
858 594867 : if ( renormalization_factor_diff[band_m_idx] > EPSILON )
859 : {
860 582738 : hDirAC->diffuseness_m[band_m_idx] /= renormalization_factor_diff[band_m_idx];
861 : }
862 : else
863 : {
864 12129 : hDirAC->diffuseness_m[band_m_idx] = 0.f;
865 : }
866 :
867 : /* set coherence to zero and convert diffuseness to energy ratio*/
868 :
869 594867 : set_f( q_direction->band_data[band_m_idx].energy_ratio, 1.f - hDirAC->diffuseness_m[band_m_idx], q_direction->cfg.nblocks );
870 :
871 2893017 : for ( block_m_idx = 0; block_m_idx < q_direction->cfg.nblocks; block_m_idx++ )
872 : {
873 2298150 : if ( q_direction->coherence_band_data != NULL )
874 : {
875 0 : q_direction->coherence_band_data[band_m_idx].spread_coherence[block_m_idx] = 0;
876 : }
877 : }
878 : }
879 :
880 : #ifdef DEBUG_SBA_MD_DUMP
881 : {
882 : char f_name[100];
883 : int16_t num_subframes = 1, num_block_groups = 1, num_elements = 1, byte_size = sizeof( float );
884 : sprintf( f_name, "dirac_enc_diffuseness.bin" );
885 : ( frame == 0 ) ? dbgwrite( &hDirAC->hConfig->nbands, sizeof( hDirAC->hConfig->nbands ), 1, 1, f_name ) : false;
886 : ( frame == 0 ) ? dbgwrite( &num_elements, sizeof( num_elements ), 1, 1, f_name ) : false;
887 : ( frame == 0 ) ? dbgwrite( &num_subframes, sizeof( num_subframes ), 1, 1, f_name ) : false;
888 : ( frame == 0 ) ? dbgwrite( &num_block_groups, sizeof( num_block_groups ), 1, 1, f_name ) : false;
889 : ( frame == 0 ) ? dbgwrite( &byte_size, sizeof( byte_size ), 1, 1, f_name ) : false;
890 : dbgwrite( hDirAC->diffuseness_m, sizeof( float ), hDirAC->hConfig->nbands, 1, f_name );
891 : sprintf( f_name, "dirac_reference_power.bin" );
892 : ( frame == 0 ) ? dbgwrite( &hDirAC->hConfig->nbands, sizeof( hDirAC->hConfig->nbands ), 1, 1, f_name ) : false;
893 : num_elements = hDirAC->block_grouping[1] - hDirAC->block_grouping[0];
894 : ( frame == 0 ) ? dbgwrite( &num_elements, sizeof( num_elements ), 1, 1, f_name ) : false;
895 : ( frame == 0 ) ? dbgwrite( &num_subframes, sizeof( num_subframes ), 1, 1, f_name ) : false;
896 : num_block_groups = hDirAC->block_grouping[1];
897 : ( frame == 0 ) ? dbgwrite( &num_block_groups, sizeof( num_block_groups ), 1, 1, f_name ) : false;
898 : ( frame == 0 ) ? dbgwrite( &byte_size, sizeof( byte_size ), 1, 1, f_name ) : false;
899 : for ( ts = hDirAC->block_grouping[0]; ts < hDirAC->block_grouping[1]; ts++ )
900 : {
901 : dbgwrite( reference_power[ts], sizeof( float ), hDirAC->hConfig->nbands, 1, f_name );
902 : }
903 : num_elements = 1;
904 : num_subframes = MAX_PARAM_SPATIAL_SUBFRAMES;
905 : num_block_groups = 1;
906 : sprintf( f_name, "dirac_enc_dir0.bin" );
907 : ( frame == 0 ) ? dbgwrite( &hDirAC->hConfig->nbands, sizeof( hDirAC->hConfig->nbands ), 1, 1, f_name ) : false;
908 : ( frame == 0 ) ? dbgwrite( &num_elements, sizeof( num_elements ), 1, 1, f_name ) : false;
909 : ( frame == 0 ) ? dbgwrite( &num_subframes, sizeof( num_subframes ), 1, 1, f_name ) : false;
910 : ( frame == 0 ) ? dbgwrite( &num_block_groups, sizeof( num_block_groups ), 1, 1, f_name ) : false;
911 : ( frame == 0 ) ? dbgwrite( &byte_size, sizeof( byte_size ), 1, 1, f_name ) : false;
912 : for ( block_m_idx = 0; block_m_idx < MAX_PARAM_SPATIAL_SUBFRAMES; block_m_idx++ )
913 : {
914 : dbgwrite( hDirAC->direction_vector_m[0][block_m_idx], sizeof( float ), hDirAC->hConfig->nbands, 1, f_name );
915 : }
916 : sprintf( f_name, "dirac_enc_dir1.bin" );
917 : ( frame == 0 ) ? dbgwrite( &hDirAC->hConfig->nbands, sizeof( hDirAC->hConfig->nbands ), 1, 1, f_name ) : false;
918 : ( frame == 0 ) ? dbgwrite( &num_elements, sizeof( num_elements ), 1, 1, f_name ) : false;
919 : ( frame == 0 ) ? dbgwrite( &num_subframes, sizeof( num_subframes ), 1, 1, f_name ) : false;
920 : ( frame == 0 ) ? dbgwrite( &num_block_groups, sizeof( num_block_groups ), 1, 1, f_name ) : false;
921 : ( frame == 0 ) ? dbgwrite( &byte_size, sizeof( byte_size ), 1, 1, f_name ) : false;
922 : for ( block_m_idx = 0; block_m_idx < MAX_PARAM_SPATIAL_SUBFRAMES; block_m_idx++ )
923 : {
924 : dbgwrite( hDirAC->direction_vector_m[1][block_m_idx], sizeof( float ), hDirAC->hConfig->nbands, 1, f_name );
925 : }
926 : sprintf( f_name, "dirac_enc_dir2.bin" );
927 : ( frame == 0 ) ? dbgwrite( &hDirAC->hConfig->nbands, sizeof( hDirAC->hConfig->nbands ), 1, 1, f_name ) : false;
928 : ( frame == 0 ) ? dbgwrite( &num_elements, sizeof( num_elements ), 1, 1, f_name ) : false;
929 : ( frame == 0 ) ? dbgwrite( &num_subframes, sizeof( num_subframes ), 1, 1, f_name ) : false;
930 : ( frame == 0 ) ? dbgwrite( &num_block_groups, sizeof( num_block_groups ), 1, 1, f_name ) : false;
931 : ( frame == 0 ) ? dbgwrite( &byte_size, sizeof( byte_size ), 1, 1, f_name ) : false;
932 : for ( block_m_idx = 0; block_m_idx < MAX_PARAM_SPATIAL_SUBFRAMES; block_m_idx++ )
933 : {
934 : dbgwrite( hDirAC->direction_vector_m[2][block_m_idx], sizeof( float ), hDirAC->hConfig->nbands, 1, f_name );
935 : }
936 : }
937 : #endif
938 : #ifdef DEBUG_MODE_DIRAC
939 : {
940 : static FILE *fp_direction_vector = NULL, *fp_diffuseness = NULL, *fp_referencePower = NULL;
941 :
942 : if ( fp_direction_vector == NULL )
943 : fp_direction_vector = fopen( "./res/dbg_direction_vector_C.bin", "wb" );
944 : if ( fp_diffuseness == NULL )
945 : fp_diffuseness = fopen( "./res/dbg_diffuseness_C.bin", "wb" );
946 : if ( fp_referencePower == NULL )
947 : fp_referencePower = fopen( "./res/dbg_reference_power_C.bin", "wb" );
948 :
949 : dbgwrite( hDirAC->diffuseness_m, sizeof( float ), hDirAC->hConfig->nbands, 1, "./res/dirac_enc_diffuseness.dat" );
950 : for ( block_m_idx = 0; block_m_idx < MAX_PARAM_SPATIAL_SUBFRAMES; block_m_idx++ )
951 : {
952 : dbgwrite( hDirAC->direction_vector_m[0][block_m_idx], sizeof( float ), hDirAC->hConfig->nbands, 1, "./res/dirac_enc_dir0.dat" );
953 : dbgwrite( hDirAC->direction_vector_m[1][block_m_idx], sizeof( float ), hDirAC->hConfig->nbands, 1, "./res/dirac_enc_dir1.dat" );
954 : dbgwrite( hDirAC->direction_vector_m[2][block_m_idx], sizeof( float ), hDirAC->hConfig->nbands, 1, "./res/dirac_enc_dir2.dat" );
955 :
956 : for ( ts = hDirAC->block_grouping[block_m_idx]; ts < hDirAC->block_grouping[block_m_idx + 1]; ts++ )
957 : {
958 : for ( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
959 : {
960 : for ( i = hDirAC->band_grouping[band_m_idx]; i < hDirAC->band_grouping[band_m_idx + 1]; i++ )
961 : {
962 : for ( d = 0; d < DIRAC_NUM_DIMS; d++ )
963 : {
964 : fwrite( &( hDirAC->direction_vector_m[d][block_m_idx][band_m_idx] ), sizeof( float ), 1, fp_direction_vector );
965 : }
966 : fwrite( &( hDirAC->diffuseness_m[band_m_idx] ), sizeof( float ), 1, fp_diffuseness );
967 : fwrite( &( reference_power[ts][band_m_idx] ), sizeof( float ), 1, fp_referencePower );
968 : }
969 : }
970 : }
971 : }
972 : }
973 : #endif
974 159500 : pop_wmops();
975 159500 : return;
976 : }
977 :
978 :
979 : /*-------------------------------------------------------------------------
980 : * computeIntensityVector_enc()
981 : *
982 : *
983 : *------------------------------------------------------------------------*/
984 :
985 638000 : static void computeIntensityVector_enc(
986 : DIRAC_ENC_HANDLE hDirAC,
987 : float Cldfb_RealBuffer[DIRAC_MAX_ANA_CHANS][DIRAC_NO_FB_BANDS_MAX], /* i : Real part of input signal */
988 : float Cldfb_ImagBuffer[DIRAC_MAX_ANA_CHANS][DIRAC_NO_FB_BANDS_MAX], /* i : Imag part of input signal */
989 : const int16_t enc_param_start_band,
990 : const int16_t num_frequency_bands,
991 : float intensity_real[DIRAC_NUM_DIMS][DIRAC_MAX_NBANDS] )
992 : {
993 : /* Reminder
994 : * X = a + ib; Y = c + id
995 : * X*Y = ac - bd + i(ad +bc)
996 : */
997 : int16_t i, j;
998 : float real, img;
999 : int16_t brange[2];
1000 :
1001 3017468 : for ( i = 0; i < num_frequency_bands; i++ )
1002 : {
1003 2379468 : brange[0] = hDirAC->band_grouping[i + enc_param_start_band];
1004 2379468 : brange[1] = hDirAC->band_grouping[i + enc_param_start_band + 1];
1005 :
1006 2379468 : intensity_real[0][i] = 0;
1007 2379468 : intensity_real[1][i] = 0;
1008 2379468 : intensity_real[2][i] = 0;
1009 :
1010 88681228 : for ( j = brange[0]; j < brange[1]; j++ )
1011 : {
1012 86301760 : real = Cldfb_RealBuffer[0][j];
1013 86301760 : img = Cldfb_ImagBuffer[0][j];
1014 86301760 : intensity_real[0][i] += Cldfb_RealBuffer[3][j] * real + Cldfb_ImagBuffer[3][j] * img;
1015 86301760 : intensity_real[1][i] += Cldfb_RealBuffer[1][j] * real + Cldfb_ImagBuffer[1][j] * img;
1016 86301760 : intensity_real[2][i] += Cldfb_RealBuffer[2][j] * real + Cldfb_ImagBuffer[2][j] * img;
1017 : }
1018 : }
1019 :
1020 638000 : return;
1021 : }
1022 :
1023 :
1024 : /*-------------------------------------------------------------------------
1025 : * computeDiffuseness_mdft()
1026 : *
1027 : *
1028 : *------------------------------------------------------------------------*/
1029 :
1030 684720 : void computeDiffuseness_mdft(
1031 : float **buffer_intensity[DIRAC_NUM_DIMS],
1032 : const float *buffer_energy,
1033 : const int16_t num_freq_bands,
1034 : const uint16_t no_col_avg_diff,
1035 : float *diffuseness )
1036 : {
1037 : float intensity_slow[DIRAC_NUM_DIMS * MASA_FREQUENCY_BANDS];
1038 : float intensity_slow_abs[MASA_FREQUENCY_BANDS];
1039 : float energy_slow[MASA_FREQUENCY_BANDS];
1040 : int16_t i, j, k;
1041 684720 : float tmp = 0;
1042 : float *p_tmp;
1043 : const float *p_tmp_c;
1044 :
1045 : /* Compute Intensity slow and energy slow */
1046 684720 : set_f( intensity_slow, 0.0f, DIRAC_NUM_DIMS * MASA_FREQUENCY_BANDS );
1047 684720 : set_f( intensity_slow_abs, 0.0f, MASA_FREQUENCY_BANDS );
1048 684720 : set_f( energy_slow, 0.0f, MASA_FREQUENCY_BANDS );
1049 :
1050 6162480 : for ( i = 0; i < no_col_avg_diff; ++i )
1051 : {
1052 : /* Energy slow */
1053 5477760 : p_tmp_c = buffer_energy + i * num_freq_bands;
1054 26382304 : for ( k = 0; k < num_freq_bands; k++ )
1055 : {
1056 20904544 : energy_slow[k] += *( p_tmp_c++ );
1057 : }
1058 :
1059 : /* Intensity slow */
1060 21911040 : for ( j = 0; j < DIRAC_NUM_DIMS; ++j )
1061 : {
1062 16433280 : p_tmp = buffer_intensity[j][i];
1063 79146912 : for ( k = 0; k < num_freq_bands; k++ )
1064 : {
1065 62713632 : intensity_slow[j * num_freq_bands + k] += *( p_tmp++ );
1066 : }
1067 : }
1068 : }
1069 :
1070 : /* intensity_slow.^2 + intensity_slow_abs*/
1071 2738880 : for ( j = 0; j < DIRAC_NUM_DIMS; ++j )
1072 : {
1073 2054160 : p_tmp = intensity_slow + j * num_freq_bands;
1074 9893364 : for ( k = 0; k < num_freq_bands; k++ )
1075 : {
1076 7839204 : *( p_tmp ) *= ( *p_tmp );
1077 7839204 : intensity_slow_abs[k] += *( p_tmp++ );
1078 : }
1079 : }
1080 :
1081 : /* Compute Diffuseness */
1082 684720 : p_tmp = intensity_slow_abs;
1083 3297788 : for ( i = 0; i < num_freq_bands; ++i )
1084 : {
1085 2613068 : tmp = sqrtf( *( p_tmp++ ) ) / ( energy_slow[i] + EPSILON );
1086 2613068 : tmp = 1.0f - tmp;
1087 2613068 : diffuseness[i] = ( ( tmp < 1.0f ) ? ( ( tmp < 0.0f ) ? 0.f : tmp ) : 1.0f );
1088 : }
1089 :
1090 684720 : return;
1091 : }
|