Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2026 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 : #ifdef DEBUGGING
43 : #include "debug.h"
44 : #endif
45 : #include "wmc_auto.h"
46 :
47 :
48 : /*--------------------------------------------------------------------------*
49 : * ivas_dec()
50 : *
51 : * Principal IVAS decoder routine, decoding of metadata and transport channels
52 : *--------------------------------------------------------------------------*/
53 :
54 21654669 : ivas_error ivas_dec(
55 : Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
56 : )
57 : {
58 : int16_t n, output_frame, nchan_out;
59 : Decoder_State *st; /* used for bitstream handling */
60 : float *p_output[MAX_TRANSPORT_CHANNELS]; /* 'float' buffer for output synthesis */
61 : int16_t nchan_remapped;
62 : int16_t nb_bits_metadata[MAX_SCE + 1];
63 : int32_t output_Fs, ivas_total_brate;
64 : AUDIO_CONFIG output_config;
65 : ivas_error error;
66 : int16_t num_md_sub_frames;
67 : int32_t ism_total_brate;
68 :
69 21654669 : push_wmops( "ivas_dec" );
70 :
71 : /*----------------------------------------------------------------*
72 : * Initialization of local vars after struct has been set
73 : *----------------------------------------------------------------*/
74 :
75 21654669 : output_Fs = st_ivas->hDecoderConfig->output_Fs;
76 21654669 : nchan_out = st_ivas->hTcBuffer->nchan_transport_rend;
77 21654669 : output_config = st_ivas->hDecoderConfig->output_config;
78 21654669 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
79 :
80 21654669 : output_frame = (int16_t) ( output_Fs / FRAMES_PER_SEC );
81 :
82 : /* set pointers to transport channels audio */
83 281510697 : for ( n = 0; n < MAX_TRANSPORT_CHANNELS; n++ )
84 : {
85 259856028 : p_output[n] = st_ivas->p_output_f[n];
86 259856028 : if ( p_output[n] != NULL )
87 : {
88 118844770 : set_zero( p_output[n], L_FRAME48k );
89 : }
90 : }
91 :
92 : /*----------------------------------------------------------------*
93 : * Decoding + pre-rendering
94 : *----------------------------------------------------------------*/
95 :
96 21654669 : if ( st_ivas->ivas_format == STEREO_FORMAT )
97 : {
98 2509032 : st_ivas->hCPE[0]->element_brate = ivas_total_brate;
99 2509032 : if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, 0 ) ) != IVAS_ERR_OK )
100 : {
101 0 : return error;
102 : }
103 :
104 : /* HP filtering */
105 6737623 : for ( n = 0; n < min( nchan_out, st_ivas->nchan_transport ); n++ )
106 : {
107 4228591 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
108 : }
109 : }
110 19145637 : else if ( st_ivas->ivas_format == ISM_FORMAT )
111 : {
112 : /* Metadata decoding and configuration */
113 3119699 : if ( ivas_total_brate == IVAS_SID_5k2 || ivas_total_brate == FRAME_NO_DATA )
114 : {
115 45144 : ivas_ism_dtx_dec( st_ivas, nb_bits_metadata );
116 :
117 : /* decode dominant object first so the noise energy of the other objects can be limited */
118 45144 : 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 )
119 : {
120 0 : return error;
121 : }
122 :
123 45144 : ivas_ism_dtx_limit_noise_energy_for_near_silence( st_ivas->hSCE, st_ivas->hISMDTX.sce_id_dtx, st_ivas->nchan_transport );
124 :
125 45144 : ivas_param_ism_dec_dequant_md( st_ivas );
126 : }
127 3074555 : else if ( st_ivas->ism_mode == ISM_MODE_PARAM )
128 : {
129 534522 : 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 )
130 : {
131 0 : return error;
132 : }
133 :
134 534522 : ivas_param_ism_dec_dequant_md( st_ivas );
135 : }
136 : else /* ISM_MODE_DISC */
137 : {
138 2540033 : 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 )
139 : {
140 0 : return error;
141 : }
142 : }
143 :
144 11279438 : for ( n = 0; n < st_ivas->nchan_transport; n++ )
145 : {
146 : /* for DTX frames, dominant object has already been decoded before */
147 8159739 : if ( !( ( ivas_total_brate == IVAS_SID_5k2 || ivas_total_brate == FRAME_NO_DATA ) && n == st_ivas->hISMDTX.sce_id_dtx ) )
148 : {
149 8114595 : if ( ( error = ivas_sce_dec( st_ivas, n, &p_output[n], output_frame, nb_bits_metadata[n] ) ) != IVAS_ERR_OK )
150 : {
151 0 : return error;
152 : }
153 : }
154 :
155 : /* HP filtering */
156 8159739 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
157 : }
158 :
159 3119699 : if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
160 : {
161 144481 : ivas_ism_mono_dmx( st_ivas, p_output, output_frame );
162 : }
163 2975218 : 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 ) )
164 : {
165 : /* loudness correction */
166 267595 : ivas_dirac_dec_binaural_sba_gain( p_output, st_ivas->nchan_transport, output_frame );
167 : }
168 : }
169 16025938 : else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
170 : {
171 9467059 : set_s( nb_bits_metadata, 0, MAX_SCE );
172 :
173 : /* read parameters from the bitstream */
174 9467059 : if ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->hQMetaData != NULL )
175 : {
176 2584621 : st = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[0]->hCoreCoder[0] : st_ivas->hCPE[0]->hCoreCoder[0];
177 :
178 2584621 : if ( ( error = ivas_masa_decode( st_ivas, st, &nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
179 : {
180 0 : return error;
181 : }
182 : }
183 6882438 : else if ( st_ivas->ivas_format == SBA_FORMAT )
184 : {
185 6882438 : if ( ( error = ivas_spar_dec( st_ivas, nb_bits_metadata ) ) != IVAS_ERR_OK )
186 : {
187 0 : return error;
188 : }
189 : }
190 :
191 9467059 : if ( st_ivas->nchan_transport == CPE_CHANNELS && st_ivas->nCPE >= 1 )
192 : {
193 3641627 : st_ivas->hCPE[0]->brate_surplus = 0;
194 3641627 : st_ivas->hCPE[0]->element_brate = ivas_total_brate;
195 : }
196 :
197 : /* core-decoding of transport channels */
198 9467059 : if ( st_ivas->nSCE == 1 )
199 : {
200 3132314 : if ( ( error = ivas_sce_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
201 : {
202 0 : return error;
203 : }
204 : }
205 6334745 : else if ( st_ivas->nCPE == 1 )
206 : {
207 3641627 : if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
208 : {
209 0 : return error;
210 : }
211 : }
212 2693118 : else if ( st_ivas->nCPE > 1 )
213 : {
214 2693118 : if ( ( error = ivas_mct_dec( st_ivas, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
215 : {
216 0 : return error;
217 : }
218 : }
219 :
220 : #ifdef DEBUG_SBA_AUDIO_DUMP
221 : /* Dump audio signal after core-decoding */
222 : ivas_spar_dump_signal_wav( output_frame, NULL, output, st_ivas->nchan_transport, spar_foa_dec_wav[0], "core-decoding" );
223 : #endif
224 : /* TCs remapping */
225 9467059 : nchan_remapped = st_ivas->nchan_transport;
226 9467059 : if ( st_ivas->sba_dirac_stereo_flag )
227 : {
228 250160 : nchan_remapped = nchan_out;
229 :
230 250160 : if ( st_ivas->ivas_format == SBA_FORMAT )
231 : {
232 250160 : ivas_agc_dec_process( st_ivas->hSpar->hAgcDec, p_output, p_output, st_ivas->hSpar->hMdDec->spar_md_cfg.nchan_transport, output_frame );
233 :
234 250160 : if ( st_ivas->hSpar->hPCA != NULL )
235 : {
236 594 : 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 );
237 : }
238 :
239 250160 : 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 ) );
240 : }
241 :
242 250160 : ivas_sba_dirac_stereo_dec( st_ivas, p_output, output_frame );
243 : }
244 9216899 : 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 ) ) )
245 : {
246 568035 : nchan_remapped = 1; /* Only one channel transported */
247 : }
248 :
249 : /* HP filtering */
250 : #ifndef DEBUG_SPAR_BYPASS_EVS_CODEC
251 28923412 : for ( n = 0; n < nchan_remapped; n++ )
252 : {
253 19456353 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
254 : }
255 : #endif
256 :
257 9467059 : if ( st_ivas->ivas_format == SBA_FORMAT )
258 : {
259 6882438 : nchan_remapped = ivas_sba_remapTCs( p_output, st_ivas, output_frame );
260 :
261 6882438 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
262 : {
263 1152096 : 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 );
264 1152096 : ivas_sba_mix_matrix_determiner( st_ivas->hSpar, p_output, st_ivas->bfi, nchan_remapped, output_frame, num_md_sub_frames );
265 : }
266 5730342 : else if ( st_ivas->renderer_type != RENDERER_DISABLE )
267 : {
268 5408229 : ivas_spar_dec_agc_pca( st_ivas, p_output, output_frame );
269 : }
270 : }
271 :
272 9467059 : if ( st_ivas->ivas_format == MASA_FORMAT )
273 : {
274 2584621 : ivas_masa_prerender( st_ivas, p_output, output_frame, nchan_remapped );
275 :
276 : /* external output */
277 2584621 : if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL && st_ivas->hMasa->config.input_ivas_format == MASA_ISM_FORMAT )
278 : {
279 54452 : for ( n = 0; n < st_ivas->nchan_ism; n++ )
280 : {
281 41506 : set_zero( p_output[st_ivas->nchan_transport + n], output_frame );
282 : }
283 :
284 12946 : ivas_omasa_rearrange_channels( p_output, st_ivas->nchan_ism, output_frame );
285 : }
286 : }
287 6882438 : else if ( st_ivas->ivas_format == SBA_FORMAT && ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM ) )
288 : {
289 : /* loudness correction */
290 1152096 : ivas_dirac_dec_binaural_sba_gain( p_output, nchan_remapped, output_frame );
291 : }
292 : }
293 6558879 : else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
294 : {
295 : int16_t nchan_ism, nchan_transport_ism;
296 : int16_t dirac_bs_md_write_idx;
297 :
298 1224556 : set_s( nb_bits_metadata, 0, MAX_SCE + 1 );
299 :
300 : /* Set the number of objects for the parametric rendering */
301 1224556 : dirac_bs_md_write_idx = 0;
302 1224556 : if ( st_ivas->hSpatParamRendCom != NULL )
303 : {
304 1000352 : st_ivas->hSpatParamRendCom->numIsmDirections = 0;
305 1000352 : if ( st_ivas->ism_mode != ISM_MASA_MODE_DISC && st_ivas->ism_mode != ISM_MASA_MODE_MASA_ONE_OBJ )
306 : {
307 242828 : st_ivas->hSpatParamRendCom->numIsmDirections = st_ivas->nchan_ism;
308 : }
309 :
310 1000352 : dirac_bs_md_write_idx = st_ivas->hSpatParamRendCom->dirac_bs_md_write_idx; /* Store the write-index for this frame */
311 : }
312 :
313 : /* MASA metadata decoding */
314 1224556 : if ( ( error = ivas_masa_decode( st_ivas, st_ivas->hCPE[0]->hCoreCoder[0], &nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
315 : {
316 0 : return error;
317 : }
318 :
319 : /* Configuration of combined-format bit-budget distribution */
320 1224556 : ivas_set_surplus_brate_dec( st_ivas, &ism_total_brate );
321 :
322 1224556 : st_ivas->hCPE[0]->hCoreCoder[0]->bit_stream = &( st_ivas->bit_stream[( ism_total_brate / FRAMES_PER_SEC )] );
323 :
324 : /* set ISM parameters and decode ISM metadata in OMASA format */
325 1224556 : 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 )
326 : {
327 0 : return error;
328 : }
329 :
330 : /* decode ISM channels */
331 3440392 : for ( n = 0; n < nchan_transport_ism; n++ )
332 : {
333 2215836 : if ( ( error = ivas_sce_dec( st_ivas, n, &p_output[st_ivas->nchan_transport + n], output_frame, nb_bits_metadata[1] ) ) != IVAS_ERR_OK )
334 : {
335 0 : return error;
336 : }
337 : }
338 :
339 : /* decode MASA channels */
340 1224556 : if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
341 : {
342 0 : return error;
343 : }
344 :
345 1224556 : if ( st_ivas->hCPE[0]->nchan_out == 1 )
346 : {
347 211421 : mvr2r( p_output[0], p_output[1], output_frame ); /* Copy mono signal to stereo output channels */
348 : }
349 :
350 : /* HP filtering */
351 5889504 : for ( n = 0; n < getNumChanSynthesis( st_ivas ); n++ )
352 : {
353 4664948 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
354 : }
355 :
356 1224556 : if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
357 : {
358 124915 : ivas_ism_mono_dmx( st_ivas, p_output, output_frame );
359 : }
360 1099641 : else if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
361 : {
362 108661 : if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
363 : {
364 91413 : ivas_omasa_rearrange_channels( p_output, nchan_transport_ism, output_frame );
365 : }
366 17248 : else if ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ )
367 : {
368 : /* Convert separate object to MASA, combine with the original MASA, and output combined MASA + empty objects. */
369 7876 : ivas_omasa_combine_separate_ism_with_masa( st_ivas, p_output, st_ivas->nchan_ism, output_frame );
370 : }
371 9372 : else if ( st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
372 : {
373 : /* Extract objects from MASA, output MASA + all objects (i.e., extracted and separated objects) */
374 9372 : ivas_omasa_render_objects_from_mix( st_ivas, p_output, st_ivas->nchan_ism, output_frame );
375 : }
376 : }
377 : }
378 5334323 : else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
379 : {
380 : int16_t nchan_ism, sba_ch_idx;
381 :
382 2667903 : set_s( nb_bits_metadata, 0, MAX_SCE + 1 );
383 2667903 : nchan_ism = st_ivas->nchan_ism;
384 2667903 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
385 : {
386 : /* set ISM parameters and decode ISM metadata in OSBA format */
387 1357230 : if ( ( error = ivas_osba_ism_metadata_dec( st_ivas, ivas_total_brate, &nchan_ism, &nb_bits_metadata[1] ) ) != IVAS_ERR_OK )
388 : {
389 0 : return error;
390 : }
391 1357230 : sba_ch_idx = st_ivas->nchan_ism;
392 : }
393 : else
394 : {
395 1310673 : nb_bits_metadata[1] += NO_BITS_MASA_ISM_NO_OBJ;
396 1310673 : sba_ch_idx = 0;
397 : }
398 :
399 : /* SBA metadata decoding */
400 2667903 : if ( ( error = ivas_spar_dec( st_ivas, nb_bits_metadata ) ) != IVAS_ERR_OK )
401 : {
402 0 : return error;
403 : }
404 :
405 2667903 : if ( st_ivas->nchan_transport == CPE_CHANNELS && st_ivas->nCPE >= 1 )
406 : {
407 510928 : st_ivas->hCPE[0]->element_brate = ivas_total_brate;
408 : }
409 :
410 : /* core-decoding of transport channels */
411 2667903 : if ( st_ivas->nSCE == 1 )
412 : {
413 668805 : if ( ( error = ivas_sce_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] + nb_bits_metadata[1] ) ) != IVAS_ERR_OK )
414 : {
415 0 : return error;
416 : }
417 : }
418 1999098 : else if ( st_ivas->nCPE == 1 )
419 : {
420 510928 : if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] + nb_bits_metadata[1] ) ) != IVAS_ERR_OK )
421 : {
422 0 : return error;
423 : }
424 : }
425 1488170 : else if ( st_ivas->nCPE > 1 )
426 : {
427 1488170 : if ( ( error = ivas_mct_dec( st_ivas, p_output, output_frame, nb_bits_metadata[0] + nb_bits_metadata[1] ) ) != IVAS_ERR_OK )
428 : {
429 0 : return error;
430 : }
431 : }
432 :
433 2667903 : if ( st_ivas->sba_dirac_stereo_flag )
434 : {
435 184215 : 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 );
436 :
437 184215 : if ( st_ivas->hSpar->hPCA != NULL )
438 : {
439 2376 : 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] );
440 : }
441 :
442 184215 : 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 ) );
443 :
444 184215 : ivas_sba_dirac_stereo_dec( st_ivas, &p_output[sba_ch_idx], output_frame );
445 : }
446 :
447 : /* HP filtering */
448 13140000 : for ( n = 0; n < getNumChanSynthesis( st_ivas ); n++ )
449 : {
450 10472097 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
451 : }
452 :
453 2667903 : nchan_remapped = ivas_sba_remapTCs( &p_output[sba_ch_idx], st_ivas, output_frame );
454 :
455 : #ifdef DEBUG_OSBA
456 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
457 : {
458 : int16_t nchan = st_ivas->nchan_transport + st_ivas->nchan_ism;
459 : for ( int16_t t = 0; t < output_frame; t++ )
460 : {
461 : for ( int16_t c = 0; c < nchan; c++ )
462 : {
463 : int16_t val = (int16_t) ( output[c][t] + 0.5f );
464 : dbgwrite( &val, sizeof( int16_t ), 1, 1, "./res/TC_dec_core_out.raw" );
465 : }
466 : }
467 : }
468 : #endif
469 2667903 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
470 : {
471 463100 : 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 );
472 :
473 463100 : ivas_sba_mix_matrix_determiner( st_ivas->hSpar, &p_output[sba_ch_idx], st_ivas->bfi, nchan_remapped, output_frame, num_md_sub_frames );
474 : }
475 2204803 : else if ( st_ivas->renderer_type != RENDERER_DISABLE && !st_ivas->sba_dirac_stereo_flag )
476 : {
477 1933525 : ivas_spar_dec_agc_pca( st_ivas, &p_output[sba_ch_idx], output_frame );
478 : }
479 :
480 2667903 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
481 : {
482 : /* loudness correction */
483 463100 : ivas_dirac_dec_binaural_sba_gain( &p_output[sba_ch_idx], nchan_remapped, output_frame );
484 : }
485 2204803 : else if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX && st_ivas->ism_mode == ISM_SBA_MODE_DISC )
486 : {
487 69742 : ivas_ism_mono_dmx( st_ivas, p_output, output_frame );
488 :
489 : /* add W */
490 139484 : for ( n = 0; n < nchan_out; n++ )
491 : {
492 69742 : v_add( p_output[n], p_output[n + max( nchan_out, nchan_ism )], p_output[n], output_frame );
493 : }
494 : }
495 : }
496 2666420 : else if ( st_ivas->ivas_format == MC_FORMAT )
497 : {
498 2666420 : st = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[0]->hCoreCoder[0] : st_ivas->hCPE[0]->hCoreCoder[0];
499 :
500 2666420 : if ( st_ivas->mc_mode == MC_MODE_MCT )
501 : {
502 : /* LFE channel decoder */
503 1413194 : ivas_lfe_dec( st_ivas->hLFE, st, output_frame, st_ivas->bfi, p_output[LFE_CHANNEL] );
504 :
505 1413194 : if ( ( error = ivas_mct_dec( st_ivas, p_output, output_frame, 0 ) ) != IVAS_ERR_OK )
506 : {
507 0 : return error;
508 : }
509 :
510 : /* HP filtering */
511 12502216 : for ( n = 0; n < st_ivas->nchan_transport; n++ )
512 : {
513 11089022 : if ( n != LFE_CHANNEL )
514 : {
515 9675828 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
516 : }
517 : }
518 :
519 1413194 : 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 ) )
520 : {
521 45521 : if ( ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) >= ( st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe ) )
522 : {
523 0 : ivas_mc2sba( st_ivas->hTransSetup, p_output, p_output, output_frame, st_ivas->hIntSetup.ambisonics_order, GAIN_LFE );
524 : }
525 : }
526 :
527 1413194 : 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 ) )
528 : {
529 139748 : if ( st_ivas->renderer_type == RENDERER_MC )
530 : {
531 127478 : ivas_ls_setup_conversion( st_ivas, st_ivas->nchan_transport, output_frame, p_output, p_output );
532 : }
533 12270 : else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
534 : {
535 12270 : ivas_mc2sba( st_ivas->hIntSetup, p_output, p_output, output_frame, st_ivas->hOutSetup.ambisonics_order, 0.f );
536 : }
537 : }
538 : }
539 1253226 : else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
540 : {
541 : /* LFE channel decoder */
542 130887 : ivas_lfe_dec( st_ivas->hLFE, st, output_frame, st_ivas->bfi, p_output[LFE_CHANNEL] );
543 :
544 130887 : ivas_mc_paramupmix_dec_read_BS( st_ivas, st, st_ivas->hMCParamUpmix, &nb_bits_metadata[0] );
545 :
546 130887 : if ( ( error = ivas_mct_dec( st_ivas, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
547 : {
548 0 : return error;
549 : }
550 :
551 : /* HP filtering */
552 1177983 : for ( n = 0; n < st_ivas->nchan_transport; n++ )
553 : {
554 1047096 : if ( n != LFE_CHANNEL )
555 : {
556 916209 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
557 : }
558 : }
559 :
560 : /* Rendering */
561 130887 : if ( st_ivas->renderer_type == RENDERER_MC && ( output_config == IVAS_AUDIO_CONFIG_MONO || output_config == IVAS_AUDIO_CONFIG_STEREO ) )
562 : {
563 : /* Compensate loudness for not doing full upmix */
564 18170 : for ( n = 4; n < 8; n++ )
565 : {
566 14536 : v_multc( p_output[n], 2.0f, p_output[n], output_frame );
567 : }
568 :
569 3634 : if ( output_config == IVAS_AUDIO_CONFIG_MONO || output_config == IVAS_AUDIO_CONFIG_STEREO )
570 : {
571 3634 : ivas_ls_setup_conversion( st_ivas, audioCfg2channels( IVAS_AUDIO_CONFIG_5_1_2 ), output_frame, p_output, p_output );
572 : }
573 : }
574 : }
575 1122339 : else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
576 : {
577 : /* read Parametric MC parameters from the bitstream */
578 459633 : ivas_param_mc_dec_read_BS( ivas_total_brate, st, st_ivas->hParamMC, &nb_bits_metadata[0] );
579 :
580 459633 : if ( st_ivas->nCPE == 1 )
581 : {
582 394975 : if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
583 : {
584 0 : return error;
585 : }
586 : }
587 64658 : else if ( st_ivas->nCPE > 1 )
588 : {
589 64658 : if ( ( error = ivas_mct_dec( st_ivas, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
590 : {
591 0 : return error;
592 : }
593 : }
594 :
595 : /* HP filtering */
596 1443557 : for ( n = 0; n < st_ivas->nchan_transport; n++ )
597 : {
598 983924 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
599 : }
600 :
601 : /* Rendering */
602 459633 : if ( output_config == IVAS_AUDIO_CONFIG_MONO || output_config == IVAS_AUDIO_CONFIG_STEREO )
603 : {
604 11084 : ivas_ls_setup_conversion( st_ivas, st_ivas->nchan_transport, output_frame, p_output, p_output );
605 : }
606 : }
607 662706 : else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
608 : {
609 662706 : if ( st_ivas->hOutSetup.separateChannelEnabled )
610 : {
611 130567 : st = st_ivas->hCPE[0]->hCoreCoder[0]; /* Metadata is always with CPE in the case of separated channel */
612 : }
613 :
614 : /* read McMASA parameters from the bitstream */
615 662706 : if ( ( error = ivas_masa_decode( st_ivas, st, &nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
616 : {
617 0 : return error;
618 : }
619 :
620 662706 : if ( st_ivas->hOutSetup.separateChannelEnabled )
621 : {
622 : /* Decode the transport audio signals */
623 130567 : if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
624 : {
625 0 : return error;
626 : }
627 :
628 : /* Identify the index of the separated channel, always LFE_CHANNEL-1 here */
629 130567 : n = LFE_CHANNEL - 1;
630 :
631 : /* Decode the separated channel to output[n] to be combined with the synthesized channels */
632 130567 : if ( ( error = ivas_sce_dec( st_ivas, 0, &p_output[n], output_frame, 0 ) ) != IVAS_ERR_OK )
633 : {
634 0 : return error;
635 : }
636 :
637 : /* Delay the separated channel to sync with CLDFB delay of the DirAC synthesis, and synthesize the LFE signal. */
638 130567 : if ( output_config == IVAS_AUDIO_CONFIG_5_1 || output_config == IVAS_AUDIO_CONFIG_7_1 ||
639 100900 : output_config == IVAS_AUDIO_CONFIG_5_1_4 || output_config == IVAS_AUDIO_CONFIG_7_1_4 ||
640 97780 : 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 )
641 : {
642 42481 : ivas_lfe_synth_with_filters( st_ivas->hMasa->hMasaLfeSynth, p_output, output_frame, n, LFE_CHANNEL );
643 : }
644 88086 : else if ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.num_lfe == 0 )
645 : {
646 : /* Delay the separated channel to sync with the DirAC rendering */
647 0 : delay_signal( p_output[n], output_frame, st_ivas->hMasa->hMasaLfeSynth->delayBuffer_syncDirAC, st_ivas->hMasa->hMasaLfeSynth->delayBuffer_syncDirAC_size );
648 : }
649 : }
650 : else
651 : {
652 532139 : if ( st_ivas->nSCE == 1 )
653 : {
654 451434 : if ( ( error = ivas_sce_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
655 : {
656 0 : return error;
657 : }
658 : }
659 80705 : else if ( st_ivas->nCPE == 1 )
660 : {
661 80705 : if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
662 : {
663 0 : return error;
664 : }
665 : }
666 : }
667 :
668 662706 : if ( st_ivas->sba_dirac_stereo_flag ) /* use the flag to trigger the DFT upmix */
669 : {
670 62597 : ivas_sba_dirac_stereo_dec( st_ivas, p_output, output_frame );
671 : }
672 :
673 : /* HP filtering */
674 1729848 : for ( n = 0; n < getNumChanSynthesis( st_ivas ); n++ )
675 : {
676 1067142 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
677 : }
678 :
679 662706 : if ( st_ivas->renderer_type == RENDERER_MCMASA_MONO_STEREO )
680 : {
681 86482 : ivas_mono_stereo_downmix_mcmasa( st_ivas, p_output, output_frame );
682 : }
683 : }
684 : }
685 :
686 : /*----------------------------------------------------------------*
687 : * Common updates
688 : *----------------------------------------------------------------*/
689 :
690 21654669 : if ( !st_ivas->bfi ) /* do not update if first frame(s) are lost or NO_DATA */
691 : {
692 20456056 : st_ivas->hDecoderConfig->last_ivas_total_brate = ivas_total_brate;
693 20456056 : st_ivas->last_active_ivas_total_brate = ( ivas_total_brate <= IVAS_SID_5k2 ) ? st_ivas->last_active_ivas_total_brate : ivas_total_brate;
694 : }
695 :
696 21654669 : 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 */
697 : {
698 5379642 : st_ivas->ini_frame++;
699 : }
700 :
701 21654669 : 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 */
702 : {
703 5377792 : st_ivas->ini_active_frame++;
704 : }
705 :
706 21654669 : st_ivas->last_ivas_format = st_ivas->ivas_format;
707 :
708 : #ifdef DEBUG_MODE_INFO
709 : dbgwrite( &st_ivas->bfi, sizeof( int16_t ), 1, output_frame, "res/bfi" );
710 : dbgwrite( &st_ivas->BER_detect, sizeof( int16_t ), 1, output_frame, "res/BER_detect" );
711 : {
712 : float tmpF = ivas_total_brate / 1000.0f;
713 : dbgwrite( &tmpF, sizeof( float ), 1, output_frame, "res/ivas_total_brate.dec" );
714 : }
715 : #endif
716 :
717 21654669 : pop_wmops();
718 21654669 : return IVAS_ERR_OK;
719 : }
720 :
721 :
722 : /*--------------------------------------------------------------------------*
723 : * ivas_dec_get_num_tc_channels()
724 : *
725 : * Get the number of channels provided to the renderer
726 : *--------------------------------------------------------------------------*/
727 :
728 461324 : int16_t ivas_dec_get_num_tc_channels(
729 : Decoder_Struct *st_ivas /* i : IVAS decoder handle */
730 : )
731 : {
732 : int16_t num_tc;
733 : int32_t ivas_total_brate;
734 : AUDIO_CONFIG output_config;
735 :
736 461324 : if ( st_ivas->renderer_type == RENDERER_DISABLE )
737 : {
738 42562 : num_tc = st_ivas->hDecoderConfig->nchan_out;
739 : }
740 : else
741 : {
742 418762 : num_tc = st_ivas->nchan_transport;
743 : }
744 :
745 461324 : output_config = st_ivas->hDecoderConfig->output_config;
746 :
747 461324 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
748 :
749 461324 : if ( st_ivas->ivas_format == MONO_FORMAT )
750 : {
751 1536 : num_tc = 1;
752 : }
753 459788 : else if ( st_ivas->ivas_format == STEREO_FORMAT && st_ivas->hDecoderConfig->nchan_out == 1 )
754 : {
755 322 : num_tc = 1;
756 : }
757 459466 : else if ( st_ivas->ivas_format == ISM_FORMAT )
758 : {
759 44872 : if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
760 : {
761 1205 : num_tc = 1;
762 : }
763 : }
764 414594 : else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
765 : {
766 148958 : if ( st_ivas->sba_dirac_stereo_flag )
767 : {
768 3514 : num_tc = CPE_CHANNELS;
769 : }
770 145444 : 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 ) ) )
771 : {
772 45088 : num_tc = 1; /* Only one channel transported */
773 : }
774 :
775 148958 : if ( st_ivas->ivas_format == MASA_FORMAT && output_config == IVAS_AUDIO_CONFIG_EXTERNAL && st_ivas->nchan_transport == 2 && ivas_total_brate < MASA_STEREO_MIN_BITRATE && ivas_total_brate > IVAS_SID_5k2 )
776 : {
777 2532 : num_tc = CPE_CHANNELS;
778 : }
779 :
780 148958 : if ( st_ivas->ivas_format == SBA_FORMAT )
781 : {
782 50938 : if ( num_tc == 3 )
783 : {
784 11146 : num_tc++;
785 : }
786 : }
787 : }
788 265636 : else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
789 : {
790 132836 : if ( output_config != IVAS_AUDIO_CONFIG_EXTERNAL )
791 : {
792 123975 : if ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
793 : {
794 72690 : num_tc++;
795 : }
796 51285 : else if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
797 : {
798 51285 : num_tc += st_ivas->nchan_ism;
799 : }
800 : }
801 : }
802 132800 : else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
803 : {
804 57656 : if ( st_ivas->sba_dirac_stereo_flag )
805 : {
806 5074 : num_tc = CPE_CHANNELS;
807 : }
808 57656 : if ( num_tc == 3 )
809 : {
810 11469 : num_tc++;
811 : }
812 57656 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
813 : {
814 25114 : num_tc += st_ivas->nchan_ism;
815 : }
816 : }
817 75144 : else if ( st_ivas->ivas_format == MC_FORMAT )
818 : {
819 72403 : if ( output_config == IVAS_AUDIO_CONFIG_MONO )
820 : {
821 775 : num_tc = 1;
822 : }
823 71628 : else if ( output_config == IVAS_AUDIO_CONFIG_STEREO )
824 : {
825 3811 : num_tc = 2;
826 : }
827 67817 : else if ( st_ivas->mc_mode == MC_MODE_MCT )
828 : {
829 : /* do all static dmx already in the TC decoder if less channels than transported... */
830 29575 : 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 ) )
831 : {
832 790 : if ( ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) >= ( st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe ) )
833 : {
834 0 : num_tc = st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe;
835 : }
836 : }
837 28785 : 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 ) )
838 : {
839 1234 : num_tc = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
840 : }
841 : }
842 38242 : else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
843 : {
844 1933 : num_tc = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
845 : }
846 36309 : else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
847 : {
848 21938 : if ( st_ivas->hOutSetup.separateChannelEnabled )
849 : {
850 4270 : num_tc++;
851 : }
852 :
853 21938 : if ( st_ivas->hOutSetup.separateChannelEnabled && ( output_config == IVAS_AUDIO_CONFIG_5_1 || output_config == IVAS_AUDIO_CONFIG_7_1 ||
854 4171 : output_config == IVAS_AUDIO_CONFIG_5_1_4 || output_config == IVAS_AUDIO_CONFIG_7_1_4 ||
855 4108 : output_config == IVAS_AUDIO_CONFIG_5_1_2 || ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.num_lfe > 0 ) ) )
856 : {
857 : /* LFE is synthesized in TD with the TCs */
858 162 : num_tc++;
859 : }
860 : }
861 : }
862 :
863 461324 : return num_tc;
864 : }
865 :
866 :
867 : /*--------------------------------------------------------------------------*
868 : * ivas_dec_get_tc_buffer_mode()
869 : *
870 : *
871 : *--------------------------------------------------------------------------*/
872 :
873 328269 : TC_BUFFER_MODE ivas_dec_get_tc_buffer_mode(
874 : Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
875 : )
876 : {
877 : TC_BUFFER_MODE buffer_mode;
878 :
879 328269 : buffer_mode = TC_BUFFER_MODE_BUFFER;
880 :
881 328269 : switch ( st_ivas->renderer_type )
882 : {
883 : /* all renderers where we are done after TC decoding (might include DMX to mono/stereo */
884 49193 : case RENDERER_DISABLE:
885 : case RENDERER_MCMASA_MONO_STEREO:
886 : case RENDERER_OSBA_STEREO:
887 : case RENDERER_MONO_DOWNMIX:
888 49193 : buffer_mode = TC_BUFFER_MODE_BUFFER;
889 49193 : break;
890 250465 : case RENDERER_NON_DIEGETIC_DOWNMIX:
891 : case RENDERER_TD_PANNING:
892 : case RENDERER_BINAURAL_OBJECTS_TD:
893 : case RENDERER_BINAURAL_FASTCONV:
894 : case RENDERER_BINAURAL_FASTCONV_ROOM:
895 : case RENDERER_BINAURAL_PARAMETRIC:
896 : case RENDERER_BINAURAL_PARAMETRIC_ROOM:
897 : case RENDERER_STEREO_PARAMETRIC:
898 : case RENDERER_DIRAC:
899 : case RENDERER_PARAM_ISM:
900 : case RENDERER_BINAURAL_MIXER_CONV:
901 : case RENDERER_BINAURAL_MIXER_CONV_ROOM:
902 : case RENDERER_OMASA_OBJECT_EXT:
903 : case RENDERER_OMASA_MIX_EXT:
904 : case RENDERER_OSBA_AMBI:
905 : case RENDERER_OSBA_LS:
906 250465 : buffer_mode = TC_BUFFER_MODE_RENDERER;
907 250465 : break;
908 : break;
909 4916 : case RENDERER_MC_PARAMMC:
910 4916 : if ( st_ivas->hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
911 : {
912 422 : buffer_mode = TC_BUFFER_MODE_BUFFER; /* TCs are already the DMX to mono or stereo */
913 : }
914 : else
915 : {
916 4494 : buffer_mode = TC_BUFFER_MODE_RENDERER;
917 : }
918 4916 : break;
919 5879 : case RENDERER_MC:
920 5879 : if ( ivas_dec_get_num_tc_channels( st_ivas ) != st_ivas->hDecoderConfig->nchan_out )
921 : {
922 4211 : buffer_mode = TC_BUFFER_MODE_RENDERER;
923 : }
924 5879 : break;
925 17816 : case RENDERER_SBA_LINEAR_ENC:
926 17816 : 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 ) )
927 : {
928 306 : buffer_mode = TC_BUFFER_MODE_BUFFER;
929 : }
930 : else
931 : {
932 17510 : buffer_mode = TC_BUFFER_MODE_RENDERER;
933 : }
934 17816 : break;
935 0 : case RENDERER_SBA_LINEAR_DEC:
936 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 ) )
937 : {
938 0 : buffer_mode = TC_BUFFER_MODE_BUFFER;
939 : }
940 : else
941 : {
942 0 : buffer_mode = TC_BUFFER_MODE_RENDERER;
943 : }
944 0 : break;
945 : #ifdef DEBUGGING
946 : default:
947 : assert( 0 );
948 : #endif
949 : }
950 :
951 328269 : return buffer_mode;
952 : }
953 :
954 :
955 : /*--------------------------------------------------------------------------*
956 : * ivas_dec_tc_audio_allocate()
957 : *
958 : * allocate and initialize TC audio buffer
959 : *--------------------------------------------------------------------------*/
960 :
961 353454 : static ivas_error ivas_dec_tc_audio_allocate(
962 : DECODER_TC_BUFFER_HANDLE hTcBuffer, /* i/o: TC buffer handle */
963 : const int32_t output_Fs, /* i : output sampling rate */
964 : const int16_t Opt_tsm /* i : TSM option flag */
965 : )
966 : {
967 : int16_t nsamp_to_allocate;
968 : int16_t ch_idx, n_samp_full, n_samp_residual, offset;
969 :
970 353454 : if ( Opt_tsm )
971 : {
972 123727 : n_samp_full = ( NS2SA( output_Fs, MAX_JBM_L_FRAME_NS ) );
973 123727 : n_samp_full = max( n_samp_full, L_FRAME48k ); /* buffers are shared between 'hTcBuffer->tc[]' and 'p_output_f[]': ensure minimal length */
974 123727 : n_samp_residual = hTcBuffer->n_samples_granularity - 1;
975 : }
976 : else
977 : {
978 229727 : n_samp_full = (int16_t) ( output_Fs / FRAMES_PER_SEC );
979 229727 : n_samp_residual = 0;
980 : }
981 :
982 353454 : nsamp_to_allocate = max( hTcBuffer->nchan_transport_rend, hTcBuffer->nchan_buffer_full ) * n_samp_full;
983 :
984 353454 : if ( Opt_tsm )
985 : {
986 : /* note: this is stack memory buffer for TC decoded and also time-scale modified audio signals */
987 123727 : if ( ( hTcBuffer->tc_buffer = (float *) malloc( nsamp_to_allocate * sizeof( float ) ) ) == NULL )
988 : {
989 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TC Buffer\n" ) );
990 : }
991 123727 : set_zero( hTcBuffer->tc_buffer, nsamp_to_allocate );
992 :
993 123727 : offset = 0;
994 597225 : for ( ch_idx = 0; ch_idx < max( hTcBuffer->nchan_transport_rend, hTcBuffer->nchan_buffer_full ); ch_idx++ )
995 : {
996 473498 : hTcBuffer->tc[ch_idx] = &hTcBuffer->tc_buffer[offset];
997 473498 : offset += n_samp_full;
998 : }
999 1629861 : for ( ; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
1000 : {
1001 1506134 : hTcBuffer->tc[ch_idx] = NULL;
1002 : }
1003 :
1004 : /* memory buffer for TC audio samples not rendered in the previous frame */
1005 597225 : for ( ch_idx = 0; ch_idx < hTcBuffer->nchan_transport_internal; ch_idx++ )
1006 : {
1007 473498 : if ( ( hTcBuffer->tc_buffer_old[ch_idx] = (float *) malloc( n_samp_residual * sizeof( float ) ) ) == NULL )
1008 : {
1009 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TC Buffer\n" ) );
1010 : }
1011 473498 : set_zero( hTcBuffer->tc_buffer_old[ch_idx], n_samp_residual );
1012 : }
1013 1629861 : for ( ; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
1014 : {
1015 1506134 : hTcBuffer->tc_buffer_old[ch_idx] = NULL;
1016 : }
1017 : }
1018 : else
1019 : {
1020 229727 : hTcBuffer->tc_buffer = NULL;
1021 :
1022 3905359 : for ( ch_idx = 0; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
1023 : {
1024 3675632 : hTcBuffer->tc[ch_idx] = NULL;
1025 : }
1026 :
1027 3905359 : for ( ch_idx = 0; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
1028 : {
1029 3675632 : hTcBuffer->tc_buffer_old[ch_idx] = NULL;
1030 : }
1031 : }
1032 :
1033 353454 : hTcBuffer->tc_buffer2 = NULL;
1034 :
1035 353454 : return IVAS_ERR_OK;
1036 : }
1037 :
1038 :
1039 : /*--------------------------------------------------------------------------*
1040 : * ivas_dec_tc_buffer_open()
1041 : *
1042 : * Open and initialize transport channel buffer handle
1043 : *--------------------------------------------------------------------------*/
1044 :
1045 90741 : ivas_error ivas_dec_tc_buffer_open(
1046 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
1047 : const TC_BUFFER_MODE tc_buffer_mode, /* i : buffer mode */
1048 : const int16_t nchan_transport_rend, /* i : number of TCs for rendering */
1049 : const int16_t nchan_transport_internal, /* i : number of totally buffered channels */
1050 : const int16_t nchan_full, /* i : number of channels to fully store */
1051 : const int16_t n_samples_granularity /* i : granularity of the renderer/buffer */
1052 : )
1053 : {
1054 : DECODER_TC_BUFFER_HANDLE hTcBuffer;
1055 : int16_t nMaxSlotsPerSubframe;
1056 : ivas_error error;
1057 :
1058 : /*-----------------------------------------------------------------*
1059 : * prepare library opening
1060 : *-----------------------------------------------------------------*/
1061 :
1062 90741 : if ( ( hTcBuffer = (DECODER_TC_BUFFER_HANDLE) malloc( sizeof( DECODER_TC_BUFFER ) ) ) == NULL )
1063 : {
1064 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TC Buffer\n" ) );
1065 : }
1066 :
1067 90741 : hTcBuffer->tc_buffer_mode = tc_buffer_mode;
1068 90741 : hTcBuffer->nchan_transport_rend = nchan_transport_rend;
1069 90741 : hTcBuffer->nchan_transport_internal = nchan_transport_internal;
1070 90741 : hTcBuffer->nchan_buffer_full = nchan_full;
1071 90741 : hTcBuffer->n_samples_granularity = n_samples_granularity;
1072 90741 : hTcBuffer->n_samples_available = 0;
1073 90741 : hTcBuffer->n_samples_buffered = 0;
1074 90741 : hTcBuffer->n_samples_rendered = 0;
1075 90741 : hTcBuffer->slots_rendered = 0;
1076 90741 : hTcBuffer->subframes_rendered = 0;
1077 90741 : hTcBuffer->n_samples_discard = 0;
1078 90741 : hTcBuffer->n_samples_flushed = 0;
1079 90741 : hTcBuffer->nb_subframes = MAX_PARAM_SPATIAL_SUBFRAMES;
1080 :
1081 90741 : nMaxSlotsPerSubframe = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / hTcBuffer->n_samples_granularity;
1082 90741 : hTcBuffer->num_slots = nMaxSlotsPerSubframe * MAX_PARAM_SPATIAL_SUBFRAMES;
1083 90741 : set_s( hTcBuffer->subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
1084 90741 : set_s( hTcBuffer->subframe_nbslots, nMaxSlotsPerSubframe, MAX_PARAM_SPATIAL_SUBFRAMES );
1085 :
1086 90741 : if ( ( error = ivas_dec_tc_audio_allocate( hTcBuffer, st_ivas->hDecoderConfig->output_Fs, st_ivas->hDecoderConfig->Opt_tsm ) ) != IVAS_ERR_OK )
1087 : {
1088 0 : return error;
1089 : }
1090 :
1091 90741 : st_ivas->hTcBuffer = hTcBuffer;
1092 :
1093 90741 : return IVAS_ERR_OK;
1094 : }
1095 :
1096 :
1097 : /*--------------------------------------------------------------------------*
1098 : * ivas_dec_tc_audio_deallocate()
1099 : *
1100 : * deallocate TC audio buffer
1101 : *--------------------------------------------------------------------------*/
1102 :
1103 353454 : static void ivas_dec_tc_audio_deallocate(
1104 : DECODER_TC_BUFFER_HANDLE hTcBuffer /* i/o: TC buffer handle */
1105 : )
1106 : {
1107 : int16_t ch_idx;
1108 :
1109 353454 : if ( hTcBuffer != NULL )
1110 : {
1111 353454 : if ( hTcBuffer->tc_buffer != NULL )
1112 : {
1113 2103359 : for ( ch_idx = 0; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
1114 : {
1115 1979632 : hTcBuffer->tc[ch_idx] = NULL;
1116 : }
1117 :
1118 123727 : free( hTcBuffer->tc_buffer );
1119 123727 : hTcBuffer->tc_buffer = NULL;
1120 : }
1121 :
1122 6008718 : for ( ch_idx = 0; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
1123 : {
1124 5655264 : if ( hTcBuffer->tc_buffer_old[ch_idx] != NULL )
1125 : {
1126 473498 : free( hTcBuffer->tc_buffer_old[ch_idx] );
1127 473498 : hTcBuffer->tc_buffer_old[ch_idx] = NULL;
1128 : }
1129 : }
1130 :
1131 353454 : if ( hTcBuffer->tc_buffer2 != NULL )
1132 : {
1133 295888 : free( hTcBuffer->tc_buffer2 );
1134 295888 : hTcBuffer->tc_buffer2 = NULL;
1135 : }
1136 : }
1137 :
1138 353454 : return;
1139 : }
1140 :
1141 :
1142 : /*--------------------------------------------------------------------------*
1143 : * ivas_dec_tc_buffer_close()
1144 : *
1145 : * Close transport channel buffer handle
1146 : *--------------------------------------------------------------------------*/
1147 :
1148 90769 : void ivas_dec_tc_buffer_close(
1149 : DECODER_TC_BUFFER_HANDLE *phTcBuffer /* i/o: TC buffer handle */
1150 : )
1151 : {
1152 90769 : if ( *phTcBuffer != NULL )
1153 : {
1154 90741 : ivas_dec_tc_audio_deallocate( *phTcBuffer );
1155 :
1156 90741 : free( *phTcBuffer );
1157 90741 : *phTcBuffer = NULL;
1158 : }
1159 :
1160 90769 : return;
1161 : }
1162 :
1163 :
1164 : /*--------------------------------------------------------------------------*
1165 : * ivas_dec_tc_buffer_reconfigure()
1166 : *
1167 : * Reconfigure transport channel buffer handle
1168 : *--------------------------------------------------------------------------*/
1169 :
1170 262713 : ivas_error ivas_dec_tc_buffer_reconfigure(
1171 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
1172 : const TC_BUFFER_MODE tc_buffer_mode, /* i : new buffer mode */
1173 : const int16_t nchan_transport_rend, /* i : new number of TCs for rendering */
1174 : const int16_t nchan_transport_internal, /* i : new number of totally buffered channels */
1175 : const int16_t nchan_full, /* i : new number of channels to fully store */
1176 : const int16_t n_samples_granularity /* i : new granularity of the renderer/buffer */
1177 : )
1178 : {
1179 : int16_t ch_idx, num_tc_buffer_mem, n_samples_still_available;
1180 : float tc_buffer_mem[MAX_INTERN_CHANNELS][L_FRAME48k / MAX_PARAM_SPATIAL_SUBFRAMES - 1];
1181 : ivas_error error;
1182 : DECODER_TC_BUFFER_HANDLE hTcBuffer;
1183 :
1184 262713 : hTcBuffer = st_ivas->hTcBuffer;
1185 :
1186 262713 : num_tc_buffer_mem = 0;
1187 262713 : n_samples_still_available = 0;
1188 :
1189 262713 : if ( st_ivas->hDecoderConfig->Opt_tsm )
1190 : {
1191 : /* save samples of the TC buffer from the previous frame */
1192 84293 : num_tc_buffer_mem = min( hTcBuffer->nchan_transport_internal, nchan_transport_internal );
1193 84293 : n_samples_still_available = hTcBuffer->n_samples_buffered - hTcBuffer->n_samples_rendered;
1194 :
1195 : /* what is remaining from last frame needs always be smaller than the new granularity */
1196 84293 : assert( n_samples_still_available < n_samples_granularity );
1197 :
1198 321182 : for ( ch_idx = 0; ch_idx < num_tc_buffer_mem; ch_idx++ )
1199 : {
1200 236889 : mvr2r( hTcBuffer->tc_buffer_old[ch_idx] + hTcBuffer->n_samples_flushed, tc_buffer_mem[ch_idx], n_samples_still_available );
1201 : }
1202 : }
1203 :
1204 : /* if granularity changes, adapt subframe_nb_slots */
1205 262713 : if ( n_samples_granularity != hTcBuffer->n_samples_granularity )
1206 : {
1207 : #ifdef DEBUGGING
1208 : int16_t nMaxSlotsPerSubframeOld;
1209 : #endif
1210 : int16_t nMaxSlotsPerSubframeNew;
1211 :
1212 59919 : nMaxSlotsPerSubframeNew = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / n_samples_granularity;
1213 : #ifdef DEBUGGING
1214 : nMaxSlotsPerSubframeOld = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / st_ivas->hTcBuffer->n_samples_granularity;
1215 : assert( hTcBuffer->subframe_nbslots[hTcBuffer->subframes_rendered - 1] == nMaxSlotsPerSubframeOld );
1216 : if ( n_samples_granularity < hTcBuffer->n_samples_granularity )
1217 : {
1218 : assert( ( hTcBuffer->n_samples_granularity % n_samples_granularity ) == 0 );
1219 : }
1220 : else
1221 : {
1222 : assert( ( n_samples_granularity % hTcBuffer->n_samples_granularity ) == 0 );
1223 : }
1224 : #endif
1225 : /* if samples were flushed, take that into account here */
1226 59919 : if ( n_samples_granularity < hTcBuffer->n_samples_granularity && hTcBuffer->n_samples_flushed > 0 )
1227 : {
1228 5188 : hTcBuffer->subframe_nbslots[hTcBuffer->subframes_rendered - 1] = hTcBuffer->n_samples_flushed / n_samples_granularity;
1229 5188 : hTcBuffer->n_samples_flushed = 0;
1230 : }
1231 : else
1232 : {
1233 54731 : hTcBuffer->subframe_nbslots[hTcBuffer->subframes_rendered - 1] = nMaxSlotsPerSubframeNew;
1234 : }
1235 : }
1236 :
1237 262713 : hTcBuffer->tc_buffer_mode = tc_buffer_mode;
1238 262713 : hTcBuffer->nchan_transport_rend = nchan_transport_rend;
1239 262713 : hTcBuffer->nchan_transport_internal = nchan_transport_internal;
1240 262713 : hTcBuffer->nchan_buffer_full = nchan_full;
1241 262713 : hTcBuffer->n_samples_granularity = n_samples_granularity;
1242 :
1243 : /* reallocate TC audio buffers */
1244 :
1245 262713 : ivas_dec_tc_audio_deallocate( hTcBuffer );
1246 :
1247 262713 : if ( ( error = ivas_dec_tc_audio_allocate( hTcBuffer, st_ivas->hDecoderConfig->output_Fs, st_ivas->hDecoderConfig->Opt_tsm ) ) != IVAS_ERR_OK )
1248 : {
1249 0 : return error;
1250 : }
1251 :
1252 : /* propagate samples of the TC buffer from the previous frame */
1253 499602 : for ( ch_idx = 0; ch_idx < num_tc_buffer_mem; ch_idx++ )
1254 : {
1255 236889 : mvr2r( tc_buffer_mem[ch_idx], hTcBuffer->tc_buffer_old[ch_idx], n_samples_still_available );
1256 : }
1257 :
1258 262713 : return IVAS_ERR_OK;
1259 : }
|