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 7228077 : 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 : float *data /* i : transport channels */
762 : )
763 : {
764 : float data_f[MAX_CLDFB_DIGEST_CHANNELS][MAX_JBM_L_FRAME48k]; /* 'float' buffer for transport channels that will be directly converted with the CLDFB */
765 : float *p_data_f[MAX_CLDFB_DIGEST_CHANNELS];
766 : int16_t n, n_render_timeslots, n_ch_cldfb;
767 : int16_t ch;
768 : DECODER_TC_BUFFER_HANDLE hTcBuffer;
769 :
770 1274778 : hTcBuffer = st_ivas->hTcBuffer;
771 1274778 : n_ch_cldfb = hTcBuffer->nchan_transport_jbm - hTcBuffer->nchan_buffer_full;
772 :
773 1274778 : if ( st_ivas->hDecoderConfig->Opt_tsm )
774 : {
775 : int16_t n_samples_still_available, m;
776 : int16_t n_ch_full_copy;
777 : int16_t n_ch_res_copy;
778 :
779 94932 : for ( n = 0; n < n_ch_cldfb; n++ )
780 : {
781 18336 : p_data_f[n] = &data_f[n][0];
782 : }
783 :
784 76596 : n_samples_still_available = hTcBuffer->n_samples_buffered - hTcBuffer->n_samples_rendered;
785 76596 : hTcBuffer->n_samples_buffered = n_samples_still_available + nSamplesForRendering + hTcBuffer->n_samples_discard;
786 76596 : hTcBuffer->n_samples_available = hTcBuffer->n_samples_granularity * ( hTcBuffer->n_samples_buffered / hTcBuffer->n_samples_granularity );
787 76596 : *nSamplesResidual = hTcBuffer->n_samples_buffered - hTcBuffer->n_samples_available;
788 76596 : n_ch_full_copy = min( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full );
789 76596 : n_ch_res_copy = hTcBuffer->nchan_transport_jbm - hTcBuffer->nchan_buffer_full;
790 :
791 257454 : for ( ch = 0; ch < n_ch_full_copy; ch++ )
792 : {
793 180858 : set_zero( hTcBuffer->tc[ch], hTcBuffer->n_samples_discard );
794 180858 : mvr2r( hTcBuffer->tc[ch] + hTcBuffer->n_samples_rendered, hTcBuffer->tc[ch] + hTcBuffer->n_samples_discard, n_samples_still_available );
795 152134230 : for ( m = 0; m < nSamplesForRendering; m++ )
796 : {
797 151953372 : hTcBuffer->tc[ch][n_samples_still_available + hTcBuffer->n_samples_discard + m] = data[m * hTcBuffer->nchan_transport_jbm + ch];
798 : }
799 : }
800 :
801 76596 : if ( n_ch_res_copy > 0 )
802 : {
803 27489 : for ( ; ch < hTcBuffer->nchan_transport_jbm; ch++ )
804 : {
805 18336 : mvr2r( hTcBuffer->tc[ch], p_data_f[ch], n_samples_still_available );
806 :
807 17536110 : for ( m = 0; m < nSamplesForRendering; m++ )
808 : {
809 17517774 : p_data_f[ch][n_samples_still_available + m] = data[m * hTcBuffer->nchan_transport_jbm + ch];
810 : }
811 18336 : mvr2r( p_data_f[ch] + hTcBuffer->n_samples_available, hTcBuffer->tc[ch], *nSamplesResidual );
812 : }
813 : }
814 :
815 76596 : n_render_timeslots = hTcBuffer->n_samples_available / hTcBuffer->n_samples_granularity;
816 : }
817 : else
818 : {
819 1282062 : for ( n = 0; n < n_ch_cldfb; n++ )
820 : {
821 83880 : p_data_f[n] = &st_ivas->p_output_f[n][0];
822 : }
823 :
824 8745372 : for ( n = 0; n < ivas_get_nchan_buffers_dec( st_ivas, st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate ); n++ )
825 : {
826 7547190 : hTcBuffer->tc[n] = st_ivas->p_output_f[n]; /* note: buffers needed in the TD decorellator */
827 : }
828 :
829 1198182 : hTcBuffer->n_samples_buffered = nSamplesForRendering;
830 1198182 : hTcBuffer->n_samples_available = hTcBuffer->n_samples_buffered;
831 1198182 : *nSamplesResidual = 0;
832 :
833 1198182 : n_render_timeslots = DEFAULT_JBM_CLDFB_TIMESLOTS;
834 : }
835 :
836 : /* CLDFB analysis for ParamMC/ParamISM */
837 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 ) )
838 : {
839 20274 : ivas_param_ism_dec_digest_tc( st_ivas, n_render_timeslots, p_data_f );
840 : }
841 1254504 : else if ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_PARAMMC && hTcBuffer->tc_buffer_mode == TC_BUFFER_MODE_RENDERER )
842 : {
843 30699 : ivas_param_mc_dec_digest_tc( st_ivas, (uint8_t) n_render_timeslots, p_data_f );
844 : }
845 :
846 1274778 : hTcBuffer->n_samples_rendered = 0;
847 1274778 : hTcBuffer->subframes_rendered = 0;
848 :
849 1274778 : return;
850 : }
851 :
852 :
853 : /*--------------------------------------------------------------------------*
854 : * ivas_dec_render()
855 : *
856 : * Principal IVAS JBM rendering routine
857 : *--------------------------------------------------------------------------*/
858 :
859 2881221 : ivas_error ivas_jbm_dec_render(
860 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
861 : const uint16_t nSamplesAsked, /* i : number of samples wanted */
862 : uint16_t *nSamplesRendered, /* o : number of samples rendered */
863 : uint16_t *nSamplesAvailableNext, /* o : number of samples still available in the rendering pipeline */
864 : const PCM_RESOLUTION pcm_resolution, /* i : type for the decoded PCM resolution */
865 : void *data /* o : output synthesis signal */
866 : )
867 : {
868 : int16_t n, nchan_out;
869 : int16_t nchan_transport;
870 : int16_t nchan_remapped;
871 : int32_t output_Fs;
872 : AUDIO_CONFIG output_config;
873 : int16_t nSamplesAskedLocal;
874 : ivas_error error;
875 : float *p_output[MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS];
876 : float *p_tc[MAX_TRANSPORT_CHANNELS + MAX_NUM_OBJECTS];
877 : int16_t nchan_out_syn_output;
878 :
879 2881221 : push_wmops( "ivas_dec_render" );
880 : /*----------------------------------------------------------------*
881 : * Initialization of local vars after struct has been set
882 : *----------------------------------------------------------------*/
883 :
884 2881221 : output_Fs = st_ivas->hDecoderConfig->output_Fs;
885 2881221 : nchan_out = st_ivas->hDecoderConfig->nchan_out;
886 2881221 : nchan_transport = st_ivas->hTcBuffer->nchan_transport_jbm;
887 2881221 : output_config = st_ivas->hDecoderConfig->output_config;
888 2881221 : nSamplesAskedLocal = nSamplesAsked + st_ivas->hTcBuffer->n_samples_discard;
889 :
890 60505641 : for ( n = 0; n < MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS; n++ )
891 : {
892 57624420 : p_output[n] = st_ivas->p_output_f[n];
893 : }
894 :
895 2881221 : if ( !st_ivas->hDecoderConfig->Opt_tsm )
896 : {
897 45325638 : for ( n = 0; n < MAX_TRANSPORT_CHANNELS + MAX_NUM_OBJECTS; n++ )
898 : {
899 42659424 : st_ivas->hTcBuffer->tc[n] = p_output[n];
900 : }
901 : }
902 :
903 12733736 : for ( n = 0; n < st_ivas->hTcBuffer->nchan_buffer_full; n++ )
904 : {
905 9852515 : p_tc[n] = &st_ivas->hTcBuffer->tc[n][st_ivas->hTcBuffer->n_samples_rendered];
906 : }
907 :
908 : /*----------------------------------------------------------------*
909 : * Update combined orientation access index
910 : *----------------------------------------------------------------*/
911 :
912 2881221 : if ( st_ivas->hCombinedOrientationData != NULL )
913 : {
914 : /* take the discard samples into account here to make sure head rotation stays on the correct 5ms grid */
915 488976 : st_ivas->hCombinedOrientationData->cur_subframe_samples_rendered_start -= st_ivas->hTcBuffer->n_samples_discard;
916 :
917 488976 : ivas_combined_orientation_set_to_start_index( st_ivas->hCombinedOrientationData );
918 : }
919 :
920 : /*----------------------------------------------------------------*
921 : * Rendering
922 : *----------------------------------------------------------------*/
923 :
924 2881221 : if ( st_ivas->ivas_format == UNDEFINED_FORMAT )
925 : {
926 0 : assert( 0 );
927 : }
928 2881221 : else if ( st_ivas->hTcBuffer->tc_buffer_mode == TC_BUFFER_MODE_BUFFER )
929 : {
930 796147 : ivas_jbm_dec_tc_buffer_playout( st_ivas, nSamplesAskedLocal, nSamplesRendered, p_output );
931 : }
932 2085074 : else if ( st_ivas->ivas_format == STEREO_FORMAT )
933 : {
934 : /* Rendering */
935 0 : if ( st_ivas->renderer_type == RENDERER_MC )
936 : {
937 0 : *nSamplesRendered = min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
938 0 : ivas_ls_setup_conversion( st_ivas, st_ivas->nchan_transport, *nSamplesRendered, p_tc, p_output );
939 : }
940 : }
941 2085074 : else if ( st_ivas->ivas_format == ISM_FORMAT )
942 : {
943 : /* Rendering */
944 563750 : if ( st_ivas->ism_mode == ISM_MODE_PARAM )
945 : {
946 133711 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
947 : {
948 80350 : ivas_dirac_dec_binaural_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, st_ivas->nchan_transport, p_output );
949 : }
950 53361 : else if ( st_ivas->renderer_type == RENDERER_PARAM_ISM || st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
951 : {
952 53361 : ivas_param_ism_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output );
953 :
954 53361 : if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
955 : {
956 : /* Convert CICP19 -> Ambisonics */
957 41232 : ivas_mc2sba( st_ivas->hIntSetup, p_output, p_output, *nSamplesRendered, st_ivas->hOutSetup.ambisonics_order, 0.f );
958 : }
959 : }
960 : }
961 : else /* ISM_MODE_DISC */
962 : {
963 430039 : *nSamplesRendered = min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
964 :
965 : /* Loudspeaker or Ambisonics rendering */
966 430039 : if ( st_ivas->renderer_type == RENDERER_TD_PANNING || st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM )
967 : {
968 : /* Convert to CICPxx; used also for ISM->CICP19->binaural_room rendering */
969 147604 : ivas_ism_render_sf( st_ivas, st_ivas->renderer_type, p_output, *nSamplesRendered );
970 : }
971 282435 : else if ( st_ivas->renderer_type == RENDERER_NON_DIEGETIC_DOWNMIX )
972 : {
973 10500 : ivas_apply_non_diegetic_panning( p_tc[0], p_output, st_ivas->hDecoderConfig->non_diegetic_pan_gain, *nSamplesRendered );
974 : }
975 : #ifdef DEBUGGING
976 : else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV )
977 : #else
978 271935 : else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
979 : #endif
980 : {
981 : /* Convert to Ambisonics */
982 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 );
983 : }
984 :
985 : /* Binaural rendering */
986 430039 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_OBJECTS_TD )
987 : {
988 222806 : if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
989 : {
990 0 : if ( ( error = ivas_td_binaural_renderer_sf_splitBinaural( st_ivas, p_output, *nSamplesRendered ) ) != IVAS_ERR_OK )
991 : {
992 0 : return error;
993 : }
994 : }
995 : else
996 : {
997 222806 : if ( ( error = ivas_td_binaural_renderer_sf( st_ivas, p_output, *nSamplesRendered ) ) != IVAS_ERR_OK )
998 : {
999 0 : return error;
1000 : }
1001 : }
1002 : }
1003 207233 : else if ( st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM )
1004 : {
1005 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,
1006 76904 : NULL, NULL, st_ivas->hTcBuffer, p_output, p_output, *nSamplesRendered, output_Fs, 0 ) ) != IVAS_ERR_OK )
1007 : {
1008 0 : return error;
1009 : }
1010 : }
1011 : #ifdef DEBUGGING
1012 : else if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
1013 : {
1014 : ivas_binaural_cldfb_sf( st_ivas, *nSamplesRendered, st_ivas->hTcBuffer->nb_subframes, p_output );
1015 : }
1016 : #endif
1017 : }
1018 : }
1019 1521324 : else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
1020 : {
1021 968792 : nchan_remapped = nchan_transport;
1022 :
1023 : /* Loudspeakers, Ambisonics or Binaural rendering */
1024 968792 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
1025 : {
1026 308438 : ivas_dirac_dec_binaural_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output );
1027 : }
1028 660354 : else if ( st_ivas->ivas_format == MASA_FORMAT )
1029 : {
1030 99041 : if ( st_ivas->renderer_type == RENDERER_DIRAC )
1031 : {
1032 99041 : ivas_dirac_dec_render( st_ivas, nchan_remapped, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output );
1033 : }
1034 : }
1035 : else
1036 : {
1037 561313 : if ( ( error = ivas_sba_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
1038 : {
1039 0 : return error;
1040 : }
1041 : }
1042 : }
1043 552532 : else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
1044 : {
1045 39965 : nchan_remapped = st_ivas->nchan_transport;
1046 :
1047 39965 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
1048 : {
1049 20892 : if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC && st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC )
1050 : {
1051 2305 : if ( ( error = ivas_omasa_dirac_td_binaural_jbm( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output ) ) != IVAS_ERR_OK )
1052 : {
1053 0 : return error;
1054 : }
1055 : }
1056 : else
1057 : {
1058 18587 : ivas_dirac_dec_binaural_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output );
1059 : }
1060 : }
1061 19073 : else if ( st_ivas->renderer_type == RENDERER_DIRAC )
1062 : {
1063 18845 : ivas_omasa_dirac_rend_jbm( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output );
1064 : }
1065 228 : else if ( st_ivas->renderer_type == RENDERER_OMASA_OBJECT_EXT || st_ivas->renderer_type == RENDERER_OMASA_MIX_EXT )
1066 : {
1067 228 : ivas_jbm_dec_tc_buffer_playout( st_ivas, nSamplesAskedLocal, nSamplesRendered, p_output );
1068 228 : ivas_omasa_rearrange_channels( p_output, st_ivas->nchan_ism, *nSamplesRendered );
1069 : }
1070 : }
1071 512567 : else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
1072 : {
1073 226741 : nchan_remapped = nchan_transport;
1074 :
1075 : /* Loudspeakers, Ambisonics or Binaural rendering */
1076 226741 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
1077 : {
1078 141174 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV )
1079 : {
1080 72316 : if ( ( error = ivas_osba_dirac_td_binaural_jbm( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
1081 : {
1082 0 : return error;
1083 : }
1084 : }
1085 68858 : else if ( st_ivas->renderer_type == RENDERER_OSBA_STEREO )
1086 : {
1087 9800 : *nSamplesRendered = min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
1088 :
1089 : /* shift SBA channels to avoid overwrite by ISM upmix in 1 object case and non-TSM unified channel memory*/
1090 9800 : if ( st_ivas->nchan_ism == 1 && st_ivas->hDecoderConfig->Opt_tsm == 0 )
1091 : {
1092 0 : mvr2r( p_tc[2], p_output[3], *nSamplesRendered );
1093 0 : mvr2r( p_tc[1], p_output[2], *nSamplesRendered );
1094 0 : p_tc[1] = p_output[2];
1095 0 : p_tc[2] = p_output[3];
1096 : }
1097 :
1098 : /* render objects */
1099 9800 : ivas_ism_render_sf( st_ivas, st_ivas->renderer_type, p_output, *nSamplesRendered );
1100 :
1101 : /* add already rendered SBA part */
1102 9800 : ivas_osba_stereo_add_channels( p_tc, p_output, st_ivas->hSbaIsmData->gain_bed, nchan_out, st_ivas->nchan_ism, *nSamplesRendered );
1103 : }
1104 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 )
1105 : {
1106 51858 : if ( ( error = ivas_osba_render_sf( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
1107 : {
1108 0 : return error;
1109 : }
1110 : }
1111 7200 : else if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL ) /*EXT output = individual objects + HOA3*/
1112 : {
1113 7200 : if ( ( error = ivas_sba_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, &p_output[st_ivas->nchan_ism] ) ) != IVAS_ERR_OK )
1114 : {
1115 0 : return error;
1116 : }
1117 :
1118 27000 : for ( n = 0; n < st_ivas->nchan_ism; n++ )
1119 : {
1120 19800 : mvr2r( p_tc[n], p_output[n], *nSamplesRendered );
1121 : }
1122 : }
1123 : else
1124 : {
1125 0 : if ( ( error = ivas_sba_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
1126 : {
1127 0 : return error;
1128 : }
1129 : }
1130 : }
1131 85567 : else if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
1132 : {
1133 59257 : ivas_dirac_dec_binaural_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output );
1134 : }
1135 : else
1136 : {
1137 26310 : if ( ( error = ivas_sba_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
1138 : {
1139 0 : return error;
1140 : }
1141 :
1142 26310 : if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
1143 : {
1144 : #ifdef DEBUGGING
1145 : assert( st_ivas->ism_mode == ISM_MODE_NONE );
1146 : #endif
1147 30600 : for ( n = st_ivas->hIntSetup.nchan_out_woLFE - 1; n >= 0; n-- )
1148 : {
1149 28800 : mvr2r( p_output[n], p_output[n + st_ivas->nchan_ism], *nSamplesRendered );
1150 : }
1151 9000 : for ( n = 0; n < st_ivas->nchan_ism; n++ )
1152 : {
1153 7200 : set_zero( p_output[n], *nSamplesRendered );
1154 : }
1155 : }
1156 : }
1157 : }
1158 285826 : else if ( st_ivas->ivas_format == MC_FORMAT )
1159 : {
1160 285826 : if ( st_ivas->mc_mode == MC_MODE_MCT )
1161 : {
1162 147003 : int16_t crendInPlaceRotation = FALSE;
1163 147003 : *nSamplesRendered = min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
1164 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 ) )
1165 : {
1166 1484 : if ( ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) < ( st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe ) )
1167 : {
1168 1484 : crendInPlaceRotation = TRUE;
1169 1484 : ivas_mc2sba( st_ivas->hTransSetup, p_tc, p_output, *nSamplesRendered, st_ivas->hIntSetup.ambisonics_order, GAIN_LFE );
1170 : }
1171 : }
1172 :
1173 : /* Rendering */
1174 147003 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV || st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM )
1175 : {
1176 94626 : if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
1177 : {
1178 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,
1179 0 : &st_ivas->hIntSetup, st_ivas->hEFAPdata, st_ivas->hTcBuffer, crendInPlaceRotation ? p_output : p_tc, p_output, *nSamplesRendered, output_Fs ) ) != IVAS_ERR_OK )
1180 : {
1181 0 : return error;
1182 : }
1183 : }
1184 : else
1185 : {
1186 189252 : if ( ( error = ivas_rend_crendProcessSubframe( st_ivas->hCrendWrapper, st_ivas->intern_config, st_ivas->hOutSetup.output_config, st_ivas->hDecoderConfig, st_ivas->hCombinedOrientationData,
1187 189252 : &st_ivas->hIntSetup, st_ivas->hEFAPdata, st_ivas->hTcBuffer, crendInPlaceRotation ? p_output : p_tc, p_output, *nSamplesRendered, output_Fs, 0 ) ) != IVAS_ERR_OK )
1188 : {
1189 0 : return error;
1190 : }
1191 :
1192 94626 : ivas_binaural_add_LFE( st_ivas, *nSamplesRendered, p_tc, p_output );
1193 : }
1194 : }
1195 52377 : else if ( st_ivas->renderer_type == RENDERER_MC )
1196 : {
1197 27065 : *nSamplesRendered = min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
1198 27065 : ivas_ls_setup_conversion( st_ivas, st_ivas->nchan_transport, *nSamplesRendered, p_tc, p_output );
1199 : }
1200 25312 : else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
1201 : {
1202 3430 : ivas_mc2sba( st_ivas->hIntSetup, p_tc, p_output, *nSamplesRendered, st_ivas->hOutSetup.ambisonics_order, 0.f );
1203 : }
1204 21882 : else if ( st_ivas->renderer_type == RENDERER_BINAURAL_OBJECTS_TD )
1205 : {
1206 21882 : if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
1207 : {
1208 0 : if ( ( error = ivas_td_binaural_renderer_sf_splitBinaural( st_ivas, p_output, *nSamplesRendered ) ) != IVAS_ERR_OK )
1209 : {
1210 0 : return error;
1211 : }
1212 : }
1213 : else
1214 : {
1215 21882 : if ( ( error = ivas_td_binaural_renderer_sf( st_ivas, p_output, *nSamplesRendered ) ) != IVAS_ERR_OK )
1216 : {
1217 0 : return error;
1218 : }
1219 :
1220 21882 : ivas_binaural_add_LFE( st_ivas, *nSamplesRendered, p_tc, p_output );
1221 : }
1222 : }
1223 : }
1224 138823 : else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
1225 : {
1226 5420 : ivas_mc_paramupmix_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_tc, p_output );
1227 :
1228 :
1229 : /* Rendering */
1230 5420 : if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ) && !st_ivas->hDecoderConfig->Opt_Headrotation )
1231 : {
1232 : /* handled in CLDFB domain already */
1233 1120 : if ( output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED && output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
1234 : {
1235 1120 : ivas_binaural_add_LFE( st_ivas, *nSamplesRendered, p_output, p_output );
1236 : }
1237 : }
1238 4300 : else if ( st_ivas->renderer_type == RENDERER_MC )
1239 : {
1240 0 : ivas_ls_setup_conversion( st_ivas, MC_PARAMUPMIX_MAX_INPUT_CHANS, *nSamplesRendered, p_output, p_output );
1241 : }
1242 4300 : else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
1243 : {
1244 70 : ivas_mc2sba( st_ivas->hIntSetup, p_output, p_output, *nSamplesRendered, st_ivas->hOutSetup.ambisonics_order, 0.f );
1245 : }
1246 : }
1247 133403 : else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
1248 : {
1249 71164 : ivas_param_mc_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output );
1250 : }
1251 62239 : else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
1252 : {
1253 62239 : nchan_remapped = st_ivas->nchan_transport;
1254 62239 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
1255 : {
1256 50046 : ivas_dirac_dec_binaural_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output );
1257 : }
1258 12193 : else if ( st_ivas->renderer_type == RENDERER_DIRAC || st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC ) /* rendering to CICPxx and Ambisonics */
1259 : {
1260 12193 : ivas_dirac_dec_render( st_ivas, nchan_remapped, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output );
1261 :
1262 12193 : if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
1263 : {
1264 : /* we still need to copy the separate channel if available */
1265 2030 : if ( st_ivas->hOutSetup.separateChannelEnabled )
1266 : {
1267 175 : mvr2r( p_tc[LFE_CHANNEL - 1], p_output[st_ivas->hOutSetup.separateChannelIndex], *nSamplesRendered );
1268 : }
1269 :
1270 2030 : ivas_mc2sba( st_ivas->hIntSetup, p_output, p_output, *nSamplesRendered, st_ivas->hOutSetup.ambisonics_order, 0.f );
1271 : }
1272 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 ) )
1273 : {
1274 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++ )
1275 : {
1276 0 : set_zero( p_output[n], *nSamplesRendered );
1277 : }
1278 : }
1279 : }
1280 :
1281 : /* copy discrete C and TD LFE from internal TC to output */
1282 62239 : if ( st_ivas->hOutSetup.separateChannelEnabled )
1283 : {
1284 1828 : if ( output_config == IVAS_AUDIO_CONFIG_5_1 || output_config == IVAS_AUDIO_CONFIG_7_1 ||
1285 785 : output_config == IVAS_AUDIO_CONFIG_5_1_4 || output_config == IVAS_AUDIO_CONFIG_7_1_4 ||
1286 785 : output_config == IVAS_AUDIO_CONFIG_5_1_2 || ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.num_lfe > 0 ) )
1287 : {
1288 1043 : mvr2r( p_tc[LFE_CHANNEL], p_output[LFE_CHANNEL], *nSamplesRendered );
1289 1043 : mvr2r( p_tc[LFE_CHANNEL - 1], p_output[st_ivas->hOutSetup.separateChannelIndex], *nSamplesRendered );
1290 : }
1291 785 : else if ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.num_lfe == 0 )
1292 : {
1293 0 : mvr2r( p_tc[LFE_CHANNEL - 1], p_output[st_ivas->hOutSetup.separateChannelIndex], *nSamplesRendered );
1294 : }
1295 : }
1296 : }
1297 : }
1298 :
1299 : /*----------------------------------------------------------------*
1300 : * Write IVAS output channels
1301 : * - compensation for saturation
1302 : * - float to integer conversion
1303 : *----------------------------------------------------------------*/
1304 :
1305 2881221 : st_ivas->hTcBuffer->n_samples_available -= *nSamplesRendered;
1306 2881221 : st_ivas->hTcBuffer->n_samples_rendered += *nSamplesRendered;
1307 :
1308 : /* update global combined orientation start index */
1309 2881221 : ivas_combined_orientation_update_start_index( st_ivas->hCombinedOrientationData, *nSamplesRendered );
1310 :
1311 2881221 : if ( st_ivas->hTcBuffer->n_samples_discard > 0 )
1312 : {
1313 2688 : 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++ )
1314 : {
1315 2370 : p_output[n] += st_ivas->hTcBuffer->n_samples_discard;
1316 : }
1317 318 : *nSamplesRendered -= st_ivas->hTcBuffer->n_samples_discard;
1318 318 : st_ivas->hTcBuffer->n_samples_discard = 0;
1319 : }
1320 :
1321 2881221 : if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
1322 : {
1323 0 : nchan_out_syn_output = BINAURAL_CHANNELS * st_ivas->hSplitBinRend->splitrend.multiBinPoseData.num_poses;
1324 : }
1325 : else
1326 : {
1327 2881221 : nchan_out_syn_output = nchan_out;
1328 : }
1329 :
1330 2881221 : if ( is_split_rendering_enabled( st_ivas->hDecoderConfig, st_ivas->hRenderConfig ) == 0 )
1331 : {
1332 2881221 : if ( st_ivas->ivas_format != MONO_FORMAT )
1333 : {
1334 : #ifndef DISABLE_LIMITER
1335 2859521 : ivas_limiter_dec( st_ivas->hLimiter, p_output, nchan_out, *nSamplesRendered, st_ivas->BER_detect );
1336 : #endif
1337 : }
1338 : }
1339 :
1340 2881221 : switch ( pcm_resolution )
1341 : {
1342 2881221 : case PCM_INT16:
1343 : #ifdef DEBUGGING
1344 : st_ivas->noClipping +=
1345 : #endif
1346 2881221 : ivas_syn_output( p_output, *nSamplesRendered, nchan_out_syn_output, (int16_t *) data );
1347 :
1348 2881221 : break;
1349 0 : case PCM_FLOAT32:
1350 0 : ivas_syn_output_f( p_output, *nSamplesRendered, nchan_out_syn_output, (float *) data );
1351 0 : break;
1352 0 : default:
1353 0 : error = IVAS_ERR_UNKNOWN;
1354 0 : break;
1355 : }
1356 :
1357 2881221 : *nSamplesAvailableNext = st_ivas->hTcBuffer->n_samples_available;
1358 :
1359 2881221 : pop_wmops();
1360 2881221 : return IVAS_ERR_OK;
1361 : }
1362 :
1363 :
1364 : /*--------------------------------------------------------------------------*
1365 : * ivas_jbm_dec_flush_renderer()
1366 : *
1367 : * Flush samples if renderer granularity changes on a bitrate change
1368 : *--------------------------------------------------------------------------*/
1369 :
1370 591 : ivas_error ivas_jbm_dec_flush_renderer(
1371 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
1372 : const int16_t tc_granularity_new, /* i : new renderer granularity */
1373 : const RENDERER_TYPE renderer_type_old, /* i : old renderer type */
1374 : const AUDIO_CONFIG intern_config_old, /* i : old internal config */
1375 : const IVAS_OUTPUT_SETUP_HANDLE hIntSetupOld, /* i : old internal output setup */
1376 : const MC_MODE mc_mode_old, /* i : old MC mode */
1377 : const ISM_MODE ism_mode_old, /* i : old ISM mode */
1378 : uint16_t *nSamplesRendered, /* o : number of samples flushed */
1379 : const PCM_RESOLUTION pcm_resolution, /* i : type for the decoded PCM resolution */
1380 : void *data /* o : output synthesis signal */
1381 : )
1382 : {
1383 : ivas_error error;
1384 : int16_t n_samples_still_available;
1385 : int16_t n_slots_still_available;
1386 : int16_t n_samples_to_render;
1387 : DECODER_TC_BUFFER_HANDLE hTcBuffer;
1388 : float *p_output[MAX_CICP_CHANNELS + MAX_NUM_OBJECTS];
1389 :
1390 591 : if ( !st_ivas->hDecoderConfig->Opt_tsm )
1391 : {
1392 0 : return IVAS_ERR_OK;
1393 : }
1394 :
1395 591 : *nSamplesRendered = 0;
1396 591 : hTcBuffer = st_ivas->hTcBuffer;
1397 :
1398 : /* get number of possible slots in new granularity */
1399 591 : n_samples_still_available = hTcBuffer->n_samples_buffered - hTcBuffer->n_samples_rendered;
1400 591 : n_slots_still_available = n_samples_still_available / tc_granularity_new;
1401 591 : *nSamplesRendered = n_slots_still_available * tc_granularity_new;
1402 591 : n_samples_to_render = *nSamplesRendered;
1403 591 : n_samples_still_available -= n_samples_to_render;
1404 591 : assert( n_samples_still_available < tc_granularity_new );
1405 :
1406 : /* update combined orientation access index */
1407 591 : ivas_combined_orientation_set_to_start_index( st_ivas->hCombinedOrientationData );
1408 :
1409 591 : if ( n_slots_still_available )
1410 : {
1411 : int16_t ch_idx;
1412 :
1413 : /* render what is still there with zero padding */
1414 1770 : for ( ch_idx = 0; ch_idx < hTcBuffer->nchan_buffer_full; ch_idx++ )
1415 : {
1416 : /* move it at the beginning of the TC buffer with zero padding */
1417 1455 : mvr2r( hTcBuffer->tc[ch_idx] + hTcBuffer->n_samples_rendered, hTcBuffer->tc[ch_idx], n_samples_to_render );
1418 1455 : set_zero( hTcBuffer->tc[ch_idx] + n_samples_to_render, hTcBuffer->n_samples_granularity - n_samples_to_render );
1419 1455 : mvr2r( hTcBuffer->tc[ch_idx] + hTcBuffer->n_samples_rendered + n_samples_to_render, hTcBuffer->tc[ch_idx] + hTcBuffer->n_samples_granularity, n_samples_still_available );
1420 : }
1421 :
1422 : /* simple change of the slot info */
1423 315 : hTcBuffer->num_slots = 1;
1424 315 : hTcBuffer->nb_subframes = 1;
1425 315 : hTcBuffer->subframes_rendered = 0;
1426 315 : hTcBuffer->slots_rendered = 0;
1427 315 : hTcBuffer->subframe_nbslots[0] = 1;
1428 315 : hTcBuffer->n_samples_buffered = hTcBuffer->n_samples_granularity + n_samples_still_available;
1429 315 : hTcBuffer->n_samples_available = 0;
1430 315 : hTcBuffer->n_samples_flushed = n_samples_to_render;
1431 315 : hTcBuffer->n_samples_rendered = 0;
1432 :
1433 6615 : for ( ch_idx = 0; ch_idx < MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS; ch_idx++ )
1434 : {
1435 6300 : p_output[ch_idx] = st_ivas->p_output_f[ch_idx];
1436 : }
1437 :
1438 315 : if ( st_ivas->ivas_format == ISM_FORMAT )
1439 : {
1440 237 : if ( ism_mode_old == ISM_MODE_DISC )
1441 : {
1442 : /* Binaural rendering */
1443 237 : if ( renderer_type_old == RENDERER_BINAURAL_OBJECTS_TD )
1444 : {
1445 216 : if ( ( error = ivas_td_binaural_renderer_sf( st_ivas, p_output, hTcBuffer->n_samples_granularity ) ) != IVAS_ERR_OK )
1446 : {
1447 0 : return error;
1448 : }
1449 : }
1450 21 : else if ( renderer_type_old == RENDERER_BINAURAL_MIXER_CONV_ROOM )
1451 : {
1452 : /* Convert to CICPxx; used also for ISM->CICP19->binaural_room rendering */
1453 21 : set_f( st_ivas->hIsmRendererData->interpolator, 1.0f, hTcBuffer->n_samples_granularity );
1454 :
1455 21 : ivas_ism_render_sf( st_ivas, renderer_type_old, p_output, hTcBuffer->n_samples_granularity );
1456 :
1457 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,
1458 21 : NULL, NULL, st_ivas->hTcBuffer, p_output, p_output, hTcBuffer->n_samples_granularity, st_ivas->hDecoderConfig->output_Fs, 0 ) ) != IVAS_ERR_OK )
1459 : {
1460 0 : return error;
1461 : }
1462 : }
1463 : }
1464 : else
1465 : {
1466 0 : return IVAS_ERROR( IVAS_ERR_WRONG_MODE, "Wrong ISM_MODE in VoIP renderer flushing!" );
1467 : }
1468 : }
1469 78 : else if ( st_ivas->ivas_format == MC_FORMAT )
1470 : {
1471 6 : if ( mc_mode_old == MC_MODE_MCT )
1472 : {
1473 6 : int16_t crendInPlaceRotation = FALSE;
1474 :
1475 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 ) )
1476 : {
1477 3 : if ( ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) < ( hIntSetupOld->nchan_out_woLFE + hIntSetupOld->num_lfe ) )
1478 : {
1479 3 : crendInPlaceRotation = TRUE;
1480 3 : ivas_mc2sba( st_ivas->hTransSetup, hTcBuffer->tc, p_output, hTcBuffer->n_samples_granularity, hIntSetupOld->ambisonics_order, GAIN_LFE );
1481 : }
1482 : }
1483 6 : if ( renderer_type_old == RENDERER_BINAURAL_MIXER_CONV || renderer_type_old == RENDERER_BINAURAL_MIXER_CONV_ROOM )
1484 : {
1485 3 : if ( ( error = ivas_rend_crendProcessSubframe( st_ivas->hCrendWrapper, intern_config_old, st_ivas->hOutSetup.output_config, st_ivas->hDecoderConfig, st_ivas->hCombinedOrientationData,
1486 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 )
1487 : {
1488 0 : return error;
1489 : }
1490 :
1491 3 : ivas_binaural_add_LFE( st_ivas, hTcBuffer->n_samples_granularity, st_ivas->hTcBuffer->tc, p_output );
1492 : }
1493 3 : else if ( renderer_type_old == RENDERER_BINAURAL_OBJECTS_TD )
1494 : {
1495 3 : if ( ( error = ivas_td_binaural_renderer_sf( st_ivas, p_output, hTcBuffer->n_samples_granularity ) ) != IVAS_ERR_OK )
1496 : {
1497 0 : return error;
1498 : }
1499 :
1500 3 : ivas_binaural_add_LFE( st_ivas, hTcBuffer->n_samples_granularity, st_ivas->hTcBuffer->tc, p_output );
1501 : }
1502 : else
1503 : {
1504 0 : return IVAS_ERROR( IVAS_ERR_WRONG_MODE, "Wrong renderer in MCT VoIP renderer flushing!" );
1505 : }
1506 : }
1507 : else
1508 : {
1509 0 : return IVAS_ERROR( IVAS_ERR_WRONG_MODE, "Wrong MC_MODE in VoIP renderer flushing!" );
1510 : }
1511 : }
1512 72 : else if ( st_ivas->ivas_format == MASA_ISM_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
1513 : {
1514 3 : if ( ism_mode_old == ISM_MASA_MODE_DISC )
1515 : {
1516 : float *tc_local[MAX_NUM_OBJECTS];
1517 :
1518 15 : for ( ch_idx = 0; ch_idx < st_ivas->nchan_ism; ch_idx++ )
1519 : {
1520 12 : tc_local[ch_idx] = &st_ivas->hTcBuffer->tc[ch_idx + 2][hTcBuffer->n_samples_rendered];
1521 12 : mvr2r( st_ivas->hMasaIsmData->delayBuffer[ch_idx], tc_local[ch_idx], st_ivas->hMasaIsmData->delayBuffer_size );
1522 : }
1523 :
1524 3 : if ( ( error = ivas_td_binaural_renderer_sf( st_ivas, p_output, hTcBuffer->n_samples_granularity ) ) != IVAS_ERR_OK )
1525 : {
1526 0 : return error;
1527 : }
1528 : }
1529 : }
1530 69 : else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
1531 : {
1532 69 : if ( ism_mode_old == ISM_SBA_MODE_DISC )
1533 : {
1534 : float *tc_local[MAX_TRANSPORT_CHANNELS];
1535 : int16_t last_spar_md_idx;
1536 : int16_t last_dirac_md_idx;
1537 : uint16_t nSamplesAvailableNext;
1538 : ISM_MODE ism_mode_orig;
1539 : RENDERER_TYPE renderer_type_orig;
1540 : int32_t ivas_total_brate;
1541 :
1542 69 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
1543 69 : renderer_type_orig = st_ivas->renderer_type;
1544 69 : ism_mode_orig = st_ivas->ism_mode;
1545 69 : st_ivas->ism_mode = ism_mode_old;
1546 69 : st_ivas->renderer_type = renderer_type_old;
1547 69 : st_ivas->hDecoderConfig->ivas_total_brate = st_ivas->hDecoderConfig->last_ivas_total_brate;
1548 :
1549 69 : last_spar_md_idx = st_ivas->hSpar->render_to_md_map[st_ivas->hSpar->slots_rendered - 1];
1550 69 : last_dirac_md_idx = st_ivas->hSpatParamRendCom->render_to_md_map[st_ivas->hSpatParamRendCom->slots_rendered - 1];
1551 : #ifdef DEBUGGING
1552 : assert( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV );
1553 : #endif
1554 :
1555 : /* copy from ISM delay buffer to the correct place in tcs */
1556 207 : for ( ch_idx = 0; ch_idx < st_ivas->nchan_ism; ch_idx++ )
1557 : {
1558 138 : tc_local[ch_idx] = &st_ivas->hTcBuffer->tc[ch_idx][hTcBuffer->n_samples_rendered];
1559 138 : mvr2r( st_ivas->hSbaIsmData->delayBuffer[ch_idx], tc_local[ch_idx], st_ivas->hSbaIsmData->delayBuffer_size );
1560 : }
1561 :
1562 : /* transfer adapted sf info from hTcBuffer to SPAR and DirAC */
1563 69 : st_ivas->hSpar->nb_subframes = 1;
1564 69 : st_ivas->hSpar->subframes_rendered = 0;
1565 69 : st_ivas->hSpar->subframe_nbslots[0] = JBM_CLDFB_SLOTS_IN_SUBFRAME;
1566 69 : st_ivas->hSpar->slots_rendered = 0;
1567 69 : st_ivas->hSpar->num_slots = JBM_CLDFB_SLOTS_IN_SUBFRAME;
1568 69 : st_ivas->hSpatParamRendCom->nb_subframes = 1;
1569 69 : st_ivas->hSpatParamRendCom->subframes_rendered = 0;
1570 69 : st_ivas->hSpatParamRendCom->subframe_nbslots[0] = JBM_CLDFB_SLOTS_IN_SUBFRAME;
1571 69 : st_ivas->hSpatParamRendCom->slots_rendered = 0;
1572 69 : st_ivas->hSpatParamRendCom->num_slots = JBM_CLDFB_SLOTS_IN_SUBFRAME;
1573 :
1574 : /* also adapt md maps, just use the last index */
1575 69 : set_s( st_ivas->hSpar->render_to_md_map, last_spar_md_idx, n_slots_still_available );
1576 69 : set_s( st_ivas->hSpatParamRendCom->render_to_md_map, last_dirac_md_idx, n_slots_still_available );
1577 :
1578 : /* render the last subframe */
1579 69 : if ( ( error = ivas_osba_dirac_td_binaural_jbm( st_ivas, (uint16_t) hTcBuffer->n_samples_granularity, nSamplesRendered, &nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
1580 : {
1581 0 : return error;
1582 : }
1583 :
1584 69 : st_ivas->ism_mode = ism_mode_orig;
1585 69 : st_ivas->renderer_type = renderer_type_orig;
1586 69 : st_ivas->hDecoderConfig->ivas_total_brate = ivas_total_brate;
1587 : }
1588 : }
1589 : else
1590 : {
1591 0 : return IVAS_ERROR( IVAS_ERR_WRONG_MODE, "Wrong IVAS format in VoIP renderer flushing!" );
1592 : }
1593 :
1594 315 : hTcBuffer->n_samples_rendered = hTcBuffer->n_samples_granularity;
1595 : }
1596 :
1597 : /* update global combined orientation start index */
1598 591 : ivas_combined_orientation_update_start_index( st_ivas->hCombinedOrientationData, *nSamplesRendered );
1599 :
1600 591 : *nSamplesRendered = n_samples_to_render;
1601 :
1602 : /* Only write out the valid data*/
1603 591 : if ( is_split_rendering_enabled( st_ivas->hDecoderConfig, st_ivas->hRenderConfig ) == 0 )
1604 : {
1605 591 : if ( st_ivas->ivas_format != MONO_FORMAT )
1606 : {
1607 : #ifndef DISABLE_LIMITER
1608 591 : ivas_limiter_dec( st_ivas->hLimiter, p_output, st_ivas->hDecoderConfig->nchan_out, *nSamplesRendered, st_ivas->BER_detect );
1609 : #endif
1610 : }
1611 : }
1612 :
1613 591 : switch ( pcm_resolution )
1614 : {
1615 591 : case PCM_INT16:
1616 : #ifdef DEBUGGING
1617 : st_ivas->noClipping +=
1618 : #endif
1619 591 : ivas_syn_output( p_output, *nSamplesRendered, st_ivas->hDecoderConfig->nchan_out, (int16_t *) data );
1620 591 : break;
1621 0 : case PCM_FLOAT32:
1622 0 : ivas_syn_output_f( p_output, *nSamplesRendered, st_ivas->hDecoderConfig->nchan_out, (float *) data );
1623 0 : break;
1624 0 : default:
1625 0 : error = IVAS_ERR_UNKNOWN;
1626 0 : break;
1627 : }
1628 :
1629 591 : return IVAS_ERR_OK;
1630 : }
1631 :
1632 :
1633 : /*--------------------------------------------------------------------------*
1634 : * ivas_jbm_dec_set_discard_samples()
1635 : *
1636 : * Set number of samples to discard in the first subframe if the renderer granularity changes on a bitrate change
1637 : *--------------------------------------------------------------------------*/
1638 :
1639 591 : ivas_error ivas_jbm_dec_set_discard_samples(
1640 : Decoder_Struct *st_ivas /* i/o: main IVAS decoder structre */
1641 : )
1642 : {
1643 : int16_t nMaxSlotsPerSubframe, nSlotsInFirstSubframe;
1644 :
1645 : /* render first frame with front zero padding and discarding those samples */
1646 591 : nMaxSlotsPerSubframe = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / st_ivas->hTcBuffer->n_samples_granularity;
1647 591 : nSlotsInFirstSubframe = nMaxSlotsPerSubframe - st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1];
1648 591 : if ( nSlotsInFirstSubframe > 0 )
1649 : {
1650 318 : st_ivas->hTcBuffer->n_samples_discard = ( nMaxSlotsPerSubframe - nSlotsInFirstSubframe ) * st_ivas->hTcBuffer->n_samples_granularity;
1651 : /* set last subframes number to max to ensure correct continuation */
1652 318 : st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1] = nMaxSlotsPerSubframe;
1653 : }
1654 :
1655 591 : return IVAS_ERR_OK;
1656 : }
1657 :
1658 :
1659 : /*--------------------------------------------------------------------------*
1660 : * ivas_jbm_dec_get_adapted_linear_interpolator()
1661 : *
1662 : * Get an interpolator that is adapted to time scale modified IVAS frame
1663 : *--------------------------------------------------------------------------*/
1664 :
1665 88923 : void ivas_jbm_dec_get_adapted_linear_interpolator(
1666 : const int16_t default_interp_length, /* i : default length of the (full-frame) interpolator */
1667 : const int16_t interp_length, /* i : length of the interpolator to be created */
1668 : float *interpolator /* o : the interpolator */
1669 : )
1670 : {
1671 : int16_t jbm_segment_len, idx;
1672 : float dec;
1673 : #ifdef DEBUGGING
1674 : assert( default_interp_length % 2 == 0 );
1675 : #endif
1676 :
1677 88923 : jbm_segment_len = ( default_interp_length >> 1 );
1678 88923 : dec = 1.0f / default_interp_length;
1679 :
1680 88923 : interpolator[interp_length - 1] = 1.0f;
1681 4755099 : for ( idx = interp_length - 2; idx >= jbm_segment_len; idx-- )
1682 : {
1683 4666176 : interpolator[idx] = max( 0.0f, interpolator[idx + 1] - dec );
1684 : }
1685 :
1686 88923 : if ( interpolator[idx + 1] > 0.0f )
1687 : {
1688 88917 : dec = interpolator[idx + 1] / ( jbm_segment_len + 1 );
1689 4837581 : for ( ; idx >= 0; idx-- )
1690 : {
1691 4748664 : interpolator[idx] = interpolator[idx + 1] - dec;
1692 : }
1693 : }
1694 : else
1695 : {
1696 6 : set_f( interpolator, 0.0f, idx + 1 );
1697 : }
1698 :
1699 88923 : return;
1700 : }
1701 :
1702 :
1703 : /*--------------------------------------------------------------------------*
1704 : * ivas_jbm_dec_get_adapted_subframes()
1705 : *
1706 : * Get an interpolator that is adapted to time scale modified IVAS frame
1707 : *--------------------------------------------------------------------------*/
1708 :
1709 940962 : void ivas_jbm_dec_get_adapted_subframes(
1710 : const int16_t nCldfbTs, /* i : number of time slots in the current frame */
1711 : int16_t *subframe_nbslots, /* i/o: subframe grid */
1712 : int16_t *nb_subframes /* i/o: number of subframes in the frame */
1713 : )
1714 : {
1715 : uint16_t nSlotsInLastSubframe, nSlotsInFirstSubframe;
1716 940962 : uint16_t nCldfbSlotsLocal = nCldfbTs;
1717 :
1718 : /* get last subframe size from previous frame, determine how many slots have to be processed
1719 : in the first subframe (i.e. potential leftover of a 5ms subframe) */
1720 940962 : nSlotsInFirstSubframe = ( PARAM_MC_MAX_NSLOTS_IN_SUBFRAME - subframe_nbslots[*nb_subframes - 1] );
1721 940962 : *nb_subframes = 0;
1722 940962 : if ( nSlotsInFirstSubframe > 0 )
1723 : {
1724 27138 : *nb_subframes = 1;
1725 27138 : nCldfbSlotsLocal -= nSlotsInFirstSubframe;
1726 : }
1727 :
1728 940962 : *nb_subframes += (int16_t) ceilf( (float) nCldfbSlotsLocal / (float) PARAM_MC_MAX_NSLOTS_IN_SUBFRAME );
1729 940962 : nSlotsInLastSubframe = nCldfbSlotsLocal % PARAM_MC_MAX_NSLOTS_IN_SUBFRAME;
1730 :
1731 940962 : set_s( subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
1732 940962 : set_s( subframe_nbslots, PARAM_MC_MAX_NSLOTS_IN_SUBFRAME, *nb_subframes );
1733 :
1734 940962 : if ( nSlotsInFirstSubframe > 0 )
1735 : {
1736 27138 : subframe_nbslots[0] = nSlotsInFirstSubframe;
1737 : }
1738 :
1739 940962 : if ( nSlotsInLastSubframe > 0 )
1740 : {
1741 27186 : subframe_nbslots[*nb_subframes - 1] = nSlotsInLastSubframe;
1742 : }
1743 :
1744 940962 : return;
1745 : }
1746 :
1747 :
1748 : /*--------------------------------------------------------------------------*
1749 : * ivas_jbm_dec_get_adapted_linear_interpolator()
1750 : *
1751 : * Get an meta data map adapted to a time scale modified IVAS frame
1752 : *--------------------------------------------------------------------------*/
1753 :
1754 829650 : void ivas_jbm_dec_get_md_map(
1755 : const int16_t default_len, /* i : default frame length in metadata slots */
1756 : const int16_t len, /* i : length of the modfied frames in metadata slots */
1757 : const int16_t subframe_len, /* i : default length of a subframe */
1758 : const int16_t offset, /* i : current read offset into the md buffer */
1759 : const int16_t buf_len, /* i : length of the metadata buffer */
1760 : int16_t *map /* o : metadata index map */
1761 : )
1762 : {
1763 : int16_t jbm_segment_len, map_idx, src_idx, src_idx_map;
1764 : float dec, src_idx_f;
1765 :
1766 : #ifdef DEBUGGING
1767 : assert( default_len % 2 == 0 );
1768 : #endif
1769 829650 : jbm_segment_len = ( default_len >> 1 );
1770 829650 : dec = 1.0f / default_len;
1771 :
1772 7467384 : for ( map_idx = len - 1, src_idx = default_len - 1; map_idx >= jbm_segment_len; map_idx--, src_idx-- )
1773 : {
1774 6637734 : src_idx_map = max( 0, src_idx / subframe_len );
1775 6637734 : map[map_idx] = ( offset + src_idx_map ) % buf_len;
1776 : }
1777 :
1778 : /* changed part (first segment), interpolate index to parameters
1779 : (we do not want to interpolate and smooth acutal direction/diffuseness values even more) */
1780 829650 : if ( src_idx >= 0 )
1781 : {
1782 829620 : dec = ( (float) ( src_idx + 1 ) ) / ( (float) jbm_segment_len );
1783 829620 : src_idx_f = (float) ( src_idx + 1 ) - dec;
1784 7466580 : for ( ; map_idx >= 0; map_idx-- )
1785 : {
1786 6636960 : src_idx = max( 0, ( (int16_t) round_f( src_idx_f ) ) / subframe_len );
1787 6636960 : map[map_idx] = ( offset + src_idx ) % buf_len;
1788 6636960 : src_idx_f -= dec;
1789 : }
1790 : }
1791 : else
1792 : {
1793 30 : set_s( map, offset, map_idx + 1 );
1794 : }
1795 :
1796 829650 : return;
1797 : }
1798 :
1799 :
1800 : /*--------------------------------------------------------------------------*
1801 : * ivas_jbm_dec_get_md_map_even_spacing()
1802 : *
1803 : * Get an meta data map adapted to a time scale modified IVAS frame. Distribute slots evenly across the modified frame.
1804 : *--------------------------------------------------------------------------*/
1805 :
1806 84543 : void ivas_jbm_dec_get_md_map_even_spacing(
1807 : const int16_t len, /* i : length of the modfied frames in metadata slots */
1808 : const int16_t subframe_len, /* i : default length of a subframe */
1809 : const int16_t offset, /* i : current read offset into the md buffer */
1810 : const int16_t buf_len, /* i : length of the metadata buffer */
1811 : int16_t *map /* o : metadata index map */
1812 : )
1813 : {
1814 : int16_t map_idx, sf_idx, sf_length, increment, subframes_written;
1815 : float decimal, decimal_sum, eps;
1816 : int16_t subframe_map_length[MAX_PARAM_SPATIAL_SUBFRAMES];
1817 :
1818 : /* subframe map length */
1819 84543 : sf_length = len / subframe_len;
1820 84543 : if ( len % subframe_len == 0 )
1821 : {
1822 : /* even subframes */
1823 422580 : for ( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
1824 : {
1825 338064 : subframe_map_length[sf_idx] = sf_length;
1826 : }
1827 : }
1828 : else
1829 : {
1830 : /* uneven subframes */
1831 27 : decimal = ( (float) len / (float) subframe_len ) - (float) sf_length;
1832 27 : decimal_sum = decimal;
1833 27 : eps = 0.001f;
1834 135 : for ( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
1835 : {
1836 108 : increment = (int16_t) floorf( decimal_sum + eps );
1837 108 : subframe_map_length[sf_idx] = sf_length + increment;
1838 108 : if ( increment > 0 )
1839 : {
1840 51 : decimal_sum -= 1.0f;
1841 : }
1842 108 : decimal_sum += decimal;
1843 : }
1844 : }
1845 :
1846 : /* map slots to subframes */
1847 84543 : sf_idx = 0;
1848 84543 : subframes_written = 0;
1849 1437546 : for ( map_idx = 0; map_idx < len; map_idx++ )
1850 : {
1851 1353003 : map[map_idx] = ( offset + sf_idx ) % buf_len;
1852 1353003 : if ( map_idx - subframes_written >= subframe_map_length[sf_idx] - 1 )
1853 : {
1854 338172 : subframes_written += subframe_map_length[sf_idx];
1855 338172 : ++sf_idx;
1856 : }
1857 : }
1858 :
1859 84543 : return;
1860 : }
1861 :
1862 :
1863 : /*--------------------------------------------------------------------------*
1864 : * ivas_jbm_dec_get_num_tc_channels()
1865 : *
1866 : * Get the number of transport channels provided by the JBM transport channel decode function
1867 : *--------------------------------------------------------------------------*/
1868 :
1869 23064 : int16_t ivas_jbm_dec_get_num_tc_channels(
1870 : Decoder_Struct *st_ivas /* i : IVAS decoder handle */
1871 : )
1872 : {
1873 : int16_t num_tc;
1874 : int32_t ivas_total_brate;
1875 : AUDIO_CONFIG output_config;
1876 :
1877 :
1878 23064 : if ( st_ivas->renderer_type == RENDERER_DISABLE )
1879 : {
1880 2811 : num_tc = st_ivas->hDecoderConfig->nchan_out;
1881 : }
1882 : else
1883 : {
1884 20253 : num_tc = st_ivas->nchan_transport;
1885 : }
1886 23064 : output_config = st_ivas->hDecoderConfig->output_config;
1887 :
1888 23064 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
1889 :
1890 23064 : if ( st_ivas->ivas_format == MONO_FORMAT )
1891 : {
1892 9 : num_tc = st_ivas->hDecoderConfig->nchan_out;
1893 : }
1894 23055 : else if ( st_ivas->ivas_format == STEREO_FORMAT && st_ivas->hDecoderConfig->nchan_out == 1 )
1895 : {
1896 66 : num_tc = 1;
1897 : }
1898 22989 : else if ( st_ivas->ivas_format == ISM_FORMAT )
1899 : {
1900 4104 : if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
1901 : {
1902 6 : num_tc = 1;
1903 : }
1904 : }
1905 18885 : else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
1906 : {
1907 9306 : if ( st_ivas->sba_dirac_stereo_flag )
1908 : {
1909 372 : num_tc = CPE_CHANNELS;
1910 : }
1911 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 ) ) )
1912 : {
1913 1968 : num_tc = 1; /* Only one channel transported */
1914 : }
1915 :
1916 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 )
1917 : {
1918 54 : num_tc = CPE_CHANNELS;
1919 : }
1920 9306 : if ( st_ivas->ivas_format == SBA_FORMAT )
1921 : {
1922 :
1923 3792 : if ( num_tc == 3 )
1924 : {
1925 804 : num_tc++;
1926 : }
1927 : }
1928 : }
1929 9579 : else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
1930 : {
1931 3615 : if ( st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_EXTERNAL )
1932 : {
1933 3438 : if ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
1934 : {
1935 2154 : num_tc++;
1936 : }
1937 1284 : else if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
1938 : {
1939 1284 : num_tc += st_ivas->nchan_ism;
1940 : }
1941 : }
1942 : }
1943 5964 : else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
1944 : {
1945 2070 : if ( st_ivas->sba_dirac_stereo_flag )
1946 : {
1947 282 : num_tc = CPE_CHANNELS;
1948 : }
1949 2070 : if ( num_tc == 3 )
1950 : {
1951 486 : num_tc++;
1952 : }
1953 2070 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
1954 : {
1955 822 : num_tc += st_ivas->nchan_ism;
1956 : }
1957 : }
1958 3894 : else if ( st_ivas->ivas_format == MC_FORMAT )
1959 : {
1960 3756 : if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_MONO )
1961 : {
1962 15 : num_tc = 1;
1963 : }
1964 3741 : else if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_STEREO )
1965 : {
1966 78 : num_tc = 2;
1967 : }
1968 3663 : else if ( st_ivas->mc_mode == MC_MODE_MCT )
1969 : {
1970 : /* do all static dmx already in the TC decoder if less channels than transported... */
1971 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 ) )
1972 : {
1973 21 : if ( ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) >= ( st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe ) )
1974 : {
1975 0 : num_tc = st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe;
1976 : }
1977 : }
1978 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 ) )
1979 : {
1980 0 : num_tc = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
1981 : }
1982 : }
1983 1875 : else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
1984 : {
1985 33 : num_tc = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
1986 : }
1987 1842 : else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
1988 : {
1989 846 : if ( st_ivas->hOutSetup.separateChannelEnabled )
1990 : {
1991 63 : num_tc++;
1992 : }
1993 846 : if ( st_ivas->hOutSetup.separateChannelEnabled && ( output_config == IVAS_AUDIO_CONFIG_5_1 || output_config == IVAS_AUDIO_CONFIG_7_1 ||
1994 60 : output_config == IVAS_AUDIO_CONFIG_5_1_4 || output_config == IVAS_AUDIO_CONFIG_7_1_4 ||
1995 60 : output_config == IVAS_AUDIO_CONFIG_5_1_2 || ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.num_lfe > 0 ) ) )
1996 : {
1997 : /* LFE is synthesized in TD with the TCs*/
1998 3 : num_tc++;
1999 : }
2000 : }
2001 : }
2002 138 : else if ( st_ivas->ivas_format == MONO_FORMAT && st_ivas->renderer_type == RENDERER_NON_DIEGETIC_DOWNMIX )
2003 : {
2004 0 : num_tc = MAX_OUTPUT_CHANNELS_IN_DIEGETIC_PAN;
2005 : }
2006 :
2007 23064 : return num_tc;
2008 : }
2009 :
2010 :
2011 : /*--------------------------------------------------------------------------*
2012 : * ivas_jbm_dec_get_render_granularity()
2013 : *
2014 : *
2015 : *--------------------------------------------------------------------------*/
2016 :
2017 : /*! r: render granularity */
2018 21603 : int16_t ivas_jbm_dec_get_render_granularity(
2019 : const RENDERER_TYPE renderer_type, /* i : renderer type */
2020 : const RENDERER_TYPE renderer_type_sec, /* i : secondary renderer type */
2021 : const int32_t output_Fs /* i : sampling rate */
2022 : )
2023 : {
2024 : int16_t render_granularity;
2025 :
2026 21603 : if ( renderer_type == RENDERER_BINAURAL_OBJECTS_TD || /* TD renderer */
2027 20493 : renderer_type == RENDERER_BINAURAL_MIXER_CONV || renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM || /* Crend */
2028 : renderer_type_sec == RENDERER_BINAURAL_OBJECTS_TD /* TD rend as a secondary renderer -> set the common granularity for both renderers */
2029 : )
2030 : {
2031 : /* 5 ms granularity */
2032 3588 : render_granularity = NS2SA( output_Fs, FRAME_SIZE_NS / MAX_PARAM_SPATIAL_SUBFRAMES );
2033 : }
2034 : else
2035 : {
2036 : /* 1.25 ms granularity */
2037 18015 : render_granularity = NS2SA( output_Fs, CLDFB_SLOT_NS );
2038 : }
2039 :
2040 21603 : return render_granularity;
2041 : }
2042 :
2043 :
2044 : /*--------------------------------------------------------------------------*
2045 : * ivas_jbm_dec_tc_buffer_open()
2046 : *
2047 : * open and initialize JBM transport channel buffer
2048 : *--------------------------------------------------------------------------*/
2049 :
2050 1881 : ivas_error ivas_jbm_dec_tc_buffer_open(
2051 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
2052 : const TC_BUFFER_MODE tc_buffer_mode, /* i : buffer mode */
2053 : const int16_t nchan_transport_jbm, /* i : number of real transport channels */
2054 : const int16_t nchan_transport_internal, /* i : number of totally buffered channels */
2055 : const int16_t nchan_full, /* i : number of channels to fully store */
2056 : const int16_t n_samples_granularity /* i : granularity of the renderer/buffer */
2057 : )
2058 : {
2059 : int16_t nsamp_to_allocate;
2060 : DECODER_TC_BUFFER_HANDLE hTcBuffer;
2061 : int16_t nMaxSlotsPerSubframe;
2062 : int16_t nchan_residual;
2063 : int16_t ch_idx;
2064 :
2065 : /*-----------------------------------------------------------------*
2066 : * prepare library opening
2067 : *-----------------------------------------------------------------*/
2068 :
2069 1881 : if ( ( hTcBuffer = (DECODER_TC_BUFFER_HANDLE) malloc( sizeof( DECODER_TC_BUFFER ) ) ) == NULL )
2070 : {
2071 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for JBM TC Buffer\n" ) );
2072 : }
2073 :
2074 1881 : hTcBuffer->tc_buffer_mode = tc_buffer_mode;
2075 1881 : hTcBuffer->nchan_transport_jbm = nchan_transport_jbm;
2076 1881 : hTcBuffer->nchan_transport_internal = nchan_transport_internal;
2077 1881 : hTcBuffer->nchan_buffer_full = nchan_full;
2078 1881 : nchan_residual = nchan_transport_internal - nchan_full;
2079 1881 : hTcBuffer->n_samples_granularity = n_samples_granularity;
2080 1881 : hTcBuffer->n_samples_available = 0;
2081 1881 : hTcBuffer->n_samples_buffered = 0;
2082 1881 : hTcBuffer->n_samples_rendered = 0;
2083 1881 : hTcBuffer->slots_rendered = 0;
2084 1881 : hTcBuffer->subframes_rendered = 0;
2085 1881 : hTcBuffer->n_samples_discard = 0;
2086 1881 : hTcBuffer->n_samples_flushed = 0;
2087 1881 : hTcBuffer->nb_subframes = MAX_PARAM_SPATIAL_SUBFRAMES;
2088 1881 : nsamp_to_allocate = 0;
2089 1881 : nMaxSlotsPerSubframe = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / hTcBuffer->n_samples_granularity;
2090 1881 : hTcBuffer->num_slots = nMaxSlotsPerSubframe * MAX_PARAM_SPATIAL_SUBFRAMES;
2091 1881 : set_s( hTcBuffer->subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
2092 1881 : set_s( hTcBuffer->subframe_nbslots, nMaxSlotsPerSubframe, MAX_PARAM_SPATIAL_SUBFRAMES );
2093 :
2094 : {
2095 : int16_t n_samp_full, n_samp_residual;
2096 : int32_t offset;
2097 1881 : if ( st_ivas->hDecoderConfig->Opt_tsm )
2098 : {
2099 105 : n_samp_full = ( NS2SA( st_ivas->hDecoderConfig->output_Fs, MAX_JBM_L_FRAME_NS ) + hTcBuffer->n_samples_granularity - 1 );
2100 105 : n_samp_residual = hTcBuffer->n_samples_granularity - 1;
2101 : }
2102 : else
2103 : {
2104 1776 : n_samp_full = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / FRAMES_PER_SEC );
2105 1776 : n_samp_residual = 0;
2106 : }
2107 :
2108 1881 : nsamp_to_allocate = hTcBuffer->nchan_buffer_full * n_samp_full;
2109 1881 : nsamp_to_allocate += nchan_residual * n_samp_residual;
2110 :
2111 1881 : if ( nsamp_to_allocate == 0 )
2112 : {
2113 45 : hTcBuffer->tc_buffer = NULL;
2114 :
2115 765 : for ( ch_idx = 0; ch_idx < MAX_TRANSPORT_CHANNELS + MAX_NUM_OBJECTS; ch_idx++ )
2116 : {
2117 720 : hTcBuffer->tc[ch_idx] = NULL;
2118 : }
2119 : }
2120 : else
2121 : {
2122 1836 : if ( st_ivas->hDecoderConfig->Opt_tsm )
2123 : {
2124 : /* note: the maximum buffer length is for OSBA DISC mode with ISMs -> 15*(1920+239)=32385 samples */
2125 105 : if ( ( hTcBuffer->tc_buffer = (float *) malloc( nsamp_to_allocate * sizeof( float ) ) ) == NULL )
2126 : {
2127 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for JBM TC Buffer\n" ) );
2128 : }
2129 105 : set_zero( hTcBuffer->tc_buffer, nsamp_to_allocate );
2130 :
2131 105 : offset = 0;
2132 483 : for ( ch_idx = 0; ch_idx < hTcBuffer->nchan_buffer_full; ch_idx++ )
2133 : {
2134 378 : hTcBuffer->tc[ch_idx] = &hTcBuffer->tc_buffer[offset];
2135 378 : offset += n_samp_full;
2136 : }
2137 117 : for ( ; ch_idx < hTcBuffer->nchan_transport_internal; ch_idx++ )
2138 : {
2139 12 : hTcBuffer->tc[ch_idx] = &hTcBuffer->tc_buffer[offset];
2140 12 : offset += n_samp_residual;
2141 : }
2142 1395 : for ( ; ch_idx < MAX_TRANSPORT_CHANNELS + MAX_NUM_OBJECTS; ch_idx++ )
2143 : {
2144 1290 : hTcBuffer->tc[ch_idx] = NULL;
2145 : }
2146 : }
2147 : else
2148 : {
2149 1731 : hTcBuffer->tc_buffer = NULL;
2150 : }
2151 : }
2152 : }
2153 :
2154 1881 : st_ivas->hTcBuffer = hTcBuffer;
2155 :
2156 1881 : return IVAS_ERR_OK;
2157 : }
2158 :
2159 :
2160 : /*--------------------------------------------------------------------------*
2161 : * ivas_jbm_dec_tc_buffer_reconfigure()
2162 : *
2163 : * open and initialize JBM transport channel buffer
2164 : *--------------------------------------------------------------------------*/
2165 :
2166 12774 : ivas_error ivas_jbm_dec_tc_buffer_reconfigure(
2167 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
2168 : const TC_BUFFER_MODE tc_buffer_mode, /* i : new buffer mode */
2169 : const int16_t nchan_transport_jbm, /* i : new number of real transport channels */
2170 : const int16_t nchan_transport_internal, /* i : new number of totally buffered channels */
2171 : const int16_t nchan_full, /* i : new number of channels to fully store */
2172 : const int16_t n_samples_granularity /* i : new granularity of the renderer/buffer */
2173 : )
2174 : {
2175 : int16_t nsamp_to_allocate, n_samp_full, n_samp_residual, offset, nchan_residual;
2176 : int16_t ch_idx;
2177 : DECODER_TC_BUFFER_HANDLE hTcBuffer;
2178 :
2179 12774 : hTcBuffer = st_ivas->hTcBuffer;
2180 :
2181 :
2182 : /* if granularity changes, adapt subframe_nb_slots */
2183 12774 : if ( n_samples_granularity != hTcBuffer->n_samples_granularity )
2184 : {
2185 : #ifdef DEBUGGING
2186 : int16_t nMaxSlotsPerSubframeOld;
2187 : #endif
2188 : int16_t nMaxSlotsPerSubframeNew;
2189 :
2190 3687 : nMaxSlotsPerSubframeNew = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / n_samples_granularity;
2191 : #ifdef DEBUGGING
2192 : nMaxSlotsPerSubframeOld = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / st_ivas->hTcBuffer->n_samples_granularity;
2193 : assert( hTcBuffer->subframe_nbslots[hTcBuffer->subframes_rendered - 1] == nMaxSlotsPerSubframeOld );
2194 : if ( n_samples_granularity < hTcBuffer->n_samples_granularity )
2195 : {
2196 : assert( ( hTcBuffer->n_samples_granularity % n_samples_granularity ) == 0 );
2197 : }
2198 : else
2199 : {
2200 : assert( ( n_samples_granularity % hTcBuffer->n_samples_granularity ) == 0 );
2201 : }
2202 : #endif
2203 : /* if samples were flushed, take that into account here */
2204 3687 : if ( n_samples_granularity < hTcBuffer->n_samples_granularity && hTcBuffer->n_samples_flushed > 0 )
2205 : {
2206 315 : hTcBuffer->subframe_nbslots[hTcBuffer->subframes_rendered - 1] = hTcBuffer->n_samples_flushed / n_samples_granularity;
2207 315 : hTcBuffer->n_samples_flushed = 0;
2208 : }
2209 : else
2210 : {
2211 3372 : hTcBuffer->subframe_nbslots[hTcBuffer->subframes_rendered - 1] = nMaxSlotsPerSubframeNew;
2212 : }
2213 : }
2214 :
2215 12774 : hTcBuffer->tc_buffer_mode = tc_buffer_mode;
2216 12774 : hTcBuffer->nchan_transport_jbm = nchan_transport_jbm;
2217 12774 : hTcBuffer->nchan_transport_internal = nchan_transport_internal;
2218 12774 : hTcBuffer->nchan_buffer_full = nchan_full;
2219 12774 : nchan_residual = nchan_transport_internal - nchan_full;
2220 12774 : hTcBuffer->n_samples_granularity = n_samples_granularity;
2221 :
2222 : #ifdef DEBUGGING
2223 : /* what is remaining from last frames needs always be smaller than n_samples_granularity */
2224 : assert( ( hTcBuffer->n_samples_buffered - hTcBuffer->n_samples_rendered ) < n_samples_granularity );
2225 : #endif
2226 : /* realloc buffers */
2227 12774 : if ( hTcBuffer->tc_buffer != NULL )
2228 : {
2229 3321 : free( hTcBuffer->tc_buffer );
2230 3321 : hTcBuffer->tc_buffer = NULL;
2231 : }
2232 :
2233 12774 : if ( st_ivas->hDecoderConfig->Opt_tsm )
2234 : {
2235 3321 : n_samp_full = ( NS2SA( st_ivas->hDecoderConfig->output_Fs, MAX_JBM_L_FRAME_NS ) + hTcBuffer->n_samples_granularity - 1 );
2236 3321 : n_samp_residual = hTcBuffer->n_samples_granularity - 1;
2237 : }
2238 : else
2239 : {
2240 9453 : n_samp_full = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / FRAMES_PER_SEC );
2241 9453 : n_samp_residual = 0;
2242 : }
2243 12774 : nsamp_to_allocate = hTcBuffer->nchan_buffer_full * n_samp_full;
2244 12774 : nsamp_to_allocate += nchan_residual * n_samp_residual;
2245 :
2246 12774 : if ( nsamp_to_allocate == 0 )
2247 : {
2248 981 : hTcBuffer->tc_buffer = NULL;
2249 16677 : for ( ch_idx = 0; ch_idx < MAX_TRANSPORT_CHANNELS + MAX_NUM_OBJECTS; ch_idx++ )
2250 : {
2251 15696 : hTcBuffer->tc[ch_idx] = NULL;
2252 : }
2253 : }
2254 : else
2255 : {
2256 11793 : if ( st_ivas->hDecoderConfig->Opt_tsm )
2257 : {
2258 3321 : if ( ( hTcBuffer->tc_buffer = (float *) malloc( nsamp_to_allocate * sizeof( float ) ) ) == NULL )
2259 : {
2260 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for JBM TC Buffer\n" ) );
2261 : }
2262 3321 : set_zero( hTcBuffer->tc_buffer, nsamp_to_allocate );
2263 :
2264 3321 : offset = 0;
2265 14922 : for ( ch_idx = 0; ch_idx < hTcBuffer->nchan_buffer_full; ch_idx++ )
2266 : {
2267 11601 : hTcBuffer->tc[ch_idx] = &hTcBuffer->tc_buffer[offset];
2268 11601 : offset += n_samp_full;
2269 : }
2270 3348 : for ( ; ch_idx < hTcBuffer->nchan_transport_internal; ch_idx++ )
2271 : {
2272 27 : hTcBuffer->tc[ch_idx] = &hTcBuffer->tc_buffer[offset];
2273 27 : offset += n_samp_residual;
2274 : }
2275 44829 : for ( ; ch_idx < MAX_TRANSPORT_CHANNELS + MAX_NUM_OBJECTS; ch_idx++ )
2276 : {
2277 41508 : hTcBuffer->tc[ch_idx] = NULL;
2278 : }
2279 : }
2280 : else
2281 : {
2282 8472 : hTcBuffer->tc_buffer = NULL;
2283 : }
2284 : }
2285 :
2286 12774 : return IVAS_ERR_OK;
2287 : }
2288 :
2289 :
2290 : /*--------------------------------------------------------------------------*
2291 : * ivas_jbm_dec_tc_buffer_playout()
2292 : *
2293 : *
2294 : *--------------------------------------------------------------------------*/
2295 :
2296 796375 : static void ivas_jbm_dec_tc_buffer_playout(
2297 : Decoder_Struct *st_ivas,
2298 : const uint16_t nSamplesAsked,
2299 : uint16_t *nSamplesRendered,
2300 : float *output[] )
2301 : {
2302 : int16_t ch_idx, slot_size, slots_to_render, first_sf, last_sf;
2303 :
2304 796375 : slot_size = st_ivas->hTcBuffer->n_samples_granularity;
2305 :
2306 : /* loop for synthesis, assume we always have to render in multiples of 5ms subframes with spills */
2307 796375 : slots_to_render = min( st_ivas->hTcBuffer->num_slots - st_ivas->hTcBuffer->slots_rendered, nSamplesAsked / slot_size );
2308 796375 : st_ivas->hTcBuffer->slots_rendered += slots_to_render;
2309 796375 : *nSamplesRendered = (uint16_t) slots_to_render * slot_size;
2310 796375 : first_sf = st_ivas->hTcBuffer->subframes_rendered;
2311 796375 : last_sf = first_sf;
2312 :
2313 2344204 : while ( slots_to_render > 0 )
2314 : {
2315 1547829 : slots_to_render -= st_ivas->hTcBuffer->subframe_nbslots[last_sf];
2316 1547829 : last_sf++;
2317 : }
2318 : #ifdef DEBUGGING
2319 : assert( slots_to_render == 0 );
2320 : #endif
2321 :
2322 2252370 : for ( ch_idx = 0; ch_idx < st_ivas->hTcBuffer->nchan_transport_jbm; ch_idx++ )
2323 : {
2324 1455995 : mvr2r( st_ivas->hTcBuffer->tc[ch_idx] + st_ivas->hTcBuffer->n_samples_rendered, output[ch_idx], *nSamplesRendered );
2325 : }
2326 :
2327 796375 : st_ivas->hTcBuffer->subframes_rendered = last_sf;
2328 :
2329 796375 : return;
2330 : }
2331 :
2332 :
2333 : /*--------------------------------------------------------------------------*
2334 : * ivas_jbm_dec_tc_buffer_close()
2335 : *
2336 : * Close JBM transport channel buffer
2337 : *--------------------------------------------------------------------------*/
2338 :
2339 1881 : void ivas_jbm_dec_tc_buffer_close(
2340 : DECODER_TC_BUFFER_HANDLE *phTcBuffer /* i/o: TC buffer handle */
2341 : )
2342 : {
2343 : int16_t i;
2344 :
2345 1881 : if ( *phTcBuffer != NULL )
2346 : {
2347 31977 : for ( i = 0; i < MAX_TRANSPORT_CHANNELS + MAX_NUM_OBJECTS; i++ )
2348 : {
2349 30096 : ( *phTcBuffer )->tc[i] = NULL;
2350 : }
2351 :
2352 1881 : if ( ( *phTcBuffer )->tc_buffer != NULL )
2353 : {
2354 105 : free( ( *phTcBuffer )->tc_buffer );
2355 105 : ( *phTcBuffer )->tc_buffer = NULL;
2356 : }
2357 :
2358 1881 : free( *phTcBuffer );
2359 1881 : *phTcBuffer = NULL;
2360 : }
2361 :
2362 1881 : return;
2363 : }
2364 :
2365 :
2366 : /*--------------------------------------------------------------------------*
2367 : * ivas_jbm_dec_td_renderers_adapt_subframes()
2368 : *
2369 : * Close JBM transport channel buffer
2370 : *--------------------------------------------------------------------------*/
2371 :
2372 738300 : void ivas_jbm_dec_td_renderers_adapt_subframes(
2373 : Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
2374 : )
2375 : {
2376 : int16_t nMaxSlotsPerSubframe, nSlotsAvailable;
2377 : uint16_t nSlotsInLastSubframe, nSlotsInFirstSubframe;
2378 :
2379 738300 : nMaxSlotsPerSubframe = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / st_ivas->hTcBuffer->n_samples_granularity;
2380 738300 : nSlotsAvailable = st_ivas->hTcBuffer->n_samples_available / st_ivas->hTcBuffer->n_samples_granularity;
2381 738300 : st_ivas->hTcBuffer->num_slots = nSlotsAvailable;
2382 738300 : st_ivas->hTcBuffer->n_samples_available = nSlotsAvailable * st_ivas->hTcBuffer->n_samples_granularity;
2383 738300 : nSlotsInFirstSubframe = nMaxSlotsPerSubframe - st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1];
2384 738300 : st_ivas->hTcBuffer->nb_subframes = 0;
2385 :
2386 738300 : if ( nSlotsInFirstSubframe > 0 )
2387 : {
2388 16041 : st_ivas->hTcBuffer->nb_subframes = 1;
2389 16041 : nSlotsAvailable -= nSlotsInFirstSubframe;
2390 : }
2391 738300 : st_ivas->hTcBuffer->nb_subframes += (int16_t) ceilf( (float) nSlotsAvailable / (float) nMaxSlotsPerSubframe );
2392 738300 : nSlotsInLastSubframe = nSlotsAvailable % nMaxSlotsPerSubframe;
2393 738300 : set_s( st_ivas->hTcBuffer->subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
2394 738300 : set_s( st_ivas->hTcBuffer->subframe_nbslots, nMaxSlotsPerSubframe, st_ivas->hTcBuffer->nb_subframes );
2395 :
2396 738300 : if ( nSlotsInFirstSubframe > 0 )
2397 : {
2398 16041 : st_ivas->hTcBuffer->subframe_nbslots[0] = nSlotsInFirstSubframe;
2399 : }
2400 :
2401 738300 : if ( nSlotsInLastSubframe > 0 )
2402 : {
2403 16068 : st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1] = nSlotsInLastSubframe;
2404 : }
2405 :
2406 738300 : st_ivas->hTcBuffer->slots_rendered = 0;
2407 738300 : st_ivas->hTcBuffer->subframes_rendered = 0;
2408 :
2409 738300 : return;
2410 : }
2411 :
2412 :
2413 : /*--------------------------------------------------------------------------*
2414 : * ivas_jbm_dec_get_tc_buffer_mode()
2415 : *
2416 : *
2417 : *--------------------------------------------------------------------------*/
2418 :
2419 16515 : TC_BUFFER_MODE ivas_jbm_dec_get_tc_buffer_mode(
2420 : Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
2421 : )
2422 : {
2423 : TC_BUFFER_MODE buffer_mode;
2424 16515 : buffer_mode = TC_BUFFER_MODE_BUFFER;
2425 :
2426 16515 : switch ( st_ivas->renderer_type )
2427 : {
2428 : /* all renderers where we are done after TC decoding (might include DMX to mono/stereo */
2429 2409 : case RENDERER_DISABLE:
2430 : case RENDERER_MCMASA_MONO_STEREO:
2431 : case RENDERER_OSBA_STEREO:
2432 : case RENDERER_MONO_DOWNMIX:
2433 2409 : buffer_mode = TC_BUFFER_MODE_BUFFER;
2434 2409 : break;
2435 12546 : case RENDERER_TD_PANNING:
2436 : case RENDERER_BINAURAL_OBJECTS_TD:
2437 : case RENDERER_BINAURAL_FASTCONV:
2438 : case RENDERER_BINAURAL_FASTCONV_ROOM:
2439 : case RENDERER_BINAURAL_PARAMETRIC:
2440 : case RENDERER_BINAURAL_PARAMETRIC_ROOM:
2441 : case RENDERER_STEREO_PARAMETRIC:
2442 : case RENDERER_DIRAC:
2443 : case RENDERER_PARAM_ISM:
2444 : case RENDERER_BINAURAL_MIXER_CONV:
2445 : case RENDERER_BINAURAL_MIXER_CONV_ROOM:
2446 : case RENDERER_OMASA_OBJECT_EXT:
2447 : case RENDERER_OMASA_MIX_EXT:
2448 : case RENDERER_OSBA_AMBI:
2449 : case RENDERER_OSBA_LS:
2450 12546 : buffer_mode = TC_BUFFER_MODE_RENDERER;
2451 12546 : break;
2452 6 : case RENDERER_NON_DIEGETIC_DOWNMIX:
2453 6 : if ( st_ivas->ivas_format == MONO_FORMAT )
2454 : {
2455 3 : buffer_mode = TC_BUFFER_MODE_BUFFER;
2456 : }
2457 : else
2458 : {
2459 3 : buffer_mode = TC_BUFFER_MODE_RENDERER;
2460 : }
2461 6 : break;
2462 258 : case RENDERER_MC_PARAMMC:
2463 258 : if ( st_ivas->hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
2464 : {
2465 6 : buffer_mode = TC_BUFFER_MODE_BUFFER; /* TCs are already the DMX to mono or stereo */
2466 : }
2467 : else
2468 : {
2469 252 : buffer_mode = TC_BUFFER_MODE_RENDERER;
2470 : }
2471 258 : break;
2472 306 : case RENDERER_MC:
2473 306 : if ( ivas_jbm_dec_get_num_tc_channels( st_ivas ) != st_ivas->hDecoderConfig->nchan_out )
2474 : {
2475 285 : buffer_mode = TC_BUFFER_MODE_RENDERER;
2476 : }
2477 306 : break;
2478 990 : case RENDERER_SBA_LINEAR_ENC:
2479 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 ) )
2480 : {
2481 0 : buffer_mode = TC_BUFFER_MODE_BUFFER;
2482 : }
2483 : else
2484 : {
2485 990 : buffer_mode = TC_BUFFER_MODE_RENDERER;
2486 : }
2487 990 : break;
2488 0 : case RENDERER_SBA_LINEAR_DEC:
2489 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 ) )
2490 : {
2491 0 : buffer_mode = TC_BUFFER_MODE_BUFFER;
2492 : }
2493 : else
2494 : {
2495 0 : buffer_mode = TC_BUFFER_MODE_RENDERER;
2496 : }
2497 0 : break;
2498 : #ifdef DEBUGGING
2499 : default:
2500 : assert( 0 );
2501 : #endif
2502 : }
2503 :
2504 16515 : return buffer_mode;
2505 : }
2506 :
2507 :
2508 : /*--------------------------------------------------------------------------*
2509 : * ivas_jbm_dec_metadata_open()
2510 : *
2511 : * Open structure for metadata buffering in JBM
2512 : *--------------------------------------------------------------------------*/
2513 :
2514 9 : ivas_error ivas_jbm_dec_metadata_open(
2515 : Decoder_Struct *st_ivas )
2516 : {
2517 : JBM_METADATA_HANDLE hJbmMetadata;
2518 9 : if ( ( hJbmMetadata = (JBM_METADATA_HANDLE) malloc( sizeof( JBM_METADATA ) ) ) == NULL )
2519 : {
2520 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for JBM metadata handle\n" ) );
2521 : }
2522 9 : hJbmMetadata->sf_write_idx = 0;
2523 9 : hJbmMetadata->sf_md_buffer_length = MASA_JBM_RINGBUFFER_FRAMES * MAX_PARAM_SPATIAL_SUBFRAMES;
2524 :
2525 9 : hJbmMetadata->slot_write_idx = 0;
2526 9 : hJbmMetadata->slot_read_idx = 0;
2527 9 : hJbmMetadata->slot_md_buffer_length = MASA_JBM_RINGBUFFER_FRAMES * MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME;
2528 :
2529 9 : st_ivas->hJbmMetadata = hJbmMetadata;
2530 :
2531 9 : return IVAS_ERR_OK;
2532 : }
2533 :
2534 :
2535 : /*--------------------------------------------------------------------------*
2536 : * ivas_jbm_dec_copy_masa_meta_to_buffer()
2537 : *
2538 : * Copy decoded MASA metadata to a ring buffer
2539 : *--------------------------------------------------------------------------*/
2540 :
2541 3930 : static void ivas_jbm_dec_copy_masa_meta_to_buffer(
2542 : Decoder_Struct *st_ivas )
2543 : {
2544 : int16_t sf, dir, band;
2545 : JBM_METADATA_HANDLE hJbmMetadata;
2546 : MASA_DECODER_EXT_OUT_META *extOutMeta;
2547 : int16_t write_idx;
2548 :
2549 3930 : hJbmMetadata = st_ivas->hJbmMetadata;
2550 3930 : extOutMeta = st_ivas->hMasa->data.extOutMeta;
2551 :
2552 19650 : for ( sf = 0; sf < MAX_PARAM_SPATIAL_SUBFRAMES; sf++ )
2553 : {
2554 15720 : write_idx = ( hJbmMetadata->sf_write_idx + sf ) % hJbmMetadata->sf_md_buffer_length;
2555 :
2556 47160 : for ( dir = 0; dir < MASA_MAXIMUM_DIRECTIONS; dir++ )
2557 : {
2558 786000 : for ( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
2559 : {
2560 754560 : hJbmMetadata->directionIndexBuffer[dir][write_idx][band] = extOutMeta->directionIndex[dir][sf][band];
2561 754560 : hJbmMetadata->directToTotalRatioBuffer[dir][write_idx][band] = extOutMeta->directToTotalRatio[dir][sf][band];
2562 754560 : hJbmMetadata->spreadCoherenceBuffer[dir][write_idx][band] = extOutMeta->spreadCoherence[dir][sf][band];
2563 : }
2564 : }
2565 :
2566 393000 : for ( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
2567 : {
2568 377280 : hJbmMetadata->diffuseToTotalRatioBuffer[write_idx][band] = extOutMeta->diffuseToTotalRatio[sf][band];
2569 377280 : hJbmMetadata->surroundCoherenceBuffer[write_idx][band] = extOutMeta->surroundCoherence[sf][band];
2570 : }
2571 :
2572 15720 : hJbmMetadata->numberOfDirections[write_idx] = extOutMeta->descriptiveMeta.numberOfDirections;
2573 : }
2574 :
2575 3930 : return;
2576 : }
2577 :
2578 :
2579 : /*--------------------------------------------------------------------------*
2580 : * ivas_jbm_masa_sf_to_slot_map()
2581 : *
2582 : * Map input MASA metadata subframes to slots in JBM processing
2583 : *--------------------------------------------------------------------------*/
2584 :
2585 3930 : static void ivas_jbm_masa_sf_to_slot_map(
2586 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
2587 : const int16_t nCldfbTs /* i : number of CLDFB time slots */
2588 : )
2589 : {
2590 : int16_t sf_to_slot_map[MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME];
2591 : int16_t num_slots_in_subfr;
2592 : JBM_METADATA_HANDLE hJbmMetadata;
2593 : int16_t slot_idx;
2594 : int16_t write_idx, sf_index;
2595 :
2596 3930 : ivas_jbm_dec_copy_masa_meta_to_buffer( st_ivas );
2597 :
2598 : /* Set values */
2599 3930 : hJbmMetadata = st_ivas->hJbmMetadata;
2600 3930 : num_slots_in_subfr = CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES;
2601 :
2602 : /* Map input subframes to slots */
2603 3930 : ivas_jbm_dec_get_md_map_even_spacing( nCldfbTs, num_slots_in_subfr, 0, MAX_PARAM_SPATIAL_SUBFRAMES, sf_to_slot_map );
2604 :
2605 66870 : for ( slot_idx = 0; slot_idx < nCldfbTs; slot_idx++ )
2606 : {
2607 62940 : write_idx = ( hJbmMetadata->slot_write_idx + slot_idx ) % hJbmMetadata->slot_md_buffer_length;
2608 62940 : sf_index = ( hJbmMetadata->sf_write_idx + sf_to_slot_map[slot_idx] ) % hJbmMetadata->sf_md_buffer_length;
2609 :
2610 62940 : hJbmMetadata->sf_to_slot_map[write_idx] = sf_index;
2611 : }
2612 :
2613 3930 : hJbmMetadata->sf_write_idx = ( hJbmMetadata->sf_write_idx + MAX_PARAM_SPATIAL_SUBFRAMES ) % hJbmMetadata->sf_md_buffer_length;
2614 3930 : hJbmMetadata->slot_write_idx = ( hJbmMetadata->slot_write_idx + nCldfbTs ) % hJbmMetadata->slot_md_buffer_length;
2615 :
2616 3930 : return;
2617 : }
2618 :
2619 :
2620 : /*--------------------------------------------------------------------------*
2621 : * ivas_jbm_masa_sf_to_sf_map()
2622 : *
2623 : * Map input MASA metadata subframes to output subframes in JBM processing
2624 : *--------------------------------------------------------------------------*/
2625 :
2626 3930 : void ivas_jbm_masa_sf_to_sf_map(
2627 : Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
2628 : )
2629 : {
2630 : int16_t sf_to_sf_map[MAX_PARAM_SPATIAL_SUBFRAMES];
2631 : JBM_METADATA_HANDLE hJbmMetadata;
2632 : MASA_DECODER_EXT_OUT_META *extOutMeta;
2633 : int16_t slot_read_idx, sf_read_idx;
2634 : int16_t sf_idx;
2635 : int16_t dir, band;
2636 : uint8_t numberOfDirections;
2637 :
2638 : /* Set values */
2639 3930 : hJbmMetadata = st_ivas->hJbmMetadata;
2640 3930 : extOutMeta = st_ivas->hMasa->data.extOutMeta;
2641 :
2642 : /* Map slots to subframes */
2643 19650 : for ( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
2644 : {
2645 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 */
2646 15720 : sf_to_sf_map[sf_idx] = hJbmMetadata->sf_to_slot_map[slot_read_idx];
2647 15720 : hJbmMetadata->slot_read_idx = ( hJbmMetadata->slot_read_idx + CLDFB_SLOTS_PER_SUBFRAME ) % hJbmMetadata->slot_md_buffer_length;
2648 : }
2649 :
2650 : /* Copy mapped metadata to the EXT meta buffer for writing */
2651 19650 : for ( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
2652 : {
2653 15720 : sf_read_idx = sf_to_sf_map[sf_idx];
2654 :
2655 47160 : for ( dir = 0; dir < MASA_MAXIMUM_DIRECTIONS; dir++ )
2656 : {
2657 786000 : for ( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
2658 : {
2659 754560 : extOutMeta->directionIndex[dir][sf_idx][band] = hJbmMetadata->directionIndexBuffer[dir][sf_read_idx][band];
2660 754560 : extOutMeta->directToTotalRatio[dir][sf_idx][band] = hJbmMetadata->directToTotalRatioBuffer[dir][sf_read_idx][band];
2661 754560 : extOutMeta->spreadCoherence[dir][sf_idx][band] = hJbmMetadata->spreadCoherenceBuffer[dir][sf_read_idx][band];
2662 : }
2663 : }
2664 :
2665 393000 : for ( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
2666 : {
2667 377280 : extOutMeta->diffuseToTotalRatio[sf_idx][band] = hJbmMetadata->diffuseToTotalRatioBuffer[sf_read_idx][band];
2668 377280 : extOutMeta->surroundCoherence[sf_idx][band] = hJbmMetadata->surroundCoherenceBuffer[sf_read_idx][band];
2669 : }
2670 : }
2671 :
2672 : /* Determine the number of directions for the frame to be written */
2673 3930 : numberOfDirections = 0;
2674 16812 : for ( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
2675 : {
2676 13596 : sf_read_idx = sf_to_sf_map[sf_idx];
2677 :
2678 13596 : if ( hJbmMetadata->numberOfDirections[sf_read_idx] == 1 )
2679 : {
2680 714 : numberOfDirections = 1;
2681 714 : break;
2682 : }
2683 : }
2684 :
2685 3930 : extOutMeta->descriptiveMeta.numberOfDirections = numberOfDirections;
2686 :
2687 3930 : return;
2688 : }
2689 :
2690 :
2691 : /*--------------------------------------------------------------------------*
2692 : * ivas_dec_prepare_renderer()
2693 : *
2694 : * prepare IVAS renderer routine
2695 : *--------------------------------------------------------------------------*/
2696 :
2697 1274778 : void ivas_dec_prepare_renderer(
2698 : Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
2699 : )
2700 : {
2701 : int16_t n, n_render_timeslots;
2702 :
2703 1274778 : push_wmops( "ivas_dec_prepare_renderer" );
2704 :
2705 1274778 : n_render_timeslots = st_ivas->hTcBuffer->n_samples_available / st_ivas->hTcBuffer->n_samples_granularity;
2706 :
2707 1274778 : if ( st_ivas->hTcBuffer->tc_buffer_mode == TC_BUFFER_MODE_BUFFER )
2708 : {
2709 385797 : ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
2710 :
2711 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 )
2712 : {
2713 3930 : ivas_jbm_masa_sf_to_slot_map( st_ivas, n_render_timeslots );
2714 : }
2715 :
2716 : /* MASA transport gaining for edited disc OMASA EXT. For ISMs, only metadata is modified */
2717 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 )
2718 : {
2719 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 );
2720 : }
2721 : }
2722 888981 : else if ( st_ivas->ivas_format == STEREO_FORMAT )
2723 : {
2724 0 : ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
2725 : }
2726 888981 : else if ( st_ivas->ivas_format == ISM_FORMAT )
2727 : {
2728 234678 : if ( st_ivas->ism_mode == ISM_MODE_PARAM )
2729 : {
2730 53139 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
2731 : {
2732 32865 : ivas_dirac_dec_set_md_map( st_ivas, n_render_timeslots );
2733 32865 : ivas_param_ism_params_to_masa_param_mapping( st_ivas );
2734 : }
2735 20274 : else if ( st_ivas->renderer_type == RENDERER_PARAM_ISM || st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
2736 : {
2737 20274 : ivas_param_ism_dec_prepare_renderer( st_ivas, n_render_timeslots );
2738 : }
2739 : }
2740 : else /* ISM_MODE_DISC */
2741 : {
2742 181539 : ivas_ism_dec_digest_tc( st_ivas );
2743 : }
2744 : }
2745 654303 : else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
2746 : {
2747 415884 : ivas_sba_dec_digest_tc( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
2748 : }
2749 238419 : else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
2750 : {
2751 100221 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
2752 : {
2753 63246 : ivas_ism_dec_digest_tc( st_ivas );
2754 :
2755 : /* delay the objects here for all renderers where it is needed */
2756 63246 : if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV ||
2757 32412 : st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ||
2758 23412 : st_ivas->renderer_type == RENDERER_OSBA_AMBI ||
2759 17400 : st_ivas->renderer_type == RENDERER_OSBA_LS ||
2760 11400 : st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL ) &&
2761 59046 : ( st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED && st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) )
2762 : {
2763 242538 : for ( n = 0; n < st_ivas->nchan_ism; n++ )
2764 : {
2765 183492 : delay_signal( st_ivas->hTcBuffer->tc[n], st_ivas->hTcBuffer->n_samples_available, st_ivas->hSbaIsmData->delayBuffer[n], st_ivas->hSbaIsmData->delayBuffer_size );
2766 : }
2767 : }
2768 :
2769 63246 : if ( !st_ivas->sba_dirac_stereo_flag )
2770 : {
2771 59046 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV )
2772 : {
2773 30834 : n_render_timeslots *= ( st_ivas->hTcBuffer->n_samples_granularity / st_ivas->hSpatParamRendCom->slot_size );
2774 : }
2775 :
2776 59046 : ivas_sba_dec_digest_tc( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
2777 : }
2778 : }
2779 : else
2780 : {
2781 36975 : ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
2782 :
2783 36975 : ivas_sba_dec_digest_tc( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
2784 : }
2785 : }
2786 138198 : else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
2787 : {
2788 16410 : if ( st_ivas->renderer_type == RENDERER_OMASA_MIX_EXT || st_ivas->renderer_type == RENDERER_OMASA_OBJECT_EXT )
2789 : {
2790 228 : ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
2791 :
2792 228 : if ( st_ivas->hDecoderConfig->Opt_tsm )
2793 : {
2794 0 : ivas_jbm_masa_sf_to_slot_map( st_ivas, n_render_timeslots );
2795 : }
2796 :
2797 : /* MASA transport gaining for edited param_one OMASA EXT. For ISMs, only metadata is modified. */
2798 228 : if ( st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ && st_ivas->hMasaIsmData->masa_gain_is_edited == 1 )
2799 : {
2800 0 : for ( n = 0; n < CPE_CHANNELS; n++ )
2801 : {
2802 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 );
2803 : }
2804 : }
2805 : }
2806 : else
2807 : {
2808 16182 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC && st_ivas->ism_mode == ISM_MASA_MODE_DISC )
2809 : {
2810 936 : n_render_timeslots *= ( st_ivas->hTcBuffer->n_samples_granularity / st_ivas->hSpatParamRendCom->slot_size );
2811 : }
2812 :
2813 16182 : ivas_sba_dec_digest_tc( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
2814 :
2815 16182 : if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
2816 : {
2817 6390 : ivas_ism_dec_digest_tc( st_ivas );
2818 : }
2819 :
2820 16182 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_DIRAC )
2821 : {
2822 : int16_t nchan_transport_ism;
2823 :
2824 : /* Delay the signal to match CLDFB delay. Delay the whole buffer. */
2825 10200 : nchan_transport_ism = 0;
2826 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 )
2827 : {
2828 4227 : nchan_transport_ism = 1;
2829 : }
2830 5973 : else if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
2831 : {
2832 4254 : nchan_transport_ism = st_ivas->nchan_ism;
2833 : }
2834 :
2835 25854 : for ( n = 0; n < nchan_transport_ism; n++ )
2836 : {
2837 15654 : if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC && st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC )
2838 : {
2839 : float gainIsm;
2840 3444 : gainIsm = OMASA_TDREND_MATCHING_GAIN;
2841 :
2842 3444 : if ( st_ivas->hMasaIsmData->ism_gain_is_edited[n] )
2843 : {
2844 2022 : gainIsm *= st_ivas->hMasaIsmData->gain_ism_edited[n];
2845 : }
2846 :
2847 3444 : v_multc( st_ivas->hTcBuffer->tc[CPE_CHANNELS + n], gainIsm, st_ivas->hTcBuffer->tc[CPE_CHANNELS + n], st_ivas->hTcBuffer->n_samples_available );
2848 : }
2849 :
2850 15654 : if ( st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED && st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
2851 : {
2852 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 );
2853 : }
2854 : }
2855 :
2856 10200 : if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC && st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC )
2857 : {
2858 : int16_t tcBufferSize;
2859 936 : tcBufferSize = st_ivas->hSpatParamRendCom->num_slots * st_ivas->hSpatParamRendCom->slot_size;
2860 :
2861 936 : if ( st_ivas->hMasaIsmData->masa_gain_is_edited )
2862 : {
2863 1728 : for ( n = 0; n < BINAURAL_CHANNELS; n++ )
2864 : {
2865 1152 : v_multc( st_ivas->hTcBuffer->tc[n], st_ivas->hMasaIsmData->gain_masa_edited, st_ivas->hTcBuffer->tc[n], tcBufferSize );
2866 : }
2867 : }
2868 : }
2869 : }
2870 : }
2871 : }
2872 121788 : else if ( st_ivas->ivas_format == MC_FORMAT )
2873 : {
2874 121788 : if ( st_ivas->mc_mode == MC_MODE_MCT )
2875 : {
2876 61785 : ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
2877 : }
2878 60003 : else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
2879 : {
2880 2340 : ivas_mc_paramupmix_dec_digest_tc( st_ivas, (uint8_t) n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
2881 : }
2882 57663 : else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
2883 : {
2884 30699 : ivas_param_mc_dec_prepare_renderer( st_ivas, (uint8_t) n_render_timeslots );
2885 : }
2886 26964 : else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
2887 : {
2888 26964 : ivas_sba_dec_digest_tc( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
2889 : }
2890 : }
2891 :
2892 1274778 : pop_wmops();
2893 1274778 : return;
2894 : }
|