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 3933 : 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 : TC_BUFFER_MODE tc_buffer_mode_new;
60 : int16_t tc_nchan_tc_new;
61 : int16_t tc_nchan_allocate_new;
62 : int16_t tc_granularity_new;
63 : int16_t nchan_out_buff, nchan_out_buff_old;
64 :
65 3933 : nCPE_old = st_ivas->nCPE;
66 3933 : nSCE_old = st_ivas->nSCE;
67 :
68 : /* temporarily set the ism mode back to the old one, otherwise this can give wrong results*/
69 3933 : ism_mode = st_ivas->ism_mode;
70 3933 : st_ivas->ism_mode = last_ism_mode;
71 3933 : ivas_init_dec_get_num_cldfb_instances( st_ivas, &numCldfbAnalyses_old, &numCldfbSyntheses_old );
72 3933 : st_ivas->ism_mode = ism_mode;
73 3933 : nchan_out_buff_old = ivas_get_nchan_buffers_dec( st_ivas, -1, -1 );
74 :
75 3933 : 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 )
76 : {
77 0 : return error;
78 : }
79 :
80 3933 : st_ivas->nSCE = st_ivas->nchan_transport;
81 :
82 : /*-----------------------------------------------------------------*
83 : * Allocate, initialize, and configure SCE/CPE/MCT handles
84 : *-----------------------------------------------------------------*/
85 :
86 3933 : 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 )
87 : {
88 0 : return error;
89 : }
90 :
91 : /*-----------------------------------------------------------------*
92 : * HP20 memories
93 : *-----------------------------------------------------------------*/
94 :
95 3933 : if ( ( error = ivas_hp20_dec_reconfig( st_ivas, nchan_transport_old ) ) != IVAS_ERR_OK )
96 : {
97 0 : return error;
98 : }
99 :
100 : /*-----------------------------------------------------------------*
101 : * Initialize the needed renderer struct and destroy the unnecessary renderer struct
102 : *-----------------------------------------------------------------*/
103 :
104 : /* select the renderer */
105 3933 : ivas_renderer_select( st_ivas );
106 :
107 3933 : ivas_output_init( &( st_ivas->hIntSetup ), st_ivas->intern_config );
108 :
109 3933 : if ( ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC ) && ( st_ivas->ism_mode == ISM_MODE_DISC ) )
110 : {
111 234 : ivas_output_init( &( st_ivas->hIntSetup ), st_ivas->hDecoderConfig->output_config );
112 : }
113 :
114 : /* transfer subframe info from DirAC or ParamMC to central tc buffer */
115 : /* 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 */
116 3933 : if ( last_ism_mode == ISM_MODE_PARAM && st_ivas->hSpatParamRendCom != NULL && st_ivas->hTcBuffer->tc_buffer_mode != TC_BUFFER_MODE_BUFFER )
117 : {
118 1209 : st_ivas->hTcBuffer->nb_subframes = st_ivas->hSpatParamRendCom->nb_subframes;
119 1209 : st_ivas->hTcBuffer->subframes_rendered = st_ivas->hSpatParamRendCom->subframes_rendered;
120 1209 : st_ivas->hTcBuffer->num_slots = st_ivas->hSpatParamRendCom->num_slots;
121 1209 : st_ivas->hTcBuffer->slots_rendered = st_ivas->hSpatParamRendCom->slots_rendered;
122 1209 : mvs2s( st_ivas->hSpatParamRendCom->subframe_nbslots, st_ivas->hTcBuffer->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
123 : }
124 :
125 3933 : if ( st_ivas->ism_mode != last_ism_mode )
126 : {
127 : /* EFAP handle */
128 1827 : efap_free_data( &st_ivas->hEFAPdata );
129 : }
130 :
131 : /*-----------------------------------------------------------------*
132 : * Switching between ParamISM and DiscISM
133 : *-----------------------------------------------------------------*/
134 :
135 : /* switching from ParamISM to DiscISM */
136 3933 : if ( st_ivas->ism_mode == ISM_MODE_DISC && last_ism_mode == ISM_MODE_PARAM )
137 : {
138 : /* Deallocate the ParamISM struct */
139 918 : ivas_param_ism_dec_close( &( st_ivas->hParamIsmDec ), &( st_ivas->hSpatParamRendCom ), st_ivas->hDecoderConfig->output_config );
140 :
141 918 : 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 )
142 : {
143 : /* close the parametric binaural renderer */
144 222 : ivas_dirac_dec_close_binaural_data( st_ivas->hDiracDecBin );
145 :
146 : /* Close omasa data struct (used for object editing) */
147 222 : ivas_omasa_data_close( &st_ivas->hMasaIsmData );
148 :
149 : /* Open the TD Binaural renderer */
150 222 : if ( st_ivas->hHrtfTD == NULL || st_ivas->hBinRendererTd == NULL )
151 : {
152 222 : if ( ( error = ivas_td_binaural_open( st_ivas ) ) != IVAS_ERR_OK )
153 : {
154 0 : return error;
155 : }
156 222 : if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
157 : {
158 0 : if ( ( error = ivas_reverb_open( &st_ivas->hReverb, st_ivas->hHrtfStatistics, st_ivas->hRenderConfig, st_ivas->hDecoderConfig->output_Fs ) ) != IVAS_ERR_OK )
159 : {
160 0 : return error;
161 : }
162 : }
163 : }
164 : }
165 : else
166 : {
167 : /* close the ISM renderer and reinitialize */
168 696 : ivas_ism_renderer_close( &st_ivas->hIsmRendererData );
169 :
170 696 : if ( ( error = ivas_ism_renderer_open( st_ivas ) ) != IVAS_ERR_OK )
171 : {
172 0 : return error;
173 : }
174 : }
175 :
176 918 : if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
177 : {
178 : /* close the parametric binaural renderer */
179 501 : ivas_dirac_dec_close_binaural_data( st_ivas->hDiracDecBin );
180 :
181 : /* Close omasa data struct (used for object editing) */
182 501 : ivas_omasa_data_close( &st_ivas->hMasaIsmData );
183 :
184 : /* Open Crend Binaural renderer */
185 501 : 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 )
186 : {
187 0 : return error;
188 : }
189 :
190 501 : st_ivas->binaural_latency_ns = st_ivas->hCrendWrapper->binaural_latency_ns;
191 : }
192 : }
193 :
194 : /* switching from Discrete ISM to ParamISM */
195 3933 : if ( st_ivas->ism_mode == ISM_MODE_PARAM && last_ism_mode == ISM_MODE_DISC )
196 : {
197 : /* Allocate and initialize the ParamISM struct */
198 909 : if ( ( error = ivas_param_ism_dec_open( st_ivas ) ) != IVAS_ERR_OK )
199 : {
200 0 : return error;
201 : }
202 :
203 909 : 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 )
204 : {
205 : /* open the parametric binaural renderer */
206 225 : if ( ( error = ivas_dirac_dec_binaural_copy_hrtfs( &st_ivas->hHrtfParambin ) ) != IVAS_ERR_OK )
207 : {
208 0 : return error;
209 : }
210 :
211 225 : if ( ( error = ivas_dirac_dec_init_binaural_data( st_ivas, &( st_ivas->hHrtfParambin ) ) ) != IVAS_ERR_OK )
212 : {
213 0 : return error;
214 : }
215 :
216 : /* Open omasa data struct (used for object editing) */
217 225 : if ( ( error = ivas_omasa_data_open( st_ivas ) ) != IVAS_ERR_OK )
218 : {
219 0 : return error;
220 : }
221 :
222 : /* Close the TD Binaural renderer */
223 225 : ivas_td_binaural_close( &st_ivas->hBinRendererTd );
224 :
225 225 : if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
226 : {
227 0 : ivas_reverb_close( &st_ivas->hReverb );
228 : }
229 : }
230 : else
231 : {
232 : /* Close the ISM renderer */
233 684 : ivas_ism_renderer_close( &st_ivas->hIsmRendererData );
234 : }
235 :
236 909 : if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
237 : {
238 : /* open the parametric binaural renderer */
239 498 : if ( ( error = ivas_dirac_dec_binaural_copy_hrtfs( &st_ivas->hHrtfParambin ) ) != IVAS_ERR_OK )
240 : {
241 0 : return error;
242 : }
243 :
244 498 : if ( ( error = ivas_dirac_dec_init_binaural_data( st_ivas, &( st_ivas->hHrtfParambin ) ) ) != IVAS_ERR_OK )
245 : {
246 0 : return error;
247 : }
248 :
249 : /* Open omasa data struct (used for object editing) */
250 498 : if ( ( error = ivas_omasa_data_open( st_ivas ) ) != IVAS_ERR_OK )
251 : {
252 0 : return error;
253 : }
254 :
255 : /* close the crend binaural renderer */
256 498 : ivas_rend_closeCrend( &( st_ivas->hCrendWrapper ), ( st_ivas->hSplitBinRend == NULL ) ? 1 : st_ivas->hSplitBinRend->splitrend.multiBinPoseData.num_poses );
257 : }
258 : }
259 :
260 : /*-----------------------------------------------------------------*
261 : * CLDFB instances
262 : *-----------------------------------------------------------------*/
263 :
264 3933 : if ( ( error = ivas_cldfb_dec_reconfig( st_ivas, nchan_transport_old, numCldfbAnalyses_old, numCldfbSyntheses_old ) ) != IVAS_ERR_OK )
265 : {
266 0 : return error;
267 : }
268 :
269 : /*-----------------------------------------------------------------*
270 : * floating-point output audio buffers
271 : *-----------------------------------------------------------------*/
272 :
273 3933 : nchan_out_buff = ivas_get_nchan_buffers_dec( st_ivas, -1, -1 );
274 :
275 3933 : if ( ( error = ivas_output_buff_dec( st_ivas->p_output_f, nchan_out_buff_old, nchan_out_buff ) ) != IVAS_ERR_OK )
276 : {
277 0 : return error;
278 : }
279 :
280 : /*-----------------------------------------------------------------*
281 : * JBM TC buffers
282 : *-----------------------------------------------------------------*/
283 :
284 : int16_t tc_nchan_full_new;
285 : DECODER_TC_BUFFER_HANDLE hTcBuffer;
286 :
287 3933 : hTcBuffer = st_ivas->hTcBuffer;
288 3933 : tc_buffer_mode_new = ivas_jbm_dec_get_tc_buffer_mode( st_ivas );
289 3933 : tc_nchan_tc_new = ivas_jbm_dec_get_num_tc_channels( st_ivas );
290 3933 : tc_nchan_allocate_new = tc_nchan_tc_new;
291 3933 : tc_nchan_full_new = tc_nchan_tc_new;
292 3933 : tc_granularity_new = ivas_jbm_dec_get_render_granularity( st_ivas->renderer_type, RENDERER_DISABLE, st_ivas->hDecoderConfig->output_Fs );
293 :
294 3933 : 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 ) )
295 : {
296 213 : tc_nchan_full_new = 0;
297 : }
298 :
299 : /* reconfigure buffer */
300 3933 : if ( hTcBuffer->tc_buffer_mode != tc_buffer_mode_new || hTcBuffer->nchan_transport_jbm != tc_nchan_tc_new ||
301 2106 : hTcBuffer->nchan_buffer_full != tc_nchan_full_new || hTcBuffer->nchan_transport_internal != tc_nchan_allocate_new )
302 : {
303 1827 : if ( ( error = ivas_jbm_dec_tc_buffer_reconfigure( st_ivas, tc_buffer_mode_new, tc_nchan_tc_new, tc_nchan_allocate_new, tc_nchan_full_new, tc_granularity_new ) ) != IVAS_ERR_OK )
304 : {
305 0 : return error;
306 : }
307 : }
308 :
309 : /* transfer subframe info from central tc buffer to ParamMC or McMASA (DirAC) */
310 3933 : if ( st_ivas->hSpatParamRendCom != NULL )
311 : {
312 1200 : st_ivas->hSpatParamRendCom->nb_subframes = st_ivas->hTcBuffer->nb_subframes;
313 1200 : st_ivas->hSpatParamRendCom->subframes_rendered = st_ivas->hTcBuffer->subframes_rendered;
314 1200 : st_ivas->hSpatParamRendCom->num_slots = st_ivas->hTcBuffer->num_slots;
315 1200 : st_ivas->hSpatParamRendCom->slots_rendered = st_ivas->hTcBuffer->slots_rendered;
316 :
317 1200 : mvs2s( st_ivas->hTcBuffer->subframe_nbslots, st_ivas->hSpatParamRendCom->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
318 : }
319 :
320 3933 : return IVAS_ERR_OK;
321 : }
322 :
323 :
324 : /*-------------------------------------------------------------------------
325 : * ivas_ism_dec_config()
326 : *
327 : * - select ISM format mode
328 : * - reconfigure the ISM format decoder
329 : *-------------------------------------------------------------------------*/
330 :
331 302229 : ivas_error ivas_ism_dec_config(
332 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
333 : const ISM_MODE last_ism_mode /* i/o: last ISM mode */
334 : )
335 : {
336 : int32_t ivas_total_brate;
337 : ivas_error error;
338 : int16_t nchan_transport_old;
339 :
340 302229 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
341 :
342 : /* Assumes that num of input objects are constant */
343 302229 : nchan_transport_old = st_ivas->nchan_ism;
344 :
345 302229 : if ( last_ism_mode == ISM_MODE_PARAM )
346 : {
347 60108 : nchan_transport_old = MAX_PARAM_ISM_WAVE;
348 : }
349 :
350 302229 : if ( !st_ivas->bfi && ivas_total_brate != IVAS_SID_5k2 && ivas_total_brate != FRAME_NO_DATA )
351 : {
352 : /* select ISM format mode */
353 300603 : st_ivas->ism_mode = ivas_ism_mode_select( st_ivas->nchan_ism, ivas_total_brate );
354 :
355 300603 : st_ivas->nchan_transport = st_ivas->nchan_ism;
356 300603 : if ( st_ivas->ism_mode == ISM_MODE_PARAM )
357 : {
358 59829 : st_ivas->nchan_transport = MAX_PARAM_ISM_WAVE;
359 59829 : if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
360 : {
361 1539 : st_ivas->hDecoderConfig->nchan_out = st_ivas->nchan_ism;
362 : }
363 : }
364 :
365 300603 : if ( st_ivas->ini_active_frame != 0 )
366 : {
367 : /* ISM bit-rate switching */
368 300381 : if ( ( st_ivas->ism_mode != last_ism_mode ) || ( ivas_total_brate != st_ivas->hDecoderConfig->last_ivas_total_brate ) )
369 : {
370 3915 : if ( ( error = ivas_ism_bitrate_switching_dec( st_ivas, nchan_transport_old, last_ism_mode ) ) != IVAS_ERR_OK )
371 : {
372 0 : return error;
373 : }
374 : }
375 : }
376 : }
377 1626 : else if ( !st_ivas->bfi && ivas_total_brate == IVAS_SID_5k2 )
378 : {
379 1626 : st_ivas->nchan_transport = st_ivas->nchan_ism;
380 1626 : if ( st_ivas->ism_mode == ISM_MODE_PARAM )
381 : {
382 321 : st_ivas->nchan_transport = MAX_PARAM_ISM_WAVE;
383 321 : if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
384 : {
385 21 : st_ivas->hDecoderConfig->nchan_out = st_ivas->nchan_ism;
386 : }
387 : }
388 :
389 : /* ISM mode switching */
390 1626 : if ( st_ivas->ism_mode != last_ism_mode )
391 : {
392 :
393 18 : if ( ( error = ivas_ism_bitrate_switching_dec( st_ivas, nchan_transport_old, last_ism_mode ) ) != IVAS_ERR_OK )
394 : {
395 0 : return error;
396 : }
397 : }
398 : }
399 :
400 302229 : switch ( st_ivas->nchan_ism )
401 : {
402 56403 : case 1:
403 56403 : st_ivas->transport_config = IVAS_AUDIO_CONFIG_ISM1;
404 56403 : break;
405 48195 : case 2:
406 48195 : st_ivas->transport_config = IVAS_AUDIO_CONFIG_ISM2;
407 48195 : break;
408 57051 : case 3:
409 57051 : st_ivas->transport_config = IVAS_AUDIO_CONFIG_ISM3;
410 57051 : break;
411 140580 : case 4:
412 140580 : st_ivas->transport_config = IVAS_AUDIO_CONFIG_ISM4;
413 140580 : break;
414 0 : default:
415 0 : st_ivas->transport_config = IVAS_AUDIO_CONFIG_INVALID;
416 0 : break;
417 : }
418 :
419 302229 : return IVAS_ERR_OK;
420 : }
|