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 : /* de-allocate old buffer of metadata indices */
1245 6903 : if ( st_ivas->ind_list_metadata != NULL )
1246 : {
1247 6903 : free( st_ivas->ind_list_metadata );
1248 : }
1249 :
1250 : /* set the maximum allowed number of metadata indices in the list */
1251 6903 : st_ivas->ivas_max_num_indices_metadata = get_ivas_max_num_indices_metadata( hEncoderConfig->ivas_format, hEncoderConfig->ivas_total_brate );
1252 :
1253 6903 : if ( st_ivas->ivas_max_num_indices_metadata > 0 )
1254 : {
1255 : /* allocate new buffer of metadata indices */
1256 6903 : if ( ( st_ivas->ind_list_metadata = (INDICE_HANDLE) malloc( st_ivas->ivas_max_num_indices_metadata * sizeof( Indice ) ) ) == NULL )
1257 : {
1258 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for buffer of metadata indices!\n" ) );
1259 : }
1260 :
1261 : /* reset the list of metadata indices */
1262 2407748 : for ( i = 0; i < st_ivas->ivas_max_num_indices_metadata; i++ )
1263 : {
1264 2400845 : st_ivas->ind_list_metadata[i].nb_bits = -1;
1265 : }
1266 : }
1267 : else
1268 : {
1269 0 : st_ivas->ind_list_metadata = NULL;
1270 : }
1271 :
1272 : /* set pointers to the new buffers of indices in each element */
1273 13325 : for ( n = 0; n < st_ivas->nSCE; n++ )
1274 : {
1275 6422 : st_ivas->hSCE[n]->hCoreCoder[0]->hBstr->ind_list = st_ivas->ind_list;
1276 6422 : st_ivas->hSCE[n]->hCoreCoder[0]->hBstr->ivas_ind_list_zero = &st_ivas->ind_list;
1277 :
1278 6422 : if ( st_ivas->hSCE[n]->hMetaData != NULL )
1279 : {
1280 3681 : st_ivas->hSCE[n]->hMetaData->ind_list = st_ivas->ind_list_metadata;
1281 3681 : st_ivas->hSCE[n]->hMetaData->ivas_ind_list_zero = &st_ivas->ind_list_metadata;
1282 : }
1283 : }
1284 :
1285 13541 : for ( n = 0; n < st_ivas->nCPE; n++ )
1286 : {
1287 19914 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
1288 : {
1289 13276 : st_ivas->hCPE[n]->hCoreCoder[ch]->hBstr->ind_list = st_ivas->ind_list;
1290 13276 : st_ivas->hCPE[n]->hCoreCoder[ch]->hBstr->ivas_ind_list_zero = &st_ivas->ind_list;
1291 : }
1292 :
1293 6638 : if ( st_ivas->hCPE[n]->hMetaData != NULL )
1294 : {
1295 4435 : st_ivas->hCPE[n]->hMetaData->ind_list = st_ivas->ind_list_metadata;
1296 4435 : st_ivas->hCPE[n]->hMetaData->ivas_ind_list_zero = &st_ivas->ind_list_metadata;
1297 : }
1298 : }
1299 : }
1300 :
1301 427388 : if ( hIvasEnc->switchingActive && hEncoderConfig->ivas_format == MONO_FORMAT )
1302 : {
1303 3100 : copy_encoder_config( st_ivas, hCoreCoder, 0 );
1304 3100 : hEncoderConfig->last_ivas_total_brate = hEncoderConfig->ivas_total_brate;
1305 : }
1306 :
1307 : /* run the main encoding routine */
1308 427388 : if ( hEncoderConfig->ivas_format == MONO_FORMAT ) /* EVS mono */
1309 : {
1310 3100 : hCoreCoder->total_brate = hEncoderConfig->ivas_total_brate; /* needed in case of bitrate switching */
1311 : #ifdef DEBUGGING
1312 : hCoreCoder->id_element = 0;
1313 : #endif
1314 :
1315 3100 : if ( hEncoderConfig->stereo_dmx_evs == 1 )
1316 : {
1317 2100 : inputBufferSize /= 2;
1318 2100 : stereo_dmx_evs_enc( st_ivas->hStereoDmxEVS, hEncoderConfig->input_Fs, inputBuffer, inputBufferSize, hEncoderConfig->is_binaural );
1319 : }
1320 :
1321 3100 : if ( hEncoderConfig->Opt_AMR_WB )
1322 : {
1323 0 : amr_wb_enc( hCoreCoder, inputBuffer, st_ivas->mem_hp20_in[0], inputBufferSize );
1324 : }
1325 : else
1326 : {
1327 3100 : if ( ( error = evs_enc( hCoreCoder, inputBuffer, st_ivas->mem_hp20_in[0], inputBufferSize ) ) != IVAS_ERR_OK )
1328 : {
1329 0 : return error;
1330 : }
1331 : }
1332 : }
1333 : else /* IVAS */
1334 : {
1335 424288 : if ( ( error = ivas_enc( st_ivas, inputBuffer, inputBufferSize ) ) != IVAS_ERR_OK )
1336 : {
1337 0 : return error;
1338 : }
1339 : }
1340 :
1341 : /* write indices into bitstream buffer */
1342 427388 : write_indices_ivas( st_ivas, outputBitStream, numOutBits );
1343 :
1344 : /* Reset switching flag before next call - can be set to "true" by some setters */
1345 427388 : hIvasEnc->switchingActive = false;
1346 :
1347 427388 : return error;
1348 : }
1349 :
1350 :
1351 : /*---------------------------------------------------------------------*
1352 : * IVAS_ENC_EncodeFrameToCompact()
1353 : *
1354 : * Main function to encode one frame to a compact bitstream (bytestream)
1355 : *---------------------------------------------------------------------*/
1356 :
1357 0 : ivas_error IVAS_ENC_EncodeFrameToCompact(
1358 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
1359 : int16_t *inputBuffer, /* i : PCM input */
1360 : const int16_t inputBufferSize, /* i : total number of samples in the input buffer. Related function: IVAS_ENC_GetInputBufferSize() */
1361 : uint8_t *outputBitStream, /* o : pointer to compact output bitstream. The array must already be allocated. */
1362 : uint16_t *numOutBits /* o : number of bits written to output bitstream */
1363 : )
1364 : {
1365 : ivas_error error;
1366 : uint16_t bitstream[IVAS_MAX_BITS_PER_FRAME];
1367 :
1368 0 : if ( ( error = IVAS_ENC_EncodeFrameToSerial( hIvasEnc, inputBuffer, inputBufferSize, bitstream, numOutBits ) ) != IVAS_ERR_OK )
1369 : {
1370 0 : return error;
1371 : }
1372 :
1373 0 : convertSerialToBytestream( bitstream, *numOutBits, outputBitStream );
1374 :
1375 0 : return IVAS_ERR_OK;
1376 : }
1377 :
1378 :
1379 : /*---------------------------------------------------------------------*
1380 : * IVAS_ENC_SetBandwidth()
1381 : *
1382 : *
1383 : *---------------------------------------------------------------------*/
1384 :
1385 21150 : ivas_error IVAS_ENC_SetBandwidth(
1386 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
1387 : const IVAS_ENC_BANDWIDTH maxBandwidth /* i : bandwidth limitation to be used */
1388 : )
1389 : {
1390 : ivas_error error;
1391 :
1392 : /* Do additional checks for user-facing function */
1393 21150 : if ( ( error = doCommonSetterChecks( hIvasEnc ) ) != IVAS_ERR_OK )
1394 : {
1395 0 : return error;
1396 : }
1397 :
1398 : /* Use internal function to set bandiwdth */
1399 21150 : return setBandwidth( hIvasEnc, maxBandwidth );
1400 : }
1401 :
1402 :
1403 : /*---------------------------------------------------------------------*
1404 : * IVAS_ENC_SetBitrate()
1405 : *
1406 : *
1407 : *---------------------------------------------------------------------*/
1408 :
1409 58138 : ivas_error IVAS_ENC_SetBitrate(
1410 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
1411 : const int32_t totalBitrate /* i : requested bitrate of the output bitstream */
1412 : )
1413 : {
1414 : ivas_error error;
1415 :
1416 : /* Do additional checks for user-facing function */
1417 58138 : if ( ( error = doCommonSetterChecks( hIvasEnc ) ) != IVAS_ERR_OK )
1418 : {
1419 0 : return error;
1420 : }
1421 :
1422 : /* Use internal function to set bitrate */
1423 58138 : return setBitrate( hIvasEnc, totalBitrate );
1424 : }
1425 :
1426 :
1427 : /*---------------------------------------------------------------------*
1428 : * IVAS_ENC_SetChannelAwareConfig()
1429 : *
1430 : * Sets the configuration of channel-aware mode. Since CA mode is only
1431 : * supported at 13.2 kbps, this function has no effect at other bitrates.
1432 : *---------------------------------------------------------------------*/
1433 :
1434 0 : ivas_error IVAS_ENC_SetChannelAwareConfig(
1435 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
1436 : const IVAS_ENC_CHANNEL_AWARE_CONFIG rfConfig /* i : configuration of channel-aware mode */
1437 : )
1438 : {
1439 : ivas_error error;
1440 :
1441 : /* Do additional checks for user-facing function */
1442 0 : if ( ( error = doCommonSetterChecks( hIvasEnc ) ) != IVAS_ERR_OK )
1443 : {
1444 0 : return error;
1445 : }
1446 :
1447 : /* Use internal function to set CA config */
1448 0 : return setChannelAwareConfig( hIvasEnc, rfConfig );
1449 : }
1450 :
1451 :
1452 : #ifdef DEBUGGING
1453 : /*---------------------------------------------------------------------*
1454 : * IVAS_ENC_SetForcedMode()
1455 : *
1456 : *
1457 : *---------------------------------------------------------------------*/
1458 :
1459 : ivas_error IVAS_ENC_SetForcedMode(
1460 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
1461 : const IVAS_ENC_FORCED_MODE forcedMode /* i : forced coding mode */
1462 : )
1463 : {
1464 : int16_t newForced;
1465 : ivas_error error;
1466 :
1467 : /* Do additional checks for user-facing function */
1468 : if ( ( error = doCommonSetterChecks( hIvasEnc ) ) != IVAS_ERR_OK )
1469 : {
1470 : return error;
1471 : }
1472 :
1473 : if ( ( error = forcedModeApiToInternal( forcedMode, &newForced ) ) != IVAS_ERR_OK )
1474 : {
1475 : return error;
1476 : }
1477 :
1478 : if ( hIvasEnc->st_ivas->hEncoderConfig->force != newForced )
1479 : {
1480 : hIvasEnc->st_ivas->hEncoderConfig->force = newForced;
1481 : hIvasEnc->switchingActive = true;
1482 : }
1483 :
1484 : return IVAS_ERR_OK;
1485 : }
1486 : #endif /* DEBUGGING */
1487 :
1488 :
1489 : /*---------------------------------------------------------------------*
1490 : * IVAS_ENC_GetDefaultBandwidth()
1491 : *
1492 : *
1493 : *---------------------------------------------------------------------*/
1494 :
1495 448 : IVAS_ENC_BANDWIDTH IVAS_ENC_GetDefaultBandwidth( const bool isEVS )
1496 : {
1497 448 : IVAS_ENC_BANDWIDTH bwidth = IVAS_ENC_BANDWIDTH_FB;
1498 :
1499 448 : if ( isEVS )
1500 : {
1501 3 : bwidth = IVAS_ENC_BANDWIDTH_SWB;
1502 : }
1503 :
1504 448 : return bwidth;
1505 : }
1506 :
1507 :
1508 : /*---------------------------------------------------------------------*
1509 : * IVAS_ENC_GetDefaultDtxConfig()
1510 : *
1511 : *
1512 : *---------------------------------------------------------------------*/
1513 :
1514 627 : IVAS_ENC_DTX_CONFIG IVAS_ENC_GetDefaultDtxConfig( void )
1515 : {
1516 : IVAS_ENC_DTX_CONFIG defaultDtxConfig;
1517 627 : defaultDtxConfig.enabled = false;
1518 627 : defaultDtxConfig.SID_interval = 0;
1519 627 : defaultDtxConfig.variable_SID_rate = false;
1520 :
1521 627 : return defaultDtxConfig;
1522 : }
1523 :
1524 :
1525 : /*---------------------------------------------------------------------*
1526 : * IVAS_ENC_GetDefaultChannelAwareConfig()
1527 : *
1528 : *
1529 : *---------------------------------------------------------------------*/
1530 :
1531 1251 : IVAS_ENC_CHANNEL_AWARE_CONFIG IVAS_ENC_GetDefaultChannelAwareConfig( void )
1532 : {
1533 : IVAS_ENC_CHANNEL_AWARE_CONFIG defaultCaConfig;
1534 1251 : defaultCaConfig.channelAwareModeEnabled = 0;
1535 1251 : defaultCaConfig.fec_indicator = IVAS_ENC_FEC_HI;
1536 1251 : defaultCaConfig.fec_offset = 0;
1537 :
1538 1251 : return defaultCaConfig;
1539 : }
1540 :
1541 :
1542 : /*---------------------------------------------------------------------*
1543 : * IVAS_ENC_GetErrorMessage()
1544 : *
1545 : *
1546 : *---------------------------------------------------------------------*/
1547 :
1548 0 : const char *IVAS_ENC_GetErrorMessage(
1549 : ivas_error error /* i : encoder error code enum */
1550 : )
1551 : {
1552 0 : return ivas_error_to_string( error );
1553 : }
1554 :
1555 :
1556 : /*---------------------------------------------------------------------*
1557 : * Local functions
1558 : *---------------------------------------------------------------------*/
1559 :
1560 627 : static ivas_error printConfigInfo_enc(
1561 : IVAS_ENC_HANDLE hIvasEnc,
1562 : const int16_t channelAwareModeEnabled )
1563 : {
1564 : Encoder_Struct *st_ivas;
1565 : ENCODER_CONFIG_HANDLE hEncoderConfig;
1566 : int16_t newBandwidthApi;
1567 : ivas_error error;
1568 :
1569 627 : st_ivas = hIvasEnc->st_ivas;
1570 627 : hEncoderConfig = st_ivas->hEncoderConfig;
1571 :
1572 : /*-----------------------------------------------------------------*
1573 : * Print input signal sampling frequency
1574 : *-----------------------------------------------------------------*/
1575 :
1576 627 : fprintf( stdout, "Input sampling rate: %d Hz\n", hEncoderConfig->input_Fs );
1577 :
1578 : /*-----------------------------------------------------------------*
1579 : * Print bitrate
1580 : *-----------------------------------------------------------------*/
1581 :
1582 627 : if ( st_ivas->hEncoderConfig->Opt_SC_VBR )
1583 : {
1584 0 : fprintf( stdout, "Average bitrate: %.2f kbps\n", (float) ACELP_5k90 / 1000 );
1585 : }
1586 : else
1587 : {
1588 627 : fprintf( stdout, "Bitrate: %.2f kbps\n", (float) hEncoderConfig->ivas_total_brate / 1000 );
1589 : }
1590 :
1591 : /*-----------------------------------------------------------------*
1592 : * Print IVAS format
1593 : *-----------------------------------------------------------------*/
1594 :
1595 627 : if ( hEncoderConfig->ivas_format == MONO_FORMAT )
1596 : {
1597 3 : if ( hEncoderConfig->stereo_dmx_evs )
1598 : {
1599 2 : fprintf( stdout, "IVAS format: stereo downmix to bit-exact EVS mono\n" );
1600 : }
1601 : else
1602 : {
1603 1 : fprintf( stdout, "IVAS format: bit-exact EVS mono\n" );
1604 : }
1605 : }
1606 624 : else if ( hEncoderConfig->ivas_format == STEREO_FORMAT )
1607 : {
1608 : #ifdef DEBUGGING
1609 : if ( hEncoderConfig->stereo_mode_cmdl == 1 )
1610 : {
1611 : fprintf( stdout, "IVAS format: stereo - Unified stereo\n" );
1612 : }
1613 : else if ( hEncoderConfig->element_mode_init == IVAS_CPE_DFT )
1614 : {
1615 : fprintf( stdout, "IVAS format: stereo - DFT stereo\n" );
1616 : }
1617 : else if ( hEncoderConfig->element_mode_init == IVAS_CPE_TD )
1618 : {
1619 : fprintf( stdout, "IVAS format: stereo - TD stereo\n" );
1620 : }
1621 : else if ( hEncoderConfig->element_mode_init == IVAS_CPE_MDCT )
1622 : {
1623 : fprintf( stdout, "IVAS format: stereo - MDCT stereo\n" );
1624 : }
1625 : #else
1626 68 : if ( hEncoderConfig->element_mode_init != IVAS_CPE_MDCT )
1627 : {
1628 50 : fprintf( stdout, "IVAS format: stereo - Unified stereo\n" );
1629 : }
1630 : else
1631 : {
1632 18 : fprintf( stdout, "IVAS format: stereo - MDCT stereo\n" );
1633 : }
1634 : #endif
1635 : }
1636 556 : else if ( hEncoderConfig->ivas_format == ISM_FORMAT )
1637 : {
1638 74 : if ( hEncoderConfig->ivas_total_brate <= ACELP_32k && hEncoderConfig->nchan_inp > 2 )
1639 : {
1640 17 : fprintf( stdout, "IVAS format: Param-ISM (%i streams)\n", hEncoderConfig->nchan_inp );
1641 : }
1642 : else
1643 : {
1644 57 : fprintf( stdout, "IVAS format: ISM (%i streams)\n", hEncoderConfig->nchan_inp );
1645 : }
1646 : }
1647 482 : else if ( hEncoderConfig->ivas_format == SBA_FORMAT )
1648 : {
1649 244 : fprintf( stdout, "IVAS format: Scene Based Audio, Ambisonic order %i %s ", hEncoderConfig->sba_order, hEncoderConfig->sba_planar ? "(Planar)" : "" );
1650 244 : if ( hEncoderConfig->Opt_PCA_ON )
1651 : {
1652 4 : fprintf( stdout, "- PCA configured with signal adaptive decision " );
1653 : }
1654 :
1655 : #ifdef DEBUG_AGC_ENCODER_CMD_OPTION
1656 : switch ( hEncoderConfig->Opt_AGC_ON )
1657 : {
1658 : case SBA_AGC_FORCE_ENABLE:
1659 : fprintf( stdout, "- AGC FORCED ON " );
1660 : break;
1661 : case SBA_AGC_FORCE_DISABLE:
1662 : fprintf( stdout, "- AGC FORCED OFF " );
1663 : break;
1664 : case SBA_AGC_DEFAULT:
1665 : fprintf( stdout, "- AGC default mode " );
1666 : break;
1667 : default:
1668 : fprintf( stdout, "- AGC unknown " );
1669 : break;
1670 : }
1671 : #endif
1672 244 : fprintf( stdout, "\n" );
1673 : }
1674 238 : else if ( hEncoderConfig->ivas_format == MASA_FORMAT )
1675 : {
1676 75 : fprintf( stdout, "IVAS format: MASA format\n" );
1677 : }
1678 163 : else if ( hEncoderConfig->ivas_format == MC_FORMAT )
1679 : {
1680 82 : if ( hEncoderConfig->mc_input_setup == MC_LS_SETUP_5_1 )
1681 : {
1682 43 : fprintf( stdout, "IVAS mode: Multi-Channel 5.1 \n" );
1683 : }
1684 39 : else if ( hEncoderConfig->mc_input_setup == MC_LS_SETUP_7_1 )
1685 : {
1686 4 : fprintf( stdout, "IVAS mode: Multi-Channel 7.1 \n" );
1687 : }
1688 35 : else if ( hEncoderConfig->mc_input_setup == MC_LS_SETUP_5_1_2 )
1689 : {
1690 6 : fprintf( stdout, "IVAS mode: Multi-Channel 5.1+2 \n" );
1691 : }
1692 29 : else if ( hEncoderConfig->mc_input_setup == MC_LS_SETUP_5_1_4 )
1693 : {
1694 6 : fprintf( stdout, "IVAS mode: Multi-Channel 5.1+4\n" );
1695 : }
1696 23 : else if ( hEncoderConfig->mc_input_setup == MC_LS_SETUP_7_1_4 )
1697 : {
1698 23 : fprintf( stdout, "IVAS mode: Multi-Channel 7.1+4\n" );
1699 : }
1700 : }
1701 81 : else if ( hEncoderConfig->ivas_format == SBA_ISM_FORMAT )
1702 : {
1703 37 : fprintf( stdout, "IVAS format: combined ISM and SBA (%i ISM stream(s))\n", hEncoderConfig->nchan_ism );
1704 : }
1705 44 : else if ( hEncoderConfig->ivas_format == MASA_ISM_FORMAT )
1706 : {
1707 44 : fprintf( stdout, "IVAS format: combined ISM and MASA (%i ISM stream(s))\n", hEncoderConfig->nchan_ism );
1708 : }
1709 :
1710 627 : if ( hEncoderConfig->is_binaural )
1711 : {
1712 0 : fprintf( stdout, "Optional indication: binaural audio\n" );
1713 : }
1714 :
1715 : /*-----------------------------------------------------------------*
1716 : * Print CNG update interval, if DTX is activated
1717 : *-----------------------------------------------------------------*/
1718 :
1719 627 : if ( hEncoderConfig->Opt_DTX_ON )
1720 : {
1721 126 : if ( hEncoderConfig->var_SID_rate_flag )
1722 : {
1723 0 : fprintf( stdout, "DTX: ON, variable CNG update interval\n" );
1724 : }
1725 : else
1726 : {
1727 126 : fprintf( stdout, "DTX: ON, CNG update interval = %d frames\n", hEncoderConfig->interval_SID );
1728 : }
1729 : }
1730 :
1731 : /*-----------------------------------------------------------------*
1732 : * Print potential limitation of audio bandwidth
1733 : *-----------------------------------------------------------------*/
1734 :
1735 627 : if ( ( error = bandwidthApiToInternal( hIvasEnc->newBandwidthApi, &newBandwidthApi ) ) != IVAS_ERR_OK )
1736 : {
1737 0 : return error;
1738 : }
1739 :
1740 627 : if ( st_ivas->hEncoderConfig->Opt_SC_VBR && !hEncoderConfig->Opt_DTX_ON )
1741 : {
1742 0 : return IVAS_ERROR( IVAS_ERR_WRONG_PARAMS, "\nError: SC-VBR 5900 bps not supported without DTX\n\n" );
1743 : }
1744 :
1745 627 : if ( hEncoderConfig->ivas_format == MONO_FORMAT )
1746 : {
1747 3 : if ( newBandwidthApi != hEncoderConfig->max_bwidth )
1748 : {
1749 0 : if ( newBandwidthApi == FB )
1750 : {
1751 0 : fprintf( stdout, "\nFB coding not supported below %.2f kbps. ", ACELP_16k40 / 1000.f );
1752 0 : if ( hEncoderConfig->max_bwidth == WB )
1753 : {
1754 0 : fprintf( stdout, "Switching to WB.\n" );
1755 : }
1756 : else
1757 : {
1758 0 : fprintf( stdout, "Switching to SWB.\n" );
1759 : }
1760 : }
1761 0 : else if ( newBandwidthApi == SWB )
1762 : {
1763 0 : fprintf( stdout, "\nSWB coding not supported below %.2f kbps. Switching to WB.\n", ACELP_9k60 / 1000.f );
1764 : }
1765 : }
1766 :
1767 : /* in case of 8kHz input sampling or "-max_band NB", require the total bitrate to be below 24.40 kbps */
1768 3 : if ( ( newBandwidthApi == NB || hEncoderConfig->input_Fs == 8000 ) && hEncoderConfig->ivas_total_brate > ACELP_24k40 )
1769 : {
1770 0 : fprintf( stdout, "\nError: Unsupported mode NB %d bps, NB mode supports rates 5900-24400 bps\n\n", hEncoderConfig->ivas_total_brate );
1771 0 : return IVAS_ERR_INVALID_BITRATE;
1772 : }
1773 : }
1774 : else
1775 : {
1776 624 : if ( newBandwidthApi != hEncoderConfig->max_bwidth )
1777 : {
1778 105 : if ( hEncoderConfig->ivas_format == ISM_FORMAT )
1779 : {
1780 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 );
1781 : }
1782 : else
1783 : {
1784 82 : fprintf( stdout, "\nFB coding not supported below %.2f kbps. Switching to SWB.\n", MIN_BRATE_FB_STEREO / 1000.f );
1785 : }
1786 : }
1787 : }
1788 :
1789 : /*-----------------------------------------------------------------*
1790 : * Print Channel-aware limitation
1791 : *-----------------------------------------------------------------*/
1792 :
1793 627 : if ( channelAwareModeEnabled )
1794 : {
1795 0 : if ( hEncoderConfig->Opt_RF_ON == 0 )
1796 : {
1797 0 : fprintf( stdout, "\nChannel-aware mode is supported at 13.2 kbps 32/48 kHz only. Switching to normal mode.\n" );
1798 : }
1799 : }
1800 :
1801 627 : return IVAS_ERR_OK;
1802 : }
1803 :
1804 :
1805 : /*---------------------------------------------------------------------*
1806 : * setBitrate()
1807 : *
1808 : *
1809 : *---------------------------------------------------------------------*/
1810 :
1811 58138 : static ivas_error setBitrate(
1812 : IVAS_ENC_HANDLE hIvasEnc,
1813 : const int32_t totalBitrate )
1814 : {
1815 : Encoder_Struct *st_ivas;
1816 : ENCODER_CONFIG_HANDLE hEncoderConfig;
1817 : ivas_error error;
1818 :
1819 58138 : st_ivas = hIvasEnc->st_ivas;
1820 58138 : hEncoderConfig = st_ivas->hEncoderConfig;
1821 :
1822 58138 : hEncoderConfig->ivas_total_brate = totalBitrate;
1823 58138 : hIvasEnc->switchingActive = true;
1824 :
1825 : /* channel-aware mode is supported only at 13.20 kbps */
1826 58138 : if ( hEncoderConfig->Opt_RF_ON && hEncoderConfig->ivas_total_brate != ACELP_13k20 )
1827 : {
1828 0 : assert( 0 && "\nChannel-aware mode is supported only at 13.20 kbps\n" );
1829 : hEncoderConfig->Opt_RF_ON = 0;
1830 : }
1831 :
1832 58138 : if ( hEncoderConfig->ivas_total_brate == ACELP_5k90 )
1833 : {
1834 0 : st_ivas->hEncoderConfig->Opt_SC_VBR = 1;
1835 0 : hEncoderConfig->ivas_total_brate = ACELP_7k20;
1836 : }
1837 : else
1838 : {
1839 58138 : st_ivas->hEncoderConfig->Opt_SC_VBR = 0;
1840 : }
1841 :
1842 : /* check if the entered bitrate is supported */
1843 58138 : if ( hEncoderConfig->element_mode_init > EVS_MONO )
1844 : {
1845 58138 : if ( !is_IVAS_bitrate( hEncoderConfig->ivas_total_brate ) )
1846 : {
1847 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Incorrect bitrate specification in IVAS: %d", hEncoderConfig->ivas_total_brate );
1848 : }
1849 : }
1850 : else
1851 : {
1852 0 : if ( !is_EVS_bitrate( hEncoderConfig->ivas_total_brate, &hEncoderConfig->Opt_AMR_WB ) )
1853 : {
1854 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Incorrect bitrate specification in EVS mono: %d", hEncoderConfig->ivas_total_brate );
1855 : }
1856 :
1857 : /* in case of 8kHz signal, limit the total bitrate to 24.40 kbps */
1858 0 : if ( hEncoderConfig->input_Fs == 8000 && hEncoderConfig->ivas_total_brate > ACELP_24k40 )
1859 : {
1860 0 : hEncoderConfig->ivas_total_brate = ACELP_24k40;
1861 : }
1862 : }
1863 :
1864 58138 : if ( hEncoderConfig->ivas_format == ISM_FORMAT )
1865 : {
1866 10086 : if ( ( error = sanitizeBitrateISM( hEncoderConfig, hIvasEnc->extMetadataApi ) ) != IVAS_ERR_OK )
1867 : {
1868 0 : return error;
1869 : }
1870 : }
1871 :
1872 58138 : st_ivas->codec_mode = MODE1;
1873 :
1874 58138 : if ( hEncoderConfig->element_mode_init == EVS_MONO )
1875 : {
1876 0 : if ( hEncoderConfig->Opt_AMR_WB )
1877 : {
1878 0 : st_ivas->codec_mode = MODE1;
1879 : }
1880 : else
1881 : {
1882 0 : st_ivas->codec_mode = get_codec_mode( hEncoderConfig->ivas_total_brate );
1883 : }
1884 : }
1885 :
1886 58138 : return IVAS_ERR_OK;
1887 : }
1888 :
1889 :
1890 : /*---------------------------------------------------------------------*
1891 : * setChannelAwareConfig()
1892 : *
1893 : *
1894 : *---------------------------------------------------------------------*/
1895 :
1896 627 : static ivas_error setChannelAwareConfig(
1897 : IVAS_ENC_HANDLE hIvasEnc,
1898 : const IVAS_ENC_CHANNEL_AWARE_CONFIG caConfig )
1899 : {
1900 : int16_t newFecIndicator;
1901 : ivas_error error;
1902 : Encoder_Struct *st_ivas;
1903 : ENCODER_CONFIG_HANDLE hEncoderConfig;
1904 :
1905 627 : st_ivas = hIvasEnc->st_ivas;
1906 627 : hEncoderConfig = st_ivas->hEncoderConfig;
1907 :
1908 : /* channel-aware mode is supported only at 13.20 kbps and with WB or SWB bandwidth */
1909 627 : if ( ( caConfig.channelAwareModeEnabled && st_ivas->hEncoderConfig->ivas_total_brate != ACELP_13k20 ) || ( hEncoderConfig->Opt_RF_ON && hEncoderConfig->input_Fs == 8000 ) )
1910 : {
1911 0 : hEncoderConfig->Opt_RF_ON = 0;
1912 0 : hEncoderConfig->rf_fec_offset = 0;
1913 0 : return IVAS_ERR_OK;
1914 : }
1915 :
1916 627 : if ( caConfig.channelAwareModeEnabled )
1917 : {
1918 0 : hEncoderConfig->Opt_RF_ON = 1;
1919 :
1920 : /* Convert FEC indicator from API type */
1921 0 : if ( ( error = fecIndicatorApiToInternal( caConfig.fec_indicator, &newFecIndicator ) ) != IVAS_ERR_OK )
1922 : {
1923 0 : return error;
1924 : }
1925 :
1926 : /* Set new values only if they differ from current values */
1927 0 : if ( ( newFecIndicator != hEncoderConfig->rf_fec_indicator || caConfig.fec_offset != hEncoderConfig->rf_fec_offset ) )
1928 : {
1929 0 : hEncoderConfig->rf_fec_indicator = newFecIndicator;
1930 :
1931 : /* Check if new FEC offset has a valid value */
1932 0 : if ( caConfig.fec_offset == 0 || caConfig.fec_offset == 2 || caConfig.fec_offset == 3 || caConfig.fec_offset == 5 || caConfig.fec_offset == 7 )
1933 : {
1934 0 : hEncoderConfig->rf_fec_offset = caConfig.fec_offset;
1935 : }
1936 : else
1937 : {
1938 0 : return IVAS_ERR_INVALID_FEC_OFFSET;
1939 : }
1940 :
1941 0 : hIvasEnc->switchingActive = true;
1942 : }
1943 :
1944 : /* Save a copy of FEC offset value - needed during encoding */
1945 0 : hIvasEnc->rf_fec_offset_loc = hEncoderConfig->rf_fec_offset;
1946 : }
1947 : else
1948 : {
1949 627 : hEncoderConfig->Opt_RF_ON = 0;
1950 : }
1951 :
1952 627 : return IVAS_ERR_OK;
1953 : }
1954 :
1955 :
1956 : /*---------------------------------------------------------------------*
1957 : * doCommonConfigureChecks()
1958 : *
1959 : *
1960 : *---------------------------------------------------------------------*/
1961 :
1962 627 : static ivas_error doCommonConfigureChecks(
1963 : IVAS_ENC_HANDLE hIvasEnc )
1964 : {
1965 627 : if ( hIvasEnc == NULL || hIvasEnc->st_ivas == NULL )
1966 : {
1967 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1968 : }
1969 :
1970 627 : if ( hIvasEnc->isConfigured )
1971 : {
1972 0 : return IVAS_ERR_RECONFIGURE_NOT_SUPPORTED;
1973 : }
1974 :
1975 627 : return IVAS_ERR_OK;
1976 : }
1977 :
1978 :
1979 : /*---------------------------------------------------------------------*
1980 : * doCommonSetterChecks()
1981 : *
1982 : *
1983 : *---------------------------------------------------------------------*/
1984 :
1985 79288 : static ivas_error doCommonSetterChecks(
1986 : IVAS_ENC_HANDLE hIvasEnc )
1987 : {
1988 79288 : if ( hIvasEnc == NULL || hIvasEnc->st_ivas == NULL )
1989 : {
1990 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1991 : }
1992 :
1993 : /* Currently settings can be changed only after configuration step */
1994 79288 : if ( !hIvasEnc->isConfigured )
1995 : {
1996 0 : return IVAS_ERR_NOT_CONFIGURED;
1997 : }
1998 :
1999 79288 : return IVAS_ERR_OK;
2000 : }
2001 :
2002 :
2003 : /*---------------------------------------------------------------------*
2004 : * sanitizeBandwidth()
2005 : *
2006 : *
2007 : *---------------------------------------------------------------------*/
2008 :
2009 428015 : static ivas_error sanitizeBandwidth(
2010 : const IVAS_ENC_HANDLE hIvasEnc )
2011 : {
2012 : ENCODER_CONFIG_HANDLE hEncoderConfig;
2013 : int16_t max_bwidth_tmp;
2014 :
2015 428015 : hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
2016 :
2017 428015 : max_bwidth_tmp = hIvasEnc->newBandwidthApi;
2018 :
2019 : /* Prevent st_ivas->max_bwidth from being higher than Fs/2 */
2020 428015 : if ( hEncoderConfig->input_Fs == 8000 && max_bwidth_tmp > NB )
2021 : {
2022 0 : max_bwidth_tmp = NB;
2023 : }
2024 428015 : else if ( hEncoderConfig->input_Fs == 16000 && max_bwidth_tmp > WB )
2025 : {
2026 0 : max_bwidth_tmp = WB;
2027 : }
2028 428015 : else if ( hEncoderConfig->input_Fs == 32000 && max_bwidth_tmp > SWB )
2029 : {
2030 1351 : max_bwidth_tmp = SWB;
2031 : }
2032 :
2033 : /* NB coding not supported in IVAS. Switching to WB. */
2034 428015 : if ( max_bwidth_tmp == NB && hEncoderConfig->ivas_format != UNDEFINED_FORMAT && hEncoderConfig->ivas_format != MONO_FORMAT )
2035 : {
2036 301 : if ( hEncoderConfig->input_Fs >= 16000 )
2037 : {
2038 301 : max_bwidth_tmp = WB;
2039 : }
2040 : else
2041 : {
2042 0 : return IVAS_ERR_INVALID_BITRATE;
2043 : }
2044 : }
2045 :
2046 428015 : if ( hEncoderConfig->ivas_format == MONO_FORMAT )
2047 : {
2048 : #if 0 // IVAS_fmToDo: temporary disabled to keep EVS bit-exactness -> to be verified
2049 : if ( max_bwidth_tmp == FB && hEncoderConfig->ivas_total_brate < ACELP_16k40 )
2050 : {
2051 : if ( hEncoderConfig->ivas_total_brate < ACELP_9k60 )
2052 : {
2053 : max_bwidth_tmp = WB;
2054 : }
2055 : else
2056 : {
2057 : max_bwidth_tmp = SWB;
2058 : }
2059 : }
2060 :
2061 : if ( max_bwidth_tmp == SWB && hEncoderConfig->ivas_total_brate < ACELP_9k60 )
2062 : {
2063 : max_bwidth_tmp = WB;
2064 : }
2065 :
2066 : /* in case of 8kHz input sampling or "-max_band NB", require the total bitrate to be below 24.40 kbps */
2067 : if ( ( max_bwidth_tmp == NB || hEncoderConfig->input_Fs == 8000 ) && hEncoderConfig->ivas_total_brate > ACELP_24k40 )
2068 : {
2069 : if ( hEncoderConfig->input_Fs >= 16000 )
2070 : {
2071 : max_bwidth_tmp = WB;
2072 : }
2073 : else
2074 : {
2075 : return IVAS_ERR_INVALID_BITRATE;
2076 : }
2077 : }
2078 : #endif
2079 : }
2080 : else
2081 : {
2082 424912 : if ( max_bwidth_tmp == FB && ( ( hEncoderConfig->ivas_format != ISM_FORMAT && hEncoderConfig->ivas_total_brate < MIN_BRATE_FB_STEREO ) ||
2083 232813 : ( hEncoderConfig->ivas_format == ISM_FORMAT && hEncoderConfig->ivas_total_brate / hEncoderConfig->nchan_ism < MIN_BRATE_FB_ISM ) ) )
2084 : {
2085 60973 : max_bwidth_tmp = SWB;
2086 : }
2087 : }
2088 :
2089 428015 : if ( hEncoderConfig->max_bwidth != max_bwidth_tmp )
2090 : {
2091 5109 : hEncoderConfig->max_bwidth = max_bwidth_tmp;
2092 5109 : hIvasEnc->switchingActive = true;
2093 : }
2094 :
2095 428015 : return IVAS_ERR_OK;
2096 : }
2097 :
2098 :
2099 : /*---------------------------------------------------------------------*
2100 : * sanitizeBitrateISM()
2101 : *
2102 : *
2103 : *---------------------------------------------------------------------*/
2104 :
2105 10160 : static ivas_error sanitizeBitrateISM(
2106 : const ENCODER_CONFIG_HANDLE hEncoderConfig,
2107 : const bool extMetadataApi )
2108 : {
2109 10160 : if ( hEncoderConfig->ivas_total_brate > IVAS_128k && hEncoderConfig->nchan_inp == 1 )
2110 : {
2111 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for 1 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
2112 : }
2113 :
2114 10160 : if ( hEncoderConfig->ivas_total_brate > IVAS_256k && hEncoderConfig->nchan_inp == 2 )
2115 : {
2116 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for 2 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
2117 : }
2118 :
2119 10160 : if ( hEncoderConfig->ivas_total_brate > IVAS_384k && hEncoderConfig->nchan_inp == 3 )
2120 : {
2121 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for 3 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
2122 : }
2123 :
2124 10160 : if ( hEncoderConfig->ivas_total_brate < IVAS_16k4 && hEncoderConfig->nchan_inp == 2 )
2125 : {
2126 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too low bitrate for 2 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
2127 : }
2128 :
2129 10160 : if ( hEncoderConfig->ivas_total_brate < IVAS_24k4 && hEncoderConfig->nchan_inp == 3 )
2130 : {
2131 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too low bitrate for 3 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
2132 : }
2133 :
2134 10160 : if ( hEncoderConfig->ivas_total_brate < IVAS_24k4 && hEncoderConfig->nchan_inp == 4 )
2135 : {
2136 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too low bitrate for 4 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
2137 : }
2138 :
2139 10160 : if ( extMetadataApi )
2140 : {
2141 1007 : hEncoderConfig->ism_extended_metadata_flag = ( hEncoderConfig->ivas_total_brate >= ISM_EXTENDED_METADATA_BRATE );
2142 : }
2143 : else
2144 : {
2145 9153 : hEncoderConfig->ism_extended_metadata_flag = 0;
2146 : }
2147 :
2148 10160 : return IVAS_ERR_OK;
2149 : }
2150 :
2151 :
2152 : /*---------------------------------------------------------------------*
2153 : * setBandwidth()
2154 : *
2155 : *
2156 : *---------------------------------------------------------------------*/
2157 :
2158 21777 : static ivas_error setBandwidth(
2159 : IVAS_ENC_HANDLE hIvasEnc,
2160 : const IVAS_ENC_BANDWIDTH maxBandwidth )
2161 : {
2162 : ivas_error error;
2163 : int16_t newBandwidth;
2164 : ENCODER_CONFIG_HANDLE hEncoderConfig;
2165 :
2166 21777 : hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
2167 :
2168 : /* Convert bandwidth from API type */
2169 21777 : if ( ( error = bandwidthApiToInternal( maxBandwidth, &newBandwidth ) ) != IVAS_ERR_OK )
2170 : {
2171 0 : return error;
2172 : }
2173 :
2174 21777 : hIvasEnc->newBandwidthApi = newBandwidth;
2175 :
2176 : /* NB coding not supported in IVAS. Switching to WB. */
2177 21777 : if ( newBandwidth == NB && hEncoderConfig->ivas_format != UNDEFINED_FORMAT && hEncoderConfig->ivas_format != MONO_FORMAT )
2178 : {
2179 301 : newBandwidth = WB;
2180 : }
2181 :
2182 21777 : if ( hEncoderConfig->max_bwidth != newBandwidth )
2183 : {
2184 3669 : hEncoderConfig->max_bwidth = newBandwidth;
2185 3669 : hIvasEnc->switchingActive = true;
2186 : }
2187 :
2188 21777 : return IVAS_ERR_OK;
2189 : }
2190 :
2191 :
2192 : /*---------------------------------------------------------------------*
2193 : * resetIsmMetadataProvidedFlags()
2194 : *
2195 : *
2196 : *---------------------------------------------------------------------*/
2197 :
2198 104453 : static void resetIsmMetadataProvidedFlags(
2199 : IVAS_ENC_HANDLE hIvasEnc )
2200 : {
2201 : int16_t i;
2202 :
2203 522265 : for ( i = 0; i < MAX_NUM_OBJECTS; ++i )
2204 : {
2205 417812 : hIvasEnc->ismMetadataProvided[i] = false;
2206 : }
2207 :
2208 104453 : return;
2209 : }
2210 :
2211 :
2212 : /*---------------------------------------------------------------------*
2213 : * bandwidthApiToInternal()
2214 : *
2215 : *
2216 : *---------------------------------------------------------------------*/
2217 :
2218 22404 : static ivas_error bandwidthApiToInternal(
2219 : const IVAS_ENC_BANDWIDTH maxBandwidth,
2220 : int16_t *internalMaxBandwidth )
2221 : {
2222 22404 : switch ( maxBandwidth )
2223 : {
2224 302 : case IVAS_ENC_BANDWIDTH_NB:
2225 302 : *internalMaxBandwidth = NB;
2226 302 : break;
2227 7340 : case IVAS_ENC_BANDWIDTH_WB:
2228 7340 : *internalMaxBandwidth = WB;
2229 7340 : break;
2230 7306 : case IVAS_ENC_BANDWIDTH_SWB:
2231 7306 : *internalMaxBandwidth = SWB;
2232 7306 : break;
2233 7456 : case IVAS_ENC_BANDWIDTH_FB:
2234 7456 : *internalMaxBandwidth = FB;
2235 7456 : break;
2236 0 : case IVAS_ENC_BANDWIDTH_UNDEFINED:
2237 : default:
2238 0 : return IVAS_ERR_INVALID_BANDWIDTH;
2239 : break;
2240 : }
2241 :
2242 22404 : return IVAS_ERR_OK;
2243 : }
2244 :
2245 : #ifdef DEBUG_AGC_ENCODER_CMD_OPTION
2246 : /*---------------------------------------------------------------------*
2247 : * agcAPIToInternal()
2248 : *
2249 : *
2250 : *---------------------------------------------------------------------*/
2251 :
2252 : static ivas_error agcAPIToInternal(
2253 : const IVAS_ENC_AGC agcOption,
2254 : int16_t *internalAGCOption )
2255 : {
2256 : switch ( agcOption )
2257 : {
2258 : case IVAS_ENC_AGC_ENABLED:
2259 : *internalAGCOption = SBA_AGC_FORCE_ENABLE;
2260 : break;
2261 : case IVAS_ENC_AGC_DISABLED:
2262 : *internalAGCOption = SBA_AGC_FORCE_DISABLE;
2263 : break;
2264 : case IVAS_ENC_AGC_UNDEFINED:
2265 : *internalAGCOption = SBA_AGC_DEFAULT;
2266 : break;
2267 : default:
2268 : return IVAS_ERR_INVALID_AGC;
2269 : break;
2270 : }
2271 :
2272 : return IVAS_ERR_OK;
2273 : }
2274 : #endif
2275 :
2276 :
2277 : /*---------------------------------------------------------------------*
2278 : * fecIndicatorApiToInternal()
2279 : *
2280 : *
2281 : *---------------------------------------------------------------------*/
2282 :
2283 0 : static ivas_error fecIndicatorApiToInternal(
2284 : const IVAS_ENC_FEC_INDICATOR fecIndicator,
2285 : int16_t *fecIndicatorInternal )
2286 : {
2287 0 : switch ( fecIndicator )
2288 : {
2289 0 : case IVAS_ENC_FEC_LO:
2290 0 : *fecIndicatorInternal = 0;
2291 0 : break;
2292 0 : case IVAS_ENC_FEC_HI:
2293 0 : *fecIndicatorInternal = 1;
2294 0 : break;
2295 0 : default:
2296 0 : return IVAS_ERR_INTERNAL;
2297 : break;
2298 : }
2299 :
2300 0 : return IVAS_ERR_OK;
2301 : }
2302 :
2303 : #ifdef DEBUGGING
2304 : /*---------------------------------------------------------------------*
2305 : * forcedModeApiToInternal()
2306 : *
2307 : *
2308 : *---------------------------------------------------------------------*/
2309 :
2310 : static ivas_error forcedModeApiToInternal(
2311 : IVAS_ENC_FORCED_MODE forcedMode,
2312 : int16_t *forcedModeInternal )
2313 : {
2314 : switch ( forcedMode )
2315 : {
2316 : case IVAS_ENC_FORCE_SPEECH:
2317 : *forcedModeInternal = FORCE_SPEECH;
2318 : break;
2319 : case IVAS_ENC_FORCE_MUSIC:
2320 : *forcedModeInternal = FORCE_MUSIC;
2321 : break;
2322 : case IVAS_ENC_FORCE_ACELP:
2323 : *forcedModeInternal = FORCE_ACELP;
2324 : break;
2325 : case IVAS_ENC_FORCE_GSC:
2326 : *forcedModeInternal = FORCE_GSC;
2327 : break;
2328 : case IVAS_ENC_FORCE_TCX:
2329 : *forcedModeInternal = FORCE_TCX;
2330 : break;
2331 : case IVAS_ENC_FORCE_HQ:
2332 : *forcedModeInternal = FORCE_HQ;
2333 : break;
2334 : case IVAS_ENC_FORCE_UNFORCED:
2335 : *forcedModeInternal = -1;
2336 : break;
2337 : default:
2338 : return IVAS_ERR_INVALID_FORCE_MODE;
2339 : break;
2340 : }
2341 :
2342 : return IVAS_ERR_OK;
2343 : }
2344 : #endif
2345 :
2346 :
2347 : /*---------------------------------------------------------------------*
2348 : * IVAS_ENC_PrintConfig()
2349 : *
2350 : *
2351 : *---------------------------------------------------------------------*/
2352 :
2353 627 : ivas_error IVAS_ENC_PrintConfig(
2354 : const IVAS_ENC_HANDLE hIvasEnc, /* i : IVAS encoder handle */
2355 : const int16_t channelAwareModeEnabled /* i : channel-aware mode enabled flag */
2356 : )
2357 : {
2358 627 : return printConfigInfo_enc( hIvasEnc, channelAwareModeEnabled );
2359 : }
2360 :
2361 :
2362 : /*---------------------------------------------------------------------*
2363 : * IVAS_ENC_PrintDisclaimer()
2364 : *
2365 : * Print IVAS disclaimer to console
2366 : *---------------------------------------------------------------------*/
2367 :
2368 627 : void IVAS_ENC_PrintDisclaimer( void )
2369 : {
2370 627 : print_disclaimer( stderr );
2371 :
2372 627 : return;
2373 : }
2374 :
2375 :
2376 : /*---------------------------------------------------------------------*
2377 : * init_encoder_config()
2378 : *
2379 : * Initialize Encoder Config. handle
2380 : *---------------------------------------------------------------------*/
2381 :
2382 627 : static void init_encoder_config(
2383 : ENCODER_CONFIG_HANDLE hEncoderConfig /* o : configuration structure */
2384 : )
2385 : {
2386 627 : hEncoderConfig->ivas_total_brate = ACELP_12k65;
2387 627 : hEncoderConfig->max_bwidth = SWB;
2388 627 : hEncoderConfig->input_Fs = 16000;
2389 627 : hEncoderConfig->nchan_inp = 1;
2390 627 : hEncoderConfig->element_mode_init = EVS_MONO;
2391 627 : hEncoderConfig->ivas_format = UNDEFINED_FORMAT;
2392 627 : hEncoderConfig->is_binaural = 0;
2393 627 : hEncoderConfig->Opt_SC_VBR = 0;
2394 627 : hEncoderConfig->last_Opt_SC_VBR = 0;
2395 627 : hEncoderConfig->Opt_AMR_WB = 0;
2396 627 : hEncoderConfig->Opt_DTX_ON = 0;
2397 627 : hEncoderConfig->Opt_RF_ON = 0;
2398 627 : hEncoderConfig->rf_fec_offset = 0;
2399 627 : hEncoderConfig->rf_fec_indicator = 1;
2400 627 : hEncoderConfig->interval_SID = FIXED_SID_RATE;
2401 627 : hEncoderConfig->var_SID_rate_flag = 1;
2402 627 : hEncoderConfig->mc_input_setup = MC_LS_SETUP_INVALID;
2403 627 : hEncoderConfig->stereo_dmx_evs = 0;
2404 627 : hEncoderConfig->nchan_ism = 0;
2405 627 : hEncoderConfig->sba_order = 0;
2406 627 : hEncoderConfig->sba_planar = 0;
2407 627 : hEncoderConfig->ism_extended_metadata_flag = 0;
2408 : #ifdef DEBUGGING
2409 : hEncoderConfig->stereo_mode_cmdl = 0;
2410 : hEncoderConfig->force = -1;
2411 : hEncoderConfig->mdct_stereo_mode_cmdl = SMDCT_MS_DECISION;
2412 : #ifdef DEBUG_AGC_ENCODER_CMD_OPTION
2413 : hEncoderConfig->Opt_AGC_ON = SBA_AGC_DEFAULT;
2414 : #endif
2415 : #endif
2416 627 : hEncoderConfig->Opt_PCA_ON = 0;
2417 :
2418 627 : return;
2419 : }
2420 :
2421 : #ifdef DEBUGGING
2422 :
2423 : /*---------------------------------------------------------------------*
2424 : * IVAS_ENC_GetNoCLipping()
2425 : *
2426 : * return number of clipped samples
2427 : *---------------------------------------------------------------------*/
2428 :
2429 : int32_t IVAS_ENC_GetNoCLipping(
2430 : IVAS_ENC_HANDLE hIvasEnc, /* i : IVAS encoder handle */
2431 : float *maxOverload, /* o : Max overload value */
2432 : float *minOverload /* o : Min overload value */
2433 : )
2434 : {
2435 : *maxOverload = hIvasEnc->st_ivas->maxOverload;
2436 : *minOverload = hIvasEnc->st_ivas->minOverload;
2437 : return hIvasEnc->st_ivas->noClipping;
2438 : }
2439 : #endif
|