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 <math.h>
34 : #include <assert.h>
35 : #include <stdio.h>
36 : #include <string.h>
37 : #include "options.h"
38 : #ifdef DEBUGGING
39 : #include "debug.h"
40 : #endif
41 : #include "lib_enc.h"
42 : #include "ivas_prot_fx.h"
43 : #include "prot_fx.h"
44 : #include "prot_fx_enc.h"
45 : #include "wmc_auto.h"
46 : #include "ivas_rom_enc.h"
47 :
48 : /*---------------------------------------------------------------------*
49 : * Local struct
50 : *---------------------------------------------------------------------*/
51 :
52 : struct IVAS_ENC
53 : {
54 : Encoder_Struct *st_ivas;
55 : ENC_CORE_HANDLE hCoreCoder;
56 : bool isConfigured;
57 : #ifdef DEBUGGING
58 : bool cmd_stereo;
59 : #endif
60 : bool switchingActive; /* flag for configuration changes during encoding - currently only used with mono */
61 : Word16 Opt_RF_ON_loc;
62 : Word16 rf_fec_offset_loc;
63 : bool ismMetadataProvided[MAX_NUM_OBJECTS];
64 : bool maxBandwidthUser; /* Was a specific max bandwith selected by the user? */
65 : IVAS_ENC_BANDWIDTH newBandwidthApi; /* maximum encoded bandwidth, as set on API level */
66 : bool extMetadataApi; /* External metadata requested, to be checked against current bit rate */
67 : };
68 :
69 : /*---------------------------------------------------------------------*
70 : * Local functions
71 : *---------------------------------------------------------------------*/
72 :
73 : // static ivas_error configureEncoder_fx( IVAS_ENC_HANDLE hIvasEnc, const Word32 inputFs, const Word32 initBitrate, const IVAS_ENC_BANDWIDTH initBandwidth, const IVAS_ENC_DTX_CONFIG dtxConfig, const IVAS_ENC_CHANNEL_AWARE_CONFIG caConfig );
74 : static ivas_error setBandwidth_fx( IVAS_ENC_HANDLE hIvasEnc, const IVAS_ENC_BANDWIDTH maxBandwidth );
75 : static ivas_error setChannelAwareConfig_fx( IVAS_ENC_HANDLE hIvasEnc, const IVAS_ENC_CHANNEL_AWARE_CONFIG caConfig );
76 : static ivas_error sanitizeBandwidth_fx( const IVAS_ENC_HANDLE hIvasEnc );
77 : static ivas_error sanitizeBitrateISM_fx( const ENCODER_CONFIG_HANDLE hEncoderConfig, const bool extMetadataApi );
78 : static Word16 getInputBufferSize_fx( const Encoder_Struct *st_ivas );
79 : static ivas_error configureEncoder( IVAS_ENC_HANDLE hIvasEnc, const Word32 inputFs, const Word32 initBitrate, const IVAS_ENC_BANDWIDTH initBandwidth, const IVAS_ENC_DTX_CONFIG dtxConfig, const IVAS_ENC_CHANNEL_AWARE_CONFIG caConfig );
80 : static ivas_error setBitrate( IVAS_ENC_HANDLE hIvasEnc, const Word32 totalBitrate );
81 : static ivas_error doCommonConfigureChecks( IVAS_ENC_HANDLE hIvasEnc );
82 : static ivas_error doCommonSetterChecks( IVAS_ENC_HANDLE hIvasEnc );
83 : static void init_encoder_config( ENCODER_CONFIG_HANDLE hEncoderConfig );
84 : static void resetIsmMetadataProvidedFlags( IVAS_ENC_HANDLE hIvasEnc );
85 : static ivas_error bandwidthApiToInternal( const IVAS_ENC_BANDWIDTH maxBandwidth, Word16 *internalMaxBandwidth );
86 : static ivas_error fecIndicatorApiToInternal( const IVAS_ENC_FEC_INDICATOR fecIndicator, Word16 *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 0 : 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 0 : 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 0 : if ( ( *phIvasEnc = (IVAS_ENC_HANDLE) malloc( sizeof( struct IVAS_ENC ) ) ) == NULL )
114 : {
115 0 : return IVAS_ERR_FAILED_ALLOC;
116 : }
117 :
118 0 : ( *phIvasEnc )->hCoreCoder = NULL;
119 0 : ( *phIvasEnc )->isConfigured = false;
120 : #ifdef DEBUGGING
121 : ( *phIvasEnc )->cmd_stereo = false;
122 : #endif
123 0 : ( *phIvasEnc )->switchingActive = false;
124 0 : ( *phIvasEnc )->maxBandwidthUser = false;
125 0 : resetIsmMetadataProvidedFlags( *phIvasEnc );
126 :
127 : /*-----------------------------------------------------------------*
128 : * Allocate IVAS-codec encoder state
129 : *-----------------------------------------------------------------*/
130 :
131 0 : 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 0 : 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 0 : st_ivas = ( *phIvasEnc )->st_ivas;
146 :
147 : /* initialize encoder Config. handle */
148 0 : init_encoder_config( st_ivas->hEncoderConfig );
149 :
150 : /* initialize pointers to handles to NULL */
151 0 : ivas_initialize_handles_enc_fx( st_ivas );
152 :
153 0 : st_ivas->ind_list = NULL;
154 0 : st_ivas->ind_list_metadata = NULL;
155 :
156 : /* set high-level parameters */
157 0 : st_ivas->mc_mode = MC_MODE_NONE;
158 0 : st_ivas->ism_mode = ISM_MODE_NONE;
159 0 : st_ivas->sba_analysis_order = 0;
160 0 : move16();
161 :
162 0 : return IVAS_ERR_OK;
163 : }
164 :
165 624 : ivas_error IVAS_ENC_Open_fx(
166 : IVAS_ENC_HANDLE *phIvasEnc /* i/o: pointer to an encoder handle to be opened */
167 : )
168 : {
169 : Encoder_Struct *st_ivas;
170 :
171 624 : IF( phIvasEnc == NULL )
172 : {
173 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
174 : }
175 :
176 : /*-----------------------------------------------------------------*
177 : * Allocate and initialize IVAS application encoder handle
178 : *-----------------------------------------------------------------*/
179 :
180 624 : IF( ( *phIvasEnc = (IVAS_ENC_HANDLE) malloc( sizeof( struct IVAS_ENC ) ) ) == NULL )
181 : {
182 0 : return IVAS_ERR_FAILED_ALLOC;
183 : }
184 :
185 624 : ( *phIvasEnc )->hCoreCoder = NULL;
186 624 : ( *phIvasEnc )->isConfigured = false;
187 : #ifdef DEBUGGING
188 : ( *phIvasEnc )->cmd_stereo = false;
189 : #endif
190 624 : ( *phIvasEnc )->switchingActive = false;
191 624 : ( *phIvasEnc )->maxBandwidthUser = false;
192 624 : resetIsmMetadataProvidedFlags( *phIvasEnc );
193 :
194 : /*-----------------------------------------------------------------*
195 : * Allocate IVAS-codec encoder state
196 : *-----------------------------------------------------------------*/
197 :
198 624 : IF( ( ( *phIvasEnc )->st_ivas = (Encoder_Struct *) malloc( sizeof( Encoder_Struct ) ) ) == NULL )
199 : {
200 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for IVAS encoder structure" );
201 : }
202 :
203 624 : IF( ( ( *phIvasEnc )->st_ivas->hEncoderConfig = (ENCODER_CONFIG_HANDLE) malloc( sizeof( ENCODER_CONFIG ) ) ) == NULL )
204 : {
205 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for Encoder config structure" );
206 : }
207 :
208 : /*-----------------------------------------------------------------*
209 : * Initialize IVAS-codec encoder state
210 : *-----------------------------------------------------------------*/
211 :
212 624 : st_ivas = ( *phIvasEnc )->st_ivas;
213 :
214 : /* initialize encoder Config. handle */
215 624 : init_encoder_config( st_ivas->hEncoderConfig );
216 :
217 : /* initialize pointers to handles to NULL */
218 624 : ivas_initialize_handles_enc_fx( st_ivas );
219 :
220 624 : st_ivas->ind_list = NULL;
221 624 : st_ivas->ind_list_metadata = NULL;
222 :
223 : /* set high-level parameters */
224 624 : st_ivas->mc_mode = MC_MODE_NONE;
225 624 : st_ivas->ism_mode = ISM_MODE_NONE;
226 624 : st_ivas->sba_analysis_order = 0;
227 624 : move16();
228 :
229 624 : return IVAS_ERR_OK;
230 : }
231 : /*---------------------------------------------------------------------*
232 : * IVAS_ENC_Close()
233 : *
234 : *
235 : *---------------------------------------------------------------------*/
236 :
237 624 : void IVAS_ENC_Close(
238 : IVAS_ENC_HANDLE *phIvasEnc /* i/o: pointer to IVAS encoder handle */
239 : )
240 : {
241 : /* Free all memory */
242 624 : test();
243 624 : IF( phIvasEnc == NULL || *phIvasEnc == NULL )
244 : {
245 0 : return;
246 : }
247 :
248 624 : IF( ( *phIvasEnc )->isConfigured )
249 : {
250 624 : ivas_destroy_enc_fx( ( *phIvasEnc )->st_ivas );
251 : }
252 : ELSE
253 : {
254 0 : IF( ( *phIvasEnc )->st_ivas->hEncoderConfig )
255 : {
256 0 : free( ( *phIvasEnc )->st_ivas->hEncoderConfig );
257 0 : ( *phIvasEnc )->st_ivas->hEncoderConfig = NULL;
258 : }
259 0 : free( ( *phIvasEnc )->st_ivas );
260 : }
261 :
262 624 : ( *phIvasEnc )->st_ivas = NULL;
263 :
264 624 : free( *phIvasEnc );
265 :
266 624 : *phIvasEnc = NULL;
267 624 : phIvasEnc = NULL;
268 :
269 624 : return;
270 : }
271 :
272 :
273 : /*---------------------------------------------------------------------*
274 : * IVAS_ENC_ConfigureForMono()
275 : *
276 : * Configure and initialize the mono encoder.
277 : *---------------------------------------------------------------------*/
278 :
279 3 : ivas_error IVAS_ENC_ConfigureForMono(
280 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
281 : const Word32 inputFs, /* i : input sampling frequency */
282 : const Word32 bitrate, /* i : requested bitrate of the output bitstream */
283 : const bool max_bwidth_user, /* i : shows if bandwidth limitation was set by the user (true) or if default bandwidth was used (false) */
284 : const IVAS_ENC_BANDWIDTH maxBandwidth, /* i : bandwidth limitation */
285 : const IVAS_ENC_DTX_CONFIG dtxConfig, /* i : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig() */
286 : const IVAS_ENC_CHANNEL_AWARE_CONFIG caConfig, /* i : configuration of channel-aware mode, can by set to default by using IVAS_ENC_GetDefaultChannelAwareConfig() */
287 : const bool downmixFromStereo, /* i : if true, the encoder accepts a stereo input and internally downmixes it to mono before encoding */
288 : const bool is_binaural /* i : if true, the input is binaural audio */
289 : )
290 : {
291 : ivas_error error;
292 :
293 3 : error = IVAS_ERR_OK;
294 :
295 3 : IF( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
296 : {
297 0 : return error;
298 : }
299 :
300 3 : hIvasEnc->st_ivas->hEncoderConfig->ivas_format = MONO_FORMAT;
301 3 : hIvasEnc->st_ivas->hEncoderConfig->is_binaural = (Word16) is_binaural;
302 3 : move16();
303 3 : move16();
304 :
305 3 : if ( downmixFromStereo )
306 : {
307 2 : hIvasEnc->st_ivas->hEncoderConfig->stereo_dmx_evs = 1;
308 2 : move16();
309 : }
310 3 : hIvasEnc->maxBandwidthUser = max_bwidth_user;
311 :
312 3 : error = configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, caConfig );
313 :
314 3 : return error;
315 : }
316 :
317 :
318 : /*---------------------------------------------------------------------*
319 : * IVAS_ENC_ConfigureForStereo()
320 : *
321 : * Configure and initialize the stereo encoder.
322 : *---------------------------------------------------------------------*/
323 :
324 68 : ivas_error IVAS_ENC_ConfigureForStereo(
325 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
326 : const Word32 inputFs, /* i : input sampling frequency */
327 : const Word32 bitrate, /* i : requested bitrate of the output bitstream */
328 : const bool max_bwidth_user, /* i : shows if bandwidth limitation was set by the user (true) or if default bandwidth was used (false) */
329 : const IVAS_ENC_BANDWIDTH maxBandwidth, /* i : bandwidth limitation */
330 : const IVAS_ENC_DTX_CONFIG dtxConfig, /* i : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig() */
331 : const bool is_binaural /* i : flag indicating if input is binaural audio */
332 : #ifdef DEBUGGING
333 : ,
334 : const IVAS_ENC_STEREO_MODE stereoMode /* i : forces a specific stereo coding mode */
335 : #endif
336 : )
337 : {
338 : Encoder_Struct *st_ivas;
339 : ENCODER_CONFIG_HANDLE hEncoderConfig;
340 :
341 : ivas_error error;
342 :
343 68 : IF( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
344 : {
345 0 : return error;
346 : }
347 :
348 68 : st_ivas = hIvasEnc->st_ivas;
349 68 : hEncoderConfig = st_ivas->hEncoderConfig;
350 68 : hEncoderConfig->nchan_inp = 2;
351 68 : move16();
352 68 : hEncoderConfig->ivas_format = STEREO_FORMAT;
353 68 : move16();
354 68 : hEncoderConfig->is_binaural = (Word16) is_binaural;
355 68 : move16();
356 :
357 : #ifdef DEBUGGING
358 : switch ( stereoMode )
359 : {
360 : case IVAS_ENC_STEREO_MODE_UNIFIED:
361 : hEncoderConfig->stereo_mode_cmdl = 1; /* set unified stereo by default */
362 : hEncoderConfig->element_mode_init = IVAS_CPE_DFT;
363 : hIvasEnc->cmd_stereo = true;
364 : break;
365 : case IVAS_ENC_STEREO_MODE_DFT:
366 : hEncoderConfig->element_mode_init = IVAS_CPE_DFT;
367 : hEncoderConfig->stereo_mode_cmdl = IVAS_CPE_DFT;
368 : break;
369 : case IVAS_ENC_STEREO_MODE_TD:
370 : hEncoderConfig->stereo_mode_cmdl = IVAS_CPE_TD;
371 : hEncoderConfig->element_mode_init = IVAS_CPE_TD;
372 : break;
373 : case IVAS_ENC_STEREO_MODE_MDCT_DECISION:
374 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
375 : hEncoderConfig->mdct_stereo_mode_cmdl = SMDCT_MS_DECISION;
376 : break;
377 : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
378 : case IVAS_ENC_STEREO_MODE_MDCT_FORCE_LR:
379 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
380 : hEncoderConfig->mdct_stereo_mode_cmdl = SMDCT_FORCE_LR;
381 : break;
382 : case IVAS_ENC_STEREO_MODE_MDCT_FORCE_MS:
383 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
384 : hEncoderConfig->mdct_stereo_mode_cmdl = SMDCT_FORCE_MS;
385 : break;
386 : #endif
387 : default:
388 : return IVAS_ERR_INVALID_STEREO_MODE;
389 : break;
390 : }
391 : #endif /* DEBUGGING */
392 :
393 68 : hIvasEnc->maxBandwidthUser = max_bwidth_user;
394 :
395 68 : error = configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
396 :
397 68 : return error;
398 : }
399 :
400 :
401 : /*---------------------------------------------------------------------*
402 : * IVAS_ENC_ConfigureForMASAObjects()
403 : *
404 : * Configure and initialize the combined MASA and ISM encoder.
405 : *---------------------------------------------------------------------*/
406 44 : ivas_error IVAS_ENC_ConfigureForMASAObjects(
407 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
408 : const Word32 inputFs, /* i : input sampling frequency */
409 : const Word32 bitrate, /* i : requested bitrate of the ouput bitstream */
410 : const IVAS_ENC_BANDWIDTH maxBandwidth, /* i : bandwidth limitation */
411 : const IVAS_ENC_DTX_CONFIG dtxConfig, /* i : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig() */
412 : const UWord16 numObjects, /* i : number of objects to be encoded */
413 : const Word16 masaVariant /* i : index specifying the number of MASA transport channels */
414 : )
415 : {
416 : Encoder_Struct *st_ivas;
417 : ivas_error error;
418 :
419 44 : IF( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
420 : {
421 0 : return error;
422 : }
423 :
424 44 : IF( GT_32( numObjects, MAX_NUM_OBJECTS ) )
425 : {
426 0 : return IVAS_ERR_TOO_MANY_INPUTS;
427 : }
428 44 : st_ivas = hIvasEnc->st_ivas;
429 44 : SWITCH( masaVariant )
430 : {
431 32 : case IVAS_ENC_MASA_2CH:
432 32 : st_ivas->hEncoderConfig->nchan_inp = add( CPE_CHANNELS, numObjects );
433 32 : st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_DFT; /* initialization only, might be changed later based on element_brate */
434 32 : move16();
435 32 : move16();
436 32 : BREAK;
437 12 : case IVAS_ENC_MASA_1CH:
438 12 : st_ivas->hEncoderConfig->nchan_inp = add( 1, numObjects );
439 12 : st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_DFT; /* initialization only, might be changed later based on element_brate */
440 12 : move16();
441 12 : move16();
442 12 : BREAK;
443 0 : default:
444 0 : return IVAS_ERR_INVALID_MASA_CONFIG;
445 : }
446 :
447 44 : st_ivas = hIvasEnc->st_ivas;
448 :
449 : /* Currently this is true but it is already shown in descriptive metadata that there can be inequality for this. */
450 44 : st_ivas->nchan_transport = sub( st_ivas->hEncoderConfig->nchan_inp, numObjects );
451 44 : st_ivas->hEncoderConfig->ivas_format = MASA_ISM_FORMAT;
452 44 : st_ivas->hEncoderConfig->nchan_ism = numObjects;
453 44 : move16();
454 44 : move16();
455 44 : move16();
456 :
457 44 : return configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
458 : }
459 :
460 :
461 : /*---------------------------------------------------------------------*
462 : * IVAS_ENC_ConfigureForObjects()
463 : *
464 : * Configure and initialize the ISM encoder.
465 : *---------------------------------------------------------------------*/
466 :
467 74 : ivas_error IVAS_ENC_ConfigureForObjects(
468 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
469 : const Word32 inputFs, /* i : input sampling frequency */
470 : const Word32 bitrate, /* i : requested bitrate of the output bitstream */
471 : const bool max_bwidth_user, /* i : shows if bandwidth limitation was set by the user (true) or if default bandwidth was used (false) */
472 : const IVAS_ENC_BANDWIDTH maxBandwidth, /* i : bandwidth limitation */
473 : const IVAS_ENC_DTX_CONFIG dtxConfig, /* i : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig() */
474 : const UWord16 numObjects, /* i : number of objects to be encoded */
475 : const bool ism_extended_metadata /* i : Extended metadata used (true/false), where extended metadata includes radius and orientation */
476 : )
477 : {
478 : Encoder_Struct *st_ivas;
479 : ivas_error error;
480 :
481 74 : IF( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
482 : {
483 0 : return error;
484 : }
485 :
486 74 : IF( GT_32( numObjects, MAX_NUM_OBJECTS ) )
487 : {
488 0 : return IVAS_ERR_TOO_MANY_INPUTS;
489 : }
490 :
491 74 : st_ivas = hIvasEnc->st_ivas;
492 74 : st_ivas->hEncoderConfig->ivas_format = ISM_FORMAT;
493 74 : move16();
494 74 : st_ivas->hEncoderConfig->element_mode_init = IVAS_SCE;
495 74 : move16();
496 74 : st_ivas->hEncoderConfig->nchan_inp = numObjects;
497 74 : move16();
498 74 : st_ivas->hEncoderConfig->nchan_ism = numObjects;
499 74 : move16();
500 74 : st_ivas->hEncoderConfig->ism_extended_metadata_flag = ism_extended_metadata;
501 74 : move16();
502 74 : hIvasEnc->extMetadataApi = ( ism_extended_metadata == 1 );
503 74 : hIvasEnc->maxBandwidthUser = max_bwidth_user;
504 :
505 74 : error = configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
506 :
507 74 : return error;
508 : }
509 :
510 :
511 : /*---------------------------------------------------------------------*
512 : * IVAS_ENC_FeedObjectMetadata()
513 : *
514 : *
515 : *---------------------------------------------------------------------*/
516 :
517 427276 : ivas_error IVAS_ENC_FeedObjectMetadata(
518 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
519 : const UWord16 ismIndex, /* i : object index */
520 : const IVAS_ISM_METADATA metadata /* i : object metadata handle for current frame */
521 : )
522 : {
523 : ivas_error error;
524 :
525 427276 : error = IVAS_ERR_OK;
526 :
527 427276 : IF( !hIvasEnc->isConfigured )
528 : {
529 0 : return IVAS_ERR_NOT_CONFIGURED;
530 : }
531 :
532 427276 : test();
533 427276 : test();
534 427276 : IF( NE_16( hIvasEnc->st_ivas->hEncoderConfig->ivas_format, ISM_FORMAT ) && NE_16( hIvasEnc->st_ivas->hEncoderConfig->ivas_format, MASA_ISM_FORMAT ) && NE_16( hIvasEnc->st_ivas->hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) )
535 : {
536 0 : return IVAS_ERR_METADATA_NOT_EXPECTED;
537 : }
538 :
539 427276 : IF( GT_32( ismIndex, hIvasEnc->st_ivas->hEncoderConfig->nchan_inp ) )
540 : {
541 0 : return IVAS_ERR_INVALID_INDEX;
542 : }
543 427276 : Word32 azimuth_fx = float_to_fix( metadata.azimuth, Q22 ); /* Q22 */
544 427276 : Word32 elevation_fx = float_to_fix( metadata.elevation, Q22 ); /* Q22 */
545 427276 : Word16 radius_fx = float_to_fix16( metadata.radius, Q9 ); /* Q9 */
546 427276 : Word32 yaw_fx = float_to_fix( metadata.yaw, Q22 ); /* Q22 */
547 427276 : Word32 pitch_fx = float_to_fix( metadata.pitch, Q22 ); /* Q22 */
548 427276 : error = ivas_set_ism_metadata_fx( hIvasEnc->st_ivas->hIsmMetaData[ismIndex], azimuth_fx, elevation_fx, radius_fx, yaw_fx, pitch_fx, metadata.non_diegetic_flag );
549 :
550 427276 : IF( error != IVAS_ERR_OK )
551 : {
552 0 : return error;
553 : }
554 :
555 427276 : hIvasEnc->ismMetadataProvided[ismIndex] = true;
556 :
557 427276 : return error;
558 : }
559 :
560 :
561 : /*---------------------------------------------------------------------*
562 : * IVAS_ENC_ConfigureForAmbisonics()
563 : *
564 : * Configure and initialize the SBA encoder.
565 : *---------------------------------------------------------------------*/
566 :
567 244 : ivas_error IVAS_ENC_ConfigureForAmbisonics(
568 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
569 : const Word32 inputFs, /* i : input sampling frequency */
570 : const Word32 bitrate, /* i : requested bitrate of the output bitstream */
571 : const bool max_bwidth_user, /* i : shows if bandwidth limitation was set by the user (true) or if default bandwidth was used (false) */
572 : const IVAS_ENC_BANDWIDTH maxBandwidth, /* i : bandwidth limitation */
573 : const IVAS_ENC_DTX_CONFIG dtxConfig, /* i : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig() */
574 : const IVAS_ENC_SBA_ORDER order, /* i : order of the Ambisonics input */
575 : const bool isPlanar, /* i : if true, input is treated as planar Ambisonics */
576 : const bool Opt_PCA_ON /* i : PCA option flag */
577 : )
578 : {
579 : ENCODER_CONFIG_HANDLE hEncoderConfig;
580 : ivas_error error;
581 :
582 244 : IF( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
583 : {
584 0 : return error;
585 : }
586 :
587 244 : hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
588 :
589 244 : hEncoderConfig->ivas_format = SBA_FORMAT;
590 244 : hEncoderConfig->element_mode_init = IVAS_SCE; /* Just needs to be something not mono, will be set later */
591 244 : hEncoderConfig->sba_planar = isPlanar;
592 244 : hEncoderConfig->sba_order = order;
593 244 : move16();
594 244 : move16();
595 244 : move16();
596 :
597 : /* Input in ACN/SN3D in all cases (3D and planar): get number of channels */
598 244 : hEncoderConfig->nchan_inp = ivas_sba_get_nchan_fx( hEncoderConfig->sba_order, 0 ); /*planar input arg. deliberately set to zero since input always in ACN/SN3D*/
599 244 : move16();
600 :
601 :
602 244 : hEncoderConfig->Opt_PCA_ON = (Word16) Opt_PCA_ON;
603 244 : move16();
604 :
605 244 : hIvasEnc->maxBandwidthUser = max_bwidth_user;
606 :
607 244 : error = configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
608 :
609 :
610 244 : return error;
611 : }
612 : #ifndef HARM_ENC_INIT
613 : ivas_error IVAS_ENC_ConfigureForAmbisonics_fx(
614 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
615 : const Word32 inputFs, /* i : input sampling frequency */
616 : const Word32 bitrate, /* i : requested bitrate of the output bitstream */
617 : const bool max_bwidth_user, /* i : shows if bandwidth limitation was set by the user (true) or if default bandwidth was used (false) */
618 : const IVAS_ENC_BANDWIDTH maxBandwidth, /* i : bandwidth limitation */
619 : const IVAS_ENC_DTX_CONFIG dtxConfig, /* i : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig() */
620 : const IVAS_ENC_SBA_ORDER order, /* i : order of the Ambisonics input */
621 : const bool isPlanar, /* i : if true, input is treated as planar Ambisonics */
622 : const bool Opt_PCA_ON /* i : PCA option flag */
623 : )
624 : {
625 : ENCODER_CONFIG_HANDLE hEncoderConfig;
626 : ivas_error error;
627 :
628 : IF( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
629 : {
630 : return error;
631 : }
632 :
633 : hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
634 :
635 : hEncoderConfig->ivas_format = SBA_FORMAT;
636 : hEncoderConfig->element_mode_init = IVAS_SCE; /* Just needs to be something not mono, will be set later */
637 : hEncoderConfig->sba_planar = isPlanar;
638 : hEncoderConfig->sba_order = order;
639 : move16();
640 : move16();
641 : move16();
642 :
643 : /* Input in ACN/SN3D in all cases (3D and planar): get number of channels */
644 : hEncoderConfig->nchan_inp = ivas_sba_get_nchan_fx( hEncoderConfig->sba_order, 0 ); /*planar input arg. deliberately set to zero since input always in ACN/SN3D*/
645 : move16();
646 :
647 :
648 : hEncoderConfig->Opt_PCA_ON = (Word16) Opt_PCA_ON;
649 : move16();
650 :
651 : hIvasEnc->maxBandwidthUser = max_bwidth_user;
652 :
653 : error = configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
654 :
655 :
656 : return error;
657 : }
658 : #endif
659 : /*---------------------------------------------------------------------*
660 : * IVAS_ENC_ConfigureForSBAObjects()
661 : *
662 : * Configure and initialize the combined SBA and ISM encoder.
663 : *---------------------------------------------------------------------*/
664 :
665 35 : ivas_error IVAS_ENC_ConfigureForSBAObjects(
666 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
667 : const Word32 inputFs, /* i : input sampling frequency */
668 : const Word32 bitrate, /* i : requested bitrate of the ouput bitstream */
669 : const IVAS_ENC_BANDWIDTH maxBandwidth, /* i : bandwidth limitation */
670 : const IVAS_ENC_DTX_CONFIG dtxConfig, /* i : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig() */
671 : const UWord16 numObjects, /* i : number of objects to be encoded */
672 : const IVAS_ENC_SBA_ORDER order, /* i : order of the Ambisonics input */
673 : const bool isPlanar, /* i : if true, input is treated as planar Ambisonics */
674 : const bool Opt_PCA_ON /* i : PCA option flag */
675 : )
676 : {
677 : Encoder_Struct *st_ivas;
678 : ivas_error error;
679 :
680 35 : IF( NE_16( ( error = doCommonConfigureChecks( hIvasEnc ) ), IVAS_ERR_OK ) )
681 : {
682 0 : return error;
683 : }
684 :
685 35 : IF( GT_32( numObjects, MAX_NUM_OBJECTS ) )
686 : {
687 0 : return IVAS_ERR_TOO_MANY_INPUTS;
688 : }
689 35 : st_ivas = hIvasEnc->st_ivas;
690 :
691 35 : st_ivas->hEncoderConfig->element_mode_init = IVAS_SCE; /* Just needs to be something not mono, will be set later */
692 35 : move16();
693 35 : st_ivas->hEncoderConfig->sba_planar = isPlanar;
694 35 : move16();
695 35 : st_ivas->hEncoderConfig->sba_order = order;
696 35 : move16();
697 :
698 : /* Input in ACN/SN3D in all cases (3D and planar): get number of channels */
699 : /*Input file will always contain all channels for a given order irrespective of planar flag*/
700 35 : st_ivas->hEncoderConfig->nchan_inp = add( ivas_sba_get_nchan_fx( st_ivas->hEncoderConfig->sba_order, 0 ), numObjects );
701 35 : move16();
702 35 : st_ivas->hEncoderConfig->Opt_PCA_ON = extract_l( Opt_PCA_ON );
703 35 : move16();
704 :
705 : /* Currently this is true but it is already shown in descriptive metadata that there can be inequality for this. */
706 35 : st_ivas->nchan_transport = sub( st_ivas->hEncoderConfig->nchan_inp, numObjects );
707 35 : move16();
708 35 : st_ivas->hEncoderConfig->ivas_format = SBA_ISM_FORMAT;
709 35 : move16();
710 35 : st_ivas->hEncoderConfig->nchan_ism = numObjects;
711 35 : move16();
712 :
713 35 : return configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
714 : }
715 :
716 :
717 : /*---------------------------------------------------------------------*
718 : * IVAS_ENC_ConfigureForMasa()
719 : *
720 : * Configure and initialize the MASA encoder.
721 : *---------------------------------------------------------------------*/
722 75 : ivas_error IVAS_ENC_ConfigureForMasa(
723 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
724 : const Word32 inputFs, /* i : input sampling frequency */
725 : const Word32 bitrate, /* i : requested bitrate of the output bitstream */
726 : const bool max_bwidth_user, /* i : shows if bandwidth limitation was set by the user (true) or if default bandwidth was used (false) */
727 : const IVAS_ENC_BANDWIDTH maxBandwidth, /* i : bandwidth limitation */
728 : const IVAS_ENC_DTX_CONFIG dtxConfig, /* i : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig() */
729 : const IVAS_ENC_MASA_VARIANT masaVariant /* i : type of MASA input (either 1 or 2 channels) */
730 : )
731 : {
732 : ENCODER_CONFIG_HANDLE hEncoderConfig;
733 : ivas_error error;
734 :
735 75 : IF( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
736 : {
737 0 : return error;
738 : }
739 :
740 75 : hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
741 :
742 75 : hEncoderConfig->ivas_format = MASA_FORMAT;
743 75 : move32();
744 :
745 75 : SWITCH( masaVariant )
746 : {
747 31 : case IVAS_ENC_MASA_1CH:
748 31 : hEncoderConfig->nchan_inp = 1;
749 31 : hEncoderConfig->element_mode_init = IVAS_SCE;
750 31 : move16();
751 31 : move16();
752 31 : BREAK;
753 44 : case IVAS_ENC_MASA_2CH:
754 44 : hEncoderConfig->nchan_inp = 2;
755 : #ifdef DEBUGGING
756 : hEncoderConfig->stereo_mode_cmdl = 1; /* set unified stereo by default */
757 : #endif
758 44 : hEncoderConfig->element_mode_init = IVAS_CPE_DFT; /* initialization only, might be changed later based on element_brate */
759 44 : move16();
760 44 : move16();
761 44 : BREAK;
762 0 : default:
763 0 : return IVAS_ERR_INVALID_MASA_CONFIG;
764 : }
765 :
766 75 : hIvasEnc->maxBandwidthUser = max_bwidth_user;
767 :
768 75 : error = configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
769 :
770 75 : return error;
771 : }
772 :
773 :
774 : /*---------------------------------------------------------------------*
775 : * IVAS_ENC_FeedMasaMetadata()
776 : *
777 : *
778 : *---------------------------------------------------------------------*/
779 :
780 39626 : ivas_error IVAS_ENC_FeedMasaMetadata(
781 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
782 : IVAS_MASA_METADATA_HANDLE hMasaMetadata /* i : MASA metadata for current frame */
783 : )
784 : {
785 39626 : IF( !hIvasEnc->isConfigured )
786 : {
787 0 : return IVAS_ERR_NOT_CONFIGURED;
788 : }
789 :
790 39626 : test();
791 39626 : IF( NE_32( hIvasEnc->st_ivas->hEncoderConfig->ivas_format, MASA_FORMAT ) && NE_32( hIvasEnc->st_ivas->hEncoderConfig->ivas_format, MASA_ISM_FORMAT ) )
792 : {
793 0 : return IVAS_ERR_METADATA_NOT_EXPECTED;
794 : }
795 :
796 39626 : hIvasEnc->st_ivas->hMasa->masaMetadata = *hMasaMetadata;
797 :
798 39626 : return IVAS_ERR_OK;
799 : }
800 :
801 :
802 : /*---------------------------------------------------------------------*
803 : * IVAS_ENC_ConfigureForMultichannel()
804 : *
805 : * Configure and initialize the MC encoder.
806 : *---------------------------------------------------------------------*/
807 :
808 81 : ivas_error IVAS_ENC_ConfigureForMultichannel(
809 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
810 : const Word32 inputFs, /* i : input sampling frequency */
811 : const Word32 bitrate, /* i : requested bitrate of the output bitstream */
812 : const bool max_bwidth_user, /* i : shows if bandwidth limitation was set by the user (true) or if default bandwidth was used (false) */
813 : const IVAS_ENC_BANDWIDTH maxBandwidth, /* i : bandwidth limitation */
814 : const IVAS_ENC_DTX_CONFIG dtxConfig, /* i : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig() */
815 : const IVAS_ENC_MC_LAYOUT mcLayout /* i : inpu MC layput */
816 : )
817 : {
818 : ENCODER_CONFIG_HANDLE hEncoderConfig;
819 : ivas_error error;
820 :
821 81 : IF( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
822 : {
823 0 : return error;
824 : }
825 :
826 81 : hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
827 :
828 81 : hEncoderConfig->ivas_format = MC_FORMAT;
829 81 : move16();
830 81 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT; /*just for initialization*/
831 81 : move16();
832 :
833 81 : SWITCH( mcLayout )
834 : {
835 42 : case IVAS_ENC_MC_5_1:
836 42 : hEncoderConfig->mc_input_setup = MC_LS_SETUP_5_1;
837 42 : BREAK;
838 4 : case IVAS_ENC_MC_7_1:
839 4 : hEncoderConfig->mc_input_setup = MC_LS_SETUP_7_1;
840 4 : BREAK;
841 6 : case IVAS_ENC_MC_5_1_2:
842 6 : hEncoderConfig->mc_input_setup = MC_LS_SETUP_5_1_2;
843 6 : BREAK;
844 6 : case IVAS_ENC_MC_5_1_4:
845 6 : hEncoderConfig->mc_input_setup = MC_LS_SETUP_5_1_4;
846 6 : BREAK;
847 23 : case IVAS_ENC_MC_7_1_4:
848 23 : hEncoderConfig->mc_input_setup = MC_LS_SETUP_7_1_4;
849 23 : BREAK;
850 0 : default:
851 0 : return IVAS_ERR_INVALID_MC_LAYOUT;
852 : BREAK;
853 : }
854 :
855 81 : hEncoderConfig->nchan_inp = ivas_mc_ls_setup_get_num_channels_fx( hEncoderConfig->mc_input_setup );
856 81 : move16();
857 :
858 81 : hIvasEnc->maxBandwidthUser = max_bwidth_user;
859 :
860 81 : error = configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
861 :
862 81 : return error;
863 : }
864 :
865 :
866 : /*---------------------------------------------------------------------*
867 : * configureEncoder()
868 : *
869 : * Configure the IVAS encoder.
870 : *---------------------------------------------------------------------*/
871 :
872 624 : static ivas_error configureEncoder(
873 : IVAS_ENC_HANDLE hIvasEnc,
874 : const Word32 inputFs,
875 : const Word32 initBitrate,
876 : const IVAS_ENC_BANDWIDTH initBandwidth,
877 : const IVAS_ENC_DTX_CONFIG dtxConfig,
878 : const IVAS_ENC_CHANNEL_AWARE_CONFIG caConfig )
879 : {
880 : Encoder_Struct *st_ivas;
881 : ENCODER_CONFIG_HANDLE hEncoderConfig;
882 : ivas_error error;
883 : Word32 cpe_brate;
884 :
885 624 : error = IVAS_ERR_OK;
886 :
887 624 : st_ivas = hIvasEnc->st_ivas;
888 624 : hEncoderConfig = st_ivas->hEncoderConfig;
889 :
890 : /*-----------------------------------------------------------------*
891 : * Bandwidth limitation
892 : *-----------------------------------------------------------------*/
893 :
894 624 : IF( ( error = setBandwidth_fx( hIvasEnc, initBandwidth ) ) != IVAS_ERR_OK )
895 : {
896 0 : return error;
897 : }
898 :
899 : /*-----------------------------------------------------------------*
900 : * DTX/CNG
901 : *-----------------------------------------------------------------*/
902 :
903 624 : IF( dtxConfig.enabled )
904 : {
905 126 : hEncoderConfig->Opt_DTX_ON = 1;
906 126 : move16();
907 :
908 126 : IF( dtxConfig.variable_SID_rate )
909 : {
910 0 : hEncoderConfig->var_SID_rate_flag = 1;
911 0 : hEncoderConfig->interval_SID = 0;
912 0 : move16();
913 0 : move16();
914 : }
915 : ELSE
916 : {
917 126 : hEncoderConfig->var_SID_rate_flag = 0;
918 126 : move16();
919 :
920 126 : test();
921 126 : IF( GE_16( dtxConfig.SID_interval, 3 ) && LE_16( dtxConfig.SID_interval, 100 ) )
922 : {
923 126 : hEncoderConfig->interval_SID = dtxConfig.SID_interval;
924 126 : move16();
925 : }
926 : ELSE
927 : {
928 0 : return IVAS_ERR_INVALID_DTX_UPDATE_RATE;
929 : }
930 : }
931 : }
932 : ELSE
933 : {
934 498 : hEncoderConfig->Opt_DTX_ON = 0;
935 498 : move16();
936 : }
937 :
938 : /*-----------------------------------------------------------------*
939 : * Bitrate
940 : *-----------------------------------------------------------------*/
941 :
942 624 : hEncoderConfig->ivas_total_brate = initBitrate;
943 624 : move32();
944 :
945 : /* SC-VBR at 5.90 kbps */
946 624 : IF( EQ_32( hEncoderConfig->ivas_total_brate, ACELP_5k90 ) )
947 : {
948 0 : hEncoderConfig->ivas_total_brate = ACELP_7k20;
949 0 : hEncoderConfig->Opt_SC_VBR = 1;
950 0 : hEncoderConfig->last_Opt_SC_VBR = hEncoderConfig->Opt_SC_VBR;
951 0 : move32();
952 0 : move16();
953 0 : move16();
954 :
955 0 : if ( ( hEncoderConfig->max_bwidth != NB ) )
956 : {
957 0 : hEncoderConfig->max_bwidth = WB;
958 0 : move16();
959 : }
960 : }
961 :
962 : /* check if the entered bitrate is supported */
963 624 : test();
964 624 : IF( NE_16( hEncoderConfig->ivas_format, UNDEFINED_FORMAT ) && NE_16( hEncoderConfig->ivas_format, MONO_FORMAT ) ) /* IVAS */
965 : {
966 621 : IF( !is_IVAS_bitrate_fx( hEncoderConfig->ivas_total_brate ) )
967 : {
968 0 : IF( hEncoderConfig->Opt_SC_VBR )
969 : {
970 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Incorrect bitrate specification in IVAS [bps]: %d", ACELP_5k90 );
971 : }
972 : ELSE
973 : {
974 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Incorrect bitrate specification in IVAS [bps]: %d", hEncoderConfig->ivas_total_brate );
975 : }
976 : }
977 :
978 621 : IF( EQ_16( hEncoderConfig->ivas_format, STEREO_FORMAT ) )
979 : {
980 : #ifdef DEBUGGING
981 : if ( hIvasEnc->cmd_stereo )
982 : #endif
983 : {
984 68 : hEncoderConfig->element_mode_init = IVAS_CPE_DFT;
985 : #ifdef DEBUGGING
986 : hEncoderConfig->stereo_mode_cmdl = 1;
987 : #endif
988 :
989 68 : if ( GE_32( hEncoderConfig->ivas_total_brate, MIN_BRATE_MDCT_STEREO ) )
990 : {
991 18 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
992 18 : move16();
993 : #ifdef DEBUGGING
994 : hEncoderConfig->stereo_mode_cmdl = 0;
995 : #endif
996 : }
997 : }
998 :
999 68 : test();
1000 68 : test();
1001 68 : IF( ( EQ_16( hEncoderConfig->element_mode_init, IVAS_CPE_TD ) || EQ_16( hEncoderConfig->element_mode_init, IVAS_CPE_DFT ) ) && GT_32( hEncoderConfig->ivas_total_brate, IVAS_48k ) )
1002 : {
1003 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for TD/DFT Stereo specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
1004 : }
1005 :
1006 68 : test();
1007 68 : IF( EQ_16( hEncoderConfig->element_mode_init, IVAS_CPE_MDCT ) && LT_32( hEncoderConfig->ivas_total_brate, IVAS_48k ) )
1008 : {
1009 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too low bitrate for MDCT Stereo specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
1010 : }
1011 :
1012 68 : IF( GT_32( hEncoderConfig->ivas_total_brate, IVAS_256k ) )
1013 : {
1014 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for Stereo specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
1015 : }
1016 : }
1017 553 : ELSE IF( EQ_16( hEncoderConfig->ivas_format, ISM_FORMAT ) )
1018 : {
1019 74 : IF( ( error = sanitizeBitrateISM_fx( hEncoderConfig, hIvasEnc->extMetadataApi ) ) != IVAS_ERR_OK )
1020 : {
1021 0 : return error;
1022 : }
1023 : }
1024 479 : ELSE IF( EQ_16( hEncoderConfig->ivas_format, SBA_FORMAT ) )
1025 : {
1026 : /* nothing */
1027 : }
1028 235 : ELSE IF( EQ_16( hEncoderConfig->ivas_format, MASA_FORMAT ) )
1029 : {
1030 : /* adapt element_mode according to the bitrate */
1031 75 : test();
1032 75 : IF( EQ_16( hEncoderConfig->nchan_inp, 2 ) && NE_16( hEncoderConfig->element_mode_init, IVAS_SCE ) )
1033 : {
1034 44 : IF( GE_32( hEncoderConfig->ivas_total_brate, IVAS_48k ) )
1035 : {
1036 22 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
1037 22 : move16();
1038 : }
1039 22 : ELSE IF( LT_32( hEncoderConfig->ivas_total_brate, MASA_STEREO_MIN_BITRATE ) )
1040 : {
1041 14 : hEncoderConfig->element_mode_init = IVAS_CPE_DFT;
1042 14 : move16();
1043 : }
1044 : }
1045 : }
1046 160 : ELSE IF( EQ_16( hEncoderConfig->ivas_format, MASA_ISM_FORMAT ) )
1047 : {
1048 44 : st_ivas->ism_mode = ivas_omasa_ism_mode_select_fx( st_ivas->hEncoderConfig->ivas_total_brate, hEncoderConfig->nchan_ism );
1049 44 : move32();
1050 :
1051 44 : cpe_brate = calculate_cpe_brate_MASA_ISM_fx( st_ivas->ism_mode, st_ivas->hEncoderConfig->ivas_total_brate, hEncoderConfig->nchan_ism );
1052 :
1053 : /*adapt element_mode according to the bit-rate*/
1054 44 : IF( NE_16( hEncoderConfig->element_mode_init, IVAS_SCE ) )
1055 : {
1056 44 : if ( GE_32( cpe_brate, IVAS_48k ) )
1057 : {
1058 17 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
1059 17 : move16();
1060 : }
1061 : }
1062 : }
1063 116 : ELSE IF( EQ_16( hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) )
1064 : {
1065 35 : st_ivas->ism_mode = ISM_MODE_NONE;
1066 35 : move16();
1067 : }
1068 : }
1069 : ELSE /* EVS mono */
1070 : {
1071 3 : hEncoderConfig->ivas_format = MONO_FORMAT;
1072 3 : move16();
1073 3 : hEncoderConfig->element_mode_init = EVS_MONO;
1074 3 : move16();
1075 :
1076 3 : IF( !is_EVS_bitrate( hEncoderConfig->ivas_total_brate, &hEncoderConfig->Opt_AMR_WB ) )
1077 : {
1078 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Incorrect bitrate specification in EVS mono: %d", hEncoderConfig->ivas_total_brate );
1079 : }
1080 :
1081 3 : IF( EQ_16( hEncoderConfig->stereo_dmx_evs, 1 ) )
1082 : {
1083 2 : hEncoderConfig->nchan_inp = 2;
1084 2 : move16();
1085 : }
1086 : }
1087 :
1088 : /*-----------------------------------------------------------------*
1089 : * Input sampling frequency
1090 : *-----------------------------------------------------------------*/
1091 624 : test();
1092 624 : test();
1093 624 : test();
1094 624 : IF( NE_32( inputFs, 8000 ) && NE_32( inputFs, 16000 ) && NE_32( inputFs, 32000 ) && NE_32( inputFs, 48000 ) )
1095 : {
1096 0 : return IVAS_ERR_INVALID_SAMPLING_RATE;
1097 : }
1098 :
1099 624 : hEncoderConfig->input_Fs = inputFs;
1100 624 : move32();
1101 :
1102 : /*-----------------------------------------------------------------*
1103 : * Channel-aware mode
1104 : *-----------------------------------------------------------------*/
1105 :
1106 624 : IF( ( error = setChannelAwareConfig_fx( hIvasEnc, caConfig ) ) != IVAS_ERR_OK )
1107 : {
1108 0 : return error;
1109 : }
1110 :
1111 : /*-----------------------------------------------------------------*
1112 : * Set codec mode
1113 : *-----------------------------------------------------------------*/
1114 :
1115 624 : st_ivas->codec_mode = MODE1; /* Note: in IVAS, set MODE1 */
1116 624 : move16();
1117 :
1118 624 : IF( EQ_16( hEncoderConfig->ivas_format, MONO_FORMAT ) )
1119 : {
1120 3 : IF( hEncoderConfig->Opt_AMR_WB )
1121 : {
1122 0 : st_ivas->codec_mode = MODE1;
1123 0 : move16();
1124 : }
1125 : ELSE
1126 : {
1127 3 : st_ivas->codec_mode = get_codec_mode( hEncoderConfig->ivas_total_brate );
1128 3 : move16();
1129 : }
1130 : }
1131 :
1132 624 : test();
1133 624 : IF( EQ_32( hEncoderConfig->ivas_total_brate, IVAS_13k2 ) && EQ_16( hEncoderConfig->Opt_RF_ON, 1 ) )
1134 : {
1135 0 : st_ivas->codec_mode = MODE2;
1136 0 : move16();
1137 : }
1138 :
1139 624 : st_ivas->last_codec_mode = st_ivas->codec_mode;
1140 624 : move16();
1141 :
1142 : /*-----------------------------------------------------------------*
1143 : * Sanity checks
1144 : *-----------------------------------------------------------------*/
1145 :
1146 624 : assert( hEncoderConfig->ivas_format != UNDEFINED_FORMAT && "\n IVAS format undefined" );
1147 :
1148 624 : test();
1149 624 : test();
1150 624 : IF( ( NE_16( hEncoderConfig->ivas_format, MONO_FORMAT ) || hEncoderConfig->stereo_dmx_evs ) && EQ_32( hEncoderConfig->input_Fs, 8000 ) )
1151 : {
1152 0 : return IVAS_ERROR( IVAS_ERR_INVALID_SAMPLING_RATE, "8kHz input sampling rate is not supported in IVAS." );
1153 : }
1154 :
1155 624 : test();
1156 624 : test();
1157 624 : test();
1158 624 : test();
1159 624 : test();
1160 624 : test();
1161 624 : IF( hEncoderConfig->Opt_DTX_ON && NE_16( hEncoderConfig->ivas_format, MONO_FORMAT ) &&
1162 : ( ( EQ_16( hEncoderConfig->ivas_format, SBA_FORMAT ) && GT_16( ivas_get_sba_num_TCs_fx( hEncoderConfig->ivas_total_brate, 1 ), 2 ) ) ||
1163 : EQ_16( hEncoderConfig->ivas_format, MC_FORMAT ) || EQ_16( hEncoderConfig->ivas_format, MASA_ISM_FORMAT ) || EQ_16( hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) ) )
1164 : {
1165 0 : return IVAS_ERROR( IVAS_ERR_DTX_NOT_SUPPORTED, "DTX is not supported in this IVAS format and element mode." );
1166 : }
1167 :
1168 624 : test();
1169 624 : test();
1170 624 : test();
1171 624 : IF( hEncoderConfig->Opt_PCA_ON && !( ( EQ_16( hEncoderConfig->ivas_format, SBA_FORMAT ) || EQ_16( hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) ) && EQ_32( hEncoderConfig->ivas_total_brate, PCA_BRATE ) && EQ_16( hEncoderConfig->sba_order, SBA_FOA_ORDER ) ) )
1172 : {
1173 0 : return IVAS_ERROR( IVAS_ERR_NOT_SUPPORTED_OPTION, "PCA supported at SBA FOA 256 kbps only." );
1174 : }
1175 :
1176 624 : IF( ( error = sanitizeBandwidth_fx( hIvasEnc ) ) != IVAS_ERR_OK )
1177 : {
1178 0 : return error;
1179 : }
1180 :
1181 624 : test();
1182 624 : test();
1183 624 : test();
1184 624 : IF( hEncoderConfig->is_binaural && !( ( EQ_16( hEncoderConfig->ivas_format, MONO_FORMAT ) && hEncoderConfig->stereo_dmx_evs ) || EQ_16( hEncoderConfig->ivas_format, STEREO_FORMAT ) ) )
1185 : {
1186 0 : return IVAS_ERROR( IVAS_ERR_NOT_SUPPORTED_OPTION, "'-binaural' option is supported only with '-stereo' or '-stereo_dmx_evs'" );
1187 : }
1188 :
1189 : /*-----------------------------------------------------------------*
1190 : * Finalize initialization
1191 : *-----------------------------------------------------------------*/
1192 :
1193 624 : IF( ( error = ivas_init_encoder_fx( st_ivas ) ) != IVAS_ERR_OK )
1194 : {
1195 0 : return error;
1196 : }
1197 :
1198 624 : IF( EQ_16( hEncoderConfig->ivas_format, MONO_FORMAT ) )
1199 : {
1200 3 : hIvasEnc->hCoreCoder = st_ivas->hSCE[0]->hCoreCoder[0]; /* Note: this is needed for switching in EVS mono */
1201 : }
1202 : ELSE
1203 : {
1204 621 : hIvasEnc->hCoreCoder = NULL;
1205 : }
1206 :
1207 624 : hIvasEnc->Opt_RF_ON_loc = hEncoderConfig->Opt_RF_ON;
1208 624 : hIvasEnc->rf_fec_offset_loc = hEncoderConfig->rf_fec_offset;
1209 624 : move16();
1210 624 : move16();
1211 :
1212 624 : hIvasEnc->isConfigured = true;
1213 :
1214 624 : return error;
1215 : }
1216 : #ifndef HARM_ENC_INIT
1217 : static ivas_error configureEncoder_fx(
1218 : IVAS_ENC_HANDLE hIvasEnc,
1219 : const Word32 inputFs,
1220 : const Word32 initBitrate,
1221 : const IVAS_ENC_BANDWIDTH initBandwidth,
1222 : const IVAS_ENC_DTX_CONFIG dtxConfig,
1223 : const IVAS_ENC_CHANNEL_AWARE_CONFIG caConfig )
1224 : {
1225 : Encoder_Struct *st_ivas;
1226 : ENCODER_CONFIG_HANDLE hEncoderConfig;
1227 : ivas_error error;
1228 : Word32 cpe_brate;
1229 :
1230 : error = IVAS_ERR_OK;
1231 :
1232 : st_ivas = hIvasEnc->st_ivas;
1233 : hEncoderConfig = st_ivas->hEncoderConfig;
1234 :
1235 : /*-----------------------------------------------------------------*
1236 : * Bandwidth limitation
1237 : *-----------------------------------------------------------------*/
1238 :
1239 : IF( ( error = setBandwidth_fx( hIvasEnc, initBandwidth ) ) != IVAS_ERR_OK )
1240 : {
1241 : return error;
1242 : }
1243 :
1244 : /*-----------------------------------------------------------------*
1245 : * DTX/CNG
1246 : *-----------------------------------------------------------------*/
1247 :
1248 : IF( dtxConfig.enabled )
1249 : {
1250 : hEncoderConfig->Opt_DTX_ON = 1;
1251 : move16();
1252 :
1253 : IF( dtxConfig.variable_SID_rate )
1254 : {
1255 : hEncoderConfig->var_SID_rate_flag = 1;
1256 : hEncoderConfig->interval_SID = 0;
1257 : move16();
1258 : move16();
1259 : }
1260 : ELSE
1261 : {
1262 : hEncoderConfig->var_SID_rate_flag = 0;
1263 : move16();
1264 : test();
1265 : IF( GE_16( dtxConfig.SID_interval, 3 ) && LE_16( dtxConfig.SID_interval, 100 ) )
1266 : {
1267 : hEncoderConfig->interval_SID = dtxConfig.SID_interval;
1268 : move16();
1269 : }
1270 : ELSE
1271 : {
1272 : return IVAS_ERR_INVALID_DTX_UPDATE_RATE;
1273 : }
1274 : }
1275 : }
1276 : ELSE
1277 : {
1278 : hEncoderConfig->Opt_DTX_ON = 0;
1279 : move16();
1280 : }
1281 :
1282 : /*-----------------------------------------------------------------*
1283 : * Bitrate
1284 : *-----------------------------------------------------------------*/
1285 :
1286 : hEncoderConfig->ivas_total_brate = initBitrate;
1287 : move16();
1288 :
1289 : /* SC-VBR at 5.90 kbps */
1290 : IF( EQ_32( hEncoderConfig->ivas_total_brate, ACELP_5k90 ) )
1291 : {
1292 : hEncoderConfig->ivas_total_brate = ACELP_7k20;
1293 : hEncoderConfig->Opt_SC_VBR = 1;
1294 : hEncoderConfig->last_Opt_SC_VBR = hEncoderConfig->Opt_SC_VBR;
1295 : move32();
1296 : move16();
1297 : move16();
1298 :
1299 : IF( ( hEncoderConfig->max_bwidth != NB ) )
1300 : {
1301 : hEncoderConfig->max_bwidth = WB;
1302 : move16();
1303 : }
1304 : }
1305 :
1306 : /* check if the entered bitrate is supported */
1307 : test();
1308 : IF( NE_16( hEncoderConfig->ivas_format, UNDEFINED_FORMAT ) && NE_16( hEncoderConfig->ivas_format, MONO_FORMAT ) ) /* IVAS */
1309 : {
1310 : IF( !is_IVAS_bitrate_fx( hEncoderConfig->ivas_total_brate ) )
1311 : {
1312 : IF( hEncoderConfig->Opt_SC_VBR )
1313 : {
1314 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Incorrect bitrate specification in IVAS [bps]: %d", ACELP_5k90 );
1315 : }
1316 : ELSE
1317 : {
1318 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Incorrect bitrate specification in IVAS [bps]: %d", hEncoderConfig->ivas_total_brate );
1319 : }
1320 : }
1321 :
1322 : IF( EQ_16( hEncoderConfig->ivas_format, STEREO_FORMAT ) )
1323 : {
1324 : {
1325 : hEncoderConfig->element_mode_init = IVAS_CPE_DFT;
1326 : move16();
1327 : IF( GE_32( hEncoderConfig->ivas_total_brate, MIN_BRATE_MDCT_STEREO ) )
1328 : {
1329 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
1330 : move16();
1331 : }
1332 : }
1333 :
1334 : test();
1335 : test();
1336 : IF( ( EQ_16( hEncoderConfig->element_mode_init, IVAS_CPE_TD ) || EQ_32( hEncoderConfig->element_mode_init, IVAS_CPE_DFT ) ) && GT_32( hEncoderConfig->ivas_total_brate, IVAS_48k ) )
1337 : {
1338 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for TD/DFT Stereo specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
1339 : }
1340 :
1341 : test();
1342 : IF( EQ_16( hEncoderConfig->element_mode_init, IVAS_CPE_MDCT ) && LT_32( hEncoderConfig->ivas_total_brate, IVAS_48k ) )
1343 : {
1344 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too low bitrate for MDCT Stereo specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
1345 : }
1346 :
1347 : test();
1348 : IF( LT_32( hEncoderConfig->ivas_total_brate, IVAS_256k ) )
1349 : {
1350 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for Stereo specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
1351 : }
1352 : }
1353 : ELSE IF( EQ_16( hEncoderConfig->ivas_format, ISM_FORMAT ) )
1354 : {
1355 : IF( ( error = sanitizeBitrateISM_fx( hEncoderConfig, hIvasEnc->extMetadataApi ) ) != IVAS_ERR_OK )
1356 : {
1357 : return error;
1358 : }
1359 : }
1360 : ELSE IF( EQ_16( hEncoderConfig->ivas_format, SBA_FORMAT ) )
1361 : {
1362 : /* nothing */
1363 : }
1364 : ELSE IF( EQ_16( hEncoderConfig->ivas_format, MASA_FORMAT ) )
1365 : {
1366 : /* adapt element_mode according to the bitrate */
1367 : test();
1368 : IF( EQ_16( hEncoderConfig->nchan_inp, 2 ) && NE_16( hEncoderConfig->element_mode_init, IVAS_SCE ) )
1369 : {
1370 : IF( GE_32( hEncoderConfig->ivas_total_brate, IVAS_48k ) )
1371 : {
1372 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
1373 : move16();
1374 : }
1375 : ELSE IF( LT_32( hEncoderConfig->ivas_total_brate, MASA_STEREO_MIN_BITRATE ) )
1376 : {
1377 : hEncoderConfig->element_mode_init = IVAS_CPE_DFT;
1378 : move16();
1379 : }
1380 : }
1381 : }
1382 : ELSE IF( EQ_32( hEncoderConfig->ivas_format, MASA_ISM_FORMAT ) )
1383 : {
1384 : st_ivas->ism_mode = ivas_omasa_ism_mode_select_fx( st_ivas->hEncoderConfig->ivas_total_brate, hEncoderConfig->nchan_ism );
1385 :
1386 : cpe_brate = calculate_cpe_brate_MASA_ISM_fx( st_ivas->ism_mode, st_ivas->hEncoderConfig->ivas_total_brate, hEncoderConfig->nchan_ism );
1387 :
1388 : /*adapt element_mode according to the bit-rate*/
1389 : IF( NE_16( hEncoderConfig->element_mode_init, IVAS_SCE ) )
1390 : {
1391 : IF( GE_32( cpe_brate, IVAS_48k ) )
1392 : {
1393 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
1394 : move16();
1395 : }
1396 : }
1397 : }
1398 : ELSE IF( EQ_16( hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) )
1399 : {
1400 : st_ivas->ism_mode = ISM_MODE_NONE;
1401 : }
1402 : }
1403 : ELSE /* EVS mono */
1404 : {
1405 : hEncoderConfig->ivas_format = MONO_FORMAT;
1406 : hEncoderConfig->element_mode_init = EVS_MONO;
1407 : move16();
1408 :
1409 : IF( !is_EVS_bitrate( hEncoderConfig->ivas_total_brate, &hEncoderConfig->Opt_AMR_WB ) )
1410 : {
1411 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Incorrect bitrate specification in EVS mono: %d", hEncoderConfig->ivas_total_brate );
1412 : }
1413 :
1414 : IF( EQ_16( hEncoderConfig->stereo_dmx_evs, 1 ) )
1415 : {
1416 : hEncoderConfig->nchan_inp = 2;
1417 : move16();
1418 : }
1419 : }
1420 :
1421 : /*-----------------------------------------------------------------*
1422 : * Input sampling frequency
1423 : *-----------------------------------------------------------------*/
1424 : test();
1425 : test();
1426 : test();
1427 : IF( NE_32( inputFs, 8000 ) && NE_32( inputFs, 16000 ) && NE_32( inputFs, 32000 ) && NE_32( inputFs, 48000 ) )
1428 : {
1429 : return IVAS_ERR_INVALID_SAMPLING_RATE;
1430 : }
1431 :
1432 : hEncoderConfig->input_Fs = inputFs;
1433 : move32();
1434 :
1435 : /*-----------------------------------------------------------------*
1436 : * Channel-aware mode
1437 : *-----------------------------------------------------------------*/
1438 :
1439 : IF( ( error = setChannelAwareConfig_fx( hIvasEnc, caConfig ) ) != IVAS_ERR_OK )
1440 : {
1441 : return error;
1442 : }
1443 :
1444 : /*-----------------------------------------------------------------*
1445 : * Set codec mode
1446 : *-----------------------------------------------------------------*/
1447 :
1448 : st_ivas->codec_mode = MODE1; /* Note: in IVAS, set MODE1 */
1449 : move16();
1450 :
1451 : IF( EQ_16( hEncoderConfig->ivas_format, MONO_FORMAT ) )
1452 : {
1453 : IF( hEncoderConfig->Opt_AMR_WB )
1454 : {
1455 : st_ivas->codec_mode = MODE1;
1456 : move16();
1457 : }
1458 : ELSE
1459 : {
1460 : st_ivas->codec_mode = get_codec_mode( hEncoderConfig->ivas_total_brate );
1461 : move16();
1462 : }
1463 : }
1464 :
1465 : test();
1466 : IF( EQ_32( hEncoderConfig->ivas_total_brate, IVAS_13k2 ) && EQ_16( hEncoderConfig->Opt_RF_ON, 1 ) )
1467 : {
1468 : st_ivas->codec_mode = MODE2;
1469 : move16();
1470 : }
1471 :
1472 : st_ivas->last_codec_mode = st_ivas->codec_mode;
1473 : move16();
1474 :
1475 : /*-----------------------------------------------------------------*
1476 : * Sanity checks
1477 : *-----------------------------------------------------------------*/
1478 :
1479 : assert( hEncoderConfig->ivas_format != UNDEFINED_FORMAT && "\n IVAS format undefined" );
1480 :
1481 : test();
1482 : test();
1483 : IF( ( NE_16( hEncoderConfig->ivas_format, MONO_FORMAT ) || hEncoderConfig->stereo_dmx_evs ) && EQ_32( hEncoderConfig->input_Fs, 8000 ) )
1484 : {
1485 : return IVAS_ERROR( IVAS_ERR_INVALID_SAMPLING_RATE, "8kHz input sampling rate is not supported in IVAS." );
1486 : }
1487 :
1488 : test();
1489 : test();
1490 : test();
1491 : test();
1492 : test();
1493 : test();
1494 : IF( hEncoderConfig->Opt_DTX_ON && NE_16( hEncoderConfig->ivas_format, MONO_FORMAT ) &&
1495 : ( ( EQ_16( hEncoderConfig->ivas_format, SBA_FORMAT ) && GT_32( ivas_get_sba_num_TCs_fx( hEncoderConfig->ivas_total_brate, 1 ), 2 ) ) ||
1496 : EQ_16( hEncoderConfig->ivas_format, MC_FORMAT ) || EQ_16( hEncoderConfig->ivas_format, MASA_ISM_FORMAT ) || EQ_16( hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) ) )
1497 : {
1498 : return IVAS_ERROR( IVAS_ERR_DTX_NOT_SUPPORTED, "DTX is not supported in this IVAS format and element mode." );
1499 : }
1500 :
1501 : test();
1502 : test();
1503 : test();
1504 : IF( hEncoderConfig->Opt_PCA_ON && !( EQ_16( hEncoderConfig->ivas_format, SBA_FORMAT ) && EQ_32( hEncoderConfig->ivas_total_brate, PCA_BRATE ) && EQ_16( hEncoderConfig->sba_order, SBA_FOA_ORDER ) ) )
1505 : {
1506 : return IVAS_ERROR( IVAS_ERR_NOT_SUPPORTED_OPTION, "PCA supported at SBA FOA 256 kbps only." );
1507 : }
1508 :
1509 : IF( ( error = sanitizeBandwidth_fx( hIvasEnc ) ) != IVAS_ERR_OK )
1510 : {
1511 : return error;
1512 : }
1513 :
1514 : test();
1515 : test();
1516 : test();
1517 : IF( hEncoderConfig->is_binaural && !( ( EQ_16( hEncoderConfig->ivas_format, MONO_FORMAT ) && hEncoderConfig->stereo_dmx_evs ) || EQ_16( hEncoderConfig->ivas_format, STEREO_FORMAT ) ) )
1518 : {
1519 : return IVAS_ERROR( IVAS_ERR_NOT_SUPPORTED_OPTION, "'-binaural' option is supported only with '-stereo' or '-stereo_dmx_evs'" );
1520 : }
1521 :
1522 : /*-----------------------------------------------------------------*
1523 : * Finalize initialization
1524 : *-----------------------------------------------------------------*/
1525 :
1526 : IF( ( error = ivas_init_encoder_fx( st_ivas ) ) != IVAS_ERR_OK )
1527 : {
1528 : return error;
1529 : }
1530 :
1531 : IF( EQ_16( hEncoderConfig->ivas_format, MONO_FORMAT ) )
1532 : {
1533 : hIvasEnc->hCoreCoder = st_ivas->hSCE[0]->hCoreCoder[0]; /* Note: this is needed for switching in EVS mono */
1534 : }
1535 : ELSE
1536 : {
1537 : hIvasEnc->hCoreCoder = NULL;
1538 : }
1539 :
1540 : hIvasEnc->Opt_RF_ON_loc = hEncoderConfig->Opt_RF_ON;
1541 : hIvasEnc->rf_fec_offset_loc = hEncoderConfig->rf_fec_offset;
1542 : move16();
1543 : move16();
1544 :
1545 : hIvasEnc->isConfigured = true;
1546 :
1547 : return error;
1548 : }
1549 : #endif
1550 : /*---------------------------------------------------------------------*
1551 : * IVAS_ENC_GetDelay()
1552 : *
1553 : *
1554 : *---------------------------------------------------------------------*/
1555 :
1556 624 : ivas_error IVAS_ENC_GetDelay(
1557 : const IVAS_ENC_HANDLE hIvasEnc, /* i : IVAS encoder handle */
1558 : Word16 *delay /* o : encoder delay Q0*/
1559 : )
1560 : {
1561 : ENCODER_CONFIG_HANDLE hEncoderConfig;
1562 :
1563 624 : hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
1564 :
1565 624 : IF( !hIvasEnc->isConfigured )
1566 : {
1567 0 : return IVAS_ERR_NOT_CONFIGURED;
1568 : }
1569 :
1570 624 : IF( delay == NULL )
1571 : {
1572 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1573 : }
1574 :
1575 624 : *delay = NS2SA_FX2( hEncoderConfig->input_Fs, get_delay_fx( ENC, hEncoderConfig->input_Fs, hEncoderConfig->ivas_format, NULL ) ); /*Q0*/
1576 624 : move16();
1577 :
1578 624 : *delay = imult1616( *delay, hEncoderConfig->nchan_inp ); /*Q0*/
1579 624 : move16();
1580 :
1581 624 : return IVAS_ERR_OK;
1582 : }
1583 :
1584 :
1585 : /*---------------------------------------------------------------------*
1586 : * getInputBufferSize()
1587 : *
1588 : *
1589 : *---------------------------------------------------------------------*/
1590 425012 : static Word16 getInputBufferSize_fx(
1591 : const Encoder_Struct *st_ivas /* i : IVAS encoder handle */
1592 : )
1593 : {
1594 425012 : return extract_l( Mpy_32_32( imult3216( st_ivas->hEncoderConfig->input_Fs, st_ivas->hEncoderConfig->nchan_inp ), ONE_BY_FRAMES_PER_SEC_Q31 ) );
1595 : }
1596 :
1597 : /*---------------------------------------------------------------------*
1598 : * IVAS_ENC_GetNumInChannels()
1599 : *
1600 : *
1601 : *---------------------------------------------------------------------*/
1602 624 : ivas_error IVAS_ENC_GetNumInChannels(
1603 : const IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
1604 : Word16 *numInChannels /* o : total number of samples expected in the input buffer for current encoder configuration */
1605 : )
1606 : {
1607 624 : test();
1608 624 : IF( hIvasEnc == NULL || numInChannels == NULL )
1609 : {
1610 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1611 : }
1612 :
1613 624 : IF( !hIvasEnc->isConfigured )
1614 : {
1615 0 : return IVAS_ERR_NOT_CONFIGURED;
1616 : }
1617 :
1618 624 : *numInChannels = hIvasEnc->st_ivas->hEncoderConfig->nchan_inp;
1619 624 : move16();
1620 :
1621 624 : return IVAS_ERR_OK;
1622 : }
1623 :
1624 :
1625 : /*---------------------------------------------------------------------*
1626 : * IVAS_ENC_GetInputBufferSize()
1627 : *
1628 : *
1629 : *---------------------------------------------------------------------*/
1630 624 : ivas_error IVAS_ENC_GetInputBufferSize(
1631 : const IVAS_ENC_HANDLE hIvasEnc, /* i : IVAS encoder handle */
1632 : Word16 *inputBufferSize /* o : total number of samples expected in the input buffer for current encoder configuration */
1633 : )
1634 : {
1635 624 : IF( !hIvasEnc->isConfigured )
1636 : {
1637 0 : return IVAS_ERR_NOT_CONFIGURED;
1638 : }
1639 :
1640 624 : IF( inputBufferSize == NULL )
1641 : {
1642 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
1643 : }
1644 :
1645 624 : *inputBufferSize = getInputBufferSize_fx( hIvasEnc->st_ivas );
1646 624 : move16();
1647 :
1648 624 : return IVAS_ERR_OK;
1649 : }
1650 :
1651 :
1652 : /*---------------------------------------------------------------------*
1653 : * IVAS_ENC_EncodeFrameToSerial()
1654 : *
1655 : * Main function to encode one frame to a serial bitstream
1656 : *---------------------------------------------------------------------*/
1657 424388 : ivas_error IVAS_ENC_EncodeFrameToSerial(
1658 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
1659 : Word16 *inputBuffer, /* i : PCM input, Q0 Q0*/
1660 : Word16 inputBufferSize, /* i : total number of samples in the input buffer. Related function: IVAS_ENC_GetInputBufferSize() Q0*/
1661 : UWord16 *outputBitStream, /* o : pointer to serial output bitstream. The array must already be allocated and be of size at least IVAS_MAX_BITS_PER_FRAME Q0*/
1662 : UWord16 *numOutBits /* o : number of bits written to output bitstream. Each bit is stored as a single uint16_t value Q0*/
1663 : )
1664 : {
1665 : Encoder_Struct *st_ivas;
1666 : ENCODER_CONFIG_HANDLE hEncoderConfig;
1667 : Word16 i;
1668 : Word16 n, ch;
1669 : ivas_error error;
1670 :
1671 424388 : error = IVAS_ERR_OK;
1672 424388 : move32();
1673 :
1674 424388 : IF( !hIvasEnc->isConfigured )
1675 : {
1676 0 : return IVAS_ERR_NOT_CONFIGURED;
1677 : }
1678 :
1679 424388 : st_ivas = hIvasEnc->st_ivas;
1680 424388 : hEncoderConfig = st_ivas->hEncoderConfig;
1681 424388 : ENC_CORE_HANDLE hCoreCoder = hIvasEnc->hCoreCoder;
1682 :
1683 424388 : IF( NE_16( inputBufferSize, getInputBufferSize_fx( st_ivas ) ) )
1684 : {
1685 0 : return IVAS_ERR_INVALID_INPUT_BUFFER_SIZE;
1686 : }
1687 :
1688 424388 : IF( NE_32( ( error = sanitizeBandwidth_fx( hIvasEnc ) ), IVAS_ERR_OK ) )
1689 : {
1690 0 : return error;
1691 : }
1692 :
1693 424388 : IF( EQ_32( hEncoderConfig->ivas_format, ISM_FORMAT ) )
1694 : {
1695 407452 : FOR( i = 0; i < hEncoderConfig->nchan_inp; ++i )
1696 : {
1697 303626 : IF( !hIvasEnc->ismMetadataProvided[i] )
1698 : {
1699 6000 : ivas_ism_reset_metadata_API( hIvasEnc->st_ivas->hIsmMetaData[i] );
1700 : }
1701 : }
1702 103826 : resetIsmMetadataProvidedFlags( hIvasEnc );
1703 : }
1704 :
1705 424388 : test();
1706 424388 : test();
1707 424388 : test();
1708 424388 : test();
1709 424388 : IF( ( hEncoderConfig->Opt_RF_ON && ( NE_32( hEncoderConfig->ivas_total_brate, ACELP_13k20 ) || EQ_32( hEncoderConfig->input_Fs, 8000 ) || hEncoderConfig->max_bwidth == NB ) ) || hEncoderConfig->rf_fec_offset == 0 )
1710 : {
1711 424388 : test();
1712 424388 : IF( EQ_32( hEncoderConfig->ivas_total_brate, ACELP_13k20 ) && EQ_32( hEncoderConfig->ivas_format, MONO_FORMAT ) )
1713 : {
1714 1050 : st_ivas->codec_mode = MODE1;
1715 1050 : move16();
1716 :
1717 1050 : reset_rf_indices_fx( hCoreCoder );
1718 : }
1719 424388 : hEncoderConfig->Opt_RF_ON = 0;
1720 424388 : move16();
1721 424388 : hEncoderConfig->rf_fec_offset = 0;
1722 424388 : move16();
1723 424388 : hIvasEnc->switchingActive = true;
1724 424388 : move16();
1725 : }
1726 :
1727 424388 : test();
1728 424388 : test();
1729 424388 : test();
1730 424388 : test();
1731 424388 : 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 )
1732 : {
1733 0 : st_ivas->codec_mode = MODE2;
1734 0 : move16();
1735 0 : test();
1736 0 : IF( hEncoderConfig->Opt_RF_ON == 0 && EQ_16( hEncoderConfig->ivas_format, MONO_FORMAT ) )
1737 : {
1738 0 : reset_rf_indices_fx( hCoreCoder );
1739 : }
1740 0 : hEncoderConfig->Opt_RF_ON = 1;
1741 0 : hEncoderConfig->rf_fec_offset = hIvasEnc->rf_fec_offset_loc;
1742 0 : hIvasEnc->switchingActive = true;
1743 0 : move16();
1744 0 : move16();
1745 0 : move16();
1746 : }
1747 :
1748 : /* in case of 8kHz sampling rate or when in "max_band NB" mode, limit the total bitrate to 24.40 kbps */
1749 424388 : test();
1750 424388 : test();
1751 424388 : IF( ( EQ_32( hEncoderConfig->input_Fs, 8000 ) || ( hEncoderConfig->max_bwidth == NB ) ) && GT_32( hEncoderConfig->ivas_total_brate, ACELP_24k40 ) )
1752 : {
1753 0 : hEncoderConfig->ivas_total_brate = ACELP_24k40;
1754 0 : st_ivas->codec_mode = MODE2;
1755 0 : hIvasEnc->switchingActive = true;
1756 0 : move32();
1757 0 : move16();
1758 0 : move16();
1759 : }
1760 :
1761 : /*-----------------------------------------------------------------*
1762 : * Re-allocate and re-initialize buffer of indices if IVAS total bitrate has changed
1763 : *-----------------------------------------------------------------*/
1764 :
1765 424388 : IF( NE_32( hEncoderConfig->ivas_total_brate, hEncoderConfig->last_ivas_total_brate ) )
1766 : {
1767 : /* de-allocate old buffer of indices */
1768 6724 : free( st_ivas->ind_list );
1769 :
1770 : /* set the maximum allowed number of indices in the list */
1771 6724 : st_ivas->ivas_max_num_indices = get_ivas_max_num_indices_fx( hEncoderConfig->ivas_format, hEncoderConfig->ivas_total_brate );
1772 6724 : move16();
1773 :
1774 : /* allocate new buffer of indices */
1775 6724 : IF( ( st_ivas->ind_list = (INDICE_HANDLE) malloc( st_ivas->ivas_max_num_indices * sizeof( Indice ) ) ) == NULL )
1776 : {
1777 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for buffer of indices!\n" ) );
1778 : }
1779 :
1780 : /* reset the list of indices */
1781 5239804 : FOR( i = 0; i < st_ivas->ivas_max_num_indices; i++ )
1782 : {
1783 5233080 : st_ivas->ind_list[i].nb_bits = -1;
1784 5233080 : move16();
1785 : }
1786 :
1787 : /* de-allocate old buffer of metadata indices */
1788 6724 : IF( st_ivas->ind_list_metadata != NULL )
1789 : {
1790 6724 : free( st_ivas->ind_list_metadata );
1791 : }
1792 :
1793 : /* set the maximum allowed number of metadata indices in the list */
1794 6724 : st_ivas->ivas_max_num_indices_metadata = get_ivas_max_num_indices_metadata_fx( hEncoderConfig->ivas_format, hEncoderConfig->ivas_total_brate );
1795 6724 : move16();
1796 :
1797 6724 : IF( st_ivas->ivas_max_num_indices_metadata > 0 )
1798 : {
1799 : /* allocate new buffer of metadata indices */
1800 6724 : IF( ( st_ivas->ind_list_metadata = (INDICE_HANDLE) malloc( st_ivas->ivas_max_num_indices_metadata * sizeof( Indice ) ) ) == NULL )
1801 : {
1802 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for buffer of metadata indices!\n" ) );
1803 : }
1804 :
1805 : /* reset the list of metadata indices */
1806 2326189 : FOR( i = 0; i < st_ivas->ivas_max_num_indices_metadata; i++ )
1807 : {
1808 2319465 : st_ivas->ind_list_metadata[i].nb_bits = -1;
1809 2319465 : move16();
1810 : }
1811 : }
1812 : ELSE
1813 : {
1814 0 : st_ivas->ind_list_metadata = NULL;
1815 : }
1816 :
1817 : /* set pointers to the new buffers of indices in each element */
1818 13092 : FOR( n = 0; n < st_ivas->nSCE; n++ )
1819 : {
1820 : #ifdef HARM_PUSH_BIT
1821 6368 : st_ivas->hSCE[n]->hCoreCoder[0]->hBstr->ind_list = st_ivas->ind_list;
1822 6368 : st_ivas->hSCE[n]->hCoreCoder[0]->hBstr->ivas_ind_list_zero = &st_ivas->ind_list;
1823 :
1824 6368 : if ( st_ivas->hSCE[n]->hMetaData != NULL )
1825 : {
1826 3627 : st_ivas->hSCE[n]->hMetaData->ind_list = st_ivas->ind_list_metadata;
1827 3627 : st_ivas->hSCE[n]->hMetaData->ivas_ind_list_zero = &st_ivas->ind_list_metadata;
1828 : }
1829 : #else
1830 : test();
1831 : IF( !( hIvasEnc->hCoreCoder == NULL && EQ_32( hEncoderConfig->ivas_format, MONO_FORMAT ) ) )
1832 : {
1833 : if ( ( hEncoderConfig->element_mode_init != EVS_MONO ) )
1834 : {
1835 : st_ivas->hSCE[n]->hCoreCoder[0]->hBstr->ind_list = st_ivas->ind_list;
1836 : }
1837 : st_ivas->hSCE[n]->hCoreCoder[0]->hBstr->ivas_ind_list_zero = &st_ivas->ind_list;
1838 : }
1839 :
1840 : IF( st_ivas->hSCE[n]->hMetaData != NULL )
1841 : {
1842 : st_ivas->hSCE[n]->hMetaData->ind_list = st_ivas->ind_list_metadata;
1843 : st_ivas->hSCE[n]->hMetaData->ivas_ind_list_zero = &st_ivas->ind_list_metadata;
1844 : }
1845 : #endif
1846 : }
1847 :
1848 12884 : FOR( n = 0; n < st_ivas->nCPE; n++ )
1849 : {
1850 18480 : FOR( ch = 0; ch < CPE_CHANNELS; ch++ )
1851 : {
1852 12320 : st_ivas->hCPE[n]->hCoreCoder[ch]->hBstr->ind_list = st_ivas->ind_list;
1853 12320 : st_ivas->hCPE[n]->hCoreCoder[ch]->hBstr->ivas_ind_list_zero = &st_ivas->ind_list;
1854 : }
1855 :
1856 6160 : IF( st_ivas->hCPE[n]->hMetaData != NULL )
1857 : {
1858 4310 : st_ivas->hCPE[n]->hMetaData->ind_list = st_ivas->ind_list_metadata;
1859 4310 : st_ivas->hCPE[n]->hMetaData->ivas_ind_list_zero = &st_ivas->ind_list_metadata;
1860 : }
1861 : }
1862 : }
1863 :
1864 424388 : test();
1865 424388 : IF( hIvasEnc->switchingActive && EQ_16( hEncoderConfig->ivas_format, MONO_FORMAT ) )
1866 : {
1867 3100 : copy_encoder_config_fx( st_ivas, hCoreCoder, 0 );
1868 3100 : hEncoderConfig->last_ivas_total_brate = hEncoderConfig->ivas_total_brate;
1869 3100 : move32();
1870 : }
1871 :
1872 : /* run the main encoding routine */
1873 424388 : IF( EQ_32( hEncoderConfig->ivas_format, MONO_FORMAT ) ) /* EVS mono */
1874 : {
1875 3100 : hCoreCoder->total_brate = hEncoderConfig->ivas_total_brate; /* needed in case of bitrate switching */
1876 3100 : move32();
1877 :
1878 3100 : IF( EQ_16( hEncoderConfig->stereo_dmx_evs, 1 ) )
1879 : {
1880 2100 : inputBufferSize = shr( inputBufferSize, 1 );
1881 2100 : stereo_dmx_evs_enc_fx( st_ivas->hStereoDmxEVS, hEncoderConfig->input_Fs, inputBuffer, inputBufferSize, hEncoderConfig->is_binaural );
1882 : }
1883 :
1884 3100 : IF( hEncoderConfig->Opt_AMR_WB )
1885 : {
1886 0 : amr_wb_enc_fx( hCoreCoder, inputBuffer, inputBufferSize );
1887 : }
1888 : ELSE
1889 : {
1890 : #ifdef DEBUG_MODE_INFO
1891 : dbgwrite( inputBuffer, sizeof( int16_t ), inputBufferSize, 1, strcat( fname( debug_dir, "ivas_input_dmx", 0, 1, ENC ), ".pcm" ) );
1892 : #endif
1893 3100 : hCoreCoder->input_frame_fx = inputBufferSize;
1894 3100 : move32();
1895 3100 : IF( NE_32( ( error = evs_enc_fx( hCoreCoder, inputBuffer, hCoreCoder->mem_hp20_in_fx, inputBufferSize ) ), IVAS_ERR_OK ) )
1896 : {
1897 0 : return error;
1898 : }
1899 : }
1900 : }
1901 : ELSE /* IVAS */
1902 : {
1903 421288 : IF( NE_32( ( error = ivas_enc_fx( st_ivas, inputBuffer, inputBufferSize ) ), IVAS_ERR_OK ) )
1904 : {
1905 0 : return error;
1906 : }
1907 : }
1908 :
1909 : /* write indices into bitstream buffer */
1910 : #ifndef HARM_PUSH_BIT
1911 : IF( hEncoderConfig->element_mode_init == EVS_MONO )
1912 : {
1913 : test();
1914 : IF( EQ_16( hEncoderConfig->ivas_format, MONO_FORMAT ) && ( hCoreCoder->element_mode == EVS_MONO ) )
1915 : {
1916 : /* write indices into bitstream file */
1917 : UWord8 pFrame[( MAX_BITS_PER_FRAME + 7 ) >> 3];
1918 : Word16 pFrame_size = 0;
1919 : move16();
1920 : write_indices_buf_fx( hCoreCoder, hCoreCoder->hBstr, outputBitStream, pFrame, pFrame_size, numOutBits );
1921 : }
1922 : }
1923 : ELSE
1924 : #endif
1925 : {
1926 424388 : write_indices_ivas_fx( st_ivas, outputBitStream, numOutBits );
1927 : }
1928 : /* Reset switching flag before next call - can be set to "true" by some setters */
1929 424388 : hIvasEnc->switchingActive = false;
1930 424388 : move16();
1931 :
1932 424388 : return error;
1933 : }
1934 :
1935 :
1936 : /*---------------------------------------------------------------------*
1937 : * IVAS_ENC_SetBandwidth()
1938 : *
1939 : *
1940 : *---------------------------------------------------------------------*/
1941 :
1942 18150 : ivas_error IVAS_ENC_SetBandwidth(
1943 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
1944 : const IVAS_ENC_BANDWIDTH maxBandwidth /* i : bandwidth limitation to be used */
1945 : )
1946 : {
1947 : ivas_error error;
1948 :
1949 : /* Do additional checks for user-facing function */
1950 18150 : IF( ( error = doCommonSetterChecks( hIvasEnc ) ) != IVAS_ERR_OK )
1951 : {
1952 0 : return error;
1953 : }
1954 :
1955 : /* Use internal function to set bandiwdth */
1956 18150 : return setBandwidth_fx( hIvasEnc, maxBandwidth );
1957 : }
1958 :
1959 :
1960 : /*---------------------------------------------------------------------*
1961 : * IVAS_ENC_SetBitrate()
1962 : *
1963 : *
1964 : *---------------------------------------------------------------------*/
1965 :
1966 56138 : ivas_error IVAS_ENC_SetBitrate(
1967 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
1968 : const Word32 totalBitrate /* i : requested bitrate of the output bitstream */
1969 : )
1970 : {
1971 : ivas_error error;
1972 :
1973 : /* Do additional checks for user-facing function */
1974 56138 : IF( ( error = doCommonSetterChecks( hIvasEnc ) ) != IVAS_ERR_OK )
1975 : {
1976 0 : return error;
1977 : }
1978 :
1979 : /* Use internal function to set bitrate */
1980 56138 : return setBitrate( hIvasEnc, totalBitrate );
1981 : }
1982 :
1983 :
1984 : /*---------------------------------------------------------------------*
1985 : * IVAS_ENC_SetChannelAwareConfig()
1986 : *
1987 : * Sets the configuration of channel-aware mode. Since CA mode is only
1988 : * supported at 13.2 kbps, this function has no effect at other bitrates.
1989 : *---------------------------------------------------------------------*/
1990 :
1991 0 : ivas_error IVAS_ENC_SetChannelAwareConfig(
1992 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
1993 : const IVAS_ENC_CHANNEL_AWARE_CONFIG rfConfig /* i : configuration of channel-aware mode */
1994 : )
1995 : {
1996 : ivas_error error;
1997 :
1998 : /* Do additional checks for user-facing function */
1999 0 : IF( ( error = doCommonSetterChecks( hIvasEnc ) ) != IVAS_ERR_OK )
2000 : {
2001 0 : return error;
2002 : }
2003 :
2004 : /* Use internal function to set CA config */
2005 0 : return setChannelAwareConfig_fx( hIvasEnc, rfConfig );
2006 : }
2007 :
2008 : #ifdef DEBUGGING
2009 : /*---------------------------------------------------------------------*
2010 : * IVAS_ENC_SetForcedMode()
2011 : *
2012 : *
2013 : *---------------------------------------------------------------------*/
2014 :
2015 : ivas_error IVAS_ENC_SetForcedMode(
2016 : IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle */
2017 : const IVAS_ENC_FORCED_MODE forcedMode /* i : forced coding mode */
2018 : #ifdef DEBUG_FORCE_DIR
2019 : ,
2020 : const char *forcedModeDir /* i : directory containing external binary files for modes/parameters enforcement */
2021 : #endif
2022 : )
2023 : {
2024 : int16_t newForced;
2025 : ivas_error error;
2026 :
2027 : /* Do additional checks for user-facing function */
2028 : if ( ( error = doCommonSetterChecks( hIvasEnc ) ) != IVAS_ERR_OK )
2029 : {
2030 : return error;
2031 : }
2032 :
2033 : #ifdef DEBUG_FORCE_DIR
2034 : hIvasEnc->st_ivas->hEncoderConfig->force_dir[0] = '\0';
2035 : if ( forcedMode < IVAS_ENC_FORCE_FILE )
2036 : {
2037 : if ( ( error = forcedModeApiToInternal( forcedMode, &newForced ) ) != IVAS_ERR_OK )
2038 : {
2039 : return error;
2040 : }
2041 :
2042 : if ( hIvasEnc->st_ivas->hEncoderConfig->force != newForced )
2043 : {
2044 : hIvasEnc->st_ivas->hEncoderConfig->force = newForced;
2045 : hIvasEnc->switchingActive = true;
2046 : }
2047 : }
2048 : else if ( forcedMode == IVAS_ENC_FORCE_DIR )
2049 : {
2050 : strcpy( hIvasEnc->st_ivas->hEncoderConfig->force_dir, forcedModeDir );
2051 : hIvasEnc->st_ivas->hEncoderConfig->force = IVAS_ENC_FORCE_UNFORCED;
2052 : }
2053 : #else
2054 : if ( ( error = forcedModeApiToInternal( forcedMode, &newForced ) ) != IVAS_ERR_OK )
2055 : {
2056 : return error;
2057 : }
2058 :
2059 : if ( hIvasEnc->st_ivas->hEncoderConfig->force != newForced )
2060 : {
2061 : hIvasEnc->st_ivas->hEncoderConfig->force = newForced;
2062 : hIvasEnc->switchingActive = true;
2063 : }
2064 : #endif
2065 :
2066 : return IVAS_ERR_OK;
2067 : }
2068 : #endif /* DEBUGGING */
2069 :
2070 : /*---------------------------------------------------------------------*
2071 : * IVAS_ENC_GetDefaultBandwidth()
2072 : *
2073 : *
2074 : *---------------------------------------------------------------------*/
2075 :
2076 448 : IVAS_ENC_BANDWIDTH IVAS_ENC_GetDefaultBandwidth( const bool isEVS )
2077 : {
2078 448 : IVAS_ENC_BANDWIDTH bwidth = IVAS_ENC_BANDWIDTH_FB;
2079 :
2080 448 : if ( isEVS )
2081 : {
2082 3 : bwidth = IVAS_ENC_BANDWIDTH_SWB;
2083 : }
2084 :
2085 448 : return bwidth;
2086 : }
2087 :
2088 :
2089 : /*---------------------------------------------------------------------*
2090 : * IVAS_ENC_GetDefaultDtxConfig()
2091 : *
2092 : *
2093 : *---------------------------------------------------------------------*/
2094 :
2095 624 : IVAS_ENC_DTX_CONFIG IVAS_ENC_GetDefaultDtxConfig( void )
2096 : {
2097 : IVAS_ENC_DTX_CONFIG defaultDtxConfig;
2098 624 : defaultDtxConfig.enabled = false;
2099 624 : defaultDtxConfig.SID_interval = 0;
2100 624 : move16();
2101 624 : defaultDtxConfig.variable_SID_rate = false;
2102 :
2103 624 : return defaultDtxConfig;
2104 : }
2105 :
2106 :
2107 : /*---------------------------------------------------------------------*
2108 : * IVAS_ENC_GetDefaultChannelAwareConfig()
2109 : *
2110 : *
2111 : *---------------------------------------------------------------------*/
2112 :
2113 1245 : IVAS_ENC_CHANNEL_AWARE_CONFIG IVAS_ENC_GetDefaultChannelAwareConfig( void )
2114 : {
2115 : IVAS_ENC_CHANNEL_AWARE_CONFIG defaultCaConfig;
2116 1245 : defaultCaConfig.channelAwareModeEnabled = 0;
2117 1245 : move16();
2118 1245 : defaultCaConfig.fec_indicator = IVAS_ENC_FEC_HI;
2119 1245 : move16();
2120 1245 : defaultCaConfig.fec_offset = 0;
2121 1245 : move16();
2122 :
2123 1245 : return defaultCaConfig;
2124 : }
2125 :
2126 :
2127 : /*---------------------------------------------------------------------*
2128 : * IVAS_ENC_GetErrorMessage()
2129 : *
2130 : *
2131 : *---------------------------------------------------------------------*/
2132 :
2133 0 : const char *IVAS_ENC_GetErrorMessage(
2134 : ivas_error error /* i : encoder error code enum */
2135 : )
2136 : {
2137 0 : return ivas_error_to_string( error );
2138 : }
2139 :
2140 :
2141 : /*---------------------------------------------------------------------*
2142 : * Local functions
2143 : *---------------------------------------------------------------------*/
2144 :
2145 624 : static ivas_error printConfigInfo_enc(
2146 : IVAS_ENC_HANDLE hIvasEnc,
2147 : const Word16 channelAwareModeEnabled )
2148 : {
2149 : Encoder_Struct *st_ivas;
2150 : ENCODER_CONFIG_HANDLE hEncoderConfig;
2151 : Word16 newBandwidthApi;
2152 : ivas_error error;
2153 :
2154 624 : st_ivas = hIvasEnc->st_ivas;
2155 624 : hEncoderConfig = st_ivas->hEncoderConfig;
2156 :
2157 : /*-----------------------------------------------------------------*
2158 : * Print input signal sampling frequency
2159 : *-----------------------------------------------------------------*/
2160 :
2161 624 : fprintf( stdout, "Input sampling rate: %d Hz\n", hEncoderConfig->input_Fs );
2162 :
2163 : /*-----------------------------------------------------------------*
2164 : * Print bitrate
2165 : *-----------------------------------------------------------------*/
2166 :
2167 624 : IF( st_ivas->hEncoderConfig->Opt_SC_VBR )
2168 : {
2169 0 : fprintf( stdout, "Average bitrate: %.2f kbps\n", (float) ACELP_5k90 / 1000 );
2170 : }
2171 : ELSE
2172 : {
2173 624 : fprintf( stdout, "Bitrate: %.2f kbps\n", (float) hEncoderConfig->ivas_total_brate / 1000 );
2174 : }
2175 :
2176 : /*-----------------------------------------------------------------*
2177 : * Print IVAS format
2178 : *-----------------------------------------------------------------*/
2179 :
2180 624 : IF( EQ_16( hEncoderConfig->ivas_format, MONO_FORMAT ) )
2181 : {
2182 3 : IF( hEncoderConfig->stereo_dmx_evs )
2183 : {
2184 2 : fprintf( stdout, "IVAS format: stereo downmix to bit-exact EVS mono\n" );
2185 : }
2186 : ELSE
2187 : {
2188 1 : fprintf( stdout, "IVAS format: bit-exact EVS mono\n" );
2189 : }
2190 : }
2191 621 : ELSE IF( hEncoderConfig->ivas_format == STEREO_FORMAT )
2192 : {
2193 : #ifdef DEBUGGING
2194 : if ( hEncoderConfig->stereo_mode_cmdl == 1 )
2195 : {
2196 : fprintf( stdout, "IVAS format: stereo - Unified stereo\n" );
2197 : }
2198 : else if ( hEncoderConfig->element_mode_init == IVAS_CPE_DFT )
2199 : {
2200 : fprintf( stdout, "IVAS format: stereo - DFT stereo\n" );
2201 : }
2202 : else if ( hEncoderConfig->element_mode_init == IVAS_CPE_TD )
2203 : {
2204 : fprintf( stdout, "IVAS format: stereo - TD stereo\n" );
2205 : }
2206 : else if ( hEncoderConfig->element_mode_init == IVAS_CPE_MDCT )
2207 : {
2208 : fprintf( stdout, "IVAS format: stereo - MDCT stereo\n" );
2209 : }
2210 : #else
2211 68 : if ( hEncoderConfig->element_mode_init != IVAS_CPE_MDCT )
2212 : {
2213 50 : fprintf( stdout, "IVAS format: stereo - Unified stereo\n" );
2214 : }
2215 : else
2216 : {
2217 18 : fprintf( stdout, "IVAS format: stereo - MDCT stereo\n" );
2218 : }
2219 : #endif
2220 : }
2221 553 : ELSE IF( EQ_16( hEncoderConfig->ivas_format, ISM_FORMAT ) )
2222 : {
2223 74 : test();
2224 74 : IF( LE_32( hEncoderConfig->ivas_total_brate, ACELP_32k ) && GT_16( hEncoderConfig->nchan_inp, 2 ) )
2225 : {
2226 17 : fprintf( stdout, "IVAS format: Param-ISM (%i streams)\n", hEncoderConfig->nchan_inp );
2227 : }
2228 : ELSE
2229 : {
2230 57 : fprintf( stdout, "IVAS format: ISM (%i streams)\n", hEncoderConfig->nchan_inp );
2231 : }
2232 : }
2233 479 : ELSE IF( EQ_16( hEncoderConfig->ivas_format, SBA_FORMAT ) )
2234 : {
2235 244 : fprintf( stdout, "IVAS format: Scene Based Audio, Ambisonic order %i %s ", hEncoderConfig->sba_order, hEncoderConfig->sba_planar ? "(Planar)" : "" );
2236 244 : IF( hEncoderConfig->Opt_PCA_ON )
2237 : {
2238 4 : fprintf( stdout, "- PCA configured with signal adaptive decision " );
2239 : }
2240 :
2241 244 : fprintf( stdout, "\n" );
2242 : }
2243 235 : ELSE IF( EQ_16( hEncoderConfig->ivas_format, MASA_FORMAT ) )
2244 : {
2245 75 : fprintf( stdout, "IVAS format: MASA format\n" );
2246 : }
2247 160 : ELSE IF( EQ_16( hEncoderConfig->ivas_format, MC_FORMAT ) )
2248 : {
2249 81 : IF( hEncoderConfig->mc_input_setup == MC_LS_SETUP_5_1 )
2250 : {
2251 42 : fprintf( stdout, "IVAS mode: Multi-Channel 5.1 \n" );
2252 : }
2253 39 : ELSE IF( hEncoderConfig->mc_input_setup == MC_LS_SETUP_7_1 )
2254 : {
2255 4 : fprintf( stdout, "IVAS mode: Multi-Channel 7.1 \n" );
2256 : }
2257 35 : ELSE IF( hEncoderConfig->mc_input_setup == MC_LS_SETUP_5_1_2 )
2258 : {
2259 6 : fprintf( stdout, "IVAS mode: Multi-Channel 5.1+2 \n" );
2260 : }
2261 29 : ELSE IF( hEncoderConfig->mc_input_setup == MC_LS_SETUP_5_1_4 )
2262 : {
2263 6 : fprintf( stdout, "IVAS mode: Multi-Channel 5.1+4\n" );
2264 : }
2265 23 : ELSE IF( hEncoderConfig->mc_input_setup == MC_LS_SETUP_7_1_4 )
2266 : {
2267 23 : fprintf( stdout, "IVAS mode: Multi-Channel 7.1+4\n" );
2268 : }
2269 : }
2270 79 : ELSE IF( EQ_16( hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) )
2271 : {
2272 35 : fprintf( stdout, "IVAS format: combined ISM and SBA (%i ISM stream(s))\n", hEncoderConfig->nchan_ism );
2273 : }
2274 44 : ELSE IF( EQ_16( hEncoderConfig->ivas_format, MASA_ISM_FORMAT ) )
2275 : {
2276 44 : fprintf( stdout, "IVAS format: combined ISM and MASA (%i ISM stream(s))\n", hEncoderConfig->nchan_ism );
2277 : }
2278 :
2279 624 : IF( hEncoderConfig->is_binaural )
2280 : {
2281 0 : fprintf( stdout, "Optional indication: binaural audio\n" );
2282 : }
2283 :
2284 : /*-----------------------------------------------------------------*
2285 : * Print CNG update interval, if DTX is activated
2286 : *-----------------------------------------------------------------*/
2287 :
2288 624 : IF( hEncoderConfig->Opt_DTX_ON )
2289 : {
2290 126 : IF( hEncoderConfig->var_SID_rate_flag )
2291 : {
2292 0 : fprintf( stdout, "DTX: ON, variable CNG update interval\n" );
2293 : }
2294 : ELSE
2295 : {
2296 126 : fprintf( stdout, "DTX: ON, CNG update interval = %d frames\n", hEncoderConfig->interval_SID );
2297 : }
2298 : }
2299 :
2300 : /*-----------------------------------------------------------------*
2301 : * Print potential limitation of audio bandwidth
2302 : *-----------------------------------------------------------------*/
2303 :
2304 624 : IF( ( error = bandwidthApiToInternal( hIvasEnc->newBandwidthApi, &newBandwidthApi ) ) != IVAS_ERR_OK )
2305 : {
2306 0 : return error;
2307 : }
2308 :
2309 624 : test();
2310 624 : IF( st_ivas->hEncoderConfig->Opt_SC_VBR && !hEncoderConfig->Opt_DTX_ON )
2311 : {
2312 0 : return IVAS_ERROR( IVAS_ERR_WRONG_PARAMS, "\nError: SC-VBR 5900 bps not supported without DTX\n\n" );
2313 : }
2314 :
2315 624 : IF( EQ_16( hEncoderConfig->ivas_format, MONO_FORMAT ) )
2316 : {
2317 3 : IF( NE_16( newBandwidthApi, hEncoderConfig->max_bwidth ) )
2318 : {
2319 0 : IF( EQ_16( newBandwidthApi, FB ) )
2320 : {
2321 0 : fprintf( stdout, "\nFB coding not supported below %.2f kbps. ", ACELP_16k40 / 1000.f );
2322 0 : IF( EQ_16( hEncoderConfig->max_bwidth, WB ) )
2323 : {
2324 0 : fprintf( stdout, "Switching to WB.\n" );
2325 : }
2326 : ELSE
2327 : {
2328 0 : fprintf( stdout, "Switching to SWB.\n" );
2329 : }
2330 : }
2331 0 : ELSE IF( EQ_16( newBandwidthApi, SWB ) )
2332 : {
2333 0 : fprintf( stdout, "\nSWB coding not supported below %.2f kbps. Switching to WB.\n", ACELP_9k60 / 1000.f );
2334 : }
2335 : }
2336 :
2337 : /* in case of 8kHz input sampling or "-max_band NB", require the total bitrate to be below 24.40 kbps */
2338 3 : test();
2339 3 : test();
2340 3 : IF( ( ( newBandwidthApi == NB ) || EQ_32( hEncoderConfig->input_Fs, 8000 ) ) && GT_32( hEncoderConfig->ivas_total_brate, ACELP_24k40 ) )
2341 : {
2342 0 : fprintf( stdout, "\nError: Unsupported mode NB %d bps, NB mode supports rates 5900-24400 bps\n\n", hEncoderConfig->ivas_total_brate );
2343 0 : return IVAS_ERR_INVALID_BITRATE;
2344 : }
2345 : }
2346 : ELSE
2347 : {
2348 621 : IF( NE_16( newBandwidthApi, hEncoderConfig->max_bwidth ) )
2349 : {
2350 104 : IF( EQ_16( hEncoderConfig->ivas_format, ISM_FORMAT ) )
2351 : {
2352 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 );
2353 : }
2354 : ELSE
2355 : {
2356 81 : fprintf( stdout, "\nFB coding not supported below %.2f kbps. Switching to SWB.\n", MIN_BRATE_FB_STEREO / 1000.f );
2357 : }
2358 : }
2359 : }
2360 :
2361 : /*-----------------------------------------------------------------*
2362 : * Print Channel-aware limitation
2363 : *-----------------------------------------------------------------*/
2364 :
2365 624 : IF( channelAwareModeEnabled )
2366 : {
2367 0 : IF( hEncoderConfig->Opt_RF_ON == 0 )
2368 : {
2369 0 : fprintf( stdout, "\nChannel-aware mode is supported at 13.2 kbps 32/48 kHz only. Switching to normal mode.\n" );
2370 : }
2371 : }
2372 :
2373 624 : return IVAS_ERR_OK;
2374 : }
2375 :
2376 :
2377 : /*---------------------------------------------------------------------*
2378 : * setBitrate()
2379 : *
2380 : *
2381 : *---------------------------------------------------------------------*/
2382 :
2383 56138 : static ivas_error setBitrate(
2384 : IVAS_ENC_HANDLE hIvasEnc,
2385 : const Word32 totalBitrate )
2386 : {
2387 : Encoder_Struct *st_ivas;
2388 : ENCODER_CONFIG_HANDLE hEncoderConfig;
2389 : ivas_error error;
2390 :
2391 56138 : st_ivas = hIvasEnc->st_ivas;
2392 56138 : hEncoderConfig = st_ivas->hEncoderConfig;
2393 :
2394 56138 : hEncoderConfig->ivas_total_brate = totalBitrate;
2395 56138 : hIvasEnc->switchingActive = true;
2396 56138 : move32();
2397 :
2398 : /* channel-aware mode is supported only at 13.20 kbps */
2399 56138 : test();
2400 56138 : IF( hEncoderConfig->Opt_RF_ON && NE_32( hEncoderConfig->ivas_total_brate, ACELP_13k20 ) )
2401 : {
2402 0 : assert( 0 && "\nChannel-aware mode is supported only at 13.20 kbps\n" );
2403 : hEncoderConfig->Opt_RF_ON = 0;
2404 : move16();
2405 : }
2406 :
2407 56138 : IF( EQ_32( hEncoderConfig->ivas_total_brate, ACELP_5k90 ) )
2408 : {
2409 0 : st_ivas->hEncoderConfig->Opt_SC_VBR = 1;
2410 0 : hEncoderConfig->ivas_total_brate = ACELP_7k20;
2411 0 : move16();
2412 0 : move32();
2413 : }
2414 : ELSE
2415 : {
2416 56138 : st_ivas->hEncoderConfig->Opt_SC_VBR = 0;
2417 56138 : move16();
2418 : }
2419 :
2420 : /* check if the entered bitrate is supported */
2421 56138 : IF( hEncoderConfig->element_mode_init > EVS_MONO )
2422 : {
2423 56138 : IF( !is_IVAS_bitrate_fx( hEncoderConfig->ivas_total_brate ) )
2424 : {
2425 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Incorrect bitrate specification in IVAS: %d", hEncoderConfig->ivas_total_brate );
2426 : }
2427 : }
2428 : ELSE
2429 : {
2430 0 : IF( !is_EVS_bitrate( hEncoderConfig->ivas_total_brate, &hEncoderConfig->Opt_AMR_WB ) )
2431 : {
2432 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Incorrect bitrate specification in EVS mono: %d", hEncoderConfig->ivas_total_brate );
2433 : }
2434 :
2435 : /* in case of 8kHz signal, limit the total bitrate to 24.40 kbps */
2436 0 : test();
2437 0 : IF( EQ_32( hEncoderConfig->input_Fs, 8000 ) && GT_32( hEncoderConfig->ivas_total_brate, ACELP_24k40 ) )
2438 : {
2439 0 : hEncoderConfig->ivas_total_brate = ACELP_24k40;
2440 0 : move32();
2441 : }
2442 : }
2443 :
2444 56138 : IF( EQ_16( hEncoderConfig->ivas_format, ISM_FORMAT ) )
2445 : {
2446 10086 : IF( ( error = sanitizeBitrateISM_fx( hEncoderConfig, hIvasEnc->extMetadataApi ) ) != IVAS_ERR_OK )
2447 : {
2448 0 : return error;
2449 : }
2450 : }
2451 :
2452 56138 : st_ivas->codec_mode = MODE1;
2453 56138 : move16();
2454 :
2455 56138 : IF( hEncoderConfig->element_mode_init == EVS_MONO )
2456 : {
2457 0 : IF( hEncoderConfig->Opt_AMR_WB )
2458 : {
2459 0 : st_ivas->codec_mode = MODE1;
2460 0 : move16();
2461 : }
2462 : ELSE
2463 : {
2464 0 : st_ivas->codec_mode = get_codec_mode( hEncoderConfig->ivas_total_brate );
2465 0 : move16();
2466 : }
2467 : }
2468 :
2469 56138 : return IVAS_ERR_OK;
2470 : }
2471 :
2472 :
2473 : /*---------------------------------------------------------------------*
2474 : * setChannelAwareConfig()
2475 : *
2476 : *
2477 : *---------------------------------------------------------------------*/
2478 :
2479 624 : static ivas_error setChannelAwareConfig_fx(
2480 : IVAS_ENC_HANDLE hIvasEnc,
2481 : const IVAS_ENC_CHANNEL_AWARE_CONFIG caConfig )
2482 : {
2483 : Word16 newFecIndicator;
2484 : ivas_error error;
2485 : Encoder_Struct *st_ivas;
2486 : ENCODER_CONFIG_HANDLE hEncoderConfig;
2487 :
2488 624 : st_ivas = hIvasEnc->st_ivas;
2489 624 : hEncoderConfig = st_ivas->hEncoderConfig;
2490 :
2491 : /* channel-aware mode is supported only at 13.20 kbps and with WB or SWB bandwidth */
2492 624 : test();
2493 624 : test();
2494 624 : test();
2495 624 : IF( ( caConfig.channelAwareModeEnabled && NE_32( st_ivas->hEncoderConfig->ivas_total_brate, ACELP_13k20 ) ) || ( hEncoderConfig->Opt_RF_ON && EQ_32( hEncoderConfig->input_Fs, 8000 ) ) )
2496 : {
2497 0 : hEncoderConfig->Opt_RF_ON = 0;
2498 0 : move16();
2499 0 : hEncoderConfig->rf_fec_offset = 0;
2500 0 : move16();
2501 0 : return IVAS_ERR_OK;
2502 : }
2503 :
2504 624 : IF( caConfig.channelAwareModeEnabled )
2505 : {
2506 0 : hEncoderConfig->Opt_RF_ON = 1;
2507 0 : move16();
2508 :
2509 : /* Convert FEC indicator from API type */
2510 0 : IF( ( error = fecIndicatorApiToInternal( caConfig.fec_indicator, &newFecIndicator ) ) != IVAS_ERR_OK )
2511 : {
2512 0 : return error;
2513 : }
2514 :
2515 : /* Set new values only if they differ from current values */
2516 0 : test();
2517 0 : IF( ( NE_16( newFecIndicator, hEncoderConfig->rf_fec_indicator ) || NE_16( caConfig.fec_offset, hEncoderConfig->rf_fec_offset ) ) )
2518 : {
2519 0 : hEncoderConfig->rf_fec_indicator = newFecIndicator;
2520 0 : move16();
2521 :
2522 : /* Check if new FEC offset has a valid value */
2523 0 : test();
2524 0 : test();
2525 0 : test();
2526 0 : test();
2527 0 : IF( EQ_16( caConfig.fec_offset, 0 ) || EQ_16( caConfig.fec_offset, 2 ) || EQ_16( caConfig.fec_offset, 3 ) || EQ_16( caConfig.fec_offset, 5 ) || EQ_16( caConfig.fec_offset, 7 ) )
2528 : {
2529 0 : hEncoderConfig->rf_fec_offset = caConfig.fec_offset;
2530 0 : move16();
2531 : }
2532 : ELSE
2533 : {
2534 0 : return IVAS_ERR_INVALID_FEC_OFFSET;
2535 : }
2536 :
2537 0 : hIvasEnc->switchingActive = true;
2538 : }
2539 :
2540 : /* Save a copy of FEC offset value - needed during encoding */
2541 0 : hIvasEnc->rf_fec_offset_loc = hEncoderConfig->rf_fec_offset;
2542 0 : move16();
2543 : }
2544 : ELSE
2545 : {
2546 624 : hEncoderConfig->Opt_RF_ON = 0;
2547 624 : move16();
2548 : }
2549 :
2550 624 : return IVAS_ERR_OK;
2551 : }
2552 : /*---------------------------------------------------------------------*
2553 : * doCommonConfigureChecks()
2554 : *
2555 : *
2556 : *---------------------------------------------------------------------*/
2557 :
2558 624 : static ivas_error doCommonConfigureChecks(
2559 : IVAS_ENC_HANDLE hIvasEnc )
2560 : {
2561 624 : test();
2562 624 : IF( hIvasEnc == NULL || hIvasEnc->st_ivas == NULL )
2563 : {
2564 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2565 : }
2566 :
2567 624 : IF( hIvasEnc->isConfigured )
2568 : {
2569 0 : return IVAS_ERR_RECONFIGURE_NOT_SUPPORTED;
2570 : }
2571 :
2572 624 : return IVAS_ERR_OK;
2573 : }
2574 :
2575 :
2576 : /*---------------------------------------------------------------------*
2577 : * doCommonSetterChecks()
2578 : *
2579 : *
2580 : *---------------------------------------------------------------------*/
2581 :
2582 74288 : static ivas_error doCommonSetterChecks(
2583 : IVAS_ENC_HANDLE hIvasEnc )
2584 : {
2585 74288 : test();
2586 74288 : IF( hIvasEnc == NULL || hIvasEnc->st_ivas == NULL )
2587 : {
2588 0 : return IVAS_ERR_UNEXPECTED_NULL_POINTER;
2589 : }
2590 :
2591 : /* Currently settings can be changed only after configuration step */
2592 74288 : IF( !hIvasEnc->isConfigured )
2593 : {
2594 0 : return IVAS_ERR_NOT_CONFIGURED;
2595 : }
2596 :
2597 74288 : return IVAS_ERR_OK;
2598 : }
2599 :
2600 :
2601 : /*---------------------------------------------------------------------*
2602 : * sanitizeBandwidth()
2603 : *
2604 : *
2605 : *---------------------------------------------------------------------*/
2606 :
2607 425012 : static ivas_error sanitizeBandwidth_fx(
2608 : const IVAS_ENC_HANDLE hIvasEnc )
2609 : {
2610 : ENCODER_CONFIG_HANDLE hEncoderConfig;
2611 : Word16 max_bwidth_tmp;
2612 :
2613 425012 : hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
2614 :
2615 425012 : max_bwidth_tmp = hIvasEnc->newBandwidthApi;
2616 :
2617 : /* Prevent st_ivas->max_bwidth from being higher than Fs/2 */
2618 425012 : test();
2619 425012 : test();
2620 425012 : test();
2621 425012 : IF( EQ_32( hEncoderConfig->input_Fs, 8000 ) && ( max_bwidth_tmp > NB ) )
2622 : {
2623 0 : max_bwidth_tmp = NB;
2624 0 : move16();
2625 : }
2626 425012 : ELSE IF( EQ_32( hEncoderConfig->input_Fs, 16000 ) && GT_16( max_bwidth_tmp, WB ) )
2627 : {
2628 0 : max_bwidth_tmp = WB;
2629 0 : move16();
2630 : }
2631 425012 : ELSE IF( EQ_32( hEncoderConfig->input_Fs, 32000 ) && GT_16( max_bwidth_tmp, SWB ) )
2632 : {
2633 1351 : max_bwidth_tmp = SWB;
2634 1351 : move16();
2635 : }
2636 :
2637 : /* NB coding not supported in IVAS. Switching to WB. */
2638 425012 : test();
2639 425012 : test();
2640 425012 : IF( ( max_bwidth_tmp == NB ) && NE_16( hEncoderConfig->ivas_format, UNDEFINED_FORMAT ) && NE_16( hEncoderConfig->ivas_format, MONO_FORMAT ) )
2641 : {
2642 301 : IF( GE_32( hEncoderConfig->input_Fs, 16000 ) )
2643 : {
2644 301 : max_bwidth_tmp = WB;
2645 301 : move16();
2646 : }
2647 : ELSE
2648 : {
2649 0 : return IVAS_ERR_INVALID_BITRATE;
2650 : }
2651 : }
2652 :
2653 425012 : IF( NE_16( hEncoderConfig->ivas_format, MONO_FORMAT ) )
2654 : {
2655 421909 : Word32 quo = 0, rem;
2656 421909 : move32();
2657 421909 : IF( EQ_16( hEncoderConfig->ivas_format, ISM_FORMAT ) )
2658 : {
2659 103900 : iDiv_and_mod_32( hEncoderConfig->ivas_total_brate, hEncoderConfig->nchan_ism, &quo, &rem, 0 );
2660 : }
2661 :
2662 421909 : test();
2663 421909 : test();
2664 421909 : test();
2665 421909 : test();
2666 421909 : IF( EQ_16( max_bwidth_tmp, FB ) && ( ( NE_16( hEncoderConfig->ivas_format, ISM_FORMAT ) && LT_32( hEncoderConfig->ivas_total_brate, MIN_BRATE_FB_STEREO ) ) ||
2667 : ( EQ_16( hEncoderConfig->ivas_format, ISM_FORMAT ) && LT_32( quo, MIN_BRATE_FB_ISM ) ) ) )
2668 : {
2669 60842 : max_bwidth_tmp = SWB;
2670 60842 : move16();
2671 : }
2672 : }
2673 :
2674 425012 : IF( NE_16( hEncoderConfig->max_bwidth, max_bwidth_tmp ) )
2675 : {
2676 4978 : hEncoderConfig->max_bwidth = max_bwidth_tmp;
2677 4978 : move16();
2678 4978 : hIvasEnc->switchingActive = true;
2679 : }
2680 :
2681 425012 : return IVAS_ERR_OK;
2682 : }
2683 : /*---------------------------------------------------------------------*
2684 : * sanitizeBitrateISM_fx()
2685 : *
2686 : *
2687 : *---------------------------------------------------------------------*/
2688 :
2689 10160 : static ivas_error sanitizeBitrateISM_fx(
2690 : const ENCODER_CONFIG_HANDLE hEncoderConfig,
2691 : const bool extMetadataApi )
2692 : {
2693 10160 : test();
2694 10160 : IF( GT_32( hEncoderConfig->ivas_total_brate, IVAS_128k ) && EQ_16( hEncoderConfig->nchan_inp, 1 ) )
2695 : {
2696 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for 1 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
2697 : }
2698 :
2699 10160 : test();
2700 10160 : IF( GT_32( hEncoderConfig->ivas_total_brate, IVAS_256k ) && EQ_16( hEncoderConfig->nchan_inp, 2 ) )
2701 : {
2702 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for 2 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
2703 : }
2704 :
2705 10160 : test();
2706 10160 : IF( GT_32( hEncoderConfig->ivas_total_brate, IVAS_384k ) && EQ_16( hEncoderConfig->nchan_inp, 3 ) )
2707 : {
2708 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for 3 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
2709 : }
2710 :
2711 10160 : test();
2712 10160 : IF( LT_32( hEncoderConfig->ivas_total_brate, IVAS_16k4 ) && EQ_16( hEncoderConfig->nchan_inp, 2 ) )
2713 : {
2714 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too low bitrate for 2 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
2715 : }
2716 :
2717 10160 : test();
2718 10160 : IF( LT_32( hEncoderConfig->ivas_total_brate, IVAS_24k4 ) && EQ_16( hEncoderConfig->nchan_inp, 3 ) )
2719 : {
2720 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too low bitrate for 3 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
2721 : }
2722 :
2723 10160 : test();
2724 10160 : IF( LT_32( hEncoderConfig->ivas_total_brate, IVAS_24k4 ) && EQ_16( hEncoderConfig->nchan_inp, 4 ) )
2725 : {
2726 0 : return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too low bitrate for 4 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
2727 : }
2728 :
2729 10160 : IF( extMetadataApi )
2730 : {
2731 1007 : hEncoderConfig->ism_extended_metadata_flag = (Word16) GE_32( hEncoderConfig->ivas_total_brate, ISM_EXTENDED_METADATA_BRATE );
2732 1007 : move16();
2733 : }
2734 : ELSE
2735 : {
2736 9153 : hEncoderConfig->ism_extended_metadata_flag = 0;
2737 9153 : move16();
2738 : }
2739 :
2740 10160 : return IVAS_ERR_OK;
2741 : }
2742 : /*---------------------------------------------------------------------*
2743 : * setBandwidth_fx()
2744 : *
2745 : *
2746 : *---------------------------------------------------------------------*/
2747 :
2748 18774 : static ivas_error setBandwidth_fx(
2749 : IVAS_ENC_HANDLE hIvasEnc,
2750 : const IVAS_ENC_BANDWIDTH maxBandwidth )
2751 : {
2752 : ivas_error error;
2753 : Word16 newBandwidth;
2754 : ENCODER_CONFIG_HANDLE hEncoderConfig;
2755 :
2756 18774 : hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
2757 :
2758 : /* Convert bandwidth from API type */
2759 18774 : IF( ( error = bandwidthApiToInternal( maxBandwidth, &newBandwidth ) ) != IVAS_ERR_OK )
2760 : {
2761 0 : return error;
2762 : }
2763 :
2764 18774 : hIvasEnc->newBandwidthApi = newBandwidth;
2765 :
2766 : /* NB coding not supported in IVAS. Switching to WB. */
2767 18774 : test();
2768 18774 : test();
2769 18774 : IF( ( newBandwidth == NB ) && NE_16( hEncoderConfig->ivas_format, UNDEFINED_FORMAT ) && NE_16( hEncoderConfig->ivas_format, MONO_FORMAT ) )
2770 : {
2771 301 : newBandwidth = WB;
2772 301 : move16();
2773 : }
2774 :
2775 18774 : IF( hEncoderConfig->max_bwidth != newBandwidth )
2776 : {
2777 3401 : hEncoderConfig->max_bwidth = newBandwidth;
2778 3401 : hIvasEnc->switchingActive = true;
2779 3401 : move16();
2780 : }
2781 :
2782 18774 : return IVAS_ERR_OK;
2783 : }
2784 :
2785 : /*---------------------------------------------------------------------*
2786 : * resetIsmMetadataProvidedFlags()
2787 : *
2788 : *
2789 : *---------------------------------------------------------------------*/
2790 :
2791 104450 : static void resetIsmMetadataProvidedFlags(
2792 : IVAS_ENC_HANDLE hIvasEnc )
2793 : {
2794 : Word16 i;
2795 :
2796 522250 : FOR( i = 0; i < MAX_NUM_OBJECTS; ++i )
2797 : {
2798 417800 : hIvasEnc->ismMetadataProvided[i] = false;
2799 : }
2800 :
2801 104450 : return;
2802 : }
2803 :
2804 :
2805 : /*---------------------------------------------------------------------*
2806 : * bandwidthApiToInternal()
2807 : *
2808 : *
2809 : *---------------------------------------------------------------------*/
2810 :
2811 19398 : static ivas_error bandwidthApiToInternal(
2812 : const IVAS_ENC_BANDWIDTH maxBandwidth,
2813 : Word16 *internalMaxBandwidth )
2814 : {
2815 19398 : SWITCH( maxBandwidth )
2816 : {
2817 302 : case IVAS_ENC_BANDWIDTH_NB:
2818 302 : *internalMaxBandwidth = NB;
2819 302 : BREAK;
2820 6316 : case IVAS_ENC_BANDWIDTH_WB:
2821 6316 : *internalMaxBandwidth = WB;
2822 6316 : BREAK;
2823 6306 : case IVAS_ENC_BANDWIDTH_SWB:
2824 6306 : *internalMaxBandwidth = SWB;
2825 6306 : BREAK;
2826 6474 : case IVAS_ENC_BANDWIDTH_FB:
2827 6474 : *internalMaxBandwidth = FB;
2828 6474 : BREAK;
2829 0 : case IVAS_ENC_BANDWIDTH_UNDEFINED:
2830 : default:
2831 0 : return IVAS_ERR_INVALID_BANDWIDTH;
2832 : }
2833 :
2834 19398 : return IVAS_ERR_OK;
2835 : }
2836 :
2837 :
2838 : /*---------------------------------------------------------------------*
2839 : * fecIndicatorApiToInternal()
2840 : *
2841 : *
2842 : *---------------------------------------------------------------------*/
2843 :
2844 0 : static ivas_error fecIndicatorApiToInternal(
2845 : const IVAS_ENC_FEC_INDICATOR fecIndicator,
2846 : Word16 *fecIndicatorInternal )
2847 : {
2848 0 : SWITCH( fecIndicator )
2849 : {
2850 0 : case IVAS_ENC_FEC_LO:
2851 0 : *fecIndicatorInternal = 0;
2852 0 : BREAK;
2853 0 : case IVAS_ENC_FEC_HI:
2854 0 : *fecIndicatorInternal = 1;
2855 0 : BREAK;
2856 0 : default:
2857 0 : return IVAS_ERR_INTERNAL;
2858 : }
2859 :
2860 0 : return IVAS_ERR_OK;
2861 : }
2862 :
2863 : #ifdef DEBUGGING
2864 : /*---------------------------------------------------------------------*
2865 : * forcedModeApiToInternal()
2866 : *
2867 : *
2868 : *---------------------------------------------------------------------*/
2869 :
2870 : static ivas_error forcedModeApiToInternal(
2871 : IVAS_ENC_FORCED_MODE forcedMode,
2872 : int16_t *forcedModeInternal )
2873 : {
2874 : switch ( forcedMode )
2875 : {
2876 : case IVAS_ENC_FORCE_SPEECH:
2877 : *forcedModeInternal = FORCE_SPEECH;
2878 : break;
2879 : case IVAS_ENC_FORCE_MUSIC:
2880 : *forcedModeInternal = FORCE_MUSIC;
2881 : break;
2882 : case IVAS_ENC_FORCE_ACELP:
2883 : *forcedModeInternal = FORCE_ACELP;
2884 : break;
2885 : case IVAS_ENC_FORCE_GSC:
2886 : *forcedModeInternal = FORCE_GSC;
2887 : break;
2888 : #ifdef SUPPORT_FORCE_TCX10_TCX20
2889 : case IVAS_ENC_FORCE_TCX10:
2890 : *forcedModeInternal = FORCE_TCX10;
2891 : break;
2892 : case IVAS_ENC_FORCE_TCX20:
2893 : *forcedModeInternal = FORCE_TCX20;
2894 : break;
2895 : #else
2896 : case IVAS_ENC_FORCE_TCX:
2897 : *forcedModeInternal = FORCE_TCX;
2898 : break;
2899 : #endif
2900 : case IVAS_ENC_FORCE_HQ:
2901 : *forcedModeInternal = FORCE_HQ;
2902 : break;
2903 : case IVAS_ENC_FORCE_UNFORCED:
2904 : *forcedModeInternal = -1;
2905 : break;
2906 : default:
2907 : return IVAS_ERR_INVALID_FORCE_MODE;
2908 : break;
2909 : }
2910 :
2911 : return IVAS_ERR_OK;
2912 : }
2913 : #endif
2914 :
2915 : /*---------------------------------------------------------------------*
2916 : * IVAS_ENC_PrintConfig()
2917 : *
2918 : *
2919 : *---------------------------------------------------------------------*/
2920 :
2921 624 : ivas_error IVAS_ENC_PrintConfig(
2922 : const IVAS_ENC_HANDLE hIvasEnc, /* i : IVAS encoder handle */
2923 : const Word16 channelAwareModeEnabled /* i : channel-aware mode enabled flag */
2924 : )
2925 : {
2926 624 : return printConfigInfo_enc( hIvasEnc, channelAwareModeEnabled );
2927 : }
2928 :
2929 :
2930 : /*---------------------------------------------------------------------*
2931 : * IVAS_ENC_PrintDisclaimer()
2932 : *
2933 : * Print IVAS disclaimer to console
2934 : *---------------------------------------------------------------------*/
2935 :
2936 624 : void IVAS_ENC_PrintDisclaimer( void )
2937 : {
2938 624 : print_disclaimer( stderr );
2939 :
2940 624 : return;
2941 : }
2942 :
2943 :
2944 : /*---------------------------------------------------------------------*
2945 : * init_encoder_config()
2946 : *
2947 : * Initialize Encoder Config. handle
2948 : *---------------------------------------------------------------------*/
2949 :
2950 624 : static void init_encoder_config(
2951 : ENCODER_CONFIG_HANDLE hEncoderConfig /* o : configuration structure */
2952 : )
2953 : {
2954 624 : hEncoderConfig->ivas_total_brate = ACELP_12k65;
2955 624 : move32();
2956 624 : hEncoderConfig->max_bwidth = SWB;
2957 624 : move16();
2958 624 : hEncoderConfig->input_Fs = 16000;
2959 624 : move32();
2960 624 : hEncoderConfig->nchan_inp = 1;
2961 624 : move16();
2962 624 : hEncoderConfig->element_mode_init = EVS_MONO;
2963 624 : move16();
2964 624 : hEncoderConfig->ivas_format = UNDEFINED_FORMAT;
2965 624 : move16();
2966 624 : hEncoderConfig->is_binaural = 0;
2967 624 : move16();
2968 624 : hEncoderConfig->Opt_SC_VBR = 0;
2969 624 : move16();
2970 624 : hEncoderConfig->last_Opt_SC_VBR = 0;
2971 624 : move16();
2972 624 : hEncoderConfig->Opt_AMR_WB = 0;
2973 624 : move16();
2974 624 : hEncoderConfig->Opt_DTX_ON = 0;
2975 624 : move16();
2976 624 : hEncoderConfig->Opt_RF_ON = 0;
2977 624 : move16();
2978 624 : hEncoderConfig->rf_fec_offset = 0;
2979 624 : move16();
2980 624 : hEncoderConfig->rf_fec_indicator = 1;
2981 624 : move16();
2982 624 : hEncoderConfig->interval_SID = FIXED_SID_RATE;
2983 624 : move16();
2984 624 : hEncoderConfig->var_SID_rate_flag = 1;
2985 624 : move16();
2986 624 : hEncoderConfig->mc_input_setup = MC_LS_SETUP_INVALID;
2987 624 : move16();
2988 624 : hEncoderConfig->stereo_dmx_evs = 0;
2989 624 : move16();
2990 624 : hEncoderConfig->nchan_ism = 0;
2991 624 : move16();
2992 624 : hEncoderConfig->sba_order = 0;
2993 624 : move16();
2994 624 : hEncoderConfig->sba_planar = 0;
2995 624 : move16();
2996 624 : hEncoderConfig->ism_extended_metadata_flag = 0;
2997 624 : move16();
2998 : #ifdef DEBUGGING
2999 : hEncoderConfig->stereo_mode_cmdl = 0;
3000 : hEncoderConfig->force = -1;
3001 : hEncoderConfig->mdct_stereo_mode_cmdl = SMDCT_MS_DECISION;
3002 : #endif
3003 624 : hEncoderConfig->Opt_PCA_ON = 0;
3004 624 : move16();
3005 :
3006 624 : return;
3007 : }
|