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