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