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 : * Local constants
51 : *------------------------------------------------------------------------*/
52 :
53 : #define DIRAC_MAX_DECORR_CLDFB_BANDS_AMBI 22
54 : #define DIRAC_MAX_DECORR_CLDFB_BANDS 15
55 :
56 : #define DIRAC_DUCK_ALPHA 0.8f
57 : #define DIRAC_DUCK_GAMMA 1.5f
58 :
59 :
60 : /*-------------------------------------------------------------------------
61 : * Local function prototypes
62 : *------------------------------------------------------------------------*/
63 :
64 : static void get_lattice_coeffs( const int16_t band_index, const int16_t channel_index, float *lattice_coeffs );
65 :
66 : static void lattice2allpass( const int16_t filter_length, const float *lattice_coeffs, float *filter_coeffs_num_real, float *filter_coeffs_den_real );
67 :
68 :
69 : /*-------------------------------------------------------------------------
70 : * ivas_dirac_dec_decorr_open()
71 : *
72 : * Allocate and initialize TD decorrelator decoder handle
73 : *------------------------------------------------------------------------*/
74 :
75 10291 : ivas_error ivas_dirac_dec_decorr_open(
76 : DIRAC_DECORR_PARAMS **ph_freq_domain_decorr_ap_params,
77 : DIRAC_DECORR_STATE **ph_freq_domain_decorr_ap_state,
78 : const int16_t num_freq_bands,
79 : int16_t num_outputs_diff,
80 : const int16_t num_protos_diff,
81 : const DIRAC_SYNTHESIS_CONFIG synthesisConf,
82 : float *frequency_axis,
83 : const int16_t nchan_transport, /* i : number of transport channels*/
84 : const int32_t output_Fs /* i : output sampling rate */
85 : )
86 : {
87 : int16_t k, l, m, n;
88 : int16_t split_band_index_start;
89 : int16_t k_in, k_out, num_bands, band_table_idx, buffer_size_decorr;
90 10291 : int16_t split_frequencies_bands[DIRAC_DECORR_NUM_SPLIT_BANDS + 1] = { 0, 0, 0, 23768 };
91 : int16_t *split_freq_ptr;
92 : float cur_lattice_delta_phi, lattice_coeffs[2 * DIRAC_MAX_DECORR_FILTER_LEN];
93 : ivas_error error;
94 :
95 : /* pointers to structs for allocation */
96 10291 : DIRAC_DECORR_PARAMS *freq_domain_decorr_ap_params = NULL;
97 10291 : DIRAC_DECORR_STATE *freq_domain_decorr_ap_state = NULL;
98 :
99 : /*-----------------------------------------------------------------*
100 : * prepare library opening
101 : *-----------------------------------------------------------------*/
102 :
103 : /* allocate structs */
104 10291 : if ( ( freq_domain_decorr_ap_params = (DIRAC_DECORR_PARAMS *) malloc( sizeof( DIRAC_DECORR_PARAMS ) ) ) == NULL )
105 : {
106 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD decorrelator\n" ) );
107 : }
108 :
109 10291 : if ( ( freq_domain_decorr_ap_state = (DIRAC_DECORR_STATE *) malloc( sizeof( DIRAC_DECORR_STATE ) ) ) == NULL )
110 : {
111 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD decorrelator\n" ) );
112 : }
113 :
114 : /*-----------------------------------------------------------------*
115 : * check input parameters
116 : *-----------------------------------------------------------------*/
117 :
118 10291 : assert( ( num_freq_bands > 0 ) && "Error: Number of frequency bands <= 0!" );
119 :
120 :
121 10291 : if ( synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
122 : {
123 2697 : num_outputs_diff -= nchan_transport;
124 : }
125 :
126 10291 : assert( ( num_outputs_diff >= 0 ) && ( num_outputs_diff <= DIRAC_MAX_NUM_DECORR_FILTERS ) && "Error: Number of channels <= 0 or > DIRAC_MAX_NUM_DECORR_FILTERS" );
127 :
128 : /*-----------------------------------------------------------------*
129 : * set default parameters
130 : *-----------------------------------------------------------------*/
131 :
132 10291 : if ( synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
133 : {
134 : /*Decorrelation in SHD*/
135 2697 : freq_domain_decorr_ap_params->add_back_onsets_on = 0;
136 2697 : if ( nchan_transport > 2 )
137 : {
138 2676 : freq_domain_decorr_ap_params->max_frequency = 2000;
139 : }
140 : else
141 : {
142 21 : freq_domain_decorr_ap_params->max_frequency = ( min( num_freq_bands, DIRAC_MAX_DECORR_CLDFB_BANDS_AMBI ) * 24000 ) / CLDFB_NO_CHANNELS_MAX;
143 : }
144 : }
145 7594 : else if ( synthesisConf == DIRAC_SYNTHESIS_COV_MC_LS )
146 : {
147 : /*Decorrelation in LS channels for MC*/
148 837 : freq_domain_decorr_ap_params->add_back_onsets_on = 1;
149 837 : freq_domain_decorr_ap_params->max_frequency = ( PARAM_MC_MAX_DECORR_CLDFB_BANDS * 24000 ) / CLDFB_NO_CHANNELS_MAX;
150 : }
151 : else
152 : {
153 : /*Decorrelation in LS channels*/
154 6757 : freq_domain_decorr_ap_params->add_back_onsets_on = 1;
155 6757 : freq_domain_decorr_ap_params->max_frequency = ( DIRAC_MAX_DECORR_CLDFB_BANDS * 24000 ) / CLDFB_NO_CHANNELS_MAX;
156 : }
157 :
158 10291 : freq_domain_decorr_ap_params->use_ducker = 1;
159 :
160 10291 : assert( ( freq_domain_decorr_ap_params->max_frequency >= 0 ) && ( freq_domain_decorr_ap_params->max_frequency <= output_Fs / 2 ) && "Error: max_frequency invalid!" );
161 :
162 : /* compute maximum band for decorrelation */
163 10291 : assert( frequency_axis != NULL );
164 428674 : for ( k = num_freq_bands - 1; k > 0; --k )
165 : {
166 428674 : freq_domain_decorr_ap_params->max_band_decorr = k + 1; /* outside "if" to avoid uninitialized variable */
167 :
168 428674 : if ( frequency_axis[k] < freq_domain_decorr_ap_params->max_frequency )
169 : {
170 10291 : break;
171 : }
172 : }
173 :
174 : /*-----------------------------------------------------------------*
175 : * open sub-modules
176 : *-----------------------------------------------------------------*/
177 :
178 : /* open onset detection module */
179 10291 : if ( synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
180 : {
181 : /*Onset detector up to Nyquist and not only up to max_band_decorr*/
182 2697 : if ( ( error = ivas_dirac_dec_onset_detection_open( num_protos_diff, num_freq_bands, num_freq_bands, &freq_domain_decorr_ap_params->h_onset_detection_power_params, &freq_domain_decorr_ap_state->h_onset_detection_power_state ) ) != IVAS_ERR_OK )
183 : {
184 0 : return error;
185 : }
186 : }
187 : else
188 : {
189 7594 : if ( ( error = ivas_dirac_dec_onset_detection_open( num_protos_diff, num_freq_bands, freq_domain_decorr_ap_params->max_band_decorr, &freq_domain_decorr_ap_params->h_onset_detection_power_params, &freq_domain_decorr_ap_state->h_onset_detection_power_state ) ) != IVAS_ERR_OK )
190 : {
191 0 : return error;
192 : }
193 : }
194 :
195 : /*-----------------------------------------------------------------*
196 : * prepare processing parameters
197 : *-----------------------------------------------------------------*/
198 :
199 : /* calculate decorrelation split bands */
200 10291 : split_freq_ptr = &split_frequencies_bands[0];
201 10291 : split_freq_ptr[0] = 0;
202 30873 : for ( k = 1; k < DIRAC_DECORR_NUM_SPLIT_BANDS; k++ )
203 : {
204 20582 : split_freq_ptr[k] = (int16_t) ( (float) CLDFB_NO_CHANNELS_MAX * ap_split_frequencies[k] + 0.5f ) - 1;
205 : }
206 10291 : split_band_index_start = 0;
207 :
208 10291 : split_frequencies_bands[k] = 0;
209 10291 : freq_domain_decorr_ap_params->num_split_frequency_bands = 0;
210 17906 : for ( k = 1; k < DIRAC_DECORR_NUM_SPLIT_BANDS + 1; k++ )
211 : {
212 17906 : freq_domain_decorr_ap_params->num_split_frequency_bands++;
213 17906 : if ( split_frequencies_bands[k] >= freq_domain_decorr_ap_params->max_band_decorr )
214 : {
215 10291 : split_frequencies_bands[k] = freq_domain_decorr_ap_params->max_band_decorr;
216 10291 : break;
217 : }
218 : }
219 :
220 10291 : if ( ( freq_domain_decorr_ap_params->split_frequency_bands = (int16_t *) malloc( sizeof( int16_t ) * ( freq_domain_decorr_ap_params->num_split_frequency_bands + 1 ) ) ) == NULL )
221 : {
222 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD decorrelator\n" ) );
223 : }
224 10291 : mvs2s( &split_frequencies_bands[0], freq_domain_decorr_ap_params->split_frequency_bands, freq_domain_decorr_ap_params->num_split_frequency_bands + 1 );
225 :
226 : /* calc buffer size and allocate arrays */
227 10291 : freq_domain_decorr_ap_state->decorr_buffer = NULL;
228 10291 : freq_domain_decorr_ap_params->filter_coeff_num_real = NULL;
229 10291 : freq_domain_decorr_ap_params->filter_coeff_den_real = NULL;
230 10291 : freq_domain_decorr_ap_params->phase_coeff_real = NULL;
231 10291 : freq_domain_decorr_ap_params->phase_coeff_imag = NULL;
232 10291 : freq_domain_decorr_ap_state->direct_energy_smooth = NULL;
233 10291 : freq_domain_decorr_ap_state->reverb_energy_smooth = NULL;
234 10291 : freq_domain_decorr_ap_params->pre_delay = NULL;
235 10291 : freq_domain_decorr_ap_params->filter_length = NULL;
236 :
237 10291 : if ( num_outputs_diff > 0 )
238 : {
239 7615 : buffer_size_decorr = ( ap_pre_delay[split_band_index_start] + ap_filter_length[split_band_index_start] );
240 7615 : if ( ( freq_domain_decorr_ap_state->decorr_buffer = (float *) malloc( sizeof( float ) * 2 * buffer_size_decorr * num_outputs_diff * freq_domain_decorr_ap_params->max_band_decorr ) ) == NULL )
241 : {
242 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD decorrelator\n" ) );
243 : }
244 7615 : set_f( freq_domain_decorr_ap_state->decorr_buffer, 0.0f, 2 * buffer_size_decorr * num_outputs_diff * freq_domain_decorr_ap_params->max_band_decorr );
245 :
246 7615 : if ( ( freq_domain_decorr_ap_params->filter_coeff_num_real = (float *) malloc( sizeof( float ) * ( ap_filter_length[split_band_index_start] + 1 ) * freq_domain_decorr_ap_params->max_band_decorr * num_outputs_diff ) ) == NULL )
247 : {
248 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD decorrelator\n" ) );
249 : }
250 :
251 7615 : if ( ( freq_domain_decorr_ap_params->filter_coeff_den_real = (float *) malloc( sizeof( float ) * ( ap_filter_length[split_band_index_start] + 1 ) * freq_domain_decorr_ap_params->max_band_decorr * num_outputs_diff ) ) == NULL )
252 : {
253 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD decorrelator\n" ) );
254 : }
255 :
256 7615 : if ( ( freq_domain_decorr_ap_params->phase_coeff_real = (float *) malloc( sizeof( float ) * freq_domain_decorr_ap_params->max_band_decorr * num_outputs_diff ) ) == NULL )
257 : {
258 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD decorrelator\n" ) );
259 : }
260 :
261 7615 : if ( ( freq_domain_decorr_ap_params->phase_coeff_imag = (float *) malloc( sizeof( float ) * freq_domain_decorr_ap_params->max_band_decorr * num_outputs_diff ) ) == NULL )
262 : {
263 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD decorrelator\n" ) );
264 : }
265 :
266 7615 : if ( ( freq_domain_decorr_ap_state->direct_energy_smooth = (float *) malloc( sizeof( float ) * freq_domain_decorr_ap_params->max_band_decorr * num_outputs_diff ) ) == NULL )
267 : {
268 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD decorrelator\n" ) );
269 : }
270 :
271 7615 : if ( ( freq_domain_decorr_ap_state->reverb_energy_smooth = (float *) malloc( sizeof( float ) * freq_domain_decorr_ap_params->max_band_decorr * num_outputs_diff ) ) == NULL )
272 : {
273 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD decorrelator\n" ) );
274 : }
275 :
276 7615 : if ( ( freq_domain_decorr_ap_params->pre_delay = (int16_t *) malloc( sizeof( int16_t ) * freq_domain_decorr_ap_params->num_split_frequency_bands ) ) == NULL )
277 : {
278 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD decorrelator\n" ) );
279 : }
280 7615 : if ( ( freq_domain_decorr_ap_params->filter_length = (int16_t *) malloc( sizeof( int16_t ) * freq_domain_decorr_ap_params->num_split_frequency_bands ) ) == NULL )
281 : {
282 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD decorrelator\n" ) );
283 : }
284 :
285 7615 : set_f( freq_domain_decorr_ap_state->direct_energy_smooth, 0.0f, freq_domain_decorr_ap_params->max_band_decorr * num_outputs_diff );
286 7615 : set_f( freq_domain_decorr_ap_state->reverb_energy_smooth, 0.0f, freq_domain_decorr_ap_params->max_band_decorr * num_outputs_diff );
287 :
288 : /* compute filter coefficients */
289 22845 : for ( k = 0; k < freq_domain_decorr_ap_params->num_split_frequency_bands; k++ )
290 : {
291 15230 : k_in = freq_domain_decorr_ap_params->split_frequency_bands[k];
292 15230 : k_out = freq_domain_decorr_ap_params->split_frequency_bands[k + 1];
293 15230 : num_bands = k_out - k_in;
294 15230 : band_table_idx = k + split_band_index_start;
295 15230 : freq_domain_decorr_ap_params->filter_length[k] = ap_filter_length[band_table_idx] + 1;
296 15230 : freq_domain_decorr_ap_params->pre_delay[k] = ap_pre_delay[band_table_idx];
297 :
298 98330 : for ( l = 0; l < num_outputs_diff; l++ )
299 : {
300 733611 : for ( m = 0; m < num_bands; m++ )
301 : {
302 650511 : n = k_in + m;
303 650511 : cur_lattice_delta_phi = ap_lattice_delta_phi[l * DIRAC_MAX_DECORR_FILTER_LEN + ap_filter_length[k] - 1] * n;
304 650511 : freq_domain_decorr_ap_params->phase_coeff_real[l * freq_domain_decorr_ap_params->max_band_decorr + n] = cosf( cur_lattice_delta_phi );
305 650511 : freq_domain_decorr_ap_params->phase_coeff_imag[l * freq_domain_decorr_ap_params->max_band_decorr + n] = -sinf( cur_lattice_delta_phi );
306 :
307 : /* calculate phase offset */
308 650511 : get_lattice_coeffs( band_table_idx, l, lattice_coeffs );
309 :
310 : /* calcualte transfer function coefficients from the lattice coefficients */
311 650511 : lattice2allpass( freq_domain_decorr_ap_params->filter_length[k], lattice_coeffs, &freq_domain_decorr_ap_params->filter_coeff_num_real[( k_in + m ) * ( freq_domain_decorr_ap_params->filter_length[0] * num_outputs_diff ) + l * freq_domain_decorr_ap_params->filter_length[0]], &freq_domain_decorr_ap_params->filter_coeff_den_real[( k_in + m ) * ( freq_domain_decorr_ap_params->filter_length[0] * num_outputs_diff ) + l * freq_domain_decorr_ap_params->filter_length[0]] );
312 : }
313 : }
314 : }
315 : }
316 :
317 10291 : *ph_freq_domain_decorr_ap_params = freq_domain_decorr_ap_params;
318 10291 : *ph_freq_domain_decorr_ap_state = freq_domain_decorr_ap_state;
319 :
320 10291 : return IVAS_ERR_OK;
321 : }
322 :
323 :
324 : /*-------------------------------------------------------------------------
325 : * ivas_dirac_dec_decorr_process()
326 : *
327 : *
328 : *------------------------------------------------------------------------*/
329 :
330 5827593 : void ivas_dirac_dec_decorr_process(
331 : const int16_t num_freq_bands,
332 : int16_t num_channels,
333 : const int16_t num_protos_diff,
334 : const DIRAC_SYNTHESIS_CONFIG synthesisConf,
335 : const int16_t nchan_transport, /* i : number of transport channels*/
336 : const float *input_frame_f,
337 : const int16_t num_protos_dir,
338 : const int16_t *proto_index_dir,
339 : float *frame_dec_f,
340 : float *onset_filter,
341 : HANDLE_DIRAC_DECORR_PARAMS h_freq_domain_decorr_ap_params,
342 : HANDLE_DIRAC_DECORR_STATE h_freq_domain_decorr_ap_state )
343 : {
344 : int16_t ch_idx, k, l, idx_in_out, max_band_decorr;
345 : int16_t split_bands_idx, band_idx, decorr_buffer_len, time_idx;
346 : int16_t offset, idx_filter, incr_aux;
347 : int16_t k_1, k_2, num_bands, filter_length, pre_delay, decorr_buffer_step;
348 : float frame_ma[2 * ( DIRAC_MAX_DECORR_FILTER_LEN + 1 )];
349 : float aux_buffer[2 * MAX_OUTPUT_CHANNELS * CLDFB_NO_CHANNELS_MAX];
350 : float direct_energy[MAX_OUTPUT_CHANNELS * CLDFB_NO_CHANNELS_MAX];
351 : float *p_frame_dec_f, *decorr_buffer, *phase_coeff_real, *phase_coeff_imag;
352 : float *filter_coeff_num_real, *filter_coeff_den_real, *decorr_buffer_start_ptr, *decorr_buffer_ptr;
353 : float input_real, input_imag, filter_frame_imag, filter_frame_real;
354 :
355 5827593 : push_wmops( "dirac_decorr_process" );
356 :
357 : /* check handles */
358 5827593 : if ( h_freq_domain_decorr_ap_params == NULL || h_freq_domain_decorr_ap_state == NULL )
359 : {
360 : #ifdef DEBUGGING
361 : assert( !"Error: NULL pointer.\n" );
362 : #endif
363 0 : return;
364 : }
365 :
366 : /* check input data */
367 5827593 : if ( input_frame_f == NULL )
368 : {
369 : #ifdef DEBUGGING
370 : assert( !"Error: NULL pointer.\n" );
371 : #endif
372 0 : return;
373 : }
374 :
375 : /* check result arrays */
376 5827593 : if ( frame_dec_f == NULL )
377 : {
378 : #ifdef DEBUGGING
379 : assert( !"Error: NULL pointer.\n" );
380 : #endif
381 0 : return;
382 : }
383 :
384 :
385 : /*-----------------------------------------------------------------*
386 : * ********** processing **********
387 : *-----------------------------------------------------------------*/
388 :
389 : /*-----------------------------------------------------------------*
390 : * collect some often used parameters
391 : *-----------------------------------------------------------------*/
392 :
393 5827593 : if ( synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
394 : {
395 2863941 : num_channels -= nchan_transport;
396 : }
397 :
398 :
399 : /*-----------------------------------------------------------------*
400 : * detect and remove onsets
401 : *-----------------------------------------------------------------*/
402 :
403 : /* compute power */
404 5827593 : max_band_decorr = h_freq_domain_decorr_ap_params->h_onset_detection_power_params.max_band_decorr;
405 :
406 15133308 : for ( ch_idx = 0; ch_idx < num_protos_dir; ch_idx++ )
407 : {
408 9305715 : v_mult( &input_frame_f[2 * ch_idx * num_freq_bands], &input_frame_f[2 * ch_idx * num_freq_bands], &aux_buffer[2 * ch_idx * max_band_decorr], 2 * max_band_decorr );
409 :
410 9305715 : v_add_inc( &aux_buffer[2 * ch_idx * max_band_decorr], 2, &aux_buffer[2 * ch_idx * max_band_decorr + 1], 2, &aux_buffer[ch_idx * max_band_decorr], 1, max_band_decorr );
411 : }
412 :
413 : /* compute onset filter */
414 5827593 : max_band_decorr = h_freq_domain_decorr_ap_params->max_band_decorr;
415 :
416 5827593 : set_f( onset_filter, 1.0f, num_protos_diff * num_freq_bands );
417 :
418 5827593 : ivas_dirac_dec_onset_detection_process( aux_buffer, onset_filter, num_protos_diff, h_freq_domain_decorr_ap_params->h_onset_detection_power_params, h_freq_domain_decorr_ap_state->h_onset_detection_power_state );
419 :
420 : /* Apply decorrelator if num_channels is greater than 0 */
421 5827593 : if ( num_channels > 0 )
422 : {
423 : /* remove onsets from prototype frame */
424 9670554 : for ( ch_idx = 0; ch_idx < num_protos_dir; ch_idx++ )
425 : {
426 6574338 : v_mult_inc( &input_frame_f[2 * ch_idx * num_freq_bands], 2, &onset_filter[ch_idx * num_freq_bands], 1, &aux_buffer[2 * ch_idx * max_band_decorr], 2, max_band_decorr );
427 :
428 6574338 : v_mult_inc( &input_frame_f[2 * ch_idx * num_freq_bands + 1], 2, &onset_filter[ch_idx * num_freq_bands], 1, &aux_buffer[2 * ch_idx * max_band_decorr + 1], 2, max_band_decorr );
429 : }
430 :
431 : /*-----------------------------------------------------------------*
432 : * all pass based decorrelation
433 : *-----------------------------------------------------------------*/
434 :
435 : /* compute decorrelated output frame */
436 : /* output is the first frame of the state */
437 3096216 : p_frame_dec_f = frame_dec_f;
438 3096216 : decorr_buffer = h_freq_domain_decorr_ap_state->decorr_buffer;
439 3096216 : phase_coeff_real = h_freq_domain_decorr_ap_params->phase_coeff_real;
440 3096216 : phase_coeff_imag = h_freq_domain_decorr_ap_params->phase_coeff_imag;
441 3096216 : incr_aux = 2 * ( num_freq_bands - max_band_decorr );
442 :
443 15509181 : for ( ch_idx = 0; ch_idx < num_channels; ++ch_idx )
444 : {
445 : /* final phase rotation */
446 216391164 : for ( k = 0; k < max_band_decorr; k++ )
447 : {
448 203978199 : *( p_frame_dec_f++ ) = ( ( *decorr_buffer ) * ( *phase_coeff_real ) ) - ( ( *( decorr_buffer + 1 ) ) * ( *phase_coeff_imag ) );
449 203978199 : *( p_frame_dec_f++ ) = ( ( *decorr_buffer ) * ( *phase_coeff_imag++ ) ) + ( ( *( decorr_buffer + 1 ) ) * ( *phase_coeff_real++ ) );
450 203978199 : decorr_buffer += 2;
451 : }
452 12412965 : p_frame_dec_f += incr_aux;
453 : }
454 :
455 : /* update state */
456 3096216 : decorr_buffer_len = h_freq_domain_decorr_ap_params->filter_length[0] + h_freq_domain_decorr_ap_params->pre_delay[0] - 1;
457 68116752 : for ( time_idx = 0; time_idx < ( decorr_buffer_len - 1 ); time_idx++ )
458 : {
459 65020536 : mvr2r( &h_freq_domain_decorr_ap_state->decorr_buffer[2 * ( time_idx + 1 ) * max_band_decorr * num_channels], &h_freq_domain_decorr_ap_state->decorr_buffer[2 * (time_idx) *max_band_decorr * num_channels], 2 * max_band_decorr * num_channels );
460 : }
461 3096216 : set_zero( &h_freq_domain_decorr_ap_state->decorr_buffer[2 * ( decorr_buffer_len - 1 ) * max_band_decorr * num_channels], 2 * max_band_decorr * num_channels );
462 :
463 : /* calculate all pass */
464 9288648 : for ( split_bands_idx = 0; split_bands_idx < h_freq_domain_decorr_ap_params->num_split_frequency_bands; split_bands_idx++ )
465 : {
466 6192432 : k_1 = h_freq_domain_decorr_ap_params->split_frequency_bands[split_bands_idx];
467 6192432 : k_2 = h_freq_domain_decorr_ap_params->split_frequency_bands[split_bands_idx + 1];
468 6192432 : num_bands = k_2 - k_1;
469 6192432 : filter_length = h_freq_domain_decorr_ap_params->filter_length[split_bands_idx];
470 6192432 : pre_delay = h_freq_domain_decorr_ap_params->pre_delay[split_bands_idx];
471 6192432 : decorr_buffer_step = num_channels * max_band_decorr;
472 :
473 56019585 : for ( k = 0; k < num_bands; ++k )
474 : {
475 49827153 : band_idx = k_1 + k;
476 :
477 253805352 : for ( ch_idx = 0; ch_idx < num_channels; ++ch_idx )
478 : {
479 203978199 : idx_filter = band_idx * ( h_freq_domain_decorr_ap_params->filter_length[0] * num_channels ) + ch_idx * h_freq_domain_decorr_ap_params->filter_length[0];
480 203978199 : filter_coeff_num_real = &h_freq_domain_decorr_ap_params->filter_coeff_num_real[idx_filter];
481 203978199 : filter_coeff_den_real = &h_freq_domain_decorr_ap_params->filter_coeff_den_real[idx_filter];
482 203978199 : decorr_buffer_start_ptr = &h_freq_domain_decorr_ap_state->decorr_buffer[2 * ( ch_idx * max_band_decorr + band_idx )];
483 203978199 : input_real = aux_buffer[2 * ( proto_index_dir[ch_idx] * max_band_decorr + band_idx )];
484 203978199 : input_imag = aux_buffer[2 * ( proto_index_dir[ch_idx] * max_band_decorr + band_idx ) + 1];
485 :
486 : /* MA part of filter impulse response */
487 2413842387 : for ( l = 0; l < filter_length; l++ )
488 : {
489 2209864188 : frame_ma[2 * l] = input_real * filter_coeff_num_real[l];
490 2209864188 : frame_ma[2 * l + 1] = input_imag * filter_coeff_num_real[l];
491 : }
492 203978199 : decorr_buffer_ptr = decorr_buffer_start_ptr + 2 * ( pre_delay - 1 ) * decorr_buffer_step;
493 :
494 : /*add MA part to state */
495 203978199 : decorr_buffer_ptr[0] += frame_ma[0];
496 203978199 : decorr_buffer_ptr[1] += frame_ma[1];
497 :
498 : /*get values for AR part */
499 203978199 : filter_frame_real = decorr_buffer_ptr[0];
500 203978199 : filter_frame_imag = decorr_buffer_ptr[1];
501 :
502 203978199 : decorr_buffer_ptr += 2 * decorr_buffer_step;
503 :
504 2209864188 : for ( l = 1; l < filter_length; l++ )
505 : {
506 2005885989 : decorr_buffer_ptr[0] += frame_ma[2 * l] - filter_frame_real * filter_coeff_den_real[l];
507 2005885989 : decorr_buffer_ptr[1] += frame_ma[2 * l + 1] - filter_frame_imag * filter_coeff_den_real[l];
508 2005885989 : decorr_buffer_ptr += 2 * decorr_buffer_step;
509 : }
510 : }
511 : }
512 : }
513 :
514 : /*-----------------------------------------------------------------*
515 : * onset/diffuse energy ratio conservation
516 : *-----------------------------------------------------------------*/
517 :
518 3096216 : if ( h_freq_domain_decorr_ap_params->use_ducker )
519 : {
520 : /* compute direct power w/o onsets for the energy ratio, signal is still in the aux buffer */
521 3096216 : v_mult( aux_buffer, aux_buffer, aux_buffer, 2 * max_band_decorr * num_protos_dir );
522 3096216 : v_add_inc( &aux_buffer[0], 2, &aux_buffer[1], 2, direct_energy, 1, max_band_decorr * num_protos_dir );
523 :
524 : /* calculate the power of the decorrelated signal */
525 :
526 15509181 : for ( ch_idx = 0; ch_idx < num_channels; ++ch_idx )
527 : {
528 12412965 : v_mult( &frame_dec_f[2 * ch_idx * num_freq_bands], &frame_dec_f[2 * ch_idx * num_freq_bands], &aux_buffer[2 * ch_idx * max_band_decorr], 2 * max_band_decorr );
529 12412965 : v_add_inc( &aux_buffer[2 * ch_idx * max_band_decorr], 2, &aux_buffer[2 * ch_idx * max_band_decorr + 1], 2, &aux_buffer[ch_idx * max_band_decorr], 1, max_band_decorr );
530 : }
531 :
532 : /* smooth energies */
533 3096216 : v_multc( aux_buffer, 1.0f - DIRAC_DUCK_ALPHA, aux_buffer, num_channels * max_band_decorr );
534 :
535 3096216 : v_multc( h_freq_domain_decorr_ap_state->reverb_energy_smooth, DIRAC_DUCK_ALPHA, h_freq_domain_decorr_ap_state->reverb_energy_smooth, num_channels * max_band_decorr );
536 :
537 3096216 : v_add( aux_buffer, h_freq_domain_decorr_ap_state->reverb_energy_smooth, h_freq_domain_decorr_ap_state->reverb_energy_smooth, num_channels * max_band_decorr );
538 :
539 3096216 : v_multc( direct_energy, 1.0f - DIRAC_DUCK_ALPHA, direct_energy, num_protos_dir * max_band_decorr );
540 :
541 3096216 : v_multc( h_freq_domain_decorr_ap_state->direct_energy_smooth, DIRAC_DUCK_ALPHA, h_freq_domain_decorr_ap_state->direct_energy_smooth, num_protos_dir * max_band_decorr );
542 :
543 3096216 : v_add( direct_energy, h_freq_domain_decorr_ap_state->direct_energy_smooth, h_freq_domain_decorr_ap_state->direct_energy_smooth, num_protos_dir * max_band_decorr );
544 :
545 15509181 : for ( ch_idx = 0; ch_idx < num_channels; ch_idx++ )
546 : {
547 12412965 : float *frame_dec_f_ptr = &frame_dec_f[ch_idx * 2 * num_freq_bands];
548 12412965 : int16_t cur_proto_index = proto_index_dir[ch_idx] * max_band_decorr;
549 12412965 : int16_t cur_reverb_index = ch_idx * max_band_decorr;
550 12412965 : float *reverb_energy_smooth_ptr = &h_freq_domain_decorr_ap_state->reverb_energy_smooth[cur_reverb_index];
551 12412965 : float *direct_energy_smooth_ptr = &h_freq_domain_decorr_ap_state->direct_energy_smooth[cur_proto_index];
552 :
553 216391164 : for ( band_idx = 0; band_idx < max_band_decorr; band_idx++ )
554 : {
555 203978199 : float duck_gain = 1.0f;
556 203978199 : float direct_energy_loc = direct_energy_smooth_ptr[band_idx];
557 203978199 : float reverb_energy_loc = reverb_energy_smooth_ptr[band_idx];
558 :
559 203978199 : if ( reverb_energy_loc > ( direct_energy_loc * DIRAC_DUCK_GAMMA ) )
560 : {
561 51144624 : duck_gain = sqrtf( direct_energy_loc * DIRAC_DUCK_GAMMA / ( reverb_energy_loc + EPSILON ) );
562 51144624 : frame_dec_f_ptr[2 * band_idx] *= duck_gain;
563 51144624 : frame_dec_f_ptr[2 * band_idx + 1] *= duck_gain;
564 : }
565 152833575 : else if ( direct_energy_loc > ( reverb_energy_loc * DIRAC_DUCK_GAMMA ) )
566 : {
567 62833482 : duck_gain = sqrtf( direct_energy_loc / ( DIRAC_DUCK_GAMMA * reverb_energy_loc + EPSILON ) );
568 62833482 : if ( duck_gain > 2.0f )
569 : {
570 12305013 : duck_gain = 2.0f;
571 : }
572 62833482 : frame_dec_f_ptr[2 * band_idx] *= duck_gain;
573 62833482 : frame_dec_f_ptr[2 * band_idx + 1] *= duck_gain;
574 : }
575 : }
576 : }
577 : }
578 :
579 : /*-----------------------------------------------------------------*
580 : * add back onsets
581 : *-----------------------------------------------------------------*/
582 :
583 3096216 : if ( h_freq_domain_decorr_ap_params->add_back_onsets_on == 1 )
584 : {
585 14978925 : for ( ch_idx = 0; ch_idx < num_channels; ++ch_idx )
586 : {
587 12015273 : offset = proto_index_dir[ch_idx] * num_freq_bands;
588 :
589 207244248 : for ( k = 0; k < max_band_decorr; ++k )
590 : {
591 195228975 : aux_buffer[2 * k] = ( 1.0f - onset_filter[offset + k] ) * input_frame_f[2 * offset + 2 * k];
592 195228975 : aux_buffer[2 * k + 1] = ( 1.0f - onset_filter[offset + k] ) * input_frame_f[2 * offset + 2 * k + 1];
593 : }
594 :
595 12015273 : v_add( &frame_dec_f[ch_idx * 2 * num_freq_bands], aux_buffer, &frame_dec_f[ch_idx * 2 * num_freq_bands], 2 * max_band_decorr );
596 : }
597 : }
598 :
599 : /* avoid decorrelation above maximum frequency -> set to zero the remaining frequencies*/
600 15509181 : for ( ch_idx = 0; ch_idx < num_channels; ++ch_idx )
601 : {
602 : /* calc output indices */
603 12412965 : idx_in_out = 2 * ( ch_idx * num_freq_bands + h_freq_domain_decorr_ap_params->max_band_decorr );
604 :
605 : /* copy to output signal */
606 12412965 : set_zero( &frame_dec_f[idx_in_out], 2 * ( num_freq_bands - h_freq_domain_decorr_ap_params->max_band_decorr ) );
607 : }
608 :
609 : } /*end of decorrelator*/
610 :
611 5827593 : pop_wmops();
612 :
613 5827593 : return;
614 : }
615 :
616 :
617 : /*-------------------------------------------------------------------------
618 : * ivas_dirac_dec_decorr_close()
619 : *
620 : *
621 : *------------------------------------------------------------------------*/
622 :
623 10291 : void ivas_dirac_dec_decorr_close(
624 : HANDLE_DIRAC_DECORR_PARAMS *ph_freq_domain_decorr_ap_params,
625 : HANDLE_DIRAC_DECORR_STATE *ph_freq_domain_decorr_ap_state )
626 : {
627 : DIRAC_ONSET_DETECTION_STATE *dirac_onset_detection_state;
628 :
629 : /*-----------------------------------------------------------------*
630 : * check input handles
631 : *-----------------------------------------------------------------*/
632 :
633 10291 : if ( ph_freq_domain_decorr_ap_params == NULL || ph_freq_domain_decorr_ap_state == NULL )
634 : {
635 : #ifdef DEBUGGING
636 : assert( 0 && "Error: Closing decorrelation synthesis failed." );
637 : #endif
638 0 : return;
639 : }
640 :
641 10291 : if ( *ph_freq_domain_decorr_ap_params == NULL || *ph_freq_domain_decorr_ap_state == NULL )
642 : {
643 : #ifdef DEBUGGING
644 : assert( 0 && "Error: Closing decorrelation synthesis failed." );
645 : #endif
646 0 : return;
647 : }
648 :
649 : /*-----------------------------------------------------------------*
650 : * free onset filter arrays
651 : *-----------------------------------------------------------------*/
652 :
653 10291 : dirac_onset_detection_state = &( *ph_freq_domain_decorr_ap_state )->h_onset_detection_power_state;
654 :
655 10291 : if ( dirac_onset_detection_state->onset_detector_1 != NULL )
656 : {
657 10291 : free( dirac_onset_detection_state->onset_detector_1 );
658 10291 : dirac_onset_detection_state->onset_detector_1 = NULL;
659 : }
660 :
661 10291 : if ( dirac_onset_detection_state->onset_detector_2 != NULL )
662 : {
663 10291 : free( dirac_onset_detection_state->onset_detector_2 );
664 10291 : dirac_onset_detection_state->onset_detector_2 = NULL;
665 : }
666 :
667 : /*-----------------------------------------------------------------*
668 : * memory deallocation
669 : *-----------------------------------------------------------------*/
670 :
671 : /* free decorrelation buffer */
672 10291 : if ( ( *ph_freq_domain_decorr_ap_state )->decorr_buffer != NULL )
673 : {
674 7615 : free( ( *ph_freq_domain_decorr_ap_state )->decorr_buffer );
675 7615 : ( *ph_freq_domain_decorr_ap_state )->decorr_buffer = NULL;
676 : }
677 :
678 : /* free ducker smoothed direct energy buffer */
679 10291 : if ( ( *ph_freq_domain_decorr_ap_state )->direct_energy_smooth != NULL )
680 : {
681 7615 : free( ( *ph_freq_domain_decorr_ap_state )->direct_energy_smooth );
682 7615 : ( *ph_freq_domain_decorr_ap_state )->direct_energy_smooth = NULL;
683 : }
684 :
685 : /* free ducker smoothed reverb energy buffer */
686 10291 : if ( ( *ph_freq_domain_decorr_ap_state )->reverb_energy_smooth != NULL )
687 : {
688 7615 : free( ( *ph_freq_domain_decorr_ap_state )->reverb_energy_smooth );
689 7615 : ( *ph_freq_domain_decorr_ap_state )->reverb_energy_smooth = NULL;
690 : }
691 :
692 : /* free pre-delay param buffer */
693 10291 : if ( ( *ph_freq_domain_decorr_ap_params )->pre_delay != NULL )
694 : {
695 7615 : free( ( *ph_freq_domain_decorr_ap_params )->pre_delay );
696 7615 : ( *ph_freq_domain_decorr_ap_params )->pre_delay = NULL;
697 : }
698 :
699 : /* free filter length param buffer */
700 10291 : if ( ( *ph_freq_domain_decorr_ap_params )->filter_length != NULL )
701 : {
702 7615 : free( ( *ph_freq_domain_decorr_ap_params )->filter_length );
703 7615 : ( *ph_freq_domain_decorr_ap_params )->filter_length = NULL;
704 : }
705 :
706 : /* free filter coeff param buffers */
707 10291 : if ( ( *ph_freq_domain_decorr_ap_params )->filter_coeff_num_real != NULL )
708 : {
709 7615 : free( ( *ph_freq_domain_decorr_ap_params )->filter_coeff_num_real );
710 7615 : ( *ph_freq_domain_decorr_ap_params )->filter_coeff_num_real = NULL;
711 : }
712 :
713 : /* free pre-delay param buffer */
714 10291 : if ( ( *ph_freq_domain_decorr_ap_params )->filter_coeff_den_real != NULL )
715 : {
716 7615 : free( ( *ph_freq_domain_decorr_ap_params )->filter_coeff_den_real );
717 7615 : ( *ph_freq_domain_decorr_ap_params )->filter_coeff_den_real = NULL;
718 : }
719 :
720 : /* free pre-delay param buffer */
721 10291 : if ( ( *ph_freq_domain_decorr_ap_params )->phase_coeff_imag != NULL )
722 : {
723 7615 : free( ( *ph_freq_domain_decorr_ap_params )->phase_coeff_imag );
724 7615 : ( *ph_freq_domain_decorr_ap_params )->phase_coeff_imag = NULL;
725 : }
726 :
727 : /* free pre-delay param buffer */
728 10291 : if ( ( *ph_freq_domain_decorr_ap_params )->phase_coeff_real != NULL )
729 : {
730 7615 : free( ( *ph_freq_domain_decorr_ap_params )->phase_coeff_real );
731 7615 : ( *ph_freq_domain_decorr_ap_params )->phase_coeff_real = NULL;
732 : }
733 :
734 : /* free pre-delay param buffer */
735 10291 : if ( ( *ph_freq_domain_decorr_ap_params )->split_frequency_bands != NULL )
736 : {
737 10291 : free( ( *ph_freq_domain_decorr_ap_params )->split_frequency_bands );
738 10291 : ( *ph_freq_domain_decorr_ap_params )->split_frequency_bands = NULL;
739 : }
740 :
741 : /* free pointers to state and parameter structs */
742 10291 : free( *ph_freq_domain_decorr_ap_params );
743 10291 : *ph_freq_domain_decorr_ap_params = NULL;
744 :
745 10291 : free( *ph_freq_domain_decorr_ap_state );
746 10291 : *ph_freq_domain_decorr_ap_state = NULL;
747 :
748 10291 : return;
749 : }
750 :
751 :
752 : /*-------------------------------------------------------------------------
753 : * Local functions
754 : *------------------------------------------------------------------------*/
755 :
756 : /* get lattice coeffs with phase offset */
757 650511 : static void get_lattice_coeffs(
758 : const int16_t band_index,
759 : const int16_t channel_index,
760 : float *lattice_coeffs )
761 : {
762 : int16_t k;
763 :
764 7171227 : for ( k = 0; k < ap_filter_length[band_index]; k++ )
765 : {
766 6520716 : float cur_lattice_coeff = ap_lattice_coeffs[band_index][channel_index * ap_filter_length[band_index] + k];
767 6520716 : lattice_coeffs[k] = cur_lattice_coeff;
768 : }
769 :
770 650511 : return;
771 : }
772 :
773 :
774 : /* convert lattice filter coeffs to all pass transfer function coeffs */
775 650511 : static void lattice2allpass(
776 : const int16_t filter_length,
777 : const float *lattice_coeffs,
778 : float *filter_coeffs_num_real,
779 : float *filter_coeffs_den_real )
780 : {
781 : int16_t i, p;
782 : float alpha_real[2][DIRAC_MAX_DECORR_FILTER_LEN + 1];
783 650511 : float *alpha_real_p_old = &alpha_real[0][0];
784 650511 : float *alpha_real_p = &alpha_real[1][0];
785 : float *tmp;
786 :
787 1951533 : for ( i = 0; i < 2; i++ )
788 : {
789 1301022 : set_f( alpha_real[i], 0.0f, DIRAC_MAX_DECORR_FILTER_LEN + 1 );
790 : }
791 :
792 650511 : alpha_real_p[0] = 1.0f;
793 650511 : alpha_real_p_old[0] = 1.0f;
794 :
795 : /* recursion */
796 7171227 : for ( p = 1; p < filter_length; p++ )
797 : {
798 6520716 : alpha_real_p[p] = lattice_coeffs[( p - 1 )];
799 :
800 42454881 : for ( i = 1; i < p; i++ )
801 : {
802 35934165 : alpha_real_p[i] = alpha_real_p_old[i] + lattice_coeffs[( p - 1 )] * alpha_real_p_old[p - i];
803 : }
804 : /* switch pointers */
805 6520716 : tmp = alpha_real_p_old;
806 6520716 : alpha_real_p_old = alpha_real_p;
807 6520716 : alpha_real_p = tmp;
808 : }
809 :
810 7821738 : for ( i = 0; i < filter_length; i++ )
811 : {
812 7171227 : filter_coeffs_den_real[i] = alpha_real_p_old[i];
813 7171227 : filter_coeffs_num_real[i] = alpha_real_p_old[filter_length - i - 1];
814 : }
815 :
816 650511 : return;
817 : }
|