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 "cnst.h"
37 : #include "ivas_cnst.h"
38 : #include "prot.h"
39 : #include "ivas_prot.h"
40 : #include "ivas_prot_rend.h"
41 : #include "ivas_rom_com.h"
42 : #include "ivas_rom_dec.h"
43 : #include <math.h>
44 : #ifdef DEBUGGING
45 : #include "debug.h"
46 : #endif
47 : #include "wmc_auto.h"
48 :
49 :
50 : /*-------------------------------------------------------------------*
51 : * ivas_sba_set_cna_cng_flag()
52 : *
53 : * Set CNA/CNG flags in IVAS SBA decoder
54 : *-------------------------------------------------------------------*/
55 :
56 7422 : void ivas_sba_set_cna_cng_flag(
57 : Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
58 : )
59 : {
60 : int16_t n, cpe_id;
61 :
62 7422 : if ( st_ivas->ivas_format == SBA_FORMAT && st_ivas->nchan_transport == 1 )
63 : {
64 : /* skip as done in init function */
65 : /* st_ivas->hSCE[0]->hCoreCoder[0]->cna_dirac_flag = 0; */
66 : /* st_ivas->hSCE[0]->hCoreCoder[0]->cng_sba_flag = 0; */
67 : }
68 6009 : else if ( st_ivas->nchan_transport == 1 && ( ( st_ivas->renderer_type == RENDERER_DIRAC && st_ivas->hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD ) || ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC ) ) )
69 : {
70 576 : st_ivas->hSCE[0]->hCoreCoder[0]->cna_dirac_flag = 1;
71 576 : st_ivas->hSCE[0]->hCoreCoder[0]->cng_sba_flag = 1;
72 : }
73 5433 : else if ( st_ivas->nchan_transport == 2 && st_ivas->ivas_format != SBA_ISM_FORMAT )
74 : {
75 6714 : for ( n = 0; n < CPE_CHANNELS; n++ )
76 : {
77 4476 : st_ivas->hCPE[0]->hCoreCoder[n]->cna_dirac_flag = 0;
78 4476 : st_ivas->hCPE[0]->hCoreCoder[n]->cng_sba_flag = 1;
79 : }
80 : }
81 : else
82 : {
83 10083 : for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
84 : {
85 20664 : for ( n = 0; n < CPE_CHANNELS; n++ )
86 : {
87 13776 : st_ivas->hCPE[cpe_id]->hCoreCoder[n]->cna_dirac_flag = 0;
88 13776 : st_ivas->hCPE[cpe_id]->hCoreCoder[n]->cng_sba_flag = 0;
89 : }
90 : }
91 : }
92 :
93 7422 : return;
94 : }
95 :
96 :
97 : /*-------------------------------------------------------------------*
98 : * ivas_sba_dec_reconfigure()
99 : *
100 : * Reconfigure IVAS SBA decoder
101 : *-------------------------------------------------------------------*/
102 :
103 5019 : ivas_error ivas_sba_dec_reconfigure(
104 : Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
105 : )
106 : {
107 : int16_t nchan_transport_old, nSCE_old, nCPE_old, nchan_hp20_old;
108 : AUDIO_CONFIG intern_config_old;
109 : int16_t numCldfbAnalyses_old, numCldfbSyntheses_old;
110 : int16_t sba_dirac_stereo_flag_old;
111 : int32_t ivas_total_brate;
112 : int32_t last_ivas_total_brate;
113 : int16_t num_channels, num_md_sub_frames;
114 : int16_t nchan_out_buff, nchan_out_buff_old;
115 : int16_t sba_analysis_order_old_flush;
116 : DECODER_CONFIG_HANDLE hDecoderConfig;
117 : ivas_error error;
118 : ISM_MODE ism_mode_old;
119 : int16_t granularity_new;
120 : int16_t nchan_transport;
121 :
122 5019 : ism_mode_old = st_ivas->ism_mode;
123 5019 : hDecoderConfig = st_ivas->hDecoderConfig;
124 5019 : ivas_total_brate = hDecoderConfig->ivas_total_brate;
125 5019 : last_ivas_total_brate = st_ivas->last_active_ivas_total_brate;
126 5019 : sba_analysis_order_old_flush = st_ivas->sba_analysis_order;
127 :
128 : /*-----------------------------------------------------------------*
129 : * Set SBA high-level parameters
130 : * Save old SBA high-level parameters
131 : *-----------------------------------------------------------------*/
132 :
133 5019 : nchan_out_buff_old = ivas_get_nchan_buffers_dec( st_ivas, sba_analysis_order_old_flush, last_ivas_total_brate );
134 :
135 5019 : ivas_init_dec_get_num_cldfb_instances( st_ivas, &numCldfbAnalyses_old, &numCldfbSyntheses_old );
136 5019 : nchan_hp20_old = getNumChanSynthesis( st_ivas );
137 :
138 5019 : if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
139 : {
140 1959 : if ( ivas_osba_ism_mode_select( ivas_total_brate, st_ivas->nchan_ism ) == ISM_SBA_MODE_DISC )
141 : {
142 765 : st_ivas->ism_mode = ISM_SBA_MODE_DISC;
143 : }
144 : else
145 : {
146 1194 : st_ivas->ism_mode = ISM_MODE_NONE;
147 : }
148 : }
149 : else
150 : {
151 3060 : st_ivas->ism_mode = ISM_MODE_NONE;
152 : }
153 :
154 5019 : nSCE_old = st_ivas->nSCE;
155 5019 : nCPE_old = st_ivas->nCPE;
156 5019 : nchan_transport_old = st_ivas->nchan_transport;
157 5019 : sba_dirac_stereo_flag_old = st_ivas->sba_dirac_stereo_flag;
158 :
159 5019 : st_ivas->sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, st_ivas->sba_order );
160 :
161 5019 : granularity_new = st_ivas->hTcBuffer->n_samples_granularity;
162 :
163 : /* we may need to flush only for binaural and OSBA and TSM */
164 5019 : if ( st_ivas->ivas_format == SBA_ISM_FORMAT && ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) )
165 : {
166 : /* determine new granularity */
167 1413 : granularity_new = ivas_jbm_dec_get_render_granularity( st_ivas->renderer_type, ivas_renderer_secondary_select( st_ivas ), st_ivas->hDecoderConfig->output_Fs );
168 :
169 1413 : if ( granularity_new > st_ivas->hTcBuffer->n_samples_granularity )
170 : {
171 : /* make sure the changed number of slots in the last subframe is not lost in the following steps */
172 402 : if ( st_ivas->hSpatParamRendCom != NULL )
173 : {
174 402 : st_ivas->hSpatParamRendCom->subframe_nbslots[st_ivas->hSpatParamRendCom->nb_subframes - 1] = st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1];
175 : }
176 402 : st_ivas->hSpar->subframe_nbslots[st_ivas->hSpar->nb_subframes - 1] = st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1];
177 : }
178 : }
179 :
180 : /* save old */
181 5019 : if ( ism_mode_old != ISM_SBA_MODE_DISC && st_ivas->hTcBuffer->tc_buffer_mode != TC_BUFFER_MODE_BUFFER )
182 : {
183 3558 : if ( st_ivas->hDirAC == NULL && st_ivas->hSpar != NULL )
184 : {
185 609 : st_ivas->hTcBuffer->num_slots = st_ivas->hSpar->num_slots;
186 609 : st_ivas->hTcBuffer->nb_subframes = st_ivas->hSpar->nb_subframes;
187 609 : st_ivas->hTcBuffer->slots_rendered = st_ivas->hSpar->slots_rendered;
188 609 : st_ivas->hTcBuffer->subframes_rendered = st_ivas->hSpar->subframes_rendered;
189 609 : mvs2s( st_ivas->hSpar->subframe_nbslots, st_ivas->hTcBuffer->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
190 : }
191 2949 : else if ( st_ivas->hSpatParamRendCom != NULL )
192 : {
193 2949 : st_ivas->hTcBuffer->num_slots = st_ivas->hSpatParamRendCom->num_slots;
194 2949 : st_ivas->hTcBuffer->nb_subframes = st_ivas->hSpatParamRendCom->nb_subframes;
195 2949 : st_ivas->hTcBuffer->slots_rendered = st_ivas->hSpatParamRendCom->slots_rendered;
196 2949 : st_ivas->hTcBuffer->subframes_rendered = st_ivas->hSpatParamRendCom->subframes_rendered;
197 2949 : mvs2s( st_ivas->hSpatParamRendCom->subframe_nbslots, st_ivas->hTcBuffer->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
198 : }
199 : }
200 :
201 : /*-----------------------------------------------------------------*
202 : * Allocate, initialize, and configure SBA handles
203 : *-----------------------------------------------------------------*/
204 :
205 : int16_t sba_order_internal;
206 : SPAR_DEC_HANDLE hSpar;
207 5019 : hSpar = st_ivas->hSpar;
208 :
209 5019 : sba_order_internal = min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER );
210 :
211 5019 : if ( hSpar != NULL )
212 : {
213 5019 : if ( ( hSpar->hPCA != NULL ) && ( ( hDecoderConfig->ivas_total_brate != PCA_BRATE ) || ( st_ivas->sba_order != 1 ) ) )
214 : {
215 36 : free( st_ivas->hSpar->hPCA );
216 36 : hSpar->hPCA = NULL;
217 : }
218 :
219 5019 : if ( nchan_transport_old != ivas_get_sba_num_TCs( ivas_total_brate, sba_order_internal ) || ( last_ivas_total_brate >= IVAS_512k && ivas_total_brate < IVAS_512k ) || ( last_ivas_total_brate < IVAS_512k && ivas_total_brate >= IVAS_512k ) )
220 : {
221 4065 : ivas_spar_dec_close( &( st_ivas->hSpar ), hDecoderConfig->output_Fs, 1 );
222 :
223 4065 : if ( ( error = ivas_spar_dec_open( st_ivas, 1 ) ) != IVAS_ERR_OK )
224 : {
225 0 : return error;
226 : }
227 : }
228 954 : else if ( last_ivas_total_brate < IVAS_24k4 && ivas_total_brate >= IVAS_24k4 )
229 : {
230 126 : num_channels = st_ivas->hSpar->hMdDec->spar_md_cfg.num_umx_chs;
231 126 : ivas_spar_md_dec_matrix_close( st_ivas->hSpar->hMdDec, num_channels );
232 :
233 126 : num_md_sub_frames = ivas_get_spar_dec_md_num_subframes( sba_order_internal, ivas_total_brate, st_ivas->last_active_ivas_total_brate );
234 126 : if ( ( error = ivas_spar_md_dec_matrix_open( st_ivas->hSpar->hMdDec, num_channels, num_md_sub_frames ) ) != IVAS_ERR_OK )
235 : {
236 0 : return error;
237 : }
238 : }
239 :
240 5019 : if ( hSpar->hPCA == NULL && st_ivas->hDecoderConfig->ivas_total_brate == PCA_BRATE && st_ivas->sba_order == 1 && ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT ) )
241 : {
242 0 : if ( ( hSpar->hPCA = (PCA_DEC_STATE *) malloc( sizeof( PCA_DEC_STATE ) ) ) == NULL )
243 : {
244 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for PCA decoder" );
245 : }
246 :
247 0 : ivas_pca_dec_init( hSpar->hPCA );
248 : }
249 :
250 5019 : ivas_spar_config( ivas_total_brate, sba_order_internal, &st_ivas->nchan_transport, &st_ivas->nSCE, &st_ivas->nCPE, &hSpar->core_nominal_brate, st_ivas->sid_format );
251 : }
252 : else
253 : {
254 0 : if ( ( error = ivas_spar_dec_open( st_ivas, 0 ) ) != IVAS_ERR_OK )
255 : {
256 0 : return error;
257 : }
258 : }
259 :
260 5019 : hSpar = st_ivas->hSpar;
261 5019 : st_ivas->sba_dirac_stereo_flag = ivas_get_sba_dirac_stereo_flag( st_ivas );
262 :
263 5019 : if ( st_ivas->nchan_transport == 1 )
264 : {
265 1800 : st_ivas->element_mode_init = IVAS_SCE;
266 : }
267 : else
268 : {
269 3219 : st_ivas->element_mode_init = IVAS_CPE_MDCT;
270 : }
271 :
272 : /*-----------------------------------------------------------------*
273 : * Renderer selection
274 : *-----------------------------------------------------------------*/
275 :
276 : /* renderer might have changed */
277 5019 : intern_config_old = st_ivas->intern_config;
278 5019 : ivas_renderer_select( st_ivas );
279 :
280 : /* side effect of the renderer selection can be a changed internal config */
281 5019 : if ( st_ivas->intern_config != intern_config_old )
282 : {
283 1005 : ivas_output_init( &( st_ivas->hIntSetup ), st_ivas->intern_config );
284 : }
285 :
286 : /*-------------------------------------------------------------------*
287 : * Reallocate and initialize binaural rendering handles
288 : *--------------------------------------------------------------------*/
289 :
290 5019 : if ( st_ivas->hBinRenderer == NULL && ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ) )
291 : {
292 : /* open fastconv binaural renderer */
293 510 : if ( ( error = ivas_binRenderer_open( st_ivas ) ) != IVAS_ERR_OK )
294 : {
295 0 : return error;
296 : }
297 : }
298 4509 : else if ( st_ivas->hBinRenderer != NULL && ( st_ivas->renderer_type != RENDERER_BINAURAL_FASTCONV && st_ivas->renderer_type != RENDERER_BINAURAL_FASTCONV_ROOM ) )
299 : {
300 495 : ivas_binRenderer_close( &st_ivas->hBinRenderer );
301 : }
302 :
303 5019 : if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX && st_ivas->hMonoDmxRenderer == NULL )
304 : {
305 0 : if ( ( error = ivas_mono_dmx_renderer_open( st_ivas ) ) != IVAS_ERR_OK )
306 : {
307 0 : return error;
308 : }
309 : }
310 :
311 5019 : if ( st_ivas->renderer_type != RENDERER_MONO_DOWNMIX )
312 : {
313 5019 : ivas_mono_dmx_renderer_close( &st_ivas->hMonoDmxRenderer );
314 : }
315 :
316 5019 : if ( ( error = ivas_dirac_sba_config( st_ivas->hQMetaData, &st_ivas->element_mode_init, ivas_total_brate, st_ivas->sba_analysis_order,
317 5019 : ivas_get_hodirac_flag( ivas_total_brate, st_ivas->sba_analysis_order ) ? IVAS_MAX_NUM_BANDS : ( IVAS_MAX_NUM_BANDS - SPAR_DIRAC_SPLIT_START_BAND ),
318 : st_ivas->ivas_format ) ) != IVAS_ERR_OK )
319 : {
320 0 : return error;
321 : }
322 :
323 5019 : if ( ( ( st_ivas->renderer_type != RENDERER_DISABLE ) && ( st_ivas->renderer_type != RENDERER_SBA_LINEAR_DEC ) ) || ( ( hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_FOA ) && ( st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_STEREO ) && ( st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_MONO ) ) )
324 3705 : {
325 : DIRAC_CONFIG_FLAG flag_config;
326 :
327 3705 : flag_config = DIRAC_OPEN;
328 3705 : if ( st_ivas->hDirAC != NULL )
329 : {
330 3621 : flag_config = DIRAC_RECONFIGURE_MODE;
331 : }
332 :
333 3705 : if ( ( error = ivas_dirac_dec_config( st_ivas, flag_config ) ) != IVAS_ERR_OK )
334 : {
335 0 : return error;
336 : }
337 : }
338 : else
339 : {
340 : int16_t band_grouping[IVAS_MAX_NUM_BANDS + 1];
341 :
342 1314 : st_ivas->hSpar->enc_param_start_band = min( IVAS_MAX_NUM_BANDS, SPAR_DIRAC_SPLIT_START_BAND );
343 1314 : if ( ivas_get_hodirac_flag( ivas_total_brate, st_ivas->sba_analysis_order ) )
344 : {
345 132 : st_ivas->hSpar->enc_param_start_band = 0;
346 :
347 132 : set_c( (int8_t *) st_ivas->hQMetaData->twoDirBands, (int8_t) 1, st_ivas->hQMetaData->q_direction[0].cfg.nbands );
348 132 : st_ivas->hQMetaData->numTwoDirBands = (uint8_t) st_ivas->hQMetaData->q_direction[0].cfg.nbands;
349 : }
350 :
351 1314 : ivas_dirac_config_bands( band_grouping, IVAS_MAX_NUM_BANDS, (int16_t) ( st_ivas->hDecoderConfig->output_Fs * INV_CLDFB_BANDWIDTH + 0.5f ),
352 1314 : st_ivas->hSpar->dirac_to_spar_md_bands, st_ivas->hQMetaData->useLowerBandRes, st_ivas->hSpar->enc_param_start_band, 0, 1 );
353 :
354 1314 : if ( st_ivas->hDirAC )
355 : {
356 75 : st_ivas->hDirAC->hConfig->enc_param_start_band = st_ivas->hSpar->enc_param_start_band;
357 : }
358 : }
359 :
360 5019 : if ( st_ivas->renderer_type == RENDERER_DISABLE )
361 : {
362 711 : ivas_dirac_rend_close( &( st_ivas->hDirACRend ) );
363 711 : ivas_spat_hSpatParamRendCom_close( &( st_ivas->hSpatParamRendCom ) );
364 711 : ivas_dirac_dec_close( &( st_ivas->hDirAC ) );
365 711 : vbap_free_data( &( st_ivas->hVBAPdata ) );
366 : }
367 :
368 5019 : if ( st_ivas->hDirAC != NULL )
369 : {
370 3705 : st_ivas->hSpar->enc_param_start_band = st_ivas->hDirAC->hConfig->enc_param_start_band;
371 : }
372 :
373 : /*-----------------------------------------------------------------*
374 : * Allocate, initialize, and configure SCE/CPE/MCT handles
375 : *-----------------------------------------------------------------*/
376 :
377 5019 : nchan_transport = st_ivas->nchan_transport;
378 5019 : if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
379 : {
380 1959 : if ( ism_mode_old == ISM_MODE_NONE && st_ivas->ism_mode == ISM_SBA_MODE_DISC )
381 558 : {
382 : int32_t temp_brate[MAX_SCE];
383 :
384 558 : st_ivas->ism_mode = ISM_SBA_MODE_DISC;
385 558 : if ( ( error = ivas_ism_metadata_dec_create( st_ivas, st_ivas->nchan_ism, temp_brate ) ) != IVAS_ERR_OK )
386 : {
387 0 : return error;
388 : }
389 :
390 558 : if ( ( st_ivas->renderer_type == RENDERER_TD_PANNING ||
391 558 : st_ivas->renderer_type == RENDERER_NON_DIEGETIC_DOWNMIX ||
392 558 : st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC ||
393 558 : st_ivas->renderer_type == RENDERER_OSBA_STEREO ||
394 480 : st_ivas->renderer_type == RENDERER_OSBA_AMBI ||
395 480 : st_ivas->renderer_type == RENDERER_OSBA_LS ||
396 480 : st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV ||
397 78 : st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ||
398 78 : st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM ) )
399 : {
400 480 : if ( ( error = ivas_ism_renderer_open( st_ivas ) ) != IVAS_ERR_OK )
401 : {
402 0 : return error;
403 : }
404 : }
405 :
406 558 : if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
407 : {
408 0 : if ( st_ivas->hMonoDmxRenderer == NULL )
409 : {
410 0 : if ( ( error = ivas_mono_dmx_renderer_open( st_ivas ) ) != IVAS_ERR_OK )
411 : {
412 0 : return error;
413 : }
414 : }
415 : }
416 : else
417 : {
418 558 : ivas_mono_dmx_renderer_close( &st_ivas->hMonoDmxRenderer );
419 : }
420 :
421 558 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV )
422 : {
423 : /* Allocate TD renderer for the objects in DISC mode */
424 402 : if ( st_ivas->hBinRendererTd == NULL )
425 : {
426 402 : if ( ( error = ivas_td_binaural_open( st_ivas ) ) != IVAS_ERR_OK )
427 : {
428 0 : return error;
429 : }
430 : }
431 :
432 402 : if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
433 : {
434 150 : if ( ( error = ivas_reverb_open( &st_ivas->hReverb, st_ivas->hHrtfStatistics, st_ivas->hRenderConfig, st_ivas->hDecoderConfig->output_Fs ) ) != IVAS_ERR_OK )
435 : {
436 0 : return error;
437 : }
438 : }
439 : }
440 :
441 : /* Allocate memory for OSBA delay buffer */
442 558 : if ( ( error = ivas_osba_data_open( st_ivas ) ) != IVAS_ERR_OK )
443 : {
444 0 : return error;
445 : }
446 :
447 558 : nchan_transport += st_ivas->nchan_ism;
448 558 : st_ivas->nCPE = ( nchan_transport + 1 ) >> 1;
449 : }
450 1401 : else if ( ism_mode_old == ISM_SBA_MODE_DISC && st_ivas->ism_mode == ISM_MODE_NONE )
451 : {
452 : /* ISM renderer handle */
453 540 : ivas_ism_renderer_close( &st_ivas->hIsmRendererData );
454 540 : ivas_ism_metadata_close( st_ivas->hIsmMetaData, 0 );
455 540 : ivas_osba_data_close( &st_ivas->hSbaIsmData );
456 :
457 : /* Time Domain binaural renderer handle */
458 540 : if ( st_ivas->hBinRendererTd != NULL )
459 : {
460 390 : ivas_td_binaural_close( &st_ivas->hBinRendererTd );
461 : }
462 :
463 540 : nchan_transport = st_ivas->nchan_transport;
464 540 : nchan_transport_old += st_ivas->nchan_ism;
465 :
466 540 : st_ivas->ism_mode = ISM_MODE_NONE;
467 : }
468 861 : else if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
469 : {
470 207 : nchan_transport = st_ivas->nchan_transport + st_ivas->nchan_ism;
471 207 : st_ivas->nCPE = ( nchan_transport + 1 ) >> 1;
472 207 : nchan_transport_old += st_ivas->nchan_ism;
473 : }
474 : }
475 :
476 5019 : if ( ( error = ivas_corecoder_dec_reconfig( st_ivas, nSCE_old, nCPE_old, nchan_transport_old, sba_dirac_stereo_flag_old, st_ivas->hDecoderConfig->ivas_total_brate / st_ivas->nchan_transport, ( st_ivas->hDecoderConfig->ivas_total_brate / st_ivas->nchan_transport ) * CPE_CHANNELS ) ) != IVAS_ERR_OK )
477 : {
478 0 : return error;
479 : }
480 :
481 : /*-----------------------------------------------------------------*
482 : * HP20 memories
483 : *-----------------------------------------------------------------*/
484 :
485 5019 : if ( ( error = ivas_hp20_dec_reconfig( st_ivas, nchan_hp20_old ) ) != IVAS_ERR_OK )
486 : {
487 0 : return error;
488 : }
489 :
490 : /*-----------------------------------------------------------------*
491 : * TD Decorrelator
492 : *-----------------------------------------------------------------*/
493 :
494 5019 : if ( st_ivas->hDiracDecBin[0] != NULL )
495 : {
496 966 : if ( ( error = ivas_td_decorr_reconfig_dec( st_ivas->ivas_format, st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->nchan_transport, st_ivas->hDecoderConfig->output_Fs, &( st_ivas->hDiracDecBin[0]->hTdDecorr ), &( st_ivas->hDiracDecBin[0]->useTdDecorr ) ) ) != IVAS_ERR_OK )
497 : {
498 0 : return error;
499 : }
500 : }
501 :
502 : /*-----------------------------------------------------------------*
503 : * CLDFB instances
504 : *-----------------------------------------------------------------*/
505 :
506 5019 : if ( ( error = ivas_cldfb_dec_reconfig( st_ivas, nchan_transport_old, numCldfbAnalyses_old, numCldfbSyntheses_old ) ) != IVAS_ERR_OK )
507 : {
508 0 : return error;
509 : }
510 :
511 : /*-----------------------------------------------------------------*
512 : * JBM TC buffers
513 : *-----------------------------------------------------------------*/
514 :
515 : {
516 : int16_t tc_nchan_to_allocate;
517 : int16_t tc_nchan_tc;
518 : TC_BUFFER_MODE tc_buffer_mode;
519 :
520 5019 : tc_buffer_mode = TC_BUFFER_MODE_RENDERER;
521 5019 : tc_nchan_tc = ivas_jbm_dec_get_num_tc_channels( st_ivas );
522 5019 : tc_nchan_to_allocate = tc_nchan_tc;
523 5019 : if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_STEREO || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_MONO )
524 : {
525 819 : if ( ( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_SBA_MODE_DISC && st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_STEREO ) )
526 : {
527 108 : tc_nchan_tc = st_ivas->hDecoderConfig->nchan_out + st_ivas->nchan_ism;
528 108 : tc_nchan_to_allocate = tc_nchan_tc;
529 : }
530 : else
531 : {
532 711 : tc_buffer_mode = TC_BUFFER_MODE_BUFFER;
533 711 : tc_nchan_tc = st_ivas->hDecoderConfig->nchan_out;
534 711 : tc_nchan_to_allocate = tc_nchan_tc;
535 : }
536 : }
537 4200 : else if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
538 : {
539 966 : tc_nchan_to_allocate = 2 * BINAURAL_CHANNELS;
540 : }
541 3234 : else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
542 : {
543 3234 : tc_nchan_to_allocate = ivas_sba_get_nchan_metadata( st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate );
544 :
545 3234 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
546 : {
547 657 : tc_nchan_to_allocate += st_ivas->nchan_ism;
548 : }
549 : }
550 : else
551 : {
552 0 : if ( st_ivas->nchan_transport == 1 && ( ( st_ivas->renderer_type == RENDERER_DIRAC && st_ivas->hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD ) || ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM ) ) )
553 : {
554 0 : tc_nchan_to_allocate++; /* we need a channel for the CNG in this case*/
555 : }
556 : }
557 :
558 5019 : if ( tc_nchan_tc != st_ivas->hTcBuffer->nchan_transport_jbm || tc_nchan_to_allocate != st_ivas->hTcBuffer->nchan_transport_internal || tc_buffer_mode != st_ivas->hTcBuffer->tc_buffer_mode || granularity_new != st_ivas->hTcBuffer->n_samples_granularity )
559 : {
560 3531 : if ( ( error = ivas_jbm_dec_tc_buffer_reconfigure( st_ivas, tc_buffer_mode, tc_nchan_tc, tc_nchan_to_allocate, tc_nchan_to_allocate, granularity_new ) ) != IVAS_ERR_OK )
561 : {
562 0 : return error;
563 : }
564 : }
565 : }
566 :
567 : /* resync SPAR and DirAC JBM info from TC Buffer */
568 5019 : if ( st_ivas->hSpatParamRendCom != NULL && st_ivas->hSpatParamRendCom->slot_size == st_ivas->hTcBuffer->n_samples_granularity )
569 : {
570 3156 : mvs2s( st_ivas->hTcBuffer->subframe_nbslots, st_ivas->hSpatParamRendCom->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
571 3156 : st_ivas->hSpatParamRendCom->nb_subframes = st_ivas->hTcBuffer->nb_subframes;
572 3156 : st_ivas->hSpatParamRendCom->subframes_rendered = st_ivas->hTcBuffer->subframes_rendered;
573 : }
574 5019 : mvs2s( st_ivas->hTcBuffer->subframe_nbslots, st_ivas->hSpar->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
575 5019 : st_ivas->hSpar->nb_subframes = st_ivas->hTcBuffer->nb_subframes;
576 5019 : st_ivas->hSpar->subframes_rendered = st_ivas->hTcBuffer->subframes_rendered;
577 :
578 5019 : if ( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV && st_ivas->ism_mode == ISM_SBA_MODE_DISC )
579 : {
580 549 : int16_t granularityMultiplier = st_ivas->hTcBuffer->n_samples_granularity / st_ivas->hSpatParamRendCom->slot_size;
581 : int16_t n;
582 4941 : for ( n = 0; n < MAX_JBM_SUBFRAMES_5MS; n++ )
583 : {
584 4392 : st_ivas->hSpatParamRendCom->subframe_nbslots[n] = st_ivas->hTcBuffer->subframe_nbslots[n] * granularityMultiplier;
585 4392 : st_ivas->hSpar->subframe_nbslots[n] = st_ivas->hSpatParamRendCom->subframe_nbslots[n];
586 : }
587 : }
588 :
589 : /*-----------------------------------------------------------------*
590 : * floating-point output audio buffers
591 : *-----------------------------------------------------------------*/
592 :
593 5019 : nchan_out_buff = ivas_get_nchan_buffers_dec( st_ivas, st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate );
594 :
595 5019 : if ( ( error = ivas_output_buff_dec( st_ivas->p_output_f, nchan_out_buff_old, nchan_out_buff ) ) != IVAS_ERR_OK )
596 : {
597 0 : return error;
598 : }
599 :
600 5019 : return IVAS_ERR_OK;
601 : }
602 :
603 :
604 : /*-------------------------------------------------------------------*
605 : * ivas_sba_dec_digest_tc()
606 : *
607 : *
608 : *-------------------------------------------------------------------*/
609 :
610 555051 : void ivas_sba_dec_digest_tc(
611 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
612 : const int16_t nCldfbSlots, /* i : number of CLDFB slots */
613 : const int16_t nSamplesForRendering /* i : number of samples provided */
614 : )
615 : {
616 : int16_t ch_idx, nchan_transport;
617 :
618 : /* set the md map */
619 555051 : if ( st_ivas->hDirAC || st_ivas->renderer_type == RENDERER_OMASA_OBJECT_EXT )
620 : {
621 425832 : ivas_dirac_dec_set_md_map( st_ivas, nCldfbSlots );
622 : }
623 :
624 555051 : if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
625 : {
626 431292 : ivas_spar_dec_digest_tc( st_ivas, st_ivas->nchan_transport, nCldfbSlots, nSamplesForRendering );
627 : }
628 :
629 555051 : if ( st_ivas->hDiracDecBin[0] != NULL && st_ivas->hDiracDecBin[0]->useTdDecorr )
630 : {
631 : int16_t nSamplesLeftForTD, default_frame;
632 : float *decorr_signal[BINAURAL_CHANNELS];
633 : float *p_tc[2 * BINAURAL_CHANNELS];
634 :
635 82866 : default_frame = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / FRAMES_PER_SEC );
636 82866 : nSamplesLeftForTD = nSamplesForRendering;
637 :
638 248598 : for ( ch_idx = 0; ch_idx < BINAURAL_CHANNELS; ch_idx++ )
639 : {
640 165732 : decorr_signal[ch_idx] = st_ivas->hTcBuffer->tc[ch_idx + BINAURAL_CHANNELS];
641 165732 : p_tc[ch_idx] = st_ivas->hTcBuffer->tc[ch_idx];
642 : }
643 :
644 165762 : while ( nSamplesLeftForTD )
645 : {
646 82896 : int16_t nSamplesToDecorr = min( nSamplesLeftForTD, default_frame );
647 :
648 82896 : if ( st_ivas->hDiracDecBin[0]->hTdDecorr )
649 : {
650 82896 : ivas_td_decorr_process( st_ivas->hDiracDecBin[0]->hTdDecorr, p_tc, decorr_signal, nSamplesToDecorr );
651 : }
652 :
653 248688 : for ( ch_idx = 0; ch_idx < BINAURAL_CHANNELS; ch_idx++ )
654 : {
655 165792 : decorr_signal[ch_idx] += nSamplesToDecorr;
656 165792 : p_tc[ch_idx] += nSamplesToDecorr;
657 : }
658 82896 : nSamplesLeftForTD -= nSamplesToDecorr;
659 : }
660 : }
661 :
662 : /* if we have a late CNG generation, do it here */
663 555051 : nchan_transport = st_ivas->nchan_transport;
664 555051 : if ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->hDecoderConfig->ivas_total_brate < MASA_STEREO_MIN_BITRATE && ( st_ivas->hDecoderConfig->ivas_total_brate > IVAS_SID_5k2 || ( st_ivas->hDecoderConfig->ivas_total_brate <= IVAS_SID_5k2 && st_ivas->nCPE > 0 && st_ivas->hCPE[0]->nchan_out == 1 ) ) )
665 : {
666 18411 : nchan_transport = 1; /* Only one channel transported */
667 : }
668 :
669 555051 : if ( ( ( st_ivas->ivas_format != SBA_FORMAT && st_ivas->ivas_format != SBA_ISM_FORMAT ) && st_ivas->nchan_transport == 1 && st_ivas->hSCE[0]->hCoreCoder[0] != NULL && st_ivas->hSCE[0]->hCoreCoder[0]->cna_dirac_flag ) ||
670 510222 : ( ( st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC ) && ( nchan_transport == 1 && st_ivas->nchan_transport != 2 && st_ivas->hSCE[0]->hCoreCoder[0] != NULL && st_ivas->hSCE[0]->hCoreCoder[0]->cng_sba_flag ) ) )
671 : {
672 57594 : Decoder_State *st = st_ivas->hSCE[0]->hCoreCoder[0];
673 57594 : generate_masking_noise_lb_dirac( st->hFdCngDec->hFdCngCom, st_ivas->hTcBuffer->tc[1], nCldfbSlots, st_ivas->hSpatParamRendCom, st->cna_dirac_flag && st->flag_cna );
674 : }
675 :
676 555051 : return;
677 : }
678 :
679 :
680 : /*-------------------------------------------------------------------*
681 : * ivas_sba_dec_render()
682 : *
683 : *
684 : *-------------------------------------------------------------------*/
685 :
686 719066 : ivas_error ivas_sba_dec_render(
687 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
688 : const uint16_t nSamplesAsked, /* i : number of CLDFB slots requested */
689 : uint16_t *nSamplesRendered, /* o : number of CLDFB slots rendered */
690 : uint16_t *nSamplesAvailableNext, /* o : number of CLDFB slots still to render */
691 : float *output_f[] /* o : rendered time signal */
692 : )
693 : {
694 : int16_t slots_to_render, first_sf, last_sf, subframe_idx;
695 : uint16_t slot_size, ch;
696 : uint16_t nchan_internal, nchan_out;
697 : SPAR_DEC_HANDLE hSpar;
698 : SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
699 : float *output_f_local[MAX_OUTPUT_CHANNELS];
700 : ivas_error error;
701 :
702 719066 : hSpar = st_ivas->hSpar;
703 719066 : hSpatParamRendCom = st_ivas->hSpatParamRendCom;
704 719066 : nchan_internal = ivas_sba_get_nchan_metadata( st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate );
705 719066 : nchan_out = st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe;
706 :
707 : #ifdef DEBUGGING
708 : assert( hSpar );
709 : #endif
710 7710425 : for ( ch = 0; ch < nchan_out; ch++ )
711 : {
712 6991359 : output_f_local[ch] = output_f[ch];
713 : }
714 :
715 719066 : slot_size = NS2SA( st_ivas->hDecoderConfig->output_Fs, CLDFB_SLOT_NS );
716 :
717 : /* loop for synthesis, assume we always have to render in multiples of 5ms subframes with spills */
718 719066 : slots_to_render = min( hSpar->num_slots - hSpar->slots_rendered, nSamplesAsked / slot_size );
719 719066 : *nSamplesRendered = slots_to_render * slot_size;
720 719066 : first_sf = hSpar->subframes_rendered;
721 719066 : last_sf = first_sf;
722 :
723 1993043 : while ( slots_to_render > 0 )
724 : {
725 1273977 : slots_to_render -= hSpar->subframe_nbslots[last_sf];
726 1273977 : last_sf++;
727 : }
728 : #ifdef DEBUGGING
729 : assert( slots_to_render == 0 );
730 : #endif
731 :
732 1993043 : for ( subframe_idx = first_sf; subframe_idx < last_sf; subframe_idx++ )
733 : {
734 1273977 : int16_t n_samples_sf = slot_size * hSpar->subframe_nbslots[subframe_idx];
735 :
736 1273977 : ivas_spar_dec_upmixer_sf( st_ivas, output_f_local, nchan_internal );
737 :
738 1273977 : if ( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_SBA_MODE_DISC )
739 : {
740 239088 : float gain = st_ivas->hSbaIsmData->gain_bed;
741 239088 : if ( gain != 1.0f && gain >= 0.0f )
742 : {
743 355572 : for ( ch = 0; ch < nchan_out; ch++ )
744 : {
745 : int16_t i;
746 75480048 : for ( i = 0; i < n_samples_sf; i++ )
747 : {
748 75155760 : output_f_local[ch][i] *= gain;
749 : }
750 : }
751 : }
752 : }
753 :
754 13698897 : for ( ch = 0; ch < nchan_out; ch++ )
755 : {
756 12424920 : output_f_local[ch] += n_samples_sf;
757 : }
758 :
759 : /* update combined orientation access index */
760 1273977 : ivas_combined_orientation_update_index( st_ivas->hCombinedOrientationData, n_samples_sf );
761 : }
762 :
763 719066 : if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_DEC )
764 : {
765 402894 : if ( ( error = ivas_sba_linear_renderer( output_f, *nSamplesRendered, st_ivas->hIntSetup.nchan_out_woLFE, 0, st_ivas->hDecoderConfig->output_config, st_ivas->hOutSetup ) ) != IVAS_ERR_OK )
766 : {
767 0 : return error;
768 : }
769 : }
770 :
771 719066 : if ( st_ivas->hDirAC != NULL && hSpar->slots_rendered == hSpar->num_slots )
772 : {
773 187827 : if ( st_ivas->hDirAC->hConfig->dec_param_estim == 1 )
774 : {
775 147447 : hSpatParamRendCom->dirac_read_idx = ( hSpatParamRendCom->dirac_read_idx + DEFAULT_JBM_CLDFB_TIMESLOTS ) % hSpatParamRendCom->dirac_md_buffer_length;
776 : }
777 : else
778 : {
779 40380 : hSpatParamRendCom->dirac_read_idx = ( hSpatParamRendCom->dirac_read_idx + DEFAULT_JBM_SUBFRAMES_5MS ) % hSpatParamRendCom->dirac_md_buffer_length;
780 : }
781 : }
782 :
783 719066 : *nSamplesAvailableNext = ( hSpar->num_slots - hSpar->slots_rendered ) * slot_size;
784 :
785 719066 : return IVAS_ERR_OK;
786 : }
|