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 7425 : 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 7425 : 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 6012 : 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 5436 : 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 10092 : for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
84 : {
85 20682 : for ( n = 0; n < CPE_CHANNELS; n++ )
86 : {
87 13788 : st_ivas->hCPE[cpe_id]->hCoreCoder[n]->cna_dirac_flag = 0;
88 13788 : st_ivas->hCPE[cpe_id]->hCoreCoder[n]->cng_sba_flag = 0;
89 : }
90 : }
91 : }
92 :
93 7425 : 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;
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 5019 : ism_mode_old = st_ivas->ism_mode;
122 5019 : hDecoderConfig = st_ivas->hDecoderConfig;
123 5019 : ivas_total_brate = hDecoderConfig->ivas_total_brate;
124 5019 : 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 5019 : ivas_init_dec_get_num_cldfb_instances( st_ivas, &numCldfbAnalyses_old, &numCldfbSyntheses_old );
132 5019 : nchan_hp20_old = getNumChanSynthesis( st_ivas );
133 :
134 5019 : if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
135 : {
136 1959 : if ( ivas_osba_ism_mode_select( ivas_total_brate, st_ivas->nchan_ism ) == ISM_SBA_MODE_DISC )
137 : {
138 765 : st_ivas->ism_mode = ISM_SBA_MODE_DISC;
139 : }
140 : else
141 : {
142 1194 : st_ivas->ism_mode = ISM_MODE_NONE;
143 : }
144 : }
145 : else
146 : {
147 3060 : st_ivas->ism_mode = ISM_MODE_NONE;
148 : }
149 :
150 5019 : nSCE_old = st_ivas->nSCE;
151 5019 : nCPE_old = st_ivas->nCPE;
152 5019 : nchan_transport_old = st_ivas->nchan_transport;
153 5019 : sba_dirac_stereo_flag_old = st_ivas->sba_dirac_stereo_flag;
154 :
155 5019 : st_ivas->sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, st_ivas->sba_order );
156 :
157 5019 : granularity_new = st_ivas->hTcBuffer->n_samples_granularity;
158 :
159 : /* we may need to flush only for binaural and OSBA and TSM */
160 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 ) )
161 : {
162 : /* determine new granularity */
163 1413 : 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 1413 : if ( granularity_new < st_ivas->hTcBuffer->n_samples_granularity )
168 : {
169 : /* flush already done in IVAS_DEC_ReadFormat() */
170 : }
171 1023 : else if ( granularity_new > st_ivas->hTcBuffer->n_samples_granularity )
172 : {
173 402 : 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 402 : if ( st_ivas->hSpatParamRendCom != NULL )
180 : {
181 402 : st_ivas->hSpatParamRendCom->subframe_nbslots[st_ivas->hSpatParamRendCom->nb_subframes - 1] = st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1];
182 : }
183 402 : 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 5019 : if ( ism_mode_old != ISM_SBA_MODE_DISC && st_ivas->hTcBuffer->tc_buffer_mode != TC_BUFFER_MODE_BUFFER )
200 : {
201 3558 : if ( st_ivas->hDirAC == NULL && st_ivas->hSpar != NULL )
202 : {
203 609 : st_ivas->hTcBuffer->num_slots = st_ivas->hSpar->num_slots;
204 609 : st_ivas->hTcBuffer->nb_subframes = st_ivas->hSpar->nb_subframes;
205 609 : st_ivas->hTcBuffer->slots_rendered = st_ivas->hSpar->slots_rendered;
206 609 : st_ivas->hTcBuffer->subframes_rendered = st_ivas->hSpar->subframes_rendered;
207 609 : mvs2s( st_ivas->hSpar->subframe_nbslots, st_ivas->hTcBuffer->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
208 : }
209 2949 : else if ( st_ivas->hSpatParamRendCom != NULL )
210 : {
211 2949 : st_ivas->hTcBuffer->num_slots = st_ivas->hSpatParamRendCom->num_slots;
212 2949 : st_ivas->hTcBuffer->nb_subframes = st_ivas->hSpatParamRendCom->nb_subframes;
213 2949 : st_ivas->hTcBuffer->slots_rendered = st_ivas->hSpatParamRendCom->slots_rendered;
214 2949 : st_ivas->hTcBuffer->subframes_rendered = st_ivas->hSpatParamRendCom->subframes_rendered;
215 2949 : 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 5019 : hSpar = st_ivas->hSpar;
226 :
227 5019 : sba_order_internal = min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER );
228 :
229 5019 : if ( hSpar != NULL )
230 : {
231 5019 : if ( ( hSpar->hPCA != NULL ) && ( ( hDecoderConfig->ivas_total_brate != PCA_BRATE ) || ( st_ivas->sba_order != 1 ) ) )
232 : {
233 36 : free( st_ivas->hSpar->hPCA );
234 36 : hSpar->hPCA = NULL;
235 : }
236 :
237 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 ) )
238 : {
239 4065 : ivas_spar_dec_close( &( st_ivas->hSpar ), hDecoderConfig->output_Fs, 1 );
240 :
241 4065 : if ( ( error = ivas_spar_dec_open( st_ivas, 1 ) ) != IVAS_ERR_OK )
242 : {
243 0 : return error;
244 : }
245 : }
246 954 : else if ( last_ivas_total_brate < IVAS_24k4 && ivas_total_brate >= IVAS_24k4 )
247 : {
248 126 : num_channels = st_ivas->hSpar->hMdDec->spar_md_cfg.num_umx_chs;
249 126 : ivas_spar_md_dec_matrix_close( st_ivas->hSpar->hMdDec, num_channels );
250 :
251 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 );
252 126 : 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 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 ) )
259 : {
260 0 : 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 0 : ivas_pca_dec_init( hSpar->hPCA );
266 : }
267 :
268 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 );
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 5019 : hSpar = st_ivas->hSpar;
279 5019 : st_ivas->sba_dirac_stereo_flag = ivas_get_sba_dirac_stereo_flag( st_ivas );
280 :
281 5019 : if ( st_ivas->nchan_transport == 1 )
282 : {
283 1800 : st_ivas->element_mode_init = IVAS_SCE;
284 : }
285 : else
286 : {
287 3219 : st_ivas->element_mode_init = IVAS_CPE_MDCT;
288 : }
289 :
290 : /*-----------------------------------------------------------------*
291 : * Renderer selection
292 : *-----------------------------------------------------------------*/
293 :
294 : /* renderer might have changed */
295 5019 : intern_config_old = st_ivas->intern_config;
296 5019 : ivas_renderer_select( st_ivas );
297 :
298 : /* side effect of the renderer selection can be a changed internal config */
299 5019 : if ( st_ivas->intern_config != intern_config_old )
300 : {
301 1005 : ivas_output_init( &( st_ivas->hIntSetup ), st_ivas->intern_config );
302 : }
303 :
304 : /*-------------------------------------------------------------------*
305 : * Reallocate and initialize binaural rendering handles
306 : *--------------------------------------------------------------------*/
307 :
308 5019 : 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 510 : if ( ( error = ivas_binRenderer_open( st_ivas ) ) != IVAS_ERR_OK )
312 : {
313 0 : return error;
314 : }
315 : }
316 4509 : else if ( st_ivas->hBinRenderer != NULL && ( st_ivas->renderer_type != RENDERER_BINAURAL_FASTCONV && st_ivas->renderer_type != RENDERER_BINAURAL_FASTCONV_ROOM ) )
317 : {
318 495 : ivas_binRenderer_close( &st_ivas->hBinRenderer );
319 : }
320 :
321 5019 : if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX && st_ivas->hMonoDmxRenderer == NULL )
322 : {
323 0 : if ( ( error = ivas_mono_dmx_renderer_open( st_ivas ) ) != IVAS_ERR_OK )
324 : {
325 0 : return error;
326 : }
327 : }
328 :
329 5019 : if ( st_ivas->renderer_type != RENDERER_MONO_DOWNMIX )
330 : {
331 5019 : ivas_mono_dmx_renderer_close( &st_ivas->hMonoDmxRenderer );
332 : }
333 :
334 5019 : if ( ( error = ivas_dirac_sba_config( st_ivas->hQMetaData, &st_ivas->element_mode_init, ivas_total_brate, st_ivas->sba_analysis_order,
335 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 ),
336 : st_ivas->ivas_format ) ) != IVAS_ERR_OK )
337 : {
338 0 : return error;
339 : }
340 :
341 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 ) ) )
342 3705 : {
343 : DIRAC_CONFIG_FLAG flag_config;
344 :
345 3705 : flag_config = DIRAC_OPEN;
346 3705 : if ( st_ivas->hDirAC != NULL )
347 : {
348 3621 : flag_config = DIRAC_RECONFIGURE_MODE;
349 : }
350 :
351 3705 : 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 1314 : st_ivas->hSpar->enc_param_start_band = min( IVAS_MAX_NUM_BANDS, SPAR_DIRAC_SPLIT_START_BAND );
361 1314 : if ( ivas_get_hodirac_flag( ivas_total_brate, st_ivas->sba_analysis_order ) )
362 : {
363 132 : st_ivas->hSpar->enc_param_start_band = 0;
364 :
365 132 : set_c( (int8_t *) st_ivas->hQMetaData->twoDirBands, (int8_t) 1, st_ivas->hQMetaData->q_direction[0].cfg.nbands );
366 132 : st_ivas->hQMetaData->numTwoDirBands = (uint8_t) st_ivas->hQMetaData->q_direction[0].cfg.nbands;
367 : }
368 :
369 1314 : ivas_dirac_config_bands( band_grouping, IVAS_MAX_NUM_BANDS, (int16_t) ( st_ivas->hDecoderConfig->output_Fs * INV_CLDFB_BANDWIDTH + 0.5f ),
370 1314 : st_ivas->hSpar->dirac_to_spar_md_bands, st_ivas->hQMetaData->useLowerBandRes, st_ivas->hSpar->enc_param_start_band, 0, 1 );
371 :
372 1314 : if ( st_ivas->hDirAC )
373 : {
374 75 : st_ivas->hDirAC->hConfig->enc_param_start_band = st_ivas->hSpar->enc_param_start_band;
375 : }
376 : }
377 :
378 5019 : if ( st_ivas->renderer_type == RENDERER_DISABLE )
379 : {
380 711 : ivas_dirac_rend_close( &( st_ivas->hDirACRend ) );
381 711 : ivas_spat_hSpatParamRendCom_close( &( st_ivas->hSpatParamRendCom ) );
382 711 : ivas_dirac_dec_close( &( st_ivas->hDirAC ) );
383 711 : vbap_free_data( &( st_ivas->hVBAPdata ) );
384 : }
385 :
386 5019 : if ( st_ivas->hDirAC != NULL )
387 : {
388 3705 : 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 5019 : nchan_transport = st_ivas->nchan_transport;
396 5019 : if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
397 : {
398 1959 : if ( ism_mode_old == ISM_MODE_NONE && st_ivas->ism_mode == ISM_SBA_MODE_DISC )
399 558 : {
400 : int32_t temp_brate[MAX_SCE];
401 :
402 558 : st_ivas->ism_mode = ISM_SBA_MODE_DISC;
403 558 : 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 558 : if ( ( st_ivas->renderer_type == RENDERER_TD_PANNING ||
409 558 : st_ivas->renderer_type == RENDERER_NON_DIEGETIC_DOWNMIX ||
410 558 : st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC ||
411 558 : st_ivas->renderer_type == RENDERER_OSBA_STEREO ||
412 480 : st_ivas->renderer_type == RENDERER_OSBA_AMBI ||
413 480 : st_ivas->renderer_type == RENDERER_OSBA_LS ||
414 480 : st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV ||
415 78 : st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ||
416 78 : st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM ) )
417 : {
418 480 : if ( ( error = ivas_ism_renderer_open( st_ivas ) ) != IVAS_ERR_OK )
419 : {
420 0 : return error;
421 : }
422 : }
423 :
424 558 : if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
425 : {
426 0 : 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 558 : ivas_mono_dmx_renderer_close( &st_ivas->hMonoDmxRenderer );
437 : }
438 :
439 558 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV )
440 : {
441 : /* Allocate TD renderer for the objects in DISC mode */
442 402 : if ( st_ivas->hBinRendererTd == NULL )
443 : {
444 402 : if ( ( error = ivas_td_binaural_open( st_ivas ) ) != IVAS_ERR_OK )
445 : {
446 0 : return error;
447 : }
448 : }
449 :
450 402 : if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
451 : {
452 150 : 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 558 : if ( ( error = ivas_osba_data_open( st_ivas ) ) != IVAS_ERR_OK )
461 : {
462 0 : return error;
463 : }
464 :
465 558 : nchan_transport += st_ivas->nchan_ism;
466 558 : st_ivas->nCPE = ( nchan_transport + 1 ) >> 1;
467 : }
468 1401 : else if ( ism_mode_old == ISM_SBA_MODE_DISC && st_ivas->ism_mode == ISM_MODE_NONE )
469 : {
470 : /* ISM renderer handle */
471 540 : ivas_ism_renderer_close( &st_ivas->hIsmRendererData );
472 540 : ivas_ism_metadata_close( st_ivas->hIsmMetaData, 0 );
473 540 : ivas_osba_data_close( &st_ivas->hSbaIsmData );
474 :
475 : /* Time Domain binaural renderer handle */
476 540 : if ( st_ivas->hBinRendererTd != NULL )
477 : {
478 390 : ivas_td_binaural_close( &st_ivas->hBinRendererTd );
479 : }
480 :
481 540 : nchan_transport = st_ivas->nchan_transport;
482 540 : nchan_transport_old += st_ivas->nchan_ism;
483 :
484 540 : st_ivas->ism_mode = ISM_MODE_NONE;
485 : }
486 861 : else if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
487 : {
488 207 : nchan_transport = st_ivas->nchan_transport + st_ivas->nchan_ism;
489 207 : st_ivas->nCPE = ( nchan_transport + 1 ) >> 1;
490 207 : nchan_transport_old += st_ivas->nchan_ism;
491 : }
492 : }
493 :
494 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 )
495 : {
496 0 : return error;
497 : }
498 :
499 : /*-----------------------------------------------------------------*
500 : * HP20 memories
501 : *-----------------------------------------------------------------*/
502 :
503 5019 : 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 5019 : if ( st_ivas->hDiracDecBin[0] != NULL )
513 : {
514 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 )
515 : {
516 0 : return error;
517 : }
518 : }
519 :
520 : /*-----------------------------------------------------------------*
521 : * CLDFB instances
522 : *-----------------------------------------------------------------*/
523 :
524 5019 : 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 5019 : tc_buffer_mode = TC_BUFFER_MODE_RENDERER;
539 5019 : tc_nchan_tc = ivas_jbm_dec_get_num_tc_channels( st_ivas );
540 5019 : tc_nchan_to_allocate = tc_nchan_tc;
541 5019 : if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_STEREO || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_MONO )
542 : {
543 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 ) )
544 : {
545 108 : tc_nchan_tc = st_ivas->hDecoderConfig->nchan_out + st_ivas->nchan_ism;
546 108 : tc_nchan_to_allocate = tc_nchan_tc;
547 : }
548 : else
549 : {
550 711 : tc_buffer_mode = TC_BUFFER_MODE_BUFFER;
551 711 : tc_nchan_tc = st_ivas->hDecoderConfig->nchan_out;
552 711 : tc_nchan_to_allocate = tc_nchan_tc;
553 : }
554 : }
555 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 )
556 : {
557 966 : tc_nchan_to_allocate = 2 * BINAURAL_CHANNELS;
558 : }
559 3234 : else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
560 : {
561 3234 : tc_nchan_to_allocate = ivas_sba_get_nchan_metadata( st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate );
562 :
563 3234 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
564 : {
565 657 : 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 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 )
577 : {
578 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 )
579 : {
580 0 : return error;
581 : }
582 : }
583 : }
584 :
585 : /* resync SPAR and DirAC JBM info from TC Buffer */
586 5019 : if ( st_ivas->hSpatParamRendCom != NULL && st_ivas->hSpatParamRendCom->slot_size == st_ivas->hTcBuffer->n_samples_granularity )
587 : {
588 3156 : mvs2s( st_ivas->hTcBuffer->subframe_nbslots, st_ivas->hSpatParamRendCom->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
589 3156 : st_ivas->hSpatParamRendCom->nb_subframes = st_ivas->hTcBuffer->nb_subframes;
590 3156 : st_ivas->hSpatParamRendCom->subframes_rendered = st_ivas->hTcBuffer->subframes_rendered;
591 : }
592 5019 : mvs2s( st_ivas->hTcBuffer->subframe_nbslots, st_ivas->hSpar->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
593 5019 : st_ivas->hSpar->nb_subframes = st_ivas->hTcBuffer->nb_subframes;
594 5019 : st_ivas->hSpar->subframes_rendered = st_ivas->hTcBuffer->subframes_rendered;
595 :
596 5019 : 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 549 : int16_t granularityMultiplier = st_ivas->hTcBuffer->n_samples_granularity / st_ivas->hSpatParamRendCom->slot_size;
599 : int16_t n;
600 4941 : for ( n = 0; n < MAX_JBM_SUBFRAMES_5MS; n++ )
601 : {
602 4392 : st_ivas->hSpatParamRendCom->subframe_nbslots[n] = st_ivas->hTcBuffer->subframe_nbslots[n] * granularityMultiplier;
603 4392 : 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 5019 : nchan_out_buff = ivas_get_nchan_buffers_dec( st_ivas, st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate );
612 5019 : 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 5019 : return IVAS_ERR_OK;
618 : }
619 :
620 :
621 : /*-------------------------------------------------------------------*
622 : * ivas_sba_dec_digest_tc()
623 : *
624 : *
625 : *-------------------------------------------------------------------*/
626 :
627 558051 : 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 558051 : if ( st_ivas->hDirAC || st_ivas->renderer_type == RENDERER_OMASA_OBJECT_EXT )
637 : {
638 428832 : ivas_dirac_dec_set_md_map( st_ivas, nCldfbSlots );
639 : }
640 :
641 558051 : if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
642 : {
643 434292 : ivas_spar_dec_digest_tc( st_ivas, st_ivas->nchan_transport, nCldfbSlots, nSamplesForRendering );
644 : }
645 :
646 558051 : 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 82866 : default_frame = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / FRAMES_PER_SEC );
653 82866 : nSamplesLeftForTD = nSamplesForRendering;
654 :
655 248598 : for ( ch_idx = 0; ch_idx < BINAURAL_CHANNELS; ch_idx++ )
656 : {
657 165732 : decorr_signal[ch_idx] = st_ivas->hTcBuffer->tc[ch_idx + BINAURAL_CHANNELS];
658 165732 : p_tc[ch_idx] = st_ivas->hTcBuffer->tc[ch_idx];
659 : }
660 :
661 165762 : while ( nSamplesLeftForTD )
662 : {
663 82896 : int16_t nSamplesToDecorr = min( nSamplesLeftForTD, default_frame );
664 :
665 82896 : if ( st_ivas->hDiracDecBin[0]->hTdDecorr )
666 : {
667 82896 : ivas_td_decorr_process( st_ivas->hDiracDecBin[0]->hTdDecorr, p_tc, decorr_signal, nSamplesToDecorr );
668 : }
669 :
670 248688 : for ( ch_idx = 0; ch_idx < BINAURAL_CHANNELS; ch_idx++ )
671 : {
672 165792 : decorr_signal[ch_idx] += nSamplesToDecorr;
673 165792 : p_tc[ch_idx] += nSamplesToDecorr;
674 : }
675 82896 : nSamplesLeftForTD -= nSamplesToDecorr;
676 : }
677 : }
678 :
679 : /* if we have a late CNG generation, do it here */
680 558051 : nchan_transport = st_ivas->nchan_transport;
681 558051 : 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 18411 : nchan_transport = 1; /* Only one channel transported */
684 : }
685 :
686 558051 : 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 513222 : ( ( 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 57594 : Decoder_State *st = st_ivas->hSCE[0]->hCoreCoder[0];
690 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 );
691 : }
692 :
693 558051 : return;
694 : }
695 :
696 :
697 : /*-------------------------------------------------------------------*
698 : * ivas_sba_dec_render()
699 : *
700 : *
701 : *-------------------------------------------------------------------*/
702 :
703 726066 : 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 726066 : hSpar = st_ivas->hSpar;
720 726066 : hSpatParamRendCom = st_ivas->hSpatParamRendCom;
721 726066 : nchan_internal = ivas_sba_get_nchan_metadata( st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate );
722 726066 : nchan_out = st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe;
723 :
724 726066 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
725 : {
726 131443 : nchan_out = max( nchan_internal, st_ivas->hDecoderConfig->nchan_out );
727 :
728 131443 : if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
729 : {
730 7200 : nchan_out = max( nchan_internal, st_ivas->hDecoderConfig->nchan_out - st_ivas->nchan_ism );
731 : }
732 : #ifdef FIX_1372_OSBA_OBJECT_EDITING
733 124243 : else if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
734 : {
735 28000 : nchan_out = BINAURAL_CHANNELS;
736 : }
737 : #endif
738 : }
739 :
740 726066 : 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 5489079 : for ( ch = 0; ch < nchan_out; ch++ )
746 : {
747 4763013 : output_f_local[ch] = output_f[ch];
748 : }
749 :
750 726066 : 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 726066 : slots_to_render = min( hSpar->num_slots - hSpar->slots_rendered, nSamplesAsked / slot_size );
754 726066 : *nSamplesRendered = slots_to_render * slot_size;
755 726066 : first_sf = hSpar->subframes_rendered;
756 726066 : last_sf = first_sf;
757 :
758 2012043 : while ( slots_to_render > 0 )
759 : {
760 1285977 : slots_to_render -= hSpar->subframe_nbslots[last_sf];
761 1285977 : last_sf++;
762 : }
763 : #ifdef DEBUGGING
764 : assert( slots_to_render == 0 );
765 : #endif
766 :
767 2012043 : for ( subframe_idx = first_sf; subframe_idx < last_sf; subframe_idx++ )
768 : {
769 1285977 : int16_t n_samples_sf = slot_size * hSpar->subframe_nbslots[subframe_idx];
770 :
771 1285977 : ivas_spar_dec_upmixer_sf( st_ivas, output_f_local, nchan_internal );
772 :
773 1285977 : if ( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_SBA_MODE_DISC )
774 : {
775 239088 : float gain = st_ivas->hSbaIsmData->gain_bed;
776 239088 : if ( gain != 1.0f && gain >= 0.0f )
777 : {
778 140268 : for ( ch = 0; ch < nchan_out; ch++ )
779 : {
780 : int16_t i;
781 23591784 : for ( i = 0; i < n_samples_sf; i++ )
782 : {
783 23482800 : output_f_local[ch][i] *= gain;
784 : }
785 : }
786 : }
787 : }
788 :
789 9901761 : for ( ch = 0; ch < nchan_out; ch++ )
790 : {
791 8615784 : output_f_local[ch] += n_samples_sf;
792 : }
793 :
794 : /* update combined orientation access index */
795 1285977 : ivas_combined_orientation_update_index( st_ivas->hCombinedOrientationData, n_samples_sf );
796 : }
797 :
798 726066 : if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_DEC )
799 : {
800 409894 : 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 726066 : if ( st_ivas->hDirAC != NULL && hSpar->slots_rendered == hSpar->num_slots )
807 : {
808 190827 : if ( st_ivas->hDirAC->hConfig->dec_param_estim == 1 )
809 : {
810 150447 : hSpatParamRendCom->dirac_read_idx = ( hSpatParamRendCom->dirac_read_idx + DEFAULT_JBM_CLDFB_TIMESLOTS ) % hSpatParamRendCom->dirac_md_buffer_length;
811 : }
812 : else
813 : {
814 40380 : hSpatParamRendCom->dirac_read_idx = ( hSpatParamRendCom->dirac_read_idx + DEFAULT_JBM_SUBFRAMES_5MS ) % hSpatParamRendCom->dirac_md_buffer_length;
815 : }
816 : }
817 :
818 726066 : *nSamplesAvailableNext = ( hSpar->num_slots - hSpar->slots_rendered ) * slot_size;
819 :
820 726066 : return IVAS_ERR_OK;
821 : }
|