Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : #include "lib_dec.h"
34 : #include <math.h>
35 : #include <assert.h>
36 : #include <string.h>
37 : #include "ivas_cnst.h"
38 : #include "ivas_prot_rend_fx.h"
39 : #include "prot_fx.h"
40 : #include "ivas_prot_fx.h"
41 :
42 : #include "jbm_jb4sb.h"
43 : #include "jbm_pcmdsp_apa.h"
44 : #include "jbm_pcmdsp_fifo.h"
45 : #include <math.h>
46 : #include <assert.h>
47 : #include "wmc_auto.h"
48 :
49 : #define INV_1000_Q31 2147484 /*1/1000 IN Q31*/
50 :
51 : /*---------------------------------------------------------------------*
52 : * Local structs
53 : *---------------------------------------------------------------------*/
54 : struct IVAS_DEC_VOIP
55 : {
56 : UWord16 nSamplesFrame; /* Total number of samples in a frame (includes number of channels) */
57 : JB4_HANDLE hJBM;
58 : UWord16 lastDecodedWasActive;
59 : JB4_DATAUNIT_HANDLE hCurrentDataUnit; /* Points to the currently processed data unit */
60 : UWord16 *bs_conversion_buf; /* Buffer for bitstream conversion from packed to serial */
61 : #ifdef NONBE_FIX_864_JBM_RENDER_FRAMESIZE
62 : Word16 nSamplesRendered20ms; /* how many samples have been rendered since the last 20ms render border*/
63 : #endif
64 : #ifdef SUPPORT_JBM_TRACEFILE
65 : IVAS_JBM_TRACE_DATA JbmTraceData;
66 : #endif
67 : };
68 :
69 : typedef struct IVAS_DEC_VOIP IVAS_DEC_VOIP;
70 :
71 : struct IVAS_DEC
72 : {
73 : IVAS_DEC_MODE mode;
74 : UWord16 nSamplesFrame;
75 : Decoder_Struct *st_ivas;
76 : IVAS_DEC_VOIP *hVoIP;
77 : bool hasBeenFedFirstGoodFrame; /* False on init. Gets set to true after first good frame has been fed into the decoder, but not decoded yet. */
78 : 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 */
79 : bool isInitialized;
80 :
81 : Word16 bitstreamformat; /* Bitstream format flag (G.192/MIME/VOIP_G192_RTP/VOIP_RTPDUMP) */
82 : bool Opt_VOIP; /* flag indicating VOIP mode with JBM */
83 : Word16 tsm_scale; /* scale for TSM operation */
84 : Word16 tsm_max_scaling;
85 : #ifdef NONBE_FIX_864_JBM_RENDER_FRAMESIZE
86 : Word16 timeScalingDone; /* have we done already one TSM in a 20ms frame? */
87 : #endif
88 : Word16 tsm_quality; /*Q14*/
89 : Word32 *apaExecBuffer_fx; /* Buffer for APA scaling */
90 : PCMDSP_APA_HANDLE hTimeScaler;
91 : bool needNewFrame;
92 : bool hasBeenFedFrame;
93 : bool updateOrientation;
94 : UWord16 nSamplesAvailableNext;
95 : Word16 nSamplesRendered;
96 : Word16 nTransportChannelsOld;
97 : Word16 amrwb_rfc4867_flag; /* MIME from rfc4867 is used */
98 : Word16 sdp_hf_only; /* RTP payload format parameter: only Header-Full format without zero padding for size collision avoidance */
99 : Word16 prev_ft_speech; /* RXDTX handler: previous frametype flag for G.192 format AMRWB SID_FIRST detection */
100 : Word16 CNG; /* RXDTX handler: CNG=1, nonCNG=0 */
101 : };
102 :
103 : /*---------------------------------------------------------------------*
104 : * Local function declarations
105 : *---------------------------------------------------------------------*/
106 :
107 : static void IVAS_DEC_Close_VoIP( IVAS_DEC_VOIP *hVoIP );
108 : #ifdef SUPPORT_JBM_TRACEFILE
109 : static void store_JbmData( IVAS_DEC_VOIP *hVoIP, JB4_DATAUNIT_HANDLE dataUnit, const UWord32 systemTimestamp_ms, const UWord16 extBufferedSamples, const Word32 output_Fs );
110 : #endif
111 : static ivas_error evs_dec_main_fx( Decoder_Struct *st_ivas, const Word16 nOutSamples, Word32 *floatBuf, Word16 *pcmBuf );
112 : static ivas_error input_format_API_to_internal( IVAS_DEC_INPUT_FORMAT input_format, Word16 *bitstream_format_internal, Word16 *sdp_hf_only, const bool is_voip_enabled );
113 : static void init_decoder_config( DECODER_CONFIG_HANDLE hDecoderConfig );
114 : static ivas_error IVAS_DEC_VoIP_reconfigure( IVAS_DEC_HANDLE hIvasDec, const UWord16 nTransportChannels, const UWord16 l_ts );
115 : static ivas_error IVAS_DEC_Setup( IVAS_DEC_HANDLE hIvasDec, UWord16 *nTcBufferGranularity, UWord8 *nTransportChannels, UWord8 *nOutChannels, UWord16 *nSamplesRendered, Word16 *data );
116 : static ivas_error IVAS_DEC_GetTcSamples( IVAS_DEC_HANDLE hIvasDec, Word32 *pcmBuf_fx, Word16 *nOutSamples );
117 : static ivas_error IVAS_DEC_RendererFeedTcSamples( IVAS_DEC_HANDLE hIvasDec, const Word16 nSamplesForRendering, Word16 *nSamplesResidual, Word32 *pcmBuf );
118 : static ivas_error IVAS_DEC_GetRenderedSamples( IVAS_DEC_HANDLE hIvasDec, const UWord16 nSamplesForRendering, UWord16 *nSamplesRendered, UWord16 *nSamplesAvailableNext, Word16 *pcmBuf );
119 : static ivas_error IVAS_DEC_GetBufferedNumberOfSamples( IVAS_DEC_HANDLE hIvasDec, Word16 *nSamplesBuffered );
120 : static Word16 get_render_frame_size_ms( IVAS_RENDER_FRAMESIZE render_framesize );
121 : #ifdef NONBE_FIX_864_JBM_RENDER_FRAMESIZE
122 : static void update_voip_rendered20ms( IVAS_DEC_HANDLE hIvasDec, const Word16 nSamplesRendered );
123 : #endif
124 :
125 :
126 : /*---------------------------------------------------------------------*
127 : * IVAS_DEC_Open()
128 : *
129 : * Open IVAS decoder
130 : *---------------------------------------------------------------------*/
131 :
132 : /* may return an error but may still have allocated memory - thus run Close also in case of error to release memory */
133 604 : ivas_error IVAS_DEC_Open(
134 : IVAS_DEC_HANDLE *phIvasDec, /* i/o: pointer to an IVAS decoder handle to be opened */
135 : const IVAS_DEC_MODE mode /* i : compatibility mode (EVS or IVAS) */
136 : )
137 : {
138 : IVAS_DEC_HANDLE hIvasDec;
139 : Decoder_Struct *st_ivas;
140 :
141 604 : IF( phIvasDec == NULL )
142 : {
143 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
144 : }
145 :
146 : /*-----------------------------------------------------------------*
147 : * Allocate and initialize IVAS application decoder handle
148 : *-----------------------------------------------------------------*/
149 :
150 604 : IF( ( *phIvasDec = (IVAS_DEC_HANDLE) malloc( sizeof( struct IVAS_DEC ) ) ) == NULL )
151 : {
152 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for IVAS decoder handle" );
153 : }
154 604 : hIvasDec = *phIvasDec;
155 604 : hIvasDec->hVoIP = NULL;
156 604 : hIvasDec->apaExecBuffer_fx = NULL;
157 604 : hIvasDec->hTimeScaler = NULL;
158 604 : hIvasDec->tsm_scale = 100;
159 604 : hIvasDec->tsm_max_scaling = 0;
160 604 : hIvasDec->tsm_quality = ONE_IN_Q14; /*1.f Q14*/
161 : #ifdef NONBE_FIX_864_JBM_RENDER_FRAMESIZE
162 604 : hIvasDec->timeScalingDone = 0;
163 : #endif
164 604 : hIvasDec->needNewFrame = false;
165 604 : hIvasDec->nTransportChannelsOld = 0;
166 604 : hIvasDec->nSamplesAvailableNext = 0;
167 604 : hIvasDec->nSamplesRendered = 0;
168 604 : hIvasDec->nSamplesFrame = 0;
169 604 : hIvasDec->hasBeenFedFrame = false;
170 604 : hIvasDec->hasBeenFedFirstGoodFrame = false;
171 604 : hIvasDec->hasDecodedFirstGoodFrame = false;
172 604 : hIvasDec->isInitialized = false;
173 604 : hIvasDec->updateOrientation = false;
174 604 : move16();
175 604 : move16();
176 604 : move16();
177 604 : move16();
178 604 : move16();
179 604 : move16();
180 604 : move16();
181 604 : move16();
182 604 : move16();
183 604 : move16();
184 604 : move16();
185 604 : move16();
186 : #ifdef NONBE_FIX_864_JBM_RENDER_FRAMESIZE
187 604 : move16();
188 : #endif
189 :
190 604 : hIvasDec->mode = mode;
191 604 : move16();
192 :
193 604 : hIvasDec->bitstreamformat = G192;
194 604 : hIvasDec->Opt_VOIP = 0;
195 604 : hIvasDec->amrwb_rfc4867_flag = -1;
196 604 : hIvasDec->prev_ft_speech = 1; /* RXDTX handler previous frametype flag for G.192 format AMRWB SID_FIRST detection */
197 604 : hIvasDec->CNG = 0; /* RXDTX handler CNG = 1, no CNG = 0*/
198 604 : move16();
199 604 : move16();
200 604 : move16();
201 604 : move16();
202 604 : move16();
203 : /*-----------------------------------------------------------------*
204 : * Initialize IVAS-codec decoder state
205 : *-----------------------------------------------------------------*/
206 :
207 604 : IF( ( hIvasDec->st_ivas = (Decoder_Struct *) malloc( sizeof( Decoder_Struct ) ) ) == NULL )
208 : {
209 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for IVAS decoder structure" );
210 : }
211 :
212 604 : IF( ( hIvasDec->st_ivas->hDecoderConfig = (DECODER_CONFIG_HANDLE) malloc( sizeof( DECODER_CONFIG ) ) ) == NULL )
213 : {
214 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for Decoder config structure" );
215 : }
216 :
217 : /*-----------------------------------------------------------------*
218 : * Initialize IVAS-codec decoder state
219 : *-----------------------------------------------------------------*/
220 :
221 604 : st_ivas = hIvasDec->st_ivas;
222 :
223 : /* initialize Decoder Config. handle */
224 604 : init_decoder_config( hIvasDec->st_ivas->hDecoderConfig );
225 :
226 : /* initialize pointers to handles to NULL */
227 604 : ivas_initialize_handles_dec( st_ivas );
228 :
229 : /* set high-level parameters */
230 604 : IF( EQ_16( mode, IVAS_DEC_MODE_EVS ) )
231 : {
232 3 : st_ivas->codec_mode = 0; /* unknown before first frame */
233 3 : st_ivas->element_mode_init = EVS_MONO;
234 3 : st_ivas->ivas_format = MONO_FORMAT;
235 3 : st_ivas->transport_config = IVAS_AUDIO_CONFIG_INVALID;
236 3 : st_ivas->intern_config = IVAS_AUDIO_CONFIG_INVALID;
237 3 : st_ivas->writeFECoffset = 0;
238 3 : hIvasDec->hasDecodedFirstGoodFrame = true; /* Functionality to suppress output for initial lost frames is disabled in EVS operation */
239 3 : move16();
240 3 : move16();
241 3 : move16();
242 3 : move16();
243 3 : move16();
244 3 : move16();
245 3 : move16();
246 :
247 3 : return IVAS_ERR_OK;
248 : }
249 601 : ELSE IF( EQ_16( mode, IVAS_DEC_MODE_IVAS ) )
250 : {
251 601 : st_ivas->codec_mode = 0; /* unknown before first frame */
252 601 : st_ivas->element_mode_init = -1;
253 601 : st_ivas->ivas_format = UNDEFINED_FORMAT;
254 601 : st_ivas->transport_config = IVAS_AUDIO_CONFIG_INVALID;
255 601 : st_ivas->intern_config = IVAS_AUDIO_CONFIG_INVALID;
256 601 : st_ivas->renderer_type = RENDERER_DISABLE;
257 601 : st_ivas->ini_frame = 0;
258 601 : st_ivas->ini_active_frame = 0;
259 601 : st_ivas->writeFECoffset = 0;
260 :
261 601 : st_ivas->ism_mode = ISM_MODE_NONE;
262 601 : st_ivas->mc_mode = MC_MODE_NONE;
263 :
264 601 : st_ivas->sba_order = 0;
265 601 : st_ivas->sba_planar = 0;
266 601 : st_ivas->sba_analysis_order = 0;
267 :
268 601 : move16();
269 601 : move16();
270 601 : move16();
271 601 : move16();
272 601 : move16();
273 601 : move16();
274 601 : move16();
275 601 : move16();
276 601 : move16();
277 601 : move16();
278 601 : move16();
279 601 : move16();
280 601 : move16();
281 601 : move16();
282 601 : return IVAS_ERR_OK;
283 : }
284 :
285 0 : return IVAS_ERR_WRONG_PARAMS;
286 : }
287 :
288 :
289 : /*---------------------------------------------------------------------*
290 : * init_decoder_config()
291 : *
292 : * Initialize Decoder Config. handle
293 : *---------------------------------------------------------------------*/
294 :
295 604 : static void init_decoder_config(
296 : DECODER_CONFIG_HANDLE hDecoderConfig /* i/o: configuration structure */
297 : )
298 : {
299 604 : hDecoderConfig->Opt_AMR_WB = 0;
300 604 : hDecoderConfig->nchan_out = 1;
301 604 : hDecoderConfig->output_config = IVAS_AUDIO_CONFIG_INVALID;
302 604 : hDecoderConfig->Opt_LsCustom = 0;
303 604 : hDecoderConfig->Opt_HRTF_binary = 0;
304 604 : hDecoderConfig->Opt_Headrotation = 0;
305 604 : hDecoderConfig->Opt_RendConfigCustom = 0;
306 604 : hDecoderConfig->orientation_tracking = IVAS_HEAD_ORIENT_TRK_NONE;
307 604 : hDecoderConfig->Opt_non_diegetic_pan = 0;
308 604 : hDecoderConfig->non_diegetic_pan_gain_fx = 0; // Q15
309 604 : hDecoderConfig->Opt_tsm = 0;
310 604 : hDecoderConfig->Opt_delay_comp = 0;
311 604 : hDecoderConfig->Opt_ExternalOrientation = 0;
312 604 : hDecoderConfig->Opt_dpid_on = 0;
313 604 : hDecoderConfig->Opt_aeid_on = 0;
314 604 : move16();
315 604 : move16();
316 604 : move16();
317 604 : move16();
318 604 : move16();
319 604 : move16();
320 604 : move16();
321 604 : move16();
322 604 : move16();
323 604 : move16();
324 604 : move16();
325 604 : move16();
326 604 : move16();
327 604 : move16();
328 604 : move16();
329 604 : return;
330 : }
331 :
332 :
333 : /*---------------------------------------------------------------------*
334 : * IVAS_DEC_Close( )
335 : *
336 : *
337 : *---------------------------------------------------------------------*/
338 :
339 620 : void IVAS_DEC_Close(
340 : IVAS_DEC_HANDLE *phIvasDec /* i/o: pointer to IVAS decoder handle */
341 : )
342 : {
343 : /* Free all memory */
344 620 : test();
345 620 : IF( phIvasDec == NULL || *phIvasDec == NULL )
346 : {
347 16 : return;
348 : }
349 :
350 604 : IF( ( *phIvasDec )->hVoIP )
351 : {
352 27 : IVAS_DEC_Close_VoIP( ( *phIvasDec )->hVoIP );
353 27 : ( *phIvasDec )->hVoIP = NULL;
354 : }
355 :
356 604 : IF( ( *phIvasDec )->st_ivas )
357 : {
358 604 : ivas_destroy_dec_fx( ( *phIvasDec )->st_ivas );
359 604 : ( *phIvasDec )->st_ivas = NULL;
360 : }
361 :
362 604 : apa_exit( &( *phIvasDec )->hTimeScaler );
363 :
364 604 : IF( ( *phIvasDec )->apaExecBuffer_fx != NULL )
365 : {
366 27 : free( ( *phIvasDec )->apaExecBuffer_fx );
367 : }
368 604 : free( *phIvasDec );
369 604 : *phIvasDec = NULL;
370 604 : phIvasDec = NULL;
371 :
372 604 : return;
373 : }
374 :
375 :
376 : /*---------------------------------------------------------------------*
377 : * mapIvasFormat( )
378 : *
379 : *
380 : *---------------------------------------------------------------------*/
381 :
382 44 : static IVAS_DEC_BS_FORMAT mapIvasFormat(
383 : const IVAS_FORMAT ivas_format )
384 : {
385 44 : SWITCH( ivas_format )
386 : {
387 0 : case MONO_FORMAT:
388 0 : return IVAS_DEC_BS_MONO;
389 0 : case STEREO_FORMAT:
390 0 : return IVAS_DEC_BS_STEREO;
391 15 : case ISM_FORMAT:
392 15 : return IVAS_DEC_BS_OBJ;
393 0 : case MC_FORMAT:
394 0 : return IVAS_DEC_BS_MC;
395 6 : case SBA_FORMAT:
396 6 : return IVAS_DEC_BS_SBA;
397 3 : case SBA_ISM_FORMAT:
398 3 : return IVAS_DEC_BS_SBA_ISM;
399 17 : case MASA_FORMAT:
400 17 : return IVAS_DEC_BS_MASA;
401 3 : case MASA_ISM_FORMAT:
402 3 : return IVAS_DEC_BS_MASA_ISM;
403 0 : default:
404 0 : BREAK;
405 : }
406 :
407 0 : return IVAS_DEC_BS_UNKOWN;
408 : }
409 :
410 :
411 : /*---------------------------------------------------------------------*
412 : * IVAS_DEC_Configure( )
413 : *
414 : * Decoder configuration
415 : * legacy code behavior: if no output format set, then it's EVS mono
416 : *---------------------------------------------------------------------*/
417 :
418 604 : ivas_error IVAS_DEC_Configure(
419 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
420 : const UWord32 sampleRate, /* i : output sampling frequency */
421 : const AUDIO_CONFIG outputConfig, /* i : output configuration */
422 : const Word16 tsmEnabled, /* i : enable time scale modification */
423 : const IVAS_RENDER_FRAMESIZE renderFramesize, /* i : rendering frame size */
424 : const Word16 customLsOutputEnabled, /* i : enable custom loudspeaker setup handle */
425 : const Word16 hrtfReaderEnabled, /* i : enable HRTF binary file input */
426 : const Word16 enableHeadRotation, /* i : enable head rotation for binaural output */
427 : const Word16 enableExternalOrientation, /* i : enable external orientations */
428 : const IVAS_HEAD_ORIENT_TRK_T orientation_tracking, /* i : head orientation tracking type */
429 : const Word16 renderConfigEnabled, /* i : enable Renderer config. file for binaural output */
430 : const Word16 Opt_non_diegetic_pan, /* i : diegetic or not */
431 : const Word16 non_diegetic_pan_gain_fx, /* i : non diegetic panning gain Q15 */
432 : const Word16 Opt_dpid_on, /* i : enable directivity pattern option */
433 : const UWord16 acousticEnvironmentId, /* i : Acoustic environment ID */
434 : const Word16 delayCompensationEnabled /* i : enable delay compensation */
435 : )
436 : {
437 : Decoder_Struct *st_ivas;
438 : DECODER_CONFIG_HANDLE hDecoderConfig;
439 : ivas_error error;
440 :
441 604 : error = IVAS_ERR_OK;
442 604 : move32();
443 604 : test();
444 604 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
445 : {
446 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
447 : }
448 :
449 604 : test();
450 604 : test();
451 604 : test();
452 604 : IF( NE_32( sampleRate, 8000 ) && NE_32( sampleRate, 16000 ) && NE_32( sampleRate, 32000 ) && NE_32( sampleRate, 48000 ) )
453 : {
454 0 : return IVAS_ERR_WRONG_PARAMS;
455 : }
456 :
457 604 : test();
458 604 : test();
459 604 : test();
460 604 : test();
461 604 : IF( EQ_16( hIvasDec->mode, IVAS_DEC_MODE_EVS ) && !( ( EQ_16( outputConfig, IVAS_AUDIO_CONFIG_MONO ) && Opt_non_diegetic_pan == 0 ) ||
462 : ( EQ_16( outputConfig, IVAS_AUDIO_CONFIG_STEREO ) && EQ_16( Opt_non_diegetic_pan, 1 ) ) ) )
463 : {
464 0 : return IVAS_ERR_WRONG_MODE;
465 : }
466 :
467 604 : st_ivas = hIvasDec->st_ivas;
468 :
469 604 : hDecoderConfig = st_ivas->hDecoderConfig;
470 :
471 604 : hDecoderConfig->output_config = outputConfig;
472 604 : move16();
473 604 : IF( EQ_16( hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_INVALID ) )
474 : {
475 0 : return IVAS_ERR_WRONG_PARAMS;
476 : }
477 :
478 604 : hDecoderConfig->output_Fs = sampleRate;
479 604 : move32();
480 604 : IF( EQ_16( hIvasDec->mode, IVAS_DEC_MODE_EVS ) )
481 : {
482 3 : st_ivas->element_mode_init = EVS_MONO;
483 3 : hDecoderConfig->nchan_out = 1;
484 3 : move16();
485 3 : move16();
486 : }
487 :
488 604 : test();
489 604 : IF( NE_16( outputConfig, IVAS_AUDIO_CONFIG_EXTERNAL ) && NE_16( outputConfig, IVAS_AUDIO_CONFIG_LS_CUSTOM ) )
490 : {
491 551 : hDecoderConfig->nchan_out = audioCfg2channels( hDecoderConfig->output_config );
492 551 : move16();
493 : }
494 :
495 604 : hDecoderConfig->Opt_tsm = tsmEnabled;
496 604 : hDecoderConfig->Opt_LsCustom = customLsOutputEnabled;
497 604 : hDecoderConfig->Opt_Headrotation = enableHeadRotation;
498 604 : hDecoderConfig->orientation_tracking = orientation_tracking;
499 604 : hDecoderConfig->Opt_HRTF_binary = hrtfReaderEnabled;
500 604 : hDecoderConfig->Opt_RendConfigCustom = renderConfigEnabled;
501 604 : hDecoderConfig->Opt_non_diegetic_pan = Opt_non_diegetic_pan;
502 604 : hDecoderConfig->non_diegetic_pan_gain_fx = non_diegetic_pan_gain_fx; // Q15
503 604 : hDecoderConfig->Opt_delay_comp = delayCompensationEnabled;
504 604 : hDecoderConfig->Opt_ExternalOrientation = enableExternalOrientation;
505 604 : hDecoderConfig->Opt_dpid_on = Opt_dpid_on;
506 604 : IF( NE_32( (Word32) acousticEnvironmentId, 65535 ) )
507 : {
508 1 : hDecoderConfig->Opt_aeid_on = TRUE;
509 : }
510 : ELSE
511 : {
512 603 : hDecoderConfig->Opt_aeid_on = FALSE;
513 : }
514 604 : move16();
515 604 : move16();
516 604 : move16();
517 604 : move16();
518 604 : move16();
519 604 : move16();
520 604 : move16();
521 604 : move16();
522 604 : move16();
523 604 : move16();
524 604 : move16();
525 :
526 604 : IF( EQ_16( renderFramesize, IVAS_RENDER_FRAMESIZE_UNKNOWN ) )
527 : {
528 0 : return IVAS_ERR_WRONG_PARAMS;
529 : }
530 604 : IF( EQ_16( outputConfig, IVAS_AUDIO_CONFIG_EXTERNAL ) )
531 : {
532 50 : hDecoderConfig->render_framesize = IVAS_RENDER_FRAMESIZE_20MS;
533 : }
534 : ELSE
535 : {
536 554 : hDecoderConfig->render_framesize = renderFramesize;
537 : }
538 :
539 604 : move16();
540 :
541 : /* Set decoder parameters to initial values */
542 604 : IF( NE_32( ( error = ivas_init_decoder_front( st_ivas ) ), IVAS_ERR_OK ) )
543 : {
544 0 : return error;
545 : }
546 :
547 604 : if ( EQ_16( hIvasDec->mode, IVAS_DEC_MODE_EVS ) )
548 : {
549 3 : hIvasDec->st_ivas->ivas_format = MONO_FORMAT;
550 3 : move16();
551 : }
552 :
553 604 : hIvasDec->nSamplesFrame = (UWord16) Mpy_32_16_1( hDecoderConfig->output_Fs, INV_FRAME_PER_SEC_Q15 );
554 604 : hIvasDec->nSamplesAvailableNext = 0;
555 604 : hIvasDec->nSamplesRendered = 0;
556 604 : hIvasDec->tsm_scale = 100;
557 604 : hIvasDec->tsm_max_scaling = 0;
558 604 : hIvasDec->tsm_quality = ONE_IN_Q14 /*1.0f Q14*/;
559 604 : move16();
560 604 : move16();
561 604 : move16();
562 604 : move16();
563 604 : move16();
564 :
565 604 : return error;
566 : }
567 :
568 :
569 : /*---------------------------------------------------------------------*
570 : * get_render_framesize_ms( )
571 : *
572 : * Get the 5ms flag
573 : *---------------------------------------------------------------------*/
574 :
575 220 : Word16 get_render_frame_size_ms(
576 : const IVAS_RENDER_FRAMESIZE render_framesize )
577 : {
578 220 : IF( EQ_16( render_framesize, IVAS_RENDER_FRAMESIZE_5MS ) )
579 : {
580 0 : return ( 1000 / ( FRAMES_PER_SEC * IVAS_MAX_PARAM_SPATIAL_SUBFRAMES ) );
581 : }
582 220 : ELSE IF( EQ_16( render_framesize, IVAS_RENDER_FRAMESIZE_10MS ) )
583 : {
584 0 : return ( ( 1000 / ( FRAMES_PER_SEC * IVAS_MAX_PARAM_SPATIAL_SUBFRAMES ) ) << 1 );
585 : }
586 220 : ELSE IF( EQ_16( render_framesize, IVAS_RENDER_FRAMESIZE_20MS ) )
587 : {
588 220 : return ( ( 1000 / ( FRAMES_PER_SEC * IVAS_MAX_PARAM_SPATIAL_SUBFRAMES ) ) << 2 );
589 : }
590 0 : return 0;
591 : }
592 :
593 :
594 : /*---------------------------------------------------------------------*
595 : * IVAS_DEC_SetRenderFramesize( )
596 : *
597 : * Get the 5ms flag
598 : *---------------------------------------------------------------------*/
599 :
600 0 : ivas_error IVAS_DEC_SetRenderFramesize(
601 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
602 : const IVAS_RENDER_FRAMESIZE render_framesize /* i : render framesize */
603 : )
604 : {
605 0 : test();
606 0 : test();
607 0 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hDecoderConfig == NULL )
608 : {
609 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
610 : }
611 :
612 0 : hIvasDec->st_ivas->hDecoderConfig->render_framesize = render_framesize;
613 0 : move16();
614 :
615 0 : return IVAS_ERR_OK;
616 : }
617 :
618 : /*---------------------------------------------------------------------*
619 : * IVAS_DEC_GetGetRenderFramesize( )
620 : *
621 : * Get the 5ms flag
622 : *---------------------------------------------------------------------*/
623 :
624 604 : ivas_error IVAS_DEC_GetRenderFramesize(
625 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
626 : IVAS_RENDER_FRAMESIZE *render_framesize /* o : render framesize */
627 : )
628 : {
629 604 : test();
630 604 : test();
631 604 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL || render_framesize == NULL )
632 : {
633 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
634 : }
635 :
636 604 : *render_framesize = hIvasDec->st_ivas->hDecoderConfig->render_framesize;
637 604 : move16();
638 :
639 604 : return IVAS_ERR_OK;
640 : }
641 :
642 : /*---------------------------------------------------------------------*
643 : * IVAS_DEC_GetGetRenderFramesizeSamples( )
644 : *
645 : * Get render framesize in samples
646 : *---------------------------------------------------------------------*/
647 :
648 600 : ivas_error IVAS_DEC_GetRenderFramesizeSamples(
649 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
650 : Word16 *render_framesize /* o : render framesize in samples Q0 */
651 : )
652 : {
653 : Word16 tmp;
654 600 : test();
655 600 : test();
656 600 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL || render_framesize == NULL )
657 : {
658 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
659 : }
660 :
661 600 : tmp = (Word16) Mpy_32_16_1( hIvasDec->st_ivas->hDecoderConfig->output_Fs, INV_FR_P_S_MX_PRM_SPL_SBFR_Q15 );
662 :
663 600 : IF( EQ_16( hIvasDec->st_ivas->hDecoderConfig->render_framesize, IVAS_RENDER_FRAMESIZE_5MS ) )
664 : {
665 0 : *render_framesize = tmp;
666 : }
667 600 : ELSE IF( EQ_16( hIvasDec->st_ivas->hDecoderConfig->render_framesize, IVAS_RENDER_FRAMESIZE_10MS ) )
668 : {
669 0 : *render_framesize = shl( tmp, 1 );
670 : }
671 600 : ELSE IF( EQ_16( hIvasDec->st_ivas->hDecoderConfig->render_framesize, IVAS_RENDER_FRAMESIZE_20MS ) )
672 : {
673 600 : *render_framesize = shl( tmp, 2 );
674 : }
675 : ELSE
676 : {
677 0 : *render_framesize = 0;
678 : }
679 600 : move16();
680 :
681 600 : return IVAS_ERR_OK;
682 : }
683 :
684 : /*---------------------------------------------------------------------*
685 : * IVAS_DEC_GetGetRenderFramesizeMs( )
686 : *
687 : * Get render framesize in milliseconds
688 : *---------------------------------------------------------------------*/
689 :
690 27 : ivas_error IVAS_DEC_GetRenderFramesizeMs(
691 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
692 : UWord32 *render_framesize /* o : render framesize in samples Q0 */
693 : )
694 : {
695 27 : test();
696 27 : test();
697 27 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL || render_framesize == NULL )
698 : {
699 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
700 : }
701 :
702 27 : *render_framesize = get_render_frame_size_ms( hIvasDec->st_ivas->hDecoderConfig->render_framesize );
703 27 : move32();
704 :
705 27 : return IVAS_ERR_OK;
706 : }
707 :
708 : /*---------------------------------------------------------------------*
709 : * IVAS_DEC_GetGetReferencesUpdateFrequency( )
710 : *
711 : * Get update frequency of the reference vector/orientation
712 : *---------------------------------------------------------------------*/
713 :
714 600 : ivas_error IVAS_DEC_GetReferencesUpdateFrequency(
715 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
716 : Word16 *update_frequency /* o : update frequency Q0 */
717 : )
718 : {
719 600 : test();
720 600 : test();
721 600 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL || update_frequency == NULL )
722 : {
723 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
724 : }
725 :
726 600 : IF( EQ_16( hIvasDec->st_ivas->hDecoderConfig->render_framesize, IVAS_RENDER_FRAMESIZE_5MS ) )
727 : {
728 0 : *update_frequency = IVAS_MAX_PARAM_SPATIAL_SUBFRAMES;
729 0 : move16();
730 : }
731 600 : ELSE IF( EQ_16( hIvasDec->st_ivas->hDecoderConfig->render_framesize, IVAS_RENDER_FRAMESIZE_10MS ) )
732 : {
733 0 : *update_frequency = IVAS_MAX_PARAM_SPATIAL_SUBFRAMES >> 1;
734 0 : move16();
735 : }
736 600 : ELSE IF( EQ_16( hIvasDec->st_ivas->hDecoderConfig->render_framesize, IVAS_RENDER_FRAMESIZE_20MS ) )
737 : {
738 600 : *update_frequency = IVAS_MAX_PARAM_SPATIAL_SUBFRAMES >> 2;
739 600 : move16();
740 : }
741 :
742 600 : return IVAS_ERR_OK;
743 : }
744 :
745 : /*---------------------------------------------------------------------*
746 : * IVAS_DEC_GetGetNumOrientationSubframes( )
747 : *
748 : * Get the number of subframes for head/ecernal orientation per render frame
749 : *---------------------------------------------------------------------*/
750 :
751 400769 : ivas_error IVAS_DEC_GetNumOrientationSubframes(
752 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
753 : Word16 *num_subframes /* o : render framesize */
754 : )
755 : {
756 400769 : test();
757 400769 : test();
758 400769 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL || num_subframes == NULL )
759 : {
760 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
761 : }
762 :
763 400769 : *num_subframes = (Word16) hIvasDec->st_ivas->hDecoderConfig->render_framesize;
764 400769 : move16();
765 :
766 400769 : return IVAS_ERR_OK;
767 : }
768 :
769 :
770 : /*---------------------------------------------------------------------*
771 : * IVAS_DEC_EnableVoIP( )
772 : *
773 : * Intitialize JBM
774 : * jbmSafetyMargin: allowed delay reserve in addition to network jitter
775 : * to reduce late-loss, default: 60 [milliseconds]
776 : *---------------------------------------------------------------------*/
777 :
778 27 : ivas_error IVAS_DEC_EnableVoIP(
779 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
780 : const Word16 jbmSafetyMargin, /* i : allowed delay reserve for JBM, in milliseconds */
781 : const IVAS_DEC_INPUT_FORMAT inputFormat /* i : format of the input bitstream */
782 : )
783 : {
784 : DECODER_CONFIG_HANDLE hDecoderConfig;
785 : ivas_error error;
786 :
787 27 : error = IVAS_ERR_OK;
788 27 : move32();
789 :
790 27 : test();
791 27 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
792 : {
793 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
794 : }
795 :
796 27 : hDecoderConfig = hIvasDec->st_ivas->hDecoderConfig;
797 :
798 27 : hIvasDec->Opt_VOIP = 1;
799 27 : hDecoderConfig->Opt_tsm = 1;
800 27 : move16();
801 27 : move16();
802 27 : IF( NE_16( (Word16) hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_EXTERNAL ) )
803 : {
804 23 : hDecoderConfig->nchan_out = audioCfg2channels( hDecoderConfig->output_config );
805 23 : move16();
806 : }
807 :
808 27 : IF( NE_32( ( error = input_format_API_to_internal( inputFormat, &hIvasDec->bitstreamformat, &hIvasDec->sdp_hf_only, true ) ), IVAS_ERR_OK ) )
809 : {
810 0 : return error;
811 : }
812 :
813 27 : IF( ( hIvasDec->hVoIP = malloc( sizeof( IVAS_DEC_VOIP ) ) ) == NULL )
814 : {
815 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Could not allocate VoIP handle" );
816 : }
817 :
818 27 : hIvasDec->hVoIP->lastDecodedWasActive = 0;
819 27 : move16();
820 27 : hIvasDec->hVoIP->hCurrentDataUnit = NULL;
821 27 : hIvasDec->hVoIP->nSamplesFrame = (UWord16) Mpy_32_16_1( hDecoderConfig->output_Fs, INV_FRAME_PER_SEC_Q15 );
822 27 : move16();
823 : #ifdef NONBE_FIX_864_JBM_RENDER_FRAMESIZE
824 27 : hIvasDec->hVoIP->nSamplesRendered20ms = 0;
825 27 : move16();
826 : #endif
827 :
828 : #define WMC_TOOL_SKIP
829 : /* Bitstream conversion is not counted towards complexity and memory usage */
830 27 : hIvasDec->hVoIP->bs_conversion_buf = malloc( sizeof( UWord16 ) * ( MAX_BITS_PER_FRAME + 4 * 8 ) );
831 : #undef WMC_TOOL_SKIP
832 :
833 27 : IF( hIvasDec->hVoIP->bs_conversion_buf == NULL )
834 : {
835 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Could not allocate VoIP handle" );
836 : }
837 :
838 : /* initialize JBM */
839 27 : IF( NE_32( ( error = JB4_Create( &hIvasDec->hVoIP->hJBM ) != IVAS_ERR_OK ), IVAS_ERR_OK ) )
840 : {
841 0 : return error;
842 : }
843 :
844 27 : IF( JB4_Init( hIvasDec->hVoIP->hJBM, jbmSafetyMargin ) != 0 )
845 : {
846 0 : return IVAS_ERR_FAILED_ALLOC;
847 : }
848 :
849 27 : return error;
850 : }
851 :
852 :
853 : /*---------------------------------------------------------------------*
854 : * IVAS_DEC_FeedFrame_Serial( )
855 : *
856 : *
857 : *---------------------------------------------------------------------*/
858 :
859 399985 : ivas_error IVAS_DEC_FeedFrame_Serial(
860 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
861 : UWord16 *serial, /* i : buffer containing serial input bitstream. Each bit should be stored as a single uint16_t value */
862 : const UWord16 num_bits, /* i : number of bits in input bitstream */
863 : Word16 bfi /* i : bad frame indicator flag */
864 : )
865 : {
866 : ivas_error error;
867 :
868 399985 : IF( !hIvasDec->isInitialized )
869 : {
870 : /* Once first frame is fed, finish initialization in EVS Mono.
871 : * In IVAS mode, initialization is done in ivas_dec(). */
872 600 : IF( EQ_16( hIvasDec->mode, IVAS_DEC_MODE_EVS ) )
873 : {
874 3 : IF( NE_32( ( error = ivas_init_decoder_fx( hIvasDec->st_ivas ) ), IVAS_ERR_OK ) )
875 : {
876 0 : return error;
877 : }
878 3 : Word32 ivas_total_brate = hIvasDec->st_ivas->hDecoderConfig->ivas_total_brate;
879 3 : move32();
880 3 : test();
881 3 : IF( ( EQ_16( hIvasDec->st_ivas->ivas_format, MC_FORMAT ) ) && ( EQ_16( hIvasDec->st_ivas->mc_mode, MC_MODE_PARAMMC ) ) )
882 : {
883 : MC_LS_SETUP mc_ls_setup;
884 0 : mc_ls_setup = ivas_mc_map_output_config_to_mc_ls_setup_fx( hIvasDec->st_ivas->transport_config );
885 0 : hIvasDec->st_ivas->nchan_transport = ivas_param_mc_getNumTransportChannels_fx( ivas_total_brate, mc_ls_setup );
886 : }
887 :
888 3 : test();
889 3 : IF( hIvasDec->hVoIP != NULL && hIvasDec->hVoIP->hCurrentDataUnit != NULL )
890 0 : {
891 0 : DEC_CORE_HANDLE st = hIvasDec->st_ivas->hSCE[0]->hCoreCoder[0];
892 0 : st->ini_frame = 0;
893 0 : st->prev_use_partial_copy = 0;
894 0 : move16();
895 0 : move16();
896 0 : hIvasDec->st_ivas->hDecoderConfig->ivas_total_brate = imult3216( hIvasDec->hVoIP->hCurrentDataUnit->dataSize, FRAMES_PER_SEC );
897 0 : move32();
898 : }
899 : ELSE
900 : {
901 3 : hIvasDec->st_ivas->hDecoderConfig->ivas_total_brate = ACELP_8k00;
902 3 : move32();
903 : }
904 3 : hIvasDec->isInitialized = true;
905 3 : move16();
906 : }
907 : }
908 :
909 399985 : if ( !bfi ) /* TODO(mcjbm): Is this ok for bfi == 2 (partial frame)? Is there enough info to fully configure decoder? */
910 : {
911 390021 : hIvasDec->hasBeenFedFirstGoodFrame = true;
912 390021 : move16();
913 : }
914 :
915 : /* Update redundant frame information in EVS (pre- read indices) */
916 399985 : test();
917 399985 : test();
918 399985 : IF( EQ_16( (Word16) hIvasDec->mode, IVAS_DEC_MODE_EVS ) && hIvasDec->hVoIP != NULL && hIvasDec->hVoIP->hCurrentDataUnit != NULL )
919 : {
920 0 : DEC_CORE_HANDLE st = hIvasDec->st_ivas->hSCE[0]->hCoreCoder[0];
921 0 : st->bit_stream = serial;
922 :
923 0 : test();
924 0 : IF( hIvasDec->hVoIP->hCurrentDataUnit->partial_frame || st->prev_use_partial_copy )
925 : {
926 0 : st->next_coder_type = hIvasDec->hVoIP->hCurrentDataUnit->nextCoderType;
927 : }
928 : ELSE
929 : {
930 0 : st->next_coder_type = INACTIVE;
931 : }
932 0 : move16();
933 :
934 0 : test();
935 0 : if ( EQ_16( (Word16) hIvasDec->hVoIP->hCurrentDataUnit->partial_frame, 1 ) && NE_16( bfi, 1 ) )
936 : {
937 0 : bfi = 2;
938 0 : move16();
939 : }
940 : }
941 :
942 399985 : IF( NE_32( ( error = read_indices_fx( hIvasDec->st_ivas, serial, num_bits, &hIvasDec->prev_ft_speech, &hIvasDec->CNG, bfi ) ), IVAS_ERR_OK ) )
943 : {
944 0 : return error;
945 : }
946 :
947 : /* Update redundant frame information in EVS (post- read indices) */
948 399985 : test();
949 399985 : test();
950 399985 : test();
951 399985 : IF( EQ_16( (Word16) hIvasDec->mode, IVAS_DEC_MODE_EVS ) &&
952 : hIvasDec->hVoIP != NULL &&
953 : hIvasDec->hVoIP->hCurrentDataUnit != NULL &&
954 : ( (Word16) hIvasDec->hVoIP->hCurrentDataUnit->partial_frame != 0 ) )
955 : {
956 0 : DEC_CORE_HANDLE st = hIvasDec->st_ivas->hSCE[0]->hCoreCoder[0];
957 0 : st->codec_mode = MODE2;
958 0 : st->use_partial_copy = 1;
959 0 : move16();
960 0 : move16();
961 : }
962 :
963 399985 : hIvasDec->needNewFrame = false;
964 399985 : hIvasDec->hasBeenFedFrame = true;
965 399985 : hIvasDec->nSamplesRendered = 0;
966 399985 : hIvasDec->nSamplesAvailableNext = hIvasDec->nSamplesFrame;
967 399985 : move16();
968 399985 : move16();
969 399985 : move16();
970 399985 : move16();
971 :
972 399985 : return IVAS_ERR_OK;
973 : }
974 :
975 :
976 : /*---------------------------------------------------------------------*
977 : * IVAS_DEC_GetSamples( )
978 : *
979 : * Main function to decode to PCM data
980 : *---------------------------------------------------------------------*/
981 :
982 :
983 411975 : ivas_error IVAS_DEC_GetSamples(
984 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
985 : const Word16 nSamplesAsked, /* i : number of samples wanted by the caller */
986 : Word16 *pcmBuf, /* i/o: buffer for decoded PCM output. The memory must already be allocated and be able to hold the expected number of output samples, based on frame size and number of output channels Q0 */
987 : Word16 *nOutSamples, /* o : number of samples per channel written to output buffer */
988 : bool *needNewFrame /* o :indication that the decoder needs a new frame */
989 : )
990 : {
991 : ivas_error error;
992 : Word16 nOutSamplesElse, nSamplesToRender;
993 : UWord16 nSamplesRendered, nSamplesRendered_loop, l_ts, nTimeScalerOutSamples;
994 : UWord8 nTransportChannels, nOutChannels;
995 :
996 411975 : nSamplesRendered = 0;
997 411975 : nOutChannels = 0;
998 411975 : nSamplesRendered_loop = 0;
999 411975 : l_ts = 0;
1000 411975 : nTransportChannels = 0;
1001 411975 : move16();
1002 411975 : move16();
1003 411975 : move16();
1004 411975 : move16();
1005 411975 : move16();
1006 :
1007 411975 : test();
1008 411975 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
1009 : {
1010 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1011 : }
1012 :
1013 411975 : IF( hIvasDec->updateOrientation )
1014 : {
1015 : /*----------------------------------------------------------------*
1016 : * Combine orientations
1017 : *----------------------------------------------------------------*/
1018 :
1019 69350 : IF( NE_32( ( error = combine_external_and_head_orientations_dec( hIvasDec->st_ivas->hHeadTrackData, hIvasDec->st_ivas->hExtOrientationData, hIvasDec->st_ivas->hCombinedOrientationData ) ), IVAS_ERR_OK ) )
1020 : {
1021 0 : return error;
1022 : }
1023 69350 : hIvasDec->updateOrientation = false;
1024 69350 : move16();
1025 : }
1026 :
1027 411975 : test();
1028 411975 : IF( !hIvasDec->hasBeenFedFrame && hIvasDec->nSamplesAvailableNext == 0 )
1029 : {
1030 : /* no frame was fed, do nothing but ask for a frame */
1031 0 : *needNewFrame = true;
1032 0 : *nOutSamples = 0;
1033 0 : hIvasDec->needNewFrame = true;
1034 0 : move16();
1035 0 : move16();
1036 0 : move16();
1037 0 : return IVAS_ERR_OK;
1038 : }
1039 :
1040 411975 : test();
1041 : /* check if we are still at the beginning with bad frames, put out zeroes, keep track of subframes */
1042 411975 : IF( !hIvasDec->isInitialized && hIvasDec->st_ivas->bfi )
1043 : {
1044 0 : hIvasDec->hasBeenFedFrame = false;
1045 0 : move16();
1046 0 : set16_fx( pcmBuf, 0, imult1616( hIvasDec->st_ivas->hDecoderConfig->nchan_out, nSamplesAsked ) );
1047 0 : hIvasDec->nSamplesRendered = add( hIvasDec->nSamplesRendered, nSamplesAsked );
1048 0 : move16();
1049 0 : *nOutSamples = nSamplesAsked;
1050 0 : move16();
1051 0 : hIvasDec->nSamplesAvailableNext = sub( hIvasDec->nSamplesAvailableNext, nSamplesAsked );
1052 0 : move16();
1053 0 : IF( hIvasDec->nSamplesAvailableNext == 0 )
1054 : {
1055 0 : hIvasDec->needNewFrame = true;
1056 0 : *needNewFrame = true;
1057 0 : move16();
1058 0 : move16();
1059 : }
1060 : }
1061 : ELSE
1062 : {
1063 : /* check if we need to run the setup function */
1064 411975 : test();
1065 411975 : IF( !hIvasDec->isInitialized || hIvasDec->hasBeenFedFrame ){
1066 : /* setup */
1067 :
1068 399985 : IF( NE_32( ( error = IVAS_DEC_Setup( hIvasDec, &l_ts, &nTransportChannels, &nOutChannels, &nSamplesRendered_loop, pcmBuf + imult3216( nSamplesRendered, nOutChannels ) ) ), IVAS_ERR_OK ) ){
1069 6 : return error;
1070 : }
1071 : }
1072 : {
1073 : /* check if we need to run the setup function, tc decoding and feeding the renderer */
1074 411969 : test();
1075 411969 : IF( !hIvasDec->isInitialized || hIvasDec->hasBeenFedFrame )
1076 : {
1077 : Word16 nResidualSamples, nSamplesTcsScaled;
1078 399979 : nSamplesRendered = add( nSamplesRendered, nSamplesRendered_loop );
1079 :
1080 399979 : test();
1081 399979 : IF( hIvasDec->st_ivas->hDecoderConfig->Opt_tsm && NE_16( (Word16) nTransportChannels, hIvasDec->nTransportChannelsOld ) )
1082 : {
1083 953 : IF( NE_32( ( error = IVAS_DEC_VoIP_reconfigure( hIvasDec, nTransportChannels, l_ts ) ), IVAS_ERR_OK ) )
1084 : {
1085 0 : return error;
1086 : }
1087 : }
1088 :
1089 : /* IVAS decoder */
1090 399979 : IF( NE_32( ( error = IVAS_DEC_GetTcSamples( hIvasDec, hIvasDec->apaExecBuffer_fx, &nOutSamplesElse ) ), IVAS_ERR_OK ) )
1091 : {
1092 0 : return error;
1093 : }
1094 :
1095 : /* JBM */
1096 399979 : IF( hIvasDec->st_ivas->hDecoderConfig->Opt_tsm )
1097 : {
1098 19405 : IF( apa_set_scale_fx( hIvasDec->hTimeScaler, hIvasDec->tsm_scale ) != 0 )
1099 : {
1100 0 : return IVAS_ERR_UNKNOWN;
1101 : }
1102 :
1103 : // tmp apaExecBuffer
1104 19405 : IF( EQ_16( (Word16) hIvasDec->mode, IVAS_DEC_MODE_EVS ) )
1105 : {
1106 : Word16 tmp_apaExecBuffer[APA_BUF];
1107 0 : FOR( Word32 i = 0; i < APA_BUF_PER_CHANNEL * nTransportChannels; ++i )
1108 : {
1109 0 : tmp_apaExecBuffer[i] = extract_l( L_shr( hIvasDec->apaExecBuffer_fx[i], Q11 ) ); // Q0
1110 : }
1111 0 : IF( apa_exec_fx( hIvasDec->hTimeScaler, tmp_apaExecBuffer, (UWord16) imult3216( hIvasDec->nSamplesFrame, nTransportChannels ), (UWord16) hIvasDec->tsm_max_scaling, tmp_apaExecBuffer, &nTimeScalerOutSamples ) != 0 )
1112 : {
1113 0 : return IVAS_ERR_UNKNOWN;
1114 : }
1115 0 : FOR( Word32 i = 0; i < APA_BUF_PER_CHANNEL * nTransportChannels; ++i )
1116 : {
1117 0 : hIvasDec->apaExecBuffer_fx[i] = L_shl( tmp_apaExecBuffer[i], Q11 ); // Q11
1118 : }
1119 : }
1120 : ELSE
1121 : {
1122 19405 : IF( apa_exec_ivas_fx( hIvasDec->hTimeScaler, hIvasDec->apaExecBuffer_fx, (UWord16) imult3216( hIvasDec->nSamplesFrame, nTransportChannels ), (UWord16) hIvasDec->tsm_max_scaling, hIvasDec->apaExecBuffer_fx, &nTimeScalerOutSamples ) != 0 )
1123 : {
1124 0 : return IVAS_ERR_UNKNOWN;
1125 : }
1126 : }
1127 19405 : assert( LE_32( (Word32) nTimeScalerOutSamples, APA_BUF ) );
1128 19405 : nSamplesTcsScaled = idiv1616U( extract_l( nTimeScalerOutSamples ), nTransportChannels );
1129 : #ifdef NONBE_FIX_864_JBM_RENDER_FRAMESIZE
1130 19405 : hIvasDec->timeScalingDone = 1;
1131 19405 : move16();
1132 : #endif
1133 : }
1134 : ELSE
1135 : {
1136 380574 : nSamplesTcsScaled = hIvasDec->nSamplesFrame;
1137 380574 : move16();
1138 : }
1139 : #ifdef DEBUG_MODE_JBM
1140 : dbgwrite( &nTimeScalerOutSamples, sizeof( uint16_t ), 1, 1, "./res/JBM_nTimeScaleOutSamples.dat" );
1141 : #endif
1142 :
1143 : /* Feed decoded transport channels samples to the renderer */
1144 399979 : IF( NE_32( ( error = IVAS_DEC_RendererFeedTcSamples( hIvasDec, nSamplesTcsScaled, &nResidualSamples, hIvasDec->apaExecBuffer_fx ) ), IVAS_ERR_OK ) )
1145 : {
1146 0 : return error;
1147 : }
1148 : #ifdef DEBUG_MODE_JBM
1149 : dbgwrite( &nResidualSamples, sizeof( int16_t ), 1, 1, "./res/JBM_nResidualSamples.dat" );
1150 : #endif
1151 :
1152 :
1153 399979 : IF( hIvasDec->st_ivas->hDecoderConfig->Opt_tsm )
1154 : {
1155 : /* feed residual samples to TSM for the next call */
1156 19405 : IF( apa_set_renderer_residual_samples( hIvasDec->hTimeScaler, (UWord16) nResidualSamples ) != 0 )
1157 : {
1158 0 : return IVAS_ERR_UNKNOWN;
1159 : }
1160 : }
1161 399979 : hIvasDec->hasBeenFedFrame = false;
1162 399979 : move16();
1163 : }
1164 :
1165 : /* render IVAS frames directly to the output buffer */
1166 411969 : nSamplesToRender = sub( nSamplesAsked, nSamplesRendered );
1167 411969 : IF( NE_32( ( error = IVAS_DEC_GetRenderedSamples( hIvasDec, nSamplesToRender, &nSamplesRendered_loop, &hIvasDec->nSamplesAvailableNext, pcmBuf + imult3216( nSamplesRendered, nOutChannels ) ) ), IVAS_ERR_OK ) )
1168 : {
1169 0 : return error;
1170 : }
1171 :
1172 411969 : nSamplesRendered = add( nSamplesRendered, nSamplesRendered_loop );
1173 411969 : nSamplesToRender = sub( nSamplesToRender, nSamplesRendered_loop );
1174 411969 : IF( hIvasDec->nSamplesAvailableNext == 0 )
1175 : {
1176 399959 : *needNewFrame = true;
1177 399959 : hIvasDec->needNewFrame = true;
1178 399959 : move16();
1179 399959 : move16();
1180 : }
1181 : ELSE
1182 : {
1183 12010 : *needNewFrame = false;
1184 12010 : move16();
1185 : }
1186 : }
1187 : }
1188 :
1189 411969 : *nOutSamples = nSamplesRendered;
1190 411969 : move16();
1191 :
1192 411969 : return IVAS_ERR_OK;
1193 : }
1194 :
1195 :
1196 : /*---------------------------------------------------------------------*
1197 : * IVAS_DEC_Setup( )
1198 : *
1199 : *
1200 : *---------------------------------------------------------------------*/
1201 :
1202 399985 : static ivas_error IVAS_DEC_Setup(
1203 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
1204 : UWord16 *nTcBufferGranularity, /* o : granularity of the TC Buffer */
1205 : UWord8 *nTransportChannels, /* o : number of decoded transport PCM channels */
1206 : UWord8 *nOutChannels, /* o : number of decoded out channels (PCM or CLDFB) */
1207 : UWord16 *nSamplesRendered, /* o : number of samples flushed from the last frame */
1208 : Word16 *data /* o : output synthesis signal Q0 */
1209 : )
1210 : {
1211 : ivas_error error;
1212 :
1213 399985 : *nSamplesRendered = 0;
1214 399985 : move16();
1215 :
1216 399985 : IF( EQ_16( (Word16) hIvasDec->mode, IVAS_DEC_MODE_EVS ) )
1217 : {
1218 3100 : IF( EQ_16( (Word16) hIvasDec->st_ivas->renderer_type, RENDERER_NON_DIEGETIC_DOWNMIX ) )
1219 : {
1220 1000 : *nTransportChannels = MAX_OUTPUT_CHANNELS_IN_DIEGETIC_PAN;
1221 1000 : *nOutChannels = MAX_OUTPUT_CHANNELS_IN_DIEGETIC_PAN;
1222 : }
1223 : ELSE
1224 : {
1225 2100 : *nTransportChannels = 1;
1226 2100 : *nOutChannels = 1;
1227 : }
1228 3100 : move16();
1229 3100 : move16();
1230 : }
1231 : ELSE
1232 : {
1233 : Decoder_Struct *st_ivas;
1234 :
1235 396885 : st_ivas = hIvasDec->st_ivas;
1236 :
1237 : /*----------------------------------------------------------------*
1238 : * IVAS decoder setup
1239 : * - read IVAS format signaling
1240 : * - read IVAS format specific signaling
1241 : * - initialize decoder in the first frame based on IVAS format and number of transport channels
1242 : * - reconfigure the decoder when the number of TC or IVAS total bitrate change
1243 : *----------------------------------------------------------------*/
1244 :
1245 396885 : IF( st_ivas->bfi == 0 )
1246 : {
1247 : Word32 ivas_total_brate;
1248 388320 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
1249 : Word16 SrcInd[MAX_NUM_TDREND_CHANNELS];
1250 388320 : Word16 num_src = 0;
1251 388320 : move16();
1252 :
1253 388320 : test();
1254 388320 : test();
1255 388320 : test();
1256 388320 : test();
1257 388320 : test();
1258 388320 : test();
1259 388320 : test();
1260 388320 : test();
1261 388320 : test();
1262 388320 : IF( is_DTXrate( ivas_total_brate ) == 0 && ( !( st_ivas->ini_active_frame == 0 && NE_32( ivas_total_brate, FRAME_NO_DATA ) && LT_32( ivas_total_brate, MASA_STEREO_MIN_BITRATE ) && EQ_16( st_ivas->nCPE, 1 ) ) ) && st_ivas->bit_stream[( ivas_total_brate / FRAMES_PER_SEC ) - 1] && st_ivas->ivas_format == MASA_FORMAT && st_ivas->last_ivas_format != MASA_FORMAT && ( ( GT_32( ivas_total_brate, IVAS_SID_5k2 ) && NE_32( ivas_total_brate, st_ivas->hDecoderConfig->last_ivas_total_brate ) ) && st_ivas->ini_active_frame == 0 ) )
1263 : {
1264 0 : IF( st_ivas->hSpar )
1265 : {
1266 0 : Word16 Q_tmp = getScaleFactor16( st_ivas->hSpar->hFbMixer->cldfb_cross_fade_fx, 16 );
1267 0 : Scale_sig( st_ivas->hSpar->hFbMixer->cldfb_cross_fade_fx, 16, sub( Q_tmp, st_ivas->hSpar->hFbMixer->cldfb_cross_fade_q ) );
1268 0 : st_ivas->hSpar->hFbMixer->cldfb_cross_fade_q = Q_tmp;
1269 : }
1270 : }
1271 :
1272 388320 : test();
1273 388320 : test();
1274 388320 : test();
1275 388320 : test();
1276 388320 : test();
1277 388320 : IF( is_DTXrate( ivas_total_brate ) == 0 && EQ_16( (Word16) st_ivas->ivas_format, MASA_ISM_FORMAT ) && GT_16( st_ivas->ini_frame, 0 ) && ( GT_32( ivas_total_brate, IVAS_SID_5k2 ) && NE_32( ivas_total_brate, st_ivas->hDecoderConfig->last_ivas_total_brate ) ) && st_ivas->ini_active_frame == 0 )
1278 : {
1279 0 : IF( st_ivas->hSpar )
1280 : {
1281 0 : Word16 Q_tmp = getScaleFactor16( st_ivas->hSpar->hFbMixer->cldfb_cross_fade_fx, 16 );
1282 0 : Scale_sig( st_ivas->hSpar->hFbMixer->cldfb_cross_fade_fx, 16, sub( Q_tmp, st_ivas->hSpar->hFbMixer->cldfb_cross_fade_q ) );
1283 0 : st_ivas->hSpar->hFbMixer->cldfb_cross_fade_q = Q_tmp;
1284 0 : move16();
1285 : }
1286 : }
1287 :
1288 388320 : IF( NE_32( ( error = ivas_dec_setup( st_ivas, nSamplesRendered, data ) ), IVAS_ERR_OK ) )
1289 : {
1290 6 : return error;
1291 : }
1292 388314 : test();
1293 388314 : IF( ( st_ivas->ivas_format == MC_FORMAT ) && ( st_ivas->mc_mode == MC_MODE_PARAMMC ) )
1294 : {
1295 : MC_LS_SETUP mc_ls_setup;
1296 8750 : mc_ls_setup = ivas_mc_map_output_config_to_mc_ls_setup_fx( st_ivas->transport_config );
1297 8750 : st_ivas->nchan_transport = ivas_param_mc_getNumTransportChannels_fx( ivas_total_brate, mc_ls_setup );
1298 : }
1299 :
1300 388314 : test();
1301 388314 : test();
1302 388314 : test();
1303 388314 : test();
1304 388314 : test();
1305 388314 : test();
1306 388314 : IF( is_DTXrate( ivas_total_brate ) == 0 && ( !( st_ivas->ini_active_frame == 0 && NE_32( ivas_total_brate, FRAME_NO_DATA ) && LT_32( ivas_total_brate, MASA_STEREO_MIN_BITRATE ) && EQ_16( st_ivas->nCPE, 1 ) ) ) && st_ivas->bit_stream[( ivas_total_brate / FRAMES_PER_SEC ) - 1] && st_ivas->ivas_format == MASA_FORMAT && ( ( GT_32( ivas_total_brate, IVAS_SID_5k2 ) && NE_32( ivas_total_brate, st_ivas->hDecoderConfig->last_ivas_total_brate ) ) || st_ivas->ini_active_frame == 0 ) )
1307 : {
1308 1047 : IF( EQ_16( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
1309 : {
1310 0 : Word16 nchan_rend = num_src;
1311 0 : IF( EQ_16( st_ivas->ivas_format, MC_FORMAT ) && NE_16( st_ivas->transport_config, IVAS_AUDIO_CONFIG_LS_CUSTOM ) )
1312 : {
1313 0 : nchan_rend--; /* Skip LFE channel -- added to the others */
1314 : }
1315 0 : FOR( Word16 nS = 0; nS < nchan_rend; nS++ )
1316 : {
1317 0 : TDREND_SRC_t *Src_p = st_ivas->hBinRendererTd->Sources[SrcInd[nS]];
1318 0 : IF( Src_p->SrcSpatial_p != NULL )
1319 : {
1320 0 : Src_p->SrcSpatial_p->q_Pos_p = Q31;
1321 0 : move16();
1322 : }
1323 0 : TDREND_SRC_SPATIAL_t *SrcSpatial_p = st_ivas->hBinRendererTd->Sources[nS]->SrcSpatial_p;
1324 0 : SrcSpatial_p->q_Pos_p = Q31;
1325 0 : move16();
1326 : }
1327 : }
1328 : }
1329 :
1330 :
1331 388314 : test();
1332 388314 : test();
1333 388314 : test();
1334 388314 : test();
1335 388314 : test();
1336 388314 : IF( is_DTXrate( ivas_total_brate ) == 0 && EQ_16( (Word16) st_ivas->ivas_format, MASA_ISM_FORMAT ) && st_ivas->ini_frame > 0 && ( GT_32( ivas_total_brate, IVAS_SID_5k2 ) && NE_32( ivas_total_brate, st_ivas->hDecoderConfig->last_ivas_total_brate ) ) && st_ivas->ini_active_frame == 0 )
1337 : {
1338 0 : IF( EQ_16( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
1339 : {
1340 0 : Word16 nchan_rend = num_src;
1341 0 : test();
1342 0 : IF( EQ_16( st_ivas->ivas_format, MC_FORMAT ) && NE_16( st_ivas->transport_config, IVAS_AUDIO_CONFIG_LS_CUSTOM ) )
1343 : {
1344 0 : nchan_rend--; /* Skip LFE channel -- added to the others */
1345 : }
1346 0 : FOR( Word16 nS = 0; nS < nchan_rend; nS++ )
1347 : {
1348 0 : TDREND_SRC_t *Src_p = st_ivas->hBinRendererTd->Sources[SrcInd[nS]];
1349 0 : IF( Src_p->SrcSpatial_p != NULL )
1350 : {
1351 0 : Src_p->SrcSpatial_p->q_Pos_p = Q31;
1352 0 : move16();
1353 : }
1354 0 : TDREND_SRC_SPATIAL_t *SrcSpatial_p = st_ivas->hBinRendererTd->Sources[nS]->SrcSpatial_p;
1355 0 : SrcSpatial_p->q_Pos_p = Q31;
1356 0 : move16();
1357 : }
1358 : }
1359 : }
1360 : }
1361 :
1362 396879 : *nTransportChannels = (UWord8) st_ivas->hTcBuffer->nchan_transport_jbm;
1363 396879 : *nTcBufferGranularity = (UWord16) st_ivas->hTcBuffer->n_samples_granularity;
1364 396879 : *nOutChannels = (UWord8) st_ivas->hDecoderConfig->nchan_out;
1365 396879 : move16();
1366 396879 : move16();
1367 396879 : move16();
1368 : }
1369 :
1370 399979 : return IVAS_ERR_OK;
1371 : }
1372 :
1373 :
1374 : /*---------------------------------------------------------------------*
1375 : * IVAS_DEC_GetTcSamples( )
1376 : *
1377 : * Main function to decode to PCM data of the transport channels
1378 : *---------------------------------------------------------------------*/
1379 :
1380 399979 : static ivas_error IVAS_DEC_GetTcSamples(
1381 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
1382 : Word32 *pcmBuf_fx, /* i/o: buffer for decoded PCM output. The memory must already be allocated and be able to hold the expected number of output samples, based on frame size and number of output channels Q11 */
1383 : Word16 *nOutSamples /* o : number of samples per channel written to output buffer */
1384 : )
1385 : {
1386 : Decoder_Struct *st_ivas;
1387 : ivas_error error;
1388 :
1389 :
1390 399979 : test();
1391 399979 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
1392 : {
1393 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1394 : }
1395 :
1396 399979 : st_ivas = hIvasDec->st_ivas;
1397 :
1398 399979 : *nOutSamples = (Word16) Mpy_32_16_1( st_ivas->hDecoderConfig->output_Fs, INV_FRAME_PER_SEC_Q15 );
1399 399979 : move16();
1400 :
1401 399979 : IF( EQ_16( (Word16) hIvasDec->mode, IVAS_DEC_MODE_EVS ) )
1402 : {
1403 3100 : IF( NE_32( ( error = evs_dec_main_fx( st_ivas, *nOutSamples, pcmBuf_fx, NULL ) ), IVAS_ERR_OK ) )
1404 : {
1405 0 : return error;
1406 : }
1407 : }
1408 396879 : ELSE IF( EQ_16( (Word16) hIvasDec->mode, IVAS_DEC_MODE_IVAS ) )
1409 : {
1410 : /* run the main IVAS decoding routine */
1411 : /*------------------------flt 2 fix----------------------*/
1412 : Word16 n;
1413 : Decoder_State *st, **sts; /* used for bitstream handling */
1414 : Word16 nCPE, cpe_id;
1415 396879 : nCPE = st_ivas->nCPE;
1416 :
1417 396879 : IF( st_ivas->hDecoderConfig->Opt_tsm == 0 )
1418 : {
1419 2735838 : FOR( n = 0; n < ivas_get_nchan_buffers_dec_ivas_fx( st_ivas, st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate ); n++ )
1420 : {
1421 2358364 : set32_fx( st_ivas->p_output_fx[n], 0, L_FRAME48k );
1422 2358364 : st_ivas->hTcBuffer->tc_fx[n] = st_ivas->p_output_fx[n]; // Q11
1423 : }
1424 : }
1425 :
1426 :
1427 : CPE_DEC_HANDLE hCPE;
1428 396879 : test();
1429 396879 : test();
1430 396879 : test();
1431 396879 : test();
1432 396879 : test();
1433 396879 : IF( ( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) || EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) || EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) || ( EQ_32( st_ivas->ivas_format, SBA_FORMAT ) || EQ_32( st_ivas->ivas_format, MASA_FORMAT ) ) || EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) ) && ( GT_16( st_ivas->nCPE, 1 ) ) )
1434 : {
1435 :
1436 93116 : nCPE = st_ivas->nCPE;
1437 93116 : move16();
1438 334254 : FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
1439 : {
1440 241138 : hCPE = st_ivas->hCPE[cpe_id];
1441 241138 : sts = hCPE->hCoreCoder;
1442 :
1443 :
1444 723414 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1445 : {
1446 482276 : st = hCPE->hCoreCoder[n];
1447 482276 : IF( st->hTcxDec )
1448 482276 : st->hTcxDec->conNoiseLevelIndex = st->hTcxDec->NoiseLevelIndex_bfi;
1449 482276 : IF( st->hTcxDec )
1450 482276 : st->hTcxDec->conCurrLevelIndex = st->hTcxDec->CurrLevelIndex_bfi;
1451 : }
1452 : }
1453 : }
1454 :
1455 : /* Function call: ivas_jbm_dec_tc function */
1456 396879 : IF( NE_32( ( error = ivas_jbm_dec_tc_fx( st_ivas, pcmBuf_fx ) ), IVAS_ERR_OK ) )
1457 : {
1458 0 : return error;
1459 : }
1460 :
1461 396879 : hIvasDec->isInitialized = true; /* Initialization done in ivas_dec() */
1462 :
1463 396879 : test();
1464 396879 : test();
1465 396879 : test();
1466 396879 : test();
1467 396879 : test();
1468 396879 : test();
1469 396879 : IF( ( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) || EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) || EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) || ( EQ_32( st_ivas->ivas_format, SBA_FORMAT ) || EQ_32( st_ivas->ivas_format, MASA_FORMAT ) ) || EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) ) && ( GT_16( st_ivas->nCPE, 1 ) ) )
1470 : {
1471 334254 : FOR( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
1472 : {
1473 241138 : hCPE = st_ivas->hCPE[cpe_id];
1474 241138 : sts = hCPE->hCoreCoder;
1475 723414 : FOR( n = 0; n < 2; n++ )
1476 : {
1477 :
1478 : /*-------------------cldfb-start-------------------------*/
1479 : /*note : cldfb_size here signifies the original size which was assigned to cldfb_state_fx buffer not its current size*/
1480 482276 : IF( sts[n]->cldfbAna != NULL )
1481 : {
1482 120 : scale_sig32( sts[n]->cldfbAna->cldfb_state_fx, sts[n]->cldfbAna->cldfb_size, sub( Q11, Q10 ) ); // Q11
1483 120 : sts[n]->cldfbAna->Q_cldfb_state = Q11;
1484 120 : move16();
1485 : }
1486 482276 : IF( sts[n]->cldfbSyn != NULL )
1487 : {
1488 482276 : scale_sig32( sts[n]->cldfbSyn->cldfb_state_fx, sts[n]->cldfbSyn->cldfb_size, sub( Q11, Q4 ) ); // Q11
1489 482276 : sts[n]->cldfbSyn->Q_cldfb_state = Q11;
1490 482276 : move16();
1491 : }
1492 : /*-------------------cldfb-end---------------------------*/
1493 : }
1494 : }
1495 : }
1496 : }
1497 399979 : if ( hIvasDec->hasBeenFedFirstGoodFrame )
1498 : {
1499 399979 : hIvasDec->hasDecodedFirstGoodFrame = true;
1500 399979 : move16();
1501 : }
1502 :
1503 399979 : return IVAS_ERR_OK;
1504 : }
1505 :
1506 :
1507 : /*---------------------------------------------------------------------*
1508 : * IVAS_DEC_Rendered_FeedTcSamples( )
1509 : *
1510 : * Feed decoded transport channels samples to the renderer
1511 : *---------------------------------------------------------------------*/
1512 :
1513 399979 : static ivas_error IVAS_DEC_RendererFeedTcSamples(
1514 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
1515 : const Word16 nSamplesForRendering, /* i : number of TC samples wanted from the renderer */
1516 : Word16 *nSamplesResidual, /* o : number of samples not fitting into the renderer grid and buffer for the next call */
1517 : Word32 *pcmBuf /* i/o: buffer for decoded PCM output. The memory must already be allocated and be able to hold the expected number of output samples, based on frame size and number of output channels Q11*/
1518 : )
1519 : {
1520 : Decoder_Struct *st_ivas;
1521 :
1522 399979 : test();
1523 399979 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
1524 : {
1525 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1526 : }
1527 :
1528 399979 : st_ivas = hIvasDec->st_ivas;
1529 :
1530 : /* feed the TCs to the IVAS renderer */
1531 399979 : ivas_jbm_dec_feed_tc_to_renderer_fx( st_ivas, nSamplesForRendering, nSamplesResidual, pcmBuf );
1532 :
1533 399979 : return IVAS_ERR_OK;
1534 : }
1535 :
1536 :
1537 : /*---------------------------------------------------------------------*
1538 : * IVAS_DEC_GetRenderedSamples( )
1539 : *
1540 : * Main function to render the transport channels to PCM output data
1541 : *---------------------------------------------------------------------*/
1542 :
1543 411989 : static ivas_error IVAS_DEC_GetRenderedSamples(
1544 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
1545 : const UWord16 nSamplesForRendering, /* i : number of TC samples wanted from the renderer */
1546 : UWord16 *nSamplesRendered, /* o : number of samples rendered */
1547 : UWord16 *nSamplesAvailableNext, /* o : number of samples still available in the renerer pipeline */
1548 : Word16 *pcmBuf // Q0
1549 : )
1550 : {
1551 : Decoder_Struct *st_ivas;
1552 : ivas_error error;
1553 :
1554 411989 : test();
1555 411989 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
1556 : {
1557 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1558 : }
1559 :
1560 411989 : st_ivas = hIvasDec->st_ivas;
1561 :
1562 : /* run the main IVAS decoding routine */
1563 411989 : error = ivas_jbm_dec_render_fx( st_ivas, nSamplesForRendering, nSamplesRendered, nSamplesAvailableNext, pcmBuf );
1564 :
1565 411989 : return error;
1566 : }
1567 :
1568 :
1569 : /*---------------------------------------------------------------------*
1570 : * IVAS_DEC_GetBufferedNumberOfSamples( )
1571 : *
1572 : * Returns the number of objects available in the decoded bitstream
1573 : *---------------------------------------------------------------------*/
1574 :
1575 19378 : static ivas_error IVAS_DEC_GetBufferedNumberOfSamples(
1576 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
1577 : Word16 *nSamplesBuffered /* o : number of samples still buffered */
1578 : )
1579 : {
1580 19378 : *nSamplesBuffered = 0;
1581 19378 : move16();
1582 19378 : test();
1583 19378 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
1584 : {
1585 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1586 : }
1587 :
1588 : /* check if the TC buffer already exists, otherweise nothing is buffered anyway */
1589 19378 : if ( hIvasDec->st_ivas->hTcBuffer != NULL )
1590 : {
1591 19378 : *nSamplesBuffered = sub( hIvasDec->st_ivas->hTcBuffer->n_samples_buffered, hIvasDec->st_ivas->hTcBuffer->n_samples_rendered );
1592 : #ifdef NONBE_FIX_864_JBM_RENDER_FRAMESIZE
1593 19378 : *nSamplesBuffered = add( *nSamplesBuffered, hIvasDec->hVoIP->nSamplesRendered20ms );
1594 : #endif
1595 19378 : move16();
1596 : }
1597 :
1598 19378 : return IVAS_ERR_OK;
1599 : }
1600 :
1601 :
1602 : /*---------------------------------------------------------------------*
1603 : * IVAS_DEC_GetNumObjects( )
1604 : *
1605 : * Returns the number of objects available in the decoded bitstream
1606 : *---------------------------------------------------------------------*/
1607 :
1608 24311 : ivas_error IVAS_DEC_GetNumObjects(
1609 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
1610 : UWord16 *numObjects /* o : number of objects for which the decoder has been configured */
1611 : )
1612 : {
1613 24311 : test();
1614 24311 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
1615 : {
1616 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1617 : }
1618 24311 : test();
1619 24311 : test();
1620 24311 : IF( EQ_16( (Word16) hIvasDec->st_ivas->ivas_format, ISM_FORMAT ) || EQ_16( (Word16) hIvasDec->st_ivas->ivas_format, SBA_ISM_FORMAT ) || EQ_16( (Word16) hIvasDec->st_ivas->ivas_format, MASA_ISM_FORMAT ) )
1621 : {
1622 24311 : *numObjects = hIvasDec->st_ivas->nchan_ism;
1623 : }
1624 : ELSE
1625 : {
1626 0 : *numObjects = 0;
1627 : }
1628 24311 : move16();
1629 :
1630 24311 : return IVAS_ERR_OK;
1631 : }
1632 :
1633 :
1634 : /*---------------------------------------------------------------------*
1635 : * IVAS_DEC_GetFormat( )
1636 : *
1637 : * Returns the format of currently decoded bitstream.
1638 : * Note: bitstream format is only known after the first (good) frame has been decoded.
1639 : *---------------------------------------------------------------------*/
1640 :
1641 44 : ivas_error IVAS_DEC_GetFormat(
1642 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
1643 : IVAS_DEC_BS_FORMAT *format /* o : format detected from bitstream fed to the decoder */
1644 : )
1645 : {
1646 44 : IF( hIvasDec->hasDecodedFirstGoodFrame )
1647 : {
1648 44 : *format = mapIvasFormat( hIvasDec->st_ivas->ivas_format );
1649 : }
1650 : ELSE
1651 : {
1652 0 : *format = IVAS_DEC_BS_UNKOWN;
1653 : }
1654 44 : move32();
1655 :
1656 44 : return IVAS_ERR_OK;
1657 : }
1658 :
1659 :
1660 : /*---------------------------------------------------------------------*
1661 : * getInputBufferSize()
1662 : *
1663 : *
1664 : *---------------------------------------------------------------------*/
1665 :
1666 600 : static Word16 getOutputBufferSize(
1667 : const Decoder_Struct *st_ivas /* i : IVAS decoder handle */
1668 : )
1669 : {
1670 600 : IF( st_ivas->hDecoderConfig == NULL )
1671 : {
1672 0 : return -1;
1673 : }
1674 :
1675 600 : IF( EQ_16( (Word16) st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_EXTERNAL ) )
1676 : {
1677 50 : return extract_l( Mult_32_16( imult3216( st_ivas->hDecoderConfig->output_Fs, ( IVAS_MAX_OUTPUT_CHANNELS + IVAS_MAX_NUM_OBJECTS ) ), INV_FRAME_PER_SEC_Q15 ) );
1678 : }
1679 550 : ELSE IF( EQ_16( (Word16) st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_LS_CUSTOM ) )
1680 : {
1681 3 : IF( st_ivas->hLsSetupCustom == NULL )
1682 : {
1683 0 : return -1;
1684 : }
1685 :
1686 3 : return extract_l( Mult_32_16( imult3216( st_ivas->hDecoderConfig->output_Fs, add( st_ivas->hLsSetupCustom->num_spk, st_ivas->hLsSetupCustom->num_lfe ) ), INV_FRAME_PER_SEC_Q15 ) );
1687 : }
1688 : ELSE
1689 : {
1690 547 : return extract_l( Mult_32_16( imult3216( st_ivas->hDecoderConfig->output_Fs, st_ivas->hDecoderConfig->nchan_out ), INV_FRAME_PER_SEC_Q15 ) );
1691 : }
1692 : }
1693 :
1694 :
1695 : /*---------------------------------------------------------------------*
1696 : * IVAS_DEC_GetOutputBufferSize()
1697 : *
1698 : *
1699 : *---------------------------------------------------------------------*/
1700 :
1701 600 : ivas_error IVAS_DEC_GetOutputBufferSize(
1702 : const IVAS_DEC_HANDLE hIvasDec, /* i : IVAS decoder handle */
1703 : Word16 *outputBufferSize /* o : total number of samples expected in the output buffer for current decoder configuration */
1704 : )
1705 : {
1706 600 : test();
1707 600 : test();
1708 600 : IF( outputBufferSize == NULL || hIvasDec == NULL || hIvasDec->st_ivas == NULL )
1709 : {
1710 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1711 : }
1712 :
1713 600 : *outputBufferSize = getOutputBufferSize( hIvasDec->st_ivas );
1714 600 : move16();
1715 :
1716 600 : IF( EQ_16( *outputBufferSize, -1 ) )
1717 : {
1718 0 : return IVAS_ERR_INVALID_OUTPUT_BUFFER_SIZE;
1719 : }
1720 : ELSE
1721 : {
1722 600 : return IVAS_ERR_OK;
1723 : }
1724 : }
1725 :
1726 :
1727 : /*---------------------------------------------------------------------*
1728 : * IVAS_DEC_GetNumOutputChannels( )
1729 : *
1730 : * Returns number of output channels
1731 : *---------------------------------------------------------------------*/
1732 :
1733 594 : ivas_error IVAS_DEC_GetNumOutputChannels(
1734 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
1735 : Word16 *numOutputChannels /* o : number of PCM output channels */
1736 : )
1737 : {
1738 594 : IF( hIvasDec->hasDecodedFirstGoodFrame )
1739 : {
1740 594 : *numOutputChannels = hIvasDec->st_ivas->hDecoderConfig->nchan_out;
1741 : }
1742 : ELSE
1743 : {
1744 0 : *numOutputChannels = 0;
1745 : }
1746 594 : move16();
1747 :
1748 594 : return IVAS_ERR_OK;
1749 : }
1750 :
1751 :
1752 : /*---------------------------------------------------------------------*
1753 : * IVAS_DEC_GetObjectMetadata( )
1754 : *
1755 : * Get metadata of one object decoded in the most recent frame
1756 : *---------------------------------------------------------------------*/
1757 68342 : ivas_error IVAS_DEC_GetObjectMetadata(
1758 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
1759 : IVAS_ISM_METADATA *metadata, /* o : struct where metadata decoded in most recently decoded frame will be written */
1760 : const UWord16 zero_flag, /* i : if this flag is enabled, this function outputs a zero-initialized metadata struct */
1761 : const UWord16 objectIdx /* i : index of the queried object */
1762 : )
1763 : {
1764 : Decoder_Struct *st_ivas;
1765 : ISM_METADATA_HANDLE hIsmMeta;
1766 :
1767 68342 : test();
1768 68342 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
1769 : {
1770 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1771 : }
1772 :
1773 68342 : st_ivas = hIvasDec->st_ivas;
1774 68342 : test();
1775 68342 : test();
1776 68342 : IF( NE_16( st_ivas->ivas_format, ISM_FORMAT ) && NE_16( st_ivas->ivas_format, MASA_ISM_FORMAT ) && NE_16( st_ivas->ivas_format, SBA_ISM_FORMAT ) )
1777 : {
1778 0 : return IVAS_ERR_WRONG_MODE;
1779 : }
1780 :
1781 68342 : IF( GE_16( objectIdx, st_ivas->nchan_ism ) )
1782 : {
1783 0 : return IVAS_ERR_INVALID_INDEX;
1784 : }
1785 :
1786 68342 : hIsmMeta = st_ivas->hIsmMetaData[objectIdx];
1787 :
1788 68342 : IF( hIsmMeta == NULL || zero_flag )
1789 : {
1790 3200 : metadata->azimuth_fx = 0; // Q22
1791 3200 : metadata->elevation_fx = 0; // Q22
1792 3200 : metadata->radius_fx = 512; // Q9
1793 3200 : metadata->yaw_fx = 0; // Q22
1794 3200 : metadata->pitch_fx = 0; // Q22
1795 3200 : metadata->spread_fx = 0; // Q22
1796 3200 : metadata->gainFactor_fx = ONE_IN_Q31; // Q31
1797 3200 : metadata->non_diegetic_flag = 0;
1798 : }
1799 : ELSE
1800 : {
1801 65142 : metadata->azimuth_fx = hIsmMeta->azimuth_fx; // Q22
1802 65142 : metadata->elevation_fx = hIsmMeta->elevation_fx; // Q22
1803 65142 : metadata->radius_fx = hIsmMeta->radius_fx; // Q9
1804 65142 : metadata->yaw_fx = hIsmMeta->yaw_fx; // Q22
1805 65142 : metadata->pitch_fx = hIsmMeta->pitch_fx; // Q22
1806 65142 : metadata->spread_fx = 0; // Q22
1807 65142 : metadata->gainFactor_fx = ONE_IN_Q31;
1808 65142 : metadata->non_diegetic_flag = hIsmMeta->non_diegetic_flag;
1809 : }
1810 :
1811 68342 : move32();
1812 68342 : move32();
1813 68342 : move32();
1814 68342 : move32();
1815 68342 : move32();
1816 68342 : move32();
1817 68342 : move16();
1818 68342 : move16();
1819 68342 : return IVAS_ERR_OK;
1820 : }
1821 :
1822 : /*---------------------------------------------------------------------*
1823 : * IVAS_DEC_GetMasaMetadata( )
1824 : *
1825 : * Get metadata of the most recently decoded MASA frame
1826 : *---------------------------------------------------------------------*/
1827 :
1828 5467 : ivas_error IVAS_DEC_GetMasaMetadata(
1829 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
1830 : 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 */
1831 : const UWord8 getFromJbmBuffer /* i : get metadata from a JBM buffer */
1832 : )
1833 : {
1834 5467 : test();
1835 5467 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
1836 : {
1837 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1838 : }
1839 :
1840 5467 : test();
1841 5467 : IF( NE_16( hIvasDec->st_ivas->ivas_format, MASA_FORMAT ) && NE_16( hIvasDec->st_ivas->ivas_format, MASA_ISM_FORMAT ) )
1842 : {
1843 0 : return IVAS_ERR_WRONG_MODE;
1844 : }
1845 :
1846 5467 : IF( getFromJbmBuffer )
1847 : {
1848 1308 : ivas_jbm_masa_sf_to_sf_map( hIvasDec->st_ivas );
1849 : }
1850 :
1851 5467 : *hMasaExtOutMeta = hIvasDec->st_ivas->hMasa->data.extOutMeta;
1852 :
1853 5467 : return IVAS_ERR_OK;
1854 : }
1855 :
1856 :
1857 : /*---------------------------------------------------------------------*
1858 : * IVAS_DEC_FeedHeadTrackData( )
1859 : *
1860 : * Feed the decoder with the head tracking data
1861 : *---------------------------------------------------------------------*/
1862 :
1863 277720 : ivas_error IVAS_DEC_FeedHeadTrackData(
1864 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
1865 : IVAS_QUATERNION orientation, /* i : head-tracking data, listener orientation */
1866 : IVAS_VECTOR3 Pos, /* i : listener position */
1867 : const Word16 subframe_idx /* i : subframe index */
1868 : )
1869 : {
1870 : HEAD_TRACK_DATA_HANDLE hHeadTrackData;
1871 : ivas_error error;
1872 :
1873 277720 : test();
1874 277720 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
1875 : {
1876 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1877 : }
1878 :
1879 277720 : hHeadTrackData = hIvasDec->st_ivas->hHeadTrackData;
1880 :
1881 277720 : IF( hHeadTrackData == NULL )
1882 : {
1883 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1884 : }
1885 :
1886 : /* Move head-tracking data to the decoder handle */
1887 : /* check for Euler angle signaling */
1888 277720 : IF( EQ_32( orientation.w_fx, -1610612736 /* -3.0f in Q29 */ ) )
1889 : {
1890 0 : Euler2Quat_fx( deg2rad_fx( orientation.x_fx ), deg2rad_fx( orientation.y_fx ), deg2rad_fx( orientation.z_fx ), &orientation );
1891 : }
1892 :
1893 277720 : Word32 updateRate_fx = 1677721600; // value is 200 in Q23
1894 277720 : move32();
1895 277720 : orientation.w_fx = L_shl( orientation.w_fx, sub( Q29, orientation.q_fact ) ); // Q29
1896 277720 : orientation.x_fx = L_shl( orientation.x_fx, sub( Q29, orientation.q_fact ) ); // Q29
1897 277720 : orientation.y_fx = L_shl( orientation.y_fx, sub( Q29, orientation.q_fact ) ); // Q29
1898 277720 : orientation.z_fx = L_shl( orientation.z_fx, sub( Q29, orientation.q_fact ) ); // Q29
1899 277720 : hHeadTrackData->OrientationTracker->refRot.w_fx = L_shl( hHeadTrackData->OrientationTracker->refRot.w_fx, sub( Q29, hHeadTrackData->OrientationTracker->refRot.q_fact ) ); // Q29
1900 277720 : hHeadTrackData->OrientationTracker->refRot.x_fx = L_shl( hHeadTrackData->OrientationTracker->refRot.x_fx, sub( Q29, hHeadTrackData->OrientationTracker->refRot.q_fact ) ); // Q29
1901 277720 : hHeadTrackData->OrientationTracker->refRot.y_fx = L_shl( hHeadTrackData->OrientationTracker->refRot.y_fx, sub( Q29, hHeadTrackData->OrientationTracker->refRot.q_fact ) ); // Q29
1902 277720 : hHeadTrackData->OrientationTracker->refRot.z_fx = L_shl( hHeadTrackData->OrientationTracker->refRot.z_fx, sub( Q29, hHeadTrackData->OrientationTracker->refRot.q_fact ) ); // Q29
1903 277720 : hHeadTrackData->OrientationTracker->absAvgRot.w_fx = L_shl( hHeadTrackData->OrientationTracker->absAvgRot.w_fx, sub( Q29, hHeadTrackData->OrientationTracker->absAvgRot.q_fact ) ); // Q29
1904 277720 : hHeadTrackData->OrientationTracker->absAvgRot.x_fx = L_shl( hHeadTrackData->OrientationTracker->absAvgRot.x_fx, sub( Q29, hHeadTrackData->OrientationTracker->absAvgRot.q_fact ) ); // Q29
1905 277720 : hHeadTrackData->OrientationTracker->absAvgRot.y_fx = L_shl( hHeadTrackData->OrientationTracker->absAvgRot.y_fx, sub( Q29, hHeadTrackData->OrientationTracker->absAvgRot.q_fact ) ); // Q29
1906 277720 : hHeadTrackData->OrientationTracker->absAvgRot.z_fx = L_shl( hHeadTrackData->OrientationTracker->absAvgRot.z_fx, sub( Q29, hHeadTrackData->OrientationTracker->absAvgRot.q_fact ) ); // Q29
1907 :
1908 277720 : orientation.q_fact = Q29;
1909 277720 : hHeadTrackData->OrientationTracker->refRot.q_fact = Q29;
1910 277720 : hHeadTrackData->OrientationTracker->absAvgRot.q_fact = Q29;
1911 277720 : move16();
1912 277720 : move16();
1913 277720 : move16();
1914 :
1915 277720 : IF( NE_32( ( error = ivas_orient_trk_Process_fx( hHeadTrackData->OrientationTracker, orientation, updateRate_fx, &hHeadTrackData->Quaternions[subframe_idx] ) ), IVAS_ERR_OK ) )
1916 : {
1917 0 : return error;
1918 : }
1919 :
1920 277720 : hHeadTrackData->Pos[subframe_idx].x_fx = Pos.x_fx;
1921 277720 : hHeadTrackData->Pos[subframe_idx].y_fx = Pos.y_fx;
1922 277720 : hHeadTrackData->Pos[subframe_idx].z_fx = Pos.z_fx;
1923 277720 : hHeadTrackData->Pos[subframe_idx].q_fact = Pos.q_fact;
1924 277720 : move32();
1925 277720 : move32();
1926 277720 : move32();
1927 277720 : move16();
1928 :
1929 277720 : hIvasDec->updateOrientation = true;
1930 277720 : move16();
1931 :
1932 277720 : return IVAS_ERR_OK;
1933 : }
1934 :
1935 :
1936 : /*---------------------------------------------------------------------*
1937 : * IVAS_DEC_FeedRefRotData( )
1938 : *
1939 : * Feed the decoder with the reference rotation
1940 : *---------------------------------------------------------------------*/
1941 :
1942 0 : ivas_error IVAS_DEC_FeedRefRotData(
1943 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
1944 : IVAS_QUATERNION rotation /* i : reference rotation data */
1945 : )
1946 : {
1947 : ivas_orient_trk_state_t *pOtr;
1948 :
1949 0 : test();
1950 0 : test();
1951 0 : test();
1952 0 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHeadTrackData == NULL || hIvasDec->st_ivas->hHeadTrackData->OrientationTracker == NULL )
1953 : {
1954 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1955 : }
1956 :
1957 0 : pOtr = hIvasDec->st_ivas->hHeadTrackData->OrientationTracker;
1958 :
1959 0 : pOtr->refRot.w_fx = rotation.w_fx; // rotation.q_fact
1960 0 : pOtr->refRot.x_fx = rotation.x_fx; // rotation.q_fact
1961 0 : pOtr->refRot.z_fx = rotation.z_fx; // rotation.q_fact
1962 0 : pOtr->refRot.y_fx = rotation.y_fx; // rotation.q_fact
1963 :
1964 0 : pOtr->refRot.q_fact = rotation.q_fact;
1965 0 : move32();
1966 0 : move32();
1967 0 : move32();
1968 0 : move32();
1969 :
1970 0 : hIvasDec->updateOrientation = true;
1971 0 : move16();
1972 :
1973 0 : return IVAS_ERR_OK;
1974 : }
1975 :
1976 :
1977 : /*---------------------------------------------------------------------*
1978 : * IVAS_DEC_FeedRefVectorData( )
1979 : *
1980 : * Feed the decoder with a reference vector spanning from listenerPos
1981 : * to refPos. Only available in OTR_TRACKING_REF_POS and
1982 : * OTR_TRACKING_REF_POS_LEV modes.
1983 : *---------------------------------------------------------------------*/
1984 :
1985 4004 : ivas_error IVAS_DEC_FeedRefVectorData(
1986 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
1987 : IVAS_VECTOR3 listenerPos, /* i : Listener position */
1988 : IVAS_VECTOR3 refPos /* i : Reference position */
1989 : )
1990 : {
1991 : ivas_orient_trk_state_t *pOtr;
1992 :
1993 4004 : test();
1994 4004 : test();
1995 4004 : test();
1996 4004 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHeadTrackData == NULL || hIvasDec->st_ivas->hHeadTrackData->OrientationTracker == NULL )
1997 : {
1998 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1999 : }
2000 :
2001 4004 : pOtr = hIvasDec->st_ivas->hHeadTrackData->OrientationTracker;
2002 :
2003 4004 : hIvasDec->updateOrientation = true;
2004 4004 : move16();
2005 :
2006 4004 : ivas_error error_fx = ivas_orient_trk_SetReferenceVector_fx( pOtr, listenerPos, refPos );
2007 4004 : return error_fx;
2008 : }
2009 :
2010 : /*---------------------------------------------------------------------*
2011 : * IVAS_DEC_FeedExternalOrientationData( )
2012 : *
2013 : * Feed the decoder with the external orientation data
2014 : *---------------------------------------------------------------------*/
2015 :
2016 116924 : ivas_error IVAS_DEC_FeedExternalOrientationData(
2017 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2018 : IVAS_QUATERNION orientation, /* i : external orientation data */
2019 : Word8 enableHeadRotation, /* i : flag to enable head rotation for this frame */
2020 : Word8 enableExternalOrientation, /* i : flag to enable external orientation for this frame */
2021 : Word8 enableRotationInterpolation, /* i : flag to interpolate rotations from current and previous frames */
2022 : Word16 numFramesToTargetOrientation, /* i : number of frames until target orientation is reached */
2023 : const Word16 subframe_idx /* i : subframe index */
2024 : )
2025 : {
2026 : EXTERNAL_ORIENTATION_HANDLE hExternalOrientationData;
2027 :
2028 116924 : test();
2029 116924 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
2030 : {
2031 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2032 : }
2033 :
2034 116924 : hExternalOrientationData = hIvasDec->st_ivas->hExtOrientationData;
2035 :
2036 116924 : IF( hExternalOrientationData == NULL )
2037 : {
2038 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2039 : }
2040 116924 : orientation.q_fact = Q29;
2041 116924 : move16();
2042 : /* Move external orientation data to the decoder handle (invert orientations) */
2043 116924 : QuaternionInverse_fx( orientation, &hExternalOrientationData->Quaternions[subframe_idx] );
2044 :
2045 116924 : hExternalOrientationData->enableHeadRotation[subframe_idx] = enableHeadRotation;
2046 116924 : hExternalOrientationData->enableExternalOrientation[subframe_idx] = enableExternalOrientation;
2047 116924 : hExternalOrientationData->enableRotationInterpolation[subframe_idx] = enableRotationInterpolation;
2048 116924 : hExternalOrientationData->numFramesToTargetOrientation[subframe_idx] = numFramesToTargetOrientation;
2049 116924 : move16();
2050 116924 : move16();
2051 116924 : move16();
2052 116924 : move16();
2053 :
2054 116924 : hIvasDec->updateOrientation = true;
2055 116924 : move16();
2056 :
2057 116924 : return IVAS_ERR_OK;
2058 : }
2059 :
2060 :
2061 : /*---------------------------------------------------------------------*
2062 : * IVAS_DEC_FeedCustomLsData( )
2063 : *
2064 : * Feed the decoder with the Custom loudspeaker data
2065 : *---------------------------------------------------------------------*/
2066 :
2067 : /*! r: error code */
2068 3 : ivas_error IVAS_DEC_FeedCustomLsData(
2069 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2070 : const IVAS_CUSTOM_LS_DATA hLsCustomData /* i : Custom loudspeaker setup data */
2071 : )
2072 : {
2073 : Word16 i, is_planar;
2074 : IVAS_LSSETUP_CUSTOM_HANDLE hLsSetupCustom;
2075 :
2076 3 : test();
2077 3 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
2078 : {
2079 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2080 : }
2081 :
2082 3 : hLsSetupCustom = hIvasDec->st_ivas->hLsSetupCustom;
2083 :
2084 3 : IF( hLsSetupCustom == NULL )
2085 : {
2086 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2087 : }
2088 :
2089 : /* Move Custom LS layout data to the decoder handle */
2090 :
2091 : /* Loudspeaker azimuths and elevations */
2092 3 : hLsSetupCustom->num_spk = hLsCustomData.num_spk;
2093 3 : move16();
2094 :
2095 3 : Copy32( hLsCustomData.azimuth_fx, hLsSetupCustom->ls_azimuth_fx, hLsCustomData.num_spk ); // Q22
2096 3 : Copy32( hLsCustomData.elevation_fx, hLsSetupCustom->ls_elevation_fx, hLsCustomData.num_spk ); // Q22
2097 :
2098 : /* Set planar flag */
2099 3 : is_planar = 1;
2100 3 : move16();
2101 51 : FOR( i = 0; i < hLsCustomData.num_spk; i++ )
2102 : {
2103 48 : test();
2104 48 : if ( is_planar && hLsSetupCustom->ls_elevation_fx[i] != 0 )
2105 : {
2106 3 : is_planar = 0;
2107 3 : move16();
2108 : }
2109 : }
2110 3 : hLsSetupCustom->is_planar_setup = is_planar;
2111 3 : move16();
2112 :
2113 : /* Loudspeaker LFE */
2114 3 : hLsSetupCustom->num_lfe = hLsCustomData.num_lfe;
2115 3 : move16();
2116 3 : Copy( hLsCustomData.lfe_idx, hLsSetupCustom->lfe_idx, hLsCustomData.num_lfe );
2117 :
2118 3 : return IVAS_ERR_OK;
2119 : }
2120 :
2121 :
2122 : /*---------------------------------------------------------------------*
2123 : * IVAS_DEC_GetHrtfHandle( )
2124 : *
2125 : *
2126 : *---------------------------------------------------------------------*/
2127 :
2128 46 : ivas_error IVAS_DEC_GetHrtfHandle(
2129 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2130 : IVAS_DEC_HRTF_HANDLE *hHrtfTD /* o : HRTF handle */
2131 : )
2132 : {
2133 46 : test();
2134 46 : test();
2135 46 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHrtfTD == NULL )
2136 : {
2137 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2138 : }
2139 :
2140 46 : *hHrtfTD = hIvasDec->st_ivas->hHrtfTD;
2141 :
2142 46 : return IVAS_ERR_OK;
2143 : }
2144 :
2145 :
2146 : /*---------------------------------------------------------------------*
2147 : * IVAS_DEC_GetHrtfCRendHandle( )
2148 : *
2149 : *
2150 : *---------------------------------------------------------------------*/
2151 :
2152 46 : ivas_error IVAS_DEC_GetHrtfCRendHandle(
2153 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2154 : IVAS_DEC_HRTF_CREND_HANDLE *hSetOfHRTF /* o : Set of HRTF handle */
2155 : )
2156 : {
2157 46 : test();
2158 46 : test();
2159 46 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hSetOfHRTF == NULL )
2160 : {
2161 0 : return IVAS_ERR_WRONG_PARAMS;
2162 : }
2163 :
2164 46 : *hSetOfHRTF = hIvasDec->st_ivas->hSetOfHRTF;
2165 :
2166 46 : return IVAS_ERR_OK;
2167 : }
2168 :
2169 :
2170 : /*---------------------------------------------------------------------*
2171 : * IVAS_DEC_GetHrtfFastConvHandle( )
2172 : *
2173 : *
2174 : *---------------------------------------------------------------------*/
2175 :
2176 23 : ivas_error IVAS_DEC_GetHrtfFastConvHandle(
2177 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2178 : IVAS_DEC_HRTF_FASTCONV_HANDLE *hHrtfFastConv /* o : FASTCONV HRTF handle */
2179 : )
2180 : {
2181 23 : test();
2182 23 : test();
2183 23 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHrtfFastConv == NULL )
2184 : {
2185 0 : return IVAS_ERR_WRONG_PARAMS;
2186 : }
2187 :
2188 23 : *hHrtfFastConv = hIvasDec->st_ivas->hHrtfFastConv;
2189 :
2190 23 : return IVAS_ERR_OK;
2191 : }
2192 :
2193 :
2194 : /*---------------------------------------------------------------------*
2195 : * IVAS_DEC_GetHrtfParamBinHandle( )
2196 : *
2197 : *
2198 : *---------------------------------------------------------------------*/
2199 :
2200 23 : ivas_error IVAS_DEC_GetHrtfParamBinHandle(
2201 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2202 : IVAS_DEC_HRTF_PARAMBIN_HANDLE *hHrtfParambin /* o : Parametric binauralizer HRTF handle */
2203 : )
2204 : {
2205 23 : test();
2206 23 : test();
2207 23 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHrtfParambin == NULL )
2208 : {
2209 0 : return IVAS_ERR_WRONG_PARAMS;
2210 : }
2211 :
2212 23 : *hHrtfParambin = hIvasDec->st_ivas->hHrtfParambin;
2213 :
2214 23 : return IVAS_ERR_OK;
2215 : }
2216 :
2217 : /*---------------------------------------------------------------------*
2218 : * copyRendererConfigStruct( )
2219 : *
2220 : *
2221 : *---------------------------------------------------------------------*/
2222 :
2223 29 : static ivas_error copyRendererConfigStruct(
2224 : RENDER_CONFIG_HANDLE hRCin,
2225 : IVAS_RENDER_CONFIG_HANDLE hRCout )
2226 : {
2227 29 : test();
2228 29 : IF( hRCin == NULL || hRCout == NULL )
2229 : {
2230 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2231 : }
2232 :
2233 29 : hRCout->roomAcoustics.override = hRCin->roomAcoustics.override;
2234 29 : hRCout->roomAcoustics.nBands = hRCin->roomAcoustics.nBands;
2235 29 : hRCout->roomAcoustics.acousticPreDelay_fx = hRCin->roomAcoustics.acousticPreDelay_fx;
2236 29 : hRCout->roomAcoustics.inputPreDelay_fx = hRCin->roomAcoustics.inputPreDelay_fx;
2237 :
2238 29 : Copy32( hRCin->roomAcoustics.pFc_input_fx, hRCout->roomAcoustics.pFc_input_fx, CLDFB_NO_CHANNELS_MAX ); // Q16
2239 29 : Copy32( hRCin->roomAcoustics.pAcoustic_rt60_fx, hRCout->roomAcoustics.pAcoustic_rt60_fx, CLDFB_NO_CHANNELS_MAX ); // Q26
2240 29 : Copy32( hRCin->roomAcoustics.pAcoustic_dsr_fx, hRCout->roomAcoustics.pAcoustic_dsr_fx, CLDFB_NO_CHANNELS_MAX ); // Q30
2241 29 : Copy( hRCin->directivity_fx, hRCout->directivity_fx, 3 * MAX_NUM_OBJECTS );
2242 29 : hRCout->roomAcoustics.use_er = hRCin->roomAcoustics.use_er;
2243 29 : hRCout->roomAcoustics.lowComplexity = hRCin->roomAcoustics.lowComplexity;
2244 29 : move16();
2245 29 : move16();
2246 29 : move16();
2247 29 : move32();
2248 29 : move32();
2249 29 : move32();
2250 :
2251 29 : return IVAS_ERR_OK;
2252 : }
2253 :
2254 :
2255 : /*---------------------------------------------------------------------*
2256 : * IVAS_DEC_GetRenderConfig( )
2257 : *
2258 : *
2259 : *---------------------------------------------------------------------*/
2260 :
2261 29 : ivas_error IVAS_DEC_GetRenderConfig(
2262 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2263 : const IVAS_RENDER_CONFIG_HANDLE hRCout /* o : Render configuration handle */
2264 : )
2265 : {
2266 29 : test();
2267 29 : test();
2268 29 : test();
2269 29 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hRenderConfig == NULL || hRCout == NULL )
2270 : {
2271 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2272 : }
2273 :
2274 29 : return copyRendererConfigStruct( hIvasDec->st_ivas->hRenderConfig, hRCout );
2275 : }
2276 :
2277 : /*! r: error code*/
2278 0 : ivas_error IVAS_DEC_GetDefaultRenderConfig(
2279 : IVAS_RENDER_CONFIG_HANDLE hRCout /* o : Render config handle */
2280 : )
2281 : {
2282 : RENDER_CONFIG_DATA RCin;
2283 0 : RENDER_CONFIG_HANDLE hRCin = &RCin;
2284 : ivas_error error;
2285 0 : IF( NE_32( ( error = ivas_render_config_init_from_rom_fx( &hRCin ) ), IVAS_ERR_OK ) )
2286 : {
2287 0 : return error;
2288 : }
2289 :
2290 0 : return copyRendererConfigStruct( hRCin, hRCout );
2291 : }
2292 :
2293 :
2294 : /*---------------------------------------------------------------------*
2295 : * IVAS_DEC_FeedRenderConfig( )
2296 : *
2297 : *
2298 : *---------------------------------------------------------------------*/
2299 :
2300 25 : ivas_error IVAS_DEC_FeedRenderConfig(
2301 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2302 : const IVAS_RENDER_CONFIG_DATA renderConfig /* i : Render configuration struct */
2303 : )
2304 : {
2305 : RENDER_CONFIG_HANDLE hRenderConfig;
2306 :
2307 25 : test();
2308 25 : test();
2309 25 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hRenderConfig == NULL )
2310 : {
2311 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2312 : }
2313 :
2314 25 : hRenderConfig = hIvasDec->st_ivas->hRenderConfig;
2315 25 : hRenderConfig->roomAcoustics.override = renderConfig.roomAcoustics.override;
2316 25 : hRenderConfig->roomAcoustics.nBands = renderConfig.roomAcoustics.nBands;
2317 25 : hRenderConfig->roomAcoustics.acousticPreDelay_fx = renderConfig.roomAcoustics.acousticPreDelay_fx;
2318 25 : hRenderConfig->roomAcoustics.inputPreDelay_fx = renderConfig.roomAcoustics.inputPreDelay_fx;
2319 :
2320 25 : hRenderConfig->roomAcoustics.use_er = 0;
2321 25 : move16();
2322 25 : move16();
2323 25 : move16();
2324 25 : move32();
2325 25 : move32();
2326 25 : IF( EQ_16( renderConfig.roomAcoustics.use_er, 1 ) )
2327 : {
2328 4 : hRenderConfig->roomAcoustics.use_er = renderConfig.roomAcoustics.use_er;
2329 4 : hRenderConfig->roomAcoustics.lowComplexity = renderConfig.roomAcoustics.lowComplexity;
2330 4 : hRenderConfig->roomAcoustics.dimensions = renderConfig.roomAcoustics.dimensions;
2331 4 : hRenderConfig->roomAcoustics.ListenerOrigin = renderConfig.roomAcoustics.ListenerOrigin;
2332 :
2333 4 : Copy32( renderConfig.roomAcoustics.AbsCoeff_fx, hRenderConfig->roomAcoustics.AbsCoeff_fx, IVAS_ROOM_ABS_COEFF );
2334 4 : move16();
2335 4 : move32();
2336 : }
2337 :
2338 25 : Copy32( renderConfig.roomAcoustics.pFc_input_fx, hRenderConfig->roomAcoustics.pFc_input_fx, CLDFB_NO_CHANNELS_MAX ); // Q16
2339 25 : Copy32( renderConfig.roomAcoustics.pAcoustic_rt60_fx, hRenderConfig->roomAcoustics.pAcoustic_rt60_fx, CLDFB_NO_CHANNELS_MAX ); // Q26
2340 25 : Copy32( renderConfig.roomAcoustics.pAcoustic_dsr_fx, hRenderConfig->roomAcoustics.pAcoustic_dsr_fx, CLDFB_NO_CHANNELS_MAX ); // Q30
2341 :
2342 25 : Copy( renderConfig.directivity_fx, hRenderConfig->directivity_fx, 3 * MAX_NUM_OBJECTS );
2343 :
2344 25 : return IVAS_ERR_OK;
2345 : }
2346 :
2347 :
2348 : /*---------------------------------------------------------------------*
2349 : * IVAS_DEC_GetDelay( )
2350 : *
2351 : *
2352 : *---------------------------------------------------------------------*/
2353 :
2354 594 : ivas_error IVAS_DEC_GetDelay(
2355 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2356 : Word16 *nSamples, /* o : decoder delay in samples */
2357 : Word32 *timeScale /* o : time scale of the delay, equal to decoder output sampling rate */
2358 : )
2359 : {
2360 : Decoder_Struct *st_ivas;
2361 : DECODER_CONFIG_HANDLE hDecoderConfig;
2362 : Word32 out_fs_fx;
2363 :
2364 594 : test();
2365 594 : test();
2366 594 : test();
2367 594 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL || nSamples == NULL || timeScale == NULL )
2368 : {
2369 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2370 : }
2371 :
2372 594 : IF( !hIvasDec->hasDecodedFirstGoodFrame )
2373 : {
2374 : /* Delay depends on IVAS format, which is unknown until first frame has been decoded */
2375 0 : return IVAS_ERR_WAITING_FOR_BITSTREAM;
2376 : }
2377 :
2378 594 : st_ivas = hIvasDec->st_ivas;
2379 594 : hDecoderConfig = st_ivas->hDecoderConfig;
2380 :
2381 594 : IF( NE_32( hDecoderConfig->output_Fs, 48000 ) )
2382 : {
2383 242 : IF( NE_32( hDecoderConfig->output_Fs, 32000 ) )
2384 : {
2385 79 : out_fs_fx = FS_16K_IN_NS_Q31;
2386 : }
2387 : ELSE
2388 : {
2389 163 : out_fs_fx = FS_32K_IN_NS_Q31;
2390 : }
2391 : }
2392 : ELSE
2393 : {
2394 352 : out_fs_fx = FS_48K_IN_NS_Q31;
2395 : }
2396 594 : move32();
2397 :
2398 594 : nSamples[1] = NS2SA_FX2( hDecoderConfig->output_Fs, get_delay_fx( DEC, hDecoderConfig->output_Fs, st_ivas->ivas_format, st_ivas->cldfbAnaDec[0] ) );
2399 594 : move16();
2400 594 : nSamples[2] = extract_l( W_round64_L( W_mult0_32_32( L_shl( st_ivas->binaural_latency_ns, 1 ), out_fs_fx ) ) );
2401 594 : move16();
2402 594 : nSamples[0] = add( nSamples[1], nSamples[2] );
2403 594 : move16();
2404 :
2405 594 : IF( EQ_16( (Word16) st_ivas->ivas_format, MASA_FORMAT ) )
2406 : {
2407 : /* note: in MASA, all delay is compensated at the decoder by default, so subtract the encoder delay for print-out */
2408 80 : nSamples[1] = sub( nSamples[1], NS2SA_FX2( hDecoderConfig->output_Fs, IVAS_ENC_DELAY_NS ) );
2409 80 : move16();
2410 : }
2411 :
2412 594 : *timeScale = hDecoderConfig->output_Fs;
2413 594 : move32();
2414 :
2415 594 : return IVAS_ERR_OK;
2416 : }
2417 :
2418 :
2419 : /*---------------------------------------------------------------------*
2420 : * IVAS_DEC_HasDecodedFirstGoodFrame( )
2421 : *
2422 : *
2423 : *---------------------------------------------------------------------*/
2424 :
2425 784 : ivas_error IVAS_DEC_HasDecodedFirstGoodFrame(
2426 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2427 : bool *hasDecodedFirstGoodFrame /* o : flag indicating if the decoder has decoded a good frame since it was configured */
2428 : )
2429 : {
2430 784 : test();
2431 784 : test();
2432 784 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hasDecodedFirstGoodFrame == NULL )
2433 : {
2434 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2435 : }
2436 :
2437 784 : *hasDecodedFirstGoodFrame = hIvasDec->hasDecodedFirstGoodFrame;
2438 784 : move16();
2439 :
2440 784 : return IVAS_ERR_OK;
2441 : }
2442 :
2443 :
2444 : /*---------------------------------------------------------------------*
2445 : * IVAS_DEC_GetPcmFrameSize( )
2446 : *
2447 : *
2448 : *---------------------------------------------------------------------*/
2449 :
2450 594 : ivas_error IVAS_DEC_GetPcmFrameSize(
2451 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2452 : Word32 *pcmFrameSize /* o : total size of the PCM output frame. This takes into account the number of output channels Q0 */
2453 : )
2454 : {
2455 594 : test();
2456 594 : test();
2457 594 : IF( hIvasDec == NULL || hIvasDec->st_ivas == NULL || pcmFrameSize == NULL )
2458 : {
2459 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2460 : }
2461 :
2462 594 : IF( hIvasDec->hasDecodedFirstGoodFrame )
2463 : {
2464 594 : *pcmFrameSize = Mult_32_16( imult3216( hIvasDec->st_ivas->hDecoderConfig->output_Fs, hIvasDec->st_ivas->hDecoderConfig->nchan_out ), INV_FRAME_PER_SEC_Q15 );
2465 : }
2466 : ELSE
2467 : {
2468 0 : *pcmFrameSize = 0;
2469 : }
2470 594 : move32();
2471 :
2472 594 : return IVAS_ERR_OK;
2473 : }
2474 :
2475 :
2476 : /*---------------------------------------------------------------------*
2477 : * isSidFrame( )
2478 : *
2479 : * Check if a frame contains a SID
2480 : *---------------------------------------------------------------------*/
2481 :
2482 17523 : static bool isSidFrame(
2483 : const UWord16 size )
2484 : {
2485 17523 : IF( EQ_16( size, ( SID_1k75 / FRAMES_PER_SEC ) ) )
2486 : {
2487 0 : return true; /* AMR-WB SID */
2488 : }
2489 17523 : ELSE IF( EQ_16( size, ( SID_2k40 / FRAMES_PER_SEC ) ) )
2490 : {
2491 0 : return true; /* EVS SID */
2492 : }
2493 17523 : ELSE IF( EQ_16( size, ( IVAS_SID_5k2 / FRAMES_PER_SEC ) ) )
2494 : {
2495 268 : return true; /* IVAS SID */
2496 : }
2497 :
2498 17255 : return false;
2499 : }
2500 :
2501 17540 : static void bsCompactToSerial( const UWord8 *compact, UWord16 *serial, UWord16 num_bits )
2502 : {
2503 : /* Bitstream conversion is not counted towards complexity and memory usage */
2504 : #define WMC_TOOL_SKIP
2505 : UWord32 i;
2506 17540 : UWord8 byte = 0;
2507 17540 : const UWord8 mask = 0x80;
2508 17540 : move16();
2509 17540 : move16();
2510 :
2511 29609164 : FOR( i = 0; i < num_bits; ++i )
2512 : {
2513 29591624 : if ( ( i % 8 ) == 0 )
2514 : {
2515 3698953 : byte = compact[i / 8];
2516 3698953 : move16();
2517 : }
2518 :
2519 29591624 : serial[i] = shr( s_and( byte, mask ), 7 );
2520 29591624 : move16();
2521 :
2522 29591624 : byte = (UWord8) shl( byte, 1 );
2523 : }
2524 :
2525 : /* Add 4 padding bytes required by core coder */
2526 578820 : FOR( i = 0; i < 4 * 8; ++i )
2527 : {
2528 561280 : serial[( num_bits + i )] = 0;
2529 561280 : move16();
2530 : }
2531 : #undef WMC_TOOL_SKIP
2532 17540 : }
2533 :
2534 :
2535 : /*---------------------------------------------------------------------*
2536 : * IVAS_DEC_VoIP_FeedFrame( )
2537 : *
2538 : * Feed RTP packet into internal jitter buffer
2539 : *---------------------------------------------------------------------*/
2540 :
2541 17523 : ivas_error IVAS_DEC_VoIP_FeedFrame(
2542 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2543 : UWord8 *au, /* i : buffer containing input access unit */
2544 : const UWord16 auSize, /* i : size of the access unit */
2545 : const UWord16 rtpSequenceNumber, /* i : RTP sequence number (16 bits) */
2546 : const UWord32 rtpTimeStamp, /* i : RTP timestamp (32 bits) */
2547 : const UWord32 rcvTime_ms, /* i : receive time of the RTP packet in milliseconds */
2548 : const bool qBit /* i : Q bit for AMR-WB IO */
2549 : )
2550 : {
2551 : JB4_DATAUNIT_HANDLE dataUnit;
2552 : Word16 partialCopyFrameType, partialCopyOffset;
2553 : Word16 result;
2554 :
2555 17523 : IF( auSize == 0 )
2556 : {
2557 0 : return IVAS_ERR_OK; /* ignore empty/NO_DATA frame - shouldn't be transmitted in RTP */
2558 : }
2559 17523 : IF( GT_16( shr( add( auSize, 7 ), 3 ), (Word16) MAX_AU_SIZE ) )
2560 : {
2561 0 : return IVAS_ERR_INVALID_BITSTREAM;
2562 : }
2563 :
2564 : /* check if frame contains a partial copy and get its offset */
2565 17523 : evs_dec_previewFrame( au, auSize, &partialCopyFrameType, &partialCopyOffset );
2566 :
2567 : /* create data unit for primary copy in the frame */
2568 17523 : dataUnit = JB4_AllocDataUnit( hIvasDec->hVoIP->hJBM );
2569 17523 : mvc2c( au, dataUnit->data, shr( add( auSize, 7 ), 3 ) );
2570 17523 : dataUnit->dataSize = auSize;
2571 17523 : dataUnit->duration = 20;
2572 17523 : dataUnit->sequenceNumber = rtpSequenceNumber;
2573 17523 : dataUnit->silenceIndicator = isSidFrame( dataUnit->dataSize );
2574 17523 : move16();
2575 17523 : dataUnit->timeScale = 1000;
2576 17523 : dataUnit->rcvTime = rcvTime_ms;
2577 17523 : dataUnit->timeStamp = rtpTimeStamp;
2578 17523 : dataUnit->partial_frame = 0;
2579 17523 : dataUnit->partialCopyOffset = partialCopyOffset;
2580 17523 : dataUnit->qBit = qBit;
2581 17523 : move16();
2582 17523 : move16();
2583 17523 : move16();
2584 17523 : move16();
2585 17523 : move16();
2586 17523 : move32();
2587 17523 : move32();
2588 17523 : move32();
2589 17523 : move32();
2590 :
2591 : /* add the frame to the JBM */
2592 17523 : result = JB4_PushDataUnit( hIvasDec->hVoIP->hJBM, dataUnit, rcvTime_ms );
2593 17523 : IF( result != 0 )
2594 : {
2595 0 : return IVAS_ERR_UNKNOWN;
2596 : }
2597 :
2598 17523 : test();
2599 17523 : IF( NE_16( partialCopyFrameType, RF_NO_DATA ) && partialCopyOffset != 0 )
2600 : {
2601 : /* create data unit for partial copy in the frame */
2602 764 : dataUnit = JB4_AllocDataUnit( hIvasDec->hVoIP->hJBM );
2603 764 : mvc2c( au, dataUnit->data, shr( add( auSize, 7 ), 3 ) );
2604 764 : dataUnit->dataSize = auSize;
2605 764 : dataUnit->duration = 20;
2606 764 : dataUnit->sequenceNumber = rtpSequenceNumber;
2607 764 : dataUnit->silenceIndicator = 0; /* there are no partial copies for SID frames */
2608 764 : dataUnit->timeScale = 1000;
2609 764 : dataUnit->rcvTime = rcvTime_ms;
2610 764 : dataUnit->timeStamp = (UWord32) W_sub( rtpTimeStamp, imult3216( dataUnit->duration, partialCopyOffset ) );
2611 764 : move32();
2612 764 : dataUnit->partial_frame = 1;
2613 764 : dataUnit->partialCopyOffset = partialCopyOffset;
2614 764 : dataUnit->qBit = qBit;
2615 764 : move16();
2616 764 : move16();
2617 764 : move16();
2618 764 : move16();
2619 764 : move16();
2620 764 : move32();
2621 764 : move32();
2622 764 : move32();
2623 :
2624 : /* add the frame to the JBM */
2625 764 : result = JB4_PushDataUnit( hIvasDec->hVoIP->hJBM, dataUnit, rcvTime_ms );
2626 764 : IF( result != 0 )
2627 : {
2628 0 : return IVAS_ERR_UNKNOWN;
2629 : }
2630 : }
2631 :
2632 17523 : return IVAS_ERR_OK;
2633 : }
2634 :
2635 : /*---------------------------------------------------------------------*
2636 : * IVAS_DEC_VoIP_SetScale( )
2637 : *
2638 : * Set the TSM scale
2639 : *---------------------------------------------------------------------*/
2640 :
2641 19595 : ivas_error IVAS_DEC_VoIP_SetScale(
2642 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2643 : const Word16 maxScaling, /* i : max allowed absolute difference in samples from the default 20ms frame size */
2644 : const Word16 scale /* i : TSM scale to set in percent of the default frame size */
2645 : )
2646 : {
2647 : ivas_error error;
2648 :
2649 19595 : error = IVAS_ERR_OK;
2650 19595 : move32();
2651 :
2652 19595 : IF( hIvasDec->st_ivas->hDecoderConfig->Opt_tsm == false )
2653 : {
2654 0 : return IVAS_ERR_TSM_NOT_ENABLED;
2655 : }
2656 : ELSE
2657 : {
2658 19595 : hIvasDec->tsm_scale = scale;
2659 19595 : hIvasDec->tsm_max_scaling = maxScaling;
2660 19595 : move16();
2661 19595 : move16();
2662 : }
2663 :
2664 19595 : return error;
2665 : }
2666 :
2667 : /*---------------------------------------------------------------------*
2668 : * IVAS_DEC_VoIP_SetScale( )
2669 : *
2670 : * Set the TSM scale
2671 : *---------------------------------------------------------------------*/
2672 :
2673 0 : ivas_error IVAS_DEC_TSM_SetQuality(
2674 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2675 : const Word16 quality /* i : target TSM quality Q14 */
2676 : )
2677 : {
2678 : ivas_error error;
2679 :
2680 0 : error = IVAS_ERR_OK;
2681 0 : move32();
2682 :
2683 0 : IF( hIvasDec->st_ivas->hDecoderConfig->Opt_tsm == false )
2684 : {
2685 0 : return IVAS_ERR_TSM_NOT_ENABLED;
2686 : }
2687 : ELSE
2688 : {
2689 0 : hIvasDec->tsm_quality = quality; // Q14
2690 0 : move16();
2691 : }
2692 :
2693 0 : return error;
2694 : }
2695 :
2696 :
2697 : /*---------------------------------------------------------------------*
2698 : * IVAS_DEC_VoIP_GetSamples( )
2699 : *
2700 : * Main function to decode one frame in VoIP
2701 : *---------------------------------------------------------------------*/
2702 :
2703 19595 : ivas_error IVAS_DEC_VoIP_GetSamples(
2704 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2705 : UWord16 nSamplesPerChannel, /* i : number of samples per channel requested to be written to output buffer */
2706 : Word16 *pcmBuf, /* i/o: buffer for decoded PCM output. The memory must already be allocated and be able to hold the expected number of output samples, based on frame size and number of output channels Q0 */
2707 : const UWord32 systemTimestamp_ms /* i : current system timestamp */
2708 : #ifdef SUPPORT_JBM_TRACEFILE
2709 : ,
2710 : JbmTraceFileWriterFn jbmWriterFn,
2711 : void *jbmWriter
2712 : #endif
2713 : )
2714 : {
2715 : Decoder_Struct *st_ivas;
2716 : DECODER_CONFIG_HANDLE hDecoderConfig;
2717 : IVAS_DEC_VOIP *hVoIP;
2718 : UWord32 extBufferedTime_ms, scale, maxScaling;
2719 : JB4_DATAUNIT_HANDLE dataUnit;
2720 : UWord16 extBufferedSamples;
2721 : #ifndef NONBE_FIX_864_JBM_RENDER_FRAMESIZE
2722 : Word16 timeScalingDone;
2723 : #endif
2724 : Word16 result;
2725 : ivas_error error;
2726 : Word16 nSamplesRendered;
2727 : UWord8 nOutChannels;
2728 :
2729 19595 : st_ivas = hIvasDec->st_ivas;
2730 19595 : hDecoderConfig = st_ivas->hDecoderConfig;
2731 19595 : hVoIP = hIvasDec->hVoIP;
2732 : #ifndef NONBE_FIX_864_JBM_RENDER_FRAMESIZE
2733 : timeScalingDone = 0;
2734 : #endif
2735 19595 : nOutChannels = (UWord8) st_ivas->hDecoderConfig->nchan_out;
2736 19595 : nSamplesRendered = 0;
2737 19595 : move16();
2738 19595 : move16();
2739 19595 : move16();
2740 :
2741 19595 : IF( nSamplesPerChannel == 0 )
2742 : {
2743 0 : return IVAS_ERR_WRONG_PARAMS;
2744 : }
2745 :
2746 : /* make sure that the FIFO after decoder/scaler contains at least one sound card frame (i.e. 20ms) */
2747 51180 : WHILE( LT_16( nSamplesRendered, nSamplesPerChannel ) )
2748 : {
2749 31585 : IF( hIvasDec->nSamplesAvailableNext == 0 )
2750 : {
2751 : Word16 nSamplesBuffered;
2752 19595 : nSamplesBuffered = 0;
2753 19595 : move16();
2754 19595 : IF( hIvasDec->hasBeenFedFirstGoodFrame )
2755 : {
2756 19378 : IF( NE_32( ( error = IVAS_DEC_GetBufferedNumberOfSamples( hIvasDec, &nSamplesBuffered ) ), IVAS_ERR_OK ) )
2757 : {
2758 0 : return error;
2759 : }
2760 : }
2761 :
2762 : #ifdef NONBE_FIX_864_JBM_RENDER_FRAMESIZE
2763 19595 : extBufferedSamples = nSamplesBuffered;
2764 19595 : move16();
2765 : #else
2766 : extBufferedSamples = add( nSamplesRendered, nSamplesBuffered );
2767 : #endif
2768 : Word16 exp;
2769 19595 : extBufferedTime_ms = BASOP_Util_Divide3232_Scale( imult3216( extBufferedSamples, 1000 ), hDecoderConfig->output_Fs, &exp );
2770 19595 : extBufferedTime_ms = (UWord32) W_shr( extBufferedTime_ms, sub( 15, exp ) ); // Q0
2771 19595 : dataUnit = NULL;
2772 :
2773 : /* pop one access unit from the jitter buffer */
2774 19595 : result = JB4_PopDataUnit( hVoIP->hJBM, systemTimestamp_ms, extBufferedTime_ms, &dataUnit, &scale, &maxScaling );
2775 19595 : IF( result != 0 )
2776 : {
2777 0 : return IVAS_ERR_UNKNOWN;
2778 : }
2779 :
2780 19595 : IF( EQ_32( hIvasDec->mode, IVAS_DEC_MODE_EVS ) )
2781 : {
2782 0 : if ( GT_32( maxScaling, 20 ) )
2783 : {
2784 0 : maxScaling = 20;
2785 0 : move16();
2786 : }
2787 : }
2788 19595 : maxScaling = Mult_32_32( imult3216( hDecoderConfig->output_Fs, (Word16) maxScaling ), INV_1000_Q31 );
2789 :
2790 : #ifdef DEBUG_MODE_JBM
2791 : dbgwrite( &extBufferedSamples, sizeof( uint16_t ), 1, 1, "./res/JBM_extBufferedSamples.dat" );
2792 : dbgwrite( &systemTimestamp_ms, sizeof( uint32_t ), 1, 1, "./res/JBM_systemTimestamp.dat" );
2793 : dbgwrite( &scale, sizeof( uint32_t ), 1, 1, "./res/JBM_scale.dat" );
2794 : dbgwrite( &maxScaling, sizeof( uint32_t ), 1, 1, "./res/JBM_maxScale.dat" );
2795 : #endif
2796 :
2797 : /* avoid time scaling multiple times in one sound card slot */
2798 19595 : IF( NE_64( scale, 100U ) )
2799 : #ifdef NONBE_FIX_864_JBM_RENDER_FRAMESIZE
2800 : {
2801 718 : IF( hIvasDec->timeScalingDone )
2802 : #else
2803 : {
2804 : IF( timeScalingDone )
2805 : #endif
2806 : {
2807 4 : scale = 100;
2808 4 : move32();
2809 : }
2810 : #ifndef NONBE_FIX_864_JBM_RENDER_FRAMESIZE
2811 : ELSE
2812 : {
2813 : timeScalingDone = 1;
2814 : move16();
2815 : }
2816 : #endif
2817 : }
2818 :
2819 : /* limit scale to range supported by time scaler */
2820 19595 : IF( LT_32( scale, APA_MIN_SCALE ) )
2821 : {
2822 0 : scale = APA_MIN_SCALE;
2823 0 : move32();
2824 : }
2825 19595 : ELSE IF( GT_32( scale, APA_MAX_SCALE ) )
2826 : {
2827 0 : scale = APA_MAX_SCALE;
2828 0 : move32();
2829 : }
2830 :
2831 19595 : IF( NE_32( ( error = IVAS_DEC_VoIP_SetScale( hIvasDec, (Word16) maxScaling, (Word16) scale ) ), IVAS_ERR_OK ) )
2832 : {
2833 0 : return error;
2834 : }
2835 :
2836 : /* copy bitstream into decoder state */
2837 19595 : IF( dataUnit )
2838 : {
2839 17540 : hIvasDec->hVoIP->hCurrentDataUnit = dataUnit;
2840 :
2841 17540 : bsCompactToSerial( dataUnit->data, hIvasDec->hVoIP->bs_conversion_buf, dataUnit->dataSize );
2842 :
2843 :
2844 17540 : IF( NE_32( ( error = IVAS_DEC_FeedFrame_Serial( hIvasDec, hIvasDec->hVoIP->bs_conversion_buf, dataUnit->dataSize, 0 ) ), IVAS_ERR_OK ) )
2845 : {
2846 0 : return error;
2847 : }
2848 : }
2849 2055 : ELSE IF( hIvasDec->hasDecodedFirstGoodFrame )
2850 : {
2851 : /* Decoder has been initialized with first good frame - do PLC */
2852 1865 : IF( NE_32( ( error = IVAS_DEC_FeedFrame_Serial( hIvasDec, hIvasDec->hVoIP->bs_conversion_buf, 0, 1 ) ), IVAS_ERR_OK ) )
2853 : {
2854 0 : return error;
2855 : }
2856 : }
2857 :
2858 : #ifdef SUPPORT_JBM_TRACEFILE
2859 : /* jbmWriterFn and jbmWriter may be NULL if tracefile writing was not requested on CLI */
2860 19595 : test();
2861 19595 : IF( jbmWriterFn != NULL && jbmWriter != NULL )
2862 : {
2863 : /* write JBM trace data entry */
2864 19595 : store_JbmData( hVoIP, dataUnit, systemTimestamp_ms, extBufferedSamples, hDecoderConfig->output_Fs );
2865 19595 : IF( ( jbmWriterFn( &hVoIP->JbmTraceData, jbmWriter ) ) != IVAS_ERR_OK )
2866 : {
2867 0 : fprintf( stderr, "\nError writing JBM Trace data to file\n" );
2868 0 : return IVAS_ERR_UNKNOWN;
2869 : }
2870 : }
2871 : #endif
2872 19595 : IF( dataUnit )
2873 : {
2874 17540 : IF( dataUnit->partial_frame != 0 )
2875 : {
2876 41 : hVoIP->lastDecodedWasActive = 1;
2877 : }
2878 : ELSE
2879 : {
2880 17499 : hVoIP->lastDecodedWasActive = !dataUnit->silenceIndicator;
2881 : }
2882 17540 : move16();
2883 :
2884 : /* data unit memory is no longer used */
2885 17540 : JB4_FreeDataUnit( hVoIP->hJBM, dataUnit );
2886 : }
2887 :
2888 19595 : IF( !hIvasDec->hasBeenFedFirstGoodFrame )
2889 : {
2890 190 : hIvasDec->nSamplesAvailableNext = hIvasDec->nSamplesFrame;
2891 190 : hIvasDec->nSamplesRendered = 0;
2892 190 : move16();
2893 190 : move16();
2894 : }
2895 : }
2896 :
2897 : /* decode */
2898 31585 : IF( !hIvasDec->hasBeenFedFirstGoodFrame )
2899 : {
2900 : /* codec mode to use not known yet - simply output silence */
2901 : /* directly set output zero */
2902 190 : Word16 nSamplesToZero = s_min( nSamplesPerChannel, hIvasDec->nSamplesAvailableNext );
2903 190 : set16_fx( pcmBuf + imult1616( nSamplesRendered, nOutChannels ), 0, imult1616( nSamplesToZero, nOutChannels ) );
2904 190 : nSamplesRendered = add( nSamplesRendered, nSamplesToZero );
2905 190 : hIvasDec->nSamplesRendered = add( hIvasDec->nSamplesRendered, nSamplesToZero );
2906 190 : hIvasDec->nSamplesAvailableNext = sub( hIvasDec->nSamplesAvailableNext, nSamplesToZero );
2907 190 : move16();
2908 190 : move16();
2909 : #ifdef NONBE_FIX_864_JBM_RENDER_FRAMESIZE
2910 190 : update_voip_rendered20ms( hIvasDec, nSamplesToZero );
2911 : #endif
2912 : }
2913 : ELSE
2914 : {
2915 : Word16 nSamplesToRender, nSamplesRendered_loop;
2916 : bool tmp;
2917 31395 : nSamplesToRender = sub( nSamplesPerChannel, nSamplesRendered );
2918 :
2919 : /* render IVAS frames directly to the output buffer */
2920 31395 : IF( NE_32( ( error = IVAS_DEC_GetSamples( hIvasDec, nSamplesToRender, pcmBuf + imult1616( nSamplesRendered, nOutChannels ), &nSamplesRendered_loop, &tmp ) ), IVAS_ERR_OK ) )
2921 : {
2922 0 : return error;
2923 : }
2924 :
2925 31395 : nSamplesRendered = add( nSamplesRendered, nSamplesRendered_loop );
2926 : #ifdef NONBE_FIX_864_JBM_RENDER_FRAMESIZE
2927 31395 : update_voip_rendered20ms( hIvasDec, nSamplesRendered_loop );
2928 : #endif
2929 : }
2930 : }
2931 :
2932 19595 : return IVAS_ERR_OK;
2933 : }
2934 :
2935 : #ifdef NONBE_FIX_864_JBM_RENDER_FRAMESIZE
2936 : /*---------------------------------------------------------------------*
2937 : * update_voip_rendered20ms( )
2938 : *
2939 : * Function to flush remaining audio in VoIP
2940 : *---------------------------------------------------------------------*/
2941 :
2942 31585 : static void update_voip_rendered20ms(
2943 : IVAS_DEC_HANDLE hIvasDec,
2944 : const Word16 nSamplesRendered )
2945 : {
2946 : Word16 nSamplesRenderedTotal;
2947 31585 : nSamplesRenderedTotal = add( hIvasDec->hVoIP->nSamplesRendered20ms, nSamplesRendered );
2948 : /* we have crossed a 20ms border, reset the time scaling done flag */
2949 31585 : IF( GE_16( nSamplesRenderedTotal, hIvasDec->hVoIP->nSamplesFrame ) )
2950 : {
2951 19595 : hIvasDec->timeScalingDone = 0;
2952 19595 : move16();
2953 : }
2954 :
2955 : /* float code was: hIvasDec->hVoIP->nSamplesRendered20ms = nSamplesRenderedTotal % hIvasDec->hVoIP->nSamplesFrame; */
2956 31585 : hIvasDec->hVoIP->nSamplesRendered20ms = nSamplesRenderedTotal;
2957 31585 : move16();
2958 51180 : WHILE( GE_16( hIvasDec->hVoIP->nSamplesRendered20ms, hIvasDec->hVoIP->nSamplesFrame ) )
2959 : {
2960 19595 : hIvasDec->hVoIP->nSamplesRendered20ms = sub( hIvasDec->hVoIP->nSamplesRendered20ms, hIvasDec->hVoIP->nSamplesFrame );
2961 : }
2962 31585 : }
2963 :
2964 : #endif
2965 :
2966 : /*---------------------------------------------------------------------*
2967 : * IVAS_DEC_VoIP_Flush( )
2968 : *
2969 : * Function to flush remaining audio in VoIP
2970 : *---------------------------------------------------------------------*/
2971 :
2972 27 : ivas_error IVAS_DEC_Flush(
2973 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
2974 : const Word16 nSamplesPerChannel, /* i : number of samples per channel requested to be written to output buffer */
2975 : Word16 *pcmBuf, /* i/o: buffer for decoded PCM output. The memory must already be allocated and be able to hold the expected number of output samples, based on frame size and number of output channels Q0 */
2976 : Word16 *nSamplesFlushed /* o : number of samples flushed */
2977 : )
2978 : {
2979 : ivas_error error;
2980 : UWord16 nSamplesToRender;
2981 : UWord16 nSamplesFlushedLocal;
2982 :
2983 27 : *nSamplesFlushed = s_min( nSamplesPerChannel, hIvasDec->nSamplesAvailableNext );
2984 27 : move16();
2985 :
2986 27 : nSamplesToRender = (UWord16) *nSamplesFlushed;
2987 27 : move16();
2988 :
2989 : /* render IVAS frames */
2990 : #ifdef NONBE_FIX_864_JBM_RENDER_FRAMESIZE
2991 27 : error = IVAS_ERR_OK;
2992 :
2993 27 : test();
2994 27 : IF( GT_16( nSamplesToRender, 0 ) && NE_16( (Word16) hIvasDec->st_ivas->ivas_format, (Word16) MONO_FORMAT ) )
2995 : {
2996 : #endif
2997 20 : error = IVAS_DEC_GetRenderedSamples( hIvasDec, nSamplesToRender, &nSamplesFlushedLocal, &hIvasDec->nSamplesAvailableNext, pcmBuf );
2998 : #ifdef NONBE_FIX_864_JBM_RENDER_FRAMESIZE
2999 : }
3000 : ELSE
3001 : {
3002 7 : *nSamplesFlushed = 0;
3003 7 : move16();
3004 : }
3005 : #endif
3006 :
3007 27 : return error;
3008 : }
3009 :
3010 :
3011 : /*---------------------------------------------------------------------*
3012 : * IVAS_DEC_VoIP_IsEmpty( )
3013 : *
3014 : *
3015 : *---------------------------------------------------------------------*/
3016 :
3017 206 : bool IVAS_DEC_VoIP_IsEmpty(
3018 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
3019 : const Word16 nSamplesAsked )
3020 : {
3021 206 : test();
3022 206 : return ( JB4_bufferedDataUnits( hIvasDec->hVoIP->hJBM ) == 0 && LT_16( hIvasDec->nSamplesAvailableNext, nSamplesAsked ) );
3023 : }
3024 :
3025 :
3026 : /*---------------------------------------------------------------------*
3027 : * IVAS_DEC_VoIP_Get_CA_offset( )
3028 : *
3029 : *
3030 : *---------------------------------------------------------------------*/
3031 :
3032 0 : ivas_error IVAS_DEC_VoIP_Get_CA_offset(
3033 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
3034 : Word16 *optimum_offset,
3035 : Word16 *FEC_hi )
3036 : {
3037 0 : test();
3038 0 : test();
3039 0 : IF( ( hIvasDec == NULL || hIvasDec->hVoIP == NULL || hIvasDec->hVoIP->hJBM == NULL ) )
3040 : {
3041 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
3042 : }
3043 :
3044 0 : *optimum_offset = JB4_getFECoffset( hIvasDec->hVoIP->hJBM );
3045 0 : *FEC_hi = JB4_FECoffset( hIvasDec->hVoIP->hJBM );
3046 0 : move16();
3047 0 : move16();
3048 :
3049 0 : return IVAS_ERR_OK;
3050 : }
3051 :
3052 :
3053 : /*---------------------------------------------------------------------*
3054 : * IVAS_DEC_Close_VoIP( )
3055 : *
3056 : *
3057 : *---------------------------------------------------------------------*/
3058 :
3059 27 : static void IVAS_DEC_Close_VoIP(
3060 : IVAS_DEC_VOIP *hVoIP /* i/o: IVAS decoder handle */
3061 : )
3062 : {
3063 27 : JB4_Destroy( &hVoIP->hJBM );
3064 :
3065 27 : IF( hVoIP->bs_conversion_buf != NULL )
3066 : {
3067 : #define WMC_TOOL_SKIP
3068 : /* Bitstream conversion is not counted towards complexity and memory usage */
3069 27 : free( hVoIP->bs_conversion_buf );
3070 : #undef WMC_TOOL_SKIP
3071 : }
3072 :
3073 27 : free( hVoIP );
3074 :
3075 27 : return;
3076 : }
3077 :
3078 :
3079 : #ifdef SUPPORT_JBM_TRACEFILE
3080 : /*---------------------------------------------------------------------*
3081 : * store_JbmData()
3082 : *
3083 : * Store JBM trace data entry
3084 : *---------------------------------------------------------------------*/
3085 :
3086 19595 : static void store_JbmData(
3087 : IVAS_DEC_VOIP *hVoIP,
3088 : JB4_DATAUNIT_HANDLE dataUnit,
3089 : const UWord32 systemTimestamp_ms,
3090 : const UWord16 extBufferedSamples,
3091 : const Word32 output_Fs )
3092 : {
3093 : IVAS_JBM_TRACE_DATA *JbmTraceData;
3094 :
3095 19595 : IF( hVoIP == NULL )
3096 : {
3097 0 : return;
3098 : }
3099 :
3100 19595 : JbmTraceData = &hVoIP->JbmTraceData;
3101 :
3102 19595 : JbmTraceData->systemTimestamp_ms = systemTimestamp_ms;
3103 19595 : JbmTraceData->extBufferedSamples = extBufferedSamples;
3104 19595 : JbmTraceData->lastDecodedWasActive = hVoIP->lastDecodedWasActive;
3105 19595 : JbmTraceData->output_Fs = output_Fs;
3106 19595 : JbmTraceData->dataUnit_flag = dataUnit != NULL;
3107 19595 : move16();
3108 19595 : move16();
3109 19595 : move16();
3110 19595 : move32();
3111 19595 : move32();
3112 19595 : IF( dataUnit != NULL )
3113 : {
3114 17540 : JbmTraceData->sequenceNumber = dataUnit->sequenceNumber;
3115 17540 : JbmTraceData->timeStamp = dataUnit->timeStamp;
3116 17540 : JbmTraceData->rcvTime = dataUnit->rcvTime;
3117 17540 : JbmTraceData->partial_frame = dataUnit->partial_frame;
3118 17540 : JbmTraceData->partialCopyOffset = dataUnit->partialCopyOffset;
3119 17540 : move16();
3120 17540 : move16();
3121 17540 : move16();
3122 17540 : move32();
3123 17540 : move32();
3124 : }
3125 :
3126 19595 : return;
3127 : }
3128 :
3129 :
3130 : /*---------------------------------------------------------------------*
3131 : * IVAS_DEC_GetJbmData()
3132 : *
3133 : *
3134 : *---------------------------------------------------------------------*/
3135 :
3136 0 : ivas_error IVAS_DEC_GetJbmData(
3137 : IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
3138 : IVAS_JBM_TRACE_DATA *JbmTraceData /* o : JBM Trace data */
3139 :
3140 : )
3141 : {
3142 0 : IF( hIvasDec->hVoIP == NULL )
3143 : {
3144 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
3145 : }
3146 :
3147 0 : *JbmTraceData = hIvasDec->hVoIP->JbmTraceData;
3148 :
3149 0 : return IVAS_ERR_OK;
3150 : }
3151 :
3152 : #endif
3153 :
3154 :
3155 : /*---------------------------------------------------------------------*
3156 : * IVAS_DEC_GetErrorMessage( )
3157 : *
3158 : * Maps error codes to error description strings
3159 : *---------------------------------------------------------------------*/
3160 :
3161 6 : const char *IVAS_DEC_GetErrorMessage(
3162 : ivas_error error /* i : decoder error code enum */
3163 : )
3164 : {
3165 6 : return ivas_error_to_string( error );
3166 : }
3167 :
3168 :
3169 : /*---------------------------------------------------------------------*
3170 : * printConfigInfo_dec( )
3171 : *
3172 : *
3173 : *---------------------------------------------------------------------*/
3174 :
3175 604 : static ivas_error printConfigInfo_dec(
3176 : Decoder_Struct *st_ivas,
3177 : const Word16 bitstreamformat,
3178 : const bool Opt_VOIP,
3179 : const bool quietModeEnabled )
3180 : {
3181 : ivas_error error;
3182 : Word8 config_str[50];
3183 : AUDIO_CONFIG output_config;
3184 :
3185 : /*-----------------------------------------------------------------*
3186 : * Print info on screen
3187 : *-----------------------------------------------------------------*/
3188 :
3189 604 : fprintf( stdout, "\n" );
3190 :
3191 : /*-----------------------------------------------------------------*
3192 : * Print output sampling frequency
3193 : *-----------------------------------------------------------------*/
3194 :
3195 604 : fprintf( stdout, "Output sampling rate: %d Hz\n", st_ivas->hDecoderConfig->output_Fs );
3196 :
3197 : /*-----------------------------------------------------------------*
3198 : * Print bitrate
3199 : *-----------------------------------------------------------------*/
3200 :
3201 604 : IF( !quietModeEnabled )
3202 : {
3203 0 : IF( !Opt_VOIP )
3204 : {
3205 0 : fprintf( stdout, "Bitrate: %.2f kbps\n", (float) st_ivas->hDecoderConfig->ivas_total_brate / 1000 );
3206 :
3207 0 : IF( st_ivas->hDecoderConfig->ivas_total_brate <= 0 )
3208 : {
3209 0 : IF( EQ_16( bitstreamformat, G192 ) )
3210 : {
3211 0 : fprintf( stdout, "Active Bitrate not identified in bitstream file \n" );
3212 : }
3213 : ELSE /* MIME */
3214 : {
3215 0 : fprintf( stdout, "Active Bitrate not identified from first MIME frame \n" );
3216 : }
3217 : }
3218 : }
3219 : }
3220 :
3221 : /*-----------------------------------------------------------------*
3222 : * Print output configuration
3223 : *-----------------------------------------------------------------*/
3224 :
3225 604 : IF( EQ_16( (Word16) st_ivas->ivas_format, MONO_FORMAT ) )
3226 : {
3227 3 : IF( st_ivas->hDecoderConfig->Opt_non_diegetic_pan )
3228 : {
3229 1 : fprintf( stdout, "Output configuration: mono EVS bit-exact decoding to stereo\n" );
3230 1 : float non_diegetic_pan_gain = fixedToFloat( st_ivas->hDecoderConfig->non_diegetic_pan_gain_fx, 15 );
3231 1 : fprintf( stdout, "Non-diegetic panning: %.2f\n", non_diegetic_pan_gain * 90.f );
3232 : }
3233 : ELSE
3234 : {
3235 2 : fprintf( stdout, "Output configuration: mono EVS bit-exact decoding\n" );
3236 : }
3237 : }
3238 : ELSE
3239 : {
3240 601 : IF( !quietModeEnabled )
3241 : {
3242 0 : IF( EQ_16( (Word16) st_ivas->ivas_format, STEREO_FORMAT ) )
3243 : {
3244 0 : fprintf( stdout, "Input configuration: Stereo\n" );
3245 : }
3246 0 : ELSE IF( EQ_16( (Word16) st_ivas->ivas_format, ISM_FORMAT ) )
3247 : {
3248 0 : IF( EQ_16( (Word16) st_ivas->ism_mode, ISM_MODE_PARAM ) )
3249 : {
3250 0 : fprintf( stdout, "Input configuration: ISM (ParamISM): 2 transport channels\n" );
3251 : }
3252 : ELSE
3253 : {
3254 0 : fprintf( stdout, "Input configuration: ISM: %d transport channel(s)\n", st_ivas->nchan_transport );
3255 : }
3256 : }
3257 0 : ELSE IF( EQ_16( (Word16) st_ivas->ivas_format, SBA_FORMAT ) )
3258 : {
3259 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 );
3260 : }
3261 0 : ELSE IF( EQ_16( (Word16) st_ivas->ivas_format, SBA_ISM_FORMAT ) )
3262 : {
3263 0 : fprintf( stdout, "Input configuration: Combined Scene Based Audio, Ambisonic order %i, with %d Objects \n", st_ivas->sba_order, st_ivas->nchan_ism );
3264 : }
3265 0 : ELSE IF( EQ_16( (Word16) st_ivas->ivas_format, MASA_FORMAT ) )
3266 : {
3267 0 : fprintf( stdout, "Input configuration: MASA - %d channel(s)\n", st_ivas->nchan_transport );
3268 : }
3269 0 : ELSE IF( EQ_16( (Word16) st_ivas->ivas_format, MC_FORMAT ) )
3270 : {
3271 0 : IF( NE_32( ( error = get_channel_config( st_ivas->transport_config, &config_str[0] ) ), IVAS_ERR_OK ) )
3272 : {
3273 0 : return error;
3274 : }
3275 :
3276 0 : fprintf( stdout, "Input configuration: %s\n", config_str );
3277 : }
3278 0 : ELSE IF( EQ_16( (Word16) st_ivas->ivas_format, MASA_ISM_FORMAT ) )
3279 : {
3280 0 : fprintf( stdout, "Input configuration: combined ISM and MASA (%i ISM stream(s)) \n", st_ivas->nchan_ism );
3281 : }
3282 : }
3283 :
3284 601 : output_config = st_ivas->hDecoderConfig->output_config;
3285 601 : move16();
3286 601 : get_channel_config( output_config, &config_str[0] );
3287 601 : fprintf( stdout, "Output configuration: %s\n", config_str );
3288 :
3289 601 : test();
3290 601 : test();
3291 601 : IF( ( EQ_16( (Word16) output_config, IVAS_AUDIO_CONFIG_BINAURAL ) || EQ_16( (Word16) output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR ) || EQ_16( (Word16) output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) ) )
3292 : {
3293 193 : fprintf( stdout, "Render framesize: %dms\n", get_render_frame_size_ms( st_ivas->hDecoderConfig->render_framesize ) );
3294 : }
3295 601 : IF( st_ivas->hDecoderConfig->Opt_HRTF_binary )
3296 : {
3297 23 : fprintf( stdout, "HRIR/BRIR file: ON\n" );
3298 : }
3299 :
3300 601 : IF( st_ivas->hDecoderConfig->Opt_RendConfigCustom )
3301 : {
3302 29 : fprintf( stdout, "Renderer config. file: ON\n" );
3303 : }
3304 :
3305 601 : IF( st_ivas->hDecoderConfig->Opt_Headrotation )
3306 : {
3307 81 : fprintf( stdout, "Head rotation: ON\n" );
3308 : }
3309 :
3310 601 : IF( st_ivas->hDecoderConfig->Opt_ExternalOrientation )
3311 : {
3312 31 : fprintf( stdout, "External orientation: ON\n" );
3313 : }
3314 :
3315 601 : IF( NE_16( (Word16) st_ivas->hDecoderConfig->orientation_tracking, IVAS_HEAD_ORIENT_TRK_NONE ) )
3316 : {
3317 16 : SWITCH( st_ivas->hDecoderConfig->orientation_tracking )
3318 : {
3319 12 : case IVAS_HEAD_ORIENT_TRK_AVG:
3320 12 : fprintf( stdout, "Orientation tracking: AVG\n" );
3321 12 : BREAK;
3322 0 : case IVAS_HEAD_ORIENT_TRK_REF:
3323 0 : fprintf( stdout, "Orientation tracking: REF\n" );
3324 0 : BREAK;
3325 2 : case IVAS_HEAD_ORIENT_TRK_REF_VEC:
3326 2 : fprintf( stdout, "Orientation tracking: REF_VEC\n" );
3327 2 : BREAK;
3328 2 : case IVAS_HEAD_ORIENT_TRK_REF_VEC_LEV:
3329 2 : fprintf( stdout, "Orientation tracking: REF_VEC_LEV\n" );
3330 2 : BREAK;
3331 0 : default:
3332 0 : BREAK;
3333 : }
3334 585 : }
3335 :
3336 601 : IF( st_ivas->hDecoderConfig->Opt_non_diegetic_pan )
3337 : {
3338 1 : float non_diegetic_pan_gain = fixedToFloat( st_ivas->hDecoderConfig->non_diegetic_pan_gain_fx, 15 );
3339 1 : fprintf( stdout, "Non-diegetic panning: %.2f\n", non_diegetic_pan_gain * 90.f );
3340 : }
3341 :
3342 601 : IF( st_ivas->hDecoderConfig->Opt_dpid_on )
3343 : {
3344 2 : fprintf( stdout, "Directivity pattern: ON\n" );
3345 : }
3346 :
3347 601 : IF( st_ivas->hDecoderConfig->Opt_aeid_on )
3348 : {
3349 1 : fprintf( stdout, "Acoustic environment ID:ON\n" );
3350 : }
3351 : }
3352 :
3353 : /*-----------------------------------------------------------------*
3354 : * Print TSM mode info
3355 : *-----------------------------------------------------------------*/
3356 :
3357 604 : IF( st_ivas->hDecoderConfig->Opt_tsm )
3358 : {
3359 27 : fprintf( stdout, "TSM mode: ON\n" );
3360 : }
3361 :
3362 604 : return IVAS_ERR_OK;
3363 : }
3364 :
3365 :
3366 : /*---------------------------------------------------------------------*
3367 : * IVAS_DEC_PrintConfig( )
3368 : *
3369 : * Print decoder set-up info
3370 : *---------------------------------------------------------------------*/
3371 :
3372 604 : void IVAS_DEC_PrintConfig(
3373 : const IVAS_DEC_HANDLE hIvasDec,
3374 : const bool quietModeEnabled,
3375 : const bool voipMode )
3376 : {
3377 604 : printConfigInfo_dec( hIvasDec->st_ivas, hIvasDec->bitstreamformat, voipMode, quietModeEnabled );
3378 :
3379 604 : return;
3380 : }
3381 :
3382 :
3383 : /*---------------------------------------------------------------------*
3384 : * IVAS_DEC_PrintDisclaimer( )
3385 : *
3386 : * Print IVAS disclaimer to console
3387 : *---------------------------------------------------------------------*/
3388 :
3389 620 : void IVAS_DEC_PrintDisclaimer( void )
3390 : {
3391 620 : print_disclaimer( stderr );
3392 :
3393 620 : return;
3394 : }
3395 :
3396 :
3397 : /*---------------------------------------------------------------------*
3398 : * evs_dec_main( )
3399 : *
3400 : * EVS codec main decoder fucntion
3401 : *---------------------------------------------------------------------*/
3402 :
3403 3100 : static ivas_error evs_dec_main_fx(
3404 : Decoder_Struct *st_ivas,
3405 : const Word16 nOutSamples,
3406 : Word32 *Buf_fx, // Q11
3407 : Word16 *pcmBuf // Q0
3408 : )
3409 : {
3410 : DEC_CORE_HANDLE *hCoreCoder;
3411 : Word16 mixer_left_fx, mixer_right_fx;
3412 : Word32 *p_output_fx[MAX_OUTPUT_CHANNELS_IN_DIEGETIC_PAN];
3413 : Word16 ch, n, i;
3414 : Word16 output_16[L_FRAME48k];
3415 : ivas_error error;
3416 :
3417 3100 : hCoreCoder = st_ivas->hSCE[0]->hCoreCoder;
3418 3100 : hCoreCoder[0]->total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
3419 3100 : hCoreCoder[0]->total_num_bits = st_ivas->num_bits;
3420 3100 : move16();
3421 3100 : move32();
3422 3100 : hCoreCoder[0]->output_frame_fx = extract_l( Mult_32_16( hCoreCoder[0]->output_Fs, 0x0290 /*Q0*/ ) ); // Q0
3423 3100 : move16();
3424 3100 : mdct_switching_dec_fx( hCoreCoder[0] );
3425 :
3426 9300 : FOR( ch = 0; ch < MAX_OUTPUT_CHANNELS_IN_DIEGETIC_PAN; ch++ )
3427 : {
3428 6200 : p_output_fx[ch] = st_ivas->p_output_fx[ch]; // Q0
3429 : }
3430 :
3431 : /* run the main EVS decoding routine */
3432 3100 : IF( EQ_16( hCoreCoder[0]->codec_mode, MODE1 ) )
3433 : {
3434 1852 : IF( hCoreCoder[0]->Opt_AMR_WB )
3435 : {
3436 0 : IF( NE_32( ( error = amr_wb_dec_fx( output_16, hCoreCoder[0] ) ), IVAS_ERR_OK ) )
3437 : {
3438 0 : return error;
3439 : }
3440 : }
3441 : ELSE
3442 : {
3443 1852 : IF( NE_32( ( error = evs_dec_fx( hCoreCoder[0], output_16, FRAMEMODE_NORMAL ) ), IVAS_ERR_OK ) )
3444 : {
3445 0 : return error;
3446 : }
3447 : }
3448 : }
3449 : ELSE
3450 : {
3451 1248 : IF( hCoreCoder[0]->bfi == 0 )
3452 : {
3453 1248 : IF( NE_32( ( error = evs_dec_fx( hCoreCoder[0], output_16, FRAMEMODE_NORMAL ) ), IVAS_ERR_OK ) )
3454 : {
3455 0 : return error;
3456 : }
3457 : }
3458 0 : ELSE IF( EQ_16( hCoreCoder[0]->bfi, 2 ) )
3459 : {
3460 0 : IF( NE_32( ( error = evs_dec_fx( hCoreCoder[0], output_16, FRAMEMODE_FUTURE ) ), IVAS_ERR_OK ) )
3461 : {
3462 0 : return error;
3463 : }
3464 : }
3465 : ELSE
3466 : {
3467 0 : IF( NE_32( ( error = evs_dec_fx( hCoreCoder[0], output_16, FRAMEMODE_MISSING ) ), IVAS_ERR_OK ) )
3468 : {
3469 0 : return error;
3470 : }
3471 : }
3472 : }
3473 :
3474 2643100 : FOR( i = 0; i < nOutSamples; i++ )
3475 : {
3476 2640000 : p_output_fx[0][i] = L_shl( output_16[i], Q11 ); /* Q11 */
3477 : }
3478 :
3479 3100 : st_ivas->BER_detect = hCoreCoder[0]->BER_detect;
3480 3100 : move16();
3481 3100 : IF( EQ_16( st_ivas->renderer_type, RENDERER_NON_DIEGETIC_DOWNMIX ) )
3482 : {
3483 1000 : mixer_left_fx = add( shr( st_ivas->hDecoderConfig->non_diegetic_pan_gain_fx, Q1 ), ONE_IN_Q14 ); // Q14
3484 1000 : mixer_right_fx = sub( MAX16B, mixer_left_fx );
3485 :
3486 1000 : v_multc_fixed_16( p_output_fx[0], mixer_right_fx, p_output_fx[1], nOutSamples ); /* Q11 */
3487 1000 : v_multc_fixed_16( p_output_fx[0], mixer_left_fx, p_output_fx[0], nOutSamples ); /* Q11 */
3488 : }
3489 :
3490 3100 : IF( !st_ivas->hDecoderConfig->Opt_tsm )
3491 : {
3492 3100 : ivas_jbm_dec_copy_tc_no_tsm_fx( st_ivas, p_output_fx, nOutSamples );
3493 : }
3494 0 : ELSE IF( Buf_fx != NULL )
3495 : {
3496 0 : FOR( n = 0; n < st_ivas->hDecoderConfig->nchan_out; n++ )
3497 : {
3498 0 : FOR( i = 0; i < nOutSamples; i++ )
3499 : {
3500 0 : Buf_fx[i * st_ivas->hDecoderConfig->nchan_out + n] = p_output_fx[n][i]; /* Does the same as above */ /* Q11 */
3501 0 : move32();
3502 : }
3503 : }
3504 : }
3505 : ELSE
3506 : {
3507 : /* ivas_syn_output( p_output, nOutSamples, st_ivas->hDecoderConfig->nchan_out, pcmBuf ); */
3508 0 : FOR( n = 0; n < st_ivas->hDecoderConfig->nchan_out; n++ )
3509 : {
3510 0 : FOR( i = 0; i < nOutSamples; i++ )
3511 : {
3512 0 : pcmBuf[i * st_ivas->hDecoderConfig->nchan_out + n] = extract_l( L_shr( p_output_fx[n][i], Q11 ) ); /* Does the same as above */ /* Q0 */
3513 0 : move16();
3514 : }
3515 : }
3516 : }
3517 :
3518 3100 : return IVAS_ERR_OK;
3519 : }
3520 :
3521 :
3522 : /*---------------------------------------------------------------------*
3523 : * input_format_API_to_internal()
3524 : *
3525 : *
3526 : *---------------------------------------------------------------------*/
3527 :
3528 27 : static ivas_error input_format_API_to_internal(
3529 : IVAS_DEC_INPUT_FORMAT input_format,
3530 : Word16 *bitstream_format_internal,
3531 : Word16 *sdp_hf_only,
3532 : const bool is_voip_enabled )
3533 : {
3534 27 : SWITCH( input_format )
3535 : {
3536 27 : case IVAS_DEC_INPUT_FORMAT_G192:
3537 27 : IF( is_voip_enabled )
3538 : {
3539 27 : *bitstream_format_internal = VOIP_G192_RTP;
3540 : }
3541 : ELSE
3542 : {
3543 0 : *bitstream_format_internal = G192;
3544 : }
3545 27 : *sdp_hf_only = 0;
3546 27 : BREAK;
3547 0 : case IVAS_DEC_INPUT_FORMAT_MIME:
3548 0 : *bitstream_format_internal = MIME;
3549 0 : *sdp_hf_only = 0;
3550 0 : BREAK;
3551 0 : case IVAS_DEC_INPUT_FORMAT_RTPDUMP:
3552 0 : assert( is_voip_enabled && "RTP dump only supported in VoIP mode" );
3553 0 : *bitstream_format_internal = VOIP_RTPDUMP;
3554 0 : *sdp_hf_only = 0;
3555 0 : BREAK;
3556 0 : case IVAS_DEC_INPUT_FORMAT_RTPDUMP_HF:
3557 0 : assert( is_voip_enabled && "RTP dump only supported in VoIP mode" );
3558 0 : *bitstream_format_internal = VOIP_RTPDUMP;
3559 0 : *sdp_hf_only = 1;
3560 0 : BREAK;
3561 0 : default:
3562 0 : return IVAS_ERR_INVALID_BITSTREAM;
3563 : }
3564 :
3565 27 : move16();
3566 27 : move16();
3567 27 : return IVAS_ERR_OK;
3568 : }
3569 :
3570 :
3571 : /*---------------------------------------------------------------------*
3572 : * IVAS_DEC_VoIP_reconfigure()
3573 : *
3574 : *
3575 : *---------------------------------------------------------------------*/
3576 :
3577 953 : static ivas_error IVAS_DEC_VoIP_reconfigure(
3578 : IVAS_DEC_HANDLE hIvasDec,
3579 : const UWord16 nTransportChannels,
3580 : const UWord16 l_ts )
3581 : {
3582 : Word16 apa_buffer_size;
3583 :
3584 953 : apa_buffer_size = hIvasDec->nSamplesFrame;
3585 953 : move16();
3586 :
3587 953 : IF( hIvasDec->apaExecBuffer_fx == NULL )
3588 : {
3589 : DECODER_CONFIG_HANDLE hDecoderConfig;
3590 :
3591 27 : IF( hIvasDec->st_ivas->hDecoderConfig->Opt_tsm )
3592 : {
3593 : UWord16 wss, css;
3594 : Word32 startQuality;
3595 :
3596 27 : startQuality = L_shl( L_deposit_l( hIvasDec->tsm_quality ), Q2 ); /* Q14 --> Q16*/
3597 27 : apa_buffer_size = APA_BUF_PER_CHANNEL;
3598 :
3599 27 : move16();
3600 : /* get current renderer type*/
3601 27 : hDecoderConfig = hIvasDec->st_ivas->hDecoderConfig;
3602 :
3603 27 : IF( EQ_32( hDecoderConfig->output_Fs, 8000 ) )
3604 : {
3605 0 : wss = 1;
3606 0 : css = 1;
3607 : }
3608 27 : ELSE IF( EQ_32( hDecoderConfig->output_Fs, 16000 ) )
3609 : {
3610 3 : wss = 2;
3611 3 : css = 1;
3612 : }
3613 24 : ELSE IF( EQ_32( hDecoderConfig->output_Fs, 32000 ) )
3614 : {
3615 8 : wss = 4;
3616 8 : css = 2;
3617 : }
3618 16 : ELSE IF( EQ_32( hDecoderConfig->output_Fs, 48000 ) )
3619 : {
3620 16 : wss = 6;
3621 16 : css = 3;
3622 : }
3623 : ELSE
3624 : {
3625 0 : return IVAS_ERR_INIT_ERROR;
3626 : }
3627 :
3628 27 : move16();
3629 27 : move16();
3630 27 : test();
3631 27 : test();
3632 27 : test();
3633 27 : test();
3634 27 : IF( NE_32( (Word32) apa_init( &hIvasDec->hTimeScaler, nTransportChannels ), IVAS_ERR_OK ) ||
3635 : apa_set_rate( hIvasDec->hTimeScaler, hDecoderConfig->output_Fs ) != 0 ||
3636 : apa_set_complexity_options( hIvasDec->hTimeScaler, wss, css ) != 0 ||
3637 : apa_set_quality( hIvasDec->hTimeScaler, startQuality, 4, 4 ) != 0 ||
3638 : apa_set_renderer_granularity( hIvasDec->hTimeScaler, l_ts ) != 0 )
3639 : {
3640 0 : return IVAS_ERR_INIT_ERROR;
3641 : }
3642 :
3643 27 : IF( EQ_16( (Word16) hIvasDec->mode, IVAS_DEC_MODE_EVS ) )
3644 : {
3645 0 : IF( apa_set_evs_compat_mode( hIvasDec->hTimeScaler, true ) != 0 )
3646 : {
3647 0 : return IVAS_ERR_INIT_ERROR;
3648 : }
3649 : }
3650 :
3651 27 : IF( ( hIvasDec->apaExecBuffer_fx = malloc( sizeof( Word32 ) * L_mult0( apa_buffer_size, (Word16) nTransportChannels ) ) ) == NULL )
3652 : {
3653 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Could not allocate VoIP handle" );
3654 : }
3655 :
3656 27 : set_zero2_fx( hIvasDec->apaExecBuffer_fx, L_mult0( apa_buffer_size, (Word16) nTransportChannels ) );
3657 : }
3658 : }
3659 : ELSE
3660 : {
3661 926 : IF( hIvasDec->st_ivas->hDecoderConfig->Opt_tsm )
3662 : {
3663 926 : IF( apa_reconfigure( hIvasDec->hTimeScaler, nTransportChannels, l_ts ) != 0 )
3664 : {
3665 0 : return IVAS_ERR_INIT_ERROR;
3666 : }
3667 926 : apa_buffer_size = APA_BUF_PER_CHANNEL;
3668 926 : move16();
3669 926 : free( hIvasDec->apaExecBuffer_fx );
3670 926 : IF( ( hIvasDec->apaExecBuffer_fx = malloc( sizeof( Word32 ) * L_mult0( apa_buffer_size, (Word16) nTransportChannels ) ) ) == NULL )
3671 : {
3672 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Could not allocate VoIP handle" );
3673 : }
3674 926 : set_zero2_fx( hIvasDec->apaExecBuffer_fx, L_mult0( apa_buffer_size, (Word16) nTransportChannels ) );
3675 : }
3676 : /* realloc apa_exe_buffer */
3677 : }
3678 :
3679 953 : hIvasDec->nTransportChannelsOld = nTransportChannels;
3680 953 : move16();
3681 :
3682 953 : return IVAS_ERR_OK;
3683 : }
|