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 1943 : 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 1943 : error = IVAS_ERR_OK;
70 :
71 1943 : 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 1943 : 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 1943 : hDirAC->firstrun_sector_params = 1;
81 1943 : set_zero( hDirAC->sec_I_vec_smth_x[0], NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
82 1943 : set_zero( hDirAC->sec_I_vec_smth_y[0], NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
83 1943 : set_zero( hDirAC->sec_I_vec_smth_z[0], NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
84 1943 : set_zero( hDirAC->azi_prev, NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
85 1943 : set_zero( hDirAC->ele_prev, NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
86 1943 : set_zero( hDirAC->energy_smth[0], NUM_ANA_SECTORS * IVAS_MAX_NUM_BANDS );
87 : /*-----------------------------------------------------------------*
88 : * DirAC main configuration
89 : *-----------------------------------------------------------------*/
90 :
91 1943 : st_ivas->hDirAC = hDirAC;
92 :
93 1943 : if ( ( error = ivas_dirac_config( (void *) st_ivas, ENC ) ) != IVAS_ERR_OK )
94 : {
95 0 : return error;
96 : }
97 :
98 : /* set FB config. */
99 1943 : hDirAC->hFbMixer = NULL;
100 :
101 27202 : for ( i = 0; i < DIRAC_MAX_NBANDS + 1; i++ )
102 : {
103 : /* adapt band grouping to from CLDFB to MDFT bands */
104 25259 : hDirAC->band_grouping[i] *= CLDFB_TO_MDFT_FAC;
105 : }
106 1943 : dirac_slot_ns = DIRAC_SLOT_ENC_NS;
107 :
108 : /* initialize delay for SPAR/DirAC delay synchronization */
109 :
110 : /* intensity 3-dim */
111 7772 : for ( i = 0; i < DIRAC_NUM_DIMS; i++ )
112 : {
113 5829 : 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 29145 : for ( j = 0; j < MAX_PARAM_SPATIAL_SUBFRAMES; j++ )
119 : {
120 23316 : 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 23316 : set_f( hDirAC->direction_vector_m[i][j], 0.0f, DIRAC_MAX_NBANDS );
125 : }
126 : }
127 :
128 1943 : hDirAC->no_col_avg_diff = (int16_t) ( DIRAC_NO_COL_AVG_DIFF_NS / dirac_slot_ns );
129 7772 : for ( i = 0; i < DIRAC_NUM_DIMS; i++ )
130 : {
131 5829 : 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 52461 : for ( j = 0; j < hDirAC->no_col_avg_diff; j++ )
136 : {
137 46632 : 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 46632 : set_f( hDirAC->buffer_intensity_real[i][j], 0.0f, DIRAC_MAX_NBANDS );
142 : }
143 : }
144 :
145 1943 : 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 1943 : set_f( hDirAC->buffer_energy, 0.0f, DIRAC_MAX_NBANDS * hDirAC->no_col_avg_diff );
150 :
151 1943 : if ( st_ivas->hQMetaData->useLowerRes )
152 : {
153 358 : hDirAC->block_grouping[0] = 0;
154 358 : hDirAC->block_grouping[1] = MAX_PARAM_SPATIAL_SUBFRAMES;
155 : }
156 : else
157 : {
158 1585 : mvs2s( DirAC_block_grouping_5ms_MDFT, hDirAC->block_grouping, MAX_PARAM_SPATIAL_SUBFRAMES + 1 );
159 : }
160 :
161 1943 : hDirAC->index_buffer_intensity = 0;
162 1943 : hDirAC->mono_frame_count = 0;
163 :
164 1943 : st_ivas->hDirAC = hDirAC;
165 1943 : st_ivas->hSpar->enc_param_start_band = st_ivas->hDirAC->hConfig->enc_param_start_band;
166 :
167 1943 : return error;
168 : }
169 :
170 :
171 : /*-------------------------------------------------------------------------
172 : * ivas_dirac_enc_reconfigure()
173 : *
174 : * Reconfigure DirAC handle
175 : *------------------------------------------------------------------------*/
176 :
177 17694 : 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 17694 : error = IVAS_ERR_OK;
185 :
186 17694 : hDirAC = st_ivas->hDirAC;
187 :
188 : /*-----------------------------------------------------------------*
189 : * DirAC main configuration
190 : *-----------------------------------------------------------------*/
191 :
192 17694 : if ( ( error = ivas_dirac_config( (void *) st_ivas, ENC ) ) != IVAS_ERR_OK )
193 : {
194 0 : return error;
195 : }
196 :
197 17694 : if ( st_ivas->hQMetaData->useLowerRes )
198 : {
199 3057 : hDirAC->block_grouping[0] = 0;
200 3057 : hDirAC->block_grouping[1] = MAX_PARAM_SPATIAL_SUBFRAMES;
201 : }
202 : else
203 : {
204 14637 : mvs2s( DirAC_block_grouping_5ms_MDFT, hDirAC->block_grouping, MAX_PARAM_SPATIAL_SUBFRAMES + 1 );
205 : }
206 :
207 17694 : return error;
208 : }
209 :
210 :
211 : /*-------------------------------------------------------------------------
212 : * ivas_dirac_enc_close()
213 : *
214 : * Close DirAC encoder handle
215 : *------------------------------------------------------------------------*/
216 :
217 4269 : 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 4269 : if ( hDirAC_out == NULL || *hDirAC_out == NULL )
226 : {
227 2326 : return;
228 : }
229 :
230 1943 : hDirAC = *hDirAC_out;
231 :
232 1943 : if ( hDirAC->hFbMixer != NULL )
233 : {
234 0 : ivas_FB_mixer_close( &hDirAC->hFbMixer, input_Fs, 0 );
235 : }
236 : /* intensity 3-dim */
237 7772 : for ( i = 0; i < DIRAC_NUM_DIMS; i++ )
238 : {
239 29145 : for ( j = 0; j < MAX_PARAM_SPATIAL_SUBFRAMES; j++ )
240 : {
241 23316 : free( hDirAC->direction_vector_m[i][j] );
242 23316 : hDirAC->direction_vector_m[i][j] = NULL;
243 : }
244 :
245 52461 : for ( j = 0; j < hDirAC->no_col_avg_diff; j++ )
246 : {
247 46632 : if ( hDirAC->buffer_intensity_real[i][j] != 0 )
248 : {
249 46632 : free( hDirAC->buffer_intensity_real[i][j] );
250 46632 : hDirAC->buffer_intensity_real[i][j] = NULL;
251 : }
252 : }
253 :
254 5829 : free( hDirAC->buffer_intensity_real[i] );
255 5829 : hDirAC->buffer_intensity_real[i] = NULL;
256 :
257 5829 : free( hDirAC->direction_vector_m[i] );
258 5829 : hDirAC->direction_vector_m[i] = NULL;
259 : }
260 :
261 1943 : free( hDirAC->buffer_energy );
262 1943 : hDirAC->buffer_energy = NULL;
263 :
264 1943 : if ( hDirAC->hConfig != NULL )
265 : {
266 1943 : free( hDirAC->hConfig );
267 1943 : hDirAC->hConfig = NULL;
268 : }
269 :
270 1943 : free( *hDirAC_out );
271 1943 : *hDirAC_out = NULL;
272 :
273 1943 : return;
274 : }
275 :
276 :
277 : /*-------------------------------------------------------------------------
278 : * ivas_dirac_enc()
279 : *
280 : * DirAC Encoder
281 : *
282 : *------------------------------------------------------------------------*/
283 :
284 2510926 : 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 2510926 : push_wmops( "ivas_dirac_enc" );
304 :
305 2510926 : 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 2510926 : if ( hQMetaData->q_direction->cfg.nbands > 0 )
308 : {
309 2510926 : orig_dirac_bands = hQMetaData->q_direction[0].cfg.nbands;
310 :
311 2510926 : if ( dtx_vad == 1 )
312 : {
313 : /* WB 4TC mode bit : disable for now*/
314 2496817 : 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 2496817 : 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 2496817 : if ( ( error = ivas_qmetadata_enc_encode( hMetaData, hQMetaData, hodirac_flag ) ) != IVAS_ERR_OK )
331 : {
332 0 : return error;
333 : }
334 : }
335 : else
336 : {
337 14109 : hQMetaData->q_direction[0].cfg.nbands = DIRAC_DTX_BANDS;
338 :
339 : /* compute directions */
340 63087 : for ( i = 0; i < hQMetaData->q_direction[0].cfg.nblocks; i++ )
341 : {
342 48978 : set_zero( dir, 3 );
343 48978 : set_zero( avg_dir, 3 );
344 48978 : energySum = 0.0f;
345 :
346 : /* combine all DirAC bands except the last one, handle last band separately, last band covers BW above WB */
347 144448 : for ( j = 0; j < orig_dirac_bands - 1; j++ )
348 : {
349 95470 : 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 95470 : vecLen = hQMetaData->q_direction[0].band_data[j].energy_ratio[i] * hDirAC->buffer_energy[i * orig_dirac_bands + j];
351 :
352 95470 : avg_dir[0] += dir[0] * vecLen;
353 95470 : avg_dir[1] += dir[1] * vecLen;
354 95470 : avg_dir[2] += dir[2] * vecLen;
355 :
356 95470 : energySum += hDirAC->buffer_energy[i * orig_dirac_bands + j];
357 : }
358 :
359 48978 : 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 48978 : hQMetaData->q_direction[0].band_data[0].energy_ratio[i] = sqrtf( dotp( avg_dir, avg_dir, 3 ) ) / ( energySum + EPSILON );
361 :
362 48978 : hQMetaData->q_direction[0].band_data[1].azimuth[i] = hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].azimuth[i];
363 48978 : hQMetaData->q_direction[0].band_data[1].elevation[i] = hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].elevation[i];
364 48978 : 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 14109 : push_next_indice( hMetaData, 1, 1 );
369 :
370 : /* encode SID parameters */
371 14109 : ivas_qmetadata_enc_sid_encode( hMetaData, hQMetaData, -1, nchan_transport, SBA_FORMAT );
372 : }
373 :
374 11064118 : for ( b = hQMetaData->q_direction->cfg.start_band; b < hQMetaData->q_direction->cfg.nbands; b++ )
375 : {
376 41590860 : for ( i_ts = 0; i_ts < ( ( dtx_vad == 1 ) ? hQMetaData->q_direction[0].cfg.nblocks : 1 ); i_ts++ )
377 : {
378 33037668 : hQMetaData->q_direction->band_data[b].azimuth[i_ts] = hQMetaData->q_direction->band_data[b].q_azimuth[i_ts];
379 33037668 : hQMetaData->q_direction->band_data[b].elevation[i_ts] = hQMetaData->q_direction->band_data[b].q_elevation[i_ts];
380 33037668 : 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 2510926 : if ( dtx_vad == 0 )
385 : {
386 70545 : for ( i = 0; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
387 : {
388 56436 : hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].azimuth[i] = hQMetaData->q_direction[0].band_data[1].azimuth[0];
389 56436 : hQMetaData->q_direction[0].band_data[orig_dirac_bands - 1].elevation[i] = hQMetaData->q_direction[0].band_data[1].elevation[0];
390 56436 : 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 70545 : for ( i = 0; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
394 : {
395 159364 : for ( j = orig_dirac_bands - 2; j >= 0; j-- )
396 : {
397 102928 : hQMetaData->q_direction[0].band_data[j].azimuth[i] = hQMetaData->q_direction[0].band_data[0].azimuth[0];
398 102928 : hQMetaData->q_direction[0].band_data[j].elevation[i] = hQMetaData->q_direction[0].band_data[0].elevation[0];
399 102928 : hQMetaData->q_direction[0].band_data[j].energy_ratio[i] = hQMetaData->q_direction[0].band_data[0].energy_ratio[0];
400 : }
401 : }
402 :
403 14109 : hQMetaData->q_direction->cfg.nbands = orig_dirac_bands;
404 : }
405 : }
406 2510926 : pop_wmops();
407 :
408 2510926 : return IVAS_ERR_OK;
409 : }
410 :
411 :
412 : /*-------------------------------------------------------------------------
413 : * ivas_dirac_get_mono_flag()
414 : *
415 : *
416 : *-------------------------------------------------------------------------*/
417 :
418 10043704 : 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 10043704 : int16_t any_mc_band = 0;
430 10043704 : int16_t any_mono_band = 0;
431 10043704 : int16_t local_mono_flag = 0;
432 : float W_band_power_norm;
433 10043704 : float threshold = 0;
434 :
435 : /* Banded Power Calculations */
436 130568152 : for ( i = 0; i < DIRAC_MAX_NBANDS; i++ )
437 : {
438 120524448 : W_band_power = 0;
439 120524448 : other_ch_band_power = 0;
440 :
441 120524448 : brange[0] = band_grouping[i];
442 120524448 : brange[1] = band_grouping[i + 1];
443 :
444 : /* Loop over the W channel bins to calculate the power in the band */
445 1827575888 : for ( j = brange[0]; j < brange[1]; j++ )
446 : {
447 1707051440 : 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 482097792 : for ( ch_idx = 1; ch_idx < nchan_ana; ch_idx++ )
452 : {
453 : /* abs()^2 */
454 5482727664 : for ( j = brange[0]; j < brange[1]; j++ )
455 : {
456 5121154320 : 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 120524448 : if ( other_ch_band_power < EPSILON )
460 : {
461 12205945 : if ( W_band_power > DIRAC_MONO_THRESH_SILENCE )
462 : {
463 0 : any_mono_band = 1;
464 : }
465 : }
466 : else
467 : {
468 108318503 : if ( ( W_band_power > DIRAC_MONO_THRESH_SILENCE ) || ( other_ch_band_power > DIRAC_MONO_THRESH_SILENCE ) )
469 : {
470 104316482 : W_band_power_norm = min( W_band_power, DIRAC_MONO_NORM_FACTOR ) * DIRAC_MONO_ONE_ON_NORM_FACTOR;
471 104316482 : threshold = max( W_band_power_norm * DIRAC_MONO_MAX_THRESH, DIRAC_MONO_MIN_THRESH );
472 104316482 : if ( W_band_power / other_ch_band_power > threshold )
473 : {
474 4616 : any_mono_band = 1;
475 : }
476 : else
477 : {
478 104311866 : any_mc_band = 1;
479 : }
480 : }
481 : }
482 : }
483 :
484 : /* If any band contains multi-channel content it's not mono */
485 10043704 : if ( any_mc_band )
486 : {
487 9959902 : local_mono_flag = 0;
488 : }
489 : else
490 : {
491 : /* If any band contains mono content the frame is mono. */
492 83802 : if ( any_mono_band )
493 : {
494 506 : 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 10043704 : if ( local_mono_flag )
500 : {
501 506 : if ( *mono_frame_count < DIRAC_MONO_FRAME_THRESH )
502 : {
503 506 : ( *mono_frame_count )++;
504 : }
505 : }
506 : else
507 : {
508 : /* Instantaneously disable actual mono flag if multi-channel content is observed */
509 10043198 : if ( any_mc_band )
510 : {
511 9959902 : *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 10043704 : if ( *mono_frame_count == DIRAC_MONO_FRAME_THRESH )
517 : {
518 0 : return 1;
519 : }
520 : else
521 : {
522 10043704 : return 0;
523 : }
524 : }
525 :
526 :
527 : /*-------------------------------------------------------------------------
528 : * computeReferencePower_enc()
529 : *
530 : *
531 : *-------------------------------------------------------------------------*/
532 :
533 10776264 : 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 10776264 : if ( dirac_mono_flag != NULL )
552 : {
553 10043704 : *dirac_mono_flag = ivas_dirac_get_mono_flag( band_grouping, Cldfb_RealBuffer, Cldfb_ImagBuffer, nchan_ana, mono_frame_count );
554 : }
555 :
556 48698324 : for ( i = 0; i < num_freq_bands; i++ )
557 : {
558 37922060 : brange[0] = band_grouping[i + enc_param_start_band];
559 37922060 : brange[1] = band_grouping[i + enc_param_start_band + 1];
560 :
561 37922060 : reference_power[i] = 0;
562 :
563 37922060 : reference_power_W[i] = 0;
564 1530566136 : for ( j = brange[0]; j < brange[1]; j++ )
565 : {
566 1492644076 : reference_power_W[i] += ( Cldfb_RealBuffer[0][j] * Cldfb_RealBuffer[0][j] ) + ( Cldfb_ImagBuffer[0][j] * Cldfb_ImagBuffer[0][j] );
567 : }
568 37922060 : reference_power[i] += reference_power_W[i];
569 :
570 151688240 : for ( ch_idx = 1; ch_idx < nchan_ana; ch_idx++ )
571 : {
572 : /* abs()^2 */
573 4591698408 : for ( j = brange[0]; j < brange[1]; j++ )
574 : {
575 4477932228 : 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 10776264 : v_multc( reference_power, 0.5f, reference_power, num_freq_bands );
581 :
582 10776264 : if ( ivas_format == SBA_FORMAT && ref_power_w == 1 )
583 : {
584 32044920 : for ( i = 0; i < num_freq_bands; i++ )
585 : {
586 23880160 : reference_power[i] = max( reference_power[i], reference_power_W[i] );
587 : }
588 : }
589 :
590 10776264 : return;
591 : }
592 :
593 :
594 : /*-------------------------------------------------------------------------
595 : * ivas_dirac_param_est_enc()
596 : *
597 : *
598 : *------------------------------------------------------------------------*/
599 :
600 2510926 : 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 2510926 : push_wmops( "dirac_enc_param_est" );
637 2510926 : num_freq_bands = hDirAC->hConfig->nbands;
638 :
639 : /* Initialization */
640 2510926 : l_ts = input_frame / MAX_PARAM_SPATIAL_SUBFRAMES;
641 2510926 : if ( useLowerRes )
642 : {
643 184227 : q_direction->cfg.nblocks = 1;
644 184227 : num_blocks = 1;
645 : }
646 : else
647 : {
648 2326699 : q_direction->cfg.nblocks = MAX_PARAM_SPATIAL_SUBFRAMES;
649 2326699 : num_blocks = MAX_PARAM_SPATIAL_SUBFRAMES;
650 : }
651 :
652 : /* Need to initialize renormalization_factors, direction_m and diffuseness_m */
653 11075741 : for ( i = 0; i < hDirAC->hConfig->nbands; i++ )
654 : {
655 8564815 : renormalization_factor_diff[i] = 0;
656 8564815 : hDirAC->diffuseness_m[i] = 0;
657 : }
658 :
659 2510926 : set_zero( azi_secs, 2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS );
660 2510926 : set_zero( ele_secs, 2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS );
661 2510926 : set_zero( diff_secs, 2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS );
662 2510926 : set_zero( ene_secs, 2 * DIRAC_MAX_ANA_CHANS * DIRAC_MAX_NBANDS );
663 :
664 : /* Copy current frame to memory for delay compensation */
665 13314795 : for ( i = 0; i < nchan_fb_in; i++ )
666 : {
667 10803869 : pcm_in[i] = data_f[i];
668 10803869 : p_Cldfb_RealBuffer[i] = &Cldfb_RealBuffer[i][0];
669 10803869 : p_Cldfb_ImagBuffer[i] = &Cldfb_ImagBuffer[i][0];
670 : }
671 :
672 : /* do processing over all CLDFB time slots */
673 12001949 : for ( block_m_idx = 0; block_m_idx < num_blocks; block_m_idx++ )
674 : {
675 9491023 : mrange[0] = hDirAC->block_grouping[block_m_idx];
676 9491023 : mrange[1] = hDirAC->block_grouping[block_m_idx + 1];
677 :
678 42644921 : for ( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
679 : {
680 33153898 : renormalization_factor[band_m_idx] = EPSILON;
681 33153898 : hDirAC->direction_vector_m[0][block_m_idx][band_m_idx] = 0;
682 33153898 : hDirAC->direction_vector_m[1][block_m_idx][band_m_idx] = 0;
683 33153898 : hDirAC->direction_vector_m[2][block_m_idx][band_m_idx] = 0;
684 : }
685 :
686 19534727 : for ( ts = mrange[0]; ts < mrange[1]; ts++ )
687 : {
688 10043704 : 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 53259180 : for ( i = 0; i < nchan_fb_in; i++ )
706 : {
707 43215476 : mvr2r( &pp_fr_real[i][ts * l_ts], Cldfb_RealBuffer[i], l_ts );
708 43215476 : mvr2r( &pp_fr_imag[i][ts * l_ts], Cldfb_ImagBuffer[i], l_ts );
709 : }
710 : }
711 :
712 10043704 : computeReferencePower_enc(
713 10043704 : hDirAC->band_grouping,
714 : Cldfb_RealBuffer,
715 : Cldfb_ImagBuffer,
716 10043704 : reference_power[ts],
717 10043704 : 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 10043704 : computeIntensityVector_enc(
726 : hDirAC,
727 : Cldfb_RealBuffer,
728 : Cldfb_ImagBuffer,
729 10043704 : hDirAC->hConfig->enc_param_start_band,
730 : num_freq_bands,
731 : intensity_real );
732 :
733 10043704 : if ( !hodirac_flag )
734 : {
735 9435572 : computeDirectionVectors(
736 : intensity_real[0],
737 : intensity_real[1],
738 : intensity_real[2],
739 9435572 : 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 10043704 : hDirAC->index_buffer_intensity = ( hDirAC->index_buffer_intensity % hDirAC->no_col_avg_diff ) + 1; /* averaging_length = 32 */
748 10043704 : index = hDirAC->index_buffer_intensity;
749 40174816 : for ( i = 0; i < DIRAC_NUM_DIMS; i++ )
750 : {
751 : /* only real part needed */
752 30131112 : mvr2r( intensity_real[i], &( hDirAC->buffer_intensity_real[i][index - 1][0] ), num_freq_bands );
753 : }
754 10043704 : mvr2r( reference_power[ts], &( hDirAC->buffer_energy[( index - 1 ) * num_freq_bands] ), num_freq_bands );
755 :
756 10043704 : computeDiffuseness_mdft( hDirAC->buffer_intensity_real, hDirAC->buffer_energy, num_freq_bands, hDirAC->no_col_avg_diff, diffuseness_vector );
757 :
758 10043704 : if ( hodirac_flag )
759 : {
760 : #ifdef DEBUGGING
761 : assert( l_ts <= DIRAC_NO_FB_BANDS_MAX );
762 : #endif
763 608132 : calculate_hodirac_sector_parameters(
764 : hDirAC,
765 : Cldfb_RealBuffer,
766 : Cldfb_ImagBuffer,
767 : 0.20f,
768 608132 : hDirAC->band_grouping,
769 608132 : hDirAC->hConfig->nbands,
770 608132 : hDirAC->hConfig->enc_param_start_band,
771 : azi_secs,
772 : ele_secs,
773 : diff_secs,
774 : ene_secs );
775 : }
776 :
777 10043704 : if ( hodirac_flag )
778 : {
779 7297584 : for ( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
780 : {
781 6689452 : hDirAC->diffuseness_m[band_m_idx] += reference_power[ts][band_m_idx] * diffuseness_vector[band_m_idx];
782 6689452 : renormalization_factor_diff[band_m_idx] += reference_power[ts][band_m_idx];
783 : }
784 : }
785 : else
786 : {
787 37005380 : for ( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
788 : {
789 27569808 : norm_tmp = reference_power[ts][band_m_idx] * ( 1 - diffuseness_vector[band_m_idx] );
790 :
791 27569808 : hDirAC->direction_vector_m[0][block_m_idx][band_m_idx] += norm_tmp * direction_vector[0][band_m_idx];
792 27569808 : hDirAC->direction_vector_m[1][block_m_idx][band_m_idx] += norm_tmp * direction_vector[1][band_m_idx];
793 27569808 : hDirAC->direction_vector_m[2][block_m_idx][band_m_idx] += norm_tmp * direction_vector[2][band_m_idx];
794 27569808 : renormalization_factor[band_m_idx] += norm_tmp;
795 :
796 27569808 : hDirAC->diffuseness_m[band_m_idx] += reference_power[ts][band_m_idx] * diffuseness_vector[band_m_idx];
797 27569808 : renormalization_factor_diff[band_m_idx] += reference_power[ts][band_m_idx];
798 : }
799 : }
800 : }
801 :
802 9491023 : if ( !hodirac_flag )
803 : {
804 35347337 : for ( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
805 : {
806 26464446 : renormalization_factor[band_m_idx] = EPSILON;
807 105857784 : for ( d = 0; d < DIRAC_NUM_DIMS; d++ )
808 : {
809 79393338 : 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 26464446 : renormalization_factor[band_m_idx] = sqrtf( renormalization_factor[band_m_idx] );
812 :
813 26464446 : if ( renormalization_factor[band_m_idx] > EPSILON )
814 : {
815 26464446 : hDirAC->direction_vector_m[0][block_m_idx][band_m_idx] /= renormalization_factor[band_m_idx];
816 26464446 : hDirAC->direction_vector_m[1][block_m_idx][band_m_idx] /= renormalization_factor[band_m_idx];
817 26464446 : 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 105857784 : for ( d = 0; d < DIRAC_NUM_DIMS; d++ )
828 : {
829 79393338 : dir_v[d] = hDirAC->direction_vector_m[d][block_m_idx][band_m_idx];
830 : }
831 :
832 26464446 : ivas_qmetadata_direction_vector_to_azimuth_elevation(
833 : dir_v,
834 26464446 : &q_direction->band_data[band_m_idx].azimuth[block_m_idx],
835 26464446 : &q_direction->band_data[band_m_idx].elevation[block_m_idx] );
836 : }
837 : }
838 :
839 : /* Sectors */
840 9491023 : if ( hodirac_flag )
841 : {
842 7297584 : for ( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
843 : {
844 6689452 : q_direction->band_data[band_m_idx].azimuth[block_m_idx] = azi_secs[band_m_idx];
845 6689452 : 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 6689452 : q_direction[1].band_data[band_m_idx].azimuth[block_m_idx] = azi_secs[num_freq_bands + band_m_idx];
849 6689452 : q_direction[1].band_data[band_m_idx].elevation[block_m_idx] = ele_secs[num_freq_bands + band_m_idx];
850 6689452 : 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 11075741 : for ( band_m_idx = 0; band_m_idx < hDirAC->hConfig->nbands; band_m_idx++ )
857 : {
858 8564815 : if ( renormalization_factor_diff[band_m_idx] > EPSILON )
859 : {
860 8210805 : hDirAC->diffuseness_m[band_m_idx] /= renormalization_factor_diff[band_m_idx];
861 : }
862 : else
863 : {
864 354010 : hDirAC->diffuseness_m[band_m_idx] = 0.f;
865 : }
866 :
867 : /* set coherence to zero and convert diffuseness to energy ratio*/
868 :
869 8564815 : 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 41718713 : for ( block_m_idx = 0; block_m_idx < q_direction->cfg.nblocks; block_m_idx++ )
872 : {
873 33153898 : 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 2510926 : pop_wmops();
975 2510926 : return;
976 : }
977 :
978 :
979 : /*-------------------------------------------------------------------------
980 : * computeIntensityVector_enc()
981 : *
982 : *
983 : *------------------------------------------------------------------------*/
984 :
985 10043704 : 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 44302964 : for ( i = 0; i < num_frequency_bands; i++ )
1002 : {
1003 34259260 : brange[0] = hDirAC->band_grouping[i + enc_param_start_band];
1004 34259260 : brange[1] = hDirAC->band_grouping[i + enc_param_start_band + 1];
1005 :
1006 34259260 : intensity_real[0][i] = 0;
1007 34259260 : intensity_real[1][i] = 0;
1008 34259260 : intensity_real[2][i] = 0;
1009 :
1010 1353066536 : for ( j = brange[0]; j < brange[1]; j++ )
1011 : {
1012 1318807276 : real = Cldfb_RealBuffer[0][j];
1013 1318807276 : img = Cldfb_ImagBuffer[0][j];
1014 1318807276 : intensity_real[0][i] += Cldfb_RealBuffer[3][j] * real + Cldfb_ImagBuffer[3][j] * img;
1015 1318807276 : intensity_real[1][i] += Cldfb_RealBuffer[1][j] * real + Cldfb_ImagBuffer[1][j] * img;
1016 1318807276 : intensity_real[2][i] += Cldfb_RealBuffer[2][j] * real + Cldfb_ImagBuffer[2][j] * img;
1017 : }
1018 : }
1019 :
1020 10043704 : return;
1021 : }
1022 :
1023 :
1024 : /*-------------------------------------------------------------------------
1025 : * computeDiffuseness_mdft()
1026 : *
1027 : *
1028 : *------------------------------------------------------------------------*/
1029 :
1030 10776264 : 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 10776264 : float tmp = 0;
1042 : float *p_tmp;
1043 : const float *p_tmp_c;
1044 :
1045 : /* Compute Intensity slow and energy slow */
1046 10776264 : set_f( intensity_slow, 0.0f, DIRAC_NUM_DIMS * MASA_FREQUENCY_BANDS );
1047 10776264 : set_f( intensity_slow_abs, 0.0f, MASA_FREQUENCY_BANDS );
1048 10776264 : set_f( energy_slow, 0.0f, MASA_FREQUENCY_BANDS );
1049 :
1050 96986376 : for ( i = 0; i < no_col_avg_diff; ++i )
1051 : {
1052 : /* Energy slow */
1053 86210112 : p_tmp_c = buffer_energy + i * num_freq_bands;
1054 389586592 : for ( k = 0; k < num_freq_bands; k++ )
1055 : {
1056 303376480 : energy_slow[k] += *( p_tmp_c++ );
1057 : }
1058 :
1059 : /* Intensity slow */
1060 344840448 : for ( j = 0; j < DIRAC_NUM_DIMS; ++j )
1061 : {
1062 258630336 : p_tmp = buffer_intensity[j][i];
1063 1168759776 : for ( k = 0; k < num_freq_bands; k++ )
1064 : {
1065 910129440 : intensity_slow[j * num_freq_bands + k] += *( p_tmp++ );
1066 : }
1067 : }
1068 : }
1069 :
1070 : /* intensity_slow.^2 + intensity_slow_abs*/
1071 43105056 : for ( j = 0; j < DIRAC_NUM_DIMS; ++j )
1072 : {
1073 32328792 : p_tmp = intensity_slow + j * num_freq_bands;
1074 146094972 : for ( k = 0; k < num_freq_bands; k++ )
1075 : {
1076 113766180 : *( p_tmp ) *= ( *p_tmp );
1077 113766180 : intensity_slow_abs[k] += *( p_tmp++ );
1078 : }
1079 : }
1080 :
1081 : /* Compute Diffuseness */
1082 10776264 : p_tmp = intensity_slow_abs;
1083 48698324 : for ( i = 0; i < num_freq_bands; ++i )
1084 : {
1085 37922060 : tmp = sqrtf( *( p_tmp++ ) ) / ( energy_slow[i] + EPSILON );
1086 37922060 : tmp = 1.0f - tmp;
1087 37922060 : diffuseness[i] = ( ( tmp < 1.0f ) ? ( ( tmp < 0.0f ) ? 0.f : tmp ) : 1.0f );
1088 : }
1089 :
1090 10776264 : return;
1091 : }
|