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 <stdint.h>
34 : #include "options.h"
35 : #include "prot.h"
36 : #include "ivas_prot.h"
37 : #include "ivas_prot_rend.h"
38 : #ifdef DEBUGGING
39 : #include "debug.h"
40 : #endif
41 : #include "wmc_auto.h"
42 :
43 : /*-------------------------------------------------------------------------*
44 : * ivas_ism_bitrate_switching_dec()
45 : *
46 : *
47 : *-------------------------------------------------------------------------*/
48 :
49 7563 : static ivas_error ivas_ism_bitrate_switching_dec(
50 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
51 : const int16_t nchan_transport_old, /* i : last number of transport channels */
52 : const ISM_MODE last_ism_mode /* i : last ISM mode */
53 : )
54 : {
55 : ivas_error error;
56 : int32_t element_brate_tmp[MAX_NUM_OBJECTS];
57 : int16_t nSCE_old, nCPE_old;
58 : int16_t numCldfbAnalyses_old, numCldfbSyntheses_old, ism_mode;
59 : int16_t tc_granularity_new;
60 : int16_t nchan_out_buff;
61 :
62 7563 : nCPE_old = st_ivas->nCPE;
63 7563 : nSCE_old = st_ivas->nSCE;
64 :
65 : /* temporarily set the ism mode back to the old one, otherwise this can give wrong results*/
66 7563 : ism_mode = st_ivas->ism_mode;
67 7563 : st_ivas->ism_mode = last_ism_mode;
68 7563 : ivas_init_dec_get_num_cldfb_instances( st_ivas, &numCldfbAnalyses_old, &numCldfbSyntheses_old );
69 7563 : st_ivas->ism_mode = ism_mode;
70 :
71 7563 : if ( ( error = ivas_ism_config( st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->nchan_transport, st_ivas->nchan_ism, NULL, 0, NULL, NULL, element_brate_tmp, NULL, NULL, 0 ) ) != IVAS_ERR_OK )
72 : {
73 0 : return error;
74 : }
75 :
76 7563 : st_ivas->nSCE = st_ivas->nchan_transport;
77 :
78 : /*-----------------------------------------------------------------*
79 : * Allocate, initialize, and configure SCE/CPE/MCT handles
80 : *-----------------------------------------------------------------*/
81 :
82 7563 : if ( ( error = ivas_corecoder_dec_reconfig( st_ivas, nSCE_old, nCPE_old, nchan_transport_old, 0, 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 )
83 : {
84 0 : return error;
85 : }
86 :
87 : /*-----------------------------------------------------------------*
88 : * HP20 memories
89 : *-----------------------------------------------------------------*/
90 :
91 7563 : if ( ( error = ivas_hp20_dec_reconfig( st_ivas, nchan_transport_old ) ) != IVAS_ERR_OK )
92 : {
93 0 : return error;
94 : }
95 :
96 : /*-----------------------------------------------------------------*
97 : * Initialize the needed renderer struct and destroy the unnecessary renderer struct
98 : *-----------------------------------------------------------------*/
99 :
100 : /* select the renderer */
101 7563 : ivas_renderer_select( st_ivas );
102 :
103 7563 : ivas_output_init( &( st_ivas->hIntSetup ), st_ivas->intern_config );
104 :
105 7563 : if ( ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC ) && ( st_ivas->ism_mode == ISM_MODE_DISC ) )
106 : {
107 732 : ivas_output_init( &( st_ivas->hIntSetup ), st_ivas->hDecoderConfig->output_config );
108 : }
109 :
110 : /* transfer subframe info from DirAC or ParamMC to central TC buffer */
111 : /* only do this if we are not having done everything already in the TC decoding part and having only played out from the TC buffer */
112 7563 : if ( last_ism_mode == ISM_MODE_PARAM && st_ivas->hSpatParamRendCom != NULL && st_ivas->hTcBuffer->tc_buffer_mode != TC_BUFFER_MODE_BUFFER )
113 : {
114 1852 : st_ivas->hTcBuffer->nb_subframes = st_ivas->hSpatParamRendCom->nb_subframes;
115 1852 : st_ivas->hTcBuffer->subframes_rendered = st_ivas->hSpatParamRendCom->subframes_rendered;
116 1852 : st_ivas->hTcBuffer->num_slots = st_ivas->hSpatParamRendCom->num_slots;
117 1852 : st_ivas->hTcBuffer->slots_rendered = st_ivas->hSpatParamRendCom->slots_rendered;
118 1852 : mvs2s( st_ivas->hSpatParamRendCom->subframe_nbslots, st_ivas->hTcBuffer->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
119 : }
120 :
121 : /* when granularity goes down (e.g. Discrete ISM with TD Obj Renderer -> ParamISM with binaural fastconv
122 : render what still fits in the new granularity */
123 7563 : tc_granularity_new = ivas_dec_get_render_granularity( st_ivas->renderer_type, RENDERER_DISABLE, st_ivas->hDecoderConfig->output_Fs );
124 :
125 7563 : if ( tc_granularity_new < st_ivas->hTcBuffer->n_samples_granularity )
126 : {
127 : /* flush already done in IVAS_DEC_ReadFormat() */
128 : }
129 : /* when granularity goes up set samples to discard at the beginning of the frame */
130 6651 : else if ( tc_granularity_new > st_ivas->hTcBuffer->n_samples_granularity )
131 : {
132 876 : if ( ( error = ivas_jbm_dec_set_discard_samples( st_ivas ) ) != IVAS_ERR_OK )
133 : {
134 0 : return error;
135 : }
136 : }
137 :
138 7563 : if ( st_ivas->ism_mode != last_ism_mode )
139 : {
140 : /* EFAP handle */
141 2609 : efap_free_data( &st_ivas->hEFAPdata );
142 : }
143 :
144 : /*-----------------------------------------------------------------*
145 : * Switching between ParamISM and DiscISM
146 : *-----------------------------------------------------------------*/
147 :
148 : /* switching from ParamISM to DiscISM */
149 7563 : if ( st_ivas->ism_mode == ISM_MODE_DISC && last_ism_mode == ISM_MODE_PARAM )
150 : {
151 : /* Deallocate the ParamISM struct */
152 1247 : ivas_param_ism_dec_close( &( st_ivas->hParamIsmDec ), &( st_ivas->hSpatParamRendCom ), st_ivas->hDecoderConfig->output_config );
153 :
154 1247 : if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL || st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB || st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
155 : {
156 : /* close the parametric binaural renderer */
157 351 : ivas_dirac_dec_close_binaural_data( st_ivas->hDiracDecBin );
158 :
159 : /* Close omasa data struct (used for object editing) */
160 351 : ivas_omasa_data_close( &st_ivas->hMasaIsmData );
161 :
162 : /* Open the TD Binaural renderer */
163 351 : if ( st_ivas->hHrtfTD == NULL || st_ivas->hBinRendererTd == NULL )
164 : {
165 351 : if ( ( error = ivas_td_binaural_open( st_ivas ) ) != IVAS_ERR_OK )
166 : {
167 0 : return error;
168 : }
169 351 : if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
170 : {
171 24 : if ( ( error = ivas_reverb_open( &st_ivas->hReverb, st_ivas->hHrtfStatistics, st_ivas->hRenderConfig, st_ivas->hDecoderConfig->output_Fs ) ) != IVAS_ERR_OK )
172 : {
173 0 : return error;
174 : }
175 : }
176 : }
177 : }
178 : else
179 : {
180 : /* close the ISM renderer and reinitialize */
181 896 : ivas_ism_renderer_close( &st_ivas->hIsmRendererData );
182 :
183 896 : if ( ( error = ivas_ism_renderer_open( st_ivas ) ) != IVAS_ERR_OK )
184 : {
185 0 : return error;
186 : }
187 : }
188 :
189 1247 : if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
190 : {
191 : /* close the parametric binaural renderer */
192 525 : ivas_dirac_dec_close_binaural_data( st_ivas->hDiracDecBin );
193 :
194 : /* Close omasa data struct (used for object editing) */
195 525 : ivas_omasa_data_close( &st_ivas->hMasaIsmData );
196 :
197 : /* Open Crend Binaural renderer */
198 525 : if ( ( error = ivas_rend_openCrend( &( st_ivas->hCrendWrapper ), st_ivas->intern_config, st_ivas->hOutSetup.output_config, st_ivas->hRenderConfig, st_ivas->hHrtfCrend, st_ivas->hHrtfStatistics, st_ivas->hDecoderConfig->output_Fs, 0, ( st_ivas->hSplitBinRend == NULL ) ? 1 : st_ivas->hSplitBinRend->splitrend.multiBinPoseData.num_poses ) ) != IVAS_ERR_OK )
199 : {
200 0 : return error;
201 : }
202 :
203 525 : st_ivas->binaural_latency_ns = st_ivas->hCrendWrapper->binaural_latency_ns;
204 : }
205 : }
206 :
207 : /* switching from Discrete ISM to ParamISM */
208 7563 : if ( st_ivas->ism_mode == ISM_MODE_PARAM && last_ism_mode == ISM_MODE_DISC )
209 : {
210 : /* Allocate and initialize the ParamISM struct */
211 1362 : if ( ( error = ivas_param_ism_dec_open( st_ivas ) ) != IVAS_ERR_OK )
212 : {
213 0 : return error;
214 : }
215 :
216 1362 : if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL || st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB || st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
217 : {
218 : /* open the parametric binaural renderer */
219 378 : if ( ( error = ivas_dirac_dec_binaural_copy_hrtfs( &st_ivas->hHrtfParambin ) ) != IVAS_ERR_OK )
220 : {
221 0 : return error;
222 : }
223 :
224 378 : if ( ( error = ivas_dirac_dec_init_binaural_data( st_ivas, &( st_ivas->hHrtfParambin ) ) ) != IVAS_ERR_OK )
225 : {
226 0 : return error;
227 : }
228 :
229 : /* Open omasa data struct (used for object editing) */
230 378 : if ( ( error = ivas_omasa_data_open( st_ivas ) ) != IVAS_ERR_OK )
231 : {
232 0 : return error;
233 : }
234 :
235 : /* Close the TD Binaural renderer */
236 378 : ivas_td_binaural_close( &st_ivas->hBinRendererTd );
237 :
238 378 : if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
239 : {
240 36 : ivas_reverb_close( &st_ivas->hReverb );
241 : }
242 : }
243 : else
244 : {
245 : /* Close the ISM renderer */
246 984 : ivas_ism_renderer_close( &st_ivas->hIsmRendererData );
247 : }
248 :
249 1362 : if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
250 : {
251 : /* open the parametric binaural renderer */
252 534 : if ( ( error = ivas_dirac_dec_binaural_copy_hrtfs( &st_ivas->hHrtfParambin ) ) != IVAS_ERR_OK )
253 : {
254 0 : return error;
255 : }
256 :
257 534 : if ( ( error = ivas_dirac_dec_init_binaural_data( st_ivas, &( st_ivas->hHrtfParambin ) ) ) != IVAS_ERR_OK )
258 : {
259 0 : return error;
260 : }
261 :
262 : /* Open omasa data struct (used for object editing) */
263 534 : if ( ( error = ivas_omasa_data_open( st_ivas ) ) != IVAS_ERR_OK )
264 : {
265 0 : return error;
266 : }
267 :
268 : /* close the crend binaural renderer */
269 534 : ivas_rend_closeCrend( &( st_ivas->hCrendWrapper ) );
270 : }
271 : }
272 :
273 : /*-----------------------------------------------------------------*
274 : * CLDFB instances
275 : *-----------------------------------------------------------------*/
276 :
277 7563 : if ( ( error = ivas_cldfb_dec_reconfig( st_ivas, nchan_transport_old, numCldfbAnalyses_old, numCldfbSyntheses_old ) ) != IVAS_ERR_OK )
278 : {
279 0 : return error;
280 : }
281 :
282 : /*-----------------------------------------------------------------*
283 : * TC buffer
284 : *-----------------------------------------------------------------*/
285 :
286 : int16_t tc_nchan_full_new;
287 : TC_BUFFER_MODE tc_buffer_mode_new;
288 : int16_t tc_nchan_rend_new;
289 : int16_t tc_nchan_allocate_new;
290 : DECODER_TC_BUFFER_HANDLE hTcBuffer;
291 :
292 7563 : hTcBuffer = st_ivas->hTcBuffer;
293 7563 : tc_buffer_mode_new = ivas_dec_get_tc_buffer_mode( st_ivas );
294 7563 : tc_nchan_rend_new = ivas_dec_get_num_tc_channels( st_ivas );
295 7563 : tc_nchan_allocate_new = tc_nchan_rend_new;
296 7563 : tc_nchan_full_new = tc_nchan_rend_new;
297 :
298 7563 : if ( st_ivas->ism_mode == ISM_MODE_PARAM && ( st_ivas->renderer_type != RENDERER_MONO_DOWNMIX && st_ivas->renderer_type != RENDERER_DISABLE && st_ivas->renderer_type != RENDERER_BINAURAL_PARAMETRIC && st_ivas->renderer_type != RENDERER_BINAURAL_PARAMETRIC_ROOM ) )
299 : {
300 597 : tc_nchan_full_new = 0;
301 : }
302 :
303 : /* reconfigure TC buffer */
304 7563 : if ( hTcBuffer->tc_buffer_mode != tc_buffer_mode_new || hTcBuffer->nchan_transport_rend != tc_nchan_rend_new ||
305 4994 : hTcBuffer->nchan_buffer_full != tc_nchan_full_new || hTcBuffer->nchan_transport_internal != tc_nchan_allocate_new )
306 : {
307 2569 : if ( ( error = ivas_dec_tc_buffer_reconfigure( st_ivas, tc_buffer_mode_new, tc_nchan_rend_new, tc_nchan_allocate_new, tc_nchan_full_new, tc_granularity_new ) ) != IVAS_ERR_OK )
308 : {
309 0 : return error;
310 : }
311 : }
312 :
313 : /* transfer subframe info from central TC buffer to ParamMC or McMASA (DirAC) */
314 7563 : if ( st_ivas->hSpatParamRendCom != NULL )
315 : {
316 1999 : st_ivas->hSpatParamRendCom->nb_subframes = hTcBuffer->nb_subframes;
317 1999 : st_ivas->hSpatParamRendCom->subframes_rendered = hTcBuffer->subframes_rendered;
318 1999 : st_ivas->hSpatParamRendCom->num_slots = hTcBuffer->num_slots;
319 1999 : st_ivas->hSpatParamRendCom->slots_rendered = hTcBuffer->slots_rendered;
320 :
321 1999 : mvs2s( hTcBuffer->subframe_nbslots, st_ivas->hSpatParamRendCom->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
322 : }
323 :
324 : /*-----------------------------------------------------------------*
325 : * floating-point output audio buffers
326 : *-----------------------------------------------------------------*/
327 :
328 7563 : nchan_out_buff = ivas_get_nchan_buffers_dec( st_ivas, -1, -1 );
329 7563 : if ( ( error = ivas_output_buff_dec( st_ivas->p_output_f, nchan_out_buff, st_ivas->hDecoderConfig->Opt_tsm, st_ivas->hTcBuffer ) ) != IVAS_ERR_OK )
330 : {
331 0 : return error;
332 : }
333 :
334 7563 : return IVAS_ERR_OK;
335 : }
336 :
337 :
338 : /*-------------------------------------------------------------------------
339 : * ivas_ism_dec_config()
340 : *
341 : * - select ISM format mode
342 : * - reconfigure the ISM format decoder
343 : *-------------------------------------------------------------------------*/
344 :
345 1074529 : ivas_error ivas_ism_dec_config(
346 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
347 : const ISM_MODE last_ism_mode /* i/o: last ISM mode */
348 : )
349 : {
350 : int32_t ivas_total_brate;
351 : ivas_error error;
352 : int16_t nchan_transport_old;
353 :
354 1074529 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
355 :
356 : /* Assumes that num of input objects are constant */
357 1074529 : nchan_transport_old = st_ivas->nchan_ism;
358 :
359 1074529 : if ( last_ism_mode == ISM_MODE_PARAM )
360 : {
361 139981 : nchan_transport_old = MAX_PARAM_ISM_WAVE;
362 : }
363 :
364 1074529 : if ( !st_ivas->bfi && ivas_total_brate != IVAS_SID_5k2 && ivas_total_brate != FRAME_NO_DATA )
365 : {
366 : /* select ISM format mode */
367 1072545 : st_ivas->ism_mode = ivas_ism_mode_select( st_ivas->nchan_ism, ivas_total_brate );
368 :
369 1072545 : st_ivas->nchan_transport = st_ivas->nchan_ism;
370 1072545 : if ( st_ivas->ism_mode == ISM_MODE_PARAM )
371 : {
372 140993 : st_ivas->nchan_transport = MAX_PARAM_ISM_WAVE;
373 140993 : if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
374 : {
375 5435 : st_ivas->hDecoderConfig->nchan_out = st_ivas->nchan_ism;
376 : }
377 : }
378 :
379 1072545 : if ( st_ivas->ini_active_frame != 0 )
380 : {
381 : /* ISM bit-rate switching */
382 1056336 : if ( ( st_ivas->ism_mode != last_ism_mode ) || ( ivas_total_brate != st_ivas->hDecoderConfig->last_ivas_total_brate ) )
383 : {
384 7545 : if ( ( error = ivas_ism_bitrate_switching_dec( st_ivas, nchan_transport_old, last_ism_mode ) ) != IVAS_ERR_OK )
385 : {
386 0 : return error;
387 : }
388 : }
389 : }
390 : }
391 1984 : else if ( !st_ivas->bfi && ivas_total_brate == IVAS_SID_5k2 )
392 : {
393 1984 : st_ivas->nchan_transport = st_ivas->nchan_ism;
394 1984 : if ( st_ivas->ism_mode == ISM_MODE_PARAM )
395 : {
396 661 : st_ivas->nchan_transport = MAX_PARAM_ISM_WAVE;
397 661 : if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
398 : {
399 21 : st_ivas->hDecoderConfig->nchan_out = st_ivas->nchan_ism;
400 : }
401 : }
402 :
403 : /* ISM mode switching */
404 1984 : if ( st_ivas->ism_mode != last_ism_mode )
405 : {
406 :
407 18 : if ( ( error = ivas_ism_bitrate_switching_dec( st_ivas, nchan_transport_old, last_ism_mode ) ) != IVAS_ERR_OK )
408 : {
409 0 : return error;
410 : }
411 : }
412 : }
413 :
414 1074529 : switch ( st_ivas->nchan_ism )
415 : {
416 203377 : case 1:
417 203377 : st_ivas->transport_config = IVAS_AUDIO_CONFIG_ISM1;
418 203377 : break;
419 233865 : case 2:
420 233865 : st_ivas->transport_config = IVAS_AUDIO_CONFIG_ISM2;
421 233865 : break;
422 260269 : case 3:
423 260269 : st_ivas->transport_config = IVAS_AUDIO_CONFIG_ISM3;
424 260269 : break;
425 377018 : case 4:
426 377018 : st_ivas->transport_config = IVAS_AUDIO_CONFIG_ISM4;
427 377018 : break;
428 0 : default:
429 0 : st_ivas->transport_config = IVAS_AUDIO_CONFIG_INVALID;
430 0 : break;
431 : }
432 :
433 1074529 : return IVAS_ERR_OK;
434 : }
|