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 710925 : 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 710925 : push_wmops( "ivas_dec" );
70 :
71 : /*----------------------------------------------------------------*
72 : * Initialization of local vars after struct has been set
73 : *----------------------------------------------------------------*/
74 :
75 710925 : output_Fs = st_ivas->hDecoderConfig->output_Fs;
76 710925 : nchan_out = st_ivas->hTcBuffer->nchan_transport_rend;
77 710925 : output_config = st_ivas->hDecoderConfig->output_config;
78 710925 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
79 :
80 710925 : output_frame = (int16_t) ( output_Fs / FRAMES_PER_SEC );
81 :
82 : /* set pointers to transport channels audio */
83 9242025 : for ( n = 0; n < MAX_TRANSPORT_CHANNELS; n++ )
84 : {
85 8531100 : p_output[n] = st_ivas->p_output_f[n];
86 8531100 : if ( p_output[n] != NULL )
87 : {
88 4305013 : set_zero( p_output[n], L_FRAME48k );
89 : }
90 : }
91 :
92 : /*----------------------------------------------------------------*
93 : * Decoding + pre-rendering
94 : *----------------------------------------------------------------*/
95 :
96 710925 : if ( st_ivas->ivas_format == STEREO_FORMAT )
97 : {
98 74556 : st_ivas->hCPE[0]->element_brate = ivas_total_brate;
99 74556 : 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 199783 : for ( n = 0; n < min( nchan_out, st_ivas->nchan_transport ); n++ )
106 : {
107 125227 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
108 : }
109 : }
110 636369 : else if ( st_ivas->ivas_format == ISM_FORMAT )
111 : {
112 : /* Metadata decoding and configuration */
113 114373 : if ( ivas_total_brate == IVAS_SID_5k2 || ivas_total_brate == FRAME_NO_DATA )
114 : {
115 2133 : 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 2133 : 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 2133 : ivas_ism_dtx_limit_noise_energy_for_near_silence( st_ivas->hSCE, st_ivas->hISMDTX.sce_id_dtx, st_ivas->nchan_transport );
124 :
125 2133 : ivas_param_ism_dec_dequant_md( st_ivas );
126 : }
127 112240 : else if ( st_ivas->ism_mode == ISM_MODE_PARAM )
128 : {
129 20688 : 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 20688 : ivas_param_ism_dec_dequant_md( st_ivas );
135 : }
136 : else /* ISM_MODE_DISC */
137 : {
138 91552 : 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 424926 : for ( n = 0; n < st_ivas->nchan_transport; n++ )
145 : {
146 : /* for DTX frames, dominant object has already been decoded before */
147 310553 : if ( !( ( ivas_total_brate == IVAS_SID_5k2 || ivas_total_brate == FRAME_NO_DATA ) && n == st_ivas->hISMDTX.sce_id_dtx ) )
148 : {
149 308420 : 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 310553 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
157 : }
158 :
159 114373 : if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
160 : {
161 3969 : ivas_ism_mono_dmx( st_ivas, p_output, output_frame );
162 : }
163 110404 : 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 11255 : ivas_dirac_dec_binaural_sba_gain( p_output, st_ivas->nchan_transport, output_frame );
167 : }
168 : }
169 521996 : else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
170 : {
171 283257 : set_s( nb_bits_metadata, 0, MAX_SCE );
172 :
173 : /* read parameters from the bitstream */
174 283257 : if ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->hQMetaData != NULL )
175 : {
176 112511 : st = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[0]->hCoreCoder[0] : st_ivas->hCPE[0]->hCoreCoder[0];
177 :
178 112511 : if ( ( error = ivas_masa_decode( st_ivas, st, &nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
179 : {
180 0 : return error;
181 : }
182 : }
183 170746 : else if ( st_ivas->ivas_format == SBA_FORMAT )
184 : {
185 170746 : if ( ( error = ivas_spar_dec( st_ivas, nb_bits_metadata ) ) != IVAS_ERR_OK )
186 : {
187 0 : return error;
188 : }
189 : }
190 :
191 283257 : if ( st_ivas->nchan_transport == CPE_CHANNELS && st_ivas->nCPE >= 1 )
192 : {
193 124420 : st_ivas->hCPE[0]->brate_surplus = 0;
194 124420 : st_ivas->hCPE[0]->element_brate = ivas_total_brate;
195 : }
196 :
197 : /* core-decoding of transport channels */
198 283257 : if ( st_ivas->nSCE == 1 )
199 : {
200 76481 : 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 206776 : else if ( st_ivas->nCPE == 1 )
206 : {
207 124420 : 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 82356 : else if ( st_ivas->nCPE > 1 )
213 : {
214 82356 : 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 283257 : nchan_remapped = st_ivas->nchan_transport;
226 283257 : if ( st_ivas->sba_dirac_stereo_flag )
227 : {
228 6660 : nchan_remapped = nchan_out;
229 :
230 6660 : if ( st_ivas->ivas_format == SBA_FORMAT )
231 : {
232 6660 : 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 6660 : if ( st_ivas->hSpar->hPCA != NULL )
235 : {
236 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 );
237 : }
238 :
239 6660 : 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 6660 : ivas_sba_dirac_stereo_dec( st_ivas, p_output, output_frame );
243 : }
244 276597 : 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 27111 : nchan_remapped = 1; /* Only one channel transported */
247 : }
248 :
249 : /* HP filtering */
250 : #ifndef DEBUG_SPAR_BYPASS_EVS_CODEC
251 888196 : for ( n = 0; n < nchan_remapped; n++ )
252 : {
253 604939 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
254 : }
255 : #endif
256 :
257 283257 : if ( st_ivas->ivas_format == SBA_FORMAT )
258 : {
259 170746 : nchan_remapped = ivas_sba_remapTCs( p_output, st_ivas, output_frame );
260 :
261 170746 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
262 : {
263 47348 : 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 47348 : ivas_sba_mix_matrix_determiner( st_ivas->hSpar, p_output, st_ivas->bfi, nchan_remapped, output_frame, num_md_sub_frames );
265 : }
266 123398 : else if ( st_ivas->renderer_type != RENDERER_DISABLE )
267 : {
268 115099 : ivas_spar_dec_agc_pca( st_ivas, p_output, output_frame );
269 : }
270 : }
271 :
272 283257 : if ( st_ivas->ivas_format == MASA_FORMAT )
273 : {
274 112511 : ivas_masa_prerender( st_ivas, p_output, output_frame, nchan_remapped );
275 :
276 : /* external output */
277 112511 : if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL && st_ivas->hMasa->config.input_ivas_format == MASA_ISM_FORMAT )
278 : {
279 1250 : for ( n = 0; n < st_ivas->nchan_ism; n++ )
280 : {
281 1000 : set_zero( p_output[st_ivas->nchan_transport + n], output_frame );
282 : }
283 :
284 250 : ivas_omasa_rearrange_channels( p_output, st_ivas->nchan_ism, output_frame );
285 : }
286 : }
287 170746 : 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 47348 : ivas_dirac_dec_binaural_sba_gain( p_output, nchan_remapped, output_frame );
291 : }
292 : }
293 238739 : 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 62384 : set_s( nb_bits_metadata, 0, MAX_SCE + 1 );
299 :
300 : /* Set the number of objects for the parametric rendering */
301 62384 : dirac_bs_md_write_idx = 0;
302 62384 : if ( st_ivas->hSpatParamRendCom != NULL )
303 : {
304 54369 : st_ivas->hSpatParamRendCom->numIsmDirections = 0;
305 54369 : if ( st_ivas->ism_mode != ISM_MASA_MODE_DISC && st_ivas->ism_mode != ISM_MASA_MODE_MASA_ONE_OBJ )
306 : {
307 11750 : st_ivas->hSpatParamRendCom->numIsmDirections = st_ivas->nchan_ism;
308 : }
309 :
310 54369 : 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 62384 : 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 62384 : ivas_set_surplus_brate_dec( st_ivas, &ism_total_brate );
321 :
322 62384 : 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 62384 : 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 166455 : for ( n = 0; n < nchan_transport_ism; n++ )
332 : {
333 104071 : 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 62384 : 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 62384 : if ( st_ivas->hCPE[0]->nchan_out == 1 )
346 : {
347 16634 : mvr2r( p_output[0], p_output[1], output_frame ); /* Copy mono signal to stereo output channels */
348 : }
349 :
350 : /* HP filtering */
351 291223 : for ( n = 0; n < getNumChanSynthesis( st_ivas ); n++ )
352 : {
353 228839 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
354 : }
355 :
356 62384 : if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
357 : {
358 4513 : ivas_ism_mono_dmx( st_ivas, p_output, output_frame );
359 : }
360 57871 : else if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
361 : {
362 3754 : if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
363 : {
364 3252 : ivas_omasa_rearrange_channels( p_output, nchan_transport_ism, output_frame );
365 : }
366 502 : 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 250 : ivas_omasa_combine_separate_ism_with_masa( st_ivas, p_output, st_ivas->nchan_ism, output_frame );
370 : }
371 252 : 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 252 : ivas_omasa_render_objects_from_mix( st_ivas, p_output, st_ivas->nchan_ism, output_frame );
375 : }
376 : }
377 : }
378 176355 : else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
379 : {
380 : int16_t nchan_ism, sba_ch_idx;
381 :
382 72143 : set_s( nb_bits_metadata, 0, MAX_SCE + 1 );
383 72143 : nchan_ism = st_ivas->nchan_ism;
384 72143 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
385 : {
386 : /* set ISM parameters and decode ISM metadata in OSBA format */
387 56618 : 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 56618 : sba_ch_idx = st_ivas->nchan_ism;
392 : }
393 : else
394 : {
395 15525 : nb_bits_metadata[1] += NO_BITS_MASA_ISM_NO_OBJ;
396 15525 : sba_ch_idx = 0;
397 : }
398 :
399 : /* SBA metadata decoding */
400 72143 : if ( ( error = ivas_spar_dec( st_ivas, nb_bits_metadata ) ) != IVAS_ERR_OK )
401 : {
402 0 : return error;
403 : }
404 :
405 72143 : if ( st_ivas->nchan_transport == CPE_CHANNELS && st_ivas->nCPE >= 1 )
406 : {
407 6527 : st_ivas->hCPE[0]->element_brate = ivas_total_brate;
408 : }
409 :
410 : /* core-decoding of transport channels */
411 72143 : if ( st_ivas->nSCE == 1 )
412 : {
413 7339 : 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 64804 : else if ( st_ivas->nCPE == 1 )
419 : {
420 6527 : 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 58277 : else if ( st_ivas->nCPE > 1 )
426 : {
427 58277 : 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 72143 : if ( st_ivas->sba_dirac_stereo_flag )
434 : {
435 3000 : 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 3000 : if ( st_ivas->hSpar->hPCA != NULL )
438 : {
439 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] );
440 : }
441 :
442 3000 : 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 3000 : ivas_sba_dirac_stereo_dec( st_ivas, &p_output[sba_ch_idx], output_frame );
445 : }
446 :
447 : /* HP filtering */
448 465222 : for ( n = 0; n < getNumChanSynthesis( st_ivas ); n++ )
449 : {
450 393079 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
451 : }
452 :
453 72143 : 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 72143 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
470 : {
471 8805 : 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 8805 : 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 63338 : else if ( st_ivas->renderer_type != RENDERER_DISABLE && !st_ivas->sba_dirac_stereo_flag )
476 : {
477 59338 : ivas_spar_dec_agc_pca( st_ivas, &p_output[sba_ch_idx], output_frame );
478 : }
479 :
480 72143 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
481 : {
482 : /* loudness correction */
483 8805 : ivas_dirac_dec_binaural_sba_gain( &p_output[sba_ch_idx], nchan_remapped, output_frame );
484 : }
485 63338 : else if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX && st_ivas->ism_mode == ISM_SBA_MODE_DISC )
486 : {
487 1000 : ivas_ism_mono_dmx( st_ivas, p_output, output_frame );
488 :
489 : /* add W */
490 2000 : for ( n = 0; n < nchan_out; n++ )
491 : {
492 1000 : v_add( p_output[n], p_output[n + max( nchan_out, nchan_ism )], p_output[n], output_frame );
493 : }
494 : }
495 : }
496 104212 : else if ( st_ivas->ivas_format == MC_FORMAT )
497 : {
498 104212 : st = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[0]->hCoreCoder[0] : st_ivas->hCPE[0]->hCoreCoder[0];
499 :
500 104212 : if ( st_ivas->mc_mode == MC_MODE_MCT )
501 : {
502 : /* LFE channel decoder */
503 70686 : ivas_lfe_dec( st_ivas->hLFE, st, output_frame, st_ivas->bfi, p_output[LFE_CHANNEL] );
504 :
505 70686 : 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 540474 : for ( n = 0; n < st_ivas->nchan_transport; n++ )
512 : {
513 469788 : if ( n != LFE_CHANNEL )
514 : {
515 399102 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
516 : }
517 : }
518 :
519 70686 : 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 5648 : 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 70686 : 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 3029 : if ( st_ivas->renderer_type == RENDERER_MC )
530 : {
531 3029 : ivas_ls_setup_conversion( st_ivas, st_ivas->nchan_transport, output_frame, p_output, p_output );
532 : }
533 0 : else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
534 : {
535 0 : ivas_mc2sba( st_ivas->hIntSetup, p_output, p_output, output_frame, st_ivas->hOutSetup.ambisonics_order, 0.f );
536 : }
537 : }
538 : }
539 33526 : else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
540 : {
541 : /* LFE channel decoder */
542 5924 : ivas_lfe_dec( st_ivas->hLFE, st, output_frame, st_ivas->bfi, p_output[LFE_CHANNEL] );
543 :
544 5924 : ivas_mc_paramupmix_dec_read_BS( st_ivas, st, st_ivas->hMCParamUpmix, &nb_bits_metadata[0] );
545 :
546 5924 : 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 53316 : for ( n = 0; n < st_ivas->nchan_transport; n++ )
553 : {
554 47392 : if ( n != LFE_CHANNEL )
555 : {
556 41468 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
557 : }
558 : }
559 :
560 : /* Rendering */
561 5924 : 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 50 : for ( n = 4; n < 8; n++ )
565 : {
566 40 : v_multc( p_output[n], 2.0f, p_output[n], output_frame );
567 : }
568 :
569 10 : if ( output_config == IVAS_AUDIO_CONFIG_MONO || output_config == IVAS_AUDIO_CONFIG_STEREO )
570 : {
571 10 : ivas_ls_setup_conversion( st_ivas, audioCfg2channels( IVAS_AUDIO_CONFIG_5_1_2 ), output_frame, p_output, p_output );
572 : }
573 : }
574 : }
575 27602 : else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
576 : {
577 : /* read Parametric MC parameters from the bitstream */
578 15727 : ivas_param_mc_dec_read_BS( ivas_total_brate, st, st_ivas->hParamMC, &nb_bits_metadata[0] );
579 :
580 15727 : if ( st_ivas->nCPE == 1 )
581 : {
582 10483 : 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 5244 : else if ( st_ivas->nCPE > 1 )
588 : {
589 5244 : 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 52425 : for ( n = 0; n < st_ivas->nchan_transport; n++ )
597 : {
598 36698 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
599 : }
600 :
601 : /* Rendering */
602 15727 : if ( output_config == IVAS_AUDIO_CONFIG_MONO || output_config == IVAS_AUDIO_CONFIG_STEREO )
603 : {
604 20 : ivas_ls_setup_conversion( st_ivas, st_ivas->nchan_transport, output_frame, p_output, p_output );
605 : }
606 : }
607 11875 : else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
608 : {
609 11875 : if ( st_ivas->hOutSetup.separateChannelEnabled )
610 : {
611 623 : 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 11875 : if ( ( error = ivas_masa_decode( st_ivas, st, &nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
616 : {
617 0 : return error;
618 : }
619 :
620 11875 : if ( st_ivas->hOutSetup.separateChannelEnabled )
621 : {
622 : /* Decode the transport audio signals */
623 623 : 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 623 : n = LFE_CHANNEL - 1;
630 :
631 : /* Decode the separated channel to output[n] to be combined with the synthesized channels */
632 623 : 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 623 : if ( output_config == IVAS_AUDIO_CONFIG_5_1 || output_config == IVAS_AUDIO_CONFIG_7_1 ||
639 474 : output_config == IVAS_AUDIO_CONFIG_5_1_4 || output_config == IVAS_AUDIO_CONFIG_7_1_4 ||
640 474 : 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 194 : ivas_lfe_synth_with_filters( st_ivas->hMasa->hMasaLfeSynth, p_output, output_frame, n, LFE_CHANNEL );
643 : }
644 429 : 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 11252 : if ( st_ivas->nSCE == 1 )
653 : {
654 10832 : 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 420 : else if ( st_ivas->nCPE == 1 )
660 : {
661 420 : 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 11875 : if ( st_ivas->sba_dirac_stereo_flag ) /* use the flag to trigger the DFT upmix */
669 : {
670 1178 : ivas_sba_dirac_stereo_dec( st_ivas, p_output, output_frame );
671 : }
672 :
673 : /* HP filtering */
674 26594 : for ( n = 0; n < getNumChanSynthesis( st_ivas ); n++ )
675 : {
676 14719 : hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
677 : }
678 :
679 11875 : if ( st_ivas->renderer_type == RENDERER_MCMASA_MONO_STEREO )
680 : {
681 499 : ivas_mono_stereo_downmix_mcmasa( st_ivas, p_output, output_frame );
682 : }
683 : }
684 : }
685 :
686 : /*----------------------------------------------------------------*
687 : * Common updates
688 : *----------------------------------------------------------------*/
689 :
690 710925 : if ( !st_ivas->bfi ) /* do not update if first frame(s) are lost or NO_DATA */
691 : {
692 691776 : st_ivas->hDecoderConfig->last_ivas_total_brate = ivas_total_brate;
693 691776 : 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 710925 : 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 303781 : st_ivas->ini_frame++;
699 : }
700 :
701 710925 : 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 303367 : st_ivas->ini_active_frame++;
704 : }
705 :
706 710925 : 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 710925 : pop_wmops();
718 710925 : 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 15517 : 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 15517 : if ( st_ivas->renderer_type == RENDERER_DISABLE )
737 : {
738 1365 : num_tc = st_ivas->hDecoderConfig->nchan_out;
739 : }
740 : else
741 : {
742 14152 : num_tc = st_ivas->nchan_transport;
743 : }
744 :
745 15517 : output_config = st_ivas->hDecoderConfig->output_config;
746 :
747 15517 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
748 :
749 15517 : if ( st_ivas->ivas_format == MONO_FORMAT )
750 : {
751 5 : num_tc = 1;
752 : }
753 15512 : else if ( st_ivas->ivas_format == STEREO_FORMAT && st_ivas->hDecoderConfig->nchan_out == 1 )
754 : {
755 22 : num_tc = 1;
756 : }
757 15490 : else if ( st_ivas->ivas_format == ISM_FORMAT )
758 : {
759 1544 : if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
760 : {
761 2 : num_tc = 1;
762 : }
763 : }
764 13946 : else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
765 : {
766 5571 : if ( st_ivas->sba_dirac_stereo_flag )
767 : {
768 124 : num_tc = CPE_CHANNELS;
769 : }
770 5447 : 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 1963 : num_tc = 1; /* Only one channel transported */
773 : }
774 :
775 5571 : 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 107 : num_tc = CPE_CHANNELS;
778 : }
779 :
780 5571 : if ( st_ivas->ivas_format == SBA_FORMAT )
781 : {
782 1565 : if ( num_tc == 3 )
783 : {
784 298 : num_tc++;
785 : }
786 : }
787 : }
788 8375 : else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
789 : {
790 5733 : if ( output_config != IVAS_AUDIO_CONFIG_EXTERNAL )
791 : {
792 5355 : if ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
793 : {
794 3238 : num_tc++;
795 : }
796 2117 : else if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
797 : {
798 2117 : num_tc += st_ivas->nchan_ism;
799 : }
800 : }
801 : }
802 2642 : else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
803 : {
804 918 : if ( st_ivas->sba_dirac_stereo_flag )
805 : {
806 94 : num_tc = CPE_CHANNELS;
807 : }
808 918 : if ( num_tc == 3 )
809 : {
810 186 : num_tc++;
811 : }
812 918 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
813 : {
814 446 : num_tc += st_ivas->nchan_ism;
815 : }
816 : }
817 1724 : else if ( st_ivas->ivas_format == MC_FORMAT )
818 : {
819 1676 : if ( output_config == IVAS_AUDIO_CONFIG_MONO )
820 : {
821 5 : num_tc = 1;
822 : }
823 1671 : else if ( output_config == IVAS_AUDIO_CONFIG_STEREO )
824 : {
825 26 : num_tc = 2;
826 : }
827 1645 : 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 878 : 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 43 : 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 835 : 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 0 : num_tc = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
840 : }
841 : }
842 767 : else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
843 : {
844 45 : num_tc = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
845 : }
846 722 : else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
847 : {
848 322 : if ( st_ivas->hOutSetup.separateChannelEnabled )
849 : {
850 21 : num_tc++;
851 : }
852 :
853 322 : if ( st_ivas->hOutSetup.separateChannelEnabled && ( output_config == IVAS_AUDIO_CONFIG_5_1 || output_config == IVAS_AUDIO_CONFIG_7_1 ||
854 20 : output_config == IVAS_AUDIO_CONFIG_5_1_4 || output_config == IVAS_AUDIO_CONFIG_7_1_4 ||
855 20 : 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 1 : num_tc++;
859 : }
860 : }
861 : }
862 :
863 15517 : return num_tc;
864 : }
865 :
866 :
867 : /*--------------------------------------------------------------------------*
868 : * ivas_dec_get_tc_buffer_mode()
869 : *
870 : *
871 : *--------------------------------------------------------------------------*/
872 :
873 12356 : 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 12356 : buffer_mode = TC_BUFFER_MODE_BUFFER;
880 :
881 12356 : switch ( st_ivas->renderer_type )
882 : {
883 : /* all renderers where we are done after TC decoding (might include DMX to mono/stereo */
884 1848 : case RENDERER_DISABLE:
885 : case RENDERER_MCMASA_MONO_STEREO:
886 : case RENDERER_OSBA_STEREO:
887 : case RENDERER_MONO_DOWNMIX:
888 1848 : buffer_mode = TC_BUFFER_MODE_BUFFER;
889 1848 : break;
890 9989 : 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 9989 : buffer_mode = TC_BUFFER_MODE_RENDERER;
907 9989 : break;
908 : break;
909 86 : case RENDERER_MC_PARAMMC:
910 86 : if ( st_ivas->hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
911 : {
912 2 : buffer_mode = TC_BUFFER_MODE_BUFFER; /* TCs are already the DMX to mono or stereo */
913 : }
914 : else
915 : {
916 84 : buffer_mode = TC_BUFFER_MODE_RENDERER;
917 : }
918 86 : break;
919 102 : case RENDERER_MC:
920 102 : if ( ivas_dec_get_num_tc_channels( st_ivas ) != st_ivas->hDecoderConfig->nchan_out )
921 : {
922 95 : buffer_mode = TC_BUFFER_MODE_RENDERER;
923 : }
924 102 : break;
925 331 : case RENDERER_SBA_LINEAR_ENC:
926 331 : 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 0 : buffer_mode = TC_BUFFER_MODE_BUFFER;
929 : }
930 : else
931 : {
932 331 : buffer_mode = TC_BUFFER_MODE_RENDERER;
933 : }
934 331 : 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 12356 : return buffer_mode;
952 : }
953 :
954 :
955 : /*--------------------------------------------------------------------------*
956 : * ivas_dec_tc_audio_allocate()
957 : *
958 : * allocate and initialize TC audio buffer
959 : *--------------------------------------------------------------------------*/
960 :
961 11600 : 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 11600 : if ( Opt_tsm )
971 : {
972 4449 : n_samp_full = ( NS2SA( output_Fs, MAX_JBM_L_FRAME_NS ) );
973 4449 : n_samp_full = max( n_samp_full, L_FRAME48k ); /* buffers are shared between 'hTcBuffer->tc[]' and 'p_output_f[]': ensure minimal length */
974 4449 : n_samp_residual = hTcBuffer->n_samples_granularity - 1;
975 : }
976 : else
977 : {
978 7151 : n_samp_full = (int16_t) ( output_Fs / FRAMES_PER_SEC );
979 7151 : n_samp_residual = 0;
980 : }
981 :
982 11600 : nsamp_to_allocate = max( hTcBuffer->nchan_transport_rend, hTcBuffer->nchan_buffer_full ) * n_samp_full;
983 :
984 11600 : if ( Opt_tsm )
985 : {
986 : /* note: this is stack memory buffer for TC decoded and also time-scale modified audio signals */
987 4449 : 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 4449 : set_zero( hTcBuffer->tc_buffer, nsamp_to_allocate );
992 :
993 4449 : offset = 0;
994 22064 : for ( ch_idx = 0; ch_idx < max( hTcBuffer->nchan_transport_rend, hTcBuffer->nchan_buffer_full ); ch_idx++ )
995 : {
996 17615 : hTcBuffer->tc[ch_idx] = &hTcBuffer->tc_buffer[offset];
997 17615 : offset += n_samp_full;
998 : }
999 58018 : for ( ; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
1000 : {
1001 53569 : hTcBuffer->tc[ch_idx] = NULL;
1002 : }
1003 :
1004 : /* memory buffer for TC audio samples not rendered in the previous frame */
1005 22064 : for ( ch_idx = 0; ch_idx < hTcBuffer->nchan_transport_internal; ch_idx++ )
1006 : {
1007 17615 : 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 17615 : set_zero( hTcBuffer->tc_buffer_old[ch_idx], n_samp_residual );
1012 : }
1013 58018 : for ( ; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
1014 : {
1015 53569 : hTcBuffer->tc_buffer_old[ch_idx] = NULL;
1016 : }
1017 : }
1018 : else
1019 : {
1020 7151 : hTcBuffer->tc_buffer = NULL;
1021 :
1022 121567 : for ( ch_idx = 0; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
1023 : {
1024 114416 : hTcBuffer->tc[ch_idx] = NULL;
1025 : }
1026 :
1027 121567 : for ( ch_idx = 0; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
1028 : {
1029 114416 : hTcBuffer->tc_buffer_old[ch_idx] = NULL;
1030 : }
1031 : }
1032 :
1033 11600 : hTcBuffer->tc_buffer2 = NULL;
1034 :
1035 11600 : 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 1671 : 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 1671 : 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 1671 : hTcBuffer->tc_buffer_mode = tc_buffer_mode;
1068 1671 : hTcBuffer->nchan_transport_rend = nchan_transport_rend;
1069 1671 : hTcBuffer->nchan_transport_internal = nchan_transport_internal;
1070 1671 : hTcBuffer->nchan_buffer_full = nchan_full;
1071 1671 : hTcBuffer->n_samples_granularity = n_samples_granularity;
1072 1671 : hTcBuffer->n_samples_available = 0;
1073 1671 : hTcBuffer->n_samples_buffered = 0;
1074 1671 : hTcBuffer->n_samples_rendered = 0;
1075 1671 : hTcBuffer->slots_rendered = 0;
1076 1671 : hTcBuffer->subframes_rendered = 0;
1077 1671 : hTcBuffer->n_samples_discard = 0;
1078 1671 : hTcBuffer->n_samples_flushed = 0;
1079 1671 : hTcBuffer->nb_subframes = MAX_PARAM_SPATIAL_SUBFRAMES;
1080 :
1081 1671 : nMaxSlotsPerSubframe = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / hTcBuffer->n_samples_granularity;
1082 1671 : hTcBuffer->num_slots = nMaxSlotsPerSubframe * MAX_PARAM_SPATIAL_SUBFRAMES;
1083 1671 : set_s( hTcBuffer->subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
1084 1671 : set_s( hTcBuffer->subframe_nbslots, nMaxSlotsPerSubframe, MAX_PARAM_SPATIAL_SUBFRAMES );
1085 :
1086 1671 : 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 1671 : st_ivas->hTcBuffer = hTcBuffer;
1092 :
1093 1671 : return IVAS_ERR_OK;
1094 : }
1095 :
1096 :
1097 : /*--------------------------------------------------------------------------*
1098 : * ivas_dec_tc_audio_deallocate()
1099 : *
1100 : * deallocate TC audio buffer
1101 : *--------------------------------------------------------------------------*/
1102 :
1103 11600 : 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 11600 : if ( hTcBuffer != NULL )
1110 : {
1111 11600 : if ( hTcBuffer->tc_buffer != NULL )
1112 : {
1113 75633 : for ( ch_idx = 0; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
1114 : {
1115 71184 : hTcBuffer->tc[ch_idx] = NULL;
1116 : }
1117 :
1118 4449 : free( hTcBuffer->tc_buffer );
1119 4449 : hTcBuffer->tc_buffer = NULL;
1120 : }
1121 :
1122 197200 : for ( ch_idx = 0; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
1123 : {
1124 185600 : if ( hTcBuffer->tc_buffer_old[ch_idx] != NULL )
1125 : {
1126 17615 : free( hTcBuffer->tc_buffer_old[ch_idx] );
1127 17615 : hTcBuffer->tc_buffer_old[ch_idx] = NULL;
1128 : }
1129 : }
1130 :
1131 11600 : if ( hTcBuffer->tc_buffer2 != NULL )
1132 : {
1133 8925 : free( hTcBuffer->tc_buffer2 );
1134 8925 : hTcBuffer->tc_buffer2 = NULL;
1135 : }
1136 : }
1137 :
1138 11600 : return;
1139 : }
1140 :
1141 :
1142 : /*--------------------------------------------------------------------------*
1143 : * ivas_dec_tc_buffer_close()
1144 : *
1145 : * Close transport channel buffer handle
1146 : *--------------------------------------------------------------------------*/
1147 :
1148 1678 : void ivas_dec_tc_buffer_close(
1149 : DECODER_TC_BUFFER_HANDLE *phTcBuffer /* i/o: TC buffer handle */
1150 : )
1151 : {
1152 1678 : if ( *phTcBuffer != NULL )
1153 : {
1154 1671 : ivas_dec_tc_audio_deallocate( *phTcBuffer );
1155 :
1156 1671 : free( *phTcBuffer );
1157 1671 : *phTcBuffer = NULL;
1158 : }
1159 :
1160 1678 : return;
1161 : }
1162 :
1163 :
1164 : /*--------------------------------------------------------------------------*
1165 : * ivas_dec_tc_buffer_reconfigure()
1166 : *
1167 : * Reconfigure transport channel buffer handle
1168 : *--------------------------------------------------------------------------*/
1169 :
1170 9929 : 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 9929 : hTcBuffer = st_ivas->hTcBuffer;
1185 :
1186 9929 : num_tc_buffer_mem = 0;
1187 9929 : n_samples_still_available = 0;
1188 :
1189 9929 : if ( st_ivas->hDecoderConfig->Opt_tsm )
1190 : {
1191 : /* save samples of the TC buffer from the previous frame */
1192 3888 : num_tc_buffer_mem = min( hTcBuffer->nchan_transport_internal, nchan_transport_internal );
1193 3888 : 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 3888 : assert( n_samples_still_available < n_samples_granularity );
1197 :
1198 15134 : for ( ch_idx = 0; ch_idx < num_tc_buffer_mem; ch_idx++ )
1199 : {
1200 11246 : 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 9929 : if ( n_samples_granularity != hTcBuffer->n_samples_granularity )
1206 : {
1207 : #ifdef DEBUGGING
1208 : int16_t nMaxSlotsPerSubframeOld;
1209 : #endif
1210 : int16_t nMaxSlotsPerSubframeNew;
1211 :
1212 2601 : 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 2601 : if ( n_samples_granularity < hTcBuffer->n_samples_granularity && hTcBuffer->n_samples_flushed > 0 )
1227 : {
1228 205 : hTcBuffer->subframe_nbslots[hTcBuffer->subframes_rendered - 1] = hTcBuffer->n_samples_flushed / n_samples_granularity;
1229 205 : hTcBuffer->n_samples_flushed = 0;
1230 : }
1231 : else
1232 : {
1233 2396 : hTcBuffer->subframe_nbslots[hTcBuffer->subframes_rendered - 1] = nMaxSlotsPerSubframeNew;
1234 : }
1235 : }
1236 :
1237 9929 : hTcBuffer->tc_buffer_mode = tc_buffer_mode;
1238 9929 : hTcBuffer->nchan_transport_rend = nchan_transport_rend;
1239 9929 : hTcBuffer->nchan_transport_internal = nchan_transport_internal;
1240 9929 : hTcBuffer->nchan_buffer_full = nchan_full;
1241 9929 : hTcBuffer->n_samples_granularity = n_samples_granularity;
1242 :
1243 : /* reallocate TC audio buffers */
1244 :
1245 9929 : ivas_dec_tc_audio_deallocate( hTcBuffer );
1246 :
1247 9929 : 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 21175 : for ( ch_idx = 0; ch_idx < num_tc_buffer_mem; ch_idx++ )
1254 : {
1255 11246 : mvr2r( tc_buffer_mem[ch_idx], hTcBuffer->tc_buffer_old[ch_idx], n_samples_still_available );
1256 : }
1257 :
1258 9929 : return IVAS_ERR_OK;
1259 : }
|