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_enc.h"
34 : #include "ivas_prot.h"
35 : #include "prot.h"
36 : #include <math.h>
37 : #include <assert.h>
38 : #include <stdio.h>
39 : #include <string.h>
40 : #ifdef DEBUGGING
41 : #include "debug.h"
42 : #endif
43 : #include "wmc_auto.h"
44 : #include "options.h"
45 :
46 : /*---------------------------------------------------------------------*
47 : * Local struct
48 : *---------------------------------------------------------------------*/
49 :
50 : struct IVAS_ENC
51 : {
52 : Encoder_Struct *st_ivas;
53 : ENC_CORE_HANDLE hCoreCoder;
54 : bool isConfigured;
55 : #ifdef DEBUGGING
56 : bool cmd_stereo;
57 : #endif
58 : bool switchingActive; /* flag for configuration changes during encoding - currently only used with mono */
59 : int16_t Opt_RF_ON_loc;
60 : int16_t rf_fec_offset_loc;
61 : bool ismMetadataProvided[MAX_NUM_OBJECTS];
62 : bool maxBandwidthUser; /* Was a specific max bandwith selected by the user? */
63 : IVAS_ENC_BANDWIDTH newBandwidthApi; /* maximum encoded bandwidth, as set on API level */
64 : bool extMetadataApi; /* External metadata requested, to be checked against current bit rate */
65 : };
66 :
67 : /*---------------------------------------------------------------------*
68 : * Local functions
69 : *---------------------------------------------------------------------*/
70 :
71 : static ivas_error configureEncoder( IVAS_ENC_HANDLE hIvasEnc, const int32_t inputFs, const int32_t initBitrate, const IVAS_ENC_BANDWIDTH initBandwidth, const IVAS_ENC_DTX_CONFIG dtxConfig, const IVAS_ENC_CHANNEL_AWARE_CONFIG caConfig );
72 : static ivas_error setBandwidth( IVAS_ENC_HANDLE hIvasEnc, const IVAS_ENC_BANDWIDTH maxBandwidth );
73 : static ivas_error setBitrate( IVAS_ENC_HANDLE hIvasEnc, const int32_t totalBitrate );
74 : static ivas_error setChannelAwareConfig( IVAS_ENC_HANDLE hIvasEnc, const IVAS_ENC_CHANNEL_AWARE_CONFIG caConfig );
75 : static int16_t getInputBufferSize( const Encoder_Struct *st_ivas );
76 : static ivas_error doCommonConfigureChecks( IVAS_ENC_HANDLE hIvasEnc );
77 : static ivas_error doCommonSetterChecks( IVAS_ENC_HANDLE hIvasEnc );
78 : static ivas_error sanitizeBandwidth( const IVAS_ENC_HANDLE hIvasEnc );
79 : static ivas_error sanitizeBitrateISM( const ENCODER_CONFIG_HANDLE hEncoderConfig, const bool extMetadataApi );
80 : static void init_encoder_config( ENCODER_CONFIG_HANDLE hEncoderConfig );
81 : static void resetIsmMetadataProvidedFlags( IVAS_ENC_HANDLE hIvasEnc );
82 : static ivas_error bandwidthApiToInternal( const IVAS_ENC_BANDWIDTH maxBandwidth, int16_t *internalMaxBandwidth );
83 : #ifdef DEBUG_AGC_ENCODER_CMD_OPTION
84 : static ivas_error agcAPIToInternal( const IVAS_ENC_AGC agcOption, int16_t *internalAGCOption );
85 : #endif
86 : static ivas_error fecIndicatorApiToInternal( const IVAS_ENC_FEC_INDICATOR fecIndicator, int16_t *fecIndicatorInternal );
87 : #ifdef DEBUGGING
88 : static ivas_error forcedModeApiToInternal( IVAS_ENC_FORCED_MODE forcedMode, int16_t *forcedModeInternal );
89 : #endif
90 :
91 :
92 : /*---------------------------------------------------------------------*
93 : * IVAS_ENC_Open()
94 : *
95 : * Open IVAS encoder
96 : *---------------------------------------------------------------------*/
97 :
98 627 : ivas_error IVAS_ENC_Open(
99 : IVAS_ENC_HANDLE *phIvasEnc /* i/o: pointer to an encoder handle to be opened */
100 : )
101 : {
102 : Encoder_Struct *st_ivas;
103 :
104 627 : if ( phIvasEnc == NULL )
105 : {
106 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
107 : }
108 :
109 : /*-----------------------------------------------------------------*
110 : * Allocate and initialize IVAS application encoder handle
111 : *-----------------------------------------------------------------*/
112 :
113 627 : if ( ( *phIvasEnc = (IVAS_ENC_HANDLE) malloc( sizeof( struct IVAS_ENC ) ) ) == NULL )
114 : {
115 0 : return IVAS_ERR_FAILED_ALLOC;
116 : }
117 :
118 627 : ( *phIvasEnc )->hCoreCoder = NULL;
119 627 : ( *phIvasEnc )->isConfigured = false;
120 : #ifdef DEBUGGING
121 : ( *phIvasEnc )->cmd_stereo = false;
122 : #endif
123 627 : ( *phIvasEnc )->switchingActive = false;
124 627 : ( *phIvasEnc )->maxBandwidthUser = false;
125 627 : resetIsmMetadataProvidedFlags( *phIvasEnc );
126 :
127 : /*-----------------------------------------------------------------*
128 : * Allocate IVAS-codec encoder state
129 : *-----------------------------------------------------------------*/
130 :
131 627 : if ( ( ( *phIvasEnc )->st_ivas = (Encoder_Struct *) malloc( sizeof( Encoder_Struct ) ) ) == NULL )
132 : {
133 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for IVAS encoder structure" );
134 : }
135 :
136 627 : if ( ( ( *phIvasEnc )->st_ivas->hEncoderConfig = (ENCODER_CONFIG_HANDLE) malloc( sizeof( ENCODER_CONFIG ) ) ) == NULL )
137 : {
138 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for Encoder config structure" );
139 : }
140 :
141 : /*-----------------------------------------------------------------*
142 : * Initialize IVAS-codec encoder state
143 : *-----------------------------------------------------------------*/
144 :
145 627 : st_ivas = ( *phIvasEnc )->st_ivas;
146 :
147 : /* initialize encoder Config. handle */
148 627 : init_encoder_config( st_ivas->hEncoderConfig );
149 :
150 : /* initialize pointers to handles to NULL */
151 627 : ivas_initialize_handles_enc( st_ivas );
152 :
153 627 : st_ivas->ind_list = NULL;
154 627 : st_ivas->ind_list_metadata = NULL;
155 :
156 : /* set high-level parameters */
157 627 : st_ivas->mc_mode = MC_MODE_NONE;
158 627 : st_ivas->ism_mode = ISM_MODE_NONE;
159 627 : st_ivas->sba_analysis_order = 0;
160 :
161 627 : return IVAS_ERR_OK;
162 : }
163 :
164 :
165 : /*---------------------------------------------------------------------*
166 : * IVAS_ENC_Close()
167 : *
168 : *
169 : *---------------------------------------------------------------------*/
170 :
171 627 : void IVAS_ENC_Close(
172 : IVAS_ENC_HANDLE *phIvasEnc /* i/o: pointer to IVAS encoder handle */
173 : )
174 : {
175 : /* Free all memory */
176 627 : if ( phIvasEnc == NULL || *phIvasEnc == NULL )
177 : {
178 0 : return;
179 : }
180 :
181 627 : if ( ( *phIvasEnc )->isConfigured )
182 : {
183 627 : ivas_destroy_enc( ( *phIvasEnc )->st_ivas );
184 : }
185 : else
186 : {
187 0 : if ( ( *phIvasEnc )->st_ivas->hEncoderConfig )
188 : {
189 0 : free( ( *phIvasEnc )->st_ivas->hEncoderConfig );
190 0 : ( *phIvasEnc )->st_ivas->hEncoderConfig = NULL;
191 : }
192 0 : free( ( *phIvasEnc )->st_ivas );
193 : }
194 :
195 627 : ( *phIvasEnc )->st_ivas = NULL;
196 :
197 627 : free( *phIvasEnc );
198 :
199 627 : *phIvasEnc = NULL;
200 627 : phIvasEnc = NULL;
201 :
202 627 : return;
203 : }
204 :
205 :
206 : /*---------------------------------------------------------------------*
207 : * IVAS_ENC_ConfigureForMono()
208 : *
209 : * Configure and initialize the mono encoder.
210 : *---------------------------------------------------------------------*/
211 :
212 3 : ivas_error IVAS_ENC_ConfigureForMono(
213 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
214 : const int32_t inputFs, /* i : input sampling frequency */
215 : const int32_t bitrate, /* i : requested bitrate of the output bitstream */
216 : const bool max_bwidth_user, /* i : shows if bandwidth limitation was set by the user (true) or if default bandwidth was used (false) */
217 : const IVAS_ENC_BANDWIDTH maxBandwidth, /* i : bandwidth limitation */
218 : const IVAS_ENC_DTX_CONFIG dtxConfig, /* i : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig() */
219 : const IVAS_ENC_CHANNEL_AWARE_CONFIG caConfig, /* i : configuration of channel-aware mode, can by set to default by using IVAS_ENC_GetDefaultChannelAwareConfig() */
220 : const bool downmixFromStereo, /* i : if true, the encoder accepts a stereo input and internally downmixes it to mono before encoding */
221 : const bool is_binaural /* i : if true, the input is binaural audio */
222 : )
223 : {
224 : ivas_error error;
225 :
226 3 : error = IVAS_ERR_OK;
227 :
228 3 : if ( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
229 : {
230 0 : return error;
231 : }
232 :
233 3 : hIvasEnc->st_ivas->hEncoderConfig->ivas_format = MONO_FORMAT;
234 3 : hIvasEnc->st_ivas->hEncoderConfig->is_binaural = (int16_t) is_binaural;
235 :
236 3 : if ( downmixFromStereo )
237 : {
238 2 : hIvasEnc->st_ivas->hEncoderConfig->stereo_dmx_evs = 1;
239 : }
240 3 : hIvasEnc->maxBandwidthUser = max_bwidth_user;
241 :
242 3 : error = configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, caConfig );
243 :
244 3 : return error;
245 : }
246 :
247 :
248 : /*---------------------------------------------------------------------*
249 : * IVAS_ENC_ConfigureForStereo()
250 : *
251 : * Configure and initialize the stereo encoder.
252 : *---------------------------------------------------------------------*/
253 :
254 68 : ivas_error IVAS_ENC_ConfigureForStereo(
255 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
256 : const int32_t inputFs, /* i : input sampling frequency */
257 : const int32_t bitrate, /* i : requested bitrate of the output bitstream */
258 : const bool max_bwidth_user, /* i : shows if bandwidth limitation was set by the user (true) or if default bandwidth was used (false) */
259 : const IVAS_ENC_BANDWIDTH maxBandwidth, /* i : bandwidth limitation */
260 : const IVAS_ENC_DTX_CONFIG dtxConfig, /* i : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig() */
261 : const bool is_binaural /* i : flag indicating if input is binaural audio */
262 : #ifdef DEBUGGING
263 : ,
264 : const IVAS_ENC_STEREO_MODE stereoMode /* i : forces a specific stereo coding mode */
265 : #endif
266 : )
267 : {
268 : Encoder_Struct *st_ivas;
269 : ENCODER_CONFIG_HANDLE hEncoderConfig;
270 :
271 : ivas_error error;
272 :
273 68 : if ( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
274 : {
275 0 : return error;
276 : }
277 :
278 68 : st_ivas = hIvasEnc->st_ivas;
279 68 : hEncoderConfig = st_ivas->hEncoderConfig;
280 68 : hEncoderConfig->nchan_inp = 2;
281 68 : hEncoderConfig->ivas_format = STEREO_FORMAT;
282 68 : hEncoderConfig->is_binaural = (int16_t) is_binaural;
283 :
284 : #ifdef DEBUGGING
285 : switch ( stereoMode )
286 : {
287 : case IVAS_ENC_STEREO_MODE_UNIFIED:
288 : hEncoderConfig->stereo_mode_cmdl = 1; /* set unified stereo by default */
289 : hEncoderConfig->element_mode_init = IVAS_CPE_DFT;
290 : hIvasEnc->cmd_stereo = true;
291 : break;
292 : case IVAS_ENC_STEREO_MODE_DFT:
293 : hEncoderConfig->element_mode_init = IVAS_CPE_DFT;
294 : hEncoderConfig->stereo_mode_cmdl = IVAS_CPE_DFT;
295 : break;
296 : case IVAS_ENC_STEREO_MODE_TD:
297 : hEncoderConfig->stereo_mode_cmdl = IVAS_CPE_TD;
298 : hEncoderConfig->element_mode_init = IVAS_CPE_TD;
299 : break;
300 : case IVAS_ENC_STEREO_MODE_MDCT_DECISION:
301 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
302 : hEncoderConfig->mdct_stereo_mode_cmdl = SMDCT_MS_DECISION;
303 : break;
304 : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
305 : case IVAS_ENC_STEREO_MODE_MDCT_FORCE_LR:
306 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
307 : hEncoderConfig->mdct_stereo_mode_cmdl = SMDCT_FORCE_LR;
308 : break;
309 : case IVAS_ENC_STEREO_MODE_MDCT_FORCE_MS:
310 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
311 : hEncoderConfig->mdct_stereo_mode_cmdl = SMDCT_FORCE_MS;
312 : break;
313 : #endif
314 : default:
315 : return IVAS_ERR_INVALID_STEREO_MODE;
316 : break;
317 : }
318 : #endif /* DEBUGGING */
319 :
320 68 : hIvasEnc->maxBandwidthUser = max_bwidth_user;
321 :
322 68 : error = configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
323 :
324 68 : return error;
325 : }
326 :
327 :
328 : /*---------------------------------------------------------------------*
329 : * IVAS_ENC_ConfigureForMASAObjects()
330 : *
331 : * Configure and initialize the combined MASA and ISM encoder.
332 : *---------------------------------------------------------------------*/
333 :
334 44 : ivas_error IVAS_ENC_ConfigureForMASAObjects(
335 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
336 : const int32_t inputFs, /* i : input sampling frequency */
337 : const int32_t bitrate, /* i : requested bitrate of the ouput bitstream */
338 : const IVAS_ENC_BANDWIDTH maxBandwidth, /* i : bandwidth limitation */
339 : const IVAS_ENC_DTX_CONFIG dtxConfig, /* i : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig() */
340 : const uint16_t numObjects, /* i : number of objects to be encoded */
341 : const int16_t masaVariant /* i : index specifying the number of MASA transport channels */
342 : )
343 : {
344 : Encoder_Struct *st_ivas;
345 : ivas_error error;
346 :
347 44 : if ( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
348 : {
349 0 : return error;
350 : }
351 :
352 44 : if ( numObjects > MAX_NUM_OBJECTS )
353 : {
354 0 : return IVAS_ERR_TOO_MANY_INPUTS;
355 : }
356 44 : st_ivas = hIvasEnc->st_ivas;
357 44 : switch ( masaVariant )
358 : {
359 32 : case IVAS_ENC_MASA_2CH:
360 32 : st_ivas->hEncoderConfig->nchan_inp = CPE_CHANNELS + numObjects;
361 32 : st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_DFT; /* initialization only, might be changed later based on element_brate */
362 32 : break;
363 12 : case IVAS_ENC_MASA_1CH:
364 12 : st_ivas->hEncoderConfig->nchan_inp = 1 + numObjects;
365 12 : st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_DFT; /* initialization only, might be changed later based on element_brate */
366 12 : break;
367 0 : default:
368 0 : return IVAS_ERR_INVALID_MASA_CONFIG;
369 : break;
370 : }
371 :
372 44 : st_ivas = hIvasEnc->st_ivas;
373 :
374 : /* Currently this is true but it is already shown in descriptive metadata that there can be inequality for this. */
375 44 : st_ivas->nchan_transport = st_ivas->hEncoderConfig->nchan_inp - numObjects;
376 44 : st_ivas->hEncoderConfig->ivas_format = MASA_ISM_FORMAT;
377 44 : st_ivas->hEncoderConfig->nchan_ism = numObjects;
378 :
379 44 : return configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
380 : }
381 :
382 :
383 : /*---------------------------------------------------------------------*
384 : * IVAS_ENC_ConfigureForObjects()
385 : *
386 : * Configure and initialize the ISM encoder.
387 : *---------------------------------------------------------------------*/
388 :
389 74 : ivas_error IVAS_ENC_ConfigureForObjects(
390 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
391 : const int32_t inputFs, /* i : input sampling frequency */
392 : const int32_t bitrate, /* i : requested bitrate of the output bitstream */
393 : const bool max_bwidth_user, /* i : shows if bandwidth limitation was set by the user (true) or if default bandwidth was used (false) */
394 : const IVAS_ENC_BANDWIDTH maxBandwidth, /* i : bandwidth limitation */
395 : const IVAS_ENC_DTX_CONFIG dtxConfig, /* i : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig() */
396 : const uint16_t numObjects, /* i : number of objects to be encoded */
397 : const bool ism_extended_metadata /* i : Extended metadata used (true/false), where extended metadata includes radius and orientation */
398 : )
399 : {
400 : Encoder_Struct *st_ivas;
401 : ivas_error error;
402 :
403 74 : if ( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
404 : {
405 0 : return error;
406 : }
407 :
408 74 : if ( numObjects > MAX_NUM_OBJECTS )
409 : {
410 0 : return IVAS_ERR_TOO_MANY_INPUTS;
411 : }
412 :
413 74 : st_ivas = hIvasEnc->st_ivas;
414 74 : st_ivas->hEncoderConfig->ivas_format = ISM_FORMAT;
415 74 : st_ivas->hEncoderConfig->element_mode_init = IVAS_SCE;
416 74 : st_ivas->hEncoderConfig->nchan_inp = numObjects;
417 74 : st_ivas->hEncoderConfig->nchan_ism = numObjects;
418 74 : st_ivas->hEncoderConfig->ism_extended_metadata_flag = ism_extended_metadata;
419 74 : hIvasEnc->extMetadataApi = ( ism_extended_metadata == 1 );
420 74 : hIvasEnc->maxBandwidthUser = max_bwidth_user;
421 :
422 74 : error = configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
423 :
424 74 : return error;
425 : }
426 :
427 :
428 : /*---------------------------------------------------------------------*
429 : * IVAS_ENC_FeedObjectMetadata()
430 : *
431 : *
432 : *---------------------------------------------------------------------*/
433 :
434 433276 : ivas_error IVAS_ENC_FeedObjectMetadata(
435 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
436 : const uint16_t ismIndex, /* i : object index */
437 : const IVAS_ISM_METADATA metadata /* i : object metadata handle for current frame */
438 : )
439 : {
440 : ivas_error error;
441 :
442 433276 : error = IVAS_ERR_OK;
443 :
444 433276 : if ( !hIvasEnc->isConfigured )
445 : {
446 0 : return IVAS_ERR_NOT_CONFIGURED;
447 : }
448 :
449 433276 : if ( hIvasEnc->st_ivas->hEncoderConfig->ivas_format != ISM_FORMAT && hIvasEnc->st_ivas->hEncoderConfig->ivas_format != MASA_ISM_FORMAT && hIvasEnc->st_ivas->hEncoderConfig->ivas_format != SBA_ISM_FORMAT )
450 : {
451 0 : return IVAS_ERR_METADATA_NOT_EXPECTED;
452 : }
453 :
454 433276 : if ( ismIndex > hIvasEnc->st_ivas->hEncoderConfig->nchan_inp )
455 : {
456 0 : return IVAS_ERR_INVALID_INDEX;
457 : }
458 :
459 433276 : error = ivas_set_ism_metadata( hIvasEnc->st_ivas->hIsmMetaData[ismIndex], metadata.azimuth, metadata.elevation, metadata.radius, metadata.yaw, metadata.pitch, metadata.non_diegetic_flag );
460 :
461 433276 : if ( error != IVAS_ERR_OK )
462 : {
463 0 : return error;
464 : }
465 :
466 433276 : hIvasEnc->ismMetadataProvided[ismIndex] = true;
467 :
468 433276 : return error;
469 : }
470 :
471 :
472 : /*---------------------------------------------------------------------*
473 : * IVAS_ENC_ConfigureForAmbisonics()
474 : *
475 : * Configure and initialize the SBA encoder.
476 : *---------------------------------------------------------------------*/
477 :
478 244 : ivas_error IVAS_ENC_ConfigureForAmbisonics(
479 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
480 : const int32_t inputFs, /* i : input sampling frequency */
481 : const int32_t bitrate, /* i : requested bitrate of the output bitstream */
482 : const bool max_bwidth_user, /* i : shows if bandwidth limitation was set by the user (true) or if default bandwidth was used (false) */
483 : const IVAS_ENC_BANDWIDTH maxBandwidth, /* i : bandwidth limitation */
484 : const IVAS_ENC_DTX_CONFIG dtxConfig, /* i : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig() */
485 : const IVAS_ENC_SBA_ORDER order, /* i : order of the Ambisonics input */
486 : const bool isPlanar, /* i : if true, input is treated as planar Ambisonics */
487 : #ifdef DEBUG_AGC_ENCODER_CMD_OPTION
488 : const IVAS_ENC_AGC Opt_AGC_ON, /* i : AGC on/off/undefined flag */
489 : #endif
490 : const bool Opt_PCA_ON /* i : PCA option flag */
491 : #ifdef DEBUG_SBA_AUDIO_DUMP
492 : ,
493 : int16_t *numTransportChannels
494 : #endif
495 : )
496 : {
497 : ENCODER_CONFIG_HANDLE hEncoderConfig;
498 : ivas_error error;
499 :
500 244 : if ( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
501 : {
502 0 : return error;
503 : }
504 :
505 244 : hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
506 :
507 244 : hEncoderConfig->ivas_format = SBA_FORMAT;
508 244 : hEncoderConfig->element_mode_init = IVAS_SCE; /* Just needs to be something not mono, will be set later */
509 244 : hEncoderConfig->sba_planar = isPlanar;
510 244 : hEncoderConfig->sba_order = order;
511 :
512 : /* Input in ACN/SN3D in all cases (3D and planar): get number of channels */
513 244 : hEncoderConfig->nchan_inp = ivas_sba_get_nchan( hEncoderConfig->sba_order, 0 ); /*planar input arg. deliberately set to zero since input always in ACN/SN3D*/
514 :
515 : #ifdef DEBUG_AGC_ENCODER_CMD_OPTION
516 : if ( ( error = agcAPIToInternal( Opt_AGC_ON, &( hEncoderConfig->Opt_AGC_ON ) ) ) != IVAS_ERR_OK )
517 : {
518 : return error;
519 : }
520 : #endif
521 :
522 244 : hEncoderConfig->Opt_PCA_ON = (int16_t) Opt_PCA_ON;
523 :
524 244 : hIvasEnc->maxBandwidthUser = max_bwidth_user;
525 :
526 244 : error = configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
527 :
528 : #ifdef DEBUG_SBA_AUDIO_DUMP
529 : *numTransportChannels = hIvasEnc->st_ivas->nchan_transport;
530 : #endif
531 :
532 244 : return error;
533 : }
534 :
535 : /*---------------------------------------------------------------------*
536 : * IVAS_ENC_ConfigureForSBAObjects()
537 : *
538 : * Configure and initialize the combined SBA and ISM encoder.
539 : *---------------------------------------------------------------------*/
540 :
541 37 : ivas_error IVAS_ENC_ConfigureForSBAObjects(
542 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
543 : const int32_t inputFs, /* i : input sampling frequency */
544 : const int32_t bitrate, /* i : requested bitrate of the ouput bitstream */
545 : const IVAS_ENC_BANDWIDTH maxBandwidth, /* i : bandwidth limitation */
546 : const IVAS_ENC_DTX_CONFIG dtxConfig, /* i : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig() */
547 : const uint16_t numObjects, /* i : number of objects to be encoded */
548 : const IVAS_ENC_SBA_ORDER order, /* i : order of the Ambisonics input */
549 : const bool isPlanar, /* i : if true, input is treated as planar Ambisonics */
550 : const bool Opt_PCA_ON /* i : PCA option flag */
551 : )
552 : {
553 : Encoder_Struct *st_ivas;
554 : ivas_error error;
555 :
556 37 : if ( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
557 : {
558 0 : return error;
559 : }
560 :
561 37 : if ( numObjects > MAX_NUM_OBJECTS )
562 : {
563 0 : return IVAS_ERR_TOO_MANY_INPUTS;
564 : }
565 37 : st_ivas = hIvasEnc->st_ivas;
566 :
567 37 : st_ivas->hEncoderConfig->element_mode_init = IVAS_SCE; /* Just needs to be something not mono, will be set later */
568 37 : st_ivas->hEncoderConfig->sba_planar = isPlanar;
569 37 : st_ivas->hEncoderConfig->sba_order = order;
570 :
571 : /* Input in ACN/SN3D in all cases (3D and planar): get number of channels */
572 : /*Input file will always contain all channels for a given order irrespective of planar flag*/
573 37 : st_ivas->hEncoderConfig->nchan_inp = ivas_sba_get_nchan( st_ivas->hEncoderConfig->sba_order, 0 ) + numObjects;
574 :
575 37 : st_ivas->hEncoderConfig->Opt_PCA_ON = (int16_t) Opt_PCA_ON;
576 :
577 : /* Currently this is true but it is already shown in descriptive metadata that there can be inequality for this. */
578 37 : st_ivas->nchan_transport = st_ivas->hEncoderConfig->nchan_inp - numObjects;
579 37 : st_ivas->hEncoderConfig->ivas_format = SBA_ISM_FORMAT;
580 37 : st_ivas->hEncoderConfig->nchan_ism = numObjects;
581 :
582 37 : return configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
583 : }
584 :
585 :
586 : /*---------------------------------------------------------------------*
587 : * IVAS_ENC_ConfigureForMasa()
588 : *
589 : * Configure and initialize the MASA encoder.
590 : *---------------------------------------------------------------------*/
591 :
592 75 : ivas_error IVAS_ENC_ConfigureForMasa(
593 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
594 : const int32_t inputFs, /* i : input sampling frequency */
595 : const int32_t bitrate, /* i : requested bitrate of the output bitstream */
596 : const bool max_bwidth_user, /* i : shows if bandwidth limitation was set by the user (true) or if default bandwidth was used (false) */
597 : const IVAS_ENC_BANDWIDTH maxBandwidth, /* i : bandwidth limitation */
598 : const IVAS_ENC_DTX_CONFIG dtxConfig, /* i : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig() */
599 : const IVAS_ENC_MASA_VARIANT masaVariant /* i : type of MASA input (either 1 or 2 channels) */
600 : )
601 : {
602 : ENCODER_CONFIG_HANDLE hEncoderConfig;
603 : ivas_error error;
604 :
605 75 : if ( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
606 : {
607 0 : return error;
608 : }
609 :
610 75 : hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
611 :
612 75 : hEncoderConfig->ivas_format = MASA_FORMAT;
613 :
614 75 : switch ( masaVariant )
615 : {
616 31 : case IVAS_ENC_MASA_1CH:
617 31 : hEncoderConfig->nchan_inp = 1;
618 31 : hEncoderConfig->element_mode_init = IVAS_SCE;
619 31 : break;
620 44 : case IVAS_ENC_MASA_2CH:
621 44 : hEncoderConfig->nchan_inp = 2;
622 : #ifdef DEBUGGING
623 : hEncoderConfig->stereo_mode_cmdl = 1; /* set unified stereo by default */
624 : #endif
625 44 : hEncoderConfig->element_mode_init = IVAS_CPE_DFT; /* initialization only, might be changed later based on element_brate */
626 44 : break;
627 0 : default:
628 0 : return IVAS_ERR_INVALID_MASA_CONFIG;
629 : break;
630 : }
631 :
632 75 : hIvasEnc->maxBandwidthUser = max_bwidth_user;
633 :
634 75 : error = configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
635 :
636 75 : return error;
637 : }
638 :
639 :
640 : /*---------------------------------------------------------------------*
641 : * IVAS_ENC_FeedMasaMetadata()
642 : *
643 : *
644 : *---------------------------------------------------------------------*/
645 :
646 39626 : ivas_error IVAS_ENC_FeedMasaMetadata(
647 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
648 : IVAS_MASA_METADATA_HANDLE hMasaMetadata /* i : MASA metadata for current frame */
649 : )
650 : {
651 39626 : if ( !hIvasEnc->isConfigured )
652 : {
653 0 : return IVAS_ERR_NOT_CONFIGURED;
654 : }
655 :
656 39626 : if ( hIvasEnc->st_ivas->hEncoderConfig->ivas_format != MASA_FORMAT && hIvasEnc->st_ivas->hEncoderConfig->ivas_format != MASA_ISM_FORMAT )
657 : {
658 0 : return IVAS_ERR_METADATA_NOT_EXPECTED;
659 : }
660 :
661 39626 : hIvasEnc->st_ivas->hMasa->masaMetadata = *hMasaMetadata;
662 :
663 39626 : return IVAS_ERR_OK;
664 : }
665 :
666 :
667 : /*---------------------------------------------------------------------*
668 : * IVAS_ENC_ConfigureForMultichannel()
669 : *
670 : * Configure and initialize the MC encoder.
671 : *---------------------------------------------------------------------*/
672 :
673 82 : ivas_error IVAS_ENC_ConfigureForMultichannel(
674 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
675 : const int32_t inputFs, /* i : input sampling frequency */
676 : const int32_t bitrate, /* i : requested bitrate of the output bitstream */
677 : const bool max_bwidth_user, /* i : shows if bandwidth limitation was set by the user (true) or if default bandwidth was used (false) */
678 : const IVAS_ENC_BANDWIDTH maxBandwidth, /* i : bandwidth limitation */
679 : const IVAS_ENC_DTX_CONFIG dtxConfig, /* i : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig() */
680 : const IVAS_ENC_MC_LAYOUT mcLayout /* i : inpu MC layput */
681 : )
682 : {
683 : ENCODER_CONFIG_HANDLE hEncoderConfig;
684 : ivas_error error;
685 :
686 82 : if ( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
687 : {
688 0 : return error;
689 : }
690 :
691 82 : hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
692 :
693 82 : hEncoderConfig->ivas_format = MC_FORMAT;
694 82 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT; /*just for initialization*/
695 :
696 82 : switch ( mcLayout )
697 : {
698 43 : case IVAS_ENC_MC_5_1:
699 43 : hEncoderConfig->mc_input_setup = MC_LS_SETUP_5_1;
700 43 : break;
701 4 : case IVAS_ENC_MC_7_1:
702 4 : hEncoderConfig->mc_input_setup = MC_LS_SETUP_7_1;
703 4 : break;
704 6 : case IVAS_ENC_MC_5_1_2:
705 6 : hEncoderConfig->mc_input_setup = MC_LS_SETUP_5_1_2;
706 6 : break;
707 6 : case IVAS_ENC_MC_5_1_4:
708 6 : hEncoderConfig->mc_input_setup = MC_LS_SETUP_5_1_4;
709 6 : break;
710 23 : case IVAS_ENC_MC_7_1_4:
711 23 : hEncoderConfig->mc_input_setup = MC_LS_SETUP_7_1_4;
712 23 : break;
713 0 : default:
714 0 : return IVAS_ERR_INVALID_MC_LAYOUT;
715 : break;
716 : }
717 :
718 82 : hEncoderConfig->nchan_inp = ivas_mc_ls_setup_get_num_channels( hEncoderConfig->mc_input_setup );
719 :
720 82 : hIvasEnc->maxBandwidthUser = max_bwidth_user;
721 :
722 82 : error = configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
723 :
724 82 : return error;
725 : }
726 :
727 :
728 : /*---------------------------------------------------------------------*
729 : * configureEncoder()
730 : *
731 : * Configure the IVAS encoder.
732 : *---------------------------------------------------------------------*/
733 :
734 627 : static ivas_error configureEncoder(
735 : IVAS_ENC_HANDLE hIvasEnc,
736 : const int32_t inputFs,
737 : const int32_t initBitrate,
738 : const IVAS_ENC_BANDWIDTH initBandwidth,
739 : const IVAS_ENC_DTX_CONFIG dtxConfig,
740 : const IVAS_ENC_CHANNEL_AWARE_CONFIG caConfig )
741 : {
742 : Encoder_Struct *st_ivas;
743 : ENCODER_CONFIG_HANDLE hEncoderConfig;
744 : ivas_error error;
745 : int32_t cpe_brate;
746 :
747 627 : error = IVAS_ERR_OK;
748 :
749 627 : st_ivas = hIvasEnc->st_ivas;
750 627 : hEncoderConfig = st_ivas->hEncoderConfig;
751 :
752 : /*-----------------------------------------------------------------*
753 : * Bandwidth limitation
754 : *-----------------------------------------------------------------*/
755 :
756 627 : if ( ( error = setBandwidth( hIvasEnc, initBandwidth ) ) != IVAS_ERR_OK )
757 : {
758 0 : return error;
759 : }
760 :
761 : /*-----------------------------------------------------------------*
762 : * DTX/CNG
763 : *-----------------------------------------------------------------*/
764 :
765 627 : if ( dtxConfig.enabled )
766 : {
767 126 : hEncoderConfig->Opt_DTX_ON = 1;
768 :
769 126 : if ( dtxConfig.variable_SID_rate )
770 : {
771 0 : hEncoderConfig->var_SID_rate_flag = 1;
772 0 : hEncoderConfig->interval_SID = 0;
773 : }
774 : else
775 : {
776 126 : hEncoderConfig->var_SID_rate_flag = 0;
777 :
778 126 : if ( dtxConfig.SID_interval >= 3 && dtxConfig.SID_interval <= 100 )
779 : {
780 126 : hEncoderConfig->interval_SID = dtxConfig.SID_interval;
781 : }
782 : else
783 : {
784 0 : return IVAS_ERR_INVALID_DTX_UPDATE_RATE;
785 : }
786 : }
787 : }
788 : else
789 : {
790 501 : hEncoderConfig->Opt_DTX_ON = 0;
791 : }
792 :
793 : /*-----------------------------------------------------------------*
794 : * Bitrate
795 : *-----------------------------------------------------------------*/
796 :
797 627 : hEncoderConfig->ivas_total_brate = initBitrate;
798 :
799 : /* SC-VBR at 5.90 kbps */
800 627 : if ( hEncoderConfig->ivas_total_brate == ACELP_5k90 )
801 : {
802 0 : hEncoderConfig->ivas_total_brate = ACELP_7k20;
803 0 : hEncoderConfig->Opt_SC_VBR = 1;
804 0 : hEncoderConfig->last_Opt_SC_VBR = hEncoderConfig->Opt_SC_VBR;
805 :
806 0 : if ( hEncoderConfig->max_bwidth != NB )
807 : {
808 0 : hEncoderConfig->max_bwidth = WB;
809 : }
810 : }
811 :
812 : /* check if the entered bitrate is supported */
813 627 : if ( hEncoderConfig->ivas_format != UNDEFINED_FORMAT && hEncoderConfig->ivas_format != MONO_FORMAT ) /* IVAS */
814 : {
815 624 : if ( !is_IVAS_bitrate( hEncoderConfig->ivas_total_brate ) )
816 : {
817 0 : if ( hEncoderConfig->Opt_SC_VBR )
818 : {
819 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Incorrect bitrate specification in IVAS [bps]: %d", ACELP_5k90 );
820 : }
821 : else
822 : {
823 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Incorrect bitrate specification in IVAS [bps]: %d", hEncoderConfig->ivas_total_brate );
824 : }
825 : }
826 :
827 624 : if ( hEncoderConfig->ivas_format == STEREO_FORMAT )
828 : {
829 : #ifdef DEBUGGING
830 : if ( hIvasEnc->cmd_stereo )
831 : #endif
832 : {
833 68 : hEncoderConfig->element_mode_init = IVAS_CPE_DFT;
834 : #ifdef DEBUGGING
835 : hEncoderConfig->stereo_mode_cmdl = 1;
836 : #endif
837 68 : if ( hEncoderConfig->ivas_total_brate >= MIN_BRATE_MDCT_STEREO )
838 : {
839 18 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
840 : #ifdef DEBUGGING
841 : hEncoderConfig->stereo_mode_cmdl = 0;
842 : #endif
843 : }
844 : }
845 :
846 68 : if ( ( hEncoderConfig->element_mode_init == IVAS_CPE_TD || hEncoderConfig->element_mode_init == IVAS_CPE_DFT ) && hEncoderConfig->ivas_total_brate > IVAS_48k )
847 : {
848 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for TD/DFT Stereo specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
849 : }
850 :
851 68 : if ( hEncoderConfig->element_mode_init == IVAS_CPE_MDCT && hEncoderConfig->ivas_total_brate < IVAS_48k )
852 : {
853 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too low bitrate for MDCT Stereo specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
854 : }
855 :
856 68 : if ( hEncoderConfig->ivas_total_brate > IVAS_256k )
857 : {
858 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for Stereo specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
859 : }
860 : }
861 556 : else if ( hEncoderConfig->ivas_format == ISM_FORMAT )
862 : {
863 74 : if ( ( error = sanitizeBitrateISM( hEncoderConfig, hIvasEnc->extMetadataApi ) ) != IVAS_ERR_OK )
864 : {
865 0 : return error;
866 : }
867 : }
868 482 : else if ( hEncoderConfig->ivas_format == SBA_FORMAT )
869 : {
870 : /* nothing */
871 : }
872 238 : else if ( hEncoderConfig->ivas_format == MASA_FORMAT )
873 : {
874 : /* adapt element_mode according to the bitrate */
875 75 : if ( hEncoderConfig->nchan_inp == 2 && hEncoderConfig->element_mode_init != IVAS_SCE )
876 : {
877 44 : if ( hEncoderConfig->ivas_total_brate >= IVAS_48k )
878 : {
879 22 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
880 : }
881 22 : else if ( hEncoderConfig->ivas_total_brate < MASA_STEREO_MIN_BITRATE )
882 : {
883 14 : hEncoderConfig->element_mode_init = IVAS_CPE_DFT;
884 : }
885 : }
886 : }
887 163 : else if ( hEncoderConfig->ivas_format == MASA_ISM_FORMAT )
888 : {
889 44 : st_ivas->ism_mode = ivas_omasa_ism_mode_select( st_ivas->hEncoderConfig->ivas_total_brate, hEncoderConfig->nchan_ism );
890 :
891 44 : cpe_brate = calculate_cpe_brate_MASA_ISM( st_ivas->ism_mode, st_ivas->hEncoderConfig->ivas_total_brate, hEncoderConfig->nchan_ism );
892 :
893 : /*adapt element_mode according to the bit-rate*/
894 44 : if ( hEncoderConfig->element_mode_init != IVAS_SCE )
895 : {
896 44 : if ( cpe_brate >= IVAS_48k )
897 : {
898 17 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
899 : }
900 : }
901 : }
902 119 : else if ( hEncoderConfig->ivas_format == SBA_ISM_FORMAT )
903 : {
904 37 : st_ivas->ism_mode = ISM_MODE_NONE;
905 : }
906 : }
907 : else /* EVS mono */
908 : {
909 3 : hEncoderConfig->ivas_format = MONO_FORMAT;
910 3 : hEncoderConfig->element_mode_init = EVS_MONO;
911 :
912 3 : if ( !is_EVS_bitrate( hEncoderConfig->ivas_total_brate, &hEncoderConfig->Opt_AMR_WB ) )
913 : {
914 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Incorrect bitrate specification in EVS mono: %d", hEncoderConfig->ivas_total_brate );
915 : }
916 :
917 3 : if ( hEncoderConfig->stereo_dmx_evs == 1 )
918 : {
919 2 : hEncoderConfig->nchan_inp = 2;
920 : }
921 : }
922 :
923 : /*-----------------------------------------------------------------*
924 : * Input sampling frequency
925 : *-----------------------------------------------------------------*/
926 :
927 627 : if ( inputFs != 8000 && inputFs != 16000 && inputFs != 32000 && inputFs != 48000 )
928 : {
929 0 : return IVAS_ERR_INVALID_SAMPLING_RATE;
930 : }
931 :
932 627 : hEncoderConfig->input_Fs = inputFs;
933 :
934 : /*-----------------------------------------------------------------*
935 : * Channel-aware mode
936 : *-----------------------------------------------------------------*/
937 :
938 627 : if ( ( error = setChannelAwareConfig( hIvasEnc, caConfig ) ) != IVAS_ERR_OK )
939 : {
940 0 : return error;
941 : }
942 :
943 : /*-----------------------------------------------------------------*
944 : * Set codec mode
945 : *-----------------------------------------------------------------*/
946 :
947 627 : st_ivas->codec_mode = MODE1; /* Note: in IVAS, set MODE1 */
948 :
949 627 : if ( hEncoderConfig->ivas_format == MONO_FORMAT )
950 : {
951 3 : if ( hEncoderConfig->Opt_AMR_WB )
952 : {
953 0 : st_ivas->codec_mode = MODE1;
954 : }
955 : else
956 : {
957 3 : st_ivas->codec_mode = get_codec_mode( hEncoderConfig->ivas_total_brate );
958 : }
959 : }
960 :
961 627 : if ( hEncoderConfig->ivas_total_brate == IVAS_13k2 && hEncoderConfig->Opt_RF_ON == 1 )
962 : {
963 0 : st_ivas->codec_mode = MODE2;
964 : }
965 :
966 627 : st_ivas->last_codec_mode = st_ivas->codec_mode;
967 :
968 : /*-----------------------------------------------------------------*
969 : * Sanity checks
970 : *-----------------------------------------------------------------*/
971 :
972 627 : assert( hEncoderConfig->ivas_format != UNDEFINED_FORMAT && "\n IVAS format undefined" );
973 :
974 627 : if ( ( hEncoderConfig->ivas_format != MONO_FORMAT || hEncoderConfig->stereo_dmx_evs ) && hEncoderConfig->input_Fs == 8000 )
975 : {
976 0 : return IVAS_ERROR( IVAS_ERR_INVALID_SAMPLING_RATE, "8kHz input sampling rate is not supported in IVAS." );
977 : }
978 :
979 627 : if ( hEncoderConfig->Opt_DTX_ON && hEncoderConfig->ivas_format != MONO_FORMAT &&
980 126 : ( ( hEncoderConfig->ivas_format == SBA_FORMAT && ivas_get_sba_num_TCs( hEncoderConfig->ivas_total_brate, 1 ) > 2 ) ||
981 126 : hEncoderConfig->ivas_format == MC_FORMAT || hEncoderConfig->ivas_format == MASA_ISM_FORMAT || hEncoderConfig->ivas_format == SBA_ISM_FORMAT ) )
982 : {
983 0 : return IVAS_ERROR( IVAS_ERR_DTX_NOT_SUPPORTED, "DTX is not supported in this IVAS format and element mode." );
984 : }
985 :
986 : #ifdef DEBUG_AGC_ENCODER_CMD_OPTION
987 : if ( hEncoderConfig->Opt_AGC_ON == SBA_AGC_FORCE_ENABLE && !( hEncoderConfig->ivas_format == SBA_FORMAT ) )
988 : {
989 : return IVAS_ERROR( IVAS_ERR_NOT_SUPPORTED_OPTION, "AGC supported in SBA format at bitrates >= 24.4 kbps only." );
990 : }
991 : #endif
992 :
993 627 : if ( hEncoderConfig->Opt_PCA_ON && !( ( hEncoderConfig->ivas_format == SBA_FORMAT || hEncoderConfig->ivas_format == SBA_ISM_FORMAT ) && hEncoderConfig->ivas_total_brate == PCA_BRATE && hEncoderConfig->sba_order == SBA_FOA_ORDER ) )
994 : {
995 0 : return IVAS_ERROR( IVAS_ERR_NOT_SUPPORTED_OPTION, "PCA supported at SBA FOA 256 kbps only." );
996 : }
997 :
998 627 : if ( ( error = sanitizeBandwidth( hIvasEnc ) ) != IVAS_ERR_OK )
999 : {
1000 0 : return error;
1001 : }
1002 :
1003 627 : if ( hEncoderConfig->is_binaural && !( ( hEncoderConfig->ivas_format == MONO_FORMAT && hEncoderConfig->stereo_dmx_evs ) || hEncoderConfig->ivas_format == STEREO_FORMAT ) )
1004 : {
1005 0 : return IVAS_ERROR( IVAS_ERR_NOT_SUPPORTED_OPTION, "'-binaural' option is supported only with '-stereo' or '-stereo_dmx_evs'" );
1006 : }
1007 :
1008 : /*-----------------------------------------------------------------*
1009 : * Finalize initialization
1010 : *-----------------------------------------------------------------*/
1011 :
1012 627 : if ( ( error = ivas_init_encoder( st_ivas ) ) != IVAS_ERR_OK )
1013 : {
1014 0 : return error;
1015 : }
1016 :
1017 627 : if ( hEncoderConfig->ivas_format == MONO_FORMAT )
1018 : {
1019 3 : hIvasEnc->hCoreCoder = st_ivas->hSCE[0]->hCoreCoder[0]; /* Note: this is needed for switching in EVS mono */
1020 : }
1021 : else
1022 : {
1023 624 : hIvasEnc->hCoreCoder = NULL;
1024 : }
1025 :
1026 627 : hIvasEnc->Opt_RF_ON_loc = hEncoderConfig->Opt_RF_ON;
1027 627 : hIvasEnc->rf_fec_offset_loc = hEncoderConfig->rf_fec_offset;
1028 :
1029 627 : hIvasEnc->isConfigured = true;
1030 :
1031 627 : return error;
1032 : }
1033 :
1034 :
1035 : /*---------------------------------------------------------------------*
1036 : * IVAS_ENC_GetDelay()
1037 : *
1038 : *
1039 : *---------------------------------------------------------------------*/
1040 :
1041 627 : ivas_error IVAS_ENC_GetDelay(
1042 : const IVAS_ENC_HANDLE hIvasEnc, /* i : IVAS encoder handle */
1043 : int16_t *delay /* o : encoder delay */
1044 : )
1045 : {
1046 : ENCODER_CONFIG_HANDLE hEncoderConfig;
1047 :
1048 627 : hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
1049 :
1050 627 : if ( !hIvasEnc->isConfigured )
1051 : {
1052 0 : return IVAS_ERR_NOT_CONFIGURED;
1053 : }
1054 :
1055 627 : if ( delay == NULL )
1056 : {
1057 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1058 : }
1059 :
1060 627 : *delay = NS2SA( hEncoderConfig->input_Fs, get_delay( ENC, hEncoderConfig->input_Fs, hEncoderConfig->ivas_format, NULL, 0 ) );
1061 :
1062 627 : *delay *= hEncoderConfig->nchan_inp;
1063 :
1064 627 : return IVAS_ERR_OK;
1065 : }
1066 :
1067 :
1068 : /*---------------------------------------------------------------------*
1069 : * getInputBufferSize()
1070 : *
1071 : *
1072 : *---------------------------------------------------------------------*/
1073 :
1074 428015 : static int16_t getInputBufferSize(
1075 : const Encoder_Struct *st_ivas /* i : IVAS encoder handle */
1076 : )
1077 : {
1078 428015 : return (int16_t) ( st_ivas->hEncoderConfig->input_Fs * st_ivas->hEncoderConfig->nchan_inp / FRAMES_PER_SEC );
1079 : }
1080 :
1081 : /*---------------------------------------------------------------------*
1082 : * IVAS_ENC_GetNumInChannels()
1083 : *
1084 : *
1085 : *---------------------------------------------------------------------*/
1086 627 : ivas_error IVAS_ENC_GetNumInChannels(
1087 : const IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
1088 : int16_t *numInChannels /* o : total number of samples expected in the input buffer for current encoder configuration */
1089 : )
1090 : {
1091 627 : if ( hIvasEnc == NULL || numInChannels == NULL )
1092 : {
1093 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1094 : }
1095 :
1096 627 : if ( !hIvasEnc->isConfigured )
1097 : {
1098 0 : return IVAS_ERR_NOT_CONFIGURED;
1099 : }
1100 :
1101 627 : *numInChannels = hIvasEnc->st_ivas->hEncoderConfig->nchan_inp;
1102 :
1103 627 : return IVAS_ERR_OK;
1104 : }
1105 :
1106 :
1107 : /*---------------------------------------------------------------------*
1108 : * IVAS_ENC_GetInputBufferSize()
1109 : *
1110 : *
1111 : *---------------------------------------------------------------------*/
1112 :
1113 627 : ivas_error IVAS_ENC_GetInputBufferSize(
1114 : const IVAS_ENC_HANDLE hIvasEnc, /* i : IVAS encoder handle */
1115 : int16_t *inputBufferSize /* o : total number of samples expected in the input buffer for current encoder configuration */
1116 : )
1117 : {
1118 627 : if ( !hIvasEnc->isConfigured )
1119 : {
1120 0 : return IVAS_ERR_NOT_CONFIGURED;
1121 : }
1122 :
1123 627 : if ( inputBufferSize == NULL )
1124 : {
1125 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1126 : }
1127 :
1128 627 : *inputBufferSize = getInputBufferSize( hIvasEnc->st_ivas );
1129 :
1130 627 : return IVAS_ERR_OK;
1131 : }
1132 :
1133 :
1134 : /*---------------------------------------------------------------------*
1135 : * IVAS_ENC_EncodeFrameToSerial()
1136 : *
1137 : * Main function to encode one frame to a serial bitstream
1138 : *---------------------------------------------------------------------*/
1139 :
1140 427388 : ivas_error IVAS_ENC_EncodeFrameToSerial(
1141 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
1142 : int16_t *inputBuffer, /* i : PCM input */
1143 : int16_t inputBufferSize, /* i : total number of samples in the input buffer. Related function: IVAS_ENC_GetInputBufferSize() */
1144 : uint16_t *outputBitStream, /* o : pointer to serial output bitstream. The array must already be allocated and be of size at least IVAS_MAX_BITS_PER_FRAME */
1145 : uint16_t *numOutBits /* o : number of bits written to output bitstream. Each bit is stored as a single uint16_t value */
1146 : )
1147 : {
1148 : Encoder_Struct *st_ivas;
1149 : ENCODER_CONFIG_HANDLE hEncoderConfig;
1150 : ENC_CORE_HANDLE hCoreCoder;
1151 : int16_t i;
1152 : int16_t n, ch;
1153 : ivas_error error;
1154 :
1155 427388 : error = IVAS_ERR_OK;
1156 :
1157 427388 : if ( !hIvasEnc->isConfigured )
1158 : {
1159 0 : return IVAS_ERR_NOT_CONFIGURED;
1160 : }
1161 :
1162 427388 : st_ivas = hIvasEnc->st_ivas;
1163 427388 : hEncoderConfig = st_ivas->hEncoderConfig;
1164 427388 : hCoreCoder = hIvasEnc->hCoreCoder;
1165 :
1166 427388 : if ( inputBufferSize != getInputBufferSize( st_ivas ) )
1167 : {
1168 0 : return IVAS_ERR_INVALID_INPUT_BUFFER_SIZE;
1169 : }
1170 :
1171 427388 : if ( ( error = sanitizeBandwidth( hIvasEnc ) ) != IVAS_ERR_OK )
1172 : {
1173 0 : return error;
1174 : }
1175 :
1176 427388 : if ( hEncoderConfig->ivas_format == ISM_FORMAT )
1177 : {
1178 407452 : for ( i = 0; i < hEncoderConfig->nchan_inp; ++i )
1179 : {
1180 303626 : if ( !hIvasEnc->ismMetadataProvided[i] )
1181 : {
1182 6000 : ivas_ism_reset_metadata_API( hIvasEnc->st_ivas->hIsmMetaData[i] );
1183 : }
1184 : }
1185 103826 : resetIsmMetadataProvidedFlags( hIvasEnc );
1186 : }
1187 :
1188 427388 : if ( ( hEncoderConfig->Opt_RF_ON && ( hEncoderConfig->ivas_total_brate != ACELP_13k20 || hEncoderConfig->input_Fs == 8000 || hEncoderConfig->max_bwidth == NB ) ) || hEncoderConfig->rf_fec_offset == 0 )
1189 : {
1190 427388 : if ( hEncoderConfig->ivas_total_brate == ACELP_13k20 && hEncoderConfig->ivas_format == MONO_FORMAT )
1191 : {
1192 1050 : st_ivas->codec_mode = MODE1;
1193 1050 : reset_rf_indices( hCoreCoder->hRF, hCoreCoder->L_frame, &( hCoreCoder->rf_target_bits_write ) );
1194 : }
1195 427388 : hEncoderConfig->Opt_RF_ON = 0;
1196 427388 : hEncoderConfig->rf_fec_offset = 0;
1197 427388 : hIvasEnc->switchingActive = true;
1198 : }
1199 :
1200 427388 : if ( hIvasEnc->Opt_RF_ON_loc && hIvasEnc->rf_fec_offset_loc != 0 && L_sub( hEncoderConfig->ivas_total_brate, ACELP_13k20 ) == 0 && L_sub( hEncoderConfig->input_Fs, 8000 ) != 0 && hEncoderConfig->max_bwidth != NB )
1201 : {
1202 0 : st_ivas->codec_mode = MODE2;
1203 0 : if ( hEncoderConfig->Opt_RF_ON == 0 && hEncoderConfig->ivas_format == MONO_FORMAT )
1204 : {
1205 0 : reset_rf_indices( hCoreCoder->hRF, hCoreCoder->L_frame, &( hCoreCoder->rf_target_bits_write ) );
1206 : }
1207 0 : hEncoderConfig->Opt_RF_ON = 1;
1208 0 : hEncoderConfig->rf_fec_offset = hIvasEnc->rf_fec_offset_loc;
1209 0 : hIvasEnc->switchingActive = true;
1210 : }
1211 :
1212 : /* in case of 8kHz sampling rate or when in "max_band NB" mode, limit the total bitrate to 24.40 kbps */
1213 427388 : if ( ( ( hEncoderConfig->input_Fs == 8000 ) || ( hEncoderConfig->max_bwidth == NB ) ) && ( hEncoderConfig->ivas_total_brate > ACELP_24k40 ) )
1214 : {
1215 0 : hEncoderConfig->ivas_total_brate = ACELP_24k40;
1216 0 : st_ivas->codec_mode = MODE2;
1217 0 : hIvasEnc->switchingActive = true;
1218 : }
1219 :
1220 : /*-----------------------------------------------------------------*
1221 : * Re-allocate and re-initialize buffer of indices if IVAS total bitrate has changed
1222 : *-----------------------------------------------------------------*/
1223 :
1224 427388 : if ( hEncoderConfig->ivas_total_brate != hEncoderConfig->last_ivas_total_brate )
1225 : {
1226 : /* de-allocate old buffer of indices */
1227 6903 : free( st_ivas->ind_list );
1228 :
1229 : /* set the maximum allowed number of indices in the list */
1230 6903 : st_ivas->ivas_max_num_indices = get_ivas_max_num_indices( hEncoderConfig->ivas_format, hEncoderConfig->ivas_total_brate );
1231 :
1232 : /* allocate new buffer of indices */
1233 6903 : if ( ( st_ivas->ind_list = (INDICE_HANDLE) malloc( st_ivas->ivas_max_num_indices * sizeof( Indice ) ) ) == NULL )
1234 : {
1235 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for buffer of indices!\n" ) );
1236 : }
1237 :
1238 : /* reset the list of indices */
1239 5416213 : for ( i = 0; i < st_ivas->ivas_max_num_indices; i++ )
1240 : {
1241 5409310 : st_ivas->ind_list[i].nb_bits = -1;
1242 : }
1243 :
1244 : #if defined( DEBUGGING ) && defined( DBG_BITSTREAM_ANALYSIS )
1245 : for ( i = 0; i < st_ivas->ivas_max_num_indices; i++ )
1246 : {
1247 : memset( st_ivas->ind_list[i].function_name, 'A', 100 * sizeof( char ) );
1248 : }
1249 : #endif
1250 :
1251 : /* de-allocate old buffer of metadata indices */
1252 6903 : if ( st_ivas->ind_list_metadata != NULL )
1253 : {
1254 6903 : free( st_ivas->ind_list_metadata );
1255 : }
1256 :
1257 : /* set the maximum allowed number of metadata indices in the list */
1258 6903 : st_ivas->ivas_max_num_indices_metadata = get_ivas_max_num_indices_metadata( hEncoderConfig->ivas_format, hEncoderConfig->ivas_total_brate );
1259 :
1260 6903 : if ( st_ivas->ivas_max_num_indices_metadata > 0 )
1261 : {
1262 : /* allocate new buffer of metadata indices */
1263 6903 : if ( ( st_ivas->ind_list_metadata = (INDICE_HANDLE) malloc( st_ivas->ivas_max_num_indices_metadata * sizeof( Indice ) ) ) == NULL )
1264 : {
1265 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for buffer of metadata indices!\n" ) );
1266 : }
1267 :
1268 : /* reset the list of metadata indices */
1269 2407748 : for ( i = 0; i < st_ivas->ivas_max_num_indices_metadata; i++ )
1270 : {
1271 2400845 : st_ivas->ind_list_metadata[i].nb_bits = -1;
1272 : }
1273 : }
1274 : else
1275 : {
1276 0 : st_ivas->ind_list_metadata = NULL;
1277 : }
1278 :
1279 : /* set pointers to the new buffers of indices in each element */
1280 13325 : for ( n = 0; n < st_ivas->nSCE; n++ )
1281 : {
1282 6422 : st_ivas->hSCE[n]->hCoreCoder[0]->hBstr->ind_list = st_ivas->ind_list;
1283 6422 : st_ivas->hSCE[n]->hCoreCoder[0]->hBstr->ivas_ind_list_zero = &st_ivas->ind_list;
1284 :
1285 6422 : if ( st_ivas->hSCE[n]->hMetaData != NULL )
1286 : {
1287 3681 : st_ivas->hSCE[n]->hMetaData->ind_list = st_ivas->ind_list_metadata;
1288 3681 : st_ivas->hSCE[n]->hMetaData->ivas_ind_list_zero = &st_ivas->ind_list_metadata;
1289 : }
1290 : }
1291 :
1292 13541 : for ( n = 0; n < st_ivas->nCPE; n++ )
1293 : {
1294 19914 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
1295 : {
1296 13276 : st_ivas->hCPE[n]->hCoreCoder[ch]->hBstr->ind_list = st_ivas->ind_list;
1297 13276 : st_ivas->hCPE[n]->hCoreCoder[ch]->hBstr->ivas_ind_list_zero = &st_ivas->ind_list;
1298 : }
1299 :
1300 6638 : if ( st_ivas->hCPE[n]->hMetaData != NULL )
1301 : {
1302 4435 : st_ivas->hCPE[n]->hMetaData->ind_list = st_ivas->ind_list_metadata;
1303 4435 : st_ivas->hCPE[n]->hMetaData->ivas_ind_list_zero = &st_ivas->ind_list_metadata;
1304 : }
1305 : }
1306 : }
1307 :
1308 427388 : if ( hIvasEnc->switchingActive && hEncoderConfig->ivas_format == MONO_FORMAT )
1309 : {
1310 3100 : copy_encoder_config( st_ivas, hCoreCoder, 0 );
1311 3100 : hEncoderConfig->last_ivas_total_brate = hEncoderConfig->ivas_total_brate;
1312 : }
1313 :
1314 : /* run the main encoding routine */
1315 427388 : if ( hEncoderConfig->ivas_format == MONO_FORMAT ) /* EVS mono */
1316 : {
1317 3100 : hCoreCoder->total_brate = hEncoderConfig->ivas_total_brate; /* needed in case of bitrate switching */
1318 : #ifdef DEBUGGING
1319 : hCoreCoder->id_element = 0;
1320 : #endif
1321 :
1322 3100 : if ( hEncoderConfig->stereo_dmx_evs == 1 )
1323 : {
1324 2100 : inputBufferSize /= 2;
1325 2100 : stereo_dmx_evs_enc( st_ivas->hStereoDmxEVS, hEncoderConfig->input_Fs, inputBuffer, inputBufferSize, hEncoderConfig->is_binaural );
1326 : }
1327 :
1328 3100 : if ( hEncoderConfig->Opt_AMR_WB )
1329 : {
1330 0 : amr_wb_enc( hCoreCoder, inputBuffer, st_ivas->mem_hp20_in[0], inputBufferSize );
1331 : }
1332 : else
1333 : {
1334 3100 : if ( ( error = evs_enc( hCoreCoder, inputBuffer, st_ivas->mem_hp20_in[0], inputBufferSize ) ) != IVAS_ERR_OK )
1335 : {
1336 0 : return error;
1337 : }
1338 : }
1339 : }
1340 : else /* IVAS */
1341 : {
1342 424288 : if ( ( error = ivas_enc( st_ivas, inputBuffer, inputBufferSize ) ) != IVAS_ERR_OK )
1343 : {
1344 0 : return error;
1345 : }
1346 : }
1347 :
1348 : /* write indices into bitstream buffer */
1349 427388 : write_indices_ivas( st_ivas, outputBitStream, numOutBits );
1350 :
1351 : /* Reset switching flag before next call - can be set to "true" by some setters */
1352 427388 : hIvasEnc->switchingActive = false;
1353 :
1354 427388 : return error;
1355 : }
1356 :
1357 :
1358 : /*---------------------------------------------------------------------*
1359 : * IVAS_ENC_EncodeFrameToCompact()
1360 : *
1361 : * Main function to encode one frame to a compact bitstream (bytestream)
1362 : *---------------------------------------------------------------------*/
1363 :
1364 0 : ivas_error IVAS_ENC_EncodeFrameToCompact(
1365 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
1366 : int16_t *inputBuffer, /* i : PCM input */
1367 : const int16_t inputBufferSize, /* i : total number of samples in the input buffer. Related function: IVAS_ENC_GetInputBufferSize() */
1368 : uint8_t *outputBitStream, /* o : pointer to compact output bitstream. The array must already be allocated. */
1369 : uint16_t *numOutBits /* o : number of bits written to output bitstream */
1370 : )
1371 : {
1372 : ivas_error error;
1373 : uint16_t bitstream[IVAS_MAX_BITS_PER_FRAME];
1374 :
1375 0 : if ( ( error = IVAS_ENC_EncodeFrameToSerial( hIvasEnc, inputBuffer, inputBufferSize, bitstream, numOutBits ) ) != IVAS_ERR_OK )
1376 : {
1377 0 : return error;
1378 : }
1379 :
1380 0 : convertSerialToBytestream( bitstream, *numOutBits, outputBitStream );
1381 :
1382 0 : return IVAS_ERR_OK;
1383 : }
1384 :
1385 :
1386 : /*---------------------------------------------------------------------*
1387 : * IVAS_ENC_SetBandwidth()
1388 : *
1389 : *
1390 : *---------------------------------------------------------------------*/
1391 :
1392 21150 : ivas_error IVAS_ENC_SetBandwidth(
1393 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
1394 : const IVAS_ENC_BANDWIDTH maxBandwidth /* i : bandwidth limitation to be used */
1395 : )
1396 : {
1397 : ivas_error error;
1398 :
1399 : /* Do additional checks for user-facing function */
1400 21150 : if ( ( error = doCommonSetterChecks( hIvasEnc ) ) != IVAS_ERR_OK )
1401 : {
1402 0 : return error;
1403 : }
1404 :
1405 : /* Use internal function to set bandiwdth */
1406 21150 : return setBandwidth( hIvasEnc, maxBandwidth );
1407 : }
1408 :
1409 :
1410 : /*---------------------------------------------------------------------*
1411 : * IVAS_ENC_SetBitrate()
1412 : *
1413 : *
1414 : *---------------------------------------------------------------------*/
1415 :
1416 58138 : ivas_error IVAS_ENC_SetBitrate(
1417 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
1418 : const int32_t totalBitrate /* i : requested bitrate of the output bitstream */
1419 : )
1420 : {
1421 : ivas_error error;
1422 :
1423 : /* Do additional checks for user-facing function */
1424 58138 : if ( ( error = doCommonSetterChecks( hIvasEnc ) ) != IVAS_ERR_OK )
1425 : {
1426 0 : return error;
1427 : }
1428 :
1429 : /* Use internal function to set bitrate */
1430 58138 : return setBitrate( hIvasEnc, totalBitrate );
1431 : }
1432 :
1433 :
1434 : /*---------------------------------------------------------------------*
1435 : * IVAS_ENC_SetChannelAwareConfig()
1436 : *
1437 : * Sets the configuration of channel-aware mode. Since CA mode is only
1438 : * supported at 13.2 kbps, this function has no effect at other bitrates.
1439 : *---------------------------------------------------------------------*/
1440 :
1441 0 : ivas_error IVAS_ENC_SetChannelAwareConfig(
1442 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
1443 : const IVAS_ENC_CHANNEL_AWARE_CONFIG rfConfig /* i : configuration of channel-aware mode */
1444 : )
1445 : {
1446 : ivas_error error;
1447 :
1448 : /* Do additional checks for user-facing function */
1449 0 : if ( ( error = doCommonSetterChecks( hIvasEnc ) ) != IVAS_ERR_OK )
1450 : {
1451 0 : return error;
1452 : }
1453 :
1454 : /* Use internal function to set CA config */
1455 0 : return setChannelAwareConfig( hIvasEnc, rfConfig );
1456 : }
1457 :
1458 :
1459 : #ifdef DEBUGGING
1460 : /*---------------------------------------------------------------------*
1461 : * IVAS_ENC_SetForcedMode()
1462 : *
1463 : *
1464 : *---------------------------------------------------------------------*/
1465 :
1466 : ivas_error IVAS_ENC_SetForcedMode(
1467 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
1468 : const IVAS_ENC_FORCED_MODE forcedMode /* i : forced coding mode */
1469 : )
1470 : {
1471 : int16_t newForced;
1472 : ivas_error error;
1473 :
1474 : /* Do additional checks for user-facing function */
1475 : if ( ( error = doCommonSetterChecks( hIvasEnc ) ) != IVAS_ERR_OK )
1476 : {
1477 : return error;
1478 : }
1479 :
1480 : if ( ( error = forcedModeApiToInternal( forcedMode, &newForced ) ) != IVAS_ERR_OK )
1481 : {
1482 : return error;
1483 : }
1484 :
1485 : if ( hIvasEnc->st_ivas->hEncoderConfig->force != newForced )
1486 : {
1487 : hIvasEnc->st_ivas->hEncoderConfig->force = newForced;
1488 : hIvasEnc->switchingActive = true;
1489 : }
1490 :
1491 : return IVAS_ERR_OK;
1492 : }
1493 : #endif /* DEBUGGING */
1494 :
1495 :
1496 : /*---------------------------------------------------------------------*
1497 : * IVAS_ENC_GetDefaultBandwidth()
1498 : *
1499 : *
1500 : *---------------------------------------------------------------------*/
1501 :
1502 448 : IVAS_ENC_BANDWIDTH IVAS_ENC_GetDefaultBandwidth( const bool isEVS )
1503 : {
1504 448 : IVAS_ENC_BANDWIDTH bwidth = IVAS_ENC_BANDWIDTH_FB;
1505 :
1506 448 : if ( isEVS )
1507 : {
1508 3 : bwidth = IVAS_ENC_BANDWIDTH_SWB;
1509 : }
1510 :
1511 448 : return bwidth;
1512 : }
1513 :
1514 :
1515 : /*---------------------------------------------------------------------*
1516 : * IVAS_ENC_GetDefaultDtxConfig()
1517 : *
1518 : *
1519 : *---------------------------------------------------------------------*/
1520 :
1521 627 : IVAS_ENC_DTX_CONFIG IVAS_ENC_GetDefaultDtxConfig( void )
1522 : {
1523 : IVAS_ENC_DTX_CONFIG defaultDtxConfig;
1524 627 : defaultDtxConfig.enabled = false;
1525 627 : defaultDtxConfig.SID_interval = 0;
1526 627 : defaultDtxConfig.variable_SID_rate = false;
1527 :
1528 627 : return defaultDtxConfig;
1529 : }
1530 :
1531 :
1532 : /*---------------------------------------------------------------------*
1533 : * IVAS_ENC_GetDefaultChannelAwareConfig()
1534 : *
1535 : *
1536 : *---------------------------------------------------------------------*/
1537 :
1538 1251 : IVAS_ENC_CHANNEL_AWARE_CONFIG IVAS_ENC_GetDefaultChannelAwareConfig( void )
1539 : {
1540 : IVAS_ENC_CHANNEL_AWARE_CONFIG defaultCaConfig;
1541 1251 : defaultCaConfig.channelAwareModeEnabled = 0;
1542 1251 : defaultCaConfig.fec_indicator = IVAS_ENC_FEC_HI;
1543 1251 : defaultCaConfig.fec_offset = 0;
1544 :
1545 1251 : return defaultCaConfig;
1546 : }
1547 :
1548 :
1549 : /*---------------------------------------------------------------------*
1550 : * IVAS_ENC_GetErrorMessage()
1551 : *
1552 : *
1553 : *---------------------------------------------------------------------*/
1554 :
1555 0 : const char *IVAS_ENC_GetErrorMessage(
1556 : ivas_error error /* i : encoder error code enum */
1557 : )
1558 : {
1559 0 : return ivas_error_to_string( error );
1560 : }
1561 :
1562 :
1563 : /*---------------------------------------------------------------------*
1564 : * Local functions
1565 : *---------------------------------------------------------------------*/
1566 :
1567 627 : static ivas_error printConfigInfo_enc(
1568 : IVAS_ENC_HANDLE hIvasEnc,
1569 : const int16_t channelAwareModeEnabled )
1570 : {
1571 : Encoder_Struct *st_ivas;
1572 : ENCODER_CONFIG_HANDLE hEncoderConfig;
1573 : int16_t newBandwidthApi;
1574 : ivas_error error;
1575 :
1576 627 : st_ivas = hIvasEnc->st_ivas;
1577 627 : hEncoderConfig = st_ivas->hEncoderConfig;
1578 :
1579 : /*-----------------------------------------------------------------*
1580 : * Print input signal sampling frequency
1581 : *-----------------------------------------------------------------*/
1582 :
1583 627 : fprintf( stdout, "Input sampling rate: %d Hz\n", hEncoderConfig->input_Fs );
1584 :
1585 : /*-----------------------------------------------------------------*
1586 : * Print bitrate
1587 : *-----------------------------------------------------------------*/
1588 :
1589 627 : if ( st_ivas->hEncoderConfig->Opt_SC_VBR )
1590 : {
1591 0 : fprintf( stdout, "Average bitrate: %.2f kbps\n", (float) ACELP_5k90 / 1000 );
1592 : }
1593 : else
1594 : {
1595 627 : fprintf( stdout, "Bitrate: %.2f kbps\n", (float) hEncoderConfig->ivas_total_brate / 1000 );
1596 : }
1597 :
1598 : /*-----------------------------------------------------------------*
1599 : * Print IVAS format
1600 : *-----------------------------------------------------------------*/
1601 :
1602 627 : if ( hEncoderConfig->ivas_format == MONO_FORMAT )
1603 : {
1604 3 : if ( hEncoderConfig->stereo_dmx_evs )
1605 : {
1606 2 : fprintf( stdout, "IVAS format: stereo downmix to bit-exact EVS mono\n" );
1607 : }
1608 : else
1609 : {
1610 1 : fprintf( stdout, "IVAS format: bit-exact EVS mono\n" );
1611 : }
1612 : }
1613 624 : else if ( hEncoderConfig->ivas_format == STEREO_FORMAT )
1614 : {
1615 : #ifdef DEBUGGING
1616 : if ( hEncoderConfig->stereo_mode_cmdl == 1 )
1617 : {
1618 : fprintf( stdout, "IVAS format: stereo - Unified stereo\n" );
1619 : }
1620 : else if ( hEncoderConfig->element_mode_init == IVAS_CPE_DFT )
1621 : {
1622 : fprintf( stdout, "IVAS format: stereo - DFT stereo\n" );
1623 : }
1624 : else if ( hEncoderConfig->element_mode_init == IVAS_CPE_TD )
1625 : {
1626 : fprintf( stdout, "IVAS format: stereo - TD stereo\n" );
1627 : }
1628 : else if ( hEncoderConfig->element_mode_init == IVAS_CPE_MDCT )
1629 : {
1630 : fprintf( stdout, "IVAS format: stereo - MDCT stereo\n" );
1631 : }
1632 : #else
1633 68 : if ( hEncoderConfig->element_mode_init != IVAS_CPE_MDCT )
1634 : {
1635 50 : fprintf( stdout, "IVAS format: stereo - Unified stereo\n" );
1636 : }
1637 : else
1638 : {
1639 18 : fprintf( stdout, "IVAS format: stereo - MDCT stereo\n" );
1640 : }
1641 : #endif
1642 : }
1643 556 : else if ( hEncoderConfig->ivas_format == ISM_FORMAT )
1644 : {
1645 74 : if ( hEncoderConfig->ivas_total_brate <= ACELP_32k && hEncoderConfig->nchan_inp > 2 )
1646 : {
1647 17 : fprintf( stdout, "IVAS format: Param-ISM (%i streams)\n", hEncoderConfig->nchan_inp );
1648 : }
1649 : else
1650 : {
1651 57 : fprintf( stdout, "IVAS format: ISM (%i streams)\n", hEncoderConfig->nchan_inp );
1652 : }
1653 : }
1654 482 : else if ( hEncoderConfig->ivas_format == SBA_FORMAT )
1655 : {
1656 244 : fprintf( stdout, "IVAS format: Scene Based Audio, Ambisonic order %i %s ", hEncoderConfig->sba_order, hEncoderConfig->sba_planar ? "(Planar)" : "" );
1657 244 : if ( hEncoderConfig->Opt_PCA_ON )
1658 : {
1659 4 : fprintf( stdout, "- PCA configured with signal adaptive decision " );
1660 : }
1661 :
1662 : #ifdef DEBUG_AGC_ENCODER_CMD_OPTION
1663 : switch ( hEncoderConfig->Opt_AGC_ON )
1664 : {
1665 : case SBA_AGC_FORCE_ENABLE:
1666 : fprintf( stdout, "- AGC FORCED ON " );
1667 : break;
1668 : case SBA_AGC_FORCE_DISABLE:
1669 : fprintf( stdout, "- AGC FORCED OFF " );
1670 : break;
1671 : case SBA_AGC_DEFAULT:
1672 : fprintf( stdout, "- AGC default mode " );
1673 : break;
1674 : default:
1675 : fprintf( stdout, "- AGC unknown " );
1676 : break;
1677 : }
1678 : #endif
1679 244 : fprintf( stdout, "\n" );
1680 : }
1681 238 : else if ( hEncoderConfig->ivas_format == MASA_FORMAT )
1682 : {
1683 75 : fprintf( stdout, "IVAS format: MASA format\n" );
1684 : }
1685 163 : else if ( hEncoderConfig->ivas_format == MC_FORMAT )
1686 : {
1687 82 : if ( hEncoderConfig->mc_input_setup == MC_LS_SETUP_5_1 )
1688 : {
1689 43 : fprintf( stdout, "IVAS mode: Multi-Channel 5.1 \n" );
1690 : }
1691 39 : else if ( hEncoderConfig->mc_input_setup == MC_LS_SETUP_7_1 )
1692 : {
1693 4 : fprintf( stdout, "IVAS mode: Multi-Channel 7.1 \n" );
1694 : }
1695 35 : else if ( hEncoderConfig->mc_input_setup == MC_LS_SETUP_5_1_2 )
1696 : {
1697 6 : fprintf( stdout, "IVAS mode: Multi-Channel 5.1+2 \n" );
1698 : }
1699 29 : else if ( hEncoderConfig->mc_input_setup == MC_LS_SETUP_5_1_4 )
1700 : {
1701 6 : fprintf( stdout, "IVAS mode: Multi-Channel 5.1+4\n" );
1702 : }
1703 23 : else if ( hEncoderConfig->mc_input_setup == MC_LS_SETUP_7_1_4 )
1704 : {
1705 23 : fprintf( stdout, "IVAS mode: Multi-Channel 7.1+4\n" );
1706 : }
1707 : }
1708 81 : else if ( hEncoderConfig->ivas_format == SBA_ISM_FORMAT )
1709 : {
1710 37 : fprintf( stdout, "IVAS format: combined ISM and SBA (%i ISM stream(s))\n", hEncoderConfig->nchan_ism );
1711 : }
1712 44 : else if ( hEncoderConfig->ivas_format == MASA_ISM_FORMAT )
1713 : {
1714 44 : fprintf( stdout, "IVAS format: combined ISM and MASA (%i ISM stream(s))\n", hEncoderConfig->nchan_ism );
1715 : }
1716 :
1717 627 : if ( hEncoderConfig->is_binaural )
1718 : {
1719 0 : fprintf( stdout, "Optional indication: binaural audio\n" );
1720 : }
1721 :
1722 : /*-----------------------------------------------------------------*
1723 : * Print CNG update interval, if DTX is activated
1724 : *-----------------------------------------------------------------*/
1725 :
1726 627 : if ( hEncoderConfig->Opt_DTX_ON )
1727 : {
1728 126 : if ( hEncoderConfig->var_SID_rate_flag )
1729 : {
1730 0 : fprintf( stdout, "DTX: ON, variable CNG update interval\n" );
1731 : }
1732 : else
1733 : {
1734 126 : fprintf( stdout, "DTX: ON, CNG update interval = %d frames\n", hEncoderConfig->interval_SID );
1735 : }
1736 : }
1737 :
1738 : /*-----------------------------------------------------------------*
1739 : * Print potential limitation of audio bandwidth
1740 : *-----------------------------------------------------------------*/
1741 :
1742 627 : if ( ( error = bandwidthApiToInternal( hIvasEnc->newBandwidthApi, &newBandwidthApi ) ) != IVAS_ERR_OK )
1743 : {
1744 0 : return error;
1745 : }
1746 :
1747 627 : if ( st_ivas->hEncoderConfig->Opt_SC_VBR && !hEncoderConfig->Opt_DTX_ON )
1748 : {
1749 0 : return IVAS_ERROR( IVAS_ERR_WRONG_PARAMS, "\nError: SC-VBR 5900 bps not supported without DTX\n\n" );
1750 : }
1751 :
1752 627 : if ( hEncoderConfig->ivas_format == MONO_FORMAT )
1753 : {
1754 3 : if ( newBandwidthApi != hEncoderConfig->max_bwidth )
1755 : {
1756 0 : if ( newBandwidthApi == FB )
1757 : {
1758 0 : fprintf( stdout, "\nFB coding not supported below %.2f kbps. ", ACELP_16k40 / 1000.f );
1759 0 : if ( hEncoderConfig->max_bwidth == WB )
1760 : {
1761 0 : fprintf( stdout, "Switching to WB.\n" );
1762 : }
1763 : else
1764 : {
1765 0 : fprintf( stdout, "Switching to SWB.\n" );
1766 : }
1767 : }
1768 0 : else if ( newBandwidthApi == SWB )
1769 : {
1770 0 : fprintf( stdout, "\nSWB coding not supported below %.2f kbps. Switching to WB.\n", ACELP_9k60 / 1000.f );
1771 : }
1772 : }
1773 :
1774 : /* in case of 8kHz input sampling or "-max_band NB", require the total bitrate to be below 24.40 kbps */
1775 3 : if ( ( newBandwidthApi == NB || hEncoderConfig->input_Fs == 8000 ) && hEncoderConfig->ivas_total_brate > ACELP_24k40 )
1776 : {
1777 0 : fprintf( stdout, "\nError: Unsupported mode NB %d bps, NB mode supports rates 5900-24400 bps\n\n", hEncoderConfig->ivas_total_brate );
1778 0 : return IVAS_ERR_INVALID_BITRATE;
1779 : }
1780 : }
1781 : else
1782 : {
1783 624 : if ( newBandwidthApi != hEncoderConfig->max_bwidth )
1784 : {
1785 105 : if ( hEncoderConfig->ivas_format == ISM_FORMAT )
1786 : {
1787 23 : fprintf( stdout, "\nFB coding not supported below %.2f kbps for %i objects. Switching to SWB.\n", hEncoderConfig->nchan_ism * MIN_BRATE_FB_ISM / 1000.f, hEncoderConfig->nchan_ism );
1788 : }
1789 : else
1790 : {
1791 82 : fprintf( stdout, "\nFB coding not supported below %.2f kbps. Switching to SWB.\n", MIN_BRATE_FB_STEREO / 1000.f );
1792 : }
1793 : }
1794 : }
1795 :
1796 : /*-----------------------------------------------------------------*
1797 : * Print Channel-aware limitation
1798 : *-----------------------------------------------------------------*/
1799 :
1800 627 : if ( channelAwareModeEnabled )
1801 : {
1802 0 : if ( hEncoderConfig->Opt_RF_ON == 0 )
1803 : {
1804 0 : fprintf( stdout, "\nChannel-aware mode is supported at 13.2 kbps 32/48 kHz only. Switching to normal mode.\n" );
1805 : }
1806 : }
1807 :
1808 627 : return IVAS_ERR_OK;
1809 : }
1810 :
1811 :
1812 : /*---------------------------------------------------------------------*
1813 : * setBitrate()
1814 : *
1815 : *
1816 : *---------------------------------------------------------------------*/
1817 :
1818 58138 : static ivas_error setBitrate(
1819 : IVAS_ENC_HANDLE hIvasEnc,
1820 : const int32_t totalBitrate )
1821 : {
1822 : Encoder_Struct *st_ivas;
1823 : ENCODER_CONFIG_HANDLE hEncoderConfig;
1824 : ivas_error error;
1825 :
1826 58138 : st_ivas = hIvasEnc->st_ivas;
1827 58138 : hEncoderConfig = st_ivas->hEncoderConfig;
1828 :
1829 58138 : hEncoderConfig->ivas_total_brate = totalBitrate;
1830 58138 : hIvasEnc->switchingActive = true;
1831 :
1832 : /* channel-aware mode is supported only at 13.20 kbps */
1833 58138 : if ( hEncoderConfig->Opt_RF_ON && hEncoderConfig->ivas_total_brate != ACELP_13k20 )
1834 : {
1835 0 : assert( 0 && "\nChannel-aware mode is supported only at 13.20 kbps\n" );
1836 : hEncoderConfig->Opt_RF_ON = 0;
1837 : }
1838 :
1839 58138 : if ( hEncoderConfig->ivas_total_brate == ACELP_5k90 )
1840 : {
1841 0 : st_ivas->hEncoderConfig->Opt_SC_VBR = 1;
1842 0 : hEncoderConfig->ivas_total_brate = ACELP_7k20;
1843 : }
1844 : else
1845 : {
1846 58138 : st_ivas->hEncoderConfig->Opt_SC_VBR = 0;
1847 : }
1848 :
1849 : /* check if the entered bitrate is supported */
1850 58138 : if ( hEncoderConfig->element_mode_init > EVS_MONO )
1851 : {
1852 58138 : if ( !is_IVAS_bitrate( hEncoderConfig->ivas_total_brate ) )
1853 : {
1854 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Incorrect bitrate specification in IVAS: %d", hEncoderConfig->ivas_total_brate );
1855 : }
1856 : }
1857 : else
1858 : {
1859 0 : if ( !is_EVS_bitrate( hEncoderConfig->ivas_total_brate, &hEncoderConfig->Opt_AMR_WB ) )
1860 : {
1861 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Incorrect bitrate specification in EVS mono: %d", hEncoderConfig->ivas_total_brate );
1862 : }
1863 :
1864 : /* in case of 8kHz signal, limit the total bitrate to 24.40 kbps */
1865 0 : if ( hEncoderConfig->input_Fs == 8000 && hEncoderConfig->ivas_total_brate > ACELP_24k40 )
1866 : {
1867 0 : hEncoderConfig->ivas_total_brate = ACELP_24k40;
1868 : }
1869 : }
1870 :
1871 58138 : if ( hEncoderConfig->ivas_format == ISM_FORMAT )
1872 : {
1873 10086 : if ( ( error = sanitizeBitrateISM( hEncoderConfig, hIvasEnc->extMetadataApi ) ) != IVAS_ERR_OK )
1874 : {
1875 0 : return error;
1876 : }
1877 : }
1878 :
1879 58138 : st_ivas->codec_mode = MODE1;
1880 :
1881 58138 : if ( hEncoderConfig->element_mode_init == EVS_MONO )
1882 : {
1883 0 : if ( hEncoderConfig->Opt_AMR_WB )
1884 : {
1885 0 : st_ivas->codec_mode = MODE1;
1886 : }
1887 : else
1888 : {
1889 0 : st_ivas->codec_mode = get_codec_mode( hEncoderConfig->ivas_total_brate );
1890 : }
1891 : }
1892 :
1893 58138 : return IVAS_ERR_OK;
1894 : }
1895 :
1896 :
1897 : /*---------------------------------------------------------------------*
1898 : * setChannelAwareConfig()
1899 : *
1900 : *
1901 : *---------------------------------------------------------------------*/
1902 :
1903 627 : static ivas_error setChannelAwareConfig(
1904 : IVAS_ENC_HANDLE hIvasEnc,
1905 : const IVAS_ENC_CHANNEL_AWARE_CONFIG caConfig )
1906 : {
1907 : int16_t newFecIndicator;
1908 : ivas_error error;
1909 : Encoder_Struct *st_ivas;
1910 : ENCODER_CONFIG_HANDLE hEncoderConfig;
1911 :
1912 627 : st_ivas = hIvasEnc->st_ivas;
1913 627 : hEncoderConfig = st_ivas->hEncoderConfig;
1914 :
1915 : /* channel-aware mode is supported only at 13.20 kbps and with WB or SWB bandwidth */
1916 627 : if ( ( caConfig.channelAwareModeEnabled && st_ivas->hEncoderConfig->ivas_total_brate != ACELP_13k20 ) || ( hEncoderConfig->Opt_RF_ON && hEncoderConfig->input_Fs == 8000 ) )
1917 : {
1918 0 : hEncoderConfig->Opt_RF_ON = 0;
1919 0 : hEncoderConfig->rf_fec_offset = 0;
1920 0 : return IVAS_ERR_OK;
1921 : }
1922 :
1923 627 : if ( caConfig.channelAwareModeEnabled )
1924 : {
1925 0 : hEncoderConfig->Opt_RF_ON = 1;
1926 :
1927 : /* Convert FEC indicator from API type */
1928 0 : if ( ( error = fecIndicatorApiToInternal( caConfig.fec_indicator, &newFecIndicator ) ) != IVAS_ERR_OK )
1929 : {
1930 0 : return error;
1931 : }
1932 :
1933 : /* Set new values only if they differ from current values */
1934 0 : if ( ( newFecIndicator != hEncoderConfig->rf_fec_indicator || caConfig.fec_offset != hEncoderConfig->rf_fec_offset ) )
1935 : {
1936 0 : hEncoderConfig->rf_fec_indicator = newFecIndicator;
1937 :
1938 : /* Check if new FEC offset has a valid value */
1939 0 : if ( caConfig.fec_offset == 0 || caConfig.fec_offset == 2 || caConfig.fec_offset == 3 || caConfig.fec_offset == 5 || caConfig.fec_offset == 7 )
1940 : {
1941 0 : hEncoderConfig->rf_fec_offset = caConfig.fec_offset;
1942 : }
1943 : else
1944 : {
1945 0 : return IVAS_ERR_INVALID_FEC_OFFSET;
1946 : }
1947 :
1948 0 : hIvasEnc->switchingActive = true;
1949 : }
1950 :
1951 : /* Save a copy of FEC offset value - needed during encoding */
1952 0 : hIvasEnc->rf_fec_offset_loc = hEncoderConfig->rf_fec_offset;
1953 : }
1954 : else
1955 : {
1956 627 : hEncoderConfig->Opt_RF_ON = 0;
1957 : }
1958 :
1959 627 : return IVAS_ERR_OK;
1960 : }
1961 :
1962 :
1963 : /*---------------------------------------------------------------------*
1964 : * doCommonConfigureChecks()
1965 : *
1966 : *
1967 : *---------------------------------------------------------------------*/
1968 :
1969 627 : static ivas_error doCommonConfigureChecks(
1970 : IVAS_ENC_HANDLE hIvasEnc )
1971 : {
1972 627 : if ( hIvasEnc == NULL || hIvasEnc->st_ivas == NULL )
1973 : {
1974 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1975 : }
1976 :
1977 627 : if ( hIvasEnc->isConfigured )
1978 : {
1979 0 : return IVAS_ERR_RECONFIGURE_NOT_SUPPORTED;
1980 : }
1981 :
1982 627 : return IVAS_ERR_OK;
1983 : }
1984 :
1985 :
1986 : /*---------------------------------------------------------------------*
1987 : * doCommonSetterChecks()
1988 : *
1989 : *
1990 : *---------------------------------------------------------------------*/
1991 :
1992 79288 : static ivas_error doCommonSetterChecks(
1993 : IVAS_ENC_HANDLE hIvasEnc )
1994 : {
1995 79288 : if ( hIvasEnc == NULL || hIvasEnc->st_ivas == NULL )
1996 : {
1997 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1998 : }
1999 :
2000 : /* Currently settings can be changed only after configuration step */
2001 79288 : if ( !hIvasEnc->isConfigured )
2002 : {
2003 0 : return IVAS_ERR_NOT_CONFIGURED;
2004 : }
2005 :
2006 79288 : return IVAS_ERR_OK;
2007 : }
2008 :
2009 :
2010 : /*---------------------------------------------------------------------*
2011 : * sanitizeBandwidth()
2012 : *
2013 : *
2014 : *---------------------------------------------------------------------*/
2015 :
2016 428015 : static ivas_error sanitizeBandwidth(
2017 : const IVAS_ENC_HANDLE hIvasEnc )
2018 : {
2019 : ENCODER_CONFIG_HANDLE hEncoderConfig;
2020 : int16_t max_bwidth_tmp;
2021 :
2022 428015 : hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
2023 :
2024 428015 : max_bwidth_tmp = hIvasEnc->newBandwidthApi;
2025 :
2026 : /* Prevent st_ivas->max_bwidth from being higher than Fs/2 */
2027 428015 : if ( hEncoderConfig->input_Fs == 8000 && max_bwidth_tmp > NB )
2028 : {
2029 0 : max_bwidth_tmp = NB;
2030 : }
2031 428015 : else if ( hEncoderConfig->input_Fs == 16000 && max_bwidth_tmp > WB )
2032 : {
2033 0 : max_bwidth_tmp = WB;
2034 : }
2035 428015 : else if ( hEncoderConfig->input_Fs == 32000 && max_bwidth_tmp > SWB )
2036 : {
2037 1351 : max_bwidth_tmp = SWB;
2038 : }
2039 :
2040 : /* NB coding not supported in IVAS. Switching to WB. */
2041 428015 : if ( max_bwidth_tmp == NB && hEncoderConfig->ivas_format != UNDEFINED_FORMAT && hEncoderConfig->ivas_format != MONO_FORMAT )
2042 : {
2043 301 : if ( hEncoderConfig->input_Fs >= 16000 )
2044 : {
2045 301 : max_bwidth_tmp = WB;
2046 : }
2047 : else
2048 : {
2049 0 : return IVAS_ERR_INVALID_BITRATE;
2050 : }
2051 : }
2052 :
2053 428015 : if ( hEncoderConfig->ivas_format == MONO_FORMAT )
2054 : {
2055 : #if 0 // IVAS_fmToDo: temporary disabled to keep EVS bit-exactness -> to be verified
2056 : if ( max_bwidth_tmp == FB && hEncoderConfig->ivas_total_brate < ACELP_16k40 )
2057 : {
2058 : if ( hEncoderConfig->ivas_total_brate < ACELP_9k60 )
2059 : {
2060 : max_bwidth_tmp = WB;
2061 : }
2062 : else
2063 : {
2064 : max_bwidth_tmp = SWB;
2065 : }
2066 : }
2067 :
2068 : if ( max_bwidth_tmp == SWB && hEncoderConfig->ivas_total_brate < ACELP_9k60 )
2069 : {
2070 : max_bwidth_tmp = WB;
2071 : }
2072 :
2073 : /* in case of 8kHz input sampling or "-max_band NB", require the total bitrate to be below 24.40 kbps */
2074 : if ( ( max_bwidth_tmp == NB || hEncoderConfig->input_Fs == 8000 ) && hEncoderConfig->ivas_total_brate > ACELP_24k40 )
2075 : {
2076 : if ( hEncoderConfig->input_Fs >= 16000 )
2077 : {
2078 : max_bwidth_tmp = WB;
2079 : }
2080 : else
2081 : {
2082 : return IVAS_ERR_INVALID_BITRATE;
2083 : }
2084 : }
2085 : #endif
2086 : }
2087 : else
2088 : {
2089 424912 : if ( max_bwidth_tmp == FB && ( ( hEncoderConfig->ivas_format != ISM_FORMAT && hEncoderConfig->ivas_total_brate < MIN_BRATE_FB_STEREO ) ||
2090 232813 : ( hEncoderConfig->ivas_format == ISM_FORMAT && hEncoderConfig->ivas_total_brate / hEncoderConfig->nchan_ism < MIN_BRATE_FB_ISM ) ) )
2091 : {
2092 60973 : max_bwidth_tmp = SWB;
2093 : }
2094 : }
2095 :
2096 428015 : if ( hEncoderConfig->max_bwidth != max_bwidth_tmp )
2097 : {
2098 5109 : hEncoderConfig->max_bwidth = max_bwidth_tmp;
2099 5109 : hIvasEnc->switchingActive = true;
2100 : }
2101 :
2102 428015 : return IVAS_ERR_OK;
2103 : }
2104 :
2105 :
2106 : /*---------------------------------------------------------------------*
2107 : * sanitizeBitrateISM()
2108 : *
2109 : *
2110 : *---------------------------------------------------------------------*/
2111 :
2112 10160 : static ivas_error sanitizeBitrateISM(
2113 : const ENCODER_CONFIG_HANDLE hEncoderConfig,
2114 : const bool extMetadataApi )
2115 : {
2116 10160 : if ( hEncoderConfig->ivas_total_brate > IVAS_128k && hEncoderConfig->nchan_inp == 1 )
2117 : {
2118 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for 1 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
2119 : }
2120 :
2121 10160 : if ( hEncoderConfig->ivas_total_brate > IVAS_256k && hEncoderConfig->nchan_inp == 2 )
2122 : {
2123 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for 2 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
2124 : }
2125 :
2126 10160 : if ( hEncoderConfig->ivas_total_brate > IVAS_384k && hEncoderConfig->nchan_inp == 3 )
2127 : {
2128 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for 3 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
2129 : }
2130 :
2131 10160 : if ( hEncoderConfig->ivas_total_brate < IVAS_16k4 && hEncoderConfig->nchan_inp == 2 )
2132 : {
2133 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too low bitrate for 2 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
2134 : }
2135 :
2136 10160 : if ( hEncoderConfig->ivas_total_brate < IVAS_24k4 && hEncoderConfig->nchan_inp == 3 )
2137 : {
2138 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too low bitrate for 3 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
2139 : }
2140 :
2141 10160 : if ( hEncoderConfig->ivas_total_brate < IVAS_24k4 && hEncoderConfig->nchan_inp == 4 )
2142 : {
2143 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too low bitrate for 4 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
2144 : }
2145 :
2146 10160 : if ( extMetadataApi )
2147 : {
2148 1007 : hEncoderConfig->ism_extended_metadata_flag = ( hEncoderConfig->ivas_total_brate >= ISM_EXTENDED_METADATA_BRATE );
2149 : }
2150 : else
2151 : {
2152 9153 : hEncoderConfig->ism_extended_metadata_flag = 0;
2153 : }
2154 :
2155 10160 : return IVAS_ERR_OK;
2156 : }
2157 :
2158 :
2159 : /*---------------------------------------------------------------------*
2160 : * setBandwidth()
2161 : *
2162 : *
2163 : *---------------------------------------------------------------------*/
2164 :
2165 21777 : static ivas_error setBandwidth(
2166 : IVAS_ENC_HANDLE hIvasEnc,
2167 : const IVAS_ENC_BANDWIDTH maxBandwidth )
2168 : {
2169 : ivas_error error;
2170 : int16_t newBandwidth;
2171 : ENCODER_CONFIG_HANDLE hEncoderConfig;
2172 :
2173 21777 : hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
2174 :
2175 : /* Convert bandwidth from API type */
2176 21777 : if ( ( error = bandwidthApiToInternal( maxBandwidth, &newBandwidth ) ) != IVAS_ERR_OK )
2177 : {
2178 0 : return error;
2179 : }
2180 :
2181 21777 : hIvasEnc->newBandwidthApi = newBandwidth;
2182 :
2183 : /* NB coding not supported in IVAS. Switching to WB. */
2184 21777 : if ( newBandwidth == NB && hEncoderConfig->ivas_format != UNDEFINED_FORMAT && hEncoderConfig->ivas_format != MONO_FORMAT )
2185 : {
2186 301 : newBandwidth = WB;
2187 : }
2188 :
2189 21777 : if ( hEncoderConfig->max_bwidth != newBandwidth )
2190 : {
2191 3669 : hEncoderConfig->max_bwidth = newBandwidth;
2192 3669 : hIvasEnc->switchingActive = true;
2193 : }
2194 :
2195 21777 : return IVAS_ERR_OK;
2196 : }
2197 :
2198 :
2199 : /*---------------------------------------------------------------------*
2200 : * resetIsmMetadataProvidedFlags()
2201 : *
2202 : *
2203 : *---------------------------------------------------------------------*/
2204 :
2205 104453 : static void resetIsmMetadataProvidedFlags(
2206 : IVAS_ENC_HANDLE hIvasEnc )
2207 : {
2208 : int16_t i;
2209 :
2210 522265 : for ( i = 0; i < MAX_NUM_OBJECTS; ++i )
2211 : {
2212 417812 : hIvasEnc->ismMetadataProvided[i] = false;
2213 : }
2214 :
2215 104453 : return;
2216 : }
2217 :
2218 :
2219 : /*---------------------------------------------------------------------*
2220 : * bandwidthApiToInternal()
2221 : *
2222 : *
2223 : *---------------------------------------------------------------------*/
2224 :
2225 22404 : static ivas_error bandwidthApiToInternal(
2226 : const IVAS_ENC_BANDWIDTH maxBandwidth,
2227 : int16_t *internalMaxBandwidth )
2228 : {
2229 22404 : switch ( maxBandwidth )
2230 : {
2231 302 : case IVAS_ENC_BANDWIDTH_NB:
2232 302 : *internalMaxBandwidth = NB;
2233 302 : break;
2234 7340 : case IVAS_ENC_BANDWIDTH_WB:
2235 7340 : *internalMaxBandwidth = WB;
2236 7340 : break;
2237 7306 : case IVAS_ENC_BANDWIDTH_SWB:
2238 7306 : *internalMaxBandwidth = SWB;
2239 7306 : break;
2240 7456 : case IVAS_ENC_BANDWIDTH_FB:
2241 7456 : *internalMaxBandwidth = FB;
2242 7456 : break;
2243 0 : case IVAS_ENC_BANDWIDTH_UNDEFINED:
2244 : default:
2245 0 : return IVAS_ERR_INVALID_BANDWIDTH;
2246 : break;
2247 : }
2248 :
2249 22404 : return IVAS_ERR_OK;
2250 : }
2251 :
2252 : #ifdef DEBUG_AGC_ENCODER_CMD_OPTION
2253 : /*---------------------------------------------------------------------*
2254 : * agcAPIToInternal()
2255 : *
2256 : *
2257 : *---------------------------------------------------------------------*/
2258 :
2259 : static ivas_error agcAPIToInternal(
2260 : const IVAS_ENC_AGC agcOption,
2261 : int16_t *internalAGCOption )
2262 : {
2263 : switch ( agcOption )
2264 : {
2265 : case IVAS_ENC_AGC_ENABLED:
2266 : *internalAGCOption = SBA_AGC_FORCE_ENABLE;
2267 : break;
2268 : case IVAS_ENC_AGC_DISABLED:
2269 : *internalAGCOption = SBA_AGC_FORCE_DISABLE;
2270 : break;
2271 : case IVAS_ENC_AGC_UNDEFINED:
2272 : *internalAGCOption = SBA_AGC_DEFAULT;
2273 : break;
2274 : default:
2275 : return IVAS_ERR_INVALID_AGC;
2276 : break;
2277 : }
2278 :
2279 : return IVAS_ERR_OK;
2280 : }
2281 : #endif
2282 :
2283 :
2284 : /*---------------------------------------------------------------------*
2285 : * fecIndicatorApiToInternal()
2286 : *
2287 : *
2288 : *---------------------------------------------------------------------*/
2289 :
2290 0 : static ivas_error fecIndicatorApiToInternal(
2291 : const IVAS_ENC_FEC_INDICATOR fecIndicator,
2292 : int16_t *fecIndicatorInternal )
2293 : {
2294 0 : switch ( fecIndicator )
2295 : {
2296 0 : case IVAS_ENC_FEC_LO:
2297 0 : *fecIndicatorInternal = 0;
2298 0 : break;
2299 0 : case IVAS_ENC_FEC_HI:
2300 0 : *fecIndicatorInternal = 1;
2301 0 : break;
2302 0 : default:
2303 0 : return IVAS_ERR_INTERNAL;
2304 : break;
2305 : }
2306 :
2307 0 : return IVAS_ERR_OK;
2308 : }
2309 :
2310 : #ifdef DEBUGGING
2311 : /*---------------------------------------------------------------------*
2312 : * forcedModeApiToInternal()
2313 : *
2314 : *
2315 : *---------------------------------------------------------------------*/
2316 :
2317 : static ivas_error forcedModeApiToInternal(
2318 : IVAS_ENC_FORCED_MODE forcedMode,
2319 : int16_t *forcedModeInternal )
2320 : {
2321 : switch ( forcedMode )
2322 : {
2323 : case IVAS_ENC_FORCE_SPEECH:
2324 : *forcedModeInternal = FORCE_SPEECH;
2325 : break;
2326 : case IVAS_ENC_FORCE_MUSIC:
2327 : *forcedModeInternal = FORCE_MUSIC;
2328 : break;
2329 : case IVAS_ENC_FORCE_ACELP:
2330 : *forcedModeInternal = FORCE_ACELP;
2331 : break;
2332 : case IVAS_ENC_FORCE_GSC:
2333 : *forcedModeInternal = FORCE_GSC;
2334 : break;
2335 : case IVAS_ENC_FORCE_TCX:
2336 : *forcedModeInternal = FORCE_TCX;
2337 : break;
2338 : case IVAS_ENC_FORCE_HQ:
2339 : *forcedModeInternal = FORCE_HQ;
2340 : break;
2341 : case IVAS_ENC_FORCE_UNFORCED:
2342 : *forcedModeInternal = -1;
2343 : break;
2344 : default:
2345 : return IVAS_ERR_INVALID_FORCE_MODE;
2346 : break;
2347 : }
2348 :
2349 : return IVAS_ERR_OK;
2350 : }
2351 : #endif
2352 :
2353 :
2354 : /*---------------------------------------------------------------------*
2355 : * IVAS_ENC_PrintConfig()
2356 : *
2357 : *
2358 : *---------------------------------------------------------------------*/
2359 :
2360 627 : ivas_error IVAS_ENC_PrintConfig(
2361 : const IVAS_ENC_HANDLE hIvasEnc, /* i : IVAS encoder handle */
2362 : const int16_t channelAwareModeEnabled /* i : channel-aware mode enabled flag */
2363 : )
2364 : {
2365 627 : return printConfigInfo_enc( hIvasEnc, channelAwareModeEnabled );
2366 : }
2367 :
2368 :
2369 : /*---------------------------------------------------------------------*
2370 : * IVAS_ENC_PrintDisclaimer()
2371 : *
2372 : * Print IVAS disclaimer to console
2373 : *---------------------------------------------------------------------*/
2374 :
2375 627 : void IVAS_ENC_PrintDisclaimer( void )
2376 : {
2377 627 : print_disclaimer( stderr );
2378 :
2379 627 : return;
2380 : }
2381 :
2382 :
2383 : /*---------------------------------------------------------------------*
2384 : * init_encoder_config()
2385 : *
2386 : * Initialize Encoder Config. handle
2387 : *---------------------------------------------------------------------*/
2388 :
2389 627 : static void init_encoder_config(
2390 : ENCODER_CONFIG_HANDLE hEncoderConfig /* o : configuration structure */
2391 : )
2392 : {
2393 627 : hEncoderConfig->ivas_total_brate = ACELP_12k65;
2394 627 : hEncoderConfig->max_bwidth = SWB;
2395 627 : hEncoderConfig->input_Fs = 16000;
2396 627 : hEncoderConfig->nchan_inp = 1;
2397 627 : hEncoderConfig->element_mode_init = EVS_MONO;
2398 627 : hEncoderConfig->ivas_format = UNDEFINED_FORMAT;
2399 627 : hEncoderConfig->is_binaural = 0;
2400 627 : hEncoderConfig->Opt_SC_VBR = 0;
2401 627 : hEncoderConfig->last_Opt_SC_VBR = 0;
2402 627 : hEncoderConfig->Opt_AMR_WB = 0;
2403 627 : hEncoderConfig->Opt_DTX_ON = 0;
2404 627 : hEncoderConfig->Opt_RF_ON = 0;
2405 627 : hEncoderConfig->rf_fec_offset = 0;
2406 627 : hEncoderConfig->rf_fec_indicator = 1;
2407 627 : hEncoderConfig->interval_SID = FIXED_SID_RATE;
2408 627 : hEncoderConfig->var_SID_rate_flag = 1;
2409 627 : hEncoderConfig->mc_input_setup = MC_LS_SETUP_INVALID;
2410 627 : hEncoderConfig->stereo_dmx_evs = 0;
2411 627 : hEncoderConfig->nchan_ism = 0;
2412 627 : hEncoderConfig->sba_order = 0;
2413 627 : hEncoderConfig->sba_planar = 0;
2414 627 : hEncoderConfig->ism_extended_metadata_flag = 0;
2415 : #ifdef DEBUGGING
2416 : hEncoderConfig->stereo_mode_cmdl = 0;
2417 : hEncoderConfig->force = -1;
2418 : hEncoderConfig->mdct_stereo_mode_cmdl = SMDCT_MS_DECISION;
2419 : #ifdef DEBUG_AGC_ENCODER_CMD_OPTION
2420 : hEncoderConfig->Opt_AGC_ON = SBA_AGC_DEFAULT;
2421 : #endif
2422 : #endif
2423 627 : hEncoderConfig->Opt_PCA_ON = 0;
2424 :
2425 627 : return;
2426 : }
2427 :
2428 : #ifdef DEBUGGING
2429 :
2430 : /*---------------------------------------------------------------------*
2431 : * IVAS_ENC_GetNoCLipping()
2432 : *
2433 : * return number of clipped samples
2434 : *---------------------------------------------------------------------*/
2435 :
2436 : int32_t IVAS_ENC_GetNoCLipping(
2437 : IVAS_ENC_HANDLE hIvasEnc, /* i : IVAS encoder handle */
2438 : float *maxOverload, /* o : Max overload value */
2439 : float *minOverload /* o : Min overload value */
2440 : )
2441 : {
2442 : *maxOverload = hIvasEnc->st_ivas->maxOverload;
2443 : *minOverload = hIvasEnc->st_ivas->minOverload;
2444 : return hIvasEnc->st_ivas->noClipping;
2445 : }
2446 : #endif
|