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 "cnst.h"
36 : #include "ivas_cnst.h"
37 : #include "rom_com.h"
38 : #include "prot.h"
39 : #include "ivas_prot.h"
40 : #include "ivas_prot_rend.h"
41 : #include "ivas_rom_com.h"
42 : #include <math.h>
43 : #ifdef DEBUGGING
44 : #include "debug.h"
45 : #endif
46 : #include "wmc_auto.h"
47 :
48 :
49 : /*-----------------------------------------------------------------------*
50 : * Local function prototypes
51 : *-----------------------------------------------------------------------*/
52 :
53 : static void ivas_jbm_dec_tc_buffer_playout( Decoder_Struct *st_ivas, const uint16_t nSamplesAsked, uint16_t *nSamplesRendered, float *output[] );
54 :
55 : static void ivas_jbm_dec_copy_masa_meta_to_buffer( Decoder_Struct *st_ivas );
56 :
57 : static void ivas_jbm_masa_sf_to_slot_map( Decoder_Struct *st_ivas, const int16_t nCldfbTs );
58 :
59 :
60 : /*--------------------------------------------------------------------------*
61 : * ivas_jbm_dec_tc()
62 : *
63 : * Principal IVAS JBM decoder routine, decoding of metadata and transport channels
64 : *--------------------------------------------------------------------------*/
65 :
66 1265478 : ivas_error ivas_jbm_dec_tc(
67 : Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
68 : )
69 : {
70 : int16_t n, output_frame, nchan_out;
71 : Decoder_State *st; /* used for bitstream handling */
72 : float *p_output[MAX_TRANSPORT_CHANNELS]; /* 'float' buffer for output synthesis */
73 : int16_t nchan_remapped;
74 : int16_t nb_bits_metadata[MAX_SCE + 1];
75 : int32_t output_Fs, ivas_total_brate;
76 : AUDIO_CONFIG output_config;
77 : ivas_error error;
78 : int16_t num_md_sub_frames;
79 : int32_t ism_total_brate;
80 :
81 1265478 : push_wmops( "ivas_jbm_dec_tc" );
82 :
83 : /*----------------------------------------------------------------*
84 : * Initialization of local vars after struct has been set
85 : *----------------------------------------------------------------*/
86 :
87 1265478 : output_Fs = st_ivas->hDecoderConfig->output_Fs;
88 1265478 : nchan_out = st_ivas->hTcBuffer->nchan_transport_jbm;
89 1265478 : output_config = st_ivas->hDecoderConfig->output_config;
90 1265478 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
91 :
92 1265478 : output_frame = (int16_t) ( output_Fs / FRAMES_PER_SEC );
93 :
94 : /* set pointers to transport channels audio */
95 16451214 : for ( n = 0; n < MAX_TRANSPORT_CHANNELS; n++ )
96 : {
97 15185736 : p_output[n] = st_ivas->p_output_f[n];
98 15185736 : if ( p_output[n] != NULL )
99 : {
100 6435099 : set_zero( p_output[n], L_FRAME48k );
101 : }
102 : }
103 :
104 : /*----------------------------------------------------------------*
105 : * Decoding + pre-rendering
106 : *----------------------------------------------------------------*/
107 :
108 1265478 : if ( st_ivas->bfi && st_ivas->ini_frame == 0 )
109 : {
110 : /* zero output when first frame(s) is lost */
111 0 : for ( n = 0; n < nchan_out; n++ )
112 : {
113 0 : set_f( p_output[n], 0.0f, output_frame );
114 : }
115 :
116 : #ifdef DEBUG_MODE_INFO
117 : create_sce_dec( st_ivas, 0, ivas_total_brate );
118 : output_debug_mode_info_dec( st_ivas->hSCE[0]->hCoreCoder, 1, output_frame, NULL );
119 : destroy_sce_dec( st_ivas->hSCE[0] );
120 : st_ivas->hSCE[0] = NULL;
121 : #endif
122 : }
123 1265478 : else if ( st_ivas->ivas_format == STEREO_FORMAT )
124 : {
125 217356 : st_ivas->hCPE[0]->element_brate = ivas_total_brate;
126 217356 : if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, 0 ) ) != IVAS_ERR_OK )
127 : {
128 0 : return error;
129 : }
130 :
131 : /* HP filtering */
132 580413 : for ( n = 0; n < min( nchan_out, st_ivas->nchan_transport ); n++ )
133 : {
134 363057 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
135 : }
136 : }
137 1048122 : else if ( st_ivas->ivas_format == ISM_FORMAT )
138 : {
139 : /* Metadata decoding and configuration */
140 311511 : if ( ivas_total_brate == IVAS_SID_5k2 || ivas_total_brate == FRAME_NO_DATA )
141 : {
142 6399 : ivas_ism_dtx_dec( st_ivas, nb_bits_metadata );
143 :
144 : /* decode dominant object first so the noise energy of the other objects can be limited */
145 6399 : if ( ( error = ivas_sce_dec( st_ivas, st_ivas->hISMDTX.sce_id_dtx, &p_output[st_ivas->hISMDTX.sce_id_dtx], output_frame, nb_bits_metadata[st_ivas->hISMDTX.sce_id_dtx] ) ) != IVAS_ERR_OK )
146 : {
147 0 : return error;
148 : }
149 :
150 6399 : ivas_ism_dtx_limit_noise_energy_for_near_silence( st_ivas->hSCE, st_ivas->hISMDTX.sce_id_dtx, st_ivas->nchan_transport );
151 :
152 6399 : ivas_param_ism_dec_dequant_md( st_ivas );
153 : }
154 305112 : else if ( st_ivas->ism_mode == ISM_MODE_PARAM )
155 : {
156 61164 : if ( ( error = ivas_ism_metadata_dec( ivas_total_brate, st_ivas->nchan_ism, &( st_ivas->nchan_transport ), st_ivas->hIsmMetaData, st_ivas->hSCE, st_ivas->bfi, nb_bits_metadata, st_ivas->ism_mode, st_ivas->hISMDTX, st_ivas->hParamIsmDec->hParamIsm, &st_ivas->ism_extmeta_active, &st_ivas->ism_extmeta_cnt, st_ivas->hSCE[0]->hCoreCoder[0] ) ) != IVAS_ERR_OK )
157 : {
158 0 : return error;
159 : }
160 :
161 61164 : ivas_param_ism_dec_dequant_md( st_ivas );
162 : }
163 : else /* ISM_MODE_DISC */
164 : {
165 243948 : if ( ( error = ivas_ism_metadata_dec( ivas_total_brate, st_ivas->nchan_ism, &( st_ivas->nchan_transport ), st_ivas->hIsmMetaData, st_ivas->hSCE, st_ivas->bfi, nb_bits_metadata, st_ivas->ism_mode, st_ivas->hISMDTX, NULL, &st_ivas->ism_extmeta_active, &st_ivas->ism_extmeta_cnt, st_ivas->hSCE[0]->hCoreCoder[0] ) ) != IVAS_ERR_OK )
166 : {
167 0 : return error;
168 : }
169 : }
170 :
171 1118538 : for ( n = 0; n < st_ivas->nchan_transport; n++ )
172 : {
173 : /* for DTX frames, dominant object has already been decoded before */
174 807027 : if ( !( ( ivas_total_brate == IVAS_SID_5k2 || ivas_total_brate == FRAME_NO_DATA ) && n == st_ivas->hISMDTX.sce_id_dtx ) )
175 : {
176 800628 : if ( ( error = ivas_sce_dec( st_ivas, n, &p_output[n], output_frame, nb_bits_metadata[n] ) ) != IVAS_ERR_OK )
177 : {
178 0 : return error;
179 : }
180 : }
181 :
182 : /* HP filtering */
183 807027 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
184 : }
185 :
186 311511 : if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
187 : {
188 11907 : ivas_ism_mono_dmx( st_ivas, p_output, output_frame );
189 : }
190 299604 : else if ( st_ivas->ism_mode == ISM_MODE_PARAM && ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC ) )
191 : {
192 : /* loudness correction */
193 32865 : ivas_dirac_dec_binaural_sba_gain( p_output, st_ivas->nchan_transport, output_frame );
194 : }
195 : }
196 736611 : else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
197 : {
198 457776 : set_s( nb_bits_metadata, 0, MAX_SCE );
199 :
200 :
201 : /* read parameters from the bitstream */
202 457776 : if ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->hQMetaData != NULL )
203 : {
204 97758 : st = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[0]->hCoreCoder[0] : st_ivas->hCPE[0]->hCoreCoder[0];
205 :
206 97758 : if ( ( error = ivas_masa_decode( st_ivas, st, &nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
207 : {
208 0 : return error;
209 : }
210 : }
211 360018 : else if ( st_ivas->ivas_format == SBA_FORMAT )
212 : {
213 360018 : if ( ( error = ivas_spar_dec( st_ivas, nb_bits_metadata ) ) != IVAS_ERR_OK )
214 : {
215 0 : return error;
216 : }
217 : }
218 :
219 457776 : if ( st_ivas->nchan_transport == CPE_CHANNELS && st_ivas->nCPE >= 1 )
220 : {
221 158913 : st_ivas->hCPE[0]->brate_surplus = 0;
222 158913 : st_ivas->hCPE[0]->element_brate = ivas_total_brate;
223 : }
224 :
225 : /* core-decoding of transport channels */
226 457776 : if ( st_ivas->nSCE == 1 )
227 : {
228 148161 : if ( ( error = ivas_sce_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
229 : {
230 0 : return error;
231 : }
232 : }
233 309615 : else if ( st_ivas->nCPE == 1 )
234 : {
235 158913 : if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
236 : {
237 0 : return error;
238 : }
239 : }
240 150702 : else if ( st_ivas->nCPE > 1 )
241 : {
242 150702 : if ( ( error = ivas_mct_dec( st_ivas, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
243 : {
244 0 : return error;
245 : }
246 : }
247 :
248 : #ifdef DEBUG_SBA_AUDIO_DUMP
249 : /* Dump audio signal after core-decoding */
250 : ivas_spar_dump_signal_wav( output_frame, NULL, output, st_ivas->nchan_transport, spar_foa_dec_wav[0], "core-decoding" );
251 : #endif
252 : /* TCs remapping */
253 457776 : nchan_remapped = st_ivas->nchan_transport;
254 457776 : if ( st_ivas->sba_dirac_stereo_flag )
255 : {
256 19830 : nchan_remapped = nchan_out;
257 :
258 19830 : if ( st_ivas->ivas_format == SBA_FORMAT )
259 : {
260 19830 : ivas_agc_dec_process( st_ivas->hSpar->hAgcDec, p_output, p_output, st_ivas->hSpar->hMdDec->spar_md_cfg.nchan_transport, output_frame );
261 :
262 19830 : if ( st_ivas->hSpar->hPCA != NULL )
263 : {
264 0 : ivas_pca_dec( st_ivas->hSpar->hPCA, output_frame, st_ivas->hSpar->hMdDec->spar_md_cfg.nchan_transport, ivas_total_brate, st_ivas->hDecoderConfig->last_ivas_total_brate, st_ivas->bfi, p_output );
265 : }
266 :
267 19830 : ivas_spar_dec_gen_umx_mat( st_ivas->hSpar->hMdDec, st_ivas->nchan_transport, IVAS_MAX_NUM_BANDS, st_ivas->bfi, ivas_get_spar_dec_md_num_subframes( st_ivas->sba_order, ivas_total_brate, st_ivas->last_active_ivas_total_brate ) );
268 : }
269 :
270 19830 : ivas_sba_dirac_stereo_dec( st_ivas, p_output, output_frame, st_ivas->ivas_format == MC_FORMAT );
271 : }
272 437946 : else if ( st_ivas->ivas_format == MASA_FORMAT && ivas_total_brate < MASA_STEREO_MIN_BITRATE && ( ivas_total_brate > IVAS_SID_5k2 || ( ivas_total_brate <= IVAS_SID_5k2 && st_ivas->nCPE > 0 && st_ivas->hCPE[0]->nchan_out == 1 ) ) )
273 : {
274 24045 : nchan_remapped = 1; /* Only one channel transported */
275 : }
276 :
277 : /* HP filtering */
278 : #ifndef DEBUG_SPAR_BYPASS_EVS_CODEC
279 1432251 : for ( n = 0; n < nchan_remapped; n++ )
280 : {
281 974475 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
282 : }
283 : #endif
284 :
285 457776 : if ( st_ivas->ivas_format == SBA_FORMAT )
286 : {
287 360018 : nchan_remapped = ivas_sba_remapTCs( p_output, st_ivas, output_frame );
288 :
289 360018 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
290 : {
291 89490 : num_md_sub_frames = ivas_get_spar_dec_md_num_subframes( st_ivas->sba_analysis_order, ivas_total_brate, st_ivas->last_active_ivas_total_brate );
292 89490 : ivas_sba_mix_matrix_determiner( st_ivas->hSpar, p_output, st_ivas->bfi, nchan_remapped, output_frame, num_md_sub_frames );
293 : }
294 270528 : else if ( st_ivas->renderer_type != RENDERER_DISABLE )
295 : {
296 245781 : ivas_spar_dec_agc_pca( st_ivas, p_output, output_frame );
297 : }
298 : }
299 :
300 457776 : if ( st_ivas->ivas_format == MASA_FORMAT )
301 : {
302 97758 : ivas_masa_prerender( st_ivas, p_output, output_frame, nchan_remapped );
303 :
304 : /* external output */
305 97758 : if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL && st_ivas->hMasa->config.input_ivas_format == MASA_ISM_FORMAT )
306 : {
307 570 : for ( n = 0; n < st_ivas->nchan_ism; n++ )
308 : {
309 456 : set_zero( p_output[st_ivas->nchan_transport + n], output_frame );
310 : }
311 :
312 114 : ivas_omasa_rearrange_channels( p_output, st_ivas->nchan_ism, output_frame );
313 : }
314 : }
315 360018 : else if ( st_ivas->ivas_format == SBA_FORMAT && ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM ) )
316 : {
317 : /* loudness correction */
318 89490 : ivas_dirac_dec_binaural_sba_gain( p_output, nchan_remapped, output_frame );
319 : }
320 : }
321 278835 : else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
322 : {
323 : int16_t nchan_ism, nchan_transport_ism;
324 : int16_t dirac_bs_md_write_idx;
325 :
326 21252 : set_s( nb_bits_metadata, 0, MAX_SCE + 1 );
327 :
328 : /* Set the number of objects for the parametric rendering */
329 21252 : dirac_bs_md_write_idx = 0;
330 21252 : if ( st_ivas->hSpatParamRendCom != NULL )
331 : {
332 16302 : st_ivas->hSpatParamRendCom->numIsmDirections = 0;
333 16302 : if ( st_ivas->ism_mode != ISM_MASA_MODE_DISC && st_ivas->ism_mode != ISM_MASA_MODE_MASA_ONE_OBJ )
334 : {
335 4476 : st_ivas->hSpatParamRendCom->numIsmDirections = st_ivas->nchan_ism;
336 : }
337 :
338 16302 : dirac_bs_md_write_idx = st_ivas->hSpatParamRendCom->dirac_bs_md_write_idx; /* Store the write-index for this frame */
339 : }
340 :
341 : /* MASA metadata decoding */
342 21252 : if ( ( error = ivas_masa_decode( st_ivas, st_ivas->hCPE[0]->hCoreCoder[0], &nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
343 : {
344 0 : return error;
345 : }
346 :
347 : /* Configuration of combined-format bit-budget distribution */
348 21252 : ivas_set_surplus_brate_dec( st_ivas, &ism_total_brate );
349 :
350 21252 : st_ivas->hCPE[0]->hCoreCoder[0]->bit_stream = &( st_ivas->bit_stream[( ism_total_brate / FRAMES_PER_SEC )] );
351 :
352 : /* set ISM parameters and decode ISM metadata in OMASA format */
353 21252 : if ( ( error = ivas_omasa_ism_metadata_dec( st_ivas, ism_total_brate, &nchan_ism, &nchan_transport_ism, dirac_bs_md_write_idx, &nb_bits_metadata[1] ) ) != IVAS_ERR_OK )
354 : {
355 0 : return error;
356 : }
357 :
358 : /* decode ISM channels */
359 58464 : for ( n = 0; n < nchan_transport_ism; n++ )
360 : {
361 37212 : if ( ( error = ivas_sce_dec( st_ivas, n, &p_output[st_ivas->nchan_transport + n], output_frame, nb_bits_metadata[1] ) ) != IVAS_ERR_OK )
362 : {
363 0 : return error;
364 : }
365 : }
366 :
367 : /* decode MASA channels */
368 21252 : if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
369 : {
370 0 : return error;
371 : }
372 :
373 21252 : if ( st_ivas->hCPE[0]->nchan_out == 1 )
374 : {
375 4158 : mvr2r( p_output[0], p_output[1], output_frame ); /* Copy mono signal to stereo output channels */
376 : }
377 :
378 : /* HP filtering */
379 100968 : for ( n = 0; n < getNumChanSynthesis( st_ivas ); n++ )
380 : {
381 79716 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
382 : }
383 :
384 21252 : if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
385 : {
386 2925 : ivas_ism_mono_dmx( st_ivas, p_output, output_frame );
387 : }
388 18327 : else if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
389 : {
390 2145 : if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
391 : {
392 1917 : ivas_omasa_rearrange_channels( p_output, nchan_transport_ism, output_frame );
393 : }
394 228 : else if ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ )
395 : {
396 : /* Convert separate object to MASA, combine with the original MASA, and output combined MASA + empty objects. */
397 108 : ivas_omasa_combine_separate_ism_with_masa( st_ivas, p_output, st_ivas->nchan_ism, output_frame );
398 : }
399 120 : else if ( st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
400 : {
401 : /* Extract objects from MASA, output MASA + all objects (i.e., extracted and separated objects) */
402 120 : ivas_omasa_render_objects_from_mix( st_ivas, p_output, st_ivas->nchan_ism, output_frame );
403 : }
404 : }
405 : }
406 257583 : else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
407 : {
408 : int16_t nchan_ism, sba_ch_idx;
409 :
410 111021 : set_s( nb_bits_metadata, 0, MAX_SCE + 1 );
411 111021 : nchan_ism = st_ivas->nchan_ism;
412 111021 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
413 : {
414 : /* set ISM parameters and decode ISM metadata in OSBA format */
415 66246 : if ( ( error = ivas_osba_ism_metadata_dec( st_ivas, ivas_total_brate, &nchan_ism, &nb_bits_metadata[1] ) ) != IVAS_ERR_OK )
416 : {
417 0 : return error;
418 : }
419 66246 : sba_ch_idx = st_ivas->nchan_ism;
420 : }
421 : else
422 : {
423 44775 : nb_bits_metadata[1] += NO_BITS_MASA_ISM_NO_OBJ;
424 44775 : sba_ch_idx = 0;
425 : }
426 :
427 : /* SBA metadata decoding */
428 111021 : if ( ( error = ivas_spar_dec( st_ivas, nb_bits_metadata ) ) != IVAS_ERR_OK )
429 : {
430 0 : return error;
431 : }
432 :
433 111021 : if ( st_ivas->nchan_transport == CPE_CHANNELS && st_ivas->nCPE >= 1 )
434 : {
435 19071 : st_ivas->hCPE[0]->element_brate = ivas_total_brate;
436 : }
437 :
438 : /* core-decoding of transport channels */
439 111021 : if ( st_ivas->nSCE == 1 )
440 : {
441 20937 : if ( ( error = ivas_sce_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] + nb_bits_metadata[1] ) ) != IVAS_ERR_OK )
442 : {
443 0 : return error;
444 : }
445 : }
446 90084 : else if ( st_ivas->nCPE == 1 )
447 : {
448 19071 : if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] + nb_bits_metadata[1] ) ) != IVAS_ERR_OK )
449 : {
450 0 : return error;
451 : }
452 : }
453 71013 : else if ( st_ivas->nCPE > 1 )
454 : {
455 71013 : if ( ( error = ivas_mct_dec( st_ivas, p_output, output_frame, nb_bits_metadata[0] + nb_bits_metadata[1] ) ) != IVAS_ERR_OK )
456 : {
457 0 : return error;
458 : }
459 : }
460 :
461 111021 : if ( st_ivas->sba_dirac_stereo_flag )
462 : {
463 9000 : ivas_agc_dec_process( st_ivas->hSpar->hAgcDec, &p_output[sba_ch_idx], &p_output[sba_ch_idx], st_ivas->hSpar->hMdDec->spar_md_cfg.nchan_transport, output_frame );
464 :
465 9000 : if ( st_ivas->hSpar->hPCA != NULL )
466 : {
467 0 : ivas_pca_dec( st_ivas->hSpar->hPCA, output_frame, st_ivas->hSpar->hMdDec->spar_md_cfg.nchan_transport, st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->hDecoderConfig->last_ivas_total_brate, st_ivas->bfi, &p_output[sba_ch_idx] );
468 : }
469 :
470 9000 : ivas_spar_dec_gen_umx_mat( st_ivas->hSpar->hMdDec, st_ivas->nchan_transport, IVAS_MAX_NUM_BANDS, st_ivas->bfi, ivas_get_spar_dec_md_num_subframes( st_ivas->sba_order, st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->last_active_ivas_total_brate ) );
471 :
472 9000 : ivas_sba_dirac_stereo_dec( st_ivas, &p_output[sba_ch_idx], output_frame, 0 );
473 : }
474 :
475 : /* HP filtering */
476 615246 : for ( n = 0; n < getNumChanSynthesis( st_ivas ); n++ )
477 : {
478 504225 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
479 : }
480 :
481 111021 : nchan_remapped = ivas_sba_remapTCs( &p_output[sba_ch_idx], st_ivas, output_frame );
482 :
483 : #ifdef DEBUG_OSBA
484 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
485 : {
486 : int16_t nchan = st_ivas->nchan_transport + st_ivas->nchan_ism;
487 : for ( int16_t t = 0; t < output_frame; t++ )
488 : {
489 : for ( int16_t c = 0; c < nchan; c++ )
490 : {
491 : int16_t val = (int16_t) ( output[c][t] + 0.5f );
492 : dbgwrite( &val, sizeof( int16_t ), 1, 1, "./res/TC_dec_core_out.raw" );
493 : }
494 : }
495 : }
496 : #endif
497 111021 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
498 : {
499 24825 : num_md_sub_frames = ivas_get_spar_dec_md_num_subframes( st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->last_active_ivas_total_brate );
500 :
501 24825 : ivas_sba_mix_matrix_determiner( st_ivas->hSpar, &p_output[sba_ch_idx], st_ivas->bfi, nchan_remapped, output_frame, num_md_sub_frames );
502 : }
503 86196 : else if ( st_ivas->renderer_type != RENDERER_DISABLE && !st_ivas->sba_dirac_stereo_flag )
504 : {
505 74196 : ivas_spar_dec_agc_pca( st_ivas, &p_output[sba_ch_idx], output_frame );
506 : }
507 :
508 111021 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
509 : {
510 : /* loudness correction */
511 24825 : ivas_dirac_dec_binaural_sba_gain( &p_output[sba_ch_idx], nchan_remapped, output_frame );
512 : }
513 86196 : else if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX && st_ivas->ism_mode == ISM_SBA_MODE_DISC )
514 : {
515 3000 : ivas_ism_mono_dmx( st_ivas, p_output, output_frame );
516 :
517 : /* add W */
518 6000 : for ( n = 0; n < nchan_out; n++ )
519 : {
520 3000 : v_add( p_output[n], p_output[n + max( nchan_out, nchan_ism )], p_output[n], output_frame );
521 : }
522 : }
523 : }
524 146562 : else if ( st_ivas->ivas_format == MC_FORMAT )
525 : {
526 146562 : st = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[0]->hCoreCoder[0] : st_ivas->hCPE[0]->hCoreCoder[0];
527 :
528 146562 : if ( st_ivas->mc_mode == MC_MODE_MCT )
529 : {
530 : /* LFE channel decoder */
531 78408 : ivas_lfe_dec( st_ivas->hLFE, st, output_frame, st_ivas->bfi, p_output[LFE_CHANNEL] );
532 :
533 78408 : if ( ( error = ivas_mct_dec( st_ivas, p_output, output_frame, 0 ) ) != IVAS_ERR_OK )
534 : {
535 0 : return error;
536 : }
537 :
538 : /* HP filtering */
539 588024 : for ( n = 0; n < st_ivas->nchan_transport; n++ )
540 : {
541 509616 : if ( n != LFE_CHANNEL )
542 : {
543 431208 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
544 : }
545 : }
546 :
547 78408 : if ( st_ivas->transport_config != st_ivas->intern_config && ( st_ivas->intern_config == IVAS_AUDIO_CONFIG_FOA || st_ivas->intern_config == IVAS_AUDIO_CONFIG_HOA2 || st_ivas->intern_config == IVAS_AUDIO_CONFIG_HOA3 ) )
548 : {
549 636 : if ( ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) >= ( st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe ) )
550 : {
551 0 : ivas_mc2sba( st_ivas->hTransSetup, p_output, p_output, output_frame, st_ivas->hIntSetup.ambisonics_order, GAIN_LFE );
552 : }
553 : }
554 :
555 78408 : if ( ( st_ivas->renderer_type == RENDERER_MC || st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC ) && ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) >= ( st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe ) )
556 : {
557 9087 : if ( st_ivas->renderer_type == RENDERER_MC )
558 : {
559 9087 : ivas_ls_setup_conversion( st_ivas, st_ivas->nchan_transport, output_frame, p_output, p_output );
560 : }
561 0 : else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
562 : {
563 0 : ivas_mc2sba( st_ivas->hIntSetup, p_output, p_output, output_frame, st_ivas->hOutSetup.ambisonics_order, 0.f );
564 : }
565 : }
566 : }
567 68154 : else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
568 : {
569 : /* LFE channel decoder */
570 2370 : ivas_lfe_dec( st_ivas->hLFE, st, output_frame, st_ivas->bfi, p_output[LFE_CHANNEL] );
571 :
572 2370 : ivas_mc_paramupmix_dec_read_BS( st_ivas, st, st_ivas->hMCParamUpmix, &nb_bits_metadata[0] );
573 :
574 2370 : if ( ( error = ivas_mct_dec( st_ivas, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
575 : {
576 0 : return error;
577 : }
578 :
579 : /* HP filtering */
580 21330 : for ( n = 0; n < st_ivas->nchan_transport; n++ )
581 : {
582 18960 : if ( n != LFE_CHANNEL )
583 : {
584 16590 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
585 : }
586 : }
587 :
588 : /* Rendering */
589 2370 : if ( st_ivas->renderer_type == RENDERER_MC && ( output_config == IVAS_AUDIO_CONFIG_MONO || output_config == IVAS_AUDIO_CONFIG_STEREO ) )
590 : {
591 : /* Compensate loudness for not doing full upmix */
592 150 : for ( n = 4; n < 8; n++ )
593 : {
594 120 : v_multc( p_output[n], 2.0f, p_output[n], output_frame );
595 : }
596 :
597 30 : if ( output_config == IVAS_AUDIO_CONFIG_MONO || output_config == IVAS_AUDIO_CONFIG_STEREO )
598 : {
599 30 : ivas_ls_setup_conversion( st_ivas, audioCfg2channels( IVAS_AUDIO_CONFIG_5_1_2 ), output_frame, p_output, p_output );
600 : }
601 : }
602 : }
603 65784 : else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
604 : {
605 : /* read Parametric MC parameters from the bitstream */
606 30759 : ivas_param_mc_dec_read_BS( ivas_total_brate, st, st_ivas->hParamMC, &nb_bits_metadata[0] );
607 :
608 30759 : if ( st_ivas->nCPE == 1 )
609 : {
610 30429 : if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
611 : {
612 0 : return error;
613 : }
614 : }
615 330 : else if ( st_ivas->nCPE > 1 )
616 : {
617 330 : if ( ( error = ivas_mct_dec( st_ivas, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
618 : {
619 0 : return error;
620 : }
621 : }
622 :
623 : /* HP filtering */
624 92607 : for ( n = 0; n < st_ivas->nchan_transport; n++ )
625 : {
626 61848 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
627 : }
628 :
629 : /* Rendering */
630 30759 : if ( output_config == IVAS_AUDIO_CONFIG_MONO || output_config == IVAS_AUDIO_CONFIG_STEREO )
631 : {
632 60 : ivas_ls_setup_conversion( st_ivas, st_ivas->nchan_transport, output_frame, p_output, p_output );
633 : }
634 : }
635 35025 : else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
636 : {
637 35025 : if ( st_ivas->hOutSetup.separateChannelEnabled )
638 : {
639 1869 : st = st_ivas->hCPE[0]->hCoreCoder[0]; /* Metadata is always with CPE in the case of separated channel */
640 : }
641 :
642 : /* read McMASA parameters from the bitstream */
643 35025 : if ( ( error = ivas_masa_decode( st_ivas, st, &nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
644 : {
645 0 : return error;
646 : }
647 :
648 35025 : if ( st_ivas->hOutSetup.separateChannelEnabled )
649 : {
650 : /* Decode the transport audio signals */
651 1869 : if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
652 : {
653 0 : return error;
654 : }
655 :
656 : /* Identify the index of the separated channel, always LFE_CHANNEL-1 here */
657 1869 : n = LFE_CHANNEL - 1;
658 :
659 : /* Decode the separated channel to output[n] to be combined with the synthesized channels */
660 1869 : if ( ( error = ivas_sce_dec( st_ivas, 0, &p_output[n], output_frame, 0 ) ) != IVAS_ERR_OK )
661 : {
662 0 : return error;
663 : }
664 :
665 : /* Delay the separated channel to sync with CLDFB delay of the DirAC synthesis, and synthesize the LFE signal. */
666 1869 : if ( output_config == IVAS_AUDIO_CONFIG_5_1 || output_config == IVAS_AUDIO_CONFIG_7_1 ||
667 1422 : output_config == IVAS_AUDIO_CONFIG_5_1_4 || output_config == IVAS_AUDIO_CONFIG_7_1_4 ||
668 1422 : output_config == IVAS_AUDIO_CONFIG_5_1_2 || ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.num_lfe > 0 ) || output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
669 : {
670 582 : ivas_lfe_synth_with_filters( st_ivas->hMasa->hMasaLfeSynth, p_output, output_frame, n, LFE_CHANNEL );
671 : }
672 1287 : else if ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.num_lfe == 0 )
673 : {
674 : /* Delay the separated channel to sync with the DirAC rendering */
675 0 : delay_signal( p_output[n], output_frame, st_ivas->hMasa->hMasaLfeSynth->delayBuffer_syncDirAC, st_ivas->hMasa->hMasaLfeSynth->delayBuffer_syncDirAC_size );
676 : }
677 : }
678 : else
679 : {
680 33156 : if ( st_ivas->nSCE == 1 )
681 : {
682 31896 : if ( ( error = ivas_sce_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
683 : {
684 0 : return error;
685 : }
686 : }
687 1260 : else if ( st_ivas->nCPE == 1 )
688 : {
689 1260 : if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
690 : {
691 0 : return error;
692 : }
693 : }
694 : }
695 :
696 35025 : if ( st_ivas->sba_dirac_stereo_flag ) /* use the flag to trigger the DFT upmix */
697 : {
698 3534 : ivas_sba_dirac_stereo_dec( st_ivas, p_output, output_frame, 1 );
699 : }
700 :
701 : /* HP filtering */
702 78582 : for ( n = 0; n < getNumChanSynthesis( st_ivas ); n++ )
703 : {
704 43557 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
705 : }
706 :
707 35025 : if ( st_ivas->renderer_type == RENDERER_MCMASA_MONO_STEREO )
708 : {
709 1497 : ivas_mono_stereo_downmix_mcmasa( st_ivas, p_output, output_frame );
710 : }
711 : }
712 : }
713 :
714 : /*----------------------------------------------------------------*
715 : * Common updates
716 : *----------------------------------------------------------------*/
717 :
718 1265478 : if ( !st_ivas->bfi ) /* do not update if first frame(s) are lost or NO_DATA */
719 : {
720 1232967 : st_ivas->hDecoderConfig->last_ivas_total_brate = ivas_total_brate;
721 1232967 : st_ivas->last_active_ivas_total_brate = ( ivas_total_brate <= IVAS_SID_5k2 ) ? st_ivas->last_active_ivas_total_brate : ivas_total_brate;
722 : }
723 :
724 1265478 : if ( st_ivas->ini_frame < MAX_FRAME_COUNTER && !( st_ivas->bfi && st_ivas->ini_frame == 0 ) ) /* keep "st_ivas->ini_frame = 0" until first good received frame */
725 : {
726 353202 : st_ivas->ini_frame++;
727 : }
728 :
729 1265478 : if ( st_ivas->ini_active_frame < MAX_FRAME_COUNTER && !( st_ivas->bfi && st_ivas->ini_frame == 0 ) && ivas_total_brate > IVAS_SID_5k2 ) /* needed in MASA decoder in case the first active frame is BFI, and there were SID-frames decoded before */
730 : {
731 350457 : st_ivas->ini_active_frame++;
732 : }
733 :
734 1265478 : st_ivas->last_ivas_format = st_ivas->ivas_format;
735 :
736 : #ifdef DEBUG_MODE_INFO
737 : dbgwrite( &st_ivas->bfi, sizeof( int16_t ), 1, output_frame, "res/bfi" );
738 : dbgwrite( &st_ivas->BER_detect, sizeof( int16_t ), 1, output_frame, "res/BER_detect" );
739 : {
740 : float tmpF = ivas_total_brate / 1000.0f;
741 : dbgwrite( &tmpF, sizeof( float ), 1, output_frame, "res/ivas_total_brate.dec" );
742 : }
743 : #endif
744 :
745 1265478 : pop_wmops();
746 1265478 : return IVAS_ERR_OK;
747 : }
748 :
749 :
750 : /*--------------------------------------------------------------------------*
751 : * ivas_dec_feed_tc_to_renderer()
752 : *
753 : * Feed decoded transport channels to the IVAS renderer routine
754 : * + digest TC channels in ParamISM and ParamMC
755 : *--------------------------------------------------------------------------*/
756 :
757 1274778 : void ivas_jbm_dec_feed_tc_to_renderer(
758 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
759 : const int16_t nSamplesForRendering, /* i : number of TC samples available for rendering */
760 : int16_t *nSamplesResidual /* o : number of samples not fitting into the renderer grid and buffer for the next call*/
761 : )
762 : {
763 : float tmp_buf[MAX_JBM_L_FRAME48k];
764 : float *p_data_f[FOA_CHANNELS + MAX_NUM_OBJECTS];
765 : int16_t n, n_render_timeslots, n_ch_cldfb;
766 : int16_t ch;
767 : DECODER_TC_BUFFER_HANDLE hTcBuffer;
768 :
769 1274778 : hTcBuffer = st_ivas->hTcBuffer;
770 1274778 : n_ch_cldfb = hTcBuffer->nchan_transport_jbm - hTcBuffer->nchan_buffer_full;
771 :
772 1274778 : if ( st_ivas->hDecoderConfig->Opt_tsm )
773 : {
774 : int16_t n_samples_still_available;
775 : int16_t n_ch_full_copy, n_ch_res_copy;
776 :
777 76596 : n_samples_still_available = hTcBuffer->n_samples_buffered - hTcBuffer->n_samples_rendered;
778 76596 : hTcBuffer->n_samples_buffered = n_samples_still_available + nSamplesForRendering + hTcBuffer->n_samples_discard;
779 76596 : hTcBuffer->n_samples_available = hTcBuffer->n_samples_granularity * ( hTcBuffer->n_samples_buffered / hTcBuffer->n_samples_granularity );
780 76596 : *nSamplesResidual = hTcBuffer->n_samples_buffered - hTcBuffer->n_samples_available;
781 76596 : n_ch_full_copy = min( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full );
782 76596 : n_ch_res_copy = hTcBuffer->nchan_transport_jbm - hTcBuffer->nchan_buffer_full;
783 :
784 257454 : for ( ch = 0; ch < n_ch_full_copy; ch++ )
785 : {
786 180858 : mvr2r( hTcBuffer->tc[ch], tmp_buf, nSamplesForRendering );
787 180858 : set_zero( hTcBuffer->tc[ch], hTcBuffer->n_samples_discard );
788 180858 : mvr2r( hTcBuffer->tc_buffer_old[ch], hTcBuffer->tc[ch] + hTcBuffer->n_samples_discard, n_samples_still_available );
789 180858 : mvr2r( tmp_buf, hTcBuffer->tc[ch] + n_samples_still_available + hTcBuffer->n_samples_discard, nSamplesForRendering - *nSamplesResidual );
790 180858 : mvr2r( tmp_buf + nSamplesForRendering - *nSamplesResidual, hTcBuffer->tc_buffer_old[ch], *nSamplesResidual );
791 : }
792 :
793 76596 : if ( n_ch_res_copy > 0 )
794 : {
795 27489 : for ( ; ch < hTcBuffer->nchan_transport_jbm; ch++ )
796 : {
797 18336 : p_data_f[ch] = hTcBuffer->tc[ch];
798 18336 : mvr2r( hTcBuffer->tc[ch], tmp_buf, nSamplesForRendering );
799 18336 : mvr2r( hTcBuffer->tc_buffer_old[ch], p_data_f[ch], n_samples_still_available );
800 18336 : mvr2r( tmp_buf, p_data_f[ch] + n_samples_still_available, nSamplesForRendering - *nSamplesResidual );
801 18336 : mvr2r( tmp_buf + nSamplesForRendering - *nSamplesResidual, hTcBuffer->tc_buffer_old[ch], *nSamplesResidual );
802 : }
803 : }
804 :
805 76596 : n_render_timeslots = hTcBuffer->n_samples_available / hTcBuffer->n_samples_granularity;
806 : }
807 : else
808 : {
809 1282062 : for ( n = 0; n < n_ch_cldfb; n++ )
810 : {
811 83880 : p_data_f[n] = &st_ivas->p_output_f[n][0];
812 : }
813 :
814 1198182 : ch = max( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full );
815 5362380 : for ( n = 0; n < ch; n++ )
816 : {
817 4164198 : hTcBuffer->tc[n] = st_ivas->p_output_f[n]; /* note: buffers needed in the TD decorellator */
818 : }
819 :
820 1198182 : hTcBuffer->n_samples_buffered = nSamplesForRendering;
821 1198182 : hTcBuffer->n_samples_available = hTcBuffer->n_samples_buffered;
822 1198182 : *nSamplesResidual = 0;
823 :
824 1198182 : n_render_timeslots = DEFAULT_JBM_CLDFB_TIMESLOTS;
825 : }
826 :
827 : /* CLDFB analysis for ParamMC/ParamISM */
828 1274778 : if ( st_ivas->ivas_format == ISM_FORMAT && st_ivas->ism_mode == ISM_MODE_PARAM && ( st_ivas->renderer_type == RENDERER_PARAM_ISM || st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC ) )
829 : {
830 20274 : ivas_param_ism_dec_digest_tc( st_ivas, n_render_timeslots, p_data_f );
831 : }
832 1254504 : else if ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_PARAMMC && hTcBuffer->tc_buffer_mode == TC_BUFFER_MODE_RENDERER )
833 : {
834 30699 : ivas_param_mc_dec_digest_tc( st_ivas, (uint8_t) n_render_timeslots, p_data_f );
835 : }
836 :
837 1274778 : hTcBuffer->n_samples_rendered = 0;
838 1274778 : hTcBuffer->subframes_rendered = 0;
839 :
840 1274778 : return;
841 : }
842 :
843 :
844 : /*--------------------------------------------------------------------------*
845 : * ivas_dec_render()
846 : *
847 : * Principal IVAS JBM rendering routine
848 : *--------------------------------------------------------------------------*/
849 :
850 2881221 : ivas_error ivas_jbm_dec_render(
851 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
852 : const uint16_t nSamplesAsked, /* i : number of samples wanted */
853 : uint16_t *nSamplesRendered, /* o : number of samples rendered */
854 : uint16_t *nSamplesAvailableNext, /* o : number of samples still available in the rendering pipeline */
855 : const PCM_RESOLUTION pcm_resolution, /* i : type for the decoded PCM resolution */
856 : void *data /* o : output synthesis signal */
857 : )
858 : {
859 : int16_t n, nchan_out;
860 : int16_t nchan_transport;
861 : int16_t nchan_remapped;
862 : int32_t output_Fs;
863 : AUDIO_CONFIG output_config;
864 : int16_t nSamplesAskedLocal;
865 : ivas_error error;
866 : float *p_output[MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS];
867 : float *p_tc[MAX_TRANSPORT_CHANNELS + MAX_NUM_OBJECTS];
868 : int16_t nchan_out_syn_output;
869 :
870 2881221 : push_wmops( "ivas_dec_render" );
871 : /*----------------------------------------------------------------*
872 : * Initialization of local vars after struct has been set
873 : *----------------------------------------------------------------*/
874 :
875 2881221 : output_Fs = st_ivas->hDecoderConfig->output_Fs;
876 2881221 : nchan_out = st_ivas->hDecoderConfig->nchan_out;
877 2881221 : nchan_transport = st_ivas->hTcBuffer->nchan_transport_jbm;
878 2881221 : output_config = st_ivas->hDecoderConfig->output_config;
879 2881221 : nSamplesAskedLocal = nSamplesAsked + st_ivas->hTcBuffer->n_samples_discard;
880 :
881 60505641 : for ( n = 0; n < MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS; n++ )
882 : {
883 57624420 : p_output[n] = st_ivas->p_output_f[n];
884 : }
885 :
886 2881221 : if ( !st_ivas->hDecoderConfig->Opt_tsm )
887 : {
888 45325638 : for ( n = 0; n < MAX_INTERN_CHANNELS; n++ )
889 : {
890 42659424 : st_ivas->hTcBuffer->tc[n] = p_output[n];
891 : }
892 : }
893 :
894 12596775 : for ( n = 0; n < st_ivas->hTcBuffer->nchan_buffer_full; n++ )
895 : {
896 9715554 : p_tc[n] = &st_ivas->hTcBuffer->tc[n][st_ivas->hTcBuffer->n_samples_rendered];
897 : }
898 :
899 : /*----------------------------------------------------------------*
900 : * Update combined orientation access index
901 : *----------------------------------------------------------------*/
902 :
903 2881221 : if ( st_ivas->hCombinedOrientationData != NULL )
904 : {
905 : /* take the discard samples into account here to make sure head rotation stays on the correct 5ms grid */
906 488976 : st_ivas->hCombinedOrientationData->cur_subframe_samples_rendered_start -= st_ivas->hTcBuffer->n_samples_discard;
907 :
908 488976 : ivas_combined_orientation_set_to_start_index( st_ivas->hCombinedOrientationData );
909 : }
910 :
911 : /*----------------------------------------------------------------*
912 : * Rendering
913 : *----------------------------------------------------------------*/
914 :
915 2881221 : if ( st_ivas->ivas_format == UNDEFINED_FORMAT )
916 : {
917 0 : assert( 0 );
918 : }
919 2881221 : else if ( st_ivas->hTcBuffer->tc_buffer_mode == TC_BUFFER_MODE_BUFFER )
920 : {
921 796147 : ivas_jbm_dec_tc_buffer_playout( st_ivas, nSamplesAskedLocal, nSamplesRendered, p_output );
922 : }
923 2085074 : else if ( st_ivas->ivas_format == STEREO_FORMAT )
924 : {
925 : /* Rendering */
926 0 : if ( st_ivas->renderer_type == RENDERER_MC )
927 : {
928 0 : *nSamplesRendered = min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
929 0 : ivas_ls_setup_conversion( st_ivas, st_ivas->nchan_transport, *nSamplesRendered, p_tc, p_output );
930 : }
931 : }
932 2085074 : else if ( st_ivas->ivas_format == ISM_FORMAT )
933 : {
934 : /* Rendering */
935 563750 : if ( st_ivas->ism_mode == ISM_MODE_PARAM )
936 : {
937 133711 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
938 : {
939 80350 : ivas_dirac_dec_binaural_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, st_ivas->nchan_transport, p_output );
940 : }
941 53361 : else if ( st_ivas->renderer_type == RENDERER_PARAM_ISM || st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
942 : {
943 53361 : ivas_param_ism_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output );
944 :
945 53361 : if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
946 : {
947 : /* Convert CICP19 -> Ambisonics */
948 41232 : ivas_mc2sba( st_ivas->hIntSetup, p_output, p_output, *nSamplesRendered, st_ivas->hOutSetup.ambisonics_order, 0.f );
949 : }
950 : }
951 : }
952 : else /* ISM_MODE_DISC */
953 : {
954 430039 : *nSamplesRendered = min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
955 :
956 : /* Loudspeaker or Ambisonics rendering */
957 430039 : if ( st_ivas->renderer_type == RENDERER_TD_PANNING || st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM )
958 : {
959 : /* Convert to CICPxx; used also for ISM->CICP19->binaural_room rendering */
960 147604 : ivas_ism_render_sf( st_ivas, st_ivas->renderer_type, p_output, *nSamplesRendered );
961 : }
962 282435 : else if ( st_ivas->renderer_type == RENDERER_NON_DIEGETIC_DOWNMIX )
963 : {
964 10500 : ivas_apply_non_diegetic_panning( p_tc[0], p_output, st_ivas->hDecoderConfig->non_diegetic_pan_gain, *nSamplesRendered );
965 : }
966 : #ifdef DEBUGGING
967 : else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV )
968 : #else
969 271935 : else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
970 : #endif
971 : {
972 : /* Convert to Ambisonics */
973 49129 : ivas_ism2sba_sf( p_tc, p_output, st_ivas->hIsmRendererData, st_ivas->nchan_transport, *nSamplesRendered, st_ivas->hTcBuffer->n_samples_rendered, st_ivas->hIntSetup.ambisonics_order );
974 : }
975 :
976 : /* Binaural rendering */
977 430039 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_OBJECTS_TD )
978 : {
979 222806 : if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
980 : {
981 0 : if ( ( error = ivas_td_binaural_renderer_sf_splitBinaural( st_ivas, p_output, *nSamplesRendered ) ) != IVAS_ERR_OK )
982 : {
983 0 : return error;
984 : }
985 : }
986 : else
987 : {
988 222806 : if ( ( error = ivas_td_binaural_renderer_sf( st_ivas, p_output, *nSamplesRendered ) ) != IVAS_ERR_OK )
989 : {
990 0 : return error;
991 : }
992 : }
993 : }
994 207233 : else if ( st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM )
995 : {
996 76904 : if ( ( error = ivas_rend_crendProcessSubframe( st_ivas->hCrendWrapper, IVAS_AUDIO_CONFIG_7_1_4, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR, st_ivas->hDecoderConfig, NULL,
997 76904 : NULL, NULL, st_ivas->hTcBuffer, p_output, p_output, *nSamplesRendered, output_Fs, 0 ) ) != IVAS_ERR_OK )
998 : {
999 0 : return error;
1000 : }
1001 : }
1002 : #ifdef DEBUGGING
1003 : else if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
1004 : {
1005 : ivas_binaural_cldfb_sf( st_ivas, *nSamplesRendered, st_ivas->hTcBuffer->nb_subframes, p_output );
1006 : }
1007 : #endif
1008 : }
1009 : }
1010 1521324 : else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
1011 : {
1012 968792 : nchan_remapped = nchan_transport;
1013 :
1014 : /* Loudspeakers, Ambisonics or Binaural rendering */
1015 968792 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
1016 : {
1017 308438 : ivas_dirac_dec_binaural_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output );
1018 : }
1019 660354 : else if ( st_ivas->ivas_format == MASA_FORMAT )
1020 : {
1021 99041 : if ( st_ivas->renderer_type == RENDERER_DIRAC )
1022 : {
1023 99041 : ivas_dirac_dec_render( st_ivas, nchan_remapped, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output );
1024 : }
1025 : }
1026 : else
1027 : {
1028 561313 : if ( ( error = ivas_sba_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
1029 : {
1030 0 : return error;
1031 : }
1032 : }
1033 : }
1034 552532 : else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
1035 : {
1036 39965 : nchan_remapped = st_ivas->nchan_transport;
1037 :
1038 39965 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
1039 : {
1040 20892 : if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC && st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC )
1041 : {
1042 2305 : if ( ( error = ivas_omasa_dirac_td_binaural_jbm( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output ) ) != IVAS_ERR_OK )
1043 : {
1044 0 : return error;
1045 : }
1046 : }
1047 : else
1048 : {
1049 18587 : ivas_dirac_dec_binaural_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output );
1050 : }
1051 : }
1052 19073 : else if ( st_ivas->renderer_type == RENDERER_DIRAC )
1053 : {
1054 18845 : ivas_omasa_dirac_rend_jbm( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output );
1055 : }
1056 228 : else if ( st_ivas->renderer_type == RENDERER_OMASA_OBJECT_EXT || st_ivas->renderer_type == RENDERER_OMASA_MIX_EXT )
1057 : {
1058 228 : ivas_jbm_dec_tc_buffer_playout( st_ivas, nSamplesAskedLocal, nSamplesRendered, p_output );
1059 228 : ivas_omasa_rearrange_channels( p_output, st_ivas->nchan_ism, *nSamplesRendered );
1060 : }
1061 : }
1062 512567 : else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
1063 : {
1064 226741 : nchan_remapped = nchan_transport;
1065 :
1066 : /* Loudspeakers, Ambisonics or Binaural rendering */
1067 226741 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
1068 : {
1069 141174 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV )
1070 : {
1071 72316 : if ( ( error = ivas_osba_dirac_td_binaural_jbm( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
1072 : {
1073 0 : return error;
1074 : }
1075 : }
1076 68858 : else if ( st_ivas->renderer_type == RENDERER_OSBA_STEREO )
1077 : {
1078 9800 : *nSamplesRendered = min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
1079 :
1080 : /* shift SBA channels to avoid overwrite by ISM upmix in 1 object case and non-TSM unified channel memory*/
1081 9800 : if ( st_ivas->nchan_ism == 1 && st_ivas->hDecoderConfig->Opt_tsm == 0 )
1082 : {
1083 0 : mvr2r( p_tc[2], p_output[3], *nSamplesRendered );
1084 0 : mvr2r( p_tc[1], p_output[2], *nSamplesRendered );
1085 0 : p_tc[1] = p_output[2];
1086 0 : p_tc[2] = p_output[3];
1087 : }
1088 :
1089 : /* render objects */
1090 9800 : ivas_ism_render_sf( st_ivas, st_ivas->renderer_type, p_output, *nSamplesRendered );
1091 :
1092 : /* add already rendered SBA part */
1093 9800 : ivas_osba_stereo_add_channels( p_tc, p_output, st_ivas->hSbaIsmData->gain_bed, nchan_out, st_ivas->nchan_ism, *nSamplesRendered );
1094 : }
1095 59058 : else if ( st_ivas->renderer_type == RENDERER_OSBA_AMBI || st_ivas->renderer_type == RENDERER_OSBA_LS || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
1096 : {
1097 51858 : if ( ( error = ivas_osba_render_sf( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
1098 : {
1099 0 : return error;
1100 : }
1101 : }
1102 7200 : else if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL ) /*EXT output = individual objects + HOA3*/
1103 : {
1104 7200 : if ( ( error = ivas_sba_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, &p_output[st_ivas->nchan_ism] ) ) != IVAS_ERR_OK )
1105 : {
1106 0 : return error;
1107 : }
1108 :
1109 27000 : for ( n = 0; n < st_ivas->nchan_ism; n++ )
1110 : {
1111 19800 : mvr2r( p_tc[n], p_output[n], *nSamplesRendered );
1112 : }
1113 : }
1114 : else
1115 : {
1116 0 : if ( ( error = ivas_sba_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
1117 : {
1118 0 : return error;
1119 : }
1120 : }
1121 : }
1122 85567 : else if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
1123 : {
1124 59257 : ivas_dirac_dec_binaural_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output );
1125 : }
1126 : else
1127 : {
1128 26310 : if ( ( error = ivas_sba_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
1129 : {
1130 0 : return error;
1131 : }
1132 :
1133 26310 : if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
1134 : {
1135 : #ifdef DEBUGGING
1136 : assert( st_ivas->ism_mode == ISM_MODE_NONE );
1137 : #endif
1138 30600 : for ( n = st_ivas->hIntSetup.nchan_out_woLFE - 1; n >= 0; n-- )
1139 : {
1140 28800 : mvr2r( p_output[n], p_output[n + st_ivas->nchan_ism], *nSamplesRendered );
1141 : }
1142 9000 : for ( n = 0; n < st_ivas->nchan_ism; n++ )
1143 : {
1144 7200 : set_zero( p_output[n], *nSamplesRendered );
1145 : }
1146 : }
1147 : }
1148 : }
1149 285826 : else if ( st_ivas->ivas_format == MC_FORMAT )
1150 : {
1151 285826 : if ( st_ivas->mc_mode == MC_MODE_MCT )
1152 : {
1153 147003 : int16_t crendInPlaceRotation = FALSE;
1154 147003 : *nSamplesRendered = min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
1155 147003 : if ( st_ivas->transport_config != st_ivas->intern_config && ( st_ivas->intern_config == IVAS_AUDIO_CONFIG_FOA || st_ivas->intern_config == IVAS_AUDIO_CONFIG_HOA2 || st_ivas->intern_config == IVAS_AUDIO_CONFIG_HOA3 ) )
1156 : {
1157 1484 : if ( ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) < ( st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe ) )
1158 : {
1159 1484 : crendInPlaceRotation = TRUE;
1160 1484 : ivas_mc2sba( st_ivas->hTransSetup, p_tc, p_output, *nSamplesRendered, st_ivas->hIntSetup.ambisonics_order, GAIN_LFE );
1161 : }
1162 : }
1163 :
1164 : /* Rendering */
1165 147003 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV || st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM )
1166 : {
1167 94626 : if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
1168 : {
1169 0 : if ( ( error = ivas_rend_crendProcessSubframesSplitBin( st_ivas->hCrendWrapper, st_ivas->intern_config, st_ivas->hOutSetup.output_config, ( st_ivas->hSplitBinRend == NULL ) ? NULL : &st_ivas->hSplitBinRend->splitrend.multiBinPoseData, st_ivas->hDecoderConfig, st_ivas->hCombinedOrientationData,
1170 0 : &st_ivas->hIntSetup, st_ivas->hEFAPdata, st_ivas->hTcBuffer, crendInPlaceRotation ? p_output : p_tc, p_output, *nSamplesRendered, output_Fs ) ) != IVAS_ERR_OK )
1171 : {
1172 0 : return error;
1173 : }
1174 : }
1175 : else
1176 : {
1177 189252 : if ( ( error = ivas_rend_crendProcessSubframe( st_ivas->hCrendWrapper, st_ivas->intern_config, st_ivas->hOutSetup.output_config, st_ivas->hDecoderConfig, st_ivas->hCombinedOrientationData,
1178 189252 : &st_ivas->hIntSetup, st_ivas->hEFAPdata, st_ivas->hTcBuffer, crendInPlaceRotation ? p_output : p_tc, p_output, *nSamplesRendered, output_Fs, 0 ) ) != IVAS_ERR_OK )
1179 : {
1180 0 : return error;
1181 : }
1182 :
1183 94626 : ivas_binaural_add_LFE( st_ivas, *nSamplesRendered, p_tc, p_output );
1184 : }
1185 : }
1186 52377 : else if ( st_ivas->renderer_type == RENDERER_MC )
1187 : {
1188 27065 : *nSamplesRendered = min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
1189 27065 : ivas_ls_setup_conversion( st_ivas, st_ivas->nchan_transport, *nSamplesRendered, p_tc, p_output );
1190 : }
1191 25312 : else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
1192 : {
1193 3430 : ivas_mc2sba( st_ivas->hIntSetup, p_tc, p_output, *nSamplesRendered, st_ivas->hOutSetup.ambisonics_order, 0.f );
1194 : }
1195 21882 : else if ( st_ivas->renderer_type == RENDERER_BINAURAL_OBJECTS_TD )
1196 : {
1197 21882 : if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
1198 : {
1199 0 : if ( ( error = ivas_td_binaural_renderer_sf_splitBinaural( st_ivas, p_output, *nSamplesRendered ) ) != IVAS_ERR_OK )
1200 : {
1201 0 : return error;
1202 : }
1203 : }
1204 : else
1205 : {
1206 21882 : if ( ( error = ivas_td_binaural_renderer_sf( st_ivas, p_output, *nSamplesRendered ) ) != IVAS_ERR_OK )
1207 : {
1208 0 : return error;
1209 : }
1210 :
1211 21882 : ivas_binaural_add_LFE( st_ivas, *nSamplesRendered, p_tc, p_output );
1212 : }
1213 : }
1214 : }
1215 138823 : else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
1216 : {
1217 5420 : ivas_mc_paramupmix_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_tc, p_output );
1218 :
1219 :
1220 : /* Rendering */
1221 5420 : if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ) && !st_ivas->hDecoderConfig->Opt_Headrotation )
1222 : {
1223 : /* handled in CLDFB domain already */
1224 1120 : if ( output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED && output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
1225 : {
1226 1120 : ivas_binaural_add_LFE( st_ivas, *nSamplesRendered, p_output, p_output );
1227 : }
1228 : }
1229 4300 : else if ( st_ivas->renderer_type == RENDERER_MC )
1230 : {
1231 0 : ivas_ls_setup_conversion( st_ivas, MC_PARAMUPMIX_MAX_INPUT_CHANS, *nSamplesRendered, p_output, p_output );
1232 : }
1233 4300 : else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
1234 : {
1235 70 : ivas_mc2sba( st_ivas->hIntSetup, p_output, p_output, *nSamplesRendered, st_ivas->hOutSetup.ambisonics_order, 0.f );
1236 : }
1237 : }
1238 133403 : else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
1239 : {
1240 71164 : ivas_param_mc_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output );
1241 : }
1242 62239 : else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
1243 : {
1244 62239 : nchan_remapped = st_ivas->nchan_transport;
1245 62239 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
1246 : {
1247 50046 : ivas_dirac_dec_binaural_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output );
1248 : }
1249 12193 : else if ( st_ivas->renderer_type == RENDERER_DIRAC || st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC ) /* rendering to CICPxx and Ambisonics */
1250 : {
1251 12193 : ivas_dirac_dec_render( st_ivas, nchan_remapped, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output );
1252 :
1253 12193 : if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
1254 : {
1255 : /* we still need to copy the separate channel if available */
1256 2030 : if ( st_ivas->hOutSetup.separateChannelEnabled )
1257 : {
1258 175 : mvr2r( p_tc[LFE_CHANNEL - 1], p_output[st_ivas->hOutSetup.separateChannelIndex], *nSamplesRendered );
1259 : }
1260 :
1261 2030 : ivas_mc2sba( st_ivas->hIntSetup, p_output, p_output, *nSamplesRendered, st_ivas->hOutSetup.ambisonics_order, 0.f );
1262 : }
1263 10163 : else if ( st_ivas->intern_config == IVAS_AUDIO_CONFIG_5_1 && ( output_config == IVAS_AUDIO_CONFIG_5_1_2 || output_config == IVAS_AUDIO_CONFIG_5_1_4 || output_config == IVAS_AUDIO_CONFIG_7_1 ) )
1264 : {
1265 0 : for ( n = st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe; n < st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe; n++ )
1266 : {
1267 0 : set_zero( p_output[n], *nSamplesRendered );
1268 : }
1269 : }
1270 : }
1271 :
1272 : /* copy discrete C and TD LFE from internal TC to output */
1273 62239 : if ( st_ivas->hOutSetup.separateChannelEnabled )
1274 : {
1275 1828 : if ( output_config == IVAS_AUDIO_CONFIG_5_1 || output_config == IVAS_AUDIO_CONFIG_7_1 ||
1276 785 : output_config == IVAS_AUDIO_CONFIG_5_1_4 || output_config == IVAS_AUDIO_CONFIG_7_1_4 ||
1277 785 : output_config == IVAS_AUDIO_CONFIG_5_1_2 || ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.num_lfe > 0 ) )
1278 : {
1279 1043 : mvr2r( p_tc[LFE_CHANNEL], p_output[LFE_CHANNEL], *nSamplesRendered );
1280 1043 : mvr2r( p_tc[LFE_CHANNEL - 1], p_output[st_ivas->hOutSetup.separateChannelIndex], *nSamplesRendered );
1281 : }
1282 785 : else if ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.num_lfe == 0 )
1283 : {
1284 0 : mvr2r( p_tc[LFE_CHANNEL - 1], p_output[st_ivas->hOutSetup.separateChannelIndex], *nSamplesRendered );
1285 : }
1286 : }
1287 : }
1288 : }
1289 :
1290 : /*----------------------------------------------------------------*
1291 : * Write IVAS output channels
1292 : * - compensation for saturation
1293 : * - float to integer conversion
1294 : *----------------------------------------------------------------*/
1295 :
1296 2881221 : st_ivas->hTcBuffer->n_samples_available -= *nSamplesRendered;
1297 2881221 : st_ivas->hTcBuffer->n_samples_rendered += *nSamplesRendered;
1298 :
1299 : /* update global combined orientation start index */
1300 2881221 : ivas_combined_orientation_update_start_index( st_ivas->hCombinedOrientationData, *nSamplesRendered );
1301 :
1302 2881221 : if ( st_ivas->hTcBuffer->n_samples_discard > 0 )
1303 : {
1304 2040 : for ( n = 0; n < min( MAX_OUTPUT_CHANNELS, ivas_get_nchan_buffers_dec( st_ivas, st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate ) ); n++ )
1305 : {
1306 1722 : p_output[n] += st_ivas->hTcBuffer->n_samples_discard;
1307 : }
1308 318 : *nSamplesRendered -= st_ivas->hTcBuffer->n_samples_discard;
1309 318 : st_ivas->hTcBuffer->n_samples_discard = 0;
1310 : }
1311 :
1312 2881221 : if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
1313 : {
1314 0 : nchan_out_syn_output = BINAURAL_CHANNELS * st_ivas->hSplitBinRend->splitrend.multiBinPoseData.num_poses;
1315 : }
1316 : else
1317 : {
1318 2881221 : nchan_out_syn_output = nchan_out;
1319 : }
1320 :
1321 2881221 : if ( is_split_rendering_enabled( st_ivas->hDecoderConfig, st_ivas->hRenderConfig ) == 0 )
1322 : {
1323 2881221 : if ( st_ivas->ivas_format != MONO_FORMAT )
1324 : {
1325 : #ifndef DISABLE_LIMITER
1326 2859521 : ivas_limiter_dec( st_ivas->hLimiter, p_output, nchan_out, *nSamplesRendered, st_ivas->BER_detect );
1327 : #endif
1328 : }
1329 : }
1330 :
1331 2881221 : switch ( pcm_resolution )
1332 : {
1333 2881221 : case PCM_INT16:
1334 : #ifdef DEBUGGING
1335 : st_ivas->noClipping +=
1336 : #endif
1337 2881221 : ivas_syn_output( p_output, *nSamplesRendered, nchan_out_syn_output, (int16_t *) data );
1338 :
1339 2881221 : break;
1340 0 : case PCM_FLOAT32:
1341 0 : ivas_buffer_deinterleaved_to_interleaved( p_output, nchan_out_syn_output, *nSamplesRendered, (float *) data );
1342 0 : break;
1343 0 : default:
1344 0 : error = IVAS_ERR_UNKNOWN;
1345 0 : break;
1346 : }
1347 :
1348 2881221 : *nSamplesAvailableNext = st_ivas->hTcBuffer->n_samples_available;
1349 :
1350 2881221 : pop_wmops();
1351 2881221 : return IVAS_ERR_OK;
1352 : }
1353 :
1354 :
1355 : /*--------------------------------------------------------------------------*
1356 : * ivas_jbm_dec_flush_renderer()
1357 : *
1358 : * Flush samples if renderer granularity changes on a bitrate change
1359 : *--------------------------------------------------------------------------*/
1360 :
1361 591 : ivas_error ivas_jbm_dec_flush_renderer(
1362 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
1363 : const int16_t tc_granularity_new, /* i : new renderer granularity */
1364 : const RENDERER_TYPE renderer_type_old, /* i : old renderer type */
1365 : const AUDIO_CONFIG intern_config_old, /* i : old internal config */
1366 : const IVAS_OUTPUT_SETUP_HANDLE hIntSetupOld, /* i : old internal output setup */
1367 : const MC_MODE mc_mode_old, /* i : old MC mode */
1368 : const ISM_MODE ism_mode_old, /* i : old ISM mode */
1369 : uint16_t *nSamplesRendered, /* o : number of samples flushed */
1370 : const PCM_RESOLUTION pcm_resolution, /* i : type for the decoded PCM resolution */
1371 : void *data /* o : output synthesis signal */
1372 : )
1373 : {
1374 : ivas_error error;
1375 : int16_t n_samples_still_available;
1376 : int16_t n_slots_still_available;
1377 : int16_t n_samples_to_render;
1378 : DECODER_TC_BUFFER_HANDLE hTcBuffer;
1379 : float *p_output[MAX_LS_CHANNELS + MAX_NUM_OBJECTS];
1380 :
1381 591 : if ( !st_ivas->hDecoderConfig->Opt_tsm )
1382 : {
1383 0 : return IVAS_ERR_OK;
1384 : }
1385 :
1386 591 : *nSamplesRendered = 0;
1387 591 : hTcBuffer = st_ivas->hTcBuffer;
1388 :
1389 : /* get number of possible slots in new granularity */
1390 591 : n_samples_still_available = hTcBuffer->n_samples_buffered - hTcBuffer->n_samples_rendered;
1391 591 : n_slots_still_available = n_samples_still_available / tc_granularity_new;
1392 591 : *nSamplesRendered = n_slots_still_available * tc_granularity_new;
1393 591 : n_samples_to_render = *nSamplesRendered;
1394 591 : n_samples_still_available -= n_samples_to_render;
1395 591 : assert( n_samples_still_available < tc_granularity_new );
1396 :
1397 : /* update combined orientation access index */
1398 591 : ivas_combined_orientation_set_to_start_index( st_ivas->hCombinedOrientationData );
1399 :
1400 591 : if ( n_slots_still_available )
1401 : {
1402 : int16_t ch_idx;
1403 :
1404 : /* render available full slots (with new lower granularity) */
1405 1770 : for ( ch_idx = 0; ch_idx < max( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full ); ch_idx++ )
1406 : {
1407 : /* move it at the beginning of the TC buffer with zero padding */
1408 1455 : mvr2r( hTcBuffer->tc_buffer_old[ch_idx], hTcBuffer->tc[ch_idx], n_samples_to_render );
1409 1455 : set_zero( hTcBuffer->tc[ch_idx] + n_samples_to_render, hTcBuffer->n_samples_granularity - n_samples_to_render );
1410 : }
1411 :
1412 : /* simple change of the slot info */
1413 315 : hTcBuffer->num_slots = 1;
1414 315 : hTcBuffer->nb_subframes = 1;
1415 315 : hTcBuffer->subframes_rendered = 0;
1416 315 : hTcBuffer->slots_rendered = 0;
1417 315 : hTcBuffer->subframe_nbslots[0] = 1;
1418 315 : hTcBuffer->n_samples_buffered = hTcBuffer->n_samples_granularity + n_samples_still_available;
1419 315 : hTcBuffer->n_samples_available = 0;
1420 315 : hTcBuffer->n_samples_flushed = n_samples_to_render;
1421 315 : hTcBuffer->n_samples_rendered = 0;
1422 :
1423 6615 : for ( ch_idx = 0; ch_idx < MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS; ch_idx++ )
1424 : {
1425 6300 : p_output[ch_idx] = st_ivas->p_output_f[ch_idx];
1426 : }
1427 :
1428 315 : if ( st_ivas->ivas_format == ISM_FORMAT )
1429 : {
1430 237 : if ( ism_mode_old == ISM_MODE_DISC )
1431 : {
1432 : /* Binaural rendering */
1433 237 : if ( renderer_type_old == RENDERER_BINAURAL_OBJECTS_TD )
1434 : {
1435 216 : if ( ( error = ivas_td_binaural_renderer_sf( st_ivas, p_output, hTcBuffer->n_samples_granularity ) ) != IVAS_ERR_OK )
1436 : {
1437 0 : return error;
1438 : }
1439 : }
1440 21 : else if ( renderer_type_old == RENDERER_BINAURAL_MIXER_CONV_ROOM )
1441 : {
1442 : /* Convert to CICPxx; used also for ISM->CICP19->binaural_room rendering */
1443 21 : set_f( st_ivas->hIsmRendererData->interpolator, 1.0f, hTcBuffer->n_samples_granularity );
1444 :
1445 21 : ivas_ism_render_sf( st_ivas, renderer_type_old, p_output, hTcBuffer->n_samples_granularity );
1446 :
1447 21 : if ( ( error = ivas_rend_crendProcessSubframe( st_ivas->hCrendWrapper, IVAS_AUDIO_CONFIG_7_1_4, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR, st_ivas->hDecoderConfig, NULL,
1448 21 : NULL, NULL, st_ivas->hTcBuffer, p_output, p_output, hTcBuffer->n_samples_granularity, st_ivas->hDecoderConfig->output_Fs, 0 ) ) != IVAS_ERR_OK )
1449 : {
1450 0 : return error;
1451 : }
1452 : }
1453 : }
1454 : else
1455 : {
1456 0 : return IVAS_ERROR( IVAS_ERR_WRONG_MODE, "Wrong ISM_MODE in VoIP renderer flushing!" );
1457 : }
1458 : }
1459 78 : else if ( st_ivas->ivas_format == MC_FORMAT )
1460 : {
1461 6 : if ( mc_mode_old == MC_MODE_MCT )
1462 : {
1463 6 : int16_t crendInPlaceRotation = FALSE;
1464 :
1465 6 : if ( st_ivas->transport_config != intern_config_old && ( intern_config_old == IVAS_AUDIO_CONFIG_FOA || intern_config_old == IVAS_AUDIO_CONFIG_HOA2 || intern_config_old == IVAS_AUDIO_CONFIG_HOA3 ) )
1466 : {
1467 3 : if ( ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) < ( hIntSetupOld->nchan_out_woLFE + hIntSetupOld->num_lfe ) )
1468 : {
1469 3 : crendInPlaceRotation = TRUE;
1470 3 : ivas_mc2sba( st_ivas->hTransSetup, hTcBuffer->tc, p_output, hTcBuffer->n_samples_granularity, hIntSetupOld->ambisonics_order, GAIN_LFE );
1471 : }
1472 : }
1473 6 : if ( renderer_type_old == RENDERER_BINAURAL_MIXER_CONV || renderer_type_old == RENDERER_BINAURAL_MIXER_CONV_ROOM )
1474 : {
1475 3 : if ( ( error = ivas_rend_crendProcessSubframe( st_ivas->hCrendWrapper, intern_config_old, st_ivas->hOutSetup.output_config, st_ivas->hDecoderConfig, st_ivas->hCombinedOrientationData,
1476 3 : hIntSetupOld, st_ivas->hEFAPdata, st_ivas->hTcBuffer, crendInPlaceRotation ? p_output : st_ivas->hTcBuffer->tc, p_output, hTcBuffer->n_samples_granularity, st_ivas->hDecoderConfig->output_Fs, 0 ) ) != IVAS_ERR_OK )
1477 : {
1478 0 : return error;
1479 : }
1480 :
1481 3 : ivas_binaural_add_LFE( st_ivas, hTcBuffer->n_samples_granularity, st_ivas->hTcBuffer->tc, p_output );
1482 : }
1483 3 : else if ( renderer_type_old == RENDERER_BINAURAL_OBJECTS_TD )
1484 : {
1485 3 : if ( ( error = ivas_td_binaural_renderer_sf( st_ivas, p_output, hTcBuffer->n_samples_granularity ) ) != IVAS_ERR_OK )
1486 : {
1487 0 : return error;
1488 : }
1489 :
1490 3 : ivas_binaural_add_LFE( st_ivas, hTcBuffer->n_samples_granularity, st_ivas->hTcBuffer->tc, p_output );
1491 : }
1492 : else
1493 : {
1494 0 : return IVAS_ERROR( IVAS_ERR_WRONG_MODE, "Wrong renderer in MCT VoIP renderer flushing!" );
1495 : }
1496 : }
1497 : else
1498 : {
1499 0 : return IVAS_ERROR( IVAS_ERR_WRONG_MODE, "Wrong MC_MODE in VoIP renderer flushing!" );
1500 : }
1501 : }
1502 72 : else if ( st_ivas->ivas_format == MASA_ISM_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
1503 : {
1504 3 : if ( ism_mode_old == ISM_MASA_MODE_DISC )
1505 : {
1506 : float *tc_local[MAX_NUM_OBJECTS];
1507 : int16_t last_dirac_md_idx;
1508 : uint16_t nSamplesAvailableNext;
1509 : ISM_MODE ism_mode_orig;
1510 : RENDERER_TYPE renderer_type_orig;
1511 : int32_t ivas_total_brate;
1512 :
1513 : /* copy from ISM delay buffer to the correct place in TCs */
1514 15 : for ( ch_idx = 0; ch_idx < st_ivas->nchan_ism; ch_idx++ )
1515 : {
1516 12 : tc_local[ch_idx] = &st_ivas->hTcBuffer->tc[ch_idx + 2][hTcBuffer->n_samples_rendered];
1517 12 : mvr2r( st_ivas->hMasaIsmData->delayBuffer[ch_idx], tc_local[ch_idx], st_ivas->hMasaIsmData->delayBuffer_size );
1518 : }
1519 :
1520 : /* to render flushed samples, use configuration from the last received frame */
1521 3 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
1522 3 : renderer_type_orig = st_ivas->renderer_type;
1523 3 : ism_mode_orig = st_ivas->ism_mode;
1524 3 : st_ivas->ism_mode = ism_mode_old;
1525 3 : st_ivas->renderer_type = renderer_type_old;
1526 3 : st_ivas->hDecoderConfig->ivas_total_brate = st_ivas->hDecoderConfig->last_ivas_total_brate;
1527 3 : last_dirac_md_idx = st_ivas->hSpatParamRendCom->render_to_md_map[st_ivas->hSpatParamRendCom->slots_rendered - 1];
1528 :
1529 : /* transfer adapted sf info from hTcBuffer to DirAC */
1530 3 : st_ivas->hSpatParamRendCom->nb_subframes = 1;
1531 3 : st_ivas->hSpatParamRendCom->subframes_rendered = 0;
1532 3 : st_ivas->hSpatParamRendCom->subframe_nbslots[0] = JBM_CLDFB_SLOTS_IN_SUBFRAME;
1533 3 : st_ivas->hSpatParamRendCom->slots_rendered = 0;
1534 3 : st_ivas->hSpatParamRendCom->num_slots = JBM_CLDFB_SLOTS_IN_SUBFRAME;
1535 3 : set_s( st_ivas->hSpatParamRendCom->render_to_md_map, last_dirac_md_idx, n_slots_still_available );
1536 :
1537 3 : if ( ( error = ivas_omasa_dirac_td_binaural_jbm( st_ivas, (uint16_t) hTcBuffer->n_samples_granularity, nSamplesRendered, &nSamplesAvailableNext, CPE_CHANNELS, p_output ) ) != IVAS_ERR_OK )
1538 : {
1539 0 : return error;
1540 : }
1541 :
1542 : /* restore original configuration */
1543 3 : st_ivas->ism_mode = ism_mode_orig;
1544 3 : st_ivas->renderer_type = renderer_type_orig;
1545 3 : st_ivas->hDecoderConfig->ivas_total_brate = ivas_total_brate;
1546 : }
1547 : }
1548 69 : else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
1549 : {
1550 69 : if ( ism_mode_old == ISM_SBA_MODE_DISC )
1551 : {
1552 : float *tc_local[MAX_TRANSPORT_CHANNELS];
1553 : int16_t last_spar_md_idx;
1554 : int16_t last_dirac_md_idx;
1555 : uint16_t nSamplesAvailableNext;
1556 : ISM_MODE ism_mode_orig;
1557 : RENDERER_TYPE renderer_type_orig;
1558 : int32_t ivas_total_brate;
1559 :
1560 : /* to render flushed samples, use configuration from the last received frame */
1561 69 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
1562 69 : renderer_type_orig = st_ivas->renderer_type;
1563 69 : ism_mode_orig = st_ivas->ism_mode;
1564 69 : st_ivas->ism_mode = ism_mode_old;
1565 69 : st_ivas->renderer_type = renderer_type_old;
1566 69 : st_ivas->hDecoderConfig->ivas_total_brate = st_ivas->hDecoderConfig->last_ivas_total_brate;
1567 69 : last_spar_md_idx = st_ivas->hSpar->render_to_md_map[st_ivas->hSpar->slots_rendered - 1];
1568 69 : last_dirac_md_idx = st_ivas->hSpatParamRendCom->render_to_md_map[st_ivas->hSpatParamRendCom->slots_rendered - 1];
1569 : #ifdef DEBUGGING
1570 : assert( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV );
1571 : #endif
1572 :
1573 : /* copy from ISM delay buffer to the correct place in TCs */
1574 207 : for ( ch_idx = 0; ch_idx < st_ivas->nchan_ism; ch_idx++ )
1575 : {
1576 138 : tc_local[ch_idx] = &st_ivas->hTcBuffer->tc[ch_idx][hTcBuffer->n_samples_rendered];
1577 138 : mvr2r( st_ivas->hSbaIsmData->delayBuffer[ch_idx], tc_local[ch_idx], st_ivas->hSbaIsmData->delayBuffer_size );
1578 : }
1579 :
1580 : /* transfer adapted sf info from hTcBuffer to SPAR and DirAC */
1581 69 : st_ivas->hSpar->nb_subframes = 1;
1582 69 : st_ivas->hSpar->subframes_rendered = 0;
1583 69 : st_ivas->hSpar->subframe_nbslots[0] = JBM_CLDFB_SLOTS_IN_SUBFRAME;
1584 69 : st_ivas->hSpar->slots_rendered = 0;
1585 69 : st_ivas->hSpar->num_slots = JBM_CLDFB_SLOTS_IN_SUBFRAME;
1586 69 : st_ivas->hSpatParamRendCom->nb_subframes = 1;
1587 69 : st_ivas->hSpatParamRendCom->subframes_rendered = 0;
1588 69 : st_ivas->hSpatParamRendCom->subframe_nbslots[0] = JBM_CLDFB_SLOTS_IN_SUBFRAME;
1589 69 : st_ivas->hSpatParamRendCom->slots_rendered = 0;
1590 69 : st_ivas->hSpatParamRendCom->num_slots = JBM_CLDFB_SLOTS_IN_SUBFRAME;
1591 :
1592 : /* also adapt md maps, just use the last index */
1593 69 : set_s( st_ivas->hSpar->render_to_md_map, last_spar_md_idx, n_slots_still_available );
1594 69 : set_s( st_ivas->hSpatParamRendCom->render_to_md_map, last_dirac_md_idx, n_slots_still_available );
1595 :
1596 : /* render the last subframe */
1597 69 : if ( ( error = ivas_osba_dirac_td_binaural_jbm( st_ivas, (uint16_t) hTcBuffer->n_samples_granularity, nSamplesRendered, &nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
1598 : {
1599 0 : return error;
1600 : }
1601 :
1602 : /* restore original configuration */
1603 69 : st_ivas->ism_mode = ism_mode_orig;
1604 69 : st_ivas->renderer_type = renderer_type_orig;
1605 69 : st_ivas->hDecoderConfig->ivas_total_brate = ivas_total_brate;
1606 : }
1607 : }
1608 : else
1609 : {
1610 0 : return IVAS_ERROR( IVAS_ERR_WRONG_MODE, "Wrong IVAS format in VoIP renderer flushing!" );
1611 : }
1612 :
1613 315 : hTcBuffer->n_samples_rendered = hTcBuffer->n_samples_granularity;
1614 : }
1615 :
1616 : /* update global combined orientation start index */
1617 591 : ivas_combined_orientation_update_start_index( st_ivas->hCombinedOrientationData, *nSamplesRendered );
1618 :
1619 591 : *nSamplesRendered = n_samples_to_render;
1620 :
1621 : /* Only write out the valid data*/
1622 591 : if ( is_split_rendering_enabled( st_ivas->hDecoderConfig, st_ivas->hRenderConfig ) == 0 )
1623 : {
1624 591 : if ( st_ivas->ivas_format != MONO_FORMAT )
1625 : {
1626 : #ifndef DISABLE_LIMITER
1627 591 : ivas_limiter_dec( st_ivas->hLimiter, p_output, st_ivas->hDecoderConfig->nchan_out, *nSamplesRendered, st_ivas->BER_detect );
1628 : #endif
1629 : }
1630 : }
1631 :
1632 591 : switch ( pcm_resolution )
1633 : {
1634 591 : case PCM_INT16:
1635 : #ifdef DEBUGGING
1636 : st_ivas->noClipping +=
1637 : #endif
1638 591 : ivas_syn_output( p_output, *nSamplesRendered, st_ivas->hDecoderConfig->nchan_out, (int16_t *) data );
1639 591 : break;
1640 0 : case PCM_FLOAT32:
1641 0 : ivas_buffer_deinterleaved_to_interleaved( p_output, st_ivas->hDecoderConfig->nchan_out, *nSamplesRendered, (float *) data );
1642 0 : break;
1643 0 : default:
1644 0 : error = IVAS_ERR_UNKNOWN;
1645 0 : break;
1646 : }
1647 :
1648 591 : return IVAS_ERR_OK;
1649 : }
1650 :
1651 :
1652 : /*--------------------------------------------------------------------------*
1653 : * ivas_jbm_dec_set_discard_samples()
1654 : *
1655 : * Set number of samples to discard in the first subframe if the renderer granularity changes on a bitrate change
1656 : *--------------------------------------------------------------------------*/
1657 :
1658 591 : ivas_error ivas_jbm_dec_set_discard_samples(
1659 : Decoder_Struct *st_ivas /* i/o: main IVAS decoder structre */
1660 : )
1661 : {
1662 : int16_t nMaxSlotsPerSubframe, nSlotsInFirstSubframe;
1663 :
1664 : /* render first frame with front zero padding and discarding those samples */
1665 591 : nMaxSlotsPerSubframe = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / st_ivas->hTcBuffer->n_samples_granularity;
1666 591 : nSlotsInFirstSubframe = nMaxSlotsPerSubframe - st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1];
1667 591 : if ( nSlotsInFirstSubframe > 0 )
1668 : {
1669 318 : st_ivas->hTcBuffer->n_samples_discard = ( nMaxSlotsPerSubframe - nSlotsInFirstSubframe ) * st_ivas->hTcBuffer->n_samples_granularity;
1670 : /* set last subframes number to max to ensure correct continuation */
1671 318 : st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1] = nMaxSlotsPerSubframe;
1672 : }
1673 :
1674 591 : return IVAS_ERR_OK;
1675 : }
1676 :
1677 :
1678 : /*--------------------------------------------------------------------------*
1679 : * ivas_jbm_dec_get_adapted_linear_interpolator()
1680 : *
1681 : * Get an interpolator that is adapted to time scale modified IVAS frame
1682 : *--------------------------------------------------------------------------*/
1683 :
1684 88923 : void ivas_jbm_dec_get_adapted_linear_interpolator(
1685 : const int16_t default_interp_length, /* i : default length of the (full-frame) interpolator */
1686 : const int16_t interp_length, /* i : length of the interpolator to be created */
1687 : float *interpolator /* o : the interpolator */
1688 : )
1689 : {
1690 : int16_t jbm_segment_len, idx;
1691 : float dec;
1692 : #ifdef DEBUGGING
1693 : assert( default_interp_length % 2 == 0 );
1694 : #endif
1695 :
1696 88923 : jbm_segment_len = ( default_interp_length >> 1 );
1697 88923 : dec = 1.0f / default_interp_length;
1698 :
1699 88923 : interpolator[interp_length - 1] = 1.0f;
1700 4755099 : for ( idx = interp_length - 2; idx >= jbm_segment_len; idx-- )
1701 : {
1702 4666176 : interpolator[idx] = max( 0.0f, interpolator[idx + 1] - dec );
1703 : }
1704 :
1705 88923 : if ( interpolator[idx + 1] > 0.0f )
1706 : {
1707 88917 : dec = interpolator[idx + 1] / ( jbm_segment_len + 1 );
1708 4837581 : for ( ; idx >= 0; idx-- )
1709 : {
1710 4748664 : interpolator[idx] = interpolator[idx + 1] - dec;
1711 : }
1712 : }
1713 : else
1714 : {
1715 6 : set_f( interpolator, 0.0f, idx + 1 );
1716 : }
1717 :
1718 88923 : return;
1719 : }
1720 :
1721 :
1722 : /*--------------------------------------------------------------------------*
1723 : * ivas_jbm_dec_get_adapted_subframes()
1724 : *
1725 : * Get an interpolator that is adapted to time scale modified IVAS frame
1726 : *--------------------------------------------------------------------------*/
1727 :
1728 940962 : void ivas_jbm_dec_get_adapted_subframes(
1729 : const int16_t nCldfbTs, /* i : number of time slots in the current frame */
1730 : int16_t *subframe_nbslots, /* i/o: subframe grid */
1731 : int16_t *nb_subframes /* i/o: number of subframes in the frame */
1732 : )
1733 : {
1734 : uint16_t nSlotsInLastSubframe, nSlotsInFirstSubframe;
1735 940962 : uint16_t nCldfbSlotsLocal = nCldfbTs;
1736 :
1737 : /* get last subframe size from previous frame, determine how many slots have to be processed
1738 : in the first subframe (i.e. potential leftover of a 5ms subframe) */
1739 940962 : nSlotsInFirstSubframe = ( PARAM_MC_MAX_NSLOTS_IN_SUBFRAME - subframe_nbslots[*nb_subframes - 1] );
1740 940962 : *nb_subframes = 0;
1741 940962 : if ( nSlotsInFirstSubframe > 0 )
1742 : {
1743 27138 : *nb_subframes = 1;
1744 27138 : nCldfbSlotsLocal -= nSlotsInFirstSubframe;
1745 : }
1746 :
1747 940962 : *nb_subframes += (int16_t) ceilf( (float) nCldfbSlotsLocal / (float) PARAM_MC_MAX_NSLOTS_IN_SUBFRAME );
1748 940962 : nSlotsInLastSubframe = nCldfbSlotsLocal % PARAM_MC_MAX_NSLOTS_IN_SUBFRAME;
1749 :
1750 940962 : set_s( subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
1751 940962 : set_s( subframe_nbslots, PARAM_MC_MAX_NSLOTS_IN_SUBFRAME, *nb_subframes );
1752 :
1753 940962 : if ( nSlotsInFirstSubframe > 0 )
1754 : {
1755 27138 : subframe_nbslots[0] = nSlotsInFirstSubframe;
1756 : }
1757 :
1758 940962 : if ( nSlotsInLastSubframe > 0 )
1759 : {
1760 27186 : subframe_nbslots[*nb_subframes - 1] = nSlotsInLastSubframe;
1761 : }
1762 :
1763 940962 : return;
1764 : }
1765 :
1766 :
1767 : /*--------------------------------------------------------------------------*
1768 : * ivas_jbm_dec_get_adapted_linear_interpolator()
1769 : *
1770 : * Get an meta data map adapted to a time scale modified IVAS frame
1771 : *--------------------------------------------------------------------------*/
1772 :
1773 829650 : void ivas_jbm_dec_get_md_map(
1774 : const int16_t default_len, /* i : default frame length in metadata slots */
1775 : const int16_t len, /* i : length of the modfied frames in metadata slots */
1776 : const int16_t subframe_len, /* i : default length of a subframe */
1777 : const int16_t offset, /* i : current read offset into the md buffer */
1778 : const int16_t buf_len, /* i : length of the metadata buffer */
1779 : int16_t *map /* o : metadata index map */
1780 : )
1781 : {
1782 : int16_t jbm_segment_len, map_idx, src_idx, src_idx_map;
1783 : float dec, src_idx_f;
1784 :
1785 : #ifdef DEBUGGING
1786 : assert( default_len % 2 == 0 );
1787 : #endif
1788 829650 : jbm_segment_len = ( default_len >> 1 );
1789 829650 : dec = 1.0f / default_len;
1790 :
1791 7467384 : for ( map_idx = len - 1, src_idx = default_len - 1; map_idx >= jbm_segment_len; map_idx--, src_idx-- )
1792 : {
1793 6637734 : src_idx_map = max( 0, src_idx / subframe_len );
1794 6637734 : map[map_idx] = ( offset + src_idx_map ) % buf_len;
1795 : }
1796 :
1797 : /* changed part (first segment), interpolate index to parameters
1798 : (we do not want to interpolate and smooth acutal direction/diffuseness values even more) */
1799 829650 : if ( src_idx >= 0 )
1800 : {
1801 829620 : dec = ( (float) ( src_idx + 1 ) ) / ( (float) jbm_segment_len );
1802 829620 : src_idx_f = (float) ( src_idx + 1 ) - dec;
1803 7466580 : for ( ; map_idx >= 0; map_idx-- )
1804 : {
1805 6636960 : src_idx = max( 0, ( (int16_t) round_f( src_idx_f ) ) / subframe_len );
1806 6636960 : map[map_idx] = ( offset + src_idx ) % buf_len;
1807 6636960 : src_idx_f -= dec;
1808 : }
1809 : }
1810 : else
1811 : {
1812 30 : set_s( map, offset, map_idx + 1 );
1813 : }
1814 :
1815 829650 : return;
1816 : }
1817 :
1818 :
1819 : /*--------------------------------------------------------------------------*
1820 : * ivas_jbm_dec_get_md_map_even_spacing()
1821 : *
1822 : * Get an meta data map adapted to a time scale modified IVAS frame. Distribute slots evenly across the modified frame.
1823 : *--------------------------------------------------------------------------*/
1824 :
1825 84543 : void ivas_jbm_dec_get_md_map_even_spacing(
1826 : const int16_t len, /* i : length of the modfied frames in metadata slots */
1827 : const int16_t subframe_len, /* i : default length of a subframe */
1828 : const int16_t offset, /* i : current read offset into the md buffer */
1829 : const int16_t buf_len, /* i : length of the metadata buffer */
1830 : int16_t *map /* o : metadata index map */
1831 : )
1832 : {
1833 : int16_t map_idx, sf_idx, sf_length, increment, subframes_written;
1834 : float decimal, decimal_sum, eps;
1835 : int16_t subframe_map_length[MAX_PARAM_SPATIAL_SUBFRAMES];
1836 :
1837 : /* subframe map length */
1838 84543 : sf_length = len / subframe_len;
1839 84543 : if ( len % subframe_len == 0 )
1840 : {
1841 : /* even subframes */
1842 422580 : for ( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
1843 : {
1844 338064 : subframe_map_length[sf_idx] = sf_length;
1845 : }
1846 : }
1847 : else
1848 : {
1849 : /* uneven subframes */
1850 27 : decimal = ( (float) len / (float) subframe_len ) - (float) sf_length;
1851 27 : decimal_sum = decimal;
1852 27 : eps = 0.001f;
1853 135 : for ( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
1854 : {
1855 108 : increment = (int16_t) floorf( decimal_sum + eps );
1856 108 : subframe_map_length[sf_idx] = sf_length + increment;
1857 108 : if ( increment > 0 )
1858 : {
1859 51 : decimal_sum -= 1.0f;
1860 : }
1861 108 : decimal_sum += decimal;
1862 : }
1863 : }
1864 :
1865 : /* map slots to subframes */
1866 84543 : sf_idx = 0;
1867 84543 : subframes_written = 0;
1868 1437546 : for ( map_idx = 0; map_idx < len; map_idx++ )
1869 : {
1870 1353003 : map[map_idx] = ( offset + sf_idx ) % buf_len;
1871 1353003 : if ( map_idx - subframes_written >= subframe_map_length[sf_idx] - 1 )
1872 : {
1873 338172 : subframes_written += subframe_map_length[sf_idx];
1874 338172 : ++sf_idx;
1875 : }
1876 : }
1877 :
1878 84543 : return;
1879 : }
1880 :
1881 :
1882 : /*--------------------------------------------------------------------------*
1883 : * ivas_jbm_dec_get_num_tc_channels()
1884 : *
1885 : * Get the number of transport channels provided by the JBM transport channel decode function
1886 : *--------------------------------------------------------------------------*/
1887 :
1888 23064 : int16_t ivas_jbm_dec_get_num_tc_channels(
1889 : Decoder_Struct *st_ivas /* i : IVAS decoder handle */
1890 : )
1891 : {
1892 : int16_t num_tc;
1893 : int32_t ivas_total_brate;
1894 : AUDIO_CONFIG output_config;
1895 :
1896 :
1897 23064 : if ( st_ivas->renderer_type == RENDERER_DISABLE )
1898 : {
1899 2811 : num_tc = st_ivas->hDecoderConfig->nchan_out;
1900 : }
1901 : else
1902 : {
1903 20253 : num_tc = st_ivas->nchan_transport;
1904 : }
1905 23064 : output_config = st_ivas->hDecoderConfig->output_config;
1906 :
1907 23064 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
1908 :
1909 23064 : if ( st_ivas->ivas_format == MONO_FORMAT )
1910 : {
1911 9 : num_tc = st_ivas->hDecoderConfig->nchan_out;
1912 : }
1913 23055 : else if ( st_ivas->ivas_format == STEREO_FORMAT && st_ivas->hDecoderConfig->nchan_out == 1 )
1914 : {
1915 66 : num_tc = 1;
1916 : }
1917 22989 : else if ( st_ivas->ivas_format == ISM_FORMAT )
1918 : {
1919 4104 : if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
1920 : {
1921 6 : num_tc = 1;
1922 : }
1923 : }
1924 18885 : else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
1925 : {
1926 9306 : if ( st_ivas->sba_dirac_stereo_flag )
1927 : {
1928 372 : num_tc = CPE_CHANNELS;
1929 : }
1930 8934 : else if ( st_ivas->ivas_format == MASA_FORMAT && ivas_total_brate < MASA_STEREO_MIN_BITRATE && ( ivas_total_brate > IVAS_SID_5k2 || ( ivas_total_brate <= IVAS_SID_5k2 && st_ivas->nCPE > 0 && st_ivas->hCPE[0] != NULL && st_ivas->hCPE[0]->nchan_out == 1 ) ) )
1931 : {
1932 1968 : num_tc = 1; /* Only one channel transported */
1933 : }
1934 :
1935 9306 : if ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL && st_ivas->nchan_transport == 2 && st_ivas->hDecoderConfig->ivas_total_brate < MASA_STEREO_MIN_BITRATE && st_ivas->hDecoderConfig->ivas_total_brate > IVAS_SID_5k2 )
1936 : {
1937 54 : num_tc = CPE_CHANNELS;
1938 : }
1939 9306 : if ( st_ivas->ivas_format == SBA_FORMAT )
1940 : {
1941 :
1942 3792 : if ( num_tc == 3 )
1943 : {
1944 804 : num_tc++;
1945 : }
1946 : }
1947 : }
1948 9579 : else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
1949 : {
1950 3615 : if ( st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_EXTERNAL )
1951 : {
1952 3438 : if ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
1953 : {
1954 2154 : num_tc++;
1955 : }
1956 1284 : else if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
1957 : {
1958 1284 : num_tc += st_ivas->nchan_ism;
1959 : }
1960 : }
1961 : }
1962 5964 : else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
1963 : {
1964 2070 : if ( st_ivas->sba_dirac_stereo_flag )
1965 : {
1966 282 : num_tc = CPE_CHANNELS;
1967 : }
1968 2070 : if ( num_tc == 3 )
1969 : {
1970 486 : num_tc++;
1971 : }
1972 2070 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
1973 : {
1974 822 : num_tc += st_ivas->nchan_ism;
1975 : }
1976 : }
1977 3894 : else if ( st_ivas->ivas_format == MC_FORMAT )
1978 : {
1979 3756 : if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_MONO )
1980 : {
1981 15 : num_tc = 1;
1982 : }
1983 3741 : else if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_STEREO )
1984 : {
1985 78 : num_tc = 2;
1986 : }
1987 3663 : else if ( st_ivas->mc_mode == MC_MODE_MCT )
1988 : {
1989 : /* do all static dmx already in the TC decoder if less channels than transported... */
1990 1788 : if ( st_ivas->transport_config != st_ivas->intern_config && ( st_ivas->intern_config == IVAS_AUDIO_CONFIG_FOA || st_ivas->intern_config == IVAS_AUDIO_CONFIG_HOA2 || st_ivas->intern_config == IVAS_AUDIO_CONFIG_HOA3 ) )
1991 : {
1992 21 : if ( ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) >= ( st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe ) )
1993 : {
1994 0 : num_tc = st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe;
1995 : }
1996 : }
1997 1767 : else if ( ( st_ivas->renderer_type == RENDERER_MC || st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC ) && ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) >= ( st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe ) )
1998 : {
1999 0 : num_tc = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
2000 : }
2001 : }
2002 1875 : else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
2003 : {
2004 33 : num_tc = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
2005 : }
2006 1842 : else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
2007 : {
2008 846 : if ( st_ivas->hOutSetup.separateChannelEnabled )
2009 : {
2010 63 : num_tc++;
2011 : }
2012 846 : if ( st_ivas->hOutSetup.separateChannelEnabled && ( output_config == IVAS_AUDIO_CONFIG_5_1 || output_config == IVAS_AUDIO_CONFIG_7_1 ||
2013 60 : output_config == IVAS_AUDIO_CONFIG_5_1_4 || output_config == IVAS_AUDIO_CONFIG_7_1_4 ||
2014 60 : output_config == IVAS_AUDIO_CONFIG_5_1_2 || ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.num_lfe > 0 ) ) )
2015 : {
2016 : /* LFE is synthesized in TD with the TCs*/
2017 3 : num_tc++;
2018 : }
2019 : }
2020 : }
2021 138 : else if ( st_ivas->ivas_format == MONO_FORMAT && st_ivas->renderer_type == RENDERER_NON_DIEGETIC_DOWNMIX )
2022 : {
2023 0 : num_tc = MAX_OUTPUT_CHANNELS_IN_DIEGETIC_PAN;
2024 : }
2025 :
2026 23064 : return num_tc;
2027 : }
2028 :
2029 :
2030 : /*--------------------------------------------------------------------------*
2031 : * ivas_jbm_dec_get_render_granularity()
2032 : *
2033 : *
2034 : *--------------------------------------------------------------------------*/
2035 :
2036 : /*! r: render granularity */
2037 21603 : int16_t ivas_jbm_dec_get_render_granularity(
2038 : const RENDERER_TYPE renderer_type, /* i : renderer type */
2039 : const RENDERER_TYPE renderer_type_sec, /* i : secondary renderer type */
2040 : const int32_t output_Fs /* i : sampling rate */
2041 : )
2042 : {
2043 : int16_t render_granularity;
2044 :
2045 21603 : if ( renderer_type == RENDERER_BINAURAL_OBJECTS_TD || /* TD renderer */
2046 20493 : renderer_type == RENDERER_BINAURAL_MIXER_CONV || renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM || /* Crend */
2047 : renderer_type_sec == RENDERER_BINAURAL_OBJECTS_TD /* TD rend as a secondary renderer -> set the common granularity for both renderers */
2048 : )
2049 : {
2050 : /* 5 ms granularity */
2051 3588 : render_granularity = NS2SA( output_Fs, FRAME_SIZE_NS / MAX_PARAM_SPATIAL_SUBFRAMES );
2052 : }
2053 : else
2054 : {
2055 : /* 1.25 ms granularity */
2056 18015 : render_granularity = NS2SA( output_Fs, CLDFB_SLOT_NS );
2057 : }
2058 :
2059 21603 : return render_granularity;
2060 : }
2061 :
2062 :
2063 : /*--------------------------------------------------------------------------*
2064 : * ivas_jbm_dec_tc_audio_allocate()
2065 : *
2066 : * allocate and initialize TC audio buffer
2067 : *--------------------------------------------------------------------------*/
2068 :
2069 14961 : static ivas_error ivas_jbm_dec_tc_audio_allocate(
2070 : DECODER_TC_BUFFER_HANDLE hTcBuffer, /* i/o: JBM TSM buffer handle */
2071 : const int32_t output_Fs, /* i : output sampling rate */
2072 : const int16_t Opt_tsm /* i : TSM option flag */
2073 : )
2074 : {
2075 : int16_t nsamp_to_allocate;
2076 : int16_t ch_idx, n_samp_full, n_samp_residual, offset;
2077 :
2078 14961 : if ( Opt_tsm )
2079 : {
2080 3528 : n_samp_full = ( NS2SA( output_Fs, MAX_JBM_L_FRAME_NS ) );
2081 3528 : n_samp_residual = hTcBuffer->n_samples_granularity - 1;
2082 : }
2083 : else
2084 : {
2085 11433 : n_samp_full = (int16_t) ( output_Fs / FRAMES_PER_SEC );
2086 11433 : n_samp_residual = 0;
2087 : }
2088 :
2089 14961 : nsamp_to_allocate = max( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full ) * n_samp_full;
2090 :
2091 14961 : if ( Opt_tsm )
2092 : {
2093 : /* note: this is stack memory buffer for time-scale modified audio signals */
2094 3528 : if ( ( hTcBuffer->tc_buffer = (float *) malloc( nsamp_to_allocate * sizeof( float ) ) ) == NULL )
2095 : {
2096 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for JBM TC Buffer\n" ) );
2097 : }
2098 3528 : set_zero( hTcBuffer->tc_buffer, nsamp_to_allocate );
2099 :
2100 3528 : offset = 0;
2101 15579 : for ( ch_idx = 0; ch_idx < max( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full ); ch_idx++ )
2102 : {
2103 12051 : hTcBuffer->tc[ch_idx] = &hTcBuffer->tc_buffer[offset];
2104 12051 : offset += n_samp_full;
2105 : }
2106 47925 : for ( ; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
2107 : {
2108 44397 : hTcBuffer->tc[ch_idx] = NULL;
2109 : }
2110 :
2111 : /* memory buffer for TC audio samples not rendered in the previous frame */
2112 15579 : for ( ch_idx = 0; ch_idx < hTcBuffer->nchan_transport_internal; ch_idx++ )
2113 : {
2114 12051 : if ( ( hTcBuffer->tc_buffer_old[ch_idx] = (float *) malloc( n_samp_residual * sizeof( float ) ) ) == NULL )
2115 : {
2116 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for JBM TC Buffer\n" ) );
2117 : }
2118 12051 : set_zero( hTcBuffer->tc_buffer_old[ch_idx], n_samp_residual );
2119 : }
2120 47925 : for ( ; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
2121 : {
2122 44397 : hTcBuffer->tc_buffer_old[ch_idx] = NULL;
2123 : }
2124 : }
2125 : else
2126 : {
2127 11433 : hTcBuffer->tc_buffer = NULL;
2128 :
2129 194361 : for ( ch_idx = 0; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
2130 : {
2131 182928 : hTcBuffer->tc[ch_idx] = NULL;
2132 : }
2133 :
2134 194361 : for ( ch_idx = 0; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
2135 : {
2136 182928 : hTcBuffer->tc_buffer_old[ch_idx] = NULL;
2137 : }
2138 : }
2139 :
2140 14961 : return IVAS_ERR_OK;
2141 : }
2142 :
2143 :
2144 : /*--------------------------------------------------------------------------*
2145 : * ivas_jbm_dec_tc_audio_deallocate()
2146 : *
2147 : * deallocate TC audio buffer
2148 : *--------------------------------------------------------------------------*/
2149 :
2150 14961 : static void ivas_jbm_dec_tc_audio_deallocate(
2151 : DECODER_TC_BUFFER_HANDLE hTcBuffer /* i/o: JBM TSM buffer handle */
2152 : )
2153 : {
2154 : int16_t ch_idx;
2155 :
2156 14961 : if ( hTcBuffer != NULL )
2157 : {
2158 14961 : if ( hTcBuffer->tc_buffer != NULL )
2159 : {
2160 59976 : for ( ch_idx = 0; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
2161 : {
2162 56448 : hTcBuffer->tc[ch_idx] = NULL;
2163 : }
2164 :
2165 3528 : free( hTcBuffer->tc_buffer );
2166 3528 : hTcBuffer->tc_buffer = NULL;
2167 : }
2168 :
2169 254337 : for ( ch_idx = 0; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
2170 : {
2171 239376 : if ( hTcBuffer->tc_buffer_old[ch_idx] != NULL )
2172 : {
2173 12051 : free( hTcBuffer->tc_buffer_old[ch_idx] );
2174 12051 : hTcBuffer->tc_buffer_old[ch_idx] = NULL;
2175 : }
2176 : }
2177 : }
2178 :
2179 14961 : return;
2180 : }
2181 :
2182 :
2183 : /*--------------------------------------------------------------------------*
2184 : * ivas_jbm_dec_tc_buffer_open()
2185 : *
2186 : * open and initialize JBM transport channel buffer
2187 : *--------------------------------------------------------------------------*/
2188 :
2189 1881 : ivas_error ivas_jbm_dec_tc_buffer_open(
2190 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
2191 : const TC_BUFFER_MODE tc_buffer_mode, /* i : buffer mode */
2192 : const int16_t nchan_transport_jbm, /* i : number of real transport channels */
2193 : const int16_t nchan_transport_internal, /* i : number of totally buffered channels */
2194 : const int16_t nchan_full, /* i : number of channels to fully store */
2195 : const int16_t n_samples_granularity /* i : granularity of the renderer/buffer */
2196 : )
2197 : {
2198 : DECODER_TC_BUFFER_HANDLE hTcBuffer;
2199 : int16_t nMaxSlotsPerSubframe;
2200 : ivas_error error;
2201 :
2202 : /*-----------------------------------------------------------------*
2203 : * prepare library opening
2204 : *-----------------------------------------------------------------*/
2205 :
2206 1881 : if ( ( hTcBuffer = (DECODER_TC_BUFFER_HANDLE) malloc( sizeof( DECODER_TC_BUFFER ) ) ) == NULL )
2207 : {
2208 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for JBM TC Buffer\n" ) );
2209 : }
2210 :
2211 1881 : hTcBuffer->tc_buffer_mode = tc_buffer_mode;
2212 1881 : hTcBuffer->nchan_transport_jbm = nchan_transport_jbm;
2213 1881 : hTcBuffer->nchan_transport_internal = nchan_transport_internal;
2214 1881 : hTcBuffer->nchan_buffer_full = nchan_full;
2215 1881 : hTcBuffer->n_samples_granularity = n_samples_granularity;
2216 1881 : hTcBuffer->n_samples_available = 0;
2217 1881 : hTcBuffer->n_samples_buffered = 0;
2218 1881 : hTcBuffer->n_samples_rendered = 0;
2219 1881 : hTcBuffer->slots_rendered = 0;
2220 1881 : hTcBuffer->subframes_rendered = 0;
2221 1881 : hTcBuffer->n_samples_discard = 0;
2222 1881 : hTcBuffer->n_samples_flushed = 0;
2223 1881 : hTcBuffer->nb_subframes = MAX_PARAM_SPATIAL_SUBFRAMES;
2224 :
2225 1881 : nMaxSlotsPerSubframe = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / hTcBuffer->n_samples_granularity;
2226 1881 : hTcBuffer->num_slots = nMaxSlotsPerSubframe * MAX_PARAM_SPATIAL_SUBFRAMES;
2227 1881 : set_s( hTcBuffer->subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
2228 1881 : set_s( hTcBuffer->subframe_nbslots, nMaxSlotsPerSubframe, MAX_PARAM_SPATIAL_SUBFRAMES );
2229 :
2230 1881 : if ( ( error = ivas_jbm_dec_tc_audio_allocate( hTcBuffer, st_ivas->hDecoderConfig->output_Fs, st_ivas->hDecoderConfig->Opt_tsm ) ) != IVAS_ERR_OK )
2231 : {
2232 0 : return error;
2233 : }
2234 :
2235 1881 : st_ivas->hTcBuffer = hTcBuffer;
2236 :
2237 1881 : return IVAS_ERR_OK;
2238 : }
2239 :
2240 :
2241 : /*--------------------------------------------------------------------------*
2242 : * ivas_jbm_dec_tc_buffer_reconfigure()
2243 : *
2244 : * open and initialize JBM transport channel buffer
2245 : *--------------------------------------------------------------------------*/
2246 :
2247 13080 : ivas_error ivas_jbm_dec_tc_buffer_reconfigure(
2248 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
2249 : const TC_BUFFER_MODE tc_buffer_mode, /* i : new buffer mode */
2250 : const int16_t nchan_transport_jbm, /* i : new number of real transport channels */
2251 : const int16_t nchan_transport_internal, /* i : new number of totally buffered channels */
2252 : const int16_t nchan_full, /* i : new number of channels to fully store */
2253 : const int16_t n_samples_granularity /* i : new granularity of the renderer/buffer */
2254 : )
2255 : {
2256 : int16_t ch_idx, num_tc_buffer_mem, n_samples_still_available;
2257 : float tc_buffer_mem[MAX_INTERN_CHANNELS][L_FRAME48k / MAX_PARAM_SPATIAL_SUBFRAMES - 1];
2258 : ivas_error error;
2259 : DECODER_TC_BUFFER_HANDLE hTcBuffer;
2260 :
2261 13080 : hTcBuffer = st_ivas->hTcBuffer;
2262 :
2263 13080 : num_tc_buffer_mem = 0;
2264 13080 : n_samples_still_available = 0;
2265 :
2266 13080 : if ( st_ivas->hDecoderConfig->Opt_tsm )
2267 : {
2268 : /* save samples of the TC buffer from the previous frame */
2269 3423 : num_tc_buffer_mem = min( hTcBuffer->nchan_transport_internal, nchan_transport_internal );
2270 3423 : n_samples_still_available = hTcBuffer->n_samples_buffered - hTcBuffer->n_samples_rendered;
2271 :
2272 : /* what is remaining from last frame needs always be smaller than the new granularity */
2273 3423 : assert( n_samples_still_available < n_samples_granularity );
2274 :
2275 11997 : for ( ch_idx = 0; ch_idx < num_tc_buffer_mem; ch_idx++ )
2276 : {
2277 8574 : mvr2r( hTcBuffer->tc_buffer_old[ch_idx] + hTcBuffer->n_samples_flushed, tc_buffer_mem[ch_idx], n_samples_still_available );
2278 : }
2279 : }
2280 :
2281 : /* if granularity changes, adapt subframe_nb_slots */
2282 13080 : if ( n_samples_granularity != hTcBuffer->n_samples_granularity )
2283 : {
2284 : #ifdef DEBUGGING
2285 : int16_t nMaxSlotsPerSubframeOld;
2286 : #endif
2287 : int16_t nMaxSlotsPerSubframeNew;
2288 :
2289 3687 : nMaxSlotsPerSubframeNew = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / n_samples_granularity;
2290 : #ifdef DEBUGGING
2291 : nMaxSlotsPerSubframeOld = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / st_ivas->hTcBuffer->n_samples_granularity;
2292 : assert( hTcBuffer->subframe_nbslots[hTcBuffer->subframes_rendered - 1] == nMaxSlotsPerSubframeOld );
2293 : if ( n_samples_granularity < hTcBuffer->n_samples_granularity )
2294 : {
2295 : assert( ( hTcBuffer->n_samples_granularity % n_samples_granularity ) == 0 );
2296 : }
2297 : else
2298 : {
2299 : assert( ( n_samples_granularity % hTcBuffer->n_samples_granularity ) == 0 );
2300 : }
2301 : #endif
2302 : /* if samples were flushed, take that into account here */
2303 3687 : if ( n_samples_granularity < hTcBuffer->n_samples_granularity && hTcBuffer->n_samples_flushed > 0 )
2304 : {
2305 315 : hTcBuffer->subframe_nbslots[hTcBuffer->subframes_rendered - 1] = hTcBuffer->n_samples_flushed / n_samples_granularity;
2306 315 : hTcBuffer->n_samples_flushed = 0;
2307 : }
2308 : else
2309 : {
2310 3372 : hTcBuffer->subframe_nbslots[hTcBuffer->subframes_rendered - 1] = nMaxSlotsPerSubframeNew;
2311 : }
2312 : }
2313 :
2314 13080 : hTcBuffer->tc_buffer_mode = tc_buffer_mode;
2315 13080 : hTcBuffer->nchan_transport_jbm = nchan_transport_jbm;
2316 13080 : hTcBuffer->nchan_transport_internal = nchan_transport_internal;
2317 13080 : hTcBuffer->nchan_buffer_full = nchan_full;
2318 13080 : hTcBuffer->n_samples_granularity = n_samples_granularity;
2319 :
2320 : /* reallocate TC audio buffers */
2321 :
2322 13080 : ivas_jbm_dec_tc_audio_deallocate( hTcBuffer );
2323 :
2324 13080 : if ( ( error = ivas_jbm_dec_tc_audio_allocate( hTcBuffer, st_ivas->hDecoderConfig->output_Fs, st_ivas->hDecoderConfig->Opt_tsm ) ) != IVAS_ERR_OK )
2325 : {
2326 0 : return error;
2327 : }
2328 :
2329 : /* propagate samples of the TC buffer from the previous frame */
2330 21654 : for ( ch_idx = 0; ch_idx < num_tc_buffer_mem; ch_idx++ )
2331 : {
2332 8574 : mvr2r( tc_buffer_mem[ch_idx], hTcBuffer->tc_buffer_old[ch_idx], n_samples_still_available );
2333 : }
2334 :
2335 13080 : return IVAS_ERR_OK;
2336 : }
2337 :
2338 :
2339 : /*--------------------------------------------------------------------------*
2340 : * ivas_jbm_dec_tc_buffer_playout()
2341 : *
2342 : *
2343 : *--------------------------------------------------------------------------*/
2344 :
2345 796375 : static void ivas_jbm_dec_tc_buffer_playout(
2346 : Decoder_Struct *st_ivas,
2347 : const uint16_t nSamplesAsked,
2348 : uint16_t *nSamplesRendered,
2349 : float *output[] )
2350 : {
2351 : int16_t ch_idx, slot_size, slots_to_render, first_sf, last_sf;
2352 :
2353 796375 : slot_size = st_ivas->hTcBuffer->n_samples_granularity;
2354 :
2355 : /* loop for synthesis, assume we always have to render in multiples of 5ms subframes with spills */
2356 796375 : slots_to_render = min( st_ivas->hTcBuffer->num_slots - st_ivas->hTcBuffer->slots_rendered, nSamplesAsked / slot_size );
2357 796375 : st_ivas->hTcBuffer->slots_rendered += slots_to_render;
2358 796375 : *nSamplesRendered = (uint16_t) slots_to_render * slot_size;
2359 796375 : first_sf = st_ivas->hTcBuffer->subframes_rendered;
2360 796375 : last_sf = first_sf;
2361 :
2362 2344204 : while ( slots_to_render > 0 )
2363 : {
2364 1547829 : slots_to_render -= st_ivas->hTcBuffer->subframe_nbslots[last_sf];
2365 1547829 : last_sf++;
2366 : }
2367 : #ifdef DEBUGGING
2368 : assert( slots_to_render == 0 );
2369 : #endif
2370 :
2371 2252370 : for ( ch_idx = 0; ch_idx < st_ivas->hTcBuffer->nchan_transport_jbm; ch_idx++ )
2372 : {
2373 1455995 : mvr2r( st_ivas->hTcBuffer->tc[ch_idx] + st_ivas->hTcBuffer->n_samples_rendered, output[ch_idx], *nSamplesRendered );
2374 : }
2375 :
2376 796375 : st_ivas->hTcBuffer->subframes_rendered = last_sf;
2377 :
2378 796375 : return;
2379 : }
2380 :
2381 :
2382 : /*--------------------------------------------------------------------------*
2383 : * ivas_jbm_dec_tc_buffer_close()
2384 : *
2385 : * Close JBM transport channel buffer
2386 : *--------------------------------------------------------------------------*/
2387 :
2388 1881 : void ivas_jbm_dec_tc_buffer_close(
2389 : DECODER_TC_BUFFER_HANDLE *phTcBuffer /* i/o: TC buffer handle */
2390 : )
2391 : {
2392 1881 : if ( *phTcBuffer != NULL )
2393 : {
2394 1881 : ivas_jbm_dec_tc_audio_deallocate( *phTcBuffer );
2395 :
2396 1881 : free( *phTcBuffer );
2397 1881 : *phTcBuffer = NULL;
2398 : }
2399 :
2400 1881 : return;
2401 : }
2402 :
2403 :
2404 : /*--------------------------------------------------------------------------*
2405 : * ivas_jbm_dec_td_renderers_adapt_subframes()
2406 : *
2407 : * Close JBM transport channel buffer
2408 : *--------------------------------------------------------------------------*/
2409 :
2410 738300 : void ivas_jbm_dec_td_renderers_adapt_subframes(
2411 : Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
2412 : )
2413 : {
2414 : int16_t nMaxSlotsPerSubframe, nSlotsAvailable;
2415 : uint16_t nSlotsInLastSubframe, nSlotsInFirstSubframe;
2416 :
2417 738300 : nMaxSlotsPerSubframe = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / st_ivas->hTcBuffer->n_samples_granularity;
2418 738300 : nSlotsAvailable = st_ivas->hTcBuffer->n_samples_available / st_ivas->hTcBuffer->n_samples_granularity;
2419 738300 : st_ivas->hTcBuffer->num_slots = nSlotsAvailable;
2420 738300 : st_ivas->hTcBuffer->n_samples_available = nSlotsAvailable * st_ivas->hTcBuffer->n_samples_granularity;
2421 738300 : nSlotsInFirstSubframe = nMaxSlotsPerSubframe - st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1];
2422 738300 : st_ivas->hTcBuffer->nb_subframes = 0;
2423 :
2424 738300 : if ( nSlotsInFirstSubframe > 0 )
2425 : {
2426 16041 : st_ivas->hTcBuffer->nb_subframes = 1;
2427 16041 : nSlotsAvailable -= nSlotsInFirstSubframe;
2428 : }
2429 738300 : st_ivas->hTcBuffer->nb_subframes += (int16_t) ceilf( (float) nSlotsAvailable / (float) nMaxSlotsPerSubframe );
2430 738300 : nSlotsInLastSubframe = nSlotsAvailable % nMaxSlotsPerSubframe;
2431 738300 : set_s( st_ivas->hTcBuffer->subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
2432 738300 : set_s( st_ivas->hTcBuffer->subframe_nbslots, nMaxSlotsPerSubframe, st_ivas->hTcBuffer->nb_subframes );
2433 :
2434 738300 : if ( nSlotsInFirstSubframe > 0 )
2435 : {
2436 16041 : st_ivas->hTcBuffer->subframe_nbslots[0] = nSlotsInFirstSubframe;
2437 : }
2438 :
2439 738300 : if ( nSlotsInLastSubframe > 0 )
2440 : {
2441 16068 : st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1] = nSlotsInLastSubframe;
2442 : }
2443 :
2444 738300 : st_ivas->hTcBuffer->slots_rendered = 0;
2445 738300 : st_ivas->hTcBuffer->subframes_rendered = 0;
2446 :
2447 738300 : return;
2448 : }
2449 :
2450 :
2451 : /*--------------------------------------------------------------------------*
2452 : * ivas_jbm_dec_get_tc_buffer_mode()
2453 : *
2454 : *
2455 : *--------------------------------------------------------------------------*/
2456 :
2457 16515 : TC_BUFFER_MODE ivas_jbm_dec_get_tc_buffer_mode(
2458 : Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
2459 : )
2460 : {
2461 : TC_BUFFER_MODE buffer_mode;
2462 16515 : buffer_mode = TC_BUFFER_MODE_BUFFER;
2463 :
2464 16515 : switch ( st_ivas->renderer_type )
2465 : {
2466 : /* all renderers where we are done after TC decoding (might include DMX to mono/stereo */
2467 2409 : case RENDERER_DISABLE:
2468 : case RENDERER_MCMASA_MONO_STEREO:
2469 : case RENDERER_OSBA_STEREO:
2470 : case RENDERER_MONO_DOWNMIX:
2471 2409 : buffer_mode = TC_BUFFER_MODE_BUFFER;
2472 2409 : break;
2473 12546 : case RENDERER_TD_PANNING:
2474 : case RENDERER_BINAURAL_OBJECTS_TD:
2475 : case RENDERER_BINAURAL_FASTCONV:
2476 : case RENDERER_BINAURAL_FASTCONV_ROOM:
2477 : case RENDERER_BINAURAL_PARAMETRIC:
2478 : case RENDERER_BINAURAL_PARAMETRIC_ROOM:
2479 : case RENDERER_STEREO_PARAMETRIC:
2480 : case RENDERER_DIRAC:
2481 : case RENDERER_PARAM_ISM:
2482 : case RENDERER_BINAURAL_MIXER_CONV:
2483 : case RENDERER_BINAURAL_MIXER_CONV_ROOM:
2484 : case RENDERER_OMASA_OBJECT_EXT:
2485 : case RENDERER_OMASA_MIX_EXT:
2486 : case RENDERER_OSBA_AMBI:
2487 : case RENDERER_OSBA_LS:
2488 12546 : buffer_mode = TC_BUFFER_MODE_RENDERER;
2489 12546 : break;
2490 6 : case RENDERER_NON_DIEGETIC_DOWNMIX:
2491 6 : if ( st_ivas->ivas_format == MONO_FORMAT )
2492 : {
2493 3 : buffer_mode = TC_BUFFER_MODE_BUFFER;
2494 : }
2495 : else
2496 : {
2497 3 : buffer_mode = TC_BUFFER_MODE_RENDERER;
2498 : }
2499 6 : break;
2500 258 : case RENDERER_MC_PARAMMC:
2501 258 : if ( st_ivas->hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
2502 : {
2503 6 : buffer_mode = TC_BUFFER_MODE_BUFFER; /* TCs are already the DMX to mono or stereo */
2504 : }
2505 : else
2506 : {
2507 252 : buffer_mode = TC_BUFFER_MODE_RENDERER;
2508 : }
2509 258 : break;
2510 306 : case RENDERER_MC:
2511 306 : if ( ivas_jbm_dec_get_num_tc_channels( st_ivas ) != st_ivas->hDecoderConfig->nchan_out )
2512 : {
2513 285 : buffer_mode = TC_BUFFER_MODE_RENDERER;
2514 : }
2515 306 : break;
2516 990 : case RENDERER_SBA_LINEAR_ENC:
2517 990 : if ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCT && ( st_ivas->renderer_type == RENDERER_MC || st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC ) && ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) >= ( st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe ) )
2518 : {
2519 0 : buffer_mode = TC_BUFFER_MODE_BUFFER;
2520 : }
2521 : else
2522 : {
2523 990 : buffer_mode = TC_BUFFER_MODE_RENDERER;
2524 : }
2525 990 : break;
2526 0 : case RENDERER_SBA_LINEAR_DEC:
2527 0 : if ( st_ivas->ivas_format == SBA_FORMAT && ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_MONO || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_STEREO ) )
2528 : {
2529 0 : buffer_mode = TC_BUFFER_MODE_BUFFER;
2530 : }
2531 : else
2532 : {
2533 0 : buffer_mode = TC_BUFFER_MODE_RENDERER;
2534 : }
2535 0 : break;
2536 : #ifdef DEBUGGING
2537 : default:
2538 : assert( 0 );
2539 : #endif
2540 : }
2541 :
2542 16515 : return buffer_mode;
2543 : }
2544 :
2545 :
2546 : /*--------------------------------------------------------------------------*
2547 : * ivas_jbm_dec_metadata_open()
2548 : *
2549 : * Open structure for metadata buffering in JBM
2550 : *--------------------------------------------------------------------------*/
2551 :
2552 9 : ivas_error ivas_jbm_dec_metadata_open(
2553 : Decoder_Struct *st_ivas )
2554 : {
2555 : JBM_METADATA_HANDLE hJbmMetadata;
2556 9 : if ( ( hJbmMetadata = (JBM_METADATA_HANDLE) malloc( sizeof( JBM_METADATA ) ) ) == NULL )
2557 : {
2558 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for JBM metadata handle\n" ) );
2559 : }
2560 9 : hJbmMetadata->sf_write_idx = 0;
2561 9 : hJbmMetadata->sf_md_buffer_length = MASA_JBM_RINGBUFFER_FRAMES * MAX_PARAM_SPATIAL_SUBFRAMES;
2562 :
2563 9 : hJbmMetadata->slot_write_idx = 0;
2564 9 : hJbmMetadata->slot_read_idx = 0;
2565 9 : hJbmMetadata->slot_md_buffer_length = MASA_JBM_RINGBUFFER_FRAMES * MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME;
2566 :
2567 9 : st_ivas->hJbmMetadata = hJbmMetadata;
2568 :
2569 9 : return IVAS_ERR_OK;
2570 : }
2571 :
2572 :
2573 : /*--------------------------------------------------------------------------*
2574 : * ivas_jbm_dec_copy_masa_meta_to_buffer()
2575 : *
2576 : * Copy decoded MASA metadata to a ring buffer
2577 : *--------------------------------------------------------------------------*/
2578 :
2579 3930 : static void ivas_jbm_dec_copy_masa_meta_to_buffer(
2580 : Decoder_Struct *st_ivas )
2581 : {
2582 : int16_t sf, dir, band;
2583 : JBM_METADATA_HANDLE hJbmMetadata;
2584 : MASA_DECODER_EXT_OUT_META *extOutMeta;
2585 : int16_t write_idx;
2586 :
2587 3930 : hJbmMetadata = st_ivas->hJbmMetadata;
2588 3930 : extOutMeta = st_ivas->hMasa->data.extOutMeta;
2589 :
2590 19650 : for ( sf = 0; sf < MAX_PARAM_SPATIAL_SUBFRAMES; sf++ )
2591 : {
2592 15720 : write_idx = ( hJbmMetadata->sf_write_idx + sf ) % hJbmMetadata->sf_md_buffer_length;
2593 :
2594 47160 : for ( dir = 0; dir < MASA_MAXIMUM_DIRECTIONS; dir++ )
2595 : {
2596 786000 : for ( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
2597 : {
2598 754560 : hJbmMetadata->directionIndexBuffer[dir][write_idx][band] = extOutMeta->directionIndex[dir][sf][band];
2599 754560 : hJbmMetadata->directToTotalRatioBuffer[dir][write_idx][band] = extOutMeta->directToTotalRatio[dir][sf][band];
2600 754560 : hJbmMetadata->spreadCoherenceBuffer[dir][write_idx][band] = extOutMeta->spreadCoherence[dir][sf][band];
2601 : }
2602 : }
2603 :
2604 393000 : for ( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
2605 : {
2606 377280 : hJbmMetadata->diffuseToTotalRatioBuffer[write_idx][band] = extOutMeta->diffuseToTotalRatio[sf][band];
2607 377280 : hJbmMetadata->surroundCoherenceBuffer[write_idx][band] = extOutMeta->surroundCoherence[sf][band];
2608 : }
2609 :
2610 15720 : hJbmMetadata->numberOfDirections[write_idx] = extOutMeta->descriptiveMeta.numberOfDirections;
2611 : }
2612 :
2613 3930 : return;
2614 : }
2615 :
2616 :
2617 : /*--------------------------------------------------------------------------*
2618 : * ivas_jbm_masa_sf_to_slot_map()
2619 : *
2620 : * Map input MASA metadata subframes to slots in JBM processing
2621 : *--------------------------------------------------------------------------*/
2622 :
2623 3930 : static void ivas_jbm_masa_sf_to_slot_map(
2624 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
2625 : const int16_t nCldfbTs /* i : number of CLDFB time slots */
2626 : )
2627 : {
2628 : int16_t sf_to_slot_map[MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME];
2629 : int16_t num_slots_in_subfr;
2630 : JBM_METADATA_HANDLE hJbmMetadata;
2631 : int16_t slot_idx;
2632 : int16_t write_idx, sf_index;
2633 :
2634 3930 : ivas_jbm_dec_copy_masa_meta_to_buffer( st_ivas );
2635 :
2636 : /* Set values */
2637 3930 : hJbmMetadata = st_ivas->hJbmMetadata;
2638 3930 : num_slots_in_subfr = CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES;
2639 :
2640 : /* Map input subframes to slots */
2641 3930 : ivas_jbm_dec_get_md_map_even_spacing( nCldfbTs, num_slots_in_subfr, 0, MAX_PARAM_SPATIAL_SUBFRAMES, sf_to_slot_map );
2642 :
2643 66870 : for ( slot_idx = 0; slot_idx < nCldfbTs; slot_idx++ )
2644 : {
2645 62940 : write_idx = ( hJbmMetadata->slot_write_idx + slot_idx ) % hJbmMetadata->slot_md_buffer_length;
2646 62940 : sf_index = ( hJbmMetadata->sf_write_idx + sf_to_slot_map[slot_idx] ) % hJbmMetadata->sf_md_buffer_length;
2647 :
2648 62940 : hJbmMetadata->sf_to_slot_map[write_idx] = sf_index;
2649 : }
2650 :
2651 3930 : hJbmMetadata->sf_write_idx = ( hJbmMetadata->sf_write_idx + MAX_PARAM_SPATIAL_SUBFRAMES ) % hJbmMetadata->sf_md_buffer_length;
2652 3930 : hJbmMetadata->slot_write_idx = ( hJbmMetadata->slot_write_idx + nCldfbTs ) % hJbmMetadata->slot_md_buffer_length;
2653 :
2654 3930 : return;
2655 : }
2656 :
2657 :
2658 : /*--------------------------------------------------------------------------*
2659 : * ivas_jbm_masa_sf_to_sf_map()
2660 : *
2661 : * Map input MASA metadata subframes to output subframes in JBM processing
2662 : *--------------------------------------------------------------------------*/
2663 :
2664 3930 : void ivas_jbm_masa_sf_to_sf_map(
2665 : Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
2666 : )
2667 : {
2668 : int16_t sf_to_sf_map[MAX_PARAM_SPATIAL_SUBFRAMES];
2669 : JBM_METADATA_HANDLE hJbmMetadata;
2670 : MASA_DECODER_EXT_OUT_META *extOutMeta;
2671 : int16_t slot_read_idx, sf_read_idx;
2672 : int16_t sf_idx;
2673 : int16_t dir, band;
2674 : uint8_t numberOfDirections;
2675 :
2676 : /* Set values */
2677 3930 : hJbmMetadata = st_ivas->hJbmMetadata;
2678 3930 : extOutMeta = st_ivas->hMasa->data.extOutMeta;
2679 :
2680 : /* Map slots to subframes */
2681 19650 : for ( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
2682 : {
2683 15720 : slot_read_idx = ( hJbmMetadata->slot_read_idx + 2 ) % hJbmMetadata->slot_md_buffer_length; /* Take the latter one of the middle slots of the output subframe */
2684 15720 : sf_to_sf_map[sf_idx] = hJbmMetadata->sf_to_slot_map[slot_read_idx];
2685 15720 : hJbmMetadata->slot_read_idx = ( hJbmMetadata->slot_read_idx + CLDFB_SLOTS_PER_SUBFRAME ) % hJbmMetadata->slot_md_buffer_length;
2686 : }
2687 :
2688 : /* Copy mapped metadata to the EXT meta buffer for writing */
2689 19650 : for ( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
2690 : {
2691 15720 : sf_read_idx = sf_to_sf_map[sf_idx];
2692 :
2693 47160 : for ( dir = 0; dir < MASA_MAXIMUM_DIRECTIONS; dir++ )
2694 : {
2695 786000 : for ( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
2696 : {
2697 754560 : extOutMeta->directionIndex[dir][sf_idx][band] = hJbmMetadata->directionIndexBuffer[dir][sf_read_idx][band];
2698 754560 : extOutMeta->directToTotalRatio[dir][sf_idx][band] = hJbmMetadata->directToTotalRatioBuffer[dir][sf_read_idx][band];
2699 754560 : extOutMeta->spreadCoherence[dir][sf_idx][band] = hJbmMetadata->spreadCoherenceBuffer[dir][sf_read_idx][band];
2700 : }
2701 : }
2702 :
2703 393000 : for ( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
2704 : {
2705 377280 : extOutMeta->diffuseToTotalRatio[sf_idx][band] = hJbmMetadata->diffuseToTotalRatioBuffer[sf_read_idx][band];
2706 377280 : extOutMeta->surroundCoherence[sf_idx][band] = hJbmMetadata->surroundCoherenceBuffer[sf_read_idx][band];
2707 : }
2708 : }
2709 :
2710 : /* Determine the number of directions for the frame to be written */
2711 3930 : numberOfDirections = 0;
2712 16812 : for ( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
2713 : {
2714 13596 : sf_read_idx = sf_to_sf_map[sf_idx];
2715 :
2716 13596 : if ( hJbmMetadata->numberOfDirections[sf_read_idx] == 1 )
2717 : {
2718 714 : numberOfDirections = 1;
2719 714 : break;
2720 : }
2721 : }
2722 :
2723 3930 : extOutMeta->descriptiveMeta.numberOfDirections = numberOfDirections;
2724 :
2725 3930 : return;
2726 : }
2727 :
2728 :
2729 : /*--------------------------------------------------------------------------*
2730 : * ivas_dec_prepare_renderer()
2731 : *
2732 : * prepare IVAS renderer routine
2733 : *--------------------------------------------------------------------------*/
2734 :
2735 1274778 : void ivas_dec_prepare_renderer(
2736 : Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
2737 : )
2738 : {
2739 : int16_t n, n_render_timeslots;
2740 :
2741 1274778 : push_wmops( "ivas_dec_prepare_renderer" );
2742 :
2743 1274778 : n_render_timeslots = st_ivas->hTcBuffer->n_samples_available / st_ivas->hTcBuffer->n_samples_granularity;
2744 :
2745 1274778 : if ( st_ivas->hTcBuffer->tc_buffer_mode == TC_BUFFER_MODE_BUFFER )
2746 : {
2747 385797 : ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
2748 :
2749 385797 : if ( ( st_ivas->ivas_format == MASA_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT ) && st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL && st_ivas->hDecoderConfig->Opt_tsm )
2750 : {
2751 3930 : ivas_jbm_masa_sf_to_slot_map( st_ivas, n_render_timeslots );
2752 : }
2753 :
2754 : /* MASA transport gaining for edited disc OMASA EXT. For ISMs, only metadata is modified */
2755 385797 : if ( st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode == ISM_MASA_MODE_DISC && st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL && st_ivas->hMasaIsmData->masa_gain_is_edited == 1 )
2756 : {
2757 0 : ivas_omasa_gain_masa_tc( st_ivas->hTcBuffer->tc, st_ivas->hMasaIsmData->gain_masa_edited, st_ivas->nchan_ism, st_ivas->hTcBuffer->n_samples_available );
2758 : }
2759 : }
2760 888981 : else if ( st_ivas->ivas_format == STEREO_FORMAT )
2761 : {
2762 0 : ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
2763 : }
2764 888981 : else if ( st_ivas->ivas_format == ISM_FORMAT )
2765 : {
2766 234678 : if ( st_ivas->ism_mode == ISM_MODE_PARAM )
2767 : {
2768 53139 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
2769 : {
2770 32865 : ivas_dirac_dec_set_md_map( st_ivas, n_render_timeslots );
2771 32865 : ivas_param_ism_params_to_masa_param_mapping( st_ivas );
2772 : }
2773 20274 : else if ( st_ivas->renderer_type == RENDERER_PARAM_ISM || st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
2774 : {
2775 20274 : ivas_param_ism_dec_prepare_renderer( st_ivas, n_render_timeslots );
2776 : }
2777 : }
2778 : else /* ISM_MODE_DISC */
2779 : {
2780 181539 : ivas_ism_dec_digest_tc( st_ivas );
2781 : }
2782 : }
2783 654303 : else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
2784 : {
2785 415884 : ivas_sba_dec_digest_tc( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
2786 : }
2787 238419 : else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
2788 : {
2789 100221 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
2790 : {
2791 63246 : ivas_ism_dec_digest_tc( st_ivas );
2792 :
2793 : /* delay the objects here for all renderers where it is needed */
2794 63246 : if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV ||
2795 32412 : st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ||
2796 23412 : st_ivas->renderer_type == RENDERER_OSBA_AMBI ||
2797 17400 : st_ivas->renderer_type == RENDERER_OSBA_LS ||
2798 11400 : st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL ) &&
2799 59046 : ( st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED && st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) )
2800 : {
2801 242538 : for ( n = 0; n < st_ivas->nchan_ism; n++ )
2802 : {
2803 183492 : delay_signal( st_ivas->hTcBuffer->tc[n], st_ivas->hTcBuffer->n_samples_available, st_ivas->hSbaIsmData->delayBuffer[n], st_ivas->hSbaIsmData->delayBuffer_size );
2804 : }
2805 : }
2806 :
2807 63246 : if ( !st_ivas->sba_dirac_stereo_flag )
2808 : {
2809 59046 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV )
2810 : {
2811 30834 : n_render_timeslots *= ( st_ivas->hTcBuffer->n_samples_granularity / st_ivas->hSpatParamRendCom->slot_size );
2812 : }
2813 :
2814 59046 : ivas_sba_dec_digest_tc( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
2815 : }
2816 : }
2817 : else
2818 : {
2819 36975 : ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
2820 :
2821 36975 : ivas_sba_dec_digest_tc( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
2822 : }
2823 : }
2824 138198 : else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
2825 : {
2826 16410 : if ( st_ivas->renderer_type == RENDERER_OMASA_MIX_EXT || st_ivas->renderer_type == RENDERER_OMASA_OBJECT_EXT )
2827 : {
2828 228 : ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
2829 :
2830 228 : if ( st_ivas->hDecoderConfig->Opt_tsm )
2831 : {
2832 0 : ivas_jbm_masa_sf_to_slot_map( st_ivas, n_render_timeslots );
2833 : }
2834 :
2835 : /* MASA transport gaining for edited param_one OMASA EXT. For ISMs, only metadata is modified. */
2836 228 : if ( st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ && st_ivas->hMasaIsmData->masa_gain_is_edited == 1 )
2837 : {
2838 0 : for ( n = 0; n < CPE_CHANNELS; n++ )
2839 : {
2840 0 : v_multc( st_ivas->hTcBuffer->tc[n], st_ivas->hMasaIsmData->gain_masa_edited, st_ivas->hTcBuffer->tc[n], st_ivas->hTcBuffer->n_samples_available );
2841 : }
2842 : }
2843 : }
2844 : else
2845 : {
2846 16182 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC && st_ivas->ism_mode == ISM_MASA_MODE_DISC )
2847 : {
2848 936 : n_render_timeslots *= ( st_ivas->hTcBuffer->n_samples_granularity / st_ivas->hSpatParamRendCom->slot_size );
2849 : }
2850 :
2851 16182 : ivas_sba_dec_digest_tc( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
2852 :
2853 16182 : if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
2854 : {
2855 6390 : ivas_ism_dec_digest_tc( st_ivas );
2856 : }
2857 :
2858 16182 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_DIRAC )
2859 : {
2860 : int16_t nchan_transport_ism;
2861 :
2862 : /* Delay the signal to match CLDFB delay. Delay the whole buffer. */
2863 10200 : nchan_transport_ism = 0;
2864 10200 : if ( ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ ) && st_ivas->renderer_type != RENDERER_BINAURAL_PARAMETRIC )
2865 : {
2866 4227 : nchan_transport_ism = 1;
2867 : }
2868 5973 : else if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
2869 : {
2870 4254 : nchan_transport_ism = st_ivas->nchan_ism;
2871 : }
2872 :
2873 25854 : for ( n = 0; n < nchan_transport_ism; n++ )
2874 : {
2875 15654 : if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC && st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC )
2876 : {
2877 : float gainIsm;
2878 3444 : gainIsm = OMASA_TDREND_MATCHING_GAIN;
2879 :
2880 3444 : if ( st_ivas->hMasaIsmData->ism_gain_is_edited[n] )
2881 : {
2882 2022 : gainIsm *= st_ivas->hMasaIsmData->gain_ism_edited[n];
2883 : }
2884 :
2885 3444 : v_multc( st_ivas->hTcBuffer->tc[CPE_CHANNELS + n], gainIsm, st_ivas->hTcBuffer->tc[CPE_CHANNELS + n], st_ivas->hTcBuffer->n_samples_available );
2886 : }
2887 :
2888 15654 : if ( st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED && st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
2889 : {
2890 15654 : delay_signal( st_ivas->hTcBuffer->tc[CPE_CHANNELS + n], st_ivas->hTcBuffer->n_samples_available, st_ivas->hMasaIsmData->delayBuffer[n], st_ivas->hMasaIsmData->delayBuffer_size );
2891 : }
2892 : }
2893 :
2894 10200 : if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC && st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC )
2895 : {
2896 : int16_t tcBufferSize;
2897 936 : tcBufferSize = st_ivas->hSpatParamRendCom->num_slots * st_ivas->hSpatParamRendCom->slot_size;
2898 :
2899 936 : if ( st_ivas->hMasaIsmData->masa_gain_is_edited )
2900 : {
2901 1728 : for ( n = 0; n < BINAURAL_CHANNELS; n++ )
2902 : {
2903 1152 : v_multc( st_ivas->hTcBuffer->tc[n], st_ivas->hMasaIsmData->gain_masa_edited, st_ivas->hTcBuffer->tc[n], tcBufferSize );
2904 : }
2905 : }
2906 : }
2907 : }
2908 : }
2909 : }
2910 121788 : else if ( st_ivas->ivas_format == MC_FORMAT )
2911 : {
2912 121788 : if ( st_ivas->mc_mode == MC_MODE_MCT )
2913 : {
2914 61785 : ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
2915 : }
2916 60003 : else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
2917 : {
2918 2340 : ivas_mc_paramupmix_dec_digest_tc( st_ivas, (uint8_t) n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
2919 : }
2920 57663 : else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
2921 : {
2922 30699 : ivas_param_mc_dec_prepare_renderer( st_ivas, (uint8_t) n_render_timeslots );
2923 : }
2924 26964 : else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
2925 : {
2926 26964 : ivas_sba_dec_digest_tc( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
2927 : }
2928 : }
2929 :
2930 1274778 : pop_wmops();
2931 1274778 : return;
2932 : }
|