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 "lib_dec.h"
34 : #include "ivas_cnst.h"
35 : #include "ivas_prot.h"
36 : #include "ivas_prot_rend.h"
37 : #include "isar_prot.h"
38 : #include "ivas_rom_com.h"
39 : #include "lib_isar_pre_rend.h"
40 : #include "prot.h"
41 : #include "jbm_jb4sb.h"
42 : #include "jbm_pcmdsp_apa.h"
43 : #include "jbm_pcmdsp_fifo.h"
44 : #include <math.h>
45 : #include <assert.h>
46 : #ifdef DEBUGGING
47 : #include "debug.h"
48 : #include <string.h>
49 : #endif
50 : #include "wmc_auto.h"
51 :
52 : /*---------------------------------------------------------------------*
53 : * Local structs
54 : *---------------------------------------------------------------------*/
55 :
56 : struct IVAS_DEC_VOIP
57 : {
58 : JB4_HANDLE hJBM;
59 : uint16_t lastDecodedWasActive;
60 : JB4_DATAUNIT_HANDLE hCurrentDataUnit; /* Points to the currently processed data unit */
61 : uint16_t *bs_conversion_buf; /* Buffer for bitstream conversion from packed to serial */
62 : int16_t nSamplesRendered20ms; /* how many samples have been rendered since the last 20ms render border*/
63 : #ifdef SUPPORT_JBM_TRACEFILE
64 : IVAS_JBM_TRACE_DATA JbmTraceData;
65 : #endif
66 : };
67 :
68 : typedef struct IVAS_DEC_VOIP IVAS_DEC_VOIP;
69 :
70 : struct IVAS_DEC
71 : {
72 : IVAS_DEC_MODE mode;
73 : uint16_t nSamplesFrame;
74 : Decoder_Struct *st_ivas;
75 : IVAS_DEC_VOIP *hVoIP;
76 : bool hasBeenFedFirstGoodFrame; /* False on init. Gets set to true after first good frame has been fed into the decoder, but not decoded yet. */
77 : bool hasDecodedFirstGoodFrame; /* False on init. Gets set to true after first good frame has been decoded -> all bitstream information is known from that point on */
78 : bool isInitialized;
79 :
80 : int16_t bitstreamformat; /* Bitstream format flag (G.192/MIME/VOIP_G192_RTP/VOIP_RTPDUMP) */
81 : #ifdef DEBUGGING
82 : bool Opt_VOIP; /* flag indicating VOIP mode with JBM */
83 : #endif
84 : int16_t tsm_scale; /* scale for TSM operation */
85 : int16_t tsm_max_scaling;
86 : int16_t timeScalingDone; /* have we done already one TSM in a 20ms frame? */
87 : float tsm_quality;
88 : PCMDSP_APA_HANDLE hTimeScaler;
89 : bool needNewFrame;
90 : bool hasBeenFedFrame;
91 : bool updateOrientation;
92 : uint16_t nSamplesAvailableNext;
93 : int16_t nTransportChannelsOld;
94 : int16_t amrwb_rfc4867_flag; /* MIME from rfc4867 is used */
95 : int16_t sdp_hf_only; /* RTP payload format parameter: only Header-Full format without zero padding for size collision avoidance */
96 : int16_t prev_ft_speech; /* RXDTX handler: previous frametype flag for G.192 format AMRWB SID_FIRST detection */
97 : int16_t CNG; /* RXDTX handler: CNG=1, nonCNG=0 */
98 :
99 : uint16_t nSamplesFlushed;
100 : void *flushbuffer;
101 : IVAS_DEC_PCM_TYPE pcmType;
102 : bool hasBeenPreparedRendering;
103 : };
104 :
105 :
106 : /*---------------------------------------------------------------------*
107 : * Local function declarations
108 : *---------------------------------------------------------------------*/
109 :
110 : static void ivas_destroy_handle_VoIP( IVAS_DEC_VOIP *hVoIP );
111 : #ifdef SUPPORT_JBM_TRACEFILE
112 : static void store_JbmData( IVAS_DEC_VOIP *hVoIP, JB4_DATAUNIT_HANDLE dataUnit, const uint32_t systemTimestamp_ms, const uint16_t extBufferedSamples, const int32_t output_Fs );
113 : #endif
114 : static ivas_error evs_dec_main( Decoder_Struct *st_ivas );
115 : static ivas_error input_format_API_to_internal( IVAS_DEC_INPUT_FORMAT input_format, int16_t *bitstream_format_internal, int16_t *sdp_hf_only, const bool is_voip_enabled );
116 : static void init_decoder_config( DECODER_CONFIG_HANDLE hDecoderConfig );
117 : static ivas_error ivas_dec_setup_all( IVAS_DEC_HANDLE hIvasDec, uint8_t *nTransportChannels, ISAR_SPLIT_REND_BITS_DATA *splitRendBits );
118 : static ivas_error apa_setup( IVAS_DEC_HANDLE hIvasDec, const bool isInitialized_voip, const uint16_t nTransportChannels );
119 : static PCM_RESOLUTION pcm_type_API_to_internal( const IVAS_DEC_PCM_TYPE pcmType );
120 : static void *pcm_buffer_offset( void *buffer, const IVAS_DEC_PCM_TYPE pcmType, const int32_t offset );
121 : static ivas_error set_pcm_buffer_to_zero( void *buffer, const IVAS_DEC_PCM_TYPE pcmType, const int16_t nZeroSamples );
122 : static ivas_error isar_set_split_rend_setup( ISAR_DEC_SPLIT_REND_WRAPPER *hSplitBinRend, const ISAR_SPLIT_REND_CONFIG_DATA *hSplitBinConfig, const COMBINED_ORIENTATION_HANDLE hCombinedOrientationData, ISAR_SPLIT_REND_BITS_DATA *splitRendBits );
123 : static ivas_error ivas_dec_reconfig_split_rend( Decoder_Struct *st_ivas );
124 : static ivas_error ivas_dec_init_split_rend( Decoder_Struct *st_ivas );
125 : static ivas_error ivas_create_handle_isar( ISAR_DEC_SPLIT_REND_WRAPPER_HANDLE *hSplitBinRend_out );
126 : static void ivas_destroy_handle_isar( ISAR_DEC_SPLIT_REND_WRAPPER_HANDLE *hSplitBinRend_out );
127 : static int16_t get_render_frame_size_ms( const IVAS_RENDER_NUM_SUBFR render_num_subfr );
128 : static int16_t get_render_frame_size_samples( const DECODER_CONFIG_HANDLE hDecoderConfig );
129 : static int16_t ivas_dec_split_rend_cldfb_in( const RENDERER_TYPE renderer_type );
130 : static void update_voip_rendered20ms( IVAS_DEC_HANDLE hIvasDec, const int16_t nSamplesRendered );
131 :
132 :
133 : /*---------------------------------------------------------------------*
134 : * IVAS_DEC_Open()
135 : *
136 : * Open IVAS decoder
137 : *---------------------------------------------------------------------*/
138 :
139 : /* may return an error but may still have allocated memory - thus run Close also in case of error to release memory */
140 1678 : ivas_error IVAS_DEC_Open(
141 : IVAS_DEC_HANDLE *phIvasDec, /* i/o: pointer to an IVAS decoder handle to be opened */
142 : const IVAS_DEC_MODE mode /* i : compatibility mode (EVS or IVAS) */
143 : )
144 : {
145 : IVAS_DEC_HANDLE hIvasDec;
146 : Decoder_Struct *st_ivas;
147 :
148 1678 : if ( phIvasDec == NULL )
149 : {
150 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
151 : }
152 :
153 : /*-----------------------------------------------------------------*
154 : * Allocate and initialize IVAS application decoder handle
155 : *-----------------------------------------------------------------*/
156 :
157 1678 : if ( ( *phIvasDec = (IVAS_DEC_HANDLE) malloc( sizeof( struct IVAS_DEC ) ) ) == NULL )
158 : {
159 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for IVAS decoder handle" );
160 : }
161 :
162 1678 : hIvasDec = *phIvasDec;
163 1678 : hIvasDec->hVoIP = NULL;
164 1678 : hIvasDec->hTimeScaler = NULL;
165 1678 : hIvasDec->tsm_scale = 100;
166 1678 : hIvasDec->tsm_max_scaling = 0;
167 1678 : hIvasDec->tsm_quality = 1.0f;
168 1678 : hIvasDec->timeScalingDone = 0;
169 1678 : hIvasDec->needNewFrame = false;
170 1678 : hIvasDec->nTransportChannelsOld = 0;
171 1678 : hIvasDec->nSamplesAvailableNext = 0;
172 1678 : hIvasDec->nSamplesFrame = 0;
173 1678 : hIvasDec->hasBeenFedFrame = false;
174 1678 : hIvasDec->hasBeenFedFirstGoodFrame = false;
175 1678 : hIvasDec->hasDecodedFirstGoodFrame = false;
176 1678 : hIvasDec->isInitialized = false;
177 1678 : hIvasDec->updateOrientation = false;
178 1678 : hIvasDec->flushbuffer = NULL;
179 1678 : hIvasDec->pcmType = IVAS_DEC_PCM_INVALID;
180 1678 : hIvasDec->nSamplesFlushed = 0;
181 1678 : hIvasDec->hasBeenPreparedRendering = false;
182 :
183 1678 : hIvasDec->mode = mode;
184 :
185 1678 : hIvasDec->bitstreamformat = G192;
186 : #ifdef DEBUGGING
187 : hIvasDec->Opt_VOIP = 0;
188 : #endif
189 1678 : hIvasDec->amrwb_rfc4867_flag = -1;
190 1678 : hIvasDec->prev_ft_speech = 1; /* RXDTX handler previous frametype flag for G.192 format AMRWB SID_FIRST detection */
191 1678 : hIvasDec->CNG = 0; /* RXDTX handler CNG = 1, no CNG = 0*/
192 :
193 : /*-----------------------------------------------------------------*
194 : * Initialize IVAS-codec decoder state
195 : *-----------------------------------------------------------------*/
196 :
197 1678 : if ( ( hIvasDec->st_ivas = (Decoder_Struct *) malloc( sizeof( Decoder_Struct ) ) ) == NULL )
198 : {
199 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for IVAS decoder structure" );
200 : }
201 :
202 1678 : if ( ( hIvasDec->st_ivas->hDecoderConfig = (DECODER_CONFIG_HANDLE) malloc( sizeof( DECODER_CONFIG ) ) ) == NULL )
203 : {
204 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for Decoder config structure" );
205 : }
206 :
207 : /*-----------------------------------------------------------------*
208 : * Initialize IVAS-codec decoder state
209 : *-----------------------------------------------------------------*/
210 :
211 1678 : st_ivas = hIvasDec->st_ivas;
212 :
213 : /* initialize Decoder Config. handle */
214 1678 : init_decoder_config( hIvasDec->st_ivas->hDecoderConfig );
215 :
216 : /* initialize pointers to handles to NULL */
217 1678 : ivas_initialize_handles_dec( st_ivas );
218 :
219 1678 : st_ivas->restartNeeded = 0;
220 :
221 : /* set high-level parameters */
222 :
223 1678 : st_ivas->codec_mode = 0; /* unknown before first frame */
224 1678 : st_ivas->transport_config = IVAS_AUDIO_CONFIG_INVALID;
225 1678 : st_ivas->intern_config = IVAS_AUDIO_CONFIG_INVALID;
226 1678 : st_ivas->writeFECoffset = 0;
227 1678 : st_ivas->sba_analysis_order = 0; /* not really used in EVS mode, but initialize here to fix MSAN complaint */
228 :
229 1678 : if ( mode == IVAS_DEC_MODE_EVS )
230 : {
231 5 : st_ivas->element_mode_init = EVS_MONO;
232 5 : st_ivas->ivas_format = MONO_FORMAT;
233 5 : hIvasDec->hasDecodedFirstGoodFrame = true; /* Functionality to suppress output for initial lost frames is disabled in EVS operation */
234 :
235 5 : return IVAS_ERR_OK;
236 : }
237 1673 : else if ( mode == IVAS_DEC_MODE_IVAS )
238 : {
239 1673 : st_ivas->element_mode_init = -1;
240 1673 : st_ivas->ivas_format = UNDEFINED_FORMAT;
241 1673 : st_ivas->renderer_type = RENDERER_DISABLE;
242 1673 : st_ivas->ini_frame = 0;
243 1673 : st_ivas->ini_active_frame = 0;
244 :
245 1673 : st_ivas->ism_mode = ISM_MODE_NONE;
246 1673 : st_ivas->mc_mode = MC_MODE_NONE;
247 :
248 1673 : st_ivas->sba_order = 0;
249 1673 : st_ivas->sba_planar = 0;
250 :
251 1673 : return IVAS_ERR_OK;
252 : }
253 :
254 0 : return IVAS_ERR_WRONG_PARAMS;
255 : }
256 :
257 :
258 : /*-------------------------------------------------------------------------*
259 : * isar_set_split_rend_setup()
260 : *
261 : * Setup IVAS split rendering
262 : *-------------------------------------------------------------------------*/
263 :
264 207483 : static ivas_error isar_set_split_rend_setup(
265 : ISAR_DEC_SPLIT_REND_WRAPPER *hSplitBinRend,
266 : const ISAR_SPLIT_REND_CONFIG_DATA *hSplitBinConfig,
267 : const COMBINED_ORIENTATION_HANDLE hCombinedOrientationData,
268 : ISAR_SPLIT_REND_BITS_DATA *splitRendBits /* o : output split rendering bits */
269 : )
270 : {
271 207483 : splitRendBits->bits_read = 0;
272 207483 : splitRendBits->bits_written = 0;
273 207483 : splitRendBits->buf_len = ISAR_MAX_SPLIT_REND_BITS_BUFFER_SIZE_IN_BYTES;
274 207483 : splitRendBits->codec = ISAR_SPLIT_REND_CODEC_DEFAULT;
275 207483 : splitRendBits->pose_correction = ISAR_SPLIT_REND_POSE_CORRECTION_MODE_NONE;
276 207483 : splitRendBits->codec_frame_size_ms = 0;
277 207483 : splitRendBits->isar_frame_size_ms = 0;
278 207483 : splitRendBits->lc3plus_highres = 0;
279 :
280 207483 : ISAR_PRE_REND_GetMultiBinPoseData( hSplitBinConfig, &hSplitBinRend->splitrend.multiBinPoseData, ( hCombinedOrientationData != NULL ) ? hCombinedOrientationData->sr_pose_pred_axis : DEFAULT_AXIS );
281 :
282 207483 : if ( hCombinedOrientationData != NULL )
283 : {
284 207483 : isar_set_split_rend_ht_setup( &hSplitBinRend->splitrend, hCombinedOrientationData->Quaternions, hCombinedOrientationData->Rmat );
285 : }
286 :
287 207483 : return IVAS_ERR_OK;
288 : }
289 :
290 : /*---------------------------------------------------------------------*
291 : * init_decoder_config()
292 : *
293 : * Initialize Decoder Config. handle
294 : *---------------------------------------------------------------------*/
295 :
296 1678 : static void init_decoder_config(
297 : DECODER_CONFIG_HANDLE hDecoderConfig /* i/o: configuration structure */
298 : )
299 : {
300 1678 : hDecoderConfig->Opt_AMR_WB = 0;
301 1678 : hDecoderConfig->output_Fs = -1;
302 1678 : hDecoderConfig->nchan_out = 1;
303 1678 : hDecoderConfig->output_config = IVAS_AUDIO_CONFIG_INVALID;
304 1678 : hDecoderConfig->Opt_LsCustom = 0;
305 1678 : hDecoderConfig->Opt_HRTF_binary = 0;
306 1678 : hDecoderConfig->Opt_Headrotation = 0;
307 1678 : hDecoderConfig->Opt_RendConfigCustom = 0;
308 1678 : hDecoderConfig->room_size = IVAS_ROOM_SIZE_AUTO;
309 1678 : hDecoderConfig->orientation_tracking = IVAS_HEAD_ORIENT_TRK_NONE;
310 1678 : hDecoderConfig->Opt_non_diegetic_pan = 0;
311 1678 : hDecoderConfig->non_diegetic_pan_gain = 0;
312 1678 : hDecoderConfig->Opt_tsm = 0;
313 1678 : hDecoderConfig->Opt_delay_comp = 0;
314 1678 : hDecoderConfig->Opt_ExternalOrientation = 0;
315 1678 : hDecoderConfig->Opt_dpid_on = 0;
316 1678 : hDecoderConfig->Opt_aeid_on = 0;
317 1678 : hDecoderConfig->Opt_ObjEdit_on = 0;
318 :
319 1678 : return;
320 : }
321 :
322 :
323 : /*---------------------------------------------------------------------*
324 : * IVAS_DEC_Close( )
325 : *
326 : * Deallocate IVAS decoder memory handles
327 : *---------------------------------------------------------------------*/
328 :
329 1678 : void IVAS_DEC_Close(
330 : IVAS_DEC_HANDLE *phIvasDec /* i/o: pointer to IVAS decoder handle */
331 : )
332 : {
333 : /* Free all memory */
334 1678 : if ( phIvasDec == NULL || *phIvasDec == NULL )
335 : {
336 0 : return;
337 : }
338 :
339 1678 : if ( ( *phIvasDec )->hVoIP )
340 : {
341 568 : ivas_destroy_handle_VoIP( ( *phIvasDec )->hVoIP );
342 568 : ( *phIvasDec )->hVoIP = NULL;
343 : }
344 :
345 1678 : if ( ( *phIvasDec )->st_ivas )
346 : {
347 : /* destroy Split binaural renderer (ISAR) handle */
348 1678 : ivas_destroy_handle_isar( &( *phIvasDec )->st_ivas->hSplitBinRend );
349 :
350 : /* destroy IVAS decoder handles */
351 1678 : ivas_destroy_dec( ( *phIvasDec )->st_ivas );
352 1678 : ( *phIvasDec )->st_ivas = NULL;
353 : }
354 :
355 1678 : apa_exit( &( *phIvasDec )->hTimeScaler );
356 :
357 1678 : if ( ( *phIvasDec )->flushbuffer != NULL )
358 : {
359 1084 : free( ( *phIvasDec )->flushbuffer );
360 : }
361 :
362 1678 : free( *phIvasDec );
363 1678 : *phIvasDec = NULL;
364 1678 : phIvasDec = NULL;
365 :
366 1678 : return;
367 : }
368 :
369 :
370 : /*---------------------------------------------------------------------*
371 : * mapIvasFormat( )
372 : *
373 : *
374 : *---------------------------------------------------------------------*/
375 :
376 52 : static IVAS_DEC_BS_FORMAT mapIvasFormat(
377 : const IVAS_FORMAT ivas_format )
378 : {
379 52 : switch ( ivas_format )
380 : {
381 0 : case MONO_FORMAT:
382 0 : return IVAS_DEC_BS_MONO;
383 2 : case STEREO_FORMAT:
384 2 : return IVAS_DEC_BS_STEREO;
385 15 : case ISM_FORMAT:
386 15 : return IVAS_DEC_BS_OBJ;
387 4 : case MC_FORMAT:
388 4 : return IVAS_DEC_BS_MC;
389 7 : case SBA_FORMAT:
390 7 : return IVAS_DEC_BS_SBA;
391 3 : case SBA_ISM_FORMAT:
392 3 : return IVAS_DEC_BS_SBA_ISM;
393 17 : case MASA_FORMAT:
394 17 : return IVAS_DEC_BS_MASA;
395 4 : case MASA_ISM_FORMAT:
396 4 : return IVAS_DEC_BS_MASA_ISM;
397 0 : default:
398 0 : break;
399 : }
400 :
401 0 : return IVAS_DEC_BS_UNKOWN;
402 : }
403 :
404 :
405 : /*---------------------------------------------------------------------*
406 : * create_flush_buffer()
407 : *
408 : * Create flush buffer - needed for binaural outputs with TSM or in VoIP mode
409 : *---------------------------------------------------------------------*/
410 :
411 568 : static ivas_error create_flush_buffer(
412 : IVAS_DEC_HANDLE hIvasDec /* i/o: IVAS decoder handle */
413 : )
414 : {
415 568 : hIvasDec->flushbuffer = (void *) malloc( CPE_CHANNELS * hIvasDec->nSamplesFrame / IVAS_MAX_PARAM_SPATIAL_SUBFRAMES * sizeof( int16_t ) );
416 568 : if ( hIvasDec->flushbuffer == NULL )
417 : {
418 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Could not allocate JBM flush buffer" );
419 : }
420 :
421 568 : hIvasDec->pcmType = IVAS_DEC_PCM_INT16;
422 568 : set_s( (int16_t *) hIvasDec->flushbuffer, 0, CPE_CHANNELS * hIvasDec->nSamplesFrame / IVAS_MAX_PARAM_SPATIAL_SUBFRAMES );
423 :
424 568 : return IVAS_ERR_OK;
425 : }
426 :
427 :
428 : /*---------------------------------------------------------------------*
429 : * IVAS_DEC_Configure( )
430 : *
431 : * Decoder configuration
432 : * legacy behavior: if no output format set, then it's EVS mono
433 : *---------------------------------------------------------------------*/
434 :
435 1678 : ivas_error IVAS_DEC_Configure(
436 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
437 : const uint32_t sampleRate, /* i : output sampling frequency */
438 : const AUDIO_CONFIG outputConfig, /* i : output configuration */
439 : const IVAS_RENDER_NUM_SUBFR render_num_subframes, /* i : rendering number of subframes */
440 : const bool customLsOutputEnabled, /* i : enable custom loudspeaker setup handle */
441 : const bool hrtfReaderEnabled, /* i : enable HRTF binary file input */
442 : const bool enableHeadRotation, /* i : enable head rotation for binaural output */
443 : const bool enableExternalOrientation, /* i : enable external orientations */
444 : const IVAS_HEAD_ORIENT_TRK_T orientation_tracking, /* i : head orientation tracking type */
445 : const bool renderConfigEnabled, /* i : enable Renderer config. file for binaural output */
446 : const IVAS_ROOM_SIZE_T roomSize, /* i : room size selector for reverb */
447 : const bool non_diegetic_pan_enabled, /* i : enabled diegetic panning */
448 : const float non_diegetic_pan_gain, /* i : non diegetic panning gain */
449 : const bool dpidEnabled, /* i : enable directivity pattern option */
450 : const uint16_t acousticEnvironmentId, /* i : Acoustic environment ID */
451 : const bool objEditEnabled, /* i : enable object editing */
452 : const bool delayCompensationEnabled /* i : enable delay compensation */
453 : )
454 : {
455 : Decoder_Struct *st_ivas;
456 : DECODER_CONFIG_HANDLE hDecoderConfig;
457 : ivas_error error;
458 :
459 1678 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
460 : {
461 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
462 : }
463 :
464 1678 : if ( sampleRate != 8000 && sampleRate != 16000 && sampleRate != 32000 && sampleRate != 48000 )
465 : {
466 0 : return IVAS_ERR_WRONG_PARAMS;
467 : }
468 :
469 1678 : if ( hIvasDec->mode == IVAS_DEC_MODE_EVS &&
470 5 : ( outputConfig == IVAS_AUDIO_CONFIG_INVALID ||
471 5 : outputConfig == IVAS_AUDIO_CONFIG_ISM1 ||
472 5 : outputConfig == IVAS_AUDIO_CONFIG_ISM2 ||
473 5 : outputConfig == IVAS_AUDIO_CONFIG_ISM3 ||
474 5 : outputConfig == IVAS_AUDIO_CONFIG_ISM4 ||
475 5 : outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED ||
476 5 : outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ||
477 5 : outputConfig == IVAS_AUDIO_CONFIG_MASA1 ||
478 : outputConfig == IVAS_AUDIO_CONFIG_MASA2 ) )
479 : {
480 0 : return IVAS_ERR_WRONG_MODE;
481 : }
482 :
483 1678 : st_ivas = hIvasDec->st_ivas;
484 :
485 1678 : hDecoderConfig = st_ivas->hDecoderConfig;
486 :
487 1678 : hDecoderConfig->output_config = outputConfig;
488 1678 : if ( hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_INVALID )
489 : {
490 0 : return IVAS_ERR_WRONG_PARAMS;
491 : }
492 :
493 1678 : hDecoderConfig->output_Fs = sampleRate;
494 :
495 1678 : if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
496 : {
497 5 : st_ivas->element_mode_init = EVS_MONO;
498 5 : hDecoderConfig->nchan_out = 1;
499 : }
500 :
501 1678 : if ( outputConfig != IVAS_AUDIO_CONFIG_EXTERNAL && outputConfig != IVAS_AUDIO_CONFIG_LS_CUSTOM )
502 : {
503 1622 : hDecoderConfig->nchan_out = audioCfg2channels( hDecoderConfig->output_config );
504 : }
505 :
506 1678 : hDecoderConfig->Opt_LsCustom = (int16_t) customLsOutputEnabled;
507 1678 : hDecoderConfig->Opt_Headrotation = (int16_t) enableHeadRotation;
508 1678 : hDecoderConfig->orientation_tracking = orientation_tracking;
509 1678 : hDecoderConfig->Opt_HRTF_binary = (int16_t) hrtfReaderEnabled;
510 1678 : hDecoderConfig->Opt_RendConfigCustom = (int16_t) renderConfigEnabled;
511 1678 : hDecoderConfig->room_size = roomSize;
512 1678 : hDecoderConfig->Opt_non_diegetic_pan = (int16_t) non_diegetic_pan_enabled;
513 1678 : hDecoderConfig->non_diegetic_pan_gain = non_diegetic_pan_gain;
514 1678 : hDecoderConfig->Opt_delay_comp = (int16_t) delayCompensationEnabled;
515 1678 : hDecoderConfig->Opt_ExternalOrientation = enableExternalOrientation;
516 1678 : hDecoderConfig->Opt_dpid_on = (int16_t) dpidEnabled;
517 1678 : hDecoderConfig->Opt_aeid_on = acousticEnvironmentId != 65535 ? TRUE : FALSE;
518 1678 : hDecoderConfig->Opt_ObjEdit_on = (int16_t) objEditEnabled;
519 :
520 1678 : if ( outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
521 : {
522 1032 : hDecoderConfig->Opt_Headrotation = 1;
523 : }
524 :
525 1678 : if ( render_num_subframes == IVAS_RENDER_NUM_SUBFR_UNKNOWN )
526 : {
527 0 : return IVAS_ERR_WRONG_PARAMS;
528 : }
529 :
530 1678 : if ( outputConfig == IVAS_AUDIO_CONFIG_EXTERNAL )
531 : {
532 53 : hDecoderConfig->render_num_subframes = IVAS_RENDER_NUM_SUBFR_20MS;
533 : }
534 : else
535 : {
536 1625 : hDecoderConfig->render_num_subframes = render_num_subframes;
537 : }
538 :
539 : /* Set decoder parameters to initial values */
540 1678 : if ( ( error = ivas_init_decoder_front( st_ivas ) ) != IVAS_ERR_OK )
541 : {
542 0 : return error;
543 : }
544 :
545 : /* create ISAR handle */
546 1678 : if ( hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
547 : {
548 1032 : if ( ( error = ivas_create_handle_isar( &st_ivas->hSplitBinRend ) ) != IVAS_ERR_OK )
549 : {
550 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for ISAR handle" );
551 : }
552 : }
553 :
554 1678 : if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
555 : {
556 5 : hIvasDec->st_ivas->ivas_format = MONO_FORMAT;
557 : }
558 :
559 1678 : hIvasDec->nSamplesFrame = (uint16_t) ( hDecoderConfig->output_Fs / FRAMES_PER_SEC );
560 :
561 1678 : return IVAS_ERR_OK;
562 : }
563 :
564 :
565 : /*---------------------------------------------------------------------*
566 : * IVAS_DEC_EnableSplitRendering( )
567 : *
568 : * Update IVAS decoder config. if Split rendering is enabled
569 : *---------------------------------------------------------------------*/
570 :
571 1032 : ivas_error IVAS_DEC_EnableSplitRendering(
572 : IVAS_DEC_HANDLE hIvasDec /* i/o: IVAS decoder handle */
573 : )
574 : {
575 : DECODER_CONFIG_HANDLE hDecoderConfig;
576 :
577 1032 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
578 : {
579 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
580 : }
581 :
582 1032 : hDecoderConfig = hIvasDec->st_ivas->hDecoderConfig;
583 :
584 1032 : hDecoderConfig->Opt_Headrotation = 1;
585 1032 : hDecoderConfig->render_num_subframes = IVAS_RENDER_NUM_SUBFR_20MS;
586 :
587 1032 : return IVAS_ERR_OK;
588 : }
589 :
590 :
591 : /*---------------------------------------------------------------------*
592 : * get_render_framesize_ms( )
593 : *
594 : * Get render framesize in ms
595 : *---------------------------------------------------------------------*/
596 :
597 : /*! r: render framesize in ms */
598 1814 : static int16_t get_render_frame_size_ms(
599 : const IVAS_RENDER_NUM_SUBFR render_num_subframes )
600 : {
601 1814 : return (int16_t) ( render_num_subframes * ( 1000 / ( FRAMES_PER_SEC * IVAS_MAX_PARAM_SPATIAL_SUBFRAMES ) ) );
602 : }
603 :
604 :
605 : /*---------------------------------------------------------------------*
606 : * IVAS_DEC_SetRenderNumSubfr( )
607 : *
608 : * Set number of rendering subrames
609 : *---------------------------------------------------------------------*/
610 :
611 1032 : ivas_error IVAS_DEC_SetRenderNumSubfr(
612 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
613 : const IVAS_RENDER_NUM_SUBFR render_num_subframes /* i : renderer number of subframes */
614 : )
615 : {
616 1032 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hDecoderConfig == NULL )
617 : {
618 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
619 : }
620 :
621 1032 : hIvasDec->st_ivas->hDecoderConfig->render_num_subframes = render_num_subframes;
622 :
623 1032 : if ( hIvasDec->st_ivas->hExtOrientationData != NULL )
624 : {
625 0 : hIvasDec->st_ivas->hExtOrientationData->num_subframes = (int16_t) render_num_subframes;
626 : }
627 :
628 1032 : if ( hIvasDec->st_ivas->hCombinedOrientationData != NULL )
629 : {
630 1032 : hIvasDec->st_ivas->hCombinedOrientationData->num_subframes = (int16_t) render_num_subframes;
631 : }
632 :
633 1032 : return IVAS_ERR_OK;
634 : }
635 :
636 :
637 : /*---------------------------------------------------------------------*
638 : * IVAS_DEC_GetRenderNumSubfr( )
639 : *
640 : * Get number of rendering subframes
641 : *---------------------------------------------------------------------*/
642 :
643 737204 : ivas_error IVAS_DEC_GetRenderNumSubfr(
644 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
645 : IVAS_RENDER_NUM_SUBFR *render_num_subframes /* o : rendering number of subframes */
646 : )
647 : {
648 737204 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || render_num_subframes == NULL )
649 : {
650 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
651 : }
652 :
653 737204 : *render_num_subframes = hIvasDec->st_ivas->hDecoderConfig->render_num_subframes;
654 :
655 737204 : return IVAS_ERR_OK;
656 : }
657 :
658 :
659 : /*---------------------------------------------------------------------*
660 : * get_render_frame_size_samples( )
661 : *
662 : *
663 : *---------------------------------------------------------------------*/
664 :
665 320160 : static int16_t get_render_frame_size_samples(
666 : const DECODER_CONFIG_HANDLE hDecoderConfig /* i : configuration structure */
667 : )
668 : {
669 320160 : return (int16_t) ( hDecoderConfig->output_Fs * hDecoderConfig->render_num_subframes / ( FRAMES_PER_SEC * IVAS_MAX_PARAM_SPATIAL_SUBFRAMES ) );
670 : }
671 :
672 :
673 : /*---------------------------------------------------------------------*
674 : * IVAS_DEC_etRenderFramesizeSamples( )
675 : *
676 : * Get render framesize in samples
677 : *---------------------------------------------------------------------*/
678 :
679 319934 : ivas_error IVAS_DEC_GetRenderFramesizeSamples(
680 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
681 : int16_t *render_framesize /* o : render framesize in samples */
682 : )
683 : {
684 319934 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || render_framesize == NULL )
685 : {
686 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
687 : }
688 :
689 319934 : *render_framesize = get_render_frame_size_samples( hIvasDec->st_ivas->hDecoderConfig );
690 :
691 319934 : return IVAS_ERR_OK;
692 : }
693 :
694 :
695 : /*---------------------------------------------------------------------*
696 : * IVAS_DEC_GetRenderFramesizeMs( )
697 : *
698 : * Get render framesize in milliseconds
699 : *---------------------------------------------------------------------*/
700 :
701 561 : ivas_error IVAS_DEC_GetRenderFramesizeMs(
702 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
703 : uint32_t *render_framesize_ms /* o : render framesize in ms */
704 : )
705 : {
706 561 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || render_framesize_ms == NULL )
707 : {
708 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
709 : }
710 :
711 561 : *render_framesize_ms = get_render_frame_size_ms( hIvasDec->st_ivas->hDecoderConfig->render_num_subframes );
712 :
713 561 : return IVAS_ERR_OK;
714 : }
715 :
716 :
717 : /*---------------------------------------------------------------------*
718 : * IVAS_DEC_GetReferencesUpdateFrequency( )
719 : *
720 : * Get update frequency of the reference vector/orientation
721 : *---------------------------------------------------------------------*/
722 :
723 2703 : ivas_error IVAS_DEC_GetReferencesUpdateFrequency(
724 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
725 : int16_t *update_frequency /* o : update frequency */
726 : )
727 : {
728 2703 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || update_frequency == NULL )
729 : {
730 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
731 : }
732 :
733 2703 : *update_frequency = (int16_t) ( IVAS_MAX_PARAM_SPATIAL_SUBFRAMES / hIvasDec->st_ivas->hDecoderConfig->render_num_subframes );
734 :
735 2703 : return IVAS_ERR_OK;
736 : }
737 :
738 :
739 : /*---------------------------------------------------------------------*
740 : * IVAS_DEC_EnableVoIP( )
741 : *
742 : * Intitialize JBM
743 : * jbmSafetyMargin: allowed delay reserve in addition to network jitter
744 : * to reduce late-loss, default: 60 [milliseconds]
745 : *---------------------------------------------------------------------*/
746 :
747 568 : ivas_error IVAS_DEC_EnableVoIP(
748 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
749 : const int16_t jbmSafetyMargin, /* i : allowed delay reserve for JBM, in milliseconds */
750 : const IVAS_DEC_INPUT_FORMAT inputFormat /* i : format of the input bitstream */
751 : )
752 : {
753 : DECODER_CONFIG_HANDLE hDecoderConfig;
754 : ivas_error error;
755 :
756 568 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
757 : {
758 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
759 : }
760 :
761 568 : hDecoderConfig = hIvasDec->st_ivas->hDecoderConfig;
762 :
763 : #ifdef DEBUGGING
764 : hIvasDec->Opt_VOIP = 1;
765 : #endif
766 568 : hDecoderConfig->Opt_tsm = 1;
767 :
768 568 : if ( hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_EXTERNAL )
769 : {
770 562 : hDecoderConfig->nchan_out = audioCfg2channels( hDecoderConfig->output_config );
771 : }
772 : #ifdef VARIABLE_SPEED_DECODING
773 : else
774 : {
775 : hDecoderConfig->nchan_out = 1;
776 : }
777 : #endif
778 :
779 568 : if ( ( error = input_format_API_to_internal( inputFormat, &hIvasDec->bitstreamformat, &hIvasDec->sdp_hf_only, true ) ) != IVAS_ERR_OK )
780 : {
781 0 : return error;
782 : }
783 :
784 568 : if ( ( hIvasDec->hVoIP = malloc( sizeof( IVAS_DEC_VOIP ) ) ) == NULL )
785 : {
786 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Could not allocate VoIP handle" );
787 : }
788 :
789 568 : hIvasDec->hVoIP->lastDecodedWasActive = 0;
790 568 : hIvasDec->hVoIP->hCurrentDataUnit = NULL;
791 568 : hIvasDec->hVoIP->nSamplesRendered20ms = 0;
792 :
793 : #define WMC_TOOL_SKIP
794 : /* Bitstream conversion is not counted towards complexity and memory usage */
795 568 : hIvasDec->hVoIP->bs_conversion_buf = malloc( sizeof( uint16_t ) * ( MAX_BITS_PER_FRAME + 4 * 8 ) );
796 : #undef WMC_TOOL_SKIP
797 :
798 568 : if ( hIvasDec->hVoIP->bs_conversion_buf == NULL )
799 : {
800 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Could not allocate VoIP handle" );
801 : }
802 :
803 : /* initialize JBM */
804 568 : if ( ( error = JB4_Create( &hIvasDec->hVoIP->hJBM ) != IVAS_ERR_OK ) != IVAS_ERR_OK )
805 : {
806 0 : return error;
807 : }
808 :
809 568 : if ( JB4_Init( hIvasDec->hVoIP->hJBM, jbmSafetyMargin ) != 0 )
810 : {
811 0 : return IVAS_ERR_FAILED_ALLOC;
812 : }
813 :
814 : #ifdef NONBE_1122_KEEP_EVS_MODE_UNCHANGED
815 568 : if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
816 : {
817 2 : JB4_TMP_SetEvsCompatFlag( hIvasDec->hVoIP->hJBM );
818 : }
819 : #endif
820 :
821 : /* init flush buffer (needed for binaural outputs) */
822 568 : if ( ( error = create_flush_buffer( hIvasDec ) ) != IVAS_ERR_OK )
823 : {
824 0 : fprintf( stderr, "\nError in create_flush_buffer , code: %d\n", error );
825 0 : return error;
826 : }
827 :
828 568 : return IVAS_ERR_OK;
829 : }
830 :
831 :
832 : /*---------------------------------------------------------------------*
833 : * IVAS_DEC_FeedFrame_Serial( )
834 : *
835 : *
836 : *---------------------------------------------------------------------*/
837 :
838 716163 : ivas_error IVAS_DEC_FeedFrame_Serial(
839 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
840 : uint16_t *serial, /* i : buffer containing serial input bitstream. Each bit should be stored as a single uint16_t value */
841 : const uint16_t num_bits, /* i : number of bits in input bitstream */
842 : int16_t bfi /* i : bad frame indicator flag */
843 : )
844 : {
845 : ivas_error error;
846 :
847 716163 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
848 : {
849 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
850 : }
851 :
852 716163 : if ( !hIvasDec->isInitialized )
853 : {
854 : /* Once first frame is fed, finish initialization in EVS Mono.
855 : * In IVAS mode, initialization is done in ivas_dec(). */
856 1751 : if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
857 : {
858 5 : hIvasDec->st_ivas->hDecoderConfig->ivas_total_brate = ACELP_8k00;
859 5 : hIvasDec->st_ivas->transport_config = IVAS_AUDIO_CONFIG_MONO;
860 :
861 5 : if ( ( error = ivas_init_decoder( hIvasDec->st_ivas ) ) != IVAS_ERR_OK )
862 : {
863 0 : return error;
864 : }
865 :
866 5 : if ( hIvasDec->hVoIP != NULL && hIvasDec->hVoIP->hCurrentDataUnit != NULL )
867 : {
868 0 : DEC_CORE_HANDLE st = hIvasDec->st_ivas->hSCE[0]->hCoreCoder[0];
869 0 : st->ini_frame = 0;
870 0 : st->prev_use_partial_copy = 0;
871 0 : hIvasDec->st_ivas->hDecoderConfig->ivas_total_brate = hIvasDec->hVoIP->hCurrentDataUnit->dataSize * FRAMES_PER_SEC;
872 : }
873 :
874 5 : hIvasDec->isInitialized = true;
875 : }
876 : }
877 :
878 716163 : if ( !bfi ) /* TODO(mcjbm): Is this ok for bfi == 2 (partial frame)? Is there enough info to fully configure decoder? */
879 : {
880 695581 : hIvasDec->hasBeenFedFirstGoodFrame = true;
881 : }
882 :
883 : /* Update redundant frame information in EVS (pre- read indices) */
884 716163 : if ( hIvasDec->mode == IVAS_DEC_MODE_EVS && hIvasDec->hVoIP != NULL && hIvasDec->hVoIP->hCurrentDataUnit != NULL )
885 : {
886 2050 : DEC_CORE_HANDLE st = hIvasDec->st_ivas->hSCE[0]->hCoreCoder[0];
887 2050 : st->bit_stream = serial;
888 :
889 2050 : if ( hIvasDec->hVoIP->hCurrentDataUnit->partial_frame || st->prev_use_partial_copy )
890 : {
891 0 : st->next_coder_type = hIvasDec->hVoIP->hCurrentDataUnit->nextCoderType;
892 : }
893 : else
894 : {
895 2050 : st->next_coder_type = INACTIVE;
896 : }
897 :
898 2050 : if ( hIvasDec->hVoIP->hCurrentDataUnit->partial_frame == 1 && bfi != 1 )
899 : {
900 0 : bfi = 2;
901 : }
902 : }
903 :
904 716163 : if ( ( error = read_indices( hIvasDec->st_ivas, serial, num_bits, &hIvasDec->prev_ft_speech, &hIvasDec->CNG, bfi ) ) != IVAS_ERR_OK )
905 : {
906 0 : return error;
907 : }
908 :
909 : /* Update redundant frame information in EVS (post- read indices) */
910 716163 : if ( hIvasDec->mode == IVAS_DEC_MODE_EVS &&
911 5158 : hIvasDec->hVoIP != NULL &&
912 2058 : hIvasDec->hVoIP->hCurrentDataUnit != NULL &&
913 2050 : hIvasDec->hVoIP->hCurrentDataUnit->partial_frame != 0 )
914 : {
915 0 : DEC_CORE_HANDLE st = hIvasDec->st_ivas->hSCE[0]->hCoreCoder[0];
916 0 : st->codec_mode = MODE2;
917 0 : st->use_partial_copy = 1;
918 : }
919 :
920 716163 : hIvasDec->needNewFrame = false;
921 716163 : hIvasDec->hasBeenFedFrame = true;
922 716163 : hIvasDec->nSamplesAvailableNext = hIvasDec->nSamplesFrame;
923 :
924 716163 : return IVAS_ERR_OK;
925 : }
926 :
927 :
928 : /*---------------------------------------------------------------------*
929 : * renderer_type_to_mode()
930 : *
931 : * Convert (codec library) renderer type to (API public) binaural renderer mode
932 : *---------------------------------------------------------------------*/
933 :
934 : /*! r: binaural renderer mode (API type) */
935 1381510 : static IVAS_BIN_RENDERER_TYPE renderer_type_to_mode(
936 : const RENDERER_TYPE renderer_type /* i : renderer type (codec library type) */
937 : )
938 : {
939 : IVAS_BIN_RENDERER_TYPE binaural_renderer;
940 :
941 1381510 : switch ( renderer_type )
942 : {
943 146531 : case RENDERER_BINAURAL_OBJECTS_TD:
944 146531 : binaural_renderer = IVAS_BIN_RENDERER_TYPE_TDREND;
945 146531 : break;
946 29751 : case RENDERER_BINAURAL_MIXER_CONV:
947 : case RENDERER_BINAURAL_MIXER_CONV_ROOM:
948 29751 : binaural_renderer = IVAS_BIN_RENDERER_TYPE_CREND;
949 29751 : break;
950 100100 : case RENDERER_BINAURAL_FASTCONV:
951 100100 : binaural_renderer = IVAS_BIN_RENDERER_TYPE_FASTCONV;
952 100100 : break;
953 13320 : case RENDERER_BINAURAL_FASTCONV_ROOM:
954 13320 : binaural_renderer = IVAS_BIN_RENDERER_TYPE_FASTCONV;
955 13320 : break;
956 176558 : case RENDERER_BINAURAL_PARAMETRIC:
957 : case RENDERER_BINAURAL_PARAMETRIC_ROOM:
958 176558 : binaural_renderer = IVAS_BIN_RENDERER_TYPE_PARAMBIN;
959 176558 : break;
960 915250 : default:
961 915250 : binaural_renderer = IVAS_BIN_RENDERER_TYPE_NONE;
962 915250 : break;
963 : }
964 :
965 1381510 : return binaural_renderer;
966 : }
967 :
968 :
969 : /*---------------------------------------------------------------------*
970 : * IVAS_DEC_ReadFormat( )
971 : *
972 : * Read main parameters from the bitstream to set-up the decoder:
973 : * - IVAS format
974 : * - IVAS format specific signaling
975 : * - compensate for renderer granularity change in JBM
976 : *---------------------------------------------------------------------*/
977 :
978 706236 : ivas_error IVAS_DEC_ReadFormat(
979 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
980 : IVAS_BIN_RENDERER_TYPE *binaural_renderer, /* o : binaural renderer type */
981 : IVAS_BIN_RENDERER_TYPE *binaural_renderer_sec, /* o : secondary binaural renderer type */
982 : IVAS_AUDIO_CONFIG *hrtf_set_audio_cfg /* o : HRTF set audio config. */
983 : )
984 : {
985 : ivas_error error;
986 : Decoder_Struct *st_ivas;
987 : ISM_MODE ism_mode_old;
988 : MC_MODE mc_mode_old;
989 : int16_t nchan_transport_old;
990 : AUDIO_CONFIG intern_config_old, transport_config_old, output_config;
991 : RENDERER_TYPE renderer_type_old, renderer_type_sec_new, renderer_type_sec_old;
992 :
993 706236 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
994 : {
995 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
996 : }
997 :
998 706236 : st_ivas = hIvasDec->st_ivas;
999 706236 : ism_mode_old = st_ivas->ism_mode;
1000 706236 : mc_mode_old = st_ivas->mc_mode;
1001 706236 : nchan_transport_old = st_ivas->nchan_transport;
1002 706236 : intern_config_old = st_ivas->intern_config;
1003 706236 : transport_config_old = st_ivas->transport_config;
1004 706236 : renderer_type_old = st_ivas->renderer_type;
1005 706236 : renderer_type_sec_old = ivas_renderer_secondary_select( st_ivas );
1006 :
1007 706236 : output_config = st_ivas->hDecoderConfig->output_config;
1008 :
1009 706236 : if ( st_ivas->ivas_format == MONO_FORMAT )
1010 : {
1011 5150 : return IVAS_ERR_OK;
1012 : }
1013 :
1014 701086 : if ( st_ivas->bfi == 0 )
1015 : {
1016 690755 : if ( ( error = ivas_dec_get_format( st_ivas ) ) != IVAS_ERR_OK )
1017 : {
1018 0 : return error;
1019 : }
1020 :
1021 690755 : if ( st_ivas->restartNeeded == 1 )
1022 : {
1023 0 : return IVAS_ERR_OK;
1024 : }
1025 :
1026 : /* Select binaural renderer */
1027 690755 : ivas_renderer_select( st_ivas );
1028 690755 : *binaural_renderer = renderer_type_to_mode( st_ivas->renderer_type );
1029 :
1030 : /* Select secondary binaural renderer (used in combined formats) */
1031 690755 : renderer_type_sec_new = ivas_renderer_secondary_select( st_ivas );
1032 690755 : *binaural_renderer_sec = renderer_type_to_mode( renderer_type_sec_new );
1033 :
1034 : /* select HRTF audio configuration to load the right HRTF set for the external binary file */
1035 690755 : *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_INVALID;
1036 690755 : if ( *binaural_renderer == IVAS_BIN_RENDERER_TYPE_FASTCONV )
1037 : {
1038 113420 : if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
1039 : {
1040 : /* SHD HRIRs */
1041 94328 : *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_HOA3;
1042 :
1043 94328 : if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
1044 : {
1045 : /* BRIRs */
1046 10950 : *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_7_1_4;
1047 : }
1048 : }
1049 19092 : else if ( st_ivas->ivas_format == MC_FORMAT )
1050 : {
1051 : /* HRIRs */
1052 19092 : *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_7_1_4;
1053 :
1054 19092 : if ( ( st_ivas->hDecoderConfig->Opt_Headrotation ) &&
1055 15598 : !( st_ivas->mc_mode == MC_MODE_PARAMUPMIX && output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR ) )
1056 : {
1057 : /* SHD HRIRs for low complexity rotation */
1058 15598 : *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_HOA3;
1059 : }
1060 : }
1061 : }
1062 577335 : else if ( *binaural_renderer == IVAS_BIN_RENDERER_TYPE_CREND )
1063 : {
1064 29751 : if ( st_ivas->ivas_format == ISM_FORMAT )
1065 : {
1066 : /* BRIRs */
1067 10568 : *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_7_1_4;
1068 : }
1069 19183 : else if ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCT )
1070 : {
1071 : /* BRIRs */
1072 19183 : *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_7_1_4;
1073 :
1074 19183 : if ( st_ivas->hDecoderConfig->Opt_Headrotation && ( output_config == IVAS_AUDIO_CONFIG_BINAURAL || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) )
1075 : {
1076 : /* SHD HRIRs for low complexity rotation */
1077 209 : *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_HOA3;
1078 : }
1079 : }
1080 : }
1081 :
1082 : /* JBM: compensate when binaural renderer granularity changes (happens in bitrate switching) */
1083 690755 : if ( st_ivas->ini_active_frame > 0 && st_ivas->hDecoderConfig->Opt_tsm &&
1084 136131 : ( ( renderer_type_old != st_ivas->renderer_type ) ||
1085 : ( renderer_type_sec_old != renderer_type_sec_new ) ) )
1086 : {
1087 1381 : int16_t tc_granularity_new = ivas_dec_get_render_granularity( st_ivas->renderer_type, renderer_type_sec_new, st_ivas->hDecoderConfig->output_Fs );
1088 :
1089 1381 : st_ivas->nchan_transport = nchan_transport_old;
1090 :
1091 1381 : if ( st_ivas->hTcBuffer == NULL )
1092 : {
1093 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1094 : }
1095 :
1096 : /* when granularity goes down, render what still fits in the new granularity */
1097 1381 : if ( tc_granularity_new < st_ivas->hTcBuffer->n_samples_granularity )
1098 : {
1099 529 : if ( ( error = ivas_jbm_dec_flush_renderer( st_ivas, tc_granularity_new, renderer_type_old, intern_config_old, &st_ivas->hIntSetup, mc_mode_old, ism_mode_old, &hIvasDec->nSamplesFlushed, pcm_type_API_to_internal( hIvasDec->pcmType ), hIvasDec->flushbuffer ) ) != IVAS_ERR_OK )
1100 : {
1101 0 : return error;
1102 : }
1103 : }
1104 : }
1105 : }
1106 :
1107 701086 : st_ivas->ism_mode = ism_mode_old;
1108 701086 : st_ivas->mc_mode = mc_mode_old;
1109 701086 : st_ivas->nchan_transport = nchan_transport_old;
1110 701086 : st_ivas->intern_config = intern_config_old;
1111 701086 : st_ivas->transport_config = transport_config_old;
1112 701086 : st_ivas->renderer_type = renderer_type_old;
1113 :
1114 701086 : return IVAS_ERR_OK;
1115 : }
1116 :
1117 :
1118 : /*---------------------------------------------------------------------*
1119 : * IVAS_DEC_GetSamplesDecoder( )
1120 : *
1121 : * Main function to run setup, decode transport channels, do TSM and feed to renderer.
1122 : *---------------------------------------------------------------------*/
1123 :
1124 716155 : ivas_error IVAS_DEC_GetSamplesDecoder(
1125 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
1126 : ISAR_SPLIT_REND_BITS_DATA *splitRendBits /* o : output split rendering bits */
1127 : )
1128 : {
1129 : ivas_error error;
1130 : Decoder_Struct *st_ivas;
1131 : uint16_t nTimeScalerOutSamples;
1132 : uint8_t nTransportChannels;
1133 : int16_t nResidualSamples, nSamplesTcsScaled;
1134 : bool isInitialized_voip;
1135 :
1136 716155 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
1137 : {
1138 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1139 : }
1140 :
1141 716155 : if ( !hIvasDec->hasBeenFedFirstGoodFrame && !hIvasDec->isInitialized ) /* note: 'isInitialized' is related to EVS decoder */
1142 : {
1143 80 : return IVAS_ERR_OK;
1144 : }
1145 :
1146 716075 : st_ivas = hIvasDec->st_ivas;
1147 :
1148 716075 : isInitialized_voip = hIvasDec->hTimeScaler != NULL;
1149 :
1150 716075 : if ( !hIvasDec->isInitialized || hIvasDec->hasBeenFedFrame ) /* wait for the first good frame */
1151 : {
1152 : /*-----------------------------------------------------------------*
1153 : * Setup all decoder parts (IVAS decoder, ISAR)
1154 : *-----------------------------------------------------------------*/
1155 :
1156 716075 : if ( ( error = ivas_dec_setup_all( hIvasDec, &nTransportChannels, splitRendBits ) ) != IVAS_ERR_OK )
1157 : {
1158 0 : return error;
1159 : }
1160 :
1161 : /*-----------------------------------------------------------------*
1162 : * IVAS decoder: decode transport channels and metadata
1163 : *-----------------------------------------------------------------*/
1164 :
1165 716075 : if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
1166 : {
1167 5150 : if ( ( error = evs_dec_main( st_ivas ) ) != IVAS_ERR_OK )
1168 : {
1169 0 : return error;
1170 : }
1171 : }
1172 710925 : else if ( hIvasDec->mode == IVAS_DEC_MODE_IVAS )
1173 : {
1174 710925 : if ( ( error = ivas_dec( st_ivas ) ) != IVAS_ERR_OK )
1175 : {
1176 0 : return error;
1177 : }
1178 :
1179 710925 : hIvasDec->isInitialized = true; /* Initialization done in ivas_dec() */
1180 : }
1181 :
1182 716075 : if ( hIvasDec->hasBeenFedFirstGoodFrame )
1183 : {
1184 716075 : hIvasDec->hasDecodedFirstGoodFrame = true;
1185 : }
1186 :
1187 : /*-----------------------------------------------------------------*
1188 : * JBM
1189 : *-----------------------------------------------------------------*/
1190 :
1191 716075 : if ( st_ivas->hDecoderConfig->Opt_tsm )
1192 : {
1193 148659 : if ( nTransportChannels != hIvasDec->nTransportChannelsOld )
1194 : {
1195 4403 : if ( ( error = apa_setup( hIvasDec, isInitialized_voip, nTransportChannels ) ) != IVAS_ERR_OK )
1196 : {
1197 0 : return error;
1198 : }
1199 : }
1200 :
1201 148659 : if ( apa_set_scale( hIvasDec->hTimeScaler, hIvasDec->tsm_scale ) != 0 )
1202 : {
1203 0 : return IVAS_ERR_UNKNOWN;
1204 : }
1205 :
1206 : /* convert deinterleaved decoded TC audio channels buffer to an interleaved one */
1207 148659 : ivas_buffer_deinterleaved_to_interleaved( st_ivas->p_output_f, st_ivas->hTcBuffer->tc_buffer, nTransportChannels, hIvasDec->nSamplesFrame );
1208 :
1209 : /* time-scale modification */
1210 148659 : if ( apa_exec( hIvasDec->hTimeScaler, st_ivas->hTcBuffer->tc_buffer, hIvasDec->nSamplesFrame * nTransportChannels, (uint16_t) hIvasDec->tsm_max_scaling, st_ivas->hTcBuffer->tc_buffer, &nTimeScalerOutSamples ) != 0 )
1211 : {
1212 0 : return IVAS_ERR_UNKNOWN;
1213 : }
1214 :
1215 148659 : assert( nTimeScalerOutSamples <= APA_BUF );
1216 148659 : nSamplesTcsScaled = nTimeScalerOutSamples / nTransportChannels;
1217 148659 : hIvasDec->timeScalingDone = 1;
1218 :
1219 : /* convert interleaved time-scaled TC audio channels buffer to deinterleaved one */
1220 148659 : ivas_buffer_interleaved_to_deinterleaved( st_ivas->hTcBuffer->tc_buffer, st_ivas->p_output_f, nTransportChannels, nSamplesTcsScaled );
1221 : }
1222 : else
1223 : {
1224 567416 : nSamplesTcsScaled = hIvasDec->nSamplesFrame;
1225 : }
1226 :
1227 : /*-----------------------------------------------------------------*
1228 : * Feed decoded transport channels samples to the renderer
1229 : *-----------------------------------------------------------------*/
1230 :
1231 716075 : ivas_dec_feed_tc_to_renderer( st_ivas, nSamplesTcsScaled, &nResidualSamples );
1232 :
1233 716075 : if ( st_ivas->hDecoderConfig->Opt_tsm )
1234 : {
1235 : /* feed residual samples to TSM for the next call */
1236 148659 : if ( apa_set_renderer_residual_samples( hIvasDec->hTimeScaler, (uint16_t) nResidualSamples ) != 0 )
1237 : {
1238 0 : return IVAS_ERR_UNKNOWN;
1239 : }
1240 : }
1241 :
1242 716075 : hIvasDec->hasBeenFedFrame = false;
1243 : }
1244 :
1245 716075 : hIvasDec->hasBeenPreparedRendering = false;
1246 :
1247 : /*-----------------------------------------------------------------*
1248 : * Set editable metadata
1249 : *-----------------------------------------------------------------*/
1250 :
1251 716075 : if ( st_ivas->hIsmMetaData[0] )
1252 : {
1253 237646 : if ( st_ivas->ivas_format == ISM_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
1254 : {
1255 233375 : if ( st_ivas->ism_mode == ISM_MODE_DISC || st_ivas->ism_mode == ISM_MASA_MODE_DISC || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ || st_ivas->ism_mode == ISM_SBA_MODE_DISC )
1256 : {
1257 : int16_t obj;
1258 197039 : ISM_METADATA_HANDLE *hIsmMetaData = st_ivas->hIsmMetaData;
1259 744559 : for ( obj = 0; obj < st_ivas->nchan_ism; obj++ )
1260 : {
1261 547520 : hIsmMetaData[obj]->edited_azimuth = hIsmMetaData[obj]->azimuth;
1262 547520 : hIsmMetaData[obj]->edited_elevation = hIsmMetaData[obj]->elevation;
1263 547520 : hIsmMetaData[obj]->edited_yaw = hIsmMetaData[obj]->yaw;
1264 547520 : hIsmMetaData[obj]->edited_pitch = hIsmMetaData[obj]->pitch;
1265 547520 : hIsmMetaData[obj]->edited_radius = hIsmMetaData[obj]->radius;
1266 547520 : hIsmMetaData[obj]->edited_gain = 1.0f;
1267 : }
1268 :
1269 197039 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
1270 : {
1271 56618 : st_ivas->hSbaIsmData->gain_bed = 1.0f;
1272 : }
1273 : }
1274 : }
1275 : }
1276 :
1277 716075 : if ( st_ivas->hParamIsmDec != NULL )
1278 : {
1279 21012 : if ( st_ivas->ism_mode == ISM_MODE_PARAM )
1280 : {
1281 21012 : int16_t obj = 0;
1282 21012 : PARAM_ISM_DEC_HANDLE hParamIsmDec = st_ivas->hParamIsmDec;
1283 98305 : for ( obj = 0; obj < st_ivas->nchan_ism; obj++ )
1284 : {
1285 77293 : hParamIsmDec->edited_azimuth_values[obj] = hParamIsmDec->azimuth_values[obj];
1286 77293 : hParamIsmDec->edited_elevation_values[obj] = hParamIsmDec->elevation_values[obj];
1287 : }
1288 : }
1289 : }
1290 :
1291 716075 : return IVAS_ERR_OK;
1292 : }
1293 :
1294 :
1295 : /*---------------------------------------------------------------------*
1296 : * IVAS_DEC_GetEditableParameters( )
1297 : *
1298 : * Get editable metadata parameters
1299 : *---------------------------------------------------------------------*/
1300 :
1301 16027 : ivas_error IVAS_DEC_GetEditableParameters(
1302 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
1303 : IVAS_EDITABLE_PARAMETERS *hIvasEditableParameters /* o : object editing parameters handle */
1304 : )
1305 : {
1306 : int16_t dirac_read_idx, obj;
1307 : Decoder_Struct *st_ivas;
1308 : ISM_MODE ism_mode;
1309 :
1310 16027 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasEditableParameters == NULL )
1311 : {
1312 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1313 : }
1314 :
1315 16027 : if ( !hIvasDec->hasBeenFedFirstGoodFrame )
1316 : {
1317 1 : hIvasEditableParameters->num_obj = 0;
1318 :
1319 1 : return IVAS_ERR_OK;
1320 : }
1321 :
1322 16026 : st_ivas = hIvasDec->st_ivas;
1323 16026 : ism_mode = st_ivas->ism_mode;
1324 :
1325 16026 : if ( !( st_ivas->ivas_format == ISM_FORMAT ||
1326 7012 : st_ivas->ivas_format == SBA_ISM_FORMAT ||
1327 4008 : st_ivas->ivas_format == MASA_ISM_FORMAT ||
1328 334 : ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->nchan_ism > 0 ) ) )
1329 : {
1330 0 : return IVAS_ERROR( IVAS_ERR_OBJECTS_EDITING_NOT_SUPPORTED, "Object editing is not supported in this operation mode." );
1331 : }
1332 :
1333 16026 : hIvasEditableParameters->gain_bed = 1.0f;
1334 16026 : hIvasEditableParameters->num_obj = st_ivas->nchan_ism;
1335 16026 : if ( st_ivas->ivas_format == ISM_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
1336 : {
1337 12018 : if ( ism_mode == ISM_MODE_DISC || ism_mode == ISM_SBA_MODE_DISC )
1338 : {
1339 31651 : for ( obj = 0; obj < hIvasEditableParameters->num_obj; obj++ )
1340 : {
1341 24739 : hIvasEditableParameters->ism_metadata[obj].azimuth = st_ivas->hIsmMetaData[obj]->azimuth;
1342 24739 : hIvasEditableParameters->ism_metadata[obj].elevation = st_ivas->hIsmMetaData[obj]->elevation;
1343 24739 : hIvasEditableParameters->ism_metadata[obj].yaw = st_ivas->hIsmMetaData[obj]->yaw;
1344 24739 : hIvasEditableParameters->ism_metadata[obj].pitch = st_ivas->hIsmMetaData[obj]->pitch;
1345 24739 : hIvasEditableParameters->ism_metadata[obj].radius = st_ivas->hIsmMetaData[obj]->radius;
1346 24739 : hIvasEditableParameters->ism_metadata[obj].gain = st_ivas->hIsmMetaData[obj]->edited_gain;
1347 24739 : hIvasEditableParameters->ism_metadata[obj].non_diegetic_flag = st_ivas->hIsmMetaData[obj]->non_diegetic_flag;
1348 : }
1349 :
1350 6912 : if ( ism_mode == ISM_SBA_MODE_DISC )
1351 : {
1352 2404 : hIvasEditableParameters->gain_bed = 1.0f;
1353 : }
1354 : }
1355 5106 : else if ( ism_mode == ISM_MODE_PARAM )
1356 : {
1357 21025 : for ( obj = 0; obj < hIvasEditableParameters->num_obj; obj++ )
1358 : {
1359 16519 : hIvasEditableParameters->ism_metadata[obj].azimuth = st_ivas->hParamIsmDec->azimuth_values[obj];
1360 16519 : hIvasEditableParameters->ism_metadata[obj].elevation = st_ivas->hParamIsmDec->elevation_values[obj];
1361 16519 : hIvasEditableParameters->ism_metadata[obj].yaw = 0.0f;
1362 16519 : hIvasEditableParameters->ism_metadata[obj].pitch = 0.0f;
1363 16519 : hIvasEditableParameters->ism_metadata[obj].radius = 0.0f;
1364 16519 : hIvasEditableParameters->ism_metadata[obj].gain = 1.0f;
1365 16519 : hIvasEditableParameters->ism_metadata[obj].non_diegetic_flag = 0;
1366 : }
1367 : }
1368 600 : else if ( ism_mode == ISM_MODE_NONE )
1369 : {
1370 600 : hIvasEditableParameters->num_obj = 0;
1371 : }
1372 : #ifdef DEBUGGING
1373 : else
1374 : {
1375 : assert( 0 && "This should never happen!" );
1376 : }
1377 : #endif
1378 : }
1379 4008 : else if ( st_ivas->ivas_format == MASA_ISM_FORMAT || ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->nchan_ism > 0 ) )
1380 : {
1381 : /* object editing possible only in two highest OMASA modes */
1382 4008 : if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
1383 : {
1384 9034 : for ( obj = 0; obj < hIvasEditableParameters->num_obj; obj++ )
1385 : {
1386 6692 : hIvasEditableParameters->ism_metadata[obj].azimuth = st_ivas->hIsmMetaData[obj]->azimuth;
1387 6692 : hIvasEditableParameters->ism_metadata[obj].elevation = st_ivas->hIsmMetaData[obj]->elevation;
1388 6692 : hIvasEditableParameters->ism_metadata[obj].yaw = st_ivas->hIsmMetaData[obj]->yaw;
1389 6692 : hIvasEditableParameters->ism_metadata[obj].pitch = st_ivas->hIsmMetaData[obj]->pitch;
1390 6692 : hIvasEditableParameters->ism_metadata[obj].radius = st_ivas->hIsmMetaData[obj]->radius;
1391 :
1392 : /* reset the otherwise unused "gain" field for the object */
1393 6692 : st_ivas->hIsmMetaData[obj]->edited_gain = 1.0f;
1394 6692 : hIvasEditableParameters->ism_metadata[obj].gain = st_ivas->hIsmMetaData[obj]->edited_gain;
1395 6692 : hIvasEditableParameters->ism_metadata[obj].non_diegetic_flag = st_ivas->hIsmMetaData[obj]->non_diegetic_flag;
1396 : }
1397 : }
1398 1666 : else if ( ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
1399 : {
1400 : /* Handle MONO output */
1401 1332 : if ( st_ivas->renderer_type != RENDERER_MONO_DOWNMIX )
1402 : {
1403 1332 : dirac_read_idx = st_ivas->hSpatParamRendCom->render_to_md_map[st_ivas->hSpatParamRendCom->subframes_rendered];
1404 : }
1405 : else
1406 : {
1407 0 : dirac_read_idx = 0;
1408 : }
1409 :
1410 5996 : for ( obj = 0; obj < hIvasEditableParameters->num_obj; obj++ )
1411 : {
1412 4664 : hIvasEditableParameters->ism_metadata[obj].azimuth = st_ivas->hMasaIsmData->azimuth_ism[obj][dirac_read_idx];
1413 4664 : hIvasEditableParameters->ism_metadata[obj].elevation = st_ivas->hMasaIsmData->elevation_ism[obj][dirac_read_idx];
1414 :
1415 4664 : hIvasEditableParameters->ism_metadata[obj].yaw = st_ivas->hIsmMetaData[obj]->yaw;
1416 4664 : hIvasEditableParameters->ism_metadata[obj].pitch = st_ivas->hIsmMetaData[obj]->pitch;
1417 4664 : hIvasEditableParameters->ism_metadata[obj].radius = st_ivas->hIsmMetaData[obj]->radius;
1418 :
1419 : /* reset the otherwise unused "gain" field for the object */
1420 4664 : st_ivas->hIsmMetaData[obj]->edited_gain = 1.0f;
1421 4664 : hIvasEditableParameters->ism_metadata[obj].gain = st_ivas->hIsmMetaData[obj]->edited_gain;
1422 4664 : hIvasEditableParameters->ism_metadata[obj].non_diegetic_flag = st_ivas->hIsmMetaData[obj]->non_diegetic_flag;
1423 : }
1424 : }
1425 334 : else if ( ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || ism_mode == ISM_MODE_NONE )
1426 : {
1427 334 : hIvasEditableParameters->num_obj = 0;
1428 : }
1429 : #ifdef DEBUGGING
1430 : else
1431 : {
1432 : assert( 0 && "This should never happen!" );
1433 : }
1434 : #endif
1435 : }
1436 :
1437 16026 : return IVAS_ERR_OK;
1438 : }
1439 :
1440 :
1441 : /*---------------------------------------------------------------------*
1442 : * IVAS_DEC_SetEditableParameters( )
1443 : *
1444 : * Set editable metadata parameters
1445 : *---------------------------------------------------------------------*/
1446 :
1447 16027 : ivas_error IVAS_DEC_SetEditableParameters(
1448 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
1449 : IVAS_EDITABLE_PARAMETERS hIvasEditableParameters /* i : object editing parameters handle */
1450 : )
1451 : {
1452 : int16_t dirac_read_idx, obj;
1453 : Decoder_Struct *st_ivas;
1454 : ISM_MODE ism_mode;
1455 :
1456 16027 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
1457 : {
1458 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1459 : }
1460 :
1461 16027 : if ( !hIvasDec->hasBeenFedFirstGoodFrame )
1462 : {
1463 1 : return IVAS_ERR_OK;
1464 : }
1465 :
1466 16026 : st_ivas = hIvasDec->st_ivas;
1467 16026 : ism_mode = st_ivas->ism_mode;
1468 :
1469 16026 : if ( !( st_ivas->ivas_format == ISM_FORMAT ||
1470 7012 : st_ivas->ivas_format == SBA_ISM_FORMAT ||
1471 4008 : st_ivas->ivas_format == MASA_ISM_FORMAT ||
1472 334 : ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->nchan_ism > 0 ) ) )
1473 : {
1474 0 : return IVAS_ERROR( IVAS_ERR_OBJECTS_EDITING_NOT_SUPPORTED, "Object editing no supported in this operation mode." );
1475 : }
1476 :
1477 16026 : if ( st_ivas->ivas_format == ISM_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
1478 : {
1479 12018 : if ( ism_mode == ISM_MODE_DISC || ism_mode == ISM_SBA_MODE_DISC )
1480 : {
1481 : #ifdef DEBUGGING
1482 : assert( hIvasEditableParameters.num_obj == st_ivas->nchan_ism );
1483 : #endif
1484 31651 : for ( obj = 0; obj < hIvasEditableParameters.num_obj; obj++ )
1485 : {
1486 24739 : st_ivas->hIsmMetaData[obj]->edited_azimuth = hIvasEditableParameters.ism_metadata[obj].azimuth;
1487 24739 : st_ivas->hIsmMetaData[obj]->edited_elevation = hIvasEditableParameters.ism_metadata[obj].elevation;
1488 24739 : st_ivas->hIsmMetaData[obj]->edited_radius = hIvasEditableParameters.ism_metadata[obj].radius;
1489 24739 : st_ivas->hIsmMetaData[obj]->edited_yaw = hIvasEditableParameters.ism_metadata[obj].yaw;
1490 24739 : st_ivas->hIsmMetaData[obj]->edited_pitch = hIvasEditableParameters.ism_metadata[obj].pitch;
1491 24739 : if ( hIvasEditableParameters.ism_metadata[obj].gain > EDIT_GAIN_MAX )
1492 : {
1493 0 : st_ivas->hIsmMetaData[obj]->edited_gain = EDIT_GAIN_MAX;
1494 : }
1495 : else
1496 : {
1497 24739 : st_ivas->hIsmMetaData[obj]->edited_gain = hIvasEditableParameters.ism_metadata[obj].gain;
1498 : }
1499 24739 : st_ivas->hIsmMetaData[obj]->non_diegetic_flag = hIvasEditableParameters.ism_metadata[obj].non_diegetic_flag;
1500 : }
1501 :
1502 6912 : if ( ism_mode == ISM_SBA_MODE_DISC )
1503 : {
1504 2404 : if ( hIvasEditableParameters.gain_bed > EDIT_GAIN_MAX )
1505 : {
1506 0 : st_ivas->hSbaIsmData->gain_bed = EDIT_GAIN_MAX;
1507 : }
1508 : else
1509 : {
1510 2404 : st_ivas->hSbaIsmData->gain_bed = hIvasEditableParameters.gain_bed;
1511 : }
1512 : }
1513 : }
1514 5106 : else if ( ism_mode == ISM_MODE_PARAM )
1515 : {
1516 : #ifdef DEBUGGING
1517 : assert( hIvasEditableParameters.num_obj == st_ivas->nchan_ism );
1518 : #endif
1519 21025 : for ( obj = 0; obj < hIvasEditableParameters.num_obj; obj++ )
1520 : {
1521 16519 : st_ivas->hParamIsmDec->edited_azimuth_values[obj] = hIvasEditableParameters.ism_metadata[obj].azimuth;
1522 16519 : st_ivas->hParamIsmDec->edited_elevation_values[obj] = hIvasEditableParameters.ism_metadata[obj].elevation;
1523 :
1524 16519 : if ( st_ivas->hMasaIsmData != NULL )
1525 : {
1526 : /* Limit gain edit to a range of +12dB to -24dB with parametric ISM mode */
1527 12004 : if ( hIvasEditableParameters.ism_metadata[obj].gain > EDIT_GAIN_MAX )
1528 : {
1529 0 : st_ivas->hMasaIsmData->gain_ism_edited[obj] = EDIT_GAIN_MAX;
1530 : }
1531 12004 : else if ( hIvasEditableParameters.ism_metadata[obj].gain < EDIT_GAIN_MIN )
1532 : {
1533 0 : st_ivas->hMasaIsmData->gain_ism_edited[obj] = EDIT_GAIN_MIN;
1534 : }
1535 : else
1536 : {
1537 12004 : st_ivas->hMasaIsmData->gain_ism_edited[obj] = hIvasEditableParameters.ism_metadata[obj].gain;
1538 : }
1539 :
1540 : /* Detect direction editing in Param-ISM mode */
1541 12004 : if ( fabsf( st_ivas->hParamIsmDec->azimuth_values[obj] - hIvasEditableParameters.ism_metadata[obj].azimuth ) > OMASA_AZI_EDIT_THR ||
1542 64 : fabsf( st_ivas->hParamIsmDec->elevation_values[obj] - hIvasEditableParameters.ism_metadata[obj].elevation ) > OMASA_ELE_EDIT_THR )
1543 : {
1544 11996 : st_ivas->hMasaIsmData->ism_dir_is_edited[obj] = 1u;
1545 : }
1546 : else
1547 : {
1548 8 : st_ivas->hMasaIsmData->ism_dir_is_edited[obj] = 0u;
1549 : }
1550 :
1551 : /* Detect gain editing in Param-ISM mode */
1552 12004 : if ( fabsf( 1.0f - hIvasEditableParameters.ism_metadata[obj].gain ) > OMASA_GAIN_EDIT_THR )
1553 : {
1554 11386 : st_ivas->hMasaIsmData->ism_gain_is_edited[obj] = 1u;
1555 : }
1556 : else
1557 : {
1558 618 : st_ivas->hMasaIsmData->ism_gain_is_edited[obj] = 0u;
1559 : }
1560 : }
1561 : }
1562 :
1563 4506 : if ( st_ivas->hMasaIsmData != NULL )
1564 : {
1565 : /* MASA is not present with the ISM format */
1566 3001 : st_ivas->hMasaIsmData->masa_gain_is_edited = 0u;
1567 : }
1568 : }
1569 600 : else if ( ism_mode == ISM_MODE_NONE )
1570 : {
1571 600 : if ( hIvasEditableParameters.num_obj != 0 )
1572 : {
1573 0 : return IVAS_ERR_OBJECTS_EDITING_NOT_SUPPORTED;
1574 : }
1575 : }
1576 : #ifdef DEBUGGING
1577 : else
1578 : {
1579 : assert( 0 && "This should never happen!" );
1580 : }
1581 : #endif
1582 : }
1583 4008 : else if ( st_ivas->ivas_format == MASA_ISM_FORMAT && ( ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ || ism_mode == ISM_MASA_MODE_DISC ) )
1584 : {
1585 : int16_t id_th;
1586 : float threshold_azi, threshold_ele;
1587 :
1588 : #ifdef DEBUGGING
1589 : assert( hIvasEditableParameters.num_obj == st_ivas->nchan_ism );
1590 : #endif
1591 15030 : for ( obj = 0; obj < hIvasEditableParameters.num_obj; obj++ )
1592 : {
1593 11356 : if ( st_ivas->hMasaIsmData != NULL )
1594 : {
1595 : /* copy relevant fields also to OMASA structs, but only if the value has been changed. original values are in st_ivas->hIsmMetaData */
1596 : /* first, need to convert float values to ints used internally */
1597 : int16_t new_azi, new_ele;
1598 11356 : if ( hIvasEditableParameters.ism_metadata[obj].azimuth > 0.0f )
1599 : {
1600 7613 : new_azi = (int16_t) ( hIvasEditableParameters.ism_metadata[obj].azimuth + 0.5f );
1601 : }
1602 : else
1603 : {
1604 3743 : new_azi = (int16_t) ( hIvasEditableParameters.ism_metadata[obj].azimuth - 0.5f );
1605 : }
1606 :
1607 11356 : if ( hIvasEditableParameters.ism_metadata[obj].elevation > 0.0f )
1608 : {
1609 3767 : new_ele = (int16_t) ( hIvasEditableParameters.ism_metadata[obj].elevation + 0.5f );
1610 : }
1611 : else
1612 : {
1613 7589 : new_ele = (int16_t) ( hIvasEditableParameters.ism_metadata[obj].elevation - 0.5f );
1614 : }
1615 :
1616 11356 : if ( ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
1617 : {
1618 : /* Handle MONO output */
1619 4664 : if ( st_ivas->renderer_type != RENDERER_MONO_DOWNMIX )
1620 : {
1621 4664 : dirac_read_idx = st_ivas->hSpatParamRendCom->render_to_md_map[st_ivas->hSpatParamRendCom->subframes_rendered];
1622 : }
1623 : else
1624 : {
1625 0 : dirac_read_idx = 0;
1626 : }
1627 :
1628 : /* determine thresholds for detecting object metadata edit for direction based on quantization resolution of the spatial direction parameters.
1629 : * these depend from the number of bits used to transmit the directions,
1630 : * which in turn depends from the object priority and importance:
1631 : * importance -> priority -> number of bits -> elevation resolution -> elevation ring index -> azimuth resolution.
1632 : * leading to elevation_resolution -> elevation threshold and azimuth resolution -> azimuth threshold */
1633 4664 : id_th = (int16_t) ( fabsf( (float) st_ivas->hMasaIsmData->elevation_ism[obj][dirac_read_idx] ) / delta_theta_masa[st_ivas->hMasaIsmData->bits_ism[obj] - 3] + 0.5f );
1634 4664 : threshold_azi = 360.0f / (float) no_phi_masa[st_ivas->hMasaIsmData->bits_ism[obj] - 1][id_th];
1635 4664 : threshold_ele = delta_theta_masa[st_ivas->hMasaIsmData->bits_ism[obj] - 3];
1636 :
1637 4664 : if ( ( (float) abs( new_azi - st_ivas->hMasaIsmData->azimuth_ism[obj][dirac_read_idx] ) > threshold_azi ) ||
1638 447 : ( (float) abs( new_ele - st_ivas->hMasaIsmData->elevation_ism[obj][dirac_read_idx] ) > threshold_ele ) )
1639 : {
1640 : /* at least one of the threshold is exceeded, so use new direction value and set editing detection flag */
1641 4456 : st_ivas->hMasaIsmData->azimuth_ism_edited[obj] = new_azi;
1642 4456 : st_ivas->hMasaIsmData->elevation_ism_edited[obj] = new_ele;
1643 :
1644 4456 : st_ivas->hMasaIsmData->ism_dir_is_edited[obj] = 1u;
1645 : }
1646 : else
1647 : {
1648 208 : st_ivas->hMasaIsmData->ism_dir_is_edited[obj] = 0u;
1649 : }
1650 : }
1651 : else
1652 : {
1653 : /* detect editing in ISM_MASA_MODE_DISC mode */
1654 6692 : if ( fabsf( st_ivas->hIsmMetaData[obj]->azimuth - hIvasEditableParameters.ism_metadata[obj].azimuth ) > OMASA_AZI_EDIT_THR ||
1655 26 : fabsf( st_ivas->hIsmMetaData[obj]->elevation - hIvasEditableParameters.ism_metadata[obj].elevation ) > OMASA_ELE_EDIT_THR )
1656 : {
1657 6690 : st_ivas->hMasaIsmData->azimuth_ism_edited[obj] = new_azi;
1658 6690 : st_ivas->hMasaIsmData->elevation_ism_edited[obj] = new_ele;
1659 :
1660 6690 : st_ivas->hMasaIsmData->ism_dir_is_edited[obj] = 1u;
1661 : }
1662 : else
1663 : {
1664 2 : st_ivas->hMasaIsmData->ism_dir_is_edited[obj] = 0u;
1665 : }
1666 : }
1667 :
1668 : /* compare pre-edit gain and the edited one to detect editing */
1669 11356 : if ( fabsf( st_ivas->hIsmMetaData[obj]->edited_gain - hIvasEditableParameters.ism_metadata[obj].gain ) > OMASA_GAIN_EDIT_THR )
1670 : {
1671 10501 : st_ivas->hMasaIsmData->ism_gain_is_edited[obj] = 1u;
1672 :
1673 : /* Limit masa gain edit to a range of +12dB to -infdB with discrete OMASA mode and +12dB to -24dB with parametric OMASA mode */
1674 10501 : if ( hIvasEditableParameters.ism_metadata[obj].gain > EDIT_GAIN_MAX )
1675 : {
1676 0 : st_ivas->hMasaIsmData->gain_ism_edited[obj] = EDIT_GAIN_MAX;
1677 : }
1678 10501 : else if ( ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ && hIvasEditableParameters.ism_metadata[obj].gain < EDIT_GAIN_MIN )
1679 : {
1680 0 : st_ivas->hMasaIsmData->gain_ism_edited[obj] = EDIT_GAIN_MIN;
1681 : }
1682 : else
1683 : {
1684 10501 : st_ivas->hMasaIsmData->gain_ism_edited[obj] = hIvasEditableParameters.ism_metadata[obj].gain;
1685 : }
1686 : }
1687 : else
1688 : {
1689 855 : st_ivas->hMasaIsmData->ism_gain_is_edited[obj] = 0u;
1690 : }
1691 : }
1692 :
1693 : /* Copy edited values to hIsmMetaData struct */
1694 11356 : if ( st_ivas->hIsmMetaData[obj] != NULL )
1695 : {
1696 11356 : st_ivas->hIsmMetaData[obj]->edited_azimuth = hIvasEditableParameters.ism_metadata[obj].azimuth;
1697 11356 : st_ivas->hIsmMetaData[obj]->edited_elevation = hIvasEditableParameters.ism_metadata[obj].elevation;
1698 :
1699 11356 : if ( ism_mode == ISM_MASA_MODE_DISC )
1700 : {
1701 6692 : st_ivas->hIsmMetaData[obj]->edited_yaw = hIvasEditableParameters.ism_metadata[obj].yaw;
1702 6692 : st_ivas->hIsmMetaData[obj]->edited_pitch = hIvasEditableParameters.ism_metadata[obj].pitch;
1703 6692 : st_ivas->hIsmMetaData[obj]->edited_radius = hIvasEditableParameters.ism_metadata[obj].radius;
1704 : }
1705 :
1706 11356 : if ( hIvasEditableParameters.ism_metadata[obj].gain > EDIT_GAIN_MAX )
1707 : {
1708 0 : st_ivas->hIsmMetaData[obj]->edited_gain = EDIT_GAIN_MAX;
1709 : }
1710 11356 : else if ( ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ && hIvasEditableParameters.ism_metadata[obj].gain < EDIT_GAIN_MIN )
1711 : {
1712 0 : st_ivas->hIsmMetaData[obj]->edited_gain = EDIT_GAIN_MIN;
1713 : }
1714 : else
1715 : {
1716 11356 : st_ivas->hIsmMetaData[obj]->edited_gain = hIvasEditableParameters.ism_metadata[obj].gain;
1717 : }
1718 :
1719 11356 : st_ivas->hIsmMetaData[obj]->non_diegetic_flag = hIvasEditableParameters.ism_metadata[obj].non_diegetic_flag;
1720 : }
1721 : }
1722 :
1723 3674 : if ( fabsf( hIvasEditableParameters.gain_bed - 1.0f ) > OMASA_GAIN_EDIT_THR )
1724 : {
1725 : /* Limit masa gain edit to a range of +12dB to -infdB with discrete OMASA mode and +12dB to -24dB with parametric OMASA mode */
1726 3533 : if ( hIvasEditableParameters.gain_bed > EDIT_GAIN_MAX )
1727 : {
1728 0 : st_ivas->hMasaIsmData->gain_masa_edited = EDIT_GAIN_MAX;
1729 : }
1730 3533 : else if ( ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ && hIvasEditableParameters.gain_bed < EDIT_GAIN_MIN )
1731 : {
1732 0 : st_ivas->hMasaIsmData->gain_masa_edited = EDIT_GAIN_MIN;
1733 : }
1734 : else
1735 : {
1736 3533 : st_ivas->hMasaIsmData->gain_masa_edited = hIvasEditableParameters.gain_bed;
1737 : }
1738 3533 : st_ivas->hMasaIsmData->masa_gain_is_edited = 1u;
1739 : }
1740 : else
1741 : {
1742 141 : st_ivas->hMasaIsmData->masa_gain_is_edited = 0u;
1743 : }
1744 : }
1745 :
1746 16026 : return IVAS_ERR_OK;
1747 : }
1748 :
1749 :
1750 : /*---------------------------------------------------------------------*
1751 : * IVAS_DEC_PrepareRenderer( )
1752 : *
1753 : * prepare IVAS renderer
1754 : *---------------------------------------------------------------------*/
1755 :
1756 716155 : ivas_error IVAS_DEC_PrepareRenderer(
1757 : IVAS_DEC_HANDLE hIvasDec /* i/o: IVAS decoder handle */
1758 : )
1759 : {
1760 716155 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
1761 : {
1762 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1763 : }
1764 :
1765 716155 : if ( hIvasDec->hasBeenFedFirstGoodFrame || hIvasDec->isInitialized ) /* note: 'isInitialized' is related to EVS decoder */
1766 : {
1767 716075 : ivas_dec_prepare_renderer( hIvasDec->st_ivas );
1768 : }
1769 :
1770 716155 : hIvasDec->hasBeenPreparedRendering = true;
1771 :
1772 716155 : return IVAS_ERR_OK;
1773 : }
1774 :
1775 :
1776 : /*---------------------------------------------------------------------*
1777 : * IVAS_DEC_GetSamplesRenderer( )
1778 : *
1779 : * Main function to render the decoded data to output data
1780 : *---------------------------------------------------------------------*/
1781 :
1782 795724 : ivas_error IVAS_DEC_GetSamplesRenderer(
1783 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
1784 : const int16_t nSamplesAsked, /* i : number of samples wanted by the caller */
1785 : const IVAS_DEC_PCM_TYPE pcmType, /* i : type for the decoded PCM resolution */
1786 : void *pcmBuf, /* o : output synthesis signal */
1787 : int16_t *nOutSamples, /* o : number of samples per channel written to output buffer */
1788 : bool *needNewFrame /* o : indication that the decoder needs a new frame */
1789 : )
1790 : {
1791 : ivas_error error;
1792 : uint16_t nSamplesRendered, nSamplesRendered_loop;
1793 : uint8_t nOutChannels;
1794 : Decoder_Struct *st_ivas;
1795 :
1796 795724 : nSamplesRendered = 0;
1797 795724 : nOutChannels = 0;
1798 795724 : nSamplesRendered_loop = 0;
1799 :
1800 795724 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
1801 : {
1802 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1803 : }
1804 :
1805 : /* the rendering needs to be prepared at this point */
1806 795724 : if ( hIvasDec->hasBeenPreparedRendering == false )
1807 : {
1808 0 : return IVAS_ERR_UNKNOWN;
1809 : }
1810 :
1811 795724 : st_ivas = hIvasDec->st_ivas;
1812 :
1813 795724 : if ( hIvasDec->updateOrientation )
1814 : {
1815 : /*----------------------------------------------------------------*
1816 : * Combine orientations
1817 : *----------------------------------------------------------------*/
1818 :
1819 299328 : if ( ( error = combine_external_and_head_orientations_dec( st_ivas->hHeadTrackData, st_ivas->hExtOrientationData, st_ivas->hCombinedOrientationData ) ) != IVAS_ERR_OK )
1820 : {
1821 0 : return error;
1822 : }
1823 :
1824 : /*----------------------------------------------------------------*
1825 : * Binaural split rendering setup
1826 : *----------------------------------------------------------------*/
1827 :
1828 299328 : if ( st_ivas->hCombinedOrientationData != NULL && ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) )
1829 : {
1830 220022 : isar_set_split_rend_ht_setup( &st_ivas->hSplitBinRend->splitrend, st_ivas->hCombinedOrientationData->Quaternions, st_ivas->hCombinedOrientationData->Rmat );
1831 : }
1832 :
1833 299328 : hIvasDec->updateOrientation = false;
1834 : }
1835 :
1836 795724 : if ( !hIvasDec->hasBeenFedFrame && hIvasDec->nSamplesAvailableNext == 0 )
1837 : {
1838 : /* no frame was fed, do nothing but ask for a frame */
1839 0 : *needNewFrame = true;
1840 0 : *nOutSamples = 0;
1841 0 : hIvasDec->needNewFrame = true;
1842 0 : return IVAS_ERR_OK;
1843 : }
1844 :
1845 : /* check if we are still at the beginning with bad frames, put out zeroes, keep track of subframes */
1846 795724 : if ( !hIvasDec->isInitialized && st_ivas->bfi )
1847 : {
1848 80 : hIvasDec->hasBeenFedFrame = false;
1849 80 : set_s( pcmBuf, 0, st_ivas->hDecoderConfig->nchan_out * nSamplesAsked );
1850 80 : nSamplesRendered = nSamplesAsked;
1851 80 : hIvasDec->nSamplesAvailableNext -= nSamplesAsked;
1852 : }
1853 : else
1854 : {
1855 795644 : nOutChannels = (uint8_t) st_ivas->hDecoderConfig->nchan_out;
1856 795644 : hIvasDec->hasBeenFedFrame = false;
1857 :
1858 : /* check for possible flushed samples from a rate switch */
1859 795644 : if ( hIvasDec->nSamplesFlushed > 0 )
1860 : {
1861 : #ifdef DEBUGGING
1862 : assert( hIvasDec->pcmType == pcmType );
1863 : #endif
1864 : /* note: offset (rendered samples) is always 0 */
1865 205 : if ( pcmType == IVAS_DEC_PCM_INT16 )
1866 : {
1867 205 : mvs2s( (int16_t *) hIvasDec->flushbuffer, pcmBuf, hIvasDec->nSamplesFlushed * nOutChannels );
1868 : }
1869 0 : else if ( pcmType == IVAS_DEC_PCM_FLOAT )
1870 : {
1871 0 : mvr2r( (float *) hIvasDec->flushbuffer, pcmBuf, hIvasDec->nSamplesFlushed * nOutChannels );
1872 : }
1873 : #ifdef DEBUGGING
1874 : else
1875 : {
1876 : assert( 0 && "wrong PCM type for the flush buffer!" );
1877 : }
1878 : #endif
1879 205 : nSamplesRendered = hIvasDec->nSamplesFlushed;
1880 205 : hIvasDec->nSamplesFlushed = 0;
1881 : }
1882 :
1883 : /* render IVAS frames directly to the output buffer */
1884 795644 : if ( ( error = ivas_dec_render( st_ivas, nSamplesAsked - nSamplesRendered, &nSamplesRendered_loop, &hIvasDec->nSamplesAvailableNext, pcm_type_API_to_internal( pcmType ), pcm_buffer_offset( pcmBuf, pcmType, nSamplesRendered * nOutChannels ) ) ) != IVAS_ERR_OK )
1885 : {
1886 0 : return error;
1887 : }
1888 :
1889 795644 : nSamplesRendered += nSamplesRendered_loop;
1890 : }
1891 :
1892 795724 : if ( hIvasDec->nSamplesAvailableNext == 0 )
1893 : {
1894 715878 : *needNewFrame = true;
1895 715878 : hIvasDec->needNewFrame = true;
1896 : }
1897 : else
1898 : {
1899 79846 : *needNewFrame = false;
1900 : }
1901 :
1902 795724 : *nOutSamples = nSamplesRendered;
1903 :
1904 795724 : return IVAS_ERR_OK;
1905 : }
1906 :
1907 :
1908 : /*---------------------------------------------------------------------*
1909 : * isar_get_frame_size( )
1910 : *
1911 : *
1912 : *---------------------------------------------------------------------*/
1913 :
1914 109200 : static int16_t isar_get_frame_size(
1915 : Decoder_Struct *st_ivas /* i : IVAS decoder handle */
1916 : )
1917 : {
1918 : int32_t output_Fs;
1919 : int16_t nSamplesPerChannel;
1920 :
1921 109200 : output_Fs = st_ivas->hDecoderConfig->output_Fs;
1922 :
1923 109200 : if ( st_ivas->hDecoderConfig->render_num_subframes != IVAS_RENDER_NUM_SUBFR_20MS &&
1924 10000 : ( st_ivas->hRenderConfig->split_rend_config.poseCorrectionMode == ISAR_SPLIT_REND_POSE_CORRECTION_MODE_NONE ||
1925 0 : st_ivas->hRenderConfig->split_rend_config.dof == 0 ) )
1926 : {
1927 10000 : nSamplesPerChannel = (int16_t) ( output_Fs / FRAMES_PER_SEC / MAX_PARAM_SPATIAL_SUBFRAMES );
1928 10000 : nSamplesPerChannel *= (int16_t) st_ivas->hDecoderConfig->render_num_subframes;
1929 : }
1930 : else
1931 : {
1932 99200 : nSamplesPerChannel = (int16_t) ( output_Fs / FRAMES_PER_SEC );
1933 : }
1934 :
1935 109200 : return nSamplesPerChannel;
1936 : }
1937 :
1938 :
1939 : /*---------------------------------------------------------------------*
1940 : * isar_render_poses( )
1941 : *
1942 : *
1943 : *---------------------------------------------------------------------*/
1944 :
1945 260215 : static ivas_error isar_render_poses(
1946 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
1947 : const int16_t nSamplesAsked, /* i : number of samples wanted by the caller */
1948 : int16_t *nOutSamples, /* o : number of samples per channel written to output buffer */
1949 : bool *needNewFrame /* o : indication that the decoder needs a new frame */
1950 : )
1951 : {
1952 : float pcmBuf[BINAURAL_CHANNELS * MAX_HEAD_ROT_POSES * L_FRAME48k];
1953 : Decoder_Struct *st_ivas;
1954 : ivas_error error;
1955 : int16_t numPoses;
1956 :
1957 260215 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
1958 : {
1959 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1960 : }
1961 :
1962 260215 : *needNewFrame = false;
1963 :
1964 260215 : st_ivas = hIvasDec->st_ivas;
1965 :
1966 260215 : numPoses = st_ivas->hSplitBinRend->splitrend.multiBinPoseData.num_poses;
1967 :
1968 : /* init flush buffer for rate switch if not already initizalized */
1969 260215 : if ( hIvasDec->flushbuffer == NULL )
1970 : {
1971 516 : hIvasDec->flushbuffer = (void *) malloc( numPoses * BINAURAL_CHANNELS * hIvasDec->nSamplesFrame / IVAS_MAX_PARAM_SPATIAL_SUBFRAMES * sizeof( float ) );
1972 516 : if ( hIvasDec->flushbuffer == NULL )
1973 : {
1974 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Could not allocate flush buffer" );
1975 : }
1976 516 : hIvasDec->pcmType = IVAS_DEC_PCM_FLOAT;
1977 516 : set_zero( (float *) hIvasDec->flushbuffer, numPoses * BINAURAL_CHANNELS * hIvasDec->nSamplesFrame / IVAS_MAX_PARAM_SPATIAL_SUBFRAMES );
1978 : }
1979 :
1980 : /* render */
1981 260215 : if ( ( error = IVAS_DEC_GetSamplesRenderer( hIvasDec, nSamplesAsked, IVAS_DEC_PCM_FLOAT, pcmBuf, nOutSamples, needNewFrame ) ) != IVAS_ERR_OK )
1982 : {
1983 0 : return error;
1984 : }
1985 :
1986 260215 : if ( !hIvasDec->hasBeenFedFirstGoodFrame )
1987 : {
1988 0 : return IVAS_ERR_OK;
1989 : }
1990 :
1991 260215 : if ( !ivas_dec_split_rend_cldfb_in( st_ivas->renderer_type ) )
1992 : {
1993 63603 : ivas_TD_RINGBUF_PushInterleaved( st_ivas->hSplitBinRend->hMultiBinTdData, pcmBuf, *nOutSamples );
1994 : }
1995 :
1996 260215 : return error;
1997 : }
1998 :
1999 :
2000 : /*---------------------------------------------------------------------*
2001 : * isar_generate_metadata_and_bitstream( )
2002 : *
2003 : *
2004 : *---------------------------------------------------------------------*/
2005 :
2006 220022 : static ivas_error isar_generate_metadata_and_bitstream(
2007 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
2008 : float **p_head_pose_buf, /* i/o: PCM buffer with head-pose data */
2009 : int16_t nSamples, /* i : duration of audio (in samples per channel) for which metadata should be generated */
2010 : ISAR_SPLIT_REND_BITS_DATA *splitRendBits /* o : output split rendering bits */
2011 : )
2012 : {
2013 : ivas_error error;
2014 : ISAR_DEC_SPLIT_REND_WRAPPER_HANDLE hSplitBinRend;
2015 : int16_t max_band;
2016 : int16_t pcm_out_flag;
2017 : int16_t cldfb_in_flag;
2018 : int16_t ro_md_flag;
2019 : IVAS_QUATERNION Quaternion;
2020 : int16_t i, j, num_poses, num_cldfb_slots, n_samples_in_cldfb_slot;
2021 : float *p_Cldfb_RealBuffer_Binaural[MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS][CLDFB_NO_COL_MAX];
2022 : float *p_Cldfb_ImagBuffer_Binaural[MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS][CLDFB_NO_COL_MAX];
2023 :
2024 220022 : hSplitBinRend = st_ivas->hSplitBinRend;
2025 :
2026 220022 : max_band = (int16_t) ( ( BINAURAL_MAXBANDS * st_ivas->hDecoderConfig->output_Fs ) / 48000 );
2027 220022 : pcm_out_flag = ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ? 1 : 0;
2028 220022 : cldfb_in_flag = ivas_dec_split_rend_cldfb_in( st_ivas->renderer_type );
2029 :
2030 220022 : if ( cldfb_in_flag )
2031 : {
2032 160747 : n_samples_in_cldfb_slot = NS2SA( st_ivas->hDecoderConfig->output_Fs, CLDFB_SLOT_NS );
2033 160747 : assert( nSamples % n_samples_in_cldfb_slot == 0 );
2034 160747 : num_cldfb_slots = nSamples / n_samples_in_cldfb_slot;
2035 :
2036 160747 : num_poses = hSplitBinRend->splitrend.multiBinPoseData.num_poses;
2037 :
2038 1366873 : for ( i = 0; i < BINAURAL_CHANNELS * num_poses; ++i )
2039 : {
2040 20310942 : for ( j = 0; j < num_cldfb_slots; ++j )
2041 : {
2042 : /* Save pointers to first CLDFB column in the ring buffer. Allows us to save
2043 : * significant amounts of memory by not copying CLDFB values into a separate buffer. */
2044 19104816 : ivas_CLDFB_RINGBUF_GetByIdx( hSplitBinRend->hMultiBinCldfbData[i], &p_Cldfb_RealBuffer_Binaural[i][j], &p_Cldfb_ImagBuffer_Binaural[i][j], 0 );
2045 :
2046 : /* Pop the CLDFB column we just saved pointers to. This is fine as long as we use
2047 : * the saved columns only before any new columns are pushed to the buffer - the new
2048 : * columns could potentially overwrite the old columns we wanted to use.
2049 : * This requirement is fulfilled in this case. */
2050 19104816 : ivas_CLDFB_RINGBUF_Pop( hSplitBinRend->hMultiBinCldfbData[i], NULL, NULL, CLDFB_NO_CHANNELS_MAX );
2051 : }
2052 : }
2053 : }
2054 : else
2055 : {
2056 59275 : ivas_TD_RINGBUF_PopChannels( st_ivas->hSplitBinRend->hMultiBinTdData, p_head_pose_buf, nSamples );
2057 : }
2058 :
2059 :
2060 220022 : if ( st_ivas->hBinRendererTd != NULL )
2061 : {
2062 108932 : ro_md_flag = 1;
2063 : }
2064 : else
2065 : {
2066 111090 : ro_md_flag = 0;
2067 : }
2068 :
2069 220022 : if ( st_ivas->hHeadTrackData != NULL )
2070 : {
2071 220022 : Quaternion = st_ivas->hHeadTrackData->Quaternions[0];
2072 : }
2073 : else
2074 : {
2075 0 : Quaternion.w = -3.0f;
2076 0 : Quaternion.x = 0.0f;
2077 0 : Quaternion.y = 0.0f;
2078 0 : Quaternion.z = 0.0f;
2079 : }
2080 :
2081 220022 : if ( ( error = ISAR_PRE_REND_MultiBinToSplitBinaural( &hSplitBinRend->splitrend,
2082 : Quaternion,
2083 220022 : st_ivas->hRenderConfig->split_rend_config.splitRendBitRate,
2084 220022 : st_ivas->hRenderConfig->split_rend_config.codec,
2085 220022 : st_ivas->hRenderConfig->split_rend_config.isar_frame_size_ms,
2086 220022 : st_ivas->hRenderConfig->split_rend_config.codec_frame_size_ms,
2087 : splitRendBits,
2088 : p_Cldfb_RealBuffer_Binaural,
2089 : p_Cldfb_ImagBuffer_Binaural,
2090 : max_band, p_head_pose_buf, 1, cldfb_in_flag, pcm_out_flag, ro_md_flag ) ) != IVAS_ERR_OK )
2091 : {
2092 0 : return error;
2093 : }
2094 :
2095 220022 : return IVAS_ERR_OK;
2096 : }
2097 :
2098 :
2099 : /*---------------------------------------------------------------------*
2100 : * IVAS_DEC_GetSplitBinauralBitstream( )
2101 : *
2102 : * Get split-rendering bitstream
2103 : *---------------------------------------------------------------------*/
2104 :
2105 109200 : ivas_error IVAS_DEC_GetSplitBinauralBitstream(
2106 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2107 : void *pcmBuf_out, /* o : output synthesis signal for BINAURAL_SPLIT_PCM */
2108 : ISAR_SPLIT_REND_BITS_DATA *splitRendBits, /* o : output split rendering bits */
2109 : int16_t *nOutSamples, /* o : number of samples per channel written to output buffer */
2110 : bool *needNewFrame /* o : indication that the decoder needs a new frame */
2111 : )
2112 : {
2113 : Decoder_Struct *st_ivas;
2114 : ivas_error error;
2115 : float head_pose_buf[BINAURAL_CHANNELS * MAX_HEAD_ROT_POSES][L_FRAME48k];
2116 : float *p_head_pose_buf[BINAURAL_CHANNELS * MAX_HEAD_ROT_POSES];
2117 : int16_t i;
2118 : int16_t pcm_out_flag;
2119 : int16_t numSamplesPerChannelToOutput;
2120 :
2121 109200 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
2122 : {
2123 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2124 : }
2125 :
2126 109200 : st_ivas = hIvasDec->st_ivas;
2127 :
2128 109200 : if ( is_split_rendering_enabled( st_ivas->hDecoderConfig, st_ivas->hRenderConfig ) == 0 )
2129 : {
2130 0 : return IVAS_ERR_WRONG_PARAMS;
2131 : }
2132 :
2133 109200 : pcm_out_flag = ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ? 1 : 0;
2134 109200 : numSamplesPerChannelToOutput = isar_get_frame_size( st_ivas );
2135 :
2136 109200 : if ( ( error = isar_render_poses( hIvasDec, numSamplesPerChannelToOutput, nOutSamples, needNewFrame ) ) != IVAS_ERR_OK )
2137 : {
2138 0 : return error;
2139 : }
2140 :
2141 109200 : if ( !hIvasDec->hasBeenFedFirstGoodFrame )
2142 : {
2143 0 : return IVAS_ERR_OK;
2144 : }
2145 :
2146 1856400 : for ( i = 0; i < BINAURAL_CHANNELS * MAX_HEAD_ROT_POSES; ++i )
2147 : {
2148 1747200 : p_head_pose_buf[i] = head_pose_buf[i];
2149 : }
2150 :
2151 109200 : if ( ( error = isar_generate_metadata_and_bitstream( st_ivas, p_head_pose_buf, *nOutSamples, splitRendBits ) ) != IVAS_ERR_OK )
2152 : {
2153 0 : return error;
2154 : }
2155 :
2156 : /* convert to int16 with limiting for BINAURAL_SPLIT_PCM */
2157 109200 : if ( pcm_out_flag )
2158 : {
2159 : #ifndef DISABLE_LIMITER
2160 950 : ivas_limiter_dec( st_ivas->hLimiter, p_head_pose_buf, st_ivas->hDecoderConfig->nchan_out, numSamplesPerChannelToOutput, st_ivas->BER_detect );
2161 : #endif
2162 :
2163 : #ifdef DEBUGGING
2164 : st_ivas->noClipping +=
2165 : #endif
2166 950 : ivas_syn_output( p_head_pose_buf, numSamplesPerChannelToOutput, st_ivas->hDecoderConfig->nchan_out, (int16_t *) pcmBuf_out );
2167 : }
2168 :
2169 109200 : return IVAS_ERR_OK;
2170 : }
2171 :
2172 :
2173 : /*---------------------------------------------------------------------*
2174 : * ivas_dec_setup_all()
2175 : *
2176 : * Set-up all decoder parts: IVAS decoder, ISAR
2177 : *---------------------------------------------------------------------*/
2178 :
2179 716075 : static ivas_error ivas_dec_setup_all(
2180 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2181 : uint8_t *nTransportChannels, /* o : number of decoded transport PCM channels */
2182 : ISAR_SPLIT_REND_BITS_DATA *splitRendBits /* o : output split rendering bits */
2183 : )
2184 : {
2185 : ivas_error error;
2186 :
2187 716075 : if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
2188 : {
2189 5150 : *nTransportChannels = 1;
2190 : }
2191 : else
2192 : {
2193 : Decoder_Struct *st_ivas;
2194 :
2195 710925 : st_ivas = hIvasDec->st_ivas;
2196 :
2197 : /* Setup IVAS split rendering */
2198 710925 : if ( splitRendBits != NULL )
2199 : {
2200 207483 : if ( ( error = isar_set_split_rend_setup( st_ivas->hSplitBinRend, &st_ivas->hRenderConfig->split_rend_config, st_ivas->hCombinedOrientationData, splitRendBits ) ) != IVAS_ERR_OK )
2201 : {
2202 0 : return error;
2203 : }
2204 : }
2205 :
2206 : /*----------------------------------------------------------------*
2207 : * IVAS decoder setup
2208 : * - read IVAS format signaling
2209 : * - read IVAS format specific signaling
2210 : * - initialize decoder in the first frame based on IVAS format and number of transport channels
2211 : * - reconfigure the decoder when the number of TC or IVAS total bitrate change
2212 : *----------------------------------------------------------------*/
2213 :
2214 710925 : if ( st_ivas->bfi == 0 )
2215 : {
2216 691776 : if ( ( error = ivas_dec_setup( st_ivas ) ) != IVAS_ERR_OK )
2217 : {
2218 0 : return error;
2219 : }
2220 : }
2221 :
2222 710925 : *nTransportChannels = (uint8_t) st_ivas->hTcBuffer->nchan_transport_rend;
2223 :
2224 : /*-----------------------------------------------------------------*
2225 : * ISAR:
2226 : * - initialize ISAR handle at the first frame
2227 : * - reconfigure the ISAR handle in case of bitrate switching (renderer might change)
2228 : *-----------------------------------------------------------------*/
2229 :
2230 710925 : if ( st_ivas->ini_frame == 0 && splitRendBits != NULL )
2231 : {
2232 1032 : if ( ( error = ivas_dec_init_split_rend( st_ivas ) ) != IVAS_ERR_OK )
2233 : {
2234 0 : return error;
2235 : }
2236 : }
2237 :
2238 710925 : if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
2239 : {
2240 207483 : if ( ( error = ivas_dec_reconfig_split_rend( st_ivas ) ) != IVAS_ERR_OK )
2241 : {
2242 0 : return error;
2243 : }
2244 : }
2245 : }
2246 :
2247 716075 : return IVAS_ERR_OK;
2248 : }
2249 :
2250 :
2251 : /*---------------------------------------------------------------------*
2252 : * IVAS_DEC_GetNumObjects( )
2253 : *
2254 : * Returns the number of objects available in the decoded bitstream
2255 : *---------------------------------------------------------------------*/
2256 :
2257 27712 : ivas_error IVAS_DEC_GetNumObjects(
2258 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2259 : uint16_t *numObjects /* o : number of objects for which the decoder has been configured */
2260 : )
2261 : {
2262 : int16_t is_masa_ism;
2263 27712 : is_masa_ism = 0;
2264 :
2265 27712 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
2266 : {
2267 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2268 : }
2269 :
2270 27712 : if ( hIvasDec->st_ivas->hMasa != NULL )
2271 : {
2272 4009 : if ( hIvasDec->st_ivas->hMasa->config.input_ivas_format == MASA_ISM_FORMAT )
2273 : {
2274 4009 : is_masa_ism = 1;
2275 : }
2276 : }
2277 :
2278 27712 : if ( hIvasDec->st_ivas->ivas_format == ISM_FORMAT || hIvasDec->st_ivas->ivas_format == SBA_ISM_FORMAT || hIvasDec->st_ivas->ivas_format == MASA_ISM_FORMAT || is_masa_ism )
2279 : {
2280 27712 : *numObjects = hIvasDec->st_ivas->nchan_ism;
2281 : }
2282 : else
2283 : {
2284 0 : *numObjects = 0;
2285 : }
2286 :
2287 27712 : return IVAS_ERR_OK;
2288 : }
2289 :
2290 :
2291 : /*---------------------------------------------------------------------*
2292 : * IVAS_DEC_GetFormat( )
2293 : *
2294 : * Returns the format of currently decoded bitstream.
2295 : * Note: bitstream format is only known after the first (good) frame has been decoded.
2296 : *---------------------------------------------------------------------*/
2297 :
2298 52 : ivas_error IVAS_DEC_GetFormat(
2299 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2300 : IVAS_DEC_BS_FORMAT *format /* o : format detected from bitstream fed to the decoder */
2301 : )
2302 : {
2303 52 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
2304 : {
2305 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2306 : }
2307 :
2308 52 : if ( hIvasDec->hasDecodedFirstGoodFrame )
2309 : {
2310 52 : *format = mapIvasFormat( hIvasDec->st_ivas->ivas_format );
2311 : }
2312 : else
2313 : {
2314 0 : *format = IVAS_DEC_BS_UNKOWN;
2315 : }
2316 :
2317 52 : if ( *format == IVAS_DEC_BS_MASA && hIvasDec->st_ivas->hMasa != NULL )
2318 : {
2319 17 : if ( hIvasDec->st_ivas->hMasa->config.input_ivas_format == MASA_ISM_FORMAT )
2320 : {
2321 0 : *format = IVAS_DEC_BS_MASA_ISM;
2322 : }
2323 : }
2324 :
2325 52 : return IVAS_ERR_OK;
2326 : }
2327 :
2328 :
2329 : /*---------------------------------------------------------------------*
2330 : * getOutputBufferSize()
2331 : *
2332 : * Get size of output buffer in samples
2333 : *---------------------------------------------------------------------*/
2334 :
2335 3342 : static int16_t getOutputBufferSize(
2336 : const Decoder_Struct *st_ivas /* i : IVAS decoder handle */
2337 : )
2338 : {
2339 3342 : if ( st_ivas->hDecoderConfig == NULL )
2340 : {
2341 0 : return -1;
2342 : }
2343 :
2344 3342 : if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
2345 : {
2346 104 : return (int16_t) ( st_ivas->hDecoderConfig->output_Fs * ( MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS ) / FRAMES_PER_SEC );
2347 : }
2348 3238 : else if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM )
2349 : {
2350 6 : if ( st_ivas->hLsSetupCustom == NULL )
2351 : {
2352 0 : return -1;
2353 : }
2354 :
2355 6 : return (int16_t) ( st_ivas->hDecoderConfig->output_Fs * ( st_ivas->hLsSetupCustom->num_spk + st_ivas->hLsSetupCustom->num_lfe ) / FRAMES_PER_SEC );
2356 : }
2357 : else
2358 : {
2359 3232 : return (int16_t) ( st_ivas->hDecoderConfig->output_Fs * st_ivas->hDecoderConfig->nchan_out / FRAMES_PER_SEC );
2360 : }
2361 : }
2362 :
2363 :
2364 : /*---------------------------------------------------------------------*
2365 : * IVAS_DEC_GetOutputBufferSize()
2366 : *
2367 : * Returns size of output buffer in samples
2368 : *---------------------------------------------------------------------*/
2369 :
2370 3342 : ivas_error IVAS_DEC_GetOutputBufferSize(
2371 : const IVAS_DEC_HANDLE hIvasDec, /* i : IVAS decoder handle */
2372 : int16_t *outputBufferSize /* o : total number of samples expected in the output buffer for current decoder configuration */
2373 : )
2374 : {
2375 3342 : if ( outputBufferSize == NULL || hIvasDec == NULL || hIvasDec->st_ivas == NULL )
2376 : {
2377 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2378 : }
2379 :
2380 3342 : *outputBufferSize = getOutputBufferSize( hIvasDec->st_ivas );
2381 :
2382 3342 : if ( *outputBufferSize == -1 )
2383 : {
2384 0 : return IVAS_ERR_INVALID_OUTPUT_BUFFER_SIZE;
2385 : }
2386 : else
2387 : {
2388 3342 : return IVAS_ERR_OK;
2389 : }
2390 : }
2391 :
2392 :
2393 : /*---------------------------------------------------------------------*
2394 : * IVAS_DEC_GetNumOutputChannels( )
2395 : *
2396 : * Returns number of output channels
2397 : *---------------------------------------------------------------------*/
2398 :
2399 1671 : ivas_error IVAS_DEC_GetNumOutputChannels(
2400 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2401 : int16_t *numOutputChannels /* o : number of PCM output channels */
2402 : )
2403 : {
2404 1671 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
2405 : {
2406 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2407 : }
2408 :
2409 1671 : if ( hIvasDec->hasDecodedFirstGoodFrame )
2410 : {
2411 1671 : *numOutputChannels = hIvasDec->st_ivas->hDecoderConfig->nchan_out;
2412 : }
2413 : else
2414 : {
2415 0 : *numOutputChannels = 0;
2416 : }
2417 :
2418 1671 : return IVAS_ERR_OK;
2419 : }
2420 :
2421 :
2422 : /*---------------------------------------------------------------------*
2423 : * IVAS_DEC_GetObjectMetadata( )
2424 : *
2425 : * Get metadata of one object decoded in the most recent frame
2426 : *---------------------------------------------------------------------*/
2427 :
2428 79846 : ivas_error IVAS_DEC_GetObjectMetadata(
2429 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2430 : IVAS_ISM_METADATA *metadata, /* o : struct where metadata decoded in most recently decoded frame will be written */
2431 : const uint16_t zero_flag, /* i : if this flag is enabled, this function outputs a zero-initialized metadata struct */
2432 : const uint16_t objectIdx /* i : index of the queried object */
2433 : )
2434 : {
2435 : Decoder_Struct *st_ivas;
2436 : ISM_METADATA_HANDLE hIsmMeta;
2437 : int16_t is_masa_ism;
2438 79846 : is_masa_ism = 0;
2439 :
2440 79846 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
2441 : {
2442 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2443 : }
2444 :
2445 79846 : st_ivas = hIvasDec->st_ivas;
2446 79846 : if ( hIvasDec->st_ivas->hMasa != NULL )
2447 : {
2448 13052 : if ( hIvasDec->st_ivas->hMasa->config.input_ivas_format == MASA_ISM_FORMAT )
2449 : {
2450 13052 : is_masa_ism = 1;
2451 : }
2452 : }
2453 79846 : if ( st_ivas->ivas_format != ISM_FORMAT && st_ivas->ivas_format != MASA_ISM_FORMAT && st_ivas->ivas_format != SBA_ISM_FORMAT && is_masa_ism == 0 )
2454 : {
2455 0 : return IVAS_ERR_WRONG_MODE;
2456 : }
2457 :
2458 79846 : if ( objectIdx >= st_ivas->nchan_ism )
2459 : {
2460 0 : return IVAS_ERR_INVALID_INDEX;
2461 : }
2462 :
2463 79846 : hIsmMeta = st_ivas->hIsmMetaData[objectIdx];
2464 :
2465 79846 : if ( hIsmMeta == NULL || zero_flag || ( st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ ) )
2466 : {
2467 3442 : metadata->azimuth = 0.f;
2468 3442 : metadata->elevation = 0.f;
2469 3442 : metadata->radius = 1.f;
2470 3442 : metadata->spread = 0.f;
2471 3442 : metadata->gainFactor = 1.f;
2472 3442 : metadata->yaw = 0.f;
2473 3442 : metadata->pitch = 0.f;
2474 3442 : metadata->non_diegetic_flag = 0;
2475 : }
2476 : else
2477 : {
2478 76404 : if ( st_ivas->ism_mode == ISM_MODE_DISC )
2479 : {
2480 55612 : metadata->azimuth = hIsmMeta->edited_azimuth;
2481 55612 : metadata->elevation = hIsmMeta->edited_elevation;
2482 55612 : metadata->radius = hIsmMeta->edited_radius;
2483 :
2484 55612 : metadata->yaw = hIsmMeta->edited_yaw;
2485 55612 : metadata->pitch = hIsmMeta->edited_pitch;
2486 55612 : metadata->spread = 0.f;
2487 :
2488 55612 : metadata->gainFactor = hIsmMeta->edited_gain;
2489 55612 : metadata->non_diegetic_flag = hIsmMeta->non_diegetic_flag;
2490 : }
2491 20792 : else if ( st_ivas->ism_mode == ISM_MODE_PARAM )
2492 : {
2493 2172 : metadata->azimuth = st_ivas->hParamIsmDec->edited_azimuth_values[objectIdx];
2494 2172 : metadata->elevation = st_ivas->hParamIsmDec->edited_elevation_values[objectIdx];
2495 2172 : metadata->radius = hIsmMeta->radius;
2496 2172 : metadata->yaw = hIsmMeta->yaw;
2497 2172 : metadata->pitch = hIsmMeta->pitch;
2498 2172 : metadata->spread = 0.f;
2499 2172 : metadata->gainFactor = 1.f;
2500 2172 : metadata->non_diegetic_flag = hIsmMeta->non_diegetic_flag;
2501 : }
2502 18620 : else if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC || st_ivas->ism_mode == ISM_MASA_MODE_DISC || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
2503 : {
2504 17620 : metadata->azimuth = st_ivas->hIsmMetaData[objectIdx]->edited_azimuth;
2505 17620 : metadata->elevation = st_ivas->hIsmMetaData[objectIdx]->edited_elevation;
2506 17620 : metadata->radius = st_ivas->hIsmMetaData[objectIdx]->edited_radius;
2507 17620 : metadata->yaw = st_ivas->hIsmMetaData[objectIdx]->edited_yaw;
2508 17620 : metadata->pitch = st_ivas->hIsmMetaData[objectIdx]->edited_pitch;
2509 17620 : metadata->spread = 0.f;
2510 17620 : metadata->gainFactor = st_ivas->hIsmMetaData[objectIdx]->edited_gain;
2511 17620 : metadata->non_diegetic_flag = st_ivas->hIsmMetaData[objectIdx]->non_diegetic_flag;
2512 : }
2513 : else
2514 : {
2515 1000 : metadata->azimuth = hIsmMeta->azimuth;
2516 1000 : metadata->elevation = hIsmMeta->elevation;
2517 1000 : metadata->radius = hIsmMeta->radius;
2518 1000 : metadata->yaw = hIsmMeta->yaw;
2519 1000 : metadata->pitch = hIsmMeta->pitch;
2520 1000 : metadata->spread = 0.f;
2521 1000 : metadata->gainFactor = 1.f;
2522 1000 : metadata->non_diegetic_flag = hIsmMeta->non_diegetic_flag;
2523 : }
2524 : }
2525 :
2526 79846 : return IVAS_ERR_OK;
2527 : }
2528 :
2529 :
2530 : /*---------------------------------------------------------------------*
2531 : * IVAS_DEC_GetMasaMetadata( )
2532 : *
2533 : * Get metadata of the most recently decoded MASA frame
2534 : *---------------------------------------------------------------------*/
2535 :
2536 19471 : ivas_error IVAS_DEC_GetMasaMetadata(
2537 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2538 : MASA_DECODER_EXT_OUT_META_HANDLE *hMasaExtOutMeta, /* o : pointer to handle, which will be set to point to metadata from the most recently decoded frame */
2539 : const uint8_t getFromJbmBuffer /* i : get metadata from a JBM buffer */
2540 : )
2541 : {
2542 19471 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
2543 : {
2544 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2545 : }
2546 :
2547 19471 : if ( hIvasDec->st_ivas->ivas_format != MASA_FORMAT && hIvasDec->st_ivas->ivas_format != MASA_ISM_FORMAT )
2548 : {
2549 0 : return IVAS_ERR_WRONG_MODE;
2550 : }
2551 :
2552 19471 : if ( getFromJbmBuffer )
2553 : {
2554 3012 : ivas_jbm_masa_sf_to_sf_map( hIvasDec->st_ivas );
2555 : }
2556 :
2557 19471 : *hMasaExtOutMeta = hIvasDec->st_ivas->hMasa->data.extOutMeta;
2558 :
2559 19471 : return IVAS_ERR_OK;
2560 : }
2561 :
2562 :
2563 : /*---------------------------------------------------------------------*
2564 : * IVAS_DEC_FeedHeadTrackData( )
2565 : *
2566 : * Feed the decoder with the head tracking data
2567 : *---------------------------------------------------------------------*/
2568 :
2569 1172046 : ivas_error IVAS_DEC_FeedHeadTrackData(
2570 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2571 : IVAS_QUATERNION orientation, /* i : head-tracking data, listener orientation */
2572 : IVAS_VECTOR3 Pos, /* i : listener position */
2573 : const int16_t subframe_idx, /* i : subframe index */
2574 : const ISAR_SPLIT_REND_ROT_AXIS rot_axis /* i : external control for rotation axis for split rendering */
2575 : )
2576 : {
2577 : HEAD_TRACK_DATA_HANDLE hHeadTrackData;
2578 : ivas_error error;
2579 :
2580 1172046 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
2581 : {
2582 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2583 : }
2584 :
2585 1172046 : hHeadTrackData = hIvasDec->st_ivas->hHeadTrackData;
2586 :
2587 1172046 : if ( hHeadTrackData == NULL )
2588 : {
2589 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2590 : }
2591 :
2592 : /* Move head-tracking data to the decoder handle */
2593 : /* check for Euler angle signaling */
2594 1172046 : if ( orientation.w == -3.0f )
2595 : {
2596 652154 : Euler2Quat( deg2rad( orientation.x ), deg2rad( orientation.y ), deg2rad( orientation.z ), &orientation );
2597 : }
2598 :
2599 1172046 : if ( ( error = ivas_orient_trk_Process( hHeadTrackData->OrientationTracker, orientation, FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES, &hHeadTrackData->Quaternions[subframe_idx] ) ) != IVAS_ERR_OK )
2600 : {
2601 0 : return error;
2602 : }
2603 :
2604 1172046 : hHeadTrackData->Pos[subframe_idx].x = Pos.x;
2605 1172046 : hHeadTrackData->Pos[subframe_idx].y = Pos.y;
2606 1172046 : hHeadTrackData->Pos[subframe_idx].z = Pos.z;
2607 :
2608 1172046 : hHeadTrackData->sr_pose_pred_axis = rot_axis;
2609 1172046 : hIvasDec->updateOrientation = true;
2610 :
2611 1172046 : return IVAS_ERR_OK;
2612 : }
2613 :
2614 :
2615 : /*---------------------------------------------------------------------*
2616 : * IVAS_DEC_FeedRefRotData( )
2617 : *
2618 : * Feed the decoder with the reference rotation data
2619 : *---------------------------------------------------------------------*/
2620 :
2621 0 : ivas_error IVAS_DEC_FeedRefRotData(
2622 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2623 : IVAS_QUATERNION rotation /* i : reference rotation data */
2624 : )
2625 : {
2626 : ivas_orient_trk_state_t *pOtr;
2627 :
2628 0 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHeadTrackData == NULL || hIvasDec->st_ivas->hHeadTrackData->OrientationTracker == NULL )
2629 : {
2630 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2631 : }
2632 :
2633 0 : pOtr = hIvasDec->st_ivas->hHeadTrackData->OrientationTracker;
2634 :
2635 0 : pOtr->refRot.w = rotation.w;
2636 0 : pOtr->refRot.x = rotation.x;
2637 0 : pOtr->refRot.z = rotation.z;
2638 0 : pOtr->refRot.y = rotation.y;
2639 :
2640 0 : hIvasDec->updateOrientation = true;
2641 :
2642 0 : return IVAS_ERR_OK;
2643 : }
2644 :
2645 :
2646 : /*---------------------------------------------------------------------*
2647 : * IVAS_DEC_FeedRefVectorData( )
2648 : *
2649 : * Feed the decoder with a reference vector spanning from listenerPos
2650 : * to refPos. Only available in OTR_TRACKING_REF_POS and
2651 : * OTR_TRACKING_REF_POS_LEV modes.
2652 : *---------------------------------------------------------------------*/
2653 :
2654 4004 : ivas_error IVAS_DEC_FeedRefVectorData(
2655 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2656 : const IVAS_VECTOR3 listenerPos, /* i : Listener position */
2657 : const IVAS_VECTOR3 refPos /* i : Reference position */
2658 : )
2659 : {
2660 : ivas_orient_trk_state_t *pOtr;
2661 :
2662 4004 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHeadTrackData == NULL || hIvasDec->st_ivas->hHeadTrackData->OrientationTracker == NULL )
2663 : {
2664 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2665 : }
2666 :
2667 4004 : pOtr = hIvasDec->st_ivas->hHeadTrackData->OrientationTracker;
2668 :
2669 4004 : hIvasDec->updateOrientation = true;
2670 :
2671 4004 : return ivas_orient_trk_SetReferenceVector( pOtr, listenerPos, refPos );
2672 : }
2673 :
2674 :
2675 : /*---------------------------------------------------------------------*
2676 : * IVAS_DEC_FeedExternalOrientationData( )
2677 : *
2678 : * Feed the decoder with the external orientation data
2679 : *---------------------------------------------------------------------*/
2680 :
2681 132524 : ivas_error IVAS_DEC_FeedExternalOrientationData(
2682 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2683 : IVAS_QUATERNION orientation, /* i : external orientation data */
2684 : int8_t enableHeadRotation, /* i : flag to enable head rotation for this frame */
2685 : int8_t enableExternalOrientation, /* i : flag to enable external orientation for this frame */
2686 : int8_t enableRotationInterpolation, /* i : flag to interpolate rotations from current and previous frames */
2687 : int16_t numFramesToTargetOrientation, /* i : number of frames until target orientation is reached */
2688 : const int16_t subframe_idx /* i : subframe index */
2689 : )
2690 : {
2691 : EXTERNAL_ORIENTATION_HANDLE hExternalOrientationData;
2692 :
2693 132524 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
2694 : {
2695 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2696 : }
2697 :
2698 132524 : hExternalOrientationData = hIvasDec->st_ivas->hExtOrientationData;
2699 :
2700 132524 : if ( hExternalOrientationData == NULL )
2701 : {
2702 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2703 : }
2704 :
2705 : /* Move external orientation data to the decoder handle (invert orientations) */
2706 132524 : QuaternionInverse( orientation, &hExternalOrientationData->Quaternions[subframe_idx] );
2707 :
2708 132524 : hExternalOrientationData->enableHeadRotation[subframe_idx] = enableHeadRotation;
2709 132524 : hExternalOrientationData->enableExternalOrientation[subframe_idx] = enableExternalOrientation;
2710 132524 : hExternalOrientationData->enableRotationInterpolation[subframe_idx] = enableRotationInterpolation;
2711 132524 : hExternalOrientationData->numFramesToTargetOrientation[subframe_idx] = numFramesToTargetOrientation;
2712 :
2713 132524 : hIvasDec->updateOrientation = true;
2714 :
2715 132524 : return IVAS_ERR_OK;
2716 : }
2717 :
2718 :
2719 : /*---------------------------------------------------------------------*
2720 : * IVAS_DEC_FeedCustomLsData( )
2721 : *
2722 : * Feed the decoder with the Custom loudspeaker data
2723 : *---------------------------------------------------------------------*/
2724 :
2725 : /*! r: error code */
2726 3 : ivas_error IVAS_DEC_FeedCustomLsData(
2727 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2728 : const IVAS_CUSTOM_LS_DATA hLsCustomData /* i : Custom loudspeaker setup data */
2729 : )
2730 : {
2731 : int16_t i, is_planar;
2732 : IVAS_LSSETUP_CUSTOM_HANDLE hLsSetupCustom;
2733 :
2734 3 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
2735 : {
2736 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2737 : }
2738 :
2739 3 : hLsSetupCustom = hIvasDec->st_ivas->hLsSetupCustom;
2740 :
2741 3 : if ( hLsSetupCustom == NULL )
2742 : {
2743 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2744 : }
2745 :
2746 : /* Move Custom LS layout data to the decoder handle */
2747 :
2748 : /* Loudspeaker azimuths and elevations */
2749 3 : hLsSetupCustom->num_spk = hLsCustomData.num_spk;
2750 :
2751 3 : mvr2r( hLsCustomData.azimuth, hLsSetupCustom->ls_azimuth, hLsCustomData.num_spk );
2752 3 : mvr2r( hLsCustomData.elevation, hLsSetupCustom->ls_elevation, hLsCustomData.num_spk );
2753 :
2754 : /* Set planar flag */
2755 3 : is_planar = 1;
2756 51 : for ( i = 0; i < hLsCustomData.num_spk; i++ )
2757 : {
2758 48 : if ( is_planar && hLsSetupCustom->ls_elevation[i] != 0.0f )
2759 : {
2760 3 : is_planar = 0;
2761 : }
2762 : }
2763 3 : hLsSetupCustom->is_planar_setup = is_planar;
2764 :
2765 : /* Loudspeaker LFE */
2766 3 : hLsSetupCustom->num_lfe = hLsCustomData.num_lfe;
2767 3 : mvs2s( hLsCustomData.lfe_idx, hLsSetupCustom->lfe_idx, hLsCustomData.num_lfe );
2768 :
2769 3 : return IVAS_ERR_OK;
2770 : }
2771 :
2772 :
2773 : /*---------------------------------------------------------------------*
2774 : * IVAS_DEC_GetHrtfTDrendHandle( )
2775 : *
2776 : * Get TD binaural renderer handle
2777 : *---------------------------------------------------------------------*/
2778 :
2779 162 : ivas_error IVAS_DEC_GetHrtfTDrendHandle(
2780 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2781 : IVAS_DEC_HRTF_TD_HANDLE **hHrtfTD /* o : HRTF handle */
2782 : )
2783 : {
2784 162 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHrtfTD == NULL )
2785 : {
2786 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2787 : }
2788 :
2789 162 : *hHrtfTD = &hIvasDec->st_ivas->hHrtfTD;
2790 :
2791 162 : return IVAS_ERR_OK;
2792 : }
2793 :
2794 :
2795 : /*---------------------------------------------------------------------*
2796 : * IVAS_DEC_GetHrtfCRendHandle( )
2797 : *
2798 : * Get Crend binaural renderer handle
2799 : *---------------------------------------------------------------------*/
2800 :
2801 28 : ivas_error IVAS_DEC_GetHrtfCRendHandle(
2802 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2803 : IVAS_DEC_HRTF_CREND_HANDLE **hHrtfCrend /* o : Crend HRTF handle */
2804 : )
2805 : {
2806 28 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHrtfCrend == NULL )
2807 : {
2808 0 : return IVAS_ERR_WRONG_PARAMS;
2809 : }
2810 :
2811 28 : *hHrtfCrend = &hIvasDec->st_ivas->hHrtfCrend;
2812 :
2813 28 : return IVAS_ERR_OK;
2814 : }
2815 :
2816 :
2817 : /*---------------------------------------------------------------------*
2818 : * IVAS_DEC_GetHrtfFastConvHandle( )
2819 : *
2820 : * Get FastConv binaural renderer handle
2821 : *---------------------------------------------------------------------*/
2822 :
2823 73 : ivas_error IVAS_DEC_GetHrtfFastConvHandle(
2824 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2825 : IVAS_DEC_HRTF_FASTCONV_HANDLE **hHrtfFastConv /* o : FASTCONV HRTF handle */
2826 : )
2827 : {
2828 73 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHrtfFastConv == NULL )
2829 : {
2830 0 : return IVAS_ERR_WRONG_PARAMS;
2831 : }
2832 :
2833 73 : *hHrtfFastConv = &hIvasDec->st_ivas->hHrtfFastConv;
2834 :
2835 73 : return IVAS_ERR_OK;
2836 : }
2837 :
2838 :
2839 : /*---------------------------------------------------------------------*
2840 : * IVAS_DEC_GetHrtfParamBinHandle( )
2841 : *
2842 : * Get Parametric binaural renderer handle
2843 : *---------------------------------------------------------------------*/
2844 :
2845 339 : ivas_error IVAS_DEC_GetHrtfParamBinHandle(
2846 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2847 : IVAS_DEC_HRTF_PARAMBIN_HANDLE **hHrtfParambin /* o : Parametric binauralizer HRTF handle */
2848 : )
2849 : {
2850 339 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHrtfParambin == NULL )
2851 : {
2852 0 : return IVAS_ERR_WRONG_PARAMS;
2853 : }
2854 :
2855 339 : *hHrtfParambin = &hIvasDec->st_ivas->hHrtfParambin;
2856 :
2857 339 : return IVAS_ERR_OK;
2858 : }
2859 :
2860 : /*---------------------------------------------------------------------*
2861 : * IVAS_DEC_GetHrtfStatisticsHandle( )
2862 : *
2863 : * Get HRTF statistics (room effect) binaural renderer handle
2864 : *---------------------------------------------------------------------*/
2865 :
2866 3 : ivas_error IVAS_DEC_GetHrtfStatisticsHandle(
2867 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2868 : IVAS_DEC_HRTF_STATISTICS_HANDLE **hHrtfStatistics /* o : HRTF statistics handle */
2869 : )
2870 : {
2871 3 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHrtfStatistics == NULL )
2872 : {
2873 0 : return IVAS_ERR_WRONG_PARAMS;
2874 : }
2875 :
2876 3 : *hHrtfStatistics = &hIvasDec->st_ivas->hHrtfStatistics;
2877 :
2878 3 : return IVAS_ERR_OK;
2879 : }
2880 :
2881 :
2882 : /*---------------------------------------------------------------------*
2883 : * IVAS_DEC_HRTF_binary_open( )
2884 : *
2885 : * Allocate HRTF binary handles
2886 : *---------------------------------------------------------------------*/
2887 :
2888 898 : ivas_error IVAS_DEC_HRTF_binary_open(
2889 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2890 : const IVAS_BIN_RENDERER_TYPE binaural_renderer /* i : binaural renderer type */
2891 : )
2892 : {
2893 : ivas_error error;
2894 : Decoder_Struct *st_ivas;
2895 :
2896 898 : st_ivas = hIvasDec->st_ivas;
2897 :
2898 898 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
2899 : {
2900 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2901 : }
2902 :
2903 898 : if ( st_ivas->hDecoderConfig->Opt_HRTF_binary )
2904 : {
2905 : /* TD binaural renderer */
2906 898 : if ( ( binaural_renderer == IVAS_BIN_RENDERER_TYPE_TDREND || binaural_renderer == IVAS_BIN_RENDERER_TYPE_DEFAULT ) && st_ivas->hHrtfTD == NULL )
2907 : {
2908 162 : if ( ( error = ivas_HRTF_td_binary_open( &( st_ivas->hHrtfTD ) ) ) != IVAS_ERR_OK )
2909 : {
2910 0 : return error;
2911 : }
2912 : }
2913 :
2914 : /* Crend binaural renderer */
2915 898 : if ( ( binaural_renderer == IVAS_BIN_RENDERER_TYPE_CREND || binaural_renderer == IVAS_BIN_RENDERER_TYPE_DEFAULT ) && st_ivas->hHrtfCrend == NULL )
2916 : {
2917 28 : if ( ( error = ivas_HRTF_CRend_binary_open( &( st_ivas->hHrtfCrend ) ) ) != IVAS_ERR_OK )
2918 : {
2919 0 : return error;
2920 : }
2921 : }
2922 :
2923 : /* FastConv binaural renderer */
2924 898 : if ( ( binaural_renderer == IVAS_BIN_RENDERER_TYPE_FASTCONV || binaural_renderer == IVAS_BIN_RENDERER_TYPE_DEFAULT ) && st_ivas->hHrtfFastConv == NULL )
2925 : {
2926 68 : if ( ( error = ivas_HRTF_fastconv_binary_open( &st_ivas->hHrtfFastConv ) ) != IVAS_ERR_OK )
2927 : {
2928 0 : return error;
2929 : }
2930 : }
2931 :
2932 : /* Parametric binaural renderer */
2933 898 : if ( ( binaural_renderer == IVAS_BIN_RENDERER_TYPE_PARAMBIN || binaural_renderer == IVAS_BIN_RENDERER_TYPE_DEFAULT ) && st_ivas->hHrtfParambin == NULL )
2934 : {
2935 90 : if ( ( error = ivas_HRTF_parambin_binary_open( &st_ivas->hHrtfParambin ) ) != IVAS_ERR_OK )
2936 : {
2937 0 : return error;
2938 : }
2939 : }
2940 :
2941 898 : if ( st_ivas->hHrtfStatistics == NULL && ( binaural_renderer == IVAS_BIN_RENDERER_TYPE_FASTCONV || binaural_renderer == IVAS_BIN_RENDERER_TYPE_CREND ) && st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
2942 : {
2943 3 : if ( ( error = ivas_HRTF_statistics_binary_open( &st_ivas->hHrtfStatistics ) ) != IVAS_ERR_OK )
2944 : {
2945 0 : return error;
2946 : }
2947 : }
2948 : }
2949 :
2950 898 : return IVAS_ERR_OK;
2951 : }
2952 :
2953 :
2954 : /*---------------------------------------------------------------------*
2955 : * IVAS_DEC_HRTF_binary_close( )
2956 : *
2957 : * Deallocate HRTF binary handles
2958 : *---------------------------------------------------------------------*/
2959 :
2960 449 : ivas_error IVAS_DEC_HRTF_binary_close(
2961 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2962 : const IVAS_BIN_RENDERER_TYPE binaural_renderer_old /* i : previous binaural renderer type */
2963 : )
2964 : {
2965 : Decoder_Struct *st_ivas;
2966 :
2967 449 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
2968 : {
2969 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2970 : }
2971 :
2972 449 : st_ivas = hIvasDec->st_ivas;
2973 :
2974 449 : if ( st_ivas->hDecoderConfig->Opt_HRTF_binary && st_ivas->ini_frame > 0 )
2975 : {
2976 425 : if ( !( binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_TDREND || binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_DEFAULT ) )
2977 : {
2978 422 : ivas_HRTF_td_binary_close( &st_ivas->hHrtfTD );
2979 : }
2980 :
2981 425 : if ( !( binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_CREND || binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_DEFAULT ) )
2982 : {
2983 : /* CRend binaural renderer handle */
2984 402 : ivas_HRTF_CRend_binary_close( &st_ivas->hHrtfCrend );
2985 : }
2986 :
2987 425 : if ( !( binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_FASTCONV || binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_DEFAULT ) )
2988 : {
2989 : /* Fastconv HRTF filters */
2990 360 : ivas_HRTF_fastconv_binary_close( &st_ivas->hHrtfFastConv );
2991 : }
2992 :
2993 425 : if ( !( binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_PARAMBIN || binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_DEFAULT ) )
2994 : {
2995 :
2996 : /* Parametric binauralizer HRTF filters */
2997 91 : ivas_HRTF_parambin_binary_close( &st_ivas->hHrtfParambin );
2998 : }
2999 : }
3000 :
3001 449 : return IVAS_ERR_OK;
3002 : }
3003 :
3004 :
3005 : /*---------------------------------------------------------------------*
3006 : * IVAS_DEC_AddAcousticEnvironment( )
3007 : *
3008 : * Adds acoustic environment configuration
3009 : *---------------------------------------------------------------------*/
3010 :
3011 43 : ivas_error IVAS_DEC_AddAcousticEnvironment(
3012 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
3013 : const IVAS_ROOM_ACOUSTICS_CONFIG_DATA roomAcousticsConfig /* i : Room acoustic configuration */
3014 : )
3015 : {
3016 : uint16_t n;
3017 : Decoder_Struct *st_ivas;
3018 43 : IVAS_ROOM_ACOUSTICS_CONFIG_DATA *pAE = NULL;
3019 :
3020 43 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || ( hIvasDec->st_ivas->acousticEnvironmentsCount > 0 && hIvasDec->st_ivas->pAcousticEnvironments == NULL ) )
3021 : {
3022 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
3023 : }
3024 :
3025 43 : st_ivas = hIvasDec->st_ivas;
3026 :
3027 : /* Check if already there */
3028 64 : for ( n = 0; n < st_ivas->acousticEnvironmentsCount; n++ )
3029 : {
3030 21 : if ( st_ivas->pAcousticEnvironments[n].aeID == roomAcousticsConfig.aeID )
3031 : {
3032 0 : pAE = &st_ivas->pAcousticEnvironments[n];
3033 0 : break;
3034 : }
3035 : }
3036 :
3037 : /* If not found */
3038 43 : if ( pAE == NULL )
3039 : {
3040 43 : IVAS_ROOM_ACOUSTICS_CONFIG_DATA *ppAE = malloc( ( st_ivas->acousticEnvironmentsCount + 1 ) * sizeof( IVAS_ROOM_ACOUSTICS_CONFIG_DATA ) );
3041 :
3042 43 : if ( ppAE == NULL )
3043 : {
3044 0 : return IVAS_ERR_FAILED_ALLOC;
3045 : }
3046 :
3047 64 : for ( n = 0; n < st_ivas->acousticEnvironmentsCount; n++ )
3048 : {
3049 21 : pAE = &ppAE[n];
3050 21 : pAE->aeID = st_ivas->pAcousticEnvironments[n].aeID;
3051 21 : pAE->nBands = st_ivas->pAcousticEnvironments[n].nBands;
3052 21 : pAE->acousticPreDelay = st_ivas->pAcousticEnvironments[n].acousticPreDelay;
3053 21 : pAE->inputPreDelay = st_ivas->pAcousticEnvironments[n].inputPreDelay;
3054 :
3055 21 : mvr2r( st_ivas->pAcousticEnvironments[n].pFc_input, pAE->pFc_input, CLDFB_NO_CHANNELS_MAX );
3056 21 : mvr2r( st_ivas->pAcousticEnvironments[n].pAcoustic_rt60, pAE->pAcoustic_rt60, CLDFB_NO_CHANNELS_MAX );
3057 21 : mvr2r( st_ivas->pAcousticEnvironments[n].pAcoustic_dsr, pAE->pAcoustic_dsr, CLDFB_NO_CHANNELS_MAX );
3058 :
3059 21 : pAE->use_er = st_ivas->pAcousticEnvironments[n].use_er;
3060 :
3061 21 : if ( pAE->use_er == 1 )
3062 : {
3063 7 : pAE->lowComplexity = st_ivas->pAcousticEnvironments[n].lowComplexity;
3064 7 : pAE->dimensions.x = st_ivas->pAcousticEnvironments[n].dimensions.x;
3065 7 : pAE->dimensions.y = st_ivas->pAcousticEnvironments[n].dimensions.y;
3066 7 : pAE->dimensions.z = st_ivas->pAcousticEnvironments[n].dimensions.z;
3067 7 : pAE->ListenerOrigin.x = st_ivas->pAcousticEnvironments[n].ListenerOrigin.x;
3068 7 : pAE->ListenerOrigin.y = st_ivas->pAcousticEnvironments[n].ListenerOrigin.y;
3069 7 : pAE->ListenerOrigin.z = st_ivas->pAcousticEnvironments[n].ListenerOrigin.z;
3070 :
3071 7 : mvr2r( st_ivas->pAcousticEnvironments[n].AbsCoeff, pAE->AbsCoeff, IVAS_ROOM_ABS_COEFF );
3072 : }
3073 : }
3074 :
3075 43 : free( st_ivas->pAcousticEnvironments );
3076 43 : st_ivas->pAcousticEnvironments = ppAE;
3077 43 : n = st_ivas->acousticEnvironmentsCount++;
3078 43 : pAE = &st_ivas->pAcousticEnvironments[n];
3079 : }
3080 :
3081 43 : pAE->aeID = roomAcousticsConfig.aeID;
3082 43 : pAE->nBands = roomAcousticsConfig.nBands;
3083 43 : pAE->acousticPreDelay = roomAcousticsConfig.acousticPreDelay;
3084 43 : pAE->inputPreDelay = roomAcousticsConfig.inputPreDelay;
3085 :
3086 43 : mvr2r( roomAcousticsConfig.pFc_input, pAE->pFc_input, CLDFB_NO_CHANNELS_MAX );
3087 43 : mvr2r( roomAcousticsConfig.pAcoustic_rt60, pAE->pAcoustic_rt60, CLDFB_NO_CHANNELS_MAX );
3088 43 : mvr2r( roomAcousticsConfig.pAcoustic_dsr, pAE->pAcoustic_dsr, CLDFB_NO_CHANNELS_MAX );
3089 :
3090 43 : pAE->use_er = roomAcousticsConfig.use_er;
3091 :
3092 43 : if ( pAE->use_er == 1 )
3093 : {
3094 18 : pAE->lowComplexity = roomAcousticsConfig.lowComplexity;
3095 18 : pAE->dimensions.x = roomAcousticsConfig.dimensions.x;
3096 18 : pAE->dimensions.y = roomAcousticsConfig.dimensions.y;
3097 18 : pAE->dimensions.z = roomAcousticsConfig.dimensions.z;
3098 18 : pAE->ListenerOrigin.x = roomAcousticsConfig.ListenerOrigin.x;
3099 18 : pAE->ListenerOrigin.y = roomAcousticsConfig.ListenerOrigin.y;
3100 18 : pAE->ListenerOrigin.z = roomAcousticsConfig.ListenerOrigin.z;
3101 :
3102 18 : mvr2r( roomAcousticsConfig.AbsCoeff, pAE->AbsCoeff, IVAS_ROOM_ABS_COEFF );
3103 : }
3104 :
3105 43 : return IVAS_ERR_OK;
3106 : }
3107 :
3108 :
3109 : /*---------------------------------------------------------------------*
3110 : * IVAS_DEC_GetAcousticEnvironment( )
3111 : *
3112 : * Gets acoustic environment configuration with a given ID
3113 : *---------------------------------------------------------------------*/
3114 :
3115 29 : ivas_error IVAS_DEC_GetAcousticEnvironment(
3116 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
3117 : uint16_t aeID, /* i : Acoustic environment ID */
3118 : IVAS_ROOM_ACOUSTICS_CONFIG_DATA *pAcEnv /* o : Room acoustic environment data pointer */
3119 : )
3120 : {
3121 : uint16_t n, m;
3122 29 : uint16_t found = 0;
3123 : Decoder_Struct *st_ivas;
3124 :
3125 29 : if ( hIvasDec == NULL || pAcEnv == NULL )
3126 : {
3127 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
3128 : }
3129 :
3130 29 : st_ivas = hIvasDec->st_ivas;
3131 :
3132 : /* In case of default AE ID, select the first one available */
3133 29 : if ( aeID == IVAS_DEFAULT_AEID && st_ivas->acousticEnvironmentsCount > 0 )
3134 : {
3135 25 : aeID = st_ivas->pAcousticEnvironments[0].aeID;
3136 : }
3137 :
3138 72 : for ( n = 0; n < st_ivas->acousticEnvironmentsCount; n++ )
3139 : {
3140 43 : IVAS_ROOM_ACOUSTICS_CONFIG_DATA ae = st_ivas->pAcousticEnvironments[n];
3141 :
3142 43 : if ( aeID == ae.aeID )
3143 : {
3144 29 : found = 1;
3145 29 : pAcEnv->aeID = aeID;
3146 29 : pAcEnv->nBands = ae.nBands;
3147 29 : pAcEnv->inputPreDelay = ae.inputPreDelay;
3148 879 : for ( m = 0; m < pAcEnv->nBands; m++ )
3149 : {
3150 850 : pAcEnv->pFc_input[m] = ae.pFc_input[m];
3151 850 : pAcEnv->pAcoustic_rt60[m] = ae.pAcoustic_rt60[m];
3152 850 : pAcEnv->pAcoustic_dsr[m] = ae.pAcoustic_dsr[m];
3153 : }
3154 :
3155 : /* If ER are allocated then propagate parameters */
3156 29 : pAcEnv->use_er = ae.use_er;
3157 29 : if ( ae.use_er != 0 )
3158 : {
3159 7 : pAcEnv->lowComplexity = ae.lowComplexity;
3160 :
3161 7 : pAcEnv->dimensions.x = ae.dimensions.x;
3162 7 : pAcEnv->dimensions.y = ae.dimensions.y;
3163 7 : pAcEnv->dimensions.z = ae.dimensions.z;
3164 :
3165 7 : pAcEnv->ListenerOrigin.x = ae.ListenerOrigin.x;
3166 7 : pAcEnv->ListenerOrigin.y = ae.ListenerOrigin.y;
3167 7 : pAcEnv->ListenerOrigin.z = ae.ListenerOrigin.z;
3168 :
3169 49 : for ( m = 0; m < IVAS_ROOM_ABS_COEFF; m++ )
3170 : {
3171 42 : pAcEnv->AbsCoeff[m] = ae.AbsCoeff[m];
3172 : }
3173 : }
3174 : }
3175 : }
3176 :
3177 29 : return found ? IVAS_ERR_OK : IVAS_ERR_ACOUSTIC_ENVIRONMENT_MISSING;
3178 : }
3179 :
3180 :
3181 : /*---------------------------------------------------------------------*
3182 : * copyRendererConfigStruct( )
3183 : *
3184 : *
3185 : *---------------------------------------------------------------------*/
3186 :
3187 1612 : static ivas_error copyRendererConfigStruct(
3188 : RENDER_CONFIG_HANDLE hRCin,
3189 : IVAS_RENDER_CONFIG_HANDLE hRCout )
3190 : {
3191 1612 : if ( hRCin == NULL || hRCout == NULL )
3192 : {
3193 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
3194 : }
3195 :
3196 1612 : hRCout->roomAcoustics.aeID = hRCin->roomAcoustics.aeID;
3197 1612 : hRCout->roomAcoustics.nBands = hRCin->roomAcoustics.nBands;
3198 1612 : hRCout->roomAcoustics.acousticPreDelay = hRCin->roomAcoustics.acousticPreDelay;
3199 1612 : hRCout->roomAcoustics.inputPreDelay = hRCin->roomAcoustics.inputPreDelay;
3200 :
3201 1612 : mvr2r( hRCin->roomAcoustics.pFc_input, hRCout->roomAcoustics.pFc_input, CLDFB_NO_CHANNELS_MAX );
3202 1612 : mvr2r( hRCin->roomAcoustics.pAcoustic_rt60, hRCout->roomAcoustics.pAcoustic_rt60, CLDFB_NO_CHANNELS_MAX );
3203 1612 : mvr2r( hRCin->roomAcoustics.pAcoustic_dsr, hRCout->roomAcoustics.pAcoustic_dsr, CLDFB_NO_CHANNELS_MAX );
3204 1612 : mvr2r( hRCin->directivity, hRCout->directivity, 3 * MAX_NUM_OBJECTS );
3205 1612 : mvr2r( hRCin->distAtt, hRCout->distAtt, 3 );
3206 :
3207 1612 : hRCout->split_rend_config = hRCin->split_rend_config;
3208 :
3209 1612 : hRCout->roomAcoustics.use_er = hRCin->roomAcoustics.use_er;
3210 1612 : hRCout->roomAcoustics.lowComplexity = hRCin->roomAcoustics.lowComplexity;
3211 :
3212 1612 : return IVAS_ERR_OK;
3213 : }
3214 :
3215 :
3216 : /*---------------------------------------------------------------------*
3217 : * IVAS_DEC_GetRenderConfig( )
3218 : *
3219 : * Return renderer configuration parameters handle
3220 : *---------------------------------------------------------------------*/
3221 :
3222 1612 : ivas_error IVAS_DEC_GetRenderConfig(
3223 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
3224 : const IVAS_RENDER_CONFIG_HANDLE hRCout /* o : Render configuration handle */
3225 : )
3226 : {
3227 1612 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hRenderConfig == NULL || hRCout == NULL )
3228 : {
3229 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
3230 : }
3231 :
3232 1612 : return copyRendererConfigStruct( hIvasDec->st_ivas->hRenderConfig, hRCout );
3233 : }
3234 :
3235 :
3236 : /*---------------------------------------------------------------------*
3237 : * IVAS_DEC_GetDefaultRenderConfig( )
3238 : *
3239 : * Return default renderer configuration parameters
3240 : *---------------------------------------------------------------------*/
3241 :
3242 : /*! r: error code*/
3243 0 : ivas_error IVAS_DEC_GetDefaultRenderConfig(
3244 : IVAS_RENDER_CONFIG_HANDLE hRCout /* o : Render config handle */
3245 : )
3246 : {
3247 : RENDER_CONFIG_DATA RCin;
3248 0 : RENDER_CONFIG_HANDLE hRCin = &RCin;
3249 : ivas_error error;
3250 :
3251 0 : if ( ( error = ivas_render_config_init_from_rom( &hRCin ) ) != IVAS_ERR_OK )
3252 : {
3253 0 : return error;
3254 : }
3255 :
3256 0 : return copyRendererConfigStruct( hRCin, hRCout );
3257 : }
3258 :
3259 :
3260 : /*---------------------------------------------------------------------*
3261 : * IVAS_DEC_FeedRenderConfig( )
3262 : *
3263 : * Set renderer configuration (acoustic environment) parameters
3264 : *---------------------------------------------------------------------*/
3265 :
3266 1075 : ivas_error IVAS_DEC_FeedRenderConfig(
3267 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
3268 : const IVAS_RENDER_CONFIG_DATA renderConfig /* i : Render configuration struct */
3269 : )
3270 : {
3271 : RENDER_CONFIG_HANDLE hRenderConfig;
3272 : Decoder_Struct *st_ivas;
3273 : ivas_error error;
3274 :
3275 1075 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hRenderConfig == NULL )
3276 : {
3277 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
3278 : }
3279 :
3280 1075 : hRenderConfig = hIvasDec->st_ivas->hRenderConfig;
3281 1075 : st_ivas = hIvasDec->st_ivas;
3282 :
3283 1075 : hRenderConfig->roomAcoustics.aeID = renderConfig.roomAcoustics.aeID;
3284 1075 : hRenderConfig->roomAcoustics.nBands = renderConfig.roomAcoustics.nBands;
3285 1075 : hRenderConfig->roomAcoustics.acousticPreDelay = renderConfig.roomAcoustics.acousticPreDelay;
3286 1075 : hRenderConfig->roomAcoustics.inputPreDelay = renderConfig.roomAcoustics.inputPreDelay;
3287 :
3288 1075 : hRenderConfig->roomAcoustics.use_er = 0;
3289 1075 : if ( renderConfig.roomAcoustics.use_er == 1 )
3290 : {
3291 14 : hRenderConfig->roomAcoustics.use_er = renderConfig.roomAcoustics.use_er;
3292 14 : hRenderConfig->roomAcoustics.lowComplexity = renderConfig.roomAcoustics.lowComplexity;
3293 14 : hRenderConfig->roomAcoustics.dimensions = renderConfig.roomAcoustics.dimensions;
3294 14 : hRenderConfig->roomAcoustics.ListenerOrigin = renderConfig.roomAcoustics.ListenerOrigin;
3295 :
3296 14 : mvr2r( renderConfig.roomAcoustics.AbsCoeff, hRenderConfig->roomAcoustics.AbsCoeff, IVAS_ROOM_ABS_COEFF );
3297 : }
3298 :
3299 1075 : mvr2r( renderConfig.roomAcoustics.pFc_input, hRenderConfig->roomAcoustics.pFc_input, CLDFB_NO_CHANNELS_MAX );
3300 1075 : mvr2r( renderConfig.roomAcoustics.pAcoustic_rt60, hRenderConfig->roomAcoustics.pAcoustic_rt60, CLDFB_NO_CHANNELS_MAX );
3301 1075 : mvr2r( renderConfig.roomAcoustics.pAcoustic_dsr, hRenderConfig->roomAcoustics.pAcoustic_dsr, CLDFB_NO_CHANNELS_MAX );
3302 :
3303 : /* Re-initialize reverb instance if already available */
3304 :
3305 : /* TD renderer Jot reverberator */
3306 1075 : if ( st_ivas->hReverb != NULL )
3307 : {
3308 0 : if ( ( error = ivas_reverb_open( &st_ivas->hReverb, st_ivas->hHrtfStatistics, hRenderConfig, st_ivas->hDecoderConfig->output_Fs ) ) != IVAS_ERR_OK )
3309 : {
3310 0 : return error;
3311 : }
3312 : }
3313 :
3314 : /* CREND Jot reverberator */
3315 1075 : if ( st_ivas->hCrendWrapper != NULL && st_ivas->hCrendWrapper->hCrend[0] != NULL && st_ivas->hCrendWrapper->hCrend[0]->hReverb != NULL )
3316 : {
3317 4 : if ( ( error = ivas_reverb_open( &st_ivas->hCrendWrapper->hCrend[0]->hReverb, st_ivas->hHrtfStatistics, hRenderConfig, st_ivas->hDecoderConfig->output_Fs ) ) != IVAS_ERR_OK )
3318 : {
3319 0 : return error;
3320 : }
3321 : }
3322 :
3323 : /* Parametric renderer reverberator */
3324 1075 : if ( st_ivas->hDiracDecBin[0] != NULL && st_ivas->hDiracDecBin[0]->hReverb != NULL )
3325 : {
3326 4 : ivas_binaural_reverb_close( &( st_ivas->hDiracDecBin[0]->hReverb ) );
3327 :
3328 4 : if ( ( error = ivas_binaural_reverb_init( &( st_ivas->hDiracDecBin[0]->hReverb ), st_ivas->hHrtfStatistics, st_ivas->hSpatParamRendCom->num_freq_bands, CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES,
3329 4 : &( hRenderConfig->roomAcoustics ), st_ivas->hDecoderConfig->output_Fs, NULL, NULL, NULL ) ) != IVAS_ERR_OK )
3330 : {
3331 0 : return error;
3332 : }
3333 : }
3334 :
3335 : /* FastConv renderer reverberator */
3336 1075 : if ( st_ivas->hBinRenderer != NULL && st_ivas->hBinRenderer->hReverb != NULL )
3337 : {
3338 3 : ivas_binaural_reverb_close( &( st_ivas->hBinRenderer->hReverb ) );
3339 :
3340 3 : if ( ( error = ivas_binaural_reverb_init( &( st_ivas->hBinRenderer->hReverb ), st_ivas->hHrtfStatistics, st_ivas->hBinRenderer->conv_band, st_ivas->hBinRenderer->timeSlots,
3341 3 : &( hRenderConfig->roomAcoustics ), st_ivas->hDecoderConfig->output_Fs, NULL, NULL, NULL ) ) != IVAS_ERR_OK )
3342 : {
3343 0 : return error;
3344 : }
3345 : }
3346 :
3347 1075 : mvr2r( renderConfig.directivity, hRenderConfig->directivity, 3 * MAX_NUM_OBJECTS );
3348 1075 : mvr2r( renderConfig.distAtt, hRenderConfig->distAtt, 3 );
3349 :
3350 1075 : hRenderConfig->split_rend_config = renderConfig.split_rend_config;
3351 :
3352 : /* Overwrite any pose correction settings if 0 DOF (no pose correction) was selected */
3353 1075 : if ( hRenderConfig->split_rend_config.dof == 0 )
3354 : {
3355 338 : hRenderConfig->split_rend_config.poseCorrectionMode = ISAR_SPLIT_REND_POSE_CORRECTION_MODE_NONE;
3356 : }
3357 :
3358 1075 : if ( is_split_rendering_enabled( st_ivas->hDecoderConfig, hRenderConfig ) )
3359 : {
3360 1032 : if ( ( error = isar_split_rend_validate_config( &hRenderConfig->split_rend_config, ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ? 1 : 0 ) ) != IVAS_ERR_OK )
3361 : {
3362 0 : return error;
3363 : }
3364 : }
3365 :
3366 1075 : return IVAS_ERR_OK;
3367 : }
3368 :
3369 :
3370 : /*---------------------------------------------------------------------*
3371 : * feedAcousticEnvPI( )
3372 : *
3373 : * Set acoustic environment from the PI data
3374 : *---------------------------------------------------------------------*/
3375 :
3376 0 : static ivas_error feedAcousticEnvPI(
3377 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
3378 : const IVAS_PIDATA_ACOUSTIC_ENV hAcoustEnvPI /* i : Render configuration struct */
3379 : )
3380 : {
3381 : RENDER_CONFIG_HANDLE hRenderConfig;
3382 : Decoder_Struct *st_ivas;
3383 : ivas_error error;
3384 :
3385 0 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hRenderConfig == NULL )
3386 : {
3387 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
3388 : }
3389 :
3390 0 : hRenderConfig = hIvasDec->st_ivas->hRenderConfig;
3391 0 : st_ivas = hIvasDec->st_ivas;
3392 :
3393 : /* Ignore if AE ID already in use */
3394 0 : if ( hRenderConfig->roomAcoustics.aeID == hAcoustEnvPI.aeid )
3395 : {
3396 0 : return IVAS_ERR_OK;
3397 : }
3398 :
3399 : /* Attempt to load the one already available */
3400 0 : if ( ( error = IVAS_DEC_GetAcousticEnvironment( hIvasDec, hAcoustEnvPI.aeid, &hRenderConfig->roomAcoustics ) ) == IVAS_ERR_ACOUSTIC_ENVIRONMENT_MISSING )
3401 : {
3402 : /* Add the new compact room environment */
3403 : IVAS_ROOM_ACOUSTICS_CONFIG_DATA acEnv;
3404 :
3405 0 : acEnv.aeID = hAcoustEnvPI.aeid;
3406 0 : acEnv.nBands = IVAS_PI_AE_NUM_BANDS;
3407 0 : acEnv.pFc_input[IVAS_PI_AE_LOW] = IVAS_PI_AE_LOW_FREQ;
3408 0 : acEnv.pFc_input[IVAS_PI_AE_MID] = IVAS_PI_AE_MID_FREQ;
3409 0 : acEnv.pFc_input[IVAS_PI_AE_HIGH] = IVAS_PI_AE_HIGH_FREQ;
3410 0 : acEnv.pAcoustic_rt60[IVAS_PI_AE_LOW] = hAcoustEnvPI.rt60[IVAS_PI_AE_LOW];
3411 0 : acEnv.pAcoustic_rt60[IVAS_PI_AE_MID] = hAcoustEnvPI.rt60[IVAS_PI_AE_MID];
3412 0 : acEnv.pAcoustic_rt60[IVAS_PI_AE_HIGH] = hAcoustEnvPI.rt60[IVAS_PI_AE_HIGH];
3413 0 : acEnv.inputPreDelay = (float) ( 0.1 * hRenderConfig->roomAcoustics.pAcoustic_rt60[IVAS_PI_AE_MID] );
3414 0 : acEnv.pAcoustic_dsr[IVAS_PI_AE_LOW] = powf( 10.0f, hAcoustEnvPI.dsr[IVAS_PI_AE_LOW] / 10.0f );
3415 0 : acEnv.pAcoustic_dsr[IVAS_PI_AE_MID] = powf( 10.0f, hAcoustEnvPI.dsr[IVAS_PI_AE_MID] / 10.0f );
3416 0 : acEnv.pAcoustic_dsr[IVAS_PI_AE_HIGH] = powf( 10.0f, hAcoustEnvPI.dsr[IVAS_PI_AE_HIGH] / 10.0f );
3417 :
3418 0 : acEnv.use_er = hAcoustEnvPI.availEarlyReflections;
3419 :
3420 0 : if ( hAcoustEnvPI.availEarlyReflections )
3421 : {
3422 0 : hRenderConfig->roomAcoustics.dimensions.x = hAcoustEnvPI.roomDimensions.x;
3423 0 : hRenderConfig->roomAcoustics.dimensions.y = hAcoustEnvPI.roomDimensions.y;
3424 0 : hRenderConfig->roomAcoustics.dimensions.z = hAcoustEnvPI.roomDimensions.z;
3425 0 : mvr2r( hAcoustEnvPI.absorbCoeffs, hRenderConfig->roomAcoustics.AbsCoeff, IVAS_ROOM_ABS_COEFF );
3426 : }
3427 :
3428 0 : if ( ( error = IVAS_DEC_AddAcousticEnvironment( hIvasDec, acEnv ) ) != IVAS_ERR_OK )
3429 : {
3430 0 : return error;
3431 : }
3432 :
3433 0 : if ( ( error = IVAS_DEC_GetAcousticEnvironment( hIvasDec, hAcoustEnvPI.aeid, &hRenderConfig->roomAcoustics ) ) != IVAS_ERR_OK )
3434 : {
3435 0 : return error;
3436 : }
3437 : }
3438 :
3439 : /* Re-initialize reverb instance if already available */
3440 :
3441 : /* TD renderer Jot reverberator */
3442 0 : if ( st_ivas->hReverb != NULL )
3443 : {
3444 0 : if ( ( error = ivas_reverb_open( &st_ivas->hReverb, st_ivas->hHrtfStatistics, hRenderConfig, st_ivas->hDecoderConfig->output_Fs ) ) != IVAS_ERR_OK )
3445 : {
3446 0 : return error;
3447 : }
3448 : }
3449 :
3450 : /* CREND Jot reverberator */
3451 0 : if ( st_ivas->hCrendWrapper != NULL && st_ivas->hCrendWrapper->hCrend[0] != NULL && st_ivas->hCrendWrapper->hCrend[0]->hReverb != NULL )
3452 : {
3453 0 : if ( ( error = ivas_reverb_open( &st_ivas->hCrendWrapper->hCrend[0]->hReverb, st_ivas->hHrtfStatistics, hRenderConfig, st_ivas->hDecoderConfig->output_Fs ) ) != IVAS_ERR_OK )
3454 : {
3455 0 : return error;
3456 : }
3457 : }
3458 :
3459 : /* Parametric renderer reverberator */
3460 0 : if ( st_ivas->hDiracDecBin[0] != NULL && st_ivas->hDiracDecBin[0]->hReverb != NULL )
3461 : {
3462 0 : ivas_binaural_reverb_close( &( st_ivas->hDiracDecBin[0]->hReverb ) );
3463 :
3464 0 : if ( ( error = ivas_binaural_reverb_init( &( st_ivas->hDiracDecBin[0]->hReverb ), st_ivas->hHrtfStatistics, st_ivas->hSpatParamRendCom->num_freq_bands, CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES,
3465 0 : &( hRenderConfig->roomAcoustics ), st_ivas->hDecoderConfig->output_Fs, NULL, NULL, NULL ) ) != IVAS_ERR_OK )
3466 : {
3467 0 : return error;
3468 : }
3469 : }
3470 :
3471 : /* FastConv renderer reverberator */
3472 0 : if ( st_ivas->hBinRenderer != NULL && st_ivas->hBinRenderer->hReverb != NULL )
3473 : {
3474 0 : ivas_binaural_reverb_close( &( st_ivas->hBinRenderer->hReverb ) );
3475 :
3476 0 : if ( ( error = ivas_binaural_reverb_init( &( st_ivas->hBinRenderer->hReverb ), st_ivas->hHrtfStatistics, st_ivas->hBinRenderer->conv_band, st_ivas->hBinRenderer->timeSlots,
3477 0 : &( hRenderConfig->roomAcoustics ), st_ivas->hDecoderConfig->output_Fs, NULL, NULL, NULL ) ) != IVAS_ERR_OK )
3478 : {
3479 0 : return error;
3480 : }
3481 : }
3482 :
3483 0 : return IVAS_ERR_OK;
3484 : }
3485 :
3486 :
3487 : /*---------------------------------------------------------------------*
3488 : * IVAS_DEC_GetDelay( )
3489 : *
3490 : * Return IVAS decoder delay in nanoseconds
3491 : *---------------------------------------------------------------------*/
3492 :
3493 22174 : ivas_error IVAS_DEC_GetDelay(
3494 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
3495 : int16_t *nSamples, /* o : decoder delay in samples */
3496 : int32_t *timeScale /* o : time scale of the delay, equal to decoder output sampling rate */
3497 : )
3498 : {
3499 : Decoder_Struct *st_ivas;
3500 : DECODER_CONFIG_HANDLE hDecoderConfig;
3501 :
3502 22174 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || nSamples == NULL || timeScale == NULL )
3503 : {
3504 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
3505 : }
3506 :
3507 22174 : if ( !hIvasDec->hasDecodedFirstGoodFrame )
3508 : {
3509 : /* Delay depends on IVAS format, which is unknown until first frame has been decoded */
3510 0 : return IVAS_ERR_WAITING_FOR_BITSTREAM;
3511 : }
3512 :
3513 22174 : st_ivas = hIvasDec->st_ivas;
3514 22174 : hDecoderConfig = st_ivas->hDecoderConfig;
3515 :
3516 22174 : nSamples[1] = NS2SA( hDecoderConfig->output_Fs, get_delay( DEC, hDecoderConfig->output_Fs, st_ivas->ivas_format, st_ivas->cldfbSynDec[0], hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED ) );
3517 :
3518 22174 : nSamples[2] = (int16_t) roundf( (float) st_ivas->binaural_latency_ns * hDecoderConfig->output_Fs / 1000000000.f );
3519 22174 : nSamples[0] = nSamples[1] + nSamples[2];
3520 :
3521 22174 : if ( st_ivas->ivas_format == MASA_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT )
3522 : {
3523 : /* note: in MASA, all delay is compensated at the decoder by default, so subtract the encoder delay for print-out */
3524 20416 : nSamples[1] -= NS2SA( hDecoderConfig->output_Fs, IVAS_ENC_DELAY_NS );
3525 : }
3526 :
3527 22174 : *timeScale = hDecoderConfig->output_Fs;
3528 :
3529 22174 : return IVAS_ERR_OK;
3530 : }
3531 :
3532 :
3533 : /*---------------------------------------------------------------------*
3534 : * IVAS_DEC_HasDecodedFirstGoodFrame( )
3535 : *
3536 : * Return flag indicating if the decoder has decoded a good frame
3537 : *---------------------------------------------------------------------*/
3538 :
3539 6903 : ivas_error IVAS_DEC_HasDecodedFirstGoodFrame(
3540 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
3541 : bool *hasDecodedFirstGoodFrame /* o : flag indicating if the decoder has decoded a good frame since it was configured */
3542 : )
3543 : {
3544 6903 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hasDecodedFirstGoodFrame == NULL )
3545 : {
3546 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
3547 : }
3548 :
3549 6903 : *hasDecodedFirstGoodFrame = hIvasDec->hasDecodedFirstGoodFrame;
3550 :
3551 6903 : return IVAS_ERR_OK;
3552 : }
3553 :
3554 :
3555 : /*---------------------------------------------------------------------*
3556 : * isSidFrame( )
3557 : *
3558 : * Check if a frame contains a SID
3559 : *---------------------------------------------------------------------*/
3560 :
3561 139293 : static bool isSidFrame(
3562 : const uint16_t size )
3563 : {
3564 139293 : if ( size == SID_1k75 / FRAMES_PER_SEC )
3565 : {
3566 0 : return true; /* AMR-WB SID */
3567 : }
3568 139293 : else if ( size == SID_2k40 / FRAMES_PER_SEC )
3569 : {
3570 0 : return true; /* EVS SID */
3571 : }
3572 139293 : else if ( size == IVAS_SID_5k2 / FRAMES_PER_SEC )
3573 : {
3574 268 : return true; /* IVAS SID */
3575 : }
3576 :
3577 139025 : return false;
3578 : }
3579 :
3580 :
3581 : /*---------------------------------------------------------------------*
3582 : * bsCompactToSerial( )
3583 : *
3584 : * Bitstream conversion to Byte format
3585 : *---------------------------------------------------------------------*/
3586 :
3587 138740 : static void bsCompactToSerial(
3588 : const uint8_t *compact,
3589 : uint16_t *serial,
3590 : const uint16_t num_bits )
3591 : {
3592 : /* Bitstream conversion is not counted towards complexity and memory usage */
3593 : #define WMC_TOOL_SKIP
3594 : uint32_t i;
3595 138740 : uint8_t byte = 0;
3596 138740 : const uint8_t mask = 0x80;
3597 :
3598 534976012 : for ( i = 0; i < num_bits; ++i )
3599 : {
3600 534837272 : if ( i % 8 == 0 )
3601 : {
3602 66854659 : byte = compact[i / 8];
3603 : }
3604 :
3605 534837272 : serial[i] = ( byte & mask ) >> 7;
3606 :
3607 534837272 : byte <<= 1;
3608 : }
3609 :
3610 : /* Add 4 padding bytes required by core coder */
3611 4578420 : for ( i = 0; i < 4 * 8; ++i )
3612 : {
3613 4439680 : serial[num_bits + i] = 0;
3614 : }
3615 : #undef WMC_TOOL_SKIP
3616 138740 : }
3617 :
3618 :
3619 : /*---------------------------------------------------------------------*
3620 : * IVAS_DEC_VoIP_FeedFrame( )
3621 : *
3622 : * Feed RTP packet into internal jitter buffer
3623 : *---------------------------------------------------------------------*/
3624 :
3625 139293 : ivas_error IVAS_DEC_VoIP_FeedFrame(
3626 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
3627 : uint8_t *au, /* i : buffer containing input access unit */
3628 : const uint16_t auSize, /* i : size of the access unit */
3629 : const uint16_t rtpSequenceNumber, /* i : RTP sequence number (16 bits) */
3630 : const uint32_t rtpTimeStamp, /* i : RTP timestamp (32 bits) */
3631 : const uint32_t rcvTime_ms, /* i : receive time of the RTP packet in milliseconds */
3632 : const bool qBit /* i : Q bit for AMR-WB IO */
3633 : )
3634 : {
3635 : JB4_DATAUNIT_HANDLE dataUnit;
3636 : int16_t partialCopyFrameType, partialCopyOffset;
3637 : int16_t result;
3638 :
3639 139293 : if ( hIvasDec == NULL || hIvasDec->hVoIP == NULL || au == NULL )
3640 : {
3641 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
3642 : }
3643 :
3644 139293 : if ( auSize == 0 )
3645 : {
3646 0 : return IVAS_ERR_OK; /* ignore empty/NO_DATA frame - shouldn't be transmitted in RTP */
3647 : }
3648 139293 : if ( ( auSize + 7 ) / 8 > MAX_AU_SIZE )
3649 : {
3650 0 : return IVAS_ERR_INVALID_BITSTREAM;
3651 : }
3652 :
3653 139293 : partialCopyFrameType = 0;
3654 139293 : partialCopyOffset = 0;
3655 :
3656 139293 : if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
3657 : {
3658 : /* check if frame contains a partial copy and get its offset */
3659 2050 : evs_dec_previewFrame( au, auSize, &partialCopyFrameType, &partialCopyOffset );
3660 : }
3661 :
3662 : /* create data unit for primary copy in the frame */
3663 139293 : dataUnit = JB4_AllocDataUnit( hIvasDec->hVoIP->hJBM );
3664 139293 : mvc2c( au, dataUnit->data, (int16_t) ( ( auSize + 7 ) / 8 ) );
3665 139293 : dataUnit->dataSize = auSize;
3666 139293 : dataUnit->duration = 20;
3667 139293 : dataUnit->sequenceNumber = rtpSequenceNumber;
3668 139293 : dataUnit->silenceIndicator = isSidFrame( dataUnit->dataSize );
3669 139293 : dataUnit->timeScale = 1000;
3670 139293 : dataUnit->rcvTime = rcvTime_ms;
3671 139293 : dataUnit->timeStamp = rtpTimeStamp;
3672 139293 : dataUnit->partial_frame = 0;
3673 139293 : dataUnit->partialCopyOffset = partialCopyOffset;
3674 139293 : dataUnit->qBit = qBit;
3675 :
3676 : /* add the frame to the JBM */
3677 139293 : result = JB4_PushDataUnit( hIvasDec->hVoIP->hJBM, dataUnit, rcvTime_ms );
3678 139293 : if ( result != 0 )
3679 : {
3680 0 : return IVAS_ERR_UNKNOWN;
3681 : }
3682 :
3683 139293 : if ( partialCopyFrameType != RF_NO_DATA && partialCopyOffset != 0 )
3684 : {
3685 : /* create data unit for partial copy in the frame */
3686 0 : dataUnit = JB4_AllocDataUnit( hIvasDec->hVoIP->hJBM );
3687 0 : mvc2c( au, dataUnit->data, (int16_t) ( ( auSize + 7 ) / 8 ) );
3688 0 : dataUnit->dataSize = auSize;
3689 0 : dataUnit->duration = 20;
3690 0 : dataUnit->sequenceNumber = rtpSequenceNumber;
3691 0 : dataUnit->silenceIndicator = 0; /* there are no partial copies for SID frames */
3692 0 : dataUnit->timeScale = 1000;
3693 0 : dataUnit->rcvTime = rcvTime_ms;
3694 0 : dataUnit->timeStamp = rtpTimeStamp - partialCopyOffset * dataUnit->duration;
3695 0 : dataUnit->partial_frame = 1;
3696 0 : dataUnit->partialCopyOffset = partialCopyOffset;
3697 0 : dataUnit->qBit = qBit;
3698 :
3699 : /* add the frame to the JBM */
3700 0 : result = JB4_PushDataUnit( hIvasDec->hVoIP->hJBM, dataUnit, rcvTime_ms );
3701 0 : if ( result != 0 )
3702 : {
3703 0 : return IVAS_ERR_UNKNOWN;
3704 : }
3705 : }
3706 :
3707 139293 : return IVAS_ERR_OK;
3708 : }
3709 :
3710 :
3711 : /*---------------------------------------------------------------------*
3712 : * IVAS_DEC_VoIP_SetScale( )
3713 : *
3714 : * Set the TSM scale
3715 : *---------------------------------------------------------------------*/
3716 :
3717 153603 : ivas_error IVAS_DEC_VoIP_SetScale(
3718 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
3719 : const int16_t maxScaling, /* i : max allowed absolute difference in samples from the default 20ms frame size */
3720 : const int16_t scale /* i : TSM scale to set in percent of the default frame size */
3721 : )
3722 : {
3723 153603 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
3724 : {
3725 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
3726 : }
3727 :
3728 153603 : if ( hIvasDec->st_ivas->hDecoderConfig->Opt_tsm == 0 )
3729 : {
3730 0 : return IVAS_ERR_TSM_NOT_ENABLED;
3731 : }
3732 : else
3733 : {
3734 153603 : hIvasDec->tsm_scale = scale;
3735 153603 : hIvasDec->tsm_max_scaling = maxScaling;
3736 : }
3737 :
3738 153603 : return IVAS_ERR_OK;
3739 : }
3740 :
3741 : #ifdef VARIABLE_SPEED_DECODING
3742 :
3743 : /*---------------------------------------------------------------------*
3744 : * IVAS_DEC_EnableTsm( )
3745 : *
3746 : * Enable Time-Scale Modification (TSM)
3747 : *---------------------------------------------------------------------*/
3748 :
3749 : ivas_error IVAS_DEC_EnableTsm(
3750 : IVAS_DEC_HANDLE hIvasDec /* i/o: IVAS decoder handle */
3751 : )
3752 : {
3753 : AUDIO_CONFIG output_config;
3754 : ivas_error error;
3755 :
3756 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
3757 : {
3758 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
3759 : }
3760 :
3761 : hIvasDec->st_ivas->hDecoderConfig->Opt_tsm = 1;
3762 :
3763 : /* init flush buffer if necessary (only needed for binaural) */
3764 : output_config = hIvasDec->st_ivas->hDecoderConfig->output_config;
3765 : if ( hIvasDec->flushbuffer == NULL && ( output_config == IVAS_AUDIO_CONFIG_BINAURAL || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) )
3766 : {
3767 : if ( ( error = create_flush_buffer( hIvasDec ) ) != IVAS_ERR_OK )
3768 : {
3769 : return error;
3770 : }
3771 : }
3772 :
3773 : return IVAS_ERR_OK;
3774 : }
3775 :
3776 :
3777 : /*---------------------------------------------------------------------*
3778 : * IVAS_DEC_TSM_SetQuality( )
3779 : *
3780 : * set the quality theshold for the time scale modiciation that is used
3781 : * to determine if the TSM yielded a signal that satisfies the minimum
3782 : * quality requirements.
3783 : * quality is lower limit for minimum quality
3784 : * Range is [-2;2] - where positive values allow
3785 : * only pasting with same phase information
3786 : * Negative values would yield cross phased pasting
3787 : * When not setting the minimum quality with this function the default
3788 : * value used is 1.0f
3789 : *
3790 : *---------------------------------------------------------------------*/
3791 :
3792 : ivas_error IVAS_DEC_TSM_SetQuality(
3793 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
3794 : const float quality /* i : target TSM quality */
3795 : )
3796 : {
3797 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
3798 : {
3799 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
3800 : }
3801 :
3802 : if ( !hIvasDec->st_ivas->hDecoderConfig->Opt_tsm )
3803 : {
3804 : return IVAS_ERR_TSM_NOT_ENABLED;
3805 : }
3806 : else
3807 : {
3808 : hIvasDec->tsm_quality = quality;
3809 : }
3810 :
3811 : return IVAS_ERR_OK;
3812 : }
3813 :
3814 : #endif
3815 :
3816 :
3817 : /*---------------------------------------------------------------------*
3818 : * ivas_dec_voip_get_samples_common( )
3819 : *
3820 : * Main function to output one frame in VoIP. Holds common code for
3821 : * regular output configs and split rendering configs.
3822 : *---------------------------------------------------------------------*/
3823 :
3824 447758 : static ivas_error ivas_dec_voip_get_samples_common(
3825 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
3826 : uint16_t nSamplesPerChannel, /* i : number of samples per channel requested to be written to output buffer */
3827 : const IVAS_DEC_PCM_TYPE pcmType, /* i : type for the decoded PCM resolution */
3828 : void *pcmBuf, /* o : output synthesis signal */
3829 : ISAR_SPLIT_REND_BITS_DATA *splitRendBits, /* o : output split rendering bits */
3830 : float **p_head_pose_buf, /* i : PCM buffer with head-pose data */
3831 : #ifdef SUPPORT_JBM_TRACEFILE
3832 : JbmTraceFileWriterFn jbmWriterFn,
3833 : void *jbmWriter,
3834 : #endif
3835 : bool *bitstreamReadDone, /* o : flag indicating that bitstream was read */
3836 : uint16_t *nSamplesRendered, /* o : number of samples rendered */
3837 : bool *parametersAvailableForEditing, /* o : indicates whether objects editing is available */
3838 : const uint32_t systemTimestamp_ms /* i : current system timestamp */
3839 : )
3840 : {
3841 : Decoder_Struct *st_ivas;
3842 : DECODER_CONFIG_HANDLE hDecoderConfig;
3843 : IVAS_DEC_VOIP *hVoIP;
3844 : uint32_t extBufferedTime_ms, scale, maxScaling;
3845 : JB4_DATAUNIT_HANDLE dataUnit;
3846 : uint16_t extBufferedSamples;
3847 : int16_t result;
3848 : ivas_error error;
3849 : uint8_t nOutChannels;
3850 :
3851 447758 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->hVoIP == NULL )
3852 : {
3853 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
3854 : }
3855 :
3856 447758 : st_ivas = hIvasDec->st_ivas;
3857 447758 : hDecoderConfig = st_ivas->hDecoderConfig;
3858 447758 : hVoIP = hIvasDec->hVoIP;
3859 447758 : nOutChannels = (uint8_t) st_ivas->hDecoderConfig->nchan_out;
3860 447758 : *parametersAvailableForEditing = false;
3861 :
3862 447758 : if ( nSamplesPerChannel == 0 )
3863 : {
3864 0 : return IVAS_ERR_WRONG_PARAMS;
3865 : }
3866 :
3867 447758 : if ( ( hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ||
3868 447758 : hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED ) &&
3869 : splitRendBits == NULL )
3870 : {
3871 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
3872 : }
3873 :
3874 : /* make sure that the FIFO after decoder/scaler contains at least one sound card frame (i.e. 20ms) */
3875 675146 : while ( *nSamplesRendered < nSamplesPerChannel )
3876 : {
3877 514787 : if ( hIvasDec->nSamplesAvailableNext == 0 )
3878 : {
3879 : int16_t nSamplesBuffered;
3880 153603 : nSamplesBuffered = 0;
3881 153603 : if ( hIvasDec->hasBeenFedFirstGoodFrame )
3882 : {
3883 : /* check if the TC buffer already exists, otherweise nothing is buffered anyway */
3884 148098 : if ( st_ivas->hTcBuffer != NULL )
3885 : {
3886 148098 : nSamplesBuffered = st_ivas->hTcBuffer->n_samples_buffered - st_ivas->hTcBuffer->n_samples_rendered;
3887 148098 : nSamplesBuffered += hVoIP->nSamplesRendered20ms;
3888 : }
3889 : }
3890 :
3891 153603 : extBufferedSamples = nSamplesBuffered;
3892 153603 : extBufferedTime_ms = extBufferedSamples * 1000 / hDecoderConfig->output_Fs;
3893 153603 : dataUnit = NULL;
3894 :
3895 : /* pop one access unit from the jitter buffer */
3896 153603 : result = JB4_PopDataUnit( hVoIP->hJBM, systemTimestamp_ms, extBufferedTime_ms, &dataUnit, &scale, &maxScaling );
3897 153603 : if ( result != 0 )
3898 : {
3899 0 : return IVAS_ERR_UNKNOWN;
3900 : }
3901 153603 : maxScaling = maxScaling * hDecoderConfig->output_Fs / 1000;
3902 :
3903 : #ifdef DEBUG_MODE_JBM
3904 : dbgwrite( &extBufferedSamples, sizeof( uint16_t ), 1, 1, "./res/JBM_extBufferedSamples.dat" );
3905 : dbgwrite( &systemTimestamp_ms, sizeof( uint32_t ), 1, 1, "./res/JBM_systemTimestamp.dat" );
3906 : dbgwrite( &scale, sizeof( uint32_t ), 1, 1, "./res/JBM_scale.dat" );
3907 : dbgwrite( &maxScaling, sizeof( uint32_t ), 1, 1, "./res/JBM_maxScale.dat" );
3908 : #endif
3909 :
3910 : /* avoid time scaling multiple times within one 20ms frame*/
3911 153603 : if ( scale != 100U )
3912 : {
3913 8349 : if ( hIvasDec->timeScalingDone )
3914 : {
3915 0 : scale = 100;
3916 : }
3917 : }
3918 :
3919 : /* limit scale to range supported by time scaler */
3920 153603 : if ( scale < APA_MIN_SCALE )
3921 : {
3922 0 : scale = APA_MIN_SCALE;
3923 : }
3924 153603 : else if ( scale > APA_MAX_SCALE )
3925 : {
3926 0 : scale = APA_MAX_SCALE;
3927 : }
3928 :
3929 153603 : if ( ( error = IVAS_DEC_VoIP_SetScale( hIvasDec, (int16_t) maxScaling, (int16_t) scale ) ) != IVAS_ERR_OK )
3930 : {
3931 0 : return error;
3932 : }
3933 :
3934 : /* copy bitstream into decoder state */
3935 153603 : if ( dataUnit )
3936 : {
3937 138740 : hIvasDec->hVoIP->hCurrentDataUnit = dataUnit;
3938 :
3939 138740 : bsCompactToSerial( dataUnit->data, hIvasDec->hVoIP->bs_conversion_buf, dataUnit->dataSize );
3940 :
3941 138740 : if ( ( error = IVAS_DEC_FeedFrame_Serial( hIvasDec, hIvasDec->hVoIP->bs_conversion_buf, dataUnit->dataSize, 0 ) ) != IVAS_ERR_OK )
3942 : {
3943 0 : return error;
3944 : }
3945 :
3946 138740 : *bitstreamReadDone = true;
3947 : }
3948 14863 : else if ( hIvasDec->hasDecodedFirstGoodFrame )
3949 : {
3950 : /* Decoder has been initialized with first good frame - do PLC */
3951 9927 : if ( ( error = IVAS_DEC_FeedFrame_Serial( hIvasDec, hIvasDec->hVoIP->bs_conversion_buf, 0, 1 ) ) != IVAS_ERR_OK )
3952 : {
3953 0 : return error;
3954 : }
3955 : }
3956 :
3957 : #ifdef SUPPORT_JBM_TRACEFILE
3958 : /* jbmWriterFn and jbmWriter may be NULL if tracefile writing was not requested on CLI */
3959 153603 : if ( jbmWriterFn != NULL && jbmWriter != NULL )
3960 : {
3961 : /* write JBM trace data entry */
3962 35890 : store_JbmData( hVoIP, dataUnit, systemTimestamp_ms, extBufferedSamples, hDecoderConfig->output_Fs );
3963 35890 : if ( ( jbmWriterFn( &hVoIP->JbmTraceData, jbmWriter ) ) != IVAS_ERR_OK )
3964 : {
3965 0 : fprintf( stderr, "\nError writing JBM Trace data to file\n" );
3966 0 : return IVAS_ERR_UNKNOWN;
3967 : }
3968 : }
3969 : #endif
3970 153603 : if ( dataUnit )
3971 : {
3972 138740 : if ( dataUnit->partial_frame != 0 )
3973 : {
3974 0 : hVoIP->lastDecodedWasActive = 1;
3975 : }
3976 : else
3977 : {
3978 138740 : hVoIP->lastDecodedWasActive = !dataUnit->silenceIndicator;
3979 : }
3980 :
3981 : /* data unit memory is no longer used */
3982 138740 : JB4_FreeDataUnit( hVoIP->hJBM, dataUnit );
3983 : }
3984 :
3985 153603 : if ( hIvasDec->hasBeenFedFirstGoodFrame && *bitstreamReadDone == true )
3986 : {
3987 : /* new bitstream was feeded, return for reconfiguration */
3988 138740 : return IVAS_ERR_OK;
3989 : }
3990 :
3991 14863 : if ( !hIvasDec->hasBeenFedFirstGoodFrame )
3992 : {
3993 4944 : hIvasDec->nSamplesAvailableNext = hIvasDec->nSamplesFrame;
3994 : }
3995 : }
3996 :
3997 : /* decode */
3998 376047 : if ( !hIvasDec->hasBeenFedFirstGoodFrame )
3999 : {
4000 : /* codec mode to use not known yet - simply output silence */
4001 : /* directly set output zero */
4002 5160 : int16_t nSamplesToZero = min( nSamplesPerChannel, hIvasDec->nSamplesAvailableNext );
4003 5160 : set_pcm_buffer_to_zero( pcm_buffer_offset( pcmBuf, pcmType, ( *nSamplesRendered ) * nOutChannels ), pcmType, nSamplesToZero * nOutChannels );
4004 5160 : *nSamplesRendered += nSamplesToZero;
4005 5160 : hIvasDec->nSamplesAvailableNext -= nSamplesToZero;
4006 5160 : update_voip_rendered20ms( hIvasDec, nSamplesToZero );
4007 : }
4008 : else
4009 : {
4010 : int16_t nSamplesToRender, nSamplesRendered_loop;
4011 : bool tmp;
4012 :
4013 : /* decode TCs, do TSM and prepare the renderer */
4014 370887 : if ( !hIvasDec->isInitialized || hIvasDec->hasBeenFedFrame )
4015 : {
4016 148659 : if ( hIvasDec->nSamplesAvailableNext == 0 || hIvasDec->nSamplesAvailableNext == hIvasDec->nSamplesFrame )
4017 : {
4018 148659 : if ( ( error = IVAS_DEC_GetSamplesDecoder( hIvasDec, splitRendBits ) ) != IVAS_ERR_OK )
4019 : {
4020 148659 : return error;
4021 : }
4022 :
4023 148659 : *bitstreamReadDone = false;
4024 148659 : *parametersAvailableForEditing = true;
4025 148659 : return IVAS_ERR_OK;
4026 : }
4027 : }
4028 :
4029 222228 : nSamplesToRender = nSamplesPerChannel - *nSamplesRendered;
4030 :
4031 : /* check if we still need to prepare the renderer */
4032 222228 : if ( hIvasDec->hasBeenPreparedRendering == false )
4033 : {
4034 148659 : if ( ( error = IVAS_DEC_PrepareRenderer( hIvasDec ) ) != IVAS_ERR_OK )
4035 : {
4036 0 : return error;
4037 : }
4038 : }
4039 :
4040 222228 : if ( splitRendBits != NULL )
4041 : {
4042 : /* Render head poses from time-scaled transport channels */
4043 151015 : if ( ( error = isar_render_poses( hIvasDec, nSamplesToRender, &nSamplesRendered_loop, &tmp ) ) != IVAS_ERR_OK )
4044 : {
4045 0 : return error;
4046 : }
4047 : }
4048 : else
4049 : {
4050 : /* render IVAS frames directly to the output buffer */
4051 71213 : if ( ( error = IVAS_DEC_GetSamplesRenderer( hIvasDec, nSamplesToRender, pcmType, pcm_buffer_offset( pcmBuf, pcmType, *nSamplesRendered * nOutChannels ), &nSamplesRendered_loop, &tmp ) ) != IVAS_ERR_OK )
4052 : {
4053 0 : return error;
4054 : }
4055 : }
4056 :
4057 222228 : *nSamplesRendered += nSamplesRendered_loop;
4058 222228 : update_voip_rendered20ms( hIvasDec, nSamplesRendered_loop );
4059 : }
4060 : }
4061 :
4062 160359 : if ( hIvasDec->hasDecodedFirstGoodFrame && splitRendBits != NULL )
4063 : {
4064 : /* Analyse head poses over entire frame, generate ISAR metadata and maybe encode if split coded */
4065 110822 : if ( ( error = isar_generate_metadata_and_bitstream( st_ivas, p_head_pose_buf, *nSamplesRendered, splitRendBits ) ) != IVAS_ERR_OK )
4066 : {
4067 0 : return error;
4068 : }
4069 :
4070 : /* Synthesise PCM output if split PCM */
4071 110822 : if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
4072 : {
4073 : #ifndef DISABLE_LIMITER
4074 965 : ivas_limiter_dec( st_ivas->hLimiter, p_head_pose_buf, st_ivas->hDecoderConfig->nchan_out, *nSamplesRendered, st_ivas->BER_detect );
4075 : #endif
4076 :
4077 : #ifdef DEBUGGING
4078 : st_ivas->noClipping +=
4079 : #endif
4080 965 : ivas_syn_output( p_head_pose_buf, *nSamplesRendered, st_ivas->hDecoderConfig->nchan_out, (int16_t *) pcmBuf );
4081 : }
4082 : }
4083 :
4084 160359 : return IVAS_ERR_OK;
4085 : }
4086 :
4087 :
4088 : /*---------------------------------------------------------------------*
4089 : * IVAS_DEC_VoIP_GetSamples( )
4090 : *
4091 : * Main function to decode one frame in VoIP
4092 : *---------------------------------------------------------------------*/
4093 :
4094 130527 : ivas_error IVAS_DEC_VoIP_GetSamples(
4095 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
4096 : uint16_t nSamplesPerChannel, /* i : number of samples per channel requested to be written to output buffer */
4097 : const IVAS_DEC_PCM_TYPE pcmType, /* i : type for the decoded PCM resolution */
4098 : void *pcmBuf, /* o : output synthesis signal */
4099 : #ifdef SUPPORT_JBM_TRACEFILE
4100 : JbmTraceFileWriterFn jbmWriterFn,
4101 : void *jbmWriter,
4102 : #endif
4103 : bool *bitstreamReadDone, /* o : flag indicating that bitstream was read */
4104 : uint16_t *nSamplesRendered, /* o : number of samples rendered */
4105 : bool *parametersAvailableForEditing, /* o : indicates whether objects editing is available */
4106 : const uint32_t systemTimestamp_ms /* i : current system timestamp */
4107 : )
4108 : {
4109 130527 : return ivas_dec_voip_get_samples_common(
4110 : hIvasDec,
4111 : nSamplesPerChannel,
4112 : pcmType,
4113 : pcmBuf,
4114 : NULL,
4115 : NULL,
4116 : #ifdef SUPPORT_JBM_TRACEFILE
4117 : jbmWriterFn,
4118 : jbmWriter,
4119 : #endif
4120 : bitstreamReadDone,
4121 : nSamplesRendered,
4122 : parametersAvailableForEditing,
4123 : systemTimestamp_ms );
4124 : }
4125 :
4126 :
4127 : /*---------------------------------------------------------------------*
4128 : * IVAS_DEC_VoIP_GetSplitBinauralBitstream( )
4129 : *
4130 : * Main function to decode one split-rendering frame in VoIP
4131 : *---------------------------------------------------------------------*/
4132 :
4133 : /*! r: error code */
4134 317231 : ivas_error IVAS_DEC_VoIP_GetSplitBinauralBitstream(
4135 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
4136 : /* const IVAS_DEC_PCM_TYPE pcmType, */ /* i : type for the decoded PCM resolution */
4137 : void *pcmBuf, /* o : output synthesis signal */
4138 : ISAR_SPLIT_REND_BITS_DATA *splitRendBits, /* o : output split rendering bits */
4139 : #ifdef SUPPORT_JBM_TRACEFILE
4140 : JbmTraceFileWriterFn jbmWriterFn,
4141 : void *jbmWriter,
4142 : #endif
4143 : bool *bitstreamReadDone, /* o : flag indicating that bitstream was read */
4144 : uint16_t *nSamplesRendered, /* o : number of samples rendered */
4145 : bool *parametersAvailableForEditing, /* o : indicates whether objects editing is available */
4146 : const uint32_t systemTimestamp_ms /* i : current system timestamp */
4147 : )
4148 : {
4149 : int16_t i;
4150 : float head_pose_buf[BINAURAL_CHANNELS * MAX_HEAD_ROT_POSES][L_FRAME48k];
4151 : float *pp_head_pose_buf[BINAURAL_CHANNELS * MAX_HEAD_ROT_POSES];
4152 317231 : ivas_error error = IVAS_ERR_UNKNOWN;
4153 317231 : int16_t nSamplesPerChannel = 0;
4154 :
4155 317231 : if ( ( error = IVAS_DEC_GetRenderFramesizeSamples( hIvasDec, &nSamplesPerChannel ) ) != IVAS_ERR_OK )
4156 : {
4157 0 : return error;
4158 : }
4159 :
4160 : /* Set pointers to beginning of head pose buffers */
4161 5392927 : for ( i = 0; i < BINAURAL_CHANNELS * MAX_HEAD_ROT_POSES; ++i )
4162 : {
4163 5075696 : pp_head_pose_buf[i] = head_pose_buf[i];
4164 : }
4165 :
4166 317231 : return ivas_dec_voip_get_samples_common(
4167 : hIvasDec,
4168 : nSamplesPerChannel,
4169 : IVAS_DEC_PCM_INT16,
4170 : pcmBuf,
4171 : splitRendBits,
4172 : pp_head_pose_buf,
4173 : #ifdef SUPPORT_JBM_TRACEFILE
4174 : jbmWriterFn,
4175 : jbmWriter,
4176 : #endif
4177 : bitstreamReadDone,
4178 : nSamplesRendered,
4179 : parametersAvailableForEditing,
4180 : systemTimestamp_ms );
4181 : }
4182 :
4183 :
4184 : /*---------------------------------------------------------------------*
4185 : * update_voip_rendered20ms( )
4186 : *
4187 : * Update the number of samples that have been rendered since the last 20ms render border
4188 : *---------------------------------------------------------------------*/
4189 :
4190 227388 : static void update_voip_rendered20ms(
4191 : IVAS_DEC_HANDLE hIvasDec,
4192 : const int16_t nSamplesRendered )
4193 : {
4194 : int16_t nSamplesRenderedTotal;
4195 :
4196 227388 : nSamplesRenderedTotal = hIvasDec->hVoIP->nSamplesRendered20ms + nSamplesRendered;
4197 :
4198 : /* we have crossed a 20ms border, reset the time scaling done flag */
4199 227388 : if ( nSamplesRenderedTotal >= hIvasDec->nSamplesFrame )
4200 : {
4201 154069 : hIvasDec->timeScalingDone = 0;
4202 : }
4203 :
4204 227388 : hIvasDec->hVoIP->nSamplesRendered20ms = nSamplesRenderedTotal % hIvasDec->nSamplesFrame;
4205 :
4206 227388 : return;
4207 : }
4208 :
4209 :
4210 : /*---------------------------------------------------------------------*
4211 : * IVAS_DEC_VoIP_Flush( )
4212 : *
4213 : * Function to flush remaining audio samples in VoIP
4214 : *---------------------------------------------------------------------*/
4215 :
4216 561 : ivas_error IVAS_DEC_Flush(
4217 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
4218 : const int16_t nSamplesPerChannel, /* i : number of samples per channel requested to be written to output buffer */
4219 : const IVAS_DEC_PCM_TYPE pcmType, /* i : type for the decoded PCM resolution */
4220 : void *pcmBuf, /* o : output synthesis signal */
4221 : int16_t *nSamplesFlushed /* o : number of samples flushed */
4222 : )
4223 : {
4224 : ivas_error error;
4225 : uint16_t nSamplesToRender;
4226 : uint16_t nSamplesFlushedLocal;
4227 :
4228 561 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
4229 : {
4230 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
4231 : }
4232 :
4233 : #ifdef TMP_1342_WORKAROUND_DEC_FLUSH_BROKEN_IN_SR
4234 561 : hIvasDec->st_ivas->flushing = 1;
4235 : #endif
4236 :
4237 561 : *nSamplesFlushed = min( nSamplesPerChannel, hIvasDec->nSamplesAvailableNext );
4238 :
4239 561 : nSamplesToRender = (uint16_t) *nSamplesFlushed;
4240 :
4241 : /* render IVAS frames */
4242 561 : error = IVAS_ERR_OK;
4243 561 : if ( nSamplesToRender > 0 && hIvasDec->st_ivas->ivas_format != MONO_FORMAT )
4244 : {
4245 277 : error = ivas_dec_render( hIvasDec->st_ivas, nSamplesToRender, &nSamplesFlushedLocal, &hIvasDec->nSamplesAvailableNext, pcm_type_API_to_internal( pcmType ), pcmBuf );
4246 : }
4247 : else
4248 : {
4249 284 : *nSamplesFlushed = 0;
4250 : }
4251 :
4252 : #ifdef TMP_1342_WORKAROUND_DEC_FLUSH_BROKEN_IN_SR
4253 561 : hIvasDec->st_ivas->flushing = 0;
4254 : #endif
4255 :
4256 561 : return error;
4257 : }
4258 :
4259 :
4260 : /*---------------------------------------------------------------------*
4261 : * IVAS_DEC_isRestartNeeded( )
4262 : *
4263 : *
4264 : *---------------------------------------------------------------------*/
4265 :
4266 138740 : ivas_error IVAS_DEC_isRestartNeeded(
4267 : IVAS_DEC_HANDLE hIvasDec, /* i : IVAS decoder handle */
4268 : bool *restartNeeded /* o : flag to signal decoder restart */
4269 :
4270 : )
4271 : {
4272 138740 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
4273 : {
4274 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
4275 : }
4276 :
4277 138740 : *restartNeeded = hIvasDec->st_ivas->restartNeeded > 0;
4278 :
4279 138740 : return IVAS_ERR_OK;
4280 : }
4281 :
4282 :
4283 : /*---------------------------------------------------------------------*
4284 : * IVAS_DEC_VoIP_IsEmpty( )
4285 : *
4286 : * Returns 'true' if decoder has no data in VoIP jitter buffer
4287 : *---------------------------------------------------------------------*/
4288 :
4289 160920 : ivas_error IVAS_DEC_VoIP_IsEmpty(
4290 : IVAS_DEC_HANDLE hIvasDec, /* i : IVAS decoder handle */
4291 : const int16_t nSamplesAsked, /* i : number of output samples asked */
4292 : bool *isEmpty /* o : isEmpty flag */
4293 : )
4294 : {
4295 160920 : if ( hIvasDec == NULL || hIvasDec->hVoIP == NULL )
4296 : {
4297 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
4298 : }
4299 :
4300 160920 : *isEmpty = ( JB4_bufferedDataUnits( hIvasDec->hVoIP->hJBM ) == 0 ) && ( hIvasDec->nSamplesAvailableNext < nSamplesAsked );
4301 :
4302 160920 : return IVAS_ERR_OK;
4303 : }
4304 :
4305 :
4306 : /*---------------------------------------------------------------------*
4307 : * IVAS_DEC_VoIP_Get_CA_offset( )
4308 : *
4309 : *
4310 : *---------------------------------------------------------------------*/
4311 :
4312 0 : ivas_error IVAS_DEC_VoIP_Get_CA_offset(
4313 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
4314 : int16_t *optimum_offset,
4315 : int16_t *FEC_hi )
4316 : {
4317 0 : if ( hIvasDec == NULL || hIvasDec->hVoIP == NULL || hIvasDec->hVoIP->hJBM == NULL )
4318 : {
4319 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
4320 : }
4321 :
4322 0 : *optimum_offset = JB4_getFECoffset( hIvasDec->hVoIP->hJBM );
4323 0 : *FEC_hi = JB4_FECoffset( hIvasDec->hVoIP->hJBM );
4324 :
4325 0 : return IVAS_ERR_OK;
4326 : }
4327 :
4328 :
4329 : /*---------------------------------------------------------------------*
4330 : * ivas_destroy_handle_VoIP( )
4331 : *
4332 : * Deallocate VoIP handle
4333 : *---------------------------------------------------------------------*/
4334 :
4335 568 : static void ivas_destroy_handle_VoIP(
4336 : IVAS_DEC_VOIP *hVoIP /* i/o: VoIP decoder handle */
4337 : )
4338 : {
4339 568 : JB4_Destroy( &hVoIP->hJBM );
4340 :
4341 568 : if ( hVoIP->bs_conversion_buf != NULL )
4342 : {
4343 : #define WMC_TOOL_SKIP
4344 : /* Bitstream conversion is not counted towards complexity and memory usage */
4345 568 : free( hVoIP->bs_conversion_buf );
4346 : #undef WMC_TOOL_SKIP
4347 : }
4348 :
4349 568 : free( hVoIP );
4350 :
4351 568 : return;
4352 : }
4353 :
4354 :
4355 : #ifdef SUPPORT_JBM_TRACEFILE
4356 : /*---------------------------------------------------------------------*
4357 : * store_JbmData()
4358 : *
4359 : * Store JBM trace data entry
4360 : *---------------------------------------------------------------------*/
4361 :
4362 35890 : static void store_JbmData(
4363 : IVAS_DEC_VOIP *hVoIP,
4364 : JB4_DATAUNIT_HANDLE dataUnit,
4365 : const uint32_t systemTimestamp_ms,
4366 : const uint16_t extBufferedSamples,
4367 : const int32_t output_Fs )
4368 : {
4369 : IVAS_JBM_TRACE_DATA *JbmTraceData;
4370 :
4371 35890 : if ( hVoIP == NULL )
4372 : {
4373 0 : return;
4374 : }
4375 :
4376 35890 : JbmTraceData = &hVoIP->JbmTraceData;
4377 :
4378 35890 : JbmTraceData->systemTimestamp_ms = systemTimestamp_ms;
4379 35890 : JbmTraceData->extBufferedSamples = extBufferedSamples;
4380 35890 : JbmTraceData->lastDecodedWasActive = hVoIP->lastDecodedWasActive;
4381 35890 : JbmTraceData->output_Fs = output_Fs;
4382 35890 : JbmTraceData->dataUnit_flag = dataUnit != NULL;
4383 35890 : if ( dataUnit != NULL )
4384 : {
4385 32724 : JbmTraceData->sequenceNumber = dataUnit->sequenceNumber;
4386 32724 : JbmTraceData->timeStamp = dataUnit->timeStamp;
4387 32724 : JbmTraceData->rcvTime = dataUnit->rcvTime;
4388 32724 : JbmTraceData->partial_frame = dataUnit->partial_frame;
4389 32724 : JbmTraceData->partialCopyOffset = dataUnit->partialCopyOffset;
4390 : }
4391 :
4392 35890 : return;
4393 : }
4394 :
4395 :
4396 : /*---------------------------------------------------------------------*
4397 : * IVAS_DEC_GetJbmData()
4398 : *
4399 : *
4400 : *---------------------------------------------------------------------*/
4401 :
4402 0 : ivas_error IVAS_DEC_GetJbmData(
4403 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
4404 : IVAS_JBM_TRACE_DATA *JbmTraceData /* o : JBM Trace data */
4405 :
4406 : )
4407 : {
4408 0 : if ( hIvasDec == NULL || hIvasDec->hVoIP == NULL || JbmTraceData == NULL )
4409 : {
4410 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
4411 : }
4412 :
4413 0 : *JbmTraceData = hIvasDec->hVoIP->JbmTraceData;
4414 :
4415 0 : return IVAS_ERR_OK;
4416 : }
4417 :
4418 : #endif
4419 :
4420 :
4421 : /*---------------------------------------------------------------------*
4422 : * IVAS_DEC_GetErrorMessage( )
4423 : *
4424 : * Maps error codes to error description strings
4425 : *---------------------------------------------------------------------*/
4426 :
4427 0 : const char *IVAS_DEC_GetErrorMessage(
4428 : ivas_error error /* i : decoder error code enum */
4429 : )
4430 : {
4431 0 : return ivas_error_to_string( error );
4432 : }
4433 :
4434 :
4435 : /*---------------------------------------------------------------------*
4436 : * printConfigInfo_dec( )
4437 : *
4438 : *
4439 : *---------------------------------------------------------------------*/
4440 :
4441 1678 : static ivas_error printConfigInfo_dec(
4442 : Decoder_Struct *st_ivas,
4443 : const int16_t bitstreamformat,
4444 : const bool Opt_VOIP,
4445 : const bool quietModeEnabled )
4446 : {
4447 : ivas_error error;
4448 : char config_str[200];
4449 : AUDIO_CONFIG output_config;
4450 :
4451 : /*-----------------------------------------------------------------*
4452 : * Print info on screen
4453 : *-----------------------------------------------------------------*/
4454 :
4455 1678 : fprintf( stdout, "\n" );
4456 :
4457 : /*-----------------------------------------------------------------*
4458 : * Print output sampling frequency
4459 : *-----------------------------------------------------------------*/
4460 :
4461 1678 : fprintf( stdout, "Output sampling rate: %d Hz\n", st_ivas->hDecoderConfig->output_Fs );
4462 :
4463 : /*-----------------------------------------------------------------*
4464 : * Print bitrate
4465 : *-----------------------------------------------------------------*/
4466 :
4467 1678 : if ( !quietModeEnabled )
4468 : {
4469 0 : if ( !Opt_VOIP )
4470 : {
4471 0 : fprintf( stdout, "Bitrate: %.2f kbps\n", (float) st_ivas->hDecoderConfig->ivas_total_brate / 1000 );
4472 :
4473 0 : if ( st_ivas->hDecoderConfig->ivas_total_brate <= 0 )
4474 : {
4475 0 : if ( bitstreamformat == G192 )
4476 : {
4477 0 : fprintf( stdout, "Active Bitrate not identified in bitstream file \n" );
4478 : }
4479 : else /* MIME */
4480 : {
4481 0 : fprintf( stdout, "Active Bitrate not identified from first MIME frame \n" );
4482 : }
4483 : }
4484 : }
4485 : }
4486 :
4487 : /*-----------------------------------------------------------------*
4488 : * Print output configuration
4489 : *-----------------------------------------------------------------*/
4490 :
4491 1678 : if ( st_ivas->ivas_format == MONO_FORMAT )
4492 : {
4493 5 : if ( st_ivas->hDecoderConfig->Opt_non_diegetic_pan )
4494 : {
4495 1 : fprintf( stdout, "Output configuration: mono EVS bit-exact decoding to stereo\n" );
4496 1 : fprintf( stdout, "Non-diegetic panning: %.2f\n", st_ivas->hDecoderConfig->non_diegetic_pan_gain * 90.f );
4497 : }
4498 : else
4499 : {
4500 4 : output_config = st_ivas->hDecoderConfig->output_config;
4501 4 : if ( output_config != IVAS_AUDIO_CONFIG_MONO )
4502 : {
4503 0 : get_channel_config( output_config, &config_str[0] );
4504 0 : fprintf( stdout, "Output configuration: mono EVS bit-exact decoding rendering to %s\n", config_str );
4505 : }
4506 : else
4507 : {
4508 4 : fprintf( stdout, "Output configuration: mono EVS bit-exact decoding\n" );
4509 : }
4510 : }
4511 : }
4512 : else
4513 : {
4514 1673 : if ( !quietModeEnabled )
4515 : {
4516 0 : if ( st_ivas->ivas_format == STEREO_FORMAT )
4517 : {
4518 0 : fprintf( stdout, "Input configuration: Stereo\n" );
4519 : }
4520 0 : else if ( st_ivas->ivas_format == ISM_FORMAT )
4521 : {
4522 0 : if ( st_ivas->ism_mode == ISM_MODE_PARAM )
4523 : {
4524 0 : fprintf( stdout, "Input configuration: ISM (ParamISM): 2 transport channels\n" );
4525 : }
4526 : else
4527 : {
4528 0 : fprintf( stdout, "Input configuration: ISM: %d transport channel(s)\n", st_ivas->nchan_transport );
4529 : }
4530 : }
4531 0 : else if ( st_ivas->ivas_format == SBA_FORMAT )
4532 : {
4533 0 : fprintf( stdout, "Input configuration: Scene Based Audio, Ambisonic order %i%s, %d transport channel(s)\n", st_ivas->sba_order, st_ivas->sba_planar ? " (Planar)" : "", st_ivas->nchan_transport );
4534 : }
4535 0 : else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
4536 : {
4537 0 : fprintf( stdout, "Input configuration: Combined Scene Based Audio, Ambisonic order %i, with %d Objects \n", st_ivas->sba_order, st_ivas->nchan_ism );
4538 : }
4539 0 : else if ( st_ivas->ivas_format == MASA_FORMAT )
4540 : {
4541 0 : fprintf( stdout, "Input configuration: MASA - %d channel(s)\n", st_ivas->nchan_transport );
4542 : }
4543 0 : else if ( st_ivas->ivas_format == MC_FORMAT )
4544 : {
4545 0 : if ( ( error = get_channel_config( st_ivas->transport_config, &config_str[0] ) ) != IVAS_ERR_OK )
4546 : {
4547 0 : return error;
4548 : }
4549 :
4550 0 : fprintf( stdout, "Input configuration: %s\n", config_str );
4551 : }
4552 0 : else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
4553 : {
4554 0 : fprintf( stdout, "Input configuration: combined ISM and MASA (%i ISM stream(s)) \n", st_ivas->nchan_ism );
4555 : }
4556 : }
4557 :
4558 1673 : output_config = st_ivas->hDecoderConfig->output_config;
4559 1673 : get_channel_config( output_config, &config_str[0] );
4560 1673 : fprintf( stdout, "Output configuration: %s\n", config_str );
4561 :
4562 1673 : if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
4563 : {
4564 1253 : fprintf( stdout, "Render framesize: %d ms\n", get_render_frame_size_ms( st_ivas->hDecoderConfig->render_num_subframes ) );
4565 : }
4566 1673 : if ( st_ivas->hDecoderConfig->Opt_HRTF_binary )
4567 : {
4568 24 : fprintf( stdout, "HRIR/BRIR file: ON\n" );
4569 : }
4570 :
4571 1673 : if ( st_ivas->hDecoderConfig->Opt_RendConfigCustom )
4572 : {
4573 1064 : fprintf( stdout, "Renderer config. file: ON\n" );
4574 : }
4575 :
4576 1673 : if ( st_ivas->hDecoderConfig->Opt_Headrotation )
4577 : {
4578 1115 : fprintf( stdout, "Head rotation: ON\n" );
4579 : }
4580 :
4581 1673 : if ( st_ivas->hDecoderConfig->Opt_ExternalOrientation )
4582 : {
4583 31 : fprintf( stdout, "External orientation: ON\n" );
4584 : }
4585 :
4586 1673 : if ( st_ivas->hDecoderConfig->orientation_tracking != IVAS_HEAD_ORIENT_TRK_NONE )
4587 : {
4588 16 : switch ( st_ivas->hDecoderConfig->orientation_tracking )
4589 : {
4590 12 : case IVAS_HEAD_ORIENT_TRK_AVG:
4591 12 : fprintf( stdout, "Orientation tracking: AVG\n" );
4592 12 : break;
4593 0 : case IVAS_HEAD_ORIENT_TRK_REF:
4594 0 : fprintf( stdout, "Orientation tracking: REF\n" );
4595 0 : break;
4596 2 : case IVAS_HEAD_ORIENT_TRK_REF_VEC:
4597 2 : fprintf( stdout, "Orientation tracking: REF_VEC\n" );
4598 2 : break;
4599 2 : case IVAS_HEAD_ORIENT_TRK_REF_VEC_LEV:
4600 2 : fprintf( stdout, "Orientation tracking: REF_VEC_LEV\n" );
4601 2 : break;
4602 0 : default:
4603 0 : break;
4604 : }
4605 1657 : }
4606 :
4607 1673 : if ( st_ivas->hDecoderConfig->Opt_non_diegetic_pan )
4608 : {
4609 1 : fprintf( stdout, "Non-diegetic panning: %.2f\n", st_ivas->hDecoderConfig->non_diegetic_pan_gain * 90.f );
4610 : }
4611 :
4612 1673 : if ( st_ivas->hDecoderConfig->Opt_dpid_on )
4613 : {
4614 2 : fprintf( stdout, "Directivity pattern: ON\n" );
4615 : }
4616 :
4617 1673 : if ( st_ivas->hDecoderConfig->Opt_aeid_on )
4618 : {
4619 4 : fprintf( stdout, "Acoustic environment ID:ON\n" );
4620 : }
4621 :
4622 1673 : if ( st_ivas->hDecoderConfig->Opt_ObjEdit_on )
4623 : {
4624 13 : fprintf( stdout, "Objects editing : ON\n" );
4625 : }
4626 : }
4627 :
4628 : /*-----------------------------------------------------------------*
4629 : * Print TSM mode info
4630 : *-----------------------------------------------------------------*/
4631 :
4632 1678 : if ( st_ivas->hDecoderConfig->Opt_tsm )
4633 : {
4634 568 : fprintf( stdout, "TSM mode: ON\n" );
4635 : }
4636 :
4637 1678 : return IVAS_ERR_OK;
4638 : }
4639 :
4640 :
4641 : /*---------------------------------------------------------------------*
4642 : * IVAS_DEC_PrintConfig( )
4643 : *
4644 : * Print decoder set-up info
4645 : *---------------------------------------------------------------------*/
4646 :
4647 1678 : ivas_error IVAS_DEC_PrintConfig(
4648 : const IVAS_DEC_HANDLE hIvasDec,
4649 : const bool quietModeEnabled,
4650 : const bool voipMode )
4651 : {
4652 1678 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
4653 : {
4654 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
4655 : }
4656 :
4657 1678 : printConfigInfo_dec( hIvasDec->st_ivas, hIvasDec->bitstreamformat, voipMode, quietModeEnabled );
4658 :
4659 1678 : return IVAS_ERR_OK;
4660 : }
4661 :
4662 :
4663 : #ifdef DEBUGGING
4664 : /*---------------------------------------------------------------------*
4665 : * IVAS_DEC_PrintConfigWithBitstream( )
4666 : *
4667 : *
4668 : *---------------------------------------------------------------------*/
4669 : #define WMC_TOOL_SKIP
4670 : void IVAS_DEC_PrintConfigWithBitstream(
4671 : IVAS_DEC_HANDLE hIvasDec,
4672 : const bool quietModeEnabled,
4673 : uint16_t bit_stream[],
4674 : const int16_t num_bits )
4675 : {
4676 : Decoder_Struct *st_ivas;
4677 :
4678 : /* Create a copy of decoder struct that will be modified by preview_indices(),
4679 : * leaving the original decoder struct unchanged. The additional memory used here
4680 : * should not be counted towards memory footprint of the decoder. */
4681 : st_ivas = malloc( sizeof( Decoder_Struct ) );
4682 : memcpy( st_ivas, hIvasDec->st_ivas, sizeof( Decoder_Struct ) );
4683 :
4684 : preview_indices( st_ivas, bit_stream, num_bits );
4685 :
4686 : /* Print config from modified decoder struct */
4687 : printConfigInfo_dec( st_ivas, hIvasDec->bitstreamformat, hIvasDec->Opt_VOIP, quietModeEnabled );
4688 :
4689 : free( st_ivas );
4690 :
4691 : return;
4692 : }
4693 :
4694 :
4695 : /*---------------------------------------------------------------------*
4696 : * IVAS_DEC_PrintConfigWithVoipBitstream( )
4697 : *
4698 : *
4699 : *---------------------------------------------------------------------*/
4700 :
4701 : void IVAS_DEC_PrintConfigWithVoipBitstream(
4702 : IVAS_DEC_HANDLE hIvasDec,
4703 : const bool quietModeEnabled,
4704 : uint8_t *au,
4705 : const uint16_t auSizeBits )
4706 : {
4707 : Decoder_Struct *st_ivas;
4708 : uint16_t bit_stream[MAX_BITS_PER_FRAME + 4 * 8];
4709 :
4710 : /* Create a copy of decoder struct that will be modified by preview_indices(),
4711 : * leaving the original decoder struct unchanged. The additional memory used here
4712 : * should not be counted towards memory footprint of the decoder. */
4713 : st_ivas = malloc( sizeof( Decoder_Struct ) );
4714 : memcpy( st_ivas, hIvasDec->st_ivas, sizeof( Decoder_Struct ) );
4715 :
4716 : bsCompactToSerial( au, bit_stream, auSizeBits );
4717 : preview_indices( st_ivas, bit_stream, auSizeBits );
4718 :
4719 : /* Print config from modified decoder struct */
4720 : printConfigInfo_dec( st_ivas, hIvasDec->bitstreamformat, hIvasDec->Opt_VOIP, quietModeEnabled );
4721 :
4722 : free( st_ivas );
4723 :
4724 : return;
4725 : }
4726 : #undef WMC_TOOL_SKIP
4727 : #endif
4728 :
4729 :
4730 : /*---------------------------------------------------------------------*
4731 : * IVAS_DEC_PrintDisclaimer( )
4732 : *
4733 : * Print IVAS disclaimer to console
4734 : *---------------------------------------------------------------------*/
4735 :
4736 1671 : void IVAS_DEC_PrintDisclaimer( void )
4737 : {
4738 1671 : print_disclaimer( stderr );
4739 :
4740 1671 : return;
4741 : }
4742 :
4743 :
4744 : /*---------------------------------------------------------------------*
4745 : * evs_dec_main( )
4746 : *
4747 : * EVS codec main decoder fucntion
4748 : *---------------------------------------------------------------------*/
4749 :
4750 5150 : static ivas_error evs_dec_main(
4751 : Decoder_Struct *st_ivas /* i : IVAS decoder structure */
4752 : )
4753 : {
4754 : DEC_CORE_HANDLE *hCoreCoder;
4755 : float *p_output[1];
4756 : int16_t nOutSamples;
4757 : ivas_error error;
4758 :
4759 5150 : hCoreCoder = st_ivas->hSCE[0]->hCoreCoder;
4760 5150 : hCoreCoder[0]->total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
4761 5150 : nOutSamples = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / FRAMES_PER_SEC );
4762 :
4763 5150 : mdct_switching_dec( hCoreCoder[0] );
4764 :
4765 5150 : p_output[0] = st_ivas->p_output_f[0];
4766 :
4767 : /* run the main EVS decoding routine */
4768 5150 : if ( hCoreCoder[0]->codec_mode == MODE1 )
4769 : {
4770 2617 : if ( hCoreCoder[0]->Opt_AMR_WB )
4771 : {
4772 0 : if ( ( error = amr_wb_dec( hCoreCoder[0], st_ivas->mem_hp20_out[0], p_output[0] ) ) != IVAS_ERR_OK )
4773 : {
4774 0 : return error;
4775 : }
4776 : }
4777 : else
4778 : {
4779 2617 : if ( ( error = evs_dec( hCoreCoder[0], st_ivas->mem_hp20_out[0], p_output[0], FRAMEMODE_NORMAL ) ) != IVAS_ERR_OK )
4780 : {
4781 0 : return error;
4782 : }
4783 : }
4784 : }
4785 : else
4786 : {
4787 2533 : if ( hCoreCoder[0]->bfi == 0 )
4788 : {
4789 2533 : if ( ( error = evs_dec( hCoreCoder[0], st_ivas->mem_hp20_out[0], p_output[0], FRAMEMODE_NORMAL ) ) != IVAS_ERR_OK )
4790 : {
4791 0 : return error;
4792 : }
4793 : }
4794 0 : else if ( hCoreCoder[0]->bfi == 2 )
4795 : {
4796 0 : if ( ( error = evs_dec( hCoreCoder[0], st_ivas->mem_hp20_out[0], p_output[0], FRAMEMODE_FUTURE ) ) != IVAS_ERR_OK )
4797 : {
4798 0 : return error;
4799 : }
4800 : }
4801 : else
4802 : {
4803 0 : if ( ( error = evs_dec( hCoreCoder[0], st_ivas->mem_hp20_out[0], p_output[0], FRAMEMODE_MISSING ) ) != IVAS_ERR_OK )
4804 : {
4805 0 : return error;
4806 : }
4807 : }
4808 : }
4809 :
4810 5150 : st_ivas->BER_detect = hCoreCoder[0]->BER_detect;
4811 :
4812 5150 : if ( st_ivas->hDecoderConfig->Opt_tsm && st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_MONO )
4813 : {
4814 : /* BE workaround: in order to keep EVS bit-exact wrt. TS 26.443, convert 'float' output data to 'short' before the TSM */
4815 : int16_t pcm_buf_local[L_FRAME48k];
4816 : #ifdef DEBUGGING
4817 : st_ivas->noClipping +=
4818 : #endif
4819 2050 : ivas_syn_output( &p_output[0], nOutSamples, 1, pcm_buf_local );
4820 2050 : mvs2r( pcm_buf_local, p_output[0], nOutSamples );
4821 : }
4822 :
4823 5150 : return IVAS_ERR_OK;
4824 : }
4825 :
4826 :
4827 : #ifdef DEBUGGING
4828 : /*---------------------------------------------------------------------*
4829 : * IVAS_DEC_GetBer_detect_flag()
4830 : *
4831 : * return BER_detect flag
4832 : *---------------------------------------------------------------------*/
4833 :
4834 : bool IVAS_DEC_GetBerDetectFlag(
4835 : IVAS_DEC_HANDLE hIvasDec /* i : IVAS decoder handle */
4836 : )
4837 : {
4838 : if ( hIvasDec->st_ivas->BER_detect == 1 )
4839 : {
4840 : return 1;
4841 : }
4842 : else
4843 : {
4844 : return 0;
4845 : }
4846 : }
4847 :
4848 :
4849 : /*---------------------------------------------------------------------*
4850 : * IVAS_DEC_GetNoCLipping()
4851 : *
4852 : * return number of clipped samples
4853 : *---------------------------------------------------------------------*/
4854 :
4855 : int32_t IVAS_DEC_GetNoCLipping(
4856 : IVAS_DEC_HANDLE hIvasDec /* i : IVAS decoder handle */
4857 : )
4858 : {
4859 : return hIvasDec->st_ivas->noClipping;
4860 : }
4861 :
4862 :
4863 : /*---------------------------------------------------------------------*
4864 : * IVAS_DEC_GetCntFramesLimited()
4865 : *
4866 : * return number of frames where limiter is applied
4867 : *---------------------------------------------------------------------*/
4868 :
4869 : int32_t IVAS_DEC_GetCntFramesLimited(
4870 : IVAS_DEC_HANDLE hIvasDec /* i : IVAS decoder handle */
4871 : )
4872 : {
4873 : if ( hIvasDec->st_ivas->hLimiter == NULL )
4874 : {
4875 : return 0;
4876 : }
4877 : else
4878 : {
4879 : return hIvasDec->st_ivas->hLimiter->cnt_frames_limited;
4880 : }
4881 : }
4882 :
4883 : #ifdef DEBUG_SBA_AUDIO_DUMP
4884 : /*---------------------------------------------------------------------*
4885 : * IVAS_DEC_GetSbaDebugParams( )
4886 : *
4887 : * Returns SBA debug parameters
4888 : *---------------------------------------------------------------------*/
4889 :
4890 : ivas_error IVAS_DEC_GetSbaDebugParams(
4891 : const IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
4892 : int16_t *numOutputChannels,
4893 : int16_t *numTransportChannels,
4894 : int16_t *pca_ingest_channels )
4895 : {
4896 : if ( hIvasDec->st_ivas == NULL )
4897 : {
4898 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
4899 : }
4900 :
4901 : if ( hIvasDec->st_ivas->ivas_format != SBA_FORMAT || hIvasDec->st_ivas->hSpar == NULL )
4902 : {
4903 : *numOutputChannels = 1;
4904 : *numTransportChannels = 1;
4905 : *pca_ingest_channels = 1;
4906 : }
4907 : else
4908 : {
4909 : *numOutputChannels = hIvasDec->st_ivas->hSpar->numOutChannels;
4910 : *numTransportChannels = hIvasDec->st_ivas->nchan_transport;
4911 : *pca_ingest_channels = hIvasDec->st_ivas->hSpar->pca_ingest_channels;
4912 : }
4913 :
4914 : return IVAS_ERR_OK;
4915 : }
4916 : #endif /* DEBUG_SBA_AUDIO_DUMP */
4917 :
4918 : #endif /* DEBUGGING */
4919 :
4920 :
4921 : /*---------------------------------------------------------------------*
4922 : * input_format_API_to_internal()
4923 : *
4924 : *
4925 : *---------------------------------------------------------------------*/
4926 :
4927 568 : static ivas_error input_format_API_to_internal(
4928 : IVAS_DEC_INPUT_FORMAT input_format,
4929 : int16_t *bitstream_format_internal,
4930 : int16_t *sdp_hf_only,
4931 : const bool is_voip_enabled )
4932 : {
4933 568 : switch ( input_format )
4934 : {
4935 552 : case IVAS_DEC_INPUT_FORMAT_G192:
4936 552 : *bitstream_format_internal = is_voip_enabled ? VOIP_G192_RTP : G192;
4937 552 : *sdp_hf_only = 0;
4938 552 : break;
4939 0 : case IVAS_DEC_INPUT_FORMAT_MIME:
4940 0 : *bitstream_format_internal = MIME;
4941 0 : *sdp_hf_only = 0;
4942 0 : break;
4943 0 : case IVAS_DEC_INPUT_FORMAT_RTPDUMP:
4944 0 : assert( is_voip_enabled && "RTP dump only supported in VoIP mode" );
4945 0 : *bitstream_format_internal = VOIP_RTPDUMP;
4946 0 : *sdp_hf_only = 0;
4947 0 : break;
4948 16 : case IVAS_DEC_INPUT_FORMAT_RTPDUMP_HF:
4949 16 : assert( is_voip_enabled && "RTP dump only supported in VoIP mode" );
4950 16 : *bitstream_format_internal = VOIP_RTPDUMP;
4951 16 : *sdp_hf_only = 1;
4952 16 : break;
4953 0 : default:
4954 0 : return IVAS_ERR_INVALID_BITSTREAM;
4955 : break;
4956 : }
4957 :
4958 568 : return IVAS_ERR_OK;
4959 : }
4960 :
4961 :
4962 : /*---------------------------------------------------------------------*
4963 : * apa_setup()
4964 : *
4965 : * Setup APA decoder
4966 : *---------------------------------------------------------------------*/
4967 :
4968 4403 : static ivas_error apa_setup(
4969 : IVAS_DEC_HANDLE hIvasDec,
4970 : const bool isInitialized_voip,
4971 : const uint16_t nTransportChannels )
4972 : {
4973 : uint16_t l_ts;
4974 :
4975 4403 : l_ts = (uint16_t) hIvasDec->st_ivas->hTcBuffer->n_samples_granularity;
4976 :
4977 4403 : if ( !isInitialized_voip )
4978 : {
4979 : DECODER_CONFIG_HANDLE hDecoderConfig;
4980 : uint16_t wss, css;
4981 : float startQuality;
4982 :
4983 561 : startQuality = hIvasDec->tsm_quality;
4984 561 : hDecoderConfig = hIvasDec->st_ivas->hDecoderConfig;
4985 :
4986 561 : if ( hDecoderConfig->output_Fs == 8000 )
4987 : {
4988 0 : wss = 1;
4989 0 : css = 1;
4990 : }
4991 561 : else if ( hDecoderConfig->output_Fs == 16000 )
4992 : {
4993 3 : wss = 2;
4994 3 : css = 1;
4995 : }
4996 558 : else if ( hDecoderConfig->output_Fs == 32000 )
4997 : {
4998 9 : wss = 4;
4999 9 : css = 2;
5000 : }
5001 549 : else if ( hDecoderConfig->output_Fs == 48000 )
5002 : {
5003 549 : wss = 6;
5004 549 : css = 3;
5005 : }
5006 : else
5007 : {
5008 0 : return IVAS_ERR_INIT_ERROR;
5009 : }
5010 :
5011 1122 : if ( apa_init( &hIvasDec->hTimeScaler, nTransportChannels ) != IVAS_ERR_OK ||
5012 1122 : apa_set_rate( hIvasDec->hTimeScaler, hDecoderConfig->output_Fs ) != 0 ||
5013 1122 : apa_set_complexity_options( hIvasDec->hTimeScaler, wss, css ) != 0 ||
5014 1122 : apa_set_quality( hIvasDec->hTimeScaler, startQuality, 4, 4 ) != 0 ||
5015 561 : apa_set_renderer_granularity( hIvasDec->hTimeScaler, l_ts ) != 0 )
5016 : {
5017 0 : return IVAS_ERR_INIT_ERROR;
5018 : }
5019 :
5020 561 : if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
5021 : {
5022 2 : if ( apa_set_evs_compat_mode( hIvasDec->hTimeScaler, true ) != 0 )
5023 : {
5024 0 : return IVAS_ERR_INIT_ERROR;
5025 : }
5026 : }
5027 : }
5028 : else
5029 : {
5030 3842 : if ( apa_reconfigure( hIvasDec->hTimeScaler, nTransportChannels, l_ts ) != 0 )
5031 : {
5032 0 : return IVAS_ERR_INIT_ERROR;
5033 : }
5034 : }
5035 :
5036 4403 : hIvasDec->nTransportChannelsOld = nTransportChannels;
5037 :
5038 4403 : return IVAS_ERR_OK;
5039 : }
5040 :
5041 :
5042 : /*---------------------------------------------------------------------*
5043 : * IVAS_DEC_GetSplitRendBitstreamHeader()
5044 : *
5045 : *
5046 : *---------------------------------------------------------------------*/
5047 :
5048 1032 : ivas_error IVAS_DEC_GetSplitRendBitstreamHeader(
5049 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
5050 : ISAR_SPLIT_REND_CODEC *pCodec, /* o : pointer to codec setting */
5051 : ISAR_SPLIT_REND_POSE_CORRECTION_MODE *poseCorrection, /* o : pointer to pose correction mode */
5052 : int16_t *pIsar_frame_size_ms, /* o : pointer to ISAR frame size setting */
5053 : int16_t *pCodec_frame_size_ms, /* o : pointer to codec frame size setting */
5054 : int16_t *pLc3plusHighRes /* o : pointer to LC3plus High-Res setting */
5055 : )
5056 : {
5057 1032 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
5058 : {
5059 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
5060 : }
5061 :
5062 1032 : *pCodec = hIvasDec->st_ivas->hRenderConfig->split_rend_config.codec;
5063 1032 : *pCodec_frame_size_ms = hIvasDec->st_ivas->hRenderConfig->split_rend_config.codec_frame_size_ms;
5064 1032 : *poseCorrection = hIvasDec->st_ivas->hRenderConfig->split_rend_config.poseCorrectionMode;
5065 1032 : *pIsar_frame_size_ms = hIvasDec->st_ivas->hRenderConfig->split_rend_config.isar_frame_size_ms;
5066 1032 : *pLc3plusHighRes = hIvasDec->st_ivas->hRenderConfig->split_rend_config.lc3plus_highres;
5067 :
5068 1032 : return IVAS_ERR_OK;
5069 : }
5070 :
5071 :
5072 : /*---------------------------------------------------------------------*
5073 : * IVAS_DEC_GetCldfbSamples()
5074 : *
5075 : * API function to output CLDFB samples
5076 : *---------------------------------------------------------------------*/
5077 :
5078 0 : ivas_error IVAS_DEC_GetCldfbSamples(
5079 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
5080 : float *out_real, /* o : buffer for decoded PCM real output in CLDFB domain */
5081 : float *out_imag, /* o : buffer for decoded PCM imag output in CLDFB domain */
5082 : AUDIO_CONFIG *audio_config, /* o : audio configuration */
5083 : int16_t *nOutSamples /* o : number of samples per channel written to output buffer */
5084 : )
5085 : {
5086 : ISAR_DEC_SPLIT_REND_WRAPPER_HANDLE hSplitBinRend;
5087 : int16_t ch, b, slot_idx, num_chs, maxBand, num_samples;
5088 :
5089 0 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hSplitBinRend == NULL )
5090 : {
5091 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
5092 : }
5093 :
5094 0 : hSplitBinRend = hIvasDec->st_ivas->hSplitBinRend;
5095 0 : num_samples = 0;
5096 :
5097 0 : if ( hSplitBinRend->hCldfbDataOut != NULL )
5098 : {
5099 0 : *audio_config = hSplitBinRend->hCldfbDataOut->config;
5100 0 : if ( hSplitBinRend->hCldfbDataOut->config != IVAS_AUDIO_CONFIG_INVALID )
5101 : {
5102 0 : num_chs = audioCfg2channels( hSplitBinRend->hCldfbDataOut->config );
5103 0 : maxBand = (int16_t) ( ( CLDFB_NO_CHANNELS_MAX * hIvasDec->st_ivas->hDecoderConfig->output_Fs ) / 48000 );
5104 :
5105 0 : for ( slot_idx = 0; slot_idx < CLDFB_NO_COL_MAX; slot_idx++ )
5106 : {
5107 0 : for ( b = 0; b < maxBand; b++ )
5108 : {
5109 0 : for ( ch = 0; ch < num_chs; ch++ )
5110 : {
5111 0 : *out_real++ = hSplitBinRend->hCldfbDataOut->Cldfb_RealBuffer[ch][slot_idx][b];
5112 0 : *out_imag++ = hSplitBinRend->hCldfbDataOut->Cldfb_ImagBuffer[ch][slot_idx][b];
5113 : }
5114 : }
5115 : }
5116 0 : num_samples = CLDFB_NO_COL_MAX * maxBand;
5117 : }
5118 : }
5119 : else
5120 : {
5121 0 : *audio_config = IVAS_AUDIO_CONFIG_INVALID;
5122 : }
5123 :
5124 0 : *nOutSamples = num_samples;
5125 :
5126 0 : return IVAS_ERR_OK;
5127 : }
5128 :
5129 :
5130 : /*---------------------------------------------------------------------*
5131 : * pcm_buffer_offset()
5132 : *
5133 : *
5134 : *---------------------------------------------------------------------*/
5135 :
5136 872017 : static void *pcm_buffer_offset(
5137 : void *buffer,
5138 : const IVAS_DEC_PCM_TYPE pcmType,
5139 : const int32_t offset )
5140 : {
5141 872017 : switch ( pcmType )
5142 : {
5143 260215 : case IVAS_DEC_PCM_FLOAT:
5144 : {
5145 260215 : float *tmpBuf = (float *) buffer;
5146 260215 : return (void *) ( tmpBuf + offset );
5147 : }
5148 : break;
5149 611802 : case IVAS_DEC_PCM_INT16:
5150 : {
5151 611802 : int16_t *tmpBuf = (int16_t *) buffer;
5152 611802 : return (void *) ( tmpBuf + offset );
5153 : }
5154 : break;
5155 0 : default:
5156 0 : break;
5157 : }
5158 :
5159 0 : return NULL;
5160 : }
5161 :
5162 :
5163 : /*---------------------------------------------------------------------*
5164 : * set_pcm_buffer_to_zero()
5165 : *
5166 : *
5167 : *---------------------------------------------------------------------*/
5168 :
5169 5160 : static ivas_error set_pcm_buffer_to_zero(
5170 : void *buffer,
5171 : const IVAS_DEC_PCM_TYPE pcmType,
5172 : const int16_t nZeroSamples )
5173 : {
5174 : ivas_error error;
5175 :
5176 5160 : error = IVAS_ERR_OK;
5177 5160 : switch ( pcmType )
5178 : {
5179 0 : case IVAS_DEC_PCM_FLOAT:
5180 0 : set_zero( (float *) buffer, nZeroSamples );
5181 0 : break;
5182 5160 : case IVAS_DEC_PCM_INT16:
5183 5160 : set_s( (int16_t *) buffer, 0, nZeroSamples );
5184 5160 : break;
5185 0 : default:
5186 0 : error = IVAS_ERR_INTERNAL;
5187 : }
5188 :
5189 5160 : return error;
5190 : }
5191 :
5192 :
5193 : /*---------------------------------------------------------------------*
5194 : * pcm_type_API_to_internal()
5195 : *
5196 : *
5197 : *---------------------------------------------------------------------*/
5198 :
5199 796450 : PCM_RESOLUTION pcm_type_API_to_internal(
5200 : const IVAS_DEC_PCM_TYPE pcmType )
5201 : {
5202 : PCM_RESOLUTION pcm_resolution;
5203 796450 : pcm_resolution = PCM_NOT_KNOW;
5204 :
5205 796450 : switch ( pcmType )
5206 : {
5207 260215 : case IVAS_DEC_PCM_FLOAT:
5208 260215 : pcm_resolution = PCM_FLOAT32;
5209 260215 : break;
5210 536235 : case IVAS_DEC_PCM_INT16:
5211 536235 : pcm_resolution = PCM_INT16;
5212 536235 : break;
5213 0 : default:
5214 0 : pcm_resolution = PCM_NOT_KNOW;
5215 : }
5216 :
5217 796450 : return pcm_resolution;
5218 : }
5219 :
5220 :
5221 : /*-------------------------------------------------------------------*
5222 : * ivas_create_handle_isar()
5223 : *
5224 : * Initialize IVAS decoder split-rendering handle
5225 : *-------------------------------------------------------------------*/
5226 :
5227 1032 : static ivas_error ivas_create_handle_isar(
5228 : ISAR_DEC_SPLIT_REND_WRAPPER_HANDLE *hSplitBinRend_out /* o : ISAR split binaural rendering handle */
5229 : )
5230 : {
5231 : ISAR_DEC_SPLIT_REND_WRAPPER_HANDLE hSplitBinRend;
5232 : int16_t i;
5233 :
5234 1032 : if ( ( hSplitBinRend = (ISAR_DEC_SPLIT_REND_WRAPPER_HANDLE) malloc( sizeof( ISAR_DEC_SPLIT_REND_WRAPPER ) ) ) == NULL )
5235 : {
5236 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for IVAS decoder handle" );
5237 : }
5238 :
5239 1032 : isar_init_split_rend_handles( &hSplitBinRend->splitrend );
5240 :
5241 1032 : hSplitBinRend->hMultiBinTdData = NULL;
5242 17544 : for ( i = 0; i < MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS; ++i )
5243 : {
5244 16512 : hSplitBinRend->hMultiBinCldfbData[i] = NULL;
5245 : }
5246 1032 : hSplitBinRend->hCldfbDataOut = NULL;
5247 1032 : hSplitBinRend->numTdSamplesPerChannelCached = 0;
5248 :
5249 1032 : *hSplitBinRend_out = hSplitBinRend;
5250 :
5251 1032 : return IVAS_ERR_OK;
5252 : }
5253 :
5254 :
5255 : /*-------------------------------------------------------------------*
5256 : * ivas_destroy_handle_isar()
5257 : *
5258 : * destroy IVAS decoder split rend handle
5259 : *-------------------------------------------------------------------*/
5260 :
5261 1678 : static void ivas_destroy_handle_isar(
5262 : ISAR_DEC_SPLIT_REND_WRAPPER_HANDLE *hSplitBinRend /* i/o: ISAR split binaural rendering handle */
5263 : )
5264 : {
5265 : int16_t i;
5266 :
5267 1678 : if ( *hSplitBinRend != NULL )
5268 : {
5269 1032 : if ( ( *hSplitBinRend )->hMultiBinTdData != NULL )
5270 : {
5271 226 : ivas_TD_RINGBUF_Close( &( *hSplitBinRend )->hMultiBinTdData );
5272 : }
5273 17544 : for ( i = 0; i < MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS; ++i )
5274 : {
5275 16512 : if ( ( *hSplitBinRend )->hMultiBinCldfbData[i] != NULL )
5276 : {
5277 6300 : ivas_CLDFB_RINGBUF_Close( &( *hSplitBinRend )->hMultiBinCldfbData[i] );
5278 : }
5279 : }
5280 :
5281 1032 : ISAR_PRE_REND_close( &( *hSplitBinRend )->splitrend, NULL );
5282 :
5283 1032 : if ( ( *hSplitBinRend )->hCldfbDataOut != NULL )
5284 : {
5285 254 : free( ( *hSplitBinRend )->hCldfbDataOut );
5286 254 : ( *hSplitBinRend )->hCldfbDataOut = NULL;
5287 : }
5288 :
5289 1032 : free( ( *hSplitBinRend ) );
5290 1032 : ( *hSplitBinRend ) = NULL;
5291 : }
5292 :
5293 1678 : return;
5294 : }
5295 :
5296 :
5297 : /*---------------------------------------------------------------------*
5298 : * IVAS_DEC_is_split_rendering_enabled()
5299 : *
5300 : *
5301 : *---------------------------------------------------------------------*/
5302 :
5303 : /*! r: decoder error code */
5304 3342 : ivas_error IVAS_DEC_is_split_rendering_enabled(
5305 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
5306 : int16_t *isSplitRend /* o : flag to indicate if split rendering is enabled */
5307 : )
5308 : {
5309 : Decoder_Struct *st_ivas;
5310 :
5311 3342 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
5312 : {
5313 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
5314 : }
5315 :
5316 3342 : st_ivas = hIvasDec->st_ivas;
5317 :
5318 3342 : *isSplitRend = is_split_rendering_enabled( st_ivas->hDecoderConfig, st_ivas->hRenderConfig );
5319 :
5320 3342 : return IVAS_ERR_OK;
5321 : }
5322 :
5323 :
5324 : /*-------------------------------------------------------------------*
5325 : * ivas_dec_reconfig_split_rend()
5326 : *
5327 : * IVAS decoder split rend reconfig
5328 : *-------------------------------------------------------------------*/
5329 :
5330 207483 : static ivas_error ivas_dec_reconfig_split_rend(
5331 : Decoder_Struct *st_ivas /* i : IVAS decoder structure */
5332 : )
5333 : {
5334 : ivas_error error;
5335 : int16_t cldfb_in_flag, num_ch, ch, isCldfbNeeded, i, pcm_out_flag;
5336 : SPLIT_REND_WRAPPER *hSplitRendWrapper;
5337 :
5338 207483 : hSplitRendWrapper = &st_ivas->hSplitBinRend->splitrend;
5339 207483 : pcm_out_flag = ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ? 1 : 0;
5340 207483 : cldfb_in_flag = 0;
5341 :
5342 207483 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV ||
5343 132457 : st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ||
5344 132457 : st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC ||
5345 53126 : st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
5346 : {
5347 154357 : cldfb_in_flag = 1;
5348 : }
5349 :
5350 207483 : ISAR_PRE_REND_GetMultiBinPoseData( &st_ivas->hRenderConfig->split_rend_config, &hSplitRendWrapper->multiBinPoseData, ( st_ivas->hHeadTrackData != NULL ) ? st_ivas->hHeadTrackData->sr_pose_pred_axis : DEFAULT_AXIS );
5351 :
5352 207483 : isCldfbNeeded = 0;
5353 :
5354 207483 : if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV && st_ivas->ivas_format == SBA_ISM_FORMAT ) ||
5355 173347 : ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC && st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode == ISM_MASA_MODE_DISC ) )
5356 : {
5357 54991 : cldfb_in_flag = 0;
5358 : }
5359 :
5360 207483 : if ( st_ivas->renderer_type != RENDERER_DISABLE )
5361 : {
5362 207483 : if ( cldfb_in_flag == 0 )
5363 : {
5364 108117 : isCldfbNeeded = 1;
5365 : }
5366 99366 : else if ( st_ivas->hRenderConfig->split_rend_config.codec == ISAR_SPLIT_REND_CODEC_LC3PLUS && cldfb_in_flag )
5367 : {
5368 26238 : isCldfbNeeded = 1;
5369 : }
5370 73128 : else if ( pcm_out_flag && cldfb_in_flag )
5371 : {
5372 804 : isCldfbNeeded = 1;
5373 : }
5374 : }
5375 0 : else if ( st_ivas->hDecoderConfig->Opt_non_diegetic_pan )
5376 : {
5377 0 : isCldfbNeeded = 1;
5378 : }
5379 :
5380 207483 : if ( isCldfbNeeded == 1 && hSplitRendWrapper->hCldfbHandles == NULL )
5381 : {
5382 0 : if ( ( hSplitRendWrapper->hCldfbHandles = (CLDFB_HANDLES_WRAPPER_HANDLE) malloc( sizeof( CLDFB_HANDLES_WRAPPER ) ) ) == NULL )
5383 : {
5384 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CLDFB handles\n" ) );
5385 : }
5386 :
5387 0 : num_ch = MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS;
5388 0 : for ( ch = 0; ch < num_ch; ch++ )
5389 : {
5390 0 : hSplitRendWrapper->hCldfbHandles->cldfbAna[ch] = NULL;
5391 : }
5392 :
5393 0 : num_ch = hSplitRendWrapper->multiBinPoseData.num_poses * BINAURAL_CHANNELS;
5394 :
5395 0 : for ( ch = 0; ch < num_ch; ch++ )
5396 : {
5397 0 : if ( ( error = openCldfb( &( hSplitRendWrapper->hCldfbHandles->cldfbAna[ch] ), CLDFB_ANALYSIS, st_ivas->hDecoderConfig->output_Fs, CLDFB_PROTOTYPE_5_00MS ) ) != IVAS_ERR_OK )
5398 : {
5399 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not open CLDFB handles\n" ) );
5400 : }
5401 : }
5402 :
5403 0 : for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
5404 : {
5405 0 : if ( ( error = openCldfb( &( hSplitRendWrapper->hCldfbHandles->cldfbSyn[ch] ), CLDFB_SYNTHESIS, st_ivas->hDecoderConfig->output_Fs, CLDFB_PROTOTYPE_5_00MS ) ) != IVAS_ERR_OK )
5406 : {
5407 0 : return error;
5408 : }
5409 : }
5410 : }
5411 207483 : else if ( isCldfbNeeded == 0 && hSplitRendWrapper->hCldfbHandles != NULL )
5412 : {
5413 0 : num_ch = MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS;
5414 0 : for ( ch = 0; ch < num_ch; ch++ )
5415 : {
5416 0 : if ( hSplitRendWrapper->hCldfbHandles->cldfbAna[ch] != NULL )
5417 : {
5418 0 : deleteCldfb( &hSplitRendWrapper->hCldfbHandles->cldfbAna[ch] );
5419 0 : hSplitRendWrapper->hCldfbHandles->cldfbAna[ch] = NULL;
5420 : }
5421 : }
5422 :
5423 0 : for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
5424 : {
5425 0 : if ( hSplitRendWrapper->hCldfbHandles->cldfbSyn[ch] != NULL )
5426 : {
5427 0 : deleteCldfb( &hSplitRendWrapper->hCldfbHandles->cldfbSyn[ch] );
5428 0 : hSplitRendWrapper->hCldfbHandles->cldfbSyn[ch] = NULL;
5429 : }
5430 : }
5431 :
5432 0 : free( hSplitRendWrapper->hCldfbHandles );
5433 0 : hSplitRendWrapper->hCldfbHandles = NULL;
5434 : }
5435 :
5436 207483 : if ( ( st_ivas->renderer_type != RENDERER_BINAURAL_OBJECTS_TD ) &&
5437 159793 : ( st_ivas->renderer_type != RENDERER_BINAURAL_FASTCONV || st_ivas->ivas_format != SBA_ISM_FORMAT ) &&
5438 125657 : !( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC && st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode == ISM_MASA_MODE_DISC ) )
5439 : {
5440 838416 : for ( i = 0; i < MAX_HEAD_ROT_POSES - 1; ++i )
5441 : {
5442 733614 : if ( st_ivas->hTdRendHandles[i] != NULL )
5443 : {
5444 0 : st_ivas->hTdRendHandles[i]->HrFiltSet_p = NULL;
5445 0 : ivas_td_binaural_close( &st_ivas->hTdRendHandles[i] );
5446 : }
5447 : }
5448 : }
5449 :
5450 207483 : return IVAS_ERR_OK;
5451 : }
5452 :
5453 :
5454 : /*-------------------------------------------------------------------*
5455 : * ivas_dec_split_rend_cldfb_in()
5456 : *
5457 : *
5458 : *-------------------------------------------------------------------*/
5459 :
5460 481269 : static int16_t ivas_dec_split_rend_cldfb_in(
5461 : const RENDERER_TYPE renderer_type /* i : renderer type */
5462 : )
5463 : {
5464 481269 : if ( renderer_type == RENDERER_BINAURAL_FASTCONV ||
5465 299173 : renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ||
5466 123104 : renderer_type == RENDERER_BINAURAL_PARAMETRIC ||
5467 : renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
5468 : {
5469 358165 : return 1;
5470 : }
5471 : else
5472 : {
5473 123104 : return 0;
5474 : }
5475 : }
5476 :
5477 :
5478 : /*-------------------------------------------------------------------*
5479 : * ivas_dec_init_split_rend()
5480 : *
5481 : * IVAS decoder split rendering initialization
5482 : *-------------------------------------------------------------------*/
5483 :
5484 1032 : static ivas_error ivas_dec_init_split_rend(
5485 : Decoder_Struct *st_ivas /* i : IVAS decoder structure */
5486 : )
5487 : {
5488 : ivas_error error;
5489 : int16_t cldfb_in_flag, pcm_out_flag;
5490 : int16_t mixed_td_cldfb_flag;
5491 : int16_t i, num_poses;
5492 :
5493 1032 : pcm_out_flag = ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ? 1 : 0;
5494 1032 : cldfb_in_flag = 0;
5495 :
5496 1032 : cldfb_in_flag = ivas_dec_split_rend_cldfb_in( st_ivas->renderer_type );
5497 :
5498 1032 : ISAR_PRE_REND_GetMultiBinPoseData( &st_ivas->hRenderConfig->split_rend_config, &st_ivas->hSplitBinRend->splitrend.multiBinPoseData, ( st_ivas->hHeadTrackData != NULL ) ? st_ivas->hHeadTrackData->sr_pose_pred_axis : DEFAULT_AXIS );
5499 :
5500 1032 : num_poses = st_ivas->hSplitBinRend->splitrend.multiBinPoseData.num_poses;
5501 1032 : assert( num_poses <= MAX_HEAD_ROT_POSES );
5502 :
5503 1032 : if ( cldfb_in_flag )
5504 : {
5505 7106 : for ( i = 0; i < num_poses * BINAURAL_CHANNELS; ++i )
5506 : {
5507 : /* note: this is intra-frame heap memory */
5508 6300 : if ( ( error = ivas_CLDFB_RINGBUF_Open( &st_ivas->hSplitBinRend->hMultiBinCldfbData[i], CLDFB_NO_COL_MAX ) ) != IVAS_ERR_OK )
5509 : {
5510 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for split rendering structure" );
5511 : }
5512 : }
5513 : }
5514 : else
5515 : {
5516 226 : if ( ( error = ivas_TD_RINGBUF_Open( &st_ivas->hSplitBinRend->hMultiBinTdData, get_render_frame_size_samples( st_ivas->hDecoderConfig ), num_poses * BINAURAL_CHANNELS ) ) != IVAS_ERR_OK )
5517 : {
5518 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for split rendering structure" );
5519 : }
5520 : }
5521 :
5522 1032 : if ( cldfb_in_flag == 1 && ( st_ivas->hSplitBinRend->splitrend.multiBinPoseData.poseCorrectionMode == ISAR_SPLIT_REND_POSE_CORRECTION_MODE_NONE ) )
5523 : {
5524 254 : if ( ( st_ivas->hSplitBinRend->hCldfbDataOut = (ISAR_DEC_SPLIT_REND_CLDFB_OUT_DATA_HANDLE) malloc( sizeof( ISAR_DEC_SPLIT_REND_CLDFB_OUT_DATA ) ) ) == NULL )
5525 : {
5526 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for cldfb data out buffer\n" ) );
5527 : }
5528 : }
5529 :
5530 1032 : mixed_td_cldfb_flag = 0;
5531 1032 : if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV && st_ivas->ivas_format == SBA_ISM_FORMAT ) ||
5532 896 : ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC && st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode == ISM_MASA_MODE_DISC ) )
5533 : {
5534 274 : mixed_td_cldfb_flag = 1;
5535 : }
5536 :
5537 1032 : error = ISAR_PRE_REND_open( &st_ivas->hSplitBinRend->splitrend, &st_ivas->hRenderConfig->split_rend_config, st_ivas->hDecoderConfig->output_Fs, cldfb_in_flag, pcm_out_flag, (int16_t) st_ivas->hDecoderConfig->render_num_subframes, mixed_td_cldfb_flag );
5538 :
5539 1032 : return error;
5540 : }
5541 :
5542 :
5543 : /*---------------------------------------------------------------------*
5544 : * IVAS_DEC_is_split_rendering_coded_out()
5545 : *
5546 : * Return flag to indicate if split rendering is enabled
5547 : *---------------------------------------------------------------------*/
5548 :
5549 : /*! r: decoder error code */
5550 3342 : ivas_error IVAS_DEC_is_split_rendering_coded_out(
5551 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
5552 : int16_t *isSplitCoded /* o : flag to indicate if split rendering is enabled */
5553 : )
5554 : {
5555 : Decoder_Struct *st_ivas;
5556 :
5557 3342 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
5558 : {
5559 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
5560 : }
5561 :
5562 3342 : st_ivas = hIvasDec->st_ivas;
5563 :
5564 3342 : *isSplitCoded = 0;
5565 :
5566 3342 : if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED ||
5567 1298 : ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_STEREO && st_ivas->hDecoderConfig->Opt_non_diegetic_pan && st_ivas->hRenderConfig->split_rend_config.dof == 0 ) )
5568 : {
5569 2044 : *isSplitCoded = 1;
5570 : }
5571 :
5572 3342 : return IVAS_ERR_OK;
5573 : }
5574 :
5575 :
5576 : /*---------------------------------------------------------------------*
5577 : * feedSinglePIorientation( )
5578 : *
5579 : * Feed a single orientation PI data to external orientation handle.
5580 : *---------------------------------------------------------------------*/
5581 :
5582 0 : static ivas_error feedSinglePIorientation(
5583 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
5584 : IVAS_QUATERNION *orientation /* i : orientation for this PI type */
5585 : )
5586 : {
5587 : int16_t i;
5588 : ivas_error error;
5589 : Decoder_Struct *st_ivas;
5590 : IVAS_QUATERNION invOrientation;
5591 :
5592 0 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
5593 : {
5594 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
5595 : }
5596 :
5597 0 : st_ivas = hIvasDec->st_ivas;
5598 :
5599 0 : if ( !st_ivas->hExtOrientationData )
5600 : {
5601 0 : if ( ( error = ivas_external_orientation_open( &( st_ivas->hExtOrientationData ), st_ivas->hDecoderConfig->render_num_subframes ) ) != IVAS_ERR_OK )
5602 : {
5603 0 : return error;
5604 : }
5605 : }
5606 :
5607 0 : if ( !st_ivas->hCombinedOrientationData )
5608 : {
5609 0 : if ( ( error = ivas_combined_orientation_open( &( st_ivas->hCombinedOrientationData ), st_ivas->hDecoderConfig->output_Fs, st_ivas->hDecoderConfig->render_num_subframes ) ) != IVAS_ERR_OK )
5610 : {
5611 0 : return error;
5612 : }
5613 : }
5614 :
5615 0 : QuaternionInverse( *orientation, &invOrientation );
5616 :
5617 : /* use the new PI orientation or the previously saved orientation in processing */
5618 0 : for ( i = 0; i < st_ivas->hExtOrientationData->num_subframes; i++ )
5619 : {
5620 0 : QuaternionProduct( st_ivas->hExtOrientationData->Quaternions[i], invOrientation,
5621 0 : &st_ivas->hExtOrientationData->Quaternions[i] );
5622 0 : st_ivas->hExtOrientationData->enableExternalOrientation[i] = true;
5623 : }
5624 :
5625 0 : hIvasDec->updateOrientation = true;
5626 :
5627 0 : return IVAS_ERR_OK;
5628 : }
5629 :
5630 :
5631 : /*---------------------------------------------------------------------*
5632 : * setDiegeticInput( )
5633 : *
5634 : * Set isDiegeticInput flag for combined orientation handle based on PI data.
5635 : *---------------------------------------------------------------------*/
5636 :
5637 0 : static void setDiegeticInputPI(
5638 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
5639 : const bool *diegeticPIValues /* i : diegetic values for the input stream */
5640 : )
5641 : {
5642 : int16_t i;
5643 :
5644 0 : if ( st_ivas->hCombinedOrientationData != NULL )
5645 : {
5646 0 : for ( i = 0; i < ( 1 + IVAS_MAX_NUM_OBJECTS ); i++ )
5647 : {
5648 0 : st_ivas->hCombinedOrientationData->isDiegeticInputPI[i] = diegeticPIValues[i];
5649 : }
5650 :
5651 0 : st_ivas->hCombinedOrientationData->isDiegeticInputPISet = true;
5652 : }
5653 :
5654 0 : return;
5655 : }
5656 :
5657 :
5658 : /*---------------------------------------------------------------------*
5659 : * IVAS_DEC_FeedPiDataToDecoder( )
5660 : *
5661 : *
5662 : *---------------------------------------------------------------------*/
5663 :
5664 0 : ivas_error IVAS_DEC_FeedPiDataToDecoder(
5665 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
5666 : IVAS_PIDATA_TS *piData, /* i : PI data received in rtp packet */
5667 : uint32_t numPiData /* i : number of PI data received in rtp packet */
5668 : )
5669 : {
5670 : uint32_t i;
5671 : Decoder_Struct *st_ivas;
5672 0 : ivas_error error = IVAS_ERR_OK;
5673 :
5674 0 : if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
5675 : {
5676 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
5677 : }
5678 :
5679 0 : st_ivas = hIvasDec->st_ivas;
5680 :
5681 0 : for ( i = 0; i < numPiData; i++ )
5682 : {
5683 0 : uint32_t piDataType = piData->data.noPiData.piDataType;
5684 :
5685 0 : switch ( piDataType )
5686 : {
5687 0 : case IVAS_PI_SCENE_ORIENTATION:
5688 : {
5689 0 : IVAS_QUATERNION *quat = &piData->data.scene.orientation;
5690 : #ifdef DEBUGGING
5691 : fprintf( stdout, "PI_SCENE_ORIENTATION : %f, %f, %f, %f\n", quat->w, quat->x, quat->y, quat->z );
5692 : #endif
5693 0 : error = feedSinglePIorientation( hIvasDec, quat );
5694 : }
5695 0 : break;
5696 :
5697 0 : case IVAS_PI_DEVICE_ORIENTATION_COMPENSATED:
5698 : {
5699 0 : IVAS_QUATERNION *quat = &piData->data.deviceCompensated.orientation;
5700 : #ifdef DEBUGGING
5701 : fprintf( stdout, "PI_DEVICE_ORIENTATION : %f, %f, %f, %f\n", quat->w, quat->x, quat->y, quat->z );
5702 : #endif
5703 0 : error = feedSinglePIorientation( hIvasDec, quat );
5704 : }
5705 0 : break;
5706 :
5707 0 : case IVAS_PI_ACOUSTIC_ENVIRONMENT:
5708 : {
5709 0 : uint16_t aeid = piData->data.acousticEnv.aeid;
5710 : #ifdef DEBUGGING
5711 : fprintf( stdout, "PI_ACOUSTIC_ENVIRONMENT : AEID : %d\n", aeid );
5712 : #endif
5713 :
5714 0 : if ( piData->data.acousticEnv.availLateReverb && st_ivas->hRenderConfig != NULL && aeid != st_ivas->hRenderConfig->roomAcoustics.aeID )
5715 : {
5716 0 : error = feedAcousticEnvPI( hIvasDec, piData->data.acousticEnv );
5717 : }
5718 : }
5719 0 : break;
5720 :
5721 0 : case IVAS_PI_DIEGETIC_TYPE:
5722 : {
5723 : #ifdef DEBUGGING
5724 : fprintf( stdout, "PI_DIEGETIC_TYPE : %d, %d, %d, %d, %d\n", piData->data.digeticIndicator.isDiegetic[0], piData->data.digeticIndicator.isDiegetic[1], piData->data.digeticIndicator.isDiegetic[2], piData->data.digeticIndicator.isDiegetic[3], piData->data.digeticIndicator.isDiegetic[4] );
5725 : #endif
5726 0 : setDiegeticInputPI( st_ivas, piData->data.digeticIndicator.isDiegetic );
5727 : }
5728 0 : break;
5729 :
5730 0 : default:
5731 : {
5732 : /* NOT HANDLED PI DATA - DO NOTHING */
5733 : }
5734 0 : break;
5735 : }
5736 :
5737 0 : if ( error != IVAS_ERR_OK )
5738 : {
5739 0 : return error;
5740 : }
5741 :
5742 0 : piData++;
5743 : }
5744 :
5745 0 : return IVAS_ERR_OK;
5746 : }
|